qib_qp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Copyright (c) 2012, 2013 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. gfp_t gfp)
  96. {
  97. unsigned long page = get_zeroed_page(gfp);
  98. /*
  99. * Free the page if someone raced with us installing it.
  100. */
  101. spin_lock(&qpt->lock);
  102. if (map->page)
  103. free_page(page);
  104. else
  105. map->page = (void *)page;
  106. spin_unlock(&qpt->lock);
  107. }
  108. /*
  109. * Allocate the next available QPN or
  110. * zero/one for QP type IB_QPT_SMI/IB_QPT_GSI.
  111. */
  112. int qib_alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
  113. enum ib_qp_type type, u8 port, gfp_t gfp)
  114. {
  115. u32 i, offset, max_scan, qpn;
  116. struct rvt_qpn_map *map;
  117. u32 ret;
  118. struct qib_ibdev *verbs_dev = container_of(rdi, struct qib_ibdev, rdi);
  119. struct qib_devdata *dd = container_of(verbs_dev, struct qib_devdata,
  120. verbs_dev);
  121. u16 qpt_mask = dd->qpn_mask;
  122. if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
  123. unsigned n;
  124. ret = type == IB_QPT_GSI;
  125. n = 1 << (ret + 2 * (port - 1));
  126. spin_lock(&qpt->lock);
  127. if (qpt->flags & n)
  128. ret = -EINVAL;
  129. else
  130. qpt->flags |= n;
  131. spin_unlock(&qpt->lock);
  132. goto bail;
  133. }
  134. qpn = qpt->last + 2;
  135. if (qpn >= RVT_QPN_MAX)
  136. qpn = 2;
  137. if (qpt_mask && ((qpn & qpt_mask) >> 1) >= dd->n_krcv_queues)
  138. qpn = (qpn | qpt_mask) + 2;
  139. offset = qpn & RVT_BITS_PER_PAGE_MASK;
  140. map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
  141. max_scan = qpt->nmaps - !offset;
  142. for (i = 0;;) {
  143. if (unlikely(!map->page)) {
  144. get_map_page(qpt, map, gfp);
  145. if (unlikely(!map->page))
  146. break;
  147. }
  148. do {
  149. if (!test_and_set_bit(offset, map->page)) {
  150. qpt->last = qpn;
  151. ret = qpn;
  152. goto bail;
  153. }
  154. offset = find_next_offset(qpt, map, offset,
  155. dd->n_krcv_queues, qpt_mask);
  156. qpn = mk_qpn(qpt, map, offset);
  157. /*
  158. * This test differs from alloc_pidmap().
  159. * If find_next_offset() does find a zero
  160. * bit, we don't need to check for QPN
  161. * wrapping around past our starting QPN.
  162. * We just need to be sure we don't loop
  163. * forever.
  164. */
  165. } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
  166. /*
  167. * In order to keep the number of pages allocated to a
  168. * minimum, we scan the all existing pages before increasing
  169. * the size of the bitmap table.
  170. */
  171. if (++i > max_scan) {
  172. if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
  173. break;
  174. map = &qpt->map[qpt->nmaps++];
  175. offset = 0;
  176. } else if (map < &qpt->map[qpt->nmaps]) {
  177. ++map;
  178. offset = 0;
  179. } else {
  180. map = &qpt->map[0];
  181. offset = 2;
  182. }
  183. qpn = mk_qpn(qpt, map, offset);
  184. }
  185. ret = -ENOMEM;
  186. bail:
  187. return ret;
  188. }
  189. /**
  190. * qib_free_all_qps - check for QPs still in use
  191. */
  192. unsigned qib_free_all_qps(struct rvt_dev_info *rdi)
  193. {
  194. struct qib_ibdev *verbs_dev = container_of(rdi, struct qib_ibdev, rdi);
  195. struct qib_devdata *dd = container_of(verbs_dev, struct qib_devdata,
  196. verbs_dev);
  197. unsigned n, qp_inuse = 0;
  198. for (n = 0; n < dd->num_pports; n++) {
  199. struct qib_ibport *ibp = &dd->pport[n].ibport_data;
  200. rcu_read_lock();
  201. if (rcu_dereference(ibp->rvp.qp[0]))
  202. qp_inuse++;
  203. if (rcu_dereference(ibp->rvp.qp[1]))
  204. qp_inuse++;
  205. rcu_read_unlock();
  206. }
  207. return qp_inuse;
  208. }
  209. void qib_notify_qp_reset(struct rvt_qp *qp)
  210. {
  211. struct qib_qp_priv *priv = qp->priv;
  212. atomic_set(&priv->s_dma_busy, 0);
  213. }
  214. void qib_notify_error_qp(struct rvt_qp *qp)
  215. {
  216. struct qib_qp_priv *priv = qp->priv;
  217. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  218. spin_lock(&dev->rdi.pending_lock);
  219. if (!list_empty(&priv->iowait) && !(qp->s_flags & RVT_S_BUSY)) {
  220. qp->s_flags &= ~RVT_S_ANY_WAIT_IO;
  221. list_del_init(&priv->iowait);
  222. }
  223. spin_unlock(&dev->rdi.pending_lock);
  224. if (!(qp->s_flags & RVT_S_BUSY)) {
  225. qp->s_hdrwords = 0;
  226. if (qp->s_rdma_mr) {
  227. rvt_put_mr(qp->s_rdma_mr);
  228. qp->s_rdma_mr = NULL;
  229. }
  230. if (priv->s_tx) {
  231. qib_put_txreq(priv->s_tx);
  232. priv->s_tx = NULL;
  233. }
  234. }
  235. }
  236. static int mtu_to_enum(u32 mtu)
  237. {
  238. int enum_mtu;
  239. switch (mtu) {
  240. case 4096:
  241. enum_mtu = IB_MTU_4096;
  242. break;
  243. case 2048:
  244. enum_mtu = IB_MTU_2048;
  245. break;
  246. case 1024:
  247. enum_mtu = IB_MTU_1024;
  248. break;
  249. case 512:
  250. enum_mtu = IB_MTU_512;
  251. break;
  252. case 256:
  253. enum_mtu = IB_MTU_256;
  254. break;
  255. default:
  256. enum_mtu = IB_MTU_2048;
  257. }
  258. return enum_mtu;
  259. }
  260. int qib_get_pmtu_from_attr(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  261. struct ib_qp_attr *attr)
  262. {
  263. int mtu, pmtu, pidx = qp->port_num - 1;
  264. struct qib_ibdev *verbs_dev = container_of(rdi, struct qib_ibdev, rdi);
  265. struct qib_devdata *dd = container_of(verbs_dev, struct qib_devdata,
  266. verbs_dev);
  267. mtu = ib_mtu_enum_to_int(attr->path_mtu);
  268. if (mtu == -1)
  269. return -EINVAL;
  270. if (mtu > dd->pport[pidx].ibmtu)
  271. pmtu = mtu_to_enum(dd->pport[pidx].ibmtu);
  272. else
  273. pmtu = attr->path_mtu;
  274. return pmtu;
  275. }
  276. int qib_mtu_to_path_mtu(u32 mtu)
  277. {
  278. return mtu_to_enum(mtu);
  279. }
  280. u32 qib_mtu_from_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, u32 pmtu)
  281. {
  282. return ib_mtu_enum_to_int(pmtu);
  283. }
  284. void *qib_qp_priv_alloc(struct rvt_dev_info *rdi, struct rvt_qp *qp, gfp_t gfp)
  285. {
  286. struct qib_qp_priv *priv;
  287. priv = kzalloc(sizeof(*priv), gfp);
  288. if (!priv)
  289. return ERR_PTR(-ENOMEM);
  290. priv->owner = qp;
  291. priv->s_hdr = kzalloc(sizeof(*priv->s_hdr), gfp);
  292. if (!priv->s_hdr) {
  293. kfree(priv);
  294. return ERR_PTR(-ENOMEM);
  295. }
  296. init_waitqueue_head(&priv->wait_dma);
  297. INIT_WORK(&priv->s_work, _qib_do_send);
  298. INIT_LIST_HEAD(&priv->iowait);
  299. return priv;
  300. }
  301. void qib_qp_priv_free(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  302. {
  303. struct qib_qp_priv *priv = qp->priv;
  304. kfree(priv->s_hdr);
  305. kfree(priv);
  306. }
  307. void qib_stop_send_queue(struct rvt_qp *qp)
  308. {
  309. struct qib_qp_priv *priv = qp->priv;
  310. cancel_work_sync(&priv->s_work);
  311. }
  312. void qib_quiesce_qp(struct rvt_qp *qp)
  313. {
  314. struct qib_qp_priv *priv = qp->priv;
  315. wait_event(priv->wait_dma, !atomic_read(&priv->s_dma_busy));
  316. if (priv->s_tx) {
  317. qib_put_txreq(priv->s_tx);
  318. priv->s_tx = NULL;
  319. }
  320. }
  321. void qib_flush_qp_waiters(struct rvt_qp *qp)
  322. {
  323. struct qib_qp_priv *priv = qp->priv;
  324. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  325. spin_lock(&dev->rdi.pending_lock);
  326. if (!list_empty(&priv->iowait))
  327. list_del_init(&priv->iowait);
  328. spin_unlock(&dev->rdi.pending_lock);
  329. }
  330. /**
  331. * qib_check_send_wqe - validate wr/wqe
  332. * @qp - The qp
  333. * @wqe - The built wqe
  334. *
  335. * validate wr/wqe. This is called
  336. * prior to inserting the wqe into
  337. * the ring but after the wqe has been
  338. * setup.
  339. *
  340. * Returns 1 to force direct progress, 0 otherwise, -EINVAL on failure
  341. */
  342. int qib_check_send_wqe(struct rvt_qp *qp,
  343. struct rvt_swqe *wqe)
  344. {
  345. struct rvt_ah *ah;
  346. int ret = 0;
  347. switch (qp->ibqp.qp_type) {
  348. case IB_QPT_RC:
  349. case IB_QPT_UC:
  350. if (wqe->length > 0x80000000U)
  351. return -EINVAL;
  352. break;
  353. case IB_QPT_SMI:
  354. case IB_QPT_GSI:
  355. case IB_QPT_UD:
  356. ah = ibah_to_rvtah(wqe->ud_wr.ah);
  357. if (wqe->length > (1 << ah->log_pmtu))
  358. return -EINVAL;
  359. /* progress hint */
  360. ret = 1;
  361. break;
  362. default:
  363. break;
  364. }
  365. return ret;
  366. }
  367. #ifdef CONFIG_DEBUG_FS
  368. struct qib_qp_iter {
  369. struct qib_ibdev *dev;
  370. struct rvt_qp *qp;
  371. int n;
  372. };
  373. struct qib_qp_iter *qib_qp_iter_init(struct qib_ibdev *dev)
  374. {
  375. struct qib_qp_iter *iter;
  376. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  377. if (!iter)
  378. return NULL;
  379. iter->dev = dev;
  380. return iter;
  381. }
  382. int qib_qp_iter_next(struct qib_qp_iter *iter)
  383. {
  384. struct qib_ibdev *dev = iter->dev;
  385. int n = iter->n;
  386. int ret = 1;
  387. struct rvt_qp *pqp = iter->qp;
  388. struct rvt_qp *qp;
  389. for (; n < dev->rdi.qp_dev->qp_table_size; n++) {
  390. if (pqp)
  391. qp = rcu_dereference(pqp->next);
  392. else
  393. qp = rcu_dereference(dev->rdi.qp_dev->qp_table[n]);
  394. pqp = qp;
  395. if (qp) {
  396. iter->qp = qp;
  397. iter->n = n;
  398. return 0;
  399. }
  400. }
  401. return ret;
  402. }
  403. static const char * const qp_type_str[] = {
  404. "SMI", "GSI", "RC", "UC", "UD",
  405. };
  406. void qib_qp_iter_print(struct seq_file *s, struct qib_qp_iter *iter)
  407. {
  408. struct rvt_swqe *wqe;
  409. struct rvt_qp *qp = iter->qp;
  410. struct qib_qp_priv *priv = qp->priv;
  411. wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  412. seq_printf(s,
  413. "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",
  414. iter->n,
  415. qp->ibqp.qp_num,
  416. qp_type_str[qp->ibqp.qp_type],
  417. qp->state,
  418. wqe->wr.opcode,
  419. qp->s_hdrwords,
  420. qp->s_flags,
  421. atomic_read(&priv->s_dma_busy),
  422. !list_empty(&priv->iowait),
  423. qp->timeout,
  424. wqe->ssn,
  425. qp->s_lsn,
  426. qp->s_last_psn,
  427. qp->s_psn, qp->s_next_psn,
  428. qp->s_sending_psn, qp->s_sending_hpsn,
  429. qp->s_last, qp->s_acked, qp->s_cur,
  430. qp->s_tail, qp->s_head, qp->s_size,
  431. qp->remote_qpn,
  432. qp->remote_ah_attr.dlid);
  433. }
  434. #endif