mthca_qp.c 62 KB

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