qp.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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. return -EINVAL;
  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. err = -EINVAL;
  663. goto out;
  664. }
  665. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  666. MLX4_QP_OPTPAR_SCHED_QUEUE);
  667. }
  668. if (attr_mask & IB_QP_TIMEOUT) {
  669. context->pri_path.ackto = attr->timeout << 3;
  670. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  671. }
  672. if (attr_mask & IB_QP_ALT_PATH) {
  673. if (attr->alt_port_num == 0 ||
  674. attr->alt_port_num > dev->dev->caps.num_ports)
  675. return -EINVAL;
  676. if (attr->alt_pkey_index >=
  677. dev->dev->caps.pkey_table_len[attr->alt_port_num])
  678. return -EINVAL;
  679. if (mlx4_set_path(dev, &attr->alt_ah_attr, &context->alt_path,
  680. attr->alt_port_num))
  681. return -EINVAL;
  682. context->alt_path.pkey_index = attr->alt_pkey_index;
  683. context->alt_path.ackto = attr->alt_timeout << 3;
  684. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  685. }
  686. context->pd = cpu_to_be32(to_mpd(ibqp->pd)->pdn);
  687. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  688. if (attr_mask & IB_QP_RNR_RETRY) {
  689. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  690. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  691. }
  692. if (attr_mask & IB_QP_RETRY_CNT) {
  693. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  694. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  695. }
  696. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  697. if (attr->max_rd_atomic)
  698. context->params1 |=
  699. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  700. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  701. }
  702. if (attr_mask & IB_QP_SQ_PSN)
  703. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  704. context->cqn_send = cpu_to_be32(to_mcq(ibqp->send_cq)->mcq.cqn);
  705. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  706. if (attr->max_dest_rd_atomic)
  707. context->params2 |=
  708. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  709. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  710. }
  711. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  712. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  713. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  714. }
  715. if (ibqp->srq)
  716. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  717. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  718. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  719. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  720. }
  721. if (attr_mask & IB_QP_RQ_PSN)
  722. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  723. context->cqn_recv = cpu_to_be32(to_mcq(ibqp->recv_cq)->mcq.cqn);
  724. if (attr_mask & IB_QP_QKEY) {
  725. context->qkey = cpu_to_be32(attr->qkey);
  726. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  727. }
  728. if (ibqp->srq)
  729. context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
  730. if (!ibqp->srq && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  731. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  732. if (cur_state == IB_QPS_INIT &&
  733. new_state == IB_QPS_RTR &&
  734. (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  735. ibqp->qp_type == IB_QPT_UD)) {
  736. context->pri_path.sched_queue = (qp->port - 1) << 6;
  737. if (is_qp0(dev, qp))
  738. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  739. else
  740. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  741. }
  742. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  743. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  744. sqd_event = 1;
  745. else
  746. sqd_event = 0;
  747. /*
  748. * Before passing a kernel QP to the HW, make sure that the
  749. * ownership bits of the send queue are set and the SQ
  750. * headroom is stamped so that the hardware doesn't start
  751. * processing stale work requests.
  752. */
  753. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  754. struct mlx4_wqe_ctrl_seg *ctrl;
  755. int i;
  756. for (i = 0; i < qp->sq.wqe_cnt; ++i) {
  757. ctrl = get_send_wqe(qp, i);
  758. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  759. stamp_send_wqe(qp, i);
  760. }
  761. }
  762. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  763. to_mlx4_state(new_state), context, optpar,
  764. sqd_event, &qp->mqp);
  765. if (err)
  766. goto out;
  767. qp->state = new_state;
  768. if (attr_mask & IB_QP_ACCESS_FLAGS)
  769. qp->atomic_rd_en = attr->qp_access_flags;
  770. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  771. qp->resp_depth = attr->max_dest_rd_atomic;
  772. if (attr_mask & IB_QP_PORT)
  773. qp->port = attr->port_num;
  774. if (attr_mask & IB_QP_ALT_PATH)
  775. qp->alt_port = attr->alt_port_num;
  776. if (is_sqp(dev, qp))
  777. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  778. /*
  779. * If we moved QP0 to RTR, bring the IB link up; if we moved
  780. * QP0 to RESET or ERROR, bring the link back down.
  781. */
  782. if (is_qp0(dev, qp)) {
  783. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  784. if (mlx4_INIT_PORT(dev->dev, qp->port))
  785. printk(KERN_WARNING "INIT_PORT failed for port %d\n",
  786. qp->port);
  787. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  788. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  789. mlx4_CLOSE_PORT(dev->dev, qp->port);
  790. }
  791. /*
  792. * If we moved a kernel QP to RESET, clean up all old CQ
  793. * entries and reinitialize the QP.
  794. */
  795. if (new_state == IB_QPS_RESET && !ibqp->uobject) {
  796. mlx4_ib_cq_clean(to_mcq(ibqp->recv_cq), qp->mqp.qpn,
  797. ibqp->srq ? to_msrq(ibqp->srq): NULL);
  798. if (ibqp->send_cq != ibqp->recv_cq)
  799. mlx4_ib_cq_clean(to_mcq(ibqp->send_cq), qp->mqp.qpn, NULL);
  800. qp->rq.head = 0;
  801. qp->rq.tail = 0;
  802. qp->sq.head = 0;
  803. qp->sq.tail = 0;
  804. if (!ibqp->srq)
  805. *qp->db.db = 0;
  806. }
  807. out:
  808. kfree(context);
  809. return err;
  810. }
  811. static const struct ib_qp_attr mlx4_ib_qp_attr = { .port_num = 1 };
  812. static const int mlx4_ib_qp_attr_mask_table[IB_QPT_UD + 1] = {
  813. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  814. IB_QP_PORT |
  815. IB_QP_QKEY),
  816. [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
  817. IB_QP_PORT |
  818. IB_QP_ACCESS_FLAGS),
  819. [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
  820. IB_QP_PORT |
  821. IB_QP_ACCESS_FLAGS),
  822. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  823. IB_QP_QKEY),
  824. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  825. IB_QP_QKEY),
  826. };
  827. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  828. int attr_mask, struct ib_udata *udata)
  829. {
  830. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  831. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  832. enum ib_qp_state cur_state, new_state;
  833. int err = -EINVAL;
  834. mutex_lock(&qp->mutex);
  835. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  836. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  837. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask))
  838. goto out;
  839. if ((attr_mask & IB_QP_PORT) &&
  840. (attr->port_num == 0 || attr->port_num > dev->dev->caps.num_ports)) {
  841. goto out;
  842. }
  843. if (attr_mask & IB_QP_PKEY_INDEX) {
  844. int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  845. if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p])
  846. goto out;
  847. }
  848. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  849. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  850. goto out;
  851. }
  852. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  853. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  854. goto out;
  855. }
  856. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  857. err = 0;
  858. goto out;
  859. }
  860. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_ERR) {
  861. err = __mlx4_ib_modify_qp(ibqp, &mlx4_ib_qp_attr,
  862. mlx4_ib_qp_attr_mask_table[ibqp->qp_type],
  863. IB_QPS_RESET, IB_QPS_INIT);
  864. if (err)
  865. goto out;
  866. cur_state = IB_QPS_INIT;
  867. }
  868. err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
  869. out:
  870. mutex_unlock(&qp->mutex);
  871. return err;
  872. }
  873. static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_send_wr *wr,
  874. void *wqe)
  875. {
  876. struct ib_device *ib_dev = &to_mdev(sqp->qp.ibqp.device)->ib_dev;
  877. struct mlx4_wqe_mlx_seg *mlx = wqe;
  878. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  879. struct mlx4_ib_ah *ah = to_mah(wr->wr.ud.ah);
  880. u16 pkey;
  881. int send_size;
  882. int header_size;
  883. int spc;
  884. int i;
  885. send_size = 0;
  886. for (i = 0; i < wr->num_sge; ++i)
  887. send_size += wr->sg_list[i].length;
  888. ib_ud_header_init(send_size, mlx4_ib_ah_grh_present(ah), &sqp->ud_header);
  889. sqp->ud_header.lrh.service_level =
  890. be32_to_cpu(ah->av.sl_tclass_flowlabel) >> 28;
  891. sqp->ud_header.lrh.destination_lid = ah->av.dlid;
  892. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.g_slid & 0x7f);
  893. if (mlx4_ib_ah_grh_present(ah)) {
  894. sqp->ud_header.grh.traffic_class =
  895. (be32_to_cpu(ah->av.sl_tclass_flowlabel) >> 20) & 0xff;
  896. sqp->ud_header.grh.flow_label =
  897. ah->av.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  898. sqp->ud_header.grh.hop_limit = ah->av.hop_limit;
  899. ib_get_cached_gid(ib_dev, be32_to_cpu(ah->av.port_pd) >> 24,
  900. ah->av.gid_index, &sqp->ud_header.grh.source_gid);
  901. memcpy(sqp->ud_header.grh.destination_gid.raw,
  902. ah->av.dgid, 16);
  903. }
  904. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  905. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  906. (sqp->ud_header.lrh.destination_lid ==
  907. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  908. (sqp->ud_header.lrh.service_level << 8));
  909. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  910. switch (wr->opcode) {
  911. case IB_WR_SEND:
  912. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  913. sqp->ud_header.immediate_present = 0;
  914. break;
  915. case IB_WR_SEND_WITH_IMM:
  916. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  917. sqp->ud_header.immediate_present = 1;
  918. sqp->ud_header.immediate_data = wr->imm_data;
  919. break;
  920. default:
  921. return -EINVAL;
  922. }
  923. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
  924. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  925. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  926. sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
  927. if (!sqp->qp.ibqp.qp_num)
  928. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  929. else
  930. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->wr.ud.pkey_index, &pkey);
  931. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  932. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  933. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  934. sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
  935. sqp->qkey : wr->wr.ud.remote_qkey);
  936. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  937. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  938. if (0) {
  939. printk(KERN_ERR "built UD header of size %d:\n", header_size);
  940. for (i = 0; i < header_size / 4; ++i) {
  941. if (i % 8 == 0)
  942. printk(" [%02x] ", i * 4);
  943. printk(" %08x",
  944. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  945. if ((i + 1) % 8 == 0)
  946. printk("\n");
  947. }
  948. printk("\n");
  949. }
  950. /*
  951. * Inline data segments may not cross a 64 byte boundary. If
  952. * our UD header is bigger than the space available up to the
  953. * next 64 byte boundary in the WQE, use two inline data
  954. * segments to hold the UD header.
  955. */
  956. spc = MLX4_INLINE_ALIGN -
  957. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  958. if (header_size <= spc) {
  959. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  960. memcpy(inl + 1, sqp->header_buf, header_size);
  961. i = 1;
  962. } else {
  963. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  964. memcpy(inl + 1, sqp->header_buf, spc);
  965. inl = (void *) (inl + 1) + spc;
  966. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  967. /*
  968. * Need a barrier here to make sure all the data is
  969. * visible before the byte_count field is set.
  970. * Otherwise the HCA prefetcher could grab the 64-byte
  971. * chunk with this inline segment and get a valid (!=
  972. * 0xffffffff) byte count but stale data, and end up
  973. * generating a packet with bad headers.
  974. *
  975. * The first inline segment's byte_count field doesn't
  976. * need a barrier, because it comes after a
  977. * control/MLX segment and therefore is at an offset
  978. * of 16 mod 64.
  979. */
  980. wmb();
  981. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  982. i = 2;
  983. }
  984. return ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  985. }
  986. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  987. {
  988. unsigned cur;
  989. struct mlx4_ib_cq *cq;
  990. cur = wq->head - wq->tail;
  991. if (likely(cur + nreq < wq->max_post))
  992. return 0;
  993. cq = to_mcq(ib_cq);
  994. spin_lock(&cq->lock);
  995. cur = wq->head - wq->tail;
  996. spin_unlock(&cq->lock);
  997. return cur + nreq >= wq->max_post;
  998. }
  999. int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1000. struct ib_send_wr **bad_wr)
  1001. {
  1002. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1003. void *wqe;
  1004. struct mlx4_wqe_ctrl_seg *ctrl;
  1005. unsigned long flags;
  1006. int nreq;
  1007. int err = 0;
  1008. int ind;
  1009. int size;
  1010. int i;
  1011. spin_lock_irqsave(&qp->rq.lock, flags);
  1012. ind = qp->sq.head;
  1013. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1014. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  1015. err = -ENOMEM;
  1016. *bad_wr = wr;
  1017. goto out;
  1018. }
  1019. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  1020. err = -EINVAL;
  1021. *bad_wr = wr;
  1022. goto out;
  1023. }
  1024. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  1025. qp->sq.wrid[ind & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  1026. ctrl->srcrb_flags =
  1027. (wr->send_flags & IB_SEND_SIGNALED ?
  1028. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  1029. (wr->send_flags & IB_SEND_SOLICITED ?
  1030. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  1031. qp->sq_signal_bits;
  1032. if (wr->opcode == IB_WR_SEND_WITH_IMM ||
  1033. wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  1034. ctrl->imm = wr->imm_data;
  1035. else
  1036. ctrl->imm = 0;
  1037. wqe += sizeof *ctrl;
  1038. size = sizeof *ctrl / 16;
  1039. switch (ibqp->qp_type) {
  1040. case IB_QPT_RC:
  1041. case IB_QPT_UC:
  1042. switch (wr->opcode) {
  1043. case IB_WR_ATOMIC_CMP_AND_SWP:
  1044. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1045. ((struct mlx4_wqe_raddr_seg *) wqe)->raddr =
  1046. cpu_to_be64(wr->wr.atomic.remote_addr);
  1047. ((struct mlx4_wqe_raddr_seg *) wqe)->rkey =
  1048. cpu_to_be32(wr->wr.atomic.rkey);
  1049. ((struct mlx4_wqe_raddr_seg *) wqe)->reserved = 0;
  1050. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1051. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  1052. ((struct mlx4_wqe_atomic_seg *) wqe)->swap_add =
  1053. cpu_to_be64(wr->wr.atomic.swap);
  1054. ((struct mlx4_wqe_atomic_seg *) wqe)->compare =
  1055. cpu_to_be64(wr->wr.atomic.compare_add);
  1056. } else {
  1057. ((struct mlx4_wqe_atomic_seg *) wqe)->swap_add =
  1058. cpu_to_be64(wr->wr.atomic.compare_add);
  1059. ((struct mlx4_wqe_atomic_seg *) wqe)->compare = 0;
  1060. }
  1061. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  1062. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  1063. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  1064. break;
  1065. case IB_WR_RDMA_READ:
  1066. case IB_WR_RDMA_WRITE:
  1067. case IB_WR_RDMA_WRITE_WITH_IMM:
  1068. ((struct mlx4_wqe_raddr_seg *) wqe)->raddr =
  1069. cpu_to_be64(wr->wr.rdma.remote_addr);
  1070. ((struct mlx4_wqe_raddr_seg *) wqe)->rkey =
  1071. cpu_to_be32(wr->wr.rdma.rkey);
  1072. ((struct mlx4_wqe_raddr_seg *) wqe)->reserved = 0;
  1073. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1074. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  1075. break;
  1076. default:
  1077. /* No extra segments required for sends */
  1078. break;
  1079. }
  1080. break;
  1081. case IB_QPT_UD:
  1082. memcpy(((struct mlx4_wqe_datagram_seg *) wqe)->av,
  1083. &to_mah(wr->wr.ud.ah)->av, sizeof (struct mlx4_av));
  1084. ((struct mlx4_wqe_datagram_seg *) wqe)->dqpn =
  1085. cpu_to_be32(wr->wr.ud.remote_qpn);
  1086. ((struct mlx4_wqe_datagram_seg *) wqe)->qkey =
  1087. cpu_to_be32(wr->wr.ud.remote_qkey);
  1088. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  1089. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  1090. break;
  1091. case IB_QPT_SMI:
  1092. case IB_QPT_GSI:
  1093. err = build_mlx_header(to_msqp(qp), wr, ctrl);
  1094. if (err < 0) {
  1095. *bad_wr = wr;
  1096. goto out;
  1097. }
  1098. wqe += err;
  1099. size += err / 16;
  1100. err = 0;
  1101. break;
  1102. default:
  1103. break;
  1104. }
  1105. for (i = 0; i < wr->num_sge; ++i) {
  1106. ((struct mlx4_wqe_data_seg *) wqe)->byte_count =
  1107. cpu_to_be32(wr->sg_list[i].length);
  1108. ((struct mlx4_wqe_data_seg *) wqe)->lkey =
  1109. cpu_to_be32(wr->sg_list[i].lkey);
  1110. ((struct mlx4_wqe_data_seg *) wqe)->addr =
  1111. cpu_to_be64(wr->sg_list[i].addr);
  1112. wqe += sizeof (struct mlx4_wqe_data_seg);
  1113. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1114. }
  1115. /* Add one more inline data segment for ICRC for MLX sends */
  1116. if (qp->ibqp.qp_type == IB_QPT_SMI || qp->ibqp.qp_type == IB_QPT_GSI) {
  1117. ((struct mlx4_wqe_inline_seg *) wqe)->byte_count =
  1118. cpu_to_be32((1 << 31) | 4);
  1119. ((u32 *) wqe)[1] = 0;
  1120. wqe += sizeof (struct mlx4_wqe_data_seg);
  1121. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1122. }
  1123. ctrl->fence_size = (wr->send_flags & IB_SEND_FENCE ?
  1124. MLX4_WQE_CTRL_FENCE : 0) | size;
  1125. /*
  1126. * Make sure descriptor is fully written before
  1127. * setting ownership bit (because HW can start
  1128. * executing as soon as we do).
  1129. */
  1130. wmb();
  1131. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  1132. err = -EINVAL;
  1133. goto out;
  1134. }
  1135. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  1136. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0);
  1137. /*
  1138. * We can improve latency by not stamping the last
  1139. * send queue WQE until after ringing the doorbell, so
  1140. * only stamp here if there are still more WQEs to post.
  1141. */
  1142. if (wr->next)
  1143. stamp_send_wqe(qp, (ind + qp->sq_spare_wqes) &
  1144. (qp->sq.wqe_cnt - 1));
  1145. ++ind;
  1146. }
  1147. out:
  1148. if (likely(nreq)) {
  1149. qp->sq.head += nreq;
  1150. /*
  1151. * Make sure that descriptors are written before
  1152. * doorbell record.
  1153. */
  1154. wmb();
  1155. writel(qp->doorbell_qpn,
  1156. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  1157. /*
  1158. * Make sure doorbells don't leak out of SQ spinlock
  1159. * and reach the HCA out of order.
  1160. */
  1161. mmiowb();
  1162. stamp_send_wqe(qp, (ind + qp->sq_spare_wqes - 1) &
  1163. (qp->sq.wqe_cnt - 1));
  1164. }
  1165. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1166. return err;
  1167. }
  1168. int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1169. struct ib_recv_wr **bad_wr)
  1170. {
  1171. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1172. struct mlx4_wqe_data_seg *scat;
  1173. unsigned long flags;
  1174. int err = 0;
  1175. int nreq;
  1176. int ind;
  1177. int i;
  1178. spin_lock_irqsave(&qp->rq.lock, flags);
  1179. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  1180. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1181. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.send_cq)) {
  1182. err = -ENOMEM;
  1183. *bad_wr = wr;
  1184. goto out;
  1185. }
  1186. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  1187. err = -EINVAL;
  1188. *bad_wr = wr;
  1189. goto out;
  1190. }
  1191. scat = get_recv_wqe(qp, ind);
  1192. for (i = 0; i < wr->num_sge; ++i) {
  1193. scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
  1194. scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey);
  1195. scat[i].addr = cpu_to_be64(wr->sg_list[i].addr);
  1196. }
  1197. if (i < qp->rq.max_gs) {
  1198. scat[i].byte_count = 0;
  1199. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  1200. scat[i].addr = 0;
  1201. }
  1202. qp->rq.wrid[ind] = wr->wr_id;
  1203. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  1204. }
  1205. out:
  1206. if (likely(nreq)) {
  1207. qp->rq.head += nreq;
  1208. /*
  1209. * Make sure that descriptors are written before
  1210. * doorbell record.
  1211. */
  1212. wmb();
  1213. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  1214. }
  1215. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1216. return err;
  1217. }
  1218. static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
  1219. {
  1220. switch (mlx4_state) {
  1221. case MLX4_QP_STATE_RST: return IB_QPS_RESET;
  1222. case MLX4_QP_STATE_INIT: return IB_QPS_INIT;
  1223. case MLX4_QP_STATE_RTR: return IB_QPS_RTR;
  1224. case MLX4_QP_STATE_RTS: return IB_QPS_RTS;
  1225. case MLX4_QP_STATE_SQ_DRAINING:
  1226. case MLX4_QP_STATE_SQD: return IB_QPS_SQD;
  1227. case MLX4_QP_STATE_SQER: return IB_QPS_SQE;
  1228. case MLX4_QP_STATE_ERR: return IB_QPS_ERR;
  1229. default: return -1;
  1230. }
  1231. }
  1232. static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
  1233. {
  1234. switch (mlx4_mig_state) {
  1235. case MLX4_QP_PM_ARMED: return IB_MIG_ARMED;
  1236. case MLX4_QP_PM_REARM: return IB_MIG_REARM;
  1237. case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
  1238. default: return -1;
  1239. }
  1240. }
  1241. static int to_ib_qp_access_flags(int mlx4_flags)
  1242. {
  1243. int ib_flags = 0;
  1244. if (mlx4_flags & MLX4_QP_BIT_RRE)
  1245. ib_flags |= IB_ACCESS_REMOTE_READ;
  1246. if (mlx4_flags & MLX4_QP_BIT_RWE)
  1247. ib_flags |= IB_ACCESS_REMOTE_WRITE;
  1248. if (mlx4_flags & MLX4_QP_BIT_RAE)
  1249. ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
  1250. return ib_flags;
  1251. }
  1252. static void to_ib_ah_attr(struct mlx4_dev *dev, struct ib_ah_attr *ib_ah_attr,
  1253. struct mlx4_qp_path *path)
  1254. {
  1255. memset(ib_ah_attr, 0, sizeof *path);
  1256. ib_ah_attr->port_num = path->sched_queue & 0x40 ? 2 : 1;
  1257. if (ib_ah_attr->port_num == 0 || ib_ah_attr->port_num > dev->caps.num_ports)
  1258. return;
  1259. ib_ah_attr->dlid = be16_to_cpu(path->rlid);
  1260. ib_ah_attr->sl = (path->sched_queue >> 2) & 0xf;
  1261. ib_ah_attr->src_path_bits = path->grh_mylmc & 0x7f;
  1262. ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
  1263. ib_ah_attr->ah_flags = (path->grh_mylmc & (1 << 7)) ? IB_AH_GRH : 0;
  1264. if (ib_ah_attr->ah_flags) {
  1265. ib_ah_attr->grh.sgid_index = path->mgid_index;
  1266. ib_ah_attr->grh.hop_limit = path->hop_limit;
  1267. ib_ah_attr->grh.traffic_class =
  1268. (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
  1269. ib_ah_attr->grh.flow_label =
  1270. be32_to_cpu(path->tclass_flowlabel) & 0xffffff;
  1271. memcpy(ib_ah_attr->grh.dgid.raw,
  1272. path->rgid, sizeof ib_ah_attr->grh.dgid.raw);
  1273. }
  1274. }
  1275. int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  1276. struct ib_qp_init_attr *qp_init_attr)
  1277. {
  1278. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1279. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1280. struct mlx4_qp_context context;
  1281. int mlx4_state;
  1282. int err;
  1283. if (qp->state == IB_QPS_RESET) {
  1284. qp_attr->qp_state = IB_QPS_RESET;
  1285. goto done;
  1286. }
  1287. err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
  1288. if (err)
  1289. return -EINVAL;
  1290. mlx4_state = be32_to_cpu(context.flags) >> 28;
  1291. qp_attr->qp_state = to_ib_qp_state(mlx4_state);
  1292. qp_attr->path_mtu = context.mtu_msgmax >> 5;
  1293. qp_attr->path_mig_state =
  1294. to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
  1295. qp_attr->qkey = be32_to_cpu(context.qkey);
  1296. qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
  1297. qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff;
  1298. qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff;
  1299. qp_attr->qp_access_flags =
  1300. to_ib_qp_access_flags(be32_to_cpu(context.params2));
  1301. if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
  1302. to_ib_ah_attr(dev->dev, &qp_attr->ah_attr, &context.pri_path);
  1303. to_ib_ah_attr(dev->dev, &qp_attr->alt_ah_attr, &context.alt_path);
  1304. qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
  1305. qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
  1306. }
  1307. qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
  1308. qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
  1309. /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
  1310. qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
  1311. qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
  1312. qp_attr->max_dest_rd_atomic =
  1313. 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
  1314. qp_attr->min_rnr_timer =
  1315. (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
  1316. qp_attr->timeout = context.pri_path.ackto >> 3;
  1317. qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7;
  1318. qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7;
  1319. qp_attr->alt_timeout = context.alt_path.ackto >> 3;
  1320. done:
  1321. qp_attr->cur_qp_state = qp_attr->qp_state;
  1322. if (!ibqp->uobject) {
  1323. qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
  1324. qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
  1325. qp_attr->cap.max_send_sge = qp->sq.max_gs;
  1326. qp_attr->cap.max_recv_sge = qp->rq.max_gs;
  1327. qp_attr->cap.max_inline_data = (1 << qp->sq.wqe_shift) -
  1328. send_wqe_overhead(qp->ibqp.qp_type) -
  1329. sizeof (struct mlx4_wqe_inline_seg);
  1330. qp_init_attr->cap = qp_attr->cap;
  1331. }
  1332. return 0;
  1333. }