2 * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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
27 * $FreeBSD: src/sys/dev/jme/if_jme.c,v 1.2 2008/07/18 04:20:48 yongari Exp $
28 * $DragonFly: src/sys/dev/netif/jme/if_jme.c,v 1.12 2008/11/26 11:55:18 sephe Exp $
31 #include "opt_polling.h"
35 #include <sys/param.h>
36 #include <sys/endian.h>
37 #include <sys/kernel.h>
39 #include <sys/interrupt.h>
40 #include <sys/malloc.h>
43 #include <sys/serialize.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/sysctl.h>
48 #include <net/ethernet.h>
51 #include <net/if_arp.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/ifq_var.h>
55 #include <net/toeplitz.h>
56 #include <net/toeplitz2.h>
57 #include <net/vlan/if_vlan_var.h>
58 #include <net/vlan/if_vlan_ether.h>
60 #include <netinet/in.h>
62 #include <dev/netif/mii_layer/miivar.h>
63 #include <dev/netif/mii_layer/jmphyreg.h>
65 #include <bus/pci/pcireg.h>
66 #include <bus/pci/pcivar.h>
67 #include <bus/pci/pcidevs.h>
69 #include <dev/netif/jme/if_jmereg.h>
70 #include <dev/netif/jme/if_jmevar.h>
72 #include "miibus_if.h"
74 /* Define the following to disable printing Rx errors. */
75 #undef JME_SHOW_ERRORS
77 #define JME_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
80 #define JME_RSS_DPRINTF(sc, lvl, fmt, ...) \
82 if ((sc)->jme_rss_debug >= (lvl)) \
83 if_printf(&(sc)->arpcom.ac_if, fmt, __VA_ARGS__); \
85 #else /* !JME_RSS_DEBUG */
86 #define JME_RSS_DPRINTF(sc, lvl, fmt, ...) ((void)0)
87 #endif /* JME_RSS_DEBUG */
89 static int jme_probe(device_t);
90 static int jme_attach(device_t);
91 static int jme_detach(device_t);
92 static int jme_shutdown(device_t);
93 static int jme_suspend(device_t);
94 static int jme_resume(device_t);
96 static int jme_miibus_readreg(device_t, int, int);
97 static int jme_miibus_writereg(device_t, int, int, int);
98 static void jme_miibus_statchg(device_t);
100 static void jme_init(void *);
101 static int jme_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
102 static void jme_start(struct ifnet *);
103 static void jme_watchdog(struct ifnet *);
104 static void jme_mediastatus(struct ifnet *, struct ifmediareq *);
105 static int jme_mediachange(struct ifnet *);
106 #ifdef DEVICE_POLLING
107 static void jme_poll(struct ifnet *, enum poll_cmd, int);
110 static void jme_intr(void *);
111 static void jme_txeof(struct jme_softc *);
112 static void jme_rxeof(struct jme_softc *, int);
113 static int jme_rxeof_chain(struct jme_softc *, int,
114 struct mbuf_chain *, int);
115 static void jme_rx_intr(struct jme_softc *, uint32_t);
117 static int jme_dma_alloc(struct jme_softc *);
118 static void jme_dma_free(struct jme_softc *);
119 static int jme_init_rx_ring(struct jme_softc *, int);
120 static void jme_init_tx_ring(struct jme_softc *);
121 static void jme_init_ssb(struct jme_softc *);
122 static int jme_newbuf(struct jme_softc *, int, struct jme_rxdesc *, int);
123 static int jme_encap(struct jme_softc *, struct mbuf **);
124 static void jme_rxpkt(struct jme_softc *, int, struct mbuf_chain *);
125 static int jme_rxring_dma_alloc(struct jme_softc *, int);
126 static int jme_rxbuf_dma_alloc(struct jme_softc *, int);
128 static void jme_tick(void *);
129 static void jme_stop(struct jme_softc *);
130 static void jme_reset(struct jme_softc *);
131 static void jme_set_vlan(struct jme_softc *);
132 static void jme_set_filter(struct jme_softc *);
133 static void jme_stop_tx(struct jme_softc *);
134 static void jme_stop_rx(struct jme_softc *);
135 static void jme_mac_config(struct jme_softc *);
136 static void jme_reg_macaddr(struct jme_softc *, uint8_t[]);
137 static int jme_eeprom_macaddr(struct jme_softc *, uint8_t[]);
138 static int jme_eeprom_read_byte(struct jme_softc *, uint8_t, uint8_t *);
140 static void jme_setwol(struct jme_softc *);
141 static void jme_setlinkspeed(struct jme_softc *);
143 static void jme_set_tx_coal(struct jme_softc *);
144 static void jme_set_rx_coal(struct jme_softc *);
145 static void jme_enable_rss(struct jme_softc *);
146 static void jme_disable_rss(struct jme_softc *);
148 static void jme_sysctl_node(struct jme_softc *);
149 static int jme_sysctl_tx_coal_to(SYSCTL_HANDLER_ARGS);
150 static int jme_sysctl_tx_coal_pkt(SYSCTL_HANDLER_ARGS);
151 static int jme_sysctl_rx_coal_to(SYSCTL_HANDLER_ARGS);
152 static int jme_sysctl_rx_coal_pkt(SYSCTL_HANDLER_ARGS);
155 * Devices supported by this driver.
157 static const struct jme_dev {
158 uint16_t jme_vendorid;
159 uint16_t jme_deviceid;
161 const char *jme_name;
163 { PCI_VENDOR_JMICRON, PCI_PRODUCT_JMICRON_JMC250,
165 "JMicron Inc, JMC250 Gigabit Ethernet" },
166 { PCI_VENDOR_JMICRON, PCI_PRODUCT_JMICRON_JMC260,
168 "JMicron Inc, JMC260 Fast Ethernet" },
172 static device_method_t jme_methods[] = {
173 /* Device interface. */
174 DEVMETHOD(device_probe, jme_probe),
175 DEVMETHOD(device_attach, jme_attach),
176 DEVMETHOD(device_detach, jme_detach),
177 DEVMETHOD(device_shutdown, jme_shutdown),
178 DEVMETHOD(device_suspend, jme_suspend),
179 DEVMETHOD(device_resume, jme_resume),
182 DEVMETHOD(bus_print_child, bus_generic_print_child),
183 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
186 DEVMETHOD(miibus_readreg, jme_miibus_readreg),
187 DEVMETHOD(miibus_writereg, jme_miibus_writereg),
188 DEVMETHOD(miibus_statchg, jme_miibus_statchg),
193 static driver_t jme_driver = {
196 sizeof(struct jme_softc)
199 static devclass_t jme_devclass;
201 DECLARE_DUMMY_MODULE(if_jme);
202 MODULE_DEPEND(if_jme, miibus, 1, 1, 1);
203 DRIVER_MODULE(if_jme, pci, jme_driver, jme_devclass, 0, 0);
204 DRIVER_MODULE(miibus, jme, miibus_driver, miibus_devclass, 0, 0);
206 static const struct {
209 } jme_rx_status[JME_NRXRING_MAX] = {
210 { INTR_RXQ0_COAL | INTR_RXQ0_COAL_TO, INTR_RXQ0_COMP },
211 { INTR_RXQ1_COAL | INTR_RXQ1_COAL_TO, INTR_RXQ1_COMP },
212 { INTR_RXQ2_COAL | INTR_RXQ2_COAL_TO, INTR_RXQ2_COMP },
213 { INTR_RXQ3_COAL | INTR_RXQ3_COAL_TO, INTR_RXQ3_COMP }
216 static int jme_rx_desc_count = JME_RX_DESC_CNT_DEF;
217 static int jme_tx_desc_count = JME_TX_DESC_CNT_DEF;
218 static int jme_rx_ring_count = JME_NRXRING_DEF;
220 TUNABLE_INT("hw.jme.rx_desc_count", &jme_rx_desc_count);
221 TUNABLE_INT("hw.jme.tx_desc_count", &jme_tx_desc_count);
222 TUNABLE_INT("hw.jme.rx_ring_count", &jme_rx_ring_count);
225 * Read a PHY register on the MII of the JMC250.
228 jme_miibus_readreg(device_t dev, int phy, int reg)
230 struct jme_softc *sc = device_get_softc(dev);
234 /* For FPGA version, PHY address 0 should be ignored. */
235 if (sc->jme_caps & JME_CAP_FPGA) {
239 if (sc->jme_phyaddr != phy)
243 CSR_WRITE_4(sc, JME_SMI, SMI_OP_READ | SMI_OP_EXECUTE |
244 SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg));
246 for (i = JME_PHY_TIMEOUT; i > 0; i--) {
248 if (((val = CSR_READ_4(sc, JME_SMI)) & SMI_OP_EXECUTE) == 0)
252 device_printf(sc->jme_dev, "phy read timeout: "
253 "phy %d, reg %d\n", phy, reg);
257 return ((val & SMI_DATA_MASK) >> SMI_DATA_SHIFT);
261 * Write a PHY register on the MII of the JMC250.
264 jme_miibus_writereg(device_t dev, int phy, int reg, int val)
266 struct jme_softc *sc = device_get_softc(dev);
269 /* For FPGA version, PHY address 0 should be ignored. */
270 if (sc->jme_caps & JME_CAP_FPGA) {
274 if (sc->jme_phyaddr != phy)
278 CSR_WRITE_4(sc, JME_SMI, SMI_OP_WRITE | SMI_OP_EXECUTE |
279 ((val << SMI_DATA_SHIFT) & SMI_DATA_MASK) |
280 SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg));
282 for (i = JME_PHY_TIMEOUT; i > 0; i--) {
284 if (((val = CSR_READ_4(sc, JME_SMI)) & SMI_OP_EXECUTE) == 0)
288 device_printf(sc->jme_dev, "phy write timeout: "
289 "phy %d, reg %d\n", phy, reg);
296 * Callback from MII layer when media changes.
299 jme_miibus_statchg(device_t dev)
301 struct jme_softc *sc = device_get_softc(dev);
302 struct ifnet *ifp = &sc->arpcom.ac_if;
303 struct mii_data *mii;
304 struct jme_txdesc *txd;
308 ASSERT_SERIALIZED(ifp->if_serializer);
310 if ((ifp->if_flags & IFF_RUNNING) == 0)
313 mii = device_get_softc(sc->jme_miibus);
315 sc->jme_flags &= ~JME_FLAG_LINK;
316 if ((mii->mii_media_status & IFM_AVALID) != 0) {
317 switch (IFM_SUBTYPE(mii->mii_media_active)) {
320 sc->jme_flags |= JME_FLAG_LINK;
323 if (sc->jme_caps & JME_CAP_FASTETH)
325 sc->jme_flags |= JME_FLAG_LINK;
333 * Disabling Rx/Tx MACs have a side-effect of resetting
334 * JME_TXNDA/JME_RXNDA register to the first address of
335 * Tx/Rx descriptor address. So driver should reset its
336 * internal procucer/consumer pointer and reclaim any
337 * allocated resources. Note, just saving the value of
338 * JME_TXNDA and JME_RXNDA registers before stopping MAC
339 * and restoring JME_TXNDA/JME_RXNDA register is not
340 * sufficient to make sure correct MAC state because
341 * stopping MAC operation can take a while and hardware
342 * might have updated JME_TXNDA/JME_RXNDA registers
343 * during the stop operation.
346 /* Disable interrupts */
347 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
350 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
352 callout_stop(&sc->jme_tick_ch);
354 /* Stop receiver/transmitter. */
358 for (r = 0; r < sc->jme_rx_ring_inuse; ++r) {
359 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
362 if (rdata->jme_rxhead != NULL)
363 m_freem(rdata->jme_rxhead);
364 JME_RXCHAIN_RESET(sc, r);
367 * Reuse configured Rx descriptors and reset
368 * procuder/consumer index.
370 rdata->jme_rx_cons = 0;
374 if (sc->jme_cdata.jme_tx_cnt != 0) {
375 /* Remove queued packets for transmit. */
376 for (i = 0; i < sc->jme_tx_desc_cnt; i++) {
377 txd = &sc->jme_cdata.jme_txdesc[i];
378 if (txd->tx_m != NULL) {
380 sc->jme_cdata.jme_tx_tag,
389 jme_init_tx_ring(sc);
391 /* Initialize shadow status block. */
394 /* Program MAC with resolved speed/duplex/flow-control. */
395 if (sc->jme_flags & JME_FLAG_LINK) {
398 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr);
400 /* Set Tx ring address to the hardware. */
401 paddr = sc->jme_cdata.jme_tx_ring_paddr;
402 CSR_WRITE_4(sc, JME_TXDBA_HI, JME_ADDR_HI(paddr));
403 CSR_WRITE_4(sc, JME_TXDBA_LO, JME_ADDR_LO(paddr));
405 for (r = 0; r < sc->jme_rx_ring_inuse; ++r) {
406 CSR_WRITE_4(sc, JME_RXCSR,
407 sc->jme_rxcsr | RXCSR_RXQ_N_SEL(r));
409 /* Set Rx ring address to the hardware. */
410 paddr = sc->jme_cdata.jme_rx_data[r].jme_rx_ring_paddr;
411 CSR_WRITE_4(sc, JME_RXDBA_HI, JME_ADDR_HI(paddr));
412 CSR_WRITE_4(sc, JME_RXDBA_LO, JME_ADDR_LO(paddr));
415 /* Restart receiver/transmitter. */
416 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RX_ENB |
418 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr | TXCSR_TX_ENB);
421 ifp->if_flags |= IFF_RUNNING;
422 ifp->if_flags &= ~IFF_OACTIVE;
423 callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
425 #ifdef DEVICE_POLLING
426 if (!(ifp->if_flags & IFF_POLLING))
428 /* Reenable interrupts. */
429 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
433 * Get the current interface media status.
436 jme_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
438 struct jme_softc *sc = ifp->if_softc;
439 struct mii_data *mii = device_get_softc(sc->jme_miibus);
441 ASSERT_SERIALIZED(ifp->if_serializer);
444 ifmr->ifm_status = mii->mii_media_status;
445 ifmr->ifm_active = mii->mii_media_active;
449 * Set hardware to newly-selected media.
452 jme_mediachange(struct ifnet *ifp)
454 struct jme_softc *sc = ifp->if_softc;
455 struct mii_data *mii = device_get_softc(sc->jme_miibus);
458 ASSERT_SERIALIZED(ifp->if_serializer);
460 if (mii->mii_instance != 0) {
461 struct mii_softc *miisc;
463 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
464 mii_phy_reset(miisc);
466 error = mii_mediachg(mii);
472 jme_probe(device_t dev)
474 const struct jme_dev *sp;
477 vid = pci_get_vendor(dev);
478 did = pci_get_device(dev);
479 for (sp = jme_devs; sp->jme_name != NULL; ++sp) {
480 if (vid == sp->jme_vendorid && did == sp->jme_deviceid) {
481 struct jme_softc *sc = device_get_softc(dev);
483 sc->jme_caps = sp->jme_caps;
484 device_set_desc(dev, sp->jme_name);
492 jme_eeprom_read_byte(struct jme_softc *sc, uint8_t addr, uint8_t *val)
498 for (i = JME_TIMEOUT; i > 0; i--) {
499 reg = CSR_READ_4(sc, JME_SMBCSR);
500 if ((reg & SMBCSR_HW_BUSY_MASK) == SMBCSR_HW_IDLE)
506 device_printf(sc->jme_dev, "EEPROM idle timeout!\n");
510 reg = ((uint32_t)addr << SMBINTF_ADDR_SHIFT) & SMBINTF_ADDR_MASK;
511 CSR_WRITE_4(sc, JME_SMBINTF, reg | SMBINTF_RD | SMBINTF_CMD_TRIGGER);
512 for (i = JME_TIMEOUT; i > 0; i--) {
514 reg = CSR_READ_4(sc, JME_SMBINTF);
515 if ((reg & SMBINTF_CMD_TRIGGER) == 0)
520 device_printf(sc->jme_dev, "EEPROM read timeout!\n");
524 reg = CSR_READ_4(sc, JME_SMBINTF);
525 *val = (reg & SMBINTF_RD_DATA_MASK) >> SMBINTF_RD_DATA_SHIFT;
531 jme_eeprom_macaddr(struct jme_softc *sc, uint8_t eaddr[])
533 uint8_t fup, reg, val;
538 if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 ||
539 fup != JME_EEPROM_SIG0)
541 if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 ||
542 fup != JME_EEPROM_SIG1)
546 if (jme_eeprom_read_byte(sc, offset, &fup) != 0)
548 if (JME_EEPROM_MKDESC(JME_EEPROM_FUNC0, JME_EEPROM_PAGE_BAR1) ==
549 (fup & (JME_EEPROM_FUNC_MASK | JME_EEPROM_PAGE_MASK))) {
550 if (jme_eeprom_read_byte(sc, offset + 1, ®) != 0)
552 if (reg >= JME_PAR0 &&
553 reg < JME_PAR0 + ETHER_ADDR_LEN) {
554 if (jme_eeprom_read_byte(sc, offset + 2,
557 eaddr[reg - JME_PAR0] = val;
561 /* Check for the end of EEPROM descriptor. */
562 if ((fup & JME_EEPROM_DESC_END) == JME_EEPROM_DESC_END)
564 /* Try next eeprom descriptor. */
565 offset += JME_EEPROM_DESC_BYTES;
566 } while (match != ETHER_ADDR_LEN && offset < JME_EEPROM_END);
568 if (match == ETHER_ADDR_LEN)
575 jme_reg_macaddr(struct jme_softc *sc, uint8_t eaddr[])
579 /* Read station address. */
580 par0 = CSR_READ_4(sc, JME_PAR0);
581 par1 = CSR_READ_4(sc, JME_PAR1);
583 if ((par0 == 0 && par1 == 0) || (par0 & 0x1)) {
584 device_printf(sc->jme_dev,
585 "generating fake ethernet address.\n");
586 par0 = karc4random();
587 /* Set OUI to JMicron. */
591 eaddr[3] = (par0 >> 16) & 0xff;
592 eaddr[4] = (par0 >> 8) & 0xff;
593 eaddr[5] = par0 & 0xff;
595 eaddr[0] = (par0 >> 0) & 0xFF;
596 eaddr[1] = (par0 >> 8) & 0xFF;
597 eaddr[2] = (par0 >> 16) & 0xFF;
598 eaddr[3] = (par0 >> 24) & 0xFF;
599 eaddr[4] = (par1 >> 0) & 0xFF;
600 eaddr[5] = (par1 >> 8) & 0xFF;
605 jme_attach(device_t dev)
607 struct jme_softc *sc = device_get_softc(dev);
608 struct ifnet *ifp = &sc->arpcom.ac_if;
611 uint8_t pcie_ptr, rev;
613 uint8_t eaddr[ETHER_ADDR_LEN];
615 sc->jme_rx_desc_cnt = roundup(jme_rx_desc_count, JME_NDESC_ALIGN);
616 if (sc->jme_rx_desc_cnt > JME_NDESC_MAX)
617 sc->jme_rx_desc_cnt = JME_NDESC_MAX;
619 sc->jme_tx_desc_cnt = roundup(jme_tx_desc_count, JME_NDESC_ALIGN);
620 if (sc->jme_tx_desc_cnt > JME_NDESC_MAX)
621 sc->jme_tx_desc_cnt = JME_NDESC_MAX;
624 sc->jme_rx_ring_cnt = jme_rx_ring_count;
625 if (sc->jme_rx_ring_cnt <= 0)
626 sc->jme_rx_ring_cnt = JME_NRXRING_1;
627 if (sc->jme_rx_ring_cnt > ncpus2)
628 sc->jme_rx_ring_cnt = ncpus2;
630 if (sc->jme_rx_ring_cnt >= JME_NRXRING_4)
631 sc->jme_rx_ring_cnt = JME_NRXRING_4;
632 else if (sc->jme_rx_ring_cnt >= JME_NRXRING_2)
633 sc->jme_rx_ring_cnt = JME_NRXRING_2;
635 sc->jme_rx_ring_cnt = JME_NRXRING_MIN;
637 sc->jme_rx_ring_inuse = sc->jme_rx_ring_cnt;
640 sc->jme_lowaddr = BUS_SPACE_MAXADDR;
642 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
644 callout_init(&sc->jme_tick_ch);
647 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
650 irq = pci_read_config(dev, PCIR_INTLINE, 4);
651 mem = pci_read_config(dev, JME_PCIR_BAR, 4);
653 device_printf(dev, "chip is in D%d power mode "
654 "-- setting to D0\n", pci_get_powerstate(dev));
656 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
658 pci_write_config(dev, PCIR_INTLINE, irq, 4);
659 pci_write_config(dev, JME_PCIR_BAR, mem, 4);
661 #endif /* !BURN_BRIDGE */
663 /* Enable bus mastering */
664 pci_enable_busmaster(dev);
669 * JMC250 supports both memory mapped and I/O register space
670 * access. Because I/O register access should use different
671 * BARs to access registers it's waste of time to use I/O
672 * register spce access. JMC250 uses 16K to map entire memory
675 sc->jme_mem_rid = JME_PCIR_BAR;
676 sc->jme_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
677 &sc->jme_mem_rid, RF_ACTIVE);
678 if (sc->jme_mem_res == NULL) {
679 device_printf(dev, "can't allocate IO memory\n");
682 sc->jme_mem_bt = rman_get_bustag(sc->jme_mem_res);
683 sc->jme_mem_bh = rman_get_bushandle(sc->jme_mem_res);
689 sc->jme_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
691 RF_SHAREABLE | RF_ACTIVE);
692 if (sc->jme_irq_res == NULL) {
693 device_printf(dev, "can't allocate irq\n");
701 reg = CSR_READ_4(sc, JME_CHIPMODE);
702 if (((reg & CHIPMODE_FPGA_REV_MASK) >> CHIPMODE_FPGA_REV_SHIFT) !=
704 sc->jme_caps |= JME_CAP_FPGA;
706 device_printf(dev, "FPGA revision: 0x%04x\n",
707 (reg & CHIPMODE_FPGA_REV_MASK) >>
708 CHIPMODE_FPGA_REV_SHIFT);
712 /* NOTE: FM revision is put in the upper 4 bits */
713 rev = ((reg & CHIPMODE_REVFM_MASK) >> CHIPMODE_REVFM_SHIFT) << 4;
714 rev |= (reg & CHIPMODE_REVECO_MASK) >> CHIPMODE_REVECO_SHIFT;
716 device_printf(dev, "Revision (FM/ECO): 0x%02x\n", rev);
718 did = pci_get_device(dev);
720 case PCI_PRODUCT_JMICRON_JMC250:
721 if (rev == JME_REV1_A2)
722 sc->jme_workaround |= JME_WA_EXTFIFO | JME_WA_HDX;
725 case PCI_PRODUCT_JMICRON_JMC260:
727 sc->jme_lowaddr = BUS_SPACE_MAXADDR_32BIT;
731 panic("unknown device id 0x%04x\n", did);
733 if (rev >= JME_REV2) {
734 sc->jme_clksrc = GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC;
735 sc->jme_clksrc_1000 = GHC_TXOFL_CLKSRC_1000 |
736 GHC_TXMAC_CLKSRC_1000;
739 /* Reset the ethernet controller. */
742 /* Get station address. */
743 reg = CSR_READ_4(sc, JME_SMBCSR);
744 if (reg & SMBCSR_EEPROM_PRESENT)
745 error = jme_eeprom_macaddr(sc, eaddr);
746 if (error != 0 || (reg & SMBCSR_EEPROM_PRESENT) == 0) {
747 if (error != 0 && (bootverbose)) {
748 device_printf(dev, "ethernet hardware address "
749 "not found in EEPROM.\n");
751 jme_reg_macaddr(sc, eaddr);
756 * Integrated JR0211 has fixed PHY address whereas FPGA version
757 * requires PHY probing to get correct PHY address.
759 if ((sc->jme_caps & JME_CAP_FPGA) == 0) {
760 sc->jme_phyaddr = CSR_READ_4(sc, JME_GPREG0) &
761 GPREG0_PHY_ADDR_MASK;
763 device_printf(dev, "PHY is at address %d.\n",
770 /* Set max allowable DMA size. */
771 pcie_ptr = pci_get_pciecap_ptr(dev);
775 sc->jme_caps |= JME_CAP_PCIE;
776 ctrl = pci_read_config(dev, pcie_ptr + PCIER_DEVCTRL, 2);
778 device_printf(dev, "Read request size : %d bytes.\n",
779 128 << ((ctrl >> 12) & 0x07));
780 device_printf(dev, "TLP payload size : %d bytes.\n",
781 128 << ((ctrl >> 5) & 0x07));
783 switch (ctrl & PCIEM_DEVCTL_MAX_READRQ_MASK) {
784 case PCIEM_DEVCTL_MAX_READRQ_128:
785 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_128;
787 case PCIEM_DEVCTL_MAX_READRQ_256:
788 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_256;
791 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_512;
794 sc->jme_rx_dma_size = RXCSR_DMA_SIZE_128;
796 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_512;
797 sc->jme_rx_dma_size = RXCSR_DMA_SIZE_128;
801 if (pci_find_extcap(dev, PCIY_PMG, &pmc) == 0)
802 sc->jme_caps |= JME_CAP_PMCAP;
810 /* Allocate DMA stuffs */
811 error = jme_dma_alloc(sc);
816 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
817 ifp->if_init = jme_init;
818 ifp->if_ioctl = jme_ioctl;
819 ifp->if_start = jme_start;
820 #ifdef DEVICE_POLLING
821 ifp->if_poll = jme_poll;
823 ifp->if_watchdog = jme_watchdog;
824 ifq_set_maxlen(&ifp->if_snd, sc->jme_tx_desc_cnt - JME_TXD_RSVD);
825 ifq_set_ready(&ifp->if_snd);
827 /* JMC250 supports Tx/Rx checksum offload and hardware vlan tagging. */
828 ifp->if_capabilities = IFCAP_HWCSUM |
830 IFCAP_VLAN_HWTAGGING;
831 if (sc->jme_rx_ring_cnt > JME_NRXRING_MIN)
832 ifp->if_capabilities |= IFCAP_RSS;
833 ifp->if_capenable = ifp->if_capabilities;
836 * Disable TXCSUM by default to improve bulk data
837 * transmit performance (+20Mbps improvement).
839 ifp->if_capenable &= ~IFCAP_TXCSUM;
841 if (ifp->if_capenable & IFCAP_TXCSUM)
842 ifp->if_hwassist = JME_CSUM_FEATURES;
844 /* Set up MII bus. */
845 error = mii_phy_probe(dev, &sc->jme_miibus,
846 jme_mediachange, jme_mediastatus);
848 device_printf(dev, "no PHY found!\n");
853 * Save PHYADDR for FPGA mode PHY.
855 if (sc->jme_caps & JME_CAP_FPGA) {
856 struct mii_data *mii = device_get_softc(sc->jme_miibus);
858 if (mii->mii_instance != 0) {
859 struct mii_softc *miisc;
861 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
862 if (miisc->mii_phy != 0) {
863 sc->jme_phyaddr = miisc->mii_phy;
867 if (sc->jme_phyaddr != 0) {
868 device_printf(sc->jme_dev,
869 "FPGA PHY is at %d\n", sc->jme_phyaddr);
871 jme_miibus_writereg(dev, sc->jme_phyaddr,
872 JMPHY_CONF, JMPHY_CONF_DEFFIFO);
874 /* XXX should we clear JME_WA_EXTFIFO */
879 ether_ifattach(ifp, eaddr, NULL);
881 /* Tell the upper layer(s) we support long frames. */
882 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
884 error = bus_setup_intr(dev, sc->jme_irq_res, INTR_MPSAFE, jme_intr, sc,
885 &sc->jme_irq_handle, ifp->if_serializer);
887 device_printf(dev, "could not set up interrupt handler.\n");
892 ifp->if_cpuid = ithread_cpuid(rman_get_start(sc->jme_irq_res));
893 KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
901 jme_detach(device_t dev)
903 struct jme_softc *sc = device_get_softc(dev);
905 if (device_is_attached(dev)) {
906 struct ifnet *ifp = &sc->arpcom.ac_if;
908 lwkt_serialize_enter(ifp->if_serializer);
910 bus_teardown_intr(dev, sc->jme_irq_res, sc->jme_irq_handle);
911 lwkt_serialize_exit(ifp->if_serializer);
916 if (sc->jme_sysctl_tree != NULL)
917 sysctl_ctx_free(&sc->jme_sysctl_ctx);
919 if (sc->jme_miibus != NULL)
920 device_delete_child(dev, sc->jme_miibus);
921 bus_generic_detach(dev);
923 if (sc->jme_irq_res != NULL) {
924 bus_release_resource(dev, SYS_RES_IRQ, sc->jme_irq_rid,
928 if (sc->jme_mem_res != NULL) {
929 bus_release_resource(dev, SYS_RES_MEMORY, sc->jme_mem_rid,
939 jme_sysctl_node(struct jme_softc *sc)
943 char rx_ring_pkt[32];
947 sysctl_ctx_init(&sc->jme_sysctl_ctx);
948 sc->jme_sysctl_tree = SYSCTL_ADD_NODE(&sc->jme_sysctl_ctx,
949 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
950 device_get_nameunit(sc->jme_dev),
952 if (sc->jme_sysctl_tree == NULL) {
953 device_printf(sc->jme_dev, "can't add sysctl node\n");
957 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
958 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
959 "tx_coal_to", CTLTYPE_INT | CTLFLAG_RW,
960 sc, 0, jme_sysctl_tx_coal_to, "I", "jme tx coalescing timeout");
962 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
963 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
964 "tx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW,
965 sc, 0, jme_sysctl_tx_coal_pkt, "I", "jme tx coalescing packet");
967 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
968 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
969 "rx_coal_to", CTLTYPE_INT | CTLFLAG_RW,
970 sc, 0, jme_sysctl_rx_coal_to, "I", "jme rx coalescing timeout");
972 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
973 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
974 "rx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW,
975 sc, 0, jme_sysctl_rx_coal_pkt, "I", "jme rx coalescing packet");
977 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
978 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
979 "rx_desc_count", CTLFLAG_RD, &sc->jme_rx_desc_cnt,
981 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
982 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
983 "tx_desc_count", CTLFLAG_RD, &sc->jme_tx_desc_cnt,
985 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
986 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
987 "rx_ring_count", CTLFLAG_RD, &sc->jme_rx_ring_cnt,
989 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
990 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
991 "rx_ring_inuse", CTLFLAG_RD, &sc->jme_rx_ring_inuse,
992 0, "RX ring in use");
994 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
995 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
996 "rss_debug", CTLFLAG_RW, &sc->jme_rss_debug,
997 0, "RSS debug level");
998 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
999 ksnprintf(rx_ring_pkt, sizeof(rx_ring_pkt), "rx_ring%d_pkt", r);
1000 SYSCTL_ADD_UINT(&sc->jme_sysctl_ctx,
1001 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1002 rx_ring_pkt, CTLFLAG_RW,
1003 &sc->jme_rx_ring_pkt[r],
1009 * Set default coalesce valves
1011 sc->jme_tx_coal_to = PCCTX_COAL_TO_DEFAULT;
1012 sc->jme_tx_coal_pkt = PCCTX_COAL_PKT_DEFAULT;
1013 sc->jme_rx_coal_to = PCCRX_COAL_TO_DEFAULT;
1014 sc->jme_rx_coal_pkt = PCCRX_COAL_PKT_DEFAULT;
1017 * Adjust coalesce valves, in case that the number of TX/RX
1018 * descs are set to small values by users.
1020 * NOTE: coal_max will not be zero, since number of descs
1021 * must aligned by JME_NDESC_ALIGN (16 currently)
1023 coal_max = sc->jme_tx_desc_cnt / 6;
1024 if (coal_max < sc->jme_tx_coal_pkt)
1025 sc->jme_tx_coal_pkt = coal_max;
1027 coal_max = sc->jme_rx_desc_cnt / 4;
1028 if (coal_max < sc->jme_rx_coal_pkt)
1029 sc->jme_rx_coal_pkt = coal_max;
1033 jme_dma_alloc(struct jme_softc *sc)
1035 struct jme_txdesc *txd;
1039 sc->jme_cdata.jme_txdesc =
1040 kmalloc(sc->jme_tx_desc_cnt * sizeof(struct jme_txdesc),
1041 M_DEVBUF, M_WAITOK | M_ZERO);
1042 for (i = 0; i < sc->jme_rx_ring_cnt; ++i) {
1043 sc->jme_cdata.jme_rx_data[i].jme_rxdesc =
1044 kmalloc(sc->jme_rx_desc_cnt * sizeof(struct jme_rxdesc),
1045 M_DEVBUF, M_WAITOK | M_ZERO);
1048 /* Create parent ring tag. */
1049 error = bus_dma_tag_create(NULL,/* parent */
1050 1, JME_RING_BOUNDARY, /* algnmnt, boundary */
1051 sc->jme_lowaddr, /* lowaddr */
1052 BUS_SPACE_MAXADDR, /* highaddr */
1053 NULL, NULL, /* filter, filterarg */
1054 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1056 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1058 &sc->jme_cdata.jme_ring_tag);
1060 device_printf(sc->jme_dev,
1061 "could not create parent ring DMA tag.\n");
1066 * Create DMA stuffs for TX ring
1068 error = bus_dmamem_coherent(sc->jme_cdata.jme_ring_tag,
1069 JME_TX_RING_ALIGN, 0,
1070 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1071 JME_TX_RING_SIZE(sc),
1072 BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
1074 device_printf(sc->jme_dev, "could not allocate Tx ring.\n");
1077 sc->jme_cdata.jme_tx_ring_tag = dmem.dmem_tag;
1078 sc->jme_cdata.jme_tx_ring_map = dmem.dmem_map;
1079 sc->jme_cdata.jme_tx_ring = dmem.dmem_addr;
1080 sc->jme_cdata.jme_tx_ring_paddr = dmem.dmem_busaddr;
1083 * Create DMA stuffs for RX rings
1085 for (i = 0; i < sc->jme_rx_ring_cnt; ++i) {
1086 error = jme_rxring_dma_alloc(sc, i);
1091 /* Create parent buffer tag. */
1092 error = bus_dma_tag_create(NULL,/* parent */
1093 1, 0, /* algnmnt, boundary */
1094 sc->jme_lowaddr, /* lowaddr */
1095 BUS_SPACE_MAXADDR, /* highaddr */
1096 NULL, NULL, /* filter, filterarg */
1097 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1099 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1101 &sc->jme_cdata.jme_buffer_tag);
1103 device_printf(sc->jme_dev,
1104 "could not create parent buffer DMA tag.\n");
1109 * Create DMA stuffs for shadow status block
1111 error = bus_dmamem_coherent(sc->jme_cdata.jme_buffer_tag,
1112 JME_SSB_ALIGN, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1113 JME_SSB_SIZE, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
1115 device_printf(sc->jme_dev,
1116 "could not create shadow status block.\n");
1119 sc->jme_cdata.jme_ssb_tag = dmem.dmem_tag;
1120 sc->jme_cdata.jme_ssb_map = dmem.dmem_map;
1121 sc->jme_cdata.jme_ssb_block = dmem.dmem_addr;
1122 sc->jme_cdata.jme_ssb_block_paddr = dmem.dmem_busaddr;
1125 * Create DMA stuffs for TX buffers
1128 /* Create tag for Tx buffers. */
1129 error = bus_dma_tag_create(sc->jme_cdata.jme_buffer_tag,/* parent */
1130 1, 0, /* algnmnt, boundary */
1131 BUS_SPACE_MAXADDR, /* lowaddr */
1132 BUS_SPACE_MAXADDR, /* highaddr */
1133 NULL, NULL, /* filter, filterarg */
1134 JME_JUMBO_FRAMELEN, /* maxsize */
1135 JME_MAXTXSEGS, /* nsegments */
1136 JME_MAXSEGSIZE, /* maxsegsize */
1137 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,/* flags */
1138 &sc->jme_cdata.jme_tx_tag);
1140 device_printf(sc->jme_dev, "could not create Tx DMA tag.\n");
1144 /* Create DMA maps for Tx buffers. */
1145 for (i = 0; i < sc->jme_tx_desc_cnt; i++) {
1146 txd = &sc->jme_cdata.jme_txdesc[i];
1147 error = bus_dmamap_create(sc->jme_cdata.jme_tx_tag,
1148 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
1153 device_printf(sc->jme_dev,
1154 "could not create %dth Tx dmamap.\n", i);
1156 for (j = 0; j < i; ++j) {
1157 txd = &sc->jme_cdata.jme_txdesc[j];
1158 bus_dmamap_destroy(sc->jme_cdata.jme_tx_tag,
1161 bus_dma_tag_destroy(sc->jme_cdata.jme_tx_tag);
1162 sc->jme_cdata.jme_tx_tag = NULL;
1168 * Create DMA stuffs for RX buffers
1170 for (i = 0; i < sc->jme_rx_ring_cnt; ++i) {
1171 error = jme_rxbuf_dma_alloc(sc, i);
1179 jme_dma_free(struct jme_softc *sc)
1181 struct jme_txdesc *txd;
1182 struct jme_rxdesc *rxd;
1183 struct jme_rxdata *rdata;
1187 if (sc->jme_cdata.jme_tx_ring_tag != NULL) {
1188 bus_dmamap_unload(sc->jme_cdata.jme_tx_ring_tag,
1189 sc->jme_cdata.jme_tx_ring_map);
1190 bus_dmamem_free(sc->jme_cdata.jme_tx_ring_tag,
1191 sc->jme_cdata.jme_tx_ring,
1192 sc->jme_cdata.jme_tx_ring_map);
1193 bus_dma_tag_destroy(sc->jme_cdata.jme_tx_ring_tag);
1194 sc->jme_cdata.jme_tx_ring_tag = NULL;
1198 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
1199 rdata = &sc->jme_cdata.jme_rx_data[r];
1200 if (rdata->jme_rx_ring_tag != NULL) {
1201 bus_dmamap_unload(rdata->jme_rx_ring_tag,
1202 rdata->jme_rx_ring_map);
1203 bus_dmamem_free(rdata->jme_rx_ring_tag,
1205 rdata->jme_rx_ring_map);
1206 bus_dma_tag_destroy(rdata->jme_rx_ring_tag);
1207 rdata->jme_rx_ring_tag = NULL;
1212 if (sc->jme_cdata.jme_tx_tag != NULL) {
1213 for (i = 0; i < sc->jme_tx_desc_cnt; i++) {
1214 txd = &sc->jme_cdata.jme_txdesc[i];
1215 bus_dmamap_destroy(sc->jme_cdata.jme_tx_tag,
1218 bus_dma_tag_destroy(sc->jme_cdata.jme_tx_tag);
1219 sc->jme_cdata.jme_tx_tag = NULL;
1223 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
1224 rdata = &sc->jme_cdata.jme_rx_data[r];
1225 if (rdata->jme_rx_tag != NULL) {
1226 for (i = 0; i < sc->jme_rx_desc_cnt; i++) {
1227 rxd = &rdata->jme_rxdesc[i];
1228 bus_dmamap_destroy(rdata->jme_rx_tag,
1231 bus_dmamap_destroy(rdata->jme_rx_tag,
1232 rdata->jme_rx_sparemap);
1233 bus_dma_tag_destroy(rdata->jme_rx_tag);
1234 rdata->jme_rx_tag = NULL;
1238 /* Shadow status block. */
1239 if (sc->jme_cdata.jme_ssb_tag != NULL) {
1240 bus_dmamap_unload(sc->jme_cdata.jme_ssb_tag,
1241 sc->jme_cdata.jme_ssb_map);
1242 bus_dmamem_free(sc->jme_cdata.jme_ssb_tag,
1243 sc->jme_cdata.jme_ssb_block,
1244 sc->jme_cdata.jme_ssb_map);
1245 bus_dma_tag_destroy(sc->jme_cdata.jme_ssb_tag);
1246 sc->jme_cdata.jme_ssb_tag = NULL;
1249 if (sc->jme_cdata.jme_buffer_tag != NULL) {
1250 bus_dma_tag_destroy(sc->jme_cdata.jme_buffer_tag);
1251 sc->jme_cdata.jme_buffer_tag = NULL;
1253 if (sc->jme_cdata.jme_ring_tag != NULL) {
1254 bus_dma_tag_destroy(sc->jme_cdata.jme_ring_tag);
1255 sc->jme_cdata.jme_ring_tag = NULL;
1258 if (sc->jme_cdata.jme_txdesc != NULL) {
1259 kfree(sc->jme_cdata.jme_txdesc, M_DEVBUF);
1260 sc->jme_cdata.jme_txdesc = NULL;
1262 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
1263 rdata = &sc->jme_cdata.jme_rx_data[r];
1264 if (rdata->jme_rxdesc != NULL) {
1265 kfree(rdata->jme_rxdesc, M_DEVBUF);
1266 rdata->jme_rxdesc = NULL;
1272 * Make sure the interface is stopped at reboot time.
1275 jme_shutdown(device_t dev)
1277 return jme_suspend(dev);
1282 * Unlike other ethernet controllers, JMC250 requires
1283 * explicit resetting link speed to 10/100Mbps as gigabit
1284 * link will cunsume more power than 375mA.
1285 * Note, we reset the link speed to 10/100Mbps with
1286 * auto-negotiation but we don't know whether that operation
1287 * would succeed or not as we have no control after powering
1288 * off. If the renegotiation fail WOL may not work. Running
1289 * at 1Gbps draws more power than 375mA at 3.3V which is
1290 * specified in PCI specification and that would result in
1291 * complete shutdowning power to ethernet controller.
1294 * Save current negotiated media speed/duplex/flow-control
1295 * to softc and restore the same link again after resuming.
1296 * PHY handling such as power down/resetting to 100Mbps
1297 * may be better handled in suspend method in phy driver.
1300 jme_setlinkspeed(struct jme_softc *sc)
1302 struct mii_data *mii;
1305 JME_LOCK_ASSERT(sc);
1307 mii = device_get_softc(sc->jme_miibus);
1310 if ((mii->mii_media_status & IFM_AVALID) != 0) {
1311 switch IFM_SUBTYPE(mii->mii_media_active) {
1321 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_100T2CR, 0);
1322 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_ANAR,
1323 ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
1324 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_BMCR,
1325 BMCR_AUTOEN | BMCR_STARTNEG);
1328 /* Poll link state until jme(4) get a 10/100 link. */
1329 for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
1331 if ((mii->mii_media_status & IFM_AVALID) != 0) {
1332 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1342 pause("jmelnk", hz);
1345 if (i == MII_ANEGTICKS_GIGE)
1346 device_printf(sc->jme_dev, "establishing link failed, "
1347 "WOL may not work!");
1350 * No link, force MAC to have 100Mbps, full-duplex link.
1351 * This is the last resort and may/may not work.
1353 mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
1354 mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1359 jme_setwol(struct jme_softc *sc)
1361 struct ifnet *ifp = &sc->arpcom.ac_if;
1366 if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1367 /* No PME capability, PHY power down. */
1368 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1369 MII_BMCR, BMCR_PDOWN);
1373 gpr = CSR_READ_4(sc, JME_GPREG0) & ~GPREG0_PME_ENB;
1374 pmcs = CSR_READ_4(sc, JME_PMCS);
1375 pmcs &= ~PMCS_WOL_ENB_MASK;
1376 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) {
1377 pmcs |= PMCS_MAGIC_FRAME | PMCS_MAGIC_FRAME_ENB;
1378 /* Enable PME message. */
1379 gpr |= GPREG0_PME_ENB;
1380 /* For gigabit controllers, reset link speed to 10/100. */
1381 if ((sc->jme_caps & JME_CAP_FASTETH) == 0)
1382 jme_setlinkspeed(sc);
1385 CSR_WRITE_4(sc, JME_PMCS, pmcs);
1386 CSR_WRITE_4(sc, JME_GPREG0, gpr);
1389 pmstat = pci_read_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, 2);
1390 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
1391 if ((ifp->if_capenable & IFCAP_WOL) != 0)
1392 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
1393 pci_write_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1394 if ((ifp->if_capenable & IFCAP_WOL) == 0) {
1395 /* No WOL, PHY power down. */
1396 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1397 MII_BMCR, BMCR_PDOWN);
1403 jme_suspend(device_t dev)
1405 struct jme_softc *sc = device_get_softc(dev);
1406 struct ifnet *ifp = &sc->arpcom.ac_if;
1408 lwkt_serialize_enter(ifp->if_serializer);
1413 lwkt_serialize_exit(ifp->if_serializer);
1419 jme_resume(device_t dev)
1421 struct jme_softc *sc = device_get_softc(dev);
1422 struct ifnet *ifp = &sc->arpcom.ac_if;
1427 lwkt_serialize_enter(ifp->if_serializer);
1430 if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1433 pmstat = pci_read_config(sc->jme_dev,
1434 pmc + PCIR_POWER_STATUS, 2);
1435 /* Disable PME clear PME status. */
1436 pmstat &= ~PCIM_PSTAT_PMEENABLE;
1437 pci_write_config(sc->jme_dev,
1438 pmc + PCIR_POWER_STATUS, pmstat, 2);
1442 if (ifp->if_flags & IFF_UP)
1445 lwkt_serialize_exit(ifp->if_serializer);
1451 jme_encap(struct jme_softc *sc, struct mbuf **m_head)
1453 struct jme_txdesc *txd;
1454 struct jme_desc *desc;
1456 bus_dma_segment_t txsegs[JME_MAXTXSEGS];
1458 int error, i, prod, symbol_desc;
1459 uint32_t cflags, flag64;
1461 M_ASSERTPKTHDR((*m_head));
1463 prod = sc->jme_cdata.jme_tx_prod;
1464 txd = &sc->jme_cdata.jme_txdesc[prod];
1466 if (sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT)
1471 maxsegs = (sc->jme_tx_desc_cnt - sc->jme_cdata.jme_tx_cnt) -
1472 (JME_TXD_RSVD + symbol_desc);
1473 if (maxsegs > JME_MAXTXSEGS)
1474 maxsegs = JME_MAXTXSEGS;
1475 KASSERT(maxsegs >= (sc->jme_txd_spare - symbol_desc),
1476 ("not enough segments %d\n", maxsegs));
1478 error = bus_dmamap_load_mbuf_defrag(sc->jme_cdata.jme_tx_tag,
1479 txd->tx_dmamap, m_head,
1480 txsegs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
1484 bus_dmamap_sync(sc->jme_cdata.jme_tx_tag, txd->tx_dmamap,
1485 BUS_DMASYNC_PREWRITE);
1490 /* Configure checksum offload. */
1491 if (m->m_pkthdr.csum_flags & CSUM_IP)
1492 cflags |= JME_TD_IPCSUM;
1493 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1494 cflags |= JME_TD_TCPCSUM;
1495 if (m->m_pkthdr.csum_flags & CSUM_UDP)
1496 cflags |= JME_TD_UDPCSUM;
1498 /* Configure VLAN. */
1499 if (m->m_flags & M_VLANTAG) {
1500 cflags |= (m->m_pkthdr.ether_vlantag & JME_TD_VLAN_MASK);
1501 cflags |= JME_TD_VLAN_TAG;
1504 desc = &sc->jme_cdata.jme_tx_ring[prod];
1505 desc->flags = htole32(cflags);
1506 desc->addr_hi = htole32(m->m_pkthdr.len);
1507 if (sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT) {
1509 * Use 64bits TX desc chain format.
1511 * The first TX desc of the chain, which is setup here,
1512 * is just a symbol TX desc carrying no payload.
1514 flag64 = JME_TD_64BIT;
1518 /* No effective TX desc is consumed */
1522 * Use 32bits TX desc chain format.
1524 * The first TX desc of the chain, which is setup here,
1525 * is an effective TX desc carrying the first segment of
1529 desc->buflen = htole32(txsegs[0].ds_len);
1530 desc->addr_lo = htole32(JME_ADDR_LO(txsegs[0].ds_addr));
1532 /* One effective TX desc is consumed */
1535 sc->jme_cdata.jme_tx_cnt++;
1536 KKASSERT(sc->jme_cdata.jme_tx_cnt - i <
1537 sc->jme_tx_desc_cnt - JME_TXD_RSVD);
1538 JME_DESC_INC(prod, sc->jme_tx_desc_cnt);
1540 txd->tx_ndesc = 1 - i;
1541 for (; i < nsegs; i++) {
1542 desc = &sc->jme_cdata.jme_tx_ring[prod];
1543 desc->flags = htole32(JME_TD_OWN | flag64);
1544 desc->buflen = htole32(txsegs[i].ds_len);
1545 desc->addr_hi = htole32(JME_ADDR_HI(txsegs[i].ds_addr));
1546 desc->addr_lo = htole32(JME_ADDR_LO(txsegs[i].ds_addr));
1548 sc->jme_cdata.jme_tx_cnt++;
1549 KKASSERT(sc->jme_cdata.jme_tx_cnt <=
1550 sc->jme_tx_desc_cnt - JME_TXD_RSVD);
1551 JME_DESC_INC(prod, sc->jme_tx_desc_cnt);
1554 /* Update producer index. */
1555 sc->jme_cdata.jme_tx_prod = prod;
1557 * Finally request interrupt and give the first descriptor
1558 * owenership to hardware.
1560 desc = txd->tx_desc;
1561 desc->flags |= htole32(JME_TD_OWN | JME_TD_INTR);
1564 txd->tx_ndesc += nsegs;
1574 jme_start(struct ifnet *ifp)
1576 struct jme_softc *sc = ifp->if_softc;
1577 struct mbuf *m_head;
1580 ASSERT_SERIALIZED(ifp->if_serializer);
1582 if ((sc->jme_flags & JME_FLAG_LINK) == 0) {
1583 ifq_purge(&ifp->if_snd);
1587 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1590 if (sc->jme_cdata.jme_tx_cnt >= JME_TX_DESC_HIWAT(sc))
1593 while (!ifq_is_empty(&ifp->if_snd)) {
1595 * Check number of available TX descs, always
1596 * leave JME_TXD_RSVD free TX descs.
1598 if (sc->jme_cdata.jme_tx_cnt + sc->jme_txd_spare >
1599 sc->jme_tx_desc_cnt - JME_TXD_RSVD) {
1600 ifp->if_flags |= IFF_OACTIVE;
1604 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1609 * Pack the data into the transmit ring. If we
1610 * don't have room, set the OACTIVE flag and wait
1611 * for the NIC to drain the ring.
1613 if (jme_encap(sc, &m_head)) {
1614 KKASSERT(m_head == NULL);
1616 ifp->if_flags |= IFF_OACTIVE;
1622 * If there's a BPF listener, bounce a copy of this frame
1625 ETHER_BPF_MTAP(ifp, m_head);
1630 * Reading TXCSR takes very long time under heavy load
1631 * so cache TXCSR value and writes the ORed value with
1632 * the kick command to the TXCSR. This saves one register
1635 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr | TXCSR_TX_ENB |
1636 TXCSR_TXQ_N_START(TXCSR_TXQ0));
1637 /* Set a timeout in case the chip goes out to lunch. */
1638 ifp->if_timer = JME_TX_TIMEOUT;
1643 jme_watchdog(struct ifnet *ifp)
1645 struct jme_softc *sc = ifp->if_softc;
1647 ASSERT_SERIALIZED(ifp->if_serializer);
1649 if ((sc->jme_flags & JME_FLAG_LINK) == 0) {
1650 if_printf(ifp, "watchdog timeout (missed link)\n");
1657 if (sc->jme_cdata.jme_tx_cnt == 0) {
1658 if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
1660 if (!ifq_is_empty(&ifp->if_snd))
1665 if_printf(ifp, "watchdog timeout\n");
1668 if (!ifq_is_empty(&ifp->if_snd))
1673 jme_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1675 struct jme_softc *sc = ifp->if_softc;
1676 struct mii_data *mii = device_get_softc(sc->jme_miibus);
1677 struct ifreq *ifr = (struct ifreq *)data;
1678 int error = 0, mask;
1680 ASSERT_SERIALIZED(ifp->if_serializer);
1684 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > JME_JUMBO_MTU ||
1685 (!(sc->jme_caps & JME_CAP_JUMBO) &&
1686 ifr->ifr_mtu > JME_MAX_MTU)) {
1691 if (ifp->if_mtu != ifr->ifr_mtu) {
1693 * No special configuration is required when interface
1694 * MTU is changed but availability of Tx checksum
1695 * offload should be chcked against new MTU size as
1696 * FIFO size is just 2K.
1698 if (ifr->ifr_mtu >= JME_TX_FIFO_SIZE) {
1699 ifp->if_capenable &= ~IFCAP_TXCSUM;
1700 ifp->if_hwassist &= ~JME_CSUM_FEATURES;
1702 ifp->if_mtu = ifr->ifr_mtu;
1703 if (ifp->if_flags & IFF_RUNNING)
1709 if (ifp->if_flags & IFF_UP) {
1710 if (ifp->if_flags & IFF_RUNNING) {
1711 if ((ifp->if_flags ^ sc->jme_if_flags) &
1712 (IFF_PROMISC | IFF_ALLMULTI))
1718 if (ifp->if_flags & IFF_RUNNING)
1721 sc->jme_if_flags = ifp->if_flags;
1726 if (ifp->if_flags & IFF_RUNNING)
1732 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1736 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1738 if ((mask & IFCAP_TXCSUM) && ifp->if_mtu < JME_TX_FIFO_SIZE) {
1739 ifp->if_capenable ^= IFCAP_TXCSUM;
1740 if (IFCAP_TXCSUM & ifp->if_capenable)
1741 ifp->if_hwassist |= JME_CSUM_FEATURES;
1743 ifp->if_hwassist &= ~JME_CSUM_FEATURES;
1745 if (mask & IFCAP_RXCSUM) {
1748 ifp->if_capenable ^= IFCAP_RXCSUM;
1749 reg = CSR_READ_4(sc, JME_RXMAC);
1750 reg &= ~RXMAC_CSUM_ENB;
1751 if (ifp->if_capenable & IFCAP_RXCSUM)
1752 reg |= RXMAC_CSUM_ENB;
1753 CSR_WRITE_4(sc, JME_RXMAC, reg);
1756 if (mask & IFCAP_VLAN_HWTAGGING) {
1757 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1761 if (mask & IFCAP_RSS) {
1762 ifp->if_capenable ^= IFCAP_RSS;
1763 if (ifp->if_flags & IFF_RUNNING)
1769 error = ether_ioctl(ifp, cmd, data);
1776 jme_mac_config(struct jme_softc *sc)
1778 struct mii_data *mii;
1779 uint32_t ghc, rxmac, txmac, txpause, gp1;
1780 int phyconf = JMPHY_CONF_DEFFIFO, hdx = 0;
1782 mii = device_get_softc(sc->jme_miibus);
1784 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
1786 CSR_WRITE_4(sc, JME_GHC, 0);
1788 rxmac = CSR_READ_4(sc, JME_RXMAC);
1789 rxmac &= ~RXMAC_FC_ENB;
1790 txmac = CSR_READ_4(sc, JME_TXMAC);
1791 txmac &= ~(TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST);
1792 txpause = CSR_READ_4(sc, JME_TXPFC);
1793 txpause &= ~TXPFC_PAUSE_ENB;
1794 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1795 ghc |= GHC_FULL_DUPLEX;
1796 rxmac &= ~RXMAC_COLL_DET_ENB;
1797 txmac &= ~(TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE |
1798 TXMAC_BACKOFF | TXMAC_CARRIER_EXT |
1801 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1802 txpause |= TXPFC_PAUSE_ENB;
1803 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1804 rxmac |= RXMAC_FC_ENB;
1806 /* Disable retry transmit timer/retry limit. */
1807 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) &
1808 ~(TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB));
1810 rxmac |= RXMAC_COLL_DET_ENB;
1811 txmac |= TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE | TXMAC_BACKOFF;
1812 /* Enable retry transmit timer/retry limit. */
1813 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) |
1814 TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB);
1818 * Reprogram Tx/Rx MACs with resolved speed/duplex.
1820 gp1 = CSR_READ_4(sc, JME_GPREG1);
1821 gp1 &= ~GPREG1_WA_HDX;
1823 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) == 0)
1826 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1828 ghc |= GHC_SPEED_10 | sc->jme_clksrc;
1830 gp1 |= GPREG1_WA_HDX;
1834 ghc |= GHC_SPEED_100 | sc->jme_clksrc;
1836 gp1 |= GPREG1_WA_HDX;
1839 * Use extended FIFO depth to workaround CRC errors
1840 * emitted by chips before JMC250B
1842 phyconf = JMPHY_CONF_EXTFIFO;
1846 if (sc->jme_caps & JME_CAP_FASTETH)
1849 ghc |= GHC_SPEED_1000 | sc->jme_clksrc_1000;
1851 txmac |= TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST;
1857 CSR_WRITE_4(sc, JME_GHC, ghc);
1858 CSR_WRITE_4(sc, JME_RXMAC, rxmac);
1859 CSR_WRITE_4(sc, JME_TXMAC, txmac);
1860 CSR_WRITE_4(sc, JME_TXPFC, txpause);
1862 if (sc->jme_workaround & JME_WA_EXTFIFO) {
1863 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1864 JMPHY_CONF, phyconf);
1866 if (sc->jme_workaround & JME_WA_HDX)
1867 CSR_WRITE_4(sc, JME_GPREG1, gp1);
1873 struct jme_softc *sc = xsc;
1874 struct ifnet *ifp = &sc->arpcom.ac_if;
1878 ASSERT_SERIALIZED(ifp->if_serializer);
1880 status = CSR_READ_4(sc, JME_INTR_REQ_STATUS);
1881 if (status == 0 || status == 0xFFFFFFFF)
1884 /* Disable interrupts. */
1885 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
1887 status = CSR_READ_4(sc, JME_INTR_STATUS);
1888 if ((status & JME_INTRS) == 0 || status == 0xFFFFFFFF)
1891 /* Reset PCC counter/timer and Ack interrupts. */
1892 status &= ~(INTR_TXQ_COMP | INTR_RXQ_COMP);
1894 if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO))
1895 status |= INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP;
1897 for (r = 0; r < sc->jme_rx_ring_inuse; ++r) {
1898 if (status & jme_rx_status[r].jme_coal) {
1899 status |= jme_rx_status[r].jme_coal |
1900 jme_rx_status[r].jme_comp;
1904 CSR_WRITE_4(sc, JME_INTR_STATUS, status);
1906 if (ifp->if_flags & IFF_RUNNING) {
1907 if (status & (INTR_RXQ_COAL | INTR_RXQ_COAL_TO))
1908 jme_rx_intr(sc, status);
1910 if (status & INTR_RXQ_DESC_EMPTY) {
1912 * Notify hardware availability of new Rx buffers.
1913 * Reading RXCSR takes very long time under heavy
1914 * load so cache RXCSR value and writes the ORed
1915 * value with the kick command to the RXCSR. This
1916 * saves one register access cycle.
1918 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr |
1919 RXCSR_RX_ENB | RXCSR_RXQ_START);
1922 if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO)) {
1924 if (!ifq_is_empty(&ifp->if_snd))
1929 /* Reenable interrupts. */
1930 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
1934 jme_txeof(struct jme_softc *sc)
1936 struct ifnet *ifp = &sc->arpcom.ac_if;
1937 struct jme_txdesc *txd;
1941 cons = sc->jme_cdata.jme_tx_cons;
1942 if (cons == sc->jme_cdata.jme_tx_prod)
1946 * Go through our Tx list and free mbufs for those
1947 * frames which have been transmitted.
1949 while (cons != sc->jme_cdata.jme_tx_prod) {
1950 txd = &sc->jme_cdata.jme_txdesc[cons];
1951 KASSERT(txd->tx_m != NULL,
1952 ("%s: freeing NULL mbuf!\n", __func__));
1954 status = le32toh(txd->tx_desc->flags);
1955 if ((status & JME_TD_OWN) == JME_TD_OWN)
1958 if (status & (JME_TD_TMOUT | JME_TD_RETRY_EXP)) {
1962 if (status & JME_TD_COLLISION) {
1963 ifp->if_collisions +=
1964 le32toh(txd->tx_desc->buflen) &
1965 JME_TD_BUF_LEN_MASK;
1970 * Only the first descriptor of multi-descriptor
1971 * transmission is updated so driver have to skip entire
1972 * chained buffers for the transmiited frame. In other
1973 * words, JME_TD_OWN bit is valid only at the first
1974 * descriptor of a multi-descriptor transmission.
1976 for (nsegs = 0; nsegs < txd->tx_ndesc; nsegs++) {
1977 sc->jme_cdata.jme_tx_ring[cons].flags = 0;
1978 JME_DESC_INC(cons, sc->jme_tx_desc_cnt);
1981 /* Reclaim transferred mbufs. */
1982 bus_dmamap_unload(sc->jme_cdata.jme_tx_tag, txd->tx_dmamap);
1985 sc->jme_cdata.jme_tx_cnt -= txd->tx_ndesc;
1986 KASSERT(sc->jme_cdata.jme_tx_cnt >= 0,
1987 ("%s: Active Tx desc counter was garbled\n", __func__));
1990 sc->jme_cdata.jme_tx_cons = cons;
1992 if (sc->jme_cdata.jme_tx_cnt == 0)
1995 if (sc->jme_cdata.jme_tx_cnt + sc->jme_txd_spare <=
1996 sc->jme_tx_desc_cnt - JME_TXD_RSVD)
1997 ifp->if_flags &= ~IFF_OACTIVE;
2000 static __inline void
2001 jme_discard_rxbufs(struct jme_softc *sc, int ring, int cons, int count)
2003 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
2006 for (i = 0; i < count; ++i) {
2007 struct jme_desc *desc = &rdata->jme_rx_ring[cons];
2009 desc->flags = htole32(JME_RD_OWN | JME_RD_INTR | JME_RD_64BIT);
2010 desc->buflen = htole32(MCLBYTES);
2011 JME_DESC_INC(cons, sc->jme_rx_desc_cnt);
2015 static __inline struct pktinfo *
2016 jme_pktinfo(struct pktinfo *pi, uint32_t flags)
2018 if (flags & JME_RD_IPV4)
2019 pi->pi_netisr = NETISR_IP;
2020 else if (flags & JME_RD_IPV6)
2021 pi->pi_netisr = NETISR_IPV6;
2026 pi->pi_l3proto = IPPROTO_UNKNOWN;
2028 if (flags & JME_RD_MORE_FRAG)
2029 pi->pi_flags |= PKTINFO_FLAG_FRAG;
2030 else if (flags & JME_RD_TCP)
2031 pi->pi_l3proto = IPPROTO_TCP;
2032 else if (flags & JME_RD_UDP)
2033 pi->pi_l3proto = IPPROTO_UDP;
2039 /* Receive a frame. */
2041 jme_rxpkt(struct jme_softc *sc, int ring, struct mbuf_chain *chain)
2043 struct ifnet *ifp = &sc->arpcom.ac_if;
2044 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
2045 struct jme_desc *desc;
2046 struct jme_rxdesc *rxd;
2047 struct mbuf *mp, *m;
2048 uint32_t flags, status, hash, hashinfo;
2049 int cons, count, nsegs;
2051 cons = rdata->jme_rx_cons;
2052 desc = &rdata->jme_rx_ring[cons];
2053 flags = le32toh(desc->flags);
2054 status = le32toh(desc->buflen);
2055 hash = le32toh(desc->addr_hi);
2056 hashinfo = le32toh(desc->addr_lo);
2057 nsegs = JME_RX_NSEGS(status);
2059 JME_RSS_DPRINTF(sc, 15, "ring%d, flags 0x%08x, "
2060 "hash 0x%08x, hash info 0x%08x\n",
2061 ring, flags, hash, hashinfo);
2063 if (status & JME_RX_ERR_STAT) {
2065 jme_discard_rxbufs(sc, ring, cons, nsegs);
2066 #ifdef JME_SHOW_ERRORS
2067 device_printf(sc->jme_dev, "%s : receive error = 0x%b\n",
2068 __func__, JME_RX_ERR(status), JME_RX_ERR_BITS);
2070 rdata->jme_rx_cons += nsegs;
2071 rdata->jme_rx_cons %= sc->jme_rx_desc_cnt;
2075 rdata->jme_rxlen = JME_RX_BYTES(status) - JME_RX_PAD_BYTES;
2076 for (count = 0; count < nsegs; count++,
2077 JME_DESC_INC(cons, sc->jme_rx_desc_cnt)) {
2078 rxd = &rdata->jme_rxdesc[cons];
2081 /* Add a new receive buffer to the ring. */
2082 if (jme_newbuf(sc, ring, rxd, 0) != 0) {
2085 jme_discard_rxbufs(sc, ring, cons, nsegs - count);
2086 if (rdata->jme_rxhead != NULL) {
2087 m_freem(rdata->jme_rxhead);
2088 JME_RXCHAIN_RESET(sc, ring);
2094 * Assume we've received a full sized frame.
2095 * Actual size is fixed when we encounter the end of
2096 * multi-segmented frame.
2098 mp->m_len = MCLBYTES;
2100 /* Chain received mbufs. */
2101 if (rdata->jme_rxhead == NULL) {
2102 rdata->jme_rxhead = mp;
2103 rdata->jme_rxtail = mp;
2106 * Receive processor can receive a maximum frame
2107 * size of 65535 bytes.
2109 rdata->jme_rxtail->m_next = mp;
2110 rdata->jme_rxtail = mp;
2113 if (count == nsegs - 1) {
2114 struct pktinfo pi0, *pi;
2116 /* Last desc. for this frame. */
2117 m = rdata->jme_rxhead;
2118 m->m_pkthdr.len = rdata->jme_rxlen;
2120 /* Set first mbuf size. */
2121 m->m_len = MCLBYTES - JME_RX_PAD_BYTES;
2122 /* Set last mbuf size. */
2123 mp->m_len = rdata->jme_rxlen -
2124 ((MCLBYTES - JME_RX_PAD_BYTES) +
2125 (MCLBYTES * (nsegs - 2)));
2127 m->m_len = rdata->jme_rxlen;
2129 m->m_pkthdr.rcvif = ifp;
2132 * Account for 10bytes auto padding which is used
2133 * to align IP header on 32bit boundary. Also note,
2134 * CRC bytes is automatically removed by the
2137 m->m_data += JME_RX_PAD_BYTES;
2139 /* Set checksum information. */
2140 if ((ifp->if_capenable & IFCAP_RXCSUM) &&
2141 (flags & JME_RD_IPV4)) {
2142 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2143 if (flags & JME_RD_IPCSUM)
2144 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2145 if ((flags & JME_RD_MORE_FRAG) == 0 &&
2146 ((flags & (JME_RD_TCP | JME_RD_TCPCSUM)) ==
2147 (JME_RD_TCP | JME_RD_TCPCSUM) ||
2148 (flags & (JME_RD_UDP | JME_RD_UDPCSUM)) ==
2149 (JME_RD_UDP | JME_RD_UDPCSUM))) {
2150 m->m_pkthdr.csum_flags |=
2151 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2152 m->m_pkthdr.csum_data = 0xffff;
2156 /* Check for VLAN tagged packets. */
2157 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) &&
2158 (flags & JME_RD_VLAN_TAG)) {
2159 m->m_pkthdr.ether_vlantag =
2160 flags & JME_RD_VLAN_MASK;
2161 m->m_flags |= M_VLANTAG;
2166 if (ifp->if_capenable & IFCAP_RSS)
2167 pi = jme_pktinfo(&pi0, flags);
2172 (hashinfo & JME_RD_HASH_FN_MASK) != 0) {
2173 m->m_flags |= M_HASH;
2174 m->m_pkthdr.hash = toeplitz_hash(hash);
2177 #ifdef JME_RSS_DEBUG
2179 JME_RSS_DPRINTF(sc, 10,
2180 "isr %d flags %08x, l3 %d %s\n",
2181 pi->pi_netisr, pi->pi_flags,
2183 (m->m_flags & M_HASH) ? "hash" : "");
2188 ether_input_chain(ifp, m, pi, chain);
2190 /* Reset mbuf chains. */
2191 JME_RXCHAIN_RESET(sc, ring);
2192 #ifdef JME_RSS_DEBUG
2193 sc->jme_rx_ring_pkt[ring]++;
2198 rdata->jme_rx_cons += nsegs;
2199 rdata->jme_rx_cons %= sc->jme_rx_desc_cnt;
2203 jme_rxeof_chain(struct jme_softc *sc, int ring, struct mbuf_chain *chain,
2206 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
2207 struct jme_desc *desc;
2208 int nsegs, prog, pktlen;
2212 #ifdef DEVICE_POLLING
2213 if (count >= 0 && count-- == 0)
2216 desc = &rdata->jme_rx_ring[rdata->jme_rx_cons];
2217 if ((le32toh(desc->flags) & JME_RD_OWN) == JME_RD_OWN)
2219 if ((le32toh(desc->buflen) & JME_RD_VALID) == 0)
2223 * Check number of segments against received bytes.
2224 * Non-matching value would indicate that hardware
2225 * is still trying to update Rx descriptors. I'm not
2226 * sure whether this check is needed.
2228 nsegs = JME_RX_NSEGS(le32toh(desc->buflen));
2229 pktlen = JME_RX_BYTES(le32toh(desc->buflen));
2230 if (nsegs != howmany(pktlen, MCLBYTES)) {
2231 if_printf(&sc->arpcom.ac_if, "RX fragment count(%d) "
2232 "and packet size(%d) mismach\n",
2237 /* Received a frame. */
2238 jme_rxpkt(sc, ring, chain);
2245 jme_rxeof(struct jme_softc *sc, int ring)
2247 struct mbuf_chain chain[MAXCPU];
2249 ether_input_chain_init(chain);
2250 if (jme_rxeof_chain(sc, ring, chain, -1))
2251 ether_input_dispatch(chain);
2257 struct jme_softc *sc = xsc;
2258 struct ifnet *ifp = &sc->arpcom.ac_if;
2259 struct mii_data *mii = device_get_softc(sc->jme_miibus);
2261 lwkt_serialize_enter(ifp->if_serializer);
2264 callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
2266 lwkt_serialize_exit(ifp->if_serializer);
2270 jme_reset(struct jme_softc *sc)
2273 /* Stop receiver, transmitter. */
2277 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2279 CSR_WRITE_4(sc, JME_GHC, 0);
2285 struct jme_softc *sc = xsc;
2286 struct ifnet *ifp = &sc->arpcom.ac_if;
2287 struct mii_data *mii;
2288 uint8_t eaddr[ETHER_ADDR_LEN];
2293 ASSERT_SERIALIZED(ifp->if_serializer);
2296 * Cancel any pending I/O.
2301 * Reset the chip to a known state.
2306 howmany(ifp->if_mtu + sizeof(struct ether_vlan_header), MCLBYTES);
2307 KKASSERT(sc->jme_txd_spare >= 1);
2310 * If we use 64bit address mode for transmitting, each Tx request
2311 * needs one more symbol descriptor.
2313 if (sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT)
2314 sc->jme_txd_spare += 1;
2316 if (ifp->if_capenable & IFCAP_RSS)
2319 jme_disable_rss(sc);
2321 /* Init RX descriptors */
2322 for (r = 0; r < sc->jme_rx_ring_inuse; ++r) {
2323 error = jme_init_rx_ring(sc, r);
2325 if_printf(ifp, "initialization failed: "
2326 "no memory for %dth RX ring.\n", r);
2332 /* Init TX descriptors */
2333 jme_init_tx_ring(sc);
2335 /* Initialize shadow status block. */
2338 /* Reprogram the station address. */
2339 bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
2340 CSR_WRITE_4(sc, JME_PAR0,
2341 eaddr[3] << 24 | eaddr[2] << 16 | eaddr[1] << 8 | eaddr[0]);
2342 CSR_WRITE_4(sc, JME_PAR1, eaddr[5] << 8 | eaddr[4]);
2345 * Configure Tx queue.
2346 * Tx priority queue weight value : 0
2347 * Tx FIFO threshold for processing next packet : 16QW
2348 * Maximum Tx DMA length : 512
2349 * Allow Tx DMA burst.
2351 sc->jme_txcsr = TXCSR_TXQ_N_SEL(TXCSR_TXQ0);
2352 sc->jme_txcsr |= TXCSR_TXQ_WEIGHT(TXCSR_TXQ_WEIGHT_MIN);
2353 sc->jme_txcsr |= TXCSR_FIFO_THRESH_16QW;
2354 sc->jme_txcsr |= sc->jme_tx_dma_size;
2355 sc->jme_txcsr |= TXCSR_DMA_BURST;
2356 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr);
2358 /* Set Tx descriptor counter. */
2359 CSR_WRITE_4(sc, JME_TXQDC, sc->jme_tx_desc_cnt);
2361 /* Set Tx ring address to the hardware. */
2362 paddr = sc->jme_cdata.jme_tx_ring_paddr;
2363 CSR_WRITE_4(sc, JME_TXDBA_HI, JME_ADDR_HI(paddr));
2364 CSR_WRITE_4(sc, JME_TXDBA_LO, JME_ADDR_LO(paddr));
2366 /* Configure TxMAC parameters. */
2367 reg = TXMAC_IFG1_DEFAULT | TXMAC_IFG2_DEFAULT | TXMAC_IFG_ENB;
2368 reg |= TXMAC_THRESH_1_PKT;
2369 reg |= TXMAC_CRC_ENB | TXMAC_PAD_ENB;
2370 CSR_WRITE_4(sc, JME_TXMAC, reg);
2373 * Configure Rx queue.
2374 * FIFO full threshold for transmitting Tx pause packet : 128T
2375 * FIFO threshold for processing next packet : 128QW
2377 * Max Rx DMA length : 128
2378 * Rx descriptor retry : 32
2379 * Rx descriptor retry time gap : 256ns
2380 * Don't receive runt/bad frame.
2382 sc->jme_rxcsr = RXCSR_FIFO_FTHRESH_128T;
2385 * Since Rx FIFO size is 4K bytes, receiving frames larger
2386 * than 4K bytes will suffer from Rx FIFO overruns. So
2387 * decrease FIFO threshold to reduce the FIFO overruns for
2388 * frames larger than 4000 bytes.
2389 * For best performance of standard MTU sized frames use
2390 * maximum allowable FIFO threshold, 128QW.
2392 if ((ifp->if_mtu + ETHER_HDR_LEN + EVL_ENCAPLEN + ETHER_CRC_LEN) >
2394 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
2396 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_128QW;
2398 /* Improve PCI Express compatibility */
2399 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
2401 sc->jme_rxcsr |= sc->jme_rx_dma_size;
2402 sc->jme_rxcsr |= RXCSR_DESC_RT_CNT(RXCSR_DESC_RT_CNT_DEFAULT);
2403 sc->jme_rxcsr |= RXCSR_DESC_RT_GAP_256 & RXCSR_DESC_RT_GAP_MASK;
2404 /* XXX TODO DROP_BAD */
2406 for (r = 0; r < sc->jme_rx_ring_inuse; ++r) {
2407 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RXQ_N_SEL(r));
2409 /* Set Rx descriptor counter. */
2410 CSR_WRITE_4(sc, JME_RXQDC, sc->jme_rx_desc_cnt);
2412 /* Set Rx ring address to the hardware. */
2413 paddr = sc->jme_cdata.jme_rx_data[r].jme_rx_ring_paddr;
2414 CSR_WRITE_4(sc, JME_RXDBA_HI, JME_ADDR_HI(paddr));
2415 CSR_WRITE_4(sc, JME_RXDBA_LO, JME_ADDR_LO(paddr));
2418 /* Clear receive filter. */
2419 CSR_WRITE_4(sc, JME_RXMAC, 0);
2421 /* Set up the receive filter. */
2426 * Disable all WOL bits as WOL can interfere normal Rx
2427 * operation. Also clear WOL detection status bits.
2429 reg = CSR_READ_4(sc, JME_PMCS);
2430 reg &= ~PMCS_WOL_ENB_MASK;
2431 CSR_WRITE_4(sc, JME_PMCS, reg);
2434 * Pad 10bytes right before received frame. This will greatly
2435 * help Rx performance on strict-alignment architectures as
2436 * it does not need to copy the frame to align the payload.
2438 reg = CSR_READ_4(sc, JME_RXMAC);
2439 reg |= RXMAC_PAD_10BYTES;
2441 if (ifp->if_capenable & IFCAP_RXCSUM)
2442 reg |= RXMAC_CSUM_ENB;
2443 CSR_WRITE_4(sc, JME_RXMAC, reg);
2445 /* Configure general purpose reg0 */
2446 reg = CSR_READ_4(sc, JME_GPREG0);
2447 reg &= ~GPREG0_PCC_UNIT_MASK;
2448 /* Set PCC timer resolution to micro-seconds unit. */
2449 reg |= GPREG0_PCC_UNIT_US;
2451 * Disable all shadow register posting as we have to read
2452 * JME_INTR_STATUS register in jme_intr. Also it seems
2453 * that it's hard to synchronize interrupt status between
2454 * hardware and software with shadow posting due to
2455 * requirements of bus_dmamap_sync(9).
2457 reg |= GPREG0_SH_POST_DW7_DIS | GPREG0_SH_POST_DW6_DIS |
2458 GPREG0_SH_POST_DW5_DIS | GPREG0_SH_POST_DW4_DIS |
2459 GPREG0_SH_POST_DW3_DIS | GPREG0_SH_POST_DW2_DIS |
2460 GPREG0_SH_POST_DW1_DIS | GPREG0_SH_POST_DW0_DIS;
2461 /* Disable posting of DW0. */
2462 reg &= ~GPREG0_POST_DW0_ENB;
2463 /* Clear PME message. */
2464 reg &= ~GPREG0_PME_ENB;
2465 /* Set PHY address. */
2466 reg &= ~GPREG0_PHY_ADDR_MASK;
2467 reg |= sc->jme_phyaddr;
2468 CSR_WRITE_4(sc, JME_GPREG0, reg);
2470 /* Configure Tx queue 0 packet completion coalescing. */
2471 jme_set_tx_coal(sc);
2473 /* Configure Rx queue 0 packet completion coalescing. */
2474 jme_set_rx_coal(sc);
2476 /* Configure shadow status block but don't enable posting. */
2477 paddr = sc->jme_cdata.jme_ssb_block_paddr;
2478 CSR_WRITE_4(sc, JME_SHBASE_ADDR_HI, JME_ADDR_HI(paddr));
2479 CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO, JME_ADDR_LO(paddr));
2481 /* Disable Timer 1 and Timer 2. */
2482 CSR_WRITE_4(sc, JME_TIMER1, 0);
2483 CSR_WRITE_4(sc, JME_TIMER2, 0);
2485 /* Configure retry transmit period, retry limit value. */
2486 CSR_WRITE_4(sc, JME_TXTRHD,
2487 ((TXTRHD_RT_PERIOD_DEFAULT << TXTRHD_RT_PERIOD_SHIFT) &
2488 TXTRHD_RT_PERIOD_MASK) |
2489 ((TXTRHD_RT_LIMIT_DEFAULT << TXTRHD_RT_LIMIT_SHIFT) &
2490 TXTRHD_RT_LIMIT_SHIFT));
2492 #ifdef DEVICE_POLLING
2493 if (!(ifp->if_flags & IFF_POLLING))
2495 /* Initialize the interrupt mask. */
2496 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
2497 CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2500 * Enabling Tx/Rx DMA engines and Rx queue processing is
2501 * done after detection of valid link in jme_miibus_statchg.
2503 sc->jme_flags &= ~JME_FLAG_LINK;
2505 /* Set the current media. */
2506 mii = device_get_softc(sc->jme_miibus);
2509 callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
2511 ifp->if_flags |= IFF_RUNNING;
2512 ifp->if_flags &= ~IFF_OACTIVE;
2516 jme_stop(struct jme_softc *sc)
2518 struct ifnet *ifp = &sc->arpcom.ac_if;
2519 struct jme_txdesc *txd;
2520 struct jme_rxdesc *rxd;
2521 struct jme_rxdata *rdata;
2524 ASSERT_SERIALIZED(ifp->if_serializer);
2527 * Mark the interface down and cancel the watchdog timer.
2529 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2532 callout_stop(&sc->jme_tick_ch);
2533 sc->jme_flags &= ~JME_FLAG_LINK;
2536 * Disable interrupts.
2538 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
2539 CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2541 /* Disable updating shadow status block. */
2542 CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO,
2543 CSR_READ_4(sc, JME_SHBASE_ADDR_LO) & ~SHBASE_POST_ENB);
2545 /* Stop receiver, transmitter. */
2550 * Free partial finished RX segments
2552 for (r = 0; r < sc->jme_rx_ring_inuse; ++r) {
2553 rdata = &sc->jme_cdata.jme_rx_data[r];
2554 if (rdata->jme_rxhead != NULL)
2555 m_freem(rdata->jme_rxhead);
2556 JME_RXCHAIN_RESET(sc, r);
2560 * Free RX and TX mbufs still in the queues.
2562 for (r = 0; r < sc->jme_rx_ring_inuse; ++r) {
2563 rdata = &sc->jme_cdata.jme_rx_data[r];
2564 for (i = 0; i < sc->jme_rx_desc_cnt; i++) {
2565 rxd = &rdata->jme_rxdesc[i];
2566 if (rxd->rx_m != NULL) {
2567 bus_dmamap_unload(rdata->jme_rx_tag,
2574 for (i = 0; i < sc->jme_tx_desc_cnt; i++) {
2575 txd = &sc->jme_cdata.jme_txdesc[i];
2576 if (txd->tx_m != NULL) {
2577 bus_dmamap_unload(sc->jme_cdata.jme_tx_tag,
2587 jme_stop_tx(struct jme_softc *sc)
2592 reg = CSR_READ_4(sc, JME_TXCSR);
2593 if ((reg & TXCSR_TX_ENB) == 0)
2595 reg &= ~TXCSR_TX_ENB;
2596 CSR_WRITE_4(sc, JME_TXCSR, reg);
2597 for (i = JME_TIMEOUT; i > 0; i--) {
2599 if ((CSR_READ_4(sc, JME_TXCSR) & TXCSR_TX_ENB) == 0)
2603 device_printf(sc->jme_dev, "stopping transmitter timeout!\n");
2607 jme_stop_rx(struct jme_softc *sc)
2612 reg = CSR_READ_4(sc, JME_RXCSR);
2613 if ((reg & RXCSR_RX_ENB) == 0)
2615 reg &= ~RXCSR_RX_ENB;
2616 CSR_WRITE_4(sc, JME_RXCSR, reg);
2617 for (i = JME_TIMEOUT; i > 0; i--) {
2619 if ((CSR_READ_4(sc, JME_RXCSR) & RXCSR_RX_ENB) == 0)
2623 device_printf(sc->jme_dev, "stopping recevier timeout!\n");
2627 jme_init_tx_ring(struct jme_softc *sc)
2629 struct jme_chain_data *cd;
2630 struct jme_txdesc *txd;
2633 sc->jme_cdata.jme_tx_prod = 0;
2634 sc->jme_cdata.jme_tx_cons = 0;
2635 sc->jme_cdata.jme_tx_cnt = 0;
2637 cd = &sc->jme_cdata;
2638 bzero(cd->jme_tx_ring, JME_TX_RING_SIZE(sc));
2639 for (i = 0; i < sc->jme_tx_desc_cnt; i++) {
2640 txd = &sc->jme_cdata.jme_txdesc[i];
2642 txd->tx_desc = &cd->jme_tx_ring[i];
2648 jme_init_ssb(struct jme_softc *sc)
2650 struct jme_chain_data *cd;
2652 cd = &sc->jme_cdata;
2653 bzero(cd->jme_ssb_block, JME_SSB_SIZE);
2657 jme_init_rx_ring(struct jme_softc *sc, int ring)
2659 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
2660 struct jme_rxdesc *rxd;
2663 KKASSERT(rdata->jme_rxhead == NULL &&
2664 rdata->jme_rxtail == NULL &&
2665 rdata->jme_rxlen == 0);
2666 rdata->jme_rx_cons = 0;
2668 bzero(rdata->jme_rx_ring, JME_RX_RING_SIZE(sc));
2669 for (i = 0; i < sc->jme_rx_desc_cnt; i++) {
2672 rxd = &rdata->jme_rxdesc[i];
2674 rxd->rx_desc = &rdata->jme_rx_ring[i];
2675 error = jme_newbuf(sc, ring, rxd, 1);
2683 jme_newbuf(struct jme_softc *sc, int ring, struct jme_rxdesc *rxd, int init)
2685 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
2686 struct jme_desc *desc;
2688 bus_dma_segment_t segs;
2692 m = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
2696 * JMC250 has 64bit boundary alignment limitation so jme(4)
2697 * takes advantage of 10 bytes padding feature of hardware
2698 * in order not to copy entire frame to align IP header on
2701 m->m_len = m->m_pkthdr.len = MCLBYTES;
2703 error = bus_dmamap_load_mbuf_segment(rdata->jme_rx_tag,
2704 rdata->jme_rx_sparemap, m, &segs, 1, &nsegs,
2709 if_printf(&sc->arpcom.ac_if, "can't load RX mbuf\n");
2713 if (rxd->rx_m != NULL) {
2714 bus_dmamap_sync(rdata->jme_rx_tag, rxd->rx_dmamap,
2715 BUS_DMASYNC_POSTREAD);
2716 bus_dmamap_unload(rdata->jme_rx_tag, rxd->rx_dmamap);
2718 map = rxd->rx_dmamap;
2719 rxd->rx_dmamap = rdata->jme_rx_sparemap;
2720 rdata->jme_rx_sparemap = map;
2723 desc = rxd->rx_desc;
2724 desc->buflen = htole32(segs.ds_len);
2725 desc->addr_lo = htole32(JME_ADDR_LO(segs.ds_addr));
2726 desc->addr_hi = htole32(JME_ADDR_HI(segs.ds_addr));
2727 desc->flags = htole32(JME_RD_OWN | JME_RD_INTR | JME_RD_64BIT);
2733 jme_set_vlan(struct jme_softc *sc)
2735 struct ifnet *ifp = &sc->arpcom.ac_if;
2738 ASSERT_SERIALIZED(ifp->if_serializer);
2740 reg = CSR_READ_4(sc, JME_RXMAC);
2741 reg &= ~RXMAC_VLAN_ENB;
2742 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
2743 reg |= RXMAC_VLAN_ENB;
2744 CSR_WRITE_4(sc, JME_RXMAC, reg);
2748 jme_set_filter(struct jme_softc *sc)
2750 struct ifnet *ifp = &sc->arpcom.ac_if;
2751 struct ifmultiaddr *ifma;
2756 ASSERT_SERIALIZED(ifp->if_serializer);
2758 rxcfg = CSR_READ_4(sc, JME_RXMAC);
2759 rxcfg &= ~(RXMAC_BROADCAST | RXMAC_PROMISC | RXMAC_MULTICAST |
2763 * Always accept frames destined to our station address.
2764 * Always accept broadcast frames.
2766 rxcfg |= RXMAC_UNICAST | RXMAC_BROADCAST;
2768 if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
2769 if (ifp->if_flags & IFF_PROMISC)
2770 rxcfg |= RXMAC_PROMISC;
2771 if (ifp->if_flags & IFF_ALLMULTI)
2772 rxcfg |= RXMAC_ALLMULTI;
2773 CSR_WRITE_4(sc, JME_MAR0, 0xFFFFFFFF);
2774 CSR_WRITE_4(sc, JME_MAR1, 0xFFFFFFFF);
2775 CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
2780 * Set up the multicast address filter by passing all multicast
2781 * addresses through a CRC generator, and then using the low-order
2782 * 6 bits as an index into the 64 bit multicast hash table. The
2783 * high order bits select the register, while the rest of the bits
2784 * select the bit within the register.
2786 rxcfg |= RXMAC_MULTICAST;
2787 bzero(mchash, sizeof(mchash));
2789 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2790 if (ifma->ifma_addr->sa_family != AF_LINK)
2792 crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
2793 ifma->ifma_addr), ETHER_ADDR_LEN);
2795 /* Just want the 6 least significant bits. */
2798 /* Set the corresponding bit in the hash table. */
2799 mchash[crc >> 5] |= 1 << (crc & 0x1f);
2802 CSR_WRITE_4(sc, JME_MAR0, mchash[0]);
2803 CSR_WRITE_4(sc, JME_MAR1, mchash[1]);
2804 CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
2808 jme_sysctl_tx_coal_to(SYSCTL_HANDLER_ARGS)
2810 struct jme_softc *sc = arg1;
2811 struct ifnet *ifp = &sc->arpcom.ac_if;
2814 lwkt_serialize_enter(ifp->if_serializer);
2816 v = sc->jme_tx_coal_to;
2817 error = sysctl_handle_int(oidp, &v, 0, req);
2818 if (error || req->newptr == NULL)
2821 if (v < PCCTX_COAL_TO_MIN || v > PCCTX_COAL_TO_MAX) {
2826 if (v != sc->jme_tx_coal_to) {
2827 sc->jme_tx_coal_to = v;
2828 if (ifp->if_flags & IFF_RUNNING)
2829 jme_set_tx_coal(sc);
2832 lwkt_serialize_exit(ifp->if_serializer);
2837 jme_sysctl_tx_coal_pkt(SYSCTL_HANDLER_ARGS)
2839 struct jme_softc *sc = arg1;
2840 struct ifnet *ifp = &sc->arpcom.ac_if;
2843 lwkt_serialize_enter(ifp->if_serializer);
2845 v = sc->jme_tx_coal_pkt;
2846 error = sysctl_handle_int(oidp, &v, 0, req);
2847 if (error || req->newptr == NULL)
2850 if (v < PCCTX_COAL_PKT_MIN || v > PCCTX_COAL_PKT_MAX) {
2855 if (v != sc->jme_tx_coal_pkt) {
2856 sc->jme_tx_coal_pkt = v;
2857 if (ifp->if_flags & IFF_RUNNING)
2858 jme_set_tx_coal(sc);
2861 lwkt_serialize_exit(ifp->if_serializer);
2866 jme_sysctl_rx_coal_to(SYSCTL_HANDLER_ARGS)
2868 struct jme_softc *sc = arg1;
2869 struct ifnet *ifp = &sc->arpcom.ac_if;
2872 lwkt_serialize_enter(ifp->if_serializer);
2874 v = sc->jme_rx_coal_to;
2875 error = sysctl_handle_int(oidp, &v, 0, req);
2876 if (error || req->newptr == NULL)
2879 if (v < PCCRX_COAL_TO_MIN || v > PCCRX_COAL_TO_MAX) {
2884 if (v != sc->jme_rx_coal_to) {
2885 sc->jme_rx_coal_to = v;
2886 if (ifp->if_flags & IFF_RUNNING)
2887 jme_set_rx_coal(sc);
2890 lwkt_serialize_exit(ifp->if_serializer);
2895 jme_sysctl_rx_coal_pkt(SYSCTL_HANDLER_ARGS)
2897 struct jme_softc *sc = arg1;
2898 struct ifnet *ifp = &sc->arpcom.ac_if;
2901 lwkt_serialize_enter(ifp->if_serializer);
2903 v = sc->jme_rx_coal_pkt;
2904 error = sysctl_handle_int(oidp, &v, 0, req);
2905 if (error || req->newptr == NULL)
2908 if (v < PCCRX_COAL_PKT_MIN || v > PCCRX_COAL_PKT_MAX) {
2913 if (v != sc->jme_rx_coal_pkt) {
2914 sc->jme_rx_coal_pkt = v;
2915 if (ifp->if_flags & IFF_RUNNING)
2916 jme_set_rx_coal(sc);
2919 lwkt_serialize_exit(ifp->if_serializer);
2924 jme_set_tx_coal(struct jme_softc *sc)
2928 reg = (sc->jme_tx_coal_to << PCCTX_COAL_TO_SHIFT) &
2930 reg |= (sc->jme_tx_coal_pkt << PCCTX_COAL_PKT_SHIFT) &
2931 PCCTX_COAL_PKT_MASK;
2932 reg |= PCCTX_COAL_TXQ0;
2933 CSR_WRITE_4(sc, JME_PCCTX, reg);
2937 jme_set_rx_coal(struct jme_softc *sc)
2942 reg = (sc->jme_rx_coal_to << PCCRX_COAL_TO_SHIFT) &
2944 reg |= (sc->jme_rx_coal_pkt << PCCRX_COAL_PKT_SHIFT) &
2945 PCCRX_COAL_PKT_MASK;
2946 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
2947 if (r < sc->jme_rx_ring_inuse)
2948 CSR_WRITE_4(sc, JME_PCCRX(r), reg);
2950 CSR_WRITE_4(sc, JME_PCCRX(r), 0);
2954 #ifdef DEVICE_POLLING
2957 jme_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2959 struct jme_softc *sc = ifp->if_softc;
2960 struct mbuf_chain chain[MAXCPU];
2964 ASSERT_SERIALIZED(ifp->if_serializer);
2968 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
2971 case POLL_DEREGISTER:
2972 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
2975 case POLL_AND_CHECK_STATUS:
2977 status = CSR_READ_4(sc, JME_INTR_STATUS);
2979 ether_input_chain_init(chain);
2980 for (r = 0; r < sc->jme_rx_ring_inuse; ++r)
2981 prog += jme_rxeof_chain(sc, r, chain, count);
2983 ether_input_dispatch(chain);
2985 if (status & INTR_RXQ_DESC_EMPTY) {
2986 CSR_WRITE_4(sc, JME_INTR_STATUS, status);
2987 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr |
2988 RXCSR_RX_ENB | RXCSR_RXQ_START);
2992 if (!ifq_is_empty(&ifp->if_snd))
2998 #endif /* DEVICE_POLLING */
3001 jme_rxring_dma_alloc(struct jme_softc *sc, int ring)
3003 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
3007 error = bus_dmamem_coherent(sc->jme_cdata.jme_ring_tag,
3008 JME_RX_RING_ALIGN, 0,
3009 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3010 JME_RX_RING_SIZE(sc),
3011 BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
3013 device_printf(sc->jme_dev,
3014 "could not allocate %dth Rx ring.\n", ring);
3017 rdata->jme_rx_ring_tag = dmem.dmem_tag;
3018 rdata->jme_rx_ring_map = dmem.dmem_map;
3019 rdata->jme_rx_ring = dmem.dmem_addr;
3020 rdata->jme_rx_ring_paddr = dmem.dmem_busaddr;
3026 jme_rxbuf_dma_alloc(struct jme_softc *sc, int ring)
3028 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
3031 /* Create tag for Rx buffers. */
3032 error = bus_dma_tag_create(sc->jme_cdata.jme_buffer_tag,/* parent */
3033 JME_RX_BUF_ALIGN, 0, /* algnmnt, boundary */
3034 BUS_SPACE_MAXADDR, /* lowaddr */
3035 BUS_SPACE_MAXADDR, /* highaddr */
3036 NULL, NULL, /* filter, filterarg */
3037 MCLBYTES, /* maxsize */
3039 MCLBYTES, /* maxsegsize */
3040 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ALIGNED,/* flags */
3041 &rdata->jme_rx_tag);
3043 device_printf(sc->jme_dev,
3044 "could not create %dth Rx DMA tag.\n", ring);
3048 /* Create DMA maps for Rx buffers. */
3049 error = bus_dmamap_create(rdata->jme_rx_tag, BUS_DMA_WAITOK,
3050 &rdata->jme_rx_sparemap);
3052 device_printf(sc->jme_dev,
3053 "could not create %dth spare Rx dmamap.\n", ring);
3054 bus_dma_tag_destroy(rdata->jme_rx_tag);
3055 rdata->jme_rx_tag = NULL;
3058 for (i = 0; i < sc->jme_rx_desc_cnt; i++) {
3059 struct jme_rxdesc *rxd = &rdata->jme_rxdesc[i];
3061 error = bus_dmamap_create(rdata->jme_rx_tag, BUS_DMA_WAITOK,
3066 device_printf(sc->jme_dev,
3067 "could not create %dth Rx dmamap "
3068 "for %dth RX ring.\n", i, ring);
3070 for (j = 0; j < i; ++j) {
3071 rxd = &rdata->jme_rxdesc[j];
3072 bus_dmamap_destroy(rdata->jme_rx_tag,
3075 bus_dmamap_destroy(rdata->jme_rx_tag,
3076 rdata->jme_rx_sparemap);
3077 bus_dma_tag_destroy(rdata->jme_rx_tag);
3078 rdata->jme_rx_tag = NULL;
3086 jme_rx_intr(struct jme_softc *sc, uint32_t status)
3088 struct mbuf_chain chain[MAXCPU];
3091 ether_input_chain_init(chain);
3092 for (r = 0; r < sc->jme_rx_ring_inuse; ++r) {
3093 if (status & jme_rx_status[r].jme_coal)
3094 prog += jme_rxeof_chain(sc, r, chain, -1);
3097 ether_input_dispatch(chain);
3101 jme_enable_rss(struct jme_softc *sc)
3104 uint8_t key[RSSKEY_NREGS * RSSKEY_REGSIZE];
3107 sc->jme_rx_ring_inuse = sc->jme_rx_ring_cnt;
3109 KASSERT(sc->jme_rx_ring_inuse == JME_NRXRING_2 ||
3110 sc->jme_rx_ring_inuse == JME_NRXRING_4,
3111 ("%s: invalid # of RX rings (%d)\n",
3112 sc->arpcom.ac_if.if_xname, sc->jme_rx_ring_inuse));
3114 rssc = RSSC_HASH_64_ENTRY;
3115 rssc |= RSSC_HASH_IPV4 | RSSC_HASH_IPV4_TCP;
3116 rssc |= sc->jme_rx_ring_inuse >> 1;
3117 JME_RSS_DPRINTF(sc, 1, "rssc 0x%08x\n", rssc);
3118 CSR_WRITE_4(sc, JME_RSSC, rssc);
3120 toeplitz_get_key(key, sizeof(key));
3121 for (i = 0; i < RSSKEY_NREGS; ++i) {
3124 keyreg = RSSKEY_REGVAL(key, i);
3125 JME_RSS_DPRINTF(sc, 5, "keyreg%d 0x%08x\n", i, keyreg);
3127 CSR_WRITE_4(sc, RSSKEY_REG(i), keyreg);
3131 * Create redirect table in following fashion:
3132 * (hash & ring_cnt_mask) == rdr_table[(hash & rdr_table_mask)]
3135 for (i = 0; i < RSSTBL_REGSIZE; ++i) {
3138 q = i % sc->jme_rx_ring_inuse;
3139 ind |= q << (i * 8);
3141 JME_RSS_DPRINTF(sc, 1, "ind 0x%08x\n", ind);
3143 for (i = 0; i < RSSTBL_NREGS; ++i)
3144 CSR_WRITE_4(sc, RSSTBL_REG(i), ind);
3148 jme_disable_rss(struct jme_softc *sc)
3150 sc->jme_rx_ring_inuse = JME_NRXRING_1;
3151 CSR_WRITE_4(sc, JME_RSSC, RSSC_DIS_RSS);