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_polling.h"
34 #include <sys/param.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
38 #include <sys/interrupt.h>
39 #include <sys/malloc.h>
42 #include <sys/serialize.h>
43 #include <sys/serialize2.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/sysctl.h>
48 #include <net/ethernet.h>
51 #include <net/if_arp.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/ifq_var.h>
55 #include <net/toeplitz.h>
56 #include <net/toeplitz2.h>
57 #include <net/vlan/if_vlan_var.h>
58 #include <net/vlan/if_vlan_ether.h>
60 #include <netinet/in.h>
62 #include <dev/netif/mii_layer/miivar.h>
63 #include <dev/netif/mii_layer/jmphyreg.h>
65 #include <bus/pci/pcireg.h>
66 #include <bus/pci/pcivar.h>
67 #include <bus/pci/pcidevs.h>
69 #include <dev/netif/jme/if_jmereg.h>
70 #include <dev/netif/jme/if_jmevar.h>
72 #include "miibus_if.h"
74 /* Define the following to disable printing Rx errors. */
75 #undef JME_SHOW_ERRORS
77 #define JME_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
80 #define JME_RSS_DPRINTF(sc, lvl, fmt, ...) \
82 if ((sc)->jme_rss_debug >= (lvl)) \
83 if_printf(&(sc)->arpcom.ac_if, fmt, __VA_ARGS__); \
85 #else /* !JME_RSS_DEBUG */
86 #define JME_RSS_DPRINTF(sc, lvl, fmt, ...) ((void)0)
87 #endif /* JME_RSS_DEBUG */
89 static int jme_probe(device_t);
90 static int jme_attach(device_t);
91 static int jme_detach(device_t);
92 static int jme_shutdown(device_t);
93 static int jme_suspend(device_t);
94 static int jme_resume(device_t);
96 static int jme_miibus_readreg(device_t, int, int);
97 static int jme_miibus_writereg(device_t, int, int, int);
98 static void jme_miibus_statchg(device_t);
100 static void jme_init(void *);
101 static int jme_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
102 static void jme_start(struct ifnet *);
103 static void jme_watchdog(struct ifnet *);
104 static void jme_mediastatus(struct ifnet *, struct ifmediareq *);
105 static int jme_mediachange(struct ifnet *);
106 #ifdef DEVICE_POLLING
107 static void jme_poll(struct ifnet *, enum poll_cmd, int);
109 static void jme_serialize(struct ifnet *, enum ifnet_serialize);
110 static void jme_deserialize(struct ifnet *, enum ifnet_serialize);
111 static int jme_tryserialize(struct ifnet *, enum ifnet_serialize);
113 static void jme_serialize_assert(struct ifnet *, enum ifnet_serialize,
117 static void jme_intr(void *);
118 static void jme_msix_tx(void *);
119 static void jme_msix_rx(void *);
120 static void jme_txeof(struct jme_softc *);
121 static void jme_rxeof(struct jme_softc *, int, int);
122 static void jme_rx_intr(struct jme_softc *, uint32_t);
124 static int jme_msix_setup(device_t);
125 static void jme_msix_teardown(device_t, int);
126 static int jme_intr_setup(device_t);
127 static void jme_intr_teardown(device_t);
128 static void jme_msix_try_alloc(device_t);
129 static void jme_msix_free(device_t);
130 static int jme_intr_alloc(device_t);
131 static void jme_intr_free(device_t);
132 static int jme_dma_alloc(struct jme_softc *);
133 static void jme_dma_free(struct jme_softc *);
134 static int jme_init_rx_ring(struct jme_softc *, int);
135 static void jme_init_tx_ring(struct jme_softc *);
136 static void jme_init_ssb(struct jme_softc *);
137 static int jme_newbuf(struct jme_softc *, int, struct jme_rxdesc *, int);
138 static int jme_encap(struct jme_softc *, struct mbuf **);
139 static void jme_rxpkt(struct jme_softc *, int);
140 static int jme_rxring_dma_alloc(struct jme_softc *, int);
141 static int jme_rxbuf_dma_alloc(struct jme_softc *, int);
143 static void jme_tick(void *);
144 static void jme_stop(struct jme_softc *);
145 static void jme_reset(struct jme_softc *);
146 static void jme_set_msinum(struct jme_softc *);
147 static void jme_set_vlan(struct jme_softc *);
148 static void jme_set_filter(struct jme_softc *);
149 static void jme_stop_tx(struct jme_softc *);
150 static void jme_stop_rx(struct jme_softc *);
151 static void jme_mac_config(struct jme_softc *);
152 static void jme_reg_macaddr(struct jme_softc *, uint8_t[]);
153 static int jme_eeprom_macaddr(struct jme_softc *, uint8_t[]);
154 static int jme_eeprom_read_byte(struct jme_softc *, uint8_t, uint8_t *);
156 static void jme_setwol(struct jme_softc *);
157 static void jme_setlinkspeed(struct jme_softc *);
159 static void jme_set_tx_coal(struct jme_softc *);
160 static void jme_set_rx_coal(struct jme_softc *);
161 static void jme_enable_rss(struct jme_softc *);
162 static void jme_disable_rss(struct jme_softc *);
164 static void jme_sysctl_node(struct jme_softc *);
165 static int jme_sysctl_tx_coal_to(SYSCTL_HANDLER_ARGS);
166 static int jme_sysctl_tx_coal_pkt(SYSCTL_HANDLER_ARGS);
167 static int jme_sysctl_rx_coal_to(SYSCTL_HANDLER_ARGS);
168 static int jme_sysctl_rx_coal_pkt(SYSCTL_HANDLER_ARGS);
171 * Devices supported by this driver.
173 static const struct jme_dev {
174 uint16_t jme_vendorid;
175 uint16_t jme_deviceid;
177 const char *jme_name;
179 { PCI_VENDOR_JMICRON, PCI_PRODUCT_JMICRON_JMC250,
181 "JMicron Inc, JMC250 Gigabit Ethernet" },
182 { PCI_VENDOR_JMICRON, PCI_PRODUCT_JMICRON_JMC260,
184 "JMicron Inc, JMC260 Fast Ethernet" },
188 static device_method_t jme_methods[] = {
189 /* Device interface. */
190 DEVMETHOD(device_probe, jme_probe),
191 DEVMETHOD(device_attach, jme_attach),
192 DEVMETHOD(device_detach, jme_detach),
193 DEVMETHOD(device_shutdown, jme_shutdown),
194 DEVMETHOD(device_suspend, jme_suspend),
195 DEVMETHOD(device_resume, jme_resume),
198 DEVMETHOD(bus_print_child, bus_generic_print_child),
199 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
202 DEVMETHOD(miibus_readreg, jme_miibus_readreg),
203 DEVMETHOD(miibus_writereg, jme_miibus_writereg),
204 DEVMETHOD(miibus_statchg, jme_miibus_statchg),
209 static driver_t jme_driver = {
212 sizeof(struct jme_softc)
215 static devclass_t jme_devclass;
217 DECLARE_DUMMY_MODULE(if_jme);
218 MODULE_DEPEND(if_jme, miibus, 1, 1, 1);
219 DRIVER_MODULE(if_jme, pci, jme_driver, jme_devclass, NULL, NULL);
220 DRIVER_MODULE(miibus, jme, miibus_driver, miibus_devclass, NULL, NULL);
222 static const struct {
226 } jme_rx_status[JME_NRXRING_MAX] = {
227 { INTR_RXQ0_COAL | INTR_RXQ0_COAL_TO, INTR_RXQ0_COMP,
228 INTR_RXQ0_DESC_EMPTY },
229 { INTR_RXQ1_COAL | INTR_RXQ1_COAL_TO, INTR_RXQ1_COMP,
230 INTR_RXQ1_DESC_EMPTY },
231 { INTR_RXQ2_COAL | INTR_RXQ2_COAL_TO, INTR_RXQ2_COMP,
232 INTR_RXQ2_DESC_EMPTY },
233 { INTR_RXQ3_COAL | INTR_RXQ3_COAL_TO, INTR_RXQ3_COMP,
234 INTR_RXQ3_DESC_EMPTY }
237 static int jme_rx_desc_count = JME_RX_DESC_CNT_DEF;
238 static int jme_tx_desc_count = JME_TX_DESC_CNT_DEF;
239 static int jme_rx_ring_count = 1;
240 static int jme_msi_enable = 1;
241 static int jme_msix_enable = 1;
243 TUNABLE_INT("hw.jme.rx_desc_count", &jme_rx_desc_count);
244 TUNABLE_INT("hw.jme.tx_desc_count", &jme_tx_desc_count);
245 TUNABLE_INT("hw.jme.rx_ring_count", &jme_rx_ring_count);
246 TUNABLE_INT("hw.jme.msi.enable", &jme_msi_enable);
247 TUNABLE_INT("hw.jme.msix.enable", &jme_msix_enable);
250 * Read a PHY register on the MII of the JMC250.
253 jme_miibus_readreg(device_t dev, int phy, int reg)
255 struct jme_softc *sc = device_get_softc(dev);
259 /* For FPGA version, PHY address 0 should be ignored. */
260 if (sc->jme_caps & JME_CAP_FPGA) {
264 if (sc->jme_phyaddr != phy)
268 CSR_WRITE_4(sc, JME_SMI, SMI_OP_READ | SMI_OP_EXECUTE |
269 SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg));
271 for (i = JME_PHY_TIMEOUT; i > 0; i--) {
273 if (((val = CSR_READ_4(sc, JME_SMI)) & SMI_OP_EXECUTE) == 0)
277 device_printf(sc->jme_dev, "phy read timeout: "
278 "phy %d, reg %d\n", phy, reg);
282 return ((val & SMI_DATA_MASK) >> SMI_DATA_SHIFT);
286 * Write a PHY register on the MII of the JMC250.
289 jme_miibus_writereg(device_t dev, int phy, int reg, int val)
291 struct jme_softc *sc = device_get_softc(dev);
294 /* For FPGA version, PHY address 0 should be ignored. */
295 if (sc->jme_caps & JME_CAP_FPGA) {
299 if (sc->jme_phyaddr != phy)
303 CSR_WRITE_4(sc, JME_SMI, SMI_OP_WRITE | SMI_OP_EXECUTE |
304 ((val << SMI_DATA_SHIFT) & SMI_DATA_MASK) |
305 SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg));
307 for (i = JME_PHY_TIMEOUT; i > 0; i--) {
309 if (((val = CSR_READ_4(sc, JME_SMI)) & SMI_OP_EXECUTE) == 0)
313 device_printf(sc->jme_dev, "phy write timeout: "
314 "phy %d, reg %d\n", phy, reg);
321 * Callback from MII layer when media changes.
324 jme_miibus_statchg(device_t dev)
326 struct jme_softc *sc = device_get_softc(dev);
327 struct ifnet *ifp = &sc->arpcom.ac_if;
328 struct mii_data *mii;
329 struct jme_txdesc *txd;
333 ASSERT_IFNET_SERIALIZED_ALL(ifp);
335 if ((ifp->if_flags & IFF_RUNNING) == 0)
338 mii = device_get_softc(sc->jme_miibus);
340 sc->jme_flags &= ~JME_FLAG_LINK;
341 if ((mii->mii_media_status & IFM_AVALID) != 0) {
342 switch (IFM_SUBTYPE(mii->mii_media_active)) {
345 sc->jme_flags |= JME_FLAG_LINK;
348 if (sc->jme_caps & JME_CAP_FASTETH)
350 sc->jme_flags |= JME_FLAG_LINK;
358 * Disabling Rx/Tx MACs have a side-effect of resetting
359 * JME_TXNDA/JME_RXNDA register to the first address of
360 * Tx/Rx descriptor address. So driver should reset its
361 * internal procucer/consumer pointer and reclaim any
362 * allocated resources. Note, just saving the value of
363 * JME_TXNDA and JME_RXNDA registers before stopping MAC
364 * and restoring JME_TXNDA/JME_RXNDA register is not
365 * sufficient to make sure correct MAC state because
366 * stopping MAC operation can take a while and hardware
367 * might have updated JME_TXNDA/JME_RXNDA registers
368 * during the stop operation.
371 /* Disable interrupts */
372 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
375 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
377 callout_stop(&sc->jme_tick_ch);
379 /* Stop receiver/transmitter. */
383 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
384 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
386 jme_rxeof(sc, r, -1);
387 if (rdata->jme_rxhead != NULL)
388 m_freem(rdata->jme_rxhead);
389 JME_RXCHAIN_RESET(sc, r);
392 * Reuse configured Rx descriptors and reset
393 * procuder/consumer index.
395 rdata->jme_rx_cons = 0;
399 if (sc->jme_cdata.jme_tx_cnt != 0) {
400 /* Remove queued packets for transmit. */
401 for (i = 0; i < sc->jme_tx_desc_cnt; i++) {
402 txd = &sc->jme_cdata.jme_txdesc[i];
403 if (txd->tx_m != NULL) {
405 sc->jme_cdata.jme_tx_tag,
414 jme_init_tx_ring(sc);
416 /* Initialize shadow status block. */
419 /* Program MAC with resolved speed/duplex/flow-control. */
420 if (sc->jme_flags & JME_FLAG_LINK) {
423 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr);
425 /* Set Tx ring address to the hardware. */
426 paddr = sc->jme_cdata.jme_tx_ring_paddr;
427 CSR_WRITE_4(sc, JME_TXDBA_HI, JME_ADDR_HI(paddr));
428 CSR_WRITE_4(sc, JME_TXDBA_LO, JME_ADDR_LO(paddr));
430 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
431 CSR_WRITE_4(sc, JME_RXCSR,
432 sc->jme_rxcsr | RXCSR_RXQ_N_SEL(r));
434 /* Set Rx ring address to the hardware. */
435 paddr = sc->jme_cdata.jme_rx_data[r].jme_rx_ring_paddr;
436 CSR_WRITE_4(sc, JME_RXDBA_HI, JME_ADDR_HI(paddr));
437 CSR_WRITE_4(sc, JME_RXDBA_LO, JME_ADDR_LO(paddr));
440 /* Restart receiver/transmitter. */
441 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RX_ENB |
443 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr | TXCSR_TX_ENB);
446 ifp->if_flags |= IFF_RUNNING;
447 ifp->if_flags &= ~IFF_OACTIVE;
448 callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
450 #ifdef DEVICE_POLLING
451 if (!(ifp->if_flags & IFF_POLLING))
453 /* Reenable interrupts. */
454 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
458 * Get the current interface media status.
461 jme_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
463 struct jme_softc *sc = ifp->if_softc;
464 struct mii_data *mii = device_get_softc(sc->jme_miibus);
466 ASSERT_IFNET_SERIALIZED_ALL(ifp);
469 ifmr->ifm_status = mii->mii_media_status;
470 ifmr->ifm_active = mii->mii_media_active;
474 * Set hardware to newly-selected media.
477 jme_mediachange(struct ifnet *ifp)
479 struct jme_softc *sc = ifp->if_softc;
480 struct mii_data *mii = device_get_softc(sc->jme_miibus);
483 ASSERT_IFNET_SERIALIZED_ALL(ifp);
485 if (mii->mii_instance != 0) {
486 struct mii_softc *miisc;
488 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
489 mii_phy_reset(miisc);
491 error = mii_mediachg(mii);
497 jme_probe(device_t dev)
499 const struct jme_dev *sp;
502 vid = pci_get_vendor(dev);
503 did = pci_get_device(dev);
504 for (sp = jme_devs; sp->jme_name != NULL; ++sp) {
505 if (vid == sp->jme_vendorid && did == sp->jme_deviceid) {
506 struct jme_softc *sc = device_get_softc(dev);
508 sc->jme_caps = sp->jme_caps;
509 device_set_desc(dev, sp->jme_name);
517 jme_eeprom_read_byte(struct jme_softc *sc, uint8_t addr, uint8_t *val)
523 for (i = JME_TIMEOUT; i > 0; i--) {
524 reg = CSR_READ_4(sc, JME_SMBCSR);
525 if ((reg & SMBCSR_HW_BUSY_MASK) == SMBCSR_HW_IDLE)
531 device_printf(sc->jme_dev, "EEPROM idle timeout!\n");
535 reg = ((uint32_t)addr << SMBINTF_ADDR_SHIFT) & SMBINTF_ADDR_MASK;
536 CSR_WRITE_4(sc, JME_SMBINTF, reg | SMBINTF_RD | SMBINTF_CMD_TRIGGER);
537 for (i = JME_TIMEOUT; i > 0; i--) {
539 reg = CSR_READ_4(sc, JME_SMBINTF);
540 if ((reg & SMBINTF_CMD_TRIGGER) == 0)
545 device_printf(sc->jme_dev, "EEPROM read timeout!\n");
549 reg = CSR_READ_4(sc, JME_SMBINTF);
550 *val = (reg & SMBINTF_RD_DATA_MASK) >> SMBINTF_RD_DATA_SHIFT;
556 jme_eeprom_macaddr(struct jme_softc *sc, uint8_t eaddr[])
558 uint8_t fup, reg, val;
563 if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 ||
564 fup != JME_EEPROM_SIG0)
566 if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 ||
567 fup != JME_EEPROM_SIG1)
571 if (jme_eeprom_read_byte(sc, offset, &fup) != 0)
573 if (JME_EEPROM_MKDESC(JME_EEPROM_FUNC0, JME_EEPROM_PAGE_BAR1) ==
574 (fup & (JME_EEPROM_FUNC_MASK | JME_EEPROM_PAGE_MASK))) {
575 if (jme_eeprom_read_byte(sc, offset + 1, ®) != 0)
577 if (reg >= JME_PAR0 &&
578 reg < JME_PAR0 + ETHER_ADDR_LEN) {
579 if (jme_eeprom_read_byte(sc, offset + 2,
582 eaddr[reg - JME_PAR0] = val;
586 /* Check for the end of EEPROM descriptor. */
587 if ((fup & JME_EEPROM_DESC_END) == JME_EEPROM_DESC_END)
589 /* Try next eeprom descriptor. */
590 offset += JME_EEPROM_DESC_BYTES;
591 } while (match != ETHER_ADDR_LEN && offset < JME_EEPROM_END);
593 if (match == ETHER_ADDR_LEN)
600 jme_reg_macaddr(struct jme_softc *sc, uint8_t eaddr[])
604 /* Read station address. */
605 par0 = CSR_READ_4(sc, JME_PAR0);
606 par1 = CSR_READ_4(sc, JME_PAR1);
608 if ((par0 == 0 && par1 == 0) || (par0 & 0x1)) {
609 device_printf(sc->jme_dev,
610 "generating fake ethernet address.\n");
611 par0 = karc4random();
612 /* Set OUI to JMicron. */
616 eaddr[3] = (par0 >> 16) & 0xff;
617 eaddr[4] = (par0 >> 8) & 0xff;
618 eaddr[5] = par0 & 0xff;
620 eaddr[0] = (par0 >> 0) & 0xFF;
621 eaddr[1] = (par0 >> 8) & 0xFF;
622 eaddr[2] = (par0 >> 16) & 0xFF;
623 eaddr[3] = (par0 >> 24) & 0xFF;
624 eaddr[4] = (par1 >> 0) & 0xFF;
625 eaddr[5] = (par1 >> 8) & 0xFF;
630 jme_attach(device_t dev)
632 struct jme_softc *sc = device_get_softc(dev);
633 struct ifnet *ifp = &sc->arpcom.ac_if;
636 uint8_t pcie_ptr, rev;
638 uint8_t eaddr[ETHER_ADDR_LEN];
640 lwkt_serialize_init(&sc->jme_serialize);
641 lwkt_serialize_init(&sc->jme_cdata.jme_tx_serialize);
642 for (i = 0; i < JME_NRXRING_MAX; ++i) {
644 &sc->jme_cdata.jme_rx_data[i].jme_rx_serialize);
647 sc->jme_rx_desc_cnt = device_getenv_int(dev, "rx_desc_count",
649 sc->jme_rx_desc_cnt = roundup(sc->jme_rx_desc_cnt, JME_NDESC_ALIGN);
650 if (sc->jme_rx_desc_cnt > JME_NDESC_MAX)
651 sc->jme_rx_desc_cnt = JME_NDESC_MAX;
653 sc->jme_tx_desc_cnt = device_getenv_int(dev, "tx_desc_count",
655 sc->jme_tx_desc_cnt = roundup(sc->jme_tx_desc_cnt, JME_NDESC_ALIGN);
656 if (sc->jme_tx_desc_cnt > JME_NDESC_MAX)
657 sc->jme_tx_desc_cnt = JME_NDESC_MAX;
662 sc->jme_rx_ring_cnt = device_getenv_int(dev, "rx_ring_count",
664 sc->jme_rx_ring_cnt = if_ring_count2(sc->jme_rx_ring_cnt,
668 sc->jme_serialize_arr[i++] = &sc->jme_serialize;
669 sc->jme_serialize_arr[i++] = &sc->jme_cdata.jme_tx_serialize;
670 for (j = 0; j < sc->jme_rx_ring_cnt; ++j) {
671 sc->jme_serialize_arr[i++] =
672 &sc->jme_cdata.jme_rx_data[j].jme_rx_serialize;
674 KKASSERT(i <= JME_NSERIALIZE);
675 sc->jme_serialize_cnt = i;
677 sc->jme_cdata.jme_sc = sc;
678 for (i = 0; i < sc->jme_rx_ring_cnt; ++i) {
679 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[i];
682 rdata->jme_rx_coal = jme_rx_status[i].jme_coal;
683 rdata->jme_rx_comp = jme_rx_status[i].jme_comp;
684 rdata->jme_rx_empty = jme_rx_status[i].jme_empty;
685 rdata->jme_rx_idx = i;
689 sc->jme_lowaddr = BUS_SPACE_MAXADDR;
691 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
693 callout_init(&sc->jme_tick_ch);
696 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
699 irq = pci_read_config(dev, PCIR_INTLINE, 4);
700 mem = pci_read_config(dev, JME_PCIR_BAR, 4);
702 device_printf(dev, "chip is in D%d power mode "
703 "-- setting to D0\n", pci_get_powerstate(dev));
705 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
707 pci_write_config(dev, PCIR_INTLINE, irq, 4);
708 pci_write_config(dev, JME_PCIR_BAR, mem, 4);
710 #endif /* !BURN_BRIDGE */
712 /* Enable bus mastering */
713 pci_enable_busmaster(dev);
718 * JMC250 supports both memory mapped and I/O register space
719 * access. Because I/O register access should use different
720 * BARs to access registers it's waste of time to use I/O
721 * register spce access. JMC250 uses 16K to map entire memory
724 sc->jme_mem_rid = JME_PCIR_BAR;
725 sc->jme_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
726 &sc->jme_mem_rid, RF_ACTIVE);
727 if (sc->jme_mem_res == NULL) {
728 device_printf(dev, "can't allocate IO memory\n");
731 sc->jme_mem_bt = rman_get_bustag(sc->jme_mem_res);
732 sc->jme_mem_bh = rman_get_bushandle(sc->jme_mem_res);
737 error = jme_intr_alloc(dev);
744 reg = CSR_READ_4(sc, JME_CHIPMODE);
745 if (((reg & CHIPMODE_FPGA_REV_MASK) >> CHIPMODE_FPGA_REV_SHIFT) !=
747 sc->jme_caps |= JME_CAP_FPGA;
749 device_printf(dev, "FPGA revision: 0x%04x\n",
750 (reg & CHIPMODE_FPGA_REV_MASK) >>
751 CHIPMODE_FPGA_REV_SHIFT);
755 /* NOTE: FM revision is put in the upper 4 bits */
756 rev = ((reg & CHIPMODE_REVFM_MASK) >> CHIPMODE_REVFM_SHIFT) << 4;
757 rev |= (reg & CHIPMODE_REVECO_MASK) >> CHIPMODE_REVECO_SHIFT;
759 device_printf(dev, "Revision (FM/ECO): 0x%02x\n", rev);
761 did = pci_get_device(dev);
763 case PCI_PRODUCT_JMICRON_JMC250:
764 if (rev == JME_REV1_A2)
765 sc->jme_workaround |= JME_WA_EXTFIFO | JME_WA_HDX;
768 case PCI_PRODUCT_JMICRON_JMC260:
770 sc->jme_lowaddr = BUS_SPACE_MAXADDR_32BIT;
774 panic("unknown device id 0x%04x\n", did);
776 if (rev >= JME_REV2) {
777 sc->jme_clksrc = GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC;
778 sc->jme_clksrc_1000 = GHC_TXOFL_CLKSRC_1000 |
779 GHC_TXMAC_CLKSRC_1000;
782 /* Reset the ethernet controller. */
785 /* Map MSI/MSI-X vectors */
788 /* Get station address. */
789 reg = CSR_READ_4(sc, JME_SMBCSR);
790 if (reg & SMBCSR_EEPROM_PRESENT)
791 error = jme_eeprom_macaddr(sc, eaddr);
792 if (error != 0 || (reg & SMBCSR_EEPROM_PRESENT) == 0) {
793 if (error != 0 && (bootverbose)) {
794 device_printf(dev, "ethernet hardware address "
795 "not found in EEPROM.\n");
797 jme_reg_macaddr(sc, eaddr);
802 * Integrated JR0211 has fixed PHY address whereas FPGA version
803 * requires PHY probing to get correct PHY address.
805 if ((sc->jme_caps & JME_CAP_FPGA) == 0) {
806 sc->jme_phyaddr = CSR_READ_4(sc, JME_GPREG0) &
807 GPREG0_PHY_ADDR_MASK;
809 device_printf(dev, "PHY is at address %d.\n",
816 /* Set max allowable DMA size. */
817 pcie_ptr = pci_get_pciecap_ptr(dev);
821 sc->jme_caps |= JME_CAP_PCIE;
822 ctrl = pci_read_config(dev, pcie_ptr + PCIER_DEVCTRL, 2);
824 device_printf(dev, "Read request size : %d bytes.\n",
825 128 << ((ctrl >> 12) & 0x07));
826 device_printf(dev, "TLP payload size : %d bytes.\n",
827 128 << ((ctrl >> 5) & 0x07));
829 switch (ctrl & PCIEM_DEVCTL_MAX_READRQ_MASK) {
830 case PCIEM_DEVCTL_MAX_READRQ_128:
831 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_128;
833 case PCIEM_DEVCTL_MAX_READRQ_256:
834 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_256;
837 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_512;
840 sc->jme_rx_dma_size = RXCSR_DMA_SIZE_128;
842 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_512;
843 sc->jme_rx_dma_size = RXCSR_DMA_SIZE_128;
847 if (pci_find_extcap(dev, PCIY_PMG, &pmc) == 0)
848 sc->jme_caps |= JME_CAP_PMCAP;
856 /* Allocate DMA stuffs */
857 error = jme_dma_alloc(sc);
862 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
863 ifp->if_init = jme_init;
864 ifp->if_ioctl = jme_ioctl;
865 ifp->if_start = jme_start;
866 #ifdef DEVICE_POLLING
867 ifp->if_poll = jme_poll;
869 ifp->if_watchdog = jme_watchdog;
870 ifp->if_serialize = jme_serialize;
871 ifp->if_deserialize = jme_deserialize;
872 ifp->if_tryserialize = jme_tryserialize;
874 ifp->if_serialize_assert = jme_serialize_assert;
876 ifq_set_maxlen(&ifp->if_snd, sc->jme_tx_desc_cnt - JME_TXD_RSVD);
877 ifq_set_ready(&ifp->if_snd);
879 /* JMC250 supports Tx/Rx checksum offload and hardware vlan tagging. */
880 ifp->if_capabilities = IFCAP_HWCSUM |
882 IFCAP_VLAN_HWTAGGING;
883 if (sc->jme_rx_ring_cnt > JME_NRXRING_MIN)
884 ifp->if_capabilities |= IFCAP_RSS;
885 ifp->if_capenable = ifp->if_capabilities;
888 * Disable TXCSUM by default to improve bulk data
889 * transmit performance (+20Mbps improvement).
891 ifp->if_capenable &= ~IFCAP_TXCSUM;
893 if (ifp->if_capenable & IFCAP_TXCSUM)
894 ifp->if_hwassist = JME_CSUM_FEATURES;
896 /* Set up MII bus. */
897 error = mii_phy_probe(dev, &sc->jme_miibus,
898 jme_mediachange, jme_mediastatus);
900 device_printf(dev, "no PHY found!\n");
905 * Save PHYADDR for FPGA mode PHY.
907 if (sc->jme_caps & JME_CAP_FPGA) {
908 struct mii_data *mii = device_get_softc(sc->jme_miibus);
910 if (mii->mii_instance != 0) {
911 struct mii_softc *miisc;
913 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
914 if (miisc->mii_phy != 0) {
915 sc->jme_phyaddr = miisc->mii_phy;
919 if (sc->jme_phyaddr != 0) {
920 device_printf(sc->jme_dev,
921 "FPGA PHY is at %d\n", sc->jme_phyaddr);
923 jme_miibus_writereg(dev, sc->jme_phyaddr,
924 JMPHY_CONF, JMPHY_CONF_DEFFIFO);
926 /* XXX should we clear JME_WA_EXTFIFO */
931 ether_ifattach(ifp, eaddr, NULL);
933 /* Tell the upper layer(s) we support long frames. */
934 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
936 error = jme_intr_setup(dev);
949 jme_detach(device_t dev)
951 struct jme_softc *sc = device_get_softc(dev);
953 if (device_is_attached(dev)) {
954 struct ifnet *ifp = &sc->arpcom.ac_if;
956 ifnet_serialize_all(ifp);
958 jme_intr_teardown(dev);
959 ifnet_deserialize_all(ifp);
964 if (sc->jme_sysctl_tree != NULL)
965 sysctl_ctx_free(&sc->jme_sysctl_ctx);
967 if (sc->jme_miibus != NULL)
968 device_delete_child(dev, sc->jme_miibus);
969 bus_generic_detach(dev);
973 if (sc->jme_mem_res != NULL) {
974 bus_release_resource(dev, SYS_RES_MEMORY, sc->jme_mem_rid,
984 jme_sysctl_node(struct jme_softc *sc)
988 char rx_ring_pkt[32];
992 sysctl_ctx_init(&sc->jme_sysctl_ctx);
993 sc->jme_sysctl_tree = SYSCTL_ADD_NODE(&sc->jme_sysctl_ctx,
994 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
995 device_get_nameunit(sc->jme_dev),
997 if (sc->jme_sysctl_tree == NULL) {
998 device_printf(sc->jme_dev, "can't add sysctl node\n");
1002 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1003 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1004 "tx_coal_to", CTLTYPE_INT | CTLFLAG_RW,
1005 sc, 0, jme_sysctl_tx_coal_to, "I", "jme tx coalescing timeout");
1007 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1008 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1009 "tx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW,
1010 sc, 0, jme_sysctl_tx_coal_pkt, "I", "jme tx coalescing packet");
1012 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1013 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1014 "rx_coal_to", CTLTYPE_INT | CTLFLAG_RW,
1015 sc, 0, jme_sysctl_rx_coal_to, "I", "jme rx coalescing timeout");
1017 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1018 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1019 "rx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW,
1020 sc, 0, jme_sysctl_rx_coal_pkt, "I", "jme rx coalescing packet");
1022 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1023 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1024 "rx_desc_count", CTLFLAG_RD, &sc->jme_rx_desc_cnt,
1025 0, "RX desc count");
1026 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1027 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1028 "tx_desc_count", CTLFLAG_RD, &sc->jme_tx_desc_cnt,
1029 0, "TX desc count");
1030 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1031 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1032 "rx_ring_count", CTLFLAG_RD, &sc->jme_rx_ring_cnt,
1033 0, "RX ring count");
1034 #ifdef JME_RSS_DEBUG
1035 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1036 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1037 "rss_debug", CTLFLAG_RW, &sc->jme_rss_debug,
1038 0, "RSS debug level");
1039 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
1040 ksnprintf(rx_ring_pkt, sizeof(rx_ring_pkt), "rx_ring%d_pkt", r);
1041 SYSCTL_ADD_UINT(&sc->jme_sysctl_ctx,
1042 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1043 rx_ring_pkt, CTLFLAG_RW,
1044 &sc->jme_rx_ring_pkt[r],
1050 * Set default coalesce valves
1052 sc->jme_tx_coal_to = PCCTX_COAL_TO_DEFAULT;
1053 sc->jme_tx_coal_pkt = PCCTX_COAL_PKT_DEFAULT;
1054 sc->jme_rx_coal_to = PCCRX_COAL_TO_DEFAULT;
1055 sc->jme_rx_coal_pkt = PCCRX_COAL_PKT_DEFAULT;
1058 * Adjust coalesce valves, in case that the number of TX/RX
1059 * descs are set to small values by users.
1061 * NOTE: coal_max will not be zero, since number of descs
1062 * must aligned by JME_NDESC_ALIGN (16 currently)
1064 coal_max = sc->jme_tx_desc_cnt / 6;
1065 if (coal_max < sc->jme_tx_coal_pkt)
1066 sc->jme_tx_coal_pkt = coal_max;
1068 coal_max = sc->jme_rx_desc_cnt / 4;
1069 if (coal_max < sc->jme_rx_coal_pkt)
1070 sc->jme_rx_coal_pkt = coal_max;
1074 jme_dma_alloc(struct jme_softc *sc)
1076 struct jme_txdesc *txd;
1080 sc->jme_cdata.jme_txdesc =
1081 kmalloc(sc->jme_tx_desc_cnt * sizeof(struct jme_txdesc),
1082 M_DEVBUF, M_WAITOK | M_ZERO);
1083 for (i = 0; i < sc->jme_rx_ring_cnt; ++i) {
1084 sc->jme_cdata.jme_rx_data[i].jme_rxdesc =
1085 kmalloc(sc->jme_rx_desc_cnt * sizeof(struct jme_rxdesc),
1086 M_DEVBUF, M_WAITOK | M_ZERO);
1089 /* Create parent ring tag. */
1090 error = bus_dma_tag_create(NULL,/* parent */
1091 1, JME_RING_BOUNDARY, /* algnmnt, boundary */
1092 sc->jme_lowaddr, /* lowaddr */
1093 BUS_SPACE_MAXADDR, /* highaddr */
1094 NULL, NULL, /* filter, filterarg */
1095 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1097 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1099 &sc->jme_cdata.jme_ring_tag);
1101 device_printf(sc->jme_dev,
1102 "could not create parent ring DMA tag.\n");
1107 * Create DMA stuffs for TX ring
1109 error = bus_dmamem_coherent(sc->jme_cdata.jme_ring_tag,
1110 JME_TX_RING_ALIGN, 0,
1111 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1112 JME_TX_RING_SIZE(sc),
1113 BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
1115 device_printf(sc->jme_dev, "could not allocate Tx ring.\n");
1118 sc->jme_cdata.jme_tx_ring_tag = dmem.dmem_tag;
1119 sc->jme_cdata.jme_tx_ring_map = dmem.dmem_map;
1120 sc->jme_cdata.jme_tx_ring = dmem.dmem_addr;
1121 sc->jme_cdata.jme_tx_ring_paddr = dmem.dmem_busaddr;
1124 * Create DMA stuffs for RX rings
1126 for (i = 0; i < sc->jme_rx_ring_cnt; ++i) {
1127 error = jme_rxring_dma_alloc(sc, i);
1132 /* Create parent buffer tag. */
1133 error = bus_dma_tag_create(NULL,/* parent */
1134 1, 0, /* algnmnt, boundary */
1135 sc->jme_lowaddr, /* lowaddr */
1136 BUS_SPACE_MAXADDR, /* highaddr */
1137 NULL, NULL, /* filter, filterarg */
1138 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1140 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1142 &sc->jme_cdata.jme_buffer_tag);
1144 device_printf(sc->jme_dev,
1145 "could not create parent buffer DMA tag.\n");
1150 * Create DMA stuffs for shadow status block
1152 error = bus_dmamem_coherent(sc->jme_cdata.jme_buffer_tag,
1153 JME_SSB_ALIGN, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1154 JME_SSB_SIZE, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
1156 device_printf(sc->jme_dev,
1157 "could not create shadow status block.\n");
1160 sc->jme_cdata.jme_ssb_tag = dmem.dmem_tag;
1161 sc->jme_cdata.jme_ssb_map = dmem.dmem_map;
1162 sc->jme_cdata.jme_ssb_block = dmem.dmem_addr;
1163 sc->jme_cdata.jme_ssb_block_paddr = dmem.dmem_busaddr;
1166 * Create DMA stuffs for TX buffers
1169 /* Create tag for Tx buffers. */
1170 error = bus_dma_tag_create(sc->jme_cdata.jme_buffer_tag,/* parent */
1171 1, 0, /* algnmnt, boundary */
1172 BUS_SPACE_MAXADDR, /* lowaddr */
1173 BUS_SPACE_MAXADDR, /* highaddr */
1174 NULL, NULL, /* filter, filterarg */
1175 JME_JUMBO_FRAMELEN, /* maxsize */
1176 JME_MAXTXSEGS, /* nsegments */
1177 JME_MAXSEGSIZE, /* maxsegsize */
1178 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,/* flags */
1179 &sc->jme_cdata.jme_tx_tag);
1181 device_printf(sc->jme_dev, "could not create Tx DMA tag.\n");
1185 /* Create DMA maps for Tx buffers. */
1186 for (i = 0; i < sc->jme_tx_desc_cnt; i++) {
1187 txd = &sc->jme_cdata.jme_txdesc[i];
1188 error = bus_dmamap_create(sc->jme_cdata.jme_tx_tag,
1189 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
1194 device_printf(sc->jme_dev,
1195 "could not create %dth Tx dmamap.\n", i);
1197 for (j = 0; j < i; ++j) {
1198 txd = &sc->jme_cdata.jme_txdesc[j];
1199 bus_dmamap_destroy(sc->jme_cdata.jme_tx_tag,
1202 bus_dma_tag_destroy(sc->jme_cdata.jme_tx_tag);
1203 sc->jme_cdata.jme_tx_tag = NULL;
1209 * Create DMA stuffs for RX buffers
1211 for (i = 0; i < sc->jme_rx_ring_cnt; ++i) {
1212 error = jme_rxbuf_dma_alloc(sc, i);
1220 jme_dma_free(struct jme_softc *sc)
1222 struct jme_txdesc *txd;
1223 struct jme_rxdesc *rxd;
1224 struct jme_rxdata *rdata;
1228 if (sc->jme_cdata.jme_tx_ring_tag != NULL) {
1229 bus_dmamap_unload(sc->jme_cdata.jme_tx_ring_tag,
1230 sc->jme_cdata.jme_tx_ring_map);
1231 bus_dmamem_free(sc->jme_cdata.jme_tx_ring_tag,
1232 sc->jme_cdata.jme_tx_ring,
1233 sc->jme_cdata.jme_tx_ring_map);
1234 bus_dma_tag_destroy(sc->jme_cdata.jme_tx_ring_tag);
1235 sc->jme_cdata.jme_tx_ring_tag = NULL;
1239 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
1240 rdata = &sc->jme_cdata.jme_rx_data[r];
1241 if (rdata->jme_rx_ring_tag != NULL) {
1242 bus_dmamap_unload(rdata->jme_rx_ring_tag,
1243 rdata->jme_rx_ring_map);
1244 bus_dmamem_free(rdata->jme_rx_ring_tag,
1246 rdata->jme_rx_ring_map);
1247 bus_dma_tag_destroy(rdata->jme_rx_ring_tag);
1248 rdata->jme_rx_ring_tag = NULL;
1253 if (sc->jme_cdata.jme_tx_tag != NULL) {
1254 for (i = 0; i < sc->jme_tx_desc_cnt; i++) {
1255 txd = &sc->jme_cdata.jme_txdesc[i];
1256 bus_dmamap_destroy(sc->jme_cdata.jme_tx_tag,
1259 bus_dma_tag_destroy(sc->jme_cdata.jme_tx_tag);
1260 sc->jme_cdata.jme_tx_tag = NULL;
1264 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
1265 rdata = &sc->jme_cdata.jme_rx_data[r];
1266 if (rdata->jme_rx_tag != NULL) {
1267 for (i = 0; i < sc->jme_rx_desc_cnt; i++) {
1268 rxd = &rdata->jme_rxdesc[i];
1269 bus_dmamap_destroy(rdata->jme_rx_tag,
1272 bus_dmamap_destroy(rdata->jme_rx_tag,
1273 rdata->jme_rx_sparemap);
1274 bus_dma_tag_destroy(rdata->jme_rx_tag);
1275 rdata->jme_rx_tag = NULL;
1279 /* Shadow status block. */
1280 if (sc->jme_cdata.jme_ssb_tag != NULL) {
1281 bus_dmamap_unload(sc->jme_cdata.jme_ssb_tag,
1282 sc->jme_cdata.jme_ssb_map);
1283 bus_dmamem_free(sc->jme_cdata.jme_ssb_tag,
1284 sc->jme_cdata.jme_ssb_block,
1285 sc->jme_cdata.jme_ssb_map);
1286 bus_dma_tag_destroy(sc->jme_cdata.jme_ssb_tag);
1287 sc->jme_cdata.jme_ssb_tag = NULL;
1290 if (sc->jme_cdata.jme_buffer_tag != NULL) {
1291 bus_dma_tag_destroy(sc->jme_cdata.jme_buffer_tag);
1292 sc->jme_cdata.jme_buffer_tag = NULL;
1294 if (sc->jme_cdata.jme_ring_tag != NULL) {
1295 bus_dma_tag_destroy(sc->jme_cdata.jme_ring_tag);
1296 sc->jme_cdata.jme_ring_tag = NULL;
1299 if (sc->jme_cdata.jme_txdesc != NULL) {
1300 kfree(sc->jme_cdata.jme_txdesc, M_DEVBUF);
1301 sc->jme_cdata.jme_txdesc = NULL;
1303 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
1304 rdata = &sc->jme_cdata.jme_rx_data[r];
1305 if (rdata->jme_rxdesc != NULL) {
1306 kfree(rdata->jme_rxdesc, M_DEVBUF);
1307 rdata->jme_rxdesc = NULL;
1313 * Make sure the interface is stopped at reboot time.
1316 jme_shutdown(device_t dev)
1318 return jme_suspend(dev);
1323 * Unlike other ethernet controllers, JMC250 requires
1324 * explicit resetting link speed to 10/100Mbps as gigabit
1325 * link will cunsume more power than 375mA.
1326 * Note, we reset the link speed to 10/100Mbps with
1327 * auto-negotiation but we don't know whether that operation
1328 * would succeed or not as we have no control after powering
1329 * off. If the renegotiation fail WOL may not work. Running
1330 * at 1Gbps draws more power than 375mA at 3.3V which is
1331 * specified in PCI specification and that would result in
1332 * complete shutdowning power to ethernet controller.
1335 * Save current negotiated media speed/duplex/flow-control
1336 * to softc and restore the same link again after resuming.
1337 * PHY handling such as power down/resetting to 100Mbps
1338 * may be better handled in suspend method in phy driver.
1341 jme_setlinkspeed(struct jme_softc *sc)
1343 struct mii_data *mii;
1346 JME_LOCK_ASSERT(sc);
1348 mii = device_get_softc(sc->jme_miibus);
1351 if ((mii->mii_media_status & IFM_AVALID) != 0) {
1352 switch IFM_SUBTYPE(mii->mii_media_active) {
1362 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_100T2CR, 0);
1363 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_ANAR,
1364 ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
1365 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_BMCR,
1366 BMCR_AUTOEN | BMCR_STARTNEG);
1369 /* Poll link state until jme(4) get a 10/100 link. */
1370 for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
1372 if ((mii->mii_media_status & IFM_AVALID) != 0) {
1373 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1383 pause("jmelnk", hz);
1386 if (i == MII_ANEGTICKS_GIGE)
1387 device_printf(sc->jme_dev, "establishing link failed, "
1388 "WOL may not work!");
1391 * No link, force MAC to have 100Mbps, full-duplex link.
1392 * This is the last resort and may/may not work.
1394 mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
1395 mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1400 jme_setwol(struct jme_softc *sc)
1402 struct ifnet *ifp = &sc->arpcom.ac_if;
1407 if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1408 /* No PME capability, PHY power down. */
1409 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1410 MII_BMCR, BMCR_PDOWN);
1414 gpr = CSR_READ_4(sc, JME_GPREG0) & ~GPREG0_PME_ENB;
1415 pmcs = CSR_READ_4(sc, JME_PMCS);
1416 pmcs &= ~PMCS_WOL_ENB_MASK;
1417 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) {
1418 pmcs |= PMCS_MAGIC_FRAME | PMCS_MAGIC_FRAME_ENB;
1419 /* Enable PME message. */
1420 gpr |= GPREG0_PME_ENB;
1421 /* For gigabit controllers, reset link speed to 10/100. */
1422 if ((sc->jme_caps & JME_CAP_FASTETH) == 0)
1423 jme_setlinkspeed(sc);
1426 CSR_WRITE_4(sc, JME_PMCS, pmcs);
1427 CSR_WRITE_4(sc, JME_GPREG0, gpr);
1430 pmstat = pci_read_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, 2);
1431 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
1432 if ((ifp->if_capenable & IFCAP_WOL) != 0)
1433 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
1434 pci_write_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1435 if ((ifp->if_capenable & IFCAP_WOL) == 0) {
1436 /* No WOL, PHY power down. */
1437 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1438 MII_BMCR, BMCR_PDOWN);
1444 jme_suspend(device_t dev)
1446 struct jme_softc *sc = device_get_softc(dev);
1447 struct ifnet *ifp = &sc->arpcom.ac_if;
1449 ifnet_serialize_all(ifp);
1454 ifnet_deserialize_all(ifp);
1460 jme_resume(device_t dev)
1462 struct jme_softc *sc = device_get_softc(dev);
1463 struct ifnet *ifp = &sc->arpcom.ac_if;
1468 ifnet_serialize_all(ifp);
1471 if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1474 pmstat = pci_read_config(sc->jme_dev,
1475 pmc + PCIR_POWER_STATUS, 2);
1476 /* Disable PME clear PME status. */
1477 pmstat &= ~PCIM_PSTAT_PMEENABLE;
1478 pci_write_config(sc->jme_dev,
1479 pmc + PCIR_POWER_STATUS, pmstat, 2);
1483 if (ifp->if_flags & IFF_UP)
1486 ifnet_deserialize_all(ifp);
1492 jme_encap(struct jme_softc *sc, struct mbuf **m_head)
1494 struct jme_txdesc *txd;
1495 struct jme_desc *desc;
1497 bus_dma_segment_t txsegs[JME_MAXTXSEGS];
1499 int error, i, prod, symbol_desc;
1500 uint32_t cflags, flag64;
1502 M_ASSERTPKTHDR((*m_head));
1504 prod = sc->jme_cdata.jme_tx_prod;
1505 txd = &sc->jme_cdata.jme_txdesc[prod];
1507 if (sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT)
1512 maxsegs = (sc->jme_tx_desc_cnt - sc->jme_cdata.jme_tx_cnt) -
1513 (JME_TXD_RSVD + symbol_desc);
1514 if (maxsegs > JME_MAXTXSEGS)
1515 maxsegs = JME_MAXTXSEGS;
1516 KASSERT(maxsegs >= (sc->jme_txd_spare - symbol_desc),
1517 ("not enough segments %d\n", maxsegs));
1519 error = bus_dmamap_load_mbuf_defrag(sc->jme_cdata.jme_tx_tag,
1520 txd->tx_dmamap, m_head,
1521 txsegs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
1525 bus_dmamap_sync(sc->jme_cdata.jme_tx_tag, txd->tx_dmamap,
1526 BUS_DMASYNC_PREWRITE);
1531 /* Configure checksum offload. */
1532 if (m->m_pkthdr.csum_flags & CSUM_IP)
1533 cflags |= JME_TD_IPCSUM;
1534 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1535 cflags |= JME_TD_TCPCSUM;
1536 if (m->m_pkthdr.csum_flags & CSUM_UDP)
1537 cflags |= JME_TD_UDPCSUM;
1539 /* Configure VLAN. */
1540 if (m->m_flags & M_VLANTAG) {
1541 cflags |= (m->m_pkthdr.ether_vlantag & JME_TD_VLAN_MASK);
1542 cflags |= JME_TD_VLAN_TAG;
1545 desc = &sc->jme_cdata.jme_tx_ring[prod];
1546 desc->flags = htole32(cflags);
1547 desc->addr_hi = htole32(m->m_pkthdr.len);
1548 if (sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT) {
1550 * Use 64bits TX desc chain format.
1552 * The first TX desc of the chain, which is setup here,
1553 * is just a symbol TX desc carrying no payload.
1555 flag64 = JME_TD_64BIT;
1559 /* No effective TX desc is consumed */
1563 * Use 32bits TX desc chain format.
1565 * The first TX desc of the chain, which is setup here,
1566 * is an effective TX desc carrying the first segment of
1570 desc->buflen = htole32(txsegs[0].ds_len);
1571 desc->addr_lo = htole32(JME_ADDR_LO(txsegs[0].ds_addr));
1573 /* One effective TX desc is consumed */
1576 sc->jme_cdata.jme_tx_cnt++;
1577 KKASSERT(sc->jme_cdata.jme_tx_cnt - i <
1578 sc->jme_tx_desc_cnt - JME_TXD_RSVD);
1579 JME_DESC_INC(prod, sc->jme_tx_desc_cnt);
1581 txd->tx_ndesc = 1 - i;
1582 for (; i < nsegs; i++) {
1583 desc = &sc->jme_cdata.jme_tx_ring[prod];
1584 desc->flags = htole32(JME_TD_OWN | flag64);
1585 desc->buflen = htole32(txsegs[i].ds_len);
1586 desc->addr_hi = htole32(JME_ADDR_HI(txsegs[i].ds_addr));
1587 desc->addr_lo = htole32(JME_ADDR_LO(txsegs[i].ds_addr));
1589 sc->jme_cdata.jme_tx_cnt++;
1590 KKASSERT(sc->jme_cdata.jme_tx_cnt <=
1591 sc->jme_tx_desc_cnt - JME_TXD_RSVD);
1592 JME_DESC_INC(prod, sc->jme_tx_desc_cnt);
1595 /* Update producer index. */
1596 sc->jme_cdata.jme_tx_prod = prod;
1598 * Finally request interrupt and give the first descriptor
1599 * owenership to hardware.
1601 desc = txd->tx_desc;
1602 desc->flags |= htole32(JME_TD_OWN | JME_TD_INTR);
1605 txd->tx_ndesc += nsegs;
1615 jme_start(struct ifnet *ifp)
1617 struct jme_softc *sc = ifp->if_softc;
1618 struct mbuf *m_head;
1621 ASSERT_SERIALIZED(&sc->jme_cdata.jme_tx_serialize);
1623 if ((sc->jme_flags & JME_FLAG_LINK) == 0) {
1624 ifq_purge(&ifp->if_snd);
1628 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1631 if (sc->jme_cdata.jme_tx_cnt >= JME_TX_DESC_HIWAT(sc))
1634 while (!ifq_is_empty(&ifp->if_snd)) {
1636 * Check number of available TX descs, always
1637 * leave JME_TXD_RSVD free TX descs.
1639 if (sc->jme_cdata.jme_tx_cnt + sc->jme_txd_spare >
1640 sc->jme_tx_desc_cnt - JME_TXD_RSVD) {
1641 ifp->if_flags |= IFF_OACTIVE;
1645 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1650 * Pack the data into the transmit ring. If we
1651 * don't have room, set the OACTIVE flag and wait
1652 * for the NIC to drain the ring.
1654 if (jme_encap(sc, &m_head)) {
1655 KKASSERT(m_head == NULL);
1657 ifp->if_flags |= IFF_OACTIVE;
1663 * If there's a BPF listener, bounce a copy of this frame
1666 ETHER_BPF_MTAP(ifp, m_head);
1671 * Reading TXCSR takes very long time under heavy load
1672 * so cache TXCSR value and writes the ORed value with
1673 * the kick command to the TXCSR. This saves one register
1676 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr | TXCSR_TX_ENB |
1677 TXCSR_TXQ_N_START(TXCSR_TXQ0));
1678 /* Set a timeout in case the chip goes out to lunch. */
1679 ifp->if_timer = JME_TX_TIMEOUT;
1684 jme_watchdog(struct ifnet *ifp)
1686 struct jme_softc *sc = ifp->if_softc;
1688 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1690 if ((sc->jme_flags & JME_FLAG_LINK) == 0) {
1691 if_printf(ifp, "watchdog timeout (missed link)\n");
1698 if (sc->jme_cdata.jme_tx_cnt == 0) {
1699 if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
1701 if (!ifq_is_empty(&ifp->if_snd))
1706 if_printf(ifp, "watchdog timeout\n");
1709 if (!ifq_is_empty(&ifp->if_snd))
1714 jme_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1716 struct jme_softc *sc = ifp->if_softc;
1717 struct mii_data *mii = device_get_softc(sc->jme_miibus);
1718 struct ifreq *ifr = (struct ifreq *)data;
1719 int error = 0, mask;
1721 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1725 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > JME_JUMBO_MTU ||
1726 (!(sc->jme_caps & JME_CAP_JUMBO) &&
1727 ifr->ifr_mtu > JME_MAX_MTU)) {
1732 if (ifp->if_mtu != ifr->ifr_mtu) {
1734 * No special configuration is required when interface
1735 * MTU is changed but availability of Tx checksum
1736 * offload should be chcked against new MTU size as
1737 * FIFO size is just 2K.
1739 if (ifr->ifr_mtu >= JME_TX_FIFO_SIZE) {
1740 ifp->if_capenable &= ~IFCAP_TXCSUM;
1741 ifp->if_hwassist &= ~JME_CSUM_FEATURES;
1743 ifp->if_mtu = ifr->ifr_mtu;
1744 if (ifp->if_flags & IFF_RUNNING)
1750 if (ifp->if_flags & IFF_UP) {
1751 if (ifp->if_flags & IFF_RUNNING) {
1752 if ((ifp->if_flags ^ sc->jme_if_flags) &
1753 (IFF_PROMISC | IFF_ALLMULTI))
1759 if (ifp->if_flags & IFF_RUNNING)
1762 sc->jme_if_flags = ifp->if_flags;
1767 if (ifp->if_flags & IFF_RUNNING)
1773 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1777 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1779 if ((mask & IFCAP_TXCSUM) && ifp->if_mtu < JME_TX_FIFO_SIZE) {
1780 ifp->if_capenable ^= IFCAP_TXCSUM;
1781 if (IFCAP_TXCSUM & ifp->if_capenable)
1782 ifp->if_hwassist |= JME_CSUM_FEATURES;
1784 ifp->if_hwassist &= ~JME_CSUM_FEATURES;
1786 if (mask & IFCAP_RXCSUM) {
1789 ifp->if_capenable ^= IFCAP_RXCSUM;
1790 reg = CSR_READ_4(sc, JME_RXMAC);
1791 reg &= ~RXMAC_CSUM_ENB;
1792 if (ifp->if_capenable & IFCAP_RXCSUM)
1793 reg |= RXMAC_CSUM_ENB;
1794 CSR_WRITE_4(sc, JME_RXMAC, reg);
1797 if (mask & IFCAP_VLAN_HWTAGGING) {
1798 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1802 if (mask & IFCAP_RSS)
1803 ifp->if_capenable ^= IFCAP_RSS;
1807 error = ether_ioctl(ifp, cmd, data);
1814 jme_mac_config(struct jme_softc *sc)
1816 struct mii_data *mii;
1817 uint32_t ghc, rxmac, txmac, txpause, gp1;
1818 int phyconf = JMPHY_CONF_DEFFIFO, hdx = 0;
1820 mii = device_get_softc(sc->jme_miibus);
1822 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
1824 CSR_WRITE_4(sc, JME_GHC, 0);
1826 rxmac = CSR_READ_4(sc, JME_RXMAC);
1827 rxmac &= ~RXMAC_FC_ENB;
1828 txmac = CSR_READ_4(sc, JME_TXMAC);
1829 txmac &= ~(TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST);
1830 txpause = CSR_READ_4(sc, JME_TXPFC);
1831 txpause &= ~TXPFC_PAUSE_ENB;
1832 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1833 ghc |= GHC_FULL_DUPLEX;
1834 rxmac &= ~RXMAC_COLL_DET_ENB;
1835 txmac &= ~(TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE |
1836 TXMAC_BACKOFF | TXMAC_CARRIER_EXT |
1839 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1840 txpause |= TXPFC_PAUSE_ENB;
1841 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1842 rxmac |= RXMAC_FC_ENB;
1844 /* Disable retry transmit timer/retry limit. */
1845 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) &
1846 ~(TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB));
1848 rxmac |= RXMAC_COLL_DET_ENB;
1849 txmac |= TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE | TXMAC_BACKOFF;
1850 /* Enable retry transmit timer/retry limit. */
1851 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) |
1852 TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB);
1856 * Reprogram Tx/Rx MACs with resolved speed/duplex.
1858 gp1 = CSR_READ_4(sc, JME_GPREG1);
1859 gp1 &= ~GPREG1_WA_HDX;
1861 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) == 0)
1864 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1866 ghc |= GHC_SPEED_10 | sc->jme_clksrc;
1868 gp1 |= GPREG1_WA_HDX;
1872 ghc |= GHC_SPEED_100 | sc->jme_clksrc;
1874 gp1 |= GPREG1_WA_HDX;
1877 * Use extended FIFO depth to workaround CRC errors
1878 * emitted by chips before JMC250B
1880 phyconf = JMPHY_CONF_EXTFIFO;
1884 if (sc->jme_caps & JME_CAP_FASTETH)
1887 ghc |= GHC_SPEED_1000 | sc->jme_clksrc_1000;
1889 txmac |= TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST;
1895 CSR_WRITE_4(sc, JME_GHC, ghc);
1896 CSR_WRITE_4(sc, JME_RXMAC, rxmac);
1897 CSR_WRITE_4(sc, JME_TXMAC, txmac);
1898 CSR_WRITE_4(sc, JME_TXPFC, txpause);
1900 if (sc->jme_workaround & JME_WA_EXTFIFO) {
1901 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1902 JMPHY_CONF, phyconf);
1904 if (sc->jme_workaround & JME_WA_HDX)
1905 CSR_WRITE_4(sc, JME_GPREG1, gp1);
1911 struct jme_softc *sc = xsc;
1912 struct ifnet *ifp = &sc->arpcom.ac_if;
1916 ASSERT_SERIALIZED(&sc->jme_serialize);
1918 status = CSR_READ_4(sc, JME_INTR_REQ_STATUS);
1919 if (status == 0 || status == 0xFFFFFFFF)
1922 /* Disable interrupts. */
1923 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
1925 status = CSR_READ_4(sc, JME_INTR_STATUS);
1926 if ((status & JME_INTRS) == 0 || status == 0xFFFFFFFF)
1929 /* Reset PCC counter/timer and Ack interrupts. */
1930 status &= ~(INTR_TXQ_COMP | INTR_RXQ_COMP);
1932 if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO))
1933 status |= INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP;
1935 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
1936 if (status & jme_rx_status[r].jme_coal) {
1937 status |= jme_rx_status[r].jme_coal |
1938 jme_rx_status[r].jme_comp;
1942 CSR_WRITE_4(sc, JME_INTR_STATUS, status);
1944 if (ifp->if_flags & IFF_RUNNING) {
1945 if (status & (INTR_RXQ_COAL | INTR_RXQ_COAL_TO))
1946 jme_rx_intr(sc, status);
1948 if (status & INTR_RXQ_DESC_EMPTY) {
1950 * Notify hardware availability of new Rx buffers.
1951 * Reading RXCSR takes very long time under heavy
1952 * load so cache RXCSR value and writes the ORed
1953 * value with the kick command to the RXCSR. This
1954 * saves one register access cycle.
1956 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr |
1957 RXCSR_RX_ENB | RXCSR_RXQ_START);
1960 if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO)) {
1961 lwkt_serialize_enter(&sc->jme_cdata.jme_tx_serialize);
1963 if (!ifq_is_empty(&ifp->if_snd))
1965 lwkt_serialize_exit(&sc->jme_cdata.jme_tx_serialize);
1969 /* Reenable interrupts. */
1970 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
1974 jme_txeof(struct jme_softc *sc)
1976 struct ifnet *ifp = &sc->arpcom.ac_if;
1977 struct jme_txdesc *txd;
1981 cons = sc->jme_cdata.jme_tx_cons;
1982 if (cons == sc->jme_cdata.jme_tx_prod)
1986 * Go through our Tx list and free mbufs for those
1987 * frames which have been transmitted.
1989 while (cons != sc->jme_cdata.jme_tx_prod) {
1990 txd = &sc->jme_cdata.jme_txdesc[cons];
1991 KASSERT(txd->tx_m != NULL,
1992 ("%s: freeing NULL mbuf!\n", __func__));
1994 status = le32toh(txd->tx_desc->flags);
1995 if ((status & JME_TD_OWN) == JME_TD_OWN)
1998 if (status & (JME_TD_TMOUT | JME_TD_RETRY_EXP)) {
2002 if (status & JME_TD_COLLISION) {
2003 ifp->if_collisions +=
2004 le32toh(txd->tx_desc->buflen) &
2005 JME_TD_BUF_LEN_MASK;
2010 * Only the first descriptor of multi-descriptor
2011 * transmission is updated so driver have to skip entire
2012 * chained buffers for the transmiited frame. In other
2013 * words, JME_TD_OWN bit is valid only at the first
2014 * descriptor of a multi-descriptor transmission.
2016 for (nsegs = 0; nsegs < txd->tx_ndesc; nsegs++) {
2017 sc->jme_cdata.jme_tx_ring[cons].flags = 0;
2018 JME_DESC_INC(cons, sc->jme_tx_desc_cnt);
2021 /* Reclaim transferred mbufs. */
2022 bus_dmamap_unload(sc->jme_cdata.jme_tx_tag, txd->tx_dmamap);
2025 sc->jme_cdata.jme_tx_cnt -= txd->tx_ndesc;
2026 KASSERT(sc->jme_cdata.jme_tx_cnt >= 0,
2027 ("%s: Active Tx desc counter was garbled\n", __func__));
2030 sc->jme_cdata.jme_tx_cons = cons;
2032 if (sc->jme_cdata.jme_tx_cnt == 0)
2035 if (sc->jme_cdata.jme_tx_cnt + sc->jme_txd_spare <=
2036 sc->jme_tx_desc_cnt - JME_TXD_RSVD)
2037 ifp->if_flags &= ~IFF_OACTIVE;
2040 static __inline void
2041 jme_discard_rxbufs(struct jme_softc *sc, int ring, int cons, int count)
2043 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
2046 for (i = 0; i < count; ++i) {
2047 struct jme_desc *desc = &rdata->jme_rx_ring[cons];
2049 desc->flags = htole32(JME_RD_OWN | JME_RD_INTR | JME_RD_64BIT);
2050 desc->buflen = htole32(MCLBYTES);
2051 JME_DESC_INC(cons, sc->jme_rx_desc_cnt);
2055 static __inline struct pktinfo *
2056 jme_pktinfo(struct pktinfo *pi, uint32_t flags)
2058 if (flags & JME_RD_IPV4)
2059 pi->pi_netisr = NETISR_IP;
2060 else if (flags & JME_RD_IPV6)
2061 pi->pi_netisr = NETISR_IPV6;
2066 pi->pi_l3proto = IPPROTO_UNKNOWN;
2068 if (flags & JME_RD_MORE_FRAG)
2069 pi->pi_flags |= PKTINFO_FLAG_FRAG;
2070 else if (flags & JME_RD_TCP)
2071 pi->pi_l3proto = IPPROTO_TCP;
2072 else if (flags & JME_RD_UDP)
2073 pi->pi_l3proto = IPPROTO_UDP;
2079 /* Receive a frame. */
2081 jme_rxpkt(struct jme_softc *sc, int ring)
2083 struct ifnet *ifp = &sc->arpcom.ac_if;
2084 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
2085 struct jme_desc *desc;
2086 struct jme_rxdesc *rxd;
2087 struct mbuf *mp, *m;
2088 uint32_t flags, status, hash, hashinfo;
2089 int cons, count, nsegs;
2091 cons = rdata->jme_rx_cons;
2092 desc = &rdata->jme_rx_ring[cons];
2093 flags = le32toh(desc->flags);
2094 status = le32toh(desc->buflen);
2095 hash = le32toh(desc->addr_hi);
2096 hashinfo = le32toh(desc->addr_lo);
2097 nsegs = JME_RX_NSEGS(status);
2099 JME_RSS_DPRINTF(sc, 15, "ring%d, flags 0x%08x, "
2100 "hash 0x%08x, hash info 0x%08x\n",
2101 ring, flags, hash, hashinfo);
2103 if (status & JME_RX_ERR_STAT) {
2105 jme_discard_rxbufs(sc, ring, cons, nsegs);
2106 #ifdef JME_SHOW_ERRORS
2107 device_printf(sc->jme_dev, "%s : receive error = 0x%b\n",
2108 __func__, JME_RX_ERR(status), JME_RX_ERR_BITS);
2110 rdata->jme_rx_cons += nsegs;
2111 rdata->jme_rx_cons %= sc->jme_rx_desc_cnt;
2115 rdata->jme_rxlen = JME_RX_BYTES(status) - JME_RX_PAD_BYTES;
2116 for (count = 0; count < nsegs; count++,
2117 JME_DESC_INC(cons, sc->jme_rx_desc_cnt)) {
2118 rxd = &rdata->jme_rxdesc[cons];
2121 /* Add a new receive buffer to the ring. */
2122 if (jme_newbuf(sc, ring, rxd, 0) != 0) {
2125 jme_discard_rxbufs(sc, ring, cons, nsegs - count);
2126 if (rdata->jme_rxhead != NULL) {
2127 m_freem(rdata->jme_rxhead);
2128 JME_RXCHAIN_RESET(sc, ring);
2134 * Assume we've received a full sized frame.
2135 * Actual size is fixed when we encounter the end of
2136 * multi-segmented frame.
2138 mp->m_len = MCLBYTES;
2140 /* Chain received mbufs. */
2141 if (rdata->jme_rxhead == NULL) {
2142 rdata->jme_rxhead = mp;
2143 rdata->jme_rxtail = mp;
2146 * Receive processor can receive a maximum frame
2147 * size of 65535 bytes.
2149 rdata->jme_rxtail->m_next = mp;
2150 rdata->jme_rxtail = mp;
2153 if (count == nsegs - 1) {
2154 struct pktinfo pi0, *pi;
2156 /* Last desc. for this frame. */
2157 m = rdata->jme_rxhead;
2158 m->m_pkthdr.len = rdata->jme_rxlen;
2160 /* Set first mbuf size. */
2161 m->m_len = MCLBYTES - JME_RX_PAD_BYTES;
2162 /* Set last mbuf size. */
2163 mp->m_len = rdata->jme_rxlen -
2164 ((MCLBYTES - JME_RX_PAD_BYTES) +
2165 (MCLBYTES * (nsegs - 2)));
2167 m->m_len = rdata->jme_rxlen;
2169 m->m_pkthdr.rcvif = ifp;
2172 * Account for 10bytes auto padding which is used
2173 * to align IP header on 32bit boundary. Also note,
2174 * CRC bytes is automatically removed by the
2177 m->m_data += JME_RX_PAD_BYTES;
2179 /* Set checksum information. */
2180 if ((ifp->if_capenable & IFCAP_RXCSUM) &&
2181 (flags & JME_RD_IPV4)) {
2182 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2183 if (flags & JME_RD_IPCSUM)
2184 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2185 if ((flags & JME_RD_MORE_FRAG) == 0 &&
2186 ((flags & (JME_RD_TCP | JME_RD_TCPCSUM)) ==
2187 (JME_RD_TCP | JME_RD_TCPCSUM) ||
2188 (flags & (JME_RD_UDP | JME_RD_UDPCSUM)) ==
2189 (JME_RD_UDP | JME_RD_UDPCSUM))) {
2190 m->m_pkthdr.csum_flags |=
2191 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2192 m->m_pkthdr.csum_data = 0xffff;
2196 /* Check for VLAN tagged packets. */
2197 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) &&
2198 (flags & JME_RD_VLAN_TAG)) {
2199 m->m_pkthdr.ether_vlantag =
2200 flags & JME_RD_VLAN_MASK;
2201 m->m_flags |= M_VLANTAG;
2206 if (ifp->if_capenable & IFCAP_RSS)
2207 pi = jme_pktinfo(&pi0, flags);
2212 (hashinfo & JME_RD_HASH_FN_MASK) != 0) {
2213 m->m_flags |= M_HASH;
2214 m->m_pkthdr.hash = toeplitz_hash(hash);
2217 #ifdef JME_RSS_DEBUG
2219 JME_RSS_DPRINTF(sc, 10,
2220 "isr %d flags %08x, l3 %d %s\n",
2221 pi->pi_netisr, pi->pi_flags,
2223 (m->m_flags & M_HASH) ? "hash" : "");
2228 ether_input_pkt(ifp, m, pi);
2230 /* Reset mbuf chains. */
2231 JME_RXCHAIN_RESET(sc, ring);
2232 #ifdef JME_RSS_DEBUG
2233 sc->jme_rx_ring_pkt[ring]++;
2238 rdata->jme_rx_cons += nsegs;
2239 rdata->jme_rx_cons %= sc->jme_rx_desc_cnt;
2243 jme_rxeof(struct jme_softc *sc, int ring, int count)
2245 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
2246 struct jme_desc *desc;
2250 #ifdef DEVICE_POLLING
2251 if (count >= 0 && count-- == 0)
2254 desc = &rdata->jme_rx_ring[rdata->jme_rx_cons];
2255 if ((le32toh(desc->flags) & JME_RD_OWN) == JME_RD_OWN)
2257 if ((le32toh(desc->buflen) & JME_RD_VALID) == 0)
2261 * Check number of segments against received bytes.
2262 * Non-matching value would indicate that hardware
2263 * is still trying to update Rx descriptors. I'm not
2264 * sure whether this check is needed.
2266 nsegs = JME_RX_NSEGS(le32toh(desc->buflen));
2267 pktlen = JME_RX_BYTES(le32toh(desc->buflen));
2268 if (nsegs != howmany(pktlen, MCLBYTES)) {
2269 if_printf(&sc->arpcom.ac_if, "RX fragment count(%d) "
2270 "and packet size(%d) mismach\n",
2275 /* Received a frame. */
2276 jme_rxpkt(sc, ring);
2283 struct jme_softc *sc = xsc;
2284 struct ifnet *ifp = &sc->arpcom.ac_if;
2285 struct mii_data *mii = device_get_softc(sc->jme_miibus);
2287 ifnet_serialize_all(ifp);
2290 callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
2292 ifnet_deserialize_all(ifp);
2296 jme_reset(struct jme_softc *sc)
2300 /* Make sure that TX and RX are stopped */
2305 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2309 * Hold reset bit before stop reset
2312 /* Disable TXMAC and TXOFL clock sources */
2313 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2314 /* Disable RXMAC clock source */
2315 val = CSR_READ_4(sc, JME_GPREG1);
2316 CSR_WRITE_4(sc, JME_GPREG1, val | GPREG1_DIS_RXMAC_CLKSRC);
2318 CSR_READ_4(sc, JME_GHC);
2321 CSR_WRITE_4(sc, JME_GHC, 0);
2323 CSR_READ_4(sc, JME_GHC);
2326 * Clear reset bit after stop reset
2329 /* Enable TXMAC and TXOFL clock sources */
2330 CSR_WRITE_4(sc, JME_GHC, GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC);
2331 /* Enable RXMAC clock source */
2332 val = CSR_READ_4(sc, JME_GPREG1);
2333 CSR_WRITE_4(sc, JME_GPREG1, val & ~GPREG1_DIS_RXMAC_CLKSRC);
2335 CSR_READ_4(sc, JME_GHC);
2337 /* Disable TXMAC and TXOFL clock sources */
2338 CSR_WRITE_4(sc, JME_GHC, 0);
2339 /* Disable RXMAC clock source */
2340 val = CSR_READ_4(sc, JME_GPREG1);
2341 CSR_WRITE_4(sc, JME_GPREG1, val | GPREG1_DIS_RXMAC_CLKSRC);
2343 CSR_READ_4(sc, JME_GHC);
2345 /* Enable TX and RX */
2346 val = CSR_READ_4(sc, JME_TXCSR);
2347 CSR_WRITE_4(sc, JME_TXCSR, val | TXCSR_TX_ENB);
2348 val = CSR_READ_4(sc, JME_RXCSR);
2349 CSR_WRITE_4(sc, JME_RXCSR, val | RXCSR_RX_ENB);
2351 CSR_READ_4(sc, JME_TXCSR);
2352 CSR_READ_4(sc, JME_RXCSR);
2354 /* Enable TXMAC and TXOFL clock sources */
2355 CSR_WRITE_4(sc, JME_GHC, GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC);
2356 /* Eisable RXMAC clock source */
2357 val = CSR_READ_4(sc, JME_GPREG1);
2358 CSR_WRITE_4(sc, JME_GPREG1, val & ~GPREG1_DIS_RXMAC_CLKSRC);
2360 CSR_READ_4(sc, JME_GHC);
2362 /* Stop TX and RX */
2370 struct jme_softc *sc = xsc;
2371 struct ifnet *ifp = &sc->arpcom.ac_if;
2372 struct mii_data *mii;
2373 uint8_t eaddr[ETHER_ADDR_LEN];
2378 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2381 * Cancel any pending I/O.
2386 * Reset the chip to a known state.
2391 * Setup MSI/MSI-X vectors to interrupts mapping
2396 howmany(ifp->if_mtu + sizeof(struct ether_vlan_header), MCLBYTES);
2397 KKASSERT(sc->jme_txd_spare >= 1);
2400 * If we use 64bit address mode for transmitting, each Tx request
2401 * needs one more symbol descriptor.
2403 if (sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT)
2404 sc->jme_txd_spare += 1;
2406 if (sc->jme_rx_ring_cnt > JME_NRXRING_MIN)
2409 jme_disable_rss(sc);
2411 /* Init RX descriptors */
2412 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
2413 error = jme_init_rx_ring(sc, r);
2415 if_printf(ifp, "initialization failed: "
2416 "no memory for %dth RX ring.\n", r);
2422 /* Init TX descriptors */
2423 jme_init_tx_ring(sc);
2425 /* Initialize shadow status block. */
2428 /* Reprogram the station address. */
2429 bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
2430 CSR_WRITE_4(sc, JME_PAR0,
2431 eaddr[3] << 24 | eaddr[2] << 16 | eaddr[1] << 8 | eaddr[0]);
2432 CSR_WRITE_4(sc, JME_PAR1, eaddr[5] << 8 | eaddr[4]);
2435 * Configure Tx queue.
2436 * Tx priority queue weight value : 0
2437 * Tx FIFO threshold for processing next packet : 16QW
2438 * Maximum Tx DMA length : 512
2439 * Allow Tx DMA burst.
2441 sc->jme_txcsr = TXCSR_TXQ_N_SEL(TXCSR_TXQ0);
2442 sc->jme_txcsr |= TXCSR_TXQ_WEIGHT(TXCSR_TXQ_WEIGHT_MIN);
2443 sc->jme_txcsr |= TXCSR_FIFO_THRESH_16QW;
2444 sc->jme_txcsr |= sc->jme_tx_dma_size;
2445 sc->jme_txcsr |= TXCSR_DMA_BURST;
2446 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr);
2448 /* Set Tx descriptor counter. */
2449 CSR_WRITE_4(sc, JME_TXQDC, sc->jme_tx_desc_cnt);
2451 /* Set Tx ring address to the hardware. */
2452 paddr = sc->jme_cdata.jme_tx_ring_paddr;
2453 CSR_WRITE_4(sc, JME_TXDBA_HI, JME_ADDR_HI(paddr));
2454 CSR_WRITE_4(sc, JME_TXDBA_LO, JME_ADDR_LO(paddr));
2456 /* Configure TxMAC parameters. */
2457 reg = TXMAC_IFG1_DEFAULT | TXMAC_IFG2_DEFAULT | TXMAC_IFG_ENB;
2458 reg |= TXMAC_THRESH_1_PKT;
2459 reg |= TXMAC_CRC_ENB | TXMAC_PAD_ENB;
2460 CSR_WRITE_4(sc, JME_TXMAC, reg);
2463 * Configure Rx queue.
2464 * FIFO full threshold for transmitting Tx pause packet : 128T
2465 * FIFO threshold for processing next packet : 128QW
2467 * Max Rx DMA length : 128
2468 * Rx descriptor retry : 32
2469 * Rx descriptor retry time gap : 256ns
2470 * Don't receive runt/bad frame.
2472 sc->jme_rxcsr = RXCSR_FIFO_FTHRESH_128T;
2475 * Since Rx FIFO size is 4K bytes, receiving frames larger
2476 * than 4K bytes will suffer from Rx FIFO overruns. So
2477 * decrease FIFO threshold to reduce the FIFO overruns for
2478 * frames larger than 4000 bytes.
2479 * For best performance of standard MTU sized frames use
2480 * maximum allowable FIFO threshold, 128QW.
2482 if ((ifp->if_mtu + ETHER_HDR_LEN + EVL_ENCAPLEN + ETHER_CRC_LEN) >
2484 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
2486 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_128QW;
2488 /* Improve PCI Express compatibility */
2489 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
2491 sc->jme_rxcsr |= sc->jme_rx_dma_size;
2492 sc->jme_rxcsr |= RXCSR_DESC_RT_CNT(RXCSR_DESC_RT_CNT_DEFAULT);
2493 sc->jme_rxcsr |= RXCSR_DESC_RT_GAP_256 & RXCSR_DESC_RT_GAP_MASK;
2494 /* XXX TODO DROP_BAD */
2496 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
2497 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RXQ_N_SEL(r));
2499 /* Set Rx descriptor counter. */
2500 CSR_WRITE_4(sc, JME_RXQDC, sc->jme_rx_desc_cnt);
2502 /* Set Rx ring address to the hardware. */
2503 paddr = sc->jme_cdata.jme_rx_data[r].jme_rx_ring_paddr;
2504 CSR_WRITE_4(sc, JME_RXDBA_HI, JME_ADDR_HI(paddr));
2505 CSR_WRITE_4(sc, JME_RXDBA_LO, JME_ADDR_LO(paddr));
2508 /* Clear receive filter. */
2509 CSR_WRITE_4(sc, JME_RXMAC, 0);
2511 /* Set up the receive filter. */
2516 * Disable all WOL bits as WOL can interfere normal Rx
2517 * operation. Also clear WOL detection status bits.
2519 reg = CSR_READ_4(sc, JME_PMCS);
2520 reg &= ~PMCS_WOL_ENB_MASK;
2521 CSR_WRITE_4(sc, JME_PMCS, reg);
2524 * Pad 10bytes right before received frame. This will greatly
2525 * help Rx performance on strict-alignment architectures as
2526 * it does not need to copy the frame to align the payload.
2528 reg = CSR_READ_4(sc, JME_RXMAC);
2529 reg |= RXMAC_PAD_10BYTES;
2531 if (ifp->if_capenable & IFCAP_RXCSUM)
2532 reg |= RXMAC_CSUM_ENB;
2533 CSR_WRITE_4(sc, JME_RXMAC, reg);
2535 /* Configure general purpose reg0 */
2536 reg = CSR_READ_4(sc, JME_GPREG0);
2537 reg &= ~GPREG0_PCC_UNIT_MASK;
2538 /* Set PCC timer resolution to micro-seconds unit. */
2539 reg |= GPREG0_PCC_UNIT_US;
2541 * Disable all shadow register posting as we have to read
2542 * JME_INTR_STATUS register in jme_intr. Also it seems
2543 * that it's hard to synchronize interrupt status between
2544 * hardware and software with shadow posting due to
2545 * requirements of bus_dmamap_sync(9).
2547 reg |= GPREG0_SH_POST_DW7_DIS | GPREG0_SH_POST_DW6_DIS |
2548 GPREG0_SH_POST_DW5_DIS | GPREG0_SH_POST_DW4_DIS |
2549 GPREG0_SH_POST_DW3_DIS | GPREG0_SH_POST_DW2_DIS |
2550 GPREG0_SH_POST_DW1_DIS | GPREG0_SH_POST_DW0_DIS;
2551 /* Disable posting of DW0. */
2552 reg &= ~GPREG0_POST_DW0_ENB;
2553 /* Clear PME message. */
2554 reg &= ~GPREG0_PME_ENB;
2555 /* Set PHY address. */
2556 reg &= ~GPREG0_PHY_ADDR_MASK;
2557 reg |= sc->jme_phyaddr;
2558 CSR_WRITE_4(sc, JME_GPREG0, reg);
2560 /* Configure Tx queue 0 packet completion coalescing. */
2561 jme_set_tx_coal(sc);
2563 /* Configure Rx queue 0 packet completion coalescing. */
2564 jme_set_rx_coal(sc);
2566 /* Configure shadow status block but don't enable posting. */
2567 paddr = sc->jme_cdata.jme_ssb_block_paddr;
2568 CSR_WRITE_4(sc, JME_SHBASE_ADDR_HI, JME_ADDR_HI(paddr));
2569 CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO, JME_ADDR_LO(paddr));
2571 /* Disable Timer 1 and Timer 2. */
2572 CSR_WRITE_4(sc, JME_TIMER1, 0);
2573 CSR_WRITE_4(sc, JME_TIMER2, 0);
2575 /* Configure retry transmit period, retry limit value. */
2576 CSR_WRITE_4(sc, JME_TXTRHD,
2577 ((TXTRHD_RT_PERIOD_DEFAULT << TXTRHD_RT_PERIOD_SHIFT) &
2578 TXTRHD_RT_PERIOD_MASK) |
2579 ((TXTRHD_RT_LIMIT_DEFAULT << TXTRHD_RT_LIMIT_SHIFT) &
2580 TXTRHD_RT_LIMIT_SHIFT));
2582 #ifdef DEVICE_POLLING
2583 if (!(ifp->if_flags & IFF_POLLING))
2585 /* Initialize the interrupt mask. */
2586 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
2587 CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2590 * Enabling Tx/Rx DMA engines and Rx queue processing is
2591 * done after detection of valid link in jme_miibus_statchg.
2593 sc->jme_flags &= ~JME_FLAG_LINK;
2595 /* Set the current media. */
2596 mii = device_get_softc(sc->jme_miibus);
2599 callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
2601 ifp->if_flags |= IFF_RUNNING;
2602 ifp->if_flags &= ~IFF_OACTIVE;
2606 jme_stop(struct jme_softc *sc)
2608 struct ifnet *ifp = &sc->arpcom.ac_if;
2609 struct jme_txdesc *txd;
2610 struct jme_rxdesc *rxd;
2611 struct jme_rxdata *rdata;
2614 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2617 * Mark the interface down and cancel the watchdog timer.
2619 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2622 callout_stop(&sc->jme_tick_ch);
2623 sc->jme_flags &= ~JME_FLAG_LINK;
2626 * Disable interrupts.
2628 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
2629 CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2631 /* Disable updating shadow status block. */
2632 CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO,
2633 CSR_READ_4(sc, JME_SHBASE_ADDR_LO) & ~SHBASE_POST_ENB);
2635 /* Stop receiver, transmitter. */
2640 * Free partial finished RX segments
2642 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
2643 rdata = &sc->jme_cdata.jme_rx_data[r];
2644 if (rdata->jme_rxhead != NULL)
2645 m_freem(rdata->jme_rxhead);
2646 JME_RXCHAIN_RESET(sc, r);
2650 * Free RX and TX mbufs still in the queues.
2652 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
2653 rdata = &sc->jme_cdata.jme_rx_data[r];
2654 for (i = 0; i < sc->jme_rx_desc_cnt; i++) {
2655 rxd = &rdata->jme_rxdesc[i];
2656 if (rxd->rx_m != NULL) {
2657 bus_dmamap_unload(rdata->jme_rx_tag,
2664 for (i = 0; i < sc->jme_tx_desc_cnt; i++) {
2665 txd = &sc->jme_cdata.jme_txdesc[i];
2666 if (txd->tx_m != NULL) {
2667 bus_dmamap_unload(sc->jme_cdata.jme_tx_tag,
2677 jme_stop_tx(struct jme_softc *sc)
2682 reg = CSR_READ_4(sc, JME_TXCSR);
2683 if ((reg & TXCSR_TX_ENB) == 0)
2685 reg &= ~TXCSR_TX_ENB;
2686 CSR_WRITE_4(sc, JME_TXCSR, reg);
2687 for (i = JME_TIMEOUT; i > 0; i--) {
2689 if ((CSR_READ_4(sc, JME_TXCSR) & TXCSR_TX_ENB) == 0)
2693 device_printf(sc->jme_dev, "stopping transmitter timeout!\n");
2697 jme_stop_rx(struct jme_softc *sc)
2702 reg = CSR_READ_4(sc, JME_RXCSR);
2703 if ((reg & RXCSR_RX_ENB) == 0)
2705 reg &= ~RXCSR_RX_ENB;
2706 CSR_WRITE_4(sc, JME_RXCSR, reg);
2707 for (i = JME_TIMEOUT; i > 0; i--) {
2709 if ((CSR_READ_4(sc, JME_RXCSR) & RXCSR_RX_ENB) == 0)
2713 device_printf(sc->jme_dev, "stopping recevier timeout!\n");
2717 jme_init_tx_ring(struct jme_softc *sc)
2719 struct jme_chain_data *cd;
2720 struct jme_txdesc *txd;
2723 sc->jme_cdata.jme_tx_prod = 0;
2724 sc->jme_cdata.jme_tx_cons = 0;
2725 sc->jme_cdata.jme_tx_cnt = 0;
2727 cd = &sc->jme_cdata;
2728 bzero(cd->jme_tx_ring, JME_TX_RING_SIZE(sc));
2729 for (i = 0; i < sc->jme_tx_desc_cnt; i++) {
2730 txd = &sc->jme_cdata.jme_txdesc[i];
2732 txd->tx_desc = &cd->jme_tx_ring[i];
2738 jme_init_ssb(struct jme_softc *sc)
2740 struct jme_chain_data *cd;
2742 cd = &sc->jme_cdata;
2743 bzero(cd->jme_ssb_block, JME_SSB_SIZE);
2747 jme_init_rx_ring(struct jme_softc *sc, int ring)
2749 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
2750 struct jme_rxdesc *rxd;
2753 KKASSERT(rdata->jme_rxhead == NULL &&
2754 rdata->jme_rxtail == NULL &&
2755 rdata->jme_rxlen == 0);
2756 rdata->jme_rx_cons = 0;
2758 bzero(rdata->jme_rx_ring, JME_RX_RING_SIZE(sc));
2759 for (i = 0; i < sc->jme_rx_desc_cnt; i++) {
2762 rxd = &rdata->jme_rxdesc[i];
2764 rxd->rx_desc = &rdata->jme_rx_ring[i];
2765 error = jme_newbuf(sc, ring, rxd, 1);
2773 jme_newbuf(struct jme_softc *sc, int ring, struct jme_rxdesc *rxd, int init)
2775 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
2776 struct jme_desc *desc;
2778 bus_dma_segment_t segs;
2782 m = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
2786 * JMC250 has 64bit boundary alignment limitation so jme(4)
2787 * takes advantage of 10 bytes padding feature of hardware
2788 * in order not to copy entire frame to align IP header on
2791 m->m_len = m->m_pkthdr.len = MCLBYTES;
2793 error = bus_dmamap_load_mbuf_segment(rdata->jme_rx_tag,
2794 rdata->jme_rx_sparemap, m, &segs, 1, &nsegs,
2799 if_printf(&sc->arpcom.ac_if, "can't load RX mbuf\n");
2803 if (rxd->rx_m != NULL) {
2804 bus_dmamap_sync(rdata->jme_rx_tag, rxd->rx_dmamap,
2805 BUS_DMASYNC_POSTREAD);
2806 bus_dmamap_unload(rdata->jme_rx_tag, rxd->rx_dmamap);
2808 map = rxd->rx_dmamap;
2809 rxd->rx_dmamap = rdata->jme_rx_sparemap;
2810 rdata->jme_rx_sparemap = map;
2813 desc = rxd->rx_desc;
2814 desc->buflen = htole32(segs.ds_len);
2815 desc->addr_lo = htole32(JME_ADDR_LO(segs.ds_addr));
2816 desc->addr_hi = htole32(JME_ADDR_HI(segs.ds_addr));
2817 desc->flags = htole32(JME_RD_OWN | JME_RD_INTR | JME_RD_64BIT);
2823 jme_set_vlan(struct jme_softc *sc)
2825 struct ifnet *ifp = &sc->arpcom.ac_if;
2828 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2830 reg = CSR_READ_4(sc, JME_RXMAC);
2831 reg &= ~RXMAC_VLAN_ENB;
2832 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
2833 reg |= RXMAC_VLAN_ENB;
2834 CSR_WRITE_4(sc, JME_RXMAC, reg);
2838 jme_set_filter(struct jme_softc *sc)
2840 struct ifnet *ifp = &sc->arpcom.ac_if;
2841 struct ifmultiaddr *ifma;
2846 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2848 rxcfg = CSR_READ_4(sc, JME_RXMAC);
2849 rxcfg &= ~(RXMAC_BROADCAST | RXMAC_PROMISC | RXMAC_MULTICAST |
2853 * Always accept frames destined to our station address.
2854 * Always accept broadcast frames.
2856 rxcfg |= RXMAC_UNICAST | RXMAC_BROADCAST;
2858 if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
2859 if (ifp->if_flags & IFF_PROMISC)
2860 rxcfg |= RXMAC_PROMISC;
2861 if (ifp->if_flags & IFF_ALLMULTI)
2862 rxcfg |= RXMAC_ALLMULTI;
2863 CSR_WRITE_4(sc, JME_MAR0, 0xFFFFFFFF);
2864 CSR_WRITE_4(sc, JME_MAR1, 0xFFFFFFFF);
2865 CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
2870 * Set up the multicast address filter by passing all multicast
2871 * addresses through a CRC generator, and then using the low-order
2872 * 6 bits as an index into the 64 bit multicast hash table. The
2873 * high order bits select the register, while the rest of the bits
2874 * select the bit within the register.
2876 rxcfg |= RXMAC_MULTICAST;
2877 bzero(mchash, sizeof(mchash));
2879 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2880 if (ifma->ifma_addr->sa_family != AF_LINK)
2882 crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
2883 ifma->ifma_addr), ETHER_ADDR_LEN);
2885 /* Just want the 6 least significant bits. */
2888 /* Set the corresponding bit in the hash table. */
2889 mchash[crc >> 5] |= 1 << (crc & 0x1f);
2892 CSR_WRITE_4(sc, JME_MAR0, mchash[0]);
2893 CSR_WRITE_4(sc, JME_MAR1, mchash[1]);
2894 CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
2898 jme_sysctl_tx_coal_to(SYSCTL_HANDLER_ARGS)
2900 struct jme_softc *sc = arg1;
2901 struct ifnet *ifp = &sc->arpcom.ac_if;
2904 ifnet_serialize_all(ifp);
2906 v = sc->jme_tx_coal_to;
2907 error = sysctl_handle_int(oidp, &v, 0, req);
2908 if (error || req->newptr == NULL)
2911 if (v < PCCTX_COAL_TO_MIN || v > PCCTX_COAL_TO_MAX) {
2916 if (v != sc->jme_tx_coal_to) {
2917 sc->jme_tx_coal_to = v;
2918 if (ifp->if_flags & IFF_RUNNING)
2919 jme_set_tx_coal(sc);
2922 ifnet_deserialize_all(ifp);
2927 jme_sysctl_tx_coal_pkt(SYSCTL_HANDLER_ARGS)
2929 struct jme_softc *sc = arg1;
2930 struct ifnet *ifp = &sc->arpcom.ac_if;
2933 ifnet_serialize_all(ifp);
2935 v = sc->jme_tx_coal_pkt;
2936 error = sysctl_handle_int(oidp, &v, 0, req);
2937 if (error || req->newptr == NULL)
2940 if (v < PCCTX_COAL_PKT_MIN || v > PCCTX_COAL_PKT_MAX) {
2945 if (v != sc->jme_tx_coal_pkt) {
2946 sc->jme_tx_coal_pkt = v;
2947 if (ifp->if_flags & IFF_RUNNING)
2948 jme_set_tx_coal(sc);
2951 ifnet_deserialize_all(ifp);
2956 jme_sysctl_rx_coal_to(SYSCTL_HANDLER_ARGS)
2958 struct jme_softc *sc = arg1;
2959 struct ifnet *ifp = &sc->arpcom.ac_if;
2962 ifnet_serialize_all(ifp);
2964 v = sc->jme_rx_coal_to;
2965 error = sysctl_handle_int(oidp, &v, 0, req);
2966 if (error || req->newptr == NULL)
2969 if (v < PCCRX_COAL_TO_MIN || v > PCCRX_COAL_TO_MAX) {
2974 if (v != sc->jme_rx_coal_to) {
2975 sc->jme_rx_coal_to = v;
2976 if (ifp->if_flags & IFF_RUNNING)
2977 jme_set_rx_coal(sc);
2980 ifnet_deserialize_all(ifp);
2985 jme_sysctl_rx_coal_pkt(SYSCTL_HANDLER_ARGS)
2987 struct jme_softc *sc = arg1;
2988 struct ifnet *ifp = &sc->arpcom.ac_if;
2991 ifnet_serialize_all(ifp);
2993 v = sc->jme_rx_coal_pkt;
2994 error = sysctl_handle_int(oidp, &v, 0, req);
2995 if (error || req->newptr == NULL)
2998 if (v < PCCRX_COAL_PKT_MIN || v > PCCRX_COAL_PKT_MAX) {
3003 if (v != sc->jme_rx_coal_pkt) {
3004 sc->jme_rx_coal_pkt = v;
3005 if (ifp->if_flags & IFF_RUNNING)
3006 jme_set_rx_coal(sc);
3009 ifnet_deserialize_all(ifp);
3014 jme_set_tx_coal(struct jme_softc *sc)
3018 reg = (sc->jme_tx_coal_to << PCCTX_COAL_TO_SHIFT) &
3020 reg |= (sc->jme_tx_coal_pkt << PCCTX_COAL_PKT_SHIFT) &
3021 PCCTX_COAL_PKT_MASK;
3022 reg |= PCCTX_COAL_TXQ0;
3023 CSR_WRITE_4(sc, JME_PCCTX, reg);
3027 jme_set_rx_coal(struct jme_softc *sc)
3032 reg = (sc->jme_rx_coal_to << PCCRX_COAL_TO_SHIFT) &
3034 reg |= (sc->jme_rx_coal_pkt << PCCRX_COAL_PKT_SHIFT) &
3035 PCCRX_COAL_PKT_MASK;
3036 for (r = 0; r < sc->jme_rx_ring_cnt; ++r)
3037 CSR_WRITE_4(sc, JME_PCCRX(r), reg);
3040 #ifdef DEVICE_POLLING
3043 jme_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3045 struct jme_softc *sc = ifp->if_softc;
3049 ASSERT_SERIALIZED(&sc->jme_serialize);
3053 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
3056 case POLL_DEREGISTER:
3057 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
3060 case POLL_AND_CHECK_STATUS:
3062 status = CSR_READ_4(sc, JME_INTR_STATUS);
3064 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
3065 struct jme_rxdata *rdata =
3066 &sc->jme_cdata.jme_rx_data[r];
3068 lwkt_serialize_enter(&rdata->jme_rx_serialize);
3069 jme_rxeof(sc, r, count);
3070 lwkt_serialize_exit(&rdata->jme_rx_serialize);
3073 if (status & INTR_RXQ_DESC_EMPTY) {
3074 CSR_WRITE_4(sc, JME_INTR_STATUS, status);
3075 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr |
3076 RXCSR_RX_ENB | RXCSR_RXQ_START);
3079 lwkt_serialize_enter(&sc->jme_cdata.jme_tx_serialize);
3081 if (!ifq_is_empty(&ifp->if_snd))
3083 lwkt_serialize_exit(&sc->jme_cdata.jme_tx_serialize);
3088 #endif /* DEVICE_POLLING */
3091 jme_rxring_dma_alloc(struct jme_softc *sc, int ring)
3093 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
3097 error = bus_dmamem_coherent(sc->jme_cdata.jme_ring_tag,
3098 JME_RX_RING_ALIGN, 0,
3099 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3100 JME_RX_RING_SIZE(sc),
3101 BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
3103 device_printf(sc->jme_dev,
3104 "could not allocate %dth Rx ring.\n", ring);
3107 rdata->jme_rx_ring_tag = dmem.dmem_tag;
3108 rdata->jme_rx_ring_map = dmem.dmem_map;
3109 rdata->jme_rx_ring = dmem.dmem_addr;
3110 rdata->jme_rx_ring_paddr = dmem.dmem_busaddr;
3116 jme_rxbuf_dma_alloc(struct jme_softc *sc, int ring)
3118 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[ring];
3121 /* Create tag for Rx buffers. */
3122 error = bus_dma_tag_create(sc->jme_cdata.jme_buffer_tag,/* parent */
3123 JME_RX_BUF_ALIGN, 0, /* algnmnt, boundary */
3124 BUS_SPACE_MAXADDR, /* lowaddr */
3125 BUS_SPACE_MAXADDR, /* highaddr */
3126 NULL, NULL, /* filter, filterarg */
3127 MCLBYTES, /* maxsize */
3129 MCLBYTES, /* maxsegsize */
3130 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ALIGNED,/* flags */
3131 &rdata->jme_rx_tag);
3133 device_printf(sc->jme_dev,
3134 "could not create %dth Rx DMA tag.\n", ring);
3138 /* Create DMA maps for Rx buffers. */
3139 error = bus_dmamap_create(rdata->jme_rx_tag, BUS_DMA_WAITOK,
3140 &rdata->jme_rx_sparemap);
3142 device_printf(sc->jme_dev,
3143 "could not create %dth spare Rx dmamap.\n", ring);
3144 bus_dma_tag_destroy(rdata->jme_rx_tag);
3145 rdata->jme_rx_tag = NULL;
3148 for (i = 0; i < sc->jme_rx_desc_cnt; i++) {
3149 struct jme_rxdesc *rxd = &rdata->jme_rxdesc[i];
3151 error = bus_dmamap_create(rdata->jme_rx_tag, BUS_DMA_WAITOK,
3156 device_printf(sc->jme_dev,
3157 "could not create %dth Rx dmamap "
3158 "for %dth RX ring.\n", i, ring);
3160 for (j = 0; j < i; ++j) {
3161 rxd = &rdata->jme_rxdesc[j];
3162 bus_dmamap_destroy(rdata->jme_rx_tag,
3165 bus_dmamap_destroy(rdata->jme_rx_tag,
3166 rdata->jme_rx_sparemap);
3167 bus_dma_tag_destroy(rdata->jme_rx_tag);
3168 rdata->jme_rx_tag = NULL;
3176 jme_rx_intr(struct jme_softc *sc, uint32_t status)
3180 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
3181 if (status & jme_rx_status[r].jme_coal) {
3182 struct jme_rxdata *rdata =
3183 &sc->jme_cdata.jme_rx_data[r];
3185 lwkt_serialize_enter(&rdata->jme_rx_serialize);
3186 jme_rxeof(sc, r, -1);
3187 lwkt_serialize_exit(&rdata->jme_rx_serialize);
3193 jme_enable_rss(struct jme_softc *sc)
3196 uint8_t key[RSSKEY_NREGS * RSSKEY_REGSIZE];
3199 KASSERT(sc->jme_rx_ring_cnt == JME_NRXRING_2 ||
3200 sc->jme_rx_ring_cnt == JME_NRXRING_4,
3201 ("%s: invalid # of RX rings (%d)\n",
3202 sc->arpcom.ac_if.if_xname, sc->jme_rx_ring_cnt));
3204 rssc = RSSC_HASH_64_ENTRY;
3205 rssc |= RSSC_HASH_IPV4 | RSSC_HASH_IPV4_TCP;
3206 rssc |= sc->jme_rx_ring_cnt >> 1;
3207 JME_RSS_DPRINTF(sc, 1, "rssc 0x%08x\n", rssc);
3208 CSR_WRITE_4(sc, JME_RSSC, rssc);
3210 toeplitz_get_key(key, sizeof(key));
3211 for (i = 0; i < RSSKEY_NREGS; ++i) {
3214 keyreg = RSSKEY_REGVAL(key, i);
3215 JME_RSS_DPRINTF(sc, 5, "keyreg%d 0x%08x\n", i, keyreg);
3217 CSR_WRITE_4(sc, RSSKEY_REG(i), keyreg);
3221 * Create redirect table in following fashion:
3222 * (hash & ring_cnt_mask) == rdr_table[(hash & rdr_table_mask)]
3225 for (i = 0; i < RSSTBL_REGSIZE; ++i) {
3228 q = i % sc->jme_rx_ring_cnt;
3229 ind |= q << (i * 8);
3231 JME_RSS_DPRINTF(sc, 1, "ind 0x%08x\n", ind);
3233 for (i = 0; i < RSSTBL_NREGS; ++i)
3234 CSR_WRITE_4(sc, RSSTBL_REG(i), ind);
3238 jme_disable_rss(struct jme_softc *sc)
3240 CSR_WRITE_4(sc, JME_RSSC, RSSC_DIS_RSS);
3244 jme_serialize(struct ifnet *ifp, enum ifnet_serialize slz)
3246 struct jme_softc *sc = ifp->if_softc;
3249 case IFNET_SERIALIZE_ALL:
3250 lwkt_serialize_array_enter(sc->jme_serialize_arr,
3251 sc->jme_serialize_cnt, 0);
3254 case IFNET_SERIALIZE_MAIN:
3255 lwkt_serialize_enter(&sc->jme_serialize);
3258 case IFNET_SERIALIZE_TX:
3259 lwkt_serialize_enter(&sc->jme_cdata.jme_tx_serialize);
3262 case IFNET_SERIALIZE_RX(0):
3263 lwkt_serialize_enter(
3264 &sc->jme_cdata.jme_rx_data[0].jme_rx_serialize);
3267 case IFNET_SERIALIZE_RX(1):
3268 lwkt_serialize_enter(
3269 &sc->jme_cdata.jme_rx_data[1].jme_rx_serialize);
3272 case IFNET_SERIALIZE_RX(2):
3273 lwkt_serialize_enter(
3274 &sc->jme_cdata.jme_rx_data[2].jme_rx_serialize);
3277 case IFNET_SERIALIZE_RX(3):
3278 lwkt_serialize_enter(
3279 &sc->jme_cdata.jme_rx_data[3].jme_rx_serialize);
3283 panic("%s unsupported serialize type\n", ifp->if_xname);
3288 jme_deserialize(struct ifnet *ifp, enum ifnet_serialize slz)
3290 struct jme_softc *sc = ifp->if_softc;
3293 case IFNET_SERIALIZE_ALL:
3294 lwkt_serialize_array_exit(sc->jme_serialize_arr,
3295 sc->jme_serialize_cnt, 0);
3298 case IFNET_SERIALIZE_MAIN:
3299 lwkt_serialize_exit(&sc->jme_serialize);
3302 case IFNET_SERIALIZE_TX:
3303 lwkt_serialize_exit(&sc->jme_cdata.jme_tx_serialize);
3306 case IFNET_SERIALIZE_RX(0):
3307 lwkt_serialize_exit(
3308 &sc->jme_cdata.jme_rx_data[0].jme_rx_serialize);
3311 case IFNET_SERIALIZE_RX(1):
3312 lwkt_serialize_exit(
3313 &sc->jme_cdata.jme_rx_data[1].jme_rx_serialize);
3316 case IFNET_SERIALIZE_RX(2):
3317 lwkt_serialize_exit(
3318 &sc->jme_cdata.jme_rx_data[2].jme_rx_serialize);
3321 case IFNET_SERIALIZE_RX(3):
3322 lwkt_serialize_exit(
3323 &sc->jme_cdata.jme_rx_data[3].jme_rx_serialize);
3327 panic("%s unsupported serialize type\n", ifp->if_xname);
3332 jme_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz)
3334 struct jme_softc *sc = ifp->if_softc;
3337 case IFNET_SERIALIZE_ALL:
3338 return lwkt_serialize_array_try(sc->jme_serialize_arr,
3339 sc->jme_serialize_cnt, 0);
3341 case IFNET_SERIALIZE_MAIN:
3342 return lwkt_serialize_try(&sc->jme_serialize);
3344 case IFNET_SERIALIZE_TX:
3345 return lwkt_serialize_try(&sc->jme_cdata.jme_tx_serialize);
3347 case IFNET_SERIALIZE_RX(0):
3348 return lwkt_serialize_try(
3349 &sc->jme_cdata.jme_rx_data[0].jme_rx_serialize);
3351 case IFNET_SERIALIZE_RX(1):
3352 return lwkt_serialize_try(
3353 &sc->jme_cdata.jme_rx_data[1].jme_rx_serialize);
3355 case IFNET_SERIALIZE_RX(2):
3356 return lwkt_serialize_try(
3357 &sc->jme_cdata.jme_rx_data[2].jme_rx_serialize);
3359 case IFNET_SERIALIZE_RX(3):
3360 return lwkt_serialize_try(
3361 &sc->jme_cdata.jme_rx_data[3].jme_rx_serialize);
3364 panic("%s unsupported serialize type\n", ifp->if_xname);
3371 jme_serialize_assert(struct ifnet *ifp, enum ifnet_serialize slz,
3372 boolean_t serialized)
3374 struct jme_softc *sc = ifp->if_softc;
3375 struct jme_rxdata *rdata;
3379 case IFNET_SERIALIZE_ALL:
3381 for (i = 0; i < sc->jme_serialize_cnt; ++i)
3382 ASSERT_SERIALIZED(sc->jme_serialize_arr[i]);
3384 for (i = 0; i < sc->jme_serialize_cnt; ++i)
3385 ASSERT_NOT_SERIALIZED(sc->jme_serialize_arr[i]);
3389 case IFNET_SERIALIZE_MAIN:
3391 ASSERT_SERIALIZED(&sc->jme_serialize);
3393 ASSERT_NOT_SERIALIZED(&sc->jme_serialize);
3396 case IFNET_SERIALIZE_TX:
3398 ASSERT_SERIALIZED(&sc->jme_cdata.jme_tx_serialize);
3400 ASSERT_NOT_SERIALIZED(&sc->jme_cdata.jme_tx_serialize);
3403 case IFNET_SERIALIZE_RX(0):
3404 rdata = &sc->jme_cdata.jme_rx_data[0];
3406 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3408 ASSERT_NOT_SERIALIZED(&rdata->jme_rx_serialize);
3411 case IFNET_SERIALIZE_RX(1):
3412 rdata = &sc->jme_cdata.jme_rx_data[1];
3414 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3416 ASSERT_NOT_SERIALIZED(&rdata->jme_rx_serialize);
3419 case IFNET_SERIALIZE_RX(2):
3420 rdata = &sc->jme_cdata.jme_rx_data[2];
3422 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3424 ASSERT_NOT_SERIALIZED(&rdata->jme_rx_serialize);
3427 case IFNET_SERIALIZE_RX(3):
3428 rdata = &sc->jme_cdata.jme_rx_data[3];
3430 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3432 ASSERT_NOT_SERIALIZED(&rdata->jme_rx_serialize);
3436 panic("%s unsupported serialize type\n", ifp->if_xname);
3440 #endif /* INVARIANTS */
3443 jme_msix_try_alloc(device_t dev)
3445 struct jme_softc *sc = device_get_softc(dev);
3446 struct jme_msix_data *msix;
3447 int error, i, r, msix_enable, msix_count;
3449 msix_count = 1 + sc->jme_rx_ring_cnt;
3450 KKASSERT(msix_count <= JME_NMSIX);
3452 msix_enable = device_getenv_int(dev, "msix.enable", jme_msix_enable);
3455 * We leave the 1st MSI-X vector unused, so we
3456 * actually need msix_count + 1 MSI-X vectors.
3458 if (!msix_enable || pci_msix_count(dev) < (msix_count + 1))
3461 for (i = 0; i < msix_count; ++i)
3462 sc->jme_msix[i].jme_msix_rid = -1;
3466 msix = &sc->jme_msix[i++];
3467 msix->jme_msix_cpuid = 0; /* XXX Put TX to cpu0 */
3468 msix->jme_msix_arg = &sc->jme_cdata;
3469 msix->jme_msix_func = jme_msix_tx;
3470 msix->jme_msix_intrs = INTR_TXQ_COAL | INTR_TXQ_COAL_TO;
3471 msix->jme_msix_serialize = &sc->jme_cdata.jme_tx_serialize;
3472 ksnprintf(msix->jme_msix_desc, sizeof(msix->jme_msix_desc), "%s tx",
3473 device_get_nameunit(dev));
3475 for (r = 0; r < sc->jme_rx_ring_cnt; ++r) {
3476 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
3478 msix = &sc->jme_msix[i++];
3479 msix->jme_msix_cpuid = r; /* XXX Put RX to cpuX */
3480 msix->jme_msix_arg = rdata;
3481 msix->jme_msix_func = jme_msix_rx;
3482 msix->jme_msix_intrs = rdata->jme_rx_coal | rdata->jme_rx_empty;
3483 msix->jme_msix_serialize = &rdata->jme_rx_serialize;
3484 ksnprintf(msix->jme_msix_desc, sizeof(msix->jme_msix_desc),
3485 "%s rx%d", device_get_nameunit(dev), r);
3488 KKASSERT(i == msix_count);
3490 error = pci_setup_msix(dev);
3494 /* Setup jme_msix_cnt early, so we could cleanup */
3495 sc->jme_msix_cnt = msix_count;
3497 for (i = 0; i < msix_count; ++i) {
3498 msix = &sc->jme_msix[i];
3500 msix->jme_msix_vector = i + 1;
3501 error = pci_alloc_msix_vector(dev, msix->jme_msix_vector,
3502 &msix->jme_msix_rid, msix->jme_msix_cpuid);
3506 msix->jme_msix_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
3507 &msix->jme_msix_rid, RF_ACTIVE);
3508 if (msix->jme_msix_res == NULL) {
3514 for (i = 0; i < JME_INTR_CNT; ++i) {
3515 uint32_t intr_mask = (1 << i);
3518 if ((JME_INTRS & intr_mask) == 0)
3521 for (x = 0; x < msix_count; ++x) {
3522 msix = &sc->jme_msix[x];
3523 if (msix->jme_msix_intrs & intr_mask) {
3526 reg = i / JME_MSINUM_FACTOR;
3527 KKASSERT(reg < JME_MSINUM_CNT);
3529 shift = (i % JME_MSINUM_FACTOR) * 4;
3531 sc->jme_msinum[reg] |=
3532 (msix->jme_msix_vector << shift);
3540 for (i = 0; i < JME_MSINUM_CNT; ++i) {
3541 device_printf(dev, "MSINUM%d: %#x\n", i,
3546 pci_enable_msix(dev);
3547 sc->jme_irq_type = PCI_INTR_TYPE_MSIX;
3555 jme_intr_alloc(device_t dev)
3557 struct jme_softc *sc = device_get_softc(dev);
3560 jme_msix_try_alloc(dev);
3562 if (sc->jme_irq_type != PCI_INTR_TYPE_MSIX) {
3563 sc->jme_irq_type = pci_alloc_1intr(dev, jme_msi_enable,
3564 &sc->jme_irq_rid, &irq_flags);
3566 sc->jme_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
3567 &sc->jme_irq_rid, irq_flags);
3568 if (sc->jme_irq_res == NULL) {
3569 device_printf(dev, "can't allocate irq\n");
3577 jme_msix_free(device_t dev)
3579 struct jme_softc *sc = device_get_softc(dev);
3582 KKASSERT(sc->jme_msix_cnt > 1);
3584 for (i = 0; i < sc->jme_msix_cnt; ++i) {
3585 struct jme_msix_data *msix = &sc->jme_msix[i];
3587 if (msix->jme_msix_res != NULL) {
3588 bus_release_resource(dev, SYS_RES_IRQ,
3589 msix->jme_msix_rid, msix->jme_msix_res);
3590 msix->jme_msix_res = NULL;
3592 if (msix->jme_msix_rid >= 0) {
3593 pci_release_msix_vector(dev, msix->jme_msix_rid);
3594 msix->jme_msix_rid = -1;
3597 pci_teardown_msix(dev);
3601 jme_intr_free(device_t dev)
3603 struct jme_softc *sc = device_get_softc(dev);
3605 if (sc->jme_irq_type != PCI_INTR_TYPE_MSIX) {
3606 if (sc->jme_irq_res != NULL) {
3607 bus_release_resource(dev, SYS_RES_IRQ, sc->jme_irq_rid,
3610 if (sc->jme_irq_type == PCI_INTR_TYPE_MSI)
3611 pci_release_msi(dev);
3618 jme_msix_tx(void *xcd)
3620 struct jme_chain_data *cd = xcd;
3621 struct jme_softc *sc = cd->jme_sc;
3622 struct ifnet *ifp = &sc->arpcom.ac_if;
3624 ASSERT_SERIALIZED(&cd->jme_tx_serialize);
3626 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, INTR_TXQ_COAL | INTR_TXQ_COAL_TO);
3628 CSR_WRITE_4(sc, JME_INTR_STATUS,
3629 INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP);
3631 if (ifp->if_flags & IFF_RUNNING) {
3633 if (!ifq_is_empty(&ifp->if_snd))
3637 CSR_WRITE_4(sc, JME_INTR_MASK_SET, INTR_TXQ_COAL | INTR_TXQ_COAL_TO);
3641 jme_msix_rx(void *xrdata)
3643 struct jme_rxdata *rdata = xrdata;
3644 struct jme_softc *sc = rdata->jme_sc;
3645 struct ifnet *ifp = &sc->arpcom.ac_if;
3648 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3650 CSR_WRITE_4(sc, JME_INTR_MASK_CLR,
3651 (rdata->jme_rx_coal | rdata->jme_rx_empty));
3653 status = CSR_READ_4(sc, JME_INTR_STATUS);
3654 status &= (rdata->jme_rx_coal | rdata->jme_rx_empty);
3656 if (status & rdata->jme_rx_coal)
3657 status |= (rdata->jme_rx_coal | rdata->jme_rx_comp);
3658 CSR_WRITE_4(sc, JME_INTR_STATUS, status);
3660 if (ifp->if_flags & IFF_RUNNING) {
3661 if (status & rdata->jme_rx_coal)
3662 jme_rxeof(sc, rdata->jme_rx_idx, -1);
3664 if (status & rdata->jme_rx_empty) {
3665 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr |
3666 RXCSR_RX_ENB | RXCSR_RXQ_START);
3670 CSR_WRITE_4(sc, JME_INTR_MASK_SET,
3671 (rdata->jme_rx_coal | rdata->jme_rx_empty));
3675 jme_set_msinum(struct jme_softc *sc)
3679 for (i = 0; i < JME_MSINUM_CNT; ++i)
3680 CSR_WRITE_4(sc, JME_MSINUM(i), sc->jme_msinum[i]);
3684 jme_intr_setup(device_t dev)
3686 struct jme_softc *sc = device_get_softc(dev);
3687 struct ifnet *ifp = &sc->arpcom.ac_if;
3690 if (sc->jme_irq_type == PCI_INTR_TYPE_MSIX)
3691 return jme_msix_setup(dev);
3693 error = bus_setup_intr(dev, sc->jme_irq_res, INTR_MPSAFE,
3694 jme_intr, sc, &sc->jme_irq_handle, &sc->jme_serialize);
3696 device_printf(dev, "could not set up interrupt handler.\n");
3700 ifp->if_cpuid = rman_get_cpuid(sc->jme_irq_res);
3701 KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
3706 jme_intr_teardown(device_t dev)
3708 struct jme_softc *sc = device_get_softc(dev);
3710 if (sc->jme_irq_type == PCI_INTR_TYPE_MSIX)
3711 jme_msix_teardown(dev, sc->jme_msix_cnt);
3713 bus_teardown_intr(dev, sc->jme_irq_res, sc->jme_irq_handle);
3717 jme_msix_setup(device_t dev)
3719 struct jme_softc *sc = device_get_softc(dev);
3720 struct ifnet *ifp = &sc->arpcom.ac_if;
3723 for (x = 0; x < sc->jme_msix_cnt; ++x) {
3724 struct jme_msix_data *msix = &sc->jme_msix[x];
3727 error = bus_setup_intr_descr(dev, msix->jme_msix_res,
3728 INTR_MPSAFE, msix->jme_msix_func, msix->jme_msix_arg,
3729 &msix->jme_msix_handle, msix->jme_msix_serialize,
3730 msix->jme_msix_desc);
3732 device_printf(dev, "could not set up %s "
3733 "interrupt handler.\n", msix->jme_msix_desc);
3734 jme_msix_teardown(dev, x);
3738 ifp->if_cpuid = 0; /* XXX */
3743 jme_msix_teardown(device_t dev, int msix_count)
3745 struct jme_softc *sc = device_get_softc(dev);
3748 for (x = 0; x < msix_count; ++x) {
3749 struct jme_msix_data *msix = &sc->jme_msix[x];
3751 bus_teardown_intr(dev, msix->jme_msix_res,
3752 msix->jme_msix_handle);