hns_roce_qp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*
  2. * Copyright (c) 2016 Hisilicon Limited.
  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/platform_device.h>
  34. #include <rdma/ib_addr.h>
  35. #include <rdma/ib_umem.h>
  36. #include "hns_roce_common.h"
  37. #include "hns_roce_device.h"
  38. #include "hns_roce_hem.h"
  39. #include "hns_roce_user.h"
  40. #define SQP_NUM (2 * HNS_ROCE_MAX_PORTS)
  41. void hns_roce_qp_event(struct hns_roce_dev *hr_dev, u32 qpn, int event_type)
  42. {
  43. struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
  44. struct device *dev = &hr_dev->pdev->dev;
  45. struct hns_roce_qp *qp;
  46. spin_lock(&qp_table->lock);
  47. qp = __hns_roce_qp_lookup(hr_dev, qpn);
  48. if (qp)
  49. atomic_inc(&qp->refcount);
  50. spin_unlock(&qp_table->lock);
  51. if (!qp) {
  52. dev_warn(dev, "Async event for bogus QP %08x\n", qpn);
  53. return;
  54. }
  55. qp->event(qp, (enum hns_roce_event)event_type);
  56. if (atomic_dec_and_test(&qp->refcount))
  57. complete(&qp->free);
  58. }
  59. static void hns_roce_ib_qp_event(struct hns_roce_qp *hr_qp,
  60. enum hns_roce_event type)
  61. {
  62. struct ib_event event;
  63. struct ib_qp *ibqp = &hr_qp->ibqp;
  64. if (ibqp->event_handler) {
  65. event.device = ibqp->device;
  66. event.element.qp = ibqp;
  67. switch (type) {
  68. case HNS_ROCE_EVENT_TYPE_PATH_MIG:
  69. event.event = IB_EVENT_PATH_MIG;
  70. break;
  71. case HNS_ROCE_EVENT_TYPE_COMM_EST:
  72. event.event = IB_EVENT_COMM_EST;
  73. break;
  74. case HNS_ROCE_EVENT_TYPE_SQ_DRAINED:
  75. event.event = IB_EVENT_SQ_DRAINED;
  76. break;
  77. case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH:
  78. event.event = IB_EVENT_QP_LAST_WQE_REACHED;
  79. break;
  80. case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR:
  81. event.event = IB_EVENT_QP_FATAL;
  82. break;
  83. case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED:
  84. event.event = IB_EVENT_PATH_MIG_ERR;
  85. break;
  86. case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR:
  87. event.event = IB_EVENT_QP_REQ_ERR;
  88. break;
  89. case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR:
  90. event.event = IB_EVENT_QP_ACCESS_ERR;
  91. break;
  92. default:
  93. dev_dbg(ibqp->device->dma_device, "roce_ib: Unexpected event type %d on QP %06lx\n",
  94. type, hr_qp->qpn);
  95. return;
  96. }
  97. ibqp->event_handler(&event, ibqp->qp_context);
  98. }
  99. }
  100. static int hns_roce_reserve_range_qp(struct hns_roce_dev *hr_dev, int cnt,
  101. int align, unsigned long *base)
  102. {
  103. struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
  104. return hns_roce_bitmap_alloc_range(&qp_table->bitmap, cnt, align, base);
  105. }
  106. enum hns_roce_qp_state to_hns_roce_state(enum ib_qp_state state)
  107. {
  108. switch (state) {
  109. case IB_QPS_RESET:
  110. return HNS_ROCE_QP_STATE_RST;
  111. case IB_QPS_INIT:
  112. return HNS_ROCE_QP_STATE_INIT;
  113. case IB_QPS_RTR:
  114. return HNS_ROCE_QP_STATE_RTR;
  115. case IB_QPS_RTS:
  116. return HNS_ROCE_QP_STATE_RTS;
  117. case IB_QPS_SQD:
  118. return HNS_ROCE_QP_STATE_SQD;
  119. case IB_QPS_ERR:
  120. return HNS_ROCE_QP_STATE_ERR;
  121. default:
  122. return HNS_ROCE_QP_NUM_STATE;
  123. }
  124. }
  125. static int hns_roce_gsi_qp_alloc(struct hns_roce_dev *hr_dev, unsigned long qpn,
  126. struct hns_roce_qp *hr_qp)
  127. {
  128. struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
  129. int ret;
  130. if (!qpn)
  131. return -EINVAL;
  132. hr_qp->qpn = qpn;
  133. spin_lock_irq(&qp_table->lock);
  134. ret = radix_tree_insert(&hr_dev->qp_table_tree,
  135. hr_qp->qpn & (hr_dev->caps.num_qps - 1), hr_qp);
  136. spin_unlock_irq(&qp_table->lock);
  137. if (ret) {
  138. dev_err(&hr_dev->pdev->dev, "QPC radix_tree_insert failed\n");
  139. goto err_put_irrl;
  140. }
  141. atomic_set(&hr_qp->refcount, 1);
  142. init_completion(&hr_qp->free);
  143. return 0;
  144. err_put_irrl:
  145. return ret;
  146. }
  147. static int hns_roce_qp_alloc(struct hns_roce_dev *hr_dev, unsigned long qpn,
  148. struct hns_roce_qp *hr_qp)
  149. {
  150. struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
  151. struct device *dev = &hr_dev->pdev->dev;
  152. int ret;
  153. if (!qpn)
  154. return -EINVAL;
  155. hr_qp->qpn = qpn;
  156. /* Alloc memory for QPC */
  157. ret = hns_roce_table_get(hr_dev, &qp_table->qp_table, hr_qp->qpn);
  158. if (ret) {
  159. dev_err(dev, "QPC table get failed\n");
  160. goto err_out;
  161. }
  162. /* Alloc memory for IRRL */
  163. ret = hns_roce_table_get(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
  164. if (ret) {
  165. dev_err(dev, "IRRL table get failed\n");
  166. goto err_put_qp;
  167. }
  168. spin_lock_irq(&qp_table->lock);
  169. ret = radix_tree_insert(&hr_dev->qp_table_tree,
  170. hr_qp->qpn & (hr_dev->caps.num_qps - 1), hr_qp);
  171. spin_unlock_irq(&qp_table->lock);
  172. if (ret) {
  173. dev_err(dev, "QPC radix_tree_insert failed\n");
  174. goto err_put_irrl;
  175. }
  176. atomic_set(&hr_qp->refcount, 1);
  177. init_completion(&hr_qp->free);
  178. return 0;
  179. err_put_irrl:
  180. hns_roce_table_put(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
  181. err_put_qp:
  182. hns_roce_table_put(hr_dev, &qp_table->qp_table, hr_qp->qpn);
  183. err_out:
  184. return ret;
  185. }
  186. void hns_roce_qp_remove(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
  187. {
  188. struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
  189. unsigned long flags;
  190. spin_lock_irqsave(&qp_table->lock, flags);
  191. radix_tree_delete(&hr_dev->qp_table_tree,
  192. hr_qp->qpn & (hr_dev->caps.num_qps - 1));
  193. spin_unlock_irqrestore(&qp_table->lock, flags);
  194. }
  195. void hns_roce_qp_free(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
  196. {
  197. struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
  198. if (atomic_dec_and_test(&hr_qp->refcount))
  199. complete(&hr_qp->free);
  200. wait_for_completion(&hr_qp->free);
  201. if ((hr_qp->ibqp.qp_type) != IB_QPT_GSI) {
  202. hns_roce_table_put(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
  203. hns_roce_table_put(hr_dev, &qp_table->qp_table, hr_qp->qpn);
  204. }
  205. }
  206. void hns_roce_release_range_qp(struct hns_roce_dev *hr_dev, int base_qpn,
  207. int cnt)
  208. {
  209. struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
  210. if (base_qpn < SQP_NUM)
  211. return;
  212. hns_roce_bitmap_free_range(&qp_table->bitmap, base_qpn, cnt);
  213. }
  214. static int hns_roce_set_rq_size(struct hns_roce_dev *hr_dev,
  215. struct ib_qp_cap *cap, int is_user, int has_srq,
  216. struct hns_roce_qp *hr_qp)
  217. {
  218. u32 max_cnt;
  219. struct device *dev = &hr_dev->pdev->dev;
  220. /* Check the validity of QP support capacity */
  221. if (cap->max_recv_wr > hr_dev->caps.max_wqes ||
  222. cap->max_recv_sge > hr_dev->caps.max_rq_sg) {
  223. dev_err(dev, "RQ WR or sge error!max_recv_wr=%d max_recv_sge=%d\n",
  224. cap->max_recv_wr, cap->max_recv_sge);
  225. return -EINVAL;
  226. }
  227. /* If srq exit, set zero for relative number of rq */
  228. if (has_srq) {
  229. if (cap->max_recv_wr) {
  230. dev_dbg(dev, "srq no need config max_recv_wr\n");
  231. return -EINVAL;
  232. }
  233. hr_qp->rq.wqe_cnt = hr_qp->rq.max_gs = 0;
  234. } else {
  235. if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge)) {
  236. dev_err(dev, "user space no need config max_recv_wr max_recv_sge\n");
  237. return -EINVAL;
  238. }
  239. /* In v1 engine, parameter verification procession */
  240. max_cnt = cap->max_recv_wr > HNS_ROCE_MIN_WQE_NUM ?
  241. cap->max_recv_wr : HNS_ROCE_MIN_WQE_NUM;
  242. hr_qp->rq.wqe_cnt = roundup_pow_of_two(max_cnt);
  243. if ((u32)hr_qp->rq.wqe_cnt > hr_dev->caps.max_wqes) {
  244. dev_err(dev, "hns_roce_set_rq_size rq.wqe_cnt too large\n");
  245. return -EINVAL;
  246. }
  247. max_cnt = max(1U, cap->max_recv_sge);
  248. hr_qp->rq.max_gs = roundup_pow_of_two(max_cnt);
  249. /* WQE is fixed for 64B */
  250. hr_qp->rq.wqe_shift = ilog2(hr_dev->caps.max_rq_desc_sz);
  251. }
  252. cap->max_recv_wr = hr_qp->rq.max_post = hr_qp->rq.wqe_cnt;
  253. cap->max_recv_sge = hr_qp->rq.max_gs;
  254. return 0;
  255. }
  256. static int hns_roce_set_user_sq_size(struct hns_roce_dev *hr_dev,
  257. struct hns_roce_qp *hr_qp,
  258. struct hns_roce_ib_create_qp *ucmd)
  259. {
  260. u32 roundup_sq_stride = roundup_pow_of_two(hr_dev->caps.max_sq_desc_sz);
  261. u8 max_sq_stride = ilog2(roundup_sq_stride);
  262. /* Sanity check SQ size before proceeding */
  263. if ((u32)(1 << ucmd->log_sq_bb_count) > hr_dev->caps.max_wqes ||
  264. ucmd->log_sq_stride > max_sq_stride ||
  265. ucmd->log_sq_stride < HNS_ROCE_IB_MIN_SQ_STRIDE) {
  266. dev_err(&hr_dev->pdev->dev, "check SQ size error!\n");
  267. return -EINVAL;
  268. }
  269. hr_qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count;
  270. hr_qp->sq.wqe_shift = ucmd->log_sq_stride;
  271. /* Get buf size, SQ and RQ are aligned to page_szie */
  272. hr_qp->buff_size = HNS_ROCE_ALOGN_UP((hr_qp->rq.wqe_cnt <<
  273. hr_qp->rq.wqe_shift), PAGE_SIZE) +
  274. HNS_ROCE_ALOGN_UP((hr_qp->sq.wqe_cnt <<
  275. hr_qp->sq.wqe_shift), PAGE_SIZE);
  276. hr_qp->sq.offset = 0;
  277. hr_qp->rq.offset = HNS_ROCE_ALOGN_UP((hr_qp->sq.wqe_cnt <<
  278. hr_qp->sq.wqe_shift), PAGE_SIZE);
  279. return 0;
  280. }
  281. static int hns_roce_set_kernel_sq_size(struct hns_roce_dev *hr_dev,
  282. struct ib_qp_cap *cap,
  283. struct hns_roce_qp *hr_qp)
  284. {
  285. struct device *dev = &hr_dev->pdev->dev;
  286. u32 max_cnt;
  287. if (cap->max_send_wr > hr_dev->caps.max_wqes ||
  288. cap->max_send_sge > hr_dev->caps.max_sq_sg ||
  289. cap->max_inline_data > hr_dev->caps.max_sq_inline) {
  290. dev_err(dev, "hns_roce_set_kernel_sq_size error1\n");
  291. return -EINVAL;
  292. }
  293. hr_qp->sq.wqe_shift = ilog2(hr_dev->caps.max_sq_desc_sz);
  294. hr_qp->sq_max_wqes_per_wr = 1;
  295. hr_qp->sq_spare_wqes = 0;
  296. /* In v1 engine, parameter verification procession */
  297. max_cnt = cap->max_send_wr > HNS_ROCE_MIN_WQE_NUM ?
  298. cap->max_send_wr : HNS_ROCE_MIN_WQE_NUM;
  299. hr_qp->sq.wqe_cnt = roundup_pow_of_two(max_cnt);
  300. if ((u32)hr_qp->sq.wqe_cnt > hr_dev->caps.max_wqes) {
  301. dev_err(dev, "hns_roce_set_kernel_sq_size sq.wqe_cnt too large\n");
  302. return -EINVAL;
  303. }
  304. /* Get data_seg numbers */
  305. max_cnt = max(1U, cap->max_send_sge);
  306. hr_qp->sq.max_gs = roundup_pow_of_two(max_cnt);
  307. /* Get buf size, SQ and RQ are aligned to page_szie */
  308. hr_qp->buff_size = HNS_ROCE_ALOGN_UP((hr_qp->rq.wqe_cnt <<
  309. hr_qp->rq.wqe_shift), PAGE_SIZE) +
  310. HNS_ROCE_ALOGN_UP((hr_qp->sq.wqe_cnt <<
  311. hr_qp->sq.wqe_shift), PAGE_SIZE);
  312. hr_qp->sq.offset = 0;
  313. hr_qp->rq.offset = HNS_ROCE_ALOGN_UP((hr_qp->sq.wqe_cnt <<
  314. hr_qp->sq.wqe_shift), PAGE_SIZE);
  315. /* Get wr and sge number which send */
  316. cap->max_send_wr = hr_qp->sq.max_post = hr_qp->sq.wqe_cnt;
  317. cap->max_send_sge = hr_qp->sq.max_gs;
  318. /* We don't support inline sends for kernel QPs (yet) */
  319. cap->max_inline_data = 0;
  320. return 0;
  321. }
  322. static int hns_roce_create_qp_common(struct hns_roce_dev *hr_dev,
  323. struct ib_pd *ib_pd,
  324. struct ib_qp_init_attr *init_attr,
  325. struct ib_udata *udata, unsigned long sqpn,
  326. struct hns_roce_qp *hr_qp)
  327. {
  328. struct device *dev = &hr_dev->pdev->dev;
  329. struct hns_roce_ib_create_qp ucmd;
  330. unsigned long qpn = 0;
  331. int ret = 0;
  332. mutex_init(&hr_qp->mutex);
  333. spin_lock_init(&hr_qp->sq.lock);
  334. spin_lock_init(&hr_qp->rq.lock);
  335. hr_qp->state = IB_QPS_RESET;
  336. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  337. hr_qp->sq_signal_bits = IB_SIGNAL_ALL_WR;
  338. else
  339. hr_qp->sq_signal_bits = IB_SIGNAL_REQ_WR;
  340. ret = hns_roce_set_rq_size(hr_dev, &init_attr->cap, !!ib_pd->uobject,
  341. !!init_attr->srq, hr_qp);
  342. if (ret) {
  343. dev_err(dev, "hns_roce_set_rq_size failed\n");
  344. goto err_out;
  345. }
  346. if (ib_pd->uobject) {
  347. if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
  348. dev_err(dev, "ib_copy_from_udata error for create qp\n");
  349. ret = -EFAULT;
  350. goto err_out;
  351. }
  352. ret = hns_roce_set_user_sq_size(hr_dev, hr_qp, &ucmd);
  353. if (ret) {
  354. dev_err(dev, "hns_roce_set_user_sq_size error for create qp\n");
  355. goto err_out;
  356. }
  357. hr_qp->umem = ib_umem_get(ib_pd->uobject->context,
  358. ucmd.buf_addr, hr_qp->buff_size, 0,
  359. 0);
  360. if (IS_ERR(hr_qp->umem)) {
  361. dev_err(dev, "ib_umem_get error for create qp\n");
  362. ret = PTR_ERR(hr_qp->umem);
  363. goto err_out;
  364. }
  365. ret = hns_roce_mtt_init(hr_dev, ib_umem_page_count(hr_qp->umem),
  366. ilog2((unsigned int)hr_qp->umem->page_size),
  367. &hr_qp->mtt);
  368. if (ret) {
  369. dev_err(dev, "hns_roce_mtt_init error for create qp\n");
  370. goto err_buf;
  371. }
  372. ret = hns_roce_ib_umem_write_mtt(hr_dev, &hr_qp->mtt,
  373. hr_qp->umem);
  374. if (ret) {
  375. dev_err(dev, "hns_roce_ib_umem_write_mtt error for create qp\n");
  376. goto err_mtt;
  377. }
  378. } else {
  379. if (init_attr->create_flags &
  380. IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
  381. dev_err(dev, "init_attr->create_flags error!\n");
  382. ret = -EINVAL;
  383. goto err_out;
  384. }
  385. if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO) {
  386. dev_err(dev, "init_attr->create_flags error!\n");
  387. ret = -EINVAL;
  388. goto err_out;
  389. }
  390. /* Set SQ size */
  391. ret = hns_roce_set_kernel_sq_size(hr_dev, &init_attr->cap,
  392. hr_qp);
  393. if (ret) {
  394. dev_err(dev, "hns_roce_set_kernel_sq_size error!\n");
  395. goto err_out;
  396. }
  397. /* QP doorbell register address */
  398. hr_qp->sq.db_reg_l = hr_dev->reg_base + ROCEE_DB_SQ_L_0_REG +
  399. DB_REG_OFFSET * hr_dev->priv_uar.index;
  400. hr_qp->rq.db_reg_l = hr_dev->reg_base +
  401. ROCEE_DB_OTHERS_L_0_REG +
  402. DB_REG_OFFSET * hr_dev->priv_uar.index;
  403. /* Allocate QP buf */
  404. if (hns_roce_buf_alloc(hr_dev, hr_qp->buff_size, PAGE_SIZE * 2,
  405. &hr_qp->hr_buf)) {
  406. dev_err(dev, "hns_roce_buf_alloc error!\n");
  407. ret = -ENOMEM;
  408. goto err_out;
  409. }
  410. /* Write MTT */
  411. ret = hns_roce_mtt_init(hr_dev, hr_qp->hr_buf.npages,
  412. hr_qp->hr_buf.page_shift, &hr_qp->mtt);
  413. if (ret) {
  414. dev_err(dev, "hns_roce_mtt_init error for kernel create qp\n");
  415. goto err_buf;
  416. }
  417. ret = hns_roce_buf_write_mtt(hr_dev, &hr_qp->mtt,
  418. &hr_qp->hr_buf);
  419. if (ret) {
  420. dev_err(dev, "hns_roce_buf_write_mtt error for kernel create qp\n");
  421. goto err_mtt;
  422. }
  423. hr_qp->sq.wrid = kmalloc_array(hr_qp->sq.wqe_cnt, sizeof(u64),
  424. GFP_KERNEL);
  425. hr_qp->rq.wrid = kmalloc_array(hr_qp->rq.wqe_cnt, sizeof(u64),
  426. GFP_KERNEL);
  427. if (!hr_qp->sq.wrid || !hr_qp->rq.wrid) {
  428. ret = -ENOMEM;
  429. goto err_wrid;
  430. }
  431. }
  432. if (sqpn) {
  433. qpn = sqpn;
  434. } else {
  435. /* Get QPN */
  436. ret = hns_roce_reserve_range_qp(hr_dev, 1, 1, &qpn);
  437. if (ret) {
  438. dev_err(dev, "hns_roce_reserve_range_qp alloc qpn error\n");
  439. goto err_wrid;
  440. }
  441. }
  442. if ((init_attr->qp_type) == IB_QPT_GSI) {
  443. ret = hns_roce_gsi_qp_alloc(hr_dev, qpn, hr_qp);
  444. if (ret) {
  445. dev_err(dev, "hns_roce_qp_alloc failed!\n");
  446. goto err_qpn;
  447. }
  448. } else {
  449. ret = hns_roce_qp_alloc(hr_dev, qpn, hr_qp);
  450. if (ret) {
  451. dev_err(dev, "hns_roce_qp_alloc failed!\n");
  452. goto err_qpn;
  453. }
  454. }
  455. if (sqpn)
  456. hr_qp->doorbell_qpn = 1;
  457. else
  458. hr_qp->doorbell_qpn = cpu_to_le64(hr_qp->qpn);
  459. hr_qp->event = hns_roce_ib_qp_event;
  460. return 0;
  461. err_qpn:
  462. if (!sqpn)
  463. hns_roce_release_range_qp(hr_dev, qpn, 1);
  464. err_wrid:
  465. kfree(hr_qp->sq.wrid);
  466. kfree(hr_qp->rq.wrid);
  467. err_mtt:
  468. hns_roce_mtt_cleanup(hr_dev, &hr_qp->mtt);
  469. err_buf:
  470. if (ib_pd->uobject)
  471. ib_umem_release(hr_qp->umem);
  472. else
  473. hns_roce_buf_free(hr_dev, hr_qp->buff_size, &hr_qp->hr_buf);
  474. err_out:
  475. return ret;
  476. }
  477. struct ib_qp *hns_roce_create_qp(struct ib_pd *pd,
  478. struct ib_qp_init_attr *init_attr,
  479. struct ib_udata *udata)
  480. {
  481. struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
  482. struct device *dev = &hr_dev->pdev->dev;
  483. struct hns_roce_sqp *hr_sqp;
  484. struct hns_roce_qp *hr_qp;
  485. int ret;
  486. switch (init_attr->qp_type) {
  487. case IB_QPT_RC: {
  488. hr_qp = kzalloc(sizeof(*hr_qp), GFP_KERNEL);
  489. if (!hr_qp)
  490. return ERR_PTR(-ENOMEM);
  491. ret = hns_roce_create_qp_common(hr_dev, pd, init_attr, udata, 0,
  492. hr_qp);
  493. if (ret) {
  494. dev_err(dev, "Create RC QP failed\n");
  495. kfree(hr_qp);
  496. return ERR_PTR(ret);
  497. }
  498. hr_qp->ibqp.qp_num = hr_qp->qpn;
  499. break;
  500. }
  501. case IB_QPT_GSI: {
  502. /* Userspace is not allowed to create special QPs: */
  503. if (pd->uobject) {
  504. dev_err(dev, "not support usr space GSI\n");
  505. return ERR_PTR(-EINVAL);
  506. }
  507. hr_sqp = kzalloc(sizeof(*hr_sqp), GFP_KERNEL);
  508. if (!hr_sqp)
  509. return ERR_PTR(-ENOMEM);
  510. hr_qp = &hr_sqp->hr_qp;
  511. hr_qp->port = init_attr->port_num - 1;
  512. hr_qp->phy_port = hr_dev->iboe.phy_port[hr_qp->port];
  513. hr_qp->ibqp.qp_num = HNS_ROCE_MAX_PORTS +
  514. hr_dev->iboe.phy_port[hr_qp->port];
  515. ret = hns_roce_create_qp_common(hr_dev, pd, init_attr, udata,
  516. hr_qp->ibqp.qp_num, hr_qp);
  517. if (ret) {
  518. dev_err(dev, "Create GSI QP failed!\n");
  519. kfree(hr_sqp);
  520. return ERR_PTR(ret);
  521. }
  522. break;
  523. }
  524. default:{
  525. dev_err(dev, "not support QP type %d\n", init_attr->qp_type);
  526. return ERR_PTR(-EINVAL);
  527. }
  528. }
  529. return &hr_qp->ibqp;
  530. }
  531. int to_hr_qp_type(int qp_type)
  532. {
  533. int transport_type;
  534. if (qp_type == IB_QPT_RC)
  535. transport_type = SERV_TYPE_RC;
  536. else if (qp_type == IB_QPT_UC)
  537. transport_type = SERV_TYPE_UC;
  538. else if (qp_type == IB_QPT_UD)
  539. transport_type = SERV_TYPE_UD;
  540. else if (qp_type == IB_QPT_GSI)
  541. transport_type = SERV_TYPE_UD;
  542. else
  543. transport_type = -1;
  544. return transport_type;
  545. }
  546. int hns_roce_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  547. int attr_mask, struct ib_udata *udata)
  548. {
  549. struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
  550. struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
  551. enum ib_qp_state cur_state, new_state;
  552. struct device *dev = &hr_dev->pdev->dev;
  553. int ret = -EINVAL;
  554. int p;
  555. enum ib_mtu active_mtu;
  556. mutex_lock(&hr_qp->mutex);
  557. cur_state = attr_mask & IB_QP_CUR_STATE ?
  558. attr->cur_qp_state : (enum ib_qp_state)hr_qp->state;
  559. new_state = attr_mask & IB_QP_STATE ?
  560. attr->qp_state : cur_state;
  561. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask,
  562. IB_LINK_LAYER_ETHERNET)) {
  563. dev_err(dev, "ib_modify_qp_is_ok failed\n");
  564. goto out;
  565. }
  566. if ((attr_mask & IB_QP_PORT) &&
  567. (attr->port_num == 0 || attr->port_num > hr_dev->caps.num_ports)) {
  568. dev_err(dev, "attr port_num invalid.attr->port_num=%d\n",
  569. attr->port_num);
  570. goto out;
  571. }
  572. if (attr_mask & IB_QP_PKEY_INDEX) {
  573. p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port;
  574. if (attr->pkey_index >= hr_dev->caps.pkey_table_len[p]) {
  575. dev_err(dev, "attr pkey_index invalid.attr->pkey_index=%d\n",
  576. attr->pkey_index);
  577. goto out;
  578. }
  579. }
  580. if (attr_mask & IB_QP_PATH_MTU) {
  581. p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port;
  582. active_mtu = iboe_get_mtu(hr_dev->iboe.netdevs[p]->mtu);
  583. if (attr->path_mtu > IB_MTU_2048 ||
  584. attr->path_mtu < IB_MTU_256 ||
  585. attr->path_mtu > active_mtu) {
  586. dev_err(dev, "attr path_mtu(%d)invalid while modify qp",
  587. attr->path_mtu);
  588. goto out;
  589. }
  590. }
  591. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  592. attr->max_rd_atomic > hr_dev->caps.max_qp_init_rdma) {
  593. dev_err(dev, "attr max_rd_atomic invalid.attr->max_rd_atomic=%d\n",
  594. attr->max_rd_atomic);
  595. goto out;
  596. }
  597. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  598. attr->max_dest_rd_atomic > hr_dev->caps.max_qp_dest_rdma) {
  599. dev_err(dev, "attr max_dest_rd_atomic invalid.attr->max_dest_rd_atomic=%d\n",
  600. attr->max_dest_rd_atomic);
  601. goto out;
  602. }
  603. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  604. ret = -EPERM;
  605. dev_err(dev, "cur_state=%d new_state=%d\n", cur_state,
  606. new_state);
  607. goto out;
  608. }
  609. ret = hr_dev->hw->modify_qp(ibqp, attr, attr_mask, cur_state,
  610. new_state);
  611. out:
  612. mutex_unlock(&hr_qp->mutex);
  613. return ret;
  614. }
  615. void hns_roce_lock_cqs(struct hns_roce_cq *send_cq, struct hns_roce_cq *recv_cq)
  616. __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
  617. {
  618. if (send_cq == recv_cq) {
  619. spin_lock_irq(&send_cq->lock);
  620. __acquire(&recv_cq->lock);
  621. } else if (send_cq->cqn < recv_cq->cqn) {
  622. spin_lock_irq(&send_cq->lock);
  623. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  624. } else {
  625. spin_lock_irq(&recv_cq->lock);
  626. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  627. }
  628. }
  629. void hns_roce_unlock_cqs(struct hns_roce_cq *send_cq,
  630. struct hns_roce_cq *recv_cq) __releases(&send_cq->lock)
  631. __releases(&recv_cq->lock)
  632. {
  633. if (send_cq == recv_cq) {
  634. __release(&recv_cq->lock);
  635. spin_unlock_irq(&send_cq->lock);
  636. } else if (send_cq->cqn < recv_cq->cqn) {
  637. spin_unlock(&recv_cq->lock);
  638. spin_unlock_irq(&send_cq->lock);
  639. } else {
  640. spin_unlock(&send_cq->lock);
  641. spin_unlock_irq(&recv_cq->lock);
  642. }
  643. }
  644. __be32 send_ieth(struct ib_send_wr *wr)
  645. {
  646. switch (wr->opcode) {
  647. case IB_WR_SEND_WITH_IMM:
  648. case IB_WR_RDMA_WRITE_WITH_IMM:
  649. return cpu_to_le32(wr->ex.imm_data);
  650. case IB_WR_SEND_WITH_INV:
  651. return cpu_to_le32(wr->ex.invalidate_rkey);
  652. default:
  653. return 0;
  654. }
  655. }
  656. static void *get_wqe(struct hns_roce_qp *hr_qp, int offset)
  657. {
  658. return hns_roce_buf_offset(&hr_qp->hr_buf, offset);
  659. }
  660. void *get_recv_wqe(struct hns_roce_qp *hr_qp, int n)
  661. {
  662. return get_wqe(hr_qp, hr_qp->rq.offset + (n << hr_qp->rq.wqe_shift));
  663. }
  664. void *get_send_wqe(struct hns_roce_qp *hr_qp, int n)
  665. {
  666. return get_wqe(hr_qp, hr_qp->sq.offset + (n << hr_qp->sq.wqe_shift));
  667. }
  668. bool hns_roce_wq_overflow(struct hns_roce_wq *hr_wq, int nreq,
  669. struct ib_cq *ib_cq)
  670. {
  671. struct hns_roce_cq *hr_cq;
  672. u32 cur;
  673. cur = hr_wq->head - hr_wq->tail;
  674. if (likely(cur + nreq < hr_wq->max_post))
  675. return 0;
  676. hr_cq = to_hr_cq(ib_cq);
  677. spin_lock(&hr_cq->lock);
  678. cur = hr_wq->head - hr_wq->tail;
  679. spin_unlock(&hr_cq->lock);
  680. return cur + nreq >= hr_wq->max_post;
  681. }
  682. int hns_roce_init_qp_table(struct hns_roce_dev *hr_dev)
  683. {
  684. struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
  685. int reserved_from_top = 0;
  686. int ret;
  687. spin_lock_init(&qp_table->lock);
  688. INIT_RADIX_TREE(&hr_dev->qp_table_tree, GFP_ATOMIC);
  689. /* A port include two SQP, six port total 12 */
  690. ret = hns_roce_bitmap_init(&qp_table->bitmap, hr_dev->caps.num_qps,
  691. hr_dev->caps.num_qps - 1, SQP_NUM,
  692. reserved_from_top);
  693. if (ret) {
  694. dev_err(&hr_dev->pdev->dev, "qp bitmap init failed!error=%d\n",
  695. ret);
  696. return ret;
  697. }
  698. return 0;
  699. }
  700. void hns_roce_cleanup_qp_table(struct hns_roce_dev *hr_dev)
  701. {
  702. hns_roce_bitmap_cleanup(&hr_dev->qp_table.bitmap);
  703. }