qp.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/log2.h>
  34. #include <linux/slab.h>
  35. #include <linux/netdevice.h>
  36. #include <rdma/ib_cache.h>
  37. #include <rdma/ib_pack.h>
  38. #include <linux/mlx4/qp.h>
  39. #include "mlx4_ib.h"
  40. #include "user.h"
  41. enum {
  42. MLX4_IB_ACK_REQ_FREQ = 8,
  43. };
  44. enum {
  45. MLX4_IB_DEFAULT_SCHED_QUEUE = 0x83,
  46. MLX4_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
  47. MLX4_IB_LINK_TYPE_IB = 0,
  48. MLX4_IB_LINK_TYPE_ETH = 1
  49. };
  50. enum {
  51. /*
  52. * Largest possible UD header: send with GRH and immediate
  53. * data plus 14 bytes for an Ethernet header. (LRH would only
  54. * use 8 bytes, so Ethernet is the biggest case)
  55. */
  56. MLX4_IB_UD_HEADER_SIZE = 78,
  57. MLX4_IB_LSO_HEADER_SPARE = 128,
  58. };
  59. enum {
  60. MLX4_IB_IBOE_ETHERTYPE = 0x8915
  61. };
  62. struct mlx4_ib_sqp {
  63. struct mlx4_ib_qp qp;
  64. int pkey_index;
  65. u32 qkey;
  66. u32 send_psn;
  67. struct ib_ud_header ud_header;
  68. u8 header_buf[MLX4_IB_UD_HEADER_SIZE];
  69. };
  70. enum {
  71. MLX4_IB_MIN_SQ_STRIDE = 6,
  72. MLX4_IB_CACHE_LINE_SIZE = 64,
  73. };
  74. static const __be32 mlx4_ib_opcode[] = {
  75. [IB_WR_SEND] = cpu_to_be32(MLX4_OPCODE_SEND),
  76. [IB_WR_LSO] = cpu_to_be32(MLX4_OPCODE_LSO),
  77. [IB_WR_SEND_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_SEND_IMM),
  78. [IB_WR_RDMA_WRITE] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE),
  79. [IB_WR_RDMA_WRITE_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE_IMM),
  80. [IB_WR_RDMA_READ] = cpu_to_be32(MLX4_OPCODE_RDMA_READ),
  81. [IB_WR_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_ATOMIC_CS),
  82. [IB_WR_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_ATOMIC_FA),
  83. [IB_WR_SEND_WITH_INV] = cpu_to_be32(MLX4_OPCODE_SEND_INVAL),
  84. [IB_WR_LOCAL_INV] = cpu_to_be32(MLX4_OPCODE_LOCAL_INVAL),
  85. [IB_WR_FAST_REG_MR] = cpu_to_be32(MLX4_OPCODE_FMR),
  86. [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_CS),
  87. [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_FA),
  88. };
  89. static struct mlx4_ib_sqp *to_msqp(struct mlx4_ib_qp *mqp)
  90. {
  91. return container_of(mqp, struct mlx4_ib_sqp, qp);
  92. }
  93. static int is_sqp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  94. {
  95. return qp->mqp.qpn >= dev->dev->caps.sqp_start &&
  96. qp->mqp.qpn <= dev->dev->caps.sqp_start + 3;
  97. }
  98. static int is_qp0(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  99. {
  100. return qp->mqp.qpn >= dev->dev->caps.sqp_start &&
  101. qp->mqp.qpn <= dev->dev->caps.sqp_start + 1;
  102. }
  103. static void *get_wqe(struct mlx4_ib_qp *qp, int offset)
  104. {
  105. return mlx4_buf_offset(&qp->buf, offset);
  106. }
  107. static void *get_recv_wqe(struct mlx4_ib_qp *qp, int n)
  108. {
  109. return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
  110. }
  111. static void *get_send_wqe(struct mlx4_ib_qp *qp, int n)
  112. {
  113. return get_wqe(qp, qp->sq.offset + (n << qp->sq.wqe_shift));
  114. }
  115. /*
  116. * Stamp a SQ WQE so that it is invalid if prefetched by marking the
  117. * first four bytes of every 64 byte chunk with
  118. * 0x7FFFFFF | (invalid_ownership_value << 31).
  119. *
  120. * When the max work request size is less than or equal to the WQE
  121. * basic block size, as an optimization, we can stamp all WQEs with
  122. * 0xffffffff, and skip the very first chunk of each WQE.
  123. */
  124. static void stamp_send_wqe(struct mlx4_ib_qp *qp, int n, int size)
  125. {
  126. __be32 *wqe;
  127. int i;
  128. int s;
  129. int ind;
  130. void *buf;
  131. __be32 stamp;
  132. struct mlx4_wqe_ctrl_seg *ctrl;
  133. if (qp->sq_max_wqes_per_wr > 1) {
  134. s = roundup(size, 1U << qp->sq.wqe_shift);
  135. for (i = 0; i < s; i += 64) {
  136. ind = (i >> qp->sq.wqe_shift) + n;
  137. stamp = ind & qp->sq.wqe_cnt ? cpu_to_be32(0x7fffffff) :
  138. cpu_to_be32(0xffffffff);
  139. buf = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  140. wqe = buf + (i & ((1 << qp->sq.wqe_shift) - 1));
  141. *wqe = stamp;
  142. }
  143. } else {
  144. ctrl = buf = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
  145. s = (ctrl->fence_size & 0x3f) << 4;
  146. for (i = 64; i < s; i += 64) {
  147. wqe = buf + i;
  148. *wqe = cpu_to_be32(0xffffffff);
  149. }
  150. }
  151. }
  152. static void post_nop_wqe(struct mlx4_ib_qp *qp, int n, int size)
  153. {
  154. struct mlx4_wqe_ctrl_seg *ctrl;
  155. struct mlx4_wqe_inline_seg *inl;
  156. void *wqe;
  157. int s;
  158. ctrl = wqe = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
  159. s = sizeof(struct mlx4_wqe_ctrl_seg);
  160. if (qp->ibqp.qp_type == IB_QPT_UD) {
  161. struct mlx4_wqe_datagram_seg *dgram = wqe + sizeof *ctrl;
  162. struct mlx4_av *av = (struct mlx4_av *)dgram->av;
  163. memset(dgram, 0, sizeof *dgram);
  164. av->port_pd = cpu_to_be32((qp->port << 24) | to_mpd(qp->ibqp.pd)->pdn);
  165. s += sizeof(struct mlx4_wqe_datagram_seg);
  166. }
  167. /* Pad the remainder of the WQE with an inline data segment. */
  168. if (size > s) {
  169. inl = wqe + s;
  170. inl->byte_count = cpu_to_be32(1 << 31 | (size - s - sizeof *inl));
  171. }
  172. ctrl->srcrb_flags = 0;
  173. ctrl->fence_size = size / 16;
  174. /*
  175. * Make sure descriptor is fully written before setting ownership bit
  176. * (because HW can start executing as soon as we do).
  177. */
  178. wmb();
  179. ctrl->owner_opcode = cpu_to_be32(MLX4_OPCODE_NOP | MLX4_WQE_CTRL_NEC) |
  180. (n & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0);
  181. stamp_send_wqe(qp, n + qp->sq_spare_wqes, size);
  182. }
  183. /* Post NOP WQE to prevent wrap-around in the middle of WR */
  184. static inline unsigned pad_wraparound(struct mlx4_ib_qp *qp, int ind)
  185. {
  186. unsigned s = qp->sq.wqe_cnt - (ind & (qp->sq.wqe_cnt - 1));
  187. if (unlikely(s < qp->sq_max_wqes_per_wr)) {
  188. post_nop_wqe(qp, ind, s << qp->sq.wqe_shift);
  189. ind += s;
  190. }
  191. return ind;
  192. }
  193. static void mlx4_ib_qp_event(struct mlx4_qp *qp, enum mlx4_event type)
  194. {
  195. struct ib_event event;
  196. struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
  197. if (type == MLX4_EVENT_TYPE_PATH_MIG)
  198. to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
  199. if (ibqp->event_handler) {
  200. event.device = ibqp->device;
  201. event.element.qp = ibqp;
  202. switch (type) {
  203. case MLX4_EVENT_TYPE_PATH_MIG:
  204. event.event = IB_EVENT_PATH_MIG;
  205. break;
  206. case MLX4_EVENT_TYPE_COMM_EST:
  207. event.event = IB_EVENT_COMM_EST;
  208. break;
  209. case MLX4_EVENT_TYPE_SQ_DRAINED:
  210. event.event = IB_EVENT_SQ_DRAINED;
  211. break;
  212. case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
  213. event.event = IB_EVENT_QP_LAST_WQE_REACHED;
  214. break;
  215. case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
  216. event.event = IB_EVENT_QP_FATAL;
  217. break;
  218. case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
  219. event.event = IB_EVENT_PATH_MIG_ERR;
  220. break;
  221. case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  222. event.event = IB_EVENT_QP_REQ_ERR;
  223. break;
  224. case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
  225. event.event = IB_EVENT_QP_ACCESS_ERR;
  226. break;
  227. default:
  228. printk(KERN_WARNING "mlx4_ib: Unexpected event type %d "
  229. "on QP %06x\n", type, qp->qpn);
  230. return;
  231. }
  232. ibqp->event_handler(&event, ibqp->qp_context);
  233. }
  234. }
  235. static int send_wqe_overhead(enum ib_qp_type type, u32 flags)
  236. {
  237. /*
  238. * UD WQEs must have a datagram segment.
  239. * RC and UC WQEs might have a remote address segment.
  240. * MLX WQEs need two extra inline data segments (for the UD
  241. * header and space for the ICRC).
  242. */
  243. switch (type) {
  244. case IB_QPT_UD:
  245. return sizeof (struct mlx4_wqe_ctrl_seg) +
  246. sizeof (struct mlx4_wqe_datagram_seg) +
  247. ((flags & MLX4_IB_QP_LSO) ? MLX4_IB_LSO_HEADER_SPARE : 0);
  248. case IB_QPT_UC:
  249. return sizeof (struct mlx4_wqe_ctrl_seg) +
  250. sizeof (struct mlx4_wqe_raddr_seg);
  251. case IB_QPT_RC:
  252. return sizeof (struct mlx4_wqe_ctrl_seg) +
  253. sizeof (struct mlx4_wqe_atomic_seg) +
  254. sizeof (struct mlx4_wqe_raddr_seg);
  255. case IB_QPT_SMI:
  256. case IB_QPT_GSI:
  257. return sizeof (struct mlx4_wqe_ctrl_seg) +
  258. ALIGN(MLX4_IB_UD_HEADER_SIZE +
  259. DIV_ROUND_UP(MLX4_IB_UD_HEADER_SIZE,
  260. MLX4_INLINE_ALIGN) *
  261. sizeof (struct mlx4_wqe_inline_seg),
  262. sizeof (struct mlx4_wqe_data_seg)) +
  263. ALIGN(4 +
  264. sizeof (struct mlx4_wqe_inline_seg),
  265. sizeof (struct mlx4_wqe_data_seg));
  266. default:
  267. return sizeof (struct mlx4_wqe_ctrl_seg);
  268. }
  269. }
  270. static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  271. int is_user, int has_srq, struct mlx4_ib_qp *qp)
  272. {
  273. /* Sanity check RQ size before proceeding */
  274. if (cap->max_recv_wr > dev->dev->caps.max_wqes ||
  275. cap->max_recv_sge > dev->dev->caps.max_rq_sg)
  276. return -EINVAL;
  277. if (has_srq) {
  278. /* QPs attached to an SRQ should have no RQ */
  279. if (cap->max_recv_wr)
  280. return -EINVAL;
  281. qp->rq.wqe_cnt = qp->rq.max_gs = 0;
  282. } else {
  283. /* HW requires >= 1 RQ entry with >= 1 gather entry */
  284. if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge))
  285. return -EINVAL;
  286. qp->rq.wqe_cnt = roundup_pow_of_two(max(1U, cap->max_recv_wr));
  287. qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge));
  288. qp->rq.wqe_shift = ilog2(qp->rq.max_gs * sizeof (struct mlx4_wqe_data_seg));
  289. }
  290. cap->max_recv_wr = qp->rq.max_post = qp->rq.wqe_cnt;
  291. cap->max_recv_sge = qp->rq.max_gs;
  292. return 0;
  293. }
  294. static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  295. enum ib_qp_type type, struct mlx4_ib_qp *qp)
  296. {
  297. int s;
  298. /* Sanity check SQ size before proceeding */
  299. if (cap->max_send_wr > dev->dev->caps.max_wqes ||
  300. cap->max_send_sge > dev->dev->caps.max_sq_sg ||
  301. cap->max_inline_data + send_wqe_overhead(type, qp->flags) +
  302. sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
  303. return -EINVAL;
  304. /*
  305. * For MLX transport we need 2 extra S/G entries:
  306. * one for the header and one for the checksum at the end
  307. */
  308. if ((type == IB_QPT_SMI || type == IB_QPT_GSI) &&
  309. cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
  310. return -EINVAL;
  311. s = max(cap->max_send_sge * sizeof (struct mlx4_wqe_data_seg),
  312. cap->max_inline_data + sizeof (struct mlx4_wqe_inline_seg)) +
  313. send_wqe_overhead(type, qp->flags);
  314. if (s > dev->dev->caps.max_sq_desc_sz)
  315. return -EINVAL;
  316. /*
  317. * Hermon supports shrinking WQEs, such that a single work
  318. * request can include multiple units of 1 << wqe_shift. This
  319. * way, work requests can differ in size, and do not have to
  320. * be a power of 2 in size, saving memory and speeding up send
  321. * WR posting. Unfortunately, if we do this then the
  322. * wqe_index field in CQEs can't be used to look up the WR ID
  323. * anymore, so we do this only if selective signaling is off.
  324. *
  325. * Further, on 32-bit platforms, we can't use vmap() to make
  326. * the QP buffer virtually contiguous. Thus we have to use
  327. * constant-sized WRs to make sure a WR is always fully within
  328. * a single page-sized chunk.
  329. *
  330. * Finally, we use NOP work requests to pad the end of the
  331. * work queue, to avoid wrap-around in the middle of WR. We
  332. * set NEC bit to avoid getting completions with error for
  333. * these NOP WRs, but since NEC is only supported starting
  334. * with firmware 2.2.232, we use constant-sized WRs for older
  335. * firmware.
  336. *
  337. * And, since MLX QPs only support SEND, we use constant-sized
  338. * WRs in this case.
  339. *
  340. * We look for the smallest value of wqe_shift such that the
  341. * resulting number of wqes does not exceed device
  342. * capabilities.
  343. *
  344. * We set WQE size to at least 64 bytes, this way stamping
  345. * invalidates each WQE.
  346. */
  347. if (dev->dev->caps.fw_ver >= MLX4_FW_VER_WQE_CTRL_NEC &&
  348. qp->sq_signal_bits && BITS_PER_LONG == 64 &&
  349. type != IB_QPT_SMI && type != IB_QPT_GSI)
  350. qp->sq.wqe_shift = ilog2(64);
  351. else
  352. qp->sq.wqe_shift = ilog2(roundup_pow_of_two(s));
  353. for (;;) {
  354. qp->sq_max_wqes_per_wr = DIV_ROUND_UP(s, 1U << qp->sq.wqe_shift);
  355. /*
  356. * We need to leave 2 KB + 1 WR of headroom in the SQ to
  357. * allow HW to prefetch.
  358. */
  359. qp->sq_spare_wqes = (2048 >> qp->sq.wqe_shift) + qp->sq_max_wqes_per_wr;
  360. qp->sq.wqe_cnt = roundup_pow_of_two(cap->max_send_wr *
  361. qp->sq_max_wqes_per_wr +
  362. qp->sq_spare_wqes);
  363. if (qp->sq.wqe_cnt <= dev->dev->caps.max_wqes)
  364. break;
  365. if (qp->sq_max_wqes_per_wr <= 1)
  366. return -EINVAL;
  367. ++qp->sq.wqe_shift;
  368. }
  369. qp->sq.max_gs = (min(dev->dev->caps.max_sq_desc_sz,
  370. (qp->sq_max_wqes_per_wr << qp->sq.wqe_shift)) -
  371. send_wqe_overhead(type, qp->flags)) /
  372. sizeof (struct mlx4_wqe_data_seg);
  373. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  374. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  375. if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
  376. qp->rq.offset = 0;
  377. qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
  378. } else {
  379. qp->rq.offset = qp->sq.wqe_cnt << qp->sq.wqe_shift;
  380. qp->sq.offset = 0;
  381. }
  382. cap->max_send_wr = qp->sq.max_post =
  383. (qp->sq.wqe_cnt - qp->sq_spare_wqes) / qp->sq_max_wqes_per_wr;
  384. cap->max_send_sge = min(qp->sq.max_gs,
  385. min(dev->dev->caps.max_sq_sg,
  386. dev->dev->caps.max_rq_sg));
  387. /* We don't support inline sends for kernel QPs (yet) */
  388. cap->max_inline_data = 0;
  389. return 0;
  390. }
  391. static int set_user_sq_size(struct mlx4_ib_dev *dev,
  392. struct mlx4_ib_qp *qp,
  393. struct mlx4_ib_create_qp *ucmd)
  394. {
  395. /* Sanity check SQ size before proceeding */
  396. if ((1 << ucmd->log_sq_bb_count) > dev->dev->caps.max_wqes ||
  397. ucmd->log_sq_stride >
  398. ilog2(roundup_pow_of_two(dev->dev->caps.max_sq_desc_sz)) ||
  399. ucmd->log_sq_stride < MLX4_IB_MIN_SQ_STRIDE)
  400. return -EINVAL;
  401. qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count;
  402. qp->sq.wqe_shift = ucmd->log_sq_stride;
  403. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  404. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  405. return 0;
  406. }
  407. static int create_qp_common(struct mlx4_ib_dev *dev, struct ib_pd *pd,
  408. struct ib_qp_init_attr *init_attr,
  409. struct ib_udata *udata, int sqpn, struct mlx4_ib_qp *qp)
  410. {
  411. int qpn;
  412. int err;
  413. mutex_init(&qp->mutex);
  414. spin_lock_init(&qp->sq.lock);
  415. spin_lock_init(&qp->rq.lock);
  416. INIT_LIST_HEAD(&qp->gid_list);
  417. qp->state = IB_QPS_RESET;
  418. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  419. qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  420. err = set_rq_size(dev, &init_attr->cap, !!pd->uobject, !!init_attr->srq, qp);
  421. if (err)
  422. goto err;
  423. if (pd->uobject) {
  424. struct mlx4_ib_create_qp ucmd;
  425. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  426. err = -EFAULT;
  427. goto err;
  428. }
  429. qp->sq_no_prefetch = ucmd.sq_no_prefetch;
  430. err = set_user_sq_size(dev, qp, &ucmd);
  431. if (err)
  432. goto err;
  433. qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
  434. qp->buf_size, 0, 0);
  435. if (IS_ERR(qp->umem)) {
  436. err = PTR_ERR(qp->umem);
  437. goto err;
  438. }
  439. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(qp->umem),
  440. ilog2(qp->umem->page_size), &qp->mtt);
  441. if (err)
  442. goto err_buf;
  443. err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
  444. if (err)
  445. goto err_mtt;
  446. if (!init_attr->srq) {
  447. err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
  448. ucmd.db_addr, &qp->db);
  449. if (err)
  450. goto err_mtt;
  451. }
  452. } else {
  453. qp->sq_no_prefetch = 0;
  454. if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
  455. qp->flags |= MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
  456. if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
  457. qp->flags |= MLX4_IB_QP_LSO;
  458. err = set_kernel_sq_size(dev, &init_attr->cap, init_attr->qp_type, qp);
  459. if (err)
  460. goto err;
  461. if (!init_attr->srq) {
  462. err = mlx4_db_alloc(dev->dev, &qp->db, 0);
  463. if (err)
  464. goto err;
  465. *qp->db.db = 0;
  466. }
  467. if (mlx4_buf_alloc(dev->dev, qp->buf_size, PAGE_SIZE * 2, &qp->buf)) {
  468. err = -ENOMEM;
  469. goto err_db;
  470. }
  471. err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
  472. &qp->mtt);
  473. if (err)
  474. goto err_buf;
  475. err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf);
  476. if (err)
  477. goto err_mtt;
  478. qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof (u64), GFP_KERNEL);
  479. qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof (u64), GFP_KERNEL);
  480. if (!qp->sq.wrid || !qp->rq.wrid) {
  481. err = -ENOMEM;
  482. goto err_wrid;
  483. }
  484. }
  485. if (sqpn) {
  486. qpn = sqpn;
  487. } else {
  488. err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn);
  489. if (err)
  490. goto err_wrid;
  491. }
  492. err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp);
  493. if (err)
  494. goto err_qpn;
  495. /*
  496. * Hardware wants QPN written in big-endian order (after
  497. * shifting) for send doorbell. Precompute this value to save
  498. * a little bit when posting sends.
  499. */
  500. qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
  501. qp->mqp.event = mlx4_ib_qp_event;
  502. return 0;
  503. err_qpn:
  504. if (!sqpn)
  505. mlx4_qp_release_range(dev->dev, qpn, 1);
  506. err_wrid:
  507. if (pd->uobject) {
  508. if (!init_attr->srq)
  509. mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context),
  510. &qp->db);
  511. } else {
  512. kfree(qp->sq.wrid);
  513. kfree(qp->rq.wrid);
  514. }
  515. err_mtt:
  516. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  517. err_buf:
  518. if (pd->uobject)
  519. ib_umem_release(qp->umem);
  520. else
  521. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  522. err_db:
  523. if (!pd->uobject && !init_attr->srq)
  524. mlx4_db_free(dev->dev, &qp->db);
  525. err:
  526. return err;
  527. }
  528. static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
  529. {
  530. switch (state) {
  531. case IB_QPS_RESET: return MLX4_QP_STATE_RST;
  532. case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
  533. case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
  534. case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
  535. case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
  536. case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
  537. case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
  538. default: return -1;
  539. }
  540. }
  541. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  542. __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
  543. {
  544. if (send_cq == recv_cq) {
  545. spin_lock_irq(&send_cq->lock);
  546. __acquire(&recv_cq->lock);
  547. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  548. spin_lock_irq(&send_cq->lock);
  549. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  550. } else {
  551. spin_lock_irq(&recv_cq->lock);
  552. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  553. }
  554. }
  555. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  556. __releases(&send_cq->lock) __releases(&recv_cq->lock)
  557. {
  558. if (send_cq == recv_cq) {
  559. __release(&recv_cq->lock);
  560. spin_unlock_irq(&send_cq->lock);
  561. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  562. spin_unlock(&recv_cq->lock);
  563. spin_unlock_irq(&send_cq->lock);
  564. } else {
  565. spin_unlock(&send_cq->lock);
  566. spin_unlock_irq(&recv_cq->lock);
  567. }
  568. }
  569. static void del_gid_entries(struct mlx4_ib_qp *qp)
  570. {
  571. struct mlx4_ib_gid_entry *ge, *tmp;
  572. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  573. list_del(&ge->list);
  574. kfree(ge);
  575. }
  576. }
  577. static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
  578. int is_user)
  579. {
  580. struct mlx4_ib_cq *send_cq, *recv_cq;
  581. if (qp->state != IB_QPS_RESET)
  582. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  583. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  584. printk(KERN_WARNING "mlx4_ib: modify QP %06x to RESET failed.\n",
  585. qp->mqp.qpn);
  586. send_cq = to_mcq(qp->ibqp.send_cq);
  587. recv_cq = to_mcq(qp->ibqp.recv_cq);
  588. mlx4_ib_lock_cqs(send_cq, recv_cq);
  589. if (!is_user) {
  590. __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  591. qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
  592. if (send_cq != recv_cq)
  593. __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  594. }
  595. mlx4_qp_remove(dev->dev, &qp->mqp);
  596. mlx4_ib_unlock_cqs(send_cq, recv_cq);
  597. mlx4_qp_free(dev->dev, &qp->mqp);
  598. if (!is_sqp(dev, qp))
  599. mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
  600. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  601. if (is_user) {
  602. if (!qp->ibqp.srq)
  603. mlx4_ib_db_unmap_user(to_mucontext(qp->ibqp.uobject->context),
  604. &qp->db);
  605. ib_umem_release(qp->umem);
  606. } else {
  607. kfree(qp->sq.wrid);
  608. kfree(qp->rq.wrid);
  609. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  610. if (!qp->ibqp.srq)
  611. mlx4_db_free(dev->dev, &qp->db);
  612. }
  613. del_gid_entries(qp);
  614. }
  615. struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
  616. struct ib_qp_init_attr *init_attr,
  617. struct ib_udata *udata)
  618. {
  619. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  620. struct mlx4_ib_sqp *sqp;
  621. struct mlx4_ib_qp *qp;
  622. int err;
  623. /*
  624. * We only support LSO and multicast loopback blocking, and
  625. * only for kernel UD QPs.
  626. */
  627. if (init_attr->create_flags & ~(IB_QP_CREATE_IPOIB_UD_LSO |
  628. IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK))
  629. return ERR_PTR(-EINVAL);
  630. if (init_attr->create_flags &&
  631. (pd->uobject || init_attr->qp_type != IB_QPT_UD))
  632. return ERR_PTR(-EINVAL);
  633. switch (init_attr->qp_type) {
  634. case IB_QPT_RC:
  635. case IB_QPT_UC:
  636. case IB_QPT_UD:
  637. {
  638. qp = kzalloc(sizeof *qp, GFP_KERNEL);
  639. if (!qp)
  640. return ERR_PTR(-ENOMEM);
  641. err = create_qp_common(dev, pd, init_attr, udata, 0, qp);
  642. if (err) {
  643. kfree(qp);
  644. return ERR_PTR(err);
  645. }
  646. qp->ibqp.qp_num = qp->mqp.qpn;
  647. break;
  648. }
  649. case IB_QPT_SMI:
  650. case IB_QPT_GSI:
  651. {
  652. /* Userspace is not allowed to create special QPs: */
  653. if (pd->uobject)
  654. return ERR_PTR(-EINVAL);
  655. sqp = kzalloc(sizeof *sqp, GFP_KERNEL);
  656. if (!sqp)
  657. return ERR_PTR(-ENOMEM);
  658. qp = &sqp->qp;
  659. err = create_qp_common(dev, pd, init_attr, udata,
  660. dev->dev->caps.sqp_start +
  661. (init_attr->qp_type == IB_QPT_SMI ? 0 : 2) +
  662. init_attr->port_num - 1,
  663. qp);
  664. if (err) {
  665. kfree(sqp);
  666. return ERR_PTR(err);
  667. }
  668. qp->port = init_attr->port_num;
  669. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  670. break;
  671. }
  672. default:
  673. /* Don't support raw QPs */
  674. return ERR_PTR(-EINVAL);
  675. }
  676. return &qp->ibqp;
  677. }
  678. int mlx4_ib_destroy_qp(struct ib_qp *qp)
  679. {
  680. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  681. struct mlx4_ib_qp *mqp = to_mqp(qp);
  682. if (is_qp0(dev, mqp))
  683. mlx4_CLOSE_PORT(dev->dev, mqp->port);
  684. destroy_qp_common(dev, mqp, !!qp->pd->uobject);
  685. if (is_sqp(dev, mqp))
  686. kfree(to_msqp(mqp));
  687. else
  688. kfree(mqp);
  689. return 0;
  690. }
  691. static int to_mlx4_st(enum ib_qp_type type)
  692. {
  693. switch (type) {
  694. case IB_QPT_RC: return MLX4_QP_ST_RC;
  695. case IB_QPT_UC: return MLX4_QP_ST_UC;
  696. case IB_QPT_UD: return MLX4_QP_ST_UD;
  697. case IB_QPT_SMI:
  698. case IB_QPT_GSI: return MLX4_QP_ST_MLX;
  699. default: return -1;
  700. }
  701. }
  702. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  703. int attr_mask)
  704. {
  705. u8 dest_rd_atomic;
  706. u32 access_flags;
  707. u32 hw_access_flags = 0;
  708. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  709. dest_rd_atomic = attr->max_dest_rd_atomic;
  710. else
  711. dest_rd_atomic = qp->resp_depth;
  712. if (attr_mask & IB_QP_ACCESS_FLAGS)
  713. access_flags = attr->qp_access_flags;
  714. else
  715. access_flags = qp->atomic_rd_en;
  716. if (!dest_rd_atomic)
  717. access_flags &= IB_ACCESS_REMOTE_WRITE;
  718. if (access_flags & IB_ACCESS_REMOTE_READ)
  719. hw_access_flags |= MLX4_QP_BIT_RRE;
  720. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  721. hw_access_flags |= MLX4_QP_BIT_RAE;
  722. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  723. hw_access_flags |= MLX4_QP_BIT_RWE;
  724. return cpu_to_be32(hw_access_flags);
  725. }
  726. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  727. int attr_mask)
  728. {
  729. if (attr_mask & IB_QP_PKEY_INDEX)
  730. sqp->pkey_index = attr->pkey_index;
  731. if (attr_mask & IB_QP_QKEY)
  732. sqp->qkey = attr->qkey;
  733. if (attr_mask & IB_QP_SQ_PSN)
  734. sqp->send_psn = attr->sq_psn;
  735. }
  736. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  737. {
  738. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  739. }
  740. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_ah_attr *ah,
  741. struct mlx4_qp_path *path, u8 port)
  742. {
  743. int err;
  744. int is_eth = rdma_port_get_link_layer(&dev->ib_dev, port) ==
  745. IB_LINK_LAYER_ETHERNET;
  746. u8 mac[6];
  747. int is_mcast;
  748. path->grh_mylmc = ah->src_path_bits & 0x7f;
  749. path->rlid = cpu_to_be16(ah->dlid);
  750. if (ah->static_rate) {
  751. path->static_rate = ah->static_rate + MLX4_STAT_RATE_OFFSET;
  752. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  753. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  754. --path->static_rate;
  755. } else
  756. path->static_rate = 0;
  757. path->counter_index = 0xff;
  758. if (ah->ah_flags & IB_AH_GRH) {
  759. if (ah->grh.sgid_index >= dev->dev->caps.gid_table_len[port]) {
  760. printk(KERN_ERR "sgid_index (%u) too large. max is %d\n",
  761. ah->grh.sgid_index, dev->dev->caps.gid_table_len[port] - 1);
  762. return -1;
  763. }
  764. path->grh_mylmc |= 1 << 7;
  765. path->mgid_index = ah->grh.sgid_index;
  766. path->hop_limit = ah->grh.hop_limit;
  767. path->tclass_flowlabel =
  768. cpu_to_be32((ah->grh.traffic_class << 20) |
  769. (ah->grh.flow_label));
  770. memcpy(path->rgid, ah->grh.dgid.raw, 16);
  771. }
  772. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  773. ((port - 1) << 6) | ((ah->sl & 0xf) << 2);
  774. if (is_eth) {
  775. if (!(ah->ah_flags & IB_AH_GRH))
  776. return -1;
  777. err = mlx4_ib_resolve_grh(dev, ah, mac, &is_mcast, port);
  778. if (err)
  779. return err;
  780. memcpy(path->dmac, mac, 6);
  781. path->ackto = MLX4_IB_LINK_TYPE_ETH;
  782. /* use index 0 into MAC table for IBoE */
  783. path->grh_mylmc &= 0x80;
  784. }
  785. return 0;
  786. }
  787. static void update_mcg_macs(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  788. {
  789. struct mlx4_ib_gid_entry *ge, *tmp;
  790. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  791. if (!ge->added && mlx4_ib_add_mc(dev, qp, &ge->gid)) {
  792. ge->added = 1;
  793. ge->port = qp->port;
  794. }
  795. }
  796. }
  797. static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
  798. const struct ib_qp_attr *attr, int attr_mask,
  799. enum ib_qp_state cur_state, enum ib_qp_state new_state)
  800. {
  801. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  802. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  803. struct mlx4_qp_context *context;
  804. enum mlx4_qp_optpar optpar = 0;
  805. int sqd_event;
  806. int err = -EINVAL;
  807. context = kzalloc(sizeof *context, GFP_KERNEL);
  808. if (!context)
  809. return -ENOMEM;
  810. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  811. (to_mlx4_st(ibqp->qp_type) << 16));
  812. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  813. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  814. else {
  815. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  816. switch (attr->path_mig_state) {
  817. case IB_MIG_MIGRATED:
  818. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  819. break;
  820. case IB_MIG_REARM:
  821. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  822. break;
  823. case IB_MIG_ARMED:
  824. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  825. break;
  826. }
  827. }
  828. if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI)
  829. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  830. else if (ibqp->qp_type == IB_QPT_UD) {
  831. if (qp->flags & MLX4_IB_QP_LSO)
  832. context->mtu_msgmax = (IB_MTU_4096 << 5) |
  833. ilog2(dev->dev->caps.max_gso_sz);
  834. else
  835. context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
  836. } else if (attr_mask & IB_QP_PATH_MTU) {
  837. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  838. printk(KERN_ERR "path MTU (%u) is invalid\n",
  839. attr->path_mtu);
  840. goto out;
  841. }
  842. context->mtu_msgmax = (attr->path_mtu << 5) |
  843. ilog2(dev->dev->caps.max_msg_sz);
  844. }
  845. if (qp->rq.wqe_cnt)
  846. context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
  847. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  848. if (qp->sq.wqe_cnt)
  849. context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
  850. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  851. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  852. context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
  853. if (qp->ibqp.uobject)
  854. context->usr_page = cpu_to_be32(to_mucontext(ibqp->uobject->context)->uar.index);
  855. else
  856. context->usr_page = cpu_to_be32(dev->priv_uar.index);
  857. if (attr_mask & IB_QP_DEST_QPN)
  858. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  859. if (attr_mask & IB_QP_PORT) {
  860. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  861. !(attr_mask & IB_QP_AV)) {
  862. mlx4_set_sched(&context->pri_path, attr->port_num);
  863. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  864. }
  865. }
  866. if (attr_mask & IB_QP_PKEY_INDEX) {
  867. context->pri_path.pkey_index = attr->pkey_index;
  868. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  869. }
  870. if (attr_mask & IB_QP_AV) {
  871. if (mlx4_set_path(dev, &attr->ah_attr, &context->pri_path,
  872. attr_mask & IB_QP_PORT ? attr->port_num : qp->port))
  873. goto out;
  874. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  875. MLX4_QP_OPTPAR_SCHED_QUEUE);
  876. }
  877. if (attr_mask & IB_QP_TIMEOUT) {
  878. context->pri_path.ackto |= attr->timeout << 3;
  879. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  880. }
  881. if (attr_mask & IB_QP_ALT_PATH) {
  882. if (attr->alt_port_num == 0 ||
  883. attr->alt_port_num > dev->dev->caps.num_ports)
  884. goto out;
  885. if (attr->alt_pkey_index >=
  886. dev->dev->caps.pkey_table_len[attr->alt_port_num])
  887. goto out;
  888. if (mlx4_set_path(dev, &attr->alt_ah_attr, &context->alt_path,
  889. attr->alt_port_num))
  890. goto out;
  891. context->alt_path.pkey_index = attr->alt_pkey_index;
  892. context->alt_path.ackto = attr->alt_timeout << 3;
  893. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  894. }
  895. context->pd = cpu_to_be32(to_mpd(ibqp->pd)->pdn);
  896. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  897. /* Set "fast registration enabled" for all kernel QPs */
  898. if (!qp->ibqp.uobject)
  899. context->params1 |= cpu_to_be32(1 << 11);
  900. if (attr_mask & IB_QP_RNR_RETRY) {
  901. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  902. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  903. }
  904. if (attr_mask & IB_QP_RETRY_CNT) {
  905. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  906. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  907. }
  908. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  909. if (attr->max_rd_atomic)
  910. context->params1 |=
  911. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  912. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  913. }
  914. if (attr_mask & IB_QP_SQ_PSN)
  915. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  916. context->cqn_send = cpu_to_be32(to_mcq(ibqp->send_cq)->mcq.cqn);
  917. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  918. if (attr->max_dest_rd_atomic)
  919. context->params2 |=
  920. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  921. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  922. }
  923. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  924. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  925. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  926. }
  927. if (ibqp->srq)
  928. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  929. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  930. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  931. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  932. }
  933. if (attr_mask & IB_QP_RQ_PSN)
  934. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  935. context->cqn_recv = cpu_to_be32(to_mcq(ibqp->recv_cq)->mcq.cqn);
  936. if (attr_mask & IB_QP_QKEY) {
  937. context->qkey = cpu_to_be32(attr->qkey);
  938. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  939. }
  940. if (ibqp->srq)
  941. context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
  942. if (!ibqp->srq && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  943. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  944. if (cur_state == IB_QPS_INIT &&
  945. new_state == IB_QPS_RTR &&
  946. (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  947. ibqp->qp_type == IB_QPT_UD)) {
  948. context->pri_path.sched_queue = (qp->port - 1) << 6;
  949. if (is_qp0(dev, qp))
  950. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  951. else
  952. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  953. }
  954. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  955. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  956. sqd_event = 1;
  957. else
  958. sqd_event = 0;
  959. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  960. context->rlkey |= (1 << 4);
  961. /*
  962. * Before passing a kernel QP to the HW, make sure that the
  963. * ownership bits of the send queue are set and the SQ
  964. * headroom is stamped so that the hardware doesn't start
  965. * processing stale work requests.
  966. */
  967. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  968. struct mlx4_wqe_ctrl_seg *ctrl;
  969. int i;
  970. for (i = 0; i < qp->sq.wqe_cnt; ++i) {
  971. ctrl = get_send_wqe(qp, i);
  972. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  973. if (qp->sq_max_wqes_per_wr == 1)
  974. ctrl->fence_size = 1 << (qp->sq.wqe_shift - 4);
  975. stamp_send_wqe(qp, i, 1 << qp->sq.wqe_shift);
  976. }
  977. }
  978. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  979. to_mlx4_state(new_state), context, optpar,
  980. sqd_event, &qp->mqp);
  981. if (err)
  982. goto out;
  983. qp->state = new_state;
  984. if (attr_mask & IB_QP_ACCESS_FLAGS)
  985. qp->atomic_rd_en = attr->qp_access_flags;
  986. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  987. qp->resp_depth = attr->max_dest_rd_atomic;
  988. if (attr_mask & IB_QP_PORT) {
  989. qp->port = attr->port_num;
  990. update_mcg_macs(dev, qp);
  991. }
  992. if (attr_mask & IB_QP_ALT_PATH)
  993. qp->alt_port = attr->alt_port_num;
  994. if (is_sqp(dev, qp))
  995. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  996. /*
  997. * If we moved QP0 to RTR, bring the IB link up; if we moved
  998. * QP0 to RESET or ERROR, bring the link back down.
  999. */
  1000. if (is_qp0(dev, qp)) {
  1001. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  1002. if (mlx4_INIT_PORT(dev->dev, qp->port))
  1003. printk(KERN_WARNING "INIT_PORT failed for port %d\n",
  1004. qp->port);
  1005. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  1006. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  1007. mlx4_CLOSE_PORT(dev->dev, qp->port);
  1008. }
  1009. /*
  1010. * If we moved a kernel QP to RESET, clean up all old CQ
  1011. * entries and reinitialize the QP.
  1012. */
  1013. if (new_state == IB_QPS_RESET && !ibqp->uobject) {
  1014. mlx4_ib_cq_clean(to_mcq(ibqp->recv_cq), qp->mqp.qpn,
  1015. ibqp->srq ? to_msrq(ibqp->srq): NULL);
  1016. if (ibqp->send_cq != ibqp->recv_cq)
  1017. mlx4_ib_cq_clean(to_mcq(ibqp->send_cq), qp->mqp.qpn, NULL);
  1018. qp->rq.head = 0;
  1019. qp->rq.tail = 0;
  1020. qp->sq.head = 0;
  1021. qp->sq.tail = 0;
  1022. qp->sq_next_wqe = 0;
  1023. if (!ibqp->srq)
  1024. *qp->db.db = 0;
  1025. }
  1026. out:
  1027. kfree(context);
  1028. return err;
  1029. }
  1030. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1031. int attr_mask, struct ib_udata *udata)
  1032. {
  1033. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1034. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1035. enum ib_qp_state cur_state, new_state;
  1036. int err = -EINVAL;
  1037. mutex_lock(&qp->mutex);
  1038. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  1039. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  1040. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask))
  1041. goto out;
  1042. if ((attr_mask & IB_QP_PORT) &&
  1043. (attr->port_num == 0 || attr->port_num > dev->dev->caps.num_ports)) {
  1044. goto out;
  1045. }
  1046. if (attr_mask & IB_QP_PKEY_INDEX) {
  1047. int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1048. if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p])
  1049. goto out;
  1050. }
  1051. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  1052. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  1053. goto out;
  1054. }
  1055. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  1056. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  1057. goto out;
  1058. }
  1059. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  1060. err = 0;
  1061. goto out;
  1062. }
  1063. err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
  1064. out:
  1065. mutex_unlock(&qp->mutex);
  1066. return err;
  1067. }
  1068. static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_send_wr *wr,
  1069. void *wqe, unsigned *mlx_seg_len)
  1070. {
  1071. struct ib_device *ib_dev = sqp->qp.ibqp.device;
  1072. struct mlx4_wqe_mlx_seg *mlx = wqe;
  1073. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  1074. struct mlx4_ib_ah *ah = to_mah(wr->wr.ud.ah);
  1075. u16 pkey;
  1076. int send_size;
  1077. int header_size;
  1078. int spc;
  1079. int i;
  1080. int is_eth;
  1081. int is_grh;
  1082. send_size = 0;
  1083. for (i = 0; i < wr->num_sge; ++i)
  1084. send_size += wr->sg_list[i].length;
  1085. is_eth = rdma_port_get_link_layer(sqp->qp.ibqp.device, sqp->qp.port) == IB_LINK_LAYER_ETHERNET;
  1086. is_grh = mlx4_ib_ah_grh_present(ah);
  1087. ib_ud_header_init(send_size, !is_eth, is_eth, is_grh, 0, &sqp->ud_header);
  1088. if (!is_eth) {
  1089. sqp->ud_header.lrh.service_level =
  1090. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  1091. sqp->ud_header.lrh.destination_lid = ah->av.ib.dlid;
  1092. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  1093. }
  1094. if (is_grh) {
  1095. sqp->ud_header.grh.traffic_class =
  1096. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  1097. sqp->ud_header.grh.flow_label =
  1098. ah->av.ib.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  1099. sqp->ud_header.grh.hop_limit = ah->av.ib.hop_limit;
  1100. ib_get_cached_gid(ib_dev, be32_to_cpu(ah->av.ib.port_pd) >> 24,
  1101. ah->av.ib.gid_index, &sqp->ud_header.grh.source_gid);
  1102. memcpy(sqp->ud_header.grh.destination_gid.raw,
  1103. ah->av.ib.dgid, 16);
  1104. }
  1105. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  1106. if (!is_eth) {
  1107. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  1108. (sqp->ud_header.lrh.destination_lid ==
  1109. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  1110. (sqp->ud_header.lrh.service_level << 8));
  1111. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  1112. }
  1113. switch (wr->opcode) {
  1114. case IB_WR_SEND:
  1115. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  1116. sqp->ud_header.immediate_present = 0;
  1117. break;
  1118. case IB_WR_SEND_WITH_IMM:
  1119. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  1120. sqp->ud_header.immediate_present = 1;
  1121. sqp->ud_header.immediate_data = wr->ex.imm_data;
  1122. break;
  1123. default:
  1124. return -EINVAL;
  1125. }
  1126. if (is_eth) {
  1127. u8 *smac;
  1128. memcpy(sqp->ud_header.eth.dmac_h, ah->av.eth.mac, 6);
  1129. /* FIXME: cache smac value? */
  1130. smac = to_mdev(sqp->qp.ibqp.device)->iboe.netdevs[sqp->qp.port - 1]->dev_addr;
  1131. memcpy(sqp->ud_header.eth.smac_h, smac, 6);
  1132. if (!memcmp(sqp->ud_header.eth.smac_h, sqp->ud_header.eth.dmac_h, 6))
  1133. mlx->flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
  1134. sqp->ud_header.eth.type = cpu_to_be16(MLX4_IB_IBOE_ETHERTYPE);
  1135. } else {
  1136. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
  1137. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  1138. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  1139. }
  1140. sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
  1141. if (!sqp->qp.ibqp.qp_num)
  1142. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  1143. else
  1144. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->wr.ud.pkey_index, &pkey);
  1145. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  1146. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  1147. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  1148. sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
  1149. sqp->qkey : wr->wr.ud.remote_qkey);
  1150. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  1151. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  1152. if (0) {
  1153. printk(KERN_ERR "built UD header of size %d:\n", header_size);
  1154. for (i = 0; i < header_size / 4; ++i) {
  1155. if (i % 8 == 0)
  1156. printk(" [%02x] ", i * 4);
  1157. printk(" %08x",
  1158. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  1159. if ((i + 1) % 8 == 0)
  1160. printk("\n");
  1161. }
  1162. printk("\n");
  1163. }
  1164. /*
  1165. * Inline data segments may not cross a 64 byte boundary. If
  1166. * our UD header is bigger than the space available up to the
  1167. * next 64 byte boundary in the WQE, use two inline data
  1168. * segments to hold the UD header.
  1169. */
  1170. spc = MLX4_INLINE_ALIGN -
  1171. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  1172. if (header_size <= spc) {
  1173. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  1174. memcpy(inl + 1, sqp->header_buf, header_size);
  1175. i = 1;
  1176. } else {
  1177. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  1178. memcpy(inl + 1, sqp->header_buf, spc);
  1179. inl = (void *) (inl + 1) + spc;
  1180. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  1181. /*
  1182. * Need a barrier here to make sure all the data is
  1183. * visible before the byte_count field is set.
  1184. * Otherwise the HCA prefetcher could grab the 64-byte
  1185. * chunk with this inline segment and get a valid (!=
  1186. * 0xffffffff) byte count but stale data, and end up
  1187. * generating a packet with bad headers.
  1188. *
  1189. * The first inline segment's byte_count field doesn't
  1190. * need a barrier, because it comes after a
  1191. * control/MLX segment and therefore is at an offset
  1192. * of 16 mod 64.
  1193. */
  1194. wmb();
  1195. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  1196. i = 2;
  1197. }
  1198. *mlx_seg_len =
  1199. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  1200. return 0;
  1201. }
  1202. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  1203. {
  1204. unsigned cur;
  1205. struct mlx4_ib_cq *cq;
  1206. cur = wq->head - wq->tail;
  1207. if (likely(cur + nreq < wq->max_post))
  1208. return 0;
  1209. cq = to_mcq(ib_cq);
  1210. spin_lock(&cq->lock);
  1211. cur = wq->head - wq->tail;
  1212. spin_unlock(&cq->lock);
  1213. return cur + nreq >= wq->max_post;
  1214. }
  1215. static __be32 convert_access(int acc)
  1216. {
  1217. return (acc & IB_ACCESS_REMOTE_ATOMIC ? cpu_to_be32(MLX4_WQE_FMR_PERM_ATOMIC) : 0) |
  1218. (acc & IB_ACCESS_REMOTE_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_REMOTE_WRITE) : 0) |
  1219. (acc & IB_ACCESS_REMOTE_READ ? cpu_to_be32(MLX4_WQE_FMR_PERM_REMOTE_READ) : 0) |
  1220. (acc & IB_ACCESS_LOCAL_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_WRITE) : 0) |
  1221. cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_READ);
  1222. }
  1223. static void set_fmr_seg(struct mlx4_wqe_fmr_seg *fseg, struct ib_send_wr *wr)
  1224. {
  1225. struct mlx4_ib_fast_reg_page_list *mfrpl = to_mfrpl(wr->wr.fast_reg.page_list);
  1226. int i;
  1227. for (i = 0; i < wr->wr.fast_reg.page_list_len; ++i)
  1228. mfrpl->mapped_page_list[i] =
  1229. cpu_to_be64(wr->wr.fast_reg.page_list->page_list[i] |
  1230. MLX4_MTT_FLAG_PRESENT);
  1231. fseg->flags = convert_access(wr->wr.fast_reg.access_flags);
  1232. fseg->mem_key = cpu_to_be32(wr->wr.fast_reg.rkey);
  1233. fseg->buf_list = cpu_to_be64(mfrpl->map);
  1234. fseg->start_addr = cpu_to_be64(wr->wr.fast_reg.iova_start);
  1235. fseg->reg_len = cpu_to_be64(wr->wr.fast_reg.length);
  1236. fseg->offset = 0; /* XXX -- is this just for ZBVA? */
  1237. fseg->page_size = cpu_to_be32(wr->wr.fast_reg.page_shift);
  1238. fseg->reserved[0] = 0;
  1239. fseg->reserved[1] = 0;
  1240. }
  1241. static void set_local_inv_seg(struct mlx4_wqe_local_inval_seg *iseg, u32 rkey)
  1242. {
  1243. iseg->flags = 0;
  1244. iseg->mem_key = cpu_to_be32(rkey);
  1245. iseg->guest_id = 0;
  1246. iseg->pa = 0;
  1247. }
  1248. static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg,
  1249. u64 remote_addr, u32 rkey)
  1250. {
  1251. rseg->raddr = cpu_to_be64(remote_addr);
  1252. rseg->rkey = cpu_to_be32(rkey);
  1253. rseg->reserved = 0;
  1254. }
  1255. static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg, struct ib_send_wr *wr)
  1256. {
  1257. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  1258. aseg->swap_add = cpu_to_be64(wr->wr.atomic.swap);
  1259. aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add);
  1260. } else if (wr->opcode == IB_WR_MASKED_ATOMIC_FETCH_AND_ADD) {
  1261. aseg->swap_add = cpu_to_be64(wr->wr.atomic.compare_add);
  1262. aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add_mask);
  1263. } else {
  1264. aseg->swap_add = cpu_to_be64(wr->wr.atomic.compare_add);
  1265. aseg->compare = 0;
  1266. }
  1267. }
  1268. static void set_masked_atomic_seg(struct mlx4_wqe_masked_atomic_seg *aseg,
  1269. struct ib_send_wr *wr)
  1270. {
  1271. aseg->swap_add = cpu_to_be64(wr->wr.atomic.swap);
  1272. aseg->swap_add_mask = cpu_to_be64(wr->wr.atomic.swap_mask);
  1273. aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add);
  1274. aseg->compare_mask = cpu_to_be64(wr->wr.atomic.compare_add_mask);
  1275. }
  1276. static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg,
  1277. struct ib_send_wr *wr)
  1278. {
  1279. memcpy(dseg->av, &to_mah(wr->wr.ud.ah)->av, sizeof (struct mlx4_av));
  1280. dseg->dqpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  1281. dseg->qkey = cpu_to_be32(wr->wr.ud.remote_qkey);
  1282. dseg->vlan = to_mah(wr->wr.ud.ah)->av.eth.vlan;
  1283. memcpy(dseg->mac, to_mah(wr->wr.ud.ah)->av.eth.mac, 6);
  1284. }
  1285. static void set_mlx_icrc_seg(void *dseg)
  1286. {
  1287. u32 *t = dseg;
  1288. struct mlx4_wqe_inline_seg *iseg = dseg;
  1289. t[1] = 0;
  1290. /*
  1291. * Need a barrier here before writing the byte_count field to
  1292. * make sure that all the data is visible before the
  1293. * byte_count field is set. Otherwise, if the segment begins
  1294. * a new cacheline, the HCA prefetcher could grab the 64-byte
  1295. * chunk and get a valid (!= * 0xffffffff) byte count but
  1296. * stale data, and end up sending the wrong data.
  1297. */
  1298. wmb();
  1299. iseg->byte_count = cpu_to_be32((1 << 31) | 4);
  1300. }
  1301. static void set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  1302. {
  1303. dseg->lkey = cpu_to_be32(sg->lkey);
  1304. dseg->addr = cpu_to_be64(sg->addr);
  1305. /*
  1306. * Need a barrier here before writing the byte_count field to
  1307. * make sure that all the data is visible before the
  1308. * byte_count field is set. Otherwise, if the segment begins
  1309. * a new cacheline, the HCA prefetcher could grab the 64-byte
  1310. * chunk and get a valid (!= * 0xffffffff) byte count but
  1311. * stale data, and end up sending the wrong data.
  1312. */
  1313. wmb();
  1314. dseg->byte_count = cpu_to_be32(sg->length);
  1315. }
  1316. static void __set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  1317. {
  1318. dseg->byte_count = cpu_to_be32(sg->length);
  1319. dseg->lkey = cpu_to_be32(sg->lkey);
  1320. dseg->addr = cpu_to_be64(sg->addr);
  1321. }
  1322. static int build_lso_seg(struct mlx4_wqe_lso_seg *wqe, struct ib_send_wr *wr,
  1323. struct mlx4_ib_qp *qp, unsigned *lso_seg_len,
  1324. __be32 *lso_hdr_sz, __be32 *blh)
  1325. {
  1326. unsigned halign = ALIGN(sizeof *wqe + wr->wr.ud.hlen, 16);
  1327. if (unlikely(halign > MLX4_IB_CACHE_LINE_SIZE))
  1328. *blh = cpu_to_be32(1 << 6);
  1329. if (unlikely(!(qp->flags & MLX4_IB_QP_LSO) &&
  1330. wr->num_sge > qp->sq.max_gs - (halign >> 4)))
  1331. return -EINVAL;
  1332. memcpy(wqe->header, wr->wr.ud.header, wr->wr.ud.hlen);
  1333. *lso_hdr_sz = cpu_to_be32((wr->wr.ud.mss - wr->wr.ud.hlen) << 16 |
  1334. wr->wr.ud.hlen);
  1335. *lso_seg_len = halign;
  1336. return 0;
  1337. }
  1338. static __be32 send_ieth(struct ib_send_wr *wr)
  1339. {
  1340. switch (wr->opcode) {
  1341. case IB_WR_SEND_WITH_IMM:
  1342. case IB_WR_RDMA_WRITE_WITH_IMM:
  1343. return wr->ex.imm_data;
  1344. case IB_WR_SEND_WITH_INV:
  1345. return cpu_to_be32(wr->ex.invalidate_rkey);
  1346. default:
  1347. return 0;
  1348. }
  1349. }
  1350. int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1351. struct ib_send_wr **bad_wr)
  1352. {
  1353. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1354. void *wqe;
  1355. struct mlx4_wqe_ctrl_seg *ctrl;
  1356. struct mlx4_wqe_data_seg *dseg;
  1357. unsigned long flags;
  1358. int nreq;
  1359. int err = 0;
  1360. unsigned ind;
  1361. int uninitialized_var(stamp);
  1362. int uninitialized_var(size);
  1363. unsigned uninitialized_var(seglen);
  1364. __be32 dummy;
  1365. __be32 *lso_wqe;
  1366. __be32 uninitialized_var(lso_hdr_sz);
  1367. __be32 blh;
  1368. int i;
  1369. spin_lock_irqsave(&qp->sq.lock, flags);
  1370. ind = qp->sq_next_wqe;
  1371. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1372. lso_wqe = &dummy;
  1373. blh = 0;
  1374. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  1375. err = -ENOMEM;
  1376. *bad_wr = wr;
  1377. goto out;
  1378. }
  1379. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  1380. err = -EINVAL;
  1381. *bad_wr = wr;
  1382. goto out;
  1383. }
  1384. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  1385. qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  1386. ctrl->srcrb_flags =
  1387. (wr->send_flags & IB_SEND_SIGNALED ?
  1388. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  1389. (wr->send_flags & IB_SEND_SOLICITED ?
  1390. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  1391. ((wr->send_flags & IB_SEND_IP_CSUM) ?
  1392. cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  1393. MLX4_WQE_CTRL_TCP_UDP_CSUM) : 0) |
  1394. qp->sq_signal_bits;
  1395. ctrl->imm = send_ieth(wr);
  1396. wqe += sizeof *ctrl;
  1397. size = sizeof *ctrl / 16;
  1398. switch (ibqp->qp_type) {
  1399. case IB_QPT_RC:
  1400. case IB_QPT_UC:
  1401. switch (wr->opcode) {
  1402. case IB_WR_ATOMIC_CMP_AND_SWP:
  1403. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1404. case IB_WR_MASKED_ATOMIC_FETCH_AND_ADD:
  1405. set_raddr_seg(wqe, wr->wr.atomic.remote_addr,
  1406. wr->wr.atomic.rkey);
  1407. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1408. set_atomic_seg(wqe, wr);
  1409. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  1410. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  1411. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  1412. break;
  1413. case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
  1414. set_raddr_seg(wqe, wr->wr.atomic.remote_addr,
  1415. wr->wr.atomic.rkey);
  1416. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1417. set_masked_atomic_seg(wqe, wr);
  1418. wqe += sizeof (struct mlx4_wqe_masked_atomic_seg);
  1419. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  1420. sizeof (struct mlx4_wqe_masked_atomic_seg)) / 16;
  1421. break;
  1422. case IB_WR_RDMA_READ:
  1423. case IB_WR_RDMA_WRITE:
  1424. case IB_WR_RDMA_WRITE_WITH_IMM:
  1425. set_raddr_seg(wqe, wr->wr.rdma.remote_addr,
  1426. wr->wr.rdma.rkey);
  1427. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1428. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  1429. break;
  1430. case IB_WR_LOCAL_INV:
  1431. ctrl->srcrb_flags |=
  1432. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  1433. set_local_inv_seg(wqe, wr->ex.invalidate_rkey);
  1434. wqe += sizeof (struct mlx4_wqe_local_inval_seg);
  1435. size += sizeof (struct mlx4_wqe_local_inval_seg) / 16;
  1436. break;
  1437. case IB_WR_FAST_REG_MR:
  1438. ctrl->srcrb_flags |=
  1439. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  1440. set_fmr_seg(wqe, wr);
  1441. wqe += sizeof (struct mlx4_wqe_fmr_seg);
  1442. size += sizeof (struct mlx4_wqe_fmr_seg) / 16;
  1443. break;
  1444. default:
  1445. /* No extra segments required for sends */
  1446. break;
  1447. }
  1448. break;
  1449. case IB_QPT_UD:
  1450. set_datagram_seg(wqe, wr);
  1451. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  1452. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  1453. if (wr->opcode == IB_WR_LSO) {
  1454. err = build_lso_seg(wqe, wr, qp, &seglen, &lso_hdr_sz, &blh);
  1455. if (unlikely(err)) {
  1456. *bad_wr = wr;
  1457. goto out;
  1458. }
  1459. lso_wqe = (__be32 *) wqe;
  1460. wqe += seglen;
  1461. size += seglen / 16;
  1462. }
  1463. break;
  1464. case IB_QPT_SMI:
  1465. case IB_QPT_GSI:
  1466. err = build_mlx_header(to_msqp(qp), wr, ctrl, &seglen);
  1467. if (unlikely(err)) {
  1468. *bad_wr = wr;
  1469. goto out;
  1470. }
  1471. wqe += seglen;
  1472. size += seglen / 16;
  1473. break;
  1474. default:
  1475. break;
  1476. }
  1477. /*
  1478. * Write data segments in reverse order, so as to
  1479. * overwrite cacheline stamp last within each
  1480. * cacheline. This avoids issues with WQE
  1481. * prefetching.
  1482. */
  1483. dseg = wqe;
  1484. dseg += wr->num_sge - 1;
  1485. size += wr->num_sge * (sizeof (struct mlx4_wqe_data_seg) / 16);
  1486. /* Add one more inline data segment for ICRC for MLX sends */
  1487. if (unlikely(qp->ibqp.qp_type == IB_QPT_SMI ||
  1488. qp->ibqp.qp_type == IB_QPT_GSI)) {
  1489. set_mlx_icrc_seg(dseg + 1);
  1490. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1491. }
  1492. for (i = wr->num_sge - 1; i >= 0; --i, --dseg)
  1493. set_data_seg(dseg, wr->sg_list + i);
  1494. /*
  1495. * Possibly overwrite stamping in cacheline with LSO
  1496. * segment only after making sure all data segments
  1497. * are written.
  1498. */
  1499. wmb();
  1500. *lso_wqe = lso_hdr_sz;
  1501. ctrl->fence_size = (wr->send_flags & IB_SEND_FENCE ?
  1502. MLX4_WQE_CTRL_FENCE : 0) | size;
  1503. /*
  1504. * Make sure descriptor is fully written before
  1505. * setting ownership bit (because HW can start
  1506. * executing as soon as we do).
  1507. */
  1508. wmb();
  1509. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  1510. err = -EINVAL;
  1511. goto out;
  1512. }
  1513. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  1514. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0) | blh;
  1515. stamp = ind + qp->sq_spare_wqes;
  1516. ind += DIV_ROUND_UP(size * 16, 1U << qp->sq.wqe_shift);
  1517. /*
  1518. * We can improve latency by not stamping the last
  1519. * send queue WQE until after ringing the doorbell, so
  1520. * only stamp here if there are still more WQEs to post.
  1521. *
  1522. * Same optimization applies to padding with NOP wqe
  1523. * in case of WQE shrinking (used to prevent wrap-around
  1524. * in the middle of WR).
  1525. */
  1526. if (wr->next) {
  1527. stamp_send_wqe(qp, stamp, size * 16);
  1528. ind = pad_wraparound(qp, ind);
  1529. }
  1530. }
  1531. out:
  1532. if (likely(nreq)) {
  1533. qp->sq.head += nreq;
  1534. /*
  1535. * Make sure that descriptors are written before
  1536. * doorbell record.
  1537. */
  1538. wmb();
  1539. writel(qp->doorbell_qpn,
  1540. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  1541. /*
  1542. * Make sure doorbells don't leak out of SQ spinlock
  1543. * and reach the HCA out of order.
  1544. */
  1545. mmiowb();
  1546. stamp_send_wqe(qp, stamp, size * 16);
  1547. ind = pad_wraparound(qp, ind);
  1548. qp->sq_next_wqe = ind;
  1549. }
  1550. spin_unlock_irqrestore(&qp->sq.lock, flags);
  1551. return err;
  1552. }
  1553. int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1554. struct ib_recv_wr **bad_wr)
  1555. {
  1556. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1557. struct mlx4_wqe_data_seg *scat;
  1558. unsigned long flags;
  1559. int err = 0;
  1560. int nreq;
  1561. int ind;
  1562. int i;
  1563. spin_lock_irqsave(&qp->rq.lock, flags);
  1564. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  1565. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1566. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  1567. err = -ENOMEM;
  1568. *bad_wr = wr;
  1569. goto out;
  1570. }
  1571. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  1572. err = -EINVAL;
  1573. *bad_wr = wr;
  1574. goto out;
  1575. }
  1576. scat = get_recv_wqe(qp, ind);
  1577. for (i = 0; i < wr->num_sge; ++i)
  1578. __set_data_seg(scat + i, wr->sg_list + i);
  1579. if (i < qp->rq.max_gs) {
  1580. scat[i].byte_count = 0;
  1581. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  1582. scat[i].addr = 0;
  1583. }
  1584. qp->rq.wrid[ind] = wr->wr_id;
  1585. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  1586. }
  1587. out:
  1588. if (likely(nreq)) {
  1589. qp->rq.head += nreq;
  1590. /*
  1591. * Make sure that descriptors are written before
  1592. * doorbell record.
  1593. */
  1594. wmb();
  1595. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  1596. }
  1597. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1598. return err;
  1599. }
  1600. static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
  1601. {
  1602. switch (mlx4_state) {
  1603. case MLX4_QP_STATE_RST: return IB_QPS_RESET;
  1604. case MLX4_QP_STATE_INIT: return IB_QPS_INIT;
  1605. case MLX4_QP_STATE_RTR: return IB_QPS_RTR;
  1606. case MLX4_QP_STATE_RTS: return IB_QPS_RTS;
  1607. case MLX4_QP_STATE_SQ_DRAINING:
  1608. case MLX4_QP_STATE_SQD: return IB_QPS_SQD;
  1609. case MLX4_QP_STATE_SQER: return IB_QPS_SQE;
  1610. case MLX4_QP_STATE_ERR: return IB_QPS_ERR;
  1611. default: return -1;
  1612. }
  1613. }
  1614. static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
  1615. {
  1616. switch (mlx4_mig_state) {
  1617. case MLX4_QP_PM_ARMED: return IB_MIG_ARMED;
  1618. case MLX4_QP_PM_REARM: return IB_MIG_REARM;
  1619. case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
  1620. default: return -1;
  1621. }
  1622. }
  1623. static int to_ib_qp_access_flags(int mlx4_flags)
  1624. {
  1625. int ib_flags = 0;
  1626. if (mlx4_flags & MLX4_QP_BIT_RRE)
  1627. ib_flags |= IB_ACCESS_REMOTE_READ;
  1628. if (mlx4_flags & MLX4_QP_BIT_RWE)
  1629. ib_flags |= IB_ACCESS_REMOTE_WRITE;
  1630. if (mlx4_flags & MLX4_QP_BIT_RAE)
  1631. ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
  1632. return ib_flags;
  1633. }
  1634. static void to_ib_ah_attr(struct mlx4_dev *dev, struct ib_ah_attr *ib_ah_attr,
  1635. struct mlx4_qp_path *path)
  1636. {
  1637. memset(ib_ah_attr, 0, sizeof *ib_ah_attr);
  1638. ib_ah_attr->port_num = path->sched_queue & 0x40 ? 2 : 1;
  1639. if (ib_ah_attr->port_num == 0 || ib_ah_attr->port_num > dev->caps.num_ports)
  1640. return;
  1641. ib_ah_attr->dlid = be16_to_cpu(path->rlid);
  1642. ib_ah_attr->sl = (path->sched_queue >> 2) & 0xf;
  1643. ib_ah_attr->src_path_bits = path->grh_mylmc & 0x7f;
  1644. ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
  1645. ib_ah_attr->ah_flags = (path->grh_mylmc & (1 << 7)) ? IB_AH_GRH : 0;
  1646. if (ib_ah_attr->ah_flags) {
  1647. ib_ah_attr->grh.sgid_index = path->mgid_index;
  1648. ib_ah_attr->grh.hop_limit = path->hop_limit;
  1649. ib_ah_attr->grh.traffic_class =
  1650. (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
  1651. ib_ah_attr->grh.flow_label =
  1652. be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
  1653. memcpy(ib_ah_attr->grh.dgid.raw,
  1654. path->rgid, sizeof ib_ah_attr->grh.dgid.raw);
  1655. }
  1656. }
  1657. int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  1658. struct ib_qp_init_attr *qp_init_attr)
  1659. {
  1660. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1661. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1662. struct mlx4_qp_context context;
  1663. int mlx4_state;
  1664. int err = 0;
  1665. mutex_lock(&qp->mutex);
  1666. if (qp->state == IB_QPS_RESET) {
  1667. qp_attr->qp_state = IB_QPS_RESET;
  1668. goto done;
  1669. }
  1670. err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
  1671. if (err) {
  1672. err = -EINVAL;
  1673. goto out;
  1674. }
  1675. mlx4_state = be32_to_cpu(context.flags) >> 28;
  1676. qp->state = to_ib_qp_state(mlx4_state);
  1677. qp_attr->qp_state = qp->state;
  1678. qp_attr->path_mtu = context.mtu_msgmax >> 5;
  1679. qp_attr->path_mig_state =
  1680. to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
  1681. qp_attr->qkey = be32_to_cpu(context.qkey);
  1682. qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
  1683. qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff;
  1684. qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff;
  1685. qp_attr->qp_access_flags =
  1686. to_ib_qp_access_flags(be32_to_cpu(context.params2));
  1687. if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
  1688. to_ib_ah_attr(dev->dev, &qp_attr->ah_attr, &context.pri_path);
  1689. to_ib_ah_attr(dev->dev, &qp_attr->alt_ah_attr, &context.alt_path);
  1690. qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
  1691. qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
  1692. }
  1693. qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
  1694. if (qp_attr->qp_state == IB_QPS_INIT)
  1695. qp_attr->port_num = qp->port;
  1696. else
  1697. qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
  1698. /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
  1699. qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
  1700. qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
  1701. qp_attr->max_dest_rd_atomic =
  1702. 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
  1703. qp_attr->min_rnr_timer =
  1704. (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
  1705. qp_attr->timeout = context.pri_path.ackto >> 3;
  1706. qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7;
  1707. qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7;
  1708. qp_attr->alt_timeout = context.alt_path.ackto >> 3;
  1709. done:
  1710. qp_attr->cur_qp_state = qp_attr->qp_state;
  1711. qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
  1712. qp_attr->cap.max_recv_sge = qp->rq.max_gs;
  1713. if (!ibqp->uobject) {
  1714. qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
  1715. qp_attr->cap.max_send_sge = qp->sq.max_gs;
  1716. } else {
  1717. qp_attr->cap.max_send_wr = 0;
  1718. qp_attr->cap.max_send_sge = 0;
  1719. }
  1720. /*
  1721. * We don't support inline sends for kernel QPs (yet), and we
  1722. * don't know what userspace's value should be.
  1723. */
  1724. qp_attr->cap.max_inline_data = 0;
  1725. qp_init_attr->cap = qp_attr->cap;
  1726. qp_init_attr->create_flags = 0;
  1727. if (qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK)
  1728. qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
  1729. if (qp->flags & MLX4_IB_QP_LSO)
  1730. qp_init_attr->create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
  1731. out:
  1732. mutex_unlock(&qp->mutex);
  1733. return err;
  1734. }