Home | History | Annotate | Line # | Download | only in dev
sbmac.c revision 1.1
      1  1.1  simonb /* $NetBSD: sbmac.c,v 1.1 2002/03/05 23:46:42 simonb Exp $ */
      2  1.1  simonb 
      3  1.1  simonb /*
      4  1.1  simonb  * Copyright 2000, 2001
      5  1.1  simonb  * Broadcom Corporation. All rights reserved.
      6  1.1  simonb  *
      7  1.1  simonb  * This software is furnished under license and may be used and copied only
      8  1.1  simonb  * in accordance with the following terms and conditions.  Subject to these
      9  1.1  simonb  * conditions, you may download, copy, install, use, modify and distribute
     10  1.1  simonb  * modified or unmodified copies of this software in source and/or binary
     11  1.1  simonb  * form. No title or ownership is transferred hereby.
     12  1.1  simonb  *
     13  1.1  simonb  * 1) Any source code used, modified or distributed must reproduce and
     14  1.1  simonb  *    retain this copyright notice and list of conditions as they appear in
     15  1.1  simonb  *    the source file.
     16  1.1  simonb  *
     17  1.1  simonb  * 2) No right is granted to use any trade name, trademark, or logo of
     18  1.1  simonb  *    Broadcom Corporation. Neither the "Broadcom Corporation" name nor any
     19  1.1  simonb  *    trademark or logo of Broadcom Corporation may be used to endorse or
     20  1.1  simonb  *    promote products derived from this software without the prior written
     21  1.1  simonb  *    permission of Broadcom Corporation.
     22  1.1  simonb  *
     23  1.1  simonb  * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
     24  1.1  simonb  *    WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
     25  1.1  simonb  *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
     26  1.1  simonb  *    NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
     27  1.1  simonb  *    FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
     28  1.1  simonb  *    LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  1.1  simonb  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  1.1  simonb  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     31  1.1  simonb  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     32  1.1  simonb  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     33  1.1  simonb  *    OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  1.1  simonb  */
     35  1.1  simonb 
     36  1.1  simonb #include "bpfilter.h"
     37  1.1  simonb #include "opt_inet.h"
     38  1.1  simonb #include "opt_ns.h"
     39  1.1  simonb 
     40  1.1  simonb #include <sys/param.h>
     41  1.1  simonb #include <sys/systm.h>
     42  1.1  simonb #include <sys/sockio.h>
     43  1.1  simonb #include <sys/mbuf.h>
     44  1.1  simonb #include <sys/malloc.h>
     45  1.1  simonb #include <sys/kernel.h>
     46  1.1  simonb #include <sys/socket.h>
     47  1.1  simonb #include <sys/queue.h>
     48  1.1  simonb #include <sys/device.h>
     49  1.1  simonb 
     50  1.1  simonb #include <net/if.h>
     51  1.1  simonb #include <net/if_arp.h>
     52  1.1  simonb #include <net/if_ether.h>
     53  1.1  simonb #include <net/if_dl.h>
     54  1.1  simonb #include <net/if_media.h>
     55  1.1  simonb 
     56  1.1  simonb #if NBPFILTER > 0
     57  1.1  simonb #include <net/bpf.h>
     58  1.1  simonb #endif
     59  1.1  simonb 
     60  1.1  simonb #ifdef INET
     61  1.1  simonb #include <netinet/in.h>
     62  1.1  simonb #include <netinet/if_inarp.h>
     63  1.1  simonb #endif
     64  1.1  simonb 
     65  1.1  simonb #ifdef NS
     66  1.1  simonb #include <netns/ns.h>
     67  1.1  simonb #include <netns/ns_if.h>
     68  1.1  simonb #endif
     69  1.1  simonb 
     70  1.1  simonb #include <machine/cpu.h>
     71  1.1  simonb #include <machine/bus.h>
     72  1.1  simonb #include <machine/intr.h>
     73  1.1  simonb #include <machine/locore.h>
     74  1.1  simonb 
     75  1.1  simonb #include "sbobiovar.h"
     76  1.1  simonb 
     77  1.1  simonb #include <dev/mii/mii.h>
     78  1.1  simonb #include <dev/mii/miivar.h>
     79  1.1  simonb #include <dev/mii/mii_bitbang.h>
     80  1.1  simonb 
     81  1.1  simonb #include <mips/sibyte/include/sb1250_defs.h>
     82  1.1  simonb #include <mips/sibyte/include/sb1250_regs.h>
     83  1.1  simonb #include <mips/sibyte/include/sb1250_mac.h>
     84  1.1  simonb #include <mips/sibyte/include/sb1250_dma.h>
     85  1.1  simonb 
     86  1.1  simonb 
     87  1.1  simonb /*  *********************************************************************
     88  1.1  simonb     *  Simple types
     89  1.1  simonb     ********************************************************************* */
     90  1.1  simonb 
     91  1.1  simonb 
     92  1.1  simonb typedef u_long sbmac_port_t;
     93  1.1  simonb typedef uint64_t sbmac_physaddr_t;
     94  1.1  simonb typedef uint64_t sbmac_enetaddr_t;
     95  1.1  simonb 
     96  1.1  simonb typedef enum { sbmac_speed_auto, sbmac_speed_10,
     97  1.1  simonb 	       sbmac_speed_100, sbmac_speed_1000 } sbmac_speed_t;
     98  1.1  simonb 
     99  1.1  simonb typedef enum { sbmac_duplex_auto, sbmac_duplex_half,
    100  1.1  simonb 	       sbmac_duplex_full } sbmac_duplex_t;
    101  1.1  simonb 
    102  1.1  simonb typedef enum { sbmac_fc_auto, sbmac_fc_disabled, sbmac_fc_frame,
    103  1.1  simonb 	       sbmac_fc_collision, sbmac_fc_carrier } sbmac_fc_t;
    104  1.1  simonb 
    105  1.1  simonb typedef enum { sbmac_state_uninit, sbmac_state_off, sbmac_state_on,
    106  1.1  simonb 	       sbmac_state_broken } sbmac_state_t;
    107  1.1  simonb 
    108  1.1  simonb 
    109  1.1  simonb /*  *********************************************************************
    110  1.1  simonb     *  Macros
    111  1.1  simonb     ********************************************************************* */
    112  1.1  simonb 
    113  1.1  simonb 
    114  1.1  simonb #define	SBDMA_NEXTBUF(d, f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? \
    115  1.1  simonb 			  (d)->sbdma_dscrtable : (d)->f+1)
    116  1.1  simonb 
    117  1.1  simonb 
    118  1.1  simonb #define	CACHELINESIZE 32
    119  1.1  simonb #define	NUMCACHEBLKS(x) (((x)+CACHELINESIZE-1)/CACHELINESIZE)
    120  1.1  simonb #define	KMALLOC(x) malloc((x), M_DEVBUF, M_DONTWAIT)
    121  1.1  simonb #define	KVTOPHYS(x) kvtophys((vaddr_t)(x))
    122  1.1  simonb 
    123  1.1  simonb #ifdef SBMACDEBUG
    124  1.1  simonb #define	dprintf(x)	printf x
    125  1.1  simonb #else
    126  1.1  simonb #define	dprintf(x)
    127  1.1  simonb #endif
    128  1.1  simonb 
    129  1.1  simonb #define	SBMAC_READCSR(t) mips3_ld((uint64_t *) (t))
    130  1.1  simonb #define	SBMAC_WRITECSR(t, v) mips3_sd((uint64_t *) (t), (v))
    131  1.1  simonb 
    132  1.1  simonb #define	PKSEG1(x) ((sbmac_port_t) MIPS_PHYS_TO_KSEG1(x))
    133  1.1  simonb 
    134  1.1  simonb #define	SBMAC_MAX_TXDESCR	64
    135  1.1  simonb #define	SBMAC_MAX_RXDESCR	64
    136  1.1  simonb 
    137  1.1  simonb #define	ETHER_ALIGN	2
    138  1.1  simonb 
    139  1.1  simonb /*  *********************************************************************
    140  1.1  simonb     *  DMA Descriptor structure
    141  1.1  simonb     ********************************************************************* */
    142  1.1  simonb 
    143  1.1  simonb typedef struct sbdmadscr_s {
    144  1.1  simonb 	uint64_t dscr_a;
    145  1.1  simonb 	uint64_t dscr_b;
    146  1.1  simonb } sbdmadscr_t;
    147  1.1  simonb 
    148  1.1  simonb /*  *********************************************************************
    149  1.1  simonb     *  DMA Controller structure
    150  1.1  simonb     ********************************************************************* */
    151  1.1  simonb 
    152  1.1  simonb typedef struct sbmacdma_s {
    153  1.1  simonb 
    154  1.1  simonb 	/*
    155  1.1  simonb 	 * This stuff is used to identify the channel and the registers
    156  1.1  simonb 	 * associated with it.
    157  1.1  simonb 	 */
    158  1.1  simonb 
    159  1.1  simonb 	struct sbmac_softc *sbdma_eth;	/* back pointer to associated MAC */
    160  1.1  simonb 	int		sbdma_channel;	/* channel number */
    161  1.1  simonb 	int		sbdma_txdir;	/* direction (1=transmit) */
    162  1.1  simonb 	int		sbdma_maxdescr;	/* total # of descriptors in ring */
    163  1.1  simonb 	sbmac_port_t	sbdma_config0;	/* DMA config register 0 */
    164  1.1  simonb 	sbmac_port_t	sbdma_config1;	/* DMA config register 1 */
    165  1.1  simonb 	sbmac_port_t	sbdma_dscrbase;	/* Descriptor base address */
    166  1.1  simonb 	sbmac_port_t	sbdma_dscrcnt; 	/* Descriptor count register */
    167  1.1  simonb 	sbmac_port_t	sbdma_curdscr;	/* current descriptor address */
    168  1.1  simonb 
    169  1.1  simonb 	/*
    170  1.1  simonb 	 * This stuff is for maintenance of the ring
    171  1.1  simonb 	 */
    172  1.1  simonb 
    173  1.1  simonb 	sbdmadscr_t	*sbdma_dscrtable;	/* base of descriptor table */
    174  1.1  simonb 	sbdmadscr_t	*sbdma_dscrtable_end;	/* end of descriptor table */
    175  1.1  simonb 
    176  1.1  simonb 	struct mbuf	**sbdma_ctxtable;	/* context table, one per descr */
    177  1.1  simonb 
    178  1.1  simonb 	paddr_t		sbdma_dscrtable_phys;	/* and also the phys addr */
    179  1.1  simonb 	sbdmadscr_t	*sbdma_addptr;		/* next dscr for sw to add */
    180  1.1  simonb 	sbdmadscr_t	*sbdma_remptr;		/* next dscr for sw to remove */
    181  1.1  simonb } sbmacdma_t;
    182  1.1  simonb 
    183  1.1  simonb 
    184  1.1  simonb /*  *********************************************************************
    185  1.1  simonb     *  Ethernet softc structure
    186  1.1  simonb     ********************************************************************* */
    187  1.1  simonb 
    188  1.1  simonb struct sbmac_softc {
    189  1.1  simonb 
    190  1.1  simonb 	/*
    191  1.1  simonb 	 * NetBSD-specific things
    192  1.1  simonb 	 */
    193  1.1  simonb 	struct device	sc_dev;		/* base device (must be first) */
    194  1.1  simonb 	struct ethercom	sc_ethercom;	/* Ethernet common part */
    195  1.1  simonb 	struct mii_data	sc_mii;
    196  1.1  simonb 	struct callout	sc_tick_ch;
    197  1.1  simonb 
    198  1.1  simonb 	int		sbm_if_flags;
    199  1.1  simonb 	void		*sbm_intrhand;
    200  1.1  simonb 
    201  1.1  simonb 	/*
    202  1.1  simonb 	 * Controller-specific things
    203  1.1  simonb 	 */
    204  1.1  simonb 
    205  1.1  simonb 	sbmac_port_t	sbm_base;	/* MAC's base address */
    206  1.1  simonb 	sbmac_state_t	sbm_state;	/* current state */
    207  1.1  simonb 
    208  1.1  simonb 	sbmac_port_t	sbm_macenable;	/* MAC Enable Register */
    209  1.1  simonb 	sbmac_port_t	sbm_maccfg;	/* MAC Configuration Register */
    210  1.1  simonb 	sbmac_port_t	sbm_fifocfg;	/* FIFO configuration register */
    211  1.1  simonb 	sbmac_port_t	sbm_framecfg;	/* Frame configuration register */
    212  1.1  simonb 	sbmac_port_t	sbm_rxfilter;	/* receive filter register */
    213  1.1  simonb 	sbmac_port_t	sbm_isr;	/* Interrupt status register */
    214  1.1  simonb 	sbmac_port_t	sbm_imr;	/* Interrupt mask register */
    215  1.1  simonb 
    216  1.1  simonb 	sbmac_speed_t	sbm_speed;	/* current speed */
    217  1.1  simonb 	sbmac_duplex_t	sbm_duplex;	/* current duplex */
    218  1.1  simonb 	sbmac_fc_t	sbm_fc;		/* current flow control setting */
    219  1.1  simonb 	int		sbm_rxflags;	/* received packet flags */
    220  1.1  simonb 
    221  1.1  simonb 	u_char		sbm_hwaddr[ETHER_ADDR_LEN];
    222  1.1  simonb 
    223  1.1  simonb 	sbmacdma_t	sbm_txdma;	/* for now, only use channel 0 */
    224  1.1  simonb 	sbmacdma_t	sbm_rxdma;
    225  1.1  simonb };
    226  1.1  simonb 
    227  1.1  simonb 
    228  1.1  simonb /*  *********************************************************************
    229  1.1  simonb     *  Externs
    230  1.1  simonb     ********************************************************************* */
    231  1.1  simonb 
    232  1.1  simonb extern paddr_t kvtophys(vaddr_t);
    233  1.1  simonb 
    234  1.1  simonb /*  *********************************************************************
    235  1.1  simonb     *  Prototypes
    236  1.1  simonb     ********************************************************************* */
    237  1.1  simonb 
    238  1.1  simonb static void sbdma_initctx(sbmacdma_t *d, struct sbmac_softc *s, int chan,
    239  1.1  simonb     int txrx, int maxdescr);
    240  1.1  simonb static void sbdma_channel_start(sbmacdma_t *d);
    241  1.1  simonb static int sbdma_add_rcvbuffer(sbmacdma_t *d, struct mbuf *m);
    242  1.1  simonb static int sbdma_add_txbuffer(sbmacdma_t *d, struct mbuf *m);
    243  1.1  simonb static void sbdma_emptyring(sbmacdma_t *d);
    244  1.1  simonb static void sbdma_fillring(sbmacdma_t *d);
    245  1.1  simonb static void sbdma_rx_process(struct sbmac_softc *sc, sbmacdma_t *d);
    246  1.1  simonb static void sbdma_tx_process(struct sbmac_softc *sc, sbmacdma_t *d);
    247  1.1  simonb static void sbmac_initctx(struct sbmac_softc *s);
    248  1.1  simonb static void sbmac_channel_start(struct sbmac_softc *s);
    249  1.1  simonb static void sbmac_channel_stop(struct sbmac_softc *s);
    250  1.1  simonb static sbmac_state_t sbmac_set_channel_state(struct sbmac_softc *,
    251  1.1  simonb     sbmac_state_t);
    252  1.1  simonb static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff);
    253  1.1  simonb static void sbmac_init_and_start(struct sbmac_softc *sc);
    254  1.1  simonb static uint64_t sbmac_addr2reg(u_char *ptr);
    255  1.1  simonb static void sbmac_intr(void *xsc, uint32_t status, uint32_t pc);
    256  1.1  simonb static void sbmac_start(struct ifnet *ifp);
    257  1.1  simonb static void sbmac_setmulti(struct sbmac_softc *sc);
    258  1.1  simonb static int sbmac_ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
    259  1.1  simonb static int sbmac_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
    260  1.1  simonb static int sbmac_mediachange(struct ifnet *ifp);
    261  1.1  simonb static void sbmac_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr);
    262  1.1  simonb static void sbmac_watchdog(struct ifnet *ifp);
    263  1.1  simonb static int sbmac_match(struct device *parent, struct cfdata *match, void *aux);
    264  1.1  simonb static void sbmac_attach(struct device *parent, struct device *self, void *aux);
    265  1.1  simonb static int sbmac_set_speed(struct sbmac_softc *s, sbmac_speed_t speed);
    266  1.1  simonb static int sbmac_set_duplex(struct sbmac_softc *s, sbmac_duplex_t duplex,
    267  1.1  simonb     sbmac_fc_t fc);
    268  1.1  simonb static void sbmac_tick(void *arg);
    269  1.1  simonb 
    270  1.1  simonb 
    271  1.1  simonb /*  *********************************************************************
    272  1.1  simonb     *  Globals
    273  1.1  simonb     ********************************************************************* */
    274  1.1  simonb 
    275  1.1  simonb const struct cfattach sbmac_ca = {
    276  1.1  simonb 	sizeof(struct sbmac_softc), sbmac_match, sbmac_attach,
    277  1.1  simonb };
    278  1.1  simonb 
    279  1.1  simonb 
    280  1.1  simonb uint32_t sbmac_mii_bitbang_read(struct device *self);
    281  1.1  simonb void sbmac_mii_bitbang_write(struct device *self, uint32_t val);
    282  1.1  simonb 
    283  1.1  simonb static const struct mii_bitbang_ops sbmac_mii_bitbang_ops = {
    284  1.1  simonb 	sbmac_mii_bitbang_read,
    285  1.1  simonb 	sbmac_mii_bitbang_write,
    286  1.1  simonb 	{
    287  1.1  simonb 		(uint32_t)M_MAC_MDIO_OUT,	/* MII_BIT_MDO */
    288  1.1  simonb 		(uint32_t)M_MAC_MDIO_IN,	/* MII_BIT_MDI */
    289  1.1  simonb 		(uint32_t)M_MAC_MDC,		/* MII_BIT_MDC */
    290  1.1  simonb 		0,				/* MII_BIT_DIR_HOST_PHY */
    291  1.1  simonb 		(uint32_t)M_MAC_MDIO_DIR	/* MII_BIT_DIR_PHY_HOST */
    292  1.1  simonb 	}
    293  1.1  simonb };
    294  1.1  simonb 
    295  1.1  simonb uint32_t
    296  1.1  simonb sbmac_mii_bitbang_read(struct device *self)
    297  1.1  simonb {
    298  1.1  simonb 	struct sbmac_softc *sc = (void *) self;
    299  1.1  simonb 	sbmac_port_t reg;
    300  1.1  simonb 
    301  1.1  simonb 	reg = PKSEG1(sc->sbm_base + R_MAC_MDIO);
    302  1.1  simonb 	return (uint32_t) SBMAC_READCSR(reg);
    303  1.1  simonb }
    304  1.1  simonb 
    305  1.1  simonb void
    306  1.1  simonb sbmac_mii_bitbang_write(struct device *self, uint32_t val)
    307  1.1  simonb {
    308  1.1  simonb 	struct sbmac_softc *sc = (void *) self;
    309  1.1  simonb 	sbmac_port_t reg;
    310  1.1  simonb 
    311  1.1  simonb 	reg = PKSEG1(sc->sbm_base + R_MAC_MDIO);
    312  1.1  simonb 
    313  1.1  simonb 	SBMAC_WRITECSR(reg, (val &
    314  1.1  simonb 	    (M_MAC_MDC|M_MAC_MDIO_DIR|M_MAC_MDIO_OUT|M_MAC_MDIO_IN)));
    315  1.1  simonb }
    316  1.1  simonb 
    317  1.1  simonb /*
    318  1.1  simonb  * Read an PHY register through the MII.
    319  1.1  simonb  */
    320  1.1  simonb static int
    321  1.1  simonb sbmac_mii_readreg(struct device *self, int phy, int reg)
    322  1.1  simonb {
    323  1.1  simonb 
    324  1.1  simonb 	return (mii_bitbang_readreg(self, &sbmac_mii_bitbang_ops, phy, reg));
    325  1.1  simonb }
    326  1.1  simonb 
    327  1.1  simonb /*
    328  1.1  simonb  * Write to a PHY register through the MII.
    329  1.1  simonb  */
    330  1.1  simonb static void
    331  1.1  simonb sbmac_mii_writereg(struct device *self, int phy, int reg, int val)
    332  1.1  simonb {
    333  1.1  simonb 
    334  1.1  simonb 	mii_bitbang_writereg(self, &sbmac_mii_bitbang_ops, phy, reg, val);
    335  1.1  simonb }
    336  1.1  simonb 
    337  1.1  simonb static void
    338  1.1  simonb sbmac_mii_statchg(struct device *self)
    339  1.1  simonb {
    340  1.1  simonb 	struct sbmac_softc *sc = (struct sbmac_softc *)self;
    341  1.1  simonb 	sbmac_state_t oldstate;
    342  1.1  simonb 
    343  1.1  simonb 	/* Stop the MAC in preparation for changing all of the parameters. */
    344  1.1  simonb 	oldstate = sbmac_set_channel_state(sc, sbmac_state_off);
    345  1.1  simonb 
    346  1.1  simonb 	switch (sc->sc_ethercom.ec_if.if_baudrate) {
    347  1.1  simonb 	default:		/* if autonegotiation fails, assume 10Mbit */
    348  1.1  simonb 	case IF_Mbps(10):
    349  1.1  simonb 		sbmac_set_speed(sc, sbmac_speed_10);
    350  1.1  simonb 		break;
    351  1.1  simonb 
    352  1.1  simonb 	case IF_Mbps(100):
    353  1.1  simonb 		sbmac_set_speed(sc, sbmac_speed_100);
    354  1.1  simonb 		break;
    355  1.1  simonb 
    356  1.1  simonb 	case IF_Mbps(1000):
    357  1.1  simonb 		sbmac_set_speed(sc, sbmac_speed_1000);
    358  1.1  simonb 		break;
    359  1.1  simonb 	}
    360  1.1  simonb 
    361  1.1  simonb 	if (sc->sc_mii.mii_media_active & IFM_FDX) {
    362  1.1  simonb 		/* Configure for full-duplex */
    363  1.1  simonb 		/* XXX: is flow control right for 10, 100? */
    364  1.1  simonb 		sbmac_set_duplex(sc, sbmac_duplex_full, sbmac_fc_frame);
    365  1.1  simonb 	} else {
    366  1.1  simonb 		/* Configure for half-duplex */
    367  1.1  simonb 		/* XXX: is flow control right? */
    368  1.1  simonb 		sbmac_set_duplex(sc, sbmac_duplex_half, sbmac_fc_disabled);
    369  1.1  simonb 	}
    370  1.1  simonb 
    371  1.1  simonb 	/* And put it back into its former state. */
    372  1.1  simonb 	sbmac_set_channel_state(sc, oldstate);
    373  1.1  simonb }
    374  1.1  simonb 
    375  1.1  simonb /*  *********************************************************************
    376  1.1  simonb     *  SBDMA_INITCTX(d, s, chan, txrx, maxdescr)
    377  1.1  simonb     *
    378  1.1  simonb     *  Initialize a DMA channel context.  Since there are potentially
    379  1.1  simonb     *  eight DMA channels per MAC, it's nice to do this in a standard
    380  1.1  simonb     *  way.
    381  1.1  simonb     *
    382  1.1  simonb     *  Input parameters:
    383  1.1  simonb     *  	   d - sbmacdma_t structure (DMA channel context)
    384  1.1  simonb     *  	   s - sbmac_softc structure (pointer to a MAC)
    385  1.1  simonb     *  	   chan - channel number (0..1 right now)
    386  1.1  simonb     *  	   txrx - Identifies DMA_TX or DMA_RX for channel direction
    387  1.1  simonb     *      maxdescr - number of descriptors
    388  1.1  simonb     *
    389  1.1  simonb     *  Return value:
    390  1.1  simonb     *  	   nothing
    391  1.1  simonb     ********************************************************************* */
    392  1.1  simonb 
    393  1.1  simonb static void
    394  1.1  simonb sbdma_initctx(sbmacdma_t *d, struct sbmac_softc *s, int chan, int txrx,
    395  1.1  simonb     int maxdescr)
    396  1.1  simonb {
    397  1.1  simonb 	/*
    398  1.1  simonb 	 * Save away interesting stuff in the structure
    399  1.1  simonb 	 */
    400  1.1  simonb 
    401  1.1  simonb 	d->sbdma_eth       = s;
    402  1.1  simonb 	d->sbdma_channel   = chan;
    403  1.1  simonb 	d->sbdma_txdir     = txrx;
    404  1.1  simonb 
    405  1.1  simonb 	/*
    406  1.1  simonb 	 * initialize register pointers
    407  1.1  simonb 	 */
    408  1.1  simonb 
    409  1.1  simonb 	d->sbdma_config0 = PKSEG1(s->sbm_base +
    410  1.1  simonb 	    R_MAC_DMA_REGISTER(txrx, chan, R_MAC_DMA_CONFIG0));
    411  1.1  simonb 	d->sbdma_config1 = PKSEG1(s->sbm_base +
    412  1.1  simonb 	    R_MAC_DMA_REGISTER(txrx, chan, R_MAC_DMA_CONFIG0));
    413  1.1  simonb 	d->sbdma_dscrbase = PKSEG1(s->sbm_base +
    414  1.1  simonb 	    R_MAC_DMA_REGISTER(txrx, chan, R_MAC_DMA_DSCR_BASE));
    415  1.1  simonb 	d->sbdma_dscrcnt = PKSEG1(s->sbm_base +
    416  1.1  simonb 	    R_MAC_DMA_REGISTER(txrx, chan, R_MAC_DMA_DSCR_CNT));
    417  1.1  simonb 	d->sbdma_curdscr = PKSEG1(s->sbm_base +
    418  1.1  simonb 	    R_MAC_DMA_REGISTER(txrx, chan, R_MAC_DMA_CUR_DSCRADDR));
    419  1.1  simonb 
    420  1.1  simonb 	/*
    421  1.1  simonb 	 * Allocate memory for the ring
    422  1.1  simonb 	 */
    423  1.1  simonb 
    424  1.1  simonb 	d->sbdma_maxdescr = maxdescr;
    425  1.1  simonb 
    426  1.1  simonb 	d->sbdma_dscrtable = (sbdmadscr_t *)
    427  1.1  simonb 	    KMALLOC(d->sbdma_maxdescr*sizeof(sbdmadscr_t));
    428  1.1  simonb 
    429  1.1  simonb 	bzero(d->sbdma_dscrtable, d->sbdma_maxdescr*sizeof(sbdmadscr_t));
    430  1.1  simonb 
    431  1.1  simonb 	d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
    432  1.1  simonb 
    433  1.1  simonb 	d->sbdma_dscrtable_phys = KVTOPHYS(d->sbdma_dscrtable);
    434  1.1  simonb 
    435  1.1  simonb 	/*
    436  1.1  simonb 	 * And context table
    437  1.1  simonb 	 */
    438  1.1  simonb 
    439  1.1  simonb 	d->sbdma_ctxtable = (struct mbuf **)
    440  1.1  simonb 	    KMALLOC(d->sbdma_maxdescr*sizeof(struct mbuf *));
    441  1.1  simonb 
    442  1.1  simonb 	bzero(d->sbdma_ctxtable, d->sbdma_maxdescr*sizeof(struct mbuf *));
    443  1.1  simonb }
    444  1.1  simonb 
    445  1.1  simonb /*  *********************************************************************
    446  1.1  simonb     *  SBDMA_CHANNEL_START(d)
    447  1.1  simonb     *
    448  1.1  simonb     *  Initialize the hardware registers for a DMA channel.
    449  1.1  simonb     *
    450  1.1  simonb     *  Input parameters:
    451  1.1  simonb     *  	   d - DMA channel to init (context must be previously init'd
    452  1.1  simonb     *
    453  1.1  simonb     *  Return value:
    454  1.1  simonb     *  	   nothing
    455  1.1  simonb     ********************************************************************* */
    456  1.1  simonb 
    457  1.1  simonb static void
    458  1.1  simonb sbdma_channel_start(sbmacdma_t *d)
    459  1.1  simonb {
    460  1.1  simonb 	/*
    461  1.1  simonb 	 * Turn on the DMA channel
    462  1.1  simonb 	 */
    463  1.1  simonb 
    464  1.1  simonb 	SBMAC_WRITECSR(d->sbdma_config1, 0);
    465  1.1  simonb 
    466  1.1  simonb 	SBMAC_WRITECSR(d->sbdma_dscrbase, d->sbdma_dscrtable_phys);
    467  1.1  simonb 
    468  1.1  simonb 	SBMAC_WRITECSR(d->sbdma_config0, V_DMA_RINGSZ(d->sbdma_maxdescr) | 0);
    469  1.1  simonb 
    470  1.1  simonb 	/*
    471  1.1  simonb 	 * Initialize ring pointers
    472  1.1  simonb 	 */
    473  1.1  simonb 
    474  1.1  simonb 	d->sbdma_addptr = d->sbdma_dscrtable;
    475  1.1  simonb 	d->sbdma_remptr = d->sbdma_dscrtable;
    476  1.1  simonb }
    477  1.1  simonb 
    478  1.1  simonb /*  *********************************************************************
    479  1.1  simonb     *  SBDMA_ADD_RCVBUFFER(d, m)
    480  1.1  simonb     *
    481  1.1  simonb     *  Add a buffer to the specified DMA channel.   For receive channels,
    482  1.1  simonb     *  this queues a buffer for inbound packets.
    483  1.1  simonb     *
    484  1.1  simonb     *  Input parameters:
    485  1.1  simonb     *  	   d - DMA channel descriptor
    486  1.1  simonb     * 	   m - mbuf to add, or NULL if we should allocate one.
    487  1.1  simonb     *
    488  1.1  simonb     *  Return value:
    489  1.1  simonb     *  	   0 if buffer could not be added (ring is full)
    490  1.1  simonb     *  	   1 if buffer added successfully
    491  1.1  simonb     ********************************************************************* */
    492  1.1  simonb 
    493  1.1  simonb 
    494  1.1  simonb static int
    495  1.1  simonb sbdma_add_rcvbuffer(sbmacdma_t *d, struct mbuf *m)
    496  1.1  simonb {
    497  1.1  simonb 	sbdmadscr_t *dsc;
    498  1.1  simonb 	sbdmadscr_t *nextdsc;
    499  1.1  simonb 	struct mbuf *m_new = NULL;
    500  1.1  simonb 
    501  1.1  simonb 	/* get pointer to our current place in the ring */
    502  1.1  simonb 
    503  1.1  simonb 	dsc = d->sbdma_addptr;
    504  1.1  simonb 	nextdsc = SBDMA_NEXTBUF(d, sbdma_addptr);
    505  1.1  simonb 
    506  1.1  simonb 	/*
    507  1.1  simonb 	 * figure out if the ring is full - if the next descriptor
    508  1.1  simonb 	 * is the same as the one that we're going to remove from
    509  1.1  simonb 	 * the ring, the ring is full
    510  1.1  simonb 	 */
    511  1.1  simonb 
    512  1.1  simonb 	if (nextdsc == d->sbdma_remptr)
    513  1.1  simonb 		return ENOSPC;
    514  1.1  simonb 
    515  1.1  simonb 	/*
    516  1.1  simonb 	 * Allocate an mbuf if we don't already have one.
    517  1.1  simonb 	 * If we do have an mbuf, reset it so that it's empty.
    518  1.1  simonb 	 */
    519  1.1  simonb 
    520  1.1  simonb 	if (m == NULL) {
    521  1.1  simonb 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    522  1.1  simonb 		if (m_new == NULL) {
    523  1.1  simonb 			printf("%s: mbuf allocation failed\n",
    524  1.1  simonb 			    d->sbdma_eth->sc_dev.dv_xname);
    525  1.1  simonb 			return ENOBUFS;
    526  1.1  simonb 		}
    527  1.1  simonb 
    528  1.1  simonb 		MCLGET(m_new, M_DONTWAIT);
    529  1.1  simonb 		if (!(m_new->m_flags & M_EXT)) {
    530  1.1  simonb 			printf("%s: mbuf cluster allocation failed\n",
    531  1.1  simonb 			    d->sbdma_eth->sc_dev.dv_xname);
    532  1.1  simonb 			m_freem(m_new);
    533  1.1  simonb 			return ENOBUFS;
    534  1.1  simonb 		}
    535  1.1  simonb 
    536  1.1  simonb 		m_new->m_len = m_new->m_pkthdr.len= MCLBYTES;
    537  1.1  simonb 		m_adj(m_new, ETHER_ALIGN);
    538  1.1  simonb 	} else {
    539  1.1  simonb 		m_new = m;
    540  1.1  simonb 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    541  1.1  simonb 		m_new->m_data = m_new->m_ext.ext_buf;
    542  1.1  simonb 		m_adj(m_new, ETHER_ALIGN);
    543  1.1  simonb 	}
    544  1.1  simonb 
    545  1.1  simonb 	/*
    546  1.1  simonb 	 * fill in the descriptor
    547  1.1  simonb 	 */
    548  1.1  simonb 
    549  1.1  simonb 	dsc->dscr_a = KVTOPHYS(mtod(m_new, caddr_t)) |
    550  1.1  simonb 	    V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(ETHER_ALIGN + m_new->m_len)) |
    551  1.1  simonb 	    M_DMA_DSCRA_INTERRUPT;
    552  1.1  simonb 
    553  1.1  simonb 	/* receiving: no options */
    554  1.1  simonb 	dsc->dscr_b = 0;
    555  1.1  simonb 
    556  1.1  simonb 	/*
    557  1.1  simonb 	 * fill in the context
    558  1.1  simonb 	 */
    559  1.1  simonb 
    560  1.1  simonb 	d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = m_new;
    561  1.1  simonb 
    562  1.1  simonb 	/*
    563  1.1  simonb 	 * point at next packet
    564  1.1  simonb 	 */
    565  1.1  simonb 
    566  1.1  simonb 	d->sbdma_addptr = nextdsc;
    567  1.1  simonb 
    568  1.1  simonb 	/*
    569  1.1  simonb 	 * Give the buffer to the DMA engine.
    570  1.1  simonb 	 */
    571  1.1  simonb 
    572  1.1  simonb 	SBMAC_WRITECSR(d->sbdma_dscrcnt, 1);
    573  1.1  simonb 
    574  1.1  simonb 	return 0;					/* we did it */
    575  1.1  simonb }
    576  1.1  simonb 
    577  1.1  simonb /*  *********************************************************************
    578  1.1  simonb     *  SBDMA_ADD_TXBUFFER(d, m)
    579  1.1  simonb     *
    580  1.1  simonb     *  Add a transmit buffer to the specified DMA channel, causing a
    581  1.1  simonb     *  transmit to start.
    582  1.1  simonb     *
    583  1.1  simonb     *  Input parameters:
    584  1.1  simonb     *  	   d - DMA channel descriptor
    585  1.1  simonb     * 	   m - mbuf to add
    586  1.1  simonb     *
    587  1.1  simonb     *  Return value:
    588  1.1  simonb     *  	   0 transmit queued successfully
    589  1.1  simonb     *  	   otherwise error code
    590  1.1  simonb     ********************************************************************* */
    591  1.1  simonb 
    592  1.1  simonb 
    593  1.1  simonb static int
    594  1.1  simonb sbdma_add_txbuffer(sbmacdma_t *d, struct mbuf *m)
    595  1.1  simonb {
    596  1.1  simonb 	sbdmadscr_t *dsc;
    597  1.1  simonb 	sbdmadscr_t *nextdsc;
    598  1.1  simonb 	struct mbuf *m_new = NULL;
    599  1.1  simonb 	int length;
    600  1.1  simonb 
    601  1.1  simonb 	/* get pointer to our current place in the ring */
    602  1.1  simonb 
    603  1.1  simonb 	dsc = d->sbdma_addptr;
    604  1.1  simonb 	nextdsc = SBDMA_NEXTBUF(d, sbdma_addptr);
    605  1.1  simonb 
    606  1.1  simonb 	/*
    607  1.1  simonb 	 * figure out if the ring is full - if the next descriptor
    608  1.1  simonb 	 * is the same as the one that we're going to remove from
    609  1.1  simonb 	 * the ring, the ring is full
    610  1.1  simonb 	 */
    611  1.1  simonb 
    612  1.1  simonb 	if (nextdsc == d->sbdma_remptr)
    613  1.1  simonb 		return ENOSPC;
    614  1.1  simonb 
    615  1.1  simonb #if 0
    616  1.1  simonb 	do {
    617  1.1  simonb 		struct mbuf *m0;
    618  1.1  simonb 
    619  1.1  simonb 		printf("mbuf chain: ");
    620  1.1  simonb 		for (m0 = m; m0 != 0; m0 = m0->m_next) {
    621  1.1  simonb 			printf("%d%c/%X ", m0->m_len,
    622  1.1  simonb 			m0->m_flags & M_EXT ? 'X' : 'N',
    623  1.1  simonb 			mtod(m0, u_int));
    624  1.1  simonb 		}
    625  1.1  simonb 		printf("\n");
    626  1.1  simonb 	} while (0);
    627  1.1  simonb #endif
    628  1.1  simonb 
    629  1.1  simonb 	/*
    630  1.1  simonb 	 * [BEGIN XXX]
    631  1.1  simonb 	 * XXX Copy/coalesce the mbufs into a single mbuf cluster (we assume
    632  1.1  simonb 	 * it will fit).  This is a temporary hack to get us going.
    633  1.1  simonb 	 */
    634  1.1  simonb 
    635  1.1  simonb 	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    636  1.1  simonb 	if (m_new == NULL) {
    637  1.1  simonb 		printf("%s: mbuf allocation failed\n",
    638  1.1  simonb 		d->sbdma_eth->sc_dev.dv_xname);
    639  1.1  simonb 		return ENOBUFS;
    640  1.1  simonb 	}
    641  1.1  simonb 
    642  1.1  simonb 	MCLGET(m_new, M_DONTWAIT);
    643  1.1  simonb 	if (!(m_new->m_flags & M_EXT)) {
    644  1.1  simonb 		printf("%s: mbuf cluster allocation failed\n",
    645  1.1  simonb 		d->sbdma_eth->sc_dev.dv_xname);
    646  1.1  simonb 		m_freem(m_new);
    647  1.1  simonb 		return ENOBUFS;
    648  1.1  simonb 	}
    649  1.1  simonb 
    650  1.1  simonb 	m_new->m_len = m_new->m_pkthdr.len= MCLBYTES;
    651  1.1  simonb 	/*m_adj(m_new, ETHER_ALIGN);*/
    652  1.1  simonb 
    653  1.1  simonb 	/*
    654  1.1  simonb 	 * XXX Don't forget to include the offset portion in the
    655  1.1  simonb 	 * XXX cache block calculation when this code is rewritten!
    656  1.1  simonb 	 */
    657  1.1  simonb 
    658  1.1  simonb 	/*
    659  1.1  simonb 	 * Copy data
    660  1.1  simonb 	 */
    661  1.1  simonb 
    662  1.1  simonb 	m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, caddr_t));
    663  1.1  simonb 	m_new->m_len = m_new->m_pkthdr.len = m->m_pkthdr.len;
    664  1.1  simonb 
    665  1.1  simonb 	/* Free old mbuf 'm', actual mbuf is now 'm_new' */
    666  1.1  simonb 
    667  1.1  simonb 	// XXX: CALLERS WILL FREE, they might have to bpf_mtap() if this
    668  1.1  simonb 	// XXX: function succeeds.
    669  1.1  simonb 	// m_freem(m);
    670  1.1  simonb 	length = m_new->m_len;
    671  1.1  simonb 
    672  1.1  simonb 	/* [END XXX] */
    673  1.1  simonb 
    674  1.1  simonb 	/*
    675  1.1  simonb 	 * fill in the descriptor
    676  1.1  simonb 	 */
    677  1.1  simonb 
    678  1.1  simonb 	dsc->dscr_a = KVTOPHYS(mtod(m_new, caddr_t)) |
    679  1.1  simonb 	    V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(m_new->m_len)) |
    680  1.1  simonb 	    M_DMA_DSCRA_INTERRUPT |
    681  1.1  simonb 	    M_DMA_ETHTX_SOP;
    682  1.1  simonb 
    683  1.1  simonb 	/* transmitting: set outbound options and length */
    684  1.1  simonb 	dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
    685  1.1  simonb 	    V_DMA_DSCRB_PKT_SIZE(length);
    686  1.1  simonb 
    687  1.1  simonb 	/*
    688  1.1  simonb 	 * fill in the context
    689  1.1  simonb 	 */
    690  1.1  simonb 
    691  1.1  simonb 	d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = m_new;
    692  1.1  simonb 
    693  1.1  simonb 	/*
    694  1.1  simonb 	 * point at next packet
    695  1.1  simonb 	 */
    696  1.1  simonb 
    697  1.1  simonb 	d->sbdma_addptr = nextdsc;
    698  1.1  simonb 
    699  1.1  simonb 	/*
    700  1.1  simonb 	 * Give the buffer to the DMA engine.
    701  1.1  simonb 	 */
    702  1.1  simonb 
    703  1.1  simonb 	SBMAC_WRITECSR(d->sbdma_dscrcnt, 1);
    704  1.1  simonb 
    705  1.1  simonb 	return 0;					/* we did it */
    706  1.1  simonb }
    707  1.1  simonb 
    708  1.1  simonb /*  *********************************************************************
    709  1.1  simonb     *  SBDMA_EMPTYRING(d)
    710  1.1  simonb     *
    711  1.1  simonb     *  Free all allocated mbufs on the specified DMA channel;
    712  1.1  simonb     *
    713  1.1  simonb     *  Input parameters:
    714  1.1  simonb     *  	   d  - DMA channel
    715  1.1  simonb     *
    716  1.1  simonb     *  Return value:
    717  1.1  simonb     *  	   nothing
    718  1.1  simonb     ********************************************************************* */
    719  1.1  simonb 
    720  1.1  simonb static void
    721  1.1  simonb sbdma_emptyring(sbmacdma_t *d)
    722  1.1  simonb {
    723  1.1  simonb 	int idx;
    724  1.1  simonb 	struct mbuf *m;
    725  1.1  simonb 
    726  1.1  simonb 	for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
    727  1.1  simonb 		m = d->sbdma_ctxtable[idx];
    728  1.1  simonb 		if (m) {
    729  1.1  simonb 			m_freem(m);
    730  1.1  simonb 			d->sbdma_ctxtable[idx] = NULL;
    731  1.1  simonb 		}
    732  1.1  simonb 	}
    733  1.1  simonb }
    734  1.1  simonb 
    735  1.1  simonb /*  *********************************************************************
    736  1.1  simonb     *  SBDMA_FILLRING(d)
    737  1.1  simonb     *
    738  1.1  simonb     *  Fill the specified DMA channel (must be receive channel)
    739  1.1  simonb     *  with mbufs
    740  1.1  simonb     *
    741  1.1  simonb     *  Input parameters:
    742  1.1  simonb     *  	   d - DMA channel
    743  1.1  simonb     *
    744  1.1  simonb     *  Return value:
    745  1.1  simonb     *  	   nothing
    746  1.1  simonb     ********************************************************************* */
    747  1.1  simonb 
    748  1.1  simonb static void
    749  1.1  simonb sbdma_fillring(sbmacdma_t *d)
    750  1.1  simonb {
    751  1.1  simonb 	int idx;
    752  1.1  simonb 
    753  1.1  simonb 	for (idx = 0; idx < SBMAC_MAX_RXDESCR-1; idx++)
    754  1.1  simonb 		if (sbdma_add_rcvbuffer(d, NULL) != 0)
    755  1.1  simonb 			break;
    756  1.1  simonb }
    757  1.1  simonb 
    758  1.1  simonb /*  *********************************************************************
    759  1.1  simonb     *  SBDMA_RX_PROCESS(sc, d)
    760  1.1  simonb     *
    761  1.1  simonb     *  Process "completed" receive buffers on the specified DMA channel.
    762  1.1  simonb     *  Note that this isn't really ideal for priority channels, since
    763  1.1  simonb     *  it processes all of the packets on a given channel before
    764  1.1  simonb     *  returning.
    765  1.1  simonb     *
    766  1.1  simonb     *  Input parameters:
    767  1.1  simonb     *	   sc - softc structure
    768  1.1  simonb     *  	   d - DMA channel context
    769  1.1  simonb     *
    770  1.1  simonb     *  Return value:
    771  1.1  simonb     *  	   nothing
    772  1.1  simonb     ********************************************************************* */
    773  1.1  simonb 
    774  1.1  simonb static void
    775  1.1  simonb sbdma_rx_process(struct sbmac_softc *sc, sbmacdma_t *d)
    776  1.1  simonb {
    777  1.1  simonb 	int curidx;
    778  1.1  simonb 	int hwidx;
    779  1.1  simonb 	sbdmadscr_t *dsc;
    780  1.1  simonb 	struct mbuf *m;
    781  1.1  simonb 	struct ether_header *eh;
    782  1.1  simonb 	int len;
    783  1.1  simonb 
    784  1.1  simonb 	struct ifnet *ifp = &(sc->sc_ethercom.ec_if);
    785  1.1  simonb 
    786  1.1  simonb 	for (;;) {
    787  1.1  simonb 		/*
    788  1.1  simonb 		 * figure out where we are (as an index) and where
    789  1.1  simonb 		 * the hardware is (also as an index)
    790  1.1  simonb 		 *
    791  1.1  simonb 		 * This could be done faster if (for example) the
    792  1.1  simonb 		 * descriptor table was page-aligned and contiguous in
    793  1.1  simonb 		 * both virtual and physical memory -- you could then
    794  1.1  simonb 		 * just compare the low-order bits of the virtual address
    795  1.1  simonb 		 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
    796  1.1  simonb 		 */
    797  1.1  simonb 
    798  1.1  simonb 		curidx = d->sbdma_remptr - d->sbdma_dscrtable;
    799  1.1  simonb 		hwidx = (int)
    800  1.1  simonb 		    (((SBMAC_READCSR(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
    801  1.1  simonb 		    d->sbdma_dscrtable_phys) / sizeof(sbdmadscr_t));
    802  1.1  simonb 
    803  1.1  simonb 		/*
    804  1.1  simonb 		 * If they're the same, that means we've processed all
    805  1.1  simonb 		 * of the descriptors up to (but not including) the one that
    806  1.1  simonb 		 * the hardware is working on right now.
    807  1.1  simonb 		 */
    808  1.1  simonb 
    809  1.1  simonb 		if (curidx == hwidx)
    810  1.1  simonb 			break;
    811  1.1  simonb 
    812  1.1  simonb 		/*
    813  1.1  simonb 		 * Otherwise, get the packet's mbuf ptr back
    814  1.1  simonb 		 */
    815  1.1  simonb 
    816  1.1  simonb 		dsc = &(d->sbdma_dscrtable[curidx]);
    817  1.1  simonb 		m = d->sbdma_ctxtable[curidx];
    818  1.1  simonb 		d->sbdma_ctxtable[curidx] = NULL;
    819  1.1  simonb 
    820  1.1  simonb 		len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
    821  1.1  simonb 
    822  1.1  simonb 		/*
    823  1.1  simonb 		 * Check packet status.  If good, process it.
    824  1.1  simonb 		 * If not, silently drop it and put it back on the
    825  1.1  simonb 		 * receive ring.
    826  1.1  simonb 		 */
    827  1.1  simonb 
    828  1.1  simonb 		if (! (dsc->dscr_a & M_DMA_ETHRX_BAD)) {
    829  1.1  simonb 
    830  1.1  simonb 			/*
    831  1.1  simonb 			 * Set length into the packet
    832  1.1  simonb 			 * XXX do we remove the CRC here?
    833  1.1  simonb 			 */
    834  1.1  simonb 			m->m_pkthdr.len = m->m_len = len;
    835  1.1  simonb 
    836  1.1  simonb 			ifp->if_ipackets++;
    837  1.1  simonb 			eh = mtod(m, struct ether_header *);
    838  1.1  simonb 			m->m_pkthdr.rcvif = ifp;
    839  1.1  simonb 
    840  1.1  simonb 
    841  1.1  simonb 			/*
    842  1.1  simonb 			 * Add a new buffer to replace the old one.
    843  1.1  simonb 			 */
    844  1.1  simonb 			sbdma_add_rcvbuffer(d, NULL);
    845  1.1  simonb 
    846  1.1  simonb #if (NBPFILTER > 0)
    847  1.1  simonb 			/*
    848  1.1  simonb 			 * Handle BPF listeners. Let the BPF user see the
    849  1.1  simonb 			 * packet, but don't pass it up to the ether_input()
    850  1.1  simonb 			 * layer unless it's a broadcast packet, multicast
    851  1.1  simonb 			 * packet, matches our ethernet address or the
    852  1.1  simonb 			 * interface is in promiscuous mode.
    853  1.1  simonb 			 */
    854  1.1  simonb 
    855  1.1  simonb 			if (ifp->if_bpf)
    856  1.1  simonb 				bpf_mtap(ifp->if_bpf, m);
    857  1.1  simonb #endif
    858  1.1  simonb 			/*
    859  1.1  simonb 			 * Pass the buffer to the kernel
    860  1.1  simonb 			 */
    861  1.1  simonb 			(*ifp->if_input)(ifp, m);
    862  1.1  simonb 		} else {
    863  1.1  simonb 			/*
    864  1.1  simonb 			 * Packet was mangled somehow.  Just drop it and
    865  1.1  simonb 			 * put it back on the receive ring.
    866  1.1  simonb 			 */
    867  1.1  simonb 			sbdma_add_rcvbuffer(d, m);
    868  1.1  simonb 		}
    869  1.1  simonb 
    870  1.1  simonb 		/*
    871  1.1  simonb 		 * .. and advance to the next buffer.
    872  1.1  simonb 		 */
    873  1.1  simonb 
    874  1.1  simonb 		d->sbdma_remptr = SBDMA_NEXTBUF(d, sbdma_remptr);
    875  1.1  simonb 	}
    876  1.1  simonb }
    877  1.1  simonb 
    878  1.1  simonb /*  *********************************************************************
    879  1.1  simonb     *  SBDMA_TX_PROCESS(sc, d)
    880  1.1  simonb     *
    881  1.1  simonb     *  Process "completed" transmit buffers on the specified DMA channel.
    882  1.1  simonb     *  This is normally called within the interrupt service routine.
    883  1.1  simonb     *  Note that this isn't really ideal for priority channels, since
    884  1.1  simonb     *  it processes all of the packets on a given channel before
    885  1.1  simonb     *  returning.
    886  1.1  simonb     *
    887  1.1  simonb     *  Input parameters:
    888  1.1  simonb     *      sc - softc structure
    889  1.1  simonb     *  	   d - DMA channel context
    890  1.1  simonb     *
    891  1.1  simonb     *  Return value:
    892  1.1  simonb     *  	   nothing
    893  1.1  simonb     ********************************************************************* */
    894  1.1  simonb 
    895  1.1  simonb static void
    896  1.1  simonb sbdma_tx_process(struct sbmac_softc *sc, sbmacdma_t *d)
    897  1.1  simonb {
    898  1.1  simonb 	int curidx;
    899  1.1  simonb 	int hwidx;
    900  1.1  simonb 	sbdmadscr_t *dsc;
    901  1.1  simonb 	struct mbuf *m;
    902  1.1  simonb 
    903  1.1  simonb 	struct ifnet *ifp = &(sc->sc_ethercom.ec_if);
    904  1.1  simonb 
    905  1.1  simonb 	for (;;) {
    906  1.1  simonb 		/*
    907  1.1  simonb 		 * figure out where we are (as an index) and where
    908  1.1  simonb 		 * the hardware is (also as an index)
    909  1.1  simonb 		 *
    910  1.1  simonb 		 * This could be done faster if (for example) the
    911  1.1  simonb 		 * descriptor table was page-aligned and contiguous in
    912  1.1  simonb 		 * both virtual and physical memory -- you could then
    913  1.1  simonb 		 * just compare the low-order bits of the virtual address
    914  1.1  simonb 		 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
    915  1.1  simonb 		 */
    916  1.1  simonb 
    917  1.1  simonb 		curidx = d->sbdma_remptr - d->sbdma_dscrtable;
    918  1.1  simonb 		hwidx = (int)
    919  1.1  simonb 		    (((SBMAC_READCSR(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
    920  1.1  simonb 		    d->sbdma_dscrtable_phys) / sizeof(sbdmadscr_t));
    921  1.1  simonb 
    922  1.1  simonb 		/*
    923  1.1  simonb 		 * If they're the same, that means we've processed all
    924  1.1  simonb 		 * of the descriptors up to (but not including) the one that
    925  1.1  simonb 		 * the hardware is working on right now.
    926  1.1  simonb 		 */
    927  1.1  simonb 
    928  1.1  simonb 		if (curidx == hwidx)
    929  1.1  simonb 			break;
    930  1.1  simonb 
    931  1.1  simonb 		/*
    932  1.1  simonb 		 * Otherwise, get the packet's mbuf ptr back
    933  1.1  simonb 		 */
    934  1.1  simonb 
    935  1.1  simonb 		dsc = &(d->sbdma_dscrtable[curidx]);
    936  1.1  simonb 		m = d->sbdma_ctxtable[curidx];
    937  1.1  simonb 		d->sbdma_ctxtable[curidx] = NULL;
    938  1.1  simonb 
    939  1.1  simonb 		/*
    940  1.1  simonb 		 * for transmits, we just free buffers.
    941  1.1  simonb 		 */
    942  1.1  simonb 
    943  1.1  simonb 		m_freem(m);
    944  1.1  simonb 
    945  1.1  simonb 		/*
    946  1.1  simonb 		 * .. and advance to the next buffer.
    947  1.1  simonb 		 */
    948  1.1  simonb 
    949  1.1  simonb 		d->sbdma_remptr = SBDMA_NEXTBUF(d, sbdma_remptr);
    950  1.1  simonb 	}
    951  1.1  simonb 
    952  1.1  simonb 	/*
    953  1.1  simonb 	 * Decide what to set the IFF_OACTIVE bit in the interface to.
    954  1.1  simonb 	 * It's supposed to reflect if the interface is actively
    955  1.1  simonb 	 * transmitting, but that's really hard to do quickly.
    956  1.1  simonb 	 */
    957  1.1  simonb 
    958  1.1  simonb 	ifp->if_flags &= ~IFF_OACTIVE;
    959  1.1  simonb }
    960  1.1  simonb 
    961  1.1  simonb 
    962  1.1  simonb 
    963  1.1  simonb /*  *********************************************************************
    964  1.1  simonb     *  SBMAC_INITCTX(s)
    965  1.1  simonb     *
    966  1.1  simonb     *  Initialize an Ethernet context structure - this is called
    967  1.1  simonb     *  once per MAC on the 1250.  Memory is allocated here, so don't
    968  1.1  simonb     *  call it again from inside the ioctl routines that bring the
    969  1.1  simonb     *  interface up/down
    970  1.1  simonb     *
    971  1.1  simonb     *  Input parameters:
    972  1.1  simonb     *  	   s - sbmac context structure
    973  1.1  simonb     *
    974  1.1  simonb     *  Return value:
    975  1.1  simonb     *  	   0
    976  1.1  simonb     ********************************************************************* */
    977  1.1  simonb 
    978  1.1  simonb static void
    979  1.1  simonb sbmac_initctx(struct sbmac_softc *s)
    980  1.1  simonb {
    981  1.1  simonb 
    982  1.1  simonb 	/*
    983  1.1  simonb 	 * figure out the addresses of some ports
    984  1.1  simonb 	 */
    985  1.1  simonb 
    986  1.1  simonb 	s->sbm_macenable = PKSEG1(s->sbm_base + R_MAC_ENABLE);
    987  1.1  simonb 	s->sbm_maccfg    = PKSEG1(s->sbm_base + R_MAC_CFG);
    988  1.1  simonb 	s->sbm_fifocfg   = PKSEG1(s->sbm_base + R_MAC_THRSH_CFG);
    989  1.1  simonb 	s->sbm_framecfg  = PKSEG1(s->sbm_base + R_MAC_FRAMECFG);
    990  1.1  simonb 	s->sbm_rxfilter  = PKSEG1(s->sbm_base + R_MAC_ADFILTER_CFG);
    991  1.1  simonb 	s->sbm_isr       = PKSEG1(s->sbm_base + R_MAC_STATUS);
    992  1.1  simonb 	s->sbm_imr       = PKSEG1(s->sbm_base + R_MAC_INT_MASK);
    993  1.1  simonb 
    994  1.1  simonb 	/*
    995  1.1  simonb 	 * Initialize the DMA channels.  Right now, only one per MAC is used
    996  1.1  simonb 	 * Note: Only do this _once_, as it allocates memory from the kernel!
    997  1.1  simonb 	 */
    998  1.1  simonb 
    999  1.1  simonb 	sbdma_initctx(&(s->sbm_txdma), s, 0, DMA_TX, SBMAC_MAX_TXDESCR);
   1000  1.1  simonb 	sbdma_initctx(&(s->sbm_rxdma), s, 0, DMA_RX, SBMAC_MAX_RXDESCR);
   1001  1.1  simonb 
   1002  1.1  simonb 	/*
   1003  1.1  simonb 	 * initial state is OFF
   1004  1.1  simonb 	 */
   1005  1.1  simonb 
   1006  1.1  simonb 	s->sbm_state = sbmac_state_off;
   1007  1.1  simonb 
   1008  1.1  simonb 	/*
   1009  1.1  simonb 	 * Initial speed is (XXX TEMP) 10MBit/s HDX no FC
   1010  1.1  simonb 	 */
   1011  1.1  simonb 
   1012  1.1  simonb 	s->sbm_speed = sbmac_speed_10;
   1013  1.1  simonb 	s->sbm_duplex = sbmac_duplex_half;
   1014  1.1  simonb 	s->sbm_fc = sbmac_fc_disabled;
   1015  1.1  simonb }
   1016  1.1  simonb 
   1017  1.1  simonb 
   1018  1.1  simonb /*  *********************************************************************
   1019  1.1  simonb     *  SBMAC_CHANNEL_START(s)
   1020  1.1  simonb     *
   1021  1.1  simonb     *  Start packet processing on this MAC.
   1022  1.1  simonb     *
   1023  1.1  simonb     *  Input parameters:
   1024  1.1  simonb     *  	   s - sbmac structure
   1025  1.1  simonb     *
   1026  1.1  simonb     *  Return value:
   1027  1.1  simonb     *  	   nothing
   1028  1.1  simonb     ********************************************************************* */
   1029  1.1  simonb 
   1030  1.1  simonb static void
   1031  1.1  simonb sbmac_channel_start(struct sbmac_softc *s)
   1032  1.1  simonb {
   1033  1.1  simonb 	uint64_t reg;
   1034  1.1  simonb 	sbmac_port_t port;
   1035  1.1  simonb 	uint64_t cfg, fifo, framecfg;
   1036  1.1  simonb 	int idx;
   1037  1.1  simonb 
   1038  1.1  simonb 	/*
   1039  1.1  simonb 	 * Don't do this if running
   1040  1.1  simonb 	 */
   1041  1.1  simonb 
   1042  1.1  simonb 	if (s->sbm_state == sbmac_state_on)
   1043  1.1  simonb 		return;
   1044  1.1  simonb 
   1045  1.1  simonb 	/*
   1046  1.1  simonb 	 * Bring the controller out of reset, but leave it off.
   1047  1.1  simonb 	 */
   1048  1.1  simonb 
   1049  1.1  simonb 	SBMAC_WRITECSR(s->sbm_macenable, 0);
   1050  1.1  simonb 
   1051  1.1  simonb 	/*
   1052  1.1  simonb 	 * Ignore all received packets
   1053  1.1  simonb 	 */
   1054  1.1  simonb 
   1055  1.1  simonb 	SBMAC_WRITECSR(s->sbm_rxfilter, 0);
   1056  1.1  simonb 
   1057  1.1  simonb 	/*
   1058  1.1  simonb 	 * Calculate values for various control registers.
   1059  1.1  simonb 	 */
   1060  1.1  simonb 
   1061  1.1  simonb 	cfg = M_MAC_RETRY_EN |
   1062  1.1  simonb 	      M_MAC_TX_HOLD_SOP_EN |
   1063  1.1  simonb 	      V_MAC_TX_PAUSE_CNT_16K |
   1064  1.1  simonb 	      M_MAC_AP_STAT_EN |
   1065  1.1  simonb 	      M_MAC_SS_EN |
   1066  1.1  simonb 	      0;
   1067  1.1  simonb 
   1068  1.1  simonb 	fifo = V_MAC_TX_WR_THRSH(4) |	/* Must be '4' or '8' */
   1069  1.1  simonb 	       V_MAC_TX_RD_THRSH(4) |
   1070  1.1  simonb 	       V_MAC_TX_RL_THRSH(4) |
   1071  1.1  simonb 	       V_MAC_RX_PL_THRSH(4) |
   1072  1.1  simonb 	       V_MAC_RX_RD_THRSH(4) |	/* Must be '4' */
   1073  1.1  simonb 	       V_MAC_RX_PL_THRSH(4) |
   1074  1.1  simonb 	       V_MAC_RX_RL_THRSH(8) |
   1075  1.1  simonb 	       0;
   1076  1.1  simonb 
   1077  1.1  simonb 	framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
   1078  1.1  simonb 	    V_MAC_MAX_FRAMESZ_DEFAULT |
   1079  1.1  simonb 	    V_MAC_BACKOFF_SEL(1);
   1080  1.1  simonb 
   1081  1.1  simonb 	/*
   1082  1.1  simonb 	 * Clear out the hash address map
   1083  1.1  simonb 	 */
   1084  1.1  simonb 
   1085  1.1  simonb 	port = PKSEG1(s->sbm_base + R_MAC_HASH_BASE);
   1086  1.1  simonb 	for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
   1087  1.1  simonb 		SBMAC_WRITECSR(port, 0);
   1088  1.1  simonb 		port += sizeof(uint64_t);
   1089  1.1  simonb 	}
   1090  1.1  simonb 
   1091  1.1  simonb 	/*
   1092  1.1  simonb 	 * Clear out the exact-match table
   1093  1.1  simonb 	 */
   1094  1.1  simonb 
   1095  1.1  simonb 	port = PKSEG1(s->sbm_base + R_MAC_ADDR_BASE);
   1096  1.1  simonb 	for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
   1097  1.1  simonb 		SBMAC_WRITECSR(port, 0);
   1098  1.1  simonb 		port += sizeof(uint64_t);
   1099  1.1  simonb 	}
   1100  1.1  simonb 
   1101  1.1  simonb 	/*
   1102  1.1  simonb 	 * Clear out the DMA Channel mapping table registers
   1103  1.1  simonb 	 */
   1104  1.1  simonb 
   1105  1.1  simonb 	port = PKSEG1(s->sbm_base + R_MAC_CHUP0_BASE);
   1106  1.1  simonb 	for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
   1107  1.1  simonb 		SBMAC_WRITECSR(port, 0);
   1108  1.1  simonb 		port += sizeof(uint64_t);
   1109  1.1  simonb 	}
   1110  1.1  simonb 
   1111  1.1  simonb 	port = PKSEG1(s->sbm_base + R_MAC_CHLO0_BASE);
   1112  1.1  simonb 	for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
   1113  1.1  simonb 		SBMAC_WRITECSR(port, 0);
   1114  1.1  simonb 		port += sizeof(uint64_t);
   1115  1.1  simonb 	}
   1116  1.1  simonb 
   1117  1.1  simonb 	/*
   1118  1.1  simonb 	 * Program the hardware address.  It goes into the hardware-address
   1119  1.1  simonb 	 * register as well as the first filter register.
   1120  1.1  simonb 	 */
   1121  1.1  simonb 
   1122  1.1  simonb 	reg = sbmac_addr2reg(s->sbm_hwaddr);
   1123  1.1  simonb 
   1124  1.1  simonb 	port = PKSEG1(s->sbm_base + R_MAC_ADDR_BASE);
   1125  1.1  simonb 	SBMAC_WRITECSR(port, reg);
   1126  1.1  simonb 	port = PKSEG1(s->sbm_base + R_MAC_ETHERNET_ADDR);
   1127  1.1  simonb 	SBMAC_WRITECSR(port, 0);			// pass1 workaround
   1128  1.1  simonb 
   1129  1.1  simonb 	/*
   1130  1.1  simonb 	 * Set the receive filter for no packets, and write values
   1131  1.1  simonb 	 * to the various config registers
   1132  1.1  simonb 	 */
   1133  1.1  simonb 
   1134  1.1  simonb 	SBMAC_WRITECSR(s->sbm_rxfilter, 0);
   1135  1.1  simonb 	SBMAC_WRITECSR(s->sbm_imr, 0);
   1136  1.1  simonb 	SBMAC_WRITECSR(s->sbm_framecfg, framecfg);
   1137  1.1  simonb 	SBMAC_WRITECSR(s->sbm_fifocfg, fifo);
   1138  1.1  simonb 	SBMAC_WRITECSR(s->sbm_maccfg, cfg);
   1139  1.1  simonb 
   1140  1.1  simonb 	/*
   1141  1.1  simonb 	 * Initialize DMA channels (rings should be ok now)
   1142  1.1  simonb 	 */
   1143  1.1  simonb 
   1144  1.1  simonb 	sbdma_channel_start(&(s->sbm_rxdma));
   1145  1.1  simonb 	sbdma_channel_start(&(s->sbm_txdma));
   1146  1.1  simonb 
   1147  1.1  simonb 	/*
   1148  1.1  simonb 	 * Configure the speed, duplex, and flow control
   1149  1.1  simonb 	 */
   1150  1.1  simonb 
   1151  1.1  simonb 	sbmac_set_speed(s, s->sbm_speed);
   1152  1.1  simonb 	sbmac_set_duplex(s, s->sbm_duplex, s->sbm_fc);
   1153  1.1  simonb 
   1154  1.1  simonb 	/*
   1155  1.1  simonb 	 * Fill the receive ring
   1156  1.1  simonb 	 */
   1157  1.1  simonb 
   1158  1.1  simonb 	sbdma_fillring(&(s->sbm_rxdma));
   1159  1.1  simonb 
   1160  1.1  simonb 	/*
   1161  1.1  simonb 	 * Turn on the rest of the bits in the enable register
   1162  1.1  simonb 	 */
   1163  1.1  simonb 
   1164  1.1  simonb 	SBMAC_WRITECSR(s->sbm_macenable, M_MAC_RXDMA_EN0 | M_MAC_TXDMA_EN0 |
   1165  1.1  simonb 	    M_MAC_RX_ENABLE | M_MAC_TX_ENABLE);
   1166  1.1  simonb 
   1167  1.1  simonb 
   1168  1.1  simonb 	/*
   1169  1.1  simonb 	 * Accept any kind of interrupt on TX and RX DMA channel 0
   1170  1.1  simonb 	 */
   1171  1.1  simonb 	SBMAC_WRITECSR(s->sbm_imr,
   1172  1.1  simonb 	    (M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
   1173  1.1  simonb 	    (M_MAC_INT_CHANNEL << S_MAC_RX_CH0));
   1174  1.1  simonb 
   1175  1.1  simonb 	/*
   1176  1.1  simonb 	 * Enable receiving unicasts and broadcasts
   1177  1.1  simonb 	 */
   1178  1.1  simonb 
   1179  1.1  simonb 	SBMAC_WRITECSR(s->sbm_rxfilter, M_MAC_UCAST_EN | M_MAC_BCAST_EN);
   1180  1.1  simonb 
   1181  1.1  simonb 	/*
   1182  1.1  simonb 	 * we're running now.
   1183  1.1  simonb 	 */
   1184  1.1  simonb 
   1185  1.1  simonb 	s->sbm_state = sbmac_state_on;
   1186  1.1  simonb 	s->sc_ethercom.ec_if.if_flags |= IFF_RUNNING;
   1187  1.1  simonb 
   1188  1.1  simonb 	/*
   1189  1.1  simonb 	 * Program multicast addresses
   1190  1.1  simonb 	 */
   1191  1.1  simonb 
   1192  1.1  simonb 	sbmac_setmulti(s);
   1193  1.1  simonb 
   1194  1.1  simonb 	/*
   1195  1.1  simonb 	 * If channel was in promiscuous mode before, turn that on
   1196  1.1  simonb 	 */
   1197  1.1  simonb 
   1198  1.1  simonb 	if (s->sc_ethercom.ec_if.if_flags & IFF_PROMISC)
   1199  1.1  simonb 		sbmac_promiscuous_mode(s, 1);
   1200  1.1  simonb 
   1201  1.1  simonb 	/*
   1202  1.1  simonb 	 * Turn on the once-per-second timer
   1203  1.1  simonb 	 */
   1204  1.1  simonb 
   1205  1.1  simonb 	callout_reset(&(s->sc_tick_ch), hz, sbmac_tick, s);
   1206  1.1  simonb }
   1207  1.1  simonb 
   1208  1.1  simonb 
   1209  1.1  simonb /*  *********************************************************************
   1210  1.1  simonb     *  SBMAC_CHANNEL_STOP(s)
   1211  1.1  simonb     *
   1212  1.1  simonb     *  Stop packet processing on this MAC.
   1213  1.1  simonb     *
   1214  1.1  simonb     *  Input parameters:
   1215  1.1  simonb     *  	   s - sbmac structure
   1216  1.1  simonb     *
   1217  1.1  simonb     *  Return value:
   1218  1.1  simonb     *  	   nothing
   1219  1.1  simonb     ********************************************************************* */
   1220  1.1  simonb 
   1221  1.1  simonb static void sbmac_channel_stop(struct sbmac_softc *s)
   1222  1.1  simonb {
   1223  1.1  simonb     uint64_t ctl;
   1224  1.1  simonb 
   1225  1.1  simonb     /* don't do this if already stopped */
   1226  1.1  simonb 
   1227  1.1  simonb     if (s->sbm_state == sbmac_state_off) return;
   1228  1.1  simonb 
   1229  1.1  simonb     /* don't accept any packets, disable all interrupts */
   1230  1.1  simonb 
   1231  1.1  simonb     SBMAC_WRITECSR(s->sbm_rxfilter, 0);
   1232  1.1  simonb     SBMAC_WRITECSR(s->sbm_imr, 0);
   1233  1.1  simonb 
   1234  1.1  simonb     /* Turn off ticker */
   1235  1.1  simonb 
   1236  1.1  simonb     callout_stop(&(s->sc_tick_ch));
   1237  1.1  simonb 
   1238  1.1  simonb     /* turn off receiver and transmitter */
   1239  1.1  simonb 
   1240  1.1  simonb     ctl = SBMAC_READCSR(s->sbm_macenable);
   1241  1.1  simonb     ctl &= ~(M_MAC_RXDMA_EN0 | M_MAC_TXDMA_EN0);
   1242  1.1  simonb     SBMAC_WRITECSR(s->sbm_macenable, ctl);
   1243  1.1  simonb 
   1244  1.1  simonb     /* We're stopped now. */
   1245  1.1  simonb 
   1246  1.1  simonb     s->sbm_state = sbmac_state_off;
   1247  1.1  simonb     s->sc_ethercom.ec_if.if_flags &= ~IFF_RUNNING;
   1248  1.1  simonb 
   1249  1.1  simonb     /* Empty the receive and transmit rings */
   1250  1.1  simonb 
   1251  1.1  simonb     sbdma_emptyring(&(s->sbm_rxdma));
   1252  1.1  simonb     sbdma_emptyring(&(s->sbm_txdma));
   1253  1.1  simonb 
   1254  1.1  simonb }
   1255  1.1  simonb 
   1256  1.1  simonb /*  *********************************************************************
   1257  1.1  simonb     *  SBMAC_SET_CHANNEL_STATE(state)
   1258  1.1  simonb     *
   1259  1.1  simonb     *  Set the channel's state ON or OFF
   1260  1.1  simonb     *
   1261  1.1  simonb     *  Input parameters:
   1262  1.1  simonb     *  	   state - new state
   1263  1.1  simonb     *
   1264  1.1  simonb     *  Return value:
   1265  1.1  simonb     *  	   old state
   1266  1.1  simonb     ********************************************************************* */
   1267  1.1  simonb static sbmac_state_t sbmac_set_channel_state(struct sbmac_softc *sc,
   1268  1.1  simonb 					     sbmac_state_t state)
   1269  1.1  simonb {
   1270  1.1  simonb     sbmac_state_t oldstate = sc->sbm_state;
   1271  1.1  simonb 
   1272  1.1  simonb     /*
   1273  1.1  simonb      * If same as previous state, return
   1274  1.1  simonb      */
   1275  1.1  simonb 
   1276  1.1  simonb     if (state == oldstate) {
   1277  1.1  simonb 	return oldstate;
   1278  1.1  simonb 	}
   1279  1.1  simonb 
   1280  1.1  simonb     /*
   1281  1.1  simonb      * If new state is ON, turn channel on
   1282  1.1  simonb      */
   1283  1.1  simonb 
   1284  1.1  simonb     if (state == sbmac_state_on) {
   1285  1.1  simonb 	sbmac_channel_start(sc);
   1286  1.1  simonb 	}
   1287  1.1  simonb     else {
   1288  1.1  simonb 	sbmac_channel_stop(sc);
   1289  1.1  simonb 	}
   1290  1.1  simonb 
   1291  1.1  simonb     /*
   1292  1.1  simonb      * Return previous state
   1293  1.1  simonb      */
   1294  1.1  simonb 
   1295  1.1  simonb     return oldstate;
   1296  1.1  simonb }
   1297  1.1  simonb 
   1298  1.1  simonb 
   1299  1.1  simonb /*  *********************************************************************
   1300  1.1  simonb     *  SBMAC_PROMISCUOUS_MODE(sc, onoff)
   1301  1.1  simonb     *
   1302  1.1  simonb     *  Turn on or off promiscuous mode
   1303  1.1  simonb     *
   1304  1.1  simonb     *  Input parameters:
   1305  1.1  simonb     *  	   sc - softc
   1306  1.1  simonb     *      onoff - 1 to turn on, 0 to turn off
   1307  1.1  simonb     *
   1308  1.1  simonb     *  Return value:
   1309  1.1  simonb     *  	   nothing
   1310  1.1  simonb     ********************************************************************* */
   1311  1.1  simonb 
   1312  1.1  simonb static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff)
   1313  1.1  simonb {
   1314  1.1  simonb     uint64_t reg;
   1315  1.1  simonb 
   1316  1.1  simonb     if (sc->sbm_state != sbmac_state_on) return;
   1317  1.1  simonb 
   1318  1.1  simonb     if (onoff) {
   1319  1.1  simonb 	reg = SBMAC_READCSR(sc->sbm_rxfilter);
   1320  1.1  simonb 	reg |= M_MAC_ALLPKT_EN;
   1321  1.1  simonb 	SBMAC_WRITECSR(sc->sbm_rxfilter, reg);
   1322  1.1  simonb 	}
   1323  1.1  simonb     else {
   1324  1.1  simonb 	reg = SBMAC_READCSR(sc->sbm_rxfilter);
   1325  1.1  simonb 	reg &= ~M_MAC_ALLPKT_EN;
   1326  1.1  simonb 	SBMAC_WRITECSR(sc->sbm_rxfilter, reg);
   1327  1.1  simonb 	}
   1328  1.1  simonb }
   1329  1.1  simonb 
   1330  1.1  simonb 
   1331  1.1  simonb 
   1332  1.1  simonb /*  *********************************************************************
   1333  1.1  simonb     *  SBMAC_INIT_AND_START(sc)
   1334  1.1  simonb     *
   1335  1.1  simonb     *  Stop the channel and restart it.  This is generally used
   1336  1.1  simonb     *  when we have to do something to the channel that requires
   1337  1.1  simonb     *  a swift kick.
   1338  1.1  simonb     *
   1339  1.1  simonb     *  Input parameters:
   1340  1.1  simonb     *  	   sc - softc
   1341  1.1  simonb     ********************************************************************* */
   1342  1.1  simonb 
   1343  1.1  simonb static void sbmac_init_and_start(struct sbmac_softc *sc)
   1344  1.1  simonb {
   1345  1.1  simonb     int s;
   1346  1.1  simonb 
   1347  1.1  simonb     s = splnet();
   1348  1.1  simonb 
   1349  1.1  simonb     mii_pollstat(&sc->sc_mii);			/* poll phy for current speed */
   1350  1.1  simonb     sbmac_mii_statchg((struct device *) sc);	/* set state to new speed */
   1351  1.1  simonb     sbmac_set_channel_state(sc, sbmac_state_on);
   1352  1.1  simonb 
   1353  1.1  simonb     splx(s);
   1354  1.1  simonb }
   1355  1.1  simonb 
   1356  1.1  simonb /*  *********************************************************************
   1357  1.1  simonb     *  SBMAC_ADDR2REG(ptr)
   1358  1.1  simonb     *
   1359  1.1  simonb     *  Convert six bytes into the 64-bit register value that
   1360  1.1  simonb     *  we typically write into the SBMAC's address/mcast registers
   1361  1.1  simonb     *
   1362  1.1  simonb     *  Input parameters:
   1363  1.1  simonb     *  	   ptr - pointer to 6 bytes
   1364  1.1  simonb     *
   1365  1.1  simonb     *  Return value:
   1366  1.1  simonb     *  	   register value
   1367  1.1  simonb     ********************************************************************* */
   1368  1.1  simonb 
   1369  1.1  simonb static uint64_t sbmac_addr2reg(u_char *ptr)
   1370  1.1  simonb {
   1371  1.1  simonb     uint64_t reg = 0;
   1372  1.1  simonb 
   1373  1.1  simonb     ptr += 6;
   1374  1.1  simonb 
   1375  1.1  simonb     reg |= (uint64_t) *(--ptr);
   1376  1.1  simonb     reg <<= 8;
   1377  1.1  simonb     reg |= (uint64_t) *(--ptr);
   1378  1.1  simonb     reg <<= 8;
   1379  1.1  simonb     reg |= (uint64_t) *(--ptr);
   1380  1.1  simonb     reg <<= 8;
   1381  1.1  simonb     reg |= (uint64_t) *(--ptr);
   1382  1.1  simonb     reg <<= 8;
   1383  1.1  simonb     reg |= (uint64_t) *(--ptr);
   1384  1.1  simonb     reg <<= 8;
   1385  1.1  simonb     reg |= (uint64_t) *(--ptr);
   1386  1.1  simonb 
   1387  1.1  simonb     return reg;
   1388  1.1  simonb }
   1389  1.1  simonb 
   1390  1.1  simonb 
   1391  1.1  simonb /*  *********************************************************************
   1392  1.1  simonb     *  SBMAC_SET_SPEED(s, speed)
   1393  1.1  simonb     *
   1394  1.1  simonb     *  Configure LAN speed for the specified MAC.
   1395  1.1  simonb     *  Warning: must be called when MAC is off!
   1396  1.1  simonb     *
   1397  1.1  simonb     *  Input parameters:
   1398  1.1  simonb     *  	   s - sbmac structure
   1399  1.1  simonb     *  	   speed - speed to set MAC to (see sbmac_speed_t enum)
   1400  1.1  simonb     *
   1401  1.1  simonb     *  Return value:
   1402  1.1  simonb     *  	   1 if successful
   1403  1.1  simonb     *      0 indicates invalid parameters
   1404  1.1  simonb     ********************************************************************* */
   1405  1.1  simonb 
   1406  1.1  simonb static int sbmac_set_speed(struct sbmac_softc *s, sbmac_speed_t speed)
   1407  1.1  simonb {
   1408  1.1  simonb     uint64_t cfg;
   1409  1.1  simonb     uint64_t framecfg;
   1410  1.1  simonb 
   1411  1.1  simonb     /*
   1412  1.1  simonb      * Save new current values
   1413  1.1  simonb      */
   1414  1.1  simonb 
   1415  1.1  simonb     s->sbm_speed = speed;
   1416  1.1  simonb 
   1417  1.1  simonb     if (s->sbm_state != sbmac_state_off)
   1418  1.1  simonb 	panic("sbmac_set_speed while MAC not off");
   1419  1.1  simonb 
   1420  1.1  simonb     /*
   1421  1.1  simonb      * Read current register values
   1422  1.1  simonb      */
   1423  1.1  simonb 
   1424  1.1  simonb     cfg = SBMAC_READCSR(s->sbm_maccfg);
   1425  1.1  simonb     framecfg = SBMAC_READCSR(s->sbm_framecfg);
   1426  1.1  simonb 
   1427  1.1  simonb     /*
   1428  1.1  simonb      * Mask out the stuff we want to change
   1429  1.1  simonb      */
   1430  1.1  simonb 
   1431  1.1  simonb     cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
   1432  1.1  simonb     framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
   1433  1.1  simonb 		  M_MAC_SLOT_SIZE);
   1434  1.1  simonb 
   1435  1.1  simonb     /*
   1436  1.1  simonb      * Now add in the new bits
   1437  1.1  simonb      */
   1438  1.1  simonb 
   1439  1.1  simonb     switch (speed) {
   1440  1.1  simonb 	case sbmac_speed_10:
   1441  1.1  simonb 	    framecfg |= V_MAC_IFG_RX_10 |
   1442  1.1  simonb 		V_MAC_IFG_TX_10 |
   1443  1.1  simonb 		K_MAC_IFG_THRSH_10 |
   1444  1.1  simonb 		V_MAC_SLOT_SIZE_10;
   1445  1.1  simonb 	    cfg |= V_MAC_SPEED_SEL_10MBPS;
   1446  1.1  simonb 	    break;
   1447  1.1  simonb 
   1448  1.1  simonb 	case sbmac_speed_100:
   1449  1.1  simonb 	    framecfg |= V_MAC_IFG_RX_100 |
   1450  1.1  simonb 		V_MAC_IFG_TX_100 |
   1451  1.1  simonb 		V_MAC_IFG_THRSH_100 |
   1452  1.1  simonb 		V_MAC_SLOT_SIZE_100;
   1453  1.1  simonb 	    cfg |= V_MAC_SPEED_SEL_100MBPS ;
   1454  1.1  simonb 	    break;
   1455  1.1  simonb 
   1456  1.1  simonb 	case sbmac_speed_1000:
   1457  1.1  simonb 	    framecfg |= V_MAC_IFG_RX_1000 |
   1458  1.1  simonb 		V_MAC_IFG_TX_1000 |
   1459  1.1  simonb 		V_MAC_IFG_THRSH_1000 |
   1460  1.1  simonb 		V_MAC_SLOT_SIZE_1000;
   1461  1.1  simonb 	    cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
   1462  1.1  simonb 	    break;
   1463  1.1  simonb 
   1464  1.1  simonb 	case sbmac_speed_auto:		/* XXX not implemented */
   1465  1.1  simonb 	    /* fall through */
   1466  1.1  simonb 	default:
   1467  1.1  simonb 	    return 0;
   1468  1.1  simonb 	}
   1469  1.1  simonb 
   1470  1.1  simonb     /*
   1471  1.1  simonb      * Send the bits back to the hardware
   1472  1.1  simonb      */
   1473  1.1  simonb 
   1474  1.1  simonb     SBMAC_WRITECSR(s->sbm_framecfg, framecfg);
   1475  1.1  simonb     SBMAC_WRITECSR(s->sbm_maccfg, cfg);
   1476  1.1  simonb 
   1477  1.1  simonb     return 1;
   1478  1.1  simonb 
   1479  1.1  simonb }
   1480  1.1  simonb 
   1481  1.1  simonb /*  *********************************************************************
   1482  1.1  simonb     *  SBMAC_SET_DUPLEX(s, duplex, fc)
   1483  1.1  simonb     *
   1484  1.1  simonb     *  Set Ethernet duplex and flow control options for this MAC
   1485  1.1  simonb     *  Warning: must be called when MAC is off!
   1486  1.1  simonb     *
   1487  1.1  simonb     *  Input parameters:
   1488  1.1  simonb     *  	   s - sbmac structure
   1489  1.1  simonb     *  	   duplex - duplex setting (see sbmac_duplex_t)
   1490  1.1  simonb     *  	   fc - flow control setting (see sbmac_fc_t)
   1491  1.1  simonb     *
   1492  1.1  simonb     *  Return value:
   1493  1.1  simonb     *  	   1 if ok
   1494  1.1  simonb     *  	   0 if an invalid parameter combination was specified
   1495  1.1  simonb     ********************************************************************* */
   1496  1.1  simonb 
   1497  1.1  simonb static int sbmac_set_duplex(struct sbmac_softc *s, sbmac_duplex_t duplex, sbmac_fc_t fc)
   1498  1.1  simonb {
   1499  1.1  simonb     uint64_t cfg;
   1500  1.1  simonb 
   1501  1.1  simonb     /*
   1502  1.1  simonb      * Save new current values
   1503  1.1  simonb      */
   1504  1.1  simonb 
   1505  1.1  simonb     s->sbm_duplex = duplex;
   1506  1.1  simonb     s->sbm_fc = fc;
   1507  1.1  simonb 
   1508  1.1  simonb     if (s->sbm_state != sbmac_state_off)
   1509  1.1  simonb 	panic("sbmac_set_duplex while MAC not off");
   1510  1.1  simonb 
   1511  1.1  simonb     /*
   1512  1.1  simonb      * Read current register values
   1513  1.1  simonb      */
   1514  1.1  simonb 
   1515  1.1  simonb     cfg = SBMAC_READCSR(s->sbm_maccfg);
   1516  1.1  simonb 
   1517  1.1  simonb     /*
   1518  1.1  simonb      * Mask off the stuff we're about to change
   1519  1.1  simonb      */
   1520  1.1  simonb 
   1521  1.1  simonb     cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
   1522  1.1  simonb 
   1523  1.1  simonb 
   1524  1.1  simonb     switch (duplex) {
   1525  1.1  simonb 	case sbmac_duplex_half:
   1526  1.1  simonb 	    switch (fc) {
   1527  1.1  simonb 		case sbmac_fc_disabled:
   1528  1.1  simonb 		    cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
   1529  1.1  simonb 		    break;
   1530  1.1  simonb 
   1531  1.1  simonb 		case sbmac_fc_collision:
   1532  1.1  simonb 		    cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
   1533  1.1  simonb 		    break;
   1534  1.1  simonb 
   1535  1.1  simonb 		case sbmac_fc_carrier:
   1536  1.1  simonb 		    cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
   1537  1.1  simonb 		    break;
   1538  1.1  simonb 
   1539  1.1  simonb 		case sbmac_fc_auto:		/* XXX not implemented */
   1540  1.1  simonb 		    /* fall through */
   1541  1.1  simonb 		case sbmac_fc_frame:		/* not valid in half duplex */
   1542  1.1  simonb 		default:			/* invalid selection */
   1543  1.1  simonb 		    panic("%s: invalid half duplex fc selection %d\n",
   1544  1.1  simonb 			s->sc_dev.dv_xname, fc);
   1545  1.1  simonb 		    return 0;
   1546  1.1  simonb 		}
   1547  1.1  simonb 	    break;
   1548  1.1  simonb 
   1549  1.1  simonb 	case sbmac_duplex_full:
   1550  1.1  simonb 	    switch (fc) {
   1551  1.1  simonb 		case sbmac_fc_disabled:
   1552  1.1  simonb 		    cfg |= V_MAC_FC_CMD_DISABLED;
   1553  1.1  simonb 		    break;
   1554  1.1  simonb 
   1555  1.1  simonb 		case sbmac_fc_frame:
   1556  1.1  simonb 		    cfg |= V_MAC_FC_CMD_ENABLED;
   1557  1.1  simonb 		    break;
   1558  1.1  simonb 
   1559  1.1  simonb 		case sbmac_fc_collision:	/* not valid in full duplex */
   1560  1.1  simonb 		case sbmac_fc_carrier:		/* not valid in full duplex */
   1561  1.1  simonb 		case sbmac_fc_auto:		/* XXX not implemented */
   1562  1.1  simonb 		    /* fall through */
   1563  1.1  simonb 		default:
   1564  1.1  simonb 		    panic("%s: invalid full duplex fc selection %d\n",
   1565  1.1  simonb 			s->sc_dev.dv_xname, fc);
   1566  1.1  simonb 		    return 0;
   1567  1.1  simonb 		}
   1568  1.1  simonb 	    break;
   1569  1.1  simonb 
   1570  1.1  simonb 	default:
   1571  1.1  simonb 	    /* fall through */
   1572  1.1  simonb 	case sbmac_duplex_auto:
   1573  1.1  simonb 	    panic("%s: bad duplex %d\n", s->sc_dev.dv_xname, duplex);
   1574  1.1  simonb 	    /* XXX not implemented */
   1575  1.1  simonb 	    break;
   1576  1.1  simonb 	}
   1577  1.1  simonb 
   1578  1.1  simonb     /*
   1579  1.1  simonb      * Send the bits back to the hardware
   1580  1.1  simonb      */
   1581  1.1  simonb 
   1582  1.1  simonb     SBMAC_WRITECSR(s->sbm_maccfg, cfg);
   1583  1.1  simonb 
   1584  1.1  simonb     return 1;
   1585  1.1  simonb }
   1586  1.1  simonb 
   1587  1.1  simonb 
   1588  1.1  simonb 
   1589  1.1  simonb 
   1590  1.1  simonb /*  *********************************************************************
   1591  1.1  simonb     *  SBMAC_INTR()
   1592  1.1  simonb     *
   1593  1.1  simonb     *  Interrupt handler for MAC interrupts
   1594  1.1  simonb     *
   1595  1.1  simonb     *  Input parameters:
   1596  1.1  simonb     *  	   MAC structure
   1597  1.1  simonb     *
   1598  1.1  simonb     *  Return value:
   1599  1.1  simonb     *  	   nothing
   1600  1.1  simonb     ********************************************************************* */
   1601  1.1  simonb /* ARGSUSED */
   1602  1.1  simonb static void
   1603  1.1  simonb sbmac_intr(void *xsc, uint32_t status, uint32_t pc)
   1604  1.1  simonb {
   1605  1.1  simonb 	struct sbmac_softc *sc = (struct sbmac_softc *) xsc;
   1606  1.1  simonb 	uint64_t isr;
   1607  1.1  simonb 
   1608  1.1  simonb 	for (;;) {
   1609  1.1  simonb 
   1610  1.1  simonb 		/*
   1611  1.1  simonb 		 * Read the ISR (this clears the bits in the real register)
   1612  1.1  simonb 		 */
   1613  1.1  simonb 
   1614  1.1  simonb 		isr = SBMAC_READCSR(sc->sbm_isr);
   1615  1.1  simonb 
   1616  1.1  simonb 		if (isr == 0)
   1617  1.1  simonb 			break;
   1618  1.1  simonb 
   1619  1.1  simonb 		/*
   1620  1.1  simonb 		 * Transmits on channel 0
   1621  1.1  simonb 		 */
   1622  1.1  simonb 
   1623  1.1  simonb 		if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
   1624  1.1  simonb 			sbdma_tx_process(sc, &(sc->sbm_txdma));
   1625  1.1  simonb 
   1626  1.1  simonb 		/*
   1627  1.1  simonb 		 * Receives on channel 0
   1628  1.1  simonb 		 */
   1629  1.1  simonb 
   1630  1.1  simonb 		if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0))
   1631  1.1  simonb 			sbdma_rx_process(sc, &(sc->sbm_rxdma));
   1632  1.1  simonb 	}
   1633  1.1  simonb }
   1634  1.1  simonb 
   1635  1.1  simonb 
   1636  1.1  simonb /*  *********************************************************************
   1637  1.1  simonb     *  SBMAC_START(ifp)
   1638  1.1  simonb     *
   1639  1.1  simonb     *  Start output on the specified interface.  Basically, we
   1640  1.1  simonb     *  queue as many buffers as we can until the ring fills up, or
   1641  1.1  simonb     *  we run off the end of the queue, whichever comes first.
   1642  1.1  simonb     *
   1643  1.1  simonb     *  Input parameters:
   1644  1.1  simonb     *  	   ifp - interface
   1645  1.1  simonb     *
   1646  1.1  simonb     *  Return value:
   1647  1.1  simonb     *  	   nothing
   1648  1.1  simonb     ********************************************************************* */
   1649  1.1  simonb static void sbmac_start(struct ifnet *ifp)
   1650  1.1  simonb {
   1651  1.1  simonb     struct sbmac_softc	*sc;
   1652  1.1  simonb     struct mbuf		*m_head = NULL;
   1653  1.1  simonb     int			rv;
   1654  1.1  simonb 
   1655  1.1  simonb     if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
   1656  1.1  simonb 	return;
   1657  1.1  simonb 
   1658  1.1  simonb     sc = ifp->if_softc;
   1659  1.1  simonb 
   1660  1.1  simonb     for (;;) {
   1661  1.1  simonb 
   1662  1.1  simonb 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1663  1.1  simonb 	if (m_head == NULL) break;
   1664  1.1  simonb 
   1665  1.1  simonb 	/*
   1666  1.1  simonb 	 * Put the buffer on the transmit ring.  If we
   1667  1.1  simonb 	 * don't have room, set the OACTIVE flag and wait
   1668  1.1  simonb 	 * for the NIC to drain the ring.
   1669  1.1  simonb 	 */
   1670  1.1  simonb 
   1671  1.1  simonb 	rv = sbdma_add_txbuffer(&(sc->sbm_txdma), m_head);
   1672  1.1  simonb 
   1673  1.1  simonb 	if (rv == 0) {
   1674  1.1  simonb 		/*
   1675  1.1  simonb 		 * If there's a BPF listener, bounce a copy of this frame
   1676  1.1  simonb 		 * to it.
   1677  1.1  simonb 		 */
   1678  1.1  simonb #if (NBPFILTER > 0)
   1679  1.1  simonb 		if (ifp->if_bpf) {
   1680  1.1  simonb 			bpf_mtap(ifp->if_bpf, m_head);
   1681  1.1  simonb 		}
   1682  1.1  simonb #endif
   1683  1.1  simonb 		m_freem(m_head);
   1684  1.1  simonb 	} else {
   1685  1.1  simonb 	    IF_PREPEND(&ifp->if_snd, m_head);
   1686  1.1  simonb 	    ifp->if_flags |= IFF_OACTIVE;
   1687  1.1  simonb 	    break;
   1688  1.1  simonb 	}
   1689  1.1  simonb     }
   1690  1.1  simonb }
   1691  1.1  simonb 
   1692  1.1  simonb /*  *********************************************************************
   1693  1.1  simonb     *  SBMAC_SETMULTI(sc)
   1694  1.1  simonb     *
   1695  1.1  simonb     *  Reprogram the multicast table into the hardware, given
   1696  1.1  simonb     *  the list of multicasts associated with the interface
   1697  1.1  simonb     *  structure.
   1698  1.1  simonb     *
   1699  1.1  simonb     *  Input parameters:
   1700  1.1  simonb     *  	   sc - softc
   1701  1.1  simonb     *
   1702  1.1  simonb     *  Return value:
   1703  1.1  simonb     *  	   nothing
   1704  1.1  simonb     ********************************************************************* */
   1705  1.1  simonb 
   1706  1.1  simonb static void sbmac_setmulti(struct sbmac_softc *sc)
   1707  1.1  simonb {
   1708  1.1  simonb     struct ifnet		*ifp;
   1709  1.1  simonb     uint64_t reg;
   1710  1.1  simonb     sbmac_port_t port;
   1711  1.1  simonb     int idx;
   1712  1.1  simonb     struct ether_multi *enm;
   1713  1.1  simonb     struct ether_multistep step;
   1714  1.1  simonb 
   1715  1.1  simonb     ifp = &sc->sc_ethercom.ec_if;
   1716  1.1  simonb 
   1717  1.1  simonb     /*
   1718  1.1  simonb      * Clear out entire multicast table.  We do this by nuking
   1719  1.1  simonb      * the entire hash table and all the direct matches except
   1720  1.1  simonb      * the first one, which is used for our station address
   1721  1.1  simonb      */
   1722  1.1  simonb 
   1723  1.1  simonb     for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
   1724  1.1  simonb 	port = PKSEG1(sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t)));
   1725  1.1  simonb 	SBMAC_WRITECSR(port, 0);
   1726  1.1  simonb 	}
   1727  1.1  simonb 
   1728  1.1  simonb     for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
   1729  1.1  simonb 	port = PKSEG1(sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t)));
   1730  1.1  simonb 	SBMAC_WRITECSR(port, 0);
   1731  1.1  simonb 	}
   1732  1.1  simonb 
   1733  1.1  simonb     /*
   1734  1.1  simonb      * Clear the filter to say we don't want any multicasts.
   1735  1.1  simonb      */
   1736  1.1  simonb 
   1737  1.1  simonb     reg = SBMAC_READCSR(sc->sbm_rxfilter);
   1738  1.1  simonb     reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
   1739  1.1  simonb     SBMAC_WRITECSR(sc->sbm_rxfilter, reg);
   1740  1.1  simonb 
   1741  1.1  simonb     if (ifp->if_flags & IFF_ALLMULTI) {
   1742  1.1  simonb 	/*
   1743  1.1  simonb 	 * Enable ALL multicasts.  Do this by inverting the
   1744  1.1  simonb 	 * multicast enable bit.
   1745  1.1  simonb 	 */
   1746  1.1  simonb 	reg = SBMAC_READCSR(sc->sbm_rxfilter);
   1747  1.1  simonb 	reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
   1748  1.1  simonb 	SBMAC_WRITECSR(sc->sbm_rxfilter, reg);
   1749  1.1  simonb 	return;
   1750  1.1  simonb 	}
   1751  1.1  simonb 
   1752  1.1  simonb     /*
   1753  1.1  simonb      * Progam new multicast entries.  For now, only use the
   1754  1.1  simonb      * perfect filter.  In the future we'll need to use the
   1755  1.1  simonb      * hash filter if the perfect filter overflows
   1756  1.1  simonb      */
   1757  1.1  simonb 
   1758  1.1  simonb     /* XXX only using perfect filter for now, need to use hash
   1759  1.1  simonb      * XXX if the table overflows */
   1760  1.1  simonb 
   1761  1.1  simonb     idx = 1;		/* skip station address */
   1762  1.1  simonb     ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   1763  1.1  simonb     while ((enm != NULL) && (idx < MAC_ADDR_COUNT)) {
   1764  1.1  simonb 	reg = sbmac_addr2reg(enm->enm_addrlo);
   1765  1.1  simonb 	port = PKSEG1(sc->sbm_base +
   1766  1.1  simonb 				 R_MAC_ADDR_BASE+(idx*sizeof(uint64_t)));
   1767  1.1  simonb 	SBMAC_WRITECSR(port, reg);
   1768  1.1  simonb 	idx++;
   1769  1.1  simonb 	ETHER_NEXT_MULTI(step, enm);
   1770  1.1  simonb 	}
   1771  1.1  simonb 
   1772  1.1  simonb     /*
   1773  1.1  simonb      * Enable the "accept multicast bits" if we programmed at least one
   1774  1.1  simonb      * multicast.
   1775  1.1  simonb      */
   1776  1.1  simonb 
   1777  1.1  simonb     if (idx > 1) {
   1778  1.1  simonb 	reg = SBMAC_READCSR(sc->sbm_rxfilter);
   1779  1.1  simonb 	reg |= M_MAC_MCAST_EN;
   1780  1.1  simonb 	SBMAC_WRITECSR(sc->sbm_rxfilter, reg);
   1781  1.1  simonb 	}
   1782  1.1  simonb }
   1783  1.1  simonb 
   1784  1.1  simonb 
   1785  1.1  simonb /*  *********************************************************************
   1786  1.1  simonb     *  SBMAC_ETHER_IOCTL(ifp, cmd, data)
   1787  1.1  simonb     *
   1788  1.1  simonb     *  Generic IOCTL requests for this interface.  The basic
   1789  1.1  simonb     *  stuff is handled here for bringing the interface up,
   1790  1.1  simonb     *  handling multicasts, etc.
   1791  1.1  simonb     *
   1792  1.1  simonb     *  Input parameters:
   1793  1.1  simonb     *  	   ifp - interface structure
   1794  1.1  simonb     *  	   cmd - command code
   1795  1.1  simonb     *  	   data - pointer to data
   1796  1.1  simonb     *
   1797  1.1  simonb     *  Return value:
   1798  1.1  simonb     *  	   return value (0 is success)
   1799  1.1  simonb     ********************************************************************* */
   1800  1.1  simonb 
   1801  1.1  simonb static int sbmac_ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1802  1.1  simonb {
   1803  1.1  simonb     struct ifaddr *ifa = (struct ifaddr *) data;
   1804  1.1  simonb     struct sbmac_softc *sc = ifp->if_softc;
   1805  1.1  simonb 
   1806  1.1  simonb     switch (cmd) {
   1807  1.1  simonb 	case SIOCSIFADDR:
   1808  1.1  simonb 	    ifp->if_flags |= IFF_UP;
   1809  1.1  simonb 
   1810  1.1  simonb 	    switch (ifa->ifa_addr->sa_family) {
   1811  1.1  simonb #ifdef INET
   1812  1.1  simonb 		case AF_INET:
   1813  1.1  simonb 		    sbmac_init_and_start(sc);
   1814  1.1  simonb 		    arp_ifinit(ifp, ifa);
   1815  1.1  simonb 		    break;
   1816  1.1  simonb #endif
   1817  1.1  simonb #ifdef NS
   1818  1.1  simonb 		case AF_NS:
   1819  1.1  simonb 		{
   1820  1.1  simonb 		    struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1821  1.1  simonb 
   1822  1.1  simonb 		    if (ns_nullhost(*ina))
   1823  1.1  simonb 			ina->x_host = *(union ns_host *)
   1824  1.1  simonb 			    LLADDR(ifp->if_sadl);
   1825  1.1  simonb 		    else
   1826  1.1  simonb 			bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
   1827  1.1  simonb 			      ifp->if_addrlen);
   1828  1.1  simonb 		    /* Set new address. */
   1829  1.1  simonb 		    sbmac_init_and_start(sc);
   1830  1.1  simonb 		    break;
   1831  1.1  simonb 		    }
   1832  1.1  simonb #endif
   1833  1.1  simonb 		default:
   1834  1.1  simonb 		    sbmac_init_and_start(sc);
   1835  1.1  simonb 		    break;
   1836  1.1  simonb 		}
   1837  1.1  simonb 	    break;
   1838  1.1  simonb 
   1839  1.1  simonb 	default:
   1840  1.1  simonb 	    return (EINVAL);
   1841  1.1  simonb 	}
   1842  1.1  simonb 
   1843  1.1  simonb     return (0);
   1844  1.1  simonb }
   1845  1.1  simonb 
   1846  1.1  simonb 
   1847  1.1  simonb /*  *********************************************************************
   1848  1.1  simonb     *  SBMAC_IOCTL(ifp, command, data)
   1849  1.1  simonb     *
   1850  1.1  simonb     *  Main IOCTL handler - dispatches to other IOCTLs for various
   1851  1.1  simonb     *  types of requests.
   1852  1.1  simonb     *
   1853  1.1  simonb     *  Input parameters:
   1854  1.1  simonb     *  	   ifp - interface pointer
   1855  1.1  simonb     *  	   command - command code
   1856  1.1  simonb     *  	   data - pointer to argument data
   1857  1.1  simonb     *
   1858  1.1  simonb     *  Return value:
   1859  1.1  simonb     *  	   0 if ok
   1860  1.1  simonb     *  	   else error code
   1861  1.1  simonb     ********************************************************************* */
   1862  1.1  simonb 
   1863  1.1  simonb static int sbmac_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
   1864  1.1  simonb {
   1865  1.1  simonb     struct sbmac_softc *sc = ifp->if_softc;
   1866  1.1  simonb     struct ifreq *ifr = (struct ifreq *) data;
   1867  1.1  simonb     int	s, error = 0;
   1868  1.1  simonb 
   1869  1.1  simonb     s = splnet();
   1870  1.1  simonb 
   1871  1.1  simonb     switch(command) {
   1872  1.1  simonb 	case SIOCSIFADDR:
   1873  1.1  simonb 	case SIOCGIFADDR:
   1874  1.1  simonb 	    error = sbmac_ether_ioctl(ifp, command, data);
   1875  1.1  simonb 	    break;
   1876  1.1  simonb 	case SIOCSIFMTU:
   1877  1.1  simonb 	    if (ifr->ifr_mtu > 1518)  /* XXX */
   1878  1.1  simonb 		error = EINVAL;
   1879  1.1  simonb 	    else {
   1880  1.1  simonb 		ifp->if_mtu = ifr->ifr_mtu;
   1881  1.1  simonb 		/* XXX Program new MTU here */
   1882  1.1  simonb 		}
   1883  1.1  simonb 	    break;
   1884  1.1  simonb 	case SIOCSIFFLAGS:
   1885  1.1  simonb 	    if (ifp->if_flags & IFF_UP) {
   1886  1.1  simonb 		/*
   1887  1.1  simonb 		 * If only the state of the PROMISC flag changed,
   1888  1.1  simonb 		 * just tweak the hardware registers.
   1889  1.1  simonb 		 */
   1890  1.1  simonb 		if ((ifp->if_flags & IFF_RUNNING) &&
   1891  1.1  simonb 		    (ifp->if_flags & IFF_PROMISC)) {
   1892  1.1  simonb 		    /* turn on promiscuous mode */
   1893  1.1  simonb 		    sbmac_promiscuous_mode(sc, 1);
   1894  1.1  simonb 		    }
   1895  1.1  simonb 		else if (ifp->if_flags & IFF_RUNNING &&
   1896  1.1  simonb 			 !(ifp->if_flags & IFF_PROMISC)) {
   1897  1.1  simonb 		    /* turn off promiscuous mode */
   1898  1.1  simonb 		    sbmac_promiscuous_mode(sc, 0);
   1899  1.1  simonb 		    }
   1900  1.1  simonb 		else {
   1901  1.1  simonb 		    sbmac_set_channel_state(sc, sbmac_state_on);
   1902  1.1  simonb 		    }
   1903  1.1  simonb 		}
   1904  1.1  simonb 	    else {
   1905  1.1  simonb 		if (ifp->if_flags & IFF_RUNNING) {
   1906  1.1  simonb 		    sbmac_set_channel_state(sc, sbmac_state_off);
   1907  1.1  simonb 		    }
   1908  1.1  simonb 		}
   1909  1.1  simonb 
   1910  1.1  simonb 	    sc->sbm_if_flags = ifp->if_flags;
   1911  1.1  simonb 	    error = 0;
   1912  1.1  simonb 	    break;
   1913  1.1  simonb 
   1914  1.1  simonb 	case SIOCADDMULTI:
   1915  1.1  simonb 	case SIOCDELMULTI:
   1916  1.1  simonb 	    if (ifp->if_flags & IFF_RUNNING) {
   1917  1.1  simonb 		sbmac_setmulti(sc);
   1918  1.1  simonb 		error = 0;
   1919  1.1  simonb 		}
   1920  1.1  simonb 	    break;
   1921  1.1  simonb 	case SIOCSIFMEDIA:
   1922  1.1  simonb 	case SIOCGIFMEDIA:
   1923  1.1  simonb 	    error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, command);
   1924  1.1  simonb 	    break;
   1925  1.1  simonb 	default:
   1926  1.1  simonb 	    error = EINVAL;
   1927  1.1  simonb 	    break;
   1928  1.1  simonb 	}
   1929  1.1  simonb 
   1930  1.1  simonb     (void)splx(s);
   1931  1.1  simonb 
   1932  1.1  simonb     return(error);
   1933  1.1  simonb }
   1934  1.1  simonb 
   1935  1.1  simonb 
   1936  1.1  simonb /*  *********************************************************************
   1937  1.1  simonb     *  SBMAC_IFMEDIA_UPD(ifp)
   1938  1.1  simonb     *
   1939  1.1  simonb     *  Configure an appropriate media type for this interface,
   1940  1.1  simonb     *  given the data in the interface structure
   1941  1.1  simonb     *
   1942  1.1  simonb     *  Input parameters:
   1943  1.1  simonb     *  	   ifp - interface
   1944  1.1  simonb     *
   1945  1.1  simonb     *  Return value:
   1946  1.1  simonb     *  	   0 if ok
   1947  1.1  simonb     *  	   else error code
   1948  1.1  simonb     ********************************************************************* */
   1949  1.1  simonb 
   1950  1.1  simonb static int sbmac_mediachange(struct ifnet *ifp)
   1951  1.1  simonb {
   1952  1.1  simonb 	struct sbmac_softc *sc = ifp->if_softc;
   1953  1.1  simonb 
   1954  1.1  simonb 	if (ifp->if_flags & IFF_UP)
   1955  1.1  simonb 		mii_mediachg(&sc->sc_mii);
   1956  1.1  simonb 	return(0);
   1957  1.1  simonb }
   1958  1.1  simonb 
   1959  1.1  simonb /*  *********************************************************************
   1960  1.1  simonb     *  SBMAC_IFMEDIA_STS(ifp, ifmr)
   1961  1.1  simonb     *
   1962  1.1  simonb     *  Report current media status (used by ifconfig, for example)
   1963  1.1  simonb     *
   1964  1.1  simonb     *  Input parameters:
   1965  1.1  simonb     *  	   ifp - interface structure
   1966  1.1  simonb     *  	   ifmr - media request structure
   1967  1.1  simonb     *
   1968  1.1  simonb     *  Return value:
   1969  1.1  simonb     *  	   nothing
   1970  1.1  simonb     ********************************************************************* */
   1971  1.1  simonb 
   1972  1.1  simonb static void sbmac_mediastatus(struct ifnet *ifp, struct ifmediareq *req)
   1973  1.1  simonb {
   1974  1.1  simonb 	struct sbmac_softc	*sc = ifp->if_softc;
   1975  1.1  simonb 
   1976  1.1  simonb   	mii_pollstat(&sc->sc_mii);
   1977  1.1  simonb 	req->ifm_status = sc->sc_mii.mii_media_status;
   1978  1.1  simonb 	req->ifm_active = sc->sc_mii.mii_media_active;
   1979  1.1  simonb }
   1980  1.1  simonb 
   1981  1.1  simonb 
   1982  1.1  simonb /*  *********************************************************************
   1983  1.1  simonb     *  SBMAC_WATCHDOG(ifp)
   1984  1.1  simonb     *
   1985  1.1  simonb     *  Called periodically to make sure we're still happy.
   1986  1.1  simonb     *
   1987  1.1  simonb     *  Input parameters:
   1988  1.1  simonb     *  	   ifp - interface structure
   1989  1.1  simonb     *
   1990  1.1  simonb     *  Return value:
   1991  1.1  simonb     *  	   nothing
   1992  1.1  simonb     ********************************************************************* */
   1993  1.1  simonb 
   1994  1.1  simonb static void sbmac_watchdog(struct ifnet *ifp)
   1995  1.1  simonb {
   1996  1.1  simonb     /* XXX do something */
   1997  1.1  simonb }
   1998  1.1  simonb 
   1999  1.1  simonb /*
   2000  1.1  simonb  * One second timer, used to tick MII.
   2001  1.1  simonb  */
   2002  1.1  simonb static void sbmac_tick(void *arg)
   2003  1.1  simonb {
   2004  1.1  simonb     struct sbmac_softc *sc = arg;
   2005  1.1  simonb     int s;
   2006  1.1  simonb 
   2007  1.1  simonb     s = splnet();
   2008  1.1  simonb     mii_tick(&sc->sc_mii);
   2009  1.1  simonb     splx(s);
   2010  1.1  simonb 
   2011  1.1  simonb     callout_reset(&sc->sc_tick_ch, hz, sbmac_tick, sc);
   2012  1.1  simonb }
   2013  1.1  simonb 
   2014  1.1  simonb 
   2015  1.1  simonb /*  *********************************************************************
   2016  1.1  simonb     *  SBMAC_MATCH(parent, match, aux)
   2017  1.1  simonb     *
   2018  1.1  simonb     *  Part of the config process - see if this device matches the
   2019  1.1  simonb     *  info about what we expect to find on the bus.
   2020  1.1  simonb     *
   2021  1.1  simonb     *  Input parameters:
   2022  1.1  simonb     *  	   parent - parent bus structure
   2023  1.1  simonb     *  	   match -
   2024  1.1  simonb     *  	   aux - bus-specific args
   2025  1.1  simonb     *
   2026  1.1  simonb     *  Return value:
   2027  1.1  simonb     *  	   1 if we match
   2028  1.1  simonb     *  	   0 if we don't match
   2029  1.1  simonb     ********************************************************************* */
   2030  1.1  simonb 
   2031  1.1  simonb static int sbmac_match(struct device *parent, struct cfdata *match, void *aux)
   2032  1.1  simonb {
   2033  1.1  simonb     struct sbobio_attach_args *sap = aux;
   2034  1.1  simonb 
   2035  1.1  simonb     /*
   2036  1.1  simonb      * Make sure it's a MAC
   2037  1.1  simonb      */
   2038  1.1  simonb 
   2039  1.1  simonb     if (sap->sa_locs.sa_type != SBOBIO_DEVTYPE_MAC) {
   2040  1.1  simonb 	return 0;
   2041  1.1  simonb 	}
   2042  1.1  simonb 
   2043  1.1  simonb     /*
   2044  1.1  simonb      * Yup, it is.
   2045  1.1  simonb      */
   2046  1.1  simonb 
   2047  1.1  simonb     return 1;
   2048  1.1  simonb }
   2049  1.1  simonb 
   2050  1.1  simonb 
   2051  1.1  simonb /*  *********************************************************************
   2052  1.1  simonb     *  SBMAC_PARSE_XDIGIT(str)
   2053  1.1  simonb     *
   2054  1.1  simonb     *  Parse a hex digit, returning its value
   2055  1.1  simonb     *
   2056  1.1  simonb     *  Input parameters:
   2057  1.1  simonb     *  	   str - character
   2058  1.1  simonb     *
   2059  1.1  simonb     *  Return value:
   2060  1.1  simonb     *  	   hex value, or -1 if invalid
   2061  1.1  simonb     ********************************************************************* */
   2062  1.1  simonb 
   2063  1.1  simonb static int sbmac_parse_xdigit(char str)
   2064  1.1  simonb {
   2065  1.1  simonb     int digit;
   2066  1.1  simonb 
   2067  1.1  simonb     if ((str >= '0') && (str <= '9')) digit = str - '0';
   2068  1.1  simonb     else if ((str >= 'a') && (str <= 'f')) digit = str - 'a' + 10;
   2069  1.1  simonb     else if ((str >= 'A') && (str <= 'F')) digit = str - 'A' + 10;
   2070  1.1  simonb     else return -1;
   2071  1.1  simonb 
   2072  1.1  simonb     return digit;
   2073  1.1  simonb }
   2074  1.1  simonb 
   2075  1.1  simonb /*  *********************************************************************
   2076  1.1  simonb     *  SBMAC_PARSE_HWADDR(str, hwaddr)
   2077  1.1  simonb     *
   2078  1.1  simonb     *  Convert a string in the form xx:xx:xx:xx:xx:xx into a 6-byte
   2079  1.1  simonb     *  Ethernet address.
   2080  1.1  simonb     *
   2081  1.1  simonb     *  Input parameters:
   2082  1.1  simonb     *  	   str - string
   2083  1.1  simonb     *  	   hwaddr - pointer to hardware address
   2084  1.1  simonb     *
   2085  1.1  simonb     *  Return value:
   2086  1.1  simonb     *  	   0 if ok, else -1
   2087  1.1  simonb     ********************************************************************* */
   2088  1.1  simonb 
   2089  1.1  simonb static int sbmac_parse_hwaddr(char *str, u_char *hwaddr)
   2090  1.1  simonb {
   2091  1.1  simonb     int digit1, digit2;
   2092  1.1  simonb     int idx = 6;
   2093  1.1  simonb 
   2094  1.1  simonb     while (*str && (idx > 0)) {
   2095  1.1  simonb 	digit1 = sbmac_parse_xdigit(*str);
   2096  1.1  simonb 	if (digit1 < 0) return -1;
   2097  1.1  simonb 	str++;
   2098  1.1  simonb 	if (!*str) return -1;
   2099  1.1  simonb 
   2100  1.1  simonb 	if ((*str == ':') || (*str == '-')) {
   2101  1.1  simonb 	    digit2 = digit1;
   2102  1.1  simonb 	    digit1 = 0;
   2103  1.1  simonb 	    }
   2104  1.1  simonb 	else {
   2105  1.1  simonb 	    digit2 = sbmac_parse_xdigit(*str);
   2106  1.1  simonb 	    if (digit2 < 0) return -1;
   2107  1.1  simonb 	    str++;
   2108  1.1  simonb 	    }
   2109  1.1  simonb 
   2110  1.1  simonb 	*hwaddr++ = (digit1 << 4) | digit2;
   2111  1.1  simonb 	idx--;
   2112  1.1  simonb 
   2113  1.1  simonb 	if (*str == '-') str++;
   2114  1.1  simonb 	if (*str == ':') str++;
   2115  1.1  simonb 	}
   2116  1.1  simonb     return 0;
   2117  1.1  simonb }
   2118  1.1  simonb 
   2119  1.1  simonb /*  *********************************************************************
   2120  1.1  simonb     *  SBMAC_ATTACH(parent, self, aux)
   2121  1.1  simonb     *
   2122  1.1  simonb     *  Attach routine - init hardware and hook ourselves into NetBSD.
   2123  1.1  simonb     *
   2124  1.1  simonb     *  Input parameters:
   2125  1.1  simonb     *  	   parent - parent bus device
   2126  1.1  simonb     *  	   self - our softc
   2127  1.1  simonb     *  	   aux - attach data
   2128  1.1  simonb     *
   2129  1.1  simonb     *  Return value:
   2130  1.1  simonb     *  	   nothing
   2131  1.1  simonb     ********************************************************************* */
   2132  1.1  simonb 
   2133  1.1  simonb static void sbmac_attach(struct device *parent, struct device *self, void *aux)
   2134  1.1  simonb {
   2135  1.1  simonb     struct ifnet *ifp;
   2136  1.1  simonb     struct sbmac_softc *sc;
   2137  1.1  simonb     struct sbobio_attach_args *sap = aux;
   2138  1.1  simonb     u_char *eaddr;
   2139  1.1  simonb     static int unit = 0;	/* XXX */
   2140  1.1  simonb     uint64_t ea_reg;
   2141  1.1  simonb     int idx;
   2142  1.1  simonb 
   2143  1.1  simonb     sc = (struct sbmac_softc *)self;
   2144  1.1  simonb 
   2145  1.1  simonb     /* Determine controller base address */
   2146  1.1  simonb 
   2147  1.1  simonb     sc->sbm_base = (sbmac_port_t) sap->sa_base + sap->sa_locs.sa_offset;
   2148  1.1  simonb 
   2149  1.1  simonb     eaddr = sc->sbm_hwaddr;
   2150  1.1  simonb 
   2151  1.1  simonb     /*
   2152  1.1  simonb      * Initialize context (get pointers to registers and stuff), then
   2153  1.1  simonb      * allocate the memory for the descriptor tables.
   2154  1.1  simonb      */
   2155  1.1  simonb 
   2156  1.1  simonb     sbmac_initctx(sc);
   2157  1.1  simonb 
   2158  1.1  simonb     callout_init(&(sc->sc_tick_ch));
   2159  1.1  simonb 
   2160  1.1  simonb     /*
   2161  1.1  simonb      * Read the ethernet address.  The firwmare left this programmed
   2162  1.1  simonb      * for us in the ethernet address register for each mac.
   2163  1.1  simonb      */
   2164  1.1  simonb 
   2165  1.1  simonb     ea_reg = SBMAC_READCSR(PKSEG1(sc->sbm_base + R_MAC_ETHERNET_ADDR));
   2166  1.1  simonb     for (idx = 0; idx < 6; idx++) {
   2167  1.1  simonb 	eaddr[idx] = (uint8_t) (ea_reg & 0xFF);
   2168  1.1  simonb 	ea_reg >>= 8;
   2169  1.1  simonb 	}
   2170  1.1  simonb 
   2171  1.1  simonb #define	SBMAC_DEFAULT_HWADDR "40:00:00:00:01:00"
   2172  1.1  simonb     if (eaddr[0] == 0 && eaddr[1] == 0 && eaddr[2] == 0 &&
   2173  1.1  simonb 	eaddr[3] == 0 && eaddr[4] == 0 && eaddr[5] == 0) {
   2174  1.1  simonb 	sbmac_parse_hwaddr(SBMAC_DEFAULT_HWADDR, eaddr);
   2175  1.1  simonb 	eaddr[5] = unit;
   2176  1.1  simonb 	}
   2177  1.1  simonb 
   2178  1.1  simonb #ifdef SBMAC_ETH0_HWADDR
   2179  1.1  simonb     if (unit == 0) sbmac_parse_hwaddr(SBMAC_ETH0_HWADDR, eaddr);
   2180  1.1  simonb #endif
   2181  1.1  simonb #ifdef SBMAC_ETH1_HWADDR
   2182  1.1  simonb     if (unit == 1) sbmac_parse_hwaddr(SBMAC_ETH1_HWADDR, eaddr);
   2183  1.1  simonb #endif
   2184  1.1  simonb #ifdef SBMAC_ETH2_HWADDR
   2185  1.1  simonb     if (unit == 2) sbmac_parse_hwaddr(SBMAC_ETH2_HWADDR, eaddr);
   2186  1.1  simonb #endif
   2187  1.1  simonb     unit++;
   2188  1.1  simonb 
   2189  1.1  simonb     /*
   2190  1.1  simonb      * Display Ethernet address (this is called during the config process
   2191  1.1  simonb      * so we need to finish off the config message that was being displayed)
   2192  1.1  simonb      */
   2193  1.1  simonb     printf(": Ethernet\n%s: Ethernet address: %s\n", self->dv_xname,
   2194  1.1  simonb 	   ether_sprintf(eaddr));
   2195  1.1  simonb 
   2196  1.1  simonb 
   2197  1.1  simonb     /*
   2198  1.1  simonb      * Set up ifnet structure
   2199  1.1  simonb      */
   2200  1.1  simonb 
   2201  1.1  simonb     ifp = &sc->sc_ethercom.ec_if;
   2202  1.1  simonb     ifp->if_softc = sc;
   2203  1.1  simonb     bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
   2204  1.1  simonb     ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | IFF_NOTRAILERS;
   2205  1.1  simonb     ifp->if_ioctl = sbmac_ioctl;
   2206  1.1  simonb     ifp->if_start = sbmac_start;
   2207  1.1  simonb     ifp->if_watchdog = sbmac_watchdog;
   2208  1.1  simonb     ifp->if_snd.ifq_maxlen = SBMAC_MAX_TXDESCR - 1;
   2209  1.1  simonb 
   2210  1.1  simonb     /*
   2211  1.1  simonb      * Set up ifmedia support.
   2212  1.1  simonb      */
   2213  1.1  simonb 
   2214  1.1  simonb     /*
   2215  1.1  simonb      * Initialize MII/media info.
   2216  1.1  simonb      */
   2217  1.1  simonb     sc->sc_mii.mii_ifp      = ifp;
   2218  1.1  simonb     sc->sc_mii.mii_readreg  = sbmac_mii_readreg;
   2219  1.1  simonb     sc->sc_mii.mii_writereg = sbmac_mii_writereg;
   2220  1.1  simonb     sc->sc_mii.mii_statchg  = sbmac_mii_statchg;
   2221  1.1  simonb     ifmedia_init(&sc->sc_mii.mii_media, 0, sbmac_mediachange,
   2222  1.1  simonb 	sbmac_mediastatus);
   2223  1.1  simonb     mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   2224  1.1  simonb 	       MII_OFFSET_ANY, 0);
   2225  1.1  simonb 
   2226  1.1  simonb     if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   2227  1.1  simonb 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   2228  1.1  simonb 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   2229  1.1  simonb 	}
   2230  1.1  simonb     else {
   2231  1.1  simonb 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   2232  1.1  simonb 	}
   2233  1.1  simonb 
   2234  1.1  simonb 
   2235  1.1  simonb     /*
   2236  1.1  simonb      * map/route interrupt
   2237  1.1  simonb      */
   2238  1.1  simonb 
   2239  1.1  simonb     sc->sbm_intrhand = cpu_intr_establish(sap->sa_locs.sa_intr[0],
   2240  1.1  simonb 					  IPL_NET, sbmac_intr, sc);
   2241  1.1  simonb 
   2242  1.1  simonb     /*
   2243  1.1  simonb      * Call MI attach routines.
   2244  1.1  simonb      */
   2245  1.1  simonb     if_attach(ifp);
   2246  1.1  simonb     ether_ifattach(ifp, eaddr);
   2247  1.1  simonb }
   2248