qp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * Copyright(c) 2015 - 2018 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_work *wait,
  66. struct sdma_txreq *stx,
  67. unsigned int seq,
  68. bool pkts_sent);
  69. static void iowait_wakeup(struct iowait *wait, int reason);
  70. static void iowait_sdma_drained(struct iowait *wait);
  71. static void qp_pio_drain(struct rvt_qp *qp);
  72. const struct rvt_operation_params hfi1_post_parms[RVT_OPERATION_MAX] = {
  73. [IB_WR_RDMA_WRITE] = {
  74. .length = sizeof(struct ib_rdma_wr),
  75. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  76. },
  77. [IB_WR_RDMA_READ] = {
  78. .length = sizeof(struct ib_rdma_wr),
  79. .qpt_support = BIT(IB_QPT_RC),
  80. .flags = RVT_OPERATION_ATOMIC,
  81. },
  82. [IB_WR_ATOMIC_CMP_AND_SWP] = {
  83. .length = sizeof(struct ib_atomic_wr),
  84. .qpt_support = BIT(IB_QPT_RC),
  85. .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
  86. },
  87. [IB_WR_ATOMIC_FETCH_AND_ADD] = {
  88. .length = sizeof(struct ib_atomic_wr),
  89. .qpt_support = BIT(IB_QPT_RC),
  90. .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
  91. },
  92. [IB_WR_RDMA_WRITE_WITH_IMM] = {
  93. .length = sizeof(struct ib_rdma_wr),
  94. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  95. },
  96. [IB_WR_SEND] = {
  97. .length = sizeof(struct ib_send_wr),
  98. .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
  99. BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  100. },
  101. [IB_WR_SEND_WITH_IMM] = {
  102. .length = sizeof(struct ib_send_wr),
  103. .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
  104. BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  105. },
  106. [IB_WR_REG_MR] = {
  107. .length = sizeof(struct ib_reg_wr),
  108. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  109. .flags = RVT_OPERATION_LOCAL,
  110. },
  111. [IB_WR_LOCAL_INV] = {
  112. .length = sizeof(struct ib_send_wr),
  113. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  114. .flags = RVT_OPERATION_LOCAL,
  115. },
  116. [IB_WR_SEND_WITH_INV] = {
  117. .length = sizeof(struct ib_send_wr),
  118. .qpt_support = BIT(IB_QPT_RC),
  119. },
  120. };
  121. static void flush_list_head(struct list_head *l)
  122. {
  123. while (!list_empty(l)) {
  124. struct sdma_txreq *tx;
  125. tx = list_first_entry(
  126. l,
  127. struct sdma_txreq,
  128. list);
  129. list_del_init(&tx->list);
  130. hfi1_put_txreq(
  131. container_of(tx, struct verbs_txreq, txreq));
  132. }
  133. }
  134. static void flush_tx_list(struct rvt_qp *qp)
  135. {
  136. struct hfi1_qp_priv *priv = qp->priv;
  137. flush_list_head(&iowait_get_ib_work(&priv->s_iowait)->tx_head);
  138. flush_list_head(&iowait_get_tid_work(&priv->s_iowait)->tx_head);
  139. }
  140. static void flush_iowait(struct rvt_qp *qp)
  141. {
  142. struct hfi1_qp_priv *priv = qp->priv;
  143. unsigned long flags;
  144. seqlock_t *lock = priv->s_iowait.lock;
  145. if (!lock)
  146. return;
  147. write_seqlock_irqsave(lock, flags);
  148. if (!list_empty(&priv->s_iowait.list)) {
  149. list_del_init(&priv->s_iowait.list);
  150. priv->s_iowait.lock = NULL;
  151. rvt_put_qp(qp);
  152. }
  153. write_sequnlock_irqrestore(lock, flags);
  154. }
  155. static inline int opa_mtu_enum_to_int(int mtu)
  156. {
  157. switch (mtu) {
  158. case OPA_MTU_8192: return 8192;
  159. case OPA_MTU_10240: return 10240;
  160. default: return -1;
  161. }
  162. }
  163. /**
  164. * This function is what we would push to the core layer if we wanted to be a
  165. * "first class citizen". Instead we hide this here and rely on Verbs ULPs
  166. * to blindly pass the MTU enum value from the PathRecord to us.
  167. */
  168. static inline int verbs_mtu_enum_to_int(struct ib_device *dev, enum ib_mtu mtu)
  169. {
  170. int val;
  171. /* Constraining 10KB packets to 8KB packets */
  172. if (mtu == (enum ib_mtu)OPA_MTU_10240)
  173. mtu = OPA_MTU_8192;
  174. val = opa_mtu_enum_to_int((int)mtu);
  175. if (val > 0)
  176. return val;
  177. return ib_mtu_enum_to_int(mtu);
  178. }
  179. int hfi1_check_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr,
  180. int attr_mask, struct ib_udata *udata)
  181. {
  182. struct ib_qp *ibqp = &qp->ibqp;
  183. struct hfi1_ibdev *dev = to_idev(ibqp->device);
  184. struct hfi1_devdata *dd = dd_from_dev(dev);
  185. u8 sc;
  186. if (attr_mask & IB_QP_AV) {
  187. sc = ah_to_sc(ibqp->device, &attr->ah_attr);
  188. if (sc == 0xf)
  189. return -EINVAL;
  190. if (!qp_to_sdma_engine(qp, sc) &&
  191. dd->flags & HFI1_HAS_SEND_DMA)
  192. return -EINVAL;
  193. if (!qp_to_send_context(qp, sc))
  194. return -EINVAL;
  195. }
  196. if (attr_mask & IB_QP_ALT_PATH) {
  197. sc = ah_to_sc(ibqp->device, &attr->alt_ah_attr);
  198. if (sc == 0xf)
  199. return -EINVAL;
  200. if (!qp_to_sdma_engine(qp, sc) &&
  201. dd->flags & HFI1_HAS_SEND_DMA)
  202. return -EINVAL;
  203. if (!qp_to_send_context(qp, sc))
  204. return -EINVAL;
  205. }
  206. return 0;
  207. }
  208. /*
  209. * qp_set_16b - Set the hdr_type based on whether the slid or the
  210. * dlid in the connection is extended. Only applicable for RC and UC
  211. * QPs. UD QPs determine this on the fly from the ah in the wqe
  212. */
  213. static inline void qp_set_16b(struct rvt_qp *qp)
  214. {
  215. struct hfi1_pportdata *ppd;
  216. struct hfi1_ibport *ibp;
  217. struct hfi1_qp_priv *priv = qp->priv;
  218. /* Update ah_attr to account for extended LIDs */
  219. hfi1_update_ah_attr(qp->ibqp.device, &qp->remote_ah_attr);
  220. /* Create 32 bit LIDs */
  221. hfi1_make_opa_lid(&qp->remote_ah_attr);
  222. if (!(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH))
  223. return;
  224. ibp = to_iport(qp->ibqp.device, qp->port_num);
  225. ppd = ppd_from_ibp(ibp);
  226. priv->hdr_type = hfi1_get_hdr_type(ppd->lid, &qp->remote_ah_attr);
  227. }
  228. void hfi1_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr,
  229. int attr_mask, struct ib_udata *udata)
  230. {
  231. struct ib_qp *ibqp = &qp->ibqp;
  232. struct hfi1_qp_priv *priv = qp->priv;
  233. if (attr_mask & IB_QP_AV) {
  234. priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr);
  235. priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
  236. priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
  237. qp_set_16b(qp);
  238. }
  239. if (attr_mask & IB_QP_PATH_MIG_STATE &&
  240. attr->path_mig_state == IB_MIG_MIGRATED &&
  241. qp->s_mig_state == IB_MIG_ARMED) {
  242. qp->s_flags |= HFI1_S_AHG_CLEAR;
  243. priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr);
  244. priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
  245. priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
  246. qp_set_16b(qp);
  247. }
  248. }
  249. /**
  250. * hfi1_setup_wqe - set up the wqe
  251. * @qp - The qp
  252. * @wqe - The built wqe
  253. * @call_send - Determine if the send should be posted or scheduled.
  254. *
  255. * Perform setup of the wqe. This is called
  256. * prior to inserting the wqe into the ring but after
  257. * the wqe has been setup by RDMAVT. This function
  258. * allows the driver the opportunity to perform
  259. * validation and additional setup of the wqe.
  260. *
  261. * Returns 0 on success, -EINVAL on failure
  262. *
  263. */
  264. int hfi1_setup_wqe(struct rvt_qp *qp, struct rvt_swqe *wqe, bool *call_send)
  265. {
  266. struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  267. struct rvt_ah *ah;
  268. struct hfi1_pportdata *ppd;
  269. struct hfi1_devdata *dd;
  270. switch (qp->ibqp.qp_type) {
  271. case IB_QPT_RC:
  272. case IB_QPT_UC:
  273. if (wqe->length > 0x80000000U)
  274. return -EINVAL;
  275. if (wqe->length > qp->pmtu)
  276. *call_send = false;
  277. break;
  278. case IB_QPT_SMI:
  279. /*
  280. * SM packets should exclusively use VL15 and their SL is
  281. * ignored (IBTA v1.3, Section 3.5.8.2). Therefore, when ah
  282. * is created, SL is 0 in most cases and as a result some
  283. * fields (vl and pmtu) in ah may not be set correctly,
  284. * depending on the SL2SC and SC2VL tables at the time.
  285. */
  286. ppd = ppd_from_ibp(ibp);
  287. dd = dd_from_ppd(ppd);
  288. if (wqe->length > dd->vld[15].mtu)
  289. return -EINVAL;
  290. break;
  291. case IB_QPT_GSI:
  292. case IB_QPT_UD:
  293. ah = ibah_to_rvtah(wqe->ud_wr.ah);
  294. if (wqe->length > (1 << ah->log_pmtu))
  295. return -EINVAL;
  296. if (ibp->sl_to_sc[rdma_ah_get_sl(&ah->attr)] == 0xf)
  297. return -EINVAL;
  298. default:
  299. break;
  300. }
  301. /*
  302. * System latency between send and schedule is large enough that
  303. * forcing call_send to true for piothreshold packets is necessary.
  304. */
  305. if (wqe->length <= piothreshold)
  306. *call_send = true;
  307. return 0;
  308. }
  309. /**
  310. * _hfi1_schedule_send - schedule progress
  311. * @qp: the QP
  312. *
  313. * This schedules qp progress w/o regard to the s_flags.
  314. *
  315. * It is only used in the post send, which doesn't hold
  316. * the s_lock.
  317. */
  318. bool _hfi1_schedule_send(struct rvt_qp *qp)
  319. {
  320. struct hfi1_qp_priv *priv = qp->priv;
  321. struct hfi1_ibport *ibp =
  322. to_iport(qp->ibqp.device, qp->port_num);
  323. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  324. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  325. return iowait_schedule(&priv->s_iowait, ppd->hfi1_wq,
  326. priv->s_sde ?
  327. priv->s_sde->cpu :
  328. cpumask_first(cpumask_of_node(dd->node)));
  329. }
  330. static void qp_pio_drain(struct rvt_qp *qp)
  331. {
  332. struct hfi1_ibdev *dev;
  333. struct hfi1_qp_priv *priv = qp->priv;
  334. if (!priv->s_sendcontext)
  335. return;
  336. dev = to_idev(qp->ibqp.device);
  337. while (iowait_pio_pending(&priv->s_iowait)) {
  338. write_seqlock_irq(&dev->iowait_lock);
  339. hfi1_sc_wantpiobuf_intr(priv->s_sendcontext, 1);
  340. write_sequnlock_irq(&dev->iowait_lock);
  341. iowait_pio_drain(&priv->s_iowait);
  342. write_seqlock_irq(&dev->iowait_lock);
  343. hfi1_sc_wantpiobuf_intr(priv->s_sendcontext, 0);
  344. write_sequnlock_irq(&dev->iowait_lock);
  345. }
  346. }
  347. /**
  348. * hfi1_schedule_send - schedule progress
  349. * @qp: the QP
  350. *
  351. * This schedules qp progress and caller should hold
  352. * the s_lock.
  353. * @return true if the first leg is scheduled;
  354. * false if the first leg is not scheduled.
  355. */
  356. bool hfi1_schedule_send(struct rvt_qp *qp)
  357. {
  358. lockdep_assert_held(&qp->s_lock);
  359. if (hfi1_send_ok(qp)) {
  360. _hfi1_schedule_send(qp);
  361. return true;
  362. }
  363. if (qp->s_flags & HFI1_S_ANY_WAIT_IO)
  364. iowait_set_flag(&((struct hfi1_qp_priv *)qp->priv)->s_iowait,
  365. IOWAIT_PENDING_IB);
  366. return false;
  367. }
  368. static void hfi1_qp_schedule(struct rvt_qp *qp)
  369. {
  370. struct hfi1_qp_priv *priv = qp->priv;
  371. bool ret;
  372. if (iowait_flag_set(&priv->s_iowait, IOWAIT_PENDING_IB)) {
  373. ret = hfi1_schedule_send(qp);
  374. if (ret)
  375. iowait_clear_flag(&priv->s_iowait, IOWAIT_PENDING_IB);
  376. }
  377. }
  378. void hfi1_qp_wakeup(struct rvt_qp *qp, u32 flag)
  379. {
  380. unsigned long flags;
  381. spin_lock_irqsave(&qp->s_lock, flags);
  382. if (qp->s_flags & flag) {
  383. qp->s_flags &= ~flag;
  384. trace_hfi1_qpwakeup(qp, flag);
  385. hfi1_qp_schedule(qp);
  386. }
  387. spin_unlock_irqrestore(&qp->s_lock, flags);
  388. /* Notify hfi1_destroy_qp() if it is waiting. */
  389. rvt_put_qp(qp);
  390. }
  391. void hfi1_qp_unbusy(struct rvt_qp *qp, struct iowait_work *wait)
  392. {
  393. if (iowait_set_work_flag(wait) == IOWAIT_IB_SE)
  394. qp->s_flags &= ~RVT_S_BUSY;
  395. }
  396. static int iowait_sleep(
  397. struct sdma_engine *sde,
  398. struct iowait_work *wait,
  399. struct sdma_txreq *stx,
  400. uint seq,
  401. bool pkts_sent)
  402. {
  403. struct verbs_txreq *tx = container_of(stx, struct verbs_txreq, txreq);
  404. struct rvt_qp *qp;
  405. struct hfi1_qp_priv *priv;
  406. unsigned long flags;
  407. int ret = 0;
  408. struct hfi1_ibdev *dev;
  409. qp = tx->qp;
  410. priv = qp->priv;
  411. spin_lock_irqsave(&qp->s_lock, flags);
  412. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  413. /*
  414. * If we couldn't queue the DMA request, save the info
  415. * and try again later rather than destroying the
  416. * buffer and undoing the side effects of the copy.
  417. */
  418. /* Make a common routine? */
  419. dev = &sde->dd->verbs_dev;
  420. list_add_tail(&stx->list, &wait->tx_head);
  421. write_seqlock(&dev->iowait_lock);
  422. if (sdma_progress(sde, seq, stx))
  423. goto eagain;
  424. if (list_empty(&priv->s_iowait.list)) {
  425. struct hfi1_ibport *ibp =
  426. to_iport(qp->ibqp.device, qp->port_num);
  427. ibp->rvp.n_dmawait++;
  428. qp->s_flags |= RVT_S_WAIT_DMA_DESC;
  429. iowait_queue(pkts_sent, &priv->s_iowait,
  430. &sde->dmawait);
  431. priv->s_iowait.lock = &dev->iowait_lock;
  432. trace_hfi1_qpsleep(qp, RVT_S_WAIT_DMA_DESC);
  433. rvt_get_qp(qp);
  434. }
  435. write_sequnlock(&dev->iowait_lock);
  436. hfi1_qp_unbusy(qp, wait);
  437. spin_unlock_irqrestore(&qp->s_lock, flags);
  438. ret = -EBUSY;
  439. } else {
  440. spin_unlock_irqrestore(&qp->s_lock, flags);
  441. hfi1_put_txreq(tx);
  442. }
  443. return ret;
  444. eagain:
  445. write_sequnlock(&dev->iowait_lock);
  446. spin_unlock_irqrestore(&qp->s_lock, flags);
  447. list_del_init(&stx->list);
  448. return -EAGAIN;
  449. }
  450. static void iowait_wakeup(struct iowait *wait, int reason)
  451. {
  452. struct rvt_qp *qp = iowait_to_qp(wait);
  453. WARN_ON(reason != SDMA_AVAIL_REASON);
  454. hfi1_qp_wakeup(qp, RVT_S_WAIT_DMA_DESC);
  455. }
  456. static void iowait_sdma_drained(struct iowait *wait)
  457. {
  458. struct rvt_qp *qp = iowait_to_qp(wait);
  459. unsigned long flags;
  460. /*
  461. * This happens when the send engine notes
  462. * a QP in the error state and cannot
  463. * do the flush work until that QP's
  464. * sdma work has finished.
  465. */
  466. spin_lock_irqsave(&qp->s_lock, flags);
  467. if (qp->s_flags & RVT_S_WAIT_DMA) {
  468. qp->s_flags &= ~RVT_S_WAIT_DMA;
  469. hfi1_schedule_send(qp);
  470. }
  471. spin_unlock_irqrestore(&qp->s_lock, flags);
  472. }
  473. /**
  474. * qp_to_sdma_engine - map a qp to a send engine
  475. * @qp: the QP
  476. * @sc5: the 5 bit sc
  477. *
  478. * Return:
  479. * A send engine for the qp or NULL for SMI type qp.
  480. */
  481. struct sdma_engine *qp_to_sdma_engine(struct rvt_qp *qp, u8 sc5)
  482. {
  483. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  484. struct sdma_engine *sde;
  485. if (!(dd->flags & HFI1_HAS_SEND_DMA))
  486. return NULL;
  487. switch (qp->ibqp.qp_type) {
  488. case IB_QPT_SMI:
  489. return NULL;
  490. default:
  491. break;
  492. }
  493. sde = sdma_select_engine_sc(dd, qp->ibqp.qp_num >> dd->qos_shift, sc5);
  494. return sde;
  495. }
  496. /*
  497. * qp_to_send_context - map a qp to a send context
  498. * @qp: the QP
  499. * @sc5: the 5 bit sc
  500. *
  501. * Return:
  502. * A send context for the qp
  503. */
  504. struct send_context *qp_to_send_context(struct rvt_qp *qp, u8 sc5)
  505. {
  506. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  507. switch (qp->ibqp.qp_type) {
  508. case IB_QPT_SMI:
  509. /* SMA packets to VL15 */
  510. return dd->vld[15].sc;
  511. default:
  512. break;
  513. }
  514. return pio_select_send_context_sc(dd, qp->ibqp.qp_num >> dd->qos_shift,
  515. sc5);
  516. }
  517. static const char * const qp_type_str[] = {
  518. "SMI", "GSI", "RC", "UC", "UD",
  519. };
  520. static int qp_idle(struct rvt_qp *qp)
  521. {
  522. return
  523. qp->s_last == qp->s_acked &&
  524. qp->s_acked == qp->s_cur &&
  525. qp->s_cur == qp->s_tail &&
  526. qp->s_tail == qp->s_head;
  527. }
  528. /**
  529. * qp_iter_print - print the qp information to seq_file
  530. * @s: the seq_file to emit the qp information on
  531. * @iter: the iterator for the qp hash list
  532. */
  533. void qp_iter_print(struct seq_file *s, struct rvt_qp_iter *iter)
  534. {
  535. struct rvt_swqe *wqe;
  536. struct rvt_qp *qp = iter->qp;
  537. struct hfi1_qp_priv *priv = qp->priv;
  538. struct sdma_engine *sde;
  539. struct send_context *send_context;
  540. struct rvt_ack_entry *e = NULL;
  541. struct rvt_srq *srq = qp->ibqp.srq ?
  542. ibsrq_to_rvtsrq(qp->ibqp.srq) : NULL;
  543. sde = qp_to_sdma_engine(qp, priv->s_sc);
  544. wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  545. send_context = qp_to_send_context(qp, priv->s_sc);
  546. if (qp->s_ack_queue)
  547. e = &qp->s_ack_queue[qp->s_tail_ack_queue];
  548. seq_printf(s,
  549. "N %d %s QP %x R %u %s %u %u f=%x %u %u %u %u %u %u SPSN %x %x %x %x %x RPSN %x S(%u %u %u %u %u %u %u) R(%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 OS %x %x E %x %x %x RNR %d %s %d\n",
  550. iter->n,
  551. qp_idle(qp) ? "I" : "B",
  552. qp->ibqp.qp_num,
  553. atomic_read(&qp->refcount),
  554. qp_type_str[qp->ibqp.qp_type],
  555. qp->state,
  556. wqe ? wqe->wr.opcode : 0,
  557. qp->s_flags,
  558. iowait_sdma_pending(&priv->s_iowait),
  559. iowait_pio_pending(&priv->s_iowait),
  560. !list_empty(&priv->s_iowait.list),
  561. qp->timeout,
  562. wqe ? wqe->ssn : 0,
  563. qp->s_lsn,
  564. qp->s_last_psn,
  565. qp->s_psn, qp->s_next_psn,
  566. qp->s_sending_psn, qp->s_sending_hpsn,
  567. qp->r_psn,
  568. qp->s_last, qp->s_acked, qp->s_cur,
  569. qp->s_tail, qp->s_head, qp->s_size,
  570. qp->s_avail,
  571. /* ack_queue ring pointers, size */
  572. qp->s_tail_ack_queue, qp->r_head_ack_queue,
  573. rvt_max_atomic(&to_idev(qp->ibqp.device)->rdi),
  574. /* remote QP info */
  575. qp->remote_qpn,
  576. rdma_ah_get_dlid(&qp->remote_ah_attr),
  577. rdma_ah_get_sl(&qp->remote_ah_attr),
  578. qp->pmtu,
  579. qp->s_retry,
  580. qp->s_retry_cnt,
  581. qp->s_rnr_retry_cnt,
  582. qp->s_rnr_retry,
  583. sde,
  584. sde ? sde->this_idx : 0,
  585. send_context,
  586. send_context ? send_context->sw_index : 0,
  587. ibcq_to_rvtcq(qp->ibqp.send_cq)->queue->head,
  588. ibcq_to_rvtcq(qp->ibqp.send_cq)->queue->tail,
  589. qp->pid,
  590. qp->s_state,
  591. qp->s_ack_state,
  592. /* ack queue information */
  593. e ? e->opcode : 0,
  594. e ? e->psn : 0,
  595. e ? e->lpsn : 0,
  596. qp->r_min_rnr_timer,
  597. srq ? "SRQ" : "RQ",
  598. srq ? srq->rq.size : qp->r_rq.size
  599. );
  600. }
  601. void *qp_priv_alloc(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  602. {
  603. struct hfi1_qp_priv *priv;
  604. priv = kzalloc_node(sizeof(*priv), GFP_KERNEL, rdi->dparms.node);
  605. if (!priv)
  606. return ERR_PTR(-ENOMEM);
  607. priv->owner = qp;
  608. priv->s_ahg = kzalloc_node(sizeof(*priv->s_ahg), GFP_KERNEL,
  609. rdi->dparms.node);
  610. if (!priv->s_ahg) {
  611. kfree(priv);
  612. return ERR_PTR(-ENOMEM);
  613. }
  614. iowait_init(
  615. &priv->s_iowait,
  616. 1,
  617. _hfi1_do_send,
  618. NULL,
  619. iowait_sleep,
  620. iowait_wakeup,
  621. iowait_sdma_drained);
  622. return priv;
  623. }
  624. void qp_priv_free(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  625. {
  626. struct hfi1_qp_priv *priv = qp->priv;
  627. kfree(priv->s_ahg);
  628. kfree(priv);
  629. }
  630. unsigned free_all_qps(struct rvt_dev_info *rdi)
  631. {
  632. struct hfi1_ibdev *verbs_dev = container_of(rdi,
  633. struct hfi1_ibdev,
  634. rdi);
  635. struct hfi1_devdata *dd = container_of(verbs_dev,
  636. struct hfi1_devdata,
  637. verbs_dev);
  638. int n;
  639. unsigned qp_inuse = 0;
  640. for (n = 0; n < dd->num_pports; n++) {
  641. struct hfi1_ibport *ibp = &dd->pport[n].ibport_data;
  642. rcu_read_lock();
  643. if (rcu_dereference(ibp->rvp.qp[0]))
  644. qp_inuse++;
  645. if (rcu_dereference(ibp->rvp.qp[1]))
  646. qp_inuse++;
  647. rcu_read_unlock();
  648. }
  649. return qp_inuse;
  650. }
  651. void flush_qp_waiters(struct rvt_qp *qp)
  652. {
  653. lockdep_assert_held(&qp->s_lock);
  654. flush_iowait(qp);
  655. }
  656. void stop_send_queue(struct rvt_qp *qp)
  657. {
  658. struct hfi1_qp_priv *priv = qp->priv;
  659. iowait_cancel_work(&priv->s_iowait);
  660. }
  661. void quiesce_qp(struct rvt_qp *qp)
  662. {
  663. struct hfi1_qp_priv *priv = qp->priv;
  664. iowait_sdma_drain(&priv->s_iowait);
  665. qp_pio_drain(qp);
  666. flush_tx_list(qp);
  667. }
  668. void notify_qp_reset(struct rvt_qp *qp)
  669. {
  670. qp->r_adefered = 0;
  671. clear_ahg(qp);
  672. }
  673. /*
  674. * Switch to alternate path.
  675. * The QP s_lock should be held and interrupts disabled.
  676. */
  677. void hfi1_migrate_qp(struct rvt_qp *qp)
  678. {
  679. struct hfi1_qp_priv *priv = qp->priv;
  680. struct ib_event ev;
  681. qp->s_mig_state = IB_MIG_MIGRATED;
  682. qp->remote_ah_attr = qp->alt_ah_attr;
  683. qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
  684. qp->s_pkey_index = qp->s_alt_pkey_index;
  685. qp->s_flags |= HFI1_S_AHG_CLEAR;
  686. priv->s_sc = ah_to_sc(qp->ibqp.device, &qp->remote_ah_attr);
  687. priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
  688. qp_set_16b(qp);
  689. ev.device = qp->ibqp.device;
  690. ev.element.qp = &qp->ibqp;
  691. ev.event = IB_EVENT_PATH_MIG;
  692. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  693. }
  694. int mtu_to_path_mtu(u32 mtu)
  695. {
  696. return mtu_to_enum(mtu, OPA_MTU_8192);
  697. }
  698. u32 mtu_from_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, u32 pmtu)
  699. {
  700. u32 mtu;
  701. struct hfi1_ibdev *verbs_dev = container_of(rdi,
  702. struct hfi1_ibdev,
  703. rdi);
  704. struct hfi1_devdata *dd = container_of(verbs_dev,
  705. struct hfi1_devdata,
  706. verbs_dev);
  707. struct hfi1_ibport *ibp;
  708. u8 sc, vl;
  709. ibp = &dd->pport[qp->port_num - 1].ibport_data;
  710. sc = ibp->sl_to_sc[rdma_ah_get_sl(&qp->remote_ah_attr)];
  711. vl = sc_to_vlt(dd, sc);
  712. mtu = verbs_mtu_enum_to_int(qp->ibqp.device, pmtu);
  713. if (vl < PER_VL_SEND_CONTEXTS)
  714. mtu = min_t(u32, mtu, dd->vld[vl].mtu);
  715. return mtu;
  716. }
  717. int get_pmtu_from_attr(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  718. struct ib_qp_attr *attr)
  719. {
  720. int mtu, pidx = qp->port_num - 1;
  721. struct hfi1_ibdev *verbs_dev = container_of(rdi,
  722. struct hfi1_ibdev,
  723. rdi);
  724. struct hfi1_devdata *dd = container_of(verbs_dev,
  725. struct hfi1_devdata,
  726. verbs_dev);
  727. mtu = verbs_mtu_enum_to_int(qp->ibqp.device, attr->path_mtu);
  728. if (mtu == -1)
  729. return -1; /* values less than 0 are error */
  730. if (mtu > dd->pport[pidx].ibmtu)
  731. return mtu_to_enum(dd->pport[pidx].ibmtu, IB_MTU_2048);
  732. else
  733. return attr->path_mtu;
  734. }
  735. void notify_error_qp(struct rvt_qp *qp)
  736. {
  737. struct hfi1_qp_priv *priv = qp->priv;
  738. seqlock_t *lock = priv->s_iowait.lock;
  739. if (lock) {
  740. write_seqlock(lock);
  741. if (!list_empty(&priv->s_iowait.list) &&
  742. !(qp->s_flags & RVT_S_BUSY)) {
  743. qp->s_flags &= ~RVT_S_ANY_WAIT_IO;
  744. list_del_init(&priv->s_iowait.list);
  745. priv->s_iowait.lock = NULL;
  746. rvt_put_qp(qp);
  747. }
  748. write_sequnlock(lock);
  749. }
  750. if (!(qp->s_flags & RVT_S_BUSY)) {
  751. if (qp->s_rdma_mr) {
  752. rvt_put_mr(qp->s_rdma_mr);
  753. qp->s_rdma_mr = NULL;
  754. }
  755. flush_tx_list(qp);
  756. }
  757. }
  758. /**
  759. * hfi1_qp_iter_cb - callback for iterator
  760. * @qp - the qp
  761. * @v - the sl in low bits of v
  762. *
  763. * This is called from the iterator callback to work
  764. * on an individual qp.
  765. */
  766. static void hfi1_qp_iter_cb(struct rvt_qp *qp, u64 v)
  767. {
  768. int lastwqe;
  769. struct ib_event ev;
  770. struct hfi1_ibport *ibp =
  771. to_iport(qp->ibqp.device, qp->port_num);
  772. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  773. u8 sl = (u8)v;
  774. if (qp->port_num != ppd->port ||
  775. (qp->ibqp.qp_type != IB_QPT_UC &&
  776. qp->ibqp.qp_type != IB_QPT_RC) ||
  777. rdma_ah_get_sl(&qp->remote_ah_attr) != sl ||
  778. !(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))
  779. return;
  780. spin_lock_irq(&qp->r_lock);
  781. spin_lock(&qp->s_hlock);
  782. spin_lock(&qp->s_lock);
  783. lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  784. spin_unlock(&qp->s_lock);
  785. spin_unlock(&qp->s_hlock);
  786. spin_unlock_irq(&qp->r_lock);
  787. if (lastwqe) {
  788. ev.device = qp->ibqp.device;
  789. ev.element.qp = &qp->ibqp;
  790. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  791. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  792. }
  793. }
  794. /**
  795. * hfi1_error_port_qps - put a port's RC/UC qps into error state
  796. * @ibp: the ibport.
  797. * @sl: the service level.
  798. *
  799. * This function places all RC/UC qps with a given service level into error
  800. * state. It is generally called to force upper lay apps to abandon stale qps
  801. * after an sl->sc mapping change.
  802. */
  803. void hfi1_error_port_qps(struct hfi1_ibport *ibp, u8 sl)
  804. {
  805. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  806. struct hfi1_ibdev *dev = &ppd->dd->verbs_dev;
  807. rvt_qp_iter(&dev->rdi, sl, hfi1_qp_iter_cb);
  808. }