Stop abusing splbio simply because others do the same. Use critical
[dragonfly.git] / sys / dev / netif / 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: src/sys/dev/sn/if_sn.c,v 1.7.2.3 2001/02/04 04:38:38 toshi Exp $
32  *   $DragonFly: src/sys/dev/netif/sn/if_sn.c,v 1.18 2005/06/12 17:03:47 joerg Exp $
33  */
34
35 /*
36  * This is a driver for SMC's 9000 series of Ethernet adapters.
37  *
38  * This FreeBSD driver is derived from the smc9194 Linux driver by
39  * Erik Stahlman and is Copyright (C) 1996 by Erik Stahlman.
40  * This driver also shamelessly borrows from the FreeBSD ep driver
41  * which is Copyright (C) 1994 Herb Peyerl <hpeyerl@novatel.ca>
42  * All rights reserved.
43  *
44  * It is set up for my SMC91C92 equipped Ampro LittleBoard embedded
45  * PC.  It is adapted from Erik Stahlman's Linux driver which worked
46  * with his EFA Info*Express SVC VLB adaptor.  According to SMC's databook,
47  * it will work for the entire SMC 9xxx series. (Ha Ha)
48  *
49  * "Features" of the SMC chip:
50  *   4608 byte packet memory. (for the 91C92.  Others have more)
51  *   EEPROM for configuration
52  *   AUI/TP selection
53  *
54  * Authors:
55  *      Erik Stahlman                   erik@vt.edu
56  *      Herb Peyerl                     hpeyerl@novatel.ca
57  *      Andres Vega Garcia              avega@sophia.inria.fr
58  *      Serge Babkin                    babkin@hq.icb.chel.su
59  *      Gardner Buchanan                gbuchanan@shl.com
60  *
61  * Sources:
62  *    o   SMC databook
63  *    o   "smc9194.c:v0.10(FIXED) 02/15/96 by Erik Stahlman (erik@vt.edu)"
64  *    o   "if_ep.c,v 1.19 1995/01/24 20:53:45 davidg Exp"
65  *
66  * Known Bugs:
67  *    o   The hardware multicast filter isn't used yet.
68  *    o   Setting of the hardware address isn't supported.
69  *    o   Hardware padding isn't used.
70  */
71
72 /*
73  * Modifications for Megahertz X-Jack Ethernet Card (XJ-10BT)
74  * 
75  * Copyright (c) 1996 by Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>
76  *                       BSD-nomads, Tokyo, Japan.
77  */
78 /*
79  * Multicast support by Kei TANAKA <kei@pal.xerox.com>
80  * Special thanks to itojun@itojun.org
81  */
82
83 #undef  SN_DEBUG        /* (by hosokawa) */
84
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/kernel.h>
88 #include <sys/errno.h>
89 #include <sys/sockio.h>
90 #include <sys/malloc.h>
91 #include <sys/mbuf.h>
92 #include <sys/socket.h>
93 #include <sys/syslog.h>
94 #include <sys/thread2.h>
95
96 #include <sys/module.h>
97 #include <sys/bus.h>
98
99 #include <machine/bus.h>
100 #include <machine/resource.h>
101 #include <sys/rman.h> 
102
103 #include <net/ethernet.h>
104 #include <net/if.h>
105 #include <net/ifq_var.h>
106 #include <net/if_arp.h>
107 #include <net/if_dl.h>
108 #include <net/if_types.h>
109 #include <net/if_mib.h>
110
111 #ifdef INET
112 #include <netinet/in.h>
113 #include <netinet/in_systm.h>
114 #include <netinet/in_var.h>
115 #include <netinet/ip.h>
116 #endif
117
118 #ifdef NS
119 #include <netns/ns.h>
120 #include <netns/ns_if.h>
121 #endif
122
123 #include <net/bpf.h>
124 #include <net/bpfdesc.h>
125
126 #include <machine/clock.h>
127
128 #include "if_snreg.h"
129 #include "if_snvar.h"
130
131 /* Exported variables */
132 devclass_t sn_devclass;
133
134 static int snioctl(struct ifnet * ifp, u_long, caddr_t, struct ucred *);
135
136 static void snresume(struct ifnet *);
137
138 void sninit(void *);
139 void snread(struct ifnet *);
140 void snreset(struct sn_softc *);
141 void snstart(struct ifnet *);
142 void snstop(struct sn_softc *);
143 void snwatchdog(struct ifnet *);
144
145 static void sn_setmcast(struct sn_softc *);
146 static int sn_getmcf(struct arpcom *ac, u_char *mcf);
147 static u_int smc_crc(u_char *);
148
149 DECLARE_DUMMY_MODULE(if_sn);
150
151 /* I (GB) have been unlucky getting the hardware padding
152  * to work properly.
153  */
154 #define SW_PAD
155
156 static const char *chip_ids[15] = {
157         NULL, NULL, NULL,
158          /* 3 */ "SMC91C90/91C92",
159          /* 4 */ "SMC91C94",
160          /* 5 */ "SMC91C95",
161         NULL,
162          /* 7 */ "SMC91C100",
163          /* 8 */ "SMC91C100FD",
164         NULL, NULL, NULL,
165         NULL, NULL, NULL
166 };
167
168 int
169 sn_attach(device_t dev)
170 {
171         struct sn_softc *sc = device_get_softc(dev);
172         struct ifnet   *ifp = &sc->arpcom.ac_if;
173         u_short         i;
174         u_char         *p;
175         struct ifaddr  *ifa;
176         struct sockaddr_dl *sdl;
177         int             rev;
178         u_short         address;
179         int             j;
180
181         sn_activate(dev);
182
183         snstop(sc);
184
185         sc->dev = dev;
186         sc->pages_wanted = -1;
187
188         device_printf(dev, " ");
189
190         SMC_SELECT_BANK(3);
191         rev = inw(BASE + REVISION_REG_W);
192         if (chip_ids[(rev >> 4) & 0xF])
193                 printf("%s ", chip_ids[(rev >> 4) & 0xF]);
194
195         SMC_SELECT_BANK(1);
196         i = inw(BASE + CONFIG_REG_W);
197         printf("%s\n", i & CR_AUI_SELECT ? "AUI" : "UTP");
198
199         if (sc->pccard_enaddr)
200                 for (j = 0; j < 3; j++) {
201                         u_short w;
202
203                         w = (u_short)sc->arpcom.ac_enaddr[j * 2] | 
204                                 (((u_short)sc->arpcom.ac_enaddr[j * 2 + 1]) << 8);
205                         outw(BASE + IAR_ADDR0_REG_W + j * 2, w);
206                 }
207
208         /*
209          * Read the station address from the chip. The MAC address is bank 1,
210          * regs 4 - 9
211          */
212         SMC_SELECT_BANK(1);
213         p = (u_char *) & sc->arpcom.ac_enaddr;
214         for (i = 0; i < 6; i += 2) {
215                 address = inw(BASE + IAR_ADDR0_REG_W + i);
216                 p[i + 1] = address >> 8;
217                 p[i] = address & 0xFF;
218         }
219         ifp->if_softc = sc;
220         if_initname(ifp, "sn", device_get_unit(dev));
221         ifp->if_mtu = ETHERMTU;
222         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
223         ifp->if_start = snstart;
224         ifp->if_ioctl = snioctl;
225         ifp->if_watchdog = snwatchdog;
226         ifp->if_init = sninit;
227         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
228         ifq_set_ready(&ifp->if_snd);
229         ifp->if_timer = 0;
230
231         ether_ifattach(ifp, sc->arpcom.ac_enaddr);
232
233         /*
234          * Fill the hardware address into ifa_addr if we find an AF_LINK
235          * entry. We need to do this so bpf's can get the hardware addr of
236          * this card. netstat likes this too!
237          */
238         ifa = TAILQ_FIRST(&ifp->if_addrhead);
239         while ((ifa != 0) && (ifa->ifa_addr != 0) &&
240                (ifa->ifa_addr->sa_family != AF_LINK))
241                 ifa = TAILQ_NEXT(ifa, ifa_link);
242
243         if ((ifa != 0) && (ifa->ifa_addr != 0)) {
244                 sdl = (struct sockaddr_dl *) ifa->ifa_addr;
245                 sdl->sdl_type = IFT_ETHER;
246                 sdl->sdl_alen = ETHER_ADDR_LEN;
247                 sdl->sdl_slen = 0;
248                 bcopy(sc->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
249         }
250
251         return 0;
252 }
253
254
255 /*
256  * Reset and initialize the chip
257  */
258 void
259 sninit(void *xsc)
260 {
261         struct sn_softc *sc = xsc;
262         struct ifnet *ifp = &sc->arpcom.ac_if;
263         int             flags;
264         int             mask;
265
266         crit_enter();
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         crit_exit();
353 }
354
355
356 void
357 snstart(struct ifnet *ifp)
358 {
359         struct sn_softc *sc = ifp->if_softc;
360         u_int  len;
361         struct mbuf *m;
362         struct mbuf    *top;
363         int             pad;
364         int             mask;
365         u_short         length;
366         u_short         numPages;
367         u_char          packet_no;
368         int             time_out;
369
370         crit_enter();
371
372         if (ifp->if_flags & IFF_OACTIVE) {
373                 crit_exit();
374                 return;
375         }
376         if (sc->pages_wanted != -1) {
377                 crit_exit();
378                 printf("%s: snstart() while memory allocation pending\n",
379                        ifp->if_xname);
380                 return;
381         }
382 startagain:
383
384         /*
385          * Sneak a peek at the next packet
386          */
387         m = ifq_poll(&ifp->if_snd);
388         if (m == 0) {
389                 crit_exit();
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("%s: large packet discarded (A)\n", ifp->if_xname);
407                 ++sc->arpcom.ac_if.if_oerrors;
408                 m = ifq_dequeue(&ifp->if_snd);
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                 ifp->if_timer = 1;
466                 ifp->if_flags |= IFF_OACTIVE;
467                 sc->pages_wanted = numPages;
468
469                 crit_exit();
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("%s: Memory allocation failed\n", ifp->if_xname);
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         m = ifq_dequeue(&ifp->if_snd);
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         ifp->if_flags |= IFF_OACTIVE;
549         ifp->if_timer = 1;
550
551         BPF_MTAP(ifp, top);
552
553         ifp->if_opackets++;
554         m_freem(top);
555
556 readcheck:
557
558         /*
559          * Is another packet coming in?  We don't want to overflow the tiny
560          * RX FIFO.  If nothing has arrived then attempt to queue another
561          * transmit packet.
562          */
563         if (inw(BASE + FIFO_PORTS_REG_W) & FIFO_REMPTY)
564                 goto startagain;
565
566         crit_exit();
567 }
568
569
570
571 /* Resume a packet transmit operation after a memory allocation
572  * has completed.
573  *
574  * This is basically a hacked up copy of snstart() which handles
575  * a completed memory allocation the same way snstart() does.
576  * It then passes control to snstart to handle any other queued
577  * packets.
578  */
579 static void
580 snresume(struct ifnet *ifp)
581 {
582         struct sn_softc *sc = ifp->if_softc;
583         u_int  len;
584         struct mbuf *m;
585         struct mbuf    *top;
586         int             pad;
587         int             mask;
588         u_short         length;
589         u_short         numPages;
590         u_short         pages_wanted;
591         u_char          packet_no;
592
593         if (sc->pages_wanted < 0)
594                 return;
595
596         pages_wanted = sc->pages_wanted;
597         sc->pages_wanted = -1;
598
599         /*
600          * Sneak a peek at the next packet
601          */
602         m = ifq_poll(&ifp->if_snd);
603         if (m == 0) {
604                 printf("%s: snresume() with nothing to send\n", ifp->if_xname);
605                 return;
606         }
607         /*
608          * Compute the frame length and set pad to give an overall even
609          * number of bytes.  Below we assume that the packet length is even.
610          */
611         for (len = 0, top = m; m; m = m->m_next)
612                 len += m->m_len;
613
614         pad = (len & 1);
615
616         /*
617          * We drop packets that are too large. Perhaps we should truncate
618          * them instead?
619          */
620         if (len + pad > ETHER_MAX_LEN - ETHER_CRC_LEN) {
621                 printf("%s: large packet discarded (B)\n", ifp->if_xname);
622                 ++ifp->if_oerrors;
623                 ifq_dequeue(&ifp->if_snd);
624                 m_freem(m);
625                 return;
626         }
627 #ifdef SW_PAD
628
629         /*
630          * If HW padding is not turned on, then pad to ETHER_MIN_LEN.
631          */
632         if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
633                 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
634
635 #endif  /* SW_PAD */
636
637         length = pad + len;
638
639
640         /*
641          * The MMU wants the number of pages to be the number of 256 byte
642          * 'pages', minus 1 (A packet can't ever have 0 pages. We also
643          * include space for the status word, byte count and control bytes in
644          * the allocation request.
645          */
646         numPages = (length + 6) >> 8;
647
648
649         SMC_SELECT_BANK(2);
650
651         /*
652          * The memory allocation completed.  Check the results. If it failed,
653          * we simply set a watchdog timer and hope for the best.
654          */
655         packet_no = inb(BASE + ALLOC_RESULT_REG_B);
656         if (packet_no & ARR_FAILED) {
657                 printf("%s: Memory allocation failed.  Weird.\n", ifp->if_xname);
658                 ifp->if_timer = 1;
659                 goto try_start;
660         }
661         /*
662          * We have a packet number, so tell the card to use it.
663          */
664         outb(BASE + PACKET_NUM_REG_B, packet_no);
665
666         /*
667          * Now, numPages should match the pages_wanted recorded when the
668          * memory allocation was initiated.
669          */
670         if (pages_wanted != numPages) {
671                 printf("%s: memory allocation wrong size.  Weird.\n", ifp->if_xname);
672                 /*
673                  * If the allocation was the wrong size we simply release the
674                  * memory once it is granted. Wait for the MMU to be un-busy.
675                  */
676                 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY)  /* NOTHING */
677                         ;
678                 outw(BASE + MMU_CMD_REG_W, MMUCR_FREEPKT);
679
680                 return;
681         }
682         /*
683          * Point to the beginning of the packet
684          */
685         outw(BASE + POINTER_REG_W, PTR_AUTOINC | 0x0000);
686
687         /*
688          * Send the packet length (+6 for status, length and control byte)
689          * and the status word (set to zeros)
690          */
691         outw(BASE + DATA_REG_W, 0);
692         outb(BASE + DATA_REG_B, (length + 6) & 0xFF);
693         outb(BASE + DATA_REG_B, (length + 6) >> 8);
694
695         /*
696          * Get the packet from the kernel.  This will include the Ethernet
697          * frame header, MAC Addresses etc.
698          */
699         m = ifq_dequeue(&ifp->if_snd);
700
701         /*
702          * Push out the data to the card.
703          */
704         for (top = m; m != 0; m = m->m_next) {
705
706                 /*
707                  * Push out words.
708                  */
709                 outsw(BASE + DATA_REG_W, mtod(m, caddr_t), m->m_len / 2);
710
711                 /*
712                  * Push out remaining byte.
713                  */
714                 if (m->m_len & 1)
715                         outb(BASE + DATA_REG_B, *(mtod(m, caddr_t) + m->m_len - 1));
716         }
717
718         /*
719          * Push out padding.
720          */
721         while (pad > 1) {
722                 outw(BASE + DATA_REG_W, 0);
723                 pad -= 2;
724         }
725         if (pad)
726                 outb(BASE + DATA_REG_B, 0);
727
728         /*
729          * Push out control byte and unused packet byte The control byte is 0
730          * meaning the packet is even lengthed and no special CRC handling is
731          * desired.
732          */
733         outw(BASE + DATA_REG_W, 0);
734
735         /*
736          * Enable the interrupts and let the chipset deal with it Also set a
737          * watchdog in case we miss the interrupt.
738          */
739         mask = inb(BASE + INTR_MASK_REG_B) | (IM_TX_INT | IM_TX_EMPTY_INT);
740         outb(BASE + INTR_MASK_REG_B, mask);
741         sc->intr_mask = mask;
742         outw(BASE + MMU_CMD_REG_W, MMUCR_ENQUEUE);
743
744         BPF_MTAP(ifp, top);
745
746         ifp->if_opackets++;
747         m_freem(top);
748
749 try_start:
750
751         /*
752          * Now pass control to snstart() to queue any additional packets
753          */
754         ifp->if_flags &= ~IFF_OACTIVE;
755         snstart(ifp);
756
757         /*
758          * We've sent something, so we're active.  Set a watchdog in case the
759          * TX_EMPTY interrupt is lost.
760          */
761         ifp->if_flags |= IFF_OACTIVE;
762         ifp->if_timer = 1;
763 }
764
765
766 void
767 sn_intr(void *arg)
768 {
769         int             status, interrupts;
770         struct sn_softc *sc = (struct sn_softc *) arg;
771         struct ifnet   *ifp = &sc->arpcom.ac_if;
772
773         /*
774          * Chip state registers
775          */
776         u_char          mask;
777         u_char          packet_no;
778         u_short         tx_status;
779         u_short         card_stats;
780
781         crit_enter();
782
783         /*
784          * Clear the watchdog.
785          */
786         ifp->if_timer = 0;
787
788         SMC_SELECT_BANK(2);
789
790         /*
791          * Obtain the current interrupt mask and clear the hardware mask
792          * while servicing interrupts.
793          */
794         mask = inb(BASE + INTR_MASK_REG_B);
795         outb(BASE + INTR_MASK_REG_B, 0x00);
796
797         /*
798          * Get the set of interrupts which occurred and eliminate any which
799          * are masked.
800          */
801         interrupts = inb(BASE + INTR_STAT_REG_B);
802         status = interrupts & mask;
803
804         /*
805          * Now, process each of the interrupt types.
806          */
807
808         /*
809          * Receive Overrun.
810          */
811         if (status & IM_RX_OVRN_INT) {
812
813                 /*
814                  * Acknowlege Interrupt
815                  */
816                 SMC_SELECT_BANK(2);
817                 outb(BASE + INTR_ACK_REG_B, IM_RX_OVRN_INT);
818
819                 ++sc->arpcom.ac_if.if_ierrors;
820         }
821         /*
822          * Got a packet.
823          */
824         if (status & IM_RCV_INT) {
825 #if 1
826                 int             packet_number;
827
828                 SMC_SELECT_BANK(2);
829                 packet_number = inw(BASE + FIFO_PORTS_REG_W);
830
831                 if (packet_number & FIFO_REMPTY) {
832
833                         /*
834                          * we got called , but nothing was on the FIFO
835                          */
836                         printf("sn: Receive interrupt with nothing on FIFO\n");
837
838                         goto out;
839                 }
840 #endif
841                 snread(ifp);
842         }
843         /*
844          * An on-card memory allocation came through.
845          */
846         if (status & IM_ALLOC_INT) {
847
848                 /*
849                  * Disable this interrupt.
850                  */
851                 mask &= ~IM_ALLOC_INT;
852                 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
853                 snresume(&sc->arpcom.ac_if);
854         }
855         /*
856          * TX Completion.  Handle a transmit error message. This will only be
857          * called when there is an error, because of the AUTO_RELEASE mode.
858          */
859         if (status & IM_TX_INT) {
860
861                 /*
862                  * Acknowlege Interrupt
863                  */
864                 SMC_SELECT_BANK(2);
865                 outb(BASE + INTR_ACK_REG_B, IM_TX_INT);
866
867                 packet_no = inw(BASE + FIFO_PORTS_REG_W);
868                 packet_no &= FIFO_TX_MASK;
869
870                 /*
871                  * select this as the packet to read from
872                  */
873                 outb(BASE + PACKET_NUM_REG_B, packet_no);
874
875                 /*
876                  * Position the pointer to the first word from this packet
877                  */
878                 outw(BASE + POINTER_REG_W, PTR_AUTOINC | PTR_READ | 0x0000);
879
880                 /*
881                  * Fetch the TX status word.  The value found here will be a
882                  * copy of the EPH_STATUS_REG_W at the time the transmit
883                  * failed.
884                  */
885                 tx_status = inw(BASE + DATA_REG_W);
886
887                 if (tx_status & EPHSR_TX_SUC) {
888                         device_printf(sc->dev, 
889                             "Successful packet caused interrupt\n");
890                 } else {
891                         ++sc->arpcom.ac_if.if_oerrors;
892                 }
893
894                 if (tx_status & EPHSR_LATCOL)
895                         ++sc->arpcom.ac_if.if_collisions;
896
897                 /*
898                  * Some of these errors will have disabled transmit.
899                  * Re-enable transmit now.
900                  */
901                 SMC_SELECT_BANK(0);
902
903 #ifdef SW_PAD
904                 outw(BASE + TXMIT_CONTROL_REG_W, TCR_ENABLE);
905 #else
906                 outw(BASE + TXMIT_CONTROL_REG_W, TCR_ENABLE | TCR_PAD_ENABLE);
907 #endif  /* SW_PAD */
908
909                 /*
910                  * kill the failed packet. Wait for the MMU to be un-busy.
911                  */
912                 SMC_SELECT_BANK(2);
913                 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY)  /* NOTHING */
914                         ;
915                 outw(BASE + MMU_CMD_REG_W, MMUCR_FREEPKT);
916
917                 /*
918                  * Attempt to queue more transmits.
919                  */
920                 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
921                 snstart(&sc->arpcom.ac_if);
922         }
923         /*
924          * Transmit underrun.  We use this opportunity to update transmit
925          * statistics from the card.
926          */
927         if (status & IM_TX_EMPTY_INT) {
928
929                 /*
930                  * Acknowlege Interrupt
931                  */
932                 SMC_SELECT_BANK(2);
933                 outb(BASE + INTR_ACK_REG_B, IM_TX_EMPTY_INT);
934
935                 /*
936                  * Disable this interrupt.
937                  */
938                 mask &= ~IM_TX_EMPTY_INT;
939
940                 SMC_SELECT_BANK(0);
941                 card_stats = inw(BASE + COUNTER_REG_W);
942
943                 /*
944                  * Single collisions
945                  */
946                 sc->arpcom.ac_if.if_collisions += card_stats & ECR_COLN_MASK;
947
948                 /*
949                  * Multiple collisions
950                  */
951                 sc->arpcom.ac_if.if_collisions += (card_stats & ECR_MCOLN_MASK) >> 4;
952
953                 SMC_SELECT_BANK(2);
954
955                 /*
956                  * Attempt to enqueue some more stuff.
957                  */
958                 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
959                 snstart(&sc->arpcom.ac_if);
960         }
961         /*
962          * Some other error.  Try to fix it by resetting the adapter.
963          */
964         if (status & IM_EPH_INT) {
965                 snstop(sc);
966                 sninit(sc);
967         }
968
969 out:
970         /*
971          * Handled all interrupt sources.
972          */
973
974         SMC_SELECT_BANK(2);
975
976         /*
977          * Reestablish interrupts from mask which have not been deselected
978          * during this interrupt.  Note that the hardware mask, which was set
979          * to 0x00 at the start of this service routine, may have been
980          * updated by one or more of the interrupt handers and we must let
981          * those new interrupts stay enabled here.
982          */
983         mask |= inb(BASE + INTR_MASK_REG_B);
984         outb(BASE + INTR_MASK_REG_B, mask);
985         sc->intr_mask = mask;
986
987         crit_exit();
988 }
989
990 void
991 snread(struct ifnet *ifp)
992 {
993         struct sn_softc *sc = ifp->if_softc;
994         struct mbuf    *m;
995         short           status;
996         int             packet_number;
997         u_short         packet_length;
998         u_char         *data;
999
1000         SMC_SELECT_BANK(2);
1001 #if 0
1002         packet_number = inw(BASE + FIFO_PORTS_REG_W);
1003
1004         if (packet_number & FIFO_REMPTY) {
1005
1006                 /*
1007                  * we got called , but nothing was on the FIFO
1008                  */
1009                 printf("sn: Receive interrupt with nothing on FIFO\n");
1010                 return;
1011         }
1012 #endif
1013 read_another:
1014
1015         /*
1016          * Start reading from the start of the packet. Since PTR_RCV is set,
1017          * packet number is found in FIFO_PORTS_REG_W, FIFO_RX_MASK.
1018          */
1019         outw(BASE + POINTER_REG_W, PTR_READ | PTR_RCV | PTR_AUTOINC | 0x0000);
1020
1021         /*
1022          * First two words are status and packet_length
1023          */
1024         status = inw(BASE + DATA_REG_W);
1025         packet_length = inw(BASE + DATA_REG_W) & RLEN_MASK;
1026
1027         /*
1028          * The packet length contains 3 extra words: status, length, and a
1029          * extra word with the control byte.
1030          */
1031         packet_length -= 6;
1032
1033         /*
1034          * Account for receive errors and discard.
1035          */
1036         if (status & RS_ERRORS) {
1037                 ++ifp->if_ierrors;
1038                 goto out;
1039         }
1040         /*
1041          * A packet is received.
1042          */
1043
1044         /*
1045          * Adjust for odd-length packet.
1046          */
1047         if (status & RS_ODDFRAME)
1048                 packet_length++;
1049
1050         /*
1051          * Allocate a header mbuf from the kernel.
1052          */
1053         MGETHDR(m, MB_DONTWAIT, MT_DATA);
1054         if (m == NULL)
1055                 goto out;
1056
1057         m->m_pkthdr.rcvif = ifp;
1058         m->m_pkthdr.len = m->m_len = packet_length;
1059
1060         /*
1061          * Attach an mbuf cluster
1062          */
1063         MCLGET(m, MB_DONTWAIT);
1064
1065         /*
1066          * Insist on getting a cluster
1067          */
1068         if ((m->m_flags & M_EXT) == 0) {
1069                 m_freem(m);
1070                 ++ifp->if_ierrors;
1071                 printf("sn: snread() kernel memory allocation problem\n");
1072                 goto out;
1073         }
1074
1075         /*
1076          * Get packet, including link layer address, from interface.
1077          */
1078
1079         data = mtod(m, u_char *);
1080         insw(BASE + DATA_REG_W, data, packet_length >> 1);
1081         if (packet_length & 1) {
1082                 data += packet_length & ~1;
1083                 *data = inb(BASE + DATA_REG_B);
1084         }
1085         ++ifp->if_ipackets;
1086
1087         m->m_pkthdr.len = m->m_len = packet_length;
1088
1089         (*ifp->if_input)(ifp, m);
1090
1091 out:
1092
1093         /*
1094          * Error or good, tell the card to get rid of this packet Wait for
1095          * the MMU to be un-busy.
1096          */
1097         SMC_SELECT_BANK(2);
1098         while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY)  /* NOTHING */
1099                 ;
1100         outw(BASE + MMU_CMD_REG_W, MMUCR_RELEASE);
1101
1102         /*
1103          * Check whether another packet is ready
1104          */
1105         packet_number = inw(BASE + FIFO_PORTS_REG_W);
1106         if (packet_number & FIFO_REMPTY) {
1107                 return;
1108         }
1109         goto read_another;
1110 }
1111
1112
1113 /*
1114  * Handle IOCTLS.  This function is completely stolen from if_ep.c
1115  * As with its progenitor, it does not handle hardware address
1116  * changes.
1117  */
1118 static int
1119 snioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1120 {
1121         struct sn_softc *sc = ifp->if_softc;
1122         int error = 0;
1123
1124         crit_enter();
1125
1126         switch (cmd) {
1127         case SIOCSIFFLAGS:
1128                 if ((ifp->if_flags & IFF_UP) == 0 && ifp->if_flags & IFF_RUNNING) {
1129                         ifp->if_flags &= ~IFF_RUNNING;
1130                         snstop(sc);
1131                         break;
1132                 } else {
1133                         /* reinitialize card on any parameter change */
1134                         sninit(sc);
1135                         break;
1136                 }
1137                 break;
1138
1139 #ifdef notdef
1140         case SIOCGHWADDR:
1141                 bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data,
1142                       sizeof(sc->sc_addr));
1143                 break;
1144 #endif
1145
1146         case SIOCADDMULTI:
1147             /* update multicast filter list. */
1148             sn_setmcast(sc);
1149             error = 0;
1150             break;
1151         case SIOCDELMULTI:
1152             /* update multicast filter list. */
1153             sn_setmcast(sc);
1154             error = 0;
1155             break;
1156         default:
1157                 error = ether_ioctl(ifp, cmd, data);
1158                 break;
1159         }
1160
1161         crit_exit();
1162
1163         return (error);
1164 }
1165
1166 void
1167 snreset(struct sn_softc *sc)
1168 {
1169         crit_enter();
1170
1171         snstop(sc);
1172         sninit(sc);
1173
1174         crit_exit();
1175 }
1176
1177 void
1178 snwatchdog(struct ifnet *ifp)
1179 {
1180         crit_enter();
1181
1182         sn_intr(ifp->if_softc);
1183
1184         crit_exit();
1185 }
1186
1187
1188 /* 1. zero the interrupt mask
1189  * 2. clear the enable receive flag
1190  * 3. clear the enable xmit flags
1191  */
1192 void
1193 snstop(struct sn_softc *sc)
1194 {
1195         
1196         struct ifnet   *ifp = &sc->arpcom.ac_if;
1197
1198         /*
1199          * Clear interrupt mask; disable all interrupts.
1200          */
1201         SMC_SELECT_BANK(2);
1202         outb(BASE + INTR_MASK_REG_B, 0x00);
1203
1204         /*
1205          * Disable transmitter and Receiver
1206          */
1207         SMC_SELECT_BANK(0);
1208         outw(BASE + RECV_CONTROL_REG_W, 0x0000);
1209         outw(BASE + TXMIT_CONTROL_REG_W, 0x0000);
1210
1211         /*
1212          * Cancel watchdog.
1213          */
1214         ifp->if_timer = 0;
1215 }
1216
1217
1218 int
1219 sn_activate(device_t dev)
1220 {
1221         struct sn_softc *sc = device_get_softc(dev);
1222         int err;
1223
1224         sc->port_rid = 0;
1225         sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
1226             0, ~0, SMC_IO_EXTENT, RF_ACTIVE);
1227         if (!sc->port_res) {
1228 #ifdef SN_DEBUG
1229                 device_printf(dev, "Cannot allocate ioport\n");
1230 #endif          
1231                 return ENOMEM;
1232         }
1233
1234         sc->irq_rid = 0;
1235         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 
1236             RF_ACTIVE);
1237         if (!sc->irq_res) {
1238 #ifdef SN_DEBUG
1239                 device_printf(dev, "Cannot allocate irq\n");
1240 #endif
1241                 sn_deactivate(dev);
1242                 return ENOMEM;
1243         }
1244         err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET, sn_intr, sc,
1245                              &sc->intrhand, NULL);
1246         if (err) {
1247                 sn_deactivate(dev);
1248                 return err;
1249         }
1250         
1251         sc->sn_io_addr = rman_get_start(sc->port_res);
1252         return (0);
1253 }
1254
1255 void
1256 sn_deactivate(device_t dev)
1257 {
1258         struct sn_softc *sc = device_get_softc(dev);
1259         
1260         if (sc->intrhand)
1261                 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
1262         sc->intrhand = 0;
1263         if (sc->port_res)
1264                 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid, 
1265                     sc->port_res);
1266         sc->port_res = 0;
1267         if (sc->irq_res)
1268                 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, 
1269                     sc->irq_res);
1270         sc->irq_res = 0;
1271         return;
1272 }
1273
1274 /*
1275  * Function: sn_probe( device_t dev, int pccard )
1276  *
1277  * Purpose:
1278  *      Tests to see if a given ioaddr points to an SMC9xxx chip.
1279  *      Tries to cause as little damage as possible if it's not a SMC chip.
1280  *      Returns a 0 on success
1281  *
1282  * Algorithm:
1283  *      (1) see if the high byte of BANK_SELECT is 0x33
1284  *      (2) compare the ioaddr with the base register's address
1285  *      (3) see if I recognize the chip ID in the appropriate register
1286  *
1287  *
1288  */
1289 int 
1290 sn_probe(device_t dev, int pccard)
1291 {
1292         struct sn_softc *sc = device_get_softc(dev);
1293         u_int           bank;
1294         u_short         revision_register;
1295         u_short         base_address_register;
1296         u_short         ioaddr;
1297         int             err;
1298
1299         if ((err = sn_activate(dev)) != 0)
1300                 return err;
1301
1302         ioaddr = sc->sn_io_addr;
1303
1304         /*
1305          * First, see if the high byte is 0x33
1306          */
1307         bank = inw(ioaddr + BANK_SELECT_REG_W);
1308         if ((bank & BSR_DETECT_MASK) != BSR_DETECT_VALUE) {
1309 #ifdef  SN_DEBUG
1310                 device_printf(dev, "test1 failed\n");
1311 #endif
1312                 goto error;
1313         }
1314         /*
1315          * The above MIGHT indicate a device, but I need to write to further
1316          * test this.  Go to bank 0, then test that the register still
1317          * reports the high byte is 0x33.
1318          */
1319         outw(ioaddr + BANK_SELECT_REG_W, 0x0000);
1320         bank = inw(ioaddr + BANK_SELECT_REG_W);
1321         if ((bank & BSR_DETECT_MASK) != BSR_DETECT_VALUE) {
1322 #ifdef  SN_DEBUG
1323                 device_printf(dev, "test2 failed\n");
1324 #endif
1325                 goto error;
1326         }
1327         /*
1328          * well, we've already written once, so hopefully another time won't
1329          * hurt.  This time, I need to switch the bank register to bank 1, so
1330          * I can access the base address register.  The contents of the
1331          * BASE_ADDR_REG_W register, after some jiggery pokery, is expected
1332          * to match the I/O port address where the adapter is being probed.
1333          */
1334         outw(ioaddr + BANK_SELECT_REG_W, 0x0001);
1335         base_address_register = inw(ioaddr + BASE_ADDR_REG_W);
1336
1337         /*
1338          * This test is nonsence on PC-card architecture, so if 
1339          * pccard == 1, skip this test. (hosokawa)
1340          */
1341         if (!pccard && (ioaddr != (base_address_register >> 3 & 0x3E0))) {
1342
1343                 /*
1344                  * Well, the base address register didn't match.  Must not
1345                  * have been a SMC chip after all.
1346                  */
1347                 /*
1348                  * printf("sn: ioaddr %x doesn't match card configuration
1349                  * (%x)\n", ioaddr, base_address_register >> 3 & 0x3E0 );
1350                  */
1351
1352 #ifdef  SN_DEBUG
1353                 device_printf(dev, "test3 failed ioaddr = 0x%x, "
1354                     "base_address_register = 0x%x\n", ioaddr,
1355                     base_address_register >> 3 & 0x3E0);
1356 #endif
1357                 goto error;
1358         }
1359         /*
1360          * Check if the revision register is something that I recognize.
1361          * These might need to be added to later, as future revisions could
1362          * be added.
1363          */
1364         outw(ioaddr + BANK_SELECT_REG_W, 0x3);
1365         revision_register = inw(ioaddr + REVISION_REG_W);
1366         if (!chip_ids[(revision_register >> 4) & 0xF]) {
1367
1368                 /*
1369                  * I don't regonize this chip, so...
1370                  */
1371 #ifdef  SN_DEBUG
1372                 device_printf(dev, "test4 failed\n");
1373 #endif
1374                 goto error;
1375         }
1376         /*
1377          * at this point I'll assume that the chip is an SMC9xxx. It might be
1378          * prudent to check a listing of MAC addresses against the hardware
1379          * address, or do some other tests.
1380          */
1381         sn_deactivate(dev);
1382         return 0;
1383  error:
1384         sn_deactivate(dev);
1385         return ENXIO;
1386 }
1387
1388 #define MCFSZ 8
1389
1390 static void
1391 sn_setmcast(struct sn_softc *sc)
1392 {
1393         struct ifnet *ifp = (struct ifnet *)sc;
1394         int flags;
1395
1396         /*
1397          * Set the receiver filter.  We want receive enabled and auto strip
1398          * of CRC from received packet.  If we are promiscuous then set that
1399          * bit too.
1400          */
1401         flags = RCR_ENABLE | RCR_STRIP_CRC;
1402   
1403         if (ifp->if_flags & IFF_PROMISC) {
1404                 flags |= RCR_PROMISC | RCR_ALMUL;
1405         } else if (ifp->if_flags & IFF_ALLMULTI) {
1406                 flags |= RCR_ALMUL;
1407         } else {
1408                 u_char mcf[MCFSZ];
1409                 if (sn_getmcf(&sc->arpcom, mcf)) {
1410                         /* set filter */
1411                         SMC_SELECT_BANK(3);
1412                         outw(BASE + MULTICAST1_REG_W,
1413                             ((u_short)mcf[1] << 8) |  mcf[0]);
1414                         outw(BASE + MULTICAST2_REG_W,
1415                             ((u_short)mcf[3] << 8) |  mcf[2]);
1416                         outw(BASE + MULTICAST3_REG_W,
1417                             ((u_short)mcf[5] << 8) |  mcf[4]);
1418                         outw(BASE + MULTICAST4_REG_W,
1419                             ((u_short)mcf[7] << 8) |  mcf[6]);
1420                 } else {
1421                         flags |= RCR_ALMUL;
1422                 }
1423         }
1424         SMC_SELECT_BANK(0);
1425         outw(BASE + RECV_CONTROL_REG_W, flags);
1426 }
1427
1428 static int
1429 sn_getmcf(struct arpcom *ac, u_char *mcf)
1430 {
1431         int i;
1432         u_int index, index2;
1433         u_char *af = (u_char *) mcf;
1434         struct ifmultiaddr *ifma;
1435
1436         bzero(mcf, MCFSZ);
1437
1438         for (ifma = ac->ac_if.if_multiaddrs.lh_first; ifma;
1439              ifma = ifma->ifma_link.le_next) {
1440             if (ifma->ifma_addr->sa_family != AF_LINK)
1441                 return 0;
1442             index = smc_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)) & 0x3f;
1443             index2 = 0;
1444             for (i = 0; i < 6; i++) {
1445                 index2 <<= 1;
1446                 index2 |= (index & 0x01);
1447                 index >>= 1;
1448             }
1449             af[index2 >> 3] |= 1 << (index2 & 7);
1450         }
1451         return 1;  /* use multicast filter */
1452 }
1453
1454 static u_int
1455 smc_crc(u_char *s)
1456 {
1457         int perByte;
1458         int perBit;
1459         const u_int poly = 0xedb88320;
1460         u_int v = 0xffffffff;
1461         u_char c;
1462   
1463         for (perByte = 0; perByte < ETHER_ADDR_LEN; perByte++) {
1464                 c = s[perByte];
1465                 for (perBit = 0; perBit < 8; perBit++) {
1466                         v = (v >> 1)^(((v ^ c) & 0x01) ? poly : 0);
1467                         c >>= 1;
1468                 }
1469         }
1470         return v;
1471 }