ipath_ruc.c 19 KB

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