Home | History | Annotate | Line # | Download | only in nubus
if_netdock_nubus.c revision 1.6
      1  1.6      chs /*	$NetBSD: if_netdock_nubus.c,v 1.6 2005/01/15 16:00:59 chs Exp $	*/
      2  1.1   itojun 
      3  1.1   itojun /*
      4  1.1   itojun  * Copyright (C) 2000,2002 Daishi Kato <daishi (at) axlight.com>
      5  1.1   itojun  * All rights reserved.
      6  1.1   itojun  *
      7  1.1   itojun  * Redistribution and use in source and binary forms, with or without
      8  1.1   itojun  * modification, are permitted provided that the following conditions
      9  1.1   itojun  * are met:
     10  1.1   itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.1   itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.1   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   itojun  *    documentation and/or other materials provided with the distribution.
     15  1.1   itojun  * 3. All advertising materials mentioning features or use of this software
     16  1.1   itojun  *    must display the following acknowledgement:
     17  1.1   itojun  *      This product includes software developed by Daishi Kato
     18  1.1   itojun  * 4. The name of the author may not be used to endorse or promote products
     19  1.1   itojun  *    derived from this software without specific prior written permission.
     20  1.1   itojun  *
     21  1.1   itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1   itojun  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1   itojun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1   itojun  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1   itojun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1   itojun  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1   itojun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1   itojun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1   itojun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1   itojun  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1   itojun  */
     32  1.1   itojun 
     33  1.1   itojun /*
     34  1.1   itojun  * Asante NetDock (for Duo series) driver
     35  1.1   itojun  * the chip inside is not known
     36  1.1   itojun  */
     37  1.1   itojun 
     38  1.1   itojun /*
     39  1.1   itojun  * The author would like to thank Takeo Kuwata <tkuwata (at) mac.com> for
     40  1.1   itojun  * his help in stabilizing this driver.
     41  1.1   itojun  */
     42  1.1   itojun 
     43  1.1   itojun /***********************/
     44  1.4    lukem 
     45  1.4    lukem #include <sys/cdefs.h>
     46  1.6      chs __KERNEL_RCSID(0, "$NetBSD: if_netdock_nubus.c,v 1.6 2005/01/15 16:00:59 chs Exp $");
     47  1.1   itojun 
     48  1.1   itojun #include <sys/param.h>
     49  1.1   itojun #include <sys/device.h>
     50  1.1   itojun #include <sys/socket.h>
     51  1.1   itojun #include <sys/systm.h>
     52  1.1   itojun #include <sys/mbuf.h>
     53  1.1   itojun #include <sys/ioctl.h>
     54  1.1   itojun 
     55  1.1   itojun #include <net/if.h>
     56  1.1   itojun #include <net/if_dl.h>
     57  1.1   itojun #include <net/if_ether.h>
     58  1.1   itojun 
     59  1.1   itojun #include "opt_inet.h"
     60  1.1   itojun #ifdef INET
     61  1.1   itojun #include <netinet/in.h>
     62  1.1   itojun #include <netinet/if_inarp.h>
     63  1.1   itojun #endif
     64  1.1   itojun 
     65  1.1   itojun #include "bpfilter.h"
     66  1.1   itojun #if NBPFILTER > 0
     67  1.1   itojun #include <net/bpf.h>
     68  1.1   itojun #endif
     69  1.1   itojun 
     70  1.1   itojun #include <machine/bus.h>
     71  1.1   itojun #include <machine/viareg.h>
     72  1.1   itojun #include <mac68k/nubus/nubus.h>
     73  1.1   itojun 
     74  1.1   itojun /***********************/
     75  1.1   itojun 
     76  1.1   itojun #define NETDOCK_DEBUG
     77  1.1   itojun 
     78  1.1   itojun #define NETDOCK_NUBUS_CATEGORY	0x0020
     79  1.1   itojun #define NETDOCK_NUBUS_TYPE	0x0003
     80  1.1   itojun #define NETDOCK_NUBUS_DRSW	0x0103
     81  1.1   itojun #define NETDOCK_NUBUS_DRHW	0x0100
     82  1.1   itojun 
     83  1.1   itojun #define ETHERMICRODOCK_NUBUS_CATEGORY	0x0020
     84  1.1   itojun #define ETHERMICRODOCK_NUBUS_TYPE	0x0003
     85  1.1   itojun #define ETHERMICRODOCK_NUBUS_DRSW	0x0102
     86  1.1   itojun #define ETHERMICRODOCK_NUBUS_DRHW	0x0100
     87  1.1   itojun 
     88  1.1   itojun #define REG_ISR		0x000c
     89  1.1   itojun #define REG_000E	0x000e
     90  1.1   itojun #define REG_0000	0x0000
     91  1.1   itojun #define REG_0002	0x0002
     92  1.1   itojun #define REG_0004	0x0004
     93  1.1   itojun #define REG_0006	0x0006
     94  1.1   itojun #define REG_DATA	0x0008
     95  1.1   itojun #define REG_EFD00	0xefd00
     96  1.1   itojun 
     97  1.1   itojun #define ISR_ALL		0x3300
     98  1.1   itojun #define ISR_TX		0x0200
     99  1.1   itojun #define ISR_RX		0x0100
    100  1.1   itojun #define ISR_READY	0x0800
    101  1.1   itojun #define ISR_BIT_0C	0x1000
    102  1.1   itojun #define ISR_BIT_0D	0x2000
    103  1.1   itojun #define ISR_MASK	0x0033
    104  1.1   itojun #define ISR_BIT_03	0x0008
    105  1.1   itojun 
    106  1.1   itojun #define REG_0002_BIT_04	0x0010
    107  1.1   itojun #define REG_0000_BIT_08	0x0100
    108  1.1   itojun #define REG_0004_BIT_0F	0x8000
    109  1.1   itojun #define REG_0004_BIT_07 0x0080
    110  1.1   itojun #define REG_DATA_BIT_02 0x0004
    111  1.1   itojun #define REG_DATA_BIT_03 0x0008
    112  1.1   itojun #define REG_DATA_BIT_04 0x0010
    113  1.1   itojun #define REG_DATA_BIT_05 0x0020
    114  1.1   itojun #define REG_DATA_BIT_08	0x0100
    115  1.1   itojun #define REG_DATA_BIT_07	0x0080
    116  1.1   itojun #define REG_DATA_BIT_0F	0x8000
    117  1.1   itojun 
    118  1.1   itojun /***********************/
    119  1.1   itojun 
    120  1.1   itojun typedef struct netdock_softc {
    121  1.1   itojun 	struct device	sc_dev;
    122  1.1   itojun 	struct ethercom	sc_ethercom;
    123  1.1   itojun #define sc_if		sc_ethercom.ec_if
    124  1.1   itojun 
    125  1.1   itojun 	bus_space_tag_t		sc_regt;
    126  1.1   itojun 	bus_space_handle_t	sc_regh;
    127  1.1   itojun 
    128  1.1   itojun 	u_int8_t	sc_enaddr[ETHER_ADDR_LEN];
    129  1.1   itojun 
    130  1.1   itojun } netdock_softc_t;
    131  1.1   itojun 
    132  1.1   itojun /***********************/
    133  1.1   itojun 
    134  1.6      chs static int	netdock_nubus_match(struct device *, struct cfdata *, void *);
    135  1.6      chs static void	netdock_nubus_attach(struct device *, struct device *, void *);
    136  1.6      chs static int	netdock_nb_get_enaddr(bus_space_tag_t, bus_space_handle_t,
    137  1.6      chs 			struct nubus_attach_args *, u_int8_t *);
    138  1.1   itojun #ifdef NETDOCK_DEBUG_DRIVER
    139  1.6      chs static void	netdock_print_driver(bus_space_tag_t, bus_space_handle_t,
    140  1.6      chs 			struct nubus_attach_args *);
    141  1.1   itojun #endif
    142  1.1   itojun 
    143  1.6      chs int	netdock_setup(struct netdock_softc *, u_int8_t *);
    144  1.6      chs void	netdock_intr(void *);
    145  1.1   itojun 
    146  1.6      chs static void	netdock_watchdog(struct ifnet *);
    147  1.6      chs static int	netdock_init(struct netdock_softc *);
    148  1.6      chs static int	netdock_stop(struct netdock_softc *);
    149  1.6      chs static int	netdock_ioctl(struct ifnet *, u_long, caddr_t);
    150  1.6      chs static void	netdock_start(struct ifnet *);
    151  1.6      chs static void	netdock_reset(struct netdock_softc *);
    152  1.6      chs static void	netdock_txint(struct netdock_softc *);
    153  1.6      chs static void	netdock_rxint(struct netdock_softc *);
    154  1.6      chs 
    155  1.6      chs static u_int	netdock_put(struct netdock_softc *, struct mbuf *);
    156  1.6      chs static int	netdock_read(struct netdock_softc *, int);
    157  1.6      chs static struct mbuf *netdock_get(struct netdock_softc *, int);
    158  1.1   itojun 
    159  1.1   itojun /***********************/
    160  1.1   itojun 
    161  1.1   itojun #define NIC_GET_1(sc, o)	(bus_space_read_1((sc)->sc_regt,	\
    162  1.1   itojun 					(sc)->sc_regh, (o)))
    163  1.1   itojun #define NIC_PUT_1(sc, o, val)	(bus_space_write_1((sc)->sc_regt,	\
    164  1.1   itojun 					(sc)->sc_regh, (o), (val)))
    165  1.1   itojun #define NIC_GET_2(sc, o)	(bus_space_read_2((sc)->sc_regt,	\
    166  1.1   itojun 					(sc)->sc_regh, (o)))
    167  1.1   itojun #define NIC_PUT_2(sc, o, val)	(bus_space_write_2((sc)->sc_regt,	\
    168  1.1   itojun 					(sc)->sc_regh, (o), (val)))
    169  1.1   itojun #define NIC_GET_4(sc, o)	(bus_space_read_4((sc)->sc_regt,	\
    170  1.1   itojun 					(sc)->sc_regh, (o)))
    171  1.1   itojun #define NIC_PUT_4(sc, o, val)	(bus_space_write_4((sc)->sc_regt,	\
    172  1.1   itojun 					(sc)->sc_regh, (o), (val)))
    173  1.1   itojun 
    174  1.1   itojun #define NIC_BSET(sc, o, b)						\
    175  1.1   itojun 	__asm__ __volatile("bset %0,%1" : : "di" ((u_short)(b)),	\
    176  1.1   itojun 		"g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
    177  1.1   itojun #define NIC_BCLR(sc, o, b)						\
    178  1.1   itojun 	__asm__ __volatile("bclr %0,%1" : : "di" ((u_short)(b)),	\
    179  1.1   itojun 		"g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
    180  1.1   itojun #define NIC_ANDW(sc, o, b)						\
    181  1.1   itojun 	__asm__ __volatile("andw %0,%1" : : "di" ((u_short)(b)),	\
    182  1.1   itojun 		"g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
    183  1.1   itojun #define NIC_ORW(sc, o, b)						\
    184  1.1   itojun 	__asm__ __volatile("orw %0,%1" : : "di" ((u_short)(b)),	\
    185  1.1   itojun 		"g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
    186  1.1   itojun 
    187  1.1   itojun 
    188  1.1   itojun /***********************/
    189  1.1   itojun 
    190  1.3  thorpej CFATTACH_DECL(netdock_nubus, sizeof(struct netdock_softc),
    191  1.3  thorpej     netdock_nubus_match, netdock_nubus_attach, NULL, NULL);
    192  1.1   itojun 
    193  1.1   itojun /***********************/
    194  1.1   itojun 
    195  1.1   itojun static int
    196  1.6      chs netdock_nubus_match(struct device *parent, struct cfdata *cf, void *aux)
    197  1.1   itojun {
    198  1.1   itojun 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
    199  1.1   itojun 	bus_space_handle_t bsh;
    200  1.1   itojun 	int rv;
    201  1.1   itojun 
    202  1.1   itojun 	if (bus_space_map(na->na_tag, NUBUS_SLOT2PA(na->slot),
    203  1.1   itojun 	    NBMEMSIZE, 0, &bsh))
    204  1.1   itojun 		return (0);
    205  1.1   itojun 
    206  1.1   itojun 	rv = 0;
    207  1.1   itojun 
    208  1.1   itojun 	if (na->category == NETDOCK_NUBUS_CATEGORY &&
    209  1.1   itojun 	    na->type == NETDOCK_NUBUS_TYPE &&
    210  1.1   itojun 	    na->drsw == NETDOCK_NUBUS_DRSW) {
    211  1.1   itojun 		/* assuming this IS Asante NetDock */
    212  1.1   itojun 		rv = 1;
    213  1.1   itojun 	}
    214  1.1   itojun 
    215  1.1   itojun 	if (na->category == ETHERMICRODOCK_NUBUS_CATEGORY &&
    216  1.1   itojun 	    na->type == ETHERMICRODOCK_NUBUS_TYPE &&
    217  1.1   itojun 	    na->drsw == ETHERMICRODOCK_NUBUS_DRSW) {
    218  1.1   itojun 		/* assuming this IS Newer EtherMicroDock */
    219  1.1   itojun 		rv = 1;
    220  1.1   itojun 	}
    221  1.1   itojun 
    222  1.1   itojun 	bus_space_unmap(na->na_tag, bsh, NBMEMSIZE);
    223  1.1   itojun 
    224  1.1   itojun 	return rv;
    225  1.1   itojun }
    226  1.1   itojun 
    227  1.1   itojun static void
    228  1.6      chs netdock_nubus_attach(struct device *parent, struct device *self, void *aux)
    229  1.1   itojun {
    230  1.1   itojun 	struct netdock_softc *sc = (struct netdock_softc *)self;
    231  1.1   itojun 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
    232  1.1   itojun 	bus_space_tag_t bst;
    233  1.1   itojun 	bus_space_handle_t bsh;
    234  1.1   itojun 	u_int8_t enaddr[ETHER_ADDR_LEN];
    235  1.1   itojun 	char *cardtype;
    236  1.1   itojun 
    237  1.1   itojun 	bst = na->na_tag;
    238  1.1   itojun 	if (bus_space_map(bst, NUBUS_SLOT2PA(na->slot), NBMEMSIZE, 0, &bsh)) {
    239  1.1   itojun 		printf(": failed to map memory space.\n");
    240  1.1   itojun 		return;
    241  1.1   itojun 	}
    242  1.1   itojun 
    243  1.1   itojun 	sc->sc_regt = bst;
    244  1.1   itojun 	cardtype = nubus_get_card_name(bst, bsh, na->fmt);
    245  1.1   itojun 
    246  1.1   itojun #ifdef NETDOCK_DEBUG_DRIVER
    247  1.1   itojun 	netdock_print_driver(bst, bsh, na);
    248  1.1   itojun #endif
    249  1.1   itojun 
    250  1.1   itojun 	if (netdock_nb_get_enaddr(bst, bsh, na, enaddr)) {
    251  1.1   itojun 		printf(": can't find MAC address.\n");
    252  1.1   itojun 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    253  1.1   itojun 		return;
    254  1.1   itojun 	}
    255  1.1   itojun 
    256  1.1   itojun 	if (bus_space_subregion(bst, bsh, 0xe00300, 0xf0000, &sc->sc_regh)) {
    257  1.1   itojun 		printf(": failed to map register space.\n");
    258  1.1   itojun 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    259  1.1   itojun 		return;
    260  1.1   itojun 	}
    261  1.1   itojun 
    262  1.1   itojun 	printf(": %s\n", cardtype);
    263  1.1   itojun 
    264  1.1   itojun 	if (netdock_setup(sc, enaddr)) {
    265  1.1   itojun 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    266  1.1   itojun 		return;
    267  1.1   itojun 	}
    268  1.1   itojun 
    269  1.1   itojun 	add_nubus_intr(na->slot, netdock_intr, (void *)sc);
    270  1.1   itojun 
    271  1.1   itojun 	return;
    272  1.1   itojun }
    273  1.1   itojun 
    274  1.1   itojun static int
    275  1.6      chs netdock_nb_get_enaddr(bus_space_tag_t bst, bus_space_handle_t bsh,
    276  1.6      chs     struct nubus_attach_args *na, u_int8_t *ep)
    277  1.1   itojun {
    278  1.1   itojun 	nubus_dir dir;
    279  1.1   itojun 	nubus_dirent dirent;
    280  1.1   itojun 
    281  1.1   itojun 	/*
    282  1.1   itojun 	 * these hardwired resource IDs are only for NetDock
    283  1.1   itojun 	 */
    284  1.1   itojun 	nubus_get_main_dir(na->fmt, &dir);
    285  1.1   itojun 	if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x81, &dirent) <= 0)
    286  1.1   itojun 		return 1;
    287  1.1   itojun 	nubus_get_dir_from_rsrc(na->fmt, &dirent, &dir);
    288  1.1   itojun 	if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x80, &dirent) <= 0)
    289  1.1   itojun 		return 1;
    290  1.1   itojun 	if (nubus_get_ind_data(bst, bsh, na->fmt, &dirent,
    291  1.1   itojun 		ep, ETHER_ADDR_LEN) <= 0)
    292  1.1   itojun 		return 1;
    293  1.1   itojun 
    294  1.1   itojun 	return 0;
    295  1.1   itojun }
    296  1.1   itojun 
    297  1.1   itojun #ifdef NETDOCK_DEBUG_DRIVER
    298  1.1   itojun static void
    299  1.6      chs netdock_print_driver(bus_space_tag_t bst, bus_space_handle_t bsh,
    300  1.6      chs     struct nubus_attach_args *na)
    301  1.1   itojun {
    302  1.1   itojun #define HEADSIZE	(8+4)
    303  1.1   itojun #define CODESIZE	(6759-4)
    304  1.1   itojun 	unsigned char mydata[HEADSIZE + CODESIZE];
    305  1.1   itojun 	nubus_dir dir;
    306  1.1   itojun 	nubus_dirent dirent;
    307  1.1   itojun 	int i, rv;
    308  1.1   itojun 
    309  1.1   itojun 	nubus_get_main_dir(na->fmt, &dir);
    310  1.1   itojun 	rv = nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x81, &dirent);
    311  1.1   itojun 	if (rv <= 0) {
    312  1.1   itojun 		printf(": can't find sResource.\n");
    313  1.1   itojun 		return;
    314  1.1   itojun 	}
    315  1.1   itojun 	nubus_get_dir_from_rsrc(na->fmt, &dirent, &dir);
    316  1.1   itojun 	if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, NUBUS_RSRC_DRVRDIR,
    317  1.1   itojun 	    &dirent) <= 0) {
    318  1.1   itojun 		printf(": can't find sResource.\n");
    319  1.1   itojun 		return;
    320  1.1   itojun 	}
    321  1.1   itojun 	if (nubus_get_ind_data(bst, bsh, na->fmt,
    322  1.1   itojun 		&dirent, mydata, HEADSIZE+CODESIZE) <= 0) {
    323  1.1   itojun 		printf(": can't find indirect data.\n");
    324  1.1   itojun 		return;
    325  1.1   itojun 	}
    326  1.1   itojun 	printf("\n########## begin driver dir");
    327  1.1   itojun 	for (i = 0; i < HEADSIZE; i++) {
    328  1.1   itojun 		if (i % 16 == 0)
    329  1.1   itojun 			printf("\n%02x:",i);
    330  1.1   itojun 		printf(" %02x", mydata[i]);
    331  1.1   itojun 	}
    332  1.1   itojun 	printf("\n########## begin driver code");
    333  1.1   itojun 	for (i = 0; i < CODESIZE; i++) {
    334  1.1   itojun 		if (i % 16 == 0)
    335  1.1   itojun 			printf("\n%02x:",i);
    336  1.1   itojun 		printf(" %02x", mydata[i + HEADSIZE]);
    337  1.1   itojun 	}
    338  1.1   itojun #if 0
    339  1.1   itojun 	printf("\n########## begin driver code (partial)\n");
    340  1.1   itojun #define PARTSIZE 256
    341  1.1   itojun #define OFFSET 0x1568
    342  1.1   itojun 	for (i = OFFSET; i < OFFSET + PARTSIZE; i++) {
    343  1.1   itojun 		if ((i - OFFSET) % 16 == 0)
    344  1.1   itojun 			printf("\n%02x:",i);
    345  1.1   itojun 		printf(" %02x", mydata[i + HEADSIZE]);
    346  1.1   itojun 	}
    347  1.1   itojun #endif
    348  1.1   itojun 	printf("\n########## end\n");
    349  1.1   itojun }
    350  1.1   itojun #endif
    351  1.1   itojun 
    352  1.1   itojun 
    353  1.1   itojun int
    354  1.6      chs netdock_setup(struct netdock_softc *sc, u_int8_t *lladdr)
    355  1.1   itojun {
    356  1.1   itojun 	struct ifnet *ifp = &sc->sc_if;
    357  1.1   itojun 
    358  1.6      chs 	memcpy(sc->sc_enaddr, lladdr, ETHER_ADDR_LEN);
    359  1.1   itojun 	printf("%s: Ethernet address %s\n",
    360  1.1   itojun 	    sc->sc_dev.dv_xname, ether_sprintf(lladdr));
    361  1.1   itojun 
    362  1.6      chs 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    363  1.1   itojun 	ifp->if_softc = sc;
    364  1.1   itojun 	ifp->if_ioctl = netdock_ioctl;
    365  1.1   itojun 	ifp->if_start = netdock_start;
    366  1.1   itojun 	ifp->if_flags =
    367  1.1   itojun 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    368  1.1   itojun 	ifp->if_watchdog = netdock_watchdog;
    369  1.1   itojun 
    370  1.1   itojun 	if_attach(ifp);
    371  1.1   itojun 	ether_ifattach(ifp, lladdr);
    372  1.1   itojun 
    373  1.1   itojun 	return (0);
    374  1.1   itojun }
    375  1.1   itojun 
    376  1.1   itojun static int
    377  1.6      chs netdock_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    378  1.1   itojun {
    379  1.1   itojun 	struct ifaddr *ifa;
    380  1.1   itojun 	struct ifreq *ifr;
    381  1.1   itojun 	struct netdock_softc *sc = ifp->if_softc;
    382  1.1   itojun 	int s = splnet();
    383  1.1   itojun 	int err = 0;
    384  1.1   itojun 	int temp;
    385  1.1   itojun 
    386  1.1   itojun 	switch (cmd) {
    387  1.1   itojun 	case SIOCSIFADDR:
    388  1.1   itojun 		ifa = (struct ifaddr *)data;
    389  1.1   itojun 		ifp->if_flags |= IFF_UP;
    390  1.1   itojun 		switch (ifa->ifa_addr->sa_family) {
    391  1.1   itojun #ifdef INET
    392  1.1   itojun 		case AF_INET:
    393  1.1   itojun 			(void)netdock_init(sc);
    394  1.1   itojun 			arp_ifinit(ifp, ifa);
    395  1.1   itojun 			break;
    396  1.1   itojun #endif
    397  1.1   itojun 		default:
    398  1.1   itojun 			(void)netdock_init(sc);
    399  1.1   itojun 			break;
    400  1.1   itojun 		}
    401  1.1   itojun 		break;
    402  1.1   itojun 
    403  1.1   itojun 	case SIOCSIFFLAGS:
    404  1.1   itojun 		if ((ifp->if_flags & IFF_UP) == 0 &&
    405  1.1   itojun 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    406  1.1   itojun 			netdock_stop(sc);
    407  1.1   itojun 			ifp->if_flags &= ~IFF_RUNNING;
    408  1.1   itojun 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    409  1.1   itojun 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    410  1.1   itojun 			(void)netdock_init(sc);
    411  1.1   itojun 		} else {
    412  1.1   itojun 			temp = ifp->if_flags & IFF_UP;
    413  1.1   itojun 			netdock_reset(sc);
    414  1.1   itojun 			ifp->if_flags |= temp;
    415  1.1   itojun 			netdock_start(ifp);
    416  1.1   itojun 		}
    417  1.1   itojun 		break;
    418  1.1   itojun 
    419  1.1   itojun 	case SIOCADDMULTI:
    420  1.1   itojun 	case SIOCDELMULTI:
    421  1.1   itojun 		ifr = (struct ifreq *)data;
    422  1.1   itojun 		if (cmd == SIOCADDMULTI)
    423  1.1   itojun 			err = ether_addmulti(ifr, &sc->sc_ethercom);
    424  1.1   itojun 		else
    425  1.1   itojun 			err = ether_delmulti(ifr, &sc->sc_ethercom);
    426  1.1   itojun 
    427  1.1   itojun 		if (err == ENETRESET) {
    428  1.5  thorpej 			if (ifp->if_flags & IFF_RUNNING) {
    429  1.5  thorpej 				temp = ifp->if_flags & IFF_UP;
    430  1.5  thorpej 				netdock_reset(sc);
    431  1.5  thorpej 				ifp->if_flags |= temp;
    432  1.5  thorpej 			}
    433  1.1   itojun 			err = 0;
    434  1.1   itojun 		}
    435  1.1   itojun 		break;
    436  1.1   itojun 	default:
    437  1.1   itojun 		err = EINVAL;
    438  1.1   itojun 		break;
    439  1.1   itojun 	}
    440  1.1   itojun 	splx(s);
    441  1.1   itojun 	return (err);
    442  1.1   itojun }
    443  1.1   itojun 
    444  1.1   itojun static void
    445  1.6      chs netdock_start(struct ifnet *ifp)
    446  1.1   itojun {
    447  1.1   itojun 	struct netdock_softc *sc = ifp->if_softc;
    448  1.1   itojun 	struct mbuf *m;
    449  1.1   itojun 
    450  1.1   itojun 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    451  1.1   itojun 		return;
    452  1.1   itojun 
    453  1.1   itojun 	while (1) {
    454  1.1   itojun 		IF_DEQUEUE(&ifp->if_snd, m);
    455  1.1   itojun 		if (m == 0)
    456  1.1   itojun 			return;
    457  1.1   itojun 
    458  1.1   itojun 		if ((m->m_flags & M_PKTHDR) == 0)
    459  1.1   itojun 			panic("%s: netdock_start: no header mbuf",
    460  1.1   itojun 				sc->sc_dev.dv_xname);
    461  1.1   itojun 
    462  1.1   itojun #if NBPFILTER > 0
    463  1.1   itojun 		if (ifp->if_bpf)
    464  1.1   itojun 			bpf_mtap(ifp->if_bpf, m);
    465  1.1   itojun #endif
    466  1.1   itojun 
    467  1.1   itojun 		if ((netdock_put(sc, m)) == 0) {
    468  1.1   itojun 			IF_PREPEND(&ifp->if_snd, m);
    469  1.1   itojun 			return;
    470  1.1   itojun 		}
    471  1.1   itojun 
    472  1.1   itojun 		ifp->if_opackets++;
    473  1.1   itojun 	}
    474  1.1   itojun 
    475  1.1   itojun }
    476  1.1   itojun 
    477  1.1   itojun static void
    478  1.6      chs netdock_reset(struct netdock_softc *sc)
    479  1.1   itojun {
    480  1.1   itojun 
    481  1.1   itojun 	netdock_stop(sc);
    482  1.1   itojun 	netdock_init(sc);
    483  1.1   itojun }
    484  1.1   itojun 
    485  1.1   itojun static int
    486  1.6      chs netdock_init(struct netdock_softc *sc)
    487  1.1   itojun {
    488  1.1   itojun 	int s;
    489  1.1   itojun 	int saveisr;
    490  1.1   itojun 	int savetmp;
    491  1.1   itojun 
    492  1.1   itojun 	if (sc->sc_if.if_flags & IFF_RUNNING)
    493  1.1   itojun 		return (0);
    494  1.1   itojun 
    495  1.1   itojun 	s = splnet();
    496  1.1   itojun 
    497  1.1   itojun 	/* 0606 */
    498  1.1   itojun 	NIC_PUT_2(sc, REG_000E, 0x0200);
    499  1.1   itojun 	NIC_PUT_2(sc, REG_ISR , 0);
    500  1.1   itojun 	NIC_PUT_2(sc, REG_000E, 0);
    501  1.1   itojun 
    502  1.1   itojun 	NIC_PUT_2(sc, REG_0000, 0x8104);
    503  1.1   itojun 	NIC_PUT_2(sc, REG_0004, 0x0043);
    504  1.1   itojun 	NIC_PUT_2(sc, REG_000E, 0x0100);
    505  1.1   itojun 	NIC_PUT_2(sc, REG_0002, 0x6618);
    506  1.1   itojun 	NIC_PUT_2(sc, REG_0000, 0x8010);
    507  1.1   itojun 
    508  1.1   itojun 	NIC_PUT_1(sc, REG_0004 + 0, sc->sc_enaddr[0]);
    509  1.1   itojun 	NIC_PUT_1(sc, REG_0004 + 1, sc->sc_enaddr[1]);
    510  1.1   itojun 	NIC_PUT_1(sc, REG_0004 + 2, sc->sc_enaddr[2]);
    511  1.1   itojun 	NIC_PUT_1(sc, REG_0004 + 3, sc->sc_enaddr[3]);
    512  1.1   itojun 	NIC_PUT_1(sc, REG_0004 + 4, sc->sc_enaddr[4]);
    513  1.1   itojun 	NIC_PUT_1(sc, REG_0004 + 5, sc->sc_enaddr[5]);
    514  1.1   itojun 
    515  1.1   itojun 	NIC_PUT_2(sc, REG_ISR , 0x2008);
    516  1.1   itojun 	NIC_PUT_2(sc, REG_000E, 0x0200);
    517  1.1   itojun 	NIC_PUT_2(sc, REG_0000, 0x4000);
    518  1.1   itojun 	NIC_PUT_2(sc, REG_ISR, ISR_MASK);
    519  1.1   itojun 
    520  1.1   itojun 
    521  1.1   itojun 	/* 1320 */
    522  1.1   itojun 	saveisr = NIC_GET_2(sc, REG_ISR);
    523  1.1   itojun 	NIC_PUT_2(sc, REG_ISR , 0);
    524  1.1   itojun 	savetmp = NIC_GET_2(sc, REG_000E);
    525  1.1   itojun 	NIC_PUT_2(sc, REG_000E, 0x0100);
    526  1.1   itojun 	NIC_ANDW(sc, REG_ISR, ~ISR_BIT_03);
    527  1.1   itojun 	NIC_PUT_2(sc, REG_000E, savetmp);
    528  1.1   itojun 
    529  1.1   itojun 	/* 1382 */
    530  1.1   itojun 	savetmp = NIC_GET_2(sc, REG_000E);
    531  1.1   itojun 	NIC_PUT_2(sc, REG_000E, 0x0100);
    532  1.1   itojun 	NIC_ORW(sc, REG_ISR, ISR_BIT_03);
    533  1.1   itojun 	NIC_PUT_2(sc, REG_000E, savetmp);
    534  1.1   itojun 	NIC_PUT_2(sc, REG_ISR , saveisr);
    535  1.1   itojun 
    536  1.1   itojun 
    537  1.1   itojun 	sc->sc_if.if_flags |= IFF_RUNNING;
    538  1.1   itojun 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    539  1.1   itojun 
    540  1.1   itojun 	splx(s);
    541  1.1   itojun 	return (0);
    542  1.1   itojun }
    543  1.1   itojun 
    544  1.1   itojun static int
    545  1.6      chs netdock_stop(struct netdock_softc *sc)
    546  1.1   itojun {
    547  1.1   itojun 	int s = splnet();
    548  1.1   itojun 
    549  1.1   itojun 	sc->sc_if.if_timer = 0;
    550  1.1   itojun 	sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
    551  1.1   itojun 
    552  1.1   itojun 	splx(s);
    553  1.1   itojun 	return (0);
    554  1.1   itojun }
    555  1.1   itojun 
    556  1.1   itojun static void
    557  1.6      chs netdock_watchdog(struct ifnet *ifp)
    558  1.1   itojun {
    559  1.1   itojun 	struct netdock_softc *sc = ifp->if_softc;
    560  1.1   itojun 	int tmp;
    561  1.1   itojun 
    562  1.1   itojun 	printf("netdock_watchdog: resetting chip\n");
    563  1.1   itojun 	tmp = ifp->if_flags & IFF_UP;
    564  1.1   itojun 	netdock_reset(sc);
    565  1.1   itojun 	ifp->if_flags |= tmp;
    566  1.1   itojun }
    567  1.1   itojun 
    568  1.1   itojun static u_int
    569  1.6      chs netdock_put(struct netdock_softc *sc, struct mbuf *m0)
    570  1.1   itojun {
    571  1.1   itojun 	struct mbuf *m;
    572  1.1   itojun 	u_int totlen = 0;
    573  1.1   itojun 	u_int tmplen;
    574  1.1   itojun 	int isr;
    575  1.1   itojun 	int timeout;
    576  1.1   itojun 	int tmp;
    577  1.1   itojun 	u_int i;
    578  1.1   itojun 
    579  1.1   itojun 	for (m = m0; m; m = m->m_next)
    580  1.1   itojun 		totlen += m->m_len;
    581  1.1   itojun 
    582  1.1   itojun 	if (totlen >= ETHER_MAX_LEN)
    583  1.1   itojun 		panic("%s: netdock_put: packet overflow", sc->sc_dev.dv_xname);
    584  1.1   itojun 
    585  1.1   itojun 	totlen += 6;
    586  1.1   itojun 	tmplen = totlen;
    587  1.1   itojun 	tmplen &= 0xff00;
    588  1.1   itojun 	tmplen |= 0x2000;
    589  1.1   itojun 	NIC_PUT_2(sc, REG_0000, tmplen);
    590  1.1   itojun 
    591  1.1   itojun 	timeout = 0x3000;
    592  1.1   itojun 	while ((((isr = NIC_GET_2(sc, REG_ISR)) & ISR_READY) == 0) &&
    593  1.1   itojun 	    timeout--) {
    594  1.1   itojun 		if (isr & ISR_TX)
    595  1.1   itojun 			netdock_txint(sc);
    596  1.1   itojun 	}
    597  1.1   itojun 	if (timeout == 0)
    598  1.1   itojun 		return (0);
    599  1.1   itojun 
    600  1.1   itojun 	tmp = NIC_GET_2(sc, REG_0002);
    601  1.1   itojun 	tmp <<= 8;
    602  1.1   itojun 	NIC_PUT_2(sc, REG_0002, tmp);
    603  1.1   itojun 	NIC_PUT_2(sc, REG_0006, 0x240);
    604  1.1   itojun 	NIC_GET_2(sc, REG_ISR);
    605  1.1   itojun 	tmplen = ((totlen << 8) & 0xfe00) | ((totlen >> 8) & 0x00ff);
    606  1.1   itojun 	NIC_PUT_2(sc, REG_DATA, tmplen);
    607  1.1   itojun 
    608  1.1   itojun 	for (m = m0; m; m = m->m_next) {
    609  1.1   itojun 		u_char *data = mtod(m, u_char *);
    610  1.1   itojun 		int len = m->m_len;
    611  1.1   itojun 		int len4 = len >> 2;
    612  1.1   itojun 		u_int32_t *data4 = (u_int32_t *)data;
    613  1.1   itojun 		for (i = 0; i < len4; i++)
    614  1.1   itojun 			NIC_PUT_4(sc, REG_DATA, data4[i]);
    615  1.1   itojun 		for (i = len4 << 2; i < len; i++)
    616  1.1   itojun 			NIC_PUT_1(sc, REG_DATA, data[i]);
    617  1.1   itojun 	}
    618  1.1   itojun 
    619  1.1   itojun 	if (totlen & 0x01)
    620  1.1   itojun 		NIC_PUT_2(sc, REG_DATA, 0x2020);
    621  1.1   itojun 	else
    622  1.1   itojun 		NIC_PUT_2(sc, REG_DATA, 0);
    623  1.1   itojun 
    624  1.1   itojun 	NIC_PUT_2(sc, REG_0000, 0xc000);
    625  1.1   itojun 
    626  1.1   itojun 	m_freem(m0);
    627  1.1   itojun 	/* sc->sc_if.if_timer = 5; */
    628  1.1   itojun 	return (totlen);
    629  1.1   itojun }
    630  1.1   itojun 
    631  1.1   itojun void
    632  1.6      chs netdock_intr(void *arg)
    633  1.1   itojun {
    634  1.1   itojun 	struct netdock_softc *sc = (struct netdock_softc *)arg;
    635  1.1   itojun 	int isr;
    636  1.1   itojun 	int tmp;
    637  1.1   itojun 
    638  1.1   itojun 	NIC_PUT_2(sc, REG_ISR, 0);
    639  1.1   itojun 	while ((isr = (NIC_GET_2(sc, REG_ISR) & ISR_ALL)) != 0) {
    640  1.1   itojun 		if (isr & ISR_TX)
    641  1.1   itojun 			netdock_txint(sc);
    642  1.1   itojun 
    643  1.1   itojun 		if (isr & ISR_RX)
    644  1.1   itojun 			netdock_rxint(sc);
    645  1.1   itojun 
    646  1.1   itojun 		if (isr & (ISR_BIT_0C | ISR_BIT_0D)) {
    647  1.1   itojun 			if (isr & ISR_BIT_0C) {
    648  1.1   itojun 				NIC_PUT_2(sc, REG_000E, 0);
    649  1.1   itojun 				NIC_BSET(sc, REG_0004, 0x08);
    650  1.1   itojun 				NIC_PUT_2(sc, REG_000E, 0x0200);
    651  1.1   itojun 			}
    652  1.1   itojun 			if (isr & ISR_BIT_0D) {
    653  1.1   itojun 				NIC_PUT_2(sc, REG_000E, 0);
    654  1.1   itojun 				tmp = NIC_GET_2(sc, REG_0002);
    655  1.1   itojun 				if (tmp & REG_0002_BIT_04)
    656  1.1   itojun 					NIC_GET_2(sc, REG_0006);
    657  1.1   itojun 				tmp = NIC_GET_2(sc, REG_0000);
    658  1.1   itojun 				if (tmp & REG_0000_BIT_08)
    659  1.1   itojun 					NIC_BSET(sc, REG_0000, 0x08);
    660  1.1   itojun 				NIC_PUT_2(sc, REG_000E, 0x0200);
    661  1.1   itojun 			}
    662  1.1   itojun 			NIC_PUT_2(sc, REG_ISR, isr);
    663  1.1   itojun 		}
    664  1.1   itojun 	}
    665  1.1   itojun 	NIC_PUT_2(sc, REG_ISR, ISR_MASK);
    666  1.1   itojun }
    667  1.1   itojun 
    668  1.1   itojun static void
    669  1.6      chs netdock_txint(struct netdock_softc *sc)
    670  1.1   itojun {
    671  1.1   itojun 	struct ifnet *ifp = &sc->sc_if;
    672  1.1   itojun 	int savereg0002;
    673  1.1   itojun 	int reg0004;
    674  1.1   itojun 	int regdata;
    675  1.1   itojun 
    676  1.1   itojun 	ifp->if_flags &= ~IFF_OACTIVE;
    677  1.1   itojun 	ifp->if_timer = 0;
    678  1.1   itojun 
    679  1.1   itojun 	savereg0002 = NIC_GET_2(sc, REG_0002);
    680  1.1   itojun 
    681  1.1   itojun 	while (((reg0004 = NIC_GET_2(sc, REG_0004)) & REG_0004_BIT_0F) == 0) {
    682  1.1   itojun 		NIC_PUT_2(sc, REG_0002, reg0004);
    683  1.1   itojun 		NIC_PUT_2(sc, REG_0006, 0x0060);
    684  1.1   itojun 		NIC_GET_2(sc, REG_ISR);
    685  1.1   itojun 		regdata = NIC_GET_2(sc, REG_DATA);
    686  1.1   itojun 		if ((regdata & REG_DATA_BIT_08) == 0) {
    687  1.1   itojun 			/* ifp->if_collisions++; */
    688  1.1   itojun 			if (regdata & REG_DATA_BIT_07)
    689  1.1   itojun 			/* ifp->if_oerrors++; */
    690  1.1   itojun 			NIC_PUT_2(sc, REG_000E, 0);
    691  1.1   itojun 			NIC_ORW(sc, REG_0000, 0x0100);
    692  1.1   itojun 			NIC_PUT_2(sc, REG_000E, 0x0200);
    693  1.1   itojun 		}
    694  1.1   itojun 		NIC_GET_2(sc ,REG_DATA);
    695  1.1   itojun 
    696  1.1   itojun 		if (regdata & REG_DATA_BIT_0F)
    697  1.1   itojun 			NIC_GET_4(sc, REG_EFD00);
    698  1.1   itojun 		NIC_PUT_2(sc, REG_0000, 0xa000);
    699  1.1   itojun 		NIC_PUT_2(sc, REG_ISR, ISR_TX);
    700  1.1   itojun 	}
    701  1.1   itojun 
    702  1.1   itojun 	NIC_PUT_2(sc, REG_0002, savereg0002);
    703  1.1   itojun 	NIC_PUT_2(sc, REG_000E, 0);
    704  1.1   itojun 	NIC_GET_2(sc, REG_0006);
    705  1.1   itojun 	NIC_PUT_2(sc, REG_000E, 0x0200);
    706  1.1   itojun 	NIC_PUT_2(sc, REG_0006, 0);
    707  1.1   itojun }
    708  1.1   itojun 
    709  1.1   itojun static void
    710  1.6      chs netdock_rxint(struct netdock_softc *sc)
    711  1.1   itojun {
    712  1.1   itojun 	struct ifnet *ifp = &sc->sc_if;
    713  1.1   itojun 	int regdata1;
    714  1.1   itojun 	int regdata2;
    715  1.1   itojun 	u_int len;
    716  1.1   itojun 	int timeout;
    717  1.1   itojun 
    718  1.1   itojun 	while ((NIC_GET_2(sc, REG_0004) & REG_0004_BIT_07) == 0) {
    719  1.1   itojun 		NIC_GET_2(sc, REG_ISR);
    720  1.1   itojun 		NIC_PUT_2(sc, REG_0006, 0x00e0);
    721  1.1   itojun 		NIC_GET_2(sc, REG_ISR);
    722  1.1   itojun 		regdata1 = NIC_GET_2(sc, REG_DATA);
    723  1.1   itojun 		regdata2 = NIC_GET_2(sc, REG_DATA);
    724  1.1   itojun 		len = ((regdata2 << 8) & 0x0700) | ((regdata2 >> 8) & 0x00ff);
    725  1.1   itojun 
    726  1.1   itojun #if 0
    727  1.1   itojun 		printf("netdock_rxint: r1=0x%04x, r2=0x%04x, len=%d\n",
    728  1.1   itojun 		       regdata1, regdata2, len);
    729  1.1   itojun #endif
    730  1.1   itojun 
    731  1.1   itojun 		if ((regdata1 & REG_DATA_BIT_04) == 0)
    732  1.1   itojun 			len -= 2;
    733  1.1   itojun 
    734  1.1   itojun 		if ((regdata1 & 0x00ac) == 0) {
    735  1.1   itojun 			if (netdock_read(sc, len))
    736  1.1   itojun 				ifp->if_ipackets++;
    737  1.1   itojun 			else
    738  1.1   itojun 				ifp->if_ierrors++;
    739  1.1   itojun 		} else {
    740  1.1   itojun 			ifp->if_ierrors++;
    741  1.1   itojun 
    742  1.1   itojun 			if (regdata1 & REG_DATA_BIT_02)
    743  1.1   itojun 				NIC_GET_4(sc, REG_EFD00);
    744  1.1   itojun 			if (regdata1 & REG_DATA_BIT_03)
    745  1.1   itojun 				;
    746  1.1   itojun 			if (regdata1 & REG_DATA_BIT_05)
    747  1.1   itojun 				NIC_GET_4(sc, REG_EFD00);
    748  1.1   itojun 			if (regdata1 & REG_DATA_BIT_07)
    749  1.1   itojun 				;
    750  1.1   itojun 		}
    751  1.1   itojun 
    752  1.1   itojun 		timeout = 0x14;
    753  1.1   itojun 		while ((NIC_GET_2(sc, REG_0000) & REG_0000_BIT_08) && timeout--)
    754  1.1   itojun 			;
    755  1.1   itojun 		if (timeout == 0)
    756  1.1   itojun 			;
    757  1.1   itojun 
    758  1.1   itojun 		NIC_PUT_2(sc, REG_0000, 0x8000);
    759  1.1   itojun 	}
    760  1.1   itojun 	NIC_PUT_2(sc, REG_0006, 0);
    761  1.1   itojun }
    762  1.1   itojun 
    763  1.1   itojun static int
    764  1.6      chs netdock_read(struct netdock_softc *sc, int len)
    765  1.1   itojun {
    766  1.1   itojun 	struct ifnet *ifp = &sc->sc_if;
    767  1.1   itojun 	struct mbuf *m;
    768  1.1   itojun 
    769  1.1   itojun 	m = netdock_get(sc, len);
    770  1.1   itojun 	if (m == 0)
    771  1.1   itojun 		return (0);
    772  1.1   itojun 
    773  1.1   itojun 	/* the data comes with frame checksum and ethernet header */
    774  1.1   itojun 	m->m_flags |= M_HASFCS;
    775  1.1   itojun 
    776  1.1   itojun #if NBPFILTER > 0
    777  1.1   itojun 	if (ifp->if_bpf)
    778  1.1   itojun 		bpf_mtap(ifp->if_bpf, m);
    779  1.1   itojun #endif
    780  1.1   itojun 
    781  1.1   itojun 	(*ifp->if_input)(ifp, m);
    782  1.1   itojun 
    783  1.1   itojun 	return (1);
    784  1.1   itojun }
    785  1.1   itojun 
    786  1.1   itojun static struct mbuf *
    787  1.6      chs netdock_get(struct netdock_softc *sc, int datalen)
    788  1.1   itojun {
    789  1.1   itojun 	struct mbuf *m, *top, **mp;
    790  1.1   itojun 	u_char *data;
    791  1.1   itojun 	int i;
    792  1.1   itojun 	int len;
    793  1.1   itojun 	int len4;
    794  1.1   itojun 	u_int32_t *data4;
    795  1.1   itojun 
    796  1.1   itojun 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    797  1.1   itojun 	if (m == NULL)
    798  1.1   itojun 		return (NULL);
    799  1.1   itojun 	m->m_pkthdr.rcvif = &sc->sc_if;
    800  1.1   itojun 	m->m_pkthdr.len = datalen;
    801  1.1   itojun 	len = MHLEN;
    802  1.1   itojun 	top = NULL;
    803  1.1   itojun 	mp = &top;
    804  1.1   itojun 
    805  1.1   itojun 	while (datalen > 0) {
    806  1.1   itojun 		if (top) {
    807  1.1   itojun 			MGET(m, M_DONTWAIT, MT_DATA);
    808  1.1   itojun 			if (m == 0) {
    809  1.1   itojun 				m_freem(top);
    810  1.1   itojun 				return (NULL);
    811  1.1   itojun 			}
    812  1.1   itojun 			len = MLEN;
    813  1.1   itojun 		}
    814  1.1   itojun 		if (datalen >= MINCLSIZE) {
    815  1.1   itojun 			MCLGET(m, M_DONTWAIT);
    816  1.1   itojun 			if ((m->m_flags & M_EXT) == 0) {
    817  1.1   itojun 				if (top)
    818  1.1   itojun 					m_freem(top);
    819  1.1   itojun 				return (NULL);
    820  1.1   itojun 			}
    821  1.1   itojun 			len = MCLBYTES;
    822  1.1   itojun 		}
    823  1.1   itojun 
    824  1.1   itojun 		if (mp == &top) {
    825  1.1   itojun 			caddr_t newdata = (caddr_t)
    826  1.1   itojun 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    827  1.1   itojun 			    sizeof(struct ether_header);
    828  1.1   itojun 			len -= newdata - m->m_data;
    829  1.1   itojun 			m->m_data = newdata;
    830  1.1   itojun 		}
    831  1.1   itojun 
    832  1.1   itojun 		m->m_len = len = min(datalen, len);
    833  1.1   itojun 
    834  1.1   itojun 		data = mtod(m, u_char *);
    835  1.1   itojun 		len4 = len >> 2;
    836  1.1   itojun 		data4 = (u_int32_t *)data;
    837  1.1   itojun 		for (i = 0; i < len4; i++)
    838  1.1   itojun 			data4[i] = NIC_GET_4(sc, REG_DATA);
    839  1.1   itojun 		for (i = len4 << 2; i < len; i++)
    840  1.1   itojun 			data[i] = NIC_GET_1(sc, REG_DATA);
    841  1.1   itojun 
    842  1.1   itojun 		datalen -= len;
    843  1.1   itojun 		*mp = m;
    844  1.1   itojun 		mp = &m->m_next;
    845  1.1   itojun 	}
    846  1.1   itojun 
    847  1.1   itojun 	return (top);
    848  1.1   itojun }
    849