mthca_qp.c 54 KB

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