qib_ruc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. * Switch to alternate path.
  39. * The QP s_lock should be held and interrupts disabled.
  40. */
  41. void qib_migrate_qp(struct rvt_qp *qp)
  42. {
  43. struct ib_event ev;
  44. qp->s_mig_state = IB_MIG_MIGRATED;
  45. qp->remote_ah_attr = qp->alt_ah_attr;
  46. qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
  47. qp->s_pkey_index = qp->s_alt_pkey_index;
  48. ev.device = qp->ibqp.device;
  49. ev.element.qp = &qp->ibqp;
  50. ev.event = IB_EVENT_PATH_MIG;
  51. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  52. }
  53. static __be64 get_sguid(struct qib_ibport *ibp, unsigned index)
  54. {
  55. if (!index) {
  56. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  57. return ppd->guid;
  58. }
  59. return ibp->guids[index - 1];
  60. }
  61. static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
  62. {
  63. return (gid->global.interface_id == id &&
  64. (gid->global.subnet_prefix == gid_prefix ||
  65. gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
  66. }
  67. /*
  68. *
  69. * This should be called with the QP r_lock held.
  70. *
  71. * The s_lock will be acquired around the qib_migrate_qp() call.
  72. */
  73. int qib_ruc_check_hdr(struct qib_ibport *ibp, struct ib_header *hdr,
  74. int has_grh, struct rvt_qp *qp, u32 bth0)
  75. {
  76. __be64 guid;
  77. unsigned long flags;
  78. if (qp->s_mig_state == IB_MIG_ARMED && (bth0 & IB_BTH_MIG_REQ)) {
  79. if (!has_grh) {
  80. if (rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
  81. IB_AH_GRH)
  82. goto err;
  83. } else {
  84. const struct ib_global_route *grh;
  85. if (!(rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
  86. IB_AH_GRH))
  87. goto err;
  88. grh = rdma_ah_read_grh(&qp->alt_ah_attr);
  89. guid = get_sguid(ibp, grh->sgid_index);
  90. if (!gid_ok(&hdr->u.l.grh.dgid,
  91. ibp->rvp.gid_prefix, guid))
  92. goto err;
  93. if (!gid_ok(&hdr->u.l.grh.sgid,
  94. grh->dgid.global.subnet_prefix,
  95. grh->dgid.global.interface_id))
  96. goto err;
  97. }
  98. if (!qib_pkey_ok((u16)bth0,
  99. qib_get_pkey(ibp, qp->s_alt_pkey_index))) {
  100. qib_bad_pkey(ibp,
  101. (u16)bth0,
  102. (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
  103. 0, qp->ibqp.qp_num,
  104. hdr->lrh[3], hdr->lrh[1]);
  105. goto err;
  106. }
  107. /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
  108. if ((be16_to_cpu(hdr->lrh[3]) !=
  109. rdma_ah_get_dlid(&qp->alt_ah_attr)) ||
  110. ppd_from_ibp(ibp)->port !=
  111. rdma_ah_get_port_num(&qp->alt_ah_attr))
  112. goto err;
  113. spin_lock_irqsave(&qp->s_lock, flags);
  114. qib_migrate_qp(qp);
  115. spin_unlock_irqrestore(&qp->s_lock, flags);
  116. } else {
  117. if (!has_grh) {
  118. if (rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
  119. IB_AH_GRH)
  120. goto err;
  121. } else {
  122. const struct ib_global_route *grh;
  123. if (!(rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
  124. IB_AH_GRH))
  125. goto err;
  126. grh = rdma_ah_read_grh(&qp->remote_ah_attr);
  127. guid = get_sguid(ibp, grh->sgid_index);
  128. if (!gid_ok(&hdr->u.l.grh.dgid,
  129. ibp->rvp.gid_prefix, guid))
  130. goto err;
  131. if (!gid_ok(&hdr->u.l.grh.sgid,
  132. grh->dgid.global.subnet_prefix,
  133. grh->dgid.global.interface_id))
  134. goto err;
  135. }
  136. if (!qib_pkey_ok((u16)bth0,
  137. qib_get_pkey(ibp, qp->s_pkey_index))) {
  138. qib_bad_pkey(ibp,
  139. (u16)bth0,
  140. (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
  141. 0, qp->ibqp.qp_num,
  142. hdr->lrh[3], hdr->lrh[1]);
  143. goto err;
  144. }
  145. /* Validate the SLID. See Ch. 9.6.1.5 */
  146. if (be16_to_cpu(hdr->lrh[3]) !=
  147. rdma_ah_get_dlid(&qp->remote_ah_attr) ||
  148. ppd_from_ibp(ibp)->port != qp->port_num)
  149. goto err;
  150. if (qp->s_mig_state == IB_MIG_REARM &&
  151. !(bth0 & IB_BTH_MIG_REQ))
  152. qp->s_mig_state = IB_MIG_ARMED;
  153. }
  154. return 0;
  155. err:
  156. return 1;
  157. }
  158. /**
  159. * qib_ruc_loopback - handle UC and RC lookback requests
  160. * @sqp: the sending QP
  161. *
  162. * This is called from qib_do_send() to
  163. * forward a WQE addressed to the same HCA.
  164. * Note that although we are single threaded due to the tasklet, we still
  165. * have to protect against post_send(). We don't have to worry about
  166. * receive interrupts since this is a connected protocol and all packets
  167. * will pass through here.
  168. */
  169. static void qib_ruc_loopback(struct rvt_qp *sqp)
  170. {
  171. struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
  172. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  173. struct qib_devdata *dd = ppd->dd;
  174. struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
  175. struct rvt_qp *qp;
  176. struct rvt_swqe *wqe;
  177. struct rvt_sge *sge;
  178. unsigned long flags;
  179. struct ib_wc wc;
  180. u64 sdata;
  181. atomic64_t *maddr;
  182. enum ib_wc_status send_status;
  183. int release;
  184. int ret;
  185. rcu_read_lock();
  186. /*
  187. * Note that we check the responder QP state after
  188. * checking the requester's state.
  189. */
  190. qp = rvt_lookup_qpn(rdi, &ibp->rvp, sqp->remote_qpn);
  191. if (!qp)
  192. goto done;
  193. spin_lock_irqsave(&sqp->s_lock, flags);
  194. /* Return if we are already busy processing a work request. */
  195. if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
  196. !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
  197. goto unlock;
  198. sqp->s_flags |= RVT_S_BUSY;
  199. again:
  200. if (sqp->s_last == READ_ONCE(sqp->s_head))
  201. goto clr_busy;
  202. wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
  203. /* Return if it is not OK to start a new work reqeust. */
  204. if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
  205. if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
  206. goto clr_busy;
  207. /* We are in the error state, flush the work request. */
  208. send_status = IB_WC_WR_FLUSH_ERR;
  209. goto flush_send;
  210. }
  211. /*
  212. * We can rely on the entry not changing without the s_lock
  213. * being held until we update s_last.
  214. * We increment s_cur to indicate s_last is in progress.
  215. */
  216. if (sqp->s_last == sqp->s_cur) {
  217. if (++sqp->s_cur >= sqp->s_size)
  218. sqp->s_cur = 0;
  219. }
  220. spin_unlock_irqrestore(&sqp->s_lock, flags);
  221. if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
  222. qp->ibqp.qp_type != sqp->ibqp.qp_type) {
  223. ibp->rvp.n_pkt_drops++;
  224. /*
  225. * For RC, the requester would timeout and retry so
  226. * shortcut the timeouts and just signal too many retries.
  227. */
  228. if (sqp->ibqp.qp_type == IB_QPT_RC)
  229. send_status = IB_WC_RETRY_EXC_ERR;
  230. else
  231. send_status = IB_WC_SUCCESS;
  232. goto serr;
  233. }
  234. memset(&wc, 0, sizeof(wc));
  235. send_status = IB_WC_SUCCESS;
  236. release = 1;
  237. sqp->s_sge.sge = wqe->sg_list[0];
  238. sqp->s_sge.sg_list = wqe->sg_list + 1;
  239. sqp->s_sge.num_sge = wqe->wr.num_sge;
  240. sqp->s_len = wqe->length;
  241. switch (wqe->wr.opcode) {
  242. case IB_WR_SEND_WITH_IMM:
  243. wc.wc_flags = IB_WC_WITH_IMM;
  244. wc.ex.imm_data = wqe->wr.ex.imm_data;
  245. /* FALLTHROUGH */
  246. case IB_WR_SEND:
  247. ret = rvt_get_rwqe(qp, false);
  248. if (ret < 0)
  249. goto op_err;
  250. if (!ret)
  251. goto rnr_nak;
  252. break;
  253. case IB_WR_RDMA_WRITE_WITH_IMM:
  254. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
  255. goto inv_err;
  256. wc.wc_flags = IB_WC_WITH_IMM;
  257. wc.ex.imm_data = wqe->wr.ex.imm_data;
  258. ret = rvt_get_rwqe(qp, true);
  259. if (ret < 0)
  260. goto op_err;
  261. if (!ret)
  262. goto rnr_nak;
  263. /* FALLTHROUGH */
  264. case IB_WR_RDMA_WRITE:
  265. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
  266. goto inv_err;
  267. if (wqe->length == 0)
  268. break;
  269. if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
  270. wqe->rdma_wr.remote_addr,
  271. wqe->rdma_wr.rkey,
  272. IB_ACCESS_REMOTE_WRITE)))
  273. goto acc_err;
  274. qp->r_sge.sg_list = NULL;
  275. qp->r_sge.num_sge = 1;
  276. qp->r_sge.total_len = wqe->length;
  277. break;
  278. case IB_WR_RDMA_READ:
  279. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
  280. goto inv_err;
  281. if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
  282. wqe->rdma_wr.remote_addr,
  283. wqe->rdma_wr.rkey,
  284. IB_ACCESS_REMOTE_READ)))
  285. goto acc_err;
  286. release = 0;
  287. sqp->s_sge.sg_list = NULL;
  288. sqp->s_sge.num_sge = 1;
  289. qp->r_sge.sge = wqe->sg_list[0];
  290. qp->r_sge.sg_list = wqe->sg_list + 1;
  291. qp->r_sge.num_sge = wqe->wr.num_sge;
  292. qp->r_sge.total_len = wqe->length;
  293. break;
  294. case IB_WR_ATOMIC_CMP_AND_SWP:
  295. case IB_WR_ATOMIC_FETCH_AND_ADD:
  296. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
  297. goto inv_err;
  298. if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
  299. wqe->atomic_wr.remote_addr,
  300. wqe->atomic_wr.rkey,
  301. IB_ACCESS_REMOTE_ATOMIC)))
  302. goto acc_err;
  303. /* Perform atomic OP and save result. */
  304. maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
  305. sdata = wqe->atomic_wr.compare_add;
  306. *(u64 *) sqp->s_sge.sge.vaddr =
  307. (wqe->atomic_wr.wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
  308. (u64) atomic64_add_return(sdata, maddr) - sdata :
  309. (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
  310. sdata, wqe->atomic_wr.swap);
  311. rvt_put_mr(qp->r_sge.sge.mr);
  312. qp->r_sge.num_sge = 0;
  313. goto send_comp;
  314. default:
  315. send_status = IB_WC_LOC_QP_OP_ERR;
  316. goto serr;
  317. }
  318. sge = &sqp->s_sge.sge;
  319. while (sqp->s_len) {
  320. u32 len = sqp->s_len;
  321. if (len > sge->length)
  322. len = sge->length;
  323. if (len > sge->sge_length)
  324. len = sge->sge_length;
  325. BUG_ON(len == 0);
  326. rvt_copy_sge(qp, &qp->r_sge, sge->vaddr, len, release, false);
  327. sge->vaddr += len;
  328. sge->length -= len;
  329. sge->sge_length -= len;
  330. if (sge->sge_length == 0) {
  331. if (!release)
  332. rvt_put_mr(sge->mr);
  333. if (--sqp->s_sge.num_sge)
  334. *sge = *sqp->s_sge.sg_list++;
  335. } else if (sge->length == 0 && sge->mr->lkey) {
  336. if (++sge->n >= RVT_SEGSZ) {
  337. if (++sge->m >= sge->mr->mapsz)
  338. break;
  339. sge->n = 0;
  340. }
  341. sge->vaddr =
  342. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  343. sge->length =
  344. sge->mr->map[sge->m]->segs[sge->n].length;
  345. }
  346. sqp->s_len -= len;
  347. }
  348. if (release)
  349. rvt_put_ss(&qp->r_sge);
  350. if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
  351. goto send_comp;
  352. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  353. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  354. else
  355. wc.opcode = IB_WC_RECV;
  356. wc.wr_id = qp->r_wr_id;
  357. wc.status = IB_WC_SUCCESS;
  358. wc.byte_len = wqe->length;
  359. wc.qp = &qp->ibqp;
  360. wc.src_qp = qp->remote_qpn;
  361. wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr);
  362. wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
  363. wc.port_num = 1;
  364. /* Signal completion event if the solicited bit is set. */
  365. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  366. wqe->wr.send_flags & IB_SEND_SOLICITED);
  367. send_comp:
  368. spin_lock_irqsave(&sqp->s_lock, flags);
  369. ibp->rvp.n_loop_pkts++;
  370. flush_send:
  371. sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
  372. rvt_send_complete(sqp, wqe, send_status);
  373. goto again;
  374. rnr_nak:
  375. /* Handle RNR NAK */
  376. if (qp->ibqp.qp_type == IB_QPT_UC)
  377. goto send_comp;
  378. ibp->rvp.n_rnr_naks++;
  379. /*
  380. * Note: we don't need the s_lock held since the BUSY flag
  381. * makes this single threaded.
  382. */
  383. if (sqp->s_rnr_retry == 0) {
  384. send_status = IB_WC_RNR_RETRY_EXC_ERR;
  385. goto serr;
  386. }
  387. if (sqp->s_rnr_retry_cnt < 7)
  388. sqp->s_rnr_retry--;
  389. spin_lock_irqsave(&sqp->s_lock, flags);
  390. if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
  391. goto clr_busy;
  392. rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
  393. IB_AETH_CREDIT_SHIFT);
  394. goto clr_busy;
  395. op_err:
  396. send_status = IB_WC_REM_OP_ERR;
  397. wc.status = IB_WC_LOC_QP_OP_ERR;
  398. goto err;
  399. inv_err:
  400. send_status = IB_WC_REM_INV_REQ_ERR;
  401. wc.status = IB_WC_LOC_QP_OP_ERR;
  402. goto err;
  403. acc_err:
  404. send_status = IB_WC_REM_ACCESS_ERR;
  405. wc.status = IB_WC_LOC_PROT_ERR;
  406. err:
  407. /* responder goes to error state */
  408. rvt_rc_error(qp, wc.status);
  409. serr:
  410. spin_lock_irqsave(&sqp->s_lock, flags);
  411. rvt_send_complete(sqp, wqe, send_status);
  412. if (sqp->ibqp.qp_type == IB_QPT_RC) {
  413. int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
  414. sqp->s_flags &= ~RVT_S_BUSY;
  415. spin_unlock_irqrestore(&sqp->s_lock, flags);
  416. if (lastwqe) {
  417. struct ib_event ev;
  418. ev.device = sqp->ibqp.device;
  419. ev.element.qp = &sqp->ibqp;
  420. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  421. sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
  422. }
  423. goto done;
  424. }
  425. clr_busy:
  426. sqp->s_flags &= ~RVT_S_BUSY;
  427. unlock:
  428. spin_unlock_irqrestore(&sqp->s_lock, flags);
  429. done:
  430. rcu_read_unlock();
  431. }
  432. /**
  433. * qib_make_grh - construct a GRH header
  434. * @ibp: a pointer to the IB port
  435. * @hdr: a pointer to the GRH header being constructed
  436. * @grh: the global route address to send to
  437. * @hwords: the number of 32 bit words of header being sent
  438. * @nwords: the number of 32 bit words of data being sent
  439. *
  440. * Return the size of the header in 32 bit words.
  441. */
  442. u32 qib_make_grh(struct qib_ibport *ibp, struct ib_grh *hdr,
  443. const struct ib_global_route *grh, u32 hwords, u32 nwords)
  444. {
  445. hdr->version_tclass_flow =
  446. cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
  447. (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
  448. (grh->flow_label << IB_GRH_FLOW_SHIFT));
  449. hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
  450. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  451. hdr->next_hdr = IB_GRH_NEXT_HDR;
  452. hdr->hop_limit = grh->hop_limit;
  453. /* The SGID is 32-bit aligned. */
  454. hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
  455. if (!grh->sgid_index)
  456. hdr->sgid.global.interface_id = ppd_from_ibp(ibp)->guid;
  457. else if (grh->sgid_index < QIB_GUIDS_PER_PORT)
  458. hdr->sgid.global.interface_id = ibp->guids[grh->sgid_index - 1];
  459. hdr->dgid = grh->dgid;
  460. /* GRH header size in 32-bit words. */
  461. return sizeof(struct ib_grh) / sizeof(u32);
  462. }
  463. void qib_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
  464. u32 bth0, u32 bth2)
  465. {
  466. struct qib_qp_priv *priv = qp->priv;
  467. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  468. u16 lrh0;
  469. u32 nwords;
  470. u32 extra_bytes;
  471. /* Construct the header. */
  472. extra_bytes = -qp->s_cur_size & 3;
  473. nwords = (qp->s_cur_size + extra_bytes) >> 2;
  474. lrh0 = QIB_LRH_BTH;
  475. if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
  476. qp->s_hdrwords +=
  477. qib_make_grh(ibp, &priv->s_hdr->u.l.grh,
  478. rdma_ah_read_grh(&qp->remote_ah_attr),
  479. qp->s_hdrwords, nwords);
  480. lrh0 = QIB_LRH_GRH;
  481. }
  482. lrh0 |= ibp->sl_to_vl[rdma_ah_get_sl(&qp->remote_ah_attr)] << 12 |
  483. rdma_ah_get_sl(&qp->remote_ah_attr) << 4;
  484. priv->s_hdr->lrh[0] = cpu_to_be16(lrh0);
  485. priv->s_hdr->lrh[1] =
  486. cpu_to_be16(rdma_ah_get_dlid(&qp->remote_ah_attr));
  487. priv->s_hdr->lrh[2] =
  488. cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
  489. priv->s_hdr->lrh[3] =
  490. cpu_to_be16(ppd_from_ibp(ibp)->lid |
  491. rdma_ah_get_path_bits(&qp->remote_ah_attr));
  492. bth0 |= qib_get_pkey(ibp, qp->s_pkey_index);
  493. bth0 |= extra_bytes << 20;
  494. if (qp->s_mig_state == IB_MIG_MIGRATED)
  495. bth0 |= IB_BTH_MIG_REQ;
  496. ohdr->bth[0] = cpu_to_be32(bth0);
  497. ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
  498. ohdr->bth[2] = cpu_to_be32(bth2);
  499. this_cpu_inc(ibp->pmastats->n_unicast_xmit);
  500. }
  501. void _qib_do_send(struct work_struct *work)
  502. {
  503. struct qib_qp_priv *priv = container_of(work, struct qib_qp_priv,
  504. s_work);
  505. struct rvt_qp *qp = priv->owner;
  506. qib_do_send(qp);
  507. }
  508. /**
  509. * qib_do_send - perform a send on a QP
  510. * @qp: pointer to the QP
  511. *
  512. * Process entries in the send work queue until credit or queue is
  513. * exhausted. Only allow one CPU to send a packet per QP (tasklet).
  514. * Otherwise, two threads could send packets out of order.
  515. */
  516. void qib_do_send(struct rvt_qp *qp)
  517. {
  518. struct qib_qp_priv *priv = qp->priv;
  519. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  520. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  521. int (*make_req)(struct rvt_qp *qp, unsigned long *flags);
  522. unsigned long flags;
  523. if ((qp->ibqp.qp_type == IB_QPT_RC ||
  524. qp->ibqp.qp_type == IB_QPT_UC) &&
  525. (rdma_ah_get_dlid(&qp->remote_ah_attr) &
  526. ~((1 << ppd->lmc) - 1)) == ppd->lid) {
  527. qib_ruc_loopback(qp);
  528. return;
  529. }
  530. if (qp->ibqp.qp_type == IB_QPT_RC)
  531. make_req = qib_make_rc_req;
  532. else if (qp->ibqp.qp_type == IB_QPT_UC)
  533. make_req = qib_make_uc_req;
  534. else
  535. make_req = qib_make_ud_req;
  536. spin_lock_irqsave(&qp->s_lock, flags);
  537. /* Return if we are already busy processing a work request. */
  538. if (!qib_send_ok(qp)) {
  539. spin_unlock_irqrestore(&qp->s_lock, flags);
  540. return;
  541. }
  542. qp->s_flags |= RVT_S_BUSY;
  543. do {
  544. /* Check for a constructed packet to be sent. */
  545. if (qp->s_hdrwords != 0) {
  546. spin_unlock_irqrestore(&qp->s_lock, flags);
  547. /*
  548. * If the packet cannot be sent now, return and
  549. * the send tasklet will be woken up later.
  550. */
  551. if (qib_verbs_send(qp, priv->s_hdr, qp->s_hdrwords,
  552. qp->s_cur_sge, qp->s_cur_size))
  553. return;
  554. /* Record that s_hdr is empty. */
  555. qp->s_hdrwords = 0;
  556. spin_lock_irqsave(&qp->s_lock, flags);
  557. }
  558. } while (make_req(qp, &flags));
  559. spin_unlock_irqrestore(&qp->s_lock, flags);
  560. }