2 * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
3 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include "ipath_verbs.h"
35 #include "ipath_kernel.h"
38 * Convert the AETH RNR timeout code into the number of milliseconds.
40 const u32 ib_ipath_rnr_table[32] = {
76 * ipath_insert_rnr_queue - put QP on the RNR timeout list for the device
79 * XXX Use a simple list for now. We might need a priority
80 * queue if we have lots of QPs waiting for RNR timeouts
81 * but that should be rare.
83 void ipath_insert_rnr_queue(struct ipath_qp *qp)
85 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
88 spin_lock_irqsave(&dev->pending_lock, flags);
89 if (list_empty(&dev->rnrwait))
90 list_add(&qp->timerwait, &dev->rnrwait);
92 struct list_head *l = &dev->rnrwait;
93 struct ipath_qp *nqp = list_entry(l->next, struct ipath_qp,
96 while (qp->s_rnr_timeout >= nqp->s_rnr_timeout) {
97 qp->s_rnr_timeout -= nqp->s_rnr_timeout;
99 if (l->next == &dev->rnrwait)
101 nqp = list_entry(l->next, struct ipath_qp,
104 list_add(&qp->timerwait, l);
106 spin_unlock_irqrestore(&dev->pending_lock, flags);
109 static int init_sge(struct ipath_qp *qp, struct ipath_rwqe *wqe)
111 int user = to_ipd(qp->ibqp.pd)->user;
116 for (i = j = 0; i < wqe->num_sge; i++) {
117 if (wqe->sg_list[i].length == 0)
120 if ((user && wqe->sg_list[i].lkey == 0) ||
121 !ipath_lkey_ok(qp, &qp->r_sg_list[j], &wqe->sg_list[i],
122 IB_ACCESS_LOCAL_WRITE))
124 qp->r_len += wqe->sg_list[i].length;
127 qp->r_sge.sge = qp->r_sg_list[0];
128 qp->r_sge.sg_list = qp->r_sg_list + 1;
129 qp->r_sge.num_sge = j;
134 wc.wr_id = wqe->wr_id;
135 wc.status = IB_WC_LOC_PROT_ERR;
136 wc.opcode = IB_WC_RECV;
146 wc.dlid_path_bits = 0;
148 /* Signal solicited completion event. */
149 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
156 * ipath_get_rwqe - copy the next RWQE into the QP's RWQE
158 * @wr_id_only: update wr_id only, not SGEs
160 * Return 0 if no RWQE is available, otherwise return 1.
162 * Can be called from interrupt level.
164 int ipath_get_rwqe(struct ipath_qp *qp, int wr_id_only)
168 struct ipath_rwq *wq;
169 struct ipath_srq *srq;
170 struct ipath_rwqe *wqe;
171 void (*handler)(struct ib_event *, void *);
176 srq = to_isrq(qp->ibqp.srq);
177 handler = srq->ibsrq.event_handler;
185 spin_lock_irqsave(&rq->lock, flags);
188 /* Validate tail before using it since it is user writable. */
189 if (tail >= rq->size)
192 if (unlikely(tail == wq->head)) {
193 spin_unlock_irqrestore(&rq->lock, flags);
197 /* Make sure entry is read after head index is read. */
199 wqe = get_rwqe_ptr(rq, tail);
200 if (++tail >= rq->size)
202 } while (!wr_id_only && !init_sge(qp, wqe));
203 qp->r_wr_id = wqe->wr_id;
207 qp->r_wrid_valid = 1;
212 * validate head pointer value and compute
213 * the number of remaining WQEs.
219 n += rq->size - tail;
222 if (n < srq->limit) {
226 spin_unlock_irqrestore(&rq->lock, flags);
227 ev.device = qp->ibqp.device;
228 ev.element.srq = qp->ibqp.srq;
229 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
230 handler(&ev, srq->ibsrq.srq_context);
234 spin_unlock_irqrestore(&rq->lock, flags);
241 * ipath_ruc_loopback - handle UC and RC lookback requests
242 * @sqp: the loopback QP
244 * This is called from ipath_do_uc_send() or ipath_do_rc_send() to
245 * forward a WQE addressed to the same HCA.
246 * Note that although we are single threaded due to the tasklet, we still
247 * have to protect against post_send(). We don't have to worry about
248 * receive interrupts since this is a connected protocol and all packets
249 * will pass through here.
251 static void ipath_ruc_loopback(struct ipath_qp *sqp)
253 struct ipath_ibdev *dev = to_idev(sqp->ibqp.device);
255 struct ipath_swqe *wqe;
256 struct ipath_sge *sge;
262 qp = ipath_lookup_qpn(&dev->qp_table, sqp->remote_qpn);
269 spin_lock_irqsave(&sqp->s_lock, flags);
271 if (!(ib_ipath_state_ops[sqp->state] & IPATH_PROCESS_SEND_OK) ||
272 sqp->s_rnr_timeout) {
273 spin_unlock_irqrestore(&sqp->s_lock, flags);
277 /* Get the next send request. */
278 if (sqp->s_last == sqp->s_head) {
279 /* Send work queue is empty. */
280 spin_unlock_irqrestore(&sqp->s_lock, flags);
285 * We can rely on the entry not changing without the s_lock
286 * being held until we update s_last.
288 wqe = get_swqe_ptr(sqp, sqp->s_last);
289 spin_unlock_irqrestore(&sqp->s_lock, flags);
294 sqp->s_sge.sge = wqe->sg_list[0];
295 sqp->s_sge.sg_list = wqe->sg_list + 1;
296 sqp->s_sge.num_sge = wqe->wr.num_sge;
297 sqp->s_len = wqe->length;
298 switch (wqe->wr.opcode) {
299 case IB_WR_SEND_WITH_IMM:
300 wc.wc_flags = IB_WC_WITH_IMM;
301 wc.imm_data = wqe->wr.imm_data;
304 if (!ipath_get_rwqe(qp, 0)) {
307 if (qp->ibqp.qp_type == IB_QPT_UC)
309 if (sqp->s_rnr_retry == 0) {
310 wc.status = IB_WC_RNR_RETRY_EXC_ERR;
313 if (sqp->s_rnr_retry_cnt < 7)
317 ib_ipath_rnr_table[qp->r_min_rnr_timer];
318 ipath_insert_rnr_queue(sqp);
323 case IB_WR_RDMA_WRITE_WITH_IMM:
324 if (unlikely(!(qp->qp_access_flags &
325 IB_ACCESS_REMOTE_WRITE))) {
326 wc.status = IB_WC_REM_INV_REQ_ERR;
329 wc.wc_flags = IB_WC_WITH_IMM;
330 wc.imm_data = wqe->wr.imm_data;
331 if (!ipath_get_rwqe(qp, 1))
334 case IB_WR_RDMA_WRITE:
335 if (unlikely(!(qp->qp_access_flags &
336 IB_ACCESS_REMOTE_WRITE))) {
337 wc.status = IB_WC_REM_INV_REQ_ERR;
340 if (wqe->length == 0)
342 if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge, wqe->length,
343 wqe->wr.wr.rdma.remote_addr,
344 wqe->wr.wr.rdma.rkey,
345 IB_ACCESS_REMOTE_WRITE))) {
347 wc.status = IB_WC_REM_ACCESS_ERR;
349 wc.wr_id = wqe->wr.wr_id;
350 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
354 wc.src_qp = sqp->remote_qpn;
356 wc.slid = sqp->remote_ah_attr.dlid;
357 wc.sl = sqp->remote_ah_attr.sl;
358 wc.dlid_path_bits = 0;
360 spin_lock_irqsave(&sqp->s_lock, flags);
361 ipath_sqerror_qp(sqp, &wc);
362 spin_unlock_irqrestore(&sqp->s_lock, flags);
367 case IB_WR_RDMA_READ:
368 if (unlikely(!(qp->qp_access_flags &
369 IB_ACCESS_REMOTE_READ))) {
370 wc.status = IB_WC_REM_INV_REQ_ERR;
373 if (unlikely(!ipath_rkey_ok(qp, &sqp->s_sge, wqe->length,
374 wqe->wr.wr.rdma.remote_addr,
375 wqe->wr.wr.rdma.rkey,
376 IB_ACCESS_REMOTE_READ)))
378 qp->r_sge.sge = wqe->sg_list[0];
379 qp->r_sge.sg_list = wqe->sg_list + 1;
380 qp->r_sge.num_sge = wqe->wr.num_sge;
383 case IB_WR_ATOMIC_CMP_AND_SWP:
384 case IB_WR_ATOMIC_FETCH_AND_ADD:
385 if (unlikely(!(qp->qp_access_flags &
386 IB_ACCESS_REMOTE_ATOMIC))) {
387 wc.status = IB_WC_REM_INV_REQ_ERR;
390 if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge, sizeof(u64),
391 wqe->wr.wr.atomic.remote_addr,
392 wqe->wr.wr.atomic.rkey,
393 IB_ACCESS_REMOTE_ATOMIC)))
395 /* Perform atomic OP and save result. */
396 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
397 sdata = wqe->wr.wr.atomic.compare_add;
398 *(u64 *) sqp->s_sge.sge.vaddr =
399 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
400 (u64) atomic64_add_return(sdata, maddr) - sdata :
401 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
402 sdata, wqe->wr.wr.atomic.swap);
409 sge = &sqp->s_sge.sge;
411 u32 len = sqp->s_len;
413 if (len > sge->length)
415 if (len > sge->sge_length)
416 len = sge->sge_length;
418 ipath_copy_sge(&qp->r_sge, sge->vaddr, len);
421 sge->sge_length -= len;
422 if (sge->sge_length == 0) {
423 if (--sqp->s_sge.num_sge)
424 *sge = *sqp->s_sge.sg_list++;
425 } else if (sge->length == 0 && sge->mr != NULL) {
426 if (++sge->n >= IPATH_SEGSZ) {
427 if (++sge->m >= sge->mr->mapsz)
432 sge->mr->map[sge->m]->segs[sge->n].vaddr;
434 sge->mr->map[sge->m]->segs[sge->n].length;
439 if (wqe->wr.opcode == IB_WR_RDMA_WRITE ||
440 wqe->wr.opcode == IB_WR_RDMA_READ)
443 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
444 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
446 wc.opcode = IB_WC_RECV;
447 wc.wr_id = qp->r_wr_id;
448 wc.status = IB_WC_SUCCESS;
450 wc.byte_len = wqe->length;
452 wc.src_qp = qp->remote_qpn;
453 /* XXX do we know which pkey matched? Only needed for GSI. */
455 wc.slid = qp->remote_ah_attr.dlid;
456 wc.sl = qp->remote_ah_attr.sl;
457 wc.dlid_path_bits = 0;
458 /* Signal completion event if the solicited bit is set. */
459 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
460 wqe->wr.send_flags & IB_SEND_SOLICITED);
463 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
465 if (!(sqp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
466 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
467 wc.wr_id = wqe->wr.wr_id;
468 wc.status = IB_WC_SUCCESS;
469 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
471 wc.byte_len = wqe->length;
477 wc.dlid_path_bits = 0;
479 ipath_cq_enter(to_icq(sqp->ibqp.send_cq), &wc, 0);
482 /* Update s_last now that we are finished with the SWQE */
483 spin_lock_irqsave(&sqp->s_lock, flags);
484 if (++sqp->s_last >= sqp->s_size)
486 spin_unlock_irqrestore(&sqp->s_lock, flags);
490 if (atomic_dec_and_test(&qp->refcount))
494 static int want_buffer(struct ipath_devdata *dd)
496 set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl);
497 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
504 * ipath_no_bufs_available - tell the layer driver we need buffers
505 * @qp: the QP that caused the problem
506 * @dev: the device we ran out of buffers on
508 * Called when we run out of PIO buffers.
510 static void ipath_no_bufs_available(struct ipath_qp *qp, struct ipath_ibdev *dev)
514 spin_lock_irqsave(&dev->pending_lock, flags);
515 if (list_empty(&qp->piowait))
516 list_add_tail(&qp->piowait, &dev->piowait);
517 spin_unlock_irqrestore(&dev->pending_lock, flags);
519 * Note that as soon as want_buffer() is called and
520 * possibly before it returns, ipath_ib_piobufavail()
521 * could be called. If we are still in the tasklet function,
522 * tasklet_hi_schedule() will not call us until the next time
523 * tasklet_hi_schedule() is called.
524 * We leave the busy flag set so that another post send doesn't
525 * try to put the same QP on the piowait list again.
527 want_buffer(dev->dd);
532 * ipath_post_ruc_send - post RC and UC sends
533 * @qp: the QP to post on
534 * @wr: the work request to send
536 int ipath_post_ruc_send(struct ipath_qp *qp, struct ib_send_wr *wr)
538 struct ipath_swqe *wqe;
546 * Don't allow RDMA reads or atomic operations on UC or
547 * undefined operations.
548 * Make sure buffer is large enough to hold the result for atomics.
550 if (qp->ibqp.qp_type == IB_QPT_UC) {
551 if ((unsigned) wr->opcode >= IB_WR_RDMA_READ) {
555 } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
558 } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
560 wr->sg_list[0].length < sizeof(u64) ||
561 wr->sg_list[0].addr & (sizeof(u64) - 1))) {
564 } else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic) {
568 /* IB spec says that num_sge == 0 is OK. */
569 if (wr->num_sge > qp->s_max_sge) {
573 spin_lock_irqsave(&qp->s_lock, flags);
574 next = qp->s_head + 1;
575 if (next >= qp->s_size)
577 if (next == qp->s_last) {
578 spin_unlock_irqrestore(&qp->s_lock, flags);
583 wqe = get_swqe_ptr(qp, qp->s_head);
585 wqe->ssn = qp->s_ssn++;
586 wqe->sg_list[0].mr = NULL;
587 wqe->sg_list[0].vaddr = NULL;
588 wqe->sg_list[0].length = 0;
589 wqe->sg_list[0].sge_length = 0;
591 acc = wr->opcode >= IB_WR_RDMA_READ ? IB_ACCESS_LOCAL_WRITE : 0;
592 for (i = 0, j = 0; i < wr->num_sge; i++) {
593 if (to_ipd(qp->ibqp.pd)->user && wr->sg_list[i].lkey == 0) {
594 spin_unlock_irqrestore(&qp->s_lock, flags);
598 if (wr->sg_list[i].length == 0)
600 if (!ipath_lkey_ok(qp, &wqe->sg_list[j], &wr->sg_list[i],
602 spin_unlock_irqrestore(&qp->s_lock, flags);
606 wqe->length += wr->sg_list[i].length;
611 spin_unlock_irqrestore(&qp->s_lock, flags);
613 ipath_do_ruc_send((unsigned long) qp);
622 * ipath_make_grh - construct a GRH header
623 * @dev: a pointer to the ipath device
624 * @hdr: a pointer to the GRH header being constructed
625 * @grh: the global route address to send to
626 * @hwords: the number of 32 bit words of header being sent
627 * @nwords: the number of 32 bit words of data being sent
629 * Return the size of the header in 32 bit words.
631 u32 ipath_make_grh(struct ipath_ibdev *dev, struct ib_grh *hdr,
632 struct ib_global_route *grh, u32 hwords, u32 nwords)
634 hdr->version_tclass_flow =
635 cpu_to_be32((6 << 28) |
636 (grh->traffic_class << 20) |
638 hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
639 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
640 hdr->next_hdr = 0x1B;
641 hdr->hop_limit = grh->hop_limit;
642 /* The SGID is 32-bit aligned. */
643 hdr->sgid.global.subnet_prefix = dev->gid_prefix;
644 hdr->sgid.global.interface_id = dev->dd->ipath_guid;
645 hdr->dgid = grh->dgid;
647 /* GRH header size in 32-bit words. */
648 return sizeof(struct ib_grh) / sizeof(u32);
652 * ipath_do_ruc_send - perform a send on an RC or UC QP
653 * @data: contains a pointer to the QP
655 * Process entries in the send work queue until credit or queue is
656 * exhausted. Only allow one CPU to send a packet per QP (tasklet).
657 * Otherwise, after we drop the QP s_lock, two threads could send
658 * packets out of order.
660 void ipath_do_ruc_send(unsigned long data)
662 struct ipath_qp *qp = (struct ipath_qp *)data;
663 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
670 u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
671 struct ipath_other_headers *ohdr;
673 if (test_and_set_bit(IPATH_S_BUSY, &qp->s_busy))
676 if (unlikely(qp->remote_ah_attr.dlid == dev->dd->ipath_lid)) {
677 ipath_ruc_loopback(qp);
681 ohdr = &qp->s_hdr.u.oth;
682 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
683 ohdr = &qp->s_hdr.u.l.oth;
686 /* Check for a constructed packet to be sent. */
687 if (qp->s_hdrwords != 0) {
689 * If no PIO bufs are available, return. An interrupt will
690 * call ipath_ib_piobufavail() when one is available.
692 if (ipath_verbs_send(dev->dd, qp->s_hdrwords,
693 (u32 *) &qp->s_hdr, qp->s_cur_size,
695 ipath_no_bufs_available(qp, dev);
698 dev->n_unicast_xmit++;
699 /* Record that we sent the packet and s_hdr is empty. */
704 * The lock is needed to synchronize between setting
705 * qp->s_ack_state, resend timer, and post_send().
707 spin_lock_irqsave(&qp->s_lock, flags);
709 if (!((qp->ibqp.qp_type == IB_QPT_RC) ?
710 ipath_make_rc_req(qp, ohdr, pmtu, &bth0, &bth2) :
711 ipath_make_uc_req(qp, ohdr, pmtu, &bth0, &bth2))) {
713 * Clear the busy bit before unlocking to avoid races with
714 * adding new work queue items and then failing to process
717 clear_bit(IPATH_S_BUSY, &qp->s_busy);
718 spin_unlock_irqrestore(&qp->s_lock, flags);
722 spin_unlock_irqrestore(&qp->s_lock, flags);
724 /* Construct the header. */
725 extra_bytes = (4 - qp->s_cur_size) & 3;
726 nwords = (qp->s_cur_size + extra_bytes) >> 2;
727 lrh0 = IPATH_LRH_BTH;
728 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
729 qp->s_hdrwords += ipath_make_grh(dev, &qp->s_hdr.u.l.grh,
730 &qp->remote_ah_attr.grh,
731 qp->s_hdrwords, nwords);
732 lrh0 = IPATH_LRH_GRH;
734 lrh0 |= qp->remote_ah_attr.sl << 4;
735 qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
736 qp->s_hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
737 qp->s_hdr.lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords +
739 qp->s_hdr.lrh[3] = cpu_to_be16(dev->dd->ipath_lid);
740 bth0 |= ipath_get_pkey(dev->dd, qp->s_pkey_index);
741 bth0 |= extra_bytes << 20;
742 ohdr->bth[0] = cpu_to_be32(bth0);
743 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
744 ohdr->bth[2] = cpu_to_be32(bth2);
746 /* Check for more work to do. */
750 clear_bit(IPATH_S_BUSY, &qp->s_busy);