ruc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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. hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, release, copy_last);
  336. sge->vaddr += len;
  337. sge->length -= len;
  338. sge->sge_length -= len;
  339. if (sge->sge_length == 0) {
  340. if (!release)
  341. rvt_put_mr(sge->mr);
  342. if (--sqp->s_sge.num_sge)
  343. *sge = *sqp->s_sge.sg_list++;
  344. } else if (sge->length == 0 && sge->mr->lkey) {
  345. if (++sge->n >= RVT_SEGSZ) {
  346. if (++sge->m >= sge->mr->mapsz)
  347. break;
  348. sge->n = 0;
  349. }
  350. sge->vaddr =
  351. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  352. sge->length =
  353. sge->mr->map[sge->m]->segs[sge->n].length;
  354. }
  355. sqp->s_len -= len;
  356. }
  357. if (release)
  358. rvt_put_ss(&qp->r_sge);
  359. if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
  360. goto send_comp;
  361. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  362. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  363. else
  364. wc.opcode = IB_WC_RECV;
  365. wc.wr_id = qp->r_wr_id;
  366. wc.status = IB_WC_SUCCESS;
  367. wc.byte_len = wqe->length;
  368. wc.qp = &qp->ibqp;
  369. wc.src_qp = qp->remote_qpn;
  370. wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr) & U16_MAX;
  371. wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
  372. wc.port_num = 1;
  373. /* Signal completion event if the solicited bit is set. */
  374. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  375. wqe->wr.send_flags & IB_SEND_SOLICITED);
  376. send_comp:
  377. spin_lock_irqsave(&sqp->s_lock, flags);
  378. ibp->rvp.n_loop_pkts++;
  379. flush_send:
  380. sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
  381. hfi1_send_complete(sqp, wqe, send_status);
  382. if (local_ops) {
  383. atomic_dec(&sqp->local_ops_pending);
  384. local_ops = 0;
  385. }
  386. goto again;
  387. rnr_nak:
  388. /* Handle RNR NAK */
  389. if (qp->ibqp.qp_type == IB_QPT_UC)
  390. goto send_comp;
  391. ibp->rvp.n_rnr_naks++;
  392. /*
  393. * Note: we don't need the s_lock held since the BUSY flag
  394. * makes this single threaded.
  395. */
  396. if (sqp->s_rnr_retry == 0) {
  397. send_status = IB_WC_RNR_RETRY_EXC_ERR;
  398. goto serr;
  399. }
  400. if (sqp->s_rnr_retry_cnt < 7)
  401. sqp->s_rnr_retry--;
  402. spin_lock_irqsave(&sqp->s_lock, flags);
  403. if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
  404. goto clr_busy;
  405. rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
  406. IB_AETH_CREDIT_SHIFT);
  407. goto clr_busy;
  408. op_err:
  409. send_status = IB_WC_REM_OP_ERR;
  410. wc.status = IB_WC_LOC_QP_OP_ERR;
  411. goto err;
  412. inv_err:
  413. send_status = IB_WC_REM_INV_REQ_ERR;
  414. wc.status = IB_WC_LOC_QP_OP_ERR;
  415. goto err;
  416. acc_err:
  417. send_status = IB_WC_REM_ACCESS_ERR;
  418. wc.status = IB_WC_LOC_PROT_ERR;
  419. err:
  420. /* responder goes to error state */
  421. rvt_rc_error(qp, wc.status);
  422. serr:
  423. spin_lock_irqsave(&sqp->s_lock, flags);
  424. hfi1_send_complete(sqp, wqe, send_status);
  425. if (sqp->ibqp.qp_type == IB_QPT_RC) {
  426. int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
  427. sqp->s_flags &= ~RVT_S_BUSY;
  428. spin_unlock_irqrestore(&sqp->s_lock, flags);
  429. if (lastwqe) {
  430. struct ib_event ev;
  431. ev.device = sqp->ibqp.device;
  432. ev.element.qp = &sqp->ibqp;
  433. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  434. sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
  435. }
  436. goto done;
  437. }
  438. clr_busy:
  439. sqp->s_flags &= ~RVT_S_BUSY;
  440. unlock:
  441. spin_unlock_irqrestore(&sqp->s_lock, flags);
  442. done:
  443. rcu_read_unlock();
  444. }
  445. /**
  446. * hfi1_make_grh - construct a GRH header
  447. * @ibp: a pointer to the IB port
  448. * @hdr: a pointer to the GRH header being constructed
  449. * @grh: the global route address to send to
  450. * @hwords: size of header after grh being sent in dwords
  451. * @nwords: the number of 32 bit words of data being sent
  452. *
  453. * Return the size of the header in 32 bit words.
  454. */
  455. u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
  456. const struct ib_global_route *grh, u32 hwords, u32 nwords)
  457. {
  458. hdr->version_tclass_flow =
  459. cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
  460. (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
  461. (grh->flow_label << IB_GRH_FLOW_SHIFT));
  462. hdr->paylen = cpu_to_be16((hwords + nwords) << 2);
  463. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  464. hdr->next_hdr = IB_GRH_NEXT_HDR;
  465. hdr->hop_limit = grh->hop_limit;
  466. /* The SGID is 32-bit aligned. */
  467. hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
  468. hdr->sgid.global.interface_id =
  469. grh->sgid_index < HFI1_GUIDS_PER_PORT ?
  470. get_sguid(ibp, grh->sgid_index) :
  471. get_sguid(ibp, HFI1_PORT_GUID_INDEX);
  472. hdr->dgid = grh->dgid;
  473. /* GRH header size in 32-bit words. */
  474. return sizeof(struct ib_grh) / sizeof(u32);
  475. }
  476. #define BTH2_OFFSET (offsetof(struct hfi1_sdma_header, \
  477. hdr.ibh.u.oth.bth[2]) / 4)
  478. /**
  479. * build_ahg - create ahg in s_ahg
  480. * @qp: a pointer to QP
  481. * @npsn: the next PSN for the request/response
  482. *
  483. * This routine handles the AHG by allocating an ahg entry and causing the
  484. * copy of the first middle.
  485. *
  486. * Subsequent middles use the copied entry, editing the
  487. * PSN with 1 or 2 edits.
  488. */
  489. static inline void build_ahg(struct rvt_qp *qp, u32 npsn)
  490. {
  491. struct hfi1_qp_priv *priv = qp->priv;
  492. if (unlikely(qp->s_flags & HFI1_S_AHG_CLEAR))
  493. clear_ahg(qp);
  494. if (!(qp->s_flags & HFI1_S_AHG_VALID)) {
  495. /* first middle that needs copy */
  496. if (qp->s_ahgidx < 0)
  497. qp->s_ahgidx = sdma_ahg_alloc(priv->s_sde);
  498. if (qp->s_ahgidx >= 0) {
  499. qp->s_ahgpsn = npsn;
  500. priv->s_ahg->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
  501. /* save to protect a change in another thread */
  502. priv->s_ahg->ahgidx = qp->s_ahgidx;
  503. qp->s_flags |= HFI1_S_AHG_VALID;
  504. }
  505. } else {
  506. /* subsequent middle after valid */
  507. if (qp->s_ahgidx >= 0) {
  508. priv->s_ahg->tx_flags |= SDMA_TXREQ_F_USE_AHG;
  509. priv->s_ahg->ahgidx = qp->s_ahgidx;
  510. priv->s_ahg->ahgcount++;
  511. priv->s_ahg->ahgdesc[0] =
  512. sdma_build_ahg_descriptor(
  513. (__force u16)cpu_to_be16((u16)npsn),
  514. BTH2_OFFSET,
  515. 16,
  516. 16);
  517. if ((npsn & 0xffff0000) !=
  518. (qp->s_ahgpsn & 0xffff0000)) {
  519. priv->s_ahg->ahgcount++;
  520. priv->s_ahg->ahgdesc[1] =
  521. sdma_build_ahg_descriptor(
  522. (__force u16)cpu_to_be16(
  523. (u16)(npsn >> 16)),
  524. BTH2_OFFSET,
  525. 0,
  526. 16);
  527. }
  528. }
  529. }
  530. }
  531. static inline void hfi1_make_ruc_bth(struct rvt_qp *qp,
  532. struct ib_other_headers *ohdr,
  533. u32 bth0, u32 bth1, u32 bth2)
  534. {
  535. bth1 |= qp->remote_qpn;
  536. ohdr->bth[0] = cpu_to_be32(bth0);
  537. ohdr->bth[1] = cpu_to_be32(bth1);
  538. ohdr->bth[2] = cpu_to_be32(bth2);
  539. }
  540. /**
  541. * hfi1_make_ruc_header_16B - build a 16B header
  542. * @qp: the queue pair
  543. * @ohdr: a pointer to the destination header memory
  544. * @bth0: bth0 passed in from the RC/UC builder
  545. * @bth2: bth2 passed in from the RC/UC builder
  546. * @middle: non zero implies indicates ahg "could" be used
  547. * @ps: the current packet state
  548. *
  549. * This routine may disarm ahg under these situations:
  550. * - packet needs a GRH
  551. * - BECN needed
  552. * - migration state not IB_MIG_MIGRATED
  553. */
  554. static inline void hfi1_make_ruc_header_16B(struct rvt_qp *qp,
  555. struct ib_other_headers *ohdr,
  556. u32 bth0, u32 bth2, int middle,
  557. struct hfi1_pkt_state *ps)
  558. {
  559. struct hfi1_qp_priv *priv = qp->priv;
  560. struct hfi1_ibport *ibp = ps->ibp;
  561. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  562. u32 bth1 = 0;
  563. u32 slid;
  564. u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
  565. u8 l4 = OPA_16B_L4_IB_LOCAL;
  566. u8 extra_bytes = hfi1_get_16b_padding(
  567. (ps->s_txreq->hdr_dwords << 2),
  568. ps->s_txreq->s_cur_size);
  569. u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
  570. extra_bytes + SIZE_OF_LT) >> 2);
  571. bool becn = false;
  572. if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH) &&
  573. hfi1_check_mcast(rdma_ah_get_dlid(&qp->remote_ah_attr))) {
  574. struct ib_grh *grh;
  575. struct ib_global_route *grd =
  576. rdma_ah_retrieve_grh(&qp->remote_ah_attr);
  577. /*
  578. * Ensure OPA GIDs are transformed to IB gids
  579. * before creating the GRH.
  580. */
  581. if (grd->sgid_index == OPA_GID_INDEX)
  582. grd->sgid_index = 0;
  583. grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
  584. l4 = OPA_16B_L4_IB_GLOBAL;
  585. ps->s_txreq->hdr_dwords +=
  586. hfi1_make_grh(ibp, grh, grd,
  587. ps->s_txreq->hdr_dwords - LRH_16B_DWORDS,
  588. nwords);
  589. middle = 0;
  590. }
  591. if (qp->s_mig_state == IB_MIG_MIGRATED)
  592. bth1 |= OPA_BTH_MIG_REQ;
  593. else
  594. middle = 0;
  595. if (qp->s_flags & RVT_S_ECN) {
  596. qp->s_flags &= ~RVT_S_ECN;
  597. /* we recently received a FECN, so return a BECN */
  598. becn = true;
  599. middle = 0;
  600. }
  601. if (middle)
  602. build_ahg(qp, bth2);
  603. else
  604. qp->s_flags &= ~HFI1_S_AHG_VALID;
  605. bth0 |= pkey;
  606. bth0 |= extra_bytes << 20;
  607. hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
  608. if (!ppd->lid)
  609. slid = be32_to_cpu(OPA_LID_PERMISSIVE);
  610. else
  611. slid = ppd->lid |
  612. (rdma_ah_get_path_bits(&qp->remote_ah_attr) &
  613. ((1 << ppd->lmc) - 1));
  614. hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
  615. slid,
  616. opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr),
  617. 16B),
  618. (ps->s_txreq->hdr_dwords + nwords) >> 1,
  619. pkey, becn, 0, l4, priv->s_sc);
  620. }
  621. /**
  622. * hfi1_make_ruc_header_9B - build a 9B header
  623. * @qp: the queue pair
  624. * @ohdr: a pointer to the destination header memory
  625. * @bth0: bth0 passed in from the RC/UC builder
  626. * @bth2: bth2 passed in from the RC/UC builder
  627. * @middle: non zero implies indicates ahg "could" be used
  628. * @ps: the current packet state
  629. *
  630. * This routine may disarm ahg under these situations:
  631. * - packet needs a GRH
  632. * - BECN needed
  633. * - migration state not IB_MIG_MIGRATED
  634. */
  635. static inline void hfi1_make_ruc_header_9B(struct rvt_qp *qp,
  636. struct ib_other_headers *ohdr,
  637. u32 bth0, u32 bth2, int middle,
  638. struct hfi1_pkt_state *ps)
  639. {
  640. struct hfi1_qp_priv *priv = qp->priv;
  641. struct hfi1_ibport *ibp = ps->ibp;
  642. u32 bth1 = 0;
  643. u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
  644. u16 lrh0 = HFI1_LRH_BTH;
  645. u8 extra_bytes = -ps->s_txreq->s_cur_size & 3;
  646. u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
  647. extra_bytes) >> 2);
  648. if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
  649. struct ib_grh *grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
  650. lrh0 = HFI1_LRH_GRH;
  651. ps->s_txreq->hdr_dwords +=
  652. hfi1_make_grh(ibp, grh,
  653. rdma_ah_read_grh(&qp->remote_ah_attr),
  654. ps->s_txreq->hdr_dwords - LRH_9B_DWORDS,
  655. nwords);
  656. middle = 0;
  657. }
  658. lrh0 |= (priv->s_sc & 0xf) << 12 |
  659. (rdma_ah_get_sl(&qp->remote_ah_attr) & 0xf) << 4;
  660. if (qp->s_mig_state == IB_MIG_MIGRATED)
  661. bth0 |= IB_BTH_MIG_REQ;
  662. else
  663. middle = 0;
  664. if (qp->s_flags & RVT_S_ECN) {
  665. qp->s_flags &= ~RVT_S_ECN;
  666. /* we recently received a FECN, so return a BECN */
  667. bth1 |= (IB_BECN_MASK << IB_BECN_SHIFT);
  668. middle = 0;
  669. }
  670. if (middle)
  671. build_ahg(qp, bth2);
  672. else
  673. qp->s_flags &= ~HFI1_S_AHG_VALID;
  674. bth0 |= pkey;
  675. bth0 |= extra_bytes << 20;
  676. hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
  677. hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
  678. lrh0,
  679. ps->s_txreq->hdr_dwords + nwords,
  680. opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr), 9B),
  681. ppd_from_ibp(ibp)->lid |
  682. rdma_ah_get_path_bits(&qp->remote_ah_attr));
  683. }
  684. typedef void (*hfi1_make_ruc_hdr)(struct rvt_qp *qp,
  685. struct ib_other_headers *ohdr,
  686. u32 bth0, u32 bth2, int middle,
  687. struct hfi1_pkt_state *ps);
  688. /* We support only two types - 9B and 16B for now */
  689. static const hfi1_make_ruc_hdr hfi1_ruc_header_tbl[2] = {
  690. [HFI1_PKT_TYPE_9B] = &hfi1_make_ruc_header_9B,
  691. [HFI1_PKT_TYPE_16B] = &hfi1_make_ruc_header_16B
  692. };
  693. void hfi1_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
  694. u32 bth0, u32 bth2, int middle,
  695. struct hfi1_pkt_state *ps)
  696. {
  697. struct hfi1_qp_priv *priv = qp->priv;
  698. /*
  699. * reset s_ahg/AHG fields
  700. *
  701. * This insures that the ahgentry/ahgcount
  702. * are at a non-AHG default to protect
  703. * build_verbs_tx_desc() from using
  704. * an include ahgidx.
  705. *
  706. * build_ahg() will modify as appropriate
  707. * to use the AHG feature.
  708. */
  709. priv->s_ahg->tx_flags = 0;
  710. priv->s_ahg->ahgcount = 0;
  711. priv->s_ahg->ahgidx = 0;
  712. /* Make the appropriate header */
  713. hfi1_ruc_header_tbl[priv->hdr_type](qp, ohdr, bth0, bth2, middle, ps);
  714. }
  715. /* when sending, force a reschedule every one of these periods */
  716. #define SEND_RESCHED_TIMEOUT (5 * HZ) /* 5s in jiffies */
  717. /**
  718. * schedule_send_yield - test for a yield required for QP send engine
  719. * @timeout: Final time for timeout slice for jiffies
  720. * @qp: a pointer to QP
  721. * @ps: a pointer to a structure with commonly lookup values for
  722. * the the send engine progress
  723. *
  724. * This routine checks if the time slice for the QP has expired
  725. * for RC QPs, if so an additional work entry is queued. At this
  726. * point, other QPs have an opportunity to be scheduled. It
  727. * returns true if a yield is required, otherwise, false
  728. * is returned.
  729. */
  730. static bool schedule_send_yield(struct rvt_qp *qp,
  731. struct hfi1_pkt_state *ps)
  732. {
  733. ps->pkts_sent = true;
  734. if (unlikely(time_after(jiffies, ps->timeout))) {
  735. if (!ps->in_thread ||
  736. workqueue_congested(ps->cpu, ps->ppd->hfi1_wq)) {
  737. spin_lock_irqsave(&qp->s_lock, ps->flags);
  738. qp->s_flags &= ~RVT_S_BUSY;
  739. hfi1_schedule_send(qp);
  740. spin_unlock_irqrestore(&qp->s_lock, ps->flags);
  741. this_cpu_inc(*ps->ppd->dd->send_schedule);
  742. trace_hfi1_rc_expired_time_slice(qp, true);
  743. return true;
  744. }
  745. cond_resched();
  746. this_cpu_inc(*ps->ppd->dd->send_schedule);
  747. ps->timeout = jiffies + ps->timeout_int;
  748. }
  749. trace_hfi1_rc_expired_time_slice(qp, false);
  750. return false;
  751. }
  752. void hfi1_do_send_from_rvt(struct rvt_qp *qp)
  753. {
  754. hfi1_do_send(qp, false);
  755. }
  756. void _hfi1_do_send(struct work_struct *work)
  757. {
  758. struct iowait *wait = container_of(work, struct iowait, iowork);
  759. struct rvt_qp *qp = iowait_to_qp(wait);
  760. hfi1_do_send(qp, true);
  761. }
  762. /**
  763. * hfi1_do_send - perform a send on a QP
  764. * @work: contains a pointer to the QP
  765. * @in_thread: true if in a workqueue thread
  766. *
  767. * Process entries in the send work queue until credit or queue is
  768. * exhausted. Only allow one CPU to send a packet per QP.
  769. * Otherwise, two threads could send packets out of order.
  770. */
  771. void hfi1_do_send(struct rvt_qp *qp, bool in_thread)
  772. {
  773. struct hfi1_pkt_state ps;
  774. struct hfi1_qp_priv *priv = qp->priv;
  775. int (*make_req)(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
  776. ps.dev = to_idev(qp->ibqp.device);
  777. ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
  778. ps.ppd = ppd_from_ibp(ps.ibp);
  779. ps.in_thread = in_thread;
  780. trace_hfi1_rc_do_send(qp, in_thread);
  781. switch (qp->ibqp.qp_type) {
  782. case IB_QPT_RC:
  783. if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
  784. ~((1 << ps.ppd->lmc) - 1)) ==
  785. ps.ppd->lid)) {
  786. ruc_loopback(qp);
  787. return;
  788. }
  789. make_req = hfi1_make_rc_req;
  790. ps.timeout_int = qp->timeout_jiffies;
  791. break;
  792. case IB_QPT_UC:
  793. if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
  794. ~((1 << ps.ppd->lmc) - 1)) ==
  795. ps.ppd->lid)) {
  796. ruc_loopback(qp);
  797. return;
  798. }
  799. make_req = hfi1_make_uc_req;
  800. ps.timeout_int = SEND_RESCHED_TIMEOUT;
  801. break;
  802. default:
  803. make_req = hfi1_make_ud_req;
  804. ps.timeout_int = SEND_RESCHED_TIMEOUT;
  805. }
  806. spin_lock_irqsave(&qp->s_lock, ps.flags);
  807. /* Return if we are already busy processing a work request. */
  808. if (!hfi1_send_ok(qp)) {
  809. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  810. return;
  811. }
  812. qp->s_flags |= RVT_S_BUSY;
  813. ps.timeout_int = ps.timeout_int / 8;
  814. ps.timeout = jiffies + ps.timeout_int;
  815. ps.cpu = priv->s_sde ? priv->s_sde->cpu :
  816. cpumask_first(cpumask_of_node(ps.ppd->dd->node));
  817. ps.pkts_sent = false;
  818. /* insure a pre-built packet is handled */
  819. ps.s_txreq = get_waiting_verbs_txreq(qp);
  820. do {
  821. /* Check for a constructed packet to be sent. */
  822. if (ps.s_txreq) {
  823. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  824. /*
  825. * If the packet cannot be sent now, return and
  826. * the send engine will be woken up later.
  827. */
  828. if (hfi1_verbs_send(qp, &ps))
  829. return;
  830. /* allow other tasks to run */
  831. if (schedule_send_yield(qp, &ps))
  832. return;
  833. spin_lock_irqsave(&qp->s_lock, ps.flags);
  834. }
  835. } while (make_req(qp, &ps));
  836. iowait_starve_clear(ps.pkts_sent, &priv->s_iowait);
  837. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  838. }
  839. /*
  840. * This should be called with s_lock held.
  841. */
  842. void hfi1_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
  843. enum ib_wc_status status)
  844. {
  845. u32 old_last, last;
  846. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
  847. return;
  848. last = qp->s_last;
  849. old_last = last;
  850. trace_hfi1_qp_send_completion(qp, wqe, last);
  851. if (++last >= qp->s_size)
  852. last = 0;
  853. trace_hfi1_qp_send_completion(qp, wqe, last);
  854. qp->s_last = last;
  855. /* See post_send() */
  856. barrier();
  857. rvt_put_swqe(wqe);
  858. if (qp->ibqp.qp_type == IB_QPT_UD ||
  859. qp->ibqp.qp_type == IB_QPT_SMI ||
  860. qp->ibqp.qp_type == IB_QPT_GSI)
  861. atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
  862. rvt_qp_swqe_complete(qp,
  863. wqe,
  864. ib_hfi1_wc_opcode[wqe->wr.opcode],
  865. status);
  866. if (qp->s_acked == old_last)
  867. qp->s_acked = last;
  868. if (qp->s_cur == old_last)
  869. qp->s_cur = last;
  870. if (qp->s_tail == old_last)
  871. qp->s_tail = last;
  872. if (qp->state == IB_QPS_SQD && last == qp->s_cur)
  873. qp->s_draining = 0;
  874. }