qp.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  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. /* We don't support inline sends for kernel QPs (yet) */
  248. cap->max_inline_data = 0;
  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. }
  336. err = mlx4_qp_alloc(dev->dev, sqpn, &qp->mqp);
  337. if (err)
  338. goto err_wrid;
  339. /*
  340. * Hardware wants QPN written in big-endian order (after
  341. * shifting) for send doorbell. Precompute this value to save
  342. * a little bit when posting sends.
  343. */
  344. qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
  345. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  346. qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  347. else
  348. qp->sq_signal_bits = 0;
  349. qp->mqp.event = mlx4_ib_qp_event;
  350. return 0;
  351. err_wrid:
  352. if (pd->uobject && !init_attr->srq)
  353. mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &qp->db);
  354. else {
  355. kfree(qp->sq.wrid);
  356. kfree(qp->rq.wrid);
  357. }
  358. err_mtt:
  359. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  360. err_buf:
  361. if (pd->uobject)
  362. ib_umem_release(qp->umem);
  363. else
  364. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  365. err_db:
  366. if (!pd->uobject && !init_attr->srq)
  367. mlx4_ib_db_free(dev, &qp->db);
  368. err:
  369. return err;
  370. }
  371. static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
  372. {
  373. switch (state) {
  374. case IB_QPS_RESET: return MLX4_QP_STATE_RST;
  375. case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
  376. case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
  377. case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
  378. case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
  379. case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
  380. case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
  381. default: return -1;
  382. }
  383. }
  384. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  385. {
  386. if (send_cq == recv_cq)
  387. spin_lock_irq(&send_cq->lock);
  388. else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  389. spin_lock_irq(&send_cq->lock);
  390. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  391. } else {
  392. spin_lock_irq(&recv_cq->lock);
  393. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  394. }
  395. }
  396. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  397. {
  398. if (send_cq == recv_cq)
  399. spin_unlock_irq(&send_cq->lock);
  400. else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  401. spin_unlock(&recv_cq->lock);
  402. spin_unlock_irq(&send_cq->lock);
  403. } else {
  404. spin_unlock(&send_cq->lock);
  405. spin_unlock_irq(&recv_cq->lock);
  406. }
  407. }
  408. static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
  409. int is_user)
  410. {
  411. struct mlx4_ib_cq *send_cq, *recv_cq;
  412. if (qp->state != IB_QPS_RESET)
  413. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  414. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  415. printk(KERN_WARNING "mlx4_ib: modify QP %06x to RESET failed.\n",
  416. qp->mqp.qpn);
  417. send_cq = to_mcq(qp->ibqp.send_cq);
  418. recv_cq = to_mcq(qp->ibqp.recv_cq);
  419. mlx4_ib_lock_cqs(send_cq, recv_cq);
  420. if (!is_user) {
  421. __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  422. qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
  423. if (send_cq != recv_cq)
  424. __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  425. }
  426. mlx4_qp_remove(dev->dev, &qp->mqp);
  427. mlx4_ib_unlock_cqs(send_cq, recv_cq);
  428. mlx4_qp_free(dev->dev, &qp->mqp);
  429. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  430. if (is_user) {
  431. if (!qp->ibqp.srq)
  432. mlx4_ib_db_unmap_user(to_mucontext(qp->ibqp.uobject->context),
  433. &qp->db);
  434. ib_umem_release(qp->umem);
  435. } else {
  436. kfree(qp->sq.wrid);
  437. kfree(qp->rq.wrid);
  438. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  439. if (!qp->ibqp.srq)
  440. mlx4_ib_db_free(dev, &qp->db);
  441. }
  442. }
  443. struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
  444. struct ib_qp_init_attr *init_attr,
  445. struct ib_udata *udata)
  446. {
  447. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  448. struct mlx4_ib_sqp *sqp;
  449. struct mlx4_ib_qp *qp;
  450. int err;
  451. switch (init_attr->qp_type) {
  452. case IB_QPT_RC:
  453. case IB_QPT_UC:
  454. case IB_QPT_UD:
  455. {
  456. qp = kmalloc(sizeof *qp, GFP_KERNEL);
  457. if (!qp)
  458. return ERR_PTR(-ENOMEM);
  459. err = create_qp_common(dev, pd, init_attr, udata, 0, qp);
  460. if (err) {
  461. kfree(qp);
  462. return ERR_PTR(err);
  463. }
  464. qp->ibqp.qp_num = qp->mqp.qpn;
  465. break;
  466. }
  467. case IB_QPT_SMI:
  468. case IB_QPT_GSI:
  469. {
  470. /* Userspace is not allowed to create special QPs: */
  471. if (pd->uobject)
  472. return ERR_PTR(-EINVAL);
  473. sqp = kmalloc(sizeof *sqp, GFP_KERNEL);
  474. if (!sqp)
  475. return ERR_PTR(-ENOMEM);
  476. qp = &sqp->qp;
  477. err = create_qp_common(dev, pd, init_attr, udata,
  478. dev->dev->caps.sqp_start +
  479. (init_attr->qp_type == IB_QPT_SMI ? 0 : 2) +
  480. init_attr->port_num - 1,
  481. qp);
  482. if (err) {
  483. kfree(sqp);
  484. return ERR_PTR(err);
  485. }
  486. qp->port = init_attr->port_num;
  487. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  488. break;
  489. }
  490. default:
  491. /* Don't support raw QPs */
  492. return ERR_PTR(-EINVAL);
  493. }
  494. return &qp->ibqp;
  495. }
  496. int mlx4_ib_destroy_qp(struct ib_qp *qp)
  497. {
  498. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  499. struct mlx4_ib_qp *mqp = to_mqp(qp);
  500. if (is_qp0(dev, mqp))
  501. mlx4_CLOSE_PORT(dev->dev, mqp->port);
  502. destroy_qp_common(dev, mqp, !!qp->pd->uobject);
  503. if (is_sqp(dev, mqp))
  504. kfree(to_msqp(mqp));
  505. else
  506. kfree(mqp);
  507. return 0;
  508. }
  509. static void init_port(struct mlx4_ib_dev *dev, int port)
  510. {
  511. struct mlx4_init_port_param param;
  512. int err;
  513. memset(&param, 0, sizeof param);
  514. param.port_width_cap = dev->dev->caps.port_width_cap;
  515. param.vl_cap = dev->dev->caps.vl_cap;
  516. param.mtu = ib_mtu_enum_to_int(dev->dev->caps.mtu_cap);
  517. param.max_gid = dev->dev->caps.gid_table_len;
  518. param.max_pkey = dev->dev->caps.pkey_table_len;
  519. err = mlx4_INIT_PORT(dev->dev, &param, port);
  520. if (err)
  521. printk(KERN_WARNING "INIT_PORT failed, return code %d.\n", err);
  522. }
  523. static int to_mlx4_st(enum ib_qp_type type)
  524. {
  525. switch (type) {
  526. case IB_QPT_RC: return MLX4_QP_ST_RC;
  527. case IB_QPT_UC: return MLX4_QP_ST_UC;
  528. case IB_QPT_UD: return MLX4_QP_ST_UD;
  529. case IB_QPT_SMI:
  530. case IB_QPT_GSI: return MLX4_QP_ST_MLX;
  531. default: return -1;
  532. }
  533. }
  534. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  535. int attr_mask)
  536. {
  537. u8 dest_rd_atomic;
  538. u32 access_flags;
  539. u32 hw_access_flags = 0;
  540. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  541. dest_rd_atomic = attr->max_dest_rd_atomic;
  542. else
  543. dest_rd_atomic = qp->resp_depth;
  544. if (attr_mask & IB_QP_ACCESS_FLAGS)
  545. access_flags = attr->qp_access_flags;
  546. else
  547. access_flags = qp->atomic_rd_en;
  548. if (!dest_rd_atomic)
  549. access_flags &= IB_ACCESS_REMOTE_WRITE;
  550. if (access_flags & IB_ACCESS_REMOTE_READ)
  551. hw_access_flags |= MLX4_QP_BIT_RRE;
  552. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  553. hw_access_flags |= MLX4_QP_BIT_RAE;
  554. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  555. hw_access_flags |= MLX4_QP_BIT_RWE;
  556. return cpu_to_be32(hw_access_flags);
  557. }
  558. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  559. int attr_mask)
  560. {
  561. if (attr_mask & IB_QP_PKEY_INDEX)
  562. sqp->pkey_index = attr->pkey_index;
  563. if (attr_mask & IB_QP_QKEY)
  564. sqp->qkey = attr->qkey;
  565. if (attr_mask & IB_QP_SQ_PSN)
  566. sqp->send_psn = attr->sq_psn;
  567. }
  568. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  569. {
  570. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  571. }
  572. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_ah_attr *ah,
  573. struct mlx4_qp_path *path, u8 port)
  574. {
  575. path->grh_mylmc = ah->src_path_bits & 0x7f;
  576. path->rlid = cpu_to_be16(ah->dlid);
  577. if (ah->static_rate) {
  578. path->static_rate = ah->static_rate + MLX4_STAT_RATE_OFFSET;
  579. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  580. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  581. --path->static_rate;
  582. } else
  583. path->static_rate = 0;
  584. path->counter_index = 0xff;
  585. if (ah->ah_flags & IB_AH_GRH) {
  586. if (ah->grh.sgid_index >= dev->dev->caps.gid_table_len) {
  587. printk(KERN_ERR "sgid_index (%u) too large. max is %d\n",
  588. ah->grh.sgid_index, dev->dev->caps.gid_table_len - 1);
  589. return -1;
  590. }
  591. path->grh_mylmc |= 1 << 7;
  592. path->mgid_index = ah->grh.sgid_index;
  593. path->hop_limit = ah->grh.hop_limit;
  594. path->tclass_flowlabel =
  595. cpu_to_be32((ah->grh.traffic_class << 20) |
  596. (ah->grh.flow_label));
  597. memcpy(path->rgid, ah->grh.dgid.raw, 16);
  598. }
  599. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  600. ((port - 1) << 6) | ((ah->sl & 0xf) << 2);
  601. return 0;
  602. }
  603. static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
  604. const struct ib_qp_attr *attr, int attr_mask,
  605. enum ib_qp_state cur_state, enum ib_qp_state new_state)
  606. {
  607. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  608. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  609. struct mlx4_qp_context *context;
  610. enum mlx4_qp_optpar optpar = 0;
  611. int sqd_event;
  612. int err = -EINVAL;
  613. context = kzalloc(sizeof *context, GFP_KERNEL);
  614. if (!context)
  615. return -ENOMEM;
  616. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  617. (to_mlx4_st(ibqp->qp_type) << 16));
  618. context->flags |= cpu_to_be32(1 << 8); /* DE? */
  619. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  620. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  621. else {
  622. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  623. switch (attr->path_mig_state) {
  624. case IB_MIG_MIGRATED:
  625. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  626. break;
  627. case IB_MIG_REARM:
  628. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  629. break;
  630. case IB_MIG_ARMED:
  631. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  632. break;
  633. }
  634. }
  635. if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  636. ibqp->qp_type == IB_QPT_UD)
  637. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  638. else if (attr_mask & IB_QP_PATH_MTU) {
  639. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  640. printk(KERN_ERR "path MTU (%u) is invalid\n",
  641. attr->path_mtu);
  642. return -EINVAL;
  643. }
  644. context->mtu_msgmax = (attr->path_mtu << 5) | 31;
  645. }
  646. if (qp->rq.wqe_cnt)
  647. context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
  648. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  649. if (qp->sq.wqe_cnt)
  650. context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
  651. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  652. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  653. context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
  654. if (qp->ibqp.uobject)
  655. context->usr_page = cpu_to_be32(to_mucontext(ibqp->uobject->context)->uar.index);
  656. else
  657. context->usr_page = cpu_to_be32(dev->priv_uar.index);
  658. if (attr_mask & IB_QP_DEST_QPN)
  659. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  660. if (attr_mask & IB_QP_PORT) {
  661. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  662. !(attr_mask & IB_QP_AV)) {
  663. mlx4_set_sched(&context->pri_path, attr->port_num);
  664. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  665. }
  666. }
  667. if (attr_mask & IB_QP_PKEY_INDEX) {
  668. context->pri_path.pkey_index = attr->pkey_index;
  669. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  670. }
  671. if (attr_mask & IB_QP_AV) {
  672. if (mlx4_set_path(dev, &attr->ah_attr, &context->pri_path,
  673. attr_mask & IB_QP_PORT ? attr->port_num : qp->port)) {
  674. err = -EINVAL;
  675. goto out;
  676. }
  677. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  678. MLX4_QP_OPTPAR_SCHED_QUEUE);
  679. }
  680. if (attr_mask & IB_QP_TIMEOUT) {
  681. context->pri_path.ackto = attr->timeout << 3;
  682. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  683. }
  684. if (attr_mask & IB_QP_ALT_PATH) {
  685. if (attr->alt_pkey_index >= dev->dev->caps.pkey_table_len)
  686. return -EINVAL;
  687. if (attr->alt_port_num == 0 ||
  688. attr->alt_port_num > dev->dev->caps.num_ports)
  689. return -EINVAL;
  690. if (mlx4_set_path(dev, &attr->alt_ah_attr, &context->alt_path,
  691. attr->alt_port_num))
  692. return -EINVAL;
  693. context->alt_path.pkey_index = attr->alt_pkey_index;
  694. context->alt_path.ackto = attr->alt_timeout << 3;
  695. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  696. }
  697. context->pd = cpu_to_be32(to_mpd(ibqp->pd)->pdn);
  698. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  699. if (attr_mask & IB_QP_RNR_RETRY) {
  700. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  701. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  702. }
  703. if (attr_mask & IB_QP_RETRY_CNT) {
  704. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  705. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  706. }
  707. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  708. if (attr->max_rd_atomic)
  709. context->params1 |=
  710. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  711. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  712. }
  713. if (attr_mask & IB_QP_SQ_PSN)
  714. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  715. context->cqn_send = cpu_to_be32(to_mcq(ibqp->send_cq)->mcq.cqn);
  716. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  717. if (attr->max_dest_rd_atomic)
  718. context->params2 |=
  719. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  720. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  721. }
  722. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  723. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  724. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  725. }
  726. if (ibqp->srq)
  727. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  728. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  729. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  730. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  731. }
  732. if (attr_mask & IB_QP_RQ_PSN)
  733. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  734. context->cqn_recv = cpu_to_be32(to_mcq(ibqp->recv_cq)->mcq.cqn);
  735. if (attr_mask & IB_QP_QKEY) {
  736. context->qkey = cpu_to_be32(attr->qkey);
  737. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  738. }
  739. if (ibqp->srq)
  740. context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
  741. if (!ibqp->srq && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  742. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  743. if (cur_state == IB_QPS_INIT &&
  744. new_state == IB_QPS_RTR &&
  745. (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  746. ibqp->qp_type == IB_QPT_UD)) {
  747. context->pri_path.sched_queue = (qp->port - 1) << 6;
  748. if (is_qp0(dev, qp))
  749. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  750. else
  751. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  752. }
  753. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  754. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  755. sqd_event = 1;
  756. else
  757. sqd_event = 0;
  758. /*
  759. * Before passing a kernel QP to the HW, make sure that the
  760. * ownership bits of the send queue are set and the SQ
  761. * headroom is stamped so that the hardware doesn't start
  762. * processing stale work requests.
  763. */
  764. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  765. struct mlx4_wqe_ctrl_seg *ctrl;
  766. int i;
  767. for (i = 0; i < qp->sq.wqe_cnt; ++i) {
  768. ctrl = get_send_wqe(qp, i);
  769. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  770. stamp_send_wqe(qp, i);
  771. }
  772. }
  773. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  774. to_mlx4_state(new_state), context, optpar,
  775. sqd_event, &qp->mqp);
  776. if (err)
  777. goto out;
  778. qp->state = new_state;
  779. if (attr_mask & IB_QP_ACCESS_FLAGS)
  780. qp->atomic_rd_en = attr->qp_access_flags;
  781. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  782. qp->resp_depth = attr->max_dest_rd_atomic;
  783. if (attr_mask & IB_QP_PORT)
  784. qp->port = attr->port_num;
  785. if (attr_mask & IB_QP_ALT_PATH)
  786. qp->alt_port = attr->alt_port_num;
  787. if (is_sqp(dev, qp))
  788. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  789. /*
  790. * If we moved QP0 to RTR, bring the IB link up; if we moved
  791. * QP0 to RESET or ERROR, bring the link back down.
  792. */
  793. if (is_qp0(dev, qp)) {
  794. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  795. init_port(dev, qp->port);
  796. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  797. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  798. mlx4_CLOSE_PORT(dev->dev, qp->port);
  799. }
  800. /*
  801. * If we moved a kernel QP to RESET, clean up all old CQ
  802. * entries and reinitialize the QP.
  803. */
  804. if (new_state == IB_QPS_RESET && !ibqp->uobject) {
  805. mlx4_ib_cq_clean(to_mcq(ibqp->recv_cq), qp->mqp.qpn,
  806. ibqp->srq ? to_msrq(ibqp->srq): NULL);
  807. if (ibqp->send_cq != ibqp->recv_cq)
  808. mlx4_ib_cq_clean(to_mcq(ibqp->send_cq), qp->mqp.qpn, NULL);
  809. qp->rq.head = 0;
  810. qp->rq.tail = 0;
  811. qp->sq.head = 0;
  812. qp->sq.tail = 0;
  813. if (!ibqp->srq)
  814. *qp->db.db = 0;
  815. }
  816. out:
  817. kfree(context);
  818. return err;
  819. }
  820. static const struct ib_qp_attr mlx4_ib_qp_attr = { .port_num = 1 };
  821. static const int mlx4_ib_qp_attr_mask_table[IB_QPT_UD + 1] = {
  822. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  823. IB_QP_PORT |
  824. IB_QP_QKEY),
  825. [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
  826. IB_QP_PORT |
  827. IB_QP_ACCESS_FLAGS),
  828. [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
  829. IB_QP_PORT |
  830. IB_QP_ACCESS_FLAGS),
  831. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  832. IB_QP_QKEY),
  833. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  834. IB_QP_QKEY),
  835. };
  836. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  837. int attr_mask, struct ib_udata *udata)
  838. {
  839. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  840. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  841. enum ib_qp_state cur_state, new_state;
  842. int err = -EINVAL;
  843. mutex_lock(&qp->mutex);
  844. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  845. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  846. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask))
  847. goto out;
  848. if ((attr_mask & IB_QP_PKEY_INDEX) &&
  849. attr->pkey_index >= dev->dev->caps.pkey_table_len) {
  850. goto out;
  851. }
  852. if ((attr_mask & IB_QP_PORT) &&
  853. (attr->port_num == 0 || attr->port_num > dev->dev->caps.num_ports)) {
  854. goto out;
  855. }
  856. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  857. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  858. goto out;
  859. }
  860. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  861. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  862. goto out;
  863. }
  864. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  865. err = 0;
  866. goto out;
  867. }
  868. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_ERR) {
  869. err = __mlx4_ib_modify_qp(ibqp, &mlx4_ib_qp_attr,
  870. mlx4_ib_qp_attr_mask_table[ibqp->qp_type],
  871. IB_QPS_RESET, IB_QPS_INIT);
  872. if (err)
  873. goto out;
  874. cur_state = IB_QPS_INIT;
  875. }
  876. err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
  877. out:
  878. mutex_unlock(&qp->mutex);
  879. return err;
  880. }
  881. static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_send_wr *wr,
  882. void *wqe)
  883. {
  884. struct ib_device *ib_dev = &to_mdev(sqp->qp.ibqp.device)->ib_dev;
  885. struct mlx4_wqe_mlx_seg *mlx = wqe;
  886. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  887. struct mlx4_ib_ah *ah = to_mah(wr->wr.ud.ah);
  888. u16 pkey;
  889. int send_size;
  890. int header_size;
  891. int i;
  892. send_size = 0;
  893. for (i = 0; i < wr->num_sge; ++i)
  894. send_size += wr->sg_list[i].length;
  895. ib_ud_header_init(send_size, mlx4_ib_ah_grh_present(ah), &sqp->ud_header);
  896. sqp->ud_header.lrh.service_level =
  897. be32_to_cpu(ah->av.sl_tclass_flowlabel) >> 28;
  898. sqp->ud_header.lrh.destination_lid = ah->av.dlid;
  899. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.g_slid & 0x7f);
  900. if (mlx4_ib_ah_grh_present(ah)) {
  901. sqp->ud_header.grh.traffic_class =
  902. (be32_to_cpu(ah->av.sl_tclass_flowlabel) >> 20) & 0xff;
  903. sqp->ud_header.grh.flow_label =
  904. ah->av.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  905. sqp->ud_header.grh.hop_limit = ah->av.hop_limit;
  906. ib_get_cached_gid(ib_dev, be32_to_cpu(ah->av.port_pd) >> 24,
  907. ah->av.gid_index, &sqp->ud_header.grh.source_gid);
  908. memcpy(sqp->ud_header.grh.destination_gid.raw,
  909. ah->av.dgid, 16);
  910. }
  911. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  912. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  913. (sqp->ud_header.lrh.destination_lid ==
  914. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  915. (sqp->ud_header.lrh.service_level << 8));
  916. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  917. switch (wr->opcode) {
  918. case IB_WR_SEND:
  919. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  920. sqp->ud_header.immediate_present = 0;
  921. break;
  922. case IB_WR_SEND_WITH_IMM:
  923. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  924. sqp->ud_header.immediate_present = 1;
  925. sqp->ud_header.immediate_data = wr->imm_data;
  926. break;
  927. default:
  928. return -EINVAL;
  929. }
  930. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
  931. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  932. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  933. sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
  934. if (!sqp->qp.ibqp.qp_num)
  935. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  936. else
  937. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->wr.ud.pkey_index, &pkey);
  938. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  939. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  940. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  941. sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
  942. sqp->qkey : wr->wr.ud.remote_qkey);
  943. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  944. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  945. if (0) {
  946. printk(KERN_ERR "built UD header of size %d:\n", header_size);
  947. for (i = 0; i < header_size / 4; ++i) {
  948. if (i % 8 == 0)
  949. printk(" [%02x] ", i * 4);
  950. printk(" %08x",
  951. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  952. if ((i + 1) % 8 == 0)
  953. printk("\n");
  954. }
  955. printk("\n");
  956. }
  957. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  958. memcpy(inl + 1, sqp->header_buf, header_size);
  959. return ALIGN(sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  960. }
  961. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  962. {
  963. unsigned cur;
  964. struct mlx4_ib_cq *cq;
  965. cur = wq->head - wq->tail;
  966. if (likely(cur + nreq < wq->max_post))
  967. return 0;
  968. cq = to_mcq(ib_cq);
  969. spin_lock(&cq->lock);
  970. cur = wq->head - wq->tail;
  971. spin_unlock(&cq->lock);
  972. return cur + nreq >= wq->max_post;
  973. }
  974. int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  975. struct ib_send_wr **bad_wr)
  976. {
  977. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  978. void *wqe;
  979. struct mlx4_wqe_ctrl_seg *ctrl;
  980. unsigned long flags;
  981. int nreq;
  982. int err = 0;
  983. int ind;
  984. int size;
  985. int i;
  986. spin_lock_irqsave(&qp->rq.lock, flags);
  987. ind = qp->sq.head;
  988. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  989. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  990. err = -ENOMEM;
  991. *bad_wr = wr;
  992. goto out;
  993. }
  994. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  995. err = -EINVAL;
  996. *bad_wr = wr;
  997. goto out;
  998. }
  999. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  1000. qp->sq.wrid[ind & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  1001. ctrl->srcrb_flags =
  1002. (wr->send_flags & IB_SEND_SIGNALED ?
  1003. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  1004. (wr->send_flags & IB_SEND_SOLICITED ?
  1005. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  1006. qp->sq_signal_bits;
  1007. if (wr->opcode == IB_WR_SEND_WITH_IMM ||
  1008. wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  1009. ctrl->imm = wr->imm_data;
  1010. else
  1011. ctrl->imm = 0;
  1012. wqe += sizeof *ctrl;
  1013. size = sizeof *ctrl / 16;
  1014. switch (ibqp->qp_type) {
  1015. case IB_QPT_RC:
  1016. case IB_QPT_UC:
  1017. switch (wr->opcode) {
  1018. case IB_WR_ATOMIC_CMP_AND_SWP:
  1019. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1020. ((struct mlx4_wqe_raddr_seg *) wqe)->raddr =
  1021. cpu_to_be64(wr->wr.atomic.remote_addr);
  1022. ((struct mlx4_wqe_raddr_seg *) wqe)->rkey =
  1023. cpu_to_be32(wr->wr.atomic.rkey);
  1024. ((struct mlx4_wqe_raddr_seg *) wqe)->reserved = 0;
  1025. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1026. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  1027. ((struct mlx4_wqe_atomic_seg *) wqe)->swap_add =
  1028. cpu_to_be64(wr->wr.atomic.swap);
  1029. ((struct mlx4_wqe_atomic_seg *) wqe)->compare =
  1030. cpu_to_be64(wr->wr.atomic.compare_add);
  1031. } else {
  1032. ((struct mlx4_wqe_atomic_seg *) wqe)->swap_add =
  1033. cpu_to_be64(wr->wr.atomic.compare_add);
  1034. ((struct mlx4_wqe_atomic_seg *) wqe)->compare = 0;
  1035. }
  1036. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  1037. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  1038. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  1039. break;
  1040. case IB_WR_RDMA_READ:
  1041. case IB_WR_RDMA_WRITE:
  1042. case IB_WR_RDMA_WRITE_WITH_IMM:
  1043. ((struct mlx4_wqe_raddr_seg *) wqe)->raddr =
  1044. cpu_to_be64(wr->wr.rdma.remote_addr);
  1045. ((struct mlx4_wqe_raddr_seg *) wqe)->rkey =
  1046. cpu_to_be32(wr->wr.rdma.rkey);
  1047. ((struct mlx4_wqe_raddr_seg *) wqe)->reserved = 0;
  1048. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1049. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  1050. break;
  1051. default:
  1052. /* No extra segments required for sends */
  1053. break;
  1054. }
  1055. break;
  1056. case IB_QPT_UD:
  1057. memcpy(((struct mlx4_wqe_datagram_seg *) wqe)->av,
  1058. &to_mah(wr->wr.ud.ah)->av, sizeof (struct mlx4_av));
  1059. ((struct mlx4_wqe_datagram_seg *) wqe)->dqpn =
  1060. cpu_to_be32(wr->wr.ud.remote_qpn);
  1061. ((struct mlx4_wqe_datagram_seg *) wqe)->qkey =
  1062. cpu_to_be32(wr->wr.ud.remote_qkey);
  1063. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  1064. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  1065. break;
  1066. case IB_QPT_SMI:
  1067. case IB_QPT_GSI:
  1068. err = build_mlx_header(to_msqp(qp), wr, ctrl);
  1069. if (err < 0) {
  1070. *bad_wr = wr;
  1071. goto out;
  1072. }
  1073. wqe += err;
  1074. size += err / 16;
  1075. err = 0;
  1076. break;
  1077. default:
  1078. break;
  1079. }
  1080. for (i = 0; i < wr->num_sge; ++i) {
  1081. ((struct mlx4_wqe_data_seg *) wqe)->byte_count =
  1082. cpu_to_be32(wr->sg_list[i].length);
  1083. ((struct mlx4_wqe_data_seg *) wqe)->lkey =
  1084. cpu_to_be32(wr->sg_list[i].lkey);
  1085. ((struct mlx4_wqe_data_seg *) wqe)->addr =
  1086. cpu_to_be64(wr->sg_list[i].addr);
  1087. wqe += sizeof (struct mlx4_wqe_data_seg);
  1088. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1089. }
  1090. /* Add one more inline data segment for ICRC for MLX sends */
  1091. if (qp->ibqp.qp_type == IB_QPT_SMI || qp->ibqp.qp_type == IB_QPT_GSI) {
  1092. ((struct mlx4_wqe_inline_seg *) wqe)->byte_count =
  1093. cpu_to_be32((1 << 31) | 4);
  1094. ((u32 *) wqe)[1] = 0;
  1095. wqe += sizeof (struct mlx4_wqe_data_seg);
  1096. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1097. }
  1098. ctrl->fence_size = (wr->send_flags & IB_SEND_FENCE ?
  1099. MLX4_WQE_CTRL_FENCE : 0) | size;
  1100. /*
  1101. * Make sure descriptor is fully written before
  1102. * setting ownership bit (because HW can start
  1103. * executing as soon as we do).
  1104. */
  1105. wmb();
  1106. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  1107. err = -EINVAL;
  1108. goto out;
  1109. }
  1110. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  1111. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0);
  1112. /*
  1113. * We can improve latency by not stamping the last
  1114. * send queue WQE until after ringing the doorbell, so
  1115. * only stamp here if there are still more WQEs to post.
  1116. */
  1117. if (wr->next)
  1118. stamp_send_wqe(qp, (ind + qp->sq_spare_wqes) &
  1119. (qp->sq.wqe_cnt - 1));
  1120. ++ind;
  1121. }
  1122. out:
  1123. if (likely(nreq)) {
  1124. qp->sq.head += nreq;
  1125. /*
  1126. * Make sure that descriptors are written before
  1127. * doorbell record.
  1128. */
  1129. wmb();
  1130. writel(qp->doorbell_qpn,
  1131. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  1132. /*
  1133. * Make sure doorbells don't leak out of SQ spinlock
  1134. * and reach the HCA out of order.
  1135. */
  1136. mmiowb();
  1137. stamp_send_wqe(qp, (ind + qp->sq_spare_wqes - 1) &
  1138. (qp->sq.wqe_cnt - 1));
  1139. }
  1140. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1141. return err;
  1142. }
  1143. int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1144. struct ib_recv_wr **bad_wr)
  1145. {
  1146. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1147. struct mlx4_wqe_data_seg *scat;
  1148. unsigned long flags;
  1149. int err = 0;
  1150. int nreq;
  1151. int ind;
  1152. int i;
  1153. spin_lock_irqsave(&qp->rq.lock, flags);
  1154. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  1155. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1156. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.send_cq)) {
  1157. err = -ENOMEM;
  1158. *bad_wr = wr;
  1159. goto out;
  1160. }
  1161. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  1162. err = -EINVAL;
  1163. *bad_wr = wr;
  1164. goto out;
  1165. }
  1166. scat = get_recv_wqe(qp, ind);
  1167. for (i = 0; i < wr->num_sge; ++i) {
  1168. scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
  1169. scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey);
  1170. scat[i].addr = cpu_to_be64(wr->sg_list[i].addr);
  1171. }
  1172. if (i < qp->rq.max_gs) {
  1173. scat[i].byte_count = 0;
  1174. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  1175. scat[i].addr = 0;
  1176. }
  1177. qp->rq.wrid[ind] = wr->wr_id;
  1178. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  1179. }
  1180. out:
  1181. if (likely(nreq)) {
  1182. qp->rq.head += nreq;
  1183. /*
  1184. * Make sure that descriptors are written before
  1185. * doorbell record.
  1186. */
  1187. wmb();
  1188. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  1189. }
  1190. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1191. return err;
  1192. }