cq.c 22 KB

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