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, IB_NOTICE_TRAP_BAD_PKEY,
293 (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
295 hdr->lrh[3], hdr->lrh[1]);
298 /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
299 if (be16_to_cpu(hdr->lrh[3]) != qp->alt_ah_attr.dlid ||
300 ppd_from_ibp(ibp)->port != qp->alt_ah_attr.port_num)
302 spin_lock_irqsave(&qp->s_lock, flags);
304 spin_unlock_irqrestore(&qp->s_lock, flags);
307 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
310 if (!(qp->remote_ah_attr.ah_flags & IB_AH_GRH))
312 guid = get_sguid(ibp,
313 qp->remote_ah_attr.grh.sgid_index);
314 if (!gid_ok(&hdr->u.l.grh.dgid, ibp->gid_prefix, guid))
316 if (!gid_ok(&hdr->u.l.grh.sgid,
317 qp->remote_ah_attr.grh.dgid.global.subnet_prefix,
318 qp->remote_ah_attr.grh.dgid.global.interface_id))
321 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0,
322 sc5, be16_to_cpu(hdr->lrh[3])))) {
323 hfi1_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY,
325 (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
327 hdr->lrh[3], hdr->lrh[1]);
330 /* Validate the SLID. See Ch. 9.6.1.5 */
331 if (be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid ||
332 ppd_from_ibp(ibp)->port != qp->port_num)
334 if (qp->s_mig_state == IB_MIG_REARM &&
335 !(bth0 & IB_BTH_MIG_REQ))
336 qp->s_mig_state = IB_MIG_ARMED;
346 * ruc_loopback - handle UC and RC loopback requests
347 * @sqp: the sending QP
349 * This is called from hfi1_do_send() to
350 * forward a WQE addressed to the same HFI.
351 * Note that although we are single threaded due to the tasklet, we still
352 * have to protect against post_send(). We don't have to worry about
353 * receive interrupts since this is a connected protocol and all packets
354 * will pass through here.
356 static void ruc_loopback(struct hfi1_qp *sqp)
358 struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
360 struct hfi1_swqe *wqe;
361 struct hfi1_sge *sge;
366 enum ib_wc_status send_status;
373 * Note that we check the responder QP state after
374 * checking the requester's state.
376 qp = hfi1_lookup_qpn(ibp, sqp->remote_qpn);
378 spin_lock_irqsave(&sqp->s_lock, flags);
380 /* Return if we are already busy processing a work request. */
381 if ((sqp->s_flags & (HFI1_S_BUSY | HFI1_S_ANY_WAIT)) ||
382 !(ib_hfi1_state_ops[sqp->state] & HFI1_PROCESS_OR_FLUSH_SEND))
385 sqp->s_flags |= HFI1_S_BUSY;
388 if (sqp->s_last == sqp->s_head)
390 wqe = get_swqe_ptr(sqp, sqp->s_last);
392 /* Return if it is not OK to start a new work request. */
393 if (!(ib_hfi1_state_ops[sqp->state] & HFI1_PROCESS_NEXT_SEND_OK)) {
394 if (!(ib_hfi1_state_ops[sqp->state] & HFI1_FLUSH_SEND))
396 /* We are in the error state, flush the work request. */
397 send_status = IB_WC_WR_FLUSH_ERR;
402 * We can rely on the entry not changing without the s_lock
403 * being held until we update s_last.
404 * We increment s_cur to indicate s_last is in progress.
406 if (sqp->s_last == sqp->s_cur) {
407 if (++sqp->s_cur >= sqp->s_size)
410 spin_unlock_irqrestore(&sqp->s_lock, flags);
412 if (!qp || !(ib_hfi1_state_ops[qp->state] & HFI1_PROCESS_RECV_OK) ||
413 qp->ibqp.qp_type != sqp->ibqp.qp_type) {
416 * For RC, the requester would timeout and retry so
417 * shortcut the timeouts and just signal too many retries.
419 if (sqp->ibqp.qp_type == IB_QPT_RC)
420 send_status = IB_WC_RETRY_EXC_ERR;
422 send_status = IB_WC_SUCCESS;
426 memset(&wc, 0, sizeof(wc));
427 send_status = IB_WC_SUCCESS;
430 sqp->s_sge.sge = wqe->sg_list[0];
431 sqp->s_sge.sg_list = wqe->sg_list + 1;
432 sqp->s_sge.num_sge = wqe->wr.num_sge;
433 sqp->s_len = wqe->length;
434 switch (wqe->wr.opcode) {
435 case IB_WR_SEND_WITH_IMM:
436 wc.wc_flags = IB_WC_WITH_IMM;
437 wc.ex.imm_data = wqe->wr.ex.imm_data;
440 ret = hfi1_get_rwqe(qp, 0);
447 case IB_WR_RDMA_WRITE_WITH_IMM:
448 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
450 wc.wc_flags = IB_WC_WITH_IMM;
451 wc.ex.imm_data = wqe->wr.ex.imm_data;
452 ret = hfi1_get_rwqe(qp, 1);
458 case IB_WR_RDMA_WRITE:
459 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
461 if (wqe->length == 0)
463 if (unlikely(!hfi1_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
464 wqe->rdma_wr.remote_addr,
466 IB_ACCESS_REMOTE_WRITE)))
468 qp->r_sge.sg_list = NULL;
469 qp->r_sge.num_sge = 1;
470 qp->r_sge.total_len = wqe->length;
473 case IB_WR_RDMA_READ:
474 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
476 if (unlikely(!hfi1_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
477 wqe->rdma_wr.remote_addr,
479 IB_ACCESS_REMOTE_READ)))
482 sqp->s_sge.sg_list = NULL;
483 sqp->s_sge.num_sge = 1;
484 qp->r_sge.sge = wqe->sg_list[0];
485 qp->r_sge.sg_list = wqe->sg_list + 1;
486 qp->r_sge.num_sge = wqe->wr.num_sge;
487 qp->r_sge.total_len = wqe->length;
490 case IB_WR_ATOMIC_CMP_AND_SWP:
491 case IB_WR_ATOMIC_FETCH_AND_ADD:
492 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
494 if (unlikely(!hfi1_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
495 wqe->atomic_wr.remote_addr,
497 IB_ACCESS_REMOTE_ATOMIC)))
499 /* Perform atomic OP and save result. */
500 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
501 sdata = wqe->atomic_wr.compare_add;
502 *(u64 *) sqp->s_sge.sge.vaddr =
503 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
504 (u64) atomic64_add_return(sdata, maddr) - sdata :
505 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
506 sdata, wqe->atomic_wr.swap);
507 hfi1_put_mr(qp->r_sge.sge.mr);
508 qp->r_sge.num_sge = 0;
512 send_status = IB_WC_LOC_QP_OP_ERR;
516 sge = &sqp->s_sge.sge;
518 u32 len = sqp->s_len;
520 if (len > sge->length)
522 if (len > sge->sge_length)
523 len = sge->sge_length;
524 WARN_ON_ONCE(len == 0);
525 hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, release);
528 sge->sge_length -= len;
529 if (sge->sge_length == 0) {
531 hfi1_put_mr(sge->mr);
532 if (--sqp->s_sge.num_sge)
533 *sge = *sqp->s_sge.sg_list++;
534 } else if (sge->length == 0 && sge->mr->lkey) {
535 if (++sge->n >= HFI1_SEGSZ) {
536 if (++sge->m >= sge->mr->mapsz)
541 sge->mr->map[sge->m]->segs[sge->n].vaddr;
543 sge->mr->map[sge->m]->segs[sge->n].length;
548 hfi1_put_ss(&qp->r_sge);
550 if (!test_and_clear_bit(HFI1_R_WRID_VALID, &qp->r_aflags))
553 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
554 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
556 wc.opcode = IB_WC_RECV;
557 wc.wr_id = qp->r_wr_id;
558 wc.status = IB_WC_SUCCESS;
559 wc.byte_len = wqe->length;
561 wc.src_qp = qp->remote_qpn;
562 wc.slid = qp->remote_ah_attr.dlid;
563 wc.sl = qp->remote_ah_attr.sl;
565 /* Signal completion event if the solicited bit is set. */
566 hfi1_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
567 wqe->wr.send_flags & IB_SEND_SOLICITED);
570 spin_lock_irqsave(&sqp->s_lock, flags);
573 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
574 hfi1_send_complete(sqp, wqe, send_status);
579 if (qp->ibqp.qp_type == IB_QPT_UC)
583 * Note: we don't need the s_lock held since the BUSY flag
584 * makes this single threaded.
586 if (sqp->s_rnr_retry == 0) {
587 send_status = IB_WC_RNR_RETRY_EXC_ERR;
590 if (sqp->s_rnr_retry_cnt < 7)
592 spin_lock_irqsave(&sqp->s_lock, flags);
593 if (!(ib_hfi1_state_ops[sqp->state] & HFI1_PROCESS_RECV_OK))
595 sqp->s_flags |= HFI1_S_WAIT_RNR;
596 sqp->s_timer.function = hfi1_rc_rnr_retry;
597 sqp->s_timer.expires = jiffies +
598 usecs_to_jiffies(ib_hfi1_rnr_table[qp->r_min_rnr_timer]);
599 add_timer(&sqp->s_timer);
603 send_status = IB_WC_REM_OP_ERR;
604 wc.status = IB_WC_LOC_QP_OP_ERR;
608 send_status = IB_WC_REM_INV_REQ_ERR;
609 wc.status = IB_WC_LOC_QP_OP_ERR;
613 send_status = IB_WC_REM_ACCESS_ERR;
614 wc.status = IB_WC_LOC_PROT_ERR;
616 /* responder goes to error state */
617 hfi1_rc_error(qp, wc.status);
620 spin_lock_irqsave(&sqp->s_lock, flags);
621 hfi1_send_complete(sqp, wqe, send_status);
622 if (sqp->ibqp.qp_type == IB_QPT_RC) {
623 int lastwqe = hfi1_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
625 sqp->s_flags &= ~HFI1_S_BUSY;
626 spin_unlock_irqrestore(&sqp->s_lock, flags);
630 ev.device = sqp->ibqp.device;
631 ev.element.qp = &sqp->ibqp;
632 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
633 sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
638 sqp->s_flags &= ~HFI1_S_BUSY;
640 spin_unlock_irqrestore(&sqp->s_lock, flags);
646 * hfi1_make_grh - construct a GRH header
647 * @ibp: a pointer to the IB port
648 * @hdr: a pointer to the GRH header being constructed
649 * @grh: the global route address to send to
650 * @hwords: the number of 32 bit words of header being sent
651 * @nwords: the number of 32 bit words of data being sent
653 * Return the size of the header in 32 bit words.
655 u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
656 struct ib_global_route *grh, u32 hwords, u32 nwords)
658 hdr->version_tclass_flow =
659 cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
660 (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
661 (grh->flow_label << IB_GRH_FLOW_SHIFT));
662 hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
663 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
664 hdr->next_hdr = IB_GRH_NEXT_HDR;
665 hdr->hop_limit = grh->hop_limit;
666 /* The SGID is 32-bit aligned. */
667 hdr->sgid.global.subnet_prefix = ibp->gid_prefix;
668 hdr->sgid.global.interface_id =
669 grh->sgid_index && grh->sgid_index < ARRAY_SIZE(ibp->guids) ?
670 ibp->guids[grh->sgid_index - 1] :
671 cpu_to_be64(ppd_from_ibp(ibp)->guid);
672 hdr->dgid = grh->dgid;
674 /* GRH header size in 32-bit words. */
675 return sizeof(struct ib_grh) / sizeof(u32);
678 #define BTH2_OFFSET (offsetof(struct hfi1_pio_header, hdr.u.oth.bth[2]) / 4)
681 * build_ahg - create ahg in s_hdr
682 * @qp: a pointer to QP
683 * @npsn: the next PSN for the request/response
685 * This routine handles the AHG by allocating an ahg entry and causing the
686 * copy of the first middle.
688 * Subsequent middles use the copied entry, editing the
689 * PSN with 1 or 2 edits.
691 static inline void build_ahg(struct hfi1_qp *qp, u32 npsn)
693 if (unlikely(qp->s_flags & HFI1_S_AHG_CLEAR))
695 if (!(qp->s_flags & HFI1_S_AHG_VALID)) {
696 /* first middle that needs copy */
697 if (qp->s_ahgidx < 0)
698 qp->s_ahgidx = sdma_ahg_alloc(qp->s_sde);
699 if (qp->s_ahgidx >= 0) {
701 qp->s_hdr->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
702 /* save to protect a change in another thread */
703 qp->s_hdr->sde = qp->s_sde;
704 qp->s_hdr->ahgidx = qp->s_ahgidx;
705 qp->s_flags |= HFI1_S_AHG_VALID;
708 /* subsequent middle after valid */
709 if (qp->s_ahgidx >= 0) {
710 qp->s_hdr->tx_flags |= SDMA_TXREQ_F_USE_AHG;
711 qp->s_hdr->ahgidx = qp->s_ahgidx;
712 qp->s_hdr->ahgcount++;
713 qp->s_hdr->ahgdesc[0] =
714 sdma_build_ahg_descriptor(
715 (__force u16)cpu_to_be16((u16)npsn),
719 if ((npsn & 0xffff0000) !=
720 (qp->s_ahgpsn & 0xffff0000)) {
721 qp->s_hdr->ahgcount++;
722 qp->s_hdr->ahgdesc[1] =
723 sdma_build_ahg_descriptor(
724 (__force u16)cpu_to_be16(
734 void hfi1_make_ruc_header(struct hfi1_qp *qp, struct hfi1_other_headers *ohdr,
735 u32 bth0, u32 bth2, int middle)
737 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
743 /* Construct the header. */
744 extra_bytes = -qp->s_cur_size & 3;
745 nwords = (qp->s_cur_size + extra_bytes) >> 2;
747 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
748 qp->s_hdrwords += hfi1_make_grh(ibp, &qp->s_hdr->ibh.u.l.grh,
749 &qp->remote_ah_attr.grh,
750 qp->s_hdrwords, nwords);
754 lrh0 |= (qp->s_sc & 0xf) << 12 | (qp->remote_ah_attr.sl & 0xf) << 4;
756 * reset s_hdr/AHG fields
758 * This insures that the ahgentry/ahgcount
759 * are at a non-AHG default to protect
760 * build_verbs_tx_desc() from using
763 * build_ahg() will modify as appropriate
764 * to use the AHG feature.
766 qp->s_hdr->tx_flags = 0;
767 qp->s_hdr->ahgcount = 0;
768 qp->s_hdr->ahgidx = 0;
769 qp->s_hdr->sde = NULL;
770 if (qp->s_mig_state == IB_MIG_MIGRATED)
771 bth0 |= IB_BTH_MIG_REQ;
777 qp->s_flags &= ~HFI1_S_AHG_VALID;
778 qp->s_hdr->ibh.lrh[0] = cpu_to_be16(lrh0);
779 qp->s_hdr->ibh.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
780 qp->s_hdr->ibh.lrh[2] =
781 cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
782 qp->s_hdr->ibh.lrh[3] = cpu_to_be16(ppd_from_ibp(ibp)->lid |
783 qp->remote_ah_attr.src_path_bits);
784 bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
785 bth0 |= extra_bytes << 20;
786 ohdr->bth[0] = cpu_to_be32(bth0);
787 bth1 = qp->remote_qpn;
788 if (qp->s_flags & HFI1_S_ECN) {
789 qp->s_flags &= ~HFI1_S_ECN;
790 /* we recently received a FECN, so return a BECN */
791 bth1 |= (HFI1_BECN_MASK << HFI1_BECN_SHIFT);
793 ohdr->bth[1] = cpu_to_be32(bth1);
794 ohdr->bth[2] = cpu_to_be32(bth2);
797 /* when sending, force a reschedule every one of these periods */
798 #define SEND_RESCHED_TIMEOUT (5 * HZ) /* 5s in jiffies */
801 * hfi1_do_send - perform a send on a QP
802 * @work: contains a pointer to the QP
804 * Process entries in the send work queue until credit or queue is
805 * exhausted. Only allow one CPU to send a packet per QP (tasklet).
806 * Otherwise, two threads could send packets out of order.
808 void hfi1_do_send(struct work_struct *work)
810 struct iowait *wait = container_of(work, struct iowait, iowork);
811 struct hfi1_qp *qp = container_of(wait, struct hfi1_qp, s_iowait);
812 struct hfi1_pkt_state ps;
813 int (*make_req)(struct hfi1_qp *qp);
815 unsigned long timeout;
817 ps.dev = to_idev(qp->ibqp.device);
818 ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
819 ps.ppd = ppd_from_ibp(ps.ibp);
821 if ((qp->ibqp.qp_type == IB_QPT_RC ||
822 qp->ibqp.qp_type == IB_QPT_UC) &&
824 (qp->remote_ah_attr.dlid & ~((1 << ps.ppd->lmc) - 1)) ==
830 if (qp->ibqp.qp_type == IB_QPT_RC)
831 make_req = hfi1_make_rc_req;
832 else if (qp->ibqp.qp_type == IB_QPT_UC)
833 make_req = hfi1_make_uc_req;
835 make_req = hfi1_make_ud_req;
837 spin_lock_irqsave(&qp->s_lock, flags);
839 /* Return if we are already busy processing a work request. */
840 if (!hfi1_send_ok(qp)) {
841 spin_unlock_irqrestore(&qp->s_lock, flags);
845 qp->s_flags |= HFI1_S_BUSY;
847 spin_unlock_irqrestore(&qp->s_lock, flags);
849 timeout = jiffies + SEND_RESCHED_TIMEOUT;
851 /* Check for a constructed packet to be sent. */
852 if (qp->s_hdrwords != 0) {
854 * If the packet cannot be sent now, return and
855 * the send tasklet will be woken up later.
857 if (hfi1_verbs_send(qp, &ps))
859 /* Record that s_hdr is empty. */
863 /* allow other tasks to run */
864 if (unlikely(time_after(jiffies, timeout))) {
866 ps.ppd->dd->verbs_dev.n_send_schedule++;
867 timeout = jiffies + SEND_RESCHED_TIMEOUT;
869 } while (make_req(qp));
873 * This should be called with s_lock held.
875 void hfi1_send_complete(struct hfi1_qp *qp, struct hfi1_swqe *wqe,
876 enum ib_wc_status status)
881 if (!(ib_hfi1_state_ops[qp->state] & HFI1_PROCESS_OR_FLUSH_SEND))
884 for (i = 0; i < wqe->wr.num_sge; i++) {
885 struct hfi1_sge *sge = &wqe->sg_list[i];
887 hfi1_put_mr(sge->mr);
889 if (qp->ibqp.qp_type == IB_QPT_UD ||
890 qp->ibqp.qp_type == IB_QPT_SMI ||
891 qp->ibqp.qp_type == IB_QPT_GSI)
892 atomic_dec(&to_iah(wqe->ud_wr.ah)->refcount);
894 /* See ch. 11.2.4.1 and 10.7.3.1 */
895 if (!(qp->s_flags & HFI1_S_SIGNAL_REQ_WR) ||
896 (wqe->wr.send_flags & IB_SEND_SIGNALED) ||
897 status != IB_WC_SUCCESS) {
900 memset(&wc, 0, sizeof(wc));
901 wc.wr_id = wqe->wr.wr_id;
903 wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode];
905 if (status == IB_WC_SUCCESS)
906 wc.byte_len = wqe->length;
907 hfi1_cq_enter(to_icq(qp->ibqp.send_cq), &wc,
908 status != IB_WC_SUCCESS);
913 if (++last >= qp->s_size)
916 if (qp->s_acked == old_last)
918 if (qp->s_cur == old_last)
920 if (qp->s_tail == old_last)
922 if (qp->state == IB_QPS_SQD && last == qp->s_cur)