2 * Copyright (c) 2006 QLogic, Inc. 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_common.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);
110 * ipath_get_rwqe - copy the next RWQE into the QP's RWQE
112 * @wr_id_only: update wr_id only, not SGEs
114 * Return 0 if no RWQE is available, otherwise return 1.
116 * Can be called from interrupt level.
118 int ipath_get_rwqe(struct ipath_qp *qp, int wr_id_only)
122 struct ipath_srq *srq;
123 struct ipath_rwqe *wqe;
128 spin_lock_irqsave(&rq->lock, flags);
130 if (unlikely(rq->tail == rq->head)) {
134 wqe = get_rwqe_ptr(rq, rq->tail);
135 qp->r_wr_id = wqe->wr_id;
137 qp->r_sge.sge = wqe->sg_list[0];
138 qp->r_sge.sg_list = wqe->sg_list + 1;
139 qp->r_sge.num_sge = wqe->num_sge;
140 qp->r_len = wqe->length;
142 if (++rq->tail >= rq->size)
147 srq = to_isrq(qp->ibqp.srq);
149 spin_lock_irqsave(&rq->lock, flags);
151 if (unlikely(rq->tail == rq->head)) {
155 wqe = get_rwqe_ptr(rq, rq->tail);
156 qp->r_wr_id = wqe->wr_id;
158 qp->r_sge.sge = wqe->sg_list[0];
159 qp->r_sge.sg_list = wqe->sg_list + 1;
160 qp->r_sge.num_sge = wqe->num_sge;
161 qp->r_len = wqe->length;
163 if (++rq->tail >= rq->size)
165 if (srq->ibsrq.event_handler) {
169 if (rq->head < rq->tail)
170 n = rq->size + rq->head - rq->tail;
172 n = rq->head - rq->tail;
173 if (n < srq->limit) {
175 spin_unlock_irqrestore(&rq->lock, flags);
176 ev.device = qp->ibqp.device;
177 ev.element.srq = qp->ibqp.srq;
178 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
179 srq->ibsrq.event_handler(&ev,
180 srq->ibsrq.srq_context);
186 spin_unlock_irqrestore(&rq->lock, flags);
192 * ipath_ruc_loopback - handle UC and RC lookback requests
193 * @sqp: the loopback QP
195 * This is called from ipath_do_uc_send() or ipath_do_rc_send() to
196 * forward a WQE addressed to the same HCA.
197 * Note that although we are single threaded due to the tasklet, we still
198 * have to protect against post_send(). We don't have to worry about
199 * receive interrupts since this is a connected protocol and all packets
200 * will pass through here.
202 static void ipath_ruc_loopback(struct ipath_qp *sqp)
204 struct ipath_ibdev *dev = to_idev(sqp->ibqp.device);
206 struct ipath_swqe *wqe;
207 struct ipath_sge *sge;
212 qp = ipath_lookup_qpn(&dev->qp_table, sqp->remote_qpn);
219 spin_lock_irqsave(&sqp->s_lock, flags);
221 if (!(ib_ipath_state_ops[sqp->state] & IPATH_PROCESS_SEND_OK)) {
222 spin_unlock_irqrestore(&sqp->s_lock, flags);
226 /* Get the next send request. */
227 if (sqp->s_last == sqp->s_head) {
228 /* Send work queue is empty. */
229 spin_unlock_irqrestore(&sqp->s_lock, flags);
234 * We can rely on the entry not changing without the s_lock
235 * being held until we update s_last.
237 wqe = get_swqe_ptr(sqp, sqp->s_last);
238 spin_unlock_irqrestore(&sqp->s_lock, flags);
243 sqp->s_sge.sge = wqe->sg_list[0];
244 sqp->s_sge.sg_list = wqe->sg_list + 1;
245 sqp->s_sge.num_sge = wqe->wr.num_sge;
246 sqp->s_len = wqe->length;
247 switch (wqe->wr.opcode) {
248 case IB_WR_SEND_WITH_IMM:
249 wc.wc_flags = IB_WC_WITH_IMM;
250 wc.imm_data = wqe->wr.imm_data;
253 if (!ipath_get_rwqe(qp, 0)) {
256 if (qp->ibqp.qp_type == IB_QPT_UC)
258 if (sqp->s_rnr_retry == 0) {
259 wc.status = IB_WC_RNR_RETRY_EXC_ERR;
262 if (sqp->s_rnr_retry_cnt < 7)
266 ib_ipath_rnr_table[sqp->r_min_rnr_timer];
267 ipath_insert_rnr_queue(sqp);
272 case IB_WR_RDMA_WRITE_WITH_IMM:
273 wc.wc_flags = IB_WC_WITH_IMM;
274 wc.imm_data = wqe->wr.imm_data;
275 if (!ipath_get_rwqe(qp, 1))
278 case IB_WR_RDMA_WRITE:
279 if (wqe->length == 0)
281 if (unlikely(!ipath_rkey_ok(dev, &qp->r_sge, wqe->length,
282 wqe->wr.wr.rdma.remote_addr,
283 wqe->wr.wr.rdma.rkey,
284 IB_ACCESS_REMOTE_WRITE))) {
286 wc.status = IB_WC_REM_ACCESS_ERR;
288 wc.wr_id = wqe->wr.wr_id;
289 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
292 wc.qp_num = sqp->ibqp.qp_num;
293 wc.src_qp = sqp->remote_qpn;
295 wc.slid = sqp->remote_ah_attr.dlid;
296 wc.sl = sqp->remote_ah_attr.sl;
297 wc.dlid_path_bits = 0;
299 ipath_sqerror_qp(sqp, &wc);
304 case IB_WR_RDMA_READ:
305 if (unlikely(!ipath_rkey_ok(dev, &sqp->s_sge, wqe->length,
306 wqe->wr.wr.rdma.remote_addr,
307 wqe->wr.wr.rdma.rkey,
308 IB_ACCESS_REMOTE_READ)))
310 if (unlikely(!(qp->qp_access_flags &
311 IB_ACCESS_REMOTE_READ)))
313 qp->r_sge.sge = wqe->sg_list[0];
314 qp->r_sge.sg_list = wqe->sg_list + 1;
315 qp->r_sge.num_sge = wqe->wr.num_sge;
318 case IB_WR_ATOMIC_CMP_AND_SWP:
319 case IB_WR_ATOMIC_FETCH_AND_ADD:
320 if (unlikely(!ipath_rkey_ok(dev, &qp->r_sge, sizeof(u64),
321 wqe->wr.wr.rdma.remote_addr,
322 wqe->wr.wr.rdma.rkey,
323 IB_ACCESS_REMOTE_ATOMIC)))
325 /* Perform atomic OP and save result. */
326 sdata = wqe->wr.wr.atomic.swap;
327 spin_lock_irqsave(&dev->pending_lock, flags);
328 qp->r_atomic_data = *(u64 *) qp->r_sge.sge.vaddr;
329 if (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
330 *(u64 *) qp->r_sge.sge.vaddr =
331 qp->r_atomic_data + sdata;
332 else if (qp->r_atomic_data == wqe->wr.wr.atomic.compare_add)
333 *(u64 *) qp->r_sge.sge.vaddr = sdata;
334 spin_unlock_irqrestore(&dev->pending_lock, flags);
335 *(u64 *) sqp->s_sge.sge.vaddr = qp->r_atomic_data;
342 sge = &sqp->s_sge.sge;
344 u32 len = sqp->s_len;
346 if (len > sge->length)
349 ipath_copy_sge(&qp->r_sge, sge->vaddr, len);
352 sge->sge_length -= len;
353 if (sge->sge_length == 0) {
354 if (--sqp->s_sge.num_sge)
355 *sge = *sqp->s_sge.sg_list++;
356 } else if (sge->length == 0 && sge->mr != NULL) {
357 if (++sge->n >= IPATH_SEGSZ) {
358 if (++sge->m >= sge->mr->mapsz)
363 sge->mr->map[sge->m]->segs[sge->n].vaddr;
365 sge->mr->map[sge->m]->segs[sge->n].length;
370 if (wqe->wr.opcode == IB_WR_RDMA_WRITE ||
371 wqe->wr.opcode == IB_WR_RDMA_READ)
374 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
375 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
377 wc.opcode = IB_WC_RECV;
378 wc.wr_id = qp->r_wr_id;
379 wc.status = IB_WC_SUCCESS;
381 wc.byte_len = wqe->length;
382 wc.qp_num = qp->ibqp.qp_num;
383 wc.src_qp = qp->remote_qpn;
384 /* XXX do we know which pkey matched? Only needed for GSI. */
386 wc.slid = qp->remote_ah_attr.dlid;
387 wc.sl = qp->remote_ah_attr.sl;
388 wc.dlid_path_bits = 0;
389 /* Signal completion event if the solicited bit is set. */
390 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
391 wqe->wr.send_flags & IB_SEND_SOLICITED);
394 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
396 if (!test_bit(IPATH_S_SIGNAL_REQ_WR, &sqp->s_flags) ||
397 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
398 wc.wr_id = wqe->wr.wr_id;
399 wc.status = IB_WC_SUCCESS;
400 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
402 wc.byte_len = wqe->length;
403 wc.qp_num = sqp->ibqp.qp_num;
408 wc.dlid_path_bits = 0;
410 ipath_cq_enter(to_icq(sqp->ibqp.send_cq), &wc, 0);
413 /* Update s_last now that we are finished with the SWQE */
414 spin_lock_irqsave(&sqp->s_lock, flags);
415 if (++sqp->s_last >= sqp->s_size)
417 spin_unlock_irqrestore(&sqp->s_lock, flags);
421 if (atomic_dec_and_test(&qp->refcount))
426 * ipath_no_bufs_available - tell the layer driver we need buffers
427 * @qp: the QP that caused the problem
428 * @dev: the device we ran out of buffers on
430 * Called when we run out of PIO buffers.
432 void ipath_no_bufs_available(struct ipath_qp *qp, struct ipath_ibdev *dev)
436 spin_lock_irqsave(&dev->pending_lock, flags);
437 if (list_empty(&qp->piowait))
438 list_add_tail(&qp->piowait, &dev->piowait);
439 spin_unlock_irqrestore(&dev->pending_lock, flags);
441 * Note that as soon as ipath_layer_want_buffer() is called and
442 * possibly before it returns, ipath_ib_piobufavail()
443 * could be called. If we are still in the tasklet function,
444 * tasklet_hi_schedule() will not call us until the next time
445 * tasklet_hi_schedule() is called.
446 * We clear the tasklet flag now since we are committing to return
447 * from the tasklet function.
449 clear_bit(IPATH_S_BUSY, &qp->s_flags);
450 tasklet_unlock(&qp->s_task);
451 ipath_layer_want_buffer(dev->dd);
456 * ipath_post_ruc_send - post RC and UC sends
457 * @qp: the QP to post on
458 * @wr: the work request to send
460 int ipath_post_ruc_send(struct ipath_qp *qp, struct ib_send_wr *wr)
462 struct ipath_swqe *wqe;
470 * Don't allow RDMA reads or atomic operations on UC or
471 * undefined operations.
472 * Make sure buffer is large enough to hold the result for atomics.
474 if (qp->ibqp.qp_type == IB_QPT_UC) {
475 if ((unsigned) wr->opcode >= IB_WR_RDMA_READ) {
479 } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
482 } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
484 wr->sg_list[0].length < sizeof(u64) ||
485 wr->sg_list[0].addr & (sizeof(u64) - 1))) {
489 /* IB spec says that num_sge == 0 is OK. */
490 if (wr->num_sge > qp->s_max_sge) {
494 spin_lock_irqsave(&qp->s_lock, flags);
495 next = qp->s_head + 1;
496 if (next >= qp->s_size)
498 if (next == qp->s_last) {
499 spin_unlock_irqrestore(&qp->s_lock, flags);
504 wqe = get_swqe_ptr(qp, qp->s_head);
506 wqe->ssn = qp->s_ssn++;
507 wqe->sg_list[0].mr = NULL;
508 wqe->sg_list[0].vaddr = NULL;
509 wqe->sg_list[0].length = 0;
510 wqe->sg_list[0].sge_length = 0;
512 acc = wr->opcode >= IB_WR_RDMA_READ ? IB_ACCESS_LOCAL_WRITE : 0;
513 for (i = 0, j = 0; i < wr->num_sge; i++) {
514 if (to_ipd(qp->ibqp.pd)->user && wr->sg_list[i].lkey == 0) {
515 spin_unlock_irqrestore(&qp->s_lock, flags);
519 if (wr->sg_list[i].length == 0)
521 if (!ipath_lkey_ok(&to_idev(qp->ibqp.device)->lk_table,
522 &wqe->sg_list[j], &wr->sg_list[i],
524 spin_unlock_irqrestore(&qp->s_lock, flags);
528 wqe->length += wr->sg_list[i].length;
533 spin_unlock_irqrestore(&qp->s_lock, flags);
535 ipath_do_ruc_send((unsigned long) qp);
544 * ipath_make_grh - construct a GRH header
545 * @dev: a pointer to the ipath device
546 * @hdr: a pointer to the GRH header being constructed
547 * @grh: the global route address to send to
548 * @hwords: the number of 32 bit words of header being sent
549 * @nwords: the number of 32 bit words of data being sent
551 * Return the size of the header in 32 bit words.
553 u32 ipath_make_grh(struct ipath_ibdev *dev, struct ib_grh *hdr,
554 struct ib_global_route *grh, u32 hwords, u32 nwords)
556 hdr->version_tclass_flow =
557 cpu_to_be32((6 << 28) |
558 (grh->traffic_class << 20) |
560 hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
561 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
562 hdr->next_hdr = 0x1B;
563 hdr->hop_limit = grh->hop_limit;
564 /* The SGID is 32-bit aligned. */
565 hdr->sgid.global.subnet_prefix = dev->gid_prefix;
566 hdr->sgid.global.interface_id = ipath_layer_get_guid(dev->dd);
567 hdr->dgid = grh->dgid;
569 /* GRH header size in 32-bit words. */
570 return sizeof(struct ib_grh) / sizeof(u32);
574 * ipath_do_ruc_send - perform a send on an RC or UC QP
575 * @data: contains a pointer to the QP
577 * Process entries in the send work queue until credit or queue is
578 * exhausted. Only allow one CPU to send a packet per QP (tasklet).
579 * Otherwise, after we drop the QP s_lock, two threads could send
580 * packets out of order.
582 void ipath_do_ruc_send(unsigned long data)
584 struct ipath_qp *qp = (struct ipath_qp *)data;
585 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
592 u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
593 struct ipath_other_headers *ohdr;
595 if (test_and_set_bit(IPATH_S_BUSY, &qp->s_flags))
598 if (unlikely(qp->remote_ah_attr.dlid ==
599 ipath_layer_get_lid(dev->dd))) {
600 ipath_ruc_loopback(qp);
604 ohdr = &qp->s_hdr.u.oth;
605 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
606 ohdr = &qp->s_hdr.u.l.oth;
609 /* Check for a constructed packet to be sent. */
610 if (qp->s_hdrwords != 0) {
612 * If no PIO bufs are available, return. An interrupt will
613 * call ipath_ib_piobufavail() when one is available.
615 if (ipath_verbs_send(dev->dd, qp->s_hdrwords,
616 (u32 *) &qp->s_hdr, qp->s_cur_size,
618 ipath_no_bufs_available(qp, dev);
621 dev->n_unicast_xmit++;
622 /* Record that we sent the packet and s_hdr is empty. */
627 * The lock is needed to synchronize between setting
628 * qp->s_ack_state, resend timer, and post_send().
630 spin_lock_irqsave(&qp->s_lock, flags);
632 /* Sending responses has higher priority over sending requests. */
633 if (qp->s_ack_state != IB_OPCODE_RC_ACKNOWLEDGE &&
634 (bth0 = ipath_make_rc_ack(qp, ohdr, pmtu)) != 0)
635 bth2 = qp->s_ack_psn++ & IPATH_PSN_MASK;
636 else if (!((qp->ibqp.qp_type == IB_QPT_RC) ?
637 ipath_make_rc_req(qp, ohdr, pmtu, &bth0, &bth2) :
638 ipath_make_uc_req(qp, ohdr, pmtu, &bth0, &bth2))) {
640 * Clear the busy bit before unlocking to avoid races with
641 * adding new work queue items and then failing to process
644 clear_bit(IPATH_S_BUSY, &qp->s_flags);
645 spin_unlock_irqrestore(&qp->s_lock, flags);
649 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;
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 +
666 qp->s_hdr.lrh[3] = cpu_to_be16(ipath_layer_get_lid(dev->dd));
667 bth0 |= ipath_layer_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);
673 /* Check for more work to do. */
677 clear_bit(IPATH_S_BUSY, &qp->s_flags);