ipath_ruc.c 19 KB

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