qib_qp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (c) 2012 - 2017 Intel Corporation. All rights reserved.
  3. * Copyright (c) 2006 - 2012 QLogic Corporation. * All rights reserved.
  4. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/err.h>
  35. #include <linux/vmalloc.h>
  36. #include <rdma/rdma_vt.h>
  37. #ifdef CONFIG_DEBUG_FS
  38. #include <linux/seq_file.h>
  39. #endif
  40. #include "qib.h"
  41. static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
  42. struct rvt_qpn_map *map, unsigned off)
  43. {
  44. return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
  45. }
  46. static inline unsigned find_next_offset(struct rvt_qpn_table *qpt,
  47. struct rvt_qpn_map *map, unsigned off,
  48. unsigned n, u16 qpt_mask)
  49. {
  50. if (qpt_mask) {
  51. off++;
  52. if (((off & qpt_mask) >> 1) >= n)
  53. off = (off | qpt_mask) + 2;
  54. } else {
  55. off = find_next_zero_bit(map->page, RVT_BITS_PER_PAGE, off);
  56. }
  57. return off;
  58. }
  59. const struct rvt_operation_params qib_post_parms[RVT_OPERATION_MAX] = {
  60. [IB_WR_RDMA_WRITE] = {
  61. .length = sizeof(struct ib_rdma_wr),
  62. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  63. },
  64. [IB_WR_RDMA_READ] = {
  65. .length = sizeof(struct ib_rdma_wr),
  66. .qpt_support = BIT(IB_QPT_RC),
  67. .flags = RVT_OPERATION_ATOMIC,
  68. },
  69. [IB_WR_ATOMIC_CMP_AND_SWP] = {
  70. .length = sizeof(struct ib_atomic_wr),
  71. .qpt_support = BIT(IB_QPT_RC),
  72. .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
  73. },
  74. [IB_WR_ATOMIC_FETCH_AND_ADD] = {
  75. .length = sizeof(struct ib_atomic_wr),
  76. .qpt_support = BIT(IB_QPT_RC),
  77. .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
  78. },
  79. [IB_WR_RDMA_WRITE_WITH_IMM] = {
  80. .length = sizeof(struct ib_rdma_wr),
  81. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  82. },
  83. [IB_WR_SEND] = {
  84. .length = sizeof(struct ib_send_wr),
  85. .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
  86. BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  87. },
  88. [IB_WR_SEND_WITH_IMM] = {
  89. .length = sizeof(struct ib_send_wr),
  90. .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
  91. BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  92. },
  93. };
  94. static void get_map_page(struct rvt_qpn_table *qpt, struct rvt_qpn_map *map)
  95. {
  96. unsigned long page = get_zeroed_page(GFP_KERNEL);
  97. /*
  98. * Free the page if someone raced with us installing it.
  99. */
  100. spin_lock(&qpt->lock);
  101. if (map->page)
  102. free_page(page);
  103. else
  104. map->page = (void *)page;
  105. spin_unlock(&qpt->lock);
  106. }
  107. /*
  108. * Allocate the next available QPN or
  109. * zero/one for QP type IB_QPT_SMI/IB_QPT_GSI.
  110. */
  111. int qib_alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
  112. enum ib_qp_type type, u8 port)
  113. {
  114. u32 i, offset, max_scan, qpn;
  115. struct rvt_qpn_map *map;
  116. u32 ret;
  117. struct qib_ibdev *verbs_dev = container_of(rdi, struct qib_ibdev, rdi);
  118. struct qib_devdata *dd = container_of(verbs_dev, struct qib_devdata,
  119. verbs_dev);
  120. u16 qpt_mask = dd->qpn_mask;
  121. if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
  122. unsigned n;
  123. ret = type == IB_QPT_GSI;
  124. n = 1 << (ret + 2 * (port - 1));
  125. spin_lock(&qpt->lock);
  126. if (qpt->flags & n)
  127. ret = -EINVAL;
  128. else
  129. qpt->flags |= n;
  130. spin_unlock(&qpt->lock);
  131. goto bail;
  132. }
  133. qpn = qpt->last + 2;
  134. if (qpn >= RVT_QPN_MAX)
  135. qpn = 2;
  136. if (qpt_mask && ((qpn & qpt_mask) >> 1) >= dd->n_krcv_queues)
  137. qpn = (qpn | qpt_mask) + 2;
  138. offset = qpn & RVT_BITS_PER_PAGE_MASK;
  139. map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
  140. max_scan = qpt->nmaps - !offset;
  141. for (i = 0;;) {
  142. if (unlikely(!map->page)) {
  143. get_map_page(qpt, map);
  144. if (unlikely(!map->page))
  145. break;
  146. }
  147. do {
  148. if (!test_and_set_bit(offset, map->page)) {
  149. qpt->last = qpn;
  150. ret = qpn;
  151. goto bail;
  152. }
  153. offset = find_next_offset(qpt, map, offset,
  154. dd->n_krcv_queues, qpt_mask);
  155. qpn = mk_qpn(qpt, map, offset);
  156. /*
  157. * This test differs from alloc_pidmap().
  158. * If find_next_offset() does find a zero
  159. * bit, we don't need to check for QPN
  160. * wrapping around past our starting QPN.
  161. * We just need to be sure we don't loop
  162. * forever.
  163. */
  164. } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
  165. /*
  166. * In order to keep the number of pages allocated to a
  167. * minimum, we scan the all existing pages before increasing
  168. * the size of the bitmap table.
  169. */
  170. if (++i > max_scan) {
  171. if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
  172. break;
  173. map = &qpt->map[qpt->nmaps++];
  174. offset = 0;
  175. } else if (map < &qpt->map[qpt->nmaps]) {
  176. ++map;
  177. offset = 0;
  178. } else {
  179. map = &qpt->map[0];
  180. offset = 2;
  181. }
  182. qpn = mk_qpn(qpt, map, offset);
  183. }
  184. ret = -ENOMEM;
  185. bail:
  186. return ret;
  187. }
  188. /**
  189. * qib_free_all_qps - check for QPs still in use
  190. */
  191. unsigned qib_free_all_qps(struct rvt_dev_info *rdi)
  192. {
  193. struct qib_ibdev *verbs_dev = container_of(rdi, struct qib_ibdev, rdi);
  194. struct qib_devdata *dd = container_of(verbs_dev, struct qib_devdata,
  195. verbs_dev);
  196. unsigned n, qp_inuse = 0;
  197. for (n = 0; n < dd->num_pports; n++) {
  198. struct qib_ibport *ibp = &dd->pport[n].ibport_data;
  199. rcu_read_lock();
  200. if (rcu_dereference(ibp->rvp.qp[0]))
  201. qp_inuse++;
  202. if (rcu_dereference(ibp->rvp.qp[1]))
  203. qp_inuse++;
  204. rcu_read_unlock();
  205. }
  206. return qp_inuse;
  207. }
  208. void qib_notify_qp_reset(struct rvt_qp *qp)
  209. {
  210. struct qib_qp_priv *priv = qp->priv;
  211. atomic_set(&priv->s_dma_busy, 0);
  212. }
  213. void qib_notify_error_qp(struct rvt_qp *qp)
  214. {
  215. struct qib_qp_priv *priv = qp->priv;
  216. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  217. spin_lock(&dev->rdi.pending_lock);
  218. if (!list_empty(&priv->iowait) && !(qp->s_flags & RVT_S_BUSY)) {
  219. qp->s_flags &= ~RVT_S_ANY_WAIT_IO;
  220. list_del_init(&priv->iowait);
  221. }
  222. spin_unlock(&dev->rdi.pending_lock);
  223. if (!(qp->s_flags & RVT_S_BUSY)) {
  224. qp->s_hdrwords = 0;
  225. if (qp->s_rdma_mr) {
  226. rvt_put_mr(qp->s_rdma_mr);
  227. qp->s_rdma_mr = NULL;
  228. }
  229. if (priv->s_tx) {
  230. qib_put_txreq(priv->s_tx);
  231. priv->s_tx = NULL;
  232. }
  233. }
  234. }
  235. static int mtu_to_enum(u32 mtu)
  236. {
  237. int enum_mtu;
  238. switch (mtu) {
  239. case 4096:
  240. enum_mtu = IB_MTU_4096;
  241. break;
  242. case 2048:
  243. enum_mtu = IB_MTU_2048;
  244. break;
  245. case 1024:
  246. enum_mtu = IB_MTU_1024;
  247. break;
  248. case 512:
  249. enum_mtu = IB_MTU_512;
  250. break;
  251. case 256:
  252. enum_mtu = IB_MTU_256;
  253. break;
  254. default:
  255. enum_mtu = IB_MTU_2048;
  256. }
  257. return enum_mtu;
  258. }
  259. int qib_get_pmtu_from_attr(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  260. struct ib_qp_attr *attr)
  261. {
  262. int mtu, pmtu, pidx = qp->port_num - 1;
  263. struct qib_ibdev *verbs_dev = container_of(rdi, struct qib_ibdev, rdi);
  264. struct qib_devdata *dd = container_of(verbs_dev, struct qib_devdata,
  265. verbs_dev);
  266. mtu = ib_mtu_enum_to_int(attr->path_mtu);
  267. if (mtu == -1)
  268. return -EINVAL;
  269. if (mtu > dd->pport[pidx].ibmtu)
  270. pmtu = mtu_to_enum(dd->pport[pidx].ibmtu);
  271. else
  272. pmtu = attr->path_mtu;
  273. return pmtu;
  274. }
  275. int qib_mtu_to_path_mtu(u32 mtu)
  276. {
  277. return mtu_to_enum(mtu);
  278. }
  279. u32 qib_mtu_from_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, u32 pmtu)
  280. {
  281. return ib_mtu_enum_to_int(pmtu);
  282. }
  283. void *qib_qp_priv_alloc(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  284. {
  285. struct qib_qp_priv *priv;
  286. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  287. if (!priv)
  288. return ERR_PTR(-ENOMEM);
  289. priv->owner = qp;
  290. priv->s_hdr = kzalloc(sizeof(*priv->s_hdr), GFP_KERNEL);
  291. if (!priv->s_hdr) {
  292. kfree(priv);
  293. return ERR_PTR(-ENOMEM);
  294. }
  295. init_waitqueue_head(&priv->wait_dma);
  296. INIT_WORK(&priv->s_work, _qib_do_send);
  297. INIT_LIST_HEAD(&priv->iowait);
  298. return priv;
  299. }
  300. void qib_qp_priv_free(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  301. {
  302. struct qib_qp_priv *priv = qp->priv;
  303. kfree(priv->s_hdr);
  304. kfree(priv);
  305. }
  306. void qib_stop_send_queue(struct rvt_qp *qp)
  307. {
  308. struct qib_qp_priv *priv = qp->priv;
  309. cancel_work_sync(&priv->s_work);
  310. }
  311. void qib_quiesce_qp(struct rvt_qp *qp)
  312. {
  313. struct qib_qp_priv *priv = qp->priv;
  314. wait_event(priv->wait_dma, !atomic_read(&priv->s_dma_busy));
  315. if (priv->s_tx) {
  316. qib_put_txreq(priv->s_tx);
  317. priv->s_tx = NULL;
  318. }
  319. }
  320. void qib_flush_qp_waiters(struct rvt_qp *qp)
  321. {
  322. struct qib_qp_priv *priv = qp->priv;
  323. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  324. spin_lock(&dev->rdi.pending_lock);
  325. if (!list_empty(&priv->iowait))
  326. list_del_init(&priv->iowait);
  327. spin_unlock(&dev->rdi.pending_lock);
  328. }
  329. /**
  330. * qib_check_send_wqe - validate wr/wqe
  331. * @qp - The qp
  332. * @wqe - The built wqe
  333. *
  334. * validate wr/wqe. This is called
  335. * prior to inserting the wqe into
  336. * the ring but after the wqe has been
  337. * setup.
  338. *
  339. * Returns 1 to force direct progress, 0 otherwise, -EINVAL on failure
  340. */
  341. int qib_check_send_wqe(struct rvt_qp *qp,
  342. struct rvt_swqe *wqe)
  343. {
  344. struct rvt_ah *ah;
  345. int ret = 0;
  346. switch (qp->ibqp.qp_type) {
  347. case IB_QPT_RC:
  348. case IB_QPT_UC:
  349. if (wqe->length > 0x80000000U)
  350. return -EINVAL;
  351. break;
  352. case IB_QPT_SMI:
  353. case IB_QPT_GSI:
  354. case IB_QPT_UD:
  355. ah = ibah_to_rvtah(wqe->ud_wr.ah);
  356. if (wqe->length > (1 << ah->log_pmtu))
  357. return -EINVAL;
  358. /* progress hint */
  359. ret = 1;
  360. break;
  361. default:
  362. break;
  363. }
  364. return ret;
  365. }
  366. #ifdef CONFIG_DEBUG_FS
  367. static const char * const qp_type_str[] = {
  368. "SMI", "GSI", "RC", "UC", "UD",
  369. };
  370. /**
  371. * qib_qp_iter_print - print information to seq_file
  372. * @s - the seq_file
  373. * @iter - the iterator
  374. */
  375. void qib_qp_iter_print(struct seq_file *s, struct rvt_qp_iter *iter)
  376. {
  377. struct rvt_swqe *wqe;
  378. struct rvt_qp *qp = iter->qp;
  379. struct qib_qp_priv *priv = qp->priv;
  380. wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  381. seq_printf(s,
  382. "N %d QP%u %s %u %u %u f=%x %u %u %u %u %u PSN %x %x %x %x %x (%u %u %u %u %u %u) QP%u LID %x\n",
  383. iter->n,
  384. qp->ibqp.qp_num,
  385. qp_type_str[qp->ibqp.qp_type],
  386. qp->state,
  387. wqe->wr.opcode,
  388. qp->s_hdrwords,
  389. qp->s_flags,
  390. atomic_read(&priv->s_dma_busy),
  391. !list_empty(&priv->iowait),
  392. qp->timeout,
  393. wqe->ssn,
  394. qp->s_lsn,
  395. qp->s_last_psn,
  396. qp->s_psn, qp->s_next_psn,
  397. qp->s_sending_psn, qp->s_sending_hpsn,
  398. qp->s_last, qp->s_acked, qp->s_cur,
  399. qp->s_tail, qp->s_head, qp->s_size,
  400. qp->remote_qpn,
  401. rdma_ah_get_dlid(&qp->remote_ah_attr));
  402. }
  403. #endif