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);
1068 ifsq_set_hw_serialize(ifq_get_subq_default(&ifp->if_snd),
1069 &sc->jme_cdata.jme_tx_data.jme_tx_serialize);
1071 error = jme_intr_setup(dev);
1073 ether_ifdetach(ifp);
1084 jme_detach(device_t dev)
1086 struct jme_softc *sc = device_get_softc(dev);
1088 if (device_is_attached(dev)) {
1089 struct ifnet *ifp = &sc->arpcom.ac_if;
1091 ifnet_serialize_all(ifp);
1093 jme_intr_teardown(dev);
1094 ifnet_deserialize_all(ifp);
1096 ether_ifdetach(ifp);
1099 if (sc->jme_sysctl_tree != NULL)
1100 sysctl_ctx_free(&sc->jme_sysctl_ctx);
1102 if (sc->jme_miibus != NULL)
1103 device_delete_child(dev, sc->jme_miibus);
1104 bus_generic_detach(dev);
1108 if (sc->jme_mem_res != NULL) {
1109 bus_release_resource(dev, SYS_RES_MEMORY, sc->jme_mem_rid,
1119 jme_sysctl_node(struct jme_softc *sc)
1121 #ifdef JME_RSS_DEBUG
1125 sysctl_ctx_init(&sc->jme_sysctl_ctx);
1126 sc->jme_sysctl_tree = SYSCTL_ADD_NODE(&sc->jme_sysctl_ctx,
1127 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
1128 device_get_nameunit(sc->jme_dev),
1130 if (sc->jme_sysctl_tree == NULL) {
1131 device_printf(sc->jme_dev, "can't add sysctl node\n");
1135 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1136 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1137 "tx_coal_to", CTLTYPE_INT | CTLFLAG_RW,
1138 sc, 0, jme_sysctl_tx_coal_to, "I", "jme tx coalescing timeout");
1140 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1141 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1142 "tx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW,
1143 sc, 0, jme_sysctl_tx_coal_pkt, "I", "jme tx coalescing packet");
1145 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1146 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1147 "rx_coal_to", CTLTYPE_INT | CTLFLAG_RW,
1148 sc, 0, jme_sysctl_rx_coal_to, "I", "jme rx coalescing timeout");
1150 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1151 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1152 "rx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW,
1153 sc, 0, jme_sysctl_rx_coal_pkt, "I", "jme rx coalescing packet");
1155 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1156 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1157 "rx_desc_count", CTLFLAG_RD,
1158 &sc->jme_cdata.jme_rx_data[0].jme_rx_desc_cnt,
1159 0, "RX desc count");
1160 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1161 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1162 "tx_desc_count", CTLFLAG_RD,
1163 &sc->jme_cdata.jme_tx_data.jme_tx_desc_cnt,
1164 0, "TX desc count");
1165 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1166 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1167 "rx_ring_count", CTLFLAG_RD,
1168 &sc->jme_cdata.jme_rx_ring_cnt,
1169 0, "RX ring count");
1170 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1171 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1172 "tx_wreg", CTLFLAG_RW,
1173 &sc->jme_cdata.jme_tx_data.jme_tx_wreg, 0,
1174 "# of segments before writing to hardware register");
1176 #ifdef JME_RSS_DEBUG
1177 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1178 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1179 "rss_debug", CTLFLAG_RW, &sc->jme_rss_debug,
1180 0, "RSS debug level");
1181 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1182 char rx_ring_desc[32];
1184 ksnprintf(rx_ring_desc, sizeof(rx_ring_desc),
1185 "rx_ring%d_pkt", r);
1186 SYSCTL_ADD_ULONG(&sc->jme_sysctl_ctx,
1187 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1188 rx_ring_desc, CTLFLAG_RW,
1189 &sc->jme_cdata.jme_rx_data[r].jme_rx_pkt, "RXed packets");
1191 ksnprintf(rx_ring_desc, sizeof(rx_ring_desc),
1192 "rx_ring%d_emp", r);
1193 SYSCTL_ADD_ULONG(&sc->jme_sysctl_ctx,
1194 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1195 rx_ring_desc, CTLFLAG_RW,
1196 &sc->jme_cdata.jme_rx_data[r].jme_rx_emp,
1197 "# of time RX ring empty");
1201 #ifdef IFPOLL_ENABLE
1202 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1203 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1204 "npoll_rxoff", CTLTYPE_INT|CTLFLAG_RW, sc, 0,
1205 jme_sysctl_npoll_rxoff, "I", "NPOLLING RX cpu offset");
1206 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1207 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1208 "npoll_txoff", CTLTYPE_INT|CTLFLAG_RW, sc, 0,
1209 jme_sysctl_npoll_txoff, "I", "NPOLLING TX cpu offset");
1214 jme_dma_alloc(struct jme_softc *sc)
1216 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
1217 struct jme_txdesc *txd;
1219 int error, i, asize;
1221 asize = __VM_CACHELINE_ALIGN(
1222 tdata->jme_tx_desc_cnt * sizeof(struct jme_txdesc));
1223 tdata->jme_txdesc = kmalloc_cachealign(asize, M_DEVBUF,
1226 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
1227 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[i];
1229 asize = __VM_CACHELINE_ALIGN(
1230 rdata->jme_rx_desc_cnt * sizeof(struct jme_rxdesc));
1231 rdata->jme_rxdesc = kmalloc_cachealign(asize, M_DEVBUF,
1235 /* Create parent ring tag. */
1236 error = bus_dma_tag_create(NULL,/* parent */
1237 1, JME_RING_BOUNDARY, /* algnmnt, boundary */
1238 sc->jme_lowaddr, /* lowaddr */
1239 BUS_SPACE_MAXADDR, /* highaddr */
1240 NULL, NULL, /* filter, filterarg */
1241 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1243 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1245 &sc->jme_cdata.jme_ring_tag);
1247 device_printf(sc->jme_dev,
1248 "could not create parent ring DMA tag.\n");
1253 * Create DMA stuffs for TX ring
1255 asize = roundup2(JME_TX_RING_SIZE(tdata), JME_TX_RING_ALIGN);
1256 error = bus_dmamem_coherent(sc->jme_cdata.jme_ring_tag,
1257 JME_TX_RING_ALIGN, 0,
1258 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1259 asize, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
1261 device_printf(sc->jme_dev, "could not allocate Tx ring.\n");
1264 tdata->jme_tx_ring_tag = dmem.dmem_tag;
1265 tdata->jme_tx_ring_map = dmem.dmem_map;
1266 tdata->jme_tx_ring = dmem.dmem_addr;
1267 tdata->jme_tx_ring_paddr = dmem.dmem_busaddr;
1270 * Create DMA stuffs for RX rings
1272 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
1273 error = jme_rxring_dma_alloc(&sc->jme_cdata.jme_rx_data[i]);
1278 /* Create parent buffer tag. */
1279 error = bus_dma_tag_create(NULL,/* parent */
1280 1, 0, /* algnmnt, boundary */
1281 sc->jme_lowaddr, /* lowaddr */
1282 BUS_SPACE_MAXADDR, /* highaddr */
1283 NULL, NULL, /* filter, filterarg */
1284 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1286 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1288 &sc->jme_cdata.jme_buffer_tag);
1290 device_printf(sc->jme_dev,
1291 "could not create parent buffer DMA tag.\n");
1296 * Create DMA stuffs for shadow status block
1298 asize = roundup2(JME_SSB_SIZE, JME_SSB_ALIGN);
1299 error = bus_dmamem_coherent(sc->jme_cdata.jme_buffer_tag,
1300 JME_SSB_ALIGN, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1301 asize, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
1303 device_printf(sc->jme_dev,
1304 "could not create shadow status block.\n");
1307 sc->jme_cdata.jme_ssb_tag = dmem.dmem_tag;
1308 sc->jme_cdata.jme_ssb_map = dmem.dmem_map;
1309 sc->jme_cdata.jme_ssb_block = dmem.dmem_addr;
1310 sc->jme_cdata.jme_ssb_block_paddr = dmem.dmem_busaddr;
1313 * Create DMA stuffs for TX buffers
1316 /* Create tag for Tx buffers. */
1317 error = bus_dma_tag_create(sc->jme_cdata.jme_buffer_tag,/* parent */
1318 1, 0, /* algnmnt, boundary */
1319 BUS_SPACE_MAXADDR, /* lowaddr */
1320 BUS_SPACE_MAXADDR, /* highaddr */
1321 NULL, NULL, /* filter, filterarg */
1322 JME_TSO_MAXSIZE, /* maxsize */
1323 JME_MAXTXSEGS, /* nsegments */
1324 JME_MAXSEGSIZE, /* maxsegsize */
1325 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,/* flags */
1326 &tdata->jme_tx_tag);
1328 device_printf(sc->jme_dev, "could not create Tx DMA tag.\n");
1332 /* Create DMA maps for Tx buffers. */
1333 for (i = 0; i < tdata->jme_tx_desc_cnt; i++) {
1334 txd = &tdata->jme_txdesc[i];
1335 error = bus_dmamap_create(tdata->jme_tx_tag,
1336 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
1341 device_printf(sc->jme_dev,
1342 "could not create %dth Tx dmamap.\n", i);
1344 for (j = 0; j < i; ++j) {
1345 txd = &tdata->jme_txdesc[j];
1346 bus_dmamap_destroy(tdata->jme_tx_tag,
1349 bus_dma_tag_destroy(tdata->jme_tx_tag);
1350 tdata->jme_tx_tag = NULL;
1356 * Create DMA stuffs for RX buffers
1358 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
1359 error = jme_rxbuf_dma_alloc(&sc->jme_cdata.jme_rx_data[i]);
1367 jme_dma_free(struct jme_softc *sc)
1369 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
1370 struct jme_txdesc *txd;
1371 struct jme_rxdesc *rxd;
1372 struct jme_rxdata *rdata;
1376 if (tdata->jme_tx_ring_tag != NULL) {
1377 bus_dmamap_unload(tdata->jme_tx_ring_tag,
1378 tdata->jme_tx_ring_map);
1379 bus_dmamem_free(tdata->jme_tx_ring_tag,
1380 tdata->jme_tx_ring, tdata->jme_tx_ring_map);
1381 bus_dma_tag_destroy(tdata->jme_tx_ring_tag);
1382 tdata->jme_tx_ring_tag = NULL;
1386 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1387 rdata = &sc->jme_cdata.jme_rx_data[r];
1388 if (rdata->jme_rx_ring_tag != NULL) {
1389 bus_dmamap_unload(rdata->jme_rx_ring_tag,
1390 rdata->jme_rx_ring_map);
1391 bus_dmamem_free(rdata->jme_rx_ring_tag,
1393 rdata->jme_rx_ring_map);
1394 bus_dma_tag_destroy(rdata->jme_rx_ring_tag);
1395 rdata->jme_rx_ring_tag = NULL;
1400 if (tdata->jme_tx_tag != NULL) {
1401 for (i = 0; i < tdata->jme_tx_desc_cnt; i++) {
1402 txd = &tdata->jme_txdesc[i];
1403 bus_dmamap_destroy(tdata->jme_tx_tag, txd->tx_dmamap);
1405 bus_dma_tag_destroy(tdata->jme_tx_tag);
1406 tdata->jme_tx_tag = NULL;
1410 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1411 rdata = &sc->jme_cdata.jme_rx_data[r];
1412 if (rdata->jme_rx_tag != NULL) {
1413 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
1414 rxd = &rdata->jme_rxdesc[i];
1415 bus_dmamap_destroy(rdata->jme_rx_tag,
1418 bus_dmamap_destroy(rdata->jme_rx_tag,
1419 rdata->jme_rx_sparemap);
1420 bus_dma_tag_destroy(rdata->jme_rx_tag);
1421 rdata->jme_rx_tag = NULL;
1425 /* Shadow status block. */
1426 if (sc->jme_cdata.jme_ssb_tag != NULL) {
1427 bus_dmamap_unload(sc->jme_cdata.jme_ssb_tag,
1428 sc->jme_cdata.jme_ssb_map);
1429 bus_dmamem_free(sc->jme_cdata.jme_ssb_tag,
1430 sc->jme_cdata.jme_ssb_block,
1431 sc->jme_cdata.jme_ssb_map);
1432 bus_dma_tag_destroy(sc->jme_cdata.jme_ssb_tag);
1433 sc->jme_cdata.jme_ssb_tag = NULL;
1436 if (sc->jme_cdata.jme_buffer_tag != NULL) {
1437 bus_dma_tag_destroy(sc->jme_cdata.jme_buffer_tag);
1438 sc->jme_cdata.jme_buffer_tag = NULL;
1440 if (sc->jme_cdata.jme_ring_tag != NULL) {
1441 bus_dma_tag_destroy(sc->jme_cdata.jme_ring_tag);
1442 sc->jme_cdata.jme_ring_tag = NULL;
1445 if (tdata->jme_txdesc != NULL) {
1446 kfree(tdata->jme_txdesc, M_DEVBUF);
1447 tdata->jme_txdesc = NULL;
1449 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1450 rdata = &sc->jme_cdata.jme_rx_data[r];
1451 if (rdata->jme_rxdesc != NULL) {
1452 kfree(rdata->jme_rxdesc, M_DEVBUF);
1453 rdata->jme_rxdesc = NULL;
1459 * Make sure the interface is stopped at reboot time.
1462 jme_shutdown(device_t dev)
1464 return jme_suspend(dev);
1469 * Unlike other ethernet controllers, JMC250 requires
1470 * explicit resetting link speed to 10/100Mbps as gigabit
1471 * link will cunsume more power than 375mA.
1472 * Note, we reset the link speed to 10/100Mbps with
1473 * auto-negotiation but we don't know whether that operation
1474 * would succeed or not as we have no control after powering
1475 * off. If the renegotiation fail WOL may not work. Running
1476 * at 1Gbps draws more power than 375mA at 3.3V which is
1477 * specified in PCI specification and that would result in
1478 * complete shutdowning power to ethernet controller.
1481 * Save current negotiated media speed/duplex/flow-control
1482 * to softc and restore the same link again after resuming.
1483 * PHY handling such as power down/resetting to 100Mbps
1484 * may be better handled in suspend method in phy driver.
1487 jme_setlinkspeed(struct jme_softc *sc)
1489 struct mii_data *mii;
1492 JME_LOCK_ASSERT(sc);
1494 mii = device_get_softc(sc->jme_miibus);
1497 if ((mii->mii_media_status & IFM_AVALID) != 0) {
1498 switch IFM_SUBTYPE(mii->mii_media_active) {
1508 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_100T2CR, 0);
1509 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_ANAR,
1510 ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
1511 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_BMCR,
1512 BMCR_AUTOEN | BMCR_STARTNEG);
1515 /* Poll link state until jme(4) get a 10/100 link. */
1516 for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
1518 if ((mii->mii_media_status & IFM_AVALID) != 0) {
1519 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1529 pause("jmelnk", hz);
1532 if (i == MII_ANEGTICKS_GIGE)
1533 device_printf(sc->jme_dev, "establishing link failed, "
1534 "WOL may not work!");
1537 * No link, force MAC to have 100Mbps, full-duplex link.
1538 * This is the last resort and may/may not work.
1540 mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
1541 mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1546 jme_setwol(struct jme_softc *sc)
1548 struct ifnet *ifp = &sc->arpcom.ac_if;
1553 if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1554 /* No PME capability, PHY power down. */
1555 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1556 MII_BMCR, BMCR_PDOWN);
1560 gpr = CSR_READ_4(sc, JME_GPREG0) & ~GPREG0_PME_ENB;
1561 pmcs = CSR_READ_4(sc, JME_PMCS);
1562 pmcs &= ~PMCS_WOL_ENB_MASK;
1563 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) {
1564 pmcs |= PMCS_MAGIC_FRAME | PMCS_MAGIC_FRAME_ENB;
1565 /* Enable PME message. */
1566 gpr |= GPREG0_PME_ENB;
1567 /* For gigabit controllers, reset link speed to 10/100. */
1568 if ((sc->jme_caps & JME_CAP_FASTETH) == 0)
1569 jme_setlinkspeed(sc);
1572 CSR_WRITE_4(sc, JME_PMCS, pmcs);
1573 CSR_WRITE_4(sc, JME_GPREG0, gpr);
1576 pmstat = pci_read_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, 2);
1577 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
1578 if ((ifp->if_capenable & IFCAP_WOL) != 0)
1579 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
1580 pci_write_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1581 if ((ifp->if_capenable & IFCAP_WOL) == 0) {
1582 /* No WOL, PHY power down. */
1583 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1584 MII_BMCR, BMCR_PDOWN);
1590 jme_suspend(device_t dev)
1592 struct jme_softc *sc = device_get_softc(dev);
1593 struct ifnet *ifp = &sc->arpcom.ac_if;
1595 ifnet_serialize_all(ifp);
1600 ifnet_deserialize_all(ifp);
1606 jme_resume(device_t dev)
1608 struct jme_softc *sc = device_get_softc(dev);
1609 struct ifnet *ifp = &sc->arpcom.ac_if;
1614 ifnet_serialize_all(ifp);
1617 if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1620 pmstat = pci_read_config(sc->jme_dev,
1621 pmc + PCIR_POWER_STATUS, 2);
1622 /* Disable PME clear PME status. */
1623 pmstat &= ~PCIM_PSTAT_PMEENABLE;
1624 pci_write_config(sc->jme_dev,
1625 pmc + PCIR_POWER_STATUS, pmstat, 2);
1629 if (ifp->if_flags & IFF_UP)
1632 ifnet_deserialize_all(ifp);
1638 jme_tso_pullup(struct mbuf **mp)
1640 int hoff, iphlen, thoff;
1644 KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
1646 iphlen = m->m_pkthdr.csum_iphlen;
1647 thoff = m->m_pkthdr.csum_thlen;
1648 hoff = m->m_pkthdr.csum_lhlen;
1650 KASSERT(iphlen > 0, ("invalid ip hlen"));
1651 KASSERT(thoff > 0, ("invalid tcp hlen"));
1652 KASSERT(hoff > 0, ("invalid ether hlen"));
1654 if (__predict_false(m->m_len < hoff + iphlen + thoff)) {
1655 m = m_pullup(m, hoff + iphlen + thoff);
1666 jme_encap(struct jme_txdata *tdata, struct mbuf **m_head, int *segs_used)
1668 struct jme_txdesc *txd;
1669 struct jme_desc *desc;
1671 bus_dma_segment_t txsegs[JME_MAXTXSEGS];
1673 int error, i, prod, symbol_desc;
1674 uint32_t cflags, flag64, mss;
1676 M_ASSERTPKTHDR((*m_head));
1678 if ((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) {
1679 /* XXX Is this necessary? */
1680 error = jme_tso_pullup(m_head);
1685 prod = tdata->jme_tx_prod;
1686 txd = &tdata->jme_txdesc[prod];
1688 if (tdata->jme_sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT)
1693 maxsegs = (tdata->jme_tx_desc_cnt - tdata->jme_tx_cnt) -
1694 (JME_TXD_RSVD + symbol_desc);
1695 if (maxsegs > JME_MAXTXSEGS)
1696 maxsegs = JME_MAXTXSEGS;
1697 KASSERT(maxsegs >= (JME_TXD_SPARE - symbol_desc),
1698 ("not enough segments %d", maxsegs));
1700 error = bus_dmamap_load_mbuf_defrag(tdata->jme_tx_tag,
1701 txd->tx_dmamap, m_head,
1702 txsegs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
1705 *segs_used += nsegs;
1707 bus_dmamap_sync(tdata->jme_tx_tag, txd->tx_dmamap,
1708 BUS_DMASYNC_PREWRITE);
1714 /* Configure checksum offload. */
1715 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1716 mss = (uint32_t)m->m_pkthdr.tso_segsz << JME_TD_MSS_SHIFT;
1717 cflags |= JME_TD_TSO;
1718 } else if (m->m_pkthdr.csum_flags & JME_CSUM_FEATURES) {
1719 if (m->m_pkthdr.csum_flags & CSUM_IP)
1720 cflags |= JME_TD_IPCSUM;
1721 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1722 cflags |= JME_TD_TCPCSUM;
1723 if (m->m_pkthdr.csum_flags & CSUM_UDP)
1724 cflags |= JME_TD_UDPCSUM;
1727 /* Configure VLAN. */
1728 if (m->m_flags & M_VLANTAG) {
1729 cflags |= (m->m_pkthdr.ether_vlantag & JME_TD_VLAN_MASK);
1730 cflags |= JME_TD_VLAN_TAG;
1733 desc = &tdata->jme_tx_ring[prod];
1734 desc->flags = htole32(cflags);
1735 desc->addr_hi = htole32(m->m_pkthdr.len);
1736 if (tdata->jme_sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT) {
1738 * Use 64bits TX desc chain format.
1740 * The first TX desc of the chain, which is setup here,
1741 * is just a symbol TX desc carrying no payload.
1743 flag64 = JME_TD_64BIT;
1744 desc->buflen = htole32(mss);
1749 /* No effective TX desc is consumed */
1753 * Use 32bits TX desc chain format.
1755 * The first TX desc of the chain, which is setup here,
1756 * is an effective TX desc carrying the first segment of
1760 desc->buflen = htole32(mss | txsegs[0].ds_len);
1761 desc->addr_lo = htole32(JME_ADDR_LO(txsegs[0].ds_addr));
1763 /* One effective TX desc is consumed */
1766 tdata->jme_tx_cnt++;
1767 KKASSERT(tdata->jme_tx_cnt - i < tdata->jme_tx_desc_cnt - JME_TXD_RSVD);
1768 JME_DESC_INC(prod, tdata->jme_tx_desc_cnt);
1770 txd->tx_ndesc = 1 - i;
1771 for (; i < nsegs; i++) {
1772 desc = &tdata->jme_tx_ring[prod];
1773 desc->buflen = htole32(txsegs[i].ds_len);
1774 desc->addr_hi = htole32(JME_ADDR_HI(txsegs[i].ds_addr));
1775 desc->addr_lo = htole32(JME_ADDR_LO(txsegs[i].ds_addr));
1776 desc->flags = htole32(JME_TD_OWN | flag64);
1778 tdata->jme_tx_cnt++;
1779 KKASSERT(tdata->jme_tx_cnt <=
1780 tdata->jme_tx_desc_cnt - JME_TXD_RSVD);
1781 JME_DESC_INC(prod, tdata->jme_tx_desc_cnt);
1784 /* Update producer index. */
1785 tdata->jme_tx_prod = prod;
1787 * Finally request interrupt and give the first descriptor
1788 * owenership to hardware.
1790 desc = txd->tx_desc;
1791 desc->flags |= htole32(JME_TD_OWN | JME_TD_INTR);
1794 txd->tx_ndesc += nsegs;
1804 jme_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1806 struct jme_softc *sc = ifp->if_softc;
1807 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
1808 struct mbuf *m_head;
1811 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1812 ASSERT_SERIALIZED(&tdata->jme_tx_serialize);
1814 if (!sc->jme_has_link) {
1815 ifq_purge(&ifp->if_snd);
1819 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
1822 if (tdata->jme_tx_cnt >= JME_TX_DESC_HIWAT(tdata))
1825 while (!ifq_is_empty(&ifp->if_snd)) {
1827 * Check number of available TX descs, always
1828 * leave JME_TXD_RSVD free TX descs.
1830 if (tdata->jme_tx_cnt + JME_TXD_SPARE >
1831 tdata->jme_tx_desc_cnt - JME_TXD_RSVD) {
1832 ifq_set_oactive(&ifp->if_snd);
1836 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1841 * Pack the data into the transmit ring. If we
1842 * don't have room, set the OACTIVE flag and wait
1843 * for the NIC to drain the ring.
1845 if (jme_encap(tdata, &m_head, &enq)) {
1846 KKASSERT(m_head == NULL);
1847 IFNET_STAT_INC(ifp, oerrors, 1);
1848 ifq_set_oactive(&ifp->if_snd);
1852 if (enq >= tdata->jme_tx_wreg) {
1853 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr |
1854 TXCSR_TX_ENB | TXCSR_TXQ_N_START(TXCSR_TXQ0));
1859 * If there's a BPF listener, bounce a copy of this frame
1862 ETHER_BPF_MTAP(ifp, m_head);
1864 /* Set a timeout in case the chip goes out to lunch. */
1865 ifp->if_timer = JME_TX_TIMEOUT;
1870 * Reading TXCSR takes very long time under heavy load
1871 * so cache TXCSR value and writes the ORed value with
1872 * the kick command to the TXCSR. This saves one register
1875 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr | TXCSR_TX_ENB |
1876 TXCSR_TXQ_N_START(TXCSR_TXQ0));
1881 jme_watchdog(struct ifnet *ifp)
1883 struct jme_softc *sc = ifp->if_softc;
1884 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
1886 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1888 if (!sc->jme_has_link) {
1889 if_printf(ifp, "watchdog timeout (missed link)\n");
1890 IFNET_STAT_INC(ifp, oerrors, 1);
1896 if (tdata->jme_tx_cnt == 0) {
1897 if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
1899 if (!ifq_is_empty(&ifp->if_snd))
1904 if_printf(ifp, "watchdog timeout\n");
1905 IFNET_STAT_INC(ifp, oerrors, 1);
1907 if (!ifq_is_empty(&ifp->if_snd))
1912 jme_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1914 struct jme_softc *sc = ifp->if_softc;
1915 struct mii_data *mii = device_get_softc(sc->jme_miibus);
1916 struct ifreq *ifr = (struct ifreq *)data;
1917 int error = 0, mask;
1919 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1923 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > JME_JUMBO_MTU ||
1924 (!(sc->jme_caps & JME_CAP_JUMBO) &&
1925 ifr->ifr_mtu > JME_MAX_MTU)) {
1930 if (ifp->if_mtu != ifr->ifr_mtu) {
1932 * No special configuration is required when interface
1933 * MTU is changed but availability of Tx checksum
1934 * offload should be chcked against new MTU size as
1935 * FIFO size is just 2K.
1937 if (ifr->ifr_mtu >= JME_TX_FIFO_SIZE) {
1938 ifp->if_capenable &=
1939 ~(IFCAP_TXCSUM | IFCAP_TSO);
1941 ~(JME_CSUM_FEATURES | CSUM_TSO);
1943 ifp->if_mtu = ifr->ifr_mtu;
1944 if (ifp->if_flags & IFF_RUNNING)
1950 if (ifp->if_flags & IFF_UP) {
1951 if (ifp->if_flags & IFF_RUNNING) {
1952 if ((ifp->if_flags ^ sc->jme_if_flags) &
1953 (IFF_PROMISC | IFF_ALLMULTI))
1959 if (ifp->if_flags & IFF_RUNNING)
1962 sc->jme_if_flags = ifp->if_flags;
1967 if (ifp->if_flags & IFF_RUNNING)
1973 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1977 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1979 if ((mask & IFCAP_TXCSUM) && ifp->if_mtu < JME_TX_FIFO_SIZE) {
1980 ifp->if_capenable ^= IFCAP_TXCSUM;
1981 if (ifp->if_capenable & IFCAP_TXCSUM)
1982 ifp->if_hwassist |= JME_CSUM_FEATURES;
1984 ifp->if_hwassist &= ~JME_CSUM_FEATURES;
1986 if (mask & IFCAP_RXCSUM) {
1989 ifp->if_capenable ^= IFCAP_RXCSUM;
1990 reg = CSR_READ_4(sc, JME_RXMAC);
1991 reg &= ~RXMAC_CSUM_ENB;
1992 if (ifp->if_capenable & IFCAP_RXCSUM)
1993 reg |= RXMAC_CSUM_ENB;
1994 CSR_WRITE_4(sc, JME_RXMAC, reg);
1997 if (mask & IFCAP_VLAN_HWTAGGING) {
1998 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2002 if ((mask & IFCAP_TSO) && ifp->if_mtu < JME_TX_FIFO_SIZE) {
2003 ifp->if_capenable ^= IFCAP_TSO;
2004 if (ifp->if_capenable & IFCAP_TSO)
2005 ifp->if_hwassist |= CSUM_TSO;
2007 ifp->if_hwassist &= ~CSUM_TSO;
2010 if (mask & IFCAP_RSS)
2011 ifp->if_capenable ^= IFCAP_RSS;
2015 error = ether_ioctl(ifp, cmd, data);
2022 jme_mac_config(struct jme_softc *sc)
2024 struct mii_data *mii;
2025 uint32_t ghc, rxmac, txmac, txpause, gp1;
2026 int phyconf = JMPHY_CONF_DEFFIFO, hdx = 0;
2028 mii = device_get_softc(sc->jme_miibus);
2030 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2032 CSR_WRITE_4(sc, JME_GHC, 0);
2034 rxmac = CSR_READ_4(sc, JME_RXMAC);
2035 rxmac &= ~RXMAC_FC_ENB;
2036 txmac = CSR_READ_4(sc, JME_TXMAC);
2037 txmac &= ~(TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST);
2038 txpause = CSR_READ_4(sc, JME_TXPFC);
2039 txpause &= ~TXPFC_PAUSE_ENB;
2040 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
2041 ghc |= GHC_FULL_DUPLEX;
2042 rxmac &= ~RXMAC_COLL_DET_ENB;
2043 txmac &= ~(TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE |
2044 TXMAC_BACKOFF | TXMAC_CARRIER_EXT |
2047 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
2048 txpause |= TXPFC_PAUSE_ENB;
2049 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
2050 rxmac |= RXMAC_FC_ENB;
2052 /* Disable retry transmit timer/retry limit. */
2053 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) &
2054 ~(TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB));
2056 rxmac |= RXMAC_COLL_DET_ENB;
2057 txmac |= TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE | TXMAC_BACKOFF;
2058 /* Enable retry transmit timer/retry limit. */
2059 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) |
2060 TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB);
2064 * Reprogram Tx/Rx MACs with resolved speed/duplex.
2066 gp1 = CSR_READ_4(sc, JME_GPREG1);
2067 gp1 &= ~GPREG1_WA_HDX;
2069 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) == 0)
2072 switch (IFM_SUBTYPE(mii->mii_media_active)) {
2074 ghc |= GHC_SPEED_10 | sc->jme_clksrc;
2076 gp1 |= GPREG1_WA_HDX;
2080 ghc |= GHC_SPEED_100 | sc->jme_clksrc;
2082 gp1 |= GPREG1_WA_HDX;
2085 * Use extended FIFO depth to workaround CRC errors
2086 * emitted by chips before JMC250B
2088 phyconf = JMPHY_CONF_EXTFIFO;
2092 if (sc->jme_caps & JME_CAP_FASTETH)
2095 ghc |= GHC_SPEED_1000 | sc->jme_clksrc_1000;
2097 txmac |= TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST;
2103 CSR_WRITE_4(sc, JME_GHC, ghc);
2104 CSR_WRITE_4(sc, JME_RXMAC, rxmac);
2105 CSR_WRITE_4(sc, JME_TXMAC, txmac);
2106 CSR_WRITE_4(sc, JME_TXPFC, txpause);
2108 if (sc->jme_workaround & JME_WA_EXTFIFO) {
2109 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
2110 JMPHY_CONF, phyconf);
2112 if (sc->jme_workaround & JME_WA_HDX)
2113 CSR_WRITE_4(sc, JME_GPREG1, gp1);
2119 struct jme_softc *sc = xsc;
2120 struct ifnet *ifp = &sc->arpcom.ac_if;
2124 ASSERT_SERIALIZED(&sc->jme_serialize);
2126 status = CSR_READ_4(sc, JME_INTR_REQ_STATUS);
2127 if (status == 0 || status == 0xFFFFFFFF)
2130 /* Disable interrupts. */
2131 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
2133 status = CSR_READ_4(sc, JME_INTR_STATUS);
2134 if ((status & JME_INTRS) == 0 || status == 0xFFFFFFFF)
2137 /* Reset PCC counter/timer and Ack interrupts. */
2138 status &= ~(INTR_TXQ_COMP | INTR_RXQ_COMP);
2140 if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO))
2141 status |= INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP;
2143 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2144 if (status & jme_rx_status[r].jme_coal) {
2145 status |= jme_rx_status[r].jme_coal |
2146 jme_rx_status[r].jme_comp;
2150 CSR_WRITE_4(sc, JME_INTR_STATUS, status);
2152 if (ifp->if_flags & IFF_RUNNING) {
2153 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
2155 if (status & (INTR_RXQ_COAL | INTR_RXQ_COAL_TO))
2156 jme_rx_intr(sc, status);
2158 if (status & INTR_RXQ_DESC_EMPTY) {
2160 * Notify hardware availability of new Rx buffers.
2161 * Reading RXCSR takes very long time under heavy
2162 * load so cache RXCSR value and writes the ORed
2163 * value with the kick command to the RXCSR. This
2164 * saves one register access cycle.
2166 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr |
2167 RXCSR_RX_ENB | RXCSR_RXQ_START);
2170 if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO)) {
2171 lwkt_serialize_enter(&tdata->jme_tx_serialize);
2173 if (!ifq_is_empty(&ifp->if_snd))
2175 lwkt_serialize_exit(&tdata->jme_tx_serialize);
2179 /* Reenable interrupts. */
2180 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
2184 jme_txeof(struct jme_txdata *tdata)
2186 struct ifnet *ifp = &tdata->jme_sc->arpcom.ac_if;
2189 cons = tdata->jme_tx_cons;
2190 if (cons == tdata->jme_tx_prod)
2194 * Go through our Tx list and free mbufs for those
2195 * frames which have been transmitted.
2197 while (cons != tdata->jme_tx_prod) {
2198 struct jme_txdesc *txd, *next_txd;
2199 uint32_t status, next_status;
2200 int next_cons, nsegs;
2202 txd = &tdata->jme_txdesc[cons];
2203 KASSERT(txd->tx_m != NULL,
2204 ("%s: freeing NULL mbuf!", __func__));
2206 status = le32toh(txd->tx_desc->flags);
2207 if ((status & JME_TD_OWN) == JME_TD_OWN)
2212 * This chip will always update the TX descriptor's
2213 * buflen field and this updating always happens
2214 * after clearing the OWN bit, so even if the OWN
2215 * bit is cleared by the chip, we still don't sure
2216 * about whether the buflen field has been updated
2217 * by the chip or not. To avoid this race, we wait
2218 * for the next TX descriptor's OWN bit to be cleared
2219 * by the chip before reusing this TX descriptor.
2222 JME_DESC_ADD(next_cons, txd->tx_ndesc, tdata->jme_tx_desc_cnt);
2223 next_txd = &tdata->jme_txdesc[next_cons];
2224 if (next_txd->tx_m == NULL)
2226 next_status = le32toh(next_txd->tx_desc->flags);
2227 if ((next_status & JME_TD_OWN) == JME_TD_OWN)
2230 if (status & (JME_TD_TMOUT | JME_TD_RETRY_EXP)) {
2231 IFNET_STAT_INC(ifp, oerrors, 1);
2233 IFNET_STAT_INC(ifp, opackets, 1);
2234 if (status & JME_TD_COLLISION) {
2235 IFNET_STAT_INC(ifp, collisions,
2236 le32toh(txd->tx_desc->buflen) &
2237 JME_TD_BUF_LEN_MASK);
2242 * Only the first descriptor of multi-descriptor
2243 * transmission is updated so driver have to skip entire
2244 * chained buffers for the transmiited frame. In other
2245 * words, JME_TD_OWN bit is valid only at the first
2246 * descriptor of a multi-descriptor transmission.
2248 for (nsegs = 0; nsegs < txd->tx_ndesc; nsegs++) {
2249 tdata->jme_tx_ring[cons].flags = 0;
2250 JME_DESC_INC(cons, tdata->jme_tx_desc_cnt);
2253 /* Reclaim transferred mbufs. */
2254 bus_dmamap_unload(tdata->jme_tx_tag, txd->tx_dmamap);
2257 tdata->jme_tx_cnt -= txd->tx_ndesc;
2258 KASSERT(tdata->jme_tx_cnt >= 0,
2259 ("%s: Active Tx desc counter was garbled", __func__));
2262 tdata->jme_tx_cons = cons;
2264 /* 1 for symbol TX descriptor */
2265 if (tdata->jme_tx_cnt <= JME_MAXTXSEGS + 1)
2268 if (tdata->jme_tx_cnt + JME_TXD_SPARE <=
2269 tdata->jme_tx_desc_cnt - JME_TXD_RSVD)
2270 ifq_clr_oactive(&ifp->if_snd);
2273 static __inline void
2274 jme_discard_rxbufs(struct jme_rxdata *rdata, int cons, int count)
2278 for (i = 0; i < count; ++i) {
2279 jme_setup_rxdesc(&rdata->jme_rxdesc[cons]);
2280 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt);
2284 static __inline struct pktinfo *
2285 jme_pktinfo(struct pktinfo *pi, uint32_t flags)
2287 if (flags & JME_RD_IPV4)
2288 pi->pi_netisr = NETISR_IP;
2289 else if (flags & JME_RD_IPV6)
2290 pi->pi_netisr = NETISR_IPV6;
2295 pi->pi_l3proto = IPPROTO_UNKNOWN;
2297 if (flags & JME_RD_MORE_FRAG)
2298 pi->pi_flags |= PKTINFO_FLAG_FRAG;
2299 else if (flags & JME_RD_TCP)
2300 pi->pi_l3proto = IPPROTO_TCP;
2301 else if (flags & JME_RD_UDP)
2302 pi->pi_l3proto = IPPROTO_UDP;
2308 /* Receive a frame. */
2310 jme_rxpkt(struct jme_rxdata *rdata)
2312 struct ifnet *ifp = &rdata->jme_sc->arpcom.ac_if;
2313 struct jme_desc *desc;
2314 struct jme_rxdesc *rxd;
2315 struct mbuf *mp, *m;
2316 uint32_t flags, status, hash, hashinfo;
2317 int cons, count, nsegs;
2319 cons = rdata->jme_rx_cons;
2320 desc = &rdata->jme_rx_ring[cons];
2322 flags = le32toh(desc->flags);
2323 status = le32toh(desc->buflen);
2324 hash = le32toh(desc->addr_hi);
2325 hashinfo = le32toh(desc->addr_lo);
2326 nsegs = JME_RX_NSEGS(status);
2329 /* Skip the first descriptor. */
2330 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt);
2333 * Clear the OWN bit of the following RX descriptors;
2334 * hardware will not clear the OWN bit except the first
2337 * Since the first RX descriptor is setup, i.e. OWN bit
2338 * on, before its followins RX descriptors, leaving the
2339 * OWN bit on the following RX descriptors will trick
2340 * the hardware into thinking that the following RX
2341 * descriptors are ready to be used too.
2343 for (count = 1; count < nsegs; count++,
2344 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt))
2345 rdata->jme_rx_ring[cons].flags = 0;
2347 cons = rdata->jme_rx_cons;
2350 JME_RSS_DPRINTF(rdata->jme_sc, 15, "ring%d, flags 0x%08x, "
2351 "hash 0x%08x, hash info 0x%08x\n",
2352 rdata->jme_rx_idx, flags, hash, hashinfo);
2354 if (status & JME_RX_ERR_STAT) {
2355 IFNET_STAT_INC(ifp, ierrors, 1);
2356 jme_discard_rxbufs(rdata, cons, nsegs);
2357 #ifdef JME_SHOW_ERRORS
2358 if_printf(ifp, "%s : receive error = 0x%b\n",
2359 __func__, JME_RX_ERR(status), JME_RX_ERR_BITS);
2361 rdata->jme_rx_cons += nsegs;
2362 rdata->jme_rx_cons %= rdata->jme_rx_desc_cnt;
2366 rdata->jme_rxlen = JME_RX_BYTES(status) - JME_RX_PAD_BYTES;
2367 for (count = 0; count < nsegs; count++,
2368 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt)) {
2369 rxd = &rdata->jme_rxdesc[cons];
2372 /* Add a new receive buffer to the ring. */
2373 if (jme_newbuf(rdata, rxd, 0) != 0) {
2374 IFNET_STAT_INC(ifp, iqdrops, 1);
2376 jme_discard_rxbufs(rdata, cons, nsegs - count);
2377 if (rdata->jme_rxhead != NULL) {
2378 m_freem(rdata->jme_rxhead);
2379 JME_RXCHAIN_RESET(rdata);
2385 * Assume we've received a full sized frame.
2386 * Actual size is fixed when we encounter the end of
2387 * multi-segmented frame.
2389 mp->m_len = MCLBYTES;
2391 /* Chain received mbufs. */
2392 if (rdata->jme_rxhead == NULL) {
2393 rdata->jme_rxhead = mp;
2394 rdata->jme_rxtail = mp;
2397 * Receive processor can receive a maximum frame
2398 * size of 65535 bytes.
2400 rdata->jme_rxtail->m_next = mp;
2401 rdata->jme_rxtail = mp;
2404 if (count == nsegs - 1) {
2405 struct pktinfo pi0, *pi;
2407 /* Last desc. for this frame. */
2408 m = rdata->jme_rxhead;
2409 m->m_pkthdr.len = rdata->jme_rxlen;
2411 /* Set first mbuf size. */
2412 m->m_len = MCLBYTES - JME_RX_PAD_BYTES;
2413 /* Set last mbuf size. */
2414 mp->m_len = rdata->jme_rxlen -
2415 ((MCLBYTES - JME_RX_PAD_BYTES) +
2416 (MCLBYTES * (nsegs - 2)));
2418 m->m_len = rdata->jme_rxlen;
2420 m->m_pkthdr.rcvif = ifp;
2423 * Account for 10bytes auto padding which is used
2424 * to align IP header on 32bit boundary. Also note,
2425 * CRC bytes is automatically removed by the
2428 m->m_data += JME_RX_PAD_BYTES;
2430 /* Set checksum information. */
2431 if ((ifp->if_capenable & IFCAP_RXCSUM) &&
2432 (flags & JME_RD_IPV4)) {
2433 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2434 if (flags & JME_RD_IPCSUM)
2435 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2436 if ((flags & JME_RD_MORE_FRAG) == 0 &&
2437 ((flags & (JME_RD_TCP | JME_RD_TCPCSUM)) ==
2438 (JME_RD_TCP | JME_RD_TCPCSUM) ||
2439 (flags & (JME_RD_UDP | JME_RD_UDPCSUM)) ==
2440 (JME_RD_UDP | JME_RD_UDPCSUM))) {
2441 m->m_pkthdr.csum_flags |=
2442 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2443 m->m_pkthdr.csum_data = 0xffff;
2447 /* Check for VLAN tagged packets. */
2448 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) &&
2449 (flags & JME_RD_VLAN_TAG)) {
2450 m->m_pkthdr.ether_vlantag =
2451 flags & JME_RD_VLAN_MASK;
2452 m->m_flags |= M_VLANTAG;
2455 IFNET_STAT_INC(ifp, ipackets, 1);
2457 if (ifp->if_capenable & IFCAP_RSS)
2458 pi = jme_pktinfo(&pi0, flags);
2463 (hashinfo & JME_RD_HASH_FN_MASK) ==
2464 JME_RD_HASH_FN_TOEPLITZ) {
2465 m->m_flags |= (M_HASH | M_CKHASH);
2466 m->m_pkthdr.hash = toeplitz_hash(hash);
2469 #ifdef JME_RSS_DEBUG
2471 JME_RSS_DPRINTF(rdata->jme_sc, 10,
2472 "isr %d flags %08x, l3 %d %s\n",
2473 pi->pi_netisr, pi->pi_flags,
2475 (m->m_flags & M_HASH) ? "hash" : "");
2480 ether_input_pkt(ifp, m, pi);
2482 /* Reset mbuf chains. */
2483 JME_RXCHAIN_RESET(rdata);
2484 #ifdef JME_RSS_DEBUG
2485 rdata->jme_rx_pkt++;
2490 rdata->jme_rx_cons += nsegs;
2491 rdata->jme_rx_cons %= rdata->jme_rx_desc_cnt;
2495 jme_rxeof(struct jme_rxdata *rdata, int count)
2497 struct jme_desc *desc;
2501 #ifdef IFPOLL_ENABLE
2502 if (count >= 0 && count-- == 0)
2505 desc = &rdata->jme_rx_ring[rdata->jme_rx_cons];
2506 if ((le32toh(desc->flags) & JME_RD_OWN) == JME_RD_OWN)
2508 if ((le32toh(desc->buflen) & JME_RD_VALID) == 0)
2512 * Check number of segments against received bytes.
2513 * Non-matching value would indicate that hardware
2514 * is still trying to update Rx descriptors. I'm not
2515 * sure whether this check is needed.
2517 nsegs = JME_RX_NSEGS(le32toh(desc->buflen));
2518 pktlen = JME_RX_BYTES(le32toh(desc->buflen));
2519 if (nsegs != howmany(pktlen, MCLBYTES)) {
2520 if_printf(&rdata->jme_sc->arpcom.ac_if,
2521 "RX fragment count(%d) and "
2522 "packet size(%d) mismach\n", nsegs, pktlen);
2528 * RSS hash and hash information may _not_ be set by the
2529 * hardware even if the OWN bit is cleared and VALID bit
2532 * If the RSS information is not delivered by the hardware
2533 * yet, we MUST NOT accept this packet, let alone reusing
2534 * its RX descriptor. If this packet was accepted and its
2535 * RX descriptor was reused before hardware delivering the
2536 * RSS information, the RX buffer's address would be trashed
2537 * by the RSS information delivered by the hardware.
2539 if (JME_ENABLE_HWRSS(rdata->jme_sc)) {
2540 struct jme_rxdesc *rxd;
2543 hashinfo = le32toh(desc->addr_lo);
2544 rxd = &rdata->jme_rxdesc[rdata->jme_rx_cons];
2547 * This test should be enough to detect the pending
2548 * RSS information delivery, given:
2549 * - If RSS hash is not calculated, the hashinfo
2550 * will be 0. Howvever, the lower 32bits of RX
2551 * buffers' physical address will never be 0.
2552 * (see jme_rxbuf_dma_filter)
2553 * - If RSS hash is calculated, the lowest 4 bits
2554 * of hashinfo will be set, while the RX buffers
2555 * are at least 2K aligned.
2557 if (hashinfo == JME_ADDR_LO(rxd->rx_paddr)) {
2558 #ifdef JME_SHOW_RSSWB
2559 if_printf(&rdata->jme_sc->arpcom.ac_if,
2560 "RSS is not written back yet\n");
2566 /* Received a frame. */
2574 struct jme_softc *sc = xsc;
2575 struct mii_data *mii = device_get_softc(sc->jme_miibus);
2577 lwkt_serialize_enter(&sc->jme_serialize);
2579 KKASSERT(mycpuid == JME_TICK_CPUID);
2581 sc->jme_in_tick = TRUE;
2583 sc->jme_in_tick = FALSE;
2585 callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
2587 lwkt_serialize_exit(&sc->jme_serialize);
2591 jme_reset(struct jme_softc *sc)
2595 /* Make sure that TX and RX are stopped */
2600 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2604 * Hold reset bit before stop reset
2607 /* Disable TXMAC and TXOFL clock sources */
2608 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2609 /* Disable RXMAC clock source */
2610 val = CSR_READ_4(sc, JME_GPREG1);
2611 CSR_WRITE_4(sc, JME_GPREG1, val | GPREG1_DIS_RXMAC_CLKSRC);
2613 CSR_READ_4(sc, JME_GHC);
2616 CSR_WRITE_4(sc, JME_GHC, 0);
2618 CSR_READ_4(sc, JME_GHC);
2621 * Clear reset bit after stop reset
2624 /* Enable TXMAC and TXOFL clock sources */
2625 CSR_WRITE_4(sc, JME_GHC, GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC);
2626 /* Enable RXMAC clock source */
2627 val = CSR_READ_4(sc, JME_GPREG1);
2628 CSR_WRITE_4(sc, JME_GPREG1, val & ~GPREG1_DIS_RXMAC_CLKSRC);
2630 CSR_READ_4(sc, JME_GHC);
2632 /* Disable TXMAC and TXOFL clock sources */
2633 CSR_WRITE_4(sc, JME_GHC, 0);
2634 /* Disable RXMAC clock source */
2635 val = CSR_READ_4(sc, JME_GPREG1);
2636 CSR_WRITE_4(sc, JME_GPREG1, val | GPREG1_DIS_RXMAC_CLKSRC);
2638 CSR_READ_4(sc, JME_GHC);
2640 /* Enable TX and RX */
2641 val = CSR_READ_4(sc, JME_TXCSR);
2642 CSR_WRITE_4(sc, JME_TXCSR, val | TXCSR_TX_ENB);
2643 val = CSR_READ_4(sc, JME_RXCSR);
2644 CSR_WRITE_4(sc, JME_RXCSR, val | RXCSR_RX_ENB);
2646 CSR_READ_4(sc, JME_TXCSR);
2647 CSR_READ_4(sc, JME_RXCSR);
2649 /* Enable TXMAC and TXOFL clock sources */
2650 CSR_WRITE_4(sc, JME_GHC, GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC);
2651 /* Eisable RXMAC clock source */
2652 val = CSR_READ_4(sc, JME_GPREG1);
2653 CSR_WRITE_4(sc, JME_GPREG1, val & ~GPREG1_DIS_RXMAC_CLKSRC);
2655 CSR_READ_4(sc, JME_GHC);
2657 /* Stop TX and RX */
2665 struct jme_softc *sc = xsc;
2666 struct ifnet *ifp = &sc->arpcom.ac_if;
2667 struct mii_data *mii;
2668 uint8_t eaddr[ETHER_ADDR_LEN];
2673 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2676 * Cancel any pending I/O.
2681 * Reset the chip to a known state.
2686 * Setup MSI/MSI-X vectors to interrupts mapping
2690 if (JME_ENABLE_HWRSS(sc))
2693 jme_disable_rss(sc);
2695 /* Init RX descriptors */
2696 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2697 error = jme_init_rx_ring(&sc->jme_cdata.jme_rx_data[r]);
2699 if_printf(ifp, "initialization failed: "
2700 "no memory for %dth RX ring.\n", r);
2706 /* Init TX descriptors */
2707 jme_init_tx_ring(&sc->jme_cdata.jme_tx_data);
2709 /* Initialize shadow status block. */
2712 /* Reprogram the station address. */
2713 bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
2714 CSR_WRITE_4(sc, JME_PAR0,
2715 eaddr[3] << 24 | eaddr[2] << 16 | eaddr[1] << 8 | eaddr[0]);
2716 CSR_WRITE_4(sc, JME_PAR1, eaddr[5] << 8 | eaddr[4]);
2719 * Configure Tx queue.
2720 * Tx priority queue weight value : 0
2721 * Tx FIFO threshold for processing next packet : 16QW
2722 * Maximum Tx DMA length : 512
2723 * Allow Tx DMA burst.
2725 sc->jme_txcsr = TXCSR_TXQ_N_SEL(TXCSR_TXQ0);
2726 sc->jme_txcsr |= TXCSR_TXQ_WEIGHT(TXCSR_TXQ_WEIGHT_MIN);
2727 sc->jme_txcsr |= TXCSR_FIFO_THRESH_16QW;
2728 sc->jme_txcsr |= sc->jme_tx_dma_size;
2729 sc->jme_txcsr |= TXCSR_DMA_BURST;
2730 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr);
2732 /* Set Tx descriptor counter. */
2733 CSR_WRITE_4(sc, JME_TXQDC, sc->jme_cdata.jme_tx_data.jme_tx_desc_cnt);
2735 /* Set Tx ring address to the hardware. */
2736 paddr = sc->jme_cdata.jme_tx_data.jme_tx_ring_paddr;
2737 CSR_WRITE_4(sc, JME_TXDBA_HI, JME_ADDR_HI(paddr));
2738 CSR_WRITE_4(sc, JME_TXDBA_LO, JME_ADDR_LO(paddr));
2740 /* Configure TxMAC parameters. */
2741 reg = TXMAC_IFG1_DEFAULT | TXMAC_IFG2_DEFAULT | TXMAC_IFG_ENB;
2742 reg |= TXMAC_THRESH_1_PKT;
2743 reg |= TXMAC_CRC_ENB | TXMAC_PAD_ENB;
2744 CSR_WRITE_4(sc, JME_TXMAC, reg);
2747 * Configure Rx queue.
2748 * FIFO full threshold for transmitting Tx pause packet : 128T
2749 * FIFO threshold for processing next packet : 128QW
2751 * Max Rx DMA length : 128
2752 * Rx descriptor retry : 32
2753 * Rx descriptor retry time gap : 256ns
2754 * Don't receive runt/bad frame.
2756 sc->jme_rxcsr = RXCSR_FIFO_FTHRESH_128T;
2759 * Since Rx FIFO size is 4K bytes, receiving frames larger
2760 * than 4K bytes will suffer from Rx FIFO overruns. So
2761 * decrease FIFO threshold to reduce the FIFO overruns for
2762 * frames larger than 4000 bytes.
2763 * For best performance of standard MTU sized frames use
2764 * maximum allowable FIFO threshold, 128QW.
2766 if ((ifp->if_mtu + ETHER_HDR_LEN + EVL_ENCAPLEN + ETHER_CRC_LEN) >
2768 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
2770 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_128QW;
2772 /* Improve PCI Express compatibility */
2773 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
2775 sc->jme_rxcsr |= sc->jme_rx_dma_size;
2776 sc->jme_rxcsr |= RXCSR_DESC_RT_CNT(RXCSR_DESC_RT_CNT_DEFAULT);
2777 sc->jme_rxcsr |= RXCSR_DESC_RT_GAP_256 & RXCSR_DESC_RT_GAP_MASK;
2778 /* XXX TODO DROP_BAD */
2780 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2781 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
2783 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RXQ_N_SEL(r));
2785 /* Set Rx descriptor counter. */
2786 CSR_WRITE_4(sc, JME_RXQDC, rdata->jme_rx_desc_cnt);
2788 /* Set Rx ring address to the hardware. */
2789 paddr = rdata->jme_rx_ring_paddr;
2790 CSR_WRITE_4(sc, JME_RXDBA_HI, JME_ADDR_HI(paddr));
2791 CSR_WRITE_4(sc, JME_RXDBA_LO, JME_ADDR_LO(paddr));
2794 /* Clear receive filter. */
2795 CSR_WRITE_4(sc, JME_RXMAC, 0);
2797 /* Set up the receive filter. */
2802 * Disable all WOL bits as WOL can interfere normal Rx
2803 * operation. Also clear WOL detection status bits.
2805 reg = CSR_READ_4(sc, JME_PMCS);
2806 reg &= ~PMCS_WOL_ENB_MASK;
2807 CSR_WRITE_4(sc, JME_PMCS, reg);
2810 * Pad 10bytes right before received frame. This will greatly
2811 * help Rx performance on strict-alignment architectures as
2812 * it does not need to copy the frame to align the payload.
2814 reg = CSR_READ_4(sc, JME_RXMAC);
2815 reg |= RXMAC_PAD_10BYTES;
2817 if (ifp->if_capenable & IFCAP_RXCSUM)
2818 reg |= RXMAC_CSUM_ENB;
2819 CSR_WRITE_4(sc, JME_RXMAC, reg);
2821 /* Configure general purpose reg0 */
2822 reg = CSR_READ_4(sc, JME_GPREG0);
2823 reg &= ~GPREG0_PCC_UNIT_MASK;
2824 /* Set PCC timer resolution to micro-seconds unit. */
2825 reg |= GPREG0_PCC_UNIT_US;
2827 * Disable all shadow register posting as we have to read
2828 * JME_INTR_STATUS register in jme_intr. Also it seems
2829 * that it's hard to synchronize interrupt status between
2830 * hardware and software with shadow posting due to
2831 * requirements of bus_dmamap_sync(9).
2833 reg |= GPREG0_SH_POST_DW7_DIS | GPREG0_SH_POST_DW6_DIS |
2834 GPREG0_SH_POST_DW5_DIS | GPREG0_SH_POST_DW4_DIS |
2835 GPREG0_SH_POST_DW3_DIS | GPREG0_SH_POST_DW2_DIS |
2836 GPREG0_SH_POST_DW1_DIS | GPREG0_SH_POST_DW0_DIS;
2837 /* Disable posting of DW0. */
2838 reg &= ~GPREG0_POST_DW0_ENB;
2839 /* Clear PME message. */
2840 reg &= ~GPREG0_PME_ENB;
2841 /* Set PHY address. */
2842 reg &= ~GPREG0_PHY_ADDR_MASK;
2843 reg |= sc->jme_phyaddr;
2844 CSR_WRITE_4(sc, JME_GPREG0, reg);
2846 /* Configure Tx queue 0 packet completion coalescing. */
2847 jme_set_tx_coal(sc);
2849 /* Configure Rx queues packet completion coalescing. */
2850 jme_set_rx_coal(sc);
2852 /* Configure shadow status block but don't enable posting. */
2853 paddr = sc->jme_cdata.jme_ssb_block_paddr;
2854 CSR_WRITE_4(sc, JME_SHBASE_ADDR_HI, JME_ADDR_HI(paddr));
2855 CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO, JME_ADDR_LO(paddr));
2857 /* Disable Timer 1 and Timer 2. */
2858 CSR_WRITE_4(sc, JME_TIMER1, 0);
2859 CSR_WRITE_4(sc, JME_TIMER2, 0);
2861 /* Configure retry transmit period, retry limit value. */
2862 CSR_WRITE_4(sc, JME_TXTRHD,
2863 ((TXTRHD_RT_PERIOD_DEFAULT << TXTRHD_RT_PERIOD_SHIFT) &
2864 TXTRHD_RT_PERIOD_MASK) |
2865 ((TXTRHD_RT_LIMIT_DEFAULT << TXTRHD_RT_LIMIT_SHIFT) &
2866 TXTRHD_RT_LIMIT_SHIFT));
2868 #ifdef IFPOLL_ENABLE
2869 if (!(ifp->if_flags & IFF_NPOLLING))
2871 /* Initialize the interrupt mask. */
2872 jme_enable_intr(sc);
2873 CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2876 * Enabling Tx/Rx DMA engines and Rx queue processing is
2877 * done after detection of valid link in jme_miibus_statchg.
2879 sc->jme_has_link = FALSE;
2881 /* Set the current media. */
2882 mii = device_get_softc(sc->jme_miibus);
2885 callout_reset_bycpu(&sc->jme_tick_ch, hz, jme_tick, sc,
2888 ifp->if_flags |= IFF_RUNNING;
2889 ifq_clr_oactive(&ifp->if_snd);
2893 jme_stop(struct jme_softc *sc)
2895 struct ifnet *ifp = &sc->arpcom.ac_if;
2896 struct jme_txdata *tdata = &sc->jme_cdata.jme_tx_data;
2897 struct jme_txdesc *txd;
2898 struct jme_rxdesc *rxd;
2899 struct jme_rxdata *rdata;
2902 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2905 * Mark the interface down and cancel the watchdog timer.
2907 ifp->if_flags &= ~IFF_RUNNING;
2908 ifq_clr_oactive(&ifp->if_snd);
2911 callout_stop(&sc->jme_tick_ch);
2912 sc->jme_has_link = FALSE;
2915 * Disable interrupts.
2917 jme_disable_intr(sc);
2918 CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2920 /* Disable updating shadow status block. */
2921 CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO,
2922 CSR_READ_4(sc, JME_SHBASE_ADDR_LO) & ~SHBASE_POST_ENB);
2924 /* Stop receiver, transmitter. */
2929 * Free partial finished RX segments
2931 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2932 rdata = &sc->jme_cdata.jme_rx_data[r];
2933 if (rdata->jme_rxhead != NULL)
2934 m_freem(rdata->jme_rxhead);
2935 JME_RXCHAIN_RESET(rdata);
2939 * Free RX and TX mbufs still in the queues.
2941 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2942 rdata = &sc->jme_cdata.jme_rx_data[r];
2943 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
2944 rxd = &rdata->jme_rxdesc[i];
2945 if (rxd->rx_m != NULL) {
2946 bus_dmamap_unload(rdata->jme_rx_tag,
2953 for (i = 0; i < tdata->jme_tx_desc_cnt; i++) {
2954 txd = &tdata->jme_txdesc[i];
2955 if (txd->tx_m != NULL) {
2956 bus_dmamap_unload(tdata->jme_tx_tag, txd->tx_dmamap);
2965 jme_stop_tx(struct jme_softc *sc)
2970 reg = CSR_READ_4(sc, JME_TXCSR);
2971 if ((reg & TXCSR_TX_ENB) == 0)
2973 reg &= ~TXCSR_TX_ENB;
2974 CSR_WRITE_4(sc, JME_TXCSR, reg);
2975 for (i = JME_TIMEOUT; i > 0; i--) {
2977 if ((CSR_READ_4(sc, JME_TXCSR) & TXCSR_TX_ENB) == 0)
2981 device_printf(sc->jme_dev, "stopping transmitter timeout!\n");
2985 jme_stop_rx(struct jme_softc *sc)
2990 reg = CSR_READ_4(sc, JME_RXCSR);
2991 if ((reg & RXCSR_RX_ENB) == 0)
2993 reg &= ~RXCSR_RX_ENB;
2994 CSR_WRITE_4(sc, JME_RXCSR, reg);
2995 for (i = JME_TIMEOUT; i > 0; i--) {
2997 if ((CSR_READ_4(sc, JME_RXCSR) & RXCSR_RX_ENB) == 0)
3001 device_printf(sc->jme_dev, "stopping recevier timeout!\n");
3005 jme_init_tx_ring(struct jme_txdata *tdata)
3007 struct jme_txdesc *txd;
3010 tdata->jme_tx_prod = 0;
3011 tdata->jme_tx_cons = 0;
3012 tdata->jme_tx_cnt = 0;
3014 bzero(tdata->jme_tx_ring, JME_TX_RING_SIZE(tdata));
3015 for (i = 0; i < tdata->jme_tx_desc_cnt; i++) {
3016 txd = &tdata->jme_txdesc[i];
3018 txd->tx_desc = &tdata->jme_tx_ring[i];
3024 jme_init_ssb(struct jme_softc *sc)
3026 struct jme_chain_data *cd;
3028 cd = &sc->jme_cdata;
3029 bzero(cd->jme_ssb_block, JME_SSB_SIZE);
3033 jme_init_rx_ring(struct jme_rxdata *rdata)
3035 struct jme_rxdesc *rxd;
3038 KKASSERT(rdata->jme_rxhead == NULL &&
3039 rdata->jme_rxtail == NULL &&
3040 rdata->jme_rxlen == 0);
3041 rdata->jme_rx_cons = 0;
3043 bzero(rdata->jme_rx_ring, JME_RX_RING_SIZE(rdata));
3044 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
3047 rxd = &rdata->jme_rxdesc[i];
3049 rxd->rx_desc = &rdata->jme_rx_ring[i];
3050 error = jme_newbuf(rdata, rxd, 1);
3058 jme_newbuf(struct jme_rxdata *rdata, struct jme_rxdesc *rxd, int init)
3061 bus_dma_segment_t segs;
3065 m = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
3069 * JMC250 has 64bit boundary alignment limitation so jme(4)
3070 * takes advantage of 10 bytes padding feature of hardware
3071 * in order not to copy entire frame to align IP header on
3074 m->m_len = m->m_pkthdr.len = MCLBYTES;
3076 error = bus_dmamap_load_mbuf_segment(rdata->jme_rx_tag,
3077 rdata->jme_rx_sparemap, m, &segs, 1, &nsegs,
3082 if_printf(&rdata->jme_sc->arpcom.ac_if,
3083 "can't load RX mbuf\n");
3088 if (rxd->rx_m != NULL) {
3089 bus_dmamap_sync(rdata->jme_rx_tag, rxd->rx_dmamap,
3090 BUS_DMASYNC_POSTREAD);
3091 bus_dmamap_unload(rdata->jme_rx_tag, rxd->rx_dmamap);
3093 map = rxd->rx_dmamap;
3094 rxd->rx_dmamap = rdata->jme_rx_sparemap;
3095 rdata->jme_rx_sparemap = map;
3097 rxd->rx_paddr = segs.ds_addr;
3099 jme_setup_rxdesc(rxd);
3104 jme_set_vlan(struct jme_softc *sc)
3106 struct ifnet *ifp = &sc->arpcom.ac_if;
3109 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3111 reg = CSR_READ_4(sc, JME_RXMAC);
3112 reg &= ~RXMAC_VLAN_ENB;
3113 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
3114 reg |= RXMAC_VLAN_ENB;
3115 CSR_WRITE_4(sc, JME_RXMAC, reg);
3119 jme_set_filter(struct jme_softc *sc)
3121 struct ifnet *ifp = &sc->arpcom.ac_if;
3122 struct ifmultiaddr *ifma;
3127 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3129 rxcfg = CSR_READ_4(sc, JME_RXMAC);
3130 rxcfg &= ~(RXMAC_BROADCAST | RXMAC_PROMISC | RXMAC_MULTICAST |
3134 * Always accept frames destined to our station address.
3135 * Always accept broadcast frames.
3137 rxcfg |= RXMAC_UNICAST | RXMAC_BROADCAST;
3139 if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
3140 if (ifp->if_flags & IFF_PROMISC)
3141 rxcfg |= RXMAC_PROMISC;
3142 if (ifp->if_flags & IFF_ALLMULTI)
3143 rxcfg |= RXMAC_ALLMULTI;
3144 CSR_WRITE_4(sc, JME_MAR0, 0xFFFFFFFF);
3145 CSR_WRITE_4(sc, JME_MAR1, 0xFFFFFFFF);
3146 CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
3151 * Set up the multicast address filter by passing all multicast
3152 * addresses through a CRC generator, and then using the low-order
3153 * 6 bits as an index into the 64 bit multicast hash table. The
3154 * high order bits select the register, while the rest of the bits
3155 * select the bit within the register.
3157 rxcfg |= RXMAC_MULTICAST;
3158 bzero(mchash, sizeof(mchash));
3160 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3161 if (ifma->ifma_addr->sa_family != AF_LINK)
3163 crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
3164 ifma->ifma_addr), ETHER_ADDR_LEN);
3166 /* Just want the 6 least significant bits. */
3169 /* Set the corresponding bit in the hash table. */
3170 mchash[crc >> 5] |= 1 << (crc & 0x1f);
3173 CSR_WRITE_4(sc, JME_MAR0, mchash[0]);
3174 CSR_WRITE_4(sc, JME_MAR1, mchash[1]);
3175 CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
3179 jme_sysctl_tx_coal_to(SYSCTL_HANDLER_ARGS)
3181 struct jme_softc *sc = arg1;
3182 struct ifnet *ifp = &sc->arpcom.ac_if;
3185 ifnet_serialize_all(ifp);
3187 v = sc->jme_tx_coal_to;
3188 error = sysctl_handle_int(oidp, &v, 0, req);
3189 if (error || req->newptr == NULL)
3192 if (v < PCCTX_COAL_TO_MIN || v > PCCTX_COAL_TO_MAX) {
3197 if (v != sc->jme_tx_coal_to) {
3198 sc->jme_tx_coal_to = v;
3199 if (ifp->if_flags & IFF_RUNNING)
3200 jme_set_tx_coal(sc);
3203 ifnet_deserialize_all(ifp);
3208 jme_sysctl_tx_coal_pkt(SYSCTL_HANDLER_ARGS)
3210 struct jme_softc *sc = arg1;
3211 struct ifnet *ifp = &sc->arpcom.ac_if;
3214 ifnet_serialize_all(ifp);
3216 v = sc->jme_tx_coal_pkt;
3217 error = sysctl_handle_int(oidp, &v, 0, req);
3218 if (error || req->newptr == NULL)
3221 if (v < PCCTX_COAL_PKT_MIN || v > PCCTX_COAL_PKT_MAX) {
3226 if (v != sc->jme_tx_coal_pkt) {
3227 sc->jme_tx_coal_pkt = v;
3228 if (ifp->if_flags & IFF_RUNNING)
3229 jme_set_tx_coal(sc);
3232 ifnet_deserialize_all(ifp);
3237 jme_sysctl_rx_coal_to(SYSCTL_HANDLER_ARGS)
3239 struct jme_softc *sc = arg1;
3240 struct ifnet *ifp = &sc->arpcom.ac_if;
3243 ifnet_serialize_all(ifp);
3245 v = sc->jme_rx_coal_to;
3246 error = sysctl_handle_int(oidp, &v, 0, req);
3247 if (error || req->newptr == NULL)
3250 if (v < PCCRX_COAL_TO_MIN || v > PCCRX_COAL_TO_MAX) {
3255 if (v != sc->jme_rx_coal_to) {
3256 sc->jme_rx_coal_to = v;
3257 if (ifp->if_flags & IFF_RUNNING)
3258 jme_set_rx_coal(sc);
3261 ifnet_deserialize_all(ifp);
3266 jme_sysctl_rx_coal_pkt(SYSCTL_HANDLER_ARGS)
3268 struct jme_softc *sc = arg1;
3269 struct ifnet *ifp = &sc->arpcom.ac_if;
3272 ifnet_serialize_all(ifp);
3274 v = sc->jme_rx_coal_pkt;
3275 error = sysctl_handle_int(oidp, &v, 0, req);
3276 if (error || req->newptr == NULL)
3279 if (v < PCCRX_COAL_PKT_MIN || v > PCCRX_COAL_PKT_MAX) {
3284 if (v != sc->jme_rx_coal_pkt) {
3285 sc->jme_rx_coal_pkt = v;
3286 if (ifp->if_flags & IFF_RUNNING)
3287 jme_set_rx_coal(sc);
3290 ifnet_deserialize_all(ifp);
3295 jme_set_tx_coal(struct jme_softc *sc)
3299 reg = (sc->jme_tx_coal_to << PCCTX_COAL_TO_SHIFT) &
3301 reg |= (sc->jme_tx_coal_pkt << PCCTX_COAL_PKT_SHIFT) &
3302 PCCTX_COAL_PKT_MASK;
3303 reg |= PCCTX_COAL_TXQ0;
3304 CSR_WRITE_4(sc, JME_PCCTX, reg);
3308 jme_set_rx_coal(struct jme_softc *sc)
3313 reg = (sc->jme_rx_coal_to << PCCRX_COAL_TO_SHIFT) &
3315 reg |= (sc->jme_rx_coal_pkt << PCCRX_COAL_PKT_SHIFT) &
3316 PCCRX_COAL_PKT_MASK;
3317 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r)
3318 CSR_WRITE_4(sc, JME_PCCRX(r), reg);
3321 #ifdef IFPOLL_ENABLE
3324 jme_npoll_status(struct ifnet *ifp)
3326 struct jme_softc *sc = ifp->if_softc;
3329 ASSERT_SERIALIZED(&sc->jme_serialize);
3331 status = CSR_READ_4(sc, JME_INTR_STATUS);
3332 if (status & INTR_RXQ_DESC_EMPTY) {
3333 CSR_WRITE_4(sc, JME_INTR_STATUS, status & INTR_RXQ_DESC_EMPTY);
3334 jme_rx_restart(sc, status);
3339 jme_npoll_rx(struct ifnet *ifp __unused, void *arg, int cycle)
3341 struct jme_rxdata *rdata = arg;
3343 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3345 jme_rxeof(rdata, cycle);
3349 jme_npoll_tx(struct ifnet *ifp, void *arg, int cycle __unused)
3351 struct jme_txdata *tdata = arg;
3353 ASSERT_SERIALIZED(&tdata->jme_tx_serialize);
3356 if (!ifq_is_empty(&ifp->if_snd))
3361 jme_npoll(struct ifnet *ifp, struct ifpoll_info *info)
3363 struct jme_softc *sc = ifp->if_softc;
3365 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3370 info->ifpi_status.status_func = jme_npoll_status;
3371 info->ifpi_status.serializer = &sc->jme_serialize;
3373 off = sc->jme_npoll_txoff;
3374 KKASSERT(off <= ncpus2);
3375 info->ifpi_tx[off].poll_func = jme_npoll_tx;
3376 info->ifpi_tx[off].arg = &sc->jme_cdata.jme_tx_data;
3377 info->ifpi_tx[off].serializer =
3378 &sc->jme_cdata.jme_tx_data.jme_tx_serialize;
3379 ifq_set_cpuid(&ifp->if_snd, sc->jme_npoll_txoff);
3381 off = sc->jme_npoll_rxoff;
3382 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
3383 struct jme_rxdata *rdata =
3384 &sc->jme_cdata.jme_rx_data[i];
3387 info->ifpi_rx[idx].poll_func = jme_npoll_rx;
3388 info->ifpi_rx[idx].arg = rdata;
3389 info->ifpi_rx[idx].serializer =
3390 &rdata->jme_rx_serialize;
3393 if (ifp->if_flags & IFF_RUNNING)
3394 jme_disable_intr(sc);
3396 ifq_set_cpuid(&ifp->if_snd, sc->jme_tx_cpuid);
3397 if (ifp->if_flags & IFF_RUNNING)
3398 jme_enable_intr(sc);
3403 jme_sysctl_npoll_rxoff(SYSCTL_HANDLER_ARGS)
3405 struct jme_softc *sc = (void *)arg1;
3406 struct ifnet *ifp = &sc->arpcom.ac_if;
3409 off = sc->jme_npoll_rxoff;
3410 error = sysctl_handle_int(oidp, &off, 0, req);
3411 if (error || req->newptr == NULL)
3416 ifnet_serialize_all(ifp);
3417 if (off >= ncpus2 || off % sc->jme_cdata.jme_rx_ring_cnt != 0) {
3421 sc->jme_npoll_rxoff = off;
3423 ifnet_deserialize_all(ifp);
3429 jme_sysctl_npoll_txoff(SYSCTL_HANDLER_ARGS)
3431 struct jme_softc *sc = (void *)arg1;
3432 struct ifnet *ifp = &sc->arpcom.ac_if;
3435 off = sc->jme_npoll_txoff;
3436 error = sysctl_handle_int(oidp, &off, 0, req);
3437 if (error || req->newptr == NULL)
3442 ifnet_serialize_all(ifp);
3443 if (off >= ncpus2) {
3447 sc->jme_npoll_txoff = off;
3449 ifnet_deserialize_all(ifp);
3454 #endif /* IFPOLL_ENABLE */
3457 jme_rxring_dma_alloc(struct jme_rxdata *rdata)
3462 asize = roundup2(JME_RX_RING_SIZE(rdata), JME_RX_RING_ALIGN);
3463 error = bus_dmamem_coherent(rdata->jme_sc->jme_cdata.jme_ring_tag,
3464 JME_RX_RING_ALIGN, 0,
3465 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3466 asize, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
3468 device_printf(rdata->jme_sc->jme_dev,
3469 "could not allocate %dth Rx ring.\n", rdata->jme_rx_idx);
3472 rdata->jme_rx_ring_tag = dmem.dmem_tag;
3473 rdata->jme_rx_ring_map = dmem.dmem_map;
3474 rdata->jme_rx_ring = dmem.dmem_addr;
3475 rdata->jme_rx_ring_paddr = dmem.dmem_busaddr;
3481 jme_rxbuf_dma_filter(void *arg __unused, bus_addr_t paddr)
3483 if ((paddr & 0xffffffff) == 0) {
3485 * Don't allow lower 32bits of the RX buffer's
3486 * physical address to be 0, else it will break
3487 * hardware pending RSS information delivery
3488 * detection on RX path.
3496 jme_rxbuf_dma_alloc(struct jme_rxdata *rdata)
3501 lowaddr = BUS_SPACE_MAXADDR;
3502 if (JME_ENABLE_HWRSS(rdata->jme_sc)) {
3503 /* jme_rxbuf_dma_filter will be called */
3504 lowaddr = BUS_SPACE_MAXADDR_32BIT;
3507 /* Create tag for Rx buffers. */
3508 error = bus_dma_tag_create(
3509 rdata->jme_sc->jme_cdata.jme_buffer_tag,/* parent */
3510 JME_RX_BUF_ALIGN, 0, /* algnmnt, boundary */
3511 lowaddr, /* lowaddr */
3512 BUS_SPACE_MAXADDR, /* highaddr */
3513 jme_rxbuf_dma_filter, NULL, /* filter, filterarg */
3514 MCLBYTES, /* maxsize */
3516 MCLBYTES, /* maxsegsize */
3517 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ALIGNED,/* flags */
3518 &rdata->jme_rx_tag);
3520 device_printf(rdata->jme_sc->jme_dev,
3521 "could not create %dth Rx DMA tag.\n", rdata->jme_rx_idx);
3525 /* Create DMA maps for Rx buffers. */
3526 error = bus_dmamap_create(rdata->jme_rx_tag, BUS_DMA_WAITOK,
3527 &rdata->jme_rx_sparemap);
3529 device_printf(rdata->jme_sc->jme_dev,
3530 "could not create %dth spare Rx dmamap.\n",
3532 bus_dma_tag_destroy(rdata->jme_rx_tag);
3533 rdata->jme_rx_tag = NULL;
3536 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
3537 struct jme_rxdesc *rxd = &rdata->jme_rxdesc[i];
3539 error = bus_dmamap_create(rdata->jme_rx_tag, BUS_DMA_WAITOK,
3544 device_printf(rdata->jme_sc->jme_dev,
3545 "could not create %dth Rx dmamap "
3546 "for %dth RX ring.\n", i, rdata->jme_rx_idx);
3548 for (j = 0; j < i; ++j) {
3549 rxd = &rdata->jme_rxdesc[j];
3550 bus_dmamap_destroy(rdata->jme_rx_tag,
3553 bus_dmamap_destroy(rdata->jme_rx_tag,
3554 rdata->jme_rx_sparemap);
3555 bus_dma_tag_destroy(rdata->jme_rx_tag);
3556 rdata->jme_rx_tag = NULL;
3564 jme_rx_intr(struct jme_softc *sc, uint32_t status)
3568 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
3569 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
3571 if (status & rdata->jme_rx_coal) {
3572 lwkt_serialize_enter(&rdata->jme_rx_serialize);
3573 jme_rxeof(rdata, -1);
3574 lwkt_serialize_exit(&rdata->jme_rx_serialize);
3580 jme_enable_rss(struct jme_softc *sc)
3583 uint8_t key[RSSKEY_NREGS * RSSKEY_REGSIZE];
3586 KASSERT(sc->jme_cdata.jme_rx_ring_cnt == JME_NRXRING_2 ||
3587 sc->jme_cdata.jme_rx_ring_cnt == JME_NRXRING_4,
3588 ("%s: invalid # of RX rings (%d)",
3589 sc->arpcom.ac_if.if_xname, sc->jme_cdata.jme_rx_ring_cnt));
3591 rssc = RSSC_HASH_64_ENTRY;
3592 rssc |= RSSC_HASH_IPV4 | RSSC_HASH_IPV4_TCP;
3593 rssc |= sc->jme_cdata.jme_rx_ring_cnt >> 1;
3594 JME_RSS_DPRINTF(sc, 1, "rssc 0x%08x\n", rssc);
3595 CSR_WRITE_4(sc, JME_RSSC, rssc);
3597 toeplitz_get_key(key, sizeof(key));
3598 for (i = 0; i < RSSKEY_NREGS; ++i) {
3601 keyreg = RSSKEY_REGVAL(key, i);
3602 JME_RSS_DPRINTF(sc, 5, "keyreg%d 0x%08x, reg 0x%08x\n",
3603 i, keyreg, RSSKEY_REG(RSSKEY_NREGS - 1 - i));
3605 CSR_WRITE_4(sc, RSSKEY_REG(RSSKEY_NREGS - 1 - i), keyreg);
3609 * Create redirect table in following fashion:
3610 * (hash & ring_cnt_mask) == rdr_table[(hash & rdr_table_mask)]
3613 for (i = 0; i < RSSTBL_REGSIZE; ++i) {
3616 q = i % sc->jme_cdata.jme_rx_ring_cnt;
3617 ind |= q << (i * 8);
3619 JME_RSS_DPRINTF(sc, 1, "ind 0x%08x\n", ind);
3621 for (i = 0; i < RSSTBL_NREGS; ++i)
3622 CSR_WRITE_4(sc, RSSTBL_REG(i), ind);
3626 jme_disable_rss(struct jme_softc *sc)
3628 CSR_WRITE_4(sc, JME_RSSC, RSSC_DIS_RSS);
3632 jme_serialize(struct ifnet *ifp, enum ifnet_serialize slz)
3634 struct jme_softc *sc = ifp->if_softc;
3636 ifnet_serialize_array_enter(sc->jme_serialize_arr,
3637 sc->jme_serialize_cnt, JME_TX_SERIALIZE, JME_RX_SERIALIZE, slz);
3641 jme_deserialize(struct ifnet *ifp, enum ifnet_serialize slz)
3643 struct jme_softc *sc = ifp->if_softc;
3645 ifnet_serialize_array_exit(sc->jme_serialize_arr,
3646 sc->jme_serialize_cnt, JME_TX_SERIALIZE, JME_RX_SERIALIZE, slz);
3650 jme_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz)
3652 struct jme_softc *sc = ifp->if_softc;
3654 return ifnet_serialize_array_try(sc->jme_serialize_arr,
3655 sc->jme_serialize_cnt, JME_TX_SERIALIZE, JME_RX_SERIALIZE, slz);
3661 jme_serialize_assert(struct ifnet *ifp, enum ifnet_serialize slz,
3662 boolean_t serialized)
3664 struct jme_softc *sc = ifp->if_softc;
3666 ifnet_serialize_array_assert(sc->jme_serialize_arr,
3667 sc->jme_serialize_cnt, JME_TX_SERIALIZE, JME_RX_SERIALIZE,
3671 #endif /* INVARIANTS */
3674 jme_msix_try_alloc(device_t dev)
3676 struct jme_softc *sc = device_get_softc(dev);
3677 struct jme_msix_data *msix;
3678 int error, i, r, msix_enable, msix_count;
3679 int offset, offset_def;
3681 msix_count = JME_MSIXCNT(sc->jme_cdata.jme_rx_ring_cnt);
3682 KKASSERT(msix_count <= JME_NMSIX);
3684 msix_enable = device_getenv_int(dev, "msix.enable", jme_msix_enable);
3687 * We leave the 1st MSI-X vector unused, so we
3688 * actually need msix_count + 1 MSI-X vectors.
3690 if (!msix_enable || pci_msix_count(dev) < (msix_count + 1))
3693 for (i = 0; i < msix_count; ++i)
3694 sc->jme_msix[i].jme_msix_rid = -1;
3699 * Setup status MSI-X
3702 msix = &sc->jme_msix[i++];
3703 msix->jme_msix_cpuid = 0;
3704 msix->jme_msix_arg = sc;
3705 msix->jme_msix_func = jme_msix_status;
3706 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
3707 msix->jme_msix_intrs |=
3708 sc->jme_cdata.jme_rx_data[r].jme_rx_empty;
3710 msix->jme_msix_serialize = &sc->jme_serialize;
3711 ksnprintf(msix->jme_msix_desc, sizeof(msix->jme_msix_desc), "%s sts",
3712 device_get_nameunit(dev));
3718 offset_def = device_get_unit(dev) % ncpus2;
3719 offset = device_getenv_int(dev, "msix.txoff", offset_def);
3720 if (offset >= ncpus2) {
3721 device_printf(dev, "invalid msix.txoff %d, use %d\n",
3722 offset, offset_def);
3723 offset = offset_def;
3726 msix = &sc->jme_msix[i++];
3727 msix->jme_msix_cpuid = offset;
3728 sc->jme_tx_cpuid = msix->jme_msix_cpuid;
3729 msix->jme_msix_arg = &sc->jme_cdata.jme_tx_data;
3730 msix->jme_msix_func = jme_msix_tx;
3731 msix->jme_msix_intrs = INTR_TXQ_COAL | INTR_TXQ_COAL_TO;
3732 msix->jme_msix_serialize = &sc->jme_cdata.jme_tx_data.jme_tx_serialize;
3733 ksnprintf(msix->jme_msix_desc, sizeof(msix->jme_msix_desc), "%s tx",
3734 device_get_nameunit(dev));
3740 if (sc->jme_cdata.jme_rx_ring_cnt == ncpus2) {
3743 offset_def = (sc->jme_cdata.jme_rx_ring_cnt *
3744 device_get_unit(dev)) % ncpus2;
3746 offset = device_getenv_int(dev, "msix.rxoff", offset_def);
3747 if (offset >= ncpus2 ||
3748 offset % sc->jme_cdata.jme_rx_ring_cnt != 0) {
3749 device_printf(dev, "invalid msix.rxoff %d, use %d\n",
3750 offset, offset_def);
3751 offset = offset_def;
3755 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
3756 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
3758 msix = &sc->jme_msix[i++];
3759 msix->jme_msix_cpuid = r + offset;
3760 KKASSERT(msix->jme_msix_cpuid < ncpus2);
3761 msix->jme_msix_arg = rdata;
3762 msix->jme_msix_func = jme_msix_rx;
3763 msix->jme_msix_intrs = rdata->jme_rx_coal;
3764 msix->jme_msix_serialize = &rdata->jme_rx_serialize;
3765 ksnprintf(msix->jme_msix_desc, sizeof(msix->jme_msix_desc),
3766 "%s rx%d", device_get_nameunit(dev), r);
3769 KKASSERT(i == msix_count);
3771 error = pci_setup_msix(dev);
3775 /* Setup jme_msix_cnt early, so we could cleanup */
3776 sc->jme_msix_cnt = msix_count;
3778 for (i = 0; i < msix_count; ++i) {
3779 msix = &sc->jme_msix[i];
3781 msix->jme_msix_vector = i + 1;
3782 error = pci_alloc_msix_vector(dev, msix->jme_msix_vector,
3783 &msix->jme_msix_rid, msix->jme_msix_cpuid);
3787 msix->jme_msix_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
3788 &msix->jme_msix_rid, RF_ACTIVE);
3789 if (msix->jme_msix_res == NULL) {
3795 for (i = 0; i < JME_INTR_CNT; ++i) {
3796 uint32_t intr_mask = (1 << i);
3799 if ((JME_INTRS & intr_mask) == 0)
3802 for (x = 0; x < msix_count; ++x) {
3803 msix = &sc->jme_msix[x];
3804 if (msix->jme_msix_intrs & intr_mask) {
3807 reg = i / JME_MSINUM_FACTOR;
3808 KKASSERT(reg < JME_MSINUM_CNT);
3810 shift = (i % JME_MSINUM_FACTOR) * 4;
3812 sc->jme_msinum[reg] |=
3813 (msix->jme_msix_vector << shift);
3821 for (i = 0; i < JME_MSINUM_CNT; ++i) {
3822 device_printf(dev, "MSINUM%d: %#x\n", i,
3827 pci_enable_msix(dev);
3828 sc->jme_irq_type = PCI_INTR_TYPE_MSIX;
3836 jme_intr_alloc(device_t dev)
3838 struct jme_softc *sc = device_get_softc(dev);
3841 jme_msix_try_alloc(dev);
3843 if (sc->jme_irq_type != PCI_INTR_TYPE_MSIX) {
3844 sc->jme_irq_type = pci_alloc_1intr(dev, jme_msi_enable,
3845 &sc->jme_irq_rid, &irq_flags);
3847 sc->jme_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
3848 &sc->jme_irq_rid, irq_flags);
3849 if (sc->jme_irq_res == NULL) {
3850 device_printf(dev, "can't allocate irq\n");
3853 sc->jme_tx_cpuid = rman_get_cpuid(sc->jme_irq_res);
3859 jme_msix_free(device_t dev)
3861 struct jme_softc *sc = device_get_softc(dev);
3864 KKASSERT(sc->jme_msix_cnt > 1);
3866 for (i = 0; i < sc->jme_msix_cnt; ++i) {
3867 struct jme_msix_data *msix = &sc->jme_msix[i];
3869 if (msix->jme_msix_res != NULL) {
3870 bus_release_resource(dev, SYS_RES_IRQ,
3871 msix->jme_msix_rid, msix->jme_msix_res);
3872 msix->jme_msix_res = NULL;
3874 if (msix->jme_msix_rid >= 0) {
3875 pci_release_msix_vector(dev, msix->jme_msix_rid);
3876 msix->jme_msix_rid = -1;
3879 pci_teardown_msix(dev);
3883 jme_intr_free(device_t dev)
3885 struct jme_softc *sc = device_get_softc(dev);
3887 if (sc->jme_irq_type != PCI_INTR_TYPE_MSIX) {
3888 if (sc->jme_irq_res != NULL) {
3889 bus_release_resource(dev, SYS_RES_IRQ, sc->jme_irq_rid,
3892 if (sc->jme_irq_type == PCI_INTR_TYPE_MSI)
3893 pci_release_msi(dev);
3900 jme_msix_tx(void *xtdata)
3902 struct jme_txdata *tdata = xtdata;
3903 struct jme_softc *sc = tdata->jme_sc;
3904 struct ifnet *ifp = &sc->arpcom.ac_if;
3906 ASSERT_SERIALIZED(&tdata->jme_tx_serialize);
3908 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, INTR_TXQ_COAL | INTR_TXQ_COAL_TO);
3910 CSR_WRITE_4(sc, JME_INTR_STATUS,
3911 INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP);
3913 if (ifp->if_flags & IFF_RUNNING) {
3915 if (!ifq_is_empty(&ifp->if_snd))
3919 CSR_WRITE_4(sc, JME_INTR_MASK_SET, INTR_TXQ_COAL | INTR_TXQ_COAL_TO);
3923 jme_msix_rx(void *xrdata)
3925 struct jme_rxdata *rdata = xrdata;
3926 struct jme_softc *sc = rdata->jme_sc;
3927 struct ifnet *ifp = &sc->arpcom.ac_if;
3929 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3931 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, rdata->jme_rx_coal);
3933 CSR_WRITE_4(sc, JME_INTR_STATUS,
3934 rdata->jme_rx_coal | rdata->jme_rx_comp);
3936 if (ifp->if_flags & IFF_RUNNING)
3937 jme_rxeof(rdata, -1);
3939 CSR_WRITE_4(sc, JME_INTR_MASK_SET, rdata->jme_rx_coal);
3943 jme_msix_status(void *xsc)
3945 struct jme_softc *sc = xsc;
3946 struct ifnet *ifp = &sc->arpcom.ac_if;
3949 ASSERT_SERIALIZED(&sc->jme_serialize);
3951 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, INTR_RXQ_DESC_EMPTY);
3953 status = CSR_READ_4(sc, JME_INTR_STATUS);
3955 if (status & INTR_RXQ_DESC_EMPTY) {
3956 CSR_WRITE_4(sc, JME_INTR_STATUS, status & INTR_RXQ_DESC_EMPTY);
3957 if (ifp->if_flags & IFF_RUNNING)
3958 jme_rx_restart(sc, status);
3961 CSR_WRITE_4(sc, JME_INTR_MASK_SET, INTR_RXQ_DESC_EMPTY);
3965 jme_rx_restart(struct jme_softc *sc, uint32_t status)
3969 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
3970 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[i];
3972 if (status & rdata->jme_rx_empty) {
3973 lwkt_serialize_enter(&rdata->jme_rx_serialize);
3974 jme_rxeof(rdata, -1);
3975 #ifdef JME_RSS_DEBUG
3976 rdata->jme_rx_emp++;
3978 lwkt_serialize_exit(&rdata->jme_rx_serialize);
3981 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RX_ENB |
3986 jme_set_msinum(struct jme_softc *sc)
3990 for (i = 0; i < JME_MSINUM_CNT; ++i)
3991 CSR_WRITE_4(sc, JME_MSINUM(i), sc->jme_msinum[i]);
3995 jme_intr_setup(device_t dev)
3997 struct jme_softc *sc = device_get_softc(dev);
4000 if (sc->jme_irq_type == PCI_INTR_TYPE_MSIX)
4001 return jme_msix_setup(dev);
4003 error = bus_setup_intr(dev, sc->jme_irq_res, INTR_MPSAFE,
4004 jme_intr, sc, &sc->jme_irq_handle, &sc->jme_serialize);
4006 device_printf(dev, "could not set up interrupt handler.\n");
4014 jme_intr_teardown(device_t dev)
4016 struct jme_softc *sc = device_get_softc(dev);
4018 if (sc->jme_irq_type == PCI_INTR_TYPE_MSIX)
4019 jme_msix_teardown(dev, sc->jme_msix_cnt);
4021 bus_teardown_intr(dev, sc->jme_irq_res, sc->jme_irq_handle);
4025 jme_msix_setup(device_t dev)
4027 struct jme_softc *sc = device_get_softc(dev);
4030 for (x = 0; x < sc->jme_msix_cnt; ++x) {
4031 struct jme_msix_data *msix = &sc->jme_msix[x];
4034 error = bus_setup_intr_descr(dev, msix->jme_msix_res,
4035 INTR_MPSAFE, msix->jme_msix_func, msix->jme_msix_arg,
4036 &msix->jme_msix_handle, msix->jme_msix_serialize,
4037 msix->jme_msix_desc);
4039 device_printf(dev, "could not set up %s "
4040 "interrupt handler.\n", msix->jme_msix_desc);
4041 jme_msix_teardown(dev, x);
4049 jme_msix_teardown(device_t dev, int msix_count)
4051 struct jme_softc *sc = device_get_softc(dev);
4054 for (x = 0; x < msix_count; ++x) {
4055 struct jme_msix_data *msix = &sc->jme_msix[x];
4057 bus_teardown_intr(dev, msix->jme_msix_res,
4058 msix->jme_msix_handle);
4063 jme_serialize_skipmain(struct jme_softc *sc)
4065 lwkt_serialize_array_enter(sc->jme_serialize_arr,
4066 sc->jme_serialize_cnt, 1);
4070 jme_deserialize_skipmain(struct jme_softc *sc)
4072 lwkt_serialize_array_exit(sc->jme_serialize_arr,
4073 sc->jme_serialize_cnt, 1);
4077 jme_enable_intr(struct jme_softc *sc)
4081 for (i = 0; i < sc->jme_serialize_cnt; ++i)
4082 lwkt_serialize_handler_enable(sc->jme_serialize_arr[i]);
4084 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
4088 jme_disable_intr(struct jme_softc *sc)
4092 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
4094 for (i = 0; i < sc->jme_serialize_cnt; ++i)
4095 lwkt_serialize_handler_disable(sc->jme_serialize_arr[i]);