pvrdma_cq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. /**
  241. * pvrdma_modify_cq - modify the CQ moderation parameters
  242. * @ibcq: the CQ to modify
  243. * @cq_count: number of CQEs that will trigger an event
  244. * @cq_period: max period of time in usec before triggering an event
  245. *
  246. * @return: -EOPNOTSUPP as CQ resize is not supported.
  247. */
  248. int pvrdma_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
  249. {
  250. return -EOPNOTSUPP;
  251. }
  252. static inline struct pvrdma_cqe *get_cqe(struct pvrdma_cq *cq, int i)
  253. {
  254. return (struct pvrdma_cqe *)pvrdma_page_dir_get_ptr(
  255. &cq->pdir,
  256. cq->offset +
  257. sizeof(struct pvrdma_cqe) * i);
  258. }
  259. void _pvrdma_flush_cqe(struct pvrdma_qp *qp, struct pvrdma_cq *cq)
  260. {
  261. unsigned int head;
  262. int has_data;
  263. if (!cq->is_kernel)
  264. return;
  265. /* Lock held */
  266. has_data = pvrdma_idx_ring_has_data(&cq->ring_state->rx,
  267. cq->ibcq.cqe, &head);
  268. if (unlikely(has_data > 0)) {
  269. int items;
  270. int curr;
  271. int tail = pvrdma_idx(&cq->ring_state->rx.prod_tail,
  272. cq->ibcq.cqe);
  273. struct pvrdma_cqe *cqe;
  274. struct pvrdma_cqe *curr_cqe;
  275. items = (tail > head) ? (tail - head) :
  276. (cq->ibcq.cqe - head + tail);
  277. curr = --tail;
  278. while (items-- > 0) {
  279. if (curr < 0)
  280. curr = cq->ibcq.cqe - 1;
  281. if (tail < 0)
  282. tail = cq->ibcq.cqe - 1;
  283. curr_cqe = get_cqe(cq, curr);
  284. if ((curr_cqe->qp & 0xFFFF) != qp->qp_handle) {
  285. if (curr != tail) {
  286. cqe = get_cqe(cq, tail);
  287. *cqe = *curr_cqe;
  288. }
  289. tail--;
  290. } else {
  291. pvrdma_idx_ring_inc(
  292. &cq->ring_state->rx.cons_head,
  293. cq->ibcq.cqe);
  294. }
  295. curr--;
  296. }
  297. }
  298. }
  299. static int pvrdma_poll_one(struct pvrdma_cq *cq, struct pvrdma_qp **cur_qp,
  300. struct ib_wc *wc)
  301. {
  302. struct pvrdma_dev *dev = to_vdev(cq->ibcq.device);
  303. int has_data;
  304. unsigned int head;
  305. bool tried = false;
  306. struct pvrdma_cqe *cqe;
  307. retry:
  308. has_data = pvrdma_idx_ring_has_data(&cq->ring_state->rx,
  309. cq->ibcq.cqe, &head);
  310. if (has_data == 0) {
  311. if (tried)
  312. return -EAGAIN;
  313. pvrdma_write_uar_cq(dev, cq->cq_handle | PVRDMA_UAR_CQ_POLL);
  314. tried = true;
  315. goto retry;
  316. } else if (has_data == PVRDMA_INVALID_IDX) {
  317. dev_err(&dev->pdev->dev, "CQ ring state invalid\n");
  318. return -EAGAIN;
  319. }
  320. cqe = get_cqe(cq, head);
  321. /* Ensure cqe is valid. */
  322. rmb();
  323. if (dev->qp_tbl[cqe->qp & 0xffff])
  324. *cur_qp = (struct pvrdma_qp *)dev->qp_tbl[cqe->qp & 0xffff];
  325. else
  326. return -EAGAIN;
  327. wc->opcode = pvrdma_wc_opcode_to_ib(cqe->opcode);
  328. wc->status = pvrdma_wc_status_to_ib(cqe->status);
  329. wc->wr_id = cqe->wr_id;
  330. wc->qp = &(*cur_qp)->ibqp;
  331. wc->byte_len = cqe->byte_len;
  332. wc->ex.imm_data = cqe->imm_data;
  333. wc->src_qp = cqe->src_qp;
  334. wc->wc_flags = pvrdma_wc_flags_to_ib(cqe->wc_flags);
  335. wc->pkey_index = cqe->pkey_index;
  336. wc->slid = cqe->slid;
  337. wc->sl = cqe->sl;
  338. wc->dlid_path_bits = cqe->dlid_path_bits;
  339. wc->port_num = cqe->port_num;
  340. wc->vendor_err = cqe->vendor_err;
  341. wc->network_hdr_type = cqe->network_hdr_type;
  342. /* Update shared ring state */
  343. pvrdma_idx_ring_inc(&cq->ring_state->rx.cons_head, cq->ibcq.cqe);
  344. return 0;
  345. }
  346. /**
  347. * pvrdma_poll_cq - poll for work completion queue entries
  348. * @ibcq: completion queue
  349. * @num_entries: the maximum number of entries
  350. * @entry: pointer to work completion array
  351. *
  352. * @return: number of polled completion entries
  353. */
  354. int pvrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
  355. {
  356. struct pvrdma_cq *cq = to_vcq(ibcq);
  357. struct pvrdma_qp *cur_qp = NULL;
  358. unsigned long flags;
  359. int npolled;
  360. if (num_entries < 1 || wc == NULL)
  361. return 0;
  362. spin_lock_irqsave(&cq->cq_lock, flags);
  363. for (npolled = 0; npolled < num_entries; ++npolled) {
  364. if (pvrdma_poll_one(cq, &cur_qp, wc + npolled))
  365. break;
  366. }
  367. spin_unlock_irqrestore(&cq->cq_lock, flags);
  368. /* Ensure we do not return errors from poll_cq */
  369. return npolled;
  370. }
  371. /**
  372. * pvrdma_resize_cq - resize CQ
  373. * @ibcq: the completion queue
  374. * @entries: CQ entries
  375. * @udata: user data
  376. *
  377. * @return: -EOPNOTSUPP as CQ resize is not supported.
  378. */
  379. int pvrdma_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
  380. {
  381. return -EOPNOTSUPP;
  382. }