mthca_qp.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  5. * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. *
  35. * $Id: mthca_qp.c 1355 2004-12-17 15:23:43Z roland $
  36. */
  37. #include <linux/init.h>
  38. #include <ib_verbs.h>
  39. #include <ib_cache.h>
  40. #include <ib_pack.h>
  41. #include "mthca_dev.h"
  42. #include "mthca_cmd.h"
  43. #include "mthca_memfree.h"
  44. enum {
  45. MTHCA_MAX_DIRECT_QP_SIZE = 4 * PAGE_SIZE,
  46. MTHCA_ACK_REQ_FREQ = 10,
  47. MTHCA_FLIGHT_LIMIT = 9,
  48. MTHCA_UD_HEADER_SIZE = 72, /* largest UD header possible */
  49. MTHCA_INLINE_HEADER_SIZE = 4, /* data segment overhead for inline */
  50. MTHCA_INLINE_CHUNK_SIZE = 16 /* inline data segment chunk */
  51. };
  52. enum {
  53. MTHCA_QP_STATE_RST = 0,
  54. MTHCA_QP_STATE_INIT = 1,
  55. MTHCA_QP_STATE_RTR = 2,
  56. MTHCA_QP_STATE_RTS = 3,
  57. MTHCA_QP_STATE_SQE = 4,
  58. MTHCA_QP_STATE_SQD = 5,
  59. MTHCA_QP_STATE_ERR = 6,
  60. MTHCA_QP_STATE_DRAINING = 7
  61. };
  62. enum {
  63. MTHCA_QP_ST_RC = 0x0,
  64. MTHCA_QP_ST_UC = 0x1,
  65. MTHCA_QP_ST_RD = 0x2,
  66. MTHCA_QP_ST_UD = 0x3,
  67. MTHCA_QP_ST_MLX = 0x7
  68. };
  69. enum {
  70. MTHCA_QP_PM_MIGRATED = 0x3,
  71. MTHCA_QP_PM_ARMED = 0x0,
  72. MTHCA_QP_PM_REARM = 0x1
  73. };
  74. enum {
  75. /* qp_context flags */
  76. MTHCA_QP_BIT_DE = 1 << 8,
  77. /* params1 */
  78. MTHCA_QP_BIT_SRE = 1 << 15,
  79. MTHCA_QP_BIT_SWE = 1 << 14,
  80. MTHCA_QP_BIT_SAE = 1 << 13,
  81. MTHCA_QP_BIT_SIC = 1 << 4,
  82. MTHCA_QP_BIT_SSC = 1 << 3,
  83. /* params2 */
  84. MTHCA_QP_BIT_RRE = 1 << 15,
  85. MTHCA_QP_BIT_RWE = 1 << 14,
  86. MTHCA_QP_BIT_RAE = 1 << 13,
  87. MTHCA_QP_BIT_RIC = 1 << 4,
  88. MTHCA_QP_BIT_RSC = 1 << 3
  89. };
  90. struct mthca_qp_path {
  91. u32 port_pkey;
  92. u8 rnr_retry;
  93. u8 g_mylmc;
  94. u16 rlid;
  95. u8 ackto;
  96. u8 mgid_index;
  97. u8 static_rate;
  98. u8 hop_limit;
  99. u32 sl_tclass_flowlabel;
  100. u8 rgid[16];
  101. } __attribute__((packed));
  102. struct mthca_qp_context {
  103. u32 flags;
  104. u32 tavor_sched_queue; /* Reserved on Arbel */
  105. u8 mtu_msgmax;
  106. u8 rq_size_stride; /* Reserved on Tavor */
  107. u8 sq_size_stride; /* Reserved on Tavor */
  108. u8 rlkey_arbel_sched_queue; /* Reserved on Tavor */
  109. u32 usr_page;
  110. u32 local_qpn;
  111. u32 remote_qpn;
  112. u32 reserved1[2];
  113. struct mthca_qp_path pri_path;
  114. struct mthca_qp_path alt_path;
  115. u32 rdd;
  116. u32 pd;
  117. u32 wqe_base;
  118. u32 wqe_lkey;
  119. u32 params1;
  120. u32 reserved2;
  121. u32 next_send_psn;
  122. u32 cqn_snd;
  123. u32 snd_wqe_base_l; /* Next send WQE on Tavor */
  124. u32 snd_db_index; /* (debugging only entries) */
  125. u32 last_acked_psn;
  126. u32 ssn;
  127. u32 params2;
  128. u32 rnr_nextrecvpsn;
  129. u32 ra_buff_indx;
  130. u32 cqn_rcv;
  131. u32 rcv_wqe_base_l; /* Next recv WQE on Tavor */
  132. u32 rcv_db_index; /* (debugging only entries) */
  133. u32 qkey;
  134. u32 srqn;
  135. u32 rmsn;
  136. u16 rq_wqe_counter; /* reserved on Tavor */
  137. u16 sq_wqe_counter; /* reserved on Tavor */
  138. u32 reserved3[18];
  139. } __attribute__((packed));
  140. struct mthca_qp_param {
  141. u32 opt_param_mask;
  142. u32 reserved1;
  143. struct mthca_qp_context context;
  144. u32 reserved2[62];
  145. } __attribute__((packed));
  146. enum {
  147. MTHCA_QP_OPTPAR_ALT_ADDR_PATH = 1 << 0,
  148. MTHCA_QP_OPTPAR_RRE = 1 << 1,
  149. MTHCA_QP_OPTPAR_RAE = 1 << 2,
  150. MTHCA_QP_OPTPAR_RWE = 1 << 3,
  151. MTHCA_QP_OPTPAR_PKEY_INDEX = 1 << 4,
  152. MTHCA_QP_OPTPAR_Q_KEY = 1 << 5,
  153. MTHCA_QP_OPTPAR_RNR_TIMEOUT = 1 << 6,
  154. MTHCA_QP_OPTPAR_PRIMARY_ADDR_PATH = 1 << 7,
  155. MTHCA_QP_OPTPAR_SRA_MAX = 1 << 8,
  156. MTHCA_QP_OPTPAR_RRA_MAX = 1 << 9,
  157. MTHCA_QP_OPTPAR_PM_STATE = 1 << 10,
  158. MTHCA_QP_OPTPAR_PORT_NUM = 1 << 11,
  159. MTHCA_QP_OPTPAR_RETRY_COUNT = 1 << 12,
  160. MTHCA_QP_OPTPAR_ALT_RNR_RETRY = 1 << 13,
  161. MTHCA_QP_OPTPAR_ACK_TIMEOUT = 1 << 14,
  162. MTHCA_QP_OPTPAR_RNR_RETRY = 1 << 15,
  163. MTHCA_QP_OPTPAR_SCHED_QUEUE = 1 << 16
  164. };
  165. enum {
  166. MTHCA_NEXT_DBD = 1 << 7,
  167. MTHCA_NEXT_FENCE = 1 << 6,
  168. MTHCA_NEXT_CQ_UPDATE = 1 << 3,
  169. MTHCA_NEXT_EVENT_GEN = 1 << 2,
  170. MTHCA_NEXT_SOLICIT = 1 << 1,
  171. MTHCA_MLX_VL15 = 1 << 17,
  172. MTHCA_MLX_SLR = 1 << 16
  173. };
  174. enum {
  175. MTHCA_INVAL_LKEY = 0x100
  176. };
  177. struct mthca_next_seg {
  178. u32 nda_op; /* [31:6] next WQE [4:0] next opcode */
  179. u32 ee_nds; /* [31:8] next EE [7] DBD [6] F [5:0] next WQE size */
  180. u32 flags; /* [3] CQ [2] Event [1] Solicit */
  181. u32 imm; /* immediate data */
  182. };
  183. struct mthca_tavor_ud_seg {
  184. u32 reserved1;
  185. u32 lkey;
  186. u64 av_addr;
  187. u32 reserved2[4];
  188. u32 dqpn;
  189. u32 qkey;
  190. u32 reserved3[2];
  191. };
  192. struct mthca_arbel_ud_seg {
  193. u32 av[8];
  194. u32 dqpn;
  195. u32 qkey;
  196. u32 reserved[2];
  197. };
  198. struct mthca_bind_seg {
  199. u32 flags; /* [31] Atomic [30] rem write [29] rem read */
  200. u32 reserved;
  201. u32 new_rkey;
  202. u32 lkey;
  203. u64 addr;
  204. u64 length;
  205. };
  206. struct mthca_raddr_seg {
  207. u64 raddr;
  208. u32 rkey;
  209. u32 reserved;
  210. };
  211. struct mthca_atomic_seg {
  212. u64 swap_add;
  213. u64 compare;
  214. };
  215. struct mthca_data_seg {
  216. u32 byte_count;
  217. u32 lkey;
  218. u64 addr;
  219. };
  220. struct mthca_mlx_seg {
  221. u32 nda_op;
  222. u32 nds;
  223. u32 flags; /* [17] VL15 [16] SLR [14:12] static rate
  224. [11:8] SL [3] C [2] E */
  225. u16 rlid;
  226. u16 vcrc;
  227. };
  228. static const u8 mthca_opcode[] = {
  229. [IB_WR_SEND] = MTHCA_OPCODE_SEND,
  230. [IB_WR_SEND_WITH_IMM] = MTHCA_OPCODE_SEND_IMM,
  231. [IB_WR_RDMA_WRITE] = MTHCA_OPCODE_RDMA_WRITE,
  232. [IB_WR_RDMA_WRITE_WITH_IMM] = MTHCA_OPCODE_RDMA_WRITE_IMM,
  233. [IB_WR_RDMA_READ] = MTHCA_OPCODE_RDMA_READ,
  234. [IB_WR_ATOMIC_CMP_AND_SWP] = MTHCA_OPCODE_ATOMIC_CS,
  235. [IB_WR_ATOMIC_FETCH_AND_ADD] = MTHCA_OPCODE_ATOMIC_FA,
  236. };
  237. static int is_sqp(struct mthca_dev *dev, struct mthca_qp *qp)
  238. {
  239. return qp->qpn >= dev->qp_table.sqp_start &&
  240. qp->qpn <= dev->qp_table.sqp_start + 3;
  241. }
  242. static int is_qp0(struct mthca_dev *dev, struct mthca_qp *qp)
  243. {
  244. return qp->qpn >= dev->qp_table.sqp_start &&
  245. qp->qpn <= dev->qp_table.sqp_start + 1;
  246. }
  247. static void *get_recv_wqe(struct mthca_qp *qp, int n)
  248. {
  249. if (qp->is_direct)
  250. return qp->queue.direct.buf + (n << qp->rq.wqe_shift);
  251. else
  252. return qp->queue.page_list[(n << qp->rq.wqe_shift) >> PAGE_SHIFT].buf +
  253. ((n << qp->rq.wqe_shift) & (PAGE_SIZE - 1));
  254. }
  255. static void *get_send_wqe(struct mthca_qp *qp, int n)
  256. {
  257. if (qp->is_direct)
  258. return qp->queue.direct.buf + qp->send_wqe_offset +
  259. (n << qp->sq.wqe_shift);
  260. else
  261. return qp->queue.page_list[(qp->send_wqe_offset +
  262. (n << qp->sq.wqe_shift)) >>
  263. PAGE_SHIFT].buf +
  264. ((qp->send_wqe_offset + (n << qp->sq.wqe_shift)) &
  265. (PAGE_SIZE - 1));
  266. }
  267. void mthca_qp_event(struct mthca_dev *dev, u32 qpn,
  268. enum ib_event_type event_type)
  269. {
  270. struct mthca_qp *qp;
  271. struct ib_event event;
  272. spin_lock(&dev->qp_table.lock);
  273. qp = mthca_array_get(&dev->qp_table.qp, qpn & (dev->limits.num_qps - 1));
  274. if (qp)
  275. atomic_inc(&qp->refcount);
  276. spin_unlock(&dev->qp_table.lock);
  277. if (!qp) {
  278. mthca_warn(dev, "Async event for bogus QP %08x\n", qpn);
  279. return;
  280. }
  281. event.device = &dev->ib_dev;
  282. event.event = event_type;
  283. event.element.qp = &qp->ibqp;
  284. if (qp->ibqp.event_handler)
  285. qp->ibqp.event_handler(&event, qp->ibqp.qp_context);
  286. if (atomic_dec_and_test(&qp->refcount))
  287. wake_up(&qp->wait);
  288. }
  289. static int to_mthca_state(enum ib_qp_state ib_state)
  290. {
  291. switch (ib_state) {
  292. case IB_QPS_RESET: return MTHCA_QP_STATE_RST;
  293. case IB_QPS_INIT: return MTHCA_QP_STATE_INIT;
  294. case IB_QPS_RTR: return MTHCA_QP_STATE_RTR;
  295. case IB_QPS_RTS: return MTHCA_QP_STATE_RTS;
  296. case IB_QPS_SQD: return MTHCA_QP_STATE_SQD;
  297. case IB_QPS_SQE: return MTHCA_QP_STATE_SQE;
  298. case IB_QPS_ERR: return MTHCA_QP_STATE_ERR;
  299. default: return -1;
  300. }
  301. }
  302. enum { RC, UC, UD, RD, RDEE, MLX, NUM_TRANS };
  303. static int to_mthca_st(int transport)
  304. {
  305. switch (transport) {
  306. case RC: return MTHCA_QP_ST_RC;
  307. case UC: return MTHCA_QP_ST_UC;
  308. case UD: return MTHCA_QP_ST_UD;
  309. case RD: return MTHCA_QP_ST_RD;
  310. case MLX: return MTHCA_QP_ST_MLX;
  311. default: return -1;
  312. }
  313. }
  314. static const struct {
  315. int trans;
  316. u32 req_param[NUM_TRANS];
  317. u32 opt_param[NUM_TRANS];
  318. } state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
  319. [IB_QPS_RESET] = {
  320. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  321. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
  322. [IB_QPS_INIT] = {
  323. .trans = MTHCA_TRANS_RST2INIT,
  324. .req_param = {
  325. [UD] = (IB_QP_PKEY_INDEX |
  326. IB_QP_PORT |
  327. IB_QP_QKEY),
  328. [UC] = (IB_QP_PKEY_INDEX |
  329. IB_QP_PORT |
  330. IB_QP_ACCESS_FLAGS),
  331. [RC] = (IB_QP_PKEY_INDEX |
  332. IB_QP_PORT |
  333. IB_QP_ACCESS_FLAGS),
  334. [MLX] = (IB_QP_PKEY_INDEX |
  335. IB_QP_QKEY),
  336. },
  337. /* bug-for-bug compatibility with VAPI: */
  338. .opt_param = {
  339. [MLX] = IB_QP_PORT
  340. }
  341. },
  342. },
  343. [IB_QPS_INIT] = {
  344. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  345. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
  346. [IB_QPS_INIT] = {
  347. .trans = MTHCA_TRANS_INIT2INIT,
  348. .opt_param = {
  349. [UD] = (IB_QP_PKEY_INDEX |
  350. IB_QP_PORT |
  351. IB_QP_QKEY),
  352. [UC] = (IB_QP_PKEY_INDEX |
  353. IB_QP_PORT |
  354. IB_QP_ACCESS_FLAGS),
  355. [RC] = (IB_QP_PKEY_INDEX |
  356. IB_QP_PORT |
  357. IB_QP_ACCESS_FLAGS),
  358. [MLX] = (IB_QP_PKEY_INDEX |
  359. IB_QP_QKEY),
  360. }
  361. },
  362. [IB_QPS_RTR] = {
  363. .trans = MTHCA_TRANS_INIT2RTR,
  364. .req_param = {
  365. [UC] = (IB_QP_AV |
  366. IB_QP_PATH_MTU |
  367. IB_QP_DEST_QPN |
  368. IB_QP_RQ_PSN |
  369. IB_QP_MAX_DEST_RD_ATOMIC),
  370. [RC] = (IB_QP_AV |
  371. IB_QP_PATH_MTU |
  372. IB_QP_DEST_QPN |
  373. IB_QP_RQ_PSN |
  374. IB_QP_MAX_DEST_RD_ATOMIC |
  375. IB_QP_MIN_RNR_TIMER),
  376. },
  377. .opt_param = {
  378. [UD] = (IB_QP_PKEY_INDEX |
  379. IB_QP_QKEY),
  380. [UC] = (IB_QP_ALT_PATH |
  381. IB_QP_ACCESS_FLAGS |
  382. IB_QP_PKEY_INDEX),
  383. [RC] = (IB_QP_ALT_PATH |
  384. IB_QP_ACCESS_FLAGS |
  385. IB_QP_PKEY_INDEX),
  386. [MLX] = (IB_QP_PKEY_INDEX |
  387. IB_QP_QKEY),
  388. }
  389. }
  390. },
  391. [IB_QPS_RTR] = {
  392. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  393. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
  394. [IB_QPS_RTS] = {
  395. .trans = MTHCA_TRANS_RTR2RTS,
  396. .req_param = {
  397. [UD] = IB_QP_SQ_PSN,
  398. [UC] = (IB_QP_SQ_PSN |
  399. IB_QP_MAX_QP_RD_ATOMIC),
  400. [RC] = (IB_QP_TIMEOUT |
  401. IB_QP_RETRY_CNT |
  402. IB_QP_RNR_RETRY |
  403. IB_QP_SQ_PSN |
  404. IB_QP_MAX_QP_RD_ATOMIC),
  405. [MLX] = IB_QP_SQ_PSN,
  406. },
  407. .opt_param = {
  408. [UD] = (IB_QP_CUR_STATE |
  409. IB_QP_QKEY),
  410. [UC] = (IB_QP_CUR_STATE |
  411. IB_QP_ALT_PATH |
  412. IB_QP_ACCESS_FLAGS |
  413. IB_QP_PKEY_INDEX |
  414. IB_QP_PATH_MIG_STATE),
  415. [RC] = (IB_QP_CUR_STATE |
  416. IB_QP_ALT_PATH |
  417. IB_QP_ACCESS_FLAGS |
  418. IB_QP_PKEY_INDEX |
  419. IB_QP_MIN_RNR_TIMER |
  420. IB_QP_PATH_MIG_STATE),
  421. [MLX] = (IB_QP_CUR_STATE |
  422. IB_QP_QKEY),
  423. }
  424. }
  425. },
  426. [IB_QPS_RTS] = {
  427. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  428. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
  429. [IB_QPS_RTS] = {
  430. .trans = MTHCA_TRANS_RTS2RTS,
  431. .opt_param = {
  432. [UD] = (IB_QP_CUR_STATE |
  433. IB_QP_QKEY),
  434. [UC] = (IB_QP_ACCESS_FLAGS |
  435. IB_QP_ALT_PATH |
  436. IB_QP_PATH_MIG_STATE),
  437. [RC] = (IB_QP_ACCESS_FLAGS |
  438. IB_QP_ALT_PATH |
  439. IB_QP_PATH_MIG_STATE |
  440. IB_QP_MIN_RNR_TIMER),
  441. [MLX] = (IB_QP_CUR_STATE |
  442. IB_QP_QKEY),
  443. }
  444. },
  445. [IB_QPS_SQD] = {
  446. .trans = MTHCA_TRANS_RTS2SQD,
  447. },
  448. },
  449. [IB_QPS_SQD] = {
  450. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  451. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
  452. [IB_QPS_RTS] = {
  453. .trans = MTHCA_TRANS_SQD2RTS,
  454. .opt_param = {
  455. [UD] = (IB_QP_CUR_STATE |
  456. IB_QP_QKEY),
  457. [UC] = (IB_QP_CUR_STATE |
  458. IB_QP_ALT_PATH |
  459. IB_QP_ACCESS_FLAGS |
  460. IB_QP_PATH_MIG_STATE),
  461. [RC] = (IB_QP_CUR_STATE |
  462. IB_QP_ALT_PATH |
  463. IB_QP_ACCESS_FLAGS |
  464. IB_QP_MIN_RNR_TIMER |
  465. IB_QP_PATH_MIG_STATE),
  466. [MLX] = (IB_QP_CUR_STATE |
  467. IB_QP_QKEY),
  468. }
  469. },
  470. [IB_QPS_SQD] = {
  471. .trans = MTHCA_TRANS_SQD2SQD,
  472. .opt_param = {
  473. [UD] = (IB_QP_PKEY_INDEX |
  474. IB_QP_QKEY),
  475. [UC] = (IB_QP_AV |
  476. IB_QP_MAX_QP_RD_ATOMIC |
  477. IB_QP_MAX_DEST_RD_ATOMIC |
  478. IB_QP_CUR_STATE |
  479. IB_QP_ALT_PATH |
  480. IB_QP_ACCESS_FLAGS |
  481. IB_QP_PKEY_INDEX |
  482. IB_QP_PATH_MIG_STATE),
  483. [RC] = (IB_QP_AV |
  484. IB_QP_TIMEOUT |
  485. IB_QP_RETRY_CNT |
  486. IB_QP_RNR_RETRY |
  487. IB_QP_MAX_QP_RD_ATOMIC |
  488. IB_QP_MAX_DEST_RD_ATOMIC |
  489. IB_QP_CUR_STATE |
  490. IB_QP_ALT_PATH |
  491. IB_QP_ACCESS_FLAGS |
  492. IB_QP_PKEY_INDEX |
  493. IB_QP_MIN_RNR_TIMER |
  494. IB_QP_PATH_MIG_STATE),
  495. [MLX] = (IB_QP_PKEY_INDEX |
  496. IB_QP_QKEY),
  497. }
  498. }
  499. },
  500. [IB_QPS_SQE] = {
  501. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  502. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
  503. [IB_QPS_RTS] = {
  504. .trans = MTHCA_TRANS_SQERR2RTS,
  505. .opt_param = {
  506. [UD] = (IB_QP_CUR_STATE |
  507. IB_QP_QKEY),
  508. [UC] = (IB_QP_CUR_STATE),
  509. [RC] = (IB_QP_CUR_STATE |
  510. IB_QP_MIN_RNR_TIMER),
  511. [MLX] = (IB_QP_CUR_STATE |
  512. IB_QP_QKEY),
  513. }
  514. }
  515. },
  516. [IB_QPS_ERR] = {
  517. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  518. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR }
  519. }
  520. };
  521. static void store_attrs(struct mthca_sqp *sqp, struct ib_qp_attr *attr,
  522. int attr_mask)
  523. {
  524. if (attr_mask & IB_QP_PKEY_INDEX)
  525. sqp->pkey_index = attr->pkey_index;
  526. if (attr_mask & IB_QP_QKEY)
  527. sqp->qkey = attr->qkey;
  528. if (attr_mask & IB_QP_SQ_PSN)
  529. sqp->send_psn = attr->sq_psn;
  530. }
  531. static void init_port(struct mthca_dev *dev, int port)
  532. {
  533. int err;
  534. u8 status;
  535. struct mthca_init_ib_param param;
  536. memset(&param, 0, sizeof param);
  537. param.enable_1x = 1;
  538. param.enable_4x = 1;
  539. param.vl_cap = dev->limits.vl_cap;
  540. param.mtu_cap = dev->limits.mtu_cap;
  541. param.gid_cap = dev->limits.gid_table_len;
  542. param.pkey_cap = dev->limits.pkey_table_len;
  543. err = mthca_INIT_IB(dev, &param, port, &status);
  544. if (err)
  545. mthca_warn(dev, "INIT_IB failed, return code %d.\n", err);
  546. if (status)
  547. mthca_warn(dev, "INIT_IB returned status %02x.\n", status);
  548. }
  549. int mthca_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask)
  550. {
  551. struct mthca_dev *dev = to_mdev(ibqp->device);
  552. struct mthca_qp *qp = to_mqp(ibqp);
  553. enum ib_qp_state cur_state, new_state;
  554. struct mthca_mailbox *mailbox;
  555. struct mthca_qp_param *qp_param;
  556. struct mthca_qp_context *qp_context;
  557. u32 req_param, opt_param;
  558. u8 status;
  559. int err;
  560. if (attr_mask & IB_QP_CUR_STATE) {
  561. if (attr->cur_qp_state != IB_QPS_RTR &&
  562. attr->cur_qp_state != IB_QPS_RTS &&
  563. attr->cur_qp_state != IB_QPS_SQD &&
  564. attr->cur_qp_state != IB_QPS_SQE)
  565. return -EINVAL;
  566. else
  567. cur_state = attr->cur_qp_state;
  568. } else {
  569. spin_lock_irq(&qp->sq.lock);
  570. spin_lock(&qp->rq.lock);
  571. cur_state = qp->state;
  572. spin_unlock(&qp->rq.lock);
  573. spin_unlock_irq(&qp->sq.lock);
  574. }
  575. if (attr_mask & IB_QP_STATE) {
  576. if (attr->qp_state < 0 || attr->qp_state > IB_QPS_ERR)
  577. return -EINVAL;
  578. new_state = attr->qp_state;
  579. } else
  580. new_state = cur_state;
  581. if (state_table[cur_state][new_state].trans == MTHCA_TRANS_INVALID) {
  582. mthca_dbg(dev, "Illegal QP transition "
  583. "%d->%d\n", cur_state, new_state);
  584. return -EINVAL;
  585. }
  586. req_param = state_table[cur_state][new_state].req_param[qp->transport];
  587. opt_param = state_table[cur_state][new_state].opt_param[qp->transport];
  588. if ((req_param & attr_mask) != req_param) {
  589. mthca_dbg(dev, "QP transition "
  590. "%d->%d missing req attr 0x%08x\n",
  591. cur_state, new_state,
  592. req_param & ~attr_mask);
  593. return -EINVAL;
  594. }
  595. if (attr_mask & ~(req_param | opt_param | IB_QP_STATE)) {
  596. mthca_dbg(dev, "QP transition (transport %d) "
  597. "%d->%d has extra attr 0x%08x\n",
  598. qp->transport,
  599. cur_state, new_state,
  600. attr_mask & ~(req_param | opt_param |
  601. IB_QP_STATE));
  602. return -EINVAL;
  603. }
  604. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  605. if (IS_ERR(mailbox))
  606. return PTR_ERR(mailbox);
  607. qp_param = mailbox->buf;
  608. qp_context = &qp_param->context;
  609. memset(qp_param, 0, sizeof *qp_param);
  610. qp_context->flags = cpu_to_be32((to_mthca_state(new_state) << 28) |
  611. (to_mthca_st(qp->transport) << 16));
  612. qp_context->flags |= cpu_to_be32(MTHCA_QP_BIT_DE);
  613. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  614. qp_context->flags |= cpu_to_be32(MTHCA_QP_PM_MIGRATED << 11);
  615. else {
  616. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_PM_STATE);
  617. switch (attr->path_mig_state) {
  618. case IB_MIG_MIGRATED:
  619. qp_context->flags |= cpu_to_be32(MTHCA_QP_PM_MIGRATED << 11);
  620. break;
  621. case IB_MIG_REARM:
  622. qp_context->flags |= cpu_to_be32(MTHCA_QP_PM_REARM << 11);
  623. break;
  624. case IB_MIG_ARMED:
  625. qp_context->flags |= cpu_to_be32(MTHCA_QP_PM_ARMED << 11);
  626. break;
  627. }
  628. }
  629. /* leave tavor_sched_queue as 0 */
  630. if (qp->transport == MLX || qp->transport == UD)
  631. qp_context->mtu_msgmax = (IB_MTU_2048 << 5) | 11;
  632. else if (attr_mask & IB_QP_PATH_MTU)
  633. qp_context->mtu_msgmax = (attr->path_mtu << 5) | 31;
  634. if (mthca_is_memfree(dev)) {
  635. qp_context->rq_size_stride =
  636. ((ffs(qp->rq.max) - 1) << 3) | (qp->rq.wqe_shift - 4);
  637. qp_context->sq_size_stride =
  638. ((ffs(qp->sq.max) - 1) << 3) | (qp->sq.wqe_shift - 4);
  639. }
  640. /* leave arbel_sched_queue as 0 */
  641. if (qp->ibqp.uobject)
  642. qp_context->usr_page =
  643. cpu_to_be32(to_mucontext(qp->ibqp.uobject->context)->uar.index);
  644. else
  645. qp_context->usr_page = cpu_to_be32(dev->driver_uar.index);
  646. qp_context->local_qpn = cpu_to_be32(qp->qpn);
  647. if (attr_mask & IB_QP_DEST_QPN) {
  648. qp_context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  649. }
  650. if (qp->transport == MLX)
  651. qp_context->pri_path.port_pkey |=
  652. cpu_to_be32(to_msqp(qp)->port << 24);
  653. else {
  654. if (attr_mask & IB_QP_PORT) {
  655. qp_context->pri_path.port_pkey |=
  656. cpu_to_be32(attr->port_num << 24);
  657. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_PORT_NUM);
  658. }
  659. }
  660. if (attr_mask & IB_QP_PKEY_INDEX) {
  661. qp_context->pri_path.port_pkey |=
  662. cpu_to_be32(attr->pkey_index);
  663. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_PKEY_INDEX);
  664. }
  665. if (attr_mask & IB_QP_RNR_RETRY) {
  666. qp_context->pri_path.rnr_retry = attr->rnr_retry << 5;
  667. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RNR_RETRY);
  668. }
  669. if (attr_mask & IB_QP_AV) {
  670. qp_context->pri_path.g_mylmc = attr->ah_attr.src_path_bits & 0x7f;
  671. qp_context->pri_path.rlid = cpu_to_be16(attr->ah_attr.dlid);
  672. qp_context->pri_path.static_rate = !!attr->ah_attr.static_rate;
  673. if (attr->ah_attr.ah_flags & IB_AH_GRH) {
  674. qp_context->pri_path.g_mylmc |= 1 << 7;
  675. qp_context->pri_path.mgid_index = attr->ah_attr.grh.sgid_index;
  676. qp_context->pri_path.hop_limit = attr->ah_attr.grh.hop_limit;
  677. qp_context->pri_path.sl_tclass_flowlabel =
  678. cpu_to_be32((attr->ah_attr.sl << 28) |
  679. (attr->ah_attr.grh.traffic_class << 20) |
  680. (attr->ah_attr.grh.flow_label));
  681. memcpy(qp_context->pri_path.rgid,
  682. attr->ah_attr.grh.dgid.raw, 16);
  683. } else {
  684. qp_context->pri_path.sl_tclass_flowlabel =
  685. cpu_to_be32(attr->ah_attr.sl << 28);
  686. }
  687. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_PRIMARY_ADDR_PATH);
  688. }
  689. if (attr_mask & IB_QP_TIMEOUT) {
  690. qp_context->pri_path.ackto = attr->timeout;
  691. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_ACK_TIMEOUT);
  692. }
  693. /* XXX alt_path */
  694. /* leave rdd as 0 */
  695. qp_context->pd = cpu_to_be32(to_mpd(ibqp->pd)->pd_num);
  696. /* leave wqe_base as 0 (we always create an MR based at 0 for WQs) */
  697. qp_context->wqe_lkey = cpu_to_be32(qp->mr.ibmr.lkey);
  698. qp_context->params1 = cpu_to_be32((MTHCA_ACK_REQ_FREQ << 28) |
  699. (MTHCA_FLIGHT_LIMIT << 24) |
  700. MTHCA_QP_BIT_SRE |
  701. MTHCA_QP_BIT_SWE |
  702. MTHCA_QP_BIT_SAE);
  703. if (qp->sq_policy == IB_SIGNAL_ALL_WR)
  704. qp_context->params1 |= cpu_to_be32(MTHCA_QP_BIT_SSC);
  705. if (attr_mask & IB_QP_RETRY_CNT) {
  706. qp_context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  707. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RETRY_COUNT);
  708. }
  709. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  710. qp_context->params1 |= cpu_to_be32(min(attr->max_rd_atomic ?
  711. ffs(attr->max_rd_atomic) - 1 : 0,
  712. 7) << 21);
  713. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_SRA_MAX);
  714. }
  715. if (attr_mask & IB_QP_SQ_PSN)
  716. qp_context->next_send_psn = cpu_to_be32(attr->sq_psn);
  717. qp_context->cqn_snd = cpu_to_be32(to_mcq(ibqp->send_cq)->cqn);
  718. if (mthca_is_memfree(dev)) {
  719. qp_context->snd_wqe_base_l = cpu_to_be32(qp->send_wqe_offset);
  720. qp_context->snd_db_index = cpu_to_be32(qp->sq.db_index);
  721. }
  722. if (attr_mask & IB_QP_ACCESS_FLAGS) {
  723. /*
  724. * Only enable RDMA/atomics if we have responder
  725. * resources set to a non-zero value.
  726. */
  727. if (qp->resp_depth) {
  728. qp_context->params2 |=
  729. cpu_to_be32(attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE ?
  730. MTHCA_QP_BIT_RWE : 0);
  731. qp_context->params2 |=
  732. cpu_to_be32(attr->qp_access_flags & IB_ACCESS_REMOTE_READ ?
  733. MTHCA_QP_BIT_RRE : 0);
  734. qp_context->params2 |=
  735. cpu_to_be32(attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC ?
  736. MTHCA_QP_BIT_RAE : 0);
  737. }
  738. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RWE |
  739. MTHCA_QP_OPTPAR_RRE |
  740. MTHCA_QP_OPTPAR_RAE);
  741. qp->atomic_rd_en = attr->qp_access_flags;
  742. }
  743. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  744. u8 rra_max;
  745. if (qp->resp_depth && !attr->max_dest_rd_atomic) {
  746. /*
  747. * Lowering our responder resources to zero.
  748. * Turn off RDMA/atomics as responder.
  749. * (RWE/RRE/RAE in params2 already zero)
  750. */
  751. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RWE |
  752. MTHCA_QP_OPTPAR_RRE |
  753. MTHCA_QP_OPTPAR_RAE);
  754. }
  755. if (!qp->resp_depth && attr->max_dest_rd_atomic) {
  756. /*
  757. * Increasing our responder resources from
  758. * zero. Turn on RDMA/atomics as appropriate.
  759. */
  760. qp_context->params2 |=
  761. cpu_to_be32(qp->atomic_rd_en & IB_ACCESS_REMOTE_WRITE ?
  762. MTHCA_QP_BIT_RWE : 0);
  763. qp_context->params2 |=
  764. cpu_to_be32(qp->atomic_rd_en & IB_ACCESS_REMOTE_READ ?
  765. MTHCA_QP_BIT_RRE : 0);
  766. qp_context->params2 |=
  767. cpu_to_be32(qp->atomic_rd_en & IB_ACCESS_REMOTE_ATOMIC ?
  768. MTHCA_QP_BIT_RAE : 0);
  769. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RWE |
  770. MTHCA_QP_OPTPAR_RRE |
  771. MTHCA_QP_OPTPAR_RAE);
  772. }
  773. for (rra_max = 0;
  774. 1 << rra_max < attr->max_dest_rd_atomic &&
  775. rra_max < dev->qp_table.rdb_shift;
  776. ++rra_max)
  777. ; /* nothing */
  778. qp_context->params2 |= cpu_to_be32(rra_max << 21);
  779. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RRA_MAX);
  780. qp->resp_depth = attr->max_dest_rd_atomic;
  781. }
  782. qp_context->params2 |= cpu_to_be32(MTHCA_QP_BIT_RSC);
  783. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  784. qp_context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  785. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RNR_TIMEOUT);
  786. }
  787. if (attr_mask & IB_QP_RQ_PSN)
  788. qp_context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  789. qp_context->ra_buff_indx =
  790. cpu_to_be32(dev->qp_table.rdb_base +
  791. ((qp->qpn & (dev->limits.num_qps - 1)) * MTHCA_RDB_ENTRY_SIZE <<
  792. dev->qp_table.rdb_shift));
  793. qp_context->cqn_rcv = cpu_to_be32(to_mcq(ibqp->recv_cq)->cqn);
  794. if (mthca_is_memfree(dev))
  795. qp_context->rcv_db_index = cpu_to_be32(qp->rq.db_index);
  796. if (attr_mask & IB_QP_QKEY) {
  797. qp_context->qkey = cpu_to_be32(attr->qkey);
  798. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_Q_KEY);
  799. }
  800. err = mthca_MODIFY_QP(dev, state_table[cur_state][new_state].trans,
  801. qp->qpn, 0, mailbox, 0, &status);
  802. if (status) {
  803. mthca_warn(dev, "modify QP %d returned status %02x.\n",
  804. state_table[cur_state][new_state].trans, status);
  805. err = -EINVAL;
  806. }
  807. if (!err)
  808. qp->state = new_state;
  809. mthca_free_mailbox(dev, mailbox);
  810. if (is_sqp(dev, qp))
  811. store_attrs(to_msqp(qp), attr, attr_mask);
  812. /*
  813. * If we are moving QP0 to RTR, bring the IB link up; if we
  814. * are moving QP0 to RESET or ERROR, bring the link back down.
  815. */
  816. if (is_qp0(dev, qp)) {
  817. if (cur_state != IB_QPS_RTR &&
  818. new_state == IB_QPS_RTR)
  819. init_port(dev, to_msqp(qp)->port);
  820. if (cur_state != IB_QPS_RESET &&
  821. cur_state != IB_QPS_ERR &&
  822. (new_state == IB_QPS_RESET ||
  823. new_state == IB_QPS_ERR))
  824. mthca_CLOSE_IB(dev, to_msqp(qp)->port, &status);
  825. }
  826. return err;
  827. }
  828. /*
  829. * Allocate and register buffer for WQEs. qp->rq.max, sq.max,
  830. * rq.max_gs and sq.max_gs must all be assigned.
  831. * mthca_alloc_wqe_buf will calculate rq.wqe_shift and
  832. * sq.wqe_shift (as well as send_wqe_offset, is_direct, and
  833. * queue)
  834. */
  835. static int mthca_alloc_wqe_buf(struct mthca_dev *dev,
  836. struct mthca_pd *pd,
  837. struct mthca_qp *qp)
  838. {
  839. int size;
  840. int i;
  841. int npages, shift;
  842. dma_addr_t t;
  843. u64 *dma_list = NULL;
  844. int err = -ENOMEM;
  845. size = sizeof (struct mthca_next_seg) +
  846. qp->rq.max_gs * sizeof (struct mthca_data_seg);
  847. for (qp->rq.wqe_shift = 6; 1 << qp->rq.wqe_shift < size;
  848. qp->rq.wqe_shift++)
  849. ; /* nothing */
  850. size = sizeof (struct mthca_next_seg) +
  851. qp->sq.max_gs * sizeof (struct mthca_data_seg);
  852. switch (qp->transport) {
  853. case MLX:
  854. size += 2 * sizeof (struct mthca_data_seg);
  855. break;
  856. case UD:
  857. if (mthca_is_memfree(dev))
  858. size += sizeof (struct mthca_arbel_ud_seg);
  859. else
  860. size += sizeof (struct mthca_tavor_ud_seg);
  861. break;
  862. default:
  863. /* bind seg is as big as atomic + raddr segs */
  864. size += sizeof (struct mthca_bind_seg);
  865. }
  866. for (qp->sq.wqe_shift = 6; 1 << qp->sq.wqe_shift < size;
  867. qp->sq.wqe_shift++)
  868. ; /* nothing */
  869. qp->send_wqe_offset = ALIGN(qp->rq.max << qp->rq.wqe_shift,
  870. 1 << qp->sq.wqe_shift);
  871. /*
  872. * If this is a userspace QP, we don't actually have to
  873. * allocate anything. All we need is to calculate the WQE
  874. * sizes and the send_wqe_offset, so we're done now.
  875. */
  876. if (pd->ibpd.uobject)
  877. return 0;
  878. size = PAGE_ALIGN(qp->send_wqe_offset +
  879. (qp->sq.max << qp->sq.wqe_shift));
  880. qp->wrid = kmalloc((qp->rq.max + qp->sq.max) * sizeof (u64),
  881. GFP_KERNEL);
  882. if (!qp->wrid)
  883. goto err_out;
  884. if (size <= MTHCA_MAX_DIRECT_QP_SIZE) {
  885. qp->is_direct = 1;
  886. npages = 1;
  887. shift = get_order(size) + PAGE_SHIFT;
  888. if (0)
  889. mthca_dbg(dev, "Creating direct QP of size %d (shift %d)\n",
  890. size, shift);
  891. qp->queue.direct.buf = dma_alloc_coherent(&dev->pdev->dev, size,
  892. &t, GFP_KERNEL);
  893. if (!qp->queue.direct.buf)
  894. goto err_out;
  895. pci_unmap_addr_set(&qp->queue.direct, mapping, t);
  896. memset(qp->queue.direct.buf, 0, size);
  897. while (t & ((1 << shift) - 1)) {
  898. --shift;
  899. npages *= 2;
  900. }
  901. dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
  902. if (!dma_list)
  903. goto err_out_free;
  904. for (i = 0; i < npages; ++i)
  905. dma_list[i] = t + i * (1 << shift);
  906. } else {
  907. qp->is_direct = 0;
  908. npages = size / PAGE_SIZE;
  909. shift = PAGE_SHIFT;
  910. if (0)
  911. mthca_dbg(dev, "Creating indirect QP with %d pages\n", npages);
  912. dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
  913. if (!dma_list)
  914. goto err_out;
  915. qp->queue.page_list = kmalloc(npages *
  916. sizeof *qp->queue.page_list,
  917. GFP_KERNEL);
  918. if (!qp->queue.page_list)
  919. goto err_out;
  920. for (i = 0; i < npages; ++i) {
  921. qp->queue.page_list[i].buf =
  922. dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
  923. &t, GFP_KERNEL);
  924. if (!qp->queue.page_list[i].buf)
  925. goto err_out_free;
  926. memset(qp->queue.page_list[i].buf, 0, PAGE_SIZE);
  927. pci_unmap_addr_set(&qp->queue.page_list[i], mapping, t);
  928. dma_list[i] = t;
  929. }
  930. }
  931. err = mthca_mr_alloc_phys(dev, pd->pd_num, dma_list, shift,
  932. npages, 0, size,
  933. MTHCA_MPT_FLAG_LOCAL_READ,
  934. &qp->mr);
  935. if (err)
  936. goto err_out_free;
  937. kfree(dma_list);
  938. return 0;
  939. err_out_free:
  940. if (qp->is_direct) {
  941. dma_free_coherent(&dev->pdev->dev, size, qp->queue.direct.buf,
  942. pci_unmap_addr(&qp->queue.direct, mapping));
  943. } else
  944. for (i = 0; i < npages; ++i) {
  945. if (qp->queue.page_list[i].buf)
  946. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  947. qp->queue.page_list[i].buf,
  948. pci_unmap_addr(&qp->queue.page_list[i],
  949. mapping));
  950. }
  951. err_out:
  952. kfree(qp->wrid);
  953. kfree(dma_list);
  954. return err;
  955. }
  956. static void mthca_free_wqe_buf(struct mthca_dev *dev,
  957. struct mthca_qp *qp)
  958. {
  959. int i;
  960. int size = PAGE_ALIGN(qp->send_wqe_offset +
  961. (qp->sq.max << qp->sq.wqe_shift));
  962. if (qp->is_direct) {
  963. dma_free_coherent(&dev->pdev->dev, size, qp->queue.direct.buf,
  964. pci_unmap_addr(&qp->queue.direct, mapping));
  965. } else {
  966. for (i = 0; i < size / PAGE_SIZE; ++i) {
  967. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  968. qp->queue.page_list[i].buf,
  969. pci_unmap_addr(&qp->queue.page_list[i],
  970. mapping));
  971. }
  972. }
  973. kfree(qp->wrid);
  974. }
  975. static int mthca_map_memfree(struct mthca_dev *dev,
  976. struct mthca_qp *qp)
  977. {
  978. int ret;
  979. if (mthca_is_memfree(dev)) {
  980. ret = mthca_table_get(dev, dev->qp_table.qp_table, qp->qpn);
  981. if (ret)
  982. return ret;
  983. ret = mthca_table_get(dev, dev->qp_table.eqp_table, qp->qpn);
  984. if (ret)
  985. goto err_qpc;
  986. ret = mthca_table_get(dev, dev->qp_table.rdb_table,
  987. qp->qpn << dev->qp_table.rdb_shift);
  988. if (ret)
  989. goto err_eqpc;
  990. }
  991. return 0;
  992. err_eqpc:
  993. mthca_table_put(dev, dev->qp_table.eqp_table, qp->qpn);
  994. err_qpc:
  995. mthca_table_put(dev, dev->qp_table.qp_table, qp->qpn);
  996. return ret;
  997. }
  998. static void mthca_unmap_memfree(struct mthca_dev *dev,
  999. struct mthca_qp *qp)
  1000. {
  1001. mthca_table_put(dev, dev->qp_table.rdb_table,
  1002. qp->qpn << dev->qp_table.rdb_shift);
  1003. mthca_table_put(dev, dev->qp_table.eqp_table, qp->qpn);
  1004. mthca_table_put(dev, dev->qp_table.qp_table, qp->qpn);
  1005. }
  1006. static int mthca_alloc_memfree(struct mthca_dev *dev,
  1007. struct mthca_qp *qp)
  1008. {
  1009. int ret = 0;
  1010. if (mthca_is_memfree(dev)) {
  1011. qp->rq.db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_RQ,
  1012. qp->qpn, &qp->rq.db);
  1013. if (qp->rq.db_index < 0)
  1014. return ret;
  1015. qp->sq.db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_SQ,
  1016. qp->qpn, &qp->sq.db);
  1017. if (qp->sq.db_index < 0)
  1018. mthca_free_db(dev, MTHCA_DB_TYPE_RQ, qp->rq.db_index);
  1019. }
  1020. return ret;
  1021. }
  1022. static void mthca_free_memfree(struct mthca_dev *dev,
  1023. struct mthca_qp *qp)
  1024. {
  1025. if (mthca_is_memfree(dev)) {
  1026. mthca_free_db(dev, MTHCA_DB_TYPE_SQ, qp->sq.db_index);
  1027. mthca_free_db(dev, MTHCA_DB_TYPE_RQ, qp->rq.db_index);
  1028. }
  1029. }
  1030. static void mthca_wq_init(struct mthca_wq* wq)
  1031. {
  1032. spin_lock_init(&wq->lock);
  1033. wq->next_ind = 0;
  1034. wq->last_comp = wq->max - 1;
  1035. wq->head = 0;
  1036. wq->tail = 0;
  1037. wq->last = NULL;
  1038. }
  1039. static int mthca_alloc_qp_common(struct mthca_dev *dev,
  1040. struct mthca_pd *pd,
  1041. struct mthca_cq *send_cq,
  1042. struct mthca_cq *recv_cq,
  1043. enum ib_sig_type send_policy,
  1044. struct mthca_qp *qp)
  1045. {
  1046. int ret;
  1047. int i;
  1048. atomic_set(&qp->refcount, 1);
  1049. qp->state = IB_QPS_RESET;
  1050. qp->atomic_rd_en = 0;
  1051. qp->resp_depth = 0;
  1052. qp->sq_policy = send_policy;
  1053. mthca_wq_init(&qp->sq);
  1054. mthca_wq_init(&qp->rq);
  1055. ret = mthca_map_memfree(dev, qp);
  1056. if (ret)
  1057. return ret;
  1058. ret = mthca_alloc_wqe_buf(dev, pd, qp);
  1059. if (ret) {
  1060. mthca_unmap_memfree(dev, qp);
  1061. return ret;
  1062. }
  1063. /*
  1064. * If this is a userspace QP, we're done now. The doorbells
  1065. * will be allocated and buffers will be initialized in
  1066. * userspace.
  1067. */
  1068. if (pd->ibpd.uobject)
  1069. return 0;
  1070. ret = mthca_alloc_memfree(dev, qp);
  1071. if (ret) {
  1072. mthca_free_wqe_buf(dev, qp);
  1073. mthca_unmap_memfree(dev, qp);
  1074. return ret;
  1075. }
  1076. if (mthca_is_memfree(dev)) {
  1077. struct mthca_next_seg *next;
  1078. struct mthca_data_seg *scatter;
  1079. int size = (sizeof (struct mthca_next_seg) +
  1080. qp->rq.max_gs * sizeof (struct mthca_data_seg)) / 16;
  1081. for (i = 0; i < qp->rq.max; ++i) {
  1082. next = get_recv_wqe(qp, i);
  1083. next->nda_op = cpu_to_be32(((i + 1) & (qp->rq.max - 1)) <<
  1084. qp->rq.wqe_shift);
  1085. next->ee_nds = cpu_to_be32(size);
  1086. for (scatter = (void *) (next + 1);
  1087. (void *) scatter < (void *) next + (1 << qp->rq.wqe_shift);
  1088. ++scatter)
  1089. scatter->lkey = cpu_to_be32(MTHCA_INVAL_LKEY);
  1090. }
  1091. for (i = 0; i < qp->sq.max; ++i) {
  1092. next = get_send_wqe(qp, i);
  1093. next->nda_op = cpu_to_be32((((i + 1) & (qp->sq.max - 1)) <<
  1094. qp->sq.wqe_shift) +
  1095. qp->send_wqe_offset);
  1096. }
  1097. }
  1098. return 0;
  1099. }
  1100. static int mthca_set_qp_size(struct mthca_dev *dev, struct ib_qp_cap *cap,
  1101. struct mthca_qp *qp)
  1102. {
  1103. /* Sanity check QP size before proceeding */
  1104. if (cap->max_send_wr > 65536 || cap->max_recv_wr > 65536 ||
  1105. cap->max_send_sge > 64 || cap->max_recv_sge > 64)
  1106. return -EINVAL;
  1107. if (mthca_is_memfree(dev)) {
  1108. qp->rq.max = cap->max_recv_wr ?
  1109. roundup_pow_of_two(cap->max_recv_wr) : 0;
  1110. qp->sq.max = cap->max_send_wr ?
  1111. roundup_pow_of_two(cap->max_send_wr) : 0;
  1112. } else {
  1113. qp->rq.max = cap->max_recv_wr;
  1114. qp->sq.max = cap->max_send_wr;
  1115. }
  1116. qp->rq.max_gs = cap->max_recv_sge;
  1117. qp->sq.max_gs = max_t(int, cap->max_send_sge,
  1118. ALIGN(cap->max_inline_data + MTHCA_INLINE_HEADER_SIZE,
  1119. MTHCA_INLINE_CHUNK_SIZE) /
  1120. sizeof (struct mthca_data_seg));
  1121. /*
  1122. * For MLX transport we need 2 extra S/G entries:
  1123. * one for the header and one for the checksum at the end
  1124. */
  1125. if ((qp->transport == MLX && qp->sq.max_gs + 2 > dev->limits.max_sg) ||
  1126. qp->sq.max_gs > dev->limits.max_sg || qp->rq.max_gs > dev->limits.max_sg)
  1127. return -EINVAL;
  1128. return 0;
  1129. }
  1130. int mthca_alloc_qp(struct mthca_dev *dev,
  1131. struct mthca_pd *pd,
  1132. struct mthca_cq *send_cq,
  1133. struct mthca_cq *recv_cq,
  1134. enum ib_qp_type type,
  1135. enum ib_sig_type send_policy,
  1136. struct ib_qp_cap *cap,
  1137. struct mthca_qp *qp)
  1138. {
  1139. int err;
  1140. err = mthca_set_qp_size(dev, cap, qp);
  1141. if (err)
  1142. return err;
  1143. switch (type) {
  1144. case IB_QPT_RC: qp->transport = RC; break;
  1145. case IB_QPT_UC: qp->transport = UC; break;
  1146. case IB_QPT_UD: qp->transport = UD; break;
  1147. default: return -EINVAL;
  1148. }
  1149. qp->qpn = mthca_alloc(&dev->qp_table.alloc);
  1150. if (qp->qpn == -1)
  1151. return -ENOMEM;
  1152. err = mthca_alloc_qp_common(dev, pd, send_cq, recv_cq,
  1153. send_policy, qp);
  1154. if (err) {
  1155. mthca_free(&dev->qp_table.alloc, qp->qpn);
  1156. return err;
  1157. }
  1158. spin_lock_irq(&dev->qp_table.lock);
  1159. mthca_array_set(&dev->qp_table.qp,
  1160. qp->qpn & (dev->limits.num_qps - 1), qp);
  1161. spin_unlock_irq(&dev->qp_table.lock);
  1162. return 0;
  1163. }
  1164. int mthca_alloc_sqp(struct mthca_dev *dev,
  1165. struct mthca_pd *pd,
  1166. struct mthca_cq *send_cq,
  1167. struct mthca_cq *recv_cq,
  1168. enum ib_sig_type send_policy,
  1169. struct ib_qp_cap *cap,
  1170. int qpn,
  1171. int port,
  1172. struct mthca_sqp *sqp)
  1173. {
  1174. u32 mqpn = qpn * 2 + dev->qp_table.sqp_start + port - 1;
  1175. int err;
  1176. err = mthca_set_qp_size(dev, cap, &sqp->qp);
  1177. if (err)
  1178. return err;
  1179. sqp->header_buf_size = sqp->qp.sq.max * MTHCA_UD_HEADER_SIZE;
  1180. sqp->header_buf = dma_alloc_coherent(&dev->pdev->dev, sqp->header_buf_size,
  1181. &sqp->header_dma, GFP_KERNEL);
  1182. if (!sqp->header_buf)
  1183. return -ENOMEM;
  1184. spin_lock_irq(&dev->qp_table.lock);
  1185. if (mthca_array_get(&dev->qp_table.qp, mqpn))
  1186. err = -EBUSY;
  1187. else
  1188. mthca_array_set(&dev->qp_table.qp, mqpn, sqp);
  1189. spin_unlock_irq(&dev->qp_table.lock);
  1190. if (err)
  1191. goto err_out;
  1192. sqp->port = port;
  1193. sqp->qp.qpn = mqpn;
  1194. sqp->qp.transport = MLX;
  1195. err = mthca_alloc_qp_common(dev, pd, send_cq, recv_cq,
  1196. send_policy, &sqp->qp);
  1197. if (err)
  1198. goto err_out_free;
  1199. atomic_inc(&pd->sqp_count);
  1200. return 0;
  1201. err_out_free:
  1202. /*
  1203. * Lock CQs here, so that CQ polling code can do QP lookup
  1204. * without taking a lock.
  1205. */
  1206. spin_lock_irq(&send_cq->lock);
  1207. if (send_cq != recv_cq)
  1208. spin_lock(&recv_cq->lock);
  1209. spin_lock(&dev->qp_table.lock);
  1210. mthca_array_clear(&dev->qp_table.qp, mqpn);
  1211. spin_unlock(&dev->qp_table.lock);
  1212. if (send_cq != recv_cq)
  1213. spin_unlock(&recv_cq->lock);
  1214. spin_unlock_irq(&send_cq->lock);
  1215. err_out:
  1216. dma_free_coherent(&dev->pdev->dev, sqp->header_buf_size,
  1217. sqp->header_buf, sqp->header_dma);
  1218. return err;
  1219. }
  1220. void mthca_free_qp(struct mthca_dev *dev,
  1221. struct mthca_qp *qp)
  1222. {
  1223. u8 status;
  1224. struct mthca_cq *send_cq;
  1225. struct mthca_cq *recv_cq;
  1226. send_cq = to_mcq(qp->ibqp.send_cq);
  1227. recv_cq = to_mcq(qp->ibqp.recv_cq);
  1228. /*
  1229. * Lock CQs here, so that CQ polling code can do QP lookup
  1230. * without taking a lock.
  1231. */
  1232. spin_lock_irq(&send_cq->lock);
  1233. if (send_cq != recv_cq)
  1234. spin_lock(&recv_cq->lock);
  1235. spin_lock(&dev->qp_table.lock);
  1236. mthca_array_clear(&dev->qp_table.qp,
  1237. qp->qpn & (dev->limits.num_qps - 1));
  1238. spin_unlock(&dev->qp_table.lock);
  1239. if (send_cq != recv_cq)
  1240. spin_unlock(&recv_cq->lock);
  1241. spin_unlock_irq(&send_cq->lock);
  1242. atomic_dec(&qp->refcount);
  1243. wait_event(qp->wait, !atomic_read(&qp->refcount));
  1244. if (qp->state != IB_QPS_RESET)
  1245. mthca_MODIFY_QP(dev, MTHCA_TRANS_ANY2RST, qp->qpn, 0, NULL, 0, &status);
  1246. /*
  1247. * If this is a userspace QP, the buffers, MR, CQs and so on
  1248. * will be cleaned up in userspace, so all we have to do is
  1249. * unref the mem-free tables and free the QPN in our table.
  1250. */
  1251. if (!qp->ibqp.uobject) {
  1252. mthca_cq_clean(dev, to_mcq(qp->ibqp.send_cq)->cqn, qp->qpn);
  1253. if (qp->ibqp.send_cq != qp->ibqp.recv_cq)
  1254. mthca_cq_clean(dev, to_mcq(qp->ibqp.recv_cq)->cqn, qp->qpn);
  1255. mthca_free_mr(dev, &qp->mr);
  1256. mthca_free_memfree(dev, qp);
  1257. mthca_free_wqe_buf(dev, qp);
  1258. }
  1259. mthca_unmap_memfree(dev, qp);
  1260. if (is_sqp(dev, qp)) {
  1261. atomic_dec(&(to_mpd(qp->ibqp.pd)->sqp_count));
  1262. dma_free_coherent(&dev->pdev->dev,
  1263. to_msqp(qp)->header_buf_size,
  1264. to_msqp(qp)->header_buf,
  1265. to_msqp(qp)->header_dma);
  1266. } else
  1267. mthca_free(&dev->qp_table.alloc, qp->qpn);
  1268. }
  1269. /* Create UD header for an MLX send and build a data segment for it */
  1270. static int build_mlx_header(struct mthca_dev *dev, struct mthca_sqp *sqp,
  1271. int ind, struct ib_send_wr *wr,
  1272. struct mthca_mlx_seg *mlx,
  1273. struct mthca_data_seg *data)
  1274. {
  1275. int header_size;
  1276. int err;
  1277. ib_ud_header_init(256, /* assume a MAD */
  1278. sqp->ud_header.grh_present,
  1279. &sqp->ud_header);
  1280. err = mthca_read_ah(dev, to_mah(wr->wr.ud.ah), &sqp->ud_header);
  1281. if (err)
  1282. return err;
  1283. mlx->flags &= ~cpu_to_be32(MTHCA_NEXT_SOLICIT | 1);
  1284. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MTHCA_MLX_VL15 : 0) |
  1285. (sqp->ud_header.lrh.destination_lid == 0xffff ?
  1286. MTHCA_MLX_SLR : 0) |
  1287. (sqp->ud_header.lrh.service_level << 8));
  1288. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  1289. mlx->vcrc = 0;
  1290. switch (wr->opcode) {
  1291. case IB_WR_SEND:
  1292. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  1293. sqp->ud_header.immediate_present = 0;
  1294. break;
  1295. case IB_WR_SEND_WITH_IMM:
  1296. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  1297. sqp->ud_header.immediate_present = 1;
  1298. sqp->ud_header.immediate_data = wr->imm_data;
  1299. break;
  1300. default:
  1301. return -EINVAL;
  1302. }
  1303. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
  1304. if (sqp->ud_header.lrh.destination_lid == 0xffff)
  1305. sqp->ud_header.lrh.source_lid = 0xffff;
  1306. sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
  1307. if (!sqp->qp.ibqp.qp_num)
  1308. ib_get_cached_pkey(&dev->ib_dev, sqp->port,
  1309. sqp->pkey_index,
  1310. &sqp->ud_header.bth.pkey);
  1311. else
  1312. ib_get_cached_pkey(&dev->ib_dev, sqp->port,
  1313. wr->wr.ud.pkey_index,
  1314. &sqp->ud_header.bth.pkey);
  1315. cpu_to_be16s(&sqp->ud_header.bth.pkey);
  1316. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  1317. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  1318. sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
  1319. sqp->qkey : wr->wr.ud.remote_qkey);
  1320. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  1321. header_size = ib_ud_header_pack(&sqp->ud_header,
  1322. sqp->header_buf +
  1323. ind * MTHCA_UD_HEADER_SIZE);
  1324. data->byte_count = cpu_to_be32(header_size);
  1325. data->lkey = cpu_to_be32(to_mpd(sqp->qp.ibqp.pd)->ntmr.ibmr.lkey);
  1326. data->addr = cpu_to_be64(sqp->header_dma +
  1327. ind * MTHCA_UD_HEADER_SIZE);
  1328. return 0;
  1329. }
  1330. static inline int mthca_wq_overflow(struct mthca_wq *wq, int nreq,
  1331. struct ib_cq *ib_cq)
  1332. {
  1333. unsigned cur;
  1334. struct mthca_cq *cq;
  1335. cur = wq->head - wq->tail;
  1336. if (likely(cur + nreq < wq->max))
  1337. return 0;
  1338. cq = to_mcq(ib_cq);
  1339. spin_lock(&cq->lock);
  1340. cur = wq->head - wq->tail;
  1341. spin_unlock(&cq->lock);
  1342. return cur + nreq >= wq->max;
  1343. }
  1344. int mthca_tavor_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1345. struct ib_send_wr **bad_wr)
  1346. {
  1347. struct mthca_dev *dev = to_mdev(ibqp->device);
  1348. struct mthca_qp *qp = to_mqp(ibqp);
  1349. void *wqe;
  1350. void *prev_wqe;
  1351. unsigned long flags;
  1352. int err = 0;
  1353. int nreq;
  1354. int i;
  1355. int size;
  1356. int size0 = 0;
  1357. u32 f0 = 0;
  1358. int ind;
  1359. u8 op0 = 0;
  1360. spin_lock_irqsave(&qp->sq.lock, flags);
  1361. /* XXX check that state is OK to post send */
  1362. ind = qp->sq.next_ind;
  1363. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1364. if (mthca_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  1365. mthca_err(dev, "SQ %06x full (%u head, %u tail,"
  1366. " %d max, %d nreq)\n", qp->qpn,
  1367. qp->sq.head, qp->sq.tail,
  1368. qp->sq.max, nreq);
  1369. err = -ENOMEM;
  1370. *bad_wr = wr;
  1371. goto out;
  1372. }
  1373. wqe = get_send_wqe(qp, ind);
  1374. prev_wqe = qp->sq.last;
  1375. qp->sq.last = wqe;
  1376. ((struct mthca_next_seg *) wqe)->nda_op = 0;
  1377. ((struct mthca_next_seg *) wqe)->ee_nds = 0;
  1378. ((struct mthca_next_seg *) wqe)->flags =
  1379. ((wr->send_flags & IB_SEND_SIGNALED) ?
  1380. cpu_to_be32(MTHCA_NEXT_CQ_UPDATE) : 0) |
  1381. ((wr->send_flags & IB_SEND_SOLICITED) ?
  1382. cpu_to_be32(MTHCA_NEXT_SOLICIT) : 0) |
  1383. cpu_to_be32(1);
  1384. if (wr->opcode == IB_WR_SEND_WITH_IMM ||
  1385. wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  1386. ((struct mthca_next_seg *) wqe)->imm = wr->imm_data;
  1387. wqe += sizeof (struct mthca_next_seg);
  1388. size = sizeof (struct mthca_next_seg) / 16;
  1389. switch (qp->transport) {
  1390. case RC:
  1391. switch (wr->opcode) {
  1392. case IB_WR_ATOMIC_CMP_AND_SWP:
  1393. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1394. ((struct mthca_raddr_seg *) wqe)->raddr =
  1395. cpu_to_be64(wr->wr.atomic.remote_addr);
  1396. ((struct mthca_raddr_seg *) wqe)->rkey =
  1397. cpu_to_be32(wr->wr.atomic.rkey);
  1398. ((struct mthca_raddr_seg *) wqe)->reserved = 0;
  1399. wqe += sizeof (struct mthca_raddr_seg);
  1400. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  1401. ((struct mthca_atomic_seg *) wqe)->swap_add =
  1402. cpu_to_be64(wr->wr.atomic.swap);
  1403. ((struct mthca_atomic_seg *) wqe)->compare =
  1404. cpu_to_be64(wr->wr.atomic.compare_add);
  1405. } else {
  1406. ((struct mthca_atomic_seg *) wqe)->swap_add =
  1407. cpu_to_be64(wr->wr.atomic.compare_add);
  1408. ((struct mthca_atomic_seg *) wqe)->compare = 0;
  1409. }
  1410. wqe += sizeof (struct mthca_atomic_seg);
  1411. size += sizeof (struct mthca_raddr_seg) / 16 +
  1412. sizeof (struct mthca_atomic_seg);
  1413. break;
  1414. case IB_WR_RDMA_WRITE:
  1415. case IB_WR_RDMA_WRITE_WITH_IMM:
  1416. case IB_WR_RDMA_READ:
  1417. ((struct mthca_raddr_seg *) wqe)->raddr =
  1418. cpu_to_be64(wr->wr.rdma.remote_addr);
  1419. ((struct mthca_raddr_seg *) wqe)->rkey =
  1420. cpu_to_be32(wr->wr.rdma.rkey);
  1421. ((struct mthca_raddr_seg *) wqe)->reserved = 0;
  1422. wqe += sizeof (struct mthca_raddr_seg);
  1423. size += sizeof (struct mthca_raddr_seg) / 16;
  1424. break;
  1425. default:
  1426. /* No extra segments required for sends */
  1427. break;
  1428. }
  1429. break;
  1430. case UC:
  1431. switch (wr->opcode) {
  1432. case IB_WR_RDMA_WRITE:
  1433. case IB_WR_RDMA_WRITE_WITH_IMM:
  1434. ((struct mthca_raddr_seg *) wqe)->raddr =
  1435. cpu_to_be64(wr->wr.rdma.remote_addr);
  1436. ((struct mthca_raddr_seg *) wqe)->rkey =
  1437. cpu_to_be32(wr->wr.rdma.rkey);
  1438. ((struct mthca_raddr_seg *) wqe)->reserved = 0;
  1439. wqe += sizeof (struct mthca_raddr_seg);
  1440. size += sizeof (struct mthca_raddr_seg) / 16;
  1441. break;
  1442. default:
  1443. /* No extra segments required for sends */
  1444. break;
  1445. }
  1446. break;
  1447. case UD:
  1448. ((struct mthca_tavor_ud_seg *) wqe)->lkey =
  1449. cpu_to_be32(to_mah(wr->wr.ud.ah)->key);
  1450. ((struct mthca_tavor_ud_seg *) wqe)->av_addr =
  1451. cpu_to_be64(to_mah(wr->wr.ud.ah)->avdma);
  1452. ((struct mthca_tavor_ud_seg *) wqe)->dqpn =
  1453. cpu_to_be32(wr->wr.ud.remote_qpn);
  1454. ((struct mthca_tavor_ud_seg *) wqe)->qkey =
  1455. cpu_to_be32(wr->wr.ud.remote_qkey);
  1456. wqe += sizeof (struct mthca_tavor_ud_seg);
  1457. size += sizeof (struct mthca_tavor_ud_seg) / 16;
  1458. break;
  1459. case MLX:
  1460. err = build_mlx_header(dev, to_msqp(qp), ind, wr,
  1461. wqe - sizeof (struct mthca_next_seg),
  1462. wqe);
  1463. if (err) {
  1464. *bad_wr = wr;
  1465. goto out;
  1466. }
  1467. wqe += sizeof (struct mthca_data_seg);
  1468. size += sizeof (struct mthca_data_seg) / 16;
  1469. break;
  1470. }
  1471. if (wr->num_sge > qp->sq.max_gs) {
  1472. mthca_err(dev, "too many gathers\n");
  1473. err = -EINVAL;
  1474. *bad_wr = wr;
  1475. goto out;
  1476. }
  1477. for (i = 0; i < wr->num_sge; ++i) {
  1478. ((struct mthca_data_seg *) wqe)->byte_count =
  1479. cpu_to_be32(wr->sg_list[i].length);
  1480. ((struct mthca_data_seg *) wqe)->lkey =
  1481. cpu_to_be32(wr->sg_list[i].lkey);
  1482. ((struct mthca_data_seg *) wqe)->addr =
  1483. cpu_to_be64(wr->sg_list[i].addr);
  1484. wqe += sizeof (struct mthca_data_seg);
  1485. size += sizeof (struct mthca_data_seg) / 16;
  1486. }
  1487. /* Add one more inline data segment for ICRC */
  1488. if (qp->transport == MLX) {
  1489. ((struct mthca_data_seg *) wqe)->byte_count =
  1490. cpu_to_be32((1 << 31) | 4);
  1491. ((u32 *) wqe)[1] = 0;
  1492. wqe += sizeof (struct mthca_data_seg);
  1493. size += sizeof (struct mthca_data_seg) / 16;
  1494. }
  1495. qp->wrid[ind + qp->rq.max] = wr->wr_id;
  1496. if (wr->opcode >= ARRAY_SIZE(mthca_opcode)) {
  1497. mthca_err(dev, "opcode invalid\n");
  1498. err = -EINVAL;
  1499. *bad_wr = wr;
  1500. goto out;
  1501. }
  1502. if (prev_wqe) {
  1503. ((struct mthca_next_seg *) prev_wqe)->nda_op =
  1504. cpu_to_be32(((ind << qp->sq.wqe_shift) +
  1505. qp->send_wqe_offset) |
  1506. mthca_opcode[wr->opcode]);
  1507. wmb();
  1508. ((struct mthca_next_seg *) prev_wqe)->ee_nds =
  1509. cpu_to_be32((size0 ? 0 : MTHCA_NEXT_DBD) | size);
  1510. }
  1511. if (!size0) {
  1512. size0 = size;
  1513. op0 = mthca_opcode[wr->opcode];
  1514. }
  1515. ++ind;
  1516. if (unlikely(ind >= qp->sq.max))
  1517. ind -= qp->sq.max;
  1518. }
  1519. out:
  1520. if (likely(nreq)) {
  1521. u32 doorbell[2];
  1522. doorbell[0] = cpu_to_be32(((qp->sq.next_ind << qp->sq.wqe_shift) +
  1523. qp->send_wqe_offset) | f0 | op0);
  1524. doorbell[1] = cpu_to_be32((qp->qpn << 8) | size0);
  1525. wmb();
  1526. mthca_write64(doorbell,
  1527. dev->kar + MTHCA_SEND_DOORBELL,
  1528. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  1529. }
  1530. qp->sq.next_ind = ind;
  1531. qp->sq.head += nreq;
  1532. spin_unlock_irqrestore(&qp->sq.lock, flags);
  1533. return err;
  1534. }
  1535. int mthca_tavor_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1536. struct ib_recv_wr **bad_wr)
  1537. {
  1538. struct mthca_dev *dev = to_mdev(ibqp->device);
  1539. struct mthca_qp *qp = to_mqp(ibqp);
  1540. unsigned long flags;
  1541. int err = 0;
  1542. int nreq;
  1543. int i;
  1544. int size;
  1545. int size0 = 0;
  1546. int ind;
  1547. void *wqe;
  1548. void *prev_wqe;
  1549. spin_lock_irqsave(&qp->rq.lock, flags);
  1550. /* XXX check that state is OK to post receive */
  1551. ind = qp->rq.next_ind;
  1552. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1553. if (mthca_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  1554. mthca_err(dev, "RQ %06x full (%u head, %u tail,"
  1555. " %d max, %d nreq)\n", qp->qpn,
  1556. qp->rq.head, qp->rq.tail,
  1557. qp->rq.max, nreq);
  1558. err = -ENOMEM;
  1559. *bad_wr = wr;
  1560. goto out;
  1561. }
  1562. wqe = get_recv_wqe(qp, ind);
  1563. prev_wqe = qp->rq.last;
  1564. qp->rq.last = wqe;
  1565. ((struct mthca_next_seg *) wqe)->nda_op = 0;
  1566. ((struct mthca_next_seg *) wqe)->ee_nds =
  1567. cpu_to_be32(MTHCA_NEXT_DBD);
  1568. ((struct mthca_next_seg *) wqe)->flags = 0;
  1569. wqe += sizeof (struct mthca_next_seg);
  1570. size = sizeof (struct mthca_next_seg) / 16;
  1571. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  1572. err = -EINVAL;
  1573. *bad_wr = wr;
  1574. goto out;
  1575. }
  1576. for (i = 0; i < wr->num_sge; ++i) {
  1577. ((struct mthca_data_seg *) wqe)->byte_count =
  1578. cpu_to_be32(wr->sg_list[i].length);
  1579. ((struct mthca_data_seg *) wqe)->lkey =
  1580. cpu_to_be32(wr->sg_list[i].lkey);
  1581. ((struct mthca_data_seg *) wqe)->addr =
  1582. cpu_to_be64(wr->sg_list[i].addr);
  1583. wqe += sizeof (struct mthca_data_seg);
  1584. size += sizeof (struct mthca_data_seg) / 16;
  1585. }
  1586. qp->wrid[ind] = wr->wr_id;
  1587. if (likely(prev_wqe)) {
  1588. ((struct mthca_next_seg *) prev_wqe)->nda_op =
  1589. cpu_to_be32((ind << qp->rq.wqe_shift) | 1);
  1590. wmb();
  1591. ((struct mthca_next_seg *) prev_wqe)->ee_nds =
  1592. cpu_to_be32(MTHCA_NEXT_DBD | size);
  1593. }
  1594. if (!size0)
  1595. size0 = size;
  1596. ++ind;
  1597. if (unlikely(ind >= qp->rq.max))
  1598. ind -= qp->rq.max;
  1599. }
  1600. out:
  1601. if (likely(nreq)) {
  1602. u32 doorbell[2];
  1603. doorbell[0] = cpu_to_be32((qp->rq.next_ind << qp->rq.wqe_shift) | size0);
  1604. doorbell[1] = cpu_to_be32((qp->qpn << 8) | nreq);
  1605. wmb();
  1606. mthca_write64(doorbell,
  1607. dev->kar + MTHCA_RECEIVE_DOORBELL,
  1608. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  1609. }
  1610. qp->rq.next_ind = ind;
  1611. qp->rq.head += nreq;
  1612. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1613. return err;
  1614. }
  1615. int mthca_arbel_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1616. struct ib_send_wr **bad_wr)
  1617. {
  1618. struct mthca_dev *dev = to_mdev(ibqp->device);
  1619. struct mthca_qp *qp = to_mqp(ibqp);
  1620. void *wqe;
  1621. void *prev_wqe;
  1622. unsigned long flags;
  1623. int err = 0;
  1624. int nreq;
  1625. int i;
  1626. int size;
  1627. int size0 = 0;
  1628. u32 f0 = 0;
  1629. int ind;
  1630. u8 op0 = 0;
  1631. spin_lock_irqsave(&qp->sq.lock, flags);
  1632. /* XXX check that state is OK to post send */
  1633. ind = qp->sq.head & (qp->sq.max - 1);
  1634. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1635. if (mthca_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  1636. mthca_err(dev, "SQ %06x full (%u head, %u tail,"
  1637. " %d max, %d nreq)\n", qp->qpn,
  1638. qp->sq.head, qp->sq.tail,
  1639. qp->sq.max, nreq);
  1640. err = -ENOMEM;
  1641. *bad_wr = wr;
  1642. goto out;
  1643. }
  1644. wqe = get_send_wqe(qp, ind);
  1645. prev_wqe = qp->sq.last;
  1646. qp->sq.last = wqe;
  1647. ((struct mthca_next_seg *) wqe)->flags =
  1648. ((wr->send_flags & IB_SEND_SIGNALED) ?
  1649. cpu_to_be32(MTHCA_NEXT_CQ_UPDATE) : 0) |
  1650. ((wr->send_flags & IB_SEND_SOLICITED) ?
  1651. cpu_to_be32(MTHCA_NEXT_SOLICIT) : 0) |
  1652. cpu_to_be32(1);
  1653. if (wr->opcode == IB_WR_SEND_WITH_IMM ||
  1654. wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  1655. ((struct mthca_next_seg *) wqe)->imm = wr->imm_data;
  1656. wqe += sizeof (struct mthca_next_seg);
  1657. size = sizeof (struct mthca_next_seg) / 16;
  1658. switch (qp->transport) {
  1659. case RC:
  1660. switch (wr->opcode) {
  1661. case IB_WR_ATOMIC_CMP_AND_SWP:
  1662. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1663. ((struct mthca_raddr_seg *) wqe)->raddr =
  1664. cpu_to_be64(wr->wr.atomic.remote_addr);
  1665. ((struct mthca_raddr_seg *) wqe)->rkey =
  1666. cpu_to_be32(wr->wr.atomic.rkey);
  1667. ((struct mthca_raddr_seg *) wqe)->reserved = 0;
  1668. wqe += sizeof (struct mthca_raddr_seg);
  1669. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  1670. ((struct mthca_atomic_seg *) wqe)->swap_add =
  1671. cpu_to_be64(wr->wr.atomic.swap);
  1672. ((struct mthca_atomic_seg *) wqe)->compare =
  1673. cpu_to_be64(wr->wr.atomic.compare_add);
  1674. } else {
  1675. ((struct mthca_atomic_seg *) wqe)->swap_add =
  1676. cpu_to_be64(wr->wr.atomic.compare_add);
  1677. ((struct mthca_atomic_seg *) wqe)->compare = 0;
  1678. }
  1679. wqe += sizeof (struct mthca_atomic_seg);
  1680. size += sizeof (struct mthca_raddr_seg) / 16 +
  1681. sizeof (struct mthca_atomic_seg);
  1682. break;
  1683. case IB_WR_RDMA_READ:
  1684. case IB_WR_RDMA_WRITE:
  1685. case IB_WR_RDMA_WRITE_WITH_IMM:
  1686. ((struct mthca_raddr_seg *) wqe)->raddr =
  1687. cpu_to_be64(wr->wr.rdma.remote_addr);
  1688. ((struct mthca_raddr_seg *) wqe)->rkey =
  1689. cpu_to_be32(wr->wr.rdma.rkey);
  1690. ((struct mthca_raddr_seg *) wqe)->reserved = 0;
  1691. wqe += sizeof (struct mthca_raddr_seg);
  1692. size += sizeof (struct mthca_raddr_seg) / 16;
  1693. break;
  1694. default:
  1695. /* No extra segments required for sends */
  1696. break;
  1697. }
  1698. break;
  1699. case UC:
  1700. switch (wr->opcode) {
  1701. case IB_WR_RDMA_WRITE:
  1702. case IB_WR_RDMA_WRITE_WITH_IMM:
  1703. ((struct mthca_raddr_seg *) wqe)->raddr =
  1704. cpu_to_be64(wr->wr.rdma.remote_addr);
  1705. ((struct mthca_raddr_seg *) wqe)->rkey =
  1706. cpu_to_be32(wr->wr.rdma.rkey);
  1707. ((struct mthca_raddr_seg *) wqe)->reserved = 0;
  1708. wqe += sizeof (struct mthca_raddr_seg);
  1709. size += sizeof (struct mthca_raddr_seg) / 16;
  1710. break;
  1711. default:
  1712. /* No extra segments required for sends */
  1713. break;
  1714. }
  1715. break;
  1716. case UD:
  1717. memcpy(((struct mthca_arbel_ud_seg *) wqe)->av,
  1718. to_mah(wr->wr.ud.ah)->av, MTHCA_AV_SIZE);
  1719. ((struct mthca_arbel_ud_seg *) wqe)->dqpn =
  1720. cpu_to_be32(wr->wr.ud.remote_qpn);
  1721. ((struct mthca_arbel_ud_seg *) wqe)->qkey =
  1722. cpu_to_be32(wr->wr.ud.remote_qkey);
  1723. wqe += sizeof (struct mthca_arbel_ud_seg);
  1724. size += sizeof (struct mthca_arbel_ud_seg) / 16;
  1725. break;
  1726. case MLX:
  1727. err = build_mlx_header(dev, to_msqp(qp), ind, wr,
  1728. wqe - sizeof (struct mthca_next_seg),
  1729. wqe);
  1730. if (err) {
  1731. *bad_wr = wr;
  1732. goto out;
  1733. }
  1734. wqe += sizeof (struct mthca_data_seg);
  1735. size += sizeof (struct mthca_data_seg) / 16;
  1736. break;
  1737. }
  1738. if (wr->num_sge > qp->sq.max_gs) {
  1739. mthca_err(dev, "too many gathers\n");
  1740. err = -EINVAL;
  1741. *bad_wr = wr;
  1742. goto out;
  1743. }
  1744. for (i = 0; i < wr->num_sge; ++i) {
  1745. ((struct mthca_data_seg *) wqe)->byte_count =
  1746. cpu_to_be32(wr->sg_list[i].length);
  1747. ((struct mthca_data_seg *) wqe)->lkey =
  1748. cpu_to_be32(wr->sg_list[i].lkey);
  1749. ((struct mthca_data_seg *) wqe)->addr =
  1750. cpu_to_be64(wr->sg_list[i].addr);
  1751. wqe += sizeof (struct mthca_data_seg);
  1752. size += sizeof (struct mthca_data_seg) / 16;
  1753. }
  1754. /* Add one more inline data segment for ICRC */
  1755. if (qp->transport == MLX) {
  1756. ((struct mthca_data_seg *) wqe)->byte_count =
  1757. cpu_to_be32((1 << 31) | 4);
  1758. ((u32 *) wqe)[1] = 0;
  1759. wqe += sizeof (struct mthca_data_seg);
  1760. size += sizeof (struct mthca_data_seg) / 16;
  1761. }
  1762. qp->wrid[ind + qp->rq.max] = wr->wr_id;
  1763. if (wr->opcode >= ARRAY_SIZE(mthca_opcode)) {
  1764. mthca_err(dev, "opcode invalid\n");
  1765. err = -EINVAL;
  1766. *bad_wr = wr;
  1767. goto out;
  1768. }
  1769. if (likely(prev_wqe)) {
  1770. ((struct mthca_next_seg *) prev_wqe)->nda_op =
  1771. cpu_to_be32(((ind << qp->sq.wqe_shift) +
  1772. qp->send_wqe_offset) |
  1773. mthca_opcode[wr->opcode]);
  1774. wmb();
  1775. ((struct mthca_next_seg *) prev_wqe)->ee_nds =
  1776. cpu_to_be32(MTHCA_NEXT_DBD | size);
  1777. }
  1778. if (!size0) {
  1779. size0 = size;
  1780. op0 = mthca_opcode[wr->opcode];
  1781. }
  1782. ++ind;
  1783. if (unlikely(ind >= qp->sq.max))
  1784. ind -= qp->sq.max;
  1785. }
  1786. out:
  1787. if (likely(nreq)) {
  1788. u32 doorbell[2];
  1789. doorbell[0] = cpu_to_be32((nreq << 24) |
  1790. ((qp->sq.head & 0xffff) << 8) |
  1791. f0 | op0);
  1792. doorbell[1] = cpu_to_be32((qp->qpn << 8) | size0);
  1793. qp->sq.head += nreq;
  1794. /*
  1795. * Make sure that descriptors are written before
  1796. * doorbell record.
  1797. */
  1798. wmb();
  1799. *qp->sq.db = cpu_to_be32(qp->sq.head & 0xffff);
  1800. /*
  1801. * Make sure doorbell record is written before we
  1802. * write MMIO send doorbell.
  1803. */
  1804. wmb();
  1805. mthca_write64(doorbell,
  1806. dev->kar + MTHCA_SEND_DOORBELL,
  1807. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  1808. }
  1809. spin_unlock_irqrestore(&qp->sq.lock, flags);
  1810. return err;
  1811. }
  1812. int mthca_arbel_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1813. struct ib_recv_wr **bad_wr)
  1814. {
  1815. struct mthca_dev *dev = to_mdev(ibqp->device);
  1816. struct mthca_qp *qp = to_mqp(ibqp);
  1817. unsigned long flags;
  1818. int err = 0;
  1819. int nreq;
  1820. int ind;
  1821. int i;
  1822. void *wqe;
  1823. spin_lock_irqsave(&qp->rq.lock, flags);
  1824. /* XXX check that state is OK to post receive */
  1825. ind = qp->rq.head & (qp->rq.max - 1);
  1826. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1827. if (mthca_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  1828. mthca_err(dev, "RQ %06x full (%u head, %u tail,"
  1829. " %d max, %d nreq)\n", qp->qpn,
  1830. qp->rq.head, qp->rq.tail,
  1831. qp->rq.max, nreq);
  1832. err = -ENOMEM;
  1833. *bad_wr = wr;
  1834. goto out;
  1835. }
  1836. wqe = get_recv_wqe(qp, ind);
  1837. ((struct mthca_next_seg *) wqe)->flags = 0;
  1838. wqe += sizeof (struct mthca_next_seg);
  1839. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  1840. err = -EINVAL;
  1841. *bad_wr = wr;
  1842. goto out;
  1843. }
  1844. for (i = 0; i < wr->num_sge; ++i) {
  1845. ((struct mthca_data_seg *) wqe)->byte_count =
  1846. cpu_to_be32(wr->sg_list[i].length);
  1847. ((struct mthca_data_seg *) wqe)->lkey =
  1848. cpu_to_be32(wr->sg_list[i].lkey);
  1849. ((struct mthca_data_seg *) wqe)->addr =
  1850. cpu_to_be64(wr->sg_list[i].addr);
  1851. wqe += sizeof (struct mthca_data_seg);
  1852. }
  1853. if (i < qp->rq.max_gs) {
  1854. ((struct mthca_data_seg *) wqe)->byte_count = 0;
  1855. ((struct mthca_data_seg *) wqe)->lkey = cpu_to_be32(MTHCA_INVAL_LKEY);
  1856. ((struct mthca_data_seg *) wqe)->addr = 0;
  1857. }
  1858. qp->wrid[ind] = wr->wr_id;
  1859. ++ind;
  1860. if (unlikely(ind >= qp->rq.max))
  1861. ind -= qp->rq.max;
  1862. }
  1863. out:
  1864. if (likely(nreq)) {
  1865. qp->rq.head += nreq;
  1866. /*
  1867. * Make sure that descriptors are written before
  1868. * doorbell record.
  1869. */
  1870. wmb();
  1871. *qp->rq.db = cpu_to_be32(qp->rq.head & 0xffff);
  1872. }
  1873. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1874. return err;
  1875. }
  1876. int mthca_free_err_wqe(struct mthca_dev *dev, struct mthca_qp *qp, int is_send,
  1877. int index, int *dbd, u32 *new_wqe)
  1878. {
  1879. struct mthca_next_seg *next;
  1880. if (is_send)
  1881. next = get_send_wqe(qp, index);
  1882. else
  1883. next = get_recv_wqe(qp, index);
  1884. if (mthca_is_memfree(dev))
  1885. *dbd = 1;
  1886. else
  1887. *dbd = !!(next->ee_nds & cpu_to_be32(MTHCA_NEXT_DBD));
  1888. if (next->ee_nds & cpu_to_be32(0x3f))
  1889. *new_wqe = (next->nda_op & cpu_to_be32(~0x3f)) |
  1890. (next->ee_nds & cpu_to_be32(0x3f));
  1891. else
  1892. *new_wqe = 0;
  1893. return 0;
  1894. }
  1895. int __devinit mthca_init_qp_table(struct mthca_dev *dev)
  1896. {
  1897. int err;
  1898. u8 status;
  1899. int i;
  1900. spin_lock_init(&dev->qp_table.lock);
  1901. /*
  1902. * We reserve 2 extra QPs per port for the special QPs. The
  1903. * special QP for port 1 has to be even, so round up.
  1904. */
  1905. dev->qp_table.sqp_start = (dev->limits.reserved_qps + 1) & ~1UL;
  1906. err = mthca_alloc_init(&dev->qp_table.alloc,
  1907. dev->limits.num_qps,
  1908. (1 << 24) - 1,
  1909. dev->qp_table.sqp_start +
  1910. MTHCA_MAX_PORTS * 2);
  1911. if (err)
  1912. return err;
  1913. err = mthca_array_init(&dev->qp_table.qp,
  1914. dev->limits.num_qps);
  1915. if (err) {
  1916. mthca_alloc_cleanup(&dev->qp_table.alloc);
  1917. return err;
  1918. }
  1919. for (i = 0; i < 2; ++i) {
  1920. err = mthca_CONF_SPECIAL_QP(dev, i ? IB_QPT_GSI : IB_QPT_SMI,
  1921. dev->qp_table.sqp_start + i * 2,
  1922. &status);
  1923. if (err)
  1924. goto err_out;
  1925. if (status) {
  1926. mthca_warn(dev, "CONF_SPECIAL_QP returned "
  1927. "status %02x, aborting.\n",
  1928. status);
  1929. err = -EINVAL;
  1930. goto err_out;
  1931. }
  1932. }
  1933. return 0;
  1934. err_out:
  1935. for (i = 0; i < 2; ++i)
  1936. mthca_CONF_SPECIAL_QP(dev, i, 0, &status);
  1937. mthca_array_cleanup(&dev->qp_table.qp, dev->limits.num_qps);
  1938. mthca_alloc_cleanup(&dev->qp_table.alloc);
  1939. return err;
  1940. }
  1941. void __devexit mthca_cleanup_qp_table(struct mthca_dev *dev)
  1942. {
  1943. int i;
  1944. u8 status;
  1945. for (i = 0; i < 2; ++i)
  1946. mthca_CONF_SPECIAL_QP(dev, i, 0, &status);
  1947. mthca_alloc_cleanup(&dev->qp_table.alloc);
  1948. }