cq.c 25 KB

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