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/errno.h>
88 #include <sys/sockio.h>
89 #include <sys/malloc.h>
91 #include <sys/socket.h>
92 #include <sys/syslog.h>
94 #include <sys/module.h>
97 #include <machine/bus.h>
98 #include <machine/resource.h>
101 #include <net/ethernet.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 <netns/ns.h>
117 #include <netns/ns_if.h>
121 #include <net/bpfdesc.h>
123 #include <machine/clock.h>
125 #include <dev/sn/if_snreg.h>
126 #include <dev/sn/if_snvar.h>
128 /* Exported variables */
129 devclass_t sn_devclass;
131 static int snioctl(struct ifnet * ifp, u_long, caddr_t);
133 static void snresume(struct ifnet *);
136 void snread(struct ifnet *);
137 void snreset(struct sn_softc *);
138 void snstart(struct ifnet *);
139 void snstop(struct sn_softc *);
140 void snwatchdog(struct ifnet *);
142 static void sn_setmcast(struct sn_softc *);
143 static int sn_getmcf(struct arpcom *ac, u_char *mcf);
144 static u_int smc_crc(u_char *);
146 /* I (GB) have been unlucky getting the hardware padding
151 static const char *chip_ids[15] = {
153 /* 3 */ "SMC91C90/91C92",
158 /* 8 */ "SMC91C100FD",
164 sn_attach(device_t dev)
166 struct sn_softc *sc = device_get_softc(dev);
167 struct ifnet *ifp = &sc->arpcom.ac_if;
171 struct sockaddr_dl *sdl;
181 sc->pages_wanted = -1;
183 device_printf(dev, " ");
186 rev = inw(BASE + REVISION_REG_W);
187 if (chip_ids[(rev >> 4) & 0xF])
188 printf("%s ", chip_ids[(rev >> 4) & 0xF]);
191 i = inw(BASE + CONFIG_REG_W);
192 printf(i & CR_AUI_SELECT ? "AUI" : "UTP");
194 if (sc->pccard_enaddr)
195 for (j = 0; j < 3; j++) {
198 w = (u_short)sc->arpcom.ac_enaddr[j * 2] |
199 (((u_short)sc->arpcom.ac_enaddr[j * 2 + 1]) << 8);
200 outw(BASE + IAR_ADDR0_REG_W + j * 2, w);
204 * Read the station address from the chip. The MAC address is bank 1,
208 p = (u_char *) & sc->arpcom.ac_enaddr;
209 for (i = 0; i < 6; i += 2) {
210 address = inw(BASE + IAR_ADDR0_REG_W + i);
211 p[i + 1] = address >> 8;
212 p[i] = address & 0xFF;
214 printf(" MAC address %6D\n", sc->arpcom.ac_enaddr, ":");
216 ifp->if_unit = device_get_unit(dev);
218 ifp->if_mtu = ETHERMTU;
219 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
220 ifp->if_output = ether_output;
221 ifp->if_start = snstart;
222 ifp->if_ioctl = snioctl;
223 ifp->if_watchdog = snwatchdog;
224 ifp->if_init = sninit;
225 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
228 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
231 * Fill the hardware address into ifa_addr if we find an AF_LINK
232 * entry. We need to do this so bpf's can get the hardware addr of
233 * this card. netstat likes this too!
235 ifa = TAILQ_FIRST(&ifp->if_addrhead);
236 while ((ifa != 0) && (ifa->ifa_addr != 0) &&
237 (ifa->ifa_addr->sa_family != AF_LINK))
238 ifa = TAILQ_NEXT(ifa, ifa_link);
240 if ((ifa != 0) && (ifa->ifa_addr != 0)) {
241 sdl = (struct sockaddr_dl *) ifa->ifa_addr;
242 sdl->sdl_type = IFT_ETHER;
243 sdl->sdl_alen = ETHER_ADDR_LEN;
245 bcopy(sc->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
253 * Reset and initialize the chip
258 register struct sn_softc *sc = xsc;
259 register struct ifnet *ifp = &sc->arpcom.ac_if;
267 * This resets the registers mostly to defaults, but doesn't affect
268 * EEPROM. After the reset cycle, we pause briefly for the chip to
272 outw(BASE + RECV_CONTROL_REG_W, RCR_SOFTRESET);
274 outw(BASE + RECV_CONTROL_REG_W, 0x0000);
278 outw(BASE + TXMIT_CONTROL_REG_W, 0x0000);
281 * Set the control register to automatically release succesfully
282 * transmitted packets (making the best use out of our limited
283 * memory) and to enable the EPH interrupt on certain TX errors.
286 outw(BASE + CONTROL_REG_W, (CTR_AUTO_RELEASE | CTR_TE_ENABLE |
287 CTR_CR_ENABLE | CTR_LE_ENABLE));
289 /* Set squelch level to 240mV (default 480mV) */
290 flags = inw(BASE + CONFIG_REG_W);
291 flags |= CR_SET_SQLCH;
292 outw(BASE + CONFIG_REG_W, flags);
295 * Reset the MMU and wait for it to be un-busy.
298 outw(BASE + MMU_CMD_REG_W, MMUCR_RESET);
299 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */
303 * Disable all interrupts
305 outb(BASE + INTR_MASK_REG_B, 0x00);
310 * Set the transmitter control. We want it enabled.
316 * I (GB) have been unlucky getting this to work.
318 flags |= TCR_PAD_ENABLE;
321 outw(BASE + TXMIT_CONTROL_REG_W, flags);
325 * Now, enable interrupts
334 outb(BASE + INTR_MASK_REG_B, mask);
335 sc->intr_mask = mask;
336 sc->pages_wanted = -1;
340 * Mark the interface running but not active.
342 ifp->if_flags |= IFF_RUNNING;
343 ifp->if_flags &= ~IFF_OACTIVE;
346 * Attempt to push out any waiting packets.
355 snstart(struct ifnet *ifp)
357 register struct sn_softc *sc = ifp->if_softc;
359 register struct mbuf *m;
370 if (sc->arpcom.ac_if.if_flags & IFF_OACTIVE) {
374 if (sc->pages_wanted != -1) {
376 printf("sn%d: snstart() while memory allocation pending\n",
383 * Sneak a peek at the next packet
385 m = sc->arpcom.ac_if.if_snd.ifq_head;
391 * Compute the frame length and set pad to give an overall even
392 * number of bytes. Below we assume that the packet length is even.
394 for (len = 0, top = m; m; m = m->m_next)
400 * We drop packets that are too large. Perhaps we should truncate
403 if (len + pad > ETHER_MAX_LEN - ETHER_CRC_LEN) {
404 printf("sn%d: large packet discarded (A)\n", ifp->if_unit);
405 ++sc->arpcom.ac_if.if_oerrors;
406 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
413 * If HW padding is not turned on, then pad to ETHER_MIN_LEN.
415 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
416 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
423 * The MMU wants the number of pages to be the number of 256 byte
424 * 'pages', minus 1 (A packet can't ever have 0 pages. We also
425 * include space for the status word, byte count and control bytes in
426 * the allocation request.
428 numPages = (length + 6) >> 8;
432 * Now, try to allocate the memory
435 outw(BASE + MMU_CMD_REG_W, MMUCR_ALLOC | numPages);
438 * Wait a short amount of time to see if the allocation request
439 * completes. Otherwise, I enable the interrupt and wait for
440 * completion asyncronously.
443 time_out = MEMORY_WAIT_TIME;
445 if (inb(BASE + INTR_STAT_REG_B) & IM_ALLOC_INT)
447 } while (--time_out);
452 * No memory now. Oh well, wait until the chip finds memory
453 * later. Remember how many pages we were asking for and
454 * enable the allocation completion interrupt. Also set a
455 * watchdog in case we miss the interrupt. We mark the
456 * interface active since there is no point in attempting an
457 * snstart() until after the memory is available.
459 mask = inb(BASE + INTR_MASK_REG_B) | IM_ALLOC_INT;
460 outb(BASE + INTR_MASK_REG_B, mask);
461 sc->intr_mask = mask;
463 sc->arpcom.ac_if.if_timer = 1;
464 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
465 sc->pages_wanted = numPages;
471 * The memory allocation completed. Check the results.
473 packet_no = inb(BASE + ALLOC_RESULT_REG_B);
474 if (packet_no & ARR_FAILED) {
475 printf("sn%d: Memory allocation failed\n", ifp->if_unit);
479 * We have a packet number, so tell the card to use it.
481 outb(BASE + PACKET_NUM_REG_B, packet_no);
484 * Point to the beginning of the packet
486 outw(BASE + POINTER_REG_W, PTR_AUTOINC | 0x0000);
489 * Send the packet length (+6 for status, length and control byte)
490 * and the status word (set to zeros)
492 outw(BASE + DATA_REG_W, 0);
493 outb(BASE + DATA_REG_B, (length + 6) & 0xFF);
494 outb(BASE + DATA_REG_B, (length + 6) >> 8);
497 * Get the packet from the kernel. This will include the Ethernet
498 * frame header, MAC Addresses etc.
500 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
503 * Push out the data to the card.
505 for (top = m; m != 0; m = m->m_next) {
510 outsw(BASE + DATA_REG_W, mtod(m, caddr_t), m->m_len / 2);
513 * Push out remaining byte.
516 outb(BASE + DATA_REG_B, *(mtod(m, caddr_t) + m->m_len - 1));
523 outw(BASE + DATA_REG_W, 0);
527 outb(BASE + DATA_REG_B, 0);
530 * Push out control byte and unused packet byte The control byte is 0
531 * meaning the packet is even lengthed and no special CRC handling is
534 outw(BASE + DATA_REG_W, 0);
537 * Enable the interrupts and let the chipset deal with it Also set a
538 * watchdog in case we miss the interrupt.
540 mask = inb(BASE + INTR_MASK_REG_B) | (IM_TX_INT | IM_TX_EMPTY_INT);
541 outb(BASE + INTR_MASK_REG_B, mask);
542 sc->intr_mask = mask;
544 outw(BASE + MMU_CMD_REG_W, MMUCR_ENQUEUE);
546 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
547 sc->arpcom.ac_if.if_timer = 1;
553 sc->arpcom.ac_if.if_opackets++;
560 * Is another packet coming in? We don't want to overflow the tiny
561 * RX FIFO. If nothing has arrived then attempt to queue another
564 if (inw(BASE + FIFO_PORTS_REG_W) & FIFO_REMPTY)
573 /* Resume a packet transmit operation after a memory allocation
576 * This is basically a hacked up copy of snstart() which handles
577 * a completed memory allocation the same way snstart() does.
578 * It then passes control to snstart to handle any other queued
582 snresume(struct ifnet *ifp)
584 register struct sn_softc *sc = ifp->if_softc;
586 register struct mbuf *m;
592 u_short pages_wanted;
595 if (sc->pages_wanted < 0)
598 pages_wanted = sc->pages_wanted;
599 sc->pages_wanted = -1;
602 * Sneak a peek at the next packet
604 m = sc->arpcom.ac_if.if_snd.ifq_head;
606 printf("sn%d: snresume() with nothing to send\n", ifp->if_unit);
610 * Compute the frame length and set pad to give an overall even
611 * number of bytes. Below we assume that the packet length is even.
613 for (len = 0, top = m; m; m = m->m_next)
619 * We drop packets that are too large. Perhaps we should truncate
622 if (len + pad > ETHER_MAX_LEN - ETHER_CRC_LEN) {
623 printf("sn%d: large packet discarded (B)\n", ifp->if_unit);
624 ++sc->arpcom.ac_if.if_oerrors;
625 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
632 * If HW padding is not turned on, then pad to ETHER_MIN_LEN.
634 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
635 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
643 * The MMU wants the number of pages to be the number of 256 byte
644 * 'pages', minus 1 (A packet can't ever have 0 pages. We also
645 * include space for the status word, byte count and control bytes in
646 * the allocation request.
648 numPages = (length + 6) >> 8;
654 * The memory allocation completed. Check the results. If it failed,
655 * we simply set a watchdog timer and hope for the best.
657 packet_no = inb(BASE + ALLOC_RESULT_REG_B);
658 if (packet_no & ARR_FAILED) {
659 printf("sn%d: Memory allocation failed. Weird.\n", ifp->if_unit);
660 sc->arpcom.ac_if.if_timer = 1;
664 * We have a packet number, so tell the card to use it.
666 outb(BASE + PACKET_NUM_REG_B, packet_no);
669 * Now, numPages should match the pages_wanted recorded when the
670 * memory allocation was initiated.
672 if (pages_wanted != numPages) {
673 printf("sn%d: memory allocation wrong size. Weird.\n", ifp->if_unit);
675 * If the allocation was the wrong size we simply release the
676 * memory once it is granted. Wait for the MMU to be un-busy.
678 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */
680 outw(BASE + MMU_CMD_REG_W, MMUCR_FREEPKT);
685 * Point to the beginning of the packet
687 outw(BASE + POINTER_REG_W, PTR_AUTOINC | 0x0000);
690 * Send the packet length (+6 for status, length and control byte)
691 * and the status word (set to zeros)
693 outw(BASE + DATA_REG_W, 0);
694 outb(BASE + DATA_REG_B, (length + 6) & 0xFF);
695 outb(BASE + DATA_REG_B, (length + 6) >> 8);
698 * Get the packet from the kernel. This will include the Ethernet
699 * frame header, MAC Addresses etc.
701 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
704 * Push out the data to the card.
706 for (top = m; m != 0; m = m->m_next) {
711 outsw(BASE + DATA_REG_W, mtod(m, caddr_t), m->m_len / 2);
714 * Push out remaining byte.
717 outb(BASE + DATA_REG_B, *(mtod(m, caddr_t) + m->m_len - 1));
724 outw(BASE + DATA_REG_W, 0);
728 outb(BASE + DATA_REG_B, 0);
731 * Push out control byte and unused packet byte The control byte is 0
732 * meaning the packet is even lengthed and no special CRC handling is
735 outw(BASE + DATA_REG_W, 0);
738 * Enable the interrupts and let the chipset deal with it Also set a
739 * watchdog in case we miss the interrupt.
741 mask = inb(BASE + INTR_MASK_REG_B) | (IM_TX_INT | IM_TX_EMPTY_INT);
742 outb(BASE + INTR_MASK_REG_B, mask);
743 sc->intr_mask = mask;
744 outw(BASE + MMU_CMD_REG_W, MMUCR_ENQUEUE);
750 sc->arpcom.ac_if.if_opackets++;
756 * Now pass control to snstart() to queue any additional packets
758 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
762 * We've sent something, so we're active. Set a watchdog in case the
763 * TX_EMPTY interrupt is lost.
765 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
766 sc->arpcom.ac_if.if_timer = 1;
775 int status, interrupts;
776 register struct sn_softc *sc = (struct sn_softc *) arg;
777 struct ifnet *ifp = &sc->arpcom.ac_if;
781 * Chip state registers
789 * if_ep.c did this, so I do too. Yet if_ed.c doesn't. I wonder...
794 * Clear the watchdog.
801 * Obtain the current interrupt mask and clear the hardware mask
802 * while servicing interrupts.
804 mask = inb(BASE + INTR_MASK_REG_B);
805 outb(BASE + INTR_MASK_REG_B, 0x00);
808 * Get the set of interrupts which occurred and eliminate any which
811 interrupts = inb(BASE + INTR_STAT_REG_B);
812 status = interrupts & mask;
815 * Now, process each of the interrupt types.
821 if (status & IM_RX_OVRN_INT) {
824 * Acknowlege Interrupt
827 outb(BASE + INTR_ACK_REG_B, IM_RX_OVRN_INT);
829 ++sc->arpcom.ac_if.if_ierrors;
834 if (status & IM_RCV_INT) {
839 packet_number = inw(BASE + FIFO_PORTS_REG_W);
841 if (packet_number & FIFO_REMPTY) {
844 * we got called , but nothing was on the FIFO
846 printf("sn: Receive interrupt with nothing on FIFO\n");
854 * An on-card memory allocation came through.
856 if (status & IM_ALLOC_INT) {
859 * Disable this interrupt.
861 mask &= ~IM_ALLOC_INT;
862 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
863 snresume(&sc->arpcom.ac_if);
866 * TX Completion. Handle a transmit error message. This will only be
867 * called when there is an error, because of the AUTO_RELEASE mode.
869 if (status & IM_TX_INT) {
872 * Acknowlege Interrupt
875 outb(BASE + INTR_ACK_REG_B, IM_TX_INT);
877 packet_no = inw(BASE + FIFO_PORTS_REG_W);
878 packet_no &= FIFO_TX_MASK;
881 * select this as the packet to read from
883 outb(BASE + PACKET_NUM_REG_B, packet_no);
886 * Position the pointer to the first word from this packet
888 outw(BASE + POINTER_REG_W, PTR_AUTOINC | PTR_READ | 0x0000);
891 * Fetch the TX status word. The value found here will be a
892 * copy of the EPH_STATUS_REG_W at the time the transmit
895 tx_status = inw(BASE + DATA_REG_W);
897 if (tx_status & EPHSR_TX_SUC) {
898 device_printf(sc->dev,
899 "Successful packet caused interrupt\n");
901 ++sc->arpcom.ac_if.if_oerrors;
904 if (tx_status & EPHSR_LATCOL)
905 ++sc->arpcom.ac_if.if_collisions;
908 * Some of these errors will have disabled transmit.
909 * Re-enable transmit now.
914 outw(BASE + TXMIT_CONTROL_REG_W, TCR_ENABLE);
916 outw(BASE + TXMIT_CONTROL_REG_W, TCR_ENABLE | TCR_PAD_ENABLE);
920 * kill the failed packet. Wait for the MMU to be un-busy.
923 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */
925 outw(BASE + MMU_CMD_REG_W, MMUCR_FREEPKT);
928 * Attempt to queue more transmits.
930 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
931 snstart(&sc->arpcom.ac_if);
934 * Transmit underrun. We use this opportunity to update transmit
935 * statistics from the card.
937 if (status & IM_TX_EMPTY_INT) {
940 * Acknowlege Interrupt
943 outb(BASE + INTR_ACK_REG_B, IM_TX_EMPTY_INT);
946 * Disable this interrupt.
948 mask &= ~IM_TX_EMPTY_INT;
951 card_stats = inw(BASE + COUNTER_REG_W);
956 sc->arpcom.ac_if.if_collisions += card_stats & ECR_COLN_MASK;
959 * Multiple collisions
961 sc->arpcom.ac_if.if_collisions += (card_stats & ECR_MCOLN_MASK) >> 4;
966 * Attempt to enqueue some more stuff.
968 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
969 snstart(&sc->arpcom.ac_if);
972 * Some other error. Try to fix it by resetting the adapter.
974 if (status & IM_EPH_INT) {
981 * Handled all interrupt sources.
987 * Reestablish interrupts from mask which have not been deselected
988 * during this interrupt. Note that the hardware mask, which was set
989 * to 0x00 at the start of this service routine, may have been
990 * updated by one or more of the interrupt handers and we must let
991 * those new interrupts stay enabled here.
993 mask |= inb(BASE + INTR_MASK_REG_B);
994 outb(BASE + INTR_MASK_REG_B, mask);
995 sc->intr_mask = mask;
1001 snread(register struct ifnet *ifp)
1003 struct sn_softc *sc = ifp->if_softc;
1004 struct ether_header *eh;
1008 u_short packet_length;
1013 packet_number = inw(BASE + FIFO_PORTS_REG_W);
1015 if (packet_number & FIFO_REMPTY) {
1018 * we got called , but nothing was on the FIFO
1020 printf("sn: Receive interrupt with nothing on FIFO\n");
1027 * Start reading from the start of the packet. Since PTR_RCV is set,
1028 * packet number is found in FIFO_PORTS_REG_W, FIFO_RX_MASK.
1030 outw(BASE + POINTER_REG_W, PTR_READ | PTR_RCV | PTR_AUTOINC | 0x0000);
1033 * First two words are status and packet_length
1035 status = inw(BASE + DATA_REG_W);
1036 packet_length = inw(BASE + DATA_REG_W) & RLEN_MASK;
1039 * The packet length contains 3 extra words: status, length, and a
1040 * extra word with the control byte.
1045 * Account for receive errors and discard.
1047 if (status & RS_ERRORS) {
1048 ++sc->arpcom.ac_if.if_ierrors;
1052 * A packet is received.
1056 * Adjust for odd-length packet.
1058 if (status & RS_ODDFRAME)
1062 * Allocate a header mbuf from the kernel.
1064 MGETHDR(m, M_DONTWAIT, MT_DATA);
1068 m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
1069 m->m_pkthdr.len = m->m_len = packet_length;
1072 * Attach an mbuf cluster
1074 MCLGET(m, M_DONTWAIT);
1077 * Insist on getting a cluster
1079 if ((m->m_flags & M_EXT) == 0) {
1081 ++sc->arpcom.ac_if.if_ierrors;
1082 printf("sn: snread() kernel memory allocation problem\n");
1085 eh = mtod(m, struct ether_header *);
1088 * Get packet, including link layer address, from interface.
1091 data = (u_char *) eh;
1092 insw(BASE + DATA_REG_W, data, packet_length >> 1);
1093 if (packet_length & 1) {
1094 data += packet_length & ~1;
1095 *data = inb(BASE + DATA_REG_B);
1097 ++sc->arpcom.ac_if.if_ipackets;
1100 * Remove link layer addresses and whatnot.
1102 m->m_pkthdr.len = m->m_len = packet_length - sizeof(struct ether_header);
1103 m->m_data += sizeof(struct ether_header);
1105 ether_input(&sc->arpcom.ac_if, eh, m);
1110 * Error or good, tell the card to get rid of this packet Wait for
1111 * the MMU to be un-busy.
1114 while (inw(BASE + MMU_CMD_REG_W) & MMUCR_BUSY) /* NOTHING */
1116 outw(BASE + MMU_CMD_REG_W, MMUCR_RELEASE);
1119 * Check whether another packet is ready
1121 packet_number = inw(BASE + FIFO_PORTS_REG_W);
1122 if (packet_number & FIFO_REMPTY) {
1130 * Handle IOCTLS. This function is completely stolen from if_ep.c
1131 * As with its progenitor, it does not handle hardware address
1135 snioctl(register struct ifnet *ifp, u_long cmd, caddr_t data)
1137 struct sn_softc *sc = ifp->if_softc;
1146 error = ether_ioctl(ifp, cmd, data);
1150 if ((ifp->if_flags & IFF_UP) == 0 && ifp->if_flags & IFF_RUNNING) {
1151 ifp->if_flags &= ~IFF_RUNNING;
1155 /* reinitialize card on any parameter change */
1163 bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data,
1164 sizeof(sc->sc_addr));
1169 /* update multicast filter list. */
1174 /* update multicast filter list. */
1188 snreset(struct sn_softc *sc)
1200 snwatchdog(struct ifnet *ifp)
1204 sn_intr(ifp->if_softc);
1209 /* 1. zero the interrupt mask
1210 * 2. clear the enable receive flag
1211 * 3. clear the enable xmit flags
1214 snstop(struct sn_softc *sc)
1217 struct ifnet *ifp = &sc->arpcom.ac_if;
1220 * Clear interrupt mask; disable all interrupts.
1223 outb(BASE + INTR_MASK_REG_B, 0x00);
1226 * Disable transmitter and Receiver
1229 outw(BASE + RECV_CONTROL_REG_W, 0x0000);
1230 outw(BASE + TXMIT_CONTROL_REG_W, 0x0000);
1240 sn_activate(device_t dev)
1242 struct sn_softc *sc = device_get_softc(dev);
1246 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
1247 0, ~0, SMC_IO_EXTENT, RF_ACTIVE);
1248 if (!sc->port_res) {
1250 device_printf(dev, "Cannot allocate ioport\n");
1256 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
1257 0, ~0, 1, RF_ACTIVE);
1260 device_printf(dev, "Cannot allocate irq\n");
1265 if ((err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET, sn_intr, sc,
1266 &sc->intrhand)) != 0) {
1271 sc->sn_io_addr = rman_get_start(sc->port_res);
1276 sn_deactivate(device_t dev)
1278 struct sn_softc *sc = device_get_softc(dev);
1281 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
1284 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1288 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
1295 * Function: sn_probe( device_t dev, int pccard )
1298 * Tests to see if a given ioaddr points to an SMC9xxx chip.
1299 * Tries to cause as little damage as possible if it's not a SMC chip.
1300 * Returns a 0 on success
1303 * (1) see if the high byte of BANK_SELECT is 0x33
1304 * (2) compare the ioaddr with the base register's address
1305 * (3) see if I recognize the chip ID in the appropriate register
1310 sn_probe(device_t dev, int pccard)
1312 struct sn_softc *sc = device_get_softc(dev);
1314 u_short revision_register;
1315 u_short base_address_register;
1319 if ((err = sn_activate(dev)) != 0)
1322 ioaddr = sc->sn_io_addr;
1325 * First, see if the high byte is 0x33
1327 bank = inw(ioaddr + BANK_SELECT_REG_W);
1328 if ((bank & BSR_DETECT_MASK) != BSR_DETECT_VALUE) {
1330 device_printf(dev, "test1 failed\n");
1335 * The above MIGHT indicate a device, but I need to write to further
1336 * test this. Go to bank 0, then test that the register still
1337 * reports the high byte is 0x33.
1339 outw(ioaddr + BANK_SELECT_REG_W, 0x0000);
1340 bank = inw(ioaddr + BANK_SELECT_REG_W);
1341 if ((bank & BSR_DETECT_MASK) != BSR_DETECT_VALUE) {
1343 device_printf(dev, "test2 failed\n");
1348 * well, we've already written once, so hopefully another time won't
1349 * hurt. This time, I need to switch the bank register to bank 1, so
1350 * I can access the base address register. The contents of the
1351 * BASE_ADDR_REG_W register, after some jiggery pokery, is expected
1352 * to match the I/O port address where the adapter is being probed.
1354 outw(ioaddr + BANK_SELECT_REG_W, 0x0001);
1355 base_address_register = inw(ioaddr + BASE_ADDR_REG_W);
1358 * This test is nonsence on PC-card architecture, so if
1359 * pccard == 1, skip this test. (hosokawa)
1361 if (!pccard && (ioaddr != (base_address_register >> 3 & 0x3E0))) {
1364 * Well, the base address register didn't match. Must not
1365 * have been a SMC chip after all.
1368 * printf("sn: ioaddr %x doesn't match card configuration
1369 * (%x)\n", ioaddr, base_address_register >> 3 & 0x3E0 );
1373 device_printf(dev, "test3 failed ioaddr = 0x%x, "
1374 "base_address_register = 0x%x\n", ioaddr,
1375 base_address_register >> 3 & 0x3E0);
1380 * Check if the revision register is something that I recognize.
1381 * These might need to be added to later, as future revisions could
1384 outw(ioaddr + BANK_SELECT_REG_W, 0x3);
1385 revision_register = inw(ioaddr + REVISION_REG_W);
1386 if (!chip_ids[(revision_register >> 4) & 0xF]) {
1389 * I don't regonize this chip, so...
1392 device_printf(dev, "test4 failed\n");
1397 * at this point I'll assume that the chip is an SMC9xxx. It might be
1398 * prudent to check a listing of MAC addresses against the hardware
1399 * address, or do some other tests.
1411 sn_setmcast(struct sn_softc *sc)
1413 struct ifnet *ifp = (struct ifnet *)sc;
1417 * Set the receiver filter. We want receive enabled and auto strip
1418 * of CRC from received packet. If we are promiscuous then set that
1421 flags = RCR_ENABLE | RCR_STRIP_CRC;
1423 if (ifp->if_flags & IFF_PROMISC) {
1424 flags |= RCR_PROMISC | RCR_ALMUL;
1425 } else if (ifp->if_flags & IFF_ALLMULTI) {
1429 if (sn_getmcf(&sc->arpcom, mcf)) {
1432 outw(BASE + MULTICAST1_REG_W,
1433 ((u_short)mcf[1] << 8) | mcf[0]);
1434 outw(BASE + MULTICAST2_REG_W,
1435 ((u_short)mcf[3] << 8) | mcf[2]);
1436 outw(BASE + MULTICAST3_REG_W,
1437 ((u_short)mcf[5] << 8) | mcf[4]);
1438 outw(BASE + MULTICAST4_REG_W,
1439 ((u_short)mcf[7] << 8) | mcf[6]);
1445 outw(BASE + RECV_CONTROL_REG_W, flags);
1449 sn_getmcf(struct arpcom *ac, u_char *mcf)
1452 register u_int index, index2;
1453 register u_char *af = (u_char *) mcf;
1454 struct ifmultiaddr *ifma;
1458 for (ifma = ac->ac_if.if_multiaddrs.lh_first; ifma;
1459 ifma = ifma->ifma_link.le_next) {
1460 if (ifma->ifma_addr->sa_family != AF_LINK)
1462 index = smc_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)) & 0x3f;
1464 for (i = 0; i < 6; i++) {
1466 index2 |= (index & 0x01);
1469 af[index2 >> 3] |= 1 << (index2 & 7);
1471 return 1; /* use multicast filter */
1479 const u_int poly = 0xedb88320;
1480 u_int v = 0xffffffff;
1483 for (perByte = 0; perByte < ETHER_ADDR_LEN; perByte++) {
1485 for (perBit = 0; perBit < 8; perBit++) {
1486 v = (v >> 1)^(((v ^ c) & 0x01) ? poly : 0);