2 * Copyright (c) 2012,2013 Bjoern A. Zeeb
3 * Copyright (c) 2014 Robert N. M. Watson
6 * This software was developed by SRI International and the University of
7 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-11-C-0249)
8 * ("MRC2"), as part of the DARPA MRC research programme.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Altera Triple-Speed Ethernet MegaCore, Function User Guide
33 * UG-01008-3.0, Software Version: 12.0, June 2012.
34 * Available at the time of writing at:
35 * http://www.altera.com/literature/ug/ug_ethernet.pdf
37 * We are using an Marvell E1111 (Alaska) PHY on the DE4. See mii/e1000phy.c.
41 * - ifOutBroadcastPkts are only counted if both ether dst and src are all-1s;
42 * seems an IP core bug, they count ether broadcasts as multicast. Is this
44 * - figure out why the TX FIFO fill status and intr did not work as expected.
45 * - test 100Mbit/s and 10Mbit/s
46 * - blacklist the one special factory programmed ethernet address (for now
47 * hardcoded, later from loader?)
48 * - resolve all XXX, left as reminders to shake out details later
49 * - Jumbo frame support
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
55 #include "opt_device_polling.h"
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
61 #include <sys/endian.h>
64 #include <sys/module.h>
65 #include <sys/mutex.h>
67 #include <sys/socket.h>
68 #include <sys/sockio.h>
69 #include <sys/types.h>
71 #include <net/ethernet.h>
73 #include <net/if_var.h>
74 #include <net/if_dl.h>
75 #include <net/if_media.h>
76 #include <net/if_types.h>
77 #include <net/if_vlan_var.h>
81 #include <machine/bus.h>
82 #include <machine/resource.h>
85 #include <dev/mii/mii.h>
86 #include <dev/mii/miivar.h>
88 #include <dev/altera/atse/if_atsereg.h>
89 #include <dev/altera/atse/a_api.h>
91 MODULE_DEPEND(atse, ether, 1, 1, 1);
92 MODULE_DEPEND(atse, miibus, 1, 1, 1);
95 #define ATSE_WATCHDOG_TIME 5
98 static poll_handler_t atse_poll;
101 /* XXX once we'd do parallel attach, we need a global lock for this. */
102 #define ATSE_ETHERNET_OPTION_BITS_UNDEF 0
103 #define ATSE_ETHERNET_OPTION_BITS_READ 1
104 static int atse_ethernet_option_bits_flag = ATSE_ETHERNET_OPTION_BITS_UNDEF;
105 static uint8_t atse_ethernet_option_bits[ALTERA_ETHERNET_OPTION_BITS_LEN];
107 static int atse_intr_debug_enable = 0;
108 SYSCTL_INT(_debug, OID_AUTO, atse_intr_debug_enable, CTLFLAG_RW,
109 &atse_intr_debug_enable, 0,
110 "Extra debugging output for atse interrupts");
113 * Softc and critical resource locking.
115 #define ATSE_LOCK(_sc) mtx_lock(&(_sc)->atse_mtx)
116 #define ATSE_UNLOCK(_sc) mtx_unlock(&(_sc)->atse_mtx)
117 #define ATSE_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->atse_mtx, MA_OWNED)
119 #define ATSE_TX_PENDING(sc) (sc->atse_tx_m != NULL || \
120 !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
123 #define DPRINTF(format, ...) printf(format, __VA_ARGS__)
125 #define DPRINTF(format, ...)
128 /* a_api.c functions; factor out? */
130 a_onchip_fifo_mem_core_write(struct resource *res, uint32_t off,
131 uint32_t val4, const char *desc, const char *f, const int l)
134 val4 = htole32(val4);
135 DPRINTF("[%s:%d] FIFOW %s 0x%08x = 0x%08x\n", f, l, desc, off, val4);
136 bus_write_4(res, off, val4);
138 static inline uint32_t
139 a_onchip_fifo_mem_core_read(struct resource *res, uint32_t off,
140 const char *desc, const char *f, const int l)
144 val4 = le32toh(bus_read_4(res, off));
145 DPRINTF("[%s:%d] FIFOR %s 0x%08x = 0x%08x\n", f, l, desc, off, val4);
149 /* The FIFO does an endian convertion, so we must not do it as well. */
150 /* XXX-BZ in fact we should do a htobe32 so le would be fine as well? */
151 #define ATSE_TX_DATA_WRITE(sc, val4) \
152 bus_write_4((sc)->atse_tx_mem_res, A_ONCHIP_FIFO_MEM_CORE_DATA, val4)
154 #define ATSE_TX_META_WRITE(sc, val4) \
155 a_onchip_fifo_mem_core_write((sc)->atse_tx_mem_res, \
156 A_ONCHIP_FIFO_MEM_CORE_METADATA, \
157 (val4), "TXM", __func__, __LINE__)
158 #define ATSE_TX_META_READ(sc) \
159 a_onchip_fifo_mem_core_read((sc)->atse_tx_mem_res, \
160 A_ONCHIP_FIFO_MEM_CORE_METADATA, \
161 "TXM", __func__, __LINE__)
163 #define ATSE_TX_READ_FILL_LEVEL(sc) \
164 a_onchip_fifo_mem_core_read((sc)->atse_txc_mem_res, \
165 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_FILL_LEVEL, \
166 "TX_FILL", __func__, __LINE__)
167 #define ATSE_RX_READ_FILL_LEVEL(sc) \
168 a_onchip_fifo_mem_core_read((sc)->atse_rxc_mem_res, \
169 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_FILL_LEVEL, \
170 "RX_FILL", __func__, __LINE__)
172 /* The FIFO does an endian convertion, so we must not do it as well. */
173 /* XXX-BZ in fact we shoudl do a htobe32 so le would be fine as well? */
174 #define ATSE_RX_DATA_READ(sc) \
175 bus_read_4((sc)->atse_rx_mem_res, A_ONCHIP_FIFO_MEM_CORE_DATA)
176 #define ATSE_RX_META_READ(sc) \
177 a_onchip_fifo_mem_core_read((sc)->atse_rx_mem_res, \
178 A_ONCHIP_FIFO_MEM_CORE_METADATA, \
179 "RXM", __func__, __LINE__)
181 #define ATSE_RX_STATUS_READ(sc) \
182 a_onchip_fifo_mem_core_read((sc)->atse_rxc_mem_res, \
183 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_I_STATUS, \
184 "RX_EVENT", __func__, __LINE__)
186 #define ATSE_TX_STATUS_READ(sc) \
187 a_onchip_fifo_mem_core_read((sc)->atse_txc_mem_res, \
188 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_I_STATUS, \
189 "TX_EVENT", __func__, __LINE__)
191 #define ATSE_RX_EVENT_READ(sc) \
192 a_onchip_fifo_mem_core_read((sc)->atse_rxc_mem_res, \
193 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_EVENT, \
194 "RX_EVENT", __func__, __LINE__)
196 #define ATSE_TX_EVENT_READ(sc) \
197 a_onchip_fifo_mem_core_read((sc)->atse_txc_mem_res, \
198 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_EVENT, \
199 "TX_EVENT", __func__, __LINE__)
201 #define ATSE_RX_EVENT_CLEAR(sc) \
205 val4 = a_onchip_fifo_mem_core_read( \
206 (sc)->atse_rxc_mem_res, \
207 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_EVENT, \
208 "RX_EVENT", __func__, __LINE__); \
210 a_onchip_fifo_mem_core_write( \
211 (sc)->atse_rxc_mem_res, \
212 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_EVENT, \
213 val4, "RX_EVENT", __func__, __LINE__); \
215 #define ATSE_TX_EVENT_CLEAR(sc) \
219 val4 = a_onchip_fifo_mem_core_read( \
220 (sc)->atse_txc_mem_res, \
221 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_EVENT, \
222 "TX_EVENT", __func__, __LINE__); \
224 a_onchip_fifo_mem_core_write( \
225 (sc)->atse_txc_mem_res, \
226 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_EVENT, \
227 val4, "TX_EVENT", __func__, __LINE__); \
230 #define ATSE_RX_EVENTS (A_ONCHIP_FIFO_MEM_CORE_INTR_FULL | \
231 A_ONCHIP_FIFO_MEM_CORE_INTR_OVERFLOW | \
232 A_ONCHIP_FIFO_MEM_CORE_INTR_UNDERFLOW)
233 #define ATSE_RX_INTR_ENABLE(sc) \
234 a_onchip_fifo_mem_core_write((sc)->atse_rxc_mem_res, \
235 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_INT_ENABLE, \
237 "RX_INTR", __func__, __LINE__) /* XXX-BZ review later. */
238 #define ATSE_RX_INTR_DISABLE(sc) \
239 a_onchip_fifo_mem_core_write((sc)->atse_rxc_mem_res, \
240 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_INT_ENABLE, 0, \
241 "RX_INTR", __func__, __LINE__)
242 #define ATSE_RX_INTR_READ(sc) \
243 a_onchip_fifo_mem_core_read((sc)->atse_rxc_mem_res, \
244 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_INT_ENABLE, \
245 "RX_INTR", __func__, __LINE__)
247 #define ATSE_TX_EVENTS (A_ONCHIP_FIFO_MEM_CORE_INTR_EMPTY | \
248 A_ONCHIP_FIFO_MEM_CORE_INTR_OVERFLOW | \
249 A_ONCHIP_FIFO_MEM_CORE_INTR_UNDERFLOW)
250 #define ATSE_TX_INTR_ENABLE(sc) \
251 a_onchip_fifo_mem_core_write((sc)->atse_txc_mem_res, \
252 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_INT_ENABLE, \
254 "TX_INTR", __func__, __LINE__) /* XXX-BZ review later. */
255 #define ATSE_TX_INTR_DISABLE(sc) \
256 a_onchip_fifo_mem_core_write((sc)->atse_txc_mem_res, \
257 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_INT_ENABLE, 0, \
258 "TX_INTR", __func__, __LINE__)
259 #define ATSE_TX_INTR_READ(sc) \
260 a_onchip_fifo_mem_core_read((sc)->atse_txc_mem_res, \
261 A_ONCHIP_FIFO_MEM_CORE_STATUS_REG_INT_ENABLE, \
262 "TX_INTR", __func__, __LINE__)
264 static int atse_rx_locked(struct atse_softc *sc);
267 * Register space access macros.
270 csr_write_4(struct atse_softc *sc, uint32_t reg, uint32_t val4,
271 const char *f, const int l)
274 val4 = htole32(val4);
275 DPRINTF("[%s:%d] CSR W %s 0x%08x (0x%08x) = 0x%08x\n", f, l,
276 "atse_mem_res", reg, reg * 4, val4);
277 bus_write_4(sc->atse_mem_res, reg * 4, val4);
280 static inline uint32_t
281 csr_read_4(struct atse_softc *sc, uint32_t reg, const char *f, const int l)
285 val4 = le32toh(bus_read_4(sc->atse_mem_res, reg * 4));
286 DPRINTF("[%s:%d] CSR R %s 0x%08x (0x%08x) = 0x%08x\n", f, l,
287 "atse_mem_res", reg, reg * 4, val4);
292 * See page 5-2 that it's all dword offsets and the MS 16 bits need to be zero
293 * on write and ignored on read.
296 pxx_write_2(struct atse_softc *sc, bus_addr_t bmcr, uint32_t reg, uint16_t val,
297 const char *f, const int l, const char *s)
301 val4 = htole32(val & 0x0000ffff);
302 DPRINTF("[%s:%d] %s W %s 0x%08x (0x%08jx) = 0x%08x\n", f, l, s,
303 "atse_mem_res", reg, (bmcr + reg) * 4, val4);
304 bus_write_4(sc->atse_mem_res, (bmcr + reg) * 4, val4);
307 static inline uint16_t
308 pxx_read_2(struct atse_softc *sc, bus_addr_t bmcr, uint32_t reg, const char *f,
309 const int l, const char *s)
314 val4 = bus_read_4(sc->atse_mem_res, (bmcr + reg) * 4);
315 val = le32toh(val4) & 0x0000ffff;
316 DPRINTF("[%s:%d] %s R %s 0x%08x (0x%08jx) = 0x%04x\n", f, l, s,
317 "atse_mem_res", reg, (bmcr + reg) * 4, val);
321 #define CSR_WRITE_4(sc, reg, val) \
322 csr_write_4((sc), (reg), (val), __func__, __LINE__)
323 #define CSR_READ_4(sc, reg) \
324 csr_read_4((sc), (reg), __func__, __LINE__)
325 #define PCS_WRITE_2(sc, reg, val) \
326 pxx_write_2((sc), sc->atse_bmcr0, (reg), (val), __func__, __LINE__, \
328 #define PCS_READ_2(sc, reg) \
329 pxx_read_2((sc), sc->atse_bmcr0, (reg), __func__, __LINE__, "PCS")
330 #define PHY_WRITE_2(sc, reg, val) \
331 pxx_write_2((sc), sc->atse_bmcr1, (reg), (val), __func__, __LINE__, \
333 #define PHY_READ_2(sc, reg) \
334 pxx_read_2((sc), sc->atse_bmcr1, (reg), __func__, __LINE__, "PHY")
336 static void atse_tick(void *);
337 static int atse_detach(device_t);
339 devclass_t atse_devclass;
342 atse_tx_locked(struct atse_softc *sc, int *sent)
345 uint32_t val4, fill_level;
348 ATSE_LOCK_ASSERT(sc);
351 KASSERT(m != NULL, ("%s: m is null: sc=%p", __func__, sc));
352 KASSERT(m->m_flags & M_PKTHDR, ("%s: not a pkthdr: m=%p", __func__, m));
355 * Copy to buffer to minimize our pain as we can only store
356 * double words which, after the first mbuf gets out of alignment
359 if (sc->atse_tx_m_offset == 0) {
360 m_copydata(m, 0, m->m_pkthdr.len, sc->atse_tx_buf);
361 sc->atse_tx_buf_len = m->m_pkthdr.len;
364 fill_level = ATSE_TX_READ_FILL_LEVEL(sc);
365 #if 0 /* Returns 0xdeadc0de. */
366 val4 = ATSE_TX_META_READ(sc);
368 if (sc->atse_tx_m_offset == 0) {
369 /* Write start of packet. */
370 val4 = A_ONCHIP_FIFO_MEM_CORE_SOP;
371 val4 &= ~A_ONCHIP_FIFO_MEM_CORE_EOP;
372 ATSE_TX_META_WRITE(sc, val4);
375 /* TX FIFO is single clock mode, so we have the full FIFO. */
377 while ((sc->atse_tx_buf_len - sc->atse_tx_m_offset) > 4 &&
378 fill_level < AVALON_FIFO_TX_BASIC_OPTS_DEPTH) {
380 bcopy(&sc->atse_tx_buf[sc->atse_tx_m_offset], &val4,
382 ATSE_TX_DATA_WRITE(sc, val4);
383 sc->atse_tx_m_offset += sizeof(val4);
387 if (fill_level == AVALON_FIFO_TX_BASIC_OPTS_DEPTH)
388 fill_level = ATSE_TX_READ_FILL_LEVEL(sc);
393 /* Set EOP *before* writing the last symbol. */
394 if (sc->atse_tx_m_offset >= (sc->atse_tx_buf_len - 4) &&
395 fill_level < AVALON_FIFO_TX_BASIC_OPTS_DEPTH) {
399 /* Set EndOfPacket. */
400 val4 = A_ONCHIP_FIFO_MEM_CORE_EOP;
402 leftm = sc->atse_tx_buf_len - sc->atse_tx_m_offset;
403 val4 |= ((4 - leftm) << A_ONCHIP_FIFO_MEM_CORE_EMPTY_SHIFT);
405 ATSE_TX_META_WRITE(sc, val4);
407 /* Write last symbol. */
409 bcopy(sc->atse_tx_buf + sc->atse_tx_m_offset, &val4, leftm);
410 ATSE_TX_DATA_WRITE(sc, val4);
415 /* OK, the packet is gone. */
416 sc->atse_tx_m = NULL;
417 sc->atse_tx_m_offset = 0;
419 /* If anyone is interested give them a copy. */
420 BPF_MTAP(sc->atse_ifp, m);
430 atse_start_locked(struct ifnet *ifp)
432 struct atse_softc *sc;
436 ATSE_LOCK_ASSERT(sc);
438 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
439 IFF_DRV_RUNNING || (sc->atse_flags & ATSE_FLAGS_LINK) == 0)
444 * Disable the watchdog while sending, we are batching packets.
445 * Though we should never reach 5 seconds, and are holding the lock,
448 sc->atse_watchdog_timer = 0;
451 if (sc->atse_tx_m != NULL) {
452 error = atse_tx_locked(sc, &sent);
456 /* We have more space to send so continue ... */
457 for (; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
459 IFQ_DRV_DEQUEUE(&ifp->if_snd, sc->atse_tx_m);
460 sc->atse_tx_m_offset = 0;
461 if (sc->atse_tx_m == NULL)
463 error = atse_tx_locked(sc, &sent);
469 /* If the IP core walks into Nekromanteion try to bail out. */
471 sc->atse_watchdog_timer = ATSE_WATCHDOG_TIME;
475 atse_start(struct ifnet *ifp)
477 struct atse_softc *sc;
481 atse_start_locked(ifp);
486 atse_stop_locked(struct atse_softc *sc)
492 ATSE_LOCK_ASSERT(sc);
494 sc->atse_watchdog_timer = 0;
495 callout_stop(&sc->atse_tick);
498 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
499 ATSE_RX_INTR_DISABLE(sc);
500 ATSE_TX_INTR_DISABLE(sc);
501 ATSE_RX_EVENT_CLEAR(sc);
502 ATSE_TX_EVENT_CLEAR(sc);
504 /* Disable MAC transmit and receive datapath. */
505 mask = BASE_CFG_COMMAND_CONFIG_TX_ENA|BASE_CFG_COMMAND_CONFIG_RX_ENA;
506 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
508 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);
509 /* Wait for bits to be cleared; i=100 is excessive. */
510 for (i = 0; i < 100; i++) {
511 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
512 if ((val4 & mask) == 0)
516 if ((val4 & mask) != 0)
517 device_printf(sc->atse_dev, "Disabling MAC TX/RX timed out.\n");
520 sc->atse_flags &= ~ATSE_FLAGS_LINK;
522 /* XXX-BZ free the RX/TX rings. */
528 atse_mchash(struct atse_softc *sc __unused, const uint8_t *addr)
534 for (i = 0; i < ETHER_ADDR_LEN; i++) {
536 for (j = 1; j < 8; j++)
537 y ^= (addr[i] >> j) & 0x01;
544 atse_rxfilter_locked(struct atse_softc *sc)
547 struct ifmultiaddr *ifma;
551 /* XXX-BZ can we find out if we have the MHASH synthesized? */
552 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
553 /* For simplicity always hash full 48 bits of addresses. */
554 if ((val4 & BASE_CFG_COMMAND_CONFIG_MHASH_SEL) != 0)
555 val4 &= ~BASE_CFG_COMMAND_CONFIG_MHASH_SEL;
558 if (ifp->if_flags & IFF_PROMISC)
559 val4 |= BASE_CFG_COMMAND_CONFIG_PROMIS_EN;
561 val4 &= ~BASE_CFG_COMMAND_CONFIG_PROMIS_EN;
563 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);
565 if (ifp->if_flags & IFF_ALLMULTI) {
566 /* Accept all multicast addresses. */
567 for (i = 0; i <= MHASH_LEN; i++)
568 CSR_WRITE_4(sc, MHASH_START + i, 0x1);
571 * Can hold MHASH_LEN entries.
572 * XXX-BZ bitstring.h would be more general.
578 * Re-build and re-program hash table. First build the
579 * bit-field "yes" or "no" for each slot per address, then
580 * do all the programming afterwards.
583 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
584 if (ifma->ifma_addr->sa_family != AF_LINK)
587 h |= (1 << atse_mchash(sc,
588 LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
590 if_maddr_runlock(ifp);
591 for (i = 0; i <= MHASH_LEN; i++)
592 CSR_WRITE_4(sc, MHASH_START + i,
593 (h & (1 << i)) ? 0x01 : 0x00);
600 atse_ethernet_option_bits_read_fdt(device_t dev)
602 struct resource *res;
606 if (atse_ethernet_option_bits_flag & ATSE_ETHERNET_OPTION_BITS_READ)
609 fdev = device_find_child(device_get_parent(dev), "cfi", 0);
614 res = bus_alloc_resource_any(fdev, SYS_RES_MEMORY, &rid,
615 RF_ACTIVE | RF_SHAREABLE);
619 for (i = 0; i < ALTERA_ETHERNET_OPTION_BITS_LEN; i++)
620 atse_ethernet_option_bits[i] = bus_read_1(res,
621 ALTERA_ETHERNET_OPTION_BITS_OFF + i);
623 bus_release_resource(fdev, SYS_RES_MEMORY, rid, res);
624 atse_ethernet_option_bits_flag |= ATSE_ETHERNET_OPTION_BITS_READ;
630 atse_ethernet_option_bits_read(device_t dev)
634 error = atse_ethernet_option_bits_read_fdt(dev);
638 device_printf(dev, "Cannot read Ethernet addresses from flash.\n");
643 atse_get_eth_address(struct atse_softc *sc)
645 unsigned long hostid;
650 * Make sure to only ever do this once. Otherwise a reset would
651 * possibly change our ethernet address, which is not good at all.
653 if (sc->atse_eth_addr[0] != 0x00 || sc->atse_eth_addr[1] != 0x00 ||
654 sc->atse_eth_addr[2] != 0x00)
657 if ((atse_ethernet_option_bits_flag &
658 ATSE_ETHERNET_OPTION_BITS_READ) == 0)
661 val4 = atse_ethernet_option_bits[0] << 24;
662 val4 |= atse_ethernet_option_bits[1] << 16;
663 val4 |= atse_ethernet_option_bits[2] << 8;
664 val4 |= atse_ethernet_option_bits[3];
665 /* They chose "safe". */
666 if (val4 != le32toh(0x00005afe)) {
667 device_printf(sc->atse_dev, "Magic '5afe' is not safe: 0x%08x. "
668 "Falling back to random numbers for hardware address.\n",
673 sc->atse_eth_addr[0] = atse_ethernet_option_bits[4];
674 sc->atse_eth_addr[1] = atse_ethernet_option_bits[5];
675 sc->atse_eth_addr[2] = atse_ethernet_option_bits[6];
676 sc->atse_eth_addr[3] = atse_ethernet_option_bits[7];
677 sc->atse_eth_addr[4] = atse_ethernet_option_bits[8];
678 sc->atse_eth_addr[5] = atse_ethernet_option_bits[9];
680 /* Handle factory default ethernet addresss: 00:07:ed:ff:ed:15 */
681 if (sc->atse_eth_addr[0] == 0x00 && sc->atse_eth_addr[1] == 0x07 &&
682 sc->atse_eth_addr[2] == 0xed && sc->atse_eth_addr[3] == 0xff &&
683 sc->atse_eth_addr[4] == 0xed && sc->atse_eth_addr[5] == 0x15) {
685 device_printf(sc->atse_dev, "Factory programmed Ethernet "
686 "hardware address blacklisted. Falling back to random "
687 "address to avoid collisions.\n");
688 device_printf(sc->atse_dev, "Please re-program your flash.\n");
692 if (sc->atse_eth_addr[0] == 0x00 && sc->atse_eth_addr[1] == 0x00 &&
693 sc->atse_eth_addr[2] == 0x00 && sc->atse_eth_addr[3] == 0x00 &&
694 sc->atse_eth_addr[4] == 0x00 && sc->atse_eth_addr[5] == 0x00) {
695 device_printf(sc->atse_dev, "All zero's Ethernet hardware "
696 "address blacklisted. Falling back to random address.\n");
697 device_printf(sc->atse_dev, "Please re-program your flash.\n");
701 if (ETHER_IS_MULTICAST(sc->atse_eth_addr)) {
702 device_printf(sc->atse_dev, "Multicast Ethernet hardware "
703 "address blacklisted. Falling back to random address.\n");
704 device_printf(sc->atse_dev, "Please re-program your flash.\n");
709 * If we find an Altera prefixed address with a 0x0 ending
710 * adjust by device unit. If not and this is not the first
711 * Ethernet, go to random.
713 unit = device_get_unit(sc->atse_dev);
718 device_printf(sc->atse_dev, "We do not support Ethernet "
719 "addresses for more than 16 MACs. Falling back to "
720 "random hadware address.\n");
723 if ((sc->atse_eth_addr[0] & ~0x2) != 0 ||
724 sc->atse_eth_addr[1] != 0x07 || sc->atse_eth_addr[2] != 0xed ||
725 (sc->atse_eth_addr[5] & 0x0f) != 0x0) {
726 device_printf(sc->atse_dev, "Ethernet address not meeting our "
727 "multi-MAC standards. Falling back to random hadware "
731 sc->atse_eth_addr[5] |= (unit & 0x0f);
737 * Fall back to random code we also use on bridge(4).
739 getcredhostid(curthread->td_ucred, &hostid);
741 arc4rand(sc->atse_eth_addr, ETHER_ADDR_LEN, 1);
742 sc->atse_eth_addr[0] &= ~1;/* clear multicast bit */
743 sc->atse_eth_addr[0] |= 2; /* set the LAA bit */
745 sc->atse_eth_addr[0] = 0x2;
746 sc->atse_eth_addr[1] = (hostid >> 24) & 0xff;
747 sc->atse_eth_addr[2] = (hostid >> 16) & 0xff;
748 sc->atse_eth_addr[3] = (hostid >> 8 ) & 0xff;
749 sc->atse_eth_addr[4] = hostid & 0xff;
750 sc->atse_eth_addr[5] = sc->atse_unit & 0xff;
757 atse_set_eth_address(struct atse_softc *sc, int n)
761 v0 = (sc->atse_eth_addr[3] << 24) | (sc->atse_eth_addr[2] << 16) |
762 (sc->atse_eth_addr[1] << 8) | sc->atse_eth_addr[0];
763 v1 = (sc->atse_eth_addr[5] << 8) | sc->atse_eth_addr[4];
765 if (n & ATSE_ETH_ADDR_DEF) {
766 CSR_WRITE_4(sc, BASE_CFG_MAC_0, v0);
767 CSR_WRITE_4(sc, BASE_CFG_MAC_1, v1);
769 if (n & ATSE_ETH_ADDR_SUPP1) {
770 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_0_0, v0);
771 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_0_1, v1);
773 if (n & ATSE_ETH_ADDR_SUPP2) {
774 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_1_0, v0);
775 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_1_1, v1);
777 if (n & ATSE_ETH_ADDR_SUPP3) {
778 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_2_0, v0);
779 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_2_1, v1);
781 if (n & ATSE_ETH_ADDR_SUPP4) {
782 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_3_0, v0);
783 CSR_WRITE_4(sc, SUPPL_ADDR_SMAC_3_1, v1);
790 atse_reset(struct atse_softc *sc)
796 /* 1. External PHY Initialization using MDIO. */
798 * We select the right MDIO space in atse_attach() and let MII do
802 /* 2. PCS Configuration Register Initialization. */
803 /* a. Set auto negotiation link timer to 1.6ms for SGMII. */
804 PCS_WRITE_2(sc, PCS_EXT_LINK_TIMER_0, 0x0D40);
805 PCS_WRITE_2(sc, PCS_EXT_LINK_TIMER_1, 0x0003);
807 /* b. Configure SGMII. */
808 val = PCS_EXT_IF_MODE_SGMII_ENA|PCS_EXT_IF_MODE_USE_SGMII_AN;
809 PCS_WRITE_2(sc, PCS_EXT_IF_MODE, val);
811 /* c. Enable auto negotiation. */
812 /* Ignore Bits 6,8,13; should be set,set,unset. */
813 val = PCS_READ_2(sc, PCS_CONTROL);
814 val &= ~(PCS_CONTROL_ISOLATE|PCS_CONTROL_POWERDOWN);
815 val &= ~PCS_CONTROL_LOOPBACK; /* Make this a -link1 option? */
816 val |= PCS_CONTROL_AUTO_NEGOTIATION_ENABLE;
817 PCS_WRITE_2(sc, PCS_CONTROL, val);
820 val = PCS_READ_2(sc, PCS_CONTROL);
821 val |= PCS_CONTROL_RESET;
822 PCS_WRITE_2(sc, PCS_CONTROL, val);
823 /* Wait for reset bit to clear; i=100 is excessive. */
824 for (i = 0; i < 100; i++) {
825 val = PCS_READ_2(sc, PCS_CONTROL);
826 if ((val & PCS_CONTROL_RESET) == 0)
830 if ((val & PCS_CONTROL_RESET) != 0) {
831 device_printf(sc->atse_dev, "PCS reset timed out.\n");
835 /* 3. MAC Configuration Register Initialization. */
836 /* a. Disable MAC transmit and receive datapath. */
837 mask = BASE_CFG_COMMAND_CONFIG_TX_ENA|BASE_CFG_COMMAND_CONFIG_RX_ENA;
838 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
840 /* Samples in the manual do have the SW_RESET bit set here, why? */
841 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);
842 /* Wait for bits to be cleared; i=100 is excessive. */
843 for (i = 0; i < 100; i++) {
844 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
845 if ((val4 & mask) == 0)
849 if ((val4 & mask) != 0) {
850 device_printf(sc->atse_dev, "Disabling MAC TX/RX timed out.\n");
853 /* b. MAC FIFO configuration. */
854 CSR_WRITE_4(sc, BASE_CFG_TX_SECTION_EMPTY, FIFO_DEPTH_TX - 16);
855 CSR_WRITE_4(sc, BASE_CFG_TX_ALMOST_FULL, 3);
856 CSR_WRITE_4(sc, BASE_CFG_TX_ALMOST_EMPTY, 8);
857 CSR_WRITE_4(sc, BASE_CFG_RX_SECTION_EMPTY, FIFO_DEPTH_RX - 16);
858 CSR_WRITE_4(sc, BASE_CFG_RX_ALMOST_FULL, 8);
859 CSR_WRITE_4(sc, BASE_CFG_RX_ALMOST_EMPTY, 8);
861 CSR_WRITE_4(sc, BASE_CFG_TX_SECTION_FULL, 16);
862 CSR_WRITE_4(sc, BASE_CFG_RX_SECTION_FULL, 16);
864 /* For store-and-forward mode, set this threshold to 0. */
865 CSR_WRITE_4(sc, BASE_CFG_TX_SECTION_FULL, 0);
866 CSR_WRITE_4(sc, BASE_CFG_RX_SECTION_FULL, 0);
868 /* c. MAC address configuration. */
869 /* Also intialize supplementary addresses to our primary one. */
870 /* XXX-BZ FreeBSD really needs to grow and API for using these. */
871 atse_get_eth_address(sc);
872 atse_set_eth_address(sc, ATSE_ETH_ADDR_ALL);
874 /* d. MAC function configuration. */
875 CSR_WRITE_4(sc, BASE_CFG_FRM_LENGTH, 1518); /* Default. */
876 CSR_WRITE_4(sc, BASE_CFG_TX_IPG_LENGTH, 12);
877 CSR_WRITE_4(sc, BASE_CFG_PAUSE_QUANT, 0xFFFF);
879 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
881 * If 1000BASE-X/SGMII PCS is initialized, set the ETH_SPEED (bit 3)
882 * and ENA_10 (bit 25) in command_config register to 0. If half duplex
883 * is reported in the PHY/PCS status register, set the HD_ENA (bit 10)
884 * to 1 in command_config register.
885 * BZ: We shoot for 1000 instead.
888 val4 |= BASE_CFG_COMMAND_CONFIG_ETH_SPEED;
890 val4 &= ~BASE_CFG_COMMAND_CONFIG_ETH_SPEED;
892 val4 &= ~BASE_CFG_COMMAND_CONFIG_ENA_10;
895 * We do not want to set this, otherwise, we could not even send
896 * random raw ethernet frames for various other research. By default
897 * FreeBSD will use the right ether source address.
899 val4 |= BASE_CFG_COMMAND_CONFIG_TX_ADDR_INS;
901 val4 |= BASE_CFG_COMMAND_CONFIG_PAD_EN;
902 val4 &= ~BASE_CFG_COMMAND_CONFIG_CRC_FWD;
904 val4 |= BASE_CFG_COMMAND_CONFIG_CNTL_FRM_ENA;
907 val4 |= BASE_CFG_COMMAND_CONFIG_RX_ERR_DISC;
909 val &= ~BASE_CFG_COMMAND_CONFIG_LOOP_ENA; /* link0? */
910 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);
913 * Make sure we do not enable 32bit alignment; FreeBSD cannot
914 * cope with the additional padding (though we should!?).
915 * Also make sure we get the CRC appended.
917 val4 = CSR_READ_4(sc, TX_CMD_STAT);
918 val4 &= ~(TX_CMD_STAT_OMIT_CRC|TX_CMD_STAT_TX_SHIFT16);
919 CSR_WRITE_4(sc, TX_CMD_STAT, val4);
920 val4 = CSR_READ_4(sc, RX_CMD_STAT);
921 val4 &= ~RX_CMD_STAT_RX_SHIFT16;
922 CSR_WRITE_4(sc, RX_CMD_STAT, val4);
925 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
926 val4 |= BASE_CFG_COMMAND_CONFIG_SW_RESET;
927 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);
928 /* Wait for bits to be cleared; i=100 is excessive. */
929 for (i = 0; i < 100; i++) {
930 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
931 if ((val4 & BASE_CFG_COMMAND_CONFIG_SW_RESET) == 0)
935 if ((val4 & BASE_CFG_COMMAND_CONFIG_SW_RESET) != 0) {
936 device_printf(sc->atse_dev, "MAC reset timed out.\n");
940 /* f. Enable MAC transmit and receive datapath. */
941 mask = BASE_CFG_COMMAND_CONFIG_TX_ENA|BASE_CFG_COMMAND_CONFIG_RX_ENA;
942 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
944 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);
945 /* Wait for bits to be cleared; i=100 is excessive. */
946 for (i = 0; i < 100; i++) {
947 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
948 if ((val4 & mask) == mask)
952 if ((val4 & mask) != mask) {
953 device_printf(sc->atse_dev, "Enabling MAC TX/RX timed out.\n");
961 atse_init_locked(struct atse_softc *sc)
964 struct mii_data *mii;
967 ATSE_LOCK_ASSERT(sc);
970 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
974 * Must update the ether address if changed. Given we do not handle
975 * in atse_ioctl() but it's in the general framework, just always
976 * do it here before atse_reset().
978 eaddr = IF_LLADDR(sc->atse_ifp);
979 bcopy(eaddr, &sc->atse_eth_addr, ETHER_ADDR_LEN);
981 /* Make things frind to halt, cleanup, ... */
982 atse_stop_locked(sc);
986 /* ... and fire up the engine again. */
987 atse_rxfilter_locked(sc);
989 /* Memory rings? DMA engine? */
991 sc->atse_rx_buf_len = 0;
992 sc->atse_flags &= ATSE_FLAGS_LINK; /* Preserve. */
994 #ifdef DEVICE_POLLING
995 /* Only enable interrupts if we are not polling. */
996 if (ifp->if_capenable & IFCAP_POLLING) {
997 ATSE_RX_INTR_DISABLE(sc);
998 ATSE_TX_INTR_DISABLE(sc);
999 ATSE_RX_EVENT_CLEAR(sc);
1000 ATSE_TX_EVENT_CLEAR(sc);
1004 ATSE_RX_INTR_ENABLE(sc);
1005 ATSE_TX_INTR_ENABLE(sc);
1008 mii = device_get_softc(sc->atse_miibus);
1010 sc->atse_flags &= ~ATSE_FLAGS_LINK;
1013 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1014 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1016 callout_reset(&sc->atse_tick, hz, atse_tick, sc);
1020 atse_init(void *xsc)
1022 struct atse_softc *sc;
1025 * XXXRW: There is some argument that we should immediately do RX
1026 * processing after enabling interrupts, or one may not fire if there
1027 * are buffered packets.
1029 sc = (struct atse_softc *)xsc;
1031 atse_init_locked(sc);
1036 atse_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1038 struct atse_softc *sc;
1045 ifr = (struct ifreq *)data;
1050 if (ifp->if_flags & IFF_UP) {
1051 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
1052 ((ifp->if_flags ^ sc->atse_if_flags) &
1053 (IFF_PROMISC | IFF_ALLMULTI)) != 0)
1054 atse_rxfilter_locked(sc);
1056 atse_init_locked(sc);
1057 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1058 atse_stop_locked(sc);
1059 sc->atse_if_flags = ifp->if_flags;
1064 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1065 #ifdef DEVICE_POLLING
1066 if ((mask & IFCAP_POLLING) != 0 &&
1067 (IFCAP_POLLING & ifp->if_capabilities) != 0) {
1068 ifp->if_capenable ^= IFCAP_POLLING;
1069 if ((IFCAP_POLLING & ifp->if_capenable) != 0) {
1071 error = ether_poll_register(atse_poll, ifp);
1076 /* Disable interrupts. */
1077 ATSE_RX_INTR_DISABLE(sc);
1078 ATSE_TX_INTR_DISABLE(sc);
1079 ATSE_RX_EVENT_CLEAR(sc);
1080 ATSE_TX_EVENT_CLEAR(sc);
1083 * Do not allow disabling of polling if we do
1084 * not have interrupts.
1086 } else if (sc->atse_rx_irq_res != NULL ||
1087 sc->atse_tx_irq_res != NULL) {
1088 error = ether_poll_deregister(ifp);
1089 /* Enable interrupts. */
1090 ATSE_RX_INTR_ENABLE(sc);
1091 ATSE_TX_INTR_ENABLE(sc);
1093 ifp->if_capenable ^= IFCAP_POLLING;
1097 #endif /* DEVICE_POLLING */
1103 atse_rxfilter_locked(sc);
1109 struct mii_data *mii;
1112 mii = device_get_softc(sc->atse_miibus);
1113 ifr = (struct ifreq *)data;
1114 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1118 error = ether_ioctl(ifp, command, data);
1126 atse_intr_debug(struct atse_softc *sc, const char *intrname)
1128 uint32_t rxs, rxe, rxi, rxf, txs, txe, txi, txf;
1130 if (!atse_intr_debug_enable)
1133 rxs = ATSE_RX_STATUS_READ(sc);
1134 rxe = ATSE_RX_EVENT_READ(sc);
1135 rxi = ATSE_RX_INTR_READ(sc);
1136 rxf = ATSE_RX_READ_FILL_LEVEL(sc);
1138 txs = ATSE_TX_STATUS_READ(sc);
1139 txe = ATSE_TX_EVENT_READ(sc);
1140 txi = ATSE_TX_INTR_READ(sc);
1141 txf = ATSE_TX_READ_FILL_LEVEL(sc);
1145 "rxs 0x%x rxe 0x%x rxi 0x%x rxf 0x%x "
1146 "txs 0x%x txe 0x%x txi 0x%x txf 0x%x\n",
1149 txs, txe, txi, txf);
1153 atse_watchdog(struct atse_softc *sc)
1156 ATSE_LOCK_ASSERT(sc);
1158 if (sc->atse_watchdog_timer == 0 || --sc->atse_watchdog_timer > 0)
1161 device_printf(sc->atse_dev, "watchdog timeout\n");
1162 sc->atse_ifp->if_oerrors++;
1164 atse_intr_debug(sc, "poll");
1166 sc->atse_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1167 atse_init_locked(sc);
1170 if (!IFQ_DRV_IS_EMPTY(&sc->atse_ifp->if_snd))
1171 atse_start_locked(sc->atse_ifp);
1175 atse_tick(void *xsc)
1177 struct atse_softc *sc;
1178 struct mii_data *mii;
1181 sc = (struct atse_softc *)xsc;
1182 ATSE_LOCK_ASSERT(sc);
1185 mii = device_get_softc(sc->atse_miibus);
1188 if ((sc->atse_flags & ATSE_FLAGS_LINK) == 0)
1189 atse_miibus_statchg(sc->atse_dev);
1190 callout_reset(&sc->atse_tick, hz, atse_tick, sc);
1194 * Set media options.
1197 atse_ifmedia_upd(struct ifnet *ifp)
1199 struct atse_softc *sc;
1200 struct mii_data *mii;
1201 struct mii_softc *miisc;
1207 mii = device_get_softc(sc->atse_miibus);
1208 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1210 error = mii_mediachg(mii);
1217 atse_update_rx_err(struct atse_softc *sc, uint32_t mask)
1221 /* RX error are 6 bits, we only know 4 of them. */
1222 for (i = 0; i < ATSE_RX_ERR_MAX; i++)
1223 if ((mask & (1 << i)) != 0)
1224 sc->atse_rx_err[i]++;
1228 atse_rx_locked(struct atse_softc *sc)
1232 uint32_t fill, i, j;
1233 uint32_t data, meta;
1236 ATSE_LOCK_ASSERT(sc);
1243 if (sc->atse_rx_m == NULL) {
1244 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1247 m->m_len = m->m_pkthdr.len = MCLBYTES;
1248 /* Make sure upper layers will be aligned. */
1249 m_adj(m, ETHER_ALIGN);
1253 fill = ATSE_RX_READ_FILL_LEVEL(sc);
1254 for (i = 0; i < fill; i++) {
1256 * XXX-BZ for whatever reason the FIFO requires the
1257 * the data read before we can access the meta data.
1259 data = ATSE_RX_DATA_READ(sc);
1260 meta = ATSE_RX_META_READ(sc);
1261 if (meta & A_ONCHIP_FIFO_MEM_CORE_ERROR_MASK) {
1262 /* XXX-BZ evaluate error. */
1263 atse_update_rx_err(sc, ((meta &
1264 A_ONCHIP_FIFO_MEM_CORE_ERROR_MASK) >>
1265 A_ONCHIP_FIFO_MEM_CORE_ERROR_SHIFT) & 0xff);
1267 sc->atse_rx_buf_len = 0;
1269 * Should still read till EOP or next SOP.
1271 * XXX-BZ might also depend on
1272 * BASE_CFG_COMMAND_CONFIG_RX_ERR_DISC
1274 sc->atse_flags |= ATSE_FLAGS_ERROR;
1277 if ((meta & A_ONCHIP_FIFO_MEM_CORE_CHANNEL_MASK) != 0)
1278 device_printf(sc->atse_dev, "%s: unexpected "
1279 "channel %u\n", __func__, (meta &
1280 A_ONCHIP_FIFO_MEM_CORE_CHANNEL_MASK) >>
1281 A_ONCHIP_FIFO_MEM_CORE_CHANNEL_SHIFT);
1283 if (meta & A_ONCHIP_FIFO_MEM_CORE_SOP) {
1285 * There is no need to clear SOP between 1st
1286 * and subsequent packet data junks.
1288 if (sc->atse_rx_buf_len != 0 &&
1289 (sc->atse_flags & ATSE_FLAGS_SOP_SEEN) == 0)
1291 device_printf(sc->atse_dev, "%s: SOP "
1292 "without empty buffer: %u\n",
1293 __func__, sc->atse_rx_buf_len);
1294 /* XXX-BZ any better counter? */
1298 if ((sc->atse_flags & ATSE_FLAGS_SOP_SEEN) == 0)
1300 sc->atse_flags |= ATSE_FLAGS_SOP_SEEN;
1301 sc->atse_rx_buf_len = 0;
1304 #if 0 /* We had to read the data before we could access meta data. See above. */
1305 data = ATSE_RX_DATA_READ(sc);
1307 /* Make sure to not overflow the mbuf data size. */
1308 if (sc->atse_rx_buf_len >= sc->atse_rx_m->m_len -
1311 * XXX-BZ Error. We need more mbufs and are
1312 * not setup for this yet.
1315 sc->atse_flags |= ATSE_FLAGS_ERROR;
1317 if ((sc->atse_flags & ATSE_FLAGS_ERROR) == 0)
1319 * MUST keep this bcopy as m_data after m_adj
1320 * for IP header aligment is on half-word
1321 * and not word alignment.
1323 bcopy(&data, (uint8_t *)(sc->atse_rx_m->m_data +
1324 sc->atse_rx_buf_len), sizeof(data));
1325 if (meta & A_ONCHIP_FIFO_MEM_CORE_EOP) {
1329 A_ONCHIP_FIFO_MEM_CORE_EMPTY_MASK) >>
1330 A_ONCHIP_FIFO_MEM_CORE_EMPTY_SHIFT;
1331 sc->atse_rx_buf_len += (4 - empty);
1337 m->m_pkthdr.len = m->m_len =
1338 sc->atse_rx_buf_len;
1339 sc->atse_rx_m = NULL;
1341 sc->atse_rx_buf_len = 0;
1342 sc->atse_flags &= ~ATSE_FLAGS_SOP_SEEN;
1343 if (sc->atse_flags & ATSE_FLAGS_ERROR) {
1344 sc->atse_flags &= ~ATSE_FLAGS_ERROR;
1347 m->m_pkthdr.rcvif = ifp;
1349 (*ifp->if_input)(ifp, m);
1352 #ifdef DEVICE_POLLING
1353 if (ifp->if_capenable & IFCAP_POLLING) {
1354 if (sc->atse_rx_cycles <= 0)
1356 sc->atse_rx_cycles--;
1359 goto outer; /* Need a new mbuf. */
1361 sc->atse_rx_buf_len += sizeof(data);
1365 /* XXX-BZ could optimize in case of another packet waiting. */
1373 * Report current media status.
1376 atse_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1378 struct atse_softc *sc;
1379 struct mii_data *mii;
1384 mii = device_get_softc(sc->atse_miibus);
1386 ifmr->ifm_active = mii->mii_media_active;
1387 ifmr->ifm_status = mii->mii_media_status;
1392 atse_rx_intr(void *arg)
1394 struct atse_softc *sc;
1398 sc = (struct atse_softc *)arg;
1402 #ifdef DEVICE_POLLING
1403 if (ifp->if_capenable & IFCAP_POLLING) {
1409 atse_intr_debug(sc, "rx");
1410 rxe = ATSE_RX_EVENT_READ(sc);
1411 if (rxe & (A_ONCHIP_FIFO_MEM_CORE_EVENT_OVERFLOW|
1412 A_ONCHIP_FIFO_MEM_CORE_EVENT_UNDERFLOW)) {
1413 /* XXX-BZ ERROR HANDLING. */
1414 atse_update_rx_err(sc, ((rxe &
1415 A_ONCHIP_FIFO_MEM_CORE_ERROR_MASK) >>
1416 A_ONCHIP_FIFO_MEM_CORE_ERROR_SHIFT) & 0xff);
1421 * There is considerable subtlety in the race-free handling of rx
1422 * interrupts: we must disable interrupts whenever we manipulate the
1423 * FIFO to prevent further interrupts from firing before we are done;
1424 * we must clear the event after processing to prevent the event from
1425 * being immediately reposted due to data remaining; we must clear the
1426 * event mask before reenabling interrupts or risk missing a positive
1427 * edge; and we must recheck everything after completing in case the
1428 * event posted between clearing events and reenabling interrupts. If
1429 * a race is experienced, we must restart the whole mechanism.
1432 ATSE_RX_INTR_DISABLE(sc);
1434 sc->atse_rx_cycles = RX_CYCLES_IN_INTR;
1437 ATSE_RX_EVENT_CLEAR(sc);
1439 /* Disable interrupts if interface is down. */
1440 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1441 ATSE_RX_INTR_ENABLE(sc);
1442 } while (!(ATSE_RX_STATUS_READ(sc) &
1443 A_ONCHIP_FIFO_MEM_CORE_STATUS_EMPTY));
1449 atse_tx_intr(void *arg)
1451 struct atse_softc *sc;
1455 sc = (struct atse_softc *)arg;
1459 #ifdef DEVICE_POLLING
1460 if (ifp->if_capenable & IFCAP_POLLING) {
1466 /* XXX-BZ build histogram. */
1467 atse_intr_debug(sc, "tx");
1468 txe = ATSE_TX_EVENT_READ(sc);
1469 if (txe & (A_ONCHIP_FIFO_MEM_CORE_EVENT_OVERFLOW|
1470 A_ONCHIP_FIFO_MEM_CORE_EVENT_UNDERFLOW)) {
1471 /* XXX-BZ ERROR HANDLING. */
1476 * There is also considerable subtlety in the race-free handling of
1477 * tx interrupts: all processing occurs with interrupts disabled to
1478 * prevent spurious refiring while transmit is in progress (which
1479 * could occur if the FIFO drains while sending -- quite likely); we
1480 * must not clear the event mask until after we've sent, also to
1481 * prevent spurious refiring; once we've cleared the event mask we can
1482 * reenable interrupts, but there is a possible race between clear and
1483 * enable, so we must recheck and potentially repeat the whole process
1484 * if it is detected.
1487 ATSE_TX_INTR_DISABLE(sc);
1488 sc->atse_watchdog_timer = 0;
1489 atse_start_locked(ifp);
1490 ATSE_TX_EVENT_CLEAR(sc);
1492 /* Disable interrupts if interface is down. */
1493 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1494 ATSE_TX_INTR_ENABLE(sc);
1495 } while (ATSE_TX_PENDING(sc) &&
1496 !(ATSE_TX_STATUS_READ(sc) & A_ONCHIP_FIFO_MEM_CORE_STATUS_FULL));
1500 #ifdef DEVICE_POLLING
1502 atse_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1504 struct atse_softc *sc;
1509 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1514 sc->atse_rx_cycles = count;
1515 rx_npkts = atse_rx_locked(sc);
1516 atse_start_locked(ifp);
1518 if (sc->atse_rx_cycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
1521 rx = ATSE_RX_EVENT_READ(sc);
1522 tx = ATSE_TX_EVENT_READ(sc);
1524 if (rx & (A_ONCHIP_FIFO_MEM_CORE_EVENT_OVERFLOW|
1525 A_ONCHIP_FIFO_MEM_CORE_EVENT_UNDERFLOW)) {
1526 /* XXX-BZ ERROR HANDLING. */
1527 atse_update_rx_err(sc, ((rx &
1528 A_ONCHIP_FIFO_MEM_CORE_ERROR_MASK) >>
1529 A_ONCHIP_FIFO_MEM_CORE_ERROR_SHIFT) & 0xff);
1532 if (tx & (A_ONCHIP_FIFO_MEM_CORE_EVENT_OVERFLOW|
1533 A_ONCHIP_FIFO_MEM_CORE_EVENT_UNDERFLOW)) {
1534 /* XXX-BZ ERROR HANDLING. */
1537 if (ATSE_TX_READ_FILL_LEVEL(sc) == 0)
1538 sc->atse_watchdog_timer = 0;
1541 if (/* Severe error; if only we could find out. */) {
1542 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1543 atse_init_locked(sc);
1551 #endif /* DEVICE_POLLING */
1553 static struct atse_mac_stats_regs {
1555 const char *descr; /* Mostly copied from Altera datasheet. */
1556 } atse_mac_stats_regs[] = {
1558 { "aFramesTransmittedOK",
1559 "The number of frames that are successfully transmitted including "
1560 "the pause frames." },
1561 { "aFramesReceivedOK",
1562 "The number of frames that are successfully received including the "
1564 { "aFrameCheckSequenceErrors",
1565 "The number of receive frames with CRC error." },
1566 { "aAlignmentErrors",
1567 "The number of receive frames with alignment error." },
1568 { "aOctetsTransmittedOK",
1569 "The lower 32 bits of the number of data and padding octets that "
1570 "are successfully transmitted." },
1571 { "aOctetsReceivedOK",
1572 "The lower 32 bits of the number of data and padding octets that "
1573 " are successfully received." },
1574 { "aTxPAUSEMACCtrlFrames",
1575 "The number of pause frames transmitted." },
1576 { "aRxPAUSEMACCtrlFrames",
1577 "The number received pause frames received." },
1579 "The number of errored frames received." },
1581 "The number of transmit frames with either a FIFO overflow error, "
1582 "a FIFO underflow error, or a error defined by the user "
1585 "The number of valid unicast frames received." },
1586 { "ifInMulticastPkts",
1587 "The number of valid multicast frames received. The count does "
1588 "not include pause frames." },
1589 { "ifInBroadcastPkts",
1590 "The number of valid broadcast frames received." },
1592 "This statistics counter is not in use. The MAC function does not "
1593 "discard frames that are written to the FIFO buffer by the user "
1596 "The number of valid unicast frames transmitted." },
1597 { "ifOutMulticastPkts",
1598 "The number of valid multicast frames transmitted, excluding pause "
1600 { "ifOutBroadcastPkts",
1601 "The number of valid broadcast frames transmitted." },
1602 { "etherStatsDropEvents",
1603 "The number of frames that are dropped due to MAC internal errors "
1604 "when FIFO buffer overflow persists." },
1605 { "etherStatsOctets",
1606 "The lower 32 bits of the total number of octets received. This "
1607 "count includes both good and errored frames." },
1609 "The total number of good and errored frames received." },
1610 { "etherStatsUndersizePkts",
1611 "The number of frames received with length less than 64 bytes. "
1612 "This count does not include errored frames." },
1613 { "etherStatsOversizePkts",
1614 "The number of frames received that are longer than the value "
1615 "configured in the frm_length register. This count does not "
1616 "include errored frames." },
1617 { "etherStatsPkts64Octets",
1618 "The number of 64-byte frames received. This count includes good "
1619 "and errored frames." },
1620 { "etherStatsPkts65to127Octets",
1621 "The number of received good and errored frames between the length "
1622 "of 65 and 127 bytes." },
1623 { "etherStatsPkts128to255Octets",
1624 "The number of received good and errored frames between the length "
1625 "of 128 and 255 bytes." },
1626 { "etherStatsPkts256to511Octets",
1627 "The number of received good and errored frames between the length "
1628 "of 256 and 511 bytes." },
1629 { "etherStatsPkts512to1023Octets",
1630 "The number of received good and errored frames between the length "
1631 "of 512 and 1023 bytes." },
1632 { "etherStatsPkts1024to1518Octets",
1633 "The number of received good and errored frames between the length "
1634 "of 1024 and 1518 bytes." },
1635 { "etherStatsPkts1519toXOctets",
1636 "The number of received good and errored frames between the length "
1637 "of 1519 and the maximum frame length configured in the frm_length "
1639 { "etherStatsJabbers",
1640 "Too long frames with CRC error." },
1641 { "etherStatsFragments",
1642 "Too short frames with CRC error." },
1643 /* 0x39 unused, 0x3a/b non-stats. */
1645 /* Extended Statistics Counters */
1646 { "msb_aOctetsTransmittedOK",
1647 "Upper 32 bits of the number of data and padding octets that are "
1648 "successfully transmitted." },
1649 { "msb_aOctetsReceivedOK",
1650 "Upper 32 bits of the number of data and padding octets that are "
1651 "successfully received." },
1652 { "msb_etherStatsOctets",
1653 "Upper 32 bits of the total number of octets received. This count "
1654 "includes both good and errored frames." }
1658 sysctl_atse_mac_stats_proc(SYSCTL_HANDLER_ARGS)
1660 struct atse_softc *sc;
1661 int error, offset, s;
1666 s = CSR_READ_4(sc, offset);
1667 error = sysctl_handle_int(oidp, &s, 0, req);
1668 if (error || !req->newptr)
1674 static struct atse_rx_err_stats_regs {
1677 } atse_rx_err_stats_regs[] = {
1679 #define ATSE_RX_ERR_FIFO_THRES_EOP 0 /* FIFO threshold reached, on EOP. */
1680 #define ATSE_RX_ERR_ELEN 1 /* Frame/payload length not valid. */
1681 #define ATSE_RX_ERR_CRC32 2 /* CRC-32 error. */
1682 #define ATSE_RX_ERR_FIFO_THRES_TRUNC 3 /* FIFO thresh., truncated frame. */
1683 #define ATSE_RX_ERR_4 4 /* ? */
1684 #define ATSE_RX_ERR_5 5 /* / */
1686 { "rx_err_fifo_thres_eop",
1687 "FIFO threshold reached, reported on EOP." },
1688 { "rx_err_fifo_elen",
1689 "Frame or payload length not valid." },
1690 { "rx_err_fifo_crc32",
1692 { "rx_err_fifo_thres_trunc",
1693 "FIFO threshold reached, truncated frame" },
1701 sysctl_atse_rx_err_stats_proc(SYSCTL_HANDLER_ARGS)
1703 struct atse_softc *sc;
1704 int error, offset, s;
1709 s = sc->atse_rx_err[offset];
1710 error = sysctl_handle_int(oidp, &s, 0, req);
1711 if (error || !req->newptr)
1718 atse_sysctl_stats_attach(device_t dev)
1720 struct sysctl_ctx_list *sctx;
1721 struct sysctl_oid *soid;
1722 struct atse_softc *sc;
1725 sc = device_get_softc(dev);
1726 sctx = device_get_sysctl_ctx(dev);
1727 soid = device_get_sysctl_tree(dev);
1729 /* MAC statistics. */
1730 for (i = 0; i < sizeof(atse_mac_stats_regs) /
1731 sizeof(*atse_mac_stats_regs); i++) {
1732 if (atse_mac_stats_regs[i].name == NULL ||
1733 atse_mac_stats_regs[i].descr == NULL)
1736 SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
1737 atse_mac_stats_regs[i].name, CTLTYPE_UINT|CTLFLAG_RD,
1738 sc, i, sysctl_atse_mac_stats_proc, "IU",
1739 atse_mac_stats_regs[i].descr);
1743 for (i = 0; i < ATSE_RX_ERR_MAX; i++) {
1744 if (atse_rx_err_stats_regs[i].name == NULL ||
1745 atse_rx_err_stats_regs[i].descr == NULL)
1748 SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
1749 atse_rx_err_stats_regs[i].name, CTLTYPE_UINT|CTLFLAG_RD,
1750 sc, i, sysctl_atse_rx_err_stats_proc, "IU",
1751 atse_rx_err_stats_regs[i].descr);
1756 * Generic device handling routines.
1759 atse_attach(device_t dev)
1761 struct atse_softc *sc;
1765 sc = device_get_softc(dev);
1767 atse_ethernet_option_bits_read(dev);
1769 mtx_init(&sc->atse_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1772 callout_init_mtx(&sc->atse_tick, &sc->atse_mtx, 0);
1774 sc->atse_tx_buf = malloc(ETHER_MAX_LEN_JUMBO, M_DEVBUF, M_WAITOK);
1777 * We are only doing single-PHY with this driver currently. The
1778 * defaults would be right so that BASE_CFG_MDIO_ADDR0 points to the
1779 * 1st PHY address (0) apart from the fact that BMCR0 is always
1780 * the PCS mapping, so we always use BMCR1. See Table 5-1 0xA0-0xBF.
1782 #if 0 /* Always PCS. */
1783 sc->atse_bmcr0 = MDIO_0_START;
1784 CSR_WRITE_4(sc, BASE_CFG_MDIO_ADDR0, 0x00);
1786 /* Always use matching PHY for atse[0..]. */
1787 sc->atse_phy_addr = device_get_unit(dev);
1788 sc->atse_bmcr1 = MDIO_1_START;
1789 CSR_WRITE_4(sc, BASE_CFG_MDIO_ADDR1, sc->atse_phy_addr);
1791 /* Reset the adapter. */
1794 /* Setup interface. */
1795 ifp = sc->atse_ifp = if_alloc(IFT_ETHER);
1797 device_printf(dev, "if_alloc() failed\n");
1802 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1803 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1804 ifp->if_ioctl = atse_ioctl;
1805 ifp->if_start = atse_start;
1806 ifp->if_init = atse_init;
1807 IFQ_SET_MAXLEN(&ifp->if_snd, ATSE_TX_LIST_CNT - 1);
1808 ifp->if_snd.ifq_drv_maxlen = ATSE_TX_LIST_CNT - 1;
1809 IFQ_SET_READY(&ifp->if_snd);
1812 error = mii_attach(dev, &sc->atse_miibus, ifp, atse_ifmedia_upd,
1813 atse_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
1815 device_printf(dev, "attaching PHY failed: %d\n", error);
1819 /* Call media-indepedent attach routine. */
1820 ether_ifattach(ifp, sc->atse_eth_addr);
1822 /* Tell the upper layer(s) about vlan mtu support. */
1823 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1824 ifp->if_capabilities |= IFCAP_VLAN_MTU;
1825 ifp->if_capenable = ifp->if_capabilities;
1826 #ifdef DEVICE_POLLING
1827 /* We will enable polling by default if no irqs available. See below. */
1828 ifp->if_capabilities |= IFCAP_POLLING;
1831 /* Hook up interrupts. */
1832 if (sc->atse_rx_irq_res != NULL) {
1833 error = bus_setup_intr(dev, sc->atse_rx_irq_res, INTR_TYPE_NET |
1834 INTR_MPSAFE, NULL, atse_rx_intr, sc, &sc->atse_rx_intrhand);
1836 device_printf(dev, "enabling RX IRQ failed\n");
1837 ether_ifdetach(ifp);
1842 if (sc->atse_tx_irq_res != NULL) {
1843 error = bus_setup_intr(dev, sc->atse_tx_irq_res, INTR_TYPE_NET |
1844 INTR_MPSAFE, NULL, atse_tx_intr, sc, &sc->atse_tx_intrhand);
1846 bus_teardown_intr(dev, sc->atse_rx_irq_res,
1847 sc->atse_rx_intrhand);
1848 device_printf(dev, "enabling TX IRQ failed\n");
1849 ether_ifdetach(ifp);
1854 if ((ifp->if_capenable & IFCAP_POLLING) != 0 ||
1855 (sc->atse_rx_irq_res == NULL && sc->atse_tx_irq_res == NULL)) {
1856 #ifdef DEVICE_POLLING
1857 /* If not on and no IRQs force it on. */
1858 if (sc->atse_rx_irq_res == NULL && sc->atse_tx_irq_res == NULL){
1859 ifp->if_capenable |= IFCAP_POLLING;
1860 device_printf(dev, "forcing to polling due to no "
1863 error = ether_poll_register(atse_poll, ifp);
1867 device_printf(dev, "no DEVICE_POLLING in kernel and no IRQs\n");
1871 ATSE_RX_INTR_ENABLE(sc);
1872 ATSE_TX_INTR_ENABLE(sc);
1880 atse_sysctl_stats_attach(dev);
1886 atse_detach(device_t dev)
1888 struct atse_softc *sc;
1891 sc = device_get_softc(dev);
1892 KASSERT(mtx_initialized(&sc->atse_mtx), ("%s: mutex not initialized",
1893 device_get_nameunit(dev)));
1896 #ifdef DEVICE_POLLING
1897 if (ifp->if_capenable & IFCAP_POLLING)
1898 ether_poll_deregister(ifp);
1901 /* Only cleanup if attach succeeded. */
1902 if (device_is_attached(dev)) {
1904 atse_stop_locked(sc);
1906 callout_drain(&sc->atse_tick);
1907 ether_ifdetach(ifp);
1909 if (sc->atse_miibus != NULL)
1910 device_delete_child(dev, sc->atse_miibus);
1912 if (sc->atse_tx_intrhand)
1913 bus_teardown_intr(dev, sc->atse_tx_irq_res,
1914 sc->atse_tx_intrhand);
1915 if (sc->atse_rx_intrhand)
1916 bus_teardown_intr(dev, sc->atse_rx_irq_res,
1917 sc->atse_rx_intrhand);
1922 if (sc->atse_tx_buf != NULL)
1923 free(sc->atse_tx_buf, M_DEVBUF);
1925 mtx_destroy(&sc->atse_mtx);
1930 /* Shared between nexus and fdt implementation. */
1932 atse_detach_resources(device_t dev)
1934 struct atse_softc *sc;
1936 sc = device_get_softc(dev);
1938 if (sc->atse_txc_mem_res != NULL) {
1939 bus_release_resource(dev, SYS_RES_MEMORY, sc->atse_txc_mem_rid,
1940 sc->atse_txc_mem_res);
1941 sc->atse_txc_mem_res = NULL;
1943 if (sc->atse_tx_mem_res != NULL) {
1944 bus_release_resource(dev, SYS_RES_MEMORY, sc->atse_tx_mem_rid,
1945 sc->atse_tx_mem_res);
1946 sc->atse_tx_mem_res = NULL;
1948 if (sc->atse_tx_irq_res != NULL) {
1949 bus_release_resource(dev, SYS_RES_IRQ, sc->atse_tx_irq_rid,
1950 sc->atse_tx_irq_res);
1951 sc->atse_tx_irq_res = NULL;
1953 if (sc->atse_rxc_mem_res != NULL) {
1954 bus_release_resource(dev, SYS_RES_MEMORY, sc->atse_rxc_mem_rid,
1955 sc->atse_rxc_mem_res);
1956 sc->atse_rxc_mem_res = NULL;
1958 if (sc->atse_rx_mem_res != NULL) {
1959 bus_release_resource(dev, SYS_RES_MEMORY, sc->atse_rx_mem_rid,
1960 sc->atse_rx_mem_res);
1961 sc->atse_rx_mem_res = NULL;
1963 if (sc->atse_rx_irq_res != NULL) {
1964 bus_release_resource(dev, SYS_RES_IRQ, sc->atse_rx_irq_rid,
1965 sc->atse_rx_irq_res);
1966 sc->atse_rx_irq_res = NULL;
1968 if (sc->atse_mem_res != NULL) {
1969 bus_release_resource(dev, SYS_RES_MEMORY, sc->atse_mem_rid,
1971 sc->atse_mem_res = NULL;
1976 atse_detach_dev(device_t dev)
1980 error = atse_detach(dev);
1982 /* We are basically in undefined state now. */
1983 device_printf(dev, "atse_detach() failed: %d\n", error);
1987 atse_detach_resources(dev);
1993 atse_miibus_readreg(device_t dev, int phy, int reg)
1995 struct atse_softc *sc;
1997 sc = device_get_softc(dev);
2000 * We currently do not support re-mapping of MDIO space on-the-fly
2001 * but de-facto hard-code the phy#.
2003 if (phy != sc->atse_phy_addr)
2006 return (PHY_READ_2(sc, reg));
2010 atse_miibus_writereg(device_t dev, int phy, int reg, int data)
2012 struct atse_softc *sc;
2014 sc = device_get_softc(dev);
2017 * We currently do not support re-mapping of MDIO space on-the-fly
2018 * but de-facto hard-code the phy#.
2020 if (phy != sc->atse_phy_addr)
2023 PHY_WRITE_2(sc, reg, data);
2028 atse_miibus_statchg(device_t dev)
2030 struct atse_softc *sc;
2031 struct mii_data *mii;
2035 sc = device_get_softc(dev);
2036 ATSE_LOCK_ASSERT(sc);
2038 mii = device_get_softc(sc->atse_miibus);
2040 if (mii == NULL || ifp == NULL ||
2041 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2044 val4 = CSR_READ_4(sc, BASE_CFG_COMMAND_CONFIG);
2046 /* Assume no link. */
2047 sc->atse_flags &= ~ATSE_FLAGS_LINK;
2049 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
2050 (IFM_ACTIVE | IFM_AVALID)) {
2052 switch (IFM_SUBTYPE(mii->mii_media_active)) {
2054 val4 |= BASE_CFG_COMMAND_CONFIG_ENA_10;
2055 val4 &= ~BASE_CFG_COMMAND_CONFIG_ETH_SPEED;
2056 sc->atse_flags |= ATSE_FLAGS_LINK;
2059 val4 &= ~BASE_CFG_COMMAND_CONFIG_ENA_10;
2060 val4 &= ~BASE_CFG_COMMAND_CONFIG_ETH_SPEED;
2061 sc->atse_flags |= ATSE_FLAGS_LINK;
2064 val4 &= ~BASE_CFG_COMMAND_CONFIG_ENA_10;
2065 val4 |= BASE_CFG_COMMAND_CONFIG_ETH_SPEED;
2066 sc->atse_flags |= ATSE_FLAGS_LINK;
2073 if ((sc->atse_flags & ATSE_FLAGS_LINK) == 0) {
2074 /* XXX-BZ need to stop the MAC? */
2078 if (IFM_OPTIONS(mii->mii_media_active & IFM_FDX) != 0)
2079 val4 &= ~BASE_CFG_COMMAND_CONFIG_HD_ENA;
2081 val4 |= BASE_CFG_COMMAND_CONFIG_HD_ENA;
2082 /* XXX-BZ flow control? */
2084 /* Make sure the MAC is activated. */
2085 val4 |= BASE_CFG_COMMAND_CONFIG_TX_ENA;
2086 val4 |= BASE_CFG_COMMAND_CONFIG_RX_ENA;
2088 CSR_WRITE_4(sc, BASE_CFG_COMMAND_CONFIG, val4);