2 * Copyright (c) 2012,2013 Bjoern A. Zeeb
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-11-C-0249)
7 * ("MRC2"), as part of the DARPA MRC research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Altera Triple-Speed Ethernet MegaCore, Function User Guide
32 * UG-01008-3.0, Software Version: 12.0, June 2012.
33 * Available at the time of writing at:
34 * http://www.altera.com/literature/ug/ug_ethernet.pdf
36 * We are using an Marvell E1111 (Alaska) PHY on the DE4. See mii/e1000phy.c.
40 * - ifOutBroadcastPkts are only counted if both ether dst and src are all-1s;
41 * seems an IP core bug, they count ether broadcasts as multicast. Is this
43 * - figure out why the TX FIFO fill status and intr did not work as expected.
44 * - test 100Mbit/s and 10Mbit/s
45 * - blacklist the one special factory programmed ethernet address (for now
46 * hardcoded, later from loader?)
47 * - resolve all XXX, left as reminders to shake out details later
48 * - Jumbo frame support
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
54 #include "opt_device_polling.h"
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
60 #include <sys/endian.h>
63 #include <sys/module.h>
64 #include <sys/mutex.h>
66 #include <sys/socket.h>
67 #include <sys/sockio.h>
68 #include <sys/types.h>
70 #include <net/ethernet.h>
72 #include <net/if_var.h>
73 #include <net/if_dl.h>
74 #include <net/if_media.h>
75 #include <net/if_types.h>
76 #include <net/if_vlan_var.h>
80 #include <machine/bus.h>
81 #include <machine/resource.h>
84 #include <dev/mii/mii.h>
85 #include <dev/mii/miivar.h>
87 #include <dev/altera/atse/if_atsereg.h>
88 #include <dev/altera/atse/a_api.h>
90 MODULE_DEPEND(atse, ether, 1, 1, 1);
91 MODULE_DEPEND(atse, miibus, 1, 1, 1);
94 #define ATSE_WATCHDOG_TIME 5
97 static poll_handler_t atse_poll;
100 /* XXX once we'd do parallel attach, we need a global lock for this. */
101 #define ATSE_ETHERNET_OPTION_BITS_UNDEF 0
102 #define ATSE_ETHERNET_OPTION_BITS_READ 1
103 static int atse_ethernet_option_bits_flag = ATSE_ETHERNET_OPTION_BITS_UNDEF;
104 static uint8_t atse_ethernet_option_bits[ALTERA_ETHERNET_OPTION_BITS_LEN];
107 * Softc and critical resource locking.
109 #define ATSE_LOCK(_sc) mtx_lock(&(_sc)->atse_mtx)
110 #define ATSE_UNLOCK(_sc) mtx_unlock(&(_sc)->atse_mtx)
111 #define ATSE_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->atse_mtx, MA_OWNED)
114 #define DPRINTF(format, ...) printf(format, __VA_ARGS__)
116 #define DPRINTF(format, ...)
119 /* a_api.c functions; factor out? */
121 a_onchip_fifo_mem_core_write(struct resource *res, uint32_t off,
122 uint32_t val4, const char *desc, const char *f, const int l)
125 val4 = htole32(val4);
126 DPRINTF("[%s:%d] FIFOW %s 0x%08x = 0x%08x\n", f, l, desc, off, val4);
127 bus_write_4(res, off, val4);
129 static inline uint32_t
130 a_onchip_fifo_mem_core_read(struct resource *res, uint32_t off,
131 const char *desc, const char *f, const int l)
135 val4 = le32toh(bus_read_4(res, off));
136 DPRINTF("[%s:%d] FIFOR %s 0x%08x = 0x%08x\n", f, l, desc, off, val4);
140 /* The FIFO does an endian convertion, so we must not do it as well. */
141 /* XXX-BZ in fact we should do a htobe32 so le would be fine as well? */
142 #define ATSE_TX_DATA_WRITE(sc, val4) \
143 bus_write_4((sc)->atse_tx_mem_res, A_ONCHIP_FIFO_MEM_CORE_DATA, val4)
145 #define ATSE_TX_META_WRITE(sc, val4) \
146 a_onchip_fifo_mem_core_write((sc)->atse_tx_mem_res, \
147 A_ONCHIP_FIFO_MEM_CORE_METADATA, \
148 (val4), "TXM", __func__, __LINE__)
149 #define ATSE_TX_META_READ(sc) \
150 a_onchip_fifo_mem_core_read((sc)->atse_tx_mem_res, \
151 A_ONCHIP_FIFO_MEM_CORE_METADATA, \
152 "TXM", __func__, __LINE__)
154 #define ATSE_TX_READ_FILL_LEVEL(sc) \
155 a_onchip_fifo_mem_core_read((sc)->atse_txc_mem_res, \
156 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_FILL_LEVEL, \
157 "TX_FILL", __func__, __LINE__)
158 #define ATSE_RX_READ_FILL_LEVEL(sc) \
159 a_onchip_fifo_mem_core_read((sc)->atse_rxc_mem_res, \
160 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_FILL_LEVEL, \
161 "RX_FILL", __func__, __LINE__)
163 /* The FIFO does an endian convertion, so we must not do it as well. */
164 /* XXX-BZ in fact we shoudl do a htobe32 so le would be fine as well? */
165 #define ATSE_RX_DATA_READ(sc) \
166 bus_read_4((sc)->atse_rx_mem_res, A_ONCHIP_FIFO_MEM_CORE_DATA)
167 #define ATSE_RX_META_READ(sc) \
168 a_onchip_fifo_mem_core_read((sc)->atse_rx_mem_res, \
169 A_ONCHIP_FIFO_MEM_CORE_METADATA, \
170 "RXM", __func__, __LINE__)
172 #define ATSE_RX_EVENT_READ(sc) \
173 a_onchip_fifo_mem_core_read((sc)->atse_rxc_mem_res, \
174 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_EVENT, \
175 "RX_EVENT", __func__, __LINE__)
177 #define ATSE_TX_EVENT_READ(sc) \
178 a_onchip_fifo_mem_core_read((sc)->atse_txc_mem_res, \
179 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_EVENT, \
180 "TX_EVENT", __func__, __LINE__)
182 #define ATSE_RX_EVENT_CLEAR(sc) \
186 val4 = a_onchip_fifo_mem_core_read( \
187 (sc)->atse_rxc_mem_res, \
188 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_EVENT, \
189 "RX_EVENT", __func__, __LINE__); \
191 a_onchip_fifo_mem_core_write( \
192 (sc)->atse_rxc_mem_res, \
193 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_EVENT, \
194 val4, "RX_EVENT", __func__, __LINE__); \
196 #define ATSE_TX_EVENT_CLEAR(sc) \
200 val4 = a_onchip_fifo_mem_core_read( \
201 (sc)->atse_txc_mem_res, \
202 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_EVENT, \
203 "TX_EVENT", __func__, __LINE__); \
205 a_onchip_fifo_mem_core_write( \
206 (sc)->atse_txc_mem_res, \
207 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_EVENT, \
208 val4, "TX_EVENT", __func__, __LINE__); \
211 #define ATSE_RX_INTR_ENABLE(sc) \
212 a_onchip_fifo_mem_core_write((sc)->atse_rxc_mem_res, \
213 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_INT_ENABLE, \
214 A_ONCHIP_FIFO_MEM_CORE_INTR_ALL, \
215 "RX_INTR", __func__, __LINE__) /* XXX-BZ review later. */
216 #define ATSE_RX_INTR_DISABLE(sc) \
217 a_onchip_fifo_mem_core_write((sc)->atse_rxc_mem_res, \
218 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_INT_ENABLE, 0, \
219 "RX_INTR", __func__, __LINE__)
220 #define ATSE_TX_INTR_ENABLE(sc) \
221 a_onchip_fifo_mem_core_write((sc)->atse_txc_mem_res, \
222 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_INT_ENABLE, \
223 A_ONCHIP_FIFO_MEM_CORE_INTR_ALL, \
224 "TX_INTR", __func__, __LINE__) /* XXX-BZ review later. */
225 #define ATSE_TX_INTR_DISABLE(sc) \
226 a_onchip_fifo_mem_core_write((sc)->atse_txc_mem_res, \
227 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_INT_ENABLE, 0, \
228 "TX_INTR", __func__, __LINE__)
231 * Register space access macros.
234 csr_write_4(struct atse_softc *sc, uint32_t reg, uint32_t val4,
235 const char *f, const int l)
238 val4 = htole32(val4);
239 DPRINTF("[%s:%d] CSR W %s 0x%08x (0x%08x) = 0x%08x\n", f, l,
240 "atse_mem_res", reg, reg * 4, val4);
241 bus_write_4(sc->atse_mem_res, reg * 4, val4);
244 static inline uint32_t
245 csr_read_4(struct atse_softc *sc, uint32_t reg, const char *f, const int l)
249 val4 = le32toh(bus_read_4(sc->atse_mem_res, reg * 4));
250 DPRINTF("[%s:%d] CSR R %s 0x%08x (0x%08x) = 0x%08x\n", f, l,
251 "atse_mem_res", reg, reg * 4, val4);
256 * See page 5-2 that it's all dword offsets and the MS 16 bits need to be zero
257 * on write and ignored on read.
260 pxx_write_2(struct atse_softc *sc, bus_addr_t bmcr, uint32_t reg, uint16_t val,
261 const char *f, const int l, const char *s)
265 val4 = htole32(val & 0x0000ffff);
266 DPRINTF("[%s:%d] %s W %s 0x%08x (0x%08jx) = 0x%08x\n", f, l, s,
267 "atse_mem_res", reg, (bmcr + reg) * 4, val4);
268 bus_write_4(sc->atse_mem_res, (bmcr + reg) * 4, val4);
271 static inline uint16_t
272 pxx_read_2(struct atse_softc *sc, bus_addr_t bmcr, uint32_t reg, const char *f,
273 const int l, const char *s)
278 val4 = bus_read_4(sc->atse_mem_res, (bmcr + reg) * 4);
279 val = le32toh(val4) & 0x0000ffff;
280 DPRINTF("[%s:%d] %s R %s 0x%08x (0x%08jx) = 0x%04x\n", f, l, s,
281 "atse_mem_res", reg, (bmcr + reg) * 4, val);
285 #define CSR_WRITE_4(sc, reg, val) \
286 csr_write_4((sc), (reg), (val), __func__, __LINE__)
287 #define CSR_READ_4(sc, reg) \
288 csr_read_4((sc), (reg), __func__, __LINE__)
289 #define PCS_WRITE_2(sc, reg, val) \
290 pxx_write_2((sc), sc->atse_bmcr0, (reg), (val), __func__, __LINE__, \
292 #define PCS_READ_2(sc, reg) \
293 pxx_read_2((sc), sc->atse_bmcr0, (reg), __func__, __LINE__, "PCS")
294 #define PHY_WRITE_2(sc, reg, val) \
295 pxx_write_2((sc), sc->atse_bmcr1, (reg), (val), __func__, __LINE__, \
297 #define PHY_READ_2(sc, reg) \
298 pxx_read_2((sc), sc->atse_bmcr1, (reg), __func__, __LINE__, "PHY")
300 static void atse_tick(void *);
301 static int atse_detach(device_t);
303 devclass_t atse_devclass;
306 atse_tx_locked(struct atse_softc *sc, int *sent)
309 uint32_t val4, fill_level;
312 ATSE_LOCK_ASSERT(sc);
315 KASSERT(m != NULL, ("%s: m is null: sc=%p", __func__, sc));
316 KASSERT(m->m_flags & M_PKTHDR, ("%s: not a pkthdr: m=%p", __func__, m));
319 * Copy to buffer to minimize our pain as we can only store
320 * double words which, after the first mbuf gets out of alignment
323 if (sc->atse_tx_m_offset == 0) {
324 m_copydata(m, 0, m->m_pkthdr.len, sc->atse_tx_buf);
325 sc->atse_tx_buf_len = m->m_pkthdr.len;
328 fill_level = ATSE_TX_READ_FILL_LEVEL(sc);
329 #if 0 /* Returns 0xdeadc0de. */
330 val4 = ATSE_TX_META_READ(sc);
332 if (sc->atse_tx_m_offset == 0) {
333 /* Write start of packet. */
334 val4 = A_ONCHIP_FIFO_MEM_CORE_SOP;
335 val4 &= ~A_ONCHIP_FIFO_MEM_CORE_EOP;
336 ATSE_TX_META_WRITE(sc, val4);
339 /* TX FIFO is single clock mode, so we have the full FIFO. */
341 while ((sc->atse_tx_buf_len - sc->atse_tx_m_offset) > 4 &&
342 fill_level < AVALON_FIFO_TX_BASIC_OPTS_DEPTH) {
344 bcopy(&sc->atse_tx_buf[sc->atse_tx_m_offset], &val4,
346 ATSE_TX_DATA_WRITE(sc, val4);
347 sc->atse_tx_m_offset += sizeof(val4);
351 if (fill_level == AVALON_FIFO_TX_BASIC_OPTS_DEPTH)
352 fill_level = ATSE_TX_READ_FILL_LEVEL(sc);
357 /* Set EOP *before* writing the last symbol. */
358 if (sc->atse_tx_m_offset >= (sc->atse_tx_buf_len - 4) &&
359 fill_level < AVALON_FIFO_TX_BASIC_OPTS_DEPTH) {
363 /* Set EndOfPacket. */
364 val4 = A_ONCHIP_FIFO_MEM_CORE_EOP;
366 leftm = sc->atse_tx_buf_len - sc->atse_tx_m_offset;
367 val4 |= ((4 - leftm) << A_ONCHIP_FIFO_MEM_CORE_EMPTY_SHIFT);
369 ATSE_TX_META_WRITE(sc, val4);
371 /* Write last symbol. */
373 bcopy(sc->atse_tx_buf + sc->atse_tx_m_offset, &val4, leftm);
374 ATSE_TX_DATA_WRITE(sc, val4);
379 /* OK, the packet is gone. */
380 sc->atse_tx_m = NULL;
381 sc->atse_tx_m_offset = 0;
383 /* If anyone is interested give them a copy. */
384 BPF_MTAP(sc->atse_ifp, m);
394 atse_start_locked(struct ifnet *ifp)
396 struct atse_softc *sc;
400 ATSE_LOCK_ASSERT(sc);
402 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
403 IFF_DRV_RUNNING || (sc->atse_flags & ATSE_FLAGS_LINK) == 0)
408 * Disable the watchdog while sending, we are batching packets.
409 * Though we should never reach 5 seconds, and are holding the lock,
412 sc->atse_watchdog_timer = 0;
415 if (sc->atse_tx_m != NULL) {
416 error = atse_tx_locked(sc, &sent);
420 /* We have more space to send so continue ... */
421 for (; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
423 IFQ_DRV_DEQUEUE(&ifp->if_snd, sc->atse_tx_m);
424 sc->atse_tx_m_offset = 0;
425 if (sc->atse_tx_m == NULL)
427 error = atse_tx_locked(sc, &sent);
433 /* If the IP core walks into Nekromanteion try to bail out. */
435 sc->atse_watchdog_timer = ATSE_WATCHDOG_TIME;
439 atse_start(struct ifnet *ifp)
441 struct atse_softc *sc;
445 atse_start_locked(ifp);
450 atse_stop_locked(struct atse_softc *sc)
456 ATSE_LOCK_ASSERT(sc);
458 sc->atse_watchdog_timer = 0;
459 callout_stop(&sc->atse_tick);
462 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
463 ATSE_RX_INTR_DISABLE(sc);
464 ATSE_TX_INTR_DISABLE(sc);
465 ATSE_RX_EVENT_CLEAR(sc);
466 ATSE_TX_EVENT_CLEAR(sc);
468 /* Disable MAC transmit and receive datapath. */
469 mask = BASE_CFG_COMMAND_CONFIG_TX_ENA|BASE_CFG_COMMAND_CONFIG_RX_ENA;
470 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
472 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);
473 /* Wait for bits to be cleared; i=100 is excessive. */
474 for (i = 0; i < 100; i++) {
475 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
476 if ((val4 & mask) == 0)
480 if ((val4 & mask) != 0)
481 device_printf(sc->atse_dev, "Disabling MAC TX/RX timed out.\n");
484 sc->atse_flags &= ~ATSE_FLAGS_LINK;
486 /* XXX-BZ free the RX/TX rings. */
492 atse_mchash(struct atse_softc *sc __unused, const uint8_t *addr)
498 for (i = 0; i < ETHER_ADDR_LEN; i++) {
500 for (j = 1; j < 8; j++)
501 y ^= (addr[i] >> j) & 0x01;
508 atse_rxfilter_locked(struct atse_softc *sc)
511 struct ifmultiaddr *ifma;
515 /* XXX-BZ can we find out if we have the MHASH synthesized? */
516 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
517 /* For simplicity always hash full 48 bits of addresses. */
518 if ((val4 & BASE_CFG_COMMAND_CONFIG_MHASH_SEL) != 0)
519 val4 &= ~BASE_CFG_COMMAND_CONFIG_MHASH_SEL;
522 if (ifp->if_flags & IFF_PROMISC)
523 val4 |= BASE_CFG_COMMAND_CONFIG_PROMIS_EN;
525 val4 &= ~BASE_CFG_COMMAND_CONFIG_PROMIS_EN;
527 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);
529 if (ifp->if_flags & IFF_ALLMULTI) {
530 /* Accept all multicast addresses. */
531 for (i = 0; i <= MHASH_LEN; i++)
532 CSR_WRITE_4(sc, MHASH_START + i, 0x1);
535 * Can hold MHASH_LEN entries.
536 * XXX-BZ bitstring.h would be more general.
542 * Re-build and re-program hash table. First build the
543 * bit-field "yes" or "no" for each slot per address, then
544 * do all the programming afterwards.
547 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
548 if (ifma->ifma_addr->sa_family != AF_LINK)
551 h |= (1 << atse_mchash(sc,
552 LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
554 if_maddr_runlock(ifp);
555 for (i = 0; i <= MHASH_LEN; i++)
556 CSR_WRITE_4(sc, MHASH_START + i,
557 (h & (1 << i)) ? 0x01 : 0x00);
564 atse_ethernet_option_bits_read_fdt(device_t dev)
566 struct resource *res;
570 if (atse_ethernet_option_bits_flag & ATSE_ETHERNET_OPTION_BITS_READ)
573 fdev = device_find_child(device_get_parent(dev), "cfi", 0);
578 res = bus_alloc_resource_any(fdev, SYS_RES_MEMORY, &rid,
579 RF_ACTIVE | RF_SHAREABLE);
583 for (i = 0; i < ALTERA_ETHERNET_OPTION_BITS_LEN; i++)
584 atse_ethernet_option_bits[i] = bus_read_1(res,
585 ALTERA_ETHERNET_OPTION_BITS_OFF + i);
587 bus_release_resource(fdev, SYS_RES_MEMORY, rid, res);
588 atse_ethernet_option_bits_flag |= ATSE_ETHERNET_OPTION_BITS_READ;
594 atse_ethernet_option_bits_read(device_t dev)
598 error = atse_ethernet_option_bits_read_fdt(dev);
602 device_printf(dev, "Cannot read Ethernet addresses from flash.\n");
607 atse_get_eth_address(struct atse_softc *sc)
609 unsigned long hostid;
614 * Make sure to only ever do this once. Otherwise a reset would
615 * possibly change our ethernet address, which is not good at all.
617 if (sc->atse_eth_addr[0] != 0x00 || sc->atse_eth_addr[1] != 0x00 ||
618 sc->atse_eth_addr[2] != 0x00)
621 if ((atse_ethernet_option_bits_flag &
622 ATSE_ETHERNET_OPTION_BITS_READ) == 0)
625 val4 = atse_ethernet_option_bits[0] << 24;
626 val4 |= atse_ethernet_option_bits[1] << 16;
627 val4 |= atse_ethernet_option_bits[2] << 8;
628 val4 |= atse_ethernet_option_bits[3];
629 /* They chose "safe". */
630 if (val4 != le32toh(0x00005afe)) {
631 device_printf(sc->atse_dev, "Magic '5afe' is not safe: 0x%08x. "
632 "Falling back to random numbers for hardware address.\n",
637 sc->atse_eth_addr[0] = atse_ethernet_option_bits[4];
638 sc->atse_eth_addr[1] = atse_ethernet_option_bits[5];
639 sc->atse_eth_addr[2] = atse_ethernet_option_bits[6];
640 sc->atse_eth_addr[3] = atse_ethernet_option_bits[7];
641 sc->atse_eth_addr[4] = atse_ethernet_option_bits[8];
642 sc->atse_eth_addr[5] = atse_ethernet_option_bits[9];
644 /* Handle factory default ethernet addresss: 00:07:ed:ff:ed:15 */
645 if (sc->atse_eth_addr[0] == 0x00 && sc->atse_eth_addr[1] == 0x07 &&
646 sc->atse_eth_addr[2] == 0xed && sc->atse_eth_addr[3] == 0xff &&
647 sc->atse_eth_addr[4] == 0xed && sc->atse_eth_addr[5] == 0x15) {
649 device_printf(sc->atse_dev, "Factory programmed Ethernet "
650 "hardware address blacklisted. Falling back to random "
651 "address to avoid collisions.\n");
652 device_printf(sc->atse_dev, "Please re-program your flash.\n");
656 if (sc->atse_eth_addr[0] == 0x00 && sc->atse_eth_addr[1] == 0x00 &&
657 sc->atse_eth_addr[2] == 0x00 && sc->atse_eth_addr[3] == 0x00 &&
658 sc->atse_eth_addr[4] == 0x00 && sc->atse_eth_addr[5] == 0x00) {
659 device_printf(sc->atse_dev, "All zero's Ethernet hardware "
660 "address blacklisted. Falling back to random address.\n");
661 device_printf(sc->atse_dev, "Please re-program your flash.\n");
665 if (ETHER_IS_MULTICAST(sc->atse_eth_addr)) {
666 device_printf(sc->atse_dev, "Multicast Ethernet hardware "
667 "address blacklisted. Falling back to random address.\n");
668 device_printf(sc->atse_dev, "Please re-program your flash.\n");
673 * If we find an Altera prefixed address with a 0x0 ending
674 * adjust by device unit. If not and this is not the first
675 * Ethernet, go to random.
677 unit = device_get_unit(sc->atse_dev);
682 device_printf(sc->atse_dev, "We do not support Ethernet "
683 "addresses for more than 16 MACs. Falling back to "
684 "random hadware address.\n");
687 if ((sc->atse_eth_addr[0] & ~0x2) != 0 ||
688 sc->atse_eth_addr[1] != 0x07 || sc->atse_eth_addr[2] != 0xed ||
689 (sc->atse_eth_addr[5] & 0x0f) != 0x0) {
690 device_printf(sc->atse_dev, "Ethernet address not meeting our "
691 "multi-MAC standards. Falling back to random hadware "
695 sc->atse_eth_addr[5] |= (unit & 0x0f);
701 * Fall back to random code we also use on bridge(4).
703 getcredhostid(curthread->td_ucred, &hostid);
705 arc4rand(sc->atse_eth_addr, ETHER_ADDR_LEN, 1);
706 sc->atse_eth_addr[0] &= ~1;/* clear multicast bit */
707 sc->atse_eth_addr[0] |= 2; /* set the LAA bit */
709 sc->atse_eth_addr[0] = 0x2;
710 sc->atse_eth_addr[1] = (hostid >> 24) & 0xff;
711 sc->atse_eth_addr[2] = (hostid >> 16) & 0xff;
712 sc->atse_eth_addr[3] = (hostid >> 8 ) & 0xff;
713 sc->atse_eth_addr[4] = hostid & 0xff;
714 sc->atse_eth_addr[5] = sc->atse_unit & 0xff;
721 atse_set_eth_address(struct atse_softc *sc, int n)
725 v0 = (sc->atse_eth_addr[3] << 24) | (sc->atse_eth_addr[2] << 16) |
726 (sc->atse_eth_addr[1] << 8) | sc->atse_eth_addr[0];
727 v1 = (sc->atse_eth_addr[5] << 8) | sc->atse_eth_addr[4];
729 if (n & ATSE_ETH_ADDR_DEF) {
730 CSR_WRITE_4(sc, BASE_CFG_MAC_0, v0);
731 CSR_WRITE_4(sc, BASE_CFG_MAC_1, v1);
733 if (n & ATSE_ETH_ADDR_SUPP1) {
734 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_0_0, v0);
735 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_0_1, v1);
737 if (n & ATSE_ETH_ADDR_SUPP2) {
738 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_1_0, v0);
739 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_1_1, v1);
741 if (n & ATSE_ETH_ADDR_SUPP3) {
742 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_2_0, v0);
743 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_2_1, v1);
745 if (n & ATSE_ETH_ADDR_SUPP4) {
746 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_3_0, v0);
747 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_3_1, v1);
754 atse_reset(struct atse_softc *sc)
760 /* 1. External PHY Initialization using MDIO. */
762 * We select the right MDIO space in atse_attach() and let MII do
766 /* 2. PCS Configuration Register Initialization. */
767 /* a. Set auto negotiation link timer to 1.6ms for SGMII. */
768 PCS_WRITE_2(sc, PCS_EXT_LINK_TIMER_0, 0x0D40);
769 PCS_WRITE_2(sc, PCS_EXT_LINK_TIMER_1, 0x0003);
771 /* b. Configure SGMII. */
772 val = PCS_EXT_IF_MODE_SGMII_ENA|PCS_EXT_IF_MODE_USE_SGMII_AN;
773 PCS_WRITE_2(sc, PCS_EXT_IF_MODE, val);
775 /* c. Enable auto negotiation. */
776 /* Ignore Bits 6,8,13; should be set,set,unset. */
777 val = PCS_READ_2(sc, PCS_CONTROL);
778 val &= ~(PCS_CONTROL_ISOLATE|PCS_CONTROL_POWERDOWN);
779 val &= ~PCS_CONTROL_LOOPBACK; /* Make this a -link1 option? */
780 val |= PCS_CONTROL_AUTO_NEGOTIATION_ENABLE;
781 PCS_WRITE_2(sc, PCS_CONTROL, val);
784 val = PCS_READ_2(sc, PCS_CONTROL);
785 val |= PCS_CONTROL_RESET;
786 PCS_WRITE_2(sc, PCS_CONTROL, val);
787 /* Wait for reset bit to clear; i=100 is excessive. */
788 for (i = 0; i < 100; i++) {
789 val = PCS_READ_2(sc, PCS_CONTROL);
790 if ((val & PCS_CONTROL_RESET) == 0)
794 if ((val & PCS_CONTROL_RESET) != 0) {
795 device_printf(sc->atse_dev, "PCS reset timed out.\n");
799 /* 3. MAC Configuration Register Initialization. */
800 /* a. Disable MAC transmit and receive datapath. */
801 mask = BASE_CFG_COMMAND_CONFIG_TX_ENA|BASE_CFG_COMMAND_CONFIG_RX_ENA;
802 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
804 /* Samples in the manual do have the SW_RESET bit set here, why? */
805 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);
806 /* Wait for bits to be cleared; i=100 is excessive. */
807 for (i = 0; i < 100; i++) {
808 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
809 if ((val4 & mask) == 0)
813 if ((val4 & mask) != 0) {
814 device_printf(sc->atse_dev, "Disabling MAC TX/RX timed out.\n");
817 /* b. MAC FIFO configuration. */
818 CSR_WRITE_4(sc, BASE_CFG_TX_SECTION_EMPTY, FIFO_DEPTH_TX - 16);
819 CSR_WRITE_4(sc, BASE_CFG_TX_ALMOST_FULL, 3);
820 CSR_WRITE_4(sc, BASE_CFG_TX_ALMOST_EMPTY, 8);
821 CSR_WRITE_4(sc, BASE_CFG_RX_SECTION_EMPTY, FIFO_DEPTH_RX - 16);
822 CSR_WRITE_4(sc, BASE_CFG_RX_ALMOST_FULL, 8);
823 CSR_WRITE_4(sc, BASE_CFG_RX_ALMOST_EMPTY, 8);
825 CSR_WRITE_4(sc, BASE_CFG_TX_SECTION_FULL, 16);
826 CSR_WRITE_4(sc, BASE_CFG_RX_SECTION_FULL, 16);
828 /* For store-and-forward mode, set this threshold to 0. */
829 CSR_WRITE_4(sc, BASE_CFG_TX_SECTION_FULL, 0);
830 CSR_WRITE_4(sc, BASE_CFG_RX_SECTION_FULL, 0);
832 /* c. MAC address configuration. */
833 /* Also intialize supplementary addresses to our primary one. */
834 /* XXX-BZ FreeBSD really needs to grow and API for using these. */
835 atse_get_eth_address(sc);
836 atse_set_eth_address(sc, ATSE_ETH_ADDR_ALL);
838 /* d. MAC function configuration. */
839 CSR_WRITE_4(sc, BASE_CFG_FRM_LENGTH, 1518); /* Default. */
840 CSR_WRITE_4(sc, BASE_CFG_TX_IPG_LENGTH, 12);
841 CSR_WRITE_4(sc, BASE_CFG_PAUSE_QUANT, 0xFFFF);
843 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
845 * If 1000BASE-X/SGMII PCS is initialized, set the ETH_SPEED (bit 3)
846 * and ENA_10 (bit 25) in command_config register to 0. If half duplex
847 * is reported in the PHY/PCS status register, set the HD_ENA (bit 10)
848 * to 1 in command_config register.
849 * BZ: We shoot for 1000 instead.
852 val4 |= BASE_CFG_COMMAND_CONFIG_ETH_SPEED;
854 val4 &= ~BASE_CFG_COMMAND_CONFIG_ETH_SPEED;
856 val4 &= ~BASE_CFG_COMMAND_CONFIG_ENA_10;
859 * We do not want to set this, otherwise, we could not even send
860 * random raw ethernet frames for various other research. By default
861 * FreeBSD will use the right ether source address.
863 val4 |= BASE_CFG_COMMAND_CONFIG_TX_ADDR_INS;
865 val4 |= BASE_CFG_COMMAND_CONFIG_PAD_EN;
866 val4 &= ~BASE_CFG_COMMAND_CONFIG_CRC_FWD;
868 val4 |= BASE_CFG_COMMAND_CONFIG_CNTL_FRM_ENA;
871 val4 |= BASE_CFG_COMMAND_CONFIG_RX_ERR_DISC;
873 val &= ~BASE_CFG_COMMAND_CONFIG_LOOP_ENA; /* link0? */
874 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);
877 * Make sure we do not enable 32bit alignment; FreeBSD cannot
878 * cope with the additional padding (though we should!?).
879 * Also make sure we get the CRC appended.
881 val4 = CSR_READ_4(sc, TX_CMD_STAT);
882 val4 &= ~(TX_CMD_STAT_OMIT_CRC|TX_CMD_STAT_TX_SHIFT16);
883 CSR_WRITE_4(sc, TX_CMD_STAT, val4);
884 val4 = CSR_READ_4(sc, RX_CMD_STAT);
885 val4 &= ~RX_CMD_STAT_RX_SHIFT16;
886 CSR_WRITE_4(sc, RX_CMD_STAT, val4);
889 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
890 val4 |= BASE_CFG_COMMAND_CONFIG_SW_RESET;
891 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);
892 /* Wait for bits to be cleared; i=100 is excessive. */
893 for (i = 0; i < 100; i++) {
894 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
895 if ((val4 & BASE_CFG_COMMAND_CONFIG_SW_RESET) == 0)
899 if ((val4 & BASE_CFG_COMMAND_CONFIG_SW_RESET) != 0) {
900 device_printf(sc->atse_dev, "MAC reset timed out.\n");
904 /* f. Enable MAC transmit and receive datapath. */
905 mask = BASE_CFG_COMMAND_CONFIG_TX_ENA|BASE_CFG_COMMAND_CONFIG_RX_ENA;
906 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
908 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);
909 /* Wait for bits to be cleared; i=100 is excessive. */
910 for (i = 0; i < 100; i++) {
911 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
912 if ((val4 & mask) == mask)
916 if ((val4 & mask) != mask) {
917 device_printf(sc->atse_dev, "Enabling MAC TX/RX timed out.\n");
925 atse_init_locked(struct atse_softc *sc)
928 struct mii_data *mii;
931 ATSE_LOCK_ASSERT(sc);
934 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
938 * Must update the ether address if changed. Given we do not handle
939 * in atse_ioctl() but it's in the general framework, just always
940 * do it here before atse_reset().
942 eaddr = IF_LLADDR(sc->atse_ifp);
943 bcopy(eaddr, &sc->atse_eth_addr, ETHER_ADDR_LEN);
945 /* Make things frind to halt, cleanup, ... */
946 atse_stop_locked(sc);
950 /* ... and fire up the engine again. */
951 atse_rxfilter_locked(sc);
953 /* Memory rings? DMA engine? */
955 sc->atse_rx_buf_len = 0;
956 sc->atse_flags &= ATSE_FLAGS_LINK; /* Preserve. */
958 #ifdef DEVICE_POLLING
959 /* Only enable interrupts if we are not polling. */
960 if (ifp->if_capenable & IFCAP_POLLING) {
961 ATSE_RX_INTR_DISABLE(sc);
962 ATSE_TX_INTR_DISABLE(sc);
963 ATSE_RX_EVENT_CLEAR(sc);
964 ATSE_TX_EVENT_CLEAR(sc);
968 ATSE_RX_INTR_ENABLE(sc);
969 ATSE_TX_INTR_ENABLE(sc);
972 mii = device_get_softc(sc->atse_miibus);
974 sc->atse_flags &= ~ATSE_FLAGS_LINK;
977 ifp->if_drv_flags |= IFF_DRV_RUNNING;
978 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
980 callout_reset(&sc->atse_tick, hz, atse_tick, sc);
986 struct atse_softc *sc;
988 sc = (struct atse_softc *)xsc;
990 atse_init_locked(sc);
995 atse_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
997 struct atse_softc *sc;
1004 ifr = (struct ifreq *)data;
1009 if (ifp->if_flags & IFF_UP) {
1010 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
1011 ((ifp->if_flags ^ sc->atse_if_flags) &
1012 (IFF_PROMISC | IFF_ALLMULTI)) != 0)
1013 atse_rxfilter_locked(sc);
1015 atse_init_locked(sc);
1016 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1017 atse_stop_locked(sc);
1018 sc->atse_if_flags = ifp->if_flags;
1023 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1024 #ifdef DEVICE_POLLING
1025 if ((mask & IFCAP_POLLING) != 0 &&
1026 (IFCAP_POLLING & ifp->if_capabilities) != 0) {
1027 ifp->if_capenable ^= IFCAP_POLLING;
1028 if ((IFCAP_POLLING & ifp->if_capenable) != 0) {
1030 error = ether_poll_register(atse_poll, ifp);
1035 /* Disable interrupts. */
1036 ATSE_RX_INTR_DISABLE(sc);
1037 ATSE_TX_INTR_DISABLE(sc);
1038 ATSE_RX_EVENT_CLEAR(sc);
1039 ATSE_TX_EVENT_CLEAR(sc);
1042 * Do not allow disabling of polling if we do
1043 * not have interrupts.
1045 } else if (sc->atse_rx_irq_res != NULL ||
1046 sc->atse_tx_irq_res != NULL) {
1047 error = ether_poll_deregister(ifp);
1048 /* Enable interrupts. */
1049 ATSE_RX_INTR_ENABLE(sc);
1050 ATSE_TX_INTR_ENABLE(sc);
1052 ifp->if_capenable ^= IFCAP_POLLING;
1056 #endif /* DEVICE_POLLING */
1062 atse_rxfilter_locked(sc);
1068 struct mii_data *mii;
1071 mii = device_get_softc(sc->atse_miibus);
1072 ifr = (struct ifreq *)data;
1073 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1077 error = ether_ioctl(ifp, command, data);
1085 atse_watchdog(struct atse_softc *sc)
1088 ATSE_LOCK_ASSERT(sc);
1090 if (sc->atse_watchdog_timer == 0 || --sc->atse_watchdog_timer > 0)
1093 device_printf(sc->atse_dev, "watchdog timeout\n");
1094 sc->atse_ifp->if_oerrors++;
1096 sc->atse_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1097 atse_init_locked(sc);
1099 if (!IFQ_DRV_IS_EMPTY(&sc->atse_ifp->if_snd))
1100 atse_start_locked(sc->atse_ifp);
1104 atse_tick(void *xsc)
1106 struct atse_softc *sc;
1107 struct mii_data *mii;
1110 sc = (struct atse_softc *)xsc;
1111 ATSE_LOCK_ASSERT(sc);
1114 mii = device_get_softc(sc->atse_miibus);
1117 if ((sc->atse_flags & ATSE_FLAGS_LINK) == 0)
1118 atse_miibus_statchg(sc->atse_dev);
1119 callout_reset(&sc->atse_tick, hz, atse_tick, sc);
1123 * Set media options.
1126 atse_ifmedia_upd(struct ifnet *ifp)
1128 struct atse_softc *sc;
1129 struct mii_data *mii;
1130 struct mii_softc *miisc;
1136 mii = device_get_softc(sc->atse_miibus);
1137 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1139 error = mii_mediachg(mii);
1146 atse_update_rx_err(struct atse_softc *sc, uint32_t mask)
1150 /* RX error are 6 bits, we only know 4 of them. */
1151 for (i = 0; i < ATSE_RX_ERR_MAX; i++)
1152 if ((mask & (1 << i)) != 0)
1153 sc->atse_rx_err[i]++;
1157 atse_rx_locked(struct atse_softc *sc)
1161 uint32_t fill, i, j;
1162 uint32_t data, meta;
1165 ATSE_LOCK_ASSERT(sc);
1172 if (sc->atse_rx_cycles <= 0)
1174 sc->atse_rx_cycles--;
1176 if (sc->atse_rx_m == NULL) {
1177 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1180 m->m_len = m->m_pkthdr.len = MCLBYTES;
1181 /* Make sure upper layers will be aligned. */
1186 fill = ATSE_RX_READ_FILL_LEVEL(sc);
1187 for (i = 0; i < fill; i++) {
1189 * XXX-BZ for whatever reason the FIFO requires the
1190 * the data read before we can access the meta data.
1192 data = ATSE_RX_DATA_READ(sc);
1193 meta = ATSE_RX_META_READ(sc);
1194 if (meta & A_ONCHIP_FIFO_MEM_CORE_ERROR_MASK) {
1195 /* XXX-BZ evaluate error. */
1196 atse_update_rx_err(sc, ((meta &
1197 A_ONCHIP_FIFO_MEM_CORE_ERROR_MASK) >>
1198 A_ONCHIP_FIFO_MEM_CORE_ERROR_SHIFT) & 0xff);
1200 sc->atse_rx_buf_len = 0;
1202 * Should still read till EOP or next SOP.
1204 * XXX-BZ might also depend on
1205 * BASE_CFG_COMMAND_CONFIG_RX_ERR_DISC
1207 sc->atse_flags |= ATSE_FLAGS_ERROR;
1210 if ((meta & A_ONCHIP_FIFO_MEM_CORE_CHANNEL_MASK) != 0)
1211 device_printf(sc->atse_dev, "%s: unexpected "
1212 "channel %u\n", __func__, (meta &
1213 A_ONCHIP_FIFO_MEM_CORE_CHANNEL_MASK) >>
1214 A_ONCHIP_FIFO_MEM_CORE_CHANNEL_SHIFT);
1216 if (meta & A_ONCHIP_FIFO_MEM_CORE_SOP) {
1218 * There is no need to clear SOP between 1st
1219 * and subsequent packet data junks.
1221 if (sc->atse_rx_buf_len != 0 &&
1222 (sc->atse_flags & ATSE_FLAGS_SOP_SEEN) == 0)
1224 device_printf(sc->atse_dev, "%s: SOP "
1225 "without empty buffer: %u\n",
1226 __func__, sc->atse_rx_buf_len);
1227 /* XXX-BZ any better counter? */
1231 if ((sc->atse_flags & ATSE_FLAGS_SOP_SEEN) == 0)
1233 sc->atse_flags |= ATSE_FLAGS_SOP_SEEN;
1234 sc->atse_rx_buf_len = 0;
1237 #if 0 /* We had to read the data before we could access meta data. See above. */
1238 data = ATSE_RX_DATA_READ(sc);
1240 /* Make sure to not overflow the mbuf data size. */
1241 if (sc->atse_rx_buf_len >= sc->atse_rx_m->m_len - 4) {
1243 * XXX-BZ Error. We need more mbufs and are
1244 * not setup for this yet.
1247 sc->atse_flags |= ATSE_FLAGS_ERROR;
1249 if ((sc->atse_flags & ATSE_FLAGS_ERROR) == 0)
1251 * MUST keep this bcopy as m_data after m_adj
1252 * for IP header aligment is on half-word
1253 * and not word alignment.
1255 bcopy(&data, (uint8_t *)(sc->atse_rx_m->m_data +
1256 sc->atse_rx_buf_len), sizeof(data));
1257 if (meta & A_ONCHIP_FIFO_MEM_CORE_EOP) {
1261 A_ONCHIP_FIFO_MEM_CORE_EMPTY_MASK) >>
1262 A_ONCHIP_FIFO_MEM_CORE_EMPTY_SHIFT;
1263 sc->atse_rx_buf_len += (4 - empty);
1269 m->m_pkthdr.len = m->m_len =
1270 sc->atse_rx_buf_len;
1271 sc->atse_rx_m = NULL;
1273 sc->atse_rx_buf_len = 0;
1274 sc->atse_flags &= ~ATSE_FLAGS_SOP_SEEN;
1275 if (sc->atse_flags & ATSE_FLAGS_ERROR) {
1276 sc->atse_flags &= ~ATSE_FLAGS_ERROR;
1278 /* Need to start with a new packet. */
1282 m->m_pkthdr.rcvif = ifp;
1285 (*ifp->if_input)(ifp, m);
1287 goto outer; /* Need a new mbuf. */
1289 sc->atse_rx_buf_len += sizeof(data);
1293 /* XXX-BZ could optimize in case of another packet waiting. */
1294 } while ((meta & A_ONCHIP_FIFO_MEM_CORE_EOP) == 0 || fill > 0);
1301 * Report current media status.
1304 atse_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1306 struct atse_softc *sc;
1307 struct mii_data *mii;
1312 mii = device_get_softc(sc->atse_miibus);
1314 ifmr->ifm_active = mii->mii_media_active;
1315 ifmr->ifm_status = mii->mii_media_status;
1320 atse_intr(void *arg)
1322 struct atse_softc *sc;
1326 sc = (struct atse_softc *)arg;
1330 #ifdef DEVICE_POLLING
1331 if (ifp->if_capenable & IFCAP_POLLING) {
1337 ATSE_RX_INTR_DISABLE(sc);
1338 ATSE_TX_INTR_DISABLE(sc);
1340 rx = ATSE_RX_EVENT_READ(sc);
1341 tx = ATSE_TX_EVENT_READ(sc);
1343 if (rx & (A_ONCHIP_FIFO_MEM_CORE_EVENT_OVERFLOW|
1344 A_ONCHIP_FIFO_MEM_CORE_EVENT_UNDERFLOW)) {
1345 /* XXX-BZ ERROR HANDLING. */
1346 atse_update_rx_err(sc, ((rx &
1347 A_ONCHIP_FIFO_MEM_CORE_ERROR_MASK) >>
1348 A_ONCHIP_FIFO_MEM_CORE_ERROR_SHIFT) & 0xff);
1351 if ((rx & A_ONCHIP_FIFO_MEM_CORE_EVENT_EMPTY) != 0) {
1352 sc->atse_rx_cycles = RX_CYCLES_IN_INTR;
1357 /* XXX-BZ build histogram. */
1358 if (tx & (A_ONCHIP_FIFO_MEM_CORE_EVENT_OVERFLOW|
1359 A_ONCHIP_FIFO_MEM_CORE_EVENT_UNDERFLOW)) {
1360 /* XXX-BZ ERROR HANDLING. */
1363 if (tx & A_ONCHIP_FIFO_MEM_CORE_EVENT_EMPTY)
1364 sc->atse_watchdog_timer = 0;
1366 if (tx & (A_ONCHIP_FIFO_MEM_CORE_EVENT_EMPTY|
1367 A_ONCHIP_FIFO_MEM_CORE_EVENT_ALMOSTEMPTY))
1368 atse_start_locked(ifp);
1372 /* Clear events before re-enabling intrs. */
1373 ATSE_TX_EVENT_CLEAR(sc);
1374 ATSE_RX_EVENT_CLEAR(sc);
1376 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1377 /* Re-enable interrupts. */
1378 ATSE_RX_INTR_ENABLE(sc);
1379 ATSE_TX_INTR_ENABLE(sc);
1381 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1382 atse_start_locked(ifp);
1388 #ifdef DEVICE_POLLING
1390 atse_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1392 struct atse_softc *sc;
1397 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1402 sc->atse_rx_cycles = count;
1403 rx_npkts = atse_rx_locked(sc);
1404 atse_start_locked(ifp);
1406 if (sc->atse_rx_cycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
1409 rx = ATSE_RX_EVENT_READ(sc);
1410 tx = ATSE_TX_EVENT_READ(sc);
1412 if (rx & (A_ONCHIP_FIFO_MEM_CORE_EVENT_OVERFLOW|
1413 A_ONCHIP_FIFO_MEM_CORE_EVENT_UNDERFLOW)) {
1414 /* XXX-BZ ERROR HANDLING. */
1415 atse_update_rx_err(sc, ((rx &
1416 A_ONCHIP_FIFO_MEM_CORE_ERROR_MASK) >>
1417 A_ONCHIP_FIFO_MEM_CORE_ERROR_SHIFT) & 0xff);
1420 if (tx & (A_ONCHIP_FIFO_MEM_CORE_EVENT_OVERFLOW|
1421 A_ONCHIP_FIFO_MEM_CORE_EVENT_UNDERFLOW)) {
1422 /* XXX-BZ ERROR HANDLING. */
1425 if (tx & A_ONCHIP_FIFO_MEM_CORE_EVENT_EMPTY)
1426 sc->atse_watchdog_timer = 0;
1429 if (/* Severe error; if only we could find out. */) {
1430 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1431 atse_init_locked(sc);
1439 #endif /* DEVICE_POLLING */
1441 static struct atse_mac_stats_regs {
1443 const char *descr; /* Mostly copied from Altera datasheet. */
1444 } atse_mac_stats_regs[] = {
1446 { "aFramesTransmittedOK",
1447 "The number of frames that are successfully transmitted including "
1448 "the pause frames." },
1449 { "aFramesReceivedOK",
1450 "The number of frames that are successfully received including the "
1452 { "aFrameCheckSequenceErrors",
1453 "The number of receive frames with CRC error." },
1454 { "aAlignmentErrors",
1455 "The number of receive frames with alignment error." },
1456 { "aOctetsTransmittedOK",
1457 "The lower 32 bits of the number of data and padding octets that "
1458 "are successfully transmitted." },
1459 { "aOctetsReceivedOK",
1460 "The lower 32 bits of the number of data and padding octets that "
1461 " are successfully received." },
1462 { "aTxPAUSEMACCtrlFrames",
1463 "The number of pause frames transmitted." },
1464 { "aRxPAUSEMACCtrlFrames",
1465 "The number received pause frames received." },
1467 "The number of errored frames received." },
1469 "The number of transmit frames with either a FIFO overflow error, "
1470 "a FIFO underflow error, or a error defined by the user "
1473 "The number of valid unicast frames received." },
1474 { "ifInMulticastPkts",
1475 "The number of valid multicast frames received. The count does "
1476 "not include pause frames." },
1477 { "ifInBroadcastPkts",
1478 "The number of valid broadcast frames received." },
1480 "This statistics counter is not in use. The MAC function does not "
1481 "discard frames that are written to the FIFO buffer by the user "
1484 "The number of valid unicast frames transmitted." },
1485 { "ifOutMulticastPkts",
1486 "The number of valid multicast frames transmitted, excluding pause "
1488 { "ifOutBroadcastPkts",
1489 "The number of valid broadcast frames transmitted." },
1490 { "etherStatsDropEvents",
1491 "The number of frames that are dropped due to MAC internal errors "
1492 "when FIFO buffer overflow persists." },
1493 { "etherStatsOctets",
1494 "The lower 32 bits of the total number of octets received. This "
1495 "count includes both good and errored frames." },
1497 "The total number of good and errored frames received." },
1498 { "etherStatsUndersizePkts",
1499 "The number of frames received with length less than 64 bytes. "
1500 "This count does not include errored frames." },
1501 { "etherStatsOversizePkts",
1502 "The number of frames received that are longer than the value "
1503 "configured in the frm_length register. This count does not "
1504 "include errored frames." },
1505 { "etherStatsPkts64Octets",
1506 "The number of 64-byte frames received. This count includes good "
1507 "and errored frames." },
1508 { "etherStatsPkts65to127Octets",
1509 "The number of received good and errored frames between the length "
1510 "of 65 and 127 bytes." },
1511 { "etherStatsPkts128to255Octets",
1512 "The number of received good and errored frames between the length "
1513 "of 128 and 255 bytes." },
1514 { "etherStatsPkts256to511Octets",
1515 "The number of received good and errored frames between the length "
1516 "of 256 and 511 bytes." },
1517 { "etherStatsPkts512to1023Octets",
1518 "The number of received good and errored frames between the length "
1519 "of 512 and 1023 bytes." },
1520 { "etherStatsPkts1024to1518Octets",
1521 "The number of received good and errored frames between the length "
1522 "of 1024 and 1518 bytes." },
1523 { "etherStatsPkts1519toXOctets",
1524 "The number of received good and errored frames between the length "
1525 "of 1519 and the maximum frame length configured in the frm_length "
1527 { "etherStatsJabbers",
1528 "Too long frames with CRC error." },
1529 { "etherStatsFragments",
1530 "Too short frames with CRC error." },
1531 /* 0x39 unused, 0x3a/b non-stats. */
1533 /* Extended Statistics Counters */
1534 { "msb_aOctetsTransmittedOK",
1535 "Upper 32 bits of the number of data and padding octets that are "
1536 "successfully transmitted." },
1537 { "msb_aOctetsReceivedOK",
1538 "Upper 32 bits of the number of data and padding octets that are "
1539 "successfully received." },
1540 { "msb_etherStatsOctets",
1541 "Upper 32 bits of the total number of octets received. This count "
1542 "includes both good and errored frames." }
1546 sysctl_atse_mac_stats_proc(SYSCTL_HANDLER_ARGS)
1548 struct atse_softc *sc;
1549 int error, offset, s;
1554 s = CSR_READ_4(sc, offset);
1555 error = sysctl_handle_int(oidp, &s, 0, req);
1556 if (error || !req->newptr)
1562 static struct atse_rx_err_stats_regs {
1565 } atse_rx_err_stats_regs[] = {
1567 #define ATSE_RX_ERR_FIFO_THRES_EOP 0 /* FIFO threshold reached, on EOP. */
1568 #define ATSE_RX_ERR_ELEN 1 /* Frame/payload length not valid. */
1569 #define ATSE_RX_ERR_CRC32 2 /* CRC-32 error. */
1570 #define ATSE_RX_ERR_FIFO_THRES_TRUNC 3 /* FIFO thresh., truncated frame. */
1571 #define ATSE_RX_ERR_4 4 /* ? */
1572 #define ATSE_RX_ERR_5 5 /* / */
1574 { "rx_err_fifo_thres_eop",
1575 "FIFO threshold reached, reported on EOP." },
1576 { "rx_err_fifo_elen",
1577 "Frame or payload length not valid." },
1578 { "rx_err_fifo_crc32",
1580 { "rx_err_fifo_thres_trunc",
1581 "FIFO threshold reached, truncated frame" },
1589 sysctl_atse_rx_err_stats_proc(SYSCTL_HANDLER_ARGS)
1591 struct atse_softc *sc;
1592 int error, offset, s;
1597 s = sc->atse_rx_err[offset];
1598 error = sysctl_handle_int(oidp, &s, 0, req);
1599 if (error || !req->newptr)
1606 atse_sysctl_stats_attach(device_t dev)
1608 struct sysctl_ctx_list *sctx;
1609 struct sysctl_oid *soid;
1610 struct atse_softc *sc;
1613 sc = device_get_softc(dev);
1614 sctx = device_get_sysctl_ctx(dev);
1615 soid = device_get_sysctl_tree(dev);
1617 /* MAC statistics. */
1618 for (i = 0; i < sizeof(atse_mac_stats_regs) /
1619 sizeof(*atse_mac_stats_regs); i++) {
1620 if (atse_mac_stats_regs[i].name == NULL ||
1621 atse_mac_stats_regs[i].descr == NULL)
1624 SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
1625 atse_mac_stats_regs[i].name, CTLTYPE_UINT|CTLFLAG_RD,
1626 sc, i, sysctl_atse_mac_stats_proc, "IU",
1627 atse_mac_stats_regs[i].descr);
1631 for (i = 0; i < ATSE_RX_ERR_MAX; i++) {
1632 if (atse_rx_err_stats_regs[i].name == NULL ||
1633 atse_rx_err_stats_regs[i].descr == NULL)
1636 SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
1637 atse_rx_err_stats_regs[i].name, CTLTYPE_UINT|CTLFLAG_RD,
1638 sc, i, sysctl_atse_rx_err_stats_proc, "IU",
1639 atse_rx_err_stats_regs[i].descr);
1644 * Generic device handling routines.
1647 atse_attach(device_t dev)
1649 struct atse_softc *sc;
1653 sc = device_get_softc(dev);
1655 atse_ethernet_option_bits_read(dev);
1657 mtx_init(&sc->atse_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1660 callout_init_mtx(&sc->atse_tick, &sc->atse_mtx, 0);
1662 sc->atse_tx_buf = malloc(ETHER_MAX_LEN_JUMBO, M_DEVBUF, M_WAITOK);
1665 * We are only doing single-PHY with this driver currently. The
1666 * defaults would be right so that BASE_CFG_MDIO_ADDR0 points to the
1667 * 1st PHY address (0) apart from the fact that BMCR0 is always
1668 * the PCS mapping, so we always use BMCR1. See Table 5-1 0xA0-0xBF.
1670 #if 0 /* Always PCS. */
1671 sc->atse_bmcr0 = MDIO_0_START;
1672 CSR_WRITE_4(sc, BASE_CFG_MDIO_ADDR0, 0x00);
1674 /* Always use matching PHY for atse[0..]. */
1675 sc->atse_phy_addr = device_get_unit(dev);
1676 sc->atse_bmcr1 = MDIO_1_START;
1677 CSR_WRITE_4(sc, BASE_CFG_MDIO_ADDR1, sc->atse_phy_addr);
1679 /* Reset the adapter. */
1682 /* Setup interface. */
1683 ifp = sc->atse_ifp = if_alloc(IFT_ETHER);
1685 device_printf(dev, "if_alloc() failed\n");
1690 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1691 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1692 ifp->if_ioctl = atse_ioctl;
1693 ifp->if_start = atse_start;
1694 ifp->if_init = atse_init;
1695 IFQ_SET_MAXLEN(&ifp->if_snd, ATSE_TX_LIST_CNT - 1);
1696 ifp->if_snd.ifq_drv_maxlen = ATSE_TX_LIST_CNT - 1;
1697 IFQ_SET_READY(&ifp->if_snd);
1700 error = mii_attach(dev, &sc->atse_miibus, ifp, atse_ifmedia_upd,
1701 atse_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
1703 device_printf(dev, "attaching PHY failed: %d\n", error);
1707 /* Call media-indepedent attach routine. */
1708 ether_ifattach(ifp, sc->atse_eth_addr);
1710 /* Tell the upper layer(s) about vlan mtu support. */
1711 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1712 ifp->if_capabilities |= IFCAP_VLAN_MTU;
1713 ifp->if_capenable = ifp->if_capabilities;
1714 #ifdef DEVICE_POLLING
1715 /* We will enable polling by default if no irqs available. See below. */
1716 ifp->if_capabilities |= IFCAP_POLLING;
1719 /* Hook up interrupts. */
1720 if (sc->atse_rx_irq_res != NULL) {
1721 error = bus_setup_intr(dev, sc->atse_rx_irq_res, INTR_TYPE_NET |
1722 INTR_MPSAFE, NULL, atse_intr, sc, &sc->atse_rx_intrhand);
1724 device_printf(dev, "enabling RX IRQ failed\n");
1725 ether_ifdetach(ifp);
1730 if (sc->atse_tx_irq_res != NULL) {
1731 error = bus_setup_intr(dev, sc->atse_tx_irq_res, INTR_TYPE_NET |
1732 INTR_MPSAFE, NULL, atse_intr, sc, &sc->atse_tx_intrhand);
1734 bus_teardown_intr(dev, sc->atse_rx_irq_res,
1735 sc->atse_rx_intrhand);
1736 device_printf(dev, "enabling TX IRQ failed\n");
1737 ether_ifdetach(ifp);
1742 if ((ifp->if_capenable & IFCAP_POLLING) != 0 ||
1743 (sc->atse_rx_irq_res == NULL && sc->atse_tx_irq_res == NULL)) {
1744 #ifdef DEVICE_POLLING
1745 /* If not on and no IRQs force it on. */
1746 if (sc->atse_rx_irq_res == NULL && sc->atse_tx_irq_res == NULL){
1747 ifp->if_capenable |= IFCAP_POLLING;
1748 device_printf(dev, "forcing to polling due to no "
1751 error = ether_poll_register(atse_poll, ifp);
1755 device_printf(dev, "no DEVICE_POLLING in kernel and no IRQs\n");
1759 ATSE_RX_INTR_ENABLE(sc);
1760 ATSE_TX_INTR_ENABLE(sc);
1768 atse_sysctl_stats_attach(dev);
1774 atse_detach(device_t dev)
1776 struct atse_softc *sc;
1779 sc = device_get_softc(dev);
1780 KASSERT(mtx_initialized(&sc->atse_mtx), ("%s: mutex not initialized",
1781 device_get_nameunit(dev)));
1784 #ifdef DEVICE_POLLING
1785 if (ifp->if_capenable & IFCAP_POLLING)
1786 ether_poll_deregister(ifp);
1789 /* Only cleanup if attach succeeded. */
1790 if (device_is_attached(dev)) {
1792 atse_stop_locked(sc);
1794 callout_drain(&sc->atse_tick);
1795 ether_ifdetach(ifp);
1797 if (sc->atse_miibus != NULL)
1798 device_delete_child(dev, sc->atse_miibus);
1800 if (sc->atse_tx_intrhand)
1801 bus_teardown_intr(dev, sc->atse_tx_irq_res,
1802 sc->atse_tx_intrhand);
1803 if (sc->atse_rx_intrhand)
1804 bus_teardown_intr(dev, sc->atse_rx_irq_res,
1805 sc->atse_rx_intrhand);
1810 if (sc->atse_tx_buf != NULL)
1811 free(sc->atse_tx_buf, M_DEVBUF);
1813 mtx_destroy(&sc->atse_mtx);
1818 /* Shared between nexus anf fdt implementation. */
1820 atse_detach_resources(device_t dev)
1822 struct atse_softc *sc;
1824 sc = device_get_softc(dev);
1826 if (sc->atse_txc_mem_res != NULL) {
1827 bus_release_resource(dev, SYS_RES_MEMORY, sc->atse_txc_mem_rid,
1828 sc->atse_txc_mem_res);
1829 sc->atse_txc_mem_res = NULL;
1831 if (sc->atse_tx_mem_res != NULL) {
1832 bus_release_resource(dev, SYS_RES_MEMORY, sc->atse_tx_mem_rid,
1833 sc->atse_tx_mem_res);
1834 sc->atse_tx_mem_res = NULL;
1836 if (sc->atse_tx_irq_res != NULL) {
1837 bus_release_resource(dev, SYS_RES_IRQ, sc->atse_tx_irq_rid,
1838 sc->atse_tx_irq_res);
1839 sc->atse_tx_irq_res = NULL;
1841 if (sc->atse_rxc_mem_res != NULL) {
1842 bus_release_resource(dev, SYS_RES_MEMORY, sc->atse_rxc_mem_rid,
1843 sc->atse_rxc_mem_res);
1844 sc->atse_rxc_mem_res = NULL;
1846 if (sc->atse_rx_mem_res != NULL) {
1847 bus_release_resource(dev, SYS_RES_MEMORY, sc->atse_rx_mem_rid,
1848 sc->atse_rx_mem_res);
1849 sc->atse_rx_mem_res = NULL;
1851 if (sc->atse_rx_irq_res != NULL) {
1852 bus_release_resource(dev, SYS_RES_IRQ, sc->atse_rx_irq_rid,
1853 sc->atse_rx_irq_res);
1854 sc->atse_rx_irq_res = NULL;
1856 if (sc->atse_mem_res != NULL) {
1857 bus_release_resource(dev, SYS_RES_MEMORY, sc->atse_mem_rid,
1859 sc->atse_mem_res = NULL;
1864 atse_detach_dev(device_t dev)
1868 error = atse_detach(dev);
1870 /* We are basically in undefined state now. */
1871 device_printf(dev, "atse_detach() failed: %d\n", error);
1875 atse_detach_resources(dev);
1881 atse_miibus_readreg(device_t dev, int phy, int reg)
1883 struct atse_softc *sc;
1885 sc = device_get_softc(dev);
1888 * We currently do not support re-mapping of MDIO space on-the-fly
1889 * but de-facto hard-code the phy#.
1891 if (phy != sc->atse_phy_addr)
1894 return (PHY_READ_2(sc, reg));
1898 atse_miibus_writereg(device_t dev, int phy, int reg, int data)
1900 struct atse_softc *sc;
1902 sc = device_get_softc(dev);
1905 * We currently do not support re-mapping of MDIO space on-the-fly
1906 * but de-facto hard-code the phy#.
1908 if (phy != sc->atse_phy_addr)
1911 PHY_WRITE_2(sc, reg, data);
1916 atse_miibus_statchg(device_t dev)
1918 struct atse_softc *sc;
1919 struct mii_data *mii;
1923 sc = device_get_softc(dev);
1924 ATSE_LOCK_ASSERT(sc);
1926 mii = device_get_softc(sc->atse_miibus);
1928 if (mii == NULL || ifp == NULL ||
1929 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1932 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
1934 /* Assume no link. */
1935 sc->atse_flags &= ~ATSE_FLAGS_LINK;
1937 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1938 (IFM_ACTIVE | IFM_AVALID)) {
1940 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1942 val4 |= BASE_CFG_COMMAND_CONFIG_ENA_10;
1943 val4 &= ~BASE_CFG_COMMAND_CONFIG_ETH_SPEED;
1944 sc->atse_flags |= ATSE_FLAGS_LINK;
1947 val4 &= ~BASE_CFG_COMMAND_CONFIG_ENA_10;
1948 val4 &= ~BASE_CFG_COMMAND_CONFIG_ETH_SPEED;
1949 sc->atse_flags |= ATSE_FLAGS_LINK;
1952 val4 &= ~BASE_CFG_COMMAND_CONFIG_ENA_10;
1953 val4 |= BASE_CFG_COMMAND_CONFIG_ETH_SPEED;
1954 sc->atse_flags |= ATSE_FLAGS_LINK;
1961 if ((sc->atse_flags & ATSE_FLAGS_LINK) == 0) {
1962 /* XXX-BZ need to stop the MAC? */
1966 if (IFM_OPTIONS(mii->mii_media_active & IFM_FDX) != 0)
1967 val4 &= ~BASE_CFG_COMMAND_CONFIG_HD_ENA;
1969 val4 |= BASE_CFG_COMMAND_CONFIG_HD_ENA;
1970 /* XXX-BZ flow control? */
1972 /* Make sure the MAC is activated. */
1973 val4 |= BASE_CFG_COMMAND_CONFIG_TX_ENA;
1974 val4 |= BASE_CFG_COMMAND_CONFIG_RX_ENA;
1976 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);