qp.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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. sizeof (struct mlx4_wqe_inline_seg),
  175. sizeof (struct mlx4_wqe_data_seg)) +
  176. ALIGN(4 +
  177. sizeof (struct mlx4_wqe_inline_seg),
  178. sizeof (struct mlx4_wqe_data_seg));
  179. default:
  180. return sizeof (struct mlx4_wqe_ctrl_seg);
  181. }
  182. }
  183. static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  184. int is_user, int has_srq, struct mlx4_ib_qp *qp)
  185. {
  186. /* Sanity check RQ size before proceeding */
  187. if (cap->max_recv_wr > dev->dev->caps.max_wqes ||
  188. cap->max_recv_sge > dev->dev->caps.max_rq_sg)
  189. return -EINVAL;
  190. if (has_srq) {
  191. /* QPs attached to an SRQ should have no RQ */
  192. if (cap->max_recv_wr)
  193. return -EINVAL;
  194. qp->rq.wqe_cnt = qp->rq.max_gs = 0;
  195. } else {
  196. /* HW requires >= 1 RQ entry with >= 1 gather entry */
  197. if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge))
  198. return -EINVAL;
  199. qp->rq.wqe_cnt = roundup_pow_of_two(max(1U, cap->max_recv_wr));
  200. qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge));
  201. qp->rq.wqe_shift = ilog2(qp->rq.max_gs * sizeof (struct mlx4_wqe_data_seg));
  202. }
  203. cap->max_recv_wr = qp->rq.max_post = qp->rq.wqe_cnt;
  204. cap->max_recv_sge = qp->rq.max_gs;
  205. return 0;
  206. }
  207. static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  208. enum ib_qp_type type, struct mlx4_ib_qp *qp)
  209. {
  210. /* Sanity check SQ size before proceeding */
  211. if (cap->max_send_wr > dev->dev->caps.max_wqes ||
  212. cap->max_send_sge > dev->dev->caps.max_sq_sg ||
  213. cap->max_inline_data + send_wqe_overhead(type) +
  214. sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
  215. return -EINVAL;
  216. /*
  217. * For MLX transport we need 2 extra S/G entries:
  218. * one for the header and one for the checksum at the end
  219. */
  220. if ((type == IB_QPT_SMI || type == IB_QPT_GSI) &&
  221. cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
  222. return -EINVAL;
  223. qp->sq.wqe_shift = ilog2(roundup_pow_of_two(max(cap->max_send_sge *
  224. sizeof (struct mlx4_wqe_data_seg),
  225. cap->max_inline_data +
  226. sizeof (struct mlx4_wqe_inline_seg)) +
  227. send_wqe_overhead(type)));
  228. qp->sq.max_gs = ((1 << qp->sq.wqe_shift) - send_wqe_overhead(type)) /
  229. sizeof (struct mlx4_wqe_data_seg);
  230. /*
  231. * We need to leave 2 KB + 1 WQE of headroom in the SQ to
  232. * allow HW to prefetch.
  233. */
  234. qp->sq_spare_wqes = (2048 >> qp->sq.wqe_shift) + 1;
  235. qp->sq.wqe_cnt = roundup_pow_of_two(cap->max_send_wr + qp->sq_spare_wqes);
  236. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  237. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  238. if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
  239. qp->rq.offset = 0;
  240. qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
  241. } else {
  242. qp->rq.offset = qp->sq.wqe_cnt << qp->sq.wqe_shift;
  243. qp->sq.offset = 0;
  244. }
  245. cap->max_send_wr = qp->sq.max_post = qp->sq.wqe_cnt - qp->sq_spare_wqes;
  246. cap->max_send_sge = qp->sq.max_gs;
  247. cap->max_inline_data = (1 << qp->sq.wqe_shift) - send_wqe_overhead(type) -
  248. sizeof (struct mlx4_wqe_inline_seg);
  249. return 0;
  250. }
  251. static int set_user_sq_size(struct mlx4_ib_qp *qp,
  252. struct mlx4_ib_create_qp *ucmd)
  253. {
  254. qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count;
  255. qp->sq.wqe_shift = ucmd->log_sq_stride;
  256. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  257. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  258. return 0;
  259. }
  260. static int create_qp_common(struct mlx4_ib_dev *dev, struct ib_pd *pd,
  261. struct ib_qp_init_attr *init_attr,
  262. struct ib_udata *udata, int sqpn, struct mlx4_ib_qp *qp)
  263. {
  264. int err;
  265. mutex_init(&qp->mutex);
  266. spin_lock_init(&qp->sq.lock);
  267. spin_lock_init(&qp->rq.lock);
  268. qp->state = IB_QPS_RESET;
  269. qp->atomic_rd_en = 0;
  270. qp->resp_depth = 0;
  271. qp->rq.head = 0;
  272. qp->rq.tail = 0;
  273. qp->sq.head = 0;
  274. qp->sq.tail = 0;
  275. err = set_rq_size(dev, &init_attr->cap, !!pd->uobject, !!init_attr->srq, qp);
  276. if (err)
  277. goto err;
  278. if (pd->uobject) {
  279. struct mlx4_ib_create_qp ucmd;
  280. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  281. err = -EFAULT;
  282. goto err;
  283. }
  284. qp->sq_no_prefetch = ucmd.sq_no_prefetch;
  285. err = set_user_sq_size(qp, &ucmd);
  286. if (err)
  287. goto err;
  288. qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
  289. qp->buf_size, 0);
  290. if (IS_ERR(qp->umem)) {
  291. err = PTR_ERR(qp->umem);
  292. goto err;
  293. }
  294. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(qp->umem),
  295. ilog2(qp->umem->page_size), &qp->mtt);
  296. if (err)
  297. goto err_buf;
  298. err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
  299. if (err)
  300. goto err_mtt;
  301. if (!init_attr->srq) {
  302. err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
  303. ucmd.db_addr, &qp->db);
  304. if (err)
  305. goto err_mtt;
  306. }
  307. } else {
  308. qp->sq_no_prefetch = 0;
  309. err = set_kernel_sq_size(dev, &init_attr->cap, init_attr->qp_type, qp);
  310. if (err)
  311. goto err;
  312. if (!init_attr->srq) {
  313. err = mlx4_ib_db_alloc(dev, &qp->db, 0);
  314. if (err)
  315. goto err;
  316. *qp->db.db = 0;
  317. }
  318. if (mlx4_buf_alloc(dev->dev, qp->buf_size, PAGE_SIZE * 2, &qp->buf)) {
  319. err = -ENOMEM;
  320. goto err_db;
  321. }
  322. err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
  323. &qp->mtt);
  324. if (err)
  325. goto err_buf;
  326. err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf);
  327. if (err)
  328. goto err_mtt;
  329. qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof (u64), GFP_KERNEL);
  330. qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof (u64), GFP_KERNEL);
  331. if (!qp->sq.wrid || !qp->rq.wrid) {
  332. err = -ENOMEM;
  333. goto err_wrid;
  334. }
  335. /* We don't support inline sends for kernel QPs (yet) */
  336. init_attr->cap.max_inline_data = 0;
  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 void init_port(struct mlx4_ib_dev *dev, int port)
  512. {
  513. struct mlx4_init_port_param param;
  514. int err;
  515. memset(&param, 0, sizeof param);
  516. param.port_width_cap = dev->dev->caps.port_width_cap;
  517. param.vl_cap = dev->dev->caps.vl_cap;
  518. param.mtu = ib_mtu_enum_to_int(dev->dev->caps.mtu_cap);
  519. param.max_gid = dev->dev->caps.gid_table_len;
  520. param.max_pkey = dev->dev->caps.pkey_table_len;
  521. err = mlx4_INIT_PORT(dev->dev, &param, port);
  522. if (err)
  523. printk(KERN_WARNING "INIT_PORT failed, return code %d.\n", err);
  524. }
  525. static int to_mlx4_st(enum ib_qp_type type)
  526. {
  527. switch (type) {
  528. case IB_QPT_RC: return MLX4_QP_ST_RC;
  529. case IB_QPT_UC: return MLX4_QP_ST_UC;
  530. case IB_QPT_UD: return MLX4_QP_ST_UD;
  531. case IB_QPT_SMI:
  532. case IB_QPT_GSI: return MLX4_QP_ST_MLX;
  533. default: return -1;
  534. }
  535. }
  536. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  537. int attr_mask)
  538. {
  539. u8 dest_rd_atomic;
  540. u32 access_flags;
  541. u32 hw_access_flags = 0;
  542. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  543. dest_rd_atomic = attr->max_dest_rd_atomic;
  544. else
  545. dest_rd_atomic = qp->resp_depth;
  546. if (attr_mask & IB_QP_ACCESS_FLAGS)
  547. access_flags = attr->qp_access_flags;
  548. else
  549. access_flags = qp->atomic_rd_en;
  550. if (!dest_rd_atomic)
  551. access_flags &= IB_ACCESS_REMOTE_WRITE;
  552. if (access_flags & IB_ACCESS_REMOTE_READ)
  553. hw_access_flags |= MLX4_QP_BIT_RRE;
  554. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  555. hw_access_flags |= MLX4_QP_BIT_RAE;
  556. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  557. hw_access_flags |= MLX4_QP_BIT_RWE;
  558. return cpu_to_be32(hw_access_flags);
  559. }
  560. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  561. int attr_mask)
  562. {
  563. if (attr_mask & IB_QP_PKEY_INDEX)
  564. sqp->pkey_index = attr->pkey_index;
  565. if (attr_mask & IB_QP_QKEY)
  566. sqp->qkey = attr->qkey;
  567. if (attr_mask & IB_QP_SQ_PSN)
  568. sqp->send_psn = attr->sq_psn;
  569. }
  570. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  571. {
  572. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  573. }
  574. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_ah_attr *ah,
  575. struct mlx4_qp_path *path, u8 port)
  576. {
  577. path->grh_mylmc = ah->src_path_bits & 0x7f;
  578. path->rlid = cpu_to_be16(ah->dlid);
  579. if (ah->static_rate) {
  580. path->static_rate = ah->static_rate + MLX4_STAT_RATE_OFFSET;
  581. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  582. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  583. --path->static_rate;
  584. } else
  585. path->static_rate = 0;
  586. path->counter_index = 0xff;
  587. if (ah->ah_flags & IB_AH_GRH) {
  588. if (ah->grh.sgid_index >= dev->dev->caps.gid_table_len) {
  589. printk(KERN_ERR "sgid_index (%u) too large. max is %d\n",
  590. ah->grh.sgid_index, dev->dev->caps.gid_table_len - 1);
  591. return -1;
  592. }
  593. path->grh_mylmc |= 1 << 7;
  594. path->mgid_index = ah->grh.sgid_index;
  595. path->hop_limit = ah->grh.hop_limit;
  596. path->tclass_flowlabel =
  597. cpu_to_be32((ah->grh.traffic_class << 20) |
  598. (ah->grh.flow_label));
  599. memcpy(path->rgid, ah->grh.dgid.raw, 16);
  600. }
  601. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  602. ((port - 1) << 6) | ((ah->sl & 0xf) << 2);
  603. return 0;
  604. }
  605. static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
  606. const struct ib_qp_attr *attr, int attr_mask,
  607. enum ib_qp_state cur_state, enum ib_qp_state new_state)
  608. {
  609. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  610. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  611. struct mlx4_qp_context *context;
  612. enum mlx4_qp_optpar optpar = 0;
  613. int sqd_event;
  614. int err = -EINVAL;
  615. context = kzalloc(sizeof *context, GFP_KERNEL);
  616. if (!context)
  617. return -ENOMEM;
  618. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  619. (to_mlx4_st(ibqp->qp_type) << 16));
  620. context->flags |= cpu_to_be32(1 << 8); /* DE? */
  621. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  622. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  623. else {
  624. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  625. switch (attr->path_mig_state) {
  626. case IB_MIG_MIGRATED:
  627. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  628. break;
  629. case IB_MIG_REARM:
  630. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  631. break;
  632. case IB_MIG_ARMED:
  633. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  634. break;
  635. }
  636. }
  637. if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  638. ibqp->qp_type == IB_QPT_UD)
  639. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  640. else if (attr_mask & IB_QP_PATH_MTU) {
  641. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  642. printk(KERN_ERR "path MTU (%u) is invalid\n",
  643. attr->path_mtu);
  644. return -EINVAL;
  645. }
  646. context->mtu_msgmax = (attr->path_mtu << 5) | 31;
  647. }
  648. if (qp->rq.wqe_cnt)
  649. context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
  650. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  651. if (qp->sq.wqe_cnt)
  652. context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
  653. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  654. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  655. context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
  656. if (qp->ibqp.uobject)
  657. context->usr_page = cpu_to_be32(to_mucontext(ibqp->uobject->context)->uar.index);
  658. else
  659. context->usr_page = cpu_to_be32(dev->priv_uar.index);
  660. if (attr_mask & IB_QP_DEST_QPN)
  661. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  662. if (attr_mask & IB_QP_PORT) {
  663. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  664. !(attr_mask & IB_QP_AV)) {
  665. mlx4_set_sched(&context->pri_path, attr->port_num);
  666. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  667. }
  668. }
  669. if (attr_mask & IB_QP_PKEY_INDEX) {
  670. context->pri_path.pkey_index = attr->pkey_index;
  671. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  672. }
  673. if (attr_mask & IB_QP_AV) {
  674. if (mlx4_set_path(dev, &attr->ah_attr, &context->pri_path,
  675. attr_mask & IB_QP_PORT ? attr->port_num : qp->port)) {
  676. err = -EINVAL;
  677. goto out;
  678. }
  679. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  680. MLX4_QP_OPTPAR_SCHED_QUEUE);
  681. }
  682. if (attr_mask & IB_QP_TIMEOUT) {
  683. context->pri_path.ackto = attr->timeout << 3;
  684. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  685. }
  686. if (attr_mask & IB_QP_ALT_PATH) {
  687. if (attr->alt_pkey_index >= dev->dev->caps.pkey_table_len)
  688. return -EINVAL;
  689. if (attr->alt_port_num == 0 ||
  690. attr->alt_port_num > dev->dev->caps.num_ports)
  691. return -EINVAL;
  692. if (mlx4_set_path(dev, &attr->alt_ah_attr, &context->alt_path,
  693. attr->alt_port_num))
  694. return -EINVAL;
  695. context->alt_path.pkey_index = attr->alt_pkey_index;
  696. context->alt_path.ackto = attr->alt_timeout << 3;
  697. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  698. }
  699. context->pd = cpu_to_be32(to_mpd(ibqp->pd)->pdn);
  700. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  701. if (attr_mask & IB_QP_RNR_RETRY) {
  702. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  703. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  704. }
  705. if (attr_mask & IB_QP_RETRY_CNT) {
  706. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  707. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  708. }
  709. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  710. if (attr->max_rd_atomic)
  711. context->params1 |=
  712. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  713. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  714. }
  715. if (attr_mask & IB_QP_SQ_PSN)
  716. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  717. context->cqn_send = cpu_to_be32(to_mcq(ibqp->send_cq)->mcq.cqn);
  718. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  719. if (attr->max_dest_rd_atomic)
  720. context->params2 |=
  721. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  722. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  723. }
  724. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  725. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  726. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  727. }
  728. if (ibqp->srq)
  729. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  730. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  731. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  732. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  733. }
  734. if (attr_mask & IB_QP_RQ_PSN)
  735. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  736. context->cqn_recv = cpu_to_be32(to_mcq(ibqp->recv_cq)->mcq.cqn);
  737. if (attr_mask & IB_QP_QKEY) {
  738. context->qkey = cpu_to_be32(attr->qkey);
  739. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  740. }
  741. if (ibqp->srq)
  742. context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
  743. if (!ibqp->srq && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  744. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  745. if (cur_state == IB_QPS_INIT &&
  746. new_state == IB_QPS_RTR &&
  747. (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  748. ibqp->qp_type == IB_QPT_UD)) {
  749. context->pri_path.sched_queue = (qp->port - 1) << 6;
  750. if (is_qp0(dev, qp))
  751. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  752. else
  753. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  754. }
  755. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  756. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  757. sqd_event = 1;
  758. else
  759. sqd_event = 0;
  760. /*
  761. * Before passing a kernel QP to the HW, make sure that the
  762. * ownership bits of the send queue are set and the SQ
  763. * headroom is stamped so that the hardware doesn't start
  764. * processing stale work requests.
  765. */
  766. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  767. struct mlx4_wqe_ctrl_seg *ctrl;
  768. int i;
  769. for (i = 0; i < qp->sq.wqe_cnt; ++i) {
  770. ctrl = get_send_wqe(qp, i);
  771. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  772. stamp_send_wqe(qp, i);
  773. }
  774. }
  775. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  776. to_mlx4_state(new_state), context, optpar,
  777. sqd_event, &qp->mqp);
  778. if (err)
  779. goto out;
  780. qp->state = new_state;
  781. if (attr_mask & IB_QP_ACCESS_FLAGS)
  782. qp->atomic_rd_en = attr->qp_access_flags;
  783. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  784. qp->resp_depth = attr->max_dest_rd_atomic;
  785. if (attr_mask & IB_QP_PORT)
  786. qp->port = attr->port_num;
  787. if (attr_mask & IB_QP_ALT_PATH)
  788. qp->alt_port = attr->alt_port_num;
  789. if (is_sqp(dev, qp))
  790. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  791. /*
  792. * If we moved QP0 to RTR, bring the IB link up; if we moved
  793. * QP0 to RESET or ERROR, bring the link back down.
  794. */
  795. if (is_qp0(dev, qp)) {
  796. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  797. init_port(dev, qp->port);
  798. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  799. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  800. mlx4_CLOSE_PORT(dev->dev, qp->port);
  801. }
  802. /*
  803. * If we moved a kernel QP to RESET, clean up all old CQ
  804. * entries and reinitialize the QP.
  805. */
  806. if (new_state == IB_QPS_RESET && !ibqp->uobject) {
  807. mlx4_ib_cq_clean(to_mcq(ibqp->recv_cq), qp->mqp.qpn,
  808. ibqp->srq ? to_msrq(ibqp->srq): NULL);
  809. if (ibqp->send_cq != ibqp->recv_cq)
  810. mlx4_ib_cq_clean(to_mcq(ibqp->send_cq), qp->mqp.qpn, NULL);
  811. qp->rq.head = 0;
  812. qp->rq.tail = 0;
  813. qp->sq.head = 0;
  814. qp->sq.tail = 0;
  815. if (!ibqp->srq)
  816. *qp->db.db = 0;
  817. }
  818. out:
  819. kfree(context);
  820. return err;
  821. }
  822. static const struct ib_qp_attr mlx4_ib_qp_attr = { .port_num = 1 };
  823. static const int mlx4_ib_qp_attr_mask_table[IB_QPT_UD + 1] = {
  824. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  825. IB_QP_PORT |
  826. IB_QP_QKEY),
  827. [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
  828. IB_QP_PORT |
  829. IB_QP_ACCESS_FLAGS),
  830. [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
  831. IB_QP_PORT |
  832. IB_QP_ACCESS_FLAGS),
  833. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  834. IB_QP_QKEY),
  835. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  836. IB_QP_QKEY),
  837. };
  838. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  839. int attr_mask, struct ib_udata *udata)
  840. {
  841. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  842. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  843. enum ib_qp_state cur_state, new_state;
  844. int err = -EINVAL;
  845. mutex_lock(&qp->mutex);
  846. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  847. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  848. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask))
  849. goto out;
  850. if ((attr_mask & IB_QP_PKEY_INDEX) &&
  851. attr->pkey_index >= dev->dev->caps.pkey_table_len) {
  852. goto out;
  853. }
  854. if ((attr_mask & IB_QP_PORT) &&
  855. (attr->port_num == 0 || attr->port_num > dev->dev->caps.num_ports)) {
  856. goto out;
  857. }
  858. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  859. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  860. goto out;
  861. }
  862. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  863. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  864. goto out;
  865. }
  866. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  867. err = 0;
  868. goto out;
  869. }
  870. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_ERR) {
  871. err = __mlx4_ib_modify_qp(ibqp, &mlx4_ib_qp_attr,
  872. mlx4_ib_qp_attr_mask_table[ibqp->qp_type],
  873. IB_QPS_RESET, IB_QPS_INIT);
  874. if (err)
  875. goto out;
  876. cur_state = IB_QPS_INIT;
  877. }
  878. err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
  879. out:
  880. mutex_unlock(&qp->mutex);
  881. return err;
  882. }
  883. static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_send_wr *wr,
  884. void *wqe)
  885. {
  886. struct ib_device *ib_dev = &to_mdev(sqp->qp.ibqp.device)->ib_dev;
  887. struct mlx4_wqe_mlx_seg *mlx = wqe;
  888. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  889. struct mlx4_ib_ah *ah = to_mah(wr->wr.ud.ah);
  890. u16 pkey;
  891. int send_size;
  892. int header_size;
  893. int i;
  894. send_size = 0;
  895. for (i = 0; i < wr->num_sge; ++i)
  896. send_size += wr->sg_list[i].length;
  897. ib_ud_header_init(send_size, mlx4_ib_ah_grh_present(ah), &sqp->ud_header);
  898. sqp->ud_header.lrh.service_level =
  899. be32_to_cpu(ah->av.sl_tclass_flowlabel) >> 28;
  900. sqp->ud_header.lrh.destination_lid = ah->av.dlid;
  901. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.g_slid & 0x7f);
  902. if (mlx4_ib_ah_grh_present(ah)) {
  903. sqp->ud_header.grh.traffic_class =
  904. (be32_to_cpu(ah->av.sl_tclass_flowlabel) >> 20) & 0xff;
  905. sqp->ud_header.grh.flow_label =
  906. ah->av.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  907. sqp->ud_header.grh.hop_limit = ah->av.hop_limit;
  908. ib_get_cached_gid(ib_dev, be32_to_cpu(ah->av.port_pd) >> 24,
  909. ah->av.gid_index, &sqp->ud_header.grh.source_gid);
  910. memcpy(sqp->ud_header.grh.destination_gid.raw,
  911. ah->av.dgid, 16);
  912. }
  913. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  914. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  915. (sqp->ud_header.lrh.destination_lid ==
  916. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  917. (sqp->ud_header.lrh.service_level << 8));
  918. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  919. switch (wr->opcode) {
  920. case IB_WR_SEND:
  921. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  922. sqp->ud_header.immediate_present = 0;
  923. break;
  924. case IB_WR_SEND_WITH_IMM:
  925. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  926. sqp->ud_header.immediate_present = 1;
  927. sqp->ud_header.immediate_data = wr->imm_data;
  928. break;
  929. default:
  930. return -EINVAL;
  931. }
  932. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
  933. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  934. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  935. sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
  936. if (!sqp->qp.ibqp.qp_num)
  937. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  938. else
  939. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->wr.ud.pkey_index, &pkey);
  940. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  941. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  942. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  943. sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
  944. sqp->qkey : wr->wr.ud.remote_qkey);
  945. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  946. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  947. if (0) {
  948. printk(KERN_ERR "built UD header of size %d:\n", header_size);
  949. for (i = 0; i < header_size / 4; ++i) {
  950. if (i % 8 == 0)
  951. printk(" [%02x] ", i * 4);
  952. printk(" %08x",
  953. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  954. if ((i + 1) % 8 == 0)
  955. printk("\n");
  956. }
  957. printk("\n");
  958. }
  959. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  960. memcpy(inl + 1, sqp->header_buf, header_size);
  961. return ALIGN(sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  962. }
  963. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  964. {
  965. unsigned cur;
  966. struct mlx4_ib_cq *cq;
  967. cur = wq->head - wq->tail;
  968. if (likely(cur + nreq < wq->max_post))
  969. return 0;
  970. cq = to_mcq(ib_cq);
  971. spin_lock(&cq->lock);
  972. cur = wq->head - wq->tail;
  973. spin_unlock(&cq->lock);
  974. return cur + nreq >= wq->max_post;
  975. }
  976. int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  977. struct ib_send_wr **bad_wr)
  978. {
  979. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  980. void *wqe;
  981. struct mlx4_wqe_ctrl_seg *ctrl;
  982. unsigned long flags;
  983. int nreq;
  984. int err = 0;
  985. int ind;
  986. int size;
  987. int i;
  988. spin_lock_irqsave(&qp->rq.lock, flags);
  989. ind = qp->sq.head;
  990. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  991. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  992. err = -ENOMEM;
  993. *bad_wr = wr;
  994. goto out;
  995. }
  996. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  997. err = -EINVAL;
  998. *bad_wr = wr;
  999. goto out;
  1000. }
  1001. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  1002. qp->sq.wrid[ind & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  1003. ctrl->srcrb_flags =
  1004. (wr->send_flags & IB_SEND_SIGNALED ?
  1005. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  1006. (wr->send_flags & IB_SEND_SOLICITED ?
  1007. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  1008. qp->sq_signal_bits;
  1009. if (wr->opcode == IB_WR_SEND_WITH_IMM ||
  1010. wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  1011. ctrl->imm = wr->imm_data;
  1012. else
  1013. ctrl->imm = 0;
  1014. wqe += sizeof *ctrl;
  1015. size = sizeof *ctrl / 16;
  1016. switch (ibqp->qp_type) {
  1017. case IB_QPT_RC:
  1018. case IB_QPT_UC:
  1019. switch (wr->opcode) {
  1020. case IB_WR_ATOMIC_CMP_AND_SWP:
  1021. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1022. ((struct mlx4_wqe_raddr_seg *) wqe)->raddr =
  1023. cpu_to_be64(wr->wr.atomic.remote_addr);
  1024. ((struct mlx4_wqe_raddr_seg *) wqe)->rkey =
  1025. cpu_to_be32(wr->wr.atomic.rkey);
  1026. ((struct mlx4_wqe_raddr_seg *) wqe)->reserved = 0;
  1027. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1028. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  1029. ((struct mlx4_wqe_atomic_seg *) wqe)->swap_add =
  1030. cpu_to_be64(wr->wr.atomic.swap);
  1031. ((struct mlx4_wqe_atomic_seg *) wqe)->compare =
  1032. cpu_to_be64(wr->wr.atomic.compare_add);
  1033. } else {
  1034. ((struct mlx4_wqe_atomic_seg *) wqe)->swap_add =
  1035. cpu_to_be64(wr->wr.atomic.compare_add);
  1036. ((struct mlx4_wqe_atomic_seg *) wqe)->compare = 0;
  1037. }
  1038. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  1039. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  1040. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  1041. break;
  1042. case IB_WR_RDMA_READ:
  1043. case IB_WR_RDMA_WRITE:
  1044. case IB_WR_RDMA_WRITE_WITH_IMM:
  1045. ((struct mlx4_wqe_raddr_seg *) wqe)->raddr =
  1046. cpu_to_be64(wr->wr.rdma.remote_addr);
  1047. ((struct mlx4_wqe_raddr_seg *) wqe)->rkey =
  1048. cpu_to_be32(wr->wr.rdma.rkey);
  1049. ((struct mlx4_wqe_raddr_seg *) wqe)->reserved = 0;
  1050. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1051. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  1052. break;
  1053. default:
  1054. /* No extra segments required for sends */
  1055. break;
  1056. }
  1057. break;
  1058. case IB_QPT_UD:
  1059. memcpy(((struct mlx4_wqe_datagram_seg *) wqe)->av,
  1060. &to_mah(wr->wr.ud.ah)->av, sizeof (struct mlx4_av));
  1061. ((struct mlx4_wqe_datagram_seg *) wqe)->dqpn =
  1062. cpu_to_be32(wr->wr.ud.remote_qpn);
  1063. ((struct mlx4_wqe_datagram_seg *) wqe)->qkey =
  1064. cpu_to_be32(wr->wr.ud.remote_qkey);
  1065. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  1066. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  1067. break;
  1068. case IB_QPT_SMI:
  1069. case IB_QPT_GSI:
  1070. err = build_mlx_header(to_msqp(qp), wr, ctrl);
  1071. if (err < 0) {
  1072. *bad_wr = wr;
  1073. goto out;
  1074. }
  1075. wqe += err;
  1076. size += err / 16;
  1077. err = 0;
  1078. break;
  1079. default:
  1080. break;
  1081. }
  1082. for (i = 0; i < wr->num_sge; ++i) {
  1083. ((struct mlx4_wqe_data_seg *) wqe)->byte_count =
  1084. cpu_to_be32(wr->sg_list[i].length);
  1085. ((struct mlx4_wqe_data_seg *) wqe)->lkey =
  1086. cpu_to_be32(wr->sg_list[i].lkey);
  1087. ((struct mlx4_wqe_data_seg *) wqe)->addr =
  1088. cpu_to_be64(wr->sg_list[i].addr);
  1089. wqe += sizeof (struct mlx4_wqe_data_seg);
  1090. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1091. }
  1092. /* Add one more inline data segment for ICRC for MLX sends */
  1093. if (qp->ibqp.qp_type == IB_QPT_SMI || qp->ibqp.qp_type == IB_QPT_GSI) {
  1094. ((struct mlx4_wqe_inline_seg *) wqe)->byte_count =
  1095. cpu_to_be32((1 << 31) | 4);
  1096. ((u32 *) wqe)[1] = 0;
  1097. wqe += sizeof (struct mlx4_wqe_data_seg);
  1098. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1099. }
  1100. ctrl->fence_size = (wr->send_flags & IB_SEND_FENCE ?
  1101. MLX4_WQE_CTRL_FENCE : 0) | size;
  1102. /*
  1103. * Make sure descriptor is fully written before
  1104. * setting ownership bit (because HW can start
  1105. * executing as soon as we do).
  1106. */
  1107. wmb();
  1108. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  1109. err = -EINVAL;
  1110. goto out;
  1111. }
  1112. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  1113. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0);
  1114. /*
  1115. * We can improve latency by not stamping the last
  1116. * send queue WQE until after ringing the doorbell, so
  1117. * only stamp here if there are still more WQEs to post.
  1118. */
  1119. if (wr->next)
  1120. stamp_send_wqe(qp, (ind + qp->sq_spare_wqes) &
  1121. (qp->sq.wqe_cnt - 1));
  1122. ++ind;
  1123. }
  1124. out:
  1125. if (likely(nreq)) {
  1126. qp->sq.head += nreq;
  1127. /*
  1128. * Make sure that descriptors are written before
  1129. * doorbell record.
  1130. */
  1131. wmb();
  1132. writel(qp->doorbell_qpn,
  1133. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  1134. /*
  1135. * Make sure doorbells don't leak out of SQ spinlock
  1136. * and reach the HCA out of order.
  1137. */
  1138. mmiowb();
  1139. stamp_send_wqe(qp, (ind + qp->sq_spare_wqes - 1) &
  1140. (qp->sq.wqe_cnt - 1));
  1141. }
  1142. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1143. return err;
  1144. }
  1145. int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1146. struct ib_recv_wr **bad_wr)
  1147. {
  1148. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1149. struct mlx4_wqe_data_seg *scat;
  1150. unsigned long flags;
  1151. int err = 0;
  1152. int nreq;
  1153. int ind;
  1154. int i;
  1155. spin_lock_irqsave(&qp->rq.lock, flags);
  1156. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  1157. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1158. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.send_cq)) {
  1159. err = -ENOMEM;
  1160. *bad_wr = wr;
  1161. goto out;
  1162. }
  1163. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  1164. err = -EINVAL;
  1165. *bad_wr = wr;
  1166. goto out;
  1167. }
  1168. scat = get_recv_wqe(qp, ind);
  1169. for (i = 0; i < wr->num_sge; ++i) {
  1170. scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
  1171. scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey);
  1172. scat[i].addr = cpu_to_be64(wr->sg_list[i].addr);
  1173. }
  1174. if (i < qp->rq.max_gs) {
  1175. scat[i].byte_count = 0;
  1176. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  1177. scat[i].addr = 0;
  1178. }
  1179. qp->rq.wrid[ind] = wr->wr_id;
  1180. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  1181. }
  1182. out:
  1183. if (likely(nreq)) {
  1184. qp->rq.head += nreq;
  1185. /*
  1186. * Make sure that descriptors are written before
  1187. * doorbell record.
  1188. */
  1189. wmb();
  1190. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  1191. }
  1192. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1193. return err;
  1194. }