2 * Copyright (c) 2008 Stanislav Sedov <stas@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, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * Driver for Attansic Technology Corp. L2 FastEthernet adapter.
27 * This driver is heavily based on age(4) Attansic L1 driver by Pyun YongHyeon.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/endian.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/queue.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/sysctl.h>
46 #include <sys/taskqueue.h>
50 #include <net/if_arp.h>
51 #include <net/ethernet.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
55 #include <net/if_vlan_var.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/tcp.h>
62 #include <dev/mii/mii.h>
63 #include <dev/mii/miivar.h>
64 #include <dev/pci/pcireg.h>
65 #include <dev/pci/pcivar.h>
67 #include <machine/bus.h>
69 #include "miibus_if.h"
75 * Devices supported by this driver.
77 static struct ae_dev {
82 { VENDORID_ATTANSIC, DEVICEID_ATTANSIC_L2,
83 "Attansic Technology Corp, L2 FastEthernet" },
85 #define AE_DEVS_COUNT (sizeof(ae_devs) / sizeof(*ae_devs))
87 static struct resource_spec ae_res_spec_mem[] = {
88 { SYS_RES_MEMORY, PCIR_BAR(0), RF_ACTIVE },
91 static struct resource_spec ae_res_spec_irq[] = {
92 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE },
95 static struct resource_spec ae_res_spec_msi[] = {
96 { SYS_RES_IRQ, 1, RF_ACTIVE },
100 static int ae_probe(device_t dev);
101 static int ae_attach(device_t dev);
102 static void ae_pcie_init(ae_softc_t *sc);
103 static void ae_phy_reset(ae_softc_t *sc);
104 static void ae_phy_init(ae_softc_t *sc);
105 static int ae_reset(ae_softc_t *sc);
106 static void ae_init(void *arg);
107 static int ae_init_locked(ae_softc_t *sc);
108 static int ae_detach(device_t dev);
109 static int ae_miibus_readreg(device_t dev, int phy, int reg);
110 static int ae_miibus_writereg(device_t dev, int phy, int reg, int val);
111 static void ae_miibus_statchg(device_t dev);
112 static void ae_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr);
113 static int ae_mediachange(struct ifnet *ifp);
114 static void ae_retrieve_address(ae_softc_t *sc);
115 static void ae_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs,
117 static int ae_alloc_rings(ae_softc_t *sc);
118 static void ae_dma_free(ae_softc_t *sc);
119 static int ae_shutdown(device_t dev);
120 static int ae_suspend(device_t dev);
121 static void ae_powersave_disable(ae_softc_t *sc);
122 static void ae_powersave_enable(ae_softc_t *sc);
123 static int ae_resume(device_t dev);
124 static unsigned int ae_tx_avail_size(ae_softc_t *sc);
125 static int ae_encap(ae_softc_t *sc, struct mbuf **m_head);
126 static void ae_start(struct ifnet *ifp);
127 static void ae_start_locked(struct ifnet *ifp);
128 static void ae_link_task(void *arg, int pending);
129 static void ae_stop_rxmac(ae_softc_t *sc);
130 static void ae_stop_txmac(ae_softc_t *sc);
131 static void ae_mac_config(ae_softc_t *sc);
132 static int ae_intr(void *arg);
133 static void ae_int_task(void *arg, int pending);
134 static void ae_tx_intr(ae_softc_t *sc);
135 static int ae_rxeof(ae_softc_t *sc, ae_rxd_t *rxd);
136 static void ae_rx_intr(ae_softc_t *sc);
137 static void ae_watchdog(ae_softc_t *sc);
138 static void ae_tick(void *arg);
139 static void ae_rxfilter(ae_softc_t *sc);
140 static void ae_rxvlan(ae_softc_t *sc);
141 static int ae_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
142 static void ae_stop(ae_softc_t *sc);
143 static int ae_check_eeprom_present(ae_softc_t *sc, int *vpdc);
144 static int ae_vpd_read_word(ae_softc_t *sc, int reg, uint32_t *word);
145 static int ae_get_vpd_eaddr(ae_softc_t *sc, uint32_t *eaddr);
146 static int ae_get_reg_eaddr(ae_softc_t *sc, uint32_t *eaddr);
147 static void ae_update_stats_rx(uint16_t flags, ae_stats_t *stats);
148 static void ae_update_stats_tx(uint16_t flags, ae_stats_t *stats);
149 static void ae_init_tunables(ae_softc_t *sc);
151 static device_method_t ae_methods[] = {
152 /* Device interface. */
153 DEVMETHOD(device_probe, ae_probe),
154 DEVMETHOD(device_attach, ae_attach),
155 DEVMETHOD(device_detach, ae_detach),
156 DEVMETHOD(device_shutdown, ae_shutdown),
157 DEVMETHOD(device_suspend, ae_suspend),
158 DEVMETHOD(device_resume, ae_resume),
161 DEVMETHOD(miibus_readreg, ae_miibus_readreg),
162 DEVMETHOD(miibus_writereg, ae_miibus_writereg),
163 DEVMETHOD(miibus_statchg, ae_miibus_statchg),
167 static driver_t ae_driver = {
172 static devclass_t ae_devclass;
174 DRIVER_MODULE(ae, pci, ae_driver, ae_devclass, 0, 0);
175 DRIVER_MODULE(miibus, ae, miibus_driver, miibus_devclass, 0, 0);
176 MODULE_DEPEND(ae, pci, 1, 1, 1);
177 MODULE_DEPEND(ae, ether, 1, 1, 1);
178 MODULE_DEPEND(ae, miibus, 1, 1, 1);
183 static int msi_disable = 0;
184 TUNABLE_INT("hw.ae.msi_disable", &msi_disable);
186 #define AE_READ_4(sc, reg) \
187 bus_read_4((sc)->mem[0], (reg))
188 #define AE_READ_2(sc, reg) \
189 bus_read_2((sc)->mem[0], (reg))
190 #define AE_READ_1(sc, reg) \
191 bus_read_1((sc)->mem[0], (reg))
192 #define AE_WRITE_4(sc, reg, val) \
193 bus_write_4((sc)->mem[0], (reg), (val))
194 #define AE_WRITE_2(sc, reg, val) \
195 bus_write_2((sc)->mem[0], (reg), (val))
196 #define AE_WRITE_1(sc, reg, val) \
197 bus_write_1((sc)->mem[0], (reg), (val))
198 #define AE_PHY_READ(sc, reg) \
199 ae_miibus_readreg(sc->dev, 0, reg)
200 #define AE_PHY_WRITE(sc, reg, val) \
201 ae_miibus_writereg(sc->dev, 0, reg, val)
202 #define AE_CHECK_EADDR_VALID(eaddr) \
203 ((eaddr[0] == 0 && eaddr[1] == 0) || \
204 (eaddr[0] == 0xffffffff && eaddr[1] == 0xffff))
205 #define AE_RXD_VLAN(vtag) \
206 (((vtag) >> 4) | (((vtag) & 0x07) << 13) | (((vtag) & 0x08) << 9))
207 #define AE_TXD_VLAN(vtag) \
208 (((vtag) << 4) | (((vtag) >> 13) & 0x07) | (((vtag) >> 9) & 0x08))
211 ae_probe(device_t dev)
213 uint16_t deviceid, vendorid;
216 vendorid = pci_get_vendor(dev);
217 deviceid = pci_get_device(dev);
220 * Search through the list of supported devs for matching one.
222 for (i = 0; i < AE_DEVS_COUNT; i++) {
223 if (vendorid == ae_devs[i].vendorid &&
224 deviceid == ae_devs[i].deviceid) {
225 device_set_desc(dev, ae_devs[i].name);
226 return (BUS_PROBE_DEFAULT);
233 ae_attach(device_t dev)
242 sc = device_get_softc(dev); /* Automatically allocated and zeroed
244 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
248 * Initialize mutexes and tasks.
250 mtx_init(&sc->mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, MTX_DEF);
251 callout_init_mtx(&sc->tick_ch, &sc->mtx, 0);
252 TASK_INIT(&sc->int_task, 0, ae_int_task, sc);
253 TASK_INIT(&sc->link_task, 0, ae_link_task, sc);
255 pci_enable_busmaster(dev); /* Enable bus mastering. */
257 sc->spec_mem = ae_res_spec_mem;
260 * Allocate memory-mapped registers.
262 error = bus_alloc_resources(dev, sc->spec_mem, sc->mem);
264 device_printf(dev, "could not allocate memory resources.\n");
270 * Retrieve PCI and chip revisions.
272 pcirev = pci_get_revid(dev);
273 chiprev = (AE_READ_4(sc, AE_MASTER_REG) >> AE_MASTER_REVNUM_SHIFT) &
274 AE_MASTER_REVNUM_MASK;
276 device_printf(dev, "pci device revision: %#04x\n", pcirev);
277 device_printf(dev, "chip id: %#02x\n", chiprev);
279 nmsi = pci_msi_count(dev);
281 device_printf(dev, "MSI count: %d.\n", nmsi);
284 * Allocate interrupt resources.
286 if (msi_disable == 0 && nmsi == 1) {
287 error = pci_alloc_msi(dev, &nmsi);
289 device_printf(dev, "Using MSI messages.\n");
290 sc->spec_irq = ae_res_spec_msi;
291 error = bus_alloc_resources(dev, sc->spec_irq, sc->irq);
293 device_printf(dev, "MSI allocation failed.\n");
295 pci_release_msi(dev);
297 sc->flags |= AE_FLAG_MSI;
301 if (sc->spec_irq == NULL) {
302 sc->spec_irq = ae_res_spec_irq;
303 error = bus_alloc_resources(dev, sc->spec_irq, sc->irq);
305 device_printf(dev, "could not allocate IRQ resources.\n");
311 ae_init_tunables(sc);
313 ae_phy_reset(sc); /* Reset PHY. */
314 error = ae_reset(sc); /* Reset the controller itself. */
320 ae_retrieve_address(sc); /* Load MAC address. */
322 error = ae_alloc_rings(sc); /* Allocate ring buffers. */
326 ifp = sc->ifp = if_alloc(IFT_ETHER);
328 device_printf(dev, "could not allocate ifnet structure.\n");
334 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
335 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
336 ifp->if_ioctl = ae_ioctl;
337 ifp->if_start = ae_start;
338 ifp->if_init = ae_init;
339 ifp->if_capabilities = IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
340 ifp->if_hwassist = 0;
341 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
342 IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
343 IFQ_SET_READY(&ifp->if_snd);
344 if (pci_find_cap(dev, PCIY_PMG, &pmc) == 0) {
345 ifp->if_capabilities |= IFCAP_WOL_MAGIC;
346 sc->flags |= AE_FLAG_PMG;
348 ifp->if_capenable = ifp->if_capabilities;
351 * Configure and attach MII bus.
353 error = mii_attach(dev, &sc->miibus, ifp, ae_mediachange,
354 ae_mediastatus, BMSR_DEFCAPMASK, AE_PHYADDR_DEFAULT,
357 device_printf(dev, "attaching PHYs failed\n");
361 ether_ifattach(ifp, sc->eaddr);
362 /* Tell the upper layer(s) we support long frames. */
363 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
366 * Create and run all helper tasks.
368 sc->tq = taskqueue_create_fast("ae_taskq", M_WAITOK,
369 taskqueue_thread_enqueue, &sc->tq);
370 if (sc->tq == NULL) {
371 device_printf(dev, "could not create taskqueue.\n");
376 taskqueue_start_threads(&sc->tq, 1, PI_NET, "%s taskq",
377 device_get_nameunit(sc->dev));
380 * Configure interrupt handlers.
382 error = bus_setup_intr(dev, sc->irq[0], INTR_TYPE_NET | INTR_MPSAFE,
383 ae_intr, NULL, sc, &sc->intrhand);
385 device_printf(dev, "could not set up interrupt handler.\n");
386 taskqueue_free(sc->tq);
399 #define AE_SYSCTL(stx, parent, name, desc, ptr) \
400 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, name, CTLFLAG_RD, ptr, 0, desc)
403 ae_init_tunables(ae_softc_t *sc)
405 struct sysctl_ctx_list *ctx;
406 struct sysctl_oid *root, *stats, *stats_rx, *stats_tx;
407 struct ae_stats *ae_stats;
409 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
410 ae_stats = &sc->stats;
412 ctx = device_get_sysctl_ctx(sc->dev);
413 root = device_get_sysctl_tree(sc->dev);
414 stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(root), OID_AUTO, "stats",
415 CTLFLAG_RD, NULL, "ae statistics");
418 * Receiver statistcics.
420 stats_rx = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(stats), OID_AUTO, "rx",
421 CTLFLAG_RD, NULL, "Rx MAC statistics");
422 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "bcast",
423 "broadcast frames", &ae_stats->rx_bcast);
424 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "mcast",
425 "multicast frames", &ae_stats->rx_mcast);
426 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "pause",
427 "PAUSE frames", &ae_stats->rx_pause);
428 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "control",
429 "control frames", &ae_stats->rx_ctrl);
430 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "crc_errors",
431 "frames with CRC errors", &ae_stats->rx_crcerr);
432 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "code_errors",
433 "frames with invalid opcode", &ae_stats->rx_codeerr);
434 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "runt",
435 "runt frames", &ae_stats->rx_runt);
436 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "frag",
437 "fragmented frames", &ae_stats->rx_frag);
438 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "align_errors",
439 "frames with alignment errors", &ae_stats->rx_align);
440 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "truncated",
441 "frames truncated due to Rx FIFO inderrun", &ae_stats->rx_trunc);
444 * Receiver statistcics.
446 stats_tx = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(stats), OID_AUTO, "tx",
447 CTLFLAG_RD, NULL, "Tx MAC statistics");
448 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "bcast",
449 "broadcast frames", &ae_stats->tx_bcast);
450 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "mcast",
451 "multicast frames", &ae_stats->tx_mcast);
452 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "pause",
453 "PAUSE frames", &ae_stats->tx_pause);
454 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "control",
455 "control frames", &ae_stats->tx_ctrl);
456 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "defers",
457 "deferrals occuried", &ae_stats->tx_defer);
458 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "exc_defers",
459 "excessive deferrals occuried", &ae_stats->tx_excdefer);
460 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "singlecols",
461 "single collisions occuried", &ae_stats->tx_singlecol);
462 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "multicols",
463 "multiple collisions occuried", &ae_stats->tx_multicol);
464 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "latecols",
465 "late collisions occuried", &ae_stats->tx_latecol);
466 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "aborts",
467 "transmit aborts due collisions", &ae_stats->tx_abortcol);
468 AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "underruns",
469 "Tx FIFO underruns", &ae_stats->tx_underrun);
473 ae_pcie_init(ae_softc_t *sc)
476 AE_WRITE_4(sc, AE_PCIE_LTSSM_TESTMODE_REG, AE_PCIE_LTSSM_TESTMODE_DEFAULT);
477 AE_WRITE_4(sc, AE_PCIE_DLL_TX_CTRL_REG, AE_PCIE_DLL_TX_CTRL_DEFAULT);
481 ae_phy_reset(ae_softc_t *sc)
484 AE_WRITE_4(sc, AE_PHY_ENABLE_REG, AE_PHY_ENABLE);
485 DELAY(1000); /* XXX: pause(9) ? */
489 ae_reset(ae_softc_t *sc)
494 * Issue a soft reset.
496 AE_WRITE_4(sc, AE_MASTER_REG, AE_MASTER_SOFT_RESET);
497 bus_barrier(sc->mem[0], AE_MASTER_REG, 4,
498 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
501 * Wait for reset to complete.
503 for (i = 0; i < AE_RESET_TIMEOUT; i++) {
504 if ((AE_READ_4(sc, AE_MASTER_REG) & AE_MASTER_SOFT_RESET) == 0)
508 if (i == AE_RESET_TIMEOUT) {
509 device_printf(sc->dev, "reset timeout.\n");
514 * Wait for everything to enter idle state.
516 for (i = 0; i < AE_IDLE_TIMEOUT; i++) {
517 if (AE_READ_4(sc, AE_IDLE_REG) == 0)
521 if (i == AE_IDLE_TIMEOUT) {
522 device_printf(sc->dev, "could not enter idle state.\n");
533 sc = (ae_softc_t *)arg;
540 ae_phy_init(ae_softc_t *sc)
544 * Enable link status change interrupt.
548 AE_PHY_WRITE(sc, 18, 0xc00);
553 ae_init_locked(ae_softc_t *sc)
556 struct mii_data *mii;
557 uint8_t eaddr[ETHER_ADDR_LEN];
564 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
566 mii = device_get_softc(sc->miibus);
570 ae_pcie_init(sc); /* Initialize PCIE stuff. */
572 ae_powersave_disable(sc);
575 * Clear and disable interrupts.
577 AE_WRITE_4(sc, AE_ISR_REG, 0xffffffff);
580 * Set the MAC address.
582 bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
583 val = eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5];
584 AE_WRITE_4(sc, AE_EADDR0_REG, val);
585 val = eaddr[0] << 8 | eaddr[1];
586 AE_WRITE_4(sc, AE_EADDR1_REG, val);
588 bzero(sc->rxd_base_dma, AE_RXD_COUNT_DEFAULT * 1536 + AE_RXD_PADDING);
589 bzero(sc->txd_base, AE_TXD_BUFSIZE_DEFAULT);
590 bzero(sc->txs_base, AE_TXS_COUNT_DEFAULT * 4);
592 * Set ring buffers base addresses.
594 addr = sc->dma_rxd_busaddr;
595 AE_WRITE_4(sc, AE_DESC_ADDR_HI_REG, BUS_ADDR_HI(addr));
596 AE_WRITE_4(sc, AE_RXD_ADDR_LO_REG, BUS_ADDR_LO(addr));
597 addr = sc->dma_txd_busaddr;
598 AE_WRITE_4(sc, AE_TXD_ADDR_LO_REG, BUS_ADDR_LO(addr));
599 addr = sc->dma_txs_busaddr;
600 AE_WRITE_4(sc, AE_TXS_ADDR_LO_REG, BUS_ADDR_LO(addr));
603 * Configure ring buffers sizes.
605 AE_WRITE_2(sc, AE_RXD_COUNT_REG, AE_RXD_COUNT_DEFAULT);
606 AE_WRITE_2(sc, AE_TXD_BUFSIZE_REG, AE_TXD_BUFSIZE_DEFAULT / 4);
607 AE_WRITE_2(sc, AE_TXS_COUNT_REG, AE_TXS_COUNT_DEFAULT);
610 * Configure interframe gap parameters.
612 val = ((AE_IFG_TXIPG_DEFAULT << AE_IFG_TXIPG_SHIFT) &
614 ((AE_IFG_RXIPG_DEFAULT << AE_IFG_RXIPG_SHIFT) &
616 ((AE_IFG_IPGR1_DEFAULT << AE_IFG_IPGR1_SHIFT) &
618 ((AE_IFG_IPGR2_DEFAULT << AE_IFG_IPGR2_SHIFT) &
620 AE_WRITE_4(sc, AE_IFG_REG, val);
623 * Configure half-duplex operation.
625 val = ((AE_HDPX_LCOL_DEFAULT << AE_HDPX_LCOL_SHIFT) &
627 ((AE_HDPX_RETRY_DEFAULT << AE_HDPX_RETRY_SHIFT) &
628 AE_HDPX_RETRY_MASK) |
629 ((AE_HDPX_ABEBT_DEFAULT << AE_HDPX_ABEBT_SHIFT) &
630 AE_HDPX_ABEBT_MASK) |
631 ((AE_HDPX_JAMIPG_DEFAULT << AE_HDPX_JAMIPG_SHIFT) &
632 AE_HDPX_JAMIPG_MASK) | AE_HDPX_EXC_EN;
633 AE_WRITE_4(sc, AE_HDPX_REG, val);
636 * Configure interrupt moderate timer.
638 AE_WRITE_2(sc, AE_IMT_REG, AE_IMT_DEFAULT);
639 val = AE_READ_4(sc, AE_MASTER_REG);
640 val |= AE_MASTER_IMT_EN;
641 AE_WRITE_4(sc, AE_MASTER_REG, val);
644 * Configure interrupt clearing timer.
646 AE_WRITE_2(sc, AE_ICT_REG, AE_ICT_DEFAULT);
651 val = ifp->if_mtu + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN +
653 AE_WRITE_2(sc, AE_MTU_REG, val);
656 * Configure cut-through threshold.
658 AE_WRITE_4(sc, AE_CUT_THRESH_REG, AE_CUT_THRESH_DEFAULT);
661 * Configure flow control.
663 AE_WRITE_2(sc, AE_FLOW_THRESH_HI_REG, (AE_RXD_COUNT_DEFAULT / 8) * 7);
664 AE_WRITE_2(sc, AE_FLOW_THRESH_LO_REG, (AE_RXD_COUNT_MIN / 8) >
665 (AE_RXD_COUNT_DEFAULT / 12) ? (AE_RXD_COUNT_MIN / 8) :
666 (AE_RXD_COUNT_DEFAULT / 12));
671 sc->txd_cur = sc->rxd_cur = 0;
672 sc->txs_ack = sc->txd_ack = 0;
674 AE_WRITE_2(sc, AE_MB_TXD_IDX_REG, sc->txd_cur);
675 AE_WRITE_2(sc, AE_MB_RXD_IDX_REG, sc->rxd_cur);
677 sc->tx_inproc = 0; /* Number of packets the chip processes now. */
678 sc->flags |= AE_FLAG_TXAVAIL; /* Free Tx's available. */
683 AE_WRITE_1(sc, AE_DMAREAD_REG, AE_DMAREAD_EN);
684 AE_WRITE_1(sc, AE_DMAWRITE_REG, AE_DMAWRITE_EN);
687 * Check if everything is OK.
689 val = AE_READ_4(sc, AE_ISR_REG);
690 if ((val & AE_ISR_PHY_LINKDOWN) != 0) {
691 device_printf(sc->dev, "Initialization failed.\n");
696 * Clear interrupt status.
698 AE_WRITE_4(sc, AE_ISR_REG, 0x3fffffff);
699 AE_WRITE_4(sc, AE_ISR_REG, 0x0);
704 val = AE_READ_4(sc, AE_MASTER_REG);
705 AE_WRITE_4(sc, AE_MASTER_REG, val | AE_MASTER_MANUAL_INT);
706 AE_WRITE_4(sc, AE_IMR_REG, AE_IMR_DEFAULT);
711 AE_WRITE_4(sc, AE_WOL_REG, 0);
716 val = AE_MAC_TX_CRC_EN | AE_MAC_TX_AUTOPAD |
717 AE_MAC_FULL_DUPLEX | AE_MAC_CLK_PHY |
718 AE_MAC_TX_FLOW_EN | AE_MAC_RX_FLOW_EN |
719 ((AE_HALFBUF_DEFAULT << AE_HALFBUF_SHIFT) & AE_HALFBUF_MASK) |
720 ((AE_MAC_PREAMBLE_DEFAULT << AE_MAC_PREAMBLE_SHIFT) &
721 AE_MAC_PREAMBLE_MASK);
722 AE_WRITE_4(sc, AE_MAC_REG, val);
733 val = AE_READ_4(sc, AE_MAC_REG);
734 AE_WRITE_4(sc, AE_MAC_REG, val | AE_MAC_TX_EN | AE_MAC_RX_EN);
736 sc->flags &= ~AE_FLAG_LINK;
737 mii_mediachg(mii); /* Switch to the current media. */
739 callout_reset(&sc->tick_ch, hz, ae_tick, sc);
741 ifp->if_drv_flags |= IFF_DRV_RUNNING;
742 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
745 device_printf(sc->dev, "Initialization complete.\n");
752 ae_detach(device_t dev)
757 sc = device_get_softc(dev);
758 KASSERT(sc != NULL, ("[ae: %d]: sc is NULL", __LINE__));
760 if (device_is_attached(dev)) {
762 sc->flags |= AE_FLAG_DETACH;
765 callout_drain(&sc->tick_ch);
766 taskqueue_drain(sc->tq, &sc->int_task);
767 taskqueue_drain(taskqueue_swi, &sc->link_task);
770 if (sc->tq != NULL) {
771 taskqueue_drain(sc->tq, &sc->int_task);
772 taskqueue_free(sc->tq);
775 if (sc->miibus != NULL) {
776 device_delete_child(dev, sc->miibus);
779 bus_generic_detach(sc->dev);
781 if (sc->intrhand != NULL) {
782 bus_teardown_intr(dev, sc->irq[0], sc->intrhand);
789 if (sc->spec_irq != NULL)
790 bus_release_resources(dev, sc->spec_irq, sc->irq);
791 if (sc->spec_mem != NULL)
792 bus_release_resources(dev, sc->spec_mem, sc->mem);
793 if ((sc->flags & AE_FLAG_MSI) != 0)
794 pci_release_msi(dev);
795 mtx_destroy(&sc->mtx);
801 ae_miibus_readreg(device_t dev, int phy, int reg)
807 sc = device_get_softc(dev);
808 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
811 * Locking is done in upper layers.
814 val = ((reg << AE_MDIO_REGADDR_SHIFT) & AE_MDIO_REGADDR_MASK) |
815 AE_MDIO_START | AE_MDIO_READ | AE_MDIO_SUP_PREAMBLE |
816 ((AE_MDIO_CLK_25_4 << AE_MDIO_CLK_SHIFT) & AE_MDIO_CLK_MASK);
817 AE_WRITE_4(sc, AE_MDIO_REG, val);
820 * Wait for operation to complete.
822 for (i = 0; i < AE_MDIO_TIMEOUT; i++) {
824 val = AE_READ_4(sc, AE_MDIO_REG);
825 if ((val & (AE_MDIO_START | AE_MDIO_BUSY)) == 0)
828 if (i == AE_MDIO_TIMEOUT) {
829 device_printf(sc->dev, "phy read timeout: %d.\n", reg);
832 return ((val << AE_MDIO_DATA_SHIFT) & AE_MDIO_DATA_MASK);
836 ae_miibus_writereg(device_t dev, int phy, int reg, int val)
842 sc = device_get_softc(dev);
843 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
846 * Locking is done in upper layers.
849 aereg = ((reg << AE_MDIO_REGADDR_SHIFT) & AE_MDIO_REGADDR_MASK) |
850 AE_MDIO_START | AE_MDIO_SUP_PREAMBLE |
851 ((AE_MDIO_CLK_25_4 << AE_MDIO_CLK_SHIFT) & AE_MDIO_CLK_MASK) |
852 ((val << AE_MDIO_DATA_SHIFT) & AE_MDIO_DATA_MASK);
853 AE_WRITE_4(sc, AE_MDIO_REG, aereg);
856 * Wait for operation to complete.
858 for (i = 0; i < AE_MDIO_TIMEOUT; i++) {
860 aereg = AE_READ_4(sc, AE_MDIO_REG);
861 if ((aereg & (AE_MDIO_START | AE_MDIO_BUSY)) == 0)
864 if (i == AE_MDIO_TIMEOUT) {
865 device_printf(sc->dev, "phy write timeout: %d.\n", reg);
871 ae_miibus_statchg(device_t dev)
875 sc = device_get_softc(dev);
876 taskqueue_enqueue(taskqueue_swi, &sc->link_task);
880 ae_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
883 struct mii_data *mii;
886 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
889 mii = device_get_softc(sc->miibus);
891 ifmr->ifm_status = mii->mii_media_status;
892 ifmr->ifm_active = mii->mii_media_active;
897 ae_mediachange(struct ifnet *ifp)
900 struct mii_data *mii;
901 struct mii_softc *mii_sc;
904 /* XXX: check IFF_UP ?? */
906 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
908 mii = device_get_softc(sc->miibus);
909 LIST_FOREACH(mii_sc, &mii->mii_phys, mii_list)
911 error = mii_mediachg(mii);
918 ae_check_eeprom_present(ae_softc_t *sc, int *vpdc)
923 KASSERT(vpdc != NULL, ("[ae, %d]: vpdc is NULL!\n", __LINE__));
926 * Not sure why, but Linux does this.
928 val = AE_READ_4(sc, AE_SPICTL_REG);
929 if ((val & AE_SPICTL_VPD_EN) != 0) {
930 val &= ~AE_SPICTL_VPD_EN;
931 AE_WRITE_4(sc, AE_SPICTL_REG, val);
933 error = pci_find_cap(sc->dev, PCIY_VPD, vpdc);
938 ae_vpd_read_word(ae_softc_t *sc, int reg, uint32_t *word)
943 AE_WRITE_4(sc, AE_VPD_DATA_REG, 0); /* Clear register value. */
946 * VPD registers start at offset 0x100. Read them.
948 val = 0x100 + reg * 4;
949 AE_WRITE_4(sc, AE_VPD_CAP_REG, (val << AE_VPD_CAP_ADDR_SHIFT) &
950 AE_VPD_CAP_ADDR_MASK);
951 for (i = 0; i < AE_VPD_TIMEOUT; i++) {
953 val = AE_READ_4(sc, AE_VPD_CAP_REG);
954 if ((val & AE_VPD_CAP_DONE) != 0)
957 if (i == AE_VPD_TIMEOUT) {
958 device_printf(sc->dev, "timeout reading VPD register %d.\n",
962 *word = AE_READ_4(sc, AE_VPD_DATA_REG);
967 ae_get_vpd_eaddr(ae_softc_t *sc, uint32_t *eaddr)
969 uint32_t word, reg, val;
975 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
976 KASSERT(eaddr != NULL, ("[ae, %d]: eaddr is NULL", __LINE__));
981 error = ae_check_eeprom_present(sc, &vpdc);
986 * Read the VPD configuration space.
987 * Each register is prefixed with signature,
988 * so we can check if it is valid.
990 for (i = 0, found = 0; i < AE_VPD_NREGS; i++) {
991 error = ae_vpd_read_word(sc, i, &word);
998 if ((word & AE_VPD_SIG_MASK) != AE_VPD_SIG)
1000 reg = word >> AE_VPD_REG_SHIFT;
1001 i++; /* Move to the next word. */
1003 if (reg != AE_EADDR0_REG && reg != AE_EADDR1_REG)
1006 error = ae_vpd_read_word(sc, i, &val);
1009 if (reg == AE_EADDR0_REG)
1019 eaddr[1] &= 0xffff; /* Only last 2 bytes are used. */
1020 if (AE_CHECK_EADDR_VALID(eaddr) != 0) {
1022 device_printf(sc->dev,
1023 "VPD ethernet address registers are invalid.\n");
1030 ae_get_reg_eaddr(ae_softc_t *sc, uint32_t *eaddr)
1034 * BIOS is supposed to set this.
1036 eaddr[0] = AE_READ_4(sc, AE_EADDR0_REG);
1037 eaddr[1] = AE_READ_4(sc, AE_EADDR1_REG);
1038 eaddr[1] &= 0xffff; /* Only last 2 bytes are used. */
1040 if (AE_CHECK_EADDR_VALID(eaddr) != 0) {
1042 device_printf(sc->dev,
1043 "Ethernet address registers are invalid.\n");
1050 ae_retrieve_address(ae_softc_t *sc)
1052 uint32_t eaddr[2] = {0, 0};
1058 error = ae_get_vpd_eaddr(sc, eaddr);
1060 error = ae_get_reg_eaddr(sc, eaddr);
1063 device_printf(sc->dev,
1064 "Generating random ethernet address.\n");
1065 eaddr[0] = arc4random();
1068 * Set OUI to ASUSTek COMPUTER INC.
1070 sc->eaddr[0] = 0x02; /* U/L bit set. */
1071 sc->eaddr[1] = 0x1f;
1072 sc->eaddr[2] = 0xc6;
1073 sc->eaddr[3] = (eaddr[0] >> 16) & 0xff;
1074 sc->eaddr[4] = (eaddr[0] >> 8) & 0xff;
1075 sc->eaddr[5] = (eaddr[0] >> 0) & 0xff;
1077 sc->eaddr[0] = (eaddr[1] >> 8) & 0xff;
1078 sc->eaddr[1] = (eaddr[1] >> 0) & 0xff;
1079 sc->eaddr[2] = (eaddr[0] >> 24) & 0xff;
1080 sc->eaddr[3] = (eaddr[0] >> 16) & 0xff;
1081 sc->eaddr[4] = (eaddr[0] >> 8) & 0xff;
1082 sc->eaddr[5] = (eaddr[0] >> 0) & 0xff;
1087 ae_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1089 bus_addr_t *addr = arg;
1093 KASSERT(nsegs == 1, ("[ae, %d]: %d segments instead of 1!", __LINE__,
1095 *addr = segs[0].ds_addr;
1099 ae_alloc_rings(ae_softc_t *sc)
1105 * Create parent DMA tag.
1107 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev),
1108 1, 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
1109 NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
1110 BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
1111 &sc->dma_parent_tag);
1113 device_printf(sc->dev, "could not creare parent DMA tag.\n");
1118 * Create DMA tag for TxD.
1120 error = bus_dma_tag_create(sc->dma_parent_tag,
1121 8, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1122 NULL, NULL, AE_TXD_BUFSIZE_DEFAULT, 1,
1123 AE_TXD_BUFSIZE_DEFAULT, 0, NULL, NULL,
1126 device_printf(sc->dev, "could not creare TxD DMA tag.\n");
1131 * Create DMA tag for TxS.
1133 error = bus_dma_tag_create(sc->dma_parent_tag,
1134 8, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1135 NULL, NULL, AE_TXS_COUNT_DEFAULT * 4, 1,
1136 AE_TXS_COUNT_DEFAULT * 4, 0, NULL, NULL,
1139 device_printf(sc->dev, "could not creare TxS DMA tag.\n");
1144 * Create DMA tag for RxD.
1146 error = bus_dma_tag_create(sc->dma_parent_tag,
1147 128, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1148 NULL, NULL, AE_RXD_COUNT_DEFAULT * 1536 + AE_RXD_PADDING, 1,
1149 AE_RXD_COUNT_DEFAULT * 1536 + AE_RXD_PADDING, 0, NULL, NULL,
1152 device_printf(sc->dev, "could not creare TxS DMA tag.\n");
1157 * Allocate TxD DMA memory.
1159 error = bus_dmamem_alloc(sc->dma_txd_tag, (void **)&sc->txd_base,
1160 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1163 device_printf(sc->dev,
1164 "could not allocate DMA memory for TxD ring.\n");
1167 error = bus_dmamap_load(sc->dma_txd_tag, sc->dma_txd_map, sc->txd_base,
1168 AE_TXD_BUFSIZE_DEFAULT, ae_dmamap_cb, &busaddr, BUS_DMA_NOWAIT);
1169 if (error != 0 || busaddr == 0) {
1170 device_printf(sc->dev,
1171 "could not load DMA map for TxD ring.\n");
1174 sc->dma_txd_busaddr = busaddr;
1177 * Allocate TxS DMA memory.
1179 error = bus_dmamem_alloc(sc->dma_txs_tag, (void **)&sc->txs_base,
1180 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1183 device_printf(sc->dev,
1184 "could not allocate DMA memory for TxS ring.\n");
1187 error = bus_dmamap_load(sc->dma_txs_tag, sc->dma_txs_map, sc->txs_base,
1188 AE_TXS_COUNT_DEFAULT * 4, ae_dmamap_cb, &busaddr, BUS_DMA_NOWAIT);
1189 if (error != 0 || busaddr == 0) {
1190 device_printf(sc->dev,
1191 "could not load DMA map for TxS ring.\n");
1194 sc->dma_txs_busaddr = busaddr;
1197 * Allocate RxD DMA memory.
1199 error = bus_dmamem_alloc(sc->dma_rxd_tag, (void **)&sc->rxd_base_dma,
1200 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1203 device_printf(sc->dev,
1204 "could not allocate DMA memory for RxD ring.\n");
1207 error = bus_dmamap_load(sc->dma_rxd_tag, sc->dma_rxd_map,
1208 sc->rxd_base_dma, AE_RXD_COUNT_DEFAULT * 1536 + AE_RXD_PADDING,
1209 ae_dmamap_cb, &busaddr, BUS_DMA_NOWAIT);
1210 if (error != 0 || busaddr == 0) {
1211 device_printf(sc->dev,
1212 "could not load DMA map for RxD ring.\n");
1215 sc->dma_rxd_busaddr = busaddr + AE_RXD_PADDING;
1216 sc->rxd_base = (ae_rxd_t *)(sc->rxd_base_dma + AE_RXD_PADDING);
1222 ae_dma_free(ae_softc_t *sc)
1225 if (sc->dma_txd_tag != NULL) {
1226 if (sc->dma_txd_map != NULL) {
1227 bus_dmamap_unload(sc->dma_txd_tag, sc->dma_txd_map);
1228 if (sc->txd_base != NULL)
1229 bus_dmamem_free(sc->dma_txd_tag, sc->txd_base,
1233 bus_dma_tag_destroy(sc->dma_txd_tag);
1234 sc->dma_txd_map = NULL;
1235 sc->dma_txd_tag = NULL;
1236 sc->txd_base = NULL;
1238 if (sc->dma_txs_tag != NULL) {
1239 if (sc->dma_txs_map != NULL) {
1240 bus_dmamap_unload(sc->dma_txs_tag, sc->dma_txs_map);
1241 if (sc->txs_base != NULL)
1242 bus_dmamem_free(sc->dma_txs_tag, sc->txs_base,
1246 bus_dma_tag_destroy(sc->dma_txs_tag);
1247 sc->dma_txs_map = NULL;
1248 sc->dma_txs_tag = NULL;
1249 sc->txs_base = NULL;
1251 if (sc->dma_rxd_tag != NULL) {
1252 if (sc->dma_rxd_map != NULL) {
1253 bus_dmamap_unload(sc->dma_rxd_tag, sc->dma_rxd_map);
1254 if (sc->rxd_base_dma != NULL)
1255 bus_dmamem_free(sc->dma_rxd_tag,
1256 sc->rxd_base_dma, sc->dma_rxd_map);
1259 bus_dma_tag_destroy(sc->dma_rxd_tag);
1260 sc->dma_rxd_map = NULL;
1261 sc->dma_rxd_tag = NULL;
1262 sc->rxd_base_dma = NULL;
1264 if (sc->dma_parent_tag != NULL) {
1265 bus_dma_tag_destroy(sc->dma_parent_tag);
1266 sc->dma_parent_tag = NULL;
1271 ae_shutdown(device_t dev)
1276 sc = device_get_softc(dev);
1277 KASSERT(sc != NULL, ("[ae: %d]: sc is NULL", __LINE__));
1279 error = ae_suspend(dev);
1281 ae_powersave_enable(sc);
1287 ae_powersave_disable(ae_softc_t *sc)
1293 AE_PHY_WRITE(sc, AE_PHY_DBG_ADDR, 0);
1294 val = AE_PHY_READ(sc, AE_PHY_DBG_DATA);
1295 if (val & AE_PHY_DBG_POWERSAVE) {
1296 val &= ~AE_PHY_DBG_POWERSAVE;
1297 AE_PHY_WRITE(sc, AE_PHY_DBG_DATA, val);
1303 ae_powersave_enable(ae_softc_t *sc)
1310 * XXX magic numbers.
1312 AE_PHY_WRITE(sc, AE_PHY_DBG_ADDR, 0);
1313 val = AE_PHY_READ(sc, AE_PHY_DBG_DATA);
1314 AE_PHY_WRITE(sc, AE_PHY_DBG_ADDR, val | 0x1000);
1315 AE_PHY_WRITE(sc, AE_PHY_DBG_ADDR, 2);
1316 AE_PHY_WRITE(sc, AE_PHY_DBG_DATA, 0x3000);
1317 AE_PHY_WRITE(sc, AE_PHY_DBG_ADDR, 3);
1318 AE_PHY_WRITE(sc, AE_PHY_DBG_DATA, 0);
1322 ae_pm_init(ae_softc_t *sc)
1327 struct mii_data *mii;
1333 if ((sc->flags & AE_FLAG_PMG) == 0) {
1334 /* Disable WOL entirely. */
1335 AE_WRITE_4(sc, AE_WOL_REG, 0);
1340 * Configure WOL if enabled.
1342 if ((ifp->if_capenable & IFCAP_WOL) != 0) {
1343 mii = device_get_softc(sc->miibus);
1345 if ((mii->mii_media_status & IFM_AVALID) != 0 &&
1346 (mii->mii_media_status & IFM_ACTIVE) != 0) {
1347 AE_WRITE_4(sc, AE_WOL_REG, AE_WOL_MAGIC | \
1353 val = AE_MAC_RX_EN | AE_MAC_CLK_PHY | \
1354 AE_MAC_TX_CRC_EN | AE_MAC_TX_AUTOPAD | \
1355 ((AE_HALFBUF_DEFAULT << AE_HALFBUF_SHIFT) & \
1356 AE_HALFBUF_MASK) | \
1357 ((AE_MAC_PREAMBLE_DEFAULT << \
1358 AE_MAC_PREAMBLE_SHIFT) & AE_MAC_PREAMBLE_MASK) | \
1359 AE_MAC_BCAST_EN | AE_MAC_MCAST_EN;
1360 if ((IFM_OPTIONS(mii->mii_media_active) & \
1362 val |= AE_MAC_FULL_DUPLEX;
1363 AE_WRITE_4(sc, AE_MAC_REG, val);
1365 } else { /* No link. */
1366 AE_WRITE_4(sc, AE_WOL_REG, AE_WOL_LNKCHG | \
1368 AE_WRITE_4(sc, AE_MAC_REG, 0);
1371 ae_powersave_enable(sc);
1375 * PCIE hacks. Magic numbers.
1377 val = AE_READ_4(sc, AE_PCIE_PHYMISC_REG);
1378 val |= AE_PCIE_PHYMISC_FORCE_RCV_DET;
1379 AE_WRITE_4(sc, AE_PCIE_PHYMISC_REG, val);
1380 val = AE_READ_4(sc, AE_PCIE_DLL_TX_CTRL_REG);
1381 val |= AE_PCIE_DLL_TX_CTRL_SEL_NOR_CLK;
1382 AE_WRITE_4(sc, AE_PCIE_DLL_TX_CTRL_REG, val);
1387 if (pci_find_cap(sc->dev, PCIY_PMG, &pmc) == 0) {
1388 pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
1389 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
1390 if ((ifp->if_capenable & IFCAP_WOL) != 0)
1391 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
1392 pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1397 ae_suspend(device_t dev)
1401 sc = device_get_softc(dev);
1412 ae_resume(device_t dev)
1416 sc = device_get_softc(dev);
1417 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
1420 AE_READ_4(sc, AE_WOL_REG); /* Clear WOL status. */
1421 if ((sc->ifp->if_flags & IFF_UP) != 0)
1429 ae_tx_avail_size(ae_softc_t *sc)
1433 if (sc->txd_cur >= sc->txd_ack)
1434 avail = AE_TXD_BUFSIZE_DEFAULT - (sc->txd_cur - sc->txd_ack);
1436 avail = sc->txd_ack - sc->txd_cur;
1442 ae_encap(ae_softc_t *sc, struct mbuf **m_head)
1446 unsigned int to_end;
1452 len = m0->m_pkthdr.len;
1454 if ((sc->flags & AE_FLAG_TXAVAIL) == 0 ||
1455 len + sizeof(ae_txd_t) + 3 > ae_tx_avail_size(sc)) {
1457 if_printf(sc->ifp, "No free Tx available.\n");
1462 hdr = (ae_txd_t *)(sc->txd_base + sc->txd_cur);
1463 bzero(hdr, sizeof(*hdr));
1464 /* Skip header size. */
1465 sc->txd_cur = (sc->txd_cur + sizeof(ae_txd_t)) % AE_TXD_BUFSIZE_DEFAULT;
1466 /* Space available to the end of the ring */
1467 to_end = AE_TXD_BUFSIZE_DEFAULT - sc->txd_cur;
1468 if (to_end >= len) {
1469 m_copydata(m0, 0, len, (caddr_t)(sc->txd_base + sc->txd_cur));
1471 m_copydata(m0, 0, to_end, (caddr_t)(sc->txd_base +
1473 m_copydata(m0, to_end, len - to_end, (caddr_t)sc->txd_base);
1477 * Set TxD flags and parameters.
1479 if ((m0->m_flags & M_VLANTAG) != 0) {
1480 hdr->vlan = htole16(AE_TXD_VLAN(m0->m_pkthdr.ether_vtag));
1481 hdr->len = htole16(len | AE_TXD_INSERT_VTAG);
1483 hdr->len = htole16(len);
1487 * Set current TxD position and round up to a 4-byte boundary.
1489 sc->txd_cur = ((sc->txd_cur + len + 3) & ~3) % AE_TXD_BUFSIZE_DEFAULT;
1490 if (sc->txd_cur == sc->txd_ack)
1491 sc->flags &= ~AE_FLAG_TXAVAIL;
1493 if_printf(sc->ifp, "New txd_cur = %d.\n", sc->txd_cur);
1497 * Update TxS position and check if there are empty TxS available.
1499 sc->txs_base[sc->txs_cur].flags &= ~htole16(AE_TXS_UPDATE);
1500 sc->txs_cur = (sc->txs_cur + 1) % AE_TXS_COUNT_DEFAULT;
1501 if (sc->txs_cur == sc->txs_ack)
1502 sc->flags &= ~AE_FLAG_TXAVAIL;
1505 * Synchronize DMA memory.
1507 bus_dmamap_sync(sc->dma_txd_tag, sc->dma_txd_map, BUS_DMASYNC_PREREAD |
1508 BUS_DMASYNC_PREWRITE);
1509 bus_dmamap_sync(sc->dma_txs_tag, sc->dma_txs_map,
1510 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1516 ae_start(struct ifnet *ifp)
1522 ae_start_locked(ifp);
1527 ae_start_locked(struct ifnet *ifp)
1535 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
1539 if_printf(ifp, "Start called.\n");
1542 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1543 IFF_DRV_RUNNING || (sc->flags & AE_FLAG_LINK) == 0)
1547 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
1548 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
1550 break; /* Nothing to do. */
1552 error = ae_encap(sc, &m0);
1555 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
1556 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1558 if_printf(ifp, "Setting OACTIVE.\n");
1566 /* Bounce a copy of the frame to BPF. */
1567 ETHER_BPF_MTAP(ifp, m0);
1572 if (count > 0) { /* Something was dequeued. */
1573 AE_WRITE_2(sc, AE_MB_TXD_IDX_REG, sc->txd_cur / 4);
1574 sc->wd_timer = AE_TX_TIMEOUT; /* Load watchdog. */
1576 if_printf(ifp, "%d packets dequeued.\n", count);
1577 if_printf(ifp, "Tx pos now is %d.\n", sc->txd_cur);
1583 ae_link_task(void *arg, int pending)
1586 struct mii_data *mii;
1590 sc = (ae_softc_t *)arg;
1591 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
1595 mii = device_get_softc(sc->miibus);
1596 if (mii == NULL || ifp == NULL ||
1597 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1598 AE_UNLOCK(sc); /* XXX: could happen? */
1602 sc->flags &= ~AE_FLAG_LINK;
1603 if ((mii->mii_media_status & (IFM_AVALID | IFM_ACTIVE)) ==
1604 (IFM_AVALID | IFM_ACTIVE)) {
1605 switch(IFM_SUBTYPE(mii->mii_media_active)) {
1608 sc->flags |= AE_FLAG_LINK;
1621 if ((sc->flags & AE_FLAG_LINK) != 0) {
1625 * Restart DMA engines.
1627 AE_WRITE_1(sc, AE_DMAREAD_REG, AE_DMAREAD_EN);
1628 AE_WRITE_1(sc, AE_DMAWRITE_REG, AE_DMAWRITE_EN);
1631 * Enable Rx and Tx MACs.
1633 val = AE_READ_4(sc, AE_MAC_REG);
1634 val |= AE_MAC_TX_EN | AE_MAC_RX_EN;
1635 AE_WRITE_4(sc, AE_MAC_REG, val);
1641 ae_stop_rxmac(ae_softc_t *sc)
1649 * Stop Rx MAC engine.
1651 val = AE_READ_4(sc, AE_MAC_REG);
1652 if ((val & AE_MAC_RX_EN) != 0) {
1653 val &= ~AE_MAC_RX_EN;
1654 AE_WRITE_4(sc, AE_MAC_REG, val);
1658 * Stop Rx DMA engine.
1660 if (AE_READ_1(sc, AE_DMAWRITE_REG) == AE_DMAWRITE_EN)
1661 AE_WRITE_1(sc, AE_DMAWRITE_REG, 0);
1664 * Wait for IDLE state.
1666 for (i = 0; i < AE_IDLE_TIMEOUT; i--) {
1667 val = AE_READ_4(sc, AE_IDLE_REG);
1668 if ((val & (AE_IDLE_RXMAC | AE_IDLE_DMAWRITE)) == 0)
1672 if (i == AE_IDLE_TIMEOUT)
1673 device_printf(sc->dev, "timed out while stopping Rx MAC.\n");
1677 ae_stop_txmac(ae_softc_t *sc)
1685 * Stop Tx MAC engine.
1687 val = AE_READ_4(sc, AE_MAC_REG);
1688 if ((val & AE_MAC_TX_EN) != 0) {
1689 val &= ~AE_MAC_TX_EN;
1690 AE_WRITE_4(sc, AE_MAC_REG, val);
1694 * Stop Tx DMA engine.
1696 if (AE_READ_1(sc, AE_DMAREAD_REG) == AE_DMAREAD_EN)
1697 AE_WRITE_1(sc, AE_DMAREAD_REG, 0);
1700 * Wait for IDLE state.
1702 for (i = 0; i < AE_IDLE_TIMEOUT; i--) {
1703 val = AE_READ_4(sc, AE_IDLE_REG);
1704 if ((val & (AE_IDLE_TXMAC | AE_IDLE_DMAREAD)) == 0)
1708 if (i == AE_IDLE_TIMEOUT)
1709 device_printf(sc->dev, "timed out while stopping Tx MAC.\n");
1713 ae_mac_config(ae_softc_t *sc)
1715 struct mii_data *mii;
1720 mii = device_get_softc(sc->miibus);
1721 val = AE_READ_4(sc, AE_MAC_REG);
1722 val &= ~AE_MAC_FULL_DUPLEX;
1723 /* XXX disable AE_MAC_TX_FLOW_EN? */
1725 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
1726 val |= AE_MAC_FULL_DUPLEX;
1728 AE_WRITE_4(sc, AE_MAC_REG, val);
1737 sc = (ae_softc_t *)arg;
1738 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
1740 val = AE_READ_4(sc, AE_ISR_REG);
1741 if (val == 0 || (val & AE_IMR_DEFAULT) == 0)
1742 return (FILTER_STRAY);
1744 /* Disable interrupts. */
1745 AE_WRITE_4(sc, AE_ISR_REG, AE_ISR_DISABLE);
1747 /* Schedule interrupt processing. */
1748 taskqueue_enqueue(sc->tq, &sc->int_task);
1750 return (FILTER_HANDLED);
1754 ae_int_task(void *arg, int pending)
1760 sc = (ae_softc_t *)arg;
1766 val = AE_READ_4(sc, AE_ISR_REG); /* Read interrupt status. */
1773 * Clear interrupts and disable them.
1775 AE_WRITE_4(sc, AE_ISR_REG, val | AE_ISR_DISABLE);
1778 if_printf(ifp, "Interrupt received: 0x%08x\n", val);
1781 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1782 if ((val & (AE_ISR_DMAR_TIMEOUT | AE_ISR_DMAW_TIMEOUT |
1783 AE_ISR_PHY_LINKDOWN)) != 0) {
1784 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1789 if ((val & AE_ISR_TX_EVENT) != 0)
1791 if ((val & AE_ISR_RX_EVENT) != 0)
1794 * Re-enable interrupts.
1796 AE_WRITE_4(sc, AE_ISR_REG, 0);
1798 if ((sc->flags & AE_FLAG_TXAVAIL) != 0) {
1799 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1800 ae_start_locked(ifp);
1808 ae_tx_intr(ae_softc_t *sc)
1820 if_printf(ifp, "Tx interrupt occuried.\n");
1824 * Syncronize DMA buffers.
1826 bus_dmamap_sync(sc->dma_txd_tag, sc->dma_txd_map,
1827 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1828 bus_dmamap_sync(sc->dma_txs_tag, sc->dma_txs_map,
1829 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1832 txs = sc->txs_base + sc->txs_ack;
1833 flags = le16toh(txs->flags);
1834 if ((flags & AE_TXS_UPDATE) == 0)
1836 txs->flags = htole16(flags & ~AE_TXS_UPDATE);
1838 ae_update_stats_tx(flags, &sc->stats);
1841 * Update TxS position.
1843 sc->txs_ack = (sc->txs_ack + 1) % AE_TXS_COUNT_DEFAULT;
1844 sc->flags |= AE_FLAG_TXAVAIL;
1846 txd = (ae_txd_t *)(sc->txd_base + sc->txd_ack);
1847 if (txs->len != txd->len)
1848 device_printf(sc->dev, "Size mismatch: TxS:%d TxD:%d\n",
1849 le16toh(txs->len), le16toh(txd->len));
1852 * Move txd ack and align on 4-byte boundary.
1854 sc->txd_ack = ((sc->txd_ack + le16toh(txd->len) +
1855 sizeof(ae_txs_t) + 3) & ~3) % AE_TXD_BUFSIZE_DEFAULT;
1857 if ((flags & AE_TXS_SUCCESS) != 0)
1865 if ((sc->flags & AE_FLAG_TXAVAIL) != 0)
1866 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1867 if (sc->tx_inproc < 0) {
1868 if_printf(ifp, "Received stray Tx interrupt(s).\n");
1872 if (sc->tx_inproc == 0)
1873 sc->wd_timer = 0; /* Unarm watchdog. */
1876 * Syncronize DMA buffers.
1878 bus_dmamap_sync(sc->dma_txd_tag, sc->dma_txd_map,
1879 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1880 bus_dmamap_sync(sc->dma_txs_tag, sc->dma_txs_map,
1881 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1885 ae_rxeof(ae_softc_t *sc, ae_rxd_t *rxd)
1895 flags = le16toh(rxd->flags);
1898 if_printf(ifp, "Rx interrupt occuried.\n");
1900 size = le16toh(rxd->len) - ETHER_CRC_LEN;
1901 if (size < (ETHER_MIN_LEN - ETHER_CRC_LEN - ETHER_VLAN_ENCAP_LEN)) {
1902 if_printf(ifp, "Runt frame received.");
1906 m = m_devget(&rxd->data[0], size, ETHER_ALIGN, ifp, NULL);
1910 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
1911 (flags & AE_RXD_HAS_VLAN) != 0) {
1912 m->m_pkthdr.ether_vtag = AE_RXD_VLAN(le16toh(rxd->vlan));
1913 m->m_flags |= M_VLANTAG;
1920 (*ifp->if_input)(ifp, m);
1927 ae_rx_intr(ae_softc_t *sc)
1934 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL!", __LINE__));
1941 * Syncronize DMA buffers.
1943 bus_dmamap_sync(sc->dma_rxd_tag, sc->dma_rxd_map,
1944 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1946 for (count = 0;; count++) {
1947 rxd = (ae_rxd_t *)(sc->rxd_base + sc->rxd_cur);
1948 flags = le16toh(rxd->flags);
1949 if ((flags & AE_RXD_UPDATE) == 0)
1951 rxd->flags = htole16(flags & ~AE_RXD_UPDATE);
1953 ae_update_stats_rx(flags, &sc->stats);
1956 * Update position index.
1958 sc->rxd_cur = (sc->rxd_cur + 1) % AE_RXD_COUNT_DEFAULT;
1960 if ((flags & AE_RXD_SUCCESS) == 0) {
1964 error = ae_rxeof(sc, rxd);
1974 bus_dmamap_sync(sc->dma_rxd_tag, sc->dma_rxd_map,
1975 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1979 AE_WRITE_2(sc, AE_MB_RXD_IDX_REG, sc->rxd_cur);
1984 ae_watchdog(ae_softc_t *sc)
1988 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL!", __LINE__));
1992 if (sc->wd_timer == 0 || --sc->wd_timer != 0)
1993 return; /* Noting to do. */
1995 if ((sc->flags & AE_FLAG_LINK) == 0)
1996 if_printf(ifp, "watchdog timeout (missed link).\n");
1998 if_printf(ifp, "watchdog timeout - resetting.\n");
2001 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2003 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2004 ae_start_locked(ifp);
2011 struct mii_data *mii;
2013 sc = (ae_softc_t *)arg;
2014 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL!", __LINE__));
2017 mii = device_get_softc(sc->miibus);
2019 ae_watchdog(sc); /* Watchdog check. */
2020 callout_reset(&sc->tick_ch, hz, ae_tick, sc);
2024 ae_rxvlan(ae_softc_t *sc)
2031 val = AE_READ_4(sc, AE_MAC_REG);
2032 val &= ~AE_MAC_RMVLAN_EN;
2033 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
2034 val |= AE_MAC_RMVLAN_EN;
2035 AE_WRITE_4(sc, AE_MAC_REG, val);
2039 ae_rxfilter(ae_softc_t *sc)
2042 struct ifmultiaddr *ifma;
2047 KASSERT(sc != NULL, ("[ae, %d]: sc is NULL!", __LINE__));
2053 rxcfg = AE_READ_4(sc, AE_MAC_REG);
2054 rxcfg &= ~(AE_MAC_MCAST_EN | AE_MAC_BCAST_EN | AE_MAC_PROMISC_EN);
2056 if ((ifp->if_flags & IFF_BROADCAST) != 0)
2057 rxcfg |= AE_MAC_BCAST_EN;
2058 if ((ifp->if_flags & IFF_PROMISC) != 0)
2059 rxcfg |= AE_MAC_PROMISC_EN;
2060 if ((ifp->if_flags & IFF_ALLMULTI) != 0)
2061 rxcfg |= AE_MAC_MCAST_EN;
2064 * Wipe old settings.
2066 AE_WRITE_4(sc, AE_REG_MHT0, 0);
2067 AE_WRITE_4(sc, AE_REG_MHT1, 0);
2068 if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
2069 AE_WRITE_4(sc, AE_REG_MHT0, 0xffffffff);
2070 AE_WRITE_4(sc, AE_REG_MHT1, 0xffffffff);
2071 AE_WRITE_4(sc, AE_MAC_REG, rxcfg);
2076 * Load multicast tables.
2078 bzero(mchash, sizeof(mchash));
2079 if_maddr_rlock(ifp);
2080 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2081 if (ifma->ifma_addr->sa_family != AF_LINK)
2083 crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
2084 ifma->ifma_addr), ETHER_ADDR_LEN);
2085 mchash[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
2087 if_maddr_runlock(ifp);
2088 AE_WRITE_4(sc, AE_REG_MHT0, mchash[0]);
2089 AE_WRITE_4(sc, AE_REG_MHT1, mchash[1]);
2090 AE_WRITE_4(sc, AE_MAC_REG, rxcfg);
2094 ae_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2096 struct ae_softc *sc;
2098 struct mii_data *mii;
2102 ifr = (struct ifreq *)data;
2107 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
2109 else if (ifp->if_mtu != ifr->ifr_mtu) {
2111 ifp->if_mtu = ifr->ifr_mtu;
2112 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2113 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2121 if ((ifp->if_flags & IFF_UP) != 0) {
2122 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2123 if (((ifp->if_flags ^ sc->if_flags)
2124 & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
2127 if ((sc->flags & AE_FLAG_DETACH) == 0)
2131 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2134 sc->if_flags = ifp->if_flags;
2140 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2146 mii = device_get_softc(sc->miibus);
2147 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
2151 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2152 if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
2153 (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
2154 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2157 VLAN_CAPABILITIES(ifp);
2161 error = ether_ioctl(ifp, cmd, data);
2168 ae_stop(ae_softc_t *sc)
2176 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2177 sc->flags &= ~AE_FLAG_LINK;
2178 sc->wd_timer = 0; /* Cancel watchdog. */
2179 callout_stop(&sc->tick_ch);
2182 * Clear and disable interrupts.
2184 AE_WRITE_4(sc, AE_IMR_REG, 0);
2185 AE_WRITE_4(sc, AE_ISR_REG, 0xffffffff);
2196 AE_WRITE_1(sc, AE_DMAREAD_REG, ~AE_DMAREAD_EN);
2197 AE_WRITE_1(sc, AE_DMAWRITE_REG, ~AE_DMAWRITE_EN);
2200 * Wait for everything to enter idle state.
2202 for (i = 0; i < AE_IDLE_TIMEOUT; i++) {
2203 if (AE_READ_4(sc, AE_IDLE_REG) == 0)
2207 if (i == AE_IDLE_TIMEOUT)
2208 device_printf(sc->dev, "could not enter idle state in stop.\n");
2212 ae_update_stats_tx(uint16_t flags, ae_stats_t *stats)
2215 if ((flags & AE_TXS_BCAST) != 0)
2217 if ((flags & AE_TXS_MCAST) != 0)
2219 if ((flags & AE_TXS_PAUSE) != 0)
2221 if ((flags & AE_TXS_CTRL) != 0)
2223 if ((flags & AE_TXS_DEFER) != 0)
2225 if ((flags & AE_TXS_EXCDEFER) != 0)
2226 stats->tx_excdefer++;
2227 if ((flags & AE_TXS_SINGLECOL) != 0)
2228 stats->tx_singlecol++;
2229 if ((flags & AE_TXS_MULTICOL) != 0)
2230 stats->tx_multicol++;
2231 if ((flags & AE_TXS_LATECOL) != 0)
2232 stats->tx_latecol++;
2233 if ((flags & AE_TXS_ABORTCOL) != 0)
2234 stats->tx_abortcol++;
2235 if ((flags & AE_TXS_UNDERRUN) != 0)
2236 stats->tx_underrun++;
2240 ae_update_stats_rx(uint16_t flags, ae_stats_t *stats)
2243 if ((flags & AE_RXD_BCAST) != 0)
2245 if ((flags & AE_RXD_MCAST) != 0)
2247 if ((flags & AE_RXD_PAUSE) != 0)
2249 if ((flags & AE_RXD_CTRL) != 0)
2251 if ((flags & AE_RXD_CRCERR) != 0)
2253 if ((flags & AE_RXD_CODEERR) != 0)
2254 stats->rx_codeerr++;
2255 if ((flags & AE_RXD_RUNT) != 0)
2257 if ((flags & AE_RXD_FRAG) != 0)
2259 if ((flags & AE_RXD_TRUNC) != 0)
2261 if ((flags & AE_RXD_ALIGN) != 0)