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 $
32 * $DragonFly: src/sys/dev/netif/sn/if_sn.c,v 1.3 2003/07/26 14:25:25 rob Exp $
36 * This is a driver for SMC's 9000 series of Ethernet adapters.
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.
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)
49 * "Features" of the SMC chip:
50 * 4608 byte packet memory. (for the 91C92. Others have more)
51 * EEPROM for configuration
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
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"
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.
73 * Modifications for Megahertz X-Jack Ethernet Card (XJ-10BT)
75 * Copyright (c) 1996 by Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>
76 * BSD-nomads, Tokyo, Japan.
79 * Multicast support by Kei TANAKA <kei@pal.xerox.com>
80 * Special thanks to itojun@itojun.org
83 #undef SN_DEBUG /* (by hosokawa) */
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>
92 #include <sys/socket.h>
93 #include <sys/syslog.h>
95 #include <sys/module.h>
98 #include <machine/bus.h>
99 #include <machine/resource.h>
100 #include <sys/rman.h>
102 #include <net/ethernet.h>
104 #include <net/if_arp.h>
105 #include <net/if_dl.h>
106 #include <net/if_types.h>
107 #include <net/if_mib.h>
110 #include <netinet/in.h>
111 #include <netinet/in_systm.h>
112 #include <netinet/in_var.h>
113 #include <netinet/ip.h>
117 #include <netns/ns.h>
118 #include <netns/ns_if.h>
122 #include <net/bpfdesc.h>
124 #include <machine/clock.h>
126 #include <dev/sn/if_snreg.h>
127 #include <dev/sn/if_snvar.h>
129 /* Exported variables */
130 devclass_t sn_devclass;
132 static int snioctl(struct ifnet * ifp, u_long, caddr_t);
134 static void snresume(struct ifnet *);
137 void snread(struct ifnet *);
138 void snreset(struct sn_softc *);
139 void snstart(struct ifnet *);
140 void snstop(struct sn_softc *);
141 void snwatchdog(struct ifnet *);
143 static void sn_setmcast(struct sn_softc *);
144 static int sn_getmcf(struct arpcom *ac, u_char *mcf);
145 static u_int smc_crc(u_char *);
147 /* I (GB) have been unlucky getting the hardware padding
152 static const char *chip_ids[15] = {
154 /* 3 */ "SMC91C90/91C92",
159 /* 8 */ "SMC91C100FD",
165 sn_attach(device_t dev)
167 struct sn_softc *sc = device_get_softc(dev);
168 struct ifnet *ifp = &sc->arpcom.ac_if;
172 struct sockaddr_dl *sdl;
182 sc->pages_wanted = -1;
184 device_printf(dev, " ");
187 rev = inw(BASE + REVISION_REG_W);
188 if (chip_ids[(rev >> 4) & 0xF])
189 printf("%s ", chip_ids[(rev >> 4) & 0xF]);
192 i = inw(BASE + CONFIG_REG_W);
193 printf(i & CR_AUI_SELECT ? "AUI" : "UTP");
195 if (sc->pccard_enaddr)
196 for (j = 0; j < 3; j++) {
199 w = (u_short)sc->arpcom.ac_enaddr[j * 2] |
200 (((u_short)sc->arpcom.ac_enaddr[j * 2 + 1]) << 8);
201 outw(BASE + IAR_ADDR0_REG_W + j * 2, w);
205 * Read the station address from the chip. The MAC address is bank 1,
209 p = (u_char *) & sc->arpcom.ac_enaddr;
210 for (i = 0; i < 6; i += 2) {
211 address = inw(BASE + IAR_ADDR0_REG_W + i);
212 p[i + 1] = address >> 8;
213 p[i] = address & 0xFF;
215 printf(" MAC address %6D\n", sc->arpcom.ac_enaddr, ":");
217 ifp->if_unit = device_get_unit(dev);
219 ifp->if_mtu = ETHERMTU;
220 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
221 ifp->if_output = ether_output;
222 ifp->if_start = snstart;
223 ifp->if_ioctl = snioctl;
224 ifp->if_watchdog = snwatchdog;
225 ifp->if_init = sninit;
226 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
229 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
232 * Fill the hardware address into ifa_addr if we find an AF_LINK
233 * entry. We need to do this so bpf's can get the hardware addr of
234 * this card. netstat likes this too!
236 ifa = TAILQ_FIRST(&ifp->if_addrhead);
237 while ((ifa != 0) && (ifa->ifa_addr != 0) &&
238 (ifa->ifa_addr->sa_family != AF_LINK))
239 ifa = TAILQ_NEXT(ifa, ifa_link);
241 if ((ifa != 0) && (ifa->ifa_addr != 0)) {
242 sdl = (struct sockaddr_dl *) ifa->ifa_addr;
243 sdl->sdl_type = IFT_ETHER;
244 sdl->sdl_alen = ETHER_ADDR_LEN;
246 bcopy(sc->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
254 * Reset and initialize the chip
259 struct sn_softc *sc = xsc;
260 struct ifnet *ifp = &sc->arpcom.ac_if;
268 * This resets the registers mostly to defaults, but doesn't affect
269 * EEPROM. After the reset cycle, we pause briefly for the chip to
273 outw(BASE + RECV_CONTROL_REG_W, RCR_SOFTRESET);
275 outw(BASE + RECV_CONTROL_REG_W, 0x0000);
279 outw(BASE + TXMIT_CONTROL_REG_W, 0x0000);
282 * Set the control register to automatically release succesfully
283 * transmitted packets (making the best use out of our limited
284 * memory) and to enable the EPH interrupt on certain TX errors.
287 outw(BASE + CONTROL_REG_W, (CTR_AUTO_RELEASE | CTR_TE_ENABLE |
288 CTR_CR_ENABLE | CTR_LE_ENABLE));
290 /* Set squelch level to 240mV (default 480mV) */
291 flags = inw(BASE + CONFIG_REG_W);
292 flags |= CR_SET_SQLCH;
293 outw(BASE + CONFIG_REG_W, flags);
296 * Reset the MMU and wait for it to be un-busy.
299 outw(BASE + MMU_CMD_REG_W, MMUCR_RESET);
300 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */
304 * Disable all interrupts
306 outb(BASE + INTR_MASK_REG_B, 0x00);
311 * Set the transmitter control. We want it enabled.
317 * I (GB) have been unlucky getting this to work.
319 flags |= TCR_PAD_ENABLE;
322 outw(BASE + TXMIT_CONTROL_REG_W, flags);
326 * Now, enable interrupts
335 outb(BASE + INTR_MASK_REG_B, mask);
336 sc->intr_mask = mask;
337 sc->pages_wanted = -1;
341 * Mark the interface running but not active.
343 ifp->if_flags |= IFF_RUNNING;
344 ifp->if_flags &= ~IFF_OACTIVE;
347 * Attempt to push out any waiting packets.
356 snstart(struct ifnet *ifp)
358 struct sn_softc *sc = ifp->if_softc;
371 if (sc->arpcom.ac_if.if_flags & IFF_OACTIVE) {
375 if (sc->pages_wanted != -1) {
377 printf("sn%d: snstart() while memory allocation pending\n",
384 * Sneak a peek at the next packet
386 m = sc->arpcom.ac_if.if_snd.ifq_head;
392 * Compute the frame length and set pad to give an overall even
393 * number of bytes. Below we assume that the packet length is even.
395 for (len = 0, top = m; m; m = m->m_next)
401 * We drop packets that are too large. Perhaps we should truncate
404 if (len + pad > ETHER_MAX_LEN - ETHER_CRC_LEN) {
405 printf("sn%d: large packet discarded (A)\n", ifp->if_unit);
406 ++sc->arpcom.ac_if.if_oerrors;
407 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
414 * If HW padding is not turned on, then pad to ETHER_MIN_LEN.
416 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
417 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
424 * The MMU wants the number of pages to be the number of 256 byte
425 * 'pages', minus 1 (A packet can't ever have 0 pages. We also
426 * include space for the status word, byte count and control bytes in
427 * the allocation request.
429 numPages = (length + 6) >> 8;
433 * Now, try to allocate the memory
436 outw(BASE + MMU_CMD_REG_W, MMUCR_ALLOC | numPages);
439 * Wait a short amount of time to see if the allocation request
440 * completes. Otherwise, I enable the interrupt and wait for
441 * completion asyncronously.
444 time_out = MEMORY_WAIT_TIME;
446 if (inb(BASE + INTR_STAT_REG_B) & IM_ALLOC_INT)
448 } while (--time_out);
453 * No memory now. Oh well, wait until the chip finds memory
454 * later. Remember how many pages we were asking for and
455 * enable the allocation completion interrupt. Also set a
456 * watchdog in case we miss the interrupt. We mark the
457 * interface active since there is no point in attempting an
458 * snstart() until after the memory is available.
460 mask = inb(BASE + INTR_MASK_REG_B) | IM_ALLOC_INT;
461 outb(BASE + INTR_MASK_REG_B, mask);
462 sc->intr_mask = mask;
464 sc->arpcom.ac_if.if_timer = 1;
465 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
466 sc->pages_wanted = numPages;
472 * The memory allocation completed. Check the results.
474 packet_no = inb(BASE + ALLOC_RESULT_REG_B);
475 if (packet_no & ARR_FAILED) {
476 printf("sn%d: Memory allocation failed\n", ifp->if_unit);
480 * We have a packet number, so tell the card to use it.
482 outb(BASE + PACKET_NUM_REG_B, packet_no);
485 * Point to the beginning of the packet
487 outw(BASE + POINTER_REG_W, PTR_AUTOINC | 0x0000);
490 * Send the packet length (+6 for status, length and control byte)
491 * and the status word (set to zeros)
493 outw(BASE + DATA_REG_W, 0);
494 outb(BASE + DATA_REG_B, (length + 6) & 0xFF);
495 outb(BASE + DATA_REG_B, (length + 6) >> 8);
498 * Get the packet from the kernel. This will include the Ethernet
499 * frame header, MAC Addresses etc.
501 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
504 * Push out the data to the card.
506 for (top = m; m != 0; m = m->m_next) {
511 outsw(BASE + DATA_REG_W, mtod(m, caddr_t), m->m_len / 2);
514 * Push out remaining byte.
517 outb(BASE + DATA_REG_B, *(mtod(m, caddr_t) + m->m_len - 1));
524 outw(BASE + DATA_REG_W, 0);
528 outb(BASE + DATA_REG_B, 0);
531 * Push out control byte and unused packet byte The control byte is 0
532 * meaning the packet is even lengthed and no special CRC handling is
535 outw(BASE + DATA_REG_W, 0);
538 * Enable the interrupts and let the chipset deal with it Also set a
539 * watchdog in case we miss the interrupt.
541 mask = inb(BASE + INTR_MASK_REG_B) | (IM_TX_INT | IM_TX_EMPTY_INT);
542 outb(BASE + INTR_MASK_REG_B, mask);
543 sc->intr_mask = mask;
545 outw(BASE + MMU_CMD_REG_W, MMUCR_ENQUEUE);
547 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
548 sc->arpcom.ac_if.if_timer = 1;
554 sc->arpcom.ac_if.if_opackets++;
561 * Is another packet coming in? We don't want to overflow the tiny
562 * RX FIFO. If nothing has arrived then attempt to queue another
565 if (inw(BASE + FIFO_PORTS_REG_W) & FIFO_REMPTY)
574 /* Resume a packet transmit operation after a memory allocation
577 * This is basically a hacked up copy of snstart() which handles
578 * a completed memory allocation the same way snstart() does.
579 * It then passes control to snstart to handle any other queued
583 snresume(struct ifnet *ifp)
585 struct sn_softc *sc = ifp->if_softc;
593 u_short pages_wanted;
596 if (sc->pages_wanted < 0)
599 pages_wanted = sc->pages_wanted;
600 sc->pages_wanted = -1;
603 * Sneak a peek at the next packet
605 m = sc->arpcom.ac_if.if_snd.ifq_head;
607 printf("sn%d: snresume() with nothing to send\n", ifp->if_unit);
611 * Compute the frame length and set pad to give an overall even
612 * number of bytes. Below we assume that the packet length is even.
614 for (len = 0, top = m; m; m = m->m_next)
620 * We drop packets that are too large. Perhaps we should truncate
623 if (len + pad > ETHER_MAX_LEN - ETHER_CRC_LEN) {
624 printf("sn%d: large packet discarded (B)\n", ifp->if_unit);
625 ++sc->arpcom.ac_if.if_oerrors;
626 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
633 * If HW padding is not turned on, then pad to ETHER_MIN_LEN.
635 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
636 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
644 * The MMU wants the number of pages to be the number of 256 byte
645 * 'pages', minus 1 (A packet can't ever have 0 pages. We also
646 * include space for the status word, byte count and control bytes in
647 * the allocation request.
649 numPages = (length + 6) >> 8;
655 * The memory allocation completed. Check the results. If it failed,
656 * we simply set a watchdog timer and hope for the best.
658 packet_no = inb(BASE + ALLOC_RESULT_REG_B);
659 if (packet_no & ARR_FAILED) {
660 printf("sn%d: Memory allocation failed. Weird.\n", ifp->if_unit);
661 sc->arpcom.ac_if.if_timer = 1;
665 * We have a packet number, so tell the card to use it.
667 outb(BASE + PACKET_NUM_REG_B, packet_no);
670 * Now, numPages should match the pages_wanted recorded when the
671 * memory allocation was initiated.
673 if (pages_wanted != numPages) {
674 printf("sn%d: memory allocation wrong size. Weird.\n", ifp->if_unit);
676 * If the allocation was the wrong size we simply release the
677 * memory once it is granted. Wait for the MMU to be un-busy.
679 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */
681 outw(BASE + MMU_CMD_REG_W, MMUCR_FREEPKT);
686 * Point to the beginning of the packet
688 outw(BASE + POINTER_REG_W, PTR_AUTOINC | 0x0000);
691 * Send the packet length (+6 for status, length and control byte)
692 * and the status word (set to zeros)
694 outw(BASE + DATA_REG_W, 0);
695 outb(BASE + DATA_REG_B, (length + 6) & 0xFF);
696 outb(BASE + DATA_REG_B, (length + 6) >> 8);
699 * Get the packet from the kernel. This will include the Ethernet
700 * frame header, MAC Addresses etc.
702 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
705 * Push out the data to the card.
707 for (top = m; m != 0; m = m->m_next) {
712 outsw(BASE + DATA_REG_W, mtod(m, caddr_t), m->m_len / 2);
715 * Push out remaining byte.
718 outb(BASE + DATA_REG_B, *(mtod(m, caddr_t) + m->m_len - 1));
725 outw(BASE + DATA_REG_W, 0);
729 outb(BASE + DATA_REG_B, 0);
732 * Push out control byte and unused packet byte The control byte is 0
733 * meaning the packet is even lengthed and no special CRC handling is
736 outw(BASE + DATA_REG_W, 0);
739 * Enable the interrupts and let the chipset deal with it Also set a
740 * watchdog in case we miss the interrupt.
742 mask = inb(BASE + INTR_MASK_REG_B) | (IM_TX_INT | IM_TX_EMPTY_INT);
743 outb(BASE + INTR_MASK_REG_B, mask);
744 sc->intr_mask = mask;
745 outw(BASE + MMU_CMD_REG_W, MMUCR_ENQUEUE);
751 sc->arpcom.ac_if.if_opackets++;
757 * Now pass control to snstart() to queue any additional packets
759 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
763 * We've sent something, so we're active. Set a watchdog in case the
764 * TX_EMPTY interrupt is lost.
766 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
767 sc->arpcom.ac_if.if_timer = 1;
776 int status, interrupts;
777 struct sn_softc *sc = (struct sn_softc *) arg;
778 struct ifnet *ifp = &sc->arpcom.ac_if;
782 * Chip state registers
790 * if_ep.c did this, so I do too. Yet if_ed.c doesn't. I wonder...
795 * Clear the watchdog.
802 * Obtain the current interrupt mask and clear the hardware mask
803 * while servicing interrupts.
805 mask = inb(BASE + INTR_MASK_REG_B);
806 outb(BASE + INTR_MASK_REG_B, 0x00);
809 * Get the set of interrupts which occurred and eliminate any which
812 interrupts = inb(BASE + INTR_STAT_REG_B);
813 status = interrupts & mask;
816 * Now, process each of the interrupt types.
822 if (status & IM_RX_OVRN_INT) {
825 * Acknowlege Interrupt
828 outb(BASE + INTR_ACK_REG_B, IM_RX_OVRN_INT);
830 ++sc->arpcom.ac_if.if_ierrors;
835 if (status & IM_RCV_INT) {
840 packet_number = inw(BASE + FIFO_PORTS_REG_W);
842 if (packet_number & FIFO_REMPTY) {
845 * we got called , but nothing was on the FIFO
847 printf("sn: Receive interrupt with nothing on FIFO\n");
855 * An on-card memory allocation came through.
857 if (status & IM_ALLOC_INT) {
860 * Disable this interrupt.
862 mask &= ~IM_ALLOC_INT;
863 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
864 snresume(&sc->arpcom.ac_if);
867 * TX Completion. Handle a transmit error message. This will only be
868 * called when there is an error, because of the AUTO_RELEASE mode.
870 if (status & IM_TX_INT) {
873 * Acknowlege Interrupt
876 outb(BASE + INTR_ACK_REG_B, IM_TX_INT);
878 packet_no = inw(BASE + FIFO_PORTS_REG_W);
879 packet_no &= FIFO_TX_MASK;
882 * select this as the packet to read from
884 outb(BASE + PACKET_NUM_REG_B, packet_no);
887 * Position the pointer to the first word from this packet
889 outw(BASE + POINTER_REG_W, PTR_AUTOINC | PTR_READ | 0x0000);
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
896 tx_status = inw(BASE + DATA_REG_W);
898 if (tx_status & EPHSR_TX_SUC) {
899 device_printf(sc->dev,
900 "Successful packet caused interrupt\n");
902 ++sc->arpcom.ac_if.if_oerrors;
905 if (tx_status & EPHSR_LATCOL)
906 ++sc->arpcom.ac_if.if_collisions;
909 * Some of these errors will have disabled transmit.
910 * Re-enable transmit now.
915 outw(BASE + TXMIT_CONTROL_REG_W, TCR_ENABLE);
917 outw(BASE + TXMIT_CONTROL_REG_W, TCR_ENABLE | TCR_PAD_ENABLE);
921 * kill the failed packet. Wait for the MMU to be un-busy.
924 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */
926 outw(BASE + MMU_CMD_REG_W, MMUCR_FREEPKT);
929 * Attempt to queue more transmits.
931 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
932 snstart(&sc->arpcom.ac_if);
935 * Transmit underrun. We use this opportunity to update transmit
936 * statistics from the card.
938 if (status & IM_TX_EMPTY_INT) {
941 * Acknowlege Interrupt
944 outb(BASE + INTR_ACK_REG_B, IM_TX_EMPTY_INT);
947 * Disable this interrupt.
949 mask &= ~IM_TX_EMPTY_INT;
952 card_stats = inw(BASE + COUNTER_REG_W);
957 sc->arpcom.ac_if.if_collisions += card_stats & ECR_COLN_MASK;
960 * Multiple collisions
962 sc->arpcom.ac_if.if_collisions += (card_stats & ECR_MCOLN_MASK) >> 4;
967 * Attempt to enqueue some more stuff.
969 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
970 snstart(&sc->arpcom.ac_if);
973 * Some other error. Try to fix it by resetting the adapter.
975 if (status & IM_EPH_INT) {
982 * Handled all interrupt sources.
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.
994 mask |= inb(BASE + INTR_MASK_REG_B);
995 outb(BASE + INTR_MASK_REG_B, mask);
996 sc->intr_mask = mask;
1002 snread(struct ifnet *ifp)
1004 struct sn_softc *sc = ifp->if_softc;
1005 struct ether_header *eh;
1009 u_short packet_length;
1014 packet_number = inw(BASE + FIFO_PORTS_REG_W);
1016 if (packet_number & FIFO_REMPTY) {
1019 * we got called , but nothing was on the FIFO
1021 printf("sn: Receive interrupt with nothing on FIFO\n");
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.
1031 outw(BASE + POINTER_REG_W, PTR_READ | PTR_RCV | PTR_AUTOINC | 0x0000);
1034 * First two words are status and packet_length
1036 status = inw(BASE + DATA_REG_W);
1037 packet_length = inw(BASE + DATA_REG_W) & RLEN_MASK;
1040 * The packet length contains 3 extra words: status, length, and a
1041 * extra word with the control byte.
1046 * Account for receive errors and discard.
1048 if (status & RS_ERRORS) {
1049 ++sc->arpcom.ac_if.if_ierrors;
1053 * A packet is received.
1057 * Adjust for odd-length packet.
1059 if (status & RS_ODDFRAME)
1063 * Allocate a header mbuf from the kernel.
1065 MGETHDR(m, M_DONTWAIT, MT_DATA);
1069 m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
1070 m->m_pkthdr.len = m->m_len = packet_length;
1073 * Attach an mbuf cluster
1075 MCLGET(m, M_DONTWAIT);
1078 * Insist on getting a cluster
1080 if ((m->m_flags & M_EXT) == 0) {
1082 ++sc->arpcom.ac_if.if_ierrors;
1083 printf("sn: snread() kernel memory allocation problem\n");
1086 eh = mtod(m, struct ether_header *);
1089 * Get packet, including link layer address, from interface.
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);
1098 ++sc->arpcom.ac_if.if_ipackets;
1101 * Remove link layer addresses and whatnot.
1103 m->m_pkthdr.len = m->m_len = packet_length - sizeof(struct ether_header);
1104 m->m_data += sizeof(struct ether_header);
1106 ether_input(&sc->arpcom.ac_if, eh, m);
1111 * Error or good, tell the card to get rid of this packet Wait for
1112 * the MMU to be un-busy.
1115 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */
1117 outw(BASE + MMU_CMD_REG_W, MMUCR_RELEASE);
1120 * Check whether another packet is ready
1122 packet_number = inw(BASE + FIFO_PORTS_REG_W);
1123 if (packet_number & FIFO_REMPTY) {
1131 * Handle IOCTLS. This function is completely stolen from if_ep.c
1132 * As with its progenitor, it does not handle hardware address
1136 snioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1138 struct sn_softc *sc = ifp->if_softc;
1147 error = ether_ioctl(ifp, cmd, data);
1151 if ((ifp->if_flags & IFF_UP) == 0 && ifp->if_flags & IFF_RUNNING) {
1152 ifp->if_flags &= ~IFF_RUNNING;
1156 /* reinitialize card on any parameter change */
1164 bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data,
1165 sizeof(sc->sc_addr));
1170 /* update multicast filter list. */
1175 /* update multicast filter list. */
1189 snreset(struct sn_softc *sc)
1201 snwatchdog(struct ifnet *ifp)
1205 sn_intr(ifp->if_softc);
1210 /* 1. zero the interrupt mask
1211 * 2. clear the enable receive flag
1212 * 3. clear the enable xmit flags
1215 snstop(struct sn_softc *sc)
1218 struct ifnet *ifp = &sc->arpcom.ac_if;
1221 * Clear interrupt mask; disable all interrupts.
1224 outb(BASE + INTR_MASK_REG_B, 0x00);
1227 * Disable transmitter and Receiver
1230 outw(BASE + RECV_CONTROL_REG_W, 0x0000);
1231 outw(BASE + TXMIT_CONTROL_REG_W, 0x0000);
1241 sn_activate(device_t dev)
1243 struct sn_softc *sc = device_get_softc(dev);
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) {
1251 device_printf(dev, "Cannot allocate ioport\n");
1257 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
1258 0, ~0, 1, RF_ACTIVE);
1261 device_printf(dev, "Cannot allocate irq\n");
1266 if ((err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET, sn_intr, sc,
1267 &sc->intrhand)) != 0) {
1272 sc->sn_io_addr = rman_get_start(sc->port_res);
1277 sn_deactivate(device_t dev)
1279 struct sn_softc *sc = device_get_softc(dev);
1282 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
1285 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1289 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
1296 * Function: sn_probe( device_t dev, int pccard )
1299 * Tests to see if a given ioaddr points to an SMC9xxx chip.
1300 * Tries to cause as little damage as possible if it's not a SMC chip.
1301 * Returns a 0 on success
1304 * (1) see if the high byte of BANK_SELECT is 0x33
1305 * (2) compare the ioaddr with the base register's address
1306 * (3) see if I recognize the chip ID in the appropriate register
1311 sn_probe(device_t dev, int pccard)
1313 struct sn_softc *sc = device_get_softc(dev);
1315 u_short revision_register;
1316 u_short base_address_register;
1320 if ((err = sn_activate(dev)) != 0)
1323 ioaddr = sc->sn_io_addr;
1326 * First, see if the high byte is 0x33
1328 bank = inw(ioaddr + BANK_SELECT_REG_W);
1329 if ((bank & BSR_DETECT_MASK) != BSR_DETECT_VALUE) {
1331 device_printf(dev, "test1 failed\n");
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.
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) {
1344 device_printf(dev, "test2 failed\n");
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.
1355 outw(ioaddr + BANK_SELECT_REG_W, 0x0001);
1356 base_address_register = inw(ioaddr + BASE_ADDR_REG_W);
1359 * This test is nonsence on PC-card architecture, so if
1360 * pccard == 1, skip this test. (hosokawa)
1362 if (!pccard && (ioaddr != (base_address_register >> 3 & 0x3E0))) {
1365 * Well, the base address register didn't match. Must not
1366 * have been a SMC chip after all.
1369 * printf("sn: ioaddr %x doesn't match card configuration
1370 * (%x)\n", ioaddr, base_address_register >> 3 & 0x3E0 );
1374 device_printf(dev, "test3 failed ioaddr = 0x%x, "
1375 "base_address_register = 0x%x\n", ioaddr,
1376 base_address_register >> 3 & 0x3E0);
1381 * Check if the revision register is something that I recognize.
1382 * These might need to be added to later, as future revisions could
1385 outw(ioaddr + BANK_SELECT_REG_W, 0x3);
1386 revision_register = inw(ioaddr + REVISION_REG_W);
1387 if (!chip_ids[(revision_register >> 4) & 0xF]) {
1390 * I don't regonize this chip, so...
1393 device_printf(dev, "test4 failed\n");
1398 * at this point I'll assume that the chip is an SMC9xxx. It might be
1399 * prudent to check a listing of MAC addresses against the hardware
1400 * address, or do some other tests.
1412 sn_setmcast(struct sn_softc *sc)
1414 struct ifnet *ifp = (struct ifnet *)sc;
1418 * Set the receiver filter. We want receive enabled and auto strip
1419 * of CRC from received packet. If we are promiscuous then set that
1422 flags = RCR_ENABLE | RCR_STRIP_CRC;
1424 if (ifp->if_flags & IFF_PROMISC) {
1425 flags |= RCR_PROMISC | RCR_ALMUL;
1426 } else if (ifp->if_flags & IFF_ALLMULTI) {
1430 if (sn_getmcf(&sc->arpcom, mcf)) {
1433 outw(BASE + MULTICAST1_REG_W,
1434 ((u_short)mcf[1] << 8) | mcf[0]);
1435 outw(BASE + MULTICAST2_REG_W,
1436 ((u_short)mcf[3] << 8) | mcf[2]);
1437 outw(BASE + MULTICAST3_REG_W,
1438 ((u_short)mcf[5] << 8) | mcf[4]);
1439 outw(BASE + MULTICAST4_REG_W,
1440 ((u_short)mcf[7] << 8) | mcf[6]);
1446 outw(BASE + RECV_CONTROL_REG_W, flags);
1450 sn_getmcf(struct arpcom *ac, u_char *mcf)
1453 u_int index, index2;
1454 u_char *af = (u_char *) mcf;
1455 struct ifmultiaddr *ifma;
1459 for (ifma = ac->ac_if.if_multiaddrs.lh_first; ifma;
1460 ifma = ifma->ifma_link.le_next) {
1461 if (ifma->ifma_addr->sa_family != AF_LINK)
1463 index = smc_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)) & 0x3f;
1465 for (i = 0; i < 6; i++) {
1467 index2 |= (index & 0x01);
1470 af[index2 >> 3] |= 1 << (index2 & 7);
1472 return 1; /* use multicast filter */
1480 const u_int poly = 0xedb88320;
1481 u_int v = 0xffffffff;
1484 for (perByte = 0; perByte < ETHER_ADDR_LEN; perByte++) {
1486 for (perBit = 0; perBit < 8; perBit++) {
1487 v = (v >> 1)^(((v ^ c) & 0x01) ? poly : 0);