cq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 <linux/mlx4/cq.h>
  33. #include <linux/mlx4/qp.h>
  34. #include "mlx4_ib.h"
  35. #include "user.h"
  36. static void mlx4_ib_cq_comp(struct mlx4_cq *cq)
  37. {
  38. struct ib_cq *ibcq = &to_mibcq(cq)->ibcq;
  39. ibcq->comp_handler(ibcq, ibcq->cq_context);
  40. }
  41. static void mlx4_ib_cq_event(struct mlx4_cq *cq, enum mlx4_event type)
  42. {
  43. struct ib_event event;
  44. struct ib_cq *ibcq;
  45. if (type != MLX4_EVENT_TYPE_CQ_ERROR) {
  46. printk(KERN_WARNING "mlx4_ib: Unexpected event type %d "
  47. "on CQ %06x\n", type, cq->cqn);
  48. return;
  49. }
  50. ibcq = &to_mibcq(cq)->ibcq;
  51. if (ibcq->event_handler) {
  52. event.device = ibcq->device;
  53. event.event = IB_EVENT_CQ_ERR;
  54. event.element.cq = ibcq;
  55. ibcq->event_handler(&event, ibcq->cq_context);
  56. }
  57. }
  58. static void *get_cqe_from_buf(struct mlx4_ib_cq_buf *buf, int n)
  59. {
  60. return mlx4_buf_offset(&buf->buf, n * sizeof (struct mlx4_cqe));
  61. }
  62. static void *get_cqe(struct mlx4_ib_cq *cq, int n)
  63. {
  64. return get_cqe_from_buf(&cq->buf, n);
  65. }
  66. static void *get_sw_cqe(struct mlx4_ib_cq *cq, int n)
  67. {
  68. struct mlx4_cqe *cqe = get_cqe(cq, n & cq->ibcq.cqe);
  69. return (!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
  70. !!(n & (cq->ibcq.cqe + 1))) ? NULL : cqe;
  71. }
  72. static struct mlx4_cqe *next_cqe_sw(struct mlx4_ib_cq *cq)
  73. {
  74. return get_sw_cqe(cq, cq->mcq.cons_index);
  75. }
  76. struct ib_cq *mlx4_ib_create_cq(struct ib_device *ibdev, int entries, int vector,
  77. struct ib_ucontext *context,
  78. struct ib_udata *udata)
  79. {
  80. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  81. struct mlx4_ib_cq *cq;
  82. struct mlx4_uar *uar;
  83. int buf_size;
  84. int err;
  85. if (entries < 1 || entries > dev->dev->caps.max_cqes)
  86. return ERR_PTR(-EINVAL);
  87. cq = kmalloc(sizeof *cq, GFP_KERNEL);
  88. if (!cq)
  89. return ERR_PTR(-ENOMEM);
  90. entries = roundup_pow_of_two(entries + 1);
  91. cq->ibcq.cqe = entries - 1;
  92. buf_size = entries * sizeof (struct mlx4_cqe);
  93. spin_lock_init(&cq->lock);
  94. if (context) {
  95. struct mlx4_ib_create_cq ucmd;
  96. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  97. err = -EFAULT;
  98. goto err_cq;
  99. }
  100. cq->umem = ib_umem_get(context, ucmd.buf_addr, buf_size,
  101. IB_ACCESS_LOCAL_WRITE);
  102. if (IS_ERR(cq->umem)) {
  103. err = PTR_ERR(cq->umem);
  104. goto err_cq;
  105. }
  106. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(cq->umem),
  107. ilog2(cq->umem->page_size), &cq->buf.mtt);
  108. if (err)
  109. goto err_buf;
  110. err = mlx4_ib_umem_write_mtt(dev, &cq->buf.mtt, cq->umem);
  111. if (err)
  112. goto err_mtt;
  113. err = mlx4_ib_db_map_user(to_mucontext(context), ucmd.db_addr,
  114. &cq->db);
  115. if (err)
  116. goto err_mtt;
  117. uar = &to_mucontext(context)->uar;
  118. } else {
  119. err = mlx4_ib_db_alloc(dev, &cq->db, 1);
  120. if (err)
  121. goto err_cq;
  122. cq->mcq.set_ci_db = cq->db.db;
  123. cq->mcq.arm_db = cq->db.db + 1;
  124. *cq->mcq.set_ci_db = 0;
  125. *cq->mcq.arm_db = 0;
  126. if (mlx4_buf_alloc(dev->dev, buf_size, PAGE_SIZE * 2, &cq->buf.buf)) {
  127. err = -ENOMEM;
  128. goto err_db;
  129. }
  130. err = mlx4_mtt_init(dev->dev, cq->buf.buf.npages, cq->buf.buf.page_shift,
  131. &cq->buf.mtt);
  132. if (err)
  133. goto err_buf;
  134. err = mlx4_buf_write_mtt(dev->dev, &cq->buf.mtt, &cq->buf.buf);
  135. if (err)
  136. goto err_mtt;
  137. uar = &dev->priv_uar;
  138. }
  139. err = mlx4_cq_alloc(dev->dev, entries, &cq->buf.mtt, uar,
  140. cq->db.dma, &cq->mcq);
  141. if (err)
  142. goto err_dbmap;
  143. cq->mcq.comp = mlx4_ib_cq_comp;
  144. cq->mcq.event = mlx4_ib_cq_event;
  145. if (context)
  146. if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof (__u32))) {
  147. err = -EFAULT;
  148. goto err_dbmap;
  149. }
  150. return &cq->ibcq;
  151. err_dbmap:
  152. if (context)
  153. mlx4_ib_db_unmap_user(to_mucontext(context), &cq->db);
  154. err_mtt:
  155. mlx4_mtt_cleanup(dev->dev, &cq->buf.mtt);
  156. err_buf:
  157. if (context)
  158. ib_umem_release(cq->umem);
  159. else
  160. mlx4_buf_free(dev->dev, entries * sizeof (struct mlx4_cqe),
  161. &cq->buf.buf);
  162. err_db:
  163. if (!context)
  164. mlx4_ib_db_free(dev, &cq->db);
  165. err_cq:
  166. kfree(cq);
  167. return ERR_PTR(err);
  168. }
  169. int mlx4_ib_destroy_cq(struct ib_cq *cq)
  170. {
  171. struct mlx4_ib_dev *dev = to_mdev(cq->device);
  172. struct mlx4_ib_cq *mcq = to_mcq(cq);
  173. mlx4_cq_free(dev->dev, &mcq->mcq);
  174. mlx4_mtt_cleanup(dev->dev, &mcq->buf.mtt);
  175. if (cq->uobject) {
  176. mlx4_ib_db_unmap_user(to_mucontext(cq->uobject->context), &mcq->db);
  177. ib_umem_release(mcq->umem);
  178. } else {
  179. mlx4_buf_free(dev->dev, (cq->cqe + 1) * sizeof (struct mlx4_cqe),
  180. &mcq->buf.buf);
  181. mlx4_ib_db_free(dev, &mcq->db);
  182. }
  183. kfree(mcq);
  184. return 0;
  185. }
  186. static void dump_cqe(void *cqe)
  187. {
  188. __be32 *buf = cqe;
  189. printk(KERN_DEBUG "CQE contents %08x %08x %08x %08x %08x %08x %08x %08x\n",
  190. be32_to_cpu(buf[0]), be32_to_cpu(buf[1]), be32_to_cpu(buf[2]),
  191. be32_to_cpu(buf[3]), be32_to_cpu(buf[4]), be32_to_cpu(buf[5]),
  192. be32_to_cpu(buf[6]), be32_to_cpu(buf[7]));
  193. }
  194. static void mlx4_ib_handle_error_cqe(struct mlx4_err_cqe *cqe,
  195. struct ib_wc *wc)
  196. {
  197. if (cqe->syndrome == MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR) {
  198. printk(KERN_DEBUG "local QP operation err "
  199. "(QPN %06x, WQE index %x, vendor syndrome %02x, "
  200. "opcode = %02x)\n",
  201. be32_to_cpu(cqe->my_qpn), be16_to_cpu(cqe->wqe_index),
  202. cqe->vendor_err_syndrome,
  203. cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
  204. dump_cqe(cqe);
  205. }
  206. switch (cqe->syndrome) {
  207. case MLX4_CQE_SYNDROME_LOCAL_LENGTH_ERR:
  208. wc->status = IB_WC_LOC_LEN_ERR;
  209. break;
  210. case MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR:
  211. wc->status = IB_WC_LOC_QP_OP_ERR;
  212. break;
  213. case MLX4_CQE_SYNDROME_LOCAL_PROT_ERR:
  214. wc->status = IB_WC_LOC_PROT_ERR;
  215. break;
  216. case MLX4_CQE_SYNDROME_WR_FLUSH_ERR:
  217. wc->status = IB_WC_WR_FLUSH_ERR;
  218. break;
  219. case MLX4_CQE_SYNDROME_MW_BIND_ERR:
  220. wc->status = IB_WC_MW_BIND_ERR;
  221. break;
  222. case MLX4_CQE_SYNDROME_BAD_RESP_ERR:
  223. wc->status = IB_WC_BAD_RESP_ERR;
  224. break;
  225. case MLX4_CQE_SYNDROME_LOCAL_ACCESS_ERR:
  226. wc->status = IB_WC_LOC_ACCESS_ERR;
  227. break;
  228. case MLX4_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
  229. wc->status = IB_WC_REM_INV_REQ_ERR;
  230. break;
  231. case MLX4_CQE_SYNDROME_REMOTE_ACCESS_ERR:
  232. wc->status = IB_WC_REM_ACCESS_ERR;
  233. break;
  234. case MLX4_CQE_SYNDROME_REMOTE_OP_ERR:
  235. wc->status = IB_WC_REM_OP_ERR;
  236. break;
  237. case MLX4_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
  238. wc->status = IB_WC_RETRY_EXC_ERR;
  239. break;
  240. case MLX4_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
  241. wc->status = IB_WC_RNR_RETRY_EXC_ERR;
  242. break;
  243. case MLX4_CQE_SYNDROME_REMOTE_ABORTED_ERR:
  244. wc->status = IB_WC_REM_ABORT_ERR;
  245. break;
  246. default:
  247. wc->status = IB_WC_GENERAL_ERR;
  248. break;
  249. }
  250. wc->vendor_err = cqe->vendor_err_syndrome;
  251. }
  252. static int mlx4_ib_poll_one(struct mlx4_ib_cq *cq,
  253. struct mlx4_ib_qp **cur_qp,
  254. struct ib_wc *wc)
  255. {
  256. struct mlx4_cqe *cqe;
  257. struct mlx4_qp *mqp;
  258. struct mlx4_ib_wq *wq;
  259. struct mlx4_ib_srq *srq;
  260. int is_send;
  261. int is_error;
  262. u32 g_mlpath_rqpn;
  263. u16 wqe_ctr;
  264. cqe = next_cqe_sw(cq);
  265. if (!cqe)
  266. return -EAGAIN;
  267. ++cq->mcq.cons_index;
  268. /*
  269. * Make sure we read CQ entry contents after we've checked the
  270. * ownership bit.
  271. */
  272. rmb();
  273. is_send = cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK;
  274. is_error = (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
  275. MLX4_CQE_OPCODE_ERROR;
  276. if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) == MLX4_OPCODE_NOP &&
  277. is_send)) {
  278. printk(KERN_WARNING "Completion for NOP opcode detected!\n");
  279. return -EINVAL;
  280. }
  281. if (!*cur_qp ||
  282. (be32_to_cpu(cqe->my_qpn) & 0xffffff) != (*cur_qp)->mqp.qpn) {
  283. /*
  284. * We do not have to take the QP table lock here,
  285. * because CQs will be locked while QPs are removed
  286. * from the table.
  287. */
  288. mqp = __mlx4_qp_lookup(to_mdev(cq->ibcq.device)->dev,
  289. be32_to_cpu(cqe->my_qpn));
  290. if (unlikely(!mqp)) {
  291. printk(KERN_WARNING "CQ %06x with entry for unknown QPN %06x\n",
  292. cq->mcq.cqn, be32_to_cpu(cqe->my_qpn) & 0xffffff);
  293. return -EINVAL;
  294. }
  295. *cur_qp = to_mibqp(mqp);
  296. }
  297. wc->qp = &(*cur_qp)->ibqp;
  298. if (is_send) {
  299. wq = &(*cur_qp)->sq;
  300. if (!(*cur_qp)->sq_signal_bits) {
  301. wqe_ctr = be16_to_cpu(cqe->wqe_index);
  302. wq->tail += (u16) (wqe_ctr - (u16) wq->tail);
  303. }
  304. wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
  305. ++wq->tail;
  306. } else if ((*cur_qp)->ibqp.srq) {
  307. srq = to_msrq((*cur_qp)->ibqp.srq);
  308. wqe_ctr = be16_to_cpu(cqe->wqe_index);
  309. wc->wr_id = srq->wrid[wqe_ctr];
  310. mlx4_ib_free_srq_wqe(srq, wqe_ctr);
  311. } else {
  312. wq = &(*cur_qp)->rq;
  313. wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
  314. ++wq->tail;
  315. }
  316. if (unlikely(is_error)) {
  317. mlx4_ib_handle_error_cqe((struct mlx4_err_cqe *) cqe, wc);
  318. return 0;
  319. }
  320. wc->status = IB_WC_SUCCESS;
  321. if (is_send) {
  322. wc->wc_flags = 0;
  323. switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
  324. case MLX4_OPCODE_RDMA_WRITE_IMM:
  325. wc->wc_flags |= IB_WC_WITH_IMM;
  326. case MLX4_OPCODE_RDMA_WRITE:
  327. wc->opcode = IB_WC_RDMA_WRITE;
  328. break;
  329. case MLX4_OPCODE_SEND_IMM:
  330. wc->wc_flags |= IB_WC_WITH_IMM;
  331. case MLX4_OPCODE_SEND:
  332. wc->opcode = IB_WC_SEND;
  333. break;
  334. case MLX4_OPCODE_RDMA_READ:
  335. wc->opcode = IB_WC_RDMA_READ;
  336. wc->byte_len = be32_to_cpu(cqe->byte_cnt);
  337. break;
  338. case MLX4_OPCODE_ATOMIC_CS:
  339. wc->opcode = IB_WC_COMP_SWAP;
  340. wc->byte_len = 8;
  341. break;
  342. case MLX4_OPCODE_ATOMIC_FA:
  343. wc->opcode = IB_WC_FETCH_ADD;
  344. wc->byte_len = 8;
  345. break;
  346. case MLX4_OPCODE_BIND_MW:
  347. wc->opcode = IB_WC_BIND_MW;
  348. break;
  349. }
  350. } else {
  351. wc->byte_len = be32_to_cpu(cqe->byte_cnt);
  352. switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
  353. case MLX4_RECV_OPCODE_RDMA_WRITE_IMM:
  354. wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
  355. wc->wc_flags = IB_WC_WITH_IMM;
  356. wc->imm_data = cqe->immed_rss_invalid;
  357. break;
  358. case MLX4_RECV_OPCODE_SEND:
  359. wc->opcode = IB_WC_RECV;
  360. wc->wc_flags = 0;
  361. break;
  362. case MLX4_RECV_OPCODE_SEND_IMM:
  363. wc->opcode = IB_WC_RECV;
  364. wc->wc_flags = IB_WC_WITH_IMM;
  365. wc->imm_data = cqe->immed_rss_invalid;
  366. break;
  367. }
  368. wc->slid = be16_to_cpu(cqe->rlid);
  369. wc->sl = cqe->sl >> 4;
  370. g_mlpath_rqpn = be32_to_cpu(cqe->g_mlpath_rqpn);
  371. wc->src_qp = g_mlpath_rqpn & 0xffffff;
  372. wc->dlid_path_bits = (g_mlpath_rqpn >> 24) & 0x7f;
  373. wc->wc_flags |= g_mlpath_rqpn & 0x80000000 ? IB_WC_GRH : 0;
  374. wc->pkey_index = be32_to_cpu(cqe->immed_rss_invalid) & 0x7f;
  375. }
  376. return 0;
  377. }
  378. int mlx4_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
  379. {
  380. struct mlx4_ib_cq *cq = to_mcq(ibcq);
  381. struct mlx4_ib_qp *cur_qp = NULL;
  382. unsigned long flags;
  383. int npolled;
  384. int err = 0;
  385. spin_lock_irqsave(&cq->lock, flags);
  386. for (npolled = 0; npolled < num_entries; ++npolled) {
  387. err = mlx4_ib_poll_one(cq, &cur_qp, wc + npolled);
  388. if (err)
  389. break;
  390. }
  391. if (npolled)
  392. mlx4_cq_set_ci(&cq->mcq);
  393. spin_unlock_irqrestore(&cq->lock, flags);
  394. if (err == 0 || err == -EAGAIN)
  395. return npolled;
  396. else
  397. return err;
  398. }
  399. int mlx4_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
  400. {
  401. mlx4_cq_arm(&to_mcq(ibcq)->mcq,
  402. (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
  403. MLX4_CQ_DB_REQ_NOT_SOL : MLX4_CQ_DB_REQ_NOT,
  404. to_mdev(ibcq->device)->uar_map,
  405. MLX4_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->uar_lock));
  406. return 0;
  407. }
  408. void __mlx4_ib_cq_clean(struct mlx4_ib_cq *cq, u32 qpn, struct mlx4_ib_srq *srq)
  409. {
  410. u32 prod_index;
  411. int nfreed = 0;
  412. struct mlx4_cqe *cqe, *dest;
  413. u8 owner_bit;
  414. /*
  415. * First we need to find the current producer index, so we
  416. * know where to start cleaning from. It doesn't matter if HW
  417. * adds new entries after this loop -- the QP we're worried
  418. * about is already in RESET, so the new entries won't come
  419. * from our QP and therefore don't need to be checked.
  420. */
  421. for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); ++prod_index)
  422. if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
  423. break;
  424. /*
  425. * Now sweep backwards through the CQ, removing CQ entries
  426. * that match our QP by copying older entries on top of them.
  427. */
  428. while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
  429. cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
  430. if ((be32_to_cpu(cqe->my_qpn) & 0xffffff) == qpn) {
  431. if (srq && !(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK))
  432. mlx4_ib_free_srq_wqe(srq, be16_to_cpu(cqe->wqe_index));
  433. ++nfreed;
  434. } else if (nfreed) {
  435. dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
  436. owner_bit = dest->owner_sr_opcode & MLX4_CQE_OWNER_MASK;
  437. memcpy(dest, cqe, sizeof *cqe);
  438. dest->owner_sr_opcode = owner_bit |
  439. (dest->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
  440. }
  441. }
  442. if (nfreed) {
  443. cq->mcq.cons_index += nfreed;
  444. /*
  445. * Make sure update of buffer contents is done before
  446. * updating consumer index.
  447. */
  448. wmb();
  449. mlx4_cq_set_ci(&cq->mcq);
  450. }
  451. }
  452. void mlx4_ib_cq_clean(struct mlx4_ib_cq *cq, u32 qpn, struct mlx4_ib_srq *srq)
  453. {
  454. spin_lock_irq(&cq->lock);
  455. __mlx4_ib_cq_clean(cq, qpn, srq);
  456. spin_unlock_irq(&cq->lock);
  457. }