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