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"
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/ifq_var.h>
54 #include <net/toeplitz.h>
55 #include <net/toeplitz2.h>
56 #include <net/vlan/if_vlan_var.h>
57 #include <net/vlan/if_vlan_ether.h>
59 #include <netinet/in.h>
61 #include <dev/netif/mii_layer/miivar.h>
62 #include <dev/netif/mii_layer/jmphyreg.h>
64 #include <bus/pci/pcireg.h>
65 #include <bus/pci/pcivar.h>
66 #include <bus/pci/pcidevs.h>
68 #include <dev/netif/jme/if_jmereg.h>
69 #include <dev/netif/jme/if_jmevar.h>
71 #include "miibus_if.h"
73 /* Define the following to disable printing Rx errors. */
74 #undef JME_SHOW_ERRORS
76 #define JME_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
79 #define JME_RSS_DPRINTF(sc, lvl, fmt, ...) \
81 if ((sc)->jme_rss_debug >= (lvl)) \
82 if_printf(&(sc)->arpcom.ac_if, fmt, __VA_ARGS__); \
84 #else /* !JME_RSS_DEBUG */
85 #define JME_RSS_DPRINTF(sc, lvl, fmt, ...) ((void)0)
86 #endif /* JME_RSS_DEBUG */
88 static int jme_probe(device_t);
89 static int jme_attach(device_t);
90 static int jme_detach(device_t);
91 static int jme_shutdown(device_t);
92 static int jme_suspend(device_t);
93 static int jme_resume(device_t);
95 static int jme_miibus_readreg(device_t, int, int);
96 static int jme_miibus_writereg(device_t, int, int, int);
97 static void jme_miibus_statchg(device_t);
99 static void jme_init(void *);
100 static int jme_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
101 static void jme_start(struct ifnet *);
102 static void jme_watchdog(struct ifnet *);
103 static void jme_mediastatus(struct ifnet *, struct ifmediareq *);
104 static int jme_mediachange(struct ifnet *);
105 #ifdef DEVICE_POLLING
106 static void jme_poll(struct ifnet *, enum poll_cmd, int);
108 static void jme_serialize(struct ifnet *, enum ifnet_serialize);
109 static void jme_deserialize(struct ifnet *, enum ifnet_serialize);
110 static int jme_tryserialize(struct ifnet *, enum ifnet_serialize);
112 static void jme_serialize_assert(struct ifnet *, enum ifnet_serialize,
116 static void jme_intr(void *);
117 static void jme_msix_tx(void *);
118 static void jme_msix_rx(void *);
119 static void jme_txeof(struct jme_softc *);
120 static void jme_rxeof(struct jme_rxdata *, int);
121 static void jme_rx_intr(struct jme_softc *, uint32_t);
123 static int jme_msix_setup(device_t);
124 static void jme_msix_teardown(device_t, int);
125 static int jme_intr_setup(device_t);
126 static void jme_intr_teardown(device_t);
127 static void jme_msix_try_alloc(device_t);
128 static void jme_msix_free(device_t);
129 static int jme_intr_alloc(device_t);
130 static void jme_intr_free(device_t);
131 static int jme_dma_alloc(struct jme_softc *);
132 static void jme_dma_free(struct jme_softc *);
133 static int jme_init_rx_ring(struct jme_rxdata *);
134 static void jme_init_tx_ring(struct jme_softc *);
135 static void jme_init_ssb(struct jme_softc *);
136 static int jme_newbuf(struct jme_rxdata *, struct jme_rxdesc *, int);
137 static int jme_encap(struct jme_softc *, struct mbuf **);
138 static void jme_rxpkt(struct jme_rxdata *);
139 static int jme_rxring_dma_alloc(struct jme_rxdata *);
140 static int jme_rxbuf_dma_alloc(struct jme_rxdata *);
142 static void jme_tick(void *);
143 static void jme_stop(struct jme_softc *);
144 static void jme_reset(struct jme_softc *);
145 static void jme_set_msinum(struct jme_softc *);
146 static void jme_set_vlan(struct jme_softc *);
147 static void jme_set_filter(struct jme_softc *);
148 static void jme_stop_tx(struct jme_softc *);
149 static void jme_stop_rx(struct jme_softc *);
150 static void jme_mac_config(struct jme_softc *);
151 static void jme_reg_macaddr(struct jme_softc *, uint8_t[]);
152 static int jme_eeprom_macaddr(struct jme_softc *, uint8_t[]);
153 static int jme_eeprom_read_byte(struct jme_softc *, uint8_t, uint8_t *);
155 static void jme_setwol(struct jme_softc *);
156 static void jme_setlinkspeed(struct jme_softc *);
158 static void jme_set_tx_coal(struct jme_softc *);
159 static void jme_set_rx_coal(struct jme_softc *);
160 static void jme_enable_rss(struct jme_softc *);
161 static void jme_disable_rss(struct jme_softc *);
163 static void jme_sysctl_node(struct jme_softc *);
164 static int jme_sysctl_tx_coal_to(SYSCTL_HANDLER_ARGS);
165 static int jme_sysctl_tx_coal_pkt(SYSCTL_HANDLER_ARGS);
166 static int jme_sysctl_rx_coal_to(SYSCTL_HANDLER_ARGS);
167 static int jme_sysctl_rx_coal_pkt(SYSCTL_HANDLER_ARGS);
170 * Devices supported by this driver.
172 static const struct jme_dev {
173 uint16_t jme_vendorid;
174 uint16_t jme_deviceid;
176 const char *jme_name;
178 { PCI_VENDOR_JMICRON, PCI_PRODUCT_JMICRON_JMC250,
180 "JMicron Inc, JMC250 Gigabit Ethernet" },
181 { PCI_VENDOR_JMICRON, PCI_PRODUCT_JMICRON_JMC260,
183 "JMicron Inc, JMC260 Fast Ethernet" },
187 static device_method_t jme_methods[] = {
188 /* Device interface. */
189 DEVMETHOD(device_probe, jme_probe),
190 DEVMETHOD(device_attach, jme_attach),
191 DEVMETHOD(device_detach, jme_detach),
192 DEVMETHOD(device_shutdown, jme_shutdown),
193 DEVMETHOD(device_suspend, jme_suspend),
194 DEVMETHOD(device_resume, jme_resume),
197 DEVMETHOD(bus_print_child, bus_generic_print_child),
198 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
201 DEVMETHOD(miibus_readreg, jme_miibus_readreg),
202 DEVMETHOD(miibus_writereg, jme_miibus_writereg),
203 DEVMETHOD(miibus_statchg, jme_miibus_statchg),
208 static driver_t jme_driver = {
211 sizeof(struct jme_softc)
214 static devclass_t jme_devclass;
216 DECLARE_DUMMY_MODULE(if_jme);
217 MODULE_DEPEND(if_jme, miibus, 1, 1, 1);
218 DRIVER_MODULE(if_jme, pci, jme_driver, jme_devclass, NULL, NULL);
219 DRIVER_MODULE(miibus, jme, miibus_driver, miibus_devclass, NULL, NULL);
221 static const struct {
225 } jme_rx_status[JME_NRXRING_MAX] = {
226 { INTR_RXQ0_COAL | INTR_RXQ0_COAL_TO, INTR_RXQ0_COMP,
227 INTR_RXQ0_DESC_EMPTY },
228 { INTR_RXQ1_COAL | INTR_RXQ1_COAL_TO, INTR_RXQ1_COMP,
229 INTR_RXQ1_DESC_EMPTY },
230 { INTR_RXQ2_COAL | INTR_RXQ2_COAL_TO, INTR_RXQ2_COMP,
231 INTR_RXQ2_DESC_EMPTY },
232 { INTR_RXQ3_COAL | INTR_RXQ3_COAL_TO, INTR_RXQ3_COMP,
233 INTR_RXQ3_DESC_EMPTY }
236 static int jme_rx_desc_count = JME_RX_DESC_CNT_DEF;
237 static int jme_tx_desc_count = JME_TX_DESC_CNT_DEF;
238 static int jme_rx_ring_count = 1;
239 static int jme_msi_enable = 1;
240 static int jme_msix_enable = 1;
242 TUNABLE_INT("hw.jme.rx_desc_count", &jme_rx_desc_count);
243 TUNABLE_INT("hw.jme.tx_desc_count", &jme_tx_desc_count);
244 TUNABLE_INT("hw.jme.rx_ring_count", &jme_rx_ring_count);
245 TUNABLE_INT("hw.jme.msi.enable", &jme_msi_enable);
246 TUNABLE_INT("hw.jme.msix.enable", &jme_msix_enable);
249 * Read a PHY register on the MII of the JMC250.
252 jme_miibus_readreg(device_t dev, int phy, int reg)
254 struct jme_softc *sc = device_get_softc(dev);
258 /* For FPGA version, PHY address 0 should be ignored. */
259 if (sc->jme_caps & JME_CAP_FPGA) {
263 if (sc->jme_phyaddr != phy)
267 CSR_WRITE_4(sc, JME_SMI, SMI_OP_READ | SMI_OP_EXECUTE |
268 SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg));
270 for (i = JME_PHY_TIMEOUT; i > 0; i--) {
272 if (((val = CSR_READ_4(sc, JME_SMI)) & SMI_OP_EXECUTE) == 0)
276 device_printf(sc->jme_dev, "phy read timeout: "
277 "phy %d, reg %d\n", phy, reg);
281 return ((val & SMI_DATA_MASK) >> SMI_DATA_SHIFT);
285 * Write a PHY register on the MII of the JMC250.
288 jme_miibus_writereg(device_t dev, int phy, int reg, int val)
290 struct jme_softc *sc = device_get_softc(dev);
293 /* For FPGA version, PHY address 0 should be ignored. */
294 if (sc->jme_caps & JME_CAP_FPGA) {
298 if (sc->jme_phyaddr != phy)
302 CSR_WRITE_4(sc, JME_SMI, SMI_OP_WRITE | SMI_OP_EXECUTE |
303 ((val << SMI_DATA_SHIFT) & SMI_DATA_MASK) |
304 SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg));
306 for (i = JME_PHY_TIMEOUT; i > 0; i--) {
308 if (((val = CSR_READ_4(sc, JME_SMI)) & SMI_OP_EXECUTE) == 0)
312 device_printf(sc->jme_dev, "phy write timeout: "
313 "phy %d, reg %d\n", phy, reg);
320 * Callback from MII layer when media changes.
323 jme_miibus_statchg(device_t dev)
325 struct jme_softc *sc = device_get_softc(dev);
326 struct ifnet *ifp = &sc->arpcom.ac_if;
327 struct mii_data *mii;
328 struct jme_txdesc *txd;
332 ASSERT_IFNET_SERIALIZED_ALL(ifp);
334 if ((ifp->if_flags & IFF_RUNNING) == 0)
337 mii = device_get_softc(sc->jme_miibus);
339 sc->jme_flags &= ~JME_FLAG_LINK;
340 if ((mii->mii_media_status & IFM_AVALID) != 0) {
341 switch (IFM_SUBTYPE(mii->mii_media_active)) {
344 sc->jme_flags |= JME_FLAG_LINK;
347 if (sc->jme_caps & JME_CAP_FASTETH)
349 sc->jme_flags |= JME_FLAG_LINK;
357 * Disabling Rx/Tx MACs have a side-effect of resetting
358 * JME_TXNDA/JME_RXNDA register to the first address of
359 * Tx/Rx descriptor address. So driver should reset its
360 * internal procucer/consumer pointer and reclaim any
361 * allocated resources. Note, just saving the value of
362 * JME_TXNDA and JME_RXNDA registers before stopping MAC
363 * and restoring JME_TXNDA/JME_RXNDA register is not
364 * sufficient to make sure correct MAC state because
365 * stopping MAC operation can take a while and hardware
366 * might have updated JME_TXNDA/JME_RXNDA registers
367 * during the stop operation.
370 /* Disable interrupts */
371 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
374 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
376 callout_stop(&sc->jme_tick_ch);
378 /* Stop receiver/transmitter. */
382 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
383 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
385 jme_rxeof(rdata, -1);
386 if (rdata->jme_rxhead != NULL)
387 m_freem(rdata->jme_rxhead);
388 JME_RXCHAIN_RESET(rdata);
391 * Reuse configured Rx descriptors and reset
392 * procuder/consumer index.
394 rdata->jme_rx_cons = 0;
398 if (sc->jme_cdata.jme_tx_cnt != 0) {
399 /* Remove queued packets for transmit. */
400 for (i = 0; i < sc->jme_cdata.jme_tx_desc_cnt; i++) {
401 txd = &sc->jme_cdata.jme_txdesc[i];
402 if (txd->tx_m != NULL) {
404 sc->jme_cdata.jme_tx_tag,
413 jme_init_tx_ring(sc);
415 /* Initialize shadow status block. */
418 /* Program MAC with resolved speed/duplex/flow-control. */
419 if (sc->jme_flags & JME_FLAG_LINK) {
422 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr);
424 /* Set Tx ring address to the hardware. */
425 paddr = sc->jme_cdata.jme_tx_ring_paddr;
426 CSR_WRITE_4(sc, JME_TXDBA_HI, JME_ADDR_HI(paddr));
427 CSR_WRITE_4(sc, JME_TXDBA_LO, JME_ADDR_LO(paddr));
429 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
430 CSR_WRITE_4(sc, JME_RXCSR,
431 sc->jme_rxcsr | RXCSR_RXQ_N_SEL(r));
433 /* Set Rx ring address to the hardware. */
434 paddr = sc->jme_cdata.jme_rx_data[r].jme_rx_ring_paddr;
435 CSR_WRITE_4(sc, JME_RXDBA_HI, JME_ADDR_HI(paddr));
436 CSR_WRITE_4(sc, JME_RXDBA_LO, JME_ADDR_LO(paddr));
439 /* Restart receiver/transmitter. */
440 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RX_ENB |
442 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr | TXCSR_TX_ENB);
445 ifp->if_flags |= IFF_RUNNING;
446 ifp->if_flags &= ~IFF_OACTIVE;
447 callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
449 #ifdef DEVICE_POLLING
450 if (!(ifp->if_flags & IFF_POLLING))
452 /* Reenable interrupts. */
453 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
457 * Get the current interface media status.
460 jme_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
462 struct jme_softc *sc = ifp->if_softc;
463 struct mii_data *mii = device_get_softc(sc->jme_miibus);
465 ASSERT_IFNET_SERIALIZED_ALL(ifp);
468 ifmr->ifm_status = mii->mii_media_status;
469 ifmr->ifm_active = mii->mii_media_active;
473 * Set hardware to newly-selected media.
476 jme_mediachange(struct ifnet *ifp)
478 struct jme_softc *sc = ifp->if_softc;
479 struct mii_data *mii = device_get_softc(sc->jme_miibus);
482 ASSERT_IFNET_SERIALIZED_ALL(ifp);
484 if (mii->mii_instance != 0) {
485 struct mii_softc *miisc;
487 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
488 mii_phy_reset(miisc);
490 error = mii_mediachg(mii);
496 jme_probe(device_t dev)
498 const struct jme_dev *sp;
501 vid = pci_get_vendor(dev);
502 did = pci_get_device(dev);
503 for (sp = jme_devs; sp->jme_name != NULL; ++sp) {
504 if (vid == sp->jme_vendorid && did == sp->jme_deviceid) {
505 struct jme_softc *sc = device_get_softc(dev);
507 sc->jme_caps = sp->jme_caps;
508 device_set_desc(dev, sp->jme_name);
516 jme_eeprom_read_byte(struct jme_softc *sc, uint8_t addr, uint8_t *val)
522 for (i = JME_TIMEOUT; i > 0; i--) {
523 reg = CSR_READ_4(sc, JME_SMBCSR);
524 if ((reg & SMBCSR_HW_BUSY_MASK) == SMBCSR_HW_IDLE)
530 device_printf(sc->jme_dev, "EEPROM idle timeout!\n");
534 reg = ((uint32_t)addr << SMBINTF_ADDR_SHIFT) & SMBINTF_ADDR_MASK;
535 CSR_WRITE_4(sc, JME_SMBINTF, reg | SMBINTF_RD | SMBINTF_CMD_TRIGGER);
536 for (i = JME_TIMEOUT; i > 0; i--) {
538 reg = CSR_READ_4(sc, JME_SMBINTF);
539 if ((reg & SMBINTF_CMD_TRIGGER) == 0)
544 device_printf(sc->jme_dev, "EEPROM read timeout!\n");
548 reg = CSR_READ_4(sc, JME_SMBINTF);
549 *val = (reg & SMBINTF_RD_DATA_MASK) >> SMBINTF_RD_DATA_SHIFT;
555 jme_eeprom_macaddr(struct jme_softc *sc, uint8_t eaddr[])
557 uint8_t fup, reg, val;
562 if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 ||
563 fup != JME_EEPROM_SIG0)
565 if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 ||
566 fup != JME_EEPROM_SIG1)
570 if (jme_eeprom_read_byte(sc, offset, &fup) != 0)
572 if (JME_EEPROM_MKDESC(JME_EEPROM_FUNC0, JME_EEPROM_PAGE_BAR1) ==
573 (fup & (JME_EEPROM_FUNC_MASK | JME_EEPROM_PAGE_MASK))) {
574 if (jme_eeprom_read_byte(sc, offset + 1, ®) != 0)
576 if (reg >= JME_PAR0 &&
577 reg < JME_PAR0 + ETHER_ADDR_LEN) {
578 if (jme_eeprom_read_byte(sc, offset + 2,
581 eaddr[reg - JME_PAR0] = val;
585 /* Check for the end of EEPROM descriptor. */
586 if ((fup & JME_EEPROM_DESC_END) == JME_EEPROM_DESC_END)
588 /* Try next eeprom descriptor. */
589 offset += JME_EEPROM_DESC_BYTES;
590 } while (match != ETHER_ADDR_LEN && offset < JME_EEPROM_END);
592 if (match == ETHER_ADDR_LEN)
599 jme_reg_macaddr(struct jme_softc *sc, uint8_t eaddr[])
603 /* Read station address. */
604 par0 = CSR_READ_4(sc, JME_PAR0);
605 par1 = CSR_READ_4(sc, JME_PAR1);
607 if ((par0 == 0 && par1 == 0) || (par0 & 0x1)) {
608 device_printf(sc->jme_dev,
609 "generating fake ethernet address.\n");
610 par0 = karc4random();
611 /* Set OUI to JMicron. */
615 eaddr[3] = (par0 >> 16) & 0xff;
616 eaddr[4] = (par0 >> 8) & 0xff;
617 eaddr[5] = par0 & 0xff;
619 eaddr[0] = (par0 >> 0) & 0xFF;
620 eaddr[1] = (par0 >> 8) & 0xFF;
621 eaddr[2] = (par0 >> 16) & 0xFF;
622 eaddr[3] = (par0 >> 24) & 0xFF;
623 eaddr[4] = (par1 >> 0) & 0xFF;
624 eaddr[5] = (par1 >> 8) & 0xFF;
629 jme_attach(device_t dev)
631 struct jme_softc *sc = device_get_softc(dev);
632 struct ifnet *ifp = &sc->arpcom.ac_if;
635 uint8_t pcie_ptr, rev;
636 int error = 0, i, j, rx_desc_cnt;
637 uint8_t eaddr[ETHER_ADDR_LEN];
639 lwkt_serialize_init(&sc->jme_serialize);
640 lwkt_serialize_init(&sc->jme_cdata.jme_tx_serialize);
641 for (i = 0; i < JME_NRXRING_MAX; ++i) {
643 &sc->jme_cdata.jme_rx_data[i].jme_rx_serialize);
646 rx_desc_cnt = device_getenv_int(dev, "rx_desc_count",
648 rx_desc_cnt = roundup(rx_desc_cnt, JME_NDESC_ALIGN);
649 if (rx_desc_cnt > JME_NDESC_MAX)
650 rx_desc_cnt = JME_NDESC_MAX;
652 sc->jme_cdata.jme_tx_desc_cnt = device_getenv_int(dev, "tx_desc_count",
654 sc->jme_cdata.jme_tx_desc_cnt = roundup(sc->jme_cdata.jme_tx_desc_cnt,
656 if (sc->jme_cdata.jme_tx_desc_cnt > JME_NDESC_MAX)
657 sc->jme_cdata.jme_tx_desc_cnt = JME_NDESC_MAX;
662 sc->jme_cdata.jme_rx_ring_cnt = device_getenv_int(dev, "rx_ring_count",
664 sc->jme_cdata.jme_rx_ring_cnt =
665 if_ring_count2(sc->jme_cdata.jme_rx_ring_cnt, JME_NRXRING_MAX);
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_cdata.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_cdata.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;
686 rdata->jme_rx_desc_cnt = rx_desc_cnt;
690 sc->jme_lowaddr = BUS_SPACE_MAXADDR;
692 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
694 callout_init(&sc->jme_tick_ch);
697 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
700 irq = pci_read_config(dev, PCIR_INTLINE, 4);
701 mem = pci_read_config(dev, JME_PCIR_BAR, 4);
703 device_printf(dev, "chip is in D%d power mode "
704 "-- setting to D0\n", pci_get_powerstate(dev));
706 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
708 pci_write_config(dev, PCIR_INTLINE, irq, 4);
709 pci_write_config(dev, JME_PCIR_BAR, mem, 4);
711 #endif /* !BURN_BRIDGE */
713 /* Enable bus mastering */
714 pci_enable_busmaster(dev);
719 * JMC250 supports both memory mapped and I/O register space
720 * access. Because I/O register access should use different
721 * BARs to access registers it's waste of time to use I/O
722 * register spce access. JMC250 uses 16K to map entire memory
725 sc->jme_mem_rid = JME_PCIR_BAR;
726 sc->jme_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
727 &sc->jme_mem_rid, RF_ACTIVE);
728 if (sc->jme_mem_res == NULL) {
729 device_printf(dev, "can't allocate IO memory\n");
732 sc->jme_mem_bt = rman_get_bustag(sc->jme_mem_res);
733 sc->jme_mem_bh = rman_get_bushandle(sc->jme_mem_res);
738 error = jme_intr_alloc(dev);
745 reg = CSR_READ_4(sc, JME_CHIPMODE);
746 if (((reg & CHIPMODE_FPGA_REV_MASK) >> CHIPMODE_FPGA_REV_SHIFT) !=
748 sc->jme_caps |= JME_CAP_FPGA;
750 device_printf(dev, "FPGA revision: 0x%04x\n",
751 (reg & CHIPMODE_FPGA_REV_MASK) >>
752 CHIPMODE_FPGA_REV_SHIFT);
756 /* NOTE: FM revision is put in the upper 4 bits */
757 rev = ((reg & CHIPMODE_REVFM_MASK) >> CHIPMODE_REVFM_SHIFT) << 4;
758 rev |= (reg & CHIPMODE_REVECO_MASK) >> CHIPMODE_REVECO_SHIFT;
760 device_printf(dev, "Revision (FM/ECO): 0x%02x\n", rev);
762 did = pci_get_device(dev);
764 case PCI_PRODUCT_JMICRON_JMC250:
765 if (rev == JME_REV1_A2)
766 sc->jme_workaround |= JME_WA_EXTFIFO | JME_WA_HDX;
769 case PCI_PRODUCT_JMICRON_JMC260:
771 sc->jme_lowaddr = BUS_SPACE_MAXADDR_32BIT;
775 panic("unknown device id 0x%04x", did);
777 if (rev >= JME_REV2) {
778 sc->jme_clksrc = GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC;
779 sc->jme_clksrc_1000 = GHC_TXOFL_CLKSRC_1000 |
780 GHC_TXMAC_CLKSRC_1000;
783 /* Reset the ethernet controller. */
786 /* Map MSI/MSI-X vectors */
789 /* Get station address. */
790 reg = CSR_READ_4(sc, JME_SMBCSR);
791 if (reg & SMBCSR_EEPROM_PRESENT)
792 error = jme_eeprom_macaddr(sc, eaddr);
793 if (error != 0 || (reg & SMBCSR_EEPROM_PRESENT) == 0) {
794 if (error != 0 && (bootverbose)) {
795 device_printf(dev, "ethernet hardware address "
796 "not found in EEPROM.\n");
798 jme_reg_macaddr(sc, eaddr);
803 * Integrated JR0211 has fixed PHY address whereas FPGA version
804 * requires PHY probing to get correct PHY address.
806 if ((sc->jme_caps & JME_CAP_FPGA) == 0) {
807 sc->jme_phyaddr = CSR_READ_4(sc, JME_GPREG0) &
808 GPREG0_PHY_ADDR_MASK;
810 device_printf(dev, "PHY is at address %d.\n",
817 /* Set max allowable DMA size. */
818 pcie_ptr = pci_get_pciecap_ptr(dev);
822 sc->jme_caps |= JME_CAP_PCIE;
823 ctrl = pci_read_config(dev, pcie_ptr + PCIER_DEVCTRL, 2);
825 device_printf(dev, "Read request size : %d bytes.\n",
826 128 << ((ctrl >> 12) & 0x07));
827 device_printf(dev, "TLP payload size : %d bytes.\n",
828 128 << ((ctrl >> 5) & 0x07));
830 switch (ctrl & PCIEM_DEVCTL_MAX_READRQ_MASK) {
831 case PCIEM_DEVCTL_MAX_READRQ_128:
832 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_128;
834 case PCIEM_DEVCTL_MAX_READRQ_256:
835 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_256;
838 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_512;
841 sc->jme_rx_dma_size = RXCSR_DMA_SIZE_128;
843 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_512;
844 sc->jme_rx_dma_size = RXCSR_DMA_SIZE_128;
848 if (pci_find_extcap(dev, PCIY_PMG, &pmc) == 0)
849 sc->jme_caps |= JME_CAP_PMCAP;
857 /* Allocate DMA stuffs */
858 error = jme_dma_alloc(sc);
863 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
864 ifp->if_init = jme_init;
865 ifp->if_ioctl = jme_ioctl;
866 ifp->if_start = jme_start;
867 #ifdef DEVICE_POLLING
868 ifp->if_poll = jme_poll;
870 ifp->if_watchdog = jme_watchdog;
871 ifp->if_serialize = jme_serialize;
872 ifp->if_deserialize = jme_deserialize;
873 ifp->if_tryserialize = jme_tryserialize;
875 ifp->if_serialize_assert = jme_serialize_assert;
877 ifq_set_maxlen(&ifp->if_snd,
878 sc->jme_cdata.jme_tx_desc_cnt - JME_TXD_RSVD);
879 ifq_set_ready(&ifp->if_snd);
881 /* JMC250 supports Tx/Rx checksum offload and hardware vlan tagging. */
882 ifp->if_capabilities = IFCAP_HWCSUM |
884 IFCAP_VLAN_HWTAGGING;
885 if (sc->jme_cdata.jme_rx_ring_cnt > JME_NRXRING_MIN)
886 ifp->if_capabilities |= IFCAP_RSS;
887 ifp->if_capenable = ifp->if_capabilities;
890 * Disable TXCSUM by default to improve bulk data
891 * transmit performance (+20Mbps improvement).
893 ifp->if_capenable &= ~IFCAP_TXCSUM;
895 if (ifp->if_capenable & IFCAP_TXCSUM)
896 ifp->if_hwassist = JME_CSUM_FEATURES;
898 /* Set up MII bus. */
899 error = mii_phy_probe(dev, &sc->jme_miibus,
900 jme_mediachange, jme_mediastatus);
902 device_printf(dev, "no PHY found!\n");
907 * Save PHYADDR for FPGA mode PHY.
909 if (sc->jme_caps & JME_CAP_FPGA) {
910 struct mii_data *mii = device_get_softc(sc->jme_miibus);
912 if (mii->mii_instance != 0) {
913 struct mii_softc *miisc;
915 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
916 if (miisc->mii_phy != 0) {
917 sc->jme_phyaddr = miisc->mii_phy;
921 if (sc->jme_phyaddr != 0) {
922 device_printf(sc->jme_dev,
923 "FPGA PHY is at %d\n", sc->jme_phyaddr);
925 jme_miibus_writereg(dev, sc->jme_phyaddr,
926 JMPHY_CONF, JMPHY_CONF_DEFFIFO);
928 /* XXX should we clear JME_WA_EXTFIFO */
933 ether_ifattach(ifp, eaddr, NULL);
935 /* Tell the upper layer(s) we support long frames. */
936 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
938 error = jme_intr_setup(dev);
951 jme_detach(device_t dev)
953 struct jme_softc *sc = device_get_softc(dev);
955 if (device_is_attached(dev)) {
956 struct ifnet *ifp = &sc->arpcom.ac_if;
958 ifnet_serialize_all(ifp);
960 jme_intr_teardown(dev);
961 ifnet_deserialize_all(ifp);
966 if (sc->jme_sysctl_tree != NULL)
967 sysctl_ctx_free(&sc->jme_sysctl_ctx);
969 if (sc->jme_miibus != NULL)
970 device_delete_child(dev, sc->jme_miibus);
971 bus_generic_detach(dev);
975 if (sc->jme_mem_res != NULL) {
976 bus_release_resource(dev, SYS_RES_MEMORY, sc->jme_mem_rid,
986 jme_sysctl_node(struct jme_softc *sc)
993 sysctl_ctx_init(&sc->jme_sysctl_ctx);
994 sc->jme_sysctl_tree = SYSCTL_ADD_NODE(&sc->jme_sysctl_ctx,
995 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
996 device_get_nameunit(sc->jme_dev),
998 if (sc->jme_sysctl_tree == NULL) {
999 device_printf(sc->jme_dev, "can't add sysctl node\n");
1003 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1004 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1005 "tx_coal_to", CTLTYPE_INT | CTLFLAG_RW,
1006 sc, 0, jme_sysctl_tx_coal_to, "I", "jme tx coalescing timeout");
1008 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1009 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1010 "tx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW,
1011 sc, 0, jme_sysctl_tx_coal_pkt, "I", "jme tx coalescing packet");
1013 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1014 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1015 "rx_coal_to", CTLTYPE_INT | CTLFLAG_RW,
1016 sc, 0, jme_sysctl_rx_coal_to, "I", "jme rx coalescing timeout");
1018 SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
1019 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1020 "rx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW,
1021 sc, 0, jme_sysctl_rx_coal_pkt, "I", "jme rx coalescing packet");
1023 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1024 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1025 "rx_desc_count", CTLFLAG_RD,
1026 &sc->jme_cdata.jme_rx_data[0].jme_rx_desc_cnt,
1027 0, "RX desc count");
1028 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1029 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1030 "tx_desc_count", CTLFLAG_RD,
1031 &sc->jme_cdata.jme_tx_desc_cnt,
1032 0, "TX desc count");
1033 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1034 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1035 "rx_ring_count", CTLFLAG_RD,
1036 &sc->jme_cdata.jme_rx_ring_cnt,
1037 0, "RX ring count");
1038 #ifdef JME_RSS_DEBUG
1039 SYSCTL_ADD_INT(&sc->jme_sysctl_ctx,
1040 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1041 "rss_debug", CTLFLAG_RW, &sc->jme_rss_debug,
1042 0, "RSS debug level");
1043 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1044 char rx_ring_pkt[32];
1046 ksnprintf(rx_ring_pkt, sizeof(rx_ring_pkt), "rx_ring%d_pkt", r);
1047 SYSCTL_ADD_ULONG(&sc->jme_sysctl_ctx,
1048 SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
1049 rx_ring_pkt, CTLFLAG_RW,
1050 &sc->jme_cdata.jme_rx_data[r].jme_rx_pkt, "RXed packets");
1055 * Set default coalesce valves
1057 sc->jme_tx_coal_to = PCCTX_COAL_TO_DEFAULT;
1058 sc->jme_tx_coal_pkt = PCCTX_COAL_PKT_DEFAULT;
1059 sc->jme_rx_coal_to = PCCRX_COAL_TO_DEFAULT;
1060 sc->jme_rx_coal_pkt = PCCRX_COAL_PKT_DEFAULT;
1063 * Adjust coalesce valves, in case that the number of TX/RX
1064 * descs are set to small values by users.
1066 * NOTE: coal_max will not be zero, since number of descs
1067 * must aligned by JME_NDESC_ALIGN (16 currently)
1069 coal_max = sc->jme_cdata.jme_tx_desc_cnt / 6;
1070 if (coal_max < sc->jme_tx_coal_pkt)
1071 sc->jme_tx_coal_pkt = coal_max;
1073 coal_max = sc->jme_cdata.jme_rx_data[0].jme_rx_desc_cnt / 4;
1074 if (coal_max < sc->jme_rx_coal_pkt)
1075 sc->jme_rx_coal_pkt = coal_max;
1079 jme_dma_alloc(struct jme_softc *sc)
1081 struct jme_txdesc *txd;
1083 int error, i, asize;
1085 sc->jme_cdata.jme_txdesc =
1086 kmalloc(sc->jme_cdata.jme_tx_desc_cnt * sizeof(struct jme_txdesc),
1087 M_DEVBUF, M_WAITOK | M_ZERO);
1088 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
1089 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[i];
1092 kmalloc(rdata->jme_rx_desc_cnt * sizeof(struct jme_rxdesc),
1093 M_DEVBUF, M_WAITOK | M_ZERO);
1096 /* Create parent ring tag. */
1097 error = bus_dma_tag_create(NULL,/* parent */
1098 1, JME_RING_BOUNDARY, /* algnmnt, boundary */
1099 sc->jme_lowaddr, /* lowaddr */
1100 BUS_SPACE_MAXADDR, /* highaddr */
1101 NULL, NULL, /* filter, filterarg */
1102 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1104 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1106 &sc->jme_cdata.jme_ring_tag);
1108 device_printf(sc->jme_dev,
1109 "could not create parent ring DMA tag.\n");
1114 * Create DMA stuffs for TX ring
1116 asize = roundup2(JME_TX_RING_SIZE(sc), JME_TX_RING_ALIGN);
1117 error = bus_dmamem_coherent(sc->jme_cdata.jme_ring_tag,
1118 JME_TX_RING_ALIGN, 0,
1119 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1120 asize, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
1122 device_printf(sc->jme_dev, "could not allocate Tx ring.\n");
1125 sc->jme_cdata.jme_tx_ring_tag = dmem.dmem_tag;
1126 sc->jme_cdata.jme_tx_ring_map = dmem.dmem_map;
1127 sc->jme_cdata.jme_tx_ring = dmem.dmem_addr;
1128 sc->jme_cdata.jme_tx_ring_paddr = dmem.dmem_busaddr;
1131 * Create DMA stuffs for RX rings
1133 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
1134 error = jme_rxring_dma_alloc(&sc->jme_cdata.jme_rx_data[i]);
1139 /* Create parent buffer tag. */
1140 error = bus_dma_tag_create(NULL,/* parent */
1141 1, 0, /* algnmnt, boundary */
1142 sc->jme_lowaddr, /* lowaddr */
1143 BUS_SPACE_MAXADDR, /* highaddr */
1144 NULL, NULL, /* filter, filterarg */
1145 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1147 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1149 &sc->jme_cdata.jme_buffer_tag);
1151 device_printf(sc->jme_dev,
1152 "could not create parent buffer DMA tag.\n");
1157 * Create DMA stuffs for shadow status block
1159 asize = roundup2(JME_SSB_SIZE, JME_SSB_ALIGN);
1160 error = bus_dmamem_coherent(sc->jme_cdata.jme_buffer_tag,
1161 JME_SSB_ALIGN, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1162 asize, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
1164 device_printf(sc->jme_dev,
1165 "could not create shadow status block.\n");
1168 sc->jme_cdata.jme_ssb_tag = dmem.dmem_tag;
1169 sc->jme_cdata.jme_ssb_map = dmem.dmem_map;
1170 sc->jme_cdata.jme_ssb_block = dmem.dmem_addr;
1171 sc->jme_cdata.jme_ssb_block_paddr = dmem.dmem_busaddr;
1174 * Create DMA stuffs for TX buffers
1177 /* Create tag for Tx buffers. */
1178 error = bus_dma_tag_create(sc->jme_cdata.jme_buffer_tag,/* parent */
1179 1, 0, /* algnmnt, boundary */
1180 BUS_SPACE_MAXADDR, /* lowaddr */
1181 BUS_SPACE_MAXADDR, /* highaddr */
1182 NULL, NULL, /* filter, filterarg */
1183 JME_JUMBO_FRAMELEN, /* maxsize */
1184 JME_MAXTXSEGS, /* nsegments */
1185 JME_MAXSEGSIZE, /* maxsegsize */
1186 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,/* flags */
1187 &sc->jme_cdata.jme_tx_tag);
1189 device_printf(sc->jme_dev, "could not create Tx DMA tag.\n");
1193 /* Create DMA maps for Tx buffers. */
1194 for (i = 0; i < sc->jme_cdata.jme_tx_desc_cnt; i++) {
1195 txd = &sc->jme_cdata.jme_txdesc[i];
1196 error = bus_dmamap_create(sc->jme_cdata.jme_tx_tag,
1197 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
1202 device_printf(sc->jme_dev,
1203 "could not create %dth Tx dmamap.\n", i);
1205 for (j = 0; j < i; ++j) {
1206 txd = &sc->jme_cdata.jme_txdesc[j];
1207 bus_dmamap_destroy(sc->jme_cdata.jme_tx_tag,
1210 bus_dma_tag_destroy(sc->jme_cdata.jme_tx_tag);
1211 sc->jme_cdata.jme_tx_tag = NULL;
1217 * Create DMA stuffs for RX buffers
1219 for (i = 0; i < sc->jme_cdata.jme_rx_ring_cnt; ++i) {
1220 error = jme_rxbuf_dma_alloc(&sc->jme_cdata.jme_rx_data[i]);
1228 jme_dma_free(struct jme_softc *sc)
1230 struct jme_txdesc *txd;
1231 struct jme_rxdesc *rxd;
1232 struct jme_rxdata *rdata;
1236 if (sc->jme_cdata.jme_tx_ring_tag != NULL) {
1237 bus_dmamap_unload(sc->jme_cdata.jme_tx_ring_tag,
1238 sc->jme_cdata.jme_tx_ring_map);
1239 bus_dmamem_free(sc->jme_cdata.jme_tx_ring_tag,
1240 sc->jme_cdata.jme_tx_ring,
1241 sc->jme_cdata.jme_tx_ring_map);
1242 bus_dma_tag_destroy(sc->jme_cdata.jme_tx_ring_tag);
1243 sc->jme_cdata.jme_tx_ring_tag = NULL;
1247 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1248 rdata = &sc->jme_cdata.jme_rx_data[r];
1249 if (rdata->jme_rx_ring_tag != NULL) {
1250 bus_dmamap_unload(rdata->jme_rx_ring_tag,
1251 rdata->jme_rx_ring_map);
1252 bus_dmamem_free(rdata->jme_rx_ring_tag,
1254 rdata->jme_rx_ring_map);
1255 bus_dma_tag_destroy(rdata->jme_rx_ring_tag);
1256 rdata->jme_rx_ring_tag = NULL;
1261 if (sc->jme_cdata.jme_tx_tag != NULL) {
1262 for (i = 0; i < sc->jme_cdata.jme_tx_desc_cnt; i++) {
1263 txd = &sc->jme_cdata.jme_txdesc[i];
1264 bus_dmamap_destroy(sc->jme_cdata.jme_tx_tag,
1267 bus_dma_tag_destroy(sc->jme_cdata.jme_tx_tag);
1268 sc->jme_cdata.jme_tx_tag = NULL;
1272 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1273 rdata = &sc->jme_cdata.jme_rx_data[r];
1274 if (rdata->jme_rx_tag != NULL) {
1275 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
1276 rxd = &rdata->jme_rxdesc[i];
1277 bus_dmamap_destroy(rdata->jme_rx_tag,
1280 bus_dmamap_destroy(rdata->jme_rx_tag,
1281 rdata->jme_rx_sparemap);
1282 bus_dma_tag_destroy(rdata->jme_rx_tag);
1283 rdata->jme_rx_tag = NULL;
1287 /* Shadow status block. */
1288 if (sc->jme_cdata.jme_ssb_tag != NULL) {
1289 bus_dmamap_unload(sc->jme_cdata.jme_ssb_tag,
1290 sc->jme_cdata.jme_ssb_map);
1291 bus_dmamem_free(sc->jme_cdata.jme_ssb_tag,
1292 sc->jme_cdata.jme_ssb_block,
1293 sc->jme_cdata.jme_ssb_map);
1294 bus_dma_tag_destroy(sc->jme_cdata.jme_ssb_tag);
1295 sc->jme_cdata.jme_ssb_tag = NULL;
1298 if (sc->jme_cdata.jme_buffer_tag != NULL) {
1299 bus_dma_tag_destroy(sc->jme_cdata.jme_buffer_tag);
1300 sc->jme_cdata.jme_buffer_tag = NULL;
1302 if (sc->jme_cdata.jme_ring_tag != NULL) {
1303 bus_dma_tag_destroy(sc->jme_cdata.jme_ring_tag);
1304 sc->jme_cdata.jme_ring_tag = NULL;
1307 if (sc->jme_cdata.jme_txdesc != NULL) {
1308 kfree(sc->jme_cdata.jme_txdesc, M_DEVBUF);
1309 sc->jme_cdata.jme_txdesc = NULL;
1311 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1312 rdata = &sc->jme_cdata.jme_rx_data[r];
1313 if (rdata->jme_rxdesc != NULL) {
1314 kfree(rdata->jme_rxdesc, M_DEVBUF);
1315 rdata->jme_rxdesc = NULL;
1321 * Make sure the interface is stopped at reboot time.
1324 jme_shutdown(device_t dev)
1326 return jme_suspend(dev);
1331 * Unlike other ethernet controllers, JMC250 requires
1332 * explicit resetting link speed to 10/100Mbps as gigabit
1333 * link will cunsume more power than 375mA.
1334 * Note, we reset the link speed to 10/100Mbps with
1335 * auto-negotiation but we don't know whether that operation
1336 * would succeed or not as we have no control after powering
1337 * off. If the renegotiation fail WOL may not work. Running
1338 * at 1Gbps draws more power than 375mA at 3.3V which is
1339 * specified in PCI specification and that would result in
1340 * complete shutdowning power to ethernet controller.
1343 * Save current negotiated media speed/duplex/flow-control
1344 * to softc and restore the same link again after resuming.
1345 * PHY handling such as power down/resetting to 100Mbps
1346 * may be better handled in suspend method in phy driver.
1349 jme_setlinkspeed(struct jme_softc *sc)
1351 struct mii_data *mii;
1354 JME_LOCK_ASSERT(sc);
1356 mii = device_get_softc(sc->jme_miibus);
1359 if ((mii->mii_media_status & IFM_AVALID) != 0) {
1360 switch IFM_SUBTYPE(mii->mii_media_active) {
1370 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_100T2CR, 0);
1371 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_ANAR,
1372 ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
1373 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_BMCR,
1374 BMCR_AUTOEN | BMCR_STARTNEG);
1377 /* Poll link state until jme(4) get a 10/100 link. */
1378 for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
1380 if ((mii->mii_media_status & IFM_AVALID) != 0) {
1381 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1391 pause("jmelnk", hz);
1394 if (i == MII_ANEGTICKS_GIGE)
1395 device_printf(sc->jme_dev, "establishing link failed, "
1396 "WOL may not work!");
1399 * No link, force MAC to have 100Mbps, full-duplex link.
1400 * This is the last resort and may/may not work.
1402 mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
1403 mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1408 jme_setwol(struct jme_softc *sc)
1410 struct ifnet *ifp = &sc->arpcom.ac_if;
1415 if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1416 /* No PME capability, PHY power down. */
1417 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1418 MII_BMCR, BMCR_PDOWN);
1422 gpr = CSR_READ_4(sc, JME_GPREG0) & ~GPREG0_PME_ENB;
1423 pmcs = CSR_READ_4(sc, JME_PMCS);
1424 pmcs &= ~PMCS_WOL_ENB_MASK;
1425 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) {
1426 pmcs |= PMCS_MAGIC_FRAME | PMCS_MAGIC_FRAME_ENB;
1427 /* Enable PME message. */
1428 gpr |= GPREG0_PME_ENB;
1429 /* For gigabit controllers, reset link speed to 10/100. */
1430 if ((sc->jme_caps & JME_CAP_FASTETH) == 0)
1431 jme_setlinkspeed(sc);
1434 CSR_WRITE_4(sc, JME_PMCS, pmcs);
1435 CSR_WRITE_4(sc, JME_GPREG0, gpr);
1438 pmstat = pci_read_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, 2);
1439 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
1440 if ((ifp->if_capenable & IFCAP_WOL) != 0)
1441 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
1442 pci_write_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1443 if ((ifp->if_capenable & IFCAP_WOL) == 0) {
1444 /* No WOL, PHY power down. */
1445 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1446 MII_BMCR, BMCR_PDOWN);
1452 jme_suspend(device_t dev)
1454 struct jme_softc *sc = device_get_softc(dev);
1455 struct ifnet *ifp = &sc->arpcom.ac_if;
1457 ifnet_serialize_all(ifp);
1462 ifnet_deserialize_all(ifp);
1468 jme_resume(device_t dev)
1470 struct jme_softc *sc = device_get_softc(dev);
1471 struct ifnet *ifp = &sc->arpcom.ac_if;
1476 ifnet_serialize_all(ifp);
1479 if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1482 pmstat = pci_read_config(sc->jme_dev,
1483 pmc + PCIR_POWER_STATUS, 2);
1484 /* Disable PME clear PME status. */
1485 pmstat &= ~PCIM_PSTAT_PMEENABLE;
1486 pci_write_config(sc->jme_dev,
1487 pmc + PCIR_POWER_STATUS, pmstat, 2);
1491 if (ifp->if_flags & IFF_UP)
1494 ifnet_deserialize_all(ifp);
1500 jme_encap(struct jme_softc *sc, struct mbuf **m_head)
1502 struct jme_txdesc *txd;
1503 struct jme_desc *desc;
1505 bus_dma_segment_t txsegs[JME_MAXTXSEGS];
1507 int error, i, prod, symbol_desc;
1508 uint32_t cflags, flag64;
1510 M_ASSERTPKTHDR((*m_head));
1512 prod = sc->jme_cdata.jme_tx_prod;
1513 txd = &sc->jme_cdata.jme_txdesc[prod];
1515 if (sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT)
1520 maxsegs = (sc->jme_cdata.jme_tx_desc_cnt - sc->jme_cdata.jme_tx_cnt) -
1521 (JME_TXD_RSVD + symbol_desc);
1522 if (maxsegs > JME_MAXTXSEGS)
1523 maxsegs = JME_MAXTXSEGS;
1524 KASSERT(maxsegs >= (sc->jme_txd_spare - symbol_desc),
1525 ("not enough segments %d", maxsegs));
1527 error = bus_dmamap_load_mbuf_defrag(sc->jme_cdata.jme_tx_tag,
1528 txd->tx_dmamap, m_head,
1529 txsegs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
1533 bus_dmamap_sync(sc->jme_cdata.jme_tx_tag, txd->tx_dmamap,
1534 BUS_DMASYNC_PREWRITE);
1539 /* Configure checksum offload. */
1540 if (m->m_pkthdr.csum_flags & CSUM_IP)
1541 cflags |= JME_TD_IPCSUM;
1542 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1543 cflags |= JME_TD_TCPCSUM;
1544 if (m->m_pkthdr.csum_flags & CSUM_UDP)
1545 cflags |= JME_TD_UDPCSUM;
1547 /* Configure VLAN. */
1548 if (m->m_flags & M_VLANTAG) {
1549 cflags |= (m->m_pkthdr.ether_vlantag & JME_TD_VLAN_MASK);
1550 cflags |= JME_TD_VLAN_TAG;
1553 desc = &sc->jme_cdata.jme_tx_ring[prod];
1554 desc->flags = htole32(cflags);
1555 desc->addr_hi = htole32(m->m_pkthdr.len);
1556 if (sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT) {
1558 * Use 64bits TX desc chain format.
1560 * The first TX desc of the chain, which is setup here,
1561 * is just a symbol TX desc carrying no payload.
1563 flag64 = JME_TD_64BIT;
1567 /* No effective TX desc is consumed */
1571 * Use 32bits TX desc chain format.
1573 * The first TX desc of the chain, which is setup here,
1574 * is an effective TX desc carrying the first segment of
1578 desc->buflen = htole32(txsegs[0].ds_len);
1579 desc->addr_lo = htole32(JME_ADDR_LO(txsegs[0].ds_addr));
1581 /* One effective TX desc is consumed */
1584 sc->jme_cdata.jme_tx_cnt++;
1585 KKASSERT(sc->jme_cdata.jme_tx_cnt - i <
1586 sc->jme_cdata.jme_tx_desc_cnt - JME_TXD_RSVD);
1587 JME_DESC_INC(prod, sc->jme_cdata.jme_tx_desc_cnt);
1589 txd->tx_ndesc = 1 - i;
1590 for (; i < nsegs; i++) {
1591 desc = &sc->jme_cdata.jme_tx_ring[prod];
1592 desc->flags = htole32(JME_TD_OWN | flag64);
1593 desc->buflen = htole32(txsegs[i].ds_len);
1594 desc->addr_hi = htole32(JME_ADDR_HI(txsegs[i].ds_addr));
1595 desc->addr_lo = htole32(JME_ADDR_LO(txsegs[i].ds_addr));
1597 sc->jme_cdata.jme_tx_cnt++;
1598 KKASSERT(sc->jme_cdata.jme_tx_cnt <=
1599 sc->jme_cdata.jme_tx_desc_cnt - JME_TXD_RSVD);
1600 JME_DESC_INC(prod, sc->jme_cdata.jme_tx_desc_cnt);
1603 /* Update producer index. */
1604 sc->jme_cdata.jme_tx_prod = prod;
1606 * Finally request interrupt and give the first descriptor
1607 * owenership to hardware.
1609 desc = txd->tx_desc;
1610 desc->flags |= htole32(JME_TD_OWN | JME_TD_INTR);
1613 txd->tx_ndesc += nsegs;
1623 jme_start(struct ifnet *ifp)
1625 struct jme_softc *sc = ifp->if_softc;
1626 struct mbuf *m_head;
1629 ASSERT_SERIALIZED(&sc->jme_cdata.jme_tx_serialize);
1631 if ((sc->jme_flags & JME_FLAG_LINK) == 0) {
1632 ifq_purge(&ifp->if_snd);
1636 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1639 if (sc->jme_cdata.jme_tx_cnt >= JME_TX_DESC_HIWAT(sc))
1642 while (!ifq_is_empty(&ifp->if_snd)) {
1644 * Check number of available TX descs, always
1645 * leave JME_TXD_RSVD free TX descs.
1647 if (sc->jme_cdata.jme_tx_cnt + sc->jme_txd_spare >
1648 sc->jme_cdata.jme_tx_desc_cnt - JME_TXD_RSVD) {
1649 ifp->if_flags |= IFF_OACTIVE;
1653 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1658 * Pack the data into the transmit ring. If we
1659 * don't have room, set the OACTIVE flag and wait
1660 * for the NIC to drain the ring.
1662 if (jme_encap(sc, &m_head)) {
1663 KKASSERT(m_head == NULL);
1665 ifp->if_flags |= IFF_OACTIVE;
1671 * If there's a BPF listener, bounce a copy of this frame
1674 ETHER_BPF_MTAP(ifp, m_head);
1679 * Reading TXCSR takes very long time under heavy load
1680 * so cache TXCSR value and writes the ORed value with
1681 * the kick command to the TXCSR. This saves one register
1684 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr | TXCSR_TX_ENB |
1685 TXCSR_TXQ_N_START(TXCSR_TXQ0));
1686 /* Set a timeout in case the chip goes out to lunch. */
1687 ifp->if_timer = JME_TX_TIMEOUT;
1692 jme_watchdog(struct ifnet *ifp)
1694 struct jme_softc *sc = ifp->if_softc;
1696 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1698 if ((sc->jme_flags & JME_FLAG_LINK) == 0) {
1699 if_printf(ifp, "watchdog timeout (missed link)\n");
1706 if (sc->jme_cdata.jme_tx_cnt == 0) {
1707 if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
1709 if (!ifq_is_empty(&ifp->if_snd))
1714 if_printf(ifp, "watchdog timeout\n");
1717 if (!ifq_is_empty(&ifp->if_snd))
1722 jme_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1724 struct jme_softc *sc = ifp->if_softc;
1725 struct mii_data *mii = device_get_softc(sc->jme_miibus);
1726 struct ifreq *ifr = (struct ifreq *)data;
1727 int error = 0, mask;
1729 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1733 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > JME_JUMBO_MTU ||
1734 (!(sc->jme_caps & JME_CAP_JUMBO) &&
1735 ifr->ifr_mtu > JME_MAX_MTU)) {
1740 if (ifp->if_mtu != ifr->ifr_mtu) {
1742 * No special configuration is required when interface
1743 * MTU is changed but availability of Tx checksum
1744 * offload should be chcked against new MTU size as
1745 * FIFO size is just 2K.
1747 if (ifr->ifr_mtu >= JME_TX_FIFO_SIZE) {
1748 ifp->if_capenable &= ~IFCAP_TXCSUM;
1749 ifp->if_hwassist &= ~JME_CSUM_FEATURES;
1751 ifp->if_mtu = ifr->ifr_mtu;
1752 if (ifp->if_flags & IFF_RUNNING)
1758 if (ifp->if_flags & IFF_UP) {
1759 if (ifp->if_flags & IFF_RUNNING) {
1760 if ((ifp->if_flags ^ sc->jme_if_flags) &
1761 (IFF_PROMISC | IFF_ALLMULTI))
1767 if (ifp->if_flags & IFF_RUNNING)
1770 sc->jme_if_flags = ifp->if_flags;
1775 if (ifp->if_flags & IFF_RUNNING)
1781 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1785 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1787 if ((mask & IFCAP_TXCSUM) && ifp->if_mtu < JME_TX_FIFO_SIZE) {
1788 ifp->if_capenable ^= IFCAP_TXCSUM;
1789 if (IFCAP_TXCSUM & ifp->if_capenable)
1790 ifp->if_hwassist |= JME_CSUM_FEATURES;
1792 ifp->if_hwassist &= ~JME_CSUM_FEATURES;
1794 if (mask & IFCAP_RXCSUM) {
1797 ifp->if_capenable ^= IFCAP_RXCSUM;
1798 reg = CSR_READ_4(sc, JME_RXMAC);
1799 reg &= ~RXMAC_CSUM_ENB;
1800 if (ifp->if_capenable & IFCAP_RXCSUM)
1801 reg |= RXMAC_CSUM_ENB;
1802 CSR_WRITE_4(sc, JME_RXMAC, reg);
1805 if (mask & IFCAP_VLAN_HWTAGGING) {
1806 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1810 if (mask & IFCAP_RSS)
1811 ifp->if_capenable ^= IFCAP_RSS;
1815 error = ether_ioctl(ifp, cmd, data);
1822 jme_mac_config(struct jme_softc *sc)
1824 struct mii_data *mii;
1825 uint32_t ghc, rxmac, txmac, txpause, gp1;
1826 int phyconf = JMPHY_CONF_DEFFIFO, hdx = 0;
1828 mii = device_get_softc(sc->jme_miibus);
1830 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
1832 CSR_WRITE_4(sc, JME_GHC, 0);
1834 rxmac = CSR_READ_4(sc, JME_RXMAC);
1835 rxmac &= ~RXMAC_FC_ENB;
1836 txmac = CSR_READ_4(sc, JME_TXMAC);
1837 txmac &= ~(TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST);
1838 txpause = CSR_READ_4(sc, JME_TXPFC);
1839 txpause &= ~TXPFC_PAUSE_ENB;
1840 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1841 ghc |= GHC_FULL_DUPLEX;
1842 rxmac &= ~RXMAC_COLL_DET_ENB;
1843 txmac &= ~(TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE |
1844 TXMAC_BACKOFF | TXMAC_CARRIER_EXT |
1847 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1848 txpause |= TXPFC_PAUSE_ENB;
1849 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1850 rxmac |= RXMAC_FC_ENB;
1852 /* Disable retry transmit timer/retry limit. */
1853 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) &
1854 ~(TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB));
1856 rxmac |= RXMAC_COLL_DET_ENB;
1857 txmac |= TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE | TXMAC_BACKOFF;
1858 /* Enable retry transmit timer/retry limit. */
1859 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) |
1860 TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB);
1864 * Reprogram Tx/Rx MACs with resolved speed/duplex.
1866 gp1 = CSR_READ_4(sc, JME_GPREG1);
1867 gp1 &= ~GPREG1_WA_HDX;
1869 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) == 0)
1872 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1874 ghc |= GHC_SPEED_10 | sc->jme_clksrc;
1876 gp1 |= GPREG1_WA_HDX;
1880 ghc |= GHC_SPEED_100 | sc->jme_clksrc;
1882 gp1 |= GPREG1_WA_HDX;
1885 * Use extended FIFO depth to workaround CRC errors
1886 * emitted by chips before JMC250B
1888 phyconf = JMPHY_CONF_EXTFIFO;
1892 if (sc->jme_caps & JME_CAP_FASTETH)
1895 ghc |= GHC_SPEED_1000 | sc->jme_clksrc_1000;
1897 txmac |= TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST;
1903 CSR_WRITE_4(sc, JME_GHC, ghc);
1904 CSR_WRITE_4(sc, JME_RXMAC, rxmac);
1905 CSR_WRITE_4(sc, JME_TXMAC, txmac);
1906 CSR_WRITE_4(sc, JME_TXPFC, txpause);
1908 if (sc->jme_workaround & JME_WA_EXTFIFO) {
1909 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1910 JMPHY_CONF, phyconf);
1912 if (sc->jme_workaround & JME_WA_HDX)
1913 CSR_WRITE_4(sc, JME_GPREG1, gp1);
1919 struct jme_softc *sc = xsc;
1920 struct ifnet *ifp = &sc->arpcom.ac_if;
1924 ASSERT_SERIALIZED(&sc->jme_serialize);
1926 status = CSR_READ_4(sc, JME_INTR_REQ_STATUS);
1927 if (status == 0 || status == 0xFFFFFFFF)
1930 /* Disable interrupts. */
1931 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
1933 status = CSR_READ_4(sc, JME_INTR_STATUS);
1934 if ((status & JME_INTRS) == 0 || status == 0xFFFFFFFF)
1937 /* Reset PCC counter/timer and Ack interrupts. */
1938 status &= ~(INTR_TXQ_COMP | INTR_RXQ_COMP);
1940 if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO))
1941 status |= INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP;
1943 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
1944 if (status & jme_rx_status[r].jme_coal) {
1945 status |= jme_rx_status[r].jme_coal |
1946 jme_rx_status[r].jme_comp;
1950 CSR_WRITE_4(sc, JME_INTR_STATUS, status);
1952 if (ifp->if_flags & IFF_RUNNING) {
1953 if (status & (INTR_RXQ_COAL | INTR_RXQ_COAL_TO))
1954 jme_rx_intr(sc, status);
1956 if (status & INTR_RXQ_DESC_EMPTY) {
1958 * Notify hardware availability of new Rx buffers.
1959 * Reading RXCSR takes very long time under heavy
1960 * load so cache RXCSR value and writes the ORed
1961 * value with the kick command to the RXCSR. This
1962 * saves one register access cycle.
1964 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr |
1965 RXCSR_RX_ENB | RXCSR_RXQ_START);
1968 if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO)) {
1969 lwkt_serialize_enter(&sc->jme_cdata.jme_tx_serialize);
1971 if (!ifq_is_empty(&ifp->if_snd))
1973 lwkt_serialize_exit(&sc->jme_cdata.jme_tx_serialize);
1977 /* Reenable interrupts. */
1978 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
1982 jme_txeof(struct jme_softc *sc)
1984 struct ifnet *ifp = &sc->arpcom.ac_if;
1985 struct jme_txdesc *txd;
1989 cons = sc->jme_cdata.jme_tx_cons;
1990 if (cons == sc->jme_cdata.jme_tx_prod)
1994 * Go through our Tx list and free mbufs for those
1995 * frames which have been transmitted.
1997 while (cons != sc->jme_cdata.jme_tx_prod) {
1998 txd = &sc->jme_cdata.jme_txdesc[cons];
1999 KASSERT(txd->tx_m != NULL,
2000 ("%s: freeing NULL mbuf!", __func__));
2002 status = le32toh(txd->tx_desc->flags);
2003 if ((status & JME_TD_OWN) == JME_TD_OWN)
2006 if (status & (JME_TD_TMOUT | JME_TD_RETRY_EXP)) {
2010 if (status & JME_TD_COLLISION) {
2011 ifp->if_collisions +=
2012 le32toh(txd->tx_desc->buflen) &
2013 JME_TD_BUF_LEN_MASK;
2018 * Only the first descriptor of multi-descriptor
2019 * transmission is updated so driver have to skip entire
2020 * chained buffers for the transmiited frame. In other
2021 * words, JME_TD_OWN bit is valid only at the first
2022 * descriptor of a multi-descriptor transmission.
2024 for (nsegs = 0; nsegs < txd->tx_ndesc; nsegs++) {
2025 sc->jme_cdata.jme_tx_ring[cons].flags = 0;
2026 JME_DESC_INC(cons, sc->jme_cdata.jme_tx_desc_cnt);
2029 /* Reclaim transferred mbufs. */
2030 bus_dmamap_unload(sc->jme_cdata.jme_tx_tag, txd->tx_dmamap);
2033 sc->jme_cdata.jme_tx_cnt -= txd->tx_ndesc;
2034 KASSERT(sc->jme_cdata.jme_tx_cnt >= 0,
2035 ("%s: Active Tx desc counter was garbled", __func__));
2038 sc->jme_cdata.jme_tx_cons = cons;
2040 if (sc->jme_cdata.jme_tx_cnt == 0)
2043 if (sc->jme_cdata.jme_tx_cnt + sc->jme_txd_spare <=
2044 sc->jme_cdata.jme_tx_desc_cnt - JME_TXD_RSVD)
2045 ifp->if_flags &= ~IFF_OACTIVE;
2048 static __inline void
2049 jme_discard_rxbufs(struct jme_rxdata *rdata, int cons, int count)
2053 for (i = 0; i < count; ++i) {
2054 struct jme_desc *desc = &rdata->jme_rx_ring[cons];
2056 desc->flags = htole32(JME_RD_OWN | JME_RD_INTR | JME_RD_64BIT);
2057 desc->buflen = htole32(MCLBYTES);
2058 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt);
2062 static __inline struct pktinfo *
2063 jme_pktinfo(struct pktinfo *pi, uint32_t flags)
2065 if (flags & JME_RD_IPV4)
2066 pi->pi_netisr = NETISR_IP;
2067 else if (flags & JME_RD_IPV6)
2068 pi->pi_netisr = NETISR_IPV6;
2073 pi->pi_l3proto = IPPROTO_UNKNOWN;
2075 if (flags & JME_RD_MORE_FRAG)
2076 pi->pi_flags |= PKTINFO_FLAG_FRAG;
2077 else if (flags & JME_RD_TCP)
2078 pi->pi_l3proto = IPPROTO_TCP;
2079 else if (flags & JME_RD_UDP)
2080 pi->pi_l3proto = IPPROTO_UDP;
2086 /* Receive a frame. */
2088 jme_rxpkt(struct jme_rxdata *rdata)
2090 struct ifnet *ifp = &rdata->jme_sc->arpcom.ac_if;
2091 struct jme_desc *desc;
2092 struct jme_rxdesc *rxd;
2093 struct mbuf *mp, *m;
2094 uint32_t flags, status, hash, hashinfo;
2095 int cons, count, nsegs;
2097 cons = rdata->jme_rx_cons;
2098 desc = &rdata->jme_rx_ring[cons];
2099 flags = le32toh(desc->flags);
2100 status = le32toh(desc->buflen);
2101 hash = le32toh(desc->addr_hi);
2102 hashinfo = le32toh(desc->addr_lo);
2103 nsegs = JME_RX_NSEGS(status);
2105 JME_RSS_DPRINTF(rdata->jme_sc, 15, "ring%d, flags 0x%08x, "
2106 "hash 0x%08x, hash info 0x%08x\n",
2107 rdata->jme_rx_idx, flags, hash, hashinfo);
2109 if (status & JME_RX_ERR_STAT) {
2111 jme_discard_rxbufs(rdata, cons, nsegs);
2112 #ifdef JME_SHOW_ERRORS
2113 if_printf(ifp, "%s : receive error = 0x%b\n",
2114 __func__, JME_RX_ERR(status), JME_RX_ERR_BITS);
2116 rdata->jme_rx_cons += nsegs;
2117 rdata->jme_rx_cons %= rdata->jme_rx_desc_cnt;
2121 rdata->jme_rxlen = JME_RX_BYTES(status) - JME_RX_PAD_BYTES;
2122 for (count = 0; count < nsegs; count++,
2123 JME_DESC_INC(cons, rdata->jme_rx_desc_cnt)) {
2124 rxd = &rdata->jme_rxdesc[cons];
2127 /* Add a new receive buffer to the ring. */
2128 if (jme_newbuf(rdata, rxd, 0) != 0) {
2131 jme_discard_rxbufs(rdata, cons, nsegs - count);
2132 if (rdata->jme_rxhead != NULL) {
2133 m_freem(rdata->jme_rxhead);
2134 JME_RXCHAIN_RESET(rdata);
2140 * Assume we've received a full sized frame.
2141 * Actual size is fixed when we encounter the end of
2142 * multi-segmented frame.
2144 mp->m_len = MCLBYTES;
2146 /* Chain received mbufs. */
2147 if (rdata->jme_rxhead == NULL) {
2148 rdata->jme_rxhead = mp;
2149 rdata->jme_rxtail = mp;
2152 * Receive processor can receive a maximum frame
2153 * size of 65535 bytes.
2155 rdata->jme_rxtail->m_next = mp;
2156 rdata->jme_rxtail = mp;
2159 if (count == nsegs - 1) {
2160 struct pktinfo pi0, *pi;
2162 /* Last desc. for this frame. */
2163 m = rdata->jme_rxhead;
2164 m->m_pkthdr.len = rdata->jme_rxlen;
2166 /* Set first mbuf size. */
2167 m->m_len = MCLBYTES - JME_RX_PAD_BYTES;
2168 /* Set last mbuf size. */
2169 mp->m_len = rdata->jme_rxlen -
2170 ((MCLBYTES - JME_RX_PAD_BYTES) +
2171 (MCLBYTES * (nsegs - 2)));
2173 m->m_len = rdata->jme_rxlen;
2175 m->m_pkthdr.rcvif = ifp;
2178 * Account for 10bytes auto padding which is used
2179 * to align IP header on 32bit boundary. Also note,
2180 * CRC bytes is automatically removed by the
2183 m->m_data += JME_RX_PAD_BYTES;
2185 /* Set checksum information. */
2186 if ((ifp->if_capenable & IFCAP_RXCSUM) &&
2187 (flags & JME_RD_IPV4)) {
2188 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2189 if (flags & JME_RD_IPCSUM)
2190 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2191 if ((flags & JME_RD_MORE_FRAG) == 0 &&
2192 ((flags & (JME_RD_TCP | JME_RD_TCPCSUM)) ==
2193 (JME_RD_TCP | JME_RD_TCPCSUM) ||
2194 (flags & (JME_RD_UDP | JME_RD_UDPCSUM)) ==
2195 (JME_RD_UDP | JME_RD_UDPCSUM))) {
2196 m->m_pkthdr.csum_flags |=
2197 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2198 m->m_pkthdr.csum_data = 0xffff;
2202 /* Check for VLAN tagged packets. */
2203 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) &&
2204 (flags & JME_RD_VLAN_TAG)) {
2205 m->m_pkthdr.ether_vlantag =
2206 flags & JME_RD_VLAN_MASK;
2207 m->m_flags |= M_VLANTAG;
2212 if (ifp->if_capenable & IFCAP_RSS)
2213 pi = jme_pktinfo(&pi0, flags);
2218 (hashinfo & JME_RD_HASH_FN_MASK) != 0) {
2219 m->m_flags |= M_HASH;
2220 m->m_pkthdr.hash = toeplitz_hash(hash);
2223 #ifdef JME_RSS_DEBUG
2225 JME_RSS_DPRINTF(rdata->jme_sc, 10,
2226 "isr %d flags %08x, l3 %d %s\n",
2227 pi->pi_netisr, pi->pi_flags,
2229 (m->m_flags & M_HASH) ? "hash" : "");
2234 ether_input_pkt(ifp, m, pi);
2236 /* Reset mbuf chains. */
2237 JME_RXCHAIN_RESET(rdata);
2238 #ifdef JME_RSS_DEBUG
2239 rdata->jme_rx_pkt++;
2244 rdata->jme_rx_cons += nsegs;
2245 rdata->jme_rx_cons %= rdata->jme_rx_desc_cnt;
2249 jme_rxeof(struct jme_rxdata *rdata, int count)
2251 struct jme_desc *desc;
2255 #ifdef DEVICE_POLLING
2256 if (count >= 0 && count-- == 0)
2259 desc = &rdata->jme_rx_ring[rdata->jme_rx_cons];
2260 if ((le32toh(desc->flags) & JME_RD_OWN) == JME_RD_OWN)
2262 if ((le32toh(desc->buflen) & JME_RD_VALID) == 0)
2266 * Check number of segments against received bytes.
2267 * Non-matching value would indicate that hardware
2268 * is still trying to update Rx descriptors. I'm not
2269 * sure whether this check is needed.
2271 nsegs = JME_RX_NSEGS(le32toh(desc->buflen));
2272 pktlen = JME_RX_BYTES(le32toh(desc->buflen));
2273 if (nsegs != howmany(pktlen, MCLBYTES)) {
2274 if_printf(&rdata->jme_sc->arpcom.ac_if,
2275 "RX fragment count(%d) and "
2276 "packet size(%d) mismach\n", nsegs, pktlen);
2280 /* Received a frame. */
2288 struct jme_softc *sc = xsc;
2289 struct ifnet *ifp = &sc->arpcom.ac_if;
2290 struct mii_data *mii = device_get_softc(sc->jme_miibus);
2292 ifnet_serialize_all(ifp);
2295 callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
2297 ifnet_deserialize_all(ifp);
2301 jme_reset(struct jme_softc *sc)
2305 /* Make sure that TX and RX are stopped */
2310 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2314 * Hold reset bit before stop reset
2317 /* Disable TXMAC and TXOFL clock sources */
2318 CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2319 /* Disable RXMAC clock source */
2320 val = CSR_READ_4(sc, JME_GPREG1);
2321 CSR_WRITE_4(sc, JME_GPREG1, val | GPREG1_DIS_RXMAC_CLKSRC);
2323 CSR_READ_4(sc, JME_GHC);
2326 CSR_WRITE_4(sc, JME_GHC, 0);
2328 CSR_READ_4(sc, JME_GHC);
2331 * Clear reset bit after stop reset
2334 /* Enable TXMAC and TXOFL clock sources */
2335 CSR_WRITE_4(sc, JME_GHC, GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC);
2336 /* Enable RXMAC clock source */
2337 val = CSR_READ_4(sc, JME_GPREG1);
2338 CSR_WRITE_4(sc, JME_GPREG1, val & ~GPREG1_DIS_RXMAC_CLKSRC);
2340 CSR_READ_4(sc, JME_GHC);
2342 /* Disable TXMAC and TXOFL clock sources */
2343 CSR_WRITE_4(sc, JME_GHC, 0);
2344 /* Disable RXMAC clock source */
2345 val = CSR_READ_4(sc, JME_GPREG1);
2346 CSR_WRITE_4(sc, JME_GPREG1, val | GPREG1_DIS_RXMAC_CLKSRC);
2348 CSR_READ_4(sc, JME_GHC);
2350 /* Enable TX and RX */
2351 val = CSR_READ_4(sc, JME_TXCSR);
2352 CSR_WRITE_4(sc, JME_TXCSR, val | TXCSR_TX_ENB);
2353 val = CSR_READ_4(sc, JME_RXCSR);
2354 CSR_WRITE_4(sc, JME_RXCSR, val | RXCSR_RX_ENB);
2356 CSR_READ_4(sc, JME_TXCSR);
2357 CSR_READ_4(sc, JME_RXCSR);
2359 /* Enable TXMAC and TXOFL clock sources */
2360 CSR_WRITE_4(sc, JME_GHC, GHC_TXOFL_CLKSRC | GHC_TXMAC_CLKSRC);
2361 /* Eisable RXMAC clock source */
2362 val = CSR_READ_4(sc, JME_GPREG1);
2363 CSR_WRITE_4(sc, JME_GPREG1, val & ~GPREG1_DIS_RXMAC_CLKSRC);
2365 CSR_READ_4(sc, JME_GHC);
2367 /* Stop TX and RX */
2375 struct jme_softc *sc = xsc;
2376 struct ifnet *ifp = &sc->arpcom.ac_if;
2377 struct mii_data *mii;
2378 uint8_t eaddr[ETHER_ADDR_LEN];
2383 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2386 * Cancel any pending I/O.
2391 * Reset the chip to a known state.
2396 * Setup MSI/MSI-X vectors to interrupts mapping
2401 howmany(ifp->if_mtu + sizeof(struct ether_vlan_header), MCLBYTES);
2402 KKASSERT(sc->jme_txd_spare >= 1);
2405 * If we use 64bit address mode for transmitting, each Tx request
2406 * needs one more symbol descriptor.
2408 if (sc->jme_lowaddr != BUS_SPACE_MAXADDR_32BIT)
2409 sc->jme_txd_spare += 1;
2411 if (sc->jme_cdata.jme_rx_ring_cnt > JME_NRXRING_MIN)
2414 jme_disable_rss(sc);
2416 /* Init RX descriptors */
2417 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2418 error = jme_init_rx_ring(&sc->jme_cdata.jme_rx_data[r]);
2420 if_printf(ifp, "initialization failed: "
2421 "no memory for %dth RX ring.\n", r);
2427 /* Init TX descriptors */
2428 jme_init_tx_ring(sc);
2430 /* Initialize shadow status block. */
2433 /* Reprogram the station address. */
2434 bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
2435 CSR_WRITE_4(sc, JME_PAR0,
2436 eaddr[3] << 24 | eaddr[2] << 16 | eaddr[1] << 8 | eaddr[0]);
2437 CSR_WRITE_4(sc, JME_PAR1, eaddr[5] << 8 | eaddr[4]);
2440 * Configure Tx queue.
2441 * Tx priority queue weight value : 0
2442 * Tx FIFO threshold for processing next packet : 16QW
2443 * Maximum Tx DMA length : 512
2444 * Allow Tx DMA burst.
2446 sc->jme_txcsr = TXCSR_TXQ_N_SEL(TXCSR_TXQ0);
2447 sc->jme_txcsr |= TXCSR_TXQ_WEIGHT(TXCSR_TXQ_WEIGHT_MIN);
2448 sc->jme_txcsr |= TXCSR_FIFO_THRESH_16QW;
2449 sc->jme_txcsr |= sc->jme_tx_dma_size;
2450 sc->jme_txcsr |= TXCSR_DMA_BURST;
2451 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr);
2453 /* Set Tx descriptor counter. */
2454 CSR_WRITE_4(sc, JME_TXQDC, sc->jme_cdata.jme_tx_desc_cnt);
2456 /* Set Tx ring address to the hardware. */
2457 paddr = sc->jme_cdata.jme_tx_ring_paddr;
2458 CSR_WRITE_4(sc, JME_TXDBA_HI, JME_ADDR_HI(paddr));
2459 CSR_WRITE_4(sc, JME_TXDBA_LO, JME_ADDR_LO(paddr));
2461 /* Configure TxMAC parameters. */
2462 reg = TXMAC_IFG1_DEFAULT | TXMAC_IFG2_DEFAULT | TXMAC_IFG_ENB;
2463 reg |= TXMAC_THRESH_1_PKT;
2464 reg |= TXMAC_CRC_ENB | TXMAC_PAD_ENB;
2465 CSR_WRITE_4(sc, JME_TXMAC, reg);
2468 * Configure Rx queue.
2469 * FIFO full threshold for transmitting Tx pause packet : 128T
2470 * FIFO threshold for processing next packet : 128QW
2472 * Max Rx DMA length : 128
2473 * Rx descriptor retry : 32
2474 * Rx descriptor retry time gap : 256ns
2475 * Don't receive runt/bad frame.
2477 sc->jme_rxcsr = RXCSR_FIFO_FTHRESH_128T;
2480 * Since Rx FIFO size is 4K bytes, receiving frames larger
2481 * than 4K bytes will suffer from Rx FIFO overruns. So
2482 * decrease FIFO threshold to reduce the FIFO overruns for
2483 * frames larger than 4000 bytes.
2484 * For best performance of standard MTU sized frames use
2485 * maximum allowable FIFO threshold, 128QW.
2487 if ((ifp->if_mtu + ETHER_HDR_LEN + EVL_ENCAPLEN + ETHER_CRC_LEN) >
2489 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
2491 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_128QW;
2493 /* Improve PCI Express compatibility */
2494 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
2496 sc->jme_rxcsr |= sc->jme_rx_dma_size;
2497 sc->jme_rxcsr |= RXCSR_DESC_RT_CNT(RXCSR_DESC_RT_CNT_DEFAULT);
2498 sc->jme_rxcsr |= RXCSR_DESC_RT_GAP_256 & RXCSR_DESC_RT_GAP_MASK;
2499 /* XXX TODO DROP_BAD */
2501 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2502 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
2504 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RXQ_N_SEL(r));
2506 /* Set Rx descriptor counter. */
2507 CSR_WRITE_4(sc, JME_RXQDC, rdata->jme_rx_desc_cnt);
2509 /* Set Rx ring address to the hardware. */
2510 paddr = rdata->jme_rx_ring_paddr;
2511 CSR_WRITE_4(sc, JME_RXDBA_HI, JME_ADDR_HI(paddr));
2512 CSR_WRITE_4(sc, JME_RXDBA_LO, JME_ADDR_LO(paddr));
2515 /* Clear receive filter. */
2516 CSR_WRITE_4(sc, JME_RXMAC, 0);
2518 /* Set up the receive filter. */
2523 * Disable all WOL bits as WOL can interfere normal Rx
2524 * operation. Also clear WOL detection status bits.
2526 reg = CSR_READ_4(sc, JME_PMCS);
2527 reg &= ~PMCS_WOL_ENB_MASK;
2528 CSR_WRITE_4(sc, JME_PMCS, reg);
2531 * Pad 10bytes right before received frame. This will greatly
2532 * help Rx performance on strict-alignment architectures as
2533 * it does not need to copy the frame to align the payload.
2535 reg = CSR_READ_4(sc, JME_RXMAC);
2536 reg |= RXMAC_PAD_10BYTES;
2538 if (ifp->if_capenable & IFCAP_RXCSUM)
2539 reg |= RXMAC_CSUM_ENB;
2540 CSR_WRITE_4(sc, JME_RXMAC, reg);
2542 /* Configure general purpose reg0 */
2543 reg = CSR_READ_4(sc, JME_GPREG0);
2544 reg &= ~GPREG0_PCC_UNIT_MASK;
2545 /* Set PCC timer resolution to micro-seconds unit. */
2546 reg |= GPREG0_PCC_UNIT_US;
2548 * Disable all shadow register posting as we have to read
2549 * JME_INTR_STATUS register in jme_intr. Also it seems
2550 * that it's hard to synchronize interrupt status between
2551 * hardware and software with shadow posting due to
2552 * requirements of bus_dmamap_sync(9).
2554 reg |= GPREG0_SH_POST_DW7_DIS | GPREG0_SH_POST_DW6_DIS |
2555 GPREG0_SH_POST_DW5_DIS | GPREG0_SH_POST_DW4_DIS |
2556 GPREG0_SH_POST_DW3_DIS | GPREG0_SH_POST_DW2_DIS |
2557 GPREG0_SH_POST_DW1_DIS | GPREG0_SH_POST_DW0_DIS;
2558 /* Disable posting of DW0. */
2559 reg &= ~GPREG0_POST_DW0_ENB;
2560 /* Clear PME message. */
2561 reg &= ~GPREG0_PME_ENB;
2562 /* Set PHY address. */
2563 reg &= ~GPREG0_PHY_ADDR_MASK;
2564 reg |= sc->jme_phyaddr;
2565 CSR_WRITE_4(sc, JME_GPREG0, reg);
2567 /* Configure Tx queue 0 packet completion coalescing. */
2568 jme_set_tx_coal(sc);
2570 /* Configure Rx queues packet completion coalescing. */
2571 jme_set_rx_coal(sc);
2573 /* Configure shadow status block but don't enable posting. */
2574 paddr = sc->jme_cdata.jme_ssb_block_paddr;
2575 CSR_WRITE_4(sc, JME_SHBASE_ADDR_HI, JME_ADDR_HI(paddr));
2576 CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO, JME_ADDR_LO(paddr));
2578 /* Disable Timer 1 and Timer 2. */
2579 CSR_WRITE_4(sc, JME_TIMER1, 0);
2580 CSR_WRITE_4(sc, JME_TIMER2, 0);
2582 /* Configure retry transmit period, retry limit value. */
2583 CSR_WRITE_4(sc, JME_TXTRHD,
2584 ((TXTRHD_RT_PERIOD_DEFAULT << TXTRHD_RT_PERIOD_SHIFT) &
2585 TXTRHD_RT_PERIOD_MASK) |
2586 ((TXTRHD_RT_LIMIT_DEFAULT << TXTRHD_RT_LIMIT_SHIFT) &
2587 TXTRHD_RT_LIMIT_SHIFT));
2589 #ifdef DEVICE_POLLING
2590 if (!(ifp->if_flags & IFF_POLLING))
2592 /* Initialize the interrupt mask. */
2593 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
2594 CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2597 * Enabling Tx/Rx DMA engines and Rx queue processing is
2598 * done after detection of valid link in jme_miibus_statchg.
2600 sc->jme_flags &= ~JME_FLAG_LINK;
2602 /* Set the current media. */
2603 mii = device_get_softc(sc->jme_miibus);
2606 callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
2608 ifp->if_flags |= IFF_RUNNING;
2609 ifp->if_flags &= ~IFF_OACTIVE;
2613 jme_stop(struct jme_softc *sc)
2615 struct ifnet *ifp = &sc->arpcom.ac_if;
2616 struct jme_txdesc *txd;
2617 struct jme_rxdesc *rxd;
2618 struct jme_rxdata *rdata;
2621 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2624 * Mark the interface down and cancel the watchdog timer.
2626 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2629 callout_stop(&sc->jme_tick_ch);
2630 sc->jme_flags &= ~JME_FLAG_LINK;
2633 * Disable interrupts.
2635 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
2636 CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2638 /* Disable updating shadow status block. */
2639 CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO,
2640 CSR_READ_4(sc, JME_SHBASE_ADDR_LO) & ~SHBASE_POST_ENB);
2642 /* Stop receiver, transmitter. */
2647 * Free partial finished RX segments
2649 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2650 rdata = &sc->jme_cdata.jme_rx_data[r];
2651 if (rdata->jme_rxhead != NULL)
2652 m_freem(rdata->jme_rxhead);
2653 JME_RXCHAIN_RESET(rdata);
2657 * Free RX and TX mbufs still in the queues.
2659 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
2660 rdata = &sc->jme_cdata.jme_rx_data[r];
2661 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
2662 rxd = &rdata->jme_rxdesc[i];
2663 if (rxd->rx_m != NULL) {
2664 bus_dmamap_unload(rdata->jme_rx_tag,
2671 for (i = 0; i < sc->jme_cdata.jme_tx_desc_cnt; i++) {
2672 txd = &sc->jme_cdata.jme_txdesc[i];
2673 if (txd->tx_m != NULL) {
2674 bus_dmamap_unload(sc->jme_cdata.jme_tx_tag,
2684 jme_stop_tx(struct jme_softc *sc)
2689 reg = CSR_READ_4(sc, JME_TXCSR);
2690 if ((reg & TXCSR_TX_ENB) == 0)
2692 reg &= ~TXCSR_TX_ENB;
2693 CSR_WRITE_4(sc, JME_TXCSR, reg);
2694 for (i = JME_TIMEOUT; i > 0; i--) {
2696 if ((CSR_READ_4(sc, JME_TXCSR) & TXCSR_TX_ENB) == 0)
2700 device_printf(sc->jme_dev, "stopping transmitter timeout!\n");
2704 jme_stop_rx(struct jme_softc *sc)
2709 reg = CSR_READ_4(sc, JME_RXCSR);
2710 if ((reg & RXCSR_RX_ENB) == 0)
2712 reg &= ~RXCSR_RX_ENB;
2713 CSR_WRITE_4(sc, JME_RXCSR, reg);
2714 for (i = JME_TIMEOUT; i > 0; i--) {
2716 if ((CSR_READ_4(sc, JME_RXCSR) & RXCSR_RX_ENB) == 0)
2720 device_printf(sc->jme_dev, "stopping recevier timeout!\n");
2724 jme_init_tx_ring(struct jme_softc *sc)
2726 struct jme_chain_data *cd;
2727 struct jme_txdesc *txd;
2730 sc->jme_cdata.jme_tx_prod = 0;
2731 sc->jme_cdata.jme_tx_cons = 0;
2732 sc->jme_cdata.jme_tx_cnt = 0;
2734 cd = &sc->jme_cdata;
2735 bzero(cd->jme_tx_ring, JME_TX_RING_SIZE(sc));
2736 for (i = 0; i < sc->jme_cdata.jme_tx_desc_cnt; i++) {
2737 txd = &sc->jme_cdata.jme_txdesc[i];
2739 txd->tx_desc = &cd->jme_tx_ring[i];
2745 jme_init_ssb(struct jme_softc *sc)
2747 struct jme_chain_data *cd;
2749 cd = &sc->jme_cdata;
2750 bzero(cd->jme_ssb_block, JME_SSB_SIZE);
2754 jme_init_rx_ring(struct jme_rxdata *rdata)
2756 struct jme_rxdesc *rxd;
2759 KKASSERT(rdata->jme_rxhead == NULL &&
2760 rdata->jme_rxtail == NULL &&
2761 rdata->jme_rxlen == 0);
2762 rdata->jme_rx_cons = 0;
2764 bzero(rdata->jme_rx_ring, JME_RX_RING_SIZE(rdata));
2765 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
2768 rxd = &rdata->jme_rxdesc[i];
2770 rxd->rx_desc = &rdata->jme_rx_ring[i];
2771 error = jme_newbuf(rdata, rxd, 1);
2779 jme_newbuf(struct jme_rxdata *rdata, struct jme_rxdesc *rxd, int init)
2781 struct jme_desc *desc;
2783 bus_dma_segment_t segs;
2787 m = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
2791 * JMC250 has 64bit boundary alignment limitation so jme(4)
2792 * takes advantage of 10 bytes padding feature of hardware
2793 * in order not to copy entire frame to align IP header on
2796 m->m_len = m->m_pkthdr.len = MCLBYTES;
2798 error = bus_dmamap_load_mbuf_segment(rdata->jme_rx_tag,
2799 rdata->jme_rx_sparemap, m, &segs, 1, &nsegs,
2804 if_printf(&rdata->jme_sc->arpcom.ac_if,
2805 "can't load RX mbuf\n");
2810 if (rxd->rx_m != NULL) {
2811 bus_dmamap_sync(rdata->jme_rx_tag, rxd->rx_dmamap,
2812 BUS_DMASYNC_POSTREAD);
2813 bus_dmamap_unload(rdata->jme_rx_tag, rxd->rx_dmamap);
2815 map = rxd->rx_dmamap;
2816 rxd->rx_dmamap = rdata->jme_rx_sparemap;
2817 rdata->jme_rx_sparemap = map;
2820 desc = rxd->rx_desc;
2821 desc->buflen = htole32(segs.ds_len);
2822 desc->addr_lo = htole32(JME_ADDR_LO(segs.ds_addr));
2823 desc->addr_hi = htole32(JME_ADDR_HI(segs.ds_addr));
2824 desc->flags = htole32(JME_RD_OWN | JME_RD_INTR | JME_RD_64BIT);
2830 jme_set_vlan(struct jme_softc *sc)
2832 struct ifnet *ifp = &sc->arpcom.ac_if;
2835 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2837 reg = CSR_READ_4(sc, JME_RXMAC);
2838 reg &= ~RXMAC_VLAN_ENB;
2839 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
2840 reg |= RXMAC_VLAN_ENB;
2841 CSR_WRITE_4(sc, JME_RXMAC, reg);
2845 jme_set_filter(struct jme_softc *sc)
2847 struct ifnet *ifp = &sc->arpcom.ac_if;
2848 struct ifmultiaddr *ifma;
2853 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2855 rxcfg = CSR_READ_4(sc, JME_RXMAC);
2856 rxcfg &= ~(RXMAC_BROADCAST | RXMAC_PROMISC | RXMAC_MULTICAST |
2860 * Always accept frames destined to our station address.
2861 * Always accept broadcast frames.
2863 rxcfg |= RXMAC_UNICAST | RXMAC_BROADCAST;
2865 if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
2866 if (ifp->if_flags & IFF_PROMISC)
2867 rxcfg |= RXMAC_PROMISC;
2868 if (ifp->if_flags & IFF_ALLMULTI)
2869 rxcfg |= RXMAC_ALLMULTI;
2870 CSR_WRITE_4(sc, JME_MAR0, 0xFFFFFFFF);
2871 CSR_WRITE_4(sc, JME_MAR1, 0xFFFFFFFF);
2872 CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
2877 * Set up the multicast address filter by passing all multicast
2878 * addresses through a CRC generator, and then using the low-order
2879 * 6 bits as an index into the 64 bit multicast hash table. The
2880 * high order bits select the register, while the rest of the bits
2881 * select the bit within the register.
2883 rxcfg |= RXMAC_MULTICAST;
2884 bzero(mchash, sizeof(mchash));
2886 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2887 if (ifma->ifma_addr->sa_family != AF_LINK)
2889 crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
2890 ifma->ifma_addr), ETHER_ADDR_LEN);
2892 /* Just want the 6 least significant bits. */
2895 /* Set the corresponding bit in the hash table. */
2896 mchash[crc >> 5] |= 1 << (crc & 0x1f);
2899 CSR_WRITE_4(sc, JME_MAR0, mchash[0]);
2900 CSR_WRITE_4(sc, JME_MAR1, mchash[1]);
2901 CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
2905 jme_sysctl_tx_coal_to(SYSCTL_HANDLER_ARGS)
2907 struct jme_softc *sc = arg1;
2908 struct ifnet *ifp = &sc->arpcom.ac_if;
2911 ifnet_serialize_all(ifp);
2913 v = sc->jme_tx_coal_to;
2914 error = sysctl_handle_int(oidp, &v, 0, req);
2915 if (error || req->newptr == NULL)
2918 if (v < PCCTX_COAL_TO_MIN || v > PCCTX_COAL_TO_MAX) {
2923 if (v != sc->jme_tx_coal_to) {
2924 sc->jme_tx_coal_to = v;
2925 if (ifp->if_flags & IFF_RUNNING)
2926 jme_set_tx_coal(sc);
2929 ifnet_deserialize_all(ifp);
2934 jme_sysctl_tx_coal_pkt(SYSCTL_HANDLER_ARGS)
2936 struct jme_softc *sc = arg1;
2937 struct ifnet *ifp = &sc->arpcom.ac_if;
2940 ifnet_serialize_all(ifp);
2942 v = sc->jme_tx_coal_pkt;
2943 error = sysctl_handle_int(oidp, &v, 0, req);
2944 if (error || req->newptr == NULL)
2947 if (v < PCCTX_COAL_PKT_MIN || v > PCCTX_COAL_PKT_MAX) {
2952 if (v != sc->jme_tx_coal_pkt) {
2953 sc->jme_tx_coal_pkt = v;
2954 if (ifp->if_flags & IFF_RUNNING)
2955 jme_set_tx_coal(sc);
2958 ifnet_deserialize_all(ifp);
2963 jme_sysctl_rx_coal_to(SYSCTL_HANDLER_ARGS)
2965 struct jme_softc *sc = arg1;
2966 struct ifnet *ifp = &sc->arpcom.ac_if;
2969 ifnet_serialize_all(ifp);
2971 v = sc->jme_rx_coal_to;
2972 error = sysctl_handle_int(oidp, &v, 0, req);
2973 if (error || req->newptr == NULL)
2976 if (v < PCCRX_COAL_TO_MIN || v > PCCRX_COAL_TO_MAX) {
2981 if (v != sc->jme_rx_coal_to) {
2982 sc->jme_rx_coal_to = v;
2983 if (ifp->if_flags & IFF_RUNNING)
2984 jme_set_rx_coal(sc);
2987 ifnet_deserialize_all(ifp);
2992 jme_sysctl_rx_coal_pkt(SYSCTL_HANDLER_ARGS)
2994 struct jme_softc *sc = arg1;
2995 struct ifnet *ifp = &sc->arpcom.ac_if;
2998 ifnet_serialize_all(ifp);
3000 v = sc->jme_rx_coal_pkt;
3001 error = sysctl_handle_int(oidp, &v, 0, req);
3002 if (error || req->newptr == NULL)
3005 if (v < PCCRX_COAL_PKT_MIN || v > PCCRX_COAL_PKT_MAX) {
3010 if (v != sc->jme_rx_coal_pkt) {
3011 sc->jme_rx_coal_pkt = v;
3012 if (ifp->if_flags & IFF_RUNNING)
3013 jme_set_rx_coal(sc);
3016 ifnet_deserialize_all(ifp);
3021 jme_set_tx_coal(struct jme_softc *sc)
3025 reg = (sc->jme_tx_coal_to << PCCTX_COAL_TO_SHIFT) &
3027 reg |= (sc->jme_tx_coal_pkt << PCCTX_COAL_PKT_SHIFT) &
3028 PCCTX_COAL_PKT_MASK;
3029 reg |= PCCTX_COAL_TXQ0;
3030 CSR_WRITE_4(sc, JME_PCCTX, reg);
3034 jme_set_rx_coal(struct jme_softc *sc)
3039 reg = (sc->jme_rx_coal_to << PCCRX_COAL_TO_SHIFT) &
3041 reg |= (sc->jme_rx_coal_pkt << PCCRX_COAL_PKT_SHIFT) &
3042 PCCRX_COAL_PKT_MASK;
3043 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r)
3044 CSR_WRITE_4(sc, JME_PCCRX(r), reg);
3047 #ifdef DEVICE_POLLING
3050 jme_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3052 struct jme_softc *sc = ifp->if_softc;
3056 ASSERT_SERIALIZED(&sc->jme_serialize);
3060 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
3063 case POLL_DEREGISTER:
3064 CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
3067 case POLL_AND_CHECK_STATUS:
3069 status = CSR_READ_4(sc, JME_INTR_STATUS);
3071 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
3072 struct jme_rxdata *rdata =
3073 &sc->jme_cdata.jme_rx_data[r];
3075 lwkt_serialize_enter(&rdata->jme_rx_serialize);
3076 jme_rxeof(rdata, count);
3077 lwkt_serialize_exit(&rdata->jme_rx_serialize);
3080 if (status & INTR_RXQ_DESC_EMPTY) {
3081 CSR_WRITE_4(sc, JME_INTR_STATUS, status);
3082 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr |
3083 RXCSR_RX_ENB | RXCSR_RXQ_START);
3086 lwkt_serialize_enter(&sc->jme_cdata.jme_tx_serialize);
3088 if (!ifq_is_empty(&ifp->if_snd))
3090 lwkt_serialize_exit(&sc->jme_cdata.jme_tx_serialize);
3095 #endif /* DEVICE_POLLING */
3098 jme_rxring_dma_alloc(struct jme_rxdata *rdata)
3103 asize = roundup2(JME_RX_RING_SIZE(rdata), JME_RX_RING_ALIGN);
3104 error = bus_dmamem_coherent(rdata->jme_sc->jme_cdata.jme_ring_tag,
3105 JME_RX_RING_ALIGN, 0,
3106 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3107 asize, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
3109 device_printf(rdata->jme_sc->jme_dev,
3110 "could not allocate %dth Rx ring.\n", rdata->jme_rx_idx);
3113 rdata->jme_rx_ring_tag = dmem.dmem_tag;
3114 rdata->jme_rx_ring_map = dmem.dmem_map;
3115 rdata->jme_rx_ring = dmem.dmem_addr;
3116 rdata->jme_rx_ring_paddr = dmem.dmem_busaddr;
3122 jme_rxbuf_dma_alloc(struct jme_rxdata *rdata)
3126 /* Create tag for Rx buffers. */
3127 error = bus_dma_tag_create(
3128 rdata->jme_sc->jme_cdata.jme_buffer_tag,/* parent */
3129 JME_RX_BUF_ALIGN, 0, /* algnmnt, boundary */
3130 BUS_SPACE_MAXADDR, /* lowaddr */
3131 BUS_SPACE_MAXADDR, /* highaddr */
3132 NULL, NULL, /* filter, filterarg */
3133 MCLBYTES, /* maxsize */
3135 MCLBYTES, /* maxsegsize */
3136 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ALIGNED,/* flags */
3137 &rdata->jme_rx_tag);
3139 device_printf(rdata->jme_sc->jme_dev,
3140 "could not create %dth Rx DMA tag.\n", rdata->jme_rx_idx);
3144 /* Create DMA maps for Rx buffers. */
3145 error = bus_dmamap_create(rdata->jme_rx_tag, BUS_DMA_WAITOK,
3146 &rdata->jme_rx_sparemap);
3148 device_printf(rdata->jme_sc->jme_dev,
3149 "could not create %dth spare Rx dmamap.\n",
3151 bus_dma_tag_destroy(rdata->jme_rx_tag);
3152 rdata->jme_rx_tag = NULL;
3155 for (i = 0; i < rdata->jme_rx_desc_cnt; i++) {
3156 struct jme_rxdesc *rxd = &rdata->jme_rxdesc[i];
3158 error = bus_dmamap_create(rdata->jme_rx_tag, BUS_DMA_WAITOK,
3163 device_printf(rdata->jme_sc->jme_dev,
3164 "could not create %dth Rx dmamap "
3165 "for %dth RX ring.\n", i, rdata->jme_rx_idx);
3167 for (j = 0; j < i; ++j) {
3168 rxd = &rdata->jme_rxdesc[j];
3169 bus_dmamap_destroy(rdata->jme_rx_tag,
3172 bus_dmamap_destroy(rdata->jme_rx_tag,
3173 rdata->jme_rx_sparemap);
3174 bus_dma_tag_destroy(rdata->jme_rx_tag);
3175 rdata->jme_rx_tag = NULL;
3183 jme_rx_intr(struct jme_softc *sc, uint32_t status)
3187 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
3188 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
3190 if (status & rdata->jme_rx_coal) {
3191 lwkt_serialize_enter(&rdata->jme_rx_serialize);
3192 jme_rxeof(rdata, -1);
3193 lwkt_serialize_exit(&rdata->jme_rx_serialize);
3199 jme_enable_rss(struct jme_softc *sc)
3202 uint8_t key[RSSKEY_NREGS * RSSKEY_REGSIZE];
3205 KASSERT(sc->jme_cdata.jme_rx_ring_cnt == JME_NRXRING_2 ||
3206 sc->jme_cdata.jme_rx_ring_cnt == JME_NRXRING_4,
3207 ("%s: invalid # of RX rings (%d)",
3208 sc->arpcom.ac_if.if_xname, sc->jme_cdata.jme_rx_ring_cnt));
3210 rssc = RSSC_HASH_64_ENTRY;
3211 rssc |= RSSC_HASH_IPV4 | RSSC_HASH_IPV4_TCP;
3212 rssc |= sc->jme_cdata.jme_rx_ring_cnt >> 1;
3213 JME_RSS_DPRINTF(sc, 1, "rssc 0x%08x\n", rssc);
3214 CSR_WRITE_4(sc, JME_RSSC, rssc);
3216 toeplitz_get_key(key, sizeof(key));
3217 for (i = 0; i < RSSKEY_NREGS; ++i) {
3220 keyreg = RSSKEY_REGVAL(key, i);
3221 JME_RSS_DPRINTF(sc, 5, "keyreg%d 0x%08x\n", i, keyreg);
3223 CSR_WRITE_4(sc, RSSKEY_REG(i), keyreg);
3227 * Create redirect table in following fashion:
3228 * (hash & ring_cnt_mask) == rdr_table[(hash & rdr_table_mask)]
3231 for (i = 0; i < RSSTBL_REGSIZE; ++i) {
3234 q = i % sc->jme_cdata.jme_rx_ring_cnt;
3235 ind |= q << (i * 8);
3237 JME_RSS_DPRINTF(sc, 1, "ind 0x%08x\n", ind);
3239 for (i = 0; i < RSSTBL_NREGS; ++i)
3240 CSR_WRITE_4(sc, RSSTBL_REG(i), ind);
3244 jme_disable_rss(struct jme_softc *sc)
3246 CSR_WRITE_4(sc, JME_RSSC, RSSC_DIS_RSS);
3250 jme_serialize(struct ifnet *ifp, enum ifnet_serialize slz)
3252 struct jme_softc *sc = ifp->if_softc;
3255 case IFNET_SERIALIZE_ALL:
3256 lwkt_serialize_array_enter(sc->jme_serialize_arr,
3257 sc->jme_serialize_cnt, 0);
3260 case IFNET_SERIALIZE_MAIN:
3261 lwkt_serialize_enter(&sc->jme_serialize);
3264 case IFNET_SERIALIZE_TX:
3265 lwkt_serialize_enter(&sc->jme_cdata.jme_tx_serialize);
3268 case IFNET_SERIALIZE_RX(0):
3269 lwkt_serialize_enter(
3270 &sc->jme_cdata.jme_rx_data[0].jme_rx_serialize);
3273 case IFNET_SERIALIZE_RX(1):
3274 lwkt_serialize_enter(
3275 &sc->jme_cdata.jme_rx_data[1].jme_rx_serialize);
3278 case IFNET_SERIALIZE_RX(2):
3279 lwkt_serialize_enter(
3280 &sc->jme_cdata.jme_rx_data[2].jme_rx_serialize);
3283 case IFNET_SERIALIZE_RX(3):
3284 lwkt_serialize_enter(
3285 &sc->jme_cdata.jme_rx_data[3].jme_rx_serialize);
3289 panic("%s unsupported serialize type", ifp->if_xname);
3294 jme_deserialize(struct ifnet *ifp, enum ifnet_serialize slz)
3296 struct jme_softc *sc = ifp->if_softc;
3299 case IFNET_SERIALIZE_ALL:
3300 lwkt_serialize_array_exit(sc->jme_serialize_arr,
3301 sc->jme_serialize_cnt, 0);
3304 case IFNET_SERIALIZE_MAIN:
3305 lwkt_serialize_exit(&sc->jme_serialize);
3308 case IFNET_SERIALIZE_TX:
3309 lwkt_serialize_exit(&sc->jme_cdata.jme_tx_serialize);
3312 case IFNET_SERIALIZE_RX(0):
3313 lwkt_serialize_exit(
3314 &sc->jme_cdata.jme_rx_data[0].jme_rx_serialize);
3317 case IFNET_SERIALIZE_RX(1):
3318 lwkt_serialize_exit(
3319 &sc->jme_cdata.jme_rx_data[1].jme_rx_serialize);
3322 case IFNET_SERIALIZE_RX(2):
3323 lwkt_serialize_exit(
3324 &sc->jme_cdata.jme_rx_data[2].jme_rx_serialize);
3327 case IFNET_SERIALIZE_RX(3):
3328 lwkt_serialize_exit(
3329 &sc->jme_cdata.jme_rx_data[3].jme_rx_serialize);
3333 panic("%s unsupported serialize type", ifp->if_xname);
3338 jme_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz)
3340 struct jme_softc *sc = ifp->if_softc;
3343 case IFNET_SERIALIZE_ALL:
3344 return lwkt_serialize_array_try(sc->jme_serialize_arr,
3345 sc->jme_serialize_cnt, 0);
3347 case IFNET_SERIALIZE_MAIN:
3348 return lwkt_serialize_try(&sc->jme_serialize);
3350 case IFNET_SERIALIZE_TX:
3351 return lwkt_serialize_try(&sc->jme_cdata.jme_tx_serialize);
3353 case IFNET_SERIALIZE_RX(0):
3354 return lwkt_serialize_try(
3355 &sc->jme_cdata.jme_rx_data[0].jme_rx_serialize);
3357 case IFNET_SERIALIZE_RX(1):
3358 return lwkt_serialize_try(
3359 &sc->jme_cdata.jme_rx_data[1].jme_rx_serialize);
3361 case IFNET_SERIALIZE_RX(2):
3362 return lwkt_serialize_try(
3363 &sc->jme_cdata.jme_rx_data[2].jme_rx_serialize);
3365 case IFNET_SERIALIZE_RX(3):
3366 return lwkt_serialize_try(
3367 &sc->jme_cdata.jme_rx_data[3].jme_rx_serialize);
3370 panic("%s unsupported serialize type", ifp->if_xname);
3377 jme_serialize_assert(struct ifnet *ifp, enum ifnet_serialize slz,
3378 boolean_t serialized)
3380 struct jme_softc *sc = ifp->if_softc;
3381 struct jme_rxdata *rdata;
3385 case IFNET_SERIALIZE_ALL:
3387 for (i = 0; i < sc->jme_serialize_cnt; ++i)
3388 ASSERT_SERIALIZED(sc->jme_serialize_arr[i]);
3390 for (i = 0; i < sc->jme_serialize_cnt; ++i)
3391 ASSERT_NOT_SERIALIZED(sc->jme_serialize_arr[i]);
3395 case IFNET_SERIALIZE_MAIN:
3397 ASSERT_SERIALIZED(&sc->jme_serialize);
3399 ASSERT_NOT_SERIALIZED(&sc->jme_serialize);
3402 case IFNET_SERIALIZE_TX:
3404 ASSERT_SERIALIZED(&sc->jme_cdata.jme_tx_serialize);
3406 ASSERT_NOT_SERIALIZED(&sc->jme_cdata.jme_tx_serialize);
3409 case IFNET_SERIALIZE_RX(0):
3410 rdata = &sc->jme_cdata.jme_rx_data[0];
3412 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3414 ASSERT_NOT_SERIALIZED(&rdata->jme_rx_serialize);
3417 case IFNET_SERIALIZE_RX(1):
3418 rdata = &sc->jme_cdata.jme_rx_data[1];
3420 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3422 ASSERT_NOT_SERIALIZED(&rdata->jme_rx_serialize);
3425 case IFNET_SERIALIZE_RX(2):
3426 rdata = &sc->jme_cdata.jme_rx_data[2];
3428 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3430 ASSERT_NOT_SERIALIZED(&rdata->jme_rx_serialize);
3433 case IFNET_SERIALIZE_RX(3):
3434 rdata = &sc->jme_cdata.jme_rx_data[3];
3436 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3438 ASSERT_NOT_SERIALIZED(&rdata->jme_rx_serialize);
3442 panic("%s unsupported serialize type", ifp->if_xname);
3446 #endif /* INVARIANTS */
3449 jme_msix_try_alloc(device_t dev)
3451 struct jme_softc *sc = device_get_softc(dev);
3452 struct jme_msix_data *msix;
3453 int error, i, r, msix_enable, msix_count;
3455 msix_count = 1 + sc->jme_cdata.jme_rx_ring_cnt;
3456 KKASSERT(msix_count <= JME_NMSIX);
3458 msix_enable = device_getenv_int(dev, "msix.enable", jme_msix_enable);
3461 * We leave the 1st MSI-X vector unused, so we
3462 * actually need msix_count + 1 MSI-X vectors.
3464 if (!msix_enable || pci_msix_count(dev) < (msix_count + 1))
3467 for (i = 0; i < msix_count; ++i)
3468 sc->jme_msix[i].jme_msix_rid = -1;
3472 msix = &sc->jme_msix[i++];
3473 msix->jme_msix_cpuid = 0; /* XXX Put TX to cpu0 */
3474 msix->jme_msix_arg = &sc->jme_cdata;
3475 msix->jme_msix_func = jme_msix_tx;
3476 msix->jme_msix_intrs = INTR_TXQ_COAL | INTR_TXQ_COAL_TO;
3477 msix->jme_msix_serialize = &sc->jme_cdata.jme_tx_serialize;
3478 ksnprintf(msix->jme_msix_desc, sizeof(msix->jme_msix_desc), "%s tx",
3479 device_get_nameunit(dev));
3481 for (r = 0; r < sc->jme_cdata.jme_rx_ring_cnt; ++r) {
3482 struct jme_rxdata *rdata = &sc->jme_cdata.jme_rx_data[r];
3484 msix = &sc->jme_msix[i++];
3485 msix->jme_msix_cpuid = r; /* XXX Put RX to cpuX */
3486 msix->jme_msix_arg = rdata;
3487 msix->jme_msix_func = jme_msix_rx;
3488 msix->jme_msix_intrs = rdata->jme_rx_coal | rdata->jme_rx_empty;
3489 msix->jme_msix_serialize = &rdata->jme_rx_serialize;
3490 ksnprintf(msix->jme_msix_desc, sizeof(msix->jme_msix_desc),
3491 "%s rx%d", device_get_nameunit(dev), r);
3494 KKASSERT(i == msix_count);
3496 error = pci_setup_msix(dev);
3500 /* Setup jme_msix_cnt early, so we could cleanup */
3501 sc->jme_msix_cnt = msix_count;
3503 for (i = 0; i < msix_count; ++i) {
3504 msix = &sc->jme_msix[i];
3506 msix->jme_msix_vector = i + 1;
3507 error = pci_alloc_msix_vector(dev, msix->jme_msix_vector,
3508 &msix->jme_msix_rid, msix->jme_msix_cpuid);
3512 msix->jme_msix_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
3513 &msix->jme_msix_rid, RF_ACTIVE);
3514 if (msix->jme_msix_res == NULL) {
3520 for (i = 0; i < JME_INTR_CNT; ++i) {
3521 uint32_t intr_mask = (1 << i);
3524 if ((JME_INTRS & intr_mask) == 0)
3527 for (x = 0; x < msix_count; ++x) {
3528 msix = &sc->jme_msix[x];
3529 if (msix->jme_msix_intrs & intr_mask) {
3532 reg = i / JME_MSINUM_FACTOR;
3533 KKASSERT(reg < JME_MSINUM_CNT);
3535 shift = (i % JME_MSINUM_FACTOR) * 4;
3537 sc->jme_msinum[reg] |=
3538 (msix->jme_msix_vector << shift);
3546 for (i = 0; i < JME_MSINUM_CNT; ++i) {
3547 device_printf(dev, "MSINUM%d: %#x\n", i,
3552 pci_enable_msix(dev);
3553 sc->jme_irq_type = PCI_INTR_TYPE_MSIX;
3561 jme_intr_alloc(device_t dev)
3563 struct jme_softc *sc = device_get_softc(dev);
3566 jme_msix_try_alloc(dev);
3568 if (sc->jme_irq_type != PCI_INTR_TYPE_MSIX) {
3569 sc->jme_irq_type = pci_alloc_1intr(dev, jme_msi_enable,
3570 &sc->jme_irq_rid, &irq_flags);
3572 sc->jme_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
3573 &sc->jme_irq_rid, irq_flags);
3574 if (sc->jme_irq_res == NULL) {
3575 device_printf(dev, "can't allocate irq\n");
3583 jme_msix_free(device_t dev)
3585 struct jme_softc *sc = device_get_softc(dev);
3588 KKASSERT(sc->jme_msix_cnt > 1);
3590 for (i = 0; i < sc->jme_msix_cnt; ++i) {
3591 struct jme_msix_data *msix = &sc->jme_msix[i];
3593 if (msix->jme_msix_res != NULL) {
3594 bus_release_resource(dev, SYS_RES_IRQ,
3595 msix->jme_msix_rid, msix->jme_msix_res);
3596 msix->jme_msix_res = NULL;
3598 if (msix->jme_msix_rid >= 0) {
3599 pci_release_msix_vector(dev, msix->jme_msix_rid);
3600 msix->jme_msix_rid = -1;
3603 pci_teardown_msix(dev);
3607 jme_intr_free(device_t dev)
3609 struct jme_softc *sc = device_get_softc(dev);
3611 if (sc->jme_irq_type != PCI_INTR_TYPE_MSIX) {
3612 if (sc->jme_irq_res != NULL) {
3613 bus_release_resource(dev, SYS_RES_IRQ, sc->jme_irq_rid,
3616 if (sc->jme_irq_type == PCI_INTR_TYPE_MSI)
3617 pci_release_msi(dev);
3624 jme_msix_tx(void *xcd)
3626 struct jme_chain_data *cd = xcd;
3627 struct jme_softc *sc = cd->jme_sc;
3628 struct ifnet *ifp = &sc->arpcom.ac_if;
3630 ASSERT_SERIALIZED(&cd->jme_tx_serialize);
3632 CSR_WRITE_4(sc, JME_INTR_MASK_CLR, INTR_TXQ_COAL | INTR_TXQ_COAL_TO);
3634 CSR_WRITE_4(sc, JME_INTR_STATUS,
3635 INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP);
3637 if (ifp->if_flags & IFF_RUNNING) {
3639 if (!ifq_is_empty(&ifp->if_snd))
3643 CSR_WRITE_4(sc, JME_INTR_MASK_SET, INTR_TXQ_COAL | INTR_TXQ_COAL_TO);
3647 jme_msix_rx(void *xrdata)
3649 struct jme_rxdata *rdata = xrdata;
3650 struct jme_softc *sc = rdata->jme_sc;
3651 struct ifnet *ifp = &sc->arpcom.ac_if;
3654 ASSERT_SERIALIZED(&rdata->jme_rx_serialize);
3656 CSR_WRITE_4(sc, JME_INTR_MASK_CLR,
3657 (rdata->jme_rx_coal | rdata->jme_rx_empty));
3659 status = CSR_READ_4(sc, JME_INTR_STATUS);
3660 status &= (rdata->jme_rx_coal | rdata->jme_rx_empty);
3662 if (status & rdata->jme_rx_coal)
3663 status |= (rdata->jme_rx_coal | rdata->jme_rx_comp);
3664 CSR_WRITE_4(sc, JME_INTR_STATUS, status);
3666 if (ifp->if_flags & IFF_RUNNING) {
3667 if (status & rdata->jme_rx_coal)
3668 jme_rxeof(rdata, -1);
3670 if (status & rdata->jme_rx_empty) {
3671 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr |
3672 RXCSR_RX_ENB | RXCSR_RXQ_START);
3676 CSR_WRITE_4(sc, JME_INTR_MASK_SET,
3677 (rdata->jme_rx_coal | rdata->jme_rx_empty));
3681 jme_set_msinum(struct jme_softc *sc)
3685 for (i = 0; i < JME_MSINUM_CNT; ++i)
3686 CSR_WRITE_4(sc, JME_MSINUM(i), sc->jme_msinum[i]);
3690 jme_intr_setup(device_t dev)
3692 struct jme_softc *sc = device_get_softc(dev);
3693 struct ifnet *ifp = &sc->arpcom.ac_if;
3696 if (sc->jme_irq_type == PCI_INTR_TYPE_MSIX)
3697 return jme_msix_setup(dev);
3699 error = bus_setup_intr(dev, sc->jme_irq_res, INTR_MPSAFE,
3700 jme_intr, sc, &sc->jme_irq_handle, &sc->jme_serialize);
3702 device_printf(dev, "could not set up interrupt handler.\n");
3706 ifp->if_cpuid = rman_get_cpuid(sc->jme_irq_res);
3707 KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
3712 jme_intr_teardown(device_t dev)
3714 struct jme_softc *sc = device_get_softc(dev);
3716 if (sc->jme_irq_type == PCI_INTR_TYPE_MSIX)
3717 jme_msix_teardown(dev, sc->jme_msix_cnt);
3719 bus_teardown_intr(dev, sc->jme_irq_res, sc->jme_irq_handle);
3723 jme_msix_setup(device_t dev)
3725 struct jme_softc *sc = device_get_softc(dev);
3726 struct ifnet *ifp = &sc->arpcom.ac_if;
3729 for (x = 0; x < sc->jme_msix_cnt; ++x) {
3730 struct jme_msix_data *msix = &sc->jme_msix[x];
3733 error = bus_setup_intr_descr(dev, msix->jme_msix_res,
3734 INTR_MPSAFE, msix->jme_msix_func, msix->jme_msix_arg,
3735 &msix->jme_msix_handle, msix->jme_msix_serialize,
3736 msix->jme_msix_desc);
3738 device_printf(dev, "could not set up %s "
3739 "interrupt handler.\n", msix->jme_msix_desc);
3740 jme_msix_teardown(dev, x);
3744 ifp->if_cpuid = 0; /* XXX */
3749 jme_msix_teardown(device_t dev, int msix_count)
3751 struct jme_softc *sc = device_get_softc(dev);
3754 for (x = 0; x < msix_count; ++x) {
3755 struct jme_msix_data *msix = &sc->jme_msix[x];
3757 bus_teardown_intr(dev, msix->jme_msix_res,
3758 msix->jme_msix_handle);