ipath_ruc.c 19 KB

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