]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/sn/if_sn.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / sys / dev / sn / if_sn.c
1 /*
2  * Copyright (c) 1996 Gardner Buchanan <gbuchanan@shl.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Gardner Buchanan.
16  * 4. The name of Gardner Buchanan may not be used to endorse or promote
17  *    products derived from this software without specific prior written
18  *    permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  *   $FreeBSD$
32  */
33
34 /*
35  * This is a driver for SMC's 9000 series of Ethernet adapters.
36  *
37  * This FreeBSD driver is derived from the smc9194 Linux driver by
38  * Erik Stahlman and is Copyright (C) 1996 by Erik Stahlman.
39  * This driver also shamelessly borrows from the FreeBSD ep driver
40  * which is Copyright (C) 1994 Herb Peyerl <hpeyerl@novatel.ca>
41  * All rights reserved.
42  *
43  * It is set up for my SMC91C92 equipped Ampro LittleBoard embedded
44  * PC.  It is adapted from Erik Stahlman's Linux driver which worked
45  * with his EFA Info*Express SVC VLB adaptor.  According to SMC's databook,
46  * it will work for the entire SMC 9xxx series. (Ha Ha)
47  *
48  * "Features" of the SMC chip:
49  *   4608 byte packet memory. (for the 91C92.  Others have more)
50  *   EEPROM for configuration
51  *   AUI/TP selection
52  *
53  * Authors:
54  *      Erik Stahlman                   erik@vt.edu
55  *      Herb Peyerl                     hpeyerl@novatel.ca
56  *      Andres Vega Garcia              avega@sophia.inria.fr
57  *      Serge Babkin                    babkin@hq.icb.chel.su
58  *      Gardner Buchanan                gbuchanan@shl.com
59  *
60  * Sources:
61  *    o   SMC databook
62  *    o   "smc9194.c:v0.10(FIXED) 02/15/96 by Erik Stahlman (erik@vt.edu)"
63  *    o   "if_ep.c,v 1.19 1995/01/24 20:53:45 davidg Exp"
64  *
65  * Known Bugs:
66  *    o   The hardware multicast filter isn't used yet.
67  *    o   Setting of the hardware address isn't supported.
68  *    o   Hardware padding isn't used.
69  */
70
71 /*
72  * Modifications for Megahertz X-Jack Ethernet Card (XJ-10BT)
73  * 
74  * Copyright (c) 1996 by Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>
75  *                       BSD-nomads, Tokyo, Japan.
76  */
77 /*
78  * Multicast support by Kei TANAKA <kei@pal.xerox.com>
79  * Special thanks to itojun@itojun.org
80  */
81
82 #undef  SN_DEBUG        /* (by hosokawa) */
83
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/kernel.h>
87 #include <sys/errno.h>
88 #include <sys/sockio.h>
89 #include <sys/malloc.h>
90 #include <sys/mbuf.h>
91 #include <sys/socket.h>
92 #include <sys/syslog.h>
93
94 #include <sys/module.h>
95 #include <sys/bus.h>
96
97 #include <machine/bus.h>
98 #include <machine/resource.h>
99 #include <sys/rman.h> 
100
101 #include <net/ethernet.h>
102 #include <net/if.h>
103 #include <net/if_arp.h>
104 #include <net/if_dl.h>
105 #include <net/if_types.h>
106 #include <net/if_mib.h>
107
108 #ifdef INET
109 #include <netinet/in.h>
110 #include <netinet/in_systm.h>
111 #include <netinet/in_var.h>
112 #include <netinet/ip.h>
113 #endif
114
115 #ifdef NS
116 #include <netns/ns.h>
117 #include <netns/ns_if.h>
118 #endif
119
120 #include <net/bpf.h>
121 #include <net/bpfdesc.h>
122
123 #include <machine/clock.h>
124
125 #include <dev/sn/if_snreg.h>
126 #include <dev/sn/if_snvar.h>
127
128 /* Exported variables */
129 devclass_t sn_devclass;
130
131 static int snioctl(struct ifnet * ifp, u_long, caddr_t);
132
133 static void snresume(struct ifnet *);
134
135 void sninit(void *);
136 void snread(struct ifnet *);
137 void snreset(struct sn_softc *);
138 void snstart(struct ifnet *);
139 void snstop(struct sn_softc *);
140 void snwatchdog(struct ifnet *);
141
142 static void sn_setmcast(struct sn_softc *);
143 static int sn_getmcf(struct arpcom *ac, u_char *mcf);
144 static u_int smc_crc(u_char *);
145
146 /* I (GB) have been unlucky getting the hardware padding
147  * to work properly.
148  */
149 #define SW_PAD
150
151 static const char *chip_ids[15] = {
152         NULL, NULL, NULL,
153          /* 3 */ "SMC91C90/91C92",
154          /* 4 */ "SMC91C94",
155          /* 5 */ "SMC91C95",
156         NULL,
157          /* 7 */ "SMC91C100",
158         NULL, NULL, NULL, NULL,
159         NULL, NULL, NULL
160 };
161
162 int
163 sn_attach(device_t dev)
164 {
165         struct sn_softc *sc = device_get_softc(dev);
166         struct ifnet   *ifp = &sc->arpcom.ac_if;
167         u_short         i;
168         u_char         *p;
169         struct ifaddr  *ifa;
170         struct sockaddr_dl *sdl;
171         int             rev;
172         u_short         address;
173         int             j;
174
175         sn_activate(dev);
176
177         snstop(sc);
178
179         sc->dev = dev;
180         sc->pages_wanted = -1;
181
182         device_printf(dev, " ");
183
184         SMC_SELECT_BANK(3);
185         rev = inw(BASE + REVISION_REG_W);
186         if (chip_ids[(rev >> 4) & 0xF])
187                 printf("%s ", chip_ids[(rev >> 4) & 0xF]);
188
189         SMC_SELECT_BANK(1);
190         i = inw(BASE + CONFIG_REG_W);
191         printf(i & CR_AUI_SELECT ? "AUI" : "UTP");
192
193         if (sc->pccard_enaddr)
194                 for (j = 0; j < 3; j++) {
195                         u_short w;
196
197                         w = (u_short)sc->arpcom.ac_enaddr[j * 2] | 
198                                 (((u_short)sc->arpcom.ac_enaddr[j * 2 + 1]) << 8);
199                         outw(BASE + IAR_ADDR0_REG_W + j * 2, w);
200                 }
201
202         /*
203          * Read the station address from the chip. The MAC address is bank 1,
204          * regs 4 - 9
205          */
206         SMC_SELECT_BANK(1);
207         p = (u_char *) & sc->arpcom.ac_enaddr;
208         for (i = 0; i < 6; i += 2) {
209                 address = inw(BASE + IAR_ADDR0_REG_W + i);
210                 p[i + 1] = address >> 8;
211                 p[i] = address & 0xFF;
212         }
213         printf(" MAC address %6D\n", sc->arpcom.ac_enaddr, ":");
214         ifp->if_softc = sc;
215         ifp->if_unit = device_get_unit(dev);
216         ifp->if_name = "sn";
217         ifp->if_mtu = ETHERMTU;
218         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
219         ifp->if_output = ether_output;
220         ifp->if_start = snstart;
221         ifp->if_ioctl = snioctl;
222         ifp->if_watchdog = snwatchdog;
223         ifp->if_init = sninit;
224         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
225         ifp->if_timer = 0;
226
227         if_attach(ifp);
228         ether_ifattach(ifp);
229
230         /*
231          * Fill the hardware address into ifa_addr if we find an AF_LINK
232          * entry. We need to do this so bpf's can get the hardware addr of
233          * this card. netstat likes this too!
234          */
235         ifa = TAILQ_FIRST(&ifp->if_addrhead);
236         while ((ifa != 0) && (ifa->ifa_addr != 0) &&
237                (ifa->ifa_addr->sa_family != AF_LINK))
238                 ifa = TAILQ_NEXT(ifa, ifa_link);
239
240         if ((ifa != 0) && (ifa->ifa_addr != 0)) {
241                 sdl = (struct sockaddr_dl *) ifa->ifa_addr;
242                 sdl->sdl_type = IFT_ETHER;
243                 sdl->sdl_alen = ETHER_ADDR_LEN;
244                 sdl->sdl_slen = 0;
245                 bcopy(sc->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
246         }
247
248         bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
249
250         return 0;
251 }
252
253
254 /*
255  * Reset and initialize the chip
256  */
257 void
258 sninit(void *xsc)
259 {
260         register struct sn_softc *sc = xsc;
261         register struct ifnet *ifp = &sc->arpcom.ac_if;
262         int             s;
263         int             flags;
264         int             mask;
265
266         s = splimp();
267
268         /*
269          * This resets the registers mostly to defaults, but doesn't affect
270          * EEPROM.  After the reset cycle, we pause briefly for the chip to
271          * be happy.
272          */
273         SMC_SELECT_BANK(0);
274         outw(BASE + RECV_CONTROL_REG_W, RCR_SOFTRESET);
275         SMC_DELAY();
276         outw(BASE + RECV_CONTROL_REG_W, 0x0000);
277         SMC_DELAY();
278         SMC_DELAY();
279
280         outw(BASE + TXMIT_CONTROL_REG_W, 0x0000);
281
282         /*
283          * Set the control register to automatically release succesfully
284          * transmitted packets (making the best use out of our limited
285          * memory) and to enable the EPH interrupt on certain TX errors.
286          */
287         SMC_SELECT_BANK(1);
288         outw(BASE + CONTROL_REG_W, (CTR_AUTO_RELEASE | CTR_TE_ENABLE |
289                                     CTR_CR_ENABLE | CTR_LE_ENABLE));
290
291         /* Set squelch level to 240mV (default 480mV) */
292         flags = inw(BASE + CONFIG_REG_W);
293         flags |= CR_SET_SQLCH;
294         outw(BASE + CONFIG_REG_W, flags);
295
296         /*
297          * Reset the MMU and wait for it to be un-busy.
298          */
299         SMC_SELECT_BANK(2);
300         outw(BASE + MMU_CMD_REG_W, MMUCR_RESET);
301         while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY)  /* NOTHING */
302                 ;
303
304         /*
305          * Disable all interrupts
306          */
307         outb(BASE + INTR_MASK_REG_B, 0x00);
308
309         sn_setmcast(sc);
310
311         /*
312          * Set the transmitter control.  We want it enabled.
313          */
314         flags = TCR_ENABLE;
315
316 #ifndef SW_PAD
317         /*
318          * I (GB) have been unlucky getting this to work.
319          */
320         flags |= TCR_PAD_ENABLE;
321 #endif  /* SW_PAD */
322
323         outw(BASE + TXMIT_CONTROL_REG_W, flags);
324
325
326         /*
327          * Now, enable interrupts
328          */
329         SMC_SELECT_BANK(2);
330
331         mask = IM_EPH_INT |
332                 IM_RX_OVRN_INT |
333                 IM_RCV_INT |
334                 IM_TX_INT;
335
336         outb(BASE + INTR_MASK_REG_B, mask);
337         sc->intr_mask = mask;
338         sc->pages_wanted = -1;
339
340
341         /*
342          * Mark the interface running but not active.
343          */
344         ifp->if_flags |= IFF_RUNNING;
345         ifp->if_flags &= ~IFF_OACTIVE;
346
347         /*
348          * Attempt to push out any waiting packets.
349          */
350         snstart(ifp);
351
352         splx(s);
353 }
354
355
356 void
357 snstart(struct ifnet *ifp)
358 {
359         register struct sn_softc *sc = ifp->if_softc;
360         register u_int  len;
361         register struct mbuf *m;
362         struct mbuf    *top;
363         int             s, pad;
364         int             mask;
365         u_short         length;
366         u_short         numPages;
367         u_char          packet_no;
368         int             time_out;
369
370         s = splimp();
371
372         if (sc->arpcom.ac_if.if_flags & IFF_OACTIVE) {
373                 splx(s);
374                 return;
375         }
376         if (sc->pages_wanted != -1) {
377                 splx(s);
378                 printf("sn%d: snstart() while memory allocation pending\n",
379                        ifp->if_unit);
380                 return;
381         }
382 startagain:
383
384         /*
385          * Sneak a peek at the next packet
386          */
387         m = sc->arpcom.ac_if.if_snd.ifq_head;
388         if (m == 0) {
389                 splx(s);
390                 return;
391         }
392         /*
393          * Compute the frame length and set pad to give an overall even
394          * number of bytes.  Below we assume that the packet length is even.
395          */
396         for (len = 0, top = m; m; m = m->m_next)
397                 len += m->m_len;
398
399         pad = (len & 1);
400
401         /*
402          * We drop packets that are too large. Perhaps we should truncate
403          * them instead?
404          */
405         if (len + pad > ETHER_MAX_LEN - ETHER_CRC_LEN) {
406                 printf("sn%d: large packet discarded (A)\n", ifp->if_unit);
407                 ++sc->arpcom.ac_if.if_oerrors;
408                 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
409                 m_freem(m);
410                 goto readcheck;
411         }
412 #ifdef SW_PAD
413
414         /*
415          * If HW padding is not turned on, then pad to ETHER_MIN_LEN.
416          */
417         if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
418                 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
419
420 #endif  /* SW_PAD */
421
422         length = pad + len;
423
424         /*
425          * The MMU wants the number of pages to be the number of 256 byte
426          * 'pages', minus 1 (A packet can't ever have 0 pages. We also
427          * include space for the status word, byte count and control bytes in
428          * the allocation request.
429          */
430         numPages = (length + 6) >> 8;
431
432
433         /*
434          * Now, try to allocate the memory
435          */
436         SMC_SELECT_BANK(2);
437         outw(BASE + MMU_CMD_REG_W, MMUCR_ALLOC | numPages);
438
439         /*
440          * Wait a short amount of time to see if the allocation request
441          * completes.  Otherwise, I enable the interrupt and wait for
442          * completion asyncronously.
443          */
444
445         time_out = MEMORY_WAIT_TIME;
446         do {
447                 if (inb(BASE + INTR_STAT_REG_B) & IM_ALLOC_INT)
448                         break;
449         } while (--time_out);
450
451         if (!time_out) {
452
453                 /*
454                  * No memory now.  Oh well, wait until the chip finds memory
455                  * later.   Remember how many pages we were asking for and
456                  * enable the allocation completion interrupt. Also set a
457                  * watchdog in case  we miss the interrupt. We mark the
458                  * interface active since there is no point in attempting an
459                  * snstart() until after the memory is available.
460                  */
461                 mask = inb(BASE + INTR_MASK_REG_B) | IM_ALLOC_INT;
462                 outb(BASE + INTR_MASK_REG_B, mask);
463                 sc->intr_mask = mask;
464
465                 sc->arpcom.ac_if.if_timer = 1;
466                 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
467                 sc->pages_wanted = numPages;
468
469                 splx(s);
470                 return;
471         }
472         /*
473          * The memory allocation completed.  Check the results.
474          */
475         packet_no = inb(BASE + ALLOC_RESULT_REG_B);
476         if (packet_no & ARR_FAILED) {
477                 printf("sn%d: Memory allocation failed\n", ifp->if_unit);
478                 goto startagain;
479         }
480         /*
481          * We have a packet number, so tell the card to use it.
482          */
483         outb(BASE + PACKET_NUM_REG_B, packet_no);
484
485         /*
486          * Point to the beginning of the packet
487          */
488         outw(BASE + POINTER_REG_W, PTR_AUTOINC | 0x0000);
489
490         /*
491          * Send the packet length (+6 for status, length and control byte)
492          * and the status word (set to zeros)
493          */
494         outw(BASE + DATA_REG_W, 0);
495         outb(BASE + DATA_REG_B, (length + 6) & 0xFF);
496         outb(BASE + DATA_REG_B, (length + 6) >> 8);
497
498         /*
499          * Get the packet from the kernel.  This will include the Ethernet
500          * frame header, MAC Addresses etc.
501          */
502         IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
503
504         /*
505          * Push out the data to the card.
506          */
507         for (top = m; m != 0; m = m->m_next) {
508
509                 /*
510                  * Push out words.
511                  */
512                 outsw(BASE + DATA_REG_W, mtod(m, caddr_t), m->m_len / 2);
513
514                 /*
515                  * Push out remaining byte.
516                  */
517                 if (m->m_len & 1)
518                         outb(BASE + DATA_REG_B, *(mtod(m, caddr_t) + m->m_len - 1));
519         }
520
521         /*
522          * Push out padding.
523          */
524         while (pad > 1) {
525                 outw(BASE + DATA_REG_W, 0);
526                 pad -= 2;
527         }
528         if (pad)
529                 outb(BASE + DATA_REG_B, 0);
530
531         /*
532          * Push out control byte and unused packet byte The control byte is 0
533          * meaning the packet is even lengthed and no special CRC handling is
534          * desired.
535          */
536         outw(BASE + DATA_REG_W, 0);
537
538         /*
539          * Enable the interrupts and let the chipset deal with it Also set a
540          * watchdog in case we miss the interrupt.
541          */
542         mask = inb(BASE + INTR_MASK_REG_B) | (IM_TX_INT | IM_TX_EMPTY_INT);
543         outb(BASE + INTR_MASK_REG_B, mask);
544         sc->intr_mask = mask;
545
546         outw(BASE + MMU_CMD_REG_W, MMUCR_ENQUEUE);
547
548         sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
549         sc->arpcom.ac_if.if_timer = 1;
550
551         if (ifp->if_bpf) {
552                 bpf_mtap(ifp, top);
553         }
554
555         sc->arpcom.ac_if.if_opackets++;
556         m_freem(top);
557
558
559 readcheck:
560
561         /*
562          * Is another packet coming in?  We don't want to overflow the tiny
563          * RX FIFO.  If nothing has arrived then attempt to queue another
564          * transmit packet.
565          */
566         if (inw(BASE + FIFO_PORTS_REG_W) & FIFO_REMPTY)
567                 goto startagain;
568
569         splx(s);
570         return;
571 }
572
573
574
575 /* Resume a packet transmit operation after a memory allocation
576  * has completed.
577  *
578  * This is basically a hacked up copy of snstart() which handles
579  * a completed memory allocation the same way snstart() does.
580  * It then passes control to snstart to handle any other queued
581  * packets.
582  */
583 static void
584 snresume(struct ifnet *ifp)
585 {
586         register struct sn_softc *sc = ifp->if_softc;
587         register u_int  len;
588         register struct mbuf *m;
589         struct mbuf    *top;
590         int             pad;
591         int             mask;
592         u_short         length;
593         u_short         numPages;
594         u_short         pages_wanted;
595         u_char          packet_no;
596
597         if (sc->pages_wanted < 0)
598                 return;
599
600         pages_wanted = sc->pages_wanted;
601         sc->pages_wanted = -1;
602
603         /*
604          * Sneak a peek at the next packet
605          */
606         m = sc->arpcom.ac_if.if_snd.ifq_head;
607         if (m == 0) {
608                 printf("sn%d: snresume() with nothing to send\n", ifp->if_unit);
609                 return;
610         }
611         /*
612          * Compute the frame length and set pad to give an overall even
613          * number of bytes.  Below we assume that the packet length is even.
614          */
615         for (len = 0, top = m; m; m = m->m_next)
616                 len += m->m_len;
617
618         pad = (len & 1);
619
620         /*
621          * We drop packets that are too large. Perhaps we should truncate
622          * them instead?
623          */
624         if (len + pad > ETHER_MAX_LEN - ETHER_CRC_LEN) {
625                 printf("sn%d: large packet discarded (B)\n", ifp->if_unit);
626                 ++sc->arpcom.ac_if.if_oerrors;
627                 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
628                 m_freem(m);
629                 return;
630         }
631 #ifdef SW_PAD
632
633         /*
634          * If HW padding is not turned on, then pad to ETHER_MIN_LEN.
635          */
636         if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
637                 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
638
639 #endif  /* SW_PAD */
640
641         length = pad + len;
642
643
644         /*
645          * The MMU wants the number of pages to be the number of 256 byte
646          * 'pages', minus 1 (A packet can't ever have 0 pages. We also
647          * include space for the status word, byte count and control bytes in
648          * the allocation request.
649          */
650         numPages = (length + 6) >> 8;
651
652
653         SMC_SELECT_BANK(2);
654
655         /*
656          * The memory allocation completed.  Check the results. If it failed,
657          * we simply set a watchdog timer and hope for the best.
658          */
659         packet_no = inb(BASE + ALLOC_RESULT_REG_B);
660         if (packet_no & ARR_FAILED) {
661                 printf("sn%d: Memory allocation failed.  Weird.\n", ifp->if_unit);
662                 sc->arpcom.ac_if.if_timer = 1;
663                 goto try_start;
664         }
665         /*
666          * We have a packet number, so tell the card to use it.
667          */
668         outb(BASE + PACKET_NUM_REG_B, packet_no);
669
670         /*
671          * Now, numPages should match the pages_wanted recorded when the
672          * memory allocation was initiated.
673          */
674         if (pages_wanted != numPages) {
675                 printf("sn%d: memory allocation wrong size.  Weird.\n", ifp->if_unit);
676                 /*
677                  * If the allocation was the wrong size we simply release the
678                  * memory once it is granted. Wait for the MMU to be un-busy.
679                  */
680                 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY)  /* NOTHING */
681                         ;
682                 outw(BASE + MMU_CMD_REG_W, MMUCR_FREEPKT);
683
684                 return;
685         }
686         /*
687          * Point to the beginning of the packet
688          */
689         outw(BASE + POINTER_REG_W, PTR_AUTOINC | 0x0000);
690
691         /*
692          * Send the packet length (+6 for status, length and control byte)
693          * and the status word (set to zeros)
694          */
695         outw(BASE + DATA_REG_W, 0);
696         outb(BASE + DATA_REG_B, (length + 6) & 0xFF);
697         outb(BASE + DATA_REG_B, (length + 6) >> 8);
698
699         /*
700          * Get the packet from the kernel.  This will include the Ethernet
701          * frame header, MAC Addresses etc.
702          */
703         IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
704
705         /*
706          * Push out the data to the card.
707          */
708         for (top = m; m != 0; m = m->m_next) {
709
710                 /*
711                  * Push out words.
712                  */
713                 outsw(BASE + DATA_REG_W, mtod(m, caddr_t), m->m_len / 2);
714
715                 /*
716                  * Push out remaining byte.
717                  */
718                 if (m->m_len & 1)
719                         outb(BASE + DATA_REG_B, *(mtod(m, caddr_t) + m->m_len - 1));
720         }
721
722         /*
723          * Push out padding.
724          */
725         while (pad > 1) {
726                 outw(BASE + DATA_REG_W, 0);
727                 pad -= 2;
728         }
729         if (pad)
730                 outb(BASE + DATA_REG_B, 0);
731
732         /*
733          * Push out control byte and unused packet byte The control byte is 0
734          * meaning the packet is even lengthed and no special CRC handling is
735          * desired.
736          */
737         outw(BASE + DATA_REG_W, 0);
738
739         /*
740          * Enable the interrupts and let the chipset deal with it Also set a
741          * watchdog in case we miss the interrupt.
742          */
743         mask = inb(BASE + INTR_MASK_REG_B) | (IM_TX_INT | IM_TX_EMPTY_INT);
744         outb(BASE + INTR_MASK_REG_B, mask);
745         sc->intr_mask = mask;
746         outw(BASE + MMU_CMD_REG_W, MMUCR_ENQUEUE);
747
748         if (ifp->if_bpf) {
749                 bpf_mtap(ifp, top);
750         }
751
752         sc->arpcom.ac_if.if_opackets++;
753         m_freem(top);
754
755 try_start:
756
757         /*
758          * Now pass control to snstart() to queue any additional packets
759          */
760         sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
761         snstart(ifp);
762
763         /*
764          * We've sent something, so we're active.  Set a watchdog in case the
765          * TX_EMPTY interrupt is lost.
766          */
767         sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
768         sc->arpcom.ac_if.if_timer = 1;
769
770         return;
771 }
772
773
774 void
775 sn_intr(void *arg)
776 {
777         int             status, interrupts;
778         register struct sn_softc *sc = (struct sn_softc *) arg;
779         struct ifnet   *ifp = &sc->arpcom.ac_if;
780         int             x;
781
782         /*
783          * Chip state registers
784          */
785         u_char          mask;
786         u_char          packet_no;
787         u_short         tx_status;
788         u_short         card_stats;
789
790         /*
791          * if_ep.c did this, so I do too.  Yet if_ed.c doesn't. I wonder...
792          */
793         x = splbio();
794
795         /*
796          * Clear the watchdog.
797          */
798         ifp->if_timer = 0;
799
800         SMC_SELECT_BANK(2);
801
802         /*
803          * Obtain the current interrupt mask and clear the hardware mask
804          * while servicing interrupts.
805          */
806         mask = inb(BASE + INTR_MASK_REG_B);
807         outb(BASE + INTR_MASK_REG_B, 0x00);
808
809         /*
810          * Get the set of interrupts which occurred and eliminate any which
811          * are masked.
812          */
813         interrupts = inb(BASE + INTR_STAT_REG_B);
814         status = interrupts & mask;
815
816         /*
817          * Now, process each of the interrupt types.
818          */
819
820         /*
821          * Receive Overrun.
822          */
823         if (status & IM_RX_OVRN_INT) {
824
825                 /*
826                  * Acknowlege Interrupt
827                  */
828                 SMC_SELECT_BANK(2);
829                 outb(BASE + INTR_ACK_REG_B, IM_RX_OVRN_INT);
830
831                 ++sc->arpcom.ac_if.if_ierrors;
832         }
833         /*
834          * Got a packet.
835          */
836         if (status & IM_RCV_INT) {
837 #if 1
838                 int             packet_number;
839
840                 SMC_SELECT_BANK(2);
841                 packet_number = inw(BASE + FIFO_PORTS_REG_W);
842
843                 if (packet_number & FIFO_REMPTY) {
844
845                         /*
846                          * we got called , but nothing was on the FIFO
847                          */
848                         printf("sn: Receive interrupt with nothing on FIFO\n");
849
850                         goto out;
851                 }
852 #endif
853                 snread(ifp);
854         }
855         /*
856          * An on-card memory allocation came through.
857          */
858         if (status & IM_ALLOC_INT) {
859
860                 /*
861                  * Disable this interrupt.
862                  */
863                 mask &= ~IM_ALLOC_INT;
864                 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
865                 snresume(&sc->arpcom.ac_if);
866         }
867         /*
868          * TX Completion.  Handle a transmit error message. This will only be
869          * called when there is an error, because of the AUTO_RELEASE mode.
870          */
871         if (status & IM_TX_INT) {
872
873                 /*
874                  * Acknowlege Interrupt
875                  */
876                 SMC_SELECT_BANK(2);
877                 outb(BASE + INTR_ACK_REG_B, IM_TX_INT);
878
879                 packet_no = inw(BASE + FIFO_PORTS_REG_W);
880                 packet_no &= FIFO_TX_MASK;
881
882                 /*
883                  * select this as the packet to read from
884                  */
885                 outb(BASE + PACKET_NUM_REG_B, packet_no);
886
887                 /*
888                  * Position the pointer to the first word from this packet
889                  */
890                 outw(BASE + POINTER_REG_W, PTR_AUTOINC | PTR_READ | 0x0000);
891
892                 /*
893                  * Fetch the TX status word.  The value found here will be a
894                  * copy of the EPH_STATUS_REG_W at the time the transmit
895                  * failed.
896                  */
897                 tx_status = inw(BASE + DATA_REG_W);
898
899                 if (tx_status & EPHSR_TX_SUC) {
900                         device_printf(sc->dev, 
901                             "Successful packet caused interrupt\n");
902                 } else {
903                         ++sc->arpcom.ac_if.if_oerrors;
904                 }
905
906                 if (tx_status & EPHSR_LATCOL)
907                         ++sc->arpcom.ac_if.if_collisions;
908
909                 /*
910                  * Some of these errors will have disabled transmit.
911                  * Re-enable transmit now.
912                  */
913                 SMC_SELECT_BANK(0);
914
915 #ifdef SW_PAD
916                 outw(BASE + TXMIT_CONTROL_REG_W, TCR_ENABLE);
917 #else
918                 outw(BASE + TXMIT_CONTROL_REG_W, TCR_ENABLE | TCR_PAD_ENABLE);
919 #endif  /* SW_PAD */
920
921                 /*
922                  * kill the failed packet. Wait for the MMU to be un-busy.
923                  */
924                 SMC_SELECT_BANK(2);
925                 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY)  /* NOTHING */
926                         ;
927                 outw(BASE + MMU_CMD_REG_W, MMUCR_FREEPKT);
928
929                 /*
930                  * Attempt to queue more transmits.
931                  */
932                 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
933                 snstart(&sc->arpcom.ac_if);
934         }
935         /*
936          * Transmit underrun.  We use this opportunity to update transmit
937          * statistics from the card.
938          */
939         if (status & IM_TX_EMPTY_INT) {
940
941                 /*
942                  * Acknowlege Interrupt
943                  */
944                 SMC_SELECT_BANK(2);
945                 outb(BASE + INTR_ACK_REG_B, IM_TX_EMPTY_INT);
946
947                 /*
948                  * Disable this interrupt.
949                  */
950                 mask &= ~IM_TX_EMPTY_INT;
951
952                 SMC_SELECT_BANK(0);
953                 card_stats = inw(BASE + COUNTER_REG_W);
954
955                 /*
956                  * Single collisions
957                  */
958                 sc->arpcom.ac_if.if_collisions += card_stats & ECR_COLN_MASK;
959
960                 /*
961                  * Multiple collisions
962                  */
963                 sc->arpcom.ac_if.if_collisions += (card_stats & ECR_MCOLN_MASK) >> 4;
964
965                 SMC_SELECT_BANK(2);
966
967                 /*
968                  * Attempt to enqueue some more stuff.
969                  */
970                 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
971                 snstart(&sc->arpcom.ac_if);
972         }
973         /*
974          * Some other error.  Try to fix it by resetting the adapter.
975          */
976         if (status & IM_EPH_INT) {
977                 snstop(sc);
978                 sninit(sc);
979         }
980
981 out:
982         /*
983          * Handled all interrupt sources.
984          */
985
986         SMC_SELECT_BANK(2);
987
988         /*
989          * Reestablish interrupts from mask which have not been deselected
990          * during this interrupt.  Note that the hardware mask, which was set
991          * to 0x00 at the start of this service routine, may have been
992          * updated by one or more of the interrupt handers and we must let
993          * those new interrupts stay enabled here.
994          */
995         mask |= inb(BASE + INTR_MASK_REG_B);
996         outb(BASE + INTR_MASK_REG_B, mask);
997         sc->intr_mask = mask;
998
999         splx(x);
1000 }
1001
1002 void
1003 snread(register struct ifnet *ifp)
1004 {
1005         struct sn_softc *sc = ifp->if_softc;
1006         struct ether_header *eh;
1007         struct mbuf    *m;
1008         short           status;
1009         int             packet_number;
1010         u_short         packet_length;
1011         u_char         *data;
1012
1013         SMC_SELECT_BANK(2);
1014 #if 0
1015         packet_number = inw(BASE + FIFO_PORTS_REG_W);
1016
1017         if (packet_number & FIFO_REMPTY) {
1018
1019                 /*
1020                  * we got called , but nothing was on the FIFO
1021                  */
1022                 printf("sn: Receive interrupt with nothing on FIFO\n");
1023                 return;
1024         }
1025 #endif
1026 read_another:
1027
1028         /*
1029          * Start reading from the start of the packet. Since PTR_RCV is set,
1030          * packet number is found in FIFO_PORTS_REG_W, FIFO_RX_MASK.
1031          */
1032         outw(BASE + POINTER_REG_W, PTR_READ | PTR_RCV | PTR_AUTOINC | 0x0000);
1033
1034         /*
1035          * First two words are status and packet_length
1036          */
1037         status = inw(BASE + DATA_REG_W);
1038         packet_length = inw(BASE + DATA_REG_W) & RLEN_MASK;
1039
1040         /*
1041          * The packet length contains 3 extra words: status, length, and a
1042          * extra word with the control byte.
1043          */
1044         packet_length -= 6;
1045
1046         /*
1047          * Account for receive errors and discard.
1048          */
1049         if (status & RS_ERRORS) {
1050                 ++sc->arpcom.ac_if.if_ierrors;
1051                 goto out;
1052         }
1053         /*
1054          * A packet is received.
1055          */
1056
1057         /*
1058          * Adjust for odd-length packet.
1059          */
1060         if (status & RS_ODDFRAME)
1061                 packet_length++;
1062
1063         /*
1064          * Allocate a header mbuf from the kernel.
1065          */
1066         MGETHDR(m, M_DONTWAIT, MT_DATA);
1067         if (m == NULL)
1068                 goto out;
1069
1070         m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
1071         m->m_pkthdr.len = m->m_len = packet_length;
1072
1073         /*
1074          * Attach an mbuf cluster
1075          */
1076         MCLGET(m, M_DONTWAIT);
1077
1078         /*
1079          * Insist on getting a cluster
1080          */
1081         if ((m->m_flags & M_EXT) == 0) {
1082                 m_freem(m);
1083                 ++sc->arpcom.ac_if.if_ierrors;
1084                 printf("sn: snread() kernel memory allocation problem\n");
1085                 goto out;
1086         }
1087         eh = mtod(m, struct ether_header *);
1088
1089         /*
1090          * Get packet, including link layer address, from interface.
1091          */
1092
1093         data = (u_char *) eh;
1094         insw(BASE + DATA_REG_W, data, packet_length >> 1);
1095         if (packet_length & 1) {
1096                 data += packet_length & ~1;
1097                 *data = inb(BASE + DATA_REG_B);
1098         }
1099         ++sc->arpcom.ac_if.if_ipackets;
1100
1101         if (sc->arpcom.ac_if.if_bpf)
1102         {
1103                 bpf_mtap(&sc->arpcom.ac_if, m);
1104
1105                 /*
1106                  * Note that the interface cannot be in promiscuous mode if
1107                  * there are no BPF listeners.  And if we are in promiscuous
1108                  * mode, we have to check if this packet is really ours.
1109                  */
1110                 if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC) &&
1111                     (eh->ether_dhost[0] & 1) == 0 &&
1112                     bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
1113                          sizeof(eh->ether_dhost)) != 0 &&
1114                     bcmp(eh->ether_dhost, etherbroadcastaddr,
1115                          sizeof(eh->ether_dhost)) != 0) {
1116                         m_freem(m);
1117                         goto out;
1118                 }
1119         }
1120
1121         /*
1122          * Remove link layer addresses and whatnot.
1123          */
1124         m->m_pkthdr.len = m->m_len = packet_length - sizeof(struct ether_header);
1125         m->m_data += sizeof(struct ether_header);
1126
1127         ether_input(&sc->arpcom.ac_if, eh, m);
1128
1129 out:
1130
1131         /*
1132          * Error or good, tell the card to get rid of this packet Wait for
1133          * the MMU to be un-busy.
1134          */
1135         SMC_SELECT_BANK(2);
1136         while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY)  /* NOTHING */
1137                 ;
1138         outw(BASE + MMU_CMD_REG_W, MMUCR_RELEASE);
1139
1140         /*
1141          * Check whether another packet is ready
1142          */
1143         packet_number = inw(BASE + FIFO_PORTS_REG_W);
1144         if (packet_number & FIFO_REMPTY) {
1145                 return;
1146         }
1147         goto read_another;
1148 }
1149
1150
1151 /*
1152  * Handle IOCTLS.  This function is completely stolen from if_ep.c
1153  * As with its progenitor, it does not handle hardware address
1154  * changes.
1155  */
1156 static int
1157 snioctl(register struct ifnet *ifp, u_long cmd, caddr_t data)
1158 {
1159         struct sn_softc *sc = ifp->if_softc;
1160         int             s, error = 0;
1161
1162         s = splimp();
1163
1164         switch (cmd) {
1165         case SIOCSIFADDR:
1166         case SIOCGIFADDR:
1167         case SIOCSIFMTU:
1168                 error = ether_ioctl(ifp, cmd, data);
1169                 break;
1170
1171         case SIOCSIFFLAGS:
1172                 if ((ifp->if_flags & IFF_UP) == 0 && ifp->if_flags & IFF_RUNNING) {
1173                         ifp->if_flags &= ~IFF_RUNNING;
1174                         snstop(sc);
1175                         break;
1176                 } else {
1177                         /* reinitialize card on any parameter change */
1178                         sninit(sc);
1179                         break;
1180                 }
1181                 break;
1182
1183 #ifdef notdef
1184         case SIOCGHWADDR:
1185                 bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data,
1186                       sizeof(sc->sc_addr));
1187                 break;
1188 #endif
1189
1190         case SIOCADDMULTI:
1191             /* update multicast filter list. */
1192             sn_setmcast(sc);
1193             error = 0;
1194             break;
1195         case SIOCDELMULTI:
1196             /* update multicast filter list. */
1197             sn_setmcast(sc);
1198             error = 0;
1199             break;
1200         default:
1201                 error = EINVAL;
1202         }
1203
1204         splx(s);
1205
1206         return (error);
1207 }
1208
1209 void
1210 snreset(struct sn_softc *sc)
1211 {
1212         int     s;
1213         
1214         s = splimp();
1215         snstop(sc);
1216         sninit(sc);
1217
1218         splx(s);
1219 }
1220
1221 void
1222 snwatchdog(struct ifnet *ifp)
1223 {
1224         int     s;
1225         s = splimp();
1226         sn_intr(ifp->if_softc);
1227         splx(s);
1228 }
1229
1230
1231 /* 1. zero the interrupt mask
1232  * 2. clear the enable receive flag
1233  * 3. clear the enable xmit flags
1234  */
1235 void
1236 snstop(struct sn_softc *sc)
1237 {
1238         
1239         struct ifnet   *ifp = &sc->arpcom.ac_if;
1240
1241         /*
1242          * Clear interrupt mask; disable all interrupts.
1243          */
1244         SMC_SELECT_BANK(2);
1245         outb(BASE + INTR_MASK_REG_B, 0x00);
1246
1247         /*
1248          * Disable transmitter and Receiver
1249          */
1250         SMC_SELECT_BANK(0);
1251         outw(BASE + RECV_CONTROL_REG_W, 0x0000);
1252         outw(BASE + TXMIT_CONTROL_REG_W, 0x0000);
1253
1254         /*
1255          * Cancel watchdog.
1256          */
1257         ifp->if_timer = 0;
1258 }
1259
1260
1261 int
1262 sn_activate(device_t dev)
1263 {
1264         struct sn_softc *sc = device_get_softc(dev);
1265         int err;
1266
1267         sc->port_rid = 0;
1268         sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
1269             0, ~0, SMC_IO_EXTENT, RF_ACTIVE);
1270         if (!sc->port_res) {
1271 #ifdef SN_DEBUG
1272                 device_printf(dev, "Cannot allocate ioport\n");
1273 #endif          
1274                 return ENOMEM;
1275         }
1276
1277         sc->irq_rid = 0;
1278         sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid, 
1279             0, ~0, 1, RF_ACTIVE);
1280         if (!sc->irq_res) {
1281 #ifdef SN_DEBUG
1282                 device_printf(dev, "Cannot allocate irq\n");
1283 #endif
1284                 sn_deactivate(dev);
1285                 return ENOMEM;
1286         }
1287         if ((err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET, sn_intr, sc,
1288             &sc->intrhand)) != 0) {
1289                 sn_deactivate(dev);
1290                 return err;
1291         }
1292         
1293         sc->sn_io_addr = rman_get_start(sc->port_res);
1294         return (0);
1295 }
1296
1297 void
1298 sn_deactivate(device_t dev)
1299 {
1300         struct sn_softc *sc = device_get_softc(dev);
1301         
1302         if (sc->intrhand)
1303                 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
1304         sc->intrhand = 0;
1305         if (sc->port_res)
1306                 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid, 
1307                     sc->port_res);
1308         sc->port_res = 0;
1309         if (sc->irq_res)
1310                 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, 
1311                     sc->irq_res);
1312         sc->irq_res = 0;
1313         return;
1314 }
1315
1316 /*
1317  * Function: sn_probe( device_t dev, int pccard )
1318  *
1319  * Purpose:
1320  *      Tests to see if a given ioaddr points to an SMC9xxx chip.
1321  *      Tries to cause as little damage as possible if it's not a SMC chip.
1322  *      Returns a 0 on success
1323  *
1324  * Algorithm:
1325  *      (1) see if the high byte of BANK_SELECT is 0x33
1326  *      (2) compare the ioaddr with the base register's address
1327  *      (3) see if I recognize the chip ID in the appropriate register
1328  *
1329  *
1330  */
1331 int 
1332 sn_probe(device_t dev, int pccard)
1333 {
1334         struct sn_softc *sc = device_get_softc(dev);
1335         u_int           bank;
1336         u_short         revision_register;
1337         u_short         base_address_register;
1338         u_short         ioaddr;
1339         int             err;
1340
1341         if ((err = sn_activate(dev)) != 0)
1342                 return err;
1343
1344         ioaddr = sc->sn_io_addr;
1345
1346         /*
1347          * First, see if the high byte is 0x33
1348          */
1349         bank = inw(ioaddr + BANK_SELECT_REG_W);
1350         if ((bank & BSR_DETECT_MASK) != BSR_DETECT_VALUE) {
1351 #ifdef  SN_DEBUG
1352                 device_printf(dev, "test1 failed\n");
1353 #endif
1354                 goto error;
1355         }
1356         /*
1357          * The above MIGHT indicate a device, but I need to write to further
1358          * test this.  Go to bank 0, then test that the register still
1359          * reports the high byte is 0x33.
1360          */
1361         outw(ioaddr + BANK_SELECT_REG_W, 0x0000);
1362         bank = inw(ioaddr + BANK_SELECT_REG_W);
1363         if ((bank & BSR_DETECT_MASK) != BSR_DETECT_VALUE) {
1364 #ifdef  SN_DEBUG
1365                 device_printf(dev, "test2 failed\n");
1366 #endif
1367                 goto error;
1368         }
1369         /*
1370          * well, we've already written once, so hopefully another time won't
1371          * hurt.  This time, I need to switch the bank register to bank 1, so
1372          * I can access the base address register.  The contents of the
1373          * BASE_ADDR_REG_W register, after some jiggery pokery, is expected
1374          * to match the I/O port address where the adapter is being probed.
1375          */
1376         outw(ioaddr + BANK_SELECT_REG_W, 0x0001);
1377         base_address_register = inw(ioaddr + BASE_ADDR_REG_W);
1378
1379         /*
1380          * This test is nonsence on PC-card architecture, so if 
1381          * pccard == 1, skip this test. (hosokawa)
1382          */
1383         if (!pccard && (ioaddr != (base_address_register >> 3 & 0x3E0))) {
1384
1385                 /*
1386                  * Well, the base address register didn't match.  Must not
1387                  * have been a SMC chip after all.
1388                  */
1389                 /*
1390                  * printf("sn: ioaddr %x doesn't match card configuration
1391                  * (%x)\n", ioaddr, base_address_register >> 3 & 0x3E0 );
1392                  */
1393
1394 #ifdef  SN_DEBUG
1395                 device_printf(dev, "test3 failed ioaddr = 0x%x, "
1396                     "base_address_register = 0x%x\n", ioaddr,
1397                     base_address_register >> 3 & 0x3E0);
1398 #endif
1399                 goto error;
1400         }
1401         /*
1402          * Check if the revision register is something that I recognize.
1403          * These might need to be added to later, as future revisions could
1404          * be added.
1405          */
1406         outw(ioaddr + BANK_SELECT_REG_W, 0x3);
1407         revision_register = inw(ioaddr + REVISION_REG_W);
1408         if (!chip_ids[(revision_register >> 4) & 0xF]) {
1409
1410                 /*
1411                  * I don't regonize this chip, so...
1412                  */
1413 #ifdef  SN_DEBUG
1414                 device_printf(dev, "test4 failed\n");
1415 #endif
1416                 goto error;
1417         }
1418         /*
1419          * at this point I'll assume that the chip is an SMC9xxx. It might be
1420          * prudent to check a listing of MAC addresses against the hardware
1421          * address, or do some other tests.
1422          */
1423         sn_deactivate(dev);
1424         return 0;
1425  error:
1426         sn_deactivate(dev);
1427         return ENXIO;
1428 }
1429
1430 #define MCFSZ 8
1431
1432 static void
1433 sn_setmcast(struct sn_softc *sc)
1434 {
1435         struct ifnet *ifp = (struct ifnet *)sc;
1436         int flags;
1437
1438         /*
1439          * Set the receiver filter.  We want receive enabled and auto strip
1440          * of CRC from received packet.  If we are promiscuous then set that
1441          * bit too.
1442          */
1443         flags = RCR_ENABLE | RCR_STRIP_CRC;
1444   
1445         if (ifp->if_flags & IFF_PROMISC) {
1446                 flags |= RCR_PROMISC | RCR_ALMUL;
1447         } else if (ifp->if_flags & IFF_ALLMULTI) {
1448                 flags |= RCR_ALMUL;
1449         } else {
1450                 u_char mcf[MCFSZ];
1451                 if (sn_getmcf(&sc->arpcom, mcf)) {
1452                         /* set filter */
1453                         SMC_SELECT_BANK(3);
1454                         outw(BASE + MULTICAST1_REG_W,
1455                             ((u_short)mcf[1] << 8) |  mcf[0]);
1456                         outw(BASE + MULTICAST2_REG_W,
1457                             ((u_short)mcf[3] << 8) |  mcf[2]);
1458                         outw(BASE + MULTICAST3_REG_W,
1459                             ((u_short)mcf[5] << 8) |  mcf[4]);
1460                         outw(BASE + MULTICAST4_REG_W,
1461                             ((u_short)mcf[7] << 8) |  mcf[6]);
1462                 } else {
1463                         flags |= RCR_ALMUL;
1464                 }
1465         }
1466         SMC_SELECT_BANK(0);
1467         outw(BASE + RECV_CONTROL_REG_W, flags);
1468 }
1469
1470 static int
1471 sn_getmcf(struct arpcom *ac, u_char *mcf)
1472 {
1473         int i;
1474         register u_int index, index2;
1475         register u_char *af = (u_char *) mcf;
1476         struct ifmultiaddr *ifma;
1477
1478         bzero(mcf, MCFSZ);
1479
1480         for (ifma = ac->ac_if.if_multiaddrs.lh_first; ifma;
1481              ifma = ifma->ifma_link.le_next) {
1482             if (ifma->ifma_addr->sa_family != AF_LINK)
1483                 return 0;
1484             index = smc_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)) & 0x3f;
1485             index2 = 0;
1486             for (i = 0; i < 6; i++) {
1487                 index2 <<= 1;
1488                 index2 |= (index & 0x01);
1489                 index >>= 1;
1490             }
1491             af[index2 >> 3] |= 1 << (index2 & 7);
1492         }
1493         return 1;  /* use multicast filter */
1494 }
1495
1496 static u_int
1497 smc_crc(u_char *s)
1498 {
1499         int perByte;
1500         int perBit;
1501         const u_int poly = 0xedb88320;
1502         u_int v = 0xffffffff;
1503         u_char c;
1504   
1505         for (perByte = 0; perByte < ETHER_ADDR_LEN; perByte++) {
1506                 c = s[perByte];
1507                 for (perBit = 0; perBit < 8; perBit++) {
1508                         v = (v >> 1)^(((v ^ c) & 0x01) ? poly : 0);
1509                         c >>= 1;
1510                 }
1511         }
1512         return v;
1513 }