ipath_ruc.c 17 KB

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