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