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