cq.c 13 KB

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