pvrdma_qp.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. static inline void get_cqs(struct pvrdma_qp *qp, struct pvrdma_cq **send_cq,
  53. struct pvrdma_cq **recv_cq)
  54. {
  55. *send_cq = to_vcq(qp->ibqp.send_cq);
  56. *recv_cq = to_vcq(qp->ibqp.recv_cq);
  57. }
  58. static void pvrdma_lock_cqs(struct pvrdma_cq *scq, struct pvrdma_cq *rcq,
  59. unsigned long *scq_flags,
  60. unsigned long *rcq_flags)
  61. __acquires(scq->cq_lock) __acquires(rcq->cq_lock)
  62. {
  63. if (scq == rcq) {
  64. spin_lock_irqsave(&scq->cq_lock, *scq_flags);
  65. __acquire(rcq->cq_lock);
  66. } else if (scq->cq_handle < rcq->cq_handle) {
  67. spin_lock_irqsave(&scq->cq_lock, *scq_flags);
  68. spin_lock_irqsave_nested(&rcq->cq_lock, *rcq_flags,
  69. SINGLE_DEPTH_NESTING);
  70. } else {
  71. spin_lock_irqsave(&rcq->cq_lock, *rcq_flags);
  72. spin_lock_irqsave_nested(&scq->cq_lock, *scq_flags,
  73. SINGLE_DEPTH_NESTING);
  74. }
  75. }
  76. static void pvrdma_unlock_cqs(struct pvrdma_cq *scq, struct pvrdma_cq *rcq,
  77. unsigned long *scq_flags,
  78. unsigned long *rcq_flags)
  79. __releases(scq->cq_lock) __releases(rcq->cq_lock)
  80. {
  81. if (scq == rcq) {
  82. __release(rcq->cq_lock);
  83. spin_unlock_irqrestore(&scq->cq_lock, *scq_flags);
  84. } else if (scq->cq_handle < rcq->cq_handle) {
  85. spin_unlock_irqrestore(&rcq->cq_lock, *rcq_flags);
  86. spin_unlock_irqrestore(&scq->cq_lock, *scq_flags);
  87. } else {
  88. spin_unlock_irqrestore(&scq->cq_lock, *scq_flags);
  89. spin_unlock_irqrestore(&rcq->cq_lock, *rcq_flags);
  90. }
  91. }
  92. static void pvrdma_reset_qp(struct pvrdma_qp *qp)
  93. {
  94. struct pvrdma_cq *scq, *rcq;
  95. unsigned long scq_flags, rcq_flags;
  96. /* Clean up cqes */
  97. get_cqs(qp, &scq, &rcq);
  98. pvrdma_lock_cqs(scq, rcq, &scq_flags, &rcq_flags);
  99. _pvrdma_flush_cqe(qp, scq);
  100. if (scq != rcq)
  101. _pvrdma_flush_cqe(qp, rcq);
  102. pvrdma_unlock_cqs(scq, rcq, &scq_flags, &rcq_flags);
  103. /*
  104. * Reset queuepair. The checks are because usermode queuepairs won't
  105. * have kernel ringstates.
  106. */
  107. if (qp->rq.ring) {
  108. atomic_set(&qp->rq.ring->cons_head, 0);
  109. atomic_set(&qp->rq.ring->prod_tail, 0);
  110. }
  111. if (qp->sq.ring) {
  112. atomic_set(&qp->sq.ring->cons_head, 0);
  113. atomic_set(&qp->sq.ring->prod_tail, 0);
  114. }
  115. }
  116. static int pvrdma_set_rq_size(struct pvrdma_dev *dev,
  117. struct ib_qp_cap *req_cap,
  118. struct pvrdma_qp *qp)
  119. {
  120. if (req_cap->max_recv_wr > dev->dsr->caps.max_qp_wr ||
  121. req_cap->max_recv_sge > dev->dsr->caps.max_sge) {
  122. dev_warn(&dev->pdev->dev, "recv queue size invalid\n");
  123. return -EINVAL;
  124. }
  125. qp->rq.wqe_cnt = roundup_pow_of_two(max(1U, req_cap->max_recv_wr));
  126. qp->rq.max_sg = roundup_pow_of_two(max(1U, req_cap->max_recv_sge));
  127. /* Write back */
  128. req_cap->max_recv_wr = qp->rq.wqe_cnt;
  129. req_cap->max_recv_sge = qp->rq.max_sg;
  130. qp->rq.wqe_size = roundup_pow_of_two(sizeof(struct pvrdma_rq_wqe_hdr) +
  131. sizeof(struct pvrdma_sge) *
  132. qp->rq.max_sg);
  133. qp->npages_recv = (qp->rq.wqe_cnt * qp->rq.wqe_size + PAGE_SIZE - 1) /
  134. PAGE_SIZE;
  135. return 0;
  136. }
  137. static int pvrdma_set_sq_size(struct pvrdma_dev *dev, struct ib_qp_cap *req_cap,
  138. struct pvrdma_qp *qp)
  139. {
  140. if (req_cap->max_send_wr > dev->dsr->caps.max_qp_wr ||
  141. req_cap->max_send_sge > dev->dsr->caps.max_sge) {
  142. dev_warn(&dev->pdev->dev, "send queue size invalid\n");
  143. return -EINVAL;
  144. }
  145. qp->sq.wqe_cnt = roundup_pow_of_two(max(1U, req_cap->max_send_wr));
  146. qp->sq.max_sg = roundup_pow_of_two(max(1U, req_cap->max_send_sge));
  147. /* Write back */
  148. req_cap->max_send_wr = qp->sq.wqe_cnt;
  149. req_cap->max_send_sge = qp->sq.max_sg;
  150. qp->sq.wqe_size = roundup_pow_of_two(sizeof(struct pvrdma_sq_wqe_hdr) +
  151. sizeof(struct pvrdma_sge) *
  152. qp->sq.max_sg);
  153. /* Note: one extra page for the header. */
  154. qp->npages_send = PVRDMA_QP_NUM_HEADER_PAGES +
  155. (qp->sq.wqe_cnt * qp->sq.wqe_size + PAGE_SIZE - 1) /
  156. PAGE_SIZE;
  157. return 0;
  158. }
  159. /**
  160. * pvrdma_create_qp - create queue pair
  161. * @pd: protection domain
  162. * @init_attr: queue pair attributes
  163. * @udata: user data
  164. *
  165. * @return: the ib_qp pointer on success, otherwise returns an errno.
  166. */
  167. struct ib_qp *pvrdma_create_qp(struct ib_pd *pd,
  168. struct ib_qp_init_attr *init_attr,
  169. struct ib_udata *udata)
  170. {
  171. struct pvrdma_qp *qp = NULL;
  172. struct pvrdma_dev *dev = to_vdev(pd->device);
  173. union pvrdma_cmd_req req;
  174. union pvrdma_cmd_resp rsp;
  175. struct pvrdma_cmd_create_qp *cmd = &req.create_qp;
  176. struct pvrdma_cmd_create_qp_resp *resp = &rsp.create_qp_resp;
  177. struct pvrdma_create_qp ucmd;
  178. unsigned long flags;
  179. int ret;
  180. bool is_srq = !!init_attr->srq;
  181. if (init_attr->create_flags) {
  182. dev_warn(&dev->pdev->dev,
  183. "invalid create queuepair flags %#x\n",
  184. init_attr->create_flags);
  185. return ERR_PTR(-EINVAL);
  186. }
  187. if (init_attr->qp_type != IB_QPT_RC &&
  188. init_attr->qp_type != IB_QPT_UD &&
  189. init_attr->qp_type != IB_QPT_GSI) {
  190. dev_warn(&dev->pdev->dev, "queuepair type %d not supported\n",
  191. init_attr->qp_type);
  192. return ERR_PTR(-EINVAL);
  193. }
  194. if (is_srq && !dev->dsr->caps.max_srq) {
  195. dev_warn(&dev->pdev->dev,
  196. "SRQs not supported by device\n");
  197. return ERR_PTR(-EINVAL);
  198. }
  199. if (!atomic_add_unless(&dev->num_qps, 1, dev->dsr->caps.max_qp))
  200. return ERR_PTR(-ENOMEM);
  201. switch (init_attr->qp_type) {
  202. case IB_QPT_GSI:
  203. if (init_attr->port_num == 0 ||
  204. init_attr->port_num > pd->device->phys_port_cnt ||
  205. udata) {
  206. dev_warn(&dev->pdev->dev, "invalid queuepair attrs\n");
  207. ret = -EINVAL;
  208. goto err_qp;
  209. }
  210. /* fall through */
  211. case IB_QPT_RC:
  212. case IB_QPT_UD:
  213. qp = kzalloc(sizeof(*qp), GFP_KERNEL);
  214. if (!qp) {
  215. ret = -ENOMEM;
  216. goto err_qp;
  217. }
  218. spin_lock_init(&qp->sq.lock);
  219. spin_lock_init(&qp->rq.lock);
  220. mutex_init(&qp->mutex);
  221. refcount_set(&qp->refcnt, 1);
  222. init_completion(&qp->free);
  223. qp->state = IB_QPS_RESET;
  224. qp->is_kernel = !(pd->uobject && udata);
  225. if (!qp->is_kernel) {
  226. dev_dbg(&dev->pdev->dev,
  227. "create queuepair from user space\n");
  228. if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
  229. ret = -EFAULT;
  230. goto err_qp;
  231. }
  232. if (!is_srq) {
  233. /* set qp->sq.wqe_cnt, shift, buf_size.. */
  234. qp->rumem = ib_umem_get(pd->uobject->context,
  235. ucmd.rbuf_addr,
  236. ucmd.rbuf_size, 0, 0);
  237. if (IS_ERR(qp->rumem)) {
  238. ret = PTR_ERR(qp->rumem);
  239. goto err_qp;
  240. }
  241. qp->srq = NULL;
  242. } else {
  243. qp->rumem = NULL;
  244. qp->srq = to_vsrq(init_attr->srq);
  245. }
  246. qp->sumem = ib_umem_get(pd->uobject->context,
  247. ucmd.sbuf_addr,
  248. ucmd.sbuf_size, 0, 0);
  249. if (IS_ERR(qp->sumem)) {
  250. if (!is_srq)
  251. ib_umem_release(qp->rumem);
  252. ret = PTR_ERR(qp->sumem);
  253. goto err_qp;
  254. }
  255. qp->npages_send = ib_umem_page_count(qp->sumem);
  256. if (!is_srq)
  257. qp->npages_recv = ib_umem_page_count(qp->rumem);
  258. else
  259. qp->npages_recv = 0;
  260. qp->npages = qp->npages_send + qp->npages_recv;
  261. } else {
  262. ret = pvrdma_set_sq_size(to_vdev(pd->device),
  263. &init_attr->cap, qp);
  264. if (ret)
  265. goto err_qp;
  266. ret = pvrdma_set_rq_size(to_vdev(pd->device),
  267. &init_attr->cap, qp);
  268. if (ret)
  269. goto err_qp;
  270. qp->npages = qp->npages_send + qp->npages_recv;
  271. /* Skip header page. */
  272. qp->sq.offset = PVRDMA_QP_NUM_HEADER_PAGES * PAGE_SIZE;
  273. /* Recv queue pages are after send pages. */
  274. qp->rq.offset = qp->npages_send * PAGE_SIZE;
  275. }
  276. if (qp->npages < 0 || qp->npages > PVRDMA_PAGE_DIR_MAX_PAGES) {
  277. dev_warn(&dev->pdev->dev,
  278. "overflow pages in queuepair\n");
  279. ret = -EINVAL;
  280. goto err_umem;
  281. }
  282. ret = pvrdma_page_dir_init(dev, &qp->pdir, qp->npages,
  283. qp->is_kernel);
  284. if (ret) {
  285. dev_warn(&dev->pdev->dev,
  286. "could not allocate page directory\n");
  287. goto err_umem;
  288. }
  289. if (!qp->is_kernel) {
  290. pvrdma_page_dir_insert_umem(&qp->pdir, qp->sumem, 0);
  291. if (!is_srq)
  292. pvrdma_page_dir_insert_umem(&qp->pdir,
  293. qp->rumem,
  294. qp->npages_send);
  295. } else {
  296. /* Ring state is always the first page. */
  297. qp->sq.ring = qp->pdir.pages[0];
  298. qp->rq.ring = is_srq ? NULL : &qp->sq.ring[1];
  299. }
  300. break;
  301. default:
  302. ret = -EINVAL;
  303. goto err_qp;
  304. }
  305. /* Not supported */
  306. init_attr->cap.max_inline_data = 0;
  307. memset(cmd, 0, sizeof(*cmd));
  308. cmd->hdr.cmd = PVRDMA_CMD_CREATE_QP;
  309. cmd->pd_handle = to_vpd(pd)->pd_handle;
  310. cmd->send_cq_handle = to_vcq(init_attr->send_cq)->cq_handle;
  311. cmd->recv_cq_handle = to_vcq(init_attr->recv_cq)->cq_handle;
  312. if (is_srq)
  313. cmd->srq_handle = to_vsrq(init_attr->srq)->srq_handle;
  314. else
  315. cmd->srq_handle = 0;
  316. cmd->max_send_wr = init_attr->cap.max_send_wr;
  317. cmd->max_recv_wr = init_attr->cap.max_recv_wr;
  318. cmd->max_send_sge = init_attr->cap.max_send_sge;
  319. cmd->max_recv_sge = init_attr->cap.max_recv_sge;
  320. cmd->max_inline_data = init_attr->cap.max_inline_data;
  321. cmd->sq_sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) ? 1 : 0;
  322. cmd->qp_type = ib_qp_type_to_pvrdma(init_attr->qp_type);
  323. cmd->is_srq = is_srq;
  324. cmd->lkey = 0;
  325. cmd->access_flags = IB_ACCESS_LOCAL_WRITE;
  326. cmd->total_chunks = qp->npages;
  327. cmd->send_chunks = qp->npages_send - PVRDMA_QP_NUM_HEADER_PAGES;
  328. cmd->pdir_dma = qp->pdir.dir_dma;
  329. dev_dbg(&dev->pdev->dev, "create queuepair with %d, %d, %d, %d\n",
  330. cmd->max_send_wr, cmd->max_recv_wr, cmd->max_send_sge,
  331. cmd->max_recv_sge);
  332. ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_QP_RESP);
  333. if (ret < 0) {
  334. dev_warn(&dev->pdev->dev,
  335. "could not create queuepair, error: %d\n", ret);
  336. goto err_pdir;
  337. }
  338. /* max_send_wr/_recv_wr/_send_sge/_recv_sge/_inline_data */
  339. qp->qp_handle = resp->qpn;
  340. qp->port = init_attr->port_num;
  341. qp->ibqp.qp_num = resp->qpn;
  342. spin_lock_irqsave(&dev->qp_tbl_lock, flags);
  343. dev->qp_tbl[qp->qp_handle % dev->dsr->caps.max_qp] = qp;
  344. spin_unlock_irqrestore(&dev->qp_tbl_lock, flags);
  345. return &qp->ibqp;
  346. err_pdir:
  347. pvrdma_page_dir_cleanup(dev, &qp->pdir);
  348. err_umem:
  349. if (!qp->is_kernel) {
  350. if (qp->rumem)
  351. ib_umem_release(qp->rumem);
  352. if (qp->sumem)
  353. ib_umem_release(qp->sumem);
  354. }
  355. err_qp:
  356. kfree(qp);
  357. atomic_dec(&dev->num_qps);
  358. return ERR_PTR(ret);
  359. }
  360. static void pvrdma_free_qp(struct pvrdma_qp *qp)
  361. {
  362. struct pvrdma_dev *dev = to_vdev(qp->ibqp.device);
  363. struct pvrdma_cq *scq;
  364. struct pvrdma_cq *rcq;
  365. unsigned long flags, scq_flags, rcq_flags;
  366. /* In case cq is polling */
  367. get_cqs(qp, &scq, &rcq);
  368. pvrdma_lock_cqs(scq, rcq, &scq_flags, &rcq_flags);
  369. _pvrdma_flush_cqe(qp, scq);
  370. if (scq != rcq)
  371. _pvrdma_flush_cqe(qp, rcq);
  372. spin_lock_irqsave(&dev->qp_tbl_lock, flags);
  373. dev->qp_tbl[qp->qp_handle] = NULL;
  374. spin_unlock_irqrestore(&dev->qp_tbl_lock, flags);
  375. pvrdma_unlock_cqs(scq, rcq, &scq_flags, &rcq_flags);
  376. if (refcount_dec_and_test(&qp->refcnt))
  377. complete(&qp->free);
  378. wait_for_completion(&qp->free);
  379. if (!qp->is_kernel) {
  380. if (qp->rumem)
  381. ib_umem_release(qp->rumem);
  382. if (qp->sumem)
  383. ib_umem_release(qp->sumem);
  384. }
  385. pvrdma_page_dir_cleanup(dev, &qp->pdir);
  386. kfree(qp);
  387. atomic_dec(&dev->num_qps);
  388. }
  389. /**
  390. * pvrdma_destroy_qp - destroy a queue pair
  391. * @qp: the queue pair to destroy
  392. *
  393. * @return: 0 on success.
  394. */
  395. int pvrdma_destroy_qp(struct ib_qp *qp)
  396. {
  397. struct pvrdma_qp *vqp = to_vqp(qp);
  398. union pvrdma_cmd_req req;
  399. struct pvrdma_cmd_destroy_qp *cmd = &req.destroy_qp;
  400. int ret;
  401. memset(cmd, 0, sizeof(*cmd));
  402. cmd->hdr.cmd = PVRDMA_CMD_DESTROY_QP;
  403. cmd->qp_handle = vqp->qp_handle;
  404. ret = pvrdma_cmd_post(to_vdev(qp->device), &req, NULL, 0);
  405. if (ret < 0)
  406. dev_warn(&to_vdev(qp->device)->pdev->dev,
  407. "destroy queuepair failed, error: %d\n", ret);
  408. pvrdma_free_qp(vqp);
  409. return 0;
  410. }
  411. /**
  412. * pvrdma_modify_qp - modify queue pair attributes
  413. * @ibqp: the queue pair
  414. * @attr: the new queue pair's attributes
  415. * @attr_mask: attributes mask
  416. * @udata: user data
  417. *
  418. * @returns 0 on success, otherwise returns an errno.
  419. */
  420. int pvrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  421. int attr_mask, struct ib_udata *udata)
  422. {
  423. struct pvrdma_dev *dev = to_vdev(ibqp->device);
  424. struct pvrdma_qp *qp = to_vqp(ibqp);
  425. union pvrdma_cmd_req req;
  426. union pvrdma_cmd_resp rsp;
  427. struct pvrdma_cmd_modify_qp *cmd = &req.modify_qp;
  428. enum ib_qp_state cur_state, next_state;
  429. int ret;
  430. /* Sanity checking. Should need lock here */
  431. mutex_lock(&qp->mutex);
  432. cur_state = (attr_mask & IB_QP_CUR_STATE) ? attr->cur_qp_state :
  433. qp->state;
  434. next_state = (attr_mask & IB_QP_STATE) ? attr->qp_state : cur_state;
  435. if (!ib_modify_qp_is_ok(cur_state, next_state, ibqp->qp_type,
  436. attr_mask, IB_LINK_LAYER_ETHERNET)) {
  437. ret = -EINVAL;
  438. goto out;
  439. }
  440. if (attr_mask & IB_QP_PORT) {
  441. if (attr->port_num == 0 ||
  442. attr->port_num > ibqp->device->phys_port_cnt) {
  443. ret = -EINVAL;
  444. goto out;
  445. }
  446. }
  447. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  448. if (attr->min_rnr_timer > 31) {
  449. ret = -EINVAL;
  450. goto out;
  451. }
  452. }
  453. if (attr_mask & IB_QP_PKEY_INDEX) {
  454. if (attr->pkey_index >= dev->dsr->caps.max_pkeys) {
  455. ret = -EINVAL;
  456. goto out;
  457. }
  458. }
  459. if (attr_mask & IB_QP_QKEY)
  460. qp->qkey = attr->qkey;
  461. if (cur_state == next_state && cur_state == IB_QPS_RESET) {
  462. ret = 0;
  463. goto out;
  464. }
  465. qp->state = next_state;
  466. memset(cmd, 0, sizeof(*cmd));
  467. cmd->hdr.cmd = PVRDMA_CMD_MODIFY_QP;
  468. cmd->qp_handle = qp->qp_handle;
  469. cmd->attr_mask = ib_qp_attr_mask_to_pvrdma(attr_mask);
  470. cmd->attrs.qp_state = ib_qp_state_to_pvrdma(attr->qp_state);
  471. cmd->attrs.cur_qp_state =
  472. ib_qp_state_to_pvrdma(attr->cur_qp_state);
  473. cmd->attrs.path_mtu = ib_mtu_to_pvrdma(attr->path_mtu);
  474. cmd->attrs.path_mig_state =
  475. ib_mig_state_to_pvrdma(attr->path_mig_state);
  476. cmd->attrs.qkey = attr->qkey;
  477. cmd->attrs.rq_psn = attr->rq_psn;
  478. cmd->attrs.sq_psn = attr->sq_psn;
  479. cmd->attrs.dest_qp_num = attr->dest_qp_num;
  480. cmd->attrs.qp_access_flags =
  481. ib_access_flags_to_pvrdma(attr->qp_access_flags);
  482. cmd->attrs.pkey_index = attr->pkey_index;
  483. cmd->attrs.alt_pkey_index = attr->alt_pkey_index;
  484. cmd->attrs.en_sqd_async_notify = attr->en_sqd_async_notify;
  485. cmd->attrs.sq_draining = attr->sq_draining;
  486. cmd->attrs.max_rd_atomic = attr->max_rd_atomic;
  487. cmd->attrs.max_dest_rd_atomic = attr->max_dest_rd_atomic;
  488. cmd->attrs.min_rnr_timer = attr->min_rnr_timer;
  489. cmd->attrs.port_num = attr->port_num;
  490. cmd->attrs.timeout = attr->timeout;
  491. cmd->attrs.retry_cnt = attr->retry_cnt;
  492. cmd->attrs.rnr_retry = attr->rnr_retry;
  493. cmd->attrs.alt_port_num = attr->alt_port_num;
  494. cmd->attrs.alt_timeout = attr->alt_timeout;
  495. ib_qp_cap_to_pvrdma(&cmd->attrs.cap, &attr->cap);
  496. rdma_ah_attr_to_pvrdma(&cmd->attrs.ah_attr, &attr->ah_attr);
  497. rdma_ah_attr_to_pvrdma(&cmd->attrs.alt_ah_attr, &attr->alt_ah_attr);
  498. ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_MODIFY_QP_RESP);
  499. if (ret < 0) {
  500. dev_warn(&dev->pdev->dev,
  501. "could not modify queuepair, error: %d\n", ret);
  502. } else if (rsp.hdr.err > 0) {
  503. dev_warn(&dev->pdev->dev,
  504. "cannot modify queuepair, error: %d\n", rsp.hdr.err);
  505. ret = -EINVAL;
  506. }
  507. if (ret == 0 && next_state == IB_QPS_RESET)
  508. pvrdma_reset_qp(qp);
  509. out:
  510. mutex_unlock(&qp->mutex);
  511. return ret;
  512. }
  513. static inline void *get_sq_wqe(struct pvrdma_qp *qp, unsigned int n)
  514. {
  515. return pvrdma_page_dir_get_ptr(&qp->pdir,
  516. qp->sq.offset + n * qp->sq.wqe_size);
  517. }
  518. static inline void *get_rq_wqe(struct pvrdma_qp *qp, unsigned int n)
  519. {
  520. return pvrdma_page_dir_get_ptr(&qp->pdir,
  521. qp->rq.offset + n * qp->rq.wqe_size);
  522. }
  523. static int set_reg_seg(struct pvrdma_sq_wqe_hdr *wqe_hdr, struct ib_reg_wr *wr)
  524. {
  525. struct pvrdma_user_mr *mr = to_vmr(wr->mr);
  526. wqe_hdr->wr.fast_reg.iova_start = mr->ibmr.iova;
  527. wqe_hdr->wr.fast_reg.pl_pdir_dma = mr->pdir.dir_dma;
  528. wqe_hdr->wr.fast_reg.page_shift = mr->page_shift;
  529. wqe_hdr->wr.fast_reg.page_list_len = mr->npages;
  530. wqe_hdr->wr.fast_reg.length = mr->ibmr.length;
  531. wqe_hdr->wr.fast_reg.access_flags = wr->access;
  532. wqe_hdr->wr.fast_reg.rkey = wr->key;
  533. return pvrdma_page_dir_insert_page_list(&mr->pdir, mr->pages,
  534. mr->npages);
  535. }
  536. /**
  537. * pvrdma_post_send - post send work request entries on a QP
  538. * @ibqp: the QP
  539. * @wr: work request list to post
  540. * @bad_wr: the first bad WR returned
  541. *
  542. * @return: 0 on success, otherwise errno returned.
  543. */
  544. int pvrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  545. struct ib_send_wr **bad_wr)
  546. {
  547. struct pvrdma_qp *qp = to_vqp(ibqp);
  548. struct pvrdma_dev *dev = to_vdev(ibqp->device);
  549. unsigned long flags;
  550. struct pvrdma_sq_wqe_hdr *wqe_hdr;
  551. struct pvrdma_sge *sge;
  552. int i, ret;
  553. /*
  554. * In states lower than RTS, we can fail immediately. In other states,
  555. * just post and let the device figure it out.
  556. */
  557. if (qp->state < IB_QPS_RTS) {
  558. *bad_wr = wr;
  559. return -EINVAL;
  560. }
  561. spin_lock_irqsave(&qp->sq.lock, flags);
  562. while (wr) {
  563. unsigned int tail = 0;
  564. if (unlikely(!pvrdma_idx_ring_has_space(
  565. qp->sq.ring, qp->sq.wqe_cnt, &tail))) {
  566. dev_warn_ratelimited(&dev->pdev->dev,
  567. "send queue is full\n");
  568. *bad_wr = wr;
  569. ret = -ENOMEM;
  570. goto out;
  571. }
  572. if (unlikely(wr->num_sge > qp->sq.max_sg || wr->num_sge < 0)) {
  573. dev_warn_ratelimited(&dev->pdev->dev,
  574. "send SGE overflow\n");
  575. *bad_wr = wr;
  576. ret = -EINVAL;
  577. goto out;
  578. }
  579. if (unlikely(wr->opcode < 0)) {
  580. dev_warn_ratelimited(&dev->pdev->dev,
  581. "invalid send opcode\n");
  582. *bad_wr = wr;
  583. ret = -EINVAL;
  584. goto out;
  585. }
  586. /*
  587. * Only support UD, RC.
  588. * Need to check opcode table for thorough checking.
  589. * opcode _UD _UC _RC
  590. * _SEND x x x
  591. * _SEND_WITH_IMM x x x
  592. * _RDMA_WRITE x x
  593. * _RDMA_WRITE_WITH_IMM x x
  594. * _LOCAL_INV x x
  595. * _SEND_WITH_INV x x
  596. * _RDMA_READ x
  597. * _ATOMIC_CMP_AND_SWP x
  598. * _ATOMIC_FETCH_AND_ADD x
  599. * _MASK_ATOMIC_CMP_AND_SWP x
  600. * _MASK_ATOMIC_FETCH_AND_ADD x
  601. * _REG_MR x
  602. *
  603. */
  604. if (qp->ibqp.qp_type != IB_QPT_UD &&
  605. qp->ibqp.qp_type != IB_QPT_RC &&
  606. wr->opcode != IB_WR_SEND) {
  607. dev_warn_ratelimited(&dev->pdev->dev,
  608. "unsupported queuepair type\n");
  609. *bad_wr = wr;
  610. ret = -EINVAL;
  611. goto out;
  612. } else if (qp->ibqp.qp_type == IB_QPT_UD ||
  613. qp->ibqp.qp_type == IB_QPT_GSI) {
  614. if (wr->opcode != IB_WR_SEND &&
  615. wr->opcode != IB_WR_SEND_WITH_IMM) {
  616. dev_warn_ratelimited(&dev->pdev->dev,
  617. "invalid send opcode\n");
  618. *bad_wr = wr;
  619. ret = -EINVAL;
  620. goto out;
  621. }
  622. }
  623. wqe_hdr = (struct pvrdma_sq_wqe_hdr *)get_sq_wqe(qp, tail);
  624. memset(wqe_hdr, 0, sizeof(*wqe_hdr));
  625. wqe_hdr->wr_id = wr->wr_id;
  626. wqe_hdr->num_sge = wr->num_sge;
  627. wqe_hdr->opcode = ib_wr_opcode_to_pvrdma(wr->opcode);
  628. wqe_hdr->send_flags = ib_send_flags_to_pvrdma(wr->send_flags);
  629. if (wr->opcode == IB_WR_SEND_WITH_IMM ||
  630. wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  631. wqe_hdr->ex.imm_data = wr->ex.imm_data;
  632. switch (qp->ibqp.qp_type) {
  633. case IB_QPT_GSI:
  634. case IB_QPT_UD:
  635. if (unlikely(!ud_wr(wr)->ah)) {
  636. dev_warn_ratelimited(&dev->pdev->dev,
  637. "invalid address handle\n");
  638. *bad_wr = wr;
  639. ret = -EINVAL;
  640. goto out;
  641. }
  642. /*
  643. * Use qkey from qp context if high order bit set,
  644. * otherwise from work request.
  645. */
  646. wqe_hdr->wr.ud.remote_qpn = ud_wr(wr)->remote_qpn;
  647. wqe_hdr->wr.ud.remote_qkey =
  648. ud_wr(wr)->remote_qkey & 0x80000000 ?
  649. qp->qkey : ud_wr(wr)->remote_qkey;
  650. wqe_hdr->wr.ud.av = to_vah(ud_wr(wr)->ah)->av;
  651. break;
  652. case IB_QPT_RC:
  653. switch (wr->opcode) {
  654. case IB_WR_RDMA_READ:
  655. case IB_WR_RDMA_WRITE:
  656. case IB_WR_RDMA_WRITE_WITH_IMM:
  657. wqe_hdr->wr.rdma.remote_addr =
  658. rdma_wr(wr)->remote_addr;
  659. wqe_hdr->wr.rdma.rkey = rdma_wr(wr)->rkey;
  660. break;
  661. case IB_WR_LOCAL_INV:
  662. case IB_WR_SEND_WITH_INV:
  663. wqe_hdr->ex.invalidate_rkey =
  664. wr->ex.invalidate_rkey;
  665. break;
  666. case IB_WR_ATOMIC_CMP_AND_SWP:
  667. case IB_WR_ATOMIC_FETCH_AND_ADD:
  668. wqe_hdr->wr.atomic.remote_addr =
  669. atomic_wr(wr)->remote_addr;
  670. wqe_hdr->wr.atomic.rkey = atomic_wr(wr)->rkey;
  671. wqe_hdr->wr.atomic.compare_add =
  672. atomic_wr(wr)->compare_add;
  673. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP)
  674. wqe_hdr->wr.atomic.swap =
  675. atomic_wr(wr)->swap;
  676. break;
  677. case IB_WR_REG_MR:
  678. ret = set_reg_seg(wqe_hdr, reg_wr(wr));
  679. if (ret < 0) {
  680. dev_warn_ratelimited(&dev->pdev->dev,
  681. "Failed to set fast register work request\n");
  682. *bad_wr = wr;
  683. goto out;
  684. }
  685. break;
  686. default:
  687. break;
  688. }
  689. break;
  690. default:
  691. dev_warn_ratelimited(&dev->pdev->dev,
  692. "invalid queuepair type\n");
  693. ret = -EINVAL;
  694. *bad_wr = wr;
  695. goto out;
  696. }
  697. sge = (struct pvrdma_sge *)(wqe_hdr + 1);
  698. for (i = 0; i < wr->num_sge; i++) {
  699. /* Need to check wqe_size 0 or max size */
  700. sge->addr = wr->sg_list[i].addr;
  701. sge->length = wr->sg_list[i].length;
  702. sge->lkey = wr->sg_list[i].lkey;
  703. sge++;
  704. }
  705. /* Make sure wqe is written before index update */
  706. smp_wmb();
  707. /* Update shared sq ring */
  708. pvrdma_idx_ring_inc(&qp->sq.ring->prod_tail,
  709. qp->sq.wqe_cnt);
  710. wr = wr->next;
  711. }
  712. ret = 0;
  713. out:
  714. spin_unlock_irqrestore(&qp->sq.lock, flags);
  715. if (!ret)
  716. pvrdma_write_uar_qp(dev, PVRDMA_UAR_QP_SEND | qp->qp_handle);
  717. return ret;
  718. }
  719. /**
  720. * pvrdma_post_receive - post receive work request entries on a QP
  721. * @ibqp: the QP
  722. * @wr: the work request list to post
  723. * @bad_wr: the first bad WR returned
  724. *
  725. * @return: 0 on success, otherwise errno returned.
  726. */
  727. int pvrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  728. struct ib_recv_wr **bad_wr)
  729. {
  730. struct pvrdma_dev *dev = to_vdev(ibqp->device);
  731. unsigned long flags;
  732. struct pvrdma_qp *qp = to_vqp(ibqp);
  733. struct pvrdma_rq_wqe_hdr *wqe_hdr;
  734. struct pvrdma_sge *sge;
  735. int ret = 0;
  736. int i;
  737. /*
  738. * In the RESET state, we can fail immediately. For other states,
  739. * just post and let the device figure it out.
  740. */
  741. if (qp->state == IB_QPS_RESET) {
  742. *bad_wr = wr;
  743. return -EINVAL;
  744. }
  745. if (qp->srq) {
  746. dev_warn(&dev->pdev->dev, "QP associated with SRQ\n");
  747. *bad_wr = wr;
  748. return -EINVAL;
  749. }
  750. spin_lock_irqsave(&qp->rq.lock, flags);
  751. while (wr) {
  752. unsigned int tail = 0;
  753. if (unlikely(wr->num_sge > qp->rq.max_sg ||
  754. wr->num_sge < 0)) {
  755. ret = -EINVAL;
  756. *bad_wr = wr;
  757. dev_warn_ratelimited(&dev->pdev->dev,
  758. "recv SGE overflow\n");
  759. goto out;
  760. }
  761. if (unlikely(!pvrdma_idx_ring_has_space(
  762. qp->rq.ring, qp->rq.wqe_cnt, &tail))) {
  763. ret = -ENOMEM;
  764. *bad_wr = wr;
  765. dev_warn_ratelimited(&dev->pdev->dev,
  766. "recv queue full\n");
  767. goto out;
  768. }
  769. wqe_hdr = (struct pvrdma_rq_wqe_hdr *)get_rq_wqe(qp, tail);
  770. wqe_hdr->wr_id = wr->wr_id;
  771. wqe_hdr->num_sge = wr->num_sge;
  772. wqe_hdr->total_len = 0;
  773. sge = (struct pvrdma_sge *)(wqe_hdr + 1);
  774. for (i = 0; i < wr->num_sge; i++) {
  775. sge->addr = wr->sg_list[i].addr;
  776. sge->length = wr->sg_list[i].length;
  777. sge->lkey = wr->sg_list[i].lkey;
  778. sge++;
  779. }
  780. /* Make sure wqe is written before index update */
  781. smp_wmb();
  782. /* Update shared rq ring */
  783. pvrdma_idx_ring_inc(&qp->rq.ring->prod_tail,
  784. qp->rq.wqe_cnt);
  785. wr = wr->next;
  786. }
  787. spin_unlock_irqrestore(&qp->rq.lock, flags);
  788. pvrdma_write_uar_qp(dev, PVRDMA_UAR_QP_RECV | qp->qp_handle);
  789. return ret;
  790. out:
  791. spin_unlock_irqrestore(&qp->rq.lock, flags);
  792. return ret;
  793. }
  794. /**
  795. * pvrdma_query_qp - query a queue pair's attributes
  796. * @ibqp: the queue pair to query
  797. * @attr: the queue pair's attributes
  798. * @attr_mask: attributes mask
  799. * @init_attr: initial queue pair attributes
  800. *
  801. * @returns 0 on success, otherwise returns an errno.
  802. */
  803. int pvrdma_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  804. int attr_mask, struct ib_qp_init_attr *init_attr)
  805. {
  806. struct pvrdma_dev *dev = to_vdev(ibqp->device);
  807. struct pvrdma_qp *qp = to_vqp(ibqp);
  808. union pvrdma_cmd_req req;
  809. union pvrdma_cmd_resp rsp;
  810. struct pvrdma_cmd_query_qp *cmd = &req.query_qp;
  811. struct pvrdma_cmd_query_qp_resp *resp = &rsp.query_qp_resp;
  812. int ret = 0;
  813. mutex_lock(&qp->mutex);
  814. if (qp->state == IB_QPS_RESET) {
  815. attr->qp_state = IB_QPS_RESET;
  816. goto out;
  817. }
  818. memset(cmd, 0, sizeof(*cmd));
  819. cmd->hdr.cmd = PVRDMA_CMD_QUERY_QP;
  820. cmd->qp_handle = qp->qp_handle;
  821. cmd->attr_mask = ib_qp_attr_mask_to_pvrdma(attr_mask);
  822. ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_QUERY_QP_RESP);
  823. if (ret < 0) {
  824. dev_warn(&dev->pdev->dev,
  825. "could not query queuepair, error: %d\n", ret);
  826. goto out;
  827. }
  828. attr->qp_state = pvrdma_qp_state_to_ib(resp->attrs.qp_state);
  829. attr->cur_qp_state =
  830. pvrdma_qp_state_to_ib(resp->attrs.cur_qp_state);
  831. attr->path_mtu = pvrdma_mtu_to_ib(resp->attrs.path_mtu);
  832. attr->path_mig_state =
  833. pvrdma_mig_state_to_ib(resp->attrs.path_mig_state);
  834. attr->qkey = resp->attrs.qkey;
  835. attr->rq_psn = resp->attrs.rq_psn;
  836. attr->sq_psn = resp->attrs.sq_psn;
  837. attr->dest_qp_num = resp->attrs.dest_qp_num;
  838. attr->qp_access_flags =
  839. pvrdma_access_flags_to_ib(resp->attrs.qp_access_flags);
  840. attr->pkey_index = resp->attrs.pkey_index;
  841. attr->alt_pkey_index = resp->attrs.alt_pkey_index;
  842. attr->en_sqd_async_notify = resp->attrs.en_sqd_async_notify;
  843. attr->sq_draining = resp->attrs.sq_draining;
  844. attr->max_rd_atomic = resp->attrs.max_rd_atomic;
  845. attr->max_dest_rd_atomic = resp->attrs.max_dest_rd_atomic;
  846. attr->min_rnr_timer = resp->attrs.min_rnr_timer;
  847. attr->port_num = resp->attrs.port_num;
  848. attr->timeout = resp->attrs.timeout;
  849. attr->retry_cnt = resp->attrs.retry_cnt;
  850. attr->rnr_retry = resp->attrs.rnr_retry;
  851. attr->alt_port_num = resp->attrs.alt_port_num;
  852. attr->alt_timeout = resp->attrs.alt_timeout;
  853. pvrdma_qp_cap_to_ib(&attr->cap, &resp->attrs.cap);
  854. pvrdma_ah_attr_to_rdma(&attr->ah_attr, &resp->attrs.ah_attr);
  855. pvrdma_ah_attr_to_rdma(&attr->alt_ah_attr, &resp->attrs.alt_ah_attr);
  856. qp->state = attr->qp_state;
  857. ret = 0;
  858. out:
  859. attr->cur_qp_state = attr->qp_state;
  860. init_attr->event_handler = qp->ibqp.event_handler;
  861. init_attr->qp_context = qp->ibqp.qp_context;
  862. init_attr->send_cq = qp->ibqp.send_cq;
  863. init_attr->recv_cq = qp->ibqp.recv_cq;
  864. init_attr->srq = qp->ibqp.srq;
  865. init_attr->xrcd = NULL;
  866. init_attr->cap = attr->cap;
  867. init_attr->sq_sig_type = 0;
  868. init_attr->qp_type = qp->ibqp.qp_type;
  869. init_attr->create_flags = 0;
  870. init_attr->port_num = qp->port;
  871. mutex_unlock(&qp->mutex);
  872. return ret;
  873. }