Home | History | Annotate | Line # | Download | only in nubus
if_netdock_nubus.c revision 1.19
      1  1.19    dyoung /*	$NetBSD: if_netdock_nubus.c,v 1.19 2008/11/07 00:20:02 dyoung 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.19    dyoung __KERNEL_RCSID(0, "$NetBSD: if_netdock_nubus.c,v 1.19 2008/11/07 00:20:02 dyoung 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.18     hauke 	device_t	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.12  christos static int	netdock_ioctl(struct ifnet *, u_long, void *);
    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.11     perry 	__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.11     perry 	__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.11     perry 	__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.11     perry 	__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.8       rjs 	const 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.17     hauke 	    sc->sc_dev->dv_xname, ether_sprintf(lladdr));
    361   1.1    itojun 
    362  1.18     hauke 	memcpy(ifp->if_xname, device_xname(sc->sc_dev), 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.12  christos netdock_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    378   1.1    itojun {
    379   1.1    itojun 	struct ifaddr *ifa;
    380   1.1    itojun 	struct netdock_softc *sc = ifp->if_softc;
    381   1.1    itojun 	int s = splnet();
    382   1.1    itojun 	int err = 0;
    383   1.1    itojun 	int temp;
    384   1.1    itojun 
    385   1.1    itojun 	switch (cmd) {
    386  1.19    dyoung 	case SIOCINITIFADDR:
    387   1.1    itojun 		ifa = (struct ifaddr *)data;
    388   1.1    itojun 		ifp->if_flags |= IFF_UP;
    389  1.19    dyoung 		(void)netdock_init(sc);
    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 			arp_ifinit(ifp, ifa);
    394   1.1    itojun 			break;
    395   1.1    itojun #endif
    396   1.1    itojun 		default:
    397   1.1    itojun 			break;
    398   1.1    itojun 		}
    399   1.1    itojun 		break;
    400   1.1    itojun 
    401   1.1    itojun 	case SIOCSIFFLAGS:
    402  1.19    dyoung 		if ((err = ifioctl_common(ifp, cmd, data)) != 0)
    403  1.19    dyoung 			break;
    404  1.19    dyoung 		/* XXX see the comment in ed_ioctl() about code re-use */
    405   1.1    itojun 		if ((ifp->if_flags & IFF_UP) == 0 &&
    406   1.1    itojun 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    407   1.1    itojun 			netdock_stop(sc);
    408   1.1    itojun 			ifp->if_flags &= ~IFF_RUNNING;
    409   1.1    itojun 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    410   1.1    itojun 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    411   1.1    itojun 			(void)netdock_init(sc);
    412   1.1    itojun 		} else {
    413   1.1    itojun 			temp = ifp->if_flags & IFF_UP;
    414   1.1    itojun 			netdock_reset(sc);
    415   1.1    itojun 			ifp->if_flags |= temp;
    416   1.1    itojun 			netdock_start(ifp);
    417   1.1    itojun 		}
    418   1.1    itojun 		break;
    419   1.1    itojun 
    420   1.1    itojun 	case SIOCADDMULTI:
    421   1.1    itojun 	case SIOCDELMULTI:
    422  1.15      jmmv 		if ((err = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
    423   1.5   thorpej 			if (ifp->if_flags & IFF_RUNNING) {
    424   1.5   thorpej 				temp = ifp->if_flags & IFF_UP;
    425   1.5   thorpej 				netdock_reset(sc);
    426   1.5   thorpej 				ifp->if_flags |= temp;
    427   1.5   thorpej 			}
    428   1.1    itojun 			err = 0;
    429   1.1    itojun 		}
    430   1.1    itojun 		break;
    431   1.1    itojun 	default:
    432  1.19    dyoung 		err = ether_ioctl(ifp, cmd, data);
    433   1.1    itojun 		break;
    434   1.1    itojun 	}
    435   1.1    itojun 	splx(s);
    436   1.1    itojun 	return (err);
    437   1.1    itojun }
    438   1.1    itojun 
    439   1.1    itojun static void
    440   1.6       chs netdock_start(struct ifnet *ifp)
    441   1.1    itojun {
    442   1.1    itojun 	struct netdock_softc *sc = ifp->if_softc;
    443   1.1    itojun 	struct mbuf *m;
    444   1.1    itojun 
    445   1.1    itojun 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    446   1.1    itojun 		return;
    447   1.1    itojun 
    448   1.1    itojun 	while (1) {
    449   1.1    itojun 		IF_DEQUEUE(&ifp->if_snd, m);
    450   1.1    itojun 		if (m == 0)
    451   1.1    itojun 			return;
    452   1.1    itojun 
    453   1.1    itojun 		if ((m->m_flags & M_PKTHDR) == 0)
    454   1.1    itojun 			panic("%s: netdock_start: no header mbuf",
    455  1.18     hauke 			    device_xname(sc->sc_dev));
    456   1.1    itojun 
    457   1.1    itojun #if NBPFILTER > 0
    458   1.1    itojun 		if (ifp->if_bpf)
    459   1.1    itojun 			bpf_mtap(ifp->if_bpf, m);
    460   1.1    itojun #endif
    461   1.1    itojun 
    462   1.1    itojun 		if ((netdock_put(sc, m)) == 0) {
    463   1.1    itojun 			IF_PREPEND(&ifp->if_snd, m);
    464   1.1    itojun 			return;
    465   1.1    itojun 		}
    466   1.1    itojun 
    467   1.1    itojun 		ifp->if_opackets++;
    468   1.1    itojun 	}
    469   1.1    itojun 
    470   1.1    itojun }
    471   1.1    itojun 
    472   1.1    itojun static void
    473   1.6       chs netdock_reset(struct netdock_softc *sc)
    474   1.1    itojun {
    475   1.1    itojun 
    476   1.1    itojun 	netdock_stop(sc);
    477   1.1    itojun 	netdock_init(sc);
    478   1.1    itojun }
    479   1.1    itojun 
    480   1.1    itojun static int
    481   1.6       chs netdock_init(struct netdock_softc *sc)
    482   1.1    itojun {
    483   1.1    itojun 	int s;
    484   1.1    itojun 	int saveisr;
    485   1.1    itojun 	int savetmp;
    486   1.1    itojun 
    487   1.1    itojun 	if (sc->sc_if.if_flags & IFF_RUNNING)
    488   1.1    itojun 		return (0);
    489   1.1    itojun 
    490   1.1    itojun 	s = splnet();
    491   1.1    itojun 
    492   1.1    itojun 	/* 0606 */
    493   1.1    itojun 	NIC_PUT_2(sc, REG_000E, 0x0200);
    494   1.1    itojun 	NIC_PUT_2(sc, REG_ISR , 0);
    495   1.1    itojun 	NIC_PUT_2(sc, REG_000E, 0);
    496   1.1    itojun 
    497   1.1    itojun 	NIC_PUT_2(sc, REG_0000, 0x8104);
    498   1.1    itojun 	NIC_PUT_2(sc, REG_0004, 0x0043);
    499   1.1    itojun 	NIC_PUT_2(sc, REG_000E, 0x0100);
    500   1.1    itojun 	NIC_PUT_2(sc, REG_0002, 0x6618);
    501   1.1    itojun 	NIC_PUT_2(sc, REG_0000, 0x8010);
    502   1.1    itojun 
    503   1.1    itojun 	NIC_PUT_1(sc, REG_0004 + 0, sc->sc_enaddr[0]);
    504   1.1    itojun 	NIC_PUT_1(sc, REG_0004 + 1, sc->sc_enaddr[1]);
    505   1.1    itojun 	NIC_PUT_1(sc, REG_0004 + 2, sc->sc_enaddr[2]);
    506   1.1    itojun 	NIC_PUT_1(sc, REG_0004 + 3, sc->sc_enaddr[3]);
    507   1.1    itojun 	NIC_PUT_1(sc, REG_0004 + 4, sc->sc_enaddr[4]);
    508   1.1    itojun 	NIC_PUT_1(sc, REG_0004 + 5, sc->sc_enaddr[5]);
    509   1.1    itojun 
    510   1.1    itojun 	NIC_PUT_2(sc, REG_ISR , 0x2008);
    511   1.1    itojun 	NIC_PUT_2(sc, REG_000E, 0x0200);
    512   1.1    itojun 	NIC_PUT_2(sc, REG_0000, 0x4000);
    513   1.1    itojun 	NIC_PUT_2(sc, REG_ISR, ISR_MASK);
    514   1.1    itojun 
    515   1.1    itojun 
    516   1.1    itojun 	/* 1320 */
    517   1.1    itojun 	saveisr = NIC_GET_2(sc, REG_ISR);
    518   1.1    itojun 	NIC_PUT_2(sc, REG_ISR , 0);
    519   1.1    itojun 	savetmp = NIC_GET_2(sc, REG_000E);
    520   1.1    itojun 	NIC_PUT_2(sc, REG_000E, 0x0100);
    521   1.1    itojun 	NIC_ANDW(sc, REG_ISR, ~ISR_BIT_03);
    522   1.1    itojun 	NIC_PUT_2(sc, REG_000E, savetmp);
    523   1.1    itojun 
    524   1.1    itojun 	/* 1382 */
    525   1.1    itojun 	savetmp = NIC_GET_2(sc, REG_000E);
    526   1.1    itojun 	NIC_PUT_2(sc, REG_000E, 0x0100);
    527   1.1    itojun 	NIC_ORW(sc, REG_ISR, ISR_BIT_03);
    528   1.1    itojun 	NIC_PUT_2(sc, REG_000E, savetmp);
    529   1.1    itojun 	NIC_PUT_2(sc, REG_ISR , saveisr);
    530   1.1    itojun 
    531   1.1    itojun 
    532   1.1    itojun 	sc->sc_if.if_flags |= IFF_RUNNING;
    533   1.1    itojun 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    534   1.1    itojun 
    535   1.1    itojun 	splx(s);
    536   1.1    itojun 	return (0);
    537   1.1    itojun }
    538   1.1    itojun 
    539   1.1    itojun static int
    540   1.6       chs netdock_stop(struct netdock_softc *sc)
    541   1.1    itojun {
    542   1.1    itojun 	int s = splnet();
    543   1.1    itojun 
    544   1.1    itojun 	sc->sc_if.if_timer = 0;
    545   1.1    itojun 	sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
    546   1.1    itojun 
    547   1.1    itojun 	splx(s);
    548   1.1    itojun 	return (0);
    549   1.1    itojun }
    550   1.1    itojun 
    551   1.1    itojun static void
    552   1.6       chs netdock_watchdog(struct ifnet *ifp)
    553   1.1    itojun {
    554   1.1    itojun 	struct netdock_softc *sc = ifp->if_softc;
    555   1.1    itojun 	int tmp;
    556   1.1    itojun 
    557   1.1    itojun 	printf("netdock_watchdog: resetting chip\n");
    558   1.1    itojun 	tmp = ifp->if_flags & IFF_UP;
    559   1.1    itojun 	netdock_reset(sc);
    560   1.1    itojun 	ifp->if_flags |= tmp;
    561   1.1    itojun }
    562   1.1    itojun 
    563   1.1    itojun static u_int
    564   1.6       chs netdock_put(struct netdock_softc *sc, struct mbuf *m0)
    565   1.1    itojun {
    566   1.1    itojun 	struct mbuf *m;
    567   1.1    itojun 	u_int totlen = 0;
    568   1.1    itojun 	u_int tmplen;
    569   1.1    itojun 	int isr;
    570   1.1    itojun 	int timeout;
    571   1.1    itojun 	int tmp;
    572   1.1    itojun 	u_int i;
    573   1.1    itojun 
    574   1.1    itojun 	for (m = m0; m; m = m->m_next)
    575   1.1    itojun 		totlen += m->m_len;
    576   1.1    itojun 
    577   1.1    itojun 	if (totlen >= ETHER_MAX_LEN)
    578  1.17     hauke 		panic("%s: netdock_put: packet overflow",
    579  1.18     hauke 		    device_xname(sc->sc_dev));
    580   1.1    itojun 
    581   1.1    itojun 	totlen += 6;
    582   1.1    itojun 	tmplen = totlen;
    583   1.1    itojun 	tmplen &= 0xff00;
    584   1.1    itojun 	tmplen |= 0x2000;
    585   1.1    itojun 	NIC_PUT_2(sc, REG_0000, tmplen);
    586   1.1    itojun 
    587   1.1    itojun 	timeout = 0x3000;
    588   1.1    itojun 	while ((((isr = NIC_GET_2(sc, REG_ISR)) & ISR_READY) == 0) &&
    589   1.1    itojun 	    timeout--) {
    590   1.1    itojun 		if (isr & ISR_TX)
    591   1.1    itojun 			netdock_txint(sc);
    592   1.1    itojun 	}
    593   1.1    itojun 	if (timeout == 0)
    594   1.1    itojun 		return (0);
    595   1.1    itojun 
    596   1.1    itojun 	tmp = NIC_GET_2(sc, REG_0002);
    597   1.1    itojun 	tmp <<= 8;
    598   1.1    itojun 	NIC_PUT_2(sc, REG_0002, tmp);
    599   1.1    itojun 	NIC_PUT_2(sc, REG_0006, 0x240);
    600   1.1    itojun 	NIC_GET_2(sc, REG_ISR);
    601   1.1    itojun 	tmplen = ((totlen << 8) & 0xfe00) | ((totlen >> 8) & 0x00ff);
    602   1.1    itojun 	NIC_PUT_2(sc, REG_DATA, tmplen);
    603   1.1    itojun 
    604   1.1    itojun 	for (m = m0; m; m = m->m_next) {
    605   1.1    itojun 		u_char *data = mtod(m, u_char *);
    606   1.1    itojun 		int len = m->m_len;
    607   1.1    itojun 		int len4 = len >> 2;
    608   1.1    itojun 		u_int32_t *data4 = (u_int32_t *)data;
    609   1.1    itojun 		for (i = 0; i < len4; i++)
    610   1.1    itojun 			NIC_PUT_4(sc, REG_DATA, data4[i]);
    611   1.1    itojun 		for (i = len4 << 2; i < len; i++)
    612   1.1    itojun 			NIC_PUT_1(sc, REG_DATA, data[i]);
    613   1.1    itojun 	}
    614   1.1    itojun 
    615   1.1    itojun 	if (totlen & 0x01)
    616   1.1    itojun 		NIC_PUT_2(sc, REG_DATA, 0x2020);
    617   1.1    itojun 	else
    618   1.1    itojun 		NIC_PUT_2(sc, REG_DATA, 0);
    619   1.1    itojun 
    620   1.1    itojun 	NIC_PUT_2(sc, REG_0000, 0xc000);
    621   1.1    itojun 
    622   1.1    itojun 	m_freem(m0);
    623   1.1    itojun 	/* sc->sc_if.if_timer = 5; */
    624   1.1    itojun 	return (totlen);
    625   1.1    itojun }
    626   1.1    itojun 
    627   1.1    itojun void
    628   1.6       chs netdock_intr(void *arg)
    629   1.1    itojun {
    630   1.1    itojun 	struct netdock_softc *sc = (struct netdock_softc *)arg;
    631   1.1    itojun 	int isr;
    632   1.1    itojun 	int tmp;
    633   1.1    itojun 
    634   1.1    itojun 	NIC_PUT_2(sc, REG_ISR, 0);
    635   1.1    itojun 	while ((isr = (NIC_GET_2(sc, REG_ISR) & ISR_ALL)) != 0) {
    636   1.1    itojun 		if (isr & ISR_TX)
    637   1.1    itojun 			netdock_txint(sc);
    638   1.1    itojun 
    639   1.1    itojun 		if (isr & ISR_RX)
    640   1.1    itojun 			netdock_rxint(sc);
    641   1.1    itojun 
    642   1.1    itojun 		if (isr & (ISR_BIT_0C | ISR_BIT_0D)) {
    643   1.1    itojun 			if (isr & ISR_BIT_0C) {
    644   1.1    itojun 				NIC_PUT_2(sc, REG_000E, 0);
    645   1.1    itojun 				NIC_BSET(sc, REG_0004, 0x08);
    646   1.1    itojun 				NIC_PUT_2(sc, REG_000E, 0x0200);
    647   1.1    itojun 			}
    648   1.1    itojun 			if (isr & ISR_BIT_0D) {
    649   1.1    itojun 				NIC_PUT_2(sc, REG_000E, 0);
    650   1.1    itojun 				tmp = NIC_GET_2(sc, REG_0002);
    651   1.1    itojun 				if (tmp & REG_0002_BIT_04)
    652   1.1    itojun 					NIC_GET_2(sc, REG_0006);
    653   1.1    itojun 				tmp = NIC_GET_2(sc, REG_0000);
    654   1.1    itojun 				if (tmp & REG_0000_BIT_08)
    655   1.1    itojun 					NIC_BSET(sc, REG_0000, 0x08);
    656   1.1    itojun 				NIC_PUT_2(sc, REG_000E, 0x0200);
    657   1.1    itojun 			}
    658   1.1    itojun 			NIC_PUT_2(sc, REG_ISR, isr);
    659   1.1    itojun 		}
    660   1.1    itojun 	}
    661   1.1    itojun 	NIC_PUT_2(sc, REG_ISR, ISR_MASK);
    662   1.1    itojun }
    663   1.1    itojun 
    664   1.1    itojun static void
    665   1.6       chs netdock_txint(struct netdock_softc *sc)
    666   1.1    itojun {
    667   1.1    itojun 	struct ifnet *ifp = &sc->sc_if;
    668   1.1    itojun 	int savereg0002;
    669   1.1    itojun 	int reg0004;
    670   1.1    itojun 	int regdata;
    671   1.1    itojun 
    672   1.1    itojun 	ifp->if_flags &= ~IFF_OACTIVE;
    673   1.1    itojun 	ifp->if_timer = 0;
    674   1.1    itojun 
    675   1.1    itojun 	savereg0002 = NIC_GET_2(sc, REG_0002);
    676   1.1    itojun 
    677   1.1    itojun 	while (((reg0004 = NIC_GET_2(sc, REG_0004)) & REG_0004_BIT_0F) == 0) {
    678   1.1    itojun 		NIC_PUT_2(sc, REG_0002, reg0004);
    679   1.1    itojun 		NIC_PUT_2(sc, REG_0006, 0x0060);
    680   1.1    itojun 		NIC_GET_2(sc, REG_ISR);
    681   1.1    itojun 		regdata = NIC_GET_2(sc, REG_DATA);
    682   1.1    itojun 		if ((regdata & REG_DATA_BIT_08) == 0) {
    683   1.1    itojun 			/* ifp->if_collisions++; */
    684   1.1    itojun 			if (regdata & REG_DATA_BIT_07)
    685   1.1    itojun 			/* ifp->if_oerrors++; */
    686   1.1    itojun 			NIC_PUT_2(sc, REG_000E, 0);
    687   1.1    itojun 			NIC_ORW(sc, REG_0000, 0x0100);
    688   1.1    itojun 			NIC_PUT_2(sc, REG_000E, 0x0200);
    689   1.1    itojun 		}
    690   1.1    itojun 		NIC_GET_2(sc ,REG_DATA);
    691   1.1    itojun 
    692   1.1    itojun 		if (regdata & REG_DATA_BIT_0F)
    693   1.1    itojun 			NIC_GET_4(sc, REG_EFD00);
    694   1.1    itojun 		NIC_PUT_2(sc, REG_0000, 0xa000);
    695   1.1    itojun 		NIC_PUT_2(sc, REG_ISR, ISR_TX);
    696   1.1    itojun 	}
    697   1.1    itojun 
    698   1.1    itojun 	NIC_PUT_2(sc, REG_0002, savereg0002);
    699   1.1    itojun 	NIC_PUT_2(sc, REG_000E, 0);
    700   1.1    itojun 	NIC_GET_2(sc, REG_0006);
    701   1.1    itojun 	NIC_PUT_2(sc, REG_000E, 0x0200);
    702   1.1    itojun 	NIC_PUT_2(sc, REG_0006, 0);
    703   1.1    itojun }
    704   1.1    itojun 
    705   1.1    itojun static void
    706   1.6       chs netdock_rxint(struct netdock_softc *sc)
    707   1.1    itojun {
    708   1.1    itojun 	struct ifnet *ifp = &sc->sc_if;
    709   1.1    itojun 	int regdata1;
    710   1.1    itojun 	int regdata2;
    711   1.1    itojun 	u_int len;
    712   1.1    itojun 	int timeout;
    713   1.1    itojun 
    714   1.1    itojun 	while ((NIC_GET_2(sc, REG_0004) & REG_0004_BIT_07) == 0) {
    715   1.1    itojun 		NIC_GET_2(sc, REG_ISR);
    716   1.1    itojun 		NIC_PUT_2(sc, REG_0006, 0x00e0);
    717   1.1    itojun 		NIC_GET_2(sc, REG_ISR);
    718   1.1    itojun 		regdata1 = NIC_GET_2(sc, REG_DATA);
    719   1.1    itojun 		regdata2 = NIC_GET_2(sc, REG_DATA);
    720   1.1    itojun 		len = ((regdata2 << 8) & 0x0700) | ((regdata2 >> 8) & 0x00ff);
    721   1.1    itojun 
    722   1.1    itojun #if 0
    723   1.1    itojun 		printf("netdock_rxint: r1=0x%04x, r2=0x%04x, len=%d\n",
    724   1.1    itojun 		       regdata1, regdata2, len);
    725   1.1    itojun #endif
    726   1.1    itojun 
    727   1.1    itojun 		if ((regdata1 & REG_DATA_BIT_04) == 0)
    728   1.1    itojun 			len -= 2;
    729   1.1    itojun 
    730   1.7   thorpej 		/* CRC is included with the packet; trim it off. */
    731   1.7   thorpej 		len -= ETHER_CRC_LEN;
    732   1.7   thorpej 
    733   1.1    itojun 		if ((regdata1 & 0x00ac) == 0) {
    734   1.1    itojun 			if (netdock_read(sc, len))
    735   1.1    itojun 				ifp->if_ipackets++;
    736   1.1    itojun 			else
    737   1.1    itojun 				ifp->if_ierrors++;
    738   1.1    itojun 		} else {
    739   1.1    itojun 			ifp->if_ierrors++;
    740   1.1    itojun 
    741   1.1    itojun 			if (regdata1 & REG_DATA_BIT_02)
    742   1.1    itojun 				NIC_GET_4(sc, REG_EFD00);
    743   1.1    itojun 			if (regdata1 & REG_DATA_BIT_03)
    744   1.1    itojun 				;
    745   1.1    itojun 			if (regdata1 & REG_DATA_BIT_05)
    746   1.1    itojun 				NIC_GET_4(sc, REG_EFD00);
    747   1.1    itojun 			if (regdata1 & REG_DATA_BIT_07)
    748   1.1    itojun 				;
    749   1.1    itojun 		}
    750   1.1    itojun 
    751   1.1    itojun 		timeout = 0x14;
    752   1.1    itojun 		while ((NIC_GET_2(sc, REG_0000) & REG_0000_BIT_08) && timeout--)
    753   1.1    itojun 			;
    754   1.1    itojun 		if (timeout == 0)
    755   1.1    itojun 			;
    756   1.1    itojun 
    757   1.1    itojun 		NIC_PUT_2(sc, REG_0000, 0x8000);
    758   1.1    itojun 	}
    759   1.1    itojun 	NIC_PUT_2(sc, REG_0006, 0);
    760   1.1    itojun }
    761   1.1    itojun 
    762   1.1    itojun static int
    763   1.6       chs netdock_read(struct netdock_softc *sc, int len)
    764   1.1    itojun {
    765   1.1    itojun 	struct ifnet *ifp = &sc->sc_if;
    766   1.1    itojun 	struct mbuf *m;
    767   1.1    itojun 
    768   1.1    itojun 	m = netdock_get(sc, len);
    769   1.1    itojun 	if (m == 0)
    770   1.1    itojun 		return (0);
    771   1.1    itojun 
    772   1.1    itojun #if NBPFILTER > 0
    773   1.1    itojun 	if (ifp->if_bpf)
    774   1.1    itojun 		bpf_mtap(ifp->if_bpf, m);
    775   1.1    itojun #endif
    776   1.1    itojun 
    777   1.1    itojun 	(*ifp->if_input)(ifp, m);
    778   1.1    itojun 
    779   1.1    itojun 	return (1);
    780   1.1    itojun }
    781   1.1    itojun 
    782   1.1    itojun static struct mbuf *
    783   1.6       chs netdock_get(struct netdock_softc *sc, int datalen)
    784   1.1    itojun {
    785   1.1    itojun 	struct mbuf *m, *top, **mp;
    786   1.1    itojun 	u_char *data;
    787   1.1    itojun 	int i;
    788   1.1    itojun 	int len;
    789   1.1    itojun 	int len4;
    790   1.1    itojun 	u_int32_t *data4;
    791   1.1    itojun 
    792   1.1    itojun 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    793   1.1    itojun 	if (m == NULL)
    794   1.1    itojun 		return (NULL);
    795   1.1    itojun 	m->m_pkthdr.rcvif = &sc->sc_if;
    796   1.1    itojun 	m->m_pkthdr.len = datalen;
    797   1.1    itojun 	len = MHLEN;
    798   1.1    itojun 	top = NULL;
    799   1.1    itojun 	mp = &top;
    800   1.1    itojun 
    801   1.1    itojun 	while (datalen > 0) {
    802   1.1    itojun 		if (top) {
    803   1.1    itojun 			MGET(m, M_DONTWAIT, MT_DATA);
    804   1.1    itojun 			if (m == 0) {
    805   1.1    itojun 				m_freem(top);
    806   1.1    itojun 				return (NULL);
    807   1.1    itojun 			}
    808   1.1    itojun 			len = MLEN;
    809   1.1    itojun 		}
    810   1.1    itojun 		if (datalen >= MINCLSIZE) {
    811   1.1    itojun 			MCLGET(m, M_DONTWAIT);
    812   1.1    itojun 			if ((m->m_flags & M_EXT) == 0) {
    813   1.1    itojun 				if (top)
    814   1.1    itojun 					m_freem(top);
    815   1.1    itojun 				return (NULL);
    816   1.1    itojun 			}
    817   1.1    itojun 			len = MCLBYTES;
    818   1.1    itojun 		}
    819   1.1    itojun 
    820   1.1    itojun 		if (mp == &top) {
    821  1.13        he 			char *newdata = (char *)
    822   1.1    itojun 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    823   1.1    itojun 			    sizeof(struct ether_header);
    824   1.1    itojun 			len -= newdata - m->m_data;
    825   1.1    itojun 			m->m_data = newdata;
    826   1.1    itojun 		}
    827   1.1    itojun 
    828   1.1    itojun 		m->m_len = len = min(datalen, len);
    829   1.1    itojun 
    830   1.1    itojun 		data = mtod(m, u_char *);
    831   1.1    itojun 		len4 = len >> 2;
    832   1.1    itojun 		data4 = (u_int32_t *)data;
    833   1.1    itojun 		for (i = 0; i < len4; i++)
    834   1.1    itojun 			data4[i] = NIC_GET_4(sc, REG_DATA);
    835   1.1    itojun 		for (i = len4 << 2; i < len; i++)
    836   1.1    itojun 			data[i] = NIC_GET_1(sc, REG_DATA);
    837   1.1    itojun 
    838   1.1    itojun 		datalen -= len;
    839   1.1    itojun 		*mp = m;
    840   1.1    itojun 		mp = &m->m_next;
    841   1.1    itojun 	}
    842   1.1    itojun 
    843   1.1    itojun 	return (top);
    844   1.1    itojun }
    845