mthca_qp.c 60 KB

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