qp.c 26 KB

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