qib_ruc.c 22 KB

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