mthca_qp.c 54 KB

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