2 * Copyright (c) 2011 Chelsio Communications, Inc.
4 * Written by: Navdeep Parhar <np@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_inet6.h"
34 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/queue.h>
42 #include <sys/taskqueue.h>
44 #include <sys/sysctl.h>
47 #include <net/ethernet.h>
49 #include <net/if_vlan_var.h>
50 #include <netinet/in.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip6.h>
53 #include <netinet/tcp.h>
54 #include <machine/md_var.h>
58 #include "common/common.h"
59 #include "common/t4_regs.h"
60 #include "common/t4_regs_values.h"
61 #include "common/t4_msg.h"
63 #ifdef T4_PKT_TIMESTAMP
64 #define RX_COPY_THRESHOLD (MINCLSIZE - 8)
66 #define RX_COPY_THRESHOLD MINCLSIZE
70 * Ethernet frames are DMA'd at this byte offset into the freelist buffer.
71 * 0-7 are valid values.
73 static int fl_pktshift = 2;
74 TUNABLE_INT("hw.cxgbe.fl_pktshift", &fl_pktshift);
77 * Pad ethernet payload up to this boundary.
78 * -1: driver should figure out a good value.
80 * Any power of 2 from 32 to 4096 (both inclusive) is also a valid value.
82 static int fl_pad = -1;
83 TUNABLE_INT("hw.cxgbe.fl_pad", &fl_pad);
87 * -1: driver should figure out a good value.
88 * 64 or 128 are the only other valid values.
90 static int spg_len = -1;
91 TUNABLE_INT("hw.cxgbe.spg_len", &spg_len);
95 * -1: no congestion feedback (not recommended).
96 * 0: backpressure the channel instead of dropping packets right away.
97 * 1: no backpressure, drop packets for the congested queue immediately.
99 static int cong_drop = 0;
100 TUNABLE_INT("hw.cxgbe.cong_drop", &cong_drop);
103 * Deliver multiple frames in the same free list buffer if they fit.
104 * -1: let the driver decide whether to enable buffer packing or not.
105 * 0: disable buffer packing.
106 * 1: enable buffer packing.
108 static int buffer_packing = -1;
109 TUNABLE_INT("hw.cxgbe.buffer_packing", &buffer_packing);
112 * Start next frame in a packed buffer at this boundary.
113 * -1: driver should figure out a good value.
117 * value specified here will be overridden by fl_pad.
119 * power of 2 from 32 to 4096 (both inclusive) is a valid value here.
122 * 16, or a power of 2 from 64 to 4096 (both inclusive) is a valid value.
124 static int fl_pack = -1;
125 static int t4_fl_pack;
126 static int t5_fl_pack;
127 TUNABLE_INT("hw.cxgbe.fl_pack", &fl_pack);
130 * Allow the driver to create mbuf(s) in a cluster allocated for rx.
131 * 0: never; always allocate mbufs from the zone_mbuf UMA zone.
132 * 1: ok to create mbuf(s) within a cluster if there is room.
134 static int allow_mbufs_in_cluster = 1;
135 TUNABLE_INT("hw.cxgbe.allow_mbufs_in_cluster", &allow_mbufs_in_cluster);
138 * Largest rx cluster size that the driver is allowed to allocate.
140 static int largest_rx_cluster = MJUM16BYTES;
141 TUNABLE_INT("hw.cxgbe.largest_rx_cluster", &largest_rx_cluster);
144 * Size of cluster allocation that's most likely to succeed. The driver will
145 * fall back to this size if it fails to allocate clusters larger than this.
147 static int safest_rx_cluster = PAGE_SIZE;
148 TUNABLE_INT("hw.cxgbe.safest_rx_cluster", &safest_rx_cluster);
150 /* Used to track coalesced tx work request */
152 uint64_t *flitp; /* ptr to flit where next pkt should start */
153 uint8_t npkt; /* # of packets in this work request */
154 uint8_t nflits; /* # of flits used by this work request */
155 uint16_t plen; /* total payload (sum of all packets) */
158 /* A packet's SGL. This + m_pkthdr has all info needed for tx */
160 int nsegs; /* # of segments in the SGL, 0 means imm. tx */
161 int nflits; /* # of flits needed for the SGL */
162 bus_dma_segment_t seg[TX_SGL_SEGS];
165 static int service_iq(struct sge_iq *, int);
166 static struct mbuf *get_fl_payload(struct adapter *, struct sge_fl *, uint32_t,
168 static int t4_eth_rx(struct sge_iq *, const struct rss_header *, struct mbuf *);
169 static inline void init_iq(struct sge_iq *, struct adapter *, int, int, int,
171 static inline void init_fl(struct adapter *, struct sge_fl *, int, int, int,
173 static inline void init_eq(struct sge_eq *, int, int, uint8_t, uint16_t,
175 static int alloc_ring(struct adapter *, size_t, bus_dma_tag_t *, bus_dmamap_t *,
176 bus_addr_t *, void **);
177 static int free_ring(struct adapter *, bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
179 static int alloc_iq_fl(struct port_info *, struct sge_iq *, struct sge_fl *,
181 static int free_iq_fl(struct port_info *, struct sge_iq *, struct sge_fl *);
182 static void add_fl_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *,
184 static int alloc_fwq(struct adapter *);
185 static int free_fwq(struct adapter *);
186 static int alloc_mgmtq(struct adapter *);
187 static int free_mgmtq(struct adapter *);
188 static int alloc_rxq(struct port_info *, struct sge_rxq *, int, int,
189 struct sysctl_oid *);
190 static int free_rxq(struct port_info *, struct sge_rxq *);
192 static int alloc_ofld_rxq(struct port_info *, struct sge_ofld_rxq *, int, int,
193 struct sysctl_oid *);
194 static int free_ofld_rxq(struct port_info *, struct sge_ofld_rxq *);
196 static int ctrl_eq_alloc(struct adapter *, struct sge_eq *);
197 static int eth_eq_alloc(struct adapter *, struct port_info *, struct sge_eq *);
199 static int ofld_eq_alloc(struct adapter *, struct port_info *, struct sge_eq *);
201 static int alloc_eq(struct adapter *, struct port_info *, struct sge_eq *);
202 static int free_eq(struct adapter *, struct sge_eq *);
203 static int alloc_wrq(struct adapter *, struct port_info *, struct sge_wrq *,
204 struct sysctl_oid *);
205 static int free_wrq(struct adapter *, struct sge_wrq *);
206 static int alloc_txq(struct port_info *, struct sge_txq *, int,
207 struct sysctl_oid *);
208 static int free_txq(struct port_info *, struct sge_txq *);
209 static void oneseg_dma_callback(void *, bus_dma_segment_t *, int, int);
210 static inline bool is_new_response(const struct sge_iq *, struct rsp_ctrl **);
211 static inline void iq_next(struct sge_iq *);
212 static inline void ring_fl_db(struct adapter *, struct sge_fl *);
213 static int refill_fl(struct adapter *, struct sge_fl *, int);
214 static void refill_sfl(void *);
215 static int alloc_fl_sdesc(struct sge_fl *);
216 static void free_fl_sdesc(struct adapter *, struct sge_fl *);
217 static void find_best_refill_source(struct adapter *, struct sge_fl *, int);
218 static void find_safe_refill_source(struct adapter *, struct sge_fl *);
219 static void add_fl_to_sfl(struct adapter *, struct sge_fl *);
221 static int get_pkt_sgl(struct sge_txq *, struct mbuf **, struct sgl *, int);
222 static int free_pkt_sgl(struct sge_txq *, struct sgl *);
223 static int write_txpkt_wr(struct port_info *, struct sge_txq *, struct mbuf *,
225 static int add_to_txpkts(struct port_info *, struct sge_txq *, struct txpkts *,
226 struct mbuf *, struct sgl *);
227 static void write_txpkts_wr(struct sge_txq *, struct txpkts *);
228 static inline void write_ulp_cpl_sgl(struct port_info *, struct sge_txq *,
229 struct txpkts *, struct mbuf *, struct sgl *);
230 static int write_sgl_to_txd(struct sge_eq *, struct sgl *, caddr_t *);
231 static inline void copy_to_txd(struct sge_eq *, caddr_t, caddr_t *, int);
232 static inline void ring_eq_db(struct adapter *, struct sge_eq *);
233 static inline int reclaimable(struct sge_eq *);
234 static int reclaim_tx_descs(struct sge_txq *, int, int);
235 static void write_eqflush_wr(struct sge_eq *);
236 static __be64 get_flit(bus_dma_segment_t *, int, int);
237 static int handle_sge_egr_update(struct sge_iq *, const struct rss_header *,
239 static int handle_fw_msg(struct sge_iq *, const struct rss_header *,
242 static int sysctl_uint16(SYSCTL_HANDLER_ARGS);
243 static int sysctl_bufsizes(SYSCTL_HANDLER_ARGS);
246 * Called on MOD_LOAD. Validates and calculates the SGE tunables.
253 /* set pad to a reasonable powerof2 between 16 and 4096 (inclusive) */
254 #if defined(__i386__) || defined(__amd64__)
255 pad = max(cpu_clflush_line_size, 16);
257 pad = max(CACHE_LINE_SIZE, 16);
259 pad = min(pad, 4096);
261 if (fl_pktshift < 0 || fl_pktshift > 7) {
262 printf("Invalid hw.cxgbe.fl_pktshift value (%d),"
263 " using 2 instead.\n", fl_pktshift);
268 (fl_pad < 32 || fl_pad > 4096 || !powerof2(fl_pad))) {
271 printf("Invalid hw.cxgbe.fl_pad value (%d),"
272 " using %d instead.\n", fl_pad, max(pad, 32));
274 fl_pad = max(pad, 32);
278 * T4 has the same pad and pack boundary. If a pad boundary is set,
279 * pack boundary must be set to the same value. Otherwise take the
280 * specified value or auto-calculate something reasonable.
284 else if (fl_pack < 32 || fl_pack > 4096 || !powerof2(fl_pack))
285 t4_fl_pack = max(pad, 32);
287 t4_fl_pack = fl_pack;
289 /* T5's pack boundary is independent of the pad boundary. */
290 if (fl_pack < 16 || fl_pack == 32 || fl_pack > 4096 ||
292 t5_fl_pack = max(pad, CACHE_LINE_SIZE);
294 t5_fl_pack = fl_pack;
296 if (spg_len != 64 && spg_len != 128) {
299 #if defined(__i386__) || defined(__amd64__)
300 len = cpu_clflush_line_size > 64 ? 128 : 64;
305 printf("Invalid hw.cxgbe.spg_len value (%d),"
306 " using %d instead.\n", spg_len, len);
311 if (cong_drop < -1 || cong_drop > 1) {
312 printf("Invalid hw.cxgbe.cong_drop value (%d),"
313 " using 0 instead.\n", cong_drop);
319 t4_init_sge_cpl_handlers(struct adapter *sc)
322 t4_register_cpl_handler(sc, CPL_FW4_MSG, handle_fw_msg);
323 t4_register_cpl_handler(sc, CPL_FW6_MSG, handle_fw_msg);
324 t4_register_cpl_handler(sc, CPL_SGE_EGR_UPDATE, handle_sge_egr_update);
325 t4_register_cpl_handler(sc, CPL_RX_PKT, t4_eth_rx);
326 t4_register_fw_msg_handler(sc, FW6_TYPE_CMD_RPL, t4_handle_fw_rpl);
330 * adap->params.vpd.cclk must be set up before this is called.
333 t4_tweak_chip_settings(struct adapter *sc)
337 int intr_timer[SGE_NTIMERS] = {1, 5, 10, 50, 100, 200};
338 int timer_max = M_TIMERVALUE0 * 1000 / sc->params.vpd.cclk;
339 int intr_pktcount[SGE_NCOUNTERS] = {1, 8, 16, 32}; /* 63 max */
340 uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE);
341 static int sge_flbuf_sizes[] = {
343 #if MJUMPAGESIZE != MCLBYTES
345 MJUMPAGESIZE - CL_METADATA_SIZE,
346 MJUMPAGESIZE - 2 * MSIZE - CL_METADATA_SIZE,
350 MCLBYTES - MSIZE - CL_METADATA_SIZE,
351 MJUM9BYTES - CL_METADATA_SIZE,
352 MJUM16BYTES - CL_METADATA_SIZE,
355 KASSERT(sc->flags & MASTER_PF,
356 ("%s: trying to change chip settings when not master.", __func__));
358 m = V_PKTSHIFT(M_PKTSHIFT) | F_RXPKTCPLMODE | F_EGRSTATUSPAGESIZE;
359 v = V_PKTSHIFT(fl_pktshift) | F_RXPKTCPLMODE |
360 V_EGRSTATUSPAGESIZE(spg_len == 128);
361 if (is_t4(sc) && (fl_pad || buffer_packing)) {
362 /* t4_fl_pack has the correct value even when fl_pad = 0 */
363 m |= V_INGPADBOUNDARY(M_INGPADBOUNDARY);
364 v |= V_INGPADBOUNDARY(ilog2(t4_fl_pack) - 5);
365 } else if (is_t5(sc) && fl_pad) {
366 m |= V_INGPADBOUNDARY(M_INGPADBOUNDARY);
367 v |= V_INGPADBOUNDARY(ilog2(fl_pad) - 5);
369 t4_set_reg_field(sc, A_SGE_CONTROL, m, v);
371 if (is_t5(sc) && buffer_packing) {
372 m = V_INGPACKBOUNDARY(M_INGPACKBOUNDARY);
373 if (t5_fl_pack == 16)
374 v = V_INGPACKBOUNDARY(0);
376 v = V_INGPACKBOUNDARY(ilog2(t5_fl_pack) - 5);
377 t4_set_reg_field(sc, A_SGE_CONTROL2, m, v);
380 v = V_HOSTPAGESIZEPF0(PAGE_SHIFT - 10) |
381 V_HOSTPAGESIZEPF1(PAGE_SHIFT - 10) |
382 V_HOSTPAGESIZEPF2(PAGE_SHIFT - 10) |
383 V_HOSTPAGESIZEPF3(PAGE_SHIFT - 10) |
384 V_HOSTPAGESIZEPF4(PAGE_SHIFT - 10) |
385 V_HOSTPAGESIZEPF5(PAGE_SHIFT - 10) |
386 V_HOSTPAGESIZEPF6(PAGE_SHIFT - 10) |
387 V_HOSTPAGESIZEPF7(PAGE_SHIFT - 10);
388 t4_write_reg(sc, A_SGE_HOST_PAGE_SIZE, v);
390 KASSERT(nitems(sge_flbuf_sizes) <= SGE_FLBUF_SIZES,
391 ("%s: hw buffer size table too big", __func__));
392 for (i = 0; i < min(nitems(sge_flbuf_sizes), SGE_FLBUF_SIZES); i++) {
393 t4_write_reg(sc, A_SGE_FL_BUFFER_SIZE0 + (4 * i),
397 v = V_THRESHOLD_0(intr_pktcount[0]) | V_THRESHOLD_1(intr_pktcount[1]) |
398 V_THRESHOLD_2(intr_pktcount[2]) | V_THRESHOLD_3(intr_pktcount[3]);
399 t4_write_reg(sc, A_SGE_INGRESS_RX_THRESHOLD, v);
401 KASSERT(intr_timer[0] <= timer_max,
402 ("%s: not a single usable timer (%d, %d)", __func__, intr_timer[0],
404 for (i = 1; i < nitems(intr_timer); i++) {
405 KASSERT(intr_timer[i] >= intr_timer[i - 1],
406 ("%s: timers not listed in increasing order (%d)",
409 while (intr_timer[i] > timer_max) {
410 if (i == nitems(intr_timer) - 1) {
411 intr_timer[i] = timer_max;
414 intr_timer[i] += intr_timer[i - 1];
419 v = V_TIMERVALUE0(us_to_core_ticks(sc, intr_timer[0])) |
420 V_TIMERVALUE1(us_to_core_ticks(sc, intr_timer[1]));
421 t4_write_reg(sc, A_SGE_TIMER_VALUE_0_AND_1, v);
422 v = V_TIMERVALUE2(us_to_core_ticks(sc, intr_timer[2])) |
423 V_TIMERVALUE3(us_to_core_ticks(sc, intr_timer[3]));
424 t4_write_reg(sc, A_SGE_TIMER_VALUE_2_AND_3, v);
425 v = V_TIMERVALUE4(us_to_core_ticks(sc, intr_timer[4])) |
426 V_TIMERVALUE5(us_to_core_ticks(sc, intr_timer[5]));
427 t4_write_reg(sc, A_SGE_TIMER_VALUE_4_AND_5, v);
429 if (cong_drop == 0) {
430 m = F_TUNNELCNGDROP0 | F_TUNNELCNGDROP1 | F_TUNNELCNGDROP2 |
432 t4_set_reg_field(sc, A_TP_PARA_REG3, m, 0);
435 /* 4K, 16K, 64K, 256K DDP "page sizes" */
436 v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6);
437 t4_write_reg(sc, A_ULP_RX_TDDP_PSZ, v);
439 m = v = F_TDDPTAGTCB;
440 t4_set_reg_field(sc, A_ULP_RX_CTL, m, v);
442 m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET |
444 v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET;
445 t4_set_reg_field(sc, A_TP_PARA_REG5, m, v);
449 * SGE wants the buffer to be at least 64B and then a multiple of the pad
450 * boundary or 16, whichever is greater.
455 int mask = max(fl_pad, 16) - 1;
457 return (hwsz >= 64 && (hwsz & mask) == 0);
461 * XXX: driver really should be able to deal with unexpected settings.
464 t4_read_chip_settings(struct adapter *sc)
466 struct sge *s = &sc->sge;
469 uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE);
470 static int sw_buf_sizes[] = { /* Sorted by size */
472 #if MJUMPAGESIZE != MCLBYTES
478 struct sw_zone_info *swz, *safe_swz;
479 struct hw_buf_info *hwb;
481 m = V_PKTSHIFT(M_PKTSHIFT) | F_RXPKTCPLMODE | F_EGRSTATUSPAGESIZE;
482 v = V_PKTSHIFT(fl_pktshift) | F_RXPKTCPLMODE |
483 V_EGRSTATUSPAGESIZE(spg_len == 128);
484 if (is_t4(sc) && (fl_pad || buffer_packing)) {
485 m |= V_INGPADBOUNDARY(M_INGPADBOUNDARY);
486 v |= V_INGPADBOUNDARY(ilog2(t4_fl_pack) - 5);
487 } else if (is_t5(sc) && fl_pad) {
488 m |= V_INGPADBOUNDARY(M_INGPADBOUNDARY);
489 v |= V_INGPADBOUNDARY(ilog2(fl_pad) - 5);
491 r = t4_read_reg(sc, A_SGE_CONTROL);
493 device_printf(sc->dev, "invalid SGE_CONTROL(0x%x)\n", r);
497 if (is_t5(sc) && buffer_packing) {
498 m = V_INGPACKBOUNDARY(M_INGPACKBOUNDARY);
499 if (t5_fl_pack == 16)
500 v = V_INGPACKBOUNDARY(0);
502 v = V_INGPACKBOUNDARY(ilog2(t5_fl_pack) - 5);
503 r = t4_read_reg(sc, A_SGE_CONTROL2);
505 device_printf(sc->dev,
506 "invalid SGE_CONTROL2(0x%x)\n", r);
510 s->pack_boundary = is_t4(sc) ? t4_fl_pack : t5_fl_pack;
512 v = V_HOSTPAGESIZEPF0(PAGE_SHIFT - 10) |
513 V_HOSTPAGESIZEPF1(PAGE_SHIFT - 10) |
514 V_HOSTPAGESIZEPF2(PAGE_SHIFT - 10) |
515 V_HOSTPAGESIZEPF3(PAGE_SHIFT - 10) |
516 V_HOSTPAGESIZEPF4(PAGE_SHIFT - 10) |
517 V_HOSTPAGESIZEPF5(PAGE_SHIFT - 10) |
518 V_HOSTPAGESIZEPF6(PAGE_SHIFT - 10) |
519 V_HOSTPAGESIZEPF7(PAGE_SHIFT - 10);
520 r = t4_read_reg(sc, A_SGE_HOST_PAGE_SIZE);
522 device_printf(sc->dev, "invalid SGE_HOST_PAGE_SIZE(0x%x)\n", r);
526 /* Filter out unusable hw buffer sizes entirely (mark with -2). */
527 hwb = &s->hw_buf_info[0];
528 for (i = 0; i < nitems(s->hw_buf_info); i++, hwb++) {
529 r = t4_read_reg(sc, A_SGE_FL_BUFFER_SIZE0 + (4 * i));
531 hwb->zidx = hwsz_ok(r) ? -1 : -2;
536 * Create a sorted list in decreasing order of hw buffer sizes (and so
537 * increasing order of spare area) for each software zone.
539 n = 0; /* no usable buffer size to begin with */
540 swz = &s->sw_zone_info[0];
542 for (i = 0; i < SW_ZONE_SIZES; i++, swz++) {
543 int8_t head = -1, tail = -1;
545 swz->size = sw_buf_sizes[i];
546 swz->zone = m_getzone(swz->size);
547 swz->type = m_gettype(swz->size);
549 if (swz->size == safest_rx_cluster)
552 hwb = &s->hw_buf_info[0];
553 for (j = 0; j < SGE_FLBUF_SIZES; j++, hwb++) {
554 if (hwb->zidx != -1 || hwb->size > swz->size)
559 else if (hwb->size < s->hw_buf_info[tail].size) {
560 s->hw_buf_info[tail].next = j;
564 struct hw_buf_info *t;
566 for (cur = &head; *cur != -1; cur = &t->next) {
567 t = &s->hw_buf_info[*cur];
568 if (hwb->size == t->size) {
572 if (hwb->size > t->size) {
580 swz->head_hwidx = head;
581 swz->tail_hwidx = tail;
585 if (swz->size - s->hw_buf_info[tail].size >=
587 sc->flags |= BUF_PACKING_OK;
591 device_printf(sc->dev, "no usable SGE FL buffer size.\n");
597 if (safe_swz != NULL) {
598 s->safe_hwidx1 = safe_swz->head_hwidx;
599 for (i = safe_swz->head_hwidx; i != -1; i = hwb->next) {
602 hwb = &s->hw_buf_info[i];
603 spare = safe_swz->size - hwb->size;
604 if (spare < CL_METADATA_SIZE)
606 if (s->safe_hwidx2 == -1 ||
607 spare == CL_METADATA_SIZE + MSIZE)
609 if (spare >= CL_METADATA_SIZE + MSIZE)
614 r = t4_read_reg(sc, A_SGE_INGRESS_RX_THRESHOLD);
615 s->counter_val[0] = G_THRESHOLD_0(r);
616 s->counter_val[1] = G_THRESHOLD_1(r);
617 s->counter_val[2] = G_THRESHOLD_2(r);
618 s->counter_val[3] = G_THRESHOLD_3(r);
620 r = t4_read_reg(sc, A_SGE_TIMER_VALUE_0_AND_1);
621 s->timer_val[0] = G_TIMERVALUE0(r) / core_ticks_per_usec(sc);
622 s->timer_val[1] = G_TIMERVALUE1(r) / core_ticks_per_usec(sc);
623 r = t4_read_reg(sc, A_SGE_TIMER_VALUE_2_AND_3);
624 s->timer_val[2] = G_TIMERVALUE2(r) / core_ticks_per_usec(sc);
625 s->timer_val[3] = G_TIMERVALUE3(r) / core_ticks_per_usec(sc);
626 r = t4_read_reg(sc, A_SGE_TIMER_VALUE_4_AND_5);
627 s->timer_val[4] = G_TIMERVALUE4(r) / core_ticks_per_usec(sc);
628 s->timer_val[5] = G_TIMERVALUE5(r) / core_ticks_per_usec(sc);
630 if (cong_drop == 0) {
631 m = F_TUNNELCNGDROP0 | F_TUNNELCNGDROP1 | F_TUNNELCNGDROP2 |
633 r = t4_read_reg(sc, A_TP_PARA_REG3);
635 device_printf(sc->dev,
636 "invalid TP_PARA_REG3(0x%x)\n", r);
641 v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6);
642 r = t4_read_reg(sc, A_ULP_RX_TDDP_PSZ);
644 device_printf(sc->dev, "invalid ULP_RX_TDDP_PSZ(0x%x)\n", r);
648 m = v = F_TDDPTAGTCB;
649 r = t4_read_reg(sc, A_ULP_RX_CTL);
651 device_printf(sc->dev, "invalid ULP_RX_CTL(0x%x)\n", r);
655 m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET |
657 v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET;
658 r = t4_read_reg(sc, A_TP_PARA_REG5);
660 device_printf(sc->dev, "invalid TP_PARA_REG5(0x%x)\n", r);
664 r = t4_read_reg(sc, A_SGE_CONM_CTRL);
665 s->fl_starve_threshold = G_EGRTHRESHOLD(r) * 2 + 1;
667 s->fl_starve_threshold2 = s->fl_starve_threshold;
669 s->fl_starve_threshold2 = G_EGRTHRESHOLDPACKING(r) * 2 + 1;
671 /* egress queues: log2 of # of doorbells per BAR2 page */
672 r = t4_read_reg(sc, A_SGE_EGRESS_QUEUES_PER_PAGE_PF);
673 r >>= S_QUEUESPERPAGEPF0 +
674 (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) * sc->pf;
675 s->eq_s_qpp = r & M_QUEUESPERPAGEPF0;
677 /* ingress queues: log2 of # of doorbells per BAR2 page */
678 r = t4_read_reg(sc, A_SGE_INGRESS_QUEUES_PER_PAGE_PF);
679 r >>= S_QUEUESPERPAGEPF0 +
680 (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) * sc->pf;
681 s->iq_s_qpp = r & M_QUEUESPERPAGEPF0;
683 t4_init_tp_params(sc);
685 t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
686 t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
692 t4_create_dma_tag(struct adapter *sc)
696 rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
697 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE,
698 BUS_SPACE_UNRESTRICTED, BUS_SPACE_MAXSIZE, BUS_DMA_ALLOCNOW, NULL,
701 device_printf(sc->dev,
702 "failed to create main DMA tag: %d\n", rc);
709 enable_buffer_packing(struct adapter *sc)
712 if (sc->flags & BUF_PACKING_OK &&
713 ((is_t5(sc) && buffer_packing) || /* 1 or -1 both ok for T5 */
714 (is_t4(sc) && buffer_packing == 1)))
720 t4_sge_sysctls(struct adapter *sc, struct sysctl_ctx_list *ctx,
721 struct sysctl_oid_list *children)
724 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "buffer_sizes",
725 CTLTYPE_STRING | CTLFLAG_RD, &sc->sge, 0, sysctl_bufsizes, "A",
726 "freelist buffer sizes");
728 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pktshift", CTLFLAG_RD,
729 NULL, fl_pktshift, "payload DMA offset in rx buffer (bytes)");
731 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pad", CTLFLAG_RD,
732 NULL, fl_pad, "payload pad boundary (bytes)");
734 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "spg_len", CTLFLAG_RD,
735 NULL, spg_len, "status page size (bytes)");
737 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_drop", CTLFLAG_RD,
738 NULL, cong_drop, "congestion drop setting");
740 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "buffer_packing", CTLFLAG_RD,
741 NULL, enable_buffer_packing(sc),
742 "pack multiple frames in one fl buffer");
744 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pack", CTLFLAG_RD,
745 NULL, sc->sge.pack_boundary, "payload pack boundary (bytes)");
749 t4_destroy_dma_tag(struct adapter *sc)
752 bus_dma_tag_destroy(sc->dmat);
758 * Allocate and initialize the firmware event queue and the management queue.
760 * Returns errno on failure. Resources allocated up to that point may still be
761 * allocated. Caller is responsible for cleanup in case this function fails.
764 t4_setup_adapter_queues(struct adapter *sc)
768 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
770 sysctl_ctx_init(&sc->ctx);
771 sc->flags |= ADAP_SYSCTL_CTX;
774 * Firmware event queue
781 * Management queue. This is just a control queue that uses the fwq as
784 rc = alloc_mgmtq(sc);
793 t4_teardown_adapter_queues(struct adapter *sc)
796 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
798 /* Do this before freeing the queue */
799 if (sc->flags & ADAP_SYSCTL_CTX) {
800 sysctl_ctx_free(&sc->ctx);
801 sc->flags &= ~ADAP_SYSCTL_CTX;
811 first_vector(struct port_info *pi)
813 struct adapter *sc = pi->adapter;
814 int rc = T4_EXTRA_INTR, i;
816 if (sc->intr_count == 1)
819 for_each_port(sc, i) {
820 struct port_info *p = sc->port[i];
822 if (i == pi->port_id)
826 if (sc->flags & INTR_DIRECT)
827 rc += p->nrxq + p->nofldrxq;
829 rc += max(p->nrxq, p->nofldrxq);
832 * Not compiled with offload support and intr_count > 1. Only
833 * NIC queues exist and they'd better be taking direct
836 KASSERT(sc->flags & INTR_DIRECT,
837 ("%s: intr_count %d, !INTR_DIRECT", __func__,
848 * Given an arbitrary "index," come up with an iq that can be used by other
849 * queues (of this port) for interrupt forwarding, SGE egress updates, etc.
850 * The iq returned is guaranteed to be something that takes direct interrupts.
852 static struct sge_iq *
853 port_intr_iq(struct port_info *pi, int idx)
855 struct adapter *sc = pi->adapter;
856 struct sge *s = &sc->sge;
857 struct sge_iq *iq = NULL;
859 if (sc->intr_count == 1)
860 return (&sc->sge.fwq);
863 if (sc->flags & INTR_DIRECT) {
864 idx %= pi->nrxq + pi->nofldrxq;
866 if (idx >= pi->nrxq) {
868 iq = &s->ofld_rxq[pi->first_ofld_rxq + idx].iq;
870 iq = &s->rxq[pi->first_rxq + idx].iq;
873 idx %= max(pi->nrxq, pi->nofldrxq);
875 if (pi->nrxq >= pi->nofldrxq)
876 iq = &s->rxq[pi->first_rxq + idx].iq;
878 iq = &s->ofld_rxq[pi->first_ofld_rxq + idx].iq;
882 * Not compiled with offload support and intr_count > 1. Only NIC
883 * queues exist and they'd better be taking direct interrupts.
885 KASSERT(sc->flags & INTR_DIRECT,
886 ("%s: intr_count %d, !INTR_DIRECT", __func__, sc->intr_count));
889 iq = &s->rxq[pi->first_rxq + idx].iq;
892 KASSERT(iq->flags & IQ_INTR, ("%s: EDOOFUS", __func__));
896 /* Maximum payload that can be delivered with a single iq descriptor */
898 mtu_to_max_payload(struct adapter *sc, int mtu, const int toe)
904 payload = sc->tt.rx_coalesce ?
905 G_RXCOALESCESIZE(t4_read_reg(sc, A_TP_PARA_REG2)) : mtu;
908 /* large enough even when hw VLAN extraction is disabled */
909 payload = fl_pktshift + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN +
914 payload = roundup2(payload, fl_pad);
920 t4_setup_port_queues(struct port_info *pi)
922 int rc = 0, i, j, intr_idx, iqid;
925 struct sge_wrq *ctrlq;
927 struct sge_ofld_rxq *ofld_rxq;
928 struct sge_wrq *ofld_txq;
929 struct sysctl_oid *oid2 = NULL;
932 struct adapter *sc = pi->adapter;
933 struct ifnet *ifp = pi->ifp;
934 struct sysctl_oid *oid = device_get_sysctl_tree(pi->dev);
935 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
936 int maxp, pack, mtu = ifp->if_mtu;
938 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "rxq", CTLFLAG_RD,
942 if (is_offload(sc)) {
943 oid2 = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ofld_rxq",
945 "rx queues for offloaded TCP connections");
949 /* Interrupt vector to start from (when using multiple vectors) */
950 intr_idx = first_vector(pi);
953 * First pass over all rx queues (NIC and TOE):
954 * a) initialize iq and fl
955 * b) allocate queue iff it will take direct interrupts.
957 maxp = mtu_to_max_payload(sc, mtu, 0);
958 pack = enable_buffer_packing(sc);
959 for_each_rxq(pi, i, rxq) {
961 init_iq(&rxq->iq, sc, pi->tmr_idx, pi->pktc_idx, pi->qsize_rxq,
964 snprintf(name, sizeof(name), "%s rxq%d-fl",
965 device_get_nameunit(pi->dev), i);
966 init_fl(sc, &rxq->fl, pi->qsize_rxq / 8, maxp, pack, name);
968 if (sc->flags & INTR_DIRECT
970 || (sc->intr_count > 1 && pi->nrxq >= pi->nofldrxq)
973 rxq->iq.flags |= IQ_INTR;
974 rc = alloc_rxq(pi, rxq, intr_idx, i, oid);
982 maxp = mtu_to_max_payload(sc, mtu, 1);
983 for_each_ofld_rxq(pi, i, ofld_rxq) {
985 init_iq(&ofld_rxq->iq, sc, pi->tmr_idx, pi->pktc_idx,
986 pi->qsize_rxq, RX_IQ_ESIZE);
988 snprintf(name, sizeof(name), "%s ofld_rxq%d-fl",
989 device_get_nameunit(pi->dev), i);
990 init_fl(sc, &ofld_rxq->fl, pi->qsize_rxq / 8, maxp, pack, name);
992 if (sc->flags & INTR_DIRECT ||
993 (sc->intr_count > 1 && pi->nofldrxq > pi->nrxq)) {
994 ofld_rxq->iq.flags |= IQ_INTR;
995 rc = alloc_ofld_rxq(pi, ofld_rxq, intr_idx, i, oid2);
1004 * Second pass over all rx queues (NIC and TOE). The queues forwarding
1005 * their interrupts are allocated now.
1008 for_each_rxq(pi, i, rxq) {
1009 if (rxq->iq.flags & IQ_INTR)
1012 intr_idx = port_intr_iq(pi, j)->abs_id;
1014 rc = alloc_rxq(pi, rxq, intr_idx, i, oid);
1021 for_each_ofld_rxq(pi, i, ofld_rxq) {
1022 if (ofld_rxq->iq.flags & IQ_INTR)
1025 intr_idx = port_intr_iq(pi, j)->abs_id;
1027 rc = alloc_ofld_rxq(pi, ofld_rxq, intr_idx, i, oid2);
1035 * Now the tx queues. Only one pass needed.
1037 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "txq", CTLFLAG_RD,
1040 for_each_txq(pi, i, txq) {
1043 iqid = port_intr_iq(pi, j)->cntxt_id;
1045 snprintf(name, sizeof(name), "%s txq%d",
1046 device_get_nameunit(pi->dev), i);
1047 init_eq(&txq->eq, EQ_ETH, pi->qsize_txq, pi->tx_chan, iqid,
1050 rc = alloc_txq(pi, txq, i, oid);
1057 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ofld_txq",
1058 CTLFLAG_RD, NULL, "tx queues for offloaded TCP connections");
1059 for_each_ofld_txq(pi, i, ofld_txq) {
1062 iqid = port_intr_iq(pi, j)->cntxt_id;
1064 snprintf(name, sizeof(name), "%s ofld_txq%d",
1065 device_get_nameunit(pi->dev), i);
1066 init_eq(&ofld_txq->eq, EQ_OFLD, pi->qsize_txq, pi->tx_chan,
1069 snprintf(name, sizeof(name), "%d", i);
1070 oid2 = SYSCTL_ADD_NODE(&pi->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1071 name, CTLFLAG_RD, NULL, "offload tx queue");
1073 rc = alloc_wrq(sc, pi, ofld_txq, oid2);
1081 * Finally, the control queue.
1083 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ctrlq", CTLFLAG_RD,
1084 NULL, "ctrl queue");
1085 ctrlq = &sc->sge.ctrlq[pi->port_id];
1086 iqid = port_intr_iq(pi, 0)->cntxt_id;
1087 snprintf(name, sizeof(name), "%s ctrlq", device_get_nameunit(pi->dev));
1088 init_eq(&ctrlq->eq, EQ_CTRL, CTRL_EQ_QSIZE, pi->tx_chan, iqid, name);
1089 rc = alloc_wrq(sc, pi, ctrlq, oid);
1093 t4_teardown_port_queues(pi);
1102 t4_teardown_port_queues(struct port_info *pi)
1105 struct adapter *sc = pi->adapter;
1106 struct sge_rxq *rxq;
1107 struct sge_txq *txq;
1109 struct sge_ofld_rxq *ofld_rxq;
1110 struct sge_wrq *ofld_txq;
1113 /* Do this before freeing the queues */
1114 if (pi->flags & PORT_SYSCTL_CTX) {
1115 sysctl_ctx_free(&pi->ctx);
1116 pi->flags &= ~PORT_SYSCTL_CTX;
1120 * Take down all the tx queues first, as they reference the rx queues
1121 * (for egress updates, etc.).
1124 free_wrq(sc, &sc->sge.ctrlq[pi->port_id]);
1126 for_each_txq(pi, i, txq) {
1131 for_each_ofld_txq(pi, i, ofld_txq) {
1132 free_wrq(sc, ofld_txq);
1137 * Then take down the rx queues that forward their interrupts, as they
1138 * reference other rx queues.
1141 for_each_rxq(pi, i, rxq) {
1142 if ((rxq->iq.flags & IQ_INTR) == 0)
1147 for_each_ofld_rxq(pi, i, ofld_rxq) {
1148 if ((ofld_rxq->iq.flags & IQ_INTR) == 0)
1149 free_ofld_rxq(pi, ofld_rxq);
1154 * Then take down the rx queues that take direct interrupts.
1157 for_each_rxq(pi, i, rxq) {
1158 if (rxq->iq.flags & IQ_INTR)
1163 for_each_ofld_rxq(pi, i, ofld_rxq) {
1164 if (ofld_rxq->iq.flags & IQ_INTR)
1165 free_ofld_rxq(pi, ofld_rxq);
1173 * Deals with errors and the firmware event queue. All data rx queues forward
1174 * their interrupt to the firmware event queue.
1177 t4_intr_all(void *arg)
1179 struct adapter *sc = arg;
1180 struct sge_iq *fwq = &sc->sge.fwq;
1183 if (atomic_cmpset_int(&fwq->state, IQS_IDLE, IQS_BUSY)) {
1185 atomic_cmpset_int(&fwq->state, IQS_BUSY, IQS_IDLE);
1189 /* Deals with error interrupts */
1191 t4_intr_err(void *arg)
1193 struct adapter *sc = arg;
1195 t4_write_reg(sc, MYPF_REG(A_PCIE_PF_CLI), 0);
1196 t4_slow_intr_handler(sc);
1200 t4_intr_evt(void *arg)
1202 struct sge_iq *iq = arg;
1204 if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) {
1206 atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE);
1213 struct sge_iq *iq = arg;
1215 if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) {
1217 atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE);
1222 * Deals with anything and everything on the given ingress queue.
1225 service_iq(struct sge_iq *iq, int budget)
1228 struct sge_rxq *rxq = iq_to_rxq(iq); /* Use iff iq is part of rxq */
1229 struct sge_fl *fl = &rxq->fl; /* Use iff IQ_HAS_FL */
1230 struct adapter *sc = iq->adapter;
1231 struct rsp_ctrl *ctrl;
1232 const struct rss_header *rss;
1233 int ndescs = 0, limit, fl_bufs_used = 0;
1237 STAILQ_HEAD(, sge_iq) iql = STAILQ_HEAD_INITIALIZER(iql);
1238 #if defined(INET) || defined(INET6)
1239 const struct timeval lro_timeout = {0, sc->lro_timeout};
1242 limit = budget ? budget : iq->qsize / 8;
1244 KASSERT(iq->state == IQS_BUSY, ("%s: iq %p not BUSY", __func__, iq));
1247 * We always come back and check the descriptor ring for new indirect
1248 * interrupts and other responses after running a single handler.
1251 while (is_new_response(iq, &ctrl)) {
1256 rsp_type = G_RSPD_TYPE(ctrl->u.type_gen);
1257 lq = be32toh(ctrl->pldbuflen_qid);
1258 rss = (const void *)iq->cdesc;
1261 case X_RSPD_TYPE_FLBUF:
1263 KASSERT(iq->flags & IQ_HAS_FL,
1264 ("%s: data for an iq (%p) with no freelist",
1267 m0 = get_fl_payload(sc, fl, lq, &fl_bufs_used);
1268 if (__predict_false(m0 == NULL))
1270 #ifdef T4_PKT_TIMESTAMP
1272 * 60 bit timestamp for the payload is
1273 * *(uint64_t *)m0->m_pktdat. Note that it is
1274 * in the leading free-space in the mbuf. The
1275 * kernel can clobber it during a pullup,
1276 * m_copymdata, etc. You need to make sure that
1277 * the mbuf reaches you unmolested if you care
1278 * about the timestamp.
1280 *(uint64_t *)m0->m_pktdat =
1281 be64toh(ctrl->u.last_flit) &
1287 case X_RSPD_TYPE_CPL:
1288 KASSERT(rss->opcode < NUM_CPL_CMDS,
1289 ("%s: bad opcode %02x.", __func__,
1291 sc->cpl_handler[rss->opcode](iq, rss, m0);
1294 case X_RSPD_TYPE_INTR:
1297 * Interrupts should be forwarded only to queues
1298 * that are not forwarding their interrupts.
1299 * This means service_iq can recurse but only 1
1302 KASSERT(budget == 0,
1303 ("%s: budget %u, rsp_type %u", __func__,
1307 * There are 1K interrupt-capable queues (qids 0
1308 * through 1023). A response type indicating a
1309 * forwarded interrupt with a qid >= 1K is an
1310 * iWARP async notification.
1313 sc->an_handler(iq, ctrl);
1317 q = sc->sge.iqmap[lq - sc->sge.iq_start];
1318 if (atomic_cmpset_int(&q->state, IQS_IDLE,
1320 if (service_iq(q, q->qsize / 8) == 0) {
1321 atomic_cmpset_int(&q->state,
1322 IQS_BUSY, IQS_IDLE);
1324 STAILQ_INSERT_TAIL(&iql, q,
1332 ("%s: illegal response type %d on iq %p",
1333 __func__, rsp_type, iq));
1335 "%s: illegal response type %d on iq %p",
1336 device_get_nameunit(sc->dev), rsp_type, iq);
1340 if (fl_bufs_used >= 16) {
1342 fl->needed += fl_bufs_used;
1343 refill_fl(sc, fl, 32);
1349 if (++ndescs == limit) {
1350 t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS),
1352 V_INGRESSQID(iq->cntxt_id) |
1353 V_SEINTARM(V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX)));
1356 #if defined(INET) || defined(INET6)
1357 if (iq->flags & IQ_LRO_ENABLED &&
1358 sc->lro_timeout != 0) {
1359 tcp_lro_flush_inactive(&rxq->lro,
1365 return (EINPROGRESS);
1370 if (STAILQ_EMPTY(&iql))
1374 * Process the head only, and send it to the back of the list if
1375 * it's still not done.
1377 q = STAILQ_FIRST(&iql);
1378 STAILQ_REMOVE_HEAD(&iql, link);
1379 if (service_iq(q, q->qsize / 8) == 0)
1380 atomic_cmpset_int(&q->state, IQS_BUSY, IQS_IDLE);
1382 STAILQ_INSERT_TAIL(&iql, q, link);
1385 #if defined(INET) || defined(INET6)
1386 if (iq->flags & IQ_LRO_ENABLED) {
1387 struct lro_ctrl *lro = &rxq->lro;
1388 struct lro_entry *l;
1390 while (!SLIST_EMPTY(&lro->lro_active)) {
1391 l = SLIST_FIRST(&lro->lro_active);
1392 SLIST_REMOVE_HEAD(&lro->lro_active, next);
1393 tcp_lro_flush(lro, l);
1398 t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_CIDXINC(ndescs) |
1399 V_INGRESSQID((u32)iq->cntxt_id) | V_SEINTARM(iq->intr_params));
1401 if (iq->flags & IQ_HAS_FL) {
1405 fl->needed += fl_bufs_used;
1406 starved = refill_fl(sc, fl, 64);
1408 if (__predict_false(starved != 0))
1409 add_fl_to_sfl(sc, fl);
1416 cl_has_metadata(struct sge_fl *fl, struct cluster_layout *cll)
1418 int rc = fl->flags & FL_BUF_PACKING || cll->region1 > 0;
1421 MPASS(cll->region3 >= CL_METADATA_SIZE);
1426 static inline struct cluster_metadata *
1427 cl_metadata(struct adapter *sc, struct sge_fl *fl, struct cluster_layout *cll,
1431 if (cl_has_metadata(fl, cll)) {
1432 struct sw_zone_info *swz = &sc->sge.sw_zone_info[cll->zidx];
1434 return ((struct cluster_metadata *)(cl + swz->size) - 1);
1440 rxb_free(struct mbuf *m, void *arg1, void *arg2)
1442 uma_zone_t zone = arg1;
1445 uma_zfree(zone, cl);
1447 return (EXT_FREE_OK);
1451 * The mbuf returned by this function could be allocated from zone_mbuf or
1452 * constructed in spare room in the cluster.
1454 * The mbuf carries the payload in one of these ways
1455 * a) frame inside the mbuf (mbuf from zone_mbuf)
1456 * b) m_cljset (for clusters without metadata) zone_mbuf
1457 * c) m_extaddref (cluster with metadata) inline mbuf
1458 * d) m_extaddref (cluster with metadata) zone_mbuf
1460 static struct mbuf *
1461 get_scatter_segment(struct adapter *sc, struct sge_fl *fl, int total, int flags)
1464 struct fl_sdesc *sd = &fl->sdesc[fl->cidx];
1465 struct cluster_layout *cll = &sd->cll;
1466 struct sw_zone_info *swz = &sc->sge.sw_zone_info[cll->zidx];
1467 struct hw_buf_info *hwb = &sc->sge.hw_buf_info[cll->hwidx];
1468 struct cluster_metadata *clm = cl_metadata(sc, fl, cll, sd->cl);
1469 int len, padded_len;
1472 len = min(total, hwb->size - fl->rx_offset);
1473 padded_len = roundup2(len, fl_pad);
1474 payload = sd->cl + cll->region1 + fl->rx_offset;
1476 if (sc->sc_do_rxcopy && len < RX_COPY_THRESHOLD) {
1479 * Copy payload into a freshly allocated mbuf.
1482 m = flags & M_PKTHDR ?
1483 m_gethdr(M_NOWAIT, MT_DATA) : m_get(M_NOWAIT, MT_DATA);
1486 fl->mbuf_allocated++;
1487 #ifdef T4_PKT_TIMESTAMP
1488 /* Leave room for a timestamp */
1491 /* copy data to mbuf */
1492 bcopy(payload, mtod(m, caddr_t), len);
1494 } else if (sd->nmbuf * MSIZE < cll->region1) {
1497 * There's spare room in the cluster for an mbuf. Create one
1498 * and associate it with the payload that's in the cluster too.
1502 m = (struct mbuf *)(sd->cl + sd->nmbuf * MSIZE);
1503 /* No bzero required */
1504 if (m_init(m, NULL, 0, M_NOWAIT, MT_DATA, flags | M_NOFREE))
1507 m_extaddref(m, payload, padded_len, &clm->refcount, rxb_free,
1514 * Grab an mbuf from zone_mbuf and associate it with the
1515 * payload in the cluster.
1518 m = flags & M_PKTHDR ?
1519 m_gethdr(M_NOWAIT, MT_DATA) : m_get(M_NOWAIT, MT_DATA);
1522 fl->mbuf_allocated++;
1524 m_extaddref(m, payload, padded_len, &clm->refcount,
1525 rxb_free, swz->zone, sd->cl);
1527 m_cljset(m, sd->cl, swz->type);
1528 sd->cl = NULL; /* consumed, not a recycle candidate */
1531 if (flags & M_PKTHDR)
1532 m->m_pkthdr.len = total;
1535 if (fl->flags & FL_BUF_PACKING) {
1536 fl->rx_offset += roundup2(padded_len, sc->sge.pack_boundary);
1537 MPASS(fl->rx_offset <= hwb->size);
1538 if (fl->rx_offset < hwb->size)
1539 return (m); /* without advancing the cidx */
1542 if (__predict_false(++fl->cidx == fl->cap))
1549 static struct mbuf *
1550 get_fl_payload(struct adapter *sc, struct sge_fl *fl, uint32_t len_newbuf,
1553 struct mbuf *m0, *m, **pnext;
1557 * No assertion for the fl lock because we don't need it. This routine
1558 * is called only from the rx interrupt handler and it only updates
1559 * fl->cidx. (Contrast that with fl->pidx/fl->needed which could be
1560 * updated in the rx interrupt handler or the starvation helper routine.
1561 * That's why code that manipulates fl->pidx/fl->needed needs the fl
1562 * lock but this routine does not).
1566 len = G_RSPD_LEN(len_newbuf);
1567 if (__predict_false(fl->m0 != NULL)) {
1568 M_ASSERTPKTHDR(fl->m0);
1569 MPASS(len == fl->m0->m_pkthdr.len);
1570 MPASS(fl->remaining < len);
1574 len = fl->remaining;
1579 if (fl->rx_offset > 0 && len_newbuf & F_RSPD_NEWBUF) {
1582 if (__predict_false(++fl->cidx == fl->cap))
1587 * Payload starts at rx_offset in the current hw buffer. Its length is
1588 * 'len' and it may span multiple hw buffers.
1591 m0 = get_scatter_segment(sc, fl, len, M_PKTHDR);
1595 pnext = &m0->m_next;
1599 MPASS(fl->rx_offset == 0);
1600 m = get_scatter_segment(sc, fl, len, 0);
1604 fl->remaining = len;
1613 if (fl->rx_offset == 0)
1616 (*fl_bufs_used) += nbuf;
1621 t4_eth_rx(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m0)
1623 struct sge_rxq *rxq = iq_to_rxq(iq);
1624 struct ifnet *ifp = rxq->ifp;
1625 const struct cpl_rx_pkt *cpl = (const void *)(rss + 1);
1626 #if defined(INET) || defined(INET6)
1627 struct lro_ctrl *lro = &rxq->lro;
1630 KASSERT(m0 != NULL, ("%s: no payload with opcode %02x", __func__,
1633 m0->m_pkthdr.len -= fl_pktshift;
1634 m0->m_len -= fl_pktshift;
1635 m0->m_data += fl_pktshift;
1637 m0->m_pkthdr.rcvif = ifp;
1638 m0->m_flags |= M_FLOWID;
1639 m0->m_pkthdr.flowid = be32toh(rss->hash_val);
1641 if (cpl->csum_calc && !cpl->err_vec) {
1642 if (ifp->if_capenable & IFCAP_RXCSUM &&
1643 cpl->l2info & htobe32(F_RXF_IP)) {
1644 m0->m_pkthdr.csum_flags = (CSUM_IP_CHECKED |
1645 CSUM_IP_VALID | CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1647 } else if (ifp->if_capenable & IFCAP_RXCSUM_IPV6 &&
1648 cpl->l2info & htobe32(F_RXF_IP6)) {
1649 m0->m_pkthdr.csum_flags = (CSUM_DATA_VALID_IPV6 |
1654 if (__predict_false(cpl->ip_frag))
1655 m0->m_pkthdr.csum_data = be16toh(cpl->csum);
1657 m0->m_pkthdr.csum_data = 0xffff;
1661 m0->m_pkthdr.ether_vtag = be16toh(cpl->vlan);
1662 m0->m_flags |= M_VLANTAG;
1663 rxq->vlan_extraction++;
1666 #if defined(INET) || defined(INET6)
1667 if (cpl->l2info & htobe32(F_RXF_LRO) &&
1668 iq->flags & IQ_LRO_ENABLED &&
1669 tcp_lro_rx(lro, m0, 0) == 0) {
1670 /* queued for LRO */
1673 ifp->if_input(ifp, m0);
1679 * Doesn't fail. Holds on to work requests it can't send right away.
1682 t4_wrq_tx_locked(struct adapter *sc, struct sge_wrq *wrq, struct wrqe *wr)
1684 struct sge_eq *eq = &wrq->eq;
1688 TXQ_LOCK_ASSERT_OWNED(wrq);
1690 KASSERT((eq->flags & EQ_TYPEMASK) == EQ_OFLD ||
1691 (eq->flags & EQ_TYPEMASK) == EQ_CTRL,
1692 ("%s: eq type %d", __func__, eq->flags & EQ_TYPEMASK));
1694 KASSERT((eq->flags & EQ_TYPEMASK) == EQ_CTRL,
1695 ("%s: eq type %d", __func__, eq->flags & EQ_TYPEMASK));
1698 if (__predict_true(wr != NULL))
1699 STAILQ_INSERT_TAIL(&wrq->wr_list, wr, link);
1701 can_reclaim = reclaimable(eq);
1702 if (__predict_false(eq->flags & EQ_STALLED)) {
1703 if (can_reclaim < tx_resume_threshold(eq))
1705 eq->flags &= ~EQ_STALLED;
1708 eq->cidx += can_reclaim;
1709 eq->avail += can_reclaim;
1710 if (__predict_false(eq->cidx >= eq->cap))
1711 eq->cidx -= eq->cap;
1713 while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL) {
1716 if (__predict_false(wr->wr_len < 0 ||
1717 wr->wr_len > SGE_MAX_WR_LEN || (wr->wr_len & 0x7))) {
1720 panic("%s: work request with length %d", __func__,
1726 log(LOG_ERR, "%s: %s work request with length %d",
1727 device_get_nameunit(sc->dev), __func__, wr->wr_len);
1728 STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
1733 ndesc = howmany(wr->wr_len, EQ_ESIZE);
1734 if (eq->avail < ndesc) {
1739 dst = (void *)&eq->desc[eq->pidx];
1740 copy_to_txd(eq, wrtod(wr), &dst, wr->wr_len);
1744 if (__predict_false(eq->pidx >= eq->cap))
1745 eq->pidx -= eq->cap;
1747 eq->pending += ndesc;
1748 if (eq->pending >= 8)
1752 STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
1755 if (eq->avail < 8) {
1756 can_reclaim = reclaimable(eq);
1757 eq->cidx += can_reclaim;
1758 eq->avail += can_reclaim;
1759 if (__predict_false(eq->cidx >= eq->cap))
1760 eq->cidx -= eq->cap;
1768 eq->flags |= EQ_STALLED;
1769 if (callout_pending(&eq->tx_callout) == 0)
1770 callout_reset(&eq->tx_callout, 1, t4_tx_callout, eq);
1774 /* Per-packet header in a coalesced tx WR, before the SGL starts (in flits) */
1775 #define TXPKTS_PKT_HDR ((\
1776 sizeof(struct ulp_txpkt) + \
1777 sizeof(struct ulptx_idata) + \
1778 sizeof(struct cpl_tx_pkt_core) \
1781 /* Header of a coalesced tx WR, before SGL of first packet (in flits) */
1782 #define TXPKTS_WR_HDR (\
1783 sizeof(struct fw_eth_tx_pkts_wr) / 8 + \
1786 /* Header of a tx WR, before SGL of first packet (in flits) */
1787 #define TXPKT_WR_HDR ((\
1788 sizeof(struct fw_eth_tx_pkt_wr) + \
1789 sizeof(struct cpl_tx_pkt_core) \
1792 /* Header of a tx LSO WR, before SGL of first packet (in flits) */
1793 #define TXPKT_LSO_WR_HDR ((\
1794 sizeof(struct fw_eth_tx_pkt_wr) + \
1795 sizeof(struct cpl_tx_pkt_lso_core) + \
1796 sizeof(struct cpl_tx_pkt_core) \
1800 t4_eth_tx(struct ifnet *ifp, struct sge_txq *txq, struct mbuf *m)
1802 struct port_info *pi = (void *)ifp->if_softc;
1803 struct adapter *sc = pi->adapter;
1804 struct sge_eq *eq = &txq->eq;
1805 struct buf_ring *br = txq->br;
1807 int rc, coalescing, can_reclaim;
1808 struct txpkts txpkts;
1811 TXQ_LOCK_ASSERT_OWNED(txq);
1812 KASSERT(m, ("%s: called with nothing to do.", __func__));
1813 KASSERT((eq->flags & EQ_TYPEMASK) == EQ_ETH,
1814 ("%s: eq type %d", __func__, eq->flags & EQ_TYPEMASK));
1816 prefetch(&eq->desc[eq->pidx]);
1817 prefetch(&txq->sdesc[eq->pidx]);
1819 txpkts.npkt = 0;/* indicates there's nothing in txpkts */
1822 can_reclaim = reclaimable(eq);
1823 if (__predict_false(eq->flags & EQ_STALLED)) {
1824 if (can_reclaim < tx_resume_threshold(eq)) {
1828 eq->flags &= ~EQ_STALLED;
1832 if (__predict_false(eq->flags & EQ_DOOMED)) {
1834 while ((m = buf_ring_dequeue_sc(txq->br)) != NULL)
1839 if (eq->avail < 8 && can_reclaim)
1840 reclaim_tx_descs(txq, can_reclaim, 32);
1842 for (; m; m = next ? next : drbr_dequeue(ifp, br)) {
1847 next = m->m_nextpkt;
1848 m->m_nextpkt = NULL;
1850 if (next || buf_ring_peek(br))
1853 rc = get_pkt_sgl(txq, &m, &sgl, coalescing);
1857 /* Short of resources, suspend tx */
1859 m->m_nextpkt = next;
1864 * Unrecoverable error for this packet, throw it away
1865 * and move on to the next. get_pkt_sgl may already
1866 * have freed m (it will be NULL in that case and the
1867 * m_freem here is still safe).
1875 add_to_txpkts(pi, txq, &txpkts, m, &sgl) == 0) {
1877 /* Successfully absorbed into txpkts */
1879 write_ulp_cpl_sgl(pi, txq, &txpkts, m, &sgl);
1884 * We weren't coalescing to begin with, or current frame could
1885 * not be coalesced (add_to_txpkts flushes txpkts if a frame
1886 * given to it can't be coalesced). Either way there should be
1887 * nothing in txpkts.
1889 KASSERT(txpkts.npkt == 0,
1890 ("%s: txpkts not empty: %d", __func__, txpkts.npkt));
1892 /* We're sending out individual packets now */
1896 reclaim_tx_descs(txq, 0, 8);
1897 rc = write_txpkt_wr(pi, txq, m, &sgl);
1900 /* Short of hardware descriptors, suspend tx */
1903 * This is an unlikely but expensive failure. We've
1904 * done all the hard work (DMA mappings etc.) and now we
1905 * can't send out the packet. What's worse, we have to
1906 * spend even more time freeing up everything in sgl.
1909 free_pkt_sgl(txq, &sgl);
1911 m->m_nextpkt = next;
1915 ETHER_BPF_MTAP(ifp, m);
1919 if (eq->pending >= 8)
1922 can_reclaim = reclaimable(eq);
1923 if (can_reclaim >= 32)
1924 reclaim_tx_descs(txq, can_reclaim, 64);
1927 if (txpkts.npkt > 0)
1928 write_txpkts_wr(txq, &txpkts);
1931 * m not NULL means there was an error but we haven't thrown it away.
1932 * This can happen when we're short of tx descriptors (no_desc) or maybe
1933 * even DMA maps (no_dmamap). Either way, a credit flush and reclaim
1934 * will get things going again.
1936 if (m && !(eq->flags & EQ_CRFLUSHED)) {
1937 struct tx_sdesc *txsd = &txq->sdesc[eq->pidx];
1940 * If EQ_CRFLUSHED is not set then we know we have at least one
1941 * available descriptor because any WR that reduces eq->avail to
1942 * 0 also sets EQ_CRFLUSHED.
1944 KASSERT(eq->avail > 0, ("%s: no space for eqflush.", __func__));
1946 txsd->desc_used = 1;
1948 write_eqflush_wr(eq);
1955 reclaim_tx_descs(txq, 0, 128);
1957 if (eq->flags & EQ_STALLED && callout_pending(&eq->tx_callout) == 0)
1958 callout_reset(&eq->tx_callout, 1, t4_tx_callout, eq);
1964 t4_update_fl_bufsize(struct ifnet *ifp)
1966 struct port_info *pi = ifp->if_softc;
1967 struct adapter *sc = pi->adapter;
1968 struct sge_rxq *rxq;
1970 struct sge_ofld_rxq *ofld_rxq;
1973 int i, maxp, mtu = ifp->if_mtu;
1975 maxp = mtu_to_max_payload(sc, mtu, 0);
1976 for_each_rxq(pi, i, rxq) {
1980 find_best_refill_source(sc, fl, maxp);
1984 maxp = mtu_to_max_payload(sc, mtu, 1);
1985 for_each_ofld_rxq(pi, i, ofld_rxq) {
1989 find_best_refill_source(sc, fl, maxp);
1996 can_resume_tx(struct sge_eq *eq)
1998 return (reclaimable(eq) >= tx_resume_threshold(eq));
2002 init_iq(struct sge_iq *iq, struct adapter *sc, int tmr_idx, int pktc_idx,
2003 int qsize, int esize)
2005 KASSERT(tmr_idx >= 0 && tmr_idx < SGE_NTIMERS,
2006 ("%s: bad tmr_idx %d", __func__, tmr_idx));
2007 KASSERT(pktc_idx < SGE_NCOUNTERS, /* -ve is ok, means don't use */
2008 ("%s: bad pktc_idx %d", __func__, pktc_idx));
2012 iq->intr_params = V_QINTR_TIMER_IDX(tmr_idx);
2013 iq->intr_pktc_idx = SGE_NCOUNTERS - 1;
2014 if (pktc_idx >= 0) {
2015 iq->intr_params |= F_QINTR_CNT_EN;
2016 iq->intr_pktc_idx = pktc_idx;
2018 iq->qsize = roundup2(qsize, 16); /* See FW_IQ_CMD/iqsize */
2019 iq->esize = max(esize, 16); /* See FW_IQ_CMD/iqesize */
2023 init_fl(struct adapter *sc, struct sge_fl *fl, int qsize, int maxp, int pack,
2028 strlcpy(fl->lockname, name, sizeof(fl->lockname));
2030 fl->flags |= FL_BUF_PACKING;
2031 find_best_refill_source(sc, fl, maxp);
2032 find_safe_refill_source(sc, fl);
2036 init_eq(struct sge_eq *eq, int eqtype, int qsize, uint8_t tx_chan,
2037 uint16_t iqid, char *name)
2039 KASSERT(tx_chan < NCHAN, ("%s: bad tx channel %d", __func__, tx_chan));
2040 KASSERT(eqtype <= EQ_TYPEMASK, ("%s: bad qtype %d", __func__, eqtype));
2042 eq->flags = eqtype & EQ_TYPEMASK;
2043 eq->tx_chan = tx_chan;
2046 strlcpy(eq->lockname, name, sizeof(eq->lockname));
2048 TASK_INIT(&eq->tx_task, 0, t4_tx_task, eq);
2049 callout_init(&eq->tx_callout, CALLOUT_MPSAFE);
2053 alloc_ring(struct adapter *sc, size_t len, bus_dma_tag_t *tag,
2054 bus_dmamap_t *map, bus_addr_t *pa, void **va)
2058 rc = bus_dma_tag_create(sc->dmat, 512, 0, BUS_SPACE_MAXADDR,
2059 BUS_SPACE_MAXADDR, NULL, NULL, len, 1, len, 0, NULL, NULL, tag);
2061 device_printf(sc->dev, "cannot allocate DMA tag: %d\n", rc);
2065 rc = bus_dmamem_alloc(*tag, va,
2066 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, map);
2068 device_printf(sc->dev, "cannot allocate DMA memory: %d\n", rc);
2072 rc = bus_dmamap_load(*tag, *map, *va, len, oneseg_dma_callback, pa, 0);
2074 device_printf(sc->dev, "cannot load DMA map: %d\n", rc);
2079 free_ring(sc, *tag, *map, *pa, *va);
2085 free_ring(struct adapter *sc, bus_dma_tag_t tag, bus_dmamap_t map,
2086 bus_addr_t pa, void *va)
2089 bus_dmamap_unload(tag, map);
2091 bus_dmamem_free(tag, va, map);
2093 bus_dma_tag_destroy(tag);
2099 * Allocates the ring for an ingress queue and an optional freelist. If the
2100 * freelist is specified it will be allocated and then associated with the
2103 * Returns errno on failure. Resources allocated up to that point may still be
2104 * allocated. Caller is responsible for cleanup in case this function fails.
2106 * If the ingress queue will take interrupts directly (iq->flags & IQ_INTR) then
2107 * the intr_idx specifies the vector, starting from 0. Otherwise it specifies
2108 * the abs_id of the ingress queue to which its interrupts should be forwarded.
2111 alloc_iq_fl(struct port_info *pi, struct sge_iq *iq, struct sge_fl *fl,
2112 int intr_idx, int cong)
2114 int rc, i, cntxt_id;
2117 struct adapter *sc = iq->adapter;
2120 len = iq->qsize * iq->esize;
2121 rc = alloc_ring(sc, len, &iq->desc_tag, &iq->desc_map, &iq->ba,
2122 (void **)&iq->desc);
2126 bzero(&c, sizeof(c));
2127 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_IQ_CMD) | F_FW_CMD_REQUEST |
2128 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_IQ_CMD_PFN(sc->pf) |
2129 V_FW_IQ_CMD_VFN(0));
2131 c.alloc_to_len16 = htobe32(F_FW_IQ_CMD_ALLOC | F_FW_IQ_CMD_IQSTART |
2134 /* Special handling for firmware event queue */
2135 if (iq == &sc->sge.fwq)
2136 v |= F_FW_IQ_CMD_IQASYNCH;
2138 if (iq->flags & IQ_INTR) {
2139 KASSERT(intr_idx < sc->intr_count,
2140 ("%s: invalid direct intr_idx %d", __func__, intr_idx));
2142 v |= F_FW_IQ_CMD_IQANDST;
2143 v |= V_FW_IQ_CMD_IQANDSTINDEX(intr_idx);
2145 c.type_to_iqandstindex = htobe32(v |
2146 V_FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
2147 V_FW_IQ_CMD_VIID(pi->viid) |
2148 V_FW_IQ_CMD_IQANUD(X_UPDATEDELIVERY_INTERRUPT));
2149 c.iqdroprss_to_iqesize = htobe16(V_FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
2150 F_FW_IQ_CMD_IQGTSMODE |
2151 V_FW_IQ_CMD_IQINTCNTTHRESH(iq->intr_pktc_idx) |
2152 V_FW_IQ_CMD_IQESIZE(ilog2(iq->esize) - 4));
2153 c.iqsize = htobe16(iq->qsize);
2154 c.iqaddr = htobe64(iq->ba);
2156 c.iqns_to_fl0congen = htobe32(F_FW_IQ_CMD_IQFLINTCONGEN);
2159 mtx_init(&fl->fl_lock, fl->lockname, NULL, MTX_DEF);
2161 len = fl->qsize * RX_FL_ESIZE;
2162 rc = alloc_ring(sc, len, &fl->desc_tag, &fl->desc_map,
2163 &fl->ba, (void **)&fl->desc);
2167 /* Allocate space for one software descriptor per buffer. */
2168 fl->cap = (fl->qsize - spg_len / RX_FL_ESIZE) * 8;
2169 rc = alloc_fl_sdesc(fl);
2171 device_printf(sc->dev,
2172 "failed to setup fl software descriptors: %d\n",
2176 fl->needed = fl->cap;
2177 fl->lowat = fl->flags & FL_BUF_PACKING ?
2178 roundup2(sc->sge.fl_starve_threshold2, 8) :
2179 roundup2(sc->sge.fl_starve_threshold, 8);
2181 c.iqns_to_fl0congen |=
2182 htobe32(V_FW_IQ_CMD_FL0HOSTFCMODE(X_HOSTFCMODE_NONE) |
2183 F_FW_IQ_CMD_FL0FETCHRO | F_FW_IQ_CMD_FL0DATARO |
2184 (fl_pad ? F_FW_IQ_CMD_FL0PADEN : 0) |
2185 (fl->flags & FL_BUF_PACKING ? F_FW_IQ_CMD_FL0PACKEN :
2188 c.iqns_to_fl0congen |=
2189 htobe32(V_FW_IQ_CMD_FL0CNGCHMAP(cong) |
2190 F_FW_IQ_CMD_FL0CONGCIF |
2191 F_FW_IQ_CMD_FL0CONGEN);
2193 c.fl0dcaen_to_fl0cidxfthresh =
2194 htobe16(V_FW_IQ_CMD_FL0FBMIN(X_FETCHBURSTMIN_64B) |
2195 V_FW_IQ_CMD_FL0FBMAX(X_FETCHBURSTMAX_512B));
2196 c.fl0size = htobe16(fl->qsize);
2197 c.fl0addr = htobe64(fl->ba);
2200 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
2202 device_printf(sc->dev,
2203 "failed to create ingress queue: %d\n", rc);
2207 iq->cdesc = iq->desc;
2210 iq->intr_next = iq->intr_params;
2211 iq->cntxt_id = be16toh(c.iqid);
2212 iq->abs_id = be16toh(c.physiqid);
2213 iq->flags |= IQ_ALLOCATED;
2215 cntxt_id = iq->cntxt_id - sc->sge.iq_start;
2216 if (cntxt_id >= sc->sge.niq) {
2217 panic ("%s: iq->cntxt_id (%d) more than the max (%d)", __func__,
2218 cntxt_id, sc->sge.niq - 1);
2220 sc->sge.iqmap[cntxt_id] = iq;
2223 fl->cntxt_id = be16toh(c.fl0id);
2224 fl->pidx = fl->cidx = 0;
2226 cntxt_id = fl->cntxt_id - sc->sge.eq_start;
2227 if (cntxt_id >= sc->sge.neq) {
2228 panic("%s: fl->cntxt_id (%d) more than the max (%d)",
2229 __func__, cntxt_id, sc->sge.neq - 1);
2231 sc->sge.eqmap[cntxt_id] = (void *)fl;
2234 /* Enough to make sure the SGE doesn't think it's starved */
2235 refill_fl(sc, fl, fl->lowat);
2238 iq->flags |= IQ_HAS_FL;
2241 if (is_t5(sc) && cong >= 0) {
2242 uint32_t param, val;
2244 param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
2245 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
2246 V_FW_PARAMS_PARAM_YZ(iq->cntxt_id);
2251 for (i = 0; i < 4; i++) {
2252 if (cong & (1 << i))
2253 val |= 1 << (i << 2);
2257 rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
2259 /* report error but carry on */
2260 device_printf(sc->dev,
2261 "failed to set congestion manager context for "
2262 "ingress queue %d: %d\n", iq->cntxt_id, rc);
2266 /* Enable IQ interrupts */
2267 atomic_store_rel_int(&iq->state, IQS_IDLE);
2268 t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_SEINTARM(iq->intr_params) |
2269 V_INGRESSQID(iq->cntxt_id));
2275 free_iq_fl(struct port_info *pi, struct sge_iq *iq, struct sge_fl *fl)
2278 struct adapter *sc = iq->adapter;
2282 return (0); /* nothing to do */
2284 dev = pi ? pi->dev : sc->dev;
2286 if (iq->flags & IQ_ALLOCATED) {
2287 rc = -t4_iq_free(sc, sc->mbox, sc->pf, 0,
2288 FW_IQ_TYPE_FL_INT_CAP, iq->cntxt_id,
2289 fl ? fl->cntxt_id : 0xffff, 0xffff);
2292 "failed to free queue %p: %d\n", iq, rc);
2295 iq->flags &= ~IQ_ALLOCATED;
2298 free_ring(sc, iq->desc_tag, iq->desc_map, iq->ba, iq->desc);
2300 bzero(iq, sizeof(*iq));
2303 free_ring(sc, fl->desc_tag, fl->desc_map, fl->ba,
2307 free_fl_sdesc(sc, fl);
2309 if (mtx_initialized(&fl->fl_lock))
2310 mtx_destroy(&fl->fl_lock);
2312 bzero(fl, sizeof(*fl));
2319 add_fl_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid,
2322 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
2324 oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "fl", CTLFLAG_RD, NULL,
2326 children = SYSCTL_CHILDREN(oid);
2328 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cntxt_id",
2329 CTLTYPE_INT | CTLFLAG_RD, &fl->cntxt_id, 0, sysctl_uint16, "I",
2330 "SGE context id of the freelist");
2331 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cidx", CTLFLAG_RD, &fl->cidx,
2332 0, "consumer index");
2333 if (fl->flags & FL_BUF_PACKING) {
2334 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_offset",
2335 CTLFLAG_RD, &fl->rx_offset, 0, "packing rx offset");
2337 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "pidx", CTLFLAG_RD, &fl->pidx,
2338 0, "producer index");
2339 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "mbuf_allocated",
2340 CTLFLAG_RD, &fl->mbuf_allocated, "# of mbuf allocated");
2341 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "mbuf_inlined",
2342 CTLFLAG_RD, &fl->mbuf_inlined, "# of mbuf inlined in clusters");
2343 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_allocated",
2344 CTLFLAG_RD, &fl->cl_allocated, "# of clusters allocated");
2345 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_recycled",
2346 CTLFLAG_RD, &fl->cl_recycled, "# of clusters recycled");
2347 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_fast_recycled",
2348 CTLFLAG_RD, &fl->cl_fast_recycled, "# of clusters recycled (fast)");
2352 alloc_fwq(struct adapter *sc)
2355 struct sge_iq *fwq = &sc->sge.fwq;
2356 struct sysctl_oid *oid = device_get_sysctl_tree(sc->dev);
2357 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
2359 init_iq(fwq, sc, 0, 0, FW_IQ_QSIZE, FW_IQ_ESIZE);
2360 fwq->flags |= IQ_INTR; /* always */
2361 intr_idx = sc->intr_count > 1 ? 1 : 0;
2362 rc = alloc_iq_fl(sc->port[0], fwq, NULL, intr_idx, -1);
2364 device_printf(sc->dev,
2365 "failed to create firmware event queue: %d\n", rc);
2369 oid = SYSCTL_ADD_NODE(&sc->ctx, children, OID_AUTO, "fwq", CTLFLAG_RD,
2370 NULL, "firmware event queue");
2371 children = SYSCTL_CHILDREN(oid);
2373 SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "abs_id",
2374 CTLTYPE_INT | CTLFLAG_RD, &fwq->abs_id, 0, sysctl_uint16, "I",
2375 "absolute id of the queue");
2376 SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "cntxt_id",
2377 CTLTYPE_INT | CTLFLAG_RD, &fwq->cntxt_id, 0, sysctl_uint16, "I",
2378 "SGE context id of the queue");
2379 SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "cidx",
2380 CTLTYPE_INT | CTLFLAG_RD, &fwq->cidx, 0, sysctl_uint16, "I",
2387 free_fwq(struct adapter *sc)
2389 return free_iq_fl(NULL, &sc->sge.fwq, NULL);
2393 alloc_mgmtq(struct adapter *sc)
2396 struct sge_wrq *mgmtq = &sc->sge.mgmtq;
2398 struct sysctl_oid *oid = device_get_sysctl_tree(sc->dev);
2399 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
2401 oid = SYSCTL_ADD_NODE(&sc->ctx, children, OID_AUTO, "mgmtq", CTLFLAG_RD,
2402 NULL, "management queue");
2404 snprintf(name, sizeof(name), "%s mgmtq", device_get_nameunit(sc->dev));
2405 init_eq(&mgmtq->eq, EQ_CTRL, CTRL_EQ_QSIZE, sc->port[0]->tx_chan,
2406 sc->sge.fwq.cntxt_id, name);
2407 rc = alloc_wrq(sc, NULL, mgmtq, oid);
2409 device_printf(sc->dev,
2410 "failed to create management queue: %d\n", rc);
2418 free_mgmtq(struct adapter *sc)
2421 return free_wrq(sc, &sc->sge.mgmtq);
2425 tnl_cong(struct port_info *pi)
2428 if (cong_drop == -1)
2430 else if (cong_drop == 1)
2433 return (pi->rx_chan_map);
2437 alloc_rxq(struct port_info *pi, struct sge_rxq *rxq, int intr_idx, int idx,
2438 struct sysctl_oid *oid)
2441 struct sysctl_oid_list *children;
2444 rc = alloc_iq_fl(pi, &rxq->iq, &rxq->fl, intr_idx, tnl_cong(pi));
2449 refill_fl(pi->adapter, &rxq->fl, rxq->fl.needed / 8);
2450 FL_UNLOCK(&rxq->fl);
2452 #if defined(INET) || defined(INET6)
2453 rc = tcp_lro_init(&rxq->lro);
2456 rxq->lro.ifp = pi->ifp; /* also indicates LRO init'ed */
2458 if (pi->ifp->if_capenable & IFCAP_LRO)
2459 rxq->iq.flags |= IQ_LRO_ENABLED;
2463 children = SYSCTL_CHILDREN(oid);
2465 snprintf(name, sizeof(name), "%d", idx);
2466 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
2468 children = SYSCTL_CHILDREN(oid);
2470 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "abs_id",
2471 CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.abs_id, 0, sysctl_uint16, "I",
2472 "absolute id of the queue");
2473 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cntxt_id",
2474 CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.cntxt_id, 0, sysctl_uint16, "I",
2475 "SGE context id of the queue");
2476 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
2477 CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.cidx, 0, sysctl_uint16, "I",
2479 #if defined(INET) || defined(INET6)
2480 SYSCTL_ADD_INT(&pi->ctx, children, OID_AUTO, "lro_queued", CTLFLAG_RD,
2481 &rxq->lro.lro_queued, 0, NULL);
2482 SYSCTL_ADD_INT(&pi->ctx, children, OID_AUTO, "lro_flushed", CTLFLAG_RD,
2483 &rxq->lro.lro_flushed, 0, NULL);
2485 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "rxcsum", CTLFLAG_RD,
2486 &rxq->rxcsum, "# of times hardware assisted with checksum");
2487 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "vlan_extraction",
2488 CTLFLAG_RD, &rxq->vlan_extraction,
2489 "# of times hardware extracted 802.1Q tag");
2491 add_fl_sysctls(&pi->ctx, oid, &rxq->fl);
2497 free_rxq(struct port_info *pi, struct sge_rxq *rxq)
2501 #if defined(INET) || defined(INET6)
2503 tcp_lro_free(&rxq->lro);
2504 rxq->lro.ifp = NULL;
2508 rc = free_iq_fl(pi, &rxq->iq, &rxq->fl);
2510 bzero(rxq, sizeof(*rxq));
2517 alloc_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq,
2518 int intr_idx, int idx, struct sysctl_oid *oid)
2521 struct sysctl_oid_list *children;
2524 rc = alloc_iq_fl(pi, &ofld_rxq->iq, &ofld_rxq->fl, intr_idx,
2529 children = SYSCTL_CHILDREN(oid);
2531 snprintf(name, sizeof(name), "%d", idx);
2532 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
2534 children = SYSCTL_CHILDREN(oid);
2536 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "abs_id",
2537 CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.abs_id, 0, sysctl_uint16,
2538 "I", "absolute id of the queue");
2539 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cntxt_id",
2540 CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.cntxt_id, 0, sysctl_uint16,
2541 "I", "SGE context id of the queue");
2542 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
2543 CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.cidx, 0, sysctl_uint16, "I",
2546 add_fl_sysctls(&pi->ctx, oid, &ofld_rxq->fl);
2552 free_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq)
2556 rc = free_iq_fl(pi, &ofld_rxq->iq, &ofld_rxq->fl);
2558 bzero(ofld_rxq, sizeof(*ofld_rxq));
2565 ctrl_eq_alloc(struct adapter *sc, struct sge_eq *eq)
2568 struct fw_eq_ctrl_cmd c;
2570 bzero(&c, sizeof(c));
2572 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_CTRL_CMD) | F_FW_CMD_REQUEST |
2573 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_CTRL_CMD_PFN(sc->pf) |
2574 V_FW_EQ_CTRL_CMD_VFN(0));
2575 c.alloc_to_len16 = htobe32(F_FW_EQ_CTRL_CMD_ALLOC |
2576 F_FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
2577 c.cmpliqid_eqid = htonl(V_FW_EQ_CTRL_CMD_CMPLIQID(eq->iqid)); /* XXX */
2578 c.physeqid_pkd = htobe32(0);
2579 c.fetchszm_to_iqid =
2580 htobe32(V_FW_EQ_CTRL_CMD_HOSTFCMODE(X_HOSTFCMODE_STATUS_PAGE) |
2581 V_FW_EQ_CTRL_CMD_PCIECHN(eq->tx_chan) |
2582 F_FW_EQ_CTRL_CMD_FETCHRO | V_FW_EQ_CTRL_CMD_IQID(eq->iqid));
2584 htobe32(V_FW_EQ_CTRL_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
2585 V_FW_EQ_CTRL_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
2586 V_FW_EQ_CTRL_CMD_CIDXFTHRESH(X_CIDXFLUSHTHRESH_32) |
2587 V_FW_EQ_CTRL_CMD_EQSIZE(eq->qsize));
2588 c.eqaddr = htobe64(eq->ba);
2590 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
2592 device_printf(sc->dev,
2593 "failed to create control queue %d: %d\n", eq->tx_chan, rc);
2596 eq->flags |= EQ_ALLOCATED;
2598 eq->cntxt_id = G_FW_EQ_CTRL_CMD_EQID(be32toh(c.cmpliqid_eqid));
2599 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
2600 if (cntxt_id >= sc->sge.neq)
2601 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
2602 cntxt_id, sc->sge.neq - 1);
2603 sc->sge.eqmap[cntxt_id] = eq;
2609 eth_eq_alloc(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
2612 struct fw_eq_eth_cmd c;
2614 bzero(&c, sizeof(c));
2616 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_ETH_CMD) | F_FW_CMD_REQUEST |
2617 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_ETH_CMD_PFN(sc->pf) |
2618 V_FW_EQ_ETH_CMD_VFN(0));
2619 c.alloc_to_len16 = htobe32(F_FW_EQ_ETH_CMD_ALLOC |
2620 F_FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
2621 c.viid_pkd = htobe32(V_FW_EQ_ETH_CMD_VIID(pi->viid));
2622 c.fetchszm_to_iqid =
2623 htobe32(V_FW_EQ_ETH_CMD_HOSTFCMODE(X_HOSTFCMODE_STATUS_PAGE) |
2624 V_FW_EQ_ETH_CMD_PCIECHN(eq->tx_chan) | F_FW_EQ_ETH_CMD_FETCHRO |
2625 V_FW_EQ_ETH_CMD_IQID(eq->iqid));
2626 c.dcaen_to_eqsize = htobe32(V_FW_EQ_ETH_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
2627 V_FW_EQ_ETH_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
2628 V_FW_EQ_ETH_CMD_CIDXFTHRESH(X_CIDXFLUSHTHRESH_32) |
2629 V_FW_EQ_ETH_CMD_EQSIZE(eq->qsize));
2630 c.eqaddr = htobe64(eq->ba);
2632 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
2634 device_printf(pi->dev,
2635 "failed to create Ethernet egress queue: %d\n", rc);
2638 eq->flags |= EQ_ALLOCATED;
2640 eq->cntxt_id = G_FW_EQ_ETH_CMD_EQID(be32toh(c.eqid_pkd));
2641 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
2642 if (cntxt_id >= sc->sge.neq)
2643 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
2644 cntxt_id, sc->sge.neq - 1);
2645 sc->sge.eqmap[cntxt_id] = eq;
2652 ofld_eq_alloc(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
2655 struct fw_eq_ofld_cmd c;
2657 bzero(&c, sizeof(c));
2659 c.op_to_vfn = htonl(V_FW_CMD_OP(FW_EQ_OFLD_CMD) | F_FW_CMD_REQUEST |
2660 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_OFLD_CMD_PFN(sc->pf) |
2661 V_FW_EQ_OFLD_CMD_VFN(0));
2662 c.alloc_to_len16 = htonl(F_FW_EQ_OFLD_CMD_ALLOC |
2663 F_FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
2664 c.fetchszm_to_iqid =
2665 htonl(V_FW_EQ_OFLD_CMD_HOSTFCMODE(X_HOSTFCMODE_STATUS_PAGE) |
2666 V_FW_EQ_OFLD_CMD_PCIECHN(eq->tx_chan) |
2667 F_FW_EQ_OFLD_CMD_FETCHRO | V_FW_EQ_OFLD_CMD_IQID(eq->iqid));
2669 htobe32(V_FW_EQ_OFLD_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
2670 V_FW_EQ_OFLD_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
2671 V_FW_EQ_OFLD_CMD_CIDXFTHRESH(X_CIDXFLUSHTHRESH_32) |
2672 V_FW_EQ_OFLD_CMD_EQSIZE(eq->qsize));
2673 c.eqaddr = htobe64(eq->ba);
2675 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
2677 device_printf(pi->dev,
2678 "failed to create egress queue for TCP offload: %d\n", rc);
2681 eq->flags |= EQ_ALLOCATED;
2683 eq->cntxt_id = G_FW_EQ_OFLD_CMD_EQID(be32toh(c.eqid_pkd));
2684 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
2685 if (cntxt_id >= sc->sge.neq)
2686 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
2687 cntxt_id, sc->sge.neq - 1);
2688 sc->sge.eqmap[cntxt_id] = eq;
2695 alloc_eq(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
2700 mtx_init(&eq->eq_lock, eq->lockname, NULL, MTX_DEF);
2702 len = eq->qsize * EQ_ESIZE;
2703 rc = alloc_ring(sc, len, &eq->desc_tag, &eq->desc_map,
2704 &eq->ba, (void **)&eq->desc);
2708 eq->cap = eq->qsize - spg_len / EQ_ESIZE;
2709 eq->spg = (void *)&eq->desc[eq->cap];
2710 eq->avail = eq->cap - 1; /* one less to avoid cidx = pidx */
2711 eq->pidx = eq->cidx = 0;
2712 eq->doorbells = sc->doorbells;
2714 switch (eq->flags & EQ_TYPEMASK) {
2716 rc = ctrl_eq_alloc(sc, eq);
2720 rc = eth_eq_alloc(sc, pi, eq);
2725 rc = ofld_eq_alloc(sc, pi, eq);
2730 panic("%s: invalid eq type %d.", __func__,
2731 eq->flags & EQ_TYPEMASK);
2734 device_printf(sc->dev,
2735 "failed to allocate egress queue(%d): %d",
2736 eq->flags & EQ_TYPEMASK, rc);
2739 eq->tx_callout.c_cpu = eq->cntxt_id % mp_ncpus;
2741 if (isset(&eq->doorbells, DOORBELL_UDB) ||
2742 isset(&eq->doorbells, DOORBELL_UDBWC) ||
2743 isset(&eq->doorbells, DOORBELL_WCWR)) {
2744 uint32_t s_qpp = sc->sge.eq_s_qpp;
2745 uint32_t mask = (1 << s_qpp) - 1;
2746 volatile uint8_t *udb;
2748 udb = sc->udbs_base + UDBS_DB_OFFSET;
2749 udb += (eq->cntxt_id >> s_qpp) << PAGE_SHIFT; /* pg offset */
2750 eq->udb_qid = eq->cntxt_id & mask; /* id in page */
2751 if (eq->udb_qid > PAGE_SIZE / UDBS_SEG_SIZE)
2752 clrbit(&eq->doorbells, DOORBELL_WCWR);
2754 udb += eq->udb_qid << UDBS_SEG_SHIFT; /* seg offset */
2757 eq->udb = (volatile void *)udb;
2764 free_eq(struct adapter *sc, struct sge_eq *eq)
2768 if (eq->flags & EQ_ALLOCATED) {
2769 switch (eq->flags & EQ_TYPEMASK) {
2771 rc = -t4_ctrl_eq_free(sc, sc->mbox, sc->pf, 0,
2776 rc = -t4_eth_eq_free(sc, sc->mbox, sc->pf, 0,
2782 rc = -t4_ofld_eq_free(sc, sc->mbox, sc->pf, 0,
2788 panic("%s: invalid eq type %d.", __func__,
2789 eq->flags & EQ_TYPEMASK);
2792 device_printf(sc->dev,
2793 "failed to free egress queue (%d): %d\n",
2794 eq->flags & EQ_TYPEMASK, rc);
2797 eq->flags &= ~EQ_ALLOCATED;
2800 free_ring(sc, eq->desc_tag, eq->desc_map, eq->ba, eq->desc);
2802 if (mtx_initialized(&eq->eq_lock))
2803 mtx_destroy(&eq->eq_lock);
2805 bzero(eq, sizeof(*eq));
2810 alloc_wrq(struct adapter *sc, struct port_info *pi, struct sge_wrq *wrq,
2811 struct sysctl_oid *oid)
2814 struct sysctl_ctx_list *ctx = pi ? &pi->ctx : &sc->ctx;
2815 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
2817 rc = alloc_eq(sc, pi, &wrq->eq);
2822 STAILQ_INIT(&wrq->wr_list);
2824 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
2825 &wrq->eq.cntxt_id, 0, "SGE context id of the queue");
2826 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cidx",
2827 CTLTYPE_INT | CTLFLAG_RD, &wrq->eq.cidx, 0, sysctl_uint16, "I",
2829 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pidx",
2830 CTLTYPE_INT | CTLFLAG_RD, &wrq->eq.pidx, 0, sysctl_uint16, "I",
2832 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs", CTLFLAG_RD,
2833 &wrq->tx_wrs, "# of work requests");
2834 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "no_desc", CTLFLAG_RD,
2836 "# of times queue ran out of hardware descriptors");
2837 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "unstalled", CTLFLAG_RD,
2838 &wrq->eq.unstalled, 0, "# of times queue recovered after stall");
2844 free_wrq(struct adapter *sc, struct sge_wrq *wrq)
2848 rc = free_eq(sc, &wrq->eq);
2852 bzero(wrq, sizeof(*wrq));
2857 alloc_txq(struct port_info *pi, struct sge_txq *txq, int idx,
2858 struct sysctl_oid *oid)
2861 struct adapter *sc = pi->adapter;
2862 struct sge_eq *eq = &txq->eq;
2864 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
2866 rc = alloc_eq(sc, pi, eq);
2872 txq->sdesc = malloc(eq->cap * sizeof(struct tx_sdesc), M_CXGBE,
2874 txq->br = buf_ring_alloc(eq->qsize, M_CXGBE, M_WAITOK, &eq->eq_lock);
2876 rc = bus_dma_tag_create(sc->dmat, 1, 0, BUS_SPACE_MAXADDR,
2877 BUS_SPACE_MAXADDR, NULL, NULL, 64 * 1024, TX_SGL_SEGS,
2878 BUS_SPACE_MAXSIZE, BUS_DMA_ALLOCNOW, NULL, NULL, &txq->tx_tag);
2880 device_printf(sc->dev,
2881 "failed to create tx DMA tag: %d\n", rc);
2886 * We can stuff ~10 frames in an 8-descriptor txpkts WR (8 is the SGE
2887 * limit for any WR). txq->no_dmamap events shouldn't occur if maps is
2888 * sized for the worst case.
2890 rc = t4_alloc_tx_maps(&txq->txmaps, txq->tx_tag, eq->qsize * 10 / 8,
2893 device_printf(sc->dev, "failed to setup tx DMA maps: %d\n", rc);
2897 snprintf(name, sizeof(name), "%d", idx);
2898 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
2900 children = SYSCTL_CHILDREN(oid);
2902 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
2903 &eq->cntxt_id, 0, "SGE context id of the queue");
2904 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
2905 CTLTYPE_INT | CTLFLAG_RD, &eq->cidx, 0, sysctl_uint16, "I",
2907 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "pidx",
2908 CTLTYPE_INT | CTLFLAG_RD, &eq->pidx, 0, sysctl_uint16, "I",
2911 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txcsum", CTLFLAG_RD,
2912 &txq->txcsum, "# of times hardware assisted with checksum");
2913 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "vlan_insertion",
2914 CTLFLAG_RD, &txq->vlan_insertion,
2915 "# of times hardware inserted 802.1Q tag");
2916 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "tso_wrs", CTLFLAG_RD,
2917 &txq->tso_wrs, "# of TSO work requests");
2918 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "imm_wrs", CTLFLAG_RD,
2919 &txq->imm_wrs, "# of work requests with immediate data");
2920 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "sgl_wrs", CTLFLAG_RD,
2921 &txq->sgl_wrs, "# of work requests with direct SGL");
2922 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkt_wrs", CTLFLAG_RD,
2923 &txq->txpkt_wrs, "# of txpkt work requests (one pkt/WR)");
2924 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts_wrs", CTLFLAG_RD,
2925 &txq->txpkts_wrs, "# of txpkts work requests (multiple pkts/WR)");
2926 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts_pkts", CTLFLAG_RD,
2927 &txq->txpkts_pkts, "# of frames tx'd using txpkts work requests");
2929 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "br_drops", CTLFLAG_RD,
2930 &txq->br->br_drops, "# of drops in the buf_ring for this queue");
2931 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "no_dmamap", CTLFLAG_RD,
2932 &txq->no_dmamap, 0, "# of times txq ran out of DMA maps");
2933 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "no_desc", CTLFLAG_RD,
2934 &txq->no_desc, 0, "# of times txq ran out of hardware descriptors");
2935 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "egr_update", CTLFLAG_RD,
2936 &eq->egr_update, 0, "egress update notifications from the SGE");
2937 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "unstalled", CTLFLAG_RD,
2938 &eq->unstalled, 0, "# of times txq recovered after stall");
2944 free_txq(struct port_info *pi, struct sge_txq *txq)
2947 struct adapter *sc = pi->adapter;
2948 struct sge_eq *eq = &txq->eq;
2950 rc = free_eq(sc, eq);
2954 free(txq->sdesc, M_CXGBE);
2956 if (txq->txmaps.maps)
2957 t4_free_tx_maps(&txq->txmaps, txq->tx_tag);
2959 buf_ring_free(txq->br, M_CXGBE);
2962 bus_dma_tag_destroy(txq->tx_tag);
2964 bzero(txq, sizeof(*txq));
2969 oneseg_dma_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2971 bus_addr_t *ba = arg;
2974 ("%s meant for single segment mappings only.", __func__));
2976 *ba = error ? 0 : segs->ds_addr;
2980 is_new_response(const struct sge_iq *iq, struct rsp_ctrl **ctrl)
2982 *ctrl = (void *)((uintptr_t)iq->cdesc +
2983 (iq->esize - sizeof(struct rsp_ctrl)));
2985 return (((*ctrl)->u.type_gen >> S_RSPD_GEN) == iq->gen);
2989 iq_next(struct sge_iq *iq)
2991 iq->cdesc = (void *) ((uintptr_t)iq->cdesc + iq->esize);
2992 if (__predict_false(++iq->cidx == iq->qsize - 1)) {
2995 iq->cdesc = iq->desc;
2999 #define FL_HW_IDX(x) ((x) >> 3)
3001 ring_fl_db(struct adapter *sc, struct sge_fl *fl)
3003 int ndesc = fl->pending / 8;
3006 if (FL_HW_IDX(fl->pidx) == FL_HW_IDX(fl->cidx))
3007 ndesc--; /* hold back one credit */
3010 return; /* nothing to do */
3012 v = F_DBPRIO | V_QID(fl->cntxt_id) | V_PIDX(ndesc);
3018 t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL), v);
3019 fl->pending -= ndesc * 8;
3023 * Fill up the freelist by upto nbufs and maybe ring its doorbell.
3025 * Returns non-zero to indicate that it should be added to the list of starving
3029 refill_fl(struct adapter *sc, struct sge_fl *fl, int nbufs)
3031 __be64 *d = &fl->desc[fl->pidx];
3032 struct fl_sdesc *sd = &fl->sdesc[fl->pidx];
3035 struct cluster_layout *cll = &fl->cll_def; /* default layout */
3036 struct sw_zone_info *swz = &sc->sge.sw_zone_info[cll->zidx];
3037 struct cluster_metadata *clm;
3039 FL_LOCK_ASSERT_OWNED(fl);
3041 if (nbufs > fl->needed)
3043 nbufs -= (fl->pidx + nbufs) % 8;
3047 if (sd->cl != NULL) {
3049 if (sd->nmbuf == 0) {
3051 * Fast recycle without involving any atomics on
3052 * the cluster's metadata (if the cluster has
3053 * metadata). This happens when all frames
3054 * received in the cluster were small enough to
3055 * fit within a single mbuf each.
3057 fl->cl_fast_recycled++;
3062 * Cluster is guaranteed to have metadata. Clusters
3063 * without metadata always take the fast recycle path
3064 * when they're recycled.
3066 clm = cl_metadata(sc, fl, &sd->cll, sd->cl);
3069 if (atomic_fetchadd_int(&clm->refcount, -1) == 1) {
3073 sd->cl = NULL; /* gave up my reference */
3075 MPASS(sd->cl == NULL);
3077 cl = uma_zalloc(swz->zone, M_NOWAIT);
3078 if (__predict_false(cl == NULL)) {
3079 if (cll == &fl->cll_alt || fl->cll_alt.zidx == -1 ||
3080 fl->cll_def.zidx == fl->cll_alt.zidx)
3083 /* fall back to the safe zone */
3085 swz = &sc->sge.sw_zone_info[cll->zidx];
3090 pa = pmap_kextract((vm_offset_t)cl);
3094 *d = htobe64(pa | cll->hwidx);
3095 clm = cl_metadata(sc, fl, cll, cl);
3109 if (__predict_false(++fl->pidx == fl->cap)) {
3116 if (fl->pending >= 8)
3119 return (FL_RUNNING_LOW(fl) && !(fl->flags & FL_STARVING));
3123 * Attempt to refill all starving freelists.
3126 refill_sfl(void *arg)
3128 struct adapter *sc = arg;
3129 struct sge_fl *fl, *fl_temp;
3131 mtx_lock(&sc->sfl_lock);
3132 TAILQ_FOREACH_SAFE(fl, &sc->sfl, link, fl_temp) {
3134 refill_fl(sc, fl, 64);
3135 if (FL_NOT_RUNNING_LOW(fl) || fl->flags & FL_DOOMED) {
3136 TAILQ_REMOVE(&sc->sfl, fl, link);
3137 fl->flags &= ~FL_STARVING;
3142 if (!TAILQ_EMPTY(&sc->sfl))
3143 callout_schedule(&sc->sfl_callout, hz / 5);
3144 mtx_unlock(&sc->sfl_lock);
3148 alloc_fl_sdesc(struct sge_fl *fl)
3151 fl->sdesc = malloc(fl->cap * sizeof(struct fl_sdesc), M_CXGBE,
3158 free_fl_sdesc(struct adapter *sc, struct sge_fl *fl)
3160 struct fl_sdesc *sd;
3161 struct cluster_metadata *clm;
3162 struct cluster_layout *cll;
3166 for (i = 0; i < fl->cap; i++, sd++) {
3171 clm = cl_metadata(sc, fl, cll, sd->cl);
3172 if (sd->nmbuf == 0 ||
3173 (clm && atomic_fetchadd_int(&clm->refcount, -1) == 1)) {
3174 uma_zfree(sc->sge.sw_zone_info[cll->zidx].zone, sd->cl);
3179 free(fl->sdesc, M_CXGBE);
3184 t4_alloc_tx_maps(struct tx_maps *txmaps, bus_dma_tag_t tx_tag, int count,
3190 txmaps->map_total = txmaps->map_avail = count;
3191 txmaps->map_cidx = txmaps->map_pidx = 0;
3193 txmaps->maps = malloc(count * sizeof(struct tx_map), M_CXGBE,
3197 for (i = 0; i < count; i++, txm++) {
3198 rc = bus_dmamap_create(tx_tag, 0, &txm->map);
3207 bus_dmamap_destroy(tx_tag, txm->map);
3209 KASSERT(txm == txmaps->maps, ("%s: EDOOFUS", __func__));
3211 free(txmaps->maps, M_CXGBE);
3212 txmaps->maps = NULL;
3218 t4_free_tx_maps(struct tx_maps *txmaps, bus_dma_tag_t tx_tag)
3224 for (i = 0; i < txmaps->map_total; i++, txm++) {
3227 bus_dmamap_unload(tx_tag, txm->map);
3232 bus_dmamap_destroy(tx_tag, txm->map);
3235 free(txmaps->maps, M_CXGBE);
3236 txmaps->maps = NULL;
3240 * We'll do immediate data tx for non-TSO, but only when not coalescing. We're
3241 * willing to use upto 2 hardware descriptors which means a maximum of 96 bytes
3242 * of immediate data.
3246 - sizeof(struct fw_eth_tx_pkt_wr) \
3247 - sizeof(struct cpl_tx_pkt_core))
3250 * Returns non-zero on failure, no need to cleanup anything in that case.
3252 * Note 1: We always try to defrag the mbuf if required and return EFBIG only
3253 * if the resulting chain still won't fit in a tx descriptor.
3255 * Note 2: We'll pullup the mbuf chain if TSO is requested and the first mbuf
3256 * does not have the TCP header in it.
3259 get_pkt_sgl(struct sge_txq *txq, struct mbuf **fp, struct sgl *sgl,
3262 struct mbuf *m = *fp;
3263 struct tx_maps *txmaps;
3265 int rc, defragged = 0, n;
3267 TXQ_LOCK_ASSERT_OWNED(txq);
3269 if (m->m_pkthdr.tso_segsz)
3270 sgl_only = 1; /* Do not allow immediate data with LSO */
3272 start: sgl->nsegs = 0;
3274 if (m->m_pkthdr.len <= IMM_LEN && !sgl_only)
3275 return (0); /* nsegs = 0 tells caller to use imm. tx */
3277 txmaps = &txq->txmaps;
3278 if (txmaps->map_avail == 0) {
3282 txm = &txmaps->maps[txmaps->map_pidx];
3284 if (m->m_pkthdr.tso_segsz && m->m_len < 50) {
3285 *fp = m_pullup(m, 50);
3291 rc = bus_dmamap_load_mbuf_sg(txq->tx_tag, txm->map, m, sgl->seg,
3292 &sgl->nsegs, BUS_DMA_NOWAIT);
3293 if (rc == EFBIG && defragged == 0) {
3294 m = m_defrag(m, M_NOWAIT);
3306 txmaps->map_avail--;
3307 if (++txmaps->map_pidx == txmaps->map_total)
3308 txmaps->map_pidx = 0;
3310 KASSERT(sgl->nsegs > 0 && sgl->nsegs <= TX_SGL_SEGS,
3311 ("%s: bad DMA mapping (%d segments)", __func__, sgl->nsegs));
3314 * Store the # of flits required to hold this frame's SGL in nflits. An
3315 * SGL has a (ULPTX header + len0, addr0) tuple optionally followed by
3316 * multiple (len0 + len1, addr0, addr1) tuples. If addr1 is not used
3317 * then len1 must be set to 0.
3320 sgl->nflits = (3 * n) / 2 + (n & 1) + 2;
3327 * Releases all the txq resources used up in the specified sgl.
3330 free_pkt_sgl(struct sge_txq *txq, struct sgl *sgl)
3332 struct tx_maps *txmaps;
3335 TXQ_LOCK_ASSERT_OWNED(txq);
3337 if (sgl->nsegs == 0)
3338 return (0); /* didn't use any map */
3340 txmaps = &txq->txmaps;
3342 /* 1 pkt uses exactly 1 map, back it out */
3344 txmaps->map_avail++;
3345 if (txmaps->map_pidx > 0)
3348 txmaps->map_pidx = txmaps->map_total - 1;
3350 txm = &txmaps->maps[txmaps->map_pidx];
3351 bus_dmamap_unload(txq->tx_tag, txm->map);
3358 write_txpkt_wr(struct port_info *pi, struct sge_txq *txq, struct mbuf *m,
3361 struct sge_eq *eq = &txq->eq;
3362 struct fw_eth_tx_pkt_wr *wr;
3363 struct cpl_tx_pkt_core *cpl;
3364 uint32_t ctrl; /* used in many unrelated places */
3366 int nflits, ndesc, pktlen;
3367 struct tx_sdesc *txsd;
3370 TXQ_LOCK_ASSERT_OWNED(txq);
3372 pktlen = m->m_pkthdr.len;
3375 * Do we have enough flits to send this frame out?
3377 ctrl = sizeof(struct cpl_tx_pkt_core);
3378 if (m->m_pkthdr.tso_segsz) {
3379 nflits = TXPKT_LSO_WR_HDR;
3380 ctrl += sizeof(struct cpl_tx_pkt_lso_core);
3382 nflits = TXPKT_WR_HDR;
3384 nflits += sgl->nflits;
3386 nflits += howmany(pktlen, 8);
3389 ndesc = howmany(nflits, 8);
3390 if (ndesc > eq->avail)
3393 /* Firmware work request header */
3394 wr = (void *)&eq->desc[eq->pidx];
3395 wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_PKT_WR) |
3396 V_FW_ETH_TX_PKT_WR_IMMDLEN(ctrl));
3397 ctrl = V_FW_WR_LEN16(howmany(nflits, 2));
3398 if (eq->avail == ndesc) {
3399 if (!(eq->flags & EQ_CRFLUSHED)) {
3400 ctrl |= F_FW_WR_EQUEQ | F_FW_WR_EQUIQ;
3401 eq->flags |= EQ_CRFLUSHED;
3403 eq->flags |= EQ_STALLED;
3406 wr->equiq_to_len16 = htobe32(ctrl);
3409 if (m->m_pkthdr.tso_segsz) {
3410 struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
3411 struct ether_header *eh;
3413 #if defined(INET) || defined(INET6)
3418 ctrl = V_LSO_OPCODE(CPL_TX_PKT_LSO) | F_LSO_FIRST_SLICE |
3421 eh = mtod(m, struct ether_header *);
3422 eh_type = ntohs(eh->ether_type);
3423 if (eh_type == ETHERTYPE_VLAN) {
3424 struct ether_vlan_header *evh = (void *)eh;
3426 ctrl |= V_LSO_ETHHDR_LEN(1);
3428 eh_type = ntohs(evh->evl_proto);
3434 case ETHERTYPE_IPV6:
3436 struct ip6_hdr *ip6 = l3hdr;
3439 * XXX-BZ For now we do not pretend to support
3440 * IPv6 extension headers.
3442 KASSERT(ip6->ip6_nxt == IPPROTO_TCP, ("%s: CSUM_TSO "
3443 "with ip6_nxt != TCP: %u", __func__, ip6->ip6_nxt));
3444 tcp = (struct tcphdr *)(ip6 + 1);
3446 ctrl |= V_LSO_IPHDR_LEN(sizeof(*ip6) >> 2) |
3447 V_LSO_TCPHDR_LEN(tcp->th_off);
3454 struct ip *ip = l3hdr;
3456 tcp = (void *)((uintptr_t)ip + ip->ip_hl * 4);
3457 ctrl |= V_LSO_IPHDR_LEN(ip->ip_hl) |
3458 V_LSO_TCPHDR_LEN(tcp->th_off);
3463 panic("%s: CSUM_TSO but no supported IP version "
3464 "(0x%04x)", __func__, eh_type);
3467 lso->lso_ctrl = htobe32(ctrl);
3468 lso->ipid_ofst = htobe16(0);
3469 lso->mss = htobe16(m->m_pkthdr.tso_segsz);
3470 lso->seqno_offset = htobe32(0);
3471 lso->len = htobe32(pktlen);
3473 cpl = (void *)(lso + 1);
3477 cpl = (void *)(wr + 1);
3479 /* Checksum offload */
3481 if (!(m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TSO)))
3482 ctrl1 |= F_TXPKT_IPCSUM_DIS;
3483 if (!(m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP | CSUM_UDP_IPV6 |
3484 CSUM_TCP_IPV6 | CSUM_TSO)))
3485 ctrl1 |= F_TXPKT_L4CSUM_DIS;
3486 if (m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP |
3487 CSUM_UDP_IPV6 | CSUM_TCP_IPV6 | CSUM_TSO))
3488 txq->txcsum++; /* some hardware assistance provided */
3490 /* VLAN tag insertion */
3491 if (m->m_flags & M_VLANTAG) {
3492 ctrl1 |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(m->m_pkthdr.ether_vtag);
3493 txq->vlan_insertion++;
3497 cpl->ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT) |
3498 V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(pi->adapter->pf));
3500 cpl->len = htobe16(pktlen);
3501 cpl->ctrl1 = htobe64(ctrl1);
3503 /* Software descriptor */
3504 txsd = &txq->sdesc[eq->pidx];
3505 txsd->desc_used = ndesc;
3507 eq->pending += ndesc;
3510 if (eq->pidx >= eq->cap)
3511 eq->pidx -= eq->cap;
3514 dst = (void *)(cpl + 1);
3515 if (sgl->nsegs > 0) {
3518 write_sgl_to_txd(eq, sgl, &dst);
3522 for (; m; m = m->m_next) {
3523 copy_to_txd(eq, mtod(m, caddr_t), &dst, m->m_len);
3529 KASSERT(pktlen == 0, ("%s: %d bytes left.", __func__, pktlen));
3539 * Returns 0 to indicate that m has been accepted into a coalesced tx work
3540 * request. It has either been folded into txpkts or txpkts was flushed and m
3541 * has started a new coalesced work request (as the first frame in a fresh
3544 * Returns non-zero to indicate a failure - caller is responsible for
3545 * transmitting m, if there was anything in txpkts it has been flushed.
3548 add_to_txpkts(struct port_info *pi, struct sge_txq *txq, struct txpkts *txpkts,
3549 struct mbuf *m, struct sgl *sgl)
3551 struct sge_eq *eq = &txq->eq;
3553 struct tx_sdesc *txsd;
3556 TXQ_LOCK_ASSERT_OWNED(txq);
3558 KASSERT(sgl->nsegs, ("%s: can't coalesce imm data", __func__));
3560 if (txpkts->npkt > 0) {
3561 flits = TXPKTS_PKT_HDR + sgl->nflits;
3562 can_coalesce = m->m_pkthdr.tso_segsz == 0 &&
3563 txpkts->nflits + flits <= TX_WR_FLITS &&
3564 txpkts->nflits + flits <= eq->avail * 8 &&
3565 txpkts->plen + m->m_pkthdr.len < 65536;
3569 txpkts->nflits += flits;
3570 txpkts->plen += m->m_pkthdr.len;
3572 txsd = &txq->sdesc[eq->pidx];
3579 * Couldn't coalesce m into txpkts. The first order of business
3580 * is to send txpkts on its way. Then we'll revisit m.
3582 write_txpkts_wr(txq, txpkts);
3586 * Check if we can start a new coalesced tx work request with m as
3587 * the first packet in it.
3590 KASSERT(txpkts->npkt == 0, ("%s: txpkts not empty", __func__));
3592 flits = TXPKTS_WR_HDR + sgl->nflits;
3593 can_coalesce = m->m_pkthdr.tso_segsz == 0 &&
3594 flits <= eq->avail * 8 && flits <= TX_WR_FLITS;
3596 if (can_coalesce == 0)
3600 * Start a fresh coalesced tx WR with m as the first frame in it.
3603 txpkts->nflits = flits;
3604 txpkts->flitp = &eq->desc[eq->pidx].flit[2];
3605 txpkts->plen = m->m_pkthdr.len;
3607 txsd = &txq->sdesc[eq->pidx];
3614 * Note that write_txpkts_wr can never run out of hardware descriptors (but
3615 * write_txpkt_wr can). add_to_txpkts ensures that a frame is accepted for
3616 * coalescing only if sufficient hardware descriptors are available.
3619 write_txpkts_wr(struct sge_txq *txq, struct txpkts *txpkts)
3621 struct sge_eq *eq = &txq->eq;
3622 struct fw_eth_tx_pkts_wr *wr;
3623 struct tx_sdesc *txsd;
3627 TXQ_LOCK_ASSERT_OWNED(txq);
3629 ndesc = howmany(txpkts->nflits, 8);
3631 wr = (void *)&eq->desc[eq->pidx];
3632 wr->op_pkd = htobe32(V_FW_WR_OP(FW_ETH_TX_PKTS_WR));
3633 ctrl = V_FW_WR_LEN16(howmany(txpkts->nflits, 2));
3634 if (eq->avail == ndesc) {
3635 if (!(eq->flags & EQ_CRFLUSHED)) {
3636 ctrl |= F_FW_WR_EQUEQ | F_FW_WR_EQUIQ;
3637 eq->flags |= EQ_CRFLUSHED;
3639 eq->flags |= EQ_STALLED;
3641 wr->equiq_to_len16 = htobe32(ctrl);
3642 wr->plen = htobe16(txpkts->plen);
3643 wr->npkt = txpkts->npkt;
3644 wr->r3 = wr->type = 0;
3646 /* Everything else already written */
3648 txsd = &txq->sdesc[eq->pidx];
3649 txsd->desc_used = ndesc;
3651 KASSERT(eq->avail >= ndesc, ("%s: out of descriptors", __func__));
3653 eq->pending += ndesc;
3656 if (eq->pidx >= eq->cap)
3657 eq->pidx -= eq->cap;
3659 txq->txpkts_pkts += txpkts->npkt;
3661 txpkts->npkt = 0; /* emptied */
3665 write_ulp_cpl_sgl(struct port_info *pi, struct sge_txq *txq,
3666 struct txpkts *txpkts, struct mbuf *m, struct sgl *sgl)
3668 struct ulp_txpkt *ulpmc;
3669 struct ulptx_idata *ulpsc;
3670 struct cpl_tx_pkt_core *cpl;
3671 struct sge_eq *eq = &txq->eq;
3672 uintptr_t flitp, start, end;
3676 KASSERT(txpkts->npkt > 0, ("%s: txpkts is empty", __func__));
3678 start = (uintptr_t)eq->desc;
3679 end = (uintptr_t)eq->spg;
3681 /* Checksum offload */
3683 if (!(m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TSO)))
3684 ctrl |= F_TXPKT_IPCSUM_DIS;
3685 if (!(m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP | CSUM_UDP_IPV6 |
3686 CSUM_TCP_IPV6 | CSUM_TSO)))
3687 ctrl |= F_TXPKT_L4CSUM_DIS;
3688 if (m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP |
3689 CSUM_UDP_IPV6 | CSUM_TCP_IPV6 | CSUM_TSO))
3690 txq->txcsum++; /* some hardware assistance provided */
3692 /* VLAN tag insertion */
3693 if (m->m_flags & M_VLANTAG) {
3694 ctrl |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(m->m_pkthdr.ether_vtag);
3695 txq->vlan_insertion++;
3699 * The previous packet's SGL must have ended at a 16 byte boundary (this
3700 * is required by the firmware/hardware). It follows that flitp cannot
3701 * wrap around between the ULPTX master command and ULPTX subcommand (8
3702 * bytes each), and that it can not wrap around in the middle of the
3703 * cpl_tx_pkt_core either.
3705 flitp = (uintptr_t)txpkts->flitp;
3706 KASSERT((flitp & 0xf) == 0,
3707 ("%s: last SGL did not end at 16 byte boundary: %p",
3708 __func__, txpkts->flitp));
3710 /* ULP master command */
3711 ulpmc = (void *)flitp;
3712 ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0) |
3713 V_ULP_TXPKT_FID(eq->iqid));
3714 ulpmc->len = htonl(howmany(sizeof(*ulpmc) + sizeof(*ulpsc) +
3715 sizeof(*cpl) + 8 * sgl->nflits, 16));
3717 /* ULP subcommand */
3718 ulpsc = (void *)(ulpmc + 1);
3719 ulpsc->cmd_more = htobe32(V_ULPTX_CMD((u32)ULP_TX_SC_IMM) |
3721 ulpsc->len = htobe32(sizeof(struct cpl_tx_pkt_core));
3723 flitp += sizeof(*ulpmc) + sizeof(*ulpsc);
3728 cpl = (void *)flitp;
3729 cpl->ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT) |
3730 V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(pi->adapter->pf));
3732 cpl->len = htobe16(m->m_pkthdr.len);
3733 cpl->ctrl1 = htobe64(ctrl);
3735 flitp += sizeof(*cpl);
3739 /* SGL for this frame */
3740 dst = (caddr_t)flitp;
3741 txpkts->nflits += write_sgl_to_txd(eq, sgl, &dst);
3742 txpkts->flitp = (void *)dst;
3744 KASSERT(((uintptr_t)dst & 0xf) == 0,
3745 ("%s: SGL ends at %p (not a 16 byte boundary)", __func__, dst));
3749 * If the SGL ends on an address that is not 16 byte aligned, this function will
3750 * add a 0 filled flit at the end. It returns 1 in that case.
3753 write_sgl_to_txd(struct sge_eq *eq, struct sgl *sgl, caddr_t *to)
3755 __be64 *flitp, *end;
3756 struct ulptx_sgl *usgl;
3757 bus_dma_segment_t *seg;
3760 KASSERT(sgl->nsegs > 0 && sgl->nflits > 0,
3761 ("%s: bad SGL - nsegs=%d, nflits=%d",
3762 __func__, sgl->nsegs, sgl->nflits));
3764 KASSERT(((uintptr_t)(*to) & 0xf) == 0,
3765 ("%s: SGL must start at a 16 byte boundary: %p", __func__, *to));
3767 flitp = (__be64 *)(*to);
3768 end = flitp + sgl->nflits;
3770 usgl = (void *)flitp;
3773 * We start at a 16 byte boundary somewhere inside the tx descriptor
3774 * ring, so we're at least 16 bytes away from the status page. There is
3775 * no chance of a wrap around in the middle of usgl (which is 16 bytes).
3778 usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
3779 V_ULPTX_NSGE(sgl->nsegs));
3780 usgl->len0 = htobe32(seg->ds_len);
3781 usgl->addr0 = htobe64(seg->ds_addr);
3784 if ((uintptr_t)end <= (uintptr_t)eq->spg) {
3786 /* Won't wrap around at all */
3788 for (i = 0; i < sgl->nsegs - 1; i++, seg++) {
3789 usgl->sge[i / 2].len[i & 1] = htobe32(seg->ds_len);
3790 usgl->sge[i / 2].addr[i & 1] = htobe64(seg->ds_addr);
3793 usgl->sge[i / 2].len[1] = htobe32(0);
3796 /* Will wrap somewhere in the rest of the SGL */
3798 /* 2 flits already written, write the rest flit by flit */
3799 flitp = (void *)(usgl + 1);
3800 for (i = 0; i < sgl->nflits - 2; i++) {
3801 if ((uintptr_t)flitp == (uintptr_t)eq->spg)
3802 flitp = (void *)eq->desc;
3803 *flitp++ = get_flit(seg, sgl->nsegs - 1, i);
3808 if ((uintptr_t)end & 0xf) {
3809 *(uint64_t *)end = 0;
3815 if ((uintptr_t)end == (uintptr_t)eq->spg)
3816 *to = (void *)eq->desc;
3824 copy_to_txd(struct sge_eq *eq, caddr_t from, caddr_t *to, int len)
3826 if (__predict_true((uintptr_t)(*to) + len <= (uintptr_t)eq->spg)) {
3827 bcopy(from, *to, len);
3830 int portion = (uintptr_t)eq->spg - (uintptr_t)(*to);
3832 bcopy(from, *to, portion);
3834 portion = len - portion; /* remaining */
3835 bcopy(from, (void *)eq->desc, portion);
3836 (*to) = (caddr_t)eq->desc + portion;
3841 ring_eq_db(struct adapter *sc, struct sge_eq *eq)
3846 pending = eq->pending;
3848 clrbit(&db, DOORBELL_WCWR);
3852 switch (ffs(db) - 1) {
3854 *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(pending));
3857 case DOORBELL_WCWR: {
3858 volatile uint64_t *dst, *src;
3862 * Queues whose 128B doorbell segment fits in the page do not
3863 * use relative qid (udb_qid is always 0). Only queues with
3864 * doorbell segments can do WCWR.
3866 KASSERT(eq->udb_qid == 0 && pending == 1,
3867 ("%s: inappropriate doorbell (0x%x, %d, %d) for eq %p",
3868 __func__, eq->doorbells, pending, eq->pidx, eq));
3870 dst = (volatile void *)((uintptr_t)eq->udb + UDBS_WR_OFFSET -
3872 i = eq->pidx ? eq->pidx - 1 : eq->cap - 1;
3873 src = (void *)&eq->desc[i];
3874 while (src != (void *)&eq->desc[i + 1])
3880 case DOORBELL_UDBWC:
3881 *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(pending));
3886 t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL),
3887 V_QID(eq->cntxt_id) | V_PIDX(pending));
3893 reclaimable(struct sge_eq *eq)
3897 cidx = eq->spg->cidx; /* stable snapshot */
3898 cidx = be16toh(cidx);
3900 if (cidx >= eq->cidx)
3901 return (cidx - eq->cidx);
3903 return (cidx + eq->cap - eq->cidx);
3907 * There are "can_reclaim" tx descriptors ready to be reclaimed. Reclaim as
3908 * many as possible but stop when there are around "n" mbufs to free.
3910 * The actual number reclaimed is provided as the return value.
3913 reclaim_tx_descs(struct sge_txq *txq, int can_reclaim, int n)
3915 struct tx_sdesc *txsd;
3916 struct tx_maps *txmaps;
3918 unsigned int reclaimed, maps;
3919 struct sge_eq *eq = &txq->eq;
3921 TXQ_LOCK_ASSERT_OWNED(txq);
3923 if (can_reclaim == 0)
3924 can_reclaim = reclaimable(eq);
3926 maps = reclaimed = 0;
3927 while (can_reclaim && maps < n) {
3930 txsd = &txq->sdesc[eq->cidx];
3931 ndesc = txsd->desc_used;
3933 /* Firmware doesn't return "partial" credits. */
3934 KASSERT(can_reclaim >= ndesc,
3935 ("%s: unexpected number of credits: %d, %d",
3936 __func__, can_reclaim, ndesc));
3938 maps += txsd->credits;
3941 can_reclaim -= ndesc;
3944 if (__predict_false(eq->cidx >= eq->cap))
3945 eq->cidx -= eq->cap;
3948 txmaps = &txq->txmaps;
3949 txm = &txmaps->maps[txmaps->map_cidx];
3953 eq->avail += reclaimed;
3954 KASSERT(eq->avail < eq->cap, /* avail tops out at (cap - 1) */
3955 ("%s: too many descriptors available", __func__));
3957 txmaps->map_avail += maps;
3958 KASSERT(txmaps->map_avail <= txmaps->map_total,
3959 ("%s: too many maps available", __func__));
3962 struct tx_map *next;
3965 if (__predict_false(txmaps->map_cidx + 1 == txmaps->map_total))
3966 next = txmaps->maps;
3969 bus_dmamap_unload(txq->tx_tag, txm->map);
3974 if (__predict_false(++txmaps->map_cidx == txmaps->map_total))
3975 txmaps->map_cidx = 0;
3982 write_eqflush_wr(struct sge_eq *eq)
3984 struct fw_eq_flush_wr *wr;
3986 EQ_LOCK_ASSERT_OWNED(eq);
3987 KASSERT(eq->avail > 0, ("%s: no descriptors left.", __func__));
3988 KASSERT(!(eq->flags & EQ_CRFLUSHED), ("%s: flushed already", __func__));
3990 wr = (void *)&eq->desc[eq->pidx];
3991 bzero(wr, sizeof(*wr));
3992 wr->opcode = FW_EQ_FLUSH_WR;
3993 wr->equiq_to_len16 = htobe32(V_FW_WR_LEN16(sizeof(*wr) / 16) |
3994 F_FW_WR_EQUEQ | F_FW_WR_EQUIQ);
3996 eq->flags |= (EQ_CRFLUSHED | EQ_STALLED);
3999 if (++eq->pidx == eq->cap)
4004 get_flit(bus_dma_segment_t *sgl, int nsegs, int idx)
4006 int i = (idx / 3) * 2;
4012 rc = htobe32(sgl[i].ds_len);
4014 rc |= (uint64_t)htobe32(sgl[i + 1].ds_len) << 32;
4019 return htobe64(sgl[i].ds_addr);
4021 return htobe64(sgl[i + 1].ds_addr);
4028 find_best_refill_source(struct adapter *sc, struct sge_fl *fl, int maxp)
4030 int8_t zidx, hwidx, idx;
4031 uint16_t region1, region3;
4032 int spare, spare_needed, n;
4033 struct sw_zone_info *swz;
4034 struct hw_buf_info *hwb, *hwb_list = &sc->sge.hw_buf_info[0];
4037 * Buffer Packing: Look for PAGE_SIZE or larger zone which has a bufsize
4038 * large enough for the max payload and cluster metadata. Otherwise
4039 * settle for the largest bufsize that leaves enough room in the cluster
4042 * Without buffer packing: Look for the smallest zone which has a
4043 * bufsize large enough for the max payload. Settle for the largest
4044 * bufsize available if there's nothing big enough for max payload.
4046 spare_needed = fl->flags & FL_BUF_PACKING ? CL_METADATA_SIZE : 0;
4047 swz = &sc->sge.sw_zone_info[0];
4049 for (zidx = 0; zidx < SW_ZONE_SIZES; zidx++, swz++) {
4050 if (swz->size > largest_rx_cluster) {
4051 if (__predict_true(hwidx != -1))
4055 * This is a misconfiguration. largest_rx_cluster is
4056 * preventing us from finding a refill source. See
4057 * dev.t5nex.<n>.buffer_sizes to figure out why.
4059 device_printf(sc->dev, "largest_rx_cluster=%u leaves no"
4060 " refill source for fl %p (dma %u). Ignored.\n",
4061 largest_rx_cluster, fl, maxp);
4063 for (idx = swz->head_hwidx; idx != -1; idx = hwb->next) {
4064 hwb = &hwb_list[idx];
4065 spare = swz->size - hwb->size;
4066 if (spare < spare_needed)
4069 hwidx = idx; /* best option so far */
4070 if (hwb->size >= maxp) {
4072 if ((fl->flags & FL_BUF_PACKING) == 0)
4073 goto done; /* stop looking (not packing) */
4075 if (swz->size >= safest_rx_cluster)
4076 goto done; /* stop looking (packing) */
4078 break; /* keep looking, next zone */
4082 /* A usable hwidx has been located. */
4084 hwb = &hwb_list[hwidx];
4086 swz = &sc->sge.sw_zone_info[zidx];
4088 region3 = swz->size - hwb->size;
4091 * Stay within this zone and see if there is a better match when mbuf
4092 * inlining is allowed. Remember that the hwidx's are sorted in
4093 * decreasing order of size (so in increasing order of spare area).
4095 for (idx = hwidx; idx != -1; idx = hwb->next) {
4096 hwb = &hwb_list[idx];
4097 spare = swz->size - hwb->size;
4099 if (allow_mbufs_in_cluster == 0 || hwb->size < maxp)
4101 if (spare < CL_METADATA_SIZE + MSIZE)
4103 n = (spare - CL_METADATA_SIZE) / MSIZE;
4104 if (n > howmany(hwb->size, maxp))
4108 if (fl->flags & FL_BUF_PACKING) {
4109 region1 = n * MSIZE;
4110 region3 = spare - region1;
4113 region3 = spare - region1;
4118 KASSERT(zidx >= 0 && zidx < SW_ZONE_SIZES,
4119 ("%s: bad zone %d for fl %p, maxp %d", __func__, zidx, fl, maxp));
4120 KASSERT(hwidx >= 0 && hwidx <= SGE_FLBUF_SIZES,
4121 ("%s: bad hwidx %d for fl %p, maxp %d", __func__, hwidx, fl, maxp));
4122 KASSERT(region1 + sc->sge.hw_buf_info[hwidx].size + region3 ==
4123 sc->sge.sw_zone_info[zidx].size,
4124 ("%s: bad buffer layout for fl %p, maxp %d. "
4125 "cl %d; r1 %d, payload %d, r3 %d", __func__, fl, maxp,
4126 sc->sge.sw_zone_info[zidx].size, region1,
4127 sc->sge.hw_buf_info[hwidx].size, region3));
4128 if (fl->flags & FL_BUF_PACKING || region1 > 0) {
4129 KASSERT(region3 >= CL_METADATA_SIZE,
4130 ("%s: no room for metadata. fl %p, maxp %d; "
4131 "cl %d; r1 %d, payload %d, r3 %d", __func__, fl, maxp,
4132 sc->sge.sw_zone_info[zidx].size, region1,
4133 sc->sge.hw_buf_info[hwidx].size, region3));
4134 KASSERT(region1 % MSIZE == 0,
4135 ("%s: bad mbuf region for fl %p, maxp %d. "
4136 "cl %d; r1 %d, payload %d, r3 %d", __func__, fl, maxp,
4137 sc->sge.sw_zone_info[zidx].size, region1,
4138 sc->sge.hw_buf_info[hwidx].size, region3));
4141 fl->cll_def.zidx = zidx;
4142 fl->cll_def.hwidx = hwidx;
4143 fl->cll_def.region1 = region1;
4144 fl->cll_def.region3 = region3;
4148 find_safe_refill_source(struct adapter *sc, struct sge_fl *fl)
4150 struct sge *s = &sc->sge;
4151 struct hw_buf_info *hwb;
4152 struct sw_zone_info *swz;
4156 if (fl->flags & FL_BUF_PACKING)
4157 hwidx = s->safe_hwidx2; /* with room for metadata */
4158 else if (allow_mbufs_in_cluster && s->safe_hwidx2 != -1) {
4159 hwidx = s->safe_hwidx2;
4160 hwb = &s->hw_buf_info[hwidx];
4161 swz = &s->sw_zone_info[hwb->zidx];
4162 spare = swz->size - hwb->size;
4164 /* no good if there isn't room for an mbuf as well */
4165 if (spare < CL_METADATA_SIZE + MSIZE)
4166 hwidx = s->safe_hwidx1;
4168 hwidx = s->safe_hwidx1;
4171 /* No fallback source */
4172 fl->cll_alt.hwidx = -1;
4173 fl->cll_alt.zidx = -1;
4178 hwb = &s->hw_buf_info[hwidx];
4179 swz = &s->sw_zone_info[hwb->zidx];
4180 spare = swz->size - hwb->size;
4181 fl->cll_alt.hwidx = hwidx;
4182 fl->cll_alt.zidx = hwb->zidx;
4183 if (allow_mbufs_in_cluster)
4184 fl->cll_alt.region1 = ((spare - CL_METADATA_SIZE) / MSIZE) * MSIZE;
4186 fl->cll_alt.region1 = 0;
4187 fl->cll_alt.region3 = spare - fl->cll_alt.region1;
4191 add_fl_to_sfl(struct adapter *sc, struct sge_fl *fl)
4193 mtx_lock(&sc->sfl_lock);
4195 if ((fl->flags & FL_DOOMED) == 0) {
4196 fl->flags |= FL_STARVING;
4197 TAILQ_INSERT_TAIL(&sc->sfl, fl, link);
4198 callout_reset(&sc->sfl_callout, hz / 5, refill_sfl, sc);
4201 mtx_unlock(&sc->sfl_lock);
4205 handle_sge_egr_update(struct sge_iq *iq, const struct rss_header *rss,
4208 const struct cpl_sge_egr_update *cpl = (const void *)(rss + 1);
4209 unsigned int qid = G_EGR_QID(ntohl(cpl->opcode_qid));
4210 struct adapter *sc = iq->adapter;
4211 struct sge *s = &sc->sge;
4214 KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__,
4217 eq = s->eqmap[qid - s->eq_start];
4219 KASSERT(eq->flags & EQ_CRFLUSHED,
4220 ("%s: unsolicited egress update", __func__));
4221 eq->flags &= ~EQ_CRFLUSHED;
4224 if (__predict_false(eq->flags & EQ_DOOMED))
4226 else if (eq->flags & EQ_STALLED && can_resume_tx(eq))
4227 taskqueue_enqueue(sc->tq[eq->tx_chan], &eq->tx_task);
4233 /* handle_fw_msg works for both fw4_msg and fw6_msg because this is valid */
4234 CTASSERT(offsetof(struct cpl_fw4_msg, data) == \
4235 offsetof(struct cpl_fw6_msg, data));
4238 handle_fw_msg(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
4240 struct adapter *sc = iq->adapter;
4241 const struct cpl_fw6_msg *cpl = (const void *)(rss + 1);
4243 KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__,
4246 if (cpl->type == FW_TYPE_RSSCPL || cpl->type == FW6_TYPE_RSSCPL) {
4247 const struct rss_header *rss2;
4249 rss2 = (const struct rss_header *)&cpl->data[0];
4250 return (sc->cpl_handler[rss2->opcode](iq, rss2, m));
4253 return (sc->fw_msg_handler[cpl->type](sc, &cpl->data[0]));
4257 sysctl_uint16(SYSCTL_HANDLER_ARGS)
4259 uint16_t *id = arg1;
4262 return sysctl_handle_int(oidp, &i, 0, req);
4266 sysctl_bufsizes(SYSCTL_HANDLER_ARGS)
4268 struct sge *s = arg1;
4269 struct hw_buf_info *hwb = &s->hw_buf_info[0];
4270 struct sw_zone_info *swz = &s->sw_zone_info[0];
4275 sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
4276 for (i = 0; i < SGE_FLBUF_SIZES; i++, hwb++) {
4277 if (hwb->zidx >= 0 && swz[hwb->zidx].size <= largest_rx_cluster)
4282 sbuf_printf(&sb, "%u%c ", hwb->size, c);
4286 rc = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);