ipath_ruc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include "ipath_verbs.h"
  33. /*
  34. * Convert the AETH RNR timeout code into the number of milliseconds.
  35. */
  36. const u32 ib_ipath_rnr_table[32] = {
  37. 656, /* 0 */
  38. 1, /* 1 */
  39. 1, /* 2 */
  40. 1, /* 3 */
  41. 1, /* 4 */
  42. 1, /* 5 */
  43. 1, /* 6 */
  44. 1, /* 7 */
  45. 1, /* 8 */
  46. 1, /* 9 */
  47. 1, /* A */
  48. 1, /* B */
  49. 1, /* C */
  50. 1, /* D */
  51. 2, /* E */
  52. 2, /* F */
  53. 3, /* 10 */
  54. 4, /* 11 */
  55. 6, /* 12 */
  56. 8, /* 13 */
  57. 11, /* 14 */
  58. 16, /* 15 */
  59. 21, /* 16 */
  60. 31, /* 17 */
  61. 41, /* 18 */
  62. 62, /* 19 */
  63. 82, /* 1A */
  64. 123, /* 1B */
  65. 164, /* 1C */
  66. 246, /* 1D */
  67. 328, /* 1E */
  68. 492 /* 1F */
  69. };
  70. /**
  71. * ipath_insert_rnr_queue - put QP on the RNR timeout list for the device
  72. * @qp: the QP
  73. *
  74. * XXX Use a simple list for now. We might need a priority
  75. * queue if we have lots of QPs waiting for RNR timeouts
  76. * but that should be rare.
  77. */
  78. void ipath_insert_rnr_queue(struct ipath_qp *qp)
  79. {
  80. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  81. unsigned long flags;
  82. spin_lock_irqsave(&dev->pending_lock, flags);
  83. if (list_empty(&dev->rnrwait))
  84. list_add(&qp->timerwait, &dev->rnrwait);
  85. else {
  86. struct list_head *l = &dev->rnrwait;
  87. struct ipath_qp *nqp = list_entry(l->next, struct ipath_qp,
  88. timerwait);
  89. while (qp->s_rnr_timeout >= nqp->s_rnr_timeout) {
  90. qp->s_rnr_timeout -= nqp->s_rnr_timeout;
  91. l = l->next;
  92. if (l->next == &dev->rnrwait)
  93. break;
  94. nqp = list_entry(l->next, struct ipath_qp,
  95. timerwait);
  96. }
  97. list_add(&qp->timerwait, l);
  98. }
  99. spin_unlock_irqrestore(&dev->pending_lock, flags);
  100. }
  101. /**
  102. * ipath_get_rwqe - copy the next RWQE into the QP's RWQE
  103. * @qp: the QP
  104. * @wr_id_only: update wr_id only, not SGEs
  105. *
  106. * Return 0 if no RWQE is available, otherwise return 1.
  107. *
  108. * Called at interrupt level with the QP r_rq.lock held.
  109. */
  110. int ipath_get_rwqe(struct ipath_qp *qp, int wr_id_only)
  111. {
  112. struct ipath_rq *rq;
  113. struct ipath_srq *srq;
  114. struct ipath_rwqe *wqe;
  115. int ret;
  116. if (!qp->ibqp.srq) {
  117. rq = &qp->r_rq;
  118. if (unlikely(rq->tail == rq->head)) {
  119. ret = 0;
  120. goto bail;
  121. }
  122. wqe = get_rwqe_ptr(rq, rq->tail);
  123. qp->r_wr_id = wqe->wr_id;
  124. if (!wr_id_only) {
  125. qp->r_sge.sge = wqe->sg_list[0];
  126. qp->r_sge.sg_list = wqe->sg_list + 1;
  127. qp->r_sge.num_sge = wqe->num_sge;
  128. qp->r_len = wqe->length;
  129. }
  130. if (++rq->tail >= rq->size)
  131. rq->tail = 0;
  132. ret = 1;
  133. goto bail;
  134. }
  135. srq = to_isrq(qp->ibqp.srq);
  136. rq = &srq->rq;
  137. spin_lock(&rq->lock);
  138. if (unlikely(rq->tail == rq->head)) {
  139. spin_unlock(&rq->lock);
  140. ret = 0;
  141. goto bail;
  142. }
  143. wqe = get_rwqe_ptr(rq, rq->tail);
  144. qp->r_wr_id = wqe->wr_id;
  145. if (!wr_id_only) {
  146. qp->r_sge.sge = wqe->sg_list[0];
  147. qp->r_sge.sg_list = wqe->sg_list + 1;
  148. qp->r_sge.num_sge = wqe->num_sge;
  149. qp->r_len = wqe->length;
  150. }
  151. if (++rq->tail >= rq->size)
  152. rq->tail = 0;
  153. if (srq->ibsrq.event_handler) {
  154. struct ib_event ev;
  155. u32 n;
  156. if (rq->head < rq->tail)
  157. n = rq->size + rq->head - rq->tail;
  158. else
  159. n = rq->head - rq->tail;
  160. if (n < srq->limit) {
  161. srq->limit = 0;
  162. spin_unlock(&rq->lock);
  163. ev.device = qp->ibqp.device;
  164. ev.element.srq = qp->ibqp.srq;
  165. ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
  166. srq->ibsrq.event_handler(&ev,
  167. srq->ibsrq.srq_context);
  168. } else
  169. spin_unlock(&rq->lock);
  170. } else
  171. spin_unlock(&rq->lock);
  172. ret = 1;
  173. bail:
  174. return ret;
  175. }
  176. /**
  177. * ipath_ruc_loopback - handle UC and RC lookback requests
  178. * @sqp: the loopback QP
  179. * @wc: the work completion entry
  180. *
  181. * This is called from ipath_do_uc_send() or ipath_do_rc_send() to
  182. * forward a WQE addressed to the same HCA.
  183. * Note that although we are single threaded due to the tasklet, we still
  184. * have to protect against post_send(). We don't have to worry about
  185. * receive interrupts since this is a connected protocol and all packets
  186. * will pass through here.
  187. */
  188. void ipath_ruc_loopback(struct ipath_qp *sqp, struct ib_wc *wc)
  189. {
  190. struct ipath_ibdev *dev = to_idev(sqp->ibqp.device);
  191. struct ipath_qp *qp;
  192. struct ipath_swqe *wqe;
  193. struct ipath_sge *sge;
  194. unsigned long flags;
  195. u64 sdata;
  196. qp = ipath_lookup_qpn(&dev->qp_table, sqp->remote_qpn);
  197. if (!qp) {
  198. dev->n_pkt_drops++;
  199. return;
  200. }
  201. again:
  202. spin_lock_irqsave(&sqp->s_lock, flags);
  203. if (!(ib_ipath_state_ops[sqp->state] & IPATH_PROCESS_SEND_OK)) {
  204. spin_unlock_irqrestore(&sqp->s_lock, flags);
  205. goto done;
  206. }
  207. /* Get the next send request. */
  208. if (sqp->s_last == sqp->s_head) {
  209. /* Send work queue is empty. */
  210. spin_unlock_irqrestore(&sqp->s_lock, flags);
  211. goto done;
  212. }
  213. /*
  214. * We can rely on the entry not changing without the s_lock
  215. * being held until we update s_last.
  216. */
  217. wqe = get_swqe_ptr(sqp, sqp->s_last);
  218. spin_unlock_irqrestore(&sqp->s_lock, flags);
  219. wc->wc_flags = 0;
  220. wc->imm_data = 0;
  221. sqp->s_sge.sge = wqe->sg_list[0];
  222. sqp->s_sge.sg_list = wqe->sg_list + 1;
  223. sqp->s_sge.num_sge = wqe->wr.num_sge;
  224. sqp->s_len = wqe->length;
  225. switch (wqe->wr.opcode) {
  226. case IB_WR_SEND_WITH_IMM:
  227. wc->wc_flags = IB_WC_WITH_IMM;
  228. wc->imm_data = wqe->wr.imm_data;
  229. /* FALLTHROUGH */
  230. case IB_WR_SEND:
  231. spin_lock_irqsave(&qp->r_rq.lock, flags);
  232. if (!ipath_get_rwqe(qp, 0)) {
  233. rnr_nak:
  234. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  235. /* Handle RNR NAK */
  236. if (qp->ibqp.qp_type == IB_QPT_UC)
  237. goto send_comp;
  238. if (sqp->s_rnr_retry == 0) {
  239. wc->status = IB_WC_RNR_RETRY_EXC_ERR;
  240. goto err;
  241. }
  242. if (sqp->s_rnr_retry_cnt < 7)
  243. sqp->s_rnr_retry--;
  244. dev->n_rnr_naks++;
  245. sqp->s_rnr_timeout =
  246. ib_ipath_rnr_table[sqp->s_min_rnr_timer];
  247. ipath_insert_rnr_queue(sqp);
  248. goto done;
  249. }
  250. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  251. break;
  252. case IB_WR_RDMA_WRITE_WITH_IMM:
  253. wc->wc_flags = IB_WC_WITH_IMM;
  254. wc->imm_data = wqe->wr.imm_data;
  255. spin_lock_irqsave(&qp->r_rq.lock, flags);
  256. if (!ipath_get_rwqe(qp, 1))
  257. goto rnr_nak;
  258. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  259. /* FALLTHROUGH */
  260. case IB_WR_RDMA_WRITE:
  261. if (wqe->length == 0)
  262. break;
  263. if (unlikely(!ipath_rkey_ok(dev, &qp->r_sge, wqe->length,
  264. wqe->wr.wr.rdma.remote_addr,
  265. wqe->wr.wr.rdma.rkey,
  266. IB_ACCESS_REMOTE_WRITE))) {
  267. acc_err:
  268. wc->status = IB_WC_REM_ACCESS_ERR;
  269. err:
  270. wc->wr_id = wqe->wr.wr_id;
  271. wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  272. wc->vendor_err = 0;
  273. wc->byte_len = 0;
  274. wc->qp_num = sqp->ibqp.qp_num;
  275. wc->src_qp = sqp->remote_qpn;
  276. wc->pkey_index = 0;
  277. wc->slid = sqp->remote_ah_attr.dlid;
  278. wc->sl = sqp->remote_ah_attr.sl;
  279. wc->dlid_path_bits = 0;
  280. wc->port_num = 0;
  281. ipath_sqerror_qp(sqp, wc);
  282. goto done;
  283. }
  284. break;
  285. case IB_WR_RDMA_READ:
  286. if (unlikely(!ipath_rkey_ok(dev, &sqp->s_sge, wqe->length,
  287. wqe->wr.wr.rdma.remote_addr,
  288. wqe->wr.wr.rdma.rkey,
  289. IB_ACCESS_REMOTE_READ)))
  290. goto acc_err;
  291. if (unlikely(!(qp->qp_access_flags &
  292. IB_ACCESS_REMOTE_READ)))
  293. goto acc_err;
  294. qp->r_sge.sge = wqe->sg_list[0];
  295. qp->r_sge.sg_list = wqe->sg_list + 1;
  296. qp->r_sge.num_sge = wqe->wr.num_sge;
  297. break;
  298. case IB_WR_ATOMIC_CMP_AND_SWP:
  299. case IB_WR_ATOMIC_FETCH_AND_ADD:
  300. if (unlikely(!ipath_rkey_ok(dev, &qp->r_sge, sizeof(u64),
  301. wqe->wr.wr.rdma.remote_addr,
  302. wqe->wr.wr.rdma.rkey,
  303. IB_ACCESS_REMOTE_ATOMIC)))
  304. goto acc_err;
  305. /* Perform atomic OP and save result. */
  306. sdata = wqe->wr.wr.atomic.swap;
  307. spin_lock_irqsave(&dev->pending_lock, flags);
  308. qp->r_atomic_data = *(u64 *) qp->r_sge.sge.vaddr;
  309. if (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
  310. *(u64 *) qp->r_sge.sge.vaddr =
  311. qp->r_atomic_data + sdata;
  312. else if (qp->r_atomic_data == wqe->wr.wr.atomic.compare_add)
  313. *(u64 *) qp->r_sge.sge.vaddr = sdata;
  314. spin_unlock_irqrestore(&dev->pending_lock, flags);
  315. *(u64 *) sqp->s_sge.sge.vaddr = qp->r_atomic_data;
  316. goto send_comp;
  317. default:
  318. goto done;
  319. }
  320. sge = &sqp->s_sge.sge;
  321. while (sqp->s_len) {
  322. u32 len = sqp->s_len;
  323. if (len > sge->length)
  324. len = sge->length;
  325. BUG_ON(len == 0);
  326. ipath_copy_sge(&qp->r_sge, sge->vaddr, len);
  327. sge->vaddr += len;
  328. sge->length -= len;
  329. sge->sge_length -= len;
  330. if (sge->sge_length == 0) {
  331. if (--sqp->s_sge.num_sge)
  332. *sge = *sqp->s_sge.sg_list++;
  333. } else if (sge->length == 0 && sge->mr != NULL) {
  334. if (++sge->n >= IPATH_SEGSZ) {
  335. if (++sge->m >= sge->mr->mapsz)
  336. break;
  337. sge->n = 0;
  338. }
  339. sge->vaddr =
  340. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  341. sge->length =
  342. sge->mr->map[sge->m]->segs[sge->n].length;
  343. }
  344. sqp->s_len -= len;
  345. }
  346. if (wqe->wr.opcode == IB_WR_RDMA_WRITE ||
  347. wqe->wr.opcode == IB_WR_RDMA_READ)
  348. goto send_comp;
  349. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  350. wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
  351. else
  352. wc->opcode = IB_WC_RECV;
  353. wc->wr_id = qp->r_wr_id;
  354. wc->status = IB_WC_SUCCESS;
  355. wc->vendor_err = 0;
  356. wc->byte_len = wqe->length;
  357. wc->qp_num = qp->ibqp.qp_num;
  358. wc->src_qp = qp->remote_qpn;
  359. /* XXX do we know which pkey matched? Only needed for GSI. */
  360. wc->pkey_index = 0;
  361. wc->slid = qp->remote_ah_attr.dlid;
  362. wc->sl = qp->remote_ah_attr.sl;
  363. wc->dlid_path_bits = 0;
  364. /* Signal completion event if the solicited bit is set. */
  365. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), wc,
  366. wqe->wr.send_flags & IB_SEND_SOLICITED);
  367. send_comp:
  368. sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
  369. if (!test_bit(IPATH_S_SIGNAL_REQ_WR, &sqp->s_flags) ||
  370. (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
  371. wc->wr_id = wqe->wr.wr_id;
  372. wc->status = IB_WC_SUCCESS;
  373. wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  374. wc->vendor_err = 0;
  375. wc->byte_len = wqe->length;
  376. wc->qp_num = sqp->ibqp.qp_num;
  377. wc->src_qp = 0;
  378. wc->pkey_index = 0;
  379. wc->slid = 0;
  380. wc->sl = 0;
  381. wc->dlid_path_bits = 0;
  382. wc->port_num = 0;
  383. ipath_cq_enter(to_icq(sqp->ibqp.send_cq), wc, 0);
  384. }
  385. /* Update s_last now that we are finished with the SWQE */
  386. spin_lock_irqsave(&sqp->s_lock, flags);
  387. if (++sqp->s_last >= sqp->s_size)
  388. sqp->s_last = 0;
  389. spin_unlock_irqrestore(&sqp->s_lock, flags);
  390. goto again;
  391. done:
  392. if (atomic_dec_and_test(&qp->refcount))
  393. wake_up(&qp->wait);
  394. }
  395. /**
  396. * ipath_no_bufs_available - tell the layer driver we need buffers
  397. * @qp: the QP that caused the problem
  398. * @dev: the device we ran out of buffers on
  399. *
  400. * Called when we run out of PIO buffers.
  401. */
  402. void ipath_no_bufs_available(struct ipath_qp *qp, struct ipath_ibdev *dev)
  403. {
  404. unsigned long flags;
  405. spin_lock_irqsave(&dev->pending_lock, flags);
  406. if (qp->piowait.next == LIST_POISON1)
  407. list_add_tail(&qp->piowait, &dev->piowait);
  408. spin_unlock_irqrestore(&dev->pending_lock, flags);
  409. /*
  410. * Note that as soon as ipath_layer_want_buffer() is called and
  411. * possibly before it returns, ipath_ib_piobufavail()
  412. * could be called. If we are still in the tasklet function,
  413. * tasklet_hi_schedule() will not call us until the next time
  414. * tasklet_hi_schedule() is called.
  415. * We clear the tasklet flag now since we are committing to return
  416. * from the tasklet function.
  417. */
  418. clear_bit(IPATH_S_BUSY, &qp->s_flags);
  419. tasklet_unlock(&qp->s_task);
  420. ipath_layer_want_buffer(dev->dd);
  421. dev->n_piowait++;
  422. }
  423. /**
  424. * ipath_post_rc_send - post RC and UC sends
  425. * @qp: the QP to post on
  426. * @wr: the work request to send
  427. */
  428. int ipath_post_rc_send(struct ipath_qp *qp, struct ib_send_wr *wr)
  429. {
  430. struct ipath_swqe *wqe;
  431. unsigned long flags;
  432. u32 next;
  433. int i, j;
  434. int acc;
  435. int ret;
  436. /*
  437. * Don't allow RDMA reads or atomic operations on UC or
  438. * undefined operations.
  439. * Make sure buffer is large enough to hold the result for atomics.
  440. */
  441. if (qp->ibqp.qp_type == IB_QPT_UC) {
  442. if ((unsigned) wr->opcode >= IB_WR_RDMA_READ) {
  443. ret = -EINVAL;
  444. goto bail;
  445. }
  446. } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
  447. ret = -EINVAL;
  448. goto bail;
  449. } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
  450. (wr->num_sge == 0 ||
  451. wr->sg_list[0].length < sizeof(u64) ||
  452. wr->sg_list[0].addr & (sizeof(u64) - 1))) {
  453. ret = -EINVAL;
  454. goto bail;
  455. }
  456. /* IB spec says that num_sge == 0 is OK. */
  457. if (wr->num_sge > qp->s_max_sge) {
  458. ret = -ENOMEM;
  459. goto bail;
  460. }
  461. spin_lock_irqsave(&qp->s_lock, flags);
  462. next = qp->s_head + 1;
  463. if (next >= qp->s_size)
  464. next = 0;
  465. if (next == qp->s_last) {
  466. spin_unlock_irqrestore(&qp->s_lock, flags);
  467. ret = -EINVAL;
  468. goto bail;
  469. }
  470. wqe = get_swqe_ptr(qp, qp->s_head);
  471. wqe->wr = *wr;
  472. wqe->ssn = qp->s_ssn++;
  473. wqe->sg_list[0].mr = NULL;
  474. wqe->sg_list[0].vaddr = NULL;
  475. wqe->sg_list[0].length = 0;
  476. wqe->sg_list[0].sge_length = 0;
  477. wqe->length = 0;
  478. acc = wr->opcode >= IB_WR_RDMA_READ ? IB_ACCESS_LOCAL_WRITE : 0;
  479. for (i = 0, j = 0; i < wr->num_sge; i++) {
  480. if (to_ipd(qp->ibqp.pd)->user && wr->sg_list[i].lkey == 0) {
  481. spin_unlock_irqrestore(&qp->s_lock, flags);
  482. ret = -EINVAL;
  483. goto bail;
  484. }
  485. if (wr->sg_list[i].length == 0)
  486. continue;
  487. if (!ipath_lkey_ok(&to_idev(qp->ibqp.device)->lk_table,
  488. &wqe->sg_list[j], &wr->sg_list[i],
  489. acc)) {
  490. spin_unlock_irqrestore(&qp->s_lock, flags);
  491. ret = -EINVAL;
  492. goto bail;
  493. }
  494. wqe->length += wr->sg_list[i].length;
  495. j++;
  496. }
  497. wqe->wr.num_sge = j;
  498. qp->s_head = next;
  499. /*
  500. * Wake up the send tasklet if the QP is not waiting
  501. * for an RNR timeout.
  502. */
  503. next = qp->s_rnr_timeout;
  504. spin_unlock_irqrestore(&qp->s_lock, flags);
  505. if (next == 0) {
  506. if (qp->ibqp.qp_type == IB_QPT_UC)
  507. ipath_do_uc_send((unsigned long) qp);
  508. else
  509. ipath_do_rc_send((unsigned long) qp);
  510. }
  511. ret = 0;
  512. bail:
  513. return ret;
  514. }