ipath_ruc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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_num = qp->ibqp.qp_num;
  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. if (handler) {
  194. u32 n;
  195. /*
  196. * validate head pointer value and compute
  197. * the number of remaining WQEs.
  198. */
  199. n = wq->head;
  200. if (n >= rq->size)
  201. n = 0;
  202. if (n < tail)
  203. n += rq->size - tail;
  204. else
  205. n -= tail;
  206. if (n < srq->limit) {
  207. struct ib_event ev;
  208. srq->limit = 0;
  209. spin_unlock_irqrestore(&rq->lock, flags);
  210. ev.device = qp->ibqp.device;
  211. ev.element.srq = qp->ibqp.srq;
  212. ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
  213. handler(&ev, srq->ibsrq.srq_context);
  214. goto bail;
  215. }
  216. }
  217. spin_unlock_irqrestore(&rq->lock, flags);
  218. bail:
  219. return ret;
  220. }
  221. /**
  222. * ipath_ruc_loopback - handle UC and RC lookback requests
  223. * @sqp: the loopback QP
  224. *
  225. * This is called from ipath_do_uc_send() or ipath_do_rc_send() to
  226. * forward a WQE addressed to the same HCA.
  227. * Note that although we are single threaded due to the tasklet, we still
  228. * have to protect against post_send(). We don't have to worry about
  229. * receive interrupts since this is a connected protocol and all packets
  230. * will pass through here.
  231. */
  232. static void ipath_ruc_loopback(struct ipath_qp *sqp)
  233. {
  234. struct ipath_ibdev *dev = to_idev(sqp->ibqp.device);
  235. struct ipath_qp *qp;
  236. struct ipath_swqe *wqe;
  237. struct ipath_sge *sge;
  238. unsigned long flags;
  239. struct ib_wc wc;
  240. u64 sdata;
  241. qp = ipath_lookup_qpn(&dev->qp_table, sqp->remote_qpn);
  242. if (!qp) {
  243. dev->n_pkt_drops++;
  244. return;
  245. }
  246. again:
  247. spin_lock_irqsave(&sqp->s_lock, flags);
  248. if (!(ib_ipath_state_ops[sqp->state] & IPATH_PROCESS_SEND_OK)) {
  249. spin_unlock_irqrestore(&sqp->s_lock, flags);
  250. goto done;
  251. }
  252. /* Get the next send request. */
  253. if (sqp->s_last == sqp->s_head) {
  254. /* Send work queue is empty. */
  255. spin_unlock_irqrestore(&sqp->s_lock, flags);
  256. goto done;
  257. }
  258. /*
  259. * We can rely on the entry not changing without the s_lock
  260. * being held until we update s_last.
  261. */
  262. wqe = get_swqe_ptr(sqp, sqp->s_last);
  263. spin_unlock_irqrestore(&sqp->s_lock, flags);
  264. wc.wc_flags = 0;
  265. wc.imm_data = 0;
  266. sqp->s_sge.sge = wqe->sg_list[0];
  267. sqp->s_sge.sg_list = wqe->sg_list + 1;
  268. sqp->s_sge.num_sge = wqe->wr.num_sge;
  269. sqp->s_len = wqe->length;
  270. switch (wqe->wr.opcode) {
  271. case IB_WR_SEND_WITH_IMM:
  272. wc.wc_flags = IB_WC_WITH_IMM;
  273. wc.imm_data = wqe->wr.imm_data;
  274. /* FALLTHROUGH */
  275. case IB_WR_SEND:
  276. if (!ipath_get_rwqe(qp, 0)) {
  277. rnr_nak:
  278. /* Handle RNR NAK */
  279. if (qp->ibqp.qp_type == IB_QPT_UC)
  280. goto send_comp;
  281. if (sqp->s_rnr_retry == 0) {
  282. wc.status = IB_WC_RNR_RETRY_EXC_ERR;
  283. goto err;
  284. }
  285. if (sqp->s_rnr_retry_cnt < 7)
  286. sqp->s_rnr_retry--;
  287. dev->n_rnr_naks++;
  288. sqp->s_rnr_timeout =
  289. ib_ipath_rnr_table[sqp->r_min_rnr_timer];
  290. ipath_insert_rnr_queue(sqp);
  291. goto done;
  292. }
  293. break;
  294. case IB_WR_RDMA_WRITE_WITH_IMM:
  295. wc.wc_flags = IB_WC_WITH_IMM;
  296. wc.imm_data = wqe->wr.imm_data;
  297. if (!ipath_get_rwqe(qp, 1))
  298. goto rnr_nak;
  299. /* FALLTHROUGH */
  300. case IB_WR_RDMA_WRITE:
  301. if (wqe->length == 0)
  302. break;
  303. if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge, wqe->length,
  304. wqe->wr.wr.rdma.remote_addr,
  305. wqe->wr.wr.rdma.rkey,
  306. IB_ACCESS_REMOTE_WRITE))) {
  307. acc_err:
  308. wc.status = IB_WC_REM_ACCESS_ERR;
  309. err:
  310. wc.wr_id = wqe->wr.wr_id;
  311. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  312. wc.vendor_err = 0;
  313. wc.byte_len = 0;
  314. wc.qp_num = sqp->ibqp.qp_num;
  315. wc.src_qp = sqp->remote_qpn;
  316. wc.pkey_index = 0;
  317. wc.slid = sqp->remote_ah_attr.dlid;
  318. wc.sl = sqp->remote_ah_attr.sl;
  319. wc.dlid_path_bits = 0;
  320. wc.port_num = 0;
  321. ipath_sqerror_qp(sqp, &wc);
  322. goto done;
  323. }
  324. break;
  325. case IB_WR_RDMA_READ:
  326. if (unlikely(!ipath_rkey_ok(qp, &sqp->s_sge, wqe->length,
  327. wqe->wr.wr.rdma.remote_addr,
  328. wqe->wr.wr.rdma.rkey,
  329. IB_ACCESS_REMOTE_READ)))
  330. goto acc_err;
  331. if (unlikely(!(qp->qp_access_flags &
  332. IB_ACCESS_REMOTE_READ)))
  333. goto acc_err;
  334. qp->r_sge.sge = wqe->sg_list[0];
  335. qp->r_sge.sg_list = wqe->sg_list + 1;
  336. qp->r_sge.num_sge = wqe->wr.num_sge;
  337. break;
  338. case IB_WR_ATOMIC_CMP_AND_SWP:
  339. case IB_WR_ATOMIC_FETCH_AND_ADD:
  340. if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge, sizeof(u64),
  341. wqe->wr.wr.rdma.remote_addr,
  342. wqe->wr.wr.rdma.rkey,
  343. IB_ACCESS_REMOTE_ATOMIC)))
  344. goto acc_err;
  345. /* Perform atomic OP and save result. */
  346. sdata = wqe->wr.wr.atomic.swap;
  347. spin_lock_irqsave(&dev->pending_lock, flags);
  348. qp->r_atomic_data = *(u64 *) qp->r_sge.sge.vaddr;
  349. if (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
  350. *(u64 *) qp->r_sge.sge.vaddr =
  351. qp->r_atomic_data + sdata;
  352. else if (qp->r_atomic_data == wqe->wr.wr.atomic.compare_add)
  353. *(u64 *) qp->r_sge.sge.vaddr = sdata;
  354. spin_unlock_irqrestore(&dev->pending_lock, flags);
  355. *(u64 *) sqp->s_sge.sge.vaddr = qp->r_atomic_data;
  356. goto send_comp;
  357. default:
  358. goto done;
  359. }
  360. sge = &sqp->s_sge.sge;
  361. while (sqp->s_len) {
  362. u32 len = sqp->s_len;
  363. if (len > sge->length)
  364. len = sge->length;
  365. BUG_ON(len == 0);
  366. ipath_copy_sge(&qp->r_sge, sge->vaddr, len);
  367. sge->vaddr += len;
  368. sge->length -= len;
  369. sge->sge_length -= len;
  370. if (sge->sge_length == 0) {
  371. if (--sqp->s_sge.num_sge)
  372. *sge = *sqp->s_sge.sg_list++;
  373. } else if (sge->length == 0 && sge->mr != NULL) {
  374. if (++sge->n >= IPATH_SEGSZ) {
  375. if (++sge->m >= sge->mr->mapsz)
  376. break;
  377. sge->n = 0;
  378. }
  379. sge->vaddr =
  380. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  381. sge->length =
  382. sge->mr->map[sge->m]->segs[sge->n].length;
  383. }
  384. sqp->s_len -= len;
  385. }
  386. if (wqe->wr.opcode == IB_WR_RDMA_WRITE ||
  387. wqe->wr.opcode == IB_WR_RDMA_READ)
  388. goto send_comp;
  389. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  390. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  391. else
  392. wc.opcode = IB_WC_RECV;
  393. wc.wr_id = qp->r_wr_id;
  394. wc.status = IB_WC_SUCCESS;
  395. wc.vendor_err = 0;
  396. wc.byte_len = wqe->length;
  397. wc.qp_num = qp->ibqp.qp_num;
  398. wc.src_qp = qp->remote_qpn;
  399. /* XXX do we know which pkey matched? Only needed for GSI. */
  400. wc.pkey_index = 0;
  401. wc.slid = qp->remote_ah_attr.dlid;
  402. wc.sl = qp->remote_ah_attr.sl;
  403. wc.dlid_path_bits = 0;
  404. /* Signal completion event if the solicited bit is set. */
  405. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
  406. wqe->wr.send_flags & IB_SEND_SOLICITED);
  407. send_comp:
  408. sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
  409. if (!test_bit(IPATH_S_SIGNAL_REQ_WR, &sqp->s_flags) ||
  410. (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
  411. wc.wr_id = wqe->wr.wr_id;
  412. wc.status = IB_WC_SUCCESS;
  413. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  414. wc.vendor_err = 0;
  415. wc.byte_len = wqe->length;
  416. wc.qp_num = sqp->ibqp.qp_num;
  417. wc.src_qp = 0;
  418. wc.pkey_index = 0;
  419. wc.slid = 0;
  420. wc.sl = 0;
  421. wc.dlid_path_bits = 0;
  422. wc.port_num = 0;
  423. ipath_cq_enter(to_icq(sqp->ibqp.send_cq), &wc, 0);
  424. }
  425. /* Update s_last now that we are finished with the SWQE */
  426. spin_lock_irqsave(&sqp->s_lock, flags);
  427. if (++sqp->s_last >= sqp->s_size)
  428. sqp->s_last = 0;
  429. spin_unlock_irqrestore(&sqp->s_lock, flags);
  430. goto again;
  431. done:
  432. if (atomic_dec_and_test(&qp->refcount))
  433. wake_up(&qp->wait);
  434. }
  435. static int want_buffer(struct ipath_devdata *dd)
  436. {
  437. set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl);
  438. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  439. dd->ipath_sendctrl);
  440. return 0;
  441. }
  442. /**
  443. * ipath_no_bufs_available - tell the layer driver we need buffers
  444. * @qp: the QP that caused the problem
  445. * @dev: the device we ran out of buffers on
  446. *
  447. * Called when we run out of PIO buffers.
  448. */
  449. void ipath_no_bufs_available(struct ipath_qp *qp, struct ipath_ibdev *dev)
  450. {
  451. unsigned long flags;
  452. spin_lock_irqsave(&dev->pending_lock, flags);
  453. if (list_empty(&qp->piowait))
  454. list_add_tail(&qp->piowait, &dev->piowait);
  455. spin_unlock_irqrestore(&dev->pending_lock, flags);
  456. /*
  457. * Note that as soon as want_buffer() is called and
  458. * possibly before it returns, ipath_ib_piobufavail()
  459. * could be called. If we are still in the tasklet function,
  460. * tasklet_hi_schedule() will not call us until the next time
  461. * tasklet_hi_schedule() is called.
  462. * We clear the tasklet flag now since we are committing to return
  463. * from the tasklet function.
  464. */
  465. clear_bit(IPATH_S_BUSY, &qp->s_flags);
  466. tasklet_unlock(&qp->s_task);
  467. want_buffer(dev->dd);
  468. dev->n_piowait++;
  469. }
  470. /**
  471. * ipath_post_ruc_send - post RC and UC sends
  472. * @qp: the QP to post on
  473. * @wr: the work request to send
  474. */
  475. int ipath_post_ruc_send(struct ipath_qp *qp, struct ib_send_wr *wr)
  476. {
  477. struct ipath_swqe *wqe;
  478. unsigned long flags;
  479. u32 next;
  480. int i, j;
  481. int acc;
  482. int ret;
  483. /*
  484. * Don't allow RDMA reads or atomic operations on UC or
  485. * undefined operations.
  486. * Make sure buffer is large enough to hold the result for atomics.
  487. */
  488. if (qp->ibqp.qp_type == IB_QPT_UC) {
  489. if ((unsigned) wr->opcode >= IB_WR_RDMA_READ) {
  490. ret = -EINVAL;
  491. goto bail;
  492. }
  493. } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
  494. ret = -EINVAL;
  495. goto bail;
  496. } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
  497. (wr->num_sge == 0 ||
  498. wr->sg_list[0].length < sizeof(u64) ||
  499. wr->sg_list[0].addr & (sizeof(u64) - 1))) {
  500. ret = -EINVAL;
  501. goto bail;
  502. }
  503. /* IB spec says that num_sge == 0 is OK. */
  504. if (wr->num_sge > qp->s_max_sge) {
  505. ret = -ENOMEM;
  506. goto bail;
  507. }
  508. spin_lock_irqsave(&qp->s_lock, flags);
  509. next = qp->s_head + 1;
  510. if (next >= qp->s_size)
  511. next = 0;
  512. if (next == qp->s_last) {
  513. spin_unlock_irqrestore(&qp->s_lock, flags);
  514. ret = -EINVAL;
  515. goto bail;
  516. }
  517. wqe = get_swqe_ptr(qp, qp->s_head);
  518. wqe->wr = *wr;
  519. wqe->ssn = qp->s_ssn++;
  520. wqe->sg_list[0].mr = NULL;
  521. wqe->sg_list[0].vaddr = NULL;
  522. wqe->sg_list[0].length = 0;
  523. wqe->sg_list[0].sge_length = 0;
  524. wqe->length = 0;
  525. acc = wr->opcode >= IB_WR_RDMA_READ ? IB_ACCESS_LOCAL_WRITE : 0;
  526. for (i = 0, j = 0; i < wr->num_sge; i++) {
  527. if (to_ipd(qp->ibqp.pd)->user && wr->sg_list[i].lkey == 0) {
  528. spin_unlock_irqrestore(&qp->s_lock, flags);
  529. ret = -EINVAL;
  530. goto bail;
  531. }
  532. if (wr->sg_list[i].length == 0)
  533. continue;
  534. if (!ipath_lkey_ok(qp, &wqe->sg_list[j], &wr->sg_list[i],
  535. acc)) {
  536. spin_unlock_irqrestore(&qp->s_lock, flags);
  537. ret = -EINVAL;
  538. goto bail;
  539. }
  540. wqe->length += wr->sg_list[i].length;
  541. j++;
  542. }
  543. wqe->wr.num_sge = j;
  544. qp->s_head = next;
  545. spin_unlock_irqrestore(&qp->s_lock, flags);
  546. ipath_do_ruc_send((unsigned long) qp);
  547. ret = 0;
  548. bail:
  549. return ret;
  550. }
  551. /**
  552. * ipath_make_grh - construct a GRH header
  553. * @dev: a pointer to the ipath device
  554. * @hdr: a pointer to the GRH header being constructed
  555. * @grh: the global route address to send to
  556. * @hwords: the number of 32 bit words of header being sent
  557. * @nwords: the number of 32 bit words of data being sent
  558. *
  559. * Return the size of the header in 32 bit words.
  560. */
  561. u32 ipath_make_grh(struct ipath_ibdev *dev, struct ib_grh *hdr,
  562. struct ib_global_route *grh, u32 hwords, u32 nwords)
  563. {
  564. hdr->version_tclass_flow =
  565. cpu_to_be32((6 << 28) |
  566. (grh->traffic_class << 20) |
  567. grh->flow_label);
  568. hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
  569. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  570. hdr->next_hdr = 0x1B;
  571. hdr->hop_limit = grh->hop_limit;
  572. /* The SGID is 32-bit aligned. */
  573. hdr->sgid.global.subnet_prefix = dev->gid_prefix;
  574. hdr->sgid.global.interface_id = dev->dd->ipath_guid;
  575. hdr->dgid = grh->dgid;
  576. /* GRH header size in 32-bit words. */
  577. return sizeof(struct ib_grh) / sizeof(u32);
  578. }
  579. /**
  580. * ipath_do_ruc_send - perform a send on an RC or UC 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, after we drop the QP s_lock, two threads could send
  586. * packets out of order.
  587. */
  588. void ipath_do_ruc_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. unsigned long flags;
  593. u16 lrh0;
  594. u32 nwords;
  595. u32 extra_bytes;
  596. u32 bth0;
  597. u32 bth2;
  598. u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
  599. struct ipath_other_headers *ohdr;
  600. if (test_and_set_bit(IPATH_S_BUSY, &qp->s_flags))
  601. goto bail;
  602. if (unlikely(qp->remote_ah_attr.dlid == dev->dd->ipath_lid)) {
  603. ipath_ruc_loopback(qp);
  604. goto clear;
  605. }
  606. ohdr = &qp->s_hdr.u.oth;
  607. if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
  608. ohdr = &qp->s_hdr.u.l.oth;
  609. again:
  610. /* Check for a constructed packet to be sent. */
  611. if (qp->s_hdrwords != 0) {
  612. /*
  613. * If no PIO bufs are available, return. An interrupt will
  614. * call ipath_ib_piobufavail() when one is available.
  615. */
  616. if (ipath_verbs_send(dev->dd, qp->s_hdrwords,
  617. (u32 *) &qp->s_hdr, qp->s_cur_size,
  618. qp->s_cur_sge)) {
  619. ipath_no_bufs_available(qp, dev);
  620. goto bail;
  621. }
  622. dev->n_unicast_xmit++;
  623. /* Record that we sent the packet and s_hdr is empty. */
  624. qp->s_hdrwords = 0;
  625. }
  626. /*
  627. * The lock is needed to synchronize between setting
  628. * qp->s_ack_state, resend timer, and post_send().
  629. */
  630. spin_lock_irqsave(&qp->s_lock, flags);
  631. /* Sending responses has higher priority over sending requests. */
  632. if (qp->s_ack_state != IB_OPCODE_RC_ACKNOWLEDGE &&
  633. (bth0 = ipath_make_rc_ack(qp, ohdr, pmtu)) != 0)
  634. bth2 = qp->s_ack_psn++ & IPATH_PSN_MASK;
  635. else if (!((qp->ibqp.qp_type == IB_QPT_RC) ?
  636. ipath_make_rc_req(qp, ohdr, pmtu, &bth0, &bth2) :
  637. ipath_make_uc_req(qp, ohdr, pmtu, &bth0, &bth2))) {
  638. /*
  639. * Clear the busy bit before unlocking to avoid races with
  640. * adding new work queue items and then failing to process
  641. * them.
  642. */
  643. clear_bit(IPATH_S_BUSY, &qp->s_flags);
  644. spin_unlock_irqrestore(&qp->s_lock, flags);
  645. goto bail;
  646. }
  647. spin_unlock_irqrestore(&qp->s_lock, flags);
  648. /* Construct the header. */
  649. extra_bytes = (4 - qp->s_cur_size) & 3;
  650. nwords = (qp->s_cur_size + extra_bytes) >> 2;
  651. lrh0 = IPATH_LRH_BTH;
  652. if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
  653. qp->s_hdrwords += ipath_make_grh(dev, &qp->s_hdr.u.l.grh,
  654. &qp->remote_ah_attr.grh,
  655. qp->s_hdrwords, nwords);
  656. lrh0 = IPATH_LRH_GRH;
  657. }
  658. lrh0 |= qp->remote_ah_attr.sl << 4;
  659. qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
  660. qp->s_hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
  661. qp->s_hdr.lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords +
  662. SIZE_OF_CRC);
  663. qp->s_hdr.lrh[3] = cpu_to_be16(dev->dd->ipath_lid);
  664. bth0 |= ipath_get_pkey(dev->dd, qp->s_pkey_index);
  665. bth0 |= extra_bytes << 20;
  666. ohdr->bth[0] = cpu_to_be32(bth0);
  667. ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
  668. ohdr->bth[2] = cpu_to_be32(bth2);
  669. /* Check for more work to do. */
  670. goto again;
  671. clear:
  672. clear_bit(IPATH_S_BUSY, &qp->s_flags);
  673. bail:
  674. return;
  675. }