154e4ee71SNavdeep Parhar /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
454e4ee71SNavdeep Parhar * Copyright (c) 2011 Chelsio Communications, Inc.
554e4ee71SNavdeep Parhar * All rights reserved.
654e4ee71SNavdeep Parhar * Written by: Navdeep Parhar <np@FreeBSD.org>
754e4ee71SNavdeep Parhar *
854e4ee71SNavdeep Parhar * Redistribution and use in source and binary forms, with or without
954e4ee71SNavdeep Parhar * modification, are permitted provided that the following conditions
1054e4ee71SNavdeep Parhar * are met:
1154e4ee71SNavdeep Parhar * 1. Redistributions of source code must retain the above copyright
1254e4ee71SNavdeep Parhar * notice, this list of conditions and the following disclaimer.
1354e4ee71SNavdeep Parhar * 2. Redistributions in binary form must reproduce the above copyright
1454e4ee71SNavdeep Parhar * notice, this list of conditions and the following disclaimer in the
1554e4ee71SNavdeep Parhar * documentation and/or other materials provided with the distribution.
1654e4ee71SNavdeep Parhar *
1754e4ee71SNavdeep Parhar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1854e4ee71SNavdeep Parhar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1954e4ee71SNavdeep Parhar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2054e4ee71SNavdeep Parhar * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2154e4ee71SNavdeep Parhar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2254e4ee71SNavdeep Parhar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2354e4ee71SNavdeep Parhar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2454e4ee71SNavdeep Parhar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2554e4ee71SNavdeep Parhar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2654e4ee71SNavdeep Parhar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2754e4ee71SNavdeep Parhar * SUCH DAMAGE.
2854e4ee71SNavdeep Parhar */
2954e4ee71SNavdeep Parhar
3054e4ee71SNavdeep Parhar #include <sys/cdefs.h>
3154e4ee71SNavdeep Parhar #include "opt_inet.h"
32a1ea9a82SNavdeep Parhar #include "opt_inet6.h"
33bddf7343SJohn Baldwin #include "opt_kern_tls.h"
34eff62dbaSNavdeep Parhar #include "opt_ratelimit.h"
3554e4ee71SNavdeep Parhar
3654e4ee71SNavdeep Parhar #include <sys/types.h>
37c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
3854e4ee71SNavdeep Parhar #include <sys/mbuf.h>
3954e4ee71SNavdeep Parhar #include <sys/socket.h>
4054e4ee71SNavdeep Parhar #include <sys/kernel.h>
41bddf7343SJohn Baldwin #include <sys/ktls.h>
42ecb79ca4SNavdeep Parhar #include <sys/malloc.h>
4314a634dfSMark Johnston #include <sys/msan.h>
44ecb79ca4SNavdeep Parhar #include <sys/queue.h>
4538035ed6SNavdeep Parhar #include <sys/sbuf.h>
46ecb79ca4SNavdeep Parhar #include <sys/taskqueue.h>
47480e603cSNavdeep Parhar #include <sys/time.h>
487951040fSNavdeep Parhar #include <sys/sglist.h>
4954e4ee71SNavdeep Parhar #include <sys/sysctl.h>
50733b9277SNavdeep Parhar #include <sys/smp.h>
51bddf7343SJohn Baldwin #include <sys/socketvar.h>
5282eff304SNavdeep Parhar #include <sys/counter.h>
5354e4ee71SNavdeep Parhar #include <net/bpf.h>
5454e4ee71SNavdeep Parhar #include <net/ethernet.h>
5554e4ee71SNavdeep Parhar #include <net/if.h>
5654e4ee71SNavdeep Parhar #include <net/if_vlan_var.h>
57a4a4ad2dSNavdeep Parhar #include <net/if_vxlan.h>
5854e4ee71SNavdeep Parhar #include <netinet/in.h>
5954e4ee71SNavdeep Parhar #include <netinet/ip.h>
60a1ea9a82SNavdeep Parhar #include <netinet/ip6.h>
6154e4ee71SNavdeep Parhar #include <netinet/tcp.h>
62786099deSNavdeep Parhar #include <netinet/udp.h>
636af45170SJohn Baldwin #include <machine/in_cksum.h>
6464db8966SDimitry Andric #include <machine/md_var.h>
6538035ed6SNavdeep Parhar #include <vm/vm.h>
6638035ed6SNavdeep Parhar #include <vm/pmap.h>
67298d969cSNavdeep Parhar #ifdef DEV_NETMAP
68298d969cSNavdeep Parhar #include <machine/bus.h>
69298d969cSNavdeep Parhar #include <sys/selinfo.h>
70298d969cSNavdeep Parhar #include <net/if_var.h>
71298d969cSNavdeep Parhar #include <net/netmap.h>
72298d969cSNavdeep Parhar #include <dev/netmap/netmap_kern.h>
73298d969cSNavdeep Parhar #endif
7454e4ee71SNavdeep Parhar
7554e4ee71SNavdeep Parhar #include "common/common.h"
7654e4ee71SNavdeep Parhar #include "common/t4_regs.h"
7754e4ee71SNavdeep Parhar #include "common/t4_regs_values.h"
7854e4ee71SNavdeep Parhar #include "common/t4_msg.h"
79671bf2b8SNavdeep Parhar #include "t4_l2t.h"
807951040fSNavdeep Parhar #include "t4_mp_ring.h"
8154e4ee71SNavdeep Parhar
82d14b0ac1SNavdeep Parhar #define RX_COPY_THRESHOLD MINCLSIZE
83d14b0ac1SNavdeep Parhar
849fb8886bSNavdeep Parhar /*
859fb8886bSNavdeep Parhar * Ethernet frames are DMA'd at this byte offset into the freelist buffer.
869fb8886bSNavdeep Parhar * 0-7 are valid values.
879fb8886bSNavdeep Parhar */
88518bca2cSNavdeep Parhar static int fl_pktshift = 0;
892d714dbcSJohn Baldwin SYSCTL_INT(_hw_cxgbe, OID_AUTO, fl_pktshift, CTLFLAG_RDTUN, &fl_pktshift, 0,
902d714dbcSJohn Baldwin "payload DMA offset in rx buffer (bytes)");
9154e4ee71SNavdeep Parhar
929fb8886bSNavdeep Parhar /*
939fb8886bSNavdeep Parhar * Pad ethernet payload up to this boundary.
949fb8886bSNavdeep Parhar * -1: driver should figure out a good value.
951458bff9SNavdeep Parhar * 0: disable padding.
961458bff9SNavdeep Parhar * Any power of 2 from 32 to 4096 (both inclusive) is also a valid value.
979fb8886bSNavdeep Parhar */
98298d969cSNavdeep Parhar int fl_pad = -1;
992d714dbcSJohn Baldwin SYSCTL_INT(_hw_cxgbe, OID_AUTO, fl_pad, CTLFLAG_RDTUN, &fl_pad, 0,
1002d714dbcSJohn Baldwin "payload pad boundary (bytes)");
1019fb8886bSNavdeep Parhar
1029fb8886bSNavdeep Parhar /*
1039fb8886bSNavdeep Parhar * Status page length.
1049fb8886bSNavdeep Parhar * -1: driver should figure out a good value.
1059fb8886bSNavdeep Parhar * 64 or 128 are the only other valid values.
1069fb8886bSNavdeep Parhar */
10729c229e9SJohn Baldwin static int spg_len = -1;
1082d714dbcSJohn Baldwin SYSCTL_INT(_hw_cxgbe, OID_AUTO, spg_len, CTLFLAG_RDTUN, &spg_len, 0,
1092d714dbcSJohn Baldwin "status page size (bytes)");
1109fb8886bSNavdeep Parhar
1119fb8886bSNavdeep Parhar /*
1129fb8886bSNavdeep Parhar * Congestion drops.
1139fb8886bSNavdeep Parhar * -1: no congestion feedback (not recommended).
1149fb8886bSNavdeep Parhar * 0: backpressure the channel instead of dropping packets right away.
1159fb8886bSNavdeep Parhar * 1: no backpressure, drop packets for the congested queue immediately.
116df275ae5SNavdeep Parhar * 2: both backpressure and drop.
1179fb8886bSNavdeep Parhar */
1189fb8886bSNavdeep Parhar static int cong_drop = 0;
1192d714dbcSJohn Baldwin SYSCTL_INT(_hw_cxgbe, OID_AUTO, cong_drop, CTLFLAG_RDTUN, &cong_drop, 0,
120df275ae5SNavdeep Parhar "Congestion control for NIC RX queues (0 = backpressure, 1 = drop, 2 = both");
121998eb37aSNavdeep Parhar #ifdef TCP_OFFLOAD
122998eb37aSNavdeep Parhar static int ofld_cong_drop = 0;
123998eb37aSNavdeep Parhar SYSCTL_INT(_hw_cxgbe, OID_AUTO, ofld_cong_drop, CTLFLAG_RDTUN, &ofld_cong_drop, 0,
124998eb37aSNavdeep Parhar "Congestion control for TOE RX queues (0 = backpressure, 1 = drop, 2 = both");
125998eb37aSNavdeep Parhar #endif
12654e4ee71SNavdeep Parhar
1271458bff9SNavdeep Parhar /*
1281458bff9SNavdeep Parhar * Deliver multiple frames in the same free list buffer if they fit.
1291458bff9SNavdeep Parhar * -1: let the driver decide whether to enable buffer packing or not.
1301458bff9SNavdeep Parhar * 0: disable buffer packing.
1311458bff9SNavdeep Parhar * 1: enable buffer packing.
1321458bff9SNavdeep Parhar */
1331458bff9SNavdeep Parhar static int buffer_packing = -1;
1342d714dbcSJohn Baldwin SYSCTL_INT(_hw_cxgbe, OID_AUTO, buffer_packing, CTLFLAG_RDTUN, &buffer_packing,
1352d714dbcSJohn Baldwin 0, "Enable buffer packing");
1361458bff9SNavdeep Parhar
1371458bff9SNavdeep Parhar /*
1381458bff9SNavdeep Parhar * Start next frame in a packed buffer at this boundary.
1391458bff9SNavdeep Parhar * -1: driver should figure out a good value.
140e3207e19SNavdeep Parhar * T4: driver will ignore this and use the same value as fl_pad above.
141e3207e19SNavdeep Parhar * T5: 16, or a power of 2 from 64 to 4096 (both inclusive) is a valid value.
1421458bff9SNavdeep Parhar */
1431458bff9SNavdeep Parhar static int fl_pack = -1;
1442d714dbcSJohn Baldwin SYSCTL_INT(_hw_cxgbe, OID_AUTO, fl_pack, CTLFLAG_RDTUN, &fl_pack, 0,
1452d714dbcSJohn Baldwin "payload pack boundary (bytes)");
1461458bff9SNavdeep Parhar
14738035ed6SNavdeep Parhar /*
14838035ed6SNavdeep Parhar * Largest rx cluster size that the driver is allowed to allocate.
14938035ed6SNavdeep Parhar */
15038035ed6SNavdeep Parhar static int largest_rx_cluster = MJUM16BYTES;
1512d714dbcSJohn Baldwin SYSCTL_INT(_hw_cxgbe, OID_AUTO, largest_rx_cluster, CTLFLAG_RDTUN,
1522d714dbcSJohn Baldwin &largest_rx_cluster, 0, "Largest rx cluster (bytes)");
15338035ed6SNavdeep Parhar
15438035ed6SNavdeep Parhar /*
15538035ed6SNavdeep Parhar * Size of cluster allocation that's most likely to succeed. The driver will
15638035ed6SNavdeep Parhar * fall back to this size if it fails to allocate clusters larger than this.
15738035ed6SNavdeep Parhar */
15838035ed6SNavdeep Parhar static int safest_rx_cluster = PAGE_SIZE;
1592d714dbcSJohn Baldwin SYSCTL_INT(_hw_cxgbe, OID_AUTO, safest_rx_cluster, CTLFLAG_RDTUN,
1602d714dbcSJohn Baldwin &safest_rx_cluster, 0, "Safe rx cluster (bytes)");
16138035ed6SNavdeep Parhar
162786099deSNavdeep Parhar #ifdef RATELIMIT
163786099deSNavdeep Parhar /*
164786099deSNavdeep Parhar * Knob to control TCP timestamp rewriting, and the granularity of the tick used
165786099deSNavdeep Parhar * for rewriting. -1 and 0-3 are all valid values.
166786099deSNavdeep Parhar * -1: hardware should leave the TCP timestamps alone.
167786099deSNavdeep Parhar * 0: 1ms
168786099deSNavdeep Parhar * 1: 100us
169786099deSNavdeep Parhar * 2: 10us
170786099deSNavdeep Parhar * 3: 1us
171786099deSNavdeep Parhar */
172786099deSNavdeep Parhar static int tsclk = -1;
1732d714dbcSJohn Baldwin SYSCTL_INT(_hw_cxgbe, OID_AUTO, tsclk, CTLFLAG_RDTUN, &tsclk, 0,
1742d714dbcSJohn Baldwin "Control TCP timestamp rewriting when using pacing");
175786099deSNavdeep Parhar
176786099deSNavdeep Parhar static int eo_max_backlog = 1024 * 1024;
1772d714dbcSJohn Baldwin SYSCTL_INT(_hw_cxgbe, OID_AUTO, eo_max_backlog, CTLFLAG_RDTUN, &eo_max_backlog,
1782d714dbcSJohn Baldwin 0, "Maximum backlog of ratelimited data per flow");
179786099deSNavdeep Parhar #endif
180786099deSNavdeep Parhar
181d491f8caSNavdeep Parhar /*
182d491f8caSNavdeep Parhar * The interrupt holdoff timers are multiplied by this value on T6+.
183d491f8caSNavdeep Parhar * 1 and 3-17 (both inclusive) are legal values.
184d491f8caSNavdeep Parhar */
185d491f8caSNavdeep Parhar static int tscale = 1;
1862d714dbcSJohn Baldwin SYSCTL_INT(_hw_cxgbe, OID_AUTO, tscale, CTLFLAG_RDTUN, &tscale, 0,
1872d714dbcSJohn Baldwin "Interrupt holdoff timer scale on T6+");
188d491f8caSNavdeep Parhar
18946f48ee5SNavdeep Parhar /*
19046f48ee5SNavdeep Parhar * Number of LRO entries in the lro_ctrl structure per rx queue.
19146f48ee5SNavdeep Parhar */
19246f48ee5SNavdeep Parhar static int lro_entries = TCP_LRO_ENTRIES;
1932d714dbcSJohn Baldwin SYSCTL_INT(_hw_cxgbe, OID_AUTO, lro_entries, CTLFLAG_RDTUN, &lro_entries, 0,
1942d714dbcSJohn Baldwin "Number of LRO entries per RX queue");
19546f48ee5SNavdeep Parhar
19646f48ee5SNavdeep Parhar /*
19746f48ee5SNavdeep Parhar * This enables presorting of frames before they're fed into tcp_lro_rx.
19846f48ee5SNavdeep Parhar */
19946f48ee5SNavdeep Parhar static int lro_mbufs = 0;
2002d714dbcSJohn Baldwin SYSCTL_INT(_hw_cxgbe, OID_AUTO, lro_mbufs, CTLFLAG_RDTUN, &lro_mbufs, 0,
2012d714dbcSJohn Baldwin "Enable presorting of LRO frames");
20246f48ee5SNavdeep Parhar
2037054f6ecSNavdeep Parhar static counter_u64_t pullups;
2047054f6ecSNavdeep Parhar SYSCTL_COUNTER_U64(_hw_cxgbe, OID_AUTO, pullups, CTLFLAG_RD, &pullups,
2057054f6ecSNavdeep Parhar "Number of mbuf pullups performed");
2067054f6ecSNavdeep Parhar
2077054f6ecSNavdeep Parhar static counter_u64_t defrags;
2087054f6ecSNavdeep Parhar SYSCTL_COUNTER_U64(_hw_cxgbe, OID_AUTO, defrags, CTLFLAG_RD, &defrags,
2097054f6ecSNavdeep Parhar "Number of mbuf defrags performed");
2107054f6ecSNavdeep Parhar
2113447df8bSNavdeep Parhar static int t4_tx_coalesce = 1;
2123447df8bSNavdeep Parhar SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_coalesce, CTLFLAG_RWTUN, &t4_tx_coalesce, 0,
2133447df8bSNavdeep Parhar "tx coalescing allowed");
2143447df8bSNavdeep Parhar
2153447df8bSNavdeep Parhar /*
2163447df8bSNavdeep Parhar * The driver will make aggressive attempts at tx coalescing if it sees these
2173447df8bSNavdeep Parhar * many packets eligible for coalescing in quick succession, with no more than
2183447df8bSNavdeep Parhar * the specified gap in between the eth_tx calls that delivered the packets.
2193447df8bSNavdeep Parhar */
2203447df8bSNavdeep Parhar static int t4_tx_coalesce_pkts = 32;
2213447df8bSNavdeep Parhar SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_coalesce_pkts, CTLFLAG_RWTUN,
2223447df8bSNavdeep Parhar &t4_tx_coalesce_pkts, 0,
2233447df8bSNavdeep Parhar "# of consecutive packets (1 - 255) that will trigger tx coalescing");
2243447df8bSNavdeep Parhar static int t4_tx_coalesce_gap = 5;
2253447df8bSNavdeep Parhar SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_coalesce_gap, CTLFLAG_RWTUN,
2263447df8bSNavdeep Parhar &t4_tx_coalesce_gap, 0, "tx gap (in microseconds)");
2277054f6ecSNavdeep Parhar
228733b9277SNavdeep Parhar static int service_iq(struct sge_iq *, int);
2293098bcfcSNavdeep Parhar static int service_iq_fl(struct sge_iq *, int);
2304d6db4e0SNavdeep Parhar static struct mbuf *get_fl_payload(struct adapter *, struct sge_fl *, uint32_t);
2311486d2deSNavdeep Parhar static int eth_rx(struct adapter *, struct sge_rxq *, const struct iq_desc *,
2321486d2deSNavdeep Parhar u_int);
23343bbae19SNavdeep Parhar static inline void init_iq(struct sge_iq *, struct adapter *, int, int, int,
234c387ff00SNavdeep Parhar int, int, int);
235e3207e19SNavdeep Parhar static inline void init_fl(struct adapter *, struct sge_fl *, int, int, char *);
23690e7434aSNavdeep Parhar static inline void init_eq(struct adapter *, struct sge_eq *, int, int, uint8_t,
23743bbae19SNavdeep Parhar struct sge_iq *, char *);
238fe2ebb76SJohn Baldwin static int alloc_iq_fl(struct vi_info *, struct sge_iq *, struct sge_fl *,
23943bbae19SNavdeep Parhar struct sysctl_ctx_list *, struct sysctl_oid *);
24043bbae19SNavdeep Parhar static void free_iq_fl(struct adapter *, struct sge_iq *, struct sge_fl *);
241348694daSNavdeep Parhar static void add_iq_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *,
242348694daSNavdeep Parhar struct sge_iq *);
243aa93b99aSNavdeep Parhar static void add_fl_sysctls(struct adapter *, struct sysctl_ctx_list *,
244aa93b99aSNavdeep Parhar struct sysctl_oid *, struct sge_fl *);
24543bbae19SNavdeep Parhar static int alloc_iq_fl_hwq(struct vi_info *, struct sge_iq *, struct sge_fl *);
24643bbae19SNavdeep Parhar static int free_iq_fl_hwq(struct adapter *, struct sge_iq *, struct sge_fl *);
247733b9277SNavdeep Parhar static int alloc_fwq(struct adapter *);
24843bbae19SNavdeep Parhar static void free_fwq(struct adapter *);
24943bbae19SNavdeep Parhar static int alloc_ctrlq(struct adapter *, int);
25043bbae19SNavdeep Parhar static void free_ctrlq(struct adapter *, int);
25143bbae19SNavdeep Parhar static int alloc_rxq(struct vi_info *, struct sge_rxq *, int, int, int);
25243bbae19SNavdeep Parhar static void free_rxq(struct vi_info *, struct sge_rxq *);
25343bbae19SNavdeep Parhar static void add_rxq_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *,
25443bbae19SNavdeep Parhar struct sge_rxq *);
25509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
256fe2ebb76SJohn Baldwin static int alloc_ofld_rxq(struct vi_info *, struct sge_ofld_rxq *, int, int,
25743bbae19SNavdeep Parhar int);
25843bbae19SNavdeep Parhar static void free_ofld_rxq(struct vi_info *, struct sge_ofld_rxq *);
25943bbae19SNavdeep Parhar static void add_ofld_rxq_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *,
26043bbae19SNavdeep Parhar struct sge_ofld_rxq *);
261733b9277SNavdeep Parhar #endif
262733b9277SNavdeep Parhar static int ctrl_eq_alloc(struct adapter *, struct sge_eq *);
263fe2ebb76SJohn Baldwin static int eth_eq_alloc(struct adapter *, struct vi_info *, struct sge_eq *);
264eff62dbaSNavdeep Parhar #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
265fe2ebb76SJohn Baldwin static int ofld_eq_alloc(struct adapter *, struct vi_info *, struct sge_eq *);
266733b9277SNavdeep Parhar #endif
26743bbae19SNavdeep Parhar static int alloc_eq(struct adapter *, struct sge_eq *, struct sysctl_ctx_list *,
26843bbae19SNavdeep Parhar struct sysctl_oid *);
26943bbae19SNavdeep Parhar static void free_eq(struct adapter *, struct sge_eq *);
27043bbae19SNavdeep Parhar static void add_eq_sysctls(struct adapter *, struct sysctl_ctx_list *,
27143bbae19SNavdeep Parhar struct sysctl_oid *, struct sge_eq *);
27243bbae19SNavdeep Parhar static int alloc_eq_hwq(struct adapter *, struct vi_info *, struct sge_eq *);
27343bbae19SNavdeep Parhar static int free_eq_hwq(struct adapter *, struct vi_info *, struct sge_eq *);
274fe2ebb76SJohn Baldwin static int alloc_wrq(struct adapter *, struct vi_info *, struct sge_wrq *,
27543bbae19SNavdeep Parhar struct sysctl_ctx_list *, struct sysctl_oid *);
27643bbae19SNavdeep Parhar static void free_wrq(struct adapter *, struct sge_wrq *);
27743bbae19SNavdeep Parhar static void add_wrq_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *,
27843bbae19SNavdeep Parhar struct sge_wrq *);
27943bbae19SNavdeep Parhar static int alloc_txq(struct vi_info *, struct sge_txq *, int);
28043bbae19SNavdeep Parhar static void free_txq(struct vi_info *, struct sge_txq *);
28143bbae19SNavdeep Parhar static void add_txq_sysctls(struct vi_info *, struct sysctl_ctx_list *,
28243bbae19SNavdeep Parhar struct sysctl_oid *, struct sge_txq *);
283077ba6a8SJohn Baldwin #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
28443bbae19SNavdeep Parhar static int alloc_ofld_txq(struct vi_info *, struct sge_ofld_txq *, int);
28543bbae19SNavdeep Parhar static void free_ofld_txq(struct vi_info *, struct sge_ofld_txq *);
28643bbae19SNavdeep Parhar static void add_ofld_txq_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *,
28743bbae19SNavdeep Parhar struct sge_ofld_txq *);
288077ba6a8SJohn Baldwin #endif
28954e4ee71SNavdeep Parhar static void oneseg_dma_callback(void *, bus_dma_segment_t *, int, int);
29054e4ee71SNavdeep Parhar static inline void ring_fl_db(struct adapter *, struct sge_fl *);
291733b9277SNavdeep Parhar static int refill_fl(struct adapter *, struct sge_fl *, int);
292733b9277SNavdeep Parhar static void refill_sfl(void *);
29346e1e307SNavdeep Parhar static int find_refill_source(struct adapter *, int, bool);
294733b9277SNavdeep Parhar static void add_fl_to_sfl(struct adapter *, struct sge_fl *);
29554e4ee71SNavdeep Parhar
2967951040fSNavdeep Parhar static inline void get_pkt_gl(struct mbuf *, struct sglist *);
297a4a4ad2dSNavdeep Parhar static inline u_int txpkt_len16(u_int, const u_int);
298a4a4ad2dSNavdeep Parhar static inline u_int txpkt_vm_len16(u_int, const u_int);
29930e3f2b4SNavdeep Parhar static inline void calculate_mbuf_len16(struct mbuf *, bool);
3007951040fSNavdeep Parhar static inline u_int txpkts0_len16(u_int);
3017951040fSNavdeep Parhar static inline u_int txpkts1_len16(void);
3025cdaef71SJohn Baldwin static u_int write_raw_wr(struct sge_txq *, void *, struct mbuf *, u_int);
303d735920dSNavdeep Parhar static u_int write_txpkt_wr(struct adapter *, struct sge_txq *, struct mbuf *,
304d735920dSNavdeep Parhar u_int);
305472a6004SNavdeep Parhar static u_int write_txpkt_vm_wr(struct adapter *, struct sge_txq *,
306d735920dSNavdeep Parhar struct mbuf *);
307d735920dSNavdeep Parhar static int add_to_txpkts_vf(struct adapter *, struct sge_txq *, struct mbuf *,
308d735920dSNavdeep Parhar int, bool *);
309d735920dSNavdeep Parhar static int add_to_txpkts_pf(struct adapter *, struct sge_txq *, struct mbuf *,
310d735920dSNavdeep Parhar int, bool *);
311d735920dSNavdeep Parhar static u_int write_txpkts_wr(struct adapter *, struct sge_txq *);
312d735920dSNavdeep Parhar static u_int write_txpkts_vm_wr(struct adapter *, struct sge_txq *);
3137951040fSNavdeep Parhar static void write_gl_to_txd(struct sge_txq *, struct mbuf *, caddr_t *, int);
31454e4ee71SNavdeep Parhar static inline void copy_to_txd(struct sge_eq *, caddr_t, caddr_t *, int);
3157951040fSNavdeep Parhar static inline void ring_eq_db(struct adapter *, struct sge_eq *, u_int);
3167951040fSNavdeep Parhar static inline uint16_t read_hw_cidx(struct sge_eq *);
3177951040fSNavdeep Parhar static inline u_int reclaimable_tx_desc(struct sge_eq *);
3187951040fSNavdeep Parhar static inline u_int total_available_tx_desc(struct sge_eq *);
3197951040fSNavdeep Parhar static u_int reclaim_tx_descs(struct sge_txq *, u_int);
3207951040fSNavdeep Parhar static void tx_reclaim(void *, int);
3217951040fSNavdeep Parhar static __be64 get_flit(struct sglist_seg *, int, int);
322733b9277SNavdeep Parhar static int handle_sge_egr_update(struct sge_iq *, const struct rss_header *,
323733b9277SNavdeep Parhar struct mbuf *);
3241b4cc91fSNavdeep Parhar static int handle_fw_msg(struct sge_iq *, const struct rss_header *,
325733b9277SNavdeep Parhar struct mbuf *);
326069af0ebSJohn Baldwin static int t4_handle_wrerr_rpl(struct adapter *, const __be64 *);
3277951040fSNavdeep Parhar static void wrq_tx_drain(void *, int);
3287951040fSNavdeep Parhar static void drain_wrq_wr_list(struct adapter *, struct sge_wrq *);
32954e4ee71SNavdeep Parhar
33038035ed6SNavdeep Parhar static int sysctl_bufsizes(SYSCTL_HANDLER_ARGS);
331786099deSNavdeep Parhar #ifdef RATELIMIT
332786099deSNavdeep Parhar static int ethofld_fw4_ack(struct sge_iq *, const struct rss_header *,
333786099deSNavdeep Parhar struct mbuf *);
334c98146aeSJohn Baldwin #if defined(INET) || defined(INET6)
335c98146aeSJohn Baldwin static inline u_int txpkt_eo_len16(u_int, u_int, u_int);
336954712e8SJustin Hibbits static int ethofld_transmit(if_t, struct mbuf *);
337786099deSNavdeep Parhar #endif
338c98146aeSJohn Baldwin #endif
339f7dfe243SNavdeep Parhar
34082eff304SNavdeep Parhar static counter_u64_t extfree_refs;
34182eff304SNavdeep Parhar static counter_u64_t extfree_rels;
34282eff304SNavdeep Parhar
343671bf2b8SNavdeep Parhar an_handler_t t4_an_handler;
344671bf2b8SNavdeep Parhar fw_msg_handler_t t4_fw_msg_handler[NUM_FW6_TYPES];
345671bf2b8SNavdeep Parhar cpl_handler_t t4_cpl_handler[NUM_CPL_CMDS];
3464535e804SNavdeep Parhar cpl_handler_t set_tcb_rpl_handlers[NUM_CPL_COOKIES];
3474535e804SNavdeep Parhar cpl_handler_t l2t_write_rpl_handlers[NUM_CPL_COOKIES];
348111638bfSNavdeep Parhar cpl_handler_t act_open_rpl_handlers[NUM_CPL_COOKIES];
34989f651e7SNavdeep Parhar cpl_handler_t abort_rpl_rss_handlers[NUM_CPL_COOKIES];
3509c707b32SNavdeep Parhar cpl_handler_t fw4_ack_handlers[NUM_CPL_COOKIES];
351671bf2b8SNavdeep Parhar
3524535e804SNavdeep Parhar void
t4_register_an_handler(an_handler_t h)353671bf2b8SNavdeep Parhar t4_register_an_handler(an_handler_t h)
354671bf2b8SNavdeep Parhar {
3554535e804SNavdeep Parhar uintptr_t *loc;
356671bf2b8SNavdeep Parhar
3574535e804SNavdeep Parhar MPASS(h == NULL || t4_an_handler == NULL);
3584535e804SNavdeep Parhar
359671bf2b8SNavdeep Parhar loc = (uintptr_t *)&t4_an_handler;
3604535e804SNavdeep Parhar atomic_store_rel_ptr(loc, (uintptr_t)h);
361671bf2b8SNavdeep Parhar }
362671bf2b8SNavdeep Parhar
3634535e804SNavdeep Parhar void
t4_register_fw_msg_handler(int type,fw_msg_handler_t h)364671bf2b8SNavdeep Parhar t4_register_fw_msg_handler(int type, fw_msg_handler_t h)
365671bf2b8SNavdeep Parhar {
3664535e804SNavdeep Parhar uintptr_t *loc;
367671bf2b8SNavdeep Parhar
3684535e804SNavdeep Parhar MPASS(type < nitems(t4_fw_msg_handler));
3694535e804SNavdeep Parhar MPASS(h == NULL || t4_fw_msg_handler[type] == NULL);
370671bf2b8SNavdeep Parhar /*
371671bf2b8SNavdeep Parhar * These are dispatched by the handler for FW{4|6}_CPL_MSG using the CPL
372671bf2b8SNavdeep Parhar * handler dispatch table. Reject any attempt to install a handler for
373671bf2b8SNavdeep Parhar * this subtype.
374671bf2b8SNavdeep Parhar */
3754535e804SNavdeep Parhar MPASS(type != FW_TYPE_RSSCPL);
3764535e804SNavdeep Parhar MPASS(type != FW6_TYPE_RSSCPL);
377671bf2b8SNavdeep Parhar
378671bf2b8SNavdeep Parhar loc = (uintptr_t *)&t4_fw_msg_handler[type];
3794535e804SNavdeep Parhar atomic_store_rel_ptr(loc, (uintptr_t)h);
3804535e804SNavdeep Parhar }
381671bf2b8SNavdeep Parhar
3824535e804SNavdeep Parhar void
t4_register_cpl_handler(int opcode,cpl_handler_t h)3834535e804SNavdeep Parhar t4_register_cpl_handler(int opcode, cpl_handler_t h)
3844535e804SNavdeep Parhar {
3854535e804SNavdeep Parhar uintptr_t *loc;
3864535e804SNavdeep Parhar
3874535e804SNavdeep Parhar MPASS(opcode < nitems(t4_cpl_handler));
3884535e804SNavdeep Parhar MPASS(h == NULL || t4_cpl_handler[opcode] == NULL);
3894535e804SNavdeep Parhar
3904535e804SNavdeep Parhar loc = (uintptr_t *)&t4_cpl_handler[opcode];
3914535e804SNavdeep Parhar atomic_store_rel_ptr(loc, (uintptr_t)h);
392671bf2b8SNavdeep Parhar }
393671bf2b8SNavdeep Parhar
394671bf2b8SNavdeep Parhar static int
set_tcb_rpl_handler(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)3954535e804SNavdeep Parhar set_tcb_rpl_handler(struct sge_iq *iq, const struct rss_header *rss,
3964535e804SNavdeep Parhar struct mbuf *m)
397671bf2b8SNavdeep Parhar {
3984535e804SNavdeep Parhar const struct cpl_set_tcb_rpl *cpl = (const void *)(rss + 1);
3994535e804SNavdeep Parhar u_int tid;
4004535e804SNavdeep Parhar int cookie;
401671bf2b8SNavdeep Parhar
4024535e804SNavdeep Parhar MPASS(m == NULL);
4034535e804SNavdeep Parhar
4044535e804SNavdeep Parhar tid = GET_TID(cpl);
4055fc0f72fSNavdeep Parhar if (is_hpftid(iq->adapter, tid) || is_ftid(iq->adapter, tid)) {
4064535e804SNavdeep Parhar /*
4074535e804SNavdeep Parhar * The return code for filter-write is put in the CPL cookie so
4084535e804SNavdeep Parhar * we have to rely on the hardware tid (is_ftid) to determine
4094535e804SNavdeep Parhar * that this is a response to a filter.
4104535e804SNavdeep Parhar */
4114535e804SNavdeep Parhar cookie = CPL_COOKIE_FILTER;
4124535e804SNavdeep Parhar } else {
4134535e804SNavdeep Parhar cookie = G_COOKIE(cpl->cookie);
4144535e804SNavdeep Parhar }
4154535e804SNavdeep Parhar MPASS(cookie > CPL_COOKIE_RESERVED);
4164535e804SNavdeep Parhar MPASS(cookie < nitems(set_tcb_rpl_handlers));
4174535e804SNavdeep Parhar
4184535e804SNavdeep Parhar return (set_tcb_rpl_handlers[cookie](iq, rss, m));
419671bf2b8SNavdeep Parhar }
420671bf2b8SNavdeep Parhar
4214535e804SNavdeep Parhar static int
l2t_write_rpl_handler(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)4224535e804SNavdeep Parhar l2t_write_rpl_handler(struct sge_iq *iq, const struct rss_header *rss,
4234535e804SNavdeep Parhar struct mbuf *m)
424671bf2b8SNavdeep Parhar {
4254535e804SNavdeep Parhar const struct cpl_l2t_write_rpl *rpl = (const void *)(rss + 1);
4264535e804SNavdeep Parhar unsigned int cookie;
427671bf2b8SNavdeep Parhar
4284535e804SNavdeep Parhar MPASS(m == NULL);
429671bf2b8SNavdeep Parhar
4304535e804SNavdeep Parhar cookie = GET_TID(rpl) & F_SYNC_WR ? CPL_COOKIE_TOM : CPL_COOKIE_FILTER;
4314535e804SNavdeep Parhar return (l2t_write_rpl_handlers[cookie](iq, rss, m));
4324535e804SNavdeep Parhar }
433671bf2b8SNavdeep Parhar
434111638bfSNavdeep Parhar static int
act_open_rpl_handler(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)435111638bfSNavdeep Parhar act_open_rpl_handler(struct sge_iq *iq, const struct rss_header *rss,
436111638bfSNavdeep Parhar struct mbuf *m)
437111638bfSNavdeep Parhar {
438111638bfSNavdeep Parhar const struct cpl_act_open_rpl *cpl = (const void *)(rss + 1);
439111638bfSNavdeep Parhar u_int cookie = G_TID_COOKIE(G_AOPEN_ATID(be32toh(cpl->atid_status)));
440111638bfSNavdeep Parhar
441111638bfSNavdeep Parhar MPASS(m == NULL);
442111638bfSNavdeep Parhar MPASS(cookie != CPL_COOKIE_RESERVED);
443111638bfSNavdeep Parhar
444111638bfSNavdeep Parhar return (act_open_rpl_handlers[cookie](iq, rss, m));
445111638bfSNavdeep Parhar }
446111638bfSNavdeep Parhar
44789f651e7SNavdeep Parhar static int
abort_rpl_rss_handler(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)44889f651e7SNavdeep Parhar abort_rpl_rss_handler(struct sge_iq *iq, const struct rss_header *rss,
44989f651e7SNavdeep Parhar struct mbuf *m)
45089f651e7SNavdeep Parhar {
45189f651e7SNavdeep Parhar struct adapter *sc = iq->adapter;
45289f651e7SNavdeep Parhar u_int cookie;
45389f651e7SNavdeep Parhar
45489f651e7SNavdeep Parhar MPASS(m == NULL);
45589f651e7SNavdeep Parhar if (is_hashfilter(sc))
45689f651e7SNavdeep Parhar cookie = CPL_COOKIE_HASHFILTER;
45789f651e7SNavdeep Parhar else
45889f651e7SNavdeep Parhar cookie = CPL_COOKIE_TOM;
45989f651e7SNavdeep Parhar
46089f651e7SNavdeep Parhar return (abort_rpl_rss_handlers[cookie](iq, rss, m));
46189f651e7SNavdeep Parhar }
46289f651e7SNavdeep Parhar
4639c707b32SNavdeep Parhar static int
fw4_ack_handler(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)4649c707b32SNavdeep Parhar fw4_ack_handler(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
4659c707b32SNavdeep Parhar {
4669c707b32SNavdeep Parhar struct adapter *sc = iq->adapter;
4679c707b32SNavdeep Parhar const struct cpl_fw4_ack *cpl = (const void *)(rss + 1);
4689c707b32SNavdeep Parhar unsigned int tid = G_CPL_FW4_ACK_FLOWID(be32toh(OPCODE_TID(cpl)));
4699c707b32SNavdeep Parhar u_int cookie;
4709c707b32SNavdeep Parhar
4719c707b32SNavdeep Parhar MPASS(m == NULL);
4729c707b32SNavdeep Parhar if (is_etid(sc, tid))
4739c707b32SNavdeep Parhar cookie = CPL_COOKIE_ETHOFLD;
4749c707b32SNavdeep Parhar else
4759c707b32SNavdeep Parhar cookie = CPL_COOKIE_TOM;
4769c707b32SNavdeep Parhar
4779c707b32SNavdeep Parhar return (fw4_ack_handlers[cookie](iq, rss, m));
4789c707b32SNavdeep Parhar }
4799c707b32SNavdeep Parhar
4804535e804SNavdeep Parhar static void
t4_init_shared_cpl_handlers(void)4814535e804SNavdeep Parhar t4_init_shared_cpl_handlers(void)
4824535e804SNavdeep Parhar {
4834535e804SNavdeep Parhar
4844535e804SNavdeep Parhar t4_register_cpl_handler(CPL_SET_TCB_RPL, set_tcb_rpl_handler);
4854535e804SNavdeep Parhar t4_register_cpl_handler(CPL_L2T_WRITE_RPL, l2t_write_rpl_handler);
486111638bfSNavdeep Parhar t4_register_cpl_handler(CPL_ACT_OPEN_RPL, act_open_rpl_handler);
48789f651e7SNavdeep Parhar t4_register_cpl_handler(CPL_ABORT_RPL_RSS, abort_rpl_rss_handler);
4889c707b32SNavdeep Parhar t4_register_cpl_handler(CPL_FW4_ACK, fw4_ack_handler);
4894535e804SNavdeep Parhar }
4904535e804SNavdeep Parhar
4914535e804SNavdeep Parhar void
t4_register_shared_cpl_handler(int opcode,cpl_handler_t h,int cookie)4924535e804SNavdeep Parhar t4_register_shared_cpl_handler(int opcode, cpl_handler_t h, int cookie)
4934535e804SNavdeep Parhar {
4944535e804SNavdeep Parhar uintptr_t *loc;
4954535e804SNavdeep Parhar
4964535e804SNavdeep Parhar MPASS(opcode < nitems(t4_cpl_handler));
4974535e804SNavdeep Parhar MPASS(cookie > CPL_COOKIE_RESERVED);
4984535e804SNavdeep Parhar MPASS(cookie < NUM_CPL_COOKIES);
4994535e804SNavdeep Parhar MPASS(t4_cpl_handler[opcode] != NULL);
5004535e804SNavdeep Parhar
5014535e804SNavdeep Parhar switch (opcode) {
5024535e804SNavdeep Parhar case CPL_SET_TCB_RPL:
5034535e804SNavdeep Parhar loc = (uintptr_t *)&set_tcb_rpl_handlers[cookie];
5044535e804SNavdeep Parhar break;
5054535e804SNavdeep Parhar case CPL_L2T_WRITE_RPL:
5064535e804SNavdeep Parhar loc = (uintptr_t *)&l2t_write_rpl_handlers[cookie];
5074535e804SNavdeep Parhar break;
508111638bfSNavdeep Parhar case CPL_ACT_OPEN_RPL:
509111638bfSNavdeep Parhar loc = (uintptr_t *)&act_open_rpl_handlers[cookie];
510111638bfSNavdeep Parhar break;
51189f651e7SNavdeep Parhar case CPL_ABORT_RPL_RSS:
51289f651e7SNavdeep Parhar loc = (uintptr_t *)&abort_rpl_rss_handlers[cookie];
51389f651e7SNavdeep Parhar break;
5149c707b32SNavdeep Parhar case CPL_FW4_ACK:
5159c707b32SNavdeep Parhar loc = (uintptr_t *)&fw4_ack_handlers[cookie];
5169c707b32SNavdeep Parhar break;
5174535e804SNavdeep Parhar default:
5184535e804SNavdeep Parhar MPASS(0);
5194535e804SNavdeep Parhar return;
5204535e804SNavdeep Parhar }
5214535e804SNavdeep Parhar MPASS(h == NULL || *loc == (uintptr_t)NULL);
5224535e804SNavdeep Parhar atomic_store_rel_ptr(loc, (uintptr_t)h);
523671bf2b8SNavdeep Parhar }
524671bf2b8SNavdeep Parhar
52594586193SNavdeep Parhar /*
5261458bff9SNavdeep Parhar * Called on MOD_LOAD. Validates and calculates the SGE tunables.
52794586193SNavdeep Parhar */
52894586193SNavdeep Parhar void
t4_sge_modload(void)52994586193SNavdeep Parhar t4_sge_modload(void)
53094586193SNavdeep Parhar {
5314defc81bSNavdeep Parhar
5329fb8886bSNavdeep Parhar if (fl_pktshift < 0 || fl_pktshift > 7) {
5339fb8886bSNavdeep Parhar printf("Invalid hw.cxgbe.fl_pktshift value (%d),"
534518bca2cSNavdeep Parhar " using 0 instead.\n", fl_pktshift);
535518bca2cSNavdeep Parhar fl_pktshift = 0;
5369fb8886bSNavdeep Parhar }
5379fb8886bSNavdeep Parhar
5389fb8886bSNavdeep Parhar if (spg_len != 64 && spg_len != 128) {
5399fb8886bSNavdeep Parhar int len;
5409fb8886bSNavdeep Parhar
5419fb8886bSNavdeep Parhar #if defined(__i386__) || defined(__amd64__)
5429fb8886bSNavdeep Parhar len = cpu_clflush_line_size > 64 ? 128 : 64;
5439fb8886bSNavdeep Parhar #else
5449fb8886bSNavdeep Parhar len = 64;
5459fb8886bSNavdeep Parhar #endif
5469fb8886bSNavdeep Parhar if (spg_len != -1) {
5479fb8886bSNavdeep Parhar printf("Invalid hw.cxgbe.spg_len value (%d),"
5489fb8886bSNavdeep Parhar " using %d instead.\n", spg_len, len);
5499fb8886bSNavdeep Parhar }
5509fb8886bSNavdeep Parhar spg_len = len;
5519fb8886bSNavdeep Parhar }
5529fb8886bSNavdeep Parhar
553df275ae5SNavdeep Parhar if (cong_drop < -1 || cong_drop > 2) {
5549fb8886bSNavdeep Parhar printf("Invalid hw.cxgbe.cong_drop value (%d),"
5559fb8886bSNavdeep Parhar " using 0 instead.\n", cong_drop);
5569fb8886bSNavdeep Parhar cong_drop = 0;
5579fb8886bSNavdeep Parhar }
558998eb37aSNavdeep Parhar #ifdef TCP_OFFLOAD
559998eb37aSNavdeep Parhar if (ofld_cong_drop < -1 || ofld_cong_drop > 2) {
560998eb37aSNavdeep Parhar printf("Invalid hw.cxgbe.ofld_cong_drop value (%d),"
561998eb37aSNavdeep Parhar " using 0 instead.\n", ofld_cong_drop);
562998eb37aSNavdeep Parhar ofld_cong_drop = 0;
563998eb37aSNavdeep Parhar }
564998eb37aSNavdeep Parhar #endif
56582eff304SNavdeep Parhar
566d491f8caSNavdeep Parhar if (tscale != 1 && (tscale < 3 || tscale > 17)) {
567d491f8caSNavdeep Parhar printf("Invalid hw.cxgbe.tscale value (%d),"
568d491f8caSNavdeep Parhar " using 1 instead.\n", tscale);
569d491f8caSNavdeep Parhar tscale = 1;
570d491f8caSNavdeep Parhar }
571d491f8caSNavdeep Parhar
5727676c62aSNavdeep Parhar if (largest_rx_cluster != MCLBYTES &&
5737676c62aSNavdeep Parhar largest_rx_cluster != MJUMPAGESIZE &&
5747676c62aSNavdeep Parhar largest_rx_cluster != MJUM9BYTES &&
5757676c62aSNavdeep Parhar largest_rx_cluster != MJUM16BYTES) {
5767676c62aSNavdeep Parhar printf("Invalid hw.cxgbe.largest_rx_cluster value (%d),"
5777676c62aSNavdeep Parhar " using %d instead.\n", largest_rx_cluster, MJUM16BYTES);
5787676c62aSNavdeep Parhar largest_rx_cluster = MJUM16BYTES;
5797676c62aSNavdeep Parhar }
5807676c62aSNavdeep Parhar
5817676c62aSNavdeep Parhar if (safest_rx_cluster != MCLBYTES &&
5827676c62aSNavdeep Parhar safest_rx_cluster != MJUMPAGESIZE &&
5837676c62aSNavdeep Parhar safest_rx_cluster != MJUM9BYTES &&
5847676c62aSNavdeep Parhar safest_rx_cluster != MJUM16BYTES) {
5857676c62aSNavdeep Parhar printf("Invalid hw.cxgbe.safest_rx_cluster value (%d),"
5867676c62aSNavdeep Parhar " using %d instead.\n", safest_rx_cluster, MJUMPAGESIZE);
5877676c62aSNavdeep Parhar safest_rx_cluster = MJUMPAGESIZE;
5887676c62aSNavdeep Parhar }
5897676c62aSNavdeep Parhar
59082eff304SNavdeep Parhar extfree_refs = counter_u64_alloc(M_WAITOK);
59182eff304SNavdeep Parhar extfree_rels = counter_u64_alloc(M_WAITOK);
5927054f6ecSNavdeep Parhar pullups = counter_u64_alloc(M_WAITOK);
5937054f6ecSNavdeep Parhar defrags = counter_u64_alloc(M_WAITOK);
59482eff304SNavdeep Parhar counter_u64_zero(extfree_refs);
59582eff304SNavdeep Parhar counter_u64_zero(extfree_rels);
5967054f6ecSNavdeep Parhar counter_u64_zero(pullups);
5977054f6ecSNavdeep Parhar counter_u64_zero(defrags);
598671bf2b8SNavdeep Parhar
5994535e804SNavdeep Parhar t4_init_shared_cpl_handlers();
600671bf2b8SNavdeep Parhar t4_register_cpl_handler(CPL_FW4_MSG, handle_fw_msg);
601671bf2b8SNavdeep Parhar t4_register_cpl_handler(CPL_FW6_MSG, handle_fw_msg);
602671bf2b8SNavdeep Parhar t4_register_cpl_handler(CPL_SGE_EGR_UPDATE, handle_sge_egr_update);
603786099deSNavdeep Parhar #ifdef RATELIMIT
604786099deSNavdeep Parhar t4_register_shared_cpl_handler(CPL_FW4_ACK, ethofld_fw4_ack,
605786099deSNavdeep Parhar CPL_COOKIE_ETHOFLD);
606786099deSNavdeep Parhar #endif
607671bf2b8SNavdeep Parhar t4_register_fw_msg_handler(FW6_TYPE_CMD_RPL, t4_handle_fw_rpl);
608069af0ebSJohn Baldwin t4_register_fw_msg_handler(FW6_TYPE_WRERR_RPL, t4_handle_wrerr_rpl);
60982eff304SNavdeep Parhar }
61082eff304SNavdeep Parhar
61182eff304SNavdeep Parhar void
t4_sge_modunload(void)61282eff304SNavdeep Parhar t4_sge_modunload(void)
61382eff304SNavdeep Parhar {
61482eff304SNavdeep Parhar
61582eff304SNavdeep Parhar counter_u64_free(extfree_refs);
61682eff304SNavdeep Parhar counter_u64_free(extfree_rels);
6177054f6ecSNavdeep Parhar counter_u64_free(pullups);
6187054f6ecSNavdeep Parhar counter_u64_free(defrags);
61982eff304SNavdeep Parhar }
62082eff304SNavdeep Parhar
62182eff304SNavdeep Parhar uint64_t
t4_sge_extfree_refs(void)62282eff304SNavdeep Parhar t4_sge_extfree_refs(void)
62382eff304SNavdeep Parhar {
62482eff304SNavdeep Parhar uint64_t refs, rels;
62582eff304SNavdeep Parhar
62682eff304SNavdeep Parhar rels = counter_u64_fetch(extfree_rels);
62782eff304SNavdeep Parhar refs = counter_u64_fetch(extfree_refs);
62882eff304SNavdeep Parhar
62982eff304SNavdeep Parhar return (refs - rels);
63094586193SNavdeep Parhar }
63194586193SNavdeep Parhar
63244c6fea8SNavdeep Parhar /* max 4096 */
63344c6fea8SNavdeep Parhar #define MAX_PACK_BOUNDARY 512
63444c6fea8SNavdeep Parhar
635e3207e19SNavdeep Parhar static inline void
setup_pad_and_pack_boundaries(struct adapter * sc)636e3207e19SNavdeep Parhar setup_pad_and_pack_boundaries(struct adapter *sc)
637e3207e19SNavdeep Parhar {
638e3207e19SNavdeep Parhar uint32_t v, m;
6390dbc6cfdSNavdeep Parhar int pad, pack, pad_shift;
640e3207e19SNavdeep Parhar
6410dbc6cfdSNavdeep Parhar pad_shift = chip_id(sc) > CHELSIO_T5 ? X_T6_INGPADBOUNDARY_SHIFT :
6420dbc6cfdSNavdeep Parhar X_INGPADBOUNDARY_SHIFT;
643e3207e19SNavdeep Parhar pad = fl_pad;
6440dbc6cfdSNavdeep Parhar if (fl_pad < (1 << pad_shift) ||
6450dbc6cfdSNavdeep Parhar fl_pad > (1 << (pad_shift + M_INGPADBOUNDARY)) ||
6460dbc6cfdSNavdeep Parhar !powerof2(fl_pad)) {
647e3207e19SNavdeep Parhar /*
648e3207e19SNavdeep Parhar * If there is any chance that we might use buffer packing and
649e3207e19SNavdeep Parhar * the chip is a T4, then pick 64 as the pad/pack boundary. Set
6500dbc6cfdSNavdeep Parhar * it to the minimum allowed in all other cases.
651e3207e19SNavdeep Parhar */
6520dbc6cfdSNavdeep Parhar pad = is_t4(sc) && buffer_packing ? 64 : 1 << pad_shift;
653e3207e19SNavdeep Parhar
654e3207e19SNavdeep Parhar /*
655e3207e19SNavdeep Parhar * For fl_pad = 0 we'll still write a reasonable value to the
656e3207e19SNavdeep Parhar * register but all the freelists will opt out of padding.
657e3207e19SNavdeep Parhar * We'll complain here only if the user tried to set it to a
658e3207e19SNavdeep Parhar * value greater than 0 that was invalid.
659e3207e19SNavdeep Parhar */
660e3207e19SNavdeep Parhar if (fl_pad > 0) {
661e3207e19SNavdeep Parhar device_printf(sc->dev, "Invalid hw.cxgbe.fl_pad value"
662e3207e19SNavdeep Parhar " (%d), using %d instead.\n", fl_pad, pad);
663e3207e19SNavdeep Parhar }
664e3207e19SNavdeep Parhar }
665e3207e19SNavdeep Parhar m = V_INGPADBOUNDARY(M_INGPADBOUNDARY);
6660dbc6cfdSNavdeep Parhar v = V_INGPADBOUNDARY(ilog2(pad) - pad_shift);
667e3207e19SNavdeep Parhar t4_set_reg_field(sc, A_SGE_CONTROL, m, v);
668e3207e19SNavdeep Parhar
669e3207e19SNavdeep Parhar if (is_t4(sc)) {
670e3207e19SNavdeep Parhar if (fl_pack != -1 && fl_pack != pad) {
671e3207e19SNavdeep Parhar /* Complain but carry on. */
672e3207e19SNavdeep Parhar device_printf(sc->dev, "hw.cxgbe.fl_pack (%d) ignored,"
673e3207e19SNavdeep Parhar " using %d instead.\n", fl_pack, pad);
674e3207e19SNavdeep Parhar }
675e3207e19SNavdeep Parhar return;
676e3207e19SNavdeep Parhar }
677e3207e19SNavdeep Parhar
678e3207e19SNavdeep Parhar pack = fl_pack;
679e3207e19SNavdeep Parhar if (fl_pack < 16 || fl_pack == 32 || fl_pack > 4096 ||
680e3207e19SNavdeep Parhar !powerof2(fl_pack)) {
68144c6fea8SNavdeep Parhar if (sc->params.pci.mps > MAX_PACK_BOUNDARY)
68244c6fea8SNavdeep Parhar pack = MAX_PACK_BOUNDARY;
68344c6fea8SNavdeep Parhar else
684e3207e19SNavdeep Parhar pack = max(sc->params.pci.mps, CACHE_LINE_SIZE);
685e3207e19SNavdeep Parhar MPASS(powerof2(pack));
686e3207e19SNavdeep Parhar if (pack < 16)
687e3207e19SNavdeep Parhar pack = 16;
688e3207e19SNavdeep Parhar if (pack == 32)
689e3207e19SNavdeep Parhar pack = 64;
690e3207e19SNavdeep Parhar if (pack > 4096)
691e3207e19SNavdeep Parhar pack = 4096;
692e3207e19SNavdeep Parhar if (fl_pack != -1) {
693e3207e19SNavdeep Parhar device_printf(sc->dev, "Invalid hw.cxgbe.fl_pack value"
694e3207e19SNavdeep Parhar " (%d), using %d instead.\n", fl_pack, pack);
695e3207e19SNavdeep Parhar }
696e3207e19SNavdeep Parhar }
697e3207e19SNavdeep Parhar m = V_INGPACKBOUNDARY(M_INGPACKBOUNDARY);
698e3207e19SNavdeep Parhar if (pack == 16)
699e3207e19SNavdeep Parhar v = V_INGPACKBOUNDARY(0);
700e3207e19SNavdeep Parhar else
701e3207e19SNavdeep Parhar v = V_INGPACKBOUNDARY(ilog2(pack) - 5);
702e3207e19SNavdeep Parhar
703e3207e19SNavdeep Parhar MPASS(!is_t4(sc)); /* T4 doesn't have SGE_CONTROL2 */
704e3207e19SNavdeep Parhar t4_set_reg_field(sc, A_SGE_CONTROL2, m, v);
705e3207e19SNavdeep Parhar }
706e3207e19SNavdeep Parhar
707cf738022SNavdeep Parhar /*
708cf738022SNavdeep Parhar * adap->params.vpd.cclk must be set up before this is called.
709cf738022SNavdeep Parhar */
710d14b0ac1SNavdeep Parhar void
t4_tweak_chip_settings(struct adapter * sc)711d14b0ac1SNavdeep Parhar t4_tweak_chip_settings(struct adapter *sc)
712d14b0ac1SNavdeep Parhar {
71346e1e307SNavdeep Parhar int i, reg;
714d14b0ac1SNavdeep Parhar uint32_t v, m;
715d14b0ac1SNavdeep Parhar int intr_timer[SGE_NTIMERS] = {1, 5, 10, 50, 100, 200};
716cf738022SNavdeep Parhar int timer_max = M_TIMERVALUE0 * 1000 / sc->params.vpd.cclk;
717d14b0ac1SNavdeep Parhar int intr_pktcount[SGE_NCOUNTERS] = {1, 8, 16, 32}; /* 63 max */
718d14b0ac1SNavdeep Parhar uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE);
71946e1e307SNavdeep Parhar static int sw_buf_sizes[] = {
7201458bff9SNavdeep Parhar MCLBYTES,
7211458bff9SNavdeep Parhar MJUMPAGESIZE,
7221458bff9SNavdeep Parhar MJUM9BYTES,
72346e1e307SNavdeep Parhar MJUM16BYTES
7241458bff9SNavdeep Parhar };
725d14b0ac1SNavdeep Parhar
726d14b0ac1SNavdeep Parhar KASSERT(sc->flags & MASTER_PF,
727d14b0ac1SNavdeep Parhar ("%s: trying to change chip settings when not master.", __func__));
728d14b0ac1SNavdeep Parhar
7291458bff9SNavdeep Parhar m = V_PKTSHIFT(M_PKTSHIFT) | F_RXPKTCPLMODE | F_EGRSTATUSPAGESIZE;
730d14b0ac1SNavdeep Parhar v = V_PKTSHIFT(fl_pktshift) | F_RXPKTCPLMODE |
7314defc81bSNavdeep Parhar V_EGRSTATUSPAGESIZE(spg_len == 128);
732d14b0ac1SNavdeep Parhar t4_set_reg_field(sc, A_SGE_CONTROL, m, v);
73354e4ee71SNavdeep Parhar
734e3207e19SNavdeep Parhar setup_pad_and_pack_boundaries(sc);
7351458bff9SNavdeep Parhar
736d14b0ac1SNavdeep Parhar v = V_HOSTPAGESIZEPF0(PAGE_SHIFT - 10) |
737733b9277SNavdeep Parhar V_HOSTPAGESIZEPF1(PAGE_SHIFT - 10) |
738733b9277SNavdeep Parhar V_HOSTPAGESIZEPF2(PAGE_SHIFT - 10) |
739733b9277SNavdeep Parhar V_HOSTPAGESIZEPF3(PAGE_SHIFT - 10) |
740733b9277SNavdeep Parhar V_HOSTPAGESIZEPF4(PAGE_SHIFT - 10) |
741733b9277SNavdeep Parhar V_HOSTPAGESIZEPF5(PAGE_SHIFT - 10) |
742733b9277SNavdeep Parhar V_HOSTPAGESIZEPF6(PAGE_SHIFT - 10) |
743733b9277SNavdeep Parhar V_HOSTPAGESIZEPF7(PAGE_SHIFT - 10);
744d14b0ac1SNavdeep Parhar t4_write_reg(sc, A_SGE_HOST_PAGE_SIZE, v);
745733b9277SNavdeep Parhar
7469b11a65dSNavdeep Parhar t4_write_reg(sc, A_SGE_FL_BUFFER_SIZE0, 4096);
7479b11a65dSNavdeep Parhar t4_write_reg(sc, A_SGE_FL_BUFFER_SIZE1, 65536);
74846e1e307SNavdeep Parhar reg = A_SGE_FL_BUFFER_SIZE2;
74946e1e307SNavdeep Parhar for (i = 0; i < nitems(sw_buf_sizes); i++) {
75046e1e307SNavdeep Parhar MPASS(reg <= A_SGE_FL_BUFFER_SIZE15);
75146e1e307SNavdeep Parhar t4_write_reg(sc, reg, sw_buf_sizes[i]);
75246e1e307SNavdeep Parhar reg += 4;
75346e1e307SNavdeep Parhar MPASS(reg <= A_SGE_FL_BUFFER_SIZE15);
75446e1e307SNavdeep Parhar t4_write_reg(sc, reg, sw_buf_sizes[i] - CL_METADATA_SIZE);
75546e1e307SNavdeep Parhar reg += 4;
75654e4ee71SNavdeep Parhar }
75754e4ee71SNavdeep Parhar
758d14b0ac1SNavdeep Parhar v = V_THRESHOLD_0(intr_pktcount[0]) | V_THRESHOLD_1(intr_pktcount[1]) |
759d14b0ac1SNavdeep Parhar V_THRESHOLD_2(intr_pktcount[2]) | V_THRESHOLD_3(intr_pktcount[3]);
760d14b0ac1SNavdeep Parhar t4_write_reg(sc, A_SGE_INGRESS_RX_THRESHOLD, v);
76154e4ee71SNavdeep Parhar
762cf738022SNavdeep Parhar KASSERT(intr_timer[0] <= timer_max,
763cf738022SNavdeep Parhar ("%s: not a single usable timer (%d, %d)", __func__, intr_timer[0],
764cf738022SNavdeep Parhar timer_max));
765cf738022SNavdeep Parhar for (i = 1; i < nitems(intr_timer); i++) {
766cf738022SNavdeep Parhar KASSERT(intr_timer[i] >= intr_timer[i - 1],
767cf738022SNavdeep Parhar ("%s: timers not listed in increasing order (%d)",
768cf738022SNavdeep Parhar __func__, i));
769cf738022SNavdeep Parhar
770cf738022SNavdeep Parhar while (intr_timer[i] > timer_max) {
771cf738022SNavdeep Parhar if (i == nitems(intr_timer) - 1) {
772cf738022SNavdeep Parhar intr_timer[i] = timer_max;
773cf738022SNavdeep Parhar break;
774cf738022SNavdeep Parhar }
775cf738022SNavdeep Parhar intr_timer[i] += intr_timer[i - 1];
776cf738022SNavdeep Parhar intr_timer[i] /= 2;
777cf738022SNavdeep Parhar }
778cf738022SNavdeep Parhar }
779cf738022SNavdeep Parhar
780d14b0ac1SNavdeep Parhar v = V_TIMERVALUE0(us_to_core_ticks(sc, intr_timer[0])) |
781d14b0ac1SNavdeep Parhar V_TIMERVALUE1(us_to_core_ticks(sc, intr_timer[1]));
782d14b0ac1SNavdeep Parhar t4_write_reg(sc, A_SGE_TIMER_VALUE_0_AND_1, v);
783d14b0ac1SNavdeep Parhar v = V_TIMERVALUE2(us_to_core_ticks(sc, intr_timer[2])) |
784d14b0ac1SNavdeep Parhar V_TIMERVALUE3(us_to_core_ticks(sc, intr_timer[3]));
785d14b0ac1SNavdeep Parhar t4_write_reg(sc, A_SGE_TIMER_VALUE_2_AND_3, v);
786d14b0ac1SNavdeep Parhar v = V_TIMERVALUE4(us_to_core_ticks(sc, intr_timer[4])) |
787d14b0ac1SNavdeep Parhar V_TIMERVALUE5(us_to_core_ticks(sc, intr_timer[5]));
788d14b0ac1SNavdeep Parhar t4_write_reg(sc, A_SGE_TIMER_VALUE_4_AND_5, v);
78986e02bf2SNavdeep Parhar
790d491f8caSNavdeep Parhar if (chip_id(sc) >= CHELSIO_T6) {
791d491f8caSNavdeep Parhar m = V_TSCALE(M_TSCALE);
792d491f8caSNavdeep Parhar if (tscale == 1)
793d491f8caSNavdeep Parhar v = 0;
794d491f8caSNavdeep Parhar else
795d491f8caSNavdeep Parhar v = V_TSCALE(tscale - 2);
796d491f8caSNavdeep Parhar t4_set_reg_field(sc, A_SGE_ITP_CONTROL, m, v);
7972f318252SNavdeep Parhar
7982f318252SNavdeep Parhar if (sc->debug_flags & DF_DISABLE_TCB_CACHE) {
7992f318252SNavdeep Parhar m = V_RDTHRESHOLD(M_RDTHRESHOLD) | F_WRTHRTHRESHEN |
8002f318252SNavdeep Parhar V_WRTHRTHRESH(M_WRTHRTHRESH);
8012f318252SNavdeep Parhar t4_tp_pio_read(sc, &v, 1, A_TP_CMM_CONFIG, 1);
8022f318252SNavdeep Parhar v &= ~m;
8032f318252SNavdeep Parhar v |= V_RDTHRESHOLD(1) | F_WRTHRTHRESHEN |
8042f318252SNavdeep Parhar V_WRTHRTHRESH(16);
8052f318252SNavdeep Parhar t4_tp_pio_write(sc, &v, 1, A_TP_CMM_CONFIG, 1);
8062f318252SNavdeep Parhar }
807d491f8caSNavdeep Parhar }
808d491f8caSNavdeep Parhar
8097cba15b1SNavdeep Parhar /* 4K, 16K, 64K, 256K DDP "page sizes" for TDDP */
810d14b0ac1SNavdeep Parhar v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6);
811d14b0ac1SNavdeep Parhar t4_write_reg(sc, A_ULP_RX_TDDP_PSZ, v);
812d14b0ac1SNavdeep Parhar
8137cba15b1SNavdeep Parhar /*
8147cba15b1SNavdeep Parhar * 4K, 8K, 16K, 64K DDP "page sizes" for iSCSI DDP. These have been
8157cba15b1SNavdeep Parhar * chosen with MAXPHYS = 128K in mind. The largest DDP buffer that we
8167cba15b1SNavdeep Parhar * may have to deal with is MAXPHYS + 1 page.
8177cba15b1SNavdeep Parhar */
8187cba15b1SNavdeep Parhar v = V_HPZ0(0) | V_HPZ1(1) | V_HPZ2(2) | V_HPZ3(4);
8197cba15b1SNavdeep Parhar t4_write_reg(sc, A_ULP_RX_ISCSI_PSZ, v);
8207cba15b1SNavdeep Parhar
8217cba15b1SNavdeep Parhar /* We use multiple DDP page sizes both in plain-TOE and ISCSI modes. */
8227cba15b1SNavdeep Parhar m = v = F_TDDPTAGTCB | F_ISCSITAGTCB;
823d14b0ac1SNavdeep Parhar t4_set_reg_field(sc, A_ULP_RX_CTL, m, v);
824d14b0ac1SNavdeep Parhar
825d14b0ac1SNavdeep Parhar m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET |
826d14b0ac1SNavdeep Parhar F_RESETDDPOFFSET;
827d14b0ac1SNavdeep Parhar v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET;
828d14b0ac1SNavdeep Parhar t4_set_reg_field(sc, A_TP_PARA_REG5, m, v);
829d14b0ac1SNavdeep Parhar }
830d14b0ac1SNavdeep Parhar
831d14b0ac1SNavdeep Parhar /*
83246e1e307SNavdeep Parhar * SGE wants the buffer to be at least 64B and then a multiple of 16. Its
83346e1e307SNavdeep Parhar * address mut be 16B aligned. If padding is in use the buffer's start and end
83446e1e307SNavdeep Parhar * need to be aligned to the pad boundary as well. We'll just make sure that
83546e1e307SNavdeep Parhar * the size is a multiple of the pad boundary here, it is up to the buffer
83646e1e307SNavdeep Parhar * allocation code to make sure the start of the buffer is aligned.
83738035ed6SNavdeep Parhar */
83838035ed6SNavdeep Parhar static inline int
hwsz_ok(struct adapter * sc,int hwsz)839e3207e19SNavdeep Parhar hwsz_ok(struct adapter *sc, int hwsz)
84038035ed6SNavdeep Parhar {
84190e7434aSNavdeep Parhar int mask = fl_pad ? sc->params.sge.pad_boundary - 1 : 16 - 1;
84238035ed6SNavdeep Parhar
843b741402cSNavdeep Parhar return (hwsz >= 64 && (hwsz & mask) == 0);
84438035ed6SNavdeep Parhar }
84538035ed6SNavdeep Parhar
84638035ed6SNavdeep Parhar /*
847fae028ddSNavdeep Parhar * Initialize the rx buffer sizes and figure out which zones the buffers will
848fae028ddSNavdeep Parhar * be allocated from.
849d14b0ac1SNavdeep Parhar */
850fae028ddSNavdeep Parhar void
t4_init_rx_buf_info(struct adapter * sc)851fae028ddSNavdeep Parhar t4_init_rx_buf_info(struct adapter *sc)
852d14b0ac1SNavdeep Parhar {
853d14b0ac1SNavdeep Parhar struct sge *s = &sc->sge;
85490e7434aSNavdeep Parhar struct sge_params *sp = &sc->params.sge;
855fae028ddSNavdeep Parhar int i, j, n;
85638035ed6SNavdeep Parhar static int sw_buf_sizes[] = { /* Sorted by size */
8571458bff9SNavdeep Parhar MCLBYTES,
8581458bff9SNavdeep Parhar MJUMPAGESIZE,
8591458bff9SNavdeep Parhar MJUM9BYTES,
8601458bff9SNavdeep Parhar MJUM16BYTES
8611458bff9SNavdeep Parhar };
86246e1e307SNavdeep Parhar struct rx_buf_info *rxb;
863d14b0ac1SNavdeep Parhar
86446e1e307SNavdeep Parhar s->safe_zidx = -1;
86546e1e307SNavdeep Parhar rxb = &s->rx_buf_info[0];
86646e1e307SNavdeep Parhar for (i = 0; i < SW_ZONE_SIZES; i++, rxb++) {
86746e1e307SNavdeep Parhar rxb->size1 = sw_buf_sizes[i];
86846e1e307SNavdeep Parhar rxb->zone = m_getzone(rxb->size1);
86946e1e307SNavdeep Parhar rxb->type = m_gettype(rxb->size1);
87046e1e307SNavdeep Parhar rxb->size2 = 0;
87146e1e307SNavdeep Parhar rxb->hwidx1 = -1;
87246e1e307SNavdeep Parhar rxb->hwidx2 = -1;
87346e1e307SNavdeep Parhar for (j = 0; j < SGE_FLBUF_SIZES; j++) {
87446e1e307SNavdeep Parhar int hwsize = sp->sge_fl_buffer_size[j];
87538035ed6SNavdeep Parhar
87646e1e307SNavdeep Parhar if (!hwsz_ok(sc, hwsize))
877e3207e19SNavdeep Parhar continue;
878e3207e19SNavdeep Parhar
87946e1e307SNavdeep Parhar /* hwidx for size1 */
88046e1e307SNavdeep Parhar if (rxb->hwidx1 == -1 && rxb->size1 == hwsize)
88146e1e307SNavdeep Parhar rxb->hwidx1 = j;
88238035ed6SNavdeep Parhar
88346e1e307SNavdeep Parhar /* hwidx for size2 (buffer packing) */
88446e1e307SNavdeep Parhar if (rxb->size1 - CL_METADATA_SIZE < hwsize)
8851458bff9SNavdeep Parhar continue;
88646e1e307SNavdeep Parhar n = rxb->size1 - hwsize - CL_METADATA_SIZE;
8871458bff9SNavdeep Parhar if (n == 0) {
88846e1e307SNavdeep Parhar rxb->hwidx2 = j;
88946e1e307SNavdeep Parhar rxb->size2 = hwsize;
89046e1e307SNavdeep Parhar break; /* stop looking */
891733b9277SNavdeep Parhar }
89246e1e307SNavdeep Parhar if (rxb->hwidx2 != -1) {
89346e1e307SNavdeep Parhar if (n < sp->sge_fl_buffer_size[rxb->hwidx2] -
89446e1e307SNavdeep Parhar hwsize - CL_METADATA_SIZE) {
89546e1e307SNavdeep Parhar rxb->hwidx2 = j;
89646e1e307SNavdeep Parhar rxb->size2 = hwsize;
89746e1e307SNavdeep Parhar }
89846e1e307SNavdeep Parhar } else if (n <= 2 * CL_METADATA_SIZE) {
89946e1e307SNavdeep Parhar rxb->hwidx2 = j;
90046e1e307SNavdeep Parhar rxb->size2 = hwsize;
90138035ed6SNavdeep Parhar }
90238035ed6SNavdeep Parhar }
90346e1e307SNavdeep Parhar if (rxb->hwidx2 != -1)
90446e1e307SNavdeep Parhar sc->flags |= BUF_PACKING_OK;
90546e1e307SNavdeep Parhar if (s->safe_zidx == -1 && rxb->size1 == safest_rx_cluster)
90646e1e307SNavdeep Parhar s->safe_zidx = i;
907e3207e19SNavdeep Parhar }
908fae028ddSNavdeep Parhar }
909fae028ddSNavdeep Parhar
910fae028ddSNavdeep Parhar /*
911fae028ddSNavdeep Parhar * Verify some basic SGE settings for the PF and VF driver, and other
912fae028ddSNavdeep Parhar * miscellaneous settings for the PF driver.
913fae028ddSNavdeep Parhar */
914fae028ddSNavdeep Parhar int
t4_verify_chip_settings(struct adapter * sc)915fae028ddSNavdeep Parhar t4_verify_chip_settings(struct adapter *sc)
916fae028ddSNavdeep Parhar {
917fae028ddSNavdeep Parhar struct sge_params *sp = &sc->params.sge;
918fae028ddSNavdeep Parhar uint32_t m, v, r;
919fae028ddSNavdeep Parhar int rc = 0;
920fae028ddSNavdeep Parhar const uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE);
921fae028ddSNavdeep Parhar
922fae028ddSNavdeep Parhar m = F_RXPKTCPLMODE;
923fae028ddSNavdeep Parhar v = F_RXPKTCPLMODE;
924fae028ddSNavdeep Parhar r = sp->sge_control;
925fae028ddSNavdeep Parhar if ((r & m) != v) {
926fae028ddSNavdeep Parhar device_printf(sc->dev, "invalid SGE_CONTROL(0x%x)\n", r);
927fae028ddSNavdeep Parhar rc = EINVAL;
928fae028ddSNavdeep Parhar }
929fae028ddSNavdeep Parhar
930fae028ddSNavdeep Parhar /*
931fae028ddSNavdeep Parhar * If this changes then every single use of PAGE_SHIFT in the driver
932fae028ddSNavdeep Parhar * needs to be carefully reviewed for PAGE_SHIFT vs sp->page_shift.
933fae028ddSNavdeep Parhar */
934fae028ddSNavdeep Parhar if (sp->page_shift != PAGE_SHIFT) {
935fae028ddSNavdeep Parhar device_printf(sc->dev, "invalid SGE_HOST_PAGE_SIZE(0x%x)\n", r);
936fae028ddSNavdeep Parhar rc = EINVAL;
937fae028ddSNavdeep Parhar }
938733b9277SNavdeep Parhar
9396af45170SJohn Baldwin if (sc->flags & IS_VF)
9406af45170SJohn Baldwin return (0);
9416af45170SJohn Baldwin
942d14b0ac1SNavdeep Parhar v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6);
943d14b0ac1SNavdeep Parhar r = t4_read_reg(sc, A_ULP_RX_TDDP_PSZ);
944d14b0ac1SNavdeep Parhar if (r != v) {
945d14b0ac1SNavdeep Parhar device_printf(sc->dev, "invalid ULP_RX_TDDP_PSZ(0x%x)\n", r);
946fae028ddSNavdeep Parhar if (sc->vres.ddp.size != 0)
947d14b0ac1SNavdeep Parhar rc = EINVAL;
948d14b0ac1SNavdeep Parhar }
949733b9277SNavdeep Parhar
950d14b0ac1SNavdeep Parhar m = v = F_TDDPTAGTCB;
951d14b0ac1SNavdeep Parhar r = t4_read_reg(sc, A_ULP_RX_CTL);
952d14b0ac1SNavdeep Parhar if ((r & m) != v) {
953d14b0ac1SNavdeep Parhar device_printf(sc->dev, "invalid ULP_RX_CTL(0x%x)\n", r);
954fae028ddSNavdeep Parhar if (sc->vres.ddp.size != 0)
955d14b0ac1SNavdeep Parhar rc = EINVAL;
956d14b0ac1SNavdeep Parhar }
957d14b0ac1SNavdeep Parhar
958d14b0ac1SNavdeep Parhar m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET |
959d14b0ac1SNavdeep Parhar F_RESETDDPOFFSET;
960d14b0ac1SNavdeep Parhar v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET;
961d14b0ac1SNavdeep Parhar r = t4_read_reg(sc, A_TP_PARA_REG5);
962d14b0ac1SNavdeep Parhar if ((r & m) != v) {
963d14b0ac1SNavdeep Parhar device_printf(sc->dev, "invalid TP_PARA_REG5(0x%x)\n", r);
964fae028ddSNavdeep Parhar if (sc->vres.ddp.size != 0)
965d14b0ac1SNavdeep Parhar rc = EINVAL;
966d14b0ac1SNavdeep Parhar }
967d14b0ac1SNavdeep Parhar
968733b9277SNavdeep Parhar return (rc);
96954e4ee71SNavdeep Parhar }
97054e4ee71SNavdeep Parhar
97154e4ee71SNavdeep Parhar int
t4_create_dma_tag(struct adapter * sc)97254e4ee71SNavdeep Parhar t4_create_dma_tag(struct adapter *sc)
97354e4ee71SNavdeep Parhar {
97454e4ee71SNavdeep Parhar int rc;
97554e4ee71SNavdeep Parhar
97654e4ee71SNavdeep Parhar rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
97754e4ee71SNavdeep Parhar BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE,
97854e4ee71SNavdeep Parhar BUS_SPACE_UNRESTRICTED, BUS_SPACE_MAXSIZE, BUS_DMA_ALLOCNOW, NULL,
97954e4ee71SNavdeep Parhar NULL, &sc->dmat);
98054e4ee71SNavdeep Parhar if (rc != 0) {
98154e4ee71SNavdeep Parhar device_printf(sc->dev,
98254e4ee71SNavdeep Parhar "failed to create main DMA tag: %d\n", rc);
98354e4ee71SNavdeep Parhar }
98454e4ee71SNavdeep Parhar
98554e4ee71SNavdeep Parhar return (rc);
98654e4ee71SNavdeep Parhar }
98754e4ee71SNavdeep Parhar
9886e22f9f3SNavdeep Parhar void
t4_sge_sysctls(struct adapter * sc,struct sysctl_ctx_list * ctx,struct sysctl_oid_list * children)9896e22f9f3SNavdeep Parhar t4_sge_sysctls(struct adapter *sc, struct sysctl_ctx_list *ctx,
9906e22f9f3SNavdeep Parhar struct sysctl_oid_list *children)
9916e22f9f3SNavdeep Parhar {
99290e7434aSNavdeep Parhar struct sge_params *sp = &sc->params.sge;
9936e22f9f3SNavdeep Parhar
99438035ed6SNavdeep Parhar SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "buffer_sizes",
9958741306bSNavdeep Parhar CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
9967029da5cSPawel Biernacki sysctl_bufsizes, "A", "freelist buffer sizes");
99738035ed6SNavdeep Parhar
9986e22f9f3SNavdeep Parhar SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pktshift", CTLFLAG_RD,
99990e7434aSNavdeep Parhar NULL, sp->fl_pktshift, "payload DMA offset in rx buffer (bytes)");
10006e22f9f3SNavdeep Parhar
10016e22f9f3SNavdeep Parhar SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pad", CTLFLAG_RD,
100290e7434aSNavdeep Parhar NULL, sp->pad_boundary, "payload pad boundary (bytes)");
10036e22f9f3SNavdeep Parhar
10046e22f9f3SNavdeep Parhar SYSCTL_ADD_INT(ctx, children, OID_AUTO, "spg_len", CTLFLAG_RD,
100590e7434aSNavdeep Parhar NULL, sp->spg_len, "status page size (bytes)");
10066e22f9f3SNavdeep Parhar
10076e22f9f3SNavdeep Parhar SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_drop", CTLFLAG_RD,
10086e22f9f3SNavdeep Parhar NULL, cong_drop, "congestion drop setting");
1009998eb37aSNavdeep Parhar #ifdef TCP_OFFLOAD
1010998eb37aSNavdeep Parhar SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ofld_cong_drop", CTLFLAG_RD,
1011998eb37aSNavdeep Parhar NULL, ofld_cong_drop, "congestion drop setting");
1012998eb37aSNavdeep Parhar #endif
10131458bff9SNavdeep Parhar
10141458bff9SNavdeep Parhar SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pack", CTLFLAG_RD,
101590e7434aSNavdeep Parhar NULL, sp->pack_boundary, "payload pack boundary (bytes)");
10166e22f9f3SNavdeep Parhar }
10176e22f9f3SNavdeep Parhar
101854e4ee71SNavdeep Parhar int
t4_destroy_dma_tag(struct adapter * sc)101954e4ee71SNavdeep Parhar t4_destroy_dma_tag(struct adapter *sc)
102054e4ee71SNavdeep Parhar {
102154e4ee71SNavdeep Parhar if (sc->dmat)
102254e4ee71SNavdeep Parhar bus_dma_tag_destroy(sc->dmat);
102354e4ee71SNavdeep Parhar
102454e4ee71SNavdeep Parhar return (0);
102554e4ee71SNavdeep Parhar }
102654e4ee71SNavdeep Parhar
102754e4ee71SNavdeep Parhar /*
102837310a98SNavdeep Parhar * Allocate and initialize the firmware event queue, control queues, and special
102937310a98SNavdeep Parhar * purpose rx queues owned by the adapter.
103054e4ee71SNavdeep Parhar *
103154e4ee71SNavdeep Parhar * Returns errno on failure. Resources allocated up to that point may still be
103254e4ee71SNavdeep Parhar * allocated. Caller is responsible for cleanup in case this function fails.
103354e4ee71SNavdeep Parhar */
103454e4ee71SNavdeep Parhar int
t4_setup_adapter_queues(struct adapter * sc)1035f7dfe243SNavdeep Parhar t4_setup_adapter_queues(struct adapter *sc)
103654e4ee71SNavdeep Parhar {
103737310a98SNavdeep Parhar int rc, i;
103854e4ee71SNavdeep Parhar
103954e4ee71SNavdeep Parhar ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
104054e4ee71SNavdeep Parhar
104156599263SNavdeep Parhar /*
104256599263SNavdeep Parhar * Firmware event queue
104356599263SNavdeep Parhar */
1044733b9277SNavdeep Parhar rc = alloc_fwq(sc);
1045aa95b653SNavdeep Parhar if (rc != 0)
1046f7dfe243SNavdeep Parhar return (rc);
1047f7dfe243SNavdeep Parhar
1048f7dfe243SNavdeep Parhar /*
104937310a98SNavdeep Parhar * That's all for the VF driver.
1050f7dfe243SNavdeep Parhar */
105137310a98SNavdeep Parhar if (sc->flags & IS_VF)
105237310a98SNavdeep Parhar return (rc);
105337310a98SNavdeep Parhar
105437310a98SNavdeep Parhar /*
105537310a98SNavdeep Parhar * XXX: General purpose rx queues, one per port.
105637310a98SNavdeep Parhar */
105737310a98SNavdeep Parhar
105837310a98SNavdeep Parhar /*
105937310a98SNavdeep Parhar * Control queues, one per port.
106037310a98SNavdeep Parhar */
106137310a98SNavdeep Parhar for_each_port(sc, i) {
106243bbae19SNavdeep Parhar rc = alloc_ctrlq(sc, i);
106337310a98SNavdeep Parhar if (rc != 0)
106437310a98SNavdeep Parhar return (rc);
106537310a98SNavdeep Parhar }
106654e4ee71SNavdeep Parhar
106754e4ee71SNavdeep Parhar return (rc);
106854e4ee71SNavdeep Parhar }
106954e4ee71SNavdeep Parhar
107054e4ee71SNavdeep Parhar /*
107154e4ee71SNavdeep Parhar * Idempotent
107254e4ee71SNavdeep Parhar */
107354e4ee71SNavdeep Parhar int
t4_teardown_adapter_queues(struct adapter * sc)1074f7dfe243SNavdeep Parhar t4_teardown_adapter_queues(struct adapter *sc)
107554e4ee71SNavdeep Parhar {
107637310a98SNavdeep Parhar int i;
107754e4ee71SNavdeep Parhar
107854e4ee71SNavdeep Parhar ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
107954e4ee71SNavdeep Parhar
1080b99651c5SNavdeep Parhar if (sc->sge.ctrlq != NULL) {
1081b99651c5SNavdeep Parhar MPASS(!(sc->flags & IS_VF)); /* VFs don't allocate ctrlq. */
108237310a98SNavdeep Parhar for_each_port(sc, i)
108343bbae19SNavdeep Parhar free_ctrlq(sc, i);
1084b8bfcb71SNavdeep Parhar }
1085733b9277SNavdeep Parhar free_fwq(sc);
108654e4ee71SNavdeep Parhar
108754e4ee71SNavdeep Parhar return (0);
108854e4ee71SNavdeep Parhar }
108954e4ee71SNavdeep Parhar
10906a59b994SNavdeep Parhar /* Maximum payload that could arrive with a single iq descriptor. */
10918340ece5SNavdeep Parhar static inline int
max_rx_payload(struct adapter * sc,if_t ifp,const bool ofld)1092954712e8SJustin Hibbits max_rx_payload(struct adapter *sc, if_t ifp, const bool ofld)
10938340ece5SNavdeep Parhar {
10946a59b994SNavdeep Parhar int maxp;
10958340ece5SNavdeep Parhar
109638035ed6SNavdeep Parhar /* large enough even when hw VLAN extraction is disabled */
10976a59b994SNavdeep Parhar maxp = sc->params.sge.fl_pktshift + ETHER_HDR_LEN +
1098954712e8SJustin Hibbits ETHER_VLAN_ENCAP_LEN + if_getmtu(ifp);
10996a59b994SNavdeep Parhar if (ofld && sc->tt.tls && sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS &&
11006a59b994SNavdeep Parhar maxp < sc->params.tp.max_rx_pdu)
11016a59b994SNavdeep Parhar maxp = sc->params.tp.max_rx_pdu;
11026a59b994SNavdeep Parhar return (maxp);
110338035ed6SNavdeep Parhar }
11046eb3180fSNavdeep Parhar
1105733b9277SNavdeep Parhar int
t4_setup_vi_queues(struct vi_info * vi)1106fe2ebb76SJohn Baldwin t4_setup_vi_queues(struct vi_info *vi)
1107733b9277SNavdeep Parhar {
110843bbae19SNavdeep Parhar int rc = 0, i, intr_idx;
1109733b9277SNavdeep Parhar struct sge_rxq *rxq;
1110733b9277SNavdeep Parhar struct sge_txq *txq;
111109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
1112733b9277SNavdeep Parhar struct sge_ofld_rxq *ofld_rxq;
1113eff62dbaSNavdeep Parhar #endif
1114eff62dbaSNavdeep Parhar #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1115077ba6a8SJohn Baldwin struct sge_ofld_txq *ofld_txq;
1116298d969cSNavdeep Parhar #endif
1117298d969cSNavdeep Parhar #ifdef DEV_NETMAP
111843bbae19SNavdeep Parhar int saved_idx, iqidx;
1119298d969cSNavdeep Parhar struct sge_nm_rxq *nm_rxq;
1120298d969cSNavdeep Parhar struct sge_nm_txq *nm_txq;
1121733b9277SNavdeep Parhar #endif
112243bbae19SNavdeep Parhar struct adapter *sc = vi->adapter;
1123954712e8SJustin Hibbits if_t ifp = vi->ifp;
11246a59b994SNavdeep Parhar int maxp;
1125733b9277SNavdeep Parhar
1126733b9277SNavdeep Parhar /* Interrupt vector to start from (when using multiple vectors) */
1127f549e352SNavdeep Parhar intr_idx = vi->first_intr;
1128fe2ebb76SJohn Baldwin
1129fe2ebb76SJohn Baldwin #ifdef DEV_NETMAP
113062291463SNavdeep Parhar saved_idx = intr_idx;
1131954712e8SJustin Hibbits if (if_getcapabilities(ifp) & IFCAP_NETMAP) {
113262291463SNavdeep Parhar
113362291463SNavdeep Parhar /* netmap is supported with direct interrupts only. */
1134f549e352SNavdeep Parhar MPASS(!forwarding_intr_to_fwq(sc));
113543bbae19SNavdeep Parhar MPASS(vi->first_intr >= 0);
113662291463SNavdeep Parhar
1137fe2ebb76SJohn Baldwin /*
1138fe2ebb76SJohn Baldwin * We don't have buffers to back the netmap rx queues
1139fe2ebb76SJohn Baldwin * right now so we create the queues in a way that
1140fe2ebb76SJohn Baldwin * doesn't set off any congestion signal in the chip.
1141fe2ebb76SJohn Baldwin */
1142fe2ebb76SJohn Baldwin for_each_nm_rxq(vi, i, nm_rxq) {
114343bbae19SNavdeep Parhar rc = alloc_nm_rxq(vi, nm_rxq, intr_idx, i);
1144fe2ebb76SJohn Baldwin if (rc != 0)
1145fe2ebb76SJohn Baldwin goto done;
1146fe2ebb76SJohn Baldwin intr_idx++;
1147fe2ebb76SJohn Baldwin }
1148fe2ebb76SJohn Baldwin
1149fe2ebb76SJohn Baldwin for_each_nm_txq(vi, i, nm_txq) {
1150f549e352SNavdeep Parhar iqidx = vi->first_nm_rxq + (i % vi->nnmrxq);
115143bbae19SNavdeep Parhar rc = alloc_nm_txq(vi, nm_txq, iqidx, i);
1152fe2ebb76SJohn Baldwin if (rc != 0)
1153fe2ebb76SJohn Baldwin goto done;
1154fe2ebb76SJohn Baldwin }
1155fe2ebb76SJohn Baldwin }
115662291463SNavdeep Parhar
115762291463SNavdeep Parhar /* Normal rx queues and netmap rx queues share the same interrupts. */
115862291463SNavdeep Parhar intr_idx = saved_idx;
1159fe2ebb76SJohn Baldwin #endif
1160733b9277SNavdeep Parhar
1161733b9277SNavdeep Parhar /*
1162f549e352SNavdeep Parhar * Allocate rx queues first because a default iqid is required when
1163f549e352SNavdeep Parhar * creating a tx queue.
1164733b9277SNavdeep Parhar */
11656a59b994SNavdeep Parhar maxp = max_rx_payload(sc, ifp, false);
1166fe2ebb76SJohn Baldwin for_each_rxq(vi, i, rxq) {
116743bbae19SNavdeep Parhar rc = alloc_rxq(vi, rxq, i, intr_idx, maxp);
116854e4ee71SNavdeep Parhar if (rc != 0)
116954e4ee71SNavdeep Parhar goto done;
117043bbae19SNavdeep Parhar if (!forwarding_intr_to_fwq(sc))
1171733b9277SNavdeep Parhar intr_idx++;
1172733b9277SNavdeep Parhar }
117362291463SNavdeep Parhar #ifdef DEV_NETMAP
1174954712e8SJustin Hibbits if (if_getcapabilities(ifp) & IFCAP_NETMAP)
117562291463SNavdeep Parhar intr_idx = saved_idx + max(vi->nrxq, vi->nnmrxq);
117662291463SNavdeep Parhar #endif
117709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
11786a59b994SNavdeep Parhar maxp = max_rx_payload(sc, ifp, true);
1179fe2ebb76SJohn Baldwin for_each_ofld_rxq(vi, i, ofld_rxq) {
118043bbae19SNavdeep Parhar rc = alloc_ofld_rxq(vi, ofld_rxq, i, intr_idx, maxp);
1181733b9277SNavdeep Parhar if (rc != 0)
1182733b9277SNavdeep Parhar goto done;
118343bbae19SNavdeep Parhar if (!forwarding_intr_to_fwq(sc))
1184733b9277SNavdeep Parhar intr_idx++;
1185733b9277SNavdeep Parhar }
1186733b9277SNavdeep Parhar #endif
1187733b9277SNavdeep Parhar
1188733b9277SNavdeep Parhar /*
1189f549e352SNavdeep Parhar * Now the tx queues.
1190733b9277SNavdeep Parhar */
1191fe2ebb76SJohn Baldwin for_each_txq(vi, i, txq) {
119243bbae19SNavdeep Parhar rc = alloc_txq(vi, txq, i);
119354e4ee71SNavdeep Parhar if (rc != 0)
119454e4ee71SNavdeep Parhar goto done;
119554e4ee71SNavdeep Parhar }
1196eff62dbaSNavdeep Parhar #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1197fe2ebb76SJohn Baldwin for_each_ofld_txq(vi, i, ofld_txq) {
119843bbae19SNavdeep Parhar rc = alloc_ofld_txq(vi, ofld_txq, i);
1199298d969cSNavdeep Parhar if (rc != 0)
1200298d969cSNavdeep Parhar goto done;
1201298d969cSNavdeep Parhar }
1202298d969cSNavdeep Parhar #endif
120354e4ee71SNavdeep Parhar done:
120454e4ee71SNavdeep Parhar if (rc)
1205fe2ebb76SJohn Baldwin t4_teardown_vi_queues(vi);
120654e4ee71SNavdeep Parhar
120754e4ee71SNavdeep Parhar return (rc);
120854e4ee71SNavdeep Parhar }
120954e4ee71SNavdeep Parhar
121054e4ee71SNavdeep Parhar /*
121154e4ee71SNavdeep Parhar * Idempotent
121254e4ee71SNavdeep Parhar */
121354e4ee71SNavdeep Parhar int
t4_teardown_vi_queues(struct vi_info * vi)1214fe2ebb76SJohn Baldwin t4_teardown_vi_queues(struct vi_info *vi)
121554e4ee71SNavdeep Parhar {
121654e4ee71SNavdeep Parhar int i;
121754e4ee71SNavdeep Parhar struct sge_rxq *rxq;
121854e4ee71SNavdeep Parhar struct sge_txq *txq;
121937310a98SNavdeep Parhar #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1220077ba6a8SJohn Baldwin struct sge_ofld_txq *ofld_txq;
122137310a98SNavdeep Parhar #endif
122209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
1223733b9277SNavdeep Parhar struct sge_ofld_rxq *ofld_rxq;
1224eff62dbaSNavdeep Parhar #endif
1225298d969cSNavdeep Parhar #ifdef DEV_NETMAP
1226298d969cSNavdeep Parhar struct sge_nm_rxq *nm_rxq;
1227298d969cSNavdeep Parhar struct sge_nm_txq *nm_txq;
1228298d969cSNavdeep Parhar #endif
122954e4ee71SNavdeep Parhar
1230fe2ebb76SJohn Baldwin #ifdef DEV_NETMAP
1231954712e8SJustin Hibbits if (if_getcapabilities(vi->ifp) & IFCAP_NETMAP) {
1232fe2ebb76SJohn Baldwin for_each_nm_txq(vi, i, nm_txq) {
1233fe2ebb76SJohn Baldwin free_nm_txq(vi, nm_txq);
1234fe2ebb76SJohn Baldwin }
1235fe2ebb76SJohn Baldwin
1236fe2ebb76SJohn Baldwin for_each_nm_rxq(vi, i, nm_rxq) {
1237fe2ebb76SJohn Baldwin free_nm_rxq(vi, nm_rxq);
1238fe2ebb76SJohn Baldwin }
1239fe2ebb76SJohn Baldwin }
1240fe2ebb76SJohn Baldwin #endif
1241fe2ebb76SJohn Baldwin
1242733b9277SNavdeep Parhar /*
1243733b9277SNavdeep Parhar * Take down all the tx queues first, as they reference the rx queues
1244733b9277SNavdeep Parhar * (for egress updates, etc.).
1245733b9277SNavdeep Parhar */
1246733b9277SNavdeep Parhar
1247fe2ebb76SJohn Baldwin for_each_txq(vi, i, txq) {
1248fe2ebb76SJohn Baldwin free_txq(vi, txq);
124954e4ee71SNavdeep Parhar }
1250eff62dbaSNavdeep Parhar #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1251fe2ebb76SJohn Baldwin for_each_ofld_txq(vi, i, ofld_txq) {
1252077ba6a8SJohn Baldwin free_ofld_txq(vi, ofld_txq);
1253733b9277SNavdeep Parhar }
1254733b9277SNavdeep Parhar #endif
1255733b9277SNavdeep Parhar
1256733b9277SNavdeep Parhar /*
1257f549e352SNavdeep Parhar * Then take down the rx queues.
1258733b9277SNavdeep Parhar */
1259733b9277SNavdeep Parhar
1260fe2ebb76SJohn Baldwin for_each_rxq(vi, i, rxq) {
1261fe2ebb76SJohn Baldwin free_rxq(vi, rxq);
126254e4ee71SNavdeep Parhar }
126309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
1264fe2ebb76SJohn Baldwin for_each_ofld_rxq(vi, i, ofld_rxq) {
1265fe2ebb76SJohn Baldwin free_ofld_rxq(vi, ofld_rxq);
1266733b9277SNavdeep Parhar }
1267733b9277SNavdeep Parhar #endif
1268733b9277SNavdeep Parhar
126954e4ee71SNavdeep Parhar return (0);
127054e4ee71SNavdeep Parhar }
127154e4ee71SNavdeep Parhar
1272733b9277SNavdeep Parhar /*
12733098bcfcSNavdeep Parhar * Interrupt handler when the driver is using only 1 interrupt. This is a very
12743098bcfcSNavdeep Parhar * unusual scenario.
12753098bcfcSNavdeep Parhar *
12763098bcfcSNavdeep Parhar * a) Deals with errors, if any.
12773098bcfcSNavdeep Parhar * b) Services firmware event queue, which is taking interrupts for all other
12783098bcfcSNavdeep Parhar * queues.
1279733b9277SNavdeep Parhar */
128054e4ee71SNavdeep Parhar void
t4_intr_all(void * arg)128154e4ee71SNavdeep Parhar t4_intr_all(void *arg)
128254e4ee71SNavdeep Parhar {
128354e4ee71SNavdeep Parhar struct adapter *sc = arg;
1284733b9277SNavdeep Parhar struct sge_iq *fwq = &sc->sge.fwq;
128554e4ee71SNavdeep Parhar
12863098bcfcSNavdeep Parhar MPASS(sc->intr_count == 1);
12873098bcfcSNavdeep Parhar
12881dca7005SNavdeep Parhar if (sc->intr_type == INTR_INTX)
12891dca7005SNavdeep Parhar t4_write_reg(sc, MYPF_REG(A_PCIE_PF_CLI), 0);
12901dca7005SNavdeep Parhar
129154e4ee71SNavdeep Parhar t4_intr_err(arg);
12923098bcfcSNavdeep Parhar t4_intr_evt(fwq);
129354e4ee71SNavdeep Parhar }
129454e4ee71SNavdeep Parhar
12953098bcfcSNavdeep Parhar /*
12963098bcfcSNavdeep Parhar * Interrupt handler for errors (installed directly when multiple interrupts are
12973098bcfcSNavdeep Parhar * being used, or called by t4_intr_all).
12983098bcfcSNavdeep Parhar */
129954e4ee71SNavdeep Parhar void
t4_intr_err(void * arg)130054e4ee71SNavdeep Parhar t4_intr_err(void *arg)
130154e4ee71SNavdeep Parhar {
130254e4ee71SNavdeep Parhar struct adapter *sc = arg;
1303dd3b96ecSNavdeep Parhar uint32_t v;
1304cb7c3f12SNavdeep Parhar const bool verbose = (sc->debug_flags & DF_VERBOSE_SLOWINTR) != 0;
130554e4ee71SNavdeep Parhar
1306e9e7bc82SNavdeep Parhar if (atomic_load_int(&sc->error_flags) & ADAP_FATAL_ERR)
1307cb7c3f12SNavdeep Parhar return;
1308cb7c3f12SNavdeep Parhar
1309dd3b96ecSNavdeep Parhar v = t4_read_reg(sc, MYPF_REG(A_PL_PF_INT_CAUSE));
1310dd3b96ecSNavdeep Parhar if (v & F_PFSW) {
1311dd3b96ecSNavdeep Parhar sc->swintr++;
1312dd3b96ecSNavdeep Parhar t4_write_reg(sc, MYPF_REG(A_PL_PF_INT_CAUSE), v);
1313dd3b96ecSNavdeep Parhar }
1314dd3b96ecSNavdeep Parhar
1315e9e7bc82SNavdeep Parhar if (t4_slow_intr_handler(sc, verbose))
1316e9e7bc82SNavdeep Parhar t4_fatal_err(sc, false);
131754e4ee71SNavdeep Parhar }
131854e4ee71SNavdeep Parhar
13193098bcfcSNavdeep Parhar /*
13203098bcfcSNavdeep Parhar * Interrupt handler for iq-only queues. The firmware event queue is the only
13213098bcfcSNavdeep Parhar * such queue right now.
13223098bcfcSNavdeep Parhar */
132354e4ee71SNavdeep Parhar void
t4_intr_evt(void * arg)132454e4ee71SNavdeep Parhar t4_intr_evt(void *arg)
132554e4ee71SNavdeep Parhar {
132654e4ee71SNavdeep Parhar struct sge_iq *iq = arg;
13272be67d29SNavdeep Parhar
1328733b9277SNavdeep Parhar if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) {
1329733b9277SNavdeep Parhar service_iq(iq, 0);
1330da6e3387SNavdeep Parhar (void) atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE);
13312be67d29SNavdeep Parhar }
13322be67d29SNavdeep Parhar }
13332be67d29SNavdeep Parhar
13343098bcfcSNavdeep Parhar /*
13353098bcfcSNavdeep Parhar * Interrupt handler for iq+fl queues.
13363098bcfcSNavdeep Parhar */
1337733b9277SNavdeep Parhar void
t4_intr(void * arg)1338733b9277SNavdeep Parhar t4_intr(void *arg)
13392be67d29SNavdeep Parhar {
13402be67d29SNavdeep Parhar struct sge_iq *iq = arg;
1341733b9277SNavdeep Parhar
1342733b9277SNavdeep Parhar if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) {
13433098bcfcSNavdeep Parhar service_iq_fl(iq, 0);
1344da6e3387SNavdeep Parhar (void) atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE);
1345733b9277SNavdeep Parhar }
1346733b9277SNavdeep Parhar }
1347733b9277SNavdeep Parhar
13483098bcfcSNavdeep Parhar #ifdef DEV_NETMAP
13493098bcfcSNavdeep Parhar /*
13503098bcfcSNavdeep Parhar * Interrupt handler for netmap rx queues.
13513098bcfcSNavdeep Parhar */
13523098bcfcSNavdeep Parhar void
t4_nm_intr(void * arg)13533098bcfcSNavdeep Parhar t4_nm_intr(void *arg)
13543098bcfcSNavdeep Parhar {
13553098bcfcSNavdeep Parhar struct sge_nm_rxq *nm_rxq = arg;
13563098bcfcSNavdeep Parhar
13573098bcfcSNavdeep Parhar if (atomic_cmpset_int(&nm_rxq->nm_state, NM_ON, NM_BUSY)) {
13583098bcfcSNavdeep Parhar service_nm_rxq(nm_rxq);
1359da6e3387SNavdeep Parhar (void) atomic_cmpset_int(&nm_rxq->nm_state, NM_BUSY, NM_ON);
13603098bcfcSNavdeep Parhar }
13613098bcfcSNavdeep Parhar }
13623098bcfcSNavdeep Parhar
13633098bcfcSNavdeep Parhar /*
13643098bcfcSNavdeep Parhar * Interrupt handler for vectors shared between NIC and netmap rx queues.
13653098bcfcSNavdeep Parhar */
136662291463SNavdeep Parhar void
t4_vi_intr(void * arg)136762291463SNavdeep Parhar t4_vi_intr(void *arg)
136862291463SNavdeep Parhar {
136962291463SNavdeep Parhar struct irq *irq = arg;
137062291463SNavdeep Parhar
13713098bcfcSNavdeep Parhar MPASS(irq->nm_rxq != NULL);
137262291463SNavdeep Parhar t4_nm_intr(irq->nm_rxq);
13733098bcfcSNavdeep Parhar
13743098bcfcSNavdeep Parhar MPASS(irq->rxq != NULL);
137562291463SNavdeep Parhar t4_intr(irq->rxq);
137662291463SNavdeep Parhar }
13773098bcfcSNavdeep Parhar #endif
137846f48ee5SNavdeep Parhar
1379733b9277SNavdeep Parhar /*
13803098bcfcSNavdeep Parhar * Deals with interrupts on an iq-only (no freelist) queue.
1381733b9277SNavdeep Parhar */
1382733b9277SNavdeep Parhar static int
service_iq(struct sge_iq * iq,int budget)1383733b9277SNavdeep Parhar service_iq(struct sge_iq *iq, int budget)
1384733b9277SNavdeep Parhar {
1385733b9277SNavdeep Parhar struct sge_iq *q;
138654e4ee71SNavdeep Parhar struct adapter *sc = iq->adapter;
1387b2daa9a9SNavdeep Parhar struct iq_desc *d = &iq->desc[iq->cidx];
13884d6db4e0SNavdeep Parhar int ndescs = 0, limit;
13893098bcfcSNavdeep Parhar int rsp_type;
1390733b9277SNavdeep Parhar uint32_t lq;
1391733b9277SNavdeep Parhar STAILQ_HEAD(, sge_iq) iql = STAILQ_HEAD_INITIALIZER(iql);
1392733b9277SNavdeep Parhar
1393733b9277SNavdeep Parhar KASSERT(iq->state == IQS_BUSY, ("%s: iq %p not BUSY", __func__, iq));
13943098bcfcSNavdeep Parhar KASSERT((iq->flags & IQ_HAS_FL) == 0,
13953098bcfcSNavdeep Parhar ("%s: called for iq %p with fl (iq->flags 0x%x)", __func__, iq,
13963098bcfcSNavdeep Parhar iq->flags));
13973098bcfcSNavdeep Parhar MPASS((iq->flags & IQ_ADJ_CREDIT) == 0);
13983098bcfcSNavdeep Parhar MPASS((iq->flags & IQ_LRO_ENABLED) == 0);
1399733b9277SNavdeep Parhar
14004d6db4e0SNavdeep Parhar limit = budget ? budget : iq->qsize / 16;
14014d6db4e0SNavdeep Parhar
1402733b9277SNavdeep Parhar /*
1403733b9277SNavdeep Parhar * We always come back and check the descriptor ring for new indirect
1404733b9277SNavdeep Parhar * interrupts and other responses after running a single handler.
1405733b9277SNavdeep Parhar */
1406733b9277SNavdeep Parhar for (;;) {
1407b2daa9a9SNavdeep Parhar while ((d->rsp.u.type_gen & F_RSPD_GEN) == iq->gen) {
140854e4ee71SNavdeep Parhar
140954e4ee71SNavdeep Parhar rmb();
141054e4ee71SNavdeep Parhar
1411b2daa9a9SNavdeep Parhar rsp_type = G_RSPD_TYPE(d->rsp.u.type_gen);
1412b2daa9a9SNavdeep Parhar lq = be32toh(d->rsp.pldbuflen_qid);
141354e4ee71SNavdeep Parhar
1414733b9277SNavdeep Parhar switch (rsp_type) {
1415733b9277SNavdeep Parhar case X_RSPD_TYPE_FLBUF:
14163098bcfcSNavdeep Parhar panic("%s: data for an iq (%p) with no freelist",
14173098bcfcSNavdeep Parhar __func__, iq);
141854e4ee71SNavdeep Parhar
14193098bcfcSNavdeep Parhar /* NOTREACHED */
1420733b9277SNavdeep Parhar
1421733b9277SNavdeep Parhar case X_RSPD_TYPE_CPL:
1422b2daa9a9SNavdeep Parhar KASSERT(d->rss.opcode < NUM_CPL_CMDS,
1423733b9277SNavdeep Parhar ("%s: bad opcode %02x.", __func__,
1424b2daa9a9SNavdeep Parhar d->rss.opcode));
14253098bcfcSNavdeep Parhar t4_cpl_handler[d->rss.opcode](iq, &d->rss, NULL);
1426733b9277SNavdeep Parhar break;
1427733b9277SNavdeep Parhar
1428733b9277SNavdeep Parhar case X_RSPD_TYPE_INTR:
142998005176SNavdeep Parhar /*
143098005176SNavdeep Parhar * There are 1K interrupt-capable queues (qids 0
143198005176SNavdeep Parhar * through 1023). A response type indicating a
143298005176SNavdeep Parhar * forwarded interrupt with a qid >= 1K is an
143398005176SNavdeep Parhar * iWARP async notification.
143498005176SNavdeep Parhar */
14353098bcfcSNavdeep Parhar if (__predict_true(lq >= 1024)) {
1436671bf2b8SNavdeep Parhar t4_an_handler(iq, &d->rsp);
143798005176SNavdeep Parhar break;
143898005176SNavdeep Parhar }
143998005176SNavdeep Parhar
1440ec55567cSJohn Baldwin q = sc->sge.iqmap[lq - sc->sge.iq_start -
1441ec55567cSJohn Baldwin sc->sge.iq_base];
1442733b9277SNavdeep Parhar if (atomic_cmpset_int(&q->state, IQS_IDLE,
1443733b9277SNavdeep Parhar IQS_BUSY)) {
14443098bcfcSNavdeep Parhar if (service_iq_fl(q, q->qsize / 16) == 0) {
1445da6e3387SNavdeep Parhar (void) atomic_cmpset_int(&q->state,
1446733b9277SNavdeep Parhar IQS_BUSY, IQS_IDLE);
1447733b9277SNavdeep Parhar } else {
1448733b9277SNavdeep Parhar STAILQ_INSERT_TAIL(&iql, q,
1449733b9277SNavdeep Parhar link);
1450733b9277SNavdeep Parhar }
1451733b9277SNavdeep Parhar }
1452733b9277SNavdeep Parhar break;
1453733b9277SNavdeep Parhar
1454733b9277SNavdeep Parhar default:
145598005176SNavdeep Parhar KASSERT(0,
145698005176SNavdeep Parhar ("%s: illegal response type %d on iq %p",
145798005176SNavdeep Parhar __func__, rsp_type, iq));
145898005176SNavdeep Parhar log(LOG_ERR,
145998005176SNavdeep Parhar "%s: illegal response type %d on iq %p",
146098005176SNavdeep Parhar device_get_nameunit(sc->dev), rsp_type, iq);
146109fe6320SNavdeep Parhar break;
146254e4ee71SNavdeep Parhar }
146356599263SNavdeep Parhar
1464b2daa9a9SNavdeep Parhar d++;
1465b2daa9a9SNavdeep Parhar if (__predict_false(++iq->cidx == iq->sidx)) {
1466b2daa9a9SNavdeep Parhar iq->cidx = 0;
1467b2daa9a9SNavdeep Parhar iq->gen ^= F_RSPD_GEN;
1468b2daa9a9SNavdeep Parhar d = &iq->desc[0];
1469b2daa9a9SNavdeep Parhar }
1470b2daa9a9SNavdeep Parhar if (__predict_false(++ndescs == limit)) {
1471315048f2SJohn Baldwin t4_write_reg(sc, sc->sge_gts_reg,
1472733b9277SNavdeep Parhar V_CIDXINC(ndescs) |
1473733b9277SNavdeep Parhar V_INGRESSQID(iq->cntxt_id) |
1474733b9277SNavdeep Parhar V_SEINTARM(V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX)));
1475733b9277SNavdeep Parhar ndescs = 0;
1476733b9277SNavdeep Parhar
14773098bcfcSNavdeep Parhar if (budget) {
14783098bcfcSNavdeep Parhar return (EINPROGRESS);
14793098bcfcSNavdeep Parhar }
14803098bcfcSNavdeep Parhar }
14813098bcfcSNavdeep Parhar }
14823098bcfcSNavdeep Parhar
14833098bcfcSNavdeep Parhar if (STAILQ_EMPTY(&iql))
14843098bcfcSNavdeep Parhar break;
14853098bcfcSNavdeep Parhar
14863098bcfcSNavdeep Parhar /*
14873098bcfcSNavdeep Parhar * Process the head only, and send it to the back of the list if
14883098bcfcSNavdeep Parhar * it's still not done.
14893098bcfcSNavdeep Parhar */
14903098bcfcSNavdeep Parhar q = STAILQ_FIRST(&iql);
14913098bcfcSNavdeep Parhar STAILQ_REMOVE_HEAD(&iql, link);
14923098bcfcSNavdeep Parhar if (service_iq_fl(q, q->qsize / 8) == 0)
1493da6e3387SNavdeep Parhar (void) atomic_cmpset_int(&q->state, IQS_BUSY, IQS_IDLE);
14943098bcfcSNavdeep Parhar else
14953098bcfcSNavdeep Parhar STAILQ_INSERT_TAIL(&iql, q, link);
14963098bcfcSNavdeep Parhar }
14973098bcfcSNavdeep Parhar
14983098bcfcSNavdeep Parhar t4_write_reg(sc, sc->sge_gts_reg, V_CIDXINC(ndescs) |
14993098bcfcSNavdeep Parhar V_INGRESSQID((u32)iq->cntxt_id) | V_SEINTARM(iq->intr_params));
15003098bcfcSNavdeep Parhar
15013098bcfcSNavdeep Parhar return (0);
15023098bcfcSNavdeep Parhar }
15033098bcfcSNavdeep Parhar
1504ffbb373cSNavdeep Parhar #if defined(INET) || defined(INET6)
15053098bcfcSNavdeep Parhar static inline int
sort_before_lro(struct lro_ctrl * lro)15063098bcfcSNavdeep Parhar sort_before_lro(struct lro_ctrl *lro)
15073098bcfcSNavdeep Parhar {
15083098bcfcSNavdeep Parhar
15093098bcfcSNavdeep Parhar return (lro->lro_mbuf_max != 0);
15103098bcfcSNavdeep Parhar }
1511ffbb373cSNavdeep Parhar #endif
15123098bcfcSNavdeep Parhar
1513e398922eSRandall Stewart #define CGBE_SHIFT_SCALE 10
1514e7e08444SNavdeep Parhar
1515e398922eSRandall Stewart static inline uint64_t
t4_tstmp_to_ns(struct adapter * sc,uint64_t lf)1516e398922eSRandall Stewart t4_tstmp_to_ns(struct adapter *sc, uint64_t lf)
1517e398922eSRandall Stewart {
1518e398922eSRandall Stewart struct clock_sync *cur, dcur;
1519e398922eSRandall Stewart uint64_t hw_clocks;
15202c74c9daSJohn Baldwin uint64_t hw_clk_div;
15212c74c9daSJohn Baldwin sbintime_t sbt_cur_to_prev, sbt;
1522e398922eSRandall Stewart uint64_t hw_tstmp = lf & 0xfffffffffffffffULL; /* 60b, not 64b. */
1523cee4fc7cSJohn Baldwin seqc_t gen;
1524e398922eSRandall Stewart
1525cee4fc7cSJohn Baldwin for (;;) {
1526e398922eSRandall Stewart cur = &sc->cal_info[sc->cal_current];
1527cee4fc7cSJohn Baldwin gen = seqc_read(&cur->gen);
1528e398922eSRandall Stewart if (gen == 0)
1529e398922eSRandall Stewart return (0);
1530e398922eSRandall Stewart dcur = *cur;
1531cee4fc7cSJohn Baldwin if (seqc_consistent(&cur->gen, gen))
1532cee4fc7cSJohn Baldwin break;
1533cee4fc7cSJohn Baldwin }
1534e398922eSRandall Stewart
1535e398922eSRandall Stewart /*
1536e398922eSRandall Stewart * Our goal here is to have a result that is:
1537e398922eSRandall Stewart *
1538e398922eSRandall Stewart * ( (cur_time - prev_time) )
1539e398922eSRandall Stewart * ((hw_tstmp - hw_prev) * ----------------------------- ) + prev_time
1540e398922eSRandall Stewart * ( (hw_cur - hw_prev) )
1541e398922eSRandall Stewart *
1542e398922eSRandall Stewart * With the constraints that we cannot use float and we
1543e398922eSRandall Stewart * don't want to overflow the uint64_t numbers we are using.
1544e398922eSRandall Stewart */
1545e398922eSRandall Stewart hw_clocks = hw_tstmp - dcur.hw_prev;
15462c74c9daSJohn Baldwin sbt_cur_to_prev = (dcur.sbt_cur - dcur.sbt_prev);
1547e398922eSRandall Stewart hw_clk_div = dcur.hw_cur - dcur.hw_prev;
15482c74c9daSJohn Baldwin sbt = hw_clocks * sbt_cur_to_prev / hw_clk_div + dcur.sbt_prev;
15492c74c9daSJohn Baldwin return (sbttons(sbt));
1550e7e08444SNavdeep Parhar }
1551e7e08444SNavdeep Parhar
155246e1e307SNavdeep Parhar static inline void
move_to_next_rxbuf(struct sge_fl * fl)155346e1e307SNavdeep Parhar move_to_next_rxbuf(struct sge_fl *fl)
155446e1e307SNavdeep Parhar {
155546e1e307SNavdeep Parhar
155646e1e307SNavdeep Parhar fl->rx_offset = 0;
155746e1e307SNavdeep Parhar if (__predict_false((++fl->cidx & 7) == 0)) {
155846e1e307SNavdeep Parhar uint16_t cidx = fl->cidx >> 3;
155946e1e307SNavdeep Parhar
156046e1e307SNavdeep Parhar if (__predict_false(cidx == fl->sidx))
156146e1e307SNavdeep Parhar fl->cidx = cidx = 0;
156246e1e307SNavdeep Parhar fl->hw_cidx = cidx;
156346e1e307SNavdeep Parhar }
156446e1e307SNavdeep Parhar }
156546e1e307SNavdeep Parhar
15663098bcfcSNavdeep Parhar /*
15673098bcfcSNavdeep Parhar * Deals with interrupts on an iq+fl queue.
15683098bcfcSNavdeep Parhar */
15693098bcfcSNavdeep Parhar static int
service_iq_fl(struct sge_iq * iq,int budget)15703098bcfcSNavdeep Parhar service_iq_fl(struct sge_iq *iq, int budget)
15713098bcfcSNavdeep Parhar {
15723098bcfcSNavdeep Parhar struct sge_rxq *rxq = iq_to_rxq(iq);
15733098bcfcSNavdeep Parhar struct sge_fl *fl;
15743098bcfcSNavdeep Parhar struct adapter *sc = iq->adapter;
15753098bcfcSNavdeep Parhar struct iq_desc *d = &iq->desc[iq->cidx];
157646e1e307SNavdeep Parhar int ndescs, limit;
157746e1e307SNavdeep Parhar int rsp_type, starved;
15783098bcfcSNavdeep Parhar uint32_t lq;
15793098bcfcSNavdeep Parhar uint16_t fl_hw_cidx;
15803098bcfcSNavdeep Parhar struct mbuf *m0;
15813098bcfcSNavdeep Parhar #if defined(INET) || defined(INET6)
15823098bcfcSNavdeep Parhar const struct timeval lro_timeout = {0, sc->lro_timeout};
15833098bcfcSNavdeep Parhar struct lro_ctrl *lro = &rxq->lro;
15843098bcfcSNavdeep Parhar #endif
15853098bcfcSNavdeep Parhar
15863098bcfcSNavdeep Parhar KASSERT(iq->state == IQS_BUSY, ("%s: iq %p not BUSY", __func__, iq));
15873098bcfcSNavdeep Parhar MPASS(iq->flags & IQ_HAS_FL);
15883098bcfcSNavdeep Parhar
158946e1e307SNavdeep Parhar ndescs = 0;
15903098bcfcSNavdeep Parhar #if defined(INET) || defined(INET6)
15913098bcfcSNavdeep Parhar if (iq->flags & IQ_ADJ_CREDIT) {
15923098bcfcSNavdeep Parhar MPASS(sort_before_lro(lro));
15933098bcfcSNavdeep Parhar iq->flags &= ~IQ_ADJ_CREDIT;
15943098bcfcSNavdeep Parhar if ((d->rsp.u.type_gen & F_RSPD_GEN) != iq->gen) {
15953098bcfcSNavdeep Parhar tcp_lro_flush_all(lro);
15963098bcfcSNavdeep Parhar t4_write_reg(sc, sc->sge_gts_reg, V_CIDXINC(1) |
15973098bcfcSNavdeep Parhar V_INGRESSQID((u32)iq->cntxt_id) |
15983098bcfcSNavdeep Parhar V_SEINTARM(iq->intr_params));
15993098bcfcSNavdeep Parhar return (0);
16003098bcfcSNavdeep Parhar }
16013098bcfcSNavdeep Parhar ndescs = 1;
16023098bcfcSNavdeep Parhar }
16033098bcfcSNavdeep Parhar #else
16043098bcfcSNavdeep Parhar MPASS((iq->flags & IQ_ADJ_CREDIT) == 0);
16053098bcfcSNavdeep Parhar #endif
16063098bcfcSNavdeep Parhar
160746e1e307SNavdeep Parhar limit = budget ? budget : iq->qsize / 16;
160846e1e307SNavdeep Parhar fl = &rxq->fl;
160946e1e307SNavdeep Parhar fl_hw_cidx = fl->hw_cidx; /* stable snapshot */
16103098bcfcSNavdeep Parhar while ((d->rsp.u.type_gen & F_RSPD_GEN) == iq->gen) {
16113098bcfcSNavdeep Parhar
16123098bcfcSNavdeep Parhar rmb();
16133098bcfcSNavdeep Parhar
16143098bcfcSNavdeep Parhar m0 = NULL;
16153098bcfcSNavdeep Parhar rsp_type = G_RSPD_TYPE(d->rsp.u.type_gen);
16163098bcfcSNavdeep Parhar lq = be32toh(d->rsp.pldbuflen_qid);
16173098bcfcSNavdeep Parhar
16183098bcfcSNavdeep Parhar switch (rsp_type) {
16193098bcfcSNavdeep Parhar case X_RSPD_TYPE_FLBUF:
162046e1e307SNavdeep Parhar if (lq & F_RSPD_NEWBUF) {
162146e1e307SNavdeep Parhar if (fl->rx_offset > 0)
162246e1e307SNavdeep Parhar move_to_next_rxbuf(fl);
162346e1e307SNavdeep Parhar lq = G_RSPD_LEN(lq);
162446e1e307SNavdeep Parhar }
162546e1e307SNavdeep Parhar if (IDXDIFF(fl->hw_cidx, fl_hw_cidx, fl->sidx) > 4) {
162646e1e307SNavdeep Parhar FL_LOCK(fl);
162746e1e307SNavdeep Parhar refill_fl(sc, fl, 64);
162846e1e307SNavdeep Parhar FL_UNLOCK(fl);
162946e1e307SNavdeep Parhar fl_hw_cidx = fl->hw_cidx;
163046e1e307SNavdeep Parhar }
16313098bcfcSNavdeep Parhar
16321486d2deSNavdeep Parhar if (d->rss.opcode == CPL_RX_PKT) {
16331486d2deSNavdeep Parhar if (__predict_true(eth_rx(sc, rxq, d, lq) == 0))
16341486d2deSNavdeep Parhar break;
16351486d2deSNavdeep Parhar goto out;
16361486d2deSNavdeep Parhar }
16373098bcfcSNavdeep Parhar m0 = get_fl_payload(sc, fl, lq);
16383098bcfcSNavdeep Parhar if (__predict_false(m0 == NULL))
16393098bcfcSNavdeep Parhar goto out;
1640e7e08444SNavdeep Parhar
16413098bcfcSNavdeep Parhar /* fall through */
16423098bcfcSNavdeep Parhar
16433098bcfcSNavdeep Parhar case X_RSPD_TYPE_CPL:
16443098bcfcSNavdeep Parhar KASSERT(d->rss.opcode < NUM_CPL_CMDS,
16453098bcfcSNavdeep Parhar ("%s: bad opcode %02x.", __func__, d->rss.opcode));
16463098bcfcSNavdeep Parhar t4_cpl_handler[d->rss.opcode](iq, &d->rss, m0);
16473098bcfcSNavdeep Parhar break;
16483098bcfcSNavdeep Parhar
16493098bcfcSNavdeep Parhar case X_RSPD_TYPE_INTR:
16503098bcfcSNavdeep Parhar
16513098bcfcSNavdeep Parhar /*
16523098bcfcSNavdeep Parhar * There are 1K interrupt-capable queues (qids 0
16533098bcfcSNavdeep Parhar * through 1023). A response type indicating a
16543098bcfcSNavdeep Parhar * forwarded interrupt with a qid >= 1K is an
16553098bcfcSNavdeep Parhar * iWARP async notification. That is the only
16563098bcfcSNavdeep Parhar * acceptable indirect interrupt on this queue.
16573098bcfcSNavdeep Parhar */
16583098bcfcSNavdeep Parhar if (__predict_false(lq < 1024)) {
16593098bcfcSNavdeep Parhar panic("%s: indirect interrupt on iq_fl %p "
16603098bcfcSNavdeep Parhar "with qid %u", __func__, iq, lq);
16613098bcfcSNavdeep Parhar }
16623098bcfcSNavdeep Parhar
16633098bcfcSNavdeep Parhar t4_an_handler(iq, &d->rsp);
16643098bcfcSNavdeep Parhar break;
16653098bcfcSNavdeep Parhar
16663098bcfcSNavdeep Parhar default:
16673098bcfcSNavdeep Parhar KASSERT(0, ("%s: illegal response type %d on iq %p",
16683098bcfcSNavdeep Parhar __func__, rsp_type, iq));
16693098bcfcSNavdeep Parhar log(LOG_ERR, "%s: illegal response type %d on iq %p",
16703098bcfcSNavdeep Parhar device_get_nameunit(sc->dev), rsp_type, iq);
16713098bcfcSNavdeep Parhar break;
16723098bcfcSNavdeep Parhar }
16733098bcfcSNavdeep Parhar
16743098bcfcSNavdeep Parhar d++;
16753098bcfcSNavdeep Parhar if (__predict_false(++iq->cidx == iq->sidx)) {
16763098bcfcSNavdeep Parhar iq->cidx = 0;
16773098bcfcSNavdeep Parhar iq->gen ^= F_RSPD_GEN;
16783098bcfcSNavdeep Parhar d = &iq->desc[0];
16793098bcfcSNavdeep Parhar }
16803098bcfcSNavdeep Parhar if (__predict_false(++ndescs == limit)) {
16813098bcfcSNavdeep Parhar t4_write_reg(sc, sc->sge_gts_reg, V_CIDXINC(ndescs) |
16823098bcfcSNavdeep Parhar V_INGRESSQID(iq->cntxt_id) |
16833098bcfcSNavdeep Parhar V_SEINTARM(V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX)));
16843098bcfcSNavdeep Parhar
1685480e603cSNavdeep Parhar #if defined(INET) || defined(INET6)
1686480e603cSNavdeep Parhar if (iq->flags & IQ_LRO_ENABLED &&
168746f48ee5SNavdeep Parhar !sort_before_lro(lro) &&
1688480e603cSNavdeep Parhar sc->lro_timeout != 0) {
16893098bcfcSNavdeep Parhar tcp_lro_flush_inactive(lro, &lro_timeout);
1690480e603cSNavdeep Parhar }
1691480e603cSNavdeep Parhar #endif
169246e1e307SNavdeep Parhar if (budget)
1693733b9277SNavdeep Parhar return (EINPROGRESS);
169446e1e307SNavdeep Parhar ndescs = 0;
16954d6db4e0SNavdeep Parhar }
1696861e42b2SNavdeep Parhar }
16973098bcfcSNavdeep Parhar out:
1698a1ea9a82SNavdeep Parhar #if defined(INET) || defined(INET6)
1699733b9277SNavdeep Parhar if (iq->flags & IQ_LRO_ENABLED) {
170046f48ee5SNavdeep Parhar if (ndescs > 0 && lro->lro_mbuf_count > 8) {
170146f48ee5SNavdeep Parhar MPASS(sort_before_lro(lro));
170246f48ee5SNavdeep Parhar /* hold back one credit and don't flush LRO state */
170346f48ee5SNavdeep Parhar iq->flags |= IQ_ADJ_CREDIT;
170446f48ee5SNavdeep Parhar ndescs--;
170546f48ee5SNavdeep Parhar } else {
17066dd38b87SSepherosa Ziehau tcp_lro_flush_all(lro);
1707733b9277SNavdeep Parhar }
170846f48ee5SNavdeep Parhar }
1709733b9277SNavdeep Parhar #endif
1710733b9277SNavdeep Parhar
1711315048f2SJohn Baldwin t4_write_reg(sc, sc->sge_gts_reg, V_CIDXINC(ndescs) |
1712733b9277SNavdeep Parhar V_INGRESSQID((u32)iq->cntxt_id) | V_SEINTARM(iq->intr_params));
1713733b9277SNavdeep Parhar
1714733b9277SNavdeep Parhar FL_LOCK(fl);
171538035ed6SNavdeep Parhar starved = refill_fl(sc, fl, 64);
1716733b9277SNavdeep Parhar FL_UNLOCK(fl);
1717733b9277SNavdeep Parhar if (__predict_false(starved != 0))
1718733b9277SNavdeep Parhar add_fl_to_sfl(sc, fl);
1719733b9277SNavdeep Parhar
1720733b9277SNavdeep Parhar return (0);
1721733b9277SNavdeep Parhar }
1722733b9277SNavdeep Parhar
172338035ed6SNavdeep Parhar static inline struct cluster_metadata *
cl_metadata(struct fl_sdesc * sd)172446e1e307SNavdeep Parhar cl_metadata(struct fl_sdesc *sd)
17251458bff9SNavdeep Parhar {
17261458bff9SNavdeep Parhar
172746e1e307SNavdeep Parhar return ((void *)(sd->cl + sd->moff));
17281458bff9SNavdeep Parhar }
17291458bff9SNavdeep Parhar
173015c28f87SGleb Smirnoff static void
rxb_free(struct mbuf * m)1731e8fd18f3SGleb Smirnoff rxb_free(struct mbuf *m)
17321458bff9SNavdeep Parhar {
1733d6f79b27SNavdeep Parhar struct cluster_metadata *clm = m->m_ext.ext_arg1;
17341458bff9SNavdeep Parhar
1735d6f79b27SNavdeep Parhar uma_zfree(clm->zone, clm->cl);
173682eff304SNavdeep Parhar counter_u64_add(extfree_rels, 1);
17371458bff9SNavdeep Parhar }
17381458bff9SNavdeep Parhar
173938035ed6SNavdeep Parhar /*
174046e1e307SNavdeep Parhar * The mbuf returned comes from zone_muf and carries the payload in one of these
174146e1e307SNavdeep Parhar * ways
174246e1e307SNavdeep Parhar * a) complete frame inside the mbuf
174346e1e307SNavdeep Parhar * b) m_cljset (for clusters without metadata)
174446e1e307SNavdeep Parhar * d) m_extaddref (cluster with metadata)
174538035ed6SNavdeep Parhar */
17461458bff9SNavdeep Parhar static struct mbuf *
get_scatter_segment(struct adapter * sc,struct sge_fl * fl,int fr_offset,int remaining)1747b741402cSNavdeep Parhar get_scatter_segment(struct adapter *sc, struct sge_fl *fl, int fr_offset,
1748b741402cSNavdeep Parhar int remaining)
174938035ed6SNavdeep Parhar {
175038035ed6SNavdeep Parhar struct mbuf *m;
175138035ed6SNavdeep Parhar struct fl_sdesc *sd = &fl->sdesc[fl->cidx];
175246e1e307SNavdeep Parhar struct rx_buf_info *rxb = &sc->sge.rx_buf_info[sd->zidx];
175346e1e307SNavdeep Parhar struct cluster_metadata *clm;
1754b741402cSNavdeep Parhar int len, blen;
175538035ed6SNavdeep Parhar caddr_t payload;
175638035ed6SNavdeep Parhar
1757e3207e19SNavdeep Parhar if (fl->flags & FL_BUF_PACKING) {
175846e1e307SNavdeep Parhar u_int l, pad;
1759b741402cSNavdeep Parhar
176046e1e307SNavdeep Parhar blen = rxb->size2 - fl->rx_offset; /* max possible in this buf */
176146e1e307SNavdeep Parhar len = min(remaining, blen);
176246e1e307SNavdeep Parhar payload = sd->cl + fl->rx_offset;
176346e1e307SNavdeep Parhar
176446e1e307SNavdeep Parhar l = fr_offset + len;
176546e1e307SNavdeep Parhar pad = roundup2(l, fl->buf_boundary) - l;
176646e1e307SNavdeep Parhar if (fl->rx_offset + len + pad < rxb->size2)
1767b741402cSNavdeep Parhar blen = len + pad;
176846e1e307SNavdeep Parhar MPASS(fl->rx_offset + blen <= rxb->size2);
1769e3207e19SNavdeep Parhar } else {
1770e3207e19SNavdeep Parhar MPASS(fl->rx_offset == 0); /* not packing */
177146e1e307SNavdeep Parhar blen = rxb->size1;
177246e1e307SNavdeep Parhar len = min(remaining, blen);
177346e1e307SNavdeep Parhar payload = sd->cl;
1774e3207e19SNavdeep Parhar }
177538035ed6SNavdeep Parhar
177646e1e307SNavdeep Parhar if (fr_offset == 0) {
177746e1e307SNavdeep Parhar m = m_gethdr(M_NOWAIT, MT_DATA);
177846e1e307SNavdeep Parhar if (__predict_false(m == NULL))
177946e1e307SNavdeep Parhar return (NULL);
178046e1e307SNavdeep Parhar m->m_pkthdr.len = remaining;
178146e1e307SNavdeep Parhar } else {
178246e1e307SNavdeep Parhar m = m_get(M_NOWAIT, MT_DATA);
178346e1e307SNavdeep Parhar if (__predict_false(m == NULL))
178446e1e307SNavdeep Parhar return (NULL);
178546e1e307SNavdeep Parhar }
178646e1e307SNavdeep Parhar m->m_len = len;
178714a634dfSMark Johnston kmsan_mark(payload, len, KMSAN_STATE_INITED);
1788b741402cSNavdeep Parhar
178938035ed6SNavdeep Parhar if (sc->sc_do_rxcopy && len < RX_COPY_THRESHOLD) {
179038035ed6SNavdeep Parhar /* copy data to mbuf */
179138035ed6SNavdeep Parhar bcopy(payload, mtod(m, caddr_t), len);
179246e1e307SNavdeep Parhar if (fl->flags & FL_BUF_PACKING) {
179346e1e307SNavdeep Parhar fl->rx_offset += blen;
179446e1e307SNavdeep Parhar MPASS(fl->rx_offset <= rxb->size2);
179546e1e307SNavdeep Parhar if (fl->rx_offset < rxb->size2)
179646e1e307SNavdeep Parhar return (m); /* without advancing the cidx */
179746e1e307SNavdeep Parhar }
179846e1e307SNavdeep Parhar } else if (fl->flags & FL_BUF_PACKING) {
179946e1e307SNavdeep Parhar clm = cl_metadata(sd);
1800a9c4062aSNavdeep Parhar if (sd->nmbuf++ == 0) {
1801a9c4062aSNavdeep Parhar clm->refcount = 1;
180246e1e307SNavdeep Parhar clm->zone = rxb->zone;
1803d6f79b27SNavdeep Parhar clm->cl = sd->cl;
1804a9c4062aSNavdeep Parhar counter_u64_add(extfree_refs, 1);
1805a9c4062aSNavdeep Parhar }
1806d6f79b27SNavdeep Parhar m_extaddref(m, payload, blen, &clm->refcount, rxb_free, clm,
1807d6f79b27SNavdeep Parhar NULL);
180838035ed6SNavdeep Parhar
180946e1e307SNavdeep Parhar fl->rx_offset += blen;
181046e1e307SNavdeep Parhar MPASS(fl->rx_offset <= rxb->size2);
181146e1e307SNavdeep Parhar if (fl->rx_offset < rxb->size2)
181246e1e307SNavdeep Parhar return (m); /* without advancing the cidx */
1813ccc69b2fSNavdeep Parhar } else {
181446e1e307SNavdeep Parhar m_cljset(m, sd->cl, rxb->type);
181538035ed6SNavdeep Parhar sd->cl = NULL; /* consumed, not a recycle candidate */
181638035ed6SNavdeep Parhar }
181738035ed6SNavdeep Parhar
181846e1e307SNavdeep Parhar move_to_next_rxbuf(fl);
181938035ed6SNavdeep Parhar
182038035ed6SNavdeep Parhar return (m);
182138035ed6SNavdeep Parhar }
182238035ed6SNavdeep Parhar
182338035ed6SNavdeep Parhar static struct mbuf *
get_fl_payload(struct adapter * sc,struct sge_fl * fl,const u_int plen)182446e1e307SNavdeep Parhar get_fl_payload(struct adapter *sc, struct sge_fl *fl, const u_int plen)
18251458bff9SNavdeep Parhar {
182638035ed6SNavdeep Parhar struct mbuf *m0, *m, **pnext;
1827b741402cSNavdeep Parhar u_int remaining;
18281458bff9SNavdeep Parhar
18294d6db4e0SNavdeep Parhar if (__predict_false(fl->flags & FL_BUF_RESUME)) {
1830368541baSNavdeep Parhar M_ASSERTPKTHDR(fl->m0);
183146e1e307SNavdeep Parhar MPASS(fl->m0->m_pkthdr.len == plen);
183246e1e307SNavdeep Parhar MPASS(fl->remaining < plen);
18331458bff9SNavdeep Parhar
183438035ed6SNavdeep Parhar m0 = fl->m0;
183538035ed6SNavdeep Parhar pnext = fl->pnext;
1836b741402cSNavdeep Parhar remaining = fl->remaining;
18374d6db4e0SNavdeep Parhar fl->flags &= ~FL_BUF_RESUME;
183838035ed6SNavdeep Parhar goto get_segment;
18391458bff9SNavdeep Parhar }
18401458bff9SNavdeep Parhar
18411458bff9SNavdeep Parhar /*
184238035ed6SNavdeep Parhar * Payload starts at rx_offset in the current hw buffer. Its length is
184338035ed6SNavdeep Parhar * 'len' and it may span multiple hw buffers.
18441458bff9SNavdeep Parhar */
18451458bff9SNavdeep Parhar
184646e1e307SNavdeep Parhar m0 = get_scatter_segment(sc, fl, 0, plen);
1847368541baSNavdeep Parhar if (m0 == NULL)
18484d6db4e0SNavdeep Parhar return (NULL);
184946e1e307SNavdeep Parhar remaining = plen - m0->m_len;
185038035ed6SNavdeep Parhar pnext = &m0->m_next;
1851b741402cSNavdeep Parhar while (remaining > 0) {
185238035ed6SNavdeep Parhar get_segment:
185338035ed6SNavdeep Parhar MPASS(fl->rx_offset == 0);
185446e1e307SNavdeep Parhar m = get_scatter_segment(sc, fl, plen - remaining, remaining);
18554d6db4e0SNavdeep Parhar if (__predict_false(m == NULL)) {
185638035ed6SNavdeep Parhar fl->m0 = m0;
185738035ed6SNavdeep Parhar fl->pnext = pnext;
1858b741402cSNavdeep Parhar fl->remaining = remaining;
18594d6db4e0SNavdeep Parhar fl->flags |= FL_BUF_RESUME;
18604d6db4e0SNavdeep Parhar return (NULL);
18611458bff9SNavdeep Parhar }
186238035ed6SNavdeep Parhar *pnext = m;
186338035ed6SNavdeep Parhar pnext = &m->m_next;
1864b741402cSNavdeep Parhar remaining -= m->m_len;
1865733b9277SNavdeep Parhar }
186638035ed6SNavdeep Parhar *pnext = NULL;
18674d6db4e0SNavdeep Parhar
1868dbbf46c4SNavdeep Parhar M_ASSERTPKTHDR(m0);
1869733b9277SNavdeep Parhar return (m0);
1870733b9277SNavdeep Parhar }
1871733b9277SNavdeep Parhar
1872733b9277SNavdeep Parhar static int
skip_scatter_segment(struct adapter * sc,struct sge_fl * fl,int fr_offset,int remaining)187387bbb333SNavdeep Parhar skip_scatter_segment(struct adapter *sc, struct sge_fl *fl, int fr_offset,
187487bbb333SNavdeep Parhar int remaining)
187587bbb333SNavdeep Parhar {
187687bbb333SNavdeep Parhar struct fl_sdesc *sd = &fl->sdesc[fl->cidx];
187787bbb333SNavdeep Parhar struct rx_buf_info *rxb = &sc->sge.rx_buf_info[sd->zidx];
187887bbb333SNavdeep Parhar int len, blen;
187987bbb333SNavdeep Parhar
188087bbb333SNavdeep Parhar if (fl->flags & FL_BUF_PACKING) {
188187bbb333SNavdeep Parhar u_int l, pad;
188287bbb333SNavdeep Parhar
188387bbb333SNavdeep Parhar blen = rxb->size2 - fl->rx_offset; /* max possible in this buf */
188487bbb333SNavdeep Parhar len = min(remaining, blen);
188587bbb333SNavdeep Parhar
188687bbb333SNavdeep Parhar l = fr_offset + len;
188787bbb333SNavdeep Parhar pad = roundup2(l, fl->buf_boundary) - l;
188887bbb333SNavdeep Parhar if (fl->rx_offset + len + pad < rxb->size2)
188987bbb333SNavdeep Parhar blen = len + pad;
189087bbb333SNavdeep Parhar fl->rx_offset += blen;
189187bbb333SNavdeep Parhar MPASS(fl->rx_offset <= rxb->size2);
189287bbb333SNavdeep Parhar if (fl->rx_offset < rxb->size2)
189387bbb333SNavdeep Parhar return (len); /* without advancing the cidx */
189487bbb333SNavdeep Parhar } else {
189587bbb333SNavdeep Parhar MPASS(fl->rx_offset == 0); /* not packing */
189687bbb333SNavdeep Parhar blen = rxb->size1;
189787bbb333SNavdeep Parhar len = min(remaining, blen);
189887bbb333SNavdeep Parhar }
189987bbb333SNavdeep Parhar move_to_next_rxbuf(fl);
190087bbb333SNavdeep Parhar return (len);
190187bbb333SNavdeep Parhar }
190287bbb333SNavdeep Parhar
190387bbb333SNavdeep Parhar static inline void
skip_fl_payload(struct adapter * sc,struct sge_fl * fl,int plen)190487bbb333SNavdeep Parhar skip_fl_payload(struct adapter *sc, struct sge_fl *fl, int plen)
190587bbb333SNavdeep Parhar {
190687bbb333SNavdeep Parhar int remaining, fr_offset, len;
190787bbb333SNavdeep Parhar
190887bbb333SNavdeep Parhar fr_offset = 0;
190987bbb333SNavdeep Parhar remaining = plen;
191087bbb333SNavdeep Parhar while (remaining > 0) {
191187bbb333SNavdeep Parhar len = skip_scatter_segment(sc, fl, fr_offset, remaining);
191287bbb333SNavdeep Parhar fr_offset += len;
191387bbb333SNavdeep Parhar remaining -= len;
191487bbb333SNavdeep Parhar }
191587bbb333SNavdeep Parhar }
191687bbb333SNavdeep Parhar
191787bbb333SNavdeep Parhar static inline int
get_segment_len(struct adapter * sc,struct sge_fl * fl,int plen)191887bbb333SNavdeep Parhar get_segment_len(struct adapter *sc, struct sge_fl *fl, int plen)
191987bbb333SNavdeep Parhar {
192087bbb333SNavdeep Parhar int len;
192187bbb333SNavdeep Parhar struct fl_sdesc *sd = &fl->sdesc[fl->cidx];
192287bbb333SNavdeep Parhar struct rx_buf_info *rxb = &sc->sge.rx_buf_info[sd->zidx];
192387bbb333SNavdeep Parhar
192487bbb333SNavdeep Parhar if (fl->flags & FL_BUF_PACKING)
192587bbb333SNavdeep Parhar len = rxb->size2 - fl->rx_offset;
192687bbb333SNavdeep Parhar else
192787bbb333SNavdeep Parhar len = rxb->size1;
192887bbb333SNavdeep Parhar
192987bbb333SNavdeep Parhar return (min(plen, len));
193087bbb333SNavdeep Parhar }
193187bbb333SNavdeep Parhar
193287bbb333SNavdeep Parhar static int
eth_rx(struct adapter * sc,struct sge_rxq * rxq,const struct iq_desc * d,u_int plen)19331486d2deSNavdeep Parhar eth_rx(struct adapter *sc, struct sge_rxq *rxq, const struct iq_desc *d,
19341486d2deSNavdeep Parhar u_int plen)
1935733b9277SNavdeep Parhar {
19361486d2deSNavdeep Parhar struct mbuf *m0;
1937954712e8SJustin Hibbits if_t ifp = rxq->ifp;
19381486d2deSNavdeep Parhar struct sge_fl *fl = &rxq->fl;
1939954712e8SJustin Hibbits struct vi_info *vi = if_getsoftc(ifp);
19401486d2deSNavdeep Parhar const struct cpl_rx_pkt *cpl;
1941a1ea9a82SNavdeep Parhar #if defined(INET) || defined(INET6)
1942733b9277SNavdeep Parhar struct lro_ctrl *lro = &rxq->lro;
1943733b9277SNavdeep Parhar #endif
1944a4a4ad2dSNavdeep Parhar uint16_t err_vec, tnl_type, tnlhdr_len;
194570ca6229SNavdeep Parhar static const int sw_hashtype[4][2] = {
194670ca6229SNavdeep Parhar {M_HASHTYPE_NONE, M_HASHTYPE_NONE},
194770ca6229SNavdeep Parhar {M_HASHTYPE_RSS_IPV4, M_HASHTYPE_RSS_IPV6},
194870ca6229SNavdeep Parhar {M_HASHTYPE_RSS_TCP_IPV4, M_HASHTYPE_RSS_TCP_IPV6},
194970ca6229SNavdeep Parhar {M_HASHTYPE_RSS_UDP_IPV4, M_HASHTYPE_RSS_UDP_IPV6},
195070ca6229SNavdeep Parhar };
1951a4a4ad2dSNavdeep Parhar static const int sw_csum_flags[2][2] = {
1952a4a4ad2dSNavdeep Parhar {
1953a4a4ad2dSNavdeep Parhar /* IP, inner IP */
1954a4a4ad2dSNavdeep Parhar CSUM_ENCAP_VXLAN |
1955a4a4ad2dSNavdeep Parhar CSUM_L3_CALC | CSUM_L3_VALID |
1956a4a4ad2dSNavdeep Parhar CSUM_L4_CALC | CSUM_L4_VALID |
1957a4a4ad2dSNavdeep Parhar CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID |
1958a4a4ad2dSNavdeep Parhar CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID,
1959a4a4ad2dSNavdeep Parhar
1960a4a4ad2dSNavdeep Parhar /* IP, inner IP6 */
1961a4a4ad2dSNavdeep Parhar CSUM_ENCAP_VXLAN |
1962a4a4ad2dSNavdeep Parhar CSUM_L3_CALC | CSUM_L3_VALID |
1963a4a4ad2dSNavdeep Parhar CSUM_L4_CALC | CSUM_L4_VALID |
1964a4a4ad2dSNavdeep Parhar CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID,
1965a4a4ad2dSNavdeep Parhar },
1966a4a4ad2dSNavdeep Parhar {
1967a4a4ad2dSNavdeep Parhar /* IP6, inner IP */
1968a4a4ad2dSNavdeep Parhar CSUM_ENCAP_VXLAN |
1969a4a4ad2dSNavdeep Parhar CSUM_L4_CALC | CSUM_L4_VALID |
1970a4a4ad2dSNavdeep Parhar CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID |
1971a4a4ad2dSNavdeep Parhar CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID,
1972a4a4ad2dSNavdeep Parhar
1973a4a4ad2dSNavdeep Parhar /* IP6, inner IP6 */
1974a4a4ad2dSNavdeep Parhar CSUM_ENCAP_VXLAN |
1975a4a4ad2dSNavdeep Parhar CSUM_L4_CALC | CSUM_L4_VALID |
1976a4a4ad2dSNavdeep Parhar CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID,
1977a4a4ad2dSNavdeep Parhar },
1978a4a4ad2dSNavdeep Parhar };
1979733b9277SNavdeep Parhar
19801486d2deSNavdeep Parhar MPASS(plen > sc->params.sge.fl_pktshift);
198187bbb333SNavdeep Parhar if (vi->pfil != NULL && PFIL_HOOKED_IN(vi->pfil) &&
198287bbb333SNavdeep Parhar __predict_true((fl->flags & FL_BUF_RESUME) == 0)) {
198387bbb333SNavdeep Parhar struct fl_sdesc *sd = &fl->sdesc[fl->cidx];
198487bbb333SNavdeep Parhar caddr_t frame;
198587bbb333SNavdeep Parhar int rc, slen;
198687bbb333SNavdeep Parhar
198787bbb333SNavdeep Parhar slen = get_segment_len(sc, fl, plen) -
198887bbb333SNavdeep Parhar sc->params.sge.fl_pktshift;
198987bbb333SNavdeep Parhar frame = sd->cl + fl->rx_offset + sc->params.sge.fl_pktshift;
1990954712e8SJustin Hibbits CURVNET_SET_QUIET(if_getvnet(ifp));
1991caf32b26SGleb Smirnoff rc = pfil_mem_in(vi->pfil, frame, slen, ifp, &m0);
199287bbb333SNavdeep Parhar CURVNET_RESTORE();
199387bbb333SNavdeep Parhar if (rc == PFIL_DROPPED || rc == PFIL_CONSUMED) {
199487bbb333SNavdeep Parhar skip_fl_payload(sc, fl, plen);
199587bbb333SNavdeep Parhar return (0);
199687bbb333SNavdeep Parhar }
199787bbb333SNavdeep Parhar if (rc == PFIL_REALLOCED) {
199887bbb333SNavdeep Parhar skip_fl_payload(sc, fl, plen);
199987bbb333SNavdeep Parhar goto have_mbuf;
200087bbb333SNavdeep Parhar }
200187bbb333SNavdeep Parhar }
200287bbb333SNavdeep Parhar
20031486d2deSNavdeep Parhar m0 = get_fl_payload(sc, fl, plen);
20041486d2deSNavdeep Parhar if (__predict_false(m0 == NULL))
20051486d2deSNavdeep Parhar return (ENOMEM);
2006733b9277SNavdeep Parhar
200790e7434aSNavdeep Parhar m0->m_pkthdr.len -= sc->params.sge.fl_pktshift;
200890e7434aSNavdeep Parhar m0->m_len -= sc->params.sge.fl_pktshift;
200990e7434aSNavdeep Parhar m0->m_data += sc->params.sge.fl_pktshift;
201054e4ee71SNavdeep Parhar
201187bbb333SNavdeep Parhar have_mbuf:
201254e4ee71SNavdeep Parhar m0->m_pkthdr.rcvif = ifp;
20131486d2deSNavdeep Parhar M_HASHTYPE_SET(m0, sw_hashtype[d->rss.hash_type][d->rss.ipv6]);
20141486d2deSNavdeep Parhar m0->m_pkthdr.flowid = be32toh(d->rss.hash_val);
201554e4ee71SNavdeep Parhar
20161486d2deSNavdeep Parhar cpl = (const void *)(&d->rss + 1);
2017a4a4ad2dSNavdeep Parhar if (sc->params.tp.rx_pkt_encap) {
2018a4a4ad2dSNavdeep Parhar const uint16_t ev = be16toh(cpl->err_vec);
20199600bf00SNavdeep Parhar
2020a4a4ad2dSNavdeep Parhar err_vec = G_T6_COMPR_RXERR_VEC(ev);
2021a4a4ad2dSNavdeep Parhar tnl_type = G_T6_RX_TNL_TYPE(ev);
2022a4a4ad2dSNavdeep Parhar tnlhdr_len = G_T6_RX_TNLHDR_LEN(ev);
2023a4a4ad2dSNavdeep Parhar } else {
2024a4a4ad2dSNavdeep Parhar err_vec = be16toh(cpl->err_vec);
2025a4a4ad2dSNavdeep Parhar tnl_type = 0;
2026a4a4ad2dSNavdeep Parhar tnlhdr_len = 0;
2027a4a4ad2dSNavdeep Parhar }
2028a4a4ad2dSNavdeep Parhar if (cpl->csum_calc && err_vec == 0) {
2029a4a4ad2dSNavdeep Parhar int ipv6 = !!(cpl->l2info & htobe32(F_RXF_IP6));
2030a4a4ad2dSNavdeep Parhar
2031a4a4ad2dSNavdeep Parhar /* checksum(s) calculated and found to be correct. */
2032a4a4ad2dSNavdeep Parhar
2033a4a4ad2dSNavdeep Parhar MPASS((cpl->l2info & htobe32(F_RXF_IP)) ^
2034a4a4ad2dSNavdeep Parhar (cpl->l2info & htobe32(F_RXF_IP6)));
203554e4ee71SNavdeep Parhar m0->m_pkthdr.csum_data = be16toh(cpl->csum);
2036a4a4ad2dSNavdeep Parhar if (tnl_type == 0) {
2037954712e8SJustin Hibbits if (!ipv6 && if_getcapenable(ifp) & IFCAP_RXCSUM) {
2038a4a4ad2dSNavdeep Parhar m0->m_pkthdr.csum_flags = CSUM_L3_CALC |
2039a4a4ad2dSNavdeep Parhar CSUM_L3_VALID | CSUM_L4_CALC |
2040a4a4ad2dSNavdeep Parhar CSUM_L4_VALID;
2041954712e8SJustin Hibbits } else if (ipv6 && if_getcapenable(ifp) & IFCAP_RXCSUM_IPV6) {
2042a4a4ad2dSNavdeep Parhar m0->m_pkthdr.csum_flags = CSUM_L4_CALC |
2043a4a4ad2dSNavdeep Parhar CSUM_L4_VALID;
2044a4a4ad2dSNavdeep Parhar }
2045a4a4ad2dSNavdeep Parhar rxq->rxcsum++;
2046a4a4ad2dSNavdeep Parhar } else {
2047a4a4ad2dSNavdeep Parhar MPASS(tnl_type == RX_PKT_TNL_TYPE_VXLAN);
2048d107ee06SNavdeep Parhar
2049d107ee06SNavdeep Parhar M_HASHTYPE_SETINNER(m0);
2050a4a4ad2dSNavdeep Parhar if (__predict_false(cpl->ip_frag)) {
2051a4a4ad2dSNavdeep Parhar /*
2052a4a4ad2dSNavdeep Parhar * csum_data is for the inner frame (which is an
2053a4a4ad2dSNavdeep Parhar * IP fragment) and is not 0xffff. There is no
2054a4a4ad2dSNavdeep Parhar * way to pass the inner csum_data to the stack.
2055a4a4ad2dSNavdeep Parhar * We don't want the stack to use the inner
2056a4a4ad2dSNavdeep Parhar * csum_data to validate the outer frame or it
2057a4a4ad2dSNavdeep Parhar * will get rejected. So we fix csum_data here
2058a4a4ad2dSNavdeep Parhar * and let sw do the checksum of inner IP
2059a4a4ad2dSNavdeep Parhar * fragments.
2060a4a4ad2dSNavdeep Parhar *
2061a4a4ad2dSNavdeep Parhar * XXX: Need 32b for csum_data2 in an rx mbuf.
2062a4a4ad2dSNavdeep Parhar * Maybe stuff it into rcv_tstmp?
2063a4a4ad2dSNavdeep Parhar */
206454e4ee71SNavdeep Parhar m0->m_pkthdr.csum_data = 0xffff;
2065a4a4ad2dSNavdeep Parhar if (ipv6) {
2066a4a4ad2dSNavdeep Parhar m0->m_pkthdr.csum_flags = CSUM_L4_CALC |
2067a4a4ad2dSNavdeep Parhar CSUM_L4_VALID;
2068a4a4ad2dSNavdeep Parhar } else {
2069a4a4ad2dSNavdeep Parhar m0->m_pkthdr.csum_flags = CSUM_L3_CALC |
2070a4a4ad2dSNavdeep Parhar CSUM_L3_VALID | CSUM_L4_CALC |
2071a4a4ad2dSNavdeep Parhar CSUM_L4_VALID;
2072a4a4ad2dSNavdeep Parhar }
2073a4a4ad2dSNavdeep Parhar } else {
2074a4a4ad2dSNavdeep Parhar int outer_ipv6;
2075a4a4ad2dSNavdeep Parhar
2076a4a4ad2dSNavdeep Parhar MPASS(m0->m_pkthdr.csum_data == 0xffff);
2077a4a4ad2dSNavdeep Parhar
2078a4a4ad2dSNavdeep Parhar outer_ipv6 = tnlhdr_len >=
2079a4a4ad2dSNavdeep Parhar sizeof(struct ether_header) +
2080a4a4ad2dSNavdeep Parhar sizeof(struct ip6_hdr);
2081a4a4ad2dSNavdeep Parhar m0->m_pkthdr.csum_flags =
2082a4a4ad2dSNavdeep Parhar sw_csum_flags[outer_ipv6][ipv6];
2083a4a4ad2dSNavdeep Parhar }
2084a4a4ad2dSNavdeep Parhar rxq->vxlan_rxcsum++;
2085a4a4ad2dSNavdeep Parhar }
208654e4ee71SNavdeep Parhar }
208754e4ee71SNavdeep Parhar
208854e4ee71SNavdeep Parhar if (cpl->vlan_ex) {
20892d0a0127SNavdeep Parhar if (sc->flags & IS_VF && sc->vlan_id) {
20902d0a0127SNavdeep Parhar /*
20912d0a0127SNavdeep Parhar * HW is not setup correctly if extracted vlan_id does
20922d0a0127SNavdeep Parhar * not match the VF's setting.
20932d0a0127SNavdeep Parhar */
20942d0a0127SNavdeep Parhar MPASS(be16toh(cpl->vlan) == sc->vlan_id);
20952d0a0127SNavdeep Parhar } else {
209654e4ee71SNavdeep Parhar m0->m_pkthdr.ether_vtag = be16toh(cpl->vlan);
209754e4ee71SNavdeep Parhar m0->m_flags |= M_VLANTAG;
209854e4ee71SNavdeep Parhar rxq->vlan_extraction++;
209954e4ee71SNavdeep Parhar }
21002d0a0127SNavdeep Parhar }
210154e4ee71SNavdeep Parhar
21021486d2deSNavdeep Parhar if (rxq->iq.flags & IQ_RX_TIMESTAMP) {
21031486d2deSNavdeep Parhar /*
2104e398922eSRandall Stewart * Fill up rcv_tstmp but do not set M_TSTMP as
2105e398922eSRandall Stewart * long as we get a non-zero back from t4_tstmp_to_ns().
21061486d2deSNavdeep Parhar */
2107e398922eSRandall Stewart m0->m_pkthdr.rcv_tstmp = t4_tstmp_to_ns(sc,
2108e398922eSRandall Stewart be64toh(d->rsp.u.last_flit));
2109e398922eSRandall Stewart if (m0->m_pkthdr.rcv_tstmp != 0)
21101486d2deSNavdeep Parhar m0->m_flags |= M_TSTMP;
21111486d2deSNavdeep Parhar }
21121486d2deSNavdeep Parhar
211350575ce1SAndrew Gallatin #ifdef NUMA
2114954712e8SJustin Hibbits m0->m_pkthdr.numa_domain = if_getnumadomain(ifp);
211550575ce1SAndrew Gallatin #endif
2116a1ea9a82SNavdeep Parhar #if defined(INET) || defined(INET6)
2117a4a4ad2dSNavdeep Parhar if (rxq->iq.flags & IQ_LRO_ENABLED && tnl_type == 0 &&
21189087a3dfSNavdeep Parhar (M_HASHTYPE_GET(m0) == M_HASHTYPE_RSS_TCP_IPV4 ||
21199087a3dfSNavdeep Parhar M_HASHTYPE_GET(m0) == M_HASHTYPE_RSS_TCP_IPV6)) {
212046f48ee5SNavdeep Parhar if (sort_before_lro(lro)) {
212146f48ee5SNavdeep Parhar tcp_lro_queue_mbuf(lro, m0);
212246f48ee5SNavdeep Parhar return (0); /* queued for sort, then LRO */
212346f48ee5SNavdeep Parhar }
212446f48ee5SNavdeep Parhar if (tcp_lro_rx(lro, m0, 0) == 0)
212546f48ee5SNavdeep Parhar return (0); /* queued for LRO */
212646f48ee5SNavdeep Parhar }
212754e4ee71SNavdeep Parhar #endif
2128954712e8SJustin Hibbits if_input(ifp, m0);
212954e4ee71SNavdeep Parhar
2130733b9277SNavdeep Parhar return (0);
213154e4ee71SNavdeep Parhar }
213254e4ee71SNavdeep Parhar
2133733b9277SNavdeep Parhar /*
21347951040fSNavdeep Parhar * Must drain the wrq or make sure that someone else will.
21357951040fSNavdeep Parhar */
21367951040fSNavdeep Parhar static void
wrq_tx_drain(void * arg,int n)21377951040fSNavdeep Parhar wrq_tx_drain(void *arg, int n)
21387951040fSNavdeep Parhar {
21397951040fSNavdeep Parhar struct sge_wrq *wrq = arg;
21407951040fSNavdeep Parhar struct sge_eq *eq = &wrq->eq;
21417951040fSNavdeep Parhar
21427951040fSNavdeep Parhar EQ_LOCK(eq);
21437951040fSNavdeep Parhar if (TAILQ_EMPTY(&wrq->incomplete_wrs) && !STAILQ_EMPTY(&wrq->wr_list))
21447951040fSNavdeep Parhar drain_wrq_wr_list(wrq->adapter, wrq);
21457951040fSNavdeep Parhar EQ_UNLOCK(eq);
21467951040fSNavdeep Parhar }
21477951040fSNavdeep Parhar
21487951040fSNavdeep Parhar static void
drain_wrq_wr_list(struct adapter * sc,struct sge_wrq * wrq)21497951040fSNavdeep Parhar drain_wrq_wr_list(struct adapter *sc, struct sge_wrq *wrq)
21507951040fSNavdeep Parhar {
21517951040fSNavdeep Parhar struct sge_eq *eq = &wrq->eq;
21527951040fSNavdeep Parhar u_int available, dbdiff; /* # of hardware descriptors */
21537951040fSNavdeep Parhar u_int n;
21547951040fSNavdeep Parhar struct wrqe *wr;
21557951040fSNavdeep Parhar struct fw_eth_tx_pkt_wr *dst; /* any fw WR struct will do */
21567951040fSNavdeep Parhar
21577951040fSNavdeep Parhar EQ_LOCK_ASSERT_OWNED(eq);
21587951040fSNavdeep Parhar MPASS(TAILQ_EMPTY(&wrq->incomplete_wrs));
21597951040fSNavdeep Parhar wr = STAILQ_FIRST(&wrq->wr_list);
21607951040fSNavdeep Parhar MPASS(wr != NULL); /* Must be called with something useful to do */
2161cda2ab0eSNavdeep Parhar MPASS(eq->pidx == eq->dbidx);
2162cda2ab0eSNavdeep Parhar dbdiff = 0;
21637951040fSNavdeep Parhar
21647951040fSNavdeep Parhar do {
21657951040fSNavdeep Parhar eq->cidx = read_hw_cidx(eq);
21667951040fSNavdeep Parhar if (eq->pidx == eq->cidx)
21677951040fSNavdeep Parhar available = eq->sidx - 1;
21687951040fSNavdeep Parhar else
21697951040fSNavdeep Parhar available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
21707951040fSNavdeep Parhar
21717951040fSNavdeep Parhar MPASS(wr->wrq == wrq);
21727951040fSNavdeep Parhar n = howmany(wr->wr_len, EQ_ESIZE);
21737951040fSNavdeep Parhar if (available < n)
2174cda2ab0eSNavdeep Parhar break;
21757951040fSNavdeep Parhar
21767951040fSNavdeep Parhar dst = (void *)&eq->desc[eq->pidx];
21777951040fSNavdeep Parhar if (__predict_true(eq->sidx - eq->pidx > n)) {
21787951040fSNavdeep Parhar /* Won't wrap, won't end exactly at the status page. */
21797951040fSNavdeep Parhar bcopy(&wr->wr[0], dst, wr->wr_len);
21807951040fSNavdeep Parhar eq->pidx += n;
21817951040fSNavdeep Parhar } else {
21827951040fSNavdeep Parhar int first_portion = (eq->sidx - eq->pidx) * EQ_ESIZE;
21837951040fSNavdeep Parhar
21847951040fSNavdeep Parhar bcopy(&wr->wr[0], dst, first_portion);
21857951040fSNavdeep Parhar if (wr->wr_len > first_portion) {
21867951040fSNavdeep Parhar bcopy(&wr->wr[first_portion], &eq->desc[0],
21877951040fSNavdeep Parhar wr->wr_len - first_portion);
21887951040fSNavdeep Parhar }
21897951040fSNavdeep Parhar eq->pidx = n - (eq->sidx - eq->pidx);
21907951040fSNavdeep Parhar }
21910459a175SNavdeep Parhar wrq->tx_wrs_copied++;
21927951040fSNavdeep Parhar
21937951040fSNavdeep Parhar if (available < eq->sidx / 4 &&
21947951040fSNavdeep Parhar atomic_cmpset_int(&eq->equiq, 0, 1)) {
2195ddf09ad6SNavdeep Parhar /*
2196ddf09ad6SNavdeep Parhar * XXX: This is not 100% reliable with some
2197ddf09ad6SNavdeep Parhar * types of WRs. But this is a very unusual
2198ddf09ad6SNavdeep Parhar * situation for an ofld/ctrl queue anyway.
2199ddf09ad6SNavdeep Parhar */
22007951040fSNavdeep Parhar dst->equiq_to_len16 |= htobe32(F_FW_WR_EQUIQ |
22017951040fSNavdeep Parhar F_FW_WR_EQUEQ);
22027951040fSNavdeep Parhar }
22037951040fSNavdeep Parhar
22047951040fSNavdeep Parhar dbdiff += n;
22057951040fSNavdeep Parhar if (dbdiff >= 16) {
22067951040fSNavdeep Parhar ring_eq_db(sc, eq, dbdiff);
22077951040fSNavdeep Parhar dbdiff = 0;
22087951040fSNavdeep Parhar }
22097951040fSNavdeep Parhar
22107951040fSNavdeep Parhar STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
22117951040fSNavdeep Parhar free_wrqe(wr);
22127951040fSNavdeep Parhar MPASS(wrq->nwr_pending > 0);
22137951040fSNavdeep Parhar wrq->nwr_pending--;
22147951040fSNavdeep Parhar MPASS(wrq->ndesc_needed >= n);
22157951040fSNavdeep Parhar wrq->ndesc_needed -= n;
22167951040fSNavdeep Parhar } while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL);
22177951040fSNavdeep Parhar
22187951040fSNavdeep Parhar if (dbdiff)
22197951040fSNavdeep Parhar ring_eq_db(sc, eq, dbdiff);
22207951040fSNavdeep Parhar }
22217951040fSNavdeep Parhar
22227951040fSNavdeep Parhar /*
2223733b9277SNavdeep Parhar * Doesn't fail. Holds on to work requests it can't send right away.
2224733b9277SNavdeep Parhar */
222509fe6320SNavdeep Parhar void
t4_wrq_tx_locked(struct adapter * sc,struct sge_wrq * wrq,struct wrqe * wr)222609fe6320SNavdeep Parhar t4_wrq_tx_locked(struct adapter *sc, struct sge_wrq *wrq, struct wrqe *wr)
2227733b9277SNavdeep Parhar {
2228733b9277SNavdeep Parhar #ifdef INVARIANTS
22297951040fSNavdeep Parhar struct sge_eq *eq = &wrq->eq;
2230733b9277SNavdeep Parhar #endif
2231733b9277SNavdeep Parhar
22327951040fSNavdeep Parhar EQ_LOCK_ASSERT_OWNED(eq);
22337951040fSNavdeep Parhar MPASS(wr != NULL);
22347951040fSNavdeep Parhar MPASS(wr->wr_len > 0 && wr->wr_len <= SGE_MAX_WR_LEN);
22357951040fSNavdeep Parhar MPASS((wr->wr_len & 0x7) == 0);
2236733b9277SNavdeep Parhar
22377951040fSNavdeep Parhar STAILQ_INSERT_TAIL(&wrq->wr_list, wr, link);
22387951040fSNavdeep Parhar wrq->nwr_pending++;
22397951040fSNavdeep Parhar wrq->ndesc_needed += howmany(wr->wr_len, EQ_ESIZE);
2240733b9277SNavdeep Parhar
22417951040fSNavdeep Parhar if (!TAILQ_EMPTY(&wrq->incomplete_wrs))
22427951040fSNavdeep Parhar return; /* commit_wrq_wr will drain wr_list as well. */
2243733b9277SNavdeep Parhar
22447951040fSNavdeep Parhar drain_wrq_wr_list(sc, wrq);
2245733b9277SNavdeep Parhar
22467951040fSNavdeep Parhar /* Doorbell must have caught up to the pidx. */
22477951040fSNavdeep Parhar MPASS(eq->pidx == eq->dbidx);
224854e4ee71SNavdeep Parhar }
224954e4ee71SNavdeep Parhar
225054e4ee71SNavdeep Parhar void
t4_update_fl_bufsize(if_t ifp)2251954712e8SJustin Hibbits t4_update_fl_bufsize(if_t ifp)
225254e4ee71SNavdeep Parhar {
2253954712e8SJustin Hibbits struct vi_info *vi = if_getsoftc(ifp);
22547c228be3SNavdeep Parhar struct adapter *sc = vi->adapter;
225554e4ee71SNavdeep Parhar struct sge_rxq *rxq;
22566eb3180fSNavdeep Parhar #ifdef TCP_OFFLOAD
22576eb3180fSNavdeep Parhar struct sge_ofld_rxq *ofld_rxq;
22586eb3180fSNavdeep Parhar #endif
225954e4ee71SNavdeep Parhar struct sge_fl *fl;
22606a59b994SNavdeep Parhar int i, maxp;
226154e4ee71SNavdeep Parhar
22626a59b994SNavdeep Parhar maxp = max_rx_payload(sc, ifp, false);
2263fe2ebb76SJohn Baldwin for_each_rxq(vi, i, rxq) {
226454e4ee71SNavdeep Parhar fl = &rxq->fl;
226554e4ee71SNavdeep Parhar
226654e4ee71SNavdeep Parhar FL_LOCK(fl);
226746e1e307SNavdeep Parhar fl->zidx = find_refill_source(sc, maxp,
226846e1e307SNavdeep Parhar fl->flags & FL_BUF_PACKING);
226954e4ee71SNavdeep Parhar FL_UNLOCK(fl);
227054e4ee71SNavdeep Parhar }
22716eb3180fSNavdeep Parhar #ifdef TCP_OFFLOAD
22726a59b994SNavdeep Parhar maxp = max_rx_payload(sc, ifp, true);
2273fe2ebb76SJohn Baldwin for_each_ofld_rxq(vi, i, ofld_rxq) {
22746eb3180fSNavdeep Parhar fl = &ofld_rxq->fl;
22756eb3180fSNavdeep Parhar
22766eb3180fSNavdeep Parhar FL_LOCK(fl);
227746e1e307SNavdeep Parhar fl->zidx = find_refill_source(sc, maxp,
227846e1e307SNavdeep Parhar fl->flags & FL_BUF_PACKING);
22796eb3180fSNavdeep Parhar FL_UNLOCK(fl);
22806eb3180fSNavdeep Parhar }
22816eb3180fSNavdeep Parhar #endif
228254e4ee71SNavdeep Parhar }
228354e4ee71SNavdeep Parhar
2284786099deSNavdeep Parhar #ifdef RATELIMIT
2285786099deSNavdeep Parhar static inline int
mbuf_eo_nsegs(struct mbuf * m)2286786099deSNavdeep Parhar mbuf_eo_nsegs(struct mbuf *m)
2287786099deSNavdeep Parhar {
2288786099deSNavdeep Parhar
2289786099deSNavdeep Parhar M_ASSERTPKTHDR(m);
2290786099deSNavdeep Parhar return (m->m_pkthdr.PH_loc.eight[1]);
2291786099deSNavdeep Parhar }
2292786099deSNavdeep Parhar
2293ffbb373cSNavdeep Parhar #if defined(INET) || defined(INET6)
2294786099deSNavdeep Parhar static inline void
set_mbuf_eo_nsegs(struct mbuf * m,uint8_t nsegs)2295786099deSNavdeep Parhar set_mbuf_eo_nsegs(struct mbuf *m, uint8_t nsegs)
2296786099deSNavdeep Parhar {
2297786099deSNavdeep Parhar
2298786099deSNavdeep Parhar M_ASSERTPKTHDR(m);
2299786099deSNavdeep Parhar m->m_pkthdr.PH_loc.eight[1] = nsegs;
2300786099deSNavdeep Parhar }
2301ffbb373cSNavdeep Parhar #endif
2302786099deSNavdeep Parhar
2303786099deSNavdeep Parhar static inline int
mbuf_eo_len16(struct mbuf * m)2304786099deSNavdeep Parhar mbuf_eo_len16(struct mbuf *m)
2305786099deSNavdeep Parhar {
2306786099deSNavdeep Parhar int n;
2307786099deSNavdeep Parhar
2308786099deSNavdeep Parhar M_ASSERTPKTHDR(m);
2309786099deSNavdeep Parhar n = m->m_pkthdr.PH_loc.eight[2];
2310786099deSNavdeep Parhar MPASS(n > 0 && n <= SGE_MAX_WR_LEN / 16);
2311786099deSNavdeep Parhar
2312786099deSNavdeep Parhar return (n);
2313786099deSNavdeep Parhar }
2314786099deSNavdeep Parhar
2315ffbb373cSNavdeep Parhar #if defined(INET) || defined(INET6)
2316786099deSNavdeep Parhar static inline void
set_mbuf_eo_len16(struct mbuf * m,uint8_t len16)2317786099deSNavdeep Parhar set_mbuf_eo_len16(struct mbuf *m, uint8_t len16)
2318786099deSNavdeep Parhar {
2319786099deSNavdeep Parhar
2320786099deSNavdeep Parhar M_ASSERTPKTHDR(m);
2321786099deSNavdeep Parhar m->m_pkthdr.PH_loc.eight[2] = len16;
2322786099deSNavdeep Parhar }
2323ffbb373cSNavdeep Parhar #endif
2324786099deSNavdeep Parhar
2325786099deSNavdeep Parhar static inline int
mbuf_eo_tsclk_tsoff(struct mbuf * m)2326786099deSNavdeep Parhar mbuf_eo_tsclk_tsoff(struct mbuf *m)
2327786099deSNavdeep Parhar {
2328786099deSNavdeep Parhar
2329786099deSNavdeep Parhar M_ASSERTPKTHDR(m);
2330786099deSNavdeep Parhar return (m->m_pkthdr.PH_loc.eight[3]);
2331786099deSNavdeep Parhar }
2332786099deSNavdeep Parhar
2333ffbb373cSNavdeep Parhar #if defined(INET) || defined(INET6)
2334786099deSNavdeep Parhar static inline void
set_mbuf_eo_tsclk_tsoff(struct mbuf * m,uint8_t tsclk_tsoff)2335786099deSNavdeep Parhar set_mbuf_eo_tsclk_tsoff(struct mbuf *m, uint8_t tsclk_tsoff)
2336786099deSNavdeep Parhar {
2337786099deSNavdeep Parhar
2338786099deSNavdeep Parhar M_ASSERTPKTHDR(m);
2339786099deSNavdeep Parhar m->m_pkthdr.PH_loc.eight[3] = tsclk_tsoff;
2340786099deSNavdeep Parhar }
2341ffbb373cSNavdeep Parhar #endif
2342786099deSNavdeep Parhar
2343786099deSNavdeep Parhar static inline int
needs_eo(struct m_snd_tag * mst)234456fb710fSJohn Baldwin needs_eo(struct m_snd_tag *mst)
2345786099deSNavdeep Parhar {
2346786099deSNavdeep Parhar
2347c782ea8bSJohn Baldwin return (mst != NULL && mst->sw->type == IF_SND_TAG_TYPE_RATE_LIMIT);
2348786099deSNavdeep Parhar }
2349786099deSNavdeep Parhar #endif
2350786099deSNavdeep Parhar
23515cdaef71SJohn Baldwin /*
23525cdaef71SJohn Baldwin * Try to allocate an mbuf to contain a raw work request. To make it
23535cdaef71SJohn Baldwin * easy to construct the work request, don't allocate a chain but a
23545cdaef71SJohn Baldwin * single mbuf.
23555cdaef71SJohn Baldwin */
23565cdaef71SJohn Baldwin struct mbuf *
alloc_wr_mbuf(int len,int how)23575cdaef71SJohn Baldwin alloc_wr_mbuf(int len, int how)
23585cdaef71SJohn Baldwin {
23595cdaef71SJohn Baldwin struct mbuf *m;
23605cdaef71SJohn Baldwin
23615cdaef71SJohn Baldwin if (len <= MHLEN)
23625cdaef71SJohn Baldwin m = m_gethdr(how, MT_DATA);
23635cdaef71SJohn Baldwin else if (len <= MCLBYTES)
23645cdaef71SJohn Baldwin m = m_getcl(how, MT_DATA, M_PKTHDR);
23655cdaef71SJohn Baldwin else
23665cdaef71SJohn Baldwin m = NULL;
23675cdaef71SJohn Baldwin if (m == NULL)
23685cdaef71SJohn Baldwin return (NULL);
23695cdaef71SJohn Baldwin m->m_pkthdr.len = len;
23705cdaef71SJohn Baldwin m->m_len = len;
23715cdaef71SJohn Baldwin set_mbuf_cflags(m, MC_RAW_WR);
23725cdaef71SJohn Baldwin set_mbuf_len16(m, howmany(len, 16));
23735cdaef71SJohn Baldwin return (m);
23745cdaef71SJohn Baldwin }
23755cdaef71SJohn Baldwin
2376a4a4ad2dSNavdeep Parhar static inline bool
needs_hwcsum(struct mbuf * m)2377c0236bd9SNavdeep Parhar needs_hwcsum(struct mbuf *m)
2378c0236bd9SNavdeep Parhar {
2379a4a4ad2dSNavdeep Parhar const uint32_t csum_flags = CSUM_IP | CSUM_IP_UDP | CSUM_IP_TCP |
2380a4a4ad2dSNavdeep Parhar CSUM_IP_TSO | CSUM_INNER_IP | CSUM_INNER_IP_UDP |
2381a4a4ad2dSNavdeep Parhar CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO | CSUM_IP6_UDP |
2382a4a4ad2dSNavdeep Parhar CSUM_IP6_TCP | CSUM_IP6_TSO | CSUM_INNER_IP6_UDP |
2383a4a4ad2dSNavdeep Parhar CSUM_INNER_IP6_TCP | CSUM_INNER_IP6_TSO;
2384c0236bd9SNavdeep Parhar
2385c0236bd9SNavdeep Parhar M_ASSERTPKTHDR(m);
2386c0236bd9SNavdeep Parhar
2387a4a4ad2dSNavdeep Parhar return (m->m_pkthdr.csum_flags & csum_flags);
2388c0236bd9SNavdeep Parhar }
2389c0236bd9SNavdeep Parhar
2390a4a4ad2dSNavdeep Parhar static inline bool
needs_tso(struct mbuf * m)23917951040fSNavdeep Parhar needs_tso(struct mbuf *m)
23927951040fSNavdeep Parhar {
2393a4a4ad2dSNavdeep Parhar const uint32_t csum_flags = CSUM_IP_TSO | CSUM_IP6_TSO |
2394a4a4ad2dSNavdeep Parhar CSUM_INNER_IP_TSO | CSUM_INNER_IP6_TSO;
23957951040fSNavdeep Parhar
23967951040fSNavdeep Parhar M_ASSERTPKTHDR(m);
23977951040fSNavdeep Parhar
2398a4a4ad2dSNavdeep Parhar return (m->m_pkthdr.csum_flags & csum_flags);
23997951040fSNavdeep Parhar }
24007951040fSNavdeep Parhar
2401a4a4ad2dSNavdeep Parhar static inline bool
needs_vxlan_csum(struct mbuf * m)2402a4a4ad2dSNavdeep Parhar needs_vxlan_csum(struct mbuf *m)
2403a4a4ad2dSNavdeep Parhar {
2404a4a4ad2dSNavdeep Parhar
2405a4a4ad2dSNavdeep Parhar M_ASSERTPKTHDR(m);
2406a4a4ad2dSNavdeep Parhar
2407a4a4ad2dSNavdeep Parhar return (m->m_pkthdr.csum_flags & CSUM_ENCAP_VXLAN);
2408a4a4ad2dSNavdeep Parhar }
2409a4a4ad2dSNavdeep Parhar
2410a4a4ad2dSNavdeep Parhar static inline bool
needs_vxlan_tso(struct mbuf * m)2411a4a4ad2dSNavdeep Parhar needs_vxlan_tso(struct mbuf *m)
2412a4a4ad2dSNavdeep Parhar {
2413a4a4ad2dSNavdeep Parhar const uint32_t csum_flags = CSUM_ENCAP_VXLAN | CSUM_INNER_IP_TSO |
2414a4a4ad2dSNavdeep Parhar CSUM_INNER_IP6_TSO;
2415a4a4ad2dSNavdeep Parhar
2416a4a4ad2dSNavdeep Parhar M_ASSERTPKTHDR(m);
2417a4a4ad2dSNavdeep Parhar
2418a4a4ad2dSNavdeep Parhar return ((m->m_pkthdr.csum_flags & csum_flags) != 0 &&
2419a4a4ad2dSNavdeep Parhar (m->m_pkthdr.csum_flags & csum_flags) != CSUM_ENCAP_VXLAN);
2420a4a4ad2dSNavdeep Parhar }
2421a4a4ad2dSNavdeep Parhar
2422ffbb373cSNavdeep Parhar #if defined(INET) || defined(INET6)
2423a4a4ad2dSNavdeep Parhar static inline bool
needs_inner_tcp_csum(struct mbuf * m)2424a4a4ad2dSNavdeep Parhar needs_inner_tcp_csum(struct mbuf *m)
2425a4a4ad2dSNavdeep Parhar {
2426a4a4ad2dSNavdeep Parhar const uint32_t csum_flags = CSUM_INNER_IP_TSO | CSUM_INNER_IP6_TSO;
2427a4a4ad2dSNavdeep Parhar
2428a4a4ad2dSNavdeep Parhar M_ASSERTPKTHDR(m);
2429a4a4ad2dSNavdeep Parhar
2430a4a4ad2dSNavdeep Parhar return (m->m_pkthdr.csum_flags & csum_flags);
2431a4a4ad2dSNavdeep Parhar }
2432ffbb373cSNavdeep Parhar #endif
2433a4a4ad2dSNavdeep Parhar
2434a4a4ad2dSNavdeep Parhar static inline bool
needs_l3_csum(struct mbuf * m)24357951040fSNavdeep Parhar needs_l3_csum(struct mbuf *m)
24367951040fSNavdeep Parhar {
2437a4a4ad2dSNavdeep Parhar const uint32_t csum_flags = CSUM_IP | CSUM_IP_TSO | CSUM_INNER_IP |
2438a4a4ad2dSNavdeep Parhar CSUM_INNER_IP_TSO;
24397951040fSNavdeep Parhar
24407951040fSNavdeep Parhar M_ASSERTPKTHDR(m);
24417951040fSNavdeep Parhar
2442a4a4ad2dSNavdeep Parhar return (m->m_pkthdr.csum_flags & csum_flags);
24437951040fSNavdeep Parhar }
24447951040fSNavdeep Parhar
2445a4a4ad2dSNavdeep Parhar static inline bool
needs_outer_tcp_csum(struct mbuf * m)2446a4a4ad2dSNavdeep Parhar needs_outer_tcp_csum(struct mbuf *m)
2447c0236bd9SNavdeep Parhar {
2448a4a4ad2dSNavdeep Parhar const uint32_t csum_flags = CSUM_IP_TCP | CSUM_IP_TSO | CSUM_IP6_TCP |
2449a4a4ad2dSNavdeep Parhar CSUM_IP6_TSO;
2450c0236bd9SNavdeep Parhar
2451c0236bd9SNavdeep Parhar M_ASSERTPKTHDR(m);
2452a4a4ad2dSNavdeep Parhar
2453a4a4ad2dSNavdeep Parhar return (m->m_pkthdr.csum_flags & csum_flags);
2454c0236bd9SNavdeep Parhar }
2455c0236bd9SNavdeep Parhar
2456c0236bd9SNavdeep Parhar #ifdef RATELIMIT
2457a4a4ad2dSNavdeep Parhar static inline bool
needs_outer_l4_csum(struct mbuf * m)2458a4a4ad2dSNavdeep Parhar needs_outer_l4_csum(struct mbuf *m)
24597951040fSNavdeep Parhar {
2460a4a4ad2dSNavdeep Parhar const uint32_t csum_flags = CSUM_IP_UDP | CSUM_IP_TCP | CSUM_IP_TSO |
2461a4a4ad2dSNavdeep Parhar CSUM_IP6_UDP | CSUM_IP6_TCP | CSUM_IP6_TSO;
24627951040fSNavdeep Parhar
24637951040fSNavdeep Parhar M_ASSERTPKTHDR(m);
24647951040fSNavdeep Parhar
2465a4a4ad2dSNavdeep Parhar return (m->m_pkthdr.csum_flags & csum_flags);
24667951040fSNavdeep Parhar }
24677951040fSNavdeep Parhar
2468a4a4ad2dSNavdeep Parhar static inline bool
needs_outer_udp_csum(struct mbuf * m)2469a4a4ad2dSNavdeep Parhar needs_outer_udp_csum(struct mbuf *m)
2470786099deSNavdeep Parhar {
2471a4a4ad2dSNavdeep Parhar const uint32_t csum_flags = CSUM_IP_UDP | CSUM_IP6_UDP;
2472786099deSNavdeep Parhar
2473786099deSNavdeep Parhar M_ASSERTPKTHDR(m);
2474a4a4ad2dSNavdeep Parhar
2475a4a4ad2dSNavdeep Parhar return (m->m_pkthdr.csum_flags & csum_flags);
2476786099deSNavdeep Parhar }
2477c3fce948SNavdeep Parhar #endif
2478786099deSNavdeep Parhar
2479a4a4ad2dSNavdeep Parhar static inline bool
needs_vlan_insertion(struct mbuf * m)24807951040fSNavdeep Parhar needs_vlan_insertion(struct mbuf *m)
24817951040fSNavdeep Parhar {
24827951040fSNavdeep Parhar
24837951040fSNavdeep Parhar M_ASSERTPKTHDR(m);
24847951040fSNavdeep Parhar
2485a6a8ff35SNavdeep Parhar return (m->m_flags & M_VLANTAG);
24867951040fSNavdeep Parhar }
24877951040fSNavdeep Parhar
248894e6b3feSNavdeep Parhar #if defined(INET) || defined(INET6)
24897951040fSNavdeep Parhar static void *
m_advance(struct mbuf ** pm,int * poffset,int len)24907951040fSNavdeep Parhar m_advance(struct mbuf **pm, int *poffset, int len)
24917951040fSNavdeep Parhar {
24927951040fSNavdeep Parhar struct mbuf *m = *pm;
24937951040fSNavdeep Parhar int offset = *poffset;
24947951040fSNavdeep Parhar uintptr_t p = 0;
24957951040fSNavdeep Parhar
24967951040fSNavdeep Parhar MPASS(len > 0);
24977951040fSNavdeep Parhar
2498e06ab612SJohn Baldwin for (;;) {
24997951040fSNavdeep Parhar if (offset + len < m->m_len) {
25007951040fSNavdeep Parhar offset += len;
25017951040fSNavdeep Parhar p = mtod(m, uintptr_t) + offset;
25027951040fSNavdeep Parhar break;
25037951040fSNavdeep Parhar }
25047951040fSNavdeep Parhar len -= m->m_len - offset;
25057951040fSNavdeep Parhar m = m->m_next;
25067951040fSNavdeep Parhar offset = 0;
25077951040fSNavdeep Parhar MPASS(m != NULL);
25087951040fSNavdeep Parhar }
25097951040fSNavdeep Parhar *poffset = offset;
25107951040fSNavdeep Parhar *pm = m;
25117951040fSNavdeep Parhar return ((void *)p);
25127951040fSNavdeep Parhar }
251394e6b3feSNavdeep Parhar #endif
25147951040fSNavdeep Parhar
2515d76bbe17SJohn Baldwin static inline int
count_mbuf_ext_pgs(struct mbuf * m,int skip,vm_paddr_t * nextaddr)2516d76bbe17SJohn Baldwin count_mbuf_ext_pgs(struct mbuf *m, int skip, vm_paddr_t *nextaddr)
2517d76bbe17SJohn Baldwin {
2518d76bbe17SJohn Baldwin vm_paddr_t paddr;
2519d76bbe17SJohn Baldwin int i, len, off, pglen, pgoff, seglen, segoff;
2520d76bbe17SJohn Baldwin int nsegs = 0;
2521d76bbe17SJohn Baldwin
2522365e8da4SGleb Smirnoff M_ASSERTEXTPG(m);
2523d76bbe17SJohn Baldwin off = mtod(m, vm_offset_t);
2524d76bbe17SJohn Baldwin len = m->m_len;
2525d76bbe17SJohn Baldwin off += skip;
2526d76bbe17SJohn Baldwin len -= skip;
2527d76bbe17SJohn Baldwin
25287b6c99d0SGleb Smirnoff if (m->m_epg_hdrlen != 0) {
25297b6c99d0SGleb Smirnoff if (off >= m->m_epg_hdrlen) {
25307b6c99d0SGleb Smirnoff off -= m->m_epg_hdrlen;
2531d76bbe17SJohn Baldwin } else {
25327b6c99d0SGleb Smirnoff seglen = m->m_epg_hdrlen - off;
2533d76bbe17SJohn Baldwin segoff = off;
2534d76bbe17SJohn Baldwin seglen = min(seglen, len);
2535d76bbe17SJohn Baldwin off = 0;
2536d76bbe17SJohn Baldwin len -= seglen;
2537d76bbe17SJohn Baldwin paddr = pmap_kextract(
25380c103266SGleb Smirnoff (vm_offset_t)&m->m_epg_hdr[segoff]);
2539d76bbe17SJohn Baldwin if (*nextaddr != paddr)
2540d76bbe17SJohn Baldwin nsegs++;
2541d76bbe17SJohn Baldwin *nextaddr = paddr + seglen;
2542d76bbe17SJohn Baldwin }
2543d76bbe17SJohn Baldwin }
25447b6c99d0SGleb Smirnoff pgoff = m->m_epg_1st_off;
25457b6c99d0SGleb Smirnoff for (i = 0; i < m->m_epg_npgs && len > 0; i++) {
2546c4ee38f8SGleb Smirnoff pglen = m_epg_pagelen(m, i, pgoff);
2547d76bbe17SJohn Baldwin if (off >= pglen) {
2548d76bbe17SJohn Baldwin off -= pglen;
2549d76bbe17SJohn Baldwin pgoff = 0;
2550d76bbe17SJohn Baldwin continue;
2551d76bbe17SJohn Baldwin }
2552d76bbe17SJohn Baldwin seglen = pglen - off;
2553d76bbe17SJohn Baldwin segoff = pgoff + off;
2554d76bbe17SJohn Baldwin off = 0;
2555d76bbe17SJohn Baldwin seglen = min(seglen, len);
2556d76bbe17SJohn Baldwin len -= seglen;
25570c103266SGleb Smirnoff paddr = m->m_epg_pa[i] + segoff;
2558d76bbe17SJohn Baldwin if (*nextaddr != paddr)
2559d76bbe17SJohn Baldwin nsegs++;
2560d76bbe17SJohn Baldwin *nextaddr = paddr + seglen;
2561d76bbe17SJohn Baldwin pgoff = 0;
2562d76bbe17SJohn Baldwin };
2563d76bbe17SJohn Baldwin if (len != 0) {
25647b6c99d0SGleb Smirnoff seglen = min(len, m->m_epg_trllen - off);
2565d76bbe17SJohn Baldwin len -= seglen;
25660c103266SGleb Smirnoff paddr = pmap_kextract((vm_offset_t)&m->m_epg_trail[off]);
2567d76bbe17SJohn Baldwin if (*nextaddr != paddr)
2568d76bbe17SJohn Baldwin nsegs++;
2569d76bbe17SJohn Baldwin *nextaddr = paddr + seglen;
2570d76bbe17SJohn Baldwin }
2571d76bbe17SJohn Baldwin
2572d76bbe17SJohn Baldwin return (nsegs);
2573d76bbe17SJohn Baldwin }
2574d76bbe17SJohn Baldwin
2575d76bbe17SJohn Baldwin
25767951040fSNavdeep Parhar /*
25777951040fSNavdeep Parhar * Can deal with empty mbufs in the chain that have m_len = 0, but the chain
2578786099deSNavdeep Parhar * must have at least one mbuf that's not empty. It is possible for this
2579786099deSNavdeep Parhar * routine to return 0 if skip accounts for all the contents of the mbuf chain.
25807951040fSNavdeep Parhar */
25817951040fSNavdeep Parhar static inline int
count_mbuf_nsegs(struct mbuf * m,int skip,uint8_t * cflags)2582d76bbe17SJohn Baldwin count_mbuf_nsegs(struct mbuf *m, int skip, uint8_t *cflags)
25837951040fSNavdeep Parhar {
2584d76bbe17SJohn Baldwin vm_paddr_t nextaddr, paddr;
258577e9044cSNavdeep Parhar vm_offset_t va;
25867951040fSNavdeep Parhar int len, nsegs;
25877951040fSNavdeep Parhar
2588786099deSNavdeep Parhar M_ASSERTPKTHDR(m);
2589786099deSNavdeep Parhar MPASS(m->m_pkthdr.len > 0);
2590786099deSNavdeep Parhar MPASS(m->m_pkthdr.len >= skip);
25917951040fSNavdeep Parhar
25927951040fSNavdeep Parhar nsegs = 0;
2593d76bbe17SJohn Baldwin nextaddr = 0;
25947951040fSNavdeep Parhar for (; m; m = m->m_next) {
25957951040fSNavdeep Parhar len = m->m_len;
25967951040fSNavdeep Parhar if (__predict_false(len == 0))
25977951040fSNavdeep Parhar continue;
2598786099deSNavdeep Parhar if (skip >= len) {
2599786099deSNavdeep Parhar skip -= len;
2600786099deSNavdeep Parhar continue;
2601786099deSNavdeep Parhar }
26026edfd179SGleb Smirnoff if ((m->m_flags & M_EXTPG) != 0) {
2603d76bbe17SJohn Baldwin *cflags |= MC_NOMAP;
2604d76bbe17SJohn Baldwin nsegs += count_mbuf_ext_pgs(m, skip, &nextaddr);
2605d76bbe17SJohn Baldwin skip = 0;
2606d76bbe17SJohn Baldwin continue;
2607d76bbe17SJohn Baldwin }
2608786099deSNavdeep Parhar va = mtod(m, vm_offset_t) + skip;
2609786099deSNavdeep Parhar len -= skip;
2610786099deSNavdeep Parhar skip = 0;
2611d76bbe17SJohn Baldwin paddr = pmap_kextract(va);
2612786099deSNavdeep Parhar nsegs += sglist_count((void *)(uintptr_t)va, len);
2613d76bbe17SJohn Baldwin if (paddr == nextaddr)
26147951040fSNavdeep Parhar nsegs--;
2615d76bbe17SJohn Baldwin nextaddr = pmap_kextract(va + len - 1) + 1;
26167951040fSNavdeep Parhar }
26177951040fSNavdeep Parhar
26187951040fSNavdeep Parhar return (nsegs);
26197951040fSNavdeep Parhar }
26207951040fSNavdeep Parhar
26217951040fSNavdeep Parhar /*
2622a4a4ad2dSNavdeep Parhar * The maximum number of segments that can fit in a WR.
2623a4a4ad2dSNavdeep Parhar */
2624a4a4ad2dSNavdeep Parhar static int
max_nsegs_allowed(struct mbuf * m,bool vm_wr)262530e3f2b4SNavdeep Parhar max_nsegs_allowed(struct mbuf *m, bool vm_wr)
2626a4a4ad2dSNavdeep Parhar {
2627a4a4ad2dSNavdeep Parhar
262830e3f2b4SNavdeep Parhar if (vm_wr) {
262930e3f2b4SNavdeep Parhar if (needs_tso(m))
263030e3f2b4SNavdeep Parhar return (TX_SGL_SEGS_VM_TSO);
263130e3f2b4SNavdeep Parhar return (TX_SGL_SEGS_VM);
263230e3f2b4SNavdeep Parhar }
263330e3f2b4SNavdeep Parhar
2634a4a4ad2dSNavdeep Parhar if (needs_tso(m)) {
2635a4a4ad2dSNavdeep Parhar if (needs_vxlan_tso(m))
2636a4a4ad2dSNavdeep Parhar return (TX_SGL_SEGS_VXLAN_TSO);
2637a4a4ad2dSNavdeep Parhar else
2638a4a4ad2dSNavdeep Parhar return (TX_SGL_SEGS_TSO);
2639a4a4ad2dSNavdeep Parhar }
2640a4a4ad2dSNavdeep Parhar
2641a4a4ad2dSNavdeep Parhar return (TX_SGL_SEGS);
2642a4a4ad2dSNavdeep Parhar }
2643a4a4ad2dSNavdeep Parhar
2644b9820bcaSNavdeep Parhar static struct timeval txerr_ratecheck = {0};
2645b9820bcaSNavdeep Parhar static const struct timeval txerr_interval = {3, 0};
2646b9820bcaSNavdeep Parhar
2647a4a4ad2dSNavdeep Parhar /*
26487951040fSNavdeep Parhar * Analyze the mbuf to determine its tx needs. The mbuf passed in may change:
26497951040fSNavdeep Parhar * a) caller can assume it's been freed if this function returns with an error.
26507951040fSNavdeep Parhar * b) it may get defragged up if the gather list is too long for the hardware.
26517951040fSNavdeep Parhar */
26527951040fSNavdeep Parhar int
parse_pkt(struct mbuf ** mp,bool vm_wr)265330e3f2b4SNavdeep Parhar parse_pkt(struct mbuf **mp, bool vm_wr)
26547951040fSNavdeep Parhar {
26557951040fSNavdeep Parhar struct mbuf *m0 = *mp, *m;
265639d5cbdcSNavdeep Parhar int rc, nsegs, defragged = 0;
26577951040fSNavdeep Parhar struct ether_header *eh;
265839d5cbdcSNavdeep Parhar #ifdef INET
26597951040fSNavdeep Parhar void *l3hdr;
266039d5cbdcSNavdeep Parhar #endif
26617951040fSNavdeep Parhar #if defined(INET) || defined(INET6)
266239d5cbdcSNavdeep Parhar int offset;
26637951040fSNavdeep Parhar struct tcphdr *tcp;
26647951040fSNavdeep Parhar #endif
2665bddf7343SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
266656fb710fSJohn Baldwin struct m_snd_tag *mst;
2667e38a50e8SJohn Baldwin #endif
26687951040fSNavdeep Parhar uint16_t eh_type;
2669d76bbe17SJohn Baldwin uint8_t cflags;
26707951040fSNavdeep Parhar
2671d76bbe17SJohn Baldwin cflags = 0;
26727951040fSNavdeep Parhar M_ASSERTPKTHDR(m0);
26737951040fSNavdeep Parhar if (__predict_false(m0->m_pkthdr.len < ETHER_HDR_LEN)) {
26747951040fSNavdeep Parhar rc = EINVAL;
26757951040fSNavdeep Parhar fail:
26767951040fSNavdeep Parhar m_freem(m0);
26777951040fSNavdeep Parhar *mp = NULL;
26787951040fSNavdeep Parhar return (rc);
26797951040fSNavdeep Parhar }
26807951040fSNavdeep Parhar restart:
26817951040fSNavdeep Parhar /*
26827951040fSNavdeep Parhar * First count the number of gather list segments in the payload.
26837951040fSNavdeep Parhar * Defrag the mbuf if nsegs exceeds the hardware limit.
26847951040fSNavdeep Parhar */
26857951040fSNavdeep Parhar M_ASSERTPKTHDR(m0);
26867951040fSNavdeep Parhar MPASS(m0->m_pkthdr.len > 0);
2687d76bbe17SJohn Baldwin nsegs = count_mbuf_nsegs(m0, 0, &cflags);
2688bddf7343SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
2689e38a50e8SJohn Baldwin if (m0->m_pkthdr.csum_flags & CSUM_SND_TAG)
269056fb710fSJohn Baldwin mst = m0->m_pkthdr.snd_tag;
2691e38a50e8SJohn Baldwin else
269256fb710fSJohn Baldwin mst = NULL;
2693e38a50e8SJohn Baldwin #endif
2694bddf7343SJohn Baldwin #ifdef KERN_TLS
2695c782ea8bSJohn Baldwin if (mst != NULL && mst->sw->type == IF_SND_TAG_TYPE_TLS) {
2696bddf7343SJohn Baldwin cflags |= MC_TLS;
2697bddf7343SJohn Baldwin set_mbuf_cflags(m0, cflags);
2698ca457729SJohn Baldwin rc = t6_ktls_parse_pkt(m0);
2699bddf7343SJohn Baldwin if (rc != 0)
2700bddf7343SJohn Baldwin goto fail;
2701ca457729SJohn Baldwin return (EINPROGRESS);
2702bddf7343SJohn Baldwin }
2703bddf7343SJohn Baldwin #endif
270430e3f2b4SNavdeep Parhar if (nsegs > max_nsegs_allowed(m0, vm_wr)) {
27057054f6ecSNavdeep Parhar if (defragged++ > 0) {
27067951040fSNavdeep Parhar rc = EFBIG;
27077951040fSNavdeep Parhar goto fail;
27087951040fSNavdeep Parhar }
27097054f6ecSNavdeep Parhar counter_u64_add(defrags, 1);
27107054f6ecSNavdeep Parhar if ((m = m_defrag(m0, M_NOWAIT)) == NULL) {
27117054f6ecSNavdeep Parhar rc = ENOMEM;
27127054f6ecSNavdeep Parhar goto fail;
27137054f6ecSNavdeep Parhar }
27147951040fSNavdeep Parhar *mp = m0 = m; /* update caller's copy after defrag */
27157951040fSNavdeep Parhar goto restart;
27167951040fSNavdeep Parhar }
27177951040fSNavdeep Parhar
2718d76bbe17SJohn Baldwin if (__predict_false(nsegs > 2 && m0->m_pkthdr.len <= MHLEN &&
2719d76bbe17SJohn Baldwin !(cflags & MC_NOMAP))) {
27207054f6ecSNavdeep Parhar counter_u64_add(pullups, 1);
27217951040fSNavdeep Parhar m0 = m_pullup(m0, m0->m_pkthdr.len);
27227951040fSNavdeep Parhar if (m0 == NULL) {
27237951040fSNavdeep Parhar /* Should have left well enough alone. */
27247951040fSNavdeep Parhar rc = EFBIG;
27257951040fSNavdeep Parhar goto fail;
27267951040fSNavdeep Parhar }
27277951040fSNavdeep Parhar *mp = m0; /* update caller's copy after pullup */
27287951040fSNavdeep Parhar goto restart;
27297951040fSNavdeep Parhar }
27307951040fSNavdeep Parhar set_mbuf_nsegs(m0, nsegs);
2731d76bbe17SJohn Baldwin set_mbuf_cflags(m0, cflags);
273230e3f2b4SNavdeep Parhar calculate_mbuf_len16(m0, vm_wr);
27337951040fSNavdeep Parhar
2734786099deSNavdeep Parhar #ifdef RATELIMIT
2735786099deSNavdeep Parhar /*
2736786099deSNavdeep Parhar * Ethofld is limited to TCP and UDP for now, and only when L4 hw
2737a4a4ad2dSNavdeep Parhar * checksumming is enabled. needs_outer_l4_csum happens to check for
2738a4a4ad2dSNavdeep Parhar * all the right things.
2739786099deSNavdeep Parhar */
274056fb710fSJohn Baldwin if (__predict_false(needs_eo(mst) && !needs_outer_l4_csum(m0))) {
2741fb3bc596SJohn Baldwin m_snd_tag_rele(m0->m_pkthdr.snd_tag);
2742786099deSNavdeep Parhar m0->m_pkthdr.snd_tag = NULL;
2743fb3bc596SJohn Baldwin m0->m_pkthdr.csum_flags &= ~CSUM_SND_TAG;
274456fb710fSJohn Baldwin mst = NULL;
2745fb3bc596SJohn Baldwin }
2746786099deSNavdeep Parhar #endif
2747786099deSNavdeep Parhar
2748c0236bd9SNavdeep Parhar if (!needs_hwcsum(m0)
2749786099deSNavdeep Parhar #ifdef RATELIMIT
275056fb710fSJohn Baldwin && !needs_eo(mst)
2751786099deSNavdeep Parhar #endif
2752c0236bd9SNavdeep Parhar )
27537951040fSNavdeep Parhar return (0);
27547951040fSNavdeep Parhar
27557951040fSNavdeep Parhar m = m0;
27567951040fSNavdeep Parhar eh = mtod(m, struct ether_header *);
27577951040fSNavdeep Parhar eh_type = ntohs(eh->ether_type);
27587951040fSNavdeep Parhar if (eh_type == ETHERTYPE_VLAN) {
27597951040fSNavdeep Parhar struct ether_vlan_header *evh = (void *)eh;
27607951040fSNavdeep Parhar
27617951040fSNavdeep Parhar eh_type = ntohs(evh->evl_proto);
27627951040fSNavdeep Parhar m0->m_pkthdr.l2hlen = sizeof(*evh);
27637951040fSNavdeep Parhar } else
27647951040fSNavdeep Parhar m0->m_pkthdr.l2hlen = sizeof(*eh);
27657951040fSNavdeep Parhar
276639d5cbdcSNavdeep Parhar #if defined(INET) || defined(INET6)
27677951040fSNavdeep Parhar offset = 0;
276839d5cbdcSNavdeep Parhar #ifdef INET
27697951040fSNavdeep Parhar l3hdr = m_advance(&m, &offset, m0->m_pkthdr.l2hlen);
277039d5cbdcSNavdeep Parhar #else
277139d5cbdcSNavdeep Parhar m_advance(&m, &offset, m0->m_pkthdr.l2hlen);
277239d5cbdcSNavdeep Parhar #endif
277339d5cbdcSNavdeep Parhar #endif
27747951040fSNavdeep Parhar
27757951040fSNavdeep Parhar switch (eh_type) {
27767951040fSNavdeep Parhar #ifdef INET6
27777951040fSNavdeep Parhar case ETHERTYPE_IPV6:
2778a4a4ad2dSNavdeep Parhar m0->m_pkthdr.l3hlen = sizeof(struct ip6_hdr);
27797951040fSNavdeep Parhar break;
27807951040fSNavdeep Parhar #endif
27817951040fSNavdeep Parhar #ifdef INET
27827951040fSNavdeep Parhar case ETHERTYPE_IP:
27837951040fSNavdeep Parhar {
27847951040fSNavdeep Parhar struct ip *ip = l3hdr;
27857951040fSNavdeep Parhar
2786a4a4ad2dSNavdeep Parhar if (needs_vxlan_csum(m0)) {
2787a4a4ad2dSNavdeep Parhar /* Driver will do the outer IP hdr checksum. */
2788a4a4ad2dSNavdeep Parhar ip->ip_sum = 0;
2789a4a4ad2dSNavdeep Parhar if (needs_vxlan_tso(m0)) {
2790a4a4ad2dSNavdeep Parhar const uint16_t ipl = ip->ip_len;
2791a4a4ad2dSNavdeep Parhar
2792a4a4ad2dSNavdeep Parhar ip->ip_len = 0;
2793a4a4ad2dSNavdeep Parhar ip->ip_sum = ~in_cksum_hdr(ip);
2794a4a4ad2dSNavdeep Parhar ip->ip_len = ipl;
2795a4a4ad2dSNavdeep Parhar } else
2796a4a4ad2dSNavdeep Parhar ip->ip_sum = in_cksum_hdr(ip);
2797a4a4ad2dSNavdeep Parhar }
2798a4a4ad2dSNavdeep Parhar m0->m_pkthdr.l3hlen = ip->ip_hl << 2;
27997951040fSNavdeep Parhar break;
28007951040fSNavdeep Parhar }
28017951040fSNavdeep Parhar #endif
28027951040fSNavdeep Parhar default:
2803b9820bcaSNavdeep Parhar if (ratecheck(&txerr_ratecheck, &txerr_interval)) {
2804b9820bcaSNavdeep Parhar log(LOG_ERR, "%s: ethertype 0x%04x unknown. "
2805b9820bcaSNavdeep Parhar "if_cxgbe must be compiled with the same "
2806b9820bcaSNavdeep Parhar "INET/INET6 options as the kernel.\n", __func__,
2807b9820bcaSNavdeep Parhar eh_type);
2808b9820bcaSNavdeep Parhar }
2809b9820bcaSNavdeep Parhar rc = EINVAL;
2810b9820bcaSNavdeep Parhar goto fail;
28117951040fSNavdeep Parhar }
28127951040fSNavdeep Parhar
281339d5cbdcSNavdeep Parhar #if defined(INET) || defined(INET6)
2814a4a4ad2dSNavdeep Parhar if (needs_vxlan_csum(m0)) {
2815a4a4ad2dSNavdeep Parhar m0->m_pkthdr.l4hlen = sizeof(struct udphdr);
2816a4a4ad2dSNavdeep Parhar m0->m_pkthdr.l5hlen = sizeof(struct vxlan_header);
2817a4a4ad2dSNavdeep Parhar
2818a4a4ad2dSNavdeep Parhar /* Inner headers. */
2819a4a4ad2dSNavdeep Parhar eh = m_advance(&m, &offset, m0->m_pkthdr.l3hlen +
2820a4a4ad2dSNavdeep Parhar sizeof(struct udphdr) + sizeof(struct vxlan_header));
2821a4a4ad2dSNavdeep Parhar eh_type = ntohs(eh->ether_type);
2822a4a4ad2dSNavdeep Parhar if (eh_type == ETHERTYPE_VLAN) {
2823a4a4ad2dSNavdeep Parhar struct ether_vlan_header *evh = (void *)eh;
2824a4a4ad2dSNavdeep Parhar
2825a4a4ad2dSNavdeep Parhar eh_type = ntohs(evh->evl_proto);
2826a4a4ad2dSNavdeep Parhar m0->m_pkthdr.inner_l2hlen = sizeof(*evh);
2827a4a4ad2dSNavdeep Parhar } else
2828a4a4ad2dSNavdeep Parhar m0->m_pkthdr.inner_l2hlen = sizeof(*eh);
282939d5cbdcSNavdeep Parhar #ifdef INET
2830a4a4ad2dSNavdeep Parhar l3hdr = m_advance(&m, &offset, m0->m_pkthdr.inner_l2hlen);
283139d5cbdcSNavdeep Parhar #else
283239d5cbdcSNavdeep Parhar m_advance(&m, &offset, m0->m_pkthdr.inner_l2hlen);
283339d5cbdcSNavdeep Parhar #endif
2834a4a4ad2dSNavdeep Parhar
2835a4a4ad2dSNavdeep Parhar switch (eh_type) {
2836a4a4ad2dSNavdeep Parhar #ifdef INET6
2837a4a4ad2dSNavdeep Parhar case ETHERTYPE_IPV6:
2838a4a4ad2dSNavdeep Parhar m0->m_pkthdr.inner_l3hlen = sizeof(struct ip6_hdr);
2839a4a4ad2dSNavdeep Parhar break;
2840a4a4ad2dSNavdeep Parhar #endif
2841a4a4ad2dSNavdeep Parhar #ifdef INET
2842a4a4ad2dSNavdeep Parhar case ETHERTYPE_IP:
2843a4a4ad2dSNavdeep Parhar {
2844a4a4ad2dSNavdeep Parhar struct ip *ip = l3hdr;
2845a4a4ad2dSNavdeep Parhar
2846a4a4ad2dSNavdeep Parhar m0->m_pkthdr.inner_l3hlen = ip->ip_hl << 2;
2847a4a4ad2dSNavdeep Parhar break;
2848a4a4ad2dSNavdeep Parhar }
2849a4a4ad2dSNavdeep Parhar #endif
2850a4a4ad2dSNavdeep Parhar default:
2851b9820bcaSNavdeep Parhar if (ratecheck(&txerr_ratecheck, &txerr_interval)) {
2852b9820bcaSNavdeep Parhar log(LOG_ERR, "%s: VXLAN hw offload requested"
2853b9820bcaSNavdeep Parhar "with unknown ethertype 0x%04x. if_cxgbe "
2854b9820bcaSNavdeep Parhar "must be compiled with the same INET/INET6 "
2855b9820bcaSNavdeep Parhar "options as the kernel.\n", __func__,
2856b9820bcaSNavdeep Parhar eh_type);
2857b9820bcaSNavdeep Parhar }
2858b9820bcaSNavdeep Parhar rc = EINVAL;
2859b9820bcaSNavdeep Parhar goto fail;
2860a4a4ad2dSNavdeep Parhar }
2861a4a4ad2dSNavdeep Parhar if (needs_inner_tcp_csum(m0)) {
2862a4a4ad2dSNavdeep Parhar tcp = m_advance(&m, &offset, m0->m_pkthdr.inner_l3hlen);
2863a4a4ad2dSNavdeep Parhar m0->m_pkthdr.inner_l4hlen = tcp->th_off * 4;
2864a4a4ad2dSNavdeep Parhar }
2865a4a4ad2dSNavdeep Parhar MPASS((m0->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
2866a4a4ad2dSNavdeep Parhar m0->m_pkthdr.csum_flags &= CSUM_INNER_IP6_UDP |
2867a4a4ad2dSNavdeep Parhar CSUM_INNER_IP6_TCP | CSUM_INNER_IP6_TSO | CSUM_INNER_IP |
2868a4a4ad2dSNavdeep Parhar CSUM_INNER_IP_UDP | CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO |
2869a4a4ad2dSNavdeep Parhar CSUM_ENCAP_VXLAN;
2870a4a4ad2dSNavdeep Parhar }
2871a4a4ad2dSNavdeep Parhar
2872a4a4ad2dSNavdeep Parhar if (needs_outer_tcp_csum(m0)) {
28737951040fSNavdeep Parhar tcp = m_advance(&m, &offset, m0->m_pkthdr.l3hlen);
28747951040fSNavdeep Parhar m0->m_pkthdr.l4hlen = tcp->th_off * 4;
2875786099deSNavdeep Parhar #ifdef RATELIMIT
2876786099deSNavdeep Parhar if (tsclk >= 0 && *(uint32_t *)(tcp + 1) == ntohl(0x0101080a)) {
2877786099deSNavdeep Parhar set_mbuf_eo_tsclk_tsoff(m0,
2878786099deSNavdeep Parhar V_FW_ETH_TX_EO_WR_TSCLK(tsclk) |
2879786099deSNavdeep Parhar V_FW_ETH_TX_EO_WR_TSOFF(sizeof(*tcp) / 2 + 1));
2880786099deSNavdeep Parhar } else
2881786099deSNavdeep Parhar set_mbuf_eo_tsclk_tsoff(m0, 0);
2882a4a4ad2dSNavdeep Parhar } else if (needs_outer_udp_csum(m0)) {
2883786099deSNavdeep Parhar m0->m_pkthdr.l4hlen = sizeof(struct udphdr);
2884786099deSNavdeep Parhar #endif
28856af45170SJohn Baldwin }
2886786099deSNavdeep Parhar #ifdef RATELIMIT
288756fb710fSJohn Baldwin if (needs_eo(mst)) {
2888786099deSNavdeep Parhar u_int immhdrs;
2889786099deSNavdeep Parhar
2890786099deSNavdeep Parhar /* EO WRs have the headers in the WR and not the GL. */
2891786099deSNavdeep Parhar immhdrs = m0->m_pkthdr.l2hlen + m0->m_pkthdr.l3hlen +
2892786099deSNavdeep Parhar m0->m_pkthdr.l4hlen;
2893d76bbe17SJohn Baldwin cflags = 0;
2894d76bbe17SJohn Baldwin nsegs = count_mbuf_nsegs(m0, immhdrs, &cflags);
2895d76bbe17SJohn Baldwin MPASS(cflags == mbuf_cflags(m0));
2896786099deSNavdeep Parhar set_mbuf_eo_nsegs(m0, nsegs);
2897786099deSNavdeep Parhar set_mbuf_eo_len16(m0,
2898786099deSNavdeep Parhar txpkt_eo_len16(nsegs, immhdrs, needs_tso(m0)));
28998afd23deSJohn Baldwin rc = ethofld_transmit(mst->ifp, m0);
29008afd23deSJohn Baldwin if (rc != 0)
29018afd23deSJohn Baldwin goto fail;
29028afd23deSJohn Baldwin return (EINPROGRESS);
2903786099deSNavdeep Parhar }
2904786099deSNavdeep Parhar #endif
29057951040fSNavdeep Parhar #endif
29067951040fSNavdeep Parhar MPASS(m0 == *mp);
29077951040fSNavdeep Parhar return (0);
29087951040fSNavdeep Parhar }
29097951040fSNavdeep Parhar
29107951040fSNavdeep Parhar void *
start_wrq_wr(struct sge_wrq * wrq,int len16,struct wrq_cookie * cookie)29117951040fSNavdeep Parhar start_wrq_wr(struct sge_wrq *wrq, int len16, struct wrq_cookie *cookie)
29127951040fSNavdeep Parhar {
29137951040fSNavdeep Parhar struct sge_eq *eq = &wrq->eq;
29147951040fSNavdeep Parhar struct adapter *sc = wrq->adapter;
29157951040fSNavdeep Parhar int ndesc, available;
29167951040fSNavdeep Parhar struct wrqe *wr;
29177951040fSNavdeep Parhar void *w;
29187951040fSNavdeep Parhar
29197951040fSNavdeep Parhar MPASS(len16 > 0);
29200cadedfcSNavdeep Parhar ndesc = tx_len16_to_desc(len16);
29217951040fSNavdeep Parhar MPASS(ndesc > 0 && ndesc <= SGE_MAX_WR_NDESC);
29227951040fSNavdeep Parhar
29237951040fSNavdeep Parhar EQ_LOCK(eq);
2924*0a9d1da6SNavdeep Parhar if (__predict_false((eq->flags & EQ_HW_ALLOCATED) == 0)) {
2925*0a9d1da6SNavdeep Parhar EQ_UNLOCK(eq);
2926*0a9d1da6SNavdeep Parhar return (NULL);
2927*0a9d1da6SNavdeep Parhar }
29287951040fSNavdeep Parhar
29298d6ae10aSNavdeep Parhar if (TAILQ_EMPTY(&wrq->incomplete_wrs) && !STAILQ_EMPTY(&wrq->wr_list))
29307951040fSNavdeep Parhar drain_wrq_wr_list(sc, wrq);
29317951040fSNavdeep Parhar
29327951040fSNavdeep Parhar if (!STAILQ_EMPTY(&wrq->wr_list)) {
29337951040fSNavdeep Parhar slowpath:
29347951040fSNavdeep Parhar EQ_UNLOCK(eq);
29357951040fSNavdeep Parhar wr = alloc_wrqe(len16 * 16, wrq);
29367951040fSNavdeep Parhar if (__predict_false(wr == NULL))
29377951040fSNavdeep Parhar return (NULL);
29387951040fSNavdeep Parhar cookie->pidx = -1;
29397951040fSNavdeep Parhar cookie->ndesc = ndesc;
29407951040fSNavdeep Parhar return (&wr->wr);
29417951040fSNavdeep Parhar }
29427951040fSNavdeep Parhar
29437951040fSNavdeep Parhar eq->cidx = read_hw_cidx(eq);
29447951040fSNavdeep Parhar if (eq->pidx == eq->cidx)
29457951040fSNavdeep Parhar available = eq->sidx - 1;
29467951040fSNavdeep Parhar else
29477951040fSNavdeep Parhar available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
29487951040fSNavdeep Parhar if (available < ndesc)
29497951040fSNavdeep Parhar goto slowpath;
29507951040fSNavdeep Parhar
29517951040fSNavdeep Parhar cookie->pidx = eq->pidx;
29527951040fSNavdeep Parhar cookie->ndesc = ndesc;
29537951040fSNavdeep Parhar TAILQ_INSERT_TAIL(&wrq->incomplete_wrs, cookie, link);
29547951040fSNavdeep Parhar
29557951040fSNavdeep Parhar w = &eq->desc[eq->pidx];
29567951040fSNavdeep Parhar IDXINCR(eq->pidx, ndesc, eq->sidx);
2957f50c49ccSNavdeep Parhar if (__predict_false(cookie->pidx + ndesc > eq->sidx)) {
29587951040fSNavdeep Parhar w = &wrq->ss[0];
29597951040fSNavdeep Parhar wrq->ss_pidx = cookie->pidx;
29607951040fSNavdeep Parhar wrq->ss_len = len16 * 16;
29617951040fSNavdeep Parhar }
29627951040fSNavdeep Parhar
29637951040fSNavdeep Parhar EQ_UNLOCK(eq);
29647951040fSNavdeep Parhar
29657951040fSNavdeep Parhar return (w);
29667951040fSNavdeep Parhar }
29677951040fSNavdeep Parhar
29687951040fSNavdeep Parhar void
commit_wrq_wr(struct sge_wrq * wrq,void * w,struct wrq_cookie * cookie)29697951040fSNavdeep Parhar commit_wrq_wr(struct sge_wrq *wrq, void *w, struct wrq_cookie *cookie)
29707951040fSNavdeep Parhar {
29717951040fSNavdeep Parhar struct sge_eq *eq = &wrq->eq;
29727951040fSNavdeep Parhar struct adapter *sc = wrq->adapter;
29737951040fSNavdeep Parhar int ndesc, pidx;
29747951040fSNavdeep Parhar struct wrq_cookie *prev, *next;
29757951040fSNavdeep Parhar
29767951040fSNavdeep Parhar if (cookie->pidx == -1) {
29777951040fSNavdeep Parhar struct wrqe *wr = __containerof(w, struct wrqe, wr);
29787951040fSNavdeep Parhar
29797951040fSNavdeep Parhar t4_wrq_tx(sc, wr);
29807951040fSNavdeep Parhar return;
29817951040fSNavdeep Parhar }
29827951040fSNavdeep Parhar
29837951040fSNavdeep Parhar if (__predict_false(w == &wrq->ss[0])) {
29847951040fSNavdeep Parhar int n = (eq->sidx - wrq->ss_pidx) * EQ_ESIZE;
29857951040fSNavdeep Parhar
29867951040fSNavdeep Parhar MPASS(wrq->ss_len > n); /* WR had better wrap around. */
29877951040fSNavdeep Parhar bcopy(&wrq->ss[0], &eq->desc[wrq->ss_pidx], n);
29887951040fSNavdeep Parhar bcopy(&wrq->ss[n], &eq->desc[0], wrq->ss_len - n);
29897951040fSNavdeep Parhar wrq->tx_wrs_ss++;
29907951040fSNavdeep Parhar } else
29917951040fSNavdeep Parhar wrq->tx_wrs_direct++;
29927951040fSNavdeep Parhar
29937951040fSNavdeep Parhar EQ_LOCK(eq);
29948d6ae10aSNavdeep Parhar ndesc = cookie->ndesc; /* Can be more than SGE_MAX_WR_NDESC here. */
29958d6ae10aSNavdeep Parhar pidx = cookie->pidx;
29968d6ae10aSNavdeep Parhar MPASS(pidx >= 0 && pidx < eq->sidx);
29977951040fSNavdeep Parhar prev = TAILQ_PREV(cookie, wrq_incomplete_wrs, link);
29987951040fSNavdeep Parhar next = TAILQ_NEXT(cookie, link);
29997951040fSNavdeep Parhar if (prev == NULL) {
30007951040fSNavdeep Parhar MPASS(pidx == eq->dbidx);
30012e09fe91SNavdeep Parhar if (next == NULL || ndesc >= 16) {
30022e09fe91SNavdeep Parhar int available;
30032e09fe91SNavdeep Parhar struct fw_eth_tx_pkt_wr *dst; /* any fw WR struct will do */
30042e09fe91SNavdeep Parhar
30052e09fe91SNavdeep Parhar /*
30062e09fe91SNavdeep Parhar * Note that the WR via which we'll request tx updates
30072e09fe91SNavdeep Parhar * is at pidx and not eq->pidx, which has moved on
30082e09fe91SNavdeep Parhar * already.
30092e09fe91SNavdeep Parhar */
30102e09fe91SNavdeep Parhar dst = (void *)&eq->desc[pidx];
30112e09fe91SNavdeep Parhar available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
30122e09fe91SNavdeep Parhar if (available < eq->sidx / 4 &&
30132e09fe91SNavdeep Parhar atomic_cmpset_int(&eq->equiq, 0, 1)) {
3014ddf09ad6SNavdeep Parhar /*
3015ddf09ad6SNavdeep Parhar * XXX: This is not 100% reliable with some
3016ddf09ad6SNavdeep Parhar * types of WRs. But this is a very unusual
3017ddf09ad6SNavdeep Parhar * situation for an ofld/ctrl queue anyway.
3018ddf09ad6SNavdeep Parhar */
30192e09fe91SNavdeep Parhar dst->equiq_to_len16 |= htobe32(F_FW_WR_EQUIQ |
30202e09fe91SNavdeep Parhar F_FW_WR_EQUEQ);
30212e09fe91SNavdeep Parhar }
30222e09fe91SNavdeep Parhar
3023*0a9d1da6SNavdeep Parhar if (__predict_true(eq->flags & EQ_HW_ALLOCATED))
30247951040fSNavdeep Parhar ring_eq_db(wrq->adapter, eq, ndesc);
3025*0a9d1da6SNavdeep Parhar else
3026*0a9d1da6SNavdeep Parhar IDXINCR(eq->dbidx, ndesc, eq->sidx);
30272e09fe91SNavdeep Parhar } else {
30287951040fSNavdeep Parhar MPASS(IDXDIFF(next->pidx, pidx, eq->sidx) == ndesc);
30297951040fSNavdeep Parhar next->pidx = pidx;
30307951040fSNavdeep Parhar next->ndesc += ndesc;
30317951040fSNavdeep Parhar }
30327951040fSNavdeep Parhar } else {
30337951040fSNavdeep Parhar MPASS(IDXDIFF(pidx, prev->pidx, eq->sidx) == prev->ndesc);
30347951040fSNavdeep Parhar prev->ndesc += ndesc;
30357951040fSNavdeep Parhar }
30367951040fSNavdeep Parhar TAILQ_REMOVE(&wrq->incomplete_wrs, cookie, link);
30377951040fSNavdeep Parhar
30387951040fSNavdeep Parhar if (TAILQ_EMPTY(&wrq->incomplete_wrs) && !STAILQ_EMPTY(&wrq->wr_list))
30397951040fSNavdeep Parhar drain_wrq_wr_list(sc, wrq);
30407951040fSNavdeep Parhar
30417951040fSNavdeep Parhar #ifdef INVARIANTS
30427951040fSNavdeep Parhar if (TAILQ_EMPTY(&wrq->incomplete_wrs)) {
30437951040fSNavdeep Parhar /* Doorbell must have caught up to the pidx. */
30447951040fSNavdeep Parhar MPASS(wrq->eq.pidx == wrq->eq.dbidx);
30457951040fSNavdeep Parhar }
30467951040fSNavdeep Parhar #endif
30477951040fSNavdeep Parhar EQ_UNLOCK(eq);
30487951040fSNavdeep Parhar }
30497951040fSNavdeep Parhar
30507951040fSNavdeep Parhar static u_int
can_resume_eth_tx(struct mp_ring * r)30517951040fSNavdeep Parhar can_resume_eth_tx(struct mp_ring *r)
30527951040fSNavdeep Parhar {
30537951040fSNavdeep Parhar struct sge_eq *eq = r->cookie;
30547951040fSNavdeep Parhar
30557951040fSNavdeep Parhar return (total_available_tx_desc(eq) > eq->sidx / 8);
30567951040fSNavdeep Parhar }
30577951040fSNavdeep Parhar
3058d735920dSNavdeep Parhar static inline bool
cannot_use_txpkts(struct mbuf * m)30597951040fSNavdeep Parhar cannot_use_txpkts(struct mbuf *m)
30607951040fSNavdeep Parhar {
30617951040fSNavdeep Parhar /* maybe put a GL limit too, to avoid silliness? */
30627951040fSNavdeep Parhar
3063bddf7343SJohn Baldwin return (needs_tso(m) || (mbuf_cflags(m) & (MC_RAW_WR | MC_TLS)) != 0);
30647951040fSNavdeep Parhar }
30657951040fSNavdeep Parhar
30661404daa7SNavdeep Parhar static inline int
discard_tx(struct sge_eq * eq)30671404daa7SNavdeep Parhar discard_tx(struct sge_eq *eq)
30681404daa7SNavdeep Parhar {
30691404daa7SNavdeep Parhar
30701404daa7SNavdeep Parhar return ((eq->flags & (EQ_ENABLED | EQ_QFLUSH)) != EQ_ENABLED);
30711404daa7SNavdeep Parhar }
30721404daa7SNavdeep Parhar
30735cdaef71SJohn Baldwin static inline int
wr_can_update_eq(void * p)3074d735920dSNavdeep Parhar wr_can_update_eq(void *p)
30755cdaef71SJohn Baldwin {
3076d735920dSNavdeep Parhar struct fw_eth_tx_pkts_wr *wr = p;
30775cdaef71SJohn Baldwin
30785cdaef71SJohn Baldwin switch (G_FW_WR_OP(be32toh(wr->op_pkd))) {
30795cdaef71SJohn Baldwin case FW_ULPTX_WR:
30805cdaef71SJohn Baldwin case FW_ETH_TX_PKT_WR:
30815cdaef71SJohn Baldwin case FW_ETH_TX_PKTS_WR:
3082693a9dfcSNavdeep Parhar case FW_ETH_TX_PKTS2_WR:
30835cdaef71SJohn Baldwin case FW_ETH_TX_PKT_VM_WR:
3084d735920dSNavdeep Parhar case FW_ETH_TX_PKTS_VM_WR:
30855cdaef71SJohn Baldwin return (1);
30865cdaef71SJohn Baldwin default:
30875cdaef71SJohn Baldwin return (0);
30885cdaef71SJohn Baldwin }
30895cdaef71SJohn Baldwin }
30905cdaef71SJohn Baldwin
3091d735920dSNavdeep Parhar static inline void
set_txupdate_flags(struct sge_txq * txq,u_int avail,struct fw_eth_tx_pkt_wr * wr)3092d735920dSNavdeep Parhar set_txupdate_flags(struct sge_txq *txq, u_int avail,
3093d735920dSNavdeep Parhar struct fw_eth_tx_pkt_wr *wr)
3094d735920dSNavdeep Parhar {
3095d735920dSNavdeep Parhar struct sge_eq *eq = &txq->eq;
3096d735920dSNavdeep Parhar struct txpkts *txp = &txq->txp;
3097d735920dSNavdeep Parhar
3098d735920dSNavdeep Parhar if ((txp->npkt > 0 || avail < eq->sidx / 2) &&
3099d735920dSNavdeep Parhar atomic_cmpset_int(&eq->equiq, 0, 1)) {
3100d735920dSNavdeep Parhar wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ | F_FW_WR_EQUIQ);
3101d735920dSNavdeep Parhar eq->equeqidx = eq->pidx;
3102d735920dSNavdeep Parhar } else if (IDXDIFF(eq->pidx, eq->equeqidx, eq->sidx) >= 32) {
3103d735920dSNavdeep Parhar wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ);
3104d735920dSNavdeep Parhar eq->equeqidx = eq->pidx;
3105d735920dSNavdeep Parhar }
3106d735920dSNavdeep Parhar }
3107d735920dSNavdeep Parhar
31083447df8bSNavdeep Parhar #if defined(__i386__) || defined(__amd64__)
31093447df8bSNavdeep Parhar extern uint64_t tsc_freq;
31103447df8bSNavdeep Parhar #endif
31113447df8bSNavdeep Parhar
31123447df8bSNavdeep Parhar static inline bool
record_eth_tx_time(struct sge_txq * txq)31133447df8bSNavdeep Parhar record_eth_tx_time(struct sge_txq *txq)
31143447df8bSNavdeep Parhar {
31153447df8bSNavdeep Parhar const uint64_t cycles = get_cyclecount();
31163447df8bSNavdeep Parhar const uint64_t last_tx = txq->last_tx;
31173447df8bSNavdeep Parhar #if defined(__i386__) || defined(__amd64__)
31183447df8bSNavdeep Parhar const uint64_t itg = tsc_freq * t4_tx_coalesce_gap / 1000000;
31193447df8bSNavdeep Parhar #else
31203447df8bSNavdeep Parhar const uint64_t itg = 0;
31213447df8bSNavdeep Parhar #endif
31223447df8bSNavdeep Parhar
31233447df8bSNavdeep Parhar MPASS(cycles >= last_tx);
31243447df8bSNavdeep Parhar txq->last_tx = cycles;
31253447df8bSNavdeep Parhar return (cycles - last_tx < itg);
31263447df8bSNavdeep Parhar }
31273447df8bSNavdeep Parhar
31287951040fSNavdeep Parhar /*
31297951040fSNavdeep Parhar * r->items[cidx] to r->items[pidx], with a wraparound at r->size, are ready to
31307951040fSNavdeep Parhar * be consumed. Return the actual number consumed. 0 indicates a stall.
31317951040fSNavdeep Parhar */
31327951040fSNavdeep Parhar static u_int
eth_tx(struct mp_ring * r,u_int cidx,u_int pidx,bool * coalescing)3133d735920dSNavdeep Parhar eth_tx(struct mp_ring *r, u_int cidx, u_int pidx, bool *coalescing)
31347951040fSNavdeep Parhar {
31357951040fSNavdeep Parhar struct sge_txq *txq = r->cookie;
3136954712e8SJustin Hibbits if_t ifp = txq->ifp;
3137d735920dSNavdeep Parhar struct sge_eq *eq = &txq->eq;
3138d735920dSNavdeep Parhar struct txpkts *txp = &txq->txp;
3139954712e8SJustin Hibbits struct vi_info *vi = if_getsoftc(ifp);
31407c228be3SNavdeep Parhar struct adapter *sc = vi->adapter;
31417951040fSNavdeep Parhar u_int total, remaining; /* # of packets */
3142d735920dSNavdeep Parhar u_int n, avail, dbdiff; /* # of hardware descriptors */
3143d735920dSNavdeep Parhar int i, rc;
3144d735920dSNavdeep Parhar struct mbuf *m0;
31453447df8bSNavdeep Parhar bool snd, recent_tx;
3146d735920dSNavdeep Parhar void *wr; /* start of the last WR written to the ring */
3147d735920dSNavdeep Parhar
3148d735920dSNavdeep Parhar TXQ_LOCK_ASSERT_OWNED(txq);
31493447df8bSNavdeep Parhar recent_tx = record_eth_tx_time(txq);
31507951040fSNavdeep Parhar
31517951040fSNavdeep Parhar remaining = IDXDIFF(pidx, cidx, r->size);
31521404daa7SNavdeep Parhar if (__predict_false(discard_tx(eq))) {
3153d735920dSNavdeep Parhar for (i = 0; i < txp->npkt; i++)
3154d735920dSNavdeep Parhar m_freem(txp->mb[i]);
3155d735920dSNavdeep Parhar txp->npkt = 0;
31567951040fSNavdeep Parhar while (cidx != pidx) {
31577951040fSNavdeep Parhar m0 = r->items[cidx];
31587951040fSNavdeep Parhar m_freem(m0);
31597951040fSNavdeep Parhar if (++cidx == r->size)
31607951040fSNavdeep Parhar cidx = 0;
31617951040fSNavdeep Parhar }
3162d735920dSNavdeep Parhar reclaim_tx_descs(txq, eq->sidx);
3163d735920dSNavdeep Parhar *coalescing = false;
3164d735920dSNavdeep Parhar return (remaining); /* emptied */
31657951040fSNavdeep Parhar }
31667951040fSNavdeep Parhar
31677951040fSNavdeep Parhar /* How many hardware descriptors do we have readily available. */
31683447df8bSNavdeep Parhar if (eq->pidx == eq->cidx)
3169d735920dSNavdeep Parhar avail = eq->sidx - 1;
31703447df8bSNavdeep Parhar else
3171d735920dSNavdeep Parhar avail = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
31727951040fSNavdeep Parhar
3173d735920dSNavdeep Parhar total = 0;
3174d735920dSNavdeep Parhar if (remaining == 0) {
31753447df8bSNavdeep Parhar txp->score = 0;
31763447df8bSNavdeep Parhar txq->txpkts_flush++;
3177d735920dSNavdeep Parhar goto send_txpkts;
3178d735920dSNavdeep Parhar }
3179d735920dSNavdeep Parhar
3180d735920dSNavdeep Parhar dbdiff = 0;
3181d735920dSNavdeep Parhar MPASS(remaining > 0);
31827951040fSNavdeep Parhar while (remaining > 0) {
31837951040fSNavdeep Parhar m0 = r->items[cidx];
31847951040fSNavdeep Parhar M_ASSERTPKTHDR(m0);
31857951040fSNavdeep Parhar MPASS(m0->m_nextpkt == NULL);
31867951040fSNavdeep Parhar
3187d735920dSNavdeep Parhar if (avail < 2 * SGE_MAX_WR_NDESC)
3188d735920dSNavdeep Parhar avail += reclaim_tx_descs(txq, 64);
3189d735920dSNavdeep Parhar
31903447df8bSNavdeep Parhar if (t4_tx_coalesce == 0 && txp->npkt == 0)
31913447df8bSNavdeep Parhar goto skip_coalescing;
31923447df8bSNavdeep Parhar if (cannot_use_txpkts(m0))
31933447df8bSNavdeep Parhar txp->score = 0;
31943447df8bSNavdeep Parhar else if (recent_tx) {
31953447df8bSNavdeep Parhar if (++txp->score == 0)
31963447df8bSNavdeep Parhar txp->score = UINT8_MAX;
31973447df8bSNavdeep Parhar } else
31983447df8bSNavdeep Parhar txp->score = 1;
31993447df8bSNavdeep Parhar if (txp->npkt > 0 || remaining > 1 ||
32003447df8bSNavdeep Parhar txp->score >= t4_tx_coalesce_pkts ||
3201d735920dSNavdeep Parhar atomic_load_int(&txq->eq.equiq) != 0) {
320230e3f2b4SNavdeep Parhar if (vi->flags & TX_USES_VM_WR)
3203d735920dSNavdeep Parhar rc = add_to_txpkts_vf(sc, txq, m0, avail, &snd);
3204d735920dSNavdeep Parhar else
3205d735920dSNavdeep Parhar rc = add_to_txpkts_pf(sc, txq, m0, avail, &snd);
3206d735920dSNavdeep Parhar } else {
3207d735920dSNavdeep Parhar snd = false;
3208d735920dSNavdeep Parhar rc = EINVAL;
3209d735920dSNavdeep Parhar }
3210d735920dSNavdeep Parhar if (snd) {
3211d735920dSNavdeep Parhar MPASS(txp->npkt > 0);
3212d735920dSNavdeep Parhar for (i = 0; i < txp->npkt; i++)
3213d735920dSNavdeep Parhar ETHER_BPF_MTAP(ifp, txp->mb[i]);
3214d735920dSNavdeep Parhar if (txp->npkt > 1) {
3215d735920dSNavdeep Parhar MPASS(avail >= tx_len16_to_desc(txp->len16));
321630e3f2b4SNavdeep Parhar if (vi->flags & TX_USES_VM_WR)
3217d735920dSNavdeep Parhar n = write_txpkts_vm_wr(sc, txq);
3218d735920dSNavdeep Parhar else
3219d735920dSNavdeep Parhar n = write_txpkts_wr(sc, txq);
3220d735920dSNavdeep Parhar } else {
3221d735920dSNavdeep Parhar MPASS(avail >=
3222d735920dSNavdeep Parhar tx_len16_to_desc(mbuf_len16(txp->mb[0])));
322330e3f2b4SNavdeep Parhar if (vi->flags & TX_USES_VM_WR)
3224d735920dSNavdeep Parhar n = write_txpkt_vm_wr(sc, txq,
3225d735920dSNavdeep Parhar txp->mb[0]);
3226d735920dSNavdeep Parhar else
3227d735920dSNavdeep Parhar n = write_txpkt_wr(sc, txq, txp->mb[0],
3228d735920dSNavdeep Parhar avail);
3229d735920dSNavdeep Parhar }
3230d735920dSNavdeep Parhar MPASS(n <= SGE_MAX_WR_NDESC);
3231d735920dSNavdeep Parhar avail -= n;
3232d735920dSNavdeep Parhar dbdiff += n;
3233d735920dSNavdeep Parhar wr = &eq->desc[eq->pidx];
3234d735920dSNavdeep Parhar IDXINCR(eq->pidx, n, eq->sidx);
3235d735920dSNavdeep Parhar txp->npkt = 0; /* emptied */
3236d735920dSNavdeep Parhar }
3237d735920dSNavdeep Parhar if (rc == 0) {
3238d735920dSNavdeep Parhar /* m0 was coalesced into txq->txpkts. */
3239d735920dSNavdeep Parhar goto next_mbuf;
3240d735920dSNavdeep Parhar }
3241d735920dSNavdeep Parhar if (rc == EAGAIN) {
3242d735920dSNavdeep Parhar /*
3243d735920dSNavdeep Parhar * m0 is suitable for tx coalescing but could not be
3244d735920dSNavdeep Parhar * combined with the existing txq->txpkts, which has now
3245d735920dSNavdeep Parhar * been transmitted. Start a new txpkts with m0.
3246d735920dSNavdeep Parhar */
3247d735920dSNavdeep Parhar MPASS(snd);
3248d735920dSNavdeep Parhar MPASS(txp->npkt == 0);
3249d735920dSNavdeep Parhar continue;
32507951040fSNavdeep Parhar }
32517951040fSNavdeep Parhar
3252d735920dSNavdeep Parhar MPASS(rc != 0 && rc != EAGAIN);
3253d735920dSNavdeep Parhar MPASS(txp->npkt == 0);
32543447df8bSNavdeep Parhar skip_coalescing:
3255565b8fceSNavdeep Parhar n = tx_len16_to_desc(mbuf_len16(m0));
3256565b8fceSNavdeep Parhar if (__predict_false(avail < n)) {
3257565b8fceSNavdeep Parhar avail += reclaim_tx_descs(txq, min(n, 32));
3258565b8fceSNavdeep Parhar if (avail < n)
3259565b8fceSNavdeep Parhar break; /* out of descriptors */
3260565b8fceSNavdeep Parhar }
3261565b8fceSNavdeep Parhar
3262d735920dSNavdeep Parhar wr = &eq->desc[eq->pidx];
3263bddf7343SJohn Baldwin if (mbuf_cflags(m0) & MC_RAW_WR) {
3264d735920dSNavdeep Parhar n = write_raw_wr(txq, wr, m0, avail);
3265bddf7343SJohn Baldwin #ifdef KERN_TLS
3266bddf7343SJohn Baldwin } else if (mbuf_cflags(m0) & MC_TLS) {
3267bddf7343SJohn Baldwin ETHER_BPF_MTAP(ifp, m0);
3268ca457729SJohn Baldwin n = t6_ktls_write_wr(txq, wr, m0, avail);
3269bddf7343SJohn Baldwin #endif
32707951040fSNavdeep Parhar } else {
32713bbb68f0SNavdeep Parhar ETHER_BPF_MTAP(ifp, m0);
327230e3f2b4SNavdeep Parhar if (vi->flags & TX_USES_VM_WR)
3273d735920dSNavdeep Parhar n = write_txpkt_vm_wr(sc, txq, m0);
3274d735920dSNavdeep Parhar else
3275d735920dSNavdeep Parhar n = write_txpkt_wr(sc, txq, m0, avail);
3276d735920dSNavdeep Parhar }
3277d735920dSNavdeep Parhar MPASS(n >= 1 && n <= avail);
3278bddf7343SJohn Baldwin if (!(mbuf_cflags(m0) & MC_TLS))
3279bddf7343SJohn Baldwin MPASS(n <= SGE_MAX_WR_NDESC);
32807951040fSNavdeep Parhar
3281d735920dSNavdeep Parhar avail -= n;
32827951040fSNavdeep Parhar dbdiff += n;
32837951040fSNavdeep Parhar IDXINCR(eq->pidx, n, eq->sidx);
32847951040fSNavdeep Parhar
3285d735920dSNavdeep Parhar if (dbdiff >= 512 / EQ_ESIZE) { /* X_FETCHBURSTMAX_512B */
3286d735920dSNavdeep Parhar if (wr_can_update_eq(wr))
3287d735920dSNavdeep Parhar set_txupdate_flags(txq, avail, wr);
32887951040fSNavdeep Parhar ring_eq_db(sc, eq, dbdiff);
3289d735920dSNavdeep Parhar avail += reclaim_tx_descs(txq, 32);
32907951040fSNavdeep Parhar dbdiff = 0;
32917951040fSNavdeep Parhar }
3292d735920dSNavdeep Parhar next_mbuf:
3293d735920dSNavdeep Parhar total++;
3294d735920dSNavdeep Parhar remaining--;
3295d735920dSNavdeep Parhar if (__predict_false(++cidx == r->size))
3296d735920dSNavdeep Parhar cidx = 0;
32977951040fSNavdeep Parhar }
32987951040fSNavdeep Parhar if (dbdiff != 0) {
3299d735920dSNavdeep Parhar if (wr_can_update_eq(wr))
3300d735920dSNavdeep Parhar set_txupdate_flags(txq, avail, wr);
33017951040fSNavdeep Parhar ring_eq_db(sc, eq, dbdiff);
33027951040fSNavdeep Parhar reclaim_tx_descs(txq, 32);
3303d735920dSNavdeep Parhar } else if (eq->pidx == eq->cidx && txp->npkt > 0 &&
3304d735920dSNavdeep Parhar atomic_load_int(&txq->eq.equiq) == 0) {
3305d735920dSNavdeep Parhar /*
3306d735920dSNavdeep Parhar * If nothing was submitted to the chip for tx (it was coalesced
3307d735920dSNavdeep Parhar * into txpkts instead) and there is no tx update outstanding
3308d735920dSNavdeep Parhar * then we need to send txpkts now.
3309d735920dSNavdeep Parhar */
3310d735920dSNavdeep Parhar send_txpkts:
3311d735920dSNavdeep Parhar MPASS(txp->npkt > 0);
3312d735920dSNavdeep Parhar for (i = 0; i < txp->npkt; i++)
3313d735920dSNavdeep Parhar ETHER_BPF_MTAP(ifp, txp->mb[i]);
3314d735920dSNavdeep Parhar if (txp->npkt > 1) {
3315d735920dSNavdeep Parhar MPASS(avail >= tx_len16_to_desc(txp->len16));
331630e3f2b4SNavdeep Parhar if (vi->flags & TX_USES_VM_WR)
3317d735920dSNavdeep Parhar n = write_txpkts_vm_wr(sc, txq);
3318d735920dSNavdeep Parhar else
3319d735920dSNavdeep Parhar n = write_txpkts_wr(sc, txq);
3320d735920dSNavdeep Parhar } else {
3321d735920dSNavdeep Parhar MPASS(avail >=
3322d735920dSNavdeep Parhar tx_len16_to_desc(mbuf_len16(txp->mb[0])));
332330e3f2b4SNavdeep Parhar if (vi->flags & TX_USES_VM_WR)
3324d735920dSNavdeep Parhar n = write_txpkt_vm_wr(sc, txq, txp->mb[0]);
3325d735920dSNavdeep Parhar else
3326d735920dSNavdeep Parhar n = write_txpkt_wr(sc, txq, txp->mb[0], avail);
33277951040fSNavdeep Parhar }
3328d735920dSNavdeep Parhar MPASS(n <= SGE_MAX_WR_NDESC);
3329d735920dSNavdeep Parhar wr = &eq->desc[eq->pidx];
3330d735920dSNavdeep Parhar IDXINCR(eq->pidx, n, eq->sidx);
3331d735920dSNavdeep Parhar txp->npkt = 0; /* emptied */
3332d735920dSNavdeep Parhar
3333d735920dSNavdeep Parhar MPASS(wr_can_update_eq(wr));
3334d735920dSNavdeep Parhar set_txupdate_flags(txq, avail - n, wr);
3335d735920dSNavdeep Parhar ring_eq_db(sc, eq, n);
3336d735920dSNavdeep Parhar reclaim_tx_descs(txq, 32);
3337d735920dSNavdeep Parhar }
3338d735920dSNavdeep Parhar *coalescing = txp->npkt > 0;
33397951040fSNavdeep Parhar
33407951040fSNavdeep Parhar return (total);
3341733b9277SNavdeep Parhar }
3342733b9277SNavdeep Parhar
334354e4ee71SNavdeep Parhar static inline void
init_iq(struct sge_iq * iq,struct adapter * sc,int tmr_idx,int pktc_idx,int qsize,int intr_idx,int cong,int qtype)334454e4ee71SNavdeep Parhar init_iq(struct sge_iq *iq, struct adapter *sc, int tmr_idx, int pktc_idx,
3345c387ff00SNavdeep Parhar int qsize, int intr_idx, int cong, int qtype)
334654e4ee71SNavdeep Parhar {
3347b2daa9a9SNavdeep Parhar
334854e4ee71SNavdeep Parhar KASSERT(tmr_idx >= 0 && tmr_idx < SGE_NTIMERS,
334954e4ee71SNavdeep Parhar ("%s: bad tmr_idx %d", __func__, tmr_idx));
335054e4ee71SNavdeep Parhar KASSERT(pktc_idx < SGE_NCOUNTERS, /* -ve is ok, means don't use */
335154e4ee71SNavdeep Parhar ("%s: bad pktc_idx %d", __func__, pktc_idx));
335243bbae19SNavdeep Parhar KASSERT(intr_idx >= -1 && intr_idx < sc->intr_count,
335343bbae19SNavdeep Parhar ("%s: bad intr_idx %d", __func__, intr_idx));
3354c387ff00SNavdeep Parhar KASSERT(qtype == FW_IQ_IQTYPE_OTHER || qtype == FW_IQ_IQTYPE_NIC ||
3355c387ff00SNavdeep Parhar qtype == FW_IQ_IQTYPE_OFLD, ("%s: bad qtype %d", __func__, qtype));
335654e4ee71SNavdeep Parhar
335754e4ee71SNavdeep Parhar iq->flags = 0;
335843bbae19SNavdeep Parhar iq->state = IQS_DISABLED;
335954e4ee71SNavdeep Parhar iq->adapter = sc;
3360c387ff00SNavdeep Parhar iq->qtype = qtype;
33617a32954cSNavdeep Parhar iq->intr_params = V_QINTR_TIMER_IDX(tmr_idx);
33627a32954cSNavdeep Parhar iq->intr_pktc_idx = SGE_NCOUNTERS - 1;
33637a32954cSNavdeep Parhar if (pktc_idx >= 0) {
33647a32954cSNavdeep Parhar iq->intr_params |= F_QINTR_CNT_EN;
336554e4ee71SNavdeep Parhar iq->intr_pktc_idx = pktc_idx;
33667a32954cSNavdeep Parhar }
3367d14b0ac1SNavdeep Parhar iq->qsize = roundup2(qsize, 16); /* See FW_IQ_CMD/iqsize */
336890e7434aSNavdeep Parhar iq->sidx = iq->qsize - sc->params.sge.spg_len / IQ_ESIZE;
336943bbae19SNavdeep Parhar iq->intr_idx = intr_idx;
3370df275ae5SNavdeep Parhar iq->cong_drop = cong;
337154e4ee71SNavdeep Parhar }
337254e4ee71SNavdeep Parhar
337354e4ee71SNavdeep Parhar static inline void
init_fl(struct adapter * sc,struct sge_fl * fl,int qsize,int maxp,char * name)3374e3207e19SNavdeep Parhar init_fl(struct adapter *sc, struct sge_fl *fl, int qsize, int maxp, char *name)
337554e4ee71SNavdeep Parhar {
337643bbae19SNavdeep Parhar struct sge_params *sp = &sc->params.sge;
33771458bff9SNavdeep Parhar
337854e4ee71SNavdeep Parhar fl->qsize = qsize;
337990e7434aSNavdeep Parhar fl->sidx = qsize - sc->params.sge.spg_len / EQ_ESIZE;
338054e4ee71SNavdeep Parhar strlcpy(fl->lockname, name, sizeof(fl->lockname));
338143bbae19SNavdeep Parhar mtx_init(&fl->fl_lock, fl->lockname, NULL, MTX_DEF);
3382e3207e19SNavdeep Parhar if (sc->flags & BUF_PACKING_OK &&
3383e3207e19SNavdeep Parhar ((!is_t4(sc) && buffer_packing) || /* T5+: enabled unless 0 */
3384e3207e19SNavdeep Parhar (is_t4(sc) && buffer_packing == 1)))/* T4: disabled unless 1 */
33851458bff9SNavdeep Parhar fl->flags |= FL_BUF_PACKING;
338646e1e307SNavdeep Parhar fl->zidx = find_refill_source(sc, maxp, fl->flags & FL_BUF_PACKING);
338746e1e307SNavdeep Parhar fl->safe_zidx = sc->sge.safe_zidx;
338843bbae19SNavdeep Parhar if (fl->flags & FL_BUF_PACKING) {
338943bbae19SNavdeep Parhar fl->lowat = roundup2(sp->fl_starve_threshold2, 8);
339043bbae19SNavdeep Parhar fl->buf_boundary = sp->pack_boundary;
339143bbae19SNavdeep Parhar } else {
339243bbae19SNavdeep Parhar fl->lowat = roundup2(sp->fl_starve_threshold, 8);
339343bbae19SNavdeep Parhar fl->buf_boundary = 16;
339443bbae19SNavdeep Parhar }
339543bbae19SNavdeep Parhar if (fl_pad && fl->buf_boundary < sp->pad_boundary)
339643bbae19SNavdeep Parhar fl->buf_boundary = sp->pad_boundary;
339754e4ee71SNavdeep Parhar }
339854e4ee71SNavdeep Parhar
339954e4ee71SNavdeep Parhar static inline void
init_eq(struct adapter * sc,struct sge_eq * eq,int eqtype,int qsize,uint8_t port_id,struct sge_iq * iq,char * name)340090e7434aSNavdeep Parhar init_eq(struct adapter *sc, struct sge_eq *eq, int eqtype, int qsize,
3401857d74b6SNavdeep Parhar uint8_t port_id, struct sge_iq *iq, char *name)
340254e4ee71SNavdeep Parhar {
340343bbae19SNavdeep Parhar KASSERT(eqtype >= EQ_CTRL && eqtype <= EQ_OFLD,
340443bbae19SNavdeep Parhar ("%s: bad qtype %d", __func__, eqtype));
3405733b9277SNavdeep Parhar
340643bbae19SNavdeep Parhar eq->type = eqtype;
3407857d74b6SNavdeep Parhar eq->port_id = port_id;
3408857d74b6SNavdeep Parhar eq->tx_chan = sc->port[port_id]->tx_chan;
340943bbae19SNavdeep Parhar eq->iq = iq;
341090e7434aSNavdeep Parhar eq->sidx = qsize - sc->params.sge.spg_len / EQ_ESIZE;
3411f7dfe243SNavdeep Parhar strlcpy(eq->lockname, name, sizeof(eq->lockname));
341243bbae19SNavdeep Parhar mtx_init(&eq->eq_lock, eq->lockname, NULL, MTX_DEF);
341354e4ee71SNavdeep Parhar }
341454e4ee71SNavdeep Parhar
34158eba75edSNavdeep Parhar int
alloc_ring(struct adapter * sc,size_t len,bus_dma_tag_t * tag,bus_dmamap_t * map,bus_addr_t * pa,void ** va)341654e4ee71SNavdeep Parhar alloc_ring(struct adapter *sc, size_t len, bus_dma_tag_t *tag,
341754e4ee71SNavdeep Parhar bus_dmamap_t *map, bus_addr_t *pa, void **va)
341854e4ee71SNavdeep Parhar {
341954e4ee71SNavdeep Parhar int rc;
342054e4ee71SNavdeep Parhar
342154e4ee71SNavdeep Parhar rc = bus_dma_tag_create(sc->dmat, 512, 0, BUS_SPACE_MAXADDR,
342254e4ee71SNavdeep Parhar BUS_SPACE_MAXADDR, NULL, NULL, len, 1, len, 0, NULL, NULL, tag);
342354e4ee71SNavdeep Parhar if (rc != 0) {
342443bbae19SNavdeep Parhar CH_ERR(sc, "cannot allocate DMA tag: %d\n", rc);
342554e4ee71SNavdeep Parhar goto done;
342654e4ee71SNavdeep Parhar }
342754e4ee71SNavdeep Parhar
342854e4ee71SNavdeep Parhar rc = bus_dmamem_alloc(*tag, va,
342954e4ee71SNavdeep Parhar BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, map);
343054e4ee71SNavdeep Parhar if (rc != 0) {
343143bbae19SNavdeep Parhar CH_ERR(sc, "cannot allocate DMA memory: %d\n", rc);
343254e4ee71SNavdeep Parhar goto done;
343354e4ee71SNavdeep Parhar }
343454e4ee71SNavdeep Parhar
343554e4ee71SNavdeep Parhar rc = bus_dmamap_load(*tag, *map, *va, len, oneseg_dma_callback, pa, 0);
343654e4ee71SNavdeep Parhar if (rc != 0) {
343743bbae19SNavdeep Parhar CH_ERR(sc, "cannot load DMA map: %d\n", rc);
343854e4ee71SNavdeep Parhar goto done;
343954e4ee71SNavdeep Parhar }
344054e4ee71SNavdeep Parhar done:
344154e4ee71SNavdeep Parhar if (rc)
344254e4ee71SNavdeep Parhar free_ring(sc, *tag, *map, *pa, *va);
344354e4ee71SNavdeep Parhar
344454e4ee71SNavdeep Parhar return (rc);
344554e4ee71SNavdeep Parhar }
344654e4ee71SNavdeep Parhar
34478eba75edSNavdeep Parhar int
free_ring(struct adapter * sc,bus_dma_tag_t tag,bus_dmamap_t map,bus_addr_t pa,void * va)344854e4ee71SNavdeep Parhar free_ring(struct adapter *sc, bus_dma_tag_t tag, bus_dmamap_t map,
344954e4ee71SNavdeep Parhar bus_addr_t pa, void *va)
345054e4ee71SNavdeep Parhar {
345154e4ee71SNavdeep Parhar if (pa)
345254e4ee71SNavdeep Parhar bus_dmamap_unload(tag, map);
345354e4ee71SNavdeep Parhar if (va)
345454e4ee71SNavdeep Parhar bus_dmamem_free(tag, va, map);
345554e4ee71SNavdeep Parhar if (tag)
345654e4ee71SNavdeep Parhar bus_dma_tag_destroy(tag);
345754e4ee71SNavdeep Parhar
345854e4ee71SNavdeep Parhar return (0);
345954e4ee71SNavdeep Parhar }
346054e4ee71SNavdeep Parhar
346154e4ee71SNavdeep Parhar /*
346243bbae19SNavdeep Parhar * Allocates the software resources (mainly memory and sysctl nodes) for an
346343bbae19SNavdeep Parhar * ingress queue and an optional freelist.
346454e4ee71SNavdeep Parhar *
346543bbae19SNavdeep Parhar * Sets IQ_SW_ALLOCATED and returns 0 on success.
346654e4ee71SNavdeep Parhar */
346754e4ee71SNavdeep Parhar static int
alloc_iq_fl(struct vi_info * vi,struct sge_iq * iq,struct sge_fl * fl,struct sysctl_ctx_list * ctx,struct sysctl_oid * oid)3468fe2ebb76SJohn Baldwin alloc_iq_fl(struct vi_info *vi, struct sge_iq *iq, struct sge_fl *fl,
346943bbae19SNavdeep Parhar struct sysctl_ctx_list *ctx, struct sysctl_oid *oid)
347054e4ee71SNavdeep Parhar {
347143bbae19SNavdeep Parhar int rc;
347254e4ee71SNavdeep Parhar size_t len;
347343bbae19SNavdeep Parhar struct adapter *sc = vi->adapter;
347443bbae19SNavdeep Parhar
347543bbae19SNavdeep Parhar MPASS(!(iq->flags & IQ_SW_ALLOCATED));
347654e4ee71SNavdeep Parhar
3477b2daa9a9SNavdeep Parhar len = iq->qsize * IQ_ESIZE;
347854e4ee71SNavdeep Parhar rc = alloc_ring(sc, len, &iq->desc_tag, &iq->desc_map, &iq->ba,
347954e4ee71SNavdeep Parhar (void **)&iq->desc);
348054e4ee71SNavdeep Parhar if (rc != 0)
348154e4ee71SNavdeep Parhar return (rc);
348254e4ee71SNavdeep Parhar
348343bbae19SNavdeep Parhar if (fl) {
348443bbae19SNavdeep Parhar len = fl->qsize * EQ_ESIZE;
348543bbae19SNavdeep Parhar rc = alloc_ring(sc, len, &fl->desc_tag, &fl->desc_map,
348643bbae19SNavdeep Parhar &fl->ba, (void **)&fl->desc);
348743bbae19SNavdeep Parhar if (rc) {
348843bbae19SNavdeep Parhar free_ring(sc, iq->desc_tag, iq->desc_map, iq->ba,
348943bbae19SNavdeep Parhar iq->desc);
349043bbae19SNavdeep Parhar return (rc);
349143bbae19SNavdeep Parhar }
349243bbae19SNavdeep Parhar
349343bbae19SNavdeep Parhar /* Allocate space for one software descriptor per buffer. */
349443bbae19SNavdeep Parhar fl->sdesc = malloc(fl->sidx * 8 * sizeof(struct fl_sdesc),
349543bbae19SNavdeep Parhar M_CXGBE, M_ZERO | M_WAITOK);
349643bbae19SNavdeep Parhar
349743bbae19SNavdeep Parhar add_fl_sysctls(sc, ctx, oid, fl);
349843bbae19SNavdeep Parhar iq->flags |= IQ_HAS_FL;
349943bbae19SNavdeep Parhar }
350043bbae19SNavdeep Parhar add_iq_sysctls(ctx, oid, iq);
350143bbae19SNavdeep Parhar iq->flags |= IQ_SW_ALLOCATED;
350243bbae19SNavdeep Parhar
350343bbae19SNavdeep Parhar return (0);
350443bbae19SNavdeep Parhar }
350543bbae19SNavdeep Parhar
350643bbae19SNavdeep Parhar /*
350743bbae19SNavdeep Parhar * Frees all software resources (memory and locks) associated with an ingress
350843bbae19SNavdeep Parhar * queue and an optional freelist.
350943bbae19SNavdeep Parhar */
351043bbae19SNavdeep Parhar static void
free_iq_fl(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)351143bbae19SNavdeep Parhar free_iq_fl(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
351243bbae19SNavdeep Parhar {
351343bbae19SNavdeep Parhar MPASS(iq->flags & IQ_SW_ALLOCATED);
351443bbae19SNavdeep Parhar
351543bbae19SNavdeep Parhar if (fl) {
351643bbae19SNavdeep Parhar MPASS(iq->flags & IQ_HAS_FL);
351743bbae19SNavdeep Parhar free_ring(sc, fl->desc_tag, fl->desc_map, fl->ba, fl->desc);
351843bbae19SNavdeep Parhar free_fl_buffers(sc, fl);
351943bbae19SNavdeep Parhar free(fl->sdesc, M_CXGBE);
352043bbae19SNavdeep Parhar mtx_destroy(&fl->fl_lock);
352143bbae19SNavdeep Parhar bzero(fl, sizeof(*fl));
352243bbae19SNavdeep Parhar }
352343bbae19SNavdeep Parhar free_ring(sc, iq->desc_tag, iq->desc_map, iq->ba, iq->desc);
352443bbae19SNavdeep Parhar bzero(iq, sizeof(*iq));
352543bbae19SNavdeep Parhar }
352643bbae19SNavdeep Parhar
352743bbae19SNavdeep Parhar /*
352843bbae19SNavdeep Parhar * Allocates a hardware ingress queue and an optional freelist that will be
352943bbae19SNavdeep Parhar * associated with it.
353043bbae19SNavdeep Parhar *
353143bbae19SNavdeep Parhar * Returns errno on failure. Resources allocated up to that point may still be
353243bbae19SNavdeep Parhar * allocated. Caller is responsible for cleanup in case this function fails.
353343bbae19SNavdeep Parhar */
353443bbae19SNavdeep Parhar static int
alloc_iq_fl_hwq(struct vi_info * vi,struct sge_iq * iq,struct sge_fl * fl)353543bbae19SNavdeep Parhar alloc_iq_fl_hwq(struct vi_info *vi, struct sge_iq *iq, struct sge_fl *fl)
353643bbae19SNavdeep Parhar {
3537df275ae5SNavdeep Parhar int rc, cntxt_id, cong_map;
353843bbae19SNavdeep Parhar struct fw_iq_cmd c;
353943bbae19SNavdeep Parhar struct adapter *sc = vi->adapter;
3540df275ae5SNavdeep Parhar struct port_info *pi = vi->pi;
354143bbae19SNavdeep Parhar __be32 v = 0;
354243bbae19SNavdeep Parhar
354343bbae19SNavdeep Parhar MPASS (!(iq->flags & IQ_HW_ALLOCATED));
354443bbae19SNavdeep Parhar
354554e4ee71SNavdeep Parhar bzero(&c, sizeof(c));
354654e4ee71SNavdeep Parhar c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_IQ_CMD) | F_FW_CMD_REQUEST |
354754e4ee71SNavdeep Parhar F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_IQ_CMD_PFN(sc->pf) |
354854e4ee71SNavdeep Parhar V_FW_IQ_CMD_VFN(0));
354954e4ee71SNavdeep Parhar
355054e4ee71SNavdeep Parhar c.alloc_to_len16 = htobe32(F_FW_IQ_CMD_ALLOC | F_FW_IQ_CMD_IQSTART |
355154e4ee71SNavdeep Parhar FW_LEN16(c));
355254e4ee71SNavdeep Parhar
355354e4ee71SNavdeep Parhar /* Special handling for firmware event queue */
355454e4ee71SNavdeep Parhar if (iq == &sc->sge.fwq)
355554e4ee71SNavdeep Parhar v |= F_FW_IQ_CMD_IQASYNCH;
355654e4ee71SNavdeep Parhar
355743bbae19SNavdeep Parhar if (iq->intr_idx < 0) {
3558f549e352SNavdeep Parhar /* Forwarded interrupts, all headed to fwq */
3559f549e352SNavdeep Parhar v |= F_FW_IQ_CMD_IQANDST;
3560f549e352SNavdeep Parhar v |= V_FW_IQ_CMD_IQANDSTINDEX(sc->sge.fwq.cntxt_id);
3561f549e352SNavdeep Parhar } else {
356243bbae19SNavdeep Parhar KASSERT(iq->intr_idx < sc->intr_count,
356343bbae19SNavdeep Parhar ("%s: invalid direct intr_idx %d", __func__, iq->intr_idx));
356443bbae19SNavdeep Parhar v |= V_FW_IQ_CMD_IQANDSTINDEX(iq->intr_idx);
3565f549e352SNavdeep Parhar }
356654e4ee71SNavdeep Parhar
356743bbae19SNavdeep Parhar bzero(iq->desc, iq->qsize * IQ_ESIZE);
356854e4ee71SNavdeep Parhar c.type_to_iqandstindex = htobe32(v |
356954e4ee71SNavdeep Parhar V_FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
3570fe2ebb76SJohn Baldwin V_FW_IQ_CMD_VIID(vi->viid) |
357154e4ee71SNavdeep Parhar V_FW_IQ_CMD_IQANUD(X_UPDATEDELIVERY_INTERRUPT));
3572df275ae5SNavdeep Parhar c.iqdroprss_to_iqesize = htobe16(V_FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
357354e4ee71SNavdeep Parhar F_FW_IQ_CMD_IQGTSMODE |
357454e4ee71SNavdeep Parhar V_FW_IQ_CMD_IQINTCNTTHRESH(iq->intr_pktc_idx) |
3575b2daa9a9SNavdeep Parhar V_FW_IQ_CMD_IQESIZE(ilog2(IQ_ESIZE) - 4));
357654e4ee71SNavdeep Parhar c.iqsize = htobe16(iq->qsize);
357754e4ee71SNavdeep Parhar c.iqaddr = htobe64(iq->ba);
3578c387ff00SNavdeep Parhar c.iqns_to_fl0congen = htobe32(V_FW_IQ_CMD_IQTYPE(iq->qtype));
3579df275ae5SNavdeep Parhar if (iq->cong_drop != -1) {
3580df275ae5SNavdeep Parhar cong_map = iq->qtype == IQ_ETH ? pi->rx_e_chan_map : 0;
3581c387ff00SNavdeep Parhar c.iqns_to_fl0congen |= htobe32(F_FW_IQ_CMD_IQFLINTCONGEN);
3582df275ae5SNavdeep Parhar }
358354e4ee71SNavdeep Parhar
358454e4ee71SNavdeep Parhar if (fl) {
358543bbae19SNavdeep Parhar bzero(fl->desc, fl->sidx * EQ_ESIZE + sc->params.sge.spg_len);
3586214c3582SNavdeep Parhar c.iqns_to_fl0congen |=
3587bc14b14dSNavdeep Parhar htobe32(V_FW_IQ_CMD_FL0HOSTFCMODE(X_HOSTFCMODE_NONE) |
3588bc14b14dSNavdeep Parhar F_FW_IQ_CMD_FL0FETCHRO | F_FW_IQ_CMD_FL0DATARO |
35891458bff9SNavdeep Parhar (fl_pad ? F_FW_IQ_CMD_FL0PADEN : 0) |
35901458bff9SNavdeep Parhar (fl->flags & FL_BUF_PACKING ? F_FW_IQ_CMD_FL0PACKEN :
35911458bff9SNavdeep Parhar 0));
3592df275ae5SNavdeep Parhar if (iq->cong_drop != -1) {
3593bc14b14dSNavdeep Parhar c.iqns_to_fl0congen |=
3594df275ae5SNavdeep Parhar htobe32(V_FW_IQ_CMD_FL0CNGCHMAP(cong_map) |
3595bc14b14dSNavdeep Parhar F_FW_IQ_CMD_FL0CONGCIF |
3596bc14b14dSNavdeep Parhar F_FW_IQ_CMD_FL0CONGEN);
3597bc14b14dSNavdeep Parhar }
359854e4ee71SNavdeep Parhar c.fl0dcaen_to_fl0cidxfthresh =
3599ed7e5640SNavdeep Parhar htobe16(V_FW_IQ_CMD_FL0FBMIN(chip_id(sc) <= CHELSIO_T5 ?
3600adb0cd84SNavdeep Parhar X_FETCHBURSTMIN_128B : X_FETCHBURSTMIN_64B_T6) |
3601ed7e5640SNavdeep Parhar V_FW_IQ_CMD_FL0FBMAX(chip_id(sc) <= CHELSIO_T5 ?
3602ed7e5640SNavdeep Parhar X_FETCHBURSTMAX_512B : X_FETCHBURSTMAX_256B));
360354e4ee71SNavdeep Parhar c.fl0size = htobe16(fl->qsize);
360454e4ee71SNavdeep Parhar c.fl0addr = htobe64(fl->ba);
360554e4ee71SNavdeep Parhar }
360654e4ee71SNavdeep Parhar
360754e4ee71SNavdeep Parhar rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
360854e4ee71SNavdeep Parhar if (rc != 0) {
360943bbae19SNavdeep Parhar CH_ERR(sc, "failed to create hw ingress queue: %d\n", rc);
361054e4ee71SNavdeep Parhar return (rc);
361154e4ee71SNavdeep Parhar }
361254e4ee71SNavdeep Parhar
361354e4ee71SNavdeep Parhar iq->cidx = 0;
3614b2daa9a9SNavdeep Parhar iq->gen = F_RSPD_GEN;
361554e4ee71SNavdeep Parhar iq->cntxt_id = be16toh(c.iqid);
361654e4ee71SNavdeep Parhar iq->abs_id = be16toh(c.physiqid);
361754e4ee71SNavdeep Parhar
361854e4ee71SNavdeep Parhar cntxt_id = iq->cntxt_id - sc->sge.iq_start;
3619b20b25e7SNavdeep Parhar if (cntxt_id >= sc->sge.iqmap_sz) {
3620733b9277SNavdeep Parhar panic ("%s: iq->cntxt_id (%d) more than the max (%d)", __func__,
3621b20b25e7SNavdeep Parhar cntxt_id, sc->sge.iqmap_sz - 1);
3622733b9277SNavdeep Parhar }
362354e4ee71SNavdeep Parhar sc->sge.iqmap[cntxt_id] = iq;
362454e4ee71SNavdeep Parhar
362554e4ee71SNavdeep Parhar if (fl) {
36264d6db4e0SNavdeep Parhar u_int qid;
362743bbae19SNavdeep Parhar #ifdef INVARIANTS
3628df275ae5SNavdeep Parhar int i;
3629df275ae5SNavdeep Parhar
363043bbae19SNavdeep Parhar MPASS(!(fl->flags & FL_BUF_RESUME));
363143bbae19SNavdeep Parhar for (i = 0; i < fl->sidx * 8; i++)
363243bbae19SNavdeep Parhar MPASS(fl->sdesc[i].cl == NULL);
363343bbae19SNavdeep Parhar #endif
363454e4ee71SNavdeep Parhar fl->cntxt_id = be16toh(c.fl0id);
363543bbae19SNavdeep Parhar fl->pidx = fl->cidx = fl->hw_cidx = fl->dbidx = 0;
363643bbae19SNavdeep Parhar fl->rx_offset = 0;
363743bbae19SNavdeep Parhar fl->flags &= ~(FL_STARVING | FL_DOOMED);
363854e4ee71SNavdeep Parhar
36399f1f7ec9SNavdeep Parhar cntxt_id = fl->cntxt_id - sc->sge.eq_start;
3640b20b25e7SNavdeep Parhar if (cntxt_id >= sc->sge.eqmap_sz) {
3641733b9277SNavdeep Parhar panic("%s: fl->cntxt_id (%d) more than the max (%d)",
3642b20b25e7SNavdeep Parhar __func__, cntxt_id, sc->sge.eqmap_sz - 1);
3643733b9277SNavdeep Parhar }
364454e4ee71SNavdeep Parhar sc->sge.eqmap[cntxt_id] = (void *)fl;
364554e4ee71SNavdeep Parhar
36464d6db4e0SNavdeep Parhar qid = fl->cntxt_id;
36474d6db4e0SNavdeep Parhar if (isset(&sc->doorbells, DOORBELL_UDB)) {
364890e7434aSNavdeep Parhar uint32_t s_qpp = sc->params.sge.eq_s_qpp;
36494d6db4e0SNavdeep Parhar uint32_t mask = (1 << s_qpp) - 1;
36504d6db4e0SNavdeep Parhar volatile uint8_t *udb;
36514d6db4e0SNavdeep Parhar
36524d6db4e0SNavdeep Parhar udb = sc->udbs_base + UDBS_DB_OFFSET;
36534d6db4e0SNavdeep Parhar udb += (qid >> s_qpp) << PAGE_SHIFT;
36544d6db4e0SNavdeep Parhar qid &= mask;
36554d6db4e0SNavdeep Parhar if (qid < PAGE_SIZE / UDBS_SEG_SIZE) {
36564d6db4e0SNavdeep Parhar udb += qid << UDBS_SEG_SHIFT;
36574d6db4e0SNavdeep Parhar qid = 0;
36584d6db4e0SNavdeep Parhar }
36594d6db4e0SNavdeep Parhar fl->udb = (volatile void *)udb;
36604d6db4e0SNavdeep Parhar }
3661d1205d09SNavdeep Parhar fl->dbval = V_QID(qid) | sc->chip_params->sge_fl_db;
36624d6db4e0SNavdeep Parhar
366354e4ee71SNavdeep Parhar FL_LOCK(fl);
3664733b9277SNavdeep Parhar /* Enough to make sure the SGE doesn't think it's starved */
3665733b9277SNavdeep Parhar refill_fl(sc, fl, fl->lowat);
366654e4ee71SNavdeep Parhar FL_UNLOCK(fl);
366754e4ee71SNavdeep Parhar }
366854e4ee71SNavdeep Parhar
3669df275ae5SNavdeep Parhar if (chip_id(sc) >= CHELSIO_T5 && !(sc->flags & IS_VF) &&
3670df275ae5SNavdeep Parhar iq->cong_drop != -1) {
3671df275ae5SNavdeep Parhar t4_sge_set_conm_context(sc, iq->cntxt_id, iq->cong_drop,
3672df275ae5SNavdeep Parhar cong_map);
3673ba41ec48SNavdeep Parhar }
3674ba41ec48SNavdeep Parhar
367554e4ee71SNavdeep Parhar /* Enable IQ interrupts */
3676733b9277SNavdeep Parhar atomic_store_rel_int(&iq->state, IQS_IDLE);
3677315048f2SJohn Baldwin t4_write_reg(sc, sc->sge_gts_reg, V_SEINTARM(iq->intr_params) |
367854e4ee71SNavdeep Parhar V_INGRESSQID(iq->cntxt_id));
367954e4ee71SNavdeep Parhar
368043bbae19SNavdeep Parhar iq->flags |= IQ_HW_ALLOCATED;
368143bbae19SNavdeep Parhar
368254e4ee71SNavdeep Parhar return (0);
368354e4ee71SNavdeep Parhar }
368454e4ee71SNavdeep Parhar
368554e4ee71SNavdeep Parhar static int
free_iq_fl_hwq(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)368643bbae19SNavdeep Parhar free_iq_fl_hwq(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
368754e4ee71SNavdeep Parhar {
368838035ed6SNavdeep Parhar int rc;
368954e4ee71SNavdeep Parhar
369043bbae19SNavdeep Parhar MPASS(iq->flags & IQ_HW_ALLOCATED);
369143bbae19SNavdeep Parhar rc = -t4_iq_free(sc, sc->mbox, sc->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
369243bbae19SNavdeep Parhar iq->cntxt_id, fl ? fl->cntxt_id : 0xffff, 0xffff);
369354e4ee71SNavdeep Parhar if (rc != 0) {
369443bbae19SNavdeep Parhar CH_ERR(sc, "failed to free iq %p: %d\n", iq, rc);
369554e4ee71SNavdeep Parhar return (rc);
369654e4ee71SNavdeep Parhar }
369743bbae19SNavdeep Parhar iq->flags &= ~IQ_HW_ALLOCATED;
369854e4ee71SNavdeep Parhar
369954e4ee71SNavdeep Parhar return (0);
370054e4ee71SNavdeep Parhar }
370154e4ee71SNavdeep Parhar
370238035ed6SNavdeep Parhar static void
add_iq_sysctls(struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_iq * iq)3703348694daSNavdeep Parhar add_iq_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid,
3704348694daSNavdeep Parhar struct sge_iq *iq)
3705348694daSNavdeep Parhar {
370643bbae19SNavdeep Parhar struct sysctl_oid_list *children;
3707348694daSNavdeep Parhar
370843bbae19SNavdeep Parhar if (ctx == NULL || oid == NULL)
370943bbae19SNavdeep Parhar return;
371043bbae19SNavdeep Parhar
371143bbae19SNavdeep Parhar children = SYSCTL_CHILDREN(oid);
3712348694daSNavdeep Parhar SYSCTL_ADD_UAUTO(ctx, children, OID_AUTO, "ba", CTLFLAG_RD, &iq->ba,
3713348694daSNavdeep Parhar "bus address of descriptor ring");
3714348694daSNavdeep Parhar SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dmalen", CTLFLAG_RD, NULL,
3715348694daSNavdeep Parhar iq->qsize * IQ_ESIZE, "descriptor ring size in bytes");
3716473f6163SNavdeep Parhar SYSCTL_ADD_U16(ctx, children, OID_AUTO, "abs_id", CTLFLAG_RD,
3717473f6163SNavdeep Parhar &iq->abs_id, 0, "absolute id of the queue");
3718473f6163SNavdeep Parhar SYSCTL_ADD_U16(ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
3719473f6163SNavdeep Parhar &iq->cntxt_id, 0, "SGE context id of the queue");
3720473f6163SNavdeep Parhar SYSCTL_ADD_U16(ctx, children, OID_AUTO, "cidx", CTLFLAG_RD, &iq->cidx,
3721473f6163SNavdeep Parhar 0, "consumer index");
3722348694daSNavdeep Parhar }
3723348694daSNavdeep Parhar
3724348694daSNavdeep Parhar static void
add_fl_sysctls(struct adapter * sc,struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_fl * fl)3725aa93b99aSNavdeep Parhar add_fl_sysctls(struct adapter *sc, struct sysctl_ctx_list *ctx,
3726aa93b99aSNavdeep Parhar struct sysctl_oid *oid, struct sge_fl *fl)
372738035ed6SNavdeep Parhar {
372843bbae19SNavdeep Parhar struct sysctl_oid_list *children;
372938035ed6SNavdeep Parhar
373043bbae19SNavdeep Parhar if (ctx == NULL || oid == NULL)
373143bbae19SNavdeep Parhar return;
373243bbae19SNavdeep Parhar
373343bbae19SNavdeep Parhar children = SYSCTL_CHILDREN(oid);
37347029da5cSPawel Biernacki oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "fl",
37357029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "freelist");
373638035ed6SNavdeep Parhar children = SYSCTL_CHILDREN(oid);
373738035ed6SNavdeep Parhar
3738aa93b99aSNavdeep Parhar SYSCTL_ADD_UAUTO(ctx, children, OID_AUTO, "ba", CTLFLAG_RD,
3739aa93b99aSNavdeep Parhar &fl->ba, "bus address of descriptor ring");
3740aa93b99aSNavdeep Parhar SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dmalen", CTLFLAG_RD, NULL,
3741aa93b99aSNavdeep Parhar fl->sidx * EQ_ESIZE + sc->params.sge.spg_len,
3742aa93b99aSNavdeep Parhar "desc ring size in bytes");
3743473f6163SNavdeep Parhar SYSCTL_ADD_U16(ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
3744473f6163SNavdeep Parhar &fl->cntxt_id, 0, "SGE context id of the freelist");
3745e3207e19SNavdeep Parhar SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "padding", CTLFLAG_RD, NULL,
3746e3207e19SNavdeep Parhar fl_pad ? 1 : 0, "padding enabled");
3747e3207e19SNavdeep Parhar SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "packing", CTLFLAG_RD, NULL,
3748e3207e19SNavdeep Parhar fl->flags & FL_BUF_PACKING ? 1 : 0, "packing enabled");
374938035ed6SNavdeep Parhar SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cidx", CTLFLAG_RD, &fl->cidx,
375038035ed6SNavdeep Parhar 0, "consumer index");
375138035ed6SNavdeep Parhar if (fl->flags & FL_BUF_PACKING) {
375238035ed6SNavdeep Parhar SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_offset",
375338035ed6SNavdeep Parhar CTLFLAG_RD, &fl->rx_offset, 0, "packing rx offset");
375438035ed6SNavdeep Parhar }
375538035ed6SNavdeep Parhar SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "pidx", CTLFLAG_RD, &fl->pidx,
375638035ed6SNavdeep Parhar 0, "producer index");
375738035ed6SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_allocated",
375838035ed6SNavdeep Parhar CTLFLAG_RD, &fl->cl_allocated, "# of clusters allocated");
375938035ed6SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_recycled",
376038035ed6SNavdeep Parhar CTLFLAG_RD, &fl->cl_recycled, "# of clusters recycled");
376138035ed6SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_fast_recycled",
376238035ed6SNavdeep Parhar CTLFLAG_RD, &fl->cl_fast_recycled, "# of clusters recycled (fast)");
376338035ed6SNavdeep Parhar }
376438035ed6SNavdeep Parhar
376543bbae19SNavdeep Parhar /*
376643bbae19SNavdeep Parhar * Idempotent.
376743bbae19SNavdeep Parhar */
376854e4ee71SNavdeep Parhar static int
alloc_fwq(struct adapter * sc)3769733b9277SNavdeep Parhar alloc_fwq(struct adapter *sc)
377054e4ee71SNavdeep Parhar {
3771733b9277SNavdeep Parhar int rc, intr_idx;
377256599263SNavdeep Parhar struct sge_iq *fwq = &sc->sge.fwq;
377343bbae19SNavdeep Parhar struct vi_info *vi = &sc->port[0]->vi[0];
377456599263SNavdeep Parhar
377543bbae19SNavdeep Parhar if (!(fwq->flags & IQ_SW_ALLOCATED)) {
377643bbae19SNavdeep Parhar MPASS(!(fwq->flags & IQ_HW_ALLOCATED));
377743bbae19SNavdeep Parhar
37786af45170SJohn Baldwin if (sc->flags & IS_VF)
37796af45170SJohn Baldwin intr_idx = 0;
37804535e804SNavdeep Parhar else
3781733b9277SNavdeep Parhar intr_idx = sc->intr_count > 1 ? 1 : 0;
3782c387ff00SNavdeep Parhar init_iq(fwq, sc, 0, 0, FW_IQ_QSIZE, intr_idx, -1, IQ_OTHER);
378343bbae19SNavdeep Parhar rc = alloc_iq_fl(vi, fwq, NULL, &sc->ctx, sc->fwq_oid);
3784733b9277SNavdeep Parhar if (rc != 0) {
378543bbae19SNavdeep Parhar CH_ERR(sc, "failed to allocate fwq: %d\n", rc);
378656599263SNavdeep Parhar return (rc);
3787733b9277SNavdeep Parhar }
378843bbae19SNavdeep Parhar MPASS(fwq->flags & IQ_SW_ALLOCATED);
378943bbae19SNavdeep Parhar }
379056599263SNavdeep Parhar
379143bbae19SNavdeep Parhar if (!(fwq->flags & IQ_HW_ALLOCATED)) {
379243bbae19SNavdeep Parhar MPASS(fwq->flags & IQ_SW_ALLOCATED);
379343bbae19SNavdeep Parhar
379443bbae19SNavdeep Parhar rc = alloc_iq_fl_hwq(vi, fwq, NULL);
379543bbae19SNavdeep Parhar if (rc != 0) {
379643bbae19SNavdeep Parhar CH_ERR(sc, "failed to create hw fwq: %d\n", rc);
379743bbae19SNavdeep Parhar return (rc);
379843bbae19SNavdeep Parhar }
379943bbae19SNavdeep Parhar MPASS(fwq->flags & IQ_HW_ALLOCATED);
380043bbae19SNavdeep Parhar }
380156599263SNavdeep Parhar
3802733b9277SNavdeep Parhar return (0);
3803733b9277SNavdeep Parhar }
3804733b9277SNavdeep Parhar
380543bbae19SNavdeep Parhar /*
380643bbae19SNavdeep Parhar * Idempotent.
380743bbae19SNavdeep Parhar */
380843bbae19SNavdeep Parhar static void
free_fwq(struct adapter * sc)3809733b9277SNavdeep Parhar free_fwq(struct adapter *sc)
3810733b9277SNavdeep Parhar {
381143bbae19SNavdeep Parhar struct sge_iq *fwq = &sc->sge.fwq;
381243bbae19SNavdeep Parhar
381343bbae19SNavdeep Parhar if (fwq->flags & IQ_HW_ALLOCATED) {
381443bbae19SNavdeep Parhar MPASS(fwq->flags & IQ_SW_ALLOCATED);
381543bbae19SNavdeep Parhar free_iq_fl_hwq(sc, fwq, NULL);
381643bbae19SNavdeep Parhar MPASS(!(fwq->flags & IQ_HW_ALLOCATED));
3817733b9277SNavdeep Parhar }
3818733b9277SNavdeep Parhar
381943bbae19SNavdeep Parhar if (fwq->flags & IQ_SW_ALLOCATED) {
382043bbae19SNavdeep Parhar MPASS(!(fwq->flags & IQ_HW_ALLOCATED));
382143bbae19SNavdeep Parhar free_iq_fl(sc, fwq, NULL);
382243bbae19SNavdeep Parhar MPASS(!(fwq->flags & IQ_SW_ALLOCATED));
382343bbae19SNavdeep Parhar }
382443bbae19SNavdeep Parhar }
382543bbae19SNavdeep Parhar
382643bbae19SNavdeep Parhar /*
382743bbae19SNavdeep Parhar * Idempotent.
382843bbae19SNavdeep Parhar */
3829733b9277SNavdeep Parhar static int
alloc_ctrlq(struct adapter * sc,int idx)383043bbae19SNavdeep Parhar alloc_ctrlq(struct adapter *sc, int idx)
3831733b9277SNavdeep Parhar {
3832733b9277SNavdeep Parhar int rc;
3833733b9277SNavdeep Parhar char name[16];
383443bbae19SNavdeep Parhar struct sysctl_oid *oid;
383543bbae19SNavdeep Parhar struct sge_wrq *ctrlq = &sc->sge.ctrlq[idx];
3836733b9277SNavdeep Parhar
383743bbae19SNavdeep Parhar MPASS(idx < sc->params.nports);
383837310a98SNavdeep Parhar
383943bbae19SNavdeep Parhar if (!(ctrlq->eq.flags & EQ_SW_ALLOCATED)) {
384043bbae19SNavdeep Parhar MPASS(!(ctrlq->eq.flags & EQ_HW_ALLOCATED));
384143bbae19SNavdeep Parhar
384237310a98SNavdeep Parhar snprintf(name, sizeof(name), "%d", idx);
384343bbae19SNavdeep Parhar oid = SYSCTL_ADD_NODE(&sc->ctx, SYSCTL_CHILDREN(sc->ctrlq_oid),
384443bbae19SNavdeep Parhar OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
384543bbae19SNavdeep Parhar "ctrl queue");
384637310a98SNavdeep Parhar
384743bbae19SNavdeep Parhar snprintf(name, sizeof(name), "%s ctrlq%d",
384843bbae19SNavdeep Parhar device_get_nameunit(sc->dev), idx);
3849857d74b6SNavdeep Parhar init_eq(sc, &ctrlq->eq, EQ_CTRL, CTRL_EQ_QSIZE, idx,
3850857d74b6SNavdeep Parhar &sc->sge.fwq, name);
385143bbae19SNavdeep Parhar rc = alloc_wrq(sc, NULL, ctrlq, &sc->ctx, oid);
385243bbae19SNavdeep Parhar if (rc != 0) {
385343bbae19SNavdeep Parhar CH_ERR(sc, "failed to allocate ctrlq%d: %d\n", idx, rc);
385443bbae19SNavdeep Parhar sysctl_remove_oid(oid, 1, 1);
385556599263SNavdeep Parhar return (rc);
385656599263SNavdeep Parhar }
385743bbae19SNavdeep Parhar MPASS(ctrlq->eq.flags & EQ_SW_ALLOCATED);
385843bbae19SNavdeep Parhar }
385943bbae19SNavdeep Parhar
386043bbae19SNavdeep Parhar if (!(ctrlq->eq.flags & EQ_HW_ALLOCATED)) {
386143bbae19SNavdeep Parhar MPASS(ctrlq->eq.flags & EQ_SW_ALLOCATED);
3862*0a9d1da6SNavdeep Parhar MPASS(ctrlq->nwr_pending == 0);
3863*0a9d1da6SNavdeep Parhar MPASS(ctrlq->ndesc_needed == 0);
386443bbae19SNavdeep Parhar
386543bbae19SNavdeep Parhar rc = alloc_eq_hwq(sc, NULL, &ctrlq->eq);
386643bbae19SNavdeep Parhar if (rc != 0) {
386743bbae19SNavdeep Parhar CH_ERR(sc, "failed to create hw ctrlq%d: %d\n", idx, rc);
386843bbae19SNavdeep Parhar return (rc);
386943bbae19SNavdeep Parhar }
387043bbae19SNavdeep Parhar MPASS(ctrlq->eq.flags & EQ_HW_ALLOCATED);
387143bbae19SNavdeep Parhar }
387243bbae19SNavdeep Parhar
387343bbae19SNavdeep Parhar return (0);
387443bbae19SNavdeep Parhar }
387543bbae19SNavdeep Parhar
387643bbae19SNavdeep Parhar /*
387743bbae19SNavdeep Parhar * Idempotent.
387843bbae19SNavdeep Parhar */
387943bbae19SNavdeep Parhar static void
free_ctrlq(struct adapter * sc,int idx)388043bbae19SNavdeep Parhar free_ctrlq(struct adapter *sc, int idx)
388143bbae19SNavdeep Parhar {
388243bbae19SNavdeep Parhar struct sge_wrq *ctrlq = &sc->sge.ctrlq[idx];
388343bbae19SNavdeep Parhar
388443bbae19SNavdeep Parhar if (ctrlq->eq.flags & EQ_HW_ALLOCATED) {
388543bbae19SNavdeep Parhar MPASS(ctrlq->eq.flags & EQ_SW_ALLOCATED);
388643bbae19SNavdeep Parhar free_eq_hwq(sc, NULL, &ctrlq->eq);
388743bbae19SNavdeep Parhar MPASS(!(ctrlq->eq.flags & EQ_HW_ALLOCATED));
388843bbae19SNavdeep Parhar }
388943bbae19SNavdeep Parhar
389043bbae19SNavdeep Parhar if (ctrlq->eq.flags & EQ_SW_ALLOCATED) {
389143bbae19SNavdeep Parhar MPASS(!(ctrlq->eq.flags & EQ_HW_ALLOCATED));
389243bbae19SNavdeep Parhar free_wrq(sc, ctrlq);
389343bbae19SNavdeep Parhar MPASS(!(ctrlq->eq.flags & EQ_SW_ALLOCATED));
389443bbae19SNavdeep Parhar }
389543bbae19SNavdeep Parhar }
389656599263SNavdeep Parhar
38971605bac6SNavdeep Parhar int
t4_sge_set_conm_context(struct adapter * sc,int cntxt_id,int cong_drop,int cong_map)3898df275ae5SNavdeep Parhar t4_sge_set_conm_context(struct adapter *sc, int cntxt_id, int cong_drop,
3899df275ae5SNavdeep Parhar int cong_map)
39009fb8886bSNavdeep Parhar {
3901df275ae5SNavdeep Parhar const int cng_ch_bits_log = sc->chip_params->cng_ch_bits_log;
3902df275ae5SNavdeep Parhar uint32_t param, val;
3903df275ae5SNavdeep Parhar uint16_t ch_map;
3904df275ae5SNavdeep Parhar int cong_mode, rc, i;
39059fb8886bSNavdeep Parhar
3906df275ae5SNavdeep Parhar if (chip_id(sc) < CHELSIO_T5)
3907df275ae5SNavdeep Parhar return (ENOTSUP);
3908df275ae5SNavdeep Parhar
3909df275ae5SNavdeep Parhar /* Convert the driver knob to the mode understood by the firmware. */
3910df275ae5SNavdeep Parhar switch (cong_drop) {
3911df275ae5SNavdeep Parhar case -1:
3912df275ae5SNavdeep Parhar cong_mode = X_CONMCTXT_CNGTPMODE_DISABLE;
3913df275ae5SNavdeep Parhar break;
3914df275ae5SNavdeep Parhar case 0:
3915df275ae5SNavdeep Parhar cong_mode = X_CONMCTXT_CNGTPMODE_CHANNEL;
3916df275ae5SNavdeep Parhar break;
3917df275ae5SNavdeep Parhar case 1:
3918df275ae5SNavdeep Parhar cong_mode = X_CONMCTXT_CNGTPMODE_QUEUE;
3919df275ae5SNavdeep Parhar break;
3920df275ae5SNavdeep Parhar case 2:
3921df275ae5SNavdeep Parhar cong_mode = X_CONMCTXT_CNGTPMODE_BOTH;
3922df275ae5SNavdeep Parhar break;
3923df275ae5SNavdeep Parhar default:
3924df275ae5SNavdeep Parhar MPASS(0);
3925df275ae5SNavdeep Parhar CH_ERR(sc, "cong_drop = %d is invalid (ingress queue %d).\n",
3926df275ae5SNavdeep Parhar cong_drop, cntxt_id);
3927df275ae5SNavdeep Parhar return (EINVAL);
3928df275ae5SNavdeep Parhar }
3929df275ae5SNavdeep Parhar
3930df275ae5SNavdeep Parhar param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
3931df275ae5SNavdeep Parhar V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
3932df275ae5SNavdeep Parhar V_FW_PARAMS_PARAM_YZ(cntxt_id);
3933df275ae5SNavdeep Parhar val = V_CONMCTXT_CNGTPMODE(cong_mode);
3934df275ae5SNavdeep Parhar if (cong_mode == X_CONMCTXT_CNGTPMODE_CHANNEL ||
3935df275ae5SNavdeep Parhar cong_mode == X_CONMCTXT_CNGTPMODE_BOTH) {
3936df275ae5SNavdeep Parhar for (i = 0, ch_map = 0; i < 4; i++) {
3937df275ae5SNavdeep Parhar if (cong_map & (1 << i))
3938df275ae5SNavdeep Parhar ch_map |= 1 << (i << cng_ch_bits_log);
3939df275ae5SNavdeep Parhar }
3940df275ae5SNavdeep Parhar val |= V_CONMCTXT_CNGCHMAP(ch_map);
3941df275ae5SNavdeep Parhar }
3942df275ae5SNavdeep Parhar rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
3943df275ae5SNavdeep Parhar if (rc != 0) {
3944df275ae5SNavdeep Parhar CH_ERR(sc, "failed to set congestion manager context "
3945df275ae5SNavdeep Parhar "for ingress queue %d: %d\n", cntxt_id, rc);
3946df275ae5SNavdeep Parhar }
3947df275ae5SNavdeep Parhar
3948df275ae5SNavdeep Parhar return (rc);
39499fb8886bSNavdeep Parhar }
39509fb8886bSNavdeep Parhar
395143bbae19SNavdeep Parhar /*
395243bbae19SNavdeep Parhar * Idempotent.
395343bbae19SNavdeep Parhar */
3954733b9277SNavdeep Parhar static int
alloc_rxq(struct vi_info * vi,struct sge_rxq * rxq,int idx,int intr_idx,int maxp)395543bbae19SNavdeep Parhar alloc_rxq(struct vi_info *vi, struct sge_rxq *rxq, int idx, int intr_idx,
395643bbae19SNavdeep Parhar int maxp)
395754e4ee71SNavdeep Parhar {
395854e4ee71SNavdeep Parhar int rc;
39597c228be3SNavdeep Parhar struct adapter *sc = vi->adapter;
3960954712e8SJustin Hibbits if_t ifp = vi->ifp;
396143bbae19SNavdeep Parhar struct sysctl_oid *oid;
396254e4ee71SNavdeep Parhar char name[16];
396354e4ee71SNavdeep Parhar
396443bbae19SNavdeep Parhar if (!(rxq->iq.flags & IQ_SW_ALLOCATED)) {
396543bbae19SNavdeep Parhar MPASS(!(rxq->iq.flags & IQ_HW_ALLOCATED));
396643bbae19SNavdeep Parhar #if defined(INET) || defined(INET6)
396743bbae19SNavdeep Parhar rc = tcp_lro_init_args(&rxq->lro, ifp, lro_entries, lro_mbufs);
396854e4ee71SNavdeep Parhar if (rc != 0)
396954e4ee71SNavdeep Parhar return (rc);
397043bbae19SNavdeep Parhar MPASS(rxq->lro.ifp == ifp); /* also indicates LRO init'ed */
397143bbae19SNavdeep Parhar #endif
397243bbae19SNavdeep Parhar rxq->ifp = ifp;
397343bbae19SNavdeep Parhar
397443bbae19SNavdeep Parhar snprintf(name, sizeof(name), "%d", idx);
397543bbae19SNavdeep Parhar oid = SYSCTL_ADD_NODE(&vi->ctx, SYSCTL_CHILDREN(vi->rxq_oid),
397643bbae19SNavdeep Parhar OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
397743bbae19SNavdeep Parhar "rx queue");
397843bbae19SNavdeep Parhar
397943bbae19SNavdeep Parhar init_iq(&rxq->iq, sc, vi->tmr_idx, vi->pktc_idx, vi->qsize_rxq,
3980df275ae5SNavdeep Parhar intr_idx, cong_drop, IQ_ETH);
3981df8437a9SAndrew Gallatin #if defined(INET) || defined(INET6)
3982954712e8SJustin Hibbits if (if_getcapenable(ifp) & IFCAP_LRO)
3983df8437a9SAndrew Gallatin rxq->iq.flags |= IQ_LRO_ENABLED;
3984df8437a9SAndrew Gallatin #endif
3985954712e8SJustin Hibbits if (if_getcapenable(ifp) & IFCAP_HWRXTSTMP)
3986df8437a9SAndrew Gallatin rxq->iq.flags |= IQ_RX_TIMESTAMP;
398743bbae19SNavdeep Parhar snprintf(name, sizeof(name), "%s rxq%d-fl",
398843bbae19SNavdeep Parhar device_get_nameunit(vi->dev), idx);
398943bbae19SNavdeep Parhar init_fl(sc, &rxq->fl, vi->qsize_rxq / 8, maxp, name);
399043bbae19SNavdeep Parhar rc = alloc_iq_fl(vi, &rxq->iq, &rxq->fl, &vi->ctx, oid);
399143bbae19SNavdeep Parhar if (rc != 0) {
399243bbae19SNavdeep Parhar CH_ERR(vi, "failed to allocate rxq%d: %d\n", idx, rc);
399343bbae19SNavdeep Parhar sysctl_remove_oid(oid, 1, 1);
399443bbae19SNavdeep Parhar #if defined(INET) || defined(INET6)
399543bbae19SNavdeep Parhar tcp_lro_free(&rxq->lro);
399643bbae19SNavdeep Parhar rxq->lro.ifp = NULL;
399743bbae19SNavdeep Parhar #endif
399843bbae19SNavdeep Parhar return (rc);
399943bbae19SNavdeep Parhar }
400043bbae19SNavdeep Parhar MPASS(rxq->iq.flags & IQ_SW_ALLOCATED);
400143bbae19SNavdeep Parhar add_rxq_sysctls(&vi->ctx, oid, rxq);
400243bbae19SNavdeep Parhar }
400343bbae19SNavdeep Parhar
400443bbae19SNavdeep Parhar if (!(rxq->iq.flags & IQ_HW_ALLOCATED)) {
400543bbae19SNavdeep Parhar MPASS(rxq->iq.flags & IQ_SW_ALLOCATED);
400643bbae19SNavdeep Parhar rc = alloc_iq_fl_hwq(vi, &rxq->iq, &rxq->fl);
400743bbae19SNavdeep Parhar if (rc != 0) {
400843bbae19SNavdeep Parhar CH_ERR(vi, "failed to create hw rxq%d: %d\n", idx, rc);
400943bbae19SNavdeep Parhar return (rc);
401043bbae19SNavdeep Parhar }
401143bbae19SNavdeep Parhar MPASS(rxq->iq.flags & IQ_HW_ALLOCATED);
401254e4ee71SNavdeep Parhar
4013ec55567cSJohn Baldwin if (idx == 0)
4014ec55567cSJohn Baldwin sc->sge.iq_base = rxq->iq.abs_id - rxq->iq.cntxt_id;
4015ec55567cSJohn Baldwin else
4016ec55567cSJohn Baldwin KASSERT(rxq->iq.cntxt_id + sc->sge.iq_base == rxq->iq.abs_id,
4017ec55567cSJohn Baldwin ("iq_base mismatch"));
4018ec55567cSJohn Baldwin KASSERT(sc->sge.iq_base == 0 || sc->flags & IS_VF,
4019ec55567cSJohn Baldwin ("PF with non-zero iq_base"));
4020ec55567cSJohn Baldwin
40214d6db4e0SNavdeep Parhar /*
402243bbae19SNavdeep Parhar * The freelist is just barely above the starvation threshold
402343bbae19SNavdeep Parhar * right now, fill it up a bit more.
40244d6db4e0SNavdeep Parhar */
40259b4d7b4eSNavdeep Parhar FL_LOCK(&rxq->fl);
4026ec55567cSJohn Baldwin refill_fl(sc, &rxq->fl, 128);
40279b4d7b4eSNavdeep Parhar FL_UNLOCK(&rxq->fl);
402854e4ee71SNavdeep Parhar }
402954e4ee71SNavdeep Parhar
403043bbae19SNavdeep Parhar return (0);
403143bbae19SNavdeep Parhar }
403243bbae19SNavdeep Parhar
403343bbae19SNavdeep Parhar /*
403443bbae19SNavdeep Parhar * Idempotent.
403543bbae19SNavdeep Parhar */
403643bbae19SNavdeep Parhar static void
free_rxq(struct vi_info * vi,struct sge_rxq * rxq)4037fe2ebb76SJohn Baldwin free_rxq(struct vi_info *vi, struct sge_rxq *rxq)
403854e4ee71SNavdeep Parhar {
403943bbae19SNavdeep Parhar if (rxq->iq.flags & IQ_HW_ALLOCATED) {
404043bbae19SNavdeep Parhar MPASS(rxq->iq.flags & IQ_SW_ALLOCATED);
404143bbae19SNavdeep Parhar free_iq_fl_hwq(vi->adapter, &rxq->iq, &rxq->fl);
404243bbae19SNavdeep Parhar MPASS(!(rxq->iq.flags & IQ_HW_ALLOCATED));
404354e4ee71SNavdeep Parhar }
404443bbae19SNavdeep Parhar
404543bbae19SNavdeep Parhar if (rxq->iq.flags & IQ_SW_ALLOCATED) {
404643bbae19SNavdeep Parhar MPASS(!(rxq->iq.flags & IQ_HW_ALLOCATED));
404743bbae19SNavdeep Parhar #if defined(INET) || defined(INET6)
404843bbae19SNavdeep Parhar tcp_lro_free(&rxq->lro);
404954e4ee71SNavdeep Parhar #endif
405043bbae19SNavdeep Parhar free_iq_fl(vi->adapter, &rxq->iq, &rxq->fl);
405143bbae19SNavdeep Parhar MPASS(!(rxq->iq.flags & IQ_SW_ALLOCATED));
405254e4ee71SNavdeep Parhar bzero(rxq, sizeof(*rxq));
405343bbae19SNavdeep Parhar }
405443bbae19SNavdeep Parhar }
405554e4ee71SNavdeep Parhar
405643bbae19SNavdeep Parhar static void
add_rxq_sysctls(struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_rxq * rxq)405743bbae19SNavdeep Parhar add_rxq_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid,
405843bbae19SNavdeep Parhar struct sge_rxq *rxq)
405943bbae19SNavdeep Parhar {
406043bbae19SNavdeep Parhar struct sysctl_oid_list *children;
406143bbae19SNavdeep Parhar
406243bbae19SNavdeep Parhar if (ctx == NULL || oid == NULL)
406343bbae19SNavdeep Parhar return;
406443bbae19SNavdeep Parhar
406543bbae19SNavdeep Parhar children = SYSCTL_CHILDREN(oid);
406643bbae19SNavdeep Parhar #if defined(INET) || defined(INET6)
406743bbae19SNavdeep Parhar SYSCTL_ADD_U64(ctx, children, OID_AUTO, "lro_queued", CTLFLAG_RD,
406843bbae19SNavdeep Parhar &rxq->lro.lro_queued, 0, NULL);
406943bbae19SNavdeep Parhar SYSCTL_ADD_U64(ctx, children, OID_AUTO, "lro_flushed", CTLFLAG_RD,
407043bbae19SNavdeep Parhar &rxq->lro.lro_flushed, 0, NULL);
407143bbae19SNavdeep Parhar #endif
407243bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "rxcsum", CTLFLAG_RD,
407343bbae19SNavdeep Parhar &rxq->rxcsum, "# of times hardware assisted with checksum");
407443bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "vlan_extraction", CTLFLAG_RD,
407543bbae19SNavdeep Parhar &rxq->vlan_extraction, "# of times hardware extracted 802.1Q tag");
407643bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "vxlan_rxcsum", CTLFLAG_RD,
407743bbae19SNavdeep Parhar &rxq->vxlan_rxcsum,
407843bbae19SNavdeep Parhar "# of times hardware assisted with inner checksum (VXLAN)");
407954e4ee71SNavdeep Parhar }
408054e4ee71SNavdeep Parhar
408109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
408243bbae19SNavdeep Parhar /*
408343bbae19SNavdeep Parhar * Idempotent.
408443bbae19SNavdeep Parhar */
408554e4ee71SNavdeep Parhar static int
alloc_ofld_rxq(struct vi_info * vi,struct sge_ofld_rxq * ofld_rxq,int idx,int intr_idx,int maxp)408643bbae19SNavdeep Parhar alloc_ofld_rxq(struct vi_info *vi, struct sge_ofld_rxq *ofld_rxq, int idx,
408743bbae19SNavdeep Parhar int intr_idx, int maxp)
4088f7dfe243SNavdeep Parhar {
4089733b9277SNavdeep Parhar int rc;
409043bbae19SNavdeep Parhar struct adapter *sc = vi->adapter;
409143bbae19SNavdeep Parhar struct sysctl_oid *oid;
4092733b9277SNavdeep Parhar char name[16];
4093f7dfe243SNavdeep Parhar
409443bbae19SNavdeep Parhar if (!(ofld_rxq->iq.flags & IQ_SW_ALLOCATED)) {
409543bbae19SNavdeep Parhar MPASS(!(ofld_rxq->iq.flags & IQ_HW_ALLOCATED));
4096733b9277SNavdeep Parhar
4097733b9277SNavdeep Parhar snprintf(name, sizeof(name), "%d", idx);
409843bbae19SNavdeep Parhar oid = SYSCTL_ADD_NODE(&vi->ctx,
409943bbae19SNavdeep Parhar SYSCTL_CHILDREN(vi->ofld_rxq_oid), OID_AUTO, name,
410043bbae19SNavdeep Parhar CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "offload rx queue");
4101733b9277SNavdeep Parhar
410243bbae19SNavdeep Parhar init_iq(&ofld_rxq->iq, sc, vi->ofld_tmr_idx, vi->ofld_pktc_idx,
4103998eb37aSNavdeep Parhar vi->qsize_rxq, intr_idx, ofld_cong_drop, IQ_OFLD);
410443bbae19SNavdeep Parhar snprintf(name, sizeof(name), "%s ofld_rxq%d-fl",
410543bbae19SNavdeep Parhar device_get_nameunit(vi->dev), idx);
410643bbae19SNavdeep Parhar init_fl(sc, &ofld_rxq->fl, vi->qsize_rxq / 8, maxp, name);
410743bbae19SNavdeep Parhar rc = alloc_iq_fl(vi, &ofld_rxq->iq, &ofld_rxq->fl, &vi->ctx,
410843bbae19SNavdeep Parhar oid);
410943bbae19SNavdeep Parhar if (rc != 0) {
411043bbae19SNavdeep Parhar CH_ERR(vi, "failed to allocate ofld_rxq%d: %d\n", idx,
411143bbae19SNavdeep Parhar rc);
411243bbae19SNavdeep Parhar sysctl_remove_oid(oid, 1, 1);
411343bbae19SNavdeep Parhar return (rc);
411443bbae19SNavdeep Parhar }
411543bbae19SNavdeep Parhar MPASS(ofld_rxq->iq.flags & IQ_SW_ALLOCATED);
4116a9f0cf48SJohn Baldwin ofld_rxq->rx_iscsi_ddp_setup_ok = counter_u64_alloc(M_WAITOK);
4117a9f0cf48SJohn Baldwin ofld_rxq->rx_iscsi_ddp_setup_error =
4118a9f0cf48SJohn Baldwin counter_u64_alloc(M_WAITOK);
4119eba13bbcSJohn Baldwin ofld_rxq->ddp_buffer_alloc = counter_u64_alloc(M_WAITOK);
4120eba13bbcSJohn Baldwin ofld_rxq->ddp_buffer_reuse = counter_u64_alloc(M_WAITOK);
4121eba13bbcSJohn Baldwin ofld_rxq->ddp_buffer_free = counter_u64_alloc(M_WAITOK);
412243bbae19SNavdeep Parhar add_ofld_rxq_sysctls(&vi->ctx, oid, ofld_rxq);
412343bbae19SNavdeep Parhar }
4124fe496dc0SJohn Baldwin
412543bbae19SNavdeep Parhar if (!(ofld_rxq->iq.flags & IQ_HW_ALLOCATED)) {
412643bbae19SNavdeep Parhar MPASS(ofld_rxq->iq.flags & IQ_SW_ALLOCATED);
412743bbae19SNavdeep Parhar rc = alloc_iq_fl_hwq(vi, &ofld_rxq->iq, &ofld_rxq->fl);
412843bbae19SNavdeep Parhar if (rc != 0) {
412943bbae19SNavdeep Parhar CH_ERR(vi, "failed to create hw ofld_rxq%d: %d\n", idx,
413043bbae19SNavdeep Parhar rc);
413143bbae19SNavdeep Parhar return (rc);
413243bbae19SNavdeep Parhar }
413343bbae19SNavdeep Parhar MPASS(ofld_rxq->iq.flags & IQ_HW_ALLOCATED);
413443bbae19SNavdeep Parhar }
4135733b9277SNavdeep Parhar return (rc);
4136733b9277SNavdeep Parhar }
4137733b9277SNavdeep Parhar
413843bbae19SNavdeep Parhar /*
413943bbae19SNavdeep Parhar * Idempotent.
414043bbae19SNavdeep Parhar */
414143bbae19SNavdeep Parhar static void
free_ofld_rxq(struct vi_info * vi,struct sge_ofld_rxq * ofld_rxq)4142fe2ebb76SJohn Baldwin free_ofld_rxq(struct vi_info *vi, struct sge_ofld_rxq *ofld_rxq)
4143733b9277SNavdeep Parhar {
414443bbae19SNavdeep Parhar if (ofld_rxq->iq.flags & IQ_HW_ALLOCATED) {
414543bbae19SNavdeep Parhar MPASS(ofld_rxq->iq.flags & IQ_SW_ALLOCATED);
414643bbae19SNavdeep Parhar free_iq_fl_hwq(vi->adapter, &ofld_rxq->iq, &ofld_rxq->fl);
414743bbae19SNavdeep Parhar MPASS(!(ofld_rxq->iq.flags & IQ_HW_ALLOCATED));
414843bbae19SNavdeep Parhar }
4149733b9277SNavdeep Parhar
415043bbae19SNavdeep Parhar if (ofld_rxq->iq.flags & IQ_SW_ALLOCATED) {
415143bbae19SNavdeep Parhar MPASS(!(ofld_rxq->iq.flags & IQ_HW_ALLOCATED));
415243bbae19SNavdeep Parhar free_iq_fl(vi->adapter, &ofld_rxq->iq, &ofld_rxq->fl);
415343bbae19SNavdeep Parhar MPASS(!(ofld_rxq->iq.flags & IQ_SW_ALLOCATED));
4154a9f0cf48SJohn Baldwin counter_u64_free(ofld_rxq->rx_iscsi_ddp_setup_ok);
4155a9f0cf48SJohn Baldwin counter_u64_free(ofld_rxq->rx_iscsi_ddp_setup_error);
4156eba13bbcSJohn Baldwin counter_u64_free(ofld_rxq->ddp_buffer_alloc);
4157eba13bbcSJohn Baldwin counter_u64_free(ofld_rxq->ddp_buffer_reuse);
4158eba13bbcSJohn Baldwin counter_u64_free(ofld_rxq->ddp_buffer_free);
4159733b9277SNavdeep Parhar bzero(ofld_rxq, sizeof(*ofld_rxq));
416043bbae19SNavdeep Parhar }
416143bbae19SNavdeep Parhar }
4162733b9277SNavdeep Parhar
416343bbae19SNavdeep Parhar static void
add_ofld_rxq_sysctls(struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_ofld_rxq * ofld_rxq)416443bbae19SNavdeep Parhar add_ofld_rxq_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid,
416543bbae19SNavdeep Parhar struct sge_ofld_rxq *ofld_rxq)
416643bbae19SNavdeep Parhar {
416743bbae19SNavdeep Parhar struct sysctl_oid_list *children;
416843bbae19SNavdeep Parhar
416943bbae19SNavdeep Parhar if (ctx == NULL || oid == NULL)
417043bbae19SNavdeep Parhar return;
417143bbae19SNavdeep Parhar
417243bbae19SNavdeep Parhar children = SYSCTL_CHILDREN(oid);
4173c3d4aea6SJohn Baldwin SYSCTL_ADD_U64(ctx, children, OID_AUTO, "rx_aio_ddp_jobs",
4174c3d4aea6SJohn Baldwin CTLFLAG_RD, &ofld_rxq->rx_aio_ddp_jobs, 0,
4175c3d4aea6SJohn Baldwin "# of aio_read(2) jobs completed via DDP");
4176c3d4aea6SJohn Baldwin SYSCTL_ADD_U64(ctx, children, OID_AUTO, "rx_aio_ddp_octets",
4177c3d4aea6SJohn Baldwin CTLFLAG_RD, &ofld_rxq->rx_aio_ddp_octets, 0,
4178c3d4aea6SJohn Baldwin "# of octets placed directly for aio_read(2) jobs");
41794b6ed075SJohn Baldwin SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
418043bbae19SNavdeep Parhar "rx_toe_tls_records", CTLFLAG_RD, &ofld_rxq->rx_toe_tls_records,
418143bbae19SNavdeep Parhar "# of TOE TLS records received");
41824b6ed075SJohn Baldwin SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
418343bbae19SNavdeep Parhar "rx_toe_tls_octets", CTLFLAG_RD, &ofld_rxq->rx_toe_tls_octets,
418443bbae19SNavdeep Parhar "# of payload octets in received TOE TLS records");
4185eba13bbcSJohn Baldwin SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
4186eba13bbcSJohn Baldwin "rx_toe_ddp_octets", CTLFLAG_RD, &ofld_rxq->rx_toe_ddp_octets,
4187eba13bbcSJohn Baldwin "# of payload octets received via TCP DDP");
4188eba13bbcSJohn Baldwin SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO,
4189eba13bbcSJohn Baldwin "ddp_buffer_alloc", CTLFLAG_RD, &ofld_rxq->ddp_buffer_alloc,
4190eba13bbcSJohn Baldwin "# of DDP RCV buffers allocated");
4191eba13bbcSJohn Baldwin SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO,
4192eba13bbcSJohn Baldwin "ddp_buffer_reuse", CTLFLAG_RD, &ofld_rxq->ddp_buffer_reuse,
4193eba13bbcSJohn Baldwin "# of DDP RCV buffers reused");
4194eba13bbcSJohn Baldwin SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO,
4195eba13bbcSJohn Baldwin "ddp_buffer_free", CTLFLAG_RD, &ofld_rxq->ddp_buffer_free,
4196eba13bbcSJohn Baldwin "# of DDP RCV buffers freed");
41974b6ed075SJohn Baldwin
41984b6ed075SJohn Baldwin oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "iscsi",
41994b6ed075SJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE iSCSI statistics");
42004b6ed075SJohn Baldwin children = SYSCTL_CHILDREN(oid);
42014b6ed075SJohn Baldwin
42024b6ed075SJohn Baldwin SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "ddp_setup_ok",
42034b6ed075SJohn Baldwin CTLFLAG_RD, &ofld_rxq->rx_iscsi_ddp_setup_ok,
42044b6ed075SJohn Baldwin "# of times DDP buffer was setup successfully.");
42054b6ed075SJohn Baldwin SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "ddp_setup_error",
42064b6ed075SJohn Baldwin CTLFLAG_RD, &ofld_rxq->rx_iscsi_ddp_setup_error,
42074b6ed075SJohn Baldwin "# of times DDP buffer setup failed.");
42084b6ed075SJohn Baldwin SYSCTL_ADD_U64(ctx, children, OID_AUTO, "ddp_octets",
42094b6ed075SJohn Baldwin CTLFLAG_RD, &ofld_rxq->rx_iscsi_ddp_octets, 0,
42104b6ed075SJohn Baldwin "# of octets placed directly");
42114b6ed075SJohn Baldwin SYSCTL_ADD_U64(ctx, children, OID_AUTO, "ddp_pdus",
42124b6ed075SJohn Baldwin CTLFLAG_RD, &ofld_rxq->rx_iscsi_ddp_pdus, 0,
42134b6ed075SJohn Baldwin "# of PDUs with data placed directly.");
42144b6ed075SJohn Baldwin SYSCTL_ADD_U64(ctx, children, OID_AUTO, "fl_octets",
42154b6ed075SJohn Baldwin CTLFLAG_RD, &ofld_rxq->rx_iscsi_fl_octets, 0,
42164b6ed075SJohn Baldwin "# of data octets delivered in freelist");
42174b6ed075SJohn Baldwin SYSCTL_ADD_U64(ctx, children, OID_AUTO, "fl_pdus",
42184b6ed075SJohn Baldwin CTLFLAG_RD, &ofld_rxq->rx_iscsi_fl_pdus, 0,
42194b6ed075SJohn Baldwin "# of PDUs with data delivered in freelist");
42204d4cf62eSJohn Baldwin SYSCTL_ADD_U64(ctx, children, OID_AUTO, "padding_errors",
42214d4cf62eSJohn Baldwin CTLFLAG_RD, &ofld_rxq->rx_iscsi_padding_errors, 0,
42224d4cf62eSJohn Baldwin "# of PDUs with invalid padding");
42234d4cf62eSJohn Baldwin SYSCTL_ADD_U64(ctx, children, OID_AUTO, "header_digest_errors",
42244d4cf62eSJohn Baldwin CTLFLAG_RD, &ofld_rxq->rx_iscsi_header_digest_errors, 0,
42254d4cf62eSJohn Baldwin "# of PDUs with invalid header digests");
42264d4cf62eSJohn Baldwin SYSCTL_ADD_U64(ctx, children, OID_AUTO, "data_digest_errors",
42274d4cf62eSJohn Baldwin CTLFLAG_RD, &ofld_rxq->rx_iscsi_data_digest_errors, 0,
42284d4cf62eSJohn Baldwin "# of PDUs with invalid data digests");
4229733b9277SNavdeep Parhar }
4230733b9277SNavdeep Parhar #endif
4231733b9277SNavdeep Parhar
4232ddf09ad6SNavdeep Parhar /*
4233ddf09ad6SNavdeep Parhar * Returns a reasonable automatic cidx flush threshold for a given queue size.
4234ddf09ad6SNavdeep Parhar */
4235ddf09ad6SNavdeep Parhar static u_int
qsize_to_fthresh(int qsize)4236ddf09ad6SNavdeep Parhar qsize_to_fthresh(int qsize)
4237ddf09ad6SNavdeep Parhar {
4238ddf09ad6SNavdeep Parhar u_int fthresh;
4239ddf09ad6SNavdeep Parhar
42405fc42387SDoug Moore fthresh = qsize == 0 ? 0 : order_base_2(qsize);
4241ddf09ad6SNavdeep Parhar if (fthresh > X_CIDXFLUSHTHRESH_128)
4242ddf09ad6SNavdeep Parhar fthresh = X_CIDXFLUSHTHRESH_128;
4243ddf09ad6SNavdeep Parhar
4244ddf09ad6SNavdeep Parhar return (fthresh);
4245ddf09ad6SNavdeep Parhar }
4246ddf09ad6SNavdeep Parhar
4247733b9277SNavdeep Parhar static int
ctrl_eq_alloc(struct adapter * sc,struct sge_eq * eq)4248733b9277SNavdeep Parhar ctrl_eq_alloc(struct adapter *sc, struct sge_eq *eq)
4249733b9277SNavdeep Parhar {
4250733b9277SNavdeep Parhar int rc, cntxt_id;
4251733b9277SNavdeep Parhar struct fw_eq_ctrl_cmd c;
425290e7434aSNavdeep Parhar int qsize = eq->sidx + sc->params.sge.spg_len / EQ_ESIZE;
4253f7dfe243SNavdeep Parhar
4254f7dfe243SNavdeep Parhar bzero(&c, sizeof(c));
4255f7dfe243SNavdeep Parhar
4256f7dfe243SNavdeep Parhar c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_CTRL_CMD) | F_FW_CMD_REQUEST |
4257f7dfe243SNavdeep Parhar F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_CTRL_CMD_PFN(sc->pf) |
4258f7dfe243SNavdeep Parhar V_FW_EQ_CTRL_CMD_VFN(0));
4259f7dfe243SNavdeep Parhar c.alloc_to_len16 = htobe32(F_FW_EQ_CTRL_CMD_ALLOC |
4260f7dfe243SNavdeep Parhar F_FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
42617951040fSNavdeep Parhar c.cmpliqid_eqid = htonl(V_FW_EQ_CTRL_CMD_CMPLIQID(eq->iqid));
4262f7dfe243SNavdeep Parhar c.physeqid_pkd = htobe32(0);
4263f7dfe243SNavdeep Parhar c.fetchszm_to_iqid =
426487b027baSNavdeep Parhar htobe32(V_FW_EQ_CTRL_CMD_HOSTFCMODE(X_HOSTFCMODE_STATUS_PAGE) |
4265733b9277SNavdeep Parhar V_FW_EQ_CTRL_CMD_PCIECHN(eq->tx_chan) |
426656599263SNavdeep Parhar F_FW_EQ_CTRL_CMD_FETCHRO | V_FW_EQ_CTRL_CMD_IQID(eq->iqid));
4267f7dfe243SNavdeep Parhar c.dcaen_to_eqsize =
4268adb0cd84SNavdeep Parhar htobe32(V_FW_EQ_CTRL_CMD_FBMIN(chip_id(sc) <= CHELSIO_T5 ?
4269adb0cd84SNavdeep Parhar X_FETCHBURSTMIN_64B : X_FETCHBURSTMIN_64B_T6) |
4270f7dfe243SNavdeep Parhar V_FW_EQ_CTRL_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
4271ddf09ad6SNavdeep Parhar V_FW_EQ_CTRL_CMD_CIDXFTHRESH(qsize_to_fthresh(qsize)) |
42727951040fSNavdeep Parhar V_FW_EQ_CTRL_CMD_EQSIZE(qsize));
4273f7dfe243SNavdeep Parhar c.eqaddr = htobe64(eq->ba);
4274f7dfe243SNavdeep Parhar
4275f7dfe243SNavdeep Parhar rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
4276f7dfe243SNavdeep Parhar if (rc != 0) {
427743bbae19SNavdeep Parhar CH_ERR(sc, "failed to create hw ctrlq for tx_chan %d: %d\n",
427843bbae19SNavdeep Parhar eq->tx_chan, rc);
4279f7dfe243SNavdeep Parhar return (rc);
4280f7dfe243SNavdeep Parhar }
4281f7dfe243SNavdeep Parhar
4282f7dfe243SNavdeep Parhar eq->cntxt_id = G_FW_EQ_CTRL_CMD_EQID(be32toh(c.cmpliqid_eqid));
428376c89022SNavdeep Parhar eq->abs_id = G_FW_EQ_CTRL_CMD_PHYSEQID(be32toh(c.physeqid_pkd));
4284f7dfe243SNavdeep Parhar cntxt_id = eq->cntxt_id - sc->sge.eq_start;
4285b20b25e7SNavdeep Parhar if (cntxt_id >= sc->sge.eqmap_sz)
4286733b9277SNavdeep Parhar panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
4287b20b25e7SNavdeep Parhar cntxt_id, sc->sge.eqmap_sz - 1);
4288f7dfe243SNavdeep Parhar sc->sge.eqmap[cntxt_id] = eq;
4289f7dfe243SNavdeep Parhar
4290f7dfe243SNavdeep Parhar return (rc);
4291f7dfe243SNavdeep Parhar }
4292f7dfe243SNavdeep Parhar
4293f7dfe243SNavdeep Parhar static int
eth_eq_alloc(struct adapter * sc,struct vi_info * vi,struct sge_eq * eq)4294fe2ebb76SJohn Baldwin eth_eq_alloc(struct adapter *sc, struct vi_info *vi, struct sge_eq *eq)
429554e4ee71SNavdeep Parhar {
429654e4ee71SNavdeep Parhar int rc, cntxt_id;
429754e4ee71SNavdeep Parhar struct fw_eq_eth_cmd c;
429890e7434aSNavdeep Parhar int qsize = eq->sidx + sc->params.sge.spg_len / EQ_ESIZE;
429954e4ee71SNavdeep Parhar
430054e4ee71SNavdeep Parhar bzero(&c, sizeof(c));
430154e4ee71SNavdeep Parhar
430254e4ee71SNavdeep Parhar c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_ETH_CMD) | F_FW_CMD_REQUEST |
430354e4ee71SNavdeep Parhar F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_ETH_CMD_PFN(sc->pf) |
430454e4ee71SNavdeep Parhar V_FW_EQ_ETH_CMD_VFN(0));
430554e4ee71SNavdeep Parhar c.alloc_to_len16 = htobe32(F_FW_EQ_ETH_CMD_ALLOC |
430654e4ee71SNavdeep Parhar F_FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
43077951040fSNavdeep Parhar c.autoequiqe_to_viid = htobe32(F_FW_EQ_ETH_CMD_AUTOEQUIQE |
4308fe2ebb76SJohn Baldwin F_FW_EQ_ETH_CMD_AUTOEQUEQE | V_FW_EQ_ETH_CMD_VIID(vi->viid));
430954e4ee71SNavdeep Parhar c.fetchszm_to_iqid =
43107951040fSNavdeep Parhar htobe32(V_FW_EQ_ETH_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) |
4311733b9277SNavdeep Parhar V_FW_EQ_ETH_CMD_PCIECHN(eq->tx_chan) | F_FW_EQ_ETH_CMD_FETCHRO |
4312aa2457e1SNavdeep Parhar V_FW_EQ_ETH_CMD_IQID(eq->iqid));
4313adb0cd84SNavdeep Parhar c.dcaen_to_eqsize =
4314adb0cd84SNavdeep Parhar htobe32(V_FW_EQ_ETH_CMD_FBMIN(chip_id(sc) <= CHELSIO_T5 ?
4315adb0cd84SNavdeep Parhar X_FETCHBURSTMIN_64B : X_FETCHBURSTMIN_64B_T6) |
431654e4ee71SNavdeep Parhar V_FW_EQ_ETH_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
43177951040fSNavdeep Parhar V_FW_EQ_ETH_CMD_EQSIZE(qsize));
431854e4ee71SNavdeep Parhar c.eqaddr = htobe64(eq->ba);
431954e4ee71SNavdeep Parhar
432054e4ee71SNavdeep Parhar rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
432154e4ee71SNavdeep Parhar if (rc != 0) {
4322fe2ebb76SJohn Baldwin device_printf(vi->dev,
4323733b9277SNavdeep Parhar "failed to create Ethernet egress queue: %d\n", rc);
4324733b9277SNavdeep Parhar return (rc);
4325733b9277SNavdeep Parhar }
4326733b9277SNavdeep Parhar
4327733b9277SNavdeep Parhar eq->cntxt_id = G_FW_EQ_ETH_CMD_EQID(be32toh(c.eqid_pkd));
4328ec55567cSJohn Baldwin eq->abs_id = G_FW_EQ_ETH_CMD_PHYSEQID(be32toh(c.physeqid_pkd));
4329733b9277SNavdeep Parhar cntxt_id = eq->cntxt_id - sc->sge.eq_start;
4330b20b25e7SNavdeep Parhar if (cntxt_id >= sc->sge.eqmap_sz)
4331733b9277SNavdeep Parhar panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
4332b20b25e7SNavdeep Parhar cntxt_id, sc->sge.eqmap_sz - 1);
4333733b9277SNavdeep Parhar sc->sge.eqmap[cntxt_id] = eq;
4334733b9277SNavdeep Parhar
433554e4ee71SNavdeep Parhar return (rc);
433654e4ee71SNavdeep Parhar }
433754e4ee71SNavdeep Parhar
4338eff62dbaSNavdeep Parhar #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4339733b9277SNavdeep Parhar static int
ofld_eq_alloc(struct adapter * sc,struct vi_info * vi,struct sge_eq * eq)4340fe2ebb76SJohn Baldwin ofld_eq_alloc(struct adapter *sc, struct vi_info *vi, struct sge_eq *eq)
4341733b9277SNavdeep Parhar {
4342733b9277SNavdeep Parhar int rc, cntxt_id;
4343733b9277SNavdeep Parhar struct fw_eq_ofld_cmd c;
434490e7434aSNavdeep Parhar int qsize = eq->sidx + sc->params.sge.spg_len / EQ_ESIZE;
434554e4ee71SNavdeep Parhar
4346733b9277SNavdeep Parhar bzero(&c, sizeof(c));
4347733b9277SNavdeep Parhar
4348733b9277SNavdeep Parhar c.op_to_vfn = htonl(V_FW_CMD_OP(FW_EQ_OFLD_CMD) | F_FW_CMD_REQUEST |
4349733b9277SNavdeep Parhar F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_OFLD_CMD_PFN(sc->pf) |
4350733b9277SNavdeep Parhar V_FW_EQ_OFLD_CMD_VFN(0));
4351733b9277SNavdeep Parhar c.alloc_to_len16 = htonl(F_FW_EQ_OFLD_CMD_ALLOC |
4352733b9277SNavdeep Parhar F_FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
4353733b9277SNavdeep Parhar c.fetchszm_to_iqid =
4354ddf09ad6SNavdeep Parhar htonl(V_FW_EQ_OFLD_CMD_HOSTFCMODE(X_HOSTFCMODE_STATUS_PAGE) |
4355733b9277SNavdeep Parhar V_FW_EQ_OFLD_CMD_PCIECHN(eq->tx_chan) |
4356733b9277SNavdeep Parhar F_FW_EQ_OFLD_CMD_FETCHRO | V_FW_EQ_OFLD_CMD_IQID(eq->iqid));
4357733b9277SNavdeep Parhar c.dcaen_to_eqsize =
4358adb0cd84SNavdeep Parhar htobe32(V_FW_EQ_OFLD_CMD_FBMIN(chip_id(sc) <= CHELSIO_T5 ?
4359adb0cd84SNavdeep Parhar X_FETCHBURSTMIN_64B : X_FETCHBURSTMIN_64B_T6) |
4360733b9277SNavdeep Parhar V_FW_EQ_OFLD_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
4361ddf09ad6SNavdeep Parhar V_FW_EQ_OFLD_CMD_CIDXFTHRESH(qsize_to_fthresh(qsize)) |
43627951040fSNavdeep Parhar V_FW_EQ_OFLD_CMD_EQSIZE(qsize));
4363733b9277SNavdeep Parhar c.eqaddr = htobe64(eq->ba);
4364733b9277SNavdeep Parhar
4365733b9277SNavdeep Parhar rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
4366733b9277SNavdeep Parhar if (rc != 0) {
4367fe2ebb76SJohn Baldwin device_printf(vi->dev,
4368733b9277SNavdeep Parhar "failed to create egress queue for TCP offload: %d\n", rc);
4369733b9277SNavdeep Parhar return (rc);
4370733b9277SNavdeep Parhar }
4371733b9277SNavdeep Parhar
4372733b9277SNavdeep Parhar eq->cntxt_id = G_FW_EQ_OFLD_CMD_EQID(be32toh(c.eqid_pkd));
437376c89022SNavdeep Parhar eq->abs_id = G_FW_EQ_OFLD_CMD_PHYSEQID(be32toh(c.physeqid_pkd));
437454e4ee71SNavdeep Parhar cntxt_id = eq->cntxt_id - sc->sge.eq_start;
4375b20b25e7SNavdeep Parhar if (cntxt_id >= sc->sge.eqmap_sz)
4376733b9277SNavdeep Parhar panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
4377b20b25e7SNavdeep Parhar cntxt_id, sc->sge.eqmap_sz - 1);
437854e4ee71SNavdeep Parhar sc->sge.eqmap[cntxt_id] = eq;
437954e4ee71SNavdeep Parhar
4380733b9277SNavdeep Parhar return (rc);
4381733b9277SNavdeep Parhar }
4382733b9277SNavdeep Parhar #endif
4383733b9277SNavdeep Parhar
438443bbae19SNavdeep Parhar /* SW only */
4385733b9277SNavdeep Parhar static int
alloc_eq(struct adapter * sc,struct sge_eq * eq,struct sysctl_ctx_list * ctx,struct sysctl_oid * oid)438643bbae19SNavdeep Parhar alloc_eq(struct adapter *sc, struct sge_eq *eq, struct sysctl_ctx_list *ctx,
438743bbae19SNavdeep Parhar struct sysctl_oid *oid)
4388733b9277SNavdeep Parhar {
43897951040fSNavdeep Parhar int rc, qsize;
4390733b9277SNavdeep Parhar size_t len;
4391733b9277SNavdeep Parhar
439243bbae19SNavdeep Parhar MPASS(!(eq->flags & EQ_SW_ALLOCATED));
4393733b9277SNavdeep Parhar
439490e7434aSNavdeep Parhar qsize = eq->sidx + sc->params.sge.spg_len / EQ_ESIZE;
43957951040fSNavdeep Parhar len = qsize * EQ_ESIZE;
439643bbae19SNavdeep Parhar rc = alloc_ring(sc, len, &eq->desc_tag, &eq->desc_map, &eq->ba,
439743bbae19SNavdeep Parhar (void **)&eq->desc);
4398733b9277SNavdeep Parhar if (rc)
4399733b9277SNavdeep Parhar return (rc);
440043bbae19SNavdeep Parhar if (ctx != NULL && oid != NULL)
440143bbae19SNavdeep Parhar add_eq_sysctls(sc, ctx, oid, eq);
440243bbae19SNavdeep Parhar eq->flags |= EQ_SW_ALLOCATED;
4403733b9277SNavdeep Parhar
440443bbae19SNavdeep Parhar return (0);
440543bbae19SNavdeep Parhar }
440643bbae19SNavdeep Parhar
440743bbae19SNavdeep Parhar /* SW only */
440843bbae19SNavdeep Parhar static void
free_eq(struct adapter * sc,struct sge_eq * eq)440943bbae19SNavdeep Parhar free_eq(struct adapter *sc, struct sge_eq *eq)
441043bbae19SNavdeep Parhar {
441143bbae19SNavdeep Parhar MPASS(eq->flags & EQ_SW_ALLOCATED);
44125ef87bf8SNavdeep Parhar if (eq->type == EQ_ETH)
441343bbae19SNavdeep Parhar MPASS(eq->pidx == eq->cidx);
441443bbae19SNavdeep Parhar
441543bbae19SNavdeep Parhar free_ring(sc, eq->desc_tag, eq->desc_map, eq->ba, eq->desc);
441643bbae19SNavdeep Parhar mtx_destroy(&eq->eq_lock);
441743bbae19SNavdeep Parhar bzero(eq, sizeof(*eq));
441843bbae19SNavdeep Parhar }
441943bbae19SNavdeep Parhar
442043bbae19SNavdeep Parhar static void
add_eq_sysctls(struct adapter * sc,struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_eq * eq)442143bbae19SNavdeep Parhar add_eq_sysctls(struct adapter *sc, struct sysctl_ctx_list *ctx,
442243bbae19SNavdeep Parhar struct sysctl_oid *oid, struct sge_eq *eq)
442343bbae19SNavdeep Parhar {
442443bbae19SNavdeep Parhar struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
442543bbae19SNavdeep Parhar
442643bbae19SNavdeep Parhar SYSCTL_ADD_UAUTO(ctx, children, OID_AUTO, "ba", CTLFLAG_RD, &eq->ba,
442743bbae19SNavdeep Parhar "bus address of descriptor ring");
442843bbae19SNavdeep Parhar SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dmalen", CTLFLAG_RD, NULL,
442943bbae19SNavdeep Parhar eq->sidx * EQ_ESIZE + sc->params.sge.spg_len,
443043bbae19SNavdeep Parhar "desc ring size in bytes");
443143bbae19SNavdeep Parhar SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "abs_id", CTLFLAG_RD,
443243bbae19SNavdeep Parhar &eq->abs_id, 0, "absolute id of the queue");
443343bbae19SNavdeep Parhar SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
443443bbae19SNavdeep Parhar &eq->cntxt_id, 0, "SGE context id of the queue");
443543bbae19SNavdeep Parhar SYSCTL_ADD_U16(ctx, children, OID_AUTO, "cidx", CTLFLAG_RD, &eq->cidx,
443643bbae19SNavdeep Parhar 0, "consumer index");
443743bbae19SNavdeep Parhar SYSCTL_ADD_U16(ctx, children, OID_AUTO, "pidx", CTLFLAG_RD, &eq->pidx,
443843bbae19SNavdeep Parhar 0, "producer index");
443943bbae19SNavdeep Parhar SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sidx", CTLFLAG_RD, NULL,
444043bbae19SNavdeep Parhar eq->sidx, "status page index");
444143bbae19SNavdeep Parhar }
444243bbae19SNavdeep Parhar
444343bbae19SNavdeep Parhar static int
alloc_eq_hwq(struct adapter * sc,struct vi_info * vi,struct sge_eq * eq)444443bbae19SNavdeep Parhar alloc_eq_hwq(struct adapter *sc, struct vi_info *vi, struct sge_eq *eq)
444543bbae19SNavdeep Parhar {
444643bbae19SNavdeep Parhar int rc;
444743bbae19SNavdeep Parhar
444843bbae19SNavdeep Parhar MPASS(!(eq->flags & EQ_HW_ALLOCATED));
444943bbae19SNavdeep Parhar
445043bbae19SNavdeep Parhar eq->iqid = eq->iq->cntxt_id;
4451ddf09ad6SNavdeep Parhar eq->pidx = eq->cidx = eq->dbidx = 0;
4452ddf09ad6SNavdeep Parhar /* Note that equeqidx is not used with sge_wrq (OFLD/CTRL) queues. */
4453ddf09ad6SNavdeep Parhar eq->equeqidx = 0;
4454d14b0ac1SNavdeep Parhar eq->doorbells = sc->doorbells;
445543bbae19SNavdeep Parhar bzero(eq->desc, eq->sidx * EQ_ESIZE + sc->params.sge.spg_len);
4456733b9277SNavdeep Parhar
445743bbae19SNavdeep Parhar switch (eq->type) {
4458733b9277SNavdeep Parhar case EQ_CTRL:
4459733b9277SNavdeep Parhar rc = ctrl_eq_alloc(sc, eq);
4460733b9277SNavdeep Parhar break;
4461733b9277SNavdeep Parhar
4462733b9277SNavdeep Parhar case EQ_ETH:
4463fe2ebb76SJohn Baldwin rc = eth_eq_alloc(sc, vi, eq);
4464733b9277SNavdeep Parhar break;
4465733b9277SNavdeep Parhar
4466eff62dbaSNavdeep Parhar #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4467733b9277SNavdeep Parhar case EQ_OFLD:
4468fe2ebb76SJohn Baldwin rc = ofld_eq_alloc(sc, vi, eq);
4469733b9277SNavdeep Parhar break;
4470733b9277SNavdeep Parhar #endif
4471733b9277SNavdeep Parhar
4472733b9277SNavdeep Parhar default:
447343bbae19SNavdeep Parhar panic("%s: invalid eq type %d.", __func__, eq->type);
4474733b9277SNavdeep Parhar }
4475733b9277SNavdeep Parhar if (rc != 0) {
447643bbae19SNavdeep Parhar CH_ERR(sc, "failed to allocate egress queue(%d): %d\n",
447743bbae19SNavdeep Parhar eq->type, rc);
447843bbae19SNavdeep Parhar return (rc);
4479733b9277SNavdeep Parhar }
4480733b9277SNavdeep Parhar
4481d14b0ac1SNavdeep Parhar if (isset(&eq->doorbells, DOORBELL_UDB) ||
4482d14b0ac1SNavdeep Parhar isset(&eq->doorbells, DOORBELL_UDBWC) ||
448377ad3c41SNavdeep Parhar isset(&eq->doorbells, DOORBELL_WCWR)) {
448490e7434aSNavdeep Parhar uint32_t s_qpp = sc->params.sge.eq_s_qpp;
4485d14b0ac1SNavdeep Parhar uint32_t mask = (1 << s_qpp) - 1;
4486d14b0ac1SNavdeep Parhar volatile uint8_t *udb;
4487d14b0ac1SNavdeep Parhar
4488d14b0ac1SNavdeep Parhar udb = sc->udbs_base + UDBS_DB_OFFSET;
4489d14b0ac1SNavdeep Parhar udb += (eq->cntxt_id >> s_qpp) << PAGE_SHIFT; /* pg offset */
4490d14b0ac1SNavdeep Parhar eq->udb_qid = eq->cntxt_id & mask; /* id in page */
4491f10405b3SNavdeep Parhar if (eq->udb_qid >= PAGE_SIZE / UDBS_SEG_SIZE)
449277ad3c41SNavdeep Parhar clrbit(&eq->doorbells, DOORBELL_WCWR);
4493d14b0ac1SNavdeep Parhar else {
4494d14b0ac1SNavdeep Parhar udb += eq->udb_qid << UDBS_SEG_SHIFT; /* seg offset */
4495d14b0ac1SNavdeep Parhar eq->udb_qid = 0;
4496d14b0ac1SNavdeep Parhar }
4497d14b0ac1SNavdeep Parhar eq->udb = (volatile void *)udb;
4498d14b0ac1SNavdeep Parhar }
4499d14b0ac1SNavdeep Parhar
450043bbae19SNavdeep Parhar eq->flags |= EQ_HW_ALLOCATED;
450143bbae19SNavdeep Parhar return (0);
4502733b9277SNavdeep Parhar }
4503733b9277SNavdeep Parhar
4504733b9277SNavdeep Parhar static int
free_eq_hwq(struct adapter * sc,struct vi_info * vi __unused,struct sge_eq * eq)450543bbae19SNavdeep Parhar free_eq_hwq(struct adapter *sc, struct vi_info *vi __unused, struct sge_eq *eq)
4506733b9277SNavdeep Parhar {
4507733b9277SNavdeep Parhar int rc;
4508733b9277SNavdeep Parhar
450943bbae19SNavdeep Parhar MPASS(eq->flags & EQ_HW_ALLOCATED);
451043bbae19SNavdeep Parhar
451143bbae19SNavdeep Parhar switch (eq->type) {
4512733b9277SNavdeep Parhar case EQ_CTRL:
451343bbae19SNavdeep Parhar rc = -t4_ctrl_eq_free(sc, sc->mbox, sc->pf, 0, eq->cntxt_id);
4514733b9277SNavdeep Parhar break;
4515733b9277SNavdeep Parhar case EQ_ETH:
451643bbae19SNavdeep Parhar rc = -t4_eth_eq_free(sc, sc->mbox, sc->pf, 0, eq->cntxt_id);
4517733b9277SNavdeep Parhar break;
4518eff62dbaSNavdeep Parhar #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4519733b9277SNavdeep Parhar case EQ_OFLD:
452043bbae19SNavdeep Parhar rc = -t4_ofld_eq_free(sc, sc->mbox, sc->pf, 0, eq->cntxt_id);
4521733b9277SNavdeep Parhar break;
4522733b9277SNavdeep Parhar #endif
4523733b9277SNavdeep Parhar default:
452443bbae19SNavdeep Parhar panic("%s: invalid eq type %d.", __func__, eq->type);
4525733b9277SNavdeep Parhar }
4526733b9277SNavdeep Parhar if (rc != 0) {
452743bbae19SNavdeep Parhar CH_ERR(sc, "failed to free eq (type %d): %d\n", eq->type, rc);
4528733b9277SNavdeep Parhar return (rc);
4529733b9277SNavdeep Parhar }
453043bbae19SNavdeep Parhar eq->flags &= ~EQ_HW_ALLOCATED;
4531733b9277SNavdeep Parhar
4532733b9277SNavdeep Parhar return (0);
4533733b9277SNavdeep Parhar }
4534733b9277SNavdeep Parhar
4535733b9277SNavdeep Parhar static int
alloc_wrq(struct adapter * sc,struct vi_info * vi,struct sge_wrq * wrq,struct sysctl_ctx_list * ctx,struct sysctl_oid * oid)4536fe2ebb76SJohn Baldwin alloc_wrq(struct adapter *sc, struct vi_info *vi, struct sge_wrq *wrq,
453743bbae19SNavdeep Parhar struct sysctl_ctx_list *ctx, struct sysctl_oid *oid)
4538733b9277SNavdeep Parhar {
453943bbae19SNavdeep Parhar struct sge_eq *eq = &wrq->eq;
4540733b9277SNavdeep Parhar int rc;
4541733b9277SNavdeep Parhar
454243bbae19SNavdeep Parhar MPASS(!(eq->flags & EQ_SW_ALLOCATED));
454343bbae19SNavdeep Parhar
454443bbae19SNavdeep Parhar rc = alloc_eq(sc, eq, ctx, oid);
4545733b9277SNavdeep Parhar if (rc)
4546733b9277SNavdeep Parhar return (rc);
454743bbae19SNavdeep Parhar MPASS(eq->flags & EQ_SW_ALLOCATED);
454843bbae19SNavdeep Parhar /* Can't fail after this. */
4549733b9277SNavdeep Parhar
4550733b9277SNavdeep Parhar wrq->adapter = sc;
45517951040fSNavdeep Parhar TASK_INIT(&wrq->wrq_tx_task, 0, wrq_tx_drain, wrq);
45527951040fSNavdeep Parhar TAILQ_INIT(&wrq->incomplete_wrs);
455309fe6320SNavdeep Parhar STAILQ_INIT(&wrq->wr_list);
45547951040fSNavdeep Parhar wrq->nwr_pending = 0;
45557951040fSNavdeep Parhar wrq->ndesc_needed = 0;
455643bbae19SNavdeep Parhar add_wrq_sysctls(ctx, oid, wrq);
4557733b9277SNavdeep Parhar
455843bbae19SNavdeep Parhar return (0);
455943bbae19SNavdeep Parhar }
456043bbae19SNavdeep Parhar
456143bbae19SNavdeep Parhar static void
free_wrq(struct adapter * sc,struct sge_wrq * wrq)456243bbae19SNavdeep Parhar free_wrq(struct adapter *sc, struct sge_wrq *wrq)
456343bbae19SNavdeep Parhar {
456443bbae19SNavdeep Parhar free_eq(sc, &wrq->eq);
456543bbae19SNavdeep Parhar MPASS(wrq->nwr_pending == 0);
4566*0a9d1da6SNavdeep Parhar MPASS(wrq->ndesc_needed == 0);
45675ef87bf8SNavdeep Parhar MPASS(TAILQ_EMPTY(&wrq->incomplete_wrs));
45685ef87bf8SNavdeep Parhar MPASS(STAILQ_EMPTY(&wrq->wr_list));
456943bbae19SNavdeep Parhar bzero(wrq, sizeof(*wrq));
457043bbae19SNavdeep Parhar }
457143bbae19SNavdeep Parhar
457243bbae19SNavdeep Parhar static void
add_wrq_sysctls(struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_wrq * wrq)457343bbae19SNavdeep Parhar add_wrq_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid,
457443bbae19SNavdeep Parhar struct sge_wrq *wrq)
457543bbae19SNavdeep Parhar {
457643bbae19SNavdeep Parhar struct sysctl_oid_list *children;
457743bbae19SNavdeep Parhar
457843bbae19SNavdeep Parhar if (ctx == NULL || oid == NULL)
457943bbae19SNavdeep Parhar return;
458043bbae19SNavdeep Parhar
458143bbae19SNavdeep Parhar children = SYSCTL_CHILDREN(oid);
45827951040fSNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs_direct", CTLFLAG_RD,
45837951040fSNavdeep Parhar &wrq->tx_wrs_direct, "# of work requests (direct)");
45847951040fSNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs_copied", CTLFLAG_RD,
45857951040fSNavdeep Parhar &wrq->tx_wrs_copied, "# of work requests (copied)");
45860459a175SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs_sspace", CTLFLAG_RD,
45870459a175SNavdeep Parhar &wrq->tx_wrs_ss, "# of work requests (copied from scratch space)");
4588733b9277SNavdeep Parhar }
4589733b9277SNavdeep Parhar
459043bbae19SNavdeep Parhar /*
459143bbae19SNavdeep Parhar * Idempotent.
459243bbae19SNavdeep Parhar */
4593733b9277SNavdeep Parhar static int
alloc_txq(struct vi_info * vi,struct sge_txq * txq,int idx)459443bbae19SNavdeep Parhar alloc_txq(struct vi_info *vi, struct sge_txq *txq, int idx)
4595733b9277SNavdeep Parhar {
459643bbae19SNavdeep Parhar int rc, iqidx;
4597fe2ebb76SJohn Baldwin struct port_info *pi = vi->pi;
459843bbae19SNavdeep Parhar struct adapter *sc = vi->adapter;
4599733b9277SNavdeep Parhar struct sge_eq *eq = &txq->eq;
4600d735920dSNavdeep Parhar struct txpkts *txp;
4601733b9277SNavdeep Parhar char name[16];
460243bbae19SNavdeep Parhar struct sysctl_oid *oid;
4603733b9277SNavdeep Parhar
460443bbae19SNavdeep Parhar if (!(eq->flags & EQ_SW_ALLOCATED)) {
460543bbae19SNavdeep Parhar MPASS(!(eq->flags & EQ_HW_ALLOCATED));
460643bbae19SNavdeep Parhar
460743bbae19SNavdeep Parhar snprintf(name, sizeof(name), "%d", idx);
460843bbae19SNavdeep Parhar oid = SYSCTL_ADD_NODE(&vi->ctx, SYSCTL_CHILDREN(vi->txq_oid),
460943bbae19SNavdeep Parhar OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
461043bbae19SNavdeep Parhar "tx queue");
461143bbae19SNavdeep Parhar
461243bbae19SNavdeep Parhar iqidx = vi->first_rxq + (idx % vi->nrxq);
461343bbae19SNavdeep Parhar snprintf(name, sizeof(name), "%s txq%d",
461443bbae19SNavdeep Parhar device_get_nameunit(vi->dev), idx);
4615857d74b6SNavdeep Parhar init_eq(sc, &txq->eq, EQ_ETH, vi->qsize_txq, pi->port_id,
461643bbae19SNavdeep Parhar &sc->sge.rxq[iqidx].iq, name);
461743bbae19SNavdeep Parhar
461843bbae19SNavdeep Parhar rc = mp_ring_alloc(&txq->r, eq->sidx, txq, eth_tx,
461943bbae19SNavdeep Parhar can_resume_eth_tx, M_CXGBE, &eq->eq_lock, M_WAITOK);
46207951040fSNavdeep Parhar if (rc != 0) {
462143bbae19SNavdeep Parhar CH_ERR(vi, "failed to allocate mp_ring for txq%d: %d\n",
462243bbae19SNavdeep Parhar idx, rc);
462343bbae19SNavdeep Parhar failed:
462443bbae19SNavdeep Parhar sysctl_remove_oid(oid, 1, 1);
46257951040fSNavdeep Parhar return (rc);
46267951040fSNavdeep Parhar }
46277951040fSNavdeep Parhar
462843bbae19SNavdeep Parhar rc = alloc_eq(sc, eq, &vi->ctx, oid);
462943bbae19SNavdeep Parhar if (rc) {
463043bbae19SNavdeep Parhar CH_ERR(vi, "failed to allocate txq%d: %d\n", idx, rc);
46317951040fSNavdeep Parhar mp_ring_free(txq->r);
463243bbae19SNavdeep Parhar goto failed;
463343bbae19SNavdeep Parhar }
463443bbae19SNavdeep Parhar MPASS(eq->flags & EQ_SW_ALLOCATED);
463543bbae19SNavdeep Parhar /* Can't fail after this point. */
463643bbae19SNavdeep Parhar
463743bbae19SNavdeep Parhar TASK_INIT(&txq->tx_reclaim_task, 0, tx_reclaim, eq);
463843bbae19SNavdeep Parhar txq->ifp = vi->ifp;
463943bbae19SNavdeep Parhar txq->gl = sglist_alloc(TX_SGL_SEGS, M_WAITOK);
464043bbae19SNavdeep Parhar txq->sdesc = malloc(eq->sidx * sizeof(struct tx_sdesc), M_CXGBE,
464143bbae19SNavdeep Parhar M_ZERO | M_WAITOK);
464243bbae19SNavdeep Parhar
464343bbae19SNavdeep Parhar add_txq_sysctls(vi, &vi->ctx, oid, txq);
46447951040fSNavdeep Parhar }
4645733b9277SNavdeep Parhar
464643bbae19SNavdeep Parhar if (!(eq->flags & EQ_HW_ALLOCATED)) {
464743bbae19SNavdeep Parhar MPASS(eq->flags & EQ_SW_ALLOCATED);
464843bbae19SNavdeep Parhar rc = alloc_eq_hwq(sc, vi, eq);
464943bbae19SNavdeep Parhar if (rc != 0) {
465043bbae19SNavdeep Parhar CH_ERR(vi, "failed to create hw txq%d: %d\n", idx, rc);
465143bbae19SNavdeep Parhar return (rc);
465243bbae19SNavdeep Parhar }
465343bbae19SNavdeep Parhar MPASS(eq->flags & EQ_HW_ALLOCATED);
46547951040fSNavdeep Parhar /* Can't fail after this point. */
46557951040fSNavdeep Parhar
4656ec55567cSJohn Baldwin if (idx == 0)
4657ec55567cSJohn Baldwin sc->sge.eq_base = eq->abs_id - eq->cntxt_id;
4658ec55567cSJohn Baldwin else
4659ec55567cSJohn Baldwin KASSERT(eq->cntxt_id + sc->sge.eq_base == eq->abs_id,
4660ec55567cSJohn Baldwin ("eq_base mismatch"));
4661ec55567cSJohn Baldwin KASSERT(sc->sge.eq_base == 0 || sc->flags & IS_VF,
4662ec55567cSJohn Baldwin ("PF with non-zero eq_base"));
4663ec55567cSJohn Baldwin
4664d735920dSNavdeep Parhar txp = &txq->txp;
4665d735920dSNavdeep Parhar MPASS(nitems(txp->mb) >= sc->params.max_pkts_per_eth_tx_pkts_wr);
4666d735920dSNavdeep Parhar txq->txp.max_npkt = min(nitems(txp->mb),
4667d735920dSNavdeep Parhar sc->params.max_pkts_per_eth_tx_pkts_wr);
466830e3f2b4SNavdeep Parhar if (vi->flags & TX_USES_VM_WR && !(sc->flags & IS_VF))
466930e3f2b4SNavdeep Parhar txq->txp.max_npkt--;
4670d735920dSNavdeep Parhar
467143bbae19SNavdeep Parhar if (vi->flags & TX_USES_VM_WR)
467243bbae19SNavdeep Parhar txq->cpl_ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
467343bbae19SNavdeep Parhar V_TXPKT_INTF(pi->tx_chan));
467443bbae19SNavdeep Parhar else
467543bbae19SNavdeep Parhar txq->cpl_ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
467643bbae19SNavdeep Parhar V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf) |
467743bbae19SNavdeep Parhar V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
467843bbae19SNavdeep Parhar
467943bbae19SNavdeep Parhar txq->tc_idx = -1;
468043bbae19SNavdeep Parhar }
468143bbae19SNavdeep Parhar
468243bbae19SNavdeep Parhar return (0);
468343bbae19SNavdeep Parhar }
468443bbae19SNavdeep Parhar
468543bbae19SNavdeep Parhar /*
468643bbae19SNavdeep Parhar * Idempotent.
468743bbae19SNavdeep Parhar */
468843bbae19SNavdeep Parhar static void
free_txq(struct vi_info * vi,struct sge_txq * txq)468943bbae19SNavdeep Parhar free_txq(struct vi_info *vi, struct sge_txq *txq)
469043bbae19SNavdeep Parhar {
469143bbae19SNavdeep Parhar struct adapter *sc = vi->adapter;
469243bbae19SNavdeep Parhar struct sge_eq *eq = &txq->eq;
469343bbae19SNavdeep Parhar
469443bbae19SNavdeep Parhar if (eq->flags & EQ_HW_ALLOCATED) {
469543bbae19SNavdeep Parhar MPASS(eq->flags & EQ_SW_ALLOCATED);
469643bbae19SNavdeep Parhar free_eq_hwq(sc, NULL, eq);
469743bbae19SNavdeep Parhar MPASS(!(eq->flags & EQ_HW_ALLOCATED));
469843bbae19SNavdeep Parhar }
469943bbae19SNavdeep Parhar
470043bbae19SNavdeep Parhar if (eq->flags & EQ_SW_ALLOCATED) {
470143bbae19SNavdeep Parhar MPASS(!(eq->flags & EQ_HW_ALLOCATED));
470243bbae19SNavdeep Parhar sglist_free(txq->gl);
470343bbae19SNavdeep Parhar free(txq->sdesc, M_CXGBE);
470443bbae19SNavdeep Parhar mp_ring_free(txq->r);
470543bbae19SNavdeep Parhar free_eq(sc, eq);
470643bbae19SNavdeep Parhar MPASS(!(eq->flags & EQ_SW_ALLOCATED));
470743bbae19SNavdeep Parhar bzero(txq, sizeof(*txq));
470843bbae19SNavdeep Parhar }
470943bbae19SNavdeep Parhar }
471043bbae19SNavdeep Parhar
471143bbae19SNavdeep Parhar static void
add_txq_sysctls(struct vi_info * vi,struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_txq * txq)471243bbae19SNavdeep Parhar add_txq_sysctls(struct vi_info *vi, struct sysctl_ctx_list *ctx,
471343bbae19SNavdeep Parhar struct sysctl_oid *oid, struct sge_txq *txq)
471443bbae19SNavdeep Parhar {
471543bbae19SNavdeep Parhar struct adapter *sc;
471643bbae19SNavdeep Parhar struct sysctl_oid_list *children;
471743bbae19SNavdeep Parhar
471843bbae19SNavdeep Parhar if (ctx == NULL || oid == NULL)
471943bbae19SNavdeep Parhar return;
472043bbae19SNavdeep Parhar
472143bbae19SNavdeep Parhar sc = vi->adapter;
472254e4ee71SNavdeep Parhar children = SYSCTL_CHILDREN(oid);
472354e4ee71SNavdeep Parhar
472443bbae19SNavdeep Parhar mp_ring_sysctls(txq->r, ctx, children);
472559bc8ce0SNavdeep Parhar
472643bbae19SNavdeep Parhar SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tc",
472743bbae19SNavdeep Parhar CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, txq - sc->sge.txq,
472843bbae19SNavdeep Parhar sysctl_tc, "I", "traffic class (-1 means none)");
472902f972e8SNavdeep Parhar
473043bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txcsum", CTLFLAG_RD,
473154e4ee71SNavdeep Parhar &txq->txcsum, "# of times hardware assisted with checksum");
473243bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "vlan_insertion", CTLFLAG_RD,
473343bbae19SNavdeep Parhar &txq->vlan_insertion, "# of times hardware inserted 802.1Q tag");
473443bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tso_wrs", CTLFLAG_RD,
4735a1ea9a82SNavdeep Parhar &txq->tso_wrs, "# of TSO work requests");
473643bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "imm_wrs", CTLFLAG_RD,
473754e4ee71SNavdeep Parhar &txq->imm_wrs, "# of work requests with immediate data");
473843bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "sgl_wrs", CTLFLAG_RD,
473954e4ee71SNavdeep Parhar &txq->sgl_wrs, "# of work requests with direct SGL");
474043bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txpkt_wrs", CTLFLAG_RD,
474154e4ee71SNavdeep Parhar &txq->txpkt_wrs, "# of txpkt work requests (one pkt/WR)");
474243bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txpkts0_wrs", CTLFLAG_RD,
474343bbae19SNavdeep Parhar &txq->txpkts0_wrs, "# of txpkts (type 0) work requests");
474443bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txpkts1_wrs", CTLFLAG_RD,
474543bbae19SNavdeep Parhar &txq->txpkts1_wrs, "# of txpkts (type 1) work requests");
474643bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txpkts0_pkts", CTLFLAG_RD,
474743bbae19SNavdeep Parhar &txq->txpkts0_pkts,
47487951040fSNavdeep Parhar "# of frames tx'd using type0 txpkts work requests");
474943bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txpkts1_pkts", CTLFLAG_RD,
475043bbae19SNavdeep Parhar &txq->txpkts1_pkts,
47517951040fSNavdeep Parhar "# of frames tx'd using type1 txpkts work requests");
475243bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txpkts_flush", CTLFLAG_RD,
475343bbae19SNavdeep Parhar &txq->txpkts_flush,
47543447df8bSNavdeep Parhar "# of times txpkts had to be flushed out by an egress-update");
475543bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "raw_wrs", CTLFLAG_RD,
47565cdaef71SJohn Baldwin &txq->raw_wrs, "# of raw work requests (non-packets)");
475743bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "vxlan_tso_wrs", CTLFLAG_RD,
475843bbae19SNavdeep Parhar &txq->vxlan_tso_wrs, "# of VXLAN TSO work requests");
475943bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "vxlan_txcsum", CTLFLAG_RD,
476043bbae19SNavdeep Parhar &txq->vxlan_txcsum,
4761a4a4ad2dSNavdeep Parhar "# of times hardware assisted with inner checksums (VXLAN)");
4762bddf7343SJohn Baldwin
4763bddf7343SJohn Baldwin #ifdef KERN_TLS
476415f33555SNavdeep Parhar if (is_ktls(sc)) {
476543bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_records",
476643bbae19SNavdeep Parhar CTLFLAG_RD, &txq->kern_tls_records,
4767bddf7343SJohn Baldwin "# of NIC TLS records transmitted");
476843bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_short",
476943bbae19SNavdeep Parhar CTLFLAG_RD, &txq->kern_tls_short,
4770bddf7343SJohn Baldwin "# of short NIC TLS records transmitted");
477143bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_partial",
477243bbae19SNavdeep Parhar CTLFLAG_RD, &txq->kern_tls_partial,
4773bddf7343SJohn Baldwin "# of partial NIC TLS records transmitted");
477443bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_full",
477543bbae19SNavdeep Parhar CTLFLAG_RD, &txq->kern_tls_full,
4776bddf7343SJohn Baldwin "# of full NIC TLS records transmitted");
477743bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_octets",
477843bbae19SNavdeep Parhar CTLFLAG_RD, &txq->kern_tls_octets,
4779bddf7343SJohn Baldwin "# of payload octets in transmitted NIC TLS records");
478043bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_waste",
478143bbae19SNavdeep Parhar CTLFLAG_RD, &txq->kern_tls_waste,
4782bddf7343SJohn Baldwin "# of octets DMAd but not transmitted in NIC TLS records");
478343bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_options",
478443bbae19SNavdeep Parhar CTLFLAG_RD, &txq->kern_tls_options,
4785bddf7343SJohn Baldwin "# of NIC TLS options-only packets transmitted");
478643bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_header",
478743bbae19SNavdeep Parhar CTLFLAG_RD, &txq->kern_tls_header,
4788bddf7343SJohn Baldwin "# of NIC TLS header-only packets transmitted");
478943bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_fin",
479043bbae19SNavdeep Parhar CTLFLAG_RD, &txq->kern_tls_fin,
4791bddf7343SJohn Baldwin "# of NIC TLS FIN-only packets transmitted");
479243bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_fin_short",
479343bbae19SNavdeep Parhar CTLFLAG_RD, &txq->kern_tls_fin_short,
4794bddf7343SJohn Baldwin "# of NIC TLS padded FIN packets on short TLS records");
479543bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_cbc",
479643bbae19SNavdeep Parhar CTLFLAG_RD, &txq->kern_tls_cbc,
4797bddf7343SJohn Baldwin "# of NIC TLS sessions using AES-CBC");
479843bbae19SNavdeep Parhar SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_gcm",
479943bbae19SNavdeep Parhar CTLFLAG_RD, &txq->kern_tls_gcm,
4800bddf7343SJohn Baldwin "# of NIC TLS sessions using AES-GCM");
4801bddf7343SJohn Baldwin }
4802bddf7343SJohn Baldwin #endif
480354e4ee71SNavdeep Parhar }
480454e4ee71SNavdeep Parhar
4805077ba6a8SJohn Baldwin #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
480643bbae19SNavdeep Parhar /*
480743bbae19SNavdeep Parhar * Idempotent.
480843bbae19SNavdeep Parhar */
4809077ba6a8SJohn Baldwin static int
alloc_ofld_txq(struct vi_info * vi,struct sge_ofld_txq * ofld_txq,int idx)481043bbae19SNavdeep Parhar alloc_ofld_txq(struct vi_info *vi, struct sge_ofld_txq *ofld_txq, int idx)
4811077ba6a8SJohn Baldwin {
481243bbae19SNavdeep Parhar struct sysctl_oid *oid;
481343bbae19SNavdeep Parhar struct port_info *pi = vi->pi;
4814077ba6a8SJohn Baldwin struct adapter *sc = vi->adapter;
481543bbae19SNavdeep Parhar struct sge_eq *eq = &ofld_txq->wrq.eq;
481643bbae19SNavdeep Parhar int rc, iqidx;
4817077ba6a8SJohn Baldwin char name[16];
4818077ba6a8SJohn Baldwin
481943bbae19SNavdeep Parhar MPASS(idx >= 0);
482043bbae19SNavdeep Parhar MPASS(idx < vi->nofldtxq);
4821077ba6a8SJohn Baldwin
482243bbae19SNavdeep Parhar if (!(eq->flags & EQ_SW_ALLOCATED)) {
4823077ba6a8SJohn Baldwin snprintf(name, sizeof(name), "%d", idx);
482443bbae19SNavdeep Parhar oid = SYSCTL_ADD_NODE(&vi->ctx,
482543bbae19SNavdeep Parhar SYSCTL_CHILDREN(vi->ofld_txq_oid), OID_AUTO, name,
4826077ba6a8SJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "offload tx queue");
4827077ba6a8SJohn Baldwin
482843bbae19SNavdeep Parhar snprintf(name, sizeof(name), "%s ofld_txq%d",
482943bbae19SNavdeep Parhar device_get_nameunit(vi->dev), idx);
483043bbae19SNavdeep Parhar if (vi->nofldrxq > 0) {
483143bbae19SNavdeep Parhar iqidx = vi->first_ofld_rxq + (idx % vi->nofldrxq);
4832857d74b6SNavdeep Parhar init_eq(sc, eq, EQ_OFLD, vi->qsize_txq, pi->port_id,
483343bbae19SNavdeep Parhar &sc->sge.ofld_rxq[iqidx].iq, name);
483443bbae19SNavdeep Parhar } else {
483543bbae19SNavdeep Parhar iqidx = vi->first_rxq + (idx % vi->nrxq);
4836857d74b6SNavdeep Parhar init_eq(sc, eq, EQ_OFLD, vi->qsize_txq, pi->port_id,
483743bbae19SNavdeep Parhar &sc->sge.rxq[iqidx].iq, name);
483843bbae19SNavdeep Parhar }
483943bbae19SNavdeep Parhar
484043bbae19SNavdeep Parhar rc = alloc_wrq(sc, vi, &ofld_txq->wrq, &vi->ctx, oid);
484143bbae19SNavdeep Parhar if (rc != 0) {
484243bbae19SNavdeep Parhar CH_ERR(vi, "failed to allocate ofld_txq%d: %d\n", idx,
484343bbae19SNavdeep Parhar rc);
484443bbae19SNavdeep Parhar sysctl_remove_oid(oid, 1, 1);
4845077ba6a8SJohn Baldwin return (rc);
484643bbae19SNavdeep Parhar }
484743bbae19SNavdeep Parhar MPASS(eq->flags & EQ_SW_ALLOCATED);
484843bbae19SNavdeep Parhar /* Can't fail after this point. */
4849077ba6a8SJohn Baldwin
4850568e69e4SJohn Baldwin ofld_txq->tx_iscsi_pdus = counter_u64_alloc(M_WAITOK);
4851568e69e4SJohn Baldwin ofld_txq->tx_iscsi_octets = counter_u64_alloc(M_WAITOK);
48525b27e4b2SJohn Baldwin ofld_txq->tx_iscsi_iso_wrs = counter_u64_alloc(M_WAITOK);
4853c3d4aea6SJohn Baldwin ofld_txq->tx_aio_jobs = counter_u64_alloc(M_WAITOK);
4854c3d4aea6SJohn Baldwin ofld_txq->tx_aio_octets = counter_u64_alloc(M_WAITOK);
4855fe496dc0SJohn Baldwin ofld_txq->tx_toe_tls_records = counter_u64_alloc(M_WAITOK);
4856fe496dc0SJohn Baldwin ofld_txq->tx_toe_tls_octets = counter_u64_alloc(M_WAITOK);
485743bbae19SNavdeep Parhar add_ofld_txq_sysctls(&vi->ctx, oid, ofld_txq);
4858077ba6a8SJohn Baldwin }
4859077ba6a8SJohn Baldwin
486043bbae19SNavdeep Parhar if (!(eq->flags & EQ_HW_ALLOCATED)) {
4861*0a9d1da6SNavdeep Parhar MPASS(eq->flags & EQ_SW_ALLOCATED);
4862*0a9d1da6SNavdeep Parhar MPASS(ofld_txq->wrq.nwr_pending == 0);
4863*0a9d1da6SNavdeep Parhar MPASS(ofld_txq->wrq.ndesc_needed == 0);
486443bbae19SNavdeep Parhar rc = alloc_eq_hwq(sc, vi, eq);
486543bbae19SNavdeep Parhar if (rc != 0) {
486643bbae19SNavdeep Parhar CH_ERR(vi, "failed to create hw ofld_txq%d: %d\n", idx,
486743bbae19SNavdeep Parhar rc);
486843bbae19SNavdeep Parhar return (rc);
486943bbae19SNavdeep Parhar }
487043bbae19SNavdeep Parhar MPASS(eq->flags & EQ_HW_ALLOCATED);
487143bbae19SNavdeep Parhar }
487243bbae19SNavdeep Parhar
487343bbae19SNavdeep Parhar return (0);
487443bbae19SNavdeep Parhar }
487543bbae19SNavdeep Parhar
487643bbae19SNavdeep Parhar /*
487743bbae19SNavdeep Parhar * Idempotent.
487843bbae19SNavdeep Parhar */
487943bbae19SNavdeep Parhar static void
free_ofld_txq(struct vi_info * vi,struct sge_ofld_txq * ofld_txq)4880077ba6a8SJohn Baldwin free_ofld_txq(struct vi_info *vi, struct sge_ofld_txq *ofld_txq)
4881077ba6a8SJohn Baldwin {
4882077ba6a8SJohn Baldwin struct adapter *sc = vi->adapter;
488343bbae19SNavdeep Parhar struct sge_eq *eq = &ofld_txq->wrq.eq;
4884077ba6a8SJohn Baldwin
488543bbae19SNavdeep Parhar if (eq->flags & EQ_HW_ALLOCATED) {
488643bbae19SNavdeep Parhar MPASS(eq->flags & EQ_SW_ALLOCATED);
488743bbae19SNavdeep Parhar free_eq_hwq(sc, NULL, eq);
488843bbae19SNavdeep Parhar MPASS(!(eq->flags & EQ_HW_ALLOCATED));
488943bbae19SNavdeep Parhar }
4890077ba6a8SJohn Baldwin
489143bbae19SNavdeep Parhar if (eq->flags & EQ_SW_ALLOCATED) {
489243bbae19SNavdeep Parhar MPASS(!(eq->flags & EQ_HW_ALLOCATED));
4893568e69e4SJohn Baldwin counter_u64_free(ofld_txq->tx_iscsi_pdus);
4894568e69e4SJohn Baldwin counter_u64_free(ofld_txq->tx_iscsi_octets);
48955b27e4b2SJohn Baldwin counter_u64_free(ofld_txq->tx_iscsi_iso_wrs);
4896c3d4aea6SJohn Baldwin counter_u64_free(ofld_txq->tx_aio_jobs);
4897c3d4aea6SJohn Baldwin counter_u64_free(ofld_txq->tx_aio_octets);
4898fe496dc0SJohn Baldwin counter_u64_free(ofld_txq->tx_toe_tls_records);
4899fe496dc0SJohn Baldwin counter_u64_free(ofld_txq->tx_toe_tls_octets);
490043bbae19SNavdeep Parhar free_wrq(sc, &ofld_txq->wrq);
490143bbae19SNavdeep Parhar MPASS(!(eq->flags & EQ_SW_ALLOCATED));
4902077ba6a8SJohn Baldwin bzero(ofld_txq, sizeof(*ofld_txq));
490343bbae19SNavdeep Parhar }
490443bbae19SNavdeep Parhar }
490543bbae19SNavdeep Parhar
490643bbae19SNavdeep Parhar static void
add_ofld_txq_sysctls(struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_ofld_txq * ofld_txq)490743bbae19SNavdeep Parhar add_ofld_txq_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid,
490843bbae19SNavdeep Parhar struct sge_ofld_txq *ofld_txq)
490943bbae19SNavdeep Parhar {
491043bbae19SNavdeep Parhar struct sysctl_oid_list *children;
491143bbae19SNavdeep Parhar
491243bbae19SNavdeep Parhar if (ctx == NULL || oid == NULL)
491343bbae19SNavdeep Parhar return;
491443bbae19SNavdeep Parhar
491543bbae19SNavdeep Parhar children = SYSCTL_CHILDREN(oid);
491643bbae19SNavdeep Parhar SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_iscsi_pdus",
491743bbae19SNavdeep Parhar CTLFLAG_RD, &ofld_txq->tx_iscsi_pdus,
491843bbae19SNavdeep Parhar "# of iSCSI PDUs transmitted");
491943bbae19SNavdeep Parhar SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_iscsi_octets",
492043bbae19SNavdeep Parhar CTLFLAG_RD, &ofld_txq->tx_iscsi_octets,
492143bbae19SNavdeep Parhar "# of payload octets in transmitted iSCSI PDUs");
49225b27e4b2SJohn Baldwin SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_iscsi_iso_wrs",
49235b27e4b2SJohn Baldwin CTLFLAG_RD, &ofld_txq->tx_iscsi_iso_wrs,
49245b27e4b2SJohn Baldwin "# of iSCSI segmentation offload work requests");
4925c3d4aea6SJohn Baldwin SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_aio_jobs",
4926c3d4aea6SJohn Baldwin CTLFLAG_RD, &ofld_txq->tx_aio_jobs,
4927c3d4aea6SJohn Baldwin "# of zero-copy aio_write(2) jobs transmitted");
4928c3d4aea6SJohn Baldwin SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_aio_octets",
4929c3d4aea6SJohn Baldwin CTLFLAG_RD, &ofld_txq->tx_aio_octets,
4930c3d4aea6SJohn Baldwin "# of payload octets in transmitted zero-copy aio_write(2) jobs");
493143bbae19SNavdeep Parhar SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_toe_tls_records",
493243bbae19SNavdeep Parhar CTLFLAG_RD, &ofld_txq->tx_toe_tls_records,
493343bbae19SNavdeep Parhar "# of TOE TLS records transmitted");
493443bbae19SNavdeep Parhar SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_toe_tls_octets",
493543bbae19SNavdeep Parhar CTLFLAG_RD, &ofld_txq->tx_toe_tls_octets,
493643bbae19SNavdeep Parhar "# of payload octets in transmitted TOE TLS records");
4937077ba6a8SJohn Baldwin }
4938077ba6a8SJohn Baldwin #endif
4939077ba6a8SJohn Baldwin
494054e4ee71SNavdeep Parhar static void
oneseg_dma_callback(void * arg,bus_dma_segment_t * segs,int nseg,int error)494154e4ee71SNavdeep Parhar oneseg_dma_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
494254e4ee71SNavdeep Parhar {
494354e4ee71SNavdeep Parhar bus_addr_t *ba = arg;
494454e4ee71SNavdeep Parhar
494554e4ee71SNavdeep Parhar KASSERT(nseg == 1,
494654e4ee71SNavdeep Parhar ("%s meant for single segment mappings only.", __func__));
494754e4ee71SNavdeep Parhar
494854e4ee71SNavdeep Parhar *ba = error ? 0 : segs->ds_addr;
494954e4ee71SNavdeep Parhar }
495054e4ee71SNavdeep Parhar
495154e4ee71SNavdeep Parhar static inline void
ring_fl_db(struct adapter * sc,struct sge_fl * fl)495254e4ee71SNavdeep Parhar ring_fl_db(struct adapter *sc, struct sge_fl *fl)
495354e4ee71SNavdeep Parhar {
49544d6db4e0SNavdeep Parhar uint32_t n, v;
495554e4ee71SNavdeep Parhar
495646e1e307SNavdeep Parhar n = IDXDIFF(fl->pidx >> 3, fl->dbidx, fl->sidx);
49574d6db4e0SNavdeep Parhar MPASS(n > 0);
4958d14b0ac1SNavdeep Parhar
495954e4ee71SNavdeep Parhar wmb();
49604d6db4e0SNavdeep Parhar v = fl->dbval | V_PIDX(n);
49614d6db4e0SNavdeep Parhar if (fl->udb)
49624d6db4e0SNavdeep Parhar *fl->udb = htole32(v);
49634d6db4e0SNavdeep Parhar else
4964315048f2SJohn Baldwin t4_write_reg(sc, sc->sge_kdoorbell_reg, v);
49654d6db4e0SNavdeep Parhar IDXINCR(fl->dbidx, n, fl->sidx);
496654e4ee71SNavdeep Parhar }
496754e4ee71SNavdeep Parhar
4968fb12416cSNavdeep Parhar /*
49694d6db4e0SNavdeep Parhar * Fills up the freelist by allocating up to 'n' buffers. Buffers that are
49704d6db4e0SNavdeep Parhar * recycled do not count towards this allocation budget.
4971733b9277SNavdeep Parhar *
49724d6db4e0SNavdeep Parhar * Returns non-zero to indicate that this freelist should be added to the list
49734d6db4e0SNavdeep Parhar * of starving freelists.
4974fb12416cSNavdeep Parhar */
4975733b9277SNavdeep Parhar static int
refill_fl(struct adapter * sc,struct sge_fl * fl,int n)49764d6db4e0SNavdeep Parhar refill_fl(struct adapter *sc, struct sge_fl *fl, int n)
497754e4ee71SNavdeep Parhar {
49784d6db4e0SNavdeep Parhar __be64 *d;
49794d6db4e0SNavdeep Parhar struct fl_sdesc *sd;
498038035ed6SNavdeep Parhar uintptr_t pa;
498154e4ee71SNavdeep Parhar caddr_t cl;
498246e1e307SNavdeep Parhar struct rx_buf_info *rxb;
498338035ed6SNavdeep Parhar struct cluster_metadata *clm;
4984294e62beSAlexander Motin uint16_t max_pidx, zidx = fl->zidx;
49854d6db4e0SNavdeep Parhar uint16_t hw_cidx = fl->hw_cidx; /* stable snapshot */
498654e4ee71SNavdeep Parhar
498754e4ee71SNavdeep Parhar FL_LOCK_ASSERT_OWNED(fl);
498854e4ee71SNavdeep Parhar
49894d6db4e0SNavdeep Parhar /*
4990453130d9SPedro F. Giffuni * We always stop at the beginning of the hardware descriptor that's just
49914d6db4e0SNavdeep Parhar * before the one with the hw cidx. This is to avoid hw pidx = hw cidx,
49924d6db4e0SNavdeep Parhar * which would mean an empty freelist to the chip.
49934d6db4e0SNavdeep Parhar */
49944d6db4e0SNavdeep Parhar max_pidx = __predict_false(hw_cidx == 0) ? fl->sidx - 1 : hw_cidx - 1;
49954d6db4e0SNavdeep Parhar if (fl->pidx == max_pidx * 8)
49964d6db4e0SNavdeep Parhar return (0);
499754e4ee71SNavdeep Parhar
49984d6db4e0SNavdeep Parhar d = &fl->desc[fl->pidx];
49994d6db4e0SNavdeep Parhar sd = &fl->sdesc[fl->pidx];
5000294e62beSAlexander Motin rxb = &sc->sge.rx_buf_info[zidx];
50014d6db4e0SNavdeep Parhar
50024d6db4e0SNavdeep Parhar while (n > 0) {
500354e4ee71SNavdeep Parhar
500454e4ee71SNavdeep Parhar if (sd->cl != NULL) {
500554e4ee71SNavdeep Parhar
5006c3fb7725SNavdeep Parhar if (sd->nmbuf == 0) {
500738035ed6SNavdeep Parhar /*
500838035ed6SNavdeep Parhar * Fast recycle without involving any atomics on
500938035ed6SNavdeep Parhar * the cluster's metadata (if the cluster has
501038035ed6SNavdeep Parhar * metadata). This happens when all frames
501138035ed6SNavdeep Parhar * received in the cluster were small enough to
501238035ed6SNavdeep Parhar * fit within a single mbuf each.
501338035ed6SNavdeep Parhar */
501438035ed6SNavdeep Parhar fl->cl_fast_recycled++;
5015a9c4062aSNavdeep Parhar goto recycled;
501638035ed6SNavdeep Parhar }
501754e4ee71SNavdeep Parhar
501838035ed6SNavdeep Parhar /*
501938035ed6SNavdeep Parhar * Cluster is guaranteed to have metadata. Clusters
502038035ed6SNavdeep Parhar * without metadata always take the fast recycle path
502138035ed6SNavdeep Parhar * when they're recycled.
502238035ed6SNavdeep Parhar */
502346e1e307SNavdeep Parhar clm = cl_metadata(sd);
502438035ed6SNavdeep Parhar MPASS(clm != NULL);
50251458bff9SNavdeep Parhar
502638035ed6SNavdeep Parhar if (atomic_fetchadd_int(&clm->refcount, -1) == 1) {
502738035ed6SNavdeep Parhar fl->cl_recycled++;
502882eff304SNavdeep Parhar counter_u64_add(extfree_rels, 1);
502954e4ee71SNavdeep Parhar goto recycled;
503054e4ee71SNavdeep Parhar }
50311458bff9SNavdeep Parhar sd->cl = NULL; /* gave up my reference */
50321458bff9SNavdeep Parhar }
503338035ed6SNavdeep Parhar MPASS(sd->cl == NULL);
503446e1e307SNavdeep Parhar cl = uma_zalloc(rxb->zone, M_NOWAIT);
50352b9010f0SNavdeep Parhar if (__predict_false(cl == NULL)) {
5036294e62beSAlexander Motin if (zidx != fl->safe_zidx) {
5037294e62beSAlexander Motin zidx = fl->safe_zidx;
5038294e62beSAlexander Motin rxb = &sc->sge.rx_buf_info[zidx];
503946e1e307SNavdeep Parhar cl = uma_zalloc(rxb->zone, M_NOWAIT);
50402b9010f0SNavdeep Parhar }
50412b9010f0SNavdeep Parhar if (cl == NULL)
504254e4ee71SNavdeep Parhar break;
504354e4ee71SNavdeep Parhar }
504438035ed6SNavdeep Parhar fl->cl_allocated++;
50454d6db4e0SNavdeep Parhar n--;
504654e4ee71SNavdeep Parhar
504738035ed6SNavdeep Parhar pa = pmap_kextract((vm_offset_t)cl);
504854e4ee71SNavdeep Parhar sd->cl = cl;
5049294e62beSAlexander Motin sd->zidx = zidx;
505046e1e307SNavdeep Parhar
505146e1e307SNavdeep Parhar if (fl->flags & FL_BUF_PACKING) {
505246e1e307SNavdeep Parhar *d = htobe64(pa | rxb->hwidx2);
505346e1e307SNavdeep Parhar sd->moff = rxb->size2;
505446e1e307SNavdeep Parhar } else {
505546e1e307SNavdeep Parhar *d = htobe64(pa | rxb->hwidx1);
505646e1e307SNavdeep Parhar sd->moff = 0;
505746e1e307SNavdeep Parhar }
50587d29df59SNavdeep Parhar recycled:
5059c3fb7725SNavdeep Parhar sd->nmbuf = 0;
506038035ed6SNavdeep Parhar d++;
506154e4ee71SNavdeep Parhar sd++;
506246e1e307SNavdeep Parhar if (__predict_false((++fl->pidx & 7) == 0)) {
506346e1e307SNavdeep Parhar uint16_t pidx = fl->pidx >> 3;
50644d6db4e0SNavdeep Parhar
50654d6db4e0SNavdeep Parhar if (__predict_false(pidx == fl->sidx)) {
506654e4ee71SNavdeep Parhar fl->pidx = 0;
50674d6db4e0SNavdeep Parhar pidx = 0;
506854e4ee71SNavdeep Parhar sd = fl->sdesc;
506954e4ee71SNavdeep Parhar d = fl->desc;
507054e4ee71SNavdeep Parhar }
507146e1e307SNavdeep Parhar if (n < 8 || pidx == max_pidx)
50724d6db4e0SNavdeep Parhar break;
50734d6db4e0SNavdeep Parhar
50744d6db4e0SNavdeep Parhar if (IDXDIFF(pidx, fl->dbidx, fl->sidx) >= 4)
50754d6db4e0SNavdeep Parhar ring_fl_db(sc, fl);
50764d6db4e0SNavdeep Parhar }
507754e4ee71SNavdeep Parhar }
5078fb12416cSNavdeep Parhar
507946e1e307SNavdeep Parhar if ((fl->pidx >> 3) != fl->dbidx)
5080fb12416cSNavdeep Parhar ring_fl_db(sc, fl);
5081733b9277SNavdeep Parhar
5082733b9277SNavdeep Parhar return (FL_RUNNING_LOW(fl) && !(fl->flags & FL_STARVING));
5083733b9277SNavdeep Parhar }
5084733b9277SNavdeep Parhar
5085733b9277SNavdeep Parhar /*
5086733b9277SNavdeep Parhar * Attempt to refill all starving freelists.
5087733b9277SNavdeep Parhar */
5088733b9277SNavdeep Parhar static void
refill_sfl(void * arg)5089733b9277SNavdeep Parhar refill_sfl(void *arg)
5090733b9277SNavdeep Parhar {
5091733b9277SNavdeep Parhar struct adapter *sc = arg;
5092733b9277SNavdeep Parhar struct sge_fl *fl, *fl_temp;
5093733b9277SNavdeep Parhar
5094fe2ebb76SJohn Baldwin mtx_assert(&sc->sfl_lock, MA_OWNED);
5095733b9277SNavdeep Parhar TAILQ_FOREACH_SAFE(fl, &sc->sfl, link, fl_temp) {
5096733b9277SNavdeep Parhar FL_LOCK(fl);
5097733b9277SNavdeep Parhar refill_fl(sc, fl, 64);
5098733b9277SNavdeep Parhar if (FL_NOT_RUNNING_LOW(fl) || fl->flags & FL_DOOMED) {
5099733b9277SNavdeep Parhar TAILQ_REMOVE(&sc->sfl, fl, link);
5100733b9277SNavdeep Parhar fl->flags &= ~FL_STARVING;
5101733b9277SNavdeep Parhar }
5102733b9277SNavdeep Parhar FL_UNLOCK(fl);
5103733b9277SNavdeep Parhar }
5104733b9277SNavdeep Parhar
5105733b9277SNavdeep Parhar if (!TAILQ_EMPTY(&sc->sfl))
5106733b9277SNavdeep Parhar callout_schedule(&sc->sfl_callout, hz / 5);
510754e4ee71SNavdeep Parhar }
510854e4ee71SNavdeep Parhar
510943bbae19SNavdeep Parhar /*
511043bbae19SNavdeep Parhar * Release the driver's reference on all buffers in the given freelist. Buffers
511143bbae19SNavdeep Parhar * with kernel references cannot be freed and will prevent the driver from being
511243bbae19SNavdeep Parhar * unloaded safely.
511343bbae19SNavdeep Parhar */
511443bbae19SNavdeep Parhar void
free_fl_buffers(struct adapter * sc,struct sge_fl * fl)511543bbae19SNavdeep Parhar free_fl_buffers(struct adapter *sc, struct sge_fl *fl)
511654e4ee71SNavdeep Parhar {
511754e4ee71SNavdeep Parhar struct fl_sdesc *sd;
511838035ed6SNavdeep Parhar struct cluster_metadata *clm;
511954e4ee71SNavdeep Parhar int i;
512054e4ee71SNavdeep Parhar
512154e4ee71SNavdeep Parhar sd = fl->sdesc;
51224d6db4e0SNavdeep Parhar for (i = 0; i < fl->sidx * 8; i++, sd++) {
512338035ed6SNavdeep Parhar if (sd->cl == NULL)
512438035ed6SNavdeep Parhar continue;
512554e4ee71SNavdeep Parhar
512682eff304SNavdeep Parhar if (sd->nmbuf == 0)
512746e1e307SNavdeep Parhar uma_zfree(sc->sge.rx_buf_info[sd->zidx].zone, sd->cl);
512846e1e307SNavdeep Parhar else if (fl->flags & FL_BUF_PACKING) {
512946e1e307SNavdeep Parhar clm = cl_metadata(sd);
513046e1e307SNavdeep Parhar if (atomic_fetchadd_int(&clm->refcount, -1) == 1) {
513146e1e307SNavdeep Parhar uma_zfree(sc->sge.rx_buf_info[sd->zidx].zone,
513246e1e307SNavdeep Parhar sd->cl);
513382eff304SNavdeep Parhar counter_u64_add(extfree_rels, 1);
513454e4ee71SNavdeep Parhar }
513546e1e307SNavdeep Parhar }
513638035ed6SNavdeep Parhar sd->cl = NULL;
513754e4ee71SNavdeep Parhar }
513854e4ee71SNavdeep Parhar
513943bbae19SNavdeep Parhar if (fl->flags & FL_BUF_RESUME) {
514043bbae19SNavdeep Parhar m_freem(fl->m0);
514143bbae19SNavdeep Parhar fl->flags &= ~FL_BUF_RESUME;
514243bbae19SNavdeep Parhar }
514354e4ee71SNavdeep Parhar }
514454e4ee71SNavdeep Parhar
51457951040fSNavdeep Parhar static inline void
get_pkt_gl(struct mbuf * m,struct sglist * gl)51467951040fSNavdeep Parhar get_pkt_gl(struct mbuf *m, struct sglist *gl)
514754e4ee71SNavdeep Parhar {
51487951040fSNavdeep Parhar int rc;
514954e4ee71SNavdeep Parhar
51507951040fSNavdeep Parhar M_ASSERTPKTHDR(m);
515154e4ee71SNavdeep Parhar
51527951040fSNavdeep Parhar sglist_reset(gl);
51537951040fSNavdeep Parhar rc = sglist_append_mbuf(gl, m);
51547951040fSNavdeep Parhar if (__predict_false(rc != 0)) {
51557951040fSNavdeep Parhar panic("%s: mbuf %p (%d segs) was vetted earlier but now fails "
51567951040fSNavdeep Parhar "with %d.", __func__, m, mbuf_nsegs(m), rc);
515754e4ee71SNavdeep Parhar }
515854e4ee71SNavdeep Parhar
51597951040fSNavdeep Parhar KASSERT(gl->sg_nseg == mbuf_nsegs(m),
51607951040fSNavdeep Parhar ("%s: nsegs changed for mbuf %p from %d to %d", __func__, m,
51617951040fSNavdeep Parhar mbuf_nsegs(m), gl->sg_nseg));
516230e3f2b4SNavdeep Parhar #if 0 /* vm_wr not readily available here. */
516330e3f2b4SNavdeep Parhar KASSERT(gl->sg_nseg > 0 && gl->sg_nseg <= max_nsegs_allowed(m, vm_wr),
51647951040fSNavdeep Parhar ("%s: %d segments, should have been 1 <= nsegs <= %d", __func__,
516530e3f2b4SNavdeep Parhar gl->sg_nseg, max_nsegs_allowed(m, vm_wr)));
516630e3f2b4SNavdeep Parhar #endif
516754e4ee71SNavdeep Parhar }
516854e4ee71SNavdeep Parhar
516954e4ee71SNavdeep Parhar /*
51707951040fSNavdeep Parhar * len16 for a txpkt WR with a GL. Includes the firmware work request header.
517154e4ee71SNavdeep Parhar */
51727951040fSNavdeep Parhar static inline u_int
txpkt_len16(u_int nsegs,const u_int extra)5173a4a4ad2dSNavdeep Parhar txpkt_len16(u_int nsegs, const u_int extra)
51747951040fSNavdeep Parhar {
51757951040fSNavdeep Parhar u_int n;
51767951040fSNavdeep Parhar
51777951040fSNavdeep Parhar MPASS(nsegs > 0);
51787951040fSNavdeep Parhar
51797951040fSNavdeep Parhar nsegs--; /* first segment is part of ulptx_sgl */
5180a4a4ad2dSNavdeep Parhar n = extra + sizeof(struct fw_eth_tx_pkt_wr) +
5181a4a4ad2dSNavdeep Parhar sizeof(struct cpl_tx_pkt_core) +
51827951040fSNavdeep Parhar sizeof(struct ulptx_sgl) + 8 * ((3 * nsegs) / 2 + (nsegs & 1));
51837951040fSNavdeep Parhar
51847951040fSNavdeep Parhar return (howmany(n, 16));
51857951040fSNavdeep Parhar }
518654e4ee71SNavdeep Parhar
518754e4ee71SNavdeep Parhar /*
51886af45170SJohn Baldwin * len16 for a txpkt_vm WR with a GL. Includes the firmware work
51896af45170SJohn Baldwin * request header.
51906af45170SJohn Baldwin */
51916af45170SJohn Baldwin static inline u_int
txpkt_vm_len16(u_int nsegs,const u_int extra)5192a4a4ad2dSNavdeep Parhar txpkt_vm_len16(u_int nsegs, const u_int extra)
51936af45170SJohn Baldwin {
51946af45170SJohn Baldwin u_int n;
51956af45170SJohn Baldwin
51966af45170SJohn Baldwin MPASS(nsegs > 0);
51976af45170SJohn Baldwin
51986af45170SJohn Baldwin nsegs--; /* first segment is part of ulptx_sgl */
5199a4a4ad2dSNavdeep Parhar n = extra + sizeof(struct fw_eth_tx_pkt_vm_wr) +
52006af45170SJohn Baldwin sizeof(struct cpl_tx_pkt_core) +
52016af45170SJohn Baldwin sizeof(struct ulptx_sgl) + 8 * ((3 * nsegs) / 2 + (nsegs & 1));
52026af45170SJohn Baldwin
52036af45170SJohn Baldwin return (howmany(n, 16));
52046af45170SJohn Baldwin }
52056af45170SJohn Baldwin
5206a4a4ad2dSNavdeep Parhar static inline void
calculate_mbuf_len16(struct mbuf * m,bool vm_wr)520730e3f2b4SNavdeep Parhar calculate_mbuf_len16(struct mbuf *m, bool vm_wr)
5208a4a4ad2dSNavdeep Parhar {
5209a4a4ad2dSNavdeep Parhar const int lso = sizeof(struct cpl_tx_pkt_lso_core);
5210a4a4ad2dSNavdeep Parhar const int tnl_lso = sizeof(struct cpl_tx_tnl_lso);
5211a4a4ad2dSNavdeep Parhar
521230e3f2b4SNavdeep Parhar if (vm_wr) {
5213a4a4ad2dSNavdeep Parhar if (needs_tso(m))
5214a4a4ad2dSNavdeep Parhar set_mbuf_len16(m, txpkt_vm_len16(mbuf_nsegs(m), lso));
5215a4a4ad2dSNavdeep Parhar else
5216a4a4ad2dSNavdeep Parhar set_mbuf_len16(m, txpkt_vm_len16(mbuf_nsegs(m), 0));
5217a4a4ad2dSNavdeep Parhar return;
5218a4a4ad2dSNavdeep Parhar }
5219a4a4ad2dSNavdeep Parhar
5220a4a4ad2dSNavdeep Parhar if (needs_tso(m)) {
5221a4a4ad2dSNavdeep Parhar if (needs_vxlan_tso(m))
5222a4a4ad2dSNavdeep Parhar set_mbuf_len16(m, txpkt_len16(mbuf_nsegs(m), tnl_lso));
5223a4a4ad2dSNavdeep Parhar else
5224a4a4ad2dSNavdeep Parhar set_mbuf_len16(m, txpkt_len16(mbuf_nsegs(m), lso));
5225a4a4ad2dSNavdeep Parhar } else
5226a4a4ad2dSNavdeep Parhar set_mbuf_len16(m, txpkt_len16(mbuf_nsegs(m), 0));
5227a4a4ad2dSNavdeep Parhar }
5228a4a4ad2dSNavdeep Parhar
52296af45170SJohn Baldwin /*
52307951040fSNavdeep Parhar * len16 for a txpkts type 0 WR with a GL. Does not include the firmware work
52317951040fSNavdeep Parhar * request header.
52327951040fSNavdeep Parhar */
52337951040fSNavdeep Parhar static inline u_int
txpkts0_len16(u_int nsegs)52347951040fSNavdeep Parhar txpkts0_len16(u_int nsegs)
52357951040fSNavdeep Parhar {
52367951040fSNavdeep Parhar u_int n;
52377951040fSNavdeep Parhar
52387951040fSNavdeep Parhar MPASS(nsegs > 0);
52397951040fSNavdeep Parhar
52407951040fSNavdeep Parhar nsegs--; /* first segment is part of ulptx_sgl */
52417951040fSNavdeep Parhar n = sizeof(struct ulp_txpkt) + sizeof(struct ulptx_idata) +
52427951040fSNavdeep Parhar sizeof(struct cpl_tx_pkt_core) + sizeof(struct ulptx_sgl) +
52437951040fSNavdeep Parhar 8 * ((3 * nsegs) / 2 + (nsegs & 1));
52447951040fSNavdeep Parhar
52457951040fSNavdeep Parhar return (howmany(n, 16));
52467951040fSNavdeep Parhar }
52477951040fSNavdeep Parhar
52487951040fSNavdeep Parhar /*
52497951040fSNavdeep Parhar * len16 for a txpkts type 1 WR with a GL. Does not include the firmware work
52507951040fSNavdeep Parhar * request header.
52517951040fSNavdeep Parhar */
52527951040fSNavdeep Parhar static inline u_int
txpkts1_len16(void)52537951040fSNavdeep Parhar txpkts1_len16(void)
52547951040fSNavdeep Parhar {
52557951040fSNavdeep Parhar u_int n;
52567951040fSNavdeep Parhar
52577951040fSNavdeep Parhar n = sizeof(struct cpl_tx_pkt_core) + sizeof(struct ulptx_sgl);
52587951040fSNavdeep Parhar
52597951040fSNavdeep Parhar return (howmany(n, 16));
52607951040fSNavdeep Parhar }
52617951040fSNavdeep Parhar
52627951040fSNavdeep Parhar static inline u_int
imm_payload(u_int ndesc)52637951040fSNavdeep Parhar imm_payload(u_int ndesc)
52647951040fSNavdeep Parhar {
52657951040fSNavdeep Parhar u_int n;
52667951040fSNavdeep Parhar
52677951040fSNavdeep Parhar n = ndesc * EQ_ESIZE - sizeof(struct fw_eth_tx_pkt_wr) -
52687951040fSNavdeep Parhar sizeof(struct cpl_tx_pkt_core);
52697951040fSNavdeep Parhar
52707951040fSNavdeep Parhar return (n);
52717951040fSNavdeep Parhar }
52727951040fSNavdeep Parhar
5273c0236bd9SNavdeep Parhar static inline uint64_t
csum_to_ctrl(struct adapter * sc,struct mbuf * m)5274c0236bd9SNavdeep Parhar csum_to_ctrl(struct adapter *sc, struct mbuf *m)
5275c0236bd9SNavdeep Parhar {
5276c0236bd9SNavdeep Parhar uint64_t ctrl;
5277a4a4ad2dSNavdeep Parhar int csum_type, l2hlen, l3hlen;
5278a4a4ad2dSNavdeep Parhar int x, y;
5279a4a4ad2dSNavdeep Parhar static const int csum_types[3][2] = {
5280a4a4ad2dSNavdeep Parhar {TX_CSUM_TCPIP, TX_CSUM_TCPIP6},
5281a4a4ad2dSNavdeep Parhar {TX_CSUM_UDPIP, TX_CSUM_UDPIP6},
5282a4a4ad2dSNavdeep Parhar {TX_CSUM_IP, 0}
5283a4a4ad2dSNavdeep Parhar };
5284c0236bd9SNavdeep Parhar
5285c0236bd9SNavdeep Parhar M_ASSERTPKTHDR(m);
5286c0236bd9SNavdeep Parhar
5287a4a4ad2dSNavdeep Parhar if (!needs_hwcsum(m))
5288c0236bd9SNavdeep Parhar return (F_TXPKT_IPCSUM_DIS | F_TXPKT_L4CSUM_DIS);
5289c0236bd9SNavdeep Parhar
5290a4a4ad2dSNavdeep Parhar MPASS(m->m_pkthdr.l2hlen >= ETHER_HDR_LEN);
5291a4a4ad2dSNavdeep Parhar MPASS(m->m_pkthdr.l3hlen >= sizeof(struct ip));
5292a4a4ad2dSNavdeep Parhar
5293a4a4ad2dSNavdeep Parhar if (needs_vxlan_csum(m)) {
5294a4a4ad2dSNavdeep Parhar MPASS(m->m_pkthdr.l4hlen > 0);
5295a4a4ad2dSNavdeep Parhar MPASS(m->m_pkthdr.l5hlen > 0);
5296a4a4ad2dSNavdeep Parhar MPASS(m->m_pkthdr.inner_l2hlen >= ETHER_HDR_LEN);
5297a4a4ad2dSNavdeep Parhar MPASS(m->m_pkthdr.inner_l3hlen >= sizeof(struct ip));
5298a4a4ad2dSNavdeep Parhar
5299a4a4ad2dSNavdeep Parhar l2hlen = m->m_pkthdr.l2hlen + m->m_pkthdr.l3hlen +
5300a4a4ad2dSNavdeep Parhar m->m_pkthdr.l4hlen + m->m_pkthdr.l5hlen +
5301a4a4ad2dSNavdeep Parhar m->m_pkthdr.inner_l2hlen - ETHER_HDR_LEN;
5302a4a4ad2dSNavdeep Parhar l3hlen = m->m_pkthdr.inner_l3hlen;
5303a4a4ad2dSNavdeep Parhar } else {
5304a4a4ad2dSNavdeep Parhar l2hlen = m->m_pkthdr.l2hlen - ETHER_HDR_LEN;
5305a4a4ad2dSNavdeep Parhar l3hlen = m->m_pkthdr.l3hlen;
5306c0236bd9SNavdeep Parhar }
5307c0236bd9SNavdeep Parhar
5308a4a4ad2dSNavdeep Parhar ctrl = 0;
5309a4a4ad2dSNavdeep Parhar if (!needs_l3_csum(m))
5310a4a4ad2dSNavdeep Parhar ctrl |= F_TXPKT_IPCSUM_DIS;
5311a4a4ad2dSNavdeep Parhar
5312a4a4ad2dSNavdeep Parhar if (m->m_pkthdr.csum_flags & (CSUM_IP_TCP | CSUM_INNER_IP_TCP |
5313a4a4ad2dSNavdeep Parhar CSUM_IP6_TCP | CSUM_INNER_IP6_TCP))
5314a4a4ad2dSNavdeep Parhar x = 0; /* TCP */
5315a4a4ad2dSNavdeep Parhar else if (m->m_pkthdr.csum_flags & (CSUM_IP_UDP | CSUM_INNER_IP_UDP |
5316a4a4ad2dSNavdeep Parhar CSUM_IP6_UDP | CSUM_INNER_IP6_UDP))
5317a4a4ad2dSNavdeep Parhar x = 1; /* UDP */
5318c0236bd9SNavdeep Parhar else
5319a4a4ad2dSNavdeep Parhar x = 2;
5320a4a4ad2dSNavdeep Parhar
5321a4a4ad2dSNavdeep Parhar if (m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_IP_TCP | CSUM_IP_UDP |
5322a4a4ad2dSNavdeep Parhar CSUM_INNER_IP | CSUM_INNER_IP_TCP | CSUM_INNER_IP_UDP))
5323a4a4ad2dSNavdeep Parhar y = 0; /* IPv4 */
5324a4a4ad2dSNavdeep Parhar else {
5325a4a4ad2dSNavdeep Parhar MPASS(m->m_pkthdr.csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP |
5326a4a4ad2dSNavdeep Parhar CSUM_INNER_IP6_TCP | CSUM_INNER_IP6_UDP));
5327a4a4ad2dSNavdeep Parhar y = 1; /* IPv6 */
5328a4a4ad2dSNavdeep Parhar }
5329a4a4ad2dSNavdeep Parhar /*
5330a4a4ad2dSNavdeep Parhar * needs_hwcsum returned true earlier so there must be some kind of
5331a4a4ad2dSNavdeep Parhar * checksum to calculate.
5332a4a4ad2dSNavdeep Parhar */
5333a4a4ad2dSNavdeep Parhar csum_type = csum_types[x][y];
5334a4a4ad2dSNavdeep Parhar MPASS(csum_type != 0);
5335a4a4ad2dSNavdeep Parhar if (csum_type == TX_CSUM_IP)
5336a4a4ad2dSNavdeep Parhar ctrl |= F_TXPKT_L4CSUM_DIS;
5337a4a4ad2dSNavdeep Parhar ctrl |= V_TXPKT_CSUM_TYPE(csum_type) | V_TXPKT_IPHDR_LEN(l3hlen);
5338a4a4ad2dSNavdeep Parhar if (chip_id(sc) <= CHELSIO_T5)
5339a4a4ad2dSNavdeep Parhar ctrl |= V_TXPKT_ETHHDR_LEN(l2hlen);
5340a4a4ad2dSNavdeep Parhar else
5341a4a4ad2dSNavdeep Parhar ctrl |= V_T6_TXPKT_ETHHDR_LEN(l2hlen);
5342c0236bd9SNavdeep Parhar
5343c0236bd9SNavdeep Parhar return (ctrl);
5344c0236bd9SNavdeep Parhar }
5345c0236bd9SNavdeep Parhar
5346a4a4ad2dSNavdeep Parhar static inline void *
write_lso_cpl(void * cpl,struct mbuf * m0)5347a4a4ad2dSNavdeep Parhar write_lso_cpl(void *cpl, struct mbuf *m0)
5348a4a4ad2dSNavdeep Parhar {
5349a4a4ad2dSNavdeep Parhar struct cpl_tx_pkt_lso_core *lso;
5350a4a4ad2dSNavdeep Parhar uint32_t ctrl;
5351a4a4ad2dSNavdeep Parhar
5352a4a4ad2dSNavdeep Parhar KASSERT(m0->m_pkthdr.l2hlen > 0 && m0->m_pkthdr.l3hlen > 0 &&
5353a4a4ad2dSNavdeep Parhar m0->m_pkthdr.l4hlen > 0,
5354a4a4ad2dSNavdeep Parhar ("%s: mbuf %p needs TSO but missing header lengths",
5355a4a4ad2dSNavdeep Parhar __func__, m0));
5356a4a4ad2dSNavdeep Parhar
5357a4a4ad2dSNavdeep Parhar ctrl = V_LSO_OPCODE(CPL_TX_PKT_LSO) |
5358a4a4ad2dSNavdeep Parhar F_LSO_FIRST_SLICE | F_LSO_LAST_SLICE |
5359a4a4ad2dSNavdeep Parhar V_LSO_ETHHDR_LEN((m0->m_pkthdr.l2hlen - ETHER_HDR_LEN) >> 2) |
5360a4a4ad2dSNavdeep Parhar V_LSO_IPHDR_LEN(m0->m_pkthdr.l3hlen >> 2) |
5361a4a4ad2dSNavdeep Parhar V_LSO_TCPHDR_LEN(m0->m_pkthdr.l4hlen >> 2);
5362a4a4ad2dSNavdeep Parhar if (m0->m_pkthdr.l3hlen == sizeof(struct ip6_hdr))
5363a4a4ad2dSNavdeep Parhar ctrl |= F_LSO_IPV6;
5364a4a4ad2dSNavdeep Parhar
5365a4a4ad2dSNavdeep Parhar lso = cpl;
5366a4a4ad2dSNavdeep Parhar lso->lso_ctrl = htobe32(ctrl);
5367a4a4ad2dSNavdeep Parhar lso->ipid_ofst = htobe16(0);
5368a4a4ad2dSNavdeep Parhar lso->mss = htobe16(m0->m_pkthdr.tso_segsz);
5369a4a4ad2dSNavdeep Parhar lso->seqno_offset = htobe32(0);
5370a4a4ad2dSNavdeep Parhar lso->len = htobe32(m0->m_pkthdr.len);
5371a4a4ad2dSNavdeep Parhar
5372a4a4ad2dSNavdeep Parhar return (lso + 1);
5373a4a4ad2dSNavdeep Parhar }
5374a4a4ad2dSNavdeep Parhar
5375a4a4ad2dSNavdeep Parhar static void *
write_tnl_lso_cpl(void * cpl,struct mbuf * m0)5376a4a4ad2dSNavdeep Parhar write_tnl_lso_cpl(void *cpl, struct mbuf *m0)
5377a4a4ad2dSNavdeep Parhar {
5378a4a4ad2dSNavdeep Parhar struct cpl_tx_tnl_lso *tnl_lso = cpl;
5379a4a4ad2dSNavdeep Parhar uint32_t ctrl;
5380a4a4ad2dSNavdeep Parhar
5381a4a4ad2dSNavdeep Parhar KASSERT(m0->m_pkthdr.inner_l2hlen > 0 &&
5382a4a4ad2dSNavdeep Parhar m0->m_pkthdr.inner_l3hlen > 0 && m0->m_pkthdr.inner_l4hlen > 0 &&
5383a4a4ad2dSNavdeep Parhar m0->m_pkthdr.inner_l5hlen > 0,
5384a4a4ad2dSNavdeep Parhar ("%s: mbuf %p needs VXLAN_TSO but missing inner header lengths",
5385a4a4ad2dSNavdeep Parhar __func__, m0));
5386a4a4ad2dSNavdeep Parhar KASSERT(m0->m_pkthdr.l2hlen > 0 && m0->m_pkthdr.l3hlen > 0 &&
5387a4a4ad2dSNavdeep Parhar m0->m_pkthdr.l4hlen > 0 && m0->m_pkthdr.l5hlen > 0,
5388a4a4ad2dSNavdeep Parhar ("%s: mbuf %p needs VXLAN_TSO but missing outer header lengths",
5389a4a4ad2dSNavdeep Parhar __func__, m0));
5390a4a4ad2dSNavdeep Parhar
5391a4a4ad2dSNavdeep Parhar /* Outer headers. */
5392a4a4ad2dSNavdeep Parhar ctrl = V_CPL_TX_TNL_LSO_OPCODE(CPL_TX_TNL_LSO) |
5393a4a4ad2dSNavdeep Parhar F_CPL_TX_TNL_LSO_FIRST | F_CPL_TX_TNL_LSO_LAST |
5394a4a4ad2dSNavdeep Parhar V_CPL_TX_TNL_LSO_ETHHDRLENOUT(
5395a4a4ad2dSNavdeep Parhar (m0->m_pkthdr.l2hlen - ETHER_HDR_LEN) >> 2) |
5396a4a4ad2dSNavdeep Parhar V_CPL_TX_TNL_LSO_IPHDRLENOUT(m0->m_pkthdr.l3hlen >> 2) |
5397a4a4ad2dSNavdeep Parhar F_CPL_TX_TNL_LSO_IPLENSETOUT;
5398a4a4ad2dSNavdeep Parhar if (m0->m_pkthdr.l3hlen == sizeof(struct ip6_hdr))
5399a4a4ad2dSNavdeep Parhar ctrl |= F_CPL_TX_TNL_LSO_IPV6OUT;
5400a4a4ad2dSNavdeep Parhar else {
5401a4a4ad2dSNavdeep Parhar ctrl |= F_CPL_TX_TNL_LSO_IPHDRCHKOUT |
5402a4a4ad2dSNavdeep Parhar F_CPL_TX_TNL_LSO_IPIDINCOUT;
5403a4a4ad2dSNavdeep Parhar }
5404a4a4ad2dSNavdeep Parhar tnl_lso->op_to_IpIdSplitOut = htobe32(ctrl);
5405a4a4ad2dSNavdeep Parhar tnl_lso->IpIdOffsetOut = 0;
5406a4a4ad2dSNavdeep Parhar tnl_lso->UdpLenSetOut_to_TnlHdrLen =
5407a4a4ad2dSNavdeep Parhar htobe16(F_CPL_TX_TNL_LSO_UDPCHKCLROUT |
5408a4a4ad2dSNavdeep Parhar F_CPL_TX_TNL_LSO_UDPLENSETOUT |
5409a4a4ad2dSNavdeep Parhar V_CPL_TX_TNL_LSO_TNLHDRLEN(m0->m_pkthdr.l2hlen +
5410a4a4ad2dSNavdeep Parhar m0->m_pkthdr.l3hlen + m0->m_pkthdr.l4hlen +
5411a4a4ad2dSNavdeep Parhar m0->m_pkthdr.l5hlen) |
5412a4a4ad2dSNavdeep Parhar V_CPL_TX_TNL_LSO_TNLTYPE(TX_TNL_TYPE_VXLAN));
5413a4a4ad2dSNavdeep Parhar tnl_lso->r1 = 0;
5414a4a4ad2dSNavdeep Parhar
5415a4a4ad2dSNavdeep Parhar /* Inner headers. */
5416a4a4ad2dSNavdeep Parhar ctrl = V_CPL_TX_TNL_LSO_ETHHDRLEN(
5417a4a4ad2dSNavdeep Parhar (m0->m_pkthdr.inner_l2hlen - ETHER_HDR_LEN) >> 2) |
5418a4a4ad2dSNavdeep Parhar V_CPL_TX_TNL_LSO_IPHDRLEN(m0->m_pkthdr.inner_l3hlen >> 2) |
5419a4a4ad2dSNavdeep Parhar V_CPL_TX_TNL_LSO_TCPHDRLEN(m0->m_pkthdr.inner_l4hlen >> 2);
5420a4a4ad2dSNavdeep Parhar if (m0->m_pkthdr.inner_l3hlen == sizeof(struct ip6_hdr))
5421a4a4ad2dSNavdeep Parhar ctrl |= F_CPL_TX_TNL_LSO_IPV6;
5422a4a4ad2dSNavdeep Parhar tnl_lso->Flow_to_TcpHdrLen = htobe32(ctrl);
5423a4a4ad2dSNavdeep Parhar tnl_lso->IpIdOffset = 0;
5424a4a4ad2dSNavdeep Parhar tnl_lso->IpIdSplit_to_Mss =
5425a4a4ad2dSNavdeep Parhar htobe16(V_CPL_TX_TNL_LSO_MSS(m0->m_pkthdr.tso_segsz));
5426a4a4ad2dSNavdeep Parhar tnl_lso->TCPSeqOffset = 0;
5427a4a4ad2dSNavdeep Parhar tnl_lso->EthLenOffset_Size =
5428a4a4ad2dSNavdeep Parhar htobe32(V_CPL_TX_TNL_LSO_SIZE(m0->m_pkthdr.len));
5429a4a4ad2dSNavdeep Parhar
5430a4a4ad2dSNavdeep Parhar return (tnl_lso + 1);
5431a4a4ad2dSNavdeep Parhar }
5432a4a4ad2dSNavdeep Parhar
5433800535c2SNavdeep Parhar #define VM_TX_L2HDR_LEN 16 /* ethmacdst to vlantci */
5434800535c2SNavdeep Parhar
54357951040fSNavdeep Parhar /*
54366af45170SJohn Baldwin * Write a VM txpkt WR for this packet to the hardware descriptors, update the
54376af45170SJohn Baldwin * software descriptor, and advance the pidx. It is guaranteed that enough
54386af45170SJohn Baldwin * descriptors are available.
54396af45170SJohn Baldwin *
54406af45170SJohn Baldwin * The return value is the # of hardware descriptors used.
54416af45170SJohn Baldwin */
54426af45170SJohn Baldwin static u_int
write_txpkt_vm_wr(struct adapter * sc,struct sge_txq * txq,struct mbuf * m0)5443d735920dSNavdeep Parhar write_txpkt_vm_wr(struct adapter *sc, struct sge_txq *txq, struct mbuf *m0)
54446af45170SJohn Baldwin {
5445d735920dSNavdeep Parhar struct sge_eq *eq;
5446d735920dSNavdeep Parhar struct fw_eth_tx_pkt_vm_wr *wr;
54476af45170SJohn Baldwin struct tx_sdesc *txsd;
54486af45170SJohn Baldwin struct cpl_tx_pkt_core *cpl;
54496af45170SJohn Baldwin uint32_t ctrl; /* used in many unrelated places */
54506af45170SJohn Baldwin uint64_t ctrl1;
545139d5cbdcSNavdeep Parhar int len16, ndesc, pktlen;
54526af45170SJohn Baldwin caddr_t dst;
54536af45170SJohn Baldwin
54546af45170SJohn Baldwin TXQ_LOCK_ASSERT_OWNED(txq);
54556af45170SJohn Baldwin M_ASSERTPKTHDR(m0);
54566af45170SJohn Baldwin
54576af45170SJohn Baldwin len16 = mbuf_len16(m0);
54586af45170SJohn Baldwin pktlen = m0->m_pkthdr.len;
54596af45170SJohn Baldwin ctrl = sizeof(struct cpl_tx_pkt_core);
54606af45170SJohn Baldwin if (needs_tso(m0))
54616af45170SJohn Baldwin ctrl += sizeof(struct cpl_tx_pkt_lso_core);
54620cadedfcSNavdeep Parhar ndesc = tx_len16_to_desc(len16);
54636af45170SJohn Baldwin
54646af45170SJohn Baldwin /* Firmware work request header */
5465d735920dSNavdeep Parhar eq = &txq->eq;
5466d735920dSNavdeep Parhar wr = (void *)&eq->desc[eq->pidx];
54676af45170SJohn Baldwin wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_PKT_VM_WR) |
54686af45170SJohn Baldwin V_FW_ETH_TX_PKT_WR_IMMDLEN(ctrl));
54696af45170SJohn Baldwin
54706af45170SJohn Baldwin ctrl = V_FW_WR_LEN16(len16);
54716af45170SJohn Baldwin wr->equiq_to_len16 = htobe32(ctrl);
54726af45170SJohn Baldwin wr->r3[0] = 0;
54736af45170SJohn Baldwin wr->r3[1] = 0;
54746af45170SJohn Baldwin
54756af45170SJohn Baldwin /*
54766af45170SJohn Baldwin * Copy over ethmacdst, ethmacsrc, ethtype, and vlantci.
54776af45170SJohn Baldwin * vlantci is ignored unless the ethtype is 0x8100, so it's
54786af45170SJohn Baldwin * simpler to always copy it rather than making it
54796af45170SJohn Baldwin * conditional. Also, it seems that we do not have to set
54806af45170SJohn Baldwin * vlantci or fake the ethtype when doing VLAN tag insertion.
54816af45170SJohn Baldwin */
5482800535c2SNavdeep Parhar m_copydata(m0, 0, VM_TX_L2HDR_LEN, wr->ethmacdst);
54836af45170SJohn Baldwin
54846af45170SJohn Baldwin if (needs_tso(m0)) {
5485a4a4ad2dSNavdeep Parhar cpl = write_lso_cpl(wr + 1, m0);
54866af45170SJohn Baldwin txq->tso_wrs++;
5487c0236bd9SNavdeep Parhar } else
54886af45170SJohn Baldwin cpl = (void *)(wr + 1);
54896af45170SJohn Baldwin
54906af45170SJohn Baldwin /* Checksum offload */
5491c0236bd9SNavdeep Parhar ctrl1 = csum_to_ctrl(sc, m0);
5492c0236bd9SNavdeep Parhar if (ctrl1 != (F_TXPKT_IPCSUM_DIS | F_TXPKT_L4CSUM_DIS))
54936af45170SJohn Baldwin txq->txcsum++; /* some hardware assistance provided */
54946af45170SJohn Baldwin
54956af45170SJohn Baldwin /* VLAN tag insertion */
54966af45170SJohn Baldwin if (needs_vlan_insertion(m0)) {
54976af45170SJohn Baldwin ctrl1 |= F_TXPKT_VLAN_VLD |
54986af45170SJohn Baldwin V_TXPKT_VLAN(m0->m_pkthdr.ether_vtag);
54996af45170SJohn Baldwin txq->vlan_insertion++;
55002d0a0127SNavdeep Parhar } else if (sc->vlan_id)
55012d0a0127SNavdeep Parhar ctrl1 |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(sc->vlan_id);
55026af45170SJohn Baldwin
55036af45170SJohn Baldwin /* CPL header */
55046af45170SJohn Baldwin cpl->ctrl0 = txq->cpl_ctrl0;
55056af45170SJohn Baldwin cpl->pack = 0;
55066af45170SJohn Baldwin cpl->len = htobe16(pktlen);
55076af45170SJohn Baldwin cpl->ctrl1 = htobe64(ctrl1);
55086af45170SJohn Baldwin
55096af45170SJohn Baldwin /* SGL */
55106af45170SJohn Baldwin dst = (void *)(cpl + 1);
55116af45170SJohn Baldwin
55126af45170SJohn Baldwin /*
55136af45170SJohn Baldwin * A packet using TSO will use up an entire descriptor for the
55146af45170SJohn Baldwin * firmware work request header, LSO CPL, and TX_PKT_XT CPL.
55156af45170SJohn Baldwin * If this descriptor is the last descriptor in the ring, wrap
55166af45170SJohn Baldwin * around to the front of the ring explicitly for the start of
55176af45170SJohn Baldwin * the sgl.
55186af45170SJohn Baldwin */
55196af45170SJohn Baldwin if (dst == (void *)&eq->desc[eq->sidx]) {
55206af45170SJohn Baldwin dst = (void *)&eq->desc[0];
55216af45170SJohn Baldwin write_gl_to_txd(txq, m0, &dst, 0);
55226af45170SJohn Baldwin } else
55236af45170SJohn Baldwin write_gl_to_txd(txq, m0, &dst, eq->sidx - ndesc < eq->pidx);
55246af45170SJohn Baldwin txq->sgl_wrs++;
55256af45170SJohn Baldwin txq->txpkt_wrs++;
55266af45170SJohn Baldwin
55276af45170SJohn Baldwin txsd = &txq->sdesc[eq->pidx];
55286af45170SJohn Baldwin txsd->m = m0;
55296af45170SJohn Baldwin txsd->desc_used = ndesc;
55306af45170SJohn Baldwin
55316af45170SJohn Baldwin return (ndesc);
55326af45170SJohn Baldwin }
55336af45170SJohn Baldwin
55346af45170SJohn Baldwin /*
55355cdaef71SJohn Baldwin * Write a raw WR to the hardware descriptors, update the software
55365cdaef71SJohn Baldwin * descriptor, and advance the pidx. It is guaranteed that enough
55375cdaef71SJohn Baldwin * descriptors are available.
55385cdaef71SJohn Baldwin *
55395cdaef71SJohn Baldwin * The return value is the # of hardware descriptors used.
55405cdaef71SJohn Baldwin */
55415cdaef71SJohn Baldwin static u_int
write_raw_wr(struct sge_txq * txq,void * wr,struct mbuf * m0,u_int available)55425cdaef71SJohn Baldwin write_raw_wr(struct sge_txq *txq, void *wr, struct mbuf *m0, u_int available)
55435cdaef71SJohn Baldwin {
55445cdaef71SJohn Baldwin struct sge_eq *eq = &txq->eq;
55455cdaef71SJohn Baldwin struct tx_sdesc *txsd;
55465cdaef71SJohn Baldwin struct mbuf *m;
55475cdaef71SJohn Baldwin caddr_t dst;
55485cdaef71SJohn Baldwin int len16, ndesc;
55495cdaef71SJohn Baldwin
55505cdaef71SJohn Baldwin len16 = mbuf_len16(m0);
55510cadedfcSNavdeep Parhar ndesc = tx_len16_to_desc(len16);
55525cdaef71SJohn Baldwin MPASS(ndesc <= available);
55535cdaef71SJohn Baldwin
55545cdaef71SJohn Baldwin dst = wr;
55555cdaef71SJohn Baldwin for (m = m0; m != NULL; m = m->m_next)
55565cdaef71SJohn Baldwin copy_to_txd(eq, mtod(m, caddr_t), &dst, m->m_len);
55575cdaef71SJohn Baldwin
55585cdaef71SJohn Baldwin txq->raw_wrs++;
55595cdaef71SJohn Baldwin
55605cdaef71SJohn Baldwin txsd = &txq->sdesc[eq->pidx];
55615cdaef71SJohn Baldwin txsd->m = m0;
55625cdaef71SJohn Baldwin txsd->desc_used = ndesc;
55635cdaef71SJohn Baldwin
55645cdaef71SJohn Baldwin return (ndesc);
55655cdaef71SJohn Baldwin }
55665cdaef71SJohn Baldwin
55675cdaef71SJohn Baldwin /*
55687951040fSNavdeep Parhar * Write a txpkt WR for this packet to the hardware descriptors, update the
55697951040fSNavdeep Parhar * software descriptor, and advance the pidx. It is guaranteed that enough
55707951040fSNavdeep Parhar * descriptors are available.
557154e4ee71SNavdeep Parhar *
55727951040fSNavdeep Parhar * The return value is the # of hardware descriptors used.
557354e4ee71SNavdeep Parhar */
55747951040fSNavdeep Parhar static u_int
write_txpkt_wr(struct adapter * sc,struct sge_txq * txq,struct mbuf * m0,u_int available)5575d735920dSNavdeep Parhar write_txpkt_wr(struct adapter *sc, struct sge_txq *txq, struct mbuf *m0,
5576d735920dSNavdeep Parhar u_int available)
557754e4ee71SNavdeep Parhar {
5578d735920dSNavdeep Parhar struct sge_eq *eq;
5579d735920dSNavdeep Parhar struct fw_eth_tx_pkt_wr *wr;
55807951040fSNavdeep Parhar struct tx_sdesc *txsd;
558154e4ee71SNavdeep Parhar struct cpl_tx_pkt_core *cpl;
558254e4ee71SNavdeep Parhar uint32_t ctrl; /* used in many unrelated places */
558354e4ee71SNavdeep Parhar uint64_t ctrl1;
55847951040fSNavdeep Parhar int len16, ndesc, pktlen, nsegs;
558554e4ee71SNavdeep Parhar caddr_t dst;
558654e4ee71SNavdeep Parhar
558754e4ee71SNavdeep Parhar TXQ_LOCK_ASSERT_OWNED(txq);
55887951040fSNavdeep Parhar M_ASSERTPKTHDR(m0);
558954e4ee71SNavdeep Parhar
55907951040fSNavdeep Parhar len16 = mbuf_len16(m0);
55917951040fSNavdeep Parhar nsegs = mbuf_nsegs(m0);
55927951040fSNavdeep Parhar pktlen = m0->m_pkthdr.len;
559354e4ee71SNavdeep Parhar ctrl = sizeof(struct cpl_tx_pkt_core);
5594a4a4ad2dSNavdeep Parhar if (needs_tso(m0)) {
5595a4a4ad2dSNavdeep Parhar if (needs_vxlan_tso(m0))
5596a4a4ad2dSNavdeep Parhar ctrl += sizeof(struct cpl_tx_tnl_lso);
5597a4a4ad2dSNavdeep Parhar else
55982a5f6b0eSNavdeep Parhar ctrl += sizeof(struct cpl_tx_pkt_lso_core);
5599a4a4ad2dSNavdeep Parhar } else if (!(mbuf_cflags(m0) & MC_NOMAP) && pktlen <= imm_payload(2) &&
5600d76bbe17SJohn Baldwin available >= 2) {
56017951040fSNavdeep Parhar /* Immediate data. Recalculate len16 and set nsegs to 0. */
5602ecb79ca4SNavdeep Parhar ctrl += pktlen;
56037951040fSNavdeep Parhar len16 = howmany(sizeof(struct fw_eth_tx_pkt_wr) +
56047951040fSNavdeep Parhar sizeof(struct cpl_tx_pkt_core) + pktlen, 16);
56057951040fSNavdeep Parhar nsegs = 0;
560654e4ee71SNavdeep Parhar }
56070cadedfcSNavdeep Parhar ndesc = tx_len16_to_desc(len16);
56087951040fSNavdeep Parhar MPASS(ndesc <= available);
560954e4ee71SNavdeep Parhar
561054e4ee71SNavdeep Parhar /* Firmware work request header */
5611d735920dSNavdeep Parhar eq = &txq->eq;
5612d735920dSNavdeep Parhar wr = (void *)&eq->desc[eq->pidx];
561354e4ee71SNavdeep Parhar wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_PKT_WR) |
5614733b9277SNavdeep Parhar V_FW_ETH_TX_PKT_WR_IMMDLEN(ctrl));
56156b49a4ecSNavdeep Parhar
56167951040fSNavdeep Parhar ctrl = V_FW_WR_LEN16(len16);
561754e4ee71SNavdeep Parhar wr->equiq_to_len16 = htobe32(ctrl);
561854e4ee71SNavdeep Parhar wr->r3 = 0;
561954e4ee71SNavdeep Parhar
56207951040fSNavdeep Parhar if (needs_tso(m0)) {
5621a4a4ad2dSNavdeep Parhar if (needs_vxlan_tso(m0)) {
5622a4a4ad2dSNavdeep Parhar cpl = write_tnl_lso_cpl(wr + 1, m0);
5623a4a4ad2dSNavdeep Parhar txq->vxlan_tso_wrs++;
5624a4a4ad2dSNavdeep Parhar } else {
5625a4a4ad2dSNavdeep Parhar cpl = write_lso_cpl(wr + 1, m0);
562654e4ee71SNavdeep Parhar txq->tso_wrs++;
5627a4a4ad2dSNavdeep Parhar }
562854e4ee71SNavdeep Parhar } else
562954e4ee71SNavdeep Parhar cpl = (void *)(wr + 1);
563054e4ee71SNavdeep Parhar
563154e4ee71SNavdeep Parhar /* Checksum offload */
5632c0236bd9SNavdeep Parhar ctrl1 = csum_to_ctrl(sc, m0);
5633a4a4ad2dSNavdeep Parhar if (ctrl1 != (F_TXPKT_IPCSUM_DIS | F_TXPKT_L4CSUM_DIS)) {
5634a4a4ad2dSNavdeep Parhar /* some hardware assistance provided */
5635a4a4ad2dSNavdeep Parhar if (needs_vxlan_csum(m0))
5636a4a4ad2dSNavdeep Parhar txq->vxlan_txcsum++;
5637a4a4ad2dSNavdeep Parhar else
5638a4a4ad2dSNavdeep Parhar txq->txcsum++;
5639a4a4ad2dSNavdeep Parhar }
564054e4ee71SNavdeep Parhar
564154e4ee71SNavdeep Parhar /* VLAN tag insertion */
56427951040fSNavdeep Parhar if (needs_vlan_insertion(m0)) {
5643a4a4ad2dSNavdeep Parhar ctrl1 |= F_TXPKT_VLAN_VLD |
5644a4a4ad2dSNavdeep Parhar V_TXPKT_VLAN(m0->m_pkthdr.ether_vtag);
564554e4ee71SNavdeep Parhar txq->vlan_insertion++;
564654e4ee71SNavdeep Parhar }
564754e4ee71SNavdeep Parhar
564854e4ee71SNavdeep Parhar /* CPL header */
56497951040fSNavdeep Parhar cpl->ctrl0 = txq->cpl_ctrl0;
565054e4ee71SNavdeep Parhar cpl->pack = 0;
5651ecb79ca4SNavdeep Parhar cpl->len = htobe16(pktlen);
565254e4ee71SNavdeep Parhar cpl->ctrl1 = htobe64(ctrl1);
565354e4ee71SNavdeep Parhar
565454e4ee71SNavdeep Parhar /* SGL */
565554e4ee71SNavdeep Parhar dst = (void *)(cpl + 1);
5656a4a4ad2dSNavdeep Parhar if (__predict_false((uintptr_t)dst == (uintptr_t)&eq->desc[eq->sidx]))
5657a4a4ad2dSNavdeep Parhar dst = (caddr_t)&eq->desc[0];
56587951040fSNavdeep Parhar if (nsegs > 0) {
56597951040fSNavdeep Parhar
56607951040fSNavdeep Parhar write_gl_to_txd(txq, m0, &dst, eq->sidx - ndesc < eq->pidx);
566154e4ee71SNavdeep Parhar txq->sgl_wrs++;
566254e4ee71SNavdeep Parhar } else {
56637951040fSNavdeep Parhar struct mbuf *m;
56647951040fSNavdeep Parhar
56657951040fSNavdeep Parhar for (m = m0; m != NULL; m = m->m_next) {
566654e4ee71SNavdeep Parhar copy_to_txd(eq, mtod(m, caddr_t), &dst, m->m_len);
5667ecb79ca4SNavdeep Parhar #ifdef INVARIANTS
5668ecb79ca4SNavdeep Parhar pktlen -= m->m_len;
5669ecb79ca4SNavdeep Parhar #endif
567054e4ee71SNavdeep Parhar }
5671ecb79ca4SNavdeep Parhar #ifdef INVARIANTS
5672ecb79ca4SNavdeep Parhar KASSERT(pktlen == 0, ("%s: %d bytes left.", __func__, pktlen));
5673ecb79ca4SNavdeep Parhar #endif
56747951040fSNavdeep Parhar txq->imm_wrs++;
567554e4ee71SNavdeep Parhar }
567654e4ee71SNavdeep Parhar
567754e4ee71SNavdeep Parhar txq->txpkt_wrs++;
567854e4ee71SNavdeep Parhar
5679f7dfe243SNavdeep Parhar txsd = &txq->sdesc[eq->pidx];
56807951040fSNavdeep Parhar txsd->m = m0;
568154e4ee71SNavdeep Parhar txsd->desc_used = ndesc;
568254e4ee71SNavdeep Parhar
56837951040fSNavdeep Parhar return (ndesc);
568454e4ee71SNavdeep Parhar }
568554e4ee71SNavdeep Parhar
5686d735920dSNavdeep Parhar static inline bool
cmp_l2hdr(struct txpkts * txp,struct mbuf * m)5687d735920dSNavdeep Parhar cmp_l2hdr(struct txpkts *txp, struct mbuf *m)
568854e4ee71SNavdeep Parhar {
5689d735920dSNavdeep Parhar int len;
56907951040fSNavdeep Parhar
5691d735920dSNavdeep Parhar MPASS(txp->npkt > 0);
5692800535c2SNavdeep Parhar MPASS(m->m_len >= VM_TX_L2HDR_LEN);
56937951040fSNavdeep Parhar
5694d735920dSNavdeep Parhar if (txp->ethtype == be16toh(ETHERTYPE_VLAN))
5695800535c2SNavdeep Parhar len = VM_TX_L2HDR_LEN;
5696d735920dSNavdeep Parhar else
5697d735920dSNavdeep Parhar len = sizeof(struct ether_header);
5698d735920dSNavdeep Parhar
5699d735920dSNavdeep Parhar return (memcmp(m->m_data, &txp->ethmacdst[0], len) != 0);
57007951040fSNavdeep Parhar }
57017951040fSNavdeep Parhar
5702d735920dSNavdeep Parhar static inline void
save_l2hdr(struct txpkts * txp,struct mbuf * m)5703d735920dSNavdeep Parhar save_l2hdr(struct txpkts *txp, struct mbuf *m)
5704d735920dSNavdeep Parhar {
5705800535c2SNavdeep Parhar MPASS(m->m_len >= VM_TX_L2HDR_LEN);
57067951040fSNavdeep Parhar
5707800535c2SNavdeep Parhar memcpy(&txp->ethmacdst[0], mtod(m, const void *), VM_TX_L2HDR_LEN);
5708d735920dSNavdeep Parhar }
57097951040fSNavdeep Parhar
5710d735920dSNavdeep Parhar static int
add_to_txpkts_vf(struct adapter * sc,struct sge_txq * txq,struct mbuf * m,int avail,bool * send)5711d735920dSNavdeep Parhar add_to_txpkts_vf(struct adapter *sc, struct sge_txq *txq, struct mbuf *m,
5712d735920dSNavdeep Parhar int avail, bool *send)
5713d735920dSNavdeep Parhar {
5714d735920dSNavdeep Parhar struct txpkts *txp = &txq->txp;
5715d735920dSNavdeep Parhar
5716d735920dSNavdeep Parhar /* Cannot have TSO and coalesce at the same time. */
5717d735920dSNavdeep Parhar if (cannot_use_txpkts(m)) {
5718d735920dSNavdeep Parhar cannot_coalesce:
5719d735920dSNavdeep Parhar *send = txp->npkt > 0;
5720d735920dSNavdeep Parhar return (EINVAL);
5721d735920dSNavdeep Parhar }
5722d735920dSNavdeep Parhar
5723d735920dSNavdeep Parhar /* VF allows coalescing of type 1 (1 GL) only */
5724d735920dSNavdeep Parhar if (mbuf_nsegs(m) > 1)
5725d735920dSNavdeep Parhar goto cannot_coalesce;
5726d735920dSNavdeep Parhar
5727d735920dSNavdeep Parhar *send = false;
5728d735920dSNavdeep Parhar if (txp->npkt > 0) {
5729d735920dSNavdeep Parhar MPASS(tx_len16_to_desc(txp->len16) <= avail);
5730d735920dSNavdeep Parhar MPASS(txp->npkt < txp->max_npkt);
5731d735920dSNavdeep Parhar MPASS(txp->wr_type == 1); /* VF supports type 1 only */
5732d735920dSNavdeep Parhar
5733d735920dSNavdeep Parhar if (tx_len16_to_desc(txp->len16 + txpkts1_len16()) > avail) {
5734d735920dSNavdeep Parhar retry_after_send:
5735d735920dSNavdeep Parhar *send = true;
5736d735920dSNavdeep Parhar return (EAGAIN);
5737d735920dSNavdeep Parhar }
5738d735920dSNavdeep Parhar if (m->m_pkthdr.len + txp->plen > 65535)
5739d735920dSNavdeep Parhar goto retry_after_send;
5740d735920dSNavdeep Parhar if (cmp_l2hdr(txp, m))
5741d735920dSNavdeep Parhar goto retry_after_send;
5742d735920dSNavdeep Parhar
5743d735920dSNavdeep Parhar txp->len16 += txpkts1_len16();
5744d735920dSNavdeep Parhar txp->plen += m->m_pkthdr.len;
5745d735920dSNavdeep Parhar txp->mb[txp->npkt++] = m;
5746d735920dSNavdeep Parhar if (txp->npkt == txp->max_npkt)
5747d735920dSNavdeep Parhar *send = true;
5748d735920dSNavdeep Parhar } else {
5749d735920dSNavdeep Parhar txp->len16 = howmany(sizeof(struct fw_eth_tx_pkts_vm_wr), 16) +
5750d735920dSNavdeep Parhar txpkts1_len16();
5751d735920dSNavdeep Parhar if (tx_len16_to_desc(txp->len16) > avail)
5752d735920dSNavdeep Parhar goto cannot_coalesce;
5753d735920dSNavdeep Parhar txp->npkt = 1;
5754d735920dSNavdeep Parhar txp->wr_type = 1;
5755d735920dSNavdeep Parhar txp->plen = m->m_pkthdr.len;
5756d735920dSNavdeep Parhar txp->mb[0] = m;
5757d735920dSNavdeep Parhar save_l2hdr(txp, m);
5758d735920dSNavdeep Parhar }
57597951040fSNavdeep Parhar return (0);
57607951040fSNavdeep Parhar }
57617951040fSNavdeep Parhar
57627951040fSNavdeep Parhar static int
add_to_txpkts_pf(struct adapter * sc,struct sge_txq * txq,struct mbuf * m,int avail,bool * send)5763d735920dSNavdeep Parhar add_to_txpkts_pf(struct adapter *sc, struct sge_txq *txq, struct mbuf *m,
5764d735920dSNavdeep Parhar int avail, bool *send)
57657951040fSNavdeep Parhar {
5766d735920dSNavdeep Parhar struct txpkts *txp = &txq->txp;
5767d735920dSNavdeep Parhar int nsegs;
5768d735920dSNavdeep Parhar
5769d735920dSNavdeep Parhar MPASS(!(sc->flags & IS_VF));
5770d735920dSNavdeep Parhar
5771d735920dSNavdeep Parhar /* Cannot have TSO and coalesce at the same time. */
5772d735920dSNavdeep Parhar if (cannot_use_txpkts(m)) {
5773d735920dSNavdeep Parhar cannot_coalesce:
5774d735920dSNavdeep Parhar *send = txp->npkt > 0;
5775d735920dSNavdeep Parhar return (EINVAL);
5776d735920dSNavdeep Parhar }
5777d735920dSNavdeep Parhar
5778d735920dSNavdeep Parhar *send = false;
5779d735920dSNavdeep Parhar nsegs = mbuf_nsegs(m);
5780d735920dSNavdeep Parhar if (txp->npkt == 0) {
5781d735920dSNavdeep Parhar if (m->m_pkthdr.len > 65535)
5782d735920dSNavdeep Parhar goto cannot_coalesce;
5783d735920dSNavdeep Parhar if (nsegs > 1) {
5784d735920dSNavdeep Parhar txp->wr_type = 0;
5785d735920dSNavdeep Parhar txp->len16 =
5786d735920dSNavdeep Parhar howmany(sizeof(struct fw_eth_tx_pkts_wr), 16) +
5787d735920dSNavdeep Parhar txpkts0_len16(nsegs);
5788d735920dSNavdeep Parhar } else {
5789d735920dSNavdeep Parhar txp->wr_type = 1;
5790d735920dSNavdeep Parhar txp->len16 =
5791d735920dSNavdeep Parhar howmany(sizeof(struct fw_eth_tx_pkts_wr), 16) +
5792d735920dSNavdeep Parhar txpkts1_len16();
5793d735920dSNavdeep Parhar }
5794d735920dSNavdeep Parhar if (tx_len16_to_desc(txp->len16) > avail)
5795d735920dSNavdeep Parhar goto cannot_coalesce;
5796d735920dSNavdeep Parhar txp->npkt = 1;
5797d735920dSNavdeep Parhar txp->plen = m->m_pkthdr.len;
5798d735920dSNavdeep Parhar txp->mb[0] = m;
5799d735920dSNavdeep Parhar } else {
5800d735920dSNavdeep Parhar MPASS(tx_len16_to_desc(txp->len16) <= avail);
5801d735920dSNavdeep Parhar MPASS(txp->npkt < txp->max_npkt);
5802d735920dSNavdeep Parhar
5803d735920dSNavdeep Parhar if (m->m_pkthdr.len + txp->plen > 65535) {
5804d735920dSNavdeep Parhar retry_after_send:
5805d735920dSNavdeep Parhar *send = true;
5806d735920dSNavdeep Parhar return (EAGAIN);
5807d735920dSNavdeep Parhar }
58087951040fSNavdeep Parhar
58097951040fSNavdeep Parhar MPASS(txp->wr_type == 0 || txp->wr_type == 1);
5810d735920dSNavdeep Parhar if (txp->wr_type == 0) {
5811d735920dSNavdeep Parhar if (tx_len16_to_desc(txp->len16 +
5812d735920dSNavdeep Parhar txpkts0_len16(nsegs)) > min(avail, SGE_MAX_WR_NDESC))
5813d735920dSNavdeep Parhar goto retry_after_send;
5814d735920dSNavdeep Parhar txp->len16 += txpkts0_len16(nsegs);
5815d735920dSNavdeep Parhar } else {
5816d735920dSNavdeep Parhar if (nsegs != 1)
5817d735920dSNavdeep Parhar goto retry_after_send;
5818d735920dSNavdeep Parhar if (tx_len16_to_desc(txp->len16 + txpkts1_len16()) >
5819d735920dSNavdeep Parhar avail)
5820d735920dSNavdeep Parhar goto retry_after_send;
5821d735920dSNavdeep Parhar txp->len16 += txpkts1_len16();
5822d735920dSNavdeep Parhar }
58237951040fSNavdeep Parhar
5824d735920dSNavdeep Parhar txp->plen += m->m_pkthdr.len;
5825d735920dSNavdeep Parhar txp->mb[txp->npkt++] = m;
5826d735920dSNavdeep Parhar if (txp->npkt == txp->max_npkt)
5827d735920dSNavdeep Parhar *send = true;
5828d735920dSNavdeep Parhar }
58297951040fSNavdeep Parhar return (0);
58307951040fSNavdeep Parhar }
58317951040fSNavdeep Parhar
58327951040fSNavdeep Parhar /*
58337951040fSNavdeep Parhar * Write a txpkts WR for the packets in txp to the hardware descriptors, update
58347951040fSNavdeep Parhar * the software descriptor, and advance the pidx. It is guaranteed that enough
58357951040fSNavdeep Parhar * descriptors are available.
58367951040fSNavdeep Parhar *
58377951040fSNavdeep Parhar * The return value is the # of hardware descriptors used.
58387951040fSNavdeep Parhar */
58397951040fSNavdeep Parhar static u_int
write_txpkts_wr(struct adapter * sc,struct sge_txq * txq)5840d735920dSNavdeep Parhar write_txpkts_wr(struct adapter *sc, struct sge_txq *txq)
58417951040fSNavdeep Parhar {
5842d735920dSNavdeep Parhar const struct txpkts *txp = &txq->txp;
58437951040fSNavdeep Parhar struct sge_eq *eq = &txq->eq;
5844d735920dSNavdeep Parhar struct fw_eth_tx_pkts_wr *wr;
58457951040fSNavdeep Parhar struct tx_sdesc *txsd;
58467951040fSNavdeep Parhar struct cpl_tx_pkt_core *cpl;
58477951040fSNavdeep Parhar uint64_t ctrl1;
5848d735920dSNavdeep Parhar int ndesc, i, checkwrap;
5849d735920dSNavdeep Parhar struct mbuf *m, *last;
58507951040fSNavdeep Parhar void *flitp;
58517951040fSNavdeep Parhar
58527951040fSNavdeep Parhar TXQ_LOCK_ASSERT_OWNED(txq);
58537951040fSNavdeep Parhar MPASS(txp->npkt > 0);
58547951040fSNavdeep Parhar MPASS(txp->len16 <= howmany(SGE_MAX_WR_LEN, 16));
58557951040fSNavdeep Parhar
5856d735920dSNavdeep Parhar wr = (void *)&eq->desc[eq->pidx];
58577951040fSNavdeep Parhar wr->op_pkd = htobe32(V_FW_WR_OP(FW_ETH_TX_PKTS_WR));
5858d735920dSNavdeep Parhar wr->equiq_to_len16 = htobe32(V_FW_WR_LEN16(txp->len16));
58597951040fSNavdeep Parhar wr->plen = htobe16(txp->plen);
58607951040fSNavdeep Parhar wr->npkt = txp->npkt;
58617951040fSNavdeep Parhar wr->r3 = 0;
58627951040fSNavdeep Parhar wr->type = txp->wr_type;
58637951040fSNavdeep Parhar flitp = wr + 1;
58647951040fSNavdeep Parhar
58657951040fSNavdeep Parhar /*
58667951040fSNavdeep Parhar * At this point we are 16B into a hardware descriptor. If checkwrap is
58677951040fSNavdeep Parhar * set then we know the WR is going to wrap around somewhere. We'll
58687951040fSNavdeep Parhar * check for that at appropriate points.
58697951040fSNavdeep Parhar */
5870d735920dSNavdeep Parhar ndesc = tx_len16_to_desc(txp->len16);
5871d735920dSNavdeep Parhar last = NULL;
58727951040fSNavdeep Parhar checkwrap = eq->sidx - ndesc < eq->pidx;
5873d735920dSNavdeep Parhar for (i = 0; i < txp->npkt; i++) {
5874d735920dSNavdeep Parhar m = txp->mb[i];
58757951040fSNavdeep Parhar if (txp->wr_type == 0) {
587654e4ee71SNavdeep Parhar struct ulp_txpkt *ulpmc;
587754e4ee71SNavdeep Parhar struct ulptx_idata *ulpsc;
587854e4ee71SNavdeep Parhar
58797951040fSNavdeep Parhar /* ULP master command */
58807951040fSNavdeep Parhar ulpmc = flitp;
58817951040fSNavdeep Parhar ulpmc->cmd_dest = htobe32(V_ULPTX_CMD(ULP_TX_PKT) |
58827951040fSNavdeep Parhar V_ULP_TXPKT_DEST(0) | V_ULP_TXPKT_FID(eq->iqid));
5883d735920dSNavdeep Parhar ulpmc->len = htobe32(txpkts0_len16(mbuf_nsegs(m)));
588454e4ee71SNavdeep Parhar
58857951040fSNavdeep Parhar /* ULP subcommand */
58867951040fSNavdeep Parhar ulpsc = (void *)(ulpmc + 1);
58877951040fSNavdeep Parhar ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM) |
58887951040fSNavdeep Parhar F_ULP_TX_SC_MORE);
58897951040fSNavdeep Parhar ulpsc->len = htobe32(sizeof(struct cpl_tx_pkt_core));
58907951040fSNavdeep Parhar
58917951040fSNavdeep Parhar cpl = (void *)(ulpsc + 1);
58927951040fSNavdeep Parhar if (checkwrap &&
58937951040fSNavdeep Parhar (uintptr_t)cpl == (uintptr_t)&eq->desc[eq->sidx])
58947951040fSNavdeep Parhar cpl = (void *)&eq->desc[0];
58957951040fSNavdeep Parhar } else {
58967951040fSNavdeep Parhar cpl = flitp;
58977951040fSNavdeep Parhar }
589854e4ee71SNavdeep Parhar
589954e4ee71SNavdeep Parhar /* Checksum offload */
5900c0236bd9SNavdeep Parhar ctrl1 = csum_to_ctrl(sc, m);
5901a4a4ad2dSNavdeep Parhar if (ctrl1 != (F_TXPKT_IPCSUM_DIS | F_TXPKT_L4CSUM_DIS)) {
5902a4a4ad2dSNavdeep Parhar /* some hardware assistance provided */
5903a4a4ad2dSNavdeep Parhar if (needs_vxlan_csum(m))
5904a4a4ad2dSNavdeep Parhar txq->vxlan_txcsum++;
5905a4a4ad2dSNavdeep Parhar else
5906a4a4ad2dSNavdeep Parhar txq->txcsum++;
5907a4a4ad2dSNavdeep Parhar }
590854e4ee71SNavdeep Parhar
590954e4ee71SNavdeep Parhar /* VLAN tag insertion */
59107951040fSNavdeep Parhar if (needs_vlan_insertion(m)) {
59117951040fSNavdeep Parhar ctrl1 |= F_TXPKT_VLAN_VLD |
59127951040fSNavdeep Parhar V_TXPKT_VLAN(m->m_pkthdr.ether_vtag);
591354e4ee71SNavdeep Parhar txq->vlan_insertion++;
591454e4ee71SNavdeep Parhar }
591554e4ee71SNavdeep Parhar
59167951040fSNavdeep Parhar /* CPL header */
59177951040fSNavdeep Parhar cpl->ctrl0 = txq->cpl_ctrl0;
591854e4ee71SNavdeep Parhar cpl->pack = 0;
591954e4ee71SNavdeep Parhar cpl->len = htobe16(m->m_pkthdr.len);
59207951040fSNavdeep Parhar cpl->ctrl1 = htobe64(ctrl1);
592154e4ee71SNavdeep Parhar
59227951040fSNavdeep Parhar flitp = cpl + 1;
59237951040fSNavdeep Parhar if (checkwrap &&
59247951040fSNavdeep Parhar (uintptr_t)flitp == (uintptr_t)&eq->desc[eq->sidx])
59257951040fSNavdeep Parhar flitp = (void *)&eq->desc[0];
592654e4ee71SNavdeep Parhar
59277951040fSNavdeep Parhar write_gl_to_txd(txq, m, (caddr_t *)(&flitp), checkwrap);
592854e4ee71SNavdeep Parhar
5929d735920dSNavdeep Parhar if (last != NULL)
5930d735920dSNavdeep Parhar last->m_nextpkt = m;
5931d735920dSNavdeep Parhar last = m;
59327951040fSNavdeep Parhar }
59337951040fSNavdeep Parhar
5934d735920dSNavdeep Parhar txq->sgl_wrs++;
5935a59a1477SNavdeep Parhar if (txp->wr_type == 0) {
5936a59a1477SNavdeep Parhar txq->txpkts0_pkts += txp->npkt;
5937a59a1477SNavdeep Parhar txq->txpkts0_wrs++;
5938a59a1477SNavdeep Parhar } else {
5939a59a1477SNavdeep Parhar txq->txpkts1_pkts += txp->npkt;
5940a59a1477SNavdeep Parhar txq->txpkts1_wrs++;
5941a59a1477SNavdeep Parhar }
5942a59a1477SNavdeep Parhar
59437951040fSNavdeep Parhar txsd = &txq->sdesc[eq->pidx];
5944d735920dSNavdeep Parhar txsd->m = txp->mb[0];
5945d735920dSNavdeep Parhar txsd->desc_used = ndesc;
5946d735920dSNavdeep Parhar
5947d735920dSNavdeep Parhar return (ndesc);
5948d735920dSNavdeep Parhar }
5949d735920dSNavdeep Parhar
5950d735920dSNavdeep Parhar static u_int
write_txpkts_vm_wr(struct adapter * sc,struct sge_txq * txq)5951d735920dSNavdeep Parhar write_txpkts_vm_wr(struct adapter *sc, struct sge_txq *txq)
5952d735920dSNavdeep Parhar {
5953d735920dSNavdeep Parhar const struct txpkts *txp = &txq->txp;
5954d735920dSNavdeep Parhar struct sge_eq *eq = &txq->eq;
5955d735920dSNavdeep Parhar struct fw_eth_tx_pkts_vm_wr *wr;
5956d735920dSNavdeep Parhar struct tx_sdesc *txsd;
5957d735920dSNavdeep Parhar struct cpl_tx_pkt_core *cpl;
5958d735920dSNavdeep Parhar uint64_t ctrl1;
5959d735920dSNavdeep Parhar int ndesc, i;
5960d735920dSNavdeep Parhar struct mbuf *m, *last;
5961d735920dSNavdeep Parhar void *flitp;
5962d735920dSNavdeep Parhar
5963d735920dSNavdeep Parhar TXQ_LOCK_ASSERT_OWNED(txq);
5964d735920dSNavdeep Parhar MPASS(txp->npkt > 0);
5965d735920dSNavdeep Parhar MPASS(txp->wr_type == 1); /* VF supports type 1 only */
5966d735920dSNavdeep Parhar MPASS(txp->mb[0] != NULL);
5967d735920dSNavdeep Parhar MPASS(txp->len16 <= howmany(SGE_MAX_WR_LEN, 16));
5968d735920dSNavdeep Parhar
5969d735920dSNavdeep Parhar wr = (void *)&eq->desc[eq->pidx];
5970d735920dSNavdeep Parhar wr->op_pkd = htobe32(V_FW_WR_OP(FW_ETH_TX_PKTS_VM_WR));
5971d735920dSNavdeep Parhar wr->equiq_to_len16 = htobe32(V_FW_WR_LEN16(txp->len16));
5972d735920dSNavdeep Parhar wr->r3 = 0;
5973d735920dSNavdeep Parhar wr->plen = htobe16(txp->plen);
5974d735920dSNavdeep Parhar wr->npkt = txp->npkt;
5975d735920dSNavdeep Parhar wr->r4 = 0;
5976d735920dSNavdeep Parhar memcpy(&wr->ethmacdst[0], &txp->ethmacdst[0], 16);
5977d735920dSNavdeep Parhar flitp = wr + 1;
5978d735920dSNavdeep Parhar
5979d735920dSNavdeep Parhar /*
5980d735920dSNavdeep Parhar * At this point we are 32B into a hardware descriptor. Each mbuf in
5981d735920dSNavdeep Parhar * the WR will take 32B so we check for the end of the descriptor ring
5982d735920dSNavdeep Parhar * before writing odd mbufs (mb[1], 3, 5, ..)
5983d735920dSNavdeep Parhar */
5984d735920dSNavdeep Parhar ndesc = tx_len16_to_desc(txp->len16);
5985d735920dSNavdeep Parhar last = NULL;
5986d735920dSNavdeep Parhar for (i = 0; i < txp->npkt; i++) {
5987d735920dSNavdeep Parhar m = txp->mb[i];
5988d735920dSNavdeep Parhar if (i & 1 && (uintptr_t)flitp == (uintptr_t)&eq->desc[eq->sidx])
5989d735920dSNavdeep Parhar flitp = &eq->desc[0];
5990d735920dSNavdeep Parhar cpl = flitp;
5991d735920dSNavdeep Parhar
5992d735920dSNavdeep Parhar /* Checksum offload */
5993d735920dSNavdeep Parhar ctrl1 = csum_to_ctrl(sc, m);
5994d735920dSNavdeep Parhar if (ctrl1 != (F_TXPKT_IPCSUM_DIS | F_TXPKT_L4CSUM_DIS))
5995d735920dSNavdeep Parhar txq->txcsum++; /* some hardware assistance provided */
5996d735920dSNavdeep Parhar
5997d735920dSNavdeep Parhar /* VLAN tag insertion */
5998d735920dSNavdeep Parhar if (needs_vlan_insertion(m)) {
5999d735920dSNavdeep Parhar ctrl1 |= F_TXPKT_VLAN_VLD |
6000d735920dSNavdeep Parhar V_TXPKT_VLAN(m->m_pkthdr.ether_vtag);
6001d735920dSNavdeep Parhar txq->vlan_insertion++;
60022d0a0127SNavdeep Parhar } else if (sc->vlan_id)
60032d0a0127SNavdeep Parhar ctrl1 |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(sc->vlan_id);
6004d735920dSNavdeep Parhar
6005d735920dSNavdeep Parhar /* CPL header */
6006d735920dSNavdeep Parhar cpl->ctrl0 = txq->cpl_ctrl0;
6007d735920dSNavdeep Parhar cpl->pack = 0;
6008d735920dSNavdeep Parhar cpl->len = htobe16(m->m_pkthdr.len);
6009d735920dSNavdeep Parhar cpl->ctrl1 = htobe64(ctrl1);
6010d735920dSNavdeep Parhar
6011d735920dSNavdeep Parhar flitp = cpl + 1;
6012d735920dSNavdeep Parhar MPASS(mbuf_nsegs(m) == 1);
6013d735920dSNavdeep Parhar write_gl_to_txd(txq, m, (caddr_t *)(&flitp), 0);
6014d735920dSNavdeep Parhar
6015d735920dSNavdeep Parhar if (last != NULL)
6016d735920dSNavdeep Parhar last->m_nextpkt = m;
6017d735920dSNavdeep Parhar last = m;
6018d735920dSNavdeep Parhar }
6019d735920dSNavdeep Parhar
6020d735920dSNavdeep Parhar txq->sgl_wrs++;
6021d735920dSNavdeep Parhar txq->txpkts1_pkts += txp->npkt;
6022d735920dSNavdeep Parhar txq->txpkts1_wrs++;
6023d735920dSNavdeep Parhar
6024d735920dSNavdeep Parhar txsd = &txq->sdesc[eq->pidx];
6025d735920dSNavdeep Parhar txsd->m = txp->mb[0];
60267951040fSNavdeep Parhar txsd->desc_used = ndesc;
60277951040fSNavdeep Parhar
60287951040fSNavdeep Parhar return (ndesc);
602954e4ee71SNavdeep Parhar }
603054e4ee71SNavdeep Parhar
603154e4ee71SNavdeep Parhar /*
603254e4ee71SNavdeep Parhar * If the SGL ends on an address that is not 16 byte aligned, this function will
60337951040fSNavdeep Parhar * add a 0 filled flit at the end.
603454e4ee71SNavdeep Parhar */
60357951040fSNavdeep Parhar static void
write_gl_to_txd(struct sge_txq * txq,struct mbuf * m,caddr_t * to,int checkwrap)60367951040fSNavdeep Parhar write_gl_to_txd(struct sge_txq *txq, struct mbuf *m, caddr_t *to, int checkwrap)
603754e4ee71SNavdeep Parhar {
60387951040fSNavdeep Parhar struct sge_eq *eq = &txq->eq;
60397951040fSNavdeep Parhar struct sglist *gl = txq->gl;
60407951040fSNavdeep Parhar struct sglist_seg *seg;
60417951040fSNavdeep Parhar __be64 *flitp, *wrap;
604254e4ee71SNavdeep Parhar struct ulptx_sgl *usgl;
60437951040fSNavdeep Parhar int i, nflits, nsegs;
604454e4ee71SNavdeep Parhar
604554e4ee71SNavdeep Parhar KASSERT(((uintptr_t)(*to) & 0xf) == 0,
604654e4ee71SNavdeep Parhar ("%s: SGL must start at a 16 byte boundary: %p", __func__, *to));
60477951040fSNavdeep Parhar MPASS((uintptr_t)(*to) >= (uintptr_t)&eq->desc[0]);
60487951040fSNavdeep Parhar MPASS((uintptr_t)(*to) < (uintptr_t)&eq->desc[eq->sidx]);
604954e4ee71SNavdeep Parhar
60507951040fSNavdeep Parhar get_pkt_gl(m, gl);
60517951040fSNavdeep Parhar nsegs = gl->sg_nseg;
60527951040fSNavdeep Parhar MPASS(nsegs > 0);
60537951040fSNavdeep Parhar
60547951040fSNavdeep Parhar nflits = (3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1) + 2;
605554e4ee71SNavdeep Parhar flitp = (__be64 *)(*to);
60567951040fSNavdeep Parhar wrap = (__be64 *)(&eq->desc[eq->sidx]);
60577951040fSNavdeep Parhar seg = &gl->sg_segs[0];
605854e4ee71SNavdeep Parhar usgl = (void *)flitp;
605954e4ee71SNavdeep Parhar
606054e4ee71SNavdeep Parhar /*
606154e4ee71SNavdeep Parhar * We start at a 16 byte boundary somewhere inside the tx descriptor
606254e4ee71SNavdeep Parhar * ring, so we're at least 16 bytes away from the status page. There is
606354e4ee71SNavdeep Parhar * no chance of a wrap around in the middle of usgl (which is 16 bytes).
606454e4ee71SNavdeep Parhar */
606554e4ee71SNavdeep Parhar
606654e4ee71SNavdeep Parhar usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
60677951040fSNavdeep Parhar V_ULPTX_NSGE(nsegs));
60687951040fSNavdeep Parhar usgl->len0 = htobe32(seg->ss_len);
60697951040fSNavdeep Parhar usgl->addr0 = htobe64(seg->ss_paddr);
607054e4ee71SNavdeep Parhar seg++;
607154e4ee71SNavdeep Parhar
60727951040fSNavdeep Parhar if (checkwrap == 0 || (uintptr_t)(flitp + nflits) <= (uintptr_t)wrap) {
607354e4ee71SNavdeep Parhar
607454e4ee71SNavdeep Parhar /* Won't wrap around at all */
607554e4ee71SNavdeep Parhar
60767951040fSNavdeep Parhar for (i = 0; i < nsegs - 1; i++, seg++) {
60777951040fSNavdeep Parhar usgl->sge[i / 2].len[i & 1] = htobe32(seg->ss_len);
60787951040fSNavdeep Parhar usgl->sge[i / 2].addr[i & 1] = htobe64(seg->ss_paddr);
607954e4ee71SNavdeep Parhar }
608054e4ee71SNavdeep Parhar if (i & 1)
608154e4ee71SNavdeep Parhar usgl->sge[i / 2].len[1] = htobe32(0);
60827951040fSNavdeep Parhar flitp += nflits;
608354e4ee71SNavdeep Parhar } else {
608454e4ee71SNavdeep Parhar
608554e4ee71SNavdeep Parhar /* Will wrap somewhere in the rest of the SGL */
608654e4ee71SNavdeep Parhar
608754e4ee71SNavdeep Parhar /* 2 flits already written, write the rest flit by flit */
608854e4ee71SNavdeep Parhar flitp = (void *)(usgl + 1);
60897951040fSNavdeep Parhar for (i = 0; i < nflits - 2; i++) {
60907951040fSNavdeep Parhar if (flitp == wrap)
609154e4ee71SNavdeep Parhar flitp = (void *)eq->desc;
60927951040fSNavdeep Parhar *flitp++ = get_flit(seg, nsegs - 1, i);
609354e4ee71SNavdeep Parhar }
609454e4ee71SNavdeep Parhar }
609554e4ee71SNavdeep Parhar
60967951040fSNavdeep Parhar if (nflits & 1) {
60977951040fSNavdeep Parhar MPASS(((uintptr_t)flitp) & 0xf);
60987951040fSNavdeep Parhar *flitp++ = 0;
60997951040fSNavdeep Parhar }
610054e4ee71SNavdeep Parhar
61017951040fSNavdeep Parhar MPASS((((uintptr_t)flitp) & 0xf) == 0);
61027951040fSNavdeep Parhar if (__predict_false(flitp == wrap))
610354e4ee71SNavdeep Parhar *to = (void *)eq->desc;
610454e4ee71SNavdeep Parhar else
61057951040fSNavdeep Parhar *to = (void *)flitp;
610654e4ee71SNavdeep Parhar }
610754e4ee71SNavdeep Parhar
610854e4ee71SNavdeep Parhar static inline void
copy_to_txd(struct sge_eq * eq,caddr_t from,caddr_t * to,int len)610954e4ee71SNavdeep Parhar copy_to_txd(struct sge_eq *eq, caddr_t from, caddr_t *to, int len)
611054e4ee71SNavdeep Parhar {
61117951040fSNavdeep Parhar
61127951040fSNavdeep Parhar MPASS((uintptr_t)(*to) >= (uintptr_t)&eq->desc[0]);
61137951040fSNavdeep Parhar MPASS((uintptr_t)(*to) < (uintptr_t)&eq->desc[eq->sidx]);
61147951040fSNavdeep Parhar
61157951040fSNavdeep Parhar if (__predict_true((uintptr_t)(*to) + len <=
61167951040fSNavdeep Parhar (uintptr_t)&eq->desc[eq->sidx])) {
611754e4ee71SNavdeep Parhar bcopy(from, *to, len);
611854e4ee71SNavdeep Parhar (*to) += len;
611954e4ee71SNavdeep Parhar } else {
61207951040fSNavdeep Parhar int portion = (uintptr_t)&eq->desc[eq->sidx] - (uintptr_t)(*to);
612154e4ee71SNavdeep Parhar
612254e4ee71SNavdeep Parhar bcopy(from, *to, portion);
612354e4ee71SNavdeep Parhar from += portion;
612454e4ee71SNavdeep Parhar portion = len - portion; /* remaining */
612554e4ee71SNavdeep Parhar bcopy(from, (void *)eq->desc, portion);
612654e4ee71SNavdeep Parhar (*to) = (caddr_t)eq->desc + portion;
612754e4ee71SNavdeep Parhar }
612854e4ee71SNavdeep Parhar }
612954e4ee71SNavdeep Parhar
613054e4ee71SNavdeep Parhar static inline void
ring_eq_db(struct adapter * sc,struct sge_eq * eq,u_int n)61317951040fSNavdeep Parhar ring_eq_db(struct adapter *sc, struct sge_eq *eq, u_int n)
613254e4ee71SNavdeep Parhar {
61337951040fSNavdeep Parhar u_int db;
61347951040fSNavdeep Parhar
61357951040fSNavdeep Parhar MPASS(n > 0);
6136d14b0ac1SNavdeep Parhar
6137d14b0ac1SNavdeep Parhar db = eq->doorbells;
61387951040fSNavdeep Parhar if (n > 1)
613977ad3c41SNavdeep Parhar clrbit(&db, DOORBELL_WCWR);
6140d14b0ac1SNavdeep Parhar wmb();
6141d14b0ac1SNavdeep Parhar
6142d14b0ac1SNavdeep Parhar switch (ffs(db) - 1) {
6143d14b0ac1SNavdeep Parhar case DOORBELL_UDB:
61447951040fSNavdeep Parhar *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(n));
61457951040fSNavdeep Parhar break;
6146d14b0ac1SNavdeep Parhar
614777ad3c41SNavdeep Parhar case DOORBELL_WCWR: {
6148d14b0ac1SNavdeep Parhar volatile uint64_t *dst, *src;
6149d14b0ac1SNavdeep Parhar int i;
6150d14b0ac1SNavdeep Parhar
6151d14b0ac1SNavdeep Parhar /*
6152d14b0ac1SNavdeep Parhar * Queues whose 128B doorbell segment fits in the page do not
6153d14b0ac1SNavdeep Parhar * use relative qid (udb_qid is always 0). Only queues with
615477ad3c41SNavdeep Parhar * doorbell segments can do WCWR.
6155d14b0ac1SNavdeep Parhar */
61567951040fSNavdeep Parhar KASSERT(eq->udb_qid == 0 && n == 1,
6157d14b0ac1SNavdeep Parhar ("%s: inappropriate doorbell (0x%x, %d, %d) for eq %p",
61587951040fSNavdeep Parhar __func__, eq->doorbells, n, eq->dbidx, eq));
6159d14b0ac1SNavdeep Parhar
6160d14b0ac1SNavdeep Parhar dst = (volatile void *)((uintptr_t)eq->udb + UDBS_WR_OFFSET -
6161d14b0ac1SNavdeep Parhar UDBS_DB_OFFSET);
61627951040fSNavdeep Parhar i = eq->dbidx;
6163d14b0ac1SNavdeep Parhar src = (void *)&eq->desc[i];
6164d14b0ac1SNavdeep Parhar while (src != (void *)&eq->desc[i + 1])
6165d14b0ac1SNavdeep Parhar *dst++ = *src++;
6166d14b0ac1SNavdeep Parhar wmb();
61677951040fSNavdeep Parhar break;
6168d14b0ac1SNavdeep Parhar }
6169d14b0ac1SNavdeep Parhar
6170d14b0ac1SNavdeep Parhar case DOORBELL_UDBWC:
61717951040fSNavdeep Parhar *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(n));
6172d14b0ac1SNavdeep Parhar wmb();
61737951040fSNavdeep Parhar break;
6174d14b0ac1SNavdeep Parhar
6175d14b0ac1SNavdeep Parhar case DOORBELL_KDB:
6176315048f2SJohn Baldwin t4_write_reg(sc, sc->sge_kdoorbell_reg,
61777951040fSNavdeep Parhar V_QID(eq->cntxt_id) | V_PIDX(n));
61787951040fSNavdeep Parhar break;
617954e4ee71SNavdeep Parhar }
618054e4ee71SNavdeep Parhar
61817951040fSNavdeep Parhar IDXINCR(eq->dbidx, n, eq->sidx);
61827951040fSNavdeep Parhar }
61837951040fSNavdeep Parhar
61847951040fSNavdeep Parhar static inline u_int
reclaimable_tx_desc(struct sge_eq * eq)61857951040fSNavdeep Parhar reclaimable_tx_desc(struct sge_eq *eq)
618654e4ee71SNavdeep Parhar {
61877951040fSNavdeep Parhar uint16_t hw_cidx;
618854e4ee71SNavdeep Parhar
61897951040fSNavdeep Parhar hw_cidx = read_hw_cidx(eq);
61907951040fSNavdeep Parhar return (IDXDIFF(hw_cidx, eq->cidx, eq->sidx));
61917951040fSNavdeep Parhar }
619254e4ee71SNavdeep Parhar
61937951040fSNavdeep Parhar static inline u_int
total_available_tx_desc(struct sge_eq * eq)61947951040fSNavdeep Parhar total_available_tx_desc(struct sge_eq *eq)
61957951040fSNavdeep Parhar {
61967951040fSNavdeep Parhar uint16_t hw_cidx, pidx;
61977951040fSNavdeep Parhar
61987951040fSNavdeep Parhar hw_cidx = read_hw_cidx(eq);
61997951040fSNavdeep Parhar pidx = eq->pidx;
62007951040fSNavdeep Parhar
62017951040fSNavdeep Parhar if (pidx == hw_cidx)
62027951040fSNavdeep Parhar return (eq->sidx - 1);
620354e4ee71SNavdeep Parhar else
62047951040fSNavdeep Parhar return (IDXDIFF(hw_cidx, pidx, eq->sidx) - 1);
62057951040fSNavdeep Parhar }
62067951040fSNavdeep Parhar
62077951040fSNavdeep Parhar static inline uint16_t
read_hw_cidx(struct sge_eq * eq)62087951040fSNavdeep Parhar read_hw_cidx(struct sge_eq *eq)
62097951040fSNavdeep Parhar {
62107951040fSNavdeep Parhar struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
62117951040fSNavdeep Parhar uint16_t cidx = spg->cidx; /* stable snapshot */
62127951040fSNavdeep Parhar
62137951040fSNavdeep Parhar return (be16toh(cidx));
6214e874ff7aSNavdeep Parhar }
621554e4ee71SNavdeep Parhar
6216e874ff7aSNavdeep Parhar /*
62177951040fSNavdeep Parhar * Reclaim 'n' descriptors approximately.
6218e874ff7aSNavdeep Parhar */
62197951040fSNavdeep Parhar static u_int
reclaim_tx_descs(struct sge_txq * txq,u_int n)62207951040fSNavdeep Parhar reclaim_tx_descs(struct sge_txq *txq, u_int n)
6221e874ff7aSNavdeep Parhar {
6222e874ff7aSNavdeep Parhar struct tx_sdesc *txsd;
6223f7dfe243SNavdeep Parhar struct sge_eq *eq = &txq->eq;
62247951040fSNavdeep Parhar u_int can_reclaim, reclaimed;
622554e4ee71SNavdeep Parhar
6226733b9277SNavdeep Parhar TXQ_LOCK_ASSERT_OWNED(txq);
62277951040fSNavdeep Parhar MPASS(n > 0);
6228e874ff7aSNavdeep Parhar
62297951040fSNavdeep Parhar reclaimed = 0;
62307951040fSNavdeep Parhar can_reclaim = reclaimable_tx_desc(eq);
62317951040fSNavdeep Parhar while (can_reclaim && reclaimed < n) {
623254e4ee71SNavdeep Parhar int ndesc;
62337951040fSNavdeep Parhar struct mbuf *m, *nextpkt;
623454e4ee71SNavdeep Parhar
6235f7dfe243SNavdeep Parhar txsd = &txq->sdesc[eq->cidx];
623654e4ee71SNavdeep Parhar ndesc = txsd->desc_used;
623754e4ee71SNavdeep Parhar
623854e4ee71SNavdeep Parhar /* Firmware doesn't return "partial" credits. */
623954e4ee71SNavdeep Parhar KASSERT(can_reclaim >= ndesc,
624054e4ee71SNavdeep Parhar ("%s: unexpected number of credits: %d, %d",
624154e4ee71SNavdeep Parhar __func__, can_reclaim, ndesc));
6242dcd50a20SJohn Baldwin KASSERT(ndesc != 0,
6243dcd50a20SJohn Baldwin ("%s: descriptor with no credits: cidx %d",
6244dcd50a20SJohn Baldwin __func__, eq->cidx));
624554e4ee71SNavdeep Parhar
62467951040fSNavdeep Parhar for (m = txsd->m; m != NULL; m = nextpkt) {
62477951040fSNavdeep Parhar nextpkt = m->m_nextpkt;
62487951040fSNavdeep Parhar m->m_nextpkt = NULL;
62497951040fSNavdeep Parhar m_freem(m);
62507951040fSNavdeep Parhar }
625154e4ee71SNavdeep Parhar reclaimed += ndesc;
625254e4ee71SNavdeep Parhar can_reclaim -= ndesc;
62537951040fSNavdeep Parhar IDXINCR(eq->cidx, ndesc, eq->sidx);
625454e4ee71SNavdeep Parhar }
625554e4ee71SNavdeep Parhar
625654e4ee71SNavdeep Parhar return (reclaimed);
625754e4ee71SNavdeep Parhar }
625854e4ee71SNavdeep Parhar
625954e4ee71SNavdeep Parhar static void
tx_reclaim(void * arg,int n)62607951040fSNavdeep Parhar tx_reclaim(void *arg, int n)
626154e4ee71SNavdeep Parhar {
62627951040fSNavdeep Parhar struct sge_txq *txq = arg;
62637951040fSNavdeep Parhar struct sge_eq *eq = &txq->eq;
626454e4ee71SNavdeep Parhar
62657951040fSNavdeep Parhar do {
62667951040fSNavdeep Parhar if (TXQ_TRYLOCK(txq) == 0)
62677951040fSNavdeep Parhar break;
62687951040fSNavdeep Parhar n = reclaim_tx_descs(txq, 32);
62697951040fSNavdeep Parhar if (eq->cidx == eq->pidx)
62707951040fSNavdeep Parhar eq->equeqidx = eq->pidx;
62717951040fSNavdeep Parhar TXQ_UNLOCK(txq);
62727951040fSNavdeep Parhar } while (n > 0);
627354e4ee71SNavdeep Parhar }
627454e4ee71SNavdeep Parhar
627554e4ee71SNavdeep Parhar static __be64
get_flit(struct sglist_seg * segs,int nsegs,int idx)62767951040fSNavdeep Parhar get_flit(struct sglist_seg *segs, int nsegs, int idx)
627754e4ee71SNavdeep Parhar {
627854e4ee71SNavdeep Parhar int i = (idx / 3) * 2;
627954e4ee71SNavdeep Parhar
628054e4ee71SNavdeep Parhar switch (idx % 3) {
628154e4ee71SNavdeep Parhar case 0: {
6282f078ecf6SWojciech Macek uint64_t rc;
628354e4ee71SNavdeep Parhar
6284f078ecf6SWojciech Macek rc = (uint64_t)segs[i].ss_len << 32;
628554e4ee71SNavdeep Parhar if (i + 1 < nsegs)
6286f078ecf6SWojciech Macek rc |= (uint64_t)(segs[i + 1].ss_len);
628754e4ee71SNavdeep Parhar
6288f078ecf6SWojciech Macek return (htobe64(rc));
628954e4ee71SNavdeep Parhar }
629054e4ee71SNavdeep Parhar case 1:
62917951040fSNavdeep Parhar return (htobe64(segs[i].ss_paddr));
629254e4ee71SNavdeep Parhar case 2:
62937951040fSNavdeep Parhar return (htobe64(segs[i + 1].ss_paddr));
629454e4ee71SNavdeep Parhar }
629554e4ee71SNavdeep Parhar
629654e4ee71SNavdeep Parhar return (0);
629754e4ee71SNavdeep Parhar }
629854e4ee71SNavdeep Parhar
629946e1e307SNavdeep Parhar static int
find_refill_source(struct adapter * sc,int maxp,bool packing)630046e1e307SNavdeep Parhar find_refill_source(struct adapter *sc, int maxp, bool packing)
630154e4ee71SNavdeep Parhar {
630246e1e307SNavdeep Parhar int i, zidx = -1;
630346e1e307SNavdeep Parhar struct rx_buf_info *rxb = &sc->sge.rx_buf_info[0];
630454e4ee71SNavdeep Parhar
630546e1e307SNavdeep Parhar if (packing) {
630646e1e307SNavdeep Parhar for (i = 0; i < SW_ZONE_SIZES; i++, rxb++) {
630746e1e307SNavdeep Parhar if (rxb->hwidx2 == -1)
630846e1e307SNavdeep Parhar continue;
630946e1e307SNavdeep Parhar if (rxb->size1 < PAGE_SIZE &&
631046e1e307SNavdeep Parhar rxb->size1 < largest_rx_cluster)
631146e1e307SNavdeep Parhar continue;
631246e1e307SNavdeep Parhar if (rxb->size1 > largest_rx_cluster)
631338035ed6SNavdeep Parhar break;
631446e1e307SNavdeep Parhar MPASS(rxb->size1 - rxb->size2 >= CL_METADATA_SIZE);
631546e1e307SNavdeep Parhar if (rxb->size2 >= maxp)
631646e1e307SNavdeep Parhar return (i);
631746e1e307SNavdeep Parhar zidx = i;
631838035ed6SNavdeep Parhar }
631938035ed6SNavdeep Parhar } else {
632046e1e307SNavdeep Parhar for (i = 0; i < SW_ZONE_SIZES; i++, rxb++) {
632146e1e307SNavdeep Parhar if (rxb->hwidx1 == -1)
632246e1e307SNavdeep Parhar continue;
632346e1e307SNavdeep Parhar if (rxb->size1 > largest_rx_cluster)
632438035ed6SNavdeep Parhar break;
632546e1e307SNavdeep Parhar if (rxb->size1 >= maxp)
632646e1e307SNavdeep Parhar return (i);
632746e1e307SNavdeep Parhar zidx = i;
632838035ed6SNavdeep Parhar }
632938035ed6SNavdeep Parhar }
633038035ed6SNavdeep Parhar
633146e1e307SNavdeep Parhar return (zidx);
633254e4ee71SNavdeep Parhar }
6333ecb79ca4SNavdeep Parhar
6334733b9277SNavdeep Parhar static void
add_fl_to_sfl(struct adapter * sc,struct sge_fl * fl)6335733b9277SNavdeep Parhar add_fl_to_sfl(struct adapter *sc, struct sge_fl *fl)
6336ecb79ca4SNavdeep Parhar {
6337733b9277SNavdeep Parhar mtx_lock(&sc->sfl_lock);
6338733b9277SNavdeep Parhar FL_LOCK(fl);
6339733b9277SNavdeep Parhar if ((fl->flags & FL_DOOMED) == 0) {
6340733b9277SNavdeep Parhar fl->flags |= FL_STARVING;
6341733b9277SNavdeep Parhar TAILQ_INSERT_TAIL(&sc->sfl, fl, link);
6342733b9277SNavdeep Parhar callout_reset(&sc->sfl_callout, hz / 5, refill_sfl, sc);
6343733b9277SNavdeep Parhar }
6344733b9277SNavdeep Parhar FL_UNLOCK(fl);
6345733b9277SNavdeep Parhar mtx_unlock(&sc->sfl_lock);
6346733b9277SNavdeep Parhar }
6347ecb79ca4SNavdeep Parhar
63487951040fSNavdeep Parhar static void
handle_wrq_egr_update(struct adapter * sc,struct sge_eq * eq)63497951040fSNavdeep Parhar handle_wrq_egr_update(struct adapter *sc, struct sge_eq *eq)
63507951040fSNavdeep Parhar {
63517951040fSNavdeep Parhar struct sge_wrq *wrq = (void *)eq;
63527951040fSNavdeep Parhar
63537951040fSNavdeep Parhar atomic_readandclear_int(&eq->equiq);
6354857d74b6SNavdeep Parhar taskqueue_enqueue(sc->tq[eq->port_id], &wrq->wrq_tx_task);
63557951040fSNavdeep Parhar }
63567951040fSNavdeep Parhar
63577951040fSNavdeep Parhar static void
handle_eth_egr_update(struct adapter * sc,struct sge_eq * eq)63587951040fSNavdeep Parhar handle_eth_egr_update(struct adapter *sc, struct sge_eq *eq)
63597951040fSNavdeep Parhar {
63607951040fSNavdeep Parhar struct sge_txq *txq = (void *)eq;
63617951040fSNavdeep Parhar
636243bbae19SNavdeep Parhar MPASS(eq->type == EQ_ETH);
63637951040fSNavdeep Parhar
63647951040fSNavdeep Parhar atomic_readandclear_int(&eq->equiq);
6365d735920dSNavdeep Parhar if (mp_ring_is_idle(txq->r))
6366857d74b6SNavdeep Parhar taskqueue_enqueue(sc->tq[eq->port_id], &txq->tx_reclaim_task);
6367d735920dSNavdeep Parhar else
6368d735920dSNavdeep Parhar mp_ring_check_drainage(txq->r, 64);
63697951040fSNavdeep Parhar }
63707951040fSNavdeep Parhar
6371733b9277SNavdeep Parhar static int
handle_sge_egr_update(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)6372733b9277SNavdeep Parhar handle_sge_egr_update(struct sge_iq *iq, const struct rss_header *rss,
6373733b9277SNavdeep Parhar struct mbuf *m)
6374733b9277SNavdeep Parhar {
6375733b9277SNavdeep Parhar const struct cpl_sge_egr_update *cpl = (const void *)(rss + 1);
6376733b9277SNavdeep Parhar unsigned int qid = G_EGR_QID(ntohl(cpl->opcode_qid));
6377733b9277SNavdeep Parhar struct adapter *sc = iq->adapter;
6378733b9277SNavdeep Parhar struct sge *s = &sc->sge;
6379733b9277SNavdeep Parhar struct sge_eq *eq;
63807951040fSNavdeep Parhar static void (*h[])(struct adapter *, struct sge_eq *) = {NULL,
63817951040fSNavdeep Parhar &handle_wrq_egr_update, &handle_eth_egr_update,
63827951040fSNavdeep Parhar &handle_wrq_egr_update};
6383733b9277SNavdeep Parhar
6384733b9277SNavdeep Parhar KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__,
6385733b9277SNavdeep Parhar rss->opcode));
6386733b9277SNavdeep Parhar
6387ec55567cSJohn Baldwin eq = s->eqmap[qid - s->eq_start - s->eq_base];
638843bbae19SNavdeep Parhar (*h[eq->type])(sc, eq);
6389ecb79ca4SNavdeep Parhar
6390ecb79ca4SNavdeep Parhar return (0);
6391ecb79ca4SNavdeep Parhar }
6392f7dfe243SNavdeep Parhar
63930abd31e2SNavdeep Parhar /* handle_fw_msg works for both fw4_msg and fw6_msg because this is valid */
63940abd31e2SNavdeep Parhar CTASSERT(offsetof(struct cpl_fw4_msg, data) == \
63950abd31e2SNavdeep Parhar offsetof(struct cpl_fw6_msg, data));
63960abd31e2SNavdeep Parhar
6397733b9277SNavdeep Parhar static int
handle_fw_msg(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)63981b4cc91fSNavdeep Parhar handle_fw_msg(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
639956599263SNavdeep Parhar {
64001b4cc91fSNavdeep Parhar struct adapter *sc = iq->adapter;
640156599263SNavdeep Parhar const struct cpl_fw6_msg *cpl = (const void *)(rss + 1);
640256599263SNavdeep Parhar
6403733b9277SNavdeep Parhar KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__,
6404733b9277SNavdeep Parhar rss->opcode));
6405733b9277SNavdeep Parhar
64060abd31e2SNavdeep Parhar if (cpl->type == FW_TYPE_RSSCPL || cpl->type == FW6_TYPE_RSSCPL) {
64070abd31e2SNavdeep Parhar const struct rss_header *rss2;
64080abd31e2SNavdeep Parhar
64090abd31e2SNavdeep Parhar rss2 = (const struct rss_header *)&cpl->data[0];
6410671bf2b8SNavdeep Parhar return (t4_cpl_handler[rss2->opcode](iq, rss2, m));
64110abd31e2SNavdeep Parhar }
64120abd31e2SNavdeep Parhar
6413671bf2b8SNavdeep Parhar return (t4_fw_msg_handler[cpl->type](sc, &cpl->data[0]));
6414f7dfe243SNavdeep Parhar }
6415af49c942SNavdeep Parhar
6416069af0ebSJohn Baldwin /**
6417069af0ebSJohn Baldwin * t4_handle_wrerr_rpl - process a FW work request error message
6418069af0ebSJohn Baldwin * @adap: the adapter
6419069af0ebSJohn Baldwin * @rpl: start of the FW message
6420069af0ebSJohn Baldwin */
6421069af0ebSJohn Baldwin static int
t4_handle_wrerr_rpl(struct adapter * adap,const __be64 * rpl)6422069af0ebSJohn Baldwin t4_handle_wrerr_rpl(struct adapter *adap, const __be64 *rpl)
6423069af0ebSJohn Baldwin {
6424069af0ebSJohn Baldwin u8 opcode = *(const u8 *)rpl;
6425069af0ebSJohn Baldwin const struct fw_error_cmd *e = (const void *)rpl;
6426069af0ebSJohn Baldwin unsigned int i;
6427069af0ebSJohn Baldwin
6428069af0ebSJohn Baldwin if (opcode != FW_ERROR_CMD) {
6429069af0ebSJohn Baldwin log(LOG_ERR,
6430069af0ebSJohn Baldwin "%s: Received WRERR_RPL message with opcode %#x\n",
6431069af0ebSJohn Baldwin device_get_nameunit(adap->dev), opcode);
6432069af0ebSJohn Baldwin return (EINVAL);
6433069af0ebSJohn Baldwin }
6434069af0ebSJohn Baldwin log(LOG_ERR, "%s: FW_ERROR (%s) ", device_get_nameunit(adap->dev),
6435069af0ebSJohn Baldwin G_FW_ERROR_CMD_FATAL(be32toh(e->op_to_type)) ? "fatal" :
6436069af0ebSJohn Baldwin "non-fatal");
6437069af0ebSJohn Baldwin switch (G_FW_ERROR_CMD_TYPE(be32toh(e->op_to_type))) {
6438069af0ebSJohn Baldwin case FW_ERROR_TYPE_EXCEPTION:
6439069af0ebSJohn Baldwin log(LOG_ERR, "exception info:\n");
6440069af0ebSJohn Baldwin for (i = 0; i < nitems(e->u.exception.info); i++)
6441069af0ebSJohn Baldwin log(LOG_ERR, "%s%08x", i == 0 ? "\t" : " ",
6442069af0ebSJohn Baldwin be32toh(e->u.exception.info[i]));
6443069af0ebSJohn Baldwin log(LOG_ERR, "\n");
6444069af0ebSJohn Baldwin break;
6445069af0ebSJohn Baldwin case FW_ERROR_TYPE_HWMODULE:
6446069af0ebSJohn Baldwin log(LOG_ERR, "HW module regaddr %08x regval %08x\n",
6447069af0ebSJohn Baldwin be32toh(e->u.hwmodule.regaddr),
6448069af0ebSJohn Baldwin be32toh(e->u.hwmodule.regval));
6449069af0ebSJohn Baldwin break;
6450069af0ebSJohn Baldwin case FW_ERROR_TYPE_WR:
6451069af0ebSJohn Baldwin log(LOG_ERR, "WR cidx %d PF %d VF %d eqid %d hdr:\n",
6452069af0ebSJohn Baldwin be16toh(e->u.wr.cidx),
6453069af0ebSJohn Baldwin G_FW_ERROR_CMD_PFN(be16toh(e->u.wr.pfn_vfn)),
6454069af0ebSJohn Baldwin G_FW_ERROR_CMD_VFN(be16toh(e->u.wr.pfn_vfn)),
6455069af0ebSJohn Baldwin be32toh(e->u.wr.eqid));
6456069af0ebSJohn Baldwin for (i = 0; i < nitems(e->u.wr.wrhdr); i++)
6457069af0ebSJohn Baldwin log(LOG_ERR, "%s%02x", i == 0 ? "\t" : " ",
6458069af0ebSJohn Baldwin e->u.wr.wrhdr[i]);
6459069af0ebSJohn Baldwin log(LOG_ERR, "\n");
6460069af0ebSJohn Baldwin break;
6461069af0ebSJohn Baldwin case FW_ERROR_TYPE_ACL:
6462069af0ebSJohn Baldwin log(LOG_ERR, "ACL cidx %d PF %d VF %d eqid %d %s",
6463069af0ebSJohn Baldwin be16toh(e->u.acl.cidx),
6464069af0ebSJohn Baldwin G_FW_ERROR_CMD_PFN(be16toh(e->u.acl.pfn_vfn)),
6465069af0ebSJohn Baldwin G_FW_ERROR_CMD_VFN(be16toh(e->u.acl.pfn_vfn)),
6466069af0ebSJohn Baldwin be32toh(e->u.acl.eqid),
6467069af0ebSJohn Baldwin G_FW_ERROR_CMD_MV(be16toh(e->u.acl.mv_pkd)) ? "vlanid" :
6468069af0ebSJohn Baldwin "MAC");
6469069af0ebSJohn Baldwin for (i = 0; i < nitems(e->u.acl.val); i++)
6470069af0ebSJohn Baldwin log(LOG_ERR, " %02x", e->u.acl.val[i]);
6471069af0ebSJohn Baldwin log(LOG_ERR, "\n");
6472069af0ebSJohn Baldwin break;
6473069af0ebSJohn Baldwin default:
6474069af0ebSJohn Baldwin log(LOG_ERR, "type %#x\n",
6475069af0ebSJohn Baldwin G_FW_ERROR_CMD_TYPE(be32toh(e->op_to_type)));
6476069af0ebSJohn Baldwin return (EINVAL);
6477069af0ebSJohn Baldwin }
6478069af0ebSJohn Baldwin return (0);
6479069af0ebSJohn Baldwin }
6480069af0ebSJohn Baldwin
648146e1e307SNavdeep Parhar static inline bool
bufidx_used(struct adapter * sc,int idx)648246e1e307SNavdeep Parhar bufidx_used(struct adapter *sc, int idx)
648346e1e307SNavdeep Parhar {
648446e1e307SNavdeep Parhar struct rx_buf_info *rxb = &sc->sge.rx_buf_info[0];
648546e1e307SNavdeep Parhar int i;
648646e1e307SNavdeep Parhar
648746e1e307SNavdeep Parhar for (i = 0; i < SW_ZONE_SIZES; i++, rxb++) {
648846e1e307SNavdeep Parhar if (rxb->size1 > largest_rx_cluster)
648946e1e307SNavdeep Parhar continue;
649046e1e307SNavdeep Parhar if (rxb->hwidx1 == idx || rxb->hwidx2 == idx)
649146e1e307SNavdeep Parhar return (true);
649246e1e307SNavdeep Parhar }
649346e1e307SNavdeep Parhar
649446e1e307SNavdeep Parhar return (false);
649546e1e307SNavdeep Parhar }
649646e1e307SNavdeep Parhar
649738035ed6SNavdeep Parhar static int
sysctl_bufsizes(SYSCTL_HANDLER_ARGS)649838035ed6SNavdeep Parhar sysctl_bufsizes(SYSCTL_HANDLER_ARGS)
649938035ed6SNavdeep Parhar {
650046e1e307SNavdeep Parhar struct adapter *sc = arg1;
650146e1e307SNavdeep Parhar struct sge_params *sp = &sc->params.sge;
650238035ed6SNavdeep Parhar int i, rc;
650338035ed6SNavdeep Parhar struct sbuf sb;
650438035ed6SNavdeep Parhar char c;
650538035ed6SNavdeep Parhar
650646e1e307SNavdeep Parhar sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
650746e1e307SNavdeep Parhar for (i = 0; i < SGE_FLBUF_SIZES; i++) {
650846e1e307SNavdeep Parhar if (bufidx_used(sc, i))
650938035ed6SNavdeep Parhar c = '*';
651038035ed6SNavdeep Parhar else
651138035ed6SNavdeep Parhar c = '\0';
651238035ed6SNavdeep Parhar
651346e1e307SNavdeep Parhar sbuf_printf(&sb, "%u%c ", sp->sge_fl_buffer_size[i], c);
651438035ed6SNavdeep Parhar }
651538035ed6SNavdeep Parhar sbuf_trim(&sb);
651638035ed6SNavdeep Parhar sbuf_finish(&sb);
651738035ed6SNavdeep Parhar rc = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
651838035ed6SNavdeep Parhar sbuf_delete(&sb);
651938035ed6SNavdeep Parhar return (rc);
652038035ed6SNavdeep Parhar }
652102f972e8SNavdeep Parhar
6522786099deSNavdeep Parhar #ifdef RATELIMIT
6523ffbb373cSNavdeep Parhar #if defined(INET) || defined(INET6)
6524786099deSNavdeep Parhar /*
6525786099deSNavdeep Parhar * len16 for a txpkt WR with a GL. Includes the firmware work request header.
6526786099deSNavdeep Parhar */
6527786099deSNavdeep Parhar static inline u_int
txpkt_eo_len16(u_int nsegs,u_int immhdrs,u_int tso)6528786099deSNavdeep Parhar txpkt_eo_len16(u_int nsegs, u_int immhdrs, u_int tso)
6529786099deSNavdeep Parhar {
6530786099deSNavdeep Parhar u_int n;
6531786099deSNavdeep Parhar
6532786099deSNavdeep Parhar MPASS(immhdrs > 0);
6533786099deSNavdeep Parhar
6534786099deSNavdeep Parhar n = roundup2(sizeof(struct fw_eth_tx_eo_wr) +
6535786099deSNavdeep Parhar sizeof(struct cpl_tx_pkt_core) + immhdrs, 16);
6536786099deSNavdeep Parhar if (__predict_false(nsegs == 0))
6537786099deSNavdeep Parhar goto done;
6538786099deSNavdeep Parhar
6539786099deSNavdeep Parhar nsegs--; /* first segment is part of ulptx_sgl */
6540786099deSNavdeep Parhar n += sizeof(struct ulptx_sgl) + 8 * ((3 * nsegs) / 2 + (nsegs & 1));
6541786099deSNavdeep Parhar if (tso)
6542786099deSNavdeep Parhar n += sizeof(struct cpl_tx_pkt_lso_core);
6543786099deSNavdeep Parhar
6544786099deSNavdeep Parhar done:
6545786099deSNavdeep Parhar return (howmany(n, 16));
6546786099deSNavdeep Parhar }
6547ffbb373cSNavdeep Parhar #endif
6548786099deSNavdeep Parhar
6549786099deSNavdeep Parhar #define ETID_FLOWC_NPARAMS 6
6550786099deSNavdeep Parhar #define ETID_FLOWC_LEN (roundup2((sizeof(struct fw_flowc_wr) + \
6551786099deSNavdeep Parhar ETID_FLOWC_NPARAMS * sizeof(struct fw_flowc_mnemval)), 16))
6552786099deSNavdeep Parhar #define ETID_FLOWC_LEN16 (howmany(ETID_FLOWC_LEN, 16))
6553786099deSNavdeep Parhar
6554c98146aeSJohn Baldwin #if defined(INET) || defined(INET6)
6555786099deSNavdeep Parhar static int
send_etid_flowc_wr(struct cxgbe_rate_tag * cst,struct port_info * pi,struct vi_info * vi)6556e38a50e8SJohn Baldwin send_etid_flowc_wr(struct cxgbe_rate_tag *cst, struct port_info *pi,
6557786099deSNavdeep Parhar struct vi_info *vi)
6558786099deSNavdeep Parhar {
6559786099deSNavdeep Parhar struct wrq_cookie cookie;
6560edb518f4SNavdeep Parhar u_int pfvf = pi->adapter->pf << S_FW_VIID_PFN;
6561786099deSNavdeep Parhar struct fw_flowc_wr *flowc;
6562786099deSNavdeep Parhar
6563786099deSNavdeep Parhar mtx_assert(&cst->lock, MA_OWNED);
6564786099deSNavdeep Parhar MPASS((cst->flags & (EO_FLOWC_PENDING | EO_FLOWC_RPL_PENDING)) ==
6565786099deSNavdeep Parhar EO_FLOWC_PENDING);
6566786099deSNavdeep Parhar
6567077ba6a8SJohn Baldwin flowc = start_wrq_wr(&cst->eo_txq->wrq, ETID_FLOWC_LEN16, &cookie);
6568786099deSNavdeep Parhar if (__predict_false(flowc == NULL))
6569786099deSNavdeep Parhar return (ENOMEM);
6570786099deSNavdeep Parhar
6571786099deSNavdeep Parhar bzero(flowc, ETID_FLOWC_LEN);
6572786099deSNavdeep Parhar flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) |
6573786099deSNavdeep Parhar V_FW_FLOWC_WR_NPARAMS(ETID_FLOWC_NPARAMS) | V_FW_WR_COMPL(0));
6574786099deSNavdeep Parhar flowc->flowid_len16 = htonl(V_FW_WR_LEN16(ETID_FLOWC_LEN16) |
6575786099deSNavdeep Parhar V_FW_WR_FLOWID(cst->etid));
6576786099deSNavdeep Parhar flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
6577786099deSNavdeep Parhar flowc->mnemval[0].val = htobe32(pfvf);
6578786099deSNavdeep Parhar flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
6579786099deSNavdeep Parhar flowc->mnemval[1].val = htobe32(pi->tx_chan);
6580786099deSNavdeep Parhar flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
6581786099deSNavdeep Parhar flowc->mnemval[2].val = htobe32(pi->tx_chan);
6582786099deSNavdeep Parhar flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
6583786099deSNavdeep Parhar flowc->mnemval[3].val = htobe32(cst->iqid);
6584786099deSNavdeep Parhar flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_EOSTATE;
6585786099deSNavdeep Parhar flowc->mnemval[4].val = htobe32(FW_FLOWC_MNEM_EOSTATE_ESTABLISHED);
6586786099deSNavdeep Parhar flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_SCHEDCLASS;
6587786099deSNavdeep Parhar flowc->mnemval[5].val = htobe32(cst->schedcl);
6588786099deSNavdeep Parhar
6589077ba6a8SJohn Baldwin commit_wrq_wr(&cst->eo_txq->wrq, flowc, &cookie);
6590786099deSNavdeep Parhar
6591786099deSNavdeep Parhar cst->flags &= ~EO_FLOWC_PENDING;
6592786099deSNavdeep Parhar cst->flags |= EO_FLOWC_RPL_PENDING;
6593786099deSNavdeep Parhar MPASS(cst->tx_credits >= ETID_FLOWC_LEN16); /* flowc is first WR. */
6594786099deSNavdeep Parhar cst->tx_credits -= ETID_FLOWC_LEN16;
6595786099deSNavdeep Parhar
6596786099deSNavdeep Parhar return (0);
6597786099deSNavdeep Parhar }
6598c98146aeSJohn Baldwin #endif
6599786099deSNavdeep Parhar
6600786099deSNavdeep Parhar #define ETID_FLUSH_LEN16 (howmany(sizeof (struct fw_flowc_wr), 16))
6601786099deSNavdeep Parhar
6602786099deSNavdeep Parhar void
send_etid_flush_wr(struct cxgbe_rate_tag * cst)6603e38a50e8SJohn Baldwin send_etid_flush_wr(struct cxgbe_rate_tag *cst)
6604786099deSNavdeep Parhar {
6605786099deSNavdeep Parhar struct fw_flowc_wr *flowc;
6606786099deSNavdeep Parhar struct wrq_cookie cookie;
6607786099deSNavdeep Parhar
6608786099deSNavdeep Parhar mtx_assert(&cst->lock, MA_OWNED);
6609786099deSNavdeep Parhar
6610077ba6a8SJohn Baldwin flowc = start_wrq_wr(&cst->eo_txq->wrq, ETID_FLUSH_LEN16, &cookie);
6611786099deSNavdeep Parhar if (__predict_false(flowc == NULL))
6612786099deSNavdeep Parhar CXGBE_UNIMPLEMENTED(__func__);
6613786099deSNavdeep Parhar
6614786099deSNavdeep Parhar bzero(flowc, ETID_FLUSH_LEN16 * 16);
6615786099deSNavdeep Parhar flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) |
6616786099deSNavdeep Parhar V_FW_FLOWC_WR_NPARAMS(0) | F_FW_WR_COMPL);
6617786099deSNavdeep Parhar flowc->flowid_len16 = htobe32(V_FW_WR_LEN16(ETID_FLUSH_LEN16) |
6618786099deSNavdeep Parhar V_FW_WR_FLOWID(cst->etid));
6619786099deSNavdeep Parhar
6620077ba6a8SJohn Baldwin commit_wrq_wr(&cst->eo_txq->wrq, flowc, &cookie);
6621786099deSNavdeep Parhar
6622786099deSNavdeep Parhar cst->flags |= EO_FLUSH_RPL_PENDING;
6623786099deSNavdeep Parhar MPASS(cst->tx_credits >= ETID_FLUSH_LEN16);
6624786099deSNavdeep Parhar cst->tx_credits -= ETID_FLUSH_LEN16;
6625786099deSNavdeep Parhar cst->ncompl++;
6626786099deSNavdeep Parhar }
6627786099deSNavdeep Parhar
6628786099deSNavdeep Parhar static void
write_ethofld_wr(struct cxgbe_rate_tag * cst,struct fw_eth_tx_eo_wr * wr,struct mbuf * m0,int compl)6629e38a50e8SJohn Baldwin write_ethofld_wr(struct cxgbe_rate_tag *cst, struct fw_eth_tx_eo_wr *wr,
6630786099deSNavdeep Parhar struct mbuf *m0, int compl)
6631786099deSNavdeep Parhar {
6632786099deSNavdeep Parhar struct cpl_tx_pkt_core *cpl;
6633786099deSNavdeep Parhar uint64_t ctrl1;
6634786099deSNavdeep Parhar uint32_t ctrl; /* used in many unrelated places */
6635786099deSNavdeep Parhar int len16, pktlen, nsegs, immhdrs;
6636786099deSNavdeep Parhar uintptr_t p;
6637786099deSNavdeep Parhar struct ulptx_sgl *usgl;
6638786099deSNavdeep Parhar struct sglist sg;
6639786099deSNavdeep Parhar struct sglist_seg segs[38]; /* XXX: find real limit. XXX: get off the stack */
6640786099deSNavdeep Parhar
6641786099deSNavdeep Parhar mtx_assert(&cst->lock, MA_OWNED);
6642786099deSNavdeep Parhar M_ASSERTPKTHDR(m0);
6643786099deSNavdeep Parhar KASSERT(m0->m_pkthdr.l2hlen > 0 && m0->m_pkthdr.l3hlen > 0 &&
6644786099deSNavdeep Parhar m0->m_pkthdr.l4hlen > 0,
6645786099deSNavdeep Parhar ("%s: ethofld mbuf %p is missing header lengths", __func__, m0));
6646786099deSNavdeep Parhar
6647786099deSNavdeep Parhar len16 = mbuf_eo_len16(m0);
6648786099deSNavdeep Parhar nsegs = mbuf_eo_nsegs(m0);
6649786099deSNavdeep Parhar pktlen = m0->m_pkthdr.len;
6650786099deSNavdeep Parhar ctrl = sizeof(struct cpl_tx_pkt_core);
6651786099deSNavdeep Parhar if (needs_tso(m0))
6652786099deSNavdeep Parhar ctrl += sizeof(struct cpl_tx_pkt_lso_core);
6653786099deSNavdeep Parhar immhdrs = m0->m_pkthdr.l2hlen + m0->m_pkthdr.l3hlen + m0->m_pkthdr.l4hlen;
6654786099deSNavdeep Parhar ctrl += immhdrs;
6655786099deSNavdeep Parhar
6656786099deSNavdeep Parhar wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_EO_WR) |
6657786099deSNavdeep Parhar V_FW_ETH_TX_EO_WR_IMMDLEN(ctrl) | V_FW_WR_COMPL(!!compl));
6658786099deSNavdeep Parhar wr->equiq_to_len16 = htobe32(V_FW_WR_LEN16(len16) |
6659786099deSNavdeep Parhar V_FW_WR_FLOWID(cst->etid));
6660786099deSNavdeep Parhar wr->r3 = 0;
6661a4a4ad2dSNavdeep Parhar if (needs_outer_udp_csum(m0)) {
66626933902dSNavdeep Parhar wr->u.udpseg.type = FW_ETH_TX_EO_TYPE_UDPSEG;
66636933902dSNavdeep Parhar wr->u.udpseg.ethlen = m0->m_pkthdr.l2hlen;
66646933902dSNavdeep Parhar wr->u.udpseg.iplen = htobe16(m0->m_pkthdr.l3hlen);
66656933902dSNavdeep Parhar wr->u.udpseg.udplen = m0->m_pkthdr.l4hlen;
66666933902dSNavdeep Parhar wr->u.udpseg.rtplen = 0;
66676933902dSNavdeep Parhar wr->u.udpseg.r4 = 0;
66686933902dSNavdeep Parhar wr->u.udpseg.mss = htobe16(pktlen - immhdrs);
66696933902dSNavdeep Parhar wr->u.udpseg.schedpktsize = wr->u.udpseg.mss;
66706933902dSNavdeep Parhar wr->u.udpseg.plen = htobe32(pktlen - immhdrs);
66716933902dSNavdeep Parhar cpl = (void *)(wr + 1);
66726933902dSNavdeep Parhar } else {
6673a4a4ad2dSNavdeep Parhar MPASS(needs_outer_tcp_csum(m0));
6674786099deSNavdeep Parhar wr->u.tcpseg.type = FW_ETH_TX_EO_TYPE_TCPSEG;
6675786099deSNavdeep Parhar wr->u.tcpseg.ethlen = m0->m_pkthdr.l2hlen;
6676786099deSNavdeep Parhar wr->u.tcpseg.iplen = htobe16(m0->m_pkthdr.l3hlen);
6677786099deSNavdeep Parhar wr->u.tcpseg.tcplen = m0->m_pkthdr.l4hlen;
6678786099deSNavdeep Parhar wr->u.tcpseg.tsclk_tsoff = mbuf_eo_tsclk_tsoff(m0);
6679786099deSNavdeep Parhar wr->u.tcpseg.r4 = 0;
6680786099deSNavdeep Parhar wr->u.tcpseg.r5 = 0;
6681786099deSNavdeep Parhar wr->u.tcpseg.plen = htobe32(pktlen - immhdrs);
6682786099deSNavdeep Parhar
6683786099deSNavdeep Parhar if (needs_tso(m0)) {
6684786099deSNavdeep Parhar struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
6685786099deSNavdeep Parhar
6686786099deSNavdeep Parhar wr->u.tcpseg.mss = htobe16(m0->m_pkthdr.tso_segsz);
6687786099deSNavdeep Parhar
66886933902dSNavdeep Parhar ctrl = V_LSO_OPCODE(CPL_TX_PKT_LSO) |
66896933902dSNavdeep Parhar F_LSO_FIRST_SLICE | F_LSO_LAST_SLICE |
6690c0236bd9SNavdeep Parhar V_LSO_ETHHDR_LEN((m0->m_pkthdr.l2hlen -
6691c0236bd9SNavdeep Parhar ETHER_HDR_LEN) >> 2) |
66926933902dSNavdeep Parhar V_LSO_IPHDR_LEN(m0->m_pkthdr.l3hlen >> 2) |
66936933902dSNavdeep Parhar V_LSO_TCPHDR_LEN(m0->m_pkthdr.l4hlen >> 2);
6694786099deSNavdeep Parhar if (m0->m_pkthdr.l3hlen == sizeof(struct ip6_hdr))
6695786099deSNavdeep Parhar ctrl |= F_LSO_IPV6;
6696786099deSNavdeep Parhar lso->lso_ctrl = htobe32(ctrl);
6697786099deSNavdeep Parhar lso->ipid_ofst = htobe16(0);
6698786099deSNavdeep Parhar lso->mss = htobe16(m0->m_pkthdr.tso_segsz);
6699786099deSNavdeep Parhar lso->seqno_offset = htobe32(0);
6700786099deSNavdeep Parhar lso->len = htobe32(pktlen);
6701786099deSNavdeep Parhar
6702786099deSNavdeep Parhar cpl = (void *)(lso + 1);
6703786099deSNavdeep Parhar } else {
6704786099deSNavdeep Parhar wr->u.tcpseg.mss = htobe16(0xffff);
6705786099deSNavdeep Parhar cpl = (void *)(wr + 1);
6706786099deSNavdeep Parhar }
67076933902dSNavdeep Parhar }
6708786099deSNavdeep Parhar
6709786099deSNavdeep Parhar /* Checksum offload must be requested for ethofld. */
6710a4a4ad2dSNavdeep Parhar MPASS(needs_outer_l4_csum(m0));
6711c0236bd9SNavdeep Parhar ctrl1 = csum_to_ctrl(cst->adapter, m0);
6712786099deSNavdeep Parhar
6713786099deSNavdeep Parhar /* VLAN tag insertion */
6714786099deSNavdeep Parhar if (needs_vlan_insertion(m0)) {
6715786099deSNavdeep Parhar ctrl1 |= F_TXPKT_VLAN_VLD |
6716786099deSNavdeep Parhar V_TXPKT_VLAN(m0->m_pkthdr.ether_vtag);
6717786099deSNavdeep Parhar }
6718786099deSNavdeep Parhar
6719786099deSNavdeep Parhar /* CPL header */
6720786099deSNavdeep Parhar cpl->ctrl0 = cst->ctrl0;
6721786099deSNavdeep Parhar cpl->pack = 0;
6722786099deSNavdeep Parhar cpl->len = htobe16(pktlen);
6723786099deSNavdeep Parhar cpl->ctrl1 = htobe64(ctrl1);
6724786099deSNavdeep Parhar
67256933902dSNavdeep Parhar /* Copy Ethernet, IP & TCP/UDP hdrs as immediate data */
6726786099deSNavdeep Parhar p = (uintptr_t)(cpl + 1);
6727786099deSNavdeep Parhar m_copydata(m0, 0, immhdrs, (void *)p);
6728786099deSNavdeep Parhar
6729786099deSNavdeep Parhar /* SGL */
6730786099deSNavdeep Parhar if (nsegs > 0) {
6731786099deSNavdeep Parhar int i, pad;
6732786099deSNavdeep Parhar
6733786099deSNavdeep Parhar /* zero-pad upto next 16Byte boundary, if not 16Byte aligned */
6734786099deSNavdeep Parhar p += immhdrs;
6735786099deSNavdeep Parhar pad = 16 - (immhdrs & 0xf);
6736786099deSNavdeep Parhar bzero((void *)p, pad);
6737786099deSNavdeep Parhar
6738786099deSNavdeep Parhar usgl = (void *)(p + pad);
6739786099deSNavdeep Parhar usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
6740786099deSNavdeep Parhar V_ULPTX_NSGE(nsegs));
6741786099deSNavdeep Parhar
6742786099deSNavdeep Parhar sglist_init(&sg, nitems(segs), segs);
6743786099deSNavdeep Parhar for (; m0 != NULL; m0 = m0->m_next) {
6744786099deSNavdeep Parhar if (__predict_false(m0->m_len == 0))
6745786099deSNavdeep Parhar continue;
6746786099deSNavdeep Parhar if (immhdrs >= m0->m_len) {
6747786099deSNavdeep Parhar immhdrs -= m0->m_len;
6748786099deSNavdeep Parhar continue;
6749786099deSNavdeep Parhar }
67506edfd179SGleb Smirnoff if (m0->m_flags & M_EXTPG)
675149b6b60eSGleb Smirnoff sglist_append_mbuf_epg(&sg, m0,
675249b6b60eSGleb Smirnoff mtod(m0, vm_offset_t), m0->m_len);
675349b6b60eSGleb Smirnoff else
6754786099deSNavdeep Parhar sglist_append(&sg, mtod(m0, char *) + immhdrs,
6755786099deSNavdeep Parhar m0->m_len - immhdrs);
6756786099deSNavdeep Parhar immhdrs = 0;
6757786099deSNavdeep Parhar }
6758786099deSNavdeep Parhar MPASS(sg.sg_nseg == nsegs);
6759786099deSNavdeep Parhar
6760786099deSNavdeep Parhar /*
6761786099deSNavdeep Parhar * Zero pad last 8B in case the WR doesn't end on a 16B
6762786099deSNavdeep Parhar * boundary.
6763786099deSNavdeep Parhar */
6764786099deSNavdeep Parhar *(uint64_t *)((char *)wr + len16 * 16 - 8) = 0;
6765786099deSNavdeep Parhar
6766786099deSNavdeep Parhar usgl->len0 = htobe32(segs[0].ss_len);
6767786099deSNavdeep Parhar usgl->addr0 = htobe64(segs[0].ss_paddr);
6768786099deSNavdeep Parhar for (i = 0; i < nsegs - 1; i++) {
6769786099deSNavdeep Parhar usgl->sge[i / 2].len[i & 1] = htobe32(segs[i + 1].ss_len);
6770786099deSNavdeep Parhar usgl->sge[i / 2].addr[i & 1] = htobe64(segs[i + 1].ss_paddr);
6771786099deSNavdeep Parhar }
6772786099deSNavdeep Parhar if (i & 1)
6773786099deSNavdeep Parhar usgl->sge[i / 2].len[1] = htobe32(0);
6774786099deSNavdeep Parhar }
6775786099deSNavdeep Parhar
6776786099deSNavdeep Parhar }
6777786099deSNavdeep Parhar
6778786099deSNavdeep Parhar static void
ethofld_tx(struct cxgbe_rate_tag * cst)6779e38a50e8SJohn Baldwin ethofld_tx(struct cxgbe_rate_tag *cst)
6780786099deSNavdeep Parhar {
6781786099deSNavdeep Parhar struct mbuf *m;
6782786099deSNavdeep Parhar struct wrq_cookie cookie;
6783786099deSNavdeep Parhar int next_credits, compl;
6784786099deSNavdeep Parhar struct fw_eth_tx_eo_wr *wr;
6785786099deSNavdeep Parhar
6786786099deSNavdeep Parhar mtx_assert(&cst->lock, MA_OWNED);
6787786099deSNavdeep Parhar
6788786099deSNavdeep Parhar while ((m = mbufq_first(&cst->pending_tx)) != NULL) {
6789786099deSNavdeep Parhar M_ASSERTPKTHDR(m);
6790786099deSNavdeep Parhar
6791786099deSNavdeep Parhar /* How many len16 credits do we need to send this mbuf. */
6792786099deSNavdeep Parhar next_credits = mbuf_eo_len16(m);
6793786099deSNavdeep Parhar MPASS(next_credits > 0);
6794786099deSNavdeep Parhar if (next_credits > cst->tx_credits) {
6795786099deSNavdeep Parhar /*
6796786099deSNavdeep Parhar * Tx will make progress eventually because there is at
6797786099deSNavdeep Parhar * least one outstanding fw4_ack that will return
6798786099deSNavdeep Parhar * credits and kick the tx.
6799786099deSNavdeep Parhar */
6800786099deSNavdeep Parhar MPASS(cst->ncompl > 0);
6801786099deSNavdeep Parhar return;
6802786099deSNavdeep Parhar }
6803077ba6a8SJohn Baldwin wr = start_wrq_wr(&cst->eo_txq->wrq, next_credits, &cookie);
6804786099deSNavdeep Parhar if (__predict_false(wr == NULL)) {
6805786099deSNavdeep Parhar /* XXX: wishful thinking, not a real assertion. */
6806786099deSNavdeep Parhar MPASS(cst->ncompl > 0);
6807786099deSNavdeep Parhar return;
6808786099deSNavdeep Parhar }
6809786099deSNavdeep Parhar cst->tx_credits -= next_credits;
6810786099deSNavdeep Parhar cst->tx_nocompl += next_credits;
6811786099deSNavdeep Parhar compl = cst->ncompl == 0 || cst->tx_nocompl >= cst->tx_total / 2;
681256fb710fSJohn Baldwin ETHER_BPF_MTAP(cst->com.ifp, m);
6813786099deSNavdeep Parhar write_ethofld_wr(cst, wr, m, compl);
6814077ba6a8SJohn Baldwin commit_wrq_wr(&cst->eo_txq->wrq, wr, &cookie);
6815786099deSNavdeep Parhar if (compl) {
6816786099deSNavdeep Parhar cst->ncompl++;
6817786099deSNavdeep Parhar cst->tx_nocompl = 0;
6818786099deSNavdeep Parhar }
6819786099deSNavdeep Parhar (void) mbufq_dequeue(&cst->pending_tx);
6820fb3bc596SJohn Baldwin
6821fb3bc596SJohn Baldwin /*
6822fb3bc596SJohn Baldwin * Drop the mbuf's reference on the tag now rather
6823fb3bc596SJohn Baldwin * than waiting until m_freem(). This ensures that
6824e38a50e8SJohn Baldwin * cxgbe_rate_tag_free gets called when the inp drops
6825fb3bc596SJohn Baldwin * its reference on the tag and there are no more
6826fb3bc596SJohn Baldwin * mbufs in the pending_tx queue and can flush any
6827fb3bc596SJohn Baldwin * pending requests. Otherwise if the last mbuf
6828fb3bc596SJohn Baldwin * doesn't request a completion the etid will never be
6829fb3bc596SJohn Baldwin * released.
6830fb3bc596SJohn Baldwin */
6831fb3bc596SJohn Baldwin m->m_pkthdr.snd_tag = NULL;
6832fb3bc596SJohn Baldwin m->m_pkthdr.csum_flags &= ~CSUM_SND_TAG;
683356fb710fSJohn Baldwin m_snd_tag_rele(&cst->com);
6834fb3bc596SJohn Baldwin
6835786099deSNavdeep Parhar mbufq_enqueue(&cst->pending_fwack, m);
6836786099deSNavdeep Parhar }
6837786099deSNavdeep Parhar }
6838786099deSNavdeep Parhar
6839c98146aeSJohn Baldwin #if defined(INET) || defined(INET6)
68408afd23deSJohn Baldwin static int
ethofld_transmit(if_t ifp,struct mbuf * m0)6841954712e8SJustin Hibbits ethofld_transmit(if_t ifp, struct mbuf *m0)
6842786099deSNavdeep Parhar {
6843e38a50e8SJohn Baldwin struct cxgbe_rate_tag *cst;
6844786099deSNavdeep Parhar int rc;
6845786099deSNavdeep Parhar
6846786099deSNavdeep Parhar MPASS(m0->m_nextpkt == NULL);
6847fb3bc596SJohn Baldwin MPASS(m0->m_pkthdr.csum_flags & CSUM_SND_TAG);
6848786099deSNavdeep Parhar MPASS(m0->m_pkthdr.snd_tag != NULL);
6849e38a50e8SJohn Baldwin cst = mst_to_crt(m0->m_pkthdr.snd_tag);
6850786099deSNavdeep Parhar
6851786099deSNavdeep Parhar mtx_lock(&cst->lock);
6852786099deSNavdeep Parhar MPASS(cst->flags & EO_SND_TAG_REF);
6853786099deSNavdeep Parhar
6854786099deSNavdeep Parhar if (__predict_false(cst->flags & EO_FLOWC_PENDING)) {
6855954712e8SJustin Hibbits struct vi_info *vi = if_getsoftc(ifp);
6856786099deSNavdeep Parhar struct port_info *pi = vi->pi;
6857786099deSNavdeep Parhar struct adapter *sc = pi->adapter;
6858786099deSNavdeep Parhar const uint32_t rss_mask = vi->rss_size - 1;
6859786099deSNavdeep Parhar uint32_t rss_hash;
6860786099deSNavdeep Parhar
6861786099deSNavdeep Parhar cst->eo_txq = &sc->sge.ofld_txq[vi->first_ofld_txq];
6862786099deSNavdeep Parhar if (M_HASHTYPE_ISHASH(m0))
6863786099deSNavdeep Parhar rss_hash = m0->m_pkthdr.flowid;
6864786099deSNavdeep Parhar else
6865786099deSNavdeep Parhar rss_hash = arc4random();
6866786099deSNavdeep Parhar /* We assume RSS hashing */
6867786099deSNavdeep Parhar cst->iqid = vi->rss[rss_hash & rss_mask];
6868786099deSNavdeep Parhar cst->eo_txq += rss_hash % vi->nofldtxq;
6869786099deSNavdeep Parhar rc = send_etid_flowc_wr(cst, pi, vi);
6870786099deSNavdeep Parhar if (rc != 0)
6871786099deSNavdeep Parhar goto done;
6872786099deSNavdeep Parhar }
6873786099deSNavdeep Parhar
6874786099deSNavdeep Parhar if (__predict_false(cst->plen + m0->m_pkthdr.len > eo_max_backlog)) {
6875786099deSNavdeep Parhar rc = ENOBUFS;
6876786099deSNavdeep Parhar goto done;
6877786099deSNavdeep Parhar }
6878786099deSNavdeep Parhar
6879786099deSNavdeep Parhar mbufq_enqueue(&cst->pending_tx, m0);
6880786099deSNavdeep Parhar cst->plen += m0->m_pkthdr.len;
6881786099deSNavdeep Parhar
6882fb3bc596SJohn Baldwin /*
6883fb3bc596SJohn Baldwin * Hold an extra reference on the tag while generating work
6884fb3bc596SJohn Baldwin * requests to ensure that we don't try to free the tag during
6885fb3bc596SJohn Baldwin * ethofld_tx() in case we are sending the final mbuf after
6886fb3bc596SJohn Baldwin * the inp was freed.
6887fb3bc596SJohn Baldwin */
688856fb710fSJohn Baldwin m_snd_tag_ref(&cst->com);
6889786099deSNavdeep Parhar ethofld_tx(cst);
6890fb3bc596SJohn Baldwin mtx_unlock(&cst->lock);
689156fb710fSJohn Baldwin m_snd_tag_rele(&cst->com);
6892fb3bc596SJohn Baldwin return (0);
6893fb3bc596SJohn Baldwin
6894786099deSNavdeep Parhar done:
6895786099deSNavdeep Parhar mtx_unlock(&cst->lock);
6896786099deSNavdeep Parhar return (rc);
6897786099deSNavdeep Parhar }
6898c98146aeSJohn Baldwin #endif
6899786099deSNavdeep Parhar
6900786099deSNavdeep Parhar static int
ethofld_fw4_ack(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m0)6901786099deSNavdeep Parhar ethofld_fw4_ack(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m0)
6902786099deSNavdeep Parhar {
6903786099deSNavdeep Parhar struct adapter *sc = iq->adapter;
6904786099deSNavdeep Parhar const struct cpl_fw4_ack *cpl = (const void *)(rss + 1);
6905786099deSNavdeep Parhar struct mbuf *m;
6906786099deSNavdeep Parhar u_int etid = G_CPL_FW4_ACK_FLOWID(be32toh(OPCODE_TID(cpl)));
6907e38a50e8SJohn Baldwin struct cxgbe_rate_tag *cst;
6908786099deSNavdeep Parhar uint8_t credits = cpl->credits;
6909786099deSNavdeep Parhar
6910786099deSNavdeep Parhar cst = lookup_etid(sc, etid);
6911786099deSNavdeep Parhar mtx_lock(&cst->lock);
6912786099deSNavdeep Parhar if (__predict_false(cst->flags & EO_FLOWC_RPL_PENDING)) {
6913786099deSNavdeep Parhar MPASS(credits >= ETID_FLOWC_LEN16);
6914786099deSNavdeep Parhar credits -= ETID_FLOWC_LEN16;
6915786099deSNavdeep Parhar cst->flags &= ~EO_FLOWC_RPL_PENDING;
6916786099deSNavdeep Parhar }
6917786099deSNavdeep Parhar
6918786099deSNavdeep Parhar KASSERT(cst->ncompl > 0,
6919786099deSNavdeep Parhar ("%s: etid %u (%p) wasn't expecting completion.",
6920786099deSNavdeep Parhar __func__, etid, cst));
6921786099deSNavdeep Parhar cst->ncompl--;
6922786099deSNavdeep Parhar
6923786099deSNavdeep Parhar while (credits > 0) {
6924786099deSNavdeep Parhar m = mbufq_dequeue(&cst->pending_fwack);
6925786099deSNavdeep Parhar if (__predict_false(m == NULL)) {
6926786099deSNavdeep Parhar /*
6927786099deSNavdeep Parhar * The remaining credits are for the final flush that
6928786099deSNavdeep Parhar * was issued when the tag was freed by the kernel.
6929786099deSNavdeep Parhar */
6930786099deSNavdeep Parhar MPASS((cst->flags &
6931786099deSNavdeep Parhar (EO_FLUSH_RPL_PENDING | EO_SND_TAG_REF)) ==
6932786099deSNavdeep Parhar EO_FLUSH_RPL_PENDING);
6933786099deSNavdeep Parhar MPASS(credits == ETID_FLUSH_LEN16);
6934786099deSNavdeep Parhar MPASS(cst->tx_credits + cpl->credits == cst->tx_total);
6935786099deSNavdeep Parhar MPASS(cst->ncompl == 0);
6936786099deSNavdeep Parhar
6937786099deSNavdeep Parhar cst->flags &= ~EO_FLUSH_RPL_PENDING;
6938786099deSNavdeep Parhar cst->tx_credits += cpl->credits;
6939e38a50e8SJohn Baldwin cxgbe_rate_tag_free_locked(cst);
6940786099deSNavdeep Parhar return (0); /* cst is gone. */
6941786099deSNavdeep Parhar }
6942786099deSNavdeep Parhar KASSERT(m != NULL,
6943786099deSNavdeep Parhar ("%s: too many credits (%u, %u)", __func__, cpl->credits,
6944786099deSNavdeep Parhar credits));
6945786099deSNavdeep Parhar KASSERT(credits >= mbuf_eo_len16(m),
6946786099deSNavdeep Parhar ("%s: too few credits (%u, %u, %u)", __func__,
6947786099deSNavdeep Parhar cpl->credits, credits, mbuf_eo_len16(m)));
6948786099deSNavdeep Parhar credits -= mbuf_eo_len16(m);
6949786099deSNavdeep Parhar cst->plen -= m->m_pkthdr.len;
6950786099deSNavdeep Parhar m_freem(m);
6951786099deSNavdeep Parhar }
6952786099deSNavdeep Parhar
6953786099deSNavdeep Parhar cst->tx_credits += cpl->credits;
6954786099deSNavdeep Parhar MPASS(cst->tx_credits <= cst->tx_total);
6955786099deSNavdeep Parhar
6956fb3bc596SJohn Baldwin if (cst->flags & EO_SND_TAG_REF) {
6957fb3bc596SJohn Baldwin /*
6958fb3bc596SJohn Baldwin * As with ethofld_transmit(), hold an extra reference
6959fb3bc596SJohn Baldwin * so that the tag is stable across ethold_tx().
6960fb3bc596SJohn Baldwin */
696156fb710fSJohn Baldwin m_snd_tag_ref(&cst->com);
6962786099deSNavdeep Parhar m = mbufq_first(&cst->pending_tx);
6963786099deSNavdeep Parhar if (m != NULL && cst->tx_credits >= mbuf_eo_len16(m))
6964786099deSNavdeep Parhar ethofld_tx(cst);
6965786099deSNavdeep Parhar mtx_unlock(&cst->lock);
696656fb710fSJohn Baldwin m_snd_tag_rele(&cst->com);
6967fb3bc596SJohn Baldwin } else {
6968fb3bc596SJohn Baldwin /*
6969fb3bc596SJohn Baldwin * There shouldn't be any pending packets if the tag
6970fb3bc596SJohn Baldwin * was freed by the kernel since any pending packet
6971fb3bc596SJohn Baldwin * should hold a reference to the tag.
6972fb3bc596SJohn Baldwin */
6973fb3bc596SJohn Baldwin MPASS(mbufq_first(&cst->pending_tx) == NULL);
6974fb3bc596SJohn Baldwin mtx_unlock(&cst->lock);
6975fb3bc596SJohn Baldwin }
6976786099deSNavdeep Parhar
6977786099deSNavdeep Parhar return (0);
6978786099deSNavdeep Parhar }
6979786099deSNavdeep Parhar #endif
6980