qp.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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. static void mlx4_ib_qp_event(struct mlx4_qp *qp, enum mlx4_event type)
  98. {
  99. struct ib_event event;
  100. struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
  101. if (type == MLX4_EVENT_TYPE_PATH_MIG)
  102. to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
  103. if (ibqp->event_handler) {
  104. event.device = ibqp->device;
  105. event.element.qp = ibqp;
  106. switch (type) {
  107. case MLX4_EVENT_TYPE_PATH_MIG:
  108. event.event = IB_EVENT_PATH_MIG;
  109. break;
  110. case MLX4_EVENT_TYPE_COMM_EST:
  111. event.event = IB_EVENT_COMM_EST;
  112. break;
  113. case MLX4_EVENT_TYPE_SQ_DRAINED:
  114. event.event = IB_EVENT_SQ_DRAINED;
  115. break;
  116. case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
  117. event.event = IB_EVENT_QP_LAST_WQE_REACHED;
  118. break;
  119. case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
  120. event.event = IB_EVENT_QP_FATAL;
  121. break;
  122. case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
  123. event.event = IB_EVENT_PATH_MIG_ERR;
  124. break;
  125. case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  126. event.event = IB_EVENT_QP_REQ_ERR;
  127. break;
  128. case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
  129. event.event = IB_EVENT_QP_ACCESS_ERR;
  130. break;
  131. default:
  132. printk(KERN_WARNING "mlx4_ib: Unexpected event type %d "
  133. "on QP %06x\n", type, qp->qpn);
  134. return;
  135. }
  136. ibqp->event_handler(&event, ibqp->qp_context);
  137. }
  138. }
  139. static int send_wqe_overhead(enum ib_qp_type type)
  140. {
  141. /*
  142. * UD WQEs must have a datagram segment.
  143. * RC and UC WQEs might have a remote address segment.
  144. * MLX WQEs need two extra inline data segments (for the UD
  145. * header and space for the ICRC).
  146. */
  147. switch (type) {
  148. case IB_QPT_UD:
  149. return sizeof (struct mlx4_wqe_ctrl_seg) +
  150. sizeof (struct mlx4_wqe_datagram_seg);
  151. case IB_QPT_UC:
  152. return sizeof (struct mlx4_wqe_ctrl_seg) +
  153. sizeof (struct mlx4_wqe_raddr_seg);
  154. case IB_QPT_RC:
  155. return sizeof (struct mlx4_wqe_ctrl_seg) +
  156. sizeof (struct mlx4_wqe_atomic_seg) +
  157. sizeof (struct mlx4_wqe_raddr_seg);
  158. case IB_QPT_SMI:
  159. case IB_QPT_GSI:
  160. return sizeof (struct mlx4_wqe_ctrl_seg) +
  161. ALIGN(MLX4_IB_UD_HEADER_SIZE +
  162. sizeof (struct mlx4_wqe_inline_seg),
  163. sizeof (struct mlx4_wqe_data_seg)) +
  164. ALIGN(4 +
  165. sizeof (struct mlx4_wqe_inline_seg),
  166. sizeof (struct mlx4_wqe_data_seg));
  167. default:
  168. return sizeof (struct mlx4_wqe_ctrl_seg);
  169. }
  170. }
  171. static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  172. struct mlx4_ib_qp *qp)
  173. {
  174. /* Sanity check RQ size before proceeding */
  175. if (cap->max_recv_wr > dev->dev->caps.max_wqes ||
  176. cap->max_recv_sge > dev->dev->caps.max_rq_sg)
  177. return -EINVAL;
  178. qp->rq.max = cap->max_recv_wr ? roundup_pow_of_two(cap->max_recv_wr) : 0;
  179. qp->rq.wqe_shift = ilog2(roundup_pow_of_two(cap->max_recv_sge *
  180. sizeof (struct mlx4_wqe_data_seg)));
  181. qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof (struct mlx4_wqe_data_seg);
  182. cap->max_recv_wr = qp->rq.max;
  183. cap->max_recv_sge = qp->rq.max_gs;
  184. return 0;
  185. }
  186. static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  187. enum ib_qp_type type, struct mlx4_ib_qp *qp)
  188. {
  189. /* Sanity check SQ size before proceeding */
  190. if (cap->max_send_wr > dev->dev->caps.max_wqes ||
  191. cap->max_send_sge > dev->dev->caps.max_sq_sg ||
  192. cap->max_inline_data + send_wqe_overhead(type) +
  193. sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
  194. return -EINVAL;
  195. /*
  196. * For MLX transport we need 2 extra S/G entries:
  197. * one for the header and one for the checksum at the end
  198. */
  199. if ((type == IB_QPT_SMI || type == IB_QPT_GSI) &&
  200. cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
  201. return -EINVAL;
  202. qp->sq.max = cap->max_send_wr ? roundup_pow_of_two(cap->max_send_wr) : 1;
  203. qp->sq.wqe_shift = ilog2(roundup_pow_of_two(max(cap->max_send_sge *
  204. sizeof (struct mlx4_wqe_data_seg),
  205. cap->max_inline_data +
  206. sizeof (struct mlx4_wqe_inline_seg)) +
  207. send_wqe_overhead(type)));
  208. qp->sq.max_gs = ((1 << qp->sq.wqe_shift) - send_wqe_overhead(type)) /
  209. sizeof (struct mlx4_wqe_data_seg);
  210. qp->buf_size = (qp->rq.max << qp->rq.wqe_shift) +
  211. (qp->sq.max << qp->sq.wqe_shift);
  212. if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
  213. qp->rq.offset = 0;
  214. qp->sq.offset = qp->rq.max << qp->rq.wqe_shift;
  215. } else {
  216. qp->rq.offset = qp->sq.max << qp->sq.wqe_shift;
  217. qp->sq.offset = 0;
  218. }
  219. cap->max_send_wr = qp->sq.max;
  220. cap->max_send_sge = qp->sq.max_gs;
  221. cap->max_inline_data = (1 << qp->sq.wqe_shift) - send_wqe_overhead(type) -
  222. sizeof (struct mlx4_wqe_inline_seg);
  223. return 0;
  224. }
  225. static int set_user_sq_size(struct mlx4_ib_qp *qp,
  226. struct mlx4_ib_create_qp *ucmd)
  227. {
  228. qp->sq.max = 1 << ucmd->log_sq_bb_count;
  229. qp->sq.wqe_shift = ucmd->log_sq_stride;
  230. qp->buf_size = (qp->rq.max << qp->rq.wqe_shift) +
  231. (qp->sq.max << qp->sq.wqe_shift);
  232. return 0;
  233. }
  234. static int create_qp_common(struct mlx4_ib_dev *dev, struct ib_pd *pd,
  235. struct ib_qp_init_attr *init_attr,
  236. struct ib_udata *udata, int sqpn, struct mlx4_ib_qp *qp)
  237. {
  238. struct mlx4_wqe_ctrl_seg *ctrl;
  239. int err;
  240. int i;
  241. mutex_init(&qp->mutex);
  242. spin_lock_init(&qp->sq.lock);
  243. spin_lock_init(&qp->rq.lock);
  244. qp->state = IB_QPS_RESET;
  245. qp->atomic_rd_en = 0;
  246. qp->resp_depth = 0;
  247. qp->rq.head = 0;
  248. qp->rq.tail = 0;
  249. qp->sq.head = 0;
  250. qp->sq.tail = 0;
  251. err = set_rq_size(dev, &init_attr->cap, qp);
  252. if (err)
  253. goto err;
  254. if (pd->uobject) {
  255. struct mlx4_ib_create_qp ucmd;
  256. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  257. err = -EFAULT;
  258. goto err;
  259. }
  260. err = set_user_sq_size(qp, &ucmd);
  261. if (err)
  262. goto err;
  263. qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
  264. qp->buf_size, 0);
  265. if (IS_ERR(qp->umem)) {
  266. err = PTR_ERR(qp->umem);
  267. goto err;
  268. }
  269. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(qp->umem),
  270. ilog2(qp->umem->page_size), &qp->mtt);
  271. if (err)
  272. goto err_buf;
  273. err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
  274. if (err)
  275. goto err_mtt;
  276. if (!init_attr->srq) {
  277. err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
  278. ucmd.db_addr, &qp->db);
  279. if (err)
  280. goto err_mtt;
  281. }
  282. } else {
  283. err = set_kernel_sq_size(dev, &init_attr->cap, init_attr->qp_type, qp);
  284. if (err)
  285. goto err;
  286. if (!init_attr->srq) {
  287. err = mlx4_ib_db_alloc(dev, &qp->db, 0);
  288. if (err)
  289. goto err;
  290. *qp->db.db = 0;
  291. }
  292. if (mlx4_buf_alloc(dev->dev, qp->buf_size, PAGE_SIZE * 2, &qp->buf)) {
  293. err = -ENOMEM;
  294. goto err_db;
  295. }
  296. err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
  297. &qp->mtt);
  298. if (err)
  299. goto err_buf;
  300. err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf);
  301. if (err)
  302. goto err_mtt;
  303. for (i = 0; i < qp->sq.max; ++i) {
  304. ctrl = get_send_wqe(qp, i);
  305. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  306. }
  307. qp->sq.wrid = kmalloc(qp->sq.max * sizeof (u64), GFP_KERNEL);
  308. qp->rq.wrid = kmalloc(qp->rq.max * sizeof (u64), GFP_KERNEL);
  309. if (!qp->sq.wrid || !qp->rq.wrid) {
  310. err = -ENOMEM;
  311. goto err_wrid;
  312. }
  313. /* We don't support inline sends for kernel QPs (yet) */
  314. init_attr->cap.max_inline_data = 0;
  315. }
  316. err = mlx4_qp_alloc(dev->dev, sqpn, &qp->mqp);
  317. if (err)
  318. goto err_wrid;
  319. /*
  320. * Hardware wants QPN written in big-endian order (after
  321. * shifting) for send doorbell. Precompute this value to save
  322. * a little bit when posting sends.
  323. */
  324. qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
  325. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  326. qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  327. else
  328. qp->sq_signal_bits = 0;
  329. qp->mqp.event = mlx4_ib_qp_event;
  330. return 0;
  331. err_wrid:
  332. if (pd->uobject && !init_attr->srq)
  333. mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &qp->db);
  334. else {
  335. kfree(qp->sq.wrid);
  336. kfree(qp->rq.wrid);
  337. }
  338. err_mtt:
  339. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  340. err_buf:
  341. if (pd->uobject)
  342. ib_umem_release(qp->umem);
  343. else
  344. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  345. err_db:
  346. if (!pd->uobject && !init_attr->srq)
  347. mlx4_ib_db_free(dev, &qp->db);
  348. err:
  349. return err;
  350. }
  351. static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
  352. {
  353. switch (state) {
  354. case IB_QPS_RESET: return MLX4_QP_STATE_RST;
  355. case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
  356. case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
  357. case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
  358. case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
  359. case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
  360. case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
  361. default: return -1;
  362. }
  363. }
  364. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  365. {
  366. if (send_cq == recv_cq)
  367. spin_lock_irq(&send_cq->lock);
  368. else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  369. spin_lock_irq(&send_cq->lock);
  370. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  371. } else {
  372. spin_lock_irq(&recv_cq->lock);
  373. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  374. }
  375. }
  376. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  377. {
  378. if (send_cq == recv_cq)
  379. spin_unlock_irq(&send_cq->lock);
  380. else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  381. spin_unlock(&recv_cq->lock);
  382. spin_unlock_irq(&send_cq->lock);
  383. } else {
  384. spin_unlock(&send_cq->lock);
  385. spin_unlock_irq(&recv_cq->lock);
  386. }
  387. }
  388. static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
  389. int is_user)
  390. {
  391. struct mlx4_ib_cq *send_cq, *recv_cq;
  392. if (qp->state != IB_QPS_RESET)
  393. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  394. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  395. printk(KERN_WARNING "mlx4_ib: modify QP %06x to RESET failed.\n",
  396. qp->mqp.qpn);
  397. send_cq = to_mcq(qp->ibqp.send_cq);
  398. recv_cq = to_mcq(qp->ibqp.recv_cq);
  399. mlx4_ib_lock_cqs(send_cq, recv_cq);
  400. if (!is_user) {
  401. __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  402. qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
  403. if (send_cq != recv_cq)
  404. __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  405. }
  406. mlx4_qp_remove(dev->dev, &qp->mqp);
  407. mlx4_ib_unlock_cqs(send_cq, recv_cq);
  408. mlx4_qp_free(dev->dev, &qp->mqp);
  409. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  410. if (is_user) {
  411. if (!qp->ibqp.srq)
  412. mlx4_ib_db_unmap_user(to_mucontext(qp->ibqp.uobject->context),
  413. &qp->db);
  414. ib_umem_release(qp->umem);
  415. } else {
  416. kfree(qp->sq.wrid);
  417. kfree(qp->rq.wrid);
  418. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  419. if (!qp->ibqp.srq)
  420. mlx4_ib_db_free(dev, &qp->db);
  421. }
  422. }
  423. struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
  424. struct ib_qp_init_attr *init_attr,
  425. struct ib_udata *udata)
  426. {
  427. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  428. struct mlx4_ib_sqp *sqp;
  429. struct mlx4_ib_qp *qp;
  430. int err;
  431. switch (init_attr->qp_type) {
  432. case IB_QPT_RC:
  433. case IB_QPT_UC:
  434. case IB_QPT_UD:
  435. {
  436. qp = kmalloc(sizeof *qp, GFP_KERNEL);
  437. if (!qp)
  438. return ERR_PTR(-ENOMEM);
  439. err = create_qp_common(dev, pd, init_attr, udata, 0, qp);
  440. if (err) {
  441. kfree(qp);
  442. return ERR_PTR(err);
  443. }
  444. qp->ibqp.qp_num = qp->mqp.qpn;
  445. break;
  446. }
  447. case IB_QPT_SMI:
  448. case IB_QPT_GSI:
  449. {
  450. /* Userspace is not allowed to create special QPs: */
  451. if (pd->uobject)
  452. return ERR_PTR(-EINVAL);
  453. sqp = kmalloc(sizeof *sqp, GFP_KERNEL);
  454. if (!sqp)
  455. return ERR_PTR(-ENOMEM);
  456. qp = &sqp->qp;
  457. err = create_qp_common(dev, pd, init_attr, udata,
  458. dev->dev->caps.sqp_start +
  459. (init_attr->qp_type == IB_QPT_SMI ? 0 : 2) +
  460. init_attr->port_num - 1,
  461. qp);
  462. if (err) {
  463. kfree(sqp);
  464. return ERR_PTR(err);
  465. }
  466. qp->port = init_attr->port_num;
  467. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  468. break;
  469. }
  470. default:
  471. /* Don't support raw QPs */
  472. return ERR_PTR(-EINVAL);
  473. }
  474. return &qp->ibqp;
  475. }
  476. int mlx4_ib_destroy_qp(struct ib_qp *qp)
  477. {
  478. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  479. struct mlx4_ib_qp *mqp = to_mqp(qp);
  480. if (is_qp0(dev, mqp))
  481. mlx4_CLOSE_PORT(dev->dev, mqp->port);
  482. destroy_qp_common(dev, mqp, !!qp->pd->uobject);
  483. if (is_sqp(dev, mqp))
  484. kfree(to_msqp(mqp));
  485. else
  486. kfree(mqp);
  487. return 0;
  488. }
  489. static void init_port(struct mlx4_ib_dev *dev, int port)
  490. {
  491. struct mlx4_init_port_param param;
  492. int err;
  493. memset(&param, 0, sizeof param);
  494. param.port_width_cap = dev->dev->caps.port_width_cap;
  495. param.vl_cap = dev->dev->caps.vl_cap;
  496. param.mtu = ib_mtu_enum_to_int(dev->dev->caps.mtu_cap);
  497. param.max_gid = dev->dev->caps.gid_table_len;
  498. param.max_pkey = dev->dev->caps.pkey_table_len;
  499. err = mlx4_INIT_PORT(dev->dev, &param, port);
  500. if (err)
  501. printk(KERN_WARNING "INIT_PORT failed, return code %d.\n", err);
  502. }
  503. static int to_mlx4_st(enum ib_qp_type type)
  504. {
  505. switch (type) {
  506. case IB_QPT_RC: return MLX4_QP_ST_RC;
  507. case IB_QPT_UC: return MLX4_QP_ST_UC;
  508. case IB_QPT_UD: return MLX4_QP_ST_UD;
  509. case IB_QPT_SMI:
  510. case IB_QPT_GSI: return MLX4_QP_ST_MLX;
  511. default: return -1;
  512. }
  513. }
  514. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  515. int attr_mask)
  516. {
  517. u8 dest_rd_atomic;
  518. u32 access_flags;
  519. u32 hw_access_flags = 0;
  520. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  521. dest_rd_atomic = attr->max_dest_rd_atomic;
  522. else
  523. dest_rd_atomic = qp->resp_depth;
  524. if (attr_mask & IB_QP_ACCESS_FLAGS)
  525. access_flags = attr->qp_access_flags;
  526. else
  527. access_flags = qp->atomic_rd_en;
  528. if (!dest_rd_atomic)
  529. access_flags &= IB_ACCESS_REMOTE_WRITE;
  530. if (access_flags & IB_ACCESS_REMOTE_READ)
  531. hw_access_flags |= MLX4_QP_BIT_RRE;
  532. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  533. hw_access_flags |= MLX4_QP_BIT_RAE;
  534. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  535. hw_access_flags |= MLX4_QP_BIT_RWE;
  536. return cpu_to_be32(hw_access_flags);
  537. }
  538. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  539. int attr_mask)
  540. {
  541. if (attr_mask & IB_QP_PKEY_INDEX)
  542. sqp->pkey_index = attr->pkey_index;
  543. if (attr_mask & IB_QP_QKEY)
  544. sqp->qkey = attr->qkey;
  545. if (attr_mask & IB_QP_SQ_PSN)
  546. sqp->send_psn = attr->sq_psn;
  547. }
  548. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  549. {
  550. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  551. }
  552. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_ah_attr *ah,
  553. struct mlx4_qp_path *path, u8 port)
  554. {
  555. path->grh_mylmc = ah->src_path_bits & 0x7f;
  556. path->rlid = cpu_to_be16(ah->dlid);
  557. if (ah->static_rate) {
  558. path->static_rate = ah->static_rate + MLX4_STAT_RATE_OFFSET;
  559. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  560. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  561. --path->static_rate;
  562. } else
  563. path->static_rate = 0;
  564. path->counter_index = 0xff;
  565. if (ah->ah_flags & IB_AH_GRH) {
  566. if (ah->grh.sgid_index >= dev->dev->caps.gid_table_len) {
  567. printk(KERN_ERR "sgid_index (%u) too large. max is %d\n",
  568. ah->grh.sgid_index, dev->dev->caps.gid_table_len - 1);
  569. return -1;
  570. }
  571. path->grh_mylmc |= 1 << 7;
  572. path->mgid_index = ah->grh.sgid_index;
  573. path->hop_limit = ah->grh.hop_limit;
  574. path->tclass_flowlabel =
  575. cpu_to_be32((ah->grh.traffic_class << 20) |
  576. (ah->grh.flow_label));
  577. memcpy(path->rgid, ah->grh.dgid.raw, 16);
  578. }
  579. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  580. ((port - 1) << 6) | ((ah->sl & 0xf) << 2);
  581. return 0;
  582. }
  583. static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
  584. const struct ib_qp_attr *attr, int attr_mask,
  585. enum ib_qp_state cur_state, enum ib_qp_state new_state)
  586. {
  587. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  588. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  589. struct mlx4_qp_context *context;
  590. enum mlx4_qp_optpar optpar = 0;
  591. int sqd_event;
  592. int err = -EINVAL;
  593. context = kzalloc(sizeof *context, GFP_KERNEL);
  594. if (!context)
  595. return -ENOMEM;
  596. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  597. (to_mlx4_st(ibqp->qp_type) << 16));
  598. context->flags |= cpu_to_be32(1 << 8); /* DE? */
  599. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  600. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  601. else {
  602. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  603. switch (attr->path_mig_state) {
  604. case IB_MIG_MIGRATED:
  605. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  606. break;
  607. case IB_MIG_REARM:
  608. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  609. break;
  610. case IB_MIG_ARMED:
  611. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  612. break;
  613. }
  614. }
  615. if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  616. ibqp->qp_type == IB_QPT_UD)
  617. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  618. else if (attr_mask & IB_QP_PATH_MTU) {
  619. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  620. printk(KERN_ERR "path MTU (%u) is invalid\n",
  621. attr->path_mtu);
  622. return -EINVAL;
  623. }
  624. context->mtu_msgmax = (attr->path_mtu << 5) | 31;
  625. }
  626. if (qp->rq.max)
  627. context->rq_size_stride = ilog2(qp->rq.max) << 3;
  628. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  629. if (qp->sq.max)
  630. context->sq_size_stride = ilog2(qp->sq.max) << 3;
  631. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  632. if (qp->ibqp.uobject)
  633. context->usr_page = cpu_to_be32(to_mucontext(ibqp->uobject->context)->uar.index);
  634. else
  635. context->usr_page = cpu_to_be32(dev->priv_uar.index);
  636. if (attr_mask & IB_QP_DEST_QPN)
  637. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  638. if (attr_mask & IB_QP_PORT) {
  639. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  640. !(attr_mask & IB_QP_AV)) {
  641. mlx4_set_sched(&context->pri_path, attr->port_num);
  642. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  643. }
  644. }
  645. if (attr_mask & IB_QP_PKEY_INDEX) {
  646. context->pri_path.pkey_index = attr->pkey_index;
  647. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  648. }
  649. if (attr_mask & IB_QP_RNR_RETRY) {
  650. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  651. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  652. }
  653. if (attr_mask & IB_QP_AV) {
  654. if (mlx4_set_path(dev, &attr->ah_attr, &context->pri_path,
  655. attr_mask & IB_QP_PORT ? attr->port_num : qp->port)) {
  656. err = -EINVAL;
  657. goto out;
  658. }
  659. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  660. MLX4_QP_OPTPAR_SCHED_QUEUE);
  661. }
  662. if (attr_mask & IB_QP_TIMEOUT) {
  663. context->pri_path.ackto = attr->timeout << 3;
  664. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  665. }
  666. if (attr_mask & IB_QP_ALT_PATH) {
  667. if (attr->alt_pkey_index >= dev->dev->caps.pkey_table_len)
  668. return -EINVAL;
  669. if (attr->alt_port_num == 0 ||
  670. attr->alt_port_num > dev->dev->caps.num_ports)
  671. return -EINVAL;
  672. if (mlx4_set_path(dev, &attr->alt_ah_attr, &context->alt_path,
  673. attr->alt_port_num))
  674. return -EINVAL;
  675. context->alt_path.pkey_index = attr->alt_pkey_index;
  676. context->alt_path.ackto = attr->alt_timeout << 3;
  677. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  678. }
  679. context->pd = cpu_to_be32(to_mpd(ibqp->pd)->pdn);
  680. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  681. if (attr_mask & IB_QP_RETRY_CNT) {
  682. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  683. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  684. }
  685. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  686. if (attr->max_rd_atomic)
  687. context->params1 |=
  688. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  689. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  690. }
  691. if (attr_mask & IB_QP_SQ_PSN)
  692. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  693. context->cqn_send = cpu_to_be32(to_mcq(ibqp->send_cq)->mcq.cqn);
  694. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  695. if (attr->max_dest_rd_atomic)
  696. context->params2 |=
  697. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  698. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  699. }
  700. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  701. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  702. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  703. }
  704. if (ibqp->srq)
  705. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  706. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  707. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  708. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  709. }
  710. if (attr_mask & IB_QP_RQ_PSN)
  711. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  712. context->cqn_recv = cpu_to_be32(to_mcq(ibqp->recv_cq)->mcq.cqn);
  713. if (attr_mask & IB_QP_QKEY) {
  714. context->qkey = cpu_to_be32(attr->qkey);
  715. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  716. }
  717. if (ibqp->srq)
  718. context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
  719. if (!ibqp->srq && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  720. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  721. if (cur_state == IB_QPS_INIT &&
  722. new_state == IB_QPS_RTR &&
  723. (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  724. ibqp->qp_type == IB_QPT_UD)) {
  725. context->pri_path.sched_queue = (qp->port - 1) << 6;
  726. if (is_qp0(dev, qp))
  727. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  728. else
  729. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  730. }
  731. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  732. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  733. sqd_event = 1;
  734. else
  735. sqd_event = 0;
  736. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  737. to_mlx4_state(new_state), context, optpar,
  738. sqd_event, &qp->mqp);
  739. if (err)
  740. goto out;
  741. qp->state = new_state;
  742. if (attr_mask & IB_QP_ACCESS_FLAGS)
  743. qp->atomic_rd_en = attr->qp_access_flags;
  744. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  745. qp->resp_depth = attr->max_dest_rd_atomic;
  746. if (attr_mask & IB_QP_PORT)
  747. qp->port = attr->port_num;
  748. if (attr_mask & IB_QP_ALT_PATH)
  749. qp->alt_port = attr->alt_port_num;
  750. if (is_sqp(dev, qp))
  751. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  752. /*
  753. * If we moved QP0 to RTR, bring the IB link up; if we moved
  754. * QP0 to RESET or ERROR, bring the link back down.
  755. */
  756. if (is_qp0(dev, qp)) {
  757. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  758. init_port(dev, qp->port);
  759. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  760. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  761. mlx4_CLOSE_PORT(dev->dev, qp->port);
  762. }
  763. /*
  764. * If we moved a kernel QP to RESET, clean up all old CQ
  765. * entries and reinitialize the QP.
  766. */
  767. if (new_state == IB_QPS_RESET && !ibqp->uobject) {
  768. mlx4_ib_cq_clean(to_mcq(ibqp->recv_cq), qp->mqp.qpn,
  769. ibqp->srq ? to_msrq(ibqp->srq): NULL);
  770. if (ibqp->send_cq != ibqp->recv_cq)
  771. mlx4_ib_cq_clean(to_mcq(ibqp->send_cq), qp->mqp.qpn, NULL);
  772. qp->rq.head = 0;
  773. qp->rq.tail = 0;
  774. qp->sq.head = 0;
  775. qp->sq.tail = 0;
  776. if (!ibqp->srq)
  777. *qp->db.db = 0;
  778. }
  779. out:
  780. kfree(context);
  781. return err;
  782. }
  783. static const struct ib_qp_attr mlx4_ib_qp_attr = { .port_num = 1 };
  784. static const int mlx4_ib_qp_attr_mask_table[IB_QPT_UD + 1] = {
  785. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  786. IB_QP_PORT |
  787. IB_QP_QKEY),
  788. [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
  789. IB_QP_PORT |
  790. IB_QP_ACCESS_FLAGS),
  791. [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
  792. IB_QP_PORT |
  793. IB_QP_ACCESS_FLAGS),
  794. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  795. IB_QP_QKEY),
  796. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  797. IB_QP_QKEY),
  798. };
  799. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  800. int attr_mask, struct ib_udata *udata)
  801. {
  802. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  803. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  804. enum ib_qp_state cur_state, new_state;
  805. int err = -EINVAL;
  806. mutex_lock(&qp->mutex);
  807. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  808. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  809. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask))
  810. goto out;
  811. if ((attr_mask & IB_QP_PKEY_INDEX) &&
  812. attr->pkey_index >= dev->dev->caps.pkey_table_len) {
  813. goto out;
  814. }
  815. if ((attr_mask & IB_QP_PORT) &&
  816. (attr->port_num == 0 || attr->port_num > dev->dev->caps.num_ports)) {
  817. goto out;
  818. }
  819. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  820. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  821. goto out;
  822. }
  823. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  824. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  825. goto out;
  826. }
  827. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  828. err = 0;
  829. goto out;
  830. }
  831. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_ERR) {
  832. err = __mlx4_ib_modify_qp(ibqp, &mlx4_ib_qp_attr,
  833. mlx4_ib_qp_attr_mask_table[ibqp->qp_type],
  834. IB_QPS_RESET, IB_QPS_INIT);
  835. if (err)
  836. goto out;
  837. cur_state = IB_QPS_INIT;
  838. }
  839. err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
  840. out:
  841. mutex_unlock(&qp->mutex);
  842. return err;
  843. }
  844. static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_send_wr *wr,
  845. void *wqe)
  846. {
  847. struct ib_device *ib_dev = &to_mdev(sqp->qp.ibqp.device)->ib_dev;
  848. struct mlx4_wqe_mlx_seg *mlx = wqe;
  849. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  850. struct mlx4_ib_ah *ah = to_mah(wr->wr.ud.ah);
  851. u16 pkey;
  852. int send_size;
  853. int header_size;
  854. int i;
  855. send_size = 0;
  856. for (i = 0; i < wr->num_sge; ++i)
  857. send_size += wr->sg_list[i].length;
  858. ib_ud_header_init(send_size, mlx4_ib_ah_grh_present(ah), &sqp->ud_header);
  859. sqp->ud_header.lrh.service_level =
  860. be32_to_cpu(ah->av.sl_tclass_flowlabel) >> 28;
  861. sqp->ud_header.lrh.destination_lid = ah->av.dlid;
  862. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.g_slid & 0x7f);
  863. if (mlx4_ib_ah_grh_present(ah)) {
  864. sqp->ud_header.grh.traffic_class =
  865. (be32_to_cpu(ah->av.sl_tclass_flowlabel) >> 20) & 0xff;
  866. sqp->ud_header.grh.flow_label =
  867. ah->av.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  868. sqp->ud_header.grh.hop_limit = ah->av.hop_limit;
  869. ib_get_cached_gid(ib_dev, be32_to_cpu(ah->av.port_pd) >> 24,
  870. ah->av.gid_index, &sqp->ud_header.grh.source_gid);
  871. memcpy(sqp->ud_header.grh.destination_gid.raw,
  872. ah->av.dgid, 16);
  873. }
  874. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  875. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  876. (sqp->ud_header.lrh.destination_lid ==
  877. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  878. (sqp->ud_header.lrh.service_level << 8));
  879. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  880. switch (wr->opcode) {
  881. case IB_WR_SEND:
  882. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  883. sqp->ud_header.immediate_present = 0;
  884. break;
  885. case IB_WR_SEND_WITH_IMM:
  886. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  887. sqp->ud_header.immediate_present = 1;
  888. sqp->ud_header.immediate_data = wr->imm_data;
  889. break;
  890. default:
  891. return -EINVAL;
  892. }
  893. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
  894. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  895. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  896. sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
  897. if (!sqp->qp.ibqp.qp_num)
  898. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  899. else
  900. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->wr.ud.pkey_index, &pkey);
  901. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  902. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  903. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  904. sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
  905. sqp->qkey : wr->wr.ud.remote_qkey);
  906. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  907. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  908. if (0) {
  909. printk(KERN_ERR "built UD header of size %d:\n", header_size);
  910. for (i = 0; i < header_size / 4; ++i) {
  911. if (i % 8 == 0)
  912. printk(" [%02x] ", i * 4);
  913. printk(" %08x",
  914. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  915. if ((i + 1) % 8 == 0)
  916. printk("\n");
  917. }
  918. printk("\n");
  919. }
  920. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  921. memcpy(inl + 1, sqp->header_buf, header_size);
  922. return ALIGN(sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  923. }
  924. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  925. {
  926. unsigned cur;
  927. struct mlx4_ib_cq *cq;
  928. cur = wq->head - wq->tail;
  929. if (likely(cur + nreq < wq->max))
  930. return 0;
  931. cq = to_mcq(ib_cq);
  932. spin_lock(&cq->lock);
  933. cur = wq->head - wq->tail;
  934. spin_unlock(&cq->lock);
  935. return cur + nreq >= wq->max;
  936. }
  937. int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  938. struct ib_send_wr **bad_wr)
  939. {
  940. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  941. void *wqe;
  942. struct mlx4_wqe_ctrl_seg *ctrl;
  943. unsigned long flags;
  944. int nreq;
  945. int err = 0;
  946. int ind;
  947. int size;
  948. int i;
  949. spin_lock_irqsave(&qp->rq.lock, flags);
  950. ind = qp->sq.head;
  951. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  952. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  953. err = -ENOMEM;
  954. *bad_wr = wr;
  955. goto out;
  956. }
  957. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  958. err = -EINVAL;
  959. *bad_wr = wr;
  960. goto out;
  961. }
  962. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.max - 1));
  963. qp->sq.wrid[ind & (qp->sq.max - 1)] = wr->wr_id;
  964. ctrl->srcrb_flags =
  965. (wr->send_flags & IB_SEND_SIGNALED ?
  966. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  967. (wr->send_flags & IB_SEND_SOLICITED ?
  968. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  969. qp->sq_signal_bits;
  970. if (wr->opcode == IB_WR_SEND_WITH_IMM ||
  971. wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  972. ctrl->imm = wr->imm_data;
  973. else
  974. ctrl->imm = 0;
  975. wqe += sizeof *ctrl;
  976. size = sizeof *ctrl / 16;
  977. switch (ibqp->qp_type) {
  978. case IB_QPT_RC:
  979. case IB_QPT_UC:
  980. switch (wr->opcode) {
  981. case IB_WR_ATOMIC_CMP_AND_SWP:
  982. case IB_WR_ATOMIC_FETCH_AND_ADD:
  983. ((struct mlx4_wqe_raddr_seg *) wqe)->raddr =
  984. cpu_to_be64(wr->wr.atomic.remote_addr);
  985. ((struct mlx4_wqe_raddr_seg *) wqe)->rkey =
  986. cpu_to_be32(wr->wr.atomic.rkey);
  987. ((struct mlx4_wqe_raddr_seg *) wqe)->reserved = 0;
  988. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  989. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  990. ((struct mlx4_wqe_atomic_seg *) wqe)->swap_add =
  991. cpu_to_be64(wr->wr.atomic.swap);
  992. ((struct mlx4_wqe_atomic_seg *) wqe)->compare =
  993. cpu_to_be64(wr->wr.atomic.compare_add);
  994. } else {
  995. ((struct mlx4_wqe_atomic_seg *) wqe)->swap_add =
  996. cpu_to_be64(wr->wr.atomic.compare_add);
  997. ((struct mlx4_wqe_atomic_seg *) wqe)->compare = 0;
  998. }
  999. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  1000. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  1001. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  1002. break;
  1003. case IB_WR_RDMA_READ:
  1004. case IB_WR_RDMA_WRITE:
  1005. case IB_WR_RDMA_WRITE_WITH_IMM:
  1006. ((struct mlx4_wqe_raddr_seg *) wqe)->raddr =
  1007. cpu_to_be64(wr->wr.rdma.remote_addr);
  1008. ((struct mlx4_wqe_raddr_seg *) wqe)->rkey =
  1009. cpu_to_be32(wr->wr.rdma.rkey);
  1010. ((struct mlx4_wqe_raddr_seg *) wqe)->reserved = 0;
  1011. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1012. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  1013. break;
  1014. default:
  1015. /* No extra segments required for sends */
  1016. break;
  1017. }
  1018. break;
  1019. case IB_QPT_UD:
  1020. memcpy(((struct mlx4_wqe_datagram_seg *) wqe)->av,
  1021. &to_mah(wr->wr.ud.ah)->av, sizeof (struct mlx4_av));
  1022. ((struct mlx4_wqe_datagram_seg *) wqe)->dqpn =
  1023. cpu_to_be32(wr->wr.ud.remote_qpn);
  1024. ((struct mlx4_wqe_datagram_seg *) wqe)->qkey =
  1025. cpu_to_be32(wr->wr.ud.remote_qkey);
  1026. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  1027. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  1028. break;
  1029. case IB_QPT_SMI:
  1030. case IB_QPT_GSI:
  1031. err = build_mlx_header(to_msqp(qp), wr, ctrl);
  1032. if (err < 0) {
  1033. *bad_wr = wr;
  1034. goto out;
  1035. }
  1036. wqe += err;
  1037. size += err / 16;
  1038. err = 0;
  1039. break;
  1040. default:
  1041. break;
  1042. }
  1043. for (i = 0; i < wr->num_sge; ++i) {
  1044. ((struct mlx4_wqe_data_seg *) wqe)->byte_count =
  1045. cpu_to_be32(wr->sg_list[i].length);
  1046. ((struct mlx4_wqe_data_seg *) wqe)->lkey =
  1047. cpu_to_be32(wr->sg_list[i].lkey);
  1048. ((struct mlx4_wqe_data_seg *) wqe)->addr =
  1049. cpu_to_be64(wr->sg_list[i].addr);
  1050. wqe += sizeof (struct mlx4_wqe_data_seg);
  1051. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1052. }
  1053. /* Add one more inline data segment for ICRC for MLX sends */
  1054. if (qp->ibqp.qp_type == IB_QPT_SMI || qp->ibqp.qp_type == IB_QPT_GSI) {
  1055. ((struct mlx4_wqe_inline_seg *) wqe)->byte_count =
  1056. cpu_to_be32((1 << 31) | 4);
  1057. ((u32 *) wqe)[1] = 0;
  1058. wqe += sizeof (struct mlx4_wqe_data_seg);
  1059. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1060. }
  1061. ctrl->fence_size = (wr->send_flags & IB_SEND_FENCE ?
  1062. MLX4_WQE_CTRL_FENCE : 0) | size;
  1063. /*
  1064. * Make sure descriptor is fully written before
  1065. * setting ownership bit (because HW can start
  1066. * executing as soon as we do).
  1067. */
  1068. wmb();
  1069. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  1070. err = -EINVAL;
  1071. goto out;
  1072. }
  1073. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  1074. (ind & qp->sq.max ? cpu_to_be32(1 << 31) : 0);
  1075. ++ind;
  1076. }
  1077. out:
  1078. if (likely(nreq)) {
  1079. qp->sq.head += nreq;
  1080. /*
  1081. * Make sure that descriptors are written before
  1082. * doorbell record.
  1083. */
  1084. wmb();
  1085. writel(qp->doorbell_qpn,
  1086. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  1087. /*
  1088. * Make sure doorbells don't leak out of SQ spinlock
  1089. * and reach the HCA out of order.
  1090. */
  1091. mmiowb();
  1092. }
  1093. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1094. return err;
  1095. }
  1096. int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1097. struct ib_recv_wr **bad_wr)
  1098. {
  1099. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1100. struct mlx4_wqe_data_seg *scat;
  1101. unsigned long flags;
  1102. int err = 0;
  1103. int nreq;
  1104. int ind;
  1105. int i;
  1106. spin_lock_irqsave(&qp->rq.lock, flags);
  1107. ind = qp->rq.head & (qp->rq.max - 1);
  1108. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1109. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.send_cq)) {
  1110. err = -ENOMEM;
  1111. *bad_wr = wr;
  1112. goto out;
  1113. }
  1114. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  1115. err = -EINVAL;
  1116. *bad_wr = wr;
  1117. goto out;
  1118. }
  1119. scat = get_recv_wqe(qp, ind);
  1120. for (i = 0; i < wr->num_sge; ++i) {
  1121. scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
  1122. scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey);
  1123. scat[i].addr = cpu_to_be64(wr->sg_list[i].addr);
  1124. }
  1125. if (i < qp->rq.max_gs) {
  1126. scat[i].byte_count = 0;
  1127. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  1128. scat[i].addr = 0;
  1129. }
  1130. qp->rq.wrid[ind] = wr->wr_id;
  1131. ind = (ind + 1) & (qp->rq.max - 1);
  1132. }
  1133. out:
  1134. if (likely(nreq)) {
  1135. qp->rq.head += nreq;
  1136. /*
  1137. * Make sure that descriptors are written before
  1138. * doorbell record.
  1139. */
  1140. wmb();
  1141. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  1142. }
  1143. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1144. return err;
  1145. }