cq.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/mlx4/cq.h>
  34. #include <linux/mlx4/qp.h>
  35. #include <linux/slab.h>
  36. #include "mlx4_ib.h"
  37. #include "user.h"
  38. static void mlx4_ib_cq_comp(struct mlx4_cq *cq)
  39. {
  40. struct ib_cq *ibcq = &to_mibcq(cq)->ibcq;
  41. ibcq->comp_handler(ibcq, ibcq->cq_context);
  42. }
  43. static void mlx4_ib_cq_event(struct mlx4_cq *cq, enum mlx4_event type)
  44. {
  45. struct ib_event event;
  46. struct ib_cq *ibcq;
  47. if (type != MLX4_EVENT_TYPE_CQ_ERROR) {
  48. pr_warn("Unexpected event type %d "
  49. "on CQ %06x\n", type, cq->cqn);
  50. return;
  51. }
  52. ibcq = &to_mibcq(cq)->ibcq;
  53. if (ibcq->event_handler) {
  54. event.device = ibcq->device;
  55. event.event = IB_EVENT_CQ_ERR;
  56. event.element.cq = ibcq;
  57. ibcq->event_handler(&event, ibcq->cq_context);
  58. }
  59. }
  60. static void *get_cqe_from_buf(struct mlx4_ib_cq_buf *buf, int n)
  61. {
  62. return mlx4_buf_offset(&buf->buf, n * sizeof (struct mlx4_cqe));
  63. }
  64. static void *get_cqe(struct mlx4_ib_cq *cq, int n)
  65. {
  66. return get_cqe_from_buf(&cq->buf, n);
  67. }
  68. static void *get_sw_cqe(struct mlx4_ib_cq *cq, int n)
  69. {
  70. struct mlx4_cqe *cqe = get_cqe(cq, n & cq->ibcq.cqe);
  71. return (!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
  72. !!(n & (cq->ibcq.cqe + 1))) ? NULL : cqe;
  73. }
  74. static struct mlx4_cqe *next_cqe_sw(struct mlx4_ib_cq *cq)
  75. {
  76. return get_sw_cqe(cq, cq->mcq.cons_index);
  77. }
  78. int mlx4_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
  79. {
  80. struct mlx4_ib_cq *mcq = to_mcq(cq);
  81. struct mlx4_ib_dev *dev = to_mdev(cq->device);
  82. return mlx4_cq_modify(dev->dev, &mcq->mcq, cq_count, cq_period);
  83. }
  84. static int mlx4_ib_alloc_cq_buf(struct mlx4_ib_dev *dev, struct mlx4_ib_cq_buf *buf, int nent)
  85. {
  86. int err;
  87. err = mlx4_buf_alloc(dev->dev, nent * sizeof(struct mlx4_cqe),
  88. PAGE_SIZE * 2, &buf->buf);
  89. if (err)
  90. goto out;
  91. err = mlx4_mtt_init(dev->dev, buf->buf.npages, buf->buf.page_shift,
  92. &buf->mtt);
  93. if (err)
  94. goto err_buf;
  95. err = mlx4_buf_write_mtt(dev->dev, &buf->mtt, &buf->buf);
  96. if (err)
  97. goto err_mtt;
  98. return 0;
  99. err_mtt:
  100. mlx4_mtt_cleanup(dev->dev, &buf->mtt);
  101. err_buf:
  102. mlx4_buf_free(dev->dev, nent * sizeof(struct mlx4_cqe),
  103. &buf->buf);
  104. out:
  105. return err;
  106. }
  107. static void mlx4_ib_free_cq_buf(struct mlx4_ib_dev *dev, struct mlx4_ib_cq_buf *buf, int cqe)
  108. {
  109. mlx4_buf_free(dev->dev, (cqe + 1) * sizeof(struct mlx4_cqe), &buf->buf);
  110. }
  111. static int mlx4_ib_get_cq_umem(struct mlx4_ib_dev *dev, struct ib_ucontext *context,
  112. struct mlx4_ib_cq_buf *buf, struct ib_umem **umem,
  113. u64 buf_addr, int cqe)
  114. {
  115. int err;
  116. *umem = ib_umem_get(context, buf_addr, cqe * sizeof (struct mlx4_cqe),
  117. IB_ACCESS_LOCAL_WRITE, 1);
  118. if (IS_ERR(*umem))
  119. return PTR_ERR(*umem);
  120. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(*umem),
  121. ilog2((*umem)->page_size), &buf->mtt);
  122. if (err)
  123. goto err_buf;
  124. err = mlx4_ib_umem_write_mtt(dev, &buf->mtt, *umem);
  125. if (err)
  126. goto err_mtt;
  127. return 0;
  128. err_mtt:
  129. mlx4_mtt_cleanup(dev->dev, &buf->mtt);
  130. err_buf:
  131. ib_umem_release(*umem);
  132. return err;
  133. }
  134. struct ib_cq *mlx4_ib_create_cq(struct ib_device *ibdev, int entries, int vector,
  135. struct ib_ucontext *context,
  136. struct ib_udata *udata)
  137. {
  138. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  139. struct mlx4_ib_cq *cq;
  140. struct mlx4_uar *uar;
  141. int err;
  142. if (entries < 1 || entries > dev->dev->caps.max_cqes)
  143. return ERR_PTR(-EINVAL);
  144. cq = kmalloc(sizeof *cq, GFP_KERNEL);
  145. if (!cq)
  146. return ERR_PTR(-ENOMEM);
  147. entries = roundup_pow_of_two(entries + 1);
  148. cq->ibcq.cqe = entries - 1;
  149. mutex_init(&cq->resize_mutex);
  150. spin_lock_init(&cq->lock);
  151. cq->resize_buf = NULL;
  152. cq->resize_umem = NULL;
  153. if (context) {
  154. struct mlx4_ib_create_cq ucmd;
  155. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  156. err = -EFAULT;
  157. goto err_cq;
  158. }
  159. err = mlx4_ib_get_cq_umem(dev, context, &cq->buf, &cq->umem,
  160. ucmd.buf_addr, entries);
  161. if (err)
  162. goto err_cq;
  163. err = mlx4_ib_db_map_user(to_mucontext(context), ucmd.db_addr,
  164. &cq->db);
  165. if (err)
  166. goto err_mtt;
  167. uar = &to_mucontext(context)->uar;
  168. } else {
  169. err = mlx4_db_alloc(dev->dev, &cq->db, 1);
  170. if (err)
  171. goto err_cq;
  172. cq->mcq.set_ci_db = cq->db.db;
  173. cq->mcq.arm_db = cq->db.db + 1;
  174. *cq->mcq.set_ci_db = 0;
  175. *cq->mcq.arm_db = 0;
  176. err = mlx4_ib_alloc_cq_buf(dev, &cq->buf, entries);
  177. if (err)
  178. goto err_db;
  179. uar = &dev->priv_uar;
  180. }
  181. if (dev->eq_table)
  182. vector = dev->eq_table[vector % ibdev->num_comp_vectors];
  183. err = mlx4_cq_alloc(dev->dev, entries, &cq->buf.mtt, uar,
  184. cq->db.dma, &cq->mcq, vector, 0);
  185. if (err)
  186. goto err_dbmap;
  187. cq->mcq.comp = mlx4_ib_cq_comp;
  188. cq->mcq.event = mlx4_ib_cq_event;
  189. if (context)
  190. if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof (__u32))) {
  191. err = -EFAULT;
  192. goto err_dbmap;
  193. }
  194. return &cq->ibcq;
  195. err_dbmap:
  196. if (context)
  197. mlx4_ib_db_unmap_user(to_mucontext(context), &cq->db);
  198. err_mtt:
  199. mlx4_mtt_cleanup(dev->dev, &cq->buf.mtt);
  200. if (context)
  201. ib_umem_release(cq->umem);
  202. else
  203. mlx4_ib_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
  204. err_db:
  205. if (!context)
  206. mlx4_db_free(dev->dev, &cq->db);
  207. err_cq:
  208. kfree(cq);
  209. return ERR_PTR(err);
  210. }
  211. static int mlx4_alloc_resize_buf(struct mlx4_ib_dev *dev, struct mlx4_ib_cq *cq,
  212. int entries)
  213. {
  214. int err;
  215. if (cq->resize_buf)
  216. return -EBUSY;
  217. cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_ATOMIC);
  218. if (!cq->resize_buf)
  219. return -ENOMEM;
  220. err = mlx4_ib_alloc_cq_buf(dev, &cq->resize_buf->buf, entries);
  221. if (err) {
  222. kfree(cq->resize_buf);
  223. cq->resize_buf = NULL;
  224. return err;
  225. }
  226. cq->resize_buf->cqe = entries - 1;
  227. return 0;
  228. }
  229. static int mlx4_alloc_resize_umem(struct mlx4_ib_dev *dev, struct mlx4_ib_cq *cq,
  230. int entries, struct ib_udata *udata)
  231. {
  232. struct mlx4_ib_resize_cq ucmd;
  233. int err;
  234. if (cq->resize_umem)
  235. return -EBUSY;
  236. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  237. return -EFAULT;
  238. cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_ATOMIC);
  239. if (!cq->resize_buf)
  240. return -ENOMEM;
  241. err = mlx4_ib_get_cq_umem(dev, cq->umem->context, &cq->resize_buf->buf,
  242. &cq->resize_umem, ucmd.buf_addr, entries);
  243. if (err) {
  244. kfree(cq->resize_buf);
  245. cq->resize_buf = NULL;
  246. return err;
  247. }
  248. cq->resize_buf->cqe = entries - 1;
  249. return 0;
  250. }
  251. static int mlx4_ib_get_outstanding_cqes(struct mlx4_ib_cq *cq)
  252. {
  253. u32 i;
  254. i = cq->mcq.cons_index;
  255. while (get_sw_cqe(cq, i & cq->ibcq.cqe))
  256. ++i;
  257. return i - cq->mcq.cons_index;
  258. }
  259. static void mlx4_ib_cq_resize_copy_cqes(struct mlx4_ib_cq *cq)
  260. {
  261. struct mlx4_cqe *cqe, *new_cqe;
  262. int i;
  263. i = cq->mcq.cons_index;
  264. cqe = get_cqe(cq, i & cq->ibcq.cqe);
  265. while ((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) != MLX4_CQE_OPCODE_RESIZE) {
  266. new_cqe = get_cqe_from_buf(&cq->resize_buf->buf,
  267. (i + 1) & cq->resize_buf->cqe);
  268. memcpy(new_cqe, get_cqe(cq, i & cq->ibcq.cqe), sizeof(struct mlx4_cqe));
  269. new_cqe->owner_sr_opcode = (cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK) |
  270. (((i + 1) & (cq->resize_buf->cqe + 1)) ? MLX4_CQE_OWNER_MASK : 0);
  271. cqe = get_cqe(cq, ++i & cq->ibcq.cqe);
  272. }
  273. ++cq->mcq.cons_index;
  274. }
  275. int mlx4_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
  276. {
  277. struct mlx4_ib_dev *dev = to_mdev(ibcq->device);
  278. struct mlx4_ib_cq *cq = to_mcq(ibcq);
  279. struct mlx4_mtt mtt;
  280. int outst_cqe;
  281. int err;
  282. mutex_lock(&cq->resize_mutex);
  283. if (entries < 1 || entries > dev->dev->caps.max_cqes) {
  284. err = -EINVAL;
  285. goto out;
  286. }
  287. entries = roundup_pow_of_two(entries + 1);
  288. if (entries == ibcq->cqe + 1) {
  289. err = 0;
  290. goto out;
  291. }
  292. if (ibcq->uobject) {
  293. err = mlx4_alloc_resize_umem(dev, cq, entries, udata);
  294. if (err)
  295. goto out;
  296. } else {
  297. /* Can't be smaller than the number of outstanding CQEs */
  298. outst_cqe = mlx4_ib_get_outstanding_cqes(cq);
  299. if (entries < outst_cqe + 1) {
  300. err = 0;
  301. goto out;
  302. }
  303. err = mlx4_alloc_resize_buf(dev, cq, entries);
  304. if (err)
  305. goto out;
  306. }
  307. mtt = cq->buf.mtt;
  308. err = mlx4_cq_resize(dev->dev, &cq->mcq, entries, &cq->resize_buf->buf.mtt);
  309. if (err)
  310. goto err_buf;
  311. mlx4_mtt_cleanup(dev->dev, &mtt);
  312. if (ibcq->uobject) {
  313. cq->buf = cq->resize_buf->buf;
  314. cq->ibcq.cqe = cq->resize_buf->cqe;
  315. ib_umem_release(cq->umem);
  316. cq->umem = cq->resize_umem;
  317. kfree(cq->resize_buf);
  318. cq->resize_buf = NULL;
  319. cq->resize_umem = NULL;
  320. } else {
  321. struct mlx4_ib_cq_buf tmp_buf;
  322. int tmp_cqe = 0;
  323. spin_lock_irq(&cq->lock);
  324. if (cq->resize_buf) {
  325. mlx4_ib_cq_resize_copy_cqes(cq);
  326. tmp_buf = cq->buf;
  327. tmp_cqe = cq->ibcq.cqe;
  328. cq->buf = cq->resize_buf->buf;
  329. cq->ibcq.cqe = cq->resize_buf->cqe;
  330. kfree(cq->resize_buf);
  331. cq->resize_buf = NULL;
  332. }
  333. spin_unlock_irq(&cq->lock);
  334. if (tmp_cqe)
  335. mlx4_ib_free_cq_buf(dev, &tmp_buf, tmp_cqe);
  336. }
  337. goto out;
  338. err_buf:
  339. mlx4_mtt_cleanup(dev->dev, &cq->resize_buf->buf.mtt);
  340. if (!ibcq->uobject)
  341. mlx4_ib_free_cq_buf(dev, &cq->resize_buf->buf,
  342. cq->resize_buf->cqe);
  343. kfree(cq->resize_buf);
  344. cq->resize_buf = NULL;
  345. if (cq->resize_umem) {
  346. ib_umem_release(cq->resize_umem);
  347. cq->resize_umem = NULL;
  348. }
  349. out:
  350. mutex_unlock(&cq->resize_mutex);
  351. return err;
  352. }
  353. int mlx4_ib_destroy_cq(struct ib_cq *cq)
  354. {
  355. struct mlx4_ib_dev *dev = to_mdev(cq->device);
  356. struct mlx4_ib_cq *mcq = to_mcq(cq);
  357. mlx4_cq_free(dev->dev, &mcq->mcq);
  358. mlx4_mtt_cleanup(dev->dev, &mcq->buf.mtt);
  359. if (cq->uobject) {
  360. mlx4_ib_db_unmap_user(to_mucontext(cq->uobject->context), &mcq->db);
  361. ib_umem_release(mcq->umem);
  362. } else {
  363. mlx4_ib_free_cq_buf(dev, &mcq->buf, cq->cqe);
  364. mlx4_db_free(dev->dev, &mcq->db);
  365. }
  366. kfree(mcq);
  367. return 0;
  368. }
  369. static void dump_cqe(void *cqe)
  370. {
  371. __be32 *buf = cqe;
  372. pr_debug("CQE contents %08x %08x %08x %08x %08x %08x %08x %08x\n",
  373. be32_to_cpu(buf[0]), be32_to_cpu(buf[1]), be32_to_cpu(buf[2]),
  374. be32_to_cpu(buf[3]), be32_to_cpu(buf[4]), be32_to_cpu(buf[5]),
  375. be32_to_cpu(buf[6]), be32_to_cpu(buf[7]));
  376. }
  377. static void mlx4_ib_handle_error_cqe(struct mlx4_err_cqe *cqe,
  378. struct ib_wc *wc)
  379. {
  380. if (cqe->syndrome == MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR) {
  381. pr_debug("local QP operation err "
  382. "(QPN %06x, WQE index %x, vendor syndrome %02x, "
  383. "opcode = %02x)\n",
  384. be32_to_cpu(cqe->my_qpn), be16_to_cpu(cqe->wqe_index),
  385. cqe->vendor_err_syndrome,
  386. cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
  387. dump_cqe(cqe);
  388. }
  389. switch (cqe->syndrome) {
  390. case MLX4_CQE_SYNDROME_LOCAL_LENGTH_ERR:
  391. wc->status = IB_WC_LOC_LEN_ERR;
  392. break;
  393. case MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR:
  394. wc->status = IB_WC_LOC_QP_OP_ERR;
  395. break;
  396. case MLX4_CQE_SYNDROME_LOCAL_PROT_ERR:
  397. wc->status = IB_WC_LOC_PROT_ERR;
  398. break;
  399. case MLX4_CQE_SYNDROME_WR_FLUSH_ERR:
  400. wc->status = IB_WC_WR_FLUSH_ERR;
  401. break;
  402. case MLX4_CQE_SYNDROME_MW_BIND_ERR:
  403. wc->status = IB_WC_MW_BIND_ERR;
  404. break;
  405. case MLX4_CQE_SYNDROME_BAD_RESP_ERR:
  406. wc->status = IB_WC_BAD_RESP_ERR;
  407. break;
  408. case MLX4_CQE_SYNDROME_LOCAL_ACCESS_ERR:
  409. wc->status = IB_WC_LOC_ACCESS_ERR;
  410. break;
  411. case MLX4_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
  412. wc->status = IB_WC_REM_INV_REQ_ERR;
  413. break;
  414. case MLX4_CQE_SYNDROME_REMOTE_ACCESS_ERR:
  415. wc->status = IB_WC_REM_ACCESS_ERR;
  416. break;
  417. case MLX4_CQE_SYNDROME_REMOTE_OP_ERR:
  418. wc->status = IB_WC_REM_OP_ERR;
  419. break;
  420. case MLX4_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
  421. wc->status = IB_WC_RETRY_EXC_ERR;
  422. break;
  423. case MLX4_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
  424. wc->status = IB_WC_RNR_RETRY_EXC_ERR;
  425. break;
  426. case MLX4_CQE_SYNDROME_REMOTE_ABORTED_ERR:
  427. wc->status = IB_WC_REM_ABORT_ERR;
  428. break;
  429. default:
  430. wc->status = IB_WC_GENERAL_ERR;
  431. break;
  432. }
  433. wc->vendor_err = cqe->vendor_err_syndrome;
  434. }
  435. static int mlx4_ib_ipoib_csum_ok(__be16 status, __be16 checksum)
  436. {
  437. return ((status & cpu_to_be16(MLX4_CQE_STATUS_IPV4 |
  438. MLX4_CQE_STATUS_IPV4F |
  439. MLX4_CQE_STATUS_IPV4OPT |
  440. MLX4_CQE_STATUS_IPV6 |
  441. MLX4_CQE_STATUS_IPOK)) ==
  442. cpu_to_be16(MLX4_CQE_STATUS_IPV4 |
  443. MLX4_CQE_STATUS_IPOK)) &&
  444. (status & cpu_to_be16(MLX4_CQE_STATUS_UDP |
  445. MLX4_CQE_STATUS_TCP)) &&
  446. checksum == cpu_to_be16(0xffff);
  447. }
  448. static int mlx4_ib_poll_one(struct mlx4_ib_cq *cq,
  449. struct mlx4_ib_qp **cur_qp,
  450. struct ib_wc *wc)
  451. {
  452. struct mlx4_cqe *cqe;
  453. struct mlx4_qp *mqp;
  454. struct mlx4_ib_wq *wq;
  455. struct mlx4_ib_srq *srq;
  456. int is_send;
  457. int is_error;
  458. u32 g_mlpath_rqpn;
  459. u16 wqe_ctr;
  460. repoll:
  461. cqe = next_cqe_sw(cq);
  462. if (!cqe)
  463. return -EAGAIN;
  464. ++cq->mcq.cons_index;
  465. /*
  466. * Make sure we read CQ entry contents after we've checked the
  467. * ownership bit.
  468. */
  469. rmb();
  470. is_send = cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK;
  471. is_error = (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
  472. MLX4_CQE_OPCODE_ERROR;
  473. if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) == MLX4_OPCODE_NOP &&
  474. is_send)) {
  475. pr_warn("Completion for NOP opcode detected!\n");
  476. return -EINVAL;
  477. }
  478. /* Resize CQ in progress */
  479. if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) == MLX4_CQE_OPCODE_RESIZE)) {
  480. if (cq->resize_buf) {
  481. struct mlx4_ib_dev *dev = to_mdev(cq->ibcq.device);
  482. mlx4_ib_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
  483. cq->buf = cq->resize_buf->buf;
  484. cq->ibcq.cqe = cq->resize_buf->cqe;
  485. kfree(cq->resize_buf);
  486. cq->resize_buf = NULL;
  487. }
  488. goto repoll;
  489. }
  490. if (!*cur_qp ||
  491. (be32_to_cpu(cqe->vlan_my_qpn) & MLX4_CQE_QPN_MASK) != (*cur_qp)->mqp.qpn) {
  492. /*
  493. * We do not have to take the QP table lock here,
  494. * because CQs will be locked while QPs are removed
  495. * from the table.
  496. */
  497. mqp = __mlx4_qp_lookup(to_mdev(cq->ibcq.device)->dev,
  498. be32_to_cpu(cqe->vlan_my_qpn));
  499. if (unlikely(!mqp)) {
  500. pr_warn("CQ %06x with entry for unknown QPN %06x\n",
  501. cq->mcq.cqn, be32_to_cpu(cqe->vlan_my_qpn) & MLX4_CQE_QPN_MASK);
  502. return -EINVAL;
  503. }
  504. *cur_qp = to_mibqp(mqp);
  505. }
  506. wc->qp = &(*cur_qp)->ibqp;
  507. if (is_send) {
  508. wq = &(*cur_qp)->sq;
  509. if (!(*cur_qp)->sq_signal_bits) {
  510. wqe_ctr = be16_to_cpu(cqe->wqe_index);
  511. wq->tail += (u16) (wqe_ctr - (u16) wq->tail);
  512. }
  513. wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
  514. ++wq->tail;
  515. } else if ((*cur_qp)->ibqp.srq) {
  516. srq = to_msrq((*cur_qp)->ibqp.srq);
  517. wqe_ctr = be16_to_cpu(cqe->wqe_index);
  518. wc->wr_id = srq->wrid[wqe_ctr];
  519. mlx4_ib_free_srq_wqe(srq, wqe_ctr);
  520. } else {
  521. wq = &(*cur_qp)->rq;
  522. wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
  523. ++wq->tail;
  524. }
  525. if (unlikely(is_error)) {
  526. mlx4_ib_handle_error_cqe((struct mlx4_err_cqe *) cqe, wc);
  527. return 0;
  528. }
  529. wc->status = IB_WC_SUCCESS;
  530. if (is_send) {
  531. wc->wc_flags = 0;
  532. switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
  533. case MLX4_OPCODE_RDMA_WRITE_IMM:
  534. wc->wc_flags |= IB_WC_WITH_IMM;
  535. case MLX4_OPCODE_RDMA_WRITE:
  536. wc->opcode = IB_WC_RDMA_WRITE;
  537. break;
  538. case MLX4_OPCODE_SEND_IMM:
  539. wc->wc_flags |= IB_WC_WITH_IMM;
  540. case MLX4_OPCODE_SEND:
  541. case MLX4_OPCODE_SEND_INVAL:
  542. wc->opcode = IB_WC_SEND;
  543. break;
  544. case MLX4_OPCODE_RDMA_READ:
  545. wc->opcode = IB_WC_RDMA_READ;
  546. wc->byte_len = be32_to_cpu(cqe->byte_cnt);
  547. break;
  548. case MLX4_OPCODE_ATOMIC_CS:
  549. wc->opcode = IB_WC_COMP_SWAP;
  550. wc->byte_len = 8;
  551. break;
  552. case MLX4_OPCODE_ATOMIC_FA:
  553. wc->opcode = IB_WC_FETCH_ADD;
  554. wc->byte_len = 8;
  555. break;
  556. case MLX4_OPCODE_MASKED_ATOMIC_CS:
  557. wc->opcode = IB_WC_MASKED_COMP_SWAP;
  558. wc->byte_len = 8;
  559. break;
  560. case MLX4_OPCODE_MASKED_ATOMIC_FA:
  561. wc->opcode = IB_WC_MASKED_FETCH_ADD;
  562. wc->byte_len = 8;
  563. break;
  564. case MLX4_OPCODE_BIND_MW:
  565. wc->opcode = IB_WC_BIND_MW;
  566. break;
  567. case MLX4_OPCODE_LSO:
  568. wc->opcode = IB_WC_LSO;
  569. break;
  570. case MLX4_OPCODE_FMR:
  571. wc->opcode = IB_WC_FAST_REG_MR;
  572. break;
  573. case MLX4_OPCODE_LOCAL_INVAL:
  574. wc->opcode = IB_WC_LOCAL_INV;
  575. break;
  576. }
  577. } else {
  578. wc->byte_len = be32_to_cpu(cqe->byte_cnt);
  579. switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
  580. case MLX4_RECV_OPCODE_RDMA_WRITE_IMM:
  581. wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
  582. wc->wc_flags = IB_WC_WITH_IMM;
  583. wc->ex.imm_data = cqe->immed_rss_invalid;
  584. break;
  585. case MLX4_RECV_OPCODE_SEND_INVAL:
  586. wc->opcode = IB_WC_RECV;
  587. wc->wc_flags = IB_WC_WITH_INVALIDATE;
  588. wc->ex.invalidate_rkey = be32_to_cpu(cqe->immed_rss_invalid);
  589. break;
  590. case MLX4_RECV_OPCODE_SEND:
  591. wc->opcode = IB_WC_RECV;
  592. wc->wc_flags = 0;
  593. break;
  594. case MLX4_RECV_OPCODE_SEND_IMM:
  595. wc->opcode = IB_WC_RECV;
  596. wc->wc_flags = IB_WC_WITH_IMM;
  597. wc->ex.imm_data = cqe->immed_rss_invalid;
  598. break;
  599. }
  600. wc->slid = be16_to_cpu(cqe->rlid);
  601. g_mlpath_rqpn = be32_to_cpu(cqe->g_mlpath_rqpn);
  602. wc->src_qp = g_mlpath_rqpn & 0xffffff;
  603. wc->dlid_path_bits = (g_mlpath_rqpn >> 24) & 0x7f;
  604. wc->wc_flags |= g_mlpath_rqpn & 0x80000000 ? IB_WC_GRH : 0;
  605. wc->pkey_index = be32_to_cpu(cqe->immed_rss_invalid) & 0x7f;
  606. wc->wc_flags |= mlx4_ib_ipoib_csum_ok(cqe->status,
  607. cqe->checksum) ? IB_WC_IP_CSUM_OK : 0;
  608. if (rdma_port_get_link_layer(wc->qp->device,
  609. (*cur_qp)->port) == IB_LINK_LAYER_ETHERNET)
  610. wc->sl = be16_to_cpu(cqe->sl_vid) >> 13;
  611. else
  612. wc->sl = be16_to_cpu(cqe->sl_vid) >> 12;
  613. }
  614. return 0;
  615. }
  616. int mlx4_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
  617. {
  618. struct mlx4_ib_cq *cq = to_mcq(ibcq);
  619. struct mlx4_ib_qp *cur_qp = NULL;
  620. unsigned long flags;
  621. int npolled;
  622. int err = 0;
  623. spin_lock_irqsave(&cq->lock, flags);
  624. for (npolled = 0; npolled < num_entries; ++npolled) {
  625. err = mlx4_ib_poll_one(cq, &cur_qp, wc + npolled);
  626. if (err)
  627. break;
  628. }
  629. mlx4_cq_set_ci(&cq->mcq);
  630. spin_unlock_irqrestore(&cq->lock, flags);
  631. if (err == 0 || err == -EAGAIN)
  632. return npolled;
  633. else
  634. return err;
  635. }
  636. int mlx4_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
  637. {
  638. mlx4_cq_arm(&to_mcq(ibcq)->mcq,
  639. (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
  640. MLX4_CQ_DB_REQ_NOT_SOL : MLX4_CQ_DB_REQ_NOT,
  641. to_mdev(ibcq->device)->uar_map,
  642. MLX4_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->uar_lock));
  643. return 0;
  644. }
  645. void __mlx4_ib_cq_clean(struct mlx4_ib_cq *cq, u32 qpn, struct mlx4_ib_srq *srq)
  646. {
  647. u32 prod_index;
  648. int nfreed = 0;
  649. struct mlx4_cqe *cqe, *dest;
  650. u8 owner_bit;
  651. /*
  652. * First we need to find the current producer index, so we
  653. * know where to start cleaning from. It doesn't matter if HW
  654. * adds new entries after this loop -- the QP we're worried
  655. * about is already in RESET, so the new entries won't come
  656. * from our QP and therefore don't need to be checked.
  657. */
  658. for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); ++prod_index)
  659. if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
  660. break;
  661. /*
  662. * Now sweep backwards through the CQ, removing CQ entries
  663. * that match our QP by copying older entries on top of them.
  664. */
  665. while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
  666. cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
  667. if ((be32_to_cpu(cqe->vlan_my_qpn) & MLX4_CQE_QPN_MASK) == qpn) {
  668. if (srq && !(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK))
  669. mlx4_ib_free_srq_wqe(srq, be16_to_cpu(cqe->wqe_index));
  670. ++nfreed;
  671. } else if (nfreed) {
  672. dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
  673. owner_bit = dest->owner_sr_opcode & MLX4_CQE_OWNER_MASK;
  674. memcpy(dest, cqe, sizeof *cqe);
  675. dest->owner_sr_opcode = owner_bit |
  676. (dest->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
  677. }
  678. }
  679. if (nfreed) {
  680. cq->mcq.cons_index += nfreed;
  681. /*
  682. * Make sure update of buffer contents is done before
  683. * updating consumer index.
  684. */
  685. wmb();
  686. mlx4_cq_set_ci(&cq->mcq);
  687. }
  688. }
  689. void mlx4_ib_cq_clean(struct mlx4_ib_cq *cq, u32 qpn, struct mlx4_ib_srq *srq)
  690. {
  691. spin_lock_irq(&cq->lock);
  692. __mlx4_ib_cq_clean(cq, qpn, srq);
  693. spin_unlock_irq(&cq->lock);
  694. }