pvrdma_cq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of EITHER the GNU General Public License
  6. * version 2 as published by the Free Software Foundation or the BSD
  7. * 2-Clause License. This program is distributed in the hope that it
  8. * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
  9. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License version 2 for more details at
  11. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program available in the file COPYING in the main
  15. * directory of this source tree.
  16. *
  17. * The BSD 2-Clause License
  18. *
  19. * Redistribution and use in source and binary forms, with or
  20. * without modification, are permitted provided that the following
  21. * conditions are met:
  22. *
  23. * - Redistributions of source code must retain the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer.
  26. *
  27. * - Redistributions in binary form must reproduce the above
  28. * copyright notice, this list of conditions and the following
  29. * disclaimer in the documentation and/or other materials
  30. * provided with the distribution.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  33. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  34. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  35. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  36. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  37. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  38. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  39. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  43. * OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. #include <asm/page.h>
  46. #include <linux/io.h>
  47. #include <linux/wait.h>
  48. #include <rdma/ib_addr.h>
  49. #include <rdma/ib_smi.h>
  50. #include <rdma/ib_user_verbs.h>
  51. #include "pvrdma.h"
  52. /**
  53. * pvrdma_req_notify_cq - request notification for a completion queue
  54. * @ibcq: the completion queue
  55. * @notify_flags: notification flags
  56. *
  57. * @return: 0 for success.
  58. */
  59. int pvrdma_req_notify_cq(struct ib_cq *ibcq,
  60. enum ib_cq_notify_flags notify_flags)
  61. {
  62. struct pvrdma_dev *dev = to_vdev(ibcq->device);
  63. struct pvrdma_cq *cq = to_vcq(ibcq);
  64. u32 val = cq->cq_handle;
  65. unsigned long flags;
  66. int has_data = 0;
  67. val |= (notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
  68. PVRDMA_UAR_CQ_ARM_SOL : PVRDMA_UAR_CQ_ARM;
  69. spin_lock_irqsave(&cq->cq_lock, flags);
  70. pvrdma_write_uar_cq(dev, val);
  71. if (notify_flags & IB_CQ_REPORT_MISSED_EVENTS) {
  72. unsigned int head;
  73. has_data = pvrdma_idx_ring_has_data(&cq->ring_state->rx,
  74. cq->ibcq.cqe, &head);
  75. if (unlikely(has_data == PVRDMA_INVALID_IDX))
  76. dev_err(&dev->pdev->dev, "CQ ring state invalid\n");
  77. }
  78. spin_unlock_irqrestore(&cq->cq_lock, flags);
  79. return has_data;
  80. }
  81. /**
  82. * pvrdma_create_cq - create completion queue
  83. * @ibdev: the device
  84. * @attr: completion queue attributes
  85. * @context: user context
  86. * @udata: user data
  87. *
  88. * @return: ib_cq completion queue pointer on success,
  89. * otherwise returns negative errno.
  90. */
  91. struct ib_cq *pvrdma_create_cq(struct ib_device *ibdev,
  92. const struct ib_cq_init_attr *attr,
  93. struct ib_ucontext *context,
  94. struct ib_udata *udata)
  95. {
  96. int entries = attr->cqe;
  97. struct pvrdma_dev *dev = to_vdev(ibdev);
  98. struct pvrdma_cq *cq;
  99. int ret;
  100. int npages;
  101. unsigned long flags;
  102. union pvrdma_cmd_req req;
  103. union pvrdma_cmd_resp rsp;
  104. struct pvrdma_cmd_create_cq *cmd = &req.create_cq;
  105. struct pvrdma_cmd_create_cq_resp *resp = &rsp.create_cq_resp;
  106. struct pvrdma_create_cq_resp cq_resp = {0};
  107. struct pvrdma_create_cq ucmd;
  108. BUILD_BUG_ON(sizeof(struct pvrdma_cqe) != 64);
  109. entries = roundup_pow_of_two(entries);
  110. if (entries < 1 || entries > dev->dsr->caps.max_cqe)
  111. return ERR_PTR(-EINVAL);
  112. if (!atomic_add_unless(&dev->num_cqs, 1, dev->dsr->caps.max_cq))
  113. return ERR_PTR(-ENOMEM);
  114. cq = kzalloc(sizeof(*cq), GFP_KERNEL);
  115. if (!cq) {
  116. atomic_dec(&dev->num_cqs);
  117. return ERR_PTR(-ENOMEM);
  118. }
  119. cq->ibcq.cqe = entries;
  120. cq->is_kernel = !context;
  121. if (!cq->is_kernel) {
  122. if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
  123. ret = -EFAULT;
  124. goto err_cq;
  125. }
  126. cq->umem = ib_umem_get(context, ucmd.buf_addr, ucmd.buf_size,
  127. IB_ACCESS_LOCAL_WRITE, 1);
  128. if (IS_ERR(cq->umem)) {
  129. ret = PTR_ERR(cq->umem);
  130. goto err_cq;
  131. }
  132. npages = ib_umem_page_count(cq->umem);
  133. } else {
  134. /* One extra page for shared ring state */
  135. npages = 1 + (entries * sizeof(struct pvrdma_cqe) +
  136. PAGE_SIZE - 1) / PAGE_SIZE;
  137. /* Skip header page. */
  138. cq->offset = PAGE_SIZE;
  139. }
  140. if (npages < 0 || npages > PVRDMA_PAGE_DIR_MAX_PAGES) {
  141. dev_warn(&dev->pdev->dev,
  142. "overflow pages in completion queue\n");
  143. ret = -EINVAL;
  144. goto err_umem;
  145. }
  146. ret = pvrdma_page_dir_init(dev, &cq->pdir, npages, cq->is_kernel);
  147. if (ret) {
  148. dev_warn(&dev->pdev->dev,
  149. "could not allocate page directory\n");
  150. goto err_umem;
  151. }
  152. /* Ring state is always the first page. Set in library for user cq. */
  153. if (cq->is_kernel)
  154. cq->ring_state = cq->pdir.pages[0];
  155. else
  156. pvrdma_page_dir_insert_umem(&cq->pdir, cq->umem, 0);
  157. refcount_set(&cq->refcnt, 1);
  158. init_completion(&cq->free);
  159. spin_lock_init(&cq->cq_lock);
  160. memset(cmd, 0, sizeof(*cmd));
  161. cmd->hdr.cmd = PVRDMA_CMD_CREATE_CQ;
  162. cmd->nchunks = npages;
  163. cmd->ctx_handle = (context) ?
  164. (u64)to_vucontext(context)->ctx_handle : 0;
  165. cmd->cqe = entries;
  166. cmd->pdir_dma = cq->pdir.dir_dma;
  167. ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_CQ_RESP);
  168. if (ret < 0) {
  169. dev_warn(&dev->pdev->dev,
  170. "could not create completion queue, error: %d\n", ret);
  171. goto err_page_dir;
  172. }
  173. cq->ibcq.cqe = resp->cqe;
  174. cq->cq_handle = resp->cq_handle;
  175. cq_resp.cqn = resp->cq_handle;
  176. spin_lock_irqsave(&dev->cq_tbl_lock, flags);
  177. dev->cq_tbl[cq->cq_handle % dev->dsr->caps.max_cq] = cq;
  178. spin_unlock_irqrestore(&dev->cq_tbl_lock, flags);
  179. if (!cq->is_kernel) {
  180. cq->uar = &(to_vucontext(context)->uar);
  181. /* Copy udata back. */
  182. if (ib_copy_to_udata(udata, &cq_resp, sizeof(cq_resp))) {
  183. dev_warn(&dev->pdev->dev,
  184. "failed to copy back udata\n");
  185. pvrdma_destroy_cq(&cq->ibcq);
  186. return ERR_PTR(-EINVAL);
  187. }
  188. }
  189. return &cq->ibcq;
  190. err_page_dir:
  191. pvrdma_page_dir_cleanup(dev, &cq->pdir);
  192. err_umem:
  193. if (!cq->is_kernel)
  194. ib_umem_release(cq->umem);
  195. err_cq:
  196. atomic_dec(&dev->num_cqs);
  197. kfree(cq);
  198. return ERR_PTR(ret);
  199. }
  200. static void pvrdma_free_cq(struct pvrdma_dev *dev, struct pvrdma_cq *cq)
  201. {
  202. if (refcount_dec_and_test(&cq->refcnt))
  203. complete(&cq->free);
  204. wait_for_completion(&cq->free);
  205. if (!cq->is_kernel)
  206. ib_umem_release(cq->umem);
  207. pvrdma_page_dir_cleanup(dev, &cq->pdir);
  208. kfree(cq);
  209. }
  210. /**
  211. * pvrdma_destroy_cq - destroy completion queue
  212. * @cq: the completion queue to destroy.
  213. *
  214. * @return: 0 for success.
  215. */
  216. int pvrdma_destroy_cq(struct ib_cq *cq)
  217. {
  218. struct pvrdma_cq *vcq = to_vcq(cq);
  219. union pvrdma_cmd_req req;
  220. struct pvrdma_cmd_destroy_cq *cmd = &req.destroy_cq;
  221. struct pvrdma_dev *dev = to_vdev(cq->device);
  222. unsigned long flags;
  223. int ret;
  224. memset(cmd, 0, sizeof(*cmd));
  225. cmd->hdr.cmd = PVRDMA_CMD_DESTROY_CQ;
  226. cmd->cq_handle = vcq->cq_handle;
  227. ret = pvrdma_cmd_post(dev, &req, NULL, 0);
  228. if (ret < 0)
  229. dev_warn(&dev->pdev->dev,
  230. "could not destroy completion queue, error: %d\n",
  231. ret);
  232. /* free cq's resources */
  233. spin_lock_irqsave(&dev->cq_tbl_lock, flags);
  234. dev->cq_tbl[vcq->cq_handle] = NULL;
  235. spin_unlock_irqrestore(&dev->cq_tbl_lock, flags);
  236. pvrdma_free_cq(dev, vcq);
  237. atomic_dec(&dev->num_cqs);
  238. return ret;
  239. }
  240. static inline struct pvrdma_cqe *get_cqe(struct pvrdma_cq *cq, int i)
  241. {
  242. return (struct pvrdma_cqe *)pvrdma_page_dir_get_ptr(
  243. &cq->pdir,
  244. cq->offset +
  245. sizeof(struct pvrdma_cqe) * i);
  246. }
  247. void _pvrdma_flush_cqe(struct pvrdma_qp *qp, struct pvrdma_cq *cq)
  248. {
  249. unsigned int head;
  250. int has_data;
  251. if (!cq->is_kernel)
  252. return;
  253. /* Lock held */
  254. has_data = pvrdma_idx_ring_has_data(&cq->ring_state->rx,
  255. cq->ibcq.cqe, &head);
  256. if (unlikely(has_data > 0)) {
  257. int items;
  258. int curr;
  259. int tail = pvrdma_idx(&cq->ring_state->rx.prod_tail,
  260. cq->ibcq.cqe);
  261. struct pvrdma_cqe *cqe;
  262. struct pvrdma_cqe *curr_cqe;
  263. items = (tail > head) ? (tail - head) :
  264. (cq->ibcq.cqe - head + tail);
  265. curr = --tail;
  266. while (items-- > 0) {
  267. if (curr < 0)
  268. curr = cq->ibcq.cqe - 1;
  269. if (tail < 0)
  270. tail = cq->ibcq.cqe - 1;
  271. curr_cqe = get_cqe(cq, curr);
  272. if ((curr_cqe->qp & 0xFFFF) != qp->qp_handle) {
  273. if (curr != tail) {
  274. cqe = get_cqe(cq, tail);
  275. *cqe = *curr_cqe;
  276. }
  277. tail--;
  278. } else {
  279. pvrdma_idx_ring_inc(
  280. &cq->ring_state->rx.cons_head,
  281. cq->ibcq.cqe);
  282. }
  283. curr--;
  284. }
  285. }
  286. }
  287. static int pvrdma_poll_one(struct pvrdma_cq *cq, struct pvrdma_qp **cur_qp,
  288. struct ib_wc *wc)
  289. {
  290. struct pvrdma_dev *dev = to_vdev(cq->ibcq.device);
  291. int has_data;
  292. unsigned int head;
  293. bool tried = false;
  294. struct pvrdma_cqe *cqe;
  295. retry:
  296. has_data = pvrdma_idx_ring_has_data(&cq->ring_state->rx,
  297. cq->ibcq.cqe, &head);
  298. if (has_data == 0) {
  299. if (tried)
  300. return -EAGAIN;
  301. pvrdma_write_uar_cq(dev, cq->cq_handle | PVRDMA_UAR_CQ_POLL);
  302. tried = true;
  303. goto retry;
  304. } else if (has_data == PVRDMA_INVALID_IDX) {
  305. dev_err(&dev->pdev->dev, "CQ ring state invalid\n");
  306. return -EAGAIN;
  307. }
  308. cqe = get_cqe(cq, head);
  309. /* Ensure cqe is valid. */
  310. rmb();
  311. if (dev->qp_tbl[cqe->qp & 0xffff])
  312. *cur_qp = (struct pvrdma_qp *)dev->qp_tbl[cqe->qp & 0xffff];
  313. else
  314. return -EAGAIN;
  315. wc->opcode = pvrdma_wc_opcode_to_ib(cqe->opcode);
  316. wc->status = pvrdma_wc_status_to_ib(cqe->status);
  317. wc->wr_id = cqe->wr_id;
  318. wc->qp = &(*cur_qp)->ibqp;
  319. wc->byte_len = cqe->byte_len;
  320. wc->ex.imm_data = cqe->imm_data;
  321. wc->src_qp = cqe->src_qp;
  322. wc->wc_flags = pvrdma_wc_flags_to_ib(cqe->wc_flags);
  323. wc->pkey_index = cqe->pkey_index;
  324. wc->slid = cqe->slid;
  325. wc->sl = cqe->sl;
  326. wc->dlid_path_bits = cqe->dlid_path_bits;
  327. wc->port_num = cqe->port_num;
  328. wc->vendor_err = cqe->vendor_err;
  329. wc->network_hdr_type = cqe->network_hdr_type;
  330. /* Update shared ring state */
  331. pvrdma_idx_ring_inc(&cq->ring_state->rx.cons_head, cq->ibcq.cqe);
  332. return 0;
  333. }
  334. /**
  335. * pvrdma_poll_cq - poll for work completion queue entries
  336. * @ibcq: completion queue
  337. * @num_entries: the maximum number of entries
  338. * @entry: pointer to work completion array
  339. *
  340. * @return: number of polled completion entries
  341. */
  342. int pvrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
  343. {
  344. struct pvrdma_cq *cq = to_vcq(ibcq);
  345. struct pvrdma_qp *cur_qp = NULL;
  346. unsigned long flags;
  347. int npolled;
  348. if (num_entries < 1 || wc == NULL)
  349. return 0;
  350. spin_lock_irqsave(&cq->cq_lock, flags);
  351. for (npolled = 0; npolled < num_entries; ++npolled) {
  352. if (pvrdma_poll_one(cq, &cur_qp, wc + npolled))
  353. break;
  354. }
  355. spin_unlock_irqrestore(&cq->cq_lock, flags);
  356. /* Ensure we do not return errors from poll_cq */
  357. return npolled;
  358. }