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>
41 #include <sys/taskqueue.h>
42 #include <sys/sysctl.h>
45 #include <net/ethernet.h>
47 #include <net/if_vlan_var.h>
48 #include <netinet/in.h>
49 #include <netinet/ip.h>
50 #include <netinet/ip6.h>
51 #include <netinet/tcp.h>
53 #include "common/common.h"
54 #include "common/t4_regs.h"
55 #include "common/t4_regs_values.h"
56 #include "common/t4_msg.h"
64 /* Filled up by t4_sge_modload */
65 static struct fl_buf_info fl_buf_info[FL_BUF_SIZES];
67 #define FL_BUF_SIZE(x) (fl_buf_info[x].size)
68 #define FL_BUF_TYPE(x) (fl_buf_info[x].type)
69 #define FL_BUF_ZONE(x) (fl_buf_info[x].zone)
71 #ifdef T4_PKT_TIMESTAMP
72 #define RX_COPY_THRESHOLD (MINCLSIZE - 8)
74 #define RX_COPY_THRESHOLD MINCLSIZE
78 * Ethernet frames are DMA'd at this byte offset into the freelist buffer.
79 * 0-7 are valid values.
81 static int fl_pktshift = 2;
82 TUNABLE_INT("hw.cxgbe.fl_pktshift", &fl_pktshift);
85 * Pad ethernet payload up to this boundary.
86 * -1: driver should figure out a good value.
87 * Any power of 2, from 32 to 4096 (both inclusive) is a valid value.
89 static int fl_pad = -1;
90 TUNABLE_INT("hw.cxgbe.fl_pad", &fl_pad);
94 * -1: driver should figure out a good value.
95 * 64 or 128 are the only other valid values.
97 static int spg_len = -1;
98 TUNABLE_INT("hw.cxgbe.spg_len", &spg_len);
102 * -1: no congestion feedback (not recommended).
103 * 0: backpressure the channel instead of dropping packets right away.
104 * 1: no backpressure, drop packets for the congested queue immediately.
106 static int cong_drop = 0;
107 TUNABLE_INT("hw.cxgbe.cong_drop", &cong_drop);
109 /* Used to track coalesced tx work request */
111 uint64_t *flitp; /* ptr to flit where next pkt should start */
112 uint8_t npkt; /* # of packets in this work request */
113 uint8_t nflits; /* # of flits used by this work request */
114 uint16_t plen; /* total payload (sum of all packets) */
117 /* A packet's SGL. This + m_pkthdr has all info needed for tx */
119 int nsegs; /* # of segments in the SGL, 0 means imm. tx */
120 int nflits; /* # of flits needed for the SGL */
121 bus_dma_segment_t seg[TX_SGL_SEGS];
124 static int service_iq(struct sge_iq *, int);
125 static struct mbuf *get_fl_payload(struct adapter *, struct sge_fl *, uint32_t,
127 static int t4_eth_rx(struct sge_iq *, const struct rss_header *, struct mbuf *);
128 static inline void init_iq(struct sge_iq *, struct adapter *, int, int, int,
130 static inline void init_fl(struct sge_fl *, int, int, char *);
131 static inline void init_eq(struct sge_eq *, int, int, uint8_t, uint16_t,
133 static int alloc_ring(struct adapter *, size_t, bus_dma_tag_t *, bus_dmamap_t *,
134 bus_addr_t *, void **);
135 static int free_ring(struct adapter *, bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
137 static int alloc_iq_fl(struct port_info *, struct sge_iq *, struct sge_fl *,
139 static int free_iq_fl(struct port_info *, struct sge_iq *, struct sge_fl *);
140 static int alloc_fwq(struct adapter *);
141 static int free_fwq(struct adapter *);
142 static int alloc_mgmtq(struct adapter *);
143 static int free_mgmtq(struct adapter *);
144 static int alloc_rxq(struct port_info *, struct sge_rxq *, int, int,
145 struct sysctl_oid *);
146 static int free_rxq(struct port_info *, struct sge_rxq *);
148 static int alloc_ofld_rxq(struct port_info *, struct sge_ofld_rxq *, int, int,
149 struct sysctl_oid *);
150 static int free_ofld_rxq(struct port_info *, struct sge_ofld_rxq *);
152 static int ctrl_eq_alloc(struct adapter *, struct sge_eq *);
153 static int eth_eq_alloc(struct adapter *, struct port_info *, struct sge_eq *);
155 static int ofld_eq_alloc(struct adapter *, struct port_info *, struct sge_eq *);
157 static int alloc_eq(struct adapter *, struct port_info *, struct sge_eq *);
158 static int free_eq(struct adapter *, struct sge_eq *);
159 static int alloc_wrq(struct adapter *, struct port_info *, struct sge_wrq *,
160 struct sysctl_oid *);
161 static int free_wrq(struct adapter *, struct sge_wrq *);
162 static int alloc_txq(struct port_info *, struct sge_txq *, int,
163 struct sysctl_oid *);
164 static int free_txq(struct port_info *, struct sge_txq *);
165 static void oneseg_dma_callback(void *, bus_dma_segment_t *, int, int);
166 static inline bool is_new_response(const struct sge_iq *, struct rsp_ctrl **);
167 static inline void iq_next(struct sge_iq *);
168 static inline void ring_fl_db(struct adapter *, struct sge_fl *);
169 static int refill_fl(struct adapter *, struct sge_fl *, int);
170 static void refill_sfl(void *);
171 static int alloc_fl_sdesc(struct sge_fl *);
172 static void free_fl_sdesc(struct sge_fl *);
173 static void set_fl_tag_idx(struct sge_fl *, int);
174 static void add_fl_to_sfl(struct adapter *, struct sge_fl *);
176 static int get_pkt_sgl(struct sge_txq *, struct mbuf **, struct sgl *, int);
177 static int free_pkt_sgl(struct sge_txq *, struct sgl *);
178 static int write_txpkt_wr(struct port_info *, struct sge_txq *, struct mbuf *,
180 static int add_to_txpkts(struct port_info *, struct sge_txq *, struct txpkts *,
181 struct mbuf *, struct sgl *);
182 static void write_txpkts_wr(struct sge_txq *, struct txpkts *);
183 static inline void write_ulp_cpl_sgl(struct port_info *, struct sge_txq *,
184 struct txpkts *, struct mbuf *, struct sgl *);
185 static int write_sgl_to_txd(struct sge_eq *, struct sgl *, caddr_t *);
186 static inline void copy_to_txd(struct sge_eq *, caddr_t, caddr_t *, int);
187 static inline void ring_eq_db(struct adapter *, struct sge_eq *);
188 static inline int reclaimable(struct sge_eq *);
189 static int reclaim_tx_descs(struct sge_txq *, int, int);
190 static void write_eqflush_wr(struct sge_eq *);
191 static __be64 get_flit(bus_dma_segment_t *, int, int);
192 static int handle_sge_egr_update(struct sge_iq *, const struct rss_header *,
194 static int handle_fw_msg(struct sge_iq *, const struct rss_header *,
197 static int sysctl_uint16(SYSCTL_HANDLER_ARGS);
199 #if defined(__i386__) || defined(__amd64__)
200 extern u_int cpu_clflush_line_size;
204 * Called on MOD_LOAD. Fills up fl_buf_info[] and validates/calculates the SGE
211 int bufsize[FL_BUF_SIZES] = {
213 #if MJUMPAGESIZE != MCLBYTES
220 for (i = 0; i < FL_BUF_SIZES; i++) {
221 FL_BUF_SIZE(i) = bufsize[i];
222 FL_BUF_TYPE(i) = m_gettype(bufsize[i]);
223 FL_BUF_ZONE(i) = m_getzone(bufsize[i]);
226 if (fl_pktshift < 0 || fl_pktshift > 7) {
227 printf("Invalid hw.cxgbe.fl_pktshift value (%d),"
228 " using 2 instead.\n", fl_pktshift);
232 if (fl_pad < 32 || fl_pad > 4096 || !powerof2(fl_pad)) {
235 #if defined(__i386__) || defined(__amd64__)
236 pad = max(cpu_clflush_line_size, 32);
238 pad = max(CACHE_LINE_SIZE, 32);
240 pad = min(pad, 4096);
243 printf("Invalid hw.cxgbe.fl_pad value (%d),"
244 " using %d instead.\n", fl_pad, pad);
249 if (spg_len != 64 && spg_len != 128) {
252 #if defined(__i386__) || defined(__amd64__)
253 len = cpu_clflush_line_size > 64 ? 128 : 64;
258 printf("Invalid hw.cxgbe.spg_len value (%d),"
259 " using %d instead.\n", spg_len, len);
264 if (cong_drop < -1 || cong_drop > 1) {
265 printf("Invalid hw.cxgbe.cong_drop value (%d),"
266 " using 0 instead.\n", cong_drop);
272 t4_init_sge_cpl_handlers(struct adapter *sc)
275 t4_register_cpl_handler(sc, CPL_FW4_MSG, handle_fw_msg);
276 t4_register_cpl_handler(sc, CPL_FW6_MSG, handle_fw_msg);
277 t4_register_cpl_handler(sc, CPL_SGE_EGR_UPDATE, handle_sge_egr_update);
278 t4_register_cpl_handler(sc, CPL_RX_PKT, t4_eth_rx);
280 t4_register_fw_msg_handler(sc, FW6_TYPE_CMD_RPL, t4_handle_fw_rpl);
284 * adap->params.vpd.cclk must be set up before this is called.
287 t4_tweak_chip_settings(struct adapter *sc)
291 int intr_timer[SGE_NTIMERS] = {1, 5, 10, 50, 100, 200};
292 int timer_max = M_TIMERVALUE0 * 1000 / sc->params.vpd.cclk;
293 int intr_pktcount[SGE_NCOUNTERS] = {1, 8, 16, 32}; /* 63 max */
294 uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE);
296 KASSERT(sc->flags & MASTER_PF,
297 ("%s: trying to change chip settings when not master.", __func__));
299 m = V_PKTSHIFT(M_PKTSHIFT) | F_RXPKTCPLMODE |
300 V_INGPADBOUNDARY(M_INGPADBOUNDARY) | F_EGRSTATUSPAGESIZE;
301 v = V_PKTSHIFT(fl_pktshift) | F_RXPKTCPLMODE |
302 V_INGPADBOUNDARY(ilog2(fl_pad) - 5) |
303 V_EGRSTATUSPAGESIZE(spg_len == 128);
304 t4_set_reg_field(sc, A_SGE_CONTROL, m, v);
306 v = V_HOSTPAGESIZEPF0(PAGE_SHIFT - 10) |
307 V_HOSTPAGESIZEPF1(PAGE_SHIFT - 10) |
308 V_HOSTPAGESIZEPF2(PAGE_SHIFT - 10) |
309 V_HOSTPAGESIZEPF3(PAGE_SHIFT - 10) |
310 V_HOSTPAGESIZEPF4(PAGE_SHIFT - 10) |
311 V_HOSTPAGESIZEPF5(PAGE_SHIFT - 10) |
312 V_HOSTPAGESIZEPF6(PAGE_SHIFT - 10) |
313 V_HOSTPAGESIZEPF7(PAGE_SHIFT - 10);
314 t4_write_reg(sc, A_SGE_HOST_PAGE_SIZE, v);
316 for (i = 0; i < FL_BUF_SIZES; i++) {
317 t4_write_reg(sc, A_SGE_FL_BUFFER_SIZE0 + (4 * i),
321 v = V_THRESHOLD_0(intr_pktcount[0]) | V_THRESHOLD_1(intr_pktcount[1]) |
322 V_THRESHOLD_2(intr_pktcount[2]) | V_THRESHOLD_3(intr_pktcount[3]);
323 t4_write_reg(sc, A_SGE_INGRESS_RX_THRESHOLD, v);
325 KASSERT(intr_timer[0] <= timer_max,
326 ("%s: not a single usable timer (%d, %d)", __func__, intr_timer[0],
328 for (i = 1; i < nitems(intr_timer); i++) {
329 KASSERT(intr_timer[i] >= intr_timer[i - 1],
330 ("%s: timers not listed in increasing order (%d)",
333 while (intr_timer[i] > timer_max) {
334 if (i == nitems(intr_timer) - 1) {
335 intr_timer[i] = timer_max;
338 intr_timer[i] += intr_timer[i - 1];
343 v = V_TIMERVALUE0(us_to_core_ticks(sc, intr_timer[0])) |
344 V_TIMERVALUE1(us_to_core_ticks(sc, intr_timer[1]));
345 t4_write_reg(sc, A_SGE_TIMER_VALUE_0_AND_1, v);
346 v = V_TIMERVALUE2(us_to_core_ticks(sc, intr_timer[2])) |
347 V_TIMERVALUE3(us_to_core_ticks(sc, intr_timer[3]));
348 t4_write_reg(sc, A_SGE_TIMER_VALUE_2_AND_3, v);
349 v = V_TIMERVALUE4(us_to_core_ticks(sc, intr_timer[4])) |
350 V_TIMERVALUE5(us_to_core_ticks(sc, intr_timer[5]));
351 t4_write_reg(sc, A_SGE_TIMER_VALUE_4_AND_5, v);
353 if (cong_drop == 0) {
354 m = F_TUNNELCNGDROP0 | F_TUNNELCNGDROP1 | F_TUNNELCNGDROP2 |
356 t4_set_reg_field(sc, A_TP_PARA_REG3, m, 0);
359 /* 4K, 16K, 64K, 256K DDP "page sizes" */
360 v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6);
361 t4_write_reg(sc, A_ULP_RX_TDDP_PSZ, v);
363 m = v = F_TDDPTAGTCB;
364 t4_set_reg_field(sc, A_ULP_RX_CTL, m, v);
366 m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET |
368 v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET;
369 t4_set_reg_field(sc, A_TP_PARA_REG5, m, v);
373 * XXX: driver really should be able to deal with unexpected settings.
376 t4_read_chip_settings(struct adapter *sc)
378 struct sge *s = &sc->sge;
381 uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE);
383 m = V_PKTSHIFT(M_PKTSHIFT) | F_RXPKTCPLMODE |
384 V_INGPADBOUNDARY(M_INGPADBOUNDARY) | F_EGRSTATUSPAGESIZE;
385 v = V_PKTSHIFT(fl_pktshift) | F_RXPKTCPLMODE |
386 V_INGPADBOUNDARY(ilog2(fl_pad) - 5) |
387 V_EGRSTATUSPAGESIZE(spg_len == 128);
388 r = t4_read_reg(sc, A_SGE_CONTROL);
390 device_printf(sc->dev, "invalid SGE_CONTROL(0x%x)\n", r);
394 v = V_HOSTPAGESIZEPF0(PAGE_SHIFT - 10) |
395 V_HOSTPAGESIZEPF1(PAGE_SHIFT - 10) |
396 V_HOSTPAGESIZEPF2(PAGE_SHIFT - 10) |
397 V_HOSTPAGESIZEPF3(PAGE_SHIFT - 10) |
398 V_HOSTPAGESIZEPF4(PAGE_SHIFT - 10) |
399 V_HOSTPAGESIZEPF5(PAGE_SHIFT - 10) |
400 V_HOSTPAGESIZEPF6(PAGE_SHIFT - 10) |
401 V_HOSTPAGESIZEPF7(PAGE_SHIFT - 10);
402 r = t4_read_reg(sc, A_SGE_HOST_PAGE_SIZE);
404 device_printf(sc->dev, "invalid SGE_HOST_PAGE_SIZE(0x%x)\n", r);
408 for (i = 0; i < FL_BUF_SIZES; i++) {
409 v = t4_read_reg(sc, A_SGE_FL_BUFFER_SIZE0 + (4 * i));
410 if (v != FL_BUF_SIZE(i)) {
411 device_printf(sc->dev,
412 "invalid SGE_FL_BUFFER_SIZE[%d](0x%x)\n", i, v);
417 r = t4_read_reg(sc, A_SGE_INGRESS_RX_THRESHOLD);
418 s->counter_val[0] = G_THRESHOLD_0(r);
419 s->counter_val[1] = G_THRESHOLD_1(r);
420 s->counter_val[2] = G_THRESHOLD_2(r);
421 s->counter_val[3] = G_THRESHOLD_3(r);
423 r = t4_read_reg(sc, A_SGE_TIMER_VALUE_0_AND_1);
424 s->timer_val[0] = G_TIMERVALUE0(r) / core_ticks_per_usec(sc);
425 s->timer_val[1] = G_TIMERVALUE1(r) / core_ticks_per_usec(sc);
426 r = t4_read_reg(sc, A_SGE_TIMER_VALUE_2_AND_3);
427 s->timer_val[2] = G_TIMERVALUE2(r) / core_ticks_per_usec(sc);
428 s->timer_val[3] = G_TIMERVALUE3(r) / core_ticks_per_usec(sc);
429 r = t4_read_reg(sc, A_SGE_TIMER_VALUE_4_AND_5);
430 s->timer_val[4] = G_TIMERVALUE4(r) / core_ticks_per_usec(sc);
431 s->timer_val[5] = G_TIMERVALUE5(r) / core_ticks_per_usec(sc);
433 if (cong_drop == 0) {
434 m = F_TUNNELCNGDROP0 | F_TUNNELCNGDROP1 | F_TUNNELCNGDROP2 |
436 r = t4_read_reg(sc, A_TP_PARA_REG3);
438 device_printf(sc->dev,
439 "invalid TP_PARA_REG3(0x%x)\n", r);
444 v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6);
445 r = t4_read_reg(sc, A_ULP_RX_TDDP_PSZ);
447 device_printf(sc->dev, "invalid ULP_RX_TDDP_PSZ(0x%x)\n", r);
451 m = v = F_TDDPTAGTCB;
452 r = t4_read_reg(sc, A_ULP_RX_CTL);
454 device_printf(sc->dev, "invalid ULP_RX_CTL(0x%x)\n", r);
458 m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET |
460 v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET;
461 r = t4_read_reg(sc, A_TP_PARA_REG5);
463 device_printf(sc->dev, "invalid TP_PARA_REG5(0x%x)\n", r);
467 r = t4_read_reg(sc, A_SGE_CONM_CTRL);
468 s->fl_starve_threshold = G_EGRTHRESHOLD(r) * 2 + 1;
471 r = t4_read_reg(sc, A_SGE_EGRESS_QUEUES_PER_PAGE_PF);
472 r >>= S_QUEUESPERPAGEPF0 +
473 (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) * sc->pf;
474 s->s_qpp = r & M_QUEUESPERPAGEPF0;
477 t4_init_tp_params(sc);
479 t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
480 t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
486 t4_create_dma_tag(struct adapter *sc)
490 rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
491 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE,
492 BUS_SPACE_UNRESTRICTED, BUS_SPACE_MAXSIZE, BUS_DMA_ALLOCNOW, NULL,
495 device_printf(sc->dev,
496 "failed to create main DMA tag: %d\n", rc);
503 t4_destroy_dma_tag(struct adapter *sc)
506 bus_dma_tag_destroy(sc->dmat);
512 * Allocate and initialize the firmware event queue and the management queue.
514 * Returns errno on failure. Resources allocated up to that point may still be
515 * allocated. Caller is responsible for cleanup in case this function fails.
518 t4_setup_adapter_queues(struct adapter *sc)
522 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
524 sysctl_ctx_init(&sc->ctx);
525 sc->flags |= ADAP_SYSCTL_CTX;
528 * Firmware event queue
535 * Management queue. This is just a control queue that uses the fwq as
538 rc = alloc_mgmtq(sc);
547 t4_teardown_adapter_queues(struct adapter *sc)
550 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
552 /* Do this before freeing the queue */
553 if (sc->flags & ADAP_SYSCTL_CTX) {
554 sysctl_ctx_free(&sc->ctx);
555 sc->flags &= ~ADAP_SYSCTL_CTX;
565 first_vector(struct port_info *pi)
567 struct adapter *sc = pi->adapter;
568 int rc = T4_EXTRA_INTR, i;
570 if (sc->intr_count == 1)
573 for_each_port(sc, i) {
574 struct port_info *p = sc->port[i];
576 if (i == pi->port_id)
580 if (sc->flags & INTR_DIRECT)
581 rc += p->nrxq + p->nofldrxq;
583 rc += max(p->nrxq, p->nofldrxq);
586 * Not compiled with offload support and intr_count > 1. Only
587 * NIC queues exist and they'd better be taking direct
590 KASSERT(sc->flags & INTR_DIRECT,
591 ("%s: intr_count %d, !INTR_DIRECT", __func__,
602 * Given an arbitrary "index," come up with an iq that can be used by other
603 * queues (of this port) for interrupt forwarding, SGE egress updates, etc.
604 * The iq returned is guaranteed to be something that takes direct interrupts.
606 static struct sge_iq *
607 port_intr_iq(struct port_info *pi, int idx)
609 struct adapter *sc = pi->adapter;
610 struct sge *s = &sc->sge;
611 struct sge_iq *iq = NULL;
613 if (sc->intr_count == 1)
614 return (&sc->sge.fwq);
617 if (sc->flags & INTR_DIRECT) {
618 idx %= pi->nrxq + pi->nofldrxq;
620 if (idx >= pi->nrxq) {
622 iq = &s->ofld_rxq[pi->first_ofld_rxq + idx].iq;
624 iq = &s->rxq[pi->first_rxq + idx].iq;
627 idx %= max(pi->nrxq, pi->nofldrxq);
629 if (pi->nrxq >= pi->nofldrxq)
630 iq = &s->rxq[pi->first_rxq + idx].iq;
632 iq = &s->ofld_rxq[pi->first_ofld_rxq + idx].iq;
636 * Not compiled with offload support and intr_count > 1. Only NIC
637 * queues exist and they'd better be taking direct interrupts.
639 KASSERT(sc->flags & INTR_DIRECT,
640 ("%s: intr_count %d, !INTR_DIRECT", __func__, sc->intr_count));
643 iq = &s->rxq[pi->first_rxq + idx].iq;
646 KASSERT(iq->flags & IQ_INTR, ("%s: EDOOFUS", __func__));
651 mtu_to_bufsize(int mtu)
655 /* large enough for a frame even when VLAN extraction is disabled */
656 bufsize = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + mtu;
657 bufsize = roundup2(bufsize + fl_pktshift, fl_pad);
664 mtu_to_bufsize_toe(struct adapter *sc, int mtu)
667 if (sc->tt.rx_coalesce)
668 return (G_RXCOALESCESIZE(t4_read_reg(sc, A_TP_PARA_REG2)));
675 t4_setup_port_queues(struct port_info *pi)
677 int rc = 0, i, j, intr_idx, iqid;
680 struct sge_wrq *ctrlq;
682 struct sge_ofld_rxq *ofld_rxq;
683 struct sge_wrq *ofld_txq;
684 struct sysctl_oid *oid2 = NULL;
687 struct adapter *sc = pi->adapter;
688 struct ifnet *ifp = pi->ifp;
689 struct sysctl_oid *oid = device_get_sysctl_tree(pi->dev);
690 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
693 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "rxq", CTLFLAG_RD,
697 if (is_offload(sc)) {
698 oid2 = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ofld_rxq",
700 "rx queues for offloaded TCP connections");
704 /* Interrupt vector to start from (when using multiple vectors) */
705 intr_idx = first_vector(pi);
708 * First pass over all rx queues (NIC and TOE):
709 * a) initialize iq and fl
710 * b) allocate queue iff it will take direct interrupts.
712 bufsize = mtu_to_bufsize(ifp->if_mtu);
713 for_each_rxq(pi, i, rxq) {
715 init_iq(&rxq->iq, sc, pi->tmr_idx, pi->pktc_idx, pi->qsize_rxq,
718 snprintf(name, sizeof(name), "%s rxq%d-fl",
719 device_get_nameunit(pi->dev), i);
720 init_fl(&rxq->fl, pi->qsize_rxq / 8, bufsize, name);
722 if (sc->flags & INTR_DIRECT
724 || (sc->intr_count > 1 && pi->nrxq >= pi->nofldrxq)
727 rxq->iq.flags |= IQ_INTR;
728 rc = alloc_rxq(pi, rxq, intr_idx, i, oid);
736 bufsize = mtu_to_bufsize_toe(sc, ifp->if_mtu);
737 for_each_ofld_rxq(pi, i, ofld_rxq) {
739 init_iq(&ofld_rxq->iq, sc, pi->tmr_idx, pi->pktc_idx,
740 pi->qsize_rxq, RX_IQ_ESIZE);
742 snprintf(name, sizeof(name), "%s ofld_rxq%d-fl",
743 device_get_nameunit(pi->dev), i);
744 init_fl(&ofld_rxq->fl, pi->qsize_rxq / 8, bufsize, name);
746 if (sc->flags & INTR_DIRECT ||
747 (sc->intr_count > 1 && pi->nofldrxq > pi->nrxq)) {
748 ofld_rxq->iq.flags |= IQ_INTR;
749 rc = alloc_ofld_rxq(pi, ofld_rxq, intr_idx, i, oid2);
758 * Second pass over all rx queues (NIC and TOE). The queues forwarding
759 * their interrupts are allocated now.
762 for_each_rxq(pi, i, rxq) {
763 if (rxq->iq.flags & IQ_INTR)
766 intr_idx = port_intr_iq(pi, j)->abs_id;
768 rc = alloc_rxq(pi, rxq, intr_idx, i, oid);
775 for_each_ofld_rxq(pi, i, ofld_rxq) {
776 if (ofld_rxq->iq.flags & IQ_INTR)
779 intr_idx = port_intr_iq(pi, j)->abs_id;
781 rc = alloc_ofld_rxq(pi, ofld_rxq, intr_idx, i, oid2);
789 * Now the tx queues. Only one pass needed.
791 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "txq", CTLFLAG_RD,
794 for_each_txq(pi, i, txq) {
797 iqid = port_intr_iq(pi, j)->cntxt_id;
799 snprintf(name, sizeof(name), "%s txq%d",
800 device_get_nameunit(pi->dev), i);
801 init_eq(&txq->eq, EQ_ETH, pi->qsize_txq, pi->tx_chan, iqid,
804 rc = alloc_txq(pi, txq, i, oid);
811 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ofld_txq",
812 CTLFLAG_RD, NULL, "tx queues for offloaded TCP connections");
813 for_each_ofld_txq(pi, i, ofld_txq) {
816 iqid = port_intr_iq(pi, j)->cntxt_id;
818 snprintf(name, sizeof(name), "%s ofld_txq%d",
819 device_get_nameunit(pi->dev), i);
820 init_eq(&ofld_txq->eq, EQ_OFLD, pi->qsize_txq, pi->tx_chan,
823 snprintf(name, sizeof(name), "%d", i);
824 oid2 = SYSCTL_ADD_NODE(&pi->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
825 name, CTLFLAG_RD, NULL, "offload tx queue");
827 rc = alloc_wrq(sc, pi, ofld_txq, oid2);
835 * Finally, the control queue.
837 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ctrlq", CTLFLAG_RD,
839 ctrlq = &sc->sge.ctrlq[pi->port_id];
840 iqid = port_intr_iq(pi, 0)->cntxt_id;
841 snprintf(name, sizeof(name), "%s ctrlq", device_get_nameunit(pi->dev));
842 init_eq(&ctrlq->eq, EQ_CTRL, CTRL_EQ_QSIZE, pi->tx_chan, iqid, name);
843 rc = alloc_wrq(sc, pi, ctrlq, oid);
847 t4_teardown_port_queues(pi);
856 t4_teardown_port_queues(struct port_info *pi)
859 struct adapter *sc = pi->adapter;
863 struct sge_ofld_rxq *ofld_rxq;
864 struct sge_wrq *ofld_txq;
867 /* Do this before freeing the queues */
868 if (pi->flags & PORT_SYSCTL_CTX) {
869 sysctl_ctx_free(&pi->ctx);
870 pi->flags &= ~PORT_SYSCTL_CTX;
874 * Take down all the tx queues first, as they reference the rx queues
875 * (for egress updates, etc.).
878 free_wrq(sc, &sc->sge.ctrlq[pi->port_id]);
880 for_each_txq(pi, i, txq) {
885 for_each_ofld_txq(pi, i, ofld_txq) {
886 free_wrq(sc, ofld_txq);
891 * Then take down the rx queues that forward their interrupts, as they
892 * reference other rx queues.
895 for_each_rxq(pi, i, rxq) {
896 if ((rxq->iq.flags & IQ_INTR) == 0)
901 for_each_ofld_rxq(pi, i, ofld_rxq) {
902 if ((ofld_rxq->iq.flags & IQ_INTR) == 0)
903 free_ofld_rxq(pi, ofld_rxq);
908 * Then take down the rx queues that take direct interrupts.
911 for_each_rxq(pi, i, rxq) {
912 if (rxq->iq.flags & IQ_INTR)
917 for_each_ofld_rxq(pi, i, ofld_rxq) {
918 if (ofld_rxq->iq.flags & IQ_INTR)
919 free_ofld_rxq(pi, ofld_rxq);
927 * Deals with errors and the firmware event queue. All data rx queues forward
928 * their interrupt to the firmware event queue.
931 t4_intr_all(void *arg)
933 struct adapter *sc = arg;
934 struct sge_iq *fwq = &sc->sge.fwq;
937 if (atomic_cmpset_int(&fwq->state, IQS_IDLE, IQS_BUSY)) {
939 atomic_cmpset_int(&fwq->state, IQS_BUSY, IQS_IDLE);
943 /* Deals with error interrupts */
945 t4_intr_err(void *arg)
947 struct adapter *sc = arg;
949 t4_write_reg(sc, MYPF_REG(A_PCIE_PF_CLI), 0);
950 t4_slow_intr_handler(sc);
954 t4_intr_evt(void *arg)
956 struct sge_iq *iq = arg;
958 if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) {
960 atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE);
967 struct sge_iq *iq = arg;
969 if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) {
971 atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE);
976 * Deals with anything and everything on the given ingress queue.
979 service_iq(struct sge_iq *iq, int budget)
982 struct sge_rxq *rxq = iq_to_rxq(iq); /* Use iff iq is part of rxq */
983 struct sge_fl *fl = &rxq->fl; /* Use iff IQ_HAS_FL */
984 struct adapter *sc = iq->adapter;
985 struct rsp_ctrl *ctrl;
986 const struct rss_header *rss;
987 int ndescs = 0, limit, fl_bufs_used = 0;
991 STAILQ_HEAD(, sge_iq) iql = STAILQ_HEAD_INITIALIZER(iql);
993 limit = budget ? budget : iq->qsize / 8;
995 KASSERT(iq->state == IQS_BUSY, ("%s: iq %p not BUSY", __func__, iq));
998 * We always come back and check the descriptor ring for new indirect
999 * interrupts and other responses after running a single handler.
1002 while (is_new_response(iq, &ctrl)) {
1007 rsp_type = G_RSPD_TYPE(ctrl->u.type_gen);
1008 lq = be32toh(ctrl->pldbuflen_qid);
1009 rss = (const void *)iq->cdesc;
1012 case X_RSPD_TYPE_FLBUF:
1014 KASSERT(iq->flags & IQ_HAS_FL,
1015 ("%s: data for an iq (%p) with no freelist",
1018 m0 = get_fl_payload(sc, fl, lq, &fl_bufs_used);
1019 #ifdef T4_PKT_TIMESTAMP
1021 * 60 bit timestamp for the payload is
1022 * *(uint64_t *)m0->m_pktdat. Note that it is
1023 * in the leading free-space in the mbuf. The
1024 * kernel can clobber it during a pullup,
1025 * m_copymdata, etc. You need to make sure that
1026 * the mbuf reaches you unmolested if you care
1027 * about the timestamp.
1029 *(uint64_t *)m0->m_pktdat =
1030 be64toh(ctrl->u.last_flit) &
1036 case X_RSPD_TYPE_CPL:
1037 KASSERT(rss->opcode < NUM_CPL_CMDS,
1038 ("%s: bad opcode %02x.", __func__,
1040 sc->cpl_handler[rss->opcode](iq, rss, m0);
1043 case X_RSPD_TYPE_INTR:
1046 * Interrupts should be forwarded only to queues
1047 * that are not forwarding their interrupts.
1048 * This means service_iq can recurse but only 1
1051 KASSERT(budget == 0,
1052 ("%s: budget %u, rsp_type %u", __func__,
1055 q = sc->sge.iqmap[lq - sc->sge.iq_start];
1056 if (atomic_cmpset_int(&q->state, IQS_IDLE,
1058 if (service_iq(q, q->qsize / 8) == 0) {
1059 atomic_cmpset_int(&q->state,
1060 IQS_BUSY, IQS_IDLE);
1062 STAILQ_INSERT_TAIL(&iql, q,
1069 sc->an_handler(iq, ctrl);
1074 if (++ndescs == limit) {
1075 t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS),
1077 V_INGRESSQID(iq->cntxt_id) |
1078 V_SEINTARM(V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX)));
1081 if (fl_bufs_used > 0) {
1083 fl->needed += fl_bufs_used;
1084 refill_fl(sc, fl, fl->cap / 8);
1090 return (EINPROGRESS);
1094 if (STAILQ_EMPTY(&iql))
1098 * Process the head only, and send it to the back of the list if
1099 * it's still not done.
1101 q = STAILQ_FIRST(&iql);
1102 STAILQ_REMOVE_HEAD(&iql, link);
1103 if (service_iq(q, q->qsize / 8) == 0)
1104 atomic_cmpset_int(&q->state, IQS_BUSY, IQS_IDLE);
1106 STAILQ_INSERT_TAIL(&iql, q, link);
1109 #if defined(INET) || defined(INET6)
1110 if (iq->flags & IQ_LRO_ENABLED) {
1111 struct lro_ctrl *lro = &rxq->lro;
1112 struct lro_entry *l;
1114 while (!SLIST_EMPTY(&lro->lro_active)) {
1115 l = SLIST_FIRST(&lro->lro_active);
1116 SLIST_REMOVE_HEAD(&lro->lro_active, next);
1117 tcp_lro_flush(lro, l);
1122 t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_CIDXINC(ndescs) |
1123 V_INGRESSQID((u32)iq->cntxt_id) | V_SEINTARM(iq->intr_params));
1125 if (iq->flags & IQ_HAS_FL) {
1129 fl->needed += fl_bufs_used;
1130 starved = refill_fl(sc, fl, fl->cap / 4);
1132 if (__predict_false(starved != 0))
1133 add_fl_to_sfl(sc, fl);
1139 static struct mbuf *
1140 get_fl_payload(struct adapter *sc, struct sge_fl *fl, uint32_t len_newbuf,
1143 struct mbuf *m0, *m;
1144 struct fl_sdesc *sd = &fl->sdesc[fl->cidx];
1145 unsigned int nbuf, len;
1148 * No assertion for the fl lock because we don't need it. This routine
1149 * is called only from the rx interrupt handler and it only updates
1150 * fl->cidx. (Contrast that with fl->pidx/fl->needed which could be
1151 * updated in the rx interrupt handler or the starvation helper routine.
1152 * That's why code that manipulates fl->pidx/fl->needed needs the fl
1153 * lock but this routine does not).
1156 if (__predict_false((len_newbuf & F_RSPD_NEWBUF) == 0))
1157 panic("%s: cannot handle packed frames", __func__);
1158 len = G_RSPD_LEN(len_newbuf);
1161 sd->m = NULL; /* consumed */
1163 bus_dmamap_sync(fl->tag[sd->tag_idx], sd->map, BUS_DMASYNC_POSTREAD);
1164 m_init(m0, NULL, 0, M_NOWAIT, MT_DATA, M_PKTHDR);
1165 #ifdef T4_PKT_TIMESTAMP
1166 /* Leave room for a timestamp */
1170 if (len < RX_COPY_THRESHOLD) {
1171 /* copy data to mbuf, buffer will be recycled */
1172 bcopy(sd->cl, mtod(m0, caddr_t), len);
1175 bus_dmamap_unload(fl->tag[sd->tag_idx], sd->map);
1176 m_cljset(m0, sd->cl, FL_BUF_TYPE(sd->tag_idx));
1177 sd->cl = NULL; /* consumed */
1178 m0->m_len = min(len, FL_BUF_SIZE(sd->tag_idx));
1180 m0->m_pkthdr.len = len;
1183 if (__predict_false(++fl->cidx == fl->cap)) {
1190 nbuf = 1; /* # of fl buffers used */
1194 sd->m = NULL; /* consumed */
1197 bus_dmamap_sync(fl->tag[sd->tag_idx], sd->map,
1198 BUS_DMASYNC_POSTREAD);
1200 m_init(m, NULL, 0, M_NOWAIT, MT_DATA, 0);
1202 bcopy(sd->cl, mtod(m, caddr_t), len);
1205 bus_dmamap_unload(fl->tag[sd->tag_idx],
1207 m_cljset(m, sd->cl, FL_BUF_TYPE(sd->tag_idx));
1208 sd->cl = NULL; /* consumed */
1209 m->m_len = min(len, FL_BUF_SIZE(sd->tag_idx));
1213 if (__predict_false(++fl->cidx == fl->cap)) {
1222 (*fl_bufs_used) += nbuf;
1228 t4_eth_rx(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m0)
1230 struct sge_rxq *rxq = iq_to_rxq(iq);
1231 struct ifnet *ifp = rxq->ifp;
1232 const struct cpl_rx_pkt *cpl = (const void *)(rss + 1);
1233 #if defined(INET) || defined(INET6)
1234 struct lro_ctrl *lro = &rxq->lro;
1237 KASSERT(m0 != NULL, ("%s: no payload with opcode %02x", __func__,
1240 m0->m_pkthdr.len -= fl_pktshift;
1241 m0->m_len -= fl_pktshift;
1242 m0->m_data += fl_pktshift;
1244 m0->m_pkthdr.rcvif = ifp;
1245 m0->m_flags |= M_FLOWID;
1246 m0->m_pkthdr.flowid = rss->hash_val;
1248 if (cpl->csum_calc && !cpl->err_vec) {
1249 if (ifp->if_capenable & IFCAP_RXCSUM &&
1250 cpl->l2info & htobe32(F_RXF_IP)) {
1251 m0->m_pkthdr.csum_flags = (CSUM_IP_CHECKED |
1252 CSUM_IP_VALID | CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1254 } else if (ifp->if_capenable & IFCAP_RXCSUM_IPV6 &&
1255 cpl->l2info & htobe32(F_RXF_IP6)) {
1256 m0->m_pkthdr.csum_flags = (CSUM_DATA_VALID_IPV6 |
1261 if (__predict_false(cpl->ip_frag))
1262 m0->m_pkthdr.csum_data = be16toh(cpl->csum);
1264 m0->m_pkthdr.csum_data = 0xffff;
1268 m0->m_pkthdr.ether_vtag = be16toh(cpl->vlan);
1269 m0->m_flags |= M_VLANTAG;
1270 rxq->vlan_extraction++;
1273 #if defined(INET) || defined(INET6)
1274 if (cpl->l2info & htobe32(F_RXF_LRO) &&
1275 iq->flags & IQ_LRO_ENABLED &&
1276 tcp_lro_rx(lro, m0, 0) == 0) {
1277 /* queued for LRO */
1280 ifp->if_input(ifp, m0);
1286 * Doesn't fail. Holds on to work requests it can't send right away.
1289 t4_wrq_tx_locked(struct adapter *sc, struct sge_wrq *wrq, struct wrqe *wr)
1291 struct sge_eq *eq = &wrq->eq;
1295 TXQ_LOCK_ASSERT_OWNED(wrq);
1297 KASSERT((eq->flags & EQ_TYPEMASK) == EQ_OFLD ||
1298 (eq->flags & EQ_TYPEMASK) == EQ_CTRL,
1299 ("%s: eq type %d", __func__, eq->flags & EQ_TYPEMASK));
1301 KASSERT((eq->flags & EQ_TYPEMASK) == EQ_CTRL,
1302 ("%s: eq type %d", __func__, eq->flags & EQ_TYPEMASK));
1305 if (__predict_true(wr != NULL))
1306 STAILQ_INSERT_TAIL(&wrq->wr_list, wr, link);
1308 can_reclaim = reclaimable(eq);
1309 if (__predict_false(eq->flags & EQ_STALLED)) {
1310 if (can_reclaim < tx_resume_threshold(eq))
1312 eq->flags &= ~EQ_STALLED;
1315 eq->cidx += can_reclaim;
1316 eq->avail += can_reclaim;
1317 if (__predict_false(eq->cidx >= eq->cap))
1318 eq->cidx -= eq->cap;
1320 while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL) {
1323 if (__predict_false(wr->wr_len < 0 ||
1324 wr->wr_len > SGE_MAX_WR_LEN || (wr->wr_len & 0x7))) {
1327 panic("%s: work request with length %d", __func__,
1333 log(LOG_ERR, "%s: %s work request with length %d",
1334 device_get_nameunit(sc->dev), __func__, wr->wr_len);
1335 STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
1340 ndesc = howmany(wr->wr_len, EQ_ESIZE);
1341 if (eq->avail < ndesc) {
1346 dst = (void *)&eq->desc[eq->pidx];
1347 copy_to_txd(eq, wrtod(wr), &dst, wr->wr_len);
1351 if (__predict_false(eq->pidx >= eq->cap))
1352 eq->pidx -= eq->cap;
1354 eq->pending += ndesc;
1355 if (eq->pending >= 8)
1359 STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
1362 if (eq->avail < 8) {
1363 can_reclaim = reclaimable(eq);
1364 eq->cidx += can_reclaim;
1365 eq->avail += can_reclaim;
1366 if (__predict_false(eq->cidx >= eq->cap))
1367 eq->cidx -= eq->cap;
1375 eq->flags |= EQ_STALLED;
1376 if (callout_pending(&eq->tx_callout) == 0)
1377 callout_reset(&eq->tx_callout, 1, t4_tx_callout, eq);
1381 /* Per-packet header in a coalesced tx WR, before the SGL starts (in flits) */
1382 #define TXPKTS_PKT_HDR ((\
1383 sizeof(struct ulp_txpkt) + \
1384 sizeof(struct ulptx_idata) + \
1385 sizeof(struct cpl_tx_pkt_core) \
1388 /* Header of a coalesced tx WR, before SGL of first packet (in flits) */
1389 #define TXPKTS_WR_HDR (\
1390 sizeof(struct fw_eth_tx_pkts_wr) / 8 + \
1393 /* Header of a tx WR, before SGL of first packet (in flits) */
1394 #define TXPKT_WR_HDR ((\
1395 sizeof(struct fw_eth_tx_pkt_wr) + \
1396 sizeof(struct cpl_tx_pkt_core) \
1399 /* Header of a tx LSO WR, before SGL of first packet (in flits) */
1400 #define TXPKT_LSO_WR_HDR ((\
1401 sizeof(struct fw_eth_tx_pkt_wr) + \
1402 sizeof(struct cpl_tx_pkt_lso_core) + \
1403 sizeof(struct cpl_tx_pkt_core) \
1407 t4_eth_tx(struct ifnet *ifp, struct sge_txq *txq, struct mbuf *m)
1409 struct port_info *pi = (void *)ifp->if_softc;
1410 struct adapter *sc = pi->adapter;
1411 struct sge_eq *eq = &txq->eq;
1412 struct buf_ring *br = txq->br;
1414 int rc, coalescing, can_reclaim;
1415 struct txpkts txpkts;
1418 TXQ_LOCK_ASSERT_OWNED(txq);
1419 KASSERT(m, ("%s: called with nothing to do.", __func__));
1420 KASSERT((eq->flags & EQ_TYPEMASK) == EQ_ETH,
1421 ("%s: eq type %d", __func__, eq->flags & EQ_TYPEMASK));
1423 prefetch(&eq->desc[eq->pidx]);
1424 prefetch(&txq->sdesc[eq->pidx]);
1426 txpkts.npkt = 0;/* indicates there's nothing in txpkts */
1429 can_reclaim = reclaimable(eq);
1430 if (__predict_false(eq->flags & EQ_STALLED)) {
1431 if (can_reclaim < tx_resume_threshold(eq)) {
1435 eq->flags &= ~EQ_STALLED;
1439 if (__predict_false(eq->flags & EQ_DOOMED)) {
1441 while ((m = buf_ring_dequeue_sc(txq->br)) != NULL)
1446 if (eq->avail < 8 && can_reclaim)
1447 reclaim_tx_descs(txq, can_reclaim, 32);
1449 for (; m; m = next ? next : drbr_dequeue(ifp, br)) {
1454 next = m->m_nextpkt;
1455 m->m_nextpkt = NULL;
1457 if (next || buf_ring_peek(br))
1460 rc = get_pkt_sgl(txq, &m, &sgl, coalescing);
1464 /* Short of resources, suspend tx */
1466 m->m_nextpkt = next;
1471 * Unrecoverable error for this packet, throw it away
1472 * and move on to the next. get_pkt_sgl may already
1473 * have freed m (it will be NULL in that case and the
1474 * m_freem here is still safe).
1482 add_to_txpkts(pi, txq, &txpkts, m, &sgl) == 0) {
1484 /* Successfully absorbed into txpkts */
1486 write_ulp_cpl_sgl(pi, txq, &txpkts, m, &sgl);
1491 * We weren't coalescing to begin with, or current frame could
1492 * not be coalesced (add_to_txpkts flushes txpkts if a frame
1493 * given to it can't be coalesced). Either way there should be
1494 * nothing in txpkts.
1496 KASSERT(txpkts.npkt == 0,
1497 ("%s: txpkts not empty: %d", __func__, txpkts.npkt));
1499 /* We're sending out individual packets now */
1503 reclaim_tx_descs(txq, 0, 8);
1504 rc = write_txpkt_wr(pi, txq, m, &sgl);
1507 /* Short of hardware descriptors, suspend tx */
1510 * This is an unlikely but expensive failure. We've
1511 * done all the hard work (DMA mappings etc.) and now we
1512 * can't send out the packet. What's worse, we have to
1513 * spend even more time freeing up everything in sgl.
1516 free_pkt_sgl(txq, &sgl);
1518 m->m_nextpkt = next;
1522 ETHER_BPF_MTAP(ifp, m);
1526 if (eq->pending >= 8)
1529 can_reclaim = reclaimable(eq);
1530 if (can_reclaim >= 32)
1531 reclaim_tx_descs(txq, can_reclaim, 64);
1534 if (txpkts.npkt > 0)
1535 write_txpkts_wr(txq, &txpkts);
1538 * m not NULL means there was an error but we haven't thrown it away.
1539 * This can happen when we're short of tx descriptors (no_desc) or maybe
1540 * even DMA maps (no_dmamap). Either way, a credit flush and reclaim
1541 * will get things going again.
1543 if (m && !(eq->flags & EQ_CRFLUSHED)) {
1544 struct tx_sdesc *txsd = &txq->sdesc[eq->pidx];
1547 * If EQ_CRFLUSHED is not set then we know we have at least one
1548 * available descriptor because any WR that reduces eq->avail to
1549 * 0 also sets EQ_CRFLUSHED.
1551 KASSERT(eq->avail > 0, ("%s: no space for eqflush.", __func__));
1553 txsd->desc_used = 1;
1555 write_eqflush_wr(eq);
1562 reclaim_tx_descs(txq, 0, 128);
1564 if (eq->flags & EQ_STALLED && callout_pending(&eq->tx_callout) == 0)
1565 callout_reset(&eq->tx_callout, 1, t4_tx_callout, eq);
1571 t4_update_fl_bufsize(struct ifnet *ifp)
1573 struct port_info *pi = ifp->if_softc;
1574 struct sge_rxq *rxq;
1576 struct sge_ofld_rxq *ofld_rxq;
1581 bufsize = mtu_to_bufsize(ifp->if_mtu);
1582 for_each_rxq(pi, i, rxq) {
1586 set_fl_tag_idx(fl, bufsize);
1590 bufsize = mtu_to_bufsize_toe(pi->adapter, ifp->if_mtu);
1591 for_each_ofld_rxq(pi, i, ofld_rxq) {
1595 set_fl_tag_idx(fl, bufsize);
1602 can_resume_tx(struct sge_eq *eq)
1604 return (reclaimable(eq) >= tx_resume_threshold(eq));
1608 init_iq(struct sge_iq *iq, struct adapter *sc, int tmr_idx, int pktc_idx,
1609 int qsize, int esize)
1611 KASSERT(tmr_idx >= 0 && tmr_idx < SGE_NTIMERS,
1612 ("%s: bad tmr_idx %d", __func__, tmr_idx));
1613 KASSERT(pktc_idx < SGE_NCOUNTERS, /* -ve is ok, means don't use */
1614 ("%s: bad pktc_idx %d", __func__, pktc_idx));
1618 iq->intr_params = V_QINTR_TIMER_IDX(tmr_idx);
1619 iq->intr_pktc_idx = SGE_NCOUNTERS - 1;
1620 if (pktc_idx >= 0) {
1621 iq->intr_params |= F_QINTR_CNT_EN;
1622 iq->intr_pktc_idx = pktc_idx;
1624 iq->qsize = roundup2(qsize, 16); /* See FW_IQ_CMD/iqsize */
1625 iq->esize = max(esize, 16); /* See FW_IQ_CMD/iqesize */
1629 init_fl(struct sge_fl *fl, int qsize, int bufsize, char *name)
1632 strlcpy(fl->lockname, name, sizeof(fl->lockname));
1633 set_fl_tag_idx(fl, bufsize);
1637 init_eq(struct sge_eq *eq, int eqtype, int qsize, uint8_t tx_chan,
1638 uint16_t iqid, char *name)
1640 KASSERT(tx_chan < NCHAN, ("%s: bad tx channel %d", __func__, tx_chan));
1641 KASSERT(eqtype <= EQ_TYPEMASK, ("%s: bad qtype %d", __func__, eqtype));
1643 eq->flags = eqtype & EQ_TYPEMASK;
1644 eq->tx_chan = tx_chan;
1647 strlcpy(eq->lockname, name, sizeof(eq->lockname));
1649 TASK_INIT(&eq->tx_task, 0, t4_tx_task, eq);
1650 callout_init(&eq->tx_callout, CALLOUT_MPSAFE);
1654 alloc_ring(struct adapter *sc, size_t len, bus_dma_tag_t *tag,
1655 bus_dmamap_t *map, bus_addr_t *pa, void **va)
1659 rc = bus_dma_tag_create(sc->dmat, 512, 0, BUS_SPACE_MAXADDR,
1660 BUS_SPACE_MAXADDR, NULL, NULL, len, 1, len, 0, NULL, NULL, tag);
1662 device_printf(sc->dev, "cannot allocate DMA tag: %d\n", rc);
1666 rc = bus_dmamem_alloc(*tag, va,
1667 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, map);
1669 device_printf(sc->dev, "cannot allocate DMA memory: %d\n", rc);
1673 rc = bus_dmamap_load(*tag, *map, *va, len, oneseg_dma_callback, pa, 0);
1675 device_printf(sc->dev, "cannot load DMA map: %d\n", rc);
1680 free_ring(sc, *tag, *map, *pa, *va);
1686 free_ring(struct adapter *sc, bus_dma_tag_t tag, bus_dmamap_t map,
1687 bus_addr_t pa, void *va)
1690 bus_dmamap_unload(tag, map);
1692 bus_dmamem_free(tag, va, map);
1694 bus_dma_tag_destroy(tag);
1700 * Allocates the ring for an ingress queue and an optional freelist. If the
1701 * freelist is specified it will be allocated and then associated with the
1704 * Returns errno on failure. Resources allocated up to that point may still be
1705 * allocated. Caller is responsible for cleanup in case this function fails.
1707 * If the ingress queue will take interrupts directly (iq->flags & IQ_INTR) then
1708 * the intr_idx specifies the vector, starting from 0. Otherwise it specifies
1709 * the abs_id of the ingress queue to which its interrupts should be forwarded.
1712 alloc_iq_fl(struct port_info *pi, struct sge_iq *iq, struct sge_fl *fl,
1713 int intr_idx, int cong)
1715 int rc, i, cntxt_id;
1718 struct adapter *sc = iq->adapter;
1721 len = iq->qsize * iq->esize;
1722 rc = alloc_ring(sc, len, &iq->desc_tag, &iq->desc_map, &iq->ba,
1723 (void **)&iq->desc);
1727 bzero(&c, sizeof(c));
1728 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_IQ_CMD) | F_FW_CMD_REQUEST |
1729 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_IQ_CMD_PFN(sc->pf) |
1730 V_FW_IQ_CMD_VFN(0));
1732 c.alloc_to_len16 = htobe32(F_FW_IQ_CMD_ALLOC | F_FW_IQ_CMD_IQSTART |
1735 /* Special handling for firmware event queue */
1736 if (iq == &sc->sge.fwq)
1737 v |= F_FW_IQ_CMD_IQASYNCH;
1739 if (iq->flags & IQ_INTR) {
1740 KASSERT(intr_idx < sc->intr_count,
1741 ("%s: invalid direct intr_idx %d", __func__, intr_idx));
1743 v |= F_FW_IQ_CMD_IQANDST;
1744 v |= V_FW_IQ_CMD_IQANDSTINDEX(intr_idx);
1746 c.type_to_iqandstindex = htobe32(v |
1747 V_FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
1748 V_FW_IQ_CMD_VIID(pi->viid) |
1749 V_FW_IQ_CMD_IQANUD(X_UPDATEDELIVERY_INTERRUPT));
1750 c.iqdroprss_to_iqesize = htobe16(V_FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
1751 F_FW_IQ_CMD_IQGTSMODE |
1752 V_FW_IQ_CMD_IQINTCNTTHRESH(iq->intr_pktc_idx) |
1753 V_FW_IQ_CMD_IQESIZE(ilog2(iq->esize) - 4));
1754 c.iqsize = htobe16(iq->qsize);
1755 c.iqaddr = htobe64(iq->ba);
1757 c.iqns_to_fl0congen = htobe32(F_FW_IQ_CMD_IQFLINTCONGEN);
1760 mtx_init(&fl->fl_lock, fl->lockname, NULL, MTX_DEF);
1762 for (i = 0; i < FL_BUF_SIZES; i++) {
1765 * A freelist buffer must be 16 byte aligned as the SGE
1766 * uses the low 4 bits of the bus addr to figure out the
1769 rc = bus_dma_tag_create(sc->dmat, 16, 0,
1770 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1771 FL_BUF_SIZE(i), 1, FL_BUF_SIZE(i), BUS_DMA_ALLOCNOW,
1772 NULL, NULL, &fl->tag[i]);
1774 device_printf(sc->dev,
1775 "failed to create fl DMA tag[%d]: %d\n",
1780 len = fl->qsize * RX_FL_ESIZE;
1781 rc = alloc_ring(sc, len, &fl->desc_tag, &fl->desc_map,
1782 &fl->ba, (void **)&fl->desc);
1786 /* Allocate space for one software descriptor per buffer. */
1787 fl->cap = (fl->qsize - spg_len / RX_FL_ESIZE) * 8;
1789 rc = alloc_fl_sdesc(fl);
1792 device_printf(sc->dev,
1793 "failed to setup fl software descriptors: %d\n",
1797 fl->needed = fl->cap;
1798 fl->lowat = roundup2(sc->sge.fl_starve_threshold, 8);
1800 c.iqns_to_fl0congen |=
1801 htobe32(V_FW_IQ_CMD_FL0HOSTFCMODE(X_HOSTFCMODE_NONE) |
1802 F_FW_IQ_CMD_FL0FETCHRO | F_FW_IQ_CMD_FL0DATARO |
1803 F_FW_IQ_CMD_FL0PADEN);
1805 c.iqns_to_fl0congen |=
1806 htobe32(V_FW_IQ_CMD_FL0CNGCHMAP(cong) |
1807 F_FW_IQ_CMD_FL0CONGCIF |
1808 F_FW_IQ_CMD_FL0CONGEN);
1810 c.fl0dcaen_to_fl0cidxfthresh =
1811 htobe16(V_FW_IQ_CMD_FL0FBMIN(X_FETCHBURSTMIN_64B) |
1812 V_FW_IQ_CMD_FL0FBMAX(X_FETCHBURSTMAX_512B));
1813 c.fl0size = htobe16(fl->qsize);
1814 c.fl0addr = htobe64(fl->ba);
1817 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
1819 device_printf(sc->dev,
1820 "failed to create ingress queue: %d\n", rc);
1824 iq->cdesc = iq->desc;
1827 iq->intr_next = iq->intr_params;
1828 iq->cntxt_id = be16toh(c.iqid);
1829 iq->abs_id = be16toh(c.physiqid);
1830 iq->flags |= IQ_ALLOCATED;
1832 cntxt_id = iq->cntxt_id - sc->sge.iq_start;
1833 if (cntxt_id >= sc->sge.niq) {
1834 panic ("%s: iq->cntxt_id (%d) more than the max (%d)", __func__,
1835 cntxt_id, sc->sge.niq - 1);
1837 sc->sge.iqmap[cntxt_id] = iq;
1840 fl->cntxt_id = be16toh(c.fl0id);
1841 fl->pidx = fl->cidx = 0;
1843 cntxt_id = fl->cntxt_id - sc->sge.eq_start;
1844 if (cntxt_id >= sc->sge.neq) {
1845 panic("%s: fl->cntxt_id (%d) more than the max (%d)",
1846 __func__, cntxt_id, sc->sge.neq - 1);
1848 sc->sge.eqmap[cntxt_id] = (void *)fl;
1851 /* Enough to make sure the SGE doesn't think it's starved */
1852 refill_fl(sc, fl, fl->lowat);
1855 iq->flags |= IQ_HAS_FL;
1858 /* Enable IQ interrupts */
1859 atomic_store_rel_int(&iq->state, IQS_IDLE);
1860 t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_SEINTARM(iq->intr_params) |
1861 V_INGRESSQID(iq->cntxt_id));
1867 free_iq_fl(struct port_info *pi, struct sge_iq *iq, struct sge_fl *fl)
1870 struct adapter *sc = iq->adapter;
1874 return (0); /* nothing to do */
1876 dev = pi ? pi->dev : sc->dev;
1878 if (iq->flags & IQ_ALLOCATED) {
1879 rc = -t4_iq_free(sc, sc->mbox, sc->pf, 0,
1880 FW_IQ_TYPE_FL_INT_CAP, iq->cntxt_id,
1881 fl ? fl->cntxt_id : 0xffff, 0xffff);
1884 "failed to free queue %p: %d\n", iq, rc);
1887 iq->flags &= ~IQ_ALLOCATED;
1890 free_ring(sc, iq->desc_tag, iq->desc_map, iq->ba, iq->desc);
1892 bzero(iq, sizeof(*iq));
1895 free_ring(sc, fl->desc_tag, fl->desc_map, fl->ba,
1904 if (mtx_initialized(&fl->fl_lock))
1905 mtx_destroy(&fl->fl_lock);
1907 for (i = 0; i < FL_BUF_SIZES; i++) {
1909 bus_dma_tag_destroy(fl->tag[i]);
1912 bzero(fl, sizeof(*fl));
1919 alloc_fwq(struct adapter *sc)
1922 struct sge_iq *fwq = &sc->sge.fwq;
1923 struct sysctl_oid *oid = device_get_sysctl_tree(sc->dev);
1924 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
1926 init_iq(fwq, sc, 0, 0, FW_IQ_QSIZE, FW_IQ_ESIZE);
1927 fwq->flags |= IQ_INTR; /* always */
1928 intr_idx = sc->intr_count > 1 ? 1 : 0;
1929 rc = alloc_iq_fl(sc->port[0], fwq, NULL, intr_idx, -1);
1931 device_printf(sc->dev,
1932 "failed to create firmware event queue: %d\n", rc);
1936 oid = SYSCTL_ADD_NODE(&sc->ctx, children, OID_AUTO, "fwq", CTLFLAG_RD,
1937 NULL, "firmware event queue");
1938 children = SYSCTL_CHILDREN(oid);
1940 SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "abs_id",
1941 CTLTYPE_INT | CTLFLAG_RD, &fwq->abs_id, 0, sysctl_uint16, "I",
1942 "absolute id of the queue");
1943 SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "cntxt_id",
1944 CTLTYPE_INT | CTLFLAG_RD, &fwq->cntxt_id, 0, sysctl_uint16, "I",
1945 "SGE context id of the queue");
1946 SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "cidx",
1947 CTLTYPE_INT | CTLFLAG_RD, &fwq->cidx, 0, sysctl_uint16, "I",
1954 free_fwq(struct adapter *sc)
1956 return free_iq_fl(NULL, &sc->sge.fwq, NULL);
1960 alloc_mgmtq(struct adapter *sc)
1963 struct sge_wrq *mgmtq = &sc->sge.mgmtq;
1965 struct sysctl_oid *oid = device_get_sysctl_tree(sc->dev);
1966 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
1968 oid = SYSCTL_ADD_NODE(&sc->ctx, children, OID_AUTO, "mgmtq", CTLFLAG_RD,
1969 NULL, "management queue");
1971 snprintf(name, sizeof(name), "%s mgmtq", device_get_nameunit(sc->dev));
1972 init_eq(&mgmtq->eq, EQ_CTRL, CTRL_EQ_QSIZE, sc->port[0]->tx_chan,
1973 sc->sge.fwq.cntxt_id, name);
1974 rc = alloc_wrq(sc, NULL, mgmtq, oid);
1976 device_printf(sc->dev,
1977 "failed to create management queue: %d\n", rc);
1985 free_mgmtq(struct adapter *sc)
1988 return free_wrq(sc, &sc->sge.mgmtq);
1992 tnl_cong(struct port_info *pi)
1995 if (cong_drop == -1)
1997 else if (cong_drop == 1)
2000 return (1 << pi->tx_chan);
2004 alloc_rxq(struct port_info *pi, struct sge_rxq *rxq, int intr_idx, int idx,
2005 struct sysctl_oid *oid)
2008 struct sysctl_oid_list *children;
2011 rc = alloc_iq_fl(pi, &rxq->iq, &rxq->fl, intr_idx, tnl_cong(pi));
2016 refill_fl(pi->adapter, &rxq->fl, rxq->fl.needed / 8);
2017 FL_UNLOCK(&rxq->fl);
2019 #if defined(INET) || defined(INET6)
2020 rc = tcp_lro_init(&rxq->lro);
2023 rxq->lro.ifp = pi->ifp; /* also indicates LRO init'ed */
2025 if (pi->ifp->if_capenable & IFCAP_LRO)
2026 rxq->iq.flags |= IQ_LRO_ENABLED;
2030 children = SYSCTL_CHILDREN(oid);
2032 snprintf(name, sizeof(name), "%d", idx);
2033 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
2035 children = SYSCTL_CHILDREN(oid);
2037 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "abs_id",
2038 CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.abs_id, 0, sysctl_uint16, "I",
2039 "absolute id of the queue");
2040 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cntxt_id",
2041 CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.cntxt_id, 0, sysctl_uint16, "I",
2042 "SGE context id of the queue");
2043 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
2044 CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.cidx, 0, sysctl_uint16, "I",
2046 #if defined(INET) || defined(INET6)
2047 SYSCTL_ADD_INT(&pi->ctx, children, OID_AUTO, "lro_queued", CTLFLAG_RD,
2048 &rxq->lro.lro_queued, 0, NULL);
2049 SYSCTL_ADD_INT(&pi->ctx, children, OID_AUTO, "lro_flushed", CTLFLAG_RD,
2050 &rxq->lro.lro_flushed, 0, NULL);
2052 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "rxcsum", CTLFLAG_RD,
2053 &rxq->rxcsum, "# of times hardware assisted with checksum");
2054 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "vlan_extraction",
2055 CTLFLAG_RD, &rxq->vlan_extraction,
2056 "# of times hardware extracted 802.1Q tag");
2058 children = SYSCTL_CHILDREN(oid);
2059 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "fl", CTLFLAG_RD,
2061 children = SYSCTL_CHILDREN(oid);
2063 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cntxt_id",
2064 CTLTYPE_INT | CTLFLAG_RD, &rxq->fl.cntxt_id, 0, sysctl_uint16, "I",
2065 "SGE context id of the queue");
2066 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "cidx", CTLFLAG_RD,
2067 &rxq->fl.cidx, 0, "consumer index");
2068 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "pidx", CTLFLAG_RD,
2069 &rxq->fl.pidx, 0, "producer index");
2075 free_rxq(struct port_info *pi, struct sge_rxq *rxq)
2079 #if defined(INET) || defined(INET6)
2081 tcp_lro_free(&rxq->lro);
2082 rxq->lro.ifp = NULL;
2086 rc = free_iq_fl(pi, &rxq->iq, &rxq->fl);
2088 bzero(rxq, sizeof(*rxq));
2095 alloc_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq,
2096 int intr_idx, int idx, struct sysctl_oid *oid)
2099 struct sysctl_oid_list *children;
2102 rc = alloc_iq_fl(pi, &ofld_rxq->iq, &ofld_rxq->fl, intr_idx,
2107 children = SYSCTL_CHILDREN(oid);
2109 snprintf(name, sizeof(name), "%d", idx);
2110 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
2112 children = SYSCTL_CHILDREN(oid);
2114 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "abs_id",
2115 CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.abs_id, 0, sysctl_uint16,
2116 "I", "absolute id of the queue");
2117 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cntxt_id",
2118 CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.cntxt_id, 0, sysctl_uint16,
2119 "I", "SGE context id of the queue");
2120 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
2121 CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.cidx, 0, sysctl_uint16, "I",
2124 children = SYSCTL_CHILDREN(oid);
2125 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "fl", CTLFLAG_RD,
2127 children = SYSCTL_CHILDREN(oid);
2129 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cntxt_id",
2130 CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->fl.cntxt_id, 0, sysctl_uint16,
2131 "I", "SGE context id of the queue");
2132 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "cidx", CTLFLAG_RD,
2133 &ofld_rxq->fl.cidx, 0, "consumer index");
2134 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "pidx", CTLFLAG_RD,
2135 &ofld_rxq->fl.pidx, 0, "producer index");
2141 free_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq)
2145 rc = free_iq_fl(pi, &ofld_rxq->iq, &ofld_rxq->fl);
2147 bzero(ofld_rxq, sizeof(*ofld_rxq));
2154 ctrl_eq_alloc(struct adapter *sc, struct sge_eq *eq)
2157 struct fw_eq_ctrl_cmd c;
2159 bzero(&c, sizeof(c));
2161 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_CTRL_CMD) | F_FW_CMD_REQUEST |
2162 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_CTRL_CMD_PFN(sc->pf) |
2163 V_FW_EQ_CTRL_CMD_VFN(0));
2164 c.alloc_to_len16 = htobe32(F_FW_EQ_CTRL_CMD_ALLOC |
2165 F_FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
2166 c.cmpliqid_eqid = htonl(V_FW_EQ_CTRL_CMD_CMPLIQID(eq->iqid)); /* XXX */
2167 c.physeqid_pkd = htobe32(0);
2168 c.fetchszm_to_iqid =
2169 htobe32(V_FW_EQ_CTRL_CMD_HOSTFCMODE(X_HOSTFCMODE_STATUS_PAGE) |
2170 V_FW_EQ_CTRL_CMD_PCIECHN(eq->tx_chan) |
2171 F_FW_EQ_CTRL_CMD_FETCHRO | V_FW_EQ_CTRL_CMD_IQID(eq->iqid));
2173 htobe32(V_FW_EQ_CTRL_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
2174 V_FW_EQ_CTRL_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
2175 V_FW_EQ_CTRL_CMD_CIDXFTHRESH(X_CIDXFLUSHTHRESH_32) |
2176 V_FW_EQ_CTRL_CMD_EQSIZE(eq->qsize));
2177 c.eqaddr = htobe64(eq->ba);
2179 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
2181 device_printf(sc->dev,
2182 "failed to create control queue %d: %d\n", eq->tx_chan, rc);
2185 eq->flags |= EQ_ALLOCATED;
2187 eq->cntxt_id = G_FW_EQ_CTRL_CMD_EQID(be32toh(c.cmpliqid_eqid));
2188 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
2189 if (cntxt_id >= sc->sge.neq)
2190 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
2191 cntxt_id, sc->sge.neq - 1);
2192 sc->sge.eqmap[cntxt_id] = eq;
2198 eth_eq_alloc(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
2201 struct fw_eq_eth_cmd c;
2203 bzero(&c, sizeof(c));
2205 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_ETH_CMD) | F_FW_CMD_REQUEST |
2206 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_ETH_CMD_PFN(sc->pf) |
2207 V_FW_EQ_ETH_CMD_VFN(0));
2208 c.alloc_to_len16 = htobe32(F_FW_EQ_ETH_CMD_ALLOC |
2209 F_FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
2210 c.viid_pkd = htobe32(V_FW_EQ_ETH_CMD_VIID(pi->viid));
2211 c.fetchszm_to_iqid =
2212 htobe32(V_FW_EQ_ETH_CMD_HOSTFCMODE(X_HOSTFCMODE_STATUS_PAGE) |
2213 V_FW_EQ_ETH_CMD_PCIECHN(eq->tx_chan) | F_FW_EQ_ETH_CMD_FETCHRO |
2214 V_FW_EQ_ETH_CMD_IQID(eq->iqid));
2215 c.dcaen_to_eqsize = htobe32(V_FW_EQ_ETH_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
2216 V_FW_EQ_ETH_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
2217 V_FW_EQ_ETH_CMD_CIDXFTHRESH(X_CIDXFLUSHTHRESH_32) |
2218 V_FW_EQ_ETH_CMD_EQSIZE(eq->qsize));
2219 c.eqaddr = htobe64(eq->ba);
2221 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
2223 device_printf(pi->dev,
2224 "failed to create Ethernet egress queue: %d\n", rc);
2227 eq->flags |= EQ_ALLOCATED;
2229 eq->cntxt_id = G_FW_EQ_ETH_CMD_EQID(be32toh(c.eqid_pkd));
2230 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
2231 if (cntxt_id >= sc->sge.neq)
2232 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
2233 cntxt_id, sc->sge.neq - 1);
2234 sc->sge.eqmap[cntxt_id] = eq;
2241 ofld_eq_alloc(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
2244 struct fw_eq_ofld_cmd c;
2246 bzero(&c, sizeof(c));
2248 c.op_to_vfn = htonl(V_FW_CMD_OP(FW_EQ_OFLD_CMD) | F_FW_CMD_REQUEST |
2249 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_OFLD_CMD_PFN(sc->pf) |
2250 V_FW_EQ_OFLD_CMD_VFN(0));
2251 c.alloc_to_len16 = htonl(F_FW_EQ_OFLD_CMD_ALLOC |
2252 F_FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
2253 c.fetchszm_to_iqid =
2254 htonl(V_FW_EQ_OFLD_CMD_HOSTFCMODE(X_HOSTFCMODE_STATUS_PAGE) |
2255 V_FW_EQ_OFLD_CMD_PCIECHN(eq->tx_chan) |
2256 F_FW_EQ_OFLD_CMD_FETCHRO | V_FW_EQ_OFLD_CMD_IQID(eq->iqid));
2258 htobe32(V_FW_EQ_OFLD_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
2259 V_FW_EQ_OFLD_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
2260 V_FW_EQ_OFLD_CMD_CIDXFTHRESH(X_CIDXFLUSHTHRESH_32) |
2261 V_FW_EQ_OFLD_CMD_EQSIZE(eq->qsize));
2262 c.eqaddr = htobe64(eq->ba);
2264 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
2266 device_printf(pi->dev,
2267 "failed to create egress queue for TCP offload: %d\n", rc);
2270 eq->flags |= EQ_ALLOCATED;
2272 eq->cntxt_id = G_FW_EQ_OFLD_CMD_EQID(be32toh(c.eqid_pkd));
2273 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
2274 if (cntxt_id >= sc->sge.neq)
2275 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
2276 cntxt_id, sc->sge.neq - 1);
2277 sc->sge.eqmap[cntxt_id] = eq;
2284 alloc_eq(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
2289 mtx_init(&eq->eq_lock, eq->lockname, NULL, MTX_DEF);
2291 len = eq->qsize * EQ_ESIZE;
2292 rc = alloc_ring(sc, len, &eq->desc_tag, &eq->desc_map,
2293 &eq->ba, (void **)&eq->desc);
2297 eq->cap = eq->qsize - spg_len / EQ_ESIZE;
2298 eq->spg = (void *)&eq->desc[eq->cap];
2299 eq->avail = eq->cap - 1; /* one less to avoid cidx = pidx */
2300 eq->pidx = eq->cidx = 0;
2301 eq->doorbells = sc->doorbells;
2303 switch (eq->flags & EQ_TYPEMASK) {
2305 rc = ctrl_eq_alloc(sc, eq);
2309 rc = eth_eq_alloc(sc, pi, eq);
2314 rc = ofld_eq_alloc(sc, pi, eq);
2319 panic("%s: invalid eq type %d.", __func__,
2320 eq->flags & EQ_TYPEMASK);
2323 device_printf(sc->dev,
2324 "failed to allocate egress queue(%d): %d",
2325 eq->flags & EQ_TYPEMASK, rc);
2328 eq->tx_callout.c_cpu = eq->cntxt_id % mp_ncpus;
2330 if (isset(&eq->doorbells, DOORBELL_UDB) ||
2331 isset(&eq->doorbells, DOORBELL_UDBWC) ||
2332 isset(&eq->doorbells, DOORBELL_WCWR)) {
2333 uint32_t s_qpp = sc->sge.s_qpp;
2334 uint32_t mask = (1 << s_qpp) - 1;
2335 volatile uint8_t *udb;
2337 udb = sc->udbs_base + UDBS_DB_OFFSET;
2338 udb += (eq->cntxt_id >> s_qpp) << PAGE_SHIFT; /* pg offset */
2339 eq->udb_qid = eq->cntxt_id & mask; /* id in page */
2340 if (eq->udb_qid > PAGE_SIZE / UDBS_SEG_SIZE)
2341 clrbit(&eq->doorbells, DOORBELL_WCWR);
2343 udb += eq->udb_qid << UDBS_SEG_SHIFT; /* seg offset */
2346 eq->udb = (volatile void *)udb;
2353 free_eq(struct adapter *sc, struct sge_eq *eq)
2357 if (eq->flags & EQ_ALLOCATED) {
2358 switch (eq->flags & EQ_TYPEMASK) {
2360 rc = -t4_ctrl_eq_free(sc, sc->mbox, sc->pf, 0,
2365 rc = -t4_eth_eq_free(sc, sc->mbox, sc->pf, 0,
2371 rc = -t4_ofld_eq_free(sc, sc->mbox, sc->pf, 0,
2377 panic("%s: invalid eq type %d.", __func__,
2378 eq->flags & EQ_TYPEMASK);
2381 device_printf(sc->dev,
2382 "failed to free egress queue (%d): %d\n",
2383 eq->flags & EQ_TYPEMASK, rc);
2386 eq->flags &= ~EQ_ALLOCATED;
2389 free_ring(sc, eq->desc_tag, eq->desc_map, eq->ba, eq->desc);
2391 if (mtx_initialized(&eq->eq_lock))
2392 mtx_destroy(&eq->eq_lock);
2394 bzero(eq, sizeof(*eq));
2399 alloc_wrq(struct adapter *sc, struct port_info *pi, struct sge_wrq *wrq,
2400 struct sysctl_oid *oid)
2403 struct sysctl_ctx_list *ctx = pi ? &pi->ctx : &sc->ctx;
2404 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
2406 rc = alloc_eq(sc, pi, &wrq->eq);
2411 STAILQ_INIT(&wrq->wr_list);
2413 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
2414 &wrq->eq.cntxt_id, 0, "SGE context id of the queue");
2415 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cidx",
2416 CTLTYPE_INT | CTLFLAG_RD, &wrq->eq.cidx, 0, sysctl_uint16, "I",
2418 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pidx",
2419 CTLTYPE_INT | CTLFLAG_RD, &wrq->eq.pidx, 0, sysctl_uint16, "I",
2421 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs", CTLFLAG_RD,
2422 &wrq->tx_wrs, "# of work requests");
2423 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "no_desc", CTLFLAG_RD,
2425 "# of times queue ran out of hardware descriptors");
2426 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "unstalled", CTLFLAG_RD,
2427 &wrq->eq.unstalled, 0, "# of times queue recovered after stall");
2434 free_wrq(struct adapter *sc, struct sge_wrq *wrq)
2438 rc = free_eq(sc, &wrq->eq);
2442 bzero(wrq, sizeof(*wrq));
2447 alloc_txq(struct port_info *pi, struct sge_txq *txq, int idx,
2448 struct sysctl_oid *oid)
2451 struct adapter *sc = pi->adapter;
2452 struct sge_eq *eq = &txq->eq;
2454 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
2456 rc = alloc_eq(sc, pi, eq);
2462 txq->sdesc = malloc(eq->cap * sizeof(struct tx_sdesc), M_CXGBE,
2464 txq->br = buf_ring_alloc(eq->qsize, M_CXGBE, M_WAITOK, &eq->eq_lock);
2466 rc = bus_dma_tag_create(sc->dmat, 1, 0, BUS_SPACE_MAXADDR,
2467 BUS_SPACE_MAXADDR, NULL, NULL, 64 * 1024, TX_SGL_SEGS,
2468 BUS_SPACE_MAXSIZE, BUS_DMA_ALLOCNOW, NULL, NULL, &txq->tx_tag);
2470 device_printf(sc->dev,
2471 "failed to create tx DMA tag: %d\n", rc);
2476 * We can stuff ~10 frames in an 8-descriptor txpkts WR (8 is the SGE
2477 * limit for any WR). txq->no_dmamap events shouldn't occur if maps is
2478 * sized for the worst case.
2480 rc = t4_alloc_tx_maps(&txq->txmaps, txq->tx_tag, eq->qsize * 10 / 8,
2483 device_printf(sc->dev, "failed to setup tx DMA maps: %d\n", rc);
2487 snprintf(name, sizeof(name), "%d", idx);
2488 oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD,
2490 children = SYSCTL_CHILDREN(oid);
2492 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
2493 &eq->cntxt_id, 0, "SGE context id of the queue");
2494 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx",
2495 CTLTYPE_INT | CTLFLAG_RD, &eq->cidx, 0, sysctl_uint16, "I",
2497 SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "pidx",
2498 CTLTYPE_INT | CTLFLAG_RD, &eq->pidx, 0, sysctl_uint16, "I",
2501 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txcsum", CTLFLAG_RD,
2502 &txq->txcsum, "# of times hardware assisted with checksum");
2503 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "vlan_insertion",
2504 CTLFLAG_RD, &txq->vlan_insertion,
2505 "# of times hardware inserted 802.1Q tag");
2506 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "tso_wrs", CTLFLAG_RD,
2507 &txq->tso_wrs, "# of TSO work requests");
2508 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "imm_wrs", CTLFLAG_RD,
2509 &txq->imm_wrs, "# of work requests with immediate data");
2510 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "sgl_wrs", CTLFLAG_RD,
2511 &txq->sgl_wrs, "# of work requests with direct SGL");
2512 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkt_wrs", CTLFLAG_RD,
2513 &txq->txpkt_wrs, "# of txpkt work requests (one pkt/WR)");
2514 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts_wrs", CTLFLAG_RD,
2515 &txq->txpkts_wrs, "# of txpkts work requests (multiple pkts/WR)");
2516 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts_pkts", CTLFLAG_RD,
2517 &txq->txpkts_pkts, "# of frames tx'd using txpkts work requests");
2519 SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "br_drops", CTLFLAG_RD,
2520 &txq->br->br_drops, "# of drops in the buf_ring for this queue");
2521 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "no_dmamap", CTLFLAG_RD,
2522 &txq->no_dmamap, 0, "# of times txq ran out of DMA maps");
2523 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "no_desc", CTLFLAG_RD,
2524 &txq->no_desc, 0, "# of times txq ran out of hardware descriptors");
2525 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "egr_update", CTLFLAG_RD,
2526 &eq->egr_update, 0, "egress update notifications from the SGE");
2527 SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "unstalled", CTLFLAG_RD,
2528 &eq->unstalled, 0, "# of times txq recovered after stall");
2534 free_txq(struct port_info *pi, struct sge_txq *txq)
2537 struct adapter *sc = pi->adapter;
2538 struct sge_eq *eq = &txq->eq;
2540 rc = free_eq(sc, eq);
2544 free(txq->sdesc, M_CXGBE);
2546 if (txq->txmaps.maps)
2547 t4_free_tx_maps(&txq->txmaps, txq->tx_tag);
2549 buf_ring_free(txq->br, M_CXGBE);
2552 bus_dma_tag_destroy(txq->tx_tag);
2554 bzero(txq, sizeof(*txq));
2559 oneseg_dma_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2561 bus_addr_t *ba = arg;
2564 ("%s meant for single segment mappings only.", __func__));
2566 *ba = error ? 0 : segs->ds_addr;
2570 is_new_response(const struct sge_iq *iq, struct rsp_ctrl **ctrl)
2572 *ctrl = (void *)((uintptr_t)iq->cdesc +
2573 (iq->esize - sizeof(struct rsp_ctrl)));
2575 return (((*ctrl)->u.type_gen >> S_RSPD_GEN) == iq->gen);
2579 iq_next(struct sge_iq *iq)
2581 iq->cdesc = (void *) ((uintptr_t)iq->cdesc + iq->esize);
2582 if (__predict_false(++iq->cidx == iq->qsize - 1)) {
2585 iq->cdesc = iq->desc;
2589 #define FL_HW_IDX(x) ((x) >> 3)
2591 ring_fl_db(struct adapter *sc, struct sge_fl *fl)
2593 int ndesc = fl->pending / 8;
2596 if (FL_HW_IDX(fl->pidx) == FL_HW_IDX(fl->cidx))
2597 ndesc--; /* hold back one credit */
2600 return; /* nothing to do */
2602 v = F_DBPRIO | V_QID(fl->cntxt_id) | V_PIDX(ndesc);
2608 t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL), v);
2609 fl->pending -= ndesc * 8;
2613 * Fill up the freelist by upto nbufs and maybe ring its doorbell.
2615 * Returns non-zero to indicate that it should be added to the list of starving
2619 refill_fl(struct adapter *sc, struct sge_fl *fl, int nbufs)
2621 __be64 *d = &fl->desc[fl->pidx];
2622 struct fl_sdesc *sd = &fl->sdesc[fl->pidx];
2628 FL_LOCK_ASSERT_OWNED(fl);
2630 if (nbufs > fl->needed)
2635 if (sd->cl != NULL) {
2638 * This happens when a frame small enough to fit
2639 * entirely in an mbuf was received in cl last time.
2640 * We'd held on to cl and can reuse it now. Note that
2641 * we reuse a cluster of the old size if fl->tag_idx is
2642 * no longer the same as sd->tag_idx.
2645 KASSERT(*d == sd->ba_tag,
2646 ("%s: recyling problem at pidx %d",
2647 __func__, fl->pidx));
2654 if (fl->tag_idx != sd->tag_idx) {
2656 bus_dma_tag_t newtag = fl->tag[fl->tag_idx];
2657 bus_dma_tag_t oldtag = fl->tag[sd->tag_idx];
2660 * An MTU change can get us here. Discard the old map
2661 * which was created with the old tag, but only if
2662 * we're able to get a new one.
2664 rc = bus_dmamap_create(newtag, 0, &map);
2666 bus_dmamap_destroy(oldtag, sd->map);
2668 sd->tag_idx = fl->tag_idx;
2672 tag = fl->tag[sd->tag_idx];
2674 cl = m_cljget(NULL, M_NOWAIT, FL_BUF_SIZE(sd->tag_idx));
2678 rc = bus_dmamap_load(tag, sd->map, cl, FL_BUF_SIZE(sd->tag_idx),
2679 oneseg_dma_callback, &pa, 0);
2680 if (rc != 0 || pa == 0) {
2681 fl->dmamap_failed++;
2682 uma_zfree(FL_BUF_ZONE(sd->tag_idx), cl);
2687 *d++ = htobe64(pa | sd->tag_idx);
2690 sd->ba_tag = htobe64(pa | sd->tag_idx);
2694 /* sd->m is never recycled, should always be NULL */
2695 KASSERT(sd->m == NULL, ("%s: stray mbuf", __func__));
2697 sd->m = m_gethdr(M_NOWAIT, MT_NOINIT);
2704 if (++fl->pidx == fl->cap) {
2711 if (fl->pending >= 8)
2714 return (FL_RUNNING_LOW(fl) && !(fl->flags & FL_STARVING));
2718 * Attempt to refill all starving freelists.
2721 refill_sfl(void *arg)
2723 struct adapter *sc = arg;
2724 struct sge_fl *fl, *fl_temp;
2726 mtx_lock(&sc->sfl_lock);
2727 TAILQ_FOREACH_SAFE(fl, &sc->sfl, link, fl_temp) {
2729 refill_fl(sc, fl, 64);
2730 if (FL_NOT_RUNNING_LOW(fl) || fl->flags & FL_DOOMED) {
2731 TAILQ_REMOVE(&sc->sfl, fl, link);
2732 fl->flags &= ~FL_STARVING;
2737 if (!TAILQ_EMPTY(&sc->sfl))
2738 callout_schedule(&sc->sfl_callout, hz / 5);
2739 mtx_unlock(&sc->sfl_lock);
2743 alloc_fl_sdesc(struct sge_fl *fl)
2745 struct fl_sdesc *sd;
2749 FL_LOCK_ASSERT_OWNED(fl);
2751 fl->sdesc = malloc(fl->cap * sizeof(struct fl_sdesc), M_CXGBE,
2754 tag = fl->tag[fl->tag_idx];
2756 for (i = 0; i < fl->cap; i++, sd++) {
2758 sd->tag_idx = fl->tag_idx;
2759 rc = bus_dmamap_create(tag, 0, &sd->map);
2768 bus_dmamap_destroy(tag, sd->map);
2770 m_init(sd->m, NULL, 0, M_NOWAIT, MT_DATA, 0);
2775 KASSERT(sd == fl->sdesc, ("%s: EDOOFUS", __func__));
2777 free(fl->sdesc, M_CXGBE);
2784 free_fl_sdesc(struct sge_fl *fl)
2786 struct fl_sdesc *sd;
2789 FL_LOCK_ASSERT_OWNED(fl);
2792 for (i = 0; i < fl->cap; i++, sd++) {
2795 m_init(sd->m, NULL, 0, M_NOWAIT, MT_DATA, 0);
2801 bus_dmamap_unload(fl->tag[sd->tag_idx], sd->map);
2802 uma_zfree(FL_BUF_ZONE(sd->tag_idx), sd->cl);
2806 bus_dmamap_destroy(fl->tag[sd->tag_idx], sd->map);
2809 free(fl->sdesc, M_CXGBE);
2814 t4_alloc_tx_maps(struct tx_maps *txmaps, bus_dma_tag_t tx_tag, int count,
2820 txmaps->map_total = txmaps->map_avail = count;
2821 txmaps->map_cidx = txmaps->map_pidx = 0;
2823 txmaps->maps = malloc(count * sizeof(struct tx_map), M_CXGBE,
2827 for (i = 0; i < count; i++, txm++) {
2828 rc = bus_dmamap_create(tx_tag, 0, &txm->map);
2837 bus_dmamap_destroy(tx_tag, txm->map);
2839 KASSERT(txm == txmaps->maps, ("%s: EDOOFUS", __func__));
2841 free(txmaps->maps, M_CXGBE);
2842 txmaps->maps = NULL;
2848 t4_free_tx_maps(struct tx_maps *txmaps, bus_dma_tag_t tx_tag)
2854 for (i = 0; i < txmaps->map_total; i++, txm++) {
2857 bus_dmamap_unload(tx_tag, txm->map);
2862 bus_dmamap_destroy(tx_tag, txm->map);
2865 free(txmaps->maps, M_CXGBE);
2866 txmaps->maps = NULL;
2870 * We'll do immediate data tx for non-TSO, but only when not coalescing. We're
2871 * willing to use upto 2 hardware descriptors which means a maximum of 96 bytes
2872 * of immediate data.
2876 - sizeof(struct fw_eth_tx_pkt_wr) \
2877 - sizeof(struct cpl_tx_pkt_core))
2880 * Returns non-zero on failure, no need to cleanup anything in that case.
2882 * Note 1: We always try to defrag the mbuf if required and return EFBIG only
2883 * if the resulting chain still won't fit in a tx descriptor.
2885 * Note 2: We'll pullup the mbuf chain if TSO is requested and the first mbuf
2886 * does not have the TCP header in it.
2889 get_pkt_sgl(struct sge_txq *txq, struct mbuf **fp, struct sgl *sgl,
2892 struct mbuf *m = *fp;
2893 struct tx_maps *txmaps;
2895 int rc, defragged = 0, n;
2897 TXQ_LOCK_ASSERT_OWNED(txq);
2899 if (m->m_pkthdr.tso_segsz)
2900 sgl_only = 1; /* Do not allow immediate data with LSO */
2902 start: sgl->nsegs = 0;
2904 if (m->m_pkthdr.len <= IMM_LEN && !sgl_only)
2905 return (0); /* nsegs = 0 tells caller to use imm. tx */
2907 txmaps = &txq->txmaps;
2908 if (txmaps->map_avail == 0) {
2912 txm = &txmaps->maps[txmaps->map_pidx];
2914 if (m->m_pkthdr.tso_segsz && m->m_len < 50) {
2915 *fp = m_pullup(m, 50);
2921 rc = bus_dmamap_load_mbuf_sg(txq->tx_tag, txm->map, m, sgl->seg,
2922 &sgl->nsegs, BUS_DMA_NOWAIT);
2923 if (rc == EFBIG && defragged == 0) {
2924 m = m_defrag(m, M_NOWAIT);
2936 txmaps->map_avail--;
2937 if (++txmaps->map_pidx == txmaps->map_total)
2938 txmaps->map_pidx = 0;
2940 KASSERT(sgl->nsegs > 0 && sgl->nsegs <= TX_SGL_SEGS,
2941 ("%s: bad DMA mapping (%d segments)", __func__, sgl->nsegs));
2944 * Store the # of flits required to hold this frame's SGL in nflits. An
2945 * SGL has a (ULPTX header + len0, addr0) tuple optionally followed by
2946 * multiple (len0 + len1, addr0, addr1) tuples. If addr1 is not used
2947 * then len1 must be set to 0.
2950 sgl->nflits = (3 * n) / 2 + (n & 1) + 2;
2957 * Releases all the txq resources used up in the specified sgl.
2960 free_pkt_sgl(struct sge_txq *txq, struct sgl *sgl)
2962 struct tx_maps *txmaps;
2965 TXQ_LOCK_ASSERT_OWNED(txq);
2967 if (sgl->nsegs == 0)
2968 return (0); /* didn't use any map */
2970 txmaps = &txq->txmaps;
2972 /* 1 pkt uses exactly 1 map, back it out */
2974 txmaps->map_avail++;
2975 if (txmaps->map_pidx > 0)
2978 txmaps->map_pidx = txmaps->map_total - 1;
2980 txm = &txmaps->maps[txmaps->map_pidx];
2981 bus_dmamap_unload(txq->tx_tag, txm->map);
2988 write_txpkt_wr(struct port_info *pi, struct sge_txq *txq, struct mbuf *m,
2991 struct sge_eq *eq = &txq->eq;
2992 struct fw_eth_tx_pkt_wr *wr;
2993 struct cpl_tx_pkt_core *cpl;
2994 uint32_t ctrl; /* used in many unrelated places */
2996 int nflits, ndesc, pktlen;
2997 struct tx_sdesc *txsd;
3000 TXQ_LOCK_ASSERT_OWNED(txq);
3002 pktlen = m->m_pkthdr.len;
3005 * Do we have enough flits to send this frame out?
3007 ctrl = sizeof(struct cpl_tx_pkt_core);
3008 if (m->m_pkthdr.tso_segsz) {
3009 nflits = TXPKT_LSO_WR_HDR;
3010 ctrl += sizeof(struct cpl_tx_pkt_lso_core);
3012 nflits = TXPKT_WR_HDR;
3014 nflits += sgl->nflits;
3016 nflits += howmany(pktlen, 8);
3019 ndesc = howmany(nflits, 8);
3020 if (ndesc > eq->avail)
3023 /* Firmware work request header */
3024 wr = (void *)&eq->desc[eq->pidx];
3025 wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_PKT_WR) |
3026 V_FW_ETH_TX_PKT_WR_IMMDLEN(ctrl));
3027 ctrl = V_FW_WR_LEN16(howmany(nflits, 2));
3028 if (eq->avail == ndesc) {
3029 if (!(eq->flags & EQ_CRFLUSHED)) {
3030 ctrl |= F_FW_WR_EQUEQ | F_FW_WR_EQUIQ;
3031 eq->flags |= EQ_CRFLUSHED;
3033 eq->flags |= EQ_STALLED;
3036 wr->equiq_to_len16 = htobe32(ctrl);
3039 if (m->m_pkthdr.tso_segsz) {
3040 struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
3041 struct ether_header *eh;
3043 #if defined(INET) || defined(INET6)
3048 ctrl = V_LSO_OPCODE(CPL_TX_PKT_LSO) | F_LSO_FIRST_SLICE |
3051 eh = mtod(m, struct ether_header *);
3052 eh_type = ntohs(eh->ether_type);
3053 if (eh_type == ETHERTYPE_VLAN) {
3054 struct ether_vlan_header *evh = (void *)eh;
3056 ctrl |= V_LSO_ETHHDR_LEN(1);
3058 eh_type = ntohs(evh->evl_proto);
3064 case ETHERTYPE_IPV6:
3066 struct ip6_hdr *ip6 = l3hdr;
3069 * XXX-BZ For now we do not pretend to support
3070 * IPv6 extension headers.
3072 KASSERT(ip6->ip6_nxt == IPPROTO_TCP, ("%s: CSUM_TSO "
3073 "with ip6_nxt != TCP: %u", __func__, ip6->ip6_nxt));
3074 tcp = (struct tcphdr *)(ip6 + 1);
3076 ctrl |= V_LSO_IPHDR_LEN(sizeof(*ip6) >> 2) |
3077 V_LSO_TCPHDR_LEN(tcp->th_off);
3084 struct ip *ip = l3hdr;
3086 tcp = (void *)((uintptr_t)ip + ip->ip_hl * 4);
3087 ctrl |= V_LSO_IPHDR_LEN(ip->ip_hl) |
3088 V_LSO_TCPHDR_LEN(tcp->th_off);
3093 panic("%s: CSUM_TSO but no supported IP version "
3094 "(0x%04x)", __func__, eh_type);
3097 lso->lso_ctrl = htobe32(ctrl);
3098 lso->ipid_ofst = htobe16(0);
3099 lso->mss = htobe16(m->m_pkthdr.tso_segsz);
3100 lso->seqno_offset = htobe32(0);
3101 lso->len = htobe32(pktlen);
3103 cpl = (void *)(lso + 1);
3107 cpl = (void *)(wr + 1);
3109 /* Checksum offload */
3111 if (!(m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TSO)))
3112 ctrl1 |= F_TXPKT_IPCSUM_DIS;
3113 if (!(m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP | CSUM_UDP_IPV6 |
3114 CSUM_TCP_IPV6 | CSUM_TSO)))
3115 ctrl1 |= F_TXPKT_L4CSUM_DIS;
3116 if (m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP |
3117 CSUM_UDP_IPV6 | CSUM_TCP_IPV6 | CSUM_TSO))
3118 txq->txcsum++; /* some hardware assistance provided */
3120 /* VLAN tag insertion */
3121 if (m->m_flags & M_VLANTAG) {
3122 ctrl1 |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(m->m_pkthdr.ether_vtag);
3123 txq->vlan_insertion++;
3127 cpl->ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT) |
3128 V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(pi->adapter->pf));
3130 cpl->len = htobe16(pktlen);
3131 cpl->ctrl1 = htobe64(ctrl1);
3133 /* Software descriptor */
3134 txsd = &txq->sdesc[eq->pidx];
3135 txsd->desc_used = ndesc;
3137 eq->pending += ndesc;
3140 if (eq->pidx >= eq->cap)
3141 eq->pidx -= eq->cap;
3144 dst = (void *)(cpl + 1);
3145 if (sgl->nsegs > 0) {
3148 write_sgl_to_txd(eq, sgl, &dst);
3152 for (; m; m = m->m_next) {
3153 copy_to_txd(eq, mtod(m, caddr_t), &dst, m->m_len);
3159 KASSERT(pktlen == 0, ("%s: %d bytes left.", __func__, pktlen));
3169 * Returns 0 to indicate that m has been accepted into a coalesced tx work
3170 * request. It has either been folded into txpkts or txpkts was flushed and m
3171 * has started a new coalesced work request (as the first frame in a fresh
3174 * Returns non-zero to indicate a failure - caller is responsible for
3175 * transmitting m, if there was anything in txpkts it has been flushed.
3178 add_to_txpkts(struct port_info *pi, struct sge_txq *txq, struct txpkts *txpkts,
3179 struct mbuf *m, struct sgl *sgl)
3181 struct sge_eq *eq = &txq->eq;
3183 struct tx_sdesc *txsd;
3186 TXQ_LOCK_ASSERT_OWNED(txq);
3188 KASSERT(sgl->nsegs, ("%s: can't coalesce imm data", __func__));
3190 if (txpkts->npkt > 0) {
3191 flits = TXPKTS_PKT_HDR + sgl->nflits;
3192 can_coalesce = m->m_pkthdr.tso_segsz == 0 &&
3193 txpkts->nflits + flits <= TX_WR_FLITS &&
3194 txpkts->nflits + flits <= eq->avail * 8 &&
3195 txpkts->plen + m->m_pkthdr.len < 65536;
3199 txpkts->nflits += flits;
3200 txpkts->plen += m->m_pkthdr.len;
3202 txsd = &txq->sdesc[eq->pidx];
3209 * Couldn't coalesce m into txpkts. The first order of business
3210 * is to send txpkts on its way. Then we'll revisit m.
3212 write_txpkts_wr(txq, txpkts);
3216 * Check if we can start a new coalesced tx work request with m as
3217 * the first packet in it.
3220 KASSERT(txpkts->npkt == 0, ("%s: txpkts not empty", __func__));
3222 flits = TXPKTS_WR_HDR + sgl->nflits;
3223 can_coalesce = m->m_pkthdr.tso_segsz == 0 &&
3224 flits <= eq->avail * 8 && flits <= TX_WR_FLITS;
3226 if (can_coalesce == 0)
3230 * Start a fresh coalesced tx WR with m as the first frame in it.
3233 txpkts->nflits = flits;
3234 txpkts->flitp = &eq->desc[eq->pidx].flit[2];
3235 txpkts->plen = m->m_pkthdr.len;
3237 txsd = &txq->sdesc[eq->pidx];
3244 * Note that write_txpkts_wr can never run out of hardware descriptors (but
3245 * write_txpkt_wr can). add_to_txpkts ensures that a frame is accepted for
3246 * coalescing only if sufficient hardware descriptors are available.
3249 write_txpkts_wr(struct sge_txq *txq, struct txpkts *txpkts)
3251 struct sge_eq *eq = &txq->eq;
3252 struct fw_eth_tx_pkts_wr *wr;
3253 struct tx_sdesc *txsd;
3257 TXQ_LOCK_ASSERT_OWNED(txq);
3259 ndesc = howmany(txpkts->nflits, 8);
3261 wr = (void *)&eq->desc[eq->pidx];
3262 wr->op_pkd = htobe32(V_FW_WR_OP(FW_ETH_TX_PKTS_WR));
3263 ctrl = V_FW_WR_LEN16(howmany(txpkts->nflits, 2));
3264 if (eq->avail == ndesc) {
3265 if (!(eq->flags & EQ_CRFLUSHED)) {
3266 ctrl |= F_FW_WR_EQUEQ | F_FW_WR_EQUIQ;
3267 eq->flags |= EQ_CRFLUSHED;
3269 eq->flags |= EQ_STALLED;
3271 wr->equiq_to_len16 = htobe32(ctrl);
3272 wr->plen = htobe16(txpkts->plen);
3273 wr->npkt = txpkts->npkt;
3274 wr->r3 = wr->type = 0;
3276 /* Everything else already written */
3278 txsd = &txq->sdesc[eq->pidx];
3279 txsd->desc_used = ndesc;
3281 KASSERT(eq->avail >= ndesc, ("%s: out of descriptors", __func__));
3283 eq->pending += ndesc;
3286 if (eq->pidx >= eq->cap)
3287 eq->pidx -= eq->cap;
3289 txq->txpkts_pkts += txpkts->npkt;
3291 txpkts->npkt = 0; /* emptied */
3295 write_ulp_cpl_sgl(struct port_info *pi, struct sge_txq *txq,
3296 struct txpkts *txpkts, struct mbuf *m, struct sgl *sgl)
3298 struct ulp_txpkt *ulpmc;
3299 struct ulptx_idata *ulpsc;
3300 struct cpl_tx_pkt_core *cpl;
3301 struct sge_eq *eq = &txq->eq;
3302 uintptr_t flitp, start, end;
3306 KASSERT(txpkts->npkt > 0, ("%s: txpkts is empty", __func__));
3308 start = (uintptr_t)eq->desc;
3309 end = (uintptr_t)eq->spg;
3311 /* Checksum offload */
3313 if (!(m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TSO)))
3314 ctrl |= F_TXPKT_IPCSUM_DIS;
3315 if (!(m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP | CSUM_UDP_IPV6 |
3316 CSUM_TCP_IPV6 | CSUM_TSO)))
3317 ctrl |= F_TXPKT_L4CSUM_DIS;
3318 if (m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP |
3319 CSUM_UDP_IPV6 | CSUM_TCP_IPV6 | CSUM_TSO))
3320 txq->txcsum++; /* some hardware assistance provided */
3322 /* VLAN tag insertion */
3323 if (m->m_flags & M_VLANTAG) {
3324 ctrl |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(m->m_pkthdr.ether_vtag);
3325 txq->vlan_insertion++;
3329 * The previous packet's SGL must have ended at a 16 byte boundary (this
3330 * is required by the firmware/hardware). It follows that flitp cannot
3331 * wrap around between the ULPTX master command and ULPTX subcommand (8
3332 * bytes each), and that it can not wrap around in the middle of the
3333 * cpl_tx_pkt_core either.
3335 flitp = (uintptr_t)txpkts->flitp;
3336 KASSERT((flitp & 0xf) == 0,
3337 ("%s: last SGL did not end at 16 byte boundary: %p",
3338 __func__, txpkts->flitp));
3340 /* ULP master command */
3341 ulpmc = (void *)flitp;
3342 ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0) |
3343 V_ULP_TXPKT_FID(eq->iqid));
3344 ulpmc->len = htonl(howmany(sizeof(*ulpmc) + sizeof(*ulpsc) +
3345 sizeof(*cpl) + 8 * sgl->nflits, 16));
3347 /* ULP subcommand */
3348 ulpsc = (void *)(ulpmc + 1);
3349 ulpsc->cmd_more = htobe32(V_ULPTX_CMD((u32)ULP_TX_SC_IMM) |
3351 ulpsc->len = htobe32(sizeof(struct cpl_tx_pkt_core));
3353 flitp += sizeof(*ulpmc) + sizeof(*ulpsc);
3358 cpl = (void *)flitp;
3359 cpl->ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT) |
3360 V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(pi->adapter->pf));
3362 cpl->len = htobe16(m->m_pkthdr.len);
3363 cpl->ctrl1 = htobe64(ctrl);
3365 flitp += sizeof(*cpl);
3369 /* SGL for this frame */
3370 dst = (caddr_t)flitp;
3371 txpkts->nflits += write_sgl_to_txd(eq, sgl, &dst);
3372 txpkts->flitp = (void *)dst;
3374 KASSERT(((uintptr_t)dst & 0xf) == 0,
3375 ("%s: SGL ends at %p (not a 16 byte boundary)", __func__, dst));
3379 * If the SGL ends on an address that is not 16 byte aligned, this function will
3380 * add a 0 filled flit at the end. It returns 1 in that case.
3383 write_sgl_to_txd(struct sge_eq *eq, struct sgl *sgl, caddr_t *to)
3385 __be64 *flitp, *end;
3386 struct ulptx_sgl *usgl;
3387 bus_dma_segment_t *seg;
3390 KASSERT(sgl->nsegs > 0 && sgl->nflits > 0,
3391 ("%s: bad SGL - nsegs=%d, nflits=%d",
3392 __func__, sgl->nsegs, sgl->nflits));
3394 KASSERT(((uintptr_t)(*to) & 0xf) == 0,
3395 ("%s: SGL must start at a 16 byte boundary: %p", __func__, *to));
3397 flitp = (__be64 *)(*to);
3398 end = flitp + sgl->nflits;
3400 usgl = (void *)flitp;
3403 * We start at a 16 byte boundary somewhere inside the tx descriptor
3404 * ring, so we're at least 16 bytes away from the status page. There is
3405 * no chance of a wrap around in the middle of usgl (which is 16 bytes).
3408 usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
3409 V_ULPTX_NSGE(sgl->nsegs));
3410 usgl->len0 = htobe32(seg->ds_len);
3411 usgl->addr0 = htobe64(seg->ds_addr);
3414 if ((uintptr_t)end <= (uintptr_t)eq->spg) {
3416 /* Won't wrap around at all */
3418 for (i = 0; i < sgl->nsegs - 1; i++, seg++) {
3419 usgl->sge[i / 2].len[i & 1] = htobe32(seg->ds_len);
3420 usgl->sge[i / 2].addr[i & 1] = htobe64(seg->ds_addr);
3423 usgl->sge[i / 2].len[1] = htobe32(0);
3426 /* Will wrap somewhere in the rest of the SGL */
3428 /* 2 flits already written, write the rest flit by flit */
3429 flitp = (void *)(usgl + 1);
3430 for (i = 0; i < sgl->nflits - 2; i++) {
3431 if ((uintptr_t)flitp == (uintptr_t)eq->spg)
3432 flitp = (void *)eq->desc;
3433 *flitp++ = get_flit(seg, sgl->nsegs - 1, i);
3438 if ((uintptr_t)end & 0xf) {
3439 *(uint64_t *)end = 0;
3445 if ((uintptr_t)end == (uintptr_t)eq->spg)
3446 *to = (void *)eq->desc;
3454 copy_to_txd(struct sge_eq *eq, caddr_t from, caddr_t *to, int len)
3456 if (__predict_true((uintptr_t)(*to) + len <= (uintptr_t)eq->spg)) {
3457 bcopy(from, *to, len);
3460 int portion = (uintptr_t)eq->spg - (uintptr_t)(*to);
3462 bcopy(from, *to, portion);
3464 portion = len - portion; /* remaining */
3465 bcopy(from, (void *)eq->desc, portion);
3466 (*to) = (caddr_t)eq->desc + portion;
3471 ring_eq_db(struct adapter *sc, struct sge_eq *eq)
3476 pending = eq->pending;
3478 clrbit(&db, DOORBELL_WCWR);
3482 switch (ffs(db) - 1) {
3484 *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(pending));
3487 case DOORBELL_WCWR: {
3488 volatile uint64_t *dst, *src;
3492 * Queues whose 128B doorbell segment fits in the page do not
3493 * use relative qid (udb_qid is always 0). Only queues with
3494 * doorbell segments can do WCWR.
3496 KASSERT(eq->udb_qid == 0 && pending == 1,
3497 ("%s: inappropriate doorbell (0x%x, %d, %d) for eq %p",
3498 __func__, eq->doorbells, pending, eq->pidx, eq));
3500 dst = (volatile void *)((uintptr_t)eq->udb + UDBS_WR_OFFSET -
3502 i = eq->pidx ? eq->pidx - 1 : eq->cap - 1;
3503 src = (void *)&eq->desc[i];
3504 while (src != (void *)&eq->desc[i + 1])
3510 case DOORBELL_UDBWC:
3511 *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(pending));
3516 t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL),
3517 V_QID(eq->cntxt_id) | V_PIDX(pending));
3523 reclaimable(struct sge_eq *eq)
3527 cidx = eq->spg->cidx; /* stable snapshot */
3528 cidx = be16toh(cidx);
3530 if (cidx >= eq->cidx)
3531 return (cidx - eq->cidx);
3533 return (cidx + eq->cap - eq->cidx);
3537 * There are "can_reclaim" tx descriptors ready to be reclaimed. Reclaim as
3538 * many as possible but stop when there are around "n" mbufs to free.
3540 * The actual number reclaimed is provided as the return value.
3543 reclaim_tx_descs(struct sge_txq *txq, int can_reclaim, int n)
3545 struct tx_sdesc *txsd;
3546 struct tx_maps *txmaps;
3548 unsigned int reclaimed, maps;
3549 struct sge_eq *eq = &txq->eq;
3551 TXQ_LOCK_ASSERT_OWNED(txq);
3553 if (can_reclaim == 0)
3554 can_reclaim = reclaimable(eq);
3556 maps = reclaimed = 0;
3557 while (can_reclaim && maps < n) {
3560 txsd = &txq->sdesc[eq->cidx];
3561 ndesc = txsd->desc_used;
3563 /* Firmware doesn't return "partial" credits. */
3564 KASSERT(can_reclaim >= ndesc,
3565 ("%s: unexpected number of credits: %d, %d",
3566 __func__, can_reclaim, ndesc));
3568 maps += txsd->credits;
3571 can_reclaim -= ndesc;
3574 if (__predict_false(eq->cidx >= eq->cap))
3575 eq->cidx -= eq->cap;
3578 txmaps = &txq->txmaps;
3579 txm = &txmaps->maps[txmaps->map_cidx];
3583 eq->avail += reclaimed;
3584 KASSERT(eq->avail < eq->cap, /* avail tops out at (cap - 1) */
3585 ("%s: too many descriptors available", __func__));
3587 txmaps->map_avail += maps;
3588 KASSERT(txmaps->map_avail <= txmaps->map_total,
3589 ("%s: too many maps available", __func__));
3592 struct tx_map *next;
3595 if (__predict_false(txmaps->map_cidx + 1 == txmaps->map_total))
3596 next = txmaps->maps;
3599 bus_dmamap_unload(txq->tx_tag, txm->map);
3604 if (__predict_false(++txmaps->map_cidx == txmaps->map_total))
3605 txmaps->map_cidx = 0;
3612 write_eqflush_wr(struct sge_eq *eq)
3614 struct fw_eq_flush_wr *wr;
3616 EQ_LOCK_ASSERT_OWNED(eq);
3617 KASSERT(eq->avail > 0, ("%s: no descriptors left.", __func__));
3618 KASSERT(!(eq->flags & EQ_CRFLUSHED), ("%s: flushed already", __func__));
3620 wr = (void *)&eq->desc[eq->pidx];
3621 bzero(wr, sizeof(*wr));
3622 wr->opcode = FW_EQ_FLUSH_WR;
3623 wr->equiq_to_len16 = htobe32(V_FW_WR_LEN16(sizeof(*wr) / 16) |
3624 F_FW_WR_EQUEQ | F_FW_WR_EQUIQ);
3626 eq->flags |= (EQ_CRFLUSHED | EQ_STALLED);
3629 if (++eq->pidx == eq->cap)
3634 get_flit(bus_dma_segment_t *sgl, int nsegs, int idx)
3636 int i = (idx / 3) * 2;
3642 rc = htobe32(sgl[i].ds_len);
3644 rc |= (uint64_t)htobe32(sgl[i + 1].ds_len) << 32;
3649 return htobe64(sgl[i].ds_addr);
3651 return htobe64(sgl[i + 1].ds_addr);
3658 set_fl_tag_idx(struct sge_fl *fl, int bufsize)
3662 for (i = 0; i < FL_BUF_SIZES - 1; i++) {
3663 if (FL_BUF_SIZE(i) >= bufsize)
3671 add_fl_to_sfl(struct adapter *sc, struct sge_fl *fl)
3673 mtx_lock(&sc->sfl_lock);
3675 if ((fl->flags & FL_DOOMED) == 0) {
3676 fl->flags |= FL_STARVING;
3677 TAILQ_INSERT_TAIL(&sc->sfl, fl, link);
3678 callout_reset(&sc->sfl_callout, hz / 5, refill_sfl, sc);
3681 mtx_unlock(&sc->sfl_lock);
3685 handle_sge_egr_update(struct sge_iq *iq, const struct rss_header *rss,
3688 const struct cpl_sge_egr_update *cpl = (const void *)(rss + 1);
3689 unsigned int qid = G_EGR_QID(ntohl(cpl->opcode_qid));
3690 struct adapter *sc = iq->adapter;
3691 struct sge *s = &sc->sge;
3694 KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__,
3697 eq = s->eqmap[qid - s->eq_start];
3699 KASSERT(eq->flags & EQ_CRFLUSHED,
3700 ("%s: unsolicited egress update", __func__));
3701 eq->flags &= ~EQ_CRFLUSHED;
3704 if (__predict_false(eq->flags & EQ_DOOMED))
3706 else if (eq->flags & EQ_STALLED && can_resume_tx(eq))
3707 taskqueue_enqueue(sc->tq[eq->tx_chan], &eq->tx_task);
3713 /* handle_fw_msg works for both fw4_msg and fw6_msg because this is valid */
3714 CTASSERT(offsetof(struct cpl_fw4_msg, data) == \
3715 offsetof(struct cpl_fw6_msg, data));
3718 handle_fw_msg(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
3720 struct adapter *sc = iq->adapter;
3721 const struct cpl_fw6_msg *cpl = (const void *)(rss + 1);
3723 KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__,
3726 if (cpl->type == FW_TYPE_RSSCPL || cpl->type == FW6_TYPE_RSSCPL) {
3727 const struct rss_header *rss2;
3729 rss2 = (const struct rss_header *)&cpl->data[0];
3730 return (sc->cpl_handler[rss2->opcode](iq, rss2, m));
3733 return (sc->fw_msg_handler[cpl->type](sc, &cpl->data[0]));
3737 sysctl_uint16(SYSCTL_HANDLER_ARGS)
3739 uint16_t *id = arg1;
3742 return sysctl_handle_int(oidp, &i, 0, req);