ruc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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/spinlock.h>
  48. #include "hfi.h"
  49. #include "mad.h"
  50. #include "qp.h"
  51. #include "verbs_txreq.h"
  52. #include "trace.h"
  53. static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
  54. {
  55. return (gid->global.interface_id == id &&
  56. (gid->global.subnet_prefix == gid_prefix ||
  57. gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
  58. }
  59. /*
  60. *
  61. * This should be called with the QP r_lock held.
  62. *
  63. * The s_lock will be acquired around the hfi1_migrate_qp() call.
  64. */
  65. int hfi1_ruc_check_hdr(struct hfi1_ibport *ibp, struct hfi1_packet *packet)
  66. {
  67. __be64 guid;
  68. unsigned long flags;
  69. struct rvt_qp *qp = packet->qp;
  70. u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(&qp->remote_ah_attr)];
  71. u32 dlid = packet->dlid;
  72. u32 slid = packet->slid;
  73. u32 sl = packet->sl;
  74. bool migrated = packet->migrated;
  75. u16 pkey = packet->pkey;
  76. if (qp->s_mig_state == IB_MIG_ARMED && migrated) {
  77. if (!packet->grh) {
  78. if ((rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
  79. IB_AH_GRH) &&
  80. (packet->etype != RHF_RCV_TYPE_BYPASS))
  81. return 1;
  82. } else {
  83. const struct ib_global_route *grh;
  84. if (!(rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
  85. IB_AH_GRH))
  86. return 1;
  87. grh = rdma_ah_read_grh(&qp->alt_ah_attr);
  88. guid = get_sguid(ibp, grh->sgid_index);
  89. if (!gid_ok(&packet->grh->dgid, ibp->rvp.gid_prefix,
  90. guid))
  91. return 1;
  92. if (!gid_ok(
  93. &packet->grh->sgid,
  94. grh->dgid.global.subnet_prefix,
  95. grh->dgid.global.interface_id))
  96. return 1;
  97. }
  98. if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), pkey,
  99. sc5, slid))) {
  100. hfi1_bad_pkey(ibp, pkey, sl, 0, qp->ibqp.qp_num,
  101. slid, dlid);
  102. return 1;
  103. }
  104. /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
  105. if (slid != rdma_ah_get_dlid(&qp->alt_ah_attr) ||
  106. ppd_from_ibp(ibp)->port !=
  107. rdma_ah_get_port_num(&qp->alt_ah_attr))
  108. return 1;
  109. spin_lock_irqsave(&qp->s_lock, flags);
  110. hfi1_migrate_qp(qp);
  111. spin_unlock_irqrestore(&qp->s_lock, flags);
  112. } else {
  113. if (!packet->grh) {
  114. if ((rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
  115. IB_AH_GRH) &&
  116. (packet->etype != RHF_RCV_TYPE_BYPASS))
  117. return 1;
  118. } else {
  119. const struct ib_global_route *grh;
  120. if (!(rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
  121. IB_AH_GRH))
  122. return 1;
  123. grh = rdma_ah_read_grh(&qp->remote_ah_attr);
  124. guid = get_sguid(ibp, grh->sgid_index);
  125. if (!gid_ok(&packet->grh->dgid, ibp->rvp.gid_prefix,
  126. guid))
  127. return 1;
  128. if (!gid_ok(
  129. &packet->grh->sgid,
  130. grh->dgid.global.subnet_prefix,
  131. grh->dgid.global.interface_id))
  132. return 1;
  133. }
  134. if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), pkey,
  135. sc5, slid))) {
  136. hfi1_bad_pkey(ibp, pkey, sl, 0, qp->ibqp.qp_num,
  137. slid, dlid);
  138. return 1;
  139. }
  140. /* Validate the SLID. See Ch. 9.6.1.5 */
  141. if ((slid != rdma_ah_get_dlid(&qp->remote_ah_attr)) ||
  142. ppd_from_ibp(ibp)->port != qp->port_num)
  143. return 1;
  144. if (qp->s_mig_state == IB_MIG_REARM && !migrated)
  145. qp->s_mig_state = IB_MIG_ARMED;
  146. }
  147. return 0;
  148. }
  149. /**
  150. * hfi1_make_grh - construct a GRH header
  151. * @ibp: a pointer to the IB port
  152. * @hdr: a pointer to the GRH header being constructed
  153. * @grh: the global route address to send to
  154. * @hwords: size of header after grh being sent in dwords
  155. * @nwords: the number of 32 bit words of data being sent
  156. *
  157. * Return the size of the header in 32 bit words.
  158. */
  159. u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
  160. const struct ib_global_route *grh, u32 hwords, u32 nwords)
  161. {
  162. hdr->version_tclass_flow =
  163. cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
  164. (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
  165. (grh->flow_label << IB_GRH_FLOW_SHIFT));
  166. hdr->paylen = cpu_to_be16((hwords + nwords) << 2);
  167. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  168. hdr->next_hdr = IB_GRH_NEXT_HDR;
  169. hdr->hop_limit = grh->hop_limit;
  170. /* The SGID is 32-bit aligned. */
  171. hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
  172. hdr->sgid.global.interface_id =
  173. grh->sgid_index < HFI1_GUIDS_PER_PORT ?
  174. get_sguid(ibp, grh->sgid_index) :
  175. get_sguid(ibp, HFI1_PORT_GUID_INDEX);
  176. hdr->dgid = grh->dgid;
  177. /* GRH header size in 32-bit words. */
  178. return sizeof(struct ib_grh) / sizeof(u32);
  179. }
  180. #define BTH2_OFFSET (offsetof(struct hfi1_sdma_header, \
  181. hdr.ibh.u.oth.bth[2]) / 4)
  182. /**
  183. * build_ahg - create ahg in s_ahg
  184. * @qp: a pointer to QP
  185. * @npsn: the next PSN for the request/response
  186. *
  187. * This routine handles the AHG by allocating an ahg entry and causing the
  188. * copy of the first middle.
  189. *
  190. * Subsequent middles use the copied entry, editing the
  191. * PSN with 1 or 2 edits.
  192. */
  193. static inline void build_ahg(struct rvt_qp *qp, u32 npsn)
  194. {
  195. struct hfi1_qp_priv *priv = qp->priv;
  196. if (unlikely(qp->s_flags & HFI1_S_AHG_CLEAR))
  197. clear_ahg(qp);
  198. if (!(qp->s_flags & HFI1_S_AHG_VALID)) {
  199. /* first middle that needs copy */
  200. if (qp->s_ahgidx < 0)
  201. qp->s_ahgidx = sdma_ahg_alloc(priv->s_sde);
  202. if (qp->s_ahgidx >= 0) {
  203. qp->s_ahgpsn = npsn;
  204. priv->s_ahg->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
  205. /* save to protect a change in another thread */
  206. priv->s_ahg->ahgidx = qp->s_ahgidx;
  207. qp->s_flags |= HFI1_S_AHG_VALID;
  208. }
  209. } else {
  210. /* subsequent middle after valid */
  211. if (qp->s_ahgidx >= 0) {
  212. priv->s_ahg->tx_flags |= SDMA_TXREQ_F_USE_AHG;
  213. priv->s_ahg->ahgidx = qp->s_ahgidx;
  214. priv->s_ahg->ahgcount++;
  215. priv->s_ahg->ahgdesc[0] =
  216. sdma_build_ahg_descriptor(
  217. (__force u16)cpu_to_be16((u16)npsn),
  218. BTH2_OFFSET,
  219. 16,
  220. 16);
  221. if ((npsn & 0xffff0000) !=
  222. (qp->s_ahgpsn & 0xffff0000)) {
  223. priv->s_ahg->ahgcount++;
  224. priv->s_ahg->ahgdesc[1] =
  225. sdma_build_ahg_descriptor(
  226. (__force u16)cpu_to_be16(
  227. (u16)(npsn >> 16)),
  228. BTH2_OFFSET,
  229. 0,
  230. 16);
  231. }
  232. }
  233. }
  234. }
  235. static inline void hfi1_make_ruc_bth(struct rvt_qp *qp,
  236. struct ib_other_headers *ohdr,
  237. u32 bth0, u32 bth1, u32 bth2)
  238. {
  239. bth1 |= qp->remote_qpn;
  240. ohdr->bth[0] = cpu_to_be32(bth0);
  241. ohdr->bth[1] = cpu_to_be32(bth1);
  242. ohdr->bth[2] = cpu_to_be32(bth2);
  243. }
  244. /**
  245. * hfi1_make_ruc_header_16B - build a 16B header
  246. * @qp: the queue pair
  247. * @ohdr: a pointer to the destination header memory
  248. * @bth0: bth0 passed in from the RC/UC builder
  249. * @bth2: bth2 passed in from the RC/UC builder
  250. * @middle: non zero implies indicates ahg "could" be used
  251. * @ps: the current packet state
  252. *
  253. * This routine may disarm ahg under these situations:
  254. * - packet needs a GRH
  255. * - BECN needed
  256. * - migration state not IB_MIG_MIGRATED
  257. */
  258. static inline void hfi1_make_ruc_header_16B(struct rvt_qp *qp,
  259. struct ib_other_headers *ohdr,
  260. u32 bth0, u32 bth2, int middle,
  261. struct hfi1_pkt_state *ps)
  262. {
  263. struct hfi1_qp_priv *priv = qp->priv;
  264. struct hfi1_ibport *ibp = ps->ibp;
  265. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  266. u32 bth1 = 0;
  267. u32 slid;
  268. u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
  269. u8 l4 = OPA_16B_L4_IB_LOCAL;
  270. u8 extra_bytes = hfi1_get_16b_padding(
  271. (ps->s_txreq->hdr_dwords << 2),
  272. ps->s_txreq->s_cur_size);
  273. u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
  274. extra_bytes + SIZE_OF_LT) >> 2);
  275. bool becn = false;
  276. if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH) &&
  277. hfi1_check_mcast(rdma_ah_get_dlid(&qp->remote_ah_attr))) {
  278. struct ib_grh *grh;
  279. struct ib_global_route *grd =
  280. rdma_ah_retrieve_grh(&qp->remote_ah_attr);
  281. /*
  282. * Ensure OPA GIDs are transformed to IB gids
  283. * before creating the GRH.
  284. */
  285. if (grd->sgid_index == OPA_GID_INDEX)
  286. grd->sgid_index = 0;
  287. grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
  288. l4 = OPA_16B_L4_IB_GLOBAL;
  289. ps->s_txreq->hdr_dwords +=
  290. hfi1_make_grh(ibp, grh, grd,
  291. ps->s_txreq->hdr_dwords - LRH_16B_DWORDS,
  292. nwords);
  293. middle = 0;
  294. }
  295. if (qp->s_mig_state == IB_MIG_MIGRATED)
  296. bth1 |= OPA_BTH_MIG_REQ;
  297. else
  298. middle = 0;
  299. if (qp->s_flags & RVT_S_ECN) {
  300. qp->s_flags &= ~RVT_S_ECN;
  301. /* we recently received a FECN, so return a BECN */
  302. becn = true;
  303. middle = 0;
  304. }
  305. if (middle)
  306. build_ahg(qp, bth2);
  307. else
  308. qp->s_flags &= ~HFI1_S_AHG_VALID;
  309. bth0 |= pkey;
  310. bth0 |= extra_bytes << 20;
  311. hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
  312. if (!ppd->lid)
  313. slid = be32_to_cpu(OPA_LID_PERMISSIVE);
  314. else
  315. slid = ppd->lid |
  316. (rdma_ah_get_path_bits(&qp->remote_ah_attr) &
  317. ((1 << ppd->lmc) - 1));
  318. hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
  319. slid,
  320. opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr),
  321. 16B),
  322. (ps->s_txreq->hdr_dwords + nwords) >> 1,
  323. pkey, becn, 0, l4, priv->s_sc);
  324. }
  325. /**
  326. * hfi1_make_ruc_header_9B - build a 9B header
  327. * @qp: the queue pair
  328. * @ohdr: a pointer to the destination header memory
  329. * @bth0: bth0 passed in from the RC/UC builder
  330. * @bth2: bth2 passed in from the RC/UC builder
  331. * @middle: non zero implies indicates ahg "could" be used
  332. * @ps: the current packet state
  333. *
  334. * This routine may disarm ahg under these situations:
  335. * - packet needs a GRH
  336. * - BECN needed
  337. * - migration state not IB_MIG_MIGRATED
  338. */
  339. static inline void hfi1_make_ruc_header_9B(struct rvt_qp *qp,
  340. struct ib_other_headers *ohdr,
  341. u32 bth0, u32 bth2, int middle,
  342. struct hfi1_pkt_state *ps)
  343. {
  344. struct hfi1_qp_priv *priv = qp->priv;
  345. struct hfi1_ibport *ibp = ps->ibp;
  346. u32 bth1 = 0;
  347. u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
  348. u16 lrh0 = HFI1_LRH_BTH;
  349. u8 extra_bytes = -ps->s_txreq->s_cur_size & 3;
  350. u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
  351. extra_bytes) >> 2);
  352. if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
  353. struct ib_grh *grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
  354. lrh0 = HFI1_LRH_GRH;
  355. ps->s_txreq->hdr_dwords +=
  356. hfi1_make_grh(ibp, grh,
  357. rdma_ah_read_grh(&qp->remote_ah_attr),
  358. ps->s_txreq->hdr_dwords - LRH_9B_DWORDS,
  359. nwords);
  360. middle = 0;
  361. }
  362. lrh0 |= (priv->s_sc & 0xf) << 12 |
  363. (rdma_ah_get_sl(&qp->remote_ah_attr) & 0xf) << 4;
  364. if (qp->s_mig_state == IB_MIG_MIGRATED)
  365. bth0 |= IB_BTH_MIG_REQ;
  366. else
  367. middle = 0;
  368. if (qp->s_flags & RVT_S_ECN) {
  369. qp->s_flags &= ~RVT_S_ECN;
  370. /* we recently received a FECN, so return a BECN */
  371. bth1 |= (IB_BECN_MASK << IB_BECN_SHIFT);
  372. middle = 0;
  373. }
  374. if (middle)
  375. build_ahg(qp, bth2);
  376. else
  377. qp->s_flags &= ~HFI1_S_AHG_VALID;
  378. bth0 |= pkey;
  379. bth0 |= extra_bytes << 20;
  380. hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
  381. hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
  382. lrh0,
  383. ps->s_txreq->hdr_dwords + nwords,
  384. opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr), 9B),
  385. ppd_from_ibp(ibp)->lid |
  386. rdma_ah_get_path_bits(&qp->remote_ah_attr));
  387. }
  388. typedef void (*hfi1_make_ruc_hdr)(struct rvt_qp *qp,
  389. struct ib_other_headers *ohdr,
  390. u32 bth0, u32 bth2, int middle,
  391. struct hfi1_pkt_state *ps);
  392. /* We support only two types - 9B and 16B for now */
  393. static const hfi1_make_ruc_hdr hfi1_ruc_header_tbl[2] = {
  394. [HFI1_PKT_TYPE_9B] = &hfi1_make_ruc_header_9B,
  395. [HFI1_PKT_TYPE_16B] = &hfi1_make_ruc_header_16B
  396. };
  397. void hfi1_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
  398. u32 bth0, u32 bth2, int middle,
  399. struct hfi1_pkt_state *ps)
  400. {
  401. struct hfi1_qp_priv *priv = qp->priv;
  402. /*
  403. * reset s_ahg/AHG fields
  404. *
  405. * This insures that the ahgentry/ahgcount
  406. * are at a non-AHG default to protect
  407. * build_verbs_tx_desc() from using
  408. * an include ahgidx.
  409. *
  410. * build_ahg() will modify as appropriate
  411. * to use the AHG feature.
  412. */
  413. priv->s_ahg->tx_flags = 0;
  414. priv->s_ahg->ahgcount = 0;
  415. priv->s_ahg->ahgidx = 0;
  416. /* Make the appropriate header */
  417. hfi1_ruc_header_tbl[priv->hdr_type](qp, ohdr, bth0, bth2, middle, ps);
  418. }
  419. /* when sending, force a reschedule every one of these periods */
  420. #define SEND_RESCHED_TIMEOUT (5 * HZ) /* 5s in jiffies */
  421. /**
  422. * schedule_send_yield - test for a yield required for QP send engine
  423. * @timeout: Final time for timeout slice for jiffies
  424. * @qp: a pointer to QP
  425. * @ps: a pointer to a structure with commonly lookup values for
  426. * the the send engine progress
  427. *
  428. * This routine checks if the time slice for the QP has expired
  429. * for RC QPs, if so an additional work entry is queued. At this
  430. * point, other QPs have an opportunity to be scheduled. It
  431. * returns true if a yield is required, otherwise, false
  432. * is returned.
  433. */
  434. static bool schedule_send_yield(struct rvt_qp *qp,
  435. struct hfi1_pkt_state *ps)
  436. {
  437. ps->pkts_sent = true;
  438. if (unlikely(time_after(jiffies, ps->timeout))) {
  439. if (!ps->in_thread ||
  440. workqueue_congested(ps->cpu, ps->ppd->hfi1_wq)) {
  441. spin_lock_irqsave(&qp->s_lock, ps->flags);
  442. qp->s_flags &= ~RVT_S_BUSY;
  443. hfi1_schedule_send(qp);
  444. spin_unlock_irqrestore(&qp->s_lock, ps->flags);
  445. this_cpu_inc(*ps->ppd->dd->send_schedule);
  446. trace_hfi1_rc_expired_time_slice(qp, true);
  447. return true;
  448. }
  449. cond_resched();
  450. this_cpu_inc(*ps->ppd->dd->send_schedule);
  451. ps->timeout = jiffies + ps->timeout_int;
  452. }
  453. trace_hfi1_rc_expired_time_slice(qp, false);
  454. return false;
  455. }
  456. void hfi1_do_send_from_rvt(struct rvt_qp *qp)
  457. {
  458. hfi1_do_send(qp, false);
  459. }
  460. void _hfi1_do_send(struct work_struct *work)
  461. {
  462. struct iowait_work *w = container_of(work, struct iowait_work, iowork);
  463. struct rvt_qp *qp = iowait_to_qp(w->iow);
  464. hfi1_do_send(qp, true);
  465. }
  466. /**
  467. * hfi1_do_send - perform a send on a QP
  468. * @work: contains a pointer to the QP
  469. * @in_thread: true if in a workqueue thread
  470. *
  471. * Process entries in the send work queue until credit or queue is
  472. * exhausted. Only allow one CPU to send a packet per QP.
  473. * Otherwise, two threads could send packets out of order.
  474. */
  475. void hfi1_do_send(struct rvt_qp *qp, bool in_thread)
  476. {
  477. struct hfi1_pkt_state ps;
  478. struct hfi1_qp_priv *priv = qp->priv;
  479. int (*make_req)(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
  480. ps.dev = to_idev(qp->ibqp.device);
  481. ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
  482. ps.ppd = ppd_from_ibp(ps.ibp);
  483. ps.in_thread = in_thread;
  484. ps.wait = iowait_get_ib_work(&priv->s_iowait);
  485. trace_hfi1_rc_do_send(qp, in_thread);
  486. switch (qp->ibqp.qp_type) {
  487. case IB_QPT_RC:
  488. if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
  489. ~((1 << ps.ppd->lmc) - 1)) ==
  490. ps.ppd->lid)) {
  491. rvt_ruc_loopback(qp);
  492. return;
  493. }
  494. make_req = hfi1_make_rc_req;
  495. ps.timeout_int = qp->timeout_jiffies;
  496. break;
  497. case IB_QPT_UC:
  498. if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
  499. ~((1 << ps.ppd->lmc) - 1)) ==
  500. ps.ppd->lid)) {
  501. rvt_ruc_loopback(qp);
  502. return;
  503. }
  504. make_req = hfi1_make_uc_req;
  505. ps.timeout_int = SEND_RESCHED_TIMEOUT;
  506. break;
  507. default:
  508. make_req = hfi1_make_ud_req;
  509. ps.timeout_int = SEND_RESCHED_TIMEOUT;
  510. }
  511. spin_lock_irqsave(&qp->s_lock, ps.flags);
  512. /* Return if we are already busy processing a work request. */
  513. if (!hfi1_send_ok(qp)) {
  514. if (qp->s_flags & HFI1_S_ANY_WAIT_IO)
  515. iowait_set_flag(&priv->s_iowait, IOWAIT_PENDING_IB);
  516. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  517. return;
  518. }
  519. qp->s_flags |= RVT_S_BUSY;
  520. ps.timeout_int = ps.timeout_int / 8;
  521. ps.timeout = jiffies + ps.timeout_int;
  522. ps.cpu = priv->s_sde ? priv->s_sde->cpu :
  523. cpumask_first(cpumask_of_node(ps.ppd->dd->node));
  524. ps.pkts_sent = false;
  525. /* insure a pre-built packet is handled */
  526. ps.s_txreq = get_waiting_verbs_txreq(ps.wait);
  527. do {
  528. /* Check for a constructed packet to be sent. */
  529. if (ps.s_txreq) {
  530. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  531. /*
  532. * If the packet cannot be sent now, return and
  533. * the send engine will be woken up later.
  534. */
  535. if (hfi1_verbs_send(qp, &ps))
  536. return;
  537. /* allow other tasks to run */
  538. if (schedule_send_yield(qp, &ps))
  539. return;
  540. spin_lock_irqsave(&qp->s_lock, ps.flags);
  541. }
  542. } while (make_req(qp, &ps));
  543. iowait_starve_clear(ps.pkts_sent, &priv->s_iowait);
  544. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  545. }