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$");
33 #include <sys/param.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/malloc.h>
40 #include <sys/queue.h>
41 #include <sys/taskqueue.h>
42 #include <sys/pciio.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
45 #include <dev/pci/pci_private.h>
46 #include <sys/firmware.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
52 #include <net/ethernet.h>
54 #include <net/if_types.h>
55 #include <net/if_dl.h>
56 #include <net/if_vlan_var.h>
58 #include "common/t4_hw.h"
59 #include "common/common.h"
60 #include "common/t4_msg.h"
61 #include "common/t4_regs.h"
62 #include "common/t4_regs_values.h"
63 #include "common/t4fw_interface.h"
67 /* T4 bus driver interface */
68 static int t4_probe(device_t);
69 static int t4_attach(device_t);
70 static int t4_detach(device_t);
71 static device_method_t t4_methods[] = {
72 DEVMETHOD(device_probe, t4_probe),
73 DEVMETHOD(device_attach, t4_attach),
74 DEVMETHOD(device_detach, t4_detach),
78 static driver_t t4_driver = {
81 sizeof(struct adapter)
85 /* T4 port (cxgbe) interface */
86 static int cxgbe_probe(device_t);
87 static int cxgbe_attach(device_t);
88 static int cxgbe_detach(device_t);
89 static device_method_t cxgbe_methods[] = {
90 DEVMETHOD(device_probe, cxgbe_probe),
91 DEVMETHOD(device_attach, cxgbe_attach),
92 DEVMETHOD(device_detach, cxgbe_detach),
95 static driver_t cxgbe_driver = {
98 sizeof(struct port_info)
101 static d_ioctl_t t4_ioctl;
102 static d_open_t t4_open;
103 static d_close_t t4_close;
105 static struct cdevsw t4_cdevsw = {
106 .d_version = D_VERSION,
114 /* ifnet + media interface */
115 static void cxgbe_init(void *);
116 static int cxgbe_ioctl(struct ifnet *, unsigned long, caddr_t);
117 static void cxgbe_start(struct ifnet *);
118 static int cxgbe_transmit(struct ifnet *, struct mbuf *);
119 static void cxgbe_qflush(struct ifnet *);
120 static int cxgbe_media_change(struct ifnet *);
121 static void cxgbe_media_status(struct ifnet *, struct ifmediareq *);
123 MALLOC_DEFINE(M_CXGBE, "cxgbe", "Chelsio T4 Ethernet driver and services");
128 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD, 0, "cxgbe driver parameters");
130 static int force_firmware_install = 0;
131 TUNABLE_INT("hw.cxgbe.force_firmware_install", &force_firmware_install);
132 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, force_firmware_install, CTLFLAG_RDTUN,
133 &force_firmware_install, 0, "install firmware on every attach.");
136 * Holdoff timer and packet counter values.
138 static unsigned int intr_timer[SGE_NTIMERS] = {1, 5, 10, 50, 100, 200};
139 static unsigned int intr_pktcount[SGE_NCOUNTERS] = {1, 8, 16, 32}; /* 63 max */
142 * Max # of tx and rx queues to use for each 10G and 1G port.
144 static unsigned int max_ntxq_10g = 8;
145 TUNABLE_INT("hw.cxgbe.max_ntxq_10G_port", &max_ntxq_10g);
146 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, max_ntxq_10G_port, CTLFLAG_RDTUN,
147 &max_ntxq_10g, 0, "maximum number of tx queues per 10G port.");
149 static unsigned int max_nrxq_10g = 8;
150 TUNABLE_INT("hw.cxgbe.max_nrxq_10G_port", &max_nrxq_10g);
151 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, max_nrxq_10G_port, CTLFLAG_RDTUN,
152 &max_nrxq_10g, 0, "maximum number of rxq's (per 10G port).");
154 static unsigned int max_ntxq_1g = 2;
155 TUNABLE_INT("hw.cxgbe.max_ntxq_1G_port", &max_ntxq_1g);
156 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, max_ntxq_1G_port, CTLFLAG_RDTUN,
157 &max_ntxq_1g, 0, "maximum number of tx queues per 1G port.");
159 static unsigned int max_nrxq_1g = 2;
160 TUNABLE_INT("hw.cxgbe.max_nrxq_1G_port", &max_nrxq_1g);
161 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, max_nrxq_1G_port, CTLFLAG_RDTUN,
162 &max_nrxq_1g, 0, "maximum number of rxq's (per 1G port).");
165 * Holdoff parameters for 10G and 1G ports.
167 static unsigned int tmr_idx_10g = 1;
168 TUNABLE_INT("hw.cxgbe.holdoff_timer_idx_10G", &tmr_idx_10g);
169 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx_10G, CTLFLAG_RDTUN,
171 "default timer index for interrupt holdoff (10G ports).");
173 static int pktc_idx_10g = 2;
174 TUNABLE_INT("hw.cxgbe.holdoff_pktc_idx_10G", &pktc_idx_10g);
175 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx_10G, CTLFLAG_RDTUN,
177 "default pkt counter index for interrupt holdoff (10G ports).");
179 static unsigned int tmr_idx_1g = 1;
180 TUNABLE_INT("hw.cxgbe.holdoff_timer_idx_1G", &tmr_idx_1g);
181 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx_1G, CTLFLAG_RDTUN,
183 "default timer index for interrupt holdoff (1G ports).");
185 static int pktc_idx_1g = 2;
186 TUNABLE_INT("hw.cxgbe.holdoff_pktc_idx_1G", &pktc_idx_1g);
187 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx_1G, CTLFLAG_RDTUN,
189 "default pkt counter index for interrupt holdoff (1G ports).");
192 * Size (# of entries) of each tx and rx queue.
194 static unsigned int qsize_txq = TX_EQ_QSIZE;
195 TUNABLE_INT("hw.cxgbe.qsize_txq", &qsize_txq);
196 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, qsize_txq, CTLFLAG_RDTUN,
197 &qsize_txq, 0, "default queue size of NIC tx queues.");
199 static unsigned int qsize_rxq = RX_IQ_QSIZE;
200 TUNABLE_INT("hw.cxgbe.qsize_rxq", &qsize_rxq);
201 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, qsize_rxq, CTLFLAG_RDTUN,
202 &qsize_rxq, 0, "default queue size of NIC rx queues.");
205 * Interrupt types allowed.
207 static int intr_types = INTR_MSIX | INTR_MSI | INTR_INTX;
208 TUNABLE_INT("hw.cxgbe.interrupt_types", &intr_types);
209 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, interrupt_types, CTLFLAG_RDTUN, &intr_types, 0,
210 "interrupt types allowed (bits 0, 1, 2 = INTx, MSI, MSI-X respectively)");
213 * Force the driver to use the same set of interrupts for all ports.
215 static int intr_shared = 0;
216 TUNABLE_INT("hw.cxgbe.interrupts_shared", &intr_shared);
217 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, interrupts_shared, CTLFLAG_RDTUN,
218 &intr_shared, 0, "interrupts shared between all ports");
220 static unsigned int filter_mode = HW_TPL_FR_MT_PR_IV_P_FC;
221 TUNABLE_INT("hw.cxgbe.filter_mode", &filter_mode);
222 SYSCTL_UINT(_hw_cxgbe, OID_AUTO, filter_mode, CTLFLAG_RDTUN,
223 &filter_mode, 0, "default global filter mode.");
225 struct intrs_and_queues {
226 int intr_type; /* INTx, MSI, or MSI-X */
227 int nirq; /* Number of vectors */
228 int intr_shared; /* Interrupts shared between all ports */
229 int ntxq10g; /* # of NIC txq's for each 10G port */
230 int nrxq10g; /* # of NIC rxq's for each 10G port */
231 int ntxq1g; /* # of NIC txq's for each 1G port */
232 int nrxq1g; /* # of NIC rxq's for each 1G port */
235 struct filter_entry {
236 uint32_t valid:1; /* filter allocated and valid */
237 uint32_t locked:1; /* filter is administratively locked */
238 uint32_t pending:1; /* filter action is pending firmware reply */
239 uint32_t smtidx:8; /* Source MAC Table index for smac */
240 struct l2t_entry *l2t; /* Layer Two Table entry for dmac */
242 struct t4_filter_specification fs;
246 MEMWIN0_APERTURE = 2048,
247 MEMWIN0_BASE = 0x1b800,
248 MEMWIN1_APERTURE = 32768,
249 MEMWIN1_BASE = 0x28000,
250 MEMWIN2_APERTURE = 65536,
251 MEMWIN2_BASE = 0x30000,
255 XGMAC_MTU = (1 << 0),
256 XGMAC_PROMISC = (1 << 1),
257 XGMAC_ALLMULTI = (1 << 2),
258 XGMAC_VLANEX = (1 << 3),
259 XGMAC_UCADDR = (1 << 4),
260 XGMAC_MCADDRS = (1 << 5),
265 static int map_bars(struct adapter *);
266 static void setup_memwin(struct adapter *);
267 static int cfg_itype_and_nqueues(struct adapter *, int, int,
268 struct intrs_and_queues *);
269 static int prep_firmware(struct adapter *);
270 static int get_devlog_params(struct adapter *, struct devlog_params *);
271 static int get_capabilities(struct adapter *, struct fw_caps_config_cmd *);
272 static int get_params(struct adapter *, struct fw_caps_config_cmd *);
273 static void t4_set_desc(struct adapter *);
274 static void build_medialist(struct port_info *);
275 static int update_mac_settings(struct port_info *, int);
276 static int cxgbe_init_locked(struct port_info *);
277 static int cxgbe_init_synchronized(struct port_info *);
278 static int cxgbe_uninit_locked(struct port_info *);
279 static int cxgbe_uninit_synchronized(struct port_info *);
280 static int first_port_up(struct adapter *);
281 static int last_port_down(struct adapter *);
282 static int t4_alloc_irq(struct adapter *, struct irq *, int rid,
283 iq_intr_handler_t *, void *, char *);
284 static int t4_free_irq(struct adapter *, struct irq *);
285 static void reg_block_dump(struct adapter *, uint8_t *, unsigned int,
287 static void t4_get_regs(struct adapter *, struct t4_regdump *, uint8_t *);
288 static void cxgbe_tick(void *);
289 static int t4_sysctls(struct adapter *);
290 static int cxgbe_sysctls(struct port_info *);
291 static int sysctl_int_array(SYSCTL_HANDLER_ARGS);
292 static int sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS);
293 static int sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS);
294 static int sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS);
295 static int sysctl_qsize_txq(SYSCTL_HANDLER_ARGS);
296 static int sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS);
297 static int sysctl_devlog(SYSCTL_HANDLER_ARGS);
298 static inline void txq_start(struct ifnet *, struct sge_txq *);
299 static uint32_t fconf_to_mode(uint32_t);
300 static uint32_t mode_to_fconf(uint32_t);
301 static uint32_t fspec_to_fconf(struct t4_filter_specification *);
302 static int get_filter_mode(struct adapter *, uint32_t *);
303 static int set_filter_mode(struct adapter *, uint32_t);
304 static inline uint64_t get_filter_hits(struct adapter *, uint32_t);
305 static int get_filter(struct adapter *, struct t4_filter *);
306 static int set_filter(struct adapter *, struct t4_filter *);
307 static int del_filter(struct adapter *, struct t4_filter *);
308 static void clear_filter(struct filter_entry *);
309 static int set_filter_wr(struct adapter *, int);
310 static int del_filter_wr(struct adapter *, int);
311 void filter_rpl(struct adapter *, const struct cpl_set_tcb_rpl *);
312 static int get_sge_context(struct adapter *, struct t4_sge_context *);
313 static int t4_mod_event(module_t, int, void *);
320 {0xa000, 0, "Chelsio Terminator 4 FPGA"},
321 {0x4400, 4, "Chelsio T440-dbg"},
322 {0x4401, 4, "Chelsio T420-CR"},
323 {0x4402, 4, "Chelsio T422-CR"},
324 {0x4403, 4, "Chelsio T440-CR"},
325 {0x4404, 4, "Chelsio T420-BCH"},
326 {0x4405, 4, "Chelsio T440-BCH"},
327 {0x4406, 4, "Chelsio T440-CH"},
328 {0x4407, 4, "Chelsio T420-SO"},
329 {0x4408, 4, "Chelsio T420-CX"},
330 {0x4409, 4, "Chelsio T420-BT"},
331 {0x440a, 4, "Chelsio T404-BT"},
335 t4_probe(device_t dev)
338 uint16_t v = pci_get_vendor(dev);
339 uint16_t d = pci_get_device(dev);
341 if (v != PCI_VENDOR_ID_CHELSIO)
344 for (i = 0; i < ARRAY_SIZE(t4_pciids); i++) {
345 if (d == t4_pciids[i].device &&
346 pci_get_function(dev) == t4_pciids[i].mpf) {
347 device_set_desc(dev, t4_pciids[i].desc);
348 return (BUS_PROBE_DEFAULT);
356 t4_attach(device_t dev)
359 int rc = 0, i, n10g, n1g, rqidx, tqidx;
360 struct fw_caps_config_cmd caps;
362 struct intrs_and_queues iaq;
365 sc = device_get_softc(dev);
367 sc->pf = pci_get_function(dev);
370 pci_enable_busmaster(dev);
371 if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
372 pci_set_max_read_req(dev, 4096);
373 v = pci_read_config(dev, i + PCIR_EXPRESS_DEVICE_CTL, 2);
374 v |= PCIM_EXP_CTL_RELAXED_ORD_ENABLE;
375 pci_write_config(dev, i + PCIR_EXPRESS_DEVICE_CTL, v, 2);
378 snprintf(sc->lockname, sizeof(sc->lockname), "%s",
379 device_get_nameunit(dev));
380 mtx_init(&sc->sc_lock, sc->lockname, 0, MTX_DEF);
384 goto done; /* error message displayed already */
386 memset(sc->chan_map, 0xff, sizeof(sc->chan_map));
388 /* Prepare the adapter for operation */
389 rc = -t4_prep_adapter(sc);
391 device_printf(dev, "failed to prepare adapter: %d.\n", rc);
395 /* Do this really early */
396 sc->cdev = make_dev(&t4_cdevsw, device_get_unit(dev), UID_ROOT,
397 GID_WHEEL, 0600, "%s", device_get_nameunit(dev));
398 sc->cdev->si_drv1 = sc;
400 /* Prepare the firmware for operation */
401 rc = prep_firmware(sc);
403 goto done; /* error message displayed already */
405 /* Read firmware devlog parameters */
406 (void) get_devlog_params(sc, &sc->params.devlog);
408 /* Get device capabilities and select which ones we'll use */
409 rc = get_capabilities(sc, &caps);
412 "failed to initialize adapter capabilities: %d.\n", rc);
416 /* Choose the global RSS mode. */
417 rc = -t4_config_glbl_rss(sc, sc->mbox,
418 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
419 F_FW_RSS_GLB_CONFIG_CMD_TNLMAPEN |
420 F_FW_RSS_GLB_CONFIG_CMD_HASHTOEPLITZ |
421 F_FW_RSS_GLB_CONFIG_CMD_TNLALLLKP);
424 "failed to select global RSS mode: %d.\n", rc);
428 /* These are total (sum of all ports) limits for a bus driver */
429 rc = -t4_cfg_pfvf(sc, sc->mbox, sc->pf, 0,
430 128, /* max # of egress queues */
431 64, /* max # of egress Ethernet or control queues */
432 64, /* max # of ingress queues with fl/interrupt */
433 0, /* max # of ingress queues without interrupt */
434 0, /* PCIe traffic class */
435 4, /* max # of virtual interfaces */
436 M_FW_PFVF_CMD_CMASK, M_FW_PFVF_CMD_PMASK, 16,
437 FW_CMD_CAP_PF, FW_CMD_CAP_PF);
440 "failed to configure pf/vf resources: %d.\n", rc);
444 /* Need this before sge_init */
445 for (i = 0; i < SGE_NTIMERS; i++)
446 sc->sge.timer_val[i] = min(intr_timer[i], 200U);
447 for (i = 0; i < SGE_NCOUNTERS; i++)
448 sc->sge.counter_val[i] = min(intr_pktcount[i], M_THRESHOLD_0);
450 /* Also need the cooked value of cclk before sge_init */
451 p = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
452 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_CCLK));
453 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &p, &v);
455 device_printf(sc->dev,
456 "failed to obtain core clock value: %d.\n", rc);
459 sc->params.vpd.cclk = v;
463 t4_set_filter_mode(sc, filter_mode);
464 t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG,
465 V_FIVETUPLELOOKUP(M_FIVETUPLELOOKUP),
466 V_FIVETUPLELOOKUP(M_FIVETUPLELOOKUP));
467 t4_tp_wr_bits_indirect(sc, A_TP_INGRESS_CONFIG, F_CSUM_HAS_PSEUDO_HDR,
470 /* get basic stuff going */
471 rc = -t4_early_init(sc, sc->mbox);
473 device_printf(dev, "early init failed: %d.\n", rc);
477 rc = get_params(sc, &caps);
479 goto done; /* error message displayed already */
481 /* These are finalized by FW initialization, load their values now */
482 v = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
483 sc->params.tp.tre = G_TIMERRESOLUTION(v);
484 sc->params.tp.dack_re = G_DELAYEDACKRESOLUTION(v);
485 t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
487 /* tweak some settings */
488 t4_write_reg(sc, A_TP_SHIFT_CNT, V_SYNSHIFTMAX(6) | V_RXTSHIFTMAXR1(4) |
489 V_RXTSHIFTMAXR2(15) | V_PERSHIFTBACKOFFMAX(8) | V_PERSHIFTMAX(8) |
490 V_KEEPALIVEMAXR1(4) | V_KEEPALIVEMAXR2(9));
491 t4_write_reg(sc, A_ULP_RX_TDDP_PSZ, V_HPZ0(PAGE_SHIFT - 12));
492 t4_set_reg_field(sc, A_TP_PARA_REG3, F_TUNNELCNGDROP0 |
493 F_TUNNELCNGDROP1 | F_TUNNELCNGDROP2 | F_TUNNELCNGDROP3, 0);
497 rc = t4_create_dma_tag(sc);
499 goto done; /* error message displayed already */
502 * First pass over all the ports - allocate VIs and initialize some
503 * basic parameters like mac address, port type, etc. We also figure
504 * out whether a port is 10G or 1G and use that information when
505 * calculating how many interrupts to attempt to allocate.
508 for_each_port(sc, i) {
509 struct port_info *pi;
511 pi = malloc(sizeof(*pi), M_CXGBE, M_ZERO | M_WAITOK);
514 /* These must be set before t4_port_init */
518 /* Allocate the vi and initialize parameters like mac addr */
519 rc = -t4_port_init(pi, sc->mbox, sc->pf, 0);
521 device_printf(dev, "unable to initialize port %d: %d\n",
528 snprintf(pi->lockname, sizeof(pi->lockname), "%sp%d",
529 device_get_nameunit(dev), i);
530 mtx_init(&pi->pi_lock, pi->lockname, 0, MTX_DEF);
532 if (is_10G_port(pi)) {
534 pi->tmr_idx = tmr_idx_10g;
535 pi->pktc_idx = pktc_idx_10g;
538 pi->tmr_idx = tmr_idx_1g;
539 pi->pktc_idx = pktc_idx_1g;
542 pi->xact_addr_filt = -1;
544 pi->qsize_rxq = max(qsize_rxq, 128);
545 while (pi->qsize_rxq & 7)
547 pi->qsize_txq = max(qsize_txq, 128);
549 if (pi->qsize_rxq != qsize_rxq) {
551 "using %d instead of %d as the rx queue size.\n",
552 pi->qsize_rxq, qsize_rxq);
554 if (pi->qsize_txq != qsize_txq) {
556 "using %d instead of %d as the tx queue size.\n",
557 pi->qsize_txq, qsize_txq);
560 pi->dev = device_add_child(dev, "cxgbe", -1);
561 if (pi->dev == NULL) {
563 "failed to add device for port %d.\n", i);
567 device_set_softc(pi->dev, pi);
569 setbit(&sc->registered_device_map, i);
572 if (sc->registered_device_map == 0) {
573 device_printf(dev, "no usable ports\n");
579 * Interrupt type, # of interrupts, # of rx/tx queues, etc.
581 rc = cfg_itype_and_nqueues(sc, n10g, n1g, &iaq);
583 goto done; /* error message displayed already */
585 sc->intr_type = iaq.intr_type;
586 sc->intr_count = iaq.nirq;
589 s->nrxq = n10g * iaq.nrxq10g + n1g * iaq.nrxq1g;
590 s->ntxq = n10g * iaq.ntxq10g + n1g * iaq.ntxq1g;
591 s->neq = s->ntxq + s->nrxq; /* the free list in an rxq is an eq */
592 s->neq += sc->params.nports; /* control queues, 1 per port */
593 s->niq = s->nrxq + 1; /* 1 extra for firmware event queue */
595 sc->flags |= INTR_SHARED;
596 s->niq += NINTRQ(sc); /* interrupt queues */
598 s->intrq = malloc(NINTRQ(sc) * sizeof(struct sge_iq), M_CXGBE,
600 s->ctrlq = malloc(sc->params.nports * sizeof(struct sge_ctrlq), M_CXGBE,
602 s->rxq = malloc(s->nrxq * sizeof(struct sge_rxq), M_CXGBE,
604 s->txq = malloc(s->ntxq * sizeof(struct sge_txq), M_CXGBE,
606 s->iqmap = malloc(s->niq * sizeof(struct sge_iq *), M_CXGBE,
608 s->eqmap = malloc(s->neq * sizeof(struct sge_eq *), M_CXGBE,
611 sc->irq = malloc(sc->intr_count * sizeof(struct irq), M_CXGBE,
614 sc->l2t = t4_init_l2t(M_WAITOK);
619 * Second pass over the ports. This time we know the number of rx and
620 * tx queues that each port should get.
623 for_each_port(sc, i) {
624 struct port_info *pi = sc->port[i];
629 pi->first_rxq = rqidx;
630 pi->nrxq = is_10G_port(pi) ? iaq.nrxq10g : iaq.nrxq1g;
632 pi->first_txq = tqidx;
633 pi->ntxq = is_10G_port(pi) ? iaq.ntxq10g : iaq.ntxq1g;
639 rc = bus_generic_attach(dev);
642 "failed to attach all child ports: %d\n", rc);
648 "%p, %d ports (0x%x), %d intr_type, %d intr_count\n",
649 sc, sc->params.nports, sc->params.portvec,
650 sc->intr_type, sc->intr_count);
665 t4_detach(device_t dev)
668 struct port_info *pi;
671 sc = device_get_softc(dev);
674 destroy_dev(sc->cdev);
676 bus_generic_detach(dev);
677 for (i = 0; i < MAX_NPORTS; i++) {
680 t4_free_vi(pi->adapter, sc->mbox, sc->pf, 0, pi->viid);
682 device_delete_child(dev, pi->dev);
684 mtx_destroy(&pi->pi_lock);
689 if (sc->flags & FW_OK)
690 t4_fw_bye(sc, sc->mbox);
692 if (sc->intr_type == INTR_MSI || sc->intr_type == INTR_MSIX)
693 pci_release_msi(dev);
696 bus_release_resource(dev, SYS_RES_MEMORY, sc->regs_rid,
700 bus_release_resource(dev, SYS_RES_MEMORY, sc->msix_rid,
704 t4_free_l2t(sc->l2t);
706 free(sc->irq, M_CXGBE);
707 free(sc->sge.rxq, M_CXGBE);
708 free(sc->sge.txq, M_CXGBE);
709 free(sc->sge.ctrlq, M_CXGBE);
710 free(sc->sge.intrq, M_CXGBE);
711 free(sc->sge.iqmap, M_CXGBE);
712 free(sc->sge.eqmap, M_CXGBE);
713 free(sc->tids.ftid_tab, M_CXGBE);
714 t4_destroy_dma_tag(sc);
715 mtx_destroy(&sc->sc_lock);
717 bzero(sc, sizeof(*sc));
724 cxgbe_probe(device_t dev)
727 struct port_info *pi = device_get_softc(dev);
729 snprintf(buf, sizeof(buf), "Port %d", pi->port_id);
730 device_set_desc_copy(dev, buf);
732 return (BUS_PROBE_DEFAULT);
735 #define T4_CAP (IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | \
736 IFCAP_VLAN_HWCSUM | IFCAP_TSO | IFCAP_JUMBO_MTU | IFCAP_LRO | \
738 #define T4_CAP_ENABLE (T4_CAP & ~IFCAP_TSO6)
741 cxgbe_attach(device_t dev)
743 struct port_info *pi = device_get_softc(dev);
746 /* Allocate an ifnet and set it up */
747 ifp = if_alloc(IFT_ETHER);
749 device_printf(dev, "Cannot allocate ifnet\n");
755 callout_init(&pi->tick, CALLOUT_MPSAFE);
756 pi->tq = taskqueue_create("cxgbe_taskq", M_NOWAIT,
757 taskqueue_thread_enqueue, &pi->tq);
758 if (pi->tq == NULL) {
759 device_printf(dev, "failed to allocate port task queue\n");
763 taskqueue_start_threads(&pi->tq, 1, PI_NET, "%s taskq",
764 device_get_nameunit(dev));
766 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
767 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
769 ifp->if_init = cxgbe_init;
770 ifp->if_ioctl = cxgbe_ioctl;
771 ifp->if_start = cxgbe_start;
772 ifp->if_transmit = cxgbe_transmit;
773 ifp->if_qflush = cxgbe_qflush;
775 ifp->if_snd.ifq_drv_maxlen = 1024;
776 IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
777 IFQ_SET_READY(&ifp->if_snd);
779 ifp->if_capabilities = T4_CAP;
780 ifp->if_capenable = T4_CAP_ENABLE;
781 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO;
783 /* Initialize ifmedia for this port */
784 ifmedia_init(&pi->media, IFM_IMASK, cxgbe_media_change,
788 ether_ifattach(ifp, pi->hw_addr);
791 device_printf(dev, "%p, %d txq, %d rxq\n", pi, pi->ntxq, pi->nrxq);
800 cxgbe_detach(device_t dev)
802 struct port_info *pi = device_get_softc(dev);
803 struct adapter *sc = pi->adapter;
806 /* Tell if_ioctl and if_init that the port is going away */
811 mtx_sleep(&sc->flags, &sc->sc_lock, 0, "t4detach", 0);
815 rc = cxgbe_uninit_synchronized(pi);
817 device_printf(dev, "port uninit failed: %d.\n", rc);
819 taskqueue_free(pi->tq);
821 ifmedia_removeall(&pi->media);
822 ether_ifdetach(pi->ifp);
827 wakeup_one(&sc->flags);
834 cxgbe_init(void *arg)
836 struct port_info *pi = arg;
837 struct adapter *sc = pi->adapter;
840 cxgbe_init_locked(pi); /* releases adapter lock */
841 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
845 cxgbe_ioctl(struct ifnet *ifp, unsigned long cmd, caddr_t data)
847 int rc = 0, mtu, flags;
848 struct port_info *pi = ifp->if_softc;
849 struct adapter *sc = pi->adapter;
850 struct ifreq *ifr = (struct ifreq *)data;
856 rc = IS_DOOMED(pi) ? ENXIO : (IS_BUSY(sc) ? EBUSY : 0);
864 if ((mtu < ETHERMIN) || (mtu > ETHERMTU_JUMBO)) {
868 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
869 t4_update_fl_bufsize(ifp);
871 rc = update_mac_settings(pi, XGMAC_MTU);
884 if (ifp->if_flags & IFF_UP) {
885 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
886 flags = pi->if_flags;
887 if ((ifp->if_flags ^ flags) &
888 (IFF_PROMISC | IFF_ALLMULTI)) {
894 rc = update_mac_settings(pi,
895 XGMAC_PROMISC | XGMAC_ALLMULTI);
900 rc = cxgbe_init_locked(pi);
901 pi->if_flags = ifp->if_flags;
902 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING)
903 rc = cxgbe_uninit_locked(pi);
907 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
911 case SIOCDELMULTI: /* these two can be called with a mutex held :-( */
913 rc = IS_DOOMED(pi) ? ENXIO : (IS_BUSY(sc) ? EBUSY : 0);
917 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
919 rc = update_mac_settings(pi, XGMAC_MCADDRS);
927 rc = IS_DOOMED(pi) ? ENXIO : (IS_BUSY(sc) ? EBUSY : 0);
931 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
932 if (mask & IFCAP_TXCSUM) {
933 ifp->if_capenable ^= IFCAP_TXCSUM;
934 ifp->if_hwassist ^= (CSUM_TCP | CSUM_UDP | CSUM_IP);
936 if (IFCAP_TSO & ifp->if_capenable &&
937 !(IFCAP_TXCSUM & ifp->if_capenable)) {
938 ifp->if_capenable &= ~IFCAP_TSO;
939 ifp->if_hwassist &= ~CSUM_TSO;
941 "tso disabled due to -txcsum.\n");
944 if (mask & IFCAP_RXCSUM)
945 ifp->if_capenable ^= IFCAP_RXCSUM;
946 if (mask & IFCAP_TSO4) {
947 ifp->if_capenable ^= IFCAP_TSO4;
949 if (IFCAP_TSO & ifp->if_capenable) {
950 if (IFCAP_TXCSUM & ifp->if_capenable)
951 ifp->if_hwassist |= CSUM_TSO;
953 ifp->if_capenable &= ~IFCAP_TSO;
954 ifp->if_hwassist &= ~CSUM_TSO;
956 "enable txcsum first.\n");
960 ifp->if_hwassist &= ~CSUM_TSO;
962 if (mask & IFCAP_LRO) {
967 ifp->if_capenable ^= IFCAP_LRO;
968 for_each_rxq(pi, i, rxq) {
969 if (ifp->if_capenable & IFCAP_LRO)
970 rxq->flags |= RXQ_LRO_ENABLED;
972 rxq->flags &= ~RXQ_LRO_ENABLED;
976 #ifndef TCP_OFFLOAD_DISABLE
977 if (mask & IFCAP_TOE4) {
981 if (mask & IFCAP_VLAN_HWTAGGING) {
982 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
983 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
985 rc = update_mac_settings(pi, XGMAC_VLANEX);
989 if (mask & IFCAP_VLAN_MTU) {
990 ifp->if_capenable ^= IFCAP_VLAN_MTU;
992 /* Need to find out how to disable auto-mtu-inflation */
994 if (mask & IFCAP_VLAN_HWTSO)
995 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
996 if (mask & IFCAP_VLAN_HWCSUM)
997 ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
999 #ifdef VLAN_CAPABILITIES
1000 VLAN_CAPABILITIES(ifp);
1007 ifmedia_ioctl(ifp, ifr, &pi->media, cmd);
1011 rc = ether_ioctl(ifp, cmd, data);
1018 cxgbe_start(struct ifnet *ifp)
1020 struct port_info *pi = ifp->if_softc;
1021 struct sge_txq *txq;
1024 for_each_txq(pi, i, txq) {
1025 if (TXQ_TRYLOCK(txq)) {
1026 txq_start(ifp, txq);
1033 cxgbe_transmit(struct ifnet *ifp, struct mbuf *m)
1035 struct port_info *pi = ifp->if_softc;
1036 struct adapter *sc = pi->adapter;
1037 struct sge_txq *txq = &sc->sge.txq[pi->first_txq];
1038 struct buf_ring *br;
1043 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1048 if (m->m_flags & M_FLOWID)
1049 txq += (m->m_pkthdr.flowid % pi->ntxq);
1052 if (TXQ_TRYLOCK(txq) == 0) {
1054 * XXX: make sure that this packet really is sent out. There is
1055 * a small race where t4_eth_tx may stop draining the drbr and
1056 * goes away, just before we enqueued this mbuf.
1059 return (drbr_enqueue(ifp, br, m));
1063 * txq->m is the mbuf that is held up due to a temporary shortage of
1064 * resources and it should be put on the wire first. Then what's in
1065 * drbr and finally the mbuf that was just passed in to us.
1067 * Return code should indicate the fate of the mbuf that was passed in
1071 TXQ_LOCK_ASSERT_OWNED(txq);
1072 if (drbr_needs_enqueue(ifp, br) || txq->m) {
1074 /* Queued for transmission. */
1076 rc = drbr_enqueue(ifp, br, m);
1077 m = txq->m ? txq->m : drbr_dequeue(ifp, br);
1078 (void) t4_eth_tx(ifp, txq, m);
1083 /* Direct transmission. */
1084 rc = t4_eth_tx(ifp, txq, m);
1085 if (rc != 0 && txq->m)
1086 rc = 0; /* held, will be transmitted soon (hopefully) */
1093 cxgbe_qflush(struct ifnet *ifp)
1095 struct port_info *pi = ifp->if_softc;
1096 struct sge_txq *txq;
1100 /* queues do not exist if !IFF_DRV_RUNNING. */
1101 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1102 for_each_txq(pi, i, txq) {
1105 while ((m = buf_ring_dequeue_sc(txq->br)) != NULL)
1114 cxgbe_media_change(struct ifnet *ifp)
1116 struct port_info *pi = ifp->if_softc;
1118 device_printf(pi->dev, "%s unimplemented.\n", __func__);
1120 return (EOPNOTSUPP);
1124 cxgbe_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1126 struct port_info *pi = ifp->if_softc;
1127 struct ifmedia_entry *cur = pi->media.ifm_cur;
1128 int speed = pi->link_cfg.speed;
1129 int data = (pi->port_type << 8) | pi->mod_type;
1131 if (cur->ifm_data != data) {
1132 build_medialist(pi);
1133 cur = pi->media.ifm_cur;
1136 ifmr->ifm_status = IFM_AVALID;
1137 if (!pi->link_cfg.link_ok)
1140 ifmr->ifm_status |= IFM_ACTIVE;
1142 /* active and current will differ iff current media is autoselect. */
1143 if (IFM_SUBTYPE(cur->ifm_media) != IFM_AUTO)
1146 ifmr->ifm_active = IFM_ETHER | IFM_FDX;
1147 if (speed == SPEED_10000)
1148 ifmr->ifm_active |= IFM_10G_T;
1149 else if (speed == SPEED_1000)
1150 ifmr->ifm_active |= IFM_1000_T;
1151 else if (speed == SPEED_100)
1152 ifmr->ifm_active |= IFM_100_TX;
1153 else if (speed == SPEED_10)
1154 ifmr->ifm_active |= IFM_10_T;
1156 KASSERT(0, ("%s: link up but speed unknown (%u)", __func__,
1161 t4_fatal_err(struct adapter *sc)
1163 t4_set_reg_field(sc, A_SGE_CONTROL, F_GLOBALENABLE, 0);
1164 t4_intr_disable(sc);
1165 log(LOG_EMERG, "%s: encountered fatal error, adapter stopped.\n",
1166 device_get_nameunit(sc->dev));
1170 map_bars(struct adapter *sc)
1172 sc->regs_rid = PCIR_BAR(0);
1173 sc->regs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
1174 &sc->regs_rid, RF_ACTIVE);
1175 if (sc->regs_res == NULL) {
1176 device_printf(sc->dev, "cannot map registers.\n");
1179 sc->bt = rman_get_bustag(sc->regs_res);
1180 sc->bh = rman_get_bushandle(sc->regs_res);
1181 sc->mmio_len = rman_get_size(sc->regs_res);
1183 sc->msix_rid = PCIR_BAR(4);
1184 sc->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
1185 &sc->msix_rid, RF_ACTIVE);
1186 if (sc->msix_res == NULL) {
1187 device_printf(sc->dev, "cannot map MSI-X BAR.\n");
1195 setup_memwin(struct adapter *sc)
1199 bar0 = rman_get_start(sc->regs_res);
1201 t4_write_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 0),
1202 (bar0 + MEMWIN0_BASE) | V_BIR(0) |
1203 V_WINDOW(ilog2(MEMWIN0_APERTURE) - 10));
1205 t4_write_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 1),
1206 (bar0 + MEMWIN1_BASE) | V_BIR(0) |
1207 V_WINDOW(ilog2(MEMWIN1_APERTURE) - 10));
1209 t4_write_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 2),
1210 (bar0 + MEMWIN2_BASE) | V_BIR(0) |
1211 V_WINDOW(ilog2(MEMWIN2_APERTURE) - 10));
1215 cfg_itype_and_nqueues(struct adapter *sc, int n10g, int n1g,
1216 struct intrs_and_queues *iaq)
1218 int rc, itype, navail, nc, nrxq10g, nrxq1g;
1220 bzero(iaq, sizeof(*iaq));
1221 nc = mp_ncpus; /* our snapshot of the number of CPUs */
1223 for (itype = INTR_MSIX; itype; itype >>= 1) {
1225 if ((itype & intr_types) == 0)
1226 continue; /* not allowed */
1228 if (itype == INTR_MSIX)
1229 navail = pci_msix_count(sc->dev);
1230 else if (itype == INTR_MSI)
1231 navail = pci_msi_count(sc->dev);
1238 iaq->intr_type = itype;
1240 iaq->ntxq10g = min(nc, max_ntxq_10g);
1241 iaq->ntxq1g = min(nc, max_ntxq_1g);
1243 nrxq10g = min(nc, max_nrxq_10g);
1244 nrxq1g = min(nc, max_nrxq_1g);
1246 iaq->nirq = n10g * nrxq10g + n1g * nrxq1g + T4_EXTRA_INTR;
1247 if (iaq->nirq <= navail && intr_shared == 0) {
1249 if (itype == INTR_MSI && !powerof2(iaq->nirq))
1252 /* One for err, one for fwq, and one for each rxq */
1254 iaq->intr_shared = 0;
1255 iaq->nrxq10g = nrxq10g;
1256 iaq->nrxq1g = nrxq1g;
1260 iaq->intr_shared = 1;
1262 if (navail >= nc + T4_EXTRA_INTR) {
1263 if (itype == INTR_MSIX)
1264 navail = nc + T4_EXTRA_INTR;
1266 /* navail is and must remain a pow2 for MSI */
1267 if (itype == INTR_MSI) {
1268 KASSERT(powerof2(navail),
1269 ("%d not power of 2", navail));
1271 while (navail / 2 >= nc + T4_EXTRA_INTR)
1275 iaq->nirq = navail; /* total # of interrupts */
1278 * If we have multiple vectors available reserve one
1279 * exclusively for errors. The rest will be shared by
1284 iaq->nrxq10g = min(nrxq10g, navail);
1285 iaq->nrxq1g = min(nrxq1g, navail);
1290 if (itype == INTR_MSIX)
1291 rc = pci_alloc_msix(sc->dev, &navail);
1292 else if (itype == INTR_MSI)
1293 rc = pci_alloc_msi(sc->dev, &navail);
1296 if (navail == iaq->nirq)
1300 * Didn't get the number requested. Use whatever number
1301 * the kernel is willing to allocate (it's in navail).
1303 pci_release_msi(sc->dev);
1307 device_printf(sc->dev,
1308 "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
1309 itype, rc, iaq->nirq, navail);
1312 device_printf(sc->dev,
1313 "failed to find a usable interrupt type. "
1314 "allowed=%d, msi-x=%d, msi=%d, intx=1", intr_types,
1315 pci_msix_count(sc->dev), pci_msi_count(sc->dev));
1321 * Install a compatible firmware (if required), establish contact with it,
1322 * become the master, and reset the device.
1325 prep_firmware(struct adapter *sc)
1327 const struct firmware *fw;
1329 enum dev_state state;
1331 /* Check firmware version and install a different one if necessary */
1332 rc = t4_check_fw_version(sc);
1333 if (rc != 0 || force_firmware_install) {
1336 fw = firmware_get(T4_FWNAME);
1338 const struct fw_hdr *hdr = (const void *)fw->data;
1340 v = ntohl(hdr->fw_ver);
1343 * The firmware module will not be used if it isn't the
1344 * same major version as what the driver was compiled
1345 * with. This check trumps force_firmware_install.
1347 if (G_FW_HDR_FW_VER_MAJOR(v) != FW_VERSION_MAJOR) {
1348 device_printf(sc->dev,
1349 "Found firmware image but version %d "
1350 "can not be used with this driver (%d)\n",
1351 G_FW_HDR_FW_VER_MAJOR(v), FW_VERSION_MAJOR);
1353 firmware_put(fw, FIRMWARE_UNLOAD);
1358 if (fw == NULL && (rc < 0 || force_firmware_install)) {
1359 device_printf(sc->dev, "No usable firmware. "
1360 "card has %d.%d.%d, driver compiled with %d.%d.%d, "
1361 "force_firmware_install%s set",
1362 G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
1363 G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
1364 G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
1365 FW_VERSION_MAJOR, FW_VERSION_MINOR,
1367 force_firmware_install ? "" : " not");
1372 * Always upgrade, even for minor/micro/build mismatches.
1373 * Downgrade only for a major version mismatch or if
1374 * force_firmware_install was specified.
1376 if (fw != NULL && (rc < 0 || force_firmware_install ||
1377 v > sc->params.fw_vers)) {
1378 device_printf(sc->dev,
1379 "installing firmware %d.%d.%d.%d on card.\n",
1380 G_FW_HDR_FW_VER_MAJOR(v), G_FW_HDR_FW_VER_MINOR(v),
1381 G_FW_HDR_FW_VER_MICRO(v), G_FW_HDR_FW_VER_BUILD(v));
1383 rc = -t4_load_fw(sc, fw->data, fw->datasize);
1385 device_printf(sc->dev,
1386 "failed to install firmware: %d\n", rc);
1387 firmware_put(fw, FIRMWARE_UNLOAD);
1391 (void) t4_check_fw_version(sc);
1396 firmware_put(fw, FIRMWARE_UNLOAD);
1399 /* Contact firmware, request master */
1400 rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MUST, &state);
1403 device_printf(sc->dev,
1404 "failed to connect to the firmware: %d.\n", rc);
1409 rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
1411 device_printf(sc->dev, "firmware reset failed: %d.\n", rc);
1412 if (rc != ETIMEDOUT && rc != EIO)
1413 t4_fw_bye(sc, sc->mbox);
1417 snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
1418 G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
1419 G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
1420 G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
1421 G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
1428 get_devlog_params(struct adapter *sc, struct devlog_params *dlog)
1430 struct fw_devlog_cmd devlog_cmd;
1434 bzero(&devlog_cmd, sizeof(devlog_cmd));
1435 devlog_cmd.op_to_write = htobe32(V_FW_CMD_OP(FW_DEVLOG_CMD) |
1436 F_FW_CMD_REQUEST | F_FW_CMD_READ);
1437 devlog_cmd.retval_len16 = htobe32(FW_LEN16(devlog_cmd));
1438 rc = -t4_wr_mbox(sc, sc->mbox, &devlog_cmd, sizeof(devlog_cmd),
1441 device_printf(sc->dev,
1442 "failed to get devlog parameters: %d.\n", rc);
1443 bzero(dlog, sizeof (*dlog));
1447 meminfo = be32toh(devlog_cmd.memtype_devlog_memaddr16_devlog);
1448 dlog->memtype = G_FW_DEVLOG_CMD_MEMTYPE_DEVLOG(meminfo);
1449 dlog->start = G_FW_DEVLOG_CMD_MEMADDR16_DEVLOG(meminfo) << 4;
1450 dlog->size = be32toh(devlog_cmd.memsize_devlog);
1456 get_capabilities(struct adapter *sc, struct fw_caps_config_cmd *caps)
1460 bzero(caps, sizeof(*caps));
1461 caps->op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
1462 F_FW_CMD_REQUEST | F_FW_CMD_READ);
1463 caps->retval_len16 = htobe32(FW_LEN16(*caps));
1465 rc = -t4_wr_mbox(sc, sc->mbox, caps, sizeof(*caps), caps);
1469 if (caps->niccaps & htobe16(FW_CAPS_CONFIG_NIC_VM))
1470 caps->niccaps ^= htobe16(FW_CAPS_CONFIG_NIC_VM);
1472 caps->op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
1473 F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
1474 rc = -t4_wr_mbox(sc, sc->mbox, caps, sizeof(*caps), NULL);
1480 get_params(struct adapter *sc, struct fw_caps_config_cmd *caps)
1483 uint32_t params[7], val[7];
1485 #define FW_PARAM_DEV(param) \
1486 (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
1487 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
1488 #define FW_PARAM_PFVF(param) \
1489 (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
1490 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
1492 params[0] = FW_PARAM_DEV(PORTVEC);
1493 params[1] = FW_PARAM_PFVF(IQFLINT_START);
1494 params[2] = FW_PARAM_PFVF(EQ_START);
1495 params[3] = FW_PARAM_PFVF(FILTER_START);
1496 params[4] = FW_PARAM_PFVF(FILTER_END);
1497 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 5, params, val);
1499 device_printf(sc->dev,
1500 "failed to query parameters: %d.\n", rc);
1504 sc->params.portvec = val[0];
1505 sc->params.nports = 0;
1507 sc->params.nports++;
1508 val[0] &= val[0] - 1;
1511 sc->sge.iq_start = val[1];
1512 sc->sge.eq_start = val[2];
1513 sc->tids.ftid_base = val[3];
1514 sc->tids.nftids = val[4] - val[3] + 1;
1516 if (caps->toecaps) {
1517 /* query offload-related parameters */
1518 params[0] = FW_PARAM_DEV(NTID);
1519 params[1] = FW_PARAM_PFVF(SERVER_START);
1520 params[2] = FW_PARAM_PFVF(SERVER_END);
1521 params[3] = FW_PARAM_PFVF(TDDP_START);
1522 params[4] = FW_PARAM_PFVF(TDDP_END);
1523 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
1524 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, params, val);
1526 device_printf(sc->dev,
1527 "failed to query TOE parameters: %d.\n", rc);
1530 sc->tids.ntids = val[0];
1531 sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
1532 sc->tids.stid_base = val[1];
1533 sc->tids.nstids = val[2] - val[1] + 1;
1534 sc->vres.ddp.start = val[3];
1535 sc->vres.ddp.size = val[4] - val[3] + 1;
1536 sc->params.ofldq_wr_cred = val[5];
1537 sc->params.offload = 1;
1539 if (caps->rdmacaps) {
1540 params[0] = FW_PARAM_PFVF(STAG_START);
1541 params[1] = FW_PARAM_PFVF(STAG_END);
1542 params[2] = FW_PARAM_PFVF(RQ_START);
1543 params[3] = FW_PARAM_PFVF(RQ_END);
1544 params[4] = FW_PARAM_PFVF(PBL_START);
1545 params[5] = FW_PARAM_PFVF(PBL_END);
1546 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, params, val);
1548 device_printf(sc->dev,
1549 "failed to query RDMA parameters: %d.\n", rc);
1552 sc->vres.stag.start = val[0];
1553 sc->vres.stag.size = val[1] - val[0] + 1;
1554 sc->vres.rq.start = val[2];
1555 sc->vres.rq.size = val[3] - val[2] + 1;
1556 sc->vres.pbl.start = val[4];
1557 sc->vres.pbl.size = val[5] - val[4] + 1;
1559 if (caps->iscsicaps) {
1560 params[0] = FW_PARAM_PFVF(ISCSI_START);
1561 params[1] = FW_PARAM_PFVF(ISCSI_END);
1562 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, params, val);
1564 device_printf(sc->dev,
1565 "failed to query iSCSI parameters: %d.\n", rc);
1568 sc->vres.iscsi.start = val[0];
1569 sc->vres.iscsi.size = val[1] - val[0] + 1;
1571 #undef FW_PARAM_PFVF
1579 t4_set_desc(struct adapter *sc)
1582 struct adapter_params *p = &sc->params;
1584 snprintf(buf, sizeof(buf),
1585 "Chelsio %s (rev %d) %d port %sNIC PCIe-x%d %d %s, S/N:%s, E/C:%s",
1586 p->vpd.id, p->rev, p->nports, is_offload(sc) ? "R" : "",
1587 p->pci.width, sc->intr_count, sc->intr_type == INTR_MSIX ? "MSI-X" :
1588 (sc->intr_type == INTR_MSI ? "MSI" : "INTx"), p->vpd.sn, p->vpd.ec);
1590 device_set_desc_copy(sc->dev, buf);
1594 build_medialist(struct port_info *pi)
1596 struct ifmedia *media = &pi->media;
1601 ifmedia_removeall(media);
1603 m = IFM_ETHER | IFM_FDX;
1604 data = (pi->port_type << 8) | pi->mod_type;
1606 switch(pi->port_type) {
1607 case FW_PORT_TYPE_BT_XFI:
1608 ifmedia_add(media, m | IFM_10G_T, data, NULL);
1611 case FW_PORT_TYPE_BT_XAUI:
1612 ifmedia_add(media, m | IFM_10G_T, data, NULL);
1615 case FW_PORT_TYPE_BT_SGMII:
1616 ifmedia_add(media, m | IFM_1000_T, data, NULL);
1617 ifmedia_add(media, m | IFM_100_TX, data, NULL);
1618 ifmedia_add(media, IFM_ETHER | IFM_AUTO, data, NULL);
1619 ifmedia_set(media, IFM_ETHER | IFM_AUTO);
1622 case FW_PORT_TYPE_CX4:
1623 ifmedia_add(media, m | IFM_10G_CX4, data, NULL);
1624 ifmedia_set(media, m | IFM_10G_CX4);
1627 case FW_PORT_TYPE_SFP:
1628 case FW_PORT_TYPE_FIBER_XFI:
1629 case FW_PORT_TYPE_FIBER_XAUI:
1630 switch (pi->mod_type) {
1632 case FW_PORT_MOD_TYPE_LR:
1633 ifmedia_add(media, m | IFM_10G_LR, data, NULL);
1634 ifmedia_set(media, m | IFM_10G_LR);
1637 case FW_PORT_MOD_TYPE_SR:
1638 ifmedia_add(media, m | IFM_10G_SR, data, NULL);
1639 ifmedia_set(media, m | IFM_10G_SR);
1642 case FW_PORT_MOD_TYPE_LRM:
1643 ifmedia_add(media, m | IFM_10G_LRM, data, NULL);
1644 ifmedia_set(media, m | IFM_10G_LRM);
1647 case FW_PORT_MOD_TYPE_TWINAX_PASSIVE:
1648 case FW_PORT_MOD_TYPE_TWINAX_ACTIVE:
1649 ifmedia_add(media, m | IFM_10G_TWINAX, data, NULL);
1650 ifmedia_set(media, m | IFM_10G_TWINAX);
1653 case FW_PORT_MOD_TYPE_NONE:
1655 ifmedia_add(media, m | IFM_NONE, data, NULL);
1656 ifmedia_set(media, m | IFM_NONE);
1659 case FW_PORT_MOD_TYPE_NA:
1660 case FW_PORT_MOD_TYPE_ER:
1662 ifmedia_add(media, m | IFM_UNKNOWN, data, NULL);
1663 ifmedia_set(media, m | IFM_UNKNOWN);
1668 case FW_PORT_TYPE_KX4:
1669 case FW_PORT_TYPE_KX:
1670 case FW_PORT_TYPE_KR:
1672 ifmedia_add(media, m | IFM_UNKNOWN, data, NULL);
1673 ifmedia_set(media, m | IFM_UNKNOWN);
1681 * Program the port's XGMAC based on parameters in ifnet. The caller also
1682 * indicates which parameters should be programmed (the rest are left alone).
1685 update_mac_settings(struct port_info *pi, int flags)
1688 struct ifnet *ifp = pi->ifp;
1689 struct adapter *sc = pi->adapter;
1690 int mtu = -1, promisc = -1, allmulti = -1, vlanex = -1;
1692 PORT_LOCK_ASSERT_OWNED(pi);
1693 KASSERT(flags, ("%s: not told what to update.", __func__));
1695 if (flags & XGMAC_MTU)
1698 if (flags & XGMAC_PROMISC)
1699 promisc = ifp->if_flags & IFF_PROMISC ? 1 : 0;
1701 if (flags & XGMAC_ALLMULTI)
1702 allmulti = ifp->if_flags & IFF_ALLMULTI ? 1 : 0;
1704 if (flags & XGMAC_VLANEX)
1705 vlanex = ifp->if_capenable & IFCAP_VLAN_HWTAGGING ? 1 : 0;
1707 rc = -t4_set_rxmode(sc, sc->mbox, pi->viid, mtu, promisc, allmulti, 1,
1710 if_printf(ifp, "set_rxmode (%x) failed: %d\n", flags, rc);
1714 if (flags & XGMAC_UCADDR) {
1715 uint8_t ucaddr[ETHER_ADDR_LEN];
1717 bcopy(IF_LLADDR(ifp), ucaddr, sizeof(ucaddr));
1718 rc = t4_change_mac(sc, sc->mbox, pi->viid, pi->xact_addr_filt,
1719 ucaddr, true, true);
1722 if_printf(ifp, "change_mac failed: %d\n", rc);
1725 pi->xact_addr_filt = rc;
1730 if (flags & XGMAC_MCADDRS) {
1731 const uint8_t *mcaddr;
1734 struct ifmultiaddr *ifma;
1736 if_maddr_rlock(ifp);
1737 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1738 if (ifma->ifma_addr->sa_family != AF_LINK)
1740 mcaddr = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
1742 rc = t4_alloc_mac_filt(sc, sc->mbox, pi->viid, del, 1,
1743 &mcaddr, NULL, &hash, 0);
1746 if_printf(ifp, "failed to add mc address"
1747 " %02x:%02x:%02x:%02x:%02x:%02x rc=%d\n",
1748 mcaddr[0], mcaddr[1], mcaddr[2], mcaddr[3],
1749 mcaddr[4], mcaddr[5], rc);
1755 rc = -t4_set_addr_hash(sc, sc->mbox, pi->viid, 0, hash, 0);
1757 if_printf(ifp, "failed to set mc address hash: %d", rc);
1759 if_maddr_runlock(ifp);
1766 cxgbe_init_locked(struct port_info *pi)
1768 struct adapter *sc = pi->adapter;
1771 ADAPTER_LOCK_ASSERT_OWNED(sc);
1773 while (!IS_DOOMED(pi) && IS_BUSY(sc)) {
1774 if (mtx_sleep(&sc->flags, &sc->sc_lock, PCATCH, "t4init", 0)) {
1779 if (IS_DOOMED(pi)) {
1783 KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
1785 /* Give up the adapter lock, port init code can sleep. */
1789 rc = cxgbe_init_synchronized(pi);
1793 KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
1795 wakeup_one(&sc->flags);
1801 cxgbe_init_synchronized(struct port_info *pi)
1803 struct adapter *sc = pi->adapter;
1804 struct ifnet *ifp = pi->ifp;
1807 struct sge_rxq *rxq;
1809 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
1811 if (isset(&sc->open_device_map, pi->port_id)) {
1812 KASSERT(ifp->if_drv_flags & IFF_DRV_RUNNING,
1813 ("mismatch between open_device_map and if_drv_flags"));
1814 return (0); /* already running */
1817 if (sc->open_device_map == 0 && ((rc = first_port_up(sc)) != 0))
1818 return (rc); /* error message displayed already */
1821 * Allocate tx/rx/fl queues for this port.
1823 rc = t4_setup_eth_queues(pi);
1825 goto done; /* error message displayed already */
1828 * Setup RSS for this port.
1830 rss = malloc(pi->nrxq * sizeof (*rss), M_CXGBE, M_ZERO | M_WAITOK);
1831 for_each_rxq(pi, i, rxq) {
1832 rss[i] = rxq->iq.abs_id;
1834 rc = -t4_config_rss_range(sc, sc->mbox, pi->viid, 0, pi->rss_size, rss,
1838 if_printf(ifp, "rss_config failed: %d\n", rc);
1843 rc = update_mac_settings(pi, XGMAC_ALL);
1846 goto done; /* error message displayed already */
1848 rc = -t4_link_start(sc, sc->mbox, pi->tx_chan, &pi->link_cfg);
1850 if_printf(ifp, "start_link failed: %d\n", rc);
1854 rc = -t4_enable_vi(sc, sc->mbox, pi->viid, true, true);
1856 if_printf(ifp, "enable_vi failed: %d\n", rc);
1859 pi->flags |= VI_ENABLED;
1862 setbit(&sc->open_device_map, pi->port_id);
1863 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1864 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1866 callout_reset(&pi->tick, hz, cxgbe_tick, pi);
1869 cxgbe_uninit_synchronized(pi);
1875 cxgbe_uninit_locked(struct port_info *pi)
1877 struct adapter *sc = pi->adapter;
1880 ADAPTER_LOCK_ASSERT_OWNED(sc);
1882 while (!IS_DOOMED(pi) && IS_BUSY(sc)) {
1883 if (mtx_sleep(&sc->flags, &sc->sc_lock, PCATCH, "t4uninit", 0)) {
1888 if (IS_DOOMED(pi)) {
1892 KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
1896 rc = cxgbe_uninit_synchronized(pi);
1899 KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
1901 wakeup_one(&sc->flags);
1911 cxgbe_uninit_synchronized(struct port_info *pi)
1913 struct adapter *sc = pi->adapter;
1914 struct ifnet *ifp = pi->ifp;
1918 * taskqueue_drain may cause a deadlock if the adapter lock is held.
1920 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
1923 * Clear this port's bit from the open device map, and then drain
1924 * tasks and callouts.
1926 clrbit(&sc->open_device_map, pi->port_id);
1929 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1930 callout_stop(&pi->tick);
1932 callout_drain(&pi->tick);
1935 * Stop and then free the queues' resources, including the queues
1938 * XXX: we could just stop the queues here (on ifconfig down) and free
1939 * them later (on port detach), but having up/down go through the entire
1940 * allocate/activate/deactivate/free sequence is a good way to find
1943 rc = t4_teardown_eth_queues(pi);
1945 if_printf(ifp, "teardown failed: %d\n", rc);
1947 if (pi->flags & VI_ENABLED) {
1948 rc = -t4_enable_vi(sc, sc->mbox, pi->viid, false, false);
1950 if_printf(ifp, "disable_vi failed: %d\n", rc);
1952 pi->flags &= ~VI_ENABLED;
1955 pi->link_cfg.link_ok = 0;
1956 pi->link_cfg.speed = 0;
1957 t4_os_link_changed(sc, pi->port_id, 0);
1959 if (sc->open_device_map == 0)
1965 #define T4_ALLOC_IRQ(sc, irq, rid, handler, arg, name) do { \
1966 rc = t4_alloc_irq(sc, irq, rid, handler, arg, name); \
1971 first_port_up(struct adapter *sc)
1973 int rc, i, rid, p, q;
1976 struct sge_iq *intrq;
1978 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
1981 * queues that belong to the adapter (not any particular port).
1983 rc = t4_setup_adapter_queues(sc);
1991 rid = sc->intr_type == INTR_INTX ? 0 : 1;
1992 if (sc->intr_count == 1) {
1993 KASSERT(sc->flags & INTR_SHARED,
1994 ("%s: single interrupt but not shared?", __func__));
1996 T4_ALLOC_IRQ(sc, irq, rid, t4_intr_all, sc, "all");
1998 /* Multiple interrupts. The first one is always error intr */
1999 T4_ALLOC_IRQ(sc, irq, rid, t4_intr_err, sc, "err");
2003 /* Firmware event queue normally has an interrupt of its own */
2004 if (sc->intr_count > T4_EXTRA_INTR) {
2005 T4_ALLOC_IRQ(sc, irq, rid, t4_intr_evt, &sc->sge.fwq,
2011 intrq = &sc->sge.intrq[0];
2012 if (sc->flags & INTR_SHARED) {
2014 /* All ports share these interrupt queues */
2016 for (i = 0; i < NINTRQ(sc); i++) {
2017 snprintf(s, sizeof(s), "*.%d", i);
2018 T4_ALLOC_IRQ(sc, irq, rid, t4_intr, intrq, s);
2025 /* Each port has its own set of interrupt queues */
2027 for (p = 0; p < sc->params.nports; p++) {
2028 for (q = 0; q < sc->port[p]->nrxq; q++) {
2029 snprintf(s, sizeof(s), "%d.%d", p, q);
2030 T4_ALLOC_IRQ(sc, irq, rid, t4_intr,
2041 sc->flags |= FULL_INIT_DONE;
2055 last_port_down(struct adapter *sc)
2059 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
2061 t4_intr_disable(sc);
2063 t4_teardown_adapter_queues(sc);
2065 for (i = 0; i < sc->intr_count; i++)
2066 t4_free_irq(sc, &sc->irq[i]);
2068 sc->flags &= ~FULL_INIT_DONE;
2074 t4_alloc_irq(struct adapter *sc, struct irq *irq, int rid,
2075 iq_intr_handler_t *handler, void *arg, char *name)
2080 irq->res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &irq->rid,
2081 RF_SHAREABLE | RF_ACTIVE);
2082 if (irq->res == NULL) {
2083 device_printf(sc->dev,
2084 "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
2088 rc = bus_setup_intr(sc->dev, irq->res, INTR_MPSAFE | INTR_TYPE_NET,
2089 NULL, handler, arg, &irq->tag);
2091 device_printf(sc->dev,
2092 "failed to setup interrupt for rid %d, name %s: %d\n",
2095 bus_describe_intr(sc->dev, irq->res, irq->tag, name);
2101 t4_free_irq(struct adapter *sc, struct irq *irq)
2104 bus_teardown_intr(sc->dev, irq->res, irq->tag);
2106 bus_release_resource(sc->dev, SYS_RES_IRQ, irq->rid, irq->res);
2108 bzero(irq, sizeof(*irq));
2114 reg_block_dump(struct adapter *sc, uint8_t *buf, unsigned int start,
2117 uint32_t *p = (uint32_t *)(buf + start);
2119 for ( ; start <= end; start += sizeof(uint32_t))
2120 *p++ = t4_read_reg(sc, start);
2124 t4_get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
2127 static const unsigned int reg_ranges[] = {
2345 regs->version = 4 | (sc->params.rev << 10);
2346 for (i = 0; i < ARRAY_SIZE(reg_ranges); i += 2)
2347 reg_block_dump(sc, buf, reg_ranges[i], reg_ranges[i + 1]);
2351 cxgbe_tick(void *arg)
2353 struct port_info *pi = arg;
2354 struct ifnet *ifp = pi->ifp;
2355 struct sge_txq *txq;
2357 struct port_stats *s = &pi->stats;
2360 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2362 return; /* without scheduling another callout */
2365 t4_get_port_stats(pi->adapter, pi->tx_chan, s);
2367 ifp->if_opackets = s->tx_frames;
2368 ifp->if_ipackets = s->rx_frames;
2369 ifp->if_obytes = s->tx_octets;
2370 ifp->if_ibytes = s->rx_octets;
2371 ifp->if_omcasts = s->tx_mcast_frames;
2372 ifp->if_imcasts = s->rx_mcast_frames;
2373 ifp->if_iqdrops = s->rx_ovflow0 + s->rx_ovflow1 + s->rx_ovflow2 +
2377 for_each_txq(pi, i, txq)
2378 drops += txq->br->br_drops;
2379 ifp->if_snd.ifq_drops = drops;
2381 ifp->if_oerrors = s->tx_error_frames;
2382 ifp->if_ierrors = s->rx_jabber + s->rx_runt + s->rx_too_long +
2383 s->rx_fcs_err + s->rx_len_err;
2385 callout_schedule(&pi->tick, hz);
2390 t4_sysctls(struct adapter *sc)
2392 struct sysctl_ctx_list *ctx;
2393 struct sysctl_oid *oid;
2394 struct sysctl_oid_list *children;
2396 ctx = device_get_sysctl_ctx(sc->dev);
2397 oid = device_get_sysctl_tree(sc->dev);
2398 children = SYSCTL_CHILDREN(oid);
2400 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nports", CTLFLAG_RD,
2401 &sc->params.nports, 0, "# of ports");
2403 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "hw_revision", CTLFLAG_RD,
2404 &sc->params.rev, 0, "chip hardware revision");
2406 SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
2407 CTLFLAG_RD, &sc->fw_version, 0, "firmware version");
2409 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "TOE", CTLFLAG_RD,
2410 &sc->params.offload, 0, "hardware is capable of TCP offload");
2412 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "core_clock", CTLFLAG_RD,
2413 &sc->params.vpd.cclk, 0, "core clock frequency (in KHz)");
2415 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_timers",
2416 CTLTYPE_STRING | CTLFLAG_RD, &intr_timer, sizeof(intr_timer),
2417 sysctl_int_array, "A", "interrupt holdoff timer values (us)");
2419 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pkt_counts",
2420 CTLTYPE_STRING | CTLFLAG_RD, &intr_pktcount, sizeof(intr_pktcount),
2421 sysctl_int_array, "A", "interrupt holdoff packet counter values");
2423 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "devlog",
2424 CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
2425 sysctl_devlog, "A", "device log");
2431 cxgbe_sysctls(struct port_info *pi)
2433 struct sysctl_ctx_list *ctx;
2434 struct sysctl_oid *oid;
2435 struct sysctl_oid_list *children;
2437 ctx = device_get_sysctl_ctx(pi->dev);
2442 oid = device_get_sysctl_tree(pi->dev);
2443 children = SYSCTL_CHILDREN(oid);
2445 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nrxq", CTLFLAG_RD,
2446 &pi->nrxq, 0, "# of rx queues");
2447 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ntxq", CTLFLAG_RD,
2448 &pi->ntxq, 0, "# of tx queues");
2449 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_rxq", CTLFLAG_RD,
2450 &pi->first_rxq, 0, "index of first rx queue");
2451 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_txq", CTLFLAG_RD,
2452 &pi->first_txq, 0, "index of first tx queue");
2454 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx",
2455 CTLTYPE_INT | CTLFLAG_RW, pi, 0, sysctl_holdoff_tmr_idx, "I",
2456 "holdoff timer index");
2457 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx",
2458 CTLTYPE_INT | CTLFLAG_RW, pi, 0, sysctl_holdoff_pktc_idx, "I",
2459 "holdoff packet counter index");
2461 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_rxq",
2462 CTLTYPE_INT | CTLFLAG_RW, pi, 0, sysctl_qsize_rxq, "I",
2464 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_txq",
2465 CTLTYPE_INT | CTLFLAG_RW, pi, 0, sysctl_qsize_txq, "I",
2469 * dev.cxgbe.X.stats.
2471 oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats", CTLFLAG_RD,
2472 NULL, "port statistics");
2473 children = SYSCTL_CHILDREN(oid);
2475 #define SYSCTL_ADD_T4_REG64(pi, name, desc, reg) \
2476 SYSCTL_ADD_OID(ctx, children, OID_AUTO, name, \
2477 CTLTYPE_U64 | CTLFLAG_RD, pi->adapter, reg, \
2478 sysctl_handle_t4_reg64, "QU", desc)
2480 SYSCTL_ADD_T4_REG64(pi, "tx_octets", "# of octets in good frames",
2481 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_BYTES_L));
2482 SYSCTL_ADD_T4_REG64(pi, "tx_frames", "total # of good frames",
2483 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_FRAMES_L));
2484 SYSCTL_ADD_T4_REG64(pi, "tx_bcast_frames", "# of broadcast frames",
2485 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_BCAST_L));
2486 SYSCTL_ADD_T4_REG64(pi, "tx_mcast_frames", "# of multicast frames",
2487 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_MCAST_L));
2488 SYSCTL_ADD_T4_REG64(pi, "tx_ucast_frames", "# of unicast frames",
2489 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_UCAST_L));
2490 SYSCTL_ADD_T4_REG64(pi, "tx_error_frames", "# of error frames",
2491 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_ERROR_L));
2492 SYSCTL_ADD_T4_REG64(pi, "tx_frames_64",
2493 "# of tx frames in this range",
2494 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_64B_L));
2495 SYSCTL_ADD_T4_REG64(pi, "tx_frames_65_127",
2496 "# of tx frames in this range",
2497 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_65B_127B_L));
2498 SYSCTL_ADD_T4_REG64(pi, "tx_frames_128_255",
2499 "# of tx frames in this range",
2500 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_128B_255B_L));
2501 SYSCTL_ADD_T4_REG64(pi, "tx_frames_256_511",
2502 "# of tx frames in this range",
2503 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_256B_511B_L));
2504 SYSCTL_ADD_T4_REG64(pi, "tx_frames_512_1023",
2505 "# of tx frames in this range",
2506 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_512B_1023B_L));
2507 SYSCTL_ADD_T4_REG64(pi, "tx_frames_1024_1518",
2508 "# of tx frames in this range",
2509 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_1024B_1518B_L));
2510 SYSCTL_ADD_T4_REG64(pi, "tx_frames_1519_max",
2511 "# of tx frames in this range",
2512 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_1519B_MAX_L));
2513 SYSCTL_ADD_T4_REG64(pi, "tx_drop", "# of dropped tx frames",
2514 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_DROP_L));
2515 SYSCTL_ADD_T4_REG64(pi, "tx_pause", "# of pause frames transmitted",
2516 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PAUSE_L));
2517 SYSCTL_ADD_T4_REG64(pi, "tx_ppp0", "# of PPP prio 0 frames transmitted",
2518 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP0_L));
2519 SYSCTL_ADD_T4_REG64(pi, "tx_ppp1", "# of PPP prio 1 frames transmitted",
2520 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP1_L));
2521 SYSCTL_ADD_T4_REG64(pi, "tx_ppp2", "# of PPP prio 2 frames transmitted",
2522 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP2_L));
2523 SYSCTL_ADD_T4_REG64(pi, "tx_ppp3", "# of PPP prio 3 frames transmitted",
2524 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP3_L));
2525 SYSCTL_ADD_T4_REG64(pi, "tx_ppp4", "# of PPP prio 4 frames transmitted",
2526 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP4_L));
2527 SYSCTL_ADD_T4_REG64(pi, "tx_ppp5", "# of PPP prio 5 frames transmitted",
2528 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP5_L));
2529 SYSCTL_ADD_T4_REG64(pi, "tx_ppp6", "# of PPP prio 6 frames transmitted",
2530 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP6_L));
2531 SYSCTL_ADD_T4_REG64(pi, "tx_ppp7", "# of PPP prio 7 frames transmitted",
2532 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP7_L));
2534 SYSCTL_ADD_T4_REG64(pi, "rx_octets", "# of octets in good frames",
2535 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_BYTES_L));
2536 SYSCTL_ADD_T4_REG64(pi, "rx_frames", "total # of good frames",
2537 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_FRAMES_L));
2538 SYSCTL_ADD_T4_REG64(pi, "rx_bcast_frames", "# of broadcast frames",
2539 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_BCAST_L));
2540 SYSCTL_ADD_T4_REG64(pi, "rx_mcast_frames", "# of multicast frames",
2541 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_MCAST_L));
2542 SYSCTL_ADD_T4_REG64(pi, "rx_ucast_frames", "# of unicast frames",
2543 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_UCAST_L));
2544 SYSCTL_ADD_T4_REG64(pi, "rx_too_long", "# of frames exceeding MTU",
2545 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_MTU_ERROR_L));
2546 SYSCTL_ADD_T4_REG64(pi, "rx_jabber", "# of jabber frames",
2547 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_MTU_CRC_ERROR_L));
2548 SYSCTL_ADD_T4_REG64(pi, "rx_fcs_err",
2549 "# of frames received with bad FCS",
2550 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_CRC_ERROR_L));
2551 SYSCTL_ADD_T4_REG64(pi, "rx_len_err",
2552 "# of frames received with length error",
2553 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_LEN_ERROR_L));
2554 SYSCTL_ADD_T4_REG64(pi, "rx_symbol_err", "symbol errors",
2555 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_SYM_ERROR_L));
2556 SYSCTL_ADD_T4_REG64(pi, "rx_runt", "# of short frames received",
2557 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_LESS_64B_L));
2558 SYSCTL_ADD_T4_REG64(pi, "rx_frames_64",
2559 "# of rx frames in this range",
2560 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_64B_L));
2561 SYSCTL_ADD_T4_REG64(pi, "rx_frames_65_127",
2562 "# of rx frames in this range",
2563 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_65B_127B_L));
2564 SYSCTL_ADD_T4_REG64(pi, "rx_frames_128_255",
2565 "# of rx frames in this range",
2566 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_128B_255B_L));
2567 SYSCTL_ADD_T4_REG64(pi, "rx_frames_256_511",
2568 "# of rx frames in this range",
2569 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_256B_511B_L));
2570 SYSCTL_ADD_T4_REG64(pi, "rx_frames_512_1023",
2571 "# of rx frames in this range",
2572 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_512B_1023B_L));
2573 SYSCTL_ADD_T4_REG64(pi, "rx_frames_1024_1518",
2574 "# of rx frames in this range",
2575 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_1024B_1518B_L));
2576 SYSCTL_ADD_T4_REG64(pi, "rx_frames_1519_max",
2577 "# of rx frames in this range",
2578 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_1519B_MAX_L));
2579 SYSCTL_ADD_T4_REG64(pi, "rx_pause", "# of pause frames received",
2580 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PAUSE_L));
2581 SYSCTL_ADD_T4_REG64(pi, "rx_ppp0", "# of PPP prio 0 frames received",
2582 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP0_L));
2583 SYSCTL_ADD_T4_REG64(pi, "rx_ppp1", "# of PPP prio 1 frames received",
2584 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP1_L));
2585 SYSCTL_ADD_T4_REG64(pi, "rx_ppp2", "# of PPP prio 2 frames received",
2586 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP2_L));
2587 SYSCTL_ADD_T4_REG64(pi, "rx_ppp3", "# of PPP prio 3 frames received",
2588 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP3_L));
2589 SYSCTL_ADD_T4_REG64(pi, "rx_ppp4", "# of PPP prio 4 frames received",
2590 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP4_L));
2591 SYSCTL_ADD_T4_REG64(pi, "rx_ppp5", "# of PPP prio 5 frames received",
2592 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP5_L));
2593 SYSCTL_ADD_T4_REG64(pi, "rx_ppp6", "# of PPP prio 6 frames received",
2594 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP6_L));
2595 SYSCTL_ADD_T4_REG64(pi, "rx_ppp7", "# of PPP prio 7 frames received",
2596 PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP7_L));
2598 #undef SYSCTL_ADD_T4_REG64
2600 #define SYSCTL_ADD_T4_PORTSTAT(name, desc) \
2601 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, #name, CTLFLAG_RD, \
2602 &pi->stats.name, desc)
2604 /* We get these from port_stats and they may be stale by upto 1s */
2605 SYSCTL_ADD_T4_PORTSTAT(rx_ovflow0,
2606 "# drops due to buffer-group 0 overflows");
2607 SYSCTL_ADD_T4_PORTSTAT(rx_ovflow1,
2608 "# drops due to buffer-group 1 overflows");
2609 SYSCTL_ADD_T4_PORTSTAT(rx_ovflow2,
2610 "# drops due to buffer-group 2 overflows");
2611 SYSCTL_ADD_T4_PORTSTAT(rx_ovflow3,
2612 "# drops due to buffer-group 3 overflows");
2613 SYSCTL_ADD_T4_PORTSTAT(rx_trunc0,
2614 "# of buffer-group 0 truncated packets");
2615 SYSCTL_ADD_T4_PORTSTAT(rx_trunc1,
2616 "# of buffer-group 1 truncated packets");
2617 SYSCTL_ADD_T4_PORTSTAT(rx_trunc2,
2618 "# of buffer-group 2 truncated packets");
2619 SYSCTL_ADD_T4_PORTSTAT(rx_trunc3,
2620 "# of buffer-group 3 truncated packets");
2622 #undef SYSCTL_ADD_T4_PORTSTAT
2628 sysctl_int_array(SYSCTL_HANDLER_ARGS)
2633 sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
2634 for (i = arg1; arg2; arg2 -= sizeof(int), i++)
2635 sbuf_printf(&sb, "%d ", *i);
2638 rc = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
2644 sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)
2646 struct port_info *pi = arg1;
2647 struct adapter *sc = pi->adapter;
2648 struct sge_rxq *rxq;
2653 rc = sysctl_handle_int(oidp, &idx, 0, req);
2654 if (rc != 0 || req->newptr == NULL)
2657 if (idx < 0 || idx >= SGE_NTIMERS)
2661 rc = IS_DOOMED(pi) ? ENXIO : (IS_BUSY(sc) ? EBUSY : 0);
2663 for_each_rxq(pi, i, rxq) {
2664 rxq->iq.intr_params = V_QINTR_TIMER_IDX(idx) |
2665 V_QINTR_CNT_EN(pi->pktc_idx != -1);
2675 sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)
2677 struct port_info *pi = arg1;
2678 struct adapter *sc = pi->adapter;
2683 rc = sysctl_handle_int(oidp, &idx, 0, req);
2684 if (rc != 0 || req->newptr == NULL)
2687 if (idx < -1 || idx >= SGE_NCOUNTERS)
2691 rc = IS_DOOMED(pi) ? ENXIO : (IS_BUSY(sc) ? EBUSY : 0);
2692 if (rc == 0 && pi->ifp->if_drv_flags & IFF_DRV_RUNNING)
2693 rc = EBUSY; /* can be changed only when port is down */
2703 sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)
2705 struct port_info *pi = arg1;
2706 struct adapter *sc = pi->adapter;
2709 qsize = pi->qsize_rxq;
2711 rc = sysctl_handle_int(oidp, &qsize, 0, req);
2712 if (rc != 0 || req->newptr == NULL)
2715 if (qsize < 128 || (qsize & 7))
2719 rc = IS_DOOMED(pi) ? ENXIO : (IS_BUSY(sc) ? EBUSY : 0);
2720 if (rc == 0 && pi->ifp->if_drv_flags & IFF_DRV_RUNNING)
2721 rc = EBUSY; /* can be changed only when port is down */
2724 pi->qsize_rxq = qsize;
2731 sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)
2733 struct port_info *pi = arg1;
2734 struct adapter *sc = pi->adapter;
2737 qsize = pi->qsize_txq;
2739 rc = sysctl_handle_int(oidp, &qsize, 0, req);
2740 if (rc != 0 || req->newptr == NULL)
2747 rc = IS_DOOMED(pi) ? ENXIO : (IS_BUSY(sc) ? EBUSY : 0);
2748 if (rc == 0 && pi->ifp->if_drv_flags & IFF_DRV_RUNNING)
2749 rc = EBUSY; /* can be changed only when port is down */
2752 pi->qsize_txq = qsize;
2759 sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)
2761 struct adapter *sc = arg1;
2765 val = t4_read_reg64(sc, reg);
2767 return (sysctl_handle_64(oidp, &val, 0, req));
2770 const char *devlog_level_strings[] = {
2771 [FW_DEVLOG_LEVEL_EMERG] = "EMERG",
2772 [FW_DEVLOG_LEVEL_CRIT] = "CRIT",
2773 [FW_DEVLOG_LEVEL_ERR] = "ERR",
2774 [FW_DEVLOG_LEVEL_NOTICE] = "NOTICE",
2775 [FW_DEVLOG_LEVEL_INFO] = "INFO",
2776 [FW_DEVLOG_LEVEL_DEBUG] = "DEBUG"
2779 const char *devlog_facility_strings[] = {
2780 [FW_DEVLOG_FACILITY_CORE] = "CORE",
2781 [FW_DEVLOG_FACILITY_SCHED] = "SCHED",
2782 [FW_DEVLOG_FACILITY_TIMER] = "TIMER",
2783 [FW_DEVLOG_FACILITY_RES] = "RES",
2784 [FW_DEVLOG_FACILITY_HW] = "HW",
2785 [FW_DEVLOG_FACILITY_FLR] = "FLR",
2786 [FW_DEVLOG_FACILITY_DMAQ] = "DMAQ",
2787 [FW_DEVLOG_FACILITY_PHY] = "PHY",
2788 [FW_DEVLOG_FACILITY_MAC] = "MAC",
2789 [FW_DEVLOG_FACILITY_PORT] = "PORT",
2790 [FW_DEVLOG_FACILITY_VI] = "VI",
2791 [FW_DEVLOG_FACILITY_FILTER] = "FILTER",
2792 [FW_DEVLOG_FACILITY_ACL] = "ACL",
2793 [FW_DEVLOG_FACILITY_TM] = "TM",
2794 [FW_DEVLOG_FACILITY_QFC] = "QFC",
2795 [FW_DEVLOG_FACILITY_DCB] = "DCB",
2796 [FW_DEVLOG_FACILITY_ETH] = "ETH",
2797 [FW_DEVLOG_FACILITY_OFLD] = "OFLD",
2798 [FW_DEVLOG_FACILITY_RI] = "RI",
2799 [FW_DEVLOG_FACILITY_ISCSI] = "ISCSI",
2800 [FW_DEVLOG_FACILITY_FCOE] = "FCOE",
2801 [FW_DEVLOG_FACILITY_FOISCSI] = "FOISCSI",
2802 [FW_DEVLOG_FACILITY_FOFCOE] = "FOFCOE"
2806 sysctl_devlog(SYSCTL_HANDLER_ARGS)
2808 struct adapter *sc = arg1;
2809 struct devlog_params *dparams = &sc->params.devlog;
2810 struct fw_devlog_e *buf, *e;
2811 int i, j, rc, nentries, first = 0;
2813 uint64_t ftstamp = UINT64_MAX;
2815 if (dparams->start == 0)
2818 nentries = dparams->size / sizeof(struct fw_devlog_e);
2820 buf = malloc(dparams->size, M_CXGBE, M_NOWAIT);
2824 rc = -t4_mem_read(sc, dparams->memtype, dparams->start, dparams->size,
2829 for (i = 0; i < nentries; i++) {
2832 if (e->timestamp == 0)
2835 e->timestamp = be64toh(e->timestamp);
2836 e->seqno = be32toh(e->seqno);
2837 for (j = 0; j < 8; j++)
2838 e->params[j] = be32toh(e->params[j]);
2840 if (e->timestamp < ftstamp) {
2841 ftstamp = e->timestamp;
2846 if (buf[first].timestamp == 0)
2847 goto done; /* nothing in the log */
2849 rc = sysctl_wire_old_buffer(req, 0);
2853 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
2854 sbuf_printf(sb, "\n%10s %15s %8s %8s %s\n",
2855 "Seq#", "Tstamp", "Level", "Facility", "Message");
2860 if (e->timestamp == 0)
2863 sbuf_printf(sb, "%10d %15ju %8s %8s ",
2864 e->seqno, e->timestamp,
2865 (e->level < ARRAY_SIZE(devlog_level_strings) ?
2866 devlog_level_strings[e->level] : "UNKNOWN"),
2867 (e->facility < ARRAY_SIZE(devlog_facility_strings) ?
2868 devlog_facility_strings[e->facility] : "UNKNOWN"));
2869 sbuf_printf(sb, e->fmt, e->params[0], e->params[1],
2870 e->params[2], e->params[3], e->params[4],
2871 e->params[5], e->params[6], e->params[7]);
2873 if (++i == nentries)
2875 } while (i != first);
2877 rc = sbuf_finish(sb);
2885 txq_start(struct ifnet *ifp, struct sge_txq *txq)
2887 struct buf_ring *br;
2890 TXQ_LOCK_ASSERT_OWNED(txq);
2893 m = txq->m ? txq->m : drbr_dequeue(ifp, br);
2895 t4_eth_tx(ifp, txq, m);
2899 cxgbe_txq_start(void *arg, int count)
2901 struct sge_txq *txq = arg;
2904 if (txq->eq.flags & EQ_CRFLUSHED) {
2905 txq->eq.flags &= ~EQ_CRFLUSHED;
2906 txq_start(txq->ifp, txq);
2908 wakeup_one(txq); /* txq is going away, wakeup free_txq */
2913 fconf_to_mode(uint32_t fconf)
2917 mode = T4_FILTER_IPv4 | T4_FILTER_IPv6 | T4_FILTER_IP_SADDR |
2918 T4_FILTER_IP_DADDR | T4_FILTER_IP_SPORT | T4_FILTER_IP_DPORT;
2920 if (fconf & F_FRAGMENTATION)
2921 mode |= T4_FILTER_IP_FRAGMENT;
2923 if (fconf & F_MPSHITTYPE)
2924 mode |= T4_FILTER_MPS_HIT_TYPE;
2926 if (fconf & F_MACMATCH)
2927 mode |= T4_FILTER_MAC_IDX;
2929 if (fconf & F_ETHERTYPE)
2930 mode |= T4_FILTER_ETH_TYPE;
2932 if (fconf & F_PROTOCOL)
2933 mode |= T4_FILTER_IP_PROTO;
2936 mode |= T4_FILTER_IP_TOS;
2939 mode |= T4_FILTER_IVLAN;
2941 if (fconf & F_VNIC_ID)
2942 mode |= T4_FILTER_OVLAN;
2945 mode |= T4_FILTER_PORT;
2948 mode |= T4_FILTER_FCoE;
2954 mode_to_fconf(uint32_t mode)
2958 if (mode & T4_FILTER_IP_FRAGMENT)
2959 fconf |= F_FRAGMENTATION;
2961 if (mode & T4_FILTER_MPS_HIT_TYPE)
2962 fconf |= F_MPSHITTYPE;
2964 if (mode & T4_FILTER_MAC_IDX)
2965 fconf |= F_MACMATCH;
2967 if (mode & T4_FILTER_ETH_TYPE)
2968 fconf |= F_ETHERTYPE;
2970 if (mode & T4_FILTER_IP_PROTO)
2971 fconf |= F_PROTOCOL;
2973 if (mode & T4_FILTER_IP_TOS)
2976 if (mode & T4_FILTER_IVLAN)
2979 if (mode & T4_FILTER_OVLAN)
2982 if (mode & T4_FILTER_PORT)
2985 if (mode & T4_FILTER_FCoE)
2992 fspec_to_fconf(struct t4_filter_specification *fs)
2996 if (fs->val.frag || fs->mask.frag)
2997 fconf |= F_FRAGMENTATION;
2999 if (fs->val.matchtype || fs->mask.matchtype)
3000 fconf |= F_MPSHITTYPE;
3002 if (fs->val.macidx || fs->mask.macidx)
3003 fconf |= F_MACMATCH;
3005 if (fs->val.ethtype || fs->mask.ethtype)
3006 fconf |= F_ETHERTYPE;
3008 if (fs->val.proto || fs->mask.proto)
3009 fconf |= F_PROTOCOL;
3011 if (fs->val.tos || fs->mask.tos)
3014 if (fs->val.ivlan_vld || fs->mask.ivlan_vld)
3017 if (fs->val.ovlan_vld || fs->mask.ovlan_vld)
3020 if (fs->val.iport || fs->mask.iport)
3023 if (fs->val.fcoe || fs->mask.fcoe)
3030 get_filter_mode(struct adapter *sc, uint32_t *mode)
3034 t4_read_indirect(sc, A_TP_PIO_ADDR, A_TP_PIO_DATA, &fconf, 1,
3037 *mode = fconf_to_mode(fconf);
3043 set_filter_mode(struct adapter *sc, uint32_t mode)
3048 fconf = mode_to_fconf(mode);
3056 if (sc->tids.ftids_in_use > 0) {
3061 rc = -t4_set_filter_mode(sc, fconf);
3067 static inline uint64_t
3068 get_filter_hits(struct adapter *sc, uint32_t fid)
3070 uint32_t tcb_base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
3073 t4_write_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 0),
3074 tcb_base + (fid + sc->tids.ftid_base) * TCB_SIZE);
3075 t4_read_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 0));
3076 hits = t4_read_reg64(sc, MEMWIN0_BASE + 16);
3078 return (be64toh(hits));
3082 get_filter(struct adapter *sc, struct t4_filter *t)
3084 int i, nfilters = sc->tids.nftids;
3085 struct filter_entry *f;
3087 ADAPTER_LOCK_ASSERT_OWNED(sc);
3092 if (sc->tids.ftids_in_use == 0 || sc->tids.ftid_tab == NULL ||
3093 t->idx >= nfilters) {
3094 t->idx = 0xffffffff;
3098 f = &sc->tids.ftid_tab[t->idx];
3099 for (i = t->idx; i < nfilters; i++, f++) {
3102 t->l2tidx = f->l2t ? f->l2t->idx : 0;
3103 t->smtidx = f->smtidx;
3105 t->hits = get_filter_hits(sc, t->idx);
3107 t->hits = UINT64_MAX;
3114 t->idx = 0xffffffff;
3119 set_filter(struct adapter *sc, struct t4_filter *t)
3122 unsigned int nfilters, nports;
3123 struct filter_entry *f;
3126 ADAPTER_LOCK_ASSERT_OWNED(sc);
3128 nfilters = sc->tids.nftids;
3129 nports = sc->params.nports;
3134 if (!(sc->flags & FULL_INIT_DONE))
3137 if (t->idx >= nfilters)
3140 /* Validate against the global filter mode */
3141 t4_read_indirect(sc, A_TP_PIO_ADDR, A_TP_PIO_DATA, &fconf, 1,
3143 if ((fconf | fspec_to_fconf(&t->fs)) != fconf)
3146 if (t->fs.action == FILTER_SWITCH && t->fs.eport >= nports)
3149 if (t->fs.val.iport >= nports)
3152 /* Can't specify an iq if not steering to it */
3153 if (!t->fs.dirsteer && t->fs.iq)
3156 /* IPv6 filter idx must be 4 aligned */
3157 if (t->fs.type == 1 &&
3158 ((t->idx & 0x3) || t->idx + 4 >= nfilters))
3161 if (sc->tids.ftid_tab == NULL) {
3162 KASSERT(sc->tids.ftids_in_use == 0,
3163 ("%s: no memory allocated but filters_in_use > 0",
3166 sc->tids.ftid_tab = malloc(sizeof (struct filter_entry) *
3167 nfilters, M_CXGBE, M_NOWAIT | M_ZERO);
3168 if (sc->tids.ftid_tab == NULL)
3172 for (i = 0; i < 4; i++) {
3173 f = &sc->tids.ftid_tab[t->idx + i];
3175 if (f->pending || f->valid)
3180 if (t->fs.type == 0)
3184 f = &sc->tids.ftid_tab[t->idx];
3187 return set_filter_wr(sc, t->idx);
3191 del_filter(struct adapter *sc, struct t4_filter *t)
3193 unsigned int nfilters;
3194 struct filter_entry *f;
3196 ADAPTER_LOCK_ASSERT_OWNED(sc);
3201 nfilters = sc->tids.nftids;
3206 if (sc->tids.ftid_tab == NULL || sc->tids.ftids_in_use == 0 ||
3210 if (!(sc->flags & FULL_INIT_DONE))
3213 f = &sc->tids.ftid_tab[t->idx];
3221 t->fs = f->fs; /* extra info for the caller */
3222 return del_filter_wr(sc, t->idx);
3229 clear_filter(struct filter_entry *f)
3232 t4_l2t_release(f->l2t);
3234 bzero(f, sizeof (*f));
3238 set_filter_wr(struct adapter *sc, int fidx)
3241 struct filter_entry *f = &sc->tids.ftid_tab[fidx];
3243 struct fw_filter_wr *fwr;
3246 ADAPTER_LOCK_ASSERT_OWNED(sc);
3248 if (f->fs.newdmac || f->fs.newvlan) {
3249 /* This filter needs an L2T entry; allocate one. */
3250 f->l2t = t4_l2t_alloc_switching(sc->l2t);
3253 if (t4_l2t_set_switching(sc, f->l2t, f->fs.vlan, f->fs.eport,
3255 t4_l2t_release(f->l2t);
3261 ftid = sc->tids.ftid_base + fidx;
3263 m = m_gethdr(M_NOWAIT, MT_DATA);
3267 fwr = mtod(m, struct fw_filter_wr *);
3268 m->m_len = m->m_pkthdr.len = sizeof(*fwr);
3269 bzero(fwr, sizeof (*fwr));
3271 fwr->op_pkd = htobe32(V_FW_WR_OP(FW_FILTER_WR));
3272 fwr->len16_pkd = htobe32(FW_LEN16(*fwr));
3274 htobe32(V_FW_FILTER_WR_TID(ftid) |
3275 V_FW_FILTER_WR_RQTYPE(f->fs.type) |
3276 V_FW_FILTER_WR_NOREPLY(0) |
3277 V_FW_FILTER_WR_IQ(f->fs.iq));
3278 fwr->del_filter_to_l2tix =
3279 htobe32(V_FW_FILTER_WR_RPTTID(f->fs.rpttid) |
3280 V_FW_FILTER_WR_DROP(f->fs.action == FILTER_DROP) |
3281 V_FW_FILTER_WR_DIRSTEER(f->fs.dirsteer) |
3282 V_FW_FILTER_WR_MASKHASH(f->fs.maskhash) |
3283 V_FW_FILTER_WR_DIRSTEERHASH(f->fs.dirsteerhash) |
3284 V_FW_FILTER_WR_LPBK(f->fs.action == FILTER_SWITCH) |
3285 V_FW_FILTER_WR_DMAC(f->fs.newdmac) |
3286 V_FW_FILTER_WR_SMAC(f->fs.newsmac) |
3287 V_FW_FILTER_WR_INSVLAN(f->fs.newvlan == VLAN_INSERT ||
3288 f->fs.newvlan == VLAN_REWRITE) |
3289 V_FW_FILTER_WR_RMVLAN(f->fs.newvlan == VLAN_REMOVE ||
3290 f->fs.newvlan == VLAN_REWRITE) |
3291 V_FW_FILTER_WR_HITCNTS(f->fs.hitcnts) |
3292 V_FW_FILTER_WR_TXCHAN(f->fs.eport) |
3293 V_FW_FILTER_WR_PRIO(f->fs.prio) |
3294 V_FW_FILTER_WR_L2TIX(f->l2t ? f->l2t->idx : 0));
3295 fwr->ethtype = htobe16(f->fs.val.ethtype);
3296 fwr->ethtypem = htobe16(f->fs.mask.ethtype);
3297 fwr->frag_to_ovlan_vldm =
3298 (V_FW_FILTER_WR_FRAG(f->fs.val.frag) |
3299 V_FW_FILTER_WR_FRAGM(f->fs.mask.frag) |
3300 V_FW_FILTER_WR_IVLAN_VLD(f->fs.val.ivlan_vld) |
3301 V_FW_FILTER_WR_OVLAN_VLD(f->fs.val.ovlan_vld) |
3302 V_FW_FILTER_WR_IVLAN_VLDM(f->fs.mask.ivlan_vld) |
3303 V_FW_FILTER_WR_OVLAN_VLDM(f->fs.mask.ovlan_vld));
3305 fwr->rx_chan_rx_rpl_iq = htobe16(V_FW_FILTER_WR_RX_CHAN(0) |
3306 V_FW_FILTER_WR_RX_RPL_IQ(sc->sge.intrq[0].abs_id));
3307 fwr->maci_to_matchtypem =
3308 htobe32(V_FW_FILTER_WR_MACI(f->fs.val.macidx) |
3309 V_FW_FILTER_WR_MACIM(f->fs.mask.macidx) |
3310 V_FW_FILTER_WR_FCOE(f->fs.val.fcoe) |
3311 V_FW_FILTER_WR_FCOEM(f->fs.mask.fcoe) |
3312 V_FW_FILTER_WR_PORT(f->fs.val.iport) |
3313 V_FW_FILTER_WR_PORTM(f->fs.mask.iport) |
3314 V_FW_FILTER_WR_MATCHTYPE(f->fs.val.matchtype) |
3315 V_FW_FILTER_WR_MATCHTYPEM(f->fs.mask.matchtype));
3316 fwr->ptcl = f->fs.val.proto;
3317 fwr->ptclm = f->fs.mask.proto;
3318 fwr->ttyp = f->fs.val.tos;
3319 fwr->ttypm = f->fs.mask.tos;
3320 fwr->ivlan = htobe16(f->fs.val.ivlan);
3321 fwr->ivlanm = htobe16(f->fs.mask.ivlan);
3322 fwr->ovlan = htobe16(f->fs.val.ovlan);
3323 fwr->ovlanm = htobe16(f->fs.mask.ovlan);
3324 bcopy(f->fs.val.dip, fwr->lip, sizeof (fwr->lip));
3325 bcopy(f->fs.mask.dip, fwr->lipm, sizeof (fwr->lipm));
3326 bcopy(f->fs.val.sip, fwr->fip, sizeof (fwr->fip));
3327 bcopy(f->fs.mask.sip, fwr->fipm, sizeof (fwr->fipm));
3328 fwr->lp = htobe16(f->fs.val.dport);
3329 fwr->lpm = htobe16(f->fs.mask.dport);
3330 fwr->fp = htobe16(f->fs.val.sport);
3331 fwr->fpm = htobe16(f->fs.mask.sport);
3333 bcopy(f->fs.smac, fwr->sma, sizeof (fwr->sma));
3336 sc->tids.ftids_in_use++;
3337 rc = t4_mgmt_tx(sc, m);
3339 sc->tids.ftids_in_use--;
3347 del_filter_wr(struct adapter *sc, int fidx)
3349 struct filter_entry *f = &sc->tids.ftid_tab[fidx];
3351 struct fw_filter_wr *fwr;
3352 unsigned int rc, ftid;
3354 ADAPTER_LOCK_ASSERT_OWNED(sc);
3356 ftid = sc->tids.ftid_base + fidx;
3358 m = m_gethdr(M_NOWAIT, MT_DATA);
3362 fwr = mtod(m, struct fw_filter_wr *);
3363 m->m_len = m->m_pkthdr.len = sizeof(*fwr);
3364 bzero(fwr, sizeof (*fwr));
3366 t4_mk_filtdelwr(ftid, fwr, sc->sge.intrq[0].abs_id);
3369 rc = t4_mgmt_tx(sc, m);
3377 /* XXX move intr handlers to main.c and make this static */
3379 filter_rpl(struct adapter *sc, const struct cpl_set_tcb_rpl *rpl)
3381 unsigned int idx = GET_TID(rpl);
3383 if (idx >= sc->tids.ftid_base &&
3384 (idx -= sc->tids.ftid_base) < sc->tids.nftids) {
3385 unsigned int rc = G_COOKIE(rpl->cookie);
3386 struct filter_entry *f = &sc->tids.ftid_tab[idx];
3388 if (rc == FW_FILTER_WR_FLT_DELETED) {
3390 * Clear the filter when we get confirmation from the
3391 * hardware that the filter has been deleted.
3394 sc->tids.ftids_in_use--;
3395 } else if (rc == FW_FILTER_WR_SMT_TBL_FULL) {
3396 device_printf(sc->dev,
3397 "filter %u setup failed due to full SMT\n", idx);
3399 sc->tids.ftids_in_use--;
3400 } else if (rc == FW_FILTER_WR_FLT_ADDED) {
3401 f->smtidx = (be64toh(rpl->oldval) >> 24) & 0xff;
3402 f->pending = 0; /* asynchronous setup completed */
3406 * Something went wrong. Issue a warning about the
3407 * problem and clear everything out.
3409 device_printf(sc->dev,
3410 "filter %u setup failed with error %u\n", idx, rc);
3412 sc->tids.ftids_in_use--;
3418 get_sge_context(struct adapter *sc, struct t4_sge_context *cntxt)
3422 if (cntxt->cid > M_CTXTQID)
3425 if (cntxt->mem_id != CTXT_EGRESS && cntxt->mem_id != CTXT_INGRESS &&
3426 cntxt->mem_id != CTXT_FLM && cntxt->mem_id != CTXT_CNM)
3429 if (sc->flags & FW_OK) {
3430 ADAPTER_LOCK(sc); /* Avoid parallel t4_wr_mbox */
3431 rc = -t4_sge_ctxt_rd(sc, sc->mbox, cntxt->cid, cntxt->mem_id,
3437 /* Read via firmware failed or wasn't even attempted */
3439 rc = -t4_sge_ctxt_rd_bd(sc, cntxt->cid, cntxt->mem_id,
3447 t4_os_find_pci_capability(struct adapter *sc, int cap)
3451 return (pci_find_cap(sc->dev, cap, &i) == 0 ? i : 0);
3455 t4_os_pci_save_state(struct adapter *sc)
3458 struct pci_devinfo *dinfo;
3461 dinfo = device_get_ivars(dev);
3463 pci_cfg_save(dev, dinfo, 0);
3468 t4_os_pci_restore_state(struct adapter *sc)
3471 struct pci_devinfo *dinfo;
3474 dinfo = device_get_ivars(dev);
3476 pci_cfg_restore(dev, dinfo);
3481 t4_os_portmod_changed(const struct adapter *sc, int idx)
3483 struct port_info *pi = sc->port[idx];
3484 static const char *mod_str[] = {
3485 NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM"
3488 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
3489 if_printf(pi->ifp, "transceiver unplugged.\n");
3490 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
3491 if_printf(pi->ifp, "unknown transceiver inserted.\n");
3492 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
3493 if_printf(pi->ifp, "unsupported transceiver inserted.\n");
3494 else if (pi->mod_type > 0 && pi->mod_type < ARRAY_SIZE(mod_str)) {
3495 if_printf(pi->ifp, "%s transceiver inserted.\n",
3496 mod_str[pi->mod_type]);
3498 if_printf(pi->ifp, "transceiver (type %d) inserted.\n",
3504 t4_os_link_changed(struct adapter *sc, int idx, int link_stat)
3506 struct port_info *pi = sc->port[idx];
3507 struct ifnet *ifp = pi->ifp;
3510 ifp->if_baudrate = IF_Mbps(pi->link_cfg.speed);
3511 if_link_state_change(ifp, LINK_STATE_UP);
3513 if_link_state_change(ifp, LINK_STATE_DOWN);
3517 t4_open(struct cdev *dev, int flags, int type, struct thread *td)
3523 t4_close(struct cdev *dev, int flags, int type, struct thread *td)
3529 t4_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
3533 struct adapter *sc = dev->si_drv1;
3535 rc = priv_check(td, PRIV_DRIVER);
3540 case CHELSIO_T4_GETREG: {
3541 struct t4_reg *edata = (struct t4_reg *)data;
3543 if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
3546 if (edata->size == 4)
3547 edata->val = t4_read_reg(sc, edata->addr);
3548 else if (edata->size == 8)
3549 edata->val = t4_read_reg64(sc, edata->addr);
3555 case CHELSIO_T4_SETREG: {
3556 struct t4_reg *edata = (struct t4_reg *)data;
3558 if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
3561 if (edata->size == 4) {
3562 if (edata->val & 0xffffffff00000000)
3564 t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
3565 } else if (edata->size == 8)
3566 t4_write_reg64(sc, edata->addr, edata->val);
3571 case CHELSIO_T4_REGDUMP: {
3572 struct t4_regdump *regs = (struct t4_regdump *)data;
3573 int reglen = T4_REGDUMP_SIZE;
3576 if (regs->len < reglen) {
3577 regs->len = reglen; /* hint to the caller */
3582 buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
3583 t4_get_regs(sc, regs, buf);
3584 rc = copyout(buf, regs->data, reglen);
3588 case CHELSIO_T4_GET_FILTER_MODE:
3589 rc = get_filter_mode(sc, (uint32_t *)data);
3591 case CHELSIO_T4_SET_FILTER_MODE:
3592 rc = set_filter_mode(sc, *(uint32_t *)data);
3594 case CHELSIO_T4_GET_FILTER:
3596 rc = get_filter(sc, (struct t4_filter *)data);
3599 case CHELSIO_T4_SET_FILTER:
3601 rc = set_filter(sc, (struct t4_filter *)data);
3604 case CHELSIO_T4_DEL_FILTER:
3606 rc = del_filter(sc, (struct t4_filter *)data);
3609 case CHELSIO_T4_GET_SGE_CONTEXT:
3610 rc = get_sge_context(sc, (struct t4_sge_context *)data);
3620 t4_mod_event(module_t mod, int cmd, void *arg)
3623 if (cmd == MOD_LOAD)
3629 static devclass_t t4_devclass;
3630 static devclass_t cxgbe_devclass;
3632 DRIVER_MODULE(t4nex, pci, t4_driver, t4_devclass, t4_mod_event, 0);
3633 MODULE_VERSION(t4nex, 1);
3635 DRIVER_MODULE(cxgbe, t4nex, cxgbe_driver, cxgbe_devclass, 0, 0);
3636 MODULE_VERSION(cxgbe, 1);