mthca_qp.c 62 KB

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