ipath_ruc.c 14 KB

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