ruc.c 27 KB

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