ipath_ruc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. 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 "ipath_verbs.h"
  34. #include "ipath_common.h"
  35. /*
  36. * Convert the AETH RNR timeout code into the number of milliseconds.
  37. */
  38. const u32 ib_ipath_rnr_table[32] = {
  39. 656, /* 0 */
  40. 1, /* 1 */
  41. 1, /* 2 */
  42. 1, /* 3 */
  43. 1, /* 4 */
  44. 1, /* 5 */
  45. 1, /* 6 */
  46. 1, /* 7 */
  47. 1, /* 8 */
  48. 1, /* 9 */
  49. 1, /* A */
  50. 1, /* B */
  51. 1, /* C */
  52. 1, /* D */
  53. 2, /* E */
  54. 2, /* F */
  55. 3, /* 10 */
  56. 4, /* 11 */
  57. 6, /* 12 */
  58. 8, /* 13 */
  59. 11, /* 14 */
  60. 16, /* 15 */
  61. 21, /* 16 */
  62. 31, /* 17 */
  63. 41, /* 18 */
  64. 62, /* 19 */
  65. 82, /* 1A */
  66. 123, /* 1B */
  67. 164, /* 1C */
  68. 246, /* 1D */
  69. 328, /* 1E */
  70. 492 /* 1F */
  71. };
  72. /**
  73. * ipath_insert_rnr_queue - put QP on the RNR timeout list for the device
  74. * @qp: the QP
  75. *
  76. * XXX Use a simple list for now. We might need a priority
  77. * queue if we have lots of QPs waiting for RNR timeouts
  78. * but that should be rare.
  79. */
  80. void ipath_insert_rnr_queue(struct ipath_qp *qp)
  81. {
  82. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  83. unsigned long flags;
  84. spin_lock_irqsave(&dev->pending_lock, flags);
  85. if (list_empty(&dev->rnrwait))
  86. list_add(&qp->timerwait, &dev->rnrwait);
  87. else {
  88. struct list_head *l = &dev->rnrwait;
  89. struct ipath_qp *nqp = list_entry(l->next, struct ipath_qp,
  90. timerwait);
  91. while (qp->s_rnr_timeout >= nqp->s_rnr_timeout) {
  92. qp->s_rnr_timeout -= nqp->s_rnr_timeout;
  93. l = l->next;
  94. if (l->next == &dev->rnrwait)
  95. break;
  96. nqp = list_entry(l->next, struct ipath_qp,
  97. timerwait);
  98. }
  99. list_add(&qp->timerwait, l);
  100. }
  101. spin_unlock_irqrestore(&dev->pending_lock, flags);
  102. }
  103. /**
  104. * ipath_get_rwqe - copy the next RWQE into the QP's RWQE
  105. * @qp: the QP
  106. * @wr_id_only: update wr_id only, not SGEs
  107. *
  108. * Return 0 if no RWQE is available, otherwise return 1.
  109. *
  110. * Can be called from interrupt level.
  111. */
  112. int ipath_get_rwqe(struct ipath_qp *qp, int wr_id_only)
  113. {
  114. unsigned long flags;
  115. struct ipath_rq *rq;
  116. struct ipath_srq *srq;
  117. struct ipath_rwqe *wqe;
  118. int ret = 1;
  119. if (!qp->ibqp.srq) {
  120. rq = &qp->r_rq;
  121. spin_lock_irqsave(&rq->lock, flags);
  122. if (unlikely(rq->tail == rq->head)) {
  123. ret = 0;
  124. goto done;
  125. }
  126. wqe = get_rwqe_ptr(rq, rq->tail);
  127. qp->r_wr_id = wqe->wr_id;
  128. if (!wr_id_only) {
  129. qp->r_sge.sge = wqe->sg_list[0];
  130. qp->r_sge.sg_list = wqe->sg_list + 1;
  131. qp->r_sge.num_sge = wqe->num_sge;
  132. qp->r_len = wqe->length;
  133. }
  134. if (++rq->tail >= rq->size)
  135. rq->tail = 0;
  136. goto done;
  137. }
  138. srq = to_isrq(qp->ibqp.srq);
  139. rq = &srq->rq;
  140. spin_lock_irqsave(&rq->lock, flags);
  141. if (unlikely(rq->tail == rq->head)) {
  142. ret = 0;
  143. goto done;
  144. }
  145. wqe = get_rwqe_ptr(rq, rq->tail);
  146. qp->r_wr_id = wqe->wr_id;
  147. if (!wr_id_only) {
  148. qp->r_sge.sge = wqe->sg_list[0];
  149. qp->r_sge.sg_list = wqe->sg_list + 1;
  150. qp->r_sge.num_sge = wqe->num_sge;
  151. qp->r_len = wqe->length;
  152. }
  153. if (++rq->tail >= rq->size)
  154. rq->tail = 0;
  155. if (srq->ibsrq.event_handler) {
  156. struct ib_event ev;
  157. u32 n;
  158. if (rq->head < rq->tail)
  159. n = rq->size + rq->head - rq->tail;
  160. else
  161. n = rq->head - rq->tail;
  162. if (n < srq->limit) {
  163. srq->limit = 0;
  164. spin_unlock_irqrestore(&rq->lock, flags);
  165. ev.device = qp->ibqp.device;
  166. ev.element.srq = qp->ibqp.srq;
  167. ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
  168. srq->ibsrq.event_handler(&ev,
  169. srq->ibsrq.srq_context);
  170. goto bail;
  171. }
  172. }
  173. done:
  174. spin_unlock_irqrestore(&rq->lock, flags);
  175. bail:
  176. return ret;
  177. }
  178. /**
  179. * ipath_ruc_loopback - handle UC and RC lookback requests
  180. * @sqp: the loopback QP
  181. *
  182. * This is called from ipath_do_uc_send() or ipath_do_rc_send() to
  183. * forward a WQE addressed to the same HCA.
  184. * Note that although we are single threaded due to the tasklet, we still
  185. * have to protect against post_send(). We don't have to worry about
  186. * receive interrupts since this is a connected protocol and all packets
  187. * will pass through here.
  188. */
  189. static void ipath_ruc_loopback(struct ipath_qp *sqp)
  190. {
  191. struct ipath_ibdev *dev = to_idev(sqp->ibqp.device);
  192. struct ipath_qp *qp;
  193. struct ipath_swqe *wqe;
  194. struct ipath_sge *sge;
  195. unsigned long flags;
  196. struct ib_wc wc;
  197. u64 sdata;
  198. qp = ipath_lookup_qpn(&dev->qp_table, sqp->remote_qpn);
  199. if (!qp) {
  200. dev->n_pkt_drops++;
  201. return;
  202. }
  203. again:
  204. spin_lock_irqsave(&sqp->s_lock, flags);
  205. if (!(ib_ipath_state_ops[sqp->state] & IPATH_PROCESS_SEND_OK)) {
  206. spin_unlock_irqrestore(&sqp->s_lock, flags);
  207. goto done;
  208. }
  209. /* Get the next send request. */
  210. if (sqp->s_last == sqp->s_head) {
  211. /* Send work queue is empty. */
  212. spin_unlock_irqrestore(&sqp->s_lock, flags);
  213. goto done;
  214. }
  215. /*
  216. * We can rely on the entry not changing without the s_lock
  217. * being held until we update s_last.
  218. */
  219. wqe = get_swqe_ptr(sqp, sqp->s_last);
  220. spin_unlock_irqrestore(&sqp->s_lock, flags);
  221. wc.wc_flags = 0;
  222. wc.imm_data = 0;
  223. sqp->s_sge.sge = wqe->sg_list[0];
  224. sqp->s_sge.sg_list = wqe->sg_list + 1;
  225. sqp->s_sge.num_sge = wqe->wr.num_sge;
  226. sqp->s_len = wqe->length;
  227. switch (wqe->wr.opcode) {
  228. case IB_WR_SEND_WITH_IMM:
  229. wc.wc_flags = IB_WC_WITH_IMM;
  230. wc.imm_data = wqe->wr.imm_data;
  231. /* FALLTHROUGH */
  232. case IB_WR_SEND:
  233. if (!ipath_get_rwqe(qp, 0)) {
  234. rnr_nak:
  235. /* Handle RNR NAK */
  236. if (qp->ibqp.qp_type == IB_QPT_UC)
  237. goto send_comp;
  238. if (sqp->s_rnr_retry == 0) {
  239. wc.status = IB_WC_RNR_RETRY_EXC_ERR;
  240. goto err;
  241. }
  242. if (sqp->s_rnr_retry_cnt < 7)
  243. sqp->s_rnr_retry--;
  244. dev->n_rnr_naks++;
  245. sqp->s_rnr_timeout =
  246. ib_ipath_rnr_table[sqp->r_min_rnr_timer];
  247. ipath_insert_rnr_queue(sqp);
  248. goto done;
  249. }
  250. break;
  251. case IB_WR_RDMA_WRITE_WITH_IMM:
  252. wc.wc_flags = IB_WC_WITH_IMM;
  253. wc.imm_data = wqe->wr.imm_data;
  254. if (!ipath_get_rwqe(qp, 1))
  255. goto rnr_nak;
  256. /* FALLTHROUGH */
  257. case IB_WR_RDMA_WRITE:
  258. if (wqe->length == 0)
  259. break;
  260. if (unlikely(!ipath_rkey_ok(dev, &qp->r_sge, wqe->length,
  261. wqe->wr.wr.rdma.remote_addr,
  262. wqe->wr.wr.rdma.rkey,
  263. IB_ACCESS_REMOTE_WRITE))) {
  264. acc_err:
  265. wc.status = IB_WC_REM_ACCESS_ERR;
  266. err:
  267. wc.wr_id = wqe->wr.wr_id;
  268. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  269. wc.vendor_err = 0;
  270. wc.byte_len = 0;
  271. wc.qp_num = sqp->ibqp.qp_num;
  272. wc.src_qp = sqp->remote_qpn;
  273. wc.pkey_index = 0;
  274. wc.slid = sqp->remote_ah_attr.dlid;
  275. wc.sl = sqp->remote_ah_attr.sl;
  276. wc.dlid_path_bits = 0;
  277. wc.port_num = 0;
  278. ipath_sqerror_qp(sqp, &wc);
  279. goto done;
  280. }
  281. break;
  282. case IB_WR_RDMA_READ:
  283. if (unlikely(!ipath_rkey_ok(dev, &sqp->s_sge, wqe->length,
  284. wqe->wr.wr.rdma.remote_addr,
  285. wqe->wr.wr.rdma.rkey,
  286. IB_ACCESS_REMOTE_READ)))
  287. goto acc_err;
  288. if (unlikely(!(qp->qp_access_flags &
  289. IB_ACCESS_REMOTE_READ)))
  290. goto acc_err;
  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. break;
  295. case IB_WR_ATOMIC_CMP_AND_SWP:
  296. case IB_WR_ATOMIC_FETCH_AND_ADD:
  297. if (unlikely(!ipath_rkey_ok(dev, &qp->r_sge, sizeof(u64),
  298. wqe->wr.wr.rdma.remote_addr,
  299. wqe->wr.wr.rdma.rkey,
  300. IB_ACCESS_REMOTE_ATOMIC)))
  301. goto acc_err;
  302. /* Perform atomic OP and save result. */
  303. sdata = wqe->wr.wr.atomic.swap;
  304. spin_lock_irqsave(&dev->pending_lock, flags);
  305. qp->r_atomic_data = *(u64 *) qp->r_sge.sge.vaddr;
  306. if (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
  307. *(u64 *) qp->r_sge.sge.vaddr =
  308. qp->r_atomic_data + sdata;
  309. else if (qp->r_atomic_data == wqe->wr.wr.atomic.compare_add)
  310. *(u64 *) qp->r_sge.sge.vaddr = sdata;
  311. spin_unlock_irqrestore(&dev->pending_lock, flags);
  312. *(u64 *) sqp->s_sge.sge.vaddr = qp->r_atomic_data;
  313. goto send_comp;
  314. default:
  315. goto done;
  316. }
  317. sge = &sqp->s_sge.sge;
  318. while (sqp->s_len) {
  319. u32 len = sqp->s_len;
  320. if (len > sge->length)
  321. len = sge->length;
  322. BUG_ON(len == 0);
  323. ipath_copy_sge(&qp->r_sge, sge->vaddr, len);
  324. sge->vaddr += len;
  325. sge->length -= len;
  326. sge->sge_length -= len;
  327. if (sge->sge_length == 0) {
  328. if (--sqp->s_sge.num_sge)
  329. *sge = *sqp->s_sge.sg_list++;
  330. } else if (sge->length == 0 && sge->mr != NULL) {
  331. if (++sge->n >= IPATH_SEGSZ) {
  332. if (++sge->m >= sge->mr->mapsz)
  333. break;
  334. sge->n = 0;
  335. }
  336. sge->vaddr =
  337. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  338. sge->length =
  339. sge->mr->map[sge->m]->segs[sge->n].length;
  340. }
  341. sqp->s_len -= len;
  342. }
  343. if (wqe->wr.opcode == IB_WR_RDMA_WRITE ||
  344. wqe->wr.opcode == IB_WR_RDMA_READ)
  345. goto send_comp;
  346. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  347. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  348. else
  349. wc.opcode = IB_WC_RECV;
  350. wc.wr_id = qp->r_wr_id;
  351. wc.status = IB_WC_SUCCESS;
  352. wc.vendor_err = 0;
  353. wc.byte_len = wqe->length;
  354. wc.qp_num = qp->ibqp.qp_num;
  355. wc.src_qp = qp->remote_qpn;
  356. /* XXX do we know which pkey matched? Only needed for GSI. */
  357. wc.pkey_index = 0;
  358. wc.slid = qp->remote_ah_attr.dlid;
  359. wc.sl = qp->remote_ah_attr.sl;
  360. wc.dlid_path_bits = 0;
  361. /* Signal completion event if the solicited bit is set. */
  362. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
  363. wqe->wr.send_flags & IB_SEND_SOLICITED);
  364. send_comp:
  365. sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
  366. if (!test_bit(IPATH_S_SIGNAL_REQ_WR, &sqp->s_flags) ||
  367. (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
  368. wc.wr_id = wqe->wr.wr_id;
  369. wc.status = IB_WC_SUCCESS;
  370. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  371. wc.vendor_err = 0;
  372. wc.byte_len = wqe->length;
  373. wc.qp_num = sqp->ibqp.qp_num;
  374. wc.src_qp = 0;
  375. wc.pkey_index = 0;
  376. wc.slid = 0;
  377. wc.sl = 0;
  378. wc.dlid_path_bits = 0;
  379. wc.port_num = 0;
  380. ipath_cq_enter(to_icq(sqp->ibqp.send_cq), &wc, 0);
  381. }
  382. /* Update s_last now that we are finished with the SWQE */
  383. spin_lock_irqsave(&sqp->s_lock, flags);
  384. if (++sqp->s_last >= sqp->s_size)
  385. sqp->s_last = 0;
  386. spin_unlock_irqrestore(&sqp->s_lock, flags);
  387. goto again;
  388. done:
  389. if (atomic_dec_and_test(&qp->refcount))
  390. wake_up(&qp->wait);
  391. }
  392. /**
  393. * ipath_no_bufs_available - tell the layer driver we need buffers
  394. * @qp: the QP that caused the problem
  395. * @dev: the device we ran out of buffers on
  396. *
  397. * Called when we run out of PIO buffers.
  398. */
  399. void ipath_no_bufs_available(struct ipath_qp *qp, struct ipath_ibdev *dev)
  400. {
  401. unsigned long flags;
  402. spin_lock_irqsave(&dev->pending_lock, flags);
  403. if (list_empty(&qp->piowait))
  404. list_add_tail(&qp->piowait, &dev->piowait);
  405. spin_unlock_irqrestore(&dev->pending_lock, flags);
  406. /*
  407. * Note that as soon as ipath_layer_want_buffer() is called and
  408. * possibly before it returns, ipath_ib_piobufavail()
  409. * could be called. If we are still in the tasklet function,
  410. * tasklet_hi_schedule() will not call us until the next time
  411. * tasklet_hi_schedule() is called.
  412. * We clear the tasklet flag now since we are committing to return
  413. * from the tasklet function.
  414. */
  415. clear_bit(IPATH_S_BUSY, &qp->s_flags);
  416. tasklet_unlock(&qp->s_task);
  417. ipath_layer_want_buffer(dev->dd);
  418. dev->n_piowait++;
  419. }
  420. /**
  421. * ipath_post_ruc_send - post RC and UC sends
  422. * @qp: the QP to post on
  423. * @wr: the work request to send
  424. */
  425. int ipath_post_ruc_send(struct ipath_qp *qp, struct ib_send_wr *wr)
  426. {
  427. struct ipath_swqe *wqe;
  428. unsigned long flags;
  429. u32 next;
  430. int i, j;
  431. int acc;
  432. int ret;
  433. /*
  434. * Don't allow RDMA reads or atomic operations on UC or
  435. * undefined operations.
  436. * Make sure buffer is large enough to hold the result for atomics.
  437. */
  438. if (qp->ibqp.qp_type == IB_QPT_UC) {
  439. if ((unsigned) wr->opcode >= IB_WR_RDMA_READ) {
  440. ret = -EINVAL;
  441. goto bail;
  442. }
  443. } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
  444. ret = -EINVAL;
  445. goto bail;
  446. } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
  447. (wr->num_sge == 0 ||
  448. wr->sg_list[0].length < sizeof(u64) ||
  449. wr->sg_list[0].addr & (sizeof(u64) - 1))) {
  450. ret = -EINVAL;
  451. goto bail;
  452. }
  453. /* IB spec says that num_sge == 0 is OK. */
  454. if (wr->num_sge > qp->s_max_sge) {
  455. ret = -ENOMEM;
  456. goto bail;
  457. }
  458. spin_lock_irqsave(&qp->s_lock, flags);
  459. next = qp->s_head + 1;
  460. if (next >= qp->s_size)
  461. next = 0;
  462. if (next == qp->s_last) {
  463. spin_unlock_irqrestore(&qp->s_lock, flags);
  464. ret = -EINVAL;
  465. goto bail;
  466. }
  467. wqe = get_swqe_ptr(qp, qp->s_head);
  468. wqe->wr = *wr;
  469. wqe->ssn = qp->s_ssn++;
  470. wqe->sg_list[0].mr = NULL;
  471. wqe->sg_list[0].vaddr = NULL;
  472. wqe->sg_list[0].length = 0;
  473. wqe->sg_list[0].sge_length = 0;
  474. wqe->length = 0;
  475. acc = wr->opcode >= IB_WR_RDMA_READ ? IB_ACCESS_LOCAL_WRITE : 0;
  476. for (i = 0, j = 0; i < wr->num_sge; i++) {
  477. if (to_ipd(qp->ibqp.pd)->user && wr->sg_list[i].lkey == 0) {
  478. spin_unlock_irqrestore(&qp->s_lock, flags);
  479. ret = -EINVAL;
  480. goto bail;
  481. }
  482. if (wr->sg_list[i].length == 0)
  483. continue;
  484. if (!ipath_lkey_ok(&to_idev(qp->ibqp.device)->lk_table,
  485. &wqe->sg_list[j], &wr->sg_list[i],
  486. acc)) {
  487. spin_unlock_irqrestore(&qp->s_lock, flags);
  488. ret = -EINVAL;
  489. goto bail;
  490. }
  491. wqe->length += wr->sg_list[i].length;
  492. j++;
  493. }
  494. wqe->wr.num_sge = j;
  495. qp->s_head = next;
  496. spin_unlock_irqrestore(&qp->s_lock, flags);
  497. ipath_do_ruc_send((unsigned long) qp);
  498. ret = 0;
  499. bail:
  500. return ret;
  501. }
  502. /**
  503. * ipath_make_grh - construct a GRH header
  504. * @dev: a pointer to the ipath device
  505. * @hdr: a pointer to the GRH header being constructed
  506. * @grh: the global route address to send to
  507. * @hwords: the number of 32 bit words of header being sent
  508. * @nwords: the number of 32 bit words of data being sent
  509. *
  510. * Return the size of the header in 32 bit words.
  511. */
  512. u32 ipath_make_grh(struct ipath_ibdev *dev, struct ib_grh *hdr,
  513. struct ib_global_route *grh, u32 hwords, u32 nwords)
  514. {
  515. hdr->version_tclass_flow =
  516. cpu_to_be32((6 << 28) |
  517. (grh->traffic_class << 20) |
  518. grh->flow_label);
  519. hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
  520. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  521. hdr->next_hdr = 0x1B;
  522. hdr->hop_limit = grh->hop_limit;
  523. /* The SGID is 32-bit aligned. */
  524. hdr->sgid.global.subnet_prefix = dev->gid_prefix;
  525. hdr->sgid.global.interface_id = ipath_layer_get_guid(dev->dd);
  526. hdr->dgid = grh->dgid;
  527. /* GRH header size in 32-bit words. */
  528. return sizeof(struct ib_grh) / sizeof(u32);
  529. }
  530. /**
  531. * ipath_do_ruc_send - perform a send on an RC or UC QP
  532. * @data: contains a pointer to the QP
  533. *
  534. * Process entries in the send work queue until credit or queue is
  535. * exhausted. Only allow one CPU to send a packet per QP (tasklet).
  536. * Otherwise, after we drop the QP s_lock, two threads could send
  537. * packets out of order.
  538. */
  539. void ipath_do_ruc_send(unsigned long data)
  540. {
  541. struct ipath_qp *qp = (struct ipath_qp *)data;
  542. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  543. unsigned long flags;
  544. u16 lrh0;
  545. u32 nwords;
  546. u32 extra_bytes;
  547. u32 bth0;
  548. u32 bth2;
  549. u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
  550. struct ipath_other_headers *ohdr;
  551. if (test_and_set_bit(IPATH_S_BUSY, &qp->s_flags))
  552. goto bail;
  553. if (unlikely(qp->remote_ah_attr.dlid ==
  554. ipath_layer_get_lid(dev->dd))) {
  555. ipath_ruc_loopback(qp);
  556. goto clear;
  557. }
  558. ohdr = &qp->s_hdr.u.oth;
  559. if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
  560. ohdr = &qp->s_hdr.u.l.oth;
  561. again:
  562. /* Check for a constructed packet to be sent. */
  563. if (qp->s_hdrwords != 0) {
  564. /*
  565. * If no PIO bufs are available, return. An interrupt will
  566. * call ipath_ib_piobufavail() when one is available.
  567. */
  568. if (ipath_verbs_send(dev->dd, qp->s_hdrwords,
  569. (u32 *) &qp->s_hdr, qp->s_cur_size,
  570. qp->s_cur_sge)) {
  571. ipath_no_bufs_available(qp, dev);
  572. goto bail;
  573. }
  574. dev->n_unicast_xmit++;
  575. /* Record that we sent the packet and s_hdr is empty. */
  576. qp->s_hdrwords = 0;
  577. }
  578. /*
  579. * The lock is needed to synchronize between setting
  580. * qp->s_ack_state, resend timer, and post_send().
  581. */
  582. spin_lock_irqsave(&qp->s_lock, flags);
  583. /* Sending responses has higher priority over sending requests. */
  584. if (qp->s_ack_state != IB_OPCODE_RC_ACKNOWLEDGE &&
  585. (bth0 = ipath_make_rc_ack(qp, ohdr, pmtu)) != 0)
  586. bth2 = qp->s_ack_psn++ & IPATH_PSN_MASK;
  587. else if (!((qp->ibqp.qp_type == IB_QPT_RC) ?
  588. ipath_make_rc_req(qp, ohdr, pmtu, &bth0, &bth2) :
  589. ipath_make_uc_req(qp, ohdr, pmtu, &bth0, &bth2))) {
  590. /*
  591. * Clear the busy bit before unlocking to avoid races with
  592. * adding new work queue items and then failing to process
  593. * them.
  594. */
  595. clear_bit(IPATH_S_BUSY, &qp->s_flags);
  596. spin_unlock_irqrestore(&qp->s_lock, flags);
  597. goto bail;
  598. }
  599. spin_unlock_irqrestore(&qp->s_lock, flags);
  600. /* Construct the header. */
  601. extra_bytes = (4 - qp->s_cur_size) & 3;
  602. nwords = (qp->s_cur_size + extra_bytes) >> 2;
  603. lrh0 = IPATH_LRH_BTH;
  604. if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
  605. qp->s_hdrwords += ipath_make_grh(dev, &qp->s_hdr.u.l.grh,
  606. &qp->remote_ah_attr.grh,
  607. qp->s_hdrwords, nwords);
  608. lrh0 = IPATH_LRH_GRH;
  609. }
  610. lrh0 |= qp->remote_ah_attr.sl << 4;
  611. qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
  612. qp->s_hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
  613. qp->s_hdr.lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords +
  614. SIZE_OF_CRC);
  615. qp->s_hdr.lrh[3] = cpu_to_be16(ipath_layer_get_lid(dev->dd));
  616. bth0 |= ipath_layer_get_pkey(dev->dd, qp->s_pkey_index);
  617. bth0 |= extra_bytes << 20;
  618. ohdr->bth[0] = cpu_to_be32(bth0);
  619. ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
  620. ohdr->bth[2] = cpu_to_be32(bth2);
  621. /* Check for more work to do. */
  622. goto again;
  623. clear:
  624. clear_bit(IPATH_S_BUSY, &qp->s_flags);
  625. bail:
  626. return;
  627. }