xref: /netbsd-src/sys/arch/next68k/dev/mb8795.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: mb8795.c,v 1.58 2017/03/31 08:38:13 msaitoh Exp $	*/
2 /*
3  * Copyright (c) 1998 Darrin B. Jewell
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: mb8795.c,v 1.58 2017/03/31 08:38:13 msaitoh Exp $");
29 
30 #include "opt_inet.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/mbuf.h>
35 #include <sys/syslog.h>
36 #include <sys/socket.h>
37 #include <sys/device.h>
38 #include <sys/malloc.h>
39 #include <sys/ioctl.h>
40 #include <sys/errno.h>
41 #include <sys/rndsource.h>
42 
43 #include <net/if.h>
44 #include <net/if_dl.h>
45 #include <net/if_ether.h>
46 
47 #include <net/if_media.h>
48 
49 #ifdef INET
50 #include <netinet/in.h>
51 #include <netinet/if_inarp.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip.h>
55 #endif
56 
57 
58 
59 #include <net/bpf.h>
60 #include <net/bpfdesc.h>
61 
62 #include <machine/cpu.h>
63 #include <machine/bus.h>
64 #include <machine/intr.h>
65 
66 /* @@@ this is here for the REALIGN_DMABUF hack below */
67 #include "nextdmareg.h"
68 #include "nextdmavar.h"
69 
70 #include "mb8795reg.h"
71 #include "mb8795var.h"
72 
73 #include "bmapreg.h"
74 
75 #ifdef DEBUG
76 #define MB8795_DEBUG
77 #endif
78 
79 #define PRINTF(x) printf x;
80 #ifdef MB8795_DEBUG
81 int mb8795_debug = 0;
82 #define DPRINTF(x) if (mb8795_debug) printf x;
83 #else
84 #define DPRINTF(x)
85 #endif
86 
87 extern int turbo;
88 
89 /*
90  * Support for
91  * Fujitsu Ethernet Data Link Controller (MB8795)
92  * and the Fujitsu Manchester Encoder/Decoder (MB502).
93  */
94 
95 void mb8795_shutdown(void *);
96 
97 bus_dmamap_t mb8795_txdma_restart(bus_dmamap_t, void *);
98 void mb8795_start_dma(struct mb8795_softc *);
99 
100 int	mb8795_mediachange(struct ifnet *);
101 void	mb8795_mediastatus(struct ifnet *, struct ifmediareq *);
102 
103 void
104 mb8795_config(struct mb8795_softc *sc, int *media, int nmedia, int defmedia)
105 {
106 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
107 
108 	DPRINTF(("%s: mb8795_config()\n",device_xname(sc->sc_dev)));
109 
110 	/* Initialize ifnet structure. */
111 	memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
112 	ifp->if_softc = sc;
113 	ifp->if_start = mb8795_start;
114 	ifp->if_ioctl = mb8795_ioctl;
115 	ifp->if_watchdog = mb8795_watchdog;
116 	ifp->if_flags =
117 		IFF_BROADCAST | IFF_NOTRAILERS;
118 
119 	/* Initialize media goo. */
120 	ifmedia_init(&sc->sc_media, 0, mb8795_mediachange,
121 		     mb8795_mediastatus);
122 	if (media != NULL) {
123 		int i;
124 		for (i = 0; i < nmedia; i++)
125 			ifmedia_add(&sc->sc_media, media[i], 0, NULL);
126 		ifmedia_set(&sc->sc_media, defmedia);
127 	} else {
128 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
129 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
130 	}
131 
132   /* Attach the interface. */
133   if_attach(ifp);
134   ether_ifattach(ifp, sc->sc_enaddr);
135 
136   sc->sc_sh = shutdownhook_establish(mb8795_shutdown, sc);
137   if (sc->sc_sh == NULL)
138     panic("mb8795_config: can't establish shutdownhook");
139 
140   rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
141                     RND_TYPE_NET, RND_FLAG_DEFAULT);
142 
143 	DPRINTF(("%s: leaving mb8795_config()\n",device_xname(sc->sc_dev)));
144 }
145 
146 /*
147  * Media change callback.
148  */
149 int
150 mb8795_mediachange(struct ifnet *ifp)
151 {
152 	struct mb8795_softc *sc = ifp->if_softc;
153 	int data;
154 
155 	if (turbo)
156 		return (0);
157 
158 	switch IFM_SUBTYPE(sc->sc_media.ifm_media) {
159 	case IFM_AUTO:
160 		if ((bus_space_read_1(sc->sc_bmap_bst, sc->sc_bmap_bsh, BMAP_DATA) &
161 		     BMAP_DATA_UTPENABLED_MASK) ||
162 		    !(bus_space_read_1(sc->sc_bmap_bst, sc->sc_bmap_bsh, BMAP_DATA) &
163 		      BMAP_DATA_UTPCARRIER_MASK)) {
164 			data = BMAP_DATA_UTPENABLE;
165 			sc->sc_media.ifm_cur->ifm_data = IFM_ETHER|IFM_10_T;
166 		} else {
167 			data = BMAP_DATA_BNCENABLE;
168 			sc->sc_media.ifm_cur->ifm_data = IFM_ETHER|IFM_10_2;
169 		}
170 		break;
171 	case IFM_10_T:
172 		data = BMAP_DATA_UTPENABLE;
173 		break;
174 	case IFM_10_2:
175 		data = BMAP_DATA_BNCENABLE;
176 		break;
177 	default:
178 		return (1);
179 		break;
180 	}
181 
182 	bus_space_write_1(sc->sc_bmap_bst, sc->sc_bmap_bsh,
183 			  BMAP_DDIR, BMAP_DDIR_UTPENABLE_MASK);
184 	bus_space_write_1(sc->sc_bmap_bst, sc->sc_bmap_bsh,
185 			  BMAP_DATA, data);
186 
187 	return (0);
188 }
189 
190 /*
191  * Media status callback.
192  */
193 void
194 mb8795_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
195 {
196 	struct mb8795_softc *sc = ifp->if_softc;
197 
198 	if (turbo)
199 		return;
200 
201 	if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_AUTO) {
202 		ifmr->ifm_active = sc->sc_media.ifm_cur->ifm_data;
203 	}
204 	if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_10_T) {
205 		ifmr->ifm_status = IFM_AVALID;
206 		if (!(bus_space_read_1(sc->sc_bmap_bst, sc->sc_bmap_bsh, BMAP_DATA) &
207 		      BMAP_DATA_UTPCARRIER_MASK))
208 			ifmr->ifm_status |= IFM_ACTIVE;
209 	} else {
210 		ifmr->ifm_status &= ~IFM_AVALID; /* don't know for 10_2 */
211 	}
212 	return;
213 }
214 
215 /****************************************************************/
216 #ifdef MB8795_DEBUG
217 #define XCHR(x) hexdigits[(x) & 0xf]
218 static void
219 mb8795_hex_dump(unsigned char *pkt, size_t len)
220 {
221 	size_t i, j;
222 
223 	printf("00000000  ");
224 	for(i=0; i<len; i++) {
225 		printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
226 		if ((i+1) % 16 == 8) {
227 			printf(" ");
228 		}
229 		if ((i+1) % 16 == 0) {
230 			printf(" %c", '|');
231 			for(j=0; j<16; j++) {
232 				printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
233 			}
234 			printf("%c\n%c%c%c%c%c%c%c%c  ", '|',
235 					XCHR((i+1)>>28),XCHR((i+1)>>24),XCHR((i+1)>>20),XCHR((i+1)>>16),
236 					XCHR((i+1)>>12), XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
237 		}
238 	}
239 	printf("\n");
240 }
241 #undef XCHR
242 #endif
243 
244 /*
245  * Controller receive interrupt.
246  */
247 void
248 mb8795_rint(struct mb8795_softc *sc)
249 {
250 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
251 	int error = 0;
252 	u_char rxstat;
253 	u_char rxmask;
254 
255 	rxstat = MB_READ_REG(sc, MB8795_RXSTAT);
256 	rxmask = MB_READ_REG(sc, MB8795_RXMASK);
257 	__USE(rxmask);
258 
259 	MB_WRITE_REG(sc, MB8795_RXSTAT, MB8795_RXSTAT_CLEAR);
260 
261 	if (rxstat & MB8795_RXSTAT_RESET) {
262 		DPRINTF(("%s: rx reset packet\n",
263 				device_xname(sc->sc_dev)));
264 		error++;
265 	}
266 	if (rxstat & MB8795_RXSTAT_SHORT) {
267 		DPRINTF(("%s: rx short packet\n",
268 				device_xname(sc->sc_dev)));
269 		error++;
270 	}
271 	if (rxstat & MB8795_RXSTAT_ALIGNERR) {
272 		DPRINTF(("%s: rx alignment error\n",
273 				device_xname(sc->sc_dev)));
274 #if 0
275 		error++;
276 #endif
277 	}
278 	if (rxstat & MB8795_RXSTAT_CRCERR) {
279 		DPRINTF(("%s: rx CRC error\n",
280 				device_xname(sc->sc_dev)));
281 #if 0
282 		error++;
283 #endif
284 	}
285 	if (rxstat & MB8795_RXSTAT_OVERFLOW) {
286 		DPRINTF(("%s: rx overflow error\n",
287 				device_xname(sc->sc_dev)));
288 #if 0
289 		error++;
290 #endif
291 	}
292 
293 	if (error) {
294 		ifp->if_ierrors++;
295 		/* @@@ handle more gracefully, free memory, etc. */
296 	}
297 
298 	if (rxstat & MB8795_RXSTAT_OK) {
299 		struct mbuf *m;
300 		int s;
301 		s = spldma();
302 
303 		while ((m = MBDMA_RX_MBUF (sc))) {
304 			/* CRC is included with the packet; trim it. */
305 			m->m_pkthdr.len = m->m_len = m->m_len - ETHER_CRC_LEN;
306 			m_set_rcvif(m, ifp);
307 
308 			/* Find receive length, keep crc */
309 			/* enable DMA interrupts while we process the packet */
310 			splx(s);
311 
312 #if defined(MB8795_DEBUG)
313 			/* Peek at the packet */
314 			DPRINTF(("%s: received packet, at VA %p-%p,len %d\n",
315 					device_xname(sc->sc_dev),mtod(m,u_char *),mtod(m,u_char *)+m->m_len,m->m_len));
316 			if (mb8795_debug > 3) {
317 				mb8795_hex_dump(mtod(m,u_char *), m->m_pkthdr.len);
318 			} else if (mb8795_debug > 2) {
319 				mb8795_hex_dump(mtod(m,u_char *), m->m_pkthdr.len < 255 ? m->m_pkthdr.len : 128 );
320 			}
321 #endif
322 
323 			/* Pass the packet up. */
324 			if_percpuq_enqueue(ifp->if_percpuq, m);
325 
326 			s = spldma();
327 
328 		}
329 
330 		splx(s);
331 
332 	}
333 
334 #ifdef MB8795_DEBUG
335 	if (mb8795_debug) {
336 		char sbuf[256];
337 
338 		snprintb(sbuf, sizeof(sbuf), MB8795_RXSTAT_BITS, rxstat);
339 		printf("%s: rx interrupt, rxstat = %s\n",
340 		       device_xname(sc->sc_dev), sbuf);
341 
342 		snprintb(sbuf, sizeof(sbuf), MB8795_RXSTAT_BITS,
343 		    MB_READ_REG(sc, MB8795_RXSTAT));
344 
345 		printf("rxstat = %s\n", sbuf);
346 
347 		snprintb(sbuf, sizeof(sbuf), MB8795_RXMASK_BITS,
348 		    MB_READ_REG(sc, MB8795_RXMASK));
349 		printf("rxmask = %s\n", sbuf);
350 
351 		snprintb(sbuf, sizeof(sbuf), MB8795_RXMODE_BITS,
352 		    MB_READ_REG(sc, MB8795_RXMODE));
353 		printf("rxmode = %s\n", sbuf);
354 	}
355 #endif
356 
357 	return;
358 }
359 
360 /*
361  * Controller transmit interrupt.
362  */
363 void
364 mb8795_tint(struct mb8795_softc *sc)
365 {
366 	u_char txstat;
367 	u_char txmask;
368 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
369 
370 	panic ("tint");
371 	txstat = MB_READ_REG(sc, MB8795_TXSTAT);
372 	txmask = MB_READ_REG(sc, MB8795_TXMASK);
373 	__USE(txmask);
374 
375 	if ((txstat & MB8795_TXSTAT_READY) ||
376 	    (txstat & MB8795_TXSTAT_TXRECV)) {
377 		/* printf("X"); */
378 		MB_WRITE_REG(sc, MB8795_TXSTAT, MB8795_TXSTAT_CLEAR);
379 		/* MB_WRITE_REG(sc, MB8795_TXMASK, txmask & ~MB8795_TXMASK_READYIE); */
380 		/* MB_WRITE_REG(sc, MB8795_TXMASK, txmask & ~MB8795_TXMASK_TXRXIE); */
381 		MB_WRITE_REG(sc, MB8795_TXMASK, 0);
382 		if ((ifp->if_flags & IFF_RUNNING) && !IF_IS_EMPTY(&sc->sc_tx_snd)) {
383 			void mb8795_start_dma(struct mb8795_softc *); /* XXXX */
384 			/* printf ("Z"); */
385 			mb8795_start_dma(sc);
386 		}
387 		return;
388 	}
389 
390 	if (txstat & MB8795_TXSTAT_SHORTED) {
391 		printf("%s: tx cable shorted\n", device_xname(sc->sc_dev));
392 		ifp->if_oerrors++;
393 	}
394 	if (txstat & MB8795_TXSTAT_UNDERFLOW) {
395 		printf("%s: tx underflow\n", device_xname(sc->sc_dev));
396 		ifp->if_oerrors++;
397 	}
398 	if (txstat & MB8795_TXSTAT_COLLERR) {
399 		DPRINTF(("%s: tx collision\n", device_xname(sc->sc_dev)));
400 		ifp->if_collisions++;
401 	}
402 	if (txstat & MB8795_TXSTAT_COLLERR16) {
403 		printf("%s: tx 16th collision\n", device_xname(sc->sc_dev));
404 		ifp->if_oerrors++;
405 		ifp->if_collisions += 16;
406 	}
407 
408 #if 0
409 	if (txstat & MB8795_TXSTAT_READY) {
410 		char sbuf[256];
411 
412 		snprintb(sbuf, sizeof(sbuf), MB8795_TXSTAT_BITS, txstat);
413 		panic("%s: unexpected tx interrupt %s",
414 				device_xname(sc->sc_dev), sbuf);
415 
416 		/* turn interrupt off */
417 		MB_WRITE_REG(sc, MB8795_TXMASK, txmask & ~MB8795_TXMASK_READYIE);
418 	}
419 #endif
420 
421 	return;
422 }
423 
424 /****************************************************************/
425 
426 void
427 mb8795_reset(struct mb8795_softc *sc)
428 {
429 	int s;
430 	int i;
431 
432 	s = splnet();
433 
434 	DPRINTF (("%s: mb8795_reset()\n",device_xname(sc->sc_dev)));
435 
436 	sc->sc_ethercom.ec_if.if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
437 	sc->sc_ethercom.ec_if.if_timer = 0;
438 
439 	MBDMA_RESET(sc);
440 
441 	MB_WRITE_REG(sc, MB8795_RESET,  MB8795_RESET_MODE);
442 
443 	mb8795_mediachange(&sc->sc_ethercom.ec_if);
444 
445 #if 0 /* This interrupt was sometimes failing to ack correctly
446        * causing a loop @@@
447        */
448 	MB_WRITE_REG(sc, MB8795_TXMASK,
449 			  MB8795_TXMASK_UNDERFLOWIE | MB8795_TXMASK_COLLIE | MB8795_TXMASK_COLL16IE
450 			  | MB8795_TXMASK_PARERRIE);
451 #else
452 	MB_WRITE_REG(sc, MB8795_TXMASK, 0);
453 #endif
454 	MB_WRITE_REG(sc, MB8795_TXSTAT, MB8795_TXSTAT_CLEAR);
455 
456 #if 0
457 	MB_WRITE_REG(sc, MB8795_RXMASK,
458 			  MB8795_RXMASK_OKIE | MB8795_RXMASK_RESETIE | MB8795_RXMASK_SHORTIE	|
459 			  MB8795_RXMASK_ALIGNERRIE	|  MB8795_RXMASK_CRCERRIE | MB8795_RXMASK_OVERFLOWIE);
460 #else
461 	MB_WRITE_REG(sc, MB8795_RXMASK,
462 			  MB8795_RXMASK_OKIE | MB8795_RXMASK_RESETIE | MB8795_RXMASK_SHORTIE);
463 #endif
464 
465 	MB_WRITE_REG(sc, MB8795_RXSTAT, MB8795_RXSTAT_CLEAR);
466 
467 	for(i=0;i<sizeof(sc->sc_enaddr);i++) {
468 		MB_WRITE_REG(sc, MB8795_ENADDR+i, sc->sc_enaddr[i]);
469 	}
470 
471 	DPRINTF(("%s: initializing ethernet %02x:%02x:%02x:%02x:%02x:%02x, size=%d\n",
472 		 device_xname(sc->sc_dev),
473 		 sc->sc_enaddr[0],sc->sc_enaddr[1],sc->sc_enaddr[2],
474 		 sc->sc_enaddr[3],sc->sc_enaddr[4],sc->sc_enaddr[5],
475 		 sizeof(sc->sc_enaddr)));
476 
477 	MB_WRITE_REG(sc, MB8795_RESET, 0);
478 
479 	splx(s);
480 }
481 
482 void
483 mb8795_watchdog(struct ifnet *ifp)
484 {
485 	struct mb8795_softc *sc = ifp->if_softc;
486 
487 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
488 	++ifp->if_oerrors;
489 
490 	DPRINTF(("%s: %lld input errors, %lld input packets\n",
491 			device_xname(sc->sc_dev), ifp->if_ierrors, ifp->if_ipackets));
492 
493 	ifp->if_flags &= ~IFF_RUNNING;
494 	mb8795_init(sc);
495 }
496 
497 /*
498  * Initialization of interface; set up initialization block
499  * and transmit/receive descriptor rings.
500  */
501 void
502 mb8795_init(struct mb8795_softc *sc)
503 {
504 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
505 	int s;
506 
507 	DPRINTF (("%s: mb8795_init()\n",device_xname(sc->sc_dev)));
508 
509 	if (ifp->if_flags & IFF_UP) {
510 		int rxmode;
511 
512 		s = spldma();
513 		if ((ifp->if_flags & IFF_RUNNING) == 0)
514 			mb8795_reset(sc);
515 
516 		if (ifp->if_flags & IFF_PROMISC)
517 			rxmode = MB8795_RXMODE_PROMISCUOUS;
518 		else
519 			rxmode = MB8795_RXMODE_NORMAL;
520 		/* XXX add support for multicast */
521 		if (turbo)
522 			rxmode |= MB8795_RXMODE_TEST;
523 
524 		/* switching mode probably borken now with turbo */
525 		MB_WRITE_REG(sc, MB8795_TXMODE,
526 			     turbo ? MB8795_TXMODE_TURBO1 : MB8795_TXMODE_LB_DISABLE);
527 		MB_WRITE_REG(sc, MB8795_RXMODE, rxmode);
528 
529 		if ((ifp->if_flags & IFF_RUNNING) == 0) {
530 			MBDMA_RX_SETUP(sc);
531 			MBDMA_TX_SETUP(sc);
532 
533 			ifp->if_flags |= IFF_RUNNING;
534 			ifp->if_flags &= ~IFF_OACTIVE;
535 			ifp->if_timer = 0;
536 
537 			MBDMA_RX_GO(sc);
538 		}
539 		splx(s);
540 #if 0
541 		s = spldma();
542 		if (! IF_IS_EMPTY(&sc->sc_tx_snd)) {
543 			mb8795_start_dma(ifp);
544 		}
545 		splx(s);
546 #endif
547 	} else {
548 		mb8795_reset(sc);
549 	}
550 }
551 
552 void
553 mb8795_shutdown(void *arg)
554 {
555 	struct mb8795_softc *sc = (struct mb8795_softc *)arg;
556 
557 	DPRINTF(("%s: mb8795_shutdown()\n",device_xname(sc->sc_dev)));
558 
559 	mb8795_reset(sc);
560 }
561 
562 /****************************************************************/
563 int
564 mb8795_ioctl(struct ifnet *ifp, u_long cmd, void *data)
565 {
566 	struct mb8795_softc *sc = ifp->if_softc;
567 	struct ifaddr *ifa = (struct ifaddr *)data;
568 	struct ifreq *ifr = (struct ifreq *)data;
569 	int s, error = 0;
570 
571 	s = splnet();
572 
573 	DPRINTF(("%s: mb8795_ioctl()\n",device_xname(sc->sc_dev)));
574 
575 	switch (cmd) {
576 
577 	case SIOCINITIFADDR:
578 		DPRINTF(("%s: mb8795_ioctl() SIOCINITIFADDR\n",device_xname(sc->sc_dev)));
579 		ifp->if_flags |= IFF_UP;
580 
581 		mb8795_init(sc);
582 		switch (ifa->ifa_addr->sa_family) {
583 #ifdef INET
584 		case AF_INET:
585 			arp_ifinit(ifp, ifa);
586 			break;
587 #endif
588 		default:
589 			break;
590 		}
591 		break;
592 
593 
594 	case SIOCSIFFLAGS:
595 		DPRINTF(("%s: mb8795_ioctl() SIOCSIFFLAGS\n",device_xname(sc->sc_dev)));
596 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
597 			break;
598 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
599 		case IFF_RUNNING:
600 			/*
601 			 * If interface is marked down and it is running, then
602 			 * stop it.
603 			 */
604 /* 			ifp->if_flags &= ~IFF_RUNNING; */
605 			mb8795_reset(sc);
606 			break;
607 		case IFF_UP:
608 			/*
609 			 * If interface is marked up and it is stopped, then
610 			 * start it.
611 			 */
612 			mb8795_init(sc);
613 			break;
614 		default:
615 			/*
616 			 * Reset the interface to pick up changes in any other
617 			 * flags that affect hardware registers.
618 			 */
619 			mb8795_init(sc);
620 			break;
621 		}
622 #ifdef MB8795_DEBUG
623 		if (ifp->if_flags & IFF_DEBUG)
624 			sc->sc_debug = 1;
625 		else
626 			sc->sc_debug = 0;
627 #endif
628 		break;
629 
630 	case SIOCADDMULTI:
631 	case SIOCDELMULTI:
632 		DPRINTF(("%s: mb8795_ioctl() SIOCADDMULTI\n",
633 		    device_xname(sc->sc_dev)));
634 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
635 			/*
636 			 * Multicast list has changed; set the hardware filter
637 			 * accordingly.
638 			 */
639 			if (ifp->if_flags & IFF_RUNNING)
640 				mb8795_init(sc);
641 			error = 0;
642 		}
643 		break;
644 
645 	case SIOCGIFMEDIA:
646 	case SIOCSIFMEDIA:
647 		DPRINTF(("%s: mb8795_ioctl() SIOCSIFMEDIA\n",device_xname(sc->sc_dev)));
648 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
649 		break;
650 
651 	default:
652 		error = ether_ioctl(ifp, cmd, data);
653 		break;
654 	}
655 
656 	splx(s);
657 
658 #if 0
659 	DPRINTF(("DEBUG: mb8795_ioctl(0x%lx) returning %d\n",
660 			cmd,error));
661 #endif
662 
663 	return (error);
664 }
665 
666 /*
667  * Setup output on interface.
668  * Get another datagram to send off of the interface queue, and map it to the
669  * interface before starting the output.
670  * Called only at splnet or interrupt level.
671  */
672 void
673 mb8795_start(struct ifnet *ifp)
674 {
675 	struct mb8795_softc *sc = ifp->if_softc;
676 	struct mbuf *m;
677 	int s;
678 
679 	DPRINTF(("%s: mb8795_start()\n",device_xname(sc->sc_dev)));
680 
681 #ifdef DIAGNOSTIC
682 	IFQ_POLL(&ifp->if_snd, m);
683 	if (m == 0) {
684 		panic("%s: No packet to start",
685 		      device_xname(sc->sc_dev));
686 	}
687 #endif
688 
689 	while (1) {
690 		if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
691 			return;
692 
693 #if 0
694 		return;	/* @@@ Turn off xmit for debugging */
695 #endif
696 
697 		ifp->if_flags |= IFF_OACTIVE;
698 
699 		IFQ_DEQUEUE(&ifp->if_snd, m);
700 		if (m == 0) {
701 			ifp->if_flags &= ~IFF_OACTIVE;
702 			return;
703 		}
704 
705 		/*
706 		 * Pass packet to bpf if there is a listener.
707 		 */
708 		bpf_mtap(ifp, m);
709 
710 		s = spldma();
711 		IF_ENQUEUE(&sc->sc_tx_snd, m);
712 		if (!MBDMA_TX_ISACTIVE(sc))
713 			mb8795_start_dma(sc);
714 		splx(s);
715 
716 		ifp->if_flags &= ~IFF_OACTIVE;
717 	}
718 
719 }
720 
721 void
722 mb8795_start_dma(struct mb8795_softc *sc)
723 {
724 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
725 	struct mbuf *m;
726 	u_char txmask;
727 
728 	DPRINTF(("%s: mb8795_start_dma()\n",device_xname(sc->sc_dev)));
729 
730 #if (defined(DIAGNOSTIC))
731 	{
732 		u_char txstat;
733 		txstat = MB_READ_REG(sc, MB8795_TXSTAT);
734 		if (!turbo && !(txstat & MB8795_TXSTAT_READY)) {
735 			/* @@@ I used to panic here, but then it paniced once.
736 			 * Let's see if I can just reset instead. [ dbj 980706.1900 ]
737 			 */
738 			printf("%s: transmitter not ready\n",
739 				device_xname(sc->sc_dev));
740 			ifp->if_flags &= ~IFF_RUNNING;
741 			mb8795_init(sc);
742 			return;
743 		}
744 	}
745 #endif
746 
747 #if 0
748 	return;	/* @@@ Turn off xmit for debugging */
749 #endif
750 
751 	IF_DEQUEUE(&sc->sc_tx_snd, m);
752 	if (m == 0) {
753 #ifdef DIAGNOSTIC
754 		panic("%s: No packet to start_dma",
755 		      device_xname(sc->sc_dev));
756 #endif
757 		return;
758 	}
759 
760 	MB_WRITE_REG(sc, MB8795_TXSTAT, MB8795_TXSTAT_CLEAR);
761 	txmask = MB_READ_REG(sc, MB8795_TXMASK);
762 	__USE(txmask);
763 	/* MB_WRITE_REG(sc, MB8795_TXMASK, txmask | MB8795_TXMASK_READYIE); */
764 	/* MB_WRITE_REG(sc, MB8795_TXMASK, txmask | MB8795_TXMASK_TXRXIE); */
765 
766 	ifp->if_timer = 5;
767 
768 	if (MBDMA_TX_MBUF(sc, m))
769 		return;
770 
771 	MBDMA_TX_GO(sc);
772 	if (turbo)
773 		MB_WRITE_REG(sc, MB8795_TXMODE, MB8795_TXMODE_TURBO1 | MB8795_TXMODE_TURBOSTART);
774 
775 	ifp->if_opackets++;
776 }
777 
778 /****************************************************************/
779