cq.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * Copyright (c) 2013, Mellanox Technologies 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/kref.h>
  33. #include <rdma/ib_umem.h>
  34. #include "mlx5_ib.h"
  35. #include "user.h"
  36. static void mlx5_ib_cq_comp(struct mlx5_core_cq *cq)
  37. {
  38. struct ib_cq *ibcq = &to_mibcq(cq)->ibcq;
  39. ibcq->comp_handler(ibcq, ibcq->cq_context);
  40. }
  41. static void mlx5_ib_cq_event(struct mlx5_core_cq *mcq, enum mlx5_event type)
  42. {
  43. struct mlx5_ib_cq *cq = container_of(mcq, struct mlx5_ib_cq, mcq);
  44. struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
  45. struct ib_cq *ibcq = &cq->ibcq;
  46. struct ib_event event;
  47. if (type != MLX5_EVENT_TYPE_CQ_ERROR) {
  48. mlx5_ib_warn(dev, "Unexpected event type %d on CQ %06x\n",
  49. type, mcq->cqn);
  50. return;
  51. }
  52. if (ibcq->event_handler) {
  53. event.device = &dev->ib_dev;
  54. event.event = IB_EVENT_CQ_ERR;
  55. event.element.cq = ibcq;
  56. ibcq->event_handler(&event, ibcq->cq_context);
  57. }
  58. }
  59. static void *get_cqe_from_buf(struct mlx5_ib_cq_buf *buf, int n, int size)
  60. {
  61. return mlx5_buf_offset(&buf->buf, n * size);
  62. }
  63. static void *get_cqe(struct mlx5_ib_cq *cq, int n)
  64. {
  65. return get_cqe_from_buf(&cq->buf, n, cq->mcq.cqe_sz);
  66. }
  67. static void *get_sw_cqe(struct mlx5_ib_cq *cq, int n)
  68. {
  69. void *cqe = get_cqe(cq, n & cq->ibcq.cqe);
  70. struct mlx5_cqe64 *cqe64;
  71. cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
  72. return ((cqe64->op_own & MLX5_CQE_OWNER_MASK) ^
  73. !!(n & (cq->ibcq.cqe + 1))) ? NULL : cqe;
  74. }
  75. static void *next_cqe_sw(struct mlx5_ib_cq *cq)
  76. {
  77. return get_sw_cqe(cq, cq->mcq.cons_index);
  78. }
  79. static enum ib_wc_opcode get_umr_comp(struct mlx5_ib_wq *wq, int idx)
  80. {
  81. switch (wq->wr_data[idx]) {
  82. case MLX5_IB_WR_UMR:
  83. return 0;
  84. case IB_WR_LOCAL_INV:
  85. return IB_WC_LOCAL_INV;
  86. case IB_WR_FAST_REG_MR:
  87. return IB_WC_FAST_REG_MR;
  88. default:
  89. pr_warn("unknown completion status\n");
  90. return 0;
  91. }
  92. }
  93. static void handle_good_req(struct ib_wc *wc, struct mlx5_cqe64 *cqe,
  94. struct mlx5_ib_wq *wq, int idx)
  95. {
  96. wc->wc_flags = 0;
  97. switch (be32_to_cpu(cqe->sop_drop_qpn) >> 24) {
  98. case MLX5_OPCODE_RDMA_WRITE_IMM:
  99. wc->wc_flags |= IB_WC_WITH_IMM;
  100. case MLX5_OPCODE_RDMA_WRITE:
  101. wc->opcode = IB_WC_RDMA_WRITE;
  102. break;
  103. case MLX5_OPCODE_SEND_IMM:
  104. wc->wc_flags |= IB_WC_WITH_IMM;
  105. case MLX5_OPCODE_SEND:
  106. case MLX5_OPCODE_SEND_INVAL:
  107. wc->opcode = IB_WC_SEND;
  108. break;
  109. case MLX5_OPCODE_RDMA_READ:
  110. wc->opcode = IB_WC_RDMA_READ;
  111. wc->byte_len = be32_to_cpu(cqe->byte_cnt);
  112. break;
  113. case MLX5_OPCODE_ATOMIC_CS:
  114. wc->opcode = IB_WC_COMP_SWAP;
  115. wc->byte_len = 8;
  116. break;
  117. case MLX5_OPCODE_ATOMIC_FA:
  118. wc->opcode = IB_WC_FETCH_ADD;
  119. wc->byte_len = 8;
  120. break;
  121. case MLX5_OPCODE_ATOMIC_MASKED_CS:
  122. wc->opcode = IB_WC_MASKED_COMP_SWAP;
  123. wc->byte_len = 8;
  124. break;
  125. case MLX5_OPCODE_ATOMIC_MASKED_FA:
  126. wc->opcode = IB_WC_MASKED_FETCH_ADD;
  127. wc->byte_len = 8;
  128. break;
  129. case MLX5_OPCODE_BIND_MW:
  130. wc->opcode = IB_WC_BIND_MW;
  131. break;
  132. case MLX5_OPCODE_UMR:
  133. wc->opcode = get_umr_comp(wq, idx);
  134. break;
  135. }
  136. }
  137. enum {
  138. MLX5_GRH_IN_BUFFER = 1,
  139. MLX5_GRH_IN_CQE = 2,
  140. };
  141. static void handle_responder(struct ib_wc *wc, struct mlx5_cqe64 *cqe,
  142. struct mlx5_ib_qp *qp)
  143. {
  144. struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
  145. struct mlx5_ib_srq *srq;
  146. struct mlx5_ib_wq *wq;
  147. u16 wqe_ctr;
  148. u8 g;
  149. if (qp->ibqp.srq || qp->ibqp.xrcd) {
  150. struct mlx5_core_srq *msrq = NULL;
  151. if (qp->ibqp.xrcd) {
  152. msrq = mlx5_core_get_srq(&dev->mdev,
  153. be32_to_cpu(cqe->srqn));
  154. srq = to_mibsrq(msrq);
  155. } else {
  156. srq = to_msrq(qp->ibqp.srq);
  157. }
  158. if (srq) {
  159. wqe_ctr = be16_to_cpu(cqe->wqe_counter);
  160. wc->wr_id = srq->wrid[wqe_ctr];
  161. mlx5_ib_free_srq_wqe(srq, wqe_ctr);
  162. if (msrq && atomic_dec_and_test(&msrq->refcount))
  163. complete(&msrq->free);
  164. }
  165. } else {
  166. wq = &qp->rq;
  167. wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
  168. ++wq->tail;
  169. }
  170. wc->byte_len = be32_to_cpu(cqe->byte_cnt);
  171. switch (cqe->op_own >> 4) {
  172. case MLX5_CQE_RESP_WR_IMM:
  173. wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
  174. wc->wc_flags = IB_WC_WITH_IMM;
  175. wc->ex.imm_data = cqe->imm_inval_pkey;
  176. break;
  177. case MLX5_CQE_RESP_SEND:
  178. wc->opcode = IB_WC_RECV;
  179. wc->wc_flags = 0;
  180. break;
  181. case MLX5_CQE_RESP_SEND_IMM:
  182. wc->opcode = IB_WC_RECV;
  183. wc->wc_flags = IB_WC_WITH_IMM;
  184. wc->ex.imm_data = cqe->imm_inval_pkey;
  185. break;
  186. case MLX5_CQE_RESP_SEND_INV:
  187. wc->opcode = IB_WC_RECV;
  188. wc->wc_flags = IB_WC_WITH_INVALIDATE;
  189. wc->ex.invalidate_rkey = be32_to_cpu(cqe->imm_inval_pkey);
  190. break;
  191. }
  192. wc->slid = be16_to_cpu(cqe->slid);
  193. wc->sl = (be32_to_cpu(cqe->flags_rqpn) >> 24) & 0xf;
  194. wc->src_qp = be32_to_cpu(cqe->flags_rqpn) & 0xffffff;
  195. wc->dlid_path_bits = cqe->ml_path;
  196. g = (be32_to_cpu(cqe->flags_rqpn) >> 28) & 3;
  197. wc->wc_flags |= g ? IB_WC_GRH : 0;
  198. wc->pkey_index = be32_to_cpu(cqe->imm_inval_pkey) & 0xffff;
  199. }
  200. static void dump_cqe(struct mlx5_ib_dev *dev, struct mlx5_err_cqe *cqe)
  201. {
  202. __be32 *p = (__be32 *)cqe;
  203. int i;
  204. mlx5_ib_warn(dev, "dump error cqe\n");
  205. for (i = 0; i < sizeof(*cqe) / 16; i++, p += 4)
  206. pr_info("%08x %08x %08x %08x\n", be32_to_cpu(p[0]),
  207. be32_to_cpu(p[1]), be32_to_cpu(p[2]),
  208. be32_to_cpu(p[3]));
  209. }
  210. static void mlx5_handle_error_cqe(struct mlx5_ib_dev *dev,
  211. struct mlx5_err_cqe *cqe,
  212. struct ib_wc *wc)
  213. {
  214. int dump = 1;
  215. switch (cqe->syndrome) {
  216. case MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR:
  217. wc->status = IB_WC_LOC_LEN_ERR;
  218. break;
  219. case MLX5_CQE_SYNDROME_LOCAL_QP_OP_ERR:
  220. wc->status = IB_WC_LOC_QP_OP_ERR;
  221. break;
  222. case MLX5_CQE_SYNDROME_LOCAL_PROT_ERR:
  223. wc->status = IB_WC_LOC_PROT_ERR;
  224. break;
  225. case MLX5_CQE_SYNDROME_WR_FLUSH_ERR:
  226. dump = 0;
  227. wc->status = IB_WC_WR_FLUSH_ERR;
  228. break;
  229. case MLX5_CQE_SYNDROME_MW_BIND_ERR:
  230. wc->status = IB_WC_MW_BIND_ERR;
  231. break;
  232. case MLX5_CQE_SYNDROME_BAD_RESP_ERR:
  233. wc->status = IB_WC_BAD_RESP_ERR;
  234. break;
  235. case MLX5_CQE_SYNDROME_LOCAL_ACCESS_ERR:
  236. wc->status = IB_WC_LOC_ACCESS_ERR;
  237. break;
  238. case MLX5_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
  239. wc->status = IB_WC_REM_INV_REQ_ERR;
  240. break;
  241. case MLX5_CQE_SYNDROME_REMOTE_ACCESS_ERR:
  242. wc->status = IB_WC_REM_ACCESS_ERR;
  243. break;
  244. case MLX5_CQE_SYNDROME_REMOTE_OP_ERR:
  245. wc->status = IB_WC_REM_OP_ERR;
  246. break;
  247. case MLX5_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
  248. wc->status = IB_WC_RETRY_EXC_ERR;
  249. dump = 0;
  250. break;
  251. case MLX5_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
  252. wc->status = IB_WC_RNR_RETRY_EXC_ERR;
  253. dump = 0;
  254. break;
  255. case MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR:
  256. wc->status = IB_WC_REM_ABORT_ERR;
  257. break;
  258. default:
  259. wc->status = IB_WC_GENERAL_ERR;
  260. break;
  261. }
  262. wc->vendor_err = cqe->vendor_err_synd;
  263. if (dump)
  264. dump_cqe(dev, cqe);
  265. }
  266. static int is_atomic_response(struct mlx5_ib_qp *qp, uint16_t idx)
  267. {
  268. /* TBD: waiting decision
  269. */
  270. return 0;
  271. }
  272. static void *mlx5_get_atomic_laddr(struct mlx5_ib_qp *qp, uint16_t idx)
  273. {
  274. struct mlx5_wqe_data_seg *dpseg;
  275. void *addr;
  276. dpseg = mlx5_get_send_wqe(qp, idx) + sizeof(struct mlx5_wqe_ctrl_seg) +
  277. sizeof(struct mlx5_wqe_raddr_seg) +
  278. sizeof(struct mlx5_wqe_atomic_seg);
  279. addr = (void *)(unsigned long)be64_to_cpu(dpseg->addr);
  280. return addr;
  281. }
  282. static void handle_atomic(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
  283. uint16_t idx)
  284. {
  285. void *addr;
  286. int byte_count;
  287. int i;
  288. if (!is_atomic_response(qp, idx))
  289. return;
  290. byte_count = be32_to_cpu(cqe64->byte_cnt);
  291. addr = mlx5_get_atomic_laddr(qp, idx);
  292. if (byte_count == 4) {
  293. *(uint32_t *)addr = be32_to_cpu(*((__be32 *)addr));
  294. } else {
  295. for (i = 0; i < byte_count; i += 8) {
  296. *(uint64_t *)addr = be64_to_cpu(*((__be64 *)addr));
  297. addr += 8;
  298. }
  299. }
  300. return;
  301. }
  302. static void handle_atomics(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
  303. u16 tail, u16 head)
  304. {
  305. int idx;
  306. do {
  307. idx = tail & (qp->sq.wqe_cnt - 1);
  308. handle_atomic(qp, cqe64, idx);
  309. if (idx == head)
  310. break;
  311. tail = qp->sq.w_list[idx].next;
  312. } while (1);
  313. tail = qp->sq.w_list[idx].next;
  314. qp->sq.last_poll = tail;
  315. }
  316. static int mlx5_poll_one(struct mlx5_ib_cq *cq,
  317. struct mlx5_ib_qp **cur_qp,
  318. struct ib_wc *wc)
  319. {
  320. struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
  321. struct mlx5_err_cqe *err_cqe;
  322. struct mlx5_cqe64 *cqe64;
  323. struct mlx5_core_qp *mqp;
  324. struct mlx5_ib_wq *wq;
  325. uint8_t opcode;
  326. uint32_t qpn;
  327. u16 wqe_ctr;
  328. void *cqe;
  329. int idx;
  330. cqe = next_cqe_sw(cq);
  331. if (!cqe)
  332. return -EAGAIN;
  333. cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
  334. ++cq->mcq.cons_index;
  335. /* Make sure we read CQ entry contents after we've checked the
  336. * ownership bit.
  337. */
  338. rmb();
  339. /* TBD: resize CQ */
  340. qpn = ntohl(cqe64->sop_drop_qpn) & 0xffffff;
  341. if (!*cur_qp || (qpn != (*cur_qp)->ibqp.qp_num)) {
  342. /* We do not have to take the QP table lock here,
  343. * because CQs will be locked while QPs are removed
  344. * from the table.
  345. */
  346. mqp = __mlx5_qp_lookup(&dev->mdev, qpn);
  347. if (unlikely(!mqp)) {
  348. mlx5_ib_warn(dev, "CQE@CQ %06x for unknown QPN %6x\n",
  349. cq->mcq.cqn, qpn);
  350. return -EINVAL;
  351. }
  352. *cur_qp = to_mibqp(mqp);
  353. }
  354. wc->qp = &(*cur_qp)->ibqp;
  355. opcode = cqe64->op_own >> 4;
  356. switch (opcode) {
  357. case MLX5_CQE_REQ:
  358. wq = &(*cur_qp)->sq;
  359. wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
  360. idx = wqe_ctr & (wq->wqe_cnt - 1);
  361. handle_good_req(wc, cqe64, wq, idx);
  362. handle_atomics(*cur_qp, cqe64, wq->last_poll, idx);
  363. wc->wr_id = wq->wrid[idx];
  364. wq->tail = wq->wqe_head[idx] + 1;
  365. wc->status = IB_WC_SUCCESS;
  366. break;
  367. case MLX5_CQE_RESP_WR_IMM:
  368. case MLX5_CQE_RESP_SEND:
  369. case MLX5_CQE_RESP_SEND_IMM:
  370. case MLX5_CQE_RESP_SEND_INV:
  371. handle_responder(wc, cqe64, *cur_qp);
  372. wc->status = IB_WC_SUCCESS;
  373. break;
  374. case MLX5_CQE_RESIZE_CQ:
  375. break;
  376. case MLX5_CQE_REQ_ERR:
  377. case MLX5_CQE_RESP_ERR:
  378. err_cqe = (struct mlx5_err_cqe *)cqe64;
  379. mlx5_handle_error_cqe(dev, err_cqe, wc);
  380. mlx5_ib_dbg(dev, "%s error cqe on cqn 0x%x:\n",
  381. opcode == MLX5_CQE_REQ_ERR ?
  382. "Requestor" : "Responder", cq->mcq.cqn);
  383. mlx5_ib_dbg(dev, "syndrome 0x%x, vendor syndrome 0x%x\n",
  384. err_cqe->syndrome, err_cqe->vendor_err_synd);
  385. if (opcode == MLX5_CQE_REQ_ERR) {
  386. wq = &(*cur_qp)->sq;
  387. wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
  388. idx = wqe_ctr & (wq->wqe_cnt - 1);
  389. wc->wr_id = wq->wrid[idx];
  390. wq->tail = wq->wqe_head[idx] + 1;
  391. } else {
  392. struct mlx5_ib_srq *srq;
  393. if ((*cur_qp)->ibqp.srq) {
  394. srq = to_msrq((*cur_qp)->ibqp.srq);
  395. wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
  396. wc->wr_id = srq->wrid[wqe_ctr];
  397. mlx5_ib_free_srq_wqe(srq, wqe_ctr);
  398. } else {
  399. wq = &(*cur_qp)->rq;
  400. wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
  401. ++wq->tail;
  402. }
  403. }
  404. break;
  405. }
  406. return 0;
  407. }
  408. int mlx5_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
  409. {
  410. struct mlx5_ib_cq *cq = to_mcq(ibcq);
  411. struct mlx5_ib_qp *cur_qp = NULL;
  412. unsigned long flags;
  413. int npolled;
  414. int err = 0;
  415. spin_lock_irqsave(&cq->lock, flags);
  416. for (npolled = 0; npolled < num_entries; npolled++) {
  417. err = mlx5_poll_one(cq, &cur_qp, wc + npolled);
  418. if (err)
  419. break;
  420. }
  421. if (npolled)
  422. mlx5_cq_set_ci(&cq->mcq);
  423. spin_unlock_irqrestore(&cq->lock, flags);
  424. if (err == 0 || err == -EAGAIN)
  425. return npolled;
  426. else
  427. return err;
  428. }
  429. int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
  430. {
  431. mlx5_cq_arm(&to_mcq(ibcq)->mcq,
  432. (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
  433. MLX5_CQ_DB_REQ_NOT_SOL : MLX5_CQ_DB_REQ_NOT,
  434. to_mdev(ibcq->device)->mdev.priv.uuari.uars[0].map,
  435. MLX5_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->mdev.priv.cq_uar_lock));
  436. return 0;
  437. }
  438. static int alloc_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf,
  439. int nent, int cqe_size)
  440. {
  441. int err;
  442. err = mlx5_buf_alloc(&dev->mdev, nent * cqe_size,
  443. PAGE_SIZE * 2, &buf->buf);
  444. if (err)
  445. return err;
  446. buf->cqe_size = cqe_size;
  447. return 0;
  448. }
  449. static void free_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf)
  450. {
  451. mlx5_buf_free(&dev->mdev, &buf->buf);
  452. }
  453. static int create_cq_user(struct mlx5_ib_dev *dev, struct ib_udata *udata,
  454. struct ib_ucontext *context, struct mlx5_ib_cq *cq,
  455. int entries, struct mlx5_create_cq_mbox_in **cqb,
  456. int *cqe_size, int *index, int *inlen)
  457. {
  458. struct mlx5_ib_create_cq ucmd;
  459. int page_shift;
  460. int npages;
  461. int ncont;
  462. int err;
  463. if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd)))
  464. return -EFAULT;
  465. if (ucmd.cqe_size != 64 && ucmd.cqe_size != 128)
  466. return -EINVAL;
  467. *cqe_size = ucmd.cqe_size;
  468. cq->buf.umem = ib_umem_get(context, ucmd.buf_addr,
  469. entries * ucmd.cqe_size,
  470. IB_ACCESS_LOCAL_WRITE, 1);
  471. if (IS_ERR(cq->buf.umem)) {
  472. err = PTR_ERR(cq->buf.umem);
  473. return err;
  474. }
  475. err = mlx5_ib_db_map_user(to_mucontext(context), ucmd.db_addr,
  476. &cq->db);
  477. if (err)
  478. goto err_umem;
  479. mlx5_ib_cont_pages(cq->buf.umem, ucmd.buf_addr, &npages, &page_shift,
  480. &ncont, NULL);
  481. mlx5_ib_dbg(dev, "addr 0x%llx, size %u, npages %d, page_shift %d, ncont %d\n",
  482. ucmd.buf_addr, entries * ucmd.cqe_size, npages, page_shift, ncont);
  483. *inlen = sizeof(**cqb) + sizeof(*(*cqb)->pas) * ncont;
  484. *cqb = mlx5_vzalloc(*inlen);
  485. if (!*cqb) {
  486. err = -ENOMEM;
  487. goto err_db;
  488. }
  489. mlx5_ib_populate_pas(dev, cq->buf.umem, page_shift, (*cqb)->pas, 0);
  490. (*cqb)->ctx.log_pg_sz = page_shift - PAGE_SHIFT;
  491. *index = to_mucontext(context)->uuari.uars[0].index;
  492. return 0;
  493. err_db:
  494. mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
  495. err_umem:
  496. ib_umem_release(cq->buf.umem);
  497. return err;
  498. }
  499. static void destroy_cq_user(struct mlx5_ib_cq *cq, struct ib_ucontext *context)
  500. {
  501. mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
  502. ib_umem_release(cq->buf.umem);
  503. }
  504. static void init_cq_buf(struct mlx5_ib_cq *cq, int nent)
  505. {
  506. int i;
  507. void *cqe;
  508. struct mlx5_cqe64 *cqe64;
  509. for (i = 0; i < nent; i++) {
  510. cqe = get_cqe(cq, i);
  511. cqe64 = (cq->buf.cqe_size == 64) ? cqe : cqe + 64;
  512. cqe64->op_own = 0xf1;
  513. }
  514. }
  515. static int create_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
  516. int entries, int cqe_size,
  517. struct mlx5_create_cq_mbox_in **cqb,
  518. int *index, int *inlen)
  519. {
  520. int err;
  521. err = mlx5_db_alloc(&dev->mdev, &cq->db);
  522. if (err)
  523. return err;
  524. cq->mcq.set_ci_db = cq->db.db;
  525. cq->mcq.arm_db = cq->db.db + 1;
  526. *cq->mcq.set_ci_db = 0;
  527. *cq->mcq.arm_db = 0;
  528. cq->mcq.cqe_sz = cqe_size;
  529. err = alloc_cq_buf(dev, &cq->buf, entries, cqe_size);
  530. if (err)
  531. goto err_db;
  532. init_cq_buf(cq, entries);
  533. *inlen = sizeof(**cqb) + sizeof(*(*cqb)->pas) * cq->buf.buf.npages;
  534. *cqb = mlx5_vzalloc(*inlen);
  535. if (!*cqb) {
  536. err = -ENOMEM;
  537. goto err_buf;
  538. }
  539. mlx5_fill_page_array(&cq->buf.buf, (*cqb)->pas);
  540. (*cqb)->ctx.log_pg_sz = cq->buf.buf.page_shift - PAGE_SHIFT;
  541. *index = dev->mdev.priv.uuari.uars[0].index;
  542. return 0;
  543. err_buf:
  544. free_cq_buf(dev, &cq->buf);
  545. err_db:
  546. mlx5_db_free(&dev->mdev, &cq->db);
  547. return err;
  548. }
  549. static void destroy_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
  550. {
  551. free_cq_buf(dev, &cq->buf);
  552. mlx5_db_free(&dev->mdev, &cq->db);
  553. }
  554. struct ib_cq *mlx5_ib_create_cq(struct ib_device *ibdev, int entries,
  555. int vector, struct ib_ucontext *context,
  556. struct ib_udata *udata)
  557. {
  558. struct mlx5_create_cq_mbox_in *cqb = NULL;
  559. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  560. struct mlx5_ib_cq *cq;
  561. int uninitialized_var(index);
  562. int uninitialized_var(inlen);
  563. int cqe_size;
  564. int irqn;
  565. int eqn;
  566. int err;
  567. entries = roundup_pow_of_two(entries + 1);
  568. if (entries < 1 || entries > dev->mdev.caps.max_cqes)
  569. return ERR_PTR(-EINVAL);
  570. cq = kzalloc(sizeof(*cq), GFP_KERNEL);
  571. if (!cq)
  572. return ERR_PTR(-ENOMEM);
  573. cq->ibcq.cqe = entries - 1;
  574. mutex_init(&cq->resize_mutex);
  575. spin_lock_init(&cq->lock);
  576. cq->resize_buf = NULL;
  577. cq->resize_umem = NULL;
  578. if (context) {
  579. err = create_cq_user(dev, udata, context, cq, entries,
  580. &cqb, &cqe_size, &index, &inlen);
  581. if (err)
  582. goto err_create;
  583. } else {
  584. /* for now choose 64 bytes till we have a proper interface */
  585. cqe_size = 64;
  586. err = create_cq_kernel(dev, cq, entries, cqe_size, &cqb,
  587. &index, &inlen);
  588. if (err)
  589. goto err_create;
  590. }
  591. cq->cqe_size = cqe_size;
  592. cqb->ctx.cqe_sz_flags = cqe_sz_to_mlx_sz(cqe_size) << 5;
  593. cqb->ctx.log_sz_usr_page = cpu_to_be32((ilog2(entries) << 24) | index);
  594. err = mlx5_vector2eqn(dev, vector, &eqn, &irqn);
  595. if (err)
  596. goto err_cqb;
  597. cqb->ctx.c_eqn = cpu_to_be16(eqn);
  598. cqb->ctx.db_record_addr = cpu_to_be64(cq->db.dma);
  599. err = mlx5_core_create_cq(&dev->mdev, &cq->mcq, cqb, inlen);
  600. if (err)
  601. goto err_cqb;
  602. mlx5_ib_dbg(dev, "cqn 0x%x\n", cq->mcq.cqn);
  603. cq->mcq.irqn = irqn;
  604. cq->mcq.comp = mlx5_ib_cq_comp;
  605. cq->mcq.event = mlx5_ib_cq_event;
  606. if (context)
  607. if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof(__u32))) {
  608. err = -EFAULT;
  609. goto err_cmd;
  610. }
  611. mlx5_vfree(cqb);
  612. return &cq->ibcq;
  613. err_cmd:
  614. mlx5_core_destroy_cq(&dev->mdev, &cq->mcq);
  615. err_cqb:
  616. mlx5_vfree(cqb);
  617. if (context)
  618. destroy_cq_user(cq, context);
  619. else
  620. destroy_cq_kernel(dev, cq);
  621. err_create:
  622. kfree(cq);
  623. return ERR_PTR(err);
  624. }
  625. int mlx5_ib_destroy_cq(struct ib_cq *cq)
  626. {
  627. struct mlx5_ib_dev *dev = to_mdev(cq->device);
  628. struct mlx5_ib_cq *mcq = to_mcq(cq);
  629. struct ib_ucontext *context = NULL;
  630. if (cq->uobject)
  631. context = cq->uobject->context;
  632. mlx5_core_destroy_cq(&dev->mdev, &mcq->mcq);
  633. if (context)
  634. destroy_cq_user(mcq, context);
  635. else
  636. destroy_cq_kernel(dev, mcq);
  637. kfree(mcq);
  638. return 0;
  639. }
  640. static int is_equal_rsn(struct mlx5_cqe64 *cqe64, struct mlx5_ib_srq *srq,
  641. u32 rsn)
  642. {
  643. u32 lrsn;
  644. if (srq)
  645. lrsn = be32_to_cpu(cqe64->srqn) & 0xffffff;
  646. else
  647. lrsn = be32_to_cpu(cqe64->sop_drop_qpn) & 0xffffff;
  648. return rsn == lrsn;
  649. }
  650. void __mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 rsn, struct mlx5_ib_srq *srq)
  651. {
  652. struct mlx5_cqe64 *cqe64, *dest64;
  653. void *cqe, *dest;
  654. u32 prod_index;
  655. int nfreed = 0;
  656. u8 owner_bit;
  657. if (!cq)
  658. return;
  659. /* First we need to find the current producer index, so we
  660. * know where to start cleaning from. It doesn't matter if HW
  661. * adds new entries after this loop -- the QP we're worried
  662. * about is already in RESET, so the new entries won't come
  663. * from our QP and therefore don't need to be checked.
  664. */
  665. for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); prod_index++)
  666. if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
  667. break;
  668. /* Now sweep backwards through the CQ, removing CQ entries
  669. * that match our QP by copying older entries on top of them.
  670. */
  671. while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
  672. cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
  673. cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
  674. if (is_equal_rsn(cqe64, srq, rsn)) {
  675. if (srq)
  676. mlx5_ib_free_srq_wqe(srq, be16_to_cpu(cqe64->wqe_counter));
  677. ++nfreed;
  678. } else if (nfreed) {
  679. dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
  680. dest64 = (cq->mcq.cqe_sz == 64) ? dest : dest + 64;
  681. owner_bit = dest64->op_own & MLX5_CQE_OWNER_MASK;
  682. memcpy(dest, cqe, cq->mcq.cqe_sz);
  683. dest64->op_own = owner_bit |
  684. (dest64->op_own & ~MLX5_CQE_OWNER_MASK);
  685. }
  686. }
  687. if (nfreed) {
  688. cq->mcq.cons_index += nfreed;
  689. /* Make sure update of buffer contents is done before
  690. * updating consumer index.
  691. */
  692. wmb();
  693. mlx5_cq_set_ci(&cq->mcq);
  694. }
  695. }
  696. void mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq)
  697. {
  698. if (!cq)
  699. return;
  700. spin_lock_irq(&cq->lock);
  701. __mlx5_ib_cq_clean(cq, qpn, srq);
  702. spin_unlock_irq(&cq->lock);
  703. }
  704. int mlx5_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
  705. {
  706. return -ENOSYS;
  707. }
  708. int mlx5_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
  709. {
  710. return -ENOSYS;
  711. }
  712. int mlx5_ib_get_cqe_size(struct mlx5_ib_dev *dev, struct ib_cq *ibcq)
  713. {
  714. struct mlx5_ib_cq *cq;
  715. if (!ibcq)
  716. return 128;
  717. cq = to_mcq(ibcq);
  718. return cq->cqe_size;
  719. }