2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright 2008 Nathan Whitehorn. All rights reserved.
5 * Copyright 2003 by Peter Grehan. All rights reserved.
6 * Copyright (C) 1998, 1999, 2000 Tsubai Masanari. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * 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 * NetBSD: if_bm.c,v 1.9.2.1 2000/11/01 15:02:49 tv Exp
36 * BMAC/BMAC+ Macio cell 10/100 ethernet driver
37 * The low-cost, low-feature Apple variant of the Sun HME
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sockio.h>
46 #include <sys/endian.h>
48 #include <sys/module.h>
49 #include <sys/malloc.h>
50 #include <sys/kernel.h>
51 #include <sys/socket.h>
55 #include <net/if_var.h>
56 #include <net/if_arp.h>
57 #include <net/ethernet.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
60 #include <net/if_types.h>
62 #include <machine/pio.h>
63 #include <machine/bus.h>
64 #include <machine/resource.h>
68 #include <dev/mii/mii.h>
69 #include <dev/mii/mii_bitbang.h>
70 #include <dev/mii/miivar.h>
72 #include <dev/ofw/ofw_bus.h>
73 #include <dev/ofw/openfirm.h>
74 #include <machine/dbdma.h>
76 MODULE_DEPEND(bm, ether, 1, 1, 1);
77 MODULE_DEPEND(bm, miibus, 1, 1, 1);
79 /* "controller miibus0" required. See GENERIC if you get errors here. */
80 #include "miibus_if.h"
85 static int bm_probe (device_t);
86 static int bm_attach (device_t);
87 static int bm_detach (device_t);
88 static int bm_shutdown (device_t);
90 static void bm_start (struct ifnet *);
91 static void bm_start_locked (struct ifnet *);
92 static int bm_encap (struct bm_softc *sc, struct mbuf **m_head);
93 static int bm_ioctl (struct ifnet *, u_long, caddr_t);
94 static void bm_init (void *);
95 static void bm_init_locked (struct bm_softc *sc);
96 static void bm_chip_setup (struct bm_softc *sc);
97 static void bm_stop (struct bm_softc *sc);
98 static void bm_setladrf (struct bm_softc *sc);
99 static void bm_dummypacket (struct bm_softc *sc);
100 static void bm_txintr (void *xsc);
101 static void bm_rxintr (void *xsc);
103 static int bm_add_rxbuf (struct bm_softc *sc, int i);
104 static int bm_add_rxbuf_dma (struct bm_softc *sc, int i);
105 static void bm_enable_interrupts (struct bm_softc *sc);
106 static void bm_disable_interrupts (struct bm_softc *sc);
107 static void bm_tick (void *xsc);
109 static int bm_ifmedia_upd (struct ifnet *);
110 static void bm_ifmedia_sts (struct ifnet *, struct ifmediareq *);
112 static int bm_miibus_readreg (device_t, int, int);
113 static int bm_miibus_writereg (device_t, int, int, int);
114 static void bm_miibus_statchg (device_t);
119 static uint32_t bm_mii_bitbang_read(device_t);
120 static void bm_mii_bitbang_write(device_t, uint32_t);
122 static const struct mii_bitbang_ops bm_mii_bitbang_ops = {
124 bm_mii_bitbang_write,
126 BM_MII_DATAOUT, /* MII_BIT_MDO */
127 BM_MII_DATAIN, /* MII_BIT_MDI */
128 BM_MII_CLK, /* MII_BIT_MDC */
129 BM_MII_OENABLE, /* MII_BIT_DIR_HOST_PHY */
130 0, /* MII_BIT_DIR_PHY_HOST */
134 static device_method_t bm_methods[] = {
135 /* Device interface */
136 DEVMETHOD(device_probe, bm_probe),
137 DEVMETHOD(device_attach, bm_attach),
138 DEVMETHOD(device_detach, bm_detach),
139 DEVMETHOD(device_shutdown, bm_shutdown),
142 DEVMETHOD(miibus_readreg, bm_miibus_readreg),
143 DEVMETHOD(miibus_writereg, bm_miibus_writereg),
144 DEVMETHOD(miibus_statchg, bm_miibus_statchg),
149 static driver_t bm_macio_driver = {
152 sizeof(struct bm_softc)
155 static devclass_t bm_devclass;
157 DRIVER_MODULE(bm, macio, bm_macio_driver, bm_devclass, 0, 0);
158 DRIVER_MODULE(miibus, bm, miibus_driver, miibus_devclass, 0, 0);
161 * MII internal routines
165 * Write the MII serial port for the MII bit-bang module.
168 bm_mii_bitbang_write(device_t dev, uint32_t val)
172 sc = device_get_softc(dev);
174 CSR_WRITE_2(sc, BM_MII_CSR, val);
175 CSR_BARRIER(sc, BM_MII_CSR, 2,
176 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
180 * Read the MII serial port for the MII bit-bang module.
183 bm_mii_bitbang_read(device_t dev)
188 sc = device_get_softc(dev);
190 reg = CSR_READ_2(sc, BM_MII_CSR);
191 CSR_BARRIER(sc, BM_MII_CSR, 2,
192 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
201 bm_miibus_readreg(device_t dev, int phy, int reg)
204 return (mii_bitbang_readreg(dev, &bm_mii_bitbang_ops, phy, reg));
208 bm_miibus_writereg(device_t dev, int phy, int reg, int data)
211 mii_bitbang_readreg(dev, &bm_mii_bitbang_ops, phy, reg);
217 bm_miibus_statchg(device_t dev)
219 struct bm_softc *sc = device_get_softc(dev);
223 reg = CSR_READ_2(sc, BM_TX_CONFIG);
224 new_duplex = IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX;
226 if (new_duplex != sc->sc_duplex) {
227 /* Turn off TX MAC while we fiddle its settings */
230 CSR_WRITE_2(sc, BM_TX_CONFIG, reg);
231 while (CSR_READ_2(sc, BM_TX_CONFIG) & BM_ENABLE)
235 if (new_duplex && !sc->sc_duplex)
236 reg |= BM_TX_IGNORECOLL | BM_TX_FULLDPX;
237 else if (!new_duplex && sc->sc_duplex)
238 reg &= ~(BM_TX_IGNORECOLL | BM_TX_FULLDPX);
240 if (new_duplex != sc->sc_duplex) {
241 /* Turn TX MAC back on */
244 CSR_WRITE_2(sc, BM_TX_CONFIG, reg);
245 sc->sc_duplex = new_duplex;
250 * ifmedia/mii callbacks
253 bm_ifmedia_upd(struct ifnet *ifp)
255 struct bm_softc *sc = ifp->if_softc;
259 error = mii_mediachg(sc->sc_mii);
265 bm_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifm)
267 struct bm_softc *sc = ifp->if_softc;
270 mii_pollstat(sc->sc_mii);
271 ifm->ifm_active = sc->sc_mii->mii_media_active;
272 ifm->ifm_status = sc->sc_mii->mii_media_status;
280 bm_probe(device_t dev)
282 const char *dname = ofw_bus_get_name(dev);
283 const char *dcompat = ofw_bus_get_compat(dev);
286 * BMAC+ cells have a name of "ethernet" and
287 * a compatible property of "bmac+"
289 if (strcmp(dname, "bmac") == 0) {
290 device_set_desc(dev, "Apple BMAC Ethernet Adaptor");
291 } else if (strcmp(dcompat, "bmac+") == 0) {
292 device_set_desc(dev, "Apple BMAC+ Ethernet Adaptor");
300 bm_attach(device_t dev)
305 int error, cellid, i;
306 struct bm_txsoft *txs;
307 struct bm_softc *sc = device_get_softc(dev);
309 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
312 sc->sc_duplex = ~IFM_FDX;
315 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
317 callout_init_mtx(&sc->sc_tick_ch, &sc->sc_mtx, 0);
319 /* Check for an improved version of Paddington */
320 sc->sc_streaming = 0;
322 node = ofw_bus_get_node(dev);
324 OF_getprop(node, "cell-id", &cellid, sizeof(cellid));
326 sc->sc_streaming = 1;
329 sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
330 &sc->sc_memrid, RF_ACTIVE);
331 if (sc->sc_memr == NULL) {
332 device_printf(dev, "Could not alloc chip registers!\n");
336 sc->sc_txdmarid = BM_TXDMA_REGISTERS;
337 sc->sc_rxdmarid = BM_RXDMA_REGISTERS;
339 sc->sc_txdmar = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
340 &sc->sc_txdmarid, RF_ACTIVE);
341 sc->sc_rxdmar = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
342 &sc->sc_rxdmarid, RF_ACTIVE);
344 if (sc->sc_txdmar == NULL || sc->sc_rxdmar == NULL) {
345 device_printf(dev, "Could not map DBDMA registers!\n");
349 error = dbdma_allocate_channel(sc->sc_txdmar, 0, bus_get_dma_tag(dev),
350 BM_MAX_DMA_COMMANDS, &sc->sc_txdma);
351 error += dbdma_allocate_channel(sc->sc_rxdmar, 0, bus_get_dma_tag(dev),
352 BM_MAX_DMA_COMMANDS, &sc->sc_rxdma);
355 device_printf(dev,"Could not allocate DBDMA channel!\n");
359 /* alloc DMA tags and buffers */
360 error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
361 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
362 BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, NULL,
363 NULL, &sc->sc_pdma_tag);
366 device_printf(dev,"Could not allocate DMA tag!\n");
370 error = bus_dma_tag_create(sc->sc_pdma_tag, 1, 0, BUS_SPACE_MAXADDR,
371 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES,
372 BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_rdma_tag);
375 device_printf(dev,"Could not allocate RX DMA channel!\n");
379 error = bus_dma_tag_create(sc->sc_pdma_tag, 1, 0, BUS_SPACE_MAXADDR,
380 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * BM_NTXSEGS, BM_NTXSEGS,
381 MCLBYTES, BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_tdma_tag);
384 device_printf(dev,"Could not allocate TX DMA tag!\n");
388 /* init transmit descriptors */
389 STAILQ_INIT(&sc->sc_txfreeq);
390 STAILQ_INIT(&sc->sc_txdirtyq);
392 /* create TX DMA maps */
394 for (i = 0; i < BM_MAX_TX_PACKETS; i++) {
395 txs = &sc->sc_txsoft[i];
396 txs->txs_mbuf = NULL;
397 error = bus_dmamap_create(sc->sc_tdma_tag, 0, &txs->txs_dmamap);
399 device_printf(sc->sc_dev,
400 "unable to create TX DMA map %d, error = %d\n",
403 STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
406 /* Create the receive buffer DMA maps. */
407 for (i = 0; i < BM_MAX_RX_PACKETS; i++) {
408 error = bus_dmamap_create(sc->sc_rdma_tag, 0,
409 &sc->sc_rxsoft[i].rxs_dmamap);
411 device_printf(sc->sc_dev,
412 "unable to create RX DMA map %d, error = %d\n",
415 sc->sc_rxsoft[i].rxs_mbuf = NULL;
418 /* alloc interrupt */
419 bm_disable_interrupts(sc);
421 sc->sc_txdmairqid = BM_TXDMA_INTERRUPT;
422 sc->sc_txdmairq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
423 &sc->sc_txdmairqid, RF_ACTIVE);
426 device_printf(dev,"Could not allocate TX interrupt!\n");
430 bus_setup_intr(dev,sc->sc_txdmairq,
431 INTR_TYPE_MISC | INTR_MPSAFE | INTR_ENTROPY, NULL, bm_txintr, sc,
434 sc->sc_rxdmairqid = BM_RXDMA_INTERRUPT;
435 sc->sc_rxdmairq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
436 &sc->sc_rxdmairqid, RF_ACTIVE);
439 device_printf(dev,"Could not allocate RX interrupt!\n");
443 bus_setup_intr(dev,sc->sc_rxdmairq,
444 INTR_TYPE_MISC | INTR_MPSAFE | INTR_ENTROPY, NULL, bm_rxintr, sc,
448 * Get the ethernet address from OpenFirmware
450 eaddr = sc->sc_enaddr;
451 OF_getprop(node, "local-mac-address", eaddr, ETHER_ADDR_LEN);
455 * On Apple BMAC controllers, we end up in a weird state of
456 * partially-completed autonegotiation on boot. So we force
457 * autonegotation to try again.
459 error = mii_attach(dev, &sc->sc_miibus, ifp, bm_ifmedia_upd,
460 bm_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY,
463 device_printf(dev, "attaching PHYs failed\n");
467 /* reset the adapter */
470 sc->sc_mii = device_get_softc(sc->sc_miibus);
472 if_initname(ifp, device_get_name(sc->sc_dev),
473 device_get_unit(sc->sc_dev));
474 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
475 ifp->if_start = bm_start;
476 ifp->if_ioctl = bm_ioctl;
477 ifp->if_init = bm_init;
478 IFQ_SET_MAXLEN(&ifp->if_snd, BM_MAX_TX_PACKETS);
479 ifp->if_snd.ifq_drv_maxlen = BM_MAX_TX_PACKETS;
480 IFQ_SET_READY(&ifp->if_snd);
482 /* Attach the interface. */
483 ether_ifattach(ifp, sc->sc_enaddr);
484 ifp->if_hwassist = 0;
490 bm_detach(device_t dev)
492 struct bm_softc *sc = device_get_softc(dev);
498 callout_drain(&sc->sc_tick_ch);
499 ether_ifdetach(sc->sc_ifp);
500 bus_teardown_intr(dev, sc->sc_txdmairq, sc->sc_txihtx);
501 bus_teardown_intr(dev, sc->sc_rxdmairq, sc->sc_rxih);
503 dbdma_free_channel(sc->sc_txdma);
504 dbdma_free_channel(sc->sc_rxdma);
506 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memr);
507 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_txdmarid,
509 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rxdmarid,
512 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_txdmairqid,
514 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rxdmairqid,
517 mtx_destroy(&sc->sc_mtx);
524 bm_shutdown(device_t dev)
528 sc = device_get_softc(dev);
538 bm_dummypacket(struct bm_softc *sc)
545 MGETHDR(m, M_NOWAIT, MT_DATA);
551 mtod(m, struct ether_header *)->ether_dhost, ETHER_ADDR_LEN);
553 mtod(m, struct ether_header *)->ether_shost, ETHER_ADDR_LEN);
554 mtod(m, struct ether_header *)->ether_type = htons(3);
555 mtod(m, unsigned char *)[14] = 0;
556 mtod(m, unsigned char *)[15] = 0;
557 mtod(m, unsigned char *)[16] = 0xE3;
558 m->m_len = m->m_pkthdr.len = sizeof(struct ether_header) + 3;
559 IF_ENQUEUE(&ifp->if_snd, m);
560 bm_start_locked(ifp);
566 struct bm_softc *sc = xsc;
567 struct ifnet *ifp = sc->sc_ifp;
569 int i, prev_stop, new_stop;
574 status = dbdma_get_chan_status(sc->sc_rxdma);
575 if (status & DBDMA_STATUS_DEAD) {
576 dbdma_reset(sc->sc_rxdma);
580 if (!(status & DBDMA_STATUS_RUN)) {
581 device_printf(sc->sc_dev,"Bad RX Interrupt!\n");
586 prev_stop = sc->next_rxdma_slot - 1;
588 prev_stop = sc->rxdma_loop_slot - 1;
596 dbdma_sync_commands(sc->sc_rxdma, BUS_DMASYNC_POSTREAD);
598 for (i = sc->next_rxdma_slot; i < BM_MAX_RX_PACKETS; i++) {
599 if (i == sc->rxdma_loop_slot)
605 status = dbdma_get_cmd_status(sc->sc_rxdma, i);
610 m = sc->sc_rxsoft[i].rxs_mbuf;
612 if (bm_add_rxbuf(sc, i)) {
613 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
621 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
622 m->m_pkthdr.rcvif = ifp;
623 m->m_len -= (dbdma_get_residuals(sc->sc_rxdma, i) + 2);
624 m->m_pkthdr.len = m->m_len;
626 /* Send up the stack */
628 (*ifp->if_input)(ifp, m);
631 /* Clear all fields on this command */
632 bm_add_rxbuf_dma(sc, i);
637 /* Change the last packet we processed to the ring buffer terminator,
638 * and restore a receive buffer to the old terminator */
640 dbdma_insert_stop(sc->sc_rxdma, new_stop);
641 bm_add_rxbuf_dma(sc, prev_stop);
642 if (i < sc->rxdma_loop_slot)
643 sc->next_rxdma_slot = i;
645 sc->next_rxdma_slot = 0;
647 dbdma_sync_commands(sc->sc_rxdma, BUS_DMASYNC_PREWRITE);
649 dbdma_wake(sc->sc_rxdma);
657 struct bm_softc *sc = xsc;
658 struct ifnet *ifp = sc->sc_ifp;
659 struct bm_txsoft *txs;
664 while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
665 if (!dbdma_get_cmd_status(sc->sc_txdma, txs->txs_lastdesc))
668 STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
669 bus_dmamap_unload(sc->sc_tdma_tag, txs->txs_dmamap);
671 if (txs->txs_mbuf != NULL) {
672 m_freem(txs->txs_mbuf);
673 txs->txs_mbuf = NULL;
676 /* Set the first used TXDMA slot to the location of the
677 * STOP/NOP command associated with this packet. */
679 sc->first_used_txdma_slot = txs->txs_stopdesc;
681 STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
683 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
689 * We freed some descriptors, so reset IFF_DRV_OACTIVE
692 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
693 sc->sc_wdog_timer = STAILQ_EMPTY(&sc->sc_txdirtyq) ? 0 : 5;
695 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) &&
696 !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
697 bm_start_locked(ifp);
704 bm_start(struct ifnet *ifp)
706 struct bm_softc *sc = ifp->if_softc;
709 bm_start_locked(ifp);
714 bm_start_locked(struct ifnet *ifp)
716 struct bm_softc *sc = ifp->if_softc;
717 struct mbuf *mb_head;
722 * We lay out our DBDMA program in the following manner:
725 * OUTPUT_LAST (+ Interrupt)
728 * To extend the channel, we append a new program,
729 * then replace STOP with NOP and wake the channel.
730 * If we stalled on the STOP already, the program proceeds,
731 * if not it will sail through the NOP.
734 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
735 IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head);
740 prev_stop = sc->next_txdma_slot - 1;
742 if (bm_encap(sc, &mb_head)) {
743 /* Put the packet back and stop */
744 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
745 IFQ_DRV_PREPEND(&ifp->if_snd, mb_head);
749 dbdma_insert_nop(sc->sc_txdma, prev_stop);
753 BPF_MTAP(ifp, mb_head);
756 dbdma_sync_commands(sc->sc_txdma, BUS_DMASYNC_PREWRITE);
759 dbdma_wake(sc->sc_txdma);
760 sc->sc_wdog_timer = 5;
765 bm_encap(struct bm_softc *sc, struct mbuf **m_head)
767 bus_dma_segment_t segs[BM_NTXSEGS];
768 struct bm_txsoft *txs;
770 int nsegs = BM_NTXSEGS;
775 /* Limit the command size to the number of free DBDMA slots */
777 if (sc->next_txdma_slot >= sc->first_used_txdma_slot)
778 nsegs = BM_MAX_DMA_COMMANDS - 2 - sc->next_txdma_slot +
779 sc->first_used_txdma_slot; /* -2 for branch and indexing */
781 nsegs = sc->first_used_txdma_slot - sc->next_txdma_slot;
783 /* Remove one slot for the STOP/NOP terminator */
786 if (nsegs > BM_NTXSEGS)
789 /* Get a work queue entry. */
790 if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) {
791 /* Ran out of descriptors. */
795 error = bus_dmamap_load_mbuf_sg(sc->sc_tdma_tag, txs->txs_dmamap,
796 *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
798 if (error == EFBIG) {
799 m = m_collapse(*m_head, M_NOWAIT, nsegs);
807 error = bus_dmamap_load_mbuf_sg(sc->sc_tdma_tag,
808 txs->txs_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
814 } else if (error != 0)
823 txs->txs_ndescs = nsegs;
824 txs->txs_firstdesc = sc->next_txdma_slot;
826 for (i = 0; i < nsegs; i++) {
827 /* Loop back to the beginning if this is our last slot */
828 if (sc->next_txdma_slot == (BM_MAX_DMA_COMMANDS - 1))
829 branch_type = DBDMA_ALWAYS;
831 branch_type = DBDMA_NEVER;
834 txs->txs_lastdesc = sc->next_txdma_slot;
836 dbdma_insert_command(sc->sc_txdma, sc->next_txdma_slot++,
837 (i + 1 < nsegs) ? DBDMA_OUTPUT_MORE : DBDMA_OUTPUT_LAST,
838 0, segs[i].ds_addr, segs[i].ds_len,
839 (i + 1 < nsegs) ? DBDMA_NEVER : DBDMA_ALWAYS,
840 branch_type, DBDMA_NEVER, 0);
842 if (branch_type == DBDMA_ALWAYS)
843 sc->next_txdma_slot = 0;
846 /* We have a corner case where the STOP command is the last slot,
847 * but you can't branch in STOP commands. So add a NOP branch here
848 * and the STOP in slot 0. */
850 if (sc->next_txdma_slot == (BM_MAX_DMA_COMMANDS - 1)) {
851 dbdma_insert_branch(sc->sc_txdma, sc->next_txdma_slot, 0);
852 sc->next_txdma_slot = 0;
855 txs->txs_stopdesc = sc->next_txdma_slot;
856 dbdma_insert_stop(sc->sc_txdma, sc->next_txdma_slot++);
858 STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
859 STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
860 txs->txs_mbuf = *m_head;
866 bm_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
868 struct bm_softc *sc = ifp->if_softc;
869 struct ifreq *ifr = (struct ifreq *)data;
877 if ((ifp->if_flags & IFF_UP) != 0) {
878 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
879 ((ifp->if_flags ^ sc->sc_ifpflags) &
880 (IFF_ALLMULTI | IFF_PROMISC)) != 0)
884 } else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
886 sc->sc_ifpflags = ifp->if_flags;
896 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd);
899 error = ether_ioctl(ifp, cmd, data);
907 bm_setladrf(struct bm_softc *sc)
909 struct ifnet *ifp = sc->sc_ifp;
910 struct ifmultiaddr *inm;
915 reg = BM_CRC_ENABLE | BM_REJECT_OWN_PKTS;
917 /* Turn off RX MAC while we fiddle its settings */
918 CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
919 while (CSR_READ_2(sc, BM_RX_CONFIG) & BM_ENABLE)
922 if ((ifp->if_flags & IFF_PROMISC) != 0) {
925 CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
929 reg = CSR_READ_2(sc, BM_RX_CONFIG);
931 CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
935 if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
936 hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
938 /* Clear the hash table. */
939 memset(hash, 0, sizeof(hash));
942 TAILQ_FOREACH(inm, &ifp->if_multiaddrs, ifma_link) {
943 if (inm->ifma_addr->sa_family != AF_LINK)
945 crc = ether_crc32_le(LLADDR((struct sockaddr_dl *)
946 inm->ifma_addr), ETHER_ADDR_LEN);
948 /* We just want the 6 most significant bits */
951 /* Set the corresponding bit in the filter. */
952 hash[crc >> 4] |= 1 << (crc & 0xf);
954 if_maddr_runlock(ifp);
957 /* Write out new hash table */
958 CSR_WRITE_2(sc, BM_HASHTAB0, hash[0]);
959 CSR_WRITE_2(sc, BM_HASHTAB1, hash[1]);
960 CSR_WRITE_2(sc, BM_HASHTAB2, hash[2]);
961 CSR_WRITE_2(sc, BM_HASHTAB3, hash[3]);
963 /* And turn the RX MAC back on, this time with the hash bit set */
964 reg |= BM_HASH_FILTER_ENABLE;
965 CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
967 while (!(CSR_READ_2(sc, BM_RX_CONFIG) & BM_HASH_FILTER_ENABLE))
970 reg = CSR_READ_2(sc, BM_RX_CONFIG);
972 CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
978 struct bm_softc *sc = xsc;
986 bm_chip_setup(struct bm_softc *sc)
989 uint16_t *eaddr_sect;
991 eaddr_sect = (uint16_t *)(sc->sc_enaddr);
992 dbdma_stop(sc->sc_txdma);
993 dbdma_stop(sc->sc_rxdma);
996 CSR_WRITE_2(sc, BM_RX_RESET, 0x0000);
997 CSR_WRITE_2(sc, BM_TX_RESET, 0x0001);
1000 reg = CSR_READ_2(sc, BM_TX_RESET);
1001 } while (reg & 0x0001);
1003 /* Some random junk. OS X uses the system time. We use
1004 * the low 16 bits of the MAC address. */
1005 CSR_WRITE_2(sc, BM_TX_RANDSEED, eaddr_sect[2]);
1007 /* Enable transmit */
1008 reg = CSR_READ_2(sc, BM_TX_IFC);
1010 CSR_WRITE_2(sc, BM_TX_IFC, reg);
1012 CSR_READ_2(sc, BM_TX_PEAKCNT);
1016 bm_stop(struct bm_softc *sc)
1018 struct bm_txsoft *txs;
1021 /* Disable TX and RX MACs */
1022 reg = CSR_READ_2(sc, BM_TX_CONFIG);
1024 CSR_WRITE_2(sc, BM_TX_CONFIG, reg);
1026 reg = CSR_READ_2(sc, BM_RX_CONFIG);
1028 CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
1032 /* Stop DMA engine */
1033 dbdma_stop(sc->sc_rxdma);
1034 dbdma_stop(sc->sc_txdma);
1035 sc->next_rxdma_slot = 0;
1036 sc->rxdma_loop_slot = 0;
1038 /* Disable interrupts */
1039 bm_disable_interrupts(sc);
1041 /* Don't worry about pending transmits anymore */
1042 while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1043 STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
1044 if (txs->txs_ndescs != 0) {
1045 bus_dmamap_sync(sc->sc_tdma_tag, txs->txs_dmamap,
1046 BUS_DMASYNC_POSTWRITE);
1047 bus_dmamap_unload(sc->sc_tdma_tag, txs->txs_dmamap);
1048 if (txs->txs_mbuf != NULL) {
1049 m_freem(txs->txs_mbuf);
1050 txs->txs_mbuf = NULL;
1053 STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1056 /* And we're down */
1057 sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1058 sc->sc_wdog_timer = 0;
1059 callout_stop(&sc->sc_tick_ch);
1063 bm_init_locked(struct bm_softc *sc)
1066 uint16_t *eaddr_sect;
1067 struct bm_rxsoft *rxs;
1070 eaddr_sect = (uint16_t *)(sc->sc_enaddr);
1072 /* Zero RX slot info and stop DMA */
1073 dbdma_stop(sc->sc_rxdma);
1074 dbdma_stop(sc->sc_txdma);
1075 sc->next_rxdma_slot = 0;
1076 sc->rxdma_loop_slot = 0;
1078 /* Initialize TX/RX DBDMA programs */
1079 dbdma_insert_stop(sc->sc_rxdma, 0);
1080 dbdma_insert_stop(sc->sc_txdma, 0);
1081 dbdma_set_current_cmd(sc->sc_rxdma, 0);
1082 dbdma_set_current_cmd(sc->sc_txdma, 0);
1084 sc->next_rxdma_slot = 0;
1085 sc->next_txdma_slot = 1;
1086 sc->first_used_txdma_slot = 0;
1088 for (i = 0; i < BM_MAX_RX_PACKETS; i++) {
1089 rxs = &sc->sc_rxsoft[i];
1090 rxs->dbdma_slot = i;
1092 if (rxs->rxs_mbuf == NULL) {
1093 bm_add_rxbuf(sc, i);
1095 if (rxs->rxs_mbuf == NULL) {
1096 /* If we can't add anymore, mark the problem */
1097 rxs->dbdma_slot = -1;
1103 bm_add_rxbuf_dma(sc, i);
1107 * Now terminate the RX ring buffer, and follow with the loop to
1110 dbdma_insert_stop(sc->sc_rxdma, i - 1);
1111 dbdma_insert_branch(sc->sc_rxdma, i, 0);
1112 sc->rxdma_loop_slot = i;
1114 /* Now add in the first element of the RX DMA chain */
1115 bm_add_rxbuf_dma(sc, 0);
1117 dbdma_sync_commands(sc->sc_rxdma, BUS_DMASYNC_PREWRITE);
1118 dbdma_sync_commands(sc->sc_txdma, BUS_DMASYNC_PREWRITE);
1120 /* Zero collision counters */
1121 CSR_WRITE_2(sc, BM_TX_NCCNT, 0);
1122 CSR_WRITE_2(sc, BM_TX_FCCNT, 0);
1123 CSR_WRITE_2(sc, BM_TX_EXCNT, 0);
1124 CSR_WRITE_2(sc, BM_TX_LTCNT, 0);
1126 /* Zero receive counters */
1127 CSR_WRITE_2(sc, BM_RX_FRCNT, 0);
1128 CSR_WRITE_2(sc, BM_RX_LECNT, 0);
1129 CSR_WRITE_2(sc, BM_RX_AECNT, 0);
1130 CSR_WRITE_2(sc, BM_RX_FECNT, 0);
1131 CSR_WRITE_2(sc, BM_RXCV, 0);
1133 /* Prime transmit */
1134 CSR_WRITE_2(sc, BM_TX_THRESH, 0xff);
1136 CSR_WRITE_2(sc, BM_TXFIFO_CSR, 0);
1137 CSR_WRITE_2(sc, BM_TXFIFO_CSR, 0x0001);
1140 CSR_WRITE_2(sc, BM_RXFIFO_CSR, 0);
1141 CSR_WRITE_2(sc, BM_RXFIFO_CSR, 0x0001);
1143 /* Clear status reg */
1144 CSR_READ_2(sc, BM_STATUS);
1146 /* Zero hash filters */
1147 CSR_WRITE_2(sc, BM_HASHTAB0, 0);
1148 CSR_WRITE_2(sc, BM_HASHTAB1, 0);
1149 CSR_WRITE_2(sc, BM_HASHTAB2, 0);
1150 CSR_WRITE_2(sc, BM_HASHTAB3, 0);
1152 /* Write MAC address to chip */
1153 CSR_WRITE_2(sc, BM_MACADDR0, eaddr_sect[0]);
1154 CSR_WRITE_2(sc, BM_MACADDR1, eaddr_sect[1]);
1155 CSR_WRITE_2(sc, BM_MACADDR2, eaddr_sect[2]);
1157 /* Final receive engine setup */
1158 reg = BM_CRC_ENABLE | BM_REJECT_OWN_PKTS | BM_HASH_FILTER_ENABLE;
1159 CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
1161 /* Now turn it all on! */
1162 dbdma_reset(sc->sc_rxdma);
1163 dbdma_reset(sc->sc_txdma);
1165 /* Enable RX and TX MACs. Setting the address filter has
1166 * the side effect of enabling the RX MAC. */
1169 reg = CSR_READ_2(sc, BM_TX_CONFIG);
1171 CSR_WRITE_2(sc, BM_TX_CONFIG, reg);
1174 * Enable interrupts, unwedge the controller with a dummy packet,
1175 * and nudge the DMA queue.
1177 bm_enable_interrupts(sc);
1179 dbdma_wake(sc->sc_rxdma); /* Nudge RXDMA */
1181 sc->sc_ifp->if_drv_flags |= IFF_DRV_RUNNING;
1182 sc->sc_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1183 sc->sc_ifpflags = sc->sc_ifp->if_flags;
1185 /* Resync PHY and MAC states */
1186 sc->sc_mii = device_get_softc(sc->sc_miibus);
1187 sc->sc_duplex = ~IFM_FDX;
1188 mii_mediachg(sc->sc_mii);
1190 /* Start the one second timer. */
1191 sc->sc_wdog_timer = 0;
1192 callout_reset(&sc->sc_tick_ch, hz, bm_tick, sc);
1198 struct bm_softc *sc = arg;
1200 /* Read error counters */
1201 if_inc_counter(sc->sc_ifp, IFCOUNTER_COLLISIONS,
1202 CSR_READ_2(sc, BM_TX_NCCNT) + CSR_READ_2(sc, BM_TX_FCCNT) +
1203 CSR_READ_2(sc, BM_TX_EXCNT) + CSR_READ_2(sc, BM_TX_LTCNT));
1205 if_inc_counter(sc->sc_ifp, IFCOUNTER_IERRORS,
1206 CSR_READ_2(sc, BM_RX_LECNT) + CSR_READ_2(sc, BM_RX_AECNT) +
1207 CSR_READ_2(sc, BM_RX_FECNT));
1209 /* Zero collision counters */
1210 CSR_WRITE_2(sc, BM_TX_NCCNT, 0);
1211 CSR_WRITE_2(sc, BM_TX_FCCNT, 0);
1212 CSR_WRITE_2(sc, BM_TX_EXCNT, 0);
1213 CSR_WRITE_2(sc, BM_TX_LTCNT, 0);
1215 /* Zero receive counters */
1216 CSR_WRITE_2(sc, BM_RX_FRCNT, 0);
1217 CSR_WRITE_2(sc, BM_RX_LECNT, 0);
1218 CSR_WRITE_2(sc, BM_RX_AECNT, 0);
1219 CSR_WRITE_2(sc, BM_RX_FECNT, 0);
1220 CSR_WRITE_2(sc, BM_RXCV, 0);
1222 /* Check for link changes and run watchdog */
1223 mii_tick(sc->sc_mii);
1224 bm_miibus_statchg(sc->sc_dev);
1226 if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0) {
1227 callout_reset(&sc->sc_tick_ch, hz, bm_tick, sc);
1232 device_printf(sc->sc_dev, "device timeout\n");
1238 bm_add_rxbuf(struct bm_softc *sc, int idx)
1240 struct bm_rxsoft *rxs = &sc->sc_rxsoft[idx];
1242 bus_dma_segment_t segs[1];
1245 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1248 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
1250 if (rxs->rxs_mbuf != NULL) {
1251 bus_dmamap_sync(sc->sc_rdma_tag, rxs->rxs_dmamap,
1252 BUS_DMASYNC_POSTREAD);
1253 bus_dmamap_unload(sc->sc_rdma_tag, rxs->rxs_dmamap);
1256 error = bus_dmamap_load_mbuf_sg(sc->sc_rdma_tag, rxs->rxs_dmamap, m,
1257 segs, &nsegs, BUS_DMA_NOWAIT);
1259 device_printf(sc->sc_dev,
1260 "cannot load RS DMA map %d, error = %d\n", idx, error);
1264 /* If nsegs is wrong then the stack is corrupt. */
1266 ("%s: too many DMA segments (%d)", __func__, nsegs));
1268 rxs->segment = segs[0];
1270 bus_dmamap_sync(sc->sc_rdma_tag, rxs->rxs_dmamap, BUS_DMASYNC_PREREAD);
1276 bm_add_rxbuf_dma(struct bm_softc *sc, int idx)
1278 struct bm_rxsoft *rxs = &sc->sc_rxsoft[idx];
1280 dbdma_insert_command(sc->sc_rxdma, idx, DBDMA_INPUT_LAST, 0,
1281 rxs->segment.ds_addr, rxs->segment.ds_len, DBDMA_ALWAYS,
1282 DBDMA_NEVER, DBDMA_NEVER, 0);
1288 bm_enable_interrupts(struct bm_softc *sc)
1290 CSR_WRITE_2(sc, BM_INTR_DISABLE,
1291 (sc->sc_streaming) ? BM_INTR_NONE : BM_INTR_NORMAL);
1295 bm_disable_interrupts(struct bm_softc *sc)
1297 CSR_WRITE_2(sc, BM_INTR_DISABLE, BM_INTR_NONE);