ipath_ruc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. All rights reserved.
  3. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include "ipath_verbs.h"
  34. #include "ipath_kernel.h"
  35. /*
  36. * Convert the AETH RNR timeout code into the number of milliseconds.
  37. */
  38. const u32 ib_ipath_rnr_table[32] = {
  39. 656, /* 0 */
  40. 1, /* 1 */
  41. 1, /* 2 */
  42. 1, /* 3 */
  43. 1, /* 4 */
  44. 1, /* 5 */
  45. 1, /* 6 */
  46. 1, /* 7 */
  47. 1, /* 8 */
  48. 1, /* 9 */
  49. 1, /* A */
  50. 1, /* B */
  51. 1, /* C */
  52. 1, /* D */
  53. 2, /* E */
  54. 2, /* F */
  55. 3, /* 10 */
  56. 4, /* 11 */
  57. 6, /* 12 */
  58. 8, /* 13 */
  59. 11, /* 14 */
  60. 16, /* 15 */
  61. 21, /* 16 */
  62. 31, /* 17 */
  63. 41, /* 18 */
  64. 62, /* 19 */
  65. 82, /* 1A */
  66. 123, /* 1B */
  67. 164, /* 1C */
  68. 246, /* 1D */
  69. 328, /* 1E */
  70. 492 /* 1F */
  71. };
  72. /**
  73. * ipath_insert_rnr_queue - put QP on the RNR timeout list for the device
  74. * @qp: the QP
  75. *
  76. * XXX Use a simple list for now. We might need a priority
  77. * queue if we have lots of QPs waiting for RNR timeouts
  78. * but that should be rare.
  79. */
  80. void ipath_insert_rnr_queue(struct ipath_qp *qp)
  81. {
  82. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  83. unsigned long flags;
  84. spin_lock_irqsave(&dev->pending_lock, flags);
  85. if (list_empty(&dev->rnrwait))
  86. list_add(&qp->timerwait, &dev->rnrwait);
  87. else {
  88. struct list_head *l = &dev->rnrwait;
  89. struct ipath_qp *nqp = list_entry(l->next, struct ipath_qp,
  90. timerwait);
  91. while (qp->s_rnr_timeout >= nqp->s_rnr_timeout) {
  92. qp->s_rnr_timeout -= nqp->s_rnr_timeout;
  93. l = l->next;
  94. if (l->next == &dev->rnrwait)
  95. break;
  96. nqp = list_entry(l->next, struct ipath_qp,
  97. timerwait);
  98. }
  99. list_add(&qp->timerwait, l);
  100. }
  101. spin_unlock_irqrestore(&dev->pending_lock, flags);
  102. }
  103. static int init_sge(struct ipath_qp *qp, struct ipath_rwqe *wqe)
  104. {
  105. int user = to_ipd(qp->ibqp.pd)->user;
  106. int i, j, ret;
  107. struct ib_wc wc;
  108. qp->r_len = 0;
  109. for (i = j = 0; i < wqe->num_sge; i++) {
  110. if (wqe->sg_list[i].length == 0)
  111. continue;
  112. /* Check LKEY */
  113. if ((user && wqe->sg_list[i].lkey == 0) ||
  114. !ipath_lkey_ok(qp, &qp->r_sg_list[j], &wqe->sg_list[i],
  115. IB_ACCESS_LOCAL_WRITE))
  116. goto bad_lkey;
  117. qp->r_len += wqe->sg_list[i].length;
  118. j++;
  119. }
  120. qp->r_sge.sge = qp->r_sg_list[0];
  121. qp->r_sge.sg_list = qp->r_sg_list + 1;
  122. qp->r_sge.num_sge = j;
  123. ret = 1;
  124. goto bail;
  125. bad_lkey:
  126. wc.wr_id = wqe->wr_id;
  127. wc.status = IB_WC_LOC_PROT_ERR;
  128. wc.opcode = IB_WC_RECV;
  129. wc.vendor_err = 0;
  130. wc.byte_len = 0;
  131. wc.imm_data = 0;
  132. wc.qp = &qp->ibqp;
  133. wc.src_qp = 0;
  134. wc.wc_flags = 0;
  135. wc.pkey_index = 0;
  136. wc.slid = 0;
  137. wc.sl = 0;
  138. wc.dlid_path_bits = 0;
  139. wc.port_num = 0;
  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. 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. wq = rq->wq;
  176. tail = wq->tail;
  177. /* Validate tail before using it since it is user writable. */
  178. if (tail >= rq->size)
  179. tail = 0;
  180. do {
  181. if (unlikely(tail == wq->head)) {
  182. spin_unlock_irqrestore(&rq->lock, flags);
  183. ret = 0;
  184. goto bail;
  185. }
  186. wqe = get_rwqe_ptr(rq, tail);
  187. if (++tail >= rq->size)
  188. tail = 0;
  189. } while (!wr_id_only && !init_sge(qp, wqe));
  190. qp->r_wr_id = wqe->wr_id;
  191. wq->tail = tail;
  192. ret = 1;
  193. qp->r_wrid_valid = 1;
  194. if (handler) {
  195. u32 n;
  196. /*
  197. * validate head pointer value and compute
  198. * the number of remaining WQEs.
  199. */
  200. n = wq->head;
  201. if (n >= rq->size)
  202. n = 0;
  203. if (n < tail)
  204. n += rq->size - tail;
  205. else
  206. n -= tail;
  207. if (n < srq->limit) {
  208. struct ib_event ev;
  209. srq->limit = 0;
  210. spin_unlock_irqrestore(&rq->lock, flags);
  211. ev.device = qp->ibqp.device;
  212. ev.element.srq = qp->ibqp.srq;
  213. ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
  214. handler(&ev, srq->ibsrq.srq_context);
  215. goto bail;
  216. }
  217. }
  218. spin_unlock_irqrestore(&rq->lock, flags);
  219. bail:
  220. return ret;
  221. }
  222. /**
  223. * ipath_ruc_loopback - handle UC and RC lookback requests
  224. * @sqp: the loopback QP
  225. *
  226. * This is called from ipath_do_uc_send() or ipath_do_rc_send() to
  227. * forward a WQE addressed to the same HCA.
  228. * Note that although we are single threaded due to the tasklet, we still
  229. * have to protect against post_send(). We don't have to worry about
  230. * receive interrupts since this is a connected protocol and all packets
  231. * will pass through here.
  232. */
  233. static void ipath_ruc_loopback(struct ipath_qp *sqp)
  234. {
  235. struct ipath_ibdev *dev = to_idev(sqp->ibqp.device);
  236. struct ipath_qp *qp;
  237. struct ipath_swqe *wqe;
  238. struct ipath_sge *sge;
  239. unsigned long flags;
  240. struct ib_wc wc;
  241. u64 sdata;
  242. atomic64_t *maddr;
  243. qp = ipath_lookup_qpn(&dev->qp_table, sqp->remote_qpn);
  244. if (!qp) {
  245. dev->n_pkt_drops++;
  246. return;
  247. }
  248. again:
  249. spin_lock_irqsave(&sqp->s_lock, flags);
  250. if (!(ib_ipath_state_ops[sqp->state] & IPATH_PROCESS_SEND_OK) ||
  251. qp->s_rnr_timeout) {
  252. spin_unlock_irqrestore(&sqp->s_lock, flags);
  253. goto done;
  254. }
  255. /* Get the next send request. */
  256. if (sqp->s_last == sqp->s_head) {
  257. /* Send work queue is empty. */
  258. spin_unlock_irqrestore(&sqp->s_lock, flags);
  259. goto done;
  260. }
  261. /*
  262. * We can rely on the entry not changing without the s_lock
  263. * being held until we update s_last.
  264. */
  265. wqe = get_swqe_ptr(sqp, sqp->s_last);
  266. spin_unlock_irqrestore(&sqp->s_lock, flags);
  267. wc.wc_flags = 0;
  268. wc.imm_data = 0;
  269. sqp->s_sge.sge = wqe->sg_list[0];
  270. sqp->s_sge.sg_list = wqe->sg_list + 1;
  271. sqp->s_sge.num_sge = wqe->wr.num_sge;
  272. sqp->s_len = wqe->length;
  273. switch (wqe->wr.opcode) {
  274. case IB_WR_SEND_WITH_IMM:
  275. wc.wc_flags = IB_WC_WITH_IMM;
  276. wc.imm_data = wqe->wr.imm_data;
  277. /* FALLTHROUGH */
  278. case IB_WR_SEND:
  279. if (!ipath_get_rwqe(qp, 0)) {
  280. rnr_nak:
  281. /* Handle RNR NAK */
  282. if (qp->ibqp.qp_type == IB_QPT_UC)
  283. goto send_comp;
  284. if (sqp->s_rnr_retry == 0) {
  285. wc.status = IB_WC_RNR_RETRY_EXC_ERR;
  286. goto err;
  287. }
  288. if (sqp->s_rnr_retry_cnt < 7)
  289. sqp->s_rnr_retry--;
  290. dev->n_rnr_naks++;
  291. sqp->s_rnr_timeout =
  292. ib_ipath_rnr_table[qp->r_min_rnr_timer];
  293. ipath_insert_rnr_queue(sqp);
  294. goto done;
  295. }
  296. break;
  297. case IB_WR_RDMA_WRITE_WITH_IMM:
  298. wc.wc_flags = IB_WC_WITH_IMM;
  299. wc.imm_data = wqe->wr.imm_data;
  300. if (!ipath_get_rwqe(qp, 1))
  301. goto rnr_nak;
  302. /* FALLTHROUGH */
  303. case IB_WR_RDMA_WRITE:
  304. if (wqe->length == 0)
  305. break;
  306. if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge, wqe->length,
  307. wqe->wr.wr.rdma.remote_addr,
  308. wqe->wr.wr.rdma.rkey,
  309. IB_ACCESS_REMOTE_WRITE))) {
  310. acc_err:
  311. wc.status = IB_WC_REM_ACCESS_ERR;
  312. err:
  313. wc.wr_id = wqe->wr.wr_id;
  314. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  315. wc.vendor_err = 0;
  316. wc.byte_len = 0;
  317. wc.qp = &sqp->ibqp;
  318. wc.src_qp = sqp->remote_qpn;
  319. wc.pkey_index = 0;
  320. wc.slid = sqp->remote_ah_attr.dlid;
  321. wc.sl = sqp->remote_ah_attr.sl;
  322. wc.dlid_path_bits = 0;
  323. wc.port_num = 0;
  324. spin_lock_irqsave(&sqp->s_lock, flags);
  325. ipath_sqerror_qp(sqp, &wc);
  326. spin_unlock_irqrestore(&sqp->s_lock, flags);
  327. goto done;
  328. }
  329. break;
  330. case IB_WR_RDMA_READ:
  331. if (unlikely(!(qp->qp_access_flags &
  332. IB_ACCESS_REMOTE_READ)))
  333. goto acc_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 &
  346. IB_ACCESS_REMOTE_ATOMIC)))
  347. goto acc_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. goto done;
  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. BUG_ON(len == 0);
  371. ipath_copy_sge(&qp->r_sge, sge->vaddr, len);
  372. sge->vaddr += len;
  373. sge->length -= len;
  374. sge->sge_length -= len;
  375. if (sge->sge_length == 0) {
  376. if (--sqp->s_sge.num_sge)
  377. *sge = *sqp->s_sge.sg_list++;
  378. } else if (sge->length == 0 && sge->mr != NULL) {
  379. if (++sge->n >= IPATH_SEGSZ) {
  380. if (++sge->m >= sge->mr->mapsz)
  381. break;
  382. sge->n = 0;
  383. }
  384. sge->vaddr =
  385. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  386. sge->length =
  387. sge->mr->map[sge->m]->segs[sge->n].length;
  388. }
  389. sqp->s_len -= len;
  390. }
  391. if (wqe->wr.opcode == IB_WR_RDMA_WRITE ||
  392. wqe->wr.opcode == IB_WR_RDMA_READ)
  393. goto send_comp;
  394. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  395. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  396. else
  397. wc.opcode = IB_WC_RECV;
  398. wc.wr_id = qp->r_wr_id;
  399. wc.status = IB_WC_SUCCESS;
  400. wc.vendor_err = 0;
  401. wc.byte_len = wqe->length;
  402. wc.qp = &qp->ibqp;
  403. wc.src_qp = qp->remote_qpn;
  404. /* XXX do we know which pkey matched? Only needed for GSI. */
  405. wc.pkey_index = 0;
  406. wc.slid = qp->remote_ah_attr.dlid;
  407. wc.sl = qp->remote_ah_attr.sl;
  408. wc.dlid_path_bits = 0;
  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. sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
  414. if (!(sqp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
  415. (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
  416. wc.wr_id = wqe->wr.wr_id;
  417. wc.status = IB_WC_SUCCESS;
  418. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  419. wc.vendor_err = 0;
  420. wc.byte_len = wqe->length;
  421. wc.qp = &sqp->ibqp;
  422. wc.src_qp = 0;
  423. wc.pkey_index = 0;
  424. wc.slid = 0;
  425. wc.sl = 0;
  426. wc.dlid_path_bits = 0;
  427. wc.port_num = 0;
  428. ipath_cq_enter(to_icq(sqp->ibqp.send_cq), &wc, 0);
  429. }
  430. /* Update s_last now that we are finished with the SWQE */
  431. spin_lock_irqsave(&sqp->s_lock, flags);
  432. if (++sqp->s_last >= sqp->s_size)
  433. sqp->s_last = 0;
  434. spin_unlock_irqrestore(&sqp->s_lock, flags);
  435. goto again;
  436. done:
  437. if (atomic_dec_and_test(&qp->refcount))
  438. wake_up(&qp->wait);
  439. }
  440. static int want_buffer(struct ipath_devdata *dd)
  441. {
  442. set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl);
  443. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  444. dd->ipath_sendctrl);
  445. return 0;
  446. }
  447. /**
  448. * ipath_no_bufs_available - tell the layer driver we need buffers
  449. * @qp: the QP that caused the problem
  450. * @dev: the device we ran out of buffers on
  451. *
  452. * Called when we run out of PIO buffers.
  453. */
  454. void ipath_no_bufs_available(struct ipath_qp *qp, struct ipath_ibdev *dev)
  455. {
  456. unsigned long flags;
  457. spin_lock_irqsave(&dev->pending_lock, flags);
  458. if (list_empty(&qp->piowait))
  459. list_add_tail(&qp->piowait, &dev->piowait);
  460. spin_unlock_irqrestore(&dev->pending_lock, flags);
  461. /*
  462. * Note that as soon as want_buffer() is called and
  463. * possibly before it returns, ipath_ib_piobufavail()
  464. * could be called. If we are still in the tasklet function,
  465. * tasklet_hi_schedule() will not call us until the next time
  466. * tasklet_hi_schedule() is called.
  467. * We clear the tasklet flag now since we are committing to return
  468. * from the tasklet function.
  469. */
  470. clear_bit(IPATH_S_BUSY, &qp->s_busy);
  471. tasklet_unlock(&qp->s_task);
  472. want_buffer(dev->dd);
  473. dev->n_piowait++;
  474. }
  475. /**
  476. * ipath_post_ruc_send - post RC and UC sends
  477. * @qp: the QP to post on
  478. * @wr: the work request to send
  479. */
  480. int ipath_post_ruc_send(struct ipath_qp *qp, struct ib_send_wr *wr)
  481. {
  482. struct ipath_swqe *wqe;
  483. unsigned long flags;
  484. u32 next;
  485. int i, j;
  486. int acc;
  487. int ret;
  488. /*
  489. * Don't allow RDMA reads or atomic operations on UC or
  490. * undefined operations.
  491. * Make sure buffer is large enough to hold the result for atomics.
  492. */
  493. if (qp->ibqp.qp_type == IB_QPT_UC) {
  494. if ((unsigned) wr->opcode >= IB_WR_RDMA_READ) {
  495. ret = -EINVAL;
  496. goto bail;
  497. }
  498. } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
  499. ret = -EINVAL;
  500. goto bail;
  501. } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
  502. (wr->num_sge == 0 ||
  503. wr->sg_list[0].length < sizeof(u64) ||
  504. wr->sg_list[0].addr & (sizeof(u64) - 1))) {
  505. ret = -EINVAL;
  506. goto bail;
  507. } else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic) {
  508. ret = -EINVAL;
  509. goto bail;
  510. }
  511. /* IB spec says that num_sge == 0 is OK. */
  512. if (wr->num_sge > qp->s_max_sge) {
  513. ret = -ENOMEM;
  514. goto bail;
  515. }
  516. spin_lock_irqsave(&qp->s_lock, flags);
  517. next = qp->s_head + 1;
  518. if (next >= qp->s_size)
  519. next = 0;
  520. if (next == qp->s_last) {
  521. spin_unlock_irqrestore(&qp->s_lock, flags);
  522. ret = -EINVAL;
  523. goto bail;
  524. }
  525. wqe = get_swqe_ptr(qp, qp->s_head);
  526. wqe->wr = *wr;
  527. wqe->ssn = qp->s_ssn++;
  528. wqe->sg_list[0].mr = NULL;
  529. wqe->sg_list[0].vaddr = NULL;
  530. wqe->sg_list[0].length = 0;
  531. wqe->sg_list[0].sge_length = 0;
  532. wqe->length = 0;
  533. acc = wr->opcode >= IB_WR_RDMA_READ ? IB_ACCESS_LOCAL_WRITE : 0;
  534. for (i = 0, j = 0; i < wr->num_sge; i++) {
  535. if (to_ipd(qp->ibqp.pd)->user && wr->sg_list[i].lkey == 0) {
  536. spin_unlock_irqrestore(&qp->s_lock, flags);
  537. ret = -EINVAL;
  538. goto bail;
  539. }
  540. if (wr->sg_list[i].length == 0)
  541. continue;
  542. if (!ipath_lkey_ok(qp, &wqe->sg_list[j], &wr->sg_list[i],
  543. acc)) {
  544. spin_unlock_irqrestore(&qp->s_lock, flags);
  545. ret = -EINVAL;
  546. goto bail;
  547. }
  548. wqe->length += wr->sg_list[i].length;
  549. j++;
  550. }
  551. wqe->wr.num_sge = j;
  552. qp->s_head = next;
  553. spin_unlock_irqrestore(&qp->s_lock, flags);
  554. ipath_do_ruc_send((unsigned long) qp);
  555. ret = 0;
  556. bail:
  557. return ret;
  558. }
  559. /**
  560. * ipath_make_grh - construct a GRH header
  561. * @dev: a pointer to the ipath device
  562. * @hdr: a pointer to the GRH header being constructed
  563. * @grh: the global route address to send to
  564. * @hwords: the number of 32 bit words of header being sent
  565. * @nwords: the number of 32 bit words of data being sent
  566. *
  567. * Return the size of the header in 32 bit words.
  568. */
  569. u32 ipath_make_grh(struct ipath_ibdev *dev, struct ib_grh *hdr,
  570. struct ib_global_route *grh, u32 hwords, u32 nwords)
  571. {
  572. hdr->version_tclass_flow =
  573. cpu_to_be32((6 << 28) |
  574. (grh->traffic_class << 20) |
  575. grh->flow_label);
  576. hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
  577. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  578. hdr->next_hdr = 0x1B;
  579. hdr->hop_limit = grh->hop_limit;
  580. /* The SGID is 32-bit aligned. */
  581. hdr->sgid.global.subnet_prefix = dev->gid_prefix;
  582. hdr->sgid.global.interface_id = dev->dd->ipath_guid;
  583. hdr->dgid = grh->dgid;
  584. /* GRH header size in 32-bit words. */
  585. return sizeof(struct ib_grh) / sizeof(u32);
  586. }
  587. /**
  588. * ipath_do_ruc_send - perform a send on an RC or UC QP
  589. * @data: contains a pointer to the QP
  590. *
  591. * Process entries in the send work queue until credit or queue is
  592. * exhausted. Only allow one CPU to send a packet per QP (tasklet).
  593. * Otherwise, after we drop the QP s_lock, two threads could send
  594. * packets out of order.
  595. */
  596. void ipath_do_ruc_send(unsigned long data)
  597. {
  598. struct ipath_qp *qp = (struct ipath_qp *)data;
  599. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  600. unsigned long flags;
  601. u16 lrh0;
  602. u32 nwords;
  603. u32 extra_bytes;
  604. u32 bth0;
  605. u32 bth2;
  606. u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
  607. struct ipath_other_headers *ohdr;
  608. if (test_and_set_bit(IPATH_S_BUSY, &qp->s_busy))
  609. goto bail;
  610. if (unlikely(qp->remote_ah_attr.dlid == dev->dd->ipath_lid)) {
  611. ipath_ruc_loopback(qp);
  612. goto clear;
  613. }
  614. ohdr = &qp->s_hdr.u.oth;
  615. if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
  616. ohdr = &qp->s_hdr.u.l.oth;
  617. again:
  618. /* Check for a constructed packet to be sent. */
  619. if (qp->s_hdrwords != 0) {
  620. /*
  621. * If no PIO bufs are available, return. An interrupt will
  622. * call ipath_ib_piobufavail() when one is available.
  623. */
  624. if (ipath_verbs_send(dev->dd, qp->s_hdrwords,
  625. (u32 *) &qp->s_hdr, qp->s_cur_size,
  626. qp->s_cur_sge)) {
  627. ipath_no_bufs_available(qp, dev);
  628. goto bail;
  629. }
  630. dev->n_unicast_xmit++;
  631. /* Record that we sent the packet and s_hdr is empty. */
  632. qp->s_hdrwords = 0;
  633. }
  634. /*
  635. * The lock is needed to synchronize between setting
  636. * qp->s_ack_state, resend timer, and post_send().
  637. */
  638. spin_lock_irqsave(&qp->s_lock, flags);
  639. if (!((qp->ibqp.qp_type == IB_QPT_RC) ?
  640. ipath_make_rc_req(qp, ohdr, pmtu, &bth0, &bth2) :
  641. ipath_make_uc_req(qp, ohdr, pmtu, &bth0, &bth2))) {
  642. /*
  643. * Clear the busy bit before unlocking to avoid races with
  644. * adding new work queue items and then failing to process
  645. * them.
  646. */
  647. clear_bit(IPATH_S_BUSY, &qp->s_busy);
  648. spin_unlock_irqrestore(&qp->s_lock, flags);
  649. goto bail;
  650. }
  651. spin_unlock_irqrestore(&qp->s_lock, flags);
  652. /* Construct the header. */
  653. extra_bytes = (4 - qp->s_cur_size) & 3;
  654. nwords = (qp->s_cur_size + extra_bytes) >> 2;
  655. lrh0 = IPATH_LRH_BTH;
  656. if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
  657. qp->s_hdrwords += ipath_make_grh(dev, &qp->s_hdr.u.l.grh,
  658. &qp->remote_ah_attr.grh,
  659. qp->s_hdrwords, nwords);
  660. lrh0 = IPATH_LRH_GRH;
  661. }
  662. lrh0 |= qp->remote_ah_attr.sl << 4;
  663. qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
  664. qp->s_hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
  665. qp->s_hdr.lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords +
  666. SIZE_OF_CRC);
  667. qp->s_hdr.lrh[3] = cpu_to_be16(dev->dd->ipath_lid);
  668. bth0 |= ipath_get_pkey(dev->dd, qp->s_pkey_index);
  669. bth0 |= extra_bytes << 20;
  670. ohdr->bth[0] = cpu_to_be32(bth0);
  671. ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
  672. ohdr->bth[2] = cpu_to_be32(bth2);
  673. /* Check for more work to do. */
  674. goto again;
  675. clear:
  676. clear_bit(IPATH_S_BUSY, &qp->s_busy);
  677. bail:
  678. return;
  679. }