2 * Copyright (c) 1996 Gardner Buchanan <gbuchanan@shl.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
31 * $FreeBSD: src/sys/dev/sn/if_sn.c,v 1.7.2.3 2001/02/04 04:38:38 toshi Exp $
35 * This is a driver for SMC's 9000 series of Ethernet adapters.
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.
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)
48 * "Features" of the SMC chip:
49 * 4608 byte packet memory. (for the 91C92. Others have more)
50 * EEPROM for configuration
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
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"
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.
72 * Modifications for Megahertz X-Jack Ethernet Card (XJ-10BT)
74 * Copyright (c) 1996 by Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>
75 * BSD-nomads, Tokyo, Japan.
78 * Multicast support by Kei TANAKA <kei@pal.xerox.com>
79 * Special thanks to itojun@itojun.org
82 #undef SN_DEBUG /* (by hosokawa) */
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/kernel.h>
87 #include <sys/interrupt.h>
88 #include <sys/errno.h>
89 #include <sys/sockio.h>
90 #include <sys/malloc.h>
92 #include <sys/socket.h>
93 #include <sys/syslog.h>
94 #include <sys/serialize.h>
95 #include <sys/module.h>
98 #include <sys/thread2.h>
100 #include <net/ethernet.h>
102 #include <net/ifq_var.h>
103 #include <net/if_arp.h>
104 #include <net/if_dl.h>
105 #include <net/if_types.h>
106 #include <net/if_mib.h>
109 #include <netinet/in.h>
110 #include <netinet/in_systm.h>
111 #include <netinet/in_var.h>
112 #include <netinet/ip.h>
116 #include <net/bpfdesc.h>
118 #include <machine/clock.h>
120 #include "if_snreg.h"
121 #include "if_snvar.h"
123 /* Exported variables */
124 devclass_t sn_devclass;
126 static int snioctl(struct ifnet * ifp, u_long, caddr_t, struct ucred *);
128 static void snresume(struct ifnet *);
131 void snread(struct ifnet *);
132 void snreset(struct sn_softc *);
133 void snstart(struct ifnet *, struct ifaltq_subque *);
134 void snstop(struct sn_softc *);
135 void snwatchdog(struct ifnet *);
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 *);
141 DECLARE_DUMMY_MODULE(if_sn);
143 /* I (GB) have been unlucky getting the hardware padding
148 static const char *chip_ids[15] = {
150 /* 3 */ "SMC91C90/91C92",
155 /* 8 */ "SMC91C100FD",
161 sn_attach(device_t dev)
163 struct sn_softc *sc = device_get_softc(dev);
164 struct ifnet *ifp = &sc->arpcom.ac_if;
177 sc->pages_wanted = -1;
179 device_printf(dev, " ");
182 rev = inw(BASE + REVISION_REG_W);
183 if (chip_ids[(rev >> 4) & 0xF])
184 kprintf("%s ", chip_ids[(rev >> 4) & 0xF]);
187 i = inw(BASE + CONFIG_REG_W);
188 kprintf("%s\n", i & CR_AUI_SELECT ? "AUI" : "UTP");
190 if (sc->pccard_enaddr)
191 for (j = 0; j < 3; j++) {
194 w = (u_short)sc->arpcom.ac_enaddr[j * 2] |
195 (((u_short)sc->arpcom.ac_enaddr[j * 2 + 1]) << 8);
196 outw(BASE + IAR_ADDR0_REG_W + j * 2, w);
200 * Read the station address from the chip. The MAC address is bank 1,
204 p = (u_char *) & sc->arpcom.ac_enaddr;
205 for (i = 0; i < 6; i += 2) {
206 address = inw(BASE + IAR_ADDR0_REG_W + i);
207 p[i + 1] = address >> 8;
208 p[i] = address & 0xFF;
211 if_initname(ifp, "sn", device_get_unit(dev));
212 ifp->if_mtu = ETHERMTU;
213 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
214 ifp->if_start = snstart;
215 ifp->if_ioctl = snioctl;
216 ifp->if_watchdog = snwatchdog;
217 ifp->if_init = sninit;
218 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
219 ifq_set_ready(&ifp->if_snd);
222 ether_ifattach(ifp, sc->arpcom.ac_enaddr, NULL);
224 ifq_set_cpuid(&ifp->if_snd, rman_get_cpuid(sc->irq_res));
226 error = bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE,
227 sn_intr, sc, &sc->intrhand,
240 * Reset and initialize the chip
245 struct sn_softc *sc = xsc;
246 struct ifnet *ifp = &sc->arpcom.ac_if;
251 * This resets the registers mostly to defaults, but doesn't affect
252 * EEPROM. After the reset cycle, we pause briefly for the chip to
256 outw(BASE + RECV_CONTROL_REG_W, RCR_SOFTRESET);
258 outw(BASE + RECV_CONTROL_REG_W, 0x0000);
262 outw(BASE + TXMIT_CONTROL_REG_W, 0x0000);
265 * Set the control register to automatically release succesfully
266 * transmitted packets (making the best use out of our limited
267 * memory) and to enable the EPH interrupt on certain TX errors.
270 outw(BASE + CONTROL_REG_W, (CTR_AUTO_RELEASE | CTR_TE_ENABLE |
271 CTR_CR_ENABLE | CTR_LE_ENABLE));
273 /* Set squelch level to 240mV (default 480mV) */
274 flags = inw(BASE + CONFIG_REG_W);
275 flags |= CR_SET_SQLCH;
276 outw(BASE + CONFIG_REG_W, flags);
279 * Reset the MMU and wait for it to be un-busy.
282 outw(BASE + MMU_CMD_REG_W, MMUCR_RESET);
283 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */
287 * Disable all interrupts
289 outb(BASE + INTR_MASK_REG_B, 0x00);
294 * Set the transmitter control. We want it enabled.
300 * I (GB) have been unlucky getting this to work.
302 flags |= TCR_PAD_ENABLE;
305 outw(BASE + TXMIT_CONTROL_REG_W, flags);
309 * Now, enable interrupts
318 outb(BASE + INTR_MASK_REG_B, mask);
319 sc->intr_mask = mask;
320 sc->pages_wanted = -1;
324 * Mark the interface running but not active.
326 ifp->if_flags |= IFF_RUNNING;
327 ifq_clr_oactive(&ifp->if_snd);
330 * Attempt to push out any waiting packets.
337 snstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
339 struct sn_softc *sc = ifp->if_softc;
350 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
352 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
355 if (sc->pages_wanted != -1) {
356 /* XXX should never happen */
357 kprintf("%s: snstart() while memory allocation pending\n",
359 ifq_set_oactive(&ifp->if_snd);
365 * Sneak a peek at the next packet
367 m = ifq_dequeue(&ifp->if_snd, NULL);
372 * Compute the frame length and set pad to give an overall even
373 * number of bytes. Below we assume that the packet length is even.
375 for (len = 0, top = m; m; m = m->m_next)
381 * We drop packets that are too large. Perhaps we should truncate
384 if (len + pad > ETHER_MAX_LEN - ETHER_CRC_LEN) {
385 kprintf("%s: large packet discarded (A)\n", ifp->if_xname);
386 IFNET_STAT_INC(&sc->arpcom.ac_if, oerrors, 1);
393 * If HW padding is not turned on, then pad to ETHER_MIN_LEN.
395 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
396 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
403 * The MMU wants the number of pages to be the number of 256 byte
404 * 'pages', minus 1 (A packet can't ever have 0 pages. We also
405 * include space for the status word, byte count and control bytes in
406 * the allocation request.
408 numPages = (length + 6) >> 8;
412 * Now, try to allocate the memory
415 outw(BASE + MMU_CMD_REG_W, MMUCR_ALLOC | numPages);
418 * Wait a short amount of time to see if the allocation request
419 * completes. Otherwise, I enable the interrupt and wait for
420 * completion asyncronously.
423 time_out = MEMORY_WAIT_TIME;
425 if (inb(BASE + INTR_STAT_REG_B) & IM_ALLOC_INT)
427 } while (--time_out);
432 * No memory now. Oh well, wait until the chip finds memory
433 * later. Remember how many pages we were asking for and
434 * enable the allocation completion interrupt. Also set a
435 * watchdog in case we miss the interrupt. We mark the
436 * interface active since there is no point in attempting an
437 * snstart() until after the memory is available.
439 mask = inb(BASE + INTR_MASK_REG_B) | IM_ALLOC_INT;
440 outb(BASE + INTR_MASK_REG_B, mask);
441 sc->intr_mask = mask;
444 ifq_set_oactive(&ifp->if_snd);
445 sc->pages_wanted = numPages;
446 ifq_prepend(&ifp->if_snd, top);
451 * The memory allocation completed. Check the results.
453 packet_no = inb(BASE + ALLOC_RESULT_REG_B);
454 if (packet_no & ARR_FAILED) {
455 kprintf("%s: Memory allocation failed\n", ifp->if_xname);
456 ifq_prepend(&ifp->if_snd, top);
460 * We have a packet number, so tell the card to use it.
462 outb(BASE + PACKET_NUM_REG_B, packet_no);
465 * Point to the beginning of the packet
467 outw(BASE + POINTER_REG_W, PTR_AUTOINC | 0x0000);
470 * Send the packet length (+6 for status, length and control byte)
471 * and the status word (set to zeros)
473 outw(BASE + DATA_REG_W, 0);
474 outb(BASE + DATA_REG_B, (length + 6) & 0xFF);
475 outb(BASE + DATA_REG_B, (length + 6) >> 8);
478 * Push out the data to the card.
480 for (m = top; m != NULL; m = m->m_next) {
485 outsw(BASE + DATA_REG_W, mtod(m, caddr_t), m->m_len / 2);
488 * Push out remaining byte.
491 outb(BASE + DATA_REG_B, *(mtod(m, caddr_t) + m->m_len - 1));
498 outw(BASE + DATA_REG_W, 0);
502 outb(BASE + DATA_REG_B, 0);
505 * Push out control byte and unused packet byte The control byte is 0
506 * meaning the packet is even lengthed and no special CRC handling is
509 outw(BASE + DATA_REG_W, 0);
512 * Enable the interrupts and let the chipset deal with it Also set a
513 * watchdog in case we miss the interrupt.
515 mask = inb(BASE + INTR_MASK_REG_B) | (IM_TX_INT | IM_TX_EMPTY_INT);
516 outb(BASE + INTR_MASK_REG_B, mask);
517 sc->intr_mask = mask;
519 outw(BASE + MMU_CMD_REG_W, MMUCR_ENQUEUE);
521 ifq_set_oactive(&ifp->if_snd);
526 IFNET_STAT_INC(ifp, opackets, 1);
532 * Is another packet coming in? We don't want to overflow the tiny
533 * RX FIFO. If nothing has arrived then attempt to queue another
536 if (inw(BASE + FIFO_PORTS_REG_W) & FIFO_REMPTY)
542 /* Resume a packet transmit operation after a memory allocation
545 * This is basically a hacked up copy of snstart() which handles
546 * a completed memory allocation the same way snstart() does.
547 * It then passes control to snstart to handle any other queued
551 snresume(struct ifnet *ifp)
553 struct sn_softc *sc = ifp->if_softc;
561 u_short pages_wanted;
564 if (sc->pages_wanted < 0)
567 pages_wanted = sc->pages_wanted;
568 sc->pages_wanted = -1;
571 * Sneak a peek at the next packet
573 m = ifq_dequeue(&ifp->if_snd, NULL);
575 kprintf("%s: snresume() with nothing to send\n",
581 * Compute the frame length and set pad to give an overall even
582 * number of bytes. Below we assume that the packet length is even.
584 for (len = 0, top = m; m; m = m->m_next)
590 * We drop packets that are too large. Perhaps we should truncate
593 if (len + pad > ETHER_MAX_LEN - ETHER_CRC_LEN) {
594 kprintf("%s: large packet discarded (B)\n", ifp->if_xname);
595 IFNET_STAT_INC(ifp, oerrors, 1);
602 * If HW padding is not turned on, then pad to ETHER_MIN_LEN.
604 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
605 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
613 * The MMU wants the number of pages to be the number of 256 byte
614 * 'pages', minus 1 (A packet can't ever have 0 pages. We also
615 * include space for the status word, byte count and control bytes in
616 * the allocation request.
618 numPages = (length + 6) >> 8;
624 * The memory allocation completed. Check the results. If it failed,
625 * we simply set a watchdog timer and hope for the best.
627 packet_no = inb(BASE + ALLOC_RESULT_REG_B);
628 if (packet_no & ARR_FAILED) {
629 kprintf("%s: Memory allocation failed. Weird.\n", ifp->if_xname);
631 ifq_prepend(&ifp->if_snd, top);
635 * We have a packet number, so tell the card to use it.
637 outb(BASE + PACKET_NUM_REG_B, packet_no);
640 * Now, numPages should match the pages_wanted recorded when the
641 * memory allocation was initiated.
643 if (pages_wanted != numPages) {
644 kprintf("%s: memory allocation wrong size. Weird.\n", ifp->if_xname);
646 * If the allocation was the wrong size we simply release the
647 * memory once it is granted. Wait for the MMU to be un-busy.
649 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */
651 outw(BASE + MMU_CMD_REG_W, MMUCR_FREEPKT);
653 ifq_prepend(&ifp->if_snd, top);
657 * Point to the beginning of the packet
659 outw(BASE + POINTER_REG_W, PTR_AUTOINC | 0x0000);
662 * Send the packet length (+6 for status, length and control byte)
663 * and the status word (set to zeros)
665 outw(BASE + DATA_REG_W, 0);
666 outb(BASE + DATA_REG_B, (length + 6) & 0xFF);
667 outb(BASE + DATA_REG_B, (length + 6) >> 8);
670 * Push out the data to the card.
672 for (m = top; m != NULL; m = m->m_next) {
677 outsw(BASE + DATA_REG_W, mtod(m, caddr_t), m->m_len / 2);
680 * Push out remaining byte.
683 outb(BASE + DATA_REG_B, *(mtod(m, caddr_t) + m->m_len - 1));
690 outw(BASE + DATA_REG_W, 0);
694 outb(BASE + DATA_REG_B, 0);
697 * Push out control byte and unused packet byte The control byte is 0
698 * meaning the packet is even lengthed and no special CRC handling is
701 outw(BASE + DATA_REG_W, 0);
704 * Enable the interrupts and let the chipset deal with it Also set a
705 * watchdog in case we miss the interrupt.
707 mask = inb(BASE + INTR_MASK_REG_B) | (IM_TX_INT | IM_TX_EMPTY_INT);
708 outb(BASE + INTR_MASK_REG_B, mask);
709 sc->intr_mask = mask;
710 outw(BASE + MMU_CMD_REG_W, MMUCR_ENQUEUE);
714 IFNET_STAT_INC(ifp, opackets, 1);
720 * Now pass control to snstart() to queue any additional packets
722 ifq_clr_oactive(&ifp->if_snd);
726 * We've sent something, so we're active. Set a watchdog in case the
727 * TX_EMPTY interrupt is lost.
729 ifq_set_oactive(&ifp->if_snd);
737 int status, interrupts;
738 struct sn_softc *sc = (struct sn_softc *) arg;
739 struct ifnet *ifp = &sc->arpcom.ac_if;
742 * Chip state registers
750 * Clear the watchdog.
757 * Obtain the current interrupt mask and clear the hardware mask
758 * while servicing interrupts.
760 mask = inb(BASE + INTR_MASK_REG_B);
761 outb(BASE + INTR_MASK_REG_B, 0x00);
764 * Get the set of interrupts which occurred and eliminate any which
767 interrupts = inb(BASE + INTR_STAT_REG_B);
768 status = interrupts & mask;
771 * Now, process each of the interrupt types.
777 if (status & IM_RX_OVRN_INT) {
780 * Acknowlege Interrupt
783 outb(BASE + INTR_ACK_REG_B, IM_RX_OVRN_INT);
785 IFNET_STAT_INC(&sc->arpcom.ac_if, ierrors, 1);
790 if (status & IM_RCV_INT) {
795 packet_number = inw(BASE + FIFO_PORTS_REG_W);
797 if (packet_number & FIFO_REMPTY) {
800 * we got called , but nothing was on the FIFO
802 kprintf("sn: Receive interrupt with nothing on FIFO\n");
810 * An on-card memory allocation came through.
812 if (status & IM_ALLOC_INT) {
815 * Disable this interrupt.
817 mask &= ~IM_ALLOC_INT;
818 ifq_clr_oactive(&sc->arpcom.ac_if.if_snd);
819 snresume(&sc->arpcom.ac_if);
822 * TX Completion. Handle a transmit error message. This will only be
823 * called when there is an error, because of the AUTO_RELEASE mode.
825 if (status & IM_TX_INT) {
828 * Acknowlege Interrupt
831 outb(BASE + INTR_ACK_REG_B, IM_TX_INT);
833 packet_no = inw(BASE + FIFO_PORTS_REG_W);
834 packet_no &= FIFO_TX_MASK;
837 * select this as the packet to read from
839 outb(BASE + PACKET_NUM_REG_B, packet_no);
842 * Position the pointer to the first word from this packet
844 outw(BASE + POINTER_REG_W, PTR_AUTOINC | PTR_READ | 0x0000);
847 * Fetch the TX status word. The value found here will be a
848 * copy of the EPH_STATUS_REG_W at the time the transmit
851 tx_status = inw(BASE + DATA_REG_W);
853 if (tx_status & EPHSR_TX_SUC) {
854 device_printf(sc->dev,
855 "Successful packet caused interrupt\n");
857 IFNET_STAT_INC(&sc->arpcom.ac_if, oerrors, 1);
860 if (tx_status & EPHSR_LATCOL)
861 IFNET_STAT_INC(&sc->arpcom.ac_if, collisions, 1);
864 * Some of these errors will have disabled transmit.
865 * Re-enable transmit now.
870 outw(BASE + TXMIT_CONTROL_REG_W, TCR_ENABLE);
872 outw(BASE + TXMIT_CONTROL_REG_W, TCR_ENABLE | TCR_PAD_ENABLE);
876 * kill the failed packet. Wait for the MMU to be un-busy.
879 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */
881 outw(BASE + MMU_CMD_REG_W, MMUCR_FREEPKT);
884 * Attempt to queue more transmits.
886 ifq_clr_oactive(&sc->arpcom.ac_if.if_snd);
887 if_devstart(&sc->arpcom.ac_if);
890 * Transmit underrun. We use this opportunity to update transmit
891 * statistics from the card.
893 if (status & IM_TX_EMPTY_INT) {
896 * Acknowlege Interrupt
899 outb(BASE + INTR_ACK_REG_B, IM_TX_EMPTY_INT);
902 * Disable this interrupt.
904 mask &= ~IM_TX_EMPTY_INT;
907 card_stats = inw(BASE + COUNTER_REG_W);
912 IFNET_STAT_INC(&sc->arpcom.ac_if, collisions,
913 card_stats & ECR_COLN_MASK);
916 * Multiple collisions
918 IFNET_STAT_INC(&sc->arpcom.ac_if, collisions,
919 (card_stats & ECR_MCOLN_MASK) >> 4);
924 * Attempt to enqueue some more stuff.
926 ifq_clr_oactive(&sc->arpcom.ac_if.if_snd);
927 if_devstart(&sc->arpcom.ac_if);
930 * Some other error. Try to fix it by resetting the adapter.
932 if (status & IM_EPH_INT) {
939 * Handled all interrupt sources.
945 * Reestablish interrupts from mask which have not been deselected
946 * during this interrupt. Note that the hardware mask, which was set
947 * to 0x00 at the start of this service routine, may have been
948 * updated by one or more of the interrupt handers and we must let
949 * those new interrupts stay enabled here.
951 mask |= inb(BASE + INTR_MASK_REG_B);
952 outb(BASE + INTR_MASK_REG_B, mask);
953 sc->intr_mask = mask;
957 snread(struct ifnet *ifp)
959 struct sn_softc *sc = ifp->if_softc;
963 u_short packet_length;
968 packet_number = inw(BASE + FIFO_PORTS_REG_W);
970 if (packet_number & FIFO_REMPTY) {
973 * we got called , but nothing was on the FIFO
975 kprintf("sn: Receive interrupt with nothing on FIFO\n");
982 * Start reading from the start of the packet. Since PTR_RCV is set,
983 * packet number is found in FIFO_PORTS_REG_W, FIFO_RX_MASK.
985 outw(BASE + POINTER_REG_W, PTR_READ | PTR_RCV | PTR_AUTOINC | 0x0000);
988 * First two words are status and packet_length
990 status = inw(BASE + DATA_REG_W);
991 packet_length = inw(BASE + DATA_REG_W) & RLEN_MASK;
994 * The packet length contains 3 extra words: status, length, and a
995 * extra word with the control byte.
1000 * Account for receive errors and discard.
1002 if (status & RS_ERRORS) {
1003 IFNET_STAT_INC(ifp, ierrors, 1);
1007 * A packet is received.
1011 * Adjust for odd-length packet.
1013 if (status & RS_ODDFRAME)
1017 * Allocate a header mbuf from the kernel.
1019 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1023 m->m_pkthdr.rcvif = ifp;
1024 m->m_pkthdr.len = m->m_len = packet_length;
1027 * Attach an mbuf cluster
1029 MCLGET(m, MB_DONTWAIT);
1032 * Insist on getting a cluster
1034 if ((m->m_flags & M_EXT) == 0) {
1036 IFNET_STAT_INC(ifp, ierrors, 1);
1037 kprintf("sn: snread() kernel memory allocation problem\n");
1042 * Get packet, including link layer address, from interface.
1045 data = mtod(m, u_char *);
1046 insw(BASE + DATA_REG_W, data, packet_length >> 1);
1047 if (packet_length & 1) {
1048 data += packet_length & ~1;
1049 *data = inb(BASE + DATA_REG_B);
1051 IFNET_STAT_INC(ifp, ipackets, 1);
1053 m->m_pkthdr.len = m->m_len = packet_length;
1055 ifp->if_input(ifp, m);
1060 * Error or good, tell the card to get rid of this packet Wait for
1061 * the MMU to be un-busy.
1064 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */
1066 outw(BASE + MMU_CMD_REG_W, MMUCR_RELEASE);
1069 * Check whether another packet is ready
1071 packet_number = inw(BASE + FIFO_PORTS_REG_W);
1072 if (packet_number & FIFO_REMPTY) {
1080 * Handle IOCTLS. This function is completely stolen from if_ep.c
1081 * As with its progenitor, it does not handle hardware address
1085 snioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1087 struct sn_softc *sc = ifp->if_softc;
1092 if ((ifp->if_flags & IFF_UP) == 0 && ifp->if_flags & IFF_RUNNING) {
1093 ifp->if_flags &= ~IFF_RUNNING;
1097 /* reinitialize card on any parameter change */
1105 bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data,
1106 sizeof(sc->sc_addr));
1111 /* update multicast filter list. */
1116 /* update multicast filter list. */
1121 error = ether_ioctl(ifp, cmd, data);
1129 snreset(struct sn_softc *sc)
1136 snwatchdog(struct ifnet *ifp)
1138 sn_intr(ifp->if_softc);
1142 /* 1. zero the interrupt mask
1143 * 2. clear the enable receive flag
1144 * 3. clear the enable xmit flags
1147 snstop(struct sn_softc *sc)
1150 struct ifnet *ifp = &sc->arpcom.ac_if;
1153 * Clear interrupt mask; disable all interrupts.
1156 outb(BASE + INTR_MASK_REG_B, 0x00);
1159 * Disable transmitter and Receiver
1162 outw(BASE + RECV_CONTROL_REG_W, 0x0000);
1163 outw(BASE + TXMIT_CONTROL_REG_W, 0x0000);
1173 sn_activate(device_t dev)
1175 struct sn_softc *sc = device_get_softc(dev);
1178 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
1179 0, ~0, SMC_IO_EXTENT, RF_ACTIVE);
1180 if (!sc->port_res) {
1182 device_printf(dev, "Cannot allocate ioport\n");
1188 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
1192 device_printf(dev, "Cannot allocate irq\n");
1198 sc->sn_io_addr = rman_get_start(sc->port_res);
1203 sn_deactivate(device_t dev)
1205 struct sn_softc *sc = device_get_softc(dev);
1208 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1212 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
1219 * Function: sn_probe( device_t dev, int pccard )
1222 * Tests to see if a given ioaddr points to an SMC9xxx chip.
1223 * Tries to cause as little damage as possible if it's not a SMC chip.
1224 * Returns a 0 on success
1227 * (1) see if the high byte of BANK_SELECT is 0x33
1228 * (2) compare the ioaddr with the base register's address
1229 * (3) see if I recognize the chip ID in the appropriate register
1234 sn_probe(device_t dev, int pccard)
1236 struct sn_softc *sc = device_get_softc(dev);
1238 u_short revision_register;
1239 u_short base_address_register;
1243 if ((err = sn_activate(dev)) != 0)
1246 ioaddr = sc->sn_io_addr;
1249 * First, see if the high byte is 0x33
1251 bank = inw(ioaddr + BANK_SELECT_REG_W);
1252 if ((bank & BSR_DETECT_MASK) != BSR_DETECT_VALUE) {
1254 device_printf(dev, "test1 failed\n");
1259 * The above MIGHT indicate a device, but I need to write to further
1260 * test this. Go to bank 0, then test that the register still
1261 * reports the high byte is 0x33.
1263 outw(ioaddr + BANK_SELECT_REG_W, 0x0000);
1264 bank = inw(ioaddr + BANK_SELECT_REG_W);
1265 if ((bank & BSR_DETECT_MASK) != BSR_DETECT_VALUE) {
1267 device_printf(dev, "test2 failed\n");
1272 * well, we've already written once, so hopefully another time won't
1273 * hurt. This time, I need to switch the bank register to bank 1, so
1274 * I can access the base address register. The contents of the
1275 * BASE_ADDR_REG_W register, after some jiggery pokery, is expected
1276 * to match the I/O port address where the adapter is being probed.
1278 outw(ioaddr + BANK_SELECT_REG_W, 0x0001);
1279 base_address_register = inw(ioaddr + BASE_ADDR_REG_W);
1282 * This test is nonsence on PC-card architecture, so if
1283 * pccard == 1, skip this test. (hosokawa)
1285 if (!pccard && (ioaddr != (base_address_register >> 3 & 0x3E0))) {
1288 * Well, the base address register didn't match. Must not
1289 * have been a SMC chip after all.
1292 * kprintf("sn: ioaddr %x doesn't match card configuration
1293 * (%x)\n", ioaddr, base_address_register >> 3 & 0x3E0 );
1297 device_printf(dev, "test3 failed ioaddr = 0x%x, "
1298 "base_address_register = 0x%x\n", ioaddr,
1299 base_address_register >> 3 & 0x3E0);
1304 * Check if the revision register is something that I recognize.
1305 * These might need to be added to later, as future revisions could
1308 outw(ioaddr + BANK_SELECT_REG_W, 0x3);
1309 revision_register = inw(ioaddr + REVISION_REG_W);
1310 if (!chip_ids[(revision_register >> 4) & 0xF]) {
1313 * I don't regonize this chip, so...
1316 device_printf(dev, "test4 failed\n");
1321 * at this point I'll assume that the chip is an SMC9xxx. It might be
1322 * prudent to check a listing of MAC addresses against the hardware
1323 * address, or do some other tests.
1335 sn_setmcast(struct sn_softc *sc)
1337 struct ifnet *ifp = (struct ifnet *)sc;
1341 * Set the receiver filter. We want receive enabled and auto strip
1342 * of CRC from received packet. If we are promiscuous then set that
1345 flags = RCR_ENABLE | RCR_STRIP_CRC;
1347 if (ifp->if_flags & IFF_PROMISC) {
1348 flags |= RCR_PROMISC | RCR_ALMUL;
1349 } else if (ifp->if_flags & IFF_ALLMULTI) {
1353 if (sn_getmcf(&sc->arpcom, mcf)) {
1356 outw(BASE + MULTICAST1_REG_W,
1357 ((u_short)mcf[1] << 8) | mcf[0]);
1358 outw(BASE + MULTICAST2_REG_W,
1359 ((u_short)mcf[3] << 8) | mcf[2]);
1360 outw(BASE + MULTICAST3_REG_W,
1361 ((u_short)mcf[5] << 8) | mcf[4]);
1362 outw(BASE + MULTICAST4_REG_W,
1363 ((u_short)mcf[7] << 8) | mcf[6]);
1369 outw(BASE + RECV_CONTROL_REG_W, flags);
1373 sn_getmcf(struct arpcom *ac, u_char *mcf)
1376 u_int index, index2;
1378 struct ifmultiaddr *ifma;
1382 TAILQ_FOREACH(ifma, &ac->ac_if.if_multiaddrs, ifma_link) {
1383 if (ifma->ifma_addr->sa_family != AF_LINK)
1385 index = smc_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)) & 0x3f;
1387 for (i = 0; i < 6; i++) {
1389 index2 |= (index & 0x01);
1392 af[index2 >> 3] |= 1 << (index2 & 7);
1394 return 1; /* use multicast filter */
1402 const u_int poly = 0xedb88320;
1403 u_int v = 0xffffffff;
1406 for (perByte = 0; perByte < ETHER_ADDR_LEN; perByte++) {
1408 for (perBit = 0; perBit < 8; perBit++) {
1409 v = (v >> 1)^(((v ^ c) & 0x01) ? poly : 0);