ipath_ruc.c 19 KB

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