qib_qp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. /*
  42. * mask field which was present in now deleted qib_qpn_table
  43. * is not present in rvt_qpn_table. Defining the same field
  44. * as qpt_mask here instead of adding the mask field to
  45. * rvt_qpn_table.
  46. */
  47. u16 qpt_mask;
  48. static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
  49. struct rvt_qpn_map *map, unsigned off)
  50. {
  51. return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
  52. }
  53. static inline unsigned find_next_offset(struct rvt_qpn_table *qpt,
  54. struct rvt_qpn_map *map, unsigned off,
  55. unsigned n)
  56. {
  57. if (qpt_mask) {
  58. off++;
  59. if (((off & qpt_mask) >> 1) >= n)
  60. off = (off | qpt_mask) + 2;
  61. } else {
  62. off = find_next_zero_bit(map->page, RVT_BITS_PER_PAGE, off);
  63. }
  64. return off;
  65. }
  66. /*
  67. * Convert the AETH credit code into the number of credits.
  68. */
  69. static u32 credit_table[31] = {
  70. 0, /* 0 */
  71. 1, /* 1 */
  72. 2, /* 2 */
  73. 3, /* 3 */
  74. 4, /* 4 */
  75. 6, /* 5 */
  76. 8, /* 6 */
  77. 12, /* 7 */
  78. 16, /* 8 */
  79. 24, /* 9 */
  80. 32, /* A */
  81. 48, /* B */
  82. 64, /* C */
  83. 96, /* D */
  84. 128, /* E */
  85. 192, /* F */
  86. 256, /* 10 */
  87. 384, /* 11 */
  88. 512, /* 12 */
  89. 768, /* 13 */
  90. 1024, /* 14 */
  91. 1536, /* 15 */
  92. 2048, /* 16 */
  93. 3072, /* 17 */
  94. 4096, /* 18 */
  95. 6144, /* 19 */
  96. 8192, /* 1A */
  97. 12288, /* 1B */
  98. 16384, /* 1C */
  99. 24576, /* 1D */
  100. 32768 /* 1E */
  101. };
  102. static void get_map_page(struct rvt_qpn_table *qpt, struct rvt_qpn_map *map,
  103. gfp_t gfp)
  104. {
  105. unsigned long page = get_zeroed_page(gfp);
  106. /*
  107. * Free the page if someone raced with us installing it.
  108. */
  109. spin_lock(&qpt->lock);
  110. if (map->page)
  111. free_page(page);
  112. else
  113. map->page = (void *)page;
  114. spin_unlock(&qpt->lock);
  115. }
  116. /*
  117. * Allocate the next available QPN or
  118. * zero/one for QP type IB_QPT_SMI/IB_QPT_GSI.
  119. */
  120. int qib_alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
  121. enum ib_qp_type type, u8 port, gfp_t gfp)
  122. {
  123. u32 i, offset, max_scan, qpn;
  124. struct rvt_qpn_map *map;
  125. u32 ret;
  126. struct qib_ibdev *verbs_dev = container_of(rdi, struct qib_ibdev, rdi);
  127. struct qib_devdata *dd = container_of(verbs_dev, struct qib_devdata,
  128. verbs_dev);
  129. if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
  130. unsigned n;
  131. ret = type == IB_QPT_GSI;
  132. n = 1 << (ret + 2 * (port - 1));
  133. spin_lock(&qpt->lock);
  134. if (qpt->flags & n)
  135. ret = -EINVAL;
  136. else
  137. qpt->flags |= n;
  138. spin_unlock(&qpt->lock);
  139. goto bail;
  140. }
  141. qpn = qpt->last + 2;
  142. if (qpn >= RVT_QPN_MAX)
  143. qpn = 2;
  144. if (qpt_mask && ((qpn & qpt_mask) >> 1) >= dd->n_krcv_queues)
  145. qpn = (qpn | qpt_mask) + 2;
  146. offset = qpn & RVT_BITS_PER_PAGE_MASK;
  147. map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
  148. max_scan = qpt->nmaps - !offset;
  149. for (i = 0;;) {
  150. if (unlikely(!map->page)) {
  151. get_map_page(qpt, map, gfp);
  152. if (unlikely(!map->page))
  153. break;
  154. }
  155. do {
  156. if (!test_and_set_bit(offset, map->page)) {
  157. qpt->last = qpn;
  158. ret = qpn;
  159. goto bail;
  160. }
  161. offset = find_next_offset(qpt, map, offset,
  162. dd->n_krcv_queues);
  163. qpn = mk_qpn(qpt, map, offset);
  164. /*
  165. * This test differs from alloc_pidmap().
  166. * If find_next_offset() does find a zero
  167. * bit, we don't need to check for QPN
  168. * wrapping around past our starting QPN.
  169. * We just need to be sure we don't loop
  170. * forever.
  171. */
  172. } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
  173. /*
  174. * In order to keep the number of pages allocated to a
  175. * minimum, we scan the all existing pages before increasing
  176. * the size of the bitmap table.
  177. */
  178. if (++i > max_scan) {
  179. if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
  180. break;
  181. map = &qpt->map[qpt->nmaps++];
  182. offset = 0;
  183. } else if (map < &qpt->map[qpt->nmaps]) {
  184. ++map;
  185. offset = 0;
  186. } else {
  187. map = &qpt->map[0];
  188. offset = 2;
  189. }
  190. qpn = mk_qpn(qpt, map, offset);
  191. }
  192. ret = -ENOMEM;
  193. bail:
  194. return ret;
  195. }
  196. /**
  197. * qib_free_all_qps - check for QPs still in use
  198. */
  199. unsigned qib_free_all_qps(struct rvt_dev_info *rdi)
  200. {
  201. struct qib_ibdev *verbs_dev = container_of(rdi, struct qib_ibdev, rdi);
  202. struct qib_devdata *dd = container_of(verbs_dev, struct qib_devdata,
  203. verbs_dev);
  204. unsigned n, qp_inuse = 0;
  205. for (n = 0; n < dd->num_pports; n++) {
  206. struct qib_ibport *ibp = &dd->pport[n].ibport_data;
  207. rcu_read_lock();
  208. if (rcu_dereference(ibp->rvp.qp[0]))
  209. qp_inuse++;
  210. if (rcu_dereference(ibp->rvp.qp[1]))
  211. qp_inuse++;
  212. rcu_read_unlock();
  213. }
  214. return qp_inuse;
  215. }
  216. void qib_notify_qp_reset(struct rvt_qp *qp)
  217. {
  218. struct qib_qp_priv *priv = qp->priv;
  219. atomic_set(&priv->s_dma_busy, 0);
  220. }
  221. void qib_notify_error_qp(struct rvt_qp *qp)
  222. {
  223. struct qib_qp_priv *priv = qp->priv;
  224. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  225. spin_lock(&dev->rdi.pending_lock);
  226. if (!list_empty(&priv->iowait) && !(qp->s_flags & RVT_S_BUSY)) {
  227. qp->s_flags &= ~RVT_S_ANY_WAIT_IO;
  228. list_del_init(&priv->iowait);
  229. }
  230. spin_unlock(&dev->rdi.pending_lock);
  231. if (!(qp->s_flags & RVT_S_BUSY)) {
  232. qp->s_hdrwords = 0;
  233. if (qp->s_rdma_mr) {
  234. rvt_put_mr(qp->s_rdma_mr);
  235. qp->s_rdma_mr = NULL;
  236. }
  237. if (priv->s_tx) {
  238. qib_put_txreq(priv->s_tx);
  239. priv->s_tx = NULL;
  240. }
  241. }
  242. }
  243. static int mtu_to_enum(u32 mtu)
  244. {
  245. int enum_mtu;
  246. switch (mtu) {
  247. case 4096:
  248. enum_mtu = IB_MTU_4096;
  249. break;
  250. case 2048:
  251. enum_mtu = IB_MTU_2048;
  252. break;
  253. case 1024:
  254. enum_mtu = IB_MTU_1024;
  255. break;
  256. case 512:
  257. enum_mtu = IB_MTU_512;
  258. break;
  259. case 256:
  260. enum_mtu = IB_MTU_256;
  261. break;
  262. default:
  263. enum_mtu = IB_MTU_2048;
  264. }
  265. return enum_mtu;
  266. }
  267. int qib_get_pmtu_from_attr(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  268. struct ib_qp_attr *attr)
  269. {
  270. int mtu, pmtu, pidx = qp->port_num - 1;
  271. struct qib_ibdev *verbs_dev = container_of(rdi, struct qib_ibdev, rdi);
  272. struct qib_devdata *dd = container_of(verbs_dev, struct qib_devdata,
  273. verbs_dev);
  274. mtu = ib_mtu_enum_to_int(attr->path_mtu);
  275. if (mtu == -1)
  276. return -EINVAL;
  277. if (mtu > dd->pport[pidx].ibmtu)
  278. pmtu = mtu_to_enum(dd->pport[pidx].ibmtu);
  279. else
  280. pmtu = attr->path_mtu;
  281. return pmtu;
  282. }
  283. int qib_mtu_to_path_mtu(u32 mtu)
  284. {
  285. return mtu_to_enum(mtu);
  286. }
  287. u32 qib_mtu_from_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, u32 pmtu)
  288. {
  289. return ib_mtu_enum_to_int(pmtu);
  290. }
  291. /**
  292. * qib_compute_aeth - compute the AETH (syndrome + MSN)
  293. * @qp: the queue pair to compute the AETH for
  294. *
  295. * Returns the AETH.
  296. */
  297. __be32 qib_compute_aeth(struct rvt_qp *qp)
  298. {
  299. u32 aeth = qp->r_msn & QIB_MSN_MASK;
  300. if (qp->ibqp.srq) {
  301. /*
  302. * Shared receive queues don't generate credits.
  303. * Set the credit field to the invalid value.
  304. */
  305. aeth |= QIB_AETH_CREDIT_INVAL << QIB_AETH_CREDIT_SHIFT;
  306. } else {
  307. u32 min, max, x;
  308. u32 credits;
  309. struct rvt_rwq *wq = qp->r_rq.wq;
  310. u32 head;
  311. u32 tail;
  312. /* sanity check pointers before trusting them */
  313. head = wq->head;
  314. if (head >= qp->r_rq.size)
  315. head = 0;
  316. tail = wq->tail;
  317. if (tail >= qp->r_rq.size)
  318. tail = 0;
  319. /*
  320. * Compute the number of credits available (RWQEs).
  321. * XXX Not holding the r_rq.lock here so there is a small
  322. * chance that the pair of reads are not atomic.
  323. */
  324. credits = head - tail;
  325. if ((int)credits < 0)
  326. credits += qp->r_rq.size;
  327. /*
  328. * Binary search the credit table to find the code to
  329. * use.
  330. */
  331. min = 0;
  332. max = 31;
  333. for (;;) {
  334. x = (min + max) / 2;
  335. if (credit_table[x] == credits)
  336. break;
  337. if (credit_table[x] > credits)
  338. max = x;
  339. else if (min == x)
  340. break;
  341. else
  342. min = x;
  343. }
  344. aeth |= x << QIB_AETH_CREDIT_SHIFT;
  345. }
  346. return cpu_to_be32(aeth);
  347. }
  348. void *qib_qp_priv_alloc(struct rvt_dev_info *rdi, struct rvt_qp *qp, gfp_t gfp)
  349. {
  350. struct qib_qp_priv *priv;
  351. priv = kzalloc(sizeof(*priv), gfp);
  352. if (!priv)
  353. return ERR_PTR(-ENOMEM);
  354. priv->owner = qp;
  355. priv->s_hdr = kzalloc(sizeof(*priv->s_hdr), gfp);
  356. if (!priv->s_hdr) {
  357. kfree(priv);
  358. return ERR_PTR(-ENOMEM);
  359. }
  360. init_waitqueue_head(&priv->wait_dma);
  361. INIT_WORK(&priv->s_work, _qib_do_send);
  362. INIT_LIST_HEAD(&priv->iowait);
  363. return priv;
  364. }
  365. void qib_qp_priv_free(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  366. {
  367. struct qib_qp_priv *priv = qp->priv;
  368. kfree(priv->s_hdr);
  369. kfree(priv);
  370. }
  371. void qib_stop_send_queue(struct rvt_qp *qp)
  372. {
  373. struct qib_qp_priv *priv = qp->priv;
  374. cancel_work_sync(&priv->s_work);
  375. del_timer_sync(&qp->s_timer);
  376. }
  377. void qib_quiesce_qp(struct rvt_qp *qp)
  378. {
  379. struct qib_qp_priv *priv = qp->priv;
  380. wait_event(priv->wait_dma, !atomic_read(&priv->s_dma_busy));
  381. if (priv->s_tx) {
  382. qib_put_txreq(priv->s_tx);
  383. priv->s_tx = NULL;
  384. }
  385. }
  386. void qib_flush_qp_waiters(struct rvt_qp *qp)
  387. {
  388. struct qib_qp_priv *priv = qp->priv;
  389. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  390. spin_lock(&dev->rdi.pending_lock);
  391. if (!list_empty(&priv->iowait))
  392. list_del_init(&priv->iowait);
  393. spin_unlock(&dev->rdi.pending_lock);
  394. }
  395. /**
  396. * qib_get_credit - flush the send work queue of a QP
  397. * @qp: the qp who's send work queue to flush
  398. * @aeth: the Acknowledge Extended Transport Header
  399. *
  400. * The QP s_lock should be held.
  401. */
  402. void qib_get_credit(struct rvt_qp *qp, u32 aeth)
  403. {
  404. u32 credit = (aeth >> QIB_AETH_CREDIT_SHIFT) & QIB_AETH_CREDIT_MASK;
  405. /*
  406. * If the credit is invalid, we can send
  407. * as many packets as we like. Otherwise, we have to
  408. * honor the credit field.
  409. */
  410. if (credit == QIB_AETH_CREDIT_INVAL) {
  411. if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) {
  412. qp->s_flags |= RVT_S_UNLIMITED_CREDIT;
  413. if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) {
  414. qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT;
  415. qib_schedule_send(qp);
  416. }
  417. }
  418. } else if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) {
  419. /* Compute new LSN (i.e., MSN + credit) */
  420. credit = (aeth + credit_table[credit]) & QIB_MSN_MASK;
  421. if (qib_cmp24(credit, qp->s_lsn) > 0) {
  422. qp->s_lsn = credit;
  423. if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) {
  424. qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT;
  425. qib_schedule_send(qp);
  426. }
  427. }
  428. }
  429. }
  430. /**
  431. * qib_check_send_wqe - validate wr/wqe
  432. * @qp - The qp
  433. * @wqe - The built wqe
  434. *
  435. * validate wr/wqe. This is called
  436. * prior to inserting the wqe into
  437. * the ring but after the wqe has been
  438. * setup.
  439. *
  440. * Returns 1 to force direct progress, 0 otherwise, -EINVAL on failure
  441. */
  442. int qib_check_send_wqe(struct rvt_qp *qp,
  443. struct rvt_swqe *wqe)
  444. {
  445. struct rvt_ah *ah;
  446. int ret = 0;
  447. switch (qp->ibqp.qp_type) {
  448. case IB_QPT_RC:
  449. case IB_QPT_UC:
  450. if (wqe->length > 0x80000000U)
  451. return -EINVAL;
  452. break;
  453. case IB_QPT_SMI:
  454. case IB_QPT_GSI:
  455. case IB_QPT_UD:
  456. ah = ibah_to_rvtah(wqe->ud_wr.ah);
  457. if (wqe->length > (1 << ah->log_pmtu))
  458. return -EINVAL;
  459. /* progress hint */
  460. ret = 1;
  461. break;
  462. default:
  463. break;
  464. }
  465. return ret;
  466. }
  467. #ifdef CONFIG_DEBUG_FS
  468. struct qib_qp_iter {
  469. struct qib_ibdev *dev;
  470. struct rvt_qp *qp;
  471. int n;
  472. };
  473. struct qib_qp_iter *qib_qp_iter_init(struct qib_ibdev *dev)
  474. {
  475. struct qib_qp_iter *iter;
  476. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  477. if (!iter)
  478. return NULL;
  479. iter->dev = dev;
  480. if (qib_qp_iter_next(iter)) {
  481. kfree(iter);
  482. return NULL;
  483. }
  484. return iter;
  485. }
  486. int qib_qp_iter_next(struct qib_qp_iter *iter)
  487. {
  488. struct qib_ibdev *dev = iter->dev;
  489. int n = iter->n;
  490. int ret = 1;
  491. struct rvt_qp *pqp = iter->qp;
  492. struct rvt_qp *qp;
  493. for (; n < dev->rdi.qp_dev->qp_table_size; n++) {
  494. if (pqp)
  495. qp = rcu_dereference(pqp->next);
  496. else
  497. qp = rcu_dereference(dev->rdi.qp_dev->qp_table[n]);
  498. pqp = qp;
  499. if (qp) {
  500. iter->qp = qp;
  501. iter->n = n;
  502. return 0;
  503. }
  504. }
  505. return ret;
  506. }
  507. static const char * const qp_type_str[] = {
  508. "SMI", "GSI", "RC", "UC", "UD",
  509. };
  510. void qib_qp_iter_print(struct seq_file *s, struct qib_qp_iter *iter)
  511. {
  512. struct rvt_swqe *wqe;
  513. struct rvt_qp *qp = iter->qp;
  514. struct qib_qp_priv *priv = qp->priv;
  515. wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  516. seq_printf(s,
  517. "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",
  518. iter->n,
  519. qp->ibqp.qp_num,
  520. qp_type_str[qp->ibqp.qp_type],
  521. qp->state,
  522. wqe->wr.opcode,
  523. qp->s_hdrwords,
  524. qp->s_flags,
  525. atomic_read(&priv->s_dma_busy),
  526. !list_empty(&priv->iowait),
  527. qp->timeout,
  528. wqe->ssn,
  529. qp->s_lsn,
  530. qp->s_last_psn,
  531. qp->s_psn, qp->s_next_psn,
  532. qp->s_sending_psn, qp->s_sending_hpsn,
  533. qp->s_last, qp->s_acked, qp->s_cur,
  534. qp->s_tail, qp->s_head, qp->s_size,
  535. qp->remote_qpn,
  536. qp->remote_ah_attr.dlid);
  537. }
  538. #endif