qib_ruc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. if (wqe->length > qp->r_len)
  253. goto inv_err;
  254. break;
  255. case IB_WR_RDMA_WRITE_WITH_IMM:
  256. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
  257. goto inv_err;
  258. wc.wc_flags = IB_WC_WITH_IMM;
  259. wc.ex.imm_data = wqe->wr.ex.imm_data;
  260. ret = rvt_get_rwqe(qp, true);
  261. if (ret < 0)
  262. goto op_err;
  263. if (!ret)
  264. goto rnr_nak;
  265. /* FALLTHROUGH */
  266. case IB_WR_RDMA_WRITE:
  267. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
  268. goto inv_err;
  269. if (wqe->length == 0)
  270. break;
  271. if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
  272. wqe->rdma_wr.remote_addr,
  273. wqe->rdma_wr.rkey,
  274. IB_ACCESS_REMOTE_WRITE)))
  275. goto acc_err;
  276. qp->r_sge.sg_list = NULL;
  277. qp->r_sge.num_sge = 1;
  278. qp->r_sge.total_len = wqe->length;
  279. break;
  280. case IB_WR_RDMA_READ:
  281. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
  282. goto inv_err;
  283. if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
  284. wqe->rdma_wr.remote_addr,
  285. wqe->rdma_wr.rkey,
  286. IB_ACCESS_REMOTE_READ)))
  287. goto acc_err;
  288. release = 0;
  289. sqp->s_sge.sg_list = NULL;
  290. sqp->s_sge.num_sge = 1;
  291. qp->r_sge.sge = wqe->sg_list[0];
  292. qp->r_sge.sg_list = wqe->sg_list + 1;
  293. qp->r_sge.num_sge = wqe->wr.num_sge;
  294. qp->r_sge.total_len = wqe->length;
  295. break;
  296. case IB_WR_ATOMIC_CMP_AND_SWP:
  297. case IB_WR_ATOMIC_FETCH_AND_ADD:
  298. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
  299. goto inv_err;
  300. if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
  301. wqe->atomic_wr.remote_addr,
  302. wqe->atomic_wr.rkey,
  303. IB_ACCESS_REMOTE_ATOMIC)))
  304. goto acc_err;
  305. /* Perform atomic OP and save result. */
  306. maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
  307. sdata = wqe->atomic_wr.compare_add;
  308. *(u64 *) sqp->s_sge.sge.vaddr =
  309. (wqe->atomic_wr.wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
  310. (u64) atomic64_add_return(sdata, maddr) - sdata :
  311. (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
  312. sdata, wqe->atomic_wr.swap);
  313. rvt_put_mr(qp->r_sge.sge.mr);
  314. qp->r_sge.num_sge = 0;
  315. goto send_comp;
  316. default:
  317. send_status = IB_WC_LOC_QP_OP_ERR;
  318. goto serr;
  319. }
  320. sge = &sqp->s_sge.sge;
  321. while (sqp->s_len) {
  322. u32 len = sqp->s_len;
  323. if (len > sge->length)
  324. len = sge->length;
  325. if (len > sge->sge_length)
  326. len = sge->sge_length;
  327. BUG_ON(len == 0);
  328. qib_copy_sge(&qp->r_sge, sge->vaddr, len, release);
  329. sge->vaddr += len;
  330. sge->length -= len;
  331. sge->sge_length -= len;
  332. if (sge->sge_length == 0) {
  333. if (!release)
  334. rvt_put_mr(sge->mr);
  335. if (--sqp->s_sge.num_sge)
  336. *sge = *sqp->s_sge.sg_list++;
  337. } else if (sge->length == 0 && sge->mr->lkey) {
  338. if (++sge->n >= RVT_SEGSZ) {
  339. if (++sge->m >= sge->mr->mapsz)
  340. break;
  341. sge->n = 0;
  342. }
  343. sge->vaddr =
  344. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  345. sge->length =
  346. sge->mr->map[sge->m]->segs[sge->n].length;
  347. }
  348. sqp->s_len -= len;
  349. }
  350. if (release)
  351. rvt_put_ss(&qp->r_sge);
  352. if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
  353. goto send_comp;
  354. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  355. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  356. else
  357. wc.opcode = IB_WC_RECV;
  358. wc.wr_id = qp->r_wr_id;
  359. wc.status = IB_WC_SUCCESS;
  360. wc.byte_len = wqe->length;
  361. wc.qp = &qp->ibqp;
  362. wc.src_qp = qp->remote_qpn;
  363. wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr);
  364. wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
  365. wc.port_num = 1;
  366. /* Signal completion event if the solicited bit is set. */
  367. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  368. wqe->wr.send_flags & IB_SEND_SOLICITED);
  369. send_comp:
  370. spin_lock_irqsave(&sqp->s_lock, flags);
  371. ibp->rvp.n_loop_pkts++;
  372. flush_send:
  373. sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
  374. qib_send_complete(sqp, wqe, send_status);
  375. goto again;
  376. rnr_nak:
  377. /* Handle RNR NAK */
  378. if (qp->ibqp.qp_type == IB_QPT_UC)
  379. goto send_comp;
  380. ibp->rvp.n_rnr_naks++;
  381. /*
  382. * Note: we don't need the s_lock held since the BUSY flag
  383. * makes this single threaded.
  384. */
  385. if (sqp->s_rnr_retry == 0) {
  386. send_status = IB_WC_RNR_RETRY_EXC_ERR;
  387. goto serr;
  388. }
  389. if (sqp->s_rnr_retry_cnt < 7)
  390. sqp->s_rnr_retry--;
  391. spin_lock_irqsave(&sqp->s_lock, flags);
  392. if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
  393. goto clr_busy;
  394. rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
  395. IB_AETH_CREDIT_SHIFT);
  396. goto clr_busy;
  397. op_err:
  398. send_status = IB_WC_REM_OP_ERR;
  399. wc.status = IB_WC_LOC_QP_OP_ERR;
  400. goto err;
  401. inv_err:
  402. send_status =
  403. sqp->ibqp.qp_type == IB_QPT_RC ?
  404. IB_WC_REM_INV_REQ_ERR :
  405. IB_WC_SUCCESS;
  406. wc.status = IB_WC_LOC_QP_OP_ERR;
  407. goto err;
  408. acc_err:
  409. send_status = IB_WC_REM_ACCESS_ERR;
  410. wc.status = IB_WC_LOC_PROT_ERR;
  411. err:
  412. /* responder goes to error state */
  413. rvt_rc_error(qp, wc.status);
  414. serr:
  415. spin_lock_irqsave(&sqp->s_lock, flags);
  416. qib_send_complete(sqp, wqe, send_status);
  417. if (sqp->ibqp.qp_type == IB_QPT_RC) {
  418. int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
  419. sqp->s_flags &= ~RVT_S_BUSY;
  420. spin_unlock_irqrestore(&sqp->s_lock, flags);
  421. if (lastwqe) {
  422. struct ib_event ev;
  423. ev.device = sqp->ibqp.device;
  424. ev.element.qp = &sqp->ibqp;
  425. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  426. sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
  427. }
  428. goto done;
  429. }
  430. clr_busy:
  431. sqp->s_flags &= ~RVT_S_BUSY;
  432. unlock:
  433. spin_unlock_irqrestore(&sqp->s_lock, flags);
  434. done:
  435. rcu_read_unlock();
  436. }
  437. /**
  438. * qib_make_grh - construct a GRH header
  439. * @ibp: a pointer to the IB port
  440. * @hdr: a pointer to the GRH header being constructed
  441. * @grh: the global route address to send to
  442. * @hwords: the number of 32 bit words of header being sent
  443. * @nwords: the number of 32 bit words of data being sent
  444. *
  445. * Return the size of the header in 32 bit words.
  446. */
  447. u32 qib_make_grh(struct qib_ibport *ibp, struct ib_grh *hdr,
  448. const struct ib_global_route *grh, u32 hwords, u32 nwords)
  449. {
  450. hdr->version_tclass_flow =
  451. cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
  452. (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
  453. (grh->flow_label << IB_GRH_FLOW_SHIFT));
  454. hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
  455. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  456. hdr->next_hdr = IB_GRH_NEXT_HDR;
  457. hdr->hop_limit = grh->hop_limit;
  458. /* The SGID is 32-bit aligned. */
  459. hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
  460. if (!grh->sgid_index)
  461. hdr->sgid.global.interface_id = ppd_from_ibp(ibp)->guid;
  462. else if (grh->sgid_index < QIB_GUIDS_PER_PORT)
  463. hdr->sgid.global.interface_id = ibp->guids[grh->sgid_index - 1];
  464. hdr->dgid = grh->dgid;
  465. /* GRH header size in 32-bit words. */
  466. return sizeof(struct ib_grh) / sizeof(u32);
  467. }
  468. void qib_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
  469. u32 bth0, u32 bth2)
  470. {
  471. struct qib_qp_priv *priv = qp->priv;
  472. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  473. u16 lrh0;
  474. u32 nwords;
  475. u32 extra_bytes;
  476. /* Construct the header. */
  477. extra_bytes = -qp->s_cur_size & 3;
  478. nwords = (qp->s_cur_size + extra_bytes) >> 2;
  479. lrh0 = QIB_LRH_BTH;
  480. if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
  481. qp->s_hdrwords +=
  482. qib_make_grh(ibp, &priv->s_hdr->u.l.grh,
  483. rdma_ah_read_grh(&qp->remote_ah_attr),
  484. qp->s_hdrwords, nwords);
  485. lrh0 = QIB_LRH_GRH;
  486. }
  487. lrh0 |= ibp->sl_to_vl[rdma_ah_get_sl(&qp->remote_ah_attr)] << 12 |
  488. rdma_ah_get_sl(&qp->remote_ah_attr) << 4;
  489. priv->s_hdr->lrh[0] = cpu_to_be16(lrh0);
  490. priv->s_hdr->lrh[1] =
  491. cpu_to_be16(rdma_ah_get_dlid(&qp->remote_ah_attr));
  492. priv->s_hdr->lrh[2] =
  493. cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
  494. priv->s_hdr->lrh[3] =
  495. cpu_to_be16(ppd_from_ibp(ibp)->lid |
  496. rdma_ah_get_path_bits(&qp->remote_ah_attr));
  497. bth0 |= qib_get_pkey(ibp, qp->s_pkey_index);
  498. bth0 |= extra_bytes << 20;
  499. if (qp->s_mig_state == IB_MIG_MIGRATED)
  500. bth0 |= IB_BTH_MIG_REQ;
  501. ohdr->bth[0] = cpu_to_be32(bth0);
  502. ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
  503. ohdr->bth[2] = cpu_to_be32(bth2);
  504. this_cpu_inc(ibp->pmastats->n_unicast_xmit);
  505. }
  506. void _qib_do_send(struct work_struct *work)
  507. {
  508. struct qib_qp_priv *priv = container_of(work, struct qib_qp_priv,
  509. s_work);
  510. struct rvt_qp *qp = priv->owner;
  511. qib_do_send(qp);
  512. }
  513. /**
  514. * qib_do_send - perform a send on a QP
  515. * @qp: pointer to the QP
  516. *
  517. * Process entries in the send work queue until credit or queue is
  518. * exhausted. Only allow one CPU to send a packet per QP (tasklet).
  519. * Otherwise, two threads could send packets out of order.
  520. */
  521. void qib_do_send(struct rvt_qp *qp)
  522. {
  523. struct qib_qp_priv *priv = qp->priv;
  524. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  525. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  526. int (*make_req)(struct rvt_qp *qp, unsigned long *flags);
  527. unsigned long flags;
  528. if ((qp->ibqp.qp_type == IB_QPT_RC ||
  529. qp->ibqp.qp_type == IB_QPT_UC) &&
  530. (rdma_ah_get_dlid(&qp->remote_ah_attr) &
  531. ~((1 << ppd->lmc) - 1)) == ppd->lid) {
  532. qib_ruc_loopback(qp);
  533. return;
  534. }
  535. if (qp->ibqp.qp_type == IB_QPT_RC)
  536. make_req = qib_make_rc_req;
  537. else if (qp->ibqp.qp_type == IB_QPT_UC)
  538. make_req = qib_make_uc_req;
  539. else
  540. make_req = qib_make_ud_req;
  541. spin_lock_irqsave(&qp->s_lock, flags);
  542. /* Return if we are already busy processing a work request. */
  543. if (!qib_send_ok(qp)) {
  544. spin_unlock_irqrestore(&qp->s_lock, flags);
  545. return;
  546. }
  547. qp->s_flags |= RVT_S_BUSY;
  548. do {
  549. /* Check for a constructed packet to be sent. */
  550. if (qp->s_hdrwords != 0) {
  551. spin_unlock_irqrestore(&qp->s_lock, flags);
  552. /*
  553. * If the packet cannot be sent now, return and
  554. * the send tasklet will be woken up later.
  555. */
  556. if (qib_verbs_send(qp, priv->s_hdr, qp->s_hdrwords,
  557. qp->s_cur_sge, qp->s_cur_size))
  558. return;
  559. /* Record that s_hdr is empty. */
  560. qp->s_hdrwords = 0;
  561. spin_lock_irqsave(&qp->s_lock, flags);
  562. }
  563. } while (make_req(qp, &flags));
  564. spin_unlock_irqrestore(&qp->s_lock, flags);
  565. }
  566. /*
  567. * This should be called with s_lock held.
  568. */
  569. void qib_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
  570. enum ib_wc_status status)
  571. {
  572. u32 old_last, last;
  573. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
  574. return;
  575. last = qp->s_last;
  576. old_last = last;
  577. if (++last >= qp->s_size)
  578. last = 0;
  579. qp->s_last = last;
  580. /* See post_send() */
  581. barrier();
  582. rvt_put_swqe(wqe);
  583. if (qp->ibqp.qp_type == IB_QPT_UD ||
  584. qp->ibqp.qp_type == IB_QPT_SMI ||
  585. qp->ibqp.qp_type == IB_QPT_GSI)
  586. atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
  587. rvt_qp_swqe_complete(qp,
  588. wqe,
  589. ib_qib_wc_opcode[wqe->wr.opcode],
  590. status);
  591. if (qp->s_acked == old_last)
  592. qp->s_acked = last;
  593. if (qp->s_cur == old_last)
  594. qp->s_cur = last;
  595. if (qp->s_tail == old_last)
  596. qp->s_tail = last;
  597. if (qp->state == IB_QPS_SQD && last == qp->s_cur)
  598. qp->s_draining = 0;
  599. }