cq.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. printk(KERN_WARNING "mlx4_ib: 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. err = mlx4_cq_alloc(dev->dev, entries, &cq->buf.mtt, uar,
  182. cq->db.dma, &cq->mcq, vector, 0);
  183. if (err)
  184. goto err_dbmap;
  185. cq->mcq.comp = mlx4_ib_cq_comp;
  186. cq->mcq.event = mlx4_ib_cq_event;
  187. if (context)
  188. if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof (__u32))) {
  189. err = -EFAULT;
  190. goto err_dbmap;
  191. }
  192. return &cq->ibcq;
  193. err_dbmap:
  194. if (context)
  195. mlx4_ib_db_unmap_user(to_mucontext(context), &cq->db);
  196. err_mtt:
  197. mlx4_mtt_cleanup(dev->dev, &cq->buf.mtt);
  198. if (context)
  199. ib_umem_release(cq->umem);
  200. else
  201. mlx4_ib_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
  202. err_db:
  203. if (!context)
  204. mlx4_db_free(dev->dev, &cq->db);
  205. err_cq:
  206. kfree(cq);
  207. return ERR_PTR(err);
  208. }
  209. static int mlx4_alloc_resize_buf(struct mlx4_ib_dev *dev, struct mlx4_ib_cq *cq,
  210. int entries)
  211. {
  212. int err;
  213. if (cq->resize_buf)
  214. return -EBUSY;
  215. cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_ATOMIC);
  216. if (!cq->resize_buf)
  217. return -ENOMEM;
  218. err = mlx4_ib_alloc_cq_buf(dev, &cq->resize_buf->buf, entries);
  219. if (err) {
  220. kfree(cq->resize_buf);
  221. cq->resize_buf = NULL;
  222. return err;
  223. }
  224. cq->resize_buf->cqe = entries - 1;
  225. return 0;
  226. }
  227. static int mlx4_alloc_resize_umem(struct mlx4_ib_dev *dev, struct mlx4_ib_cq *cq,
  228. int entries, struct ib_udata *udata)
  229. {
  230. struct mlx4_ib_resize_cq ucmd;
  231. int err;
  232. if (cq->resize_umem)
  233. return -EBUSY;
  234. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  235. return -EFAULT;
  236. cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_ATOMIC);
  237. if (!cq->resize_buf)
  238. return -ENOMEM;
  239. err = mlx4_ib_get_cq_umem(dev, cq->umem->context, &cq->resize_buf->buf,
  240. &cq->resize_umem, ucmd.buf_addr, entries);
  241. if (err) {
  242. kfree(cq->resize_buf);
  243. cq->resize_buf = NULL;
  244. return err;
  245. }
  246. cq->resize_buf->cqe = entries - 1;
  247. return 0;
  248. }
  249. static int mlx4_ib_get_outstanding_cqes(struct mlx4_ib_cq *cq)
  250. {
  251. u32 i;
  252. i = cq->mcq.cons_index;
  253. while (get_sw_cqe(cq, i & cq->ibcq.cqe))
  254. ++i;
  255. return i - cq->mcq.cons_index;
  256. }
  257. static void mlx4_ib_cq_resize_copy_cqes(struct mlx4_ib_cq *cq)
  258. {
  259. struct mlx4_cqe *cqe, *new_cqe;
  260. int i;
  261. i = cq->mcq.cons_index;
  262. cqe = get_cqe(cq, i & cq->ibcq.cqe);
  263. while ((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) != MLX4_CQE_OPCODE_RESIZE) {
  264. new_cqe = get_cqe_from_buf(&cq->resize_buf->buf,
  265. (i + 1) & cq->resize_buf->cqe);
  266. memcpy(new_cqe, get_cqe(cq, i & cq->ibcq.cqe), sizeof(struct mlx4_cqe));
  267. new_cqe->owner_sr_opcode = (cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK) |
  268. (((i + 1) & (cq->resize_buf->cqe + 1)) ? MLX4_CQE_OWNER_MASK : 0);
  269. cqe = get_cqe(cq, ++i & cq->ibcq.cqe);
  270. }
  271. ++cq->mcq.cons_index;
  272. }
  273. int mlx4_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
  274. {
  275. struct mlx4_ib_dev *dev = to_mdev(ibcq->device);
  276. struct mlx4_ib_cq *cq = to_mcq(ibcq);
  277. struct mlx4_mtt mtt;
  278. int outst_cqe;
  279. int err;
  280. mutex_lock(&cq->resize_mutex);
  281. if (entries < 1 || entries > dev->dev->caps.max_cqes) {
  282. err = -EINVAL;
  283. goto out;
  284. }
  285. entries = roundup_pow_of_two(entries + 1);
  286. if (entries == ibcq->cqe + 1) {
  287. err = 0;
  288. goto out;
  289. }
  290. if (ibcq->uobject) {
  291. err = mlx4_alloc_resize_umem(dev, cq, entries, udata);
  292. if (err)
  293. goto out;
  294. } else {
  295. /* Can't be smaller than the number of outstanding CQEs */
  296. outst_cqe = mlx4_ib_get_outstanding_cqes(cq);
  297. if (entries < outst_cqe + 1) {
  298. err = 0;
  299. goto out;
  300. }
  301. err = mlx4_alloc_resize_buf(dev, cq, entries);
  302. if (err)
  303. goto out;
  304. }
  305. mtt = cq->buf.mtt;
  306. err = mlx4_cq_resize(dev->dev, &cq->mcq, entries, &cq->resize_buf->buf.mtt);
  307. if (err)
  308. goto err_buf;
  309. mlx4_mtt_cleanup(dev->dev, &mtt);
  310. if (ibcq->uobject) {
  311. cq->buf = cq->resize_buf->buf;
  312. cq->ibcq.cqe = cq->resize_buf->cqe;
  313. ib_umem_release(cq->umem);
  314. cq->umem = cq->resize_umem;
  315. kfree(cq->resize_buf);
  316. cq->resize_buf = NULL;
  317. cq->resize_umem = NULL;
  318. } else {
  319. struct mlx4_ib_cq_buf tmp_buf;
  320. int tmp_cqe = 0;
  321. spin_lock_irq(&cq->lock);
  322. if (cq->resize_buf) {
  323. mlx4_ib_cq_resize_copy_cqes(cq);
  324. tmp_buf = cq->buf;
  325. tmp_cqe = cq->ibcq.cqe;
  326. cq->buf = cq->resize_buf->buf;
  327. cq->ibcq.cqe = cq->resize_buf->cqe;
  328. kfree(cq->resize_buf);
  329. cq->resize_buf = NULL;
  330. }
  331. spin_unlock_irq(&cq->lock);
  332. if (tmp_cqe)
  333. mlx4_ib_free_cq_buf(dev, &tmp_buf, tmp_cqe);
  334. }
  335. goto out;
  336. err_buf:
  337. mlx4_mtt_cleanup(dev->dev, &cq->resize_buf->buf.mtt);
  338. if (!ibcq->uobject)
  339. mlx4_ib_free_cq_buf(dev, &cq->resize_buf->buf,
  340. cq->resize_buf->cqe);
  341. kfree(cq->resize_buf);
  342. cq->resize_buf = NULL;
  343. if (cq->resize_umem) {
  344. ib_umem_release(cq->resize_umem);
  345. cq->resize_umem = NULL;
  346. }
  347. out:
  348. mutex_unlock(&cq->resize_mutex);
  349. return err;
  350. }
  351. int mlx4_ib_destroy_cq(struct ib_cq *cq)
  352. {
  353. struct mlx4_ib_dev *dev = to_mdev(cq->device);
  354. struct mlx4_ib_cq *mcq = to_mcq(cq);
  355. mlx4_cq_free(dev->dev, &mcq->mcq);
  356. mlx4_mtt_cleanup(dev->dev, &mcq->buf.mtt);
  357. if (cq->uobject) {
  358. mlx4_ib_db_unmap_user(to_mucontext(cq->uobject->context), &mcq->db);
  359. ib_umem_release(mcq->umem);
  360. } else {
  361. mlx4_ib_free_cq_buf(dev, &mcq->buf, cq->cqe);
  362. mlx4_db_free(dev->dev, &mcq->db);
  363. }
  364. kfree(mcq);
  365. return 0;
  366. }
  367. static void dump_cqe(void *cqe)
  368. {
  369. __be32 *buf = cqe;
  370. printk(KERN_DEBUG "CQE contents %08x %08x %08x %08x %08x %08x %08x %08x\n",
  371. be32_to_cpu(buf[0]), be32_to_cpu(buf[1]), be32_to_cpu(buf[2]),
  372. be32_to_cpu(buf[3]), be32_to_cpu(buf[4]), be32_to_cpu(buf[5]),
  373. be32_to_cpu(buf[6]), be32_to_cpu(buf[7]));
  374. }
  375. static void mlx4_ib_handle_error_cqe(struct mlx4_err_cqe *cqe,
  376. struct ib_wc *wc)
  377. {
  378. if (cqe->syndrome == MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR) {
  379. printk(KERN_DEBUG "local QP operation err "
  380. "(QPN %06x, WQE index %x, vendor syndrome %02x, "
  381. "opcode = %02x)\n",
  382. be32_to_cpu(cqe->my_qpn), be16_to_cpu(cqe->wqe_index),
  383. cqe->vendor_err_syndrome,
  384. cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
  385. dump_cqe(cqe);
  386. }
  387. switch (cqe->syndrome) {
  388. case MLX4_CQE_SYNDROME_LOCAL_LENGTH_ERR:
  389. wc->status = IB_WC_LOC_LEN_ERR;
  390. break;
  391. case MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR:
  392. wc->status = IB_WC_LOC_QP_OP_ERR;
  393. break;
  394. case MLX4_CQE_SYNDROME_LOCAL_PROT_ERR:
  395. wc->status = IB_WC_LOC_PROT_ERR;
  396. break;
  397. case MLX4_CQE_SYNDROME_WR_FLUSH_ERR:
  398. wc->status = IB_WC_WR_FLUSH_ERR;
  399. break;
  400. case MLX4_CQE_SYNDROME_MW_BIND_ERR:
  401. wc->status = IB_WC_MW_BIND_ERR;
  402. break;
  403. case MLX4_CQE_SYNDROME_BAD_RESP_ERR:
  404. wc->status = IB_WC_BAD_RESP_ERR;
  405. break;
  406. case MLX4_CQE_SYNDROME_LOCAL_ACCESS_ERR:
  407. wc->status = IB_WC_LOC_ACCESS_ERR;
  408. break;
  409. case MLX4_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
  410. wc->status = IB_WC_REM_INV_REQ_ERR;
  411. break;
  412. case MLX4_CQE_SYNDROME_REMOTE_ACCESS_ERR:
  413. wc->status = IB_WC_REM_ACCESS_ERR;
  414. break;
  415. case MLX4_CQE_SYNDROME_REMOTE_OP_ERR:
  416. wc->status = IB_WC_REM_OP_ERR;
  417. break;
  418. case MLX4_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
  419. wc->status = IB_WC_RETRY_EXC_ERR;
  420. break;
  421. case MLX4_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
  422. wc->status = IB_WC_RNR_RETRY_EXC_ERR;
  423. break;
  424. case MLX4_CQE_SYNDROME_REMOTE_ABORTED_ERR:
  425. wc->status = IB_WC_REM_ABORT_ERR;
  426. break;
  427. default:
  428. wc->status = IB_WC_GENERAL_ERR;
  429. break;
  430. }
  431. wc->vendor_err = cqe->vendor_err_syndrome;
  432. }
  433. static int mlx4_ib_ipoib_csum_ok(__be16 status, __be16 checksum)
  434. {
  435. return ((status & cpu_to_be16(MLX4_CQE_STATUS_IPV4 |
  436. MLX4_CQE_STATUS_IPV4F |
  437. MLX4_CQE_STATUS_IPV4OPT |
  438. MLX4_CQE_STATUS_IPV6 |
  439. MLX4_CQE_STATUS_IPOK)) ==
  440. cpu_to_be16(MLX4_CQE_STATUS_IPV4 |
  441. MLX4_CQE_STATUS_IPOK)) &&
  442. (status & cpu_to_be16(MLX4_CQE_STATUS_UDP |
  443. MLX4_CQE_STATUS_TCP)) &&
  444. checksum == cpu_to_be16(0xffff);
  445. }
  446. static int mlx4_ib_poll_one(struct mlx4_ib_cq *cq,
  447. struct mlx4_ib_qp **cur_qp,
  448. struct ib_wc *wc)
  449. {
  450. struct mlx4_cqe *cqe;
  451. struct mlx4_qp *mqp;
  452. struct mlx4_ib_wq *wq;
  453. struct mlx4_ib_srq *srq;
  454. int is_send;
  455. int is_error;
  456. u32 g_mlpath_rqpn;
  457. u16 wqe_ctr;
  458. repoll:
  459. cqe = next_cqe_sw(cq);
  460. if (!cqe)
  461. return -EAGAIN;
  462. ++cq->mcq.cons_index;
  463. /*
  464. * Make sure we read CQ entry contents after we've checked the
  465. * ownership bit.
  466. */
  467. rmb();
  468. is_send = cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK;
  469. is_error = (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
  470. MLX4_CQE_OPCODE_ERROR;
  471. if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) == MLX4_OPCODE_NOP &&
  472. is_send)) {
  473. printk(KERN_WARNING "Completion for NOP opcode detected!\n");
  474. return -EINVAL;
  475. }
  476. /* Resize CQ in progress */
  477. if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) == MLX4_CQE_OPCODE_RESIZE)) {
  478. if (cq->resize_buf) {
  479. struct mlx4_ib_dev *dev = to_mdev(cq->ibcq.device);
  480. mlx4_ib_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
  481. cq->buf = cq->resize_buf->buf;
  482. cq->ibcq.cqe = cq->resize_buf->cqe;
  483. kfree(cq->resize_buf);
  484. cq->resize_buf = NULL;
  485. }
  486. goto repoll;
  487. }
  488. if (!*cur_qp ||
  489. (be32_to_cpu(cqe->vlan_my_qpn) & MLX4_CQE_QPN_MASK) != (*cur_qp)->mqp.qpn) {
  490. /*
  491. * We do not have to take the QP table lock here,
  492. * because CQs will be locked while QPs are removed
  493. * from the table.
  494. */
  495. mqp = __mlx4_qp_lookup(to_mdev(cq->ibcq.device)->dev,
  496. be32_to_cpu(cqe->vlan_my_qpn));
  497. if (unlikely(!mqp)) {
  498. printk(KERN_WARNING "CQ %06x with entry for unknown QPN %06x\n",
  499. cq->mcq.cqn, be32_to_cpu(cqe->vlan_my_qpn) & MLX4_CQE_QPN_MASK);
  500. return -EINVAL;
  501. }
  502. *cur_qp = to_mibqp(mqp);
  503. }
  504. wc->qp = &(*cur_qp)->ibqp;
  505. if (is_send) {
  506. wq = &(*cur_qp)->sq;
  507. if (!(*cur_qp)->sq_signal_bits) {
  508. wqe_ctr = be16_to_cpu(cqe->wqe_index);
  509. wq->tail += (u16) (wqe_ctr - (u16) wq->tail);
  510. }
  511. wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
  512. ++wq->tail;
  513. } else if ((*cur_qp)->ibqp.srq) {
  514. srq = to_msrq((*cur_qp)->ibqp.srq);
  515. wqe_ctr = be16_to_cpu(cqe->wqe_index);
  516. wc->wr_id = srq->wrid[wqe_ctr];
  517. mlx4_ib_free_srq_wqe(srq, wqe_ctr);
  518. } else {
  519. wq = &(*cur_qp)->rq;
  520. wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
  521. ++wq->tail;
  522. }
  523. if (unlikely(is_error)) {
  524. mlx4_ib_handle_error_cqe((struct mlx4_err_cqe *) cqe, wc);
  525. return 0;
  526. }
  527. wc->status = IB_WC_SUCCESS;
  528. if (is_send) {
  529. wc->wc_flags = 0;
  530. switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
  531. case MLX4_OPCODE_RDMA_WRITE_IMM:
  532. wc->wc_flags |= IB_WC_WITH_IMM;
  533. case MLX4_OPCODE_RDMA_WRITE:
  534. wc->opcode = IB_WC_RDMA_WRITE;
  535. break;
  536. case MLX4_OPCODE_SEND_IMM:
  537. wc->wc_flags |= IB_WC_WITH_IMM;
  538. case MLX4_OPCODE_SEND:
  539. case MLX4_OPCODE_SEND_INVAL:
  540. wc->opcode = IB_WC_SEND;
  541. break;
  542. case MLX4_OPCODE_RDMA_READ:
  543. wc->opcode = IB_WC_RDMA_READ;
  544. wc->byte_len = be32_to_cpu(cqe->byte_cnt);
  545. break;
  546. case MLX4_OPCODE_ATOMIC_CS:
  547. wc->opcode = IB_WC_COMP_SWAP;
  548. wc->byte_len = 8;
  549. break;
  550. case MLX4_OPCODE_ATOMIC_FA:
  551. wc->opcode = IB_WC_FETCH_ADD;
  552. wc->byte_len = 8;
  553. break;
  554. case MLX4_OPCODE_MASKED_ATOMIC_CS:
  555. wc->opcode = IB_WC_MASKED_COMP_SWAP;
  556. wc->byte_len = 8;
  557. break;
  558. case MLX4_OPCODE_MASKED_ATOMIC_FA:
  559. wc->opcode = IB_WC_MASKED_FETCH_ADD;
  560. wc->byte_len = 8;
  561. break;
  562. case MLX4_OPCODE_BIND_MW:
  563. wc->opcode = IB_WC_BIND_MW;
  564. break;
  565. case MLX4_OPCODE_LSO:
  566. wc->opcode = IB_WC_LSO;
  567. break;
  568. case MLX4_OPCODE_FMR:
  569. wc->opcode = IB_WC_FAST_REG_MR;
  570. break;
  571. case MLX4_OPCODE_LOCAL_INVAL:
  572. wc->opcode = IB_WC_LOCAL_INV;
  573. break;
  574. }
  575. } else {
  576. wc->byte_len = be32_to_cpu(cqe->byte_cnt);
  577. switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
  578. case MLX4_RECV_OPCODE_RDMA_WRITE_IMM:
  579. wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
  580. wc->wc_flags = IB_WC_WITH_IMM;
  581. wc->ex.imm_data = cqe->immed_rss_invalid;
  582. break;
  583. case MLX4_RECV_OPCODE_SEND_INVAL:
  584. wc->opcode = IB_WC_RECV;
  585. wc->wc_flags = IB_WC_WITH_INVALIDATE;
  586. wc->ex.invalidate_rkey = be32_to_cpu(cqe->immed_rss_invalid);
  587. break;
  588. case MLX4_RECV_OPCODE_SEND:
  589. wc->opcode = IB_WC_RECV;
  590. wc->wc_flags = 0;
  591. break;
  592. case MLX4_RECV_OPCODE_SEND_IMM:
  593. wc->opcode = IB_WC_RECV;
  594. wc->wc_flags = IB_WC_WITH_IMM;
  595. wc->ex.imm_data = cqe->immed_rss_invalid;
  596. break;
  597. }
  598. wc->slid = be16_to_cpu(cqe->rlid);
  599. g_mlpath_rqpn = be32_to_cpu(cqe->g_mlpath_rqpn);
  600. wc->src_qp = g_mlpath_rqpn & 0xffffff;
  601. wc->dlid_path_bits = (g_mlpath_rqpn >> 24) & 0x7f;
  602. wc->wc_flags |= g_mlpath_rqpn & 0x80000000 ? IB_WC_GRH : 0;
  603. wc->pkey_index = be32_to_cpu(cqe->immed_rss_invalid) & 0x7f;
  604. wc->wc_flags |= mlx4_ib_ipoib_csum_ok(cqe->status,
  605. cqe->checksum) ? IB_WC_IP_CSUM_OK : 0;
  606. if (rdma_port_get_link_layer(wc->qp->device,
  607. (*cur_qp)->port) == IB_LINK_LAYER_ETHERNET)
  608. wc->sl = be16_to_cpu(cqe->sl_vid) >> 13;
  609. else
  610. wc->sl = be16_to_cpu(cqe->sl_vid) >> 12;
  611. }
  612. return 0;
  613. }
  614. int mlx4_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
  615. {
  616. struct mlx4_ib_cq *cq = to_mcq(ibcq);
  617. struct mlx4_ib_qp *cur_qp = NULL;
  618. unsigned long flags;
  619. int npolled;
  620. int err = 0;
  621. spin_lock_irqsave(&cq->lock, flags);
  622. for (npolled = 0; npolled < num_entries; ++npolled) {
  623. err = mlx4_ib_poll_one(cq, &cur_qp, wc + npolled);
  624. if (err)
  625. break;
  626. }
  627. mlx4_cq_set_ci(&cq->mcq);
  628. spin_unlock_irqrestore(&cq->lock, flags);
  629. if (err == 0 || err == -EAGAIN)
  630. return npolled;
  631. else
  632. return err;
  633. }
  634. int mlx4_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
  635. {
  636. mlx4_cq_arm(&to_mcq(ibcq)->mcq,
  637. (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
  638. MLX4_CQ_DB_REQ_NOT_SOL : MLX4_CQ_DB_REQ_NOT,
  639. to_mdev(ibcq->device)->uar_map,
  640. MLX4_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->uar_lock));
  641. return 0;
  642. }
  643. void __mlx4_ib_cq_clean(struct mlx4_ib_cq *cq, u32 qpn, struct mlx4_ib_srq *srq)
  644. {
  645. u32 prod_index;
  646. int nfreed = 0;
  647. struct mlx4_cqe *cqe, *dest;
  648. u8 owner_bit;
  649. /*
  650. * First we need to find the current producer index, so we
  651. * know where to start cleaning from. It doesn't matter if HW
  652. * adds new entries after this loop -- the QP we're worried
  653. * about is already in RESET, so the new entries won't come
  654. * from our QP and therefore don't need to be checked.
  655. */
  656. for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); ++prod_index)
  657. if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
  658. break;
  659. /*
  660. * Now sweep backwards through the CQ, removing CQ entries
  661. * that match our QP by copying older entries on top of them.
  662. */
  663. while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
  664. cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
  665. if ((be32_to_cpu(cqe->vlan_my_qpn) & MLX4_CQE_QPN_MASK) == qpn) {
  666. if (srq && !(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK))
  667. mlx4_ib_free_srq_wqe(srq, be16_to_cpu(cqe->wqe_index));
  668. ++nfreed;
  669. } else if (nfreed) {
  670. dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
  671. owner_bit = dest->owner_sr_opcode & MLX4_CQE_OWNER_MASK;
  672. memcpy(dest, cqe, sizeof *cqe);
  673. dest->owner_sr_opcode = owner_bit |
  674. (dest->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
  675. }
  676. }
  677. if (nfreed) {
  678. cq->mcq.cons_index += nfreed;
  679. /*
  680. * Make sure update of buffer contents is done before
  681. * updating consumer index.
  682. */
  683. wmb();
  684. mlx4_cq_set_ci(&cq->mcq);
  685. }
  686. }
  687. void mlx4_ib_cq_clean(struct mlx4_ib_cq *cq, u32 qpn, struct mlx4_ib_srq *srq)
  688. {
  689. spin_lock_irq(&cq->lock);
  690. __mlx4_ib_cq_clean(cq, qpn, srq);
  691. spin_unlock_irq(&cq->lock);
  692. }