xref: /openbsd-src/sys/dev/ic/smc91cxx.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: smc91cxx.c,v 1.30 2008/11/28 02:44:17 brad Exp $	*/
2 /*	$NetBSD: smc91cxx.c,v 1.11 1998/08/08 23:51:41 mycroft Exp $	*/
3 
4 /*-
5  * Copyright (c) 1997 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1996 Gardner Buchanan <gbuchanan@shl.com>
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by Gardner Buchanan.
49  * 4. The name of Gardner Buchanan may not be used to endorse or promote
50  *    products derived from this software without specific prior written
51  *    permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
54  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
55  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
57  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
58  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
62  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63  *
64  *   from FreeBSD Id: if_sn.c,v 1.4 1996/03/18 15:47:16 gardner Exp
65  */
66 
67 /*
68  * Core driver for the SMC 91Cxx family of Ethernet chips.
69  *
70  * Memory allocation interrupt logic is drived from an SMC 91C90 driver
71  * written for NetBSD/amiga by Michael Hitch.
72  */
73 
74 #include "bpfilter.h"
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/mbuf.h>
79 #include <sys/syslog.h>
80 #include <sys/socket.h>
81 #include <sys/device.h>
82 #include <sys/timeout.h>
83 #include <sys/kernel.h>
84 #include <sys/malloc.h>
85 #include <sys/ioctl.h>
86 #include <sys/errno.h>
87 
88 #include <machine/bus.h>
89 #include <machine/intr.h>
90 
91 #include <net/if.h>
92 #include <net/if_dl.h>
93 #include <net/if_media.h>
94 
95 #ifdef INET
96 #include <netinet/in.h>
97 #include <netinet/if_ether.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/in_var.h>
100 #include <netinet/ip.h>
101 #endif
102 
103 #if NBPFILTER > 0
104 #include <net/bpf.h>
105 #endif
106 
107 #include <dev/mii/mii.h>
108 #include <dev/mii/miivar.h>
109 #include <dev/mii/mii_bitbang.h>
110 
111 #include <dev/ic/smc91cxxreg.h>
112 #include <dev/ic/smc91cxxvar.h>
113 
114 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
115 #define bus_space_write_multi_stream_2 bus_space_write_multi_2
116 #define bus_space_write_multi_stream_4 bus_space_write_multi_4
117 #define bus_space_read_multi_stream_2  bus_space_read_multi_2
118 #define bus_space_read_multi_stream_4  bus_space_read_multi_4
119 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
120 
121 /* XXX Hardware padding doesn't work yet(?) */
122 #define	SMC91CXX_SW_PAD
123 
124 const char *smc91cxx_idstrs[] = {
125 	NULL,				/* 0 */
126 	NULL,				/* 1 */
127 	NULL,				/* 2 */
128 	"SMC91C90/91C92",		/* 3 */
129 	"SMC91C94/91C96",		/* 4 */
130 	"SMC91C95",			/* 5 */
131 	NULL,				/* 6 */
132 	"SMC91C100",			/* 7 */
133 	"SMC91C100FD",			/* 8 */
134 	NULL,				/* 9 */
135 	NULL,				/* 10 */
136 	NULL,				/* 11 */
137 	NULL,				/* 12 */
138 	NULL,				/* 13 */
139 	NULL,				/* 14 */
140 	NULL,				/* 15 */
141 };
142 
143 /* Supported media types. */
144 const int smc91cxx_media[] = {
145 	IFM_ETHER|IFM_10_T,
146 	IFM_ETHER|IFM_10_5,
147 };
148 #define	NSMC91CxxMEDIA	(sizeof(smc91cxx_media) / sizeof(smc91cxx_media[0]))
149 
150 /*
151  * MII bit-bang glue.
152  */
153 u_int32_t smc91cxx_mii_bitbang_read(struct device *);
154 void smc91cxx_mii_bitbang_write(struct device *, u_int32_t);
155 
156 const struct mii_bitbang_ops smc91cxx_mii_bitbang_ops = {
157 	smc91cxx_mii_bitbang_read,
158 	smc91cxx_mii_bitbang_write,
159 	{
160 		MR_MDO,		/* MII_BIT_MDO */
161 		MR_MDI,		/* MII_BIT_MDI */
162 		MR_MCLK,	/* MII_BIT_MDC */
163 		MR_MDOE,	/* MII_BIT_DIR_HOST_PHY */
164 		0,		/* MII_BIT_DIR_PHY_HOST */
165 	}
166 };
167 
168 struct cfdriver sm_cd = {
169 	NULL, "sm", DV_IFNET
170 };
171 
172 /* MII callbacks */
173 int	smc91cxx_mii_readreg(struct device *, int, int);
174 void	smc91cxx_mii_writereg(struct device *, int, int, int);
175 void	smc91cxx_statchg(struct device *);
176 void	smc91cxx_tick(void *);
177 
178 int	smc91cxx_mediachange(struct ifnet *);
179 void	smc91cxx_mediastatus(struct ifnet *, struct ifmediareq *);
180 
181 int	smc91cxx_set_media(struct smc91cxx_softc *, int);
182 
183 void	smc91cxx_read(struct smc91cxx_softc *);
184 void	smc91cxx_reset(struct smc91cxx_softc *);
185 void	smc91cxx_start(struct ifnet *);
186 void	smc91cxx_resume(struct smc91cxx_softc *);
187 void	smc91cxx_watchdog(struct ifnet *);
188 int	smc91cxx_ioctl(struct ifnet *, u_long, caddr_t);
189 
190 static __inline int ether_cmp(void *, void *);
191 static __inline int
192 ether_cmp(va, vb)
193 	void *va, *vb;
194 {
195 	u_int8_t *a = va;
196 	u_int8_t *b = vb;
197 
198 	return ((a[5] != b[5]) || (a[4] != b[4]) || (a[3] != b[3]) ||
199 		(a[2] != b[2]) || (a[1] != b[1]) || (a[0] != b[0]));
200 }
201 
202 void
203 smc91cxx_attach(sc, myea)
204 	struct smc91cxx_softc *sc;
205 	u_int8_t *myea;
206 {
207 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
208 	bus_space_tag_t bst = sc->sc_bst;
209 	bus_space_handle_t bsh = sc->sc_bsh;
210 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
211 	u_int32_t miicapabilities;
212 	u_int16_t tmp;
213 	int i, aui;
214 	const char *idstr;
215 
216 	/* Make sure the chip is stopped. */
217 	smc91cxx_stop(sc);
218 
219 	SMC_SELECT_BANK(sc, 3);
220 	tmp = bus_space_read_2(bst, bsh, REVISION_REG_W);
221 	sc->sc_chipid = RR_ID(tmp);
222 	/* check magic number */
223 	if ((tmp & BSR_DETECT_MASK) != BSR_DETECT_VALUE) {
224 		idstr = NULL;
225 		printf("%s: invalid BSR 0x%04x\n", sc->sc_dev.dv_xname, tmp);
226 	} else
227 		idstr = smc91cxx_idstrs[RR_ID(tmp)];
228 #ifdef SMC_DEBUG
229 	printf("\n%s: ", sc->sc_dev.dv_xname);
230 	if (idstr != NULL)
231 		printf("%s, ", idstr);
232 	else
233 		printf("unknown chip id %d, ", sc->sc_chipid);
234 	printf("revision %d", RR_REV(tmp));
235 #endif
236 
237 	/* Read the station address from the chip. */
238 	SMC_SELECT_BANK(sc, 1);
239 	if (myea == NULL) {
240 		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
241 			tmp = bus_space_read_2(bst, bsh, IAR_ADDR0_REG_W + i);
242 			sc->sc_arpcom.ac_enaddr[i + 1] = (tmp >>8) & 0xff;
243 			sc->sc_arpcom.ac_enaddr[i] = tmp & 0xff;
244 		}
245 	} else {
246 		bcopy(myea, sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN);
247 	}
248 
249 	printf(": address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
250 
251 	/* Initialize the ifnet structure. */
252 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
253 	ifp->if_softc = sc;
254 	ifp->if_start = smc91cxx_start;
255 	ifp->if_ioctl = smc91cxx_ioctl;
256 	ifp->if_watchdog = smc91cxx_watchdog;
257 	ifp->if_flags =
258 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
259 	IFQ_SET_READY(&ifp->if_snd);
260 
261 	/* Attach the interface. */
262 	if_attach(ifp);
263 	ether_ifattach(ifp);
264 
265 	/*
266 	 * Initialize our media structures and MII info.  We will
267 	 * probe the MII if we are on the SMC91Cxx
268 	 */
269 	sc->sc_mii.mii_ifp = ifp;
270 	sc->sc_mii.mii_readreg = smc91cxx_mii_readreg;
271 	sc->sc_mii.mii_writereg = smc91cxx_mii_writereg;
272 	sc->sc_mii.mii_statchg = smc91cxx_statchg;
273 	ifmedia_init(ifm, 0, smc91cxx_mediachange, smc91cxx_mediastatus);
274 
275 	SMC_SELECT_BANK(sc, 1);
276 	tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W);
277 
278 	miicapabilities = BMSR_MEDIAMASK|BMSR_ANEG;
279 	switch (sc->sc_chipid) {
280 	case CHIP_91100:
281 		/*
282 		 * The 91100 does not have full-duplex capabilities,
283 		 * even if the PHY does.
284 		 */
285 		miicapabilities &= ~(BMSR_100TXFDX | BMSR_10TFDX);
286 	case CHIP_91100FD:
287 		if (tmp & CR_MII_SELECT) {
288 #ifdef SMC_DEBUG
289 			printf("%s: default media MII\n",
290 			    sc->sc_dev.dv_xname);
291 #endif
292 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
293 			    MII_PHY_ANY, MII_OFFSET_ANY, 0);
294 			if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
295 				ifmedia_add(&sc->sc_mii.mii_media,
296 				    IFM_ETHER|IFM_NONE, 0, NULL);
297 				ifmedia_set(&sc->sc_mii.mii_media,
298 				    IFM_ETHER|IFM_NONE);
299 			} else {
300 				ifmedia_set(&sc->sc_mii.mii_media,
301 				    IFM_ETHER|IFM_AUTO);
302 			}
303 			sc->sc_flags |= SMC_FLAGS_HAS_MII;
304 			break;
305 		}
306 		/*FALLTHROUGH*/
307 	default:
308 		aui = tmp & CR_AUI_SELECT;
309 #ifdef SMC_DEBUG
310 		printf("%s: default media %s\n", sc->sc_dev.dv_xname,
311 			aui ? "AUI" : "UTP");
312 #endif
313 		for (i = 0; i < NSMC91CxxMEDIA; i++)
314 			ifmedia_add(ifm, smc91cxx_media[i], 0, NULL);
315 		ifmedia_set(ifm, IFM_ETHER | (aui ? IFM_10_5 : IFM_10_T));
316 		break;
317 	}
318 
319 	/* The attach is successful. */
320 	sc->sc_flags |= SMC_FLAGS_ATTACHED;
321 }
322 
323 /*
324  * Change media according to request.
325  */
326 int
327 smc91cxx_mediachange(ifp)
328 	struct ifnet *ifp;
329 {
330 	struct smc91cxx_softc *sc = ifp->if_softc;
331 
332 	return (smc91cxx_set_media(sc, sc->sc_mii.mii_media.ifm_media));
333 }
334 
335 int
336 smc91cxx_set_media(sc, media)
337 	struct smc91cxx_softc *sc;
338 	int media;
339 {
340 	bus_space_tag_t bst = sc->sc_bst;
341 	bus_space_handle_t bsh = sc->sc_bsh;
342 	u_int16_t tmp;
343 
344 	/*
345 	 * If the interface is not currently powered on, just return.
346 	 * When it is enabled later, smc91cxx_init() will properly set
347 	 * up the media for us.
348 	 */
349 	if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0)
350 		return (0);
351 
352 	if (IFM_TYPE(media) != IFM_ETHER)
353 		return (EINVAL);
354 
355 	if (sc->sc_flags & SMC_FLAGS_HAS_MII)
356 		return (mii_mediachg(&sc->sc_mii));
357 
358 	switch (IFM_SUBTYPE(media)) {
359 	case IFM_10_T:
360 	case IFM_10_5:
361 		SMC_SELECT_BANK(sc, 1);
362 		tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W);
363 		if (IFM_SUBTYPE(media) == IFM_10_5)
364 			tmp |= CR_AUI_SELECT;
365 		else
366 			tmp &= ~CR_AUI_SELECT;
367 		bus_space_write_2(bst, bsh, CONFIG_REG_W, tmp);
368 		delay(20000);	/* XXX is this needed? */
369 		break;
370 
371 	default:
372 		return (EINVAL);
373 	}
374 
375 	return (0);
376 }
377 
378 /*
379  * Notify the world which media we're using.
380  */
381 void
382 smc91cxx_mediastatus(ifp, ifmr)
383 	struct ifnet *ifp;
384 	struct ifmediareq *ifmr;
385 {
386 	struct smc91cxx_softc *sc = ifp->if_softc;
387 	bus_space_tag_t bst = sc->sc_bst;
388 	bus_space_handle_t bsh = sc->sc_bsh;
389 	u_int16_t tmp;
390 
391 	if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0) {
392 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
393 		ifmr->ifm_status = 0;
394 		return;
395 	}
396 
397 	/*
398 	 * If we have MII, go ask the PHY what's going on.
399 	 */
400 	if (sc->sc_flags & SMC_FLAGS_HAS_MII) {
401 		mii_pollstat(&sc->sc_mii);
402 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
403 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
404 		return;
405 	}
406 
407 	SMC_SELECT_BANK(sc, 1);
408 	tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W);
409 	ifmr->ifm_active =
410 	    IFM_ETHER | ((tmp & CR_AUI_SELECT) ? IFM_10_5 : IFM_10_T);
411 }
412 
413 /*
414  * Reset and initialize the chip.
415  */
416 void
417 smc91cxx_init(sc)
418 	struct smc91cxx_softc *sc;
419 {
420 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
421 	bus_space_tag_t bst = sc->sc_bst;
422 	bus_space_handle_t bsh = sc->sc_bsh;
423 	u_int16_t tmp;
424 	int s, i;
425 
426 	s = splnet();
427 
428 	/*
429 	 * This resets the registers mostly to defaults, but doesn't
430 	 * affect the EEPROM.  After the reset cycle, we pause briefly
431 	 * for the chip to recover.
432 	 *
433 	 * XXX how long are we really supposed to delay?  --thorpej
434 	 */
435 	SMC_SELECT_BANK(sc, 0);
436 	bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, RCR_SOFTRESET);
437 	delay(100);
438 	bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, 0);
439 	delay(200);
440 
441 	bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, 0);
442 
443 	/* Set the Ethernet address. */
444 	SMC_SELECT_BANK(sc, 1);
445 	for (i = 0; i < ETHER_ADDR_LEN; i++ )
446 		bus_space_write_1(bst, bsh, IAR_ADDR0_REG_W + i,
447 		    sc->sc_arpcom.ac_enaddr[i]);
448 
449 	/*
450 	 * Set the control register to automatically release successfully
451 	 * transmitted packets (making the best use of our limited memory)
452 	 * and enable the EPH interrupt on certain TX errors.
453 	 */
454 	bus_space_write_2(bst, bsh, CONTROL_REG_W, (CTR_AUTO_RELEASE |
455 	    CTR_TE_ENABLE | CTR_CR_ENABLE | CTR_LE_ENABLE));
456 
457 	/*
458 	 * Reset the MMU and wait for it to be un-busy.
459 	 */
460 	SMC_SELECT_BANK(sc, 2);
461 	bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_RESET);
462 	while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
463 		/* XXX bound this loop! */ ;
464 
465 	/*
466 	 * Disable all interrupts.
467 	 */
468 	bus_space_write_1(bst, bsh, INTR_MASK_REG_B, 0);
469 
470 	/*
471 	 * Set current media.
472 	 */
473 	smc91cxx_set_media(sc, sc->sc_mii.mii_media.ifm_cur->ifm_media);
474 
475 	/*
476 	 * Set the receive filter.  We want receive enable and auto
477 	 * strip of CRC from received packet.  If we are in promisc. mode,
478 	 * then set that bit as well.
479 	 *
480 	 * XXX Initialize multicast filter.  For now, we just accept
481 	 * XXX all multicast.
482 	 */
483 	SMC_SELECT_BANK(sc, 0);
484 
485 	tmp = RCR_ENABLE | RCR_STRIP_CRC | RCR_ALMUL;
486 	if (ifp->if_flags & IFF_PROMISC)
487 		tmp |= RCR_PROMISC;
488 
489 	bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, tmp);
490 
491 	/*
492 	 * Set transmitter control to "enabled".
493 	 */
494 	tmp = TCR_ENABLE;
495 
496 #ifndef SMC91CXX_SW_PAD
497 	/*
498 	 * Enable hardware padding of transmitted packets.
499 	 * XXX doesn't work?
500 	 */
501 	tmp |= TCR_PAD_ENABLE;
502 #endif
503 
504 	bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, tmp);
505 
506 	/*
507 	 * Now, enable interrupts.
508 	 */
509 	SMC_SELECT_BANK(sc, 2);
510 
511 	bus_space_write_1(bst, bsh, INTR_MASK_REG_B,
512 	    IM_EPH_INT | IM_RX_OVRN_INT | IM_RCV_INT | IM_TX_INT);
513 
514 	/* Interface is now running, with no output active. */
515 	ifp->if_flags |= IFF_RUNNING;
516 	ifp->if_flags &= ~IFF_OACTIVE;
517 
518 	if (sc->sc_flags & SMC_FLAGS_HAS_MII) {
519 		/* Start the one second clock. */
520 		timeout_set(&sc->sc_mii_timeout, smc91cxx_tick, sc);
521 		timeout_add_sec(&sc->sc_mii_timeout, 1);
522 	}
523 
524 	/*
525 	 * Attempt to start any pending transmission.
526 	 */
527 	smc91cxx_start(ifp);
528 
529 	splx(s);
530 }
531 
532 /*
533  * Start output on an interface.
534  * Must be called at splnet or interrupt level.
535  */
536 void
537 smc91cxx_start(ifp)
538 	struct ifnet *ifp;
539 {
540 	struct smc91cxx_softc *sc = ifp->if_softc;
541 	bus_space_tag_t bst = sc->sc_bst;
542 	bus_space_handle_t bsh = sc->sc_bsh;
543 	u_int len;
544 	struct mbuf *m, *top;
545 	u_int16_t length, npages;
546 	u_int8_t packetno;
547 	int timo, pad;
548 
549 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
550 		return;
551 
552  again:
553 	/*
554 	 * Peek at the next packet.
555 	 */
556 	IFQ_POLL(&ifp->if_snd, m);
557 	if (m == NULL)
558 		return;
559 
560 	/*
561 	 * Compute the frame length and set pad to give an overall even
562 	 * number of bytes.  Below, we assume that the packet length
563 	 * is even.
564 	 */
565 	for (len = 0, top = m; m != NULL; m = m->m_next)
566 		len += m->m_len;
567 	pad = (len & 1);
568 
569 	/*
570 	 * We drop packets that are too large.  Perhaps we should
571 	 * truncate them instead?
572 	 */
573 	if ((len + pad) > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
574 		printf("%s: large packet discarded\n", sc->sc_dev.dv_xname);
575 		ifp->if_oerrors++;
576 		IFQ_DEQUEUE(&ifp->if_snd, m);
577 		m_freem(m);
578 		goto readcheck;
579 	}
580 
581 #ifdef SMC91CXX_SW_PAD
582 	/*
583 	 * Not using hardware padding; pad to ETHER_MIN_LEN.
584 	 */
585 	if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
586 		pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
587 #endif
588 
589 	length = pad + len;
590 
591 	/*
592 	 * The MMU has a 256 byte page size.  The MMU expects us to
593 	 * ask for "npages - 1".  We include space for the status word,
594 	 * byte count, and control bytes in the allocation request.
595 	 */
596 	npages = (length + 6) >> 8;
597 
598 	/*
599 	 * Now allocate the memory.
600 	 */
601 	SMC_SELECT_BANK(sc, 2);
602 	bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_ALLOC | npages);
603 
604 	timo = MEMORY_WAIT_TIME;
605 	do {
606 		if (bus_space_read_1(bst, bsh, INTR_STAT_REG_B) & IM_ALLOC_INT)
607 			break;
608 		delay(1);
609 	} while (--timo);
610 
611 	packetno = bus_space_read_1(bst, bsh, ALLOC_RESULT_REG_B);
612 
613 	if (packetno & ARR_FAILED || timo == 0) {
614 		/*
615 		 * No transmit memory is available.  Record the number
616 		 * of requestd pages and enable the allocation completion
617 		 * interrupt.  Set up the watchdog timer in case we miss
618 		 * the interrupt.  Mark the interface as active so that
619 		 * no one else attempts to transmit while we're allocating
620 		 * memory.
621 		 */
622 		bus_space_write_1(bst, bsh, INTR_MASK_REG_B,
623 		    bus_space_read_1(bst, bsh, INTR_MASK_REG_B) | IM_ALLOC_INT);
624 
625 		ifp->if_timer = 5;
626 		ifp->if_flags |= IFF_OACTIVE;
627 
628 		return;
629 	}
630 
631 	/*
632 	 * We have a packet number - set the data window.
633 	 */
634 	bus_space_write_1(bst, bsh, PACKET_NUM_REG_B, packetno);
635 
636 	/*
637 	 * Point to the beginning of the packet.
638 	 */
639 	bus_space_write_2(bst, bsh, POINTER_REG_W, PTR_AUTOINC /* | 0x0000 */);
640 
641 	/*
642 	 * Send the packet length (+6 for stats, length, and control bytes)
643 	 * and the status word (set to zeros).
644 	 */
645 	bus_space_write_2(bst, bsh, DATA_REG_W, 0);
646 	bus_space_write_1(bst, bsh, DATA_REG_B, (length + 6) & 0xff);
647 	bus_space_write_1(bst, bsh, DATA_REG_B, ((length + 6) >> 8) & 0xff);
648 
649 	/*
650 	 * Get the packet from the kernel.  This will include the Ethernet
651 	 * frame header, MAC address, etc.
652 	 */
653 	IFQ_DEQUEUE(&ifp->if_snd, m);
654 
655 	/*
656 	 * Push the packet out to the card.
657 	 */
658 	for (top = m; m != NULL; m = m->m_next) {
659 		/* Words... */
660 		if (m->m_len > 1)
661 			bus_space_write_multi_stream_2(bst, bsh, DATA_REG_W,
662 			    mtod(m, u_int16_t *), m->m_len >> 1);
663 
664 		/* ...and the remaining byte, if any. */
665 		if (m->m_len & 1)
666 			bus_space_write_1(bst, bsh, DATA_REG_B,
667 			  *(u_int8_t *)(mtod(m, u_int8_t *) + (m->m_len - 1)));
668 	}
669 
670 #ifdef SMC91CXX_SW_PAD
671 	/*
672 	 * Push out padding.
673 	 */
674 	while (pad > 1) {
675 		bus_space_write_2(bst, bsh, DATA_REG_W, 0);
676 		pad -= 2;
677 	}
678 	if (pad)
679 		bus_space_write_1(bst, bsh, DATA_REG_B, 0);
680 #endif
681 
682 	/*
683 	 * Push out control byte and unused packet byte.  The control byte
684 	 * is 0, meaning the packet is even lengthed and no special
685 	 * CRC handling is necessary.
686 	 */
687 	bus_space_write_2(bst, bsh, DATA_REG_W, 0);
688 
689 	/*
690 	 * Enable transmit interrupts and let the chip go.  Set a watchdog
691 	 * in case we miss the interrupt.
692 	 */
693 	bus_space_write_1(bst, bsh, INTR_MASK_REG_B,
694 	    bus_space_read_1(bst, bsh, INTR_MASK_REG_B) |
695 	    IM_TX_INT | IM_TX_EMPTY_INT);
696 
697 	bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_ENQUEUE);
698 
699 	ifp->if_timer = 5;
700 
701 #if NBPFILTER > 0
702 	/* Hand off a copy to the bpf. */
703 	if (ifp->if_bpf)
704 		bpf_mtap(ifp->if_bpf, top, BPF_DIRECTION_OUT);
705 #endif
706 
707 	ifp->if_opackets++;
708 	m_freem(top);
709 
710  readcheck:
711 	/*
712 	 * Check for incoming pcakets.  We don't want to overflow the small
713 	 * RX FIFO.  If nothing has arrived, attempt to queue another
714 	 * transmit packet.
715 	 */
716 	if (bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W) & FIFO_REMPTY)
717 		goto again;
718 }
719 
720 /*
721  * Interrupt service routine.
722  */
723 int
724 smc91cxx_intr(arg)
725 	void *arg;
726 {
727 	struct smc91cxx_softc *sc = arg;
728 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
729 	bus_space_tag_t bst = sc->sc_bst;
730 	bus_space_handle_t bsh = sc->sc_bsh;
731 	u_int8_t mask, interrupts, status;
732 	u_int16_t packetno, tx_status, card_stats;
733 
734 	if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0 ||
735 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
736 		return (0);
737 
738 	SMC_SELECT_BANK(sc, 2);
739 
740 	/*
741 	 * Obtain the current interrupt mask.
742 	 */
743 	mask = bus_space_read_1(bst, bsh, INTR_MASK_REG_B);
744 
745 	/*
746 	 * Get the set of interrupt which occurred and eliminate any
747 	 * which are not enabled.
748 	 */
749 	interrupts = bus_space_read_1(bst, bsh, INTR_STAT_REG_B);
750 	status = interrupts & mask;
751 
752 	/* Ours? */
753 	if (status == 0)
754 		return (0);
755 
756 	/*
757 	 * It's ours; disable all interrupts while we process them.
758 	 */
759 	bus_space_write_1(bst, bsh, INTR_MASK_REG_B, 0);
760 
761 	/*
762 	 * Receive overrun interrupts.
763 	 */
764 	if (status & IM_RX_OVRN_INT) {
765 		bus_space_write_1(bst, bsh, INTR_ACK_REG_B, IM_RX_OVRN_INT);
766 		ifp->if_ierrors++;
767 	}
768 
769 	/*
770 	 * Receive interrupts.
771 	 */
772 	if (status & IM_RCV_INT) {
773 #if 1 /* DIAGNOSTIC */
774 		packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W);
775 		if (packetno & FIFO_REMPTY) {
776 			printf("%s: receive interrupt on empty fifo\n",
777 			    sc->sc_dev.dv_xname);
778 			goto out;
779 		} else
780 #endif
781 		smc91cxx_read(sc);
782 	}
783 
784 	/*
785 	 * Memory allocation interrupts.
786 	 */
787 	if (status & IM_ALLOC_INT) {
788 		/* Disable this interrupt. */
789 		mask &= ~IM_ALLOC_INT;
790 
791 		/*
792 		 * Release the just-allocated memory.  We will reallocate
793 		 * it through the normal start logic.
794 		 */
795 		while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
796 			/* XXX bound this loop! */ ;
797 		bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_FREEPKT);
798 
799 		ifp->if_flags &= ~IFF_OACTIVE;
800 		ifp->if_timer = 0;
801 	}
802 
803 	/*
804 	 * Transmit complete interrupt.  Handle transmission error messages.
805 	 * This will only be called on error condition because of AUTO RELEASE
806 	 * mode.
807 	 */
808 	if (status & IM_TX_INT) {
809 		bus_space_write_1(bst, bsh, INTR_ACK_REG_B, IM_TX_INT);
810 
811 		packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W) &
812 		    FIFO_TX_MASK;
813 
814 		/*
815 		 * Select this as the packet to read from.
816 		 */
817 		bus_space_write_1(bst, bsh, PACKET_NUM_REG_B, packetno);
818 
819 		/*
820 		 * Position the pointer to the beginning of the packet.
821 		 */
822 		bus_space_write_2(bst, bsh, POINTER_REG_W,
823 		    PTR_AUTOINC | PTR_READ /* | 0x0000 */);
824 
825 		/*
826 		 * Fetch the TX status word.  This will be a copy of
827 		 * the EPH_STATUS_REG_W at the time of the transmission
828 		 * failure.
829 		 */
830 		tx_status = bus_space_read_2(bst, bsh, DATA_REG_W);
831 
832 		if (tx_status & EPHSR_TX_SUC)
833 			printf("%s: successful packet caused TX interrupt?!\n",
834 			    sc->sc_dev.dv_xname);
835 		else
836 			ifp->if_oerrors++;
837 
838 		if (tx_status & EPHSR_LATCOL)
839 			ifp->if_collisions++;
840 
841 		/*
842 		 * Some of these errors disable the transmitter; reenable it.
843 		 */
844 		SMC_SELECT_BANK(sc, 0);
845 #ifdef SMC91CXX_SW_PAD
846 		bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, TCR_ENABLE);
847 #else
848 		bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W,
849 		    TCR_ENABLE | TCR_PAD_ENABLE);
850 #endif
851 
852 		/*
853 		 * Kill the failed packet and wait for the MMU to unbusy.
854 		 */
855 		SMC_SELECT_BANK(sc, 2);
856 		while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
857 			/* XXX bound this loop! */ ;
858 		bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_FREEPKT);
859 
860 		ifp->if_timer = 0;
861 	}
862 
863 	/*
864 	 * Transmit underrun interrupts.  We use this opportunity to
865 	 * update transmit statistics from the card.
866 	 */
867 	if (status & IM_TX_EMPTY_INT) {
868 		bus_space_write_1(bst, bsh, INTR_ACK_REG_B, IM_TX_EMPTY_INT);
869 
870 		/* Disable this interrupt. */
871 		mask &= ~IM_TX_EMPTY_INT;
872 
873 		SMC_SELECT_BANK(sc, 0);
874 		card_stats = bus_space_read_2(bst, bsh, COUNTER_REG_W);
875 
876 		/* Single collisions. */
877 		ifp->if_collisions += card_stats & ECR_COLN_MASK;
878 
879 		/* Multiple collisions. */
880 		ifp->if_collisions += (card_stats & ECR_MCOLN_MASK) >> 4;
881 
882 		SMC_SELECT_BANK(sc, 2);
883 
884 		ifp->if_timer = 0;
885 	}
886 
887 	/*
888 	 * Other errors.  Reset the interface.
889 	 */
890 	if (status & IM_EPH_INT) {
891 		smc91cxx_stop(sc);
892 		smc91cxx_init(sc);
893 	}
894 
895 	/*
896 	 * Attempt to queue more packets for transmission.
897 	 */
898 	smc91cxx_start(ifp);
899 
900 out:
901 	/*
902 	 * Reenable the interrupts we wish to receive now that processing
903 	 * is complete.
904 	 */
905 	mask |= bus_space_read_1(bst, bsh, INTR_MASK_REG_B);
906 	bus_space_write_1(bst, bsh, INTR_MASK_REG_B, mask);
907 
908 	return (1);
909 }
910 
911 /*
912  * Read a packet from the card and pass it up to the kernel.
913  * NOTE!  WE EXPECT TO BE IN REGISTER WINDOW 2!
914  */
915 void
916 smc91cxx_read(sc)
917 	struct smc91cxx_softc *sc;
918 {
919 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
920 	bus_space_tag_t bst = sc->sc_bst;
921 	bus_space_handle_t bsh = sc->sc_bsh;
922 	struct mbuf *m;
923 	u_int16_t status, packetno, packetlen;
924 	u_int8_t *data;
925 
926  again:
927 	/*
928 	 * Set data pointer to the beginning of the packet.  Since
929 	 * PTR_RCV is set, the packet number will be found automatically
930 	 * in FIFO_PORTS_REG_W, FIFO_RX_MASK.
931 	 */
932 	bus_space_write_2(bst, bsh, POINTER_REG_W,
933 	    PTR_READ | PTR_RCV | PTR_AUTOINC /* | 0x0000 */);
934 
935 	/*
936 	 * First two words are status and packet length.
937 	 */
938 	status = bus_space_read_2(bst, bsh, DATA_REG_W);
939 	packetlen = bus_space_read_2(bst, bsh, DATA_REG_W);
940 
941 	/*
942 	 * The packet length includes 3 extra words: status, length,
943 	 * and an extra word that includes the control byte.
944 	 */
945 	packetlen -= 6;
946 
947 	/*
948 	 * Account for receive errors and discard.
949 	 */
950 	if (status & RS_ERRORS) {
951 		ifp->if_ierrors++;
952 		goto out;
953 	}
954 
955 	/*
956 	 * Adjust for odd-length packet.
957 	 */
958 	if (status & RS_ODDFRAME)
959 		packetlen++;
960 
961 	/*
962 	 * Allocate a header mbuf.
963 	 */
964 	MGETHDR(m, M_DONTWAIT, MT_DATA);
965 	if (m == NULL)
966 		goto out;
967 	m->m_pkthdr.rcvif = ifp;
968 	m->m_pkthdr.len = packetlen;
969 
970 	/*
971 	 * Always put the packet in a cluster.
972 	 * XXX should chain small mbufs if less than threshold.
973 	 */
974 	MCLGET(m, M_DONTWAIT);
975 	if ((m->m_flags & M_EXT) == 0) {
976 		m_freem(m);
977 		ifp->if_ierrors++;
978 		printf("%s: can't allocate cluster for incoming packet\n",
979 		    sc->sc_dev.dv_xname);
980 		goto out;
981 	}
982 
983 	/*
984 	 * Pull the packet off the interface.  Make sure the payload
985 	 * is aligned.
986 	 */
987 	m->m_data = (caddr_t) ALIGN(mtod(m, caddr_t) +
988 	    sizeof(struct ether_header)) - sizeof(struct ether_header);
989 
990 	data = mtod(m, u_int8_t *);
991 	if (packetlen > 1)
992 		bus_space_read_multi_stream_2(bst, bsh, DATA_REG_W,
993 		    (u_int16_t *)data, packetlen >> 1);
994 	if (packetlen & 1) {
995 		data += packetlen & ~1;
996 		*data = bus_space_read_1(bst, bsh, DATA_REG_B);
997 	}
998 
999 	ifp->if_ipackets++;
1000 
1001 #if NBPFILTER > 0
1002 	/*
1003 	 * Hand the packet off to bpf listeners.  If there's a bpf listener,
1004 	 * we need to check if the packet is ours.
1005 	 */
1006 	if (ifp->if_bpf)
1007 		bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN);
1008 #endif
1009 
1010 	m->m_pkthdr.len = m->m_len = packetlen;
1011 	ether_input_mbuf(ifp, m);
1012 
1013  out:
1014 	/*
1015 	 * Tell the card to free the memory occupied by this packet.
1016 	 */
1017 	while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
1018 		/* XXX bound this loop! */ ;
1019 	bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_RELEASE);
1020 
1021 	/*
1022 	 * Check for another packet.
1023 	 */
1024 	packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W);
1025 	if (packetno & FIFO_REMPTY)
1026 		return;
1027 	goto again;
1028 }
1029 
1030 /*
1031  * Process an ioctl request.
1032  */
1033 int
1034 smc91cxx_ioctl(ifp, cmd, data)
1035 	struct ifnet *ifp;
1036 	u_long cmd;
1037 	caddr_t data;
1038 {
1039 	struct smc91cxx_softc *sc = ifp->if_softc;
1040 	struct ifaddr *ifa = (struct ifaddr *)data;
1041 	struct ifreq *ifr = (struct ifreq *)data;
1042 	int s, error = 0;
1043 
1044 	s = splnet();
1045 
1046 	switch (cmd) {
1047 	case SIOCSIFADDR:
1048 		if ((error = smc91cxx_enable(sc)) != 0)
1049 			break;
1050 		ifp->if_flags |= IFF_UP;
1051 		switch (ifa->ifa_addr->sa_family) {
1052 #ifdef INET
1053 		case AF_INET:
1054 			smc91cxx_init(sc);
1055 			arp_ifinit(&sc->sc_arpcom, ifa);
1056 			break;
1057 #endif
1058 		default:
1059 			smc91cxx_init(sc);
1060 			break;
1061 		}
1062 		break;
1063 
1064 	case SIOCSIFFLAGS:
1065 		if ((ifp->if_flags & IFF_UP) == 0 &&
1066 		    (ifp->if_flags & IFF_RUNNING) != 0) {
1067 			/*
1068 			 * If interface is marked down and it is running,
1069 			 * stop it.
1070 			 */
1071 			smc91cxx_stop(sc);
1072 			ifp->if_flags &= ~IFF_RUNNING;
1073 			smc91cxx_disable(sc);
1074 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
1075 			   (ifp->if_flags & IFF_RUNNING) == 0) {
1076 			/*
1077 			 * If interface is marked up and it is stopped,
1078 			 * start it.
1079 			 */
1080 			if ((error = smc91cxx_enable(sc)) != 0)
1081 				break;
1082 			smc91cxx_init(sc);
1083 		} else if ((ifp->if_flags & IFF_UP) != 0) {
1084 			/*
1085 			 * Reset the interface to pick up changes in any
1086 			 * other flags that affect hardware registers.
1087 			 */
1088 			smc91cxx_reset(sc);
1089 		}
1090 		break;
1091 
1092 	case SIOCGIFMEDIA:
1093 	case SIOCSIFMEDIA:
1094 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1095 		break;
1096 
1097 	default:
1098 		error = ether_ioctl(ifp, &sc->sc_arpcom, cmd, data);
1099 	}
1100 
1101 	if (error == ENETRESET) {
1102 		if (ifp->if_flags & IFF_RUNNING)
1103 			smc91cxx_reset(sc);
1104 		error = 0;
1105 	}
1106 
1107 	splx(s);
1108 	return (error);
1109 }
1110 
1111 /*
1112  * Reset the interface.
1113  */
1114 void
1115 smc91cxx_reset(sc)
1116 	struct smc91cxx_softc *sc;
1117 {
1118 	int s;
1119 
1120 	s = splnet();
1121 	smc91cxx_stop(sc);
1122 	smc91cxx_init(sc);
1123 	splx(s);
1124 }
1125 
1126 /*
1127  * Watchdog timer.
1128  */
1129 void
1130 smc91cxx_watchdog(ifp)
1131 	struct ifnet *ifp;
1132 {
1133 	struct smc91cxx_softc *sc = ifp->if_softc;
1134 
1135 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
1136 	++sc->sc_arpcom.ac_if.if_oerrors;
1137 
1138 	smc91cxx_reset(sc);
1139 }
1140 
1141 /*
1142  * Stop output on the interface.
1143  */
1144 void
1145 smc91cxx_stop(sc)
1146 	struct smc91cxx_softc *sc;
1147 {
1148 	bus_space_tag_t bst = sc->sc_bst;
1149 	bus_space_handle_t bsh = sc->sc_bsh;
1150 
1151 	/*
1152 	 * Clear interrupt mask; disable all interrupts.
1153 	 */
1154 	SMC_SELECT_BANK(sc, 2);
1155 	bus_space_write_1(bst, bsh, INTR_MASK_REG_B, 0);
1156 
1157 	/*
1158 	 * Disable transmitter and receiver.
1159 	 */
1160 	SMC_SELECT_BANK(sc, 0);
1161 	bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, 0);
1162 	bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, 0);
1163 
1164 	/*
1165 	 * Cancel watchdog timer.
1166 	 */
1167 	sc->sc_arpcom.ac_if.if_timer = 0;
1168 }
1169 
1170 /*
1171  * Enable power on the interface.
1172  */
1173 int
1174 smc91cxx_enable(sc)
1175 	struct smc91cxx_softc *sc;
1176 {
1177 	if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0 && sc->sc_enable != NULL) {
1178 		if ((*sc->sc_enable)(sc) != 0) {
1179 			printf("%s: device enable failed\n",
1180 			    sc->sc_dev.dv_xname);
1181 			return (EIO);
1182 		}
1183 	}
1184 
1185 	sc->sc_flags |= SMC_FLAGS_ENABLED;
1186 	return (0);
1187 }
1188 
1189 /*
1190  * Disable power on the interface.
1191  */
1192 void
1193 smc91cxx_disable(sc)
1194 	struct smc91cxx_softc *sc;
1195 {
1196 	if ((sc->sc_flags & SMC_FLAGS_ENABLED) != 0 && sc->sc_disable != NULL) {
1197 		(*sc->sc_disable)(sc);
1198 		sc->sc_flags &= ~SMC_FLAGS_ENABLED;
1199 	}
1200 }
1201 
1202 int
1203 smc91cxx_activate(self, act)
1204 	struct device *self;
1205 	enum devact act;
1206 {
1207 #if 0
1208 	struct smc91cxx_softc *sc = (struct smc91cxx_softc *)self;
1209 #endif
1210 	int rv = 0, s;
1211 
1212 	s = splnet();
1213 	switch (act) {
1214 	case DVACT_ACTIVATE:
1215 		break;
1216 
1217 	case DVACT_DEACTIVATE:
1218 #if 0
1219 		if_deactivate(&sc->sc_ic.ic_if);
1220 #endif
1221 		break;
1222 	}
1223 	splx(s);
1224 	return(rv);
1225 }
1226 
1227 int
1228 smc91cxx_detach(self, flags)
1229 	struct device *self;
1230 	int flags;
1231 {
1232 	struct smc91cxx_softc *sc = (struct smc91cxx_softc *)self;
1233 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1234 
1235 	/* Succeed now if there's no work to do. */
1236 	if ((sc->sc_flags & SMC_FLAGS_ATTACHED) == 0)
1237 		return(0);
1238 
1239 	/* smc91cxx_disable() checks SMC_FLAGS_ENABLED */
1240 	smc91cxx_disable(sc);
1241 
1242 	/* smc91cxx_attach() never fails */
1243 
1244 	/* Delete all media. */
1245 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
1246 
1247 #if NBPFILTER > 0
1248 	bpfdetach(ifp);
1249 #endif
1250 	ether_ifdetach(ifp);
1251 	if_detach(ifp);
1252 
1253 	return (0);
1254 }
1255 
1256 u_int32_t
1257 smc91cxx_mii_bitbang_read(self)
1258 	struct device *self;
1259 {
1260 	struct smc91cxx_softc *sc = (void *) self;
1261 
1262 	/* We're already in bank 3. */
1263 	return (bus_space_read_2(sc->sc_bst, sc->sc_bsh, MGMT_REG_W));
1264 }
1265 
1266 void
1267 smc91cxx_mii_bitbang_write(self, val)
1268 	struct device *self;
1269 	u_int32_t val;
1270 {
1271 	struct smc91cxx_softc *sc = (void *) self;
1272 
1273 	/* We're already in bank 3. */
1274 	bus_space_write_2(sc->sc_bst, sc->sc_bsh, MGMT_REG_W, val);
1275 }
1276 
1277 int
1278 smc91cxx_mii_readreg(self, phy, reg)
1279 	struct device *self;
1280 	int phy, reg;
1281 {
1282 	struct smc91cxx_softc *sc = (void *) self;
1283 	int val;
1284 
1285 	SMC_SELECT_BANK(sc, 3);
1286 
1287 	val = mii_bitbang_readreg(self, &smc91cxx_mii_bitbang_ops, phy, reg);
1288 
1289 	SMC_SELECT_BANK(sc, 2);
1290 
1291 	return (val);
1292 }
1293 
1294 void
1295 smc91cxx_mii_writereg(self, phy, reg, val)
1296 	struct device *self;
1297 	int phy, reg, val;
1298 {
1299 	struct smc91cxx_softc *sc = (void *) self;
1300 
1301 	SMC_SELECT_BANK(sc, 3);
1302 
1303 	mii_bitbang_writereg(self, &smc91cxx_mii_bitbang_ops, phy, reg, val);
1304 
1305 	SMC_SELECT_BANK(sc, 2);
1306 }
1307 
1308 void
1309 smc91cxx_statchg(self)
1310 	struct device *self;
1311 {
1312 	struct smc91cxx_softc *sc = (struct smc91cxx_softc *)self;
1313 	bus_space_tag_t bst = sc->sc_bst;
1314 	bus_space_handle_t bsh = sc->sc_bsh;
1315 	int mctl;
1316 
1317 	SMC_SELECT_BANK(sc, 0);
1318 	mctl = bus_space_read_2(bst, bsh, TXMIT_CONTROL_REG_W);
1319 	if (sc->sc_mii.mii_media_active & IFM_FDX)
1320 		mctl |= TCR_SWFDUP;
1321 	else
1322 		mctl &= ~TCR_SWFDUP;
1323 	bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, mctl);
1324 	SMC_SELECT_BANK(sc, 2); /* back to operating window */
1325 }
1326 
1327 /*
1328  * One second timer, used to tick the MII.
1329  */
1330 void
1331 smc91cxx_tick(arg)
1332 	void *arg;
1333 {
1334 	struct smc91cxx_softc *sc = arg;
1335 	int s;
1336 
1337 #ifdef DIAGNOSTIC
1338 	if ((sc->sc_flags & SMC_FLAGS_HAS_MII) == 0)
1339 		panic("smc91cxx_tick");
1340 #endif
1341 
1342 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1343 		return;
1344 
1345 	s = splnet();
1346 	mii_tick(&sc->sc_mii);
1347 	splx(s);
1348 
1349 	timeout_add_sec(&sc->sc_mii_timeout, 1);
1350 }
1351