ruc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. * ruc_loopback - handle UC and RC loopback requests
  151. * @sqp: the sending QP
  152. *
  153. * This is called from hfi1_do_send() to
  154. * forward a WQE addressed to the same HFI.
  155. * Note that although we are single threaded due to the send engine, we still
  156. * have to protect against post_send(). We don't have to worry about
  157. * receive interrupts since this is a connected protocol and all packets
  158. * will pass through here.
  159. */
  160. static void ruc_loopback(struct rvt_qp *sqp)
  161. {
  162. struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
  163. struct rvt_qp *qp;
  164. struct rvt_swqe *wqe;
  165. struct rvt_sge *sge;
  166. unsigned long flags;
  167. struct ib_wc wc;
  168. u64 sdata;
  169. atomic64_t *maddr;
  170. enum ib_wc_status send_status;
  171. bool release;
  172. int ret;
  173. bool copy_last = false;
  174. int local_ops = 0;
  175. rcu_read_lock();
  176. /*
  177. * Note that we check the responder QP state after
  178. * checking the requester's state.
  179. */
  180. qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
  181. sqp->remote_qpn);
  182. spin_lock_irqsave(&sqp->s_lock, flags);
  183. /* Return if we are already busy processing a work request. */
  184. if ((sqp->s_flags & (RVT_S_BUSY | HFI1_S_ANY_WAIT)) ||
  185. !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
  186. goto unlock;
  187. sqp->s_flags |= RVT_S_BUSY;
  188. again:
  189. if (sqp->s_last == READ_ONCE(sqp->s_head))
  190. goto clr_busy;
  191. wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
  192. /* Return if it is not OK to start a new work request. */
  193. if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
  194. if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
  195. goto clr_busy;
  196. /* We are in the error state, flush the work request. */
  197. send_status = IB_WC_WR_FLUSH_ERR;
  198. goto flush_send;
  199. }
  200. /*
  201. * We can rely on the entry not changing without the s_lock
  202. * being held until we update s_last.
  203. * We increment s_cur to indicate s_last is in progress.
  204. */
  205. if (sqp->s_last == sqp->s_cur) {
  206. if (++sqp->s_cur >= sqp->s_size)
  207. sqp->s_cur = 0;
  208. }
  209. spin_unlock_irqrestore(&sqp->s_lock, flags);
  210. if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
  211. qp->ibqp.qp_type != sqp->ibqp.qp_type) {
  212. ibp->rvp.n_pkt_drops++;
  213. /*
  214. * For RC, the requester would timeout and retry so
  215. * shortcut the timeouts and just signal too many retries.
  216. */
  217. if (sqp->ibqp.qp_type == IB_QPT_RC)
  218. send_status = IB_WC_RETRY_EXC_ERR;
  219. else
  220. send_status = IB_WC_SUCCESS;
  221. goto serr;
  222. }
  223. memset(&wc, 0, sizeof(wc));
  224. send_status = IB_WC_SUCCESS;
  225. release = true;
  226. sqp->s_sge.sge = wqe->sg_list[0];
  227. sqp->s_sge.sg_list = wqe->sg_list + 1;
  228. sqp->s_sge.num_sge = wqe->wr.num_sge;
  229. sqp->s_len = wqe->length;
  230. switch (wqe->wr.opcode) {
  231. case IB_WR_REG_MR:
  232. goto send_comp;
  233. case IB_WR_LOCAL_INV:
  234. if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
  235. if (rvt_invalidate_rkey(sqp,
  236. wqe->wr.ex.invalidate_rkey))
  237. send_status = IB_WC_LOC_PROT_ERR;
  238. local_ops = 1;
  239. }
  240. goto send_comp;
  241. case IB_WR_SEND_WITH_INV:
  242. if (!rvt_invalidate_rkey(qp, wqe->wr.ex.invalidate_rkey)) {
  243. wc.wc_flags = IB_WC_WITH_INVALIDATE;
  244. wc.ex.invalidate_rkey = wqe->wr.ex.invalidate_rkey;
  245. }
  246. goto send;
  247. case IB_WR_SEND_WITH_IMM:
  248. wc.wc_flags = IB_WC_WITH_IMM;
  249. wc.ex.imm_data = wqe->wr.ex.imm_data;
  250. /* FALLTHROUGH */
  251. case IB_WR_SEND:
  252. send:
  253. ret = rvt_get_rwqe(qp, false);
  254. if (ret < 0)
  255. goto op_err;
  256. if (!ret)
  257. goto rnr_nak;
  258. break;
  259. case IB_WR_RDMA_WRITE_WITH_IMM:
  260. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
  261. goto inv_err;
  262. wc.wc_flags = IB_WC_WITH_IMM;
  263. wc.ex.imm_data = wqe->wr.ex.imm_data;
  264. ret = rvt_get_rwqe(qp, true);
  265. if (ret < 0)
  266. goto op_err;
  267. if (!ret)
  268. goto rnr_nak;
  269. /* skip copy_last set and qp_access_flags recheck */
  270. goto do_write;
  271. case IB_WR_RDMA_WRITE:
  272. copy_last = rvt_is_user_qp(qp);
  273. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
  274. goto inv_err;
  275. do_write:
  276. if (wqe->length == 0)
  277. break;
  278. if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
  279. wqe->rdma_wr.remote_addr,
  280. wqe->rdma_wr.rkey,
  281. IB_ACCESS_REMOTE_WRITE)))
  282. goto acc_err;
  283. qp->r_sge.sg_list = NULL;
  284. qp->r_sge.num_sge = 1;
  285. qp->r_sge.total_len = wqe->length;
  286. break;
  287. case IB_WR_RDMA_READ:
  288. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
  289. goto inv_err;
  290. if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
  291. wqe->rdma_wr.remote_addr,
  292. wqe->rdma_wr.rkey,
  293. IB_ACCESS_REMOTE_READ)))
  294. goto acc_err;
  295. release = false;
  296. sqp->s_sge.sg_list = NULL;
  297. sqp->s_sge.num_sge = 1;
  298. qp->r_sge.sge = wqe->sg_list[0];
  299. qp->r_sge.sg_list = wqe->sg_list + 1;
  300. qp->r_sge.num_sge = wqe->wr.num_sge;
  301. qp->r_sge.total_len = wqe->length;
  302. break;
  303. case IB_WR_ATOMIC_CMP_AND_SWP:
  304. case IB_WR_ATOMIC_FETCH_AND_ADD:
  305. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
  306. goto inv_err;
  307. if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
  308. wqe->atomic_wr.remote_addr,
  309. wqe->atomic_wr.rkey,
  310. IB_ACCESS_REMOTE_ATOMIC)))
  311. goto acc_err;
  312. /* Perform atomic OP and save result. */
  313. maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
  314. sdata = wqe->atomic_wr.compare_add;
  315. *(u64 *)sqp->s_sge.sge.vaddr =
  316. (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
  317. (u64)atomic64_add_return(sdata, maddr) - sdata :
  318. (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
  319. sdata, wqe->atomic_wr.swap);
  320. rvt_put_mr(qp->r_sge.sge.mr);
  321. qp->r_sge.num_sge = 0;
  322. goto send_comp;
  323. default:
  324. send_status = IB_WC_LOC_QP_OP_ERR;
  325. goto serr;
  326. }
  327. sge = &sqp->s_sge.sge;
  328. while (sqp->s_len) {
  329. u32 len = sqp->s_len;
  330. if (len > sge->length)
  331. len = sge->length;
  332. if (len > sge->sge_length)
  333. len = sge->sge_length;
  334. WARN_ON_ONCE(len == 0);
  335. rvt_copy_sge(qp, &qp->r_sge, sge->vaddr,
  336. len, release, copy_last);
  337. sge->vaddr += len;
  338. sge->length -= len;
  339. sge->sge_length -= len;
  340. if (sge->sge_length == 0) {
  341. if (!release)
  342. rvt_put_mr(sge->mr);
  343. if (--sqp->s_sge.num_sge)
  344. *sge = *sqp->s_sge.sg_list++;
  345. } else if (sge->length == 0 && sge->mr->lkey) {
  346. if (++sge->n >= RVT_SEGSZ) {
  347. if (++sge->m >= sge->mr->mapsz)
  348. break;
  349. sge->n = 0;
  350. }
  351. sge->vaddr =
  352. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  353. sge->length =
  354. sge->mr->map[sge->m]->segs[sge->n].length;
  355. }
  356. sqp->s_len -= len;
  357. }
  358. if (release)
  359. rvt_put_ss(&qp->r_sge);
  360. if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
  361. goto send_comp;
  362. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  363. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  364. else
  365. wc.opcode = IB_WC_RECV;
  366. wc.wr_id = qp->r_wr_id;
  367. wc.status = IB_WC_SUCCESS;
  368. wc.byte_len = wqe->length;
  369. wc.qp = &qp->ibqp;
  370. wc.src_qp = qp->remote_qpn;
  371. wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr) & U16_MAX;
  372. wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
  373. wc.port_num = 1;
  374. /* Signal completion event if the solicited bit is set. */
  375. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  376. wqe->wr.send_flags & IB_SEND_SOLICITED);
  377. send_comp:
  378. spin_lock_irqsave(&sqp->s_lock, flags);
  379. ibp->rvp.n_loop_pkts++;
  380. flush_send:
  381. sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
  382. rvt_send_complete(sqp, wqe, send_status);
  383. if (local_ops) {
  384. atomic_dec(&sqp->local_ops_pending);
  385. local_ops = 0;
  386. }
  387. goto again;
  388. rnr_nak:
  389. /* Handle RNR NAK */
  390. if (qp->ibqp.qp_type == IB_QPT_UC)
  391. goto send_comp;
  392. ibp->rvp.n_rnr_naks++;
  393. /*
  394. * Note: we don't need the s_lock held since the BUSY flag
  395. * makes this single threaded.
  396. */
  397. if (sqp->s_rnr_retry == 0) {
  398. send_status = IB_WC_RNR_RETRY_EXC_ERR;
  399. goto serr;
  400. }
  401. if (sqp->s_rnr_retry_cnt < 7)
  402. sqp->s_rnr_retry--;
  403. spin_lock_irqsave(&sqp->s_lock, flags);
  404. if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
  405. goto clr_busy;
  406. rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
  407. IB_AETH_CREDIT_SHIFT);
  408. goto clr_busy;
  409. op_err:
  410. send_status = IB_WC_REM_OP_ERR;
  411. wc.status = IB_WC_LOC_QP_OP_ERR;
  412. goto err;
  413. inv_err:
  414. send_status = IB_WC_REM_INV_REQ_ERR;
  415. wc.status = IB_WC_LOC_QP_OP_ERR;
  416. goto err;
  417. acc_err:
  418. send_status = IB_WC_REM_ACCESS_ERR;
  419. wc.status = IB_WC_LOC_PROT_ERR;
  420. err:
  421. /* responder goes to error state */
  422. rvt_rc_error(qp, wc.status);
  423. serr:
  424. spin_lock_irqsave(&sqp->s_lock, flags);
  425. rvt_send_complete(sqp, wqe, send_status);
  426. if (sqp->ibqp.qp_type == IB_QPT_RC) {
  427. int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
  428. sqp->s_flags &= ~RVT_S_BUSY;
  429. spin_unlock_irqrestore(&sqp->s_lock, flags);
  430. if (lastwqe) {
  431. struct ib_event ev;
  432. ev.device = sqp->ibqp.device;
  433. ev.element.qp = &sqp->ibqp;
  434. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  435. sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
  436. }
  437. goto done;
  438. }
  439. clr_busy:
  440. sqp->s_flags &= ~RVT_S_BUSY;
  441. unlock:
  442. spin_unlock_irqrestore(&sqp->s_lock, flags);
  443. done:
  444. rcu_read_unlock();
  445. }
  446. /**
  447. * hfi1_make_grh - construct a GRH header
  448. * @ibp: a pointer to the IB port
  449. * @hdr: a pointer to the GRH header being constructed
  450. * @grh: the global route address to send to
  451. * @hwords: size of header after grh being sent in dwords
  452. * @nwords: the number of 32 bit words of data being sent
  453. *
  454. * Return the size of the header in 32 bit words.
  455. */
  456. u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
  457. const struct ib_global_route *grh, u32 hwords, u32 nwords)
  458. {
  459. hdr->version_tclass_flow =
  460. cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
  461. (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
  462. (grh->flow_label << IB_GRH_FLOW_SHIFT));
  463. hdr->paylen = cpu_to_be16((hwords + nwords) << 2);
  464. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  465. hdr->next_hdr = IB_GRH_NEXT_HDR;
  466. hdr->hop_limit = grh->hop_limit;
  467. /* The SGID is 32-bit aligned. */
  468. hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
  469. hdr->sgid.global.interface_id =
  470. grh->sgid_index < HFI1_GUIDS_PER_PORT ?
  471. get_sguid(ibp, grh->sgid_index) :
  472. get_sguid(ibp, HFI1_PORT_GUID_INDEX);
  473. hdr->dgid = grh->dgid;
  474. /* GRH header size in 32-bit words. */
  475. return sizeof(struct ib_grh) / sizeof(u32);
  476. }
  477. #define BTH2_OFFSET (offsetof(struct hfi1_sdma_header, \
  478. hdr.ibh.u.oth.bth[2]) / 4)
  479. /**
  480. * build_ahg - create ahg in s_ahg
  481. * @qp: a pointer to QP
  482. * @npsn: the next PSN for the request/response
  483. *
  484. * This routine handles the AHG by allocating an ahg entry and causing the
  485. * copy of the first middle.
  486. *
  487. * Subsequent middles use the copied entry, editing the
  488. * PSN with 1 or 2 edits.
  489. */
  490. static inline void build_ahg(struct rvt_qp *qp, u32 npsn)
  491. {
  492. struct hfi1_qp_priv *priv = qp->priv;
  493. if (unlikely(qp->s_flags & HFI1_S_AHG_CLEAR))
  494. clear_ahg(qp);
  495. if (!(qp->s_flags & HFI1_S_AHG_VALID)) {
  496. /* first middle that needs copy */
  497. if (qp->s_ahgidx < 0)
  498. qp->s_ahgidx = sdma_ahg_alloc(priv->s_sde);
  499. if (qp->s_ahgidx >= 0) {
  500. qp->s_ahgpsn = npsn;
  501. priv->s_ahg->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
  502. /* save to protect a change in another thread */
  503. priv->s_ahg->ahgidx = qp->s_ahgidx;
  504. qp->s_flags |= HFI1_S_AHG_VALID;
  505. }
  506. } else {
  507. /* subsequent middle after valid */
  508. if (qp->s_ahgidx >= 0) {
  509. priv->s_ahg->tx_flags |= SDMA_TXREQ_F_USE_AHG;
  510. priv->s_ahg->ahgidx = qp->s_ahgidx;
  511. priv->s_ahg->ahgcount++;
  512. priv->s_ahg->ahgdesc[0] =
  513. sdma_build_ahg_descriptor(
  514. (__force u16)cpu_to_be16((u16)npsn),
  515. BTH2_OFFSET,
  516. 16,
  517. 16);
  518. if ((npsn & 0xffff0000) !=
  519. (qp->s_ahgpsn & 0xffff0000)) {
  520. priv->s_ahg->ahgcount++;
  521. priv->s_ahg->ahgdesc[1] =
  522. sdma_build_ahg_descriptor(
  523. (__force u16)cpu_to_be16(
  524. (u16)(npsn >> 16)),
  525. BTH2_OFFSET,
  526. 0,
  527. 16);
  528. }
  529. }
  530. }
  531. }
  532. static inline void hfi1_make_ruc_bth(struct rvt_qp *qp,
  533. struct ib_other_headers *ohdr,
  534. u32 bth0, u32 bth1, u32 bth2)
  535. {
  536. bth1 |= qp->remote_qpn;
  537. ohdr->bth[0] = cpu_to_be32(bth0);
  538. ohdr->bth[1] = cpu_to_be32(bth1);
  539. ohdr->bth[2] = cpu_to_be32(bth2);
  540. }
  541. /**
  542. * hfi1_make_ruc_header_16B - build a 16B header
  543. * @qp: the queue pair
  544. * @ohdr: a pointer to the destination header memory
  545. * @bth0: bth0 passed in from the RC/UC builder
  546. * @bth2: bth2 passed in from the RC/UC builder
  547. * @middle: non zero implies indicates ahg "could" be used
  548. * @ps: the current packet state
  549. *
  550. * This routine may disarm ahg under these situations:
  551. * - packet needs a GRH
  552. * - BECN needed
  553. * - migration state not IB_MIG_MIGRATED
  554. */
  555. static inline void hfi1_make_ruc_header_16B(struct rvt_qp *qp,
  556. struct ib_other_headers *ohdr,
  557. u32 bth0, u32 bth2, int middle,
  558. struct hfi1_pkt_state *ps)
  559. {
  560. struct hfi1_qp_priv *priv = qp->priv;
  561. struct hfi1_ibport *ibp = ps->ibp;
  562. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  563. u32 bth1 = 0;
  564. u32 slid;
  565. u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
  566. u8 l4 = OPA_16B_L4_IB_LOCAL;
  567. u8 extra_bytes = hfi1_get_16b_padding(
  568. (ps->s_txreq->hdr_dwords << 2),
  569. ps->s_txreq->s_cur_size);
  570. u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
  571. extra_bytes + SIZE_OF_LT) >> 2);
  572. bool becn = false;
  573. if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH) &&
  574. hfi1_check_mcast(rdma_ah_get_dlid(&qp->remote_ah_attr))) {
  575. struct ib_grh *grh;
  576. struct ib_global_route *grd =
  577. rdma_ah_retrieve_grh(&qp->remote_ah_attr);
  578. /*
  579. * Ensure OPA GIDs are transformed to IB gids
  580. * before creating the GRH.
  581. */
  582. if (grd->sgid_index == OPA_GID_INDEX)
  583. grd->sgid_index = 0;
  584. grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
  585. l4 = OPA_16B_L4_IB_GLOBAL;
  586. ps->s_txreq->hdr_dwords +=
  587. hfi1_make_grh(ibp, grh, grd,
  588. ps->s_txreq->hdr_dwords - LRH_16B_DWORDS,
  589. nwords);
  590. middle = 0;
  591. }
  592. if (qp->s_mig_state == IB_MIG_MIGRATED)
  593. bth1 |= OPA_BTH_MIG_REQ;
  594. else
  595. middle = 0;
  596. if (qp->s_flags & RVT_S_ECN) {
  597. qp->s_flags &= ~RVT_S_ECN;
  598. /* we recently received a FECN, so return a BECN */
  599. becn = true;
  600. middle = 0;
  601. }
  602. if (middle)
  603. build_ahg(qp, bth2);
  604. else
  605. qp->s_flags &= ~HFI1_S_AHG_VALID;
  606. bth0 |= pkey;
  607. bth0 |= extra_bytes << 20;
  608. hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
  609. if (!ppd->lid)
  610. slid = be32_to_cpu(OPA_LID_PERMISSIVE);
  611. else
  612. slid = ppd->lid |
  613. (rdma_ah_get_path_bits(&qp->remote_ah_attr) &
  614. ((1 << ppd->lmc) - 1));
  615. hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
  616. slid,
  617. opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr),
  618. 16B),
  619. (ps->s_txreq->hdr_dwords + nwords) >> 1,
  620. pkey, becn, 0, l4, priv->s_sc);
  621. }
  622. /**
  623. * hfi1_make_ruc_header_9B - build a 9B header
  624. * @qp: the queue pair
  625. * @ohdr: a pointer to the destination header memory
  626. * @bth0: bth0 passed in from the RC/UC builder
  627. * @bth2: bth2 passed in from the RC/UC builder
  628. * @middle: non zero implies indicates ahg "could" be used
  629. * @ps: the current packet state
  630. *
  631. * This routine may disarm ahg under these situations:
  632. * - packet needs a GRH
  633. * - BECN needed
  634. * - migration state not IB_MIG_MIGRATED
  635. */
  636. static inline void hfi1_make_ruc_header_9B(struct rvt_qp *qp,
  637. struct ib_other_headers *ohdr,
  638. u32 bth0, u32 bth2, int middle,
  639. struct hfi1_pkt_state *ps)
  640. {
  641. struct hfi1_qp_priv *priv = qp->priv;
  642. struct hfi1_ibport *ibp = ps->ibp;
  643. u32 bth1 = 0;
  644. u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
  645. u16 lrh0 = HFI1_LRH_BTH;
  646. u8 extra_bytes = -ps->s_txreq->s_cur_size & 3;
  647. u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
  648. extra_bytes) >> 2);
  649. if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
  650. struct ib_grh *grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
  651. lrh0 = HFI1_LRH_GRH;
  652. ps->s_txreq->hdr_dwords +=
  653. hfi1_make_grh(ibp, grh,
  654. rdma_ah_read_grh(&qp->remote_ah_attr),
  655. ps->s_txreq->hdr_dwords - LRH_9B_DWORDS,
  656. nwords);
  657. middle = 0;
  658. }
  659. lrh0 |= (priv->s_sc & 0xf) << 12 |
  660. (rdma_ah_get_sl(&qp->remote_ah_attr) & 0xf) << 4;
  661. if (qp->s_mig_state == IB_MIG_MIGRATED)
  662. bth0 |= IB_BTH_MIG_REQ;
  663. else
  664. middle = 0;
  665. if (qp->s_flags & RVT_S_ECN) {
  666. qp->s_flags &= ~RVT_S_ECN;
  667. /* we recently received a FECN, so return a BECN */
  668. bth1 |= (IB_BECN_MASK << IB_BECN_SHIFT);
  669. middle = 0;
  670. }
  671. if (middle)
  672. build_ahg(qp, bth2);
  673. else
  674. qp->s_flags &= ~HFI1_S_AHG_VALID;
  675. bth0 |= pkey;
  676. bth0 |= extra_bytes << 20;
  677. hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
  678. hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
  679. lrh0,
  680. ps->s_txreq->hdr_dwords + nwords,
  681. opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr), 9B),
  682. ppd_from_ibp(ibp)->lid |
  683. rdma_ah_get_path_bits(&qp->remote_ah_attr));
  684. }
  685. typedef void (*hfi1_make_ruc_hdr)(struct rvt_qp *qp,
  686. struct ib_other_headers *ohdr,
  687. u32 bth0, u32 bth2, int middle,
  688. struct hfi1_pkt_state *ps);
  689. /* We support only two types - 9B and 16B for now */
  690. static const hfi1_make_ruc_hdr hfi1_ruc_header_tbl[2] = {
  691. [HFI1_PKT_TYPE_9B] = &hfi1_make_ruc_header_9B,
  692. [HFI1_PKT_TYPE_16B] = &hfi1_make_ruc_header_16B
  693. };
  694. void hfi1_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
  695. u32 bth0, u32 bth2, int middle,
  696. struct hfi1_pkt_state *ps)
  697. {
  698. struct hfi1_qp_priv *priv = qp->priv;
  699. /*
  700. * reset s_ahg/AHG fields
  701. *
  702. * This insures that the ahgentry/ahgcount
  703. * are at a non-AHG default to protect
  704. * build_verbs_tx_desc() from using
  705. * an include ahgidx.
  706. *
  707. * build_ahg() will modify as appropriate
  708. * to use the AHG feature.
  709. */
  710. priv->s_ahg->tx_flags = 0;
  711. priv->s_ahg->ahgcount = 0;
  712. priv->s_ahg->ahgidx = 0;
  713. /* Make the appropriate header */
  714. hfi1_ruc_header_tbl[priv->hdr_type](qp, ohdr, bth0, bth2, middle, ps);
  715. }
  716. /* when sending, force a reschedule every one of these periods */
  717. #define SEND_RESCHED_TIMEOUT (5 * HZ) /* 5s in jiffies */
  718. /**
  719. * schedule_send_yield - test for a yield required for QP send engine
  720. * @timeout: Final time for timeout slice for jiffies
  721. * @qp: a pointer to QP
  722. * @ps: a pointer to a structure with commonly lookup values for
  723. * the the send engine progress
  724. *
  725. * This routine checks if the time slice for the QP has expired
  726. * for RC QPs, if so an additional work entry is queued. At this
  727. * point, other QPs have an opportunity to be scheduled. It
  728. * returns true if a yield is required, otherwise, false
  729. * is returned.
  730. */
  731. static bool schedule_send_yield(struct rvt_qp *qp,
  732. struct hfi1_pkt_state *ps)
  733. {
  734. ps->pkts_sent = true;
  735. if (unlikely(time_after(jiffies, ps->timeout))) {
  736. if (!ps->in_thread ||
  737. workqueue_congested(ps->cpu, ps->ppd->hfi1_wq)) {
  738. spin_lock_irqsave(&qp->s_lock, ps->flags);
  739. qp->s_flags &= ~RVT_S_BUSY;
  740. hfi1_schedule_send(qp);
  741. spin_unlock_irqrestore(&qp->s_lock, ps->flags);
  742. this_cpu_inc(*ps->ppd->dd->send_schedule);
  743. trace_hfi1_rc_expired_time_slice(qp, true);
  744. return true;
  745. }
  746. cond_resched();
  747. this_cpu_inc(*ps->ppd->dd->send_schedule);
  748. ps->timeout = jiffies + ps->timeout_int;
  749. }
  750. trace_hfi1_rc_expired_time_slice(qp, false);
  751. return false;
  752. }
  753. void hfi1_do_send_from_rvt(struct rvt_qp *qp)
  754. {
  755. hfi1_do_send(qp, false);
  756. }
  757. void _hfi1_do_send(struct work_struct *work)
  758. {
  759. struct iowait_work *w = container_of(work, struct iowait_work, iowork);
  760. struct rvt_qp *qp = iowait_to_qp(w->iow);
  761. hfi1_do_send(qp, true);
  762. }
  763. /**
  764. * hfi1_do_send - perform a send on a QP
  765. * @work: contains a pointer to the QP
  766. * @in_thread: true if in a workqueue thread
  767. *
  768. * Process entries in the send work queue until credit or queue is
  769. * exhausted. Only allow one CPU to send a packet per QP.
  770. * Otherwise, two threads could send packets out of order.
  771. */
  772. void hfi1_do_send(struct rvt_qp *qp, bool in_thread)
  773. {
  774. struct hfi1_pkt_state ps;
  775. struct hfi1_qp_priv *priv = qp->priv;
  776. int (*make_req)(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
  777. ps.dev = to_idev(qp->ibqp.device);
  778. ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
  779. ps.ppd = ppd_from_ibp(ps.ibp);
  780. ps.in_thread = in_thread;
  781. ps.wait = iowait_get_ib_work(&priv->s_iowait);
  782. trace_hfi1_rc_do_send(qp, in_thread);
  783. switch (qp->ibqp.qp_type) {
  784. case IB_QPT_RC:
  785. if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
  786. ~((1 << ps.ppd->lmc) - 1)) ==
  787. ps.ppd->lid)) {
  788. ruc_loopback(qp);
  789. return;
  790. }
  791. make_req = hfi1_make_rc_req;
  792. ps.timeout_int = qp->timeout_jiffies;
  793. break;
  794. case IB_QPT_UC:
  795. if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
  796. ~((1 << ps.ppd->lmc) - 1)) ==
  797. ps.ppd->lid)) {
  798. ruc_loopback(qp);
  799. return;
  800. }
  801. make_req = hfi1_make_uc_req;
  802. ps.timeout_int = SEND_RESCHED_TIMEOUT;
  803. break;
  804. default:
  805. make_req = hfi1_make_ud_req;
  806. ps.timeout_int = SEND_RESCHED_TIMEOUT;
  807. }
  808. spin_lock_irqsave(&qp->s_lock, ps.flags);
  809. /* Return if we are already busy processing a work request. */
  810. if (!hfi1_send_ok(qp)) {
  811. if (qp->s_flags & HFI1_S_ANY_WAIT_IO)
  812. iowait_set_flag(&priv->s_iowait, IOWAIT_PENDING_IB);
  813. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  814. return;
  815. }
  816. qp->s_flags |= RVT_S_BUSY;
  817. ps.timeout_int = ps.timeout_int / 8;
  818. ps.timeout = jiffies + ps.timeout_int;
  819. ps.cpu = priv->s_sde ? priv->s_sde->cpu :
  820. cpumask_first(cpumask_of_node(ps.ppd->dd->node));
  821. ps.pkts_sent = false;
  822. /* insure a pre-built packet is handled */
  823. ps.s_txreq = get_waiting_verbs_txreq(ps.wait);
  824. do {
  825. /* Check for a constructed packet to be sent. */
  826. if (ps.s_txreq) {
  827. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  828. /*
  829. * If the packet cannot be sent now, return and
  830. * the send engine will be woken up later.
  831. */
  832. if (hfi1_verbs_send(qp, &ps))
  833. return;
  834. /* allow other tasks to run */
  835. if (schedule_send_yield(qp, &ps))
  836. return;
  837. spin_lock_irqsave(&qp->s_lock, ps.flags);
  838. }
  839. } while (make_req(qp, &ps));
  840. iowait_starve_clear(ps.pkts_sent, &priv->s_iowait);
  841. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  842. }