mthca_qp.c 56 KB

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