Home | History | Annotate | Line # | Download | only in dev
if_smap.c revision 1.1
      1  1.1  uch /*	$NetBSD: if_smap.c,v 1.1 2001/10/16 15:38:33 uch Exp $	*/
      2  1.1  uch 
      3  1.1  uch /*-
      4  1.1  uch  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  1.1  uch  * All rights reserved.
      6  1.1  uch  *
      7  1.1  uch  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  uch  * by UCHIYAMA Yasushi.
      9  1.1  uch  *
     10  1.1  uch  * Redistribution and use in source and binary forms, with or without
     11  1.1  uch  * modification, are permitted provided that the following conditions
     12  1.1  uch  * are met:
     13  1.1  uch  * 1. Redistributions of source code must retain the above copyright
     14  1.1  uch  *    notice, this list of conditions and the following disclaimer.
     15  1.1  uch  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  uch  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  uch  *    documentation and/or other materials provided with the distribution.
     18  1.1  uch  * 3. All advertising materials mentioning features or use of this software
     19  1.1  uch  *    must display the following acknowledgement:
     20  1.1  uch  *        This product includes software developed by the NetBSD
     21  1.1  uch  *        Foundation, Inc. and its contributors.
     22  1.1  uch  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  uch  *    contributors may be used to endorse or promote products derived
     24  1.1  uch  *    from this software without specific prior written permission.
     25  1.1  uch  *
     26  1.1  uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  uch  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  uch  */
     38  1.1  uch 
     39  1.1  uch #include "debug_playstation2.h"
     40  1.1  uch 
     41  1.1  uch #include "bpfilter.h"
     42  1.1  uch #include "rnd.h"
     43  1.1  uch 
     44  1.1  uch #include <sys/param.h>
     45  1.1  uch #include <sys/systm.h>
     46  1.1  uch 
     47  1.1  uch #include <sys/syslog.h>
     48  1.1  uch #include <sys/mbuf.h>
     49  1.1  uch #include <sys/ioctl.h>
     50  1.1  uch #include <sys/socket.h>
     51  1.1  uch 
     52  1.1  uch #include <playstation2/ee/eevar.h>
     53  1.1  uch 
     54  1.1  uch #if NRND > 0
     55  1.1  uch #include <sys/rnd.h>
     56  1.1  uch #endif
     57  1.1  uch 
     58  1.1  uch #include <net/if.h>
     59  1.1  uch #include <net/if_dl.h>
     60  1.1  uch #include <net/if_types.h>
     61  1.1  uch 
     62  1.1  uch #include <net/if_ether.h>
     63  1.1  uch #include <net/if_media.h>
     64  1.1  uch 
     65  1.1  uch #include <dev/mii/mii.h>
     66  1.1  uch #include <dev/mii/miivar.h>
     67  1.1  uch 
     68  1.1  uch #include <netinet/in.h>
     69  1.1  uch #include <netinet/in_systm.h>
     70  1.1  uch #include <netinet/in_var.h>
     71  1.1  uch #include <netinet/ip.h>
     72  1.1  uch #include <netinet/if_inarp.h>
     73  1.1  uch 
     74  1.1  uch #if NBPFILTER > 0
     75  1.1  uch #include <net/bpf.h>
     76  1.1  uch #include <net/bpfdesc.h>
     77  1.1  uch #endif
     78  1.1  uch 
     79  1.1  uch #include <playstation2/dev/spdvar.h>
     80  1.1  uch #include <playstation2/dev/spdreg.h>
     81  1.1  uch #include <playstation2/dev/emac3var.h>
     82  1.1  uch #include <playstation2/dev/if_smapreg.h>
     83  1.1  uch 
     84  1.1  uch #ifdef SMAP_DEBUG
     85  1.1  uch #include <playstation2/ee/gsvar.h>
     86  1.1  uch int	smap_debug = 0;
     87  1.1  uch #define	DPRINTF(fmt, args...)						\
     88  1.1  uch 	if (smap_debug)							\
     89  1.1  uch 		printf("%s: " fmt, __FUNCTION__ , ##args)
     90  1.1  uch #define	DPRINTFN(n, arg)						\
     91  1.1  uch 	if (smap_debug > (n))						\
     92  1.1  uch 		printf("%s: " fmt, __FUNCTION__ , ##args)
     93  1.1  uch #define STATIC
     94  1.1  uch struct smap_softc *__sc;
     95  1.1  uch void __smap_status(int);
     96  1.1  uch void __smap_lock_check(const char *, int);
     97  1.1  uch #define FUNC_ENTER()	__smap_lock_check(__FUNCTION__, 1)
     98  1.1  uch #define FUNC_EXIT()	__smap_lock_check(__FUNCTION__, 0)
     99  1.1  uch #else
    100  1.1  uch #define	DPRINTF(arg...)		((void)0)
    101  1.1  uch #define DPRINTFN(n, arg...)	((void)0)
    102  1.1  uch #define STATIC			static
    103  1.1  uch #define FUNC_ENTER()		((void)0)
    104  1.1  uch #define FUNC_EXIT()		((void)0)
    105  1.1  uch #endif
    106  1.1  uch 
    107  1.1  uch struct smap_softc {
    108  1.1  uch 	struct emac3_softc emac3;
    109  1.1  uch 	struct ethercom ethercom;
    110  1.1  uch 
    111  1.1  uch 	u_int32_t *tx_buf;
    112  1.1  uch 	u_int32_t *rx_buf;
    113  1.1  uch 	struct smap_desc *tx_desc;
    114  1.1  uch 	struct smap_desc *rx_desc;
    115  1.1  uch 
    116  1.1  uch #define	SMAP_FIFO_ALIGN		4
    117  1.1  uch 	int tx_buf_freesize;	/* buffer usage */
    118  1.1  uch 	int tx_desc_cnt;	/* descriptor usage */
    119  1.1  uch 	u_int16_t tx_fifo_ptr;
    120  1.1  uch 	int tx_done_index, tx_start_index;
    121  1.1  uch 	int rx_done_index;
    122  1.1  uch 
    123  1.1  uch #if NRND > 0
    124  1.1  uch 	rndsource_element_t rnd_source;
    125  1.1  uch #endif
    126  1.1  uch };
    127  1.1  uch 
    128  1.1  uch #define DEVNAME		(sc->emac3.dev.dv_xname)
    129  1.1  uch #define ROUND4(x)	(((x) + 3) & ~3)
    130  1.1  uch #define ROUND16(x)	(((x) + 15) & ~15)
    131  1.1  uch 
    132  1.1  uch STATIC int smap_match(struct device *, struct cfdata *, void *);
    133  1.1  uch STATIC void smap_attach(struct device *, struct device *, void *);
    134  1.1  uch 
    135  1.1  uch struct cfattach smap_ca = {
    136  1.1  uch 	sizeof (struct smap_softc), smap_match, smap_attach
    137  1.1  uch };
    138  1.1  uch 
    139  1.1  uch STATIC int smap_intr(void *);
    140  1.1  uch STATIC void smap_rxeof(void *);
    141  1.1  uch STATIC void smap_txeof(void *);
    142  1.1  uch STATIC void smap_start(struct ifnet *);
    143  1.1  uch STATIC void smap_watchdog(struct ifnet *);
    144  1.1  uch STATIC int smap_ioctl(struct ifnet *, u_long, caddr_t);
    145  1.1  uch STATIC int smap_init(struct ifnet *);
    146  1.1  uch STATIC void smap_stop(struct ifnet *, int);
    147  1.1  uch 
    148  1.1  uch STATIC int smap_get_eaddr(struct smap_softc *, u_int8_t *);
    149  1.1  uch STATIC int smap_fifo_init(struct smap_softc *);
    150  1.1  uch STATIC int smap_fifo_reset(bus_addr_t);
    151  1.1  uch STATIC void smap_desc_init(struct smap_softc *);
    152  1.1  uch 
    153  1.1  uch int
    154  1.1  uch smap_match(struct device *parent, struct cfdata *cf, void *aux)
    155  1.1  uch {
    156  1.1  uch 	struct spd_attach_args *spa = aux;
    157  1.1  uch 
    158  1.1  uch 	if (spa->spa_slot != SPD_NIC)
    159  1.1  uch 		return (0);
    160  1.1  uch 
    161  1.1  uch 	return (1);
    162  1.1  uch }
    163  1.1  uch 
    164  1.1  uch void
    165  1.1  uch smap_attach(struct device *parent, struct device *self, void *aux)
    166  1.1  uch {
    167  1.1  uch 	struct spd_attach_args *spa = aux;
    168  1.1  uch 	struct smap_softc *sc = (void *)self;
    169  1.1  uch 	struct emac3_softc *emac3 = &sc->emac3;
    170  1.1  uch 	struct ifnet *ifp = &sc->ethercom.ec_if;
    171  1.1  uch 	struct mii_data *mii = &emac3->mii;
    172  1.1  uch 	void *txbuf, *rxbuf;
    173  1.1  uch 	u_int16_t r;
    174  1.1  uch 
    175  1.1  uch #ifdef SMAP_DEBUG
    176  1.1  uch 	__sc = sc;
    177  1.1  uch #endif
    178  1.1  uch 
    179  1.1  uch 	printf(": %s\n", spa->spa_product_name);
    180  1.1  uch 
    181  1.1  uch 	/* SPD EEPROM */
    182  1.1  uch 	if (smap_get_eaddr(sc, emac3->eaddr) != 0)
    183  1.1  uch 		return;
    184  1.1  uch 
    185  1.1  uch 	printf("%s: Ethernet address %s\n", DEVNAME,
    186  1.1  uch 	    ether_sprintf(emac3->eaddr));
    187  1.1  uch 
    188  1.1  uch 	/* disable interrupts */
    189  1.1  uch 	r = _reg_read_2(SPD_INTR_ENABLE_REG16);
    190  1.1  uch 	r &= ~(SPD_INTR_RXEND | SPD_INTR_TXEND | SPD_INTR_RXDNV |
    191  1.1  uch 	    SPD_INTR_EMAC3);
    192  1.1  uch 	_reg_write_2(SPD_INTR_ENABLE_REG16, r);
    193  1.1  uch 	emac3_intr_disable();
    194  1.1  uch 
    195  1.1  uch 	/* clear pending interrupts */
    196  1.1  uch 	_reg_write_2(SPD_INTR_CLEAR_REG16, SPD_INTR_RXEND | SPD_INTR_TXEND |
    197  1.1  uch 	    SPD_INTR_RXDNV);
    198  1.1  uch 	emac3_intr_clear();
    199  1.1  uch 
    200  1.1  uch 	/* buffer descriptor mode */
    201  1.1  uch 	_reg_write_1(SMAP_DESC_MODE_REG8, 0);
    202  1.1  uch 
    203  1.1  uch 	if (smap_fifo_init(sc) != 0)
    204  1.1  uch 		return;
    205  1.1  uch 
    206  1.1  uch 	if (emac3_init(&sc->emac3) != 0)
    207  1.1  uch 		return;
    208  1.1  uch 	emac3_intr_disable();
    209  1.1  uch 	emac3_disable();
    210  1.1  uch 
    211  1.1  uch 	smap_desc_init(sc);
    212  1.1  uch 
    213  1.1  uch 	/* allocate temporary buffer */
    214  1.1  uch 	txbuf = malloc(ETHER_MAX_LEN - ETHER_CRC_LEN + SMAP_FIFO_ALIGN + 16,
    215  1.1  uch 	    M_DEVBUF, M_NOWAIT);
    216  1.1  uch 	if (txbuf == NULL) {
    217  1.1  uch 		printf("%s: no memory.\n", DEVNAME);
    218  1.1  uch 		return;
    219  1.1  uch 	}
    220  1.1  uch 
    221  1.1  uch 	rxbuf = malloc(ETHER_MAX_LEN + SMAP_FIFO_ALIGN + 16,
    222  1.1  uch 	    M_DEVBUF, M_NOWAIT);
    223  1.1  uch 	if (rxbuf == NULL) {
    224  1.1  uch 		printf("%s: no memory.\n", DEVNAME);
    225  1.1  uch 		free(txbuf, M_DEVBUF);
    226  1.1  uch 		return;
    227  1.1  uch 	}
    228  1.1  uch 
    229  1.1  uch 	sc->tx_buf = (u_int32_t *)ROUND16((vaddr_t)txbuf);
    230  1.1  uch 	sc->rx_buf = (u_int32_t *)ROUND16((vaddr_t)rxbuf);
    231  1.1  uch 
    232  1.1  uch 	/*
    233  1.1  uch 	 * setup MI layer
    234  1.1  uch 	 */
    235  1.1  uch 	strcpy(ifp->if_xname, DEVNAME);
    236  1.1  uch 	ifp->if_softc	= sc;
    237  1.1  uch 	ifp->if_start	= smap_start;
    238  1.1  uch 	ifp->if_ioctl	= smap_ioctl;
    239  1.1  uch 	ifp->if_init	= smap_init;
    240  1.1  uch 	ifp->if_stop	= smap_stop;
    241  1.1  uch 	ifp->if_watchdog= smap_watchdog;
    242  1.1  uch 	ifp->if_flags	= IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS |
    243  1.1  uch 	    IFF_MULTICAST;
    244  1.1  uch 	IFQ_SET_READY(&ifp->if_snd);
    245  1.1  uch 
    246  1.1  uch 	/* ifmedia setup. */
    247  1.1  uch 	mii->mii_ifp		= ifp;
    248  1.1  uch 	mii->mii_readreg	= emac3_phy_readreg;
    249  1.1  uch 	mii->mii_writereg	= emac3_phy_writereg;
    250  1.1  uch 	mii->mii_statchg	= emac3_phy_statchg;
    251  1.1  uch 	ifmedia_init(&mii->mii_media, 0, emac3_ifmedia_upd, emac3_ifmedia_sts);
    252  1.1  uch 	mii_attach(&emac3->dev, mii, 0xffffffff, MII_PHY_ANY,
    253  1.1  uch 	    MII_OFFSET_ANY, 0);
    254  1.1  uch 
    255  1.1  uch 	/* Choose a default media. */
    256  1.1  uch 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    257  1.1  uch 		ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    258  1.1  uch 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_NONE);
    259  1.1  uch 	} else {
    260  1.1  uch 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
    261  1.1  uch 	}
    262  1.1  uch 
    263  1.1  uch 	if_attach(ifp);
    264  1.1  uch 	ether_ifattach(ifp, emac3->eaddr);
    265  1.1  uch 
    266  1.1  uch 	spd_intr_establish(SPD_NIC, smap_intr, sc);
    267  1.1  uch 
    268  1.1  uch #if NRND > 0
    269  1.1  uch 	rnd_attach_source(&sc->rnd_source, DEVNAME,
    270  1.1  uch 	    RND_TYPE_NET, 0);
    271  1.1  uch #endif
    272  1.1  uch }
    273  1.1  uch 
    274  1.1  uch int
    275  1.1  uch smap_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
    276  1.1  uch {
    277  1.1  uch 	struct smap_softc *sc = ifp->if_softc;
    278  1.1  uch 	struct ifreq *ifr = (struct ifreq *) data;
    279  1.1  uch 	int error, s;
    280  1.1  uch 
    281  1.1  uch 	s = splnet();
    282  1.1  uch 
    283  1.1  uch 	switch (command) {
    284  1.1  uch 	case SIOCGIFMEDIA:
    285  1.1  uch 	case SIOCSIFMEDIA:
    286  1.1  uch 		error = ifmedia_ioctl(ifp, ifr, &sc->emac3.mii.mii_media,
    287  1.1  uch 		    command);
    288  1.1  uch 		break;
    289  1.1  uch 
    290  1.1  uch 	default:
    291  1.1  uch 		error = ether_ioctl(ifp, command, data);
    292  1.1  uch 
    293  1.1  uch 		if (error == ENETRESET) {
    294  1.1  uch 			emac3_setmulti(&sc->emac3, &sc->ethercom);
    295  1.1  uch 			error = 0;
    296  1.1  uch 		}
    297  1.1  uch 		break;
    298  1.1  uch 	}
    299  1.1  uch 
    300  1.1  uch 	splx(s);
    301  1.1  uch 
    302  1.1  uch 	return (error);
    303  1.1  uch }
    304  1.1  uch 
    305  1.1  uch int
    306  1.1  uch smap_intr(void *arg)
    307  1.1  uch {
    308  1.1  uch 	struct smap_softc *sc = arg;
    309  1.1  uch 	struct ifnet *ifp;
    310  1.1  uch 	u_int16_t cause, disable, r;
    311  1.1  uch 
    312  1.1  uch 	cause = _reg_read_2(SPD_INTR_STATUS_REG16) &
    313  1.1  uch 	    _reg_read_2(SPD_INTR_ENABLE_REG16);
    314  1.1  uch 
    315  1.1  uch 	disable = cause & (SPD_INTR_RXDNV | SPD_INTR_TXDNV);
    316  1.1  uch 	if (disable) {
    317  1.1  uch 		r = _reg_read_2(SPD_INTR_ENABLE_REG16);
    318  1.1  uch 		r &= ~disable;
    319  1.1  uch 		_reg_write_2(SPD_INTR_ENABLE_REG16, r);
    320  1.1  uch 
    321  1.1  uch 		printf("%s: invalid descriptor. (%c%c)\n", DEVNAME,
    322  1.1  uch 		    disable & SPD_INTR_RXDNV ? 'R' : '_',
    323  1.1  uch 		    disable & SPD_INTR_TXDNV ? 'T' : '_');
    324  1.1  uch 
    325  1.1  uch 		if (disable & SPD_INTR_RXDNV)
    326  1.1  uch 			smap_rxeof(arg);
    327  1.1  uch 
    328  1.1  uch 		_reg_write_2(SPD_INTR_CLEAR_REG16, disable);
    329  1.1  uch 	}
    330  1.1  uch 
    331  1.1  uch 	if (cause & SPD_INTR_TXEND) {
    332  1.1  uch 		_reg_write_2(SPD_INTR_CLEAR_REG16, SPD_INTR_TXEND);
    333  1.1  uch 		if (_reg_read_1(SMAP_RXFIFO_FRAME_REG8) > 0)
    334  1.1  uch 			cause |= SPD_INTR_RXEND;
    335  1.1  uch 		smap_txeof(arg);
    336  1.1  uch 	}
    337  1.1  uch 
    338  1.1  uch 	if (cause & SPD_INTR_RXEND) {
    339  1.1  uch 		_reg_write_2(SPD_INTR_CLEAR_REG16, SPD_INTR_RXEND);
    340  1.1  uch 		smap_rxeof(arg);
    341  1.1  uch 		if (sc->tx_desc_cnt > 0 &&
    342  1.1  uch 		    sc->tx_desc_cnt > _reg_read_1(SMAP_TXFIFO_FRAME_REG8))
    343  1.1  uch 			smap_txeof(arg);
    344  1.1  uch 	}
    345  1.1  uch 
    346  1.1  uch 	if (cause & SPD_INTR_EMAC3)
    347  1.1  uch 		emac3_intr(arg);
    348  1.1  uch 
    349  1.1  uch 	/* if transmition is pending, start here */
    350  1.1  uch 	ifp = &sc->ethercom.ec_if;
    351  1.1  uch 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    352  1.1  uch 		smap_start(ifp);
    353  1.1  uch #if NRND > 0
    354  1.1  uch 	rnd_add_uint32(&sc->rnd_source, cause | sc->tx_fifo_ptr << 16);
    355  1.1  uch #endif
    356  1.1  uch 
    357  1.1  uch 	return (1);
    358  1.1  uch }
    359  1.1  uch 
    360  1.1  uch void
    361  1.1  uch smap_rxeof(void *arg)
    362  1.1  uch {
    363  1.1  uch 	struct smap_softc *sc = arg;
    364  1.1  uch 	struct smap_desc *d;
    365  1.1  uch 	struct ifnet *ifp = &sc->ethercom.ec_if;
    366  1.1  uch 	struct mbuf *m;
    367  1.1  uch 	u_int16_t r16, stat;
    368  1.1  uch 	u_int32_t *p;
    369  1.1  uch 	int i, j, sz, rxsz, cnt;
    370  1.1  uch 
    371  1.1  uch 	FUNC_ENTER();
    372  1.1  uch 
    373  1.1  uch 	i = sc->rx_done_index;
    374  1.1  uch 
    375  1.1  uch 	for (cnt = 0;; cnt++, i = (i + 1) & 0x3f) {
    376  1.1  uch 		m = NULL;
    377  1.1  uch 		d = &sc->rx_desc[i];
    378  1.1  uch 		stat = d->stat;
    379  1.1  uch 
    380  1.1  uch 		if ((stat & SMAP_RXDESC_EMPTY) != 0) {
    381  1.1  uch 			break;
    382  1.1  uch 		} else if (stat & 0x7fff) {
    383  1.1  uch 			ifp->if_ierrors++;
    384  1.1  uch 			goto next_packet;
    385  1.1  uch 		}
    386  1.1  uch 
    387  1.1  uch 		sz = d->sz;
    388  1.1  uch 		rxsz = ROUND4(sz);
    389  1.1  uch 
    390  1.1  uch 		KDASSERT(sz >= ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN);
    391  1.1  uch 		KDASSERT(sz <= ETHER_MAX_LEN);
    392  1.1  uch 
    393  1.1  uch 		/* load data from FIFO */
    394  1.1  uch 		_reg_write_2(SMAP_RXFIFO_PTR_REG16, d->ptr & 0x3ffc);
    395  1.1  uch 		p = sc->rx_buf;
    396  1.1  uch 		for (j = 0; j < rxsz; j += sizeof(u_int32_t)) {
    397  1.1  uch 			*p++ = _reg_read_4(SMAP_RXFIFO_DATA_REG);
    398  1.1  uch 		}
    399  1.1  uch 
    400  1.1  uch 		/* put to mbuf */
    401  1.1  uch 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    402  1.1  uch 		if (m == NULL) {
    403  1.1  uch 			printf("%s: unable to allocate Rx mbuf\n", DEVNAME);
    404  1.1  uch 			ifp->if_ierrors++;
    405  1.1  uch 			goto next_packet;
    406  1.1  uch 		}
    407  1.1  uch 
    408  1.1  uch 		if (sz > (MHLEN - 2)) {
    409  1.1  uch 			MCLGET(m, M_DONTWAIT);
    410  1.1  uch 			if ((m->m_flags & M_EXT) == 0) {
    411  1.1  uch 				printf("%s: unable to allocate Rx cluster\n",
    412  1.1  uch 				    DEVNAME);
    413  1.1  uch 				m_freem(m);
    414  1.1  uch 				m = NULL;
    415  1.1  uch 				ifp->if_ierrors++;
    416  1.1  uch 				goto next_packet;
    417  1.1  uch 			}
    418  1.1  uch 		}
    419  1.1  uch 
    420  1.1  uch 		m->m_data += 2; /* for alignment */
    421  1.1  uch 		m->m_pkthdr.rcvif = ifp;
    422  1.1  uch 		m->m_pkthdr.len = m->m_len = sz;
    423  1.1  uch 		memcpy(mtod(m, caddr_t), (caddr_t)sc->rx_buf, sz);
    424  1.1  uch 
    425  1.1  uch 	next_packet:
    426  1.1  uch 		ifp->if_ipackets++;
    427  1.1  uch 
    428  1.1  uch 		_reg_write_1(SMAP_RXFIFO_FRAME_DEC_REG8, 1);
    429  1.1  uch 
    430  1.1  uch 		/* free descriptor */
    431  1.1  uch 		d->sz	= 0;
    432  1.1  uch 		d->ptr	= 0;
    433  1.1  uch 		d->stat	= SMAP_RXDESC_EMPTY;
    434  1.1  uch 		_wbflush();
    435  1.1  uch 
    436  1.1  uch 		if (m != NULL) {
    437  1.1  uch #if NBPFILTER > 0
    438  1.1  uch 			if (ifp->if_bpf)
    439  1.1  uch 				bpf_mtap(ifp->if_bpf, m);
    440  1.1  uch #endif
    441  1.1  uch 			(*ifp->if_input)(ifp, m);
    442  1.1  uch 		}
    443  1.1  uch 	}
    444  1.1  uch 	sc->rx_done_index = i;
    445  1.1  uch 
    446  1.1  uch 	r16 = _reg_read_2(SPD_INTR_ENABLE_REG16);
    447  1.1  uch 	if (((r16 & SPD_INTR_RXDNV) == 0) && cnt > 0) {
    448  1.1  uch 		r16  |= SPD_INTR_RXDNV;
    449  1.1  uch 		_reg_write_2(SPD_INTR_ENABLE_REG16, r16);
    450  1.1  uch 	}
    451  1.1  uch 
    452  1.1  uch 	FUNC_EXIT();
    453  1.1  uch }
    454  1.1  uch 
    455  1.1  uch void
    456  1.1  uch smap_txeof(void *arg)
    457  1.1  uch {
    458  1.1  uch 	struct smap_softc *sc = arg;
    459  1.1  uch 	struct ifnet *ifp = &sc->ethercom.ec_if;
    460  1.1  uch 	struct smap_desc *d;
    461  1.1  uch 	int i;
    462  1.1  uch 
    463  1.1  uch 	FUNC_ENTER();
    464  1.1  uch 
    465  1.1  uch 	/* clear the timeout timer. */
    466  1.1  uch 	ifp->if_timer = 0;
    467  1.1  uch 
    468  1.1  uch 	/* garbage collect */
    469  1.1  uch 	for (i = sc->tx_done_index;; i = (i + 1) & 0x3f) {
    470  1.1  uch 		u_int16_t stat;
    471  1.1  uch 
    472  1.1  uch 		d = &sc->tx_desc[i];
    473  1.1  uch 		stat = d->stat;
    474  1.1  uch 		if (stat & SMAP_TXDESC_READY) {
    475  1.1  uch 			/* all descriptor processed. */
    476  1.1  uch 			break;
    477  1.1  uch 		} else if (stat & 0x7fff) {
    478  1.1  uch 			if (stat & (SMAP_TXDESC_ECOLL | SMAP_TXDESC_LCOLL |
    479  1.1  uch 			    SMAP_TXDESC_MCOLL | SMAP_TXDESC_SCOLL))
    480  1.1  uch 				ifp->if_collisions++;
    481  1.1  uch 			else
    482  1.1  uch 				ifp->if_oerrors++;
    483  1.1  uch 		} else {
    484  1.1  uch 			ifp->if_opackets++;
    485  1.1  uch 		}
    486  1.1  uch 
    487  1.1  uch 		if (sc->tx_desc_cnt == 0)
    488  1.1  uch 			break;
    489  1.1  uch 
    490  1.1  uch 		sc->tx_buf_freesize += ROUND4(d->sz);
    491  1.1  uch 		sc->tx_desc_cnt--;
    492  1.1  uch 
    493  1.1  uch 		d->sz = 0;
    494  1.1  uch 		d->ptr = 0;
    495  1.1  uch 		d->stat = 0;
    496  1.1  uch 		_wbflush();
    497  1.1  uch 	}
    498  1.1  uch 	sc->tx_done_index = i;
    499  1.1  uch 
    500  1.1  uch 	/* OK to start transmit */
    501  1.1  uch 	ifp->if_flags &= ~IFF_OACTIVE;
    502  1.1  uch 
    503  1.1  uch 	FUNC_EXIT();
    504  1.1  uch }
    505  1.1  uch 
    506  1.1  uch void
    507  1.1  uch smap_start(struct ifnet *ifp)
    508  1.1  uch {
    509  1.1  uch 	struct smap_softc *sc = ifp->if_softc;
    510  1.1  uch 	struct smap_desc *d;
    511  1.1  uch 	struct mbuf *m0, *m;
    512  1.1  uch 	u_int8_t *p, *q;
    513  1.1  uch 	u_int32_t *r;
    514  1.1  uch 	int i, sz, pktsz;
    515  1.1  uch 	u_int16_t fifop;
    516  1.1  uch 	u_int16_t r16;
    517  1.1  uch 
    518  1.1  uch 	KDASSERT(ifp->if_flags & IFF_RUNNING);
    519  1.1  uch 	FUNC_ENTER();
    520  1.1  uch 
    521  1.1  uch 	while (1) {
    522  1.1  uch 		IFQ_POLL(&ifp->if_snd, m0);
    523  1.1  uch 		if (m0 == NULL)
    524  1.1  uch 			goto end;
    525  1.1  uch 
    526  1.1  uch 		pktsz = m0->m_pkthdr.len;
    527  1.1  uch 		KDASSERT(pktsz <= ETHER_MAX_LEN - ETHER_CRC_LEN);
    528  1.1  uch 		sz = ROUND4(pktsz);
    529  1.1  uch 
    530  1.1  uch 		if (sz > sc->tx_buf_freesize ||
    531  1.1  uch 		    sc->tx_desc_cnt >= SMAP_DESC_MAX ||
    532  1.1  uch 		    emac3_tx_done() != 0) {
    533  1.1  uch 			ifp->if_flags |= IFF_OACTIVE;
    534  1.1  uch 			goto end;
    535  1.1  uch 		}
    536  1.1  uch 
    537  1.1  uch 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    538  1.1  uch 		KDASSERT(m0 != NULL);
    539  1.1  uch #if NBPFILTER > 0
    540  1.1  uch 		if (ifp->if_bpf)
    541  1.1  uch 			bpf_mtap(ifp->if_bpf, m0);
    542  1.1  uch #endif
    543  1.1  uch 
    544  1.1  uch 		p = (u_int8_t *)sc->tx_buf;
    545  1.1  uch 		q = p + sz;
    546  1.1  uch 		/* copy to temporary buffer area */
    547  1.1  uch 		for (m = m0; m != 0; m = m->m_next) {
    548  1.1  uch 			memcpy(p, mtod(m, caddr_t), m->m_len);
    549  1.1  uch 			p += m->m_len;
    550  1.1  uch 		}
    551  1.1  uch 		m_freem(m0);
    552  1.1  uch 
    553  1.1  uch 		/* zero padding area */
    554  1.1  uch 		for (; p < q; p++)
    555  1.1  uch 			*p = 0;
    556  1.1  uch 
    557  1.1  uch 		/* put to FIFO */
    558  1.1  uch 		fifop = sc->tx_fifo_ptr;
    559  1.1  uch 		KDASSERT((fifop & 3) == 0);
    560  1.1  uch 		_reg_write_2(SMAP_TXFIFO_PTR_REG16, fifop);
    561  1.1  uch 		sc->tx_fifo_ptr = (fifop + sz) & 0xfff;
    562  1.1  uch 
    563  1.1  uch 		r = sc->tx_buf;
    564  1.1  uch 		for (i = 0; i < sz; i += sizeof(u_int32_t))
    565  1.1  uch 			*(__volatile__ u_int32_t *)SMAP_TXFIFO_DATA_REG = *r++;
    566  1.1  uch 		_wbflush();
    567  1.1  uch 
    568  1.1  uch 		/* put FIFO to EMAC3 */
    569  1.1  uch 		d = &sc->tx_desc[sc->tx_start_index];
    570  1.1  uch 		KDASSERT((d->stat & SMAP_TXDESC_READY) == 0);
    571  1.1  uch 
    572  1.1  uch 		d->sz = pktsz;
    573  1.1  uch 		d->ptr = fifop + SMAP_TXBUF_BASE;
    574  1.1  uch 		d->stat = SMAP_TXDESC_READY | SMAP_TXDESC_GENFCS |
    575  1.1  uch 		    SMAP_TXDESC_GENPAD;
    576  1.1  uch 		_wbflush();
    577  1.1  uch 
    578  1.1  uch 		sc->tx_buf_freesize -= sz;
    579  1.1  uch 		sc->tx_desc_cnt++;
    580  1.1  uch 		sc->tx_start_index = (sc->tx_start_index + 1) & 0x3f;
    581  1.1  uch 		_reg_write_1(SMAP_TXFIFO_FRAME_INC_REG8, 1);
    582  1.1  uch 
    583  1.1  uch 		emac3_tx_kick();
    584  1.1  uch 		r16 = _reg_read_2(SPD_INTR_ENABLE_REG16);
    585  1.1  uch 		if ((r16 & SPD_INTR_TXDNV) == 0) {
    586  1.1  uch 			r16 |= SPD_INTR_TXDNV;
    587  1.1  uch 			_reg_write_2(SPD_INTR_ENABLE_REG16, r16);
    588  1.1  uch 		}
    589  1.1  uch 	}
    590  1.1  uch  end:
    591  1.1  uch 	/* set watchdog timer */
    592  1.1  uch 	ifp->if_timer = 5;
    593  1.1  uch 
    594  1.1  uch 	FUNC_EXIT();
    595  1.1  uch }
    596  1.1  uch 
    597  1.1  uch void
    598  1.1  uch smap_watchdog(struct ifnet *ifp)
    599  1.1  uch {
    600  1.1  uch 	struct smap_softc *sc = ifp->if_softc;
    601  1.1  uch 
    602  1.1  uch 	printf("%s: watchdog timeout\n",DEVNAME);
    603  1.1  uch 	sc->ethercom.ec_if.if_oerrors++;
    604  1.1  uch 
    605  1.1  uch 	smap_fifo_init(sc);
    606  1.1  uch 	smap_desc_init(sc);
    607  1.1  uch 	emac3_reset(&sc->emac3);
    608  1.1  uch }
    609  1.1  uch 
    610  1.1  uch int
    611  1.1  uch smap_init(struct ifnet *ifp)
    612  1.1  uch {
    613  1.1  uch 	struct smap_softc *sc = ifp->if_softc;
    614  1.1  uch 	u_int16_t r16;
    615  1.1  uch 
    616  1.1  uch 	smap_fifo_init(sc);
    617  1.1  uch 	emac3_reset(&sc->emac3);
    618  1.1  uch 	smap_desc_init(sc);
    619  1.1  uch 
    620  1.1  uch 	_reg_write_2(SPD_INTR_CLEAR_REG16, SPD_INTR_RXEND | SPD_INTR_TXEND |
    621  1.1  uch 	    SPD_INTR_RXDNV);
    622  1.1  uch 	emac3_intr_clear();
    623  1.1  uch 
    624  1.1  uch 	r16 = _reg_read_2(SPD_INTR_ENABLE_REG16);
    625  1.1  uch 	r16 |=  SPD_INTR_EMAC3 | SPD_INTR_RXEND | SPD_INTR_TXEND |
    626  1.1  uch 	    SPD_INTR_RXDNV;
    627  1.1  uch 	_reg_write_2(SPD_INTR_ENABLE_REG16, r16);
    628  1.1  uch 	emac3_intr_enable();
    629  1.1  uch 
    630  1.1  uch 	emac3_enable();
    631  1.1  uch 
    632  1.1  uch 	/* Program the multicast filter, if necessary. */
    633  1.1  uch 	emac3_setmulti(&sc->emac3, &sc->ethercom);
    634  1.1  uch 
    635  1.1  uch 	/* Set current media. */
    636  1.1  uch 	mii_mediachg(&sc->emac3.mii);
    637  1.1  uch 
    638  1.1  uch 	ifp->if_flags |= IFF_RUNNING;
    639  1.1  uch 
    640  1.1  uch 	return (0);
    641  1.1  uch }
    642  1.1  uch 
    643  1.1  uch void
    644  1.1  uch smap_stop(struct ifnet *ifp, int disable)
    645  1.1  uch {
    646  1.1  uch 	struct smap_softc *sc = ifp->if_softc;
    647  1.1  uch 
    648  1.1  uch 	mii_down(&sc->emac3.mii);
    649  1.1  uch 
    650  1.1  uch 	if (disable)
    651  1.1  uch 		emac3_disable();
    652  1.1  uch 
    653  1.1  uch 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    654  1.1  uch }
    655  1.1  uch 
    656  1.1  uch /*
    657  1.1  uch  * FIFO
    658  1.1  uch  */
    659  1.1  uch int
    660  1.1  uch smap_fifo_init(struct smap_softc *sc)
    661  1.1  uch {
    662  1.1  uch 
    663  1.1  uch 	if (smap_fifo_reset(SMAP_TXFIFO_CTRL_REG8) != 0)
    664  1.1  uch 		goto error;
    665  1.1  uch 
    666  1.1  uch 	if (smap_fifo_reset(SMAP_RXFIFO_CTRL_REG8) != 0)
    667  1.1  uch 		goto error;
    668  1.1  uch 
    669  1.1  uch 	return (0);
    670  1.1  uch error:
    671  1.1  uch 	printf("%s: FIFO reset not complete.\n", DEVNAME);
    672  1.1  uch 
    673  1.1  uch 	return (1);
    674  1.1  uch }
    675  1.1  uch 
    676  1.1  uch int
    677  1.1  uch smap_fifo_reset(bus_addr_t a)
    678  1.1  uch {
    679  1.1  uch 	int retry = 10000;
    680  1.1  uch 
    681  1.1  uch 	_reg_write_1(a, SMAP_FIFO_RESET);
    682  1.1  uch 
    683  1.1  uch 	while ((_reg_read_1(a) & SMAP_FIFO_RESET) && --retry > 0)
    684  1.1  uch 		;
    685  1.1  uch 
    686  1.1  uch 	return (retry == 0);
    687  1.1  uch }
    688  1.1  uch 
    689  1.1  uch /*
    690  1.1  uch  * Buffer descriptor
    691  1.1  uch  */
    692  1.1  uch void
    693  1.1  uch smap_desc_init(struct smap_softc *sc)
    694  1.1  uch {
    695  1.1  uch 	struct smap_desc *d;
    696  1.1  uch 	int i;
    697  1.1  uch 
    698  1.1  uch 	sc->tx_desc = (void *)SMAP_TXDESC_BASE;
    699  1.1  uch 	sc->rx_desc = (void *)SMAP_RXDESC_BASE;
    700  1.1  uch 
    701  1.1  uch 	sc->tx_buf_freesize = SMAP_TXBUF_SIZE;
    702  1.1  uch 	sc->tx_fifo_ptr = 0;
    703  1.1  uch 	sc->tx_start_index = 0;
    704  1.1  uch 	sc->tx_done_index = 0;
    705  1.1  uch 	sc->rx_done_index = 0;
    706  1.1  uch 
    707  1.1  uch 	/* intialize entry */
    708  1.1  uch 	d = sc->tx_desc;
    709  1.1  uch 	for (i = 0; i < SMAP_DESC_MAX; i++, d++) {
    710  1.1  uch 		d->stat = 0;
    711  1.1  uch 		d->__reserved = 0;
    712  1.1  uch 		d->sz = 0;
    713  1.1  uch 		d->ptr = 0;
    714  1.1  uch 	}
    715  1.1  uch 
    716  1.1  uch 	d = sc->rx_desc;
    717  1.1  uch 	for (i = 0; i < SMAP_DESC_MAX; i++, d++) {
    718  1.1  uch 		d->stat = SMAP_RXDESC_EMPTY;
    719  1.1  uch 		d->__reserved = 0;
    720  1.1  uch 		d->sz = 0;
    721  1.1  uch 		d->ptr = 0;
    722  1.1  uch 	}
    723  1.1  uch 	_wbflush();
    724  1.1  uch }
    725  1.1  uch 
    726  1.1  uch 
    727  1.1  uch /*
    728  1.1  uch  * EEPROM
    729  1.1  uch  */
    730  1.1  uch int
    731  1.1  uch smap_get_eaddr(struct smap_softc *sc, u_int8_t *eaddr)
    732  1.1  uch {
    733  1.1  uch 	u_int16_t checksum, *p = (u_int16_t *)eaddr;
    734  1.1  uch 
    735  1.1  uch 	spd_eeprom_read(0, p, 3);
    736  1.1  uch 	spd_eeprom_read(3, &checksum, 1);
    737  1.1  uch 
    738  1.1  uch 	if (checksum != (u_int16_t)(p[0] + p[1] + p[2])) {
    739  1.1  uch 		printf("%s: Ethernet address checksum error.(%s)\n",
    740  1.1  uch 		    DEVNAME, ether_sprintf(eaddr));
    741  1.1  uch 		return (1);
    742  1.1  uch 	}
    743  1.1  uch 
    744  1.1  uch 	return (0);
    745  1.1  uch }
    746  1.1  uch 
    747  1.1  uch #ifdef SMAP_DEBUG
    748  1.1  uch #include <mips/locore.h>
    749  1.1  uch void
    750  1.1  uch __smap_lock_check(const char *func, int enter)
    751  1.1  uch {
    752  1.1  uch 	static int cnt;
    753  1.1  uch 	static const char *last;
    754  1.1  uch 
    755  1.1  uch 	cnt += enter ? 1 : -1;
    756  1.1  uch 
    757  1.1  uch 	if (cnt < 0 || cnt > 1)
    758  1.1  uch 		panic("%s cnt=%d last=%s", func, cnt, last);
    759  1.1  uch 
    760  1.1  uch 	last = func;
    761  1.1  uch }
    762  1.1  uch 
    763  1.1  uch void
    764  1.1  uch __smap_status(int msg)
    765  1.1  uch {
    766  1.1  uch 	static int cnt;
    767  1.1  uch 	__gsfb_print(1, "%d: tx=%d rx=%d txcnt=%d free=%d cnt=%d\n", msg,
    768  1.1  uch 	    _reg_read_1(SMAP_TXFIFO_FRAME_REG8),
    769  1.1  uch 	    _reg_read_1(SMAP_RXFIFO_FRAME_REG8), __sc->tx_desc_cnt,
    770  1.1  uch 	    __sc->tx_buf_freesize, cnt++);
    771  1.1  uch }
    772  1.1  uch #endif /* SMAP_DEBUG */
    773