mthca_qp.c 61 KB

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