2 * Copyright(c) 2015 - 2017 Intel Corporation.
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 #include <linux/spinlock.h>
53 #include "verbs_txreq.h"
57 * Convert the AETH RNR timeout code into the number of microseconds.
59 const u32 ib_hfi1_rnr_table[32] = {
60 655360, /* 00: 655.36 */
80 10240, /* 14: 10.24 */
81 15360, /* 15: 15.36 */
82 20480, /* 16: 20.48 */
83 30720, /* 17: 30.72 */
84 40960, /* 18: 40.96 */
85 61440, /* 19: 61.44 */
86 81920, /* 1A: 81.92 */
87 122880, /* 1B: 122.88 */
88 163840, /* 1C: 163.84 */
89 245760, /* 1D: 245.76 */
90 327680, /* 1E: 327.68 */
91 491520 /* 1F: 491.52 */
95 * Validate a RWQE and fill in the SGE state.
98 static int init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
102 struct rvt_lkey_table *rkt;
104 struct rvt_sge_state *ss;
106 rkt = &to_idev(qp->ibqp.device)->rdi.lkey_table;
107 pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
109 ss->sg_list = qp->r_sg_list;
111 for (i = j = 0; i < wqe->num_sge; i++) {
112 if (wqe->sg_list[i].length == 0)
115 if (!rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
116 &wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE))
118 qp->r_len += wqe->sg_list[i].length;
122 ss->total_len = qp->r_len;
128 struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
133 memset(&wc, 0, sizeof(wc));
134 wc.wr_id = wqe->wr_id;
135 wc.status = IB_WC_LOC_PROT_ERR;
136 wc.opcode = IB_WC_RECV;
138 /* Signal solicited completion event. */
139 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
146 * hfi1_rvt_get_rwqe - copy the next RWQE into the QP's RWQE
148 * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
150 * Return -1 if there is a local error, 0 if no RWQE is available,
151 * otherwise return 1.
153 * Can be called from interrupt level.
155 int hfi1_rvt_get_rwqe(struct rvt_qp *qp, int wr_id_only)
161 struct rvt_rwqe *wqe;
162 void (*handler)(struct ib_event *, void *);
167 srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
168 handler = srq->ibsrq.event_handler;
176 spin_lock_irqsave(&rq->lock, flags);
177 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
184 /* Validate tail before using it since it is user writable. */
185 if (tail >= rq->size)
187 if (unlikely(tail == wq->head)) {
191 /* Make sure entry is read after head index is read. */
193 wqe = rvt_get_rwqe_ptr(rq, tail);
195 * Even though we update the tail index in memory, the verbs
196 * consumer is not supposed to post more entries until a
197 * completion is generated.
199 if (++tail >= rq->size)
202 if (!wr_id_only && !init_sge(qp, wqe)) {
206 qp->r_wr_id = wqe->wr_id;
209 set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
214 * Validate head pointer value and compute
215 * the number of remaining WQEs.
221 n += rq->size - tail;
224 if (n < srq->limit) {
228 spin_unlock_irqrestore(&rq->lock, flags);
229 ev.device = qp->ibqp.device;
230 ev.element.srq = qp->ibqp.srq;
231 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
232 handler(&ev, srq->ibsrq.srq_context);
237 spin_unlock_irqrestore(&rq->lock, flags);
242 static __be64 get_sguid(struct hfi1_ibport *ibp, unsigned index)
245 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
247 return cpu_to_be64(ppd->guid);
249 return ibp->guids[index - 1];
252 static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
254 return (gid->global.interface_id == id &&
255 (gid->global.subnet_prefix == gid_prefix ||
256 gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
261 * This should be called with the QP r_lock held.
263 * The s_lock will be acquired around the hfi1_migrate_qp() call.
265 int hfi1_ruc_check_hdr(struct hfi1_ibport *ibp, struct ib_header *hdr,
266 int has_grh, struct rvt_qp *qp, u32 bth0)
270 u8 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
272 if (qp->s_mig_state == IB_MIG_ARMED && (bth0 & IB_BTH_MIG_REQ)) {
274 if (qp->alt_ah_attr.ah_flags & IB_AH_GRH)
277 if (!(qp->alt_ah_attr.ah_flags & IB_AH_GRH))
279 guid = get_sguid(ibp, qp->alt_ah_attr.grh.sgid_index);
280 if (!gid_ok(&hdr->u.l.grh.dgid, ibp->rvp.gid_prefix,
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->rvp.gid_prefix,
320 qp->remote_ah_attr.grh.dgid.global.subnet_prefix,
321 qp->remote_ah_attr.grh.dgid.global.interface_id))
324 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0,
325 sc5, be16_to_cpu(hdr->lrh[3])))) {
326 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
328 (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
330 be16_to_cpu(hdr->lrh[3]),
331 be16_to_cpu(hdr->lrh[1]));
334 /* Validate the SLID. See Ch. 9.6.1.5 */
335 if (be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid ||
336 ppd_from_ibp(ibp)->port != qp->port_num)
338 if (qp->s_mig_state == IB_MIG_REARM &&
339 !(bth0 & IB_BTH_MIG_REQ))
340 qp->s_mig_state = IB_MIG_ARMED;
350 * ruc_loopback - handle UC and RC loopback requests
351 * @sqp: the sending QP
353 * This is called from hfi1_do_send() to
354 * forward a WQE addressed to the same HFI.
355 * Note that although we are single threaded due to the send engine, we still
356 * have to protect against post_send(). We don't have to worry about
357 * receive interrupts since this is a connected protocol and all packets
358 * will pass through here.
360 static void ruc_loopback(struct rvt_qp *sqp)
362 struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
364 struct rvt_swqe *wqe;
370 enum ib_wc_status send_status;
380 * Note that we check the responder QP state after
381 * checking the requester's state.
383 qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
386 spin_lock_irqsave(&sqp->s_lock, flags);
388 /* Return if we are already busy processing a work request. */
389 if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
390 !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
393 sqp->s_flags |= RVT_S_BUSY;
396 smp_read_barrier_depends(); /* see post_one_send() */
397 if (sqp->s_last == ACCESS_ONCE(sqp->s_head))
399 wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
401 /* Return if it is not OK to start a new work request. */
402 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
403 if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
405 /* We are in the error state, flush the work request. */
406 send_status = IB_WC_WR_FLUSH_ERR;
411 * We can rely on the entry not changing without the s_lock
412 * being held until we update s_last.
413 * We increment s_cur to indicate s_last is in progress.
415 if (sqp->s_last == sqp->s_cur) {
416 if (++sqp->s_cur >= sqp->s_size)
419 spin_unlock_irqrestore(&sqp->s_lock, flags);
421 if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
422 qp->ibqp.qp_type != sqp->ibqp.qp_type) {
423 ibp->rvp.n_pkt_drops++;
425 * For RC, the requester would timeout and retry so
426 * shortcut the timeouts and just signal too many retries.
428 if (sqp->ibqp.qp_type == IB_QPT_RC)
429 send_status = IB_WC_RETRY_EXC_ERR;
431 send_status = IB_WC_SUCCESS;
435 memset(&wc, 0, sizeof(wc));
436 send_status = IB_WC_SUCCESS;
439 sqp->s_sge.sge = wqe->sg_list[0];
440 sqp->s_sge.sg_list = wqe->sg_list + 1;
441 sqp->s_sge.num_sge = wqe->wr.num_sge;
442 sqp->s_len = wqe->length;
443 switch (wqe->wr.opcode) {
447 case IB_WR_LOCAL_INV:
448 if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
449 if (rvt_invalidate_rkey(sqp,
450 wqe->wr.ex.invalidate_rkey))
451 send_status = IB_WC_LOC_PROT_ERR;
456 case IB_WR_SEND_WITH_INV:
457 if (!rvt_invalidate_rkey(qp, wqe->wr.ex.invalidate_rkey)) {
458 wc.wc_flags = IB_WC_WITH_INVALIDATE;
459 wc.ex.invalidate_rkey = wqe->wr.ex.invalidate_rkey;
463 case IB_WR_SEND_WITH_IMM:
464 wc.wc_flags = IB_WC_WITH_IMM;
465 wc.ex.imm_data = wqe->wr.ex.imm_data;
469 ret = hfi1_rvt_get_rwqe(qp, 0);
474 if (wqe->length > qp->r_len)
478 case IB_WR_RDMA_WRITE_WITH_IMM:
479 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
481 wc.wc_flags = IB_WC_WITH_IMM;
482 wc.ex.imm_data = wqe->wr.ex.imm_data;
483 ret = hfi1_rvt_get_rwqe(qp, 1);
488 /* skip copy_last set and qp_access_flags recheck */
490 case IB_WR_RDMA_WRITE:
491 copy_last = ibpd_to_rvtpd(qp->ibqp.pd)->user;
492 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
495 if (wqe->length == 0)
497 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
498 wqe->rdma_wr.remote_addr,
500 IB_ACCESS_REMOTE_WRITE)))
502 qp->r_sge.sg_list = NULL;
503 qp->r_sge.num_sge = 1;
504 qp->r_sge.total_len = wqe->length;
507 case IB_WR_RDMA_READ:
508 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
510 if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
511 wqe->rdma_wr.remote_addr,
513 IB_ACCESS_REMOTE_READ)))
516 sqp->s_sge.sg_list = NULL;
517 sqp->s_sge.num_sge = 1;
518 qp->r_sge.sge = wqe->sg_list[0];
519 qp->r_sge.sg_list = wqe->sg_list + 1;
520 qp->r_sge.num_sge = wqe->wr.num_sge;
521 qp->r_sge.total_len = wqe->length;
524 case IB_WR_ATOMIC_CMP_AND_SWP:
525 case IB_WR_ATOMIC_FETCH_AND_ADD:
526 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
528 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
529 wqe->atomic_wr.remote_addr,
531 IB_ACCESS_REMOTE_ATOMIC)))
533 /* Perform atomic OP and save result. */
534 maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
535 sdata = wqe->atomic_wr.compare_add;
536 *(u64 *)sqp->s_sge.sge.vaddr =
537 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
538 (u64)atomic64_add_return(sdata, maddr) - sdata :
539 (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
540 sdata, wqe->atomic_wr.swap);
541 rvt_put_mr(qp->r_sge.sge.mr);
542 qp->r_sge.num_sge = 0;
546 send_status = IB_WC_LOC_QP_OP_ERR;
550 sge = &sqp->s_sge.sge;
552 u32 len = sqp->s_len;
554 if (len > sge->length)
556 if (len > sge->sge_length)
557 len = sge->sge_length;
558 WARN_ON_ONCE(len == 0);
559 hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, release, copy_last);
562 sge->sge_length -= len;
563 if (sge->sge_length == 0) {
566 if (--sqp->s_sge.num_sge)
567 *sge = *sqp->s_sge.sg_list++;
568 } else if (sge->length == 0 && sge->mr->lkey) {
569 if (++sge->n >= RVT_SEGSZ) {
570 if (++sge->m >= sge->mr->mapsz)
575 sge->mr->map[sge->m]->segs[sge->n].vaddr;
577 sge->mr->map[sge->m]->segs[sge->n].length;
582 rvt_put_ss(&qp->r_sge);
584 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
587 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
588 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
590 wc.opcode = IB_WC_RECV;
591 wc.wr_id = qp->r_wr_id;
592 wc.status = IB_WC_SUCCESS;
593 wc.byte_len = wqe->length;
595 wc.src_qp = qp->remote_qpn;
596 wc.slid = qp->remote_ah_attr.dlid;
597 wc.sl = qp->remote_ah_attr.sl;
599 /* Signal completion event if the solicited bit is set. */
600 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
601 wqe->wr.send_flags & IB_SEND_SOLICITED);
604 spin_lock_irqsave(&sqp->s_lock, flags);
605 ibp->rvp.n_loop_pkts++;
607 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
608 hfi1_send_complete(sqp, wqe, send_status);
610 atomic_dec(&sqp->local_ops_pending);
617 if (qp->ibqp.qp_type == IB_QPT_UC)
619 ibp->rvp.n_rnr_naks++;
621 * Note: we don't need the s_lock held since the BUSY flag
622 * makes this single threaded.
624 if (sqp->s_rnr_retry == 0) {
625 send_status = IB_WC_RNR_RETRY_EXC_ERR;
628 if (sqp->s_rnr_retry_cnt < 7)
630 spin_lock_irqsave(&sqp->s_lock, flags);
631 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
633 to = ib_hfi1_rnr_table[qp->r_min_rnr_timer];
634 hfi1_add_rnr_timer(sqp, to);
638 send_status = IB_WC_REM_OP_ERR;
639 wc.status = IB_WC_LOC_QP_OP_ERR;
644 sqp->ibqp.qp_type == IB_QPT_RC ?
645 IB_WC_REM_INV_REQ_ERR :
647 wc.status = IB_WC_LOC_QP_OP_ERR;
651 send_status = IB_WC_REM_ACCESS_ERR;
652 wc.status = IB_WC_LOC_PROT_ERR;
654 /* responder goes to error state */
655 hfi1_rc_error(qp, wc.status);
658 spin_lock_irqsave(&sqp->s_lock, flags);
659 hfi1_send_complete(sqp, wqe, send_status);
660 if (sqp->ibqp.qp_type == IB_QPT_RC) {
661 int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
663 sqp->s_flags &= ~RVT_S_BUSY;
664 spin_unlock_irqrestore(&sqp->s_lock, flags);
668 ev.device = sqp->ibqp.device;
669 ev.element.qp = &sqp->ibqp;
670 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
671 sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
676 sqp->s_flags &= ~RVT_S_BUSY;
678 spin_unlock_irqrestore(&sqp->s_lock, flags);
684 * hfi1_make_grh - construct a GRH header
685 * @ibp: a pointer to the IB port
686 * @hdr: a pointer to the GRH header being constructed
687 * @grh: the global route address to send to
688 * @hwords: the number of 32 bit words of header being sent
689 * @nwords: the number of 32 bit words of data being sent
691 * Return the size of the header in 32 bit words.
693 u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
694 struct ib_global_route *grh, u32 hwords, u32 nwords)
696 hdr->version_tclass_flow =
697 cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
698 (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
699 (grh->flow_label << IB_GRH_FLOW_SHIFT));
700 hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
701 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
702 hdr->next_hdr = IB_GRH_NEXT_HDR;
703 hdr->hop_limit = grh->hop_limit;
704 /* The SGID is 32-bit aligned. */
705 hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
706 hdr->sgid.global.interface_id =
707 grh->sgid_index && grh->sgid_index < ARRAY_SIZE(ibp->guids) ?
708 ibp->guids[grh->sgid_index - 1] :
709 cpu_to_be64(ppd_from_ibp(ibp)->guid);
710 hdr->dgid = grh->dgid;
712 /* GRH header size in 32-bit words. */
713 return sizeof(struct ib_grh) / sizeof(u32);
716 #define BTH2_OFFSET (offsetof(struct hfi1_sdma_header, hdr.u.oth.bth[2]) / 4)
719 * build_ahg - create ahg in s_ahg
720 * @qp: a pointer to QP
721 * @npsn: the next PSN for the request/response
723 * This routine handles the AHG by allocating an ahg entry and causing the
724 * copy of the first middle.
726 * Subsequent middles use the copied entry, editing the
727 * PSN with 1 or 2 edits.
729 static inline void build_ahg(struct rvt_qp *qp, u32 npsn)
731 struct hfi1_qp_priv *priv = qp->priv;
733 if (unlikely(qp->s_flags & RVT_S_AHG_CLEAR))
735 if (!(qp->s_flags & RVT_S_AHG_VALID)) {
736 /* first middle that needs copy */
737 if (qp->s_ahgidx < 0)
738 qp->s_ahgidx = sdma_ahg_alloc(priv->s_sde);
739 if (qp->s_ahgidx >= 0) {
741 priv->s_ahg->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
742 /* save to protect a change in another thread */
743 priv->s_ahg->ahgidx = qp->s_ahgidx;
744 qp->s_flags |= RVT_S_AHG_VALID;
747 /* subsequent middle after valid */
748 if (qp->s_ahgidx >= 0) {
749 priv->s_ahg->tx_flags |= SDMA_TXREQ_F_USE_AHG;
750 priv->s_ahg->ahgidx = qp->s_ahgidx;
751 priv->s_ahg->ahgcount++;
752 priv->s_ahg->ahgdesc[0] =
753 sdma_build_ahg_descriptor(
754 (__force u16)cpu_to_be16((u16)npsn),
758 if ((npsn & 0xffff0000) !=
759 (qp->s_ahgpsn & 0xffff0000)) {
760 priv->s_ahg->ahgcount++;
761 priv->s_ahg->ahgdesc[1] =
762 sdma_build_ahg_descriptor(
763 (__force u16)cpu_to_be16(
773 void hfi1_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
774 u32 bth0, u32 bth2, int middle,
775 struct hfi1_pkt_state *ps)
777 struct hfi1_qp_priv *priv = qp->priv;
778 struct hfi1_ibport *ibp = ps->ibp;
784 /* Construct the header. */
785 extra_bytes = -qp->s_cur_size & 3;
786 nwords = (qp->s_cur_size + extra_bytes) >> 2;
788 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
789 qp->s_hdrwords += hfi1_make_grh(ibp,
790 &ps->s_txreq->phdr.hdr.u.l.grh,
791 &qp->remote_ah_attr.grh,
792 qp->s_hdrwords, nwords);
796 lrh0 |= (priv->s_sc & 0xf) << 12 | (qp->remote_ah_attr.sl & 0xf) << 4;
798 * reset s_ahg/AHG fields
800 * This insures that the ahgentry/ahgcount
801 * are at a non-AHG default to protect
802 * build_verbs_tx_desc() from using
805 * build_ahg() will modify as appropriate
806 * to use the AHG feature.
808 priv->s_ahg->tx_flags = 0;
809 priv->s_ahg->ahgcount = 0;
810 priv->s_ahg->ahgidx = 0;
811 if (qp->s_mig_state == IB_MIG_MIGRATED)
812 bth0 |= IB_BTH_MIG_REQ;
818 qp->s_flags &= ~RVT_S_AHG_VALID;
819 ps->s_txreq->phdr.hdr.lrh[0] = cpu_to_be16(lrh0);
820 ps->s_txreq->phdr.hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
821 ps->s_txreq->phdr.hdr.lrh[2] =
822 cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
823 ps->s_txreq->phdr.hdr.lrh[3] = cpu_to_be16(ppd_from_ibp(ibp)->lid |
824 qp->remote_ah_attr.src_path_bits);
825 bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
826 bth0 |= extra_bytes << 20;
827 ohdr->bth[0] = cpu_to_be32(bth0);
828 bth1 = qp->remote_qpn;
829 if (qp->s_flags & RVT_S_ECN) {
830 qp->s_flags &= ~RVT_S_ECN;
831 /* we recently received a FECN, so return a BECN */
832 bth1 |= (HFI1_BECN_MASK << HFI1_BECN_SHIFT);
834 ohdr->bth[1] = cpu_to_be32(bth1);
835 ohdr->bth[2] = cpu_to_be32(bth2);
838 /* when sending, force a reschedule every one of these periods */
839 #define SEND_RESCHED_TIMEOUT (5 * HZ) /* 5s in jiffies */
841 void hfi1_do_send_from_rvt(struct rvt_qp *qp)
843 hfi1_do_send(qp, false);
846 void _hfi1_do_send(struct work_struct *work)
848 struct iowait *wait = container_of(work, struct iowait, iowork);
849 struct rvt_qp *qp = iowait_to_qp(wait);
851 hfi1_do_send(qp, true);
855 * hfi1_do_send - perform a send on a QP
856 * @work: contains a pointer to the QP
857 * @in_thread: true if in a workqueue thread
859 * Process entries in the send work queue until credit or queue is
860 * exhausted. Only allow one CPU to send a packet per QP.
861 * Otherwise, two threads could send packets out of order.
863 void hfi1_do_send(struct rvt_qp *qp, bool in_thread)
865 struct hfi1_pkt_state ps;
866 struct hfi1_qp_priv *priv = qp->priv;
867 int (*make_req)(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
868 unsigned long timeout;
869 unsigned long timeout_int;
872 ps.dev = to_idev(qp->ibqp.device);
873 ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
874 ps.ppd = ppd_from_ibp(ps.ibp);
876 switch (qp->ibqp.qp_type) {
878 if (!loopback && ((qp->remote_ah_attr.dlid & ~((1 << ps.ppd->lmc
884 make_req = hfi1_make_rc_req;
885 timeout_int = (qp->timeout_jiffies);
888 if (!loopback && ((qp->remote_ah_attr.dlid & ~((1 << ps.ppd->lmc
894 make_req = hfi1_make_uc_req;
895 timeout_int = SEND_RESCHED_TIMEOUT;
898 make_req = hfi1_make_ud_req;
899 timeout_int = SEND_RESCHED_TIMEOUT;
902 spin_lock_irqsave(&qp->s_lock, ps.flags);
904 /* Return if we are already busy processing a work request. */
905 if (!hfi1_send_ok(qp)) {
906 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
910 qp->s_flags |= RVT_S_BUSY;
912 timeout = jiffies + (timeout_int) / 8;
913 cpu = priv->s_sde ? priv->s_sde->cpu :
914 cpumask_first(cpumask_of_node(ps.ppd->dd->node));
915 /* insure a pre-built packet is handled */
916 ps.s_txreq = get_waiting_verbs_txreq(qp);
918 /* Check for a constructed packet to be sent. */
919 if (qp->s_hdrwords != 0) {
920 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
922 * If the packet cannot be sent now, return and
923 * the send engine will be woken up later.
925 if (hfi1_verbs_send(qp, &ps))
927 /* Record that s_ahg is empty. */
929 /* allow other tasks to run */
930 if (unlikely(time_after(jiffies, timeout))) {
938 qp->s_flags &= ~RVT_S_BUSY;
939 hfi1_schedule_send(qp);
940 spin_unlock_irqrestore(
944 *ps.ppd->dd->send_schedule);
949 *ps.ppd->dd->send_schedule);
950 timeout = jiffies + (timeout_int) / 8;
952 spin_lock_irqsave(&qp->s_lock, ps.flags);
954 } while (make_req(qp, &ps));
956 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
960 * This should be called with s_lock held.
962 void hfi1_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
963 enum ib_wc_status status)
968 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
973 if (++last >= qp->s_size)
976 /* See post_send() */
978 for (i = 0; i < wqe->wr.num_sge; i++) {
979 struct rvt_sge *sge = &wqe->sg_list[i];
983 if (qp->ibqp.qp_type == IB_QPT_UD ||
984 qp->ibqp.qp_type == IB_QPT_SMI ||
985 qp->ibqp.qp_type == IB_QPT_GSI)
986 atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
988 /* See ch. 11.2.4.1 and 10.7.3.1 */
989 if (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
990 (wqe->wr.send_flags & IB_SEND_SIGNALED) ||
991 status != IB_WC_SUCCESS) {
994 memset(&wc, 0, sizeof(wc));
995 wc.wr_id = wqe->wr.wr_id;
997 wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode];
999 if (status == IB_WC_SUCCESS)
1000 wc.byte_len = wqe->length;
1001 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.send_cq), &wc,
1002 status != IB_WC_SUCCESS);
1005 if (qp->s_acked == old_last)
1007 if (qp->s_cur == old_last)
1009 if (qp->s_tail == old_last)
1011 if (qp->state == IB_QPS_SQD && last == qp->s_cur)