qp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/err.h>
  48. #include <linux/vmalloc.h>
  49. #include <linux/hash.h>
  50. #include <linux/module.h>
  51. #include <linux/seq_file.h>
  52. #include <rdma/rdma_vt.h>
  53. #include <rdma/rdmavt_qp.h>
  54. #include <rdma/ib_verbs.h>
  55. #include "hfi.h"
  56. #include "qp.h"
  57. #include "trace.h"
  58. #include "verbs_txreq.h"
  59. unsigned int hfi1_qp_table_size = 256;
  60. module_param_named(qp_table_size, hfi1_qp_table_size, uint, S_IRUGO);
  61. MODULE_PARM_DESC(qp_table_size, "QP table size");
  62. static void flush_tx_list(struct rvt_qp *qp);
  63. static int iowait_sleep(
  64. struct sdma_engine *sde,
  65. struct iowait *wait,
  66. struct sdma_txreq *stx,
  67. unsigned seq);
  68. static void iowait_wakeup(struct iowait *wait, int reason);
  69. static void iowait_sdma_drained(struct iowait *wait);
  70. static void qp_pio_drain(struct rvt_qp *qp);
  71. static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
  72. struct rvt_qpn_map *map, unsigned off)
  73. {
  74. return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
  75. }
  76. const struct rvt_operation_params hfi1_post_parms[RVT_OPERATION_MAX] = {
  77. [IB_WR_RDMA_WRITE] = {
  78. .length = sizeof(struct ib_rdma_wr),
  79. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  80. },
  81. [IB_WR_RDMA_READ] = {
  82. .length = sizeof(struct ib_rdma_wr),
  83. .qpt_support = BIT(IB_QPT_RC),
  84. .flags = RVT_OPERATION_ATOMIC,
  85. },
  86. [IB_WR_ATOMIC_CMP_AND_SWP] = {
  87. .length = sizeof(struct ib_atomic_wr),
  88. .qpt_support = BIT(IB_QPT_RC),
  89. .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
  90. },
  91. [IB_WR_ATOMIC_FETCH_AND_ADD] = {
  92. .length = sizeof(struct ib_atomic_wr),
  93. .qpt_support = BIT(IB_QPT_RC),
  94. .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
  95. },
  96. [IB_WR_RDMA_WRITE_WITH_IMM] = {
  97. .length = sizeof(struct ib_rdma_wr),
  98. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  99. },
  100. [IB_WR_SEND] = {
  101. .length = sizeof(struct ib_send_wr),
  102. .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
  103. BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  104. },
  105. [IB_WR_SEND_WITH_IMM] = {
  106. .length = sizeof(struct ib_send_wr),
  107. .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
  108. BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  109. },
  110. [IB_WR_REG_MR] = {
  111. .length = sizeof(struct ib_reg_wr),
  112. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  113. .flags = RVT_OPERATION_LOCAL,
  114. },
  115. [IB_WR_LOCAL_INV] = {
  116. .length = sizeof(struct ib_send_wr),
  117. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  118. .flags = RVT_OPERATION_LOCAL,
  119. },
  120. [IB_WR_SEND_WITH_INV] = {
  121. .length = sizeof(struct ib_send_wr),
  122. .qpt_support = BIT(IB_QPT_RC),
  123. },
  124. };
  125. static void flush_tx_list(struct rvt_qp *qp)
  126. {
  127. struct hfi1_qp_priv *priv = qp->priv;
  128. while (!list_empty(&priv->s_iowait.tx_head)) {
  129. struct sdma_txreq *tx;
  130. tx = list_first_entry(
  131. &priv->s_iowait.tx_head,
  132. struct sdma_txreq,
  133. list);
  134. list_del_init(&tx->list);
  135. hfi1_put_txreq(
  136. container_of(tx, struct verbs_txreq, txreq));
  137. }
  138. }
  139. static void flush_iowait(struct rvt_qp *qp)
  140. {
  141. struct hfi1_qp_priv *priv = qp->priv;
  142. unsigned long flags;
  143. seqlock_t *lock = priv->s_iowait.lock;
  144. if (!lock)
  145. return;
  146. write_seqlock_irqsave(lock, flags);
  147. if (!list_empty(&priv->s_iowait.list)) {
  148. list_del_init(&priv->s_iowait.list);
  149. priv->s_iowait.lock = NULL;
  150. rvt_put_qp(qp);
  151. }
  152. write_sequnlock_irqrestore(lock, flags);
  153. }
  154. static inline int opa_mtu_enum_to_int(int mtu)
  155. {
  156. switch (mtu) {
  157. case OPA_MTU_8192: return 8192;
  158. case OPA_MTU_10240: return 10240;
  159. default: return -1;
  160. }
  161. }
  162. /**
  163. * This function is what we would push to the core layer if we wanted to be a
  164. * "first class citizen". Instead we hide this here and rely on Verbs ULPs
  165. * to blindly pass the MTU enum value from the PathRecord to us.
  166. */
  167. static inline int verbs_mtu_enum_to_int(struct ib_device *dev, enum ib_mtu mtu)
  168. {
  169. int val;
  170. /* Constraining 10KB packets to 8KB packets */
  171. if (mtu == (enum ib_mtu)OPA_MTU_10240)
  172. mtu = OPA_MTU_8192;
  173. val = opa_mtu_enum_to_int((int)mtu);
  174. if (val > 0)
  175. return val;
  176. return ib_mtu_enum_to_int(mtu);
  177. }
  178. int hfi1_check_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr,
  179. int attr_mask, struct ib_udata *udata)
  180. {
  181. struct ib_qp *ibqp = &qp->ibqp;
  182. struct hfi1_ibdev *dev = to_idev(ibqp->device);
  183. struct hfi1_devdata *dd = dd_from_dev(dev);
  184. u8 sc;
  185. if (attr_mask & IB_QP_AV) {
  186. sc = ah_to_sc(ibqp->device, &attr->ah_attr);
  187. if (sc == 0xf)
  188. return -EINVAL;
  189. if (!qp_to_sdma_engine(qp, sc) &&
  190. dd->flags & HFI1_HAS_SEND_DMA)
  191. return -EINVAL;
  192. if (!qp_to_send_context(qp, sc))
  193. return -EINVAL;
  194. }
  195. if (attr_mask & IB_QP_ALT_PATH) {
  196. sc = ah_to_sc(ibqp->device, &attr->alt_ah_attr);
  197. if (sc == 0xf)
  198. return -EINVAL;
  199. if (!qp_to_sdma_engine(qp, sc) &&
  200. dd->flags & HFI1_HAS_SEND_DMA)
  201. return -EINVAL;
  202. if (!qp_to_send_context(qp, sc))
  203. return -EINVAL;
  204. }
  205. return 0;
  206. }
  207. void hfi1_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr,
  208. int attr_mask, struct ib_udata *udata)
  209. {
  210. struct ib_qp *ibqp = &qp->ibqp;
  211. struct hfi1_qp_priv *priv = qp->priv;
  212. if (attr_mask & IB_QP_AV) {
  213. priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr);
  214. priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
  215. priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
  216. }
  217. if (attr_mask & IB_QP_PATH_MIG_STATE &&
  218. attr->path_mig_state == IB_MIG_MIGRATED &&
  219. qp->s_mig_state == IB_MIG_ARMED) {
  220. qp->s_flags |= RVT_S_AHG_CLEAR;
  221. priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr);
  222. priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
  223. priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
  224. }
  225. }
  226. /**
  227. * hfi1_check_send_wqe - validate wqe
  228. * @qp - The qp
  229. * @wqe - The built wqe
  230. *
  231. * validate wqe. This is called
  232. * prior to inserting the wqe into
  233. * the ring but after the wqe has been
  234. * setup.
  235. *
  236. * Returns 0 on success, -EINVAL on failure
  237. *
  238. */
  239. int hfi1_check_send_wqe(struct rvt_qp *qp,
  240. struct rvt_swqe *wqe)
  241. {
  242. struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  243. struct rvt_ah *ah;
  244. switch (qp->ibqp.qp_type) {
  245. case IB_QPT_RC:
  246. case IB_QPT_UC:
  247. if (wqe->length > 0x80000000U)
  248. return -EINVAL;
  249. break;
  250. case IB_QPT_SMI:
  251. ah = ibah_to_rvtah(wqe->ud_wr.ah);
  252. if (wqe->length > (1 << ah->log_pmtu))
  253. return -EINVAL;
  254. break;
  255. case IB_QPT_GSI:
  256. case IB_QPT_UD:
  257. ah = ibah_to_rvtah(wqe->ud_wr.ah);
  258. if (wqe->length > (1 << ah->log_pmtu))
  259. return -EINVAL;
  260. if (ibp->sl_to_sc[ah->attr.sl] == 0xf)
  261. return -EINVAL;
  262. default:
  263. break;
  264. }
  265. return wqe->length <= piothreshold;
  266. }
  267. /**
  268. * _hfi1_schedule_send - schedule progress
  269. * @qp: the QP
  270. *
  271. * This schedules qp progress w/o regard to the s_flags.
  272. *
  273. * It is only used in the post send, which doesn't hold
  274. * the s_lock.
  275. */
  276. void _hfi1_schedule_send(struct rvt_qp *qp)
  277. {
  278. struct hfi1_qp_priv *priv = qp->priv;
  279. struct hfi1_ibport *ibp =
  280. to_iport(qp->ibqp.device, qp->port_num);
  281. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  282. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  283. iowait_schedule(&priv->s_iowait, ppd->hfi1_wq,
  284. priv->s_sde ?
  285. priv->s_sde->cpu :
  286. cpumask_first(cpumask_of_node(dd->node)));
  287. }
  288. static void qp_pio_drain(struct rvt_qp *qp)
  289. {
  290. struct hfi1_ibdev *dev;
  291. struct hfi1_qp_priv *priv = qp->priv;
  292. if (!priv->s_sendcontext)
  293. return;
  294. dev = to_idev(qp->ibqp.device);
  295. while (iowait_pio_pending(&priv->s_iowait)) {
  296. write_seqlock_irq(&dev->iowait_lock);
  297. hfi1_sc_wantpiobuf_intr(priv->s_sendcontext, 1);
  298. write_sequnlock_irq(&dev->iowait_lock);
  299. iowait_pio_drain(&priv->s_iowait);
  300. write_seqlock_irq(&dev->iowait_lock);
  301. hfi1_sc_wantpiobuf_intr(priv->s_sendcontext, 0);
  302. write_sequnlock_irq(&dev->iowait_lock);
  303. }
  304. }
  305. /**
  306. * hfi1_schedule_send - schedule progress
  307. * @qp: the QP
  308. *
  309. * This schedules qp progress and caller should hold
  310. * the s_lock.
  311. */
  312. void hfi1_schedule_send(struct rvt_qp *qp)
  313. {
  314. lockdep_assert_held(&qp->s_lock);
  315. if (hfi1_send_ok(qp))
  316. _hfi1_schedule_send(qp);
  317. }
  318. void hfi1_qp_wakeup(struct rvt_qp *qp, u32 flag)
  319. {
  320. unsigned long flags;
  321. spin_lock_irqsave(&qp->s_lock, flags);
  322. if (qp->s_flags & flag) {
  323. qp->s_flags &= ~flag;
  324. trace_hfi1_qpwakeup(qp, flag);
  325. hfi1_schedule_send(qp);
  326. }
  327. spin_unlock_irqrestore(&qp->s_lock, flags);
  328. /* Notify hfi1_destroy_qp() if it is waiting. */
  329. rvt_put_qp(qp);
  330. }
  331. static int iowait_sleep(
  332. struct sdma_engine *sde,
  333. struct iowait *wait,
  334. struct sdma_txreq *stx,
  335. unsigned seq)
  336. {
  337. struct verbs_txreq *tx = container_of(stx, struct verbs_txreq, txreq);
  338. struct rvt_qp *qp;
  339. struct hfi1_qp_priv *priv;
  340. unsigned long flags;
  341. int ret = 0;
  342. struct hfi1_ibdev *dev;
  343. qp = tx->qp;
  344. priv = qp->priv;
  345. spin_lock_irqsave(&qp->s_lock, flags);
  346. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  347. /*
  348. * If we couldn't queue the DMA request, save the info
  349. * and try again later rather than destroying the
  350. * buffer and undoing the side effects of the copy.
  351. */
  352. /* Make a common routine? */
  353. dev = &sde->dd->verbs_dev;
  354. list_add_tail(&stx->list, &wait->tx_head);
  355. write_seqlock(&dev->iowait_lock);
  356. if (sdma_progress(sde, seq, stx))
  357. goto eagain;
  358. if (list_empty(&priv->s_iowait.list)) {
  359. struct hfi1_ibport *ibp =
  360. to_iport(qp->ibqp.device, qp->port_num);
  361. ibp->rvp.n_dmawait++;
  362. qp->s_flags |= RVT_S_WAIT_DMA_DESC;
  363. list_add_tail(&priv->s_iowait.list, &sde->dmawait);
  364. priv->s_iowait.lock = &dev->iowait_lock;
  365. trace_hfi1_qpsleep(qp, RVT_S_WAIT_DMA_DESC);
  366. rvt_get_qp(qp);
  367. }
  368. write_sequnlock(&dev->iowait_lock);
  369. qp->s_flags &= ~RVT_S_BUSY;
  370. spin_unlock_irqrestore(&qp->s_lock, flags);
  371. ret = -EBUSY;
  372. } else {
  373. spin_unlock_irqrestore(&qp->s_lock, flags);
  374. hfi1_put_txreq(tx);
  375. }
  376. return ret;
  377. eagain:
  378. write_sequnlock(&dev->iowait_lock);
  379. spin_unlock_irqrestore(&qp->s_lock, flags);
  380. list_del_init(&stx->list);
  381. return -EAGAIN;
  382. }
  383. static void iowait_wakeup(struct iowait *wait, int reason)
  384. {
  385. struct rvt_qp *qp = iowait_to_qp(wait);
  386. WARN_ON(reason != SDMA_AVAIL_REASON);
  387. hfi1_qp_wakeup(qp, RVT_S_WAIT_DMA_DESC);
  388. }
  389. static void iowait_sdma_drained(struct iowait *wait)
  390. {
  391. struct rvt_qp *qp = iowait_to_qp(wait);
  392. unsigned long flags;
  393. /*
  394. * This happens when the send engine notes
  395. * a QP in the error state and cannot
  396. * do the flush work until that QP's
  397. * sdma work has finished.
  398. */
  399. spin_lock_irqsave(&qp->s_lock, flags);
  400. if (qp->s_flags & RVT_S_WAIT_DMA) {
  401. qp->s_flags &= ~RVT_S_WAIT_DMA;
  402. hfi1_schedule_send(qp);
  403. }
  404. spin_unlock_irqrestore(&qp->s_lock, flags);
  405. }
  406. /**
  407. *
  408. * qp_to_sdma_engine - map a qp to a send engine
  409. * @qp: the QP
  410. * @sc5: the 5 bit sc
  411. *
  412. * Return:
  413. * A send engine for the qp or NULL for SMI type qp.
  414. */
  415. struct sdma_engine *qp_to_sdma_engine(struct rvt_qp *qp, u8 sc5)
  416. {
  417. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  418. struct sdma_engine *sde;
  419. if (!(dd->flags & HFI1_HAS_SEND_DMA))
  420. return NULL;
  421. switch (qp->ibqp.qp_type) {
  422. case IB_QPT_SMI:
  423. return NULL;
  424. default:
  425. break;
  426. }
  427. sde = sdma_select_engine_sc(dd, qp->ibqp.qp_num >> dd->qos_shift, sc5);
  428. return sde;
  429. }
  430. /*
  431. * qp_to_send_context - map a qp to a send context
  432. * @qp: the QP
  433. * @sc5: the 5 bit sc
  434. *
  435. * Return:
  436. * A send context for the qp
  437. */
  438. struct send_context *qp_to_send_context(struct rvt_qp *qp, u8 sc5)
  439. {
  440. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  441. switch (qp->ibqp.qp_type) {
  442. case IB_QPT_SMI:
  443. /* SMA packets to VL15 */
  444. return dd->vld[15].sc;
  445. default:
  446. break;
  447. }
  448. return pio_select_send_context_sc(dd, qp->ibqp.qp_num >> dd->qos_shift,
  449. sc5);
  450. }
  451. struct qp_iter {
  452. struct hfi1_ibdev *dev;
  453. struct rvt_qp *qp;
  454. int specials;
  455. int n;
  456. };
  457. struct qp_iter *qp_iter_init(struct hfi1_ibdev *dev)
  458. {
  459. struct qp_iter *iter;
  460. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  461. if (!iter)
  462. return NULL;
  463. iter->dev = dev;
  464. iter->specials = dev->rdi.ibdev.phys_port_cnt * 2;
  465. return iter;
  466. }
  467. int qp_iter_next(struct qp_iter *iter)
  468. {
  469. struct hfi1_ibdev *dev = iter->dev;
  470. int n = iter->n;
  471. int ret = 1;
  472. struct rvt_qp *pqp = iter->qp;
  473. struct rvt_qp *qp;
  474. /*
  475. * The approach is to consider the special qps
  476. * as an additional table entries before the
  477. * real hash table. Since the qp code sets
  478. * the qp->next hash link to NULL, this works just fine.
  479. *
  480. * iter->specials is 2 * # ports
  481. *
  482. * n = 0..iter->specials is the special qp indices
  483. *
  484. * n = iter->specials..dev->rdi.qp_dev->qp_table_size+iter->specials are
  485. * the potential hash bucket entries
  486. *
  487. */
  488. for (; n < dev->rdi.qp_dev->qp_table_size + iter->specials; n++) {
  489. if (pqp) {
  490. qp = rcu_dereference(pqp->next);
  491. } else {
  492. if (n < iter->specials) {
  493. struct hfi1_pportdata *ppd;
  494. struct hfi1_ibport *ibp;
  495. int pidx;
  496. pidx = n % dev->rdi.ibdev.phys_port_cnt;
  497. ppd = &dd_from_dev(dev)->pport[pidx];
  498. ibp = &ppd->ibport_data;
  499. if (!(n & 1))
  500. qp = rcu_dereference(ibp->rvp.qp[0]);
  501. else
  502. qp = rcu_dereference(ibp->rvp.qp[1]);
  503. } else {
  504. qp = rcu_dereference(
  505. dev->rdi.qp_dev->qp_table[
  506. (n - iter->specials)]);
  507. }
  508. }
  509. pqp = qp;
  510. if (qp) {
  511. iter->qp = qp;
  512. iter->n = n;
  513. return 0;
  514. }
  515. }
  516. return ret;
  517. }
  518. static const char * const qp_type_str[] = {
  519. "SMI", "GSI", "RC", "UC", "UD",
  520. };
  521. static int qp_idle(struct rvt_qp *qp)
  522. {
  523. return
  524. qp->s_last == qp->s_acked &&
  525. qp->s_acked == qp->s_cur &&
  526. qp->s_cur == qp->s_tail &&
  527. qp->s_tail == qp->s_head;
  528. }
  529. void qp_iter_print(struct seq_file *s, struct qp_iter *iter)
  530. {
  531. struct rvt_swqe *wqe;
  532. struct rvt_qp *qp = iter->qp;
  533. struct hfi1_qp_priv *priv = qp->priv;
  534. struct sdma_engine *sde;
  535. struct send_context *send_context;
  536. sde = qp_to_sdma_engine(qp, priv->s_sc);
  537. wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  538. send_context = qp_to_send_context(qp, priv->s_sc);
  539. seq_printf(s,
  540. "N %d %s QP %x R %u %s %u %u %u f=%x %u %u %u %u %u %u SPSN %x %x %x %x %x RPSN %x (%u %u %u %u %u %u %u) RQP %x LID %x SL %u MTU %u %u %u %u %u SDE %p,%u SC %p,%u SCQ %u %u PID %d\n",
  541. iter->n,
  542. qp_idle(qp) ? "I" : "B",
  543. qp->ibqp.qp_num,
  544. atomic_read(&qp->refcount),
  545. qp_type_str[qp->ibqp.qp_type],
  546. qp->state,
  547. wqe ? wqe->wr.opcode : 0,
  548. qp->s_hdrwords,
  549. qp->s_flags,
  550. iowait_sdma_pending(&priv->s_iowait),
  551. iowait_pio_pending(&priv->s_iowait),
  552. !list_empty(&priv->s_iowait.list),
  553. qp->timeout,
  554. wqe ? wqe->ssn : 0,
  555. qp->s_lsn,
  556. qp->s_last_psn,
  557. qp->s_psn, qp->s_next_psn,
  558. qp->s_sending_psn, qp->s_sending_hpsn,
  559. qp->r_psn,
  560. qp->s_last, qp->s_acked, qp->s_cur,
  561. qp->s_tail, qp->s_head, qp->s_size,
  562. qp->s_avail,
  563. qp->remote_qpn,
  564. qp->remote_ah_attr.dlid,
  565. qp->remote_ah_attr.sl,
  566. qp->pmtu,
  567. qp->s_retry,
  568. qp->s_retry_cnt,
  569. qp->s_rnr_retry_cnt,
  570. qp->s_rnr_retry,
  571. sde,
  572. sde ? sde->this_idx : 0,
  573. send_context,
  574. send_context ? send_context->sw_index : 0,
  575. ibcq_to_rvtcq(qp->ibqp.send_cq)->queue->head,
  576. ibcq_to_rvtcq(qp->ibqp.send_cq)->queue->tail,
  577. qp->pid);
  578. }
  579. void *qp_priv_alloc(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  580. gfp_t gfp)
  581. {
  582. struct hfi1_qp_priv *priv;
  583. priv = kzalloc_node(sizeof(*priv), gfp, rdi->dparms.node);
  584. if (!priv)
  585. return ERR_PTR(-ENOMEM);
  586. priv->owner = qp;
  587. priv->s_ahg = kzalloc_node(sizeof(*priv->s_ahg), gfp,
  588. rdi->dparms.node);
  589. if (!priv->s_ahg) {
  590. kfree(priv);
  591. return ERR_PTR(-ENOMEM);
  592. }
  593. iowait_init(
  594. &priv->s_iowait,
  595. 1,
  596. _hfi1_do_send,
  597. iowait_sleep,
  598. iowait_wakeup,
  599. iowait_sdma_drained);
  600. return priv;
  601. }
  602. void qp_priv_free(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  603. {
  604. struct hfi1_qp_priv *priv = qp->priv;
  605. kfree(priv->s_ahg);
  606. kfree(priv);
  607. }
  608. unsigned free_all_qps(struct rvt_dev_info *rdi)
  609. {
  610. struct hfi1_ibdev *verbs_dev = container_of(rdi,
  611. struct hfi1_ibdev,
  612. rdi);
  613. struct hfi1_devdata *dd = container_of(verbs_dev,
  614. struct hfi1_devdata,
  615. verbs_dev);
  616. int n;
  617. unsigned qp_inuse = 0;
  618. for (n = 0; n < dd->num_pports; n++) {
  619. struct hfi1_ibport *ibp = &dd->pport[n].ibport_data;
  620. rcu_read_lock();
  621. if (rcu_dereference(ibp->rvp.qp[0]))
  622. qp_inuse++;
  623. if (rcu_dereference(ibp->rvp.qp[1]))
  624. qp_inuse++;
  625. rcu_read_unlock();
  626. }
  627. return qp_inuse;
  628. }
  629. void flush_qp_waiters(struct rvt_qp *qp)
  630. {
  631. lockdep_assert_held(&qp->s_lock);
  632. flush_iowait(qp);
  633. }
  634. void stop_send_queue(struct rvt_qp *qp)
  635. {
  636. struct hfi1_qp_priv *priv = qp->priv;
  637. cancel_work_sync(&priv->s_iowait.iowork);
  638. }
  639. void quiesce_qp(struct rvt_qp *qp)
  640. {
  641. struct hfi1_qp_priv *priv = qp->priv;
  642. iowait_sdma_drain(&priv->s_iowait);
  643. qp_pio_drain(qp);
  644. flush_tx_list(qp);
  645. }
  646. void notify_qp_reset(struct rvt_qp *qp)
  647. {
  648. struct hfi1_qp_priv *priv = qp->priv;
  649. priv->r_adefered = 0;
  650. clear_ahg(qp);
  651. }
  652. /*
  653. * Switch to alternate path.
  654. * The QP s_lock should be held and interrupts disabled.
  655. */
  656. void hfi1_migrate_qp(struct rvt_qp *qp)
  657. {
  658. struct hfi1_qp_priv *priv = qp->priv;
  659. struct ib_event ev;
  660. qp->s_mig_state = IB_MIG_MIGRATED;
  661. qp->remote_ah_attr = qp->alt_ah_attr;
  662. qp->port_num = qp->alt_ah_attr.port_num;
  663. qp->s_pkey_index = qp->s_alt_pkey_index;
  664. qp->s_flags |= RVT_S_AHG_CLEAR;
  665. priv->s_sc = ah_to_sc(qp->ibqp.device, &qp->remote_ah_attr);
  666. priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
  667. ev.device = qp->ibqp.device;
  668. ev.element.qp = &qp->ibqp;
  669. ev.event = IB_EVENT_PATH_MIG;
  670. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  671. }
  672. int mtu_to_path_mtu(u32 mtu)
  673. {
  674. return mtu_to_enum(mtu, OPA_MTU_8192);
  675. }
  676. u32 mtu_from_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, u32 pmtu)
  677. {
  678. u32 mtu;
  679. struct hfi1_ibdev *verbs_dev = container_of(rdi,
  680. struct hfi1_ibdev,
  681. rdi);
  682. struct hfi1_devdata *dd = container_of(verbs_dev,
  683. struct hfi1_devdata,
  684. verbs_dev);
  685. struct hfi1_ibport *ibp;
  686. u8 sc, vl;
  687. ibp = &dd->pport[qp->port_num - 1].ibport_data;
  688. sc = ibp->sl_to_sc[qp->remote_ah_attr.sl];
  689. vl = sc_to_vlt(dd, sc);
  690. mtu = verbs_mtu_enum_to_int(qp->ibqp.device, pmtu);
  691. if (vl < PER_VL_SEND_CONTEXTS)
  692. mtu = min_t(u32, mtu, dd->vld[vl].mtu);
  693. return mtu;
  694. }
  695. int get_pmtu_from_attr(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  696. struct ib_qp_attr *attr)
  697. {
  698. int mtu, pidx = qp->port_num - 1;
  699. struct hfi1_ibdev *verbs_dev = container_of(rdi,
  700. struct hfi1_ibdev,
  701. rdi);
  702. struct hfi1_devdata *dd = container_of(verbs_dev,
  703. struct hfi1_devdata,
  704. verbs_dev);
  705. mtu = verbs_mtu_enum_to_int(qp->ibqp.device, attr->path_mtu);
  706. if (mtu == -1)
  707. return -1; /* values less than 0 are error */
  708. if (mtu > dd->pport[pidx].ibmtu)
  709. return mtu_to_enum(dd->pport[pidx].ibmtu, IB_MTU_2048);
  710. else
  711. return attr->path_mtu;
  712. }
  713. void notify_error_qp(struct rvt_qp *qp)
  714. {
  715. struct hfi1_qp_priv *priv = qp->priv;
  716. seqlock_t *lock = priv->s_iowait.lock;
  717. if (lock) {
  718. write_seqlock(lock);
  719. if (!list_empty(&priv->s_iowait.list) &&
  720. !(qp->s_flags & RVT_S_BUSY)) {
  721. qp->s_flags &= ~RVT_S_ANY_WAIT_IO;
  722. list_del_init(&priv->s_iowait.list);
  723. priv->s_iowait.lock = NULL;
  724. rvt_put_qp(qp);
  725. }
  726. write_sequnlock(lock);
  727. }
  728. if (!(qp->s_flags & RVT_S_BUSY)) {
  729. qp->s_hdrwords = 0;
  730. if (qp->s_rdma_mr) {
  731. rvt_put_mr(qp->s_rdma_mr);
  732. qp->s_rdma_mr = NULL;
  733. }
  734. flush_tx_list(qp);
  735. }
  736. }
  737. /**
  738. * hfi1_error_port_qps - put a port's RC/UC qps into error state
  739. * @ibp: the ibport.
  740. * @sl: the service level.
  741. *
  742. * This function places all RC/UC qps with a given service level into error
  743. * state. It is generally called to force upper lay apps to abandon stale qps
  744. * after an sl->sc mapping change.
  745. */
  746. void hfi1_error_port_qps(struct hfi1_ibport *ibp, u8 sl)
  747. {
  748. struct rvt_qp *qp = NULL;
  749. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  750. struct hfi1_ibdev *dev = &ppd->dd->verbs_dev;
  751. int n;
  752. int lastwqe;
  753. struct ib_event ev;
  754. rcu_read_lock();
  755. /* Deal only with RC/UC qps that use the given SL. */
  756. for (n = 0; n < dev->rdi.qp_dev->qp_table_size; n++) {
  757. for (qp = rcu_dereference(dev->rdi.qp_dev->qp_table[n]); qp;
  758. qp = rcu_dereference(qp->next)) {
  759. if (qp->port_num == ppd->port &&
  760. (qp->ibqp.qp_type == IB_QPT_UC ||
  761. qp->ibqp.qp_type == IB_QPT_RC) &&
  762. qp->remote_ah_attr.sl == sl &&
  763. (ib_rvt_state_ops[qp->state] &
  764. RVT_POST_SEND_OK)) {
  765. spin_lock_irq(&qp->r_lock);
  766. spin_lock(&qp->s_hlock);
  767. spin_lock(&qp->s_lock);
  768. lastwqe = rvt_error_qp(qp,
  769. IB_WC_WR_FLUSH_ERR);
  770. spin_unlock(&qp->s_lock);
  771. spin_unlock(&qp->s_hlock);
  772. spin_unlock_irq(&qp->r_lock);
  773. if (lastwqe) {
  774. ev.device = qp->ibqp.device;
  775. ev.element.qp = &qp->ibqp;
  776. ev.event =
  777. IB_EVENT_QP_LAST_WQE_REACHED;
  778. qp->ibqp.event_handler(&ev,
  779. qp->ibqp.qp_context);
  780. }
  781. }
  782. }
  783. }
  784. rcu_read_unlock();
  785. }