3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2015 Intel Corporation.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
21 * Copyright(c) 2015 Intel Corporation.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * - Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * - Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in
31 * the documentation and/or other materials provided with the
33 * - Neither the name of Intel Corporation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 #include <linux/spinlock.h>
59 * Convert the AETH RNR timeout code into the number of microseconds.
61 const u32 ib_hfi1_rnr_table[32] = {
62 655360, /* 00: 655.36 */
82 10240, /* 14: 10.24 */
83 15360, /* 15: 15.36 */
84 20480, /* 16: 20.48 */
85 30720, /* 17: 30.72 */
86 40960, /* 18: 40.96 */
87 61440, /* 19: 61.44 */
88 81920, /* 1A: 81.92 */
89 122880, /* 1B: 122.88 */
90 163840, /* 1C: 163.84 */
91 245760, /* 1D: 245.76 */
92 327680, /* 1E: 327.68 */
93 491520 /* 1F: 491.52 */
97 * Validate a RWQE and fill in the SGE state.
100 static int init_sge(struct hfi1_qp *qp, struct hfi1_rwqe *wqe)
104 struct hfi1_lkey_table *rkt;
106 struct hfi1_sge_state *ss;
108 rkt = &to_idev(qp->ibqp.device)->lk_table;
109 pd = to_ipd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
111 ss->sg_list = qp->r_sg_list;
113 for (i = j = 0; i < wqe->num_sge; i++) {
114 if (wqe->sg_list[i].length == 0)
117 if (!hfi1_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
118 &wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE))
120 qp->r_len += wqe->sg_list[i].length;
124 ss->total_len = qp->r_len;
130 struct hfi1_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
132 hfi1_put_mr(sge->mr);
135 memset(&wc, 0, sizeof(wc));
136 wc.wr_id = wqe->wr_id;
137 wc.status = IB_WC_LOC_PROT_ERR;
138 wc.opcode = IB_WC_RECV;
140 /* Signal solicited completion event. */
141 hfi1_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
148 * hfi1_get_rwqe - copy the next RWQE into the QP's RWQE
150 * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
152 * Return -1 if there is a local error, 0 if no RWQE is available,
153 * otherwise return 1.
155 * Can be called from interrupt level.
157 int hfi1_get_rwqe(struct hfi1_qp *qp, int wr_id_only)
162 struct hfi1_srq *srq;
163 struct hfi1_rwqe *wqe;
164 void (*handler)(struct ib_event *, void *);
169 srq = to_isrq(qp->ibqp.srq);
170 handler = srq->ibsrq.event_handler;
178 spin_lock_irqsave(&rq->lock, flags);
179 if (!(ib_hfi1_state_ops[qp->state] & HFI1_PROCESS_RECV_OK)) {
186 /* Validate tail before using it since it is user writable. */
187 if (tail >= rq->size)
189 if (unlikely(tail == wq->head)) {
193 /* Make sure entry is read after head index is read. */
195 wqe = get_rwqe_ptr(rq, tail);
197 * Even though we update the tail index in memory, the verbs
198 * consumer is not supposed to post more entries until a
199 * completion is generated.
201 if (++tail >= rq->size)
204 if (!wr_id_only && !init_sge(qp, wqe)) {
208 qp->r_wr_id = wqe->wr_id;
211 set_bit(HFI1_R_WRID_VALID, &qp->r_aflags);
216 * Validate head pointer value and compute
217 * the number of remaining WQEs.
223 n += rq->size - tail;
226 if (n < srq->limit) {
230 spin_unlock_irqrestore(&rq->lock, flags);
231 ev.device = qp->ibqp.device;
232 ev.element.srq = qp->ibqp.srq;
233 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
234 handler(&ev, srq->ibsrq.srq_context);
239 spin_unlock_irqrestore(&rq->lock, flags);
244 static __be64 get_sguid(struct hfi1_ibport *ibp, unsigned index)
247 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
249 return cpu_to_be64(ppd->guid);
251 return ibp->guids[index - 1];
254 static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
256 return (gid->global.interface_id == id &&
257 (gid->global.subnet_prefix == gid_prefix ||
258 gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
263 * This should be called with the QP r_lock held.
265 * The s_lock will be acquired around the hfi1_migrate_qp() call.
267 int hfi1_ruc_check_hdr(struct hfi1_ibport *ibp, struct hfi1_ib_header *hdr,
268 int has_grh, struct hfi1_qp *qp, u32 bth0)
272 u8 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
274 if (qp->s_mig_state == IB_MIG_ARMED && (bth0 & IB_BTH_MIG_REQ)) {
276 if (qp->alt_ah_attr.ah_flags & IB_AH_GRH)
279 if (!(qp->alt_ah_attr.ah_flags & IB_AH_GRH))
281 guid = get_sguid(ibp, qp->alt_ah_attr.grh.sgid_index);
282 if (!gid_ok(&hdr->u.l.grh.dgid, ibp->gid_prefix, guid))
284 if (!gid_ok(&hdr->u.l.grh.sgid,
285 qp->alt_ah_attr.grh.dgid.global.subnet_prefix,
286 qp->alt_ah_attr.grh.dgid.global.interface_id))
289 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0,
290 sc5, be16_to_cpu(hdr->lrh[3])))) {
291 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
293 (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
295 be16_to_cpu(hdr->lrh[3]),
296 be16_to_cpu(hdr->lrh[1]));
299 /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
300 if (be16_to_cpu(hdr->lrh[3]) != qp->alt_ah_attr.dlid ||
301 ppd_from_ibp(ibp)->port != qp->alt_ah_attr.port_num)
303 spin_lock_irqsave(&qp->s_lock, flags);
305 spin_unlock_irqrestore(&qp->s_lock, flags);
308 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
311 if (!(qp->remote_ah_attr.ah_flags & IB_AH_GRH))
313 guid = get_sguid(ibp,
314 qp->remote_ah_attr.grh.sgid_index);
315 if (!gid_ok(&hdr->u.l.grh.dgid, ibp->gid_prefix, guid))
317 if (!gid_ok(&hdr->u.l.grh.sgid,
318 qp->remote_ah_attr.grh.dgid.global.subnet_prefix,
319 qp->remote_ah_attr.grh.dgid.global.interface_id))
322 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0,
323 sc5, be16_to_cpu(hdr->lrh[3])))) {
324 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
326 (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
328 be16_to_cpu(hdr->lrh[3]),
329 be16_to_cpu(hdr->lrh[1]));
332 /* Validate the SLID. See Ch. 9.6.1.5 */
333 if (be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid ||
334 ppd_from_ibp(ibp)->port != qp->port_num)
336 if (qp->s_mig_state == IB_MIG_REARM &&
337 !(bth0 & IB_BTH_MIG_REQ))
338 qp->s_mig_state = IB_MIG_ARMED;
348 * ruc_loopback - handle UC and RC loopback requests
349 * @sqp: the sending QP
351 * This is called from hfi1_do_send() to
352 * forward a WQE addressed to the same HFI.
353 * Note that although we are single threaded due to the tasklet, we still
354 * have to protect against post_send(). We don't have to worry about
355 * receive interrupts since this is a connected protocol and all packets
356 * will pass through here.
358 static void ruc_loopback(struct hfi1_qp *sqp)
360 struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
362 struct hfi1_swqe *wqe;
363 struct hfi1_sge *sge;
368 enum ib_wc_status send_status;
375 * Note that we check the responder QP state after
376 * checking the requester's state.
378 qp = hfi1_lookup_qpn(ibp, sqp->remote_qpn);
380 spin_lock_irqsave(&sqp->s_lock, flags);
382 /* Return if we are already busy processing a work request. */
383 if ((sqp->s_flags & (HFI1_S_BUSY | HFI1_S_ANY_WAIT)) ||
384 !(ib_hfi1_state_ops[sqp->state] & HFI1_PROCESS_OR_FLUSH_SEND))
387 sqp->s_flags |= HFI1_S_BUSY;
390 if (sqp->s_last == sqp->s_head)
392 wqe = get_swqe_ptr(sqp, sqp->s_last);
394 /* Return if it is not OK to start a new work request. */
395 if (!(ib_hfi1_state_ops[sqp->state] & HFI1_PROCESS_NEXT_SEND_OK)) {
396 if (!(ib_hfi1_state_ops[sqp->state] & HFI1_FLUSH_SEND))
398 /* We are in the error state, flush the work request. */
399 send_status = IB_WC_WR_FLUSH_ERR;
404 * We can rely on the entry not changing without the s_lock
405 * being held until we update s_last.
406 * We increment s_cur to indicate s_last is in progress.
408 if (sqp->s_last == sqp->s_cur) {
409 if (++sqp->s_cur >= sqp->s_size)
412 spin_unlock_irqrestore(&sqp->s_lock, flags);
414 if (!qp || !(ib_hfi1_state_ops[qp->state] & HFI1_PROCESS_RECV_OK) ||
415 qp->ibqp.qp_type != sqp->ibqp.qp_type) {
418 * For RC, the requester would timeout and retry so
419 * shortcut the timeouts and just signal too many retries.
421 if (sqp->ibqp.qp_type == IB_QPT_RC)
422 send_status = IB_WC_RETRY_EXC_ERR;
424 send_status = IB_WC_SUCCESS;
428 memset(&wc, 0, sizeof(wc));
429 send_status = IB_WC_SUCCESS;
432 sqp->s_sge.sge = wqe->sg_list[0];
433 sqp->s_sge.sg_list = wqe->sg_list + 1;
434 sqp->s_sge.num_sge = wqe->wr.num_sge;
435 sqp->s_len = wqe->length;
436 switch (wqe->wr.opcode) {
437 case IB_WR_SEND_WITH_IMM:
438 wc.wc_flags = IB_WC_WITH_IMM;
439 wc.ex.imm_data = wqe->wr.ex.imm_data;
442 ret = hfi1_get_rwqe(qp, 0);
449 case IB_WR_RDMA_WRITE_WITH_IMM:
450 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
452 wc.wc_flags = IB_WC_WITH_IMM;
453 wc.ex.imm_data = wqe->wr.ex.imm_data;
454 ret = hfi1_get_rwqe(qp, 1);
460 case IB_WR_RDMA_WRITE:
461 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
463 if (wqe->length == 0)
465 if (unlikely(!hfi1_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
466 wqe->rdma_wr.remote_addr,
468 IB_ACCESS_REMOTE_WRITE)))
470 qp->r_sge.sg_list = NULL;
471 qp->r_sge.num_sge = 1;
472 qp->r_sge.total_len = wqe->length;
475 case IB_WR_RDMA_READ:
476 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
478 if (unlikely(!hfi1_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
479 wqe->rdma_wr.remote_addr,
481 IB_ACCESS_REMOTE_READ)))
484 sqp->s_sge.sg_list = NULL;
485 sqp->s_sge.num_sge = 1;
486 qp->r_sge.sge = wqe->sg_list[0];
487 qp->r_sge.sg_list = wqe->sg_list + 1;
488 qp->r_sge.num_sge = wqe->wr.num_sge;
489 qp->r_sge.total_len = wqe->length;
492 case IB_WR_ATOMIC_CMP_AND_SWP:
493 case IB_WR_ATOMIC_FETCH_AND_ADD:
494 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
496 if (unlikely(!hfi1_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
497 wqe->atomic_wr.remote_addr,
499 IB_ACCESS_REMOTE_ATOMIC)))
501 /* Perform atomic OP and save result. */
502 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
503 sdata = wqe->atomic_wr.compare_add;
504 *(u64 *) sqp->s_sge.sge.vaddr =
505 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
506 (u64) atomic64_add_return(sdata, maddr) - sdata :
507 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
508 sdata, wqe->atomic_wr.swap);
509 hfi1_put_mr(qp->r_sge.sge.mr);
510 qp->r_sge.num_sge = 0;
514 send_status = IB_WC_LOC_QP_OP_ERR;
518 sge = &sqp->s_sge.sge;
520 u32 len = sqp->s_len;
522 if (len > sge->length)
524 if (len > sge->sge_length)
525 len = sge->sge_length;
526 WARN_ON_ONCE(len == 0);
527 hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, release);
530 sge->sge_length -= len;
531 if (sge->sge_length == 0) {
533 hfi1_put_mr(sge->mr);
534 if (--sqp->s_sge.num_sge)
535 *sge = *sqp->s_sge.sg_list++;
536 } else if (sge->length == 0 && sge->mr->lkey) {
537 if (++sge->n >= HFI1_SEGSZ) {
538 if (++sge->m >= sge->mr->mapsz)
543 sge->mr->map[sge->m]->segs[sge->n].vaddr;
545 sge->mr->map[sge->m]->segs[sge->n].length;
550 hfi1_put_ss(&qp->r_sge);
552 if (!test_and_clear_bit(HFI1_R_WRID_VALID, &qp->r_aflags))
555 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
556 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
558 wc.opcode = IB_WC_RECV;
559 wc.wr_id = qp->r_wr_id;
560 wc.status = IB_WC_SUCCESS;
561 wc.byte_len = wqe->length;
563 wc.src_qp = qp->remote_qpn;
564 wc.slid = qp->remote_ah_attr.dlid;
565 wc.sl = qp->remote_ah_attr.sl;
567 /* Signal completion event if the solicited bit is set. */
568 hfi1_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
569 wqe->wr.send_flags & IB_SEND_SOLICITED);
572 spin_lock_irqsave(&sqp->s_lock, flags);
575 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
576 hfi1_send_complete(sqp, wqe, send_status);
581 if (qp->ibqp.qp_type == IB_QPT_UC)
585 * Note: we don't need the s_lock held since the BUSY flag
586 * makes this single threaded.
588 if (sqp->s_rnr_retry == 0) {
589 send_status = IB_WC_RNR_RETRY_EXC_ERR;
592 if (sqp->s_rnr_retry_cnt < 7)
594 spin_lock_irqsave(&sqp->s_lock, flags);
595 if (!(ib_hfi1_state_ops[sqp->state] & HFI1_PROCESS_RECV_OK))
597 sqp->s_flags |= HFI1_S_WAIT_RNR;
598 sqp->s_timer.function = hfi1_rc_rnr_retry;
599 sqp->s_timer.expires = jiffies +
600 usecs_to_jiffies(ib_hfi1_rnr_table[qp->r_min_rnr_timer]);
601 add_timer(&sqp->s_timer);
605 send_status = IB_WC_REM_OP_ERR;
606 wc.status = IB_WC_LOC_QP_OP_ERR;
610 send_status = IB_WC_REM_INV_REQ_ERR;
611 wc.status = IB_WC_LOC_QP_OP_ERR;
615 send_status = IB_WC_REM_ACCESS_ERR;
616 wc.status = IB_WC_LOC_PROT_ERR;
618 /* responder goes to error state */
619 hfi1_rc_error(qp, wc.status);
622 spin_lock_irqsave(&sqp->s_lock, flags);
623 hfi1_send_complete(sqp, wqe, send_status);
624 if (sqp->ibqp.qp_type == IB_QPT_RC) {
625 int lastwqe = hfi1_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
627 sqp->s_flags &= ~HFI1_S_BUSY;
628 spin_unlock_irqrestore(&sqp->s_lock, flags);
632 ev.device = sqp->ibqp.device;
633 ev.element.qp = &sqp->ibqp;
634 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
635 sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
640 sqp->s_flags &= ~HFI1_S_BUSY;
642 spin_unlock_irqrestore(&sqp->s_lock, flags);
648 * hfi1_make_grh - construct a GRH header
649 * @ibp: a pointer to the IB port
650 * @hdr: a pointer to the GRH header being constructed
651 * @grh: the global route address to send to
652 * @hwords: the number of 32 bit words of header being sent
653 * @nwords: the number of 32 bit words of data being sent
655 * Return the size of the header in 32 bit words.
657 u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
658 struct ib_global_route *grh, u32 hwords, u32 nwords)
660 hdr->version_tclass_flow =
661 cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
662 (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
663 (grh->flow_label << IB_GRH_FLOW_SHIFT));
664 hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
665 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
666 hdr->next_hdr = IB_GRH_NEXT_HDR;
667 hdr->hop_limit = grh->hop_limit;
668 /* The SGID is 32-bit aligned. */
669 hdr->sgid.global.subnet_prefix = ibp->gid_prefix;
670 hdr->sgid.global.interface_id =
671 grh->sgid_index && grh->sgid_index < ARRAY_SIZE(ibp->guids) ?
672 ibp->guids[grh->sgid_index - 1] :
673 cpu_to_be64(ppd_from_ibp(ibp)->guid);
674 hdr->dgid = grh->dgid;
676 /* GRH header size in 32-bit words. */
677 return sizeof(struct ib_grh) / sizeof(u32);
680 #define BTH2_OFFSET (offsetof(struct hfi1_pio_header, hdr.u.oth.bth[2]) / 4)
683 * build_ahg - create ahg in s_hdr
684 * @qp: a pointer to QP
685 * @npsn: the next PSN for the request/response
687 * This routine handles the AHG by allocating an ahg entry and causing the
688 * copy of the first middle.
690 * Subsequent middles use the copied entry, editing the
691 * PSN with 1 or 2 edits.
693 static inline void build_ahg(struct hfi1_qp *qp, u32 npsn)
695 if (unlikely(qp->s_flags & HFI1_S_AHG_CLEAR))
697 if (!(qp->s_flags & HFI1_S_AHG_VALID)) {
698 /* first middle that needs copy */
699 if (qp->s_ahgidx < 0)
700 qp->s_ahgidx = sdma_ahg_alloc(qp->s_sde);
701 if (qp->s_ahgidx >= 0) {
703 qp->s_hdr->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
704 /* save to protect a change in another thread */
705 qp->s_hdr->sde = qp->s_sde;
706 qp->s_hdr->ahgidx = qp->s_ahgidx;
707 qp->s_flags |= HFI1_S_AHG_VALID;
710 /* subsequent middle after valid */
711 if (qp->s_ahgidx >= 0) {
712 qp->s_hdr->tx_flags |= SDMA_TXREQ_F_USE_AHG;
713 qp->s_hdr->ahgidx = qp->s_ahgidx;
714 qp->s_hdr->ahgcount++;
715 qp->s_hdr->ahgdesc[0] =
716 sdma_build_ahg_descriptor(
717 (__force u16)cpu_to_be16((u16)npsn),
721 if ((npsn & 0xffff0000) !=
722 (qp->s_ahgpsn & 0xffff0000)) {
723 qp->s_hdr->ahgcount++;
724 qp->s_hdr->ahgdesc[1] =
725 sdma_build_ahg_descriptor(
726 (__force u16)cpu_to_be16(
736 void hfi1_make_ruc_header(struct hfi1_qp *qp, struct hfi1_other_headers *ohdr,
737 u32 bth0, u32 bth2, int middle)
739 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
745 /* Construct the header. */
746 extra_bytes = -qp->s_cur_size & 3;
747 nwords = (qp->s_cur_size + extra_bytes) >> 2;
749 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
750 qp->s_hdrwords += hfi1_make_grh(ibp, &qp->s_hdr->ibh.u.l.grh,
751 &qp->remote_ah_attr.grh,
752 qp->s_hdrwords, nwords);
756 lrh0 |= (qp->s_sc & 0xf) << 12 | (qp->remote_ah_attr.sl & 0xf) << 4;
758 * reset s_hdr/AHG fields
760 * This insures that the ahgentry/ahgcount
761 * are at a non-AHG default to protect
762 * build_verbs_tx_desc() from using
765 * build_ahg() will modify as appropriate
766 * to use the AHG feature.
768 qp->s_hdr->tx_flags = 0;
769 qp->s_hdr->ahgcount = 0;
770 qp->s_hdr->ahgidx = 0;
771 qp->s_hdr->sde = NULL;
772 if (qp->s_mig_state == IB_MIG_MIGRATED)
773 bth0 |= IB_BTH_MIG_REQ;
779 qp->s_flags &= ~HFI1_S_AHG_VALID;
780 qp->s_hdr->ibh.lrh[0] = cpu_to_be16(lrh0);
781 qp->s_hdr->ibh.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
782 qp->s_hdr->ibh.lrh[2] =
783 cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
784 qp->s_hdr->ibh.lrh[3] = cpu_to_be16(ppd_from_ibp(ibp)->lid |
785 qp->remote_ah_attr.src_path_bits);
786 bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
787 bth0 |= extra_bytes << 20;
788 ohdr->bth[0] = cpu_to_be32(bth0);
789 bth1 = qp->remote_qpn;
790 if (qp->s_flags & HFI1_S_ECN) {
791 qp->s_flags &= ~HFI1_S_ECN;
792 /* we recently received a FECN, so return a BECN */
793 bth1 |= (HFI1_BECN_MASK << HFI1_BECN_SHIFT);
795 ohdr->bth[1] = cpu_to_be32(bth1);
796 ohdr->bth[2] = cpu_to_be32(bth2);
799 /* when sending, force a reschedule every one of these periods */
800 #define SEND_RESCHED_TIMEOUT (5 * HZ) /* 5s in jiffies */
803 * hfi1_do_send - perform a send on a QP
804 * @work: contains a pointer to the QP
806 * Process entries in the send work queue until credit or queue is
807 * exhausted. Only allow one CPU to send a packet per QP (tasklet).
808 * Otherwise, two threads could send packets out of order.
810 void hfi1_do_send(struct work_struct *work)
812 struct iowait *wait = container_of(work, struct iowait, iowork);
813 struct hfi1_qp *qp = container_of(wait, struct hfi1_qp, s_iowait);
814 struct hfi1_pkt_state ps;
815 int (*make_req)(struct hfi1_qp *qp);
817 unsigned long timeout;
819 ps.dev = to_idev(qp->ibqp.device);
820 ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
821 ps.ppd = ppd_from_ibp(ps.ibp);
823 if ((qp->ibqp.qp_type == IB_QPT_RC ||
824 qp->ibqp.qp_type == IB_QPT_UC) &&
826 (qp->remote_ah_attr.dlid & ~((1 << ps.ppd->lmc) - 1)) ==
832 if (qp->ibqp.qp_type == IB_QPT_RC)
833 make_req = hfi1_make_rc_req;
834 else if (qp->ibqp.qp_type == IB_QPT_UC)
835 make_req = hfi1_make_uc_req;
837 make_req = hfi1_make_ud_req;
839 spin_lock_irqsave(&qp->s_lock, flags);
841 /* Return if we are already busy processing a work request. */
842 if (!hfi1_send_ok(qp)) {
843 spin_unlock_irqrestore(&qp->s_lock, flags);
847 qp->s_flags |= HFI1_S_BUSY;
849 spin_unlock_irqrestore(&qp->s_lock, flags);
851 timeout = jiffies + SEND_RESCHED_TIMEOUT;
853 /* Check for a constructed packet to be sent. */
854 if (qp->s_hdrwords != 0) {
856 * If the packet cannot be sent now, return and
857 * the send tasklet will be woken up later.
859 if (hfi1_verbs_send(qp, &ps))
861 /* Record that s_hdr is empty. */
865 /* allow other tasks to run */
866 if (unlikely(time_after(jiffies, timeout))) {
868 ps.ppd->dd->verbs_dev.n_send_schedule++;
869 timeout = jiffies + SEND_RESCHED_TIMEOUT;
871 } while (make_req(qp));
875 * This should be called with s_lock held.
877 void hfi1_send_complete(struct hfi1_qp *qp, struct hfi1_swqe *wqe,
878 enum ib_wc_status status)
883 if (!(ib_hfi1_state_ops[qp->state] & HFI1_PROCESS_OR_FLUSH_SEND))
886 for (i = 0; i < wqe->wr.num_sge; i++) {
887 struct hfi1_sge *sge = &wqe->sg_list[i];
889 hfi1_put_mr(sge->mr);
891 if (qp->ibqp.qp_type == IB_QPT_UD ||
892 qp->ibqp.qp_type == IB_QPT_SMI ||
893 qp->ibqp.qp_type == IB_QPT_GSI)
894 atomic_dec(&to_iah(wqe->ud_wr.ah)->refcount);
896 /* See ch. 11.2.4.1 and 10.7.3.1 */
897 if (!(qp->s_flags & HFI1_S_SIGNAL_REQ_WR) ||
898 (wqe->wr.send_flags & IB_SEND_SIGNALED) ||
899 status != IB_WC_SUCCESS) {
902 memset(&wc, 0, sizeof(wc));
903 wc.wr_id = wqe->wr.wr_id;
905 wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode];
907 if (status == IB_WC_SUCCESS)
908 wc.byte_len = wqe->length;
909 hfi1_cq_enter(to_icq(qp->ibqp.send_cq), &wc,
910 status != IB_WC_SUCCESS);
915 if (++last >= qp->s_size)
918 if (qp->s_acked == old_last)
920 if (qp->s_cur == old_last)
922 if (qp->s_tail == old_last)
924 if (qp->state == IB_QPS_SQD && last == qp->s_cur)