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/ale/if_ale.c,v 1.3 2008/12/03 09:01:12 yongari Exp $
30 /* Driver for Atheros AR8121/AR8113/AR8114 PCIe Ethernet. */
32 #include <sys/param.h>
33 #include <sys/endian.h>
34 #include <sys/kernel.h>
36 #include <sys/interrupt.h>
37 #include <sys/malloc.h>
40 #include <sys/serialize.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/sysctl.h>
45 #include <net/ethernet.h>
48 #include <net/if_arp.h>
49 #include <net/if_dl.h>
50 #include <net/if_llc.h>
51 #include <net/if_media.h>
52 #include <net/ifq_var.h>
53 #include <net/vlan/if_vlan_var.h>
54 #include <net/vlan/if_vlan_ether.h>
56 #include <netinet/ip.h>
58 #include <dev/netif/mii_layer/mii.h>
59 #include <dev/netif/mii_layer/miivar.h>
61 #include <bus/pci/pcireg.h>
62 #include <bus/pci/pcivar.h>
65 #include <dev/netif/ale/if_alereg.h>
66 #include <dev/netif/ale/if_alevar.h>
68 /* "device miibus" required. See GENERIC if you get errors here. */
69 #include "miibus_if.h"
71 /* For more information about Tx checksum offload issues see ale_encap(). */
72 #define ALE_CSUM_FEATURES (CSUM_TCP | CSUM_UDP)
74 struct ale_dmamap_ctx
{
76 bus_dma_segment_t
*segs
;
79 static int ale_probe(device_t
);
80 static int ale_attach(device_t
);
81 static int ale_detach(device_t
);
82 static int ale_shutdown(device_t
);
83 static int ale_suspend(device_t
);
84 static int ale_resume(device_t
);
86 static int ale_miibus_readreg(device_t
, int, int);
87 static int ale_miibus_writereg(device_t
, int, int, int);
88 static void ale_miibus_statchg(device_t
);
90 static void ale_init(void *);
91 static void ale_start(struct ifnet
*, struct ifaltq_subque
*);
92 static int ale_ioctl(struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
93 static void ale_watchdog(struct ifnet
*);
94 static int ale_mediachange(struct ifnet
*);
95 static void ale_mediastatus(struct ifnet
*, struct ifmediareq
*);
97 static void ale_intr(void *);
98 static int ale_rxeof(struct ale_softc
*sc
);
99 static void ale_rx_update_page(struct ale_softc
*, struct ale_rx_page
**,
100 uint32_t, uint32_t *);
101 static void ale_rxcsum(struct ale_softc
*, struct mbuf
*, uint32_t);
102 static void ale_txeof(struct ale_softc
*);
104 static int ale_dma_alloc(struct ale_softc
*);
105 static void ale_dma_free(struct ale_softc
*);
106 static int ale_check_boundary(struct ale_softc
*);
107 static void ale_dmamap_cb(void *, bus_dma_segment_t
*, int, int);
108 static void ale_dmamap_buf_cb(void *, bus_dma_segment_t
*, int,
110 static int ale_encap(struct ale_softc
*, struct mbuf
**);
111 static void ale_init_rx_pages(struct ale_softc
*);
112 static void ale_init_tx_ring(struct ale_softc
*);
114 static void ale_stop(struct ale_softc
*);
115 static void ale_tick(void *);
116 static void ale_get_macaddr(struct ale_softc
*);
117 static void ale_mac_config(struct ale_softc
*);
118 static void ale_phy_reset(struct ale_softc
*);
119 static void ale_reset(struct ale_softc
*);
120 static void ale_rxfilter(struct ale_softc
*);
121 static void ale_rxvlan(struct ale_softc
*);
122 static void ale_stats_clear(struct ale_softc
*);
123 static void ale_stats_update(struct ale_softc
*);
124 static void ale_stop_mac(struct ale_softc
*);
126 static void ale_setlinkspeed(struct ale_softc
*);
127 static void ale_setwol(struct ale_softc
*);
130 static void ale_sysctl_node(struct ale_softc
*);
131 static int sysctl_hw_ale_int_mod(SYSCTL_HANDLER_ARGS
);
134 * Devices supported by this driver.
136 static struct ale_dev
{
137 uint16_t ale_vendorid
;
138 uint16_t ale_deviceid
;
139 const char *ale_name
;
141 { VENDORID_ATHEROS
, DEVICEID_ATHEROS_AR81XX
,
142 "Atheros AR8121/AR8113/AR8114 PCIe Ethernet" },
145 static device_method_t ale_methods
[] = {
146 /* Device interface. */
147 DEVMETHOD(device_probe
, ale_probe
),
148 DEVMETHOD(device_attach
, ale_attach
),
149 DEVMETHOD(device_detach
, ale_detach
),
150 DEVMETHOD(device_shutdown
, ale_shutdown
),
151 DEVMETHOD(device_suspend
, ale_suspend
),
152 DEVMETHOD(device_resume
, ale_resume
),
155 DEVMETHOD(bus_print_child
, bus_generic_print_child
),
156 DEVMETHOD(bus_driver_added
, bus_generic_driver_added
),
159 DEVMETHOD(miibus_readreg
, ale_miibus_readreg
),
160 DEVMETHOD(miibus_writereg
, ale_miibus_writereg
),
161 DEVMETHOD(miibus_statchg
, ale_miibus_statchg
),
166 static driver_t ale_driver
= {
169 sizeof(struct ale_softc
)
172 static devclass_t ale_devclass
;
174 DECLARE_DUMMY_MODULE(if_ale
);
175 MODULE_VERSION(if_ale
, 1);
176 MODULE_DEPEND(if_ale
, miibus
, 1, 1, 1);
177 DRIVER_MODULE(if_ale
, pci
, ale_driver
, ale_devclass
, NULL
, NULL
);
178 DRIVER_MODULE(miibus
, ale
, miibus_driver
, miibus_devclass
, NULL
, NULL
);
181 ale_miibus_readreg(device_t dev
, int phy
, int reg
)
183 struct ale_softc
*sc
;
187 sc
= device_get_softc(dev
);
189 if (phy
!= sc
->ale_phyaddr
)
192 if (sc
->ale_flags
& ALE_FLAG_FASTETHER
) {
193 if (reg
== MII_100T2CR
|| reg
== MII_100T2SR
||
198 CSR_WRITE_4(sc
, ALE_MDIO
, MDIO_OP_EXECUTE
| MDIO_OP_READ
|
199 MDIO_SUP_PREAMBLE
| MDIO_CLK_25_4
| MDIO_REG_ADDR(reg
));
200 for (i
= ALE_PHY_TIMEOUT
; i
> 0; i
--) {
202 v
= CSR_READ_4(sc
, ALE_MDIO
);
203 if ((v
& (MDIO_OP_EXECUTE
| MDIO_OP_BUSY
)) == 0)
208 device_printf(sc
->ale_dev
, "phy read timeout : %d\n", reg
);
212 return ((v
& MDIO_DATA_MASK
) >> MDIO_DATA_SHIFT
);
216 ale_miibus_writereg(device_t dev
, int phy
, int reg
, int val
)
218 struct ale_softc
*sc
;
222 sc
= device_get_softc(dev
);
224 if (phy
!= sc
->ale_phyaddr
)
227 if (sc
->ale_flags
& ALE_FLAG_FASTETHER
) {
228 if (reg
== MII_100T2CR
|| reg
== MII_100T2SR
||
233 CSR_WRITE_4(sc
, ALE_MDIO
, MDIO_OP_EXECUTE
| MDIO_OP_WRITE
|
234 (val
& MDIO_DATA_MASK
) << MDIO_DATA_SHIFT
|
235 MDIO_SUP_PREAMBLE
| MDIO_CLK_25_4
| MDIO_REG_ADDR(reg
));
236 for (i
= ALE_PHY_TIMEOUT
; i
> 0; i
--) {
238 v
= CSR_READ_4(sc
, ALE_MDIO
);
239 if ((v
& (MDIO_OP_EXECUTE
| MDIO_OP_BUSY
)) == 0)
244 device_printf(sc
->ale_dev
, "phy write timeout : %d\n", reg
);
250 ale_miibus_statchg(device_t dev
)
252 struct ale_softc
*sc
= device_get_softc(dev
);
253 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
254 struct mii_data
*mii
;
257 ASSERT_SERIALIZED(ifp
->if_serializer
);
259 if ((ifp
->if_flags
& IFF_RUNNING
) == 0)
262 mii
= device_get_softc(sc
->ale_miibus
);
264 sc
->ale_flags
&= ~ALE_FLAG_LINK
;
265 if ((mii
->mii_media_status
& (IFM_ACTIVE
| IFM_AVALID
)) ==
266 (IFM_ACTIVE
| IFM_AVALID
)) {
267 switch (IFM_SUBTYPE(mii
->mii_media_active
)) {
270 sc
->ale_flags
|= ALE_FLAG_LINK
;
274 if ((sc
->ale_flags
& ALE_FLAG_FASTETHER
) == 0)
275 sc
->ale_flags
|= ALE_FLAG_LINK
;
283 /* Stop Rx/Tx MACs. */
286 /* Program MACs with resolved speed/duplex/flow-control. */
287 if ((sc
->ale_flags
& ALE_FLAG_LINK
) != 0) {
289 /* Reenable Tx/Rx MACs. */
290 reg
= CSR_READ_4(sc
, ALE_MAC_CFG
);
291 reg
|= MAC_CFG_TX_ENB
| MAC_CFG_RX_ENB
;
292 CSR_WRITE_4(sc
, ALE_MAC_CFG
, reg
);
297 ale_mediastatus(struct ifnet
*ifp
, struct ifmediareq
*ifmr
)
299 struct ale_softc
*sc
= ifp
->if_softc
;
300 struct mii_data
*mii
= device_get_softc(sc
->ale_miibus
);
302 ASSERT_SERIALIZED(ifp
->if_serializer
);
305 ifmr
->ifm_status
= mii
->mii_media_status
;
306 ifmr
->ifm_active
= mii
->mii_media_active
;
310 ale_mediachange(struct ifnet
*ifp
)
312 struct ale_softc
*sc
= ifp
->if_softc
;
313 struct mii_data
*mii
= device_get_softc(sc
->ale_miibus
);
316 ASSERT_SERIALIZED(ifp
->if_serializer
);
318 if (mii
->mii_instance
!= 0) {
319 struct mii_softc
*miisc
;
321 LIST_FOREACH(miisc
, &mii
->mii_phys
, mii_list
)
322 mii_phy_reset(miisc
);
324 error
= mii_mediachg(mii
);
330 ale_probe(device_t dev
)
334 uint16_t vendor
, devid
;
336 vendor
= pci_get_vendor(dev
);
337 devid
= pci_get_device(dev
);
339 for (i
= 0; i
< NELEM(ale_devs
); i
++) {
340 if (vendor
== sp
->ale_vendorid
&&
341 devid
== sp
->ale_deviceid
) {
342 device_set_desc(dev
, sp
->ale_name
);
352 ale_get_macaddr(struct ale_softc
*sc
)
357 reg
= CSR_READ_4(sc
, ALE_SPI_CTRL
);
358 if ((reg
& SPI_VPD_ENB
) != 0) {
360 CSR_WRITE_4(sc
, ALE_SPI_CTRL
, reg
);
363 vpdc
= pci_get_vpdcap_ptr(sc
->ale_dev
);
366 * PCI VPD capability found, let TWSI reload EEPROM.
367 * This will set ethernet address of controller.
369 CSR_WRITE_4(sc
, ALE_TWSI_CTRL
, CSR_READ_4(sc
, ALE_TWSI_CTRL
) |
370 TWSI_CTRL_SW_LD_START
);
371 for (i
= 100; i
> 0; i
--) {
373 reg
= CSR_READ_4(sc
, ALE_TWSI_CTRL
);
374 if ((reg
& TWSI_CTRL_SW_LD_START
) == 0)
378 device_printf(sc
->ale_dev
,
379 "reloading EEPROM timeout!\n");
382 device_printf(sc
->ale_dev
,
383 "PCI VPD capability not found!\n");
386 ea
[0] = CSR_READ_4(sc
, ALE_PAR0
);
387 ea
[1] = CSR_READ_4(sc
, ALE_PAR1
);
388 sc
->ale_eaddr
[0] = (ea
[1] >> 8) & 0xFF;
389 sc
->ale_eaddr
[1] = (ea
[1] >> 0) & 0xFF;
390 sc
->ale_eaddr
[2] = (ea
[0] >> 24) & 0xFF;
391 sc
->ale_eaddr
[3] = (ea
[0] >> 16) & 0xFF;
392 sc
->ale_eaddr
[4] = (ea
[0] >> 8) & 0xFF;
393 sc
->ale_eaddr
[5] = (ea
[0] >> 0) & 0xFF;
397 ale_phy_reset(struct ale_softc
*sc
)
399 /* Reset magic from Linux. */
400 CSR_WRITE_2(sc
, ALE_GPHY_CTRL
,
401 GPHY_CTRL_HIB_EN
| GPHY_CTRL_HIB_PULSE
| GPHY_CTRL_SEL_ANA_RESET
|
402 GPHY_CTRL_PHY_PLL_ON
);
404 CSR_WRITE_2(sc
, ALE_GPHY_CTRL
,
405 GPHY_CTRL_EXT_RESET
| GPHY_CTRL_HIB_EN
| GPHY_CTRL_HIB_PULSE
|
406 GPHY_CTRL_SEL_ANA_RESET
| GPHY_CTRL_PHY_PLL_ON
);
409 #define ATPHY_DBG_ADDR 0x1D
410 #define ATPHY_DBG_DATA 0x1E
412 /* Enable hibernation mode. */
413 ale_miibus_writereg(sc
->ale_dev
, sc
->ale_phyaddr
,
414 ATPHY_DBG_ADDR
, 0x0B);
415 ale_miibus_writereg(sc
->ale_dev
, sc
->ale_phyaddr
,
416 ATPHY_DBG_DATA
, 0xBC00);
417 /* Set Class A/B for all modes. */
418 ale_miibus_writereg(sc
->ale_dev
, sc
->ale_phyaddr
,
419 ATPHY_DBG_ADDR
, 0x00);
420 ale_miibus_writereg(sc
->ale_dev
, sc
->ale_phyaddr
,
421 ATPHY_DBG_DATA
, 0x02EF);
422 /* Enable 10BT power saving. */
423 ale_miibus_writereg(sc
->ale_dev
, sc
->ale_phyaddr
,
424 ATPHY_DBG_ADDR
, 0x12);
425 ale_miibus_writereg(sc
->ale_dev
, sc
->ale_phyaddr
,
426 ATPHY_DBG_DATA
, 0x4C04);
427 /* Adjust 1000T power. */
428 ale_miibus_writereg(sc
->ale_dev
, sc
->ale_phyaddr
,
429 ATPHY_DBG_ADDR
, 0x04);
430 ale_miibus_writereg(sc
->ale_dev
, sc
->ale_phyaddr
,
431 ATPHY_DBG_ADDR
, 0x8BBB);
432 /* 10BT center tap voltage. */
433 ale_miibus_writereg(sc
->ale_dev
, sc
->ale_phyaddr
,
434 ATPHY_DBG_ADDR
, 0x05);
435 ale_miibus_writereg(sc
->ale_dev
, sc
->ale_phyaddr
,
436 ATPHY_DBG_ADDR
, 0x2C46);
438 #undef ATPHY_DBG_ADDR
439 #undef ATPHY_DBG_DATA
444 ale_attach(device_t dev
)
446 struct ale_softc
*sc
= device_get_softc(dev
);
447 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
449 uint32_t rxf_len
, txf_len
;
452 if_initname(ifp
, device_get_name(dev
), device_get_unit(dev
));
455 callout_init(&sc
->ale_tick_ch
);
458 if (pci_get_powerstate(dev
) != PCI_POWERSTATE_D0
) {
461 irq
= pci_read_config(dev
, PCIR_INTLINE
, 4);
462 mem
= pci_read_config(dev
, ALE_PCIR_BAR
, 4);
464 device_printf(dev
, "chip is in D%d power mode "
465 "-- setting to D0\n", pci_get_powerstate(dev
));
467 pci_set_powerstate(dev
, PCI_POWERSTATE_D0
);
469 pci_write_config(dev
, PCIR_INTLINE
, irq
, 4);
470 pci_write_config(dev
, ALE_PCIR_BAR
, mem
, 4);
472 #endif /* !BURN_BRIDGE */
474 /* Enable bus mastering */
475 pci_enable_busmaster(dev
);
478 * Allocate memory mapped IO
480 sc
->ale_mem_rid
= ALE_PCIR_BAR
;
481 sc
->ale_mem_res
= bus_alloc_resource_any(dev
, SYS_RES_MEMORY
,
482 &sc
->ale_mem_rid
, RF_ACTIVE
);
483 if (sc
->ale_mem_res
== NULL
) {
484 device_printf(dev
, "can't allocate IO memory\n");
487 sc
->ale_mem_bt
= rman_get_bustag(sc
->ale_mem_res
);
488 sc
->ale_mem_bh
= rman_get_bushandle(sc
->ale_mem_res
);
494 sc
->ale_irq_res
= bus_alloc_resource_any(dev
, SYS_RES_IRQ
,
496 RF_SHAREABLE
| RF_ACTIVE
);
497 if (sc
->ale_irq_res
== NULL
) {
498 device_printf(dev
, "can't allocate irq\n");
503 /* Set PHY address. */
504 sc
->ale_phyaddr
= ALE_PHY_ADDR
;
509 /* Reset the ethernet controller. */
512 /* Get PCI and chip id/revision. */
513 sc
->ale_rev
= pci_get_revid(dev
);
514 if (sc
->ale_rev
>= 0xF0) {
515 /* L2E Rev. B. AR8114 */
516 sc
->ale_flags
|= ALE_FLAG_FASTETHER
;
518 if ((CSR_READ_4(sc
, ALE_PHY_STATUS
) & PHY_STATUS_100M
) != 0) {
520 sc
->ale_flags
|= ALE_FLAG_JUMBO
;
522 /* L2E Rev. A. AR8113 */
523 sc
->ale_flags
|= ALE_FLAG_FASTETHER
;
528 * All known controllers seems to require 4 bytes alignment
529 * of Tx buffers to make Tx checksum offload with custom
530 * checksum generation method work.
532 sc
->ale_flags
|= ALE_FLAG_TXCSUM_BUG
;
535 * All known controllers seems to have issues on Rx checksum
536 * offload for fragmented IP datagrams.
538 sc
->ale_flags
|= ALE_FLAG_RXCSUM_BUG
;
541 * Don't use Tx CMB. It is known to cause RRS update failure
542 * under certain circumstances. Typical phenomenon of the
543 * issue would be unexpected sequence number encountered in
546 sc
->ale_flags
|= ALE_FLAG_TXCMB_BUG
;
547 sc
->ale_chip_rev
= CSR_READ_4(sc
, ALE_MASTER_CFG
) >>
548 MASTER_CHIP_REV_SHIFT
;
550 device_printf(dev
, "PCI device revision : 0x%04x\n",
552 device_printf(dev
, "Chip id/revision : 0x%04x\n",
557 * Uninitialized hardware returns an invalid chip id/revision
558 * as well as 0xFFFFFFFF for Tx/Rx fifo length.
560 txf_len
= CSR_READ_4(sc
, ALE_SRAM_TX_FIFO_LEN
);
561 rxf_len
= CSR_READ_4(sc
, ALE_SRAM_RX_FIFO_LEN
);
562 if (sc
->ale_chip_rev
== 0xFFFF || txf_len
== 0xFFFFFFFF ||
563 rxf_len
== 0xFFFFFFF) {
564 device_printf(dev
,"chip revision : 0x%04x, %u Tx FIFO "
565 "%u Rx FIFO -- not initialized?\n", sc
->ale_chip_rev
,
570 device_printf(dev
, "%u Tx FIFO, %u Rx FIFO\n", txf_len
, rxf_len
);
572 /* Get DMA parameters from PCIe device control register. */
573 pcie_ptr
= pci_get_pciecap_ptr(dev
);
577 sc
->ale_flags
|= ALE_FLAG_PCIE
;
578 devctl
= pci_read_config(dev
, pcie_ptr
+ PCIER_DEVCTRL
, 2);
579 /* Max read request size. */
580 sc
->ale_dma_rd_burst
= ((devctl
>> 12) & 0x07) <<
581 DMA_CFG_RD_BURST_SHIFT
;
582 /* Max payload size. */
583 sc
->ale_dma_wr_burst
= ((devctl
>> 5) & 0x07) <<
584 DMA_CFG_WR_BURST_SHIFT
;
586 device_printf(dev
, "Read request size : %d bytes.\n",
587 128 << ((devctl
>> 12) & 0x07));
588 device_printf(dev
, "TLP payload size : %d bytes.\n",
589 128 << ((devctl
>> 5) & 0x07));
592 sc
->ale_dma_rd_burst
= DMA_CFG_RD_BURST_128
;
593 sc
->ale_dma_wr_burst
= DMA_CFG_WR_BURST_128
;
596 /* Create device sysctl node. */
599 if ((error
= ale_dma_alloc(sc
)) != 0)
602 /* Load station address. */
606 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
607 ifp
->if_ioctl
= ale_ioctl
;
608 ifp
->if_start
= ale_start
;
609 ifp
->if_init
= ale_init
;
610 ifp
->if_watchdog
= ale_watchdog
;
611 ifq_set_maxlen(&ifp
->if_snd
, ALE_TX_RING_CNT
- 1);
612 ifq_set_ready(&ifp
->if_snd
);
614 ifp
->if_capabilities
= IFCAP_RXCSUM
|
616 IFCAP_VLAN_HWTAGGING
;
618 ifp
->if_capabilities
|= IFCAP_TXCSUM
;
619 ifp
->if_hwassist
= ALE_CSUM_FEATURES
;
621 ifp
->if_capenable
= ifp
->if_capabilities
;
623 /* Set up MII bus. */
624 if ((error
= mii_phy_probe(dev
, &sc
->ale_miibus
, ale_mediachange
,
625 ale_mediastatus
)) != 0) {
626 device_printf(dev
, "no PHY found!\n");
630 ether_ifattach(ifp
, sc
->ale_eaddr
, NULL
);
632 /* Tell the upper layer(s) we support long frames. */
633 ifp
->if_data
.ifi_hdrlen
= sizeof(struct ether_vlan_header
);
635 ifq_set_cpuid(&ifp
->if_snd
, rman_get_cpuid(sc
->ale_irq_res
));
637 error
= bus_setup_intr(dev
, sc
->ale_irq_res
, INTR_MPSAFE
, ale_intr
, sc
,
638 &sc
->ale_irq_handle
, ifp
->if_serializer
);
640 device_printf(dev
, "could not set up interrupt handler.\n");
652 ale_detach(device_t dev
)
654 struct ale_softc
*sc
= device_get_softc(dev
);
656 if (device_is_attached(dev
)) {
657 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
659 lwkt_serialize_enter(ifp
->if_serializer
);
660 sc
->ale_flags
|= ALE_FLAG_DETACH
;
662 bus_teardown_intr(dev
, sc
->ale_irq_res
, sc
->ale_irq_handle
);
663 lwkt_serialize_exit(ifp
->if_serializer
);
668 if (sc
->ale_miibus
!= NULL
)
669 device_delete_child(dev
, sc
->ale_miibus
);
670 bus_generic_detach(dev
);
672 if (sc
->ale_irq_res
!= NULL
) {
673 bus_release_resource(dev
, SYS_RES_IRQ
, sc
->ale_irq_rid
,
676 if (sc
->ale_mem_res
!= NULL
) {
677 bus_release_resource(dev
, SYS_RES_MEMORY
, sc
->ale_mem_rid
,
686 #define ALE_SYSCTL_STAT_ADD32(c, h, n, p, d) \
687 SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
688 #define ALE_SYSCTL_STAT_ADD64(c, h, n, p, d) \
689 SYSCTL_ADD_QUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
692 ale_sysctl_node(struct ale_softc
*sc
)
694 struct sysctl_ctx_list
*ctx
;
695 struct sysctl_oid_list
*child
, *parent
;
696 struct sysctl_oid
*tree
;
697 struct ale_hw_stats
*stats
;
700 stats
= &sc
->ale_stats
;
701 ctx
= device_get_sysctl_ctx(sc
->ale_dev
);
702 child
= SYSCTL_CHILDREN(device_get_sysctl_tree(sc
->ale_dev
));
704 SYSCTL_ADD_PROC(ctx
, child
, OID_AUTO
, "int_rx_mod",
705 CTLTYPE_INT
| CTLFLAG_RW
, &sc
->ale_int_rx_mod
, 0,
706 sysctl_hw_ale_int_mod
, "I", "ale Rx interrupt moderation");
707 SYSCTL_ADD_PROC(ctx
, child
, OID_AUTO
, "int_tx_mod",
708 CTLTYPE_INT
| CTLFLAG_RW
, &sc
->ale_int_tx_mod
, 0,
709 sysctl_hw_ale_int_mod
, "I", "ale Tx interrupt moderation");
712 * Pull in device tunables.
714 sc
->ale_int_rx_mod
= ALE_IM_RX_TIMER_DEFAULT
;
715 error
= resource_int_value(device_get_name(sc
->ale_dev
),
716 device_get_unit(sc
->ale_dev
), "int_rx_mod", &sc
->ale_int_rx_mod
);
718 if (sc
->ale_int_rx_mod
< ALE_IM_TIMER_MIN
||
719 sc
->ale_int_rx_mod
> ALE_IM_TIMER_MAX
) {
720 device_printf(sc
->ale_dev
, "int_rx_mod value out of "
721 "range; using default: %d\n",
722 ALE_IM_RX_TIMER_DEFAULT
);
723 sc
->ale_int_rx_mod
= ALE_IM_RX_TIMER_DEFAULT
;
727 sc
->ale_int_tx_mod
= ALE_IM_TX_TIMER_DEFAULT
;
728 error
= resource_int_value(device_get_name(sc
->ale_dev
),
729 device_get_unit(sc
->ale_dev
), "int_tx_mod", &sc
->ale_int_tx_mod
);
731 if (sc
->ale_int_tx_mod
< ALE_IM_TIMER_MIN
||
732 sc
->ale_int_tx_mod
> ALE_IM_TIMER_MAX
) {
733 device_printf(sc
->ale_dev
, "int_tx_mod value out of "
734 "range; using default: %d\n",
735 ALE_IM_TX_TIMER_DEFAULT
);
736 sc
->ale_int_tx_mod
= ALE_IM_TX_TIMER_DEFAULT
;
740 /* Misc statistics. */
741 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "reset_brk_seq",
742 &stats
->reset_brk_seq
,
743 "Controller resets due to broken Rx sequnce number");
745 tree
= SYSCTL_ADD_NODE(ctx
, child
, OID_AUTO
, "stats", CTLFLAG_RD
,
746 NULL
, "ATE statistics");
747 parent
= SYSCTL_CHILDREN(tree
);
750 tree
= SYSCTL_ADD_NODE(ctx
, parent
, OID_AUTO
, "rx", CTLFLAG_RD
,
751 NULL
, "Rx MAC statistics");
752 child
= SYSCTL_CHILDREN(tree
);
753 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "good_frames",
754 &stats
->rx_frames
, "Good frames");
755 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "good_bcast_frames",
756 &stats
->rx_bcast_frames
, "Good broadcast frames");
757 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "good_mcast_frames",
758 &stats
->rx_mcast_frames
, "Good multicast frames");
759 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "pause_frames",
760 &stats
->rx_pause_frames
, "Pause control frames");
761 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "control_frames",
762 &stats
->rx_control_frames
, "Control frames");
763 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "crc_errs",
764 &stats
->rx_crcerrs
, "CRC errors");
765 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "len_errs",
766 &stats
->rx_lenerrs
, "Frames with length mismatched");
767 ALE_SYSCTL_STAT_ADD64(ctx
, child
, "good_octets",
768 &stats
->rx_bytes
, "Good octets");
769 ALE_SYSCTL_STAT_ADD64(ctx
, child
, "good_bcast_octets",
770 &stats
->rx_bcast_bytes
, "Good broadcast octets");
771 ALE_SYSCTL_STAT_ADD64(ctx
, child
, "good_mcast_octets",
772 &stats
->rx_mcast_bytes
, "Good multicast octets");
773 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "runts",
774 &stats
->rx_runts
, "Too short frames");
775 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "fragments",
776 &stats
->rx_fragments
, "Fragmented frames");
777 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_64",
778 &stats
->rx_pkts_64
, "64 bytes frames");
779 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_65_127",
780 &stats
->rx_pkts_65_127
, "65 to 127 bytes frames");
781 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_128_255",
782 &stats
->rx_pkts_128_255
, "128 to 255 bytes frames");
783 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_256_511",
784 &stats
->rx_pkts_256_511
, "256 to 511 bytes frames");
785 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_512_1023",
786 &stats
->rx_pkts_512_1023
, "512 to 1023 bytes frames");
787 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_1024_1518",
788 &stats
->rx_pkts_1024_1518
, "1024 to 1518 bytes frames");
789 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_1519_max",
790 &stats
->rx_pkts_1519_max
, "1519 to max frames");
791 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "trunc_errs",
792 &stats
->rx_pkts_truncated
, "Truncated frames due to MTU size");
793 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "fifo_oflows",
794 &stats
->rx_fifo_oflows
, "FIFO overflows");
795 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "rrs_errs",
796 &stats
->rx_rrs_errs
, "Return status write-back errors");
797 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "align_errs",
798 &stats
->rx_alignerrs
, "Alignment errors");
799 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "filtered",
800 &stats
->rx_pkts_filtered
,
801 "Frames dropped due to address filtering");
804 tree
= SYSCTL_ADD_NODE(ctx
, parent
, OID_AUTO
, "tx", CTLFLAG_RD
,
805 NULL
, "Tx MAC statistics");
806 child
= SYSCTL_CHILDREN(tree
);
807 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "good_frames",
808 &stats
->tx_frames
, "Good frames");
809 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "good_bcast_frames",
810 &stats
->tx_bcast_frames
, "Good broadcast frames");
811 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "good_mcast_frames",
812 &stats
->tx_mcast_frames
, "Good multicast frames");
813 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "pause_frames",
814 &stats
->tx_pause_frames
, "Pause control frames");
815 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "control_frames",
816 &stats
->tx_control_frames
, "Control frames");
817 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "excess_defers",
818 &stats
->tx_excess_defer
, "Frames with excessive derferrals");
819 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "defers",
820 &stats
->tx_excess_defer
, "Frames with derferrals");
821 ALE_SYSCTL_STAT_ADD64(ctx
, child
, "good_octets",
822 &stats
->tx_bytes
, "Good octets");
823 ALE_SYSCTL_STAT_ADD64(ctx
, child
, "good_bcast_octets",
824 &stats
->tx_bcast_bytes
, "Good broadcast octets");
825 ALE_SYSCTL_STAT_ADD64(ctx
, child
, "good_mcast_octets",
826 &stats
->tx_mcast_bytes
, "Good multicast octets");
827 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_64",
828 &stats
->tx_pkts_64
, "64 bytes frames");
829 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_65_127",
830 &stats
->tx_pkts_65_127
, "65 to 127 bytes frames");
831 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_128_255",
832 &stats
->tx_pkts_128_255
, "128 to 255 bytes frames");
833 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_256_511",
834 &stats
->tx_pkts_256_511
, "256 to 511 bytes frames");
835 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_512_1023",
836 &stats
->tx_pkts_512_1023
, "512 to 1023 bytes frames");
837 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_1024_1518",
838 &stats
->tx_pkts_1024_1518
, "1024 to 1518 bytes frames");
839 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "frames_1519_max",
840 &stats
->tx_pkts_1519_max
, "1519 to max frames");
841 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "single_colls",
842 &stats
->tx_single_colls
, "Single collisions");
843 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "multi_colls",
844 &stats
->tx_multi_colls
, "Multiple collisions");
845 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "late_colls",
846 &stats
->tx_late_colls
, "Late collisions");
847 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "excess_colls",
848 &stats
->tx_excess_colls
, "Excessive collisions");
849 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "abort",
850 &stats
->tx_abort
, "Aborted frames due to Excessive collisions");
851 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "underruns",
852 &stats
->tx_underrun
, "FIFO underruns");
853 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "desc_underruns",
854 &stats
->tx_desc_underrun
, "Descriptor write-back errors");
855 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "len_errs",
856 &stats
->tx_lenerrs
, "Frames with length mismatched");
857 ALE_SYSCTL_STAT_ADD32(ctx
, child
, "trunc_errs",
858 &stats
->tx_pkts_truncated
, "Truncated frames due to MTU size");
861 #undef ALE_SYSCTL_STAT_ADD32
862 #undef ALE_SYSCTL_STAT_ADD64
864 struct ale_dmamap_arg
{
865 bus_addr_t ale_busaddr
;
869 ale_dmamap_cb(void *arg
, bus_dma_segment_t
*segs
, int nsegs
, int error
)
871 struct ale_dmamap_arg
*ctx
;
876 KASSERT(nsegs
== 1, ("%s: %d segments returned!", __func__
, nsegs
));
878 ctx
= (struct ale_dmamap_arg
*)arg
;
879 ctx
->ale_busaddr
= segs
[0].ds_addr
;
883 * Tx descriptors/RXF0/CMB DMA blocks share ALE_DESC_ADDR_HI register
884 * which specifies high address region of DMA blocks. Therefore these
885 * blocks should have the same high address of given 4GB address
886 * space(i.e. crossing 4GB boundary is not allowed).
889 ale_check_boundary(struct ale_softc
*sc
)
891 bus_addr_t rx_cmb_end
[ALE_RX_PAGES
], tx_cmb_end
;
892 bus_addr_t rx_page_end
[ALE_RX_PAGES
], tx_ring_end
;
894 rx_page_end
[0] = sc
->ale_cdata
.ale_rx_page
[0].page_paddr
+
896 rx_page_end
[1] = sc
->ale_cdata
.ale_rx_page
[1].page_paddr
+
898 tx_ring_end
= sc
->ale_cdata
.ale_tx_ring_paddr
+ ALE_TX_RING_SZ
;
899 tx_cmb_end
= sc
->ale_cdata
.ale_tx_cmb_paddr
+ ALE_TX_CMB_SZ
;
900 rx_cmb_end
[0] = sc
->ale_cdata
.ale_rx_page
[0].cmb_paddr
+ ALE_RX_CMB_SZ
;
901 rx_cmb_end
[1] = sc
->ale_cdata
.ale_rx_page
[1].cmb_paddr
+ ALE_RX_CMB_SZ
;
903 if ((ALE_ADDR_HI(tx_ring_end
) !=
904 ALE_ADDR_HI(sc
->ale_cdata
.ale_tx_ring_paddr
)) ||
905 (ALE_ADDR_HI(rx_page_end
[0]) !=
906 ALE_ADDR_HI(sc
->ale_cdata
.ale_rx_page
[0].page_paddr
)) ||
907 (ALE_ADDR_HI(rx_page_end
[1]) !=
908 ALE_ADDR_HI(sc
->ale_cdata
.ale_rx_page
[1].page_paddr
)) ||
909 (ALE_ADDR_HI(tx_cmb_end
) !=
910 ALE_ADDR_HI(sc
->ale_cdata
.ale_tx_cmb_paddr
)) ||
911 (ALE_ADDR_HI(rx_cmb_end
[0]) !=
912 ALE_ADDR_HI(sc
->ale_cdata
.ale_rx_page
[0].cmb_paddr
)) ||
913 (ALE_ADDR_HI(rx_cmb_end
[1]) !=
914 ALE_ADDR_HI(sc
->ale_cdata
.ale_rx_page
[1].cmb_paddr
)))
917 if ((ALE_ADDR_HI(tx_ring_end
) != ALE_ADDR_HI(rx_page_end
[0])) ||
918 (ALE_ADDR_HI(tx_ring_end
) != ALE_ADDR_HI(rx_page_end
[1])) ||
919 (ALE_ADDR_HI(tx_ring_end
) != ALE_ADDR_HI(rx_cmb_end
[0])) ||
920 (ALE_ADDR_HI(tx_ring_end
) != ALE_ADDR_HI(rx_cmb_end
[1])) ||
921 (ALE_ADDR_HI(tx_ring_end
) != ALE_ADDR_HI(tx_cmb_end
)))
928 ale_dma_alloc(struct ale_softc
*sc
)
930 struct ale_txdesc
*txd
;
932 struct ale_dmamap_arg ctx
;
933 int error
, guard_size
, i
;
935 if ((sc
->ale_flags
& ALE_FLAG_JUMBO
) != 0)
936 guard_size
= ALE_JUMBO_FRAMELEN
;
938 guard_size
= ALE_MAX_FRAMELEN
;
939 sc
->ale_pagesize
= roundup(guard_size
+ ALE_RX_PAGE_SZ
,
941 lowaddr
= BUS_SPACE_MAXADDR
;
943 /* Create parent DMA tag. */
944 error
= bus_dma_tag_create(
946 1, 0, /* alignment, boundary */
947 lowaddr
, /* lowaddr */
948 BUS_SPACE_MAXADDR
, /* highaddr */
949 NULL
, NULL
, /* filter, filterarg */
950 BUS_SPACE_MAXSIZE_32BIT
, /* maxsize */
952 BUS_SPACE_MAXSIZE_32BIT
, /* maxsegsize */
954 &sc
->ale_cdata
.ale_parent_tag
);
956 device_printf(sc
->ale_dev
,
957 "could not create parent DMA tag.\n");
961 /* Create DMA tag for Tx descriptor ring. */
962 error
= bus_dma_tag_create(
963 sc
->ale_cdata
.ale_parent_tag
, /* parent */
964 ALE_TX_RING_ALIGN
, 0, /* alignment, boundary */
965 BUS_SPACE_MAXADDR
, /* lowaddr */
966 BUS_SPACE_MAXADDR
, /* highaddr */
967 NULL
, NULL
, /* filter, filterarg */
968 ALE_TX_RING_SZ
, /* maxsize */
970 ALE_TX_RING_SZ
, /* maxsegsize */
972 &sc
->ale_cdata
.ale_tx_ring_tag
);
974 device_printf(sc
->ale_dev
,
975 "could not create Tx ring DMA tag.\n");
979 /* Create DMA tag for Rx pages. */
980 for (i
= 0; i
< ALE_RX_PAGES
; i
++) {
981 error
= bus_dma_tag_create(
982 sc
->ale_cdata
.ale_parent_tag
, /* parent */
983 ALE_RX_PAGE_ALIGN
, 0, /* alignment, boundary */
984 BUS_SPACE_MAXADDR
, /* lowaddr */
985 BUS_SPACE_MAXADDR
, /* highaddr */
986 NULL
, NULL
, /* filter, filterarg */
987 sc
->ale_pagesize
, /* maxsize */
989 sc
->ale_pagesize
, /* maxsegsize */
991 &sc
->ale_cdata
.ale_rx_page
[i
].page_tag
);
993 device_printf(sc
->ale_dev
,
994 "could not create Rx page %d DMA tag.\n", i
);
999 /* Create DMA tag for Tx coalescing message block. */
1000 error
= bus_dma_tag_create(
1001 sc
->ale_cdata
.ale_parent_tag
, /* parent */
1002 ALE_CMB_ALIGN
, 0, /* alignment, boundary */
1003 BUS_SPACE_MAXADDR
, /* lowaddr */
1004 BUS_SPACE_MAXADDR
, /* highaddr */
1005 NULL
, NULL
, /* filter, filterarg */
1006 ALE_TX_CMB_SZ
, /* maxsize */
1008 ALE_TX_CMB_SZ
, /* maxsegsize */
1010 &sc
->ale_cdata
.ale_tx_cmb_tag
);
1012 device_printf(sc
->ale_dev
,
1013 "could not create Tx CMB DMA tag.\n");
1017 /* Create DMA tag for Rx coalescing message block. */
1018 for (i
= 0; i
< ALE_RX_PAGES
; i
++) {
1019 error
= bus_dma_tag_create(
1020 sc
->ale_cdata
.ale_parent_tag
, /* parent */
1021 ALE_CMB_ALIGN
, 0, /* alignment, boundary */
1022 BUS_SPACE_MAXADDR
, /* lowaddr */
1023 BUS_SPACE_MAXADDR
, /* highaddr */
1024 NULL
, NULL
, /* filter, filterarg */
1025 ALE_RX_CMB_SZ
, /* maxsize */
1027 ALE_RX_CMB_SZ
, /* maxsegsize */
1029 &sc
->ale_cdata
.ale_rx_page
[i
].cmb_tag
);
1031 device_printf(sc
->ale_dev
,
1032 "could not create Rx page %d CMB DMA tag.\n", i
);
1037 /* Allocate DMA'able memory and load the DMA map for Tx ring. */
1038 error
= bus_dmamem_alloc(sc
->ale_cdata
.ale_tx_ring_tag
,
1039 (void **)&sc
->ale_cdata
.ale_tx_ring
,
1040 BUS_DMA_WAITOK
| BUS_DMA_ZERO
,
1041 &sc
->ale_cdata
.ale_tx_ring_map
);
1043 device_printf(sc
->ale_dev
,
1044 "could not allocate DMA'able memory for Tx ring.\n");
1047 ctx
.ale_busaddr
= 0;
1048 error
= bus_dmamap_load(sc
->ale_cdata
.ale_tx_ring_tag
,
1049 sc
->ale_cdata
.ale_tx_ring_map
, sc
->ale_cdata
.ale_tx_ring
,
1050 ALE_TX_RING_SZ
, ale_dmamap_cb
, &ctx
, 0);
1051 if (error
!= 0 || ctx
.ale_busaddr
== 0) {
1052 device_printf(sc
->ale_dev
,
1053 "could not load DMA'able memory for Tx ring.\n");
1056 sc
->ale_cdata
.ale_tx_ring_paddr
= ctx
.ale_busaddr
;
1059 for (i
= 0; i
< ALE_RX_PAGES
; i
++) {
1060 error
= bus_dmamem_alloc(sc
->ale_cdata
.ale_rx_page
[i
].page_tag
,
1061 (void **)&sc
->ale_cdata
.ale_rx_page
[i
].page_addr
,
1062 BUS_DMA_WAITOK
| BUS_DMA_ZERO
,
1063 &sc
->ale_cdata
.ale_rx_page
[i
].page_map
);
1065 device_printf(sc
->ale_dev
,
1066 "could not allocate DMA'able memory for "
1067 "Rx page %d.\n", i
);
1070 ctx
.ale_busaddr
= 0;
1071 error
= bus_dmamap_load(sc
->ale_cdata
.ale_rx_page
[i
].page_tag
,
1072 sc
->ale_cdata
.ale_rx_page
[i
].page_map
,
1073 sc
->ale_cdata
.ale_rx_page
[i
].page_addr
,
1074 sc
->ale_pagesize
, ale_dmamap_cb
, &ctx
, 0);
1075 if (error
!= 0 || ctx
.ale_busaddr
== 0) {
1076 device_printf(sc
->ale_dev
,
1077 "could not load DMA'able memory for "
1078 "Rx page %d.\n", i
);
1081 sc
->ale_cdata
.ale_rx_page
[i
].page_paddr
= ctx
.ale_busaddr
;
1085 error
= bus_dmamem_alloc(sc
->ale_cdata
.ale_tx_cmb_tag
,
1086 (void **)&sc
->ale_cdata
.ale_tx_cmb
,
1087 BUS_DMA_WAITOK
| BUS_DMA_ZERO
,
1088 &sc
->ale_cdata
.ale_tx_cmb_map
);
1090 device_printf(sc
->ale_dev
,
1091 "could not allocate DMA'able memory for Tx CMB.\n");
1094 ctx
.ale_busaddr
= 0;
1095 error
= bus_dmamap_load(sc
->ale_cdata
.ale_tx_cmb_tag
,
1096 sc
->ale_cdata
.ale_tx_cmb_map
, sc
->ale_cdata
.ale_tx_cmb
,
1097 ALE_TX_CMB_SZ
, ale_dmamap_cb
, &ctx
, 0);
1098 if (error
!= 0 || ctx
.ale_busaddr
== 0) {
1099 device_printf(sc
->ale_dev
,
1100 "could not load DMA'able memory for Tx CMB.\n");
1103 sc
->ale_cdata
.ale_tx_cmb_paddr
= ctx
.ale_busaddr
;
1106 for (i
= 0; i
< ALE_RX_PAGES
; i
++) {
1107 error
= bus_dmamem_alloc(sc
->ale_cdata
.ale_rx_page
[i
].cmb_tag
,
1108 (void **)&sc
->ale_cdata
.ale_rx_page
[i
].cmb_addr
,
1109 BUS_DMA_WAITOK
| BUS_DMA_ZERO
,
1110 &sc
->ale_cdata
.ale_rx_page
[i
].cmb_map
);
1112 device_printf(sc
->ale_dev
, "could not allocate "
1113 "DMA'able memory for Rx page %d CMB.\n", i
);
1116 ctx
.ale_busaddr
= 0;
1117 error
= bus_dmamap_load(sc
->ale_cdata
.ale_rx_page
[i
].cmb_tag
,
1118 sc
->ale_cdata
.ale_rx_page
[i
].cmb_map
,
1119 sc
->ale_cdata
.ale_rx_page
[i
].cmb_addr
,
1120 ALE_RX_CMB_SZ
, ale_dmamap_cb
, &ctx
, 0);
1121 if (error
!= 0 || ctx
.ale_busaddr
== 0) {
1122 device_printf(sc
->ale_dev
, "could not load DMA'able "
1123 "memory for Rx page %d CMB.\n", i
);
1126 sc
->ale_cdata
.ale_rx_page
[i
].cmb_paddr
= ctx
.ale_busaddr
;
1130 * Tx descriptors/RXF0/CMB DMA blocks share the same
1131 * high address region of 64bit DMA address space.
1133 if (lowaddr
!= BUS_SPACE_MAXADDR_32BIT
&&
1134 (error
= ale_check_boundary(sc
)) != 0) {
1135 device_printf(sc
->ale_dev
, "4GB boundary crossed, "
1136 "switching to 32bit DMA addressing mode.\n");
1139 * Limit max allowable DMA address space to 32bit
1142 lowaddr
= BUS_SPACE_MAXADDR_32BIT
;
1147 * Create Tx buffer parent tag.
1148 * AR81xx allows 64bit DMA addressing of Tx buffers so it
1149 * needs separate parent DMA tag as parent DMA address space
1150 * could be restricted to be within 32bit address space by
1151 * 4GB boundary crossing.
1153 error
= bus_dma_tag_create(
1155 1, 0, /* alignment, boundary */
1156 BUS_SPACE_MAXADDR
, /* lowaddr */
1157 BUS_SPACE_MAXADDR
, /* highaddr */
1158 NULL
, NULL
, /* filter, filterarg */
1159 BUS_SPACE_MAXSIZE_32BIT
, /* maxsize */
1161 BUS_SPACE_MAXSIZE_32BIT
, /* maxsegsize */
1163 &sc
->ale_cdata
.ale_buffer_tag
);
1165 device_printf(sc
->ale_dev
,
1166 "could not create parent buffer DMA tag.\n");
1170 /* Create DMA tag for Tx buffers. */
1171 error
= bus_dma_tag_create(
1172 sc
->ale_cdata
.ale_buffer_tag
, /* parent */
1173 1, 0, /* alignment, boundary */
1174 BUS_SPACE_MAXADDR
, /* lowaddr */
1175 BUS_SPACE_MAXADDR
, /* highaddr */
1176 NULL
, NULL
, /* filter, filterarg */
1177 ALE_TSO_MAXSIZE
, /* maxsize */
1178 ALE_MAXTXSEGS
, /* nsegments */
1179 ALE_TSO_MAXSEGSIZE
, /* maxsegsize */
1181 &sc
->ale_cdata
.ale_tx_tag
);
1183 device_printf(sc
->ale_dev
, "could not create Tx DMA tag.\n");
1187 /* Create DMA maps for Tx buffers. */
1188 for (i
= 0; i
< ALE_TX_RING_CNT
; i
++) {
1189 txd
= &sc
->ale_cdata
.ale_txdesc
[i
];
1191 txd
->tx_dmamap
= NULL
;
1192 error
= bus_dmamap_create(sc
->ale_cdata
.ale_tx_tag
, 0,
1195 device_printf(sc
->ale_dev
,
1196 "could not create Tx dmamap.\n");
1205 ale_dma_free(struct ale_softc
*sc
)
1207 struct ale_txdesc
*txd
;
1211 if (sc
->ale_cdata
.ale_tx_tag
!= NULL
) {
1212 for (i
= 0; i
< ALE_TX_RING_CNT
; i
++) {
1213 txd
= &sc
->ale_cdata
.ale_txdesc
[i
];
1214 if (txd
->tx_dmamap
!= NULL
) {
1215 bus_dmamap_destroy(sc
->ale_cdata
.ale_tx_tag
,
1217 txd
->tx_dmamap
= NULL
;
1220 bus_dma_tag_destroy(sc
->ale_cdata
.ale_tx_tag
);
1221 sc
->ale_cdata
.ale_tx_tag
= NULL
;
1223 /* Tx descriptor ring. */
1224 if (sc
->ale_cdata
.ale_tx_ring_tag
!= NULL
) {
1225 if (sc
->ale_cdata
.ale_tx_ring_map
!= NULL
)
1226 bus_dmamap_unload(sc
->ale_cdata
.ale_tx_ring_tag
,
1227 sc
->ale_cdata
.ale_tx_ring_map
);
1228 if (sc
->ale_cdata
.ale_tx_ring_map
!= NULL
&&
1229 sc
->ale_cdata
.ale_tx_ring
!= NULL
)
1230 bus_dmamem_free(sc
->ale_cdata
.ale_tx_ring_tag
,
1231 sc
->ale_cdata
.ale_tx_ring
,
1232 sc
->ale_cdata
.ale_tx_ring_map
);
1233 sc
->ale_cdata
.ale_tx_ring
= NULL
;
1234 sc
->ale_cdata
.ale_tx_ring_map
= NULL
;
1235 bus_dma_tag_destroy(sc
->ale_cdata
.ale_tx_ring_tag
);
1236 sc
->ale_cdata
.ale_tx_ring_tag
= NULL
;
1238 /* Rx page block. */
1239 for (i
= 0; i
< ALE_RX_PAGES
; i
++) {
1240 if (sc
->ale_cdata
.ale_rx_page
[i
].page_tag
!= NULL
) {
1241 if (sc
->ale_cdata
.ale_rx_page
[i
].page_map
!= NULL
)
1243 sc
->ale_cdata
.ale_rx_page
[i
].page_tag
,
1244 sc
->ale_cdata
.ale_rx_page
[i
].page_map
);
1245 if (sc
->ale_cdata
.ale_rx_page
[i
].page_map
!= NULL
&&
1246 sc
->ale_cdata
.ale_rx_page
[i
].page_addr
!= NULL
)
1248 sc
->ale_cdata
.ale_rx_page
[i
].page_tag
,
1249 sc
->ale_cdata
.ale_rx_page
[i
].page_addr
,
1250 sc
->ale_cdata
.ale_rx_page
[i
].page_map
);
1251 sc
->ale_cdata
.ale_rx_page
[i
].page_addr
= NULL
;
1252 sc
->ale_cdata
.ale_rx_page
[i
].page_map
= NULL
;
1253 bus_dma_tag_destroy(
1254 sc
->ale_cdata
.ale_rx_page
[i
].page_tag
);
1255 sc
->ale_cdata
.ale_rx_page
[i
].page_tag
= NULL
;
1259 for (i
= 0; i
< ALE_RX_PAGES
; i
++) {
1260 if (sc
->ale_cdata
.ale_rx_page
[i
].cmb_tag
!= NULL
) {
1261 if (sc
->ale_cdata
.ale_rx_page
[i
].cmb_map
!= NULL
)
1263 sc
->ale_cdata
.ale_rx_page
[i
].cmb_tag
,
1264 sc
->ale_cdata
.ale_rx_page
[i
].cmb_map
);
1265 if (sc
->ale_cdata
.ale_rx_page
[i
].cmb_map
!= NULL
&&
1266 sc
->ale_cdata
.ale_rx_page
[i
].cmb_addr
!= NULL
)
1268 sc
->ale_cdata
.ale_rx_page
[i
].cmb_tag
,
1269 sc
->ale_cdata
.ale_rx_page
[i
].cmb_addr
,
1270 sc
->ale_cdata
.ale_rx_page
[i
].cmb_map
);
1271 sc
->ale_cdata
.ale_rx_page
[i
].cmb_addr
= NULL
;
1272 sc
->ale_cdata
.ale_rx_page
[i
].cmb_map
= NULL
;
1273 bus_dma_tag_destroy(
1274 sc
->ale_cdata
.ale_rx_page
[i
].cmb_tag
);
1275 sc
->ale_cdata
.ale_rx_page
[i
].cmb_tag
= NULL
;
1279 if (sc
->ale_cdata
.ale_tx_cmb_tag
!= NULL
) {
1280 if (sc
->ale_cdata
.ale_tx_cmb_map
!= NULL
)
1281 bus_dmamap_unload(sc
->ale_cdata
.ale_tx_cmb_tag
,
1282 sc
->ale_cdata
.ale_tx_cmb_map
);
1283 if (sc
->ale_cdata
.ale_tx_cmb_map
!= NULL
&&
1284 sc
->ale_cdata
.ale_tx_cmb
!= NULL
)
1285 bus_dmamem_free(sc
->ale_cdata
.ale_tx_cmb_tag
,
1286 sc
->ale_cdata
.ale_tx_cmb
,
1287 sc
->ale_cdata
.ale_tx_cmb_map
);
1288 sc
->ale_cdata
.ale_tx_cmb
= NULL
;
1289 sc
->ale_cdata
.ale_tx_cmb_map
= NULL
;
1290 bus_dma_tag_destroy(sc
->ale_cdata
.ale_tx_cmb_tag
);
1291 sc
->ale_cdata
.ale_tx_cmb_tag
= NULL
;
1293 if (sc
->ale_cdata
.ale_buffer_tag
!= NULL
) {
1294 bus_dma_tag_destroy(sc
->ale_cdata
.ale_buffer_tag
);
1295 sc
->ale_cdata
.ale_buffer_tag
= NULL
;
1297 if (sc
->ale_cdata
.ale_parent_tag
!= NULL
) {
1298 bus_dma_tag_destroy(sc
->ale_cdata
.ale_parent_tag
);
1299 sc
->ale_cdata
.ale_parent_tag
= NULL
;
1304 ale_shutdown(device_t dev
)
1306 return (ale_suspend(dev
));
1312 * Note, this driver resets the link speed to 10/100Mbps by
1313 * restarting auto-negotiation in suspend/shutdown phase but we
1314 * don't know whether that auto-negotiation would succeed or not
1315 * as driver has no control after powering off/suspend operation.
1316 * If the renegotiation fail WOL may not work. Running at 1Gbps
1317 * will draw more power than 375mA at 3.3V which is specified in
1318 * PCI specification and that would result in complete
1319 * shutdowning power to ethernet controller.
1322 * Save current negotiated media speed/duplex/flow-control to
1323 * softc and restore the same link again after resuming. PHY
1324 * handling such as power down/resetting to 100Mbps may be better
1325 * handled in suspend method in phy driver.
1328 ale_setlinkspeed(struct ale_softc
*sc
)
1330 struct mii_data
*mii
;
1333 mii
= device_get_softc(sc
->ale_miibus
);
1336 if ((mii
->mii_media_status
& (IFM_ACTIVE
| IFM_AVALID
)) ==
1337 (IFM_ACTIVE
| IFM_AVALID
)) {
1338 switch IFM_SUBTYPE(mii
->mii_media_active
) {
1349 ale_miibus_writereg(sc
->ale_dev
, sc
->ale_phyaddr
, MII_100T2CR
, 0);
1350 ale_miibus_writereg(sc
->ale_dev
, sc
->ale_phyaddr
,
1351 MII_ANAR
, ANAR_TX_FD
| ANAR_TX
| ANAR_10_FD
| ANAR_10
| ANAR_CSMA
);
1352 ale_miibus_writereg(sc
->ale_dev
, sc
->ale_phyaddr
,
1353 MII_BMCR
, BMCR_RESET
| BMCR_AUTOEN
| BMCR_STARTNEG
);
1357 * Poll link state until ale(4) get a 10/100Mbps link.
1359 for (i
= 0; i
< MII_ANEGTICKS_GIGE
; i
++) {
1361 if ((mii
->mii_media_status
& (IFM_ACTIVE
| IFM_AVALID
))
1362 == (IFM_ACTIVE
| IFM_AVALID
)) {
1363 switch (IFM_SUBTYPE(
1364 mii
->mii_media_active
)) {
1374 pause("alelnk", hz
);
1377 if (i
== MII_ANEGTICKS_GIGE
)
1378 device_printf(sc
->ale_dev
,
1379 "establishing a link failed, WOL may not work!");
1382 * No link, force MAC to have 100Mbps, full-duplex link.
1383 * This is the last resort and may/may not work.
1385 mii
->mii_media_status
= IFM_AVALID
| IFM_ACTIVE
;
1386 mii
->mii_media_active
= IFM_ETHER
| IFM_100_TX
| IFM_FDX
;
1391 ale_setwol(struct ale_softc
*sc
)
1398 ALE_LOCK_ASSERT(sc
);
1400 if (pci_find_extcap(sc
->ale_dev
, PCIY_PMG
, &pmc
) != 0) {
1402 CSR_WRITE_4(sc
, ALE_WOL_CFG
, 0);
1403 reg
= CSR_READ_4(sc
, ALE_PCIE_PHYMISC
);
1404 reg
|= PCIE_PHYMISC_FORCE_RCV_DET
;
1405 CSR_WRITE_4(sc
, ALE_PCIE_PHYMISC
, reg
);
1406 /* Force PHY power down. */
1407 CSR_WRITE_2(sc
, ALE_GPHY_CTRL
,
1408 GPHY_CTRL_EXT_RESET
| GPHY_CTRL_HIB_EN
|
1409 GPHY_CTRL_HIB_PULSE
| GPHY_CTRL_PHY_PLL_ON
|
1410 GPHY_CTRL_SEL_ANA_RESET
| GPHY_CTRL_PHY_IDDQ
|
1411 GPHY_CTRL_PCLK_SEL_DIS
| GPHY_CTRL_PWDOWN_HW
);
1416 if ((ifp
->if_capenable
& IFCAP_WOL
) != 0) {
1417 if ((sc
->ale_flags
& ALE_FLAG_FASTETHER
) == 0)
1418 ale_setlinkspeed(sc
);
1422 if ((ifp
->if_capenable
& IFCAP_WOL_MAGIC
) != 0)
1423 pmcs
|= WOL_CFG_MAGIC
| WOL_CFG_MAGIC_ENB
;
1424 CSR_WRITE_4(sc
, ALE_WOL_CFG
, pmcs
);
1425 reg
= CSR_READ_4(sc
, ALE_MAC_CFG
);
1426 reg
&= ~(MAC_CFG_DBG
| MAC_CFG_PROMISC
| MAC_CFG_ALLMULTI
|
1428 if ((ifp
->if_capenable
& IFCAP_WOL_MCAST
) != 0)
1429 reg
|= MAC_CFG_ALLMULTI
| MAC_CFG_BCAST
;
1430 if ((ifp
->if_capenable
& IFCAP_WOL
) != 0)
1431 reg
|= MAC_CFG_RX_ENB
;
1432 CSR_WRITE_4(sc
, ALE_MAC_CFG
, reg
);
1434 if ((ifp
->if_capenable
& IFCAP_WOL
) == 0) {
1435 /* WOL disabled, PHY power down. */
1436 reg
= CSR_READ_4(sc
, ALE_PCIE_PHYMISC
);
1437 reg
|= PCIE_PHYMISC_FORCE_RCV_DET
;
1438 CSR_WRITE_4(sc
, ALE_PCIE_PHYMISC
, reg
);
1439 CSR_WRITE_2(sc
, ALE_GPHY_CTRL
,
1440 GPHY_CTRL_EXT_RESET
| GPHY_CTRL_HIB_EN
|
1441 GPHY_CTRL_HIB_PULSE
| GPHY_CTRL_SEL_ANA_RESET
|
1442 GPHY_CTRL_PHY_IDDQ
| GPHY_CTRL_PCLK_SEL_DIS
|
1443 GPHY_CTRL_PWDOWN_HW
);
1446 pmstat
= pci_read_config(sc
->ale_dev
, pmc
+ PCIR_POWER_STATUS
, 2);
1447 pmstat
&= ~(PCIM_PSTAT_PME
| PCIM_PSTAT_PMEENABLE
);
1448 if ((ifp
->if_capenable
& IFCAP_WOL
) != 0)
1449 pmstat
|= PCIM_PSTAT_PME
| PCIM_PSTAT_PMEENABLE
;
1450 pci_write_config(sc
->ale_dev
, pmc
+ PCIR_POWER_STATUS
, pmstat
, 2);
1456 ale_suspend(device_t dev
)
1458 struct ale_softc
*sc
= device_get_softc(dev
);
1459 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1461 lwkt_serialize_enter(ifp
->if_serializer
);
1466 lwkt_serialize_exit(ifp
->if_serializer
);
1471 ale_resume(device_t dev
)
1473 struct ale_softc
*sc
= device_get_softc(dev
);
1474 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1477 lwkt_serialize_enter(ifp
->if_serializer
);
1480 * Clear INTx emulation disable for hardwares that
1481 * is set in resume event. From Linux.
1483 cmd
= pci_read_config(sc
->ale_dev
, PCIR_COMMAND
, 2);
1484 if ((cmd
& 0x0400) != 0) {
1486 pci_write_config(sc
->ale_dev
, PCIR_COMMAND
, cmd
, 2);
1490 if (pci_find_extcap(sc
->ale_dev
, PCIY_PMG
, &pmc
) == 0) {
1494 /* Disable PME and clear PME status. */
1495 pmstat
= pci_read_config(sc
->ale_dev
,
1496 pmc
+ PCIR_POWER_STATUS
, 2);
1497 if ((pmstat
& PCIM_PSTAT_PMEENABLE
) != 0) {
1498 pmstat
&= ~PCIM_PSTAT_PMEENABLE
;
1499 pci_write_config(sc
->ale_dev
,
1500 pmc
+ PCIR_POWER_STATUS
, pmstat
, 2);
1507 if ((ifp
->if_flags
& IFF_UP
) != 0)
1510 lwkt_serialize_exit(ifp
->if_serializer
);
1515 ale_encap(struct ale_softc
*sc
, struct mbuf
**m_head
)
1517 struct ale_txdesc
*txd
, *txd_last
;
1518 struct tx_desc
*desc
;
1520 bus_dma_segment_t txsegs
[ALE_MAXTXSEGS
];
1521 struct ale_dmamap_ctx ctx
;
1523 uint32_t cflags
, poff
, vtag
;
1524 int error
, i
, nsegs
, prod
;
1526 M_ASSERTPKTHDR((*m_head
));
1532 prod
= sc
->ale_cdata
.ale_tx_prod
;
1533 txd
= &sc
->ale_cdata
.ale_txdesc
[prod
];
1535 map
= txd
->tx_dmamap
;
1537 ctx
.nsegs
= ALE_MAXTXSEGS
;
1539 error
= bus_dmamap_load_mbuf(sc
->ale_cdata
.ale_tx_tag
, map
,
1540 *m_head
, ale_dmamap_buf_cb
, &ctx
,
1542 if (error
== EFBIG
) {
1543 m
= m_defrag(*m_head
, M_NOWAIT
);
1551 ctx
.nsegs
= ALE_MAXTXSEGS
;
1553 error
= bus_dmamap_load_mbuf(sc
->ale_cdata
.ale_tx_tag
, map
,
1554 *m_head
, ale_dmamap_buf_cb
, &ctx
,
1561 } else if (error
!= 0) {
1572 /* Check descriptor overrun. */
1573 if (sc
->ale_cdata
.ale_tx_cnt
+ nsegs
>= ALE_TX_RING_CNT
- 2) {
1574 bus_dmamap_unload(sc
->ale_cdata
.ale_tx_tag
, map
);
1577 bus_dmamap_sync(sc
->ale_cdata
.ale_tx_tag
, map
, BUS_DMASYNC_PREWRITE
);
1580 /* Configure Tx checksum offload. */
1581 if ((m
->m_pkthdr
.csum_flags
& ALE_CSUM_FEATURES
) != 0) {
1583 * AR81xx supports Tx custom checksum offload feature
1584 * that offloads single 16bit checksum computation.
1585 * So you can choose one among IP, TCP and UDP.
1586 * Normally driver sets checksum start/insertion
1587 * position from the information of TCP/UDP frame as
1588 * TCP/UDP checksum takes more time than that of IP.
1589 * However it seems that custom checksum offload
1590 * requires 4 bytes aligned Tx buffers due to hardware
1592 * AR81xx also supports explicit Tx checksum computation
1593 * if it is told that the size of IP header and TCP
1594 * header(for UDP, the header size does not matter
1595 * because it's fixed length). However with this scheme
1596 * TSO does not work so you have to choose one either
1597 * TSO or explicit Tx checksum offload. I chosen TSO
1598 * plus custom checksum offload with work-around which
1599 * will cover most common usage for this consumer
1600 * ethernet controller. The work-around takes a lot of
1601 * CPU cycles if Tx buffer is not aligned on 4 bytes
1604 cflags
|= ALE_TD_CXSUM
;
1605 /* Set checksum start offset. */
1606 cflags
|= (poff
<< ALE_TD_CSUM_PLOADOFFSET_SHIFT
);
1607 /* Set checksum insertion position of TCP/UDP. */
1608 cflags
|= ((poff
+ m
->m_pkthdr
.csum_data
) <<
1609 ALE_TD_CSUM_XSUMOFFSET_SHIFT
);
1612 /* Configure VLAN hardware tag insertion. */
1613 if ((m
->m_flags
& M_VLANTAG
) != 0) {
1614 vtag
= ALE_TX_VLAN_TAG(m
->m_pkthdr
.ether_vlantag
);
1615 vtag
= ((vtag
<< ALE_TD_VLAN_SHIFT
) & ALE_TD_VLAN_MASK
);
1616 cflags
|= ALE_TD_INSERT_VLAN_TAG
;
1620 for (i
= 0; i
< nsegs
; i
++) {
1621 desc
= &sc
->ale_cdata
.ale_tx_ring
[prod
];
1622 desc
->addr
= htole64(txsegs
[i
].ds_addr
);
1623 desc
->len
= htole32(ALE_TX_BYTES(txsegs
[i
].ds_len
) | vtag
);
1624 desc
->flags
= htole32(cflags
);
1625 sc
->ale_cdata
.ale_tx_cnt
++;
1626 ALE_DESC_INC(prod
, ALE_TX_RING_CNT
);
1628 /* Update producer index. */
1629 sc
->ale_cdata
.ale_tx_prod
= prod
;
1631 /* Finally set EOP on the last descriptor. */
1632 prod
= (prod
+ ALE_TX_RING_CNT
- 1) % ALE_TX_RING_CNT
;
1633 desc
= &sc
->ale_cdata
.ale_tx_ring
[prod
];
1634 desc
->flags
|= htole32(ALE_TD_EOP
);
1636 /* Swap dmamap of the first and the last. */
1637 txd
= &sc
->ale_cdata
.ale_txdesc
[prod
];
1638 map
= txd_last
->tx_dmamap
;
1639 txd_last
->tx_dmamap
= txd
->tx_dmamap
;
1640 txd
->tx_dmamap
= map
;
1643 /* Sync descriptors. */
1644 bus_dmamap_sync(sc
->ale_cdata
.ale_tx_ring_tag
,
1645 sc
->ale_cdata
.ale_tx_ring_map
, BUS_DMASYNC_PREWRITE
);
1651 ale_start(struct ifnet
*ifp
, struct ifaltq_subque
*ifsq
)
1653 struct ale_softc
*sc
= ifp
->if_softc
;
1654 struct mbuf
*m_head
;
1657 ASSERT_ALTQ_SQ_DEFAULT(ifp
, ifsq
);
1658 ASSERT_SERIALIZED(ifp
->if_serializer
);
1660 if ((sc
->ale_flags
& ALE_FLAG_LINK
) == 0) {
1661 ifq_purge(&ifp
->if_snd
);
1665 if ((ifp
->if_flags
& IFF_RUNNING
) == 0 || ifq_is_oactive(&ifp
->if_snd
))
1668 /* Reclaim transmitted frames. */
1669 if (sc
->ale_cdata
.ale_tx_cnt
>= ALE_TX_DESC_HIWAT
)
1673 while (!ifq_is_empty(&ifp
->if_snd
)) {
1674 m_head
= ifq_dequeue(&ifp
->if_snd
);
1679 * Pack the data into the transmit ring. If we
1680 * don't have room, set the OACTIVE flag and wait
1681 * for the NIC to drain the ring.
1683 if (ale_encap(sc
, &m_head
)) {
1686 ifq_prepend(&ifp
->if_snd
, m_head
);
1687 ifq_set_oactive(&ifp
->if_snd
);
1693 * If there's a BPF listener, bounce a copy of this frame
1696 ETHER_BPF_MTAP(ifp
, m_head
);
1701 CSR_WRITE_4(sc
, ALE_MBOX_TPD_PROD_IDX
,
1702 sc
->ale_cdata
.ale_tx_prod
);
1704 /* Set a timeout in case the chip goes out to lunch. */
1705 ifp
->if_timer
= ALE_TX_TIMEOUT
;
1710 ale_watchdog(struct ifnet
*ifp
)
1712 struct ale_softc
*sc
= ifp
->if_softc
;
1714 ASSERT_SERIALIZED(ifp
->if_serializer
);
1716 if ((sc
->ale_flags
& ALE_FLAG_LINK
) == 0) {
1717 if_printf(ifp
, "watchdog timeout (lost link)\n");
1718 IFNET_STAT_INC(ifp
, oerrors
, 1);
1723 if_printf(ifp
, "watchdog timeout -- resetting\n");
1724 IFNET_STAT_INC(ifp
, oerrors
, 1);
1727 if (!ifq_is_empty(&ifp
->if_snd
))
1732 ale_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
1734 struct ale_softc
*sc
;
1736 struct mii_data
*mii
;
1739 ASSERT_SERIALIZED(ifp
->if_serializer
);
1742 ifr
= (struct ifreq
*)data
;
1747 if (ifr
->ifr_mtu
< ETHERMIN
|| ifr
->ifr_mtu
> ALE_JUMBO_MTU
||
1748 ((sc
->ale_flags
& ALE_FLAG_JUMBO
) == 0 &&
1749 ifr
->ifr_mtu
> ETHERMTU
))
1751 else if (ifp
->if_mtu
!= ifr
->ifr_mtu
) {
1752 ifp
->if_mtu
= ifr
->ifr_mtu
;
1753 if ((ifp
->if_flags
& IFF_RUNNING
) != 0)
1759 if ((ifp
->if_flags
& IFF_UP
) != 0) {
1760 if ((ifp
->if_flags
& IFF_RUNNING
) != 0) {
1761 if (((ifp
->if_flags
^ sc
->ale_if_flags
)
1762 & (IFF_PROMISC
| IFF_ALLMULTI
)) != 0)
1765 if ((sc
->ale_flags
& ALE_FLAG_DETACH
) == 0)
1769 if ((ifp
->if_flags
& IFF_RUNNING
) != 0)
1772 sc
->ale_if_flags
= ifp
->if_flags
;
1777 if ((ifp
->if_flags
& IFF_RUNNING
) != 0)
1783 mii
= device_get_softc(sc
->ale_miibus
);
1784 error
= ifmedia_ioctl(ifp
, ifr
, &mii
->mii_media
, cmd
);
1788 mask
= ifr
->ifr_reqcap
^ ifp
->if_capenable
;
1789 if ((mask
& IFCAP_TXCSUM
) != 0 &&
1790 (ifp
->if_capabilities
& IFCAP_TXCSUM
) != 0) {
1791 ifp
->if_capenable
^= IFCAP_TXCSUM
;
1792 if ((ifp
->if_capenable
& IFCAP_TXCSUM
) != 0)
1793 ifp
->if_hwassist
|= ALE_CSUM_FEATURES
;
1795 ifp
->if_hwassist
&= ~ALE_CSUM_FEATURES
;
1797 if ((mask
& IFCAP_RXCSUM
) != 0 &&
1798 (ifp
->if_capabilities
& IFCAP_RXCSUM
) != 0)
1799 ifp
->if_capenable
^= IFCAP_RXCSUM
;
1801 if ((mask
& IFCAP_VLAN_HWTAGGING
) != 0 &&
1802 (ifp
->if_capabilities
& IFCAP_VLAN_HWTAGGING
) != 0) {
1803 ifp
->if_capenable
^= IFCAP_VLAN_HWTAGGING
;
1809 error
= ether_ioctl(ifp
, cmd
, data
);
1816 ale_mac_config(struct ale_softc
*sc
)
1818 struct mii_data
*mii
;
1821 mii
= device_get_softc(sc
->ale_miibus
);
1822 reg
= CSR_READ_4(sc
, ALE_MAC_CFG
);
1823 reg
&= ~(MAC_CFG_FULL_DUPLEX
| MAC_CFG_TX_FC
| MAC_CFG_RX_FC
|
1824 MAC_CFG_SPEED_MASK
);
1825 /* Reprogram MAC with resolved speed/duplex. */
1826 switch (IFM_SUBTYPE(mii
->mii_media_active
)) {
1829 reg
|= MAC_CFG_SPEED_10_100
;
1832 reg
|= MAC_CFG_SPEED_1000
;
1835 if ((IFM_OPTIONS(mii
->mii_media_active
) & IFM_FDX
) != 0) {
1836 reg
|= MAC_CFG_FULL_DUPLEX
;
1838 if ((IFM_OPTIONS(mii
->mii_media_active
) & IFM_ETH_TXPAUSE
) != 0)
1839 reg
|= MAC_CFG_TX_FC
;
1840 if ((IFM_OPTIONS(mii
->mii_media_active
) & IFM_ETH_RXPAUSE
) != 0)
1841 reg
|= MAC_CFG_RX_FC
;
1844 CSR_WRITE_4(sc
, ALE_MAC_CFG
, reg
);
1848 ale_stats_clear(struct ale_softc
*sc
)
1854 for (reg
= &sb
.rx_frames
, i
= 0; reg
<= &sb
.rx_pkts_filtered
; reg
++) {
1855 CSR_READ_4(sc
, ALE_RX_MIB_BASE
+ i
);
1856 i
+= sizeof(uint32_t);
1858 /* Read Tx statistics. */
1859 for (reg
= &sb
.tx_frames
, i
= 0; reg
<= &sb
.tx_mcast_bytes
; reg
++) {
1860 CSR_READ_4(sc
, ALE_TX_MIB_BASE
+ i
);
1861 i
+= sizeof(uint32_t);
1866 ale_stats_update(struct ale_softc
*sc
)
1868 struct ale_hw_stats
*stat
;
1869 struct smb sb
, *smb
;
1874 ifp
= &sc
->arpcom
.ac_if
;
1875 stat
= &sc
->ale_stats
;
1878 /* Read Rx statistics. */
1879 for (reg
= &sb
.rx_frames
, i
= 0; reg
<= &sb
.rx_pkts_filtered
; reg
++) {
1880 *reg
= CSR_READ_4(sc
, ALE_RX_MIB_BASE
+ i
);
1881 i
+= sizeof(uint32_t);
1883 /* Read Tx statistics. */
1884 for (reg
= &sb
.tx_frames
, i
= 0; reg
<= &sb
.tx_mcast_bytes
; reg
++) {
1885 *reg
= CSR_READ_4(sc
, ALE_TX_MIB_BASE
+ i
);
1886 i
+= sizeof(uint32_t);
1890 stat
->rx_frames
+= smb
->rx_frames
;
1891 stat
->rx_bcast_frames
+= smb
->rx_bcast_frames
;
1892 stat
->rx_mcast_frames
+= smb
->rx_mcast_frames
;
1893 stat
->rx_pause_frames
+= smb
->rx_pause_frames
;
1894 stat
->rx_control_frames
+= smb
->rx_control_frames
;
1895 stat
->rx_crcerrs
+= smb
->rx_crcerrs
;
1896 stat
->rx_lenerrs
+= smb
->rx_lenerrs
;
1897 stat
->rx_bytes
+= smb
->rx_bytes
;
1898 stat
->rx_runts
+= smb
->rx_runts
;
1899 stat
->rx_fragments
+= smb
->rx_fragments
;
1900 stat
->rx_pkts_64
+= smb
->rx_pkts_64
;
1901 stat
->rx_pkts_65_127
+= smb
->rx_pkts_65_127
;
1902 stat
->rx_pkts_128_255
+= smb
->rx_pkts_128_255
;
1903 stat
->rx_pkts_256_511
+= smb
->rx_pkts_256_511
;
1904 stat
->rx_pkts_512_1023
+= smb
->rx_pkts_512_1023
;
1905 stat
->rx_pkts_1024_1518
+= smb
->rx_pkts_1024_1518
;
1906 stat
->rx_pkts_1519_max
+= smb
->rx_pkts_1519_max
;
1907 stat
->rx_pkts_truncated
+= smb
->rx_pkts_truncated
;
1908 stat
->rx_fifo_oflows
+= smb
->rx_fifo_oflows
;
1909 stat
->rx_rrs_errs
+= smb
->rx_rrs_errs
;
1910 stat
->rx_alignerrs
+= smb
->rx_alignerrs
;
1911 stat
->rx_bcast_bytes
+= smb
->rx_bcast_bytes
;
1912 stat
->rx_mcast_bytes
+= smb
->rx_mcast_bytes
;
1913 stat
->rx_pkts_filtered
+= smb
->rx_pkts_filtered
;
1916 stat
->tx_frames
+= smb
->tx_frames
;
1917 stat
->tx_bcast_frames
+= smb
->tx_bcast_frames
;
1918 stat
->tx_mcast_frames
+= smb
->tx_mcast_frames
;
1919 stat
->tx_pause_frames
+= smb
->tx_pause_frames
;
1920 stat
->tx_excess_defer
+= smb
->tx_excess_defer
;
1921 stat
->tx_control_frames
+= smb
->tx_control_frames
;
1922 stat
->tx_deferred
+= smb
->tx_deferred
;
1923 stat
->tx_bytes
+= smb
->tx_bytes
;
1924 stat
->tx_pkts_64
+= smb
->tx_pkts_64
;
1925 stat
->tx_pkts_65_127
+= smb
->tx_pkts_65_127
;
1926 stat
->tx_pkts_128_255
+= smb
->tx_pkts_128_255
;
1927 stat
->tx_pkts_256_511
+= smb
->tx_pkts_256_511
;
1928 stat
->tx_pkts_512_1023
+= smb
->tx_pkts_512_1023
;
1929 stat
->tx_pkts_1024_1518
+= smb
->tx_pkts_1024_1518
;
1930 stat
->tx_pkts_1519_max
+= smb
->tx_pkts_1519_max
;
1931 stat
->tx_single_colls
+= smb
->tx_single_colls
;
1932 stat
->tx_multi_colls
+= smb
->tx_multi_colls
;
1933 stat
->tx_late_colls
+= smb
->tx_late_colls
;
1934 stat
->tx_excess_colls
+= smb
->tx_excess_colls
;
1935 stat
->tx_abort
+= smb
->tx_abort
;
1936 stat
->tx_underrun
+= smb
->tx_underrun
;
1937 stat
->tx_desc_underrun
+= smb
->tx_desc_underrun
;
1938 stat
->tx_lenerrs
+= smb
->tx_lenerrs
;
1939 stat
->tx_pkts_truncated
+= smb
->tx_pkts_truncated
;
1940 stat
->tx_bcast_bytes
+= smb
->tx_bcast_bytes
;
1941 stat
->tx_mcast_bytes
+= smb
->tx_mcast_bytes
;
1943 /* Update counters in ifnet. */
1944 IFNET_STAT_INC(ifp
, opackets
, smb
->tx_frames
);
1946 IFNET_STAT_INC(ifp
, collisions
, smb
->tx_single_colls
+
1947 smb
->tx_multi_colls
* 2 + smb
->tx_late_colls
+
1948 smb
->tx_abort
* HDPX_CFG_RETRY_DEFAULT
);
1952 * tx_pkts_truncated counter looks suspicious. It constantly
1953 * increments with no sign of Tx errors. This may indicate
1954 * the counter name is not correct one so I've removed the
1955 * counter in output errors.
1957 IFNET_STAT_INC(ifp
, oerrors
, smb
->tx_abort
+ smb
->tx_late_colls
+
1960 IFNET_STAT_INC(ifp
, ipackets
, smb
->rx_frames
);
1962 IFNET_STAT_INC(ifp
, ierrors
, smb
->rx_crcerrs
+ smb
->rx_lenerrs
+
1963 smb
->rx_runts
+ smb
->rx_pkts_truncated
+
1964 smb
->rx_fifo_oflows
+ smb
->rx_rrs_errs
+
1971 struct ale_softc
*sc
= xsc
;
1972 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1975 ASSERT_SERIALIZED(ifp
->if_serializer
);
1977 status
= CSR_READ_4(sc
, ALE_INTR_STATUS
);
1978 if ((status
& ALE_INTRS
) == 0)
1981 /* Acknowledge and disable interrupts. */
1982 CSR_WRITE_4(sc
, ALE_INTR_STATUS
, status
| INTR_DIS_INT
);
1984 if ((ifp
->if_flags
& IFF_RUNNING
) != 0) {
1987 error
= ale_rxeof(sc
);
1989 sc
->ale_stats
.reset_brk_seq
++;
1994 if ((status
& (INTR_DMA_RD_TO_RST
| INTR_DMA_WR_TO_RST
)) != 0) {
1995 if ((status
& INTR_DMA_RD_TO_RST
) != 0)
1996 device_printf(sc
->ale_dev
,
1997 "DMA read error! -- resetting\n");
1998 if ((status
& INTR_DMA_WR_TO_RST
) != 0)
1999 device_printf(sc
->ale_dev
,
2000 "DMA write error! -- resetting\n");
2006 if (!ifq_is_empty(&ifp
->if_snd
))
2010 /* Re-enable interrupts. */
2011 CSR_WRITE_4(sc
, ALE_INTR_STATUS
, 0x7FFFFFFF);
2015 ale_txeof(struct ale_softc
*sc
)
2017 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
2018 struct ale_txdesc
*txd
;
2019 uint32_t cons
, prod
;
2022 if (sc
->ale_cdata
.ale_tx_cnt
== 0)
2025 bus_dmamap_sync(sc
->ale_cdata
.ale_tx_ring_tag
,
2026 sc
->ale_cdata
.ale_tx_ring_map
, BUS_DMASYNC_POSTREAD
);
2027 if ((sc
->ale_flags
& ALE_FLAG_TXCMB_BUG
) == 0) {
2028 bus_dmamap_sync(sc
->ale_cdata
.ale_tx_cmb_tag
,
2029 sc
->ale_cdata
.ale_tx_cmb_map
, BUS_DMASYNC_POSTREAD
);
2030 prod
= *sc
->ale_cdata
.ale_tx_cmb
& TPD_CNT_MASK
;
2032 prod
= CSR_READ_2(sc
, ALE_TPD_CONS_IDX
);
2033 cons
= sc
->ale_cdata
.ale_tx_cons
;
2035 * Go through our Tx list and free mbufs for those
2036 * frames which have been transmitted.
2038 for (prog
= 0; cons
!= prod
; prog
++,
2039 ALE_DESC_INC(cons
, ALE_TX_RING_CNT
)) {
2040 if (sc
->ale_cdata
.ale_tx_cnt
<= 0)
2043 ifq_clr_oactive(&ifp
->if_snd
);
2044 sc
->ale_cdata
.ale_tx_cnt
--;
2045 txd
= &sc
->ale_cdata
.ale_txdesc
[cons
];
2046 if (txd
->tx_m
!= NULL
) {
2047 /* Reclaim transmitted mbufs. */
2048 bus_dmamap_unload(sc
->ale_cdata
.ale_tx_tag
,
2056 sc
->ale_cdata
.ale_tx_cons
= cons
;
2058 * Unarm watchdog timer only when there is no pending
2059 * Tx descriptors in queue.
2061 if (sc
->ale_cdata
.ale_tx_cnt
== 0)
2067 ale_rx_update_page(struct ale_softc
*sc
, struct ale_rx_page
**page
,
2068 uint32_t length
, uint32_t *prod
)
2070 struct ale_rx_page
*rx_page
;
2073 /* Update consumer position. */
2074 rx_page
->cons
+= roundup(length
+ sizeof(struct rx_rs
),
2076 if (rx_page
->cons
>= ALE_RX_PAGE_SZ
) {
2078 * End of Rx page reached, let hardware reuse
2082 *rx_page
->cmb_addr
= 0;
2083 bus_dmamap_sync(rx_page
->cmb_tag
, rx_page
->cmb_map
,
2084 BUS_DMASYNC_PREWRITE
);
2085 CSR_WRITE_1(sc
, ALE_RXF0_PAGE0
+ sc
->ale_cdata
.ale_rx_curp
,
2087 /* Switch to alternate Rx page. */
2088 sc
->ale_cdata
.ale_rx_curp
^= 1;
2090 &sc
->ale_cdata
.ale_rx_page
[sc
->ale_cdata
.ale_rx_curp
];
2091 /* Page flipped, sync CMB and Rx page. */
2092 bus_dmamap_sync(rx_page
->page_tag
, rx_page
->page_map
,
2093 BUS_DMASYNC_POSTREAD
);
2094 bus_dmamap_sync(rx_page
->cmb_tag
, rx_page
->cmb_map
,
2095 BUS_DMASYNC_POSTREAD
);
2096 /* Sync completed, cache updated producer index. */
2097 *prod
= *rx_page
->cmb_addr
;
2103 * It seems that AR81xx controller can compute partial checksum.
2104 * The partial checksum value can be used to accelerate checksum
2105 * computation for fragmented TCP/UDP packets. Upper network stack
2106 * already takes advantage of the partial checksum value in IP
2107 * reassembly stage. But I'm not sure the correctness of the
2108 * partial hardware checksum assistance due to lack of data sheet.
2109 * In addition, the Rx feature of controller that requires copying
2110 * for every frames effectively nullifies one of most nice offload
2111 * capability of controller.
2114 ale_rxcsum(struct ale_softc
*sc
, struct mbuf
*m
, uint32_t status
)
2116 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
2120 m
->m_pkthdr
.csum_flags
|= CSUM_IP_CHECKED
;
2121 if ((status
& ALE_RD_IPCSUM_NOK
) == 0)
2122 m
->m_pkthdr
.csum_flags
|= CSUM_IP_VALID
;
2124 if ((sc
->ale_flags
& ALE_FLAG_RXCSUM_BUG
) == 0) {
2125 if (((status
& ALE_RD_IPV4_FRAG
) == 0) &&
2126 ((status
& (ALE_RD_TCP
| ALE_RD_UDP
)) != 0) &&
2127 ((status
& ALE_RD_TCP_UDPCSUM_NOK
) == 0)) {
2128 m
->m_pkthdr
.csum_flags
|=
2129 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
2130 m
->m_pkthdr
.csum_data
= 0xffff;
2133 if ((status
& (ALE_RD_TCP
| ALE_RD_UDP
)) != 0 &&
2134 (status
& ALE_RD_TCP_UDPCSUM_NOK
) == 0) {
2135 p
= mtod(m
, char *);
2137 if ((status
& ALE_RD_802_3
) != 0)
2138 p
+= LLC_SNAPFRAMELEN
;
2139 if ((ifp
->if_capenable
& IFCAP_VLAN_HWTAGGING
) == 0 &&
2140 (status
& ALE_RD_VLAN
) != 0)
2142 ip
= (struct ip
*)p
;
2143 if (ip
->ip_off
!= 0 && (status
& ALE_RD_IPV4_DF
) == 0)
2145 m
->m_pkthdr
.csum_flags
|= CSUM_DATA_VALID
|
2147 m
->m_pkthdr
.csum_data
= 0xffff;
2151 * Don't mark bad checksum for TCP/UDP frames
2152 * as fragmented frames may always have set
2153 * bad checksummed bit of frame status.
2157 /* Process received frames. */
2159 ale_rxeof(struct ale_softc
*sc
)
2161 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
2162 struct ale_rx_page
*rx_page
;
2165 uint32_t length
, prod
, seqno
, status
, vtags
;
2168 rx_page
= &sc
->ale_cdata
.ale_rx_page
[sc
->ale_cdata
.ale_rx_curp
];
2169 bus_dmamap_sync(rx_page
->cmb_tag
, rx_page
->cmb_map
,
2170 BUS_DMASYNC_POSTREAD
);
2171 bus_dmamap_sync(rx_page
->page_tag
, rx_page
->page_map
,
2172 BUS_DMASYNC_POSTREAD
);
2174 * Don't directly access producer index as hardware may
2175 * update it while Rx handler is in progress. It would
2176 * be even better if there is a way to let hardware
2177 * know how far driver processed its received frames.
2178 * Alternatively, hardware could provide a way to disable
2179 * CMB updates until driver acknowledges the end of CMB
2182 prod
= *rx_page
->cmb_addr
;
2183 for (prog
= 0; ; prog
++) {
2184 if (rx_page
->cons
>= prod
)
2186 rs
= (struct rx_rs
*)(rx_page
->page_addr
+ rx_page
->cons
);
2187 seqno
= ALE_RX_SEQNO(le32toh(rs
->seqno
));
2188 if (sc
->ale_cdata
.ale_rx_seqno
!= seqno
) {
2190 * Normally I believe this should not happen unless
2191 * severe driver bug or corrupted memory. However
2192 * it seems to happen under certain conditions which
2193 * is triggered by abrupt Rx events such as initiation
2194 * of bulk transfer of remote host. It's not easy to
2195 * reproduce this and I doubt it could be related
2196 * with FIFO overflow of hardware or activity of Tx
2197 * CMB updates. I also remember similar behaviour
2198 * seen on RealTek 8139 which uses resembling Rx
2202 device_printf(sc
->ale_dev
,
2203 "garbled seq: %u, expected: %u -- "
2204 "resetting!\n", seqno
,
2205 sc
->ale_cdata
.ale_rx_seqno
);
2208 /* Frame received. */
2209 sc
->ale_cdata
.ale_rx_seqno
++;
2210 length
= ALE_RX_BYTES(le32toh(rs
->length
));
2211 status
= le32toh(rs
->flags
);
2212 if ((status
& ALE_RD_ERROR
) != 0) {
2214 * We want to pass the following frames to upper
2215 * layer regardless of error status of Rx return
2218 * o IP/TCP/UDP checksum is bad.
2219 * o frame length and protocol specific length
2222 if ((status
& (ALE_RD_CRC
| ALE_RD_CODE
|
2223 ALE_RD_DRIBBLE
| ALE_RD_RUNT
| ALE_RD_OFLOW
|
2224 ALE_RD_TRUNC
)) != 0) {
2225 ale_rx_update_page(sc
, &rx_page
, length
, &prod
);
2230 * m_devget(9) is major bottle-neck of ale(4) (It comes
2231 * from hardware limitation). For jumbo frames we could
2232 * get a slightly better performance if driver use
2233 * m_getjcl(9) with proper buffer size argument. However
2234 * that would make code more complicated and I don't
2235 * think users would expect good Rx performance numbers
2236 * on these low-end consumer ethernet controller.
2238 m
= m_devget((char *)(rs
+ 1), length
- ETHER_CRC_LEN
,
2241 IFNET_STAT_INC(ifp
, iqdrops
, 1);
2242 ale_rx_update_page(sc
, &rx_page
, length
, &prod
);
2245 if ((ifp
->if_capenable
& IFCAP_RXCSUM
) != 0 &&
2246 (status
& ALE_RD_IPV4
) != 0)
2247 ale_rxcsum(sc
, m
, status
);
2248 if ((ifp
->if_capenable
& IFCAP_VLAN_HWTAGGING
) != 0 &&
2249 (status
& ALE_RD_VLAN
) != 0) {
2250 vtags
= ALE_RX_VLAN(le32toh(rs
->vtags
));
2251 m
->m_pkthdr
.ether_vlantag
= ALE_RX_VLAN_TAG(vtags
);
2252 m
->m_flags
|= M_VLANTAG
;
2255 /* Pass it to upper layer. */
2256 ifp
->if_input(ifp
, m
, NULL
, -1);
2258 ale_rx_update_page(sc
, &rx_page
, length
, &prod
);
2266 struct ale_softc
*sc
= xsc
;
2267 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
2268 struct mii_data
*mii
;
2270 lwkt_serialize_enter(ifp
->if_serializer
);
2272 mii
= device_get_softc(sc
->ale_miibus
);
2274 ale_stats_update(sc
);
2276 callout_reset(&sc
->ale_tick_ch
, hz
, ale_tick
, sc
);
2278 lwkt_serialize_exit(ifp
->if_serializer
);
2282 ale_reset(struct ale_softc
*sc
)
2287 /* Initialize PCIe module. From Linux. */
2288 CSR_WRITE_4(sc
, 0x1008, CSR_READ_4(sc
, 0x1008) | 0x8000);
2290 CSR_WRITE_4(sc
, ALE_MASTER_CFG
, MASTER_RESET
);
2291 for (i
= ALE_RESET_TIMEOUT
; i
> 0; i
--) {
2293 if ((CSR_READ_4(sc
, ALE_MASTER_CFG
) & MASTER_RESET
) == 0)
2297 device_printf(sc
->ale_dev
, "master reset timeout!\n");
2299 for (i
= ALE_RESET_TIMEOUT
; i
> 0; i
--) {
2300 if ((reg
= CSR_READ_4(sc
, ALE_IDLE_STATUS
)) == 0)
2306 device_printf(sc
->ale_dev
, "reset timeout(0x%08x)!\n", reg
);
2312 struct ale_softc
*sc
= xsc
;
2313 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
2314 struct mii_data
*mii
;
2315 uint8_t eaddr
[ETHER_ADDR_LEN
];
2317 uint32_t reg
, rxf_hi
, rxf_lo
;
2319 ASSERT_SERIALIZED(ifp
->if_serializer
);
2321 mii
= device_get_softc(sc
->ale_miibus
);
2324 * Cancel any pending I/O.
2329 * Reset the chip to a known state.
2333 /* Initialize Tx descriptors, DMA memory blocks. */
2334 ale_init_rx_pages(sc
);
2335 ale_init_tx_ring(sc
);
2337 /* Reprogram the station address. */
2338 bcopy(IF_LLADDR(ifp
), eaddr
, ETHER_ADDR_LEN
);
2339 CSR_WRITE_4(sc
, ALE_PAR0
,
2340 eaddr
[2] << 24 | eaddr
[3] << 16 | eaddr
[4] << 8 | eaddr
[5]);
2341 CSR_WRITE_4(sc
, ALE_PAR1
, eaddr
[0] << 8 | eaddr
[1]);
2344 * Clear WOL status and disable all WOL feature as WOL
2345 * would interfere Rx operation under normal environments.
2347 CSR_READ_4(sc
, ALE_WOL_CFG
);
2348 CSR_WRITE_4(sc
, ALE_WOL_CFG
, 0);
2351 * Set Tx descriptor/RXF0/CMB base addresses. They share
2352 * the same high address part of DMAable region.
2354 paddr
= sc
->ale_cdata
.ale_tx_ring_paddr
;
2355 CSR_WRITE_4(sc
, ALE_TPD_ADDR_HI
, ALE_ADDR_HI(paddr
));
2356 CSR_WRITE_4(sc
, ALE_TPD_ADDR_LO
, ALE_ADDR_LO(paddr
));
2357 CSR_WRITE_4(sc
, ALE_TPD_CNT
,
2358 (ALE_TX_RING_CNT
<< TPD_CNT_SHIFT
) & TPD_CNT_MASK
);
2360 /* Set Rx page base address, note we use single queue. */
2361 paddr
= sc
->ale_cdata
.ale_rx_page
[0].page_paddr
;
2362 CSR_WRITE_4(sc
, ALE_RXF0_PAGE0_ADDR_LO
, ALE_ADDR_LO(paddr
));
2363 paddr
= sc
->ale_cdata
.ale_rx_page
[1].page_paddr
;
2364 CSR_WRITE_4(sc
, ALE_RXF0_PAGE1_ADDR_LO
, ALE_ADDR_LO(paddr
));
2366 /* Set Tx/Rx CMB addresses. */
2367 paddr
= sc
->ale_cdata
.ale_tx_cmb_paddr
;
2368 CSR_WRITE_4(sc
, ALE_TX_CMB_ADDR_LO
, ALE_ADDR_LO(paddr
));
2369 paddr
= sc
->ale_cdata
.ale_rx_page
[0].cmb_paddr
;
2370 CSR_WRITE_4(sc
, ALE_RXF0_CMB0_ADDR_LO
, ALE_ADDR_LO(paddr
));
2371 paddr
= sc
->ale_cdata
.ale_rx_page
[1].cmb_paddr
;
2372 CSR_WRITE_4(sc
, ALE_RXF0_CMB1_ADDR_LO
, ALE_ADDR_LO(paddr
));
2374 /* Mark RXF0 is valid. */
2375 CSR_WRITE_1(sc
, ALE_RXF0_PAGE0
, RXF_VALID
);
2376 CSR_WRITE_1(sc
, ALE_RXF0_PAGE1
, RXF_VALID
);
2378 * No need to initialize RFX1/RXF2/RXF3. We don't use
2382 /* Set Rx page size, excluding guard frame size. */
2383 CSR_WRITE_4(sc
, ALE_RXF_PAGE_SIZE
, ALE_RX_PAGE_SZ
);
2385 /* Tell hardware that we're ready to load DMA blocks. */
2386 CSR_WRITE_4(sc
, ALE_DMA_BLOCK
, DMA_BLOCK_LOAD
);
2388 /* Set Rx/Tx interrupt trigger threshold. */
2389 CSR_WRITE_4(sc
, ALE_INT_TRIG_THRESH
, (1 << INT_TRIG_RX_THRESH_SHIFT
) |
2390 (4 << INT_TRIG_TX_THRESH_SHIFT
));
2393 * Set interrupt trigger timer, its purpose and relation
2394 * with interrupt moderation mechanism is not clear yet.
2396 CSR_WRITE_4(sc
, ALE_INT_TRIG_TIMER
,
2397 ((ALE_USECS(10) << INT_TRIG_RX_TIMER_SHIFT
) |
2398 (ALE_USECS(1000) << INT_TRIG_TX_TIMER_SHIFT
)));
2400 /* Configure interrupt moderation timer. */
2401 reg
= ALE_USECS(sc
->ale_int_rx_mod
) << IM_TIMER_RX_SHIFT
;
2402 reg
|= ALE_USECS(sc
->ale_int_tx_mod
) << IM_TIMER_TX_SHIFT
;
2403 CSR_WRITE_4(sc
, ALE_IM_TIMER
, reg
);
2404 reg
= CSR_READ_4(sc
, ALE_MASTER_CFG
);
2405 reg
&= ~(MASTER_CHIP_REV_MASK
| MASTER_CHIP_ID_MASK
);
2406 reg
&= ~(MASTER_IM_RX_TIMER_ENB
| MASTER_IM_TX_TIMER_ENB
);
2407 if (ALE_USECS(sc
->ale_int_rx_mod
) != 0)
2408 reg
|= MASTER_IM_RX_TIMER_ENB
;
2409 if (ALE_USECS(sc
->ale_int_tx_mod
) != 0)
2410 reg
|= MASTER_IM_TX_TIMER_ENB
;
2411 CSR_WRITE_4(sc
, ALE_MASTER_CFG
, reg
);
2412 CSR_WRITE_2(sc
, ALE_INTR_CLR_TIMER
, ALE_USECS(1000));
2414 /* Set Maximum frame size of controller. */
2415 if (ifp
->if_mtu
< ETHERMTU
)
2416 sc
->ale_max_frame_size
= ETHERMTU
;
2418 sc
->ale_max_frame_size
= ifp
->if_mtu
;
2419 sc
->ale_max_frame_size
+= ETHER_HDR_LEN
+ EVL_ENCAPLEN
+ ETHER_CRC_LEN
;
2420 CSR_WRITE_4(sc
, ALE_FRAME_SIZE
, sc
->ale_max_frame_size
);
2422 /* Configure IPG/IFG parameters. */
2423 CSR_WRITE_4(sc
, ALE_IPG_IFG_CFG
,
2424 ((IPG_IFG_IPGT_DEFAULT
<< IPG_IFG_IPGT_SHIFT
) & IPG_IFG_IPGT_MASK
) |
2425 ((IPG_IFG_MIFG_DEFAULT
<< IPG_IFG_MIFG_SHIFT
) & IPG_IFG_MIFG_MASK
) |
2426 ((IPG_IFG_IPG1_DEFAULT
<< IPG_IFG_IPG1_SHIFT
) & IPG_IFG_IPG1_MASK
) |
2427 ((IPG_IFG_IPG2_DEFAULT
<< IPG_IFG_IPG2_SHIFT
) & IPG_IFG_IPG2_MASK
));
2429 /* Set parameters for half-duplex media. */
2430 CSR_WRITE_4(sc
, ALE_HDPX_CFG
,
2431 ((HDPX_CFG_LCOL_DEFAULT
<< HDPX_CFG_LCOL_SHIFT
) &
2432 HDPX_CFG_LCOL_MASK
) |
2433 ((HDPX_CFG_RETRY_DEFAULT
<< HDPX_CFG_RETRY_SHIFT
) &
2434 HDPX_CFG_RETRY_MASK
) | HDPX_CFG_EXC_DEF_EN
|
2435 ((HDPX_CFG_ABEBT_DEFAULT
<< HDPX_CFG_ABEBT_SHIFT
) &
2436 HDPX_CFG_ABEBT_MASK
) |
2437 ((HDPX_CFG_JAMIPG_DEFAULT
<< HDPX_CFG_JAMIPG_SHIFT
) &
2438 HDPX_CFG_JAMIPG_MASK
));
2440 /* Configure Tx jumbo frame parameters. */
2441 if ((sc
->ale_flags
& ALE_FLAG_JUMBO
) != 0) {
2442 if (ifp
->if_mtu
< ETHERMTU
)
2443 reg
= sc
->ale_max_frame_size
;
2444 else if (ifp
->if_mtu
< 6 * 1024)
2445 reg
= (sc
->ale_max_frame_size
* 2) / 3;
2447 reg
= sc
->ale_max_frame_size
/ 2;
2448 CSR_WRITE_4(sc
, ALE_TX_JUMBO_THRESH
,
2449 roundup(reg
, TX_JUMBO_THRESH_UNIT
) >>
2450 TX_JUMBO_THRESH_UNIT_SHIFT
);
2453 /* Configure TxQ. */
2454 reg
= (128 << (sc
->ale_dma_rd_burst
>> DMA_CFG_RD_BURST_SHIFT
))
2455 << TXQ_CFG_TX_FIFO_BURST_SHIFT
;
2456 reg
|= (TXQ_CFG_TPD_BURST_DEFAULT
<< TXQ_CFG_TPD_BURST_SHIFT
) &
2457 TXQ_CFG_TPD_BURST_MASK
;
2458 CSR_WRITE_4(sc
, ALE_TXQ_CFG
, reg
| TXQ_CFG_ENHANCED_MODE
| TXQ_CFG_ENB
);
2460 /* Configure Rx jumbo frame & flow control parameters. */
2461 if ((sc
->ale_flags
& ALE_FLAG_JUMBO
) != 0) {
2462 reg
= roundup(sc
->ale_max_frame_size
, RX_JUMBO_THRESH_UNIT
);
2463 CSR_WRITE_4(sc
, ALE_RX_JUMBO_THRESH
,
2464 (((reg
>> RX_JUMBO_THRESH_UNIT_SHIFT
) <<
2465 RX_JUMBO_THRESH_MASK_SHIFT
) & RX_JUMBO_THRESH_MASK
) |
2466 ((RX_JUMBO_LKAH_DEFAULT
<< RX_JUMBO_LKAH_SHIFT
) &
2467 RX_JUMBO_LKAH_MASK
));
2468 reg
= CSR_READ_4(sc
, ALE_SRAM_RX_FIFO_LEN
);
2469 rxf_hi
= (reg
* 7) / 10;
2470 rxf_lo
= (reg
* 3)/ 10;
2471 CSR_WRITE_4(sc
, ALE_RX_FIFO_PAUSE_THRESH
,
2472 ((rxf_lo
<< RX_FIFO_PAUSE_THRESH_LO_SHIFT
) &
2473 RX_FIFO_PAUSE_THRESH_LO_MASK
) |
2474 ((rxf_hi
<< RX_FIFO_PAUSE_THRESH_HI_SHIFT
) &
2475 RX_FIFO_PAUSE_THRESH_HI_MASK
));
2479 CSR_WRITE_4(sc
, ALE_RSS_IDT_TABLE0
, 0);
2480 CSR_WRITE_4(sc
, ALE_RSS_CPU
, 0);
2482 /* Configure RxQ. */
2483 CSR_WRITE_4(sc
, ALE_RXQ_CFG
,
2484 RXQ_CFG_ALIGN_32
| RXQ_CFG_CUT_THROUGH_ENB
| RXQ_CFG_ENB
);
2486 /* Configure DMA parameters. */
2488 if ((sc
->ale_flags
& ALE_FLAG_TXCMB_BUG
) == 0)
2489 reg
|= DMA_CFG_TXCMB_ENB
;
2490 CSR_WRITE_4(sc
, ALE_DMA_CFG
,
2491 DMA_CFG_OUT_ORDER
| DMA_CFG_RD_REQ_PRI
| DMA_CFG_RCB_64
|
2492 sc
->ale_dma_rd_burst
| reg
|
2493 sc
->ale_dma_wr_burst
| DMA_CFG_RXCMB_ENB
|
2494 ((DMA_CFG_RD_DELAY_CNT_DEFAULT
<< DMA_CFG_RD_DELAY_CNT_SHIFT
) &
2495 DMA_CFG_RD_DELAY_CNT_MASK
) |
2496 ((DMA_CFG_WR_DELAY_CNT_DEFAULT
<< DMA_CFG_WR_DELAY_CNT_SHIFT
) &
2497 DMA_CFG_WR_DELAY_CNT_MASK
));
2500 * Hardware can be configured to issue SMB interrupt based
2501 * on programmed interval. Since there is a callout that is
2502 * invoked for every hz in driver we use that instead of
2503 * relying on periodic SMB interrupt.
2505 CSR_WRITE_4(sc
, ALE_SMB_STAT_TIMER
, ALE_USECS(0));
2507 /* Clear MAC statistics. */
2508 ale_stats_clear(sc
);
2511 * Configure Tx/Rx MACs.
2512 * - Auto-padding for short frames.
2513 * - Enable CRC generation.
2514 * Actual reconfiguration of MAC for resolved speed/duplex
2515 * is followed after detection of link establishment.
2516 * AR81xx always does checksum computation regardless of
2517 * MAC_CFG_RXCSUM_ENB bit. In fact, setting the bit will
2518 * cause Rx handling issue for fragmented IP datagrams due
2521 reg
= MAC_CFG_TX_CRC_ENB
| MAC_CFG_TX_AUTO_PAD
| MAC_CFG_FULL_DUPLEX
|
2522 ((MAC_CFG_PREAMBLE_DEFAULT
<< MAC_CFG_PREAMBLE_SHIFT
) &
2523 MAC_CFG_PREAMBLE_MASK
);
2524 if ((sc
->ale_flags
& ALE_FLAG_FASTETHER
) != 0)
2525 reg
|= MAC_CFG_SPEED_10_100
;
2527 reg
|= MAC_CFG_SPEED_1000
;
2528 CSR_WRITE_4(sc
, ALE_MAC_CFG
, reg
);
2530 /* Set up the receive filter. */
2534 /* Acknowledge all pending interrupts and clear it. */
2535 CSR_WRITE_4(sc
, ALE_INTR_MASK
, ALE_INTRS
);
2536 CSR_WRITE_4(sc
, ALE_INTR_STATUS
, 0xFFFFFFFF);
2537 CSR_WRITE_4(sc
, ALE_INTR_STATUS
, 0);
2539 sc
->ale_flags
&= ~ALE_FLAG_LINK
;
2541 /* Switch to the current media. */
2544 callout_reset(&sc
->ale_tick_ch
, hz
, ale_tick
, sc
);
2546 ifp
->if_flags
|= IFF_RUNNING
;
2547 ifq_clr_oactive(&ifp
->if_snd
);
2551 ale_stop(struct ale_softc
*sc
)
2553 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
2554 struct ale_txdesc
*txd
;
2558 ASSERT_SERIALIZED(ifp
->if_serializer
);
2561 * Mark the interface down and cancel the watchdog timer.
2563 ifp
->if_flags
&= ~IFF_RUNNING
;
2564 ifq_clr_oactive(&ifp
->if_snd
);
2567 callout_stop(&sc
->ale_tick_ch
);
2568 sc
->ale_flags
&= ~ALE_FLAG_LINK
;
2570 ale_stats_update(sc
);
2572 /* Disable interrupts. */
2573 CSR_WRITE_4(sc
, ALE_INTR_MASK
, 0);
2574 CSR_WRITE_4(sc
, ALE_INTR_STATUS
, 0xFFFFFFFF);
2576 /* Disable queue processing and DMA. */
2577 reg
= CSR_READ_4(sc
, ALE_TXQ_CFG
);
2578 reg
&= ~TXQ_CFG_ENB
;
2579 CSR_WRITE_4(sc
, ALE_TXQ_CFG
, reg
);
2580 reg
= CSR_READ_4(sc
, ALE_RXQ_CFG
);
2581 reg
&= ~RXQ_CFG_ENB
;
2582 CSR_WRITE_4(sc
, ALE_RXQ_CFG
, reg
);
2583 reg
= CSR_READ_4(sc
, ALE_DMA_CFG
);
2584 reg
&= ~(DMA_CFG_TXCMB_ENB
| DMA_CFG_RXCMB_ENB
);
2585 CSR_WRITE_4(sc
, ALE_DMA_CFG
, reg
);
2588 /* Stop Rx/Tx MACs. */
2591 /* Disable interrupts again? XXX */
2592 CSR_WRITE_4(sc
, ALE_INTR_STATUS
, 0xFFFFFFFF);
2595 * Free TX mbufs still in the queues.
2597 for (i
= 0; i
< ALE_TX_RING_CNT
; i
++) {
2598 txd
= &sc
->ale_cdata
.ale_txdesc
[i
];
2599 if (txd
->tx_m
!= NULL
) {
2600 bus_dmamap_unload(sc
->ale_cdata
.ale_tx_tag
,
2609 ale_stop_mac(struct ale_softc
*sc
)
2614 reg
= CSR_READ_4(sc
, ALE_MAC_CFG
);
2615 if ((reg
& (MAC_CFG_TX_ENB
| MAC_CFG_RX_ENB
)) != 0) {
2616 reg
&= ~MAC_CFG_TX_ENB
| MAC_CFG_RX_ENB
;
2617 CSR_WRITE_4(sc
, ALE_MAC_CFG
, reg
);
2620 for (i
= ALE_TIMEOUT
; i
> 0; i
--) {
2621 reg
= CSR_READ_4(sc
, ALE_IDLE_STATUS
);
2627 device_printf(sc
->ale_dev
,
2628 "could not disable Tx/Rx MAC(0x%08x)!\n", reg
);
2632 ale_init_tx_ring(struct ale_softc
*sc
)
2634 struct ale_txdesc
*txd
;
2637 sc
->ale_cdata
.ale_tx_prod
= 0;
2638 sc
->ale_cdata
.ale_tx_cons
= 0;
2639 sc
->ale_cdata
.ale_tx_cnt
= 0;
2641 bzero(sc
->ale_cdata
.ale_tx_ring
, ALE_TX_RING_SZ
);
2642 bzero(sc
->ale_cdata
.ale_tx_cmb
, ALE_TX_CMB_SZ
);
2643 for (i
= 0; i
< ALE_TX_RING_CNT
; i
++) {
2644 txd
= &sc
->ale_cdata
.ale_txdesc
[i
];
2647 *sc
->ale_cdata
.ale_tx_cmb
= 0;
2648 bus_dmamap_sync(sc
->ale_cdata
.ale_tx_cmb_tag
,
2649 sc
->ale_cdata
.ale_tx_cmb_map
,
2650 BUS_DMASYNC_PREWRITE
);
2651 bus_dmamap_sync(sc
->ale_cdata
.ale_tx_ring_tag
,
2652 sc
->ale_cdata
.ale_tx_ring_map
,
2653 BUS_DMASYNC_PREWRITE
);
2657 ale_init_rx_pages(struct ale_softc
*sc
)
2659 struct ale_rx_page
*rx_page
;
2662 sc
->ale_cdata
.ale_rx_seqno
= 0;
2663 sc
->ale_cdata
.ale_rx_curp
= 0;
2665 for (i
= 0; i
< ALE_RX_PAGES
; i
++) {
2666 rx_page
= &sc
->ale_cdata
.ale_rx_page
[i
];
2667 bzero(rx_page
->page_addr
, sc
->ale_pagesize
);
2668 bzero(rx_page
->cmb_addr
, ALE_RX_CMB_SZ
);
2670 *rx_page
->cmb_addr
= 0;
2671 bus_dmamap_sync(rx_page
->page_tag
, rx_page
->page_map
,
2672 BUS_DMASYNC_PREWRITE
);
2673 bus_dmamap_sync(rx_page
->cmb_tag
, rx_page
->cmb_map
,
2674 BUS_DMASYNC_PREWRITE
);
2679 ale_rxvlan(struct ale_softc
*sc
)
2684 ifp
= &sc
->arpcom
.ac_if
;
2685 reg
= CSR_READ_4(sc
, ALE_MAC_CFG
);
2686 reg
&= ~MAC_CFG_VLAN_TAG_STRIP
;
2687 if ((ifp
->if_capenable
& IFCAP_VLAN_HWTAGGING
) != 0)
2688 reg
|= MAC_CFG_VLAN_TAG_STRIP
;
2689 CSR_WRITE_4(sc
, ALE_MAC_CFG
, reg
);
2693 ale_rxfilter(struct ale_softc
*sc
)
2696 struct ifmultiaddr
*ifma
;
2701 ifp
= &sc
->arpcom
.ac_if
;
2703 rxcfg
= CSR_READ_4(sc
, ALE_MAC_CFG
);
2704 rxcfg
&= ~(MAC_CFG_ALLMULTI
| MAC_CFG_BCAST
| MAC_CFG_PROMISC
);
2705 if ((ifp
->if_flags
& IFF_BROADCAST
) != 0)
2706 rxcfg
|= MAC_CFG_BCAST
;
2707 if ((ifp
->if_flags
& (IFF_PROMISC
| IFF_ALLMULTI
)) != 0) {
2708 if ((ifp
->if_flags
& IFF_PROMISC
) != 0)
2709 rxcfg
|= MAC_CFG_PROMISC
;
2710 if ((ifp
->if_flags
& IFF_ALLMULTI
) != 0)
2711 rxcfg
|= MAC_CFG_ALLMULTI
;
2712 CSR_WRITE_4(sc
, ALE_MAR0
, 0xFFFFFFFF);
2713 CSR_WRITE_4(sc
, ALE_MAR1
, 0xFFFFFFFF);
2714 CSR_WRITE_4(sc
, ALE_MAC_CFG
, rxcfg
);
2718 /* Program new filter. */
2719 bzero(mchash
, sizeof(mchash
));
2721 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
2722 if (ifma
->ifma_addr
->sa_family
!= AF_LINK
)
2724 crc
= ether_crc32_le(LLADDR((struct sockaddr_dl
*)
2725 ifma
->ifma_addr
), ETHER_ADDR_LEN
);
2726 mchash
[crc
>> 31] |= 1 << ((crc
>> 26) & 0x1f);
2729 CSR_WRITE_4(sc
, ALE_MAR0
, mchash
[0]);
2730 CSR_WRITE_4(sc
, ALE_MAR1
, mchash
[1]);
2731 CSR_WRITE_4(sc
, ALE_MAC_CFG
, rxcfg
);
2735 sysctl_hw_ale_int_mod(SYSCTL_HANDLER_ARGS
)
2737 return (sysctl_int_range(oidp
, arg1
, arg2
, req
,
2738 ALE_IM_TIMER_MIN
, ALE_IM_TIMER_MAX
));
2742 ale_dmamap_buf_cb(void *xctx
, bus_dma_segment_t
*segs
, int nsegs
,
2743 bus_size_t mapsz __unused
, int error
)
2745 struct ale_dmamap_ctx
*ctx
= xctx
;
2751 if (nsegs
> ctx
->nsegs
) {
2757 for (i
= 0; i
< nsegs
; ++i
)
2758 ctx
->segs
[i
] = segs
[i
];