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 $
30 #include "opt_ifpoll.h"
33 #include <sys/param.h>
34 #include <sys/endian.h>
35 #include <sys/kernel.h>
37 #include <sys/interrupt.h>
38 #include <sys/malloc.h>
41 #include <sys/serialize.h>
42 #include <sys/serialize2.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/sysctl.h>
47 #include <net/ethernet.h>
50 #include <net/if_arp.h>
51 #include <net/if_dl.h>
52 #include <net/if_media.h>
53 #include <net/if_poll.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/ip.h>
61 #include <netinet/tcp.h>
63 #include <dev/netif/mii_layer/miivar.h>
64 #include <dev/netif/mii_layer/jmphyreg.h>
66 #include <bus/pci/pcireg.h>
67 #include <bus/pci/pcivar.h>
68 #include <bus/pci/pcidevs.h>
70 #include <dev/netif/jme/if_jmereg.h>
71 #include <dev/netif/jme/if_jmevar.h>
73 #include "miibus_if.h"
75 #define JME_TICK_CPUID 0 /* DO NOT CHANGE THIS */
77 #define JME_TX_SERIALIZE 1
78 #define JME_RX_SERIALIZE 2
80 #define JME_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
83 #define JME_RSS_DPRINTF(sc, lvl, fmt, ...) \
85 if ((sc)->jme_rss_debug >= (lvl)) \
86 if_printf(&(sc)->arpcom.ac_if, fmt, __VA_ARGS__); \
88 #else /* !JME_RSS_DEBUG */
89 #define JME_RSS_DPRINTF(sc, lvl, fmt, ...) ((void)0)
90 #endif /* JME_RSS_DEBUG */
92 static int jme_probe(device_t);
93 static int jme_attach(device_t);
94 static int jme_detach(device_t);
95 static int jme_shutdown(device_t);
96 static int jme_suspend(device_t);
97 static int jme_resume(device_t);
99 static int jme_miibus_readreg(device_t, int, int);
100 static int jme_miibus_writereg(device_t, int, int, int);
101 static void jme_miibus_statchg(device_t);
103 static void jme_init(void *);
104 static int jme_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
105 static void jme_start(struct ifnet *, struct ifaltq_subque *);
106 static void jme_watchdog(struct ifnet *);
107 static void jme_mediastatus(struct ifnet *, struct ifmediareq *);
108 static int jme_mediachange(struct ifnet *);
110 static void jme_npoll(struct ifnet *, struct ifpoll_info *);
111 static void jme_npoll_status(struct ifnet *);
112 static void jme_npoll_rx(struct ifnet *, void *, int);
113 static void jme_npoll_tx(struct ifnet *, void *, int);
115 static void jme_serialize(struct ifnet *, enum ifnet_serialize);
116 static void jme_deserialize(struct ifnet *, enum ifnet_serialize);
117 static int jme_tryserialize(struct ifnet *, enum ifnet_serialize);
119 static void jme_serialize_assert(struct ifnet *, enum ifnet_serialize,
123 static void jme_intr(void *);
124 static void jme_msix_tx(void *);
125 static void jme_msix_rx(void *);
126 static void jme_msix_status(void *);
127 static void jme_txeof(struct jme_txdata *);
128 static void jme_rxeof(struct jme_rxdata *, int);
129 static void jme_rx_intr(struct jme_softc *, uint32_t);
130 static void jme_enable_intr(struct jme_softc *);
131 static void jme_disable_intr(struct jme_softc *);
132 static void jme_rx_restart(struct jme_softc *, uint32_t);
134 static int jme_msix_setup(device_t);
135 static void jme_msix_teardown(device_t, int);
136 static int jme_intr_setup(device_t);
137 static void jme_intr_teardown(device_t);
138 static void jme_msix_try_alloc(device_t);
139 static void jme_msix_free(device_t);
140 static int jme_intr_alloc(device_t);
141 static void jme_intr_free(device_t);
142 static int jme_dma_alloc(struct jme_softc *);
143 static void jme_dma_free(struct jme_softc *);
144 static int jme_init_rx_ring(struct jme_rxdata *);
145 static void jme_init_tx_ring(struct jme_txdata *);
146 static void jme_init_ssb(struct jme_softc *);
147 static int jme_newbuf(struct jme_rxdata *, struct jme_rxdesc *, int);
148 static int jme_encap(struct jme_txdata *, struct mbuf **, int *);
149 static void jme_rxpkt(struct jme_rxdata *);
150 static int jme_rxring_dma_alloc(struct jme_rxdata *);
151 static int jme_rxbuf_dma_alloc(struct jme_rxdata *);
152 static int jme_rxbuf_dma_filter(void *, bus_addr_t);
154 static void jme_tick(void *);
155 static void jme_stop(struct jme_softc *);
156 static void jme_reset(struct jme_softc *);
157 static void jme_set_msinum(struct jme_softc *);
158 static void jme_set_vlan(struct jme_softc *);
159 static void jme_set_filter(struct jme_softc *);
160 static void jme_stop_tx(struct jme_softc *);
161 static void jme_stop_rx(struct jme_softc *);
162 static void jme_mac_config(struct jme_softc *);
163 static void jme_reg_macaddr(struct jme_softc *, uint8_t[]);
164 static int jme_eeprom_macaddr(struct jme_softc *, uint8_t[]);
165 static int jme_eeprom_read_byte(struct jme_softc *, uint8_t, uint8_t *);
167 static void jme_setwol(struct jme_softc *);
168 static void jme_setlinkspeed(struct jme_softc *);
170 static void jme_set_tx_coal(struct jme_softc *);
171 static void jme_set_rx_coal(struct jme_softc *);
172 static void jme_enable_rss(struct jme_softc *);
173 static void jme_disable_rss(struct jme_softc *);
174 static void jme_serialize_skipmain(struct jme_softc *);
175 static void jme_deserialize_skipmain(struct jme_softc *);
177 static void jme_sysctl_node(struct jme_softc *);
178 static int jme_sysctl_tx_coal_to(SYSCTL_HANDLER_ARGS);
179 static int jme_sysctl_tx_coal_pkt(SYSCTL_HANDLER_ARGS);
180 static int jme_sysctl_rx_coal_to(SYSCTL_HANDLER_ARGS);
181 static int jme_sysctl_rx_coal_pkt(SYSCTL_HANDLER_ARGS);
183 static int jme_sysctl_npoll_rxoff(SYSCTL_HANDLER_ARGS);
184 static int jme_sysctl_npoll_txoff(SYSCTL_HANDLER_ARGS);
188 * Devices supported by this driver.
190 static const struct jme_dev {
191 uint16_t jme_vendorid;
192 uint16_t jme_deviceid;
194 const char *jme_name;
196 { PCI_VENDOR_JMICRON, PCI_PRODUCT_JMICRON_JMC250,
198 "JMicron Inc, JMC250 Gigabit Ethernet" },
199 { PCI_VENDOR_JMICRON, PCI_PRODUCT_JMICRON_JMC260,
201 "JMicron Inc, JMC260 Fast Ethernet" },
205 static device_method_t jme_methods[] = {
206 /* Device interface. */
207 DEVMETHOD(device_probe, jme_probe),
208 DEVMETHOD(device_attach, jme_attach),
209 DEVMETHOD(device_detach, jme_detach),
210 DEVMETHOD(device_shutdown, jme_shutdown),
211 DEVMETHOD(device_suspend, jme_suspend),
212 DEVMETHOD(device_resume, jme_resume),
215 DEVMETHOD(bus_print_child, bus_generic_print_child),
216 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
219 DEVMETHOD(miibus_readreg, jme_miibus_readreg),
220 DEVMETHOD(miibus_writereg, jme_miibus_writereg),
221 DEVMETHOD(miibus_statchg, jme_miibus_statchg),
226 static driver_t jme_driver = {
229 sizeof(struct jme_softc)
232 static devclass_t jme_devclass;
234 DECLARE_DUMMY_MODULE(if_jme);
235 MODULE_DEPEND(if_jme, miibus, 1, 1, 1);
236 DRIVER_MODULE(if_jme, pci, jme_driver, jme_devclass, NULL, NULL);
237 DRIVER_MODULE(miibus, jme, miibus_driver, miibus_devclass, NULL, NULL);
239 static const struct {
243 } jme_rx_status[JME_NRXRING_MAX] = {
244 { INTR_RXQ0_COAL | INTR_RXQ0_COAL_TO, INTR_RXQ0_COMP,
245 INTR_RXQ0_DESC_EMPTY },
246 { INTR_RXQ1_COAL | INTR_RXQ1_COAL_TO, INTR_RXQ1_COMP,
247 INTR_RXQ1_DESC_EMPTY },
248 { INTR_RXQ2_COAL | INTR_RXQ2_COAL_TO, INTR_RXQ2_COMP,
249 INTR_RXQ2_DESC_EMPTY },
250 { INTR_RXQ3_COAL | INTR_RXQ3_COAL_TO, INTR_RXQ3_COMP,
251 INTR_RXQ3_DESC_EMPTY }
254 static int jme_rx_desc_count = JME_RX_DESC_CNT_DEF;
255 static int jme_tx_desc_count = JME_TX_DESC_CNT_DEF;
256 static int jme_rx_ring_count = 0;
257 static int jme_msi_enable = 1;
258 static int jme_msix_enable = 1;
260 TUNABLE_INT("hw.jme.rx_desc_count", &jme_rx_desc_count);
261 TUNABLE_INT("hw.jme.tx_desc_count", &jme_tx_desc_count);
262 TUNABLE_INT("hw.jme.rx_ring_count", &jme_rx_ring_count);
263 TUNABLE_INT("hw.jme.msi.enable", &jme_msi_enable);
264 TUNABLE_INT("hw.jme.msix.enable", &jme_msix_enable);
267 jme_setup_rxdesc(struct jme_rxdesc *rxd)
269 struct jme_desc *desc;
272 desc->buflen = htole32(MCLBYTES);
273 desc->addr_lo = htole32(JME_ADDR_LO(rxd->rx_paddr));
274 desc->addr_hi = htole32(JME_ADDR_HI(rxd->rx_paddr));
275 desc->flags = htole32(JME_RD_OWN | JME_RD_INTR | JME_RD_64BIT);
279 * Read a PHY register on the MII of the JMC250.
282 jme_miibus_readreg(device_t dev, int phy, int reg)
284 struct jme_softc *sc = device_get_softc(dev);
288 /* For FPGA version, PHY address 0 should be ignored. */
289 if (sc->jme_caps & JME_CAP_FPGA) {
293 if (sc->jme_phyaddr != phy)
297 CSR_WRITE_4(sc, JME_SMI, SMI_OP_READ | SMI_OP_EXECUTE |
298 SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg));
300 for (i = JME_PHY_TIMEOUT; i > 0; i--) {
302 if (((val = CSR_READ_4(sc, JME_SMI)) & SMI_OP_EXECUTE) == 0)
306 device_printf(sc->jme_dev, "phy read timeout: "
307 "phy %d, reg %d\n", phy, reg);
311 return ((val & SMI_DATA_MASK) >> SMI_DATA_SHIFT);
315 * Write a PHY register on the MII of the JMC250.
318 jme_miibus_writereg(device_t dev, int phy, int reg, int val)
320 struct jme_softc *sc = device_get_softc(dev);
323 /* For FPGA version, PHY address 0 should be ignored. */
324 if (sc->jme_caps & JME_CAP_FPGA) {
328 if (sc->jme_phyaddr != phy)
332 CSR_WRITE_4(sc, JME_SMI, SMI_OP_WRITE | SMI_OP_EXECUTE |
333 ((val << SMI_DATA_SHIFT) & SMI_DATA_MASK) |
334 SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg));
336 for (i = JME_PHY_TIMEOUT; i > 0; i--) {
338 if (((val = CSR_READ_4(sc, JME_SMI)) & SMI_OP_EXECUTE) == 0)
342 device_printf(sc->jme_dev, "phy write timeout: "
343 "phy %d, reg %d\n", phy, reg);
350 * Callback from MII layer when media changes.
353 jme_miibus_statchg(device_t dev)
355 struct jme_softc *sc = device_get_softc(dev);
356 struct ifnet *ifp = &sc->arpcom.ac_if;
357 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
358 struct mii_data *mii;
359 struct jme_txdesc *txd;
364 jme_serialize_skipmain(sc);
365 ASSERT_IFNET_SERIALIZED_ALL(ifp);
367 if ((ifp->if_flags & IFF_RUNNING) == 0)
370 mii = device_get_softc(sc->jme_miibus);
372 sc->jme_has_link = FALSE;
373 if ((mii->mii_media_status & IFM_AVALID) != 0) {
374 switch (IFM_SUBTYPE(mii->mii_media_active)) {
377 sc->jme_has_link = TRUE;
380 if (sc->jme_caps & JME_CAP_FASTETH)
382 sc->jme_has_link = TRUE;
390 * Disabling Rx/Tx MACs have a side-effect of resetting
391 * JME_TXNDA/JME_RXNDA register to the first address of
392 * Tx/Rx descriptor address. So driver should reset its
393 * internal procucer/consumer pointer and reclaim any
394 * allocated resources. Note, just saving the value of
395 * JME_TXNDA and JME_RXNDA registers before stopping MAC
396 * and restoring JME_TXNDA/JME_RXNDA register is not
397 * sufficient to make sure correct MAC state because
398 * stopping MAC operation can take a while and hardware
399 * might have updated JME_TXNDA/JME_RXNDA registers
400 * during the stop operation.
403 /* Disable interrupts */
404 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
407 ifp->if_flags &= ~IFF_RUNNING;
408 ifq_clr_oactive(&ifp->if_snd);
410 callout_stop(&sc->jme_tick_ch);
412 /* Stop receiver/transmitter. */
416 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
417 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
419 jme_rxeof(rdata, -1);
420 if (rdata->jme_rxhead != NULL)
421 m_freem(rdata->jme_rxhead);
422 JME_RXCHAIN_RESET(rdata);
425 * Reuse configured Rx descriptors and reset
426 * procuder/consumer index.
428 rdata->jme_rx_cons = 0;
430 if (JME_ENABLE_HWRSS(sc))
436 if (tdata->jme_tx_cnt != 0) {
437 /* Remove queued packets for transmit. */
438 for (i = 0; i < tdata->jme_tx_desc_cnt; i++) {
439 txd = &tdata->jme_txdesc[i];
440 if (txd->tx_m != NULL) {
441 bus_dmamap_unload( tdata->jme_tx_tag,
446 IFNET_STAT_INC(ifp, oerrors, 1);
450 jme_init_tx_ring(tdata);
452 /* Initialize shadow status block. */
455 /* Program MAC with resolved speed/duplex/flow-control. */
456 if (sc->jme_has_link) {
459 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr);
461 /* Set Tx ring address to the hardware. */
462 paddr = tdata->jme_tx_ring_paddr;
463 CSR_WRITE_4(sc, JME_TXDBA_HI, JME_ADDR_HI(paddr));
464 CSR_WRITE_4(sc, JME_TXDBA_LO, JME_ADDR_LO(paddr));
466 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
467 CSR_WRITE_4(sc, JME_RXCSR,
468 sc->jme_rxcsr | RXCSR_RXQ_N_SEL(r));
470 /* Set Rx ring address to the hardware. */
471 paddr = sc->jme_cdata.jme_rx_data[r].jme_rx_ring_paddr;
472 CSR_WRITE_4(sc, JME_RXDBA_HI, JME_ADDR_HI(paddr));
473 CSR_WRITE_4(sc, JME_RXDBA_LO, JME_ADDR_LO(paddr));
476 /* Restart receiver/transmitter. */
477 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RX_ENB |
479 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr | TXCSR_TX_ENB);
482 ifp->if_flags |= IFF_RUNNING;
483 ifq_clr_oactive(&ifp->if_snd);
484 callout_reset_bycpu(&sc->jme_tick_ch, hz, jme_tick, sc,
488 if (!(ifp->if_flags & IFF_NPOLLING))
490 /* Reenable interrupts. */
491 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
495 jme_deserialize_skipmain(sc);
499 * Get the current interface media status.
502 jme_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
504 struct jme_softc *sc = ifp->if_softc;
505 struct mii_data *mii = device_get_softc(sc->jme_miibus);
507 ASSERT_IFNET_SERIALIZED_ALL(ifp);
510 ifmr->ifm_status = mii->mii_media_status;
511 ifmr->ifm_active = mii->mii_media_active;
515 * Set hardware to newly-selected media.
518 jme_mediachange(struct ifnet *ifp)
520 struct jme_softc *sc = ifp->if_softc;
521 struct mii_data *mii = device_get_softc(sc->jme_miibus);
524 ASSERT_IFNET_SERIALIZED_ALL(ifp);
526 if (mii->mii_instance != 0) {
527 struct mii_softc *miisc;
529 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
530 mii_phy_reset(miisc);
532 error = mii_mediachg(mii);
538 jme_probe(device_t dev)
540 const struct jme_dev *sp;
543 vid = pci_get_vendor(dev);
544 did = pci_get_device(dev);
545 for (sp = jme_devs; sp->jme_name != NULL; ++sp) {
546 if (vid == sp->jme_vendorid && did == sp->jme_deviceid) {
547 struct jme_softc *sc = device_get_softc(dev);
549 sc->jme_caps = sp->jme_caps;
550 device_set_desc(dev, sp->jme_name);
558 jme_eeprom_read_byte(struct jme_softc *sc, uint8_t addr, uint8_t *val)
564 for (i = JME_TIMEOUT; i > 0; i--) {
565 reg = CSR_READ_4(sc, JME_SMBCSR);
566 if ((reg & SMBCSR_HW_BUSY_MASK) == SMBCSR_HW_IDLE)
572 device_printf(sc->jme_dev, "EEPROM idle timeout!\n");
576 reg = ((uint32_t)addr << SMBINTF_ADDR_SHIFT) & SMBINTF_ADDR_MASK;
577 CSR_WRITE_4(sc, JME_SMBINTF, reg | SMBINTF_RD | SMBINTF_CMD_TRIGGER);
578 for (i = JME_TIMEOUT; i > 0; i--) {
580 reg = CSR_READ_4(sc, JME_SMBINTF);
581 if ((reg & SMBINTF_CMD_TRIGGER) == 0)
586 device_printf(sc->jme_dev, "EEPROM read timeout!\n");
590 reg = CSR_READ_4(sc, JME_SMBINTF);
591 *val = (reg & SMBINTF_RD_DATA_MASK) >> SMBINTF_RD_DATA_SHIFT;
597 jme_eeprom_macaddr(struct jme_softc *sc, uint8_t eaddr[])
599 uint8_t fup, reg, val;
604 if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 ||
605 fup != JME_EEPROM_SIG0)
607 if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 ||
608 fup != JME_EEPROM_SIG1)
612 if (jme_eeprom_read_byte(sc, offset, &fup) != 0)
614 if (JME_EEPROM_MKDESC(JME_EEPROM_FUNC0, JME_EEPROM_PAGE_BAR1) ==
615 (fup & (JME_EEPROM_FUNC_MASK | JME_EEPROM_PAGE_MASK))) {
616 if (jme_eeprom_read_byte(sc, offset + 1, ®) != 0)
618 if (reg >= JME_PAR0 &&
619 reg < JME_PAR0 + ETHER_ADDR_LEN) {
620 if (jme_eeprom_read_byte(sc, offset + 2,
623 eaddr[reg - JME_PAR0] = val;
627 /* Check for the end of EEPROM descriptor. */
628 if ((fup & JME_EEPROM_DESC_END) == JME_EEPROM_DESC_END)
630 /* Try next eeprom descriptor. */
631 offset += JME_EEPROM_DESC_BYTES;
632 } while (match != ETHER_ADDR_LEN && offset < JME_EEPROM_END);
634 if (match == ETHER_ADDR_LEN)
641 jme_reg_macaddr(struct jme_softc *sc, uint8_t eaddr[])
645 /* Read station address. */
646 par0 = CSR_READ_4(sc, JME_PAR0);
647 par1 = CSR_READ_4(sc, JME_PAR1);
649 if ((par0 == 0 && par1 == 0) || (par0 & 0x1)) {
650 device_printf(sc->jme_dev,
651 "generating fake ethernet address.\n");
652 par0 = karc4random();
653 /* Set OUI to JMicron. */
657 eaddr[3] = (par0 >> 16) & 0xff;
658 eaddr[4] = (par0 >> 8) & 0xff;
659 eaddr[5] = par0 & 0xff;
661 eaddr[0] = (par0 >> 0) & 0xFF;
662 eaddr[1] = (par0 >> 8) & 0xFF;
663 eaddr[2] = (par0 >> 16) & 0xFF;
664 eaddr[3] = (par0 >> 24) & 0xFF;
665 eaddr[4] = (par1 >> 0) & 0xFF;
666 eaddr[5] = (par1 >> 8) & 0xFF;
671 jme_attach(device_t dev)
673 struct jme_softc *sc = device_get_softc(dev);
674 struct ifnet *ifp = &sc->arpcom.ac_if;
677 uint8_t pcie_ptr, rev;
678 int error = 0, i, j, rx_desc_cnt, coal_max;
679 uint8_t eaddr[ETHER_ADDR_LEN];
681 int offset, offset_def;
685 * Initialize serializers
687 lwkt_serialize_init(&sc->jme_serialize);
688 lwkt_serialize_init(&sc->jme_cdata.jme_tx_data.jme_tx_serialize);
689 for (i = 0; i < JME_NRXRING_MAX; ++i) {
691 &sc->jme_cdata.jme_rx_data[i].jme_rx_serialize);
695 * Get # of RX ring descriptors
697 rx_desc_cnt = device_getenv_int(dev, "rx_desc_count",
699 rx_desc_cnt = roundup(rx_desc_cnt, JME_NDESC_ALIGN);
700 if (rx_desc_cnt > JME_NDESC_MAX)
701 rx_desc_cnt = JME_NDESC_MAX;
704 * Get # of TX ring descriptors
706 sc->jme_cdata.jme_tx_data.jme_tx_desc_cnt =
707 device_getenv_int(dev, "tx_desc_count", jme_tx_desc_count);
708 sc->jme_cdata.jme_tx_data.jme_tx_desc_cnt =
709 roundup(sc->jme_cdata.jme_tx_data.jme_tx_desc_cnt, JME_NDESC_ALIGN);
710 if (sc->jme_cdata.jme_tx_data.jme_tx_desc_cnt > JME_NDESC_MAX)
711 sc->jme_cdata.jme_tx_data.jme_tx_desc_cnt = JME_NDESC_MAX;
716 sc->jme_cdata.jme_rx_ring_cnt = device_getenv_int(dev, "rx_ring_count",
718 sc->jme_cdata.jme_rx_ring_cnt =
719 if_ring_count2(sc->jme_cdata.jme_rx_ring_cnt, JME_NRXRING_MAX);
722 * Initialize serializer array
725 sc->jme_serialize_arr[i++] = &sc->jme_serialize;
727 KKASSERT(i == JME_TX_SERIALIZE);
728 sc->jme_serialize_arr[i++] =
729 &sc->jme_cdata.jme_tx_data.jme_tx_serialize;
731 KKASSERT(i == JME_RX_SERIALIZE);
732 for (j = 0; j < sc->jme_cdata.jme_rx_ring_cnt; ++j) {
733 sc->jme_serialize_arr[i++] =
734 &sc->jme_cdata.jme_rx_data[j].jme_rx_serialize;
736 KKASSERT(i <= JME_NSERIALIZE);
737 sc->jme_serialize_cnt = i;
740 * Setup TX ring specific data
742 sc->jme_cdata.jme_tx_data.jme_sc = sc;
745 * Setup RX rings specific data
747 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
748 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[i];
751 rdata->jme_rx_coal = jme_rx_status[i].jme_coal;
752 rdata->jme_rx_comp = jme_rx_status[i].jme_comp;
753 rdata->jme_rx_empty = jme_rx_status[i].jme_empty;
754 rdata->jme_rx_idx = i;
755 rdata->jme_rx_desc_cnt = rx_desc_cnt;
759 sc->jme_lowaddr = BUS_SPACE_MAXADDR;
761 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
763 callout_init(&sc->jme_tick_ch);
766 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
769 irq = pci_read_config(dev, PCIR_INTLINE, 4);
770 mem = pci_read_config(dev, JME_PCIR_BAR, 4);
772 device_printf(dev, "chip is in D%d power mode "
773 "-- setting to D0\n", pci_get_powerstate(dev));
775 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
777 pci_write_config(dev, PCIR_INTLINE, irq, 4);
778 pci_write_config(dev, JME_PCIR_BAR, mem, 4);
780 #endif /* !BURN_BRIDGE */
782 /* Enable bus mastering */
783 pci_enable_busmaster(dev);
788 * JMC250 supports both memory mapped and I/O register space
789 * access. Because I/O register access should use different
790 * BARs to access registers it's waste of time to use I/O
791 * register spce access. JMC250 uses 16K to map entire memory
794 sc->jme_mem_rid = JME_PCIR_BAR;
795 sc->jme_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
796 &sc->jme_mem_rid, RF_ACTIVE);
797 if (sc->jme_mem_res == NULL) {
798 device_printf(dev, "can't allocate IO memory\n");
801 sc->jme_mem_bt = rman_get_bustag(sc->jme_mem_res);
802 sc->jme_mem_bh = rman_get_bushandle(sc->jme_mem_res);
807 error = jme_intr_alloc(dev);
814 reg = CSR_READ_4(sc, JME_CHIPMODE);
815 if (((reg & CHIPMODE_FPGA_REV_MASK) >> CHIPMODE_FPGA_REV_SHIFT) !=
817 sc->jme_caps |= JME_CAP_FPGA;
819 device_printf(dev, "FPGA revision: 0x%04x\n",
820 (reg & CHIPMODE_FPGA_REV_MASK) >>
821 CHIPMODE_FPGA_REV_SHIFT);
825 /* NOTE: FM revision is put in the upper 4 bits */
826 rev = ((reg & CHIPMODE_REVFM_MASK) >> CHIPMODE_REVFM_SHIFT) << 4;
827 rev |= (reg & CHIPMODE_REVECO_MASK) >> CHIPMODE_REVECO_SHIFT;
829 device_printf(dev, "Revision (FM/ECO): 0x%02x\n", rev);
831 did = pci_get_device(dev);
833 case PCI_PRODUCT_JMICRON_JMC250:
834 if (rev == JME_REV1_A2)
835 sc->jme_workaround |= JME_WA_EXTFIFO | JME_WA_HDX;
838 case PCI_PRODUCT_JMICRON_JMC260:
840 sc->jme_lowaddr = BUS_SPACE_MAXADDR_32BIT;
844 panic("unknown device id 0x%04x", did);
846 if (rev >= JME_REV2) {
847 sc->jme_clksrc = GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC;
848 sc->jme_clksrc_1000 = GHC_TXOFL_CLKSRC_1000 |
849 GHC_TXMAC_CLKSRC_1000;
852 /* Reset the ethernet controller. */
855 /* Map MSI/MSI-X vectors */
858 /* Get station address. */
859 reg = CSR_READ_4(sc, JME_SMBCSR);
860 if (reg & SMBCSR_EEPROM_PRESENT)
861 error = jme_eeprom_macaddr(sc, eaddr);
862 if (error != 0 || (reg & SMBCSR_EEPROM_PRESENT) == 0) {
863 if (error != 0 && (bootverbose)) {
864 device_printf(dev, "ethernet hardware address "
865 "not found in EEPROM.\n");
867 jme_reg_macaddr(sc, eaddr);
872 * Integrated JR0211 has fixed PHY address whereas FPGA version
873 * requires PHY probing to get correct PHY address.
875 if ((sc->jme_caps & JME_CAP_FPGA) == 0) {
876 sc->jme_phyaddr = CSR_READ_4(sc, JME_GPREG0) &
877 GPREG0_PHY_ADDR_MASK;
879 device_printf(dev, "PHY is at address %d.\n",
886 /* Set max allowable DMA size. */
887 pcie_ptr = pci_get_pciecap_ptr(dev);
891 sc->jme_caps |= JME_CAP_PCIE;
892 ctrl = pci_read_config(dev, pcie_ptr + PCIER_DEVCTRL, 2);
894 device_printf(dev, "Read request size : %d bytes.\n",
895 128 << ((ctrl >> 12) & 0x07));
896 device_printf(dev, "TLP payload size : %d bytes.\n",
897 128 << ((ctrl >> 5) & 0x07));
899 switch (ctrl & PCIEM_DEVCTL_MAX_READRQ_MASK) {
900 case PCIEM_DEVCTL_MAX_READRQ_128:
901 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_128;
903 case PCIEM_DEVCTL_MAX_READRQ_256:
904 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_256;
907 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_512;
910 sc->jme_rx_dma_size = RXCSR_DMA_SIZE_128;
912 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_512;
913 sc->jme_rx_dma_size = RXCSR_DMA_SIZE_128;
917 if (pci_find_extcap(dev, PCIY_PMG, &pmc) == 0)
918 sc->jme_caps |= JME_CAP_PMCAP;
923 * NPOLLING RX CPU offset
925 if (sc->jme_cdata.jme_rx_ring_cnt == ncpus2) {
928 offset_def = (sc->jme_cdata.jme_rx_ring_cnt *
929 device_get_unit(dev)) % ncpus2;
930 offset = device_getenv_int(dev, "npoll.rxoff", offset_def);
931 if (offset >= ncpus2 ||
932 offset % sc->jme_cdata.jme_rx_ring_cnt != 0) {
933 device_printf(dev, "invalid npoll.rxoff %d, use %d\n",
938 sc->jme_npoll_rxoff = offset;
941 * NPOLLING TX CPU offset
943 offset_def = sc->jme_npoll_rxoff;
944 offset = device_getenv_int(dev, "npoll.txoff", offset_def);
945 if (offset >= ncpus2) {
946 device_printf(dev, "invalid npoll.txoff %d, use %d\n",
950 sc->jme_npoll_txoff = offset;
954 * Set default coalesce valves
956 sc->jme_tx_coal_to = PCCTX_COAL_TO_DEFAULT;
957 sc->jme_tx_coal_pkt = PCCTX_COAL_PKT_DEFAULT;
958 sc->jme_rx_coal_to = PCCRX_COAL_TO_DEFAULT;
959 sc->jme_rx_coal_pkt = PCCRX_COAL_PKT_DEFAULT;
962 * Adjust coalesce valves, in case that the number of TX/RX
963 * descs are set to small values by users.
965 * NOTE: coal_max will not be zero, since number of descs
966 * must aligned by JME_NDESC_ALIGN (16 currently)
968 coal_max = sc->jme_cdata.jme_tx_data.jme_tx_desc_cnt / 2;
969 if (coal_max < sc->jme_tx_coal_pkt)
970 sc->jme_tx_coal_pkt = coal_max;
972 coal_max = sc->jme_cdata.jme_rx_data[0].jme_rx_desc_cnt / 2;
973 if (coal_max < sc->jme_rx_coal_pkt)
974 sc->jme_rx_coal_pkt = coal_max;
976 sc->jme_cdata.jme_tx_data.jme_tx_wreg = 16;
983 /* Allocate DMA stuffs */
984 error = jme_dma_alloc(sc);
989 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
990 ifp->if_init = jme_init;
991 ifp->if_ioctl = jme_ioctl;
992 ifp->if_start = jme_start;
994 ifp->if_npoll = jme_npoll;
996 ifp->if_watchdog = jme_watchdog;
997 ifp->if_serialize = jme_serialize;
998 ifp->if_deserialize = jme_deserialize;
999 ifp->if_tryserialize = jme_tryserialize;
1001 ifp->if_serialize_assert = jme_serialize_assert;
1003 ifq_set_maxlen(&ifp->if_snd,
1004 sc->jme_cdata.jme_tx_data.jme_tx_desc_cnt - JME_TXD_RSVD);
1005 ifq_set_ready(&ifp->if_snd);
1007 /* JMC250 supports Tx/Rx checksum offload and hardware vlan tagging. */
1008 ifp->if_capabilities = IFCAP_HWCSUM |
1011 IFCAP_VLAN_HWTAGGING;
1012 if (sc->jme_cdata.jme_rx_ring_cnt > JME_NRXRING_MIN)
1013 ifp->if_capabilities |= IFCAP_RSS;
1014 ifp->if_capenable = ifp->if_capabilities;
1017 * Disable TXCSUM by default to improve bulk data
1018 * transmit performance (+20Mbps improvement).
1020 ifp->if_capenable &= ~IFCAP_TXCSUM;
1022 if (ifp->if_capenable & IFCAP_TXCSUM)
1023 ifp->if_hwassist |= JME_CSUM_FEATURES;
1024 ifp->if_hwassist |= CSUM_TSO;
1026 /* Set up MII bus. */
1027 error = mii_phy_probe(dev, &sc->jme_miibus,
1028 jme_mediachange, jme_mediastatus);
1030 device_printf(dev, "no PHY found!\n");
1035 * Save PHYADDR for FPGA mode PHY.
1037 if (sc->jme_caps & JME_CAP_FPGA) {
1038 struct mii_data *mii = device_get_softc(sc->jme_miibus);
1040 if (mii->mii_instance != 0) {
1041 struct mii_softc *miisc;
1043 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
1044 if (miisc->mii_phy != 0) {
1045 sc->jme_phyaddr = miisc->mii_phy;
1049 if (sc->jme_phyaddr != 0) {
1050 device_printf(sc->jme_dev,
1051 "FPGA PHY is at %d\n", sc->jme_phyaddr);
1053 jme_miibus_writereg(dev, sc->jme_phyaddr,
1054 JMPHY_CONF, JMPHY_CONF_DEFFIFO);
1056 /* XXX should we clear JME_WA_EXTFIFO */
1061 ether_ifattach(ifp, eaddr, NULL);
1063 /* Tell the upper layer(s) we support long frames. */
1064 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1066 error = jme_intr_setup(dev);
1068 ether_ifdetach(ifp);
1071 ifq_set_cpuid(&ifp->if_snd, sc->jme_tx_cpuid);
1080 jme_detach(device_t dev)
1082 struct jme_softc *sc = device_get_softc(dev);
1084 if (device_is_attached(dev)) {
1085 struct ifnet *ifp = &sc->arpcom.ac_if;
1087 ifnet_serialize_all(ifp);
1089 jme_intr_teardown(dev);
1090 ifnet_deserialize_all(ifp);
1092 ether_ifdetach(ifp);
1095 if (sc->jme_sysctl_tree != NULL)
1096 sysctl_ctx_free(&sc->jme_sysctl_ctx);
1098 if (sc->jme_miibus != NULL)
1099 device_delete_child(dev, sc->jme_miibus);
1100 bus_generic_detach(dev);
1104 if (sc->jme_mem_res != NULL) {
1105 bus_release_resource(dev, SYS_RES_MEMORY, sc->jme_mem_rid,
1115 jme_sysctl_node(struct jme_softc *sc)
1117 #ifdef JME_RSS_DEBUG
1121 sysctl_ctx_init(&sc->jme_sysctl_ctx);
1122 sc->jme_sysctl_tree = SYSCTL_ADD_NODE(&sc->jme_sysctl_ctx,
1123 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
1124 device_get_nameunit(sc->jme_dev),
1126 if (sc->jme_sysctl_tree == NULL) {
1127 device_printf(sc->jme_dev, "can't add sysctl node\n");
1131 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1132 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1133 "tx_coal_to", CTLTYPE_INT | CTLFLAG_RW,
1134 sc, 0, jme_sysctl_tx_coal_to, "I", "jme tx coalescing timeout");
1136 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1137 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1138 "tx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW,
1139 sc, 0, jme_sysctl_tx_coal_pkt, "I", "jme tx coalescing packet");
1141 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1142 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1143 "rx_coal_to", CTLTYPE_INT | CTLFLAG_RW,
1144 sc, 0, jme_sysctl_rx_coal_to, "I", "jme rx coalescing timeout");
1146 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1147 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1148 "rx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW,
1149 sc, 0, jme_sysctl_rx_coal_pkt, "I", "jme rx coalescing packet");
1151 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1152 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1153 "rx_desc_count", CTLFLAG_RD,
1154 &sc->jme_cdata.jme_rx_data[0].jme_rx_desc_cnt,
1155 0, "RX desc count");
1156 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1157 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1158 "tx_desc_count", CTLFLAG_RD,
1159 &sc->jme_cdata.jme_tx_data.jme_tx_desc_cnt,
1160 0, "TX desc count");
1161 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1162 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1163 "rx_ring_count", CTLFLAG_RD,
1164 &sc->jme_cdata.jme_rx_ring_cnt,
1165 0, "RX ring count");
1166 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1167 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1168 "tx_wreg", CTLFLAG_RW,
1169 &sc->jme_cdata.jme_tx_data.jme_tx_wreg, 0,
1170 "# of segments before writing to hardware register");
1172 #ifdef JME_RSS_DEBUG
1173 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1174 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1175 "rss_debug", CTLFLAG_RW, &sc->jme_rss_debug,
1176 0, "RSS debug level");
1177 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1178 char rx_ring_desc[32];
1180 ksnprintf(rx_ring_desc, sizeof(rx_ring_desc),
1181 "rx_ring%d_pkt", r);
1182 SYSCTL_ADD_ULONG(&sc->jme_sysctl_ctx,
1183 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1184 rx_ring_desc, CTLFLAG_RW,
1185 &sc->jme_cdata.jme_rx_data[r].jme_rx_pkt, "RXed packets");
1187 ksnprintf(rx_ring_desc, sizeof(rx_ring_desc),
1188 "rx_ring%d_emp", r);
1189 SYSCTL_ADD_ULONG(&sc->jme_sysctl_ctx,
1190 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1191 rx_ring_desc, CTLFLAG_RW,
1192 &sc->jme_cdata.jme_rx_data[r].jme_rx_emp,
1193 "# of time RX ring empty");
1197 #ifdef IFPOLL_ENABLE
1198 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1199 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1200 "npoll_rxoff", CTLTYPE_INT|CTLFLAG_RW, sc, 0,
1201 jme_sysctl_npoll_rxoff, "I", "NPOLLING RX cpu offset");
1202 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1203 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1204 "npoll_txoff", CTLTYPE_INT|CTLFLAG_RW, sc, 0,
1205 jme_sysctl_npoll_txoff, "I", "NPOLLING TX cpu offset");
1210 jme_dma_alloc(struct jme_softc *sc)
1212 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
1213 struct jme_txdesc *txd;
1215 int error, i, asize;
1217 asize = __VM_CACHELINE_ALIGN(
1218 tdata->jme_tx_desc_cnt * sizeof(struct jme_txdesc));
1219 tdata->jme_txdesc = kmalloc_cachealign(asize, M_DEVBUF,
1222 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
1223 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[i];
1225 asize = __VM_CACHELINE_ALIGN(
1226 rdata->jme_rx_desc_cnt * sizeof(struct jme_rxdesc));
1227 rdata->jme_rxdesc = kmalloc_cachealign(asize, M_DEVBUF,
1231 /* Create parent ring tag. */
1232 error = bus_dma_tag_create(NULL,/* parent */
1233 1, JME_RING_BOUNDARY, /* algnmnt, boundary */
1234 sc->jme_lowaddr, /* lowaddr */
1235 BUS_SPACE_MAXADDR, /* highaddr */
1236 NULL, NULL, /* filter, filterarg */
1237 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1239 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1241 &sc->jme_cdata.jme_ring_tag);
1243 device_printf(sc->jme_dev,
1244 "could not create parent ring DMA tag.\n");
1249 * Create DMA stuffs for TX ring
1251 asize = roundup2(JME_TX_RING_SIZE(tdata), JME_TX_RING_ALIGN);
1252 error = bus_dmamem_coherent(sc->jme_cdata.jme_ring_tag,
1253 JME_TX_RING_ALIGN, 0,
1254 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1255 asize, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
1257 device_printf(sc->jme_dev, "could not allocate Tx ring.\n");
1260 tdata->jme_tx_ring_tag = dmem.dmem_tag;
1261 tdata->jme_tx_ring_map = dmem.dmem_map;
1262 tdata->jme_tx_ring = dmem.dmem_addr;
1263 tdata->jme_tx_ring_paddr = dmem.dmem_busaddr;
1266 * Create DMA stuffs for RX rings
1268 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
1269 error = jme_rxring_dma_alloc(&sc->jme_cdata.jme_rx_data[i]);
1274 /* Create parent buffer tag. */
1275 error = bus_dma_tag_create(NULL,/* parent */
1276 1, 0, /* algnmnt, boundary */
1277 sc->jme_lowaddr, /* lowaddr */
1278 BUS_SPACE_MAXADDR, /* highaddr */
1279 NULL, NULL, /* filter, filterarg */
1280 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1282 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1284 &sc->jme_cdata.jme_buffer_tag);
1286 device_printf(sc->jme_dev,
1287 "could not create parent buffer DMA tag.\n");
1292 * Create DMA stuffs for shadow status block
1294 asize = roundup2(JME_SSB_SIZE, JME_SSB_ALIGN);
1295 error = bus_dmamem_coherent(sc->jme_cdata.jme_buffer_tag,
1296 JME_SSB_ALIGN, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1297 asize, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
1299 device_printf(sc->jme_dev,
1300 "could not create shadow status block.\n");
1303 sc->jme_cdata.jme_ssb_tag = dmem.dmem_tag;
1304 sc->jme_cdata.jme_ssb_map = dmem.dmem_map;
1305 sc->jme_cdata.jme_ssb_block = dmem.dmem_addr;
1306 sc->jme_cdata.jme_ssb_block_paddr = dmem.dmem_busaddr;
1309 * Create DMA stuffs for TX buffers
1312 /* Create tag for Tx buffers. */
1313 error = bus_dma_tag_create(sc->jme_cdata.jme_buffer_tag,/* parent */
1314 1, 0, /* algnmnt, boundary */
1315 BUS_SPACE_MAXADDR, /* lowaddr */
1316 BUS_SPACE_MAXADDR, /* highaddr */
1317 NULL, NULL, /* filter, filterarg */
1318 JME_TSO_MAXSIZE, /* maxsize */
1319 JME_MAXTXSEGS, /* nsegments */
1320 JME_MAXSEGSIZE, /* maxsegsize */
1321 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,/* flags */
1322 &tdata->jme_tx_tag);
1324 device_printf(sc->jme_dev, "could not create Tx DMA tag.\n");
1328 /* Create DMA maps for Tx buffers. */
1329 for (i = 0; i < tdata->jme_tx_desc_cnt; i++) {
1330 txd = &tdata->jme_txdesc[i];
1331 error = bus_dmamap_create(tdata->jme_tx_tag,
1332 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
1337 device_printf(sc->jme_dev,
1338 "could not create %dth Tx dmamap.\n", i);
1340 for (j = 0; j < i; ++j) {
1341 txd = &tdata->jme_txdesc[j];
1342 bus_dmamap_destroy(tdata->jme_tx_tag,
1345 bus_dma_tag_destroy(tdata->jme_tx_tag);
1346 tdata->jme_tx_tag = NULL;
1352 * Create DMA stuffs for RX buffers
1354 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
1355 error = jme_rxbuf_dma_alloc(&sc->jme_cdata.jme_rx_data[i]);
1363 jme_dma_free(struct jme_softc *sc)
1365 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
1366 struct jme_txdesc *txd;
1367 struct jme_rxdesc *rxd;
1368 struct jme_rxdata *rdata;
1372 if (tdata->jme_tx_ring_tag != NULL) {
1373 bus_dmamap_unload(tdata->jme_tx_ring_tag,
1374 tdata->jme_tx_ring_map);
1375 bus_dmamem_free(tdata->jme_tx_ring_tag,
1376 tdata->jme_tx_ring, tdata->jme_tx_ring_map);
1377 bus_dma_tag_destroy(tdata->jme_tx_ring_tag);
1378 tdata->jme_tx_ring_tag = NULL;
1382 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1383 rdata = &sc->jme_cdata.jme_rx_data[r];
1384 if (rdata->jme_rx_ring_tag != NULL) {
1385 bus_dmamap_unload(rdata->jme_rx_ring_tag,
1386 rdata->jme_rx_ring_map);
1387 bus_dmamem_free(rdata->jme_rx_ring_tag,
1389 rdata->jme_rx_ring_map);
1390 bus_dma_tag_destroy(rdata->jme_rx_ring_tag);
1391 rdata->jme_rx_ring_tag = NULL;
1396 if (tdata->jme_tx_tag != NULL) {
1397 for (i = 0; i < tdata->jme_tx_desc_cnt; i++) {
1398 txd = &tdata->jme_txdesc[i];
1399 bus_dmamap_destroy(tdata->jme_tx_tag, txd->tx_dmamap);
1401 bus_dma_tag_destroy(tdata->jme_tx_tag);
1402 tdata->jme_tx_tag = NULL;
1406 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1407 rdata = &sc->jme_cdata.jme_rx_data[r];
1408 if (rdata->jme_rx_tag != NULL) {
1409 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
1410 rxd = &rdata->jme_rxdesc[i];
1411 bus_dmamap_destroy(rdata->jme_rx_tag,
1414 bus_dmamap_destroy(rdata->jme_rx_tag,
1415 rdata->jme_rx_sparemap);
1416 bus_dma_tag_destroy(rdata->jme_rx_tag);
1417 rdata->jme_rx_tag = NULL;
1421 /* Shadow status block. */
1422 if (sc->jme_cdata.jme_ssb_tag != NULL) {
1423 bus_dmamap_unload(sc->jme_cdata.jme_ssb_tag,
1424 sc->jme_cdata.jme_ssb_map);
1425 bus_dmamem_free(sc->jme_cdata.jme_ssb_tag,
1426 sc->jme_cdata.jme_ssb_block,
1427 sc->jme_cdata.jme_ssb_map);
1428 bus_dma_tag_destroy(sc->jme_cdata.jme_ssb_tag);
1429 sc->jme_cdata.jme_ssb_tag = NULL;
1432 if (sc->jme_cdata.jme_buffer_tag != NULL) {
1433 bus_dma_tag_destroy(sc->jme_cdata.jme_buffer_tag);
1434 sc->jme_cdata.jme_buffer_tag = NULL;
1436 if (sc->jme_cdata.jme_ring_tag != NULL) {
1437 bus_dma_tag_destroy(sc->jme_cdata.jme_ring_tag);
1438 sc->jme_cdata.jme_ring_tag = NULL;
1441 if (tdata->jme_txdesc != NULL) {
1442 kfree(tdata->jme_txdesc, M_DEVBUF);
1443 tdata->jme_txdesc = NULL;
1445 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1446 rdata = &sc->jme_cdata.jme_rx_data[r];
1447 if (rdata->jme_rxdesc != NULL) {
1448 kfree(rdata->jme_rxdesc, M_DEVBUF);
1449 rdata->jme_rxdesc = NULL;
1455 * Make sure the interface is stopped at reboot time.
1458 jme_shutdown(device_t dev)
1460 return jme_suspend(dev);
1465 * Unlike other ethernet controllers, JMC250 requires
1466 * explicit resetting link speed to 10/100Mbps as gigabit
1467 * link will cunsume more power than 375mA.
1468 * Note, we reset the link speed to 10/100Mbps with
1469 * auto-negotiation but we don't know whether that operation
1470 * would succeed or not as we have no control after powering
1471 * off. If the renegotiation fail WOL may not work. Running
1472 * at 1Gbps draws more power than 375mA at 3.3V which is
1473 * specified in PCI specification and that would result in
1474 * complete shutdowning power to ethernet controller.
1477 * Save current negotiated media speed/duplex/flow-control
1478 * to softc and restore the same link again after resuming.
1479 * PHY handling such as power down/resetting to 100Mbps
1480 * may be better handled in suspend method in phy driver.
1483 jme_setlinkspeed(struct jme_softc *sc)
1485 struct mii_data *mii;
1488 JME_LOCK_ASSERT(sc);
1490 mii = device_get_softc(sc->jme_miibus);
1493 if ((mii->mii_media_status & IFM_AVALID) != 0) {
1494 switch IFM_SUBTYPE(mii->mii_media_active) {
1504 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_100T2CR, 0);
1505 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_ANAR,
1506 ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
1507 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_BMCR,
1508 BMCR_AUTOEN | BMCR_STARTNEG);
1511 /* Poll link state until jme(4) get a 10/100 link. */
1512 for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
1514 if ((mii->mii_media_status & IFM_AVALID) != 0) {
1515 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1525 pause("jmelnk", hz);
1528 if (i == MII_ANEGTICKS_GIGE)
1529 device_printf(sc->jme_dev, "establishing link failed, "
1530 "WOL may not work!");
1533 * No link, force MAC to have 100Mbps, full-duplex link.
1534 * This is the last resort and may/may not work.
1536 mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
1537 mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1542 jme_setwol(struct jme_softc *sc)
1544 struct ifnet *ifp = &sc->arpcom.ac_if;
1549 if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1550 /* No PME capability, PHY power down. */
1551 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1552 MII_BMCR, BMCR_PDOWN);
1556 gpr = CSR_READ_4(sc, JME_GPREG0) & ~GPREG0_PME_ENB;
1557 pmcs = CSR_READ_4(sc, JME_PMCS);
1558 pmcs &= ~PMCS_WOL_ENB_MASK;
1559 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) {
1560 pmcs |= PMCS_MAGIC_FRAME | PMCS_MAGIC_FRAME_ENB;
1561 /* Enable PME message. */
1562 gpr |= GPREG0_PME_ENB;
1563 /* For gigabit controllers, reset link speed to 10/100. */
1564 if ((sc->jme_caps & JME_CAP_FASTETH) == 0)
1565 jme_setlinkspeed(sc);
1568 CSR_WRITE_4(sc, JME_PMCS, pmcs);
1569 CSR_WRITE_4(sc, JME_GPREG0, gpr);
1572 pmstat = pci_read_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, 2);
1573 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
1574 if ((ifp->if_capenable & IFCAP_WOL) != 0)
1575 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
1576 pci_write_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1577 if ((ifp->if_capenable & IFCAP_WOL) == 0) {
1578 /* No WOL, PHY power down. */
1579 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1580 MII_BMCR, BMCR_PDOWN);
1586 jme_suspend(device_t dev)
1588 struct jme_softc *sc = device_get_softc(dev);
1589 struct ifnet *ifp = &sc->arpcom.ac_if;
1591 ifnet_serialize_all(ifp);
1596 ifnet_deserialize_all(ifp);
1602 jme_resume(device_t dev)
1604 struct jme_softc *sc = device_get_softc(dev);
1605 struct ifnet *ifp = &sc->arpcom.ac_if;
1610 ifnet_serialize_all(ifp);
1613 if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1616 pmstat = pci_read_config(sc->jme_dev,
1617 pmc + PCIR_POWER_STATUS, 2);
1618 /* Disable PME clear PME status. */
1619 pmstat &= ~PCIM_PSTAT_PMEENABLE;
1620 pci_write_config(sc->jme_dev,
1621 pmc + PCIR_POWER_STATUS, pmstat, 2);
1625 if (ifp->if_flags & IFF_UP)
1628 ifnet_deserialize_all(ifp);
1634 jme_tso_pullup(struct mbuf **mp)
1636 int hoff, iphlen, thoff;
1640 KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
1642 iphlen = m->m_pkthdr.csum_iphlen;
1643 thoff = m->m_pkthdr.csum_thlen;
1644 hoff = m->m_pkthdr.csum_lhlen;
1646 KASSERT(iphlen > 0, ("invalid ip hlen"));
1647 KASSERT(thoff > 0, ("invalid tcp hlen"));
1648 KASSERT(hoff > 0, ("invalid ether hlen"));
1650 if (__predict_false(m->m_len < hoff + iphlen + thoff)) {
1651 m = m_pullup(m, hoff + iphlen + thoff);
1662 jme_encap(struct jme_txdata *tdata, struct mbuf **m_head, int *segs_used)
1664 struct jme_txdesc *txd;
1665 struct jme_desc *desc;
1667 bus_dma_segment_t txsegs[JME_MAXTXSEGS];
1669 int error, i, prod, symbol_desc;
1670 uint32_t cflags, flag64, mss;
1672 M_ASSERTPKTHDR((*m_head));
1674 if ((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) {
1675 /* XXX Is this necessary? */
1676 error = jme_tso_pullup(m_head);
1681 prod = tdata->jme_tx_prod;
1682 txd = &tdata->jme_txdesc[prod];
1684 if (tdata->jme_sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT)
1689 maxsegs = (tdata->jme_tx_desc_cnt - tdata->jme_tx_cnt) -
1690 (JME_TXD_RSVD + symbol_desc);
1691 if (maxsegs > JME_MAXTXSEGS)
1692 maxsegs = JME_MAXTXSEGS;
1693 KASSERT(maxsegs >= (JME_TXD_SPARE - symbol_desc),
1694 ("not enough segments %d", maxsegs));
1696 error = bus_dmamap_load_mbuf_defrag(tdata->jme_tx_tag,
1697 txd->tx_dmamap, m_head,
1698 txsegs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
1701 *segs_used += nsegs;
1703 bus_dmamap_sync(tdata->jme_tx_tag, txd->tx_dmamap,
1704 BUS_DMASYNC_PREWRITE);
1710 /* Configure checksum offload. */
1711 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1712 mss = (uint32_t)m->m_pkthdr.tso_segsz << JME_TD_MSS_SHIFT;
1713 cflags |= JME_TD_TSO;
1714 } else if (m->m_pkthdr.csum_flags & JME_CSUM_FEATURES) {
1715 if (m->m_pkthdr.csum_flags & CSUM_IP)
1716 cflags |= JME_TD_IPCSUM;
1717 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1718 cflags |= JME_TD_TCPCSUM;
1719 if (m->m_pkthdr.csum_flags & CSUM_UDP)
1720 cflags |= JME_TD_UDPCSUM;
1723 /* Configure VLAN. */
1724 if (m->m_flags & M_VLANTAG) {
1725 cflags |= (m->m_pkthdr.ether_vlantag & JME_TD_VLAN_MASK);
1726 cflags |= JME_TD_VLAN_TAG;
1729 desc = &tdata->jme_tx_ring[prod];
1730 desc->flags = htole32(cflags);
1731 desc->addr_hi = htole32(m->m_pkthdr.len);
1732 if (tdata->jme_sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT) {
1734 * Use 64bits TX desc chain format.
1736 * The first TX desc of the chain, which is setup here,
1737 * is just a symbol TX desc carrying no payload.
1739 flag64 = JME_TD_64BIT;
1740 desc->buflen = htole32(mss);
1745 /* No effective TX desc is consumed */
1749 * Use 32bits TX desc chain format.
1751 * The first TX desc of the chain, which is setup here,
1752 * is an effective TX desc carrying the first segment of
1756 desc->buflen = htole32(mss | txsegs[0].ds_len);
1757 desc->addr_lo = htole32(JME_ADDR_LO(txsegs[0].ds_addr));
1759 /* One effective TX desc is consumed */
1762 tdata->jme_tx_cnt++;
1763 KKASSERT(tdata->jme_tx_cnt - i < tdata->jme_tx_desc_cnt - JME_TXD_RSVD);
1764 JME_DESC_INC(prod, tdata->jme_tx_desc_cnt);
1766 txd->tx_ndesc = 1 - i;
1767 for (; i < nsegs; i++) {
1768 desc = &tdata->jme_tx_ring[prod];
1769 desc->buflen = htole32(txsegs[i].ds_len);
1770 desc->addr_hi = htole32(JME_ADDR_HI(txsegs[i].ds_addr));
1771 desc->addr_lo = htole32(JME_ADDR_LO(txsegs[i].ds_addr));
1772 desc->flags = htole32(JME_TD_OWN | flag64);
1774 tdata->jme_tx_cnt++;
1775 KKASSERT(tdata->jme_tx_cnt <=
1776 tdata->jme_tx_desc_cnt - JME_TXD_RSVD);
1777 JME_DESC_INC(prod, tdata->jme_tx_desc_cnt);
1780 /* Update producer index. */
1781 tdata->jme_tx_prod = prod;
1783 * Finally request interrupt and give the first descriptor
1784 * owenership to hardware.
1786 desc = txd->tx_desc;
1787 desc->flags |= htole32(JME_TD_OWN | JME_TD_INTR);
1790 txd->tx_ndesc += nsegs;
1800 jme_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1802 struct jme_softc *sc = ifp->if_softc;
1803 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
1804 struct mbuf *m_head;
1807 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1808 ASSERT_SERIALIZED(&tdata->jme_tx_serialize);
1810 if (!sc->jme_has_link) {
1811 ifq_purge(&ifp->if_snd);
1815 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
1818 if (tdata->jme_tx_cnt >= JME_TX_DESC_HIWAT(tdata))
1821 while (!ifq_is_empty(&ifp->if_snd)) {
1823 * Check number of available TX descs, always
1824 * leave JME_TXD_RSVD free TX descs.
1826 if (tdata->jme_tx_cnt + JME_TXD_SPARE >
1827 tdata->jme_tx_desc_cnt - JME_TXD_RSVD) {
1828 ifq_set_oactive(&ifp->if_snd);
1832 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1837 * Pack the data into the transmit ring. If we
1838 * don't have room, set the OACTIVE flag and wait
1839 * for the NIC to drain the ring.
1841 if (jme_encap(tdata, &m_head, &enq)) {
1842 KKASSERT(m_head == NULL);
1843 IFNET_STAT_INC(ifp, oerrors, 1);
1844 ifq_set_oactive(&ifp->if_snd);
1848 if (enq >= tdata->jme_tx_wreg) {
1849 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr |
1850 TXCSR_TX_ENB | TXCSR_TXQ_N_START(TXCSR_TXQ0));
1855 * If there's a BPF listener, bounce a copy of this frame
1858 ETHER_BPF_MTAP(ifp, m_head);
1860 /* Set a timeout in case the chip goes out to lunch. */
1861 ifp->if_timer = JME_TX_TIMEOUT;
1866 * Reading TXCSR takes very long time under heavy load
1867 * so cache TXCSR value and writes the ORed value with
1868 * the kick command to the TXCSR. This saves one register
1871 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr | TXCSR_TX_ENB |
1872 TXCSR_TXQ_N_START(TXCSR_TXQ0));
1877 jme_watchdog(struct ifnet *ifp)
1879 struct jme_softc *sc = ifp->if_softc;
1880 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
1882 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1884 if (!sc->jme_has_link) {
1885 if_printf(ifp, "watchdog timeout (missed link)\n");
1886 IFNET_STAT_INC(ifp, oerrors, 1);
1892 if (tdata->jme_tx_cnt == 0) {
1893 if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
1895 if (!ifq_is_empty(&ifp->if_snd))
1900 if_printf(ifp, "watchdog timeout\n");
1901 IFNET_STAT_INC(ifp, oerrors, 1);
1903 if (!ifq_is_empty(&ifp->if_snd))
1908 jme_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1910 struct jme_softc *sc = ifp->if_softc;
1911 struct mii_data *mii = device_get_softc(sc->jme_miibus);
1912 struct ifreq *ifr = (struct ifreq *)data;
1913 int error = 0, mask;
1915 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1919 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > JME_JUMBO_MTU ||
1920 (!(sc->jme_caps & JME_CAP_JUMBO) &&
1921 ifr->ifr_mtu > JME_MAX_MTU)) {
1926 if (ifp->if_mtu != ifr->ifr_mtu) {
1928 * No special configuration is required when interface
1929 * MTU is changed but availability of Tx checksum
1930 * offload should be chcked against new MTU size as
1931 * FIFO size is just 2K.
1933 if (ifr->ifr_mtu >= JME_TX_FIFO_SIZE) {
1934 ifp->if_capenable &=
1935 ~(IFCAP_TXCSUM | IFCAP_TSO);
1937 ~(JME_CSUM_FEATURES | CSUM_TSO);
1939 ifp->if_mtu = ifr->ifr_mtu;
1940 if (ifp->if_flags & IFF_RUNNING)
1946 if (ifp->if_flags & IFF_UP) {
1947 if (ifp->if_flags & IFF_RUNNING) {
1948 if ((ifp->if_flags ^ sc->jme_if_flags) &
1949 (IFF_PROMISC | IFF_ALLMULTI))
1955 if (ifp->if_flags & IFF_RUNNING)
1958 sc->jme_if_flags = ifp->if_flags;
1963 if (ifp->if_flags & IFF_RUNNING)
1969 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1973 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1975 if ((mask & IFCAP_TXCSUM) && ifp->if_mtu < JME_TX_FIFO_SIZE) {
1976 ifp->if_capenable ^= IFCAP_TXCSUM;
1977 if (ifp->if_capenable & IFCAP_TXCSUM)
1978 ifp->if_hwassist |= JME_CSUM_FEATURES;
1980 ifp->if_hwassist &= ~JME_CSUM_FEATURES;
1982 if (mask & IFCAP_RXCSUM) {
1985 ifp->if_capenable ^= IFCAP_RXCSUM;
1986 reg = CSR_READ_4(sc, JME_RXMAC);
1987 reg &= ~RXMAC_CSUM_ENB;
1988 if (ifp->if_capenable & IFCAP_RXCSUM)
1989 reg |= RXMAC_CSUM_ENB;
1990 CSR_WRITE_4(sc, JME_RXMAC, reg);
1993 if (mask & IFCAP_VLAN_HWTAGGING) {
1994 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1998 if ((mask & IFCAP_TSO) && ifp->if_mtu < JME_TX_FIFO_SIZE) {
1999 ifp->if_capenable ^= IFCAP_TSO;
2000 if (ifp->if_capenable & IFCAP_TSO)
2001 ifp->if_hwassist |= CSUM_TSO;
2003 ifp->if_hwassist &= ~CSUM_TSO;
2006 if (mask & IFCAP_RSS)
2007 ifp->if_capenable ^= IFCAP_RSS;
2011 error = ether_ioctl(ifp, cmd, data);
2018 jme_mac_config(struct jme_softc *sc)
2020 struct mii_data *mii;
2021 uint32_t ghc, rxmac, txmac, txpause, gp1;
2022 int phyconf = JMPHY_CONF_DEFFIFO, hdx = 0;
2024 mii = device_get_softc(sc->jme_miibus);
2026 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2028 CSR_WRITE_4(sc, JME_GHC, 0);
2030 rxmac = CSR_READ_4(sc, JME_RXMAC);
2031 rxmac &= ~RXMAC_FC_ENB;
2032 txmac = CSR_READ_4(sc, JME_TXMAC);
2033 txmac &= ~(TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST);
2034 txpause = CSR_READ_4(sc, JME_TXPFC);
2035 txpause &= ~TXPFC_PAUSE_ENB;
2036 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
2037 ghc |= GHC_FULL_DUPLEX;
2038 rxmac &= ~RXMAC_COLL_DET_ENB;
2039 txmac &= ~(TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE |
2040 TXMAC_BACKOFF | TXMAC_CARRIER_EXT |
2043 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
2044 txpause |= TXPFC_PAUSE_ENB;
2045 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
2046 rxmac |= RXMAC_FC_ENB;
2048 /* Disable retry transmit timer/retry limit. */
2049 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) &
2050 ~(TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB));
2052 rxmac |= RXMAC_COLL_DET_ENB;
2053 txmac |= TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE | TXMAC_BACKOFF;
2054 /* Enable retry transmit timer/retry limit. */
2055 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) |
2056 TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB);
2060 * Reprogram Tx/Rx MACs with resolved speed/duplex.
2062 gp1 = CSR_READ_4(sc, JME_GPREG1);
2063 gp1 &= ~GPREG1_WA_HDX;
2065 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) == 0)
2068 switch (IFM_SUBTYPE(mii->mii_media_active)) {
2070 ghc |= GHC_SPEED_10 | sc->jme_clksrc;
2072 gp1 |= GPREG1_WA_HDX;
2076 ghc |= GHC_SPEED_100 | sc->jme_clksrc;
2078 gp1 |= GPREG1_WA_HDX;
2081 * Use extended FIFO depth to workaround CRC errors
2082 * emitted by chips before JMC250B
2084 phyconf = JMPHY_CONF_EXTFIFO;
2088 if (sc->jme_caps & JME_CAP_FASTETH)
2091 ghc |= GHC_SPEED_1000 | sc->jme_clksrc_1000;
2093 txmac |= TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST;
2099 CSR_WRITE_4(sc, JME_GHC, ghc);
2100 CSR_WRITE_4(sc, JME_RXMAC, rxmac);
2101 CSR_WRITE_4(sc, JME_TXMAC, txmac);
2102 CSR_WRITE_4(sc, JME_TXPFC, txpause);
2104 if (sc->jme_workaround & JME_WA_EXTFIFO) {
2105 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
2106 JMPHY_CONF, phyconf);
2108 if (sc->jme_workaround & JME_WA_HDX)
2109 CSR_WRITE_4(sc, JME_GPREG1, gp1);
2115 struct jme_softc *sc = xsc;
2116 struct ifnet *ifp = &sc->arpcom.ac_if;
2120 ASSERT_SERIALIZED(&sc->jme_serialize);
2122 status = CSR_READ_4(sc, JME_INTR_REQ_STATUS);
2123 if (status == 0 || status == 0xFFFFFFFF)
2126 /* Disable interrupts. */
2127 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
2129 status = CSR_READ_4(sc, JME_INTR_STATUS);
2130 if ((status & JME_INTRS) == 0 || status == 0xFFFFFFFF)
2133 /* Reset PCC counter/timer and Ack interrupts. */
2134 status &= ~(INTR_TXQ_COMP | INTR_RXQ_COMP);
2136 if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO))
2137 status |= INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP;
2139 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2140 if (status & jme_rx_status[r].jme_coal) {
2141 status |= jme_rx_status[r].jme_coal |
2142 jme_rx_status[r].jme_comp;
2146 CSR_WRITE_4(sc, JME_INTR_STATUS, status);
2148 if (ifp->if_flags & IFF_RUNNING) {
2149 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
2151 if (status & (INTR_RXQ_COAL | INTR_RXQ_COAL_TO))
2152 jme_rx_intr(sc, status);
2154 if (status & INTR_RXQ_DESC_EMPTY) {
2156 * Notify hardware availability of new Rx buffers.
2157 * Reading RXCSR takes very long time under heavy
2158 * load so cache RXCSR value and writes the ORed
2159 * value with the kick command to the RXCSR. This
2160 * saves one register access cycle.
2162 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr |
2163 RXCSR_RX_ENB | RXCSR_RXQ_START);
2166 if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO)) {
2167 lwkt_serialize_enter(&tdata->jme_tx_serialize);
2169 if (!ifq_is_empty(&ifp->if_snd))
2171 lwkt_serialize_exit(&tdata->jme_tx_serialize);
2175 /* Reenable interrupts. */
2176 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
2180 jme_txeof(struct jme_txdata *tdata)
2182 struct ifnet *ifp = &tdata->jme_sc->arpcom.ac_if;
2185 cons = tdata->jme_tx_cons;
2186 if (cons == tdata->jme_tx_prod)
2190 * Go through our Tx list and free mbufs for those
2191 * frames which have been transmitted.
2193 while (cons != tdata->jme_tx_prod) {
2194 struct jme_txdesc *txd, *next_txd;
2195 uint32_t status, next_status;
2196 int next_cons, nsegs;
2198 txd = &tdata->jme_txdesc[cons];
2199 KASSERT(txd->tx_m != NULL,
2200 ("%s: freeing NULL mbuf!", __func__));
2202 status = le32toh(txd->tx_desc->flags);
2203 if ((status & JME_TD_OWN) == JME_TD_OWN)
2208 * This chip will always update the TX descriptor's
2209 * buflen field and this updating always happens
2210 * after clearing the OWN bit, so even if the OWN
2211 * bit is cleared by the chip, we still don't sure
2212 * about whether the buflen field has been updated
2213 * by the chip or not. To avoid this race, we wait
2214 * for the next TX descriptor's OWN bit to be cleared
2215 * by the chip before reusing this TX descriptor.
2218 JME_DESC_ADD(next_cons, txd->tx_ndesc, tdata->jme_tx_desc_cnt);
2219 next_txd = &tdata->jme_txdesc[next_cons];
2220 if (next_txd->tx_m == NULL)
2222 next_status = le32toh(next_txd->tx_desc->flags);
2223 if ((next_status & JME_TD_OWN) == JME_TD_OWN)
2226 if (status & (JME_TD_TMOUT | JME_TD_RETRY_EXP)) {
2227 IFNET_STAT_INC(ifp, oerrors, 1);
2229 IFNET_STAT_INC(ifp, opackets, 1);
2230 if (status & JME_TD_COLLISION) {
2231 IFNET_STAT_INC(ifp, collisions,
2232 le32toh(txd->tx_desc->buflen) &
2233 JME_TD_BUF_LEN_MASK);
2238 * Only the first descriptor of multi-descriptor
2239 * transmission is updated so driver have to skip entire
2240 * chained buffers for the transmiited frame. In other
2241 * words, JME_TD_OWN bit is valid only at the first
2242 * descriptor of a multi-descriptor transmission.
2244 for (nsegs = 0; nsegs < txd->tx_ndesc; nsegs++) {
2245 tdata->jme_tx_ring[cons].flags = 0;
2246 JME_DESC_INC(cons, tdata->jme_tx_desc_cnt);
2249 /* Reclaim transferred mbufs. */
2250 bus_dmamap_unload(tdata->jme_tx_tag, txd->tx_dmamap);
2253 tdata->jme_tx_cnt -= txd->tx_ndesc;
2254 KASSERT(tdata->jme_tx_cnt >= 0,
2255 ("%s: Active Tx desc counter was garbled", __func__));
2258 tdata->jme_tx_cons = cons;
2260 /* 1 for symbol TX descriptor */
2261 if (tdata->jme_tx_cnt <= JME_MAXTXSEGS + 1)
2264 if (tdata->jme_tx_cnt + JME_TXD_SPARE <=
2265 tdata->jme_tx_desc_cnt - JME_TXD_RSVD)
2266 ifq_clr_oactive(&ifp->if_snd);
2269 static __inline void
2270 jme_discard_rxbufs(struct jme_rxdata *rdata, int cons, int count)
2274 for (i = 0; i < count; ++i) {
2275 jme_setup_rxdesc(&rdata->jme_rxdesc[cons]);
2276 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt);
2280 static __inline struct pktinfo *
2281 jme_pktinfo(struct pktinfo *pi, uint32_t flags)
2283 if (flags & JME_RD_IPV4)
2284 pi->pi_netisr = NETISR_IP;
2285 else if (flags & JME_RD_IPV6)
2286 pi->pi_netisr = NETISR_IPV6;
2291 pi->pi_l3proto = IPPROTO_UNKNOWN;
2293 if (flags & JME_RD_MORE_FRAG)
2294 pi->pi_flags |= PKTINFO_FLAG_FRAG;
2295 else if (flags & JME_RD_TCP)
2296 pi->pi_l3proto = IPPROTO_TCP;
2297 else if (flags & JME_RD_UDP)
2298 pi->pi_l3proto = IPPROTO_UDP;
2304 /* Receive a frame. */
2306 jme_rxpkt(struct jme_rxdata *rdata)
2308 struct ifnet *ifp = &rdata->jme_sc->arpcom.ac_if;
2309 struct jme_desc *desc;
2310 struct jme_rxdesc *rxd;
2311 struct mbuf *mp, *m;
2312 uint32_t flags, status, hash, hashinfo;
2313 int cons, count, nsegs;
2315 cons = rdata->jme_rx_cons;
2316 desc = &rdata->jme_rx_ring[cons];
2318 flags = le32toh(desc->flags);
2319 status = le32toh(desc->buflen);
2320 hash = le32toh(desc->addr_hi);
2321 hashinfo = le32toh(desc->addr_lo);
2322 nsegs = JME_RX_NSEGS(status);
2325 /* Skip the first descriptor. */
2326 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt);
2329 * Clear the OWN bit of the following RX descriptors;
2330 * hardware will not clear the OWN bit except the first
2333 * Since the first RX descriptor is setup, i.e. OWN bit
2334 * on, before its followins RX descriptors, leaving the
2335 * OWN bit on the following RX descriptors will trick
2336 * the hardware into thinking that the following RX
2337 * descriptors are ready to be used too.
2339 for (count = 1; count < nsegs; count++,
2340 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt))
2341 rdata->jme_rx_ring[cons].flags = 0;
2343 cons = rdata->jme_rx_cons;
2346 JME_RSS_DPRINTF(rdata->jme_sc, 15, "ring%d, flags 0x%08x, "
2347 "hash 0x%08x, hash info 0x%08x\n",
2348 rdata->jme_rx_idx, flags, hash, hashinfo);
2350 if (status & JME_RX_ERR_STAT) {
2351 IFNET_STAT_INC(ifp, ierrors, 1);
2352 jme_discard_rxbufs(rdata, cons, nsegs);
2353 #ifdef JME_SHOW_ERRORS
2354 if_printf(ifp, "%s : receive error = 0x%b\n",
2355 __func__, JME_RX_ERR(status), JME_RX_ERR_BITS);
2357 rdata->jme_rx_cons += nsegs;
2358 rdata->jme_rx_cons %= rdata->jme_rx_desc_cnt;
2362 rdata->jme_rxlen = JME_RX_BYTES(status) - JME_RX_PAD_BYTES;
2363 for (count = 0; count < nsegs; count++,
2364 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt)) {
2365 rxd = &rdata->jme_rxdesc[cons];
2368 /* Add a new receive buffer to the ring. */
2369 if (jme_newbuf(rdata, rxd, 0) != 0) {
2370 IFNET_STAT_INC(ifp, iqdrops, 1);
2372 jme_discard_rxbufs(rdata, cons, nsegs - count);
2373 if (rdata->jme_rxhead != NULL) {
2374 m_freem(rdata->jme_rxhead);
2375 JME_RXCHAIN_RESET(rdata);
2381 * Assume we've received a full sized frame.
2382 * Actual size is fixed when we encounter the end of
2383 * multi-segmented frame.
2385 mp->m_len = MCLBYTES;
2387 /* Chain received mbufs. */
2388 if (rdata->jme_rxhead == NULL) {
2389 rdata->jme_rxhead = mp;
2390 rdata->jme_rxtail = mp;
2393 * Receive processor can receive a maximum frame
2394 * size of 65535 bytes.
2396 rdata->jme_rxtail->m_next = mp;
2397 rdata->jme_rxtail = mp;
2400 if (count == nsegs - 1) {
2401 struct pktinfo pi0, *pi;
2403 /* Last desc. for this frame. */
2404 m = rdata->jme_rxhead;
2405 m->m_pkthdr.len = rdata->jme_rxlen;
2407 /* Set first mbuf size. */
2408 m->m_len = MCLBYTES - JME_RX_PAD_BYTES;
2409 /* Set last mbuf size. */
2410 mp->m_len = rdata->jme_rxlen -
2411 ((MCLBYTES - JME_RX_PAD_BYTES) +
2412 (MCLBYTES * (nsegs - 2)));
2414 m->m_len = rdata->jme_rxlen;
2416 m->m_pkthdr.rcvif = ifp;
2419 * Account for 10bytes auto padding which is used
2420 * to align IP header on 32bit boundary. Also note,
2421 * CRC bytes is automatically removed by the
2424 m->m_data += JME_RX_PAD_BYTES;
2426 /* Set checksum information. */
2427 if ((ifp->if_capenable & IFCAP_RXCSUM) &&
2428 (flags & JME_RD_IPV4)) {
2429 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2430 if (flags & JME_RD_IPCSUM)
2431 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2432 if ((flags & JME_RD_MORE_FRAG) == 0 &&
2433 ((flags & (JME_RD_TCP | JME_RD_TCPCSUM)) ==
2434 (JME_RD_TCP | JME_RD_TCPCSUM) ||
2435 (flags & (JME_RD_UDP | JME_RD_UDPCSUM)) ==
2436 (JME_RD_UDP | JME_RD_UDPCSUM))) {
2437 m->m_pkthdr.csum_flags |=
2438 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2439 m->m_pkthdr.csum_data = 0xffff;
2443 /* Check for VLAN tagged packets. */
2444 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) &&
2445 (flags & JME_RD_VLAN_TAG)) {
2446 m->m_pkthdr.ether_vlantag =
2447 flags & JME_RD_VLAN_MASK;
2448 m->m_flags |= M_VLANTAG;
2451 IFNET_STAT_INC(ifp, ipackets, 1);
2453 if (ifp->if_capenable & IFCAP_RSS)
2454 pi = jme_pktinfo(&pi0, flags);
2459 (hashinfo & JME_RD_HASH_FN_MASK) ==
2460 JME_RD_HASH_FN_TOEPLITZ) {
2461 m->m_flags |= (M_HASH | M_CKHASH);
2462 m->m_pkthdr.hash = toeplitz_hash(hash);
2465 #ifdef JME_RSS_DEBUG
2467 JME_RSS_DPRINTF(rdata->jme_sc, 10,
2468 "isr %d flags %08x, l3 %d %s\n",
2469 pi->pi_netisr, pi->pi_flags,
2471 (m->m_flags & M_HASH) ? "hash" : "");
2476 ether_input_pkt(ifp, m, pi);
2478 /* Reset mbuf chains. */
2479 JME_RXCHAIN_RESET(rdata);
2480 #ifdef JME_RSS_DEBUG
2481 rdata->jme_rx_pkt++;
2486 rdata->jme_rx_cons += nsegs;
2487 rdata->jme_rx_cons %= rdata->jme_rx_desc_cnt;
2491 jme_rxeof(struct jme_rxdata *rdata, int count)
2493 struct jme_desc *desc;
2497 #ifdef IFPOLL_ENABLE
2498 if (count >= 0 && count-- == 0)
2501 desc = &rdata->jme_rx_ring[rdata->jme_rx_cons];
2502 if ((le32toh(desc->flags) & JME_RD_OWN) == JME_RD_OWN)
2504 if ((le32toh(desc->buflen) & JME_RD_VALID) == 0)
2508 * Check number of segments against received bytes.
2509 * Non-matching value would indicate that hardware
2510 * is still trying to update Rx descriptors. I'm not
2511 * sure whether this check is needed.
2513 nsegs = JME_RX_NSEGS(le32toh(desc->buflen));
2514 pktlen = JME_RX_BYTES(le32toh(desc->buflen));
2515 if (nsegs != howmany(pktlen, MCLBYTES)) {
2516 if_printf(&rdata->jme_sc->arpcom.ac_if,
2517 "RX fragment count(%d) and "
2518 "packet size(%d) mismach\n", nsegs, pktlen);
2524 * RSS hash and hash information may _not_ be set by the
2525 * hardware even if the OWN bit is cleared and VALID bit
2528 * If the RSS information is not delivered by the hardware
2529 * yet, we MUST NOT accept this packet, let alone reusing
2530 * its RX descriptor. If this packet was accepted and its
2531 * RX descriptor was reused before hardware delivering the
2532 * RSS information, the RX buffer's address would be trashed
2533 * by the RSS information delivered by the hardware.
2535 if (JME_ENABLE_HWRSS(rdata->jme_sc)) {
2536 struct jme_rxdesc *rxd;
2539 hashinfo = le32toh(desc->addr_lo);
2540 rxd = &rdata->jme_rxdesc[rdata->jme_rx_cons];
2543 * This test should be enough to detect the pending
2544 * RSS information delivery, given:
2545 * - If RSS hash is not calculated, the hashinfo
2546 * will be 0. Howvever, the lower 32bits of RX
2547 * buffers' physical address will never be 0.
2548 * (see jme_rxbuf_dma_filter)
2549 * - If RSS hash is calculated, the lowest 4 bits
2550 * of hashinfo will be set, while the RX buffers
2551 * are at least 2K aligned.
2553 if (hashinfo == JME_ADDR_LO(rxd->rx_paddr)) {
2554 #ifdef JME_SHOW_RSSWB
2555 if_printf(&rdata->jme_sc->arpcom.ac_if,
2556 "RSS is not written back yet\n");
2562 /* Received a frame. */
2570 struct jme_softc *sc = xsc;
2571 struct mii_data *mii = device_get_softc(sc->jme_miibus);
2573 lwkt_serialize_enter(&sc->jme_serialize);
2575 KKASSERT(mycpuid == JME_TICK_CPUID);
2577 sc->jme_in_tick = TRUE;
2579 sc->jme_in_tick = FALSE;
2581 callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
2583 lwkt_serialize_exit(&sc->jme_serialize);
2587 jme_reset(struct jme_softc *sc)
2591 /* Make sure that TX and RX are stopped */
2596 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2600 * Hold reset bit before stop reset
2603 /* Disable TXMAC and TXOFL clock sources */
2604 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2605 /* Disable RXMAC clock source */
2606 val = CSR_READ_4(sc, JME_GPREG1);
2607 CSR_WRITE_4(sc, JME_GPREG1, val | GPREG1_DIS_RXMAC_CLKSRC);
2609 CSR_READ_4(sc, JME_GHC);
2612 CSR_WRITE_4(sc, JME_GHC, 0);
2614 CSR_READ_4(sc, JME_GHC);
2617 * Clear reset bit after stop reset
2620 /* Enable TXMAC and TXOFL clock sources */
2621 CSR_WRITE_4(sc, JME_GHC, GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC);
2622 /* Enable RXMAC clock source */
2623 val = CSR_READ_4(sc, JME_GPREG1);
2624 CSR_WRITE_4(sc, JME_GPREG1, val & ~GPREG1_DIS_RXMAC_CLKSRC);
2626 CSR_READ_4(sc, JME_GHC);
2628 /* Disable TXMAC and TXOFL clock sources */
2629 CSR_WRITE_4(sc, JME_GHC, 0);
2630 /* Disable RXMAC clock source */
2631 val = CSR_READ_4(sc, JME_GPREG1);
2632 CSR_WRITE_4(sc, JME_GPREG1, val | GPREG1_DIS_RXMAC_CLKSRC);
2634 CSR_READ_4(sc, JME_GHC);
2636 /* Enable TX and RX */
2637 val = CSR_READ_4(sc, JME_TXCSR);
2638 CSR_WRITE_4(sc, JME_TXCSR, val | TXCSR_TX_ENB);
2639 val = CSR_READ_4(sc, JME_RXCSR);
2640 CSR_WRITE_4(sc, JME_RXCSR, val | RXCSR_RX_ENB);
2642 CSR_READ_4(sc, JME_TXCSR);
2643 CSR_READ_4(sc, JME_RXCSR);
2645 /* Enable TXMAC and TXOFL clock sources */
2646 CSR_WRITE_4(sc, JME_GHC, GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC);
2647 /* Eisable RXMAC clock source */
2648 val = CSR_READ_4(sc, JME_GPREG1);
2649 CSR_WRITE_4(sc, JME_GPREG1, val & ~GPREG1_DIS_RXMAC_CLKSRC);
2651 CSR_READ_4(sc, JME_GHC);
2653 /* Stop TX and RX */
2661 struct jme_softc *sc = xsc;
2662 struct ifnet *ifp = &sc->arpcom.ac_if;
2663 struct mii_data *mii;
2664 uint8_t eaddr[ETHER_ADDR_LEN];
2669 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2672 * Cancel any pending I/O.
2677 * Reset the chip to a known state.
2682 * Setup MSI/MSI-X vectors to interrupts mapping
2686 if (JME_ENABLE_HWRSS(sc))
2689 jme_disable_rss(sc);
2691 /* Init RX descriptors */
2692 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2693 error = jme_init_rx_ring(&sc->jme_cdata.jme_rx_data[r]);
2695 if_printf(ifp, "initialization failed: "
2696 "no memory for %dth RX ring.\n", r);
2702 /* Init TX descriptors */
2703 jme_init_tx_ring(&sc->jme_cdata.jme_tx_data);
2705 /* Initialize shadow status block. */
2708 /* Reprogram the station address. */
2709 bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
2710 CSR_WRITE_4(sc, JME_PAR0,
2711 eaddr[3] << 24 | eaddr[2] << 16 | eaddr[1] << 8 | eaddr[0]);
2712 CSR_WRITE_4(sc, JME_PAR1, eaddr[5] << 8 | eaddr[4]);
2715 * Configure Tx queue.
2716 * Tx priority queue weight value : 0
2717 * Tx FIFO threshold for processing next packet : 16QW
2718 * Maximum Tx DMA length : 512
2719 * Allow Tx DMA burst.
2721 sc->jme_txcsr = TXCSR_TXQ_N_SEL(TXCSR_TXQ0);
2722 sc->jme_txcsr |= TXCSR_TXQ_WEIGHT(TXCSR_TXQ_WEIGHT_MIN);
2723 sc->jme_txcsr |= TXCSR_FIFO_THRESH_16QW;
2724 sc->jme_txcsr |= sc->jme_tx_dma_size;
2725 sc->jme_txcsr |= TXCSR_DMA_BURST;
2726 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr);
2728 /* Set Tx descriptor counter. */
2729 CSR_WRITE_4(sc, JME_TXQDC, sc->jme_cdata.jme_tx_data.jme_tx_desc_cnt);
2731 /* Set Tx ring address to the hardware. */
2732 paddr = sc->jme_cdata.jme_tx_data.jme_tx_ring_paddr;
2733 CSR_WRITE_4(sc, JME_TXDBA_HI, JME_ADDR_HI(paddr));
2734 CSR_WRITE_4(sc, JME_TXDBA_LO, JME_ADDR_LO(paddr));
2736 /* Configure TxMAC parameters. */
2737 reg = TXMAC_IFG1_DEFAULT | TXMAC_IFG2_DEFAULT | TXMAC_IFG_ENB;
2738 reg |= TXMAC_THRESH_1_PKT;
2739 reg |= TXMAC_CRC_ENB | TXMAC_PAD_ENB;
2740 CSR_WRITE_4(sc, JME_TXMAC, reg);
2743 * Configure Rx queue.
2744 * FIFO full threshold for transmitting Tx pause packet : 128T
2745 * FIFO threshold for processing next packet : 128QW
2747 * Max Rx DMA length : 128
2748 * Rx descriptor retry : 32
2749 * Rx descriptor retry time gap : 256ns
2750 * Don't receive runt/bad frame.
2752 sc->jme_rxcsr = RXCSR_FIFO_FTHRESH_128T;
2755 * Since Rx FIFO size is 4K bytes, receiving frames larger
2756 * than 4K bytes will suffer from Rx FIFO overruns. So
2757 * decrease FIFO threshold to reduce the FIFO overruns for
2758 * frames larger than 4000 bytes.
2759 * For best performance of standard MTU sized frames use
2760 * maximum allowable FIFO threshold, 128QW.
2762 if ((ifp->if_mtu + ETHER_HDR_LEN + EVL_ENCAPLEN + ETHER_CRC_LEN) >
2764 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
2766 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_128QW;
2768 /* Improve PCI Express compatibility */
2769 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
2771 sc->jme_rxcsr |= sc->jme_rx_dma_size;
2772 sc->jme_rxcsr |= RXCSR_DESC_RT_CNT(RXCSR_DESC_RT_CNT_DEFAULT);
2773 sc->jme_rxcsr |= RXCSR_DESC_RT_GAP_256 & RXCSR_DESC_RT_GAP_MASK;
2774 /* XXX TODO DROP_BAD */
2776 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2777 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
2779 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RXQ_N_SEL(r));
2781 /* Set Rx descriptor counter. */
2782 CSR_WRITE_4(sc, JME_RXQDC, rdata->jme_rx_desc_cnt);
2784 /* Set Rx ring address to the hardware. */
2785 paddr = rdata->jme_rx_ring_paddr;
2786 CSR_WRITE_4(sc, JME_RXDBA_HI, JME_ADDR_HI(paddr));
2787 CSR_WRITE_4(sc, JME_RXDBA_LO, JME_ADDR_LO(paddr));
2790 /* Clear receive filter. */
2791 CSR_WRITE_4(sc, JME_RXMAC, 0);
2793 /* Set up the receive filter. */
2798 * Disable all WOL bits as WOL can interfere normal Rx
2799 * operation. Also clear WOL detection status bits.
2801 reg = CSR_READ_4(sc, JME_PMCS);
2802 reg &= ~PMCS_WOL_ENB_MASK;
2803 CSR_WRITE_4(sc, JME_PMCS, reg);
2806 * Pad 10bytes right before received frame. This will greatly
2807 * help Rx performance on strict-alignment architectures as
2808 * it does not need to copy the frame to align the payload.
2810 reg = CSR_READ_4(sc, JME_RXMAC);
2811 reg |= RXMAC_PAD_10BYTES;
2813 if (ifp->if_capenable & IFCAP_RXCSUM)
2814 reg |= RXMAC_CSUM_ENB;
2815 CSR_WRITE_4(sc, JME_RXMAC, reg);
2817 /* Configure general purpose reg0 */
2818 reg = CSR_READ_4(sc, JME_GPREG0);
2819 reg &= ~GPREG0_PCC_UNIT_MASK;
2820 /* Set PCC timer resolution to micro-seconds unit. */
2821 reg |= GPREG0_PCC_UNIT_US;
2823 * Disable all shadow register posting as we have to read
2824 * JME_INTR_STATUS register in jme_intr. Also it seems
2825 * that it's hard to synchronize interrupt status between
2826 * hardware and software with shadow posting due to
2827 * requirements of bus_dmamap_sync(9).
2829 reg |= GPREG0_SH_POST_DW7_DIS | GPREG0_SH_POST_DW6_DIS |
2830 GPREG0_SH_POST_DW5_DIS | GPREG0_SH_POST_DW4_DIS |
2831 GPREG0_SH_POST_DW3_DIS | GPREG0_SH_POST_DW2_DIS |
2832 GPREG0_SH_POST_DW1_DIS | GPREG0_SH_POST_DW0_DIS;
2833 /* Disable posting of DW0. */
2834 reg &= ~GPREG0_POST_DW0_ENB;
2835 /* Clear PME message. */
2836 reg &= ~GPREG0_PME_ENB;
2837 /* Set PHY address. */
2838 reg &= ~GPREG0_PHY_ADDR_MASK;
2839 reg |= sc->jme_phyaddr;
2840 CSR_WRITE_4(sc, JME_GPREG0, reg);
2842 /* Configure Tx queue 0 packet completion coalescing. */
2843 jme_set_tx_coal(sc);
2845 /* Configure Rx queues packet completion coalescing. */
2846 jme_set_rx_coal(sc);
2848 /* Configure shadow status block but don't enable posting. */
2849 paddr = sc->jme_cdata.jme_ssb_block_paddr;
2850 CSR_WRITE_4(sc, JME_SHBASE_ADDR_HI, JME_ADDR_HI(paddr));
2851 CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO, JME_ADDR_LO(paddr));
2853 /* Disable Timer 1 and Timer 2. */
2854 CSR_WRITE_4(sc, JME_TIMER1, 0);
2855 CSR_WRITE_4(sc, JME_TIMER2, 0);
2857 /* Configure retry transmit period, retry limit value. */
2858 CSR_WRITE_4(sc, JME_TXTRHD,
2859 ((TXTRHD_RT_PERIOD_DEFAULT << TXTRHD_RT_PERIOD_SHIFT) &
2860 TXTRHD_RT_PERIOD_MASK) |
2861 ((TXTRHD_RT_LIMIT_DEFAULT << TXTRHD_RT_LIMIT_SHIFT) &
2862 TXTRHD_RT_LIMIT_SHIFT));
2864 #ifdef IFPOLL_ENABLE
2865 if (!(ifp->if_flags & IFF_NPOLLING))
2867 /* Initialize the interrupt mask. */
2868 jme_enable_intr(sc);
2869 CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2872 * Enabling Tx/Rx DMA engines and Rx queue processing is
2873 * done after detection of valid link in jme_miibus_statchg.
2875 sc->jme_has_link = FALSE;
2877 /* Set the current media. */
2878 mii = device_get_softc(sc->jme_miibus);
2881 callout_reset_bycpu(&sc->jme_tick_ch, hz, jme_tick, sc,
2884 ifp->if_flags |= IFF_RUNNING;
2885 ifq_clr_oactive(&ifp->if_snd);
2889 jme_stop(struct jme_softc *sc)
2891 struct ifnet *ifp = &sc->arpcom.ac_if;
2892 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
2893 struct jme_txdesc *txd;
2894 struct jme_rxdesc *rxd;
2895 struct jme_rxdata *rdata;
2898 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2901 * Mark the interface down and cancel the watchdog timer.
2903 ifp->if_flags &= ~IFF_RUNNING;
2904 ifq_clr_oactive(&ifp->if_snd);
2907 callout_stop(&sc->jme_tick_ch);
2908 sc->jme_has_link = FALSE;
2911 * Disable interrupts.
2913 jme_disable_intr(sc);
2914 CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2916 /* Disable updating shadow status block. */
2917 CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO,
2918 CSR_READ_4(sc, JME_SHBASE_ADDR_LO) & ~SHBASE_POST_ENB);
2920 /* Stop receiver, transmitter. */
2925 * Free partial finished RX segments
2927 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2928 rdata = &sc->jme_cdata.jme_rx_data[r];
2929 if (rdata->jme_rxhead != NULL)
2930 m_freem(rdata->jme_rxhead);
2931 JME_RXCHAIN_RESET(rdata);
2935 * Free RX and TX mbufs still in the queues.
2937 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2938 rdata = &sc->jme_cdata.jme_rx_data[r];
2939 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
2940 rxd = &rdata->jme_rxdesc[i];
2941 if (rxd->rx_m != NULL) {
2942 bus_dmamap_unload(rdata->jme_rx_tag,
2949 for (i = 0; i < tdata->jme_tx_desc_cnt; i++) {
2950 txd = &tdata->jme_txdesc[i];
2951 if (txd->tx_m != NULL) {
2952 bus_dmamap_unload(tdata->jme_tx_tag, txd->tx_dmamap);
2961 jme_stop_tx(struct jme_softc *sc)
2966 reg = CSR_READ_4(sc, JME_TXCSR);
2967 if ((reg & TXCSR_TX_ENB) == 0)
2969 reg &= ~TXCSR_TX_ENB;
2970 CSR_WRITE_4(sc, JME_TXCSR, reg);
2971 for (i = JME_TIMEOUT; i > 0; i--) {
2973 if ((CSR_READ_4(sc, JME_TXCSR) & TXCSR_TX_ENB) == 0)
2977 device_printf(sc->jme_dev, "stopping transmitter timeout!\n");
2981 jme_stop_rx(struct jme_softc *sc)
2986 reg = CSR_READ_4(sc, JME_RXCSR);
2987 if ((reg & RXCSR_RX_ENB) == 0)
2989 reg &= ~RXCSR_RX_ENB;
2990 CSR_WRITE_4(sc, JME_RXCSR, reg);
2991 for (i = JME_TIMEOUT; i > 0; i--) {
2993 if ((CSR_READ_4(sc, JME_RXCSR) & RXCSR_RX_ENB) == 0)
2997 device_printf(sc->jme_dev, "stopping recevier timeout!\n");
3001 jme_init_tx_ring(struct jme_txdata *tdata)
3003 struct jme_txdesc *txd;
3006 tdata->jme_tx_prod = 0;
3007 tdata->jme_tx_cons = 0;
3008 tdata->jme_tx_cnt = 0;
3010 bzero(tdata->jme_tx_ring, JME_TX_RING_SIZE(tdata));
3011 for (i = 0; i < tdata->jme_tx_desc_cnt; i++) {
3012 txd = &tdata->jme_txdesc[i];
3014 txd->tx_desc = &tdata->jme_tx_ring[i];
3020 jme_init_ssb(struct jme_softc *sc)
3022 struct jme_chain_data *cd;
3024 cd = &sc->jme_cdata;
3025 bzero(cd->jme_ssb_block, JME_SSB_SIZE);
3029 jme_init_rx_ring(struct jme_rxdata *rdata)
3031 struct jme_rxdesc *rxd;
3034 KKASSERT(rdata->jme_rxhead == NULL &&
3035 rdata->jme_rxtail == NULL &&
3036 rdata->jme_rxlen == 0);
3037 rdata->jme_rx_cons = 0;
3039 bzero(rdata->jme_rx_ring, JME_RX_RING_SIZE(rdata));
3040 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
3043 rxd = &rdata->jme_rxdesc[i];
3045 rxd->rx_desc = &rdata->jme_rx_ring[i];
3046 error = jme_newbuf(rdata, rxd, 1);
3054 jme_newbuf(struct jme_rxdata *rdata, struct jme_rxdesc *rxd, int init)
3057 bus_dma_segment_t segs;
3061 m = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
3065 * JMC250 has 64bit boundary alignment limitation so jme(4)
3066 * takes advantage of 10 bytes padding feature of hardware
3067 * in order not to copy entire frame to align IP header on
3070 m->m_len = m->m_pkthdr.len = MCLBYTES;
3072 error = bus_dmamap_load_mbuf_segment(rdata->jme_rx_tag,
3073 rdata->jme_rx_sparemap, m, &segs, 1, &nsegs,
3078 if_printf(&rdata->jme_sc->arpcom.ac_if,
3079 "can't load RX mbuf\n");
3084 if (rxd->rx_m != NULL) {
3085 bus_dmamap_sync(rdata->jme_rx_tag, rxd->rx_dmamap,
3086 BUS_DMASYNC_POSTREAD);
3087 bus_dmamap_unload(rdata->jme_rx_tag, rxd->rx_dmamap);
3089 map = rxd->rx_dmamap;
3090 rxd->rx_dmamap = rdata->jme_rx_sparemap;
3091 rdata->jme_rx_sparemap = map;
3093 rxd->rx_paddr = segs.ds_addr;
3095 jme_setup_rxdesc(rxd);
3100 jme_set_vlan(struct jme_softc *sc)
3102 struct ifnet *ifp = &sc->arpcom.ac_if;
3105 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3107 reg = CSR_READ_4(sc, JME_RXMAC);
3108 reg &= ~RXMAC_VLAN_ENB;
3109 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
3110 reg |= RXMAC_VLAN_ENB;
3111 CSR_WRITE_4(sc, JME_RXMAC, reg);
3115 jme_set_filter(struct jme_softc *sc)
3117 struct ifnet *ifp = &sc->arpcom.ac_if;
3118 struct ifmultiaddr *ifma;
3123 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3125 rxcfg = CSR_READ_4(sc, JME_RXMAC);
3126 rxcfg &= ~(RXMAC_BROADCAST | RXMAC_PROMISC | RXMAC_MULTICAST |
3130 * Always accept frames destined to our station address.
3131 * Always accept broadcast frames.
3133 rxcfg |= RXMAC_UNICAST | RXMAC_BROADCAST;
3135 if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
3136 if (ifp->if_flags & IFF_PROMISC)
3137 rxcfg |= RXMAC_PROMISC;
3138 if (ifp->if_flags & IFF_ALLMULTI)
3139 rxcfg |= RXMAC_ALLMULTI;
3140 CSR_WRITE_4(sc, JME_MAR0, 0xFFFFFFFF);
3141 CSR_WRITE_4(sc, JME_MAR1, 0xFFFFFFFF);
3142 CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
3147 * Set up the multicast address filter by passing all multicast
3148 * addresses through a CRC generator, and then using the low-order
3149 * 6 bits as an index into the 64 bit multicast hash table. The
3150 * high order bits select the register, while the rest of the bits
3151 * select the bit within the register.
3153 rxcfg |= RXMAC_MULTICAST;
3154 bzero(mchash, sizeof(mchash));
3156 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3157 if (ifma->ifma_addr->sa_family != AF_LINK)
3159 crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
3160 ifma->ifma_addr), ETHER_ADDR_LEN);
3162 /* Just want the 6 least significant bits. */
3165 /* Set the corresponding bit in the hash table. */
3166 mchash[crc >> 5] |= 1 << (crc & 0x1f);
3169 CSR_WRITE_4(sc, JME_MAR0, mchash[0]);
3170 CSR_WRITE_4(sc, JME_MAR1, mchash[1]);
3171 CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
3175 jme_sysctl_tx_coal_to(SYSCTL_HANDLER_ARGS)
3177 struct jme_softc *sc = arg1;
3178 struct ifnet *ifp = &sc->arpcom.ac_if;
3181 ifnet_serialize_all(ifp);
3183 v = sc->jme_tx_coal_to;
3184 error = sysctl_handle_int(oidp, &v, 0, req);
3185 if (error || req->newptr == NULL)
3188 if (v < PCCTX_COAL_TO_MIN || v > PCCTX_COAL_TO_MAX) {
3193 if (v != sc->jme_tx_coal_to) {
3194 sc->jme_tx_coal_to = v;
3195 if (ifp->if_flags & IFF_RUNNING)
3196 jme_set_tx_coal(sc);
3199 ifnet_deserialize_all(ifp);
3204 jme_sysctl_tx_coal_pkt(SYSCTL_HANDLER_ARGS)
3206 struct jme_softc *sc = arg1;
3207 struct ifnet *ifp = &sc->arpcom.ac_if;
3210 ifnet_serialize_all(ifp);
3212 v = sc->jme_tx_coal_pkt;
3213 error = sysctl_handle_int(oidp, &v, 0, req);
3214 if (error || req->newptr == NULL)
3217 if (v < PCCTX_COAL_PKT_MIN || v > PCCTX_COAL_PKT_MAX) {
3222 if (v != sc->jme_tx_coal_pkt) {
3223 sc->jme_tx_coal_pkt = v;
3224 if (ifp->if_flags & IFF_RUNNING)
3225 jme_set_tx_coal(sc);
3228 ifnet_deserialize_all(ifp);
3233 jme_sysctl_rx_coal_to(SYSCTL_HANDLER_ARGS)
3235 struct jme_softc *sc = arg1;
3236 struct ifnet *ifp = &sc->arpcom.ac_if;
3239 ifnet_serialize_all(ifp);
3241 v = sc->jme_rx_coal_to;
3242 error = sysctl_handle_int(oidp, &v, 0, req);
3243 if (error || req->newptr == NULL)
3246 if (v < PCCRX_COAL_TO_MIN || v > PCCRX_COAL_TO_MAX) {
3251 if (v != sc->jme_rx_coal_to) {
3252 sc->jme_rx_coal_to = v;
3253 if (ifp->if_flags & IFF_RUNNING)
3254 jme_set_rx_coal(sc);
3257 ifnet_deserialize_all(ifp);
3262 jme_sysctl_rx_coal_pkt(SYSCTL_HANDLER_ARGS)
3264 struct jme_softc *sc = arg1;
3265 struct ifnet *ifp = &sc->arpcom.ac_if;
3268 ifnet_serialize_all(ifp);
3270 v = sc->jme_rx_coal_pkt;
3271 error = sysctl_handle_int(oidp, &v, 0, req);
3272 if (error || req->newptr == NULL)
3275 if (v < PCCRX_COAL_PKT_MIN || v > PCCRX_COAL_PKT_MAX) {
3280 if (v != sc->jme_rx_coal_pkt) {
3281 sc->jme_rx_coal_pkt = v;
3282 if (ifp->if_flags & IFF_RUNNING)
3283 jme_set_rx_coal(sc);
3286 ifnet_deserialize_all(ifp);
3291 jme_set_tx_coal(struct jme_softc *sc)
3295 reg = (sc->jme_tx_coal_to << PCCTX_COAL_TO_SHIFT) &
3297 reg |= (sc->jme_tx_coal_pkt << PCCTX_COAL_PKT_SHIFT) &
3298 PCCTX_COAL_PKT_MASK;
3299 reg |= PCCTX_COAL_TXQ0;
3300 CSR_WRITE_4(sc, JME_PCCTX, reg);
3304 jme_set_rx_coal(struct jme_softc *sc)
3309 reg = (sc->jme_rx_coal_to << PCCRX_COAL_TO_SHIFT) &
3311 reg |= (sc->jme_rx_coal_pkt << PCCRX_COAL_PKT_SHIFT) &
3312 PCCRX_COAL_PKT_MASK;
3313 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r)
3314 CSR_WRITE_4(sc, JME_PCCRX(r), reg);
3317 #ifdef IFPOLL_ENABLE
3320 jme_npoll_status(struct ifnet *ifp)
3322 struct jme_softc *sc = ifp->if_softc;
3325 ASSERT_SERIALIZED(&sc->jme_serialize);
3327 status = CSR_READ_4(sc, JME_INTR_STATUS);
3328 if (status & INTR_RXQ_DESC_EMPTY) {
3329 CSR_WRITE_4(sc, JME_INTR_STATUS, status & INTR_RXQ_DESC_EMPTY);
3330 jme_rx_restart(sc, status);
3335 jme_npoll_rx(struct ifnet *ifp __unused, void *arg, int cycle)
3337 struct jme_rxdata *rdata = arg;
3339 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3341 jme_rxeof(rdata, cycle);
3345 jme_npoll_tx(struct ifnet *ifp, void *arg, int cycle __unused)
3347 struct jme_txdata *tdata = arg;
3349 ASSERT_SERIALIZED(&tdata->jme_tx_serialize);
3352 if (!ifq_is_empty(&ifp->if_snd))
3357 jme_npoll(struct ifnet *ifp, struct ifpoll_info *info)
3359 struct jme_softc *sc = ifp->if_softc;
3361 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3366 info->ifpi_status.status_func = jme_npoll_status;
3367 info->ifpi_status.serializer = &sc->jme_serialize;
3369 off = sc->jme_npoll_txoff;
3370 KKASSERT(off <= ncpus2);
3371 info->ifpi_tx[off].poll_func = jme_npoll_tx;
3372 info->ifpi_tx[off].arg = &sc->jme_cdata.jme_tx_data;
3373 info->ifpi_tx[off].serializer =
3374 &sc->jme_cdata.jme_tx_data.jme_tx_serialize;
3376 off = sc->jme_npoll_rxoff;
3377 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
3378 struct jme_rxdata *rdata =
3379 &sc->jme_cdata.jme_rx_data[i];
3382 info->ifpi_rx[idx].poll_func = jme_npoll_rx;
3383 info->ifpi_rx[idx].arg = rdata;
3384 info->ifpi_rx[idx].serializer =
3385 &rdata->jme_rx_serialize;
3388 if (ifp->if_flags & IFF_RUNNING)
3389 jme_disable_intr(sc);
3390 ifq_set_cpuid(&ifp->if_snd, sc->jme_npoll_txoff);
3392 if (ifp->if_flags & IFF_RUNNING)
3393 jme_enable_intr(sc);
3394 ifq_set_cpuid(&ifp->if_snd, sc->jme_tx_cpuid);
3399 jme_sysctl_npoll_rxoff(SYSCTL_HANDLER_ARGS)
3401 struct jme_softc *sc = (void *)arg1;
3402 struct ifnet *ifp = &sc->arpcom.ac_if;
3405 off = sc->jme_npoll_rxoff;
3406 error = sysctl_handle_int(oidp, &off, 0, req);
3407 if (error || req->newptr == NULL)
3412 ifnet_serialize_all(ifp);
3413 if (off >= ncpus2 || off % sc->jme_cdata.jme_rx_ring_cnt != 0) {
3417 sc->jme_npoll_rxoff = off;
3419 ifnet_deserialize_all(ifp);
3425 jme_sysctl_npoll_txoff(SYSCTL_HANDLER_ARGS)
3427 struct jme_softc *sc = (void *)arg1;
3428 struct ifnet *ifp = &sc->arpcom.ac_if;
3431 off = sc->jme_npoll_txoff;
3432 error = sysctl_handle_int(oidp, &off, 0, req);
3433 if (error || req->newptr == NULL)
3438 ifnet_serialize_all(ifp);
3439 if (off >= ncpus2) {
3443 sc->jme_npoll_txoff = off;
3445 ifnet_deserialize_all(ifp);
3450 #endif /* IFPOLL_ENABLE */
3453 jme_rxring_dma_alloc(struct jme_rxdata *rdata)
3458 asize = roundup2(JME_RX_RING_SIZE(rdata), JME_RX_RING_ALIGN);
3459 error = bus_dmamem_coherent(rdata->jme_sc->jme_cdata.jme_ring_tag,
3460 JME_RX_RING_ALIGN, 0,
3461 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3462 asize, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
3464 device_printf(rdata->jme_sc->jme_dev,
3465 "could not allocate %dth Rx ring.\n", rdata->jme_rx_idx);
3468 rdata->jme_rx_ring_tag = dmem.dmem_tag;
3469 rdata->jme_rx_ring_map = dmem.dmem_map;
3470 rdata->jme_rx_ring = dmem.dmem_addr;
3471 rdata->jme_rx_ring_paddr = dmem.dmem_busaddr;
3477 jme_rxbuf_dma_filter(void *arg __unused, bus_addr_t paddr)
3479 if ((paddr & 0xffffffff) == 0) {
3481 * Don't allow lower 32bits of the RX buffer's
3482 * physical address to be 0, else it will break
3483 * hardware pending RSS information delivery
3484 * detection on RX path.
3492 jme_rxbuf_dma_alloc(struct jme_rxdata *rdata)
3497 lowaddr = BUS_SPACE_MAXADDR;
3498 if (JME_ENABLE_HWRSS(rdata->jme_sc)) {
3499 /* jme_rxbuf_dma_filter will be called */
3500 lowaddr = BUS_SPACE_MAXADDR_32BIT;
3503 /* Create tag for Rx buffers. */
3504 error = bus_dma_tag_create(
3505 rdata->jme_sc->jme_cdata.jme_buffer_tag,/* parent */
3506 JME_RX_BUF_ALIGN, 0, /* algnmnt, boundary */
3507 lowaddr, /* lowaddr */
3508 BUS_SPACE_MAXADDR, /* highaddr */
3509 jme_rxbuf_dma_filter, NULL, /* filter, filterarg */
3510 MCLBYTES, /* maxsize */
3512 MCLBYTES, /* maxsegsize */
3513 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ALIGNED,/* flags */
3514 &rdata->jme_rx_tag);
3516 device_printf(rdata->jme_sc->jme_dev,
3517 "could not create %dth Rx DMA tag.\n", rdata->jme_rx_idx);
3521 /* Create DMA maps for Rx buffers. */
3522 error = bus_dmamap_create(rdata->jme_rx_tag, BUS_DMA_WAITOK,
3523 &rdata->jme_rx_sparemap);
3525 device_printf(rdata->jme_sc->jme_dev,
3526 "could not create %dth spare Rx dmamap.\n",
3528 bus_dma_tag_destroy(rdata->jme_rx_tag);
3529 rdata->jme_rx_tag = NULL;
3532 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
3533 struct jme_rxdesc *rxd = &rdata->jme_rxdesc[i];
3535 error = bus_dmamap_create(rdata->jme_rx_tag, BUS_DMA_WAITOK,
3540 device_printf(rdata->jme_sc->jme_dev,
3541 "could not create %dth Rx dmamap "
3542 "for %dth RX ring.\n", i, rdata->jme_rx_idx);
3544 for (j = 0; j < i; ++j) {
3545 rxd = &rdata->jme_rxdesc[j];
3546 bus_dmamap_destroy(rdata->jme_rx_tag,
3549 bus_dmamap_destroy(rdata->jme_rx_tag,
3550 rdata->jme_rx_sparemap);
3551 bus_dma_tag_destroy(rdata->jme_rx_tag);
3552 rdata->jme_rx_tag = NULL;
3560 jme_rx_intr(struct jme_softc *sc, uint32_t status)
3564 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
3565 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
3567 if (status & rdata->jme_rx_coal) {
3568 lwkt_serialize_enter(&rdata->jme_rx_serialize);
3569 jme_rxeof(rdata, -1);
3570 lwkt_serialize_exit(&rdata->jme_rx_serialize);
3576 jme_enable_rss(struct jme_softc *sc)
3579 uint8_t key[RSSKEY_NREGS * RSSKEY_REGSIZE];
3582 KASSERT(sc->jme_cdata.jme_rx_ring_cnt == JME_NRXRING_2 ||
3583 sc->jme_cdata.jme_rx_ring_cnt == JME_NRXRING_4,
3584 ("%s: invalid # of RX rings (%d)",
3585 sc->arpcom.ac_if.if_xname, sc->jme_cdata.jme_rx_ring_cnt));
3587 rssc = RSSC_HASH_64_ENTRY;
3588 rssc |= RSSC_HASH_IPV4 | RSSC_HASH_IPV4_TCP;
3589 rssc |= sc->jme_cdata.jme_rx_ring_cnt >> 1;
3590 JME_RSS_DPRINTF(sc, 1, "rssc 0x%08x\n", rssc);
3591 CSR_WRITE_4(sc, JME_RSSC, rssc);
3593 toeplitz_get_key(key, sizeof(key));
3594 for (i = 0; i < RSSKEY_NREGS; ++i) {
3597 keyreg = RSSKEY_REGVAL(key, i);
3598 JME_RSS_DPRINTF(sc, 5, "keyreg%d 0x%08x\n", i, keyreg);
3600 CSR_WRITE_4(sc, RSSKEY_REG(i), keyreg);
3604 * Create redirect table in following fashion:
3605 * (hash & ring_cnt_mask) == rdr_table[(hash & rdr_table_mask)]
3608 for (i = 0; i < RSSTBL_REGSIZE; ++i) {
3611 q = i % sc->jme_cdata.jme_rx_ring_cnt;
3612 ind |= q << (i * 8);
3614 JME_RSS_DPRINTF(sc, 1, "ind 0x%08x\n", ind);
3616 for (i = 0; i < RSSTBL_NREGS; ++i)
3617 CSR_WRITE_4(sc, RSSTBL_REG(i), ind);
3621 jme_disable_rss(struct jme_softc *sc)
3623 CSR_WRITE_4(sc, JME_RSSC, RSSC_DIS_RSS);
3627 jme_serialize(struct ifnet *ifp, enum ifnet_serialize slz)
3629 struct jme_softc *sc = ifp->if_softc;
3631 ifnet_serialize_array_enter(sc->jme_serialize_arr,
3632 sc->jme_serialize_cnt, JME_TX_SERIALIZE, JME_RX_SERIALIZE, slz);
3636 jme_deserialize(struct ifnet *ifp, enum ifnet_serialize slz)
3638 struct jme_softc *sc = ifp->if_softc;
3640 ifnet_serialize_array_exit(sc->jme_serialize_arr,
3641 sc->jme_serialize_cnt, JME_TX_SERIALIZE, JME_RX_SERIALIZE, slz);
3645 jme_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz)
3647 struct jme_softc *sc = ifp->if_softc;
3649 return ifnet_serialize_array_try(sc->jme_serialize_arr,
3650 sc->jme_serialize_cnt, JME_TX_SERIALIZE, JME_RX_SERIALIZE, slz);
3656 jme_serialize_assert(struct ifnet *ifp, enum ifnet_serialize slz,
3657 boolean_t serialized)
3659 struct jme_softc *sc = ifp->if_softc;
3661 ifnet_serialize_array_assert(sc->jme_serialize_arr,
3662 sc->jme_serialize_cnt, JME_TX_SERIALIZE, JME_RX_SERIALIZE,
3666 #endif /* INVARIANTS */
3669 jme_msix_try_alloc(device_t dev)
3671 struct jme_softc *sc = device_get_softc(dev);
3672 struct jme_msix_data *msix;
3673 int error, i, r, msix_enable, msix_count;
3674 int offset, offset_def;
3676 msix_count = JME_MSIXCNT(sc->jme_cdata.jme_rx_ring_cnt);
3677 KKASSERT(msix_count <= JME_NMSIX);
3679 msix_enable = device_getenv_int(dev, "msix.enable", jme_msix_enable);
3682 * We leave the 1st MSI-X vector unused, so we
3683 * actually need msix_count + 1 MSI-X vectors.
3685 if (!msix_enable || pci_msix_count(dev) < (msix_count + 1))
3688 for (i = 0; i < msix_count; ++i)
3689 sc->jme_msix[i].jme_msix_rid = -1;
3694 * Setup status MSI-X
3697 msix = &sc->jme_msix[i++];
3698 msix->jme_msix_cpuid = 0;
3699 msix->jme_msix_arg = sc;
3700 msix->jme_msix_func = jme_msix_status;
3701 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
3702 msix->jme_msix_intrs |=
3703 sc->jme_cdata.jme_rx_data[r].jme_rx_empty;
3705 msix->jme_msix_serialize = &sc->jme_serialize;
3706 ksnprintf(msix->jme_msix_desc, sizeof(msix->jme_msix_desc), "%s sts",
3707 device_get_nameunit(dev));
3713 offset_def = device_get_unit(dev) % ncpus2;
3714 offset = device_getenv_int(dev, "msix.txoff", offset_def);
3715 if (offset >= ncpus2) {
3716 device_printf(dev, "invalid msix.txoff %d, use %d\n",
3717 offset, offset_def);
3718 offset = offset_def;
3721 msix = &sc->jme_msix[i++];
3722 msix->jme_msix_cpuid = offset;
3723 sc->jme_tx_cpuid = msix->jme_msix_cpuid;
3724 msix->jme_msix_arg = &sc->jme_cdata.jme_tx_data;
3725 msix->jme_msix_func = jme_msix_tx;
3726 msix->jme_msix_intrs = INTR_TXQ_COAL | INTR_TXQ_COAL_TO;
3727 msix->jme_msix_serialize = &sc->jme_cdata.jme_tx_data.jme_tx_serialize;
3728 ksnprintf(msix->jme_msix_desc, sizeof(msix->jme_msix_desc), "%s tx",
3729 device_get_nameunit(dev));
3735 if (sc->jme_cdata.jme_rx_ring_cnt == ncpus2) {
3738 offset_def = (sc->jme_cdata.jme_rx_ring_cnt *
3739 device_get_unit(dev)) % ncpus2;
3741 offset = device_getenv_int(dev, "msix.rxoff", offset_def);
3742 if (offset >= ncpus2 ||
3743 offset % sc->jme_cdata.jme_rx_ring_cnt != 0) {
3744 device_printf(dev, "invalid msix.rxoff %d, use %d\n",
3745 offset, offset_def);
3746 offset = offset_def;
3750 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
3751 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
3753 msix = &sc->jme_msix[i++];
3754 msix->jme_msix_cpuid = r + offset;
3755 KKASSERT(msix->jme_msix_cpuid < ncpus2);
3756 msix->jme_msix_arg = rdata;
3757 msix->jme_msix_func = jme_msix_rx;
3758 msix->jme_msix_intrs = rdata->jme_rx_coal;
3759 msix->jme_msix_serialize = &rdata->jme_rx_serialize;
3760 ksnprintf(msix->jme_msix_desc, sizeof(msix->jme_msix_desc),
3761 "%s rx%d", device_get_nameunit(dev), r);
3764 KKASSERT(i == msix_count);
3766 error = pci_setup_msix(dev);
3770 /* Setup jme_msix_cnt early, so we could cleanup */
3771 sc->jme_msix_cnt = msix_count;
3773 for (i = 0; i < msix_count; ++i) {
3774 msix = &sc->jme_msix[i];
3776 msix->jme_msix_vector = i + 1;
3777 error = pci_alloc_msix_vector(dev, msix->jme_msix_vector,
3778 &msix->jme_msix_rid, msix->jme_msix_cpuid);
3782 msix->jme_msix_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
3783 &msix->jme_msix_rid, RF_ACTIVE);
3784 if (msix->jme_msix_res == NULL) {
3790 for (i = 0; i < JME_INTR_CNT; ++i) {
3791 uint32_t intr_mask = (1 << i);
3794 if ((JME_INTRS & intr_mask) == 0)
3797 for (x = 0; x < msix_count; ++x) {
3798 msix = &sc->jme_msix[x];
3799 if (msix->jme_msix_intrs & intr_mask) {
3802 reg = i / JME_MSINUM_FACTOR;
3803 KKASSERT(reg < JME_MSINUM_CNT);
3805 shift = (i % JME_MSINUM_FACTOR) * 4;
3807 sc->jme_msinum[reg] |=
3808 (msix->jme_msix_vector << shift);
3816 for (i = 0; i < JME_MSINUM_CNT; ++i) {
3817 device_printf(dev, "MSINUM%d: %#x\n", i,
3822 pci_enable_msix(dev);
3823 sc->jme_irq_type = PCI_INTR_TYPE_MSIX;
3831 jme_intr_alloc(device_t dev)
3833 struct jme_softc *sc = device_get_softc(dev);
3836 jme_msix_try_alloc(dev);
3838 if (sc->jme_irq_type != PCI_INTR_TYPE_MSIX) {
3839 sc->jme_irq_type = pci_alloc_1intr(dev, jme_msi_enable,
3840 &sc->jme_irq_rid, &irq_flags);
3842 sc->jme_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
3843 &sc->jme_irq_rid, irq_flags);
3844 if (sc->jme_irq_res == NULL) {
3845 device_printf(dev, "can't allocate irq\n");
3853 jme_msix_free(device_t dev)
3855 struct jme_softc *sc = device_get_softc(dev);
3858 KKASSERT(sc->jme_msix_cnt > 1);
3860 for (i = 0; i < sc->jme_msix_cnt; ++i) {
3861 struct jme_msix_data *msix = &sc->jme_msix[i];
3863 if (msix->jme_msix_res != NULL) {
3864 bus_release_resource(dev, SYS_RES_IRQ,
3865 msix->jme_msix_rid, msix->jme_msix_res);
3866 msix->jme_msix_res = NULL;
3868 if (msix->jme_msix_rid >= 0) {
3869 pci_release_msix_vector(dev, msix->jme_msix_rid);
3870 msix->jme_msix_rid = -1;
3873 pci_teardown_msix(dev);
3877 jme_intr_free(device_t dev)
3879 struct jme_softc *sc = device_get_softc(dev);
3881 if (sc->jme_irq_type != PCI_INTR_TYPE_MSIX) {
3882 if (sc->jme_irq_res != NULL) {
3883 bus_release_resource(dev, SYS_RES_IRQ, sc->jme_irq_rid,
3886 if (sc->jme_irq_type == PCI_INTR_TYPE_MSI)
3887 pci_release_msi(dev);
3894 jme_msix_tx(void *xtdata)
3896 struct jme_txdata *tdata = xtdata;
3897 struct jme_softc *sc = tdata->jme_sc;
3898 struct ifnet *ifp = &sc->arpcom.ac_if;
3900 ASSERT_SERIALIZED(&tdata->jme_tx_serialize);
3902 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, INTR_TXQ_COAL | INTR_TXQ_COAL_TO);
3904 CSR_WRITE_4(sc, JME_INTR_STATUS,
3905 INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP);
3907 if (ifp->if_flags & IFF_RUNNING) {
3909 if (!ifq_is_empty(&ifp->if_snd))
3913 CSR_WRITE_4(sc, JME_INTR_MASK_SET, INTR_TXQ_COAL | INTR_TXQ_COAL_TO);
3917 jme_msix_rx(void *xrdata)
3919 struct jme_rxdata *rdata = xrdata;
3920 struct jme_softc *sc = rdata->jme_sc;
3921 struct ifnet *ifp = &sc->arpcom.ac_if;
3923 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3925 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, rdata->jme_rx_coal);
3927 CSR_WRITE_4(sc, JME_INTR_STATUS,
3928 rdata->jme_rx_coal | rdata->jme_rx_comp);
3930 if (ifp->if_flags & IFF_RUNNING)
3931 jme_rxeof(rdata, -1);
3933 CSR_WRITE_4(sc, JME_INTR_MASK_SET, rdata->jme_rx_coal);
3937 jme_msix_status(void *xsc)
3939 struct jme_softc *sc = xsc;
3940 struct ifnet *ifp = &sc->arpcom.ac_if;
3943 ASSERT_SERIALIZED(&sc->jme_serialize);
3945 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, INTR_RXQ_DESC_EMPTY);
3947 status = CSR_READ_4(sc, JME_INTR_STATUS);
3949 if (status & INTR_RXQ_DESC_EMPTY) {
3950 CSR_WRITE_4(sc, JME_INTR_STATUS, status & INTR_RXQ_DESC_EMPTY);
3951 if (ifp->if_flags & IFF_RUNNING)
3952 jme_rx_restart(sc, status);
3955 CSR_WRITE_4(sc, JME_INTR_MASK_SET, INTR_RXQ_DESC_EMPTY);
3959 jme_rx_restart(struct jme_softc *sc, uint32_t status)
3963 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
3964 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[i];
3966 if (status & rdata->jme_rx_empty) {
3967 lwkt_serialize_enter(&rdata->jme_rx_serialize);
3968 jme_rxeof(rdata, -1);
3969 #ifdef JME_RSS_DEBUG
3970 rdata->jme_rx_emp++;
3972 lwkt_serialize_exit(&rdata->jme_rx_serialize);
3975 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RX_ENB |
3980 jme_set_msinum(struct jme_softc *sc)
3984 for (i = 0; i < JME_MSINUM_CNT; ++i)
3985 CSR_WRITE_4(sc, JME_MSINUM(i), sc->jme_msinum[i]);
3989 jme_intr_setup(device_t dev)
3991 struct jme_softc *sc = device_get_softc(dev);
3994 if (sc->jme_irq_type == PCI_INTR_TYPE_MSIX)
3995 return jme_msix_setup(dev);
3997 error = bus_setup_intr(dev, sc->jme_irq_res, INTR_MPSAFE,
3998 jme_intr, sc, &sc->jme_irq_handle, &sc->jme_serialize);
4000 device_printf(dev, "could not set up interrupt handler.\n");
4003 sc->jme_tx_cpuid = rman_get_cpuid(sc->jme_irq_res);
4009 jme_intr_teardown(device_t dev)
4011 struct jme_softc *sc = device_get_softc(dev);
4013 if (sc->jme_irq_type == PCI_INTR_TYPE_MSIX)
4014 jme_msix_teardown(dev, sc->jme_msix_cnt);
4016 bus_teardown_intr(dev, sc->jme_irq_res, sc->jme_irq_handle);
4020 jme_msix_setup(device_t dev)
4022 struct jme_softc *sc = device_get_softc(dev);
4025 for (x = 0; x < sc->jme_msix_cnt; ++x) {
4026 struct jme_msix_data *msix = &sc->jme_msix[x];
4029 error = bus_setup_intr_descr(dev, msix->jme_msix_res,
4030 INTR_MPSAFE, msix->jme_msix_func, msix->jme_msix_arg,
4031 &msix->jme_msix_handle, msix->jme_msix_serialize,
4032 msix->jme_msix_desc);
4034 device_printf(dev, "could not set up %s "
4035 "interrupt handler.\n", msix->jme_msix_desc);
4036 jme_msix_teardown(dev, x);
4044 jme_msix_teardown(device_t dev, int msix_count)
4046 struct jme_softc *sc = device_get_softc(dev);
4049 for (x = 0; x < msix_count; ++x) {
4050 struct jme_msix_data *msix = &sc->jme_msix[x];
4052 bus_teardown_intr(dev, msix->jme_msix_res,
4053 msix->jme_msix_handle);
4058 jme_serialize_skipmain(struct jme_softc *sc)
4060 lwkt_serialize_array_enter(sc->jme_serialize_arr,
4061 sc->jme_serialize_cnt, 1);
4065 jme_deserialize_skipmain(struct jme_softc *sc)
4067 lwkt_serialize_array_exit(sc->jme_serialize_arr,
4068 sc->jme_serialize_cnt, 1);
4072 jme_enable_intr(struct jme_softc *sc)
4076 for (i = 0; i < sc->jme_serialize_cnt; ++i)
4077 lwkt_serialize_handler_enable(sc->jme_serialize_arr[i]);
4079 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
4083 jme_disable_intr(struct jme_softc *sc)
4087 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
4089 for (i = 0; i < sc->jme_serialize_cnt; ++i)
4090 lwkt_serialize_handler_disable(sc->jme_serialize_arr[i]);