cq.c 20 KB

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