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