ipath_ruc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 "ips_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. * Called at interrupt level with the QP r_rq.lock held.
  111. */
  112. int ipath_get_rwqe(struct ipath_qp *qp, int wr_id_only)
  113. {
  114. struct ipath_rq *rq;
  115. struct ipath_srq *srq;
  116. struct ipath_rwqe *wqe;
  117. int ret;
  118. if (!qp->ibqp.srq) {
  119. rq = &qp->r_rq;
  120. if (unlikely(rq->tail == rq->head)) {
  121. ret = 0;
  122. goto bail;
  123. }
  124. wqe = get_rwqe_ptr(rq, rq->tail);
  125. qp->r_wr_id = wqe->wr_id;
  126. if (!wr_id_only) {
  127. qp->r_sge.sge = wqe->sg_list[0];
  128. qp->r_sge.sg_list = wqe->sg_list + 1;
  129. qp->r_sge.num_sge = wqe->num_sge;
  130. qp->r_len = wqe->length;
  131. }
  132. if (++rq->tail >= rq->size)
  133. rq->tail = 0;
  134. ret = 1;
  135. goto bail;
  136. }
  137. srq = to_isrq(qp->ibqp.srq);
  138. rq = &srq->rq;
  139. spin_lock(&rq->lock);
  140. if (unlikely(rq->tail == rq->head)) {
  141. spin_unlock(&rq->lock);
  142. ret = 0;
  143. goto bail;
  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(&rq->lock);
  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. } else
  171. spin_unlock(&rq->lock);
  172. } else
  173. spin_unlock(&rq->lock);
  174. ret = 1;
  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. spin_lock_irqsave(&qp->r_rq.lock, flags);
  234. if (!ipath_get_rwqe(qp, 0)) {
  235. rnr_nak:
  236. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  237. /* Handle RNR NAK */
  238. if (qp->ibqp.qp_type == IB_QPT_UC)
  239. goto send_comp;
  240. if (sqp->s_rnr_retry == 0) {
  241. wc.status = IB_WC_RNR_RETRY_EXC_ERR;
  242. goto err;
  243. }
  244. if (sqp->s_rnr_retry_cnt < 7)
  245. sqp->s_rnr_retry--;
  246. dev->n_rnr_naks++;
  247. sqp->s_rnr_timeout =
  248. ib_ipath_rnr_table[sqp->s_min_rnr_timer];
  249. ipath_insert_rnr_queue(sqp);
  250. goto done;
  251. }
  252. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  253. break;
  254. case IB_WR_RDMA_WRITE_WITH_IMM:
  255. wc.wc_flags = IB_WC_WITH_IMM;
  256. wc.imm_data = wqe->wr.imm_data;
  257. spin_lock_irqsave(&qp->r_rq.lock, flags);
  258. if (!ipath_get_rwqe(qp, 1))
  259. goto rnr_nak;
  260. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  261. /* FALLTHROUGH */
  262. case IB_WR_RDMA_WRITE:
  263. if (wqe->length == 0)
  264. break;
  265. if (unlikely(!ipath_rkey_ok(dev, &qp->r_sge, wqe->length,
  266. wqe->wr.wr.rdma.remote_addr,
  267. wqe->wr.wr.rdma.rkey,
  268. IB_ACCESS_REMOTE_WRITE))) {
  269. acc_err:
  270. wc.status = IB_WC_REM_ACCESS_ERR;
  271. err:
  272. wc.wr_id = wqe->wr.wr_id;
  273. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  274. wc.vendor_err = 0;
  275. wc.byte_len = 0;
  276. wc.qp_num = sqp->ibqp.qp_num;
  277. wc.src_qp = sqp->remote_qpn;
  278. wc.pkey_index = 0;
  279. wc.slid = sqp->remote_ah_attr.dlid;
  280. wc.sl = sqp->remote_ah_attr.sl;
  281. wc.dlid_path_bits = 0;
  282. wc.port_num = 0;
  283. ipath_sqerror_qp(sqp, &wc);
  284. goto done;
  285. }
  286. break;
  287. case IB_WR_RDMA_READ:
  288. if (unlikely(!ipath_rkey_ok(dev, &sqp->s_sge, wqe->length,
  289. wqe->wr.wr.rdma.remote_addr,
  290. wqe->wr.wr.rdma.rkey,
  291. IB_ACCESS_REMOTE_READ)))
  292. goto acc_err;
  293. if (unlikely(!(qp->qp_access_flags &
  294. IB_ACCESS_REMOTE_READ)))
  295. goto acc_err;
  296. qp->r_sge.sge = wqe->sg_list[0];
  297. qp->r_sge.sg_list = wqe->sg_list + 1;
  298. qp->r_sge.num_sge = wqe->wr.num_sge;
  299. break;
  300. case IB_WR_ATOMIC_CMP_AND_SWP:
  301. case IB_WR_ATOMIC_FETCH_AND_ADD:
  302. if (unlikely(!ipath_rkey_ok(dev, &qp->r_sge, sizeof(u64),
  303. wqe->wr.wr.rdma.remote_addr,
  304. wqe->wr.wr.rdma.rkey,
  305. IB_ACCESS_REMOTE_ATOMIC)))
  306. goto acc_err;
  307. /* Perform atomic OP and save result. */
  308. sdata = wqe->wr.wr.atomic.swap;
  309. spin_lock_irqsave(&dev->pending_lock, flags);
  310. qp->r_atomic_data = *(u64 *) qp->r_sge.sge.vaddr;
  311. if (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
  312. *(u64 *) qp->r_sge.sge.vaddr =
  313. qp->r_atomic_data + sdata;
  314. else if (qp->r_atomic_data == wqe->wr.wr.atomic.compare_add)
  315. *(u64 *) qp->r_sge.sge.vaddr = sdata;
  316. spin_unlock_irqrestore(&dev->pending_lock, flags);
  317. *(u64 *) sqp->s_sge.sge.vaddr = qp->r_atomic_data;
  318. goto send_comp;
  319. default:
  320. goto done;
  321. }
  322. sge = &sqp->s_sge.sge;
  323. while (sqp->s_len) {
  324. u32 len = sqp->s_len;
  325. if (len > sge->length)
  326. len = sge->length;
  327. BUG_ON(len == 0);
  328. ipath_copy_sge(&qp->r_sge, sge->vaddr, len);
  329. sge->vaddr += len;
  330. sge->length -= len;
  331. sge->sge_length -= len;
  332. if (sge->sge_length == 0) {
  333. if (--sqp->s_sge.num_sge)
  334. *sge = *sqp->s_sge.sg_list++;
  335. } else if (sge->length == 0 && sge->mr != NULL) {
  336. if (++sge->n >= IPATH_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 (wqe->wr.opcode == IB_WR_RDMA_WRITE ||
  349. wqe->wr.opcode == IB_WR_RDMA_READ)
  350. goto send_comp;
  351. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  352. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  353. else
  354. wc.opcode = IB_WC_RECV;
  355. wc.wr_id = qp->r_wr_id;
  356. wc.status = IB_WC_SUCCESS;
  357. wc.vendor_err = 0;
  358. wc.byte_len = wqe->length;
  359. wc.qp_num = qp->ibqp.qp_num;
  360. wc.src_qp = qp->remote_qpn;
  361. /* XXX do we know which pkey matched? Only needed for GSI. */
  362. wc.pkey_index = 0;
  363. wc.slid = qp->remote_ah_attr.dlid;
  364. wc.sl = qp->remote_ah_attr.sl;
  365. wc.dlid_path_bits = 0;
  366. /* Signal completion event if the solicited bit is set. */
  367. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
  368. wqe->wr.send_flags & IB_SEND_SOLICITED);
  369. send_comp:
  370. sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
  371. if (!test_bit(IPATH_S_SIGNAL_REQ_WR, &sqp->s_flags) ||
  372. (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
  373. wc.wr_id = wqe->wr.wr_id;
  374. wc.status = IB_WC_SUCCESS;
  375. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  376. wc.vendor_err = 0;
  377. wc.byte_len = wqe->length;
  378. wc.qp_num = sqp->ibqp.qp_num;
  379. wc.src_qp = 0;
  380. wc.pkey_index = 0;
  381. wc.slid = 0;
  382. wc.sl = 0;
  383. wc.dlid_path_bits = 0;
  384. wc.port_num = 0;
  385. ipath_cq_enter(to_icq(sqp->ibqp.send_cq), &wc, 0);
  386. }
  387. /* Update s_last now that we are finished with the SWQE */
  388. spin_lock_irqsave(&sqp->s_lock, flags);
  389. if (++sqp->s_last >= sqp->s_size)
  390. sqp->s_last = 0;
  391. spin_unlock_irqrestore(&sqp->s_lock, flags);
  392. goto again;
  393. done:
  394. if (atomic_dec_and_test(&qp->refcount))
  395. wake_up(&qp->wait);
  396. }
  397. /**
  398. * ipath_no_bufs_available - tell the layer driver we need buffers
  399. * @qp: the QP that caused the problem
  400. * @dev: the device we ran out of buffers on
  401. *
  402. * Called when we run out of PIO buffers.
  403. */
  404. void ipath_no_bufs_available(struct ipath_qp *qp, struct ipath_ibdev *dev)
  405. {
  406. unsigned long flags;
  407. spin_lock_irqsave(&dev->pending_lock, flags);
  408. if (list_empty(&qp->piowait))
  409. list_add_tail(&qp->piowait, &dev->piowait);
  410. spin_unlock_irqrestore(&dev->pending_lock, flags);
  411. /*
  412. * Note that as soon as ipath_layer_want_buffer() is called and
  413. * possibly before it returns, ipath_ib_piobufavail()
  414. * could be called. If we are still in the tasklet function,
  415. * tasklet_hi_schedule() will not call us until the next time
  416. * tasklet_hi_schedule() is called.
  417. * We clear the tasklet flag now since we are committing to return
  418. * from the tasklet function.
  419. */
  420. clear_bit(IPATH_S_BUSY, &qp->s_flags);
  421. tasklet_unlock(&qp->s_task);
  422. ipath_layer_want_buffer(dev->dd);
  423. dev->n_piowait++;
  424. }
  425. /**
  426. * ipath_post_ruc_send - post RC and UC sends
  427. * @qp: the QP to post on
  428. * @wr: the work request to send
  429. */
  430. int ipath_post_ruc_send(struct ipath_qp *qp, struct ib_send_wr *wr)
  431. {
  432. struct ipath_swqe *wqe;
  433. unsigned long flags;
  434. u32 next;
  435. int i, j;
  436. int acc;
  437. int ret;
  438. /*
  439. * Don't allow RDMA reads or atomic operations on UC or
  440. * undefined operations.
  441. * Make sure buffer is large enough to hold the result for atomics.
  442. */
  443. if (qp->ibqp.qp_type == IB_QPT_UC) {
  444. if ((unsigned) wr->opcode >= IB_WR_RDMA_READ) {
  445. ret = -EINVAL;
  446. goto bail;
  447. }
  448. } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
  449. ret = -EINVAL;
  450. goto bail;
  451. } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
  452. (wr->num_sge == 0 ||
  453. wr->sg_list[0].length < sizeof(u64) ||
  454. wr->sg_list[0].addr & (sizeof(u64) - 1))) {
  455. ret = -EINVAL;
  456. goto bail;
  457. }
  458. /* IB spec says that num_sge == 0 is OK. */
  459. if (wr->num_sge > qp->s_max_sge) {
  460. ret = -ENOMEM;
  461. goto bail;
  462. }
  463. spin_lock_irqsave(&qp->s_lock, flags);
  464. next = qp->s_head + 1;
  465. if (next >= qp->s_size)
  466. next = 0;
  467. if (next == qp->s_last) {
  468. spin_unlock_irqrestore(&qp->s_lock, flags);
  469. ret = -EINVAL;
  470. goto bail;
  471. }
  472. wqe = get_swqe_ptr(qp, qp->s_head);
  473. wqe->wr = *wr;
  474. wqe->ssn = qp->s_ssn++;
  475. wqe->sg_list[0].mr = NULL;
  476. wqe->sg_list[0].vaddr = NULL;
  477. wqe->sg_list[0].length = 0;
  478. wqe->sg_list[0].sge_length = 0;
  479. wqe->length = 0;
  480. acc = wr->opcode >= IB_WR_RDMA_READ ? IB_ACCESS_LOCAL_WRITE : 0;
  481. for (i = 0, j = 0; i < wr->num_sge; i++) {
  482. if (to_ipd(qp->ibqp.pd)->user && wr->sg_list[i].lkey == 0) {
  483. spin_unlock_irqrestore(&qp->s_lock, flags);
  484. ret = -EINVAL;
  485. goto bail;
  486. }
  487. if (wr->sg_list[i].length == 0)
  488. continue;
  489. if (!ipath_lkey_ok(&to_idev(qp->ibqp.device)->lk_table,
  490. &wqe->sg_list[j], &wr->sg_list[i],
  491. acc)) {
  492. spin_unlock_irqrestore(&qp->s_lock, flags);
  493. ret = -EINVAL;
  494. goto bail;
  495. }
  496. wqe->length += wr->sg_list[i].length;
  497. j++;
  498. }
  499. wqe->wr.num_sge = j;
  500. qp->s_head = next;
  501. spin_unlock_irqrestore(&qp->s_lock, flags);
  502. ipath_do_ruc_send((unsigned long) qp);
  503. ret = 0;
  504. bail:
  505. return ret;
  506. }
  507. /**
  508. * ipath_make_grh - construct a GRH header
  509. * @dev: a pointer to the ipath device
  510. * @hdr: a pointer to the GRH header being constructed
  511. * @grh: the global route address to send to
  512. * @hwords: the number of 32 bit words of header being sent
  513. * @nwords: the number of 32 bit words of data being sent
  514. *
  515. * Return the size of the header in 32 bit words.
  516. */
  517. u32 ipath_make_grh(struct ipath_ibdev *dev, struct ib_grh *hdr,
  518. struct ib_global_route *grh, u32 hwords, u32 nwords)
  519. {
  520. hdr->version_tclass_flow =
  521. cpu_to_be32((6 << 28) |
  522. (grh->traffic_class << 20) |
  523. grh->flow_label);
  524. hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
  525. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  526. hdr->next_hdr = 0x1B;
  527. hdr->hop_limit = grh->hop_limit;
  528. /* The SGID is 32-bit aligned. */
  529. hdr->sgid.global.subnet_prefix = dev->gid_prefix;
  530. hdr->sgid.global.interface_id = ipath_layer_get_guid(dev->dd);
  531. hdr->dgid = grh->dgid;
  532. /* GRH header size in 32-bit words. */
  533. return sizeof(struct ib_grh) / sizeof(u32);
  534. }
  535. /**
  536. * ipath_do_ruc_send - perform a send on an RC or UC QP
  537. * @data: contains a pointer to the QP
  538. *
  539. * Process entries in the send work queue until credit or queue is
  540. * exhausted. Only allow one CPU to send a packet per QP (tasklet).
  541. * Otherwise, after we drop the QP s_lock, two threads could send
  542. * packets out of order.
  543. */
  544. void ipath_do_ruc_send(unsigned long data)
  545. {
  546. struct ipath_qp *qp = (struct ipath_qp *)data;
  547. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  548. unsigned long flags;
  549. u16 lrh0;
  550. u32 nwords;
  551. u32 extra_bytes;
  552. u32 bth0;
  553. u32 bth2;
  554. u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
  555. struct ipath_other_headers *ohdr;
  556. if (test_and_set_bit(IPATH_S_BUSY, &qp->s_flags))
  557. goto bail;
  558. if (unlikely(qp->remote_ah_attr.dlid ==
  559. ipath_layer_get_lid(dev->dd))) {
  560. ipath_ruc_loopback(qp);
  561. goto clear;
  562. }
  563. ohdr = &qp->s_hdr.u.oth;
  564. if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
  565. ohdr = &qp->s_hdr.u.l.oth;
  566. again:
  567. /* Check for a constructed packet to be sent. */
  568. if (qp->s_hdrwords != 0) {
  569. /*
  570. * If no PIO bufs are available, return. An interrupt will
  571. * call ipath_ib_piobufavail() when one is available.
  572. */
  573. if (ipath_verbs_send(dev->dd, qp->s_hdrwords,
  574. (u32 *) &qp->s_hdr, qp->s_cur_size,
  575. qp->s_cur_sge)) {
  576. ipath_no_bufs_available(qp, dev);
  577. goto bail;
  578. }
  579. dev->n_unicast_xmit++;
  580. /* Record that we sent the packet and s_hdr is empty. */
  581. qp->s_hdrwords = 0;
  582. }
  583. /*
  584. * The lock is needed to synchronize between setting
  585. * qp->s_ack_state, resend timer, and post_send().
  586. */
  587. spin_lock_irqsave(&qp->s_lock, flags);
  588. /* Sending responses has higher priority over sending requests. */
  589. if (qp->s_ack_state != IB_OPCODE_RC_ACKNOWLEDGE &&
  590. (bth0 = ipath_make_rc_ack(qp, ohdr, pmtu)) != 0)
  591. bth2 = qp->s_ack_psn++ & IPS_PSN_MASK;
  592. else if (!((qp->ibqp.qp_type == IB_QPT_RC) ?
  593. ipath_make_rc_req(qp, ohdr, pmtu, &bth0, &bth2) :
  594. ipath_make_uc_req(qp, ohdr, pmtu, &bth0, &bth2))) {
  595. /*
  596. * Clear the busy bit before unlocking to avoid races with
  597. * adding new work queue items and then failing to process
  598. * them.
  599. */
  600. clear_bit(IPATH_S_BUSY, &qp->s_flags);
  601. spin_unlock_irqrestore(&qp->s_lock, flags);
  602. goto bail;
  603. }
  604. spin_unlock_irqrestore(&qp->s_lock, flags);
  605. /* Construct the header. */
  606. extra_bytes = (4 - qp->s_cur_size) & 3;
  607. nwords = (qp->s_cur_size + extra_bytes) >> 2;
  608. lrh0 = IPS_LRH_BTH;
  609. if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
  610. qp->s_hdrwords += ipath_make_grh(dev, &qp->s_hdr.u.l.grh,
  611. &qp->remote_ah_attr.grh,
  612. qp->s_hdrwords, nwords);
  613. lrh0 = IPS_LRH_GRH;
  614. }
  615. lrh0 |= qp->remote_ah_attr.sl << 4;
  616. qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
  617. qp->s_hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
  618. qp->s_hdr.lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords +
  619. SIZE_OF_CRC);
  620. qp->s_hdr.lrh[3] = cpu_to_be16(ipath_layer_get_lid(dev->dd));
  621. bth0 |= ipath_layer_get_pkey(dev->dd, qp->s_pkey_index);
  622. bth0 |= extra_bytes << 20;
  623. ohdr->bth[0] = cpu_to_be32(bth0);
  624. ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
  625. ohdr->bth[2] = cpu_to_be32(bth2);
  626. /* Check for more work to do. */
  627. goto again;
  628. clear:
  629. clear_bit(IPATH_S_BUSY, &qp->s_flags);
  630. bail:
  631. return;
  632. }