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>
35 #include <sys/eventhandler.h>
37 #include <sys/socket.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/queue.h>
42 #include <sys/taskqueue.h>
44 #include <sys/sglist.h>
45 #include <sys/sysctl.h>
47 #include <sys/counter.h>
49 #include <net/ethernet.h>
51 #include <net/if_vlan_var.h>
52 #include <netinet/in.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip6.h>
55 #include <netinet/tcp.h>
56 #include <machine/md_var.h>
60 #include <machine/bus.h>
61 #include <sys/selinfo.h>
62 #include <net/if_var.h>
63 #include <net/netmap.h>
64 #include <dev/netmap/netmap_kern.h>
67 #include "common/common.h"
68 #include "common/t4_regs.h"
69 #include "common/t4_regs_values.h"
70 #include "common/t4_msg.h"
71 #include "t4_mp_ring.h"
73 #ifdef T4_PKT_TIMESTAMP
74 #define RX_COPY_THRESHOLD (MINCLSIZE - 8)
76 #define RX_COPY_THRESHOLD MINCLSIZE
80 * Ethernet frames are DMA'd at this byte offset into the freelist buffer.
81 * 0-7 are valid values.
84 TUNABLE_INT("hw.cxgbe.fl_pktshift", &fl_pktshift);
87 * Pad ethernet payload up to this boundary.
88 * -1: driver should figure out a good value.
90 * Any power of 2 from 32 to 4096 (both inclusive) is also a valid value.
93 TUNABLE_INT("hw.cxgbe.fl_pad", &fl_pad);
97 * -1: driver should figure out a good value.
98 * 64 or 128 are the only other valid values.
101 TUNABLE_INT("hw.cxgbe.spg_len", &spg_len);
105 * -1: no congestion feedback (not recommended).
106 * 0: backpressure the channel instead of dropping packets right away.
107 * 1: no backpressure, drop packets for the congested queue immediately.
109 static int cong_drop = 0;
110 TUNABLE_INT("hw.cxgbe.cong_drop", &cong_drop);
113 * Deliver multiple frames in the same free list buffer if they fit.
114 * -1: let the driver decide whether to enable buffer packing or not.
115 * 0: disable buffer packing.
116 * 1: enable buffer packing.
118 static int buffer_packing = -1;
119 TUNABLE_INT("hw.cxgbe.buffer_packing", &buffer_packing);
122 * Start next frame in a packed buffer at this boundary.
123 * -1: driver should figure out a good value.
124 * T4: driver will ignore this and use the same value as fl_pad above.
125 * T5: 16, or a power of 2 from 64 to 4096 (both inclusive) is a valid value.
127 static int fl_pack = -1;
128 TUNABLE_INT("hw.cxgbe.fl_pack", &fl_pack);
131 * Allow the driver to create mbuf(s) in a cluster allocated for rx.
132 * 0: never; always allocate mbufs from the zone_mbuf UMA zone.
133 * 1: ok to create mbuf(s) within a cluster if there is room.
135 static int allow_mbufs_in_cluster = 1;
136 TUNABLE_INT("hw.cxgbe.allow_mbufs_in_cluster", &allow_mbufs_in_cluster);
139 * Largest rx cluster size that the driver is allowed to allocate.
141 static int largest_rx_cluster = MJUM16BYTES;
142 TUNABLE_INT("hw.cxgbe.largest_rx_cluster", &largest_rx_cluster);
145 * Size of cluster allocation that's most likely to succeed. The driver will
146 * fall back to this size if it fails to allocate clusters larger than this.
148 static int safest_rx_cluster = PAGE_SIZE;
149 TUNABLE_INT("hw.cxgbe.safest_rx_cluster", &safest_rx_cluster);
152 u_int wr_type; /* type 0 or type 1 */
153 u_int npkt; /* # of packets in this work request */
154 u_int plen; /* total payload (sum of all packets) */
155 u_int len16; /* # of 16B pieces used by this work request */
158 /* A packet's SGL. This + m_pkthdr has all info needed for tx */
161 struct sglist_seg seg[TX_SGL_SEGS];
164 static int service_iq(struct sge_iq *, int);
165 static struct mbuf *get_fl_payload(struct adapter *, struct sge_fl *, uint32_t);
166 static int t4_eth_rx(struct sge_iq *, const struct rss_header *, struct mbuf *);
167 static inline void init_iq(struct sge_iq *, struct adapter *, int, int, int);
168 static inline void init_fl(struct adapter *, struct sge_fl *, int, int, char *);
169 static inline void init_eq(struct sge_eq *, int, int, uint8_t, uint16_t,
171 static int alloc_ring(struct adapter *, size_t, bus_dma_tag_t *, bus_dmamap_t *,
172 bus_addr_t *, void **);
173 static int free_ring(struct adapter *, bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
175 static int alloc_iq_fl(struct port_info *, struct sge_iq *, struct sge_fl *,
177 static int free_iq_fl(struct port_info *, struct sge_iq *, struct sge_fl *);
178 static void add_fl_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *,
180 static int alloc_fwq(struct adapter *);
181 static int free_fwq(struct adapter *);
182 static int alloc_mgmtq(struct adapter *);
183 static int free_mgmtq(struct adapter *);
184 static int alloc_rxq(struct port_info *, struct sge_rxq *, int, int,
185 struct sysctl_oid *);
186 static int free_rxq(struct port_info *, struct sge_rxq *);
188 static int alloc_ofld_rxq(struct port_info *, struct sge_ofld_rxq *, int, int,
189 struct sysctl_oid *);
190 static int free_ofld_rxq(struct port_info *, struct sge_ofld_rxq *);
193 static int alloc_nm_rxq(struct port_info *, struct sge_nm_rxq *, int, int,
194 struct sysctl_oid *);
195 static int free_nm_rxq(struct port_info *, struct sge_nm_rxq *);
196 static int alloc_nm_txq(struct port_info *, struct sge_nm_txq *, int, int,
197 struct sysctl_oid *);
198 static int free_nm_txq(struct port_info *, struct sge_nm_txq *);
200 static int ctrl_eq_alloc(struct adapter *, struct sge_eq *);
201 static int eth_eq_alloc(struct adapter *, struct port_info *, struct sge_eq *);
203 static int ofld_eq_alloc(struct adapter *, struct port_info *, struct sge_eq *);
205 static int alloc_eq(struct adapter *, struct port_info *, struct sge_eq *);
206 static int free_eq(struct adapter *, struct sge_eq *);
207 static int alloc_wrq(struct adapter *, struct port_info *, struct sge_wrq *,
208 struct sysctl_oid *);
209 static int free_wrq(struct adapter *, struct sge_wrq *);
210 static int alloc_txq(struct port_info *, struct sge_txq *, int,
211 struct sysctl_oid *);
212 static int free_txq(struct port_info *, struct sge_txq *);
213 static void oneseg_dma_callback(void *, bus_dma_segment_t *, int, int);
214 static inline void ring_fl_db(struct adapter *, struct sge_fl *);
215 static int refill_fl(struct adapter *, struct sge_fl *, int);
216 static void refill_sfl(void *);
217 static int alloc_fl_sdesc(struct sge_fl *);
218 static void free_fl_sdesc(struct adapter *, struct sge_fl *);
219 static void find_best_refill_source(struct adapter *, struct sge_fl *, int);
220 static void find_safe_refill_source(struct adapter *, struct sge_fl *);
221 static void add_fl_to_sfl(struct adapter *, struct sge_fl *);
223 static inline void get_pkt_gl(struct mbuf *, struct sglist *);
224 static inline u_int txpkt_len16(u_int, u_int);
225 static inline u_int txpkts0_len16(u_int);
226 static inline u_int txpkts1_len16(void);
227 static u_int write_txpkt_wr(struct sge_txq *, struct fw_eth_tx_pkt_wr *,
228 struct mbuf *, u_int);
229 static int try_txpkts(struct mbuf *, struct mbuf *, struct txpkts *, u_int);
230 static int add_to_txpkts(struct mbuf *, struct txpkts *, u_int);
231 static u_int write_txpkts_wr(struct sge_txq *, struct fw_eth_tx_pkts_wr *,
232 struct mbuf *, const struct txpkts *, u_int);
233 static void write_gl_to_txd(struct sge_txq *, struct mbuf *, caddr_t *, int);
234 static inline void copy_to_txd(struct sge_eq *, caddr_t, caddr_t *, int);
235 static inline void ring_eq_db(struct adapter *, struct sge_eq *, u_int);
236 static inline uint16_t read_hw_cidx(struct sge_eq *);
237 static inline u_int reclaimable_tx_desc(struct sge_eq *);
238 static inline u_int total_available_tx_desc(struct sge_eq *);
239 static u_int reclaim_tx_descs(struct sge_txq *, u_int);
240 static void tx_reclaim(void *, int);
241 static __be64 get_flit(struct sglist_seg *, int, int);
242 static int handle_sge_egr_update(struct sge_iq *, const struct rss_header *,
244 static int handle_fw_msg(struct sge_iq *, const struct rss_header *,
246 static void wrq_tx_drain(void *, int);
247 static void drain_wrq_wr_list(struct adapter *, struct sge_wrq *);
249 static int sysctl_uint16(SYSCTL_HANDLER_ARGS);
250 static int sysctl_bufsizes(SYSCTL_HANDLER_ARGS);
252 static counter_u64_t extfree_refs;
253 static counter_u64_t extfree_rels;
256 * Called on MOD_LOAD. Validates and calculates the SGE tunables.
262 if (fl_pktshift < 0 || fl_pktshift > 7) {
263 printf("Invalid hw.cxgbe.fl_pktshift value (%d),"
264 " using 2 instead.\n", fl_pktshift);
268 if (spg_len != 64 && spg_len != 128) {
271 #if defined(__i386__) || defined(__amd64__)
272 len = cpu_clflush_line_size > 64 ? 128 : 64;
277 printf("Invalid hw.cxgbe.spg_len value (%d),"
278 " using %d instead.\n", spg_len, len);
283 if (cong_drop < -1 || cong_drop > 1) {
284 printf("Invalid hw.cxgbe.cong_drop value (%d),"
285 " using 0 instead.\n", cong_drop);
289 extfree_refs = counter_u64_alloc(M_WAITOK);
290 extfree_rels = counter_u64_alloc(M_WAITOK);
291 counter_u64_zero(extfree_refs);
292 counter_u64_zero(extfree_rels);
296 t4_sge_modunload(void)
299 counter_u64_free(extfree_refs);
300 counter_u64_free(extfree_rels);
304 t4_sge_extfree_refs(void)
308 rels = counter_u64_fetch(extfree_rels);
309 refs = counter_u64_fetch(extfree_refs);
311 return (refs - rels);
315 t4_init_sge_cpl_handlers(struct adapter *sc)
318 t4_register_cpl_handler(sc, CPL_FW4_MSG, handle_fw_msg);
319 t4_register_cpl_handler(sc, CPL_FW6_MSG, handle_fw_msg);
320 t4_register_cpl_handler(sc, CPL_SGE_EGR_UPDATE, handle_sge_egr_update);
321 t4_register_cpl_handler(sc, CPL_RX_PKT, t4_eth_rx);
322 t4_register_fw_msg_handler(sc, FW6_TYPE_CMD_RPL, t4_handle_fw_rpl);
326 setup_pad_and_pack_boundaries(struct adapter *sc)
332 if (fl_pad < 32 || fl_pad > 4096 || !powerof2(fl_pad)) {
334 * If there is any chance that we might use buffer packing and
335 * the chip is a T4, then pick 64 as the pad/pack boundary. Set
336 * it to 32 in all other cases.
338 pad = is_t4(sc) && buffer_packing ? 64 : 32;
341 * For fl_pad = 0 we'll still write a reasonable value to the
342 * register but all the freelists will opt out of padding.
343 * We'll complain here only if the user tried to set it to a
344 * value greater than 0 that was invalid.
347 device_printf(sc->dev, "Invalid hw.cxgbe.fl_pad value"
348 " (%d), using %d instead.\n", fl_pad, pad);
351 m = V_INGPADBOUNDARY(M_INGPADBOUNDARY);
352 v = V_INGPADBOUNDARY(ilog2(pad) - 5);
353 t4_set_reg_field(sc, A_SGE_CONTROL, m, v);
356 if (fl_pack != -1 && fl_pack != pad) {
357 /* Complain but carry on. */
358 device_printf(sc->dev, "hw.cxgbe.fl_pack (%d) ignored,"
359 " using %d instead.\n", fl_pack, pad);
365 if (fl_pack < 16 || fl_pack == 32 || fl_pack > 4096 ||
366 !powerof2(fl_pack)) {
367 pack = max(sc->params.pci.mps, CACHE_LINE_SIZE);
368 MPASS(powerof2(pack));
376 device_printf(sc->dev, "Invalid hw.cxgbe.fl_pack value"
377 " (%d), using %d instead.\n", fl_pack, pack);
380 m = V_INGPACKBOUNDARY(M_INGPACKBOUNDARY);
382 v = V_INGPACKBOUNDARY(0);
384 v = V_INGPACKBOUNDARY(ilog2(pack) - 5);
386 MPASS(!is_t4(sc)); /* T4 doesn't have SGE_CONTROL2 */
387 t4_set_reg_field(sc, A_SGE_CONTROL2, m, v);
391 * adap->params.vpd.cclk must be set up before this is called.
394 t4_tweak_chip_settings(struct adapter *sc)
398 int intr_timer[SGE_NTIMERS] = {1, 5, 10, 50, 100, 200};
399 int timer_max = M_TIMERVALUE0 * 1000 / sc->params.vpd.cclk;
400 int intr_pktcount[SGE_NCOUNTERS] = {1, 8, 16, 32}; /* 63 max */
401 uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE);
402 static int sge_flbuf_sizes[] = {
404 #if MJUMPAGESIZE != MCLBYTES
406 MJUMPAGESIZE - CL_METADATA_SIZE,
407 MJUMPAGESIZE - 2 * MSIZE - CL_METADATA_SIZE,
411 MCLBYTES - MSIZE - CL_METADATA_SIZE,
412 MJUM9BYTES - CL_METADATA_SIZE,
413 MJUM16BYTES - CL_METADATA_SIZE,
416 KASSERT(sc->flags & MASTER_PF,
417 ("%s: trying to change chip settings when not master.", __func__));
419 m = V_PKTSHIFT(M_PKTSHIFT) | F_RXPKTCPLMODE | F_EGRSTATUSPAGESIZE;
420 v = V_PKTSHIFT(fl_pktshift) | F_RXPKTCPLMODE |
421 V_EGRSTATUSPAGESIZE(spg_len == 128);
422 t4_set_reg_field(sc, A_SGE_CONTROL, m, v);
424 setup_pad_and_pack_boundaries(sc);
426 v = V_HOSTPAGESIZEPF0(PAGE_SHIFT - 10) |
427 V_HOSTPAGESIZEPF1(PAGE_SHIFT - 10) |
428 V_HOSTPAGESIZEPF2(PAGE_SHIFT - 10) |
429 V_HOSTPAGESIZEPF3(PAGE_SHIFT - 10) |
430 V_HOSTPAGESIZEPF4(PAGE_SHIFT - 10) |
431 V_HOSTPAGESIZEPF5(PAGE_SHIFT - 10) |
432 V_HOSTPAGESIZEPF6(PAGE_SHIFT - 10) |
433 V_HOSTPAGESIZEPF7(PAGE_SHIFT - 10);
434 t4_write_reg(sc, A_SGE_HOST_PAGE_SIZE, v);
436 KASSERT(nitems(sge_flbuf_sizes) <= SGE_FLBUF_SIZES,
437 ("%s: hw buffer size table too big", __func__));
438 for (i = 0; i < min(nitems(sge_flbuf_sizes), SGE_FLBUF_SIZES); i++) {
439 t4_write_reg(sc, A_SGE_FL_BUFFER_SIZE0 + (4 * i),
443 v = V_THRESHOLD_0(intr_pktcount[0]) | V_THRESHOLD_1(intr_pktcount[1]) |
444 V_THRESHOLD_2(intr_pktcount[2]) | V_THRESHOLD_3(intr_pktcount[3]);
445 t4_write_reg(sc, A_SGE_INGRESS_RX_THRESHOLD, v);
447 KASSERT(intr_timer[0] <= timer_max,
448 ("%s: not a single usable timer (%d, %d)", __func__, intr_timer[0],
450 for (i = 1; i < nitems(intr_timer); i++) {
451 KASSERT(intr_timer[i] >= intr_timer[i - 1],
452 ("%s: timers not listed in increasing order (%d)",
455 while (intr_timer[i] > timer_max) {
456 if (i == nitems(intr_timer) - 1) {
457 intr_timer[i] = timer_max;
460 intr_timer[i] += intr_timer[i - 1];
465 v = V_TIMERVALUE0(us_to_core_ticks(sc, intr_timer[0])) |
466 V_TIMERVALUE1(us_to_core_ticks(sc, intr_timer[1]));
467 t4_write_reg(sc, A_SGE_TIMER_VALUE_0_AND_1, v);
468 v = V_TIMERVALUE2(us_to_core_ticks(sc, intr_timer[2])) |
469 V_TIMERVALUE3(us_to_core_ticks(sc, intr_timer[3]));
470 t4_write_reg(sc, A_SGE_TIMER_VALUE_2_AND_3, v);
471 v = V_TIMERVALUE4(us_to_core_ticks(sc, intr_timer[4])) |
472 V_TIMERVALUE5(us_to_core_ticks(sc, intr_timer[5]));
473 t4_write_reg(sc, A_SGE_TIMER_VALUE_4_AND_5, v);
475 if (cong_drop == 0) {
476 m = F_TUNNELCNGDROP0 | F_TUNNELCNGDROP1 | F_TUNNELCNGDROP2 |
478 t4_set_reg_field(sc, A_TP_PARA_REG3, m, 0);
481 /* 4K, 16K, 64K, 256K DDP "page sizes" */
482 v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6);
483 t4_write_reg(sc, A_ULP_RX_TDDP_PSZ, v);
485 m = v = F_TDDPTAGTCB;
486 t4_set_reg_field(sc, A_ULP_RX_CTL, m, v);
488 m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET |
490 v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET;
491 t4_set_reg_field(sc, A_TP_PARA_REG5, m, v);
495 * SGE wants the buffer to be at least 64B and then a multiple of 16. If
496 * padding is is use the buffer's start and end need to be aligned to the pad
497 * boundary as well. We'll just make sure that the size is a multiple of the
498 * boundary here, it is up to the buffer allocation code to make sure the start
499 * of the buffer is aligned as well.
502 hwsz_ok(struct adapter *sc, int hwsz)
504 int mask = fl_pad ? sc->sge.pad_boundary - 1 : 16 - 1;
506 return (hwsz >= 64 && (hwsz & mask) == 0);
510 * XXX: driver really should be able to deal with unexpected settings.
513 t4_read_chip_settings(struct adapter *sc)
515 struct sge *s = &sc->sge;
518 uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE);
519 static int sw_buf_sizes[] = { /* Sorted by size */
521 #if MJUMPAGESIZE != MCLBYTES
527 struct sw_zone_info *swz, *safe_swz;
528 struct hw_buf_info *hwb;
530 m = V_PKTSHIFT(M_PKTSHIFT) | F_RXPKTCPLMODE | F_EGRSTATUSPAGESIZE;
531 v = V_PKTSHIFT(fl_pktshift) | F_RXPKTCPLMODE |
532 V_EGRSTATUSPAGESIZE(spg_len == 128);
533 r = t4_read_reg(sc, A_SGE_CONTROL);
535 device_printf(sc->dev, "invalid SGE_CONTROL(0x%x)\n", r);
538 s->pad_boundary = 1 << (G_INGPADBOUNDARY(r) + 5);
541 s->pack_boundary = s->pad_boundary;
543 r = t4_read_reg(sc, A_SGE_CONTROL2);
544 if (G_INGPACKBOUNDARY(r) == 0)
545 s->pack_boundary = 16;
547 s->pack_boundary = 1 << (G_INGPACKBOUNDARY(r) + 5);
550 v = V_HOSTPAGESIZEPF0(PAGE_SHIFT - 10) |
551 V_HOSTPAGESIZEPF1(PAGE_SHIFT - 10) |
552 V_HOSTPAGESIZEPF2(PAGE_SHIFT - 10) |
553 V_HOSTPAGESIZEPF3(PAGE_SHIFT - 10) |
554 V_HOSTPAGESIZEPF4(PAGE_SHIFT - 10) |
555 V_HOSTPAGESIZEPF5(PAGE_SHIFT - 10) |
556 V_HOSTPAGESIZEPF6(PAGE_SHIFT - 10) |
557 V_HOSTPAGESIZEPF7(PAGE_SHIFT - 10);
558 r = t4_read_reg(sc, A_SGE_HOST_PAGE_SIZE);
560 device_printf(sc->dev, "invalid SGE_HOST_PAGE_SIZE(0x%x)\n", r);
564 /* Filter out unusable hw buffer sizes entirely (mark with -2). */
565 hwb = &s->hw_buf_info[0];
566 for (i = 0; i < nitems(s->hw_buf_info); i++, hwb++) {
567 r = t4_read_reg(sc, A_SGE_FL_BUFFER_SIZE0 + (4 * i));
569 hwb->zidx = hwsz_ok(sc, r) ? -1 : -2;
574 * Create a sorted list in decreasing order of hw buffer sizes (and so
575 * increasing order of spare area) for each software zone.
577 * If padding is enabled then the start and end of the buffer must align
578 * to the pad boundary; if packing is enabled then they must align with
579 * the pack boundary as well. Allocations from the cluster zones are
580 * aligned to min(size, 4K), so the buffer starts at that alignment and
581 * ends at hwb->size alignment. If mbuf inlining is allowed the
582 * starting alignment will be reduced to MSIZE and the driver will
583 * exercise appropriate caution when deciding on the best buffer layout
586 n = 0; /* no usable buffer size to begin with */
587 swz = &s->sw_zone_info[0];
589 for (i = 0; i < SW_ZONE_SIZES; i++, swz++) {
590 int8_t head = -1, tail = -1;
592 swz->size = sw_buf_sizes[i];
593 swz->zone = m_getzone(swz->size);
594 swz->type = m_gettype(swz->size);
596 if (swz->size < PAGE_SIZE) {
597 MPASS(powerof2(swz->size));
598 if (fl_pad && (swz->size % sc->sge.pad_boundary != 0))
602 if (swz->size == safest_rx_cluster)
605 hwb = &s->hw_buf_info[0];
606 for (j = 0; j < SGE_FLBUF_SIZES; j++, hwb++) {
607 if (hwb->zidx != -1 || hwb->size > swz->size)
611 MPASS(hwb->size % sc->sge.pad_boundary == 0);
616 else if (hwb->size < s->hw_buf_info[tail].size) {
617 s->hw_buf_info[tail].next = j;
621 struct hw_buf_info *t;
623 for (cur = &head; *cur != -1; cur = &t->next) {
624 t = &s->hw_buf_info[*cur];
625 if (hwb->size == t->size) {
629 if (hwb->size > t->size) {
637 swz->head_hwidx = head;
638 swz->tail_hwidx = tail;
642 if (swz->size - s->hw_buf_info[tail].size >=
644 sc->flags |= BUF_PACKING_OK;
648 device_printf(sc->dev, "no usable SGE FL buffer size.\n");
654 if (safe_swz != NULL) {
655 s->safe_hwidx1 = safe_swz->head_hwidx;
656 for (i = safe_swz->head_hwidx; i != -1; i = hwb->next) {
659 hwb = &s->hw_buf_info[i];
662 MPASS(hwb->size % sc->sge.pad_boundary == 0);
664 spare = safe_swz->size - hwb->size;
665 if (spare >= CL_METADATA_SIZE) {
672 r = t4_read_reg(sc, A_SGE_INGRESS_RX_THRESHOLD);
673 s->counter_val[0] = G_THRESHOLD_0(r);
674 s->counter_val[1] = G_THRESHOLD_1(r);
675 s->counter_val[2] = G_THRESHOLD_2(r);
676 s->counter_val[3] = G_THRESHOLD_3(r);
678 r = t4_read_reg(sc, A_SGE_TIMER_VALUE_0_AND_1);
679 s->timer_val[0] = G_TIMERVALUE0(r) / core_ticks_per_usec(sc);
680 s->timer_val[1] = G_TIMERVALUE1(r) / core_ticks_per_usec(sc);
681 r = t4_read_reg(sc, A_SGE_TIMER_VALUE_2_AND_3);
682 s->timer_val[2] = G_TIMERVALUE2(r) / core_ticks_per_usec(sc);
683 s->timer_val[3] = G_TIMERVALUE3(r) / core_ticks_per_usec(sc);
684 r = t4_read_reg(sc, A_SGE_TIMER_VALUE_4_AND_5);
685 s->timer_val[4] = G_TIMERVALUE4(r) / core_ticks_per_usec(sc);
686 s->timer_val[5] = G_TIMERVALUE5(r) / core_ticks_per_usec(sc);
688 if (cong_drop == 0) {
689 m = F_TUNNELCNGDROP0 | F_TUNNELCNGDROP1 | F_TUNNELCNGDROP2 |
691 r = t4_read_reg(sc, A_TP_PARA_REG3);
693 device_printf(sc->dev,
694 "invalid TP_PARA_REG3(0x%x)\n", r);
699 v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6);
700 r = t4_read_reg(sc, A_ULP_RX_TDDP_PSZ);
702 device_printf(sc->dev, "invalid ULP_RX_TDDP_PSZ(0x%x)\n", r);
706 m = v = F_TDDPTAGTCB;
707 r = t4_read_reg(sc, A_ULP_RX_CTL);
709 device_printf(sc->dev, "invalid ULP_RX_CTL(0x%x)\n", r);
713 m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET |
715 v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET;
716 r = t4_read_reg(sc, A_TP_PARA_REG5);
718 device_printf(sc->dev, "invalid TP_PARA_REG5(0x%x)\n", r);
722 r = t4_read_reg(sc, A_SGE_CONM_CTRL);
723 s->fl_starve_threshold = G_EGRTHRESHOLD(r) * 2 + 1;
725 s->fl_starve_threshold2 = s->fl_starve_threshold;
727 s->fl_starve_threshold2 = G_EGRTHRESHOLDPACKING(r) * 2 + 1;
729 /* egress queues: log2 of # of doorbells per BAR2 page */
730 r = t4_read_reg(sc, A_SGE_EGRESS_QUEUES_PER_PAGE_PF);
731 r >>= S_QUEUESPERPAGEPF0 +
732 (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) * sc->pf;
733 s->eq_s_qpp = r & M_QUEUESPERPAGEPF0;
735 /* ingress queues: log2 of # of doorbells per BAR2 page */
736 r = t4_read_reg(sc, A_SGE_INGRESS_QUEUES_PER_PAGE_PF);
737 r >>= S_QUEUESPERPAGEPF0 +
738 (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) * sc->pf;
739 s->iq_s_qpp = r & M_QUEUESPERPAGEPF0;
741 t4_init_tp_params(sc);
743 t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
744 t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
750 t4_create_dma_tag(struct adapter *sc)
754 rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
755 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE,
756 BUS_SPACE_UNRESTRICTED, BUS_SPACE_MAXSIZE, BUS_DMA_ALLOCNOW, NULL,
759 device_printf(sc->dev,
760 "failed to create main DMA tag: %d\n", rc);
767 t4_sge_sysctls(struct adapter *sc, struct sysctl_ctx_list *ctx,
768 struct sysctl_oid_list *children)
771 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "buffer_sizes",
772 CTLTYPE_STRING | CTLFLAG_RD, &sc->sge, 0, sysctl_bufsizes, "A",
773 "freelist buffer sizes");
775 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pktshift", CTLFLAG_RD,
776 NULL, fl_pktshift, "payload DMA offset in rx buffer (bytes)");
778 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pad", CTLFLAG_RD,
779 NULL, sc->sge.pad_boundary, "payload pad boundary (bytes)");
781 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "spg_len", CTLFLAG_RD,
782 NULL, spg_len, "status page size (bytes)");
784 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_drop", CTLFLAG_RD,
785 NULL, cong_drop, "congestion drop setting");
787 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pack", CTLFLAG_RD,
788 NULL, sc->sge.pack_boundary, "payload pack boundary (bytes)");
792 t4_destroy_dma_tag(struct adapter *sc)
795 bus_dma_tag_destroy(sc->dmat);
801 * Allocate and initialize the firmware event queue and the management queue.
803 * Returns errno on failure. Resources allocated up to that point may still be
804 * allocated. Caller is responsible for cleanup in case this function fails.
807 t4_setup_adapter_queues(struct adapter *sc)
811 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
813 sysctl_ctx_init(&sc->ctx);
814 sc->flags |= ADAP_SYSCTL_CTX;
817 * Firmware event queue
824 * Management queue. This is just a control queue that uses the fwq as
827 rc = alloc_mgmtq(sc);
836 t4_teardown_adapter_queues(struct adapter *sc)
839 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
841 /* Do this before freeing the queue */
842 if (sc->flags & ADAP_SYSCTL_CTX) {
843 sysctl_ctx_free(&sc->ctx);
844 sc->flags &= ~ADAP_SYSCTL_CTX;
854 port_intr_count(struct port_info *pi)
858 if (pi->flags & INTR_RXQ)
861 if (pi->flags & INTR_OFLD_RXQ)
865 if (pi->flags & INTR_NM_RXQ)
872 first_vector(struct port_info *pi)
874 struct adapter *sc = pi->adapter;
875 int rc = T4_EXTRA_INTR, i;
877 if (sc->intr_count == 1)
880 for_each_port(sc, i) {
881 if (i == pi->port_id)
884 rc += port_intr_count(sc->port[i]);
891 * Given an arbitrary "index," come up with an iq that can be used by other
892 * queues (of this port) for interrupt forwarding, SGE egress updates, etc.
893 * The iq returned is guaranteed to be something that takes direct interrupts.
895 static struct sge_iq *
896 port_intr_iq(struct port_info *pi, int idx)
898 struct adapter *sc = pi->adapter;
899 struct sge *s = &sc->sge;
900 struct sge_iq *iq = NULL;
903 if (sc->intr_count == 1)
904 return (&sc->sge.fwq);
906 nintr = port_intr_count(pi);
908 ("%s: pi %p has no exclusive interrupts, total interrupts = %d",
909 __func__, pi, sc->intr_count));
911 /* Exclude netmap queues as they can't take anyone else's interrupts */
912 if (pi->flags & INTR_NM_RXQ)
915 ("%s: pi %p has nintr %d after netmap adjustment of %d", __func__,
916 pi, nintr, pi->nnmrxq));
920 if (pi->flags & INTR_RXQ) {
922 iq = &s->rxq[pi->first_rxq + i].iq;
928 if (pi->flags & INTR_OFLD_RXQ) {
929 if (i < pi->nofldrxq) {
930 iq = &s->ofld_rxq[pi->first_ofld_rxq + i].iq;
936 panic("%s: pi %p, intr_flags 0x%lx, idx %d, total intr %d\n", __func__,
937 pi, pi->flags & INTR_ALL, idx, nintr);
940 KASSERT(iq->flags & IQ_INTR,
941 ("%s: iq %p (port %p, intr_flags 0x%lx, idx %d)", __func__, iq, pi,
942 pi->flags & INTR_ALL, idx));
946 /* Maximum payload that can be delivered with a single iq descriptor */
948 mtu_to_max_payload(struct adapter *sc, int mtu, const int toe)
954 payload = sc->tt.rx_coalesce ?
955 G_RXCOALESCESIZE(t4_read_reg(sc, A_TP_PARA_REG2)) : mtu;
958 /* large enough even when hw VLAN extraction is disabled */
959 payload = fl_pktshift + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN +
969 t4_setup_port_queues(struct port_info *pi)
971 int rc = 0, i, j, intr_idx, iqid;
974 struct sge_wrq *ctrlq;
976 struct sge_ofld_rxq *ofld_rxq;
977 struct sge_wrq *ofld_txq;
980 struct sge_nm_rxq *nm_rxq;
981 struct sge_nm_txq *nm_txq;
984 struct adapter *sc = pi->adapter;
985 struct ifnet *ifp = pi->ifp;
986 struct sysctl_oid *oid = device_get_sysctl_tree(pi->dev);
987 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
988 int maxp, mtu = ifp->if_mtu;
990 /* Interrupt vector to start from (when using multiple vectors) */
991 intr_idx = first_vector(pi);
994 * First pass over all NIC and TOE rx queues:
995 * a) initialize iq and fl
996 * b) allocate queue iff it will take direct interrupts.
998 maxp = mtu_to_max_payload(sc, mtu, 0);
999 if (pi->flags & INTR_RXQ) {
1000 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "rxq",
1001 CTLFLAG_RD, NULL, "rx queues");
1003 for_each_rxq(pi, i, rxq) {
1005 init_iq(&rxq->iq, sc, pi->tmr_idx, pi->pktc_idx, pi->qsize_rxq);
1007 snprintf(name, sizeof(name), "%s rxq%d-fl",
1008 device_get_nameunit(pi->dev), i);
1009 init_fl(sc, &rxq->fl, pi->qsize_rxq / 8, maxp, name);
1011 if (pi->flags & INTR_RXQ) {
1012 rxq->iq.flags |= IQ_INTR;
1013 rc = alloc_rxq(pi, rxq, intr_idx, i, oid);
1020 maxp = mtu_to_max_payload(sc, mtu, 1);
1021 if (is_offload(sc) && pi->flags & INTR_OFLD_RXQ) {
1022 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ofld_rxq",
1024 "rx queues for offloaded TCP connections");
1026 for_each_ofld_rxq(pi, i, ofld_rxq) {
1028 init_iq(&ofld_rxq->iq, sc, pi->tmr_idx, pi->pktc_idx,
1031 snprintf(name, sizeof(name), "%s ofld_rxq%d-fl",
1032 device_get_nameunit(pi->dev), i);
1033 init_fl(sc, &ofld_rxq->fl, pi->qsize_rxq / 8, maxp, name);
1035 if (pi->flags & INTR_OFLD_RXQ) {
1036 ofld_rxq->iq.flags |= IQ_INTR;
1037 rc = alloc_ofld_rxq(pi, ofld_rxq, intr_idx, i, oid);
1046 * We don't have buffers to back the netmap rx queues right now so we
1047 * create the queues in a way that doesn't set off any congestion signal
1050 if (pi->flags & INTR_NM_RXQ) {
1051 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "nm_rxq",
1052 CTLFLAG_RD, NULL, "rx queues for netmap");
1053 for_each_nm_rxq(pi, i, nm_rxq) {
1054 rc = alloc_nm_rxq(pi, nm_rxq, intr_idx, i, oid);
1063 * Second pass over all NIC and TOE rx queues. The queues forwarding
1064 * their interrupts are allocated now.
1067 if (!(pi->flags & INTR_RXQ)) {
1068 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "rxq",
1069 CTLFLAG_RD, NULL, "rx queues");
1070 for_each_rxq(pi, i, rxq) {
1071 MPASS(!(rxq->iq.flags & IQ_INTR));
1073 intr_idx = port_intr_iq(pi, j)->abs_id;
1075 rc = alloc_rxq(pi, rxq, intr_idx, i, oid);
1082 if (is_offload(sc) && !(pi->flags & INTR_OFLD_RXQ)) {
1083 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ofld_rxq",
1085 "rx queues for offloaded TCP connections");
1086 for_each_ofld_rxq(pi, i, ofld_rxq) {
1087 MPASS(!(ofld_rxq->iq.flags & IQ_INTR));
1089 intr_idx = port_intr_iq(pi, j)->abs_id;
1091 rc = alloc_ofld_rxq(pi, ofld_rxq, intr_idx, i, oid);
1099 if (!(pi->flags & INTR_NM_RXQ))
1100 CXGBE_UNIMPLEMENTED(__func__);
1104 * Now the tx queues. Only one pass needed.
1106 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "txq", CTLFLAG_RD,
1109 for_each_txq(pi, i, txq) {
1110 iqid = port_intr_iq(pi, j)->cntxt_id;
1111 snprintf(name, sizeof(name), "%s txq%d",
1112 device_get_nameunit(pi->dev), i);
1113 init_eq(&txq->eq, EQ_ETH, pi->qsize_txq, pi->tx_chan, iqid,
1116 rc = alloc_txq(pi, txq, i, oid);
1122 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ofld_txq",
1123 CTLFLAG_RD, NULL, "tx queues for offloaded TCP connections");
1124 for_each_ofld_txq(pi, i, ofld_txq) {
1125 struct sysctl_oid *oid2;
1127 iqid = port_intr_iq(pi, j)->cntxt_id;
1128 snprintf(name, sizeof(name), "%s ofld_txq%d",
1129 device_get_nameunit(pi->dev), i);
1130 init_eq(&ofld_txq->eq, EQ_OFLD, pi->qsize_txq, pi->tx_chan,
1133 snprintf(name, sizeof(name), "%d", i);
1134 oid2 = SYSCTL_ADD_NODE(&pi->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1135 name, CTLFLAG_RD, NULL, "offload tx queue");
1137 rc = alloc_wrq(sc, pi, ofld_txq, oid2);
1144 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "nm_txq",
1145 CTLFLAG_RD, NULL, "tx queues for netmap use");
1146 for_each_nm_txq(pi, i, nm_txq) {
1147 iqid = pi->first_nm_rxq + (j % pi->nnmrxq);
1148 rc = alloc_nm_txq(pi, nm_txq, iqid, i, oid);
1156 * Finally, the control queue.
1158 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ctrlq", CTLFLAG_RD,
1159 NULL, "ctrl queue");
1160 ctrlq = &sc->sge.ctrlq[pi->port_id];
1161 iqid = port_intr_iq(pi, 0)->cntxt_id;
1162 snprintf(name, sizeof(name), "%s ctrlq", device_get_nameunit(pi->dev));
1163 init_eq(&ctrlq->eq, EQ_CTRL, CTRL_EQ_QSIZE, pi->tx_chan, iqid, name);
1164 rc = alloc_wrq(sc, pi, ctrlq, oid);
1168 t4_teardown_port_queues(pi);
1177 t4_teardown_port_queues(struct port_info *pi)
1180 struct adapter *sc = pi->adapter;
1181 struct sge_rxq *rxq;
1182 struct sge_txq *txq;
1184 struct sge_ofld_rxq *ofld_rxq;
1185 struct sge_wrq *ofld_txq;
1188 struct sge_nm_rxq *nm_rxq;
1189 struct sge_nm_txq *nm_txq;
1192 /* Do this before freeing the queues */
1193 if (pi->flags & PORT_SYSCTL_CTX) {
1194 sysctl_ctx_free(&pi->ctx);
1195 pi->flags &= ~PORT_SYSCTL_CTX;
1199 * Take down all the tx queues first, as they reference the rx queues
1200 * (for egress updates, etc.).
1203 free_wrq(sc, &sc->sge.ctrlq[pi->port_id]);
1205 for_each_txq(pi, i, txq) {
1209 for_each_ofld_txq(pi, i, ofld_txq) {
1210 free_wrq(sc, ofld_txq);
1214 for_each_nm_txq(pi, i, nm_txq)
1215 free_nm_txq(pi, nm_txq);
1219 * Then take down the rx queues that forward their interrupts, as they
1220 * reference other rx queues.
1223 for_each_rxq(pi, i, rxq) {
1224 if ((rxq->iq.flags & IQ_INTR) == 0)
1228 for_each_ofld_rxq(pi, i, ofld_rxq) {
1229 if ((ofld_rxq->iq.flags & IQ_INTR) == 0)
1230 free_ofld_rxq(pi, ofld_rxq);
1234 for_each_nm_rxq(pi, i, nm_rxq)
1235 free_nm_rxq(pi, nm_rxq);
1239 * Then take down the rx queues that take direct interrupts.
1242 for_each_rxq(pi, i, rxq) {
1243 if (rxq->iq.flags & IQ_INTR)
1247 for_each_ofld_rxq(pi, i, ofld_rxq) {
1248 if (ofld_rxq->iq.flags & IQ_INTR)
1249 free_ofld_rxq(pi, ofld_rxq);
1257 * Deals with errors and the firmware event queue. All data rx queues forward
1258 * their interrupt to the firmware event queue.
1261 t4_intr_all(void *arg)
1263 struct adapter *sc = arg;
1264 struct sge_iq *fwq = &sc->sge.fwq;
1267 if (atomic_cmpset_int(&fwq->state, IQS_IDLE, IQS_BUSY)) {
1269 atomic_cmpset_int(&fwq->state, IQS_BUSY, IQS_IDLE);
1273 /* Deals with error interrupts */
1275 t4_intr_err(void *arg)
1277 struct adapter *sc = arg;
1279 t4_write_reg(sc, MYPF_REG(A_PCIE_PF_CLI), 0);
1280 t4_slow_intr_handler(sc);
1284 t4_intr_evt(void *arg)
1286 struct sge_iq *iq = arg;
1288 if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) {
1290 atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE);
1297 struct sge_iq *iq = arg;
1299 if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) {
1301 atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE);
1306 * Deals with anything and everything on the given ingress queue.
1309 service_iq(struct sge_iq *iq, int budget)
1312 struct sge_rxq *rxq = iq_to_rxq(iq); /* Use iff iq is part of rxq */
1313 struct sge_fl *fl; /* Use iff IQ_HAS_FL */
1314 struct adapter *sc = iq->adapter;
1315 struct iq_desc *d = &iq->desc[iq->cidx];
1316 int ndescs = 0, limit;
1317 int rsp_type, refill;
1319 uint16_t fl_hw_cidx;
1321 STAILQ_HEAD(, sge_iq) iql = STAILQ_HEAD_INITIALIZER(iql);
1322 #if defined(INET) || defined(INET6)
1323 const struct timeval lro_timeout = {0, sc->lro_timeout};
1326 KASSERT(iq->state == IQS_BUSY, ("%s: iq %p not BUSY", __func__, iq));
1328 limit = budget ? budget : iq->qsize / 16;
1330 if (iq->flags & IQ_HAS_FL) {
1332 fl_hw_cidx = fl->hw_cidx; /* stable snapshot */
1335 fl_hw_cidx = 0; /* to silence gcc warning */
1339 * We always come back and check the descriptor ring for new indirect
1340 * interrupts and other responses after running a single handler.
1343 while ((d->rsp.u.type_gen & F_RSPD_GEN) == iq->gen) {
1349 rsp_type = G_RSPD_TYPE(d->rsp.u.type_gen);
1350 lq = be32toh(d->rsp.pldbuflen_qid);
1353 case X_RSPD_TYPE_FLBUF:
1355 KASSERT(iq->flags & IQ_HAS_FL,
1356 ("%s: data for an iq (%p) with no freelist",
1359 m0 = get_fl_payload(sc, fl, lq);
1360 if (__predict_false(m0 == NULL))
1362 refill = IDXDIFF(fl->hw_cidx, fl_hw_cidx, fl->sidx) > 2;
1363 #ifdef T4_PKT_TIMESTAMP
1365 * 60 bit timestamp for the payload is
1366 * *(uint64_t *)m0->m_pktdat. Note that it is
1367 * in the leading free-space in the mbuf. The
1368 * kernel can clobber it during a pullup,
1369 * m_copymdata, etc. You need to make sure that
1370 * the mbuf reaches you unmolested if you care
1371 * about the timestamp.
1373 *(uint64_t *)m0->m_pktdat =
1374 be64toh(ctrl->u.last_flit) &
1380 case X_RSPD_TYPE_CPL:
1381 KASSERT(d->rss.opcode < NUM_CPL_CMDS,
1382 ("%s: bad opcode %02x.", __func__,
1384 sc->cpl_handler[d->rss.opcode](iq, &d->rss, m0);
1387 case X_RSPD_TYPE_INTR:
1390 * Interrupts should be forwarded only to queues
1391 * that are not forwarding their interrupts.
1392 * This means service_iq can recurse but only 1
1395 KASSERT(budget == 0,
1396 ("%s: budget %u, rsp_type %u", __func__,
1400 * There are 1K interrupt-capable queues (qids 0
1401 * through 1023). A response type indicating a
1402 * forwarded interrupt with a qid >= 1K is an
1403 * iWARP async notification.
1406 sc->an_handler(iq, &d->rsp);
1410 q = sc->sge.iqmap[lq - sc->sge.iq_start];
1411 if (atomic_cmpset_int(&q->state, IQS_IDLE,
1413 if (service_iq(q, q->qsize / 16) == 0) {
1414 atomic_cmpset_int(&q->state,
1415 IQS_BUSY, IQS_IDLE);
1417 STAILQ_INSERT_TAIL(&iql, q,
1425 ("%s: illegal response type %d on iq %p",
1426 __func__, rsp_type, iq));
1428 "%s: illegal response type %d on iq %p",
1429 device_get_nameunit(sc->dev), rsp_type, iq);
1434 if (__predict_false(++iq->cidx == iq->sidx)) {
1436 iq->gen ^= F_RSPD_GEN;
1439 if (__predict_false(++ndescs == limit)) {
1440 t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS),
1442 V_INGRESSQID(iq->cntxt_id) |
1443 V_SEINTARM(V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX)));
1446 #if defined(INET) || defined(INET6)
1447 if (iq->flags & IQ_LRO_ENABLED &&
1448 sc->lro_timeout != 0) {
1449 tcp_lro_flush_inactive(&rxq->lro,
1455 if (iq->flags & IQ_HAS_FL) {
1457 refill_fl(sc, fl, 32);
1460 return (EINPROGRESS);
1465 refill_fl(sc, fl, 32);
1467 fl_hw_cidx = fl->hw_cidx;
1472 if (STAILQ_EMPTY(&iql))
1476 * Process the head only, and send it to the back of the list if
1477 * it's still not done.
1479 q = STAILQ_FIRST(&iql);
1480 STAILQ_REMOVE_HEAD(&iql, link);
1481 if (service_iq(q, q->qsize / 8) == 0)
1482 atomic_cmpset_int(&q->state, IQS_BUSY, IQS_IDLE);
1484 STAILQ_INSERT_TAIL(&iql, q, link);
1487 #if defined(INET) || defined(INET6)
1488 if (iq->flags & IQ_LRO_ENABLED) {
1489 struct lro_ctrl *lro = &rxq->lro;
1490 struct lro_entry *l;
1492 while (!SLIST_EMPTY(&lro->lro_active)) {
1493 l = SLIST_FIRST(&lro->lro_active);
1494 SLIST_REMOVE_HEAD(&lro->lro_active, next);
1495 tcp_lro_flush(lro, l);
1500 t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_CIDXINC(ndescs) |
1501 V_INGRESSQID((u32)iq->cntxt_id) | V_SEINTARM(iq->intr_params));
1503 if (iq->flags & IQ_HAS_FL) {
1507 starved = refill_fl(sc, fl, 64);
1509 if (__predict_false(starved != 0))
1510 add_fl_to_sfl(sc, fl);
1517 cl_has_metadata(struct sge_fl *fl, struct cluster_layout *cll)
1519 int rc = fl->flags & FL_BUF_PACKING || cll->region1 > 0;
1522 MPASS(cll->region3 >= CL_METADATA_SIZE);
1527 static inline struct cluster_metadata *
1528 cl_metadata(struct adapter *sc, struct sge_fl *fl, struct cluster_layout *cll,
1532 if (cl_has_metadata(fl, cll)) {
1533 struct sw_zone_info *swz = &sc->sge.sw_zone_info[cll->zidx];
1535 return ((struct cluster_metadata *)(cl + swz->size) - 1);
1541 rxb_free(struct mbuf *m, void *arg1, void *arg2)
1543 uma_zone_t zone = arg1;
1546 uma_zfree(zone, cl);
1547 counter_u64_add(extfree_rels, 1);
1551 * The mbuf returned by this function could be allocated from zone_mbuf or
1552 * constructed in spare room in the cluster.
1554 * The mbuf carries the payload in one of these ways
1555 * a) frame inside the mbuf (mbuf from zone_mbuf)
1556 * b) m_cljset (for clusters without metadata) zone_mbuf
1557 * c) m_extaddref (cluster with metadata) inline mbuf
1558 * d) m_extaddref (cluster with metadata) zone_mbuf
1560 static struct mbuf *
1561 get_scatter_segment(struct adapter *sc, struct sge_fl *fl, int fr_offset,
1565 struct fl_sdesc *sd = &fl->sdesc[fl->cidx];
1566 struct cluster_layout *cll = &sd->cll;
1567 struct sw_zone_info *swz = &sc->sge.sw_zone_info[cll->zidx];
1568 struct hw_buf_info *hwb = &sc->sge.hw_buf_info[cll->hwidx];
1569 struct cluster_metadata *clm = cl_metadata(sc, fl, cll, sd->cl);
1573 blen = hwb->size - fl->rx_offset; /* max possible in this buf */
1574 len = min(remaining, blen);
1575 payload = sd->cl + cll->region1 + fl->rx_offset;
1576 if (fl->flags & FL_BUF_PACKING) {
1577 const u_int l = fr_offset + len;
1578 const u_int pad = roundup2(l, fl->buf_boundary) - l;
1580 if (fl->rx_offset + len + pad < hwb->size)
1582 MPASS(fl->rx_offset + blen <= hwb->size);
1584 MPASS(fl->rx_offset == 0); /* not packing */
1588 if (sc->sc_do_rxcopy && len < RX_COPY_THRESHOLD) {
1591 * Copy payload into a freshly allocated mbuf.
1594 m = fr_offset == 0 ?
1595 m_gethdr(M_NOWAIT, MT_DATA) : m_get(M_NOWAIT, MT_DATA);
1598 fl->mbuf_allocated++;
1599 #ifdef T4_PKT_TIMESTAMP
1600 /* Leave room for a timestamp */
1603 /* copy data to mbuf */
1604 bcopy(payload, mtod(m, caddr_t), len);
1606 } else if (sd->nmbuf * MSIZE < cll->region1) {
1609 * There's spare room in the cluster for an mbuf. Create one
1610 * and associate it with the payload that's in the cluster.
1614 m = (struct mbuf *)(sd->cl + sd->nmbuf * MSIZE);
1615 /* No bzero required */
1616 if (m_init(m, NULL, 0, M_NOWAIT, MT_DATA,
1617 fr_offset == 0 ? M_PKTHDR | M_NOFREE : M_NOFREE))
1620 m_extaddref(m, payload, blen, &clm->refcount, rxb_free,
1622 if (sd->nmbuf++ == 0)
1623 counter_u64_add(extfree_refs, 1);
1628 * Grab an mbuf from zone_mbuf and associate it with the
1629 * payload in the cluster.
1632 m = fr_offset == 0 ?
1633 m_gethdr(M_NOWAIT, MT_DATA) : m_get(M_NOWAIT, MT_DATA);
1636 fl->mbuf_allocated++;
1638 m_extaddref(m, payload, blen, &clm->refcount,
1639 rxb_free, swz->zone, sd->cl);
1640 if (sd->nmbuf++ == 0)
1641 counter_u64_add(extfree_refs, 1);
1643 m_cljset(m, sd->cl, swz->type);
1644 sd->cl = NULL; /* consumed, not a recycle candidate */
1648 m->m_pkthdr.len = remaining;
1651 if (fl->flags & FL_BUF_PACKING) {
1652 fl->rx_offset += blen;
1653 MPASS(fl->rx_offset <= hwb->size);
1654 if (fl->rx_offset < hwb->size)
1655 return (m); /* without advancing the cidx */
1658 if (__predict_false(++fl->cidx % 8 == 0)) {
1659 uint16_t cidx = fl->cidx / 8;
1661 if (__predict_false(cidx == fl->sidx))
1662 fl->cidx = cidx = 0;
1670 static struct mbuf *
1671 get_fl_payload(struct adapter *sc, struct sge_fl *fl, uint32_t len_newbuf)
1673 struct mbuf *m0, *m, **pnext;
1675 const u_int total = G_RSPD_LEN(len_newbuf);
1677 if (__predict_false(fl->flags & FL_BUF_RESUME)) {
1678 M_ASSERTPKTHDR(fl->m0);
1679 MPASS(fl->m0->m_pkthdr.len == total);
1680 MPASS(fl->remaining < total);
1684 remaining = fl->remaining;
1685 fl->flags &= ~FL_BUF_RESUME;
1689 if (fl->rx_offset > 0 && len_newbuf & F_RSPD_NEWBUF) {
1691 if (__predict_false(++fl->cidx % 8 == 0)) {
1692 uint16_t cidx = fl->cidx / 8;
1694 if (__predict_false(cidx == fl->sidx))
1695 fl->cidx = cidx = 0;
1701 * Payload starts at rx_offset in the current hw buffer. Its length is
1702 * 'len' and it may span multiple hw buffers.
1705 m0 = get_scatter_segment(sc, fl, 0, total);
1708 remaining = total - m0->m_len;
1709 pnext = &m0->m_next;
1710 while (remaining > 0) {
1712 MPASS(fl->rx_offset == 0);
1713 m = get_scatter_segment(sc, fl, total - remaining, remaining);
1714 if (__predict_false(m == NULL)) {
1717 fl->remaining = remaining;
1718 fl->flags |= FL_BUF_RESUME;
1723 remaining -= m->m_len;
1731 t4_eth_rx(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m0)
1733 struct sge_rxq *rxq = iq_to_rxq(iq);
1734 struct ifnet *ifp = rxq->ifp;
1735 const struct cpl_rx_pkt *cpl = (const void *)(rss + 1);
1736 #if defined(INET) || defined(INET6)
1737 struct lro_ctrl *lro = &rxq->lro;
1740 KASSERT(m0 != NULL, ("%s: no payload with opcode %02x", __func__,
1743 m0->m_pkthdr.len -= fl_pktshift;
1744 m0->m_len -= fl_pktshift;
1745 m0->m_data += fl_pktshift;
1747 m0->m_pkthdr.rcvif = ifp;
1748 M_HASHTYPE_SET(m0, M_HASHTYPE_OPAQUE);
1749 m0->m_pkthdr.flowid = be32toh(rss->hash_val);
1751 if (cpl->csum_calc && !cpl->err_vec) {
1752 if (ifp->if_capenable & IFCAP_RXCSUM &&
1753 cpl->l2info & htobe32(F_RXF_IP)) {
1754 m0->m_pkthdr.csum_flags = (CSUM_IP_CHECKED |
1755 CSUM_IP_VALID | CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1757 } else if (ifp->if_capenable & IFCAP_RXCSUM_IPV6 &&
1758 cpl->l2info & htobe32(F_RXF_IP6)) {
1759 m0->m_pkthdr.csum_flags = (CSUM_DATA_VALID_IPV6 |
1764 if (__predict_false(cpl->ip_frag))
1765 m0->m_pkthdr.csum_data = be16toh(cpl->csum);
1767 m0->m_pkthdr.csum_data = 0xffff;
1771 m0->m_pkthdr.ether_vtag = be16toh(cpl->vlan);
1772 m0->m_flags |= M_VLANTAG;
1773 rxq->vlan_extraction++;
1776 #if defined(INET) || defined(INET6)
1777 if (cpl->l2info & htobe32(F_RXF_LRO) &&
1778 iq->flags & IQ_LRO_ENABLED &&
1779 tcp_lro_rx(lro, m0, 0) == 0) {
1780 /* queued for LRO */
1783 ifp->if_input(ifp, m0);
1789 * Must drain the wrq or make sure that someone else will.
1792 wrq_tx_drain(void *arg, int n)
1794 struct sge_wrq *wrq = arg;
1795 struct sge_eq *eq = &wrq->eq;
1798 if (TAILQ_EMPTY(&wrq->incomplete_wrs) && !STAILQ_EMPTY(&wrq->wr_list))
1799 drain_wrq_wr_list(wrq->adapter, wrq);
1804 drain_wrq_wr_list(struct adapter *sc, struct sge_wrq *wrq)
1806 struct sge_eq *eq = &wrq->eq;
1807 u_int available, dbdiff; /* # of hardware descriptors */
1810 struct fw_eth_tx_pkt_wr *dst; /* any fw WR struct will do */
1812 EQ_LOCK_ASSERT_OWNED(eq);
1813 MPASS(TAILQ_EMPTY(&wrq->incomplete_wrs));
1814 wr = STAILQ_FIRST(&wrq->wr_list);
1815 MPASS(wr != NULL); /* Must be called with something useful to do */
1816 dbdiff = IDXDIFF(eq->pidx, eq->dbidx, eq->sidx);
1819 eq->cidx = read_hw_cidx(eq);
1820 if (eq->pidx == eq->cidx)
1821 available = eq->sidx - 1;
1823 available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
1825 MPASS(wr->wrq == wrq);
1826 n = howmany(wr->wr_len, EQ_ESIZE);
1830 dst = (void *)&eq->desc[eq->pidx];
1831 if (__predict_true(eq->sidx - eq->pidx > n)) {
1832 /* Won't wrap, won't end exactly at the status page. */
1833 bcopy(&wr->wr[0], dst, wr->wr_len);
1836 int first_portion = (eq->sidx - eq->pidx) * EQ_ESIZE;
1838 bcopy(&wr->wr[0], dst, first_portion);
1839 if (wr->wr_len > first_portion) {
1840 bcopy(&wr->wr[first_portion], &eq->desc[0],
1841 wr->wr_len - first_portion);
1843 eq->pidx = n - (eq->sidx - eq->pidx);
1846 if (available < eq->sidx / 4 &&
1847 atomic_cmpset_int(&eq->equiq, 0, 1)) {
1848 dst->equiq_to_len16 |= htobe32(F_FW_WR_EQUIQ |
1850 eq->equeqidx = eq->pidx;
1851 } else if (IDXDIFF(eq->pidx, eq->equeqidx, eq->sidx) >= 32) {
1852 dst->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ);
1853 eq->equeqidx = eq->pidx;
1858 ring_eq_db(sc, eq, dbdiff);
1862 STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
1864 MPASS(wrq->nwr_pending > 0);
1866 MPASS(wrq->ndesc_needed >= n);
1867 wrq->ndesc_needed -= n;
1868 } while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL);
1871 ring_eq_db(sc, eq, dbdiff);
1875 * Doesn't fail. Holds on to work requests it can't send right away.
1878 t4_wrq_tx_locked(struct adapter *sc, struct sge_wrq *wrq, struct wrqe *wr)
1881 struct sge_eq *eq = &wrq->eq;
1884 EQ_LOCK_ASSERT_OWNED(eq);
1886 MPASS(wr->wr_len > 0 && wr->wr_len <= SGE_MAX_WR_LEN);
1887 MPASS((wr->wr_len & 0x7) == 0);
1889 STAILQ_INSERT_TAIL(&wrq->wr_list, wr, link);
1891 wrq->ndesc_needed += howmany(wr->wr_len, EQ_ESIZE);
1893 if (!TAILQ_EMPTY(&wrq->incomplete_wrs))
1894 return; /* commit_wrq_wr will drain wr_list as well. */
1896 drain_wrq_wr_list(sc, wrq);
1898 /* Doorbell must have caught up to the pidx. */
1899 MPASS(eq->pidx == eq->dbidx);
1903 t4_update_fl_bufsize(struct ifnet *ifp)
1905 struct port_info *pi = ifp->if_softc;
1906 struct adapter *sc = pi->adapter;
1907 struct sge_rxq *rxq;
1909 struct sge_ofld_rxq *ofld_rxq;
1912 int i, maxp, mtu = ifp->if_mtu;
1914 maxp = mtu_to_max_payload(sc, mtu, 0);
1915 for_each_rxq(pi, i, rxq) {
1919 find_best_refill_source(sc, fl, maxp);
1923 maxp = mtu_to_max_payload(sc, mtu, 1);
1924 for_each_ofld_rxq(pi, i, ofld_rxq) {
1928 find_best_refill_source(sc, fl, maxp);
1935 mbuf_nsegs(struct mbuf *m)
1939 KASSERT(m->m_pkthdr.l5hlen > 0,
1940 ("%s: mbuf %p missing information on # of segments.", __func__, m));
1942 return (m->m_pkthdr.l5hlen);
1946 set_mbuf_nsegs(struct mbuf *m, uint8_t nsegs)
1950 m->m_pkthdr.l5hlen = nsegs;
1954 mbuf_len16(struct mbuf *m)
1959 n = m->m_pkthdr.PH_loc.eight[0];
1960 MPASS(n > 0 && n <= SGE_MAX_WR_LEN / 16);
1966 set_mbuf_len16(struct mbuf *m, uint8_t len16)
1970 m->m_pkthdr.PH_loc.eight[0] = len16;
1974 needs_tso(struct mbuf *m)
1979 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1980 KASSERT(m->m_pkthdr.tso_segsz > 0,
1981 ("%s: TSO requested in mbuf %p but MSS not provided",
1990 needs_l3_csum(struct mbuf *m)
1995 if (m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TSO))
2001 needs_l4_csum(struct mbuf *m)
2006 if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP | CSUM_UDP_IPV6 |
2007 CSUM_TCP_IPV6 | CSUM_TSO))
2013 needs_vlan_insertion(struct mbuf *m)
2018 if (m->m_flags & M_VLANTAG) {
2019 KASSERT(m->m_pkthdr.ether_vtag != 0,
2020 ("%s: HWVLAN requested in mbuf %p but tag not provided",
2028 m_advance(struct mbuf **pm, int *poffset, int len)
2030 struct mbuf *m = *pm;
2031 int offset = *poffset;
2037 if (offset + len < m->m_len) {
2039 p = mtod(m, uintptr_t) + offset;
2042 len -= m->m_len - offset;
2053 same_paddr(char *a, char *b)
2058 else if (a != NULL && b != NULL) {
2059 vm_offset_t x = (vm_offset_t)a;
2060 vm_offset_t y = (vm_offset_t)b;
2062 if ((x & PAGE_MASK) == (y & PAGE_MASK) &&
2063 pmap_kextract(x) == pmap_kextract(y))
2071 * Can deal with empty mbufs in the chain that have m_len = 0, but the chain
2072 * must have at least one mbuf that's not empty.
2075 count_mbuf_nsegs(struct mbuf *m)
2077 char *prev_end, *start;
2084 for (; m; m = m->m_next) {
2087 if (__predict_false(len == 0))
2089 start = mtod(m, char *);
2091 nsegs += sglist_count(start, len);
2092 if (same_paddr(prev_end, start))
2094 prev_end = start + len;
2102 * Analyze the mbuf to determine its tx needs. The mbuf passed in may change:
2103 * a) caller can assume it's been freed if this function returns with an error.
2104 * b) it may get defragged up if the gather list is too long for the hardware.
2107 parse_pkt(struct mbuf **mp)
2109 struct mbuf *m0 = *mp, *m;
2110 int rc, nsegs, defragged = 0, offset;
2111 struct ether_header *eh;
2113 #if defined(INET) || defined(INET6)
2119 if (__predict_false(m0->m_pkthdr.len < ETHER_HDR_LEN)) {
2128 * First count the number of gather list segments in the payload.
2129 * Defrag the mbuf if nsegs exceeds the hardware limit.
2132 MPASS(m0->m_pkthdr.len > 0);
2133 nsegs = count_mbuf_nsegs(m0);
2134 if (nsegs > (needs_tso(m0) ? TX_SGL_SEGS_TSO : TX_SGL_SEGS)) {
2135 if (defragged++ > 0 || (m = m_defrag(m0, M_NOWAIT)) == NULL) {
2139 *mp = m0 = m; /* update caller's copy after defrag */
2143 if (__predict_false(nsegs > 2 && m0->m_pkthdr.len <= MHLEN)) {
2144 m0 = m_pullup(m0, m0->m_pkthdr.len);
2146 /* Should have left well enough alone. */
2150 *mp = m0; /* update caller's copy after pullup */
2153 set_mbuf_nsegs(m0, nsegs);
2154 set_mbuf_len16(m0, txpkt_len16(nsegs, needs_tso(m0)));
2160 eh = mtod(m, struct ether_header *);
2161 eh_type = ntohs(eh->ether_type);
2162 if (eh_type == ETHERTYPE_VLAN) {
2163 struct ether_vlan_header *evh = (void *)eh;
2165 eh_type = ntohs(evh->evl_proto);
2166 m0->m_pkthdr.l2hlen = sizeof(*evh);
2168 m0->m_pkthdr.l2hlen = sizeof(*eh);
2171 l3hdr = m_advance(&m, &offset, m0->m_pkthdr.l2hlen);
2175 case ETHERTYPE_IPV6:
2177 struct ip6_hdr *ip6 = l3hdr;
2179 MPASS(ip6->ip6_nxt == IPPROTO_TCP);
2181 m0->m_pkthdr.l3hlen = sizeof(*ip6);
2188 struct ip *ip = l3hdr;
2190 m0->m_pkthdr.l3hlen = ip->ip_hl * 4;
2195 panic("%s: ethertype 0x%04x unknown. if_cxgbe must be compiled"
2196 " with the same INET/INET6 options as the kernel.",
2200 #if defined(INET) || defined(INET6)
2201 tcp = m_advance(&m, &offset, m0->m_pkthdr.l3hlen);
2202 m0->m_pkthdr.l4hlen = tcp->th_off * 4;
2209 start_wrq_wr(struct sge_wrq *wrq, int len16, struct wrq_cookie *cookie)
2211 struct sge_eq *eq = &wrq->eq;
2212 struct adapter *sc = wrq->adapter;
2213 int ndesc, available;
2218 ndesc = howmany(len16, EQ_ESIZE / 16);
2219 MPASS(ndesc > 0 && ndesc <= SGE_MAX_WR_NDESC);
2223 if (!STAILQ_EMPTY(&wrq->wr_list))
2224 drain_wrq_wr_list(sc, wrq);
2226 if (!STAILQ_EMPTY(&wrq->wr_list)) {
2229 wr = alloc_wrqe(len16 * 16, wrq);
2230 if (__predict_false(wr == NULL))
2233 cookie->ndesc = ndesc;
2237 eq->cidx = read_hw_cidx(eq);
2238 if (eq->pidx == eq->cidx)
2239 available = eq->sidx - 1;
2241 available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
2242 if (available < ndesc)
2245 cookie->pidx = eq->pidx;
2246 cookie->ndesc = ndesc;
2247 TAILQ_INSERT_TAIL(&wrq->incomplete_wrs, cookie, link);
2249 w = &eq->desc[eq->pidx];
2250 IDXINCR(eq->pidx, ndesc, eq->sidx);
2251 if (__predict_false(eq->pidx < ndesc - 1)) {
2253 wrq->ss_pidx = cookie->pidx;
2254 wrq->ss_len = len16 * 16;
2263 commit_wrq_wr(struct sge_wrq *wrq, void *w, struct wrq_cookie *cookie)
2265 struct sge_eq *eq = &wrq->eq;
2266 struct adapter *sc = wrq->adapter;
2268 struct wrq_cookie *prev, *next;
2270 if (cookie->pidx == -1) {
2271 struct wrqe *wr = __containerof(w, struct wrqe, wr);
2277 ndesc = cookie->ndesc; /* Can be more than SGE_MAX_WR_NDESC here. */
2278 pidx = cookie->pidx;
2279 MPASS(pidx >= 0 && pidx < eq->sidx);
2280 if (__predict_false(w == &wrq->ss[0])) {
2281 int n = (eq->sidx - wrq->ss_pidx) * EQ_ESIZE;
2283 MPASS(wrq->ss_len > n); /* WR had better wrap around. */
2284 bcopy(&wrq->ss[0], &eq->desc[wrq->ss_pidx], n);
2285 bcopy(&wrq->ss[n], &eq->desc[0], wrq->ss_len - n);
2288 wrq->tx_wrs_direct++;
2291 prev = TAILQ_PREV(cookie, wrq_incomplete_wrs, link);
2292 next = TAILQ_NEXT(cookie, link);
2294 MPASS(pidx == eq->dbidx);
2295 if (next == NULL || ndesc >= 16)
2296 ring_eq_db(wrq->adapter, eq, ndesc);
2298 MPASS(IDXDIFF(next->pidx, pidx, eq->sidx) == ndesc);
2300 next->ndesc += ndesc;
2303 MPASS(IDXDIFF(pidx, prev->pidx, eq->sidx) == prev->ndesc);
2304 prev->ndesc += ndesc;
2306 TAILQ_REMOVE(&wrq->incomplete_wrs, cookie, link);
2308 if (TAILQ_EMPTY(&wrq->incomplete_wrs) && !STAILQ_EMPTY(&wrq->wr_list))
2309 drain_wrq_wr_list(sc, wrq);
2312 if (TAILQ_EMPTY(&wrq->incomplete_wrs)) {
2313 /* Doorbell must have caught up to the pidx. */
2314 MPASS(wrq->eq.pidx == wrq->eq.dbidx);
2321 can_resume_eth_tx(struct mp_ring *r)
2323 struct sge_eq *eq = r->cookie;
2325 return (total_available_tx_desc(eq) > eq->sidx / 8);
2329 cannot_use_txpkts(struct mbuf *m)
2331 /* maybe put a GL limit too, to avoid silliness? */
2333 return (needs_tso(m));
2337 * r->items[cidx] to r->items[pidx], with a wraparound at r->size, are ready to
2338 * be consumed. Return the actual number consumed. 0 indicates a stall.
2341 eth_tx(struct mp_ring *r, u_int cidx, u_int pidx)
2343 struct sge_txq *txq = r->cookie;
2344 struct sge_eq *eq = &txq->eq;
2345 struct ifnet *ifp = txq->ifp;
2346 struct port_info *pi = (void *)ifp->if_softc;
2347 struct adapter *sc = pi->adapter;
2348 u_int total, remaining; /* # of packets */
2349 u_int available, dbdiff; /* # of hardware descriptors */
2351 struct mbuf *m0, *tail;
2353 struct fw_eth_tx_pkts_wr *wr; /* any fw WR struct will do */
2355 remaining = IDXDIFF(pidx, cidx, r->size);
2356 MPASS(remaining > 0); /* Must not be called without work to do. */
2360 if (__predict_false((eq->flags & EQ_ENABLED) == 0)) {
2361 while (cidx != pidx) {
2362 m0 = r->items[cidx];
2364 if (++cidx == r->size)
2367 reclaim_tx_descs(txq, 2048);
2372 /* How many hardware descriptors do we have readily available. */
2373 if (eq->pidx == eq->cidx)
2374 available = eq->sidx - 1;
2376 available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
2377 dbdiff = IDXDIFF(eq->pidx, eq->dbidx, eq->sidx);
2379 while (remaining > 0) {
2381 m0 = r->items[cidx];
2383 MPASS(m0->m_nextpkt == NULL);
2385 if (available < SGE_MAX_WR_NDESC) {
2386 available += reclaim_tx_descs(txq, 64);
2387 if (available < howmany(mbuf_len16(m0), EQ_ESIZE / 16))
2388 break; /* out of descriptors */
2391 next_cidx = cidx + 1;
2392 if (__predict_false(next_cidx == r->size))
2395 wr = (void *)&eq->desc[eq->pidx];
2396 if (remaining > 1 &&
2397 try_txpkts(m0, r->items[next_cidx], &txp, available) == 0) {
2399 /* pkts at cidx, next_cidx should both be in txp. */
2400 MPASS(txp.npkt == 2);
2401 tail = r->items[next_cidx];
2402 MPASS(tail->m_nextpkt == NULL);
2403 ETHER_BPF_MTAP(ifp, m0);
2404 ETHER_BPF_MTAP(ifp, tail);
2405 m0->m_nextpkt = tail;
2407 if (__predict_false(++next_cidx == r->size))
2410 while (next_cidx != pidx) {
2411 if (add_to_txpkts(r->items[next_cidx], &txp,
2414 tail->m_nextpkt = r->items[next_cidx];
2415 tail = tail->m_nextpkt;
2416 ETHER_BPF_MTAP(ifp, tail);
2417 if (__predict_false(++next_cidx == r->size))
2421 n = write_txpkts_wr(txq, wr, m0, &txp, available);
2423 remaining -= txp.npkt;
2427 n = write_txpkt_wr(txq, (void *)wr, m0, available);
2428 ETHER_BPF_MTAP(ifp, m0);
2430 MPASS(n >= 1 && n <= available && n <= SGE_MAX_WR_NDESC);
2434 IDXINCR(eq->pidx, n, eq->sidx);
2436 if (total_available_tx_desc(eq) < eq->sidx / 4 &&
2437 atomic_cmpset_int(&eq->equiq, 0, 1)) {
2438 wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUIQ |
2440 eq->equeqidx = eq->pidx;
2441 } else if (IDXDIFF(eq->pidx, eq->equeqidx, eq->sidx) >= 32) {
2442 wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ);
2443 eq->equeqidx = eq->pidx;
2446 if (dbdiff >= 16 && remaining >= 4) {
2447 ring_eq_db(sc, eq, dbdiff);
2448 available += reclaim_tx_descs(txq, 4 * dbdiff);
2455 ring_eq_db(sc, eq, dbdiff);
2456 reclaim_tx_descs(txq, 32);
2465 init_iq(struct sge_iq *iq, struct adapter *sc, int tmr_idx, int pktc_idx,
2469 KASSERT(tmr_idx >= 0 && tmr_idx < SGE_NTIMERS,
2470 ("%s: bad tmr_idx %d", __func__, tmr_idx));
2471 KASSERT(pktc_idx < SGE_NCOUNTERS, /* -ve is ok, means don't use */
2472 ("%s: bad pktc_idx %d", __func__, pktc_idx));
2476 iq->intr_params = V_QINTR_TIMER_IDX(tmr_idx);
2477 iq->intr_pktc_idx = SGE_NCOUNTERS - 1;
2478 if (pktc_idx >= 0) {
2479 iq->intr_params |= F_QINTR_CNT_EN;
2480 iq->intr_pktc_idx = pktc_idx;
2482 iq->qsize = roundup2(qsize, 16); /* See FW_IQ_CMD/iqsize */
2483 iq->sidx = iq->qsize - spg_len / IQ_ESIZE;
2487 init_fl(struct adapter *sc, struct sge_fl *fl, int qsize, int maxp, char *name)
2491 fl->sidx = qsize - spg_len / EQ_ESIZE;
2492 strlcpy(fl->lockname, name, sizeof(fl->lockname));
2493 if (sc->flags & BUF_PACKING_OK &&
2494 ((!is_t4(sc) && buffer_packing) || /* T5+: enabled unless 0 */
2495 (is_t4(sc) && buffer_packing == 1)))/* T4: disabled unless 1 */
2496 fl->flags |= FL_BUF_PACKING;
2497 find_best_refill_source(sc, fl, maxp);
2498 find_safe_refill_source(sc, fl);
2502 init_eq(struct sge_eq *eq, int eqtype, int qsize, uint8_t tx_chan,
2503 uint16_t iqid, char *name)
2505 KASSERT(tx_chan < NCHAN, ("%s: bad tx channel %d", __func__, tx_chan));
2506 KASSERT(eqtype <= EQ_TYPEMASK, ("%s: bad qtype %d", __func__, eqtype));
2508 eq->flags = eqtype & EQ_TYPEMASK;
2509 eq->tx_chan = tx_chan;
2511 eq->sidx = qsize - spg_len / EQ_ESIZE;
2512 strlcpy(eq->lockname, name, sizeof(eq->lockname));
2516 alloc_ring(struct adapter *sc, size_t len, bus_dma_tag_t *tag,
2517 bus_dmamap_t *map, bus_addr_t *pa, void **va)
2521 rc = bus_dma_tag_create(sc->dmat, 512, 0, BUS_SPACE_MAXADDR,
2522 BUS_SPACE_MAXADDR, NULL, NULL, len, 1, len, 0, NULL, NULL, tag);
2524 device_printf(sc->dev, "cannot allocate DMA tag: %d\n", rc);
2528 rc = bus_dmamem_alloc(*tag, va,
2529 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, map);
2531 device_printf(sc->dev, "cannot allocate DMA memory: %d\n", rc);
2535 rc = bus_dmamap_load(*tag, *map, *va, len, oneseg_dma_callback, pa, 0);
2537 device_printf(sc->dev, "cannot load DMA map: %d\n", rc);
2542 free_ring(sc, *tag, *map, *pa, *va);
2548 free_ring(struct adapter *sc, bus_dma_tag_t tag, bus_dmamap_t map,
2549 bus_addr_t pa, void *va)
2552 bus_dmamap_unload(tag, map);
2554 bus_dmamem_free(tag, va, map);
2556 bus_dma_tag_destroy(tag);
2562 * Allocates the ring for an ingress queue and an optional freelist. If the
2563 * freelist is specified it will be allocated and then associated with the
2566 * Returns errno on failure. Resources allocated up to that point may still be
2567 * allocated. Caller is responsible for cleanup in case this function fails.
2569 * If the ingress queue will take interrupts directly (iq->flags & IQ_INTR) then
2570 * the intr_idx specifies the vector, starting from 0. Otherwise it specifies
2571 * the abs_id of the ingress queue to which its interrupts should be forwarded.
2574 alloc_iq_fl(struct port_info *pi, struct sge_iq *iq, struct sge_fl *fl,
2575 int intr_idx, int cong)
2577 int rc, i, cntxt_id;
2580 struct adapter *sc = iq->adapter;
2583 len = iq->qsize * IQ_ESIZE;
2584 rc = alloc_ring(sc, len, &iq->desc_tag, &iq->desc_map, &iq->ba,
2585 (void **)&iq->desc);
2589 bzero(&c, sizeof(c));
2590 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_IQ_CMD) | F_FW_CMD_REQUEST |
2591 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_IQ_CMD_PFN(sc->pf) |
2592 V_FW_IQ_CMD_VFN(0));
2594 c.alloc_to_len16 = htobe32(F_FW_IQ_CMD_ALLOC | F_FW_IQ_CMD_IQSTART |
2597 /* Special handling for firmware event queue */
2598 if (iq == &sc->sge.fwq)
2599 v |= F_FW_IQ_CMD_IQASYNCH;
2601 if (iq->flags & IQ_INTR) {
2602 KASSERT(intr_idx < sc->intr_count,
2603 ("%s: invalid direct intr_idx %d", __func__, intr_idx));
2605 v |= F_FW_IQ_CMD_IQANDST;
2606 v |= V_FW_IQ_CMD_IQANDSTINDEX(intr_idx);
2608 c.type_to_iqandstindex = htobe32(v |
2609 V_FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
2610 V_FW_IQ_CMD_VIID(pi->viid) |
2611 V_FW_IQ_CMD_IQANUD(X_UPDATEDELIVERY_INTERRUPT));
2612 c.iqdroprss_to_iqesize = htobe16(V_FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
2613 F_FW_IQ_CMD_IQGTSMODE |
2614 V_FW_IQ_CMD_IQINTCNTTHRESH(iq->intr_pktc_idx) |
2615 V_FW_IQ_CMD_IQESIZE(ilog2(IQ_ESIZE) - 4));
2616 c.iqsize = htobe16(iq->qsize);
2617 c.iqaddr = htobe64(iq->ba);
2619 c.iqns_to_fl0congen = htobe32(F_FW_IQ_CMD_IQFLINTCONGEN);
2622 mtx_init(&fl->fl_lock, fl->lockname, NULL, MTX_DEF);
2624 len = fl->qsize * EQ_ESIZE;
2625 rc = alloc_ring(sc, len, &fl->desc_tag, &fl->desc_map,
2626 &fl->ba, (void **)&fl->desc);
2630 /* Allocate space for one software descriptor per buffer. */
2631 rc = alloc_fl_sdesc(fl);
2633 device_printf(sc->dev,
2634 "failed to setup fl software descriptors: %d\n",
2639 if (fl->flags & FL_BUF_PACKING) {
2640 fl->lowat = roundup2(sc->sge.fl_starve_threshold2, 8);
2641 fl->buf_boundary = sc->sge.pack_boundary;
2643 fl->lowat = roundup2(sc->sge.fl_starve_threshold, 8);
2644 fl->buf_boundary = 16;
2646 if (fl_pad && fl->buf_boundary < sc->sge.pad_boundary)
2647 fl->buf_boundary = sc->sge.pad_boundary;
2649 c.iqns_to_fl0congen |=
2650 htobe32(V_FW_IQ_CMD_FL0HOSTFCMODE(X_HOSTFCMODE_NONE) |
2651 F_FW_IQ_CMD_FL0FETCHRO | F_FW_IQ_CMD_FL0DATARO |
2652 (fl_pad ? F_FW_IQ_CMD_FL0PADEN : 0) |
2653 (fl->flags & FL_BUF_PACKING ? F_FW_IQ_CMD_FL0PACKEN :
2656 c.iqns_to_fl0congen |=
2657 htobe32(V_FW_IQ_CMD_FL0CNGCHMAP(cong) |
2658 F_FW_IQ_CMD_FL0CONGCIF |
2659 F_FW_IQ_CMD_FL0CONGEN);
2661 c.fl0dcaen_to_fl0cidxfthresh =
2662 htobe16(V_FW_IQ_CMD_FL0FBMIN(X_FETCHBURSTMIN_64B) |
2663 V_FW_IQ_CMD_FL0FBMAX(X_FETCHBURSTMAX_512B));
2664 c.fl0size = htobe16(fl->qsize);
2665 c.fl0addr = htobe64(fl->ba);
2668 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
2670 device_printf(sc->dev,
2671 "failed to create ingress queue: %d\n", rc);
2676 iq->gen = F_RSPD_GEN;
2677 iq->intr_next = iq->intr_params;
2678 iq->cntxt_id = be16toh(c.iqid);
2679 iq->abs_id = be16toh(c.physiqid);
2680 iq->flags |= IQ_ALLOCATED;
2682 cntxt_id = iq->cntxt_id - sc->sge.iq_start;
2683 if (cntxt_id >= sc->sge.niq) {
2684 panic ("%s: iq->cntxt_id (%d) more than the max (%d)", __func__,
2685 cntxt_id, sc->sge.niq - 1);
2687 sc->sge.iqmap[cntxt_id] = iq;
2692 iq->flags |= IQ_HAS_FL;
2693 fl->cntxt_id = be16toh(c.fl0id);
2694 fl->pidx = fl->cidx = 0;
2696 cntxt_id = fl->cntxt_id - sc->sge.eq_start;
2697 if (cntxt_id >= sc->sge.neq) {
2698 panic("%s: fl->cntxt_id (%d) more than the max (%d)",
2699 __func__, cntxt_id, sc->sge.neq - 1);
2701 sc->sge.eqmap[cntxt_id] = (void *)fl;
2704 if (isset(&sc->doorbells, DOORBELL_UDB)) {
2705 uint32_t s_qpp = sc->sge.eq_s_qpp;
2706 uint32_t mask = (1 << s_qpp) - 1;
2707 volatile uint8_t *udb;
2709 udb = sc->udbs_base + UDBS_DB_OFFSET;
2710 udb += (qid >> s_qpp) << PAGE_SHIFT;
2712 if (qid < PAGE_SIZE / UDBS_SEG_SIZE) {
2713 udb += qid << UDBS_SEG_SHIFT;
2716 fl->udb = (volatile void *)udb;
2718 fl->dbval = F_DBPRIO | V_QID(qid);
2720 fl->dbval |= F_DBTYPE;
2723 /* Enough to make sure the SGE doesn't think it's starved */
2724 refill_fl(sc, fl, fl->lowat);
2728 if (is_t5(sc) && cong >= 0) {
2729 uint32_t param, val;
2731 param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
2732 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
2733 V_FW_PARAMS_PARAM_YZ(iq->cntxt_id);
2738 for (i = 0; i < 4; i++) {
2739 if (cong & (1 << i))
2740 val |= 1 << (i << 2);
2744 rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
2746 /* report error but carry on */
2747 device_printf(sc->dev,
2748 "failed to set congestion manager context for "
2749 "ingress queue %d: %d\n", iq->cntxt_id, rc);
2753 /* Enable IQ interrupts */
2754 atomic_store_rel_int(&iq->state, IQS_IDLE);
2755 t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_SEINTARM(iq->intr_params) |
2756 V_INGRESSQID(iq->cntxt_id));
2762 free_iq_fl(struct port_info *pi, struct sge_iq *iq, struct sge_fl *fl)
2765 struct adapter *sc = iq->adapter;
2769 return (0); /* nothing to do */
2771 dev = pi ? pi->dev : sc->dev;
2773 if (iq->flags & IQ_ALLOCATED) {
2774 rc = -t4_iq_free(sc, sc->mbox, sc->pf, 0,
2775 FW_IQ_TYPE_FL_INT_CAP, iq->cntxt_id,
2776 fl ? fl->cntxt_id : 0xffff, 0xffff);
2779 "failed to free queue %p: %d\n", iq, rc);
2782 iq->flags &= ~IQ_ALLOCATED;
2785 free_ring(sc, iq->desc_tag, iq->desc_map, iq->ba, iq->desc);
2787 bzero(iq, sizeof(*iq));
2790 free_ring(sc, fl->desc_tag, fl->desc_map, fl->ba,
2794 free_fl_sdesc(sc, fl);
2796 if (mtx_initialized(&fl->fl_lock))
2797 mtx_destroy(&fl->fl_lock);
2799 bzero(fl, sizeof(*fl));
2806 add_fl_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid,
2809 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
2811 oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "fl", CTLFLAG_RD, NULL,
2813 children = SYSCTL_CHILDREN(oid);
2815 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cntxt_id",
2816 CTLTYPE_INT | CTLFLAG_RD, &fl->cntxt_id, 0, sysctl_uint16, "I",
2817 "SGE context id of the freelist");
2818 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "padding", CTLFLAG_RD, NULL,
2819 fl_pad ? 1 : 0, "padding enabled");
2820 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "packing", CTLFLAG_RD, NULL,
2821 fl->flags & FL_BUF_PACKING ? 1 : 0, "packing enabled");
2822 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cidx", CTLFLAG_RD, &fl->cidx,
2823 0, "consumer index");
2824 if (fl->flags & FL_BUF_PACKING) {
2825 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_offset",
2826 CTLFLAG_RD, &fl->rx_offset, 0, "packing rx offset");
2828 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "pidx", CTLFLAG_RD, &fl->pidx,
2829 0, "producer index");
2830 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "mbuf_allocated",
2831 CTLFLAG_RD, &fl->mbuf_allocated, "# of mbuf allocated");
2832 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "mbuf_inlined",
2833 CTLFLAG_RD, &fl->mbuf_inlined, "# of mbuf inlined in clusters");
2834 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_allocated",
2835 CTLFLAG_RD, &fl->cl_allocated, "# of clusters allocated");
2836 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_recycled",
2837 CTLFLAG_RD, &fl->cl_recycled, "# of clusters recycled");
2838 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_fast_recycled",
2839 CTLFLAG_RD, &fl->cl_fast_recycled, "# of clusters recycled (fast)");
2843 alloc_fwq(struct adapter *sc)
2846 struct sge_iq *fwq = &sc->sge.fwq;
2847 struct sysctl_oid *oid = device_get_sysctl_tree(sc->dev);
2848 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
2850 init_iq(fwq, sc, 0, 0, FW_IQ_QSIZE);
2851 fwq->flags |= IQ_INTR; /* always */
2852 intr_idx = sc->intr_count > 1 ? 1 : 0;
2853 rc = alloc_iq_fl(sc->port[0], fwq, NULL, intr_idx, -1);
2855 device_printf(sc->dev,
2856 "failed to create firmware event queue: %d\n", rc);
2860 oid = SYSCTL_ADD_NODE(&sc->ctx, children, OID_AUTO, "fwq", CTLFLAG_RD,
2861 NULL, "firmware event queue");
2862 children = SYSCTL_CHILDREN(oid);
2864 SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "abs_id",
2865 CTLTYPE_INT | CTLFLAG_RD, &fwq->abs_id, 0, sysctl_uint16, "I",
2866 "absolute id of the queue");
2867 SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "cntxt_id",
2868 CTLTYPE_INT | CTLFLAG_RD, &fwq->cntxt_id, 0, sysctl_uint16, "I",
2869 "SGE context id of the queue");
2870 SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "cidx",
2871 CTLTYPE_INT | CTLFLAG_RD, &fwq->cidx, 0, sysctl_uint16, "I",
2878 free_fwq(struct adapter *sc)
2880 return free_iq_fl(NULL, &sc->sge.fwq, NULL);
2884 alloc_mgmtq(struct adapter *sc)
2887 struct sge_wrq *mgmtq = &sc->sge.mgmtq;
2889 struct sysctl_oid *oid = device_get_sysctl_tree(sc->dev);
2890 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
2892 oid = SYSCTL_ADD_NODE(&sc->ctx, children, OID_AUTO, "mgmtq", CTLFLAG_RD,
2893 NULL, "management queue");
2895 snprintf(name, sizeof(name), "%s mgmtq", device_get_nameunit(sc->dev));
2896 init_eq(&mgmtq->eq, EQ_CTRL, CTRL_EQ_QSIZE, sc->port[0]->tx_chan,
2897 sc->sge.fwq.cntxt_id, name);
2898 rc = alloc_wrq(sc, NULL, mgmtq, oid);
2900 device_printf(sc->dev,
2901 "failed to create management queue: %d\n", rc);
2909 free_mgmtq(struct adapter *sc)
2912 return free_wrq(sc, &sc->sge.mgmtq);
2916 tnl_cong(struct port_info *pi)
2919 if (cong_drop == -1)
2921 else if (cong_drop == 1)
2924 return (pi->rx_chan_map);
2928 alloc_rxq(struct port_info *pi, struct sge_rxq *rxq, int intr_idx, int idx,
2929 struct sysctl_oid *oid)
2932 struct sysctl_oid_list *children;
2935 rc = alloc_iq_fl(pi, &rxq->iq, &rxq->fl, intr_idx, tnl_cong(pi));
2940 * The freelist is just barely above the starvation threshold right now,
2941 * fill it up a bit more.
2944 refill_fl(pi->adapter, &rxq->fl, 128);
2945 FL_UNLOCK(&rxq->fl);
2947 #if defined(INET) || defined(INET6)
2948 rc = tcp_lro_init(&rxq->lro);
2951 rxq->lro.ifp = pi->ifp; /* also indicates LRO init'ed */
2953 if (pi->ifp->if_capenable & IFCAP_LRO)
2954 rxq->iq.flags |= IQ_LRO_ENABLED;
2958 children = SYSCTL_CHILDREN(oid);
2960 snprintf(name, sizeof(name), "%d", idx);
2961 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
2963 children = SYSCTL_CHILDREN(oid);
2965 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "abs_id",
2966 CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.abs_id, 0, sysctl_uint16, "I",
2967 "absolute id of the queue");
2968 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cntxt_id",
2969 CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.cntxt_id, 0, sysctl_uint16, "I",
2970 "SGE context id of the queue");
2971 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
2972 CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.cidx, 0, sysctl_uint16, "I",
2974 #if defined(INET) || defined(INET6)
2975 SYSCTL_ADD_INT(&pi->ctx, children, OID_AUTO, "lro_queued", CTLFLAG_RD,
2976 &rxq->lro.lro_queued, 0, NULL);
2977 SYSCTL_ADD_INT(&pi->ctx, children, OID_AUTO, "lro_flushed", CTLFLAG_RD,
2978 &rxq->lro.lro_flushed, 0, NULL);
2980 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "rxcsum", CTLFLAG_RD,
2981 &rxq->rxcsum, "# of times hardware assisted with checksum");
2982 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "vlan_extraction",
2983 CTLFLAG_RD, &rxq->vlan_extraction,
2984 "# of times hardware extracted 802.1Q tag");
2986 add_fl_sysctls(&pi->ctx, oid, &rxq->fl);
2992 free_rxq(struct port_info *pi, struct sge_rxq *rxq)
2996 #if defined(INET) || defined(INET6)
2998 tcp_lro_free(&rxq->lro);
2999 rxq->lro.ifp = NULL;
3003 rc = free_iq_fl(pi, &rxq->iq, &rxq->fl);
3005 bzero(rxq, sizeof(*rxq));
3012 alloc_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq,
3013 int intr_idx, int idx, struct sysctl_oid *oid)
3016 struct sysctl_oid_list *children;
3019 rc = alloc_iq_fl(pi, &ofld_rxq->iq, &ofld_rxq->fl, intr_idx,
3024 children = SYSCTL_CHILDREN(oid);
3026 snprintf(name, sizeof(name), "%d", idx);
3027 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
3029 children = SYSCTL_CHILDREN(oid);
3031 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "abs_id",
3032 CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.abs_id, 0, sysctl_uint16,
3033 "I", "absolute id of the queue");
3034 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cntxt_id",
3035 CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.cntxt_id, 0, sysctl_uint16,
3036 "I", "SGE context id of the queue");
3037 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
3038 CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.cidx, 0, sysctl_uint16, "I",
3041 add_fl_sysctls(&pi->ctx, oid, &ofld_rxq->fl);
3047 free_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq)
3051 rc = free_iq_fl(pi, &ofld_rxq->iq, &ofld_rxq->fl);
3053 bzero(ofld_rxq, sizeof(*ofld_rxq));
3061 alloc_nm_rxq(struct port_info *pi, struct sge_nm_rxq *nm_rxq, int intr_idx,
3062 int idx, struct sysctl_oid *oid)
3065 struct sysctl_oid_list *children;
3066 struct sysctl_ctx_list *ctx;
3069 struct adapter *sc = pi->adapter;
3070 struct netmap_adapter *na = NA(pi->nm_ifp);
3074 len = pi->qsize_rxq * IQ_ESIZE;
3075 rc = alloc_ring(sc, len, &nm_rxq->iq_desc_tag, &nm_rxq->iq_desc_map,
3076 &nm_rxq->iq_ba, (void **)&nm_rxq->iq_desc);
3080 len = na->num_rx_desc * EQ_ESIZE + spg_len;
3081 rc = alloc_ring(sc, len, &nm_rxq->fl_desc_tag, &nm_rxq->fl_desc_map,
3082 &nm_rxq->fl_ba, (void **)&nm_rxq->fl_desc);
3088 nm_rxq->iq_cidx = 0;
3089 nm_rxq->iq_sidx = pi->qsize_rxq - spg_len / IQ_ESIZE;
3090 nm_rxq->iq_gen = F_RSPD_GEN;
3091 nm_rxq->fl_pidx = nm_rxq->fl_cidx = 0;
3092 nm_rxq->fl_sidx = na->num_rx_desc;
3093 nm_rxq->intr_idx = intr_idx;
3096 children = SYSCTL_CHILDREN(oid);
3098 snprintf(name, sizeof(name), "%d", idx);
3099 oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, name, CTLFLAG_RD, NULL,
3101 children = SYSCTL_CHILDREN(oid);
3103 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "abs_id",
3104 CTLTYPE_INT | CTLFLAG_RD, &nm_rxq->iq_abs_id, 0, sysctl_uint16,
3105 "I", "absolute id of the queue");
3106 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cntxt_id",
3107 CTLTYPE_INT | CTLFLAG_RD, &nm_rxq->iq_cntxt_id, 0, sysctl_uint16,
3108 "I", "SGE context id of the queue");
3109 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cidx",
3110 CTLTYPE_INT | CTLFLAG_RD, &nm_rxq->iq_cidx, 0, sysctl_uint16, "I",
3113 children = SYSCTL_CHILDREN(oid);
3114 oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "fl", CTLFLAG_RD, NULL,
3116 children = SYSCTL_CHILDREN(oid);
3118 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cntxt_id",
3119 CTLTYPE_INT | CTLFLAG_RD, &nm_rxq->fl_cntxt_id, 0, sysctl_uint16,
3120 "I", "SGE context id of the freelist");
3121 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cidx", CTLFLAG_RD,
3122 &nm_rxq->fl_cidx, 0, "consumer index");
3123 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "pidx", CTLFLAG_RD,
3124 &nm_rxq->fl_pidx, 0, "producer index");
3131 free_nm_rxq(struct port_info *pi, struct sge_nm_rxq *nm_rxq)
3133 struct adapter *sc = pi->adapter;
3135 free_ring(sc, nm_rxq->iq_desc_tag, nm_rxq->iq_desc_map, nm_rxq->iq_ba,
3137 free_ring(sc, nm_rxq->fl_desc_tag, nm_rxq->fl_desc_map, nm_rxq->fl_ba,
3144 alloc_nm_txq(struct port_info *pi, struct sge_nm_txq *nm_txq, int iqidx, int idx,
3145 struct sysctl_oid *oid)
3149 struct adapter *sc = pi->adapter;
3150 struct netmap_adapter *na = NA(pi->nm_ifp);
3152 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
3154 len = na->num_tx_desc * EQ_ESIZE + spg_len;
3155 rc = alloc_ring(sc, len, &nm_txq->desc_tag, &nm_txq->desc_map,
3156 &nm_txq->ba, (void **)&nm_txq->desc);
3160 nm_txq->pidx = nm_txq->cidx = 0;
3161 nm_txq->sidx = na->num_tx_desc;
3163 nm_txq->iqidx = iqidx;
3164 nm_txq->cpl_ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT) |
3165 V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf));
3167 snprintf(name, sizeof(name), "%d", idx);
3168 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
3169 NULL, "netmap tx queue");
3170 children = SYSCTL_CHILDREN(oid);
3172 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
3173 &nm_txq->cntxt_id, 0, "SGE context id of the queue");
3174 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
3175 CTLTYPE_INT | CTLFLAG_RD, &nm_txq->cidx, 0, sysctl_uint16, "I",
3177 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "pidx",
3178 CTLTYPE_INT | CTLFLAG_RD, &nm_txq->pidx, 0, sysctl_uint16, "I",
3185 free_nm_txq(struct port_info *pi, struct sge_nm_txq *nm_txq)
3187 struct adapter *sc = pi->adapter;
3189 free_ring(sc, nm_txq->desc_tag, nm_txq->desc_map, nm_txq->ba,
3197 ctrl_eq_alloc(struct adapter *sc, struct sge_eq *eq)
3200 struct fw_eq_ctrl_cmd c;
3201 int qsize = eq->sidx + spg_len / EQ_ESIZE;
3203 bzero(&c, sizeof(c));
3205 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_CTRL_CMD) | F_FW_CMD_REQUEST |
3206 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_CTRL_CMD_PFN(sc->pf) |
3207 V_FW_EQ_CTRL_CMD_VFN(0));
3208 c.alloc_to_len16 = htobe32(F_FW_EQ_CTRL_CMD_ALLOC |
3209 F_FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
3210 c.cmpliqid_eqid = htonl(V_FW_EQ_CTRL_CMD_CMPLIQID(eq->iqid));
3211 c.physeqid_pkd = htobe32(0);
3212 c.fetchszm_to_iqid =
3213 htobe32(V_FW_EQ_CTRL_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) |
3214 V_FW_EQ_CTRL_CMD_PCIECHN(eq->tx_chan) |
3215 F_FW_EQ_CTRL_CMD_FETCHRO | V_FW_EQ_CTRL_CMD_IQID(eq->iqid));
3217 htobe32(V_FW_EQ_CTRL_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
3218 V_FW_EQ_CTRL_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
3219 V_FW_EQ_CTRL_CMD_EQSIZE(qsize));
3220 c.eqaddr = htobe64(eq->ba);
3222 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
3224 device_printf(sc->dev,
3225 "failed to create control queue %d: %d\n", eq->tx_chan, rc);
3228 eq->flags |= EQ_ALLOCATED;
3230 eq->cntxt_id = G_FW_EQ_CTRL_CMD_EQID(be32toh(c.cmpliqid_eqid));
3231 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
3232 if (cntxt_id >= sc->sge.neq)
3233 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
3234 cntxt_id, sc->sge.neq - 1);
3235 sc->sge.eqmap[cntxt_id] = eq;
3241 eth_eq_alloc(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
3244 struct fw_eq_eth_cmd c;
3245 int qsize = eq->sidx + spg_len / EQ_ESIZE;
3247 bzero(&c, sizeof(c));
3249 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_ETH_CMD) | F_FW_CMD_REQUEST |
3250 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_ETH_CMD_PFN(sc->pf) |
3251 V_FW_EQ_ETH_CMD_VFN(0));
3252 c.alloc_to_len16 = htobe32(F_FW_EQ_ETH_CMD_ALLOC |
3253 F_FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
3254 c.autoequiqe_to_viid = htobe32(F_FW_EQ_ETH_CMD_AUTOEQUIQE |
3255 F_FW_EQ_ETH_CMD_AUTOEQUEQE | V_FW_EQ_ETH_CMD_VIID(pi->viid));
3256 c.fetchszm_to_iqid =
3257 htobe32(V_FW_EQ_ETH_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) |
3258 V_FW_EQ_ETH_CMD_PCIECHN(eq->tx_chan) | F_FW_EQ_ETH_CMD_FETCHRO |
3259 V_FW_EQ_ETH_CMD_IQID(eq->iqid));
3260 c.dcaen_to_eqsize = htobe32(V_FW_EQ_ETH_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
3261 V_FW_EQ_ETH_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
3262 V_FW_EQ_ETH_CMD_EQSIZE(qsize));
3263 c.eqaddr = htobe64(eq->ba);
3265 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
3267 device_printf(pi->dev,
3268 "failed to create Ethernet egress queue: %d\n", rc);
3271 eq->flags |= EQ_ALLOCATED;
3273 eq->cntxt_id = G_FW_EQ_ETH_CMD_EQID(be32toh(c.eqid_pkd));
3274 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
3275 if (cntxt_id >= sc->sge.neq)
3276 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
3277 cntxt_id, sc->sge.neq - 1);
3278 sc->sge.eqmap[cntxt_id] = eq;
3285 ofld_eq_alloc(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
3288 struct fw_eq_ofld_cmd c;
3289 int qsize = eq->sidx + spg_len / EQ_ESIZE;
3291 bzero(&c, sizeof(c));
3293 c.op_to_vfn = htonl(V_FW_CMD_OP(FW_EQ_OFLD_CMD) | F_FW_CMD_REQUEST |
3294 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_OFLD_CMD_PFN(sc->pf) |
3295 V_FW_EQ_OFLD_CMD_VFN(0));
3296 c.alloc_to_len16 = htonl(F_FW_EQ_OFLD_CMD_ALLOC |
3297 F_FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
3298 c.fetchszm_to_iqid =
3299 htonl(V_FW_EQ_OFLD_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) |
3300 V_FW_EQ_OFLD_CMD_PCIECHN(eq->tx_chan) |
3301 F_FW_EQ_OFLD_CMD_FETCHRO | V_FW_EQ_OFLD_CMD_IQID(eq->iqid));
3303 htobe32(V_FW_EQ_OFLD_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
3304 V_FW_EQ_OFLD_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
3305 V_FW_EQ_OFLD_CMD_EQSIZE(qsize));
3306 c.eqaddr = htobe64(eq->ba);
3308 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
3310 device_printf(pi->dev,
3311 "failed to create egress queue for TCP offload: %d\n", rc);
3314 eq->flags |= EQ_ALLOCATED;
3316 eq->cntxt_id = G_FW_EQ_OFLD_CMD_EQID(be32toh(c.eqid_pkd));
3317 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
3318 if (cntxt_id >= sc->sge.neq)
3319 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
3320 cntxt_id, sc->sge.neq - 1);
3321 sc->sge.eqmap[cntxt_id] = eq;
3328 alloc_eq(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
3333 mtx_init(&eq->eq_lock, eq->lockname, NULL, MTX_DEF);
3335 qsize = eq->sidx + spg_len / EQ_ESIZE;
3336 len = qsize * EQ_ESIZE;
3337 rc = alloc_ring(sc, len, &eq->desc_tag, &eq->desc_map,
3338 &eq->ba, (void **)&eq->desc);
3342 eq->pidx = eq->cidx = 0;
3343 eq->equeqidx = eq->dbidx = 0;
3344 eq->doorbells = sc->doorbells;
3346 switch (eq->flags & EQ_TYPEMASK) {
3348 rc = ctrl_eq_alloc(sc, eq);
3352 rc = eth_eq_alloc(sc, pi, eq);
3357 rc = ofld_eq_alloc(sc, pi, eq);
3362 panic("%s: invalid eq type %d.", __func__,
3363 eq->flags & EQ_TYPEMASK);
3366 device_printf(sc->dev,
3367 "failed to allocate egress queue(%d): %d\n",
3368 eq->flags & EQ_TYPEMASK, rc);
3371 if (isset(&eq->doorbells, DOORBELL_UDB) ||
3372 isset(&eq->doorbells, DOORBELL_UDBWC) ||
3373 isset(&eq->doorbells, DOORBELL_WCWR)) {
3374 uint32_t s_qpp = sc->sge.eq_s_qpp;
3375 uint32_t mask = (1 << s_qpp) - 1;
3376 volatile uint8_t *udb;
3378 udb = sc->udbs_base + UDBS_DB_OFFSET;
3379 udb += (eq->cntxt_id >> s_qpp) << PAGE_SHIFT; /* pg offset */
3380 eq->udb_qid = eq->cntxt_id & mask; /* id in page */
3381 if (eq->udb_qid >= PAGE_SIZE / UDBS_SEG_SIZE)
3382 clrbit(&eq->doorbells, DOORBELL_WCWR);
3384 udb += eq->udb_qid << UDBS_SEG_SHIFT; /* seg offset */
3387 eq->udb = (volatile void *)udb;
3394 free_eq(struct adapter *sc, struct sge_eq *eq)
3398 if (eq->flags & EQ_ALLOCATED) {
3399 switch (eq->flags & EQ_TYPEMASK) {
3401 rc = -t4_ctrl_eq_free(sc, sc->mbox, sc->pf, 0,
3406 rc = -t4_eth_eq_free(sc, sc->mbox, sc->pf, 0,
3412 rc = -t4_ofld_eq_free(sc, sc->mbox, sc->pf, 0,
3418 panic("%s: invalid eq type %d.", __func__,
3419 eq->flags & EQ_TYPEMASK);
3422 device_printf(sc->dev,
3423 "failed to free egress queue (%d): %d\n",
3424 eq->flags & EQ_TYPEMASK, rc);
3427 eq->flags &= ~EQ_ALLOCATED;
3430 free_ring(sc, eq->desc_tag, eq->desc_map, eq->ba, eq->desc);
3432 if (mtx_initialized(&eq->eq_lock))
3433 mtx_destroy(&eq->eq_lock);
3435 bzero(eq, sizeof(*eq));
3440 alloc_wrq(struct adapter *sc, struct port_info *pi, struct sge_wrq *wrq,
3441 struct sysctl_oid *oid)
3444 struct sysctl_ctx_list *ctx = pi ? &pi->ctx : &sc->ctx;
3445 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
3447 rc = alloc_eq(sc, pi, &wrq->eq);
3452 TASK_INIT(&wrq->wrq_tx_task, 0, wrq_tx_drain, wrq);
3453 TAILQ_INIT(&wrq->incomplete_wrs);
3454 STAILQ_INIT(&wrq->wr_list);
3455 wrq->nwr_pending = 0;
3456 wrq->ndesc_needed = 0;
3458 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
3459 &wrq->eq.cntxt_id, 0, "SGE context id of the queue");
3460 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cidx",
3461 CTLTYPE_INT | CTLFLAG_RD, &wrq->eq.cidx, 0, sysctl_uint16, "I",
3463 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pidx",
3464 CTLTYPE_INT | CTLFLAG_RD, &wrq->eq.pidx, 0, sysctl_uint16, "I",
3466 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs_direct", CTLFLAG_RD,
3467 &wrq->tx_wrs_direct, "# of work requests (direct)");
3468 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs_copied", CTLFLAG_RD,
3469 &wrq->tx_wrs_copied, "# of work requests (copied)");
3475 free_wrq(struct adapter *sc, struct sge_wrq *wrq)
3479 rc = free_eq(sc, &wrq->eq);
3483 bzero(wrq, sizeof(*wrq));
3488 alloc_txq(struct port_info *pi, struct sge_txq *txq, int idx,
3489 struct sysctl_oid *oid)
3492 struct adapter *sc = pi->adapter;
3493 struct sge_eq *eq = &txq->eq;
3495 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
3497 rc = mp_ring_alloc(&txq->r, eq->sidx, txq, eth_tx, can_resume_eth_tx,
3500 device_printf(sc->dev, "failed to allocate mp_ring: %d\n", rc);
3504 rc = alloc_eq(sc, pi, eq);
3506 mp_ring_free(txq->r);
3511 /* Can't fail after this point. */
3513 TASK_INIT(&txq->tx_reclaim_task, 0, tx_reclaim, eq);
3515 txq->gl = sglist_alloc(TX_SGL_SEGS, M_WAITOK);
3516 txq->cpl_ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT) |
3517 V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf));
3518 txq->sdesc = malloc(eq->sidx * sizeof(struct tx_sdesc), M_CXGBE,
3521 snprintf(name, sizeof(name), "%d", idx);
3522 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
3524 children = SYSCTL_CHILDREN(oid);
3526 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
3527 &eq->cntxt_id, 0, "SGE context id of the queue");
3528 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
3529 CTLTYPE_INT | CTLFLAG_RD, &eq->cidx, 0, sysctl_uint16, "I",
3531 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "pidx",
3532 CTLTYPE_INT | CTLFLAG_RD, &eq->pidx, 0, sysctl_uint16, "I",
3535 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txcsum", CTLFLAG_RD,
3536 &txq->txcsum, "# of times hardware assisted with checksum");
3537 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "vlan_insertion",
3538 CTLFLAG_RD, &txq->vlan_insertion,
3539 "# of times hardware inserted 802.1Q tag");
3540 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "tso_wrs", CTLFLAG_RD,
3541 &txq->tso_wrs, "# of TSO work requests");
3542 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "imm_wrs", CTLFLAG_RD,
3543 &txq->imm_wrs, "# of work requests with immediate data");
3544 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "sgl_wrs", CTLFLAG_RD,
3545 &txq->sgl_wrs, "# of work requests with direct SGL");
3546 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkt_wrs", CTLFLAG_RD,
3547 &txq->txpkt_wrs, "# of txpkt work requests (one pkt/WR)");
3548 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts0_wrs",
3549 CTLFLAG_RD, &txq->txpkts0_wrs,
3550 "# of txpkts (type 0) work requests");
3551 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts1_wrs",
3552 CTLFLAG_RD, &txq->txpkts1_wrs,
3553 "# of txpkts (type 1) work requests");
3554 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts0_pkts",
3555 CTLFLAG_RD, &txq->txpkts0_pkts,
3556 "# of frames tx'd using type0 txpkts work requests");
3557 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts1_pkts",
3558 CTLFLAG_RD, &txq->txpkts1_pkts,
3559 "# of frames tx'd using type1 txpkts work requests");
3561 SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_enqueues",
3562 CTLFLAG_RD, &txq->r->enqueues,
3563 "# of enqueues to the mp_ring for this queue");
3564 SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_drops",
3565 CTLFLAG_RD, &txq->r->drops,
3566 "# of drops in the mp_ring for this queue");
3567 SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_starts",
3568 CTLFLAG_RD, &txq->r->starts,
3569 "# of normal consumer starts in the mp_ring for this queue");
3570 SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_stalls",
3571 CTLFLAG_RD, &txq->r->stalls,
3572 "# of consumer stalls in the mp_ring for this queue");
3573 SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_restarts",
3574 CTLFLAG_RD, &txq->r->restarts,
3575 "# of consumer restarts in the mp_ring for this queue");
3576 SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_abdications",
3577 CTLFLAG_RD, &txq->r->abdications,
3578 "# of consumer abdications in the mp_ring for this queue");
3584 free_txq(struct port_info *pi, struct sge_txq *txq)
3587 struct adapter *sc = pi->adapter;
3588 struct sge_eq *eq = &txq->eq;
3590 rc = free_eq(sc, eq);
3594 sglist_free(txq->gl);
3595 free(txq->sdesc, M_CXGBE);
3596 mp_ring_free(txq->r);
3598 bzero(txq, sizeof(*txq));
3603 oneseg_dma_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
3605 bus_addr_t *ba = arg;
3608 ("%s meant for single segment mappings only.", __func__));
3610 *ba = error ? 0 : segs->ds_addr;
3614 ring_fl_db(struct adapter *sc, struct sge_fl *fl)
3618 n = IDXDIFF(fl->pidx / 8, fl->dbidx, fl->sidx);
3622 v = fl->dbval | V_PIDX(n);
3624 *fl->udb = htole32(v);
3626 t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL), v);
3627 IDXINCR(fl->dbidx, n, fl->sidx);
3631 * Fills up the freelist by allocating upto 'n' buffers. Buffers that are
3632 * recycled do not count towards this allocation budget.
3634 * Returns non-zero to indicate that this freelist should be added to the list
3635 * of starving freelists.
3638 refill_fl(struct adapter *sc, struct sge_fl *fl, int n)
3641 struct fl_sdesc *sd;
3644 struct cluster_layout *cll;
3645 struct sw_zone_info *swz;
3646 struct cluster_metadata *clm;
3648 uint16_t hw_cidx = fl->hw_cidx; /* stable snapshot */
3650 FL_LOCK_ASSERT_OWNED(fl);
3653 * We always stop at the begining of the hardware descriptor that's just
3654 * before the one with the hw cidx. This is to avoid hw pidx = hw cidx,
3655 * which would mean an empty freelist to the chip.
3657 max_pidx = __predict_false(hw_cidx == 0) ? fl->sidx - 1 : hw_cidx - 1;
3658 if (fl->pidx == max_pidx * 8)
3661 d = &fl->desc[fl->pidx];
3662 sd = &fl->sdesc[fl->pidx];
3663 cll = &fl->cll_def; /* default layout */
3664 swz = &sc->sge.sw_zone_info[cll->zidx];
3668 if (sd->cl != NULL) {
3670 if (sd->nmbuf == 0) {
3672 * Fast recycle without involving any atomics on
3673 * the cluster's metadata (if the cluster has
3674 * metadata). This happens when all frames
3675 * received in the cluster were small enough to
3676 * fit within a single mbuf each.
3678 fl->cl_fast_recycled++;
3680 clm = cl_metadata(sc, fl, &sd->cll, sd->cl);
3682 MPASS(clm->refcount == 1);
3688 * Cluster is guaranteed to have metadata. Clusters
3689 * without metadata always take the fast recycle path
3690 * when they're recycled.
3692 clm = cl_metadata(sc, fl, &sd->cll, sd->cl);
3695 if (atomic_fetchadd_int(&clm->refcount, -1) == 1) {
3697 counter_u64_add(extfree_rels, 1);
3700 sd->cl = NULL; /* gave up my reference */
3702 MPASS(sd->cl == NULL);
3704 cl = uma_zalloc(swz->zone, M_NOWAIT);
3705 if (__predict_false(cl == NULL)) {
3706 if (cll == &fl->cll_alt || fl->cll_alt.zidx == -1 ||
3707 fl->cll_def.zidx == fl->cll_alt.zidx)
3710 /* fall back to the safe zone */
3712 swz = &sc->sge.sw_zone_info[cll->zidx];
3718 pa = pmap_kextract((vm_offset_t)cl);
3722 *d = htobe64(pa | cll->hwidx);
3723 clm = cl_metadata(sc, fl, cll, cl);
3735 if (__predict_false(++fl->pidx % 8 == 0)) {
3736 uint16_t pidx = fl->pidx / 8;
3738 if (__predict_false(pidx == fl->sidx)) {
3744 if (pidx == max_pidx)
3747 if (IDXDIFF(pidx, fl->dbidx, fl->sidx) >= 4)
3752 if (fl->pidx / 8 != fl->dbidx)
3755 return (FL_RUNNING_LOW(fl) && !(fl->flags & FL_STARVING));
3759 * Attempt to refill all starving freelists.
3762 refill_sfl(void *arg)
3764 struct adapter *sc = arg;
3765 struct sge_fl *fl, *fl_temp;
3767 mtx_lock(&sc->sfl_lock);
3768 TAILQ_FOREACH_SAFE(fl, &sc->sfl, link, fl_temp) {
3770 refill_fl(sc, fl, 64);
3771 if (FL_NOT_RUNNING_LOW(fl) || fl->flags & FL_DOOMED) {
3772 TAILQ_REMOVE(&sc->sfl, fl, link);
3773 fl->flags &= ~FL_STARVING;
3778 if (!TAILQ_EMPTY(&sc->sfl))
3779 callout_schedule(&sc->sfl_callout, hz / 5);
3780 mtx_unlock(&sc->sfl_lock);
3784 alloc_fl_sdesc(struct sge_fl *fl)
3787 fl->sdesc = malloc(fl->sidx * 8 * sizeof(struct fl_sdesc), M_CXGBE,
3794 free_fl_sdesc(struct adapter *sc, struct sge_fl *fl)
3796 struct fl_sdesc *sd;
3797 struct cluster_metadata *clm;
3798 struct cluster_layout *cll;
3802 for (i = 0; i < fl->sidx * 8; i++, sd++) {
3807 clm = cl_metadata(sc, fl, cll, sd->cl);
3809 uma_zfree(sc->sge.sw_zone_info[cll->zidx].zone, sd->cl);
3810 else if (clm && atomic_fetchadd_int(&clm->refcount, -1) == 1) {
3811 uma_zfree(sc->sge.sw_zone_info[cll->zidx].zone, sd->cl);
3812 counter_u64_add(extfree_rels, 1);
3817 free(fl->sdesc, M_CXGBE);
3822 get_pkt_gl(struct mbuf *m, struct sglist *gl)
3829 rc = sglist_append_mbuf(gl, m);
3830 if (__predict_false(rc != 0)) {
3831 panic("%s: mbuf %p (%d segs) was vetted earlier but now fails "
3832 "with %d.", __func__, m, mbuf_nsegs(m), rc);
3835 KASSERT(gl->sg_nseg == mbuf_nsegs(m),
3836 ("%s: nsegs changed for mbuf %p from %d to %d", __func__, m,
3837 mbuf_nsegs(m), gl->sg_nseg));
3838 KASSERT(gl->sg_nseg > 0 &&
3839 gl->sg_nseg <= (needs_tso(m) ? TX_SGL_SEGS_TSO : TX_SGL_SEGS),
3840 ("%s: %d segments, should have been 1 <= nsegs <= %d", __func__,
3841 gl->sg_nseg, needs_tso(m) ? TX_SGL_SEGS_TSO : TX_SGL_SEGS));
3845 * len16 for a txpkt WR with a GL. Includes the firmware work request header.
3848 txpkt_len16(u_int nsegs, u_int tso)
3854 nsegs--; /* first segment is part of ulptx_sgl */
3855 n = sizeof(struct fw_eth_tx_pkt_wr) + sizeof(struct cpl_tx_pkt_core) +
3856 sizeof(struct ulptx_sgl) + 8 * ((3 * nsegs) / 2 + (nsegs & 1));
3858 n += sizeof(struct cpl_tx_pkt_lso_core);
3860 return (howmany(n, 16));
3864 * len16 for a txpkts type 0 WR with a GL. Does not include the firmware work
3868 txpkts0_len16(u_int nsegs)
3874 nsegs--; /* first segment is part of ulptx_sgl */
3875 n = sizeof(struct ulp_txpkt) + sizeof(struct ulptx_idata) +
3876 sizeof(struct cpl_tx_pkt_core) + sizeof(struct ulptx_sgl) +
3877 8 * ((3 * nsegs) / 2 + (nsegs & 1));
3879 return (howmany(n, 16));
3883 * len16 for a txpkts type 1 WR with a GL. Does not include the firmware work
3891 n = sizeof(struct cpl_tx_pkt_core) + sizeof(struct ulptx_sgl);
3893 return (howmany(n, 16));
3897 imm_payload(u_int ndesc)
3901 n = ndesc * EQ_ESIZE - sizeof(struct fw_eth_tx_pkt_wr) -
3902 sizeof(struct cpl_tx_pkt_core);
3908 * Write a txpkt WR for this packet to the hardware descriptors, update the
3909 * software descriptor, and advance the pidx. It is guaranteed that enough
3910 * descriptors are available.
3912 * The return value is the # of hardware descriptors used.
3915 write_txpkt_wr(struct sge_txq *txq, struct fw_eth_tx_pkt_wr *wr,
3916 struct mbuf *m0, u_int available)
3918 struct sge_eq *eq = &txq->eq;
3919 struct tx_sdesc *txsd;
3920 struct cpl_tx_pkt_core *cpl;
3921 uint32_t ctrl; /* used in many unrelated places */
3923 int len16, ndesc, pktlen, nsegs;
3926 TXQ_LOCK_ASSERT_OWNED(txq);
3928 MPASS(available > 0 && available < eq->sidx);
3930 len16 = mbuf_len16(m0);
3931 nsegs = mbuf_nsegs(m0);
3932 pktlen = m0->m_pkthdr.len;
3933 ctrl = sizeof(struct cpl_tx_pkt_core);
3935 ctrl += sizeof(struct cpl_tx_pkt_lso_core);
3936 else if (pktlen <= imm_payload(2) && available >= 2) {
3937 /* Immediate data. Recalculate len16 and set nsegs to 0. */
3939 len16 = howmany(sizeof(struct fw_eth_tx_pkt_wr) +
3940 sizeof(struct cpl_tx_pkt_core) + pktlen, 16);
3943 ndesc = howmany(len16, EQ_ESIZE / 16);
3944 MPASS(ndesc <= available);
3946 /* Firmware work request header */
3947 MPASS(wr == (void *)&eq->desc[eq->pidx]);
3948 wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_PKT_WR) |
3949 V_FW_ETH_TX_PKT_WR_IMMDLEN(ctrl));
3951 ctrl = V_FW_WR_LEN16(len16);
3952 wr->equiq_to_len16 = htobe32(ctrl);
3955 if (needs_tso(m0)) {
3956 struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
3958 KASSERT(m0->m_pkthdr.l2hlen > 0 && m0->m_pkthdr.l3hlen > 0 &&
3959 m0->m_pkthdr.l4hlen > 0,
3960 ("%s: mbuf %p needs TSO but missing header lengths",
3963 ctrl = V_LSO_OPCODE(CPL_TX_PKT_LSO) | F_LSO_FIRST_SLICE |
3964 F_LSO_LAST_SLICE | V_LSO_IPHDR_LEN(m0->m_pkthdr.l3hlen >> 2)
3965 | V_LSO_TCPHDR_LEN(m0->m_pkthdr.l4hlen >> 2);
3966 if (m0->m_pkthdr.l2hlen == sizeof(struct ether_vlan_header))
3967 ctrl |= V_LSO_ETHHDR_LEN(1);
3968 if (m0->m_pkthdr.l3hlen == sizeof(struct ip6_hdr))
3971 lso->lso_ctrl = htobe32(ctrl);
3972 lso->ipid_ofst = htobe16(0);
3973 lso->mss = htobe16(m0->m_pkthdr.tso_segsz);
3974 lso->seqno_offset = htobe32(0);
3975 lso->len = htobe32(pktlen);
3977 cpl = (void *)(lso + 1);
3981 cpl = (void *)(wr + 1);
3983 /* Checksum offload */
3985 if (needs_l3_csum(m0) == 0)
3986 ctrl1 |= F_TXPKT_IPCSUM_DIS;
3987 if (needs_l4_csum(m0) == 0)
3988 ctrl1 |= F_TXPKT_L4CSUM_DIS;
3989 if (m0->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP |
3990 CSUM_UDP_IPV6 | CSUM_TCP_IPV6 | CSUM_TSO))
3991 txq->txcsum++; /* some hardware assistance provided */
3993 /* VLAN tag insertion */
3994 if (needs_vlan_insertion(m0)) {
3995 ctrl1 |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(m0->m_pkthdr.ether_vtag);
3996 txq->vlan_insertion++;
4000 cpl->ctrl0 = txq->cpl_ctrl0;
4002 cpl->len = htobe16(pktlen);
4003 cpl->ctrl1 = htobe64(ctrl1);
4006 dst = (void *)(cpl + 1);
4009 write_gl_to_txd(txq, m0, &dst, eq->sidx - ndesc < eq->pidx);
4014 for (m = m0; m != NULL; m = m->m_next) {
4015 copy_to_txd(eq, mtod(m, caddr_t), &dst, m->m_len);
4021 KASSERT(pktlen == 0, ("%s: %d bytes left.", __func__, pktlen));
4028 txsd = &txq->sdesc[eq->pidx];
4030 txsd->desc_used = ndesc;
4036 try_txpkts(struct mbuf *m, struct mbuf *n, struct txpkts *txp, u_int available)
4038 u_int needed, nsegs1, nsegs2, l1, l2;
4040 if (cannot_use_txpkts(m) || cannot_use_txpkts(n))
4043 nsegs1 = mbuf_nsegs(m);
4044 nsegs2 = mbuf_nsegs(n);
4045 if (nsegs1 + nsegs2 == 2) {
4047 l1 = l2 = txpkts1_len16();
4050 l1 = txpkts0_len16(nsegs1);
4051 l2 = txpkts0_len16(nsegs2);
4053 txp->len16 = howmany(sizeof(struct fw_eth_tx_pkts_wr), 16) + l1 + l2;
4054 needed = howmany(txp->len16, EQ_ESIZE / 16);
4055 if (needed > SGE_MAX_WR_NDESC || needed > available)
4058 txp->plen = m->m_pkthdr.len + n->m_pkthdr.len;
4059 if (txp->plen > 65535)
4063 set_mbuf_len16(m, l1);
4064 set_mbuf_len16(n, l2);
4070 add_to_txpkts(struct mbuf *m, struct txpkts *txp, u_int available)
4072 u_int plen, len16, needed, nsegs;
4074 MPASS(txp->wr_type == 0 || txp->wr_type == 1);
4076 nsegs = mbuf_nsegs(m);
4077 if (needs_tso(m) || (txp->wr_type == 1 && nsegs != 1))
4080 plen = txp->plen + m->m_pkthdr.len;
4084 if (txp->wr_type == 0)
4085 len16 = txpkts0_len16(nsegs);
4087 len16 = txpkts1_len16();
4088 needed = howmany(txp->len16 + len16, EQ_ESIZE / 16);
4089 if (needed > SGE_MAX_WR_NDESC || needed > available)
4094 txp->len16 += len16;
4095 set_mbuf_len16(m, len16);
4101 * Write a txpkts WR for the packets in txp to the hardware descriptors, update
4102 * the software descriptor, and advance the pidx. It is guaranteed that enough
4103 * descriptors are available.
4105 * The return value is the # of hardware descriptors used.
4108 write_txpkts_wr(struct sge_txq *txq, struct fw_eth_tx_pkts_wr *wr,
4109 struct mbuf *m0, const struct txpkts *txp, u_int available)
4111 struct sge_eq *eq = &txq->eq;
4112 struct tx_sdesc *txsd;
4113 struct cpl_tx_pkt_core *cpl;
4116 int ndesc, checkwrap;
4120 TXQ_LOCK_ASSERT_OWNED(txq);
4121 MPASS(txp->npkt > 0);
4122 MPASS(txp->plen < 65536);
4124 MPASS(m0->m_nextpkt != NULL);
4125 MPASS(txp->len16 <= howmany(SGE_MAX_WR_LEN, 16));
4126 MPASS(available > 0 && available < eq->sidx);
4128 ndesc = howmany(txp->len16, EQ_ESIZE / 16);
4129 MPASS(ndesc <= available);
4131 MPASS(wr == (void *)&eq->desc[eq->pidx]);
4132 wr->op_pkd = htobe32(V_FW_WR_OP(FW_ETH_TX_PKTS_WR));
4133 ctrl = V_FW_WR_LEN16(txp->len16);
4134 wr->equiq_to_len16 = htobe32(ctrl);
4135 wr->plen = htobe16(txp->plen);
4136 wr->npkt = txp->npkt;
4138 wr->type = txp->wr_type;
4142 * At this point we are 16B into a hardware descriptor. If checkwrap is
4143 * set then we know the WR is going to wrap around somewhere. We'll
4144 * check for that at appropriate points.
4146 checkwrap = eq->sidx - ndesc < eq->pidx;
4147 for (m = m0; m != NULL; m = m->m_nextpkt) {
4148 if (txp->wr_type == 0) {
4149 struct ulp_txpkt *ulpmc;
4150 struct ulptx_idata *ulpsc;
4152 /* ULP master command */
4154 ulpmc->cmd_dest = htobe32(V_ULPTX_CMD(ULP_TX_PKT) |
4155 V_ULP_TXPKT_DEST(0) | V_ULP_TXPKT_FID(eq->iqid));
4156 ulpmc->len = htobe32(mbuf_len16(m));
4158 /* ULP subcommand */
4159 ulpsc = (void *)(ulpmc + 1);
4160 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM) |
4162 ulpsc->len = htobe32(sizeof(struct cpl_tx_pkt_core));
4164 cpl = (void *)(ulpsc + 1);
4166 (uintptr_t)cpl == (uintptr_t)&eq->desc[eq->sidx])
4167 cpl = (void *)&eq->desc[0];
4168 txq->txpkts0_pkts += txp->npkt;
4172 txq->txpkts1_pkts += txp->npkt;
4176 /* Checksum offload */
4178 if (needs_l3_csum(m) == 0)
4179 ctrl1 |= F_TXPKT_IPCSUM_DIS;
4180 if (needs_l4_csum(m) == 0)
4181 ctrl1 |= F_TXPKT_L4CSUM_DIS;
4182 if (m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP |
4183 CSUM_UDP_IPV6 | CSUM_TCP_IPV6 | CSUM_TSO))
4184 txq->txcsum++; /* some hardware assistance provided */
4186 /* VLAN tag insertion */
4187 if (needs_vlan_insertion(m)) {
4188 ctrl1 |= F_TXPKT_VLAN_VLD |
4189 V_TXPKT_VLAN(m->m_pkthdr.ether_vtag);
4190 txq->vlan_insertion++;
4194 cpl->ctrl0 = txq->cpl_ctrl0;
4196 cpl->len = htobe16(m->m_pkthdr.len);
4197 cpl->ctrl1 = htobe64(ctrl1);
4201 (uintptr_t)flitp == (uintptr_t)&eq->desc[eq->sidx])
4202 flitp = (void *)&eq->desc[0];
4204 write_gl_to_txd(txq, m, (caddr_t *)(&flitp), checkwrap);
4208 txsd = &txq->sdesc[eq->pidx];
4210 txsd->desc_used = ndesc;
4216 * If the SGL ends on an address that is not 16 byte aligned, this function will
4217 * add a 0 filled flit at the end.
4220 write_gl_to_txd(struct sge_txq *txq, struct mbuf *m, caddr_t *to, int checkwrap)
4222 struct sge_eq *eq = &txq->eq;
4223 struct sglist *gl = txq->gl;
4224 struct sglist_seg *seg;
4225 __be64 *flitp, *wrap;
4226 struct ulptx_sgl *usgl;
4227 int i, nflits, nsegs;
4229 KASSERT(((uintptr_t)(*to) & 0xf) == 0,
4230 ("%s: SGL must start at a 16 byte boundary: %p", __func__, *to));
4231 MPASS((uintptr_t)(*to) >= (uintptr_t)&eq->desc[0]);
4232 MPASS((uintptr_t)(*to) < (uintptr_t)&eq->desc[eq->sidx]);
4235 nsegs = gl->sg_nseg;
4238 nflits = (3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1) + 2;
4239 flitp = (__be64 *)(*to);
4240 wrap = (__be64 *)(&eq->desc[eq->sidx]);
4241 seg = &gl->sg_segs[0];
4242 usgl = (void *)flitp;
4245 * We start at a 16 byte boundary somewhere inside the tx descriptor
4246 * ring, so we're at least 16 bytes away from the status page. There is
4247 * no chance of a wrap around in the middle of usgl (which is 16 bytes).
4250 usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
4251 V_ULPTX_NSGE(nsegs));
4252 usgl->len0 = htobe32(seg->ss_len);
4253 usgl->addr0 = htobe64(seg->ss_paddr);
4256 if (checkwrap == 0 || (uintptr_t)(flitp + nflits) <= (uintptr_t)wrap) {
4258 /* Won't wrap around at all */
4260 for (i = 0; i < nsegs - 1; i++, seg++) {
4261 usgl->sge[i / 2].len[i & 1] = htobe32(seg->ss_len);
4262 usgl->sge[i / 2].addr[i & 1] = htobe64(seg->ss_paddr);
4265 usgl->sge[i / 2].len[1] = htobe32(0);
4269 /* Will wrap somewhere in the rest of the SGL */
4271 /* 2 flits already written, write the rest flit by flit */
4272 flitp = (void *)(usgl + 1);
4273 for (i = 0; i < nflits - 2; i++) {
4275 flitp = (void *)eq->desc;
4276 *flitp++ = get_flit(seg, nsegs - 1, i);
4281 MPASS(((uintptr_t)flitp) & 0xf);
4285 MPASS((((uintptr_t)flitp) & 0xf) == 0);
4286 if (__predict_false(flitp == wrap))
4287 *to = (void *)eq->desc;
4289 *to = (void *)flitp;
4293 copy_to_txd(struct sge_eq *eq, caddr_t from, caddr_t *to, int len)
4296 MPASS((uintptr_t)(*to) >= (uintptr_t)&eq->desc[0]);
4297 MPASS((uintptr_t)(*to) < (uintptr_t)&eq->desc[eq->sidx]);
4299 if (__predict_true((uintptr_t)(*to) + len <=
4300 (uintptr_t)&eq->desc[eq->sidx])) {
4301 bcopy(from, *to, len);
4304 int portion = (uintptr_t)&eq->desc[eq->sidx] - (uintptr_t)(*to);
4306 bcopy(from, *to, portion);
4308 portion = len - portion; /* remaining */
4309 bcopy(from, (void *)eq->desc, portion);
4310 (*to) = (caddr_t)eq->desc + portion;
4315 ring_eq_db(struct adapter *sc, struct sge_eq *eq, u_int n)
4323 clrbit(&db, DOORBELL_WCWR);
4326 switch (ffs(db) - 1) {
4328 *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(n));
4331 case DOORBELL_WCWR: {
4332 volatile uint64_t *dst, *src;
4336 * Queues whose 128B doorbell segment fits in the page do not
4337 * use relative qid (udb_qid is always 0). Only queues with
4338 * doorbell segments can do WCWR.
4340 KASSERT(eq->udb_qid == 0 && n == 1,
4341 ("%s: inappropriate doorbell (0x%x, %d, %d) for eq %p",
4342 __func__, eq->doorbells, n, eq->dbidx, eq));
4344 dst = (volatile void *)((uintptr_t)eq->udb + UDBS_WR_OFFSET -
4347 src = (void *)&eq->desc[i];
4348 while (src != (void *)&eq->desc[i + 1])
4354 case DOORBELL_UDBWC:
4355 *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(n));
4360 t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL),
4361 V_QID(eq->cntxt_id) | V_PIDX(n));
4365 IDXINCR(eq->dbidx, n, eq->sidx);
4369 reclaimable_tx_desc(struct sge_eq *eq)
4373 hw_cidx = read_hw_cidx(eq);
4374 return (IDXDIFF(hw_cidx, eq->cidx, eq->sidx));
4378 total_available_tx_desc(struct sge_eq *eq)
4380 uint16_t hw_cidx, pidx;
4382 hw_cidx = read_hw_cidx(eq);
4385 if (pidx == hw_cidx)
4386 return (eq->sidx - 1);
4388 return (IDXDIFF(hw_cidx, pidx, eq->sidx) - 1);
4391 static inline uint16_t
4392 read_hw_cidx(struct sge_eq *eq)
4394 struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
4395 uint16_t cidx = spg->cidx; /* stable snapshot */
4397 return (be16toh(cidx));
4401 * Reclaim 'n' descriptors approximately.
4404 reclaim_tx_descs(struct sge_txq *txq, u_int n)
4406 struct tx_sdesc *txsd;
4407 struct sge_eq *eq = &txq->eq;
4408 u_int can_reclaim, reclaimed;
4410 TXQ_LOCK_ASSERT_OWNED(txq);
4414 can_reclaim = reclaimable_tx_desc(eq);
4415 while (can_reclaim && reclaimed < n) {
4417 struct mbuf *m, *nextpkt;
4419 txsd = &txq->sdesc[eq->cidx];
4420 ndesc = txsd->desc_used;
4422 /* Firmware doesn't return "partial" credits. */
4423 KASSERT(can_reclaim >= ndesc,
4424 ("%s: unexpected number of credits: %d, %d",
4425 __func__, can_reclaim, ndesc));
4427 for (m = txsd->m; m != NULL; m = nextpkt) {
4428 nextpkt = m->m_nextpkt;
4429 m->m_nextpkt = NULL;
4433 can_reclaim -= ndesc;
4434 IDXINCR(eq->cidx, ndesc, eq->sidx);
4441 tx_reclaim(void *arg, int n)
4443 struct sge_txq *txq = arg;
4444 struct sge_eq *eq = &txq->eq;
4447 if (TXQ_TRYLOCK(txq) == 0)
4449 n = reclaim_tx_descs(txq, 32);
4450 if (eq->cidx == eq->pidx)
4451 eq->equeqidx = eq->pidx;
4457 get_flit(struct sglist_seg *segs, int nsegs, int idx)
4459 int i = (idx / 3) * 2;
4465 rc = htobe32(segs[i].ss_len);
4467 rc |= (uint64_t)htobe32(segs[i + 1].ss_len) << 32;
4472 return (htobe64(segs[i].ss_paddr));
4474 return (htobe64(segs[i + 1].ss_paddr));
4481 find_best_refill_source(struct adapter *sc, struct sge_fl *fl, int maxp)
4483 int8_t zidx, hwidx, idx;
4484 uint16_t region1, region3;
4485 int spare, spare_needed, n;
4486 struct sw_zone_info *swz;
4487 struct hw_buf_info *hwb, *hwb_list = &sc->sge.hw_buf_info[0];
4490 * Buffer Packing: Look for PAGE_SIZE or larger zone which has a bufsize
4491 * large enough for the max payload and cluster metadata. Otherwise
4492 * settle for the largest bufsize that leaves enough room in the cluster
4495 * Without buffer packing: Look for the smallest zone which has a
4496 * bufsize large enough for the max payload. Settle for the largest
4497 * bufsize available if there's nothing big enough for max payload.
4499 spare_needed = fl->flags & FL_BUF_PACKING ? CL_METADATA_SIZE : 0;
4500 swz = &sc->sge.sw_zone_info[0];
4502 for (zidx = 0; zidx < SW_ZONE_SIZES; zidx++, swz++) {
4503 if (swz->size > largest_rx_cluster) {
4504 if (__predict_true(hwidx != -1))
4508 * This is a misconfiguration. largest_rx_cluster is
4509 * preventing us from finding a refill source. See
4510 * dev.t5nex.<n>.buffer_sizes to figure out why.
4512 device_printf(sc->dev, "largest_rx_cluster=%u leaves no"
4513 " refill source for fl %p (dma %u). Ignored.\n",
4514 largest_rx_cluster, fl, maxp);
4516 for (idx = swz->head_hwidx; idx != -1; idx = hwb->next) {
4517 hwb = &hwb_list[idx];
4518 spare = swz->size - hwb->size;
4519 if (spare < spare_needed)
4522 hwidx = idx; /* best option so far */
4523 if (hwb->size >= maxp) {
4525 if ((fl->flags & FL_BUF_PACKING) == 0)
4526 goto done; /* stop looking (not packing) */
4528 if (swz->size >= safest_rx_cluster)
4529 goto done; /* stop looking (packing) */
4531 break; /* keep looking, next zone */
4535 /* A usable hwidx has been located. */
4537 hwb = &hwb_list[hwidx];
4539 swz = &sc->sge.sw_zone_info[zidx];
4541 region3 = swz->size - hwb->size;
4544 * Stay within this zone and see if there is a better match when mbuf
4545 * inlining is allowed. Remember that the hwidx's are sorted in
4546 * decreasing order of size (so in increasing order of spare area).
4548 for (idx = hwidx; idx != -1; idx = hwb->next) {
4549 hwb = &hwb_list[idx];
4550 spare = swz->size - hwb->size;
4552 if (allow_mbufs_in_cluster == 0 || hwb->size < maxp)
4556 * Do not inline mbufs if doing so would violate the pad/pack
4557 * boundary alignment requirement.
4559 if (fl_pad && (MSIZE % sc->sge.pad_boundary) != 0)
4561 if (fl->flags & FL_BUF_PACKING &&
4562 (MSIZE % sc->sge.pack_boundary) != 0)
4565 if (spare < CL_METADATA_SIZE + MSIZE)
4567 n = (spare - CL_METADATA_SIZE) / MSIZE;
4568 if (n > howmany(hwb->size, maxp))
4572 if (fl->flags & FL_BUF_PACKING) {
4573 region1 = n * MSIZE;
4574 region3 = spare - region1;
4577 region3 = spare - region1;
4582 KASSERT(zidx >= 0 && zidx < SW_ZONE_SIZES,
4583 ("%s: bad zone %d for fl %p, maxp %d", __func__, zidx, fl, maxp));
4584 KASSERT(hwidx >= 0 && hwidx <= SGE_FLBUF_SIZES,
4585 ("%s: bad hwidx %d for fl %p, maxp %d", __func__, hwidx, fl, maxp));
4586 KASSERT(region1 + sc->sge.hw_buf_info[hwidx].size + region3 ==
4587 sc->sge.sw_zone_info[zidx].size,
4588 ("%s: bad buffer layout for fl %p, maxp %d. "
4589 "cl %d; r1 %d, payload %d, r3 %d", __func__, fl, maxp,
4590 sc->sge.sw_zone_info[zidx].size, region1,
4591 sc->sge.hw_buf_info[hwidx].size, region3));
4592 if (fl->flags & FL_BUF_PACKING || region1 > 0) {
4593 KASSERT(region3 >= CL_METADATA_SIZE,
4594 ("%s: no room for metadata. fl %p, maxp %d; "
4595 "cl %d; r1 %d, payload %d, r3 %d", __func__, fl, maxp,
4596 sc->sge.sw_zone_info[zidx].size, region1,
4597 sc->sge.hw_buf_info[hwidx].size, region3));
4598 KASSERT(region1 % MSIZE == 0,
4599 ("%s: bad mbuf region for fl %p, maxp %d. "
4600 "cl %d; r1 %d, payload %d, r3 %d", __func__, fl, maxp,
4601 sc->sge.sw_zone_info[zidx].size, region1,
4602 sc->sge.hw_buf_info[hwidx].size, region3));
4605 fl->cll_def.zidx = zidx;
4606 fl->cll_def.hwidx = hwidx;
4607 fl->cll_def.region1 = region1;
4608 fl->cll_def.region3 = region3;
4612 find_safe_refill_source(struct adapter *sc, struct sge_fl *fl)
4614 struct sge *s = &sc->sge;
4615 struct hw_buf_info *hwb;
4616 struct sw_zone_info *swz;
4620 if (fl->flags & FL_BUF_PACKING)
4621 hwidx = s->safe_hwidx2; /* with room for metadata */
4622 else if (allow_mbufs_in_cluster && s->safe_hwidx2 != -1) {
4623 hwidx = s->safe_hwidx2;
4624 hwb = &s->hw_buf_info[hwidx];
4625 swz = &s->sw_zone_info[hwb->zidx];
4626 spare = swz->size - hwb->size;
4628 /* no good if there isn't room for an mbuf as well */
4629 if (spare < CL_METADATA_SIZE + MSIZE)
4630 hwidx = s->safe_hwidx1;
4632 hwidx = s->safe_hwidx1;
4635 /* No fallback source */
4636 fl->cll_alt.hwidx = -1;
4637 fl->cll_alt.zidx = -1;
4642 hwb = &s->hw_buf_info[hwidx];
4643 swz = &s->sw_zone_info[hwb->zidx];
4644 spare = swz->size - hwb->size;
4645 fl->cll_alt.hwidx = hwidx;
4646 fl->cll_alt.zidx = hwb->zidx;
4647 if (allow_mbufs_in_cluster &&
4648 (fl_pad == 0 || (MSIZE % sc->sge.pad_boundary) == 0))
4649 fl->cll_alt.region1 = ((spare - CL_METADATA_SIZE) / MSIZE) * MSIZE;
4651 fl->cll_alt.region1 = 0;
4652 fl->cll_alt.region3 = spare - fl->cll_alt.region1;
4656 add_fl_to_sfl(struct adapter *sc, struct sge_fl *fl)
4658 mtx_lock(&sc->sfl_lock);
4660 if ((fl->flags & FL_DOOMED) == 0) {
4661 fl->flags |= FL_STARVING;
4662 TAILQ_INSERT_TAIL(&sc->sfl, fl, link);
4663 callout_reset(&sc->sfl_callout, hz / 5, refill_sfl, sc);
4666 mtx_unlock(&sc->sfl_lock);
4670 handle_wrq_egr_update(struct adapter *sc, struct sge_eq *eq)
4672 struct sge_wrq *wrq = (void *)eq;
4674 atomic_readandclear_int(&eq->equiq);
4675 taskqueue_enqueue(sc->tq[eq->tx_chan], &wrq->wrq_tx_task);
4679 handle_eth_egr_update(struct adapter *sc, struct sge_eq *eq)
4681 struct sge_txq *txq = (void *)eq;
4683 MPASS((eq->flags & EQ_TYPEMASK) == EQ_ETH);
4685 atomic_readandclear_int(&eq->equiq);
4686 mp_ring_check_drainage(txq->r, 0);
4687 taskqueue_enqueue(sc->tq[eq->tx_chan], &txq->tx_reclaim_task);
4691 handle_sge_egr_update(struct sge_iq *iq, const struct rss_header *rss,
4694 const struct cpl_sge_egr_update *cpl = (const void *)(rss + 1);
4695 unsigned int qid = G_EGR_QID(ntohl(cpl->opcode_qid));
4696 struct adapter *sc = iq->adapter;
4697 struct sge *s = &sc->sge;
4699 static void (*h[])(struct adapter *, struct sge_eq *) = {NULL,
4700 &handle_wrq_egr_update, &handle_eth_egr_update,
4701 &handle_wrq_egr_update};
4703 KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__,
4706 eq = s->eqmap[qid - s->eq_start];
4707 (*h[eq->flags & EQ_TYPEMASK])(sc, eq);
4712 /* handle_fw_msg works for both fw4_msg and fw6_msg because this is valid */
4713 CTASSERT(offsetof(struct cpl_fw4_msg, data) == \
4714 offsetof(struct cpl_fw6_msg, data));
4717 handle_fw_msg(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
4719 struct adapter *sc = iq->adapter;
4720 const struct cpl_fw6_msg *cpl = (const void *)(rss + 1);
4722 KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__,
4725 if (cpl->type == FW_TYPE_RSSCPL || cpl->type == FW6_TYPE_RSSCPL) {
4726 const struct rss_header *rss2;
4728 rss2 = (const struct rss_header *)&cpl->data[0];
4729 return (sc->cpl_handler[rss2->opcode](iq, rss2, m));
4732 return (sc->fw_msg_handler[cpl->type](sc, &cpl->data[0]));
4736 sysctl_uint16(SYSCTL_HANDLER_ARGS)
4738 uint16_t *id = arg1;
4741 return sysctl_handle_int(oidp, &i, 0, req);
4745 sysctl_bufsizes(SYSCTL_HANDLER_ARGS)
4747 struct sge *s = arg1;
4748 struct hw_buf_info *hwb = &s->hw_buf_info[0];
4749 struct sw_zone_info *swz = &s->sw_zone_info[0];
4754 sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
4755 for (i = 0; i < SGE_FLBUF_SIZES; i++, hwb++) {
4756 if (hwb->zidx >= 0 && swz[hwb->zidx].size <= largest_rx_cluster)
4761 sbuf_printf(&sb, "%u%c ", hwb->size, c);
4765 rc = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);