mthca_qp.c 61 KB

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