cq.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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/mlx4/srq.h>
  36. #include <linux/slab.h>
  37. #include "mlx4_ib.h"
  38. #include <rdma/mlx4-abi.h>
  39. #include <rdma/uverbs_ioctl.h>
  40. static void mlx4_ib_cq_comp(struct mlx4_cq *cq)
  41. {
  42. struct ib_cq *ibcq = &to_mibcq(cq)->ibcq;
  43. ibcq->comp_handler(ibcq, ibcq->cq_context);
  44. }
  45. static void mlx4_ib_cq_event(struct mlx4_cq *cq, enum mlx4_event type)
  46. {
  47. struct ib_event event;
  48. struct ib_cq *ibcq;
  49. if (type != MLX4_EVENT_TYPE_CQ_ERROR) {
  50. pr_warn("Unexpected event type %d "
  51. "on CQ %06x\n", type, cq->cqn);
  52. return;
  53. }
  54. ibcq = &to_mibcq(cq)->ibcq;
  55. if (ibcq->event_handler) {
  56. event.device = ibcq->device;
  57. event.event = IB_EVENT_CQ_ERR;
  58. event.element.cq = ibcq;
  59. ibcq->event_handler(&event, ibcq->cq_context);
  60. }
  61. }
  62. static void *get_cqe_from_buf(struct mlx4_ib_cq_buf *buf, int n)
  63. {
  64. return mlx4_buf_offset(&buf->buf, n * buf->entry_size);
  65. }
  66. static void *get_cqe(struct mlx4_ib_cq *cq, int n)
  67. {
  68. return get_cqe_from_buf(&cq->buf, n);
  69. }
  70. static void *get_sw_cqe(struct mlx4_ib_cq *cq, int n)
  71. {
  72. struct mlx4_cqe *cqe = get_cqe(cq, n & cq->ibcq.cqe);
  73. struct mlx4_cqe *tcqe = ((cq->buf.entry_size == 64) ? (cqe + 1) : cqe);
  74. return (!!(tcqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
  75. !!(n & (cq->ibcq.cqe + 1))) ? NULL : cqe;
  76. }
  77. static struct mlx4_cqe *next_cqe_sw(struct mlx4_ib_cq *cq)
  78. {
  79. return get_sw_cqe(cq, cq->mcq.cons_index);
  80. }
  81. int mlx4_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
  82. {
  83. struct mlx4_ib_cq *mcq = to_mcq(cq);
  84. struct mlx4_ib_dev *dev = to_mdev(cq->device);
  85. return mlx4_cq_modify(dev->dev, &mcq->mcq, cq_count, cq_period);
  86. }
  87. static int mlx4_ib_alloc_cq_buf(struct mlx4_ib_dev *dev, struct mlx4_ib_cq_buf *buf, int nent)
  88. {
  89. int err;
  90. err = mlx4_buf_alloc(dev->dev, nent * dev->dev->caps.cqe_size,
  91. PAGE_SIZE * 2, &buf->buf);
  92. if (err)
  93. goto out;
  94. buf->entry_size = dev->dev->caps.cqe_size;
  95. err = mlx4_mtt_init(dev->dev, buf->buf.npages, buf->buf.page_shift,
  96. &buf->mtt);
  97. if (err)
  98. goto err_buf;
  99. err = mlx4_buf_write_mtt(dev->dev, &buf->mtt, &buf->buf);
  100. if (err)
  101. goto err_mtt;
  102. return 0;
  103. err_mtt:
  104. mlx4_mtt_cleanup(dev->dev, &buf->mtt);
  105. err_buf:
  106. mlx4_buf_free(dev->dev, nent * buf->entry_size, &buf->buf);
  107. out:
  108. return err;
  109. }
  110. static void mlx4_ib_free_cq_buf(struct mlx4_ib_dev *dev, struct mlx4_ib_cq_buf *buf, int cqe)
  111. {
  112. mlx4_buf_free(dev->dev, (cqe + 1) * buf->entry_size, &buf->buf);
  113. }
  114. static int mlx4_ib_get_cq_umem(struct mlx4_ib_dev *dev,
  115. struct mlx4_ib_cq_buf *buf,
  116. struct ib_umem **umem, u64 buf_addr, int cqe)
  117. {
  118. int err;
  119. int cqe_size = dev->dev->caps.cqe_size;
  120. int shift;
  121. int n;
  122. *umem = ib_umem_get(&dev->ib_dev, buf_addr, cqe * cqe_size,
  123. IB_ACCESS_LOCAL_WRITE);
  124. if (IS_ERR(*umem))
  125. return PTR_ERR(*umem);
  126. shift = mlx4_ib_umem_calc_optimal_mtt_size(*umem, 0, &n);
  127. err = mlx4_mtt_init(dev->dev, n, shift, &buf->mtt);
  128. if (err)
  129. goto err_buf;
  130. err = mlx4_ib_umem_write_mtt(dev, &buf->mtt, *umem);
  131. if (err)
  132. goto err_mtt;
  133. return 0;
  134. err_mtt:
  135. mlx4_mtt_cleanup(dev->dev, &buf->mtt);
  136. err_buf:
  137. ib_umem_release(*umem);
  138. return err;
  139. }
  140. #define CQ_CREATE_FLAGS_SUPPORTED IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION
  141. int mlx4_ib_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
  142. struct ib_udata *udata)
  143. {
  144. struct ib_device *ibdev = ibcq->device;
  145. int entries = attr->cqe;
  146. int vector = attr->comp_vector;
  147. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  148. struct mlx4_ib_cq *cq = to_mcq(ibcq);
  149. struct mlx4_uar *uar;
  150. void *buf_addr;
  151. int err;
  152. struct mlx4_ib_ucontext *context = rdma_udata_to_drv_context(
  153. udata, struct mlx4_ib_ucontext, ibucontext);
  154. if (entries < 1 || entries > dev->dev->caps.max_cqes)
  155. return -EINVAL;
  156. if (attr->flags & ~CQ_CREATE_FLAGS_SUPPORTED)
  157. return -EINVAL;
  158. entries = roundup_pow_of_two(entries + 1);
  159. cq->ibcq.cqe = entries - 1;
  160. mutex_init(&cq->resize_mutex);
  161. spin_lock_init(&cq->lock);
  162. cq->resize_buf = NULL;
  163. cq->resize_umem = NULL;
  164. cq->create_flags = attr->flags;
  165. INIT_LIST_HEAD(&cq->send_qp_list);
  166. INIT_LIST_HEAD(&cq->recv_qp_list);
  167. if (udata) {
  168. struct mlx4_ib_create_cq ucmd;
  169. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  170. err = -EFAULT;
  171. goto err_cq;
  172. }
  173. buf_addr = (void *)(unsigned long)ucmd.buf_addr;
  174. err = mlx4_ib_get_cq_umem(dev, &cq->buf, &cq->umem,
  175. ucmd.buf_addr, entries);
  176. if (err)
  177. goto err_cq;
  178. err = mlx4_ib_db_map_user(udata, ucmd.db_addr, &cq->db);
  179. if (err)
  180. goto err_mtt;
  181. uar = &context->uar;
  182. cq->mcq.usage = MLX4_RES_USAGE_USER_VERBS;
  183. } else {
  184. err = mlx4_db_alloc(dev->dev, &cq->db, 1);
  185. if (err)
  186. goto err_cq;
  187. cq->mcq.set_ci_db = cq->db.db;
  188. cq->mcq.arm_db = cq->db.db + 1;
  189. *cq->mcq.set_ci_db = 0;
  190. *cq->mcq.arm_db = 0;
  191. err = mlx4_ib_alloc_cq_buf(dev, &cq->buf, entries);
  192. if (err)
  193. goto err_db;
  194. buf_addr = &cq->buf.buf;
  195. uar = &dev->priv_uar;
  196. cq->mcq.usage = MLX4_RES_USAGE_DRIVER;
  197. }
  198. if (dev->eq_table)
  199. vector = dev->eq_table[vector % ibdev->num_comp_vectors];
  200. err = mlx4_cq_alloc(dev->dev, entries, &cq->buf.mtt, uar, cq->db.dma,
  201. &cq->mcq, vector, 0,
  202. !!(cq->create_flags &
  203. IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION),
  204. buf_addr, !!udata);
  205. if (err)
  206. goto err_dbmap;
  207. if (udata)
  208. cq->mcq.tasklet_ctx.comp = mlx4_ib_cq_comp;
  209. else
  210. cq->mcq.comp = mlx4_ib_cq_comp;
  211. cq->mcq.event = mlx4_ib_cq_event;
  212. if (udata)
  213. if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof (__u32))) {
  214. err = -EFAULT;
  215. goto err_cq_free;
  216. }
  217. return 0;
  218. err_cq_free:
  219. mlx4_cq_free(dev->dev, &cq->mcq);
  220. err_dbmap:
  221. if (udata)
  222. mlx4_ib_db_unmap_user(context, &cq->db);
  223. err_mtt:
  224. mlx4_mtt_cleanup(dev->dev, &cq->buf.mtt);
  225. ib_umem_release(cq->umem);
  226. if (!udata)
  227. mlx4_ib_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
  228. err_db:
  229. if (!udata)
  230. mlx4_db_free(dev->dev, &cq->db);
  231. err_cq:
  232. return err;
  233. }
  234. static int mlx4_alloc_resize_buf(struct mlx4_ib_dev *dev, struct mlx4_ib_cq *cq,
  235. int entries)
  236. {
  237. int err;
  238. if (cq->resize_buf)
  239. return -EBUSY;
  240. cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_KERNEL);
  241. if (!cq->resize_buf)
  242. return -ENOMEM;
  243. err = mlx4_ib_alloc_cq_buf(dev, &cq->resize_buf->buf, entries);
  244. if (err) {
  245. kfree(cq->resize_buf);
  246. cq->resize_buf = NULL;
  247. return err;
  248. }
  249. cq->resize_buf->cqe = entries - 1;
  250. return 0;
  251. }
  252. static int mlx4_alloc_resize_umem(struct mlx4_ib_dev *dev, struct mlx4_ib_cq *cq,
  253. int entries, struct ib_udata *udata)
  254. {
  255. struct mlx4_ib_resize_cq ucmd;
  256. int err;
  257. if (cq->resize_umem)
  258. return -EBUSY;
  259. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  260. return -EFAULT;
  261. cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_KERNEL);
  262. if (!cq->resize_buf)
  263. return -ENOMEM;
  264. err = mlx4_ib_get_cq_umem(dev, &cq->resize_buf->buf, &cq->resize_umem,
  265. ucmd.buf_addr, entries);
  266. if (err) {
  267. kfree(cq->resize_buf);
  268. cq->resize_buf = NULL;
  269. return err;
  270. }
  271. cq->resize_buf->cqe = entries - 1;
  272. return 0;
  273. }
  274. static int mlx4_ib_get_outstanding_cqes(struct mlx4_ib_cq *cq)
  275. {
  276. u32 i;
  277. i = cq->mcq.cons_index;
  278. while (get_sw_cqe(cq, i))
  279. ++i;
  280. return i - cq->mcq.cons_index;
  281. }
  282. static void mlx4_ib_cq_resize_copy_cqes(struct mlx4_ib_cq *cq)
  283. {
  284. struct mlx4_cqe *cqe, *new_cqe;
  285. int i;
  286. int cqe_size = cq->buf.entry_size;
  287. int cqe_inc = cqe_size == 64 ? 1 : 0;
  288. i = cq->mcq.cons_index;
  289. cqe = get_cqe(cq, i & cq->ibcq.cqe);
  290. cqe += cqe_inc;
  291. while ((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) != MLX4_CQE_OPCODE_RESIZE) {
  292. new_cqe = get_cqe_from_buf(&cq->resize_buf->buf,
  293. (i + 1) & cq->resize_buf->cqe);
  294. memcpy(new_cqe, get_cqe(cq, i & cq->ibcq.cqe), cqe_size);
  295. new_cqe += cqe_inc;
  296. new_cqe->owner_sr_opcode = (cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK) |
  297. (((i + 1) & (cq->resize_buf->cqe + 1)) ? MLX4_CQE_OWNER_MASK : 0);
  298. cqe = get_cqe(cq, ++i & cq->ibcq.cqe);
  299. cqe += cqe_inc;
  300. }
  301. ++cq->mcq.cons_index;
  302. }
  303. int mlx4_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
  304. {
  305. struct mlx4_ib_dev *dev = to_mdev(ibcq->device);
  306. struct mlx4_ib_cq *cq = to_mcq(ibcq);
  307. struct mlx4_mtt mtt;
  308. int outst_cqe;
  309. int err;
  310. mutex_lock(&cq->resize_mutex);
  311. if (entries < 1 || entries > dev->dev->caps.max_cqes) {
  312. err = -EINVAL;
  313. goto out;
  314. }
  315. entries = roundup_pow_of_two(entries + 1);
  316. if (entries == ibcq->cqe + 1) {
  317. err = 0;
  318. goto out;
  319. }
  320. if (entries > dev->dev->caps.max_cqes + 1) {
  321. err = -EINVAL;
  322. goto out;
  323. }
  324. if (ibcq->uobject) {
  325. err = mlx4_alloc_resize_umem(dev, cq, entries, udata);
  326. if (err)
  327. goto out;
  328. } else {
  329. /* Can't be smaller than the number of outstanding CQEs */
  330. outst_cqe = mlx4_ib_get_outstanding_cqes(cq);
  331. if (entries < outst_cqe + 1) {
  332. err = -EINVAL;
  333. goto out;
  334. }
  335. err = mlx4_alloc_resize_buf(dev, cq, entries);
  336. if (err)
  337. goto out;
  338. }
  339. mtt = cq->buf.mtt;
  340. err = mlx4_cq_resize(dev->dev, &cq->mcq, entries, &cq->resize_buf->buf.mtt);
  341. if (err)
  342. goto err_buf;
  343. mlx4_mtt_cleanup(dev->dev, &mtt);
  344. if (ibcq->uobject) {
  345. cq->buf = cq->resize_buf->buf;
  346. cq->ibcq.cqe = cq->resize_buf->cqe;
  347. ib_umem_release(cq->umem);
  348. cq->umem = cq->resize_umem;
  349. kfree(cq->resize_buf);
  350. cq->resize_buf = NULL;
  351. cq->resize_umem = NULL;
  352. } else {
  353. struct mlx4_ib_cq_buf tmp_buf;
  354. int tmp_cqe = 0;
  355. spin_lock_irq(&cq->lock);
  356. if (cq->resize_buf) {
  357. mlx4_ib_cq_resize_copy_cqes(cq);
  358. tmp_buf = cq->buf;
  359. tmp_cqe = cq->ibcq.cqe;
  360. cq->buf = cq->resize_buf->buf;
  361. cq->ibcq.cqe = cq->resize_buf->cqe;
  362. kfree(cq->resize_buf);
  363. cq->resize_buf = NULL;
  364. }
  365. spin_unlock_irq(&cq->lock);
  366. if (tmp_cqe)
  367. mlx4_ib_free_cq_buf(dev, &tmp_buf, tmp_cqe);
  368. }
  369. goto out;
  370. err_buf:
  371. mlx4_mtt_cleanup(dev->dev, &cq->resize_buf->buf.mtt);
  372. if (!ibcq->uobject)
  373. mlx4_ib_free_cq_buf(dev, &cq->resize_buf->buf,
  374. cq->resize_buf->cqe);
  375. kfree(cq->resize_buf);
  376. cq->resize_buf = NULL;
  377. ib_umem_release(cq->resize_umem);
  378. cq->resize_umem = NULL;
  379. out:
  380. mutex_unlock(&cq->resize_mutex);
  381. return err;
  382. }
  383. int mlx4_ib_destroy_cq(struct ib_cq *cq, struct ib_udata *udata)
  384. {
  385. struct mlx4_ib_dev *dev = to_mdev(cq->device);
  386. struct mlx4_ib_cq *mcq = to_mcq(cq);
  387. mlx4_cq_free(dev->dev, &mcq->mcq);
  388. mlx4_mtt_cleanup(dev->dev, &mcq->buf.mtt);
  389. if (udata) {
  390. mlx4_ib_db_unmap_user(
  391. rdma_udata_to_drv_context(
  392. udata,
  393. struct mlx4_ib_ucontext,
  394. ibucontext),
  395. &mcq->db);
  396. } else {
  397. mlx4_ib_free_cq_buf(dev, &mcq->buf, cq->cqe);
  398. mlx4_db_free(dev->dev, &mcq->db);
  399. }
  400. ib_umem_release(mcq->umem);
  401. return 0;
  402. }
  403. static void dump_cqe(void *cqe)
  404. {
  405. __be32 *buf = cqe;
  406. pr_debug("CQE contents %08x %08x %08x %08x %08x %08x %08x %08x\n",
  407. be32_to_cpu(buf[0]), be32_to_cpu(buf[1]), be32_to_cpu(buf[2]),
  408. be32_to_cpu(buf[3]), be32_to_cpu(buf[4]), be32_to_cpu(buf[5]),
  409. be32_to_cpu(buf[6]), be32_to_cpu(buf[7]));
  410. }
  411. static void mlx4_ib_handle_error_cqe(struct mlx4_err_cqe *cqe,
  412. struct ib_wc *wc)
  413. {
  414. if (cqe->syndrome == MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR) {
  415. pr_debug("local QP operation err "
  416. "(QPN %06x, WQE index %x, vendor syndrome %02x, "
  417. "opcode = %02x)\n",
  418. be32_to_cpu(cqe->my_qpn), be16_to_cpu(cqe->wqe_index),
  419. cqe->vendor_err_syndrome,
  420. cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
  421. dump_cqe(cqe);
  422. }
  423. switch (cqe->syndrome) {
  424. case MLX4_CQE_SYNDROME_LOCAL_LENGTH_ERR:
  425. wc->status = IB_WC_LOC_LEN_ERR;
  426. break;
  427. case MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR:
  428. wc->status = IB_WC_LOC_QP_OP_ERR;
  429. break;
  430. case MLX4_CQE_SYNDROME_LOCAL_PROT_ERR:
  431. wc->status = IB_WC_LOC_PROT_ERR;
  432. break;
  433. case MLX4_CQE_SYNDROME_WR_FLUSH_ERR:
  434. wc->status = IB_WC_WR_FLUSH_ERR;
  435. break;
  436. case MLX4_CQE_SYNDROME_MW_BIND_ERR:
  437. wc->status = IB_WC_MW_BIND_ERR;
  438. break;
  439. case MLX4_CQE_SYNDROME_BAD_RESP_ERR:
  440. wc->status = IB_WC_BAD_RESP_ERR;
  441. break;
  442. case MLX4_CQE_SYNDROME_LOCAL_ACCESS_ERR:
  443. wc->status = IB_WC_LOC_ACCESS_ERR;
  444. break;
  445. case MLX4_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
  446. wc->status = IB_WC_REM_INV_REQ_ERR;
  447. break;
  448. case MLX4_CQE_SYNDROME_REMOTE_ACCESS_ERR:
  449. wc->status = IB_WC_REM_ACCESS_ERR;
  450. break;
  451. case MLX4_CQE_SYNDROME_REMOTE_OP_ERR:
  452. wc->status = IB_WC_REM_OP_ERR;
  453. break;
  454. case MLX4_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
  455. wc->status = IB_WC_RETRY_EXC_ERR;
  456. break;
  457. case MLX4_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
  458. wc->status = IB_WC_RNR_RETRY_EXC_ERR;
  459. break;
  460. case MLX4_CQE_SYNDROME_REMOTE_ABORTED_ERR:
  461. wc->status = IB_WC_REM_ABORT_ERR;
  462. break;
  463. default:
  464. wc->status = IB_WC_GENERAL_ERR;
  465. break;
  466. }
  467. wc->vendor_err = cqe->vendor_err_syndrome;
  468. }
  469. static int mlx4_ib_ipoib_csum_ok(__be16 status, u8 badfcs_enc, __be16 checksum)
  470. {
  471. return ((badfcs_enc & MLX4_CQE_STATUS_L4_CSUM) ||
  472. ((status & cpu_to_be16(MLX4_CQE_STATUS_IPOK)) &&
  473. (status & cpu_to_be16(MLX4_CQE_STATUS_TCP |
  474. MLX4_CQE_STATUS_UDP)) &&
  475. (checksum == cpu_to_be16(0xffff))));
  476. }
  477. static void use_tunnel_data(struct mlx4_ib_qp *qp, struct mlx4_ib_cq *cq, struct ib_wc *wc,
  478. unsigned tail, struct mlx4_cqe *cqe, int is_eth)
  479. {
  480. struct mlx4_ib_proxy_sqp_hdr *hdr;
  481. ib_dma_sync_single_for_cpu(qp->ibqp.device,
  482. qp->sqp_proxy_rcv[tail].map,
  483. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  484. DMA_FROM_DEVICE);
  485. hdr = (struct mlx4_ib_proxy_sqp_hdr *) (qp->sqp_proxy_rcv[tail].addr);
  486. wc->pkey_index = be16_to_cpu(hdr->tun.pkey_index);
  487. wc->src_qp = be32_to_cpu(hdr->tun.flags_src_qp) & 0xFFFFFF;
  488. wc->wc_flags |= (hdr->tun.g_ml_path & 0x80) ? (IB_WC_GRH) : 0;
  489. wc->dlid_path_bits = 0;
  490. if (is_eth) {
  491. wc->slid = 0;
  492. wc->vlan_id = be16_to_cpu(hdr->tun.sl_vid);
  493. memcpy(&(wc->smac[0]), (char *)&hdr->tun.mac_31_0, 4);
  494. memcpy(&(wc->smac[4]), (char *)&hdr->tun.slid_mac_47_32, 2);
  495. wc->wc_flags |= (IB_WC_WITH_VLAN | IB_WC_WITH_SMAC);
  496. } else {
  497. wc->slid = be16_to_cpu(hdr->tun.slid_mac_47_32);
  498. wc->sl = (u8) (be16_to_cpu(hdr->tun.sl_vid) >> 12);
  499. }
  500. }
  501. static void mlx4_ib_qp_sw_comp(struct mlx4_ib_qp *qp, int num_entries,
  502. struct ib_wc *wc, int *npolled, int is_send)
  503. {
  504. struct mlx4_ib_wq *wq;
  505. unsigned cur;
  506. int i;
  507. wq = is_send ? &qp->sq : &qp->rq;
  508. cur = wq->head - wq->tail;
  509. if (cur == 0)
  510. return;
  511. for (i = 0; i < cur && *npolled < num_entries; i++) {
  512. wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
  513. wc->status = IB_WC_WR_FLUSH_ERR;
  514. wc->vendor_err = MLX4_CQE_SYNDROME_WR_FLUSH_ERR;
  515. wq->tail++;
  516. (*npolled)++;
  517. wc->qp = &qp->ibqp;
  518. wc++;
  519. }
  520. }
  521. static void mlx4_ib_poll_sw_comp(struct mlx4_ib_cq *cq, int num_entries,
  522. struct ib_wc *wc, int *npolled)
  523. {
  524. struct mlx4_ib_qp *qp;
  525. *npolled = 0;
  526. /* Find uncompleted WQEs belonging to that cq and return
  527. * simulated FLUSH_ERR completions
  528. */
  529. list_for_each_entry(qp, &cq->send_qp_list, cq_send_list) {
  530. mlx4_ib_qp_sw_comp(qp, num_entries, wc + *npolled, npolled, 1);
  531. if (*npolled >= num_entries)
  532. goto out;
  533. }
  534. list_for_each_entry(qp, &cq->recv_qp_list, cq_recv_list) {
  535. mlx4_ib_qp_sw_comp(qp, num_entries, wc + *npolled, npolled, 0);
  536. if (*npolled >= num_entries)
  537. goto out;
  538. }
  539. out:
  540. return;
  541. }
  542. static int mlx4_ib_poll_one(struct mlx4_ib_cq *cq,
  543. struct mlx4_ib_qp **cur_qp,
  544. struct ib_wc *wc)
  545. {
  546. struct mlx4_cqe *cqe;
  547. struct mlx4_qp *mqp;
  548. struct mlx4_ib_wq *wq;
  549. struct mlx4_ib_srq *srq;
  550. struct mlx4_srq *msrq = NULL;
  551. int is_send;
  552. int is_error;
  553. int is_eth;
  554. u32 g_mlpath_rqpn;
  555. u16 wqe_ctr;
  556. unsigned tail = 0;
  557. repoll:
  558. cqe = next_cqe_sw(cq);
  559. if (!cqe)
  560. return -EAGAIN;
  561. if (cq->buf.entry_size == 64)
  562. cqe++;
  563. ++cq->mcq.cons_index;
  564. /*
  565. * Make sure we read CQ entry contents after we've checked the
  566. * ownership bit.
  567. */
  568. rmb();
  569. is_send = cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK;
  570. is_error = (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
  571. MLX4_CQE_OPCODE_ERROR;
  572. /* Resize CQ in progress */
  573. if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) == MLX4_CQE_OPCODE_RESIZE)) {
  574. if (cq->resize_buf) {
  575. struct mlx4_ib_dev *dev = to_mdev(cq->ibcq.device);
  576. mlx4_ib_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
  577. cq->buf = cq->resize_buf->buf;
  578. cq->ibcq.cqe = cq->resize_buf->cqe;
  579. kfree(cq->resize_buf);
  580. cq->resize_buf = NULL;
  581. }
  582. goto repoll;
  583. }
  584. if (!*cur_qp ||
  585. (be32_to_cpu(cqe->vlan_my_qpn) & MLX4_CQE_QPN_MASK) != (*cur_qp)->mqp.qpn) {
  586. /*
  587. * We do not have to take the QP table lock here,
  588. * because CQs will be locked while QPs are removed
  589. * from the table.
  590. */
  591. mqp = __mlx4_qp_lookup(to_mdev(cq->ibcq.device)->dev,
  592. be32_to_cpu(cqe->vlan_my_qpn));
  593. *cur_qp = to_mibqp(mqp);
  594. }
  595. wc->qp = &(*cur_qp)->ibqp;
  596. if (wc->qp->qp_type == IB_QPT_XRC_TGT) {
  597. u32 srq_num;
  598. g_mlpath_rqpn = be32_to_cpu(cqe->g_mlpath_rqpn);
  599. srq_num = g_mlpath_rqpn & 0xffffff;
  600. /* SRQ is also in the radix tree */
  601. msrq = mlx4_srq_lookup(to_mdev(cq->ibcq.device)->dev,
  602. srq_num);
  603. }
  604. if (is_send) {
  605. wq = &(*cur_qp)->sq;
  606. if (!(*cur_qp)->sq_signal_bits) {
  607. wqe_ctr = be16_to_cpu(cqe->wqe_index);
  608. wq->tail += (u16) (wqe_ctr - (u16) wq->tail);
  609. }
  610. wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
  611. ++wq->tail;
  612. } else if ((*cur_qp)->ibqp.srq) {
  613. srq = to_msrq((*cur_qp)->ibqp.srq);
  614. wqe_ctr = be16_to_cpu(cqe->wqe_index);
  615. wc->wr_id = srq->wrid[wqe_ctr];
  616. mlx4_ib_free_srq_wqe(srq, wqe_ctr);
  617. } else if (msrq) {
  618. srq = to_mibsrq(msrq);
  619. wqe_ctr = be16_to_cpu(cqe->wqe_index);
  620. wc->wr_id = srq->wrid[wqe_ctr];
  621. mlx4_ib_free_srq_wqe(srq, wqe_ctr);
  622. } else {
  623. wq = &(*cur_qp)->rq;
  624. tail = wq->tail & (wq->wqe_cnt - 1);
  625. wc->wr_id = wq->wrid[tail];
  626. ++wq->tail;
  627. }
  628. if (unlikely(is_error)) {
  629. mlx4_ib_handle_error_cqe((struct mlx4_err_cqe *) cqe, wc);
  630. return 0;
  631. }
  632. wc->status = IB_WC_SUCCESS;
  633. if (is_send) {
  634. wc->wc_flags = 0;
  635. switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
  636. case MLX4_OPCODE_RDMA_WRITE_IMM:
  637. wc->wc_flags |= IB_WC_WITH_IMM;
  638. fallthrough;
  639. case MLX4_OPCODE_RDMA_WRITE:
  640. wc->opcode = IB_WC_RDMA_WRITE;
  641. break;
  642. case MLX4_OPCODE_SEND_IMM:
  643. wc->wc_flags |= IB_WC_WITH_IMM;
  644. fallthrough;
  645. case MLX4_OPCODE_SEND:
  646. case MLX4_OPCODE_SEND_INVAL:
  647. wc->opcode = IB_WC_SEND;
  648. break;
  649. case MLX4_OPCODE_RDMA_READ:
  650. wc->opcode = IB_WC_RDMA_READ;
  651. wc->byte_len = be32_to_cpu(cqe->byte_cnt);
  652. break;
  653. case MLX4_OPCODE_ATOMIC_CS:
  654. wc->opcode = IB_WC_COMP_SWAP;
  655. wc->byte_len = 8;
  656. break;
  657. case MLX4_OPCODE_ATOMIC_FA:
  658. wc->opcode = IB_WC_FETCH_ADD;
  659. wc->byte_len = 8;
  660. break;
  661. case MLX4_OPCODE_MASKED_ATOMIC_CS:
  662. wc->opcode = IB_WC_MASKED_COMP_SWAP;
  663. wc->byte_len = 8;
  664. break;
  665. case MLX4_OPCODE_MASKED_ATOMIC_FA:
  666. wc->opcode = IB_WC_MASKED_FETCH_ADD;
  667. wc->byte_len = 8;
  668. break;
  669. case MLX4_OPCODE_LSO:
  670. wc->opcode = IB_WC_LSO;
  671. break;
  672. case MLX4_OPCODE_FMR:
  673. wc->opcode = IB_WC_REG_MR;
  674. break;
  675. case MLX4_OPCODE_LOCAL_INVAL:
  676. wc->opcode = IB_WC_LOCAL_INV;
  677. break;
  678. }
  679. } else {
  680. wc->byte_len = be32_to_cpu(cqe->byte_cnt);
  681. switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
  682. case MLX4_RECV_OPCODE_RDMA_WRITE_IMM:
  683. wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
  684. wc->wc_flags = IB_WC_WITH_IMM;
  685. wc->ex.imm_data = cqe->immed_rss_invalid;
  686. break;
  687. case MLX4_RECV_OPCODE_SEND_INVAL:
  688. wc->opcode = IB_WC_RECV;
  689. wc->wc_flags = IB_WC_WITH_INVALIDATE;
  690. wc->ex.invalidate_rkey = be32_to_cpu(cqe->immed_rss_invalid);
  691. break;
  692. case MLX4_RECV_OPCODE_SEND:
  693. wc->opcode = IB_WC_RECV;
  694. wc->wc_flags = 0;
  695. break;
  696. case MLX4_RECV_OPCODE_SEND_IMM:
  697. wc->opcode = IB_WC_RECV;
  698. wc->wc_flags = IB_WC_WITH_IMM;
  699. wc->ex.imm_data = cqe->immed_rss_invalid;
  700. break;
  701. }
  702. is_eth = (rdma_port_get_link_layer(wc->qp->device,
  703. (*cur_qp)->port) ==
  704. IB_LINK_LAYER_ETHERNET);
  705. if (mlx4_is_mfunc(to_mdev(cq->ibcq.device)->dev)) {
  706. if ((*cur_qp)->mlx4_ib_qp_type &
  707. (MLX4_IB_QPT_PROXY_SMI_OWNER |
  708. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
  709. use_tunnel_data(*cur_qp, cq, wc, tail, cqe,
  710. is_eth);
  711. return 0;
  712. }
  713. }
  714. g_mlpath_rqpn = be32_to_cpu(cqe->g_mlpath_rqpn);
  715. wc->src_qp = g_mlpath_rqpn & 0xffffff;
  716. wc->dlid_path_bits = (g_mlpath_rqpn >> 24) & 0x7f;
  717. wc->wc_flags |= g_mlpath_rqpn & 0x80000000 ? IB_WC_GRH : 0;
  718. wc->pkey_index = be32_to_cpu(cqe->immed_rss_invalid) & 0x7f;
  719. wc->wc_flags |= mlx4_ib_ipoib_csum_ok(cqe->status,
  720. cqe->badfcs_enc,
  721. cqe->checksum) ? IB_WC_IP_CSUM_OK : 0;
  722. if (is_eth) {
  723. wc->slid = 0;
  724. wc->sl = be16_to_cpu(cqe->sl_vid) >> 13;
  725. if (be32_to_cpu(cqe->vlan_my_qpn) &
  726. MLX4_CQE_CVLAN_PRESENT_MASK) {
  727. wc->vlan_id = be16_to_cpu(cqe->sl_vid) &
  728. MLX4_CQE_VID_MASK;
  729. } else {
  730. wc->vlan_id = 0xffff;
  731. }
  732. memcpy(wc->smac, cqe->smac, ETH_ALEN);
  733. wc->wc_flags |= (IB_WC_WITH_VLAN | IB_WC_WITH_SMAC);
  734. } else {
  735. wc->slid = be16_to_cpu(cqe->rlid);
  736. wc->sl = be16_to_cpu(cqe->sl_vid) >> 12;
  737. wc->vlan_id = 0xffff;
  738. }
  739. }
  740. return 0;
  741. }
  742. int mlx4_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
  743. {
  744. struct mlx4_ib_cq *cq = to_mcq(ibcq);
  745. struct mlx4_ib_qp *cur_qp = NULL;
  746. unsigned long flags;
  747. int npolled;
  748. struct mlx4_ib_dev *mdev = to_mdev(cq->ibcq.device);
  749. spin_lock_irqsave(&cq->lock, flags);
  750. if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  751. mlx4_ib_poll_sw_comp(cq, num_entries, wc, &npolled);
  752. goto out;
  753. }
  754. for (npolled = 0; npolled < num_entries; ++npolled) {
  755. if (mlx4_ib_poll_one(cq, &cur_qp, wc + npolled))
  756. break;
  757. }
  758. mlx4_cq_set_ci(&cq->mcq);
  759. out:
  760. spin_unlock_irqrestore(&cq->lock, flags);
  761. return npolled;
  762. }
  763. int mlx4_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
  764. {
  765. mlx4_cq_arm(&to_mcq(ibcq)->mcq,
  766. (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
  767. MLX4_CQ_DB_REQ_NOT_SOL : MLX4_CQ_DB_REQ_NOT,
  768. to_mdev(ibcq->device)->uar_map,
  769. MLX4_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->uar_lock));
  770. return 0;
  771. }
  772. void __mlx4_ib_cq_clean(struct mlx4_ib_cq *cq, u32 qpn, struct mlx4_ib_srq *srq)
  773. {
  774. u32 prod_index;
  775. int nfreed = 0;
  776. struct mlx4_cqe *cqe, *dest;
  777. u8 owner_bit;
  778. int cqe_inc = cq->buf.entry_size == 64 ? 1 : 0;
  779. /*
  780. * First we need to find the current producer index, so we
  781. * know where to start cleaning from. It doesn't matter if HW
  782. * adds new entries after this loop -- the QP we're worried
  783. * about is already in RESET, so the new entries won't come
  784. * from our QP and therefore don't need to be checked.
  785. */
  786. for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); ++prod_index)
  787. if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
  788. break;
  789. /*
  790. * Now sweep backwards through the CQ, removing CQ entries
  791. * that match our QP by copying older entries on top of them.
  792. */
  793. while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
  794. cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
  795. cqe += cqe_inc;
  796. if ((be32_to_cpu(cqe->vlan_my_qpn) & MLX4_CQE_QPN_MASK) == qpn) {
  797. if (srq && !(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK))
  798. mlx4_ib_free_srq_wqe(srq, be16_to_cpu(cqe->wqe_index));
  799. ++nfreed;
  800. } else if (nfreed) {
  801. dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
  802. dest += cqe_inc;
  803. owner_bit = dest->owner_sr_opcode & MLX4_CQE_OWNER_MASK;
  804. memcpy(dest, cqe, sizeof *cqe);
  805. dest->owner_sr_opcode = owner_bit |
  806. (dest->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
  807. }
  808. }
  809. if (nfreed) {
  810. cq->mcq.cons_index += nfreed;
  811. /*
  812. * Make sure update of buffer contents is done before
  813. * updating consumer index.
  814. */
  815. wmb();
  816. mlx4_cq_set_ci(&cq->mcq);
  817. }
  818. }
  819. void mlx4_ib_cq_clean(struct mlx4_ib_cq *cq, u32 qpn, struct mlx4_ib_srq *srq)
  820. {
  821. spin_lock_irq(&cq->lock);
  822. __mlx4_ib_cq_clean(cq, qpn, srq);
  823. spin_unlock_irq(&cq->lock);
  824. }