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