2 * Copyright (c) 2011 Chelsio Communications, Inc.
4 * Written by: Navdeep Parhar <np@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_inet6.h"
34 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/queue.h>
41 #include <sys/taskqueue.h>
43 #include <sys/sglist.h>
44 #include <sys/sysctl.h>
46 #include <sys/counter.h>
48 #include <net/ethernet.h>
50 #include <net/if_vlan_var.h>
51 #include <netinet/in.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip6.h>
54 #include <netinet/tcp.h>
55 #include <machine/md_var.h>
59 #include <machine/bus.h>
60 #include <sys/selinfo.h>
61 #include <net/if_var.h>
62 #include <net/netmap.h>
63 #include <dev/netmap/netmap_kern.h>
66 #include "common/common.h"
67 #include "common/t4_regs.h"
68 #include "common/t4_regs_values.h"
69 #include "common/t4_msg.h"
70 #include "t4_mp_ring.h"
72 #ifdef T4_PKT_TIMESTAMP
73 #define RX_COPY_THRESHOLD (MINCLSIZE - 8)
75 #define RX_COPY_THRESHOLD MINCLSIZE
79 * Ethernet frames are DMA'd at this byte offset into the freelist buffer.
80 * 0-7 are valid values.
83 TUNABLE_INT("hw.cxgbe.fl_pktshift", &fl_pktshift);
86 * Pad ethernet payload up to this boundary.
87 * -1: driver should figure out a good value.
89 * Any power of 2 from 32 to 4096 (both inclusive) is also a valid value.
92 TUNABLE_INT("hw.cxgbe.fl_pad", &fl_pad);
96 * -1: driver should figure out a good value.
97 * 64 or 128 are the only other valid values.
100 TUNABLE_INT("hw.cxgbe.spg_len", &spg_len);
104 * -1: no congestion feedback (not recommended).
105 * 0: backpressure the channel instead of dropping packets right away.
106 * 1: no backpressure, drop packets for the congested queue immediately.
108 static int cong_drop = 0;
109 TUNABLE_INT("hw.cxgbe.cong_drop", &cong_drop);
112 * Deliver multiple frames in the same free list buffer if they fit.
113 * -1: let the driver decide whether to enable buffer packing or not.
114 * 0: disable buffer packing.
115 * 1: enable buffer packing.
117 static int buffer_packing = -1;
118 TUNABLE_INT("hw.cxgbe.buffer_packing", &buffer_packing);
121 * Start next frame in a packed buffer at this boundary.
122 * -1: driver should figure out a good value.
123 * T4: driver will ignore this and use the same value as fl_pad above.
124 * T5: 16, or a power of 2 from 64 to 4096 (both inclusive) is a valid value.
126 static int fl_pack = -1;
127 TUNABLE_INT("hw.cxgbe.fl_pack", &fl_pack);
130 * Allow the driver to create mbuf(s) in a cluster allocated for rx.
131 * 0: never; always allocate mbufs from the zone_mbuf UMA zone.
132 * 1: ok to create mbuf(s) within a cluster if there is room.
134 static int allow_mbufs_in_cluster = 1;
135 TUNABLE_INT("hw.cxgbe.allow_mbufs_in_cluster", &allow_mbufs_in_cluster);
138 * Largest rx cluster size that the driver is allowed to allocate.
140 static int largest_rx_cluster = MJUM16BYTES;
141 TUNABLE_INT("hw.cxgbe.largest_rx_cluster", &largest_rx_cluster);
144 * Size of cluster allocation that's most likely to succeed. The driver will
145 * fall back to this size if it fails to allocate clusters larger than this.
147 static int safest_rx_cluster = PAGE_SIZE;
148 TUNABLE_INT("hw.cxgbe.safest_rx_cluster", &safest_rx_cluster);
151 u_int wr_type; /* type 0 or type 1 */
152 u_int npkt; /* # of packets in this work request */
153 u_int plen; /* total payload (sum of all packets) */
154 u_int len16; /* # of 16B pieces used by this work request */
157 /* A packet's SGL. This + m_pkthdr has all info needed for tx */
160 struct sglist_seg seg[TX_SGL_SEGS];
163 static int service_iq(struct sge_iq *, int);
164 static struct mbuf *get_fl_payload(struct adapter *, struct sge_fl *, uint32_t);
165 static int t4_eth_rx(struct sge_iq *, const struct rss_header *, struct mbuf *);
166 static inline void init_iq(struct sge_iq *, struct adapter *, int, int, int);
167 static inline void init_fl(struct adapter *, struct sge_fl *, int, int, char *);
168 static inline void init_eq(struct sge_eq *, int, int, uint8_t, uint16_t,
170 static int alloc_ring(struct adapter *, size_t, bus_dma_tag_t *, bus_dmamap_t *,
171 bus_addr_t *, void **);
172 static int free_ring(struct adapter *, bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
174 static int alloc_iq_fl(struct port_info *, struct sge_iq *, struct sge_fl *,
176 static int free_iq_fl(struct port_info *, struct sge_iq *, struct sge_fl *);
177 static void add_fl_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *,
179 static int alloc_fwq(struct adapter *);
180 static int free_fwq(struct adapter *);
181 static int alloc_mgmtq(struct adapter *);
182 static int free_mgmtq(struct adapter *);
183 static int alloc_rxq(struct port_info *, struct sge_rxq *, int, int,
184 struct sysctl_oid *);
185 static int free_rxq(struct port_info *, struct sge_rxq *);
187 static int alloc_ofld_rxq(struct port_info *, struct sge_ofld_rxq *, int, int,
188 struct sysctl_oid *);
189 static int free_ofld_rxq(struct port_info *, struct sge_ofld_rxq *);
192 static int alloc_nm_rxq(struct port_info *, struct sge_nm_rxq *, int, int,
193 struct sysctl_oid *);
194 static int free_nm_rxq(struct port_info *, struct sge_nm_rxq *);
195 static int alloc_nm_txq(struct port_info *, struct sge_nm_txq *, int, int,
196 struct sysctl_oid *);
197 static int free_nm_txq(struct port_info *, struct sge_nm_txq *);
199 static int ctrl_eq_alloc(struct adapter *, struct sge_eq *);
200 static int eth_eq_alloc(struct adapter *, struct port_info *, struct sge_eq *);
202 static int ofld_eq_alloc(struct adapter *, struct port_info *, struct sge_eq *);
204 static int alloc_eq(struct adapter *, struct port_info *, struct sge_eq *);
205 static int free_eq(struct adapter *, struct sge_eq *);
206 static int alloc_wrq(struct adapter *, struct port_info *, struct sge_wrq *,
207 struct sysctl_oid *);
208 static int free_wrq(struct adapter *, struct sge_wrq *);
209 static int alloc_txq(struct port_info *, struct sge_txq *, int,
210 struct sysctl_oid *);
211 static int free_txq(struct port_info *, struct sge_txq *);
212 static void oneseg_dma_callback(void *, bus_dma_segment_t *, int, int);
213 static inline void ring_fl_db(struct adapter *, struct sge_fl *);
214 static int refill_fl(struct adapter *, struct sge_fl *, int);
215 static void refill_sfl(void *);
216 static int alloc_fl_sdesc(struct sge_fl *);
217 static void free_fl_sdesc(struct adapter *, struct sge_fl *);
218 static void find_best_refill_source(struct adapter *, struct sge_fl *, int);
219 static void find_safe_refill_source(struct adapter *, struct sge_fl *);
220 static void add_fl_to_sfl(struct adapter *, struct sge_fl *);
222 static inline void get_pkt_gl(struct mbuf *, struct sglist *);
223 static inline u_int txpkt_len16(u_int, u_int);
224 static inline u_int txpkts0_len16(u_int);
225 static inline u_int txpkts1_len16(void);
226 static u_int write_txpkt_wr(struct sge_txq *, struct fw_eth_tx_pkt_wr *,
227 struct mbuf *, u_int);
228 static int try_txpkts(struct mbuf *, struct mbuf *, struct txpkts *, u_int);
229 static int add_to_txpkts(struct mbuf *, struct txpkts *, u_int);
230 static u_int write_txpkts_wr(struct sge_txq *, struct fw_eth_tx_pkts_wr *,
231 struct mbuf *, const struct txpkts *, u_int);
232 static void write_gl_to_txd(struct sge_txq *, struct mbuf *, caddr_t *, int);
233 static inline void copy_to_txd(struct sge_eq *, caddr_t, caddr_t *, int);
234 static inline void ring_eq_db(struct adapter *, struct sge_eq *, u_int);
235 static inline uint16_t read_hw_cidx(struct sge_eq *);
236 static inline u_int reclaimable_tx_desc(struct sge_eq *);
237 static inline u_int total_available_tx_desc(struct sge_eq *);
238 static u_int reclaim_tx_descs(struct sge_txq *, u_int);
239 static void tx_reclaim(void *, int);
240 static __be64 get_flit(struct sglist_seg *, int, int);
241 static int handle_sge_egr_update(struct sge_iq *, const struct rss_header *,
243 static int handle_fw_msg(struct sge_iq *, const struct rss_header *,
245 static void wrq_tx_drain(void *, int);
246 static void drain_wrq_wr_list(struct adapter *, struct sge_wrq *);
248 static int sysctl_uint16(SYSCTL_HANDLER_ARGS);
249 static int sysctl_bufsizes(SYSCTL_HANDLER_ARGS);
251 static counter_u64_t extfree_refs;
252 static counter_u64_t extfree_rels;
255 * Called on MOD_LOAD. Validates and calculates the SGE tunables.
261 if (fl_pktshift < 0 || fl_pktshift > 7) {
262 printf("Invalid hw.cxgbe.fl_pktshift value (%d),"
263 " using 2 instead.\n", fl_pktshift);
267 if (spg_len != 64 && spg_len != 128) {
270 #if defined(__i386__) || defined(__amd64__)
271 len = cpu_clflush_line_size > 64 ? 128 : 64;
276 printf("Invalid hw.cxgbe.spg_len value (%d),"
277 " using %d instead.\n", spg_len, len);
282 if (cong_drop < -1 || cong_drop > 1) {
283 printf("Invalid hw.cxgbe.cong_drop value (%d),"
284 " using 0 instead.\n", cong_drop);
288 extfree_refs = counter_u64_alloc(M_WAITOK);
289 extfree_rels = counter_u64_alloc(M_WAITOK);
290 counter_u64_zero(extfree_refs);
291 counter_u64_zero(extfree_rels);
295 t4_sge_modunload(void)
298 counter_u64_free(extfree_refs);
299 counter_u64_free(extfree_rels);
303 t4_sge_extfree_refs(void)
307 rels = counter_u64_fetch(extfree_rels);
308 refs = counter_u64_fetch(extfree_refs);
310 return (refs - rels);
314 t4_init_sge_cpl_handlers(struct adapter *sc)
317 t4_register_cpl_handler(sc, CPL_FW4_MSG, handle_fw_msg);
318 t4_register_cpl_handler(sc, CPL_FW6_MSG, handle_fw_msg);
319 t4_register_cpl_handler(sc, CPL_SGE_EGR_UPDATE, handle_sge_egr_update);
320 t4_register_cpl_handler(sc, CPL_RX_PKT, t4_eth_rx);
321 t4_register_fw_msg_handler(sc, FW6_TYPE_CMD_RPL, t4_handle_fw_rpl);
325 setup_pad_and_pack_boundaries(struct adapter *sc)
331 if (fl_pad < 32 || fl_pad > 4096 || !powerof2(fl_pad)) {
333 * If there is any chance that we might use buffer packing and
334 * the chip is a T4, then pick 64 as the pad/pack boundary. Set
335 * it to 32 in all other cases.
337 pad = is_t4(sc) && buffer_packing ? 64 : 32;
340 * For fl_pad = 0 we'll still write a reasonable value to the
341 * register but all the freelists will opt out of padding.
342 * We'll complain here only if the user tried to set it to a
343 * value greater than 0 that was invalid.
346 device_printf(sc->dev, "Invalid hw.cxgbe.fl_pad value"
347 " (%d), using %d instead.\n", fl_pad, pad);
350 m = V_INGPADBOUNDARY(M_INGPADBOUNDARY);
351 v = V_INGPADBOUNDARY(ilog2(pad) - 5);
352 t4_set_reg_field(sc, A_SGE_CONTROL, m, v);
355 if (fl_pack != -1 && fl_pack != pad) {
356 /* Complain but carry on. */
357 device_printf(sc->dev, "hw.cxgbe.fl_pack (%d) ignored,"
358 " using %d instead.\n", fl_pack, pad);
364 if (fl_pack < 16 || fl_pack == 32 || fl_pack > 4096 ||
365 !powerof2(fl_pack)) {
366 pack = max(sc->params.pci.mps, CACHE_LINE_SIZE);
367 MPASS(powerof2(pack));
375 device_printf(sc->dev, "Invalid hw.cxgbe.fl_pack value"
376 " (%d), using %d instead.\n", fl_pack, pack);
379 m = V_INGPACKBOUNDARY(M_INGPACKBOUNDARY);
381 v = V_INGPACKBOUNDARY(0);
383 v = V_INGPACKBOUNDARY(ilog2(pack) - 5);
385 MPASS(!is_t4(sc)); /* T4 doesn't have SGE_CONTROL2 */
386 t4_set_reg_field(sc, A_SGE_CONTROL2, m, v);
390 * adap->params.vpd.cclk must be set up before this is called.
393 t4_tweak_chip_settings(struct adapter *sc)
397 int intr_timer[SGE_NTIMERS] = {1, 5, 10, 50, 100, 200};
398 int timer_max = M_TIMERVALUE0 * 1000 / sc->params.vpd.cclk;
399 int intr_pktcount[SGE_NCOUNTERS] = {1, 8, 16, 32}; /* 63 max */
400 uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE);
401 static int sge_flbuf_sizes[] = {
403 #if MJUMPAGESIZE != MCLBYTES
405 MJUMPAGESIZE - CL_METADATA_SIZE,
406 MJUMPAGESIZE - 2 * MSIZE - CL_METADATA_SIZE,
410 MCLBYTES - MSIZE - CL_METADATA_SIZE,
411 MJUM9BYTES - CL_METADATA_SIZE,
412 MJUM16BYTES - CL_METADATA_SIZE,
415 KASSERT(sc->flags & MASTER_PF,
416 ("%s: trying to change chip settings when not master.", __func__));
418 m = V_PKTSHIFT(M_PKTSHIFT) | F_RXPKTCPLMODE | F_EGRSTATUSPAGESIZE;
419 v = V_PKTSHIFT(fl_pktshift) | F_RXPKTCPLMODE |
420 V_EGRSTATUSPAGESIZE(spg_len == 128);
421 t4_set_reg_field(sc, A_SGE_CONTROL, m, v);
423 setup_pad_and_pack_boundaries(sc);
425 v = V_HOSTPAGESIZEPF0(PAGE_SHIFT - 10) |
426 V_HOSTPAGESIZEPF1(PAGE_SHIFT - 10) |
427 V_HOSTPAGESIZEPF2(PAGE_SHIFT - 10) |
428 V_HOSTPAGESIZEPF3(PAGE_SHIFT - 10) |
429 V_HOSTPAGESIZEPF4(PAGE_SHIFT - 10) |
430 V_HOSTPAGESIZEPF5(PAGE_SHIFT - 10) |
431 V_HOSTPAGESIZEPF6(PAGE_SHIFT - 10) |
432 V_HOSTPAGESIZEPF7(PAGE_SHIFT - 10);
433 t4_write_reg(sc, A_SGE_HOST_PAGE_SIZE, v);
435 KASSERT(nitems(sge_flbuf_sizes) <= SGE_FLBUF_SIZES,
436 ("%s: hw buffer size table too big", __func__));
437 for (i = 0; i < min(nitems(sge_flbuf_sizes), SGE_FLBUF_SIZES); i++) {
438 t4_write_reg(sc, A_SGE_FL_BUFFER_SIZE0 + (4 * i),
442 v = V_THRESHOLD_0(intr_pktcount[0]) | V_THRESHOLD_1(intr_pktcount[1]) |
443 V_THRESHOLD_2(intr_pktcount[2]) | V_THRESHOLD_3(intr_pktcount[3]);
444 t4_write_reg(sc, A_SGE_INGRESS_RX_THRESHOLD, v);
446 KASSERT(intr_timer[0] <= timer_max,
447 ("%s: not a single usable timer (%d, %d)", __func__, intr_timer[0],
449 for (i = 1; i < nitems(intr_timer); i++) {
450 KASSERT(intr_timer[i] >= intr_timer[i - 1],
451 ("%s: timers not listed in increasing order (%d)",
454 while (intr_timer[i] > timer_max) {
455 if (i == nitems(intr_timer) - 1) {
456 intr_timer[i] = timer_max;
459 intr_timer[i] += intr_timer[i - 1];
464 v = V_TIMERVALUE0(us_to_core_ticks(sc, intr_timer[0])) |
465 V_TIMERVALUE1(us_to_core_ticks(sc, intr_timer[1]));
466 t4_write_reg(sc, A_SGE_TIMER_VALUE_0_AND_1, v);
467 v = V_TIMERVALUE2(us_to_core_ticks(sc, intr_timer[2])) |
468 V_TIMERVALUE3(us_to_core_ticks(sc, intr_timer[3]));
469 t4_write_reg(sc, A_SGE_TIMER_VALUE_2_AND_3, v);
470 v = V_TIMERVALUE4(us_to_core_ticks(sc, intr_timer[4])) |
471 V_TIMERVALUE5(us_to_core_ticks(sc, intr_timer[5]));
472 t4_write_reg(sc, A_SGE_TIMER_VALUE_4_AND_5, v);
474 /* 4K, 16K, 64K, 256K DDP "page sizes" */
475 v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6);
476 t4_write_reg(sc, A_ULP_RX_TDDP_PSZ, v);
478 m = v = F_TDDPTAGTCB;
479 t4_set_reg_field(sc, A_ULP_RX_CTL, m, v);
481 m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET |
483 v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET;
484 t4_set_reg_field(sc, A_TP_PARA_REG5, m, v);
488 * SGE wants the buffer to be at least 64B and then a multiple of 16. If
489 * padding is is use the buffer's start and end need to be aligned to the pad
490 * boundary as well. We'll just make sure that the size is a multiple of the
491 * boundary here, it is up to the buffer allocation code to make sure the start
492 * of the buffer is aligned as well.
495 hwsz_ok(struct adapter *sc, int hwsz)
497 int mask = fl_pad ? sc->sge.pad_boundary - 1 : 16 - 1;
499 return (hwsz >= 64 && (hwsz & mask) == 0);
503 * XXX: driver really should be able to deal with unexpected settings.
506 t4_read_chip_settings(struct adapter *sc)
508 struct sge *s = &sc->sge;
511 uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE);
512 static int sw_buf_sizes[] = { /* Sorted by size */
514 #if MJUMPAGESIZE != MCLBYTES
520 struct sw_zone_info *swz, *safe_swz;
521 struct hw_buf_info *hwb;
523 m = V_PKTSHIFT(M_PKTSHIFT) | F_RXPKTCPLMODE | F_EGRSTATUSPAGESIZE;
524 v = V_PKTSHIFT(fl_pktshift) | F_RXPKTCPLMODE |
525 V_EGRSTATUSPAGESIZE(spg_len == 128);
526 r = t4_read_reg(sc, A_SGE_CONTROL);
528 device_printf(sc->dev, "invalid SGE_CONTROL(0x%x)\n", r);
531 s->pad_boundary = 1 << (G_INGPADBOUNDARY(r) + 5);
534 s->pack_boundary = s->pad_boundary;
536 r = t4_read_reg(sc, A_SGE_CONTROL2);
537 if (G_INGPACKBOUNDARY(r) == 0)
538 s->pack_boundary = 16;
540 s->pack_boundary = 1 << (G_INGPACKBOUNDARY(r) + 5);
543 v = V_HOSTPAGESIZEPF0(PAGE_SHIFT - 10) |
544 V_HOSTPAGESIZEPF1(PAGE_SHIFT - 10) |
545 V_HOSTPAGESIZEPF2(PAGE_SHIFT - 10) |
546 V_HOSTPAGESIZEPF3(PAGE_SHIFT - 10) |
547 V_HOSTPAGESIZEPF4(PAGE_SHIFT - 10) |
548 V_HOSTPAGESIZEPF5(PAGE_SHIFT - 10) |
549 V_HOSTPAGESIZEPF6(PAGE_SHIFT - 10) |
550 V_HOSTPAGESIZEPF7(PAGE_SHIFT - 10);
551 r = t4_read_reg(sc, A_SGE_HOST_PAGE_SIZE);
553 device_printf(sc->dev, "invalid SGE_HOST_PAGE_SIZE(0x%x)\n", r);
557 /* Filter out unusable hw buffer sizes entirely (mark with -2). */
558 hwb = &s->hw_buf_info[0];
559 for (i = 0; i < nitems(s->hw_buf_info); i++, hwb++) {
560 r = t4_read_reg(sc, A_SGE_FL_BUFFER_SIZE0 + (4 * i));
562 hwb->zidx = hwsz_ok(sc, r) ? -1 : -2;
567 * Create a sorted list in decreasing order of hw buffer sizes (and so
568 * increasing order of spare area) for each software zone.
570 * If padding is enabled then the start and end of the buffer must align
571 * to the pad boundary; if packing is enabled then they must align with
572 * the pack boundary as well. Allocations from the cluster zones are
573 * aligned to min(size, 4K), so the buffer starts at that alignment and
574 * ends at hwb->size alignment. If mbuf inlining is allowed the
575 * starting alignment will be reduced to MSIZE and the driver will
576 * exercise appropriate caution when deciding on the best buffer layout
579 n = 0; /* no usable buffer size to begin with */
580 swz = &s->sw_zone_info[0];
582 for (i = 0; i < SW_ZONE_SIZES; i++, swz++) {
583 int8_t head = -1, tail = -1;
585 swz->size = sw_buf_sizes[i];
586 swz->zone = m_getzone(swz->size);
587 swz->type = m_gettype(swz->size);
589 if (swz->size < PAGE_SIZE) {
590 MPASS(powerof2(swz->size));
591 if (fl_pad && (swz->size % sc->sge.pad_boundary != 0))
595 if (swz->size == safest_rx_cluster)
598 hwb = &s->hw_buf_info[0];
599 for (j = 0; j < SGE_FLBUF_SIZES; j++, hwb++) {
600 if (hwb->zidx != -1 || hwb->size > swz->size)
604 MPASS(hwb->size % sc->sge.pad_boundary == 0);
609 else if (hwb->size < s->hw_buf_info[tail].size) {
610 s->hw_buf_info[tail].next = j;
614 struct hw_buf_info *t;
616 for (cur = &head; *cur != -1; cur = &t->next) {
617 t = &s->hw_buf_info[*cur];
618 if (hwb->size == t->size) {
622 if (hwb->size > t->size) {
630 swz->head_hwidx = head;
631 swz->tail_hwidx = tail;
635 if (swz->size - s->hw_buf_info[tail].size >=
637 sc->flags |= BUF_PACKING_OK;
641 device_printf(sc->dev, "no usable SGE FL buffer size.\n");
647 if (safe_swz != NULL) {
648 s->safe_hwidx1 = safe_swz->head_hwidx;
649 for (i = safe_swz->head_hwidx; i != -1; i = hwb->next) {
652 hwb = &s->hw_buf_info[i];
655 MPASS(hwb->size % sc->sge.pad_boundary == 0);
657 spare = safe_swz->size - hwb->size;
658 if (spare >= CL_METADATA_SIZE) {
665 r = t4_read_reg(sc, A_SGE_INGRESS_RX_THRESHOLD);
666 s->counter_val[0] = G_THRESHOLD_0(r);
667 s->counter_val[1] = G_THRESHOLD_1(r);
668 s->counter_val[2] = G_THRESHOLD_2(r);
669 s->counter_val[3] = G_THRESHOLD_3(r);
671 r = t4_read_reg(sc, A_SGE_TIMER_VALUE_0_AND_1);
672 s->timer_val[0] = G_TIMERVALUE0(r) / core_ticks_per_usec(sc);
673 s->timer_val[1] = G_TIMERVALUE1(r) / core_ticks_per_usec(sc);
674 r = t4_read_reg(sc, A_SGE_TIMER_VALUE_2_AND_3);
675 s->timer_val[2] = G_TIMERVALUE2(r) / core_ticks_per_usec(sc);
676 s->timer_val[3] = G_TIMERVALUE3(r) / core_ticks_per_usec(sc);
677 r = t4_read_reg(sc, A_SGE_TIMER_VALUE_4_AND_5);
678 s->timer_val[4] = G_TIMERVALUE4(r) / core_ticks_per_usec(sc);
679 s->timer_val[5] = G_TIMERVALUE5(r) / core_ticks_per_usec(sc);
681 v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6);
682 r = t4_read_reg(sc, A_ULP_RX_TDDP_PSZ);
684 device_printf(sc->dev, "invalid ULP_RX_TDDP_PSZ(0x%x)\n", r);
688 m = v = F_TDDPTAGTCB;
689 r = t4_read_reg(sc, A_ULP_RX_CTL);
691 device_printf(sc->dev, "invalid ULP_RX_CTL(0x%x)\n", r);
695 m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET |
697 v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET;
698 r = t4_read_reg(sc, A_TP_PARA_REG5);
700 device_printf(sc->dev, "invalid TP_PARA_REG5(0x%x)\n", r);
704 r = t4_read_reg(sc, A_SGE_CONM_CTRL);
705 s->fl_starve_threshold = G_EGRTHRESHOLD(r) * 2 + 1;
707 s->fl_starve_threshold2 = s->fl_starve_threshold;
709 s->fl_starve_threshold2 = G_EGRTHRESHOLDPACKING(r) * 2 + 1;
711 /* egress queues: log2 of # of doorbells per BAR2 page */
712 r = t4_read_reg(sc, A_SGE_EGRESS_QUEUES_PER_PAGE_PF);
713 r >>= S_QUEUESPERPAGEPF0 +
714 (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) * sc->pf;
715 s->eq_s_qpp = r & M_QUEUESPERPAGEPF0;
717 /* ingress queues: log2 of # of doorbells per BAR2 page */
718 r = t4_read_reg(sc, A_SGE_INGRESS_QUEUES_PER_PAGE_PF);
719 r >>= S_QUEUESPERPAGEPF0 +
720 (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) * sc->pf;
721 s->iq_s_qpp = r & M_QUEUESPERPAGEPF0;
723 t4_init_tp_params(sc);
725 t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
726 t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
732 t4_create_dma_tag(struct adapter *sc)
736 rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
737 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE,
738 BUS_SPACE_UNRESTRICTED, BUS_SPACE_MAXSIZE, BUS_DMA_ALLOCNOW, NULL,
741 device_printf(sc->dev,
742 "failed to create main DMA tag: %d\n", rc);
749 t4_sge_sysctls(struct adapter *sc, struct sysctl_ctx_list *ctx,
750 struct sysctl_oid_list *children)
753 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "buffer_sizes",
754 CTLTYPE_STRING | CTLFLAG_RD, &sc->sge, 0, sysctl_bufsizes, "A",
755 "freelist buffer sizes");
757 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pktshift", CTLFLAG_RD,
758 NULL, fl_pktshift, "payload DMA offset in rx buffer (bytes)");
760 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pad", CTLFLAG_RD,
761 NULL, sc->sge.pad_boundary, "payload pad boundary (bytes)");
763 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "spg_len", CTLFLAG_RD,
764 NULL, spg_len, "status page size (bytes)");
766 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_drop", CTLFLAG_RD,
767 NULL, cong_drop, "congestion drop setting");
769 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pack", CTLFLAG_RD,
770 NULL, sc->sge.pack_boundary, "payload pack boundary (bytes)");
774 t4_destroy_dma_tag(struct adapter *sc)
777 bus_dma_tag_destroy(sc->dmat);
783 * Allocate and initialize the firmware event queue and the management queue.
785 * Returns errno on failure. Resources allocated up to that point may still be
786 * allocated. Caller is responsible for cleanup in case this function fails.
789 t4_setup_adapter_queues(struct adapter *sc)
793 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
795 sysctl_ctx_init(&sc->ctx);
796 sc->flags |= ADAP_SYSCTL_CTX;
799 * Firmware event queue
806 * Management queue. This is just a control queue that uses the fwq as
809 rc = alloc_mgmtq(sc);
818 t4_teardown_adapter_queues(struct adapter *sc)
821 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
823 /* Do this before freeing the queue */
824 if (sc->flags & ADAP_SYSCTL_CTX) {
825 sysctl_ctx_free(&sc->ctx);
826 sc->flags &= ~ADAP_SYSCTL_CTX;
836 port_intr_count(struct port_info *pi)
840 if (pi->flags & INTR_RXQ)
843 if (pi->flags & INTR_OFLD_RXQ)
847 if (pi->flags & INTR_NM_RXQ)
854 first_vector(struct port_info *pi)
856 struct adapter *sc = pi->adapter;
857 int rc = T4_EXTRA_INTR, i;
859 if (sc->intr_count == 1)
862 for_each_port(sc, i) {
863 if (i == pi->port_id)
866 rc += port_intr_count(sc->port[i]);
873 * Given an arbitrary "index," come up with an iq that can be used by other
874 * queues (of this port) for interrupt forwarding, SGE egress updates, etc.
875 * The iq returned is guaranteed to be something that takes direct interrupts.
877 static struct sge_iq *
878 port_intr_iq(struct port_info *pi, int idx)
880 struct adapter *sc = pi->adapter;
881 struct sge *s = &sc->sge;
882 struct sge_iq *iq = NULL;
885 if (sc->intr_count == 1)
886 return (&sc->sge.fwq);
888 nintr = port_intr_count(pi);
890 ("%s: pi %p has no exclusive interrupts, total interrupts = %d",
891 __func__, pi, sc->intr_count));
893 /* Exclude netmap queues as they can't take anyone else's interrupts */
894 if (pi->flags & INTR_NM_RXQ)
897 ("%s: pi %p has nintr %d after netmap adjustment of %d", __func__,
898 pi, nintr, pi->nnmrxq));
902 if (pi->flags & INTR_RXQ) {
904 iq = &s->rxq[pi->first_rxq + i].iq;
910 if (pi->flags & INTR_OFLD_RXQ) {
911 if (i < pi->nofldrxq) {
912 iq = &s->ofld_rxq[pi->first_ofld_rxq + i].iq;
918 panic("%s: pi %p, intr_flags 0x%lx, idx %d, total intr %d\n", __func__,
919 pi, pi->flags & INTR_ALL, idx, nintr);
922 KASSERT(iq->flags & IQ_INTR,
923 ("%s: iq %p (port %p, intr_flags 0x%lx, idx %d)", __func__, iq, pi,
924 pi->flags & INTR_ALL, idx));
928 /* Maximum payload that can be delivered with a single iq descriptor */
930 mtu_to_max_payload(struct adapter *sc, int mtu, const int toe)
936 payload = sc->tt.rx_coalesce ?
937 G_RXCOALESCESIZE(t4_read_reg(sc, A_TP_PARA_REG2)) : mtu;
940 /* large enough even when hw VLAN extraction is disabled */
941 payload = fl_pktshift + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN +
951 t4_setup_port_queues(struct port_info *pi)
953 int rc = 0, i, j, intr_idx, iqid;
956 struct sge_wrq *ctrlq;
958 struct sge_ofld_rxq *ofld_rxq;
959 struct sge_wrq *ofld_txq;
962 struct sge_nm_rxq *nm_rxq;
963 struct sge_nm_txq *nm_txq;
966 struct adapter *sc = pi->adapter;
967 struct ifnet *ifp = pi->ifp;
968 struct sysctl_oid *oid = device_get_sysctl_tree(pi->dev);
969 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
970 int maxp, mtu = ifp->if_mtu;
972 /* Interrupt vector to start from (when using multiple vectors) */
973 intr_idx = first_vector(pi);
976 * First pass over all NIC and TOE rx queues:
977 * a) initialize iq and fl
978 * b) allocate queue iff it will take direct interrupts.
980 maxp = mtu_to_max_payload(sc, mtu, 0);
981 if (pi->flags & INTR_RXQ) {
982 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "rxq",
983 CTLFLAG_RD, NULL, "rx queues");
985 for_each_rxq(pi, i, rxq) {
987 init_iq(&rxq->iq, sc, pi->tmr_idx, pi->pktc_idx, pi->qsize_rxq);
989 snprintf(name, sizeof(name), "%s rxq%d-fl",
990 device_get_nameunit(pi->dev), i);
991 init_fl(sc, &rxq->fl, pi->qsize_rxq / 8, maxp, name);
993 if (pi->flags & INTR_RXQ) {
994 rxq->iq.flags |= IQ_INTR;
995 rc = alloc_rxq(pi, rxq, intr_idx, i, oid);
1002 maxp = mtu_to_max_payload(sc, mtu, 1);
1003 if (is_offload(sc) && pi->flags & INTR_OFLD_RXQ) {
1004 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ofld_rxq",
1006 "rx queues for offloaded TCP connections");
1008 for_each_ofld_rxq(pi, i, ofld_rxq) {
1010 init_iq(&ofld_rxq->iq, sc, pi->tmr_idx, pi->pktc_idx,
1013 snprintf(name, sizeof(name), "%s ofld_rxq%d-fl",
1014 device_get_nameunit(pi->dev), i);
1015 init_fl(sc, &ofld_rxq->fl, pi->qsize_rxq / 8, maxp, name);
1017 if (pi->flags & INTR_OFLD_RXQ) {
1018 ofld_rxq->iq.flags |= IQ_INTR;
1019 rc = alloc_ofld_rxq(pi, ofld_rxq, intr_idx, i, oid);
1028 * We don't have buffers to back the netmap rx queues right now so we
1029 * create the queues in a way that doesn't set off any congestion signal
1032 if (pi->flags & INTR_NM_RXQ) {
1033 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "nm_rxq",
1034 CTLFLAG_RD, NULL, "rx queues for netmap");
1035 for_each_nm_rxq(pi, i, nm_rxq) {
1036 rc = alloc_nm_rxq(pi, nm_rxq, intr_idx, i, oid);
1045 * Second pass over all NIC and TOE rx queues. The queues forwarding
1046 * their interrupts are allocated now.
1049 if (!(pi->flags & INTR_RXQ)) {
1050 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "rxq",
1051 CTLFLAG_RD, NULL, "rx queues");
1052 for_each_rxq(pi, i, rxq) {
1053 MPASS(!(rxq->iq.flags & IQ_INTR));
1055 intr_idx = port_intr_iq(pi, j)->abs_id;
1057 rc = alloc_rxq(pi, rxq, intr_idx, i, oid);
1064 if (is_offload(sc) && !(pi->flags & INTR_OFLD_RXQ)) {
1065 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ofld_rxq",
1067 "rx queues for offloaded TCP connections");
1068 for_each_ofld_rxq(pi, i, ofld_rxq) {
1069 MPASS(!(ofld_rxq->iq.flags & IQ_INTR));
1071 intr_idx = port_intr_iq(pi, j)->abs_id;
1073 rc = alloc_ofld_rxq(pi, ofld_rxq, intr_idx, i, oid);
1081 if (!(pi->flags & INTR_NM_RXQ))
1082 CXGBE_UNIMPLEMENTED(__func__);
1086 * Now the tx queues. Only one pass needed.
1088 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "txq", CTLFLAG_RD,
1091 for_each_txq(pi, i, txq) {
1092 iqid = port_intr_iq(pi, j)->cntxt_id;
1093 snprintf(name, sizeof(name), "%s txq%d",
1094 device_get_nameunit(pi->dev), i);
1095 init_eq(&txq->eq, EQ_ETH, pi->qsize_txq, pi->tx_chan, iqid,
1098 rc = alloc_txq(pi, txq, i, oid);
1104 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ofld_txq",
1105 CTLFLAG_RD, NULL, "tx queues for offloaded TCP connections");
1106 for_each_ofld_txq(pi, i, ofld_txq) {
1107 struct sysctl_oid *oid2;
1109 iqid = port_intr_iq(pi, j)->cntxt_id;
1110 snprintf(name, sizeof(name), "%s ofld_txq%d",
1111 device_get_nameunit(pi->dev), i);
1112 init_eq(&ofld_txq->eq, EQ_OFLD, pi->qsize_txq, pi->tx_chan,
1115 snprintf(name, sizeof(name), "%d", i);
1116 oid2 = SYSCTL_ADD_NODE(&pi->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1117 name, CTLFLAG_RD, NULL, "offload tx queue");
1119 rc = alloc_wrq(sc, pi, ofld_txq, oid2);
1126 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "nm_txq",
1127 CTLFLAG_RD, NULL, "tx queues for netmap use");
1128 for_each_nm_txq(pi, i, nm_txq) {
1129 iqid = pi->first_nm_rxq + (j % pi->nnmrxq);
1130 rc = alloc_nm_txq(pi, nm_txq, iqid, i, oid);
1138 * Finally, the control queue.
1140 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ctrlq", CTLFLAG_RD,
1141 NULL, "ctrl queue");
1142 ctrlq = &sc->sge.ctrlq[pi->port_id];
1143 iqid = port_intr_iq(pi, 0)->cntxt_id;
1144 snprintf(name, sizeof(name), "%s ctrlq", device_get_nameunit(pi->dev));
1145 init_eq(&ctrlq->eq, EQ_CTRL, CTRL_EQ_QSIZE, pi->tx_chan, iqid, name);
1146 rc = alloc_wrq(sc, pi, ctrlq, oid);
1150 t4_teardown_port_queues(pi);
1159 t4_teardown_port_queues(struct port_info *pi)
1162 struct adapter *sc = pi->adapter;
1163 struct sge_rxq *rxq;
1164 struct sge_txq *txq;
1166 struct sge_ofld_rxq *ofld_rxq;
1167 struct sge_wrq *ofld_txq;
1170 struct sge_nm_rxq *nm_rxq;
1171 struct sge_nm_txq *nm_txq;
1174 /* Do this before freeing the queues */
1175 if (pi->flags & PORT_SYSCTL_CTX) {
1176 sysctl_ctx_free(&pi->ctx);
1177 pi->flags &= ~PORT_SYSCTL_CTX;
1181 * Take down all the tx queues first, as they reference the rx queues
1182 * (for egress updates, etc.).
1185 free_wrq(sc, &sc->sge.ctrlq[pi->port_id]);
1187 for_each_txq(pi, i, txq) {
1191 for_each_ofld_txq(pi, i, ofld_txq) {
1192 free_wrq(sc, ofld_txq);
1196 for_each_nm_txq(pi, i, nm_txq)
1197 free_nm_txq(pi, nm_txq);
1201 * Then take down the rx queues that forward their interrupts, as they
1202 * reference other rx queues.
1205 for_each_rxq(pi, i, rxq) {
1206 if ((rxq->iq.flags & IQ_INTR) == 0)
1210 for_each_ofld_rxq(pi, i, ofld_rxq) {
1211 if ((ofld_rxq->iq.flags & IQ_INTR) == 0)
1212 free_ofld_rxq(pi, ofld_rxq);
1216 for_each_nm_rxq(pi, i, nm_rxq)
1217 free_nm_rxq(pi, nm_rxq);
1221 * Then take down the rx queues that take direct interrupts.
1224 for_each_rxq(pi, i, rxq) {
1225 if (rxq->iq.flags & IQ_INTR)
1229 for_each_ofld_rxq(pi, i, ofld_rxq) {
1230 if (ofld_rxq->iq.flags & IQ_INTR)
1231 free_ofld_rxq(pi, ofld_rxq);
1239 * Deals with errors and the firmware event queue. All data rx queues forward
1240 * their interrupt to the firmware event queue.
1243 t4_intr_all(void *arg)
1245 struct adapter *sc = arg;
1246 struct sge_iq *fwq = &sc->sge.fwq;
1249 if (atomic_cmpset_int(&fwq->state, IQS_IDLE, IQS_BUSY)) {
1251 atomic_cmpset_int(&fwq->state, IQS_BUSY, IQS_IDLE);
1255 /* Deals with error interrupts */
1257 t4_intr_err(void *arg)
1259 struct adapter *sc = arg;
1261 t4_write_reg(sc, MYPF_REG(A_PCIE_PF_CLI), 0);
1262 t4_slow_intr_handler(sc);
1266 t4_intr_evt(void *arg)
1268 struct sge_iq *iq = arg;
1270 if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) {
1272 atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE);
1279 struct sge_iq *iq = arg;
1281 if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) {
1283 atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE);
1288 * Deals with anything and everything on the given ingress queue.
1291 service_iq(struct sge_iq *iq, int budget)
1294 struct sge_rxq *rxq = iq_to_rxq(iq); /* Use iff iq is part of rxq */
1295 struct sge_fl *fl; /* Use iff IQ_HAS_FL */
1296 struct adapter *sc = iq->adapter;
1297 struct iq_desc *d = &iq->desc[iq->cidx];
1298 int ndescs = 0, limit;
1299 int rsp_type, refill;
1301 uint16_t fl_hw_cidx;
1303 STAILQ_HEAD(, sge_iq) iql = STAILQ_HEAD_INITIALIZER(iql);
1304 #if defined(INET) || defined(INET6)
1305 const struct timeval lro_timeout = {0, sc->lro_timeout};
1308 KASSERT(iq->state == IQS_BUSY, ("%s: iq %p not BUSY", __func__, iq));
1310 limit = budget ? budget : iq->qsize / 16;
1312 if (iq->flags & IQ_HAS_FL) {
1314 fl_hw_cidx = fl->hw_cidx; /* stable snapshot */
1317 fl_hw_cidx = 0; /* to silence gcc warning */
1321 * We always come back and check the descriptor ring for new indirect
1322 * interrupts and other responses after running a single handler.
1325 while ((d->rsp.u.type_gen & F_RSPD_GEN) == iq->gen) {
1331 rsp_type = G_RSPD_TYPE(d->rsp.u.type_gen);
1332 lq = be32toh(d->rsp.pldbuflen_qid);
1335 case X_RSPD_TYPE_FLBUF:
1337 KASSERT(iq->flags & IQ_HAS_FL,
1338 ("%s: data for an iq (%p) with no freelist",
1341 m0 = get_fl_payload(sc, fl, lq);
1342 if (__predict_false(m0 == NULL))
1344 refill = IDXDIFF(fl->hw_cidx, fl_hw_cidx, fl->sidx) > 2;
1345 #ifdef T4_PKT_TIMESTAMP
1347 * 60 bit timestamp for the payload is
1348 * *(uint64_t *)m0->m_pktdat. Note that it is
1349 * in the leading free-space in the mbuf. The
1350 * kernel can clobber it during a pullup,
1351 * m_copymdata, etc. You need to make sure that
1352 * the mbuf reaches you unmolested if you care
1353 * about the timestamp.
1355 *(uint64_t *)m0->m_pktdat =
1356 be64toh(ctrl->u.last_flit) &
1362 case X_RSPD_TYPE_CPL:
1363 KASSERT(d->rss.opcode < NUM_CPL_CMDS,
1364 ("%s: bad opcode %02x.", __func__,
1366 sc->cpl_handler[d->rss.opcode](iq, &d->rss, m0);
1369 case X_RSPD_TYPE_INTR:
1372 * Interrupts should be forwarded only to queues
1373 * that are not forwarding their interrupts.
1374 * This means service_iq can recurse but only 1
1377 KASSERT(budget == 0,
1378 ("%s: budget %u, rsp_type %u", __func__,
1382 * There are 1K interrupt-capable queues (qids 0
1383 * through 1023). A response type indicating a
1384 * forwarded interrupt with a qid >= 1K is an
1385 * iWARP async notification.
1388 sc->an_handler(iq, &d->rsp);
1392 q = sc->sge.iqmap[lq - sc->sge.iq_start];
1393 if (atomic_cmpset_int(&q->state, IQS_IDLE,
1395 if (service_iq(q, q->qsize / 16) == 0) {
1396 atomic_cmpset_int(&q->state,
1397 IQS_BUSY, IQS_IDLE);
1399 STAILQ_INSERT_TAIL(&iql, q,
1407 ("%s: illegal response type %d on iq %p",
1408 __func__, rsp_type, iq));
1410 "%s: illegal response type %d on iq %p",
1411 device_get_nameunit(sc->dev), rsp_type, iq);
1416 if (__predict_false(++iq->cidx == iq->sidx)) {
1418 iq->gen ^= F_RSPD_GEN;
1421 if (__predict_false(++ndescs == limit)) {
1422 t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS),
1424 V_INGRESSQID(iq->cntxt_id) |
1425 V_SEINTARM(V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX)));
1428 #if defined(INET) || defined(INET6)
1429 if (iq->flags & IQ_LRO_ENABLED &&
1430 sc->lro_timeout != 0) {
1431 tcp_lro_flush_inactive(&rxq->lro,
1437 if (iq->flags & IQ_HAS_FL) {
1439 refill_fl(sc, fl, 32);
1442 return (EINPROGRESS);
1447 refill_fl(sc, fl, 32);
1449 fl_hw_cidx = fl->hw_cidx;
1454 if (STAILQ_EMPTY(&iql))
1458 * Process the head only, and send it to the back of the list if
1459 * it's still not done.
1461 q = STAILQ_FIRST(&iql);
1462 STAILQ_REMOVE_HEAD(&iql, link);
1463 if (service_iq(q, q->qsize / 8) == 0)
1464 atomic_cmpset_int(&q->state, IQS_BUSY, IQS_IDLE);
1466 STAILQ_INSERT_TAIL(&iql, q, link);
1469 #if defined(INET) || defined(INET6)
1470 if (iq->flags & IQ_LRO_ENABLED) {
1471 struct lro_ctrl *lro = &rxq->lro;
1472 struct lro_entry *l;
1474 while (!SLIST_EMPTY(&lro->lro_active)) {
1475 l = SLIST_FIRST(&lro->lro_active);
1476 SLIST_REMOVE_HEAD(&lro->lro_active, next);
1477 tcp_lro_flush(lro, l);
1482 t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_CIDXINC(ndescs) |
1483 V_INGRESSQID((u32)iq->cntxt_id) | V_SEINTARM(iq->intr_params));
1485 if (iq->flags & IQ_HAS_FL) {
1489 starved = refill_fl(sc, fl, 64);
1491 if (__predict_false(starved != 0))
1492 add_fl_to_sfl(sc, fl);
1499 cl_has_metadata(struct sge_fl *fl, struct cluster_layout *cll)
1501 int rc = fl->flags & FL_BUF_PACKING || cll->region1 > 0;
1504 MPASS(cll->region3 >= CL_METADATA_SIZE);
1509 static inline struct cluster_metadata *
1510 cl_metadata(struct adapter *sc, struct sge_fl *fl, struct cluster_layout *cll,
1514 if (cl_has_metadata(fl, cll)) {
1515 struct sw_zone_info *swz = &sc->sge.sw_zone_info[cll->zidx];
1517 return ((struct cluster_metadata *)(cl + swz->size) - 1);
1523 rxb_free(struct mbuf *m, void *arg1, void *arg2)
1525 uma_zone_t zone = arg1;
1528 uma_zfree(zone, cl);
1529 counter_u64_add(extfree_rels, 1);
1531 return (EXT_FREE_OK);
1535 * The mbuf returned by this function could be allocated from zone_mbuf or
1536 * constructed in spare room in the cluster.
1538 * The mbuf carries the payload in one of these ways
1539 * a) frame inside the mbuf (mbuf from zone_mbuf)
1540 * b) m_cljset (for clusters without metadata) zone_mbuf
1541 * c) m_extaddref (cluster with metadata) inline mbuf
1542 * d) m_extaddref (cluster with metadata) zone_mbuf
1544 static struct mbuf *
1545 get_scatter_segment(struct adapter *sc, struct sge_fl *fl, int fr_offset,
1549 struct fl_sdesc *sd = &fl->sdesc[fl->cidx];
1550 struct cluster_layout *cll = &sd->cll;
1551 struct sw_zone_info *swz = &sc->sge.sw_zone_info[cll->zidx];
1552 struct hw_buf_info *hwb = &sc->sge.hw_buf_info[cll->hwidx];
1553 struct cluster_metadata *clm = cl_metadata(sc, fl, cll, sd->cl);
1557 blen = hwb->size - fl->rx_offset; /* max possible in this buf */
1558 len = min(remaining, blen);
1559 payload = sd->cl + cll->region1 + fl->rx_offset;
1560 if (fl->flags & FL_BUF_PACKING) {
1561 const u_int l = fr_offset + len;
1562 const u_int pad = roundup2(l, fl->buf_boundary) - l;
1564 if (fl->rx_offset + len + pad < hwb->size)
1566 MPASS(fl->rx_offset + blen <= hwb->size);
1568 MPASS(fl->rx_offset == 0); /* not packing */
1572 if (sc->sc_do_rxcopy && len < RX_COPY_THRESHOLD) {
1575 * Copy payload into a freshly allocated mbuf.
1578 m = fr_offset == 0 ?
1579 m_gethdr(M_NOWAIT, MT_DATA) : m_get(M_NOWAIT, MT_DATA);
1582 fl->mbuf_allocated++;
1583 #ifdef T4_PKT_TIMESTAMP
1584 /* Leave room for a timestamp */
1587 /* copy data to mbuf */
1588 bcopy(payload, mtod(m, caddr_t), len);
1590 } else if (sd->nmbuf * MSIZE < cll->region1) {
1593 * There's spare room in the cluster for an mbuf. Create one
1594 * and associate it with the payload that's in the cluster.
1598 m = (struct mbuf *)(sd->cl + sd->nmbuf * MSIZE);
1599 /* No bzero required */
1600 if (m_init(m, NULL, 0, M_NOWAIT, MT_DATA,
1601 fr_offset == 0 ? M_PKTHDR | M_NOFREE : M_NOFREE))
1604 m_extaddref(m, payload, blen, &clm->refcount, rxb_free,
1606 if (sd->nmbuf++ == 0)
1607 counter_u64_add(extfree_refs, 1);
1612 * Grab an mbuf from zone_mbuf and associate it with the
1613 * payload in the cluster.
1616 m = fr_offset == 0 ?
1617 m_gethdr(M_NOWAIT, MT_DATA) : m_get(M_NOWAIT, MT_DATA);
1620 fl->mbuf_allocated++;
1622 m_extaddref(m, payload, blen, &clm->refcount,
1623 rxb_free, swz->zone, sd->cl);
1624 if (sd->nmbuf++ == 0)
1625 counter_u64_add(extfree_refs, 1);
1627 m_cljset(m, sd->cl, swz->type);
1628 sd->cl = NULL; /* consumed, not a recycle candidate */
1632 m->m_pkthdr.len = remaining;
1635 if (fl->flags & FL_BUF_PACKING) {
1636 fl->rx_offset += blen;
1637 MPASS(fl->rx_offset <= hwb->size);
1638 if (fl->rx_offset < hwb->size)
1639 return (m); /* without advancing the cidx */
1642 if (__predict_false(++fl->cidx % 8 == 0)) {
1643 uint16_t cidx = fl->cidx / 8;
1645 if (__predict_false(cidx == fl->sidx))
1646 fl->cidx = cidx = 0;
1654 static struct mbuf *
1655 get_fl_payload(struct adapter *sc, struct sge_fl *fl, uint32_t len_newbuf)
1657 struct mbuf *m0, *m, **pnext;
1659 const u_int total = G_RSPD_LEN(len_newbuf);
1661 if (__predict_false(fl->flags & FL_BUF_RESUME)) {
1662 M_ASSERTPKTHDR(fl->m0);
1663 MPASS(fl->m0->m_pkthdr.len == total);
1664 MPASS(fl->remaining < total);
1668 remaining = fl->remaining;
1669 fl->flags &= ~FL_BUF_RESUME;
1673 if (fl->rx_offset > 0 && len_newbuf & F_RSPD_NEWBUF) {
1675 if (__predict_false(++fl->cidx % 8 == 0)) {
1676 uint16_t cidx = fl->cidx / 8;
1678 if (__predict_false(cidx == fl->sidx))
1679 fl->cidx = cidx = 0;
1685 * Payload starts at rx_offset in the current hw buffer. Its length is
1686 * 'len' and it may span multiple hw buffers.
1689 m0 = get_scatter_segment(sc, fl, 0, total);
1692 remaining = total - m0->m_len;
1693 pnext = &m0->m_next;
1694 while (remaining > 0) {
1696 MPASS(fl->rx_offset == 0);
1697 m = get_scatter_segment(sc, fl, total - remaining, remaining);
1698 if (__predict_false(m == NULL)) {
1701 fl->remaining = remaining;
1702 fl->flags |= FL_BUF_RESUME;
1707 remaining -= m->m_len;
1716 t4_eth_rx(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m0)
1718 struct sge_rxq *rxq = iq_to_rxq(iq);
1719 struct ifnet *ifp = rxq->ifp;
1720 const struct cpl_rx_pkt *cpl = (const void *)(rss + 1);
1721 #if defined(INET) || defined(INET6)
1722 struct lro_ctrl *lro = &rxq->lro;
1725 KASSERT(m0 != NULL, ("%s: no payload with opcode %02x", __func__,
1728 m0->m_pkthdr.len -= fl_pktshift;
1729 m0->m_len -= fl_pktshift;
1730 m0->m_data += fl_pktshift;
1732 m0->m_pkthdr.rcvif = ifp;
1733 M_HASHTYPE_SET(m0, M_HASHTYPE_OPAQUE);
1734 m0->m_pkthdr.flowid = be32toh(rss->hash_val);
1736 if (cpl->csum_calc && !cpl->err_vec) {
1737 if (ifp->if_capenable & IFCAP_RXCSUM &&
1738 cpl->l2info & htobe32(F_RXF_IP)) {
1739 m0->m_pkthdr.csum_flags = (CSUM_IP_CHECKED |
1740 CSUM_IP_VALID | CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1742 } else if (ifp->if_capenable & IFCAP_RXCSUM_IPV6 &&
1743 cpl->l2info & htobe32(F_RXF_IP6)) {
1744 m0->m_pkthdr.csum_flags = (CSUM_DATA_VALID_IPV6 |
1749 if (__predict_false(cpl->ip_frag))
1750 m0->m_pkthdr.csum_data = be16toh(cpl->csum);
1752 m0->m_pkthdr.csum_data = 0xffff;
1756 m0->m_pkthdr.ether_vtag = be16toh(cpl->vlan);
1757 m0->m_flags |= M_VLANTAG;
1758 rxq->vlan_extraction++;
1761 #if defined(INET) || defined(INET6)
1762 if (cpl->l2info & htobe32(F_RXF_LRO) &&
1763 iq->flags & IQ_LRO_ENABLED &&
1764 tcp_lro_rx(lro, m0, 0) == 0) {
1765 /* queued for LRO */
1768 ifp->if_input(ifp, m0);
1774 * Must drain the wrq or make sure that someone else will.
1777 wrq_tx_drain(void *arg, int n)
1779 struct sge_wrq *wrq = arg;
1780 struct sge_eq *eq = &wrq->eq;
1783 if (TAILQ_EMPTY(&wrq->incomplete_wrs) && !STAILQ_EMPTY(&wrq->wr_list))
1784 drain_wrq_wr_list(wrq->adapter, wrq);
1789 drain_wrq_wr_list(struct adapter *sc, struct sge_wrq *wrq)
1791 struct sge_eq *eq = &wrq->eq;
1792 u_int available, dbdiff; /* # of hardware descriptors */
1795 struct fw_eth_tx_pkt_wr *dst; /* any fw WR struct will do */
1797 EQ_LOCK_ASSERT_OWNED(eq);
1798 MPASS(TAILQ_EMPTY(&wrq->incomplete_wrs));
1799 wr = STAILQ_FIRST(&wrq->wr_list);
1800 MPASS(wr != NULL); /* Must be called with something useful to do */
1801 dbdiff = IDXDIFF(eq->pidx, eq->dbidx, eq->sidx);
1804 eq->cidx = read_hw_cidx(eq);
1805 if (eq->pidx == eq->cidx)
1806 available = eq->sidx - 1;
1808 available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
1810 MPASS(wr->wrq == wrq);
1811 n = howmany(wr->wr_len, EQ_ESIZE);
1815 dst = (void *)&eq->desc[eq->pidx];
1816 if (__predict_true(eq->sidx - eq->pidx > n)) {
1817 /* Won't wrap, won't end exactly at the status page. */
1818 bcopy(&wr->wr[0], dst, wr->wr_len);
1821 int first_portion = (eq->sidx - eq->pidx) * EQ_ESIZE;
1823 bcopy(&wr->wr[0], dst, first_portion);
1824 if (wr->wr_len > first_portion) {
1825 bcopy(&wr->wr[first_portion], &eq->desc[0],
1826 wr->wr_len - first_portion);
1828 eq->pidx = n - (eq->sidx - eq->pidx);
1831 if (available < eq->sidx / 4 &&
1832 atomic_cmpset_int(&eq->equiq, 0, 1)) {
1833 dst->equiq_to_len16 |= htobe32(F_FW_WR_EQUIQ |
1835 eq->equeqidx = eq->pidx;
1836 } else if (IDXDIFF(eq->pidx, eq->equeqidx, eq->sidx) >= 32) {
1837 dst->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ);
1838 eq->equeqidx = eq->pidx;
1843 ring_eq_db(sc, eq, dbdiff);
1847 STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
1849 MPASS(wrq->nwr_pending > 0);
1851 MPASS(wrq->ndesc_needed >= n);
1852 wrq->ndesc_needed -= n;
1853 } while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL);
1856 ring_eq_db(sc, eq, dbdiff);
1860 * Doesn't fail. Holds on to work requests it can't send right away.
1863 t4_wrq_tx_locked(struct adapter *sc, struct sge_wrq *wrq, struct wrqe *wr)
1866 struct sge_eq *eq = &wrq->eq;
1869 EQ_LOCK_ASSERT_OWNED(eq);
1871 MPASS(wr->wr_len > 0 && wr->wr_len <= SGE_MAX_WR_LEN);
1872 MPASS((wr->wr_len & 0x7) == 0);
1874 STAILQ_INSERT_TAIL(&wrq->wr_list, wr, link);
1876 wrq->ndesc_needed += howmany(wr->wr_len, EQ_ESIZE);
1878 if (!TAILQ_EMPTY(&wrq->incomplete_wrs))
1879 return; /* commit_wrq_wr will drain wr_list as well. */
1881 drain_wrq_wr_list(sc, wrq);
1883 /* Doorbell must have caught up to the pidx. */
1884 MPASS(eq->pidx == eq->dbidx);
1888 t4_update_fl_bufsize(struct ifnet *ifp)
1890 struct port_info *pi = ifp->if_softc;
1891 struct adapter *sc = pi->adapter;
1892 struct sge_rxq *rxq;
1894 struct sge_ofld_rxq *ofld_rxq;
1897 int i, maxp, mtu = ifp->if_mtu;
1899 maxp = mtu_to_max_payload(sc, mtu, 0);
1900 for_each_rxq(pi, i, rxq) {
1904 find_best_refill_source(sc, fl, maxp);
1908 maxp = mtu_to_max_payload(sc, mtu, 1);
1909 for_each_ofld_rxq(pi, i, ofld_rxq) {
1913 find_best_refill_source(sc, fl, maxp);
1920 mbuf_nsegs(struct mbuf *m)
1924 KASSERT(m->m_pkthdr.l5hlen > 0,
1925 ("%s: mbuf %p missing information on # of segments.", __func__, m));
1927 return (m->m_pkthdr.l5hlen);
1931 set_mbuf_nsegs(struct mbuf *m, uint8_t nsegs)
1935 m->m_pkthdr.l5hlen = nsegs;
1939 mbuf_len16(struct mbuf *m)
1944 n = m->m_pkthdr.PH_loc.eigth[0];
1945 MPASS(n > 0 && n <= SGE_MAX_WR_LEN / 16);
1951 set_mbuf_len16(struct mbuf *m, uint8_t len16)
1955 m->m_pkthdr.PH_loc.eigth[0] = len16;
1959 needs_tso(struct mbuf *m)
1964 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1965 KASSERT(m->m_pkthdr.tso_segsz > 0,
1966 ("%s: TSO requested in mbuf %p but MSS not provided",
1975 needs_l3_csum(struct mbuf *m)
1980 if (m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TSO))
1986 needs_l4_csum(struct mbuf *m)
1991 if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP | CSUM_UDP_IPV6 |
1992 CSUM_TCP_IPV6 | CSUM_TSO))
1998 needs_vlan_insertion(struct mbuf *m)
2003 if (m->m_flags & M_VLANTAG) {
2004 KASSERT(m->m_pkthdr.ether_vtag != 0,
2005 ("%s: HWVLAN requested in mbuf %p but tag not provided",
2013 m_advance(struct mbuf **pm, int *poffset, int len)
2015 struct mbuf *m = *pm;
2016 int offset = *poffset;
2022 if (offset + len < m->m_len) {
2024 p = mtod(m, uintptr_t) + offset;
2027 len -= m->m_len - offset;
2038 same_paddr(char *a, char *b)
2043 else if (a != NULL && b != NULL) {
2044 vm_offset_t x = (vm_offset_t)a;
2045 vm_offset_t y = (vm_offset_t)b;
2047 if ((x & PAGE_MASK) == (y & PAGE_MASK) &&
2048 pmap_kextract(x) == pmap_kextract(y))
2056 * Can deal with empty mbufs in the chain that have m_len = 0, but the chain
2057 * must have at least one mbuf that's not empty.
2060 count_mbuf_nsegs(struct mbuf *m)
2062 char *prev_end, *start;
2069 for (; m; m = m->m_next) {
2072 if (__predict_false(len == 0))
2074 start = mtod(m, char *);
2076 nsegs += sglist_count(start, len);
2077 if (same_paddr(prev_end, start))
2079 prev_end = start + len;
2087 * Analyze the mbuf to determine its tx needs. The mbuf passed in may change:
2088 * a) caller can assume it's been freed if this function returns with an error.
2089 * b) it may get defragged up if the gather list is too long for the hardware.
2092 parse_pkt(struct mbuf **mp)
2094 struct mbuf *m0 = *mp, *m;
2095 int rc, nsegs, defragged = 0, offset;
2096 struct ether_header *eh;
2098 #if defined(INET) || defined(INET6)
2104 if (__predict_false(m0->m_pkthdr.len < ETHER_HDR_LEN)) {
2113 * First count the number of gather list segments in the payload.
2114 * Defrag the mbuf if nsegs exceeds the hardware limit.
2117 MPASS(m0->m_pkthdr.len > 0);
2118 nsegs = count_mbuf_nsegs(m0);
2119 if (nsegs > (needs_tso(m0) ? TX_SGL_SEGS_TSO : TX_SGL_SEGS)) {
2120 if (defragged++ > 0 || (m = m_defrag(m0, M_NOWAIT)) == NULL) {
2124 *mp = m0 = m; /* update caller's copy after defrag */
2128 if (__predict_false(nsegs > 2 && m0->m_pkthdr.len <= MHLEN)) {
2129 m0 = m_pullup(m0, m0->m_pkthdr.len);
2131 /* Should have left well enough alone. */
2135 *mp = m0; /* update caller's copy after pullup */
2138 set_mbuf_nsegs(m0, nsegs);
2139 set_mbuf_len16(m0, txpkt_len16(nsegs, needs_tso(m0)));
2145 eh = mtod(m, struct ether_header *);
2146 eh_type = ntohs(eh->ether_type);
2147 if (eh_type == ETHERTYPE_VLAN) {
2148 struct ether_vlan_header *evh = (void *)eh;
2150 eh_type = ntohs(evh->evl_proto);
2151 m0->m_pkthdr.l2hlen = sizeof(*evh);
2153 m0->m_pkthdr.l2hlen = sizeof(*eh);
2156 l3hdr = m_advance(&m, &offset, m0->m_pkthdr.l2hlen);
2160 case ETHERTYPE_IPV6:
2162 struct ip6_hdr *ip6 = l3hdr;
2164 MPASS(ip6->ip6_nxt == IPPROTO_TCP);
2166 m0->m_pkthdr.l3hlen = sizeof(*ip6);
2173 struct ip *ip = l3hdr;
2175 m0->m_pkthdr.l3hlen = ip->ip_hl * 4;
2180 panic("%s: ethertype 0x%04x unknown. if_cxgbe must be compiled"
2181 " with the same INET/INET6 options as the kernel.",
2185 #if defined(INET) || defined(INET6)
2186 tcp = m_advance(&m, &offset, m0->m_pkthdr.l3hlen);
2187 m0->m_pkthdr.l4hlen = tcp->th_off * 4;
2194 start_wrq_wr(struct sge_wrq *wrq, int len16, struct wrq_cookie *cookie)
2196 struct sge_eq *eq = &wrq->eq;
2197 struct adapter *sc = wrq->adapter;
2198 int ndesc, available;
2203 ndesc = howmany(len16, EQ_ESIZE / 16);
2204 MPASS(ndesc > 0 && ndesc <= SGE_MAX_WR_NDESC);
2208 if (!STAILQ_EMPTY(&wrq->wr_list))
2209 drain_wrq_wr_list(sc, wrq);
2211 if (!STAILQ_EMPTY(&wrq->wr_list)) {
2214 wr = alloc_wrqe(len16 * 16, wrq);
2215 if (__predict_false(wr == NULL))
2218 cookie->ndesc = ndesc;
2222 eq->cidx = read_hw_cidx(eq);
2223 if (eq->pidx == eq->cidx)
2224 available = eq->sidx - 1;
2226 available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
2227 if (available < ndesc)
2230 cookie->pidx = eq->pidx;
2231 cookie->ndesc = ndesc;
2232 TAILQ_INSERT_TAIL(&wrq->incomplete_wrs, cookie, link);
2234 w = &eq->desc[eq->pidx];
2235 IDXINCR(eq->pidx, ndesc, eq->sidx);
2236 if (__predict_false(eq->pidx < ndesc - 1)) {
2238 wrq->ss_pidx = cookie->pidx;
2239 wrq->ss_len = len16 * 16;
2248 commit_wrq_wr(struct sge_wrq *wrq, void *w, struct wrq_cookie *cookie)
2250 struct sge_eq *eq = &wrq->eq;
2251 struct adapter *sc = wrq->adapter;
2253 struct wrq_cookie *prev, *next;
2255 if (cookie->pidx == -1) {
2256 struct wrqe *wr = __containerof(w, struct wrqe, wr);
2262 ndesc = cookie->ndesc; /* Can be more than SGE_MAX_WR_NDESC here. */
2263 pidx = cookie->pidx;
2264 MPASS(pidx >= 0 && pidx < eq->sidx);
2265 if (__predict_false(w == &wrq->ss[0])) {
2266 int n = (eq->sidx - wrq->ss_pidx) * EQ_ESIZE;
2268 MPASS(wrq->ss_len > n); /* WR had better wrap around. */
2269 bcopy(&wrq->ss[0], &eq->desc[wrq->ss_pidx], n);
2270 bcopy(&wrq->ss[n], &eq->desc[0], wrq->ss_len - n);
2273 wrq->tx_wrs_direct++;
2276 prev = TAILQ_PREV(cookie, wrq_incomplete_wrs, link);
2277 next = TAILQ_NEXT(cookie, link);
2279 MPASS(pidx == eq->dbidx);
2280 if (next == NULL || ndesc >= 16)
2281 ring_eq_db(wrq->adapter, eq, ndesc);
2283 MPASS(IDXDIFF(next->pidx, pidx, eq->sidx) == ndesc);
2285 next->ndesc += ndesc;
2288 MPASS(IDXDIFF(pidx, prev->pidx, eq->sidx) == prev->ndesc);
2289 prev->ndesc += ndesc;
2291 TAILQ_REMOVE(&wrq->incomplete_wrs, cookie, link);
2293 if (TAILQ_EMPTY(&wrq->incomplete_wrs) && !STAILQ_EMPTY(&wrq->wr_list))
2294 drain_wrq_wr_list(sc, wrq);
2297 if (TAILQ_EMPTY(&wrq->incomplete_wrs)) {
2298 /* Doorbell must have caught up to the pidx. */
2299 MPASS(wrq->eq.pidx == wrq->eq.dbidx);
2306 can_resume_eth_tx(struct mp_ring *r)
2308 struct sge_eq *eq = r->cookie;
2310 return (total_available_tx_desc(eq) > eq->sidx / 8);
2314 cannot_use_txpkts(struct mbuf *m)
2316 /* maybe put a GL limit too, to avoid silliness? */
2318 return (needs_tso(m));
2322 * r->items[cidx] to r->items[pidx], with a wraparound at r->size, are ready to
2323 * be consumed. Return the actual number consumed. 0 indicates a stall.
2326 eth_tx(struct mp_ring *r, u_int cidx, u_int pidx)
2328 struct sge_txq *txq = r->cookie;
2329 struct sge_eq *eq = &txq->eq;
2330 struct ifnet *ifp = txq->ifp;
2331 struct port_info *pi = (void *)ifp->if_softc;
2332 struct adapter *sc = pi->adapter;
2333 u_int total, remaining; /* # of packets */
2334 u_int available, dbdiff; /* # of hardware descriptors */
2336 struct mbuf *m0, *tail;
2338 struct fw_eth_tx_pkts_wr *wr; /* any fw WR struct will do */
2340 remaining = IDXDIFF(pidx, cidx, r->size);
2341 MPASS(remaining > 0); /* Must not be called without work to do. */
2345 if (__predict_false((eq->flags & EQ_ENABLED) == 0)) {
2346 while (cidx != pidx) {
2347 m0 = r->items[cidx];
2349 if (++cidx == r->size)
2352 reclaim_tx_descs(txq, 2048);
2357 /* How many hardware descriptors do we have readily available. */
2358 if (eq->pidx == eq->cidx)
2359 available = eq->sidx - 1;
2361 available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
2362 dbdiff = IDXDIFF(eq->pidx, eq->dbidx, eq->sidx);
2364 while (remaining > 0) {
2366 m0 = r->items[cidx];
2368 MPASS(m0->m_nextpkt == NULL);
2370 if (available < SGE_MAX_WR_NDESC) {
2371 available += reclaim_tx_descs(txq, 64);
2372 if (available < howmany(mbuf_len16(m0), EQ_ESIZE / 16))
2373 break; /* out of descriptors */
2376 next_cidx = cidx + 1;
2377 if (__predict_false(next_cidx == r->size))
2380 wr = (void *)&eq->desc[eq->pidx];
2381 if (remaining > 1 &&
2382 try_txpkts(m0, r->items[next_cidx], &txp, available) == 0) {
2384 /* pkts at cidx, next_cidx should both be in txp. */
2385 MPASS(txp.npkt == 2);
2386 tail = r->items[next_cidx];
2387 MPASS(tail->m_nextpkt == NULL);
2388 ETHER_BPF_MTAP(ifp, m0);
2389 ETHER_BPF_MTAP(ifp, tail);
2390 m0->m_nextpkt = tail;
2392 if (__predict_false(++next_cidx == r->size))
2395 while (next_cidx != pidx) {
2396 if (add_to_txpkts(r->items[next_cidx], &txp,
2399 tail->m_nextpkt = r->items[next_cidx];
2400 tail = tail->m_nextpkt;
2401 ETHER_BPF_MTAP(ifp, tail);
2402 if (__predict_false(++next_cidx == r->size))
2406 n = write_txpkts_wr(txq, wr, m0, &txp, available);
2408 remaining -= txp.npkt;
2412 n = write_txpkt_wr(txq, (void *)wr, m0, available);
2413 ETHER_BPF_MTAP(ifp, m0);
2415 MPASS(n >= 1 && n <= available && n <= SGE_MAX_WR_NDESC);
2419 IDXINCR(eq->pidx, n, eq->sidx);
2421 if (total_available_tx_desc(eq) < eq->sidx / 4 &&
2422 atomic_cmpset_int(&eq->equiq, 0, 1)) {
2423 wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUIQ |
2425 eq->equeqidx = eq->pidx;
2426 } else if (IDXDIFF(eq->pidx, eq->equeqidx, eq->sidx) >= 32) {
2427 wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ);
2428 eq->equeqidx = eq->pidx;
2431 if (dbdiff >= 16 && remaining >= 4) {
2432 ring_eq_db(sc, eq, dbdiff);
2433 available += reclaim_tx_descs(txq, 4 * dbdiff);
2440 ring_eq_db(sc, eq, dbdiff);
2441 reclaim_tx_descs(txq, 32);
2450 init_iq(struct sge_iq *iq, struct adapter *sc, int tmr_idx, int pktc_idx,
2454 KASSERT(tmr_idx >= 0 && tmr_idx < SGE_NTIMERS,
2455 ("%s: bad tmr_idx %d", __func__, tmr_idx));
2456 KASSERT(pktc_idx < SGE_NCOUNTERS, /* -ve is ok, means don't use */
2457 ("%s: bad pktc_idx %d", __func__, pktc_idx));
2461 iq->intr_params = V_QINTR_TIMER_IDX(tmr_idx);
2462 iq->intr_pktc_idx = SGE_NCOUNTERS - 1;
2463 if (pktc_idx >= 0) {
2464 iq->intr_params |= F_QINTR_CNT_EN;
2465 iq->intr_pktc_idx = pktc_idx;
2467 iq->qsize = roundup2(qsize, 16); /* See FW_IQ_CMD/iqsize */
2468 iq->sidx = iq->qsize - spg_len / IQ_ESIZE;
2472 init_fl(struct adapter *sc, struct sge_fl *fl, int qsize, int maxp, char *name)
2476 fl->sidx = qsize - spg_len / EQ_ESIZE;
2477 strlcpy(fl->lockname, name, sizeof(fl->lockname));
2478 if (sc->flags & BUF_PACKING_OK &&
2479 ((!is_t4(sc) && buffer_packing) || /* T5+: enabled unless 0 */
2480 (is_t4(sc) && buffer_packing == 1)))/* T4: disabled unless 1 */
2481 fl->flags |= FL_BUF_PACKING;
2482 find_best_refill_source(sc, fl, maxp);
2483 find_safe_refill_source(sc, fl);
2487 init_eq(struct sge_eq *eq, int eqtype, int qsize, uint8_t tx_chan,
2488 uint16_t iqid, char *name)
2490 KASSERT(tx_chan < NCHAN, ("%s: bad tx channel %d", __func__, tx_chan));
2491 KASSERT(eqtype <= EQ_TYPEMASK, ("%s: bad qtype %d", __func__, eqtype));
2493 eq->flags = eqtype & EQ_TYPEMASK;
2494 eq->tx_chan = tx_chan;
2496 eq->sidx = qsize - spg_len / EQ_ESIZE;
2497 strlcpy(eq->lockname, name, sizeof(eq->lockname));
2501 alloc_ring(struct adapter *sc, size_t len, bus_dma_tag_t *tag,
2502 bus_dmamap_t *map, bus_addr_t *pa, void **va)
2506 rc = bus_dma_tag_create(sc->dmat, 512, 0, BUS_SPACE_MAXADDR,
2507 BUS_SPACE_MAXADDR, NULL, NULL, len, 1, len, 0, NULL, NULL, tag);
2509 device_printf(sc->dev, "cannot allocate DMA tag: %d\n", rc);
2513 rc = bus_dmamem_alloc(*tag, va,
2514 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, map);
2516 device_printf(sc->dev, "cannot allocate DMA memory: %d\n", rc);
2520 rc = bus_dmamap_load(*tag, *map, *va, len, oneseg_dma_callback, pa, 0);
2522 device_printf(sc->dev, "cannot load DMA map: %d\n", rc);
2527 free_ring(sc, *tag, *map, *pa, *va);
2533 free_ring(struct adapter *sc, bus_dma_tag_t tag, bus_dmamap_t map,
2534 bus_addr_t pa, void *va)
2537 bus_dmamap_unload(tag, map);
2539 bus_dmamem_free(tag, va, map);
2541 bus_dma_tag_destroy(tag);
2547 * Allocates the ring for an ingress queue and an optional freelist. If the
2548 * freelist is specified it will be allocated and then associated with the
2551 * Returns errno on failure. Resources allocated up to that point may still be
2552 * allocated. Caller is responsible for cleanup in case this function fails.
2554 * If the ingress queue will take interrupts directly (iq->flags & IQ_INTR) then
2555 * the intr_idx specifies the vector, starting from 0. Otherwise it specifies
2556 * the abs_id of the ingress queue to which its interrupts should be forwarded.
2559 alloc_iq_fl(struct port_info *pi, struct sge_iq *iq, struct sge_fl *fl,
2560 int intr_idx, int cong)
2562 int rc, i, cntxt_id;
2565 struct adapter *sc = iq->adapter;
2568 len = iq->qsize * IQ_ESIZE;
2569 rc = alloc_ring(sc, len, &iq->desc_tag, &iq->desc_map, &iq->ba,
2570 (void **)&iq->desc);
2574 bzero(&c, sizeof(c));
2575 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_IQ_CMD) | F_FW_CMD_REQUEST |
2576 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_IQ_CMD_PFN(sc->pf) |
2577 V_FW_IQ_CMD_VFN(0));
2579 c.alloc_to_len16 = htobe32(F_FW_IQ_CMD_ALLOC | F_FW_IQ_CMD_IQSTART |
2582 /* Special handling for firmware event queue */
2583 if (iq == &sc->sge.fwq)
2584 v |= F_FW_IQ_CMD_IQASYNCH;
2586 if (iq->flags & IQ_INTR) {
2587 KASSERT(intr_idx < sc->intr_count,
2588 ("%s: invalid direct intr_idx %d", __func__, intr_idx));
2590 v |= F_FW_IQ_CMD_IQANDST;
2591 v |= V_FW_IQ_CMD_IQANDSTINDEX(intr_idx);
2593 c.type_to_iqandstindex = htobe32(v |
2594 V_FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
2595 V_FW_IQ_CMD_VIID(pi->viid) |
2596 V_FW_IQ_CMD_IQANUD(X_UPDATEDELIVERY_INTERRUPT));
2597 c.iqdroprss_to_iqesize = htobe16(V_FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
2598 F_FW_IQ_CMD_IQGTSMODE |
2599 V_FW_IQ_CMD_IQINTCNTTHRESH(iq->intr_pktc_idx) |
2600 V_FW_IQ_CMD_IQESIZE(ilog2(IQ_ESIZE) - 4));
2601 c.iqsize = htobe16(iq->qsize);
2602 c.iqaddr = htobe64(iq->ba);
2604 c.iqns_to_fl0congen = htobe32(F_FW_IQ_CMD_IQFLINTCONGEN);
2607 mtx_init(&fl->fl_lock, fl->lockname, NULL, MTX_DEF);
2609 len = fl->qsize * EQ_ESIZE;
2610 rc = alloc_ring(sc, len, &fl->desc_tag, &fl->desc_map,
2611 &fl->ba, (void **)&fl->desc);
2615 /* Allocate space for one software descriptor per buffer. */
2616 rc = alloc_fl_sdesc(fl);
2618 device_printf(sc->dev,
2619 "failed to setup fl software descriptors: %d\n",
2624 if (fl->flags & FL_BUF_PACKING) {
2625 fl->lowat = roundup2(sc->sge.fl_starve_threshold2, 8);
2626 fl->buf_boundary = sc->sge.pack_boundary;
2628 fl->lowat = roundup2(sc->sge.fl_starve_threshold, 8);
2629 fl->buf_boundary = 16;
2631 if (fl_pad && fl->buf_boundary < sc->sge.pad_boundary)
2632 fl->buf_boundary = sc->sge.pad_boundary;
2634 c.iqns_to_fl0congen |=
2635 htobe32(V_FW_IQ_CMD_FL0HOSTFCMODE(X_HOSTFCMODE_NONE) |
2636 F_FW_IQ_CMD_FL0FETCHRO | F_FW_IQ_CMD_FL0DATARO |
2637 (fl_pad ? F_FW_IQ_CMD_FL0PADEN : 0) |
2638 (fl->flags & FL_BUF_PACKING ? F_FW_IQ_CMD_FL0PACKEN :
2641 c.iqns_to_fl0congen |=
2642 htobe32(V_FW_IQ_CMD_FL0CNGCHMAP(cong) |
2643 F_FW_IQ_CMD_FL0CONGCIF |
2644 F_FW_IQ_CMD_FL0CONGEN);
2646 c.fl0dcaen_to_fl0cidxfthresh =
2647 htobe16(V_FW_IQ_CMD_FL0FBMIN(X_FETCHBURSTMIN_128B) |
2648 V_FW_IQ_CMD_FL0FBMAX(X_FETCHBURSTMAX_512B));
2649 c.fl0size = htobe16(fl->qsize);
2650 c.fl0addr = htobe64(fl->ba);
2653 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
2655 device_printf(sc->dev,
2656 "failed to create ingress queue: %d\n", rc);
2661 iq->gen = F_RSPD_GEN;
2662 iq->intr_next = iq->intr_params;
2663 iq->cntxt_id = be16toh(c.iqid);
2664 iq->abs_id = be16toh(c.physiqid);
2665 iq->flags |= IQ_ALLOCATED;
2667 cntxt_id = iq->cntxt_id - sc->sge.iq_start;
2668 if (cntxt_id >= sc->sge.niq) {
2669 panic ("%s: iq->cntxt_id (%d) more than the max (%d)", __func__,
2670 cntxt_id, sc->sge.niq - 1);
2672 sc->sge.iqmap[cntxt_id] = iq;
2677 iq->flags |= IQ_HAS_FL;
2678 fl->cntxt_id = be16toh(c.fl0id);
2679 fl->pidx = fl->cidx = 0;
2681 cntxt_id = fl->cntxt_id - sc->sge.eq_start;
2682 if (cntxt_id >= sc->sge.neq) {
2683 panic("%s: fl->cntxt_id (%d) more than the max (%d)",
2684 __func__, cntxt_id, sc->sge.neq - 1);
2686 sc->sge.eqmap[cntxt_id] = (void *)fl;
2689 if (isset(&sc->doorbells, DOORBELL_UDB)) {
2690 uint32_t s_qpp = sc->sge.eq_s_qpp;
2691 uint32_t mask = (1 << s_qpp) - 1;
2692 volatile uint8_t *udb;
2694 udb = sc->udbs_base + UDBS_DB_OFFSET;
2695 udb += (qid >> s_qpp) << PAGE_SHIFT;
2697 if (qid < PAGE_SIZE / UDBS_SEG_SIZE) {
2698 udb += qid << UDBS_SEG_SHIFT;
2701 fl->udb = (volatile void *)udb;
2703 fl->dbval = F_DBPRIO | V_QID(qid);
2705 fl->dbval |= F_DBTYPE;
2708 /* Enough to make sure the SGE doesn't think it's starved */
2709 refill_fl(sc, fl, fl->lowat);
2713 if (is_t5(sc) && cong >= 0) {
2714 uint32_t param, val;
2716 param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
2717 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
2718 V_FW_PARAMS_PARAM_YZ(iq->cntxt_id);
2723 for (i = 0; i < 4; i++) {
2724 if (cong & (1 << i))
2725 val |= 1 << (i << 2);
2729 rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
2731 /* report error but carry on */
2732 device_printf(sc->dev,
2733 "failed to set congestion manager context for "
2734 "ingress queue %d: %d\n", iq->cntxt_id, rc);
2738 /* Enable IQ interrupts */
2739 atomic_store_rel_int(&iq->state, IQS_IDLE);
2740 t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_SEINTARM(iq->intr_params) |
2741 V_INGRESSQID(iq->cntxt_id));
2747 free_iq_fl(struct port_info *pi, struct sge_iq *iq, struct sge_fl *fl)
2750 struct adapter *sc = iq->adapter;
2754 return (0); /* nothing to do */
2756 dev = pi ? pi->dev : sc->dev;
2758 if (iq->flags & IQ_ALLOCATED) {
2759 rc = -t4_iq_free(sc, sc->mbox, sc->pf, 0,
2760 FW_IQ_TYPE_FL_INT_CAP, iq->cntxt_id,
2761 fl ? fl->cntxt_id : 0xffff, 0xffff);
2764 "failed to free queue %p: %d\n", iq, rc);
2767 iq->flags &= ~IQ_ALLOCATED;
2770 free_ring(sc, iq->desc_tag, iq->desc_map, iq->ba, iq->desc);
2772 bzero(iq, sizeof(*iq));
2775 free_ring(sc, fl->desc_tag, fl->desc_map, fl->ba,
2779 free_fl_sdesc(sc, fl);
2781 if (mtx_initialized(&fl->fl_lock))
2782 mtx_destroy(&fl->fl_lock);
2784 bzero(fl, sizeof(*fl));
2791 add_fl_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid,
2794 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
2796 oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "fl", CTLFLAG_RD, NULL,
2798 children = SYSCTL_CHILDREN(oid);
2800 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cntxt_id",
2801 CTLTYPE_INT | CTLFLAG_RD, &fl->cntxt_id, 0, sysctl_uint16, "I",
2802 "SGE context id of the freelist");
2803 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "padding", CTLFLAG_RD, NULL,
2804 fl_pad ? 1 : 0, "padding enabled");
2805 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "packing", CTLFLAG_RD, NULL,
2806 fl->flags & FL_BUF_PACKING ? 1 : 0, "packing enabled");
2807 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cidx", CTLFLAG_RD, &fl->cidx,
2808 0, "consumer index");
2809 if (fl->flags & FL_BUF_PACKING) {
2810 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_offset",
2811 CTLFLAG_RD, &fl->rx_offset, 0, "packing rx offset");
2813 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "pidx", CTLFLAG_RD, &fl->pidx,
2814 0, "producer index");
2815 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "mbuf_allocated",
2816 CTLFLAG_RD, &fl->mbuf_allocated, "# of mbuf allocated");
2817 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "mbuf_inlined",
2818 CTLFLAG_RD, &fl->mbuf_inlined, "# of mbuf inlined in clusters");
2819 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_allocated",
2820 CTLFLAG_RD, &fl->cl_allocated, "# of clusters allocated");
2821 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_recycled",
2822 CTLFLAG_RD, &fl->cl_recycled, "# of clusters recycled");
2823 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_fast_recycled",
2824 CTLFLAG_RD, &fl->cl_fast_recycled, "# of clusters recycled (fast)");
2828 alloc_fwq(struct adapter *sc)
2831 struct sge_iq *fwq = &sc->sge.fwq;
2832 struct sysctl_oid *oid = device_get_sysctl_tree(sc->dev);
2833 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
2835 init_iq(fwq, sc, 0, 0, FW_IQ_QSIZE);
2836 fwq->flags |= IQ_INTR; /* always */
2837 intr_idx = sc->intr_count > 1 ? 1 : 0;
2838 rc = alloc_iq_fl(sc->port[0], fwq, NULL, intr_idx, -1);
2840 device_printf(sc->dev,
2841 "failed to create firmware event queue: %d\n", rc);
2845 oid = SYSCTL_ADD_NODE(&sc->ctx, children, OID_AUTO, "fwq", CTLFLAG_RD,
2846 NULL, "firmware event queue");
2847 children = SYSCTL_CHILDREN(oid);
2849 SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "abs_id",
2850 CTLTYPE_INT | CTLFLAG_RD, &fwq->abs_id, 0, sysctl_uint16, "I",
2851 "absolute id of the queue");
2852 SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "cntxt_id",
2853 CTLTYPE_INT | CTLFLAG_RD, &fwq->cntxt_id, 0, sysctl_uint16, "I",
2854 "SGE context id of the queue");
2855 SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "cidx",
2856 CTLTYPE_INT | CTLFLAG_RD, &fwq->cidx, 0, sysctl_uint16, "I",
2863 free_fwq(struct adapter *sc)
2865 return free_iq_fl(NULL, &sc->sge.fwq, NULL);
2869 alloc_mgmtq(struct adapter *sc)
2872 struct sge_wrq *mgmtq = &sc->sge.mgmtq;
2874 struct sysctl_oid *oid = device_get_sysctl_tree(sc->dev);
2875 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
2877 oid = SYSCTL_ADD_NODE(&sc->ctx, children, OID_AUTO, "mgmtq", CTLFLAG_RD,
2878 NULL, "management queue");
2880 snprintf(name, sizeof(name), "%s mgmtq", device_get_nameunit(sc->dev));
2881 init_eq(&mgmtq->eq, EQ_CTRL, CTRL_EQ_QSIZE, sc->port[0]->tx_chan,
2882 sc->sge.fwq.cntxt_id, name);
2883 rc = alloc_wrq(sc, NULL, mgmtq, oid);
2885 device_printf(sc->dev,
2886 "failed to create management queue: %d\n", rc);
2894 free_mgmtq(struct adapter *sc)
2897 return free_wrq(sc, &sc->sge.mgmtq);
2901 tnl_cong(struct port_info *pi, int drop)
2909 return (pi->rx_chan_map);
2913 alloc_rxq(struct port_info *pi, struct sge_rxq *rxq, int intr_idx, int idx,
2914 struct sysctl_oid *oid)
2917 struct sysctl_oid_list *children;
2920 rc = alloc_iq_fl(pi, &rxq->iq, &rxq->fl, intr_idx,
2921 tnl_cong(pi, cong_drop));
2926 * The freelist is just barely above the starvation threshold right now,
2927 * fill it up a bit more.
2930 refill_fl(pi->adapter, &rxq->fl, 128);
2931 FL_UNLOCK(&rxq->fl);
2933 #if defined(INET) || defined(INET6)
2934 rc = tcp_lro_init(&rxq->lro);
2937 rxq->lro.ifp = pi->ifp; /* also indicates LRO init'ed */
2939 if (pi->ifp->if_capenable & IFCAP_LRO)
2940 rxq->iq.flags |= IQ_LRO_ENABLED;
2944 children = SYSCTL_CHILDREN(oid);
2946 snprintf(name, sizeof(name), "%d", idx);
2947 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
2949 children = SYSCTL_CHILDREN(oid);
2951 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "abs_id",
2952 CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.abs_id, 0, sysctl_uint16, "I",
2953 "absolute id of the queue");
2954 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cntxt_id",
2955 CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.cntxt_id, 0, sysctl_uint16, "I",
2956 "SGE context id of the queue");
2957 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
2958 CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.cidx, 0, sysctl_uint16, "I",
2960 #if defined(INET) || defined(INET6)
2961 SYSCTL_ADD_INT(&pi->ctx, children, OID_AUTO, "lro_queued", CTLFLAG_RD,
2962 &rxq->lro.lro_queued, 0, NULL);
2963 SYSCTL_ADD_INT(&pi->ctx, children, OID_AUTO, "lro_flushed", CTLFLAG_RD,
2964 &rxq->lro.lro_flushed, 0, NULL);
2966 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "rxcsum", CTLFLAG_RD,
2967 &rxq->rxcsum, "# of times hardware assisted with checksum");
2968 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "vlan_extraction",
2969 CTLFLAG_RD, &rxq->vlan_extraction,
2970 "# of times hardware extracted 802.1Q tag");
2972 add_fl_sysctls(&pi->ctx, oid, &rxq->fl);
2978 free_rxq(struct port_info *pi, struct sge_rxq *rxq)
2982 #if defined(INET) || defined(INET6)
2984 tcp_lro_free(&rxq->lro);
2985 rxq->lro.ifp = NULL;
2989 rc = free_iq_fl(pi, &rxq->iq, &rxq->fl);
2991 bzero(rxq, sizeof(*rxq));
2998 alloc_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq,
2999 int intr_idx, int idx, struct sysctl_oid *oid)
3002 struct sysctl_oid_list *children;
3005 rc = alloc_iq_fl(pi, &ofld_rxq->iq, &ofld_rxq->fl, intr_idx,
3010 children = SYSCTL_CHILDREN(oid);
3012 snprintf(name, sizeof(name), "%d", idx);
3013 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
3015 children = SYSCTL_CHILDREN(oid);
3017 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "abs_id",
3018 CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.abs_id, 0, sysctl_uint16,
3019 "I", "absolute id of the queue");
3020 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cntxt_id",
3021 CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.cntxt_id, 0, sysctl_uint16,
3022 "I", "SGE context id of the queue");
3023 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
3024 CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.cidx, 0, sysctl_uint16, "I",
3027 add_fl_sysctls(&pi->ctx, oid, &ofld_rxq->fl);
3033 free_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq)
3037 rc = free_iq_fl(pi, &ofld_rxq->iq, &ofld_rxq->fl);
3039 bzero(ofld_rxq, sizeof(*ofld_rxq));
3047 alloc_nm_rxq(struct port_info *pi, struct sge_nm_rxq *nm_rxq, int intr_idx,
3048 int idx, struct sysctl_oid *oid)
3051 struct sysctl_oid_list *children;
3052 struct sysctl_ctx_list *ctx;
3055 struct adapter *sc = pi->adapter;
3056 struct netmap_adapter *na = NA(pi->nm_ifp);
3060 len = pi->qsize_rxq * IQ_ESIZE;
3061 rc = alloc_ring(sc, len, &nm_rxq->iq_desc_tag, &nm_rxq->iq_desc_map,
3062 &nm_rxq->iq_ba, (void **)&nm_rxq->iq_desc);
3066 len = na->num_rx_desc * EQ_ESIZE + spg_len;
3067 rc = alloc_ring(sc, len, &nm_rxq->fl_desc_tag, &nm_rxq->fl_desc_map,
3068 &nm_rxq->fl_ba, (void **)&nm_rxq->fl_desc);
3074 nm_rxq->iq_cidx = 0;
3075 nm_rxq->iq_sidx = pi->qsize_rxq - spg_len / IQ_ESIZE;
3076 nm_rxq->iq_gen = F_RSPD_GEN;
3077 nm_rxq->fl_pidx = nm_rxq->fl_cidx = 0;
3078 nm_rxq->fl_sidx = na->num_rx_desc;
3079 nm_rxq->intr_idx = intr_idx;
3082 children = SYSCTL_CHILDREN(oid);
3084 snprintf(name, sizeof(name), "%d", idx);
3085 oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, name, CTLFLAG_RD, NULL,
3087 children = SYSCTL_CHILDREN(oid);
3089 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "abs_id",
3090 CTLTYPE_INT | CTLFLAG_RD, &nm_rxq->iq_abs_id, 0, sysctl_uint16,
3091 "I", "absolute id of the queue");
3092 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cntxt_id",
3093 CTLTYPE_INT | CTLFLAG_RD, &nm_rxq->iq_cntxt_id, 0, sysctl_uint16,
3094 "I", "SGE context id of the queue");
3095 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cidx",
3096 CTLTYPE_INT | CTLFLAG_RD, &nm_rxq->iq_cidx, 0, sysctl_uint16, "I",
3099 children = SYSCTL_CHILDREN(oid);
3100 oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "fl", CTLFLAG_RD, NULL,
3102 children = SYSCTL_CHILDREN(oid);
3104 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cntxt_id",
3105 CTLTYPE_INT | CTLFLAG_RD, &nm_rxq->fl_cntxt_id, 0, sysctl_uint16,
3106 "I", "SGE context id of the freelist");
3107 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cidx", CTLFLAG_RD,
3108 &nm_rxq->fl_cidx, 0, "consumer index");
3109 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "pidx", CTLFLAG_RD,
3110 &nm_rxq->fl_pidx, 0, "producer index");
3117 free_nm_rxq(struct port_info *pi, struct sge_nm_rxq *nm_rxq)
3119 struct adapter *sc = pi->adapter;
3121 free_ring(sc, nm_rxq->iq_desc_tag, nm_rxq->iq_desc_map, nm_rxq->iq_ba,
3123 free_ring(sc, nm_rxq->fl_desc_tag, nm_rxq->fl_desc_map, nm_rxq->fl_ba,
3130 alloc_nm_txq(struct port_info *pi, struct sge_nm_txq *nm_txq, int iqidx, int idx,
3131 struct sysctl_oid *oid)
3135 struct adapter *sc = pi->adapter;
3136 struct netmap_adapter *na = NA(pi->nm_ifp);
3138 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
3140 len = na->num_tx_desc * EQ_ESIZE + spg_len;
3141 rc = alloc_ring(sc, len, &nm_txq->desc_tag, &nm_txq->desc_map,
3142 &nm_txq->ba, (void **)&nm_txq->desc);
3146 nm_txq->pidx = nm_txq->cidx = 0;
3147 nm_txq->sidx = na->num_tx_desc;
3149 nm_txq->iqidx = iqidx;
3150 nm_txq->cpl_ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT) |
3151 V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf));
3153 snprintf(name, sizeof(name), "%d", idx);
3154 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
3155 NULL, "netmap tx queue");
3156 children = SYSCTL_CHILDREN(oid);
3158 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
3159 &nm_txq->cntxt_id, 0, "SGE context id of the queue");
3160 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
3161 CTLTYPE_INT | CTLFLAG_RD, &nm_txq->cidx, 0, sysctl_uint16, "I",
3163 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "pidx",
3164 CTLTYPE_INT | CTLFLAG_RD, &nm_txq->pidx, 0, sysctl_uint16, "I",
3171 free_nm_txq(struct port_info *pi, struct sge_nm_txq *nm_txq)
3173 struct adapter *sc = pi->adapter;
3175 free_ring(sc, nm_txq->desc_tag, nm_txq->desc_map, nm_txq->ba,
3183 ctrl_eq_alloc(struct adapter *sc, struct sge_eq *eq)
3186 struct fw_eq_ctrl_cmd c;
3187 int qsize = eq->sidx + spg_len / EQ_ESIZE;
3189 bzero(&c, sizeof(c));
3191 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_CTRL_CMD) | F_FW_CMD_REQUEST |
3192 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_CTRL_CMD_PFN(sc->pf) |
3193 V_FW_EQ_CTRL_CMD_VFN(0));
3194 c.alloc_to_len16 = htobe32(F_FW_EQ_CTRL_CMD_ALLOC |
3195 F_FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
3196 c.cmpliqid_eqid = htonl(V_FW_EQ_CTRL_CMD_CMPLIQID(eq->iqid));
3197 c.physeqid_pkd = htobe32(0);
3198 c.fetchszm_to_iqid =
3199 htobe32(V_FW_EQ_CTRL_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) |
3200 V_FW_EQ_CTRL_CMD_PCIECHN(eq->tx_chan) |
3201 F_FW_EQ_CTRL_CMD_FETCHRO | V_FW_EQ_CTRL_CMD_IQID(eq->iqid));
3203 htobe32(V_FW_EQ_CTRL_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
3204 V_FW_EQ_CTRL_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
3205 V_FW_EQ_CTRL_CMD_EQSIZE(qsize));
3206 c.eqaddr = htobe64(eq->ba);
3208 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
3210 device_printf(sc->dev,
3211 "failed to create control queue %d: %d\n", eq->tx_chan, rc);
3214 eq->flags |= EQ_ALLOCATED;
3216 eq->cntxt_id = G_FW_EQ_CTRL_CMD_EQID(be32toh(c.cmpliqid_eqid));
3217 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
3218 if (cntxt_id >= sc->sge.neq)
3219 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
3220 cntxt_id, sc->sge.neq - 1);
3221 sc->sge.eqmap[cntxt_id] = eq;
3227 eth_eq_alloc(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
3230 struct fw_eq_eth_cmd c;
3231 int qsize = eq->sidx + spg_len / EQ_ESIZE;
3233 bzero(&c, sizeof(c));
3235 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_ETH_CMD) | F_FW_CMD_REQUEST |
3236 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_ETH_CMD_PFN(sc->pf) |
3237 V_FW_EQ_ETH_CMD_VFN(0));
3238 c.alloc_to_len16 = htobe32(F_FW_EQ_ETH_CMD_ALLOC |
3239 F_FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
3240 c.autoequiqe_to_viid = htobe32(F_FW_EQ_ETH_CMD_AUTOEQUIQE |
3241 F_FW_EQ_ETH_CMD_AUTOEQUEQE | V_FW_EQ_ETH_CMD_VIID(pi->viid));
3242 c.fetchszm_to_iqid =
3243 htobe32(V_FW_EQ_ETH_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) |
3244 V_FW_EQ_ETH_CMD_PCIECHN(eq->tx_chan) | F_FW_EQ_ETH_CMD_FETCHRO |
3245 V_FW_EQ_ETH_CMD_IQID(eq->iqid));
3246 c.dcaen_to_eqsize = htobe32(V_FW_EQ_ETH_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
3247 V_FW_EQ_ETH_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
3248 V_FW_EQ_ETH_CMD_EQSIZE(qsize));
3249 c.eqaddr = htobe64(eq->ba);
3251 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
3253 device_printf(pi->dev,
3254 "failed to create Ethernet egress queue: %d\n", rc);
3257 eq->flags |= EQ_ALLOCATED;
3259 eq->cntxt_id = G_FW_EQ_ETH_CMD_EQID(be32toh(c.eqid_pkd));
3260 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
3261 if (cntxt_id >= sc->sge.neq)
3262 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
3263 cntxt_id, sc->sge.neq - 1);
3264 sc->sge.eqmap[cntxt_id] = eq;
3271 ofld_eq_alloc(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
3274 struct fw_eq_ofld_cmd c;
3275 int qsize = eq->sidx + spg_len / EQ_ESIZE;
3277 bzero(&c, sizeof(c));
3279 c.op_to_vfn = htonl(V_FW_CMD_OP(FW_EQ_OFLD_CMD) | F_FW_CMD_REQUEST |
3280 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_OFLD_CMD_PFN(sc->pf) |
3281 V_FW_EQ_OFLD_CMD_VFN(0));
3282 c.alloc_to_len16 = htonl(F_FW_EQ_OFLD_CMD_ALLOC |
3283 F_FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
3284 c.fetchszm_to_iqid =
3285 htonl(V_FW_EQ_OFLD_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) |
3286 V_FW_EQ_OFLD_CMD_PCIECHN(eq->tx_chan) |
3287 F_FW_EQ_OFLD_CMD_FETCHRO | V_FW_EQ_OFLD_CMD_IQID(eq->iqid));
3289 htobe32(V_FW_EQ_OFLD_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
3290 V_FW_EQ_OFLD_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
3291 V_FW_EQ_OFLD_CMD_EQSIZE(qsize));
3292 c.eqaddr = htobe64(eq->ba);
3294 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
3296 device_printf(pi->dev,
3297 "failed to create egress queue for TCP offload: %d\n", rc);
3300 eq->flags |= EQ_ALLOCATED;
3302 eq->cntxt_id = G_FW_EQ_OFLD_CMD_EQID(be32toh(c.eqid_pkd));
3303 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
3304 if (cntxt_id >= sc->sge.neq)
3305 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
3306 cntxt_id, sc->sge.neq - 1);
3307 sc->sge.eqmap[cntxt_id] = eq;
3314 alloc_eq(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
3319 mtx_init(&eq->eq_lock, eq->lockname, NULL, MTX_DEF);
3321 qsize = eq->sidx + spg_len / EQ_ESIZE;
3322 len = qsize * EQ_ESIZE;
3323 rc = alloc_ring(sc, len, &eq->desc_tag, &eq->desc_map,
3324 &eq->ba, (void **)&eq->desc);
3328 eq->pidx = eq->cidx = 0;
3329 eq->equeqidx = eq->dbidx = 0;
3330 eq->doorbells = sc->doorbells;
3332 switch (eq->flags & EQ_TYPEMASK) {
3334 rc = ctrl_eq_alloc(sc, eq);
3338 rc = eth_eq_alloc(sc, pi, eq);
3343 rc = ofld_eq_alloc(sc, pi, eq);
3348 panic("%s: invalid eq type %d.", __func__,
3349 eq->flags & EQ_TYPEMASK);
3352 device_printf(sc->dev,
3353 "failed to allocate egress queue(%d): %d\n",
3354 eq->flags & EQ_TYPEMASK, rc);
3357 if (isset(&eq->doorbells, DOORBELL_UDB) ||
3358 isset(&eq->doorbells, DOORBELL_UDBWC) ||
3359 isset(&eq->doorbells, DOORBELL_WCWR)) {
3360 uint32_t s_qpp = sc->sge.eq_s_qpp;
3361 uint32_t mask = (1 << s_qpp) - 1;
3362 volatile uint8_t *udb;
3364 udb = sc->udbs_base + UDBS_DB_OFFSET;
3365 udb += (eq->cntxt_id >> s_qpp) << PAGE_SHIFT; /* pg offset */
3366 eq->udb_qid = eq->cntxt_id & mask; /* id in page */
3367 if (eq->udb_qid >= PAGE_SIZE / UDBS_SEG_SIZE)
3368 clrbit(&eq->doorbells, DOORBELL_WCWR);
3370 udb += eq->udb_qid << UDBS_SEG_SHIFT; /* seg offset */
3373 eq->udb = (volatile void *)udb;
3380 free_eq(struct adapter *sc, struct sge_eq *eq)
3384 if (eq->flags & EQ_ALLOCATED) {
3385 switch (eq->flags & EQ_TYPEMASK) {
3387 rc = -t4_ctrl_eq_free(sc, sc->mbox, sc->pf, 0,
3392 rc = -t4_eth_eq_free(sc, sc->mbox, sc->pf, 0,
3398 rc = -t4_ofld_eq_free(sc, sc->mbox, sc->pf, 0,
3404 panic("%s: invalid eq type %d.", __func__,
3405 eq->flags & EQ_TYPEMASK);
3408 device_printf(sc->dev,
3409 "failed to free egress queue (%d): %d\n",
3410 eq->flags & EQ_TYPEMASK, rc);
3413 eq->flags &= ~EQ_ALLOCATED;
3416 free_ring(sc, eq->desc_tag, eq->desc_map, eq->ba, eq->desc);
3418 if (mtx_initialized(&eq->eq_lock))
3419 mtx_destroy(&eq->eq_lock);
3421 bzero(eq, sizeof(*eq));
3426 alloc_wrq(struct adapter *sc, struct port_info *pi, struct sge_wrq *wrq,
3427 struct sysctl_oid *oid)
3430 struct sysctl_ctx_list *ctx = pi ? &pi->ctx : &sc->ctx;
3431 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
3433 rc = alloc_eq(sc, pi, &wrq->eq);
3438 TASK_INIT(&wrq->wrq_tx_task, 0, wrq_tx_drain, wrq);
3439 TAILQ_INIT(&wrq->incomplete_wrs);
3440 STAILQ_INIT(&wrq->wr_list);
3441 wrq->nwr_pending = 0;
3442 wrq->ndesc_needed = 0;
3444 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
3445 &wrq->eq.cntxt_id, 0, "SGE context id of the queue");
3446 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cidx",
3447 CTLTYPE_INT | CTLFLAG_RD, &wrq->eq.cidx, 0, sysctl_uint16, "I",
3449 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pidx",
3450 CTLTYPE_INT | CTLFLAG_RD, &wrq->eq.pidx, 0, sysctl_uint16, "I",
3452 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs_direct", CTLFLAG_RD,
3453 &wrq->tx_wrs_direct, "# of work requests (direct)");
3454 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs_copied", CTLFLAG_RD,
3455 &wrq->tx_wrs_copied, "# of work requests (copied)");
3461 free_wrq(struct adapter *sc, struct sge_wrq *wrq)
3465 rc = free_eq(sc, &wrq->eq);
3469 bzero(wrq, sizeof(*wrq));
3474 alloc_txq(struct port_info *pi, struct sge_txq *txq, int idx,
3475 struct sysctl_oid *oid)
3478 struct adapter *sc = pi->adapter;
3479 struct sge_eq *eq = &txq->eq;
3481 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
3483 rc = mp_ring_alloc(&txq->r, eq->sidx, txq, eth_tx, can_resume_eth_tx,
3486 device_printf(sc->dev, "failed to allocate mp_ring: %d\n", rc);
3490 rc = alloc_eq(sc, pi, eq);
3492 mp_ring_free(txq->r);
3497 /* Can't fail after this point. */
3499 TASK_INIT(&txq->tx_reclaim_task, 0, tx_reclaim, eq);
3501 txq->gl = sglist_alloc(TX_SGL_SEGS, M_WAITOK);
3502 txq->cpl_ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT) |
3503 V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf));
3504 txq->sdesc = malloc(eq->sidx * sizeof(struct tx_sdesc), M_CXGBE,
3507 snprintf(name, sizeof(name), "%d", idx);
3508 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
3510 children = SYSCTL_CHILDREN(oid);
3512 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
3513 &eq->cntxt_id, 0, "SGE context id of the queue");
3514 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
3515 CTLTYPE_INT | CTLFLAG_RD, &eq->cidx, 0, sysctl_uint16, "I",
3517 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "pidx",
3518 CTLTYPE_INT | CTLFLAG_RD, &eq->pidx, 0, sysctl_uint16, "I",
3521 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txcsum", CTLFLAG_RD,
3522 &txq->txcsum, "# of times hardware assisted with checksum");
3523 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "vlan_insertion",
3524 CTLFLAG_RD, &txq->vlan_insertion,
3525 "# of times hardware inserted 802.1Q tag");
3526 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "tso_wrs", CTLFLAG_RD,
3527 &txq->tso_wrs, "# of TSO work requests");
3528 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "imm_wrs", CTLFLAG_RD,
3529 &txq->imm_wrs, "# of work requests with immediate data");
3530 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "sgl_wrs", CTLFLAG_RD,
3531 &txq->sgl_wrs, "# of work requests with direct SGL");
3532 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkt_wrs", CTLFLAG_RD,
3533 &txq->txpkt_wrs, "# of txpkt work requests (one pkt/WR)");
3534 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts0_wrs",
3535 CTLFLAG_RD, &txq->txpkts0_wrs,
3536 "# of txpkts (type 0) work requests");
3537 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts1_wrs",
3538 CTLFLAG_RD, &txq->txpkts1_wrs,
3539 "# of txpkts (type 1) work requests");
3540 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts0_pkts",
3541 CTLFLAG_RD, &txq->txpkts0_pkts,
3542 "# of frames tx'd using type0 txpkts work requests");
3543 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts1_pkts",
3544 CTLFLAG_RD, &txq->txpkts1_pkts,
3545 "# of frames tx'd using type1 txpkts work requests");
3547 SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_enqueues",
3548 CTLFLAG_RD, &txq->r->enqueues,
3549 "# of enqueues to the mp_ring for this queue");
3550 SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_drops",
3551 CTLFLAG_RD, &txq->r->drops,
3552 "# of drops in the mp_ring for this queue");
3553 SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_starts",
3554 CTLFLAG_RD, &txq->r->starts,
3555 "# of normal consumer starts in the mp_ring for this queue");
3556 SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_stalls",
3557 CTLFLAG_RD, &txq->r->stalls,
3558 "# of consumer stalls in the mp_ring for this queue");
3559 SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_restarts",
3560 CTLFLAG_RD, &txq->r->restarts,
3561 "# of consumer restarts in the mp_ring for this queue");
3562 SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_abdications",
3563 CTLFLAG_RD, &txq->r->abdications,
3564 "# of consumer abdications in the mp_ring for this queue");
3570 free_txq(struct port_info *pi, struct sge_txq *txq)
3573 struct adapter *sc = pi->adapter;
3574 struct sge_eq *eq = &txq->eq;
3576 rc = free_eq(sc, eq);
3580 sglist_free(txq->gl);
3581 free(txq->sdesc, M_CXGBE);
3582 mp_ring_free(txq->r);
3584 bzero(txq, sizeof(*txq));
3589 oneseg_dma_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
3591 bus_addr_t *ba = arg;
3594 ("%s meant for single segment mappings only.", __func__));
3596 *ba = error ? 0 : segs->ds_addr;
3600 ring_fl_db(struct adapter *sc, struct sge_fl *fl)
3604 n = IDXDIFF(fl->pidx / 8, fl->dbidx, fl->sidx);
3608 v = fl->dbval | V_PIDX(n);
3610 *fl->udb = htole32(v);
3612 t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL), v);
3613 IDXINCR(fl->dbidx, n, fl->sidx);
3617 * Fills up the freelist by allocating upto 'n' buffers. Buffers that are
3618 * recycled do not count towards this allocation budget.
3620 * Returns non-zero to indicate that this freelist should be added to the list
3621 * of starving freelists.
3624 refill_fl(struct adapter *sc, struct sge_fl *fl, int n)
3627 struct fl_sdesc *sd;
3630 struct cluster_layout *cll;
3631 struct sw_zone_info *swz;
3632 struct cluster_metadata *clm;
3634 uint16_t hw_cidx = fl->hw_cidx; /* stable snapshot */
3636 FL_LOCK_ASSERT_OWNED(fl);
3639 * We always stop at the begining of the hardware descriptor that's just
3640 * before the one with the hw cidx. This is to avoid hw pidx = hw cidx,
3641 * which would mean an empty freelist to the chip.
3643 max_pidx = __predict_false(hw_cidx == 0) ? fl->sidx - 1 : hw_cidx - 1;
3644 if (fl->pidx == max_pidx * 8)
3647 d = &fl->desc[fl->pidx];
3648 sd = &fl->sdesc[fl->pidx];
3649 cll = &fl->cll_def; /* default layout */
3650 swz = &sc->sge.sw_zone_info[cll->zidx];
3654 if (sd->cl != NULL) {
3656 if (sd->nmbuf == 0) {
3658 * Fast recycle without involving any atomics on
3659 * the cluster's metadata (if the cluster has
3660 * metadata). This happens when all frames
3661 * received in the cluster were small enough to
3662 * fit within a single mbuf each.
3664 fl->cl_fast_recycled++;
3666 clm = cl_metadata(sc, fl, &sd->cll, sd->cl);
3668 MPASS(clm->refcount == 1);
3674 * Cluster is guaranteed to have metadata. Clusters
3675 * without metadata always take the fast recycle path
3676 * when they're recycled.
3678 clm = cl_metadata(sc, fl, &sd->cll, sd->cl);
3681 if (atomic_fetchadd_int(&clm->refcount, -1) == 1) {
3683 counter_u64_add(extfree_rels, 1);
3686 sd->cl = NULL; /* gave up my reference */
3688 MPASS(sd->cl == NULL);
3690 cl = uma_zalloc(swz->zone, M_NOWAIT);
3691 if (__predict_false(cl == NULL)) {
3692 if (cll == &fl->cll_alt || fl->cll_alt.zidx == -1 ||
3693 fl->cll_def.zidx == fl->cll_alt.zidx)
3696 /* fall back to the safe zone */
3698 swz = &sc->sge.sw_zone_info[cll->zidx];
3704 pa = pmap_kextract((vm_offset_t)cl);
3708 *d = htobe64(pa | cll->hwidx);
3709 clm = cl_metadata(sc, fl, cll, cl);
3721 if (__predict_false(++fl->pidx % 8 == 0)) {
3722 uint16_t pidx = fl->pidx / 8;
3724 if (__predict_false(pidx == fl->sidx)) {
3730 if (pidx == max_pidx)
3733 if (IDXDIFF(pidx, fl->dbidx, fl->sidx) >= 4)
3738 if (fl->pidx / 8 != fl->dbidx)
3741 return (FL_RUNNING_LOW(fl) && !(fl->flags & FL_STARVING));
3745 * Attempt to refill all starving freelists.
3748 refill_sfl(void *arg)
3750 struct adapter *sc = arg;
3751 struct sge_fl *fl, *fl_temp;
3753 mtx_lock(&sc->sfl_lock);
3754 TAILQ_FOREACH_SAFE(fl, &sc->sfl, link, fl_temp) {
3756 refill_fl(sc, fl, 64);
3757 if (FL_NOT_RUNNING_LOW(fl) || fl->flags & FL_DOOMED) {
3758 TAILQ_REMOVE(&sc->sfl, fl, link);
3759 fl->flags &= ~FL_STARVING;
3764 if (!TAILQ_EMPTY(&sc->sfl))
3765 callout_schedule(&sc->sfl_callout, hz / 5);
3766 mtx_unlock(&sc->sfl_lock);
3770 alloc_fl_sdesc(struct sge_fl *fl)
3773 fl->sdesc = malloc(fl->sidx * 8 * sizeof(struct fl_sdesc), M_CXGBE,
3780 free_fl_sdesc(struct adapter *sc, struct sge_fl *fl)
3782 struct fl_sdesc *sd;
3783 struct cluster_metadata *clm;
3784 struct cluster_layout *cll;
3788 for (i = 0; i < fl->sidx * 8; i++, sd++) {
3793 clm = cl_metadata(sc, fl, cll, sd->cl);
3795 uma_zfree(sc->sge.sw_zone_info[cll->zidx].zone, sd->cl);
3796 else if (clm && atomic_fetchadd_int(&clm->refcount, -1) == 1) {
3797 uma_zfree(sc->sge.sw_zone_info[cll->zidx].zone, sd->cl);
3798 counter_u64_add(extfree_rels, 1);
3803 free(fl->sdesc, M_CXGBE);
3808 get_pkt_gl(struct mbuf *m, struct sglist *gl)
3815 rc = sglist_append_mbuf(gl, m);
3816 if (__predict_false(rc != 0)) {
3817 panic("%s: mbuf %p (%d segs) was vetted earlier but now fails "
3818 "with %d.", __func__, m, mbuf_nsegs(m), rc);
3821 KASSERT(gl->sg_nseg == mbuf_nsegs(m),
3822 ("%s: nsegs changed for mbuf %p from %d to %d", __func__, m,
3823 mbuf_nsegs(m), gl->sg_nseg));
3824 KASSERT(gl->sg_nseg > 0 &&
3825 gl->sg_nseg <= (needs_tso(m) ? TX_SGL_SEGS_TSO : TX_SGL_SEGS),
3826 ("%s: %d segments, should have been 1 <= nsegs <= %d", __func__,
3827 gl->sg_nseg, needs_tso(m) ? TX_SGL_SEGS_TSO : TX_SGL_SEGS));
3831 * len16 for a txpkt WR with a GL. Includes the firmware work request header.
3834 txpkt_len16(u_int nsegs, u_int tso)
3840 nsegs--; /* first segment is part of ulptx_sgl */
3841 n = sizeof(struct fw_eth_tx_pkt_wr) + sizeof(struct cpl_tx_pkt_core) +
3842 sizeof(struct ulptx_sgl) + 8 * ((3 * nsegs) / 2 + (nsegs & 1));
3844 n += sizeof(struct cpl_tx_pkt_lso_core);
3846 return (howmany(n, 16));
3850 * len16 for a txpkts type 0 WR with a GL. Does not include the firmware work
3854 txpkts0_len16(u_int nsegs)
3860 nsegs--; /* first segment is part of ulptx_sgl */
3861 n = sizeof(struct ulp_txpkt) + sizeof(struct ulptx_idata) +
3862 sizeof(struct cpl_tx_pkt_core) + sizeof(struct ulptx_sgl) +
3863 8 * ((3 * nsegs) / 2 + (nsegs & 1));
3865 return (howmany(n, 16));
3869 * len16 for a txpkts type 1 WR with a GL. Does not include the firmware work
3877 n = sizeof(struct cpl_tx_pkt_core) + sizeof(struct ulptx_sgl);
3879 return (howmany(n, 16));
3883 imm_payload(u_int ndesc)
3887 n = ndesc * EQ_ESIZE - sizeof(struct fw_eth_tx_pkt_wr) -
3888 sizeof(struct cpl_tx_pkt_core);
3894 * Write a txpkt WR for this packet to the hardware descriptors, update the
3895 * software descriptor, and advance the pidx. It is guaranteed that enough
3896 * descriptors are available.
3898 * The return value is the # of hardware descriptors used.
3901 write_txpkt_wr(struct sge_txq *txq, struct fw_eth_tx_pkt_wr *wr,
3902 struct mbuf *m0, u_int available)
3904 struct sge_eq *eq = &txq->eq;
3905 struct tx_sdesc *txsd;
3906 struct cpl_tx_pkt_core *cpl;
3907 uint32_t ctrl; /* used in many unrelated places */
3909 int len16, ndesc, pktlen, nsegs;
3912 TXQ_LOCK_ASSERT_OWNED(txq);
3914 MPASS(available > 0 && available < eq->sidx);
3916 len16 = mbuf_len16(m0);
3917 nsegs = mbuf_nsegs(m0);
3918 pktlen = m0->m_pkthdr.len;
3919 ctrl = sizeof(struct cpl_tx_pkt_core);
3921 ctrl += sizeof(struct cpl_tx_pkt_lso_core);
3922 else if (pktlen <= imm_payload(2) && available >= 2) {
3923 /* Immediate data. Recalculate len16 and set nsegs to 0. */
3925 len16 = howmany(sizeof(struct fw_eth_tx_pkt_wr) +
3926 sizeof(struct cpl_tx_pkt_core) + pktlen, 16);
3929 ndesc = howmany(len16, EQ_ESIZE / 16);
3930 MPASS(ndesc <= available);
3932 /* Firmware work request header */
3933 MPASS(wr == (void *)&eq->desc[eq->pidx]);
3934 wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_PKT_WR) |
3935 V_FW_ETH_TX_PKT_WR_IMMDLEN(ctrl));
3937 ctrl = V_FW_WR_LEN16(len16);
3938 wr->equiq_to_len16 = htobe32(ctrl);
3941 if (needs_tso(m0)) {
3942 struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
3944 KASSERT(m0->m_pkthdr.l2hlen > 0 && m0->m_pkthdr.l3hlen > 0 &&
3945 m0->m_pkthdr.l4hlen > 0,
3946 ("%s: mbuf %p needs TSO but missing header lengths",
3949 ctrl = V_LSO_OPCODE(CPL_TX_PKT_LSO) | F_LSO_FIRST_SLICE |
3950 F_LSO_LAST_SLICE | V_LSO_IPHDR_LEN(m0->m_pkthdr.l3hlen >> 2)
3951 | V_LSO_TCPHDR_LEN(m0->m_pkthdr.l4hlen >> 2);
3952 if (m0->m_pkthdr.l2hlen == sizeof(struct ether_vlan_header))
3953 ctrl |= V_LSO_ETHHDR_LEN(1);
3954 if (m0->m_pkthdr.l3hlen == sizeof(struct ip6_hdr))
3957 lso->lso_ctrl = htobe32(ctrl);
3958 lso->ipid_ofst = htobe16(0);
3959 lso->mss = htobe16(m0->m_pkthdr.tso_segsz);
3960 lso->seqno_offset = htobe32(0);
3961 lso->len = htobe32(pktlen);
3963 cpl = (void *)(lso + 1);
3967 cpl = (void *)(wr + 1);
3969 /* Checksum offload */
3971 if (needs_l3_csum(m0) == 0)
3972 ctrl1 |= F_TXPKT_IPCSUM_DIS;
3973 if (needs_l4_csum(m0) == 0)
3974 ctrl1 |= F_TXPKT_L4CSUM_DIS;
3975 if (m0->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP |
3976 CSUM_UDP_IPV6 | CSUM_TCP_IPV6 | CSUM_TSO))
3977 txq->txcsum++; /* some hardware assistance provided */
3979 /* VLAN tag insertion */
3980 if (needs_vlan_insertion(m0)) {
3981 ctrl1 |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(m0->m_pkthdr.ether_vtag);
3982 txq->vlan_insertion++;
3986 cpl->ctrl0 = txq->cpl_ctrl0;
3988 cpl->len = htobe16(pktlen);
3989 cpl->ctrl1 = htobe64(ctrl1);
3992 dst = (void *)(cpl + 1);
3995 write_gl_to_txd(txq, m0, &dst, eq->sidx - ndesc < eq->pidx);
4000 for (m = m0; m != NULL; m = m->m_next) {
4001 copy_to_txd(eq, mtod(m, caddr_t), &dst, m->m_len);
4007 KASSERT(pktlen == 0, ("%s: %d bytes left.", __func__, pktlen));
4014 txsd = &txq->sdesc[eq->pidx];
4016 txsd->desc_used = ndesc;
4022 try_txpkts(struct mbuf *m, struct mbuf *n, struct txpkts *txp, u_int available)
4024 u_int needed, nsegs1, nsegs2, l1, l2;
4026 if (cannot_use_txpkts(m) || cannot_use_txpkts(n))
4029 nsegs1 = mbuf_nsegs(m);
4030 nsegs2 = mbuf_nsegs(n);
4031 if (nsegs1 + nsegs2 == 2) {
4033 l1 = l2 = txpkts1_len16();
4036 l1 = txpkts0_len16(nsegs1);
4037 l2 = txpkts0_len16(nsegs2);
4039 txp->len16 = howmany(sizeof(struct fw_eth_tx_pkts_wr), 16) + l1 + l2;
4040 needed = howmany(txp->len16, EQ_ESIZE / 16);
4041 if (needed > SGE_MAX_WR_NDESC || needed > available)
4044 txp->plen = m->m_pkthdr.len + n->m_pkthdr.len;
4045 if (txp->plen > 65535)
4049 set_mbuf_len16(m, l1);
4050 set_mbuf_len16(n, l2);
4056 add_to_txpkts(struct mbuf *m, struct txpkts *txp, u_int available)
4058 u_int plen, len16, needed, nsegs;
4060 MPASS(txp->wr_type == 0 || txp->wr_type == 1);
4062 nsegs = mbuf_nsegs(m);
4063 if (needs_tso(m) || (txp->wr_type == 1 && nsegs != 1))
4066 plen = txp->plen + m->m_pkthdr.len;
4070 if (txp->wr_type == 0)
4071 len16 = txpkts0_len16(nsegs);
4073 len16 = txpkts1_len16();
4074 needed = howmany(txp->len16 + len16, EQ_ESIZE / 16);
4075 if (needed > SGE_MAX_WR_NDESC || needed > available)
4080 txp->len16 += len16;
4081 set_mbuf_len16(m, len16);
4087 * Write a txpkts WR for the packets in txp to the hardware descriptors, update
4088 * the software descriptor, and advance the pidx. It is guaranteed that enough
4089 * descriptors are available.
4091 * The return value is the # of hardware descriptors used.
4094 write_txpkts_wr(struct sge_txq *txq, struct fw_eth_tx_pkts_wr *wr,
4095 struct mbuf *m0, const struct txpkts *txp, u_int available)
4097 struct sge_eq *eq = &txq->eq;
4098 struct tx_sdesc *txsd;
4099 struct cpl_tx_pkt_core *cpl;
4102 int ndesc, checkwrap;
4106 TXQ_LOCK_ASSERT_OWNED(txq);
4107 MPASS(txp->npkt > 0);
4108 MPASS(txp->plen < 65536);
4110 MPASS(m0->m_nextpkt != NULL);
4111 MPASS(txp->len16 <= howmany(SGE_MAX_WR_LEN, 16));
4112 MPASS(available > 0 && available < eq->sidx);
4114 ndesc = howmany(txp->len16, EQ_ESIZE / 16);
4115 MPASS(ndesc <= available);
4117 MPASS(wr == (void *)&eq->desc[eq->pidx]);
4118 wr->op_pkd = htobe32(V_FW_WR_OP(FW_ETH_TX_PKTS_WR));
4119 ctrl = V_FW_WR_LEN16(txp->len16);
4120 wr->equiq_to_len16 = htobe32(ctrl);
4121 wr->plen = htobe16(txp->plen);
4122 wr->npkt = txp->npkt;
4124 wr->type = txp->wr_type;
4128 * At this point we are 16B into a hardware descriptor. If checkwrap is
4129 * set then we know the WR is going to wrap around somewhere. We'll
4130 * check for that at appropriate points.
4132 checkwrap = eq->sidx - ndesc < eq->pidx;
4133 for (m = m0; m != NULL; m = m->m_nextpkt) {
4134 if (txp->wr_type == 0) {
4135 struct ulp_txpkt *ulpmc;
4136 struct ulptx_idata *ulpsc;
4138 /* ULP master command */
4140 ulpmc->cmd_dest = htobe32(V_ULPTX_CMD(ULP_TX_PKT) |
4141 V_ULP_TXPKT_DEST(0) | V_ULP_TXPKT_FID(eq->iqid));
4142 ulpmc->len = htobe32(mbuf_len16(m));
4144 /* ULP subcommand */
4145 ulpsc = (void *)(ulpmc + 1);
4146 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM) |
4148 ulpsc->len = htobe32(sizeof(struct cpl_tx_pkt_core));
4150 cpl = (void *)(ulpsc + 1);
4152 (uintptr_t)cpl == (uintptr_t)&eq->desc[eq->sidx])
4153 cpl = (void *)&eq->desc[0];
4154 txq->txpkts0_pkts += txp->npkt;
4158 txq->txpkts1_pkts += txp->npkt;
4162 /* Checksum offload */
4164 if (needs_l3_csum(m) == 0)
4165 ctrl1 |= F_TXPKT_IPCSUM_DIS;
4166 if (needs_l4_csum(m) == 0)
4167 ctrl1 |= F_TXPKT_L4CSUM_DIS;
4168 if (m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP |
4169 CSUM_UDP_IPV6 | CSUM_TCP_IPV6 | CSUM_TSO))
4170 txq->txcsum++; /* some hardware assistance provided */
4172 /* VLAN tag insertion */
4173 if (needs_vlan_insertion(m)) {
4174 ctrl1 |= F_TXPKT_VLAN_VLD |
4175 V_TXPKT_VLAN(m->m_pkthdr.ether_vtag);
4176 txq->vlan_insertion++;
4180 cpl->ctrl0 = txq->cpl_ctrl0;
4182 cpl->len = htobe16(m->m_pkthdr.len);
4183 cpl->ctrl1 = htobe64(ctrl1);
4187 (uintptr_t)flitp == (uintptr_t)&eq->desc[eq->sidx])
4188 flitp = (void *)&eq->desc[0];
4190 write_gl_to_txd(txq, m, (caddr_t *)(&flitp), checkwrap);
4194 txsd = &txq->sdesc[eq->pidx];
4196 txsd->desc_used = ndesc;
4202 * If the SGL ends on an address that is not 16 byte aligned, this function will
4203 * add a 0 filled flit at the end.
4206 write_gl_to_txd(struct sge_txq *txq, struct mbuf *m, caddr_t *to, int checkwrap)
4208 struct sge_eq *eq = &txq->eq;
4209 struct sglist *gl = txq->gl;
4210 struct sglist_seg *seg;
4211 __be64 *flitp, *wrap;
4212 struct ulptx_sgl *usgl;
4213 int i, nflits, nsegs;
4215 KASSERT(((uintptr_t)(*to) & 0xf) == 0,
4216 ("%s: SGL must start at a 16 byte boundary: %p", __func__, *to));
4217 MPASS((uintptr_t)(*to) >= (uintptr_t)&eq->desc[0]);
4218 MPASS((uintptr_t)(*to) < (uintptr_t)&eq->desc[eq->sidx]);
4221 nsegs = gl->sg_nseg;
4224 nflits = (3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1) + 2;
4225 flitp = (__be64 *)(*to);
4226 wrap = (__be64 *)(&eq->desc[eq->sidx]);
4227 seg = &gl->sg_segs[0];
4228 usgl = (void *)flitp;
4231 * We start at a 16 byte boundary somewhere inside the tx descriptor
4232 * ring, so we're at least 16 bytes away from the status page. There is
4233 * no chance of a wrap around in the middle of usgl (which is 16 bytes).
4236 usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
4237 V_ULPTX_NSGE(nsegs));
4238 usgl->len0 = htobe32(seg->ss_len);
4239 usgl->addr0 = htobe64(seg->ss_paddr);
4242 if (checkwrap == 0 || (uintptr_t)(flitp + nflits) <= (uintptr_t)wrap) {
4244 /* Won't wrap around at all */
4246 for (i = 0; i < nsegs - 1; i++, seg++) {
4247 usgl->sge[i / 2].len[i & 1] = htobe32(seg->ss_len);
4248 usgl->sge[i / 2].addr[i & 1] = htobe64(seg->ss_paddr);
4251 usgl->sge[i / 2].len[1] = htobe32(0);
4255 /* Will wrap somewhere in the rest of the SGL */
4257 /* 2 flits already written, write the rest flit by flit */
4258 flitp = (void *)(usgl + 1);
4259 for (i = 0; i < nflits - 2; i++) {
4261 flitp = (void *)eq->desc;
4262 *flitp++ = get_flit(seg, nsegs - 1, i);
4267 MPASS(((uintptr_t)flitp) & 0xf);
4271 MPASS((((uintptr_t)flitp) & 0xf) == 0);
4272 if (__predict_false(flitp == wrap))
4273 *to = (void *)eq->desc;
4275 *to = (void *)flitp;
4279 copy_to_txd(struct sge_eq *eq, caddr_t from, caddr_t *to, int len)
4282 MPASS((uintptr_t)(*to) >= (uintptr_t)&eq->desc[0]);
4283 MPASS((uintptr_t)(*to) < (uintptr_t)&eq->desc[eq->sidx]);
4285 if (__predict_true((uintptr_t)(*to) + len <=
4286 (uintptr_t)&eq->desc[eq->sidx])) {
4287 bcopy(from, *to, len);
4290 int portion = (uintptr_t)&eq->desc[eq->sidx] - (uintptr_t)(*to);
4292 bcopy(from, *to, portion);
4294 portion = len - portion; /* remaining */
4295 bcopy(from, (void *)eq->desc, portion);
4296 (*to) = (caddr_t)eq->desc + portion;
4301 ring_eq_db(struct adapter *sc, struct sge_eq *eq, u_int n)
4309 clrbit(&db, DOORBELL_WCWR);
4312 switch (ffs(db) - 1) {
4314 *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(n));
4317 case DOORBELL_WCWR: {
4318 volatile uint64_t *dst, *src;
4322 * Queues whose 128B doorbell segment fits in the page do not
4323 * use relative qid (udb_qid is always 0). Only queues with
4324 * doorbell segments can do WCWR.
4326 KASSERT(eq->udb_qid == 0 && n == 1,
4327 ("%s: inappropriate doorbell (0x%x, %d, %d) for eq %p",
4328 __func__, eq->doorbells, n, eq->dbidx, eq));
4330 dst = (volatile void *)((uintptr_t)eq->udb + UDBS_WR_OFFSET -
4333 src = (void *)&eq->desc[i];
4334 while (src != (void *)&eq->desc[i + 1])
4340 case DOORBELL_UDBWC:
4341 *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(n));
4346 t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL),
4347 V_QID(eq->cntxt_id) | V_PIDX(n));
4351 IDXINCR(eq->dbidx, n, eq->sidx);
4355 reclaimable_tx_desc(struct sge_eq *eq)
4359 hw_cidx = read_hw_cidx(eq);
4360 return (IDXDIFF(hw_cidx, eq->cidx, eq->sidx));
4364 total_available_tx_desc(struct sge_eq *eq)
4366 uint16_t hw_cidx, pidx;
4368 hw_cidx = read_hw_cidx(eq);
4371 if (pidx == hw_cidx)
4372 return (eq->sidx - 1);
4374 return (IDXDIFF(hw_cidx, pidx, eq->sidx) - 1);
4377 static inline uint16_t
4378 read_hw_cidx(struct sge_eq *eq)
4380 struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
4381 uint16_t cidx = spg->cidx; /* stable snapshot */
4383 return (be16toh(cidx));
4387 * Reclaim 'n' descriptors approximately.
4390 reclaim_tx_descs(struct sge_txq *txq, u_int n)
4392 struct tx_sdesc *txsd;
4393 struct sge_eq *eq = &txq->eq;
4394 u_int can_reclaim, reclaimed;
4396 TXQ_LOCK_ASSERT_OWNED(txq);
4400 can_reclaim = reclaimable_tx_desc(eq);
4401 while (can_reclaim && reclaimed < n) {
4403 struct mbuf *m, *nextpkt;
4405 txsd = &txq->sdesc[eq->cidx];
4406 ndesc = txsd->desc_used;
4408 /* Firmware doesn't return "partial" credits. */
4409 KASSERT(can_reclaim >= ndesc,
4410 ("%s: unexpected number of credits: %d, %d",
4411 __func__, can_reclaim, ndesc));
4413 for (m = txsd->m; m != NULL; m = nextpkt) {
4414 nextpkt = m->m_nextpkt;
4415 m->m_nextpkt = NULL;
4419 can_reclaim -= ndesc;
4420 IDXINCR(eq->cidx, ndesc, eq->sidx);
4427 tx_reclaim(void *arg, int n)
4429 struct sge_txq *txq = arg;
4430 struct sge_eq *eq = &txq->eq;
4433 if (TXQ_TRYLOCK(txq) == 0)
4435 n = reclaim_tx_descs(txq, 32);
4436 if (eq->cidx == eq->pidx)
4437 eq->equeqidx = eq->pidx;
4443 get_flit(struct sglist_seg *segs, int nsegs, int idx)
4445 int i = (idx / 3) * 2;
4451 rc = htobe32(segs[i].ss_len);
4453 rc |= (uint64_t)htobe32(segs[i + 1].ss_len) << 32;
4458 return (htobe64(segs[i].ss_paddr));
4460 return (htobe64(segs[i + 1].ss_paddr));
4467 find_best_refill_source(struct adapter *sc, struct sge_fl *fl, int maxp)
4469 int8_t zidx, hwidx, idx;
4470 uint16_t region1, region3;
4471 int spare, spare_needed, n;
4472 struct sw_zone_info *swz;
4473 struct hw_buf_info *hwb, *hwb_list = &sc->sge.hw_buf_info[0];
4476 * Buffer Packing: Look for PAGE_SIZE or larger zone which has a bufsize
4477 * large enough for the max payload and cluster metadata. Otherwise
4478 * settle for the largest bufsize that leaves enough room in the cluster
4481 * Without buffer packing: Look for the smallest zone which has a
4482 * bufsize large enough for the max payload. Settle for the largest
4483 * bufsize available if there's nothing big enough for max payload.
4485 spare_needed = fl->flags & FL_BUF_PACKING ? CL_METADATA_SIZE : 0;
4486 swz = &sc->sge.sw_zone_info[0];
4488 for (zidx = 0; zidx < SW_ZONE_SIZES; zidx++, swz++) {
4489 if (swz->size > largest_rx_cluster) {
4490 if (__predict_true(hwidx != -1))
4494 * This is a misconfiguration. largest_rx_cluster is
4495 * preventing us from finding a refill source. See
4496 * dev.t5nex.<n>.buffer_sizes to figure out why.
4498 device_printf(sc->dev, "largest_rx_cluster=%u leaves no"
4499 " refill source for fl %p (dma %u). Ignored.\n",
4500 largest_rx_cluster, fl, maxp);
4502 for (idx = swz->head_hwidx; idx != -1; idx = hwb->next) {
4503 hwb = &hwb_list[idx];
4504 spare = swz->size - hwb->size;
4505 if (spare < spare_needed)
4508 hwidx = idx; /* best option so far */
4509 if (hwb->size >= maxp) {
4511 if ((fl->flags & FL_BUF_PACKING) == 0)
4512 goto done; /* stop looking (not packing) */
4514 if (swz->size >= safest_rx_cluster)
4515 goto done; /* stop looking (packing) */
4517 break; /* keep looking, next zone */
4521 /* A usable hwidx has been located. */
4523 hwb = &hwb_list[hwidx];
4525 swz = &sc->sge.sw_zone_info[zidx];
4527 region3 = swz->size - hwb->size;
4530 * Stay within this zone and see if there is a better match when mbuf
4531 * inlining is allowed. Remember that the hwidx's are sorted in
4532 * decreasing order of size (so in increasing order of spare area).
4534 for (idx = hwidx; idx != -1; idx = hwb->next) {
4535 hwb = &hwb_list[idx];
4536 spare = swz->size - hwb->size;
4538 if (allow_mbufs_in_cluster == 0 || hwb->size < maxp)
4542 * Do not inline mbufs if doing so would violate the pad/pack
4543 * boundary alignment requirement.
4545 if (fl_pad && (MSIZE % sc->sge.pad_boundary) != 0)
4547 if (fl->flags & FL_BUF_PACKING &&
4548 (MSIZE % sc->sge.pack_boundary) != 0)
4551 if (spare < CL_METADATA_SIZE + MSIZE)
4553 n = (spare - CL_METADATA_SIZE) / MSIZE;
4554 if (n > howmany(hwb->size, maxp))
4558 if (fl->flags & FL_BUF_PACKING) {
4559 region1 = n * MSIZE;
4560 region3 = spare - region1;
4563 region3 = spare - region1;
4568 KASSERT(zidx >= 0 && zidx < SW_ZONE_SIZES,
4569 ("%s: bad zone %d for fl %p, maxp %d", __func__, zidx, fl, maxp));
4570 KASSERT(hwidx >= 0 && hwidx <= SGE_FLBUF_SIZES,
4571 ("%s: bad hwidx %d for fl %p, maxp %d", __func__, hwidx, fl, maxp));
4572 KASSERT(region1 + sc->sge.hw_buf_info[hwidx].size + region3 ==
4573 sc->sge.sw_zone_info[zidx].size,
4574 ("%s: bad buffer layout for fl %p, maxp %d. "
4575 "cl %d; r1 %d, payload %d, r3 %d", __func__, fl, maxp,
4576 sc->sge.sw_zone_info[zidx].size, region1,
4577 sc->sge.hw_buf_info[hwidx].size, region3));
4578 if (fl->flags & FL_BUF_PACKING || region1 > 0) {
4579 KASSERT(region3 >= CL_METADATA_SIZE,
4580 ("%s: no room for metadata. fl %p, maxp %d; "
4581 "cl %d; r1 %d, payload %d, r3 %d", __func__, fl, maxp,
4582 sc->sge.sw_zone_info[zidx].size, region1,
4583 sc->sge.hw_buf_info[hwidx].size, region3));
4584 KASSERT(region1 % MSIZE == 0,
4585 ("%s: bad mbuf region for fl %p, maxp %d. "
4586 "cl %d; r1 %d, payload %d, r3 %d", __func__, fl, maxp,
4587 sc->sge.sw_zone_info[zidx].size, region1,
4588 sc->sge.hw_buf_info[hwidx].size, region3));
4591 fl->cll_def.zidx = zidx;
4592 fl->cll_def.hwidx = hwidx;
4593 fl->cll_def.region1 = region1;
4594 fl->cll_def.region3 = region3;
4598 find_safe_refill_source(struct adapter *sc, struct sge_fl *fl)
4600 struct sge *s = &sc->sge;
4601 struct hw_buf_info *hwb;
4602 struct sw_zone_info *swz;
4606 if (fl->flags & FL_BUF_PACKING)
4607 hwidx = s->safe_hwidx2; /* with room for metadata */
4608 else if (allow_mbufs_in_cluster && s->safe_hwidx2 != -1) {
4609 hwidx = s->safe_hwidx2;
4610 hwb = &s->hw_buf_info[hwidx];
4611 swz = &s->sw_zone_info[hwb->zidx];
4612 spare = swz->size - hwb->size;
4614 /* no good if there isn't room for an mbuf as well */
4615 if (spare < CL_METADATA_SIZE + MSIZE)
4616 hwidx = s->safe_hwidx1;
4618 hwidx = s->safe_hwidx1;
4621 /* No fallback source */
4622 fl->cll_alt.hwidx = -1;
4623 fl->cll_alt.zidx = -1;
4628 hwb = &s->hw_buf_info[hwidx];
4629 swz = &s->sw_zone_info[hwb->zidx];
4630 spare = swz->size - hwb->size;
4631 fl->cll_alt.hwidx = hwidx;
4632 fl->cll_alt.zidx = hwb->zidx;
4633 if (allow_mbufs_in_cluster &&
4634 (fl_pad == 0 || (MSIZE % sc->sge.pad_boundary) == 0))
4635 fl->cll_alt.region1 = ((spare - CL_METADATA_SIZE) / MSIZE) * MSIZE;
4637 fl->cll_alt.region1 = 0;
4638 fl->cll_alt.region3 = spare - fl->cll_alt.region1;
4642 add_fl_to_sfl(struct adapter *sc, struct sge_fl *fl)
4644 mtx_lock(&sc->sfl_lock);
4646 if ((fl->flags & FL_DOOMED) == 0) {
4647 fl->flags |= FL_STARVING;
4648 TAILQ_INSERT_TAIL(&sc->sfl, fl, link);
4649 callout_reset(&sc->sfl_callout, hz / 5, refill_sfl, sc);
4652 mtx_unlock(&sc->sfl_lock);
4656 handle_wrq_egr_update(struct adapter *sc, struct sge_eq *eq)
4658 struct sge_wrq *wrq = (void *)eq;
4660 atomic_readandclear_int(&eq->equiq);
4661 taskqueue_enqueue(sc->tq[eq->tx_chan], &wrq->wrq_tx_task);
4665 handle_eth_egr_update(struct adapter *sc, struct sge_eq *eq)
4667 struct sge_txq *txq = (void *)eq;
4669 MPASS((eq->flags & EQ_TYPEMASK) == EQ_ETH);
4671 atomic_readandclear_int(&eq->equiq);
4672 mp_ring_check_drainage(txq->r, 0);
4673 taskqueue_enqueue(sc->tq[eq->tx_chan], &txq->tx_reclaim_task);
4677 handle_sge_egr_update(struct sge_iq *iq, const struct rss_header *rss,
4680 const struct cpl_sge_egr_update *cpl = (const void *)(rss + 1);
4681 unsigned int qid = G_EGR_QID(ntohl(cpl->opcode_qid));
4682 struct adapter *sc = iq->adapter;
4683 struct sge *s = &sc->sge;
4685 static void (*h[])(struct adapter *, struct sge_eq *) = {NULL,
4686 &handle_wrq_egr_update, &handle_eth_egr_update,
4687 &handle_wrq_egr_update};
4689 KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__,
4692 eq = s->eqmap[qid - s->eq_start];
4693 (*h[eq->flags & EQ_TYPEMASK])(sc, eq);
4698 /* handle_fw_msg works for both fw4_msg and fw6_msg because this is valid */
4699 CTASSERT(offsetof(struct cpl_fw4_msg, data) == \
4700 offsetof(struct cpl_fw6_msg, data));
4703 handle_fw_msg(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
4705 struct adapter *sc = iq->adapter;
4706 const struct cpl_fw6_msg *cpl = (const void *)(rss + 1);
4708 KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__,
4711 if (cpl->type == FW_TYPE_RSSCPL || cpl->type == FW6_TYPE_RSSCPL) {
4712 const struct rss_header *rss2;
4714 rss2 = (const struct rss_header *)&cpl->data[0];
4715 return (sc->cpl_handler[rss2->opcode](iq, rss2, m));
4718 return (sc->fw_msg_handler[cpl->type](sc, &cpl->data[0]));
4722 sysctl_uint16(SYSCTL_HANDLER_ARGS)
4724 uint16_t *id = arg1;
4727 return sysctl_handle_int(oidp, &i, 0, req);
4731 sysctl_bufsizes(SYSCTL_HANDLER_ARGS)
4733 struct sge *s = arg1;
4734 struct hw_buf_info *hwb = &s->hw_buf_info[0];
4735 struct sw_zone_info *swz = &s->sw_zone_info[0];
4740 sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
4741 for (i = 0; i < SGE_FLBUF_SIZES; i++, hwb++) {
4742 if (hwb->zidx >= 0 && swz[hwb->zidx].size <= largest_rx_cluster)
4747 sbuf_printf(&sb, "%u%c ", hwb->size, c);
4751 rc = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);