1 /*	$NetBSD: if_netdock_nubus.c,v 1.35 2022/09/18 02:45:38 thorpej Exp $	*/
2 
3 /*
4  * Copyright (C) 2000,2002 Daishi Kato <daishi@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@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.35 2022/09/18 02:45:38 thorpej 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
netdock_nubus_match(device_t parent,cfdata_t cf,void * aux)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
netdock_nubus_attach(device_t parent,device_t self,void * aux)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
netdock_nb_get_enaddr(bus_space_tag_t bst,bus_space_handle_t bsh,struct nubus_attach_args * na,u_int8_t * ep)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
netdock_print_driver(bus_space_tag_t bst,bus_space_handle_t bsh,struct nubus_attach_args * na)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
netdock_setup(struct netdock_softc * sc,u_int8_t * lladdr)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
netdock_ioctl(struct ifnet * ifp,u_long cmd,void * data)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
netdock_start(struct ifnet * ifp)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) == 0)
443 		return;
444 
445 	while (1) {
446 		IF_DEQUEUE(&ifp->if_snd, m);
447 		if (m == 0)
448 			return;
449 
450 		KASSERT(m->m_flags & M_PKTHDR);
451 
452 		bpf_mtap(ifp, m, BPF_D_OUT);
453 
454 		if ((netdock_put(sc, m)) == 0) {
455 			IF_PREPEND(&ifp->if_snd, m);
456 			return;
457 		}
458 
459 		if_statinc(ifp, if_opackets);
460 	}
461 }
462 
463 static void
netdock_reset(struct netdock_softc * sc)464 netdock_reset(struct netdock_softc *sc)
465 {
466 
467 	netdock_stop(sc);
468 	netdock_init(sc);
469 }
470 
471 static int
netdock_init(struct netdock_softc * sc)472 netdock_init(struct netdock_softc *sc)
473 {
474 	int s;
475 	int saveisr;
476 	int savetmp;
477 
478 	if (sc->sc_if.if_flags & IFF_RUNNING)
479 		return (0);
480 
481 	s = splnet();
482 
483 	/* 0606 */
484 	NIC_PUT_2(sc, REG_000E, 0x0200);
485 	NIC_PUT_2(sc, REG_ISR , 0);
486 	NIC_PUT_2(sc, REG_000E, 0);
487 
488 	NIC_PUT_2(sc, REG_0000, 0x8104);
489 	NIC_PUT_2(sc, REG_0004, 0x0043);
490 	NIC_PUT_2(sc, REG_000E, 0x0100);
491 	NIC_PUT_2(sc, REG_0002, 0x6618);
492 	NIC_PUT_2(sc, REG_0000, 0x8010);
493 
494 	NIC_PUT_1(sc, REG_0004 + 0, sc->sc_enaddr[0]);
495 	NIC_PUT_1(sc, REG_0004 + 1, sc->sc_enaddr[1]);
496 	NIC_PUT_1(sc, REG_0004 + 2, sc->sc_enaddr[2]);
497 	NIC_PUT_1(sc, REG_0004 + 3, sc->sc_enaddr[3]);
498 	NIC_PUT_1(sc, REG_0004 + 4, sc->sc_enaddr[4]);
499 	NIC_PUT_1(sc, REG_0004 + 5, sc->sc_enaddr[5]);
500 
501 	NIC_PUT_2(sc, REG_ISR , 0x2008);
502 	NIC_PUT_2(sc, REG_000E, 0x0200);
503 	NIC_PUT_2(sc, REG_0000, 0x4000);
504 	NIC_PUT_2(sc, REG_ISR, ISR_MASK);
505 
506 
507 	/* 1320 */
508 	saveisr = NIC_GET_2(sc, REG_ISR);
509 	NIC_PUT_2(sc, REG_ISR , 0);
510 	savetmp = NIC_GET_2(sc, REG_000E);
511 	NIC_PUT_2(sc, REG_000E, 0x0100);
512 	NIC_ANDW(sc, REG_ISR, ~ISR_BIT_03);
513 	NIC_PUT_2(sc, REG_000E, savetmp);
514 
515 	/* 1382 */
516 	savetmp = NIC_GET_2(sc, REG_000E);
517 	NIC_PUT_2(sc, REG_000E, 0x0100);
518 	NIC_ORW(sc, REG_ISR, ISR_BIT_03);
519 	NIC_PUT_2(sc, REG_000E, savetmp);
520 	NIC_PUT_2(sc, REG_ISR , saveisr);
521 
522 
523 	sc->sc_if.if_flags |= IFF_RUNNING;
524 
525 	splx(s);
526 	return (0);
527 }
528 
529 static int
netdock_stop(struct netdock_softc * sc)530 netdock_stop(struct netdock_softc *sc)
531 {
532 	int s = splnet();
533 
534 	sc->sc_if.if_timer = 0;
535 	sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
536 
537 	splx(s);
538 	return (0);
539 }
540 
541 static void
netdock_watchdog(struct ifnet * ifp)542 netdock_watchdog(struct ifnet *ifp)
543 {
544 	struct netdock_softc *sc = ifp->if_softc;
545 	u_short tmp;
546 
547 	printf("netdock_watchdog: resetting chip\n");
548 	tmp = ifp->if_flags & IFF_UP;
549 	netdock_reset(sc);
550 	ifp->if_flags |= tmp;
551 }
552 
553 static u_int
netdock_put(struct netdock_softc * sc,struct mbuf * m0)554 netdock_put(struct netdock_softc *sc, struct mbuf *m0)
555 {
556 	struct mbuf *m;
557 	u_int totlen = 0;
558 	u_int tmplen;
559 	int isr;
560 	int timeout;
561 	int tmp;
562 	u_int i;
563 
564 	for (m = m0; m; m = m->m_next)
565 		totlen += m->m_len;
566 
567 	if (totlen >= ETHER_MAX_LEN)
568 		panic("%s: netdock_put: packet overflow",
569 		    device_xname(sc->sc_dev));
570 
571 	totlen += 6;
572 	tmplen = totlen;
573 	tmplen &= 0xff00;
574 	tmplen |= 0x2000;
575 	NIC_PUT_2(sc, REG_0000, tmplen);
576 
577 	timeout = 0x3000;
578 	while ((((isr = NIC_GET_2(sc, REG_ISR)) & ISR_READY) == 0) &&
579 	    timeout--) {
580 		if (isr & ISR_TX)
581 			netdock_txint(sc);
582 	}
583 	if (timeout == 0)
584 		return (0);
585 
586 	tmp = NIC_GET_2(sc, REG_0002);
587 	tmp <<= 8;
588 	NIC_PUT_2(sc, REG_0002, tmp);
589 	NIC_PUT_2(sc, REG_0006, 0x240);
590 	NIC_GET_2(sc, REG_ISR);
591 	tmplen = ((totlen << 8) & 0xfe00) | ((totlen >> 8) & 0x00ff);
592 	NIC_PUT_2(sc, REG_DATA, tmplen);
593 
594 	for (m = m0; m; m = m->m_next) {
595 		u_char *data = mtod(m, u_char *);
596 		int len = m->m_len;
597 		int len4 = len >> 2;
598 		u_int32_t *data4 = (u_int32_t *)data;
599 		for (i = 0; i < len4; i++)
600 			NIC_PUT_4(sc, REG_DATA, data4[i]);
601 		for (i = len4 << 2; i < len; i++)
602 			NIC_PUT_1(sc, REG_DATA, data[i]);
603 	}
604 
605 	if (totlen & 0x01)
606 		NIC_PUT_2(sc, REG_DATA, 0x2020);
607 	else
608 		NIC_PUT_2(sc, REG_DATA, 0);
609 
610 	NIC_PUT_2(sc, REG_0000, 0xc000);
611 
612 	m_freem(m0);
613 	/* sc->sc_if.if_timer = 5; */
614 	return (totlen);
615 }
616 
617 void
netdock_intr(void * arg)618 netdock_intr(void *arg)
619 {
620 	struct netdock_softc *sc = (struct netdock_softc *)arg;
621 	int isr;
622 	int tmp;
623 
624 	NIC_PUT_2(sc, REG_ISR, 0);
625 	while ((isr = (NIC_GET_2(sc, REG_ISR) & ISR_ALL)) != 0) {
626 		if (isr & ISR_TX)
627 			netdock_txint(sc);
628 
629 		if (isr & ISR_RX)
630 			netdock_rxint(sc);
631 
632 		if (isr & (ISR_BIT_0C | ISR_BIT_0D)) {
633 			if (isr & ISR_BIT_0C) {
634 				NIC_PUT_2(sc, REG_000E, 0);
635 				NIC_BSET(sc, REG_0004, 0x08);
636 				NIC_PUT_2(sc, REG_000E, 0x0200);
637 			}
638 			if (isr & ISR_BIT_0D) {
639 				NIC_PUT_2(sc, REG_000E, 0);
640 				tmp = NIC_GET_2(sc, REG_0002);
641 				if (tmp & REG_0002_BIT_04)
642 					NIC_GET_2(sc, REG_0006);
643 				tmp = NIC_GET_2(sc, REG_0000);
644 				if (tmp & REG_0000_BIT_08)
645 					NIC_BSET(sc, REG_0000, 0x08);
646 				NIC_PUT_2(sc, REG_000E, 0x0200);
647 			}
648 			NIC_PUT_2(sc, REG_ISR, isr);
649 		}
650 	}
651 	NIC_PUT_2(sc, REG_ISR, ISR_MASK);
652 }
653 
654 static void
netdock_txint(struct netdock_softc * sc)655 netdock_txint(struct netdock_softc *sc)
656 {
657 	struct ifnet *ifp = &sc->sc_if;
658 	int savereg0002;
659 	int reg0004;
660 	int regdata;
661 
662 	ifp->if_timer = 0;
663 
664 	savereg0002 = NIC_GET_2(sc, REG_0002);
665 
666 	while (((reg0004 = NIC_GET_2(sc, REG_0004)) & REG_0004_BIT_0F) == 0) {
667 		NIC_PUT_2(sc, REG_0002, reg0004);
668 		NIC_PUT_2(sc, REG_0006, 0x0060);
669 		NIC_GET_2(sc, REG_ISR);
670 		regdata = NIC_GET_2(sc, REG_DATA);
671 		if ((regdata & REG_DATA_BIT_08) == 0) {
672 			/* if_statinc(ifp, if_collisions); */
673 			if (regdata & REG_DATA_BIT_07)
674 			/* if_statinc(ifp, if_oerrors); */
675 			NIC_PUT_2(sc, REG_000E, 0);
676 			NIC_ORW(sc, REG_0000, 0x0100);
677 			NIC_PUT_2(sc, REG_000E, 0x0200);
678 		}
679 		NIC_GET_2(sc ,REG_DATA);
680 
681 		if (regdata & REG_DATA_BIT_0F)
682 			NIC_GET_4(sc, REG_EFD00);
683 		NIC_PUT_2(sc, REG_0000, 0xa000);
684 		NIC_PUT_2(sc, REG_ISR, ISR_TX);
685 	}
686 
687 	NIC_PUT_2(sc, REG_0002, savereg0002);
688 	NIC_PUT_2(sc, REG_000E, 0);
689 	NIC_GET_2(sc, REG_0006);
690 	NIC_PUT_2(sc, REG_000E, 0x0200);
691 	NIC_PUT_2(sc, REG_0006, 0);
692 }
693 
694 static void
netdock_rxint(struct netdock_softc * sc)695 netdock_rxint(struct netdock_softc *sc)
696 {
697 	struct ifnet *ifp = &sc->sc_if;
698 	int regdata1;
699 	int regdata2;
700 	u_int len;
701 	int timeout;
702 
703 	while ((NIC_GET_2(sc, REG_0004) & REG_0004_BIT_07) == 0) {
704 		NIC_GET_2(sc, REG_ISR);
705 		NIC_PUT_2(sc, REG_0006, 0x00e0);
706 		NIC_GET_2(sc, REG_ISR);
707 		regdata1 = NIC_GET_2(sc, REG_DATA);
708 		regdata2 = NIC_GET_2(sc, REG_DATA);
709 		len = ((regdata2 << 8) & 0x0700) | ((regdata2 >> 8) & 0x00ff);
710 
711 #if 0
712 		printf("netdock_rxint: r1=0x%04x, r2=0x%04x, len=%d\n",
713 		       regdata1, regdata2, len);
714 #endif
715 
716 		if ((regdata1 & REG_DATA_BIT_04) == 0)
717 			len -= 2;
718 
719 		/* CRC is included with the packet; trim it off. */
720 		len -= ETHER_CRC_LEN;
721 
722 		if ((regdata1 & 0x00ac) == 0) {
723 			if (netdock_read(sc, len) == 0)
724 				if_statinc(ifp, if_ierrors);
725 		} else {
726 			if_statinc(ifp, if_ierrors);
727 
728 			if (regdata1 & REG_DATA_BIT_02)
729 				NIC_GET_4(sc, REG_EFD00);
730 			if (regdata1 & REG_DATA_BIT_03)
731 				;
732 			if (regdata1 & REG_DATA_BIT_05)
733 				NIC_GET_4(sc, REG_EFD00);
734 			if (regdata1 & REG_DATA_BIT_07)
735 				;
736 		}
737 
738 		timeout = 0x14;
739 		while ((NIC_GET_2(sc, REG_0000) & REG_0000_BIT_08) && timeout--)
740 			;
741 		if (timeout == 0)
742 			;
743 
744 		NIC_PUT_2(sc, REG_0000, 0x8000);
745 	}
746 	NIC_PUT_2(sc, REG_0006, 0);
747 }
748 
749 static int
netdock_read(struct netdock_softc * sc,int len)750 netdock_read(struct netdock_softc *sc, int len)
751 {
752 	struct ifnet *ifp = &sc->sc_if;
753 	struct mbuf *m;
754 
755 	m = netdock_get(sc, len);
756 	if (m == 0)
757 		return (0);
758 
759 	if_percpuq_enqueue(ifp->if_percpuq, m);
760 
761 	return (1);
762 }
763 
764 static struct mbuf *
netdock_get(struct netdock_softc * sc,int datalen)765 netdock_get(struct netdock_softc *sc, int datalen)
766 {
767 	struct mbuf *m, *top, **mp;
768 	u_char *data;
769 	int i;
770 	int len;
771 	int len4;
772 	u_int32_t *data4;
773 
774 	MGETHDR(m, M_DONTWAIT, MT_DATA);
775 	if (m == NULL)
776 		return (NULL);
777 	m_set_rcvif(m, &sc->sc_if);
778 	m->m_pkthdr.len = datalen;
779 	len = MHLEN;
780 	top = NULL;
781 	mp = &top;
782 
783 	while (datalen > 0) {
784 		if (top) {
785 			MGET(m, M_DONTWAIT, MT_DATA);
786 			if (m == 0) {
787 				m_freem(top);
788 				return (NULL);
789 			}
790 			len = MLEN;
791 		}
792 		if (datalen >= MINCLSIZE) {
793 			MCLGET(m, M_DONTWAIT);
794 			if ((m->m_flags & M_EXT) == 0) {
795 				if (top)
796 					m_freem(top);
797 				else
798 					m_freem(m);
799 				return (NULL);
800 			}
801 			len = MCLBYTES;
802 		}
803 
804 		if (mp == &top) {
805 			char *newdata = (char *)
806 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
807 			    sizeof(struct ether_header);
808 			len -= newdata - m->m_data;
809 			m->m_data = newdata;
810 		}
811 
812 		m->m_len = len = uimin(datalen, len);
813 
814 		data = mtod(m, u_char *);
815 		len4 = len >> 2;
816 		data4 = (u_int32_t *)data;
817 		for (i = 0; i < len4; i++)
818 			data4[i] = NIC_GET_4(sc, REG_DATA);
819 		for (i = len4 << 2; i < len; i++)
820 			data[i] = NIC_GET_1(sc, REG_DATA);
821 
822 		datalen -= len;
823 		*mp = m;
824 		mp = &m->m_next;
825 	}
826 
827 	return (top);
828 }
829