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 = JME_TXWREG_NSEGS;
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 /* Setup the TX ring's CPUID */
1067 ifq_set_cpuid(&ifp->if_snd, sc->jme_tx_cpuid);
1069 error = jme_intr_setup(dev);
1071 ether_ifdetach(ifp);
1082 jme_detach(device_t dev)
1084 struct jme_softc *sc = device_get_softc(dev);
1086 if (device_is_attached(dev)) {
1087 struct ifnet *ifp = &sc->arpcom.ac_if;
1089 ifnet_serialize_all(ifp);
1091 jme_intr_teardown(dev);
1092 ifnet_deserialize_all(ifp);
1094 ether_ifdetach(ifp);
1097 if (sc->jme_sysctl_tree != NULL)
1098 sysctl_ctx_free(&sc->jme_sysctl_ctx);
1100 if (sc->jme_miibus != NULL)
1101 device_delete_child(dev, sc->jme_miibus);
1102 bus_generic_detach(dev);
1106 if (sc->jme_mem_res != NULL) {
1107 bus_release_resource(dev, SYS_RES_MEMORY, sc->jme_mem_rid,
1117 jme_sysctl_node(struct jme_softc *sc)
1119 #ifdef JME_RSS_DEBUG
1123 sysctl_ctx_init(&sc->jme_sysctl_ctx);
1124 sc->jme_sysctl_tree = SYSCTL_ADD_NODE(&sc->jme_sysctl_ctx,
1125 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
1126 device_get_nameunit(sc->jme_dev),
1128 if (sc->jme_sysctl_tree == NULL) {
1129 device_printf(sc->jme_dev, "can't add sysctl node\n");
1133 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1134 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1135 "tx_coal_to", CTLTYPE_INT | CTLFLAG_RW,
1136 sc, 0, jme_sysctl_tx_coal_to, "I", "jme tx coalescing timeout");
1138 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1139 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1140 "tx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW,
1141 sc, 0, jme_sysctl_tx_coal_pkt, "I", "jme tx coalescing packet");
1143 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1144 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1145 "rx_coal_to", CTLTYPE_INT | CTLFLAG_RW,
1146 sc, 0, jme_sysctl_rx_coal_to, "I", "jme rx coalescing timeout");
1148 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1149 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1150 "rx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW,
1151 sc, 0, jme_sysctl_rx_coal_pkt, "I", "jme rx coalescing packet");
1153 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1154 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1155 "rx_desc_count", CTLFLAG_RD,
1156 &sc->jme_cdata.jme_rx_data[0].jme_rx_desc_cnt,
1157 0, "RX desc count");
1158 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1159 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1160 "tx_desc_count", CTLFLAG_RD,
1161 &sc->jme_cdata.jme_tx_data.jme_tx_desc_cnt,
1162 0, "TX desc count");
1163 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1164 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1165 "rx_ring_count", CTLFLAG_RD,
1166 &sc->jme_cdata.jme_rx_ring_cnt,
1167 0, "RX ring count");
1168 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1169 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1170 "tx_wreg", CTLFLAG_RW,
1171 &sc->jme_cdata.jme_tx_data.jme_tx_wreg, 0,
1172 "# of segments before writing to hardware register");
1174 #ifdef JME_RSS_DEBUG
1175 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1176 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1177 "rss_debug", CTLFLAG_RW, &sc->jme_rss_debug,
1178 0, "RSS debug level");
1179 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1180 char rx_ring_desc[32];
1182 ksnprintf(rx_ring_desc, sizeof(rx_ring_desc),
1183 "rx_ring%d_pkt", r);
1184 SYSCTL_ADD_ULONG(&sc->jme_sysctl_ctx,
1185 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1186 rx_ring_desc, CTLFLAG_RW,
1187 &sc->jme_cdata.jme_rx_data[r].jme_rx_pkt, "RXed packets");
1189 ksnprintf(rx_ring_desc, sizeof(rx_ring_desc),
1190 "rx_ring%d_emp", r);
1191 SYSCTL_ADD_ULONG(&sc->jme_sysctl_ctx,
1192 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1193 rx_ring_desc, CTLFLAG_RW,
1194 &sc->jme_cdata.jme_rx_data[r].jme_rx_emp,
1195 "# of time RX ring empty");
1199 #ifdef IFPOLL_ENABLE
1200 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1201 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1202 "npoll_rxoff", CTLTYPE_INT|CTLFLAG_RW, sc, 0,
1203 jme_sysctl_npoll_rxoff, "I", "NPOLLING RX cpu offset");
1204 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1205 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1206 "npoll_txoff", CTLTYPE_INT|CTLFLAG_RW, sc, 0,
1207 jme_sysctl_npoll_txoff, "I", "NPOLLING TX cpu offset");
1212 jme_dma_alloc(struct jme_softc *sc)
1214 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
1215 struct jme_txdesc *txd;
1217 int error, i, asize;
1219 asize = __VM_CACHELINE_ALIGN(
1220 tdata->jme_tx_desc_cnt * sizeof(struct jme_txdesc));
1221 tdata->jme_txdesc = kmalloc_cachealign(asize, M_DEVBUF,
1224 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
1225 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[i];
1227 asize = __VM_CACHELINE_ALIGN(
1228 rdata->jme_rx_desc_cnt * sizeof(struct jme_rxdesc));
1229 rdata->jme_rxdesc = kmalloc_cachealign(asize, M_DEVBUF,
1233 /* Create parent ring tag. */
1234 error = bus_dma_tag_create(NULL,/* parent */
1235 1, JME_RING_BOUNDARY, /* algnmnt, boundary */
1236 sc->jme_lowaddr, /* lowaddr */
1237 BUS_SPACE_MAXADDR, /* highaddr */
1238 NULL, NULL, /* filter, filterarg */
1239 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1241 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1243 &sc->jme_cdata.jme_ring_tag);
1245 device_printf(sc->jme_dev,
1246 "could not create parent ring DMA tag.\n");
1251 * Create DMA stuffs for TX ring
1253 asize = roundup2(JME_TX_RING_SIZE(tdata), JME_TX_RING_ALIGN);
1254 error = bus_dmamem_coherent(sc->jme_cdata.jme_ring_tag,
1255 JME_TX_RING_ALIGN, 0,
1256 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1257 asize, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
1259 device_printf(sc->jme_dev, "could not allocate Tx ring.\n");
1262 tdata->jme_tx_ring_tag = dmem.dmem_tag;
1263 tdata->jme_tx_ring_map = dmem.dmem_map;
1264 tdata->jme_tx_ring = dmem.dmem_addr;
1265 tdata->jme_tx_ring_paddr = dmem.dmem_busaddr;
1268 * Create DMA stuffs for RX rings
1270 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
1271 error = jme_rxring_dma_alloc(&sc->jme_cdata.jme_rx_data[i]);
1276 /* Create parent buffer tag. */
1277 error = bus_dma_tag_create(NULL,/* parent */
1278 1, 0, /* algnmnt, boundary */
1279 sc->jme_lowaddr, /* lowaddr */
1280 BUS_SPACE_MAXADDR, /* highaddr */
1281 NULL, NULL, /* filter, filterarg */
1282 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1284 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1286 &sc->jme_cdata.jme_buffer_tag);
1288 device_printf(sc->jme_dev,
1289 "could not create parent buffer DMA tag.\n");
1294 * Create DMA stuffs for shadow status block
1296 asize = roundup2(JME_SSB_SIZE, JME_SSB_ALIGN);
1297 error = bus_dmamem_coherent(sc->jme_cdata.jme_buffer_tag,
1298 JME_SSB_ALIGN, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1299 asize, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
1301 device_printf(sc->jme_dev,
1302 "could not create shadow status block.\n");
1305 sc->jme_cdata.jme_ssb_tag = dmem.dmem_tag;
1306 sc->jme_cdata.jme_ssb_map = dmem.dmem_map;
1307 sc->jme_cdata.jme_ssb_block = dmem.dmem_addr;
1308 sc->jme_cdata.jme_ssb_block_paddr = dmem.dmem_busaddr;
1311 * Create DMA stuffs for TX buffers
1314 /* Create tag for Tx buffers. */
1315 error = bus_dma_tag_create(sc->jme_cdata.jme_buffer_tag,/* parent */
1316 1, 0, /* algnmnt, boundary */
1317 BUS_SPACE_MAXADDR, /* lowaddr */
1318 BUS_SPACE_MAXADDR, /* highaddr */
1319 NULL, NULL, /* filter, filterarg */
1320 JME_TSO_MAXSIZE, /* maxsize */
1321 JME_MAXTXSEGS, /* nsegments */
1322 JME_MAXSEGSIZE, /* maxsegsize */
1323 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,/* flags */
1324 &tdata->jme_tx_tag);
1326 device_printf(sc->jme_dev, "could not create Tx DMA tag.\n");
1330 /* Create DMA maps for Tx buffers. */
1331 for (i = 0; i < tdata->jme_tx_desc_cnt; i++) {
1332 txd = &tdata->jme_txdesc[i];
1333 error = bus_dmamap_create(tdata->jme_tx_tag,
1334 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
1339 device_printf(sc->jme_dev,
1340 "could not create %dth Tx dmamap.\n", i);
1342 for (j = 0; j < i; ++j) {
1343 txd = &tdata->jme_txdesc[j];
1344 bus_dmamap_destroy(tdata->jme_tx_tag,
1347 bus_dma_tag_destroy(tdata->jme_tx_tag);
1348 tdata->jme_tx_tag = NULL;
1354 * Create DMA stuffs for RX buffers
1356 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
1357 error = jme_rxbuf_dma_alloc(&sc->jme_cdata.jme_rx_data[i]);
1365 jme_dma_free(struct jme_softc *sc)
1367 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
1368 struct jme_txdesc *txd;
1369 struct jme_rxdesc *rxd;
1370 struct jme_rxdata *rdata;
1374 if (tdata->jme_tx_ring_tag != NULL) {
1375 bus_dmamap_unload(tdata->jme_tx_ring_tag,
1376 tdata->jme_tx_ring_map);
1377 bus_dmamem_free(tdata->jme_tx_ring_tag,
1378 tdata->jme_tx_ring, tdata->jme_tx_ring_map);
1379 bus_dma_tag_destroy(tdata->jme_tx_ring_tag);
1380 tdata->jme_tx_ring_tag = NULL;
1384 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1385 rdata = &sc->jme_cdata.jme_rx_data[r];
1386 if (rdata->jme_rx_ring_tag != NULL) {
1387 bus_dmamap_unload(rdata->jme_rx_ring_tag,
1388 rdata->jme_rx_ring_map);
1389 bus_dmamem_free(rdata->jme_rx_ring_tag,
1391 rdata->jme_rx_ring_map);
1392 bus_dma_tag_destroy(rdata->jme_rx_ring_tag);
1393 rdata->jme_rx_ring_tag = NULL;
1398 if (tdata->jme_tx_tag != NULL) {
1399 for (i = 0; i < tdata->jme_tx_desc_cnt; i++) {
1400 txd = &tdata->jme_txdesc[i];
1401 bus_dmamap_destroy(tdata->jme_tx_tag, txd->tx_dmamap);
1403 bus_dma_tag_destroy(tdata->jme_tx_tag);
1404 tdata->jme_tx_tag = NULL;
1408 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1409 rdata = &sc->jme_cdata.jme_rx_data[r];
1410 if (rdata->jme_rx_tag != NULL) {
1411 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
1412 rxd = &rdata->jme_rxdesc[i];
1413 bus_dmamap_destroy(rdata->jme_rx_tag,
1416 bus_dmamap_destroy(rdata->jme_rx_tag,
1417 rdata->jme_rx_sparemap);
1418 bus_dma_tag_destroy(rdata->jme_rx_tag);
1419 rdata->jme_rx_tag = NULL;
1423 /* Shadow status block. */
1424 if (sc->jme_cdata.jme_ssb_tag != NULL) {
1425 bus_dmamap_unload(sc->jme_cdata.jme_ssb_tag,
1426 sc->jme_cdata.jme_ssb_map);
1427 bus_dmamem_free(sc->jme_cdata.jme_ssb_tag,
1428 sc->jme_cdata.jme_ssb_block,
1429 sc->jme_cdata.jme_ssb_map);
1430 bus_dma_tag_destroy(sc->jme_cdata.jme_ssb_tag);
1431 sc->jme_cdata.jme_ssb_tag = NULL;
1434 if (sc->jme_cdata.jme_buffer_tag != NULL) {
1435 bus_dma_tag_destroy(sc->jme_cdata.jme_buffer_tag);
1436 sc->jme_cdata.jme_buffer_tag = NULL;
1438 if (sc->jme_cdata.jme_ring_tag != NULL) {
1439 bus_dma_tag_destroy(sc->jme_cdata.jme_ring_tag);
1440 sc->jme_cdata.jme_ring_tag = NULL;
1443 if (tdata->jme_txdesc != NULL) {
1444 kfree(tdata->jme_txdesc, M_DEVBUF);
1445 tdata->jme_txdesc = NULL;
1447 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1448 rdata = &sc->jme_cdata.jme_rx_data[r];
1449 if (rdata->jme_rxdesc != NULL) {
1450 kfree(rdata->jme_rxdesc, M_DEVBUF);
1451 rdata->jme_rxdesc = NULL;
1457 * Make sure the interface is stopped at reboot time.
1460 jme_shutdown(device_t dev)
1462 return jme_suspend(dev);
1467 * Unlike other ethernet controllers, JMC250 requires
1468 * explicit resetting link speed to 10/100Mbps as gigabit
1469 * link will cunsume more power than 375mA.
1470 * Note, we reset the link speed to 10/100Mbps with
1471 * auto-negotiation but we don't know whether that operation
1472 * would succeed or not as we have no control after powering
1473 * off. If the renegotiation fail WOL may not work. Running
1474 * at 1Gbps draws more power than 375mA at 3.3V which is
1475 * specified in PCI specification and that would result in
1476 * complete shutdowning power to ethernet controller.
1479 * Save current negotiated media speed/duplex/flow-control
1480 * to softc and restore the same link again after resuming.
1481 * PHY handling such as power down/resetting to 100Mbps
1482 * may be better handled in suspend method in phy driver.
1485 jme_setlinkspeed(struct jme_softc *sc)
1487 struct mii_data *mii;
1490 JME_LOCK_ASSERT(sc);
1492 mii = device_get_softc(sc->jme_miibus);
1495 if ((mii->mii_media_status & IFM_AVALID) != 0) {
1496 switch IFM_SUBTYPE(mii->mii_media_active) {
1506 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_100T2CR, 0);
1507 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_ANAR,
1508 ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
1509 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_BMCR,
1510 BMCR_AUTOEN | BMCR_STARTNEG);
1513 /* Poll link state until jme(4) get a 10/100 link. */
1514 for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
1516 if ((mii->mii_media_status & IFM_AVALID) != 0) {
1517 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1527 pause("jmelnk", hz);
1530 if (i == MII_ANEGTICKS_GIGE)
1531 device_printf(sc->jme_dev, "establishing link failed, "
1532 "WOL may not work!");
1535 * No link, force MAC to have 100Mbps, full-duplex link.
1536 * This is the last resort and may/may not work.
1538 mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
1539 mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1544 jme_setwol(struct jme_softc *sc)
1546 struct ifnet *ifp = &sc->arpcom.ac_if;
1551 if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1552 /* No PME capability, PHY power down. */
1553 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1554 MII_BMCR, BMCR_PDOWN);
1558 gpr = CSR_READ_4(sc, JME_GPREG0) & ~GPREG0_PME_ENB;
1559 pmcs = CSR_READ_4(sc, JME_PMCS);
1560 pmcs &= ~PMCS_WOL_ENB_MASK;
1561 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) {
1562 pmcs |= PMCS_MAGIC_FRAME | PMCS_MAGIC_FRAME_ENB;
1563 /* Enable PME message. */
1564 gpr |= GPREG0_PME_ENB;
1565 /* For gigabit controllers, reset link speed to 10/100. */
1566 if ((sc->jme_caps & JME_CAP_FASTETH) == 0)
1567 jme_setlinkspeed(sc);
1570 CSR_WRITE_4(sc, JME_PMCS, pmcs);
1571 CSR_WRITE_4(sc, JME_GPREG0, gpr);
1574 pmstat = pci_read_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, 2);
1575 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
1576 if ((ifp->if_capenable & IFCAP_WOL) != 0)
1577 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
1578 pci_write_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1579 if ((ifp->if_capenable & IFCAP_WOL) == 0) {
1580 /* No WOL, PHY power down. */
1581 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1582 MII_BMCR, BMCR_PDOWN);
1588 jme_suspend(device_t dev)
1590 struct jme_softc *sc = device_get_softc(dev);
1591 struct ifnet *ifp = &sc->arpcom.ac_if;
1593 ifnet_serialize_all(ifp);
1598 ifnet_deserialize_all(ifp);
1604 jme_resume(device_t dev)
1606 struct jme_softc *sc = device_get_softc(dev);
1607 struct ifnet *ifp = &sc->arpcom.ac_if;
1612 ifnet_serialize_all(ifp);
1615 if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1618 pmstat = pci_read_config(sc->jme_dev,
1619 pmc + PCIR_POWER_STATUS, 2);
1620 /* Disable PME clear PME status. */
1621 pmstat &= ~PCIM_PSTAT_PMEENABLE;
1622 pci_write_config(sc->jme_dev,
1623 pmc + PCIR_POWER_STATUS, pmstat, 2);
1627 if (ifp->if_flags & IFF_UP)
1630 ifnet_deserialize_all(ifp);
1636 jme_tso_pullup(struct mbuf **mp)
1638 int hoff, iphlen, thoff;
1642 KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
1644 iphlen = m->m_pkthdr.csum_iphlen;
1645 thoff = m->m_pkthdr.csum_thlen;
1646 hoff = m->m_pkthdr.csum_lhlen;
1648 KASSERT(iphlen > 0, ("invalid ip hlen"));
1649 KASSERT(thoff > 0, ("invalid tcp hlen"));
1650 KASSERT(hoff > 0, ("invalid ether hlen"));
1652 if (__predict_false(m->m_len < hoff + iphlen + thoff)) {
1653 m = m_pullup(m, hoff + iphlen + thoff);
1664 jme_encap(struct jme_txdata *tdata, struct mbuf **m_head, int *segs_used)
1666 struct jme_txdesc *txd;
1667 struct jme_desc *desc;
1669 bus_dma_segment_t txsegs[JME_MAXTXSEGS];
1671 int error, i, prod, symbol_desc;
1672 uint32_t cflags, flag64, mss;
1674 M_ASSERTPKTHDR((*m_head));
1676 if ((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) {
1677 /* XXX Is this necessary? */
1678 error = jme_tso_pullup(m_head);
1683 prod = tdata->jme_tx_prod;
1684 txd = &tdata->jme_txdesc[prod];
1686 if (tdata->jme_sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT)
1691 maxsegs = (tdata->jme_tx_desc_cnt - tdata->jme_tx_cnt) -
1692 (JME_TXD_RSVD + symbol_desc);
1693 if (maxsegs > JME_MAXTXSEGS)
1694 maxsegs = JME_MAXTXSEGS;
1695 KASSERT(maxsegs >= (JME_TXD_SPARE - symbol_desc),
1696 ("not enough segments %d", maxsegs));
1698 error = bus_dmamap_load_mbuf_defrag(tdata->jme_tx_tag,
1699 txd->tx_dmamap, m_head,
1700 txsegs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
1703 *segs_used += nsegs;
1705 bus_dmamap_sync(tdata->jme_tx_tag, txd->tx_dmamap,
1706 BUS_DMASYNC_PREWRITE);
1712 /* Configure checksum offload. */
1713 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1714 mss = (uint32_t)m->m_pkthdr.tso_segsz << JME_TD_MSS_SHIFT;
1715 cflags |= JME_TD_TSO;
1716 } else if (m->m_pkthdr.csum_flags & JME_CSUM_FEATURES) {
1717 if (m->m_pkthdr.csum_flags & CSUM_IP)
1718 cflags |= JME_TD_IPCSUM;
1719 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1720 cflags |= JME_TD_TCPCSUM;
1721 if (m->m_pkthdr.csum_flags & CSUM_UDP)
1722 cflags |= JME_TD_UDPCSUM;
1725 /* Configure VLAN. */
1726 if (m->m_flags & M_VLANTAG) {
1727 cflags |= (m->m_pkthdr.ether_vlantag & JME_TD_VLAN_MASK);
1728 cflags |= JME_TD_VLAN_TAG;
1731 desc = &tdata->jme_tx_ring[prod];
1732 desc->flags = htole32(cflags);
1733 desc->addr_hi = htole32(m->m_pkthdr.len);
1734 if (tdata->jme_sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT) {
1736 * Use 64bits TX desc chain format.
1738 * The first TX desc of the chain, which is setup here,
1739 * is just a symbol TX desc carrying no payload.
1741 flag64 = JME_TD_64BIT;
1742 desc->buflen = htole32(mss);
1747 /* No effective TX desc is consumed */
1751 * Use 32bits TX desc chain format.
1753 * The first TX desc of the chain, which is setup here,
1754 * is an effective TX desc carrying the first segment of
1758 desc->buflen = htole32(mss | txsegs[0].ds_len);
1759 desc->addr_lo = htole32(JME_ADDR_LO(txsegs[0].ds_addr));
1761 /* One effective TX desc is consumed */
1764 tdata->jme_tx_cnt++;
1765 KKASSERT(tdata->jme_tx_cnt - i < tdata->jme_tx_desc_cnt - JME_TXD_RSVD);
1766 JME_DESC_INC(prod, tdata->jme_tx_desc_cnt);
1768 txd->tx_ndesc = 1 - i;
1769 for (; i < nsegs; i++) {
1770 desc = &tdata->jme_tx_ring[prod];
1771 desc->buflen = htole32(txsegs[i].ds_len);
1772 desc->addr_hi = htole32(JME_ADDR_HI(txsegs[i].ds_addr));
1773 desc->addr_lo = htole32(JME_ADDR_LO(txsegs[i].ds_addr));
1774 desc->flags = htole32(JME_TD_OWN | flag64);
1776 tdata->jme_tx_cnt++;
1777 KKASSERT(tdata->jme_tx_cnt <=
1778 tdata->jme_tx_desc_cnt - JME_TXD_RSVD);
1779 JME_DESC_INC(prod, tdata->jme_tx_desc_cnt);
1782 /* Update producer index. */
1783 tdata->jme_tx_prod = prod;
1785 * Finally request interrupt and give the first descriptor
1786 * owenership to hardware.
1788 desc = txd->tx_desc;
1789 desc->flags |= htole32(JME_TD_OWN | JME_TD_INTR);
1792 txd->tx_ndesc += nsegs;
1802 jme_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1804 struct jme_softc *sc = ifp->if_softc;
1805 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
1806 struct mbuf *m_head;
1809 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1810 ASSERT_SERIALIZED(&tdata->jme_tx_serialize);
1812 if (!sc->jme_has_link) {
1813 ifq_purge(&ifp->if_snd);
1817 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
1820 if (tdata->jme_tx_cnt >= JME_TX_DESC_HIWAT(tdata))
1823 while (!ifq_is_empty(&ifp->if_snd)) {
1825 * Check number of available TX descs, always
1826 * leave JME_TXD_RSVD free TX descs.
1828 if (tdata->jme_tx_cnt + JME_TXD_SPARE >
1829 tdata->jme_tx_desc_cnt - JME_TXD_RSVD) {
1830 ifq_set_oactive(&ifp->if_snd);
1834 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1839 * Pack the data into the transmit ring. If we
1840 * don't have room, set the OACTIVE flag and wait
1841 * for the NIC to drain the ring.
1843 if (jme_encap(tdata, &m_head, &enq)) {
1844 KKASSERT(m_head == NULL);
1845 IFNET_STAT_INC(ifp, oerrors, 1);
1846 ifq_set_oactive(&ifp->if_snd);
1850 if (enq >= tdata->jme_tx_wreg) {
1851 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr |
1852 TXCSR_TX_ENB | TXCSR_TXQ_N_START(TXCSR_TXQ0));
1857 * If there's a BPF listener, bounce a copy of this frame
1860 ETHER_BPF_MTAP(ifp, m_head);
1862 /* Set a timeout in case the chip goes out to lunch. */
1863 ifp->if_timer = JME_TX_TIMEOUT;
1868 * Reading TXCSR takes very long time under heavy load
1869 * so cache TXCSR value and writes the ORed value with
1870 * the kick command to the TXCSR. This saves one register
1873 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr | TXCSR_TX_ENB |
1874 TXCSR_TXQ_N_START(TXCSR_TXQ0));
1879 jme_watchdog(struct ifnet *ifp)
1881 struct jme_softc *sc = ifp->if_softc;
1882 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
1884 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1886 if (!sc->jme_has_link) {
1887 if_printf(ifp, "watchdog timeout (missed link)\n");
1888 IFNET_STAT_INC(ifp, oerrors, 1);
1894 if (tdata->jme_tx_cnt == 0) {
1895 if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
1897 if (!ifq_is_empty(&ifp->if_snd))
1902 if_printf(ifp, "watchdog timeout\n");
1903 IFNET_STAT_INC(ifp, oerrors, 1);
1905 if (!ifq_is_empty(&ifp->if_snd))
1910 jme_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1912 struct jme_softc *sc = ifp->if_softc;
1913 struct mii_data *mii = device_get_softc(sc->jme_miibus);
1914 struct ifreq *ifr = (struct ifreq *)data;
1915 int error = 0, mask;
1917 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1921 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > JME_JUMBO_MTU ||
1922 (!(sc->jme_caps & JME_CAP_JUMBO) &&
1923 ifr->ifr_mtu > JME_MAX_MTU)) {
1928 if (ifp->if_mtu != ifr->ifr_mtu) {
1930 * No special configuration is required when interface
1931 * MTU is changed but availability of Tx checksum
1932 * offload should be chcked against new MTU size as
1933 * FIFO size is just 2K.
1935 if (ifr->ifr_mtu >= JME_TX_FIFO_SIZE) {
1936 ifp->if_capenable &=
1937 ~(IFCAP_TXCSUM | IFCAP_TSO);
1939 ~(JME_CSUM_FEATURES | CSUM_TSO);
1941 ifp->if_mtu = ifr->ifr_mtu;
1942 if (ifp->if_flags & IFF_RUNNING)
1948 if (ifp->if_flags & IFF_UP) {
1949 if (ifp->if_flags & IFF_RUNNING) {
1950 if ((ifp->if_flags ^ sc->jme_if_flags) &
1951 (IFF_PROMISC | IFF_ALLMULTI))
1957 if (ifp->if_flags & IFF_RUNNING)
1960 sc->jme_if_flags = ifp->if_flags;
1965 if (ifp->if_flags & IFF_RUNNING)
1971 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1975 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1977 if ((mask & IFCAP_TXCSUM) && ifp->if_mtu < JME_TX_FIFO_SIZE) {
1978 ifp->if_capenable ^= IFCAP_TXCSUM;
1979 if (ifp->if_capenable & IFCAP_TXCSUM)
1980 ifp->if_hwassist |= JME_CSUM_FEATURES;
1982 ifp->if_hwassist &= ~JME_CSUM_FEATURES;
1984 if (mask & IFCAP_RXCSUM) {
1987 ifp->if_capenable ^= IFCAP_RXCSUM;
1988 reg = CSR_READ_4(sc, JME_RXMAC);
1989 reg &= ~RXMAC_CSUM_ENB;
1990 if (ifp->if_capenable & IFCAP_RXCSUM)
1991 reg |= RXMAC_CSUM_ENB;
1992 CSR_WRITE_4(sc, JME_RXMAC, reg);
1995 if (mask & IFCAP_VLAN_HWTAGGING) {
1996 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2000 if ((mask & IFCAP_TSO) && ifp->if_mtu < JME_TX_FIFO_SIZE) {
2001 ifp->if_capenable ^= IFCAP_TSO;
2002 if (ifp->if_capenable & IFCAP_TSO)
2003 ifp->if_hwassist |= CSUM_TSO;
2005 ifp->if_hwassist &= ~CSUM_TSO;
2008 if (mask & IFCAP_RSS)
2009 ifp->if_capenable ^= IFCAP_RSS;
2013 error = ether_ioctl(ifp, cmd, data);
2020 jme_mac_config(struct jme_softc *sc)
2022 struct mii_data *mii;
2023 uint32_t ghc, rxmac, txmac, txpause, gp1;
2024 int phyconf = JMPHY_CONF_DEFFIFO, hdx = 0;
2026 mii = device_get_softc(sc->jme_miibus);
2028 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2030 CSR_WRITE_4(sc, JME_GHC, 0);
2032 rxmac = CSR_READ_4(sc, JME_RXMAC);
2033 rxmac &= ~RXMAC_FC_ENB;
2034 txmac = CSR_READ_4(sc, JME_TXMAC);
2035 txmac &= ~(TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST);
2036 txpause = CSR_READ_4(sc, JME_TXPFC);
2037 txpause &= ~TXPFC_PAUSE_ENB;
2038 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
2039 ghc |= GHC_FULL_DUPLEX;
2040 rxmac &= ~RXMAC_COLL_DET_ENB;
2041 txmac &= ~(TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE |
2042 TXMAC_BACKOFF | TXMAC_CARRIER_EXT |
2045 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
2046 txpause |= TXPFC_PAUSE_ENB;
2047 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
2048 rxmac |= RXMAC_FC_ENB;
2050 /* Disable retry transmit timer/retry limit. */
2051 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) &
2052 ~(TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB));
2054 rxmac |= RXMAC_COLL_DET_ENB;
2055 txmac |= TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE | TXMAC_BACKOFF;
2056 /* Enable retry transmit timer/retry limit. */
2057 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) |
2058 TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB);
2062 * Reprogram Tx/Rx MACs with resolved speed/duplex.
2064 gp1 = CSR_READ_4(sc, JME_GPREG1);
2065 gp1 &= ~GPREG1_WA_HDX;
2067 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) == 0)
2070 switch (IFM_SUBTYPE(mii->mii_media_active)) {
2072 ghc |= GHC_SPEED_10 | sc->jme_clksrc;
2074 gp1 |= GPREG1_WA_HDX;
2078 ghc |= GHC_SPEED_100 | sc->jme_clksrc;
2080 gp1 |= GPREG1_WA_HDX;
2083 * Use extended FIFO depth to workaround CRC errors
2084 * emitted by chips before JMC250B
2086 phyconf = JMPHY_CONF_EXTFIFO;
2090 if (sc->jme_caps & JME_CAP_FASTETH)
2093 ghc |= GHC_SPEED_1000 | sc->jme_clksrc_1000;
2095 txmac |= TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST;
2101 CSR_WRITE_4(sc, JME_GHC, ghc);
2102 CSR_WRITE_4(sc, JME_RXMAC, rxmac);
2103 CSR_WRITE_4(sc, JME_TXMAC, txmac);
2104 CSR_WRITE_4(sc, JME_TXPFC, txpause);
2106 if (sc->jme_workaround & JME_WA_EXTFIFO) {
2107 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
2108 JMPHY_CONF, phyconf);
2110 if (sc->jme_workaround & JME_WA_HDX)
2111 CSR_WRITE_4(sc, JME_GPREG1, gp1);
2117 struct jme_softc *sc = xsc;
2118 struct ifnet *ifp = &sc->arpcom.ac_if;
2122 ASSERT_SERIALIZED(&sc->jme_serialize);
2124 status = CSR_READ_4(sc, JME_INTR_REQ_STATUS);
2125 if (status == 0 || status == 0xFFFFFFFF)
2128 /* Disable interrupts. */
2129 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
2131 status = CSR_READ_4(sc, JME_INTR_STATUS);
2132 if ((status & JME_INTRS) == 0 || status == 0xFFFFFFFF)
2135 /* Reset PCC counter/timer and Ack interrupts. */
2136 status &= ~(INTR_TXQ_COMP | INTR_RXQ_COMP);
2138 if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO))
2139 status |= INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP;
2141 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2142 if (status & jme_rx_status[r].jme_coal) {
2143 status |= jme_rx_status[r].jme_coal |
2144 jme_rx_status[r].jme_comp;
2148 CSR_WRITE_4(sc, JME_INTR_STATUS, status);
2150 if (ifp->if_flags & IFF_RUNNING) {
2151 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
2153 if (status & (INTR_RXQ_COAL | INTR_RXQ_COAL_TO))
2154 jme_rx_intr(sc, status);
2156 if (status & INTR_RXQ_DESC_EMPTY) {
2158 * Notify hardware availability of new Rx buffers.
2159 * Reading RXCSR takes very long time under heavy
2160 * load so cache RXCSR value and writes the ORed
2161 * value with the kick command to the RXCSR. This
2162 * saves one register access cycle.
2164 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr |
2165 RXCSR_RX_ENB | RXCSR_RXQ_START);
2168 if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO)) {
2169 lwkt_serialize_enter(&tdata->jme_tx_serialize);
2171 if (!ifq_is_empty(&ifp->if_snd))
2173 lwkt_serialize_exit(&tdata->jme_tx_serialize);
2177 /* Reenable interrupts. */
2178 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
2182 jme_txeof(struct jme_txdata *tdata)
2184 struct ifnet *ifp = &tdata->jme_sc->arpcom.ac_if;
2187 cons = tdata->jme_tx_cons;
2188 if (cons == tdata->jme_tx_prod)
2192 * Go through our Tx list and free mbufs for those
2193 * frames which have been transmitted.
2195 while (cons != tdata->jme_tx_prod) {
2196 struct jme_txdesc *txd, *next_txd;
2197 uint32_t status, next_status;
2198 int next_cons, nsegs;
2200 txd = &tdata->jme_txdesc[cons];
2201 KASSERT(txd->tx_m != NULL,
2202 ("%s: freeing NULL mbuf!", __func__));
2204 status = le32toh(txd->tx_desc->flags);
2205 if ((status & JME_TD_OWN) == JME_TD_OWN)
2210 * This chip will always update the TX descriptor's
2211 * buflen field and this updating always happens
2212 * after clearing the OWN bit, so even if the OWN
2213 * bit is cleared by the chip, we still don't sure
2214 * about whether the buflen field has been updated
2215 * by the chip or not. To avoid this race, we wait
2216 * for the next TX descriptor's OWN bit to be cleared
2217 * by the chip before reusing this TX descriptor.
2220 JME_DESC_ADD(next_cons, txd->tx_ndesc, tdata->jme_tx_desc_cnt);
2221 next_txd = &tdata->jme_txdesc[next_cons];
2222 if (next_txd->tx_m == NULL)
2224 next_status = le32toh(next_txd->tx_desc->flags);
2225 if ((next_status & JME_TD_OWN) == JME_TD_OWN)
2228 if (status & (JME_TD_TMOUT | JME_TD_RETRY_EXP)) {
2229 IFNET_STAT_INC(ifp, oerrors, 1);
2231 IFNET_STAT_INC(ifp, opackets, 1);
2232 if (status & JME_TD_COLLISION) {
2233 IFNET_STAT_INC(ifp, collisions,
2234 le32toh(txd->tx_desc->buflen) &
2235 JME_TD_BUF_LEN_MASK);
2240 * Only the first descriptor of multi-descriptor
2241 * transmission is updated so driver have to skip entire
2242 * chained buffers for the transmiited frame. In other
2243 * words, JME_TD_OWN bit is valid only at the first
2244 * descriptor of a multi-descriptor transmission.
2246 for (nsegs = 0; nsegs < txd->tx_ndesc; nsegs++) {
2247 tdata->jme_tx_ring[cons].flags = 0;
2248 JME_DESC_INC(cons, tdata->jme_tx_desc_cnt);
2251 /* Reclaim transferred mbufs. */
2252 bus_dmamap_unload(tdata->jme_tx_tag, txd->tx_dmamap);
2255 tdata->jme_tx_cnt -= txd->tx_ndesc;
2256 KASSERT(tdata->jme_tx_cnt >= 0,
2257 ("%s: Active Tx desc counter was garbled", __func__));
2260 tdata->jme_tx_cons = cons;
2262 /* 1 for symbol TX descriptor */
2263 if (tdata->jme_tx_cnt <= JME_MAXTXSEGS + 1)
2266 if (tdata->jme_tx_cnt + JME_TXD_SPARE <=
2267 tdata->jme_tx_desc_cnt - JME_TXD_RSVD)
2268 ifq_clr_oactive(&ifp->if_snd);
2271 static __inline void
2272 jme_discard_rxbufs(struct jme_rxdata *rdata, int cons, int count)
2276 for (i = 0; i < count; ++i) {
2277 jme_setup_rxdesc(&rdata->jme_rxdesc[cons]);
2278 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt);
2282 static __inline struct pktinfo *
2283 jme_pktinfo(struct pktinfo *pi, uint32_t flags)
2285 if (flags & JME_RD_IPV4)
2286 pi->pi_netisr = NETISR_IP;
2287 else if (flags & JME_RD_IPV6)
2288 pi->pi_netisr = NETISR_IPV6;
2293 pi->pi_l3proto = IPPROTO_UNKNOWN;
2295 if (flags & JME_RD_MORE_FRAG)
2296 pi->pi_flags |= PKTINFO_FLAG_FRAG;
2297 else if (flags & JME_RD_TCP)
2298 pi->pi_l3proto = IPPROTO_TCP;
2299 else if (flags & JME_RD_UDP)
2300 pi->pi_l3proto = IPPROTO_UDP;
2306 /* Receive a frame. */
2308 jme_rxpkt(struct jme_rxdata *rdata)
2310 struct ifnet *ifp = &rdata->jme_sc->arpcom.ac_if;
2311 struct jme_desc *desc;
2312 struct jme_rxdesc *rxd;
2313 struct mbuf *mp, *m;
2314 uint32_t flags, status, hash, hashinfo;
2315 int cons, count, nsegs;
2317 cons = rdata->jme_rx_cons;
2318 desc = &rdata->jme_rx_ring[cons];
2320 flags = le32toh(desc->flags);
2321 status = le32toh(desc->buflen);
2322 hash = le32toh(desc->addr_hi);
2323 hashinfo = le32toh(desc->addr_lo);
2324 nsegs = JME_RX_NSEGS(status);
2327 /* Skip the first descriptor. */
2328 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt);
2331 * Clear the OWN bit of the following RX descriptors;
2332 * hardware will not clear the OWN bit except the first
2335 * Since the first RX descriptor is setup, i.e. OWN bit
2336 * on, before its followins RX descriptors, leaving the
2337 * OWN bit on the following RX descriptors will trick
2338 * the hardware into thinking that the following RX
2339 * descriptors are ready to be used too.
2341 for (count = 1; count < nsegs; count++,
2342 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt))
2343 rdata->jme_rx_ring[cons].flags = 0;
2345 cons = rdata->jme_rx_cons;
2348 JME_RSS_DPRINTF(rdata->jme_sc, 15, "ring%d, flags 0x%08x, "
2349 "hash 0x%08x, hash info 0x%08x\n",
2350 rdata->jme_rx_idx, flags, hash, hashinfo);
2352 if (status & JME_RX_ERR_STAT) {
2353 IFNET_STAT_INC(ifp, ierrors, 1);
2354 jme_discard_rxbufs(rdata, cons, nsegs);
2355 #ifdef JME_SHOW_ERRORS
2356 if_printf(ifp, "%s : receive error = 0x%b\n",
2357 __func__, JME_RX_ERR(status), JME_RX_ERR_BITS);
2359 rdata->jme_rx_cons += nsegs;
2360 rdata->jme_rx_cons %= rdata->jme_rx_desc_cnt;
2364 rdata->jme_rxlen = JME_RX_BYTES(status) - JME_RX_PAD_BYTES;
2365 for (count = 0; count < nsegs; count++,
2366 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt)) {
2367 rxd = &rdata->jme_rxdesc[cons];
2370 /* Add a new receive buffer to the ring. */
2371 if (jme_newbuf(rdata, rxd, 0) != 0) {
2372 IFNET_STAT_INC(ifp, iqdrops, 1);
2374 jme_discard_rxbufs(rdata, cons, nsegs - count);
2375 if (rdata->jme_rxhead != NULL) {
2376 m_freem(rdata->jme_rxhead);
2377 JME_RXCHAIN_RESET(rdata);
2383 * Assume we've received a full sized frame.
2384 * Actual size is fixed when we encounter the end of
2385 * multi-segmented frame.
2387 mp->m_len = MCLBYTES;
2389 /* Chain received mbufs. */
2390 if (rdata->jme_rxhead == NULL) {
2391 rdata->jme_rxhead = mp;
2392 rdata->jme_rxtail = mp;
2395 * Receive processor can receive a maximum frame
2396 * size of 65535 bytes.
2398 rdata->jme_rxtail->m_next = mp;
2399 rdata->jme_rxtail = mp;
2402 if (count == nsegs - 1) {
2403 struct pktinfo pi0, *pi;
2405 /* Last desc. for this frame. */
2406 m = rdata->jme_rxhead;
2407 m->m_pkthdr.len = rdata->jme_rxlen;
2409 /* Set first mbuf size. */
2410 m->m_len = MCLBYTES - JME_RX_PAD_BYTES;
2411 /* Set last mbuf size. */
2412 mp->m_len = rdata->jme_rxlen -
2413 ((MCLBYTES - JME_RX_PAD_BYTES) +
2414 (MCLBYTES * (nsegs - 2)));
2416 m->m_len = rdata->jme_rxlen;
2418 m->m_pkthdr.rcvif = ifp;
2421 * Account for 10bytes auto padding which is used
2422 * to align IP header on 32bit boundary. Also note,
2423 * CRC bytes is automatically removed by the
2426 m->m_data += JME_RX_PAD_BYTES;
2428 /* Set checksum information. */
2429 if ((ifp->if_capenable & IFCAP_RXCSUM) &&
2430 (flags & JME_RD_IPV4)) {
2431 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2432 if (flags & JME_RD_IPCSUM)
2433 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2434 if ((flags & JME_RD_MORE_FRAG) == 0 &&
2435 ((flags & (JME_RD_TCP | JME_RD_TCPCSUM)) ==
2436 (JME_RD_TCP | JME_RD_TCPCSUM) ||
2437 (flags & (JME_RD_UDP | JME_RD_UDPCSUM)) ==
2438 (JME_RD_UDP | JME_RD_UDPCSUM))) {
2439 m->m_pkthdr.csum_flags |=
2440 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2441 m->m_pkthdr.csum_data = 0xffff;
2445 /* Check for VLAN tagged packets. */
2446 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) &&
2447 (flags & JME_RD_VLAN_TAG)) {
2448 m->m_pkthdr.ether_vlantag =
2449 flags & JME_RD_VLAN_MASK;
2450 m->m_flags |= M_VLANTAG;
2453 IFNET_STAT_INC(ifp, ipackets, 1);
2455 if (ifp->if_capenable & IFCAP_RSS)
2456 pi = jme_pktinfo(&pi0, flags);
2461 (hashinfo & JME_RD_HASH_FN_MASK) ==
2462 JME_RD_HASH_FN_TOEPLITZ) {
2463 m->m_flags |= (M_HASH | M_CKHASH);
2464 m->m_pkthdr.hash = toeplitz_hash(hash);
2467 #ifdef JME_RSS_DEBUG
2469 JME_RSS_DPRINTF(rdata->jme_sc, 10,
2470 "isr %d flags %08x, l3 %d %s\n",
2471 pi->pi_netisr, pi->pi_flags,
2473 (m->m_flags & M_HASH) ? "hash" : "");
2478 ether_input_pkt(ifp, m, pi);
2480 /* Reset mbuf chains. */
2481 JME_RXCHAIN_RESET(rdata);
2482 #ifdef JME_RSS_DEBUG
2483 rdata->jme_rx_pkt++;
2488 rdata->jme_rx_cons += nsegs;
2489 rdata->jme_rx_cons %= rdata->jme_rx_desc_cnt;
2493 jme_rxeof(struct jme_rxdata *rdata, int count)
2495 struct jme_desc *desc;
2499 #ifdef IFPOLL_ENABLE
2500 if (count >= 0 && count-- == 0)
2503 desc = &rdata->jme_rx_ring[rdata->jme_rx_cons];
2504 if ((le32toh(desc->flags) & JME_RD_OWN) == JME_RD_OWN)
2506 if ((le32toh(desc->buflen) & JME_RD_VALID) == 0)
2510 * Check number of segments against received bytes.
2511 * Non-matching value would indicate that hardware
2512 * is still trying to update Rx descriptors. I'm not
2513 * sure whether this check is needed.
2515 nsegs = JME_RX_NSEGS(le32toh(desc->buflen));
2516 pktlen = JME_RX_BYTES(le32toh(desc->buflen));
2517 if (nsegs != howmany(pktlen, MCLBYTES)) {
2518 if_printf(&rdata->jme_sc->arpcom.ac_if,
2519 "RX fragment count(%d) and "
2520 "packet size(%d) mismach\n", nsegs, pktlen);
2526 * RSS hash and hash information may _not_ be set by the
2527 * hardware even if the OWN bit is cleared and VALID bit
2530 * If the RSS information is not delivered by the hardware
2531 * yet, we MUST NOT accept this packet, let alone reusing
2532 * its RX descriptor. If this packet was accepted and its
2533 * RX descriptor was reused before hardware delivering the
2534 * RSS information, the RX buffer's address would be trashed
2535 * by the RSS information delivered by the hardware.
2537 if (JME_ENABLE_HWRSS(rdata->jme_sc)) {
2538 struct jme_rxdesc *rxd;
2541 hashinfo = le32toh(desc->addr_lo);
2542 rxd = &rdata->jme_rxdesc[rdata->jme_rx_cons];
2545 * This test should be enough to detect the pending
2546 * RSS information delivery, given:
2547 * - If RSS hash is not calculated, the hashinfo
2548 * will be 0. Howvever, the lower 32bits of RX
2549 * buffers' physical address will never be 0.
2550 * (see jme_rxbuf_dma_filter)
2551 * - If RSS hash is calculated, the lowest 4 bits
2552 * of hashinfo will be set, while the RX buffers
2553 * are at least 2K aligned.
2555 if (hashinfo == JME_ADDR_LO(rxd->rx_paddr)) {
2556 #ifdef JME_SHOW_RSSWB
2557 if_printf(&rdata->jme_sc->arpcom.ac_if,
2558 "RSS is not written back yet\n");
2564 /* Received a frame. */
2572 struct jme_softc *sc = xsc;
2573 struct mii_data *mii = device_get_softc(sc->jme_miibus);
2575 lwkt_serialize_enter(&sc->jme_serialize);
2577 KKASSERT(mycpuid == JME_TICK_CPUID);
2579 sc->jme_in_tick = TRUE;
2581 sc->jme_in_tick = FALSE;
2583 callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
2585 lwkt_serialize_exit(&sc->jme_serialize);
2589 jme_reset(struct jme_softc *sc)
2593 /* Make sure that TX and RX are stopped */
2598 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2602 * Hold reset bit before stop reset
2605 /* Disable TXMAC and TXOFL clock sources */
2606 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2607 /* Disable RXMAC clock source */
2608 val = CSR_READ_4(sc, JME_GPREG1);
2609 CSR_WRITE_4(sc, JME_GPREG1, val | GPREG1_DIS_RXMAC_CLKSRC);
2611 CSR_READ_4(sc, JME_GHC);
2614 CSR_WRITE_4(sc, JME_GHC, 0);
2616 CSR_READ_4(sc, JME_GHC);
2619 * Clear reset bit after stop reset
2622 /* Enable TXMAC and TXOFL clock sources */
2623 CSR_WRITE_4(sc, JME_GHC, GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC);
2624 /* Enable RXMAC clock source */
2625 val = CSR_READ_4(sc, JME_GPREG1);
2626 CSR_WRITE_4(sc, JME_GPREG1, val & ~GPREG1_DIS_RXMAC_CLKSRC);
2628 CSR_READ_4(sc, JME_GHC);
2630 /* Disable TXMAC and TXOFL clock sources */
2631 CSR_WRITE_4(sc, JME_GHC, 0);
2632 /* Disable RXMAC clock source */
2633 val = CSR_READ_4(sc, JME_GPREG1);
2634 CSR_WRITE_4(sc, JME_GPREG1, val | GPREG1_DIS_RXMAC_CLKSRC);
2636 CSR_READ_4(sc, JME_GHC);
2638 /* Enable TX and RX */
2639 val = CSR_READ_4(sc, JME_TXCSR);
2640 CSR_WRITE_4(sc, JME_TXCSR, val | TXCSR_TX_ENB);
2641 val = CSR_READ_4(sc, JME_RXCSR);
2642 CSR_WRITE_4(sc, JME_RXCSR, val | RXCSR_RX_ENB);
2644 CSR_READ_4(sc, JME_TXCSR);
2645 CSR_READ_4(sc, JME_RXCSR);
2647 /* Enable TXMAC and TXOFL clock sources */
2648 CSR_WRITE_4(sc, JME_GHC, GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC);
2649 /* Eisable RXMAC clock source */
2650 val = CSR_READ_4(sc, JME_GPREG1);
2651 CSR_WRITE_4(sc, JME_GPREG1, val & ~GPREG1_DIS_RXMAC_CLKSRC);
2653 CSR_READ_4(sc, JME_GHC);
2655 /* Stop TX and RX */
2663 struct jme_softc *sc = xsc;
2664 struct ifnet *ifp = &sc->arpcom.ac_if;
2665 struct mii_data *mii;
2666 uint8_t eaddr[ETHER_ADDR_LEN];
2671 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2674 * Cancel any pending I/O.
2679 * Reset the chip to a known state.
2684 * Setup MSI/MSI-X vectors to interrupts mapping
2688 if (JME_ENABLE_HWRSS(sc))
2691 jme_disable_rss(sc);
2693 /* Init RX descriptors */
2694 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2695 error = jme_init_rx_ring(&sc->jme_cdata.jme_rx_data[r]);
2697 if_printf(ifp, "initialization failed: "
2698 "no memory for %dth RX ring.\n", r);
2704 /* Init TX descriptors */
2705 jme_init_tx_ring(&sc->jme_cdata.jme_tx_data);
2707 /* Initialize shadow status block. */
2710 /* Reprogram the station address. */
2711 bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
2712 CSR_WRITE_4(sc, JME_PAR0,
2713 eaddr[3] << 24 | eaddr[2] << 16 | eaddr[1] << 8 | eaddr[0]);
2714 CSR_WRITE_4(sc, JME_PAR1, eaddr[5] << 8 | eaddr[4]);
2717 * Configure Tx queue.
2718 * Tx priority queue weight value : 0
2719 * Tx FIFO threshold for processing next packet : 16QW
2720 * Maximum Tx DMA length : 512
2721 * Allow Tx DMA burst.
2723 sc->jme_txcsr = TXCSR_TXQ_N_SEL(TXCSR_TXQ0);
2724 sc->jme_txcsr |= TXCSR_TXQ_WEIGHT(TXCSR_TXQ_WEIGHT_MIN);
2725 sc->jme_txcsr |= TXCSR_FIFO_THRESH_16QW;
2726 sc->jme_txcsr |= sc->jme_tx_dma_size;
2727 sc->jme_txcsr |= TXCSR_DMA_BURST;
2728 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr);
2730 /* Set Tx descriptor counter. */
2731 CSR_WRITE_4(sc, JME_TXQDC, sc->jme_cdata.jme_tx_data.jme_tx_desc_cnt);
2733 /* Set Tx ring address to the hardware. */
2734 paddr = sc->jme_cdata.jme_tx_data.jme_tx_ring_paddr;
2735 CSR_WRITE_4(sc, JME_TXDBA_HI, JME_ADDR_HI(paddr));
2736 CSR_WRITE_4(sc, JME_TXDBA_LO, JME_ADDR_LO(paddr));
2738 /* Configure TxMAC parameters. */
2739 reg = TXMAC_IFG1_DEFAULT | TXMAC_IFG2_DEFAULT | TXMAC_IFG_ENB;
2740 reg |= TXMAC_THRESH_1_PKT;
2741 reg |= TXMAC_CRC_ENB | TXMAC_PAD_ENB;
2742 CSR_WRITE_4(sc, JME_TXMAC, reg);
2745 * Configure Rx queue.
2746 * FIFO full threshold for transmitting Tx pause packet : 128T
2747 * FIFO threshold for processing next packet : 128QW
2749 * Max Rx DMA length : 128
2750 * Rx descriptor retry : 32
2751 * Rx descriptor retry time gap : 256ns
2752 * Don't receive runt/bad frame.
2754 sc->jme_rxcsr = RXCSR_FIFO_FTHRESH_128T;
2757 * Since Rx FIFO size is 4K bytes, receiving frames larger
2758 * than 4K bytes will suffer from Rx FIFO overruns. So
2759 * decrease FIFO threshold to reduce the FIFO overruns for
2760 * frames larger than 4000 bytes.
2761 * For best performance of standard MTU sized frames use
2762 * maximum allowable FIFO threshold, 128QW.
2764 if ((ifp->if_mtu + ETHER_HDR_LEN + EVL_ENCAPLEN + ETHER_CRC_LEN) >
2766 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
2768 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_128QW;
2770 /* Improve PCI Express compatibility */
2771 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
2773 sc->jme_rxcsr |= sc->jme_rx_dma_size;
2774 sc->jme_rxcsr |= RXCSR_DESC_RT_CNT(RXCSR_DESC_RT_CNT_DEFAULT);
2775 sc->jme_rxcsr |= RXCSR_DESC_RT_GAP_256 & RXCSR_DESC_RT_GAP_MASK;
2776 /* XXX TODO DROP_BAD */
2778 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2779 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
2781 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RXQ_N_SEL(r));
2783 /* Set Rx descriptor counter. */
2784 CSR_WRITE_4(sc, JME_RXQDC, rdata->jme_rx_desc_cnt);
2786 /* Set Rx ring address to the hardware. */
2787 paddr = rdata->jme_rx_ring_paddr;
2788 CSR_WRITE_4(sc, JME_RXDBA_HI, JME_ADDR_HI(paddr));
2789 CSR_WRITE_4(sc, JME_RXDBA_LO, JME_ADDR_LO(paddr));
2792 /* Clear receive filter. */
2793 CSR_WRITE_4(sc, JME_RXMAC, 0);
2795 /* Set up the receive filter. */
2800 * Disable all WOL bits as WOL can interfere normal Rx
2801 * operation. Also clear WOL detection status bits.
2803 reg = CSR_READ_4(sc, JME_PMCS);
2804 reg &= ~PMCS_WOL_ENB_MASK;
2805 CSR_WRITE_4(sc, JME_PMCS, reg);
2808 * Pad 10bytes right before received frame. This will greatly
2809 * help Rx performance on strict-alignment architectures as
2810 * it does not need to copy the frame to align the payload.
2812 reg = CSR_READ_4(sc, JME_RXMAC);
2813 reg |= RXMAC_PAD_10BYTES;
2815 if (ifp->if_capenable & IFCAP_RXCSUM)
2816 reg |= RXMAC_CSUM_ENB;
2817 CSR_WRITE_4(sc, JME_RXMAC, reg);
2819 /* Configure general purpose reg0 */
2820 reg = CSR_READ_4(sc, JME_GPREG0);
2821 reg &= ~GPREG0_PCC_UNIT_MASK;
2822 /* Set PCC timer resolution to micro-seconds unit. */
2823 reg |= GPREG0_PCC_UNIT_US;
2825 * Disable all shadow register posting as we have to read
2826 * JME_INTR_STATUS register in jme_intr. Also it seems
2827 * that it's hard to synchronize interrupt status between
2828 * hardware and software with shadow posting due to
2829 * requirements of bus_dmamap_sync(9).
2831 reg |= GPREG0_SH_POST_DW7_DIS | GPREG0_SH_POST_DW6_DIS |
2832 GPREG0_SH_POST_DW5_DIS | GPREG0_SH_POST_DW4_DIS |
2833 GPREG0_SH_POST_DW3_DIS | GPREG0_SH_POST_DW2_DIS |
2834 GPREG0_SH_POST_DW1_DIS | GPREG0_SH_POST_DW0_DIS;
2835 /* Disable posting of DW0. */
2836 reg &= ~GPREG0_POST_DW0_ENB;
2837 /* Clear PME message. */
2838 reg &= ~GPREG0_PME_ENB;
2839 /* Set PHY address. */
2840 reg &= ~GPREG0_PHY_ADDR_MASK;
2841 reg |= sc->jme_phyaddr;
2842 CSR_WRITE_4(sc, JME_GPREG0, reg);
2844 /* Configure Tx queue 0 packet completion coalescing. */
2845 jme_set_tx_coal(sc);
2847 /* Configure Rx queues packet completion coalescing. */
2848 jme_set_rx_coal(sc);
2850 /* Configure shadow status block but don't enable posting. */
2851 paddr = sc->jme_cdata.jme_ssb_block_paddr;
2852 CSR_WRITE_4(sc, JME_SHBASE_ADDR_HI, JME_ADDR_HI(paddr));
2853 CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO, JME_ADDR_LO(paddr));
2855 /* Disable Timer 1 and Timer 2. */
2856 CSR_WRITE_4(sc, JME_TIMER1, 0);
2857 CSR_WRITE_4(sc, JME_TIMER2, 0);
2859 /* Configure retry transmit period, retry limit value. */
2860 CSR_WRITE_4(sc, JME_TXTRHD,
2861 ((TXTRHD_RT_PERIOD_DEFAULT << TXTRHD_RT_PERIOD_SHIFT) &
2862 TXTRHD_RT_PERIOD_MASK) |
2863 ((TXTRHD_RT_LIMIT_DEFAULT << TXTRHD_RT_LIMIT_SHIFT) &
2864 TXTRHD_RT_LIMIT_SHIFT));
2866 #ifdef IFPOLL_ENABLE
2867 if (!(ifp->if_flags & IFF_NPOLLING))
2869 /* Initialize the interrupt mask. */
2870 jme_enable_intr(sc);
2871 CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2874 * Enabling Tx/Rx DMA engines and Rx queue processing is
2875 * done after detection of valid link in jme_miibus_statchg.
2877 sc->jme_has_link = FALSE;
2879 /* Set the current media. */
2880 mii = device_get_softc(sc->jme_miibus);
2883 callout_reset_bycpu(&sc->jme_tick_ch, hz, jme_tick, sc,
2886 ifp->if_flags |= IFF_RUNNING;
2887 ifq_clr_oactive(&ifp->if_snd);
2891 jme_stop(struct jme_softc *sc)
2893 struct ifnet *ifp = &sc->arpcom.ac_if;
2894 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
2895 struct jme_txdesc *txd;
2896 struct jme_rxdesc *rxd;
2897 struct jme_rxdata *rdata;
2900 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2903 * Mark the interface down and cancel the watchdog timer.
2905 ifp->if_flags &= ~IFF_RUNNING;
2906 ifq_clr_oactive(&ifp->if_snd);
2909 callout_stop(&sc->jme_tick_ch);
2910 sc->jme_has_link = FALSE;
2913 * Disable interrupts.
2915 jme_disable_intr(sc);
2916 CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2918 /* Disable updating shadow status block. */
2919 CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO,
2920 CSR_READ_4(sc, JME_SHBASE_ADDR_LO) & ~SHBASE_POST_ENB);
2922 /* Stop receiver, transmitter. */
2927 * Free partial finished RX segments
2929 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2930 rdata = &sc->jme_cdata.jme_rx_data[r];
2931 if (rdata->jme_rxhead != NULL)
2932 m_freem(rdata->jme_rxhead);
2933 JME_RXCHAIN_RESET(rdata);
2937 * Free RX and TX mbufs still in the queues.
2939 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2940 rdata = &sc->jme_cdata.jme_rx_data[r];
2941 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
2942 rxd = &rdata->jme_rxdesc[i];
2943 if (rxd->rx_m != NULL) {
2944 bus_dmamap_unload(rdata->jme_rx_tag,
2951 for (i = 0; i < tdata->jme_tx_desc_cnt; i++) {
2952 txd = &tdata->jme_txdesc[i];
2953 if (txd->tx_m != NULL) {
2954 bus_dmamap_unload(tdata->jme_tx_tag, txd->tx_dmamap);
2963 jme_stop_tx(struct jme_softc *sc)
2968 reg = CSR_READ_4(sc, JME_TXCSR);
2969 if ((reg & TXCSR_TX_ENB) == 0)
2971 reg &= ~TXCSR_TX_ENB;
2972 CSR_WRITE_4(sc, JME_TXCSR, reg);
2973 for (i = JME_TIMEOUT; i > 0; i--) {
2975 if ((CSR_READ_4(sc, JME_TXCSR) & TXCSR_TX_ENB) == 0)
2979 device_printf(sc->jme_dev, "stopping transmitter timeout!\n");
2983 jme_stop_rx(struct jme_softc *sc)
2988 reg = CSR_READ_4(sc, JME_RXCSR);
2989 if ((reg & RXCSR_RX_ENB) == 0)
2991 reg &= ~RXCSR_RX_ENB;
2992 CSR_WRITE_4(sc, JME_RXCSR, reg);
2993 for (i = JME_TIMEOUT; i > 0; i--) {
2995 if ((CSR_READ_4(sc, JME_RXCSR) & RXCSR_RX_ENB) == 0)
2999 device_printf(sc->jme_dev, "stopping recevier timeout!\n");
3003 jme_init_tx_ring(struct jme_txdata *tdata)
3005 struct jme_txdesc *txd;
3008 tdata->jme_tx_prod = 0;
3009 tdata->jme_tx_cons = 0;
3010 tdata->jme_tx_cnt = 0;
3012 bzero(tdata->jme_tx_ring, JME_TX_RING_SIZE(tdata));
3013 for (i = 0; i < tdata->jme_tx_desc_cnt; i++) {
3014 txd = &tdata->jme_txdesc[i];
3016 txd->tx_desc = &tdata->jme_tx_ring[i];
3022 jme_init_ssb(struct jme_softc *sc)
3024 struct jme_chain_data *cd;
3026 cd = &sc->jme_cdata;
3027 bzero(cd->jme_ssb_block, JME_SSB_SIZE);
3031 jme_init_rx_ring(struct jme_rxdata *rdata)
3033 struct jme_rxdesc *rxd;
3036 KKASSERT(rdata->jme_rxhead == NULL &&
3037 rdata->jme_rxtail == NULL &&
3038 rdata->jme_rxlen == 0);
3039 rdata->jme_rx_cons = 0;
3041 bzero(rdata->jme_rx_ring, JME_RX_RING_SIZE(rdata));
3042 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
3045 rxd = &rdata->jme_rxdesc[i];
3047 rxd->rx_desc = &rdata->jme_rx_ring[i];
3048 error = jme_newbuf(rdata, rxd, 1);
3056 jme_newbuf(struct jme_rxdata *rdata, struct jme_rxdesc *rxd, int init)
3059 bus_dma_segment_t segs;
3063 m = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
3067 * JMC250 has 64bit boundary alignment limitation so jme(4)
3068 * takes advantage of 10 bytes padding feature of hardware
3069 * in order not to copy entire frame to align IP header on
3072 m->m_len = m->m_pkthdr.len = MCLBYTES;
3074 error = bus_dmamap_load_mbuf_segment(rdata->jme_rx_tag,
3075 rdata->jme_rx_sparemap, m, &segs, 1, &nsegs,
3080 if_printf(&rdata->jme_sc->arpcom.ac_if,
3081 "can't load RX mbuf\n");
3086 if (rxd->rx_m != NULL) {
3087 bus_dmamap_sync(rdata->jme_rx_tag, rxd->rx_dmamap,
3088 BUS_DMASYNC_POSTREAD);
3089 bus_dmamap_unload(rdata->jme_rx_tag, rxd->rx_dmamap);
3091 map = rxd->rx_dmamap;
3092 rxd->rx_dmamap = rdata->jme_rx_sparemap;
3093 rdata->jme_rx_sparemap = map;
3095 rxd->rx_paddr = segs.ds_addr;
3097 jme_setup_rxdesc(rxd);
3102 jme_set_vlan(struct jme_softc *sc)
3104 struct ifnet *ifp = &sc->arpcom.ac_if;
3107 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3109 reg = CSR_READ_4(sc, JME_RXMAC);
3110 reg &= ~RXMAC_VLAN_ENB;
3111 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
3112 reg |= RXMAC_VLAN_ENB;
3113 CSR_WRITE_4(sc, JME_RXMAC, reg);
3117 jme_set_filter(struct jme_softc *sc)
3119 struct ifnet *ifp = &sc->arpcom.ac_if;
3120 struct ifmultiaddr *ifma;
3125 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3127 rxcfg = CSR_READ_4(sc, JME_RXMAC);
3128 rxcfg &= ~(RXMAC_BROADCAST | RXMAC_PROMISC | RXMAC_MULTICAST |
3132 * Always accept frames destined to our station address.
3133 * Always accept broadcast frames.
3135 rxcfg |= RXMAC_UNICAST | RXMAC_BROADCAST;
3137 if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
3138 if (ifp->if_flags & IFF_PROMISC)
3139 rxcfg |= RXMAC_PROMISC;
3140 if (ifp->if_flags & IFF_ALLMULTI)
3141 rxcfg |= RXMAC_ALLMULTI;
3142 CSR_WRITE_4(sc, JME_MAR0, 0xFFFFFFFF);
3143 CSR_WRITE_4(sc, JME_MAR1, 0xFFFFFFFF);
3144 CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
3149 * Set up the multicast address filter by passing all multicast
3150 * addresses through a CRC generator, and then using the low-order
3151 * 6 bits as an index into the 64 bit multicast hash table. The
3152 * high order bits select the register, while the rest of the bits
3153 * select the bit within the register.
3155 rxcfg |= RXMAC_MULTICAST;
3156 bzero(mchash, sizeof(mchash));
3158 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3159 if (ifma->ifma_addr->sa_family != AF_LINK)
3161 crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
3162 ifma->ifma_addr), ETHER_ADDR_LEN);
3164 /* Just want the 6 least significant bits. */
3167 /* Set the corresponding bit in the hash table. */
3168 mchash[crc >> 5] |= 1 << (crc & 0x1f);
3171 CSR_WRITE_4(sc, JME_MAR0, mchash[0]);
3172 CSR_WRITE_4(sc, JME_MAR1, mchash[1]);
3173 CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
3177 jme_sysctl_tx_coal_to(SYSCTL_HANDLER_ARGS)
3179 struct jme_softc *sc = arg1;
3180 struct ifnet *ifp = &sc->arpcom.ac_if;
3183 ifnet_serialize_all(ifp);
3185 v = sc->jme_tx_coal_to;
3186 error = sysctl_handle_int(oidp, &v, 0, req);
3187 if (error || req->newptr == NULL)
3190 if (v < PCCTX_COAL_TO_MIN || v > PCCTX_COAL_TO_MAX) {
3195 if (v != sc->jme_tx_coal_to) {
3196 sc->jme_tx_coal_to = v;
3197 if (ifp->if_flags & IFF_RUNNING)
3198 jme_set_tx_coal(sc);
3201 ifnet_deserialize_all(ifp);
3206 jme_sysctl_tx_coal_pkt(SYSCTL_HANDLER_ARGS)
3208 struct jme_softc *sc = arg1;
3209 struct ifnet *ifp = &sc->arpcom.ac_if;
3212 ifnet_serialize_all(ifp);
3214 v = sc->jme_tx_coal_pkt;
3215 error = sysctl_handle_int(oidp, &v, 0, req);
3216 if (error || req->newptr == NULL)
3219 if (v < PCCTX_COAL_PKT_MIN || v > PCCTX_COAL_PKT_MAX) {
3224 if (v != sc->jme_tx_coal_pkt) {
3225 sc->jme_tx_coal_pkt = v;
3226 if (ifp->if_flags & IFF_RUNNING)
3227 jme_set_tx_coal(sc);
3230 ifnet_deserialize_all(ifp);
3235 jme_sysctl_rx_coal_to(SYSCTL_HANDLER_ARGS)
3237 struct jme_softc *sc = arg1;
3238 struct ifnet *ifp = &sc->arpcom.ac_if;
3241 ifnet_serialize_all(ifp);
3243 v = sc->jme_rx_coal_to;
3244 error = sysctl_handle_int(oidp, &v, 0, req);
3245 if (error || req->newptr == NULL)
3248 if (v < PCCRX_COAL_TO_MIN || v > PCCRX_COAL_TO_MAX) {
3253 if (v != sc->jme_rx_coal_to) {
3254 sc->jme_rx_coal_to = v;
3255 if (ifp->if_flags & IFF_RUNNING)
3256 jme_set_rx_coal(sc);
3259 ifnet_deserialize_all(ifp);
3264 jme_sysctl_rx_coal_pkt(SYSCTL_HANDLER_ARGS)
3266 struct jme_softc *sc = arg1;
3267 struct ifnet *ifp = &sc->arpcom.ac_if;
3270 ifnet_serialize_all(ifp);
3272 v = sc->jme_rx_coal_pkt;
3273 error = sysctl_handle_int(oidp, &v, 0, req);
3274 if (error || req->newptr == NULL)
3277 if (v < PCCRX_COAL_PKT_MIN || v > PCCRX_COAL_PKT_MAX) {
3282 if (v != sc->jme_rx_coal_pkt) {
3283 sc->jme_rx_coal_pkt = v;
3284 if (ifp->if_flags & IFF_RUNNING)
3285 jme_set_rx_coal(sc);
3288 ifnet_deserialize_all(ifp);
3293 jme_set_tx_coal(struct jme_softc *sc)
3297 reg = (sc->jme_tx_coal_to << PCCTX_COAL_TO_SHIFT) &
3299 reg |= (sc->jme_tx_coal_pkt << PCCTX_COAL_PKT_SHIFT) &
3300 PCCTX_COAL_PKT_MASK;
3301 reg |= PCCTX_COAL_TXQ0;
3302 CSR_WRITE_4(sc, JME_PCCTX, reg);
3306 jme_set_rx_coal(struct jme_softc *sc)
3311 reg = (sc->jme_rx_coal_to << PCCRX_COAL_TO_SHIFT) &
3313 reg |= (sc->jme_rx_coal_pkt << PCCRX_COAL_PKT_SHIFT) &
3314 PCCRX_COAL_PKT_MASK;
3315 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r)
3316 CSR_WRITE_4(sc, JME_PCCRX(r), reg);
3319 #ifdef IFPOLL_ENABLE
3322 jme_npoll_status(struct ifnet *ifp)
3324 struct jme_softc *sc = ifp->if_softc;
3327 ASSERT_SERIALIZED(&sc->jme_serialize);
3329 status = CSR_READ_4(sc, JME_INTR_STATUS);
3330 if (status & INTR_RXQ_DESC_EMPTY) {
3331 CSR_WRITE_4(sc, JME_INTR_STATUS, status & INTR_RXQ_DESC_EMPTY);
3332 jme_rx_restart(sc, status);
3337 jme_npoll_rx(struct ifnet *ifp __unused, void *arg, int cycle)
3339 struct jme_rxdata *rdata = arg;
3341 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3343 jme_rxeof(rdata, cycle);
3347 jme_npoll_tx(struct ifnet *ifp, void *arg, int cycle __unused)
3349 struct jme_txdata *tdata = arg;
3351 ASSERT_SERIALIZED(&tdata->jme_tx_serialize);
3354 if (!ifq_is_empty(&ifp->if_snd))
3359 jme_npoll(struct ifnet *ifp, struct ifpoll_info *info)
3361 struct jme_softc *sc = ifp->if_softc;
3363 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3368 info->ifpi_status.status_func = jme_npoll_status;
3369 info->ifpi_status.serializer = &sc->jme_serialize;
3371 off = sc->jme_npoll_txoff;
3372 KKASSERT(off <= ncpus2);
3373 info->ifpi_tx[off].poll_func = jme_npoll_tx;
3374 info->ifpi_tx[off].arg = &sc->jme_cdata.jme_tx_data;
3375 info->ifpi_tx[off].serializer =
3376 &sc->jme_cdata.jme_tx_data.jme_tx_serialize;
3377 ifq_set_cpuid(&ifp->if_snd, sc->jme_npoll_txoff);
3379 off = sc->jme_npoll_rxoff;
3380 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
3381 struct jme_rxdata *rdata =
3382 &sc->jme_cdata.jme_rx_data[i];
3385 info->ifpi_rx[idx].poll_func = jme_npoll_rx;
3386 info->ifpi_rx[idx].arg = rdata;
3387 info->ifpi_rx[idx].serializer =
3388 &rdata->jme_rx_serialize;
3391 if (ifp->if_flags & IFF_RUNNING)
3392 jme_disable_intr(sc);
3394 ifq_set_cpuid(&ifp->if_snd, sc->jme_tx_cpuid);
3395 if (ifp->if_flags & IFF_RUNNING)
3396 jme_enable_intr(sc);
3401 jme_sysctl_npoll_rxoff(SYSCTL_HANDLER_ARGS)
3403 struct jme_softc *sc = (void *)arg1;
3404 struct ifnet *ifp = &sc->arpcom.ac_if;
3407 off = sc->jme_npoll_rxoff;
3408 error = sysctl_handle_int(oidp, &off, 0, req);
3409 if (error || req->newptr == NULL)
3414 ifnet_serialize_all(ifp);
3415 if (off >= ncpus2 || off % sc->jme_cdata.jme_rx_ring_cnt != 0) {
3419 sc->jme_npoll_rxoff = off;
3421 ifnet_deserialize_all(ifp);
3427 jme_sysctl_npoll_txoff(SYSCTL_HANDLER_ARGS)
3429 struct jme_softc *sc = (void *)arg1;
3430 struct ifnet *ifp = &sc->arpcom.ac_if;
3433 off = sc->jme_npoll_txoff;
3434 error = sysctl_handle_int(oidp, &off, 0, req);
3435 if (error || req->newptr == NULL)
3440 ifnet_serialize_all(ifp);
3441 if (off >= ncpus2) {
3445 sc->jme_npoll_txoff = off;
3447 ifnet_deserialize_all(ifp);
3452 #endif /* IFPOLL_ENABLE */
3455 jme_rxring_dma_alloc(struct jme_rxdata *rdata)
3460 asize = roundup2(JME_RX_RING_SIZE(rdata), JME_RX_RING_ALIGN);
3461 error = bus_dmamem_coherent(rdata->jme_sc->jme_cdata.jme_ring_tag,
3462 JME_RX_RING_ALIGN, 0,
3463 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3464 asize, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
3466 device_printf(rdata->jme_sc->jme_dev,
3467 "could not allocate %dth Rx ring.\n", rdata->jme_rx_idx);
3470 rdata->jme_rx_ring_tag = dmem.dmem_tag;
3471 rdata->jme_rx_ring_map = dmem.dmem_map;
3472 rdata->jme_rx_ring = dmem.dmem_addr;
3473 rdata->jme_rx_ring_paddr = dmem.dmem_busaddr;
3479 jme_rxbuf_dma_filter(void *arg __unused, bus_addr_t paddr)
3481 if ((paddr & 0xffffffff) == 0) {
3483 * Don't allow lower 32bits of the RX buffer's
3484 * physical address to be 0, else it will break
3485 * hardware pending RSS information delivery
3486 * detection on RX path.
3494 jme_rxbuf_dma_alloc(struct jme_rxdata *rdata)
3499 lowaddr = BUS_SPACE_MAXADDR;
3500 if (JME_ENABLE_HWRSS(rdata->jme_sc)) {
3501 /* jme_rxbuf_dma_filter will be called */
3502 lowaddr = BUS_SPACE_MAXADDR_32BIT;
3505 /* Create tag for Rx buffers. */
3506 error = bus_dma_tag_create(
3507 rdata->jme_sc->jme_cdata.jme_buffer_tag,/* parent */
3508 JME_RX_BUF_ALIGN, 0, /* algnmnt, boundary */
3509 lowaddr, /* lowaddr */
3510 BUS_SPACE_MAXADDR, /* highaddr */
3511 jme_rxbuf_dma_filter, NULL, /* filter, filterarg */
3512 MCLBYTES, /* maxsize */
3514 MCLBYTES, /* maxsegsize */
3515 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ALIGNED,/* flags */
3516 &rdata->jme_rx_tag);
3518 device_printf(rdata->jme_sc->jme_dev,
3519 "could not create %dth Rx DMA tag.\n", rdata->jme_rx_idx);
3523 /* Create DMA maps for Rx buffers. */
3524 error = bus_dmamap_create(rdata->jme_rx_tag, BUS_DMA_WAITOK,
3525 &rdata->jme_rx_sparemap);
3527 device_printf(rdata->jme_sc->jme_dev,
3528 "could not create %dth spare Rx dmamap.\n",
3530 bus_dma_tag_destroy(rdata->jme_rx_tag);
3531 rdata->jme_rx_tag = NULL;
3534 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
3535 struct jme_rxdesc *rxd = &rdata->jme_rxdesc[i];
3537 error = bus_dmamap_create(rdata->jme_rx_tag, BUS_DMA_WAITOK,
3542 device_printf(rdata->jme_sc->jme_dev,
3543 "could not create %dth Rx dmamap "
3544 "for %dth RX ring.\n", i, rdata->jme_rx_idx);
3546 for (j = 0; j < i; ++j) {
3547 rxd = &rdata->jme_rxdesc[j];
3548 bus_dmamap_destroy(rdata->jme_rx_tag,
3551 bus_dmamap_destroy(rdata->jme_rx_tag,
3552 rdata->jme_rx_sparemap);
3553 bus_dma_tag_destroy(rdata->jme_rx_tag);
3554 rdata->jme_rx_tag = NULL;
3562 jme_rx_intr(struct jme_softc *sc, uint32_t status)
3566 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
3567 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
3569 if (status & rdata->jme_rx_coal) {
3570 lwkt_serialize_enter(&rdata->jme_rx_serialize);
3571 jme_rxeof(rdata, -1);
3572 lwkt_serialize_exit(&rdata->jme_rx_serialize);
3578 jme_enable_rss(struct jme_softc *sc)
3581 uint8_t key[RSSKEY_NREGS * RSSKEY_REGSIZE];
3584 KASSERT(sc->jme_cdata.jme_rx_ring_cnt == JME_NRXRING_2 ||
3585 sc->jme_cdata.jme_rx_ring_cnt == JME_NRXRING_4,
3586 ("%s: invalid # of RX rings (%d)",
3587 sc->arpcom.ac_if.if_xname, sc->jme_cdata.jme_rx_ring_cnt));
3589 rssc = RSSC_HASH_64_ENTRY;
3590 rssc |= RSSC_HASH_IPV4 | RSSC_HASH_IPV4_TCP;
3591 rssc |= sc->jme_cdata.jme_rx_ring_cnt >> 1;
3592 JME_RSS_DPRINTF(sc, 1, "rssc 0x%08x\n", rssc);
3593 CSR_WRITE_4(sc, JME_RSSC, rssc);
3595 toeplitz_get_key(key, sizeof(key));
3596 for (i = 0; i < RSSKEY_NREGS; ++i) {
3599 keyreg = RSSKEY_REGVAL(key, i);
3600 JME_RSS_DPRINTF(sc, 5, "keyreg%d 0x%08x, reg 0x%08x\n",
3601 i, keyreg, RSSKEY_REG(RSSKEY_NREGS - 1 - i));
3603 CSR_WRITE_4(sc, RSSKEY_REG(RSSKEY_NREGS - 1 - i), keyreg);
3607 * Create redirect table in following fashion:
3608 * (hash & ring_cnt_mask) == rdr_table[(hash & rdr_table_mask)]
3611 for (i = 0; i < RSSTBL_REGSIZE; ++i) {
3614 q = i % sc->jme_cdata.jme_rx_ring_cnt;
3615 ind |= q << (i * 8);
3617 JME_RSS_DPRINTF(sc, 1, "ind 0x%08x\n", ind);
3619 for (i = 0; i < RSSTBL_NREGS; ++i)
3620 CSR_WRITE_4(sc, RSSTBL_REG(i), ind);
3624 jme_disable_rss(struct jme_softc *sc)
3626 CSR_WRITE_4(sc, JME_RSSC, RSSC_DIS_RSS);
3630 jme_serialize(struct ifnet *ifp, enum ifnet_serialize slz)
3632 struct jme_softc *sc = ifp->if_softc;
3634 ifnet_serialize_array_enter(sc->jme_serialize_arr,
3635 sc->jme_serialize_cnt, JME_TX_SERIALIZE, JME_RX_SERIALIZE, slz);
3639 jme_deserialize(struct ifnet *ifp, enum ifnet_serialize slz)
3641 struct jme_softc *sc = ifp->if_softc;
3643 ifnet_serialize_array_exit(sc->jme_serialize_arr,
3644 sc->jme_serialize_cnt, JME_TX_SERIALIZE, JME_RX_SERIALIZE, slz);
3648 jme_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz)
3650 struct jme_softc *sc = ifp->if_softc;
3652 return ifnet_serialize_array_try(sc->jme_serialize_arr,
3653 sc->jme_serialize_cnt, JME_TX_SERIALIZE, JME_RX_SERIALIZE, slz);
3659 jme_serialize_assert(struct ifnet *ifp, enum ifnet_serialize slz,
3660 boolean_t serialized)
3662 struct jme_softc *sc = ifp->if_softc;
3664 ifnet_serialize_array_assert(sc->jme_serialize_arr,
3665 sc->jme_serialize_cnt, JME_TX_SERIALIZE, JME_RX_SERIALIZE,
3669 #endif /* INVARIANTS */
3672 jme_msix_try_alloc(device_t dev)
3674 struct jme_softc *sc = device_get_softc(dev);
3675 struct jme_msix_data *msix;
3676 int error, i, r, msix_enable, msix_count;
3677 int offset, offset_def;
3679 msix_count = JME_MSIXCNT(sc->jme_cdata.jme_rx_ring_cnt);
3680 KKASSERT(msix_count <= JME_NMSIX);
3682 msix_enable = device_getenv_int(dev, "msix.enable", jme_msix_enable);
3685 * We leave the 1st MSI-X vector unused, so we
3686 * actually need msix_count + 1 MSI-X vectors.
3688 if (!msix_enable || pci_msix_count(dev) < (msix_count + 1))
3691 for (i = 0; i < msix_count; ++i)
3692 sc->jme_msix[i].jme_msix_rid = -1;
3697 * Setup status MSI-X
3700 msix = &sc->jme_msix[i++];
3701 msix->jme_msix_cpuid = 0;
3702 msix->jme_msix_arg = sc;
3703 msix->jme_msix_func = jme_msix_status;
3704 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
3705 msix->jme_msix_intrs |=
3706 sc->jme_cdata.jme_rx_data[r].jme_rx_empty;
3708 msix->jme_msix_serialize = &sc->jme_serialize;
3709 ksnprintf(msix->jme_msix_desc, sizeof(msix->jme_msix_desc), "%s sts",
3710 device_get_nameunit(dev));
3716 offset_def = device_get_unit(dev) % ncpus2;
3717 offset = device_getenv_int(dev, "msix.txoff", offset_def);
3718 if (offset >= ncpus2) {
3719 device_printf(dev, "invalid msix.txoff %d, use %d\n",
3720 offset, offset_def);
3721 offset = offset_def;
3724 msix = &sc->jme_msix[i++];
3725 msix->jme_msix_cpuid = offset;
3726 sc->jme_tx_cpuid = msix->jme_msix_cpuid;
3727 msix->jme_msix_arg = &sc->jme_cdata.jme_tx_data;
3728 msix->jme_msix_func = jme_msix_tx;
3729 msix->jme_msix_intrs = INTR_TXQ_COAL | INTR_TXQ_COAL_TO;
3730 msix->jme_msix_serialize = &sc->jme_cdata.jme_tx_data.jme_tx_serialize;
3731 ksnprintf(msix->jme_msix_desc, sizeof(msix->jme_msix_desc), "%s tx",
3732 device_get_nameunit(dev));
3738 if (sc->jme_cdata.jme_rx_ring_cnt == ncpus2) {
3741 offset_def = (sc->jme_cdata.jme_rx_ring_cnt *
3742 device_get_unit(dev)) % ncpus2;
3744 offset = device_getenv_int(dev, "msix.rxoff", offset_def);
3745 if (offset >= ncpus2 ||
3746 offset % sc->jme_cdata.jme_rx_ring_cnt != 0) {
3747 device_printf(dev, "invalid msix.rxoff %d, use %d\n",
3748 offset, offset_def);
3749 offset = offset_def;
3753 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
3754 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
3756 msix = &sc->jme_msix[i++];
3757 msix->jme_msix_cpuid = r + offset;
3758 KKASSERT(msix->jme_msix_cpuid < ncpus2);
3759 msix->jme_msix_arg = rdata;
3760 msix->jme_msix_func = jme_msix_rx;
3761 msix->jme_msix_intrs = rdata->jme_rx_coal;
3762 msix->jme_msix_serialize = &rdata->jme_rx_serialize;
3763 ksnprintf(msix->jme_msix_desc, sizeof(msix->jme_msix_desc),
3764 "%s rx%d", device_get_nameunit(dev), r);
3767 KKASSERT(i == msix_count);
3769 error = pci_setup_msix(dev);
3773 /* Setup jme_msix_cnt early, so we could cleanup */
3774 sc->jme_msix_cnt = msix_count;
3776 for (i = 0; i < msix_count; ++i) {
3777 msix = &sc->jme_msix[i];
3779 msix->jme_msix_vector = i + 1;
3780 error = pci_alloc_msix_vector(dev, msix->jme_msix_vector,
3781 &msix->jme_msix_rid, msix->jme_msix_cpuid);
3785 msix->jme_msix_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
3786 &msix->jme_msix_rid, RF_ACTIVE);
3787 if (msix->jme_msix_res == NULL) {
3793 for (i = 0; i < JME_INTR_CNT; ++i) {
3794 uint32_t intr_mask = (1 << i);
3797 if ((JME_INTRS & intr_mask) == 0)
3800 for (x = 0; x < msix_count; ++x) {
3801 msix = &sc->jme_msix[x];
3802 if (msix->jme_msix_intrs & intr_mask) {
3805 reg = i / JME_MSINUM_FACTOR;
3806 KKASSERT(reg < JME_MSINUM_CNT);
3808 shift = (i % JME_MSINUM_FACTOR) * 4;
3810 sc->jme_msinum[reg] |=
3811 (msix->jme_msix_vector << shift);
3819 for (i = 0; i < JME_MSINUM_CNT; ++i) {
3820 device_printf(dev, "MSINUM%d: %#x\n", i,
3825 pci_enable_msix(dev);
3826 sc->jme_irq_type = PCI_INTR_TYPE_MSIX;
3834 jme_intr_alloc(device_t dev)
3836 struct jme_softc *sc = device_get_softc(dev);
3839 jme_msix_try_alloc(dev);
3841 if (sc->jme_irq_type != PCI_INTR_TYPE_MSIX) {
3842 sc->jme_irq_type = pci_alloc_1intr(dev, jme_msi_enable,
3843 &sc->jme_irq_rid, &irq_flags);
3845 sc->jme_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
3846 &sc->jme_irq_rid, irq_flags);
3847 if (sc->jme_irq_res == NULL) {
3848 device_printf(dev, "can't allocate irq\n");
3851 sc->jme_tx_cpuid = rman_get_cpuid(sc->jme_irq_res);
3857 jme_msix_free(device_t dev)
3859 struct jme_softc *sc = device_get_softc(dev);
3862 KKASSERT(sc->jme_msix_cnt > 1);
3864 for (i = 0; i < sc->jme_msix_cnt; ++i) {
3865 struct jme_msix_data *msix = &sc->jme_msix[i];
3867 if (msix->jme_msix_res != NULL) {
3868 bus_release_resource(dev, SYS_RES_IRQ,
3869 msix->jme_msix_rid, msix->jme_msix_res);
3870 msix->jme_msix_res = NULL;
3872 if (msix->jme_msix_rid >= 0) {
3873 pci_release_msix_vector(dev, msix->jme_msix_rid);
3874 msix->jme_msix_rid = -1;
3877 pci_teardown_msix(dev);
3881 jme_intr_free(device_t dev)
3883 struct jme_softc *sc = device_get_softc(dev);
3885 if (sc->jme_irq_type != PCI_INTR_TYPE_MSIX) {
3886 if (sc->jme_irq_res != NULL) {
3887 bus_release_resource(dev, SYS_RES_IRQ, sc->jme_irq_rid,
3890 if (sc->jme_irq_type == PCI_INTR_TYPE_MSI)
3891 pci_release_msi(dev);
3898 jme_msix_tx(void *xtdata)
3900 struct jme_txdata *tdata = xtdata;
3901 struct jme_softc *sc = tdata->jme_sc;
3902 struct ifnet *ifp = &sc->arpcom.ac_if;
3904 ASSERT_SERIALIZED(&tdata->jme_tx_serialize);
3906 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, INTR_TXQ_COAL | INTR_TXQ_COAL_TO);
3908 CSR_WRITE_4(sc, JME_INTR_STATUS,
3909 INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP);
3911 if (ifp->if_flags & IFF_RUNNING) {
3913 if (!ifq_is_empty(&ifp->if_snd))
3917 CSR_WRITE_4(sc, JME_INTR_MASK_SET, INTR_TXQ_COAL | INTR_TXQ_COAL_TO);
3921 jme_msix_rx(void *xrdata)
3923 struct jme_rxdata *rdata = xrdata;
3924 struct jme_softc *sc = rdata->jme_sc;
3925 struct ifnet *ifp = &sc->arpcom.ac_if;
3927 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3929 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, rdata->jme_rx_coal);
3931 CSR_WRITE_4(sc, JME_INTR_STATUS,
3932 rdata->jme_rx_coal | rdata->jme_rx_comp);
3934 if (ifp->if_flags & IFF_RUNNING)
3935 jme_rxeof(rdata, -1);
3937 CSR_WRITE_4(sc, JME_INTR_MASK_SET, rdata->jme_rx_coal);
3941 jme_msix_status(void *xsc)
3943 struct jme_softc *sc = xsc;
3944 struct ifnet *ifp = &sc->arpcom.ac_if;
3947 ASSERT_SERIALIZED(&sc->jme_serialize);
3949 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, INTR_RXQ_DESC_EMPTY);
3951 status = CSR_READ_4(sc, JME_INTR_STATUS);
3953 if (status & INTR_RXQ_DESC_EMPTY) {
3954 CSR_WRITE_4(sc, JME_INTR_STATUS, status & INTR_RXQ_DESC_EMPTY);
3955 if (ifp->if_flags & IFF_RUNNING)
3956 jme_rx_restart(sc, status);
3959 CSR_WRITE_4(sc, JME_INTR_MASK_SET, INTR_RXQ_DESC_EMPTY);
3963 jme_rx_restart(struct jme_softc *sc, uint32_t status)
3967 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
3968 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[i];
3970 if (status & rdata->jme_rx_empty) {
3971 lwkt_serialize_enter(&rdata->jme_rx_serialize);
3972 jme_rxeof(rdata, -1);
3973 #ifdef JME_RSS_DEBUG
3974 rdata->jme_rx_emp++;
3976 lwkt_serialize_exit(&rdata->jme_rx_serialize);
3979 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RX_ENB |
3984 jme_set_msinum(struct jme_softc *sc)
3988 for (i = 0; i < JME_MSINUM_CNT; ++i)
3989 CSR_WRITE_4(sc, JME_MSINUM(i), sc->jme_msinum[i]);
3993 jme_intr_setup(device_t dev)
3995 struct jme_softc *sc = device_get_softc(dev);
3998 if (sc->jme_irq_type == PCI_INTR_TYPE_MSIX)
3999 return jme_msix_setup(dev);
4001 error = bus_setup_intr(dev, sc->jme_irq_res, INTR_MPSAFE,
4002 jme_intr, sc, &sc->jme_irq_handle, &sc->jme_serialize);
4004 device_printf(dev, "could not set up interrupt handler.\n");
4012 jme_intr_teardown(device_t dev)
4014 struct jme_softc *sc = device_get_softc(dev);
4016 if (sc->jme_irq_type == PCI_INTR_TYPE_MSIX)
4017 jme_msix_teardown(dev, sc->jme_msix_cnt);
4019 bus_teardown_intr(dev, sc->jme_irq_res, sc->jme_irq_handle);
4023 jme_msix_setup(device_t dev)
4025 struct jme_softc *sc = device_get_softc(dev);
4028 for (x = 0; x < sc->jme_msix_cnt; ++x) {
4029 struct jme_msix_data *msix = &sc->jme_msix[x];
4032 error = bus_setup_intr_descr(dev, msix->jme_msix_res,
4033 INTR_MPSAFE, msix->jme_msix_func, msix->jme_msix_arg,
4034 &msix->jme_msix_handle, msix->jme_msix_serialize,
4035 msix->jme_msix_desc);
4037 device_printf(dev, "could not set up %s "
4038 "interrupt handler.\n", msix->jme_msix_desc);
4039 jme_msix_teardown(dev, x);
4047 jme_msix_teardown(device_t dev, int msix_count)
4049 struct jme_softc *sc = device_get_softc(dev);
4052 for (x = 0; x < msix_count; ++x) {
4053 struct jme_msix_data *msix = &sc->jme_msix[x];
4055 bus_teardown_intr(dev, msix->jme_msix_res,
4056 msix->jme_msix_handle);
4061 jme_serialize_skipmain(struct jme_softc *sc)
4063 lwkt_serialize_array_enter(sc->jme_serialize_arr,
4064 sc->jme_serialize_cnt, 1);
4068 jme_deserialize_skipmain(struct jme_softc *sc)
4070 lwkt_serialize_array_exit(sc->jme_serialize_arr,
4071 sc->jme_serialize_cnt, 1);
4075 jme_enable_intr(struct jme_softc *sc)
4079 for (i = 0; i < sc->jme_serialize_cnt; ++i)
4080 lwkt_serialize_handler_enable(sc->jme_serialize_arr[i]);
4082 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
4086 jme_disable_intr(struct jme_softc *sc)
4090 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
4092 for (i = 0; i < sc->jme_serialize_cnt; ++i)
4093 lwkt_serialize_handler_disable(sc->jme_serialize_arr[i]);