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