qp.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <rdma/ib_cache.h>
  33. #include <rdma/ib_pack.h>
  34. #include <linux/mlx4/qp.h>
  35. #include "mlx4_ib.h"
  36. #include "user.h"
  37. enum {
  38. MLX4_IB_ACK_REQ_FREQ = 8,
  39. };
  40. enum {
  41. MLX4_IB_DEFAULT_SCHED_QUEUE = 0x83,
  42. MLX4_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f
  43. };
  44. enum {
  45. /*
  46. * Largest possible UD header: send with GRH and immediate data.
  47. */
  48. MLX4_IB_UD_HEADER_SIZE = 72
  49. };
  50. struct mlx4_ib_sqp {
  51. struct mlx4_ib_qp qp;
  52. int pkey_index;
  53. u32 qkey;
  54. u32 send_psn;
  55. struct ib_ud_header ud_header;
  56. u8 header_buf[MLX4_IB_UD_HEADER_SIZE];
  57. };
  58. static const __be32 mlx4_ib_opcode[] = {
  59. [IB_WR_SEND] = __constant_cpu_to_be32(MLX4_OPCODE_SEND),
  60. [IB_WR_SEND_WITH_IMM] = __constant_cpu_to_be32(MLX4_OPCODE_SEND_IMM),
  61. [IB_WR_RDMA_WRITE] = __constant_cpu_to_be32(MLX4_OPCODE_RDMA_WRITE),
  62. [IB_WR_RDMA_WRITE_WITH_IMM] = __constant_cpu_to_be32(MLX4_OPCODE_RDMA_WRITE_IMM),
  63. [IB_WR_RDMA_READ] = __constant_cpu_to_be32(MLX4_OPCODE_RDMA_READ),
  64. [IB_WR_ATOMIC_CMP_AND_SWP] = __constant_cpu_to_be32(MLX4_OPCODE_ATOMIC_CS),
  65. [IB_WR_ATOMIC_FETCH_AND_ADD] = __constant_cpu_to_be32(MLX4_OPCODE_ATOMIC_FA),
  66. };
  67. static struct mlx4_ib_sqp *to_msqp(struct mlx4_ib_qp *mqp)
  68. {
  69. return container_of(mqp, struct mlx4_ib_sqp, qp);
  70. }
  71. static int is_sqp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  72. {
  73. return qp->mqp.qpn >= dev->dev->caps.sqp_start &&
  74. qp->mqp.qpn <= dev->dev->caps.sqp_start + 3;
  75. }
  76. static int is_qp0(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  77. {
  78. return qp->mqp.qpn >= dev->dev->caps.sqp_start &&
  79. qp->mqp.qpn <= dev->dev->caps.sqp_start + 1;
  80. }
  81. static void *get_wqe(struct mlx4_ib_qp *qp, int offset)
  82. {
  83. if (qp->buf.nbufs == 1)
  84. return qp->buf.u.direct.buf + offset;
  85. else
  86. return qp->buf.u.page_list[offset >> PAGE_SHIFT].buf +
  87. (offset & (PAGE_SIZE - 1));
  88. }
  89. static void *get_recv_wqe(struct mlx4_ib_qp *qp, int n)
  90. {
  91. return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
  92. }
  93. static void *get_send_wqe(struct mlx4_ib_qp *qp, int n)
  94. {
  95. return get_wqe(qp, qp->sq.offset + (n << qp->sq.wqe_shift));
  96. }
  97. /*
  98. * Stamp a SQ WQE so that it is invalid if prefetched by marking the
  99. * first four bytes of every 64 byte chunk with 0xffffffff, except for
  100. * the very first chunk of the WQE.
  101. */
  102. static void stamp_send_wqe(struct mlx4_ib_qp *qp, int n)
  103. {
  104. u32 *wqe = get_send_wqe(qp, n);
  105. int i;
  106. for (i = 16; i < 1 << (qp->sq.wqe_shift - 2); i += 16)
  107. wqe[i] = 0xffffffff;
  108. }
  109. static void mlx4_ib_qp_event(struct mlx4_qp *qp, enum mlx4_event type)
  110. {
  111. struct ib_event event;
  112. struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
  113. if (type == MLX4_EVENT_TYPE_PATH_MIG)
  114. to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
  115. if (ibqp->event_handler) {
  116. event.device = ibqp->device;
  117. event.element.qp = ibqp;
  118. switch (type) {
  119. case MLX4_EVENT_TYPE_PATH_MIG:
  120. event.event = IB_EVENT_PATH_MIG;
  121. break;
  122. case MLX4_EVENT_TYPE_COMM_EST:
  123. event.event = IB_EVENT_COMM_EST;
  124. break;
  125. case MLX4_EVENT_TYPE_SQ_DRAINED:
  126. event.event = IB_EVENT_SQ_DRAINED;
  127. break;
  128. case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
  129. event.event = IB_EVENT_QP_LAST_WQE_REACHED;
  130. break;
  131. case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
  132. event.event = IB_EVENT_QP_FATAL;
  133. break;
  134. case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
  135. event.event = IB_EVENT_PATH_MIG_ERR;
  136. break;
  137. case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  138. event.event = IB_EVENT_QP_REQ_ERR;
  139. break;
  140. case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
  141. event.event = IB_EVENT_QP_ACCESS_ERR;
  142. break;
  143. default:
  144. printk(KERN_WARNING "mlx4_ib: Unexpected event type %d "
  145. "on QP %06x\n", type, qp->qpn);
  146. return;
  147. }
  148. ibqp->event_handler(&event, ibqp->qp_context);
  149. }
  150. }
  151. static int send_wqe_overhead(enum ib_qp_type type)
  152. {
  153. /*
  154. * UD WQEs must have a datagram segment.
  155. * RC and UC WQEs might have a remote address segment.
  156. * MLX WQEs need two extra inline data segments (for the UD
  157. * header and space for the ICRC).
  158. */
  159. switch (type) {
  160. case IB_QPT_UD:
  161. return sizeof (struct mlx4_wqe_ctrl_seg) +
  162. sizeof (struct mlx4_wqe_datagram_seg);
  163. case IB_QPT_UC:
  164. return sizeof (struct mlx4_wqe_ctrl_seg) +
  165. sizeof (struct mlx4_wqe_raddr_seg);
  166. case IB_QPT_RC:
  167. return sizeof (struct mlx4_wqe_ctrl_seg) +
  168. sizeof (struct mlx4_wqe_atomic_seg) +
  169. sizeof (struct mlx4_wqe_raddr_seg);
  170. case IB_QPT_SMI:
  171. case IB_QPT_GSI:
  172. return sizeof (struct mlx4_wqe_ctrl_seg) +
  173. ALIGN(MLX4_IB_UD_HEADER_SIZE +
  174. DIV_ROUND_UP(MLX4_IB_UD_HEADER_SIZE,
  175. MLX4_INLINE_ALIGN) *
  176. sizeof (struct mlx4_wqe_inline_seg),
  177. sizeof (struct mlx4_wqe_data_seg)) +
  178. ALIGN(4 +
  179. sizeof (struct mlx4_wqe_inline_seg),
  180. sizeof (struct mlx4_wqe_data_seg));
  181. default:
  182. return sizeof (struct mlx4_wqe_ctrl_seg);
  183. }
  184. }
  185. static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  186. int is_user, int has_srq, struct mlx4_ib_qp *qp)
  187. {
  188. /* Sanity check RQ size before proceeding */
  189. if (cap->max_recv_wr > dev->dev->caps.max_wqes ||
  190. cap->max_recv_sge > dev->dev->caps.max_rq_sg)
  191. return -EINVAL;
  192. if (has_srq) {
  193. /* QPs attached to an SRQ should have no RQ */
  194. if (cap->max_recv_wr)
  195. return -EINVAL;
  196. qp->rq.wqe_cnt = qp->rq.max_gs = 0;
  197. } else {
  198. /* HW requires >= 1 RQ entry with >= 1 gather entry */
  199. if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge))
  200. return -EINVAL;
  201. qp->rq.wqe_cnt = roundup_pow_of_two(max(1U, cap->max_recv_wr));
  202. qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge));
  203. qp->rq.wqe_shift = ilog2(qp->rq.max_gs * sizeof (struct mlx4_wqe_data_seg));
  204. }
  205. cap->max_recv_wr = qp->rq.max_post = qp->rq.wqe_cnt;
  206. cap->max_recv_sge = qp->rq.max_gs;
  207. return 0;
  208. }
  209. static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  210. enum ib_qp_type type, struct mlx4_ib_qp *qp)
  211. {
  212. /* Sanity check SQ size before proceeding */
  213. if (cap->max_send_wr > dev->dev->caps.max_wqes ||
  214. cap->max_send_sge > dev->dev->caps.max_sq_sg ||
  215. cap->max_inline_data + send_wqe_overhead(type) +
  216. sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
  217. return -EINVAL;
  218. /*
  219. * For MLX transport we need 2 extra S/G entries:
  220. * one for the header and one for the checksum at the end
  221. */
  222. if ((type == IB_QPT_SMI || type == IB_QPT_GSI) &&
  223. cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
  224. return -EINVAL;
  225. qp->sq.wqe_shift = ilog2(roundup_pow_of_two(max(cap->max_send_sge *
  226. sizeof (struct mlx4_wqe_data_seg),
  227. cap->max_inline_data +
  228. sizeof (struct mlx4_wqe_inline_seg)) +
  229. send_wqe_overhead(type)));
  230. qp->sq.max_gs = ((1 << qp->sq.wqe_shift) - send_wqe_overhead(type)) /
  231. sizeof (struct mlx4_wqe_data_seg);
  232. /*
  233. * We need to leave 2 KB + 1 WQE of headroom in the SQ to
  234. * allow HW to prefetch.
  235. */
  236. qp->sq_spare_wqes = (2048 >> qp->sq.wqe_shift) + 1;
  237. qp->sq.wqe_cnt = roundup_pow_of_two(cap->max_send_wr + qp->sq_spare_wqes);
  238. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  239. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  240. if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
  241. qp->rq.offset = 0;
  242. qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
  243. } else {
  244. qp->rq.offset = qp->sq.wqe_cnt << qp->sq.wqe_shift;
  245. qp->sq.offset = 0;
  246. }
  247. cap->max_send_wr = qp->sq.max_post = qp->sq.wqe_cnt - qp->sq_spare_wqes;
  248. cap->max_send_sge = qp->sq.max_gs;
  249. /* We don't support inline sends for kernel QPs (yet) */
  250. cap->max_inline_data = 0;
  251. return 0;
  252. }
  253. static int set_user_sq_size(struct mlx4_ib_qp *qp,
  254. struct mlx4_ib_create_qp *ucmd)
  255. {
  256. qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count;
  257. qp->sq.wqe_shift = ucmd->log_sq_stride;
  258. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  259. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  260. return 0;
  261. }
  262. static int create_qp_common(struct mlx4_ib_dev *dev, struct ib_pd *pd,
  263. struct ib_qp_init_attr *init_attr,
  264. struct ib_udata *udata, int sqpn, struct mlx4_ib_qp *qp)
  265. {
  266. int err;
  267. mutex_init(&qp->mutex);
  268. spin_lock_init(&qp->sq.lock);
  269. spin_lock_init(&qp->rq.lock);
  270. qp->state = IB_QPS_RESET;
  271. qp->atomic_rd_en = 0;
  272. qp->resp_depth = 0;
  273. qp->rq.head = 0;
  274. qp->rq.tail = 0;
  275. qp->sq.head = 0;
  276. qp->sq.tail = 0;
  277. err = set_rq_size(dev, &init_attr->cap, !!pd->uobject, !!init_attr->srq, qp);
  278. if (err)
  279. goto err;
  280. if (pd->uobject) {
  281. struct mlx4_ib_create_qp ucmd;
  282. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  283. err = -EFAULT;
  284. goto err;
  285. }
  286. qp->sq_no_prefetch = ucmd.sq_no_prefetch;
  287. err = set_user_sq_size(qp, &ucmd);
  288. if (err)
  289. goto err;
  290. qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
  291. qp->buf_size, 0);
  292. if (IS_ERR(qp->umem)) {
  293. err = PTR_ERR(qp->umem);
  294. goto err;
  295. }
  296. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(qp->umem),
  297. ilog2(qp->umem->page_size), &qp->mtt);
  298. if (err)
  299. goto err_buf;
  300. err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
  301. if (err)
  302. goto err_mtt;
  303. if (!init_attr->srq) {
  304. err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
  305. ucmd.db_addr, &qp->db);
  306. if (err)
  307. goto err_mtt;
  308. }
  309. } else {
  310. qp->sq_no_prefetch = 0;
  311. err = set_kernel_sq_size(dev, &init_attr->cap, init_attr->qp_type, qp);
  312. if (err)
  313. goto err;
  314. if (!init_attr->srq) {
  315. err = mlx4_ib_db_alloc(dev, &qp->db, 0);
  316. if (err)
  317. goto err;
  318. *qp->db.db = 0;
  319. }
  320. if (mlx4_buf_alloc(dev->dev, qp->buf_size, PAGE_SIZE * 2, &qp->buf)) {
  321. err = -ENOMEM;
  322. goto err_db;
  323. }
  324. err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
  325. &qp->mtt);
  326. if (err)
  327. goto err_buf;
  328. err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf);
  329. if (err)
  330. goto err_mtt;
  331. qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof (u64), GFP_KERNEL);
  332. qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof (u64), GFP_KERNEL);
  333. if (!qp->sq.wrid || !qp->rq.wrid) {
  334. err = -ENOMEM;
  335. goto err_wrid;
  336. }
  337. }
  338. err = mlx4_qp_alloc(dev->dev, sqpn, &qp->mqp);
  339. if (err)
  340. goto err_wrid;
  341. /*
  342. * Hardware wants QPN written in big-endian order (after
  343. * shifting) for send doorbell. Precompute this value to save
  344. * a little bit when posting sends.
  345. */
  346. qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
  347. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  348. qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  349. else
  350. qp->sq_signal_bits = 0;
  351. qp->mqp.event = mlx4_ib_qp_event;
  352. return 0;
  353. err_wrid:
  354. if (pd->uobject && !init_attr->srq)
  355. mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &qp->db);
  356. else {
  357. kfree(qp->sq.wrid);
  358. kfree(qp->rq.wrid);
  359. }
  360. err_mtt:
  361. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  362. err_buf:
  363. if (pd->uobject)
  364. ib_umem_release(qp->umem);
  365. else
  366. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  367. err_db:
  368. if (!pd->uobject && !init_attr->srq)
  369. mlx4_ib_db_free(dev, &qp->db);
  370. err:
  371. return err;
  372. }
  373. static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
  374. {
  375. switch (state) {
  376. case IB_QPS_RESET: return MLX4_QP_STATE_RST;
  377. case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
  378. case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
  379. case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
  380. case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
  381. case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
  382. case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
  383. default: return -1;
  384. }
  385. }
  386. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  387. {
  388. if (send_cq == recv_cq)
  389. spin_lock_irq(&send_cq->lock);
  390. else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  391. spin_lock_irq(&send_cq->lock);
  392. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  393. } else {
  394. spin_lock_irq(&recv_cq->lock);
  395. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  396. }
  397. }
  398. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  399. {
  400. if (send_cq == recv_cq)
  401. spin_unlock_irq(&send_cq->lock);
  402. else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  403. spin_unlock(&recv_cq->lock);
  404. spin_unlock_irq(&send_cq->lock);
  405. } else {
  406. spin_unlock(&send_cq->lock);
  407. spin_unlock_irq(&recv_cq->lock);
  408. }
  409. }
  410. static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
  411. int is_user)
  412. {
  413. struct mlx4_ib_cq *send_cq, *recv_cq;
  414. if (qp->state != IB_QPS_RESET)
  415. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  416. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  417. printk(KERN_WARNING "mlx4_ib: modify QP %06x to RESET failed.\n",
  418. qp->mqp.qpn);
  419. send_cq = to_mcq(qp->ibqp.send_cq);
  420. recv_cq = to_mcq(qp->ibqp.recv_cq);
  421. mlx4_ib_lock_cqs(send_cq, recv_cq);
  422. if (!is_user) {
  423. __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  424. qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
  425. if (send_cq != recv_cq)
  426. __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  427. }
  428. mlx4_qp_remove(dev->dev, &qp->mqp);
  429. mlx4_ib_unlock_cqs(send_cq, recv_cq);
  430. mlx4_qp_free(dev->dev, &qp->mqp);
  431. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  432. if (is_user) {
  433. if (!qp->ibqp.srq)
  434. mlx4_ib_db_unmap_user(to_mucontext(qp->ibqp.uobject->context),
  435. &qp->db);
  436. ib_umem_release(qp->umem);
  437. } else {
  438. kfree(qp->sq.wrid);
  439. kfree(qp->rq.wrid);
  440. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  441. if (!qp->ibqp.srq)
  442. mlx4_ib_db_free(dev, &qp->db);
  443. }
  444. }
  445. struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
  446. struct ib_qp_init_attr *init_attr,
  447. struct ib_udata *udata)
  448. {
  449. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  450. struct mlx4_ib_sqp *sqp;
  451. struct mlx4_ib_qp *qp;
  452. int err;
  453. switch (init_attr->qp_type) {
  454. case IB_QPT_RC:
  455. case IB_QPT_UC:
  456. case IB_QPT_UD:
  457. {
  458. qp = kmalloc(sizeof *qp, GFP_KERNEL);
  459. if (!qp)
  460. return ERR_PTR(-ENOMEM);
  461. err = create_qp_common(dev, pd, init_attr, udata, 0, qp);
  462. if (err) {
  463. kfree(qp);
  464. return ERR_PTR(err);
  465. }
  466. qp->ibqp.qp_num = qp->mqp.qpn;
  467. break;
  468. }
  469. case IB_QPT_SMI:
  470. case IB_QPT_GSI:
  471. {
  472. /* Userspace is not allowed to create special QPs: */
  473. if (pd->uobject)
  474. return ERR_PTR(-EINVAL);
  475. sqp = kmalloc(sizeof *sqp, GFP_KERNEL);
  476. if (!sqp)
  477. return ERR_PTR(-ENOMEM);
  478. qp = &sqp->qp;
  479. err = create_qp_common(dev, pd, init_attr, udata,
  480. dev->dev->caps.sqp_start +
  481. (init_attr->qp_type == IB_QPT_SMI ? 0 : 2) +
  482. init_attr->port_num - 1,
  483. qp);
  484. if (err) {
  485. kfree(sqp);
  486. return ERR_PTR(err);
  487. }
  488. qp->port = init_attr->port_num;
  489. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  490. break;
  491. }
  492. default:
  493. /* Don't support raw QPs */
  494. return ERR_PTR(-EINVAL);
  495. }
  496. return &qp->ibqp;
  497. }
  498. int mlx4_ib_destroy_qp(struct ib_qp *qp)
  499. {
  500. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  501. struct mlx4_ib_qp *mqp = to_mqp(qp);
  502. if (is_qp0(dev, mqp))
  503. mlx4_CLOSE_PORT(dev->dev, mqp->port);
  504. destroy_qp_common(dev, mqp, !!qp->pd->uobject);
  505. if (is_sqp(dev, mqp))
  506. kfree(to_msqp(mqp));
  507. else
  508. kfree(mqp);
  509. return 0;
  510. }
  511. static int to_mlx4_st(enum ib_qp_type type)
  512. {
  513. switch (type) {
  514. case IB_QPT_RC: return MLX4_QP_ST_RC;
  515. case IB_QPT_UC: return MLX4_QP_ST_UC;
  516. case IB_QPT_UD: return MLX4_QP_ST_UD;
  517. case IB_QPT_SMI:
  518. case IB_QPT_GSI: return MLX4_QP_ST_MLX;
  519. default: return -1;
  520. }
  521. }
  522. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  523. int attr_mask)
  524. {
  525. u8 dest_rd_atomic;
  526. u32 access_flags;
  527. u32 hw_access_flags = 0;
  528. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  529. dest_rd_atomic = attr->max_dest_rd_atomic;
  530. else
  531. dest_rd_atomic = qp->resp_depth;
  532. if (attr_mask & IB_QP_ACCESS_FLAGS)
  533. access_flags = attr->qp_access_flags;
  534. else
  535. access_flags = qp->atomic_rd_en;
  536. if (!dest_rd_atomic)
  537. access_flags &= IB_ACCESS_REMOTE_WRITE;
  538. if (access_flags & IB_ACCESS_REMOTE_READ)
  539. hw_access_flags |= MLX4_QP_BIT_RRE;
  540. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  541. hw_access_flags |= MLX4_QP_BIT_RAE;
  542. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  543. hw_access_flags |= MLX4_QP_BIT_RWE;
  544. return cpu_to_be32(hw_access_flags);
  545. }
  546. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  547. int attr_mask)
  548. {
  549. if (attr_mask & IB_QP_PKEY_INDEX)
  550. sqp->pkey_index = attr->pkey_index;
  551. if (attr_mask & IB_QP_QKEY)
  552. sqp->qkey = attr->qkey;
  553. if (attr_mask & IB_QP_SQ_PSN)
  554. sqp->send_psn = attr->sq_psn;
  555. }
  556. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  557. {
  558. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  559. }
  560. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_ah_attr *ah,
  561. struct mlx4_qp_path *path, u8 port)
  562. {
  563. path->grh_mylmc = ah->src_path_bits & 0x7f;
  564. path->rlid = cpu_to_be16(ah->dlid);
  565. if (ah->static_rate) {
  566. path->static_rate = ah->static_rate + MLX4_STAT_RATE_OFFSET;
  567. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  568. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  569. --path->static_rate;
  570. } else
  571. path->static_rate = 0;
  572. path->counter_index = 0xff;
  573. if (ah->ah_flags & IB_AH_GRH) {
  574. if (ah->grh.sgid_index >= dev->dev->caps.gid_table_len[port]) {
  575. printk(KERN_ERR "sgid_index (%u) too large. max is %d\n",
  576. ah->grh.sgid_index, dev->dev->caps.gid_table_len[port] - 1);
  577. return -1;
  578. }
  579. path->grh_mylmc |= 1 << 7;
  580. path->mgid_index = ah->grh.sgid_index;
  581. path->hop_limit = ah->grh.hop_limit;
  582. path->tclass_flowlabel =
  583. cpu_to_be32((ah->grh.traffic_class << 20) |
  584. (ah->grh.flow_label));
  585. memcpy(path->rgid, ah->grh.dgid.raw, 16);
  586. }
  587. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  588. ((port - 1) << 6) | ((ah->sl & 0xf) << 2);
  589. return 0;
  590. }
  591. static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
  592. const struct ib_qp_attr *attr, int attr_mask,
  593. enum ib_qp_state cur_state, enum ib_qp_state new_state)
  594. {
  595. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  596. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  597. struct mlx4_qp_context *context;
  598. enum mlx4_qp_optpar optpar = 0;
  599. int sqd_event;
  600. int err = -EINVAL;
  601. context = kzalloc(sizeof *context, GFP_KERNEL);
  602. if (!context)
  603. return -ENOMEM;
  604. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  605. (to_mlx4_st(ibqp->qp_type) << 16));
  606. context->flags |= cpu_to_be32(1 << 8); /* DE? */
  607. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  608. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  609. else {
  610. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  611. switch (attr->path_mig_state) {
  612. case IB_MIG_MIGRATED:
  613. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  614. break;
  615. case IB_MIG_REARM:
  616. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  617. break;
  618. case IB_MIG_ARMED:
  619. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  620. break;
  621. }
  622. }
  623. if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  624. ibqp->qp_type == IB_QPT_UD)
  625. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  626. else if (attr_mask & IB_QP_PATH_MTU) {
  627. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  628. printk(KERN_ERR "path MTU (%u) is invalid\n",
  629. attr->path_mtu);
  630. goto out;
  631. }
  632. context->mtu_msgmax = (attr->path_mtu << 5) | 31;
  633. }
  634. if (qp->rq.wqe_cnt)
  635. context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
  636. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  637. if (qp->sq.wqe_cnt)
  638. context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
  639. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  640. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  641. context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
  642. if (qp->ibqp.uobject)
  643. context->usr_page = cpu_to_be32(to_mucontext(ibqp->uobject->context)->uar.index);
  644. else
  645. context->usr_page = cpu_to_be32(dev->priv_uar.index);
  646. if (attr_mask & IB_QP_DEST_QPN)
  647. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  648. if (attr_mask & IB_QP_PORT) {
  649. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  650. !(attr_mask & IB_QP_AV)) {
  651. mlx4_set_sched(&context->pri_path, attr->port_num);
  652. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  653. }
  654. }
  655. if (attr_mask & IB_QP_PKEY_INDEX) {
  656. context->pri_path.pkey_index = attr->pkey_index;
  657. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  658. }
  659. if (attr_mask & IB_QP_AV) {
  660. if (mlx4_set_path(dev, &attr->ah_attr, &context->pri_path,
  661. attr_mask & IB_QP_PORT ? attr->port_num : qp->port))
  662. goto out;
  663. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  664. MLX4_QP_OPTPAR_SCHED_QUEUE);
  665. }
  666. if (attr_mask & IB_QP_TIMEOUT) {
  667. context->pri_path.ackto = attr->timeout << 3;
  668. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  669. }
  670. if (attr_mask & IB_QP_ALT_PATH) {
  671. if (attr->alt_port_num == 0 ||
  672. attr->alt_port_num > dev->dev->caps.num_ports)
  673. goto out;
  674. if (attr->alt_pkey_index >=
  675. dev->dev->caps.pkey_table_len[attr->alt_port_num])
  676. goto out;
  677. if (mlx4_set_path(dev, &attr->alt_ah_attr, &context->alt_path,
  678. attr->alt_port_num))
  679. goto out;
  680. context->alt_path.pkey_index = attr->alt_pkey_index;
  681. context->alt_path.ackto = attr->alt_timeout << 3;
  682. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  683. }
  684. context->pd = cpu_to_be32(to_mpd(ibqp->pd)->pdn);
  685. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  686. if (attr_mask & IB_QP_RNR_RETRY) {
  687. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  688. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  689. }
  690. if (attr_mask & IB_QP_RETRY_CNT) {
  691. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  692. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  693. }
  694. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  695. if (attr->max_rd_atomic)
  696. context->params1 |=
  697. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  698. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  699. }
  700. if (attr_mask & IB_QP_SQ_PSN)
  701. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  702. context->cqn_send = cpu_to_be32(to_mcq(ibqp->send_cq)->mcq.cqn);
  703. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  704. if (attr->max_dest_rd_atomic)
  705. context->params2 |=
  706. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  707. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  708. }
  709. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  710. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  711. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  712. }
  713. if (ibqp->srq)
  714. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  715. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  716. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  717. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  718. }
  719. if (attr_mask & IB_QP_RQ_PSN)
  720. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  721. context->cqn_recv = cpu_to_be32(to_mcq(ibqp->recv_cq)->mcq.cqn);
  722. if (attr_mask & IB_QP_QKEY) {
  723. context->qkey = cpu_to_be32(attr->qkey);
  724. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  725. }
  726. if (ibqp->srq)
  727. context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
  728. if (!ibqp->srq && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  729. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  730. if (cur_state == IB_QPS_INIT &&
  731. new_state == IB_QPS_RTR &&
  732. (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  733. ibqp->qp_type == IB_QPT_UD)) {
  734. context->pri_path.sched_queue = (qp->port - 1) << 6;
  735. if (is_qp0(dev, qp))
  736. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  737. else
  738. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  739. }
  740. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  741. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  742. sqd_event = 1;
  743. else
  744. sqd_event = 0;
  745. /*
  746. * Before passing a kernel QP to the HW, make sure that the
  747. * ownership bits of the send queue are set and the SQ
  748. * headroom is stamped so that the hardware doesn't start
  749. * processing stale work requests.
  750. */
  751. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  752. struct mlx4_wqe_ctrl_seg *ctrl;
  753. int i;
  754. for (i = 0; i < qp->sq.wqe_cnt; ++i) {
  755. ctrl = get_send_wqe(qp, i);
  756. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  757. stamp_send_wqe(qp, i);
  758. }
  759. }
  760. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  761. to_mlx4_state(new_state), context, optpar,
  762. sqd_event, &qp->mqp);
  763. if (err)
  764. goto out;
  765. qp->state = new_state;
  766. if (attr_mask & IB_QP_ACCESS_FLAGS)
  767. qp->atomic_rd_en = attr->qp_access_flags;
  768. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  769. qp->resp_depth = attr->max_dest_rd_atomic;
  770. if (attr_mask & IB_QP_PORT)
  771. qp->port = attr->port_num;
  772. if (attr_mask & IB_QP_ALT_PATH)
  773. qp->alt_port = attr->alt_port_num;
  774. if (is_sqp(dev, qp))
  775. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  776. /*
  777. * If we moved QP0 to RTR, bring the IB link up; if we moved
  778. * QP0 to RESET or ERROR, bring the link back down.
  779. */
  780. if (is_qp0(dev, qp)) {
  781. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  782. if (mlx4_INIT_PORT(dev->dev, qp->port))
  783. printk(KERN_WARNING "INIT_PORT failed for port %d\n",
  784. qp->port);
  785. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  786. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  787. mlx4_CLOSE_PORT(dev->dev, qp->port);
  788. }
  789. /*
  790. * If we moved a kernel QP to RESET, clean up all old CQ
  791. * entries and reinitialize the QP.
  792. */
  793. if (new_state == IB_QPS_RESET && !ibqp->uobject) {
  794. mlx4_ib_cq_clean(to_mcq(ibqp->recv_cq), qp->mqp.qpn,
  795. ibqp->srq ? to_msrq(ibqp->srq): NULL);
  796. if (ibqp->send_cq != ibqp->recv_cq)
  797. mlx4_ib_cq_clean(to_mcq(ibqp->send_cq), qp->mqp.qpn, NULL);
  798. qp->rq.head = 0;
  799. qp->rq.tail = 0;
  800. qp->sq.head = 0;
  801. qp->sq.tail = 0;
  802. if (!ibqp->srq)
  803. *qp->db.db = 0;
  804. }
  805. out:
  806. kfree(context);
  807. return err;
  808. }
  809. static const struct ib_qp_attr mlx4_ib_qp_attr = { .port_num = 1 };
  810. static const int mlx4_ib_qp_attr_mask_table[IB_QPT_UD + 1] = {
  811. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  812. IB_QP_PORT |
  813. IB_QP_QKEY),
  814. [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
  815. IB_QP_PORT |
  816. IB_QP_ACCESS_FLAGS),
  817. [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
  818. IB_QP_PORT |
  819. IB_QP_ACCESS_FLAGS),
  820. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  821. IB_QP_QKEY),
  822. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  823. IB_QP_QKEY),
  824. };
  825. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  826. int attr_mask, struct ib_udata *udata)
  827. {
  828. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  829. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  830. enum ib_qp_state cur_state, new_state;
  831. int err = -EINVAL;
  832. mutex_lock(&qp->mutex);
  833. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  834. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  835. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask))
  836. goto out;
  837. if ((attr_mask & IB_QP_PORT) &&
  838. (attr->port_num == 0 || attr->port_num > dev->dev->caps.num_ports)) {
  839. goto out;
  840. }
  841. if (attr_mask & IB_QP_PKEY_INDEX) {
  842. int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  843. if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p])
  844. goto out;
  845. }
  846. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  847. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  848. goto out;
  849. }
  850. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  851. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  852. goto out;
  853. }
  854. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  855. err = 0;
  856. goto out;
  857. }
  858. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_ERR) {
  859. err = __mlx4_ib_modify_qp(ibqp, &mlx4_ib_qp_attr,
  860. mlx4_ib_qp_attr_mask_table[ibqp->qp_type],
  861. IB_QPS_RESET, IB_QPS_INIT);
  862. if (err)
  863. goto out;
  864. cur_state = IB_QPS_INIT;
  865. }
  866. err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
  867. out:
  868. mutex_unlock(&qp->mutex);
  869. return err;
  870. }
  871. static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_send_wr *wr,
  872. void *wqe)
  873. {
  874. struct ib_device *ib_dev = &to_mdev(sqp->qp.ibqp.device)->ib_dev;
  875. struct mlx4_wqe_mlx_seg *mlx = wqe;
  876. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  877. struct mlx4_ib_ah *ah = to_mah(wr->wr.ud.ah);
  878. u16 pkey;
  879. int send_size;
  880. int header_size;
  881. int spc;
  882. int i;
  883. send_size = 0;
  884. for (i = 0; i < wr->num_sge; ++i)
  885. send_size += wr->sg_list[i].length;
  886. ib_ud_header_init(send_size, mlx4_ib_ah_grh_present(ah), &sqp->ud_header);
  887. sqp->ud_header.lrh.service_level =
  888. be32_to_cpu(ah->av.sl_tclass_flowlabel) >> 28;
  889. sqp->ud_header.lrh.destination_lid = ah->av.dlid;
  890. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.g_slid & 0x7f);
  891. if (mlx4_ib_ah_grh_present(ah)) {
  892. sqp->ud_header.grh.traffic_class =
  893. (be32_to_cpu(ah->av.sl_tclass_flowlabel) >> 20) & 0xff;
  894. sqp->ud_header.grh.flow_label =
  895. ah->av.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  896. sqp->ud_header.grh.hop_limit = ah->av.hop_limit;
  897. ib_get_cached_gid(ib_dev, be32_to_cpu(ah->av.port_pd) >> 24,
  898. ah->av.gid_index, &sqp->ud_header.grh.source_gid);
  899. memcpy(sqp->ud_header.grh.destination_gid.raw,
  900. ah->av.dgid, 16);
  901. }
  902. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  903. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  904. (sqp->ud_header.lrh.destination_lid ==
  905. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  906. (sqp->ud_header.lrh.service_level << 8));
  907. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  908. switch (wr->opcode) {
  909. case IB_WR_SEND:
  910. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  911. sqp->ud_header.immediate_present = 0;
  912. break;
  913. case IB_WR_SEND_WITH_IMM:
  914. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  915. sqp->ud_header.immediate_present = 1;
  916. sqp->ud_header.immediate_data = wr->imm_data;
  917. break;
  918. default:
  919. return -EINVAL;
  920. }
  921. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
  922. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  923. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  924. sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
  925. if (!sqp->qp.ibqp.qp_num)
  926. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  927. else
  928. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->wr.ud.pkey_index, &pkey);
  929. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  930. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  931. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  932. sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
  933. sqp->qkey : wr->wr.ud.remote_qkey);
  934. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  935. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  936. if (0) {
  937. printk(KERN_ERR "built UD header of size %d:\n", header_size);
  938. for (i = 0; i < header_size / 4; ++i) {
  939. if (i % 8 == 0)
  940. printk(" [%02x] ", i * 4);
  941. printk(" %08x",
  942. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  943. if ((i + 1) % 8 == 0)
  944. printk("\n");
  945. }
  946. printk("\n");
  947. }
  948. /*
  949. * Inline data segments may not cross a 64 byte boundary. If
  950. * our UD header is bigger than the space available up to the
  951. * next 64 byte boundary in the WQE, use two inline data
  952. * segments to hold the UD header.
  953. */
  954. spc = MLX4_INLINE_ALIGN -
  955. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  956. if (header_size <= spc) {
  957. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  958. memcpy(inl + 1, sqp->header_buf, header_size);
  959. i = 1;
  960. } else {
  961. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  962. memcpy(inl + 1, sqp->header_buf, spc);
  963. inl = (void *) (inl + 1) + spc;
  964. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  965. /*
  966. * Need a barrier here to make sure all the data is
  967. * visible before the byte_count field is set.
  968. * Otherwise the HCA prefetcher could grab the 64-byte
  969. * chunk with this inline segment and get a valid (!=
  970. * 0xffffffff) byte count but stale data, and end up
  971. * generating a packet with bad headers.
  972. *
  973. * The first inline segment's byte_count field doesn't
  974. * need a barrier, because it comes after a
  975. * control/MLX segment and therefore is at an offset
  976. * of 16 mod 64.
  977. */
  978. wmb();
  979. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  980. i = 2;
  981. }
  982. return ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  983. }
  984. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  985. {
  986. unsigned cur;
  987. struct mlx4_ib_cq *cq;
  988. cur = wq->head - wq->tail;
  989. if (likely(cur + nreq < wq->max_post))
  990. return 0;
  991. cq = to_mcq(ib_cq);
  992. spin_lock(&cq->lock);
  993. cur = wq->head - wq->tail;
  994. spin_unlock(&cq->lock);
  995. return cur + nreq >= wq->max_post;
  996. }
  997. static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg,
  998. u64 remote_addr, u32 rkey)
  999. {
  1000. rseg->raddr = cpu_to_be64(remote_addr);
  1001. rseg->rkey = cpu_to_be32(rkey);
  1002. rseg->reserved = 0;
  1003. }
  1004. static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg, struct ib_send_wr *wr)
  1005. {
  1006. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  1007. aseg->swap_add = cpu_to_be64(wr->wr.atomic.swap);
  1008. aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add);
  1009. } else {
  1010. aseg->swap_add = cpu_to_be64(wr->wr.atomic.compare_add);
  1011. aseg->compare = 0;
  1012. }
  1013. }
  1014. static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg,
  1015. struct ib_send_wr *wr)
  1016. {
  1017. memcpy(dseg->av, &to_mah(wr->wr.ud.ah)->av, sizeof (struct mlx4_av));
  1018. dseg->dqpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  1019. dseg->qkey = cpu_to_be32(wr->wr.ud.remote_qkey);
  1020. }
  1021. static void set_data_seg(struct mlx4_wqe_data_seg *dseg,
  1022. struct ib_sge *sg)
  1023. {
  1024. dseg->byte_count = cpu_to_be32(sg->length);
  1025. dseg->lkey = cpu_to_be32(sg->lkey);
  1026. dseg->addr = cpu_to_be64(sg->addr);
  1027. }
  1028. int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1029. struct ib_send_wr **bad_wr)
  1030. {
  1031. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1032. void *wqe;
  1033. struct mlx4_wqe_ctrl_seg *ctrl;
  1034. unsigned long flags;
  1035. int nreq;
  1036. int err = 0;
  1037. int ind;
  1038. int size;
  1039. int i;
  1040. spin_lock_irqsave(&qp->rq.lock, flags);
  1041. ind = qp->sq.head;
  1042. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1043. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  1044. err = -ENOMEM;
  1045. *bad_wr = wr;
  1046. goto out;
  1047. }
  1048. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  1049. err = -EINVAL;
  1050. *bad_wr = wr;
  1051. goto out;
  1052. }
  1053. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  1054. qp->sq.wrid[ind & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  1055. ctrl->srcrb_flags =
  1056. (wr->send_flags & IB_SEND_SIGNALED ?
  1057. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  1058. (wr->send_flags & IB_SEND_SOLICITED ?
  1059. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  1060. qp->sq_signal_bits;
  1061. if (wr->opcode == IB_WR_SEND_WITH_IMM ||
  1062. wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  1063. ctrl->imm = wr->imm_data;
  1064. else
  1065. ctrl->imm = 0;
  1066. wqe += sizeof *ctrl;
  1067. size = sizeof *ctrl / 16;
  1068. switch (ibqp->qp_type) {
  1069. case IB_QPT_RC:
  1070. case IB_QPT_UC:
  1071. switch (wr->opcode) {
  1072. case IB_WR_ATOMIC_CMP_AND_SWP:
  1073. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1074. set_raddr_seg(wqe, wr->wr.atomic.remote_addr,
  1075. wr->wr.atomic.rkey);
  1076. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1077. set_atomic_seg(wqe, wr);
  1078. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  1079. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  1080. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  1081. break;
  1082. case IB_WR_RDMA_READ:
  1083. case IB_WR_RDMA_WRITE:
  1084. case IB_WR_RDMA_WRITE_WITH_IMM:
  1085. set_raddr_seg(wqe, wr->wr.rdma.remote_addr,
  1086. wr->wr.rdma.rkey);
  1087. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1088. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  1089. break;
  1090. default:
  1091. /* No extra segments required for sends */
  1092. break;
  1093. }
  1094. break;
  1095. case IB_QPT_UD:
  1096. set_datagram_seg(wqe, wr);
  1097. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  1098. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  1099. break;
  1100. case IB_QPT_SMI:
  1101. case IB_QPT_GSI:
  1102. err = build_mlx_header(to_msqp(qp), wr, ctrl);
  1103. if (err < 0) {
  1104. *bad_wr = wr;
  1105. goto out;
  1106. }
  1107. wqe += err;
  1108. size += err / 16;
  1109. err = 0;
  1110. break;
  1111. default:
  1112. break;
  1113. }
  1114. for (i = 0; i < wr->num_sge; ++i) {
  1115. set_data_seg(wqe, wr->sg_list + i);
  1116. wqe += sizeof (struct mlx4_wqe_data_seg);
  1117. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1118. }
  1119. /* Add one more inline data segment for ICRC for MLX sends */
  1120. if (qp->ibqp.qp_type == IB_QPT_SMI || qp->ibqp.qp_type == IB_QPT_GSI) {
  1121. ((struct mlx4_wqe_inline_seg *) wqe)->byte_count =
  1122. cpu_to_be32((1 << 31) | 4);
  1123. ((u32 *) wqe)[1] = 0;
  1124. wqe += sizeof (struct mlx4_wqe_data_seg);
  1125. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1126. }
  1127. ctrl->fence_size = (wr->send_flags & IB_SEND_FENCE ?
  1128. MLX4_WQE_CTRL_FENCE : 0) | size;
  1129. /*
  1130. * Make sure descriptor is fully written before
  1131. * setting ownership bit (because HW can start
  1132. * executing as soon as we do).
  1133. */
  1134. wmb();
  1135. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  1136. err = -EINVAL;
  1137. goto out;
  1138. }
  1139. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  1140. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0);
  1141. /*
  1142. * We can improve latency by not stamping the last
  1143. * send queue WQE until after ringing the doorbell, so
  1144. * only stamp here if there are still more WQEs to post.
  1145. */
  1146. if (wr->next)
  1147. stamp_send_wqe(qp, (ind + qp->sq_spare_wqes) &
  1148. (qp->sq.wqe_cnt - 1));
  1149. ++ind;
  1150. }
  1151. out:
  1152. if (likely(nreq)) {
  1153. qp->sq.head += nreq;
  1154. /*
  1155. * Make sure that descriptors are written before
  1156. * doorbell record.
  1157. */
  1158. wmb();
  1159. writel(qp->doorbell_qpn,
  1160. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  1161. /*
  1162. * Make sure doorbells don't leak out of SQ spinlock
  1163. * and reach the HCA out of order.
  1164. */
  1165. mmiowb();
  1166. stamp_send_wqe(qp, (ind + qp->sq_spare_wqes - 1) &
  1167. (qp->sq.wqe_cnt - 1));
  1168. }
  1169. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1170. return err;
  1171. }
  1172. int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1173. struct ib_recv_wr **bad_wr)
  1174. {
  1175. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1176. struct mlx4_wqe_data_seg *scat;
  1177. unsigned long flags;
  1178. int err = 0;
  1179. int nreq;
  1180. int ind;
  1181. int i;
  1182. spin_lock_irqsave(&qp->rq.lock, flags);
  1183. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  1184. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1185. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.send_cq)) {
  1186. err = -ENOMEM;
  1187. *bad_wr = wr;
  1188. goto out;
  1189. }
  1190. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  1191. err = -EINVAL;
  1192. *bad_wr = wr;
  1193. goto out;
  1194. }
  1195. scat = get_recv_wqe(qp, ind);
  1196. for (i = 0; i < wr->num_sge; ++i) {
  1197. scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
  1198. scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey);
  1199. scat[i].addr = cpu_to_be64(wr->sg_list[i].addr);
  1200. }
  1201. if (i < qp->rq.max_gs) {
  1202. scat[i].byte_count = 0;
  1203. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  1204. scat[i].addr = 0;
  1205. }
  1206. qp->rq.wrid[ind] = wr->wr_id;
  1207. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  1208. }
  1209. out:
  1210. if (likely(nreq)) {
  1211. qp->rq.head += nreq;
  1212. /*
  1213. * Make sure that descriptors are written before
  1214. * doorbell record.
  1215. */
  1216. wmb();
  1217. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  1218. }
  1219. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1220. return err;
  1221. }
  1222. static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
  1223. {
  1224. switch (mlx4_state) {
  1225. case MLX4_QP_STATE_RST: return IB_QPS_RESET;
  1226. case MLX4_QP_STATE_INIT: return IB_QPS_INIT;
  1227. case MLX4_QP_STATE_RTR: return IB_QPS_RTR;
  1228. case MLX4_QP_STATE_RTS: return IB_QPS_RTS;
  1229. case MLX4_QP_STATE_SQ_DRAINING:
  1230. case MLX4_QP_STATE_SQD: return IB_QPS_SQD;
  1231. case MLX4_QP_STATE_SQER: return IB_QPS_SQE;
  1232. case MLX4_QP_STATE_ERR: return IB_QPS_ERR;
  1233. default: return -1;
  1234. }
  1235. }
  1236. static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
  1237. {
  1238. switch (mlx4_mig_state) {
  1239. case MLX4_QP_PM_ARMED: return IB_MIG_ARMED;
  1240. case MLX4_QP_PM_REARM: return IB_MIG_REARM;
  1241. case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
  1242. default: return -1;
  1243. }
  1244. }
  1245. static int to_ib_qp_access_flags(int mlx4_flags)
  1246. {
  1247. int ib_flags = 0;
  1248. if (mlx4_flags & MLX4_QP_BIT_RRE)
  1249. ib_flags |= IB_ACCESS_REMOTE_READ;
  1250. if (mlx4_flags & MLX4_QP_BIT_RWE)
  1251. ib_flags |= IB_ACCESS_REMOTE_WRITE;
  1252. if (mlx4_flags & MLX4_QP_BIT_RAE)
  1253. ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
  1254. return ib_flags;
  1255. }
  1256. static void to_ib_ah_attr(struct mlx4_dev *dev, struct ib_ah_attr *ib_ah_attr,
  1257. struct mlx4_qp_path *path)
  1258. {
  1259. memset(ib_ah_attr, 0, sizeof *ib_ah_attr);
  1260. ib_ah_attr->port_num = path->sched_queue & 0x40 ? 2 : 1;
  1261. if (ib_ah_attr->port_num == 0 || ib_ah_attr->port_num > dev->caps.num_ports)
  1262. return;
  1263. ib_ah_attr->dlid = be16_to_cpu(path->rlid);
  1264. ib_ah_attr->sl = (path->sched_queue >> 2) & 0xf;
  1265. ib_ah_attr->src_path_bits = path->grh_mylmc & 0x7f;
  1266. ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
  1267. ib_ah_attr->ah_flags = (path->grh_mylmc & (1 << 7)) ? IB_AH_GRH : 0;
  1268. if (ib_ah_attr->ah_flags) {
  1269. ib_ah_attr->grh.sgid_index = path->mgid_index;
  1270. ib_ah_attr->grh.hop_limit = path->hop_limit;
  1271. ib_ah_attr->grh.traffic_class =
  1272. (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
  1273. ib_ah_attr->grh.flow_label =
  1274. be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
  1275. memcpy(ib_ah_attr->grh.dgid.raw,
  1276. path->rgid, sizeof ib_ah_attr->grh.dgid.raw);
  1277. }
  1278. }
  1279. int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  1280. struct ib_qp_init_attr *qp_init_attr)
  1281. {
  1282. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1283. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1284. struct mlx4_qp_context context;
  1285. int mlx4_state;
  1286. int err;
  1287. if (qp->state == IB_QPS_RESET) {
  1288. qp_attr->qp_state = IB_QPS_RESET;
  1289. goto done;
  1290. }
  1291. err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
  1292. if (err)
  1293. return -EINVAL;
  1294. mlx4_state = be32_to_cpu(context.flags) >> 28;
  1295. qp_attr->qp_state = to_ib_qp_state(mlx4_state);
  1296. qp_attr->path_mtu = context.mtu_msgmax >> 5;
  1297. qp_attr->path_mig_state =
  1298. to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
  1299. qp_attr->qkey = be32_to_cpu(context.qkey);
  1300. qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
  1301. qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff;
  1302. qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff;
  1303. qp_attr->qp_access_flags =
  1304. to_ib_qp_access_flags(be32_to_cpu(context.params2));
  1305. if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
  1306. to_ib_ah_attr(dev->dev, &qp_attr->ah_attr, &context.pri_path);
  1307. to_ib_ah_attr(dev->dev, &qp_attr->alt_ah_attr, &context.alt_path);
  1308. qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
  1309. qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
  1310. }
  1311. qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
  1312. if (qp_attr->qp_state == IB_QPS_INIT)
  1313. qp_attr->port_num = qp->port;
  1314. else
  1315. qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
  1316. /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
  1317. qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
  1318. qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
  1319. qp_attr->max_dest_rd_atomic =
  1320. 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
  1321. qp_attr->min_rnr_timer =
  1322. (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
  1323. qp_attr->timeout = context.pri_path.ackto >> 3;
  1324. qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7;
  1325. qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7;
  1326. qp_attr->alt_timeout = context.alt_path.ackto >> 3;
  1327. done:
  1328. qp_attr->cur_qp_state = qp_attr->qp_state;
  1329. qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
  1330. qp_attr->cap.max_recv_sge = qp->rq.max_gs;
  1331. if (!ibqp->uobject) {
  1332. qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
  1333. qp_attr->cap.max_send_sge = qp->sq.max_gs;
  1334. } else {
  1335. qp_attr->cap.max_send_wr = 0;
  1336. qp_attr->cap.max_send_sge = 0;
  1337. }
  1338. /*
  1339. * We don't support inline sends for kernel QPs (yet), and we
  1340. * don't know what userspace's value should be.
  1341. */
  1342. qp_attr->cap.max_inline_data = 0;
  1343. qp_init_attr->cap = qp_attr->cap;
  1344. return 0;
  1345. }