mthca_qp.c 62 KB

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