qib_ruc.c 21 KB

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