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