qib_ruc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
  3. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/spinlock.h>
  34. #include <rdma/ib_smi.h>
  35. #include "qib.h"
  36. #include "qib_mad.h"
  37. /*
  38. * Validate a RWQE and fill in the SGE state.
  39. * Return 1 if OK.
  40. */
  41. static int qib_init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
  42. {
  43. int i, j, ret;
  44. struct ib_wc wc;
  45. struct rvt_lkey_table *rkt;
  46. struct rvt_pd *pd;
  47. struct rvt_sge_state *ss;
  48. rkt = &to_idev(qp->ibqp.device)->rdi.lkey_table;
  49. pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
  50. ss = &qp->r_sge;
  51. ss->sg_list = qp->r_sg_list;
  52. qp->r_len = 0;
  53. for (i = j = 0; i < wqe->num_sge; i++) {
  54. if (wqe->sg_list[i].length == 0)
  55. continue;
  56. /* Check LKEY */
  57. ret = rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
  58. NULL, &wqe->sg_list[i],
  59. IB_ACCESS_LOCAL_WRITE);
  60. if (unlikely(ret <= 0))
  61. goto bad_lkey;
  62. qp->r_len += wqe->sg_list[i].length;
  63. j++;
  64. }
  65. ss->num_sge = j;
  66. ss->total_len = qp->r_len;
  67. ret = 1;
  68. goto bail;
  69. bad_lkey:
  70. while (j) {
  71. struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
  72. rvt_put_mr(sge->mr);
  73. }
  74. ss->num_sge = 0;
  75. memset(&wc, 0, sizeof(wc));
  76. wc.wr_id = wqe->wr_id;
  77. wc.status = IB_WC_LOC_PROT_ERR;
  78. wc.opcode = IB_WC_RECV;
  79. wc.qp = &qp->ibqp;
  80. /* Signal solicited completion event. */
  81. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  82. ret = 0;
  83. bail:
  84. return ret;
  85. }
  86. /**
  87. * qib_get_rwqe - copy the next RWQE into the QP's RWQE
  88. * @qp: the QP
  89. * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
  90. *
  91. * Return -1 if there is a local error, 0 if no RWQE is available,
  92. * otherwise return 1.
  93. *
  94. * Can be called from interrupt level.
  95. */
  96. int qib_get_rwqe(struct rvt_qp *qp, int wr_id_only)
  97. {
  98. unsigned long flags;
  99. struct rvt_rq *rq;
  100. struct rvt_rwq *wq;
  101. struct rvt_srq *srq;
  102. struct rvt_rwqe *wqe;
  103. void (*handler)(struct ib_event *, void *);
  104. u32 tail;
  105. int ret;
  106. if (qp->ibqp.srq) {
  107. srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
  108. handler = srq->ibsrq.event_handler;
  109. rq = &srq->rq;
  110. } else {
  111. srq = NULL;
  112. handler = NULL;
  113. rq = &qp->r_rq;
  114. }
  115. spin_lock_irqsave(&rq->lock, flags);
  116. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
  117. ret = 0;
  118. goto unlock;
  119. }
  120. wq = rq->wq;
  121. tail = wq->tail;
  122. /* Validate tail before using it since it is user writable. */
  123. if (tail >= rq->size)
  124. tail = 0;
  125. if (unlikely(tail == wq->head)) {
  126. ret = 0;
  127. goto unlock;
  128. }
  129. /* Make sure entry is read after head index is read. */
  130. smp_rmb();
  131. wqe = rvt_get_rwqe_ptr(rq, tail);
  132. /*
  133. * Even though we update the tail index in memory, the verbs
  134. * consumer is not supposed to post more entries until a
  135. * completion is generated.
  136. */
  137. if (++tail >= rq->size)
  138. tail = 0;
  139. wq->tail = tail;
  140. if (!wr_id_only && !qib_init_sge(qp, wqe)) {
  141. ret = -1;
  142. goto unlock;
  143. }
  144. qp->r_wr_id = wqe->wr_id;
  145. ret = 1;
  146. set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
  147. if (handler) {
  148. u32 n;
  149. /*
  150. * Validate head pointer value and compute
  151. * the number of remaining WQEs.
  152. */
  153. n = wq->head;
  154. if (n >= rq->size)
  155. n = 0;
  156. if (n < tail)
  157. n += rq->size - tail;
  158. else
  159. n -= tail;
  160. if (n < srq->limit) {
  161. struct ib_event ev;
  162. srq->limit = 0;
  163. spin_unlock_irqrestore(&rq->lock, flags);
  164. ev.device = qp->ibqp.device;
  165. ev.element.srq = qp->ibqp.srq;
  166. ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
  167. handler(&ev, srq->ibsrq.srq_context);
  168. goto bail;
  169. }
  170. }
  171. unlock:
  172. spin_unlock_irqrestore(&rq->lock, flags);
  173. bail:
  174. return ret;
  175. }
  176. /*
  177. * Switch to alternate path.
  178. * The QP s_lock should be held and interrupts disabled.
  179. */
  180. void qib_migrate_qp(struct rvt_qp *qp)
  181. {
  182. struct ib_event ev;
  183. qp->s_mig_state = IB_MIG_MIGRATED;
  184. qp->remote_ah_attr = qp->alt_ah_attr;
  185. qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
  186. qp->s_pkey_index = qp->s_alt_pkey_index;
  187. ev.device = qp->ibqp.device;
  188. ev.element.qp = &qp->ibqp;
  189. ev.event = IB_EVENT_PATH_MIG;
  190. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  191. }
  192. static __be64 get_sguid(struct qib_ibport *ibp, unsigned index)
  193. {
  194. if (!index) {
  195. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  196. return ppd->guid;
  197. }
  198. return ibp->guids[index - 1];
  199. }
  200. static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
  201. {
  202. return (gid->global.interface_id == id &&
  203. (gid->global.subnet_prefix == gid_prefix ||
  204. gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
  205. }
  206. /*
  207. *
  208. * This should be called with the QP r_lock held.
  209. *
  210. * The s_lock will be acquired around the qib_migrate_qp() call.
  211. */
  212. int qib_ruc_check_hdr(struct qib_ibport *ibp, struct ib_header *hdr,
  213. int has_grh, struct rvt_qp *qp, u32 bth0)
  214. {
  215. __be64 guid;
  216. unsigned long flags;
  217. if (qp->s_mig_state == IB_MIG_ARMED && (bth0 & IB_BTH_MIG_REQ)) {
  218. if (!has_grh) {
  219. if (rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
  220. IB_AH_GRH)
  221. goto err;
  222. } else {
  223. const struct ib_global_route *grh;
  224. if (!(rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
  225. IB_AH_GRH))
  226. goto err;
  227. grh = rdma_ah_read_grh(&qp->alt_ah_attr);
  228. guid = get_sguid(ibp, grh->sgid_index);
  229. if (!gid_ok(&hdr->u.l.grh.dgid,
  230. ibp->rvp.gid_prefix, guid))
  231. goto err;
  232. if (!gid_ok(&hdr->u.l.grh.sgid,
  233. grh->dgid.global.subnet_prefix,
  234. grh->dgid.global.interface_id))
  235. goto err;
  236. }
  237. if (!qib_pkey_ok((u16)bth0,
  238. qib_get_pkey(ibp, qp->s_alt_pkey_index))) {
  239. qib_bad_pkey(ibp,
  240. (u16)bth0,
  241. (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
  242. 0, qp->ibqp.qp_num,
  243. hdr->lrh[3], hdr->lrh[1]);
  244. goto err;
  245. }
  246. /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
  247. if ((be16_to_cpu(hdr->lrh[3]) !=
  248. rdma_ah_get_dlid(&qp->alt_ah_attr)) ||
  249. ppd_from_ibp(ibp)->port !=
  250. rdma_ah_get_port_num(&qp->alt_ah_attr))
  251. goto err;
  252. spin_lock_irqsave(&qp->s_lock, flags);
  253. qib_migrate_qp(qp);
  254. spin_unlock_irqrestore(&qp->s_lock, flags);
  255. } else {
  256. if (!has_grh) {
  257. if (rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
  258. IB_AH_GRH)
  259. goto err;
  260. } else {
  261. const struct ib_global_route *grh;
  262. if (!(rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
  263. IB_AH_GRH))
  264. goto err;
  265. grh = rdma_ah_read_grh(&qp->remote_ah_attr);
  266. guid = get_sguid(ibp, grh->sgid_index);
  267. if (!gid_ok(&hdr->u.l.grh.dgid,
  268. ibp->rvp.gid_prefix, guid))
  269. goto err;
  270. if (!gid_ok(&hdr->u.l.grh.sgid,
  271. grh->dgid.global.subnet_prefix,
  272. grh->dgid.global.interface_id))
  273. goto err;
  274. }
  275. if (!qib_pkey_ok((u16)bth0,
  276. qib_get_pkey(ibp, qp->s_pkey_index))) {
  277. qib_bad_pkey(ibp,
  278. (u16)bth0,
  279. (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
  280. 0, qp->ibqp.qp_num,
  281. hdr->lrh[3], hdr->lrh[1]);
  282. goto err;
  283. }
  284. /* Validate the SLID. See Ch. 9.6.1.5 */
  285. if (be16_to_cpu(hdr->lrh[3]) !=
  286. rdma_ah_get_dlid(&qp->remote_ah_attr) ||
  287. ppd_from_ibp(ibp)->port != qp->port_num)
  288. goto err;
  289. if (qp->s_mig_state == IB_MIG_REARM &&
  290. !(bth0 & IB_BTH_MIG_REQ))
  291. qp->s_mig_state = IB_MIG_ARMED;
  292. }
  293. return 0;
  294. err:
  295. return 1;
  296. }
  297. /**
  298. * qib_ruc_loopback - handle UC and RC lookback requests
  299. * @sqp: the sending QP
  300. *
  301. * This is called from qib_do_send() to
  302. * forward a WQE addressed to the same HCA.
  303. * Note that although we are single threaded due to the tasklet, we still
  304. * have to protect against post_send(). We don't have to worry about
  305. * receive interrupts since this is a connected protocol and all packets
  306. * will pass through here.
  307. */
  308. static void qib_ruc_loopback(struct rvt_qp *sqp)
  309. {
  310. struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
  311. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  312. struct qib_devdata *dd = ppd->dd;
  313. struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
  314. struct rvt_qp *qp;
  315. struct rvt_swqe *wqe;
  316. struct rvt_sge *sge;
  317. unsigned long flags;
  318. struct ib_wc wc;
  319. u64 sdata;
  320. atomic64_t *maddr;
  321. enum ib_wc_status send_status;
  322. int release;
  323. int ret;
  324. rcu_read_lock();
  325. /*
  326. * Note that we check the responder QP state after
  327. * checking the requester's state.
  328. */
  329. qp = rvt_lookup_qpn(rdi, &ibp->rvp, sqp->remote_qpn);
  330. if (!qp)
  331. goto done;
  332. spin_lock_irqsave(&sqp->s_lock, flags);
  333. /* Return if we are already busy processing a work request. */
  334. if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
  335. !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
  336. goto unlock;
  337. sqp->s_flags |= RVT_S_BUSY;
  338. again:
  339. smp_read_barrier_depends(); /* see post_one_send() */
  340. if (sqp->s_last == ACCESS_ONCE(sqp->s_head))
  341. goto clr_busy;
  342. wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
  343. /* Return if it is not OK to start a new work reqeust. */
  344. if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
  345. if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
  346. goto clr_busy;
  347. /* We are in the error state, flush the work request. */
  348. send_status = IB_WC_WR_FLUSH_ERR;
  349. goto flush_send;
  350. }
  351. /*
  352. * We can rely on the entry not changing without the s_lock
  353. * being held until we update s_last.
  354. * We increment s_cur to indicate s_last is in progress.
  355. */
  356. if (sqp->s_last == sqp->s_cur) {
  357. if (++sqp->s_cur >= sqp->s_size)
  358. sqp->s_cur = 0;
  359. }
  360. spin_unlock_irqrestore(&sqp->s_lock, flags);
  361. if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
  362. qp->ibqp.qp_type != sqp->ibqp.qp_type) {
  363. ibp->rvp.n_pkt_drops++;
  364. /*
  365. * For RC, the requester would timeout and retry so
  366. * shortcut the timeouts and just signal too many retries.
  367. */
  368. if (sqp->ibqp.qp_type == IB_QPT_RC)
  369. send_status = IB_WC_RETRY_EXC_ERR;
  370. else
  371. send_status = IB_WC_SUCCESS;
  372. goto serr;
  373. }
  374. memset(&wc, 0, sizeof(wc));
  375. send_status = IB_WC_SUCCESS;
  376. release = 1;
  377. sqp->s_sge.sge = wqe->sg_list[0];
  378. sqp->s_sge.sg_list = wqe->sg_list + 1;
  379. sqp->s_sge.num_sge = wqe->wr.num_sge;
  380. sqp->s_len = wqe->length;
  381. switch (wqe->wr.opcode) {
  382. case IB_WR_SEND_WITH_IMM:
  383. wc.wc_flags = IB_WC_WITH_IMM;
  384. wc.ex.imm_data = wqe->wr.ex.imm_data;
  385. /* FALLTHROUGH */
  386. case IB_WR_SEND:
  387. ret = qib_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 = qib_get_rwqe(qp, 1);
  399. if (ret < 0)
  400. goto op_err;
  401. if (!ret)
  402. goto rnr_nak;
  403. /* FALLTHROUGH */
  404. case IB_WR_RDMA_WRITE:
  405. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
  406. goto inv_err;
  407. if (wqe->length == 0)
  408. break;
  409. if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
  410. wqe->rdma_wr.remote_addr,
  411. wqe->rdma_wr.rkey,
  412. IB_ACCESS_REMOTE_WRITE)))
  413. goto acc_err;
  414. qp->r_sge.sg_list = NULL;
  415. qp->r_sge.num_sge = 1;
  416. qp->r_sge.total_len = wqe->length;
  417. break;
  418. case IB_WR_RDMA_READ:
  419. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
  420. goto inv_err;
  421. if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
  422. wqe->rdma_wr.remote_addr,
  423. wqe->rdma_wr.rkey,
  424. IB_ACCESS_REMOTE_READ)))
  425. goto acc_err;
  426. release = 0;
  427. sqp->s_sge.sg_list = NULL;
  428. sqp->s_sge.num_sge = 1;
  429. qp->r_sge.sge = wqe->sg_list[0];
  430. qp->r_sge.sg_list = wqe->sg_list + 1;
  431. qp->r_sge.num_sge = wqe->wr.num_sge;
  432. qp->r_sge.total_len = wqe->length;
  433. break;
  434. case IB_WR_ATOMIC_CMP_AND_SWP:
  435. case IB_WR_ATOMIC_FETCH_AND_ADD:
  436. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
  437. goto inv_err;
  438. if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
  439. wqe->atomic_wr.remote_addr,
  440. wqe->atomic_wr.rkey,
  441. IB_ACCESS_REMOTE_ATOMIC)))
  442. goto acc_err;
  443. /* Perform atomic OP and save result. */
  444. maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
  445. sdata = wqe->atomic_wr.compare_add;
  446. *(u64 *) sqp->s_sge.sge.vaddr =
  447. (wqe->atomic_wr.wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
  448. (u64) atomic64_add_return(sdata, maddr) - sdata :
  449. (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
  450. sdata, wqe->atomic_wr.swap);
  451. rvt_put_mr(qp->r_sge.sge.mr);
  452. qp->r_sge.num_sge = 0;
  453. goto send_comp;
  454. default:
  455. send_status = IB_WC_LOC_QP_OP_ERR;
  456. goto serr;
  457. }
  458. sge = &sqp->s_sge.sge;
  459. while (sqp->s_len) {
  460. u32 len = sqp->s_len;
  461. if (len > sge->length)
  462. len = sge->length;
  463. if (len > sge->sge_length)
  464. len = sge->sge_length;
  465. BUG_ON(len == 0);
  466. qib_copy_sge(&qp->r_sge, sge->vaddr, len, release);
  467. sge->vaddr += len;
  468. sge->length -= len;
  469. sge->sge_length -= len;
  470. if (sge->sge_length == 0) {
  471. if (!release)
  472. rvt_put_mr(sge->mr);
  473. if (--sqp->s_sge.num_sge)
  474. *sge = *sqp->s_sge.sg_list++;
  475. } else if (sge->length == 0 && sge->mr->lkey) {
  476. if (++sge->n >= RVT_SEGSZ) {
  477. if (++sge->m >= sge->mr->mapsz)
  478. break;
  479. sge->n = 0;
  480. }
  481. sge->vaddr =
  482. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  483. sge->length =
  484. sge->mr->map[sge->m]->segs[sge->n].length;
  485. }
  486. sqp->s_len -= len;
  487. }
  488. if (release)
  489. rvt_put_ss(&qp->r_sge);
  490. if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
  491. goto send_comp;
  492. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  493. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  494. else
  495. wc.opcode = IB_WC_RECV;
  496. wc.wr_id = qp->r_wr_id;
  497. wc.status = IB_WC_SUCCESS;
  498. wc.byte_len = wqe->length;
  499. wc.qp = &qp->ibqp;
  500. wc.src_qp = qp->remote_qpn;
  501. wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr);
  502. wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
  503. wc.port_num = 1;
  504. /* Signal completion event if the solicited bit is set. */
  505. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  506. wqe->wr.send_flags & IB_SEND_SOLICITED);
  507. send_comp:
  508. spin_lock_irqsave(&sqp->s_lock, flags);
  509. ibp->rvp.n_loop_pkts++;
  510. flush_send:
  511. sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
  512. qib_send_complete(sqp, wqe, send_status);
  513. goto again;
  514. rnr_nak:
  515. /* Handle RNR NAK */
  516. if (qp->ibqp.qp_type == IB_QPT_UC)
  517. goto send_comp;
  518. ibp->rvp.n_rnr_naks++;
  519. /*
  520. * Note: we don't need the s_lock held since the BUSY flag
  521. * makes this single threaded.
  522. */
  523. if (sqp->s_rnr_retry == 0) {
  524. send_status = IB_WC_RNR_RETRY_EXC_ERR;
  525. goto serr;
  526. }
  527. if (sqp->s_rnr_retry_cnt < 7)
  528. sqp->s_rnr_retry--;
  529. spin_lock_irqsave(&sqp->s_lock, flags);
  530. if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
  531. goto clr_busy;
  532. rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
  533. IB_AETH_CREDIT_SHIFT);
  534. goto clr_busy;
  535. op_err:
  536. send_status = IB_WC_REM_OP_ERR;
  537. wc.status = IB_WC_LOC_QP_OP_ERR;
  538. goto err;
  539. inv_err:
  540. send_status = IB_WC_REM_INV_REQ_ERR;
  541. wc.status = IB_WC_LOC_QP_OP_ERR;
  542. goto err;
  543. acc_err:
  544. send_status = IB_WC_REM_ACCESS_ERR;
  545. wc.status = IB_WC_LOC_PROT_ERR;
  546. err:
  547. /* responder goes to error state */
  548. rvt_rc_error(qp, wc.status);
  549. serr:
  550. spin_lock_irqsave(&sqp->s_lock, flags);
  551. qib_send_complete(sqp, wqe, send_status);
  552. if (sqp->ibqp.qp_type == IB_QPT_RC) {
  553. int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
  554. sqp->s_flags &= ~RVT_S_BUSY;
  555. spin_unlock_irqrestore(&sqp->s_lock, flags);
  556. if (lastwqe) {
  557. struct ib_event ev;
  558. ev.device = sqp->ibqp.device;
  559. ev.element.qp = &sqp->ibqp;
  560. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  561. sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
  562. }
  563. goto done;
  564. }
  565. clr_busy:
  566. sqp->s_flags &= ~RVT_S_BUSY;
  567. unlock:
  568. spin_unlock_irqrestore(&sqp->s_lock, flags);
  569. done:
  570. rcu_read_unlock();
  571. }
  572. /**
  573. * qib_make_grh - construct a GRH header
  574. * @ibp: a pointer to the IB port
  575. * @hdr: a pointer to the GRH header being constructed
  576. * @grh: the global route address to send to
  577. * @hwords: the number of 32 bit words of header being sent
  578. * @nwords: the number of 32 bit words of data being sent
  579. *
  580. * Return the size of the header in 32 bit words.
  581. */
  582. u32 qib_make_grh(struct qib_ibport *ibp, struct ib_grh *hdr,
  583. const struct ib_global_route *grh, u32 hwords, u32 nwords)
  584. {
  585. hdr->version_tclass_flow =
  586. cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
  587. (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
  588. (grh->flow_label << IB_GRH_FLOW_SHIFT));
  589. hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
  590. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  591. hdr->next_hdr = IB_GRH_NEXT_HDR;
  592. hdr->hop_limit = grh->hop_limit;
  593. /* The SGID is 32-bit aligned. */
  594. hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
  595. if (!grh->sgid_index)
  596. hdr->sgid.global.interface_id = ppd_from_ibp(ibp)->guid;
  597. else if (grh->sgid_index < QIB_GUIDS_PER_PORT)
  598. hdr->sgid.global.interface_id = ibp->guids[grh->sgid_index - 1];
  599. hdr->dgid = grh->dgid;
  600. /* GRH header size in 32-bit words. */
  601. return sizeof(struct ib_grh) / sizeof(u32);
  602. }
  603. void qib_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
  604. u32 bth0, u32 bth2)
  605. {
  606. struct qib_qp_priv *priv = qp->priv;
  607. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  608. u16 lrh0;
  609. u32 nwords;
  610. u32 extra_bytes;
  611. /* Construct the header. */
  612. extra_bytes = -qp->s_cur_size & 3;
  613. nwords = (qp->s_cur_size + extra_bytes) >> 2;
  614. lrh0 = QIB_LRH_BTH;
  615. if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
  616. qp->s_hdrwords +=
  617. qib_make_grh(ibp, &priv->s_hdr->u.l.grh,
  618. rdma_ah_read_grh(&qp->remote_ah_attr),
  619. qp->s_hdrwords, nwords);
  620. lrh0 = QIB_LRH_GRH;
  621. }
  622. lrh0 |= ibp->sl_to_vl[rdma_ah_get_sl(&qp->remote_ah_attr)] << 12 |
  623. rdma_ah_get_sl(&qp->remote_ah_attr) << 4;
  624. priv->s_hdr->lrh[0] = cpu_to_be16(lrh0);
  625. priv->s_hdr->lrh[1] =
  626. cpu_to_be16(rdma_ah_get_dlid(&qp->remote_ah_attr));
  627. priv->s_hdr->lrh[2] =
  628. cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
  629. priv->s_hdr->lrh[3] =
  630. cpu_to_be16(ppd_from_ibp(ibp)->lid |
  631. rdma_ah_get_path_bits(&qp->remote_ah_attr));
  632. bth0 |= qib_get_pkey(ibp, qp->s_pkey_index);
  633. bth0 |= extra_bytes << 20;
  634. if (qp->s_mig_state == IB_MIG_MIGRATED)
  635. bth0 |= IB_BTH_MIG_REQ;
  636. ohdr->bth[0] = cpu_to_be32(bth0);
  637. ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
  638. ohdr->bth[2] = cpu_to_be32(bth2);
  639. this_cpu_inc(ibp->pmastats->n_unicast_xmit);
  640. }
  641. void _qib_do_send(struct work_struct *work)
  642. {
  643. struct qib_qp_priv *priv = container_of(work, struct qib_qp_priv,
  644. s_work);
  645. struct rvt_qp *qp = priv->owner;
  646. qib_do_send(qp);
  647. }
  648. /**
  649. * qib_do_send - perform a send on a QP
  650. * @qp: pointer to the QP
  651. *
  652. * Process entries in the send work queue until credit or queue is
  653. * exhausted. Only allow one CPU to send a packet per QP (tasklet).
  654. * Otherwise, two threads could send packets out of order.
  655. */
  656. void qib_do_send(struct rvt_qp *qp)
  657. {
  658. struct qib_qp_priv *priv = qp->priv;
  659. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  660. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  661. int (*make_req)(struct rvt_qp *qp, unsigned long *flags);
  662. unsigned long flags;
  663. if ((qp->ibqp.qp_type == IB_QPT_RC ||
  664. qp->ibqp.qp_type == IB_QPT_UC) &&
  665. (rdma_ah_get_dlid(&qp->remote_ah_attr) &
  666. ~((1 << ppd->lmc) - 1)) == ppd->lid) {
  667. qib_ruc_loopback(qp);
  668. return;
  669. }
  670. if (qp->ibqp.qp_type == IB_QPT_RC)
  671. make_req = qib_make_rc_req;
  672. else if (qp->ibqp.qp_type == IB_QPT_UC)
  673. make_req = qib_make_uc_req;
  674. else
  675. make_req = qib_make_ud_req;
  676. spin_lock_irqsave(&qp->s_lock, flags);
  677. /* Return if we are already busy processing a work request. */
  678. if (!qib_send_ok(qp)) {
  679. spin_unlock_irqrestore(&qp->s_lock, flags);
  680. return;
  681. }
  682. qp->s_flags |= RVT_S_BUSY;
  683. do {
  684. /* Check for a constructed packet to be sent. */
  685. if (qp->s_hdrwords != 0) {
  686. spin_unlock_irqrestore(&qp->s_lock, flags);
  687. /*
  688. * If the packet cannot be sent now, return and
  689. * the send tasklet will be woken up later.
  690. */
  691. if (qib_verbs_send(qp, priv->s_hdr, qp->s_hdrwords,
  692. qp->s_cur_sge, qp->s_cur_size))
  693. return;
  694. /* Record that s_hdr is empty. */
  695. qp->s_hdrwords = 0;
  696. spin_lock_irqsave(&qp->s_lock, flags);
  697. }
  698. } while (make_req(qp, &flags));
  699. spin_unlock_irqrestore(&qp->s_lock, flags);
  700. }
  701. /*
  702. * This should be called with s_lock held.
  703. */
  704. void qib_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
  705. enum ib_wc_status status)
  706. {
  707. u32 old_last, last;
  708. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
  709. return;
  710. last = qp->s_last;
  711. old_last = last;
  712. if (++last >= qp->s_size)
  713. last = 0;
  714. qp->s_last = last;
  715. /* See post_send() */
  716. barrier();
  717. rvt_put_swqe(wqe);
  718. if (qp->ibqp.qp_type == IB_QPT_UD ||
  719. qp->ibqp.qp_type == IB_QPT_SMI ||
  720. qp->ibqp.qp_type == IB_QPT_GSI)
  721. atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
  722. rvt_qp_swqe_complete(qp,
  723. wqe,
  724. ib_qib_wc_opcode[wqe->wr.opcode],
  725. status);
  726. if (qp->s_acked == old_last)
  727. qp->s_acked = last;
  728. if (qp->s_cur == old_last)
  729. qp->s_cur = last;
  730. if (qp->s_tail == old_last)
  731. qp->s_tail = last;
  732. if (qp->state == IB_QPS_SQD && last == qp->s_cur)
  733. qp->s_draining = 0;
  734. }