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