mthca_qp.c 59 KB

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