mthca_qp.c 58 KB

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