mthca_qp.c 61 KB

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