ruc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/spinlock.h>
  48. #include "hfi.h"
  49. #include "mad.h"
  50. #include "qp.h"
  51. #include "verbs_txreq.h"
  52. #include "trace.h"
  53. /*
  54. * Validate a RWQE and fill in the SGE state.
  55. * Return 1 if OK.
  56. */
  57. static int init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
  58. {
  59. int i, j, ret;
  60. struct ib_wc wc;
  61. struct rvt_lkey_table *rkt;
  62. struct rvt_pd *pd;
  63. struct rvt_sge_state *ss;
  64. rkt = &to_idev(qp->ibqp.device)->rdi.lkey_table;
  65. pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
  66. ss = &qp->r_sge;
  67. ss->sg_list = qp->r_sg_list;
  68. qp->r_len = 0;
  69. for (i = j = 0; i < wqe->num_sge; i++) {
  70. if (wqe->sg_list[i].length == 0)
  71. continue;
  72. /* Check LKEY */
  73. if (!rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
  74. &wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE))
  75. goto bad_lkey;
  76. qp->r_len += wqe->sg_list[i].length;
  77. j++;
  78. }
  79. ss->num_sge = j;
  80. ss->total_len = qp->r_len;
  81. ret = 1;
  82. goto bail;
  83. bad_lkey:
  84. while (j) {
  85. struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
  86. rvt_put_mr(sge->mr);
  87. }
  88. ss->num_sge = 0;
  89. memset(&wc, 0, sizeof(wc));
  90. wc.wr_id = wqe->wr_id;
  91. wc.status = IB_WC_LOC_PROT_ERR;
  92. wc.opcode = IB_WC_RECV;
  93. wc.qp = &qp->ibqp;
  94. /* Signal solicited completion event. */
  95. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  96. ret = 0;
  97. bail:
  98. return ret;
  99. }
  100. /**
  101. * hfi1_rvt_get_rwqe - copy the next RWQE into the QP's RWQE
  102. * @qp: the QP
  103. * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
  104. *
  105. * Return -1 if there is a local error, 0 if no RWQE is available,
  106. * otherwise return 1.
  107. *
  108. * Can be called from interrupt level.
  109. */
  110. int hfi1_rvt_get_rwqe(struct rvt_qp *qp, int wr_id_only)
  111. {
  112. unsigned long flags;
  113. struct rvt_rq *rq;
  114. struct rvt_rwq *wq;
  115. struct rvt_srq *srq;
  116. struct rvt_rwqe *wqe;
  117. void (*handler)(struct ib_event *, void *);
  118. u32 tail;
  119. int ret;
  120. if (qp->ibqp.srq) {
  121. srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
  122. handler = srq->ibsrq.event_handler;
  123. rq = &srq->rq;
  124. } else {
  125. srq = NULL;
  126. handler = NULL;
  127. rq = &qp->r_rq;
  128. }
  129. spin_lock_irqsave(&rq->lock, flags);
  130. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
  131. ret = 0;
  132. goto unlock;
  133. }
  134. wq = rq->wq;
  135. tail = wq->tail;
  136. /* Validate tail before using it since it is user writable. */
  137. if (tail >= rq->size)
  138. tail = 0;
  139. if (unlikely(tail == wq->head)) {
  140. ret = 0;
  141. goto unlock;
  142. }
  143. /* Make sure entry is read after head index is read. */
  144. smp_rmb();
  145. wqe = rvt_get_rwqe_ptr(rq, tail);
  146. /*
  147. * Even though we update the tail index in memory, the verbs
  148. * consumer is not supposed to post more entries until a
  149. * completion is generated.
  150. */
  151. if (++tail >= rq->size)
  152. tail = 0;
  153. wq->tail = tail;
  154. if (!wr_id_only && !init_sge(qp, wqe)) {
  155. ret = -1;
  156. goto unlock;
  157. }
  158. qp->r_wr_id = wqe->wr_id;
  159. ret = 1;
  160. set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
  161. if (handler) {
  162. u32 n;
  163. /*
  164. * Validate head pointer value and compute
  165. * the number of remaining WQEs.
  166. */
  167. n = wq->head;
  168. if (n >= rq->size)
  169. n = 0;
  170. if (n < tail)
  171. n += rq->size - tail;
  172. else
  173. n -= tail;
  174. if (n < srq->limit) {
  175. struct ib_event ev;
  176. srq->limit = 0;
  177. spin_unlock_irqrestore(&rq->lock, flags);
  178. ev.device = qp->ibqp.device;
  179. ev.element.srq = qp->ibqp.srq;
  180. ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
  181. handler(&ev, srq->ibsrq.srq_context);
  182. goto bail;
  183. }
  184. }
  185. unlock:
  186. spin_unlock_irqrestore(&rq->lock, flags);
  187. bail:
  188. return ret;
  189. }
  190. static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
  191. {
  192. return (gid->global.interface_id == id &&
  193. (gid->global.subnet_prefix == gid_prefix ||
  194. gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
  195. }
  196. /*
  197. *
  198. * This should be called with the QP r_lock held.
  199. *
  200. * The s_lock will be acquired around the hfi1_migrate_qp() call.
  201. */
  202. int hfi1_ruc_check_hdr(struct hfi1_ibport *ibp, struct ib_header *hdr,
  203. int has_grh, struct rvt_qp *qp, u32 bth0)
  204. {
  205. __be64 guid;
  206. unsigned long flags;
  207. u8 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
  208. if (qp->s_mig_state == IB_MIG_ARMED && (bth0 & IB_BTH_MIG_REQ)) {
  209. if (!has_grh) {
  210. if (qp->alt_ah_attr.ah_flags & IB_AH_GRH)
  211. goto err;
  212. } else {
  213. if (!(qp->alt_ah_attr.ah_flags & IB_AH_GRH))
  214. goto err;
  215. guid = get_sguid(ibp, qp->alt_ah_attr.grh.sgid_index);
  216. if (!gid_ok(&hdr->u.l.grh.dgid, ibp->rvp.gid_prefix,
  217. guid))
  218. goto err;
  219. if (!gid_ok(
  220. &hdr->u.l.grh.sgid,
  221. qp->alt_ah_attr.grh.dgid.global.subnet_prefix,
  222. qp->alt_ah_attr.grh.dgid.global.interface_id))
  223. goto err;
  224. }
  225. if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0,
  226. sc5, be16_to_cpu(hdr->lrh[3])))) {
  227. hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
  228. (u16)bth0,
  229. (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
  230. 0, qp->ibqp.qp_num,
  231. be16_to_cpu(hdr->lrh[3]),
  232. be16_to_cpu(hdr->lrh[1]));
  233. goto err;
  234. }
  235. /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
  236. if (be16_to_cpu(hdr->lrh[3]) != qp->alt_ah_attr.dlid ||
  237. ppd_from_ibp(ibp)->port != qp->alt_ah_attr.port_num)
  238. goto err;
  239. spin_lock_irqsave(&qp->s_lock, flags);
  240. hfi1_migrate_qp(qp);
  241. spin_unlock_irqrestore(&qp->s_lock, flags);
  242. } else {
  243. if (!has_grh) {
  244. if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
  245. goto err;
  246. } else {
  247. if (!(qp->remote_ah_attr.ah_flags & IB_AH_GRH))
  248. goto err;
  249. guid = get_sguid(ibp,
  250. qp->remote_ah_attr.grh.sgid_index);
  251. if (!gid_ok(&hdr->u.l.grh.dgid, ibp->rvp.gid_prefix,
  252. guid))
  253. goto err;
  254. if (!gid_ok(
  255. &hdr->u.l.grh.sgid,
  256. qp->remote_ah_attr.grh.dgid.global.subnet_prefix,
  257. qp->remote_ah_attr.grh.dgid.global.interface_id))
  258. goto err;
  259. }
  260. if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0,
  261. sc5, be16_to_cpu(hdr->lrh[3])))) {
  262. hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
  263. (u16)bth0,
  264. (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
  265. 0, qp->ibqp.qp_num,
  266. be16_to_cpu(hdr->lrh[3]),
  267. be16_to_cpu(hdr->lrh[1]));
  268. goto err;
  269. }
  270. /* Validate the SLID. See Ch. 9.6.1.5 */
  271. if (be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid ||
  272. ppd_from_ibp(ibp)->port != qp->port_num)
  273. goto err;
  274. if (qp->s_mig_state == IB_MIG_REARM &&
  275. !(bth0 & IB_BTH_MIG_REQ))
  276. qp->s_mig_state = IB_MIG_ARMED;
  277. }
  278. return 0;
  279. err:
  280. return 1;
  281. }
  282. /**
  283. * ruc_loopback - handle UC and RC loopback requests
  284. * @sqp: the sending QP
  285. *
  286. * This is called from hfi1_do_send() to
  287. * forward a WQE addressed to the same HFI.
  288. * Note that although we are single threaded due to the send engine, we still
  289. * have to protect against post_send(). We don't have to worry about
  290. * receive interrupts since this is a connected protocol and all packets
  291. * will pass through here.
  292. */
  293. static void ruc_loopback(struct rvt_qp *sqp)
  294. {
  295. struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
  296. struct rvt_qp *qp;
  297. struct rvt_swqe *wqe;
  298. struct rvt_sge *sge;
  299. unsigned long flags;
  300. struct ib_wc wc;
  301. u64 sdata;
  302. atomic64_t *maddr;
  303. enum ib_wc_status send_status;
  304. bool release;
  305. int ret;
  306. bool copy_last = false;
  307. int local_ops = 0;
  308. rcu_read_lock();
  309. /*
  310. * Note that we check the responder QP state after
  311. * checking the requester's state.
  312. */
  313. qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
  314. sqp->remote_qpn);
  315. spin_lock_irqsave(&sqp->s_lock, flags);
  316. /* Return if we are already busy processing a work request. */
  317. if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
  318. !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
  319. goto unlock;
  320. sqp->s_flags |= RVT_S_BUSY;
  321. again:
  322. smp_read_barrier_depends(); /* see post_one_send() */
  323. if (sqp->s_last == ACCESS_ONCE(sqp->s_head))
  324. goto clr_busy;
  325. wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
  326. /* Return if it is not OK to start a new work request. */
  327. if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
  328. if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
  329. goto clr_busy;
  330. /* We are in the error state, flush the work request. */
  331. send_status = IB_WC_WR_FLUSH_ERR;
  332. goto flush_send;
  333. }
  334. /*
  335. * We can rely on the entry not changing without the s_lock
  336. * being held until we update s_last.
  337. * We increment s_cur to indicate s_last is in progress.
  338. */
  339. if (sqp->s_last == sqp->s_cur) {
  340. if (++sqp->s_cur >= sqp->s_size)
  341. sqp->s_cur = 0;
  342. }
  343. spin_unlock_irqrestore(&sqp->s_lock, flags);
  344. if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
  345. qp->ibqp.qp_type != sqp->ibqp.qp_type) {
  346. ibp->rvp.n_pkt_drops++;
  347. /*
  348. * For RC, the requester would timeout and retry so
  349. * shortcut the timeouts and just signal too many retries.
  350. */
  351. if (sqp->ibqp.qp_type == IB_QPT_RC)
  352. send_status = IB_WC_RETRY_EXC_ERR;
  353. else
  354. send_status = IB_WC_SUCCESS;
  355. goto serr;
  356. }
  357. memset(&wc, 0, sizeof(wc));
  358. send_status = IB_WC_SUCCESS;
  359. release = true;
  360. sqp->s_sge.sge = wqe->sg_list[0];
  361. sqp->s_sge.sg_list = wqe->sg_list + 1;
  362. sqp->s_sge.num_sge = wqe->wr.num_sge;
  363. sqp->s_len = wqe->length;
  364. switch (wqe->wr.opcode) {
  365. case IB_WR_REG_MR:
  366. goto send_comp;
  367. case IB_WR_LOCAL_INV:
  368. if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
  369. if (rvt_invalidate_rkey(sqp,
  370. wqe->wr.ex.invalidate_rkey))
  371. send_status = IB_WC_LOC_PROT_ERR;
  372. local_ops = 1;
  373. }
  374. goto send_comp;
  375. case IB_WR_SEND_WITH_INV:
  376. if (!rvt_invalidate_rkey(qp, wqe->wr.ex.invalidate_rkey)) {
  377. wc.wc_flags = IB_WC_WITH_INVALIDATE;
  378. wc.ex.invalidate_rkey = wqe->wr.ex.invalidate_rkey;
  379. }
  380. goto send;
  381. case IB_WR_SEND_WITH_IMM:
  382. wc.wc_flags = IB_WC_WITH_IMM;
  383. wc.ex.imm_data = wqe->wr.ex.imm_data;
  384. /* FALLTHROUGH */
  385. case IB_WR_SEND:
  386. send:
  387. ret = hfi1_rvt_get_rwqe(qp, 0);
  388. if (ret < 0)
  389. goto op_err;
  390. if (!ret)
  391. goto rnr_nak;
  392. break;
  393. case IB_WR_RDMA_WRITE_WITH_IMM:
  394. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
  395. goto inv_err;
  396. wc.wc_flags = IB_WC_WITH_IMM;
  397. wc.ex.imm_data = wqe->wr.ex.imm_data;
  398. ret = hfi1_rvt_get_rwqe(qp, 1);
  399. if (ret < 0)
  400. goto op_err;
  401. if (!ret)
  402. goto rnr_nak;
  403. /* skip copy_last set and qp_access_flags recheck */
  404. goto do_write;
  405. case IB_WR_RDMA_WRITE:
  406. copy_last = rvt_is_user_qp(qp);
  407. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
  408. goto inv_err;
  409. do_write:
  410. if (wqe->length == 0)
  411. break;
  412. if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
  413. wqe->rdma_wr.remote_addr,
  414. wqe->rdma_wr.rkey,
  415. IB_ACCESS_REMOTE_WRITE)))
  416. goto acc_err;
  417. qp->r_sge.sg_list = NULL;
  418. qp->r_sge.num_sge = 1;
  419. qp->r_sge.total_len = wqe->length;
  420. break;
  421. case IB_WR_RDMA_READ:
  422. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
  423. goto inv_err;
  424. if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
  425. wqe->rdma_wr.remote_addr,
  426. wqe->rdma_wr.rkey,
  427. IB_ACCESS_REMOTE_READ)))
  428. goto acc_err;
  429. release = false;
  430. sqp->s_sge.sg_list = NULL;
  431. sqp->s_sge.num_sge = 1;
  432. qp->r_sge.sge = wqe->sg_list[0];
  433. qp->r_sge.sg_list = wqe->sg_list + 1;
  434. qp->r_sge.num_sge = wqe->wr.num_sge;
  435. qp->r_sge.total_len = wqe->length;
  436. break;
  437. case IB_WR_ATOMIC_CMP_AND_SWP:
  438. case IB_WR_ATOMIC_FETCH_AND_ADD:
  439. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
  440. goto inv_err;
  441. if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
  442. wqe->atomic_wr.remote_addr,
  443. wqe->atomic_wr.rkey,
  444. IB_ACCESS_REMOTE_ATOMIC)))
  445. goto acc_err;
  446. /* Perform atomic OP and save result. */
  447. maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
  448. sdata = wqe->atomic_wr.compare_add;
  449. *(u64 *)sqp->s_sge.sge.vaddr =
  450. (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
  451. (u64)atomic64_add_return(sdata, maddr) - sdata :
  452. (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
  453. sdata, wqe->atomic_wr.swap);
  454. rvt_put_mr(qp->r_sge.sge.mr);
  455. qp->r_sge.num_sge = 0;
  456. goto send_comp;
  457. default:
  458. send_status = IB_WC_LOC_QP_OP_ERR;
  459. goto serr;
  460. }
  461. sge = &sqp->s_sge.sge;
  462. while (sqp->s_len) {
  463. u32 len = sqp->s_len;
  464. if (len > sge->length)
  465. len = sge->length;
  466. if (len > sge->sge_length)
  467. len = sge->sge_length;
  468. WARN_ON_ONCE(len == 0);
  469. hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, release, copy_last);
  470. sge->vaddr += len;
  471. sge->length -= len;
  472. sge->sge_length -= len;
  473. if (sge->sge_length == 0) {
  474. if (!release)
  475. rvt_put_mr(sge->mr);
  476. if (--sqp->s_sge.num_sge)
  477. *sge = *sqp->s_sge.sg_list++;
  478. } else if (sge->length == 0 && sge->mr->lkey) {
  479. if (++sge->n >= RVT_SEGSZ) {
  480. if (++sge->m >= sge->mr->mapsz)
  481. break;
  482. sge->n = 0;
  483. }
  484. sge->vaddr =
  485. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  486. sge->length =
  487. sge->mr->map[sge->m]->segs[sge->n].length;
  488. }
  489. sqp->s_len -= len;
  490. }
  491. if (release)
  492. rvt_put_ss(&qp->r_sge);
  493. if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
  494. goto send_comp;
  495. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  496. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  497. else
  498. wc.opcode = IB_WC_RECV;
  499. wc.wr_id = qp->r_wr_id;
  500. wc.status = IB_WC_SUCCESS;
  501. wc.byte_len = wqe->length;
  502. wc.qp = &qp->ibqp;
  503. wc.src_qp = qp->remote_qpn;
  504. wc.slid = qp->remote_ah_attr.dlid;
  505. wc.sl = qp->remote_ah_attr.sl;
  506. wc.port_num = 1;
  507. /* Signal completion event if the solicited bit is set. */
  508. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  509. wqe->wr.send_flags & IB_SEND_SOLICITED);
  510. send_comp:
  511. spin_lock_irqsave(&sqp->s_lock, flags);
  512. ibp->rvp.n_loop_pkts++;
  513. flush_send:
  514. sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
  515. hfi1_send_complete(sqp, wqe, send_status);
  516. if (local_ops) {
  517. atomic_dec(&sqp->local_ops_pending);
  518. local_ops = 0;
  519. }
  520. goto again;
  521. rnr_nak:
  522. /* Handle RNR NAK */
  523. if (qp->ibqp.qp_type == IB_QPT_UC)
  524. goto send_comp;
  525. ibp->rvp.n_rnr_naks++;
  526. /*
  527. * Note: we don't need the s_lock held since the BUSY flag
  528. * makes this single threaded.
  529. */
  530. if (sqp->s_rnr_retry == 0) {
  531. send_status = IB_WC_RNR_RETRY_EXC_ERR;
  532. goto serr;
  533. }
  534. if (sqp->s_rnr_retry_cnt < 7)
  535. sqp->s_rnr_retry--;
  536. spin_lock_irqsave(&sqp->s_lock, flags);
  537. if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
  538. goto clr_busy;
  539. rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
  540. IB_AETH_CREDIT_SHIFT);
  541. goto clr_busy;
  542. op_err:
  543. send_status = IB_WC_REM_OP_ERR;
  544. wc.status = IB_WC_LOC_QP_OP_ERR;
  545. goto err;
  546. inv_err:
  547. send_status = IB_WC_REM_INV_REQ_ERR;
  548. wc.status = IB_WC_LOC_QP_OP_ERR;
  549. goto err;
  550. acc_err:
  551. send_status = IB_WC_REM_ACCESS_ERR;
  552. wc.status = IB_WC_LOC_PROT_ERR;
  553. err:
  554. /* responder goes to error state */
  555. rvt_rc_error(qp, wc.status);
  556. serr:
  557. spin_lock_irqsave(&sqp->s_lock, flags);
  558. hfi1_send_complete(sqp, wqe, send_status);
  559. if (sqp->ibqp.qp_type == IB_QPT_RC) {
  560. int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
  561. sqp->s_flags &= ~RVT_S_BUSY;
  562. spin_unlock_irqrestore(&sqp->s_lock, flags);
  563. if (lastwqe) {
  564. struct ib_event ev;
  565. ev.device = sqp->ibqp.device;
  566. ev.element.qp = &sqp->ibqp;
  567. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  568. sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
  569. }
  570. goto done;
  571. }
  572. clr_busy:
  573. sqp->s_flags &= ~RVT_S_BUSY;
  574. unlock:
  575. spin_unlock_irqrestore(&sqp->s_lock, flags);
  576. done:
  577. rcu_read_unlock();
  578. }
  579. /**
  580. * hfi1_make_grh - construct a GRH header
  581. * @ibp: a pointer to the IB port
  582. * @hdr: a pointer to the GRH header being constructed
  583. * @grh: the global route address to send to
  584. * @hwords: the number of 32 bit words of header being sent
  585. * @nwords: the number of 32 bit words of data being sent
  586. *
  587. * Return the size of the header in 32 bit words.
  588. */
  589. u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
  590. struct ib_global_route *grh, u32 hwords, u32 nwords)
  591. {
  592. hdr->version_tclass_flow =
  593. cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
  594. (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
  595. (grh->flow_label << IB_GRH_FLOW_SHIFT));
  596. hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
  597. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  598. hdr->next_hdr = IB_GRH_NEXT_HDR;
  599. hdr->hop_limit = grh->hop_limit;
  600. /* The SGID is 32-bit aligned. */
  601. hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
  602. hdr->sgid.global.interface_id =
  603. grh->sgid_index < HFI1_GUIDS_PER_PORT ?
  604. get_sguid(ibp, grh->sgid_index) :
  605. get_sguid(ibp, HFI1_PORT_GUID_INDEX);
  606. hdr->dgid = grh->dgid;
  607. /* GRH header size in 32-bit words. */
  608. return sizeof(struct ib_grh) / sizeof(u32);
  609. }
  610. #define BTH2_OFFSET (offsetof(struct hfi1_sdma_header, hdr.u.oth.bth[2]) / 4)
  611. /**
  612. * build_ahg - create ahg in s_ahg
  613. * @qp: a pointer to QP
  614. * @npsn: the next PSN for the request/response
  615. *
  616. * This routine handles the AHG by allocating an ahg entry and causing the
  617. * copy of the first middle.
  618. *
  619. * Subsequent middles use the copied entry, editing the
  620. * PSN with 1 or 2 edits.
  621. */
  622. static inline void build_ahg(struct rvt_qp *qp, u32 npsn)
  623. {
  624. struct hfi1_qp_priv *priv = qp->priv;
  625. if (unlikely(qp->s_flags & RVT_S_AHG_CLEAR))
  626. clear_ahg(qp);
  627. if (!(qp->s_flags & RVT_S_AHG_VALID)) {
  628. /* first middle that needs copy */
  629. if (qp->s_ahgidx < 0)
  630. qp->s_ahgidx = sdma_ahg_alloc(priv->s_sde);
  631. if (qp->s_ahgidx >= 0) {
  632. qp->s_ahgpsn = npsn;
  633. priv->s_ahg->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
  634. /* save to protect a change in another thread */
  635. priv->s_ahg->ahgidx = qp->s_ahgidx;
  636. qp->s_flags |= RVT_S_AHG_VALID;
  637. }
  638. } else {
  639. /* subsequent middle after valid */
  640. if (qp->s_ahgidx >= 0) {
  641. priv->s_ahg->tx_flags |= SDMA_TXREQ_F_USE_AHG;
  642. priv->s_ahg->ahgidx = qp->s_ahgidx;
  643. priv->s_ahg->ahgcount++;
  644. priv->s_ahg->ahgdesc[0] =
  645. sdma_build_ahg_descriptor(
  646. (__force u16)cpu_to_be16((u16)npsn),
  647. BTH2_OFFSET,
  648. 16,
  649. 16);
  650. if ((npsn & 0xffff0000) !=
  651. (qp->s_ahgpsn & 0xffff0000)) {
  652. priv->s_ahg->ahgcount++;
  653. priv->s_ahg->ahgdesc[1] =
  654. sdma_build_ahg_descriptor(
  655. (__force u16)cpu_to_be16(
  656. (u16)(npsn >> 16)),
  657. BTH2_OFFSET,
  658. 0,
  659. 16);
  660. }
  661. }
  662. }
  663. }
  664. void hfi1_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
  665. u32 bth0, u32 bth2, int middle,
  666. struct hfi1_pkt_state *ps)
  667. {
  668. struct hfi1_qp_priv *priv = qp->priv;
  669. struct hfi1_ibport *ibp = ps->ibp;
  670. u16 lrh0;
  671. u32 nwords;
  672. u32 extra_bytes;
  673. u32 bth1;
  674. /* Construct the header. */
  675. extra_bytes = -ps->s_txreq->s_cur_size & 3;
  676. nwords = (ps->s_txreq->s_cur_size + extra_bytes) >> 2;
  677. lrh0 = HFI1_LRH_BTH;
  678. if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
  679. qp->s_hdrwords += hfi1_make_grh(ibp,
  680. &ps->s_txreq->phdr.hdr.u.l.grh,
  681. &qp->remote_ah_attr.grh,
  682. qp->s_hdrwords, nwords);
  683. lrh0 = HFI1_LRH_GRH;
  684. middle = 0;
  685. }
  686. lrh0 |= (priv->s_sc & 0xf) << 12 | (qp->remote_ah_attr.sl & 0xf) << 4;
  687. /*
  688. * reset s_ahg/AHG fields
  689. *
  690. * This insures that the ahgentry/ahgcount
  691. * are at a non-AHG default to protect
  692. * build_verbs_tx_desc() from using
  693. * an include ahgidx.
  694. *
  695. * build_ahg() will modify as appropriate
  696. * to use the AHG feature.
  697. */
  698. priv->s_ahg->tx_flags = 0;
  699. priv->s_ahg->ahgcount = 0;
  700. priv->s_ahg->ahgidx = 0;
  701. if (qp->s_mig_state == IB_MIG_MIGRATED)
  702. bth0 |= IB_BTH_MIG_REQ;
  703. else
  704. middle = 0;
  705. if (middle)
  706. build_ahg(qp, bth2);
  707. else
  708. qp->s_flags &= ~RVT_S_AHG_VALID;
  709. ps->s_txreq->phdr.hdr.lrh[0] = cpu_to_be16(lrh0);
  710. ps->s_txreq->phdr.hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
  711. ps->s_txreq->phdr.hdr.lrh[2] =
  712. cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
  713. ps->s_txreq->phdr.hdr.lrh[3] = cpu_to_be16(ppd_from_ibp(ibp)->lid |
  714. qp->remote_ah_attr.src_path_bits);
  715. bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
  716. bth0 |= extra_bytes << 20;
  717. ohdr->bth[0] = cpu_to_be32(bth0);
  718. bth1 = qp->remote_qpn;
  719. if (qp->s_flags & RVT_S_ECN) {
  720. qp->s_flags &= ~RVT_S_ECN;
  721. /* we recently received a FECN, so return a BECN */
  722. bth1 |= (HFI1_BECN_MASK << HFI1_BECN_SHIFT);
  723. }
  724. ohdr->bth[1] = cpu_to_be32(bth1);
  725. ohdr->bth[2] = cpu_to_be32(bth2);
  726. }
  727. /* when sending, force a reschedule every one of these periods */
  728. #define SEND_RESCHED_TIMEOUT (5 * HZ) /* 5s in jiffies */
  729. void _hfi1_do_send(struct work_struct *work)
  730. {
  731. struct iowait *wait = container_of(work, struct iowait, iowork);
  732. struct rvt_qp *qp = iowait_to_qp(wait);
  733. hfi1_do_send(qp);
  734. }
  735. /**
  736. * hfi1_do_send - perform a send on a QP
  737. * @work: contains a pointer to the QP
  738. *
  739. * Process entries in the send work queue until credit or queue is
  740. * exhausted. Only allow one CPU to send a packet per QP.
  741. * Otherwise, two threads could send packets out of order.
  742. */
  743. void hfi1_do_send(struct rvt_qp *qp)
  744. {
  745. struct hfi1_pkt_state ps;
  746. struct hfi1_qp_priv *priv = qp->priv;
  747. int (*make_req)(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
  748. unsigned long timeout;
  749. unsigned long timeout_int;
  750. int cpu;
  751. ps.dev = to_idev(qp->ibqp.device);
  752. ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
  753. ps.ppd = ppd_from_ibp(ps.ibp);
  754. switch (qp->ibqp.qp_type) {
  755. case IB_QPT_RC:
  756. if (!loopback && ((qp->remote_ah_attr.dlid & ~((1 << ps.ppd->lmc
  757. ) - 1)) ==
  758. ps.ppd->lid)) {
  759. ruc_loopback(qp);
  760. return;
  761. }
  762. make_req = hfi1_make_rc_req;
  763. timeout_int = (qp->timeout_jiffies);
  764. break;
  765. case IB_QPT_UC:
  766. if (!loopback && ((qp->remote_ah_attr.dlid & ~((1 << ps.ppd->lmc
  767. ) - 1)) ==
  768. ps.ppd->lid)) {
  769. ruc_loopback(qp);
  770. return;
  771. }
  772. make_req = hfi1_make_uc_req;
  773. timeout_int = SEND_RESCHED_TIMEOUT;
  774. break;
  775. default:
  776. make_req = hfi1_make_ud_req;
  777. timeout_int = SEND_RESCHED_TIMEOUT;
  778. }
  779. spin_lock_irqsave(&qp->s_lock, ps.flags);
  780. /* Return if we are already busy processing a work request. */
  781. if (!hfi1_send_ok(qp)) {
  782. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  783. return;
  784. }
  785. qp->s_flags |= RVT_S_BUSY;
  786. timeout = jiffies + (timeout_int) / 8;
  787. cpu = priv->s_sde ? priv->s_sde->cpu :
  788. cpumask_first(cpumask_of_node(ps.ppd->dd->node));
  789. /* insure a pre-built packet is handled */
  790. ps.s_txreq = get_waiting_verbs_txreq(qp);
  791. do {
  792. /* Check for a constructed packet to be sent. */
  793. if (qp->s_hdrwords != 0) {
  794. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  795. /*
  796. * If the packet cannot be sent now, return and
  797. * the send engine will be woken up later.
  798. */
  799. if (hfi1_verbs_send(qp, &ps))
  800. return;
  801. /* Record that s_ahg is empty. */
  802. qp->s_hdrwords = 0;
  803. /* allow other tasks to run */
  804. if (unlikely(time_after(jiffies, timeout))) {
  805. if (workqueue_congested(cpu,
  806. ps.ppd->hfi1_wq)) {
  807. spin_lock_irqsave(
  808. &qp->s_lock,
  809. ps.flags);
  810. qp->s_flags &= ~RVT_S_BUSY;
  811. hfi1_schedule_send(qp);
  812. spin_unlock_irqrestore(
  813. &qp->s_lock,
  814. ps.flags);
  815. this_cpu_inc(
  816. *ps.ppd->dd->send_schedule);
  817. return;
  818. }
  819. if (!irqs_disabled()) {
  820. cond_resched();
  821. this_cpu_inc(
  822. *ps.ppd->dd->send_schedule);
  823. }
  824. timeout = jiffies + (timeout_int) / 8;
  825. }
  826. spin_lock_irqsave(&qp->s_lock, ps.flags);
  827. }
  828. } while (make_req(qp, &ps));
  829. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  830. }
  831. /*
  832. * This should be called with s_lock held.
  833. */
  834. void hfi1_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
  835. enum ib_wc_status status)
  836. {
  837. u32 old_last, last;
  838. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
  839. return;
  840. last = qp->s_last;
  841. old_last = last;
  842. if (++last >= qp->s_size)
  843. last = 0;
  844. qp->s_last = last;
  845. /* See post_send() */
  846. barrier();
  847. rvt_put_swqe(wqe);
  848. if (qp->ibqp.qp_type == IB_QPT_UD ||
  849. qp->ibqp.qp_type == IB_QPT_SMI ||
  850. qp->ibqp.qp_type == IB_QPT_GSI)
  851. atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
  852. rvt_qp_swqe_complete(qp, wqe, status);
  853. if (qp->s_acked == old_last)
  854. qp->s_acked = last;
  855. if (qp->s_cur == old_last)
  856. qp->s_cur = last;
  857. if (qp->s_tail == old_last)
  858. qp->s_tail = last;
  859. if (qp->state == IB_QPS_SQD && last == qp->s_cur)
  860. qp->s_draining = 0;
  861. }