smc91cxx.c revision 1.18 1 /* $NetBSD: smc91cxx.c,v 1.18 1999/09/10 00:23:34 itojun 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 "opt_inet.h"
81 #include "opt_ccitt.h"
82 #include "opt_llc.h"
83 #include "opt_ns.h"
84 #include "bpfilter.h"
85 #include "rnd.h"
86
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/mbuf.h>
90 #include <sys/syslog.h>
91 #include <sys/socket.h>
92 #include <sys/device.h>
93 #include <sys/malloc.h>
94 #include <sys/ioctl.h>
95 #include <sys/errno.h>
96 #if NRND > 0
97 #include <sys/rnd.h>
98 #endif
99
100 #include <machine/bus.h>
101 #include <machine/intr.h>
102
103 #include <net/if.h>
104 #include <net/if_dl.h>
105 #include <net/if_ether.h>
106 #include <net/if_media.h>
107
108 #ifdef INET
109 #include <netinet/in.h>
110 #include <netinet/if_inarp.h>
111 #include <netinet/in_systm.h>
112 #include <netinet/in_var.h>
113 #include <netinet/ip.h>
114 #endif
115
116 #ifdef NS
117 #include <netns/ns.h>
118 #include <netns/ns_if.h>
119 #endif
120
121 #if defined(CCITT) && defined(LLC)
122 #include <sys/socketvar.h>
123 #include <netccitt/x25.h>
124 #include <netccitt/pk.h>
125 #include <netccitt/pk_var.h>
126 #include <netccitt/pk_extern.h>
127 #endif
128
129 #if NBPFILTER > 0
130 #include <net/bpf.h>
131 #include <net/bpfdesc.h>
132 #endif
133
134 #include <dev/ic/smc91cxxreg.h>
135 #include <dev/ic/smc91cxxvar.h>
136
137 /* XXX Hardware padding doesn't work yet(?) */
138 #define SMC91CXX_SW_PAD
139
140 const char *smc91cxx_idstrs[] = {
141 NULL, /* 0 */
142 NULL, /* 1 */
143 NULL, /* 2 */
144 "SMC91C90/91C92", /* 3 */
145 "SMC91C94", /* 4 */
146 "SMC91C95", /* 5 */
147 NULL, /* 6 */
148 "SMC91C100", /* 7 */
149 NULL, /* 8 */
150 NULL, /* 9 */
151 NULL, /* 10 */
152 NULL, /* 11 */
153 NULL, /* 12 */
154 NULL, /* 13 */
155 NULL, /* 14 */
156 NULL, /* 15 */
157 };
158
159 /* Supported media types. */
160 const int smc91cxx_media[] = {
161 IFM_ETHER|IFM_10_T,
162 IFM_ETHER|IFM_10_5,
163 };
164 #define NSMC91CxxMEDIA (sizeof(smc91cxx_media) / sizeof(smc91cxx_media[0]))
165
166 int smc91cxx_mediachange __P((struct ifnet *));
167 void smc91cxx_mediastatus __P((struct ifnet *, struct ifmediareq *));
168
169 int smc91cxx_set_media __P((struct smc91cxx_softc *, int));
170
171 void smc91cxx_init __P((struct smc91cxx_softc *));
172 void smc91cxx_read __P((struct smc91cxx_softc *));
173 void smc91cxx_reset __P((struct smc91cxx_softc *));
174 void smc91cxx_start __P((struct ifnet *));
175 void smc91cxx_resume __P((struct smc91cxx_softc *));
176 void smc91cxx_stop __P((struct smc91cxx_softc *));
177 void smc91cxx_watchdog __P((struct ifnet *));
178 int smc91cxx_ioctl __P((struct ifnet *, u_long, caddr_t));
179
180 static __inline int ether_cmp __P((void *, void *));
181 static __inline int
182 ether_cmp(va, vb)
183 void *va, *vb;
184 {
185 u_int8_t *a = va;
186 u_int8_t *b = vb;
187
188 return ((a[5] != b[5]) || (a[4] != b[4]) || (a[3] != b[3]) ||
189 (a[2] != b[2]) || (a[1] != b[1]) || (a[0] != b[0]));
190 }
191
192 void
193 smc91cxx_attach(sc, myea)
194 struct smc91cxx_softc *sc;
195 u_int8_t *myea;
196 {
197 struct ifnet *ifp = &sc->sc_ec.ec_if;
198 bus_space_tag_t bst = sc->sc_bst;
199 bus_space_handle_t bsh = sc->sc_bsh;
200 const char *idstr;
201 u_int16_t tmp;
202 u_int8_t enaddr[ETHER_ADDR_LEN];
203 int i, aui;
204
205 /* Make sure the chip is stopped. */
206 smc91cxx_stop(sc);
207
208 SMC_SELECT_BANK(sc, 3);
209 tmp = bus_space_read_2(bst, bsh, REVISION_REG_W);
210 idstr = smc91cxx_idstrs[RR_ID(tmp)];
211 printf("%s: ", sc->sc_dev.dv_xname);
212 if (idstr != NULL)
213 printf("%s, ", idstr);
214 else
215 printf("unknown chip id %d, ", RR_ID(tmp));
216 printf("revision %d\n", RR_REV(tmp));
217
218 /* Read the station address from the chip. */
219 SMC_SELECT_BANK(sc, 1);
220 if (myea == NULL) {
221 myea = enaddr;
222 for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
223 tmp = bus_space_read_2(bst, bsh, IAR_ADDR0_REG_W + i);
224 myea[i + 1] = (tmp >> 8) & 0xff;
225 myea[i] = tmp & 0xff;
226 }
227 }
228 printf("%s: MAC address %s, ", sc->sc_dev.dv_xname,
229 ether_sprintf(myea));
230
231 /* ..and default media. */
232 tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W);
233 printf("default media %s\n", (aui = (tmp & CR_AUI_SELECT)) ?
234 "AUI" : "UTP");
235
236 /* Initialize the ifnet structure. */
237 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
238 ifp->if_softc = sc;
239 ifp->if_start = smc91cxx_start;
240 ifp->if_ioctl = smc91cxx_ioctl;
241 ifp->if_watchdog = smc91cxx_watchdog;
242 ifp->if_flags =
243 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
244
245 /* Attach the interface. */
246 if_attach(ifp);
247 ether_ifattach(ifp, myea);
248
249 /* Initialize the media structures. */
250 ifmedia_init(&sc->sc_media, 0, smc91cxx_mediachange,
251 smc91cxx_mediastatus);
252 for (i = 0; i < NSMC91CxxMEDIA; i++)
253 ifmedia_add(&sc->sc_media, smc91cxx_media[i], 0, NULL);
254 ifmedia_set(&sc->sc_media, IFM_ETHER | (aui ? IFM_10_5 : IFM_10_T));
255
256 #if NBPFILTER > 0
257 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
258 #endif
259
260 #if NRND > 0
261 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
262 RND_TYPE_NET, 0);
263 #endif
264 }
265
266 /*
267 * Change media according to request.
268 */
269 int
270 smc91cxx_mediachange(ifp)
271 struct ifnet *ifp;
272 {
273 struct smc91cxx_softc *sc = ifp->if_softc;
274
275 return (smc91cxx_set_media(sc, sc->sc_media.ifm_media));
276 }
277
278 int
279 smc91cxx_set_media(sc, media)
280 struct smc91cxx_softc *sc;
281 int media;
282 {
283 bus_space_tag_t bst = sc->sc_bst;
284 bus_space_handle_t bsh = sc->sc_bsh;
285 u_int16_t tmp;
286
287 /*
288 * If the interface is not currently powered on, just return.
289 * When it is enabled later, smc91cxx_init() will properly set
290 * up the media for us.
291 */
292 if (sc->sc_enabled == 0)
293 return (0);
294
295 if (IFM_TYPE(media) != IFM_ETHER)
296 return (EINVAL);
297
298 switch (IFM_SUBTYPE(media)) {
299 case IFM_10_T:
300 case IFM_10_5:
301 SMC_SELECT_BANK(sc, 1);
302 tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W);
303 if (IFM_SUBTYPE(media) == IFM_10_5)
304 tmp |= CR_AUI_SELECT;
305 else
306 tmp &= ~CR_AUI_SELECT;
307 bus_space_write_2(bst, bsh, CONFIG_REG_W, tmp);
308 delay(20000); /* XXX is this needed? */
309 break;
310
311 default:
312 return (EINVAL);
313 }
314
315 return (0);
316 }
317
318 /*
319 * Notify the world which media we're using.
320 */
321 void
322 smc91cxx_mediastatus(ifp, ifmr)
323 struct ifnet *ifp;
324 struct ifmediareq *ifmr;
325 {
326 struct smc91cxx_softc *sc = ifp->if_softc;
327 bus_space_tag_t bst = sc->sc_bst;
328 bus_space_handle_t bsh = sc->sc_bsh;
329 u_int16_t tmp;
330
331 if (sc->sc_enabled == 0) {
332 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
333 ifmr->ifm_status = 0;
334 return;
335 }
336
337 SMC_SELECT_BANK(sc, 1);
338 tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W);
339 ifmr->ifm_active =
340 IFM_ETHER | ((tmp & CR_AUI_SELECT) ? IFM_10_5 : IFM_10_T);
341 }
342
343 /*
344 * Reset and initialize the chip.
345 */
346 void
347 smc91cxx_init(sc)
348 struct smc91cxx_softc *sc;
349 {
350 struct ifnet *ifp = &sc->sc_ec.ec_if;
351 bus_space_tag_t bst = sc->sc_bst;
352 bus_space_handle_t bsh = sc->sc_bsh;
353 u_int16_t tmp;
354 u_int8_t *enaddr;
355 int s, i;
356
357 s = splnet();
358
359 /*
360 * This resets the registersmostly to defaults, but doesn't
361 * affect the EEPROM. After the reset cycle, we pause briefly
362 * for the chip to recover.
363 *
364 * XXX how long are we really supposed to delay? --thorpej
365 */
366 SMC_SELECT_BANK(sc, 0);
367 bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, RCR_SOFTRESET);
368 delay(100);
369 bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, 0);
370 delay(200);
371
372 bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, 0);
373
374 /* Set the Ethernet address. */
375 SMC_SELECT_BANK(sc, 1);
376 enaddr = (u_int8_t *)LLADDR(ifp->if_sadl);
377 for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
378 tmp = enaddr[i + 1] << 8 | enaddr[i];
379 bus_space_write_2(bst, bsh, IAR_ADDR0_REG_W + i, tmp);
380 }
381
382 /*
383 * Set the control register to automatically release successfully
384 * transmitted packets (making the best use of our limited memory)
385 * and enable the EPH interrupt on certain TX errors.
386 */
387 bus_space_write_2(bst, bsh, CONTROL_REG_W, (CTR_AUTO_RELEASE |
388 CTR_TE_ENABLE | CTR_CR_ENABLE | CTR_LE_ENABLE));
389
390 /*
391 * Reset the MMU and wait for it to be un-busy.
392 */
393 SMC_SELECT_BANK(sc, 2);
394 bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_RESET);
395 while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
396 /* XXX bound this loop! */ ;
397
398 /*
399 * Disable all interrupts.
400 */
401 bus_space_write_1(bst, bsh, INTR_MASK_REG_B, 0);
402
403 /*
404 * Set current media.
405 */
406 smc91cxx_set_media(sc, sc->sc_media.ifm_cur->ifm_media);
407
408 /*
409 * Set the receive filter. We want receive enable and auto
410 * strip of CRC from received packet. If we are in promisc. mode,
411 * then set that bit as well.
412 *
413 * XXX Initialize multicast filter. For now, we just accept
414 * XXX all multicast.
415 */
416 SMC_SELECT_BANK(sc, 0);
417
418 tmp = RCR_ENABLE | RCR_STRIP_CRC | RCR_ALMUL;
419 if (ifp->if_flags & IFF_PROMISC)
420 tmp |= RCR_PROMISC;
421
422 bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, tmp);
423
424 /*
425 * Set transmitter control to "enabled".
426 */
427 tmp = TCR_ENABLE;
428
429 #ifndef SMC91CXX_SW_PAD
430 /*
431 * Enable hardware padding of transmitted packets.
432 * XXX doesn't work?
433 */
434 tmp |= TCR_PAD_ENABLE;
435 #endif
436
437 bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, tmp);
438
439 /*
440 * Now, enable interrupts.
441 */
442 SMC_SELECT_BANK(sc, 2);
443
444 bus_space_write_1(bst, bsh, INTR_MASK_REG_B,
445 IM_EPH_INT | IM_RX_OVRN_INT | IM_RCV_INT | IM_TX_INT);
446
447 /* Interface is now running, with no output active. */
448 ifp->if_flags |= IFF_RUNNING;
449 ifp->if_flags &= ~IFF_OACTIVE;
450
451 /*
452 * Attempt to start any pending transmission.
453 */
454 smc91cxx_start(ifp);
455
456 splx(s);
457 }
458
459 /*
460 * Start output on an interface.
461 * Must be called at splnet or interrupt level.
462 */
463 void
464 smc91cxx_start(ifp)
465 struct ifnet *ifp;
466 {
467 struct smc91cxx_softc *sc = ifp->if_softc;
468 bus_space_tag_t bst = sc->sc_bst;
469 bus_space_handle_t bsh = sc->sc_bsh;
470 u_int len;
471 struct mbuf *m, *top;
472 u_int16_t length, npages;
473 u_int8_t packetno;
474 int timo, pad;
475
476 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
477 return;
478
479 again:
480 /*
481 * Peek at the next packet.
482 */
483 if ((m = ifp->if_snd.ifq_head) == NULL)
484 return;
485
486 /*
487 * Compute the frame length and set pad to give an overall even
488 * number of bytes. Below, we assume that the packet length
489 * is even.
490 */
491 for (len = 0, top = m; m != NULL; m = m->m_next)
492 len += m->m_len;
493 pad = (len & 1);
494
495 /*
496 * We drop packets that are too large. Perhaps we should
497 * truncate them instead?
498 */
499 if ((len + pad) > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
500 printf("%s: large packet discarded\n", sc->sc_dev.dv_xname);
501 ifp->if_oerrors++;
502 IF_DEQUEUE(&ifp->if_snd, m);
503 m_freem(m);
504 goto readcheck;
505 }
506
507 #ifdef SMC91CXX_SW_PAD
508 /*
509 * Not using hardware padding; pad to ETHER_MIN_LEN.
510 */
511 if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
512 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
513 #endif
514
515 length = pad + len;
516
517 /*
518 * The MMU has a 256 byte page size. The MMU expects us to
519 * ask for "npages - 1". We include space for the status word,
520 * byte count, and control bytes in the allocation request.
521 */
522 npages = (length + 6) >> 8;
523
524 /*
525 * Now allocate the memory.
526 */
527 SMC_SELECT_BANK(sc, 2);
528 bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_ALLOC | npages);
529
530 timo = MEMORY_WAIT_TIME;
531 do {
532 if (bus_space_read_1(bst, bsh, INTR_STAT_REG_B) & IM_ALLOC_INT)
533 break;
534 delay(1);
535 } while (--timo);
536
537 packetno = bus_space_read_1(bst, bsh, ALLOC_RESULT_REG_B);
538
539 if (packetno & ARR_FAILED || timo == 0) {
540 /*
541 * No transmit memory is available. Record the number
542 * of requestd pages and enable the allocation completion
543 * interrupt. Set up the watchdog timer in case we miss
544 * the interrupt. Mark the interface as active so that
545 * no one else attempts to transmit while we're allocating
546 * memory.
547 */
548 bus_space_write_1(bst, bsh, INTR_MASK_REG_B,
549 bus_space_read_1(bst, bsh, INTR_MASK_REG_B) | IM_ALLOC_INT);
550
551 ifp->if_timer = 5;
552 ifp->if_flags |= IFF_OACTIVE;
553
554 return;
555 }
556
557 /*
558 * We have a packet number - set the data window.
559 */
560 bus_space_write_1(bst, bsh, PACKET_NUM_REG_B, packetno);
561
562 /*
563 * Point to the beginning of the packet.
564 */
565 bus_space_write_2(bst, bsh, POINTER_REG_W, PTR_AUTOINC /* | 0x0000 */);
566
567 /*
568 * Send the packet length (+6 for stats, length, and control bytes)
569 * and the status word (set to zeros).
570 */
571 bus_space_write_2(bst, bsh, DATA_REG_W, 0);
572 bus_space_write_1(bst, bsh, DATA_REG_B, (length + 6) & 0xff);
573 bus_space_write_1(bst, bsh, DATA_REG_B, ((length + 6) >> 8) & 0xff);
574
575 /*
576 * Get the packet from the kernel. This will include the Ethernet
577 * frame header, MAC address, etc.
578 */
579 IF_DEQUEUE(&ifp->if_snd, m);
580
581 /*
582 * Push the packet out to the card.
583 */
584 for (top = m; m != NULL; m = m->m_next) {
585 /* Words... */
586 bus_space_write_multi_2(bst, bsh, DATA_REG_W,
587 mtod(m, u_int16_t *), m->m_len >> 1);
588
589 /* ...and the remaining byte, if any. */
590 if (m->m_len & 1)
591 bus_space_write_1(bst, bsh, DATA_REG_B,
592 *(u_int8_t *)(mtod(m, u_int8_t *) + (m->m_len - 1)));
593 }
594
595 #ifdef SMC91CXX_SW_PAD
596 /*
597 * Push out padding.
598 */
599 while (pad > 1) {
600 bus_space_write_2(bst, bsh, DATA_REG_W, 0);
601 pad -= 2;
602 }
603 if (pad)
604 bus_space_write_1(bst, bsh, DATA_REG_B, 0);
605 #endif
606
607 /*
608 * Push out control byte and unused packet byte. The control byte
609 * is 0, meaning the packet is even lengthed and no special
610 * CRC handling is necessary.
611 */
612 bus_space_write_2(bst, bsh, DATA_REG_W, 0);
613
614 /*
615 * Enable transmit interrupts and let the chip go. Set a watchdog
616 * in case we miss the interrupt.
617 */
618 bus_space_write_1(bst, bsh, INTR_MASK_REG_B,
619 bus_space_read_1(bst, bsh, INTR_MASK_REG_B) |
620 IM_TX_INT | IM_TX_EMPTY_INT);
621
622 bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_ENQUEUE);
623
624 ifp->if_timer = 5;
625
626 #if NBPFILTER > 0
627 /* Hand off a copy to the bpf. */
628 if (ifp->if_bpf)
629 bpf_mtap(ifp->if_bpf, top);
630 #endif
631
632 ifp->if_opackets++;
633 m_freem(top);
634
635 readcheck:
636 /*
637 * Check for incoming pcakets. We don't want to overflow the small
638 * RX FIFO. If nothing has arrived, attempt to queue another
639 * transmit packet.
640 */
641 if (bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W) & FIFO_REMPTY)
642 goto again;
643 }
644
645 /*
646 * Interrupt service routine.
647 */
648 int
649 smc91cxx_intr(arg)
650 void *arg;
651 {
652 struct smc91cxx_softc *sc = arg;
653 struct ifnet *ifp = &sc->sc_ec.ec_if;
654 bus_space_tag_t bst = sc->sc_bst;
655 bus_space_handle_t bsh = sc->sc_bsh;
656 u_int8_t mask, interrupts, status;
657 u_int16_t packetno, tx_status, card_stats;
658
659 if (sc->sc_enabled == 0)
660 return (0);
661
662 SMC_SELECT_BANK(sc, 2);
663
664 /*
665 * Obtain the current interrupt mask.
666 */
667 mask = bus_space_read_1(bst, bsh, INTR_MASK_REG_B);
668
669 /*
670 * Get the set of interrupt which occurred and eliminate any
671 * which are not enabled.
672 */
673 interrupts = bus_space_read_1(bst, bsh, INTR_STAT_REG_B);
674 status = interrupts & mask;
675
676 /* Ours? */
677 if (status == 0)
678 return (0);
679
680 /*
681 * It's ours; disable all interrupts while we process them.
682 */
683 bus_space_write_1(bst, bsh, INTR_MASK_REG_B, 0);
684
685 /*
686 * Receive overrun interrupts.
687 */
688 if (status & IM_RX_OVRN_INT) {
689 bus_space_write_1(bst, bsh, INTR_ACK_REG_B, IM_RX_OVRN_INT);
690 ifp->if_ierrors++;
691 }
692
693 /*
694 * Receive interrupts.
695 */
696 if (status & IM_RCV_INT) {
697 #if 1 /* DIAGNOSTIC */
698 packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W);
699 if (packetno & FIFO_REMPTY)
700 printf("%s: receive interrupt on empty fifo\n",
701 sc->sc_dev.dv_xname);
702 else
703 #endif
704 smc91cxx_read(sc);
705 }
706
707 /*
708 * Memory allocation interrupts.
709 */
710 if (status & IM_ALLOC_INT) {
711 /* Disable this interrupt. */
712 mask &= ~IM_ALLOC_INT;
713
714 /*
715 * Release the just-allocated memory. We will reallocate
716 * it through the normal start logic.
717 */
718 while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
719 /* XXX bound this loop! */ ;
720 bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_FREEPKT);
721
722 ifp->if_flags &= ~IFF_OACTIVE;
723 ifp->if_timer = 0;
724 }
725
726 /*
727 * Transmit complete interrupt. Handle transmission error messages.
728 * This will only be called on error condition because of AUTO RELEASE
729 * mode.
730 */
731 if (status & IM_TX_INT) {
732 bus_space_write_1(bst, bsh, INTR_ACK_REG_B, IM_TX_INT);
733
734 packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W) &
735 FIFO_TX_MASK;
736
737 /*
738 * Select this as the packet to read from.
739 */
740 bus_space_write_1(bst, bsh, PACKET_NUM_REG_B, packetno);
741
742 /*
743 * Position the pointer to the beginning of the packet.
744 */
745 bus_space_write_2(bst, bsh, POINTER_REG_W,
746 PTR_AUTOINC | PTR_READ /* | 0x0000 */);
747
748 /*
749 * Fetch the TX status word. This will be a copy of
750 * the EPH_STATUS_REG_W at the time of the transmission
751 * failure.
752 */
753 tx_status = bus_space_read_2(bst, bsh, DATA_REG_W);
754
755 if (tx_status & EPHSR_TX_SUC)
756 printf("%s: successful packet caused TX interrupt?!\n",
757 sc->sc_dev.dv_xname);
758 else
759 ifp->if_oerrors++;
760
761 if (tx_status & EPHSR_LATCOL)
762 ifp->if_collisions++;
763
764 /*
765 * Some of these errors disable the transmitter; reenable it.
766 */
767 SMC_SELECT_BANK(sc, 0);
768 #ifdef SMC91CXX_SW_PAD
769 bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, TCR_ENABLE);
770 #else
771 bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W,
772 TCR_ENABLE | TCR_PAD_ENABLE);
773 #endif
774
775 /*
776 * Kill the failed packet and wait for the MMU to unbusy.
777 */
778 SMC_SELECT_BANK(sc, 2);
779 while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
780 /* XXX bound this loop! */ ;
781 bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_FREEPKT);
782
783 ifp->if_timer = 0;
784 }
785
786 /*
787 * Transmit underrun interrupts. We use this opportunity to
788 * update transmit statistics from the card.
789 */
790 if (status & IM_TX_EMPTY_INT) {
791 bus_space_write_1(bst, bsh, INTR_ACK_REG_B, IM_TX_EMPTY_INT);
792
793 /* Disable this interrupt. */
794 mask &= ~IM_TX_EMPTY_INT;
795
796 SMC_SELECT_BANK(sc, 0);
797 card_stats = bus_space_read_2(bst, bsh, COUNTER_REG_W);
798
799 /* Single collisions. */
800 ifp->if_collisions += card_stats & ECR_COLN_MASK;
801
802 /* Multiple collisions. */
803 ifp->if_collisions += (card_stats & ECR_MCOLN_MASK) >> 4;
804
805 SMC_SELECT_BANK(sc, 2);
806
807 ifp->if_timer = 0;
808 }
809
810 /*
811 * Other errors. Reset the interface.
812 */
813 if (status & IM_EPH_INT) {
814 smc91cxx_stop(sc);
815 smc91cxx_init(sc);
816 }
817
818 /*
819 * Attempt to queue more packets for transmission.
820 */
821 smc91cxx_start(ifp);
822
823 /*
824 * Reenable the interrupts we wish to receive now that processing
825 * is complete.
826 */
827 mask |= bus_space_read_1(bst, bsh, INTR_MASK_REG_B);
828 bus_space_write_1(bst, bsh, INTR_MASK_REG_B, mask);
829
830 #if NRND > 0
831 if (status)
832 rnd_add_uint32(&sc->rnd_source, status);
833 #endif
834
835 return (1);
836 }
837
838 /*
839 * Read a packet from the card and pass it up to the kernel.
840 * NOTE! WE EXPECT TO BE IN REGISTER WINDOW 2!
841 */
842 void
843 smc91cxx_read(sc)
844 struct smc91cxx_softc *sc;
845 {
846 struct ifnet *ifp = &sc->sc_ec.ec_if;
847 bus_space_tag_t bst = sc->sc_bst;
848 bus_space_handle_t bsh = sc->sc_bsh;
849 struct ether_header *eh;
850 struct mbuf *m;
851 u_int16_t status, packetno, packetlen;
852 u_int8_t *data;
853
854 again:
855 /*
856 * Set data pointer to the beginning of the packet. Since
857 * PTR_RCV is set, the packet number will be found automatically
858 * in FIFO_PORTS_REG_W, FIFO_RX_MASK.
859 */
860 bus_space_write_2(bst, bsh, POINTER_REG_W,
861 PTR_READ | PTR_RCV | PTR_AUTOINC /* | 0x0000 */);
862
863 /*
864 * First two words are status and packet length.
865 */
866 status = bus_space_read_2(bst, bsh, DATA_REG_W);
867 packetlen = bus_space_read_2(bst, bsh, DATA_REG_W);
868
869 /*
870 * The packet length includes 3 extra words: status, length,
871 * and an extra word that includes the control byte.
872 */
873 packetlen -= 6;
874
875 /*
876 * Account for receive errors and discard.
877 */
878 if (status & RS_ERRORS) {
879 ifp->if_ierrors++;
880 goto out;
881 }
882
883 /*
884 * Adjust for odd-length packet.
885 */
886 if (status & RS_ODDFRAME)
887 packetlen++;
888
889 /*
890 * Allocate a header mbuf.
891 */
892 MGETHDR(m, M_DONTWAIT, MT_DATA);
893 if (m == NULL)
894 goto out;
895
896 m->m_pkthdr.rcvif = ifp;
897 m->m_pkthdr.len = m->m_len = packetlen;
898
899 /*
900 * Always put the packet in a cluster.
901 * XXX should chain small mbufs if less than threshold.
902 */
903 MCLGET(m, M_DONTWAIT);
904 if ((m->m_flags & M_EXT) == 0) {
905 m_freem(m);
906 ifp->if_ierrors++;
907 printf("%s: can't allocate cluster for incoming packet\n",
908 sc->sc_dev.dv_xname);
909 goto out;
910 }
911
912 /*
913 * Pull the packet off the interface.
914 */
915 eh = mtod(m, struct ether_header *);
916 data = mtod(m, u_int8_t *);
917 bus_space_read_multi_2(bst, bsh, DATA_REG_W, (u_int16_t *)data,
918 packetlen >> 1);
919 if (packetlen & 1) {
920 data += packetlen & ~1;
921 *data = bus_space_read_1(bst, bsh, DATA_REG_B);
922 }
923
924 ifp->if_ipackets++;
925
926 #if NBPFILTER > 0
927 /*
928 * Hand the packet off to bpf listeners. If there's a bpf listener,
929 * we need to check if the packet is ours.
930 */
931 if (ifp->if_bpf)
932 bpf_mtap(ifp->if_bpf, m);
933 #endif
934
935 if ((ifp->if_flags & IFF_PROMISC) != 0) {
936 /*
937 * If this is unicast and not for me, drop it.
938 */
939 if ((eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
940 ether_cmp(eh->ether_dhost, LLADDR(ifp->if_sadl)) != 0) {
941 m_freem(m);
942 goto out;
943 }
944
945 /*
946 * Make sure to behave as IFF_SIMPLEX in all cases.
947 * Drop multicast/broadcast packet looped back from myself.
948 *
949 * This is to cope with SMC91C92 (Megahertz XJ10BT), which
950 * loops back multicast packet to itself on promiscuous mode.
951 * (should be ensured by chipset configuration)
952 */
953 if ((eh->ether_dhost[0] & 1) == 1 && /* mcast || bcast */
954 ether_cmp(eh->ether_shost, LLADDR(ifp->if_sadl)) == 0) {
955 m_freem(m);
956 goto out;
957 }
958 }
959
960 m->m_pkthdr.len = m->m_len = packetlen;
961 (*ifp->if_input)(ifp, m);
962
963 out:
964 /*
965 * Tell the card to free the memory occupied by this packet.
966 */
967 while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
968 /* XXX bound this loop! */ ;
969 bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_RELEASE);
970
971 /*
972 * Check for another packet.
973 */
974 packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W);
975 if (packetno & FIFO_REMPTY)
976 return;
977 goto again;
978 }
979
980 /*
981 * Process an ioctl request.
982 */
983 int
984 smc91cxx_ioctl(ifp, cmd, data)
985 struct ifnet *ifp;
986 u_long cmd;
987 caddr_t data;
988 {
989 struct smc91cxx_softc *sc = ifp->if_softc;
990 struct ifaddr *ifa = (struct ifaddr *)data;
991 struct ifreq *ifr = (struct ifreq *)data;
992 int s, error = 0;
993
994 s = splnet();
995
996 switch (cmd) {
997 case SIOCSIFADDR:
998 if ((error = smc91cxx_enable(sc)) != 0)
999 break;
1000 ifp->if_flags |= IFF_UP;
1001 switch (ifa->ifa_addr->sa_family) {
1002 #ifdef INET
1003 case AF_INET:
1004 smc91cxx_init(sc);
1005 arp_ifinit(ifp, ifa);
1006 break;
1007 #endif
1008 #ifdef NS
1009 case AF_NS:
1010 {
1011 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1012
1013 if (ns_nullhost(*ina))
1014 ina->x_host =
1015 *(union ns_host *)LLADDR(ifp->if_sadl);
1016 else {
1017 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
1018 ETHER_ADDR_LEN);
1019 }
1020
1021 /*
1022 * Set new address. Reset, because the receiver
1023 * has to be stopped before we can set the new
1024 * MAC address.
1025 */
1026 smc91cxx_reset(sc);
1027 break;
1028 }
1029 #endif
1030 default:
1031 smc91cxx_init(sc);
1032 break;
1033 }
1034 break;
1035
1036 #if defined(CCITT) && defined(LLC)
1037 case SIOCSIFCONF_X25:
1038 if ((error = smc91cxx_enable(sc)) != 0)
1039 break;
1040 ifp->if_flags |= IFF_UP;
1041 ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
1042 error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
1043 if (error == 0)
1044 smc91cxx_init(sc);
1045 break;
1046 #endif
1047
1048 case SIOCSIFFLAGS:
1049 if ((ifp->if_flags & IFF_UP) == 0 &&
1050 (ifp->if_flags & IFF_RUNNING) != 0) {
1051 /*
1052 * If interface is marked down and it is running,
1053 * stop it.
1054 */
1055 smc91cxx_stop(sc);
1056 ifp->if_flags &= ~IFF_RUNNING;
1057 smc91cxx_disable(sc);
1058 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1059 (ifp->if_flags & IFF_RUNNING) == 0) {
1060 /*
1061 * If interface is marked up and it is stopped,
1062 * start it.
1063 */
1064 if ((error = smc91cxx_enable(sc)) != 0)
1065 break;
1066 smc91cxx_init(sc);
1067 } else if ((ifp->if_flags & IFF_UP) != 0) {
1068 /*
1069 * Reset the interface to pick up changes in any
1070 * other flags that affect hardware registers.
1071 */
1072 smc91cxx_reset(sc);
1073 }
1074 break;
1075
1076 case SIOCADDMULTI:
1077 case SIOCDELMULTI:
1078 if (sc->sc_enabled == 0) {
1079 error = EIO;
1080 break;
1081 }
1082
1083 error = (cmd == SIOCADDMULTI) ?
1084 ether_addmulti(ifr, &sc->sc_ec) :
1085 ether_delmulti(ifr, &sc->sc_ec);
1086 if (error == ENETRESET) {
1087 /*
1088 * Multicast list has changed; set the hardware
1089 * filter accordingly.
1090 */
1091 smc91cxx_reset(sc);
1092 error = 0;
1093 }
1094 break;
1095
1096 case SIOCGIFMEDIA:
1097 case SIOCSIFMEDIA:
1098 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1099 break;
1100
1101 default:
1102 error = EINVAL;
1103 break;
1104 }
1105
1106 splx(s);
1107 return (error);
1108 }
1109
1110 /*
1111 * Reset the interface.
1112 */
1113 void
1114 smc91cxx_reset(sc)
1115 struct smc91cxx_softc *sc;
1116 {
1117 int s;
1118
1119 s = splnet();
1120 smc91cxx_stop(sc);
1121 smc91cxx_init(sc);
1122 splx(s);
1123 }
1124
1125 /*
1126 * Watchdog timer.
1127 */
1128 void
1129 smc91cxx_watchdog(ifp)
1130 struct ifnet *ifp;
1131 {
1132 struct smc91cxx_softc *sc = ifp->if_softc;
1133
1134 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
1135 ifp->if_oerrors++;
1136 smc91cxx_reset(sc);
1137 }
1138
1139 /*
1140 * Stop output on the interface.
1141 */
1142 void
1143 smc91cxx_stop(sc)
1144 struct smc91cxx_softc *sc;
1145 {
1146 bus_space_tag_t bst = sc->sc_bst;
1147 bus_space_handle_t bsh = sc->sc_bsh;
1148
1149 /*
1150 * Clear interrupt mask; disable all interrupts.
1151 */
1152 SMC_SELECT_BANK(sc, 2);
1153 bus_space_write_1(bst, bsh, INTR_MASK_REG_B, 0);
1154
1155 /*
1156 * Disable transmitter and receiver.
1157 */
1158 SMC_SELECT_BANK(sc, 0);
1159 bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, 0);
1160 bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, 0);
1161
1162 /*
1163 * Cancel watchdog timer.
1164 */
1165 sc->sc_ec.ec_if.if_timer = 0;
1166 }
1167
1168 /*
1169 * Enable power on the interface.
1170 */
1171 int
1172 smc91cxx_enable(sc)
1173 struct smc91cxx_softc *sc;
1174 {
1175
1176 if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1177 if ((*sc->sc_enable)(sc) != 0) {
1178 printf("%s: device enable failed\n",
1179 sc->sc_dev.dv_xname);
1180 return (EIO);
1181 }
1182 }
1183
1184 sc->sc_enabled = 1;
1185 return (0);
1186 }
1187
1188 /*
1189 * Disable power on the interface.
1190 */
1191 void
1192 smc91cxx_disable(sc)
1193 struct smc91cxx_softc *sc;
1194 {
1195
1196 if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1197 (*sc->sc_disable)(sc);
1198 sc->sc_enabled = 0;
1199 }
1200 }
1201
1202 int
1203 smc91cxx_activate(self, act)
1204 struct device *self;
1205 enum devact act;
1206 {
1207 struct smc91cxx_softc *sc = (struct smc91cxx_softc *)self;
1208 int rv = 0, s;
1209
1210 s = splnet();
1211 switch (act) {
1212 case DVACT_ACTIVATE:
1213 rv = EOPNOTSUPP;
1214 break;
1215
1216 case DVACT_DEACTIVATE:
1217 #ifdef notyet
1218 /* First, kill off the interface. */
1219 if_detach(sc->sc_ec.ec_if);
1220 #endif
1221
1222 /* Now disable the interface. */
1223 smc91cxx_disable(sc);
1224 break;
1225 }
1226 splx(s);
1227 return (rv);
1228 }
1229