ipath_ruc.c 19 KB

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