qp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. /*
  77. * Convert the AETH credit code into the number of credits.
  78. */
  79. static const u16 credit_table[31] = {
  80. 0, /* 0 */
  81. 1, /* 1 */
  82. 2, /* 2 */
  83. 3, /* 3 */
  84. 4, /* 4 */
  85. 6, /* 5 */
  86. 8, /* 6 */
  87. 12, /* 7 */
  88. 16, /* 8 */
  89. 24, /* 9 */
  90. 32, /* A */
  91. 48, /* B */
  92. 64, /* C */
  93. 96, /* D */
  94. 128, /* E */
  95. 192, /* F */
  96. 256, /* 10 */
  97. 384, /* 11 */
  98. 512, /* 12 */
  99. 768, /* 13 */
  100. 1024, /* 14 */
  101. 1536, /* 15 */
  102. 2048, /* 16 */
  103. 3072, /* 17 */
  104. 4096, /* 18 */
  105. 6144, /* 19 */
  106. 8192, /* 1A */
  107. 12288, /* 1B */
  108. 16384, /* 1C */
  109. 24576, /* 1D */
  110. 32768 /* 1E */
  111. };
  112. const struct rvt_operation_params hfi1_post_parms[RVT_OPERATION_MAX] = {
  113. [IB_WR_RDMA_WRITE] = {
  114. .length = sizeof(struct ib_rdma_wr),
  115. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  116. },
  117. [IB_WR_RDMA_READ] = {
  118. .length = sizeof(struct ib_rdma_wr),
  119. .qpt_support = BIT(IB_QPT_RC),
  120. .flags = RVT_OPERATION_ATOMIC,
  121. },
  122. [IB_WR_ATOMIC_CMP_AND_SWP] = {
  123. .length = sizeof(struct ib_atomic_wr),
  124. .qpt_support = BIT(IB_QPT_RC),
  125. .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
  126. },
  127. [IB_WR_ATOMIC_FETCH_AND_ADD] = {
  128. .length = sizeof(struct ib_atomic_wr),
  129. .qpt_support = BIT(IB_QPT_RC),
  130. .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
  131. },
  132. [IB_WR_RDMA_WRITE_WITH_IMM] = {
  133. .length = sizeof(struct ib_rdma_wr),
  134. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  135. },
  136. [IB_WR_SEND] = {
  137. .length = sizeof(struct ib_send_wr),
  138. .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
  139. BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  140. },
  141. [IB_WR_SEND_WITH_IMM] = {
  142. .length = sizeof(struct ib_send_wr),
  143. .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
  144. BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  145. },
  146. [IB_WR_REG_MR] = {
  147. .length = sizeof(struct ib_reg_wr),
  148. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  149. .flags = RVT_OPERATION_LOCAL,
  150. },
  151. [IB_WR_LOCAL_INV] = {
  152. .length = sizeof(struct ib_send_wr),
  153. .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
  154. .flags = RVT_OPERATION_LOCAL,
  155. },
  156. [IB_WR_SEND_WITH_INV] = {
  157. .length = sizeof(struct ib_send_wr),
  158. .qpt_support = BIT(IB_QPT_RC),
  159. },
  160. };
  161. static void flush_tx_list(struct rvt_qp *qp)
  162. {
  163. struct hfi1_qp_priv *priv = qp->priv;
  164. while (!list_empty(&priv->s_iowait.tx_head)) {
  165. struct sdma_txreq *tx;
  166. tx = list_first_entry(
  167. &priv->s_iowait.tx_head,
  168. struct sdma_txreq,
  169. list);
  170. list_del_init(&tx->list);
  171. hfi1_put_txreq(
  172. container_of(tx, struct verbs_txreq, txreq));
  173. }
  174. }
  175. static void flush_iowait(struct rvt_qp *qp)
  176. {
  177. struct hfi1_qp_priv *priv = qp->priv;
  178. struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
  179. unsigned long flags;
  180. write_seqlock_irqsave(&dev->iowait_lock, flags);
  181. if (!list_empty(&priv->s_iowait.list)) {
  182. list_del_init(&priv->s_iowait.list);
  183. rvt_put_qp(qp);
  184. }
  185. write_sequnlock_irqrestore(&dev->iowait_lock, flags);
  186. }
  187. static inline int opa_mtu_enum_to_int(int mtu)
  188. {
  189. switch (mtu) {
  190. case OPA_MTU_8192: return 8192;
  191. case OPA_MTU_10240: return 10240;
  192. default: return -1;
  193. }
  194. }
  195. /**
  196. * This function is what we would push to the core layer if we wanted to be a
  197. * "first class citizen". Instead we hide this here and rely on Verbs ULPs
  198. * to blindly pass the MTU enum value from the PathRecord to us.
  199. */
  200. static inline int verbs_mtu_enum_to_int(struct ib_device *dev, enum ib_mtu mtu)
  201. {
  202. int val;
  203. /* Constraining 10KB packets to 8KB packets */
  204. if (mtu == (enum ib_mtu)OPA_MTU_10240)
  205. mtu = OPA_MTU_8192;
  206. val = opa_mtu_enum_to_int((int)mtu);
  207. if (val > 0)
  208. return val;
  209. return ib_mtu_enum_to_int(mtu);
  210. }
  211. int hfi1_check_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr,
  212. int attr_mask, struct ib_udata *udata)
  213. {
  214. struct ib_qp *ibqp = &qp->ibqp;
  215. struct hfi1_ibdev *dev = to_idev(ibqp->device);
  216. struct hfi1_devdata *dd = dd_from_dev(dev);
  217. u8 sc;
  218. if (attr_mask & IB_QP_AV) {
  219. sc = ah_to_sc(ibqp->device, &attr->ah_attr);
  220. if (sc == 0xf)
  221. return -EINVAL;
  222. if (!qp_to_sdma_engine(qp, sc) &&
  223. dd->flags & HFI1_HAS_SEND_DMA)
  224. return -EINVAL;
  225. if (!qp_to_send_context(qp, sc))
  226. return -EINVAL;
  227. }
  228. if (attr_mask & IB_QP_ALT_PATH) {
  229. sc = ah_to_sc(ibqp->device, &attr->alt_ah_attr);
  230. if (sc == 0xf)
  231. return -EINVAL;
  232. if (!qp_to_sdma_engine(qp, sc) &&
  233. dd->flags & HFI1_HAS_SEND_DMA)
  234. return -EINVAL;
  235. if (!qp_to_send_context(qp, sc))
  236. return -EINVAL;
  237. }
  238. return 0;
  239. }
  240. void hfi1_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr,
  241. int attr_mask, struct ib_udata *udata)
  242. {
  243. struct ib_qp *ibqp = &qp->ibqp;
  244. struct hfi1_qp_priv *priv = qp->priv;
  245. if (attr_mask & IB_QP_AV) {
  246. priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr);
  247. priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
  248. priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
  249. }
  250. if (attr_mask & IB_QP_PATH_MIG_STATE &&
  251. attr->path_mig_state == IB_MIG_MIGRATED &&
  252. qp->s_mig_state == IB_MIG_ARMED) {
  253. qp->s_flags |= RVT_S_AHG_CLEAR;
  254. priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr);
  255. priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
  256. priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
  257. }
  258. }
  259. /**
  260. * hfi1_check_send_wqe - validate wqe
  261. * @qp - The qp
  262. * @wqe - The built wqe
  263. *
  264. * validate wqe. This is called
  265. * prior to inserting the wqe into
  266. * the ring but after the wqe has been
  267. * setup.
  268. *
  269. * Returns 0 on success, -EINVAL on failure
  270. *
  271. */
  272. int hfi1_check_send_wqe(struct rvt_qp *qp,
  273. struct rvt_swqe *wqe)
  274. {
  275. struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  276. struct rvt_ah *ah;
  277. switch (qp->ibqp.qp_type) {
  278. case IB_QPT_RC:
  279. case IB_QPT_UC:
  280. if (wqe->length > 0x80000000U)
  281. return -EINVAL;
  282. break;
  283. case IB_QPT_SMI:
  284. ah = ibah_to_rvtah(wqe->ud_wr.ah);
  285. if (wqe->length > (1 << ah->log_pmtu))
  286. return -EINVAL;
  287. break;
  288. case IB_QPT_GSI:
  289. case IB_QPT_UD:
  290. ah = ibah_to_rvtah(wqe->ud_wr.ah);
  291. if (wqe->length > (1 << ah->log_pmtu))
  292. return -EINVAL;
  293. if (ibp->sl_to_sc[ah->attr.sl] == 0xf)
  294. return -EINVAL;
  295. default:
  296. break;
  297. }
  298. return wqe->length <= piothreshold;
  299. }
  300. /**
  301. * hfi1_compute_aeth - compute the AETH (syndrome + MSN)
  302. * @qp: the queue pair to compute the AETH for
  303. *
  304. * Returns the AETH.
  305. */
  306. __be32 hfi1_compute_aeth(struct rvt_qp *qp)
  307. {
  308. u32 aeth = qp->r_msn & HFI1_MSN_MASK;
  309. if (qp->ibqp.srq) {
  310. /*
  311. * Shared receive queues don't generate credits.
  312. * Set the credit field to the invalid value.
  313. */
  314. aeth |= HFI1_AETH_CREDIT_INVAL << HFI1_AETH_CREDIT_SHIFT;
  315. } else {
  316. u32 min, max, x;
  317. u32 credits;
  318. struct rvt_rwq *wq = qp->r_rq.wq;
  319. u32 head;
  320. u32 tail;
  321. /* sanity check pointers before trusting them */
  322. head = wq->head;
  323. if (head >= qp->r_rq.size)
  324. head = 0;
  325. tail = wq->tail;
  326. if (tail >= qp->r_rq.size)
  327. tail = 0;
  328. /*
  329. * Compute the number of credits available (RWQEs).
  330. * There is a small chance that the pair of reads are
  331. * not atomic, which is OK, since the fuzziness is
  332. * resolved as further ACKs go out.
  333. */
  334. credits = head - tail;
  335. if ((int)credits < 0)
  336. credits += qp->r_rq.size;
  337. /*
  338. * Binary search the credit table to find the code to
  339. * use.
  340. */
  341. min = 0;
  342. max = 31;
  343. for (;;) {
  344. x = (min + max) / 2;
  345. if (credit_table[x] == credits)
  346. break;
  347. if (credit_table[x] > credits) {
  348. max = x;
  349. } else {
  350. if (min == x)
  351. break;
  352. min = x;
  353. }
  354. }
  355. aeth |= x << HFI1_AETH_CREDIT_SHIFT;
  356. }
  357. return cpu_to_be32(aeth);
  358. }
  359. /**
  360. * _hfi1_schedule_send - schedule progress
  361. * @qp: the QP
  362. *
  363. * This schedules qp progress w/o regard to the s_flags.
  364. *
  365. * It is only used in the post send, which doesn't hold
  366. * the s_lock.
  367. */
  368. void _hfi1_schedule_send(struct rvt_qp *qp)
  369. {
  370. struct hfi1_qp_priv *priv = qp->priv;
  371. struct hfi1_ibport *ibp =
  372. to_iport(qp->ibqp.device, qp->port_num);
  373. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  374. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  375. iowait_schedule(&priv->s_iowait, ppd->hfi1_wq,
  376. priv->s_sde ?
  377. priv->s_sde->cpu :
  378. cpumask_first(cpumask_of_node(dd->node)));
  379. }
  380. static void qp_pio_drain(struct rvt_qp *qp)
  381. {
  382. struct hfi1_ibdev *dev;
  383. struct hfi1_qp_priv *priv = qp->priv;
  384. if (!priv->s_sendcontext)
  385. return;
  386. dev = to_idev(qp->ibqp.device);
  387. while (iowait_pio_pending(&priv->s_iowait)) {
  388. write_seqlock_irq(&dev->iowait_lock);
  389. hfi1_sc_wantpiobuf_intr(priv->s_sendcontext, 1);
  390. write_sequnlock_irq(&dev->iowait_lock);
  391. iowait_pio_drain(&priv->s_iowait);
  392. write_seqlock_irq(&dev->iowait_lock);
  393. hfi1_sc_wantpiobuf_intr(priv->s_sendcontext, 0);
  394. write_sequnlock_irq(&dev->iowait_lock);
  395. }
  396. }
  397. /**
  398. * hfi1_schedule_send - schedule progress
  399. * @qp: the QP
  400. *
  401. * This schedules qp progress and caller should hold
  402. * the s_lock.
  403. */
  404. void hfi1_schedule_send(struct rvt_qp *qp)
  405. {
  406. lockdep_assert_held(&qp->s_lock);
  407. if (hfi1_send_ok(qp))
  408. _hfi1_schedule_send(qp);
  409. }
  410. /**
  411. * hfi1_get_credit - handle credit in aeth
  412. * @qp: the qp
  413. * @aeth: the Acknowledge Extended Transport Header
  414. *
  415. * The QP s_lock should be held.
  416. */
  417. void hfi1_get_credit(struct rvt_qp *qp, u32 aeth)
  418. {
  419. u32 credit = (aeth >> HFI1_AETH_CREDIT_SHIFT) & HFI1_AETH_CREDIT_MASK;
  420. lockdep_assert_held(&qp->s_lock);
  421. /*
  422. * If the credit is invalid, we can send
  423. * as many packets as we like. Otherwise, we have to
  424. * honor the credit field.
  425. */
  426. if (credit == HFI1_AETH_CREDIT_INVAL) {
  427. if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) {
  428. qp->s_flags |= RVT_S_UNLIMITED_CREDIT;
  429. if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) {
  430. qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT;
  431. hfi1_schedule_send(qp);
  432. }
  433. }
  434. } else if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) {
  435. /* Compute new LSN (i.e., MSN + credit) */
  436. credit = (aeth + credit_table[credit]) & HFI1_MSN_MASK;
  437. if (cmp_msn(credit, qp->s_lsn) > 0) {
  438. qp->s_lsn = credit;
  439. if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) {
  440. qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT;
  441. hfi1_schedule_send(qp);
  442. }
  443. }
  444. }
  445. }
  446. void hfi1_qp_wakeup(struct rvt_qp *qp, u32 flag)
  447. {
  448. unsigned long flags;
  449. spin_lock_irqsave(&qp->s_lock, flags);
  450. if (qp->s_flags & flag) {
  451. qp->s_flags &= ~flag;
  452. trace_hfi1_qpwakeup(qp, flag);
  453. hfi1_schedule_send(qp);
  454. }
  455. spin_unlock_irqrestore(&qp->s_lock, flags);
  456. /* Notify hfi1_destroy_qp() if it is waiting. */
  457. rvt_put_qp(qp);
  458. }
  459. static int iowait_sleep(
  460. struct sdma_engine *sde,
  461. struct iowait *wait,
  462. struct sdma_txreq *stx,
  463. unsigned seq)
  464. {
  465. struct verbs_txreq *tx = container_of(stx, struct verbs_txreq, txreq);
  466. struct rvt_qp *qp;
  467. struct hfi1_qp_priv *priv;
  468. unsigned long flags;
  469. int ret = 0;
  470. struct hfi1_ibdev *dev;
  471. qp = tx->qp;
  472. priv = qp->priv;
  473. spin_lock_irqsave(&qp->s_lock, flags);
  474. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  475. /*
  476. * If we couldn't queue the DMA request, save the info
  477. * and try again later rather than destroying the
  478. * buffer and undoing the side effects of the copy.
  479. */
  480. /* Make a common routine? */
  481. dev = &sde->dd->verbs_dev;
  482. list_add_tail(&stx->list, &wait->tx_head);
  483. write_seqlock(&dev->iowait_lock);
  484. if (sdma_progress(sde, seq, stx))
  485. goto eagain;
  486. if (list_empty(&priv->s_iowait.list)) {
  487. struct hfi1_ibport *ibp =
  488. to_iport(qp->ibqp.device, qp->port_num);
  489. ibp->rvp.n_dmawait++;
  490. qp->s_flags |= RVT_S_WAIT_DMA_DESC;
  491. list_add_tail(&priv->s_iowait.list, &sde->dmawait);
  492. trace_hfi1_qpsleep(qp, RVT_S_WAIT_DMA_DESC);
  493. rvt_get_qp(qp);
  494. }
  495. write_sequnlock(&dev->iowait_lock);
  496. qp->s_flags &= ~RVT_S_BUSY;
  497. spin_unlock_irqrestore(&qp->s_lock, flags);
  498. ret = -EBUSY;
  499. } else {
  500. spin_unlock_irqrestore(&qp->s_lock, flags);
  501. hfi1_put_txreq(tx);
  502. }
  503. return ret;
  504. eagain:
  505. write_sequnlock(&dev->iowait_lock);
  506. spin_unlock_irqrestore(&qp->s_lock, flags);
  507. list_del_init(&stx->list);
  508. return -EAGAIN;
  509. }
  510. static void iowait_wakeup(struct iowait *wait, int reason)
  511. {
  512. struct rvt_qp *qp = iowait_to_qp(wait);
  513. WARN_ON(reason != SDMA_AVAIL_REASON);
  514. hfi1_qp_wakeup(qp, RVT_S_WAIT_DMA_DESC);
  515. }
  516. static void iowait_sdma_drained(struct iowait *wait)
  517. {
  518. struct rvt_qp *qp = iowait_to_qp(wait);
  519. unsigned long flags;
  520. /*
  521. * This happens when the send engine notes
  522. * a QP in the error state and cannot
  523. * do the flush work until that QP's
  524. * sdma work has finished.
  525. */
  526. spin_lock_irqsave(&qp->s_lock, flags);
  527. if (qp->s_flags & RVT_S_WAIT_DMA) {
  528. qp->s_flags &= ~RVT_S_WAIT_DMA;
  529. hfi1_schedule_send(qp);
  530. }
  531. spin_unlock_irqrestore(&qp->s_lock, flags);
  532. }
  533. /**
  534. *
  535. * qp_to_sdma_engine - map a qp to a send engine
  536. * @qp: the QP
  537. * @sc5: the 5 bit sc
  538. *
  539. * Return:
  540. * A send engine for the qp or NULL for SMI type qp.
  541. */
  542. struct sdma_engine *qp_to_sdma_engine(struct rvt_qp *qp, u8 sc5)
  543. {
  544. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  545. struct sdma_engine *sde;
  546. if (!(dd->flags & HFI1_HAS_SEND_DMA))
  547. return NULL;
  548. switch (qp->ibqp.qp_type) {
  549. case IB_QPT_SMI:
  550. return NULL;
  551. default:
  552. break;
  553. }
  554. sde = sdma_select_engine_sc(dd, qp->ibqp.qp_num >> dd->qos_shift, sc5);
  555. return sde;
  556. }
  557. /*
  558. * qp_to_send_context - map a qp to a send context
  559. * @qp: the QP
  560. * @sc5: the 5 bit sc
  561. *
  562. * Return:
  563. * A send context for the qp
  564. */
  565. struct send_context *qp_to_send_context(struct rvt_qp *qp, u8 sc5)
  566. {
  567. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  568. switch (qp->ibqp.qp_type) {
  569. case IB_QPT_SMI:
  570. /* SMA packets to VL15 */
  571. return dd->vld[15].sc;
  572. default:
  573. break;
  574. }
  575. return pio_select_send_context_sc(dd, qp->ibqp.qp_num >> dd->qos_shift,
  576. sc5);
  577. }
  578. struct qp_iter {
  579. struct hfi1_ibdev *dev;
  580. struct rvt_qp *qp;
  581. int specials;
  582. int n;
  583. };
  584. struct qp_iter *qp_iter_init(struct hfi1_ibdev *dev)
  585. {
  586. struct qp_iter *iter;
  587. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  588. if (!iter)
  589. return NULL;
  590. iter->dev = dev;
  591. iter->specials = dev->rdi.ibdev.phys_port_cnt * 2;
  592. return iter;
  593. }
  594. int qp_iter_next(struct qp_iter *iter)
  595. {
  596. struct hfi1_ibdev *dev = iter->dev;
  597. int n = iter->n;
  598. int ret = 1;
  599. struct rvt_qp *pqp = iter->qp;
  600. struct rvt_qp *qp;
  601. /*
  602. * The approach is to consider the special qps
  603. * as an additional table entries before the
  604. * real hash table. Since the qp code sets
  605. * the qp->next hash link to NULL, this works just fine.
  606. *
  607. * iter->specials is 2 * # ports
  608. *
  609. * n = 0..iter->specials is the special qp indices
  610. *
  611. * n = iter->specials..dev->rdi.qp_dev->qp_table_size+iter->specials are
  612. * the potential hash bucket entries
  613. *
  614. */
  615. for (; n < dev->rdi.qp_dev->qp_table_size + iter->specials; n++) {
  616. if (pqp) {
  617. qp = rcu_dereference(pqp->next);
  618. } else {
  619. if (n < iter->specials) {
  620. struct hfi1_pportdata *ppd;
  621. struct hfi1_ibport *ibp;
  622. int pidx;
  623. pidx = n % dev->rdi.ibdev.phys_port_cnt;
  624. ppd = &dd_from_dev(dev)->pport[pidx];
  625. ibp = &ppd->ibport_data;
  626. if (!(n & 1))
  627. qp = rcu_dereference(ibp->rvp.qp[0]);
  628. else
  629. qp = rcu_dereference(ibp->rvp.qp[1]);
  630. } else {
  631. qp = rcu_dereference(
  632. dev->rdi.qp_dev->qp_table[
  633. (n - iter->specials)]);
  634. }
  635. }
  636. pqp = qp;
  637. if (qp) {
  638. iter->qp = qp;
  639. iter->n = n;
  640. return 0;
  641. }
  642. }
  643. return ret;
  644. }
  645. static const char * const qp_type_str[] = {
  646. "SMI", "GSI", "RC", "UC", "UD",
  647. };
  648. static int qp_idle(struct rvt_qp *qp)
  649. {
  650. return
  651. qp->s_last == qp->s_acked &&
  652. qp->s_acked == qp->s_cur &&
  653. qp->s_cur == qp->s_tail &&
  654. qp->s_tail == qp->s_head;
  655. }
  656. void qp_iter_print(struct seq_file *s, struct qp_iter *iter)
  657. {
  658. struct rvt_swqe *wqe;
  659. struct rvt_qp *qp = iter->qp;
  660. struct hfi1_qp_priv *priv = qp->priv;
  661. struct sdma_engine *sde;
  662. struct send_context *send_context;
  663. sde = qp_to_sdma_engine(qp, priv->s_sc);
  664. wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  665. send_context = qp_to_send_context(qp, priv->s_sc);
  666. seq_printf(s,
  667. "N %d %s QP %x R %u %s %u %u %u f=%x %u %u %u %u %u %u PSN %x %x %x %x %x (%u %u %u %u %u %u %u) RQP %x LID %x SL %u MTU %u %u %u %u SDE %p,%u SC %p,%u SCQ %u %u PID %d\n",
  668. iter->n,
  669. qp_idle(qp) ? "I" : "B",
  670. qp->ibqp.qp_num,
  671. atomic_read(&qp->refcount),
  672. qp_type_str[qp->ibqp.qp_type],
  673. qp->state,
  674. wqe ? wqe->wr.opcode : 0,
  675. qp->s_hdrwords,
  676. qp->s_flags,
  677. iowait_sdma_pending(&priv->s_iowait),
  678. iowait_pio_pending(&priv->s_iowait),
  679. !list_empty(&priv->s_iowait.list),
  680. qp->timeout,
  681. wqe ? wqe->ssn : 0,
  682. qp->s_lsn,
  683. qp->s_last_psn,
  684. qp->s_psn, qp->s_next_psn,
  685. qp->s_sending_psn, qp->s_sending_hpsn,
  686. qp->s_last, qp->s_acked, qp->s_cur,
  687. qp->s_tail, qp->s_head, qp->s_size,
  688. qp->s_avail,
  689. qp->remote_qpn,
  690. qp->remote_ah_attr.dlid,
  691. qp->remote_ah_attr.sl,
  692. qp->pmtu,
  693. qp->s_retry,
  694. qp->s_retry_cnt,
  695. qp->s_rnr_retry_cnt,
  696. sde,
  697. sde ? sde->this_idx : 0,
  698. send_context,
  699. send_context ? send_context->sw_index : 0,
  700. ibcq_to_rvtcq(qp->ibqp.send_cq)->queue->head,
  701. ibcq_to_rvtcq(qp->ibqp.send_cq)->queue->tail,
  702. qp->pid);
  703. }
  704. void qp_comm_est(struct rvt_qp *qp)
  705. {
  706. qp->r_flags |= RVT_R_COMM_EST;
  707. if (qp->ibqp.event_handler) {
  708. struct ib_event ev;
  709. ev.device = qp->ibqp.device;
  710. ev.element.qp = &qp->ibqp;
  711. ev.event = IB_EVENT_COMM_EST;
  712. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  713. }
  714. }
  715. void *qp_priv_alloc(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  716. gfp_t gfp)
  717. {
  718. struct hfi1_qp_priv *priv;
  719. priv = kzalloc_node(sizeof(*priv), gfp, rdi->dparms.node);
  720. if (!priv)
  721. return ERR_PTR(-ENOMEM);
  722. priv->owner = qp;
  723. priv->s_ahg = kzalloc_node(sizeof(*priv->s_ahg), gfp,
  724. rdi->dparms.node);
  725. if (!priv->s_ahg) {
  726. kfree(priv);
  727. return ERR_PTR(-ENOMEM);
  728. }
  729. iowait_init(
  730. &priv->s_iowait,
  731. 1,
  732. _hfi1_do_send,
  733. iowait_sleep,
  734. iowait_wakeup,
  735. iowait_sdma_drained);
  736. setup_timer(&priv->s_rnr_timer, hfi1_rc_rnr_retry, (unsigned long)qp);
  737. qp->s_timer.function = hfi1_rc_timeout;
  738. return priv;
  739. }
  740. void qp_priv_free(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  741. {
  742. struct hfi1_qp_priv *priv = qp->priv;
  743. kfree(priv->s_ahg);
  744. kfree(priv);
  745. }
  746. unsigned free_all_qps(struct rvt_dev_info *rdi)
  747. {
  748. struct hfi1_ibdev *verbs_dev = container_of(rdi,
  749. struct hfi1_ibdev,
  750. rdi);
  751. struct hfi1_devdata *dd = container_of(verbs_dev,
  752. struct hfi1_devdata,
  753. verbs_dev);
  754. int n;
  755. unsigned qp_inuse = 0;
  756. for (n = 0; n < dd->num_pports; n++) {
  757. struct hfi1_ibport *ibp = &dd->pport[n].ibport_data;
  758. rcu_read_lock();
  759. if (rcu_dereference(ibp->rvp.qp[0]))
  760. qp_inuse++;
  761. if (rcu_dereference(ibp->rvp.qp[1]))
  762. qp_inuse++;
  763. rcu_read_unlock();
  764. }
  765. return qp_inuse;
  766. }
  767. void flush_qp_waiters(struct rvt_qp *qp)
  768. {
  769. lockdep_assert_held(&qp->s_lock);
  770. flush_iowait(qp);
  771. hfi1_stop_rc_timers(qp);
  772. }
  773. void stop_send_queue(struct rvt_qp *qp)
  774. {
  775. struct hfi1_qp_priv *priv = qp->priv;
  776. cancel_work_sync(&priv->s_iowait.iowork);
  777. hfi1_del_timers_sync(qp);
  778. }
  779. void quiesce_qp(struct rvt_qp *qp)
  780. {
  781. struct hfi1_qp_priv *priv = qp->priv;
  782. iowait_sdma_drain(&priv->s_iowait);
  783. qp_pio_drain(qp);
  784. flush_tx_list(qp);
  785. }
  786. void notify_qp_reset(struct rvt_qp *qp)
  787. {
  788. struct hfi1_qp_priv *priv = qp->priv;
  789. priv->r_adefered = 0;
  790. clear_ahg(qp);
  791. }
  792. /*
  793. * Switch to alternate path.
  794. * The QP s_lock should be held and interrupts disabled.
  795. */
  796. void hfi1_migrate_qp(struct rvt_qp *qp)
  797. {
  798. struct hfi1_qp_priv *priv = qp->priv;
  799. struct ib_event ev;
  800. qp->s_mig_state = IB_MIG_MIGRATED;
  801. qp->remote_ah_attr = qp->alt_ah_attr;
  802. qp->port_num = qp->alt_ah_attr.port_num;
  803. qp->s_pkey_index = qp->s_alt_pkey_index;
  804. qp->s_flags |= RVT_S_AHG_CLEAR;
  805. priv->s_sc = ah_to_sc(qp->ibqp.device, &qp->remote_ah_attr);
  806. priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
  807. ev.device = qp->ibqp.device;
  808. ev.element.qp = &qp->ibqp;
  809. ev.event = IB_EVENT_PATH_MIG;
  810. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  811. }
  812. int mtu_to_path_mtu(u32 mtu)
  813. {
  814. return mtu_to_enum(mtu, OPA_MTU_8192);
  815. }
  816. u32 mtu_from_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, u32 pmtu)
  817. {
  818. u32 mtu;
  819. struct hfi1_ibdev *verbs_dev = container_of(rdi,
  820. struct hfi1_ibdev,
  821. rdi);
  822. struct hfi1_devdata *dd = container_of(verbs_dev,
  823. struct hfi1_devdata,
  824. verbs_dev);
  825. struct hfi1_ibport *ibp;
  826. u8 sc, vl;
  827. ibp = &dd->pport[qp->port_num - 1].ibport_data;
  828. sc = ibp->sl_to_sc[qp->remote_ah_attr.sl];
  829. vl = sc_to_vlt(dd, sc);
  830. mtu = verbs_mtu_enum_to_int(qp->ibqp.device, pmtu);
  831. if (vl < PER_VL_SEND_CONTEXTS)
  832. mtu = min_t(u32, mtu, dd->vld[vl].mtu);
  833. return mtu;
  834. }
  835. int get_pmtu_from_attr(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  836. struct ib_qp_attr *attr)
  837. {
  838. int mtu, pidx = qp->port_num - 1;
  839. struct hfi1_ibdev *verbs_dev = container_of(rdi,
  840. struct hfi1_ibdev,
  841. rdi);
  842. struct hfi1_devdata *dd = container_of(verbs_dev,
  843. struct hfi1_devdata,
  844. verbs_dev);
  845. mtu = verbs_mtu_enum_to_int(qp->ibqp.device, attr->path_mtu);
  846. if (mtu == -1)
  847. return -1; /* values less than 0 are error */
  848. if (mtu > dd->pport[pidx].ibmtu)
  849. return mtu_to_enum(dd->pport[pidx].ibmtu, IB_MTU_2048);
  850. else
  851. return attr->path_mtu;
  852. }
  853. void notify_error_qp(struct rvt_qp *qp)
  854. {
  855. struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
  856. struct hfi1_qp_priv *priv = qp->priv;
  857. write_seqlock(&dev->iowait_lock);
  858. if (!list_empty(&priv->s_iowait.list) && !(qp->s_flags & RVT_S_BUSY)) {
  859. qp->s_flags &= ~RVT_S_ANY_WAIT_IO;
  860. list_del_init(&priv->s_iowait.list);
  861. rvt_put_qp(qp);
  862. }
  863. write_sequnlock(&dev->iowait_lock);
  864. if (!(qp->s_flags & RVT_S_BUSY)) {
  865. qp->s_hdrwords = 0;
  866. if (qp->s_rdma_mr) {
  867. rvt_put_mr(qp->s_rdma_mr);
  868. qp->s_rdma_mr = NULL;
  869. }
  870. flush_tx_list(qp);
  871. }
  872. }
  873. /**
  874. * hfi1_error_port_qps - put a port's RC/UC qps into error state
  875. * @ibp: the ibport.
  876. * @sl: the service level.
  877. *
  878. * This function places all RC/UC qps with a given service level into error
  879. * state. It is generally called to force upper lay apps to abandon stale qps
  880. * after an sl->sc mapping change.
  881. */
  882. void hfi1_error_port_qps(struct hfi1_ibport *ibp, u8 sl)
  883. {
  884. struct rvt_qp *qp = NULL;
  885. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  886. struct hfi1_ibdev *dev = &ppd->dd->verbs_dev;
  887. int n;
  888. int lastwqe;
  889. struct ib_event ev;
  890. rcu_read_lock();
  891. /* Deal only with RC/UC qps that use the given SL. */
  892. for (n = 0; n < dev->rdi.qp_dev->qp_table_size; n++) {
  893. for (qp = rcu_dereference(dev->rdi.qp_dev->qp_table[n]); qp;
  894. qp = rcu_dereference(qp->next)) {
  895. if (qp->port_num == ppd->port &&
  896. (qp->ibqp.qp_type == IB_QPT_UC ||
  897. qp->ibqp.qp_type == IB_QPT_RC) &&
  898. qp->remote_ah_attr.sl == sl &&
  899. (ib_rvt_state_ops[qp->state] &
  900. RVT_POST_SEND_OK)) {
  901. spin_lock_irq(&qp->r_lock);
  902. spin_lock(&qp->s_hlock);
  903. spin_lock(&qp->s_lock);
  904. lastwqe = rvt_error_qp(qp,
  905. IB_WC_WR_FLUSH_ERR);
  906. spin_unlock(&qp->s_lock);
  907. spin_unlock(&qp->s_hlock);
  908. spin_unlock_irq(&qp->r_lock);
  909. if (lastwqe) {
  910. ev.device = qp->ibqp.device;
  911. ev.element.qp = &qp->ibqp;
  912. ev.event =
  913. IB_EVENT_QP_LAST_WQE_REACHED;
  914. qp->ibqp.event_handler(&ev,
  915. qp->ibqp.qp_context);
  916. }
  917. }
  918. }
  919. }
  920. rcu_read_unlock();
  921. }