FreeBSD/Linux Kernel Cross Reference
sys/i386/isa/if_ze.c
1 /*-
2 * TODO:
3 * [1] integrate into current if_ed.c
4 * [2] parse tuples to find out where to map the shared memory buffer,
5 * and what to write into the configuration register
6 * [3] move pcic-specific code into a separate module.
7 *
8 * Device driver for IBM PCMCIA Credit Card Adapter for Ethernet,
9 * if_ze.c
10 *
11 * Based on the Device driver for National Semiconductor DS8390 ethernet
12 * adapters by David Greenman. Modifications for PCMCIA by Keith Moore.
13 * Adapted for FreeBSD 1.1.5 by Jordan Hubbard.
14 *
15 * Currently supports only the IBM Credit Card Adapter for Ethernet, but
16 * could probably work with other PCMCIA cards also, if it were modified
17 * to get the locations of the PCMCIA configuration option register (COR)
18 * by parsing the configuration tuples, rather than by hard-coding in
19 * the value expected by IBM's card.
20 *
21 * Sources for data on the PCMCIA/IBM CCAE specific portions of the driver:
22 *
23 * [1] _Local Area Network Credit Card Adapters Technical Reference_,
24 * IBM Corp., SC30-3585-00, part # 33G9243.
25 * [2] "pre-alpha" PCMCIA support code for Linux by Barry Jaspan.
26 * [3] Intel 82536SL PC Card Interface Controller Data Sheet, Intel
27 * Order Number 290423-002
28 * [4] National Semiconductor DP83902A ST-NIC (tm) Serial Network
29 * Interface Controller for Twisted Pair data sheet.
30 *
31 *
32 * Copyright (C) 1993, David Greenman. This software may be used, modified,
33 * copied, distributed, and sold, in both source and binary form provided
34 * that the above copyright and these terms are retained. Under no
35 * circumstances is the author responsible for the proper functioning
36 * of this software, nor does the author assume any responsibility
37 * for damages incurred with its use.
38 */
39 /*
40 * I doubled delay loops in this file because it is not enough for some
41 * laptop machines' PCIC (especially, on my Chaplet ILFA 350 ^^;).
42 * HOSOKAWA, Tatsumi <hosokawa@mt.cs.keio.ac.jp>
43 */
44 /*
45 * Very small patch for IBM Ethernet PCMCIA Card II and IBM ThinkPad230Cs.
46 * ETO, Toshihisa <eto@osl.fujitsu.co.jp>
47 */
48
49 /*
50 * $FreeBSD: src/sys/i386/isa/if_ze.c,v 1.33.2.7 1999/09/05 08:12:59 peter Exp $
51 */
52
53 /* XXX - Don't mix different PCCARD support code */
54 #include "card.h"
55 #include "pcic.h"
56 #if NCARD > 0 || NPCIC > 0
57 #ifndef LINT_PCCARD_HACK
58 #error "Dedicated PCMCIA drivers and generic PCMCIA support can't be mixed"
59 #else
60 #warning "Dedicated PCMCIA drivers and generic PCMCIA support can't be mixed"
61 #endif
62 #endif
63
64 #include "ze.h"
65 #if NZE > 0
66 #include "bpfilter.h"
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/conf.h>
71 #include <sys/errno.h>
72 #include <sys/ioctl.h>
73 #include <sys/mbuf.h>
74 #include <sys/socket.h>
75 #include <sys/syslog.h>
76
77 #include <net/if.h>
78 #include <net/if_dl.h>
79 #include <net/if_types.h>
80 #include <net/netisr.h>
81
82 #ifdef INET
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/in_var.h>
86 #include <netinet/ip.h>
87 #include <netinet/if_ether.h>
88 #endif
89
90 #ifdef IPX
91 #include <netipx/ipx.h>
92 #include <netipx/ipx_if.h>
93 #endif
94
95 #ifdef NS
96 #include <netns/ns.h>
97 #include <netns/ns_if.h>
98 #endif
99
100 #if NBPFILTER > 0
101 #include <net/bpf.h>
102 #include <net/bpfdesc.h>
103 #endif
104
105 #include <machine/clock.h>
106 #include <machine/md_var.h>
107
108 #include <i386/isa/isa.h>
109 #include <i386/isa/isa_device.h>
110 #include <i386/isa/icu.h>
111 #include <i386/isa/if_edreg.h>
112 #include <i386/isa/pcic.h>
113
114 #include "apm.h"
115 #if NAPM > 0
116 #include <machine/apm_bios.h>
117 #endif /* NAPM > 0 */
118
119
120 /*****************************************************************************
121 * Driver for Ethernet Adapter *
122 *****************************************************************************/
123 /*
124 * ze_softc: per line info and status
125 */
126 static struct ze_softc {
127
128 struct arpcom arpcom; /* ethernet common */
129
130 caddr_t maddr;
131 u_long iobase, irq;
132
133 char *type_str; /* pointer to type string */
134 char *mau; /* type of media access unit */
135 u_short nic_addr; /* NIC (DS8390) I/O bus address */
136
137 caddr_t smem_start; /* shared memory start address */
138 caddr_t smem_end; /* shared memory end address */
139 u_long smem_size; /* total shared memory size */
140 caddr_t smem_ring; /* start of RX ring-buffer (in smem) */
141
142 u_char memwidth; /* width of access to card mem 8 or 16 */
143 u_char xmit_busy; /* transmitter is busy */
144 u_char txb_cnt; /* Number of transmit buffers */
145 u_char txb_next; /* Pointer to next buffer ready to xmit */
146 u_short txb_next_len; /* next xmit buffer length */
147 u_char data_buffered; /* data has been buffered in interface memory */
148 u_char tx_page_start; /* first page of TX buffer area */
149
150 u_char rec_page_start; /* first page of RX ring-buffer */
151 u_char rec_page_stop; /* last page of RX ring-buffer */
152 u_char next_packet; /* pointer to next unread RX packet */
153 int slot; /* information for reconfiguration */
154 u_char last_alive; /* information for reconfiguration */
155 u_char last_up; /* information for reconfiguration */
156 #if NAPM > 0
157 struct apmhook s_hook; /* reconfiguration support */
158 struct apmhook r_hook; /* reconfiguration support */
159 #endif /* NAPM > 0 */
160 } ze_softc[NZE];
161
162 static int ze_check_cis __P((unsigned char *scratch));
163 static int ze_find_adapter __P((unsigned char *scratch, int reconfig));
164 static int ze_probe __P((struct isa_device *isa_dev));
165 static void ze_setup __P((struct ze_softc *sc));
166 static int ze_suspend __P((void *visa_dev));
167 static int ze_resume __P((void *visa_dev));
168 static int ze_attach __P((struct isa_device *isa_dev));
169 static void ze_reset __P((int unit));
170 static void ze_stop __P((int unit));
171 static void ze_watchdog __P((struct ifnet *ifp));
172 static void ze_init __P((int unit));
173 static __inline void ze_xmit __P((struct ifnet *ifp));
174 static void ze_start __P((struct ifnet *ifp));
175 static __inline void ze_rint __P((int unit));
176 static int ze_ioctl __P((struct ifnet *ifp, int command, caddr_t data));
177 static void ze_get_packet __P((struct ze_softc *sc, char *buf, int len));
178 static __inline char *ze_ring_copy __P((struct ze_softc *sc, char *src, char *dst, int amount));
179 static struct mbuf *ze_ring_to_mbuf __P((struct ze_softc *sc, char *src, struct mbuf *dst, int total_len));
180
181 struct isa_driver zedriver = {
182 ze_probe,
183 ze_attach,
184 "ze"
185 };
186
187 static unsigned char enet_addr[6];
188 static unsigned char card_info[256];
189
190 #define CARD_INFO "IBM Corp.~Ethernet~0933495"
191
192 /*
193 * IBM Ethernet PCMCIA Card II returns following info.
194 */
195 #define CARD2_INFO "IBM Corp.~Ethernet~0934214"
196
197 /* */
198
199 #define CARD3_INFO "National Semiconductor~InfoMover NE4"
200
201 /*
202 * scan the card information structure looking for the version/product info
203 * tuple. when we find it, compare it to the string we are looking for.
204 * return 1 if we find it, 0 otherwise.
205 */
206
207 static int
208 ze_check_cis (unsigned char *scratch)
209 {
210 int i,j,k;
211
212 card_info[0] = '\0';
213 i = 0;
214 while (scratch[i] != 0xff && i < 1024) {
215 unsigned char link = scratch[i+2];
216
217 #if 0
218 printf ("[%02x] %02x ", i, link);
219 for (j = 4; j < 2 * link + 4 && j < 32; j += 2)
220 printf ("%02x ", scratch[j + i]);
221 printf ("\n");
222 #endif
223 if (scratch[i] == 0x15) {
224 /*
225 * level 1 version/product info
226 * copy to card_info, translating '\0' to '~'
227 */
228 k = 0;
229 for (j = i+8; scratch[j] != 0xff; j += 2)
230 card_info[k++] = scratch[j] == '\0' ? '~' : scratch[j];
231 card_info[k++] = '\0';
232 #if 0
233 return (bcmp (card_info, CARD_INFO, sizeof(CARD_INFO)-1) == 0);
234 #else
235 if ((bcmp (card_info, CARD_INFO, sizeof(CARD_INFO)-1) == 0) ||
236 (bcmp (card_info, CARD2_INFO, sizeof(CARD2_INFO)-1) == 0) ||
237 (bcmp (card_info, CARD3_INFO, sizeof(CARD3_INFO)-1) == 0)) {
238 return 1;
239 }
240 return 0;
241 #endif
242 }
243 i += 4 + 2 * link;
244 }
245 return 0;
246 }
247
248 /*
249 * Probe each slot looking for an IBM Credit Card Adapter for Ethernet
250 * For each card that we find, map its card information structure
251 * into system memory at 'scratch' and see whether it's one of ours.
252 * Return the slot number if we find a card, or -1 otherwise.
253 *
254 * Side effects:
255 * + On success, leaves CIS mapped into memory at 'scratch';
256 * caller must free it.
257 * + On success, leaves ethernet address in enet_addr.
258 * + Leaves product/vendor id of last card probed in 'card_info'
259 */
260
261 static int prev_slot = 0;
262
263 static int
264 ze_find_adapter (unsigned char *scratch, int reconfig)
265 {
266 int slot;
267
268 for (slot = prev_slot; slot < MAXSLOT; ++slot) {
269 /*
270 * see if there's a PCMCIA controller here
271 * Intel PCMCIA controllers use 0x82 and 0x83
272 * IBM clone chips use 0x88 and 0x89, apparently
273 */
274 /*
275 * IBM ThinkPad230Cs use 0x84.
276 */
277 unsigned char idbyte = pcic_getb (slot, PCIC_ID_REV);
278
279 if (idbyte != 0x82 && idbyte != 0x83 &&
280 idbyte != 0x84 && /* for IBM ThinkPad 230Cs */
281 idbyte != 0x88 && idbyte != 0x89) {
282 #if 0
283 printf ("ibmccae: pcic slot %d: wierd id/rev code 0x%02x\n",
284 slot, idbyte);
285 #endif
286 continue;
287 }
288 if ((pcic_getb (slot, PCIC_STATUS) & PCIC_CD) != PCIC_CD) {
289 if (!reconfig) {
290 printf ("ze: slot %d: no card in slot\n", slot);
291 }
292 else {
293 log (LOG_NOTICE, "ze: slot %d: no card in slot\n", slot);
294 }
295 /* no card in slot */
296 continue;
297 }
298 pcic_power_on (slot);
299 pcic_reset (slot);
300 /*
301 * map the card's attribute memory and examine its
302 * card information structure tuples for something
303 * we recognize.
304 */
305 pcic_map_memory (slot, 0, kvtop (scratch), 0L,
306 0xFFFL, ATTRIBUTE, 1);
307
308 if ((ze_check_cis (scratch)) > 0) {
309 /* found it */
310 if (!reconfig) {
311 printf ("ze: found card in slot %d\n", slot);
312 }
313 else {
314 log (LOG_NOTICE, "ze: found card in slot %d\n", slot);
315 }
316 prev_slot = (prev_slot == MAXSLOT - 1) ? 0 : prev_slot+1;
317
318 return slot;
319 }
320 else {
321 if (!reconfig) {
322 printf ("ze: pcmcia slot %d: %s\n", slot, card_info);
323 }
324 else {
325 log (LOG_NOTICE, "ze: pcmcia slot %d: %s\n", slot, card_info);
326 }
327 }
328 pcic_unmap_memory (slot, 0);
329 }
330 prev_slot = 0;
331 return -1;
332 }
333
334
335 /*
336 * macros to handle casting unsigned long to (char *) so we can
337 * read/write into physical memory space.
338 */
339
340 #define PEEK(addr) (*((unsigned char *)(addr)))
341 #define POKE(addr,val) do { PEEK(addr) = (val); } while (0)
342
343 /*
344 * Determine if the device is present
345 *
346 * on entry:
347 * a pointer to an isa_device struct
348 * on exit:
349 * NULL if device not found
350 * or # of i/o addresses used (if found)
351 pcic(
352 */
353 static int
354 ze_probe(isa_dev)
355 struct isa_device *isa_dev;
356 {
357 struct ze_softc *sc = &ze_softc[isa_dev->id_unit];
358 int i;
359 u_int memsize;
360 u_char tmp;
361 int slot;
362
363 if ((slot = ze_find_adapter (isa_dev->id_maddr, isa_dev->id_reconfig)) < 0)
364 return 0;
365
366 /*
367 * okay, we found a card, so set it up
368 */
369 /*
370 * Inhibit 16 bit memory delay.
371 * POINTETH.SYS apparently does this, for what reason I don't know.
372 */
373 pcic_putb (slot, PCIC_CDGC,
374 pcic_getb (slot, PCIC_CDGC) | PCIC_16_DL_INH);
375 /*
376 * things to map
377 * (1) card's EEPROM is already mapped by the find_adapter routine
378 * but we still need to get the card's ethernet address.
379 * after that we unmap that part of attribute memory.
380 * (2) card configuration registers need to be mapped in so we
381 * can set the configuration and socket # registers.
382 * (3) shared memory packet buffer
383 * (4) i/o ports
384 * (5) IRQ
385 */
386 /*
387 * Sigh. Location of the ethernet address isn't documented in [1].
388 * It was derived by doing a hex dump of all of attribute memory
389 * and looking for the IBM vendor prefix.
390 */
391 enet_addr[0] = PEEK(isa_dev->id_maddr+0xff0);
392 enet_addr[1] = PEEK(isa_dev->id_maddr+0xff2);
393 enet_addr[2] = PEEK(isa_dev->id_maddr+0xff4);
394 enet_addr[3] = PEEK(isa_dev->id_maddr+0xff6);
395 enet_addr[4] = PEEK(isa_dev->id_maddr+0xff8);
396 enet_addr[5] = PEEK(isa_dev->id_maddr+0xffa);
397 pcic_unmap_memory (slot, 0);
398
399 sc->maddr = isa_dev->id_maddr;
400 sc->irq = isa_dev->id_irq;
401 sc->iobase = isa_dev->id_iobase;
402 sc->slot = slot;
403 /*
404 * Setup i/o addresses
405 */
406 sc->nic_addr = sc->iobase;
407 sc->smem_start = (caddr_t)sc->maddr;
408
409 ze_setup(sc);
410
411 tmp = inb (sc->iobase + ZE_RESET);
412 sc->mau = tmp & 0x09 ? "10base2" : "10baseT";
413
414 /* set width/size */
415 sc->type_str = "IBM PCMCIA";
416 memsize = 16*1024;
417 sc->memwidth = 16;
418
419 /* allocate 1 xmit buffer */
420 sc->smem_ring = sc->smem_start + (ED_PAGE_SIZE * ED_TXBUF_SIZE);
421 sc->txb_cnt = 1;
422 sc->rec_page_start = ED_TXBUF_SIZE + ZE_PAGE_OFFSET;
423 sc->smem_size = memsize;
424 sc->smem_end = sc->smem_start + memsize;
425 sc->rec_page_stop = memsize / ED_PAGE_SIZE + ZE_PAGE_OFFSET;
426 sc->tx_page_start = ZE_PAGE_OFFSET;
427
428 /* get station address */
429 for (i = 0; i < ETHER_ADDR_LEN; ++i)
430 sc->arpcom.ac_enaddr[i] = enet_addr[i];
431
432 isa_dev->id_msize = memsize;
433
434
435 /* information for reconfiguration */
436 sc->last_alive = 0;
437 sc->last_up = 0;
438
439 return 32;
440 }
441
442
443 static void
444 ze_setup(struct ze_softc *sc)
445 {
446 int re_init_flag = 0,tmp,slot = sc->slot;
447
448 re_init:
449 /*
450 * (2) map card configuration registers. these are offset
451 * in card memory space by 0x20000. normally we could get
452 * this offset from the card information structure, but I'm
453 * too lazy and am not quite sure if I understand the CIS anyway.
454 *
455 * XXX IF YOU'RE TRYING TO PORT THIS DRIVER FOR A DIFFERENT
456 * PCMCIA CARD, the most likely thing to change is the constant
457 * 0x20000 in the next statement. Oh yes, also change the
458 * card id string that we probe for.
459 */
460 pcic_map_memory (slot, 0, kvtop (sc->maddr), 0x20000, 8L,
461 ATTRIBUTE, 1);
462 POKE(sc->maddr, 0x80); /* reset the card (how long?) */
463 DELAY (40000);
464 /*
465 * Set the configuration index. According to [1], the adapter won't
466 * respond to any i/o signals until we do this; it uses the
467 * Memory Only interface (whatever that is; it's not documented).
468 * Also turn on "level" (not pulse) interrupts.
469 *
470 * XXX probably should init the socket and copy register also,
471 * so that we can deal with multiple instances of the same card.
472 */
473 POKE(sc->maddr, 0x41);
474 pcic_unmap_memory (slot, 0);
475
476 /*
477 * (3) now map in the shared memory buffer. This has to be mapped
478 * as words, not bytes, and on a 16k boundary. The offset value
479 * was derived by installing IBM's POINTETH.SYS under DOS and
480 * looking at the PCIC registers; it's not documented in IBM's
481 * tech ref manual ([1]).
482 */
483 pcic_map_memory (slot, 0, kvtop (sc->maddr), 0x4000L, 0x4000L,
484 COMMON, 2);
485
486 /*
487 * (4) map i/o ports.
488 *
489 * XXX is it possible that the config file leaves this unspecified,
490 * in which case we have to pick one?
491 *
492 * At least one PCMCIA device driver I'v seen maps a block
493 * of 32 consecutive i/o ports as two windows of 16 ports each.
494 * Maybe some other pcic chips are restricted to 16-port windows;
495 * the 82365SL doesn't seem to have that problem. But since
496 * we have an extra window anyway...
497 */
498 #ifdef SHARED_MEMORY
499 pcic_map_io (slot, 0, sc->iobase, 32, 1);
500 #else
501 pcic_map_io (slot, 0, sc->iobase, 16, 1);
502 pcic_map_io (slot, 1, sc->iobase+16, 16, 2);
503 #endif /* SHARED_MEMORY */
504
505 /*
506 * (5) configure the card for the desired interrupt
507 *
508 * XXX is it possible that the config file leaves this unspecified?
509 */
510 pcic_map_irq (slot, ffs (sc->irq) - 1);
511
512 /* tell the PCIC that this is an I/O card (not memory) */
513 pcic_putb (slot, PCIC_INT_GEN,
514 pcic_getb (slot, PCIC_INT_GEN) | PCIC_CARDTYPE);
515
516 #if 0
517 /* tell the PCIC to use level-mode interrupts */
518 /* XXX this register may not be present on all controllers */
519 pcic_putb (slot, PCIC_GLO_CTRL,
520 pcic_getb (slot, PCIC_GLO_CTRL) | PCIC_LVL_MODE);
521 #endif
522
523 #if 0
524 pcic_print_regs (slot);
525 #endif
526
527 /* reset card to force it into a known state */
528 tmp = inb (sc->iobase + ZE_RESET);
529 DELAY(20000);
530 outb (sc->iobase + ZE_RESET, tmp);
531 DELAY(20000);
532
533 #if 0
534 tmp = inb(sc->iobase);
535 printf("CR = 0x%x\n", tmp);
536 #endif
537 /*
538 * query MAM bit in misc register for 10base2
539 */
540 tmp = inb (sc->iobase + ZE_MISC);
541
542 /*
543 * Some Intel-compatible PCICs of Cirrus Logic fails in
544 * initializing them. This is a quick hack to fix this
545 * problem.
546 * HOSOKAWA, Tatsumi <hosokawa@mt.cs.keio.ac.jp>
547 */
548 if (!tmp && !re_init_flag) {
549 re_init_flag++;
550 goto re_init;
551 }
552 }
553
554 #if NAPM > 0
555 static int
556 ze_suspend(visa_dev)
557 void *visa_dev;
558 {
559 struct isa_device *isa_dev = visa_dev;
560 struct ze_softc *sc = &ze_softc[isa_dev->id_unit];
561
562 pcic_power_off(sc->slot);
563 return 0;
564 }
565
566 static int
567 ze_resume(visa_dev)
568 void *visa_dev;
569 {
570 struct isa_device *isa_dev = visa_dev;
571
572 #if 0
573 printf("Resume ze:\n");
574 #endif
575 prev_slot = 0;
576 reconfig_isadev(isa_dev, &net_imask);
577 return 0;
578 }
579 #endif /* NAPM > 0 */
580
581 /*
582 * Install interface into kernel networking data structures
583 */
584
585 static int
586 ze_attach(isa_dev)
587 struct isa_device *isa_dev;
588 {
589 struct ze_softc *sc = &ze_softc[isa_dev->id_unit];
590 struct ifnet *ifp = &sc->arpcom.ac_if;
591 int pl;
592
593 /* PCMCIA card can be offlined. Reconfiguration is required */
594 if (isa_dev->id_reconfig) {
595 ze_reset(isa_dev->id_unit);
596 if (!isa_dev->id_alive && sc->last_alive) {
597 pl = splimp();
598 sc->last_up = (ifp->if_flags & IFF_UP);
599 if_down(ifp);
600 splx(pl);
601 sc->last_alive = 0;
602 }
603 if (isa_dev->id_alive && !sc->last_alive) {
604 if (sc->last_up) {
605 pl = splimp();
606 if_up(ifp);
607 splx(pl);
608 }
609 sc->last_alive = 1;
610 }
611 return 1;
612 }
613 else {
614 sc->last_alive = 1;
615 }
616
617 /*
618 * Set interface to stopped condition (reset)
619 */
620 ze_stop(isa_dev->id_unit);
621
622 /*
623 * Initialize ifnet structure
624 */
625 ifp->if_softc = sc;
626 ifp->if_unit = isa_dev->id_unit;
627 ifp->if_name = "ze" ;
628 ifp->if_mtu = ETHERMTU;
629 ifp->if_output = ether_output;
630 ifp->if_start = ze_start;
631 ifp->if_ioctl = ze_ioctl;
632 ifp->if_watchdog = ze_watchdog;
633
634 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX);
635
636 /*
637 * Attach the interface
638 */
639 if_attach(ifp);
640 ether_ifattach(ifp);
641
642 /*
643 * Print additional info when attached
644 */
645 printf("ze%d: address %6D, type %s (%dbit), MAU %s\n",
646 isa_dev->id_unit,
647 sc->arpcom.ac_enaddr, ":", sc->type_str,
648 sc->memwidth,
649 sc->mau);
650
651 /*
652 * If BPF is in the kernel, call the attach for it
653 */
654 #if NBPFILTER > 0
655 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
656 #endif
657
658 #if NAPM > 0
659 sc->s_hook.ah_fun = ze_suspend;
660 sc->s_hook.ah_arg = (void *)isa_dev;
661 sc->s_hook.ah_name = "IBM PCMCIA Ethernet I/II";
662 sc->s_hook.ah_order = APM_MID_ORDER;
663 apm_hook_establish(APM_HOOK_SUSPEND , &sc->s_hook);
664 sc->r_hook.ah_fun = ze_resume;
665 sc->r_hook.ah_arg = (void *)isa_dev;
666 sc->r_hook.ah_name = "IBM PCMCIA Ethernet I/II";
667 sc->r_hook.ah_order = APM_MID_ORDER;
668 apm_hook_establish(APM_HOOK_RESUME , &sc->r_hook);
669 #endif /* NAPM > 0 */
670
671 return 1;
672 }
673
674 /*
675 * Reset interface.
676 */
677 static void
678 ze_reset(unit)
679 int unit;
680 {
681 int s;
682
683 s = splnet();
684
685 /*
686 * Stop interface and re-initialize.
687 */
688 ze_stop(unit);
689 ze_init(unit);
690
691 (void) splx(s);
692 }
693
694 /*
695 * Take interface offline.
696 */
697 static void
698 ze_stop(unit)
699 int unit;
700 {
701 struct ze_softc *sc = &ze_softc[unit];
702 int n = 5000;
703
704 /*
705 * Stop everything on the interface, and select page 0 registers.
706 */
707 outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2|ED_CR_STP);
708
709 /*
710 * Wait for interface to enter stopped state, but limit # of checks
711 * to 'n' (about 5ms). It shouldn't even take 5us on modern
712 * DS8390's, but just in case it's an old one.
713 */
714 while (((inb(sc->nic_addr + ED_P0_ISR) & ED_ISR_RST) == 0) && --n);
715 pcic_power_off(sc->slot);
716
717 }
718
719 /*
720 * Device timeout/watchdog routine. Entered if the device neglects to
721 * generate an interrupt after a transmit has been started on it.
722 */
723 static void
724 ze_watchdog(ifp)
725 struct ifnet *ifp;
726 {
727 #if 1
728 struct ze_softc *sc = (struct ze_softc *)ifp;
729 u_char isr, imr;
730 u_short imask;
731
732 if(!(ifp->if_flags & IFF_UP))
733 return;
734 /* select page zero */
735 outb (sc->nic_addr + ED_P0_CR,
736 (inb (sc->nic_addr + ED_P0_CR) & 0x3f) | ED_CR_PAGE_0);
737
738 /* read interrupt status register */
739 isr = inb (sc->nic_addr + ED_P0_ISR) & 0xff;
740
741 /* select page two */
742 outb (sc->nic_addr + ED_P0_CR,
743 (inb (sc->nic_addr + ED_P0_CR) & 0x3f) | ED_CR_PAGE_2);
744
745 /* read interrupt mask register */
746 imr = inb (sc->nic_addr + ED_P2_IMR) & 0xff;
747
748 imask = inb(IO_ICU2) << 8 | inb(IO_ICU1);
749
750 log (LOG_ERR, "ze%d: device timeout, isr=%02x, imr=%02x, imask=%04x\n",
751 ifp->if_unit, isr, imr, imask);
752 #else
753 log(LOG_ERR, "ze%d: device timeout\n", ifp->if_unit);
754 #endif
755
756 ze_reset(ifp->if_unit);
757 }
758
759 /*
760 * Initialize device.
761 */
762 static void
763 ze_init(unit)
764 int unit;
765 {
766 struct ze_softc *sc = &ze_softc[unit];
767 struct ifnet *ifp = &sc->arpcom.ac_if;
768 int i, s;
769
770
771 pcic_power_on(sc->slot);
772 pcic_reset(sc->slot);
773 if(!(sc->arpcom.ac_if.if_flags & IFF_UP))
774 Debugger("here!!");
775 ze_setup(sc);
776 /* address not known */
777 if (ifp->if_addrlist == (struct ifaddr *)0) return;
778
779 /*
780 * Initialize the NIC in the exact order outlined in the NS manual.
781 * This init procedure is "mandatory"...don't change what or when
782 * things happen.
783 */
784 s = splnet();
785
786 /* reset transmitter flags */
787 sc->data_buffered = 0;
788 sc->xmit_busy = 0;
789 sc->arpcom.ac_if.if_timer = 0;
790
791 sc->txb_next = 0;
792
793 /* This variable is used below - don't move this assignment */
794 sc->next_packet = sc->rec_page_start + 1;
795
796 /*
797 * Set interface for page 0, Remote DMA complete, Stopped
798 */
799 outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2|ED_CR_STP);
800
801 if (sc->memwidth == 16) {
802 /*
803 * Set FIFO threshold to 8, No auto-init Remote DMA,
804 * byte order=80x86, word-wide DMA xfers
805 */
806 outb(sc->nic_addr + ED_P0_DCR, ED_DCR_FT1|ED_DCR_WTS);
807 } else {
808 /*
809 * Same as above, but byte-wide DMA xfers
810 */
811 outb(sc->nic_addr + ED_P0_DCR, ED_DCR_FT1);
812 }
813
814 /*
815 * Clear Remote Byte Count Registers
816 */
817 outb(sc->nic_addr + ED_P0_RBCR0, 0);
818 outb(sc->nic_addr + ED_P0_RBCR1, 0);
819
820 /*
821 * Enable reception of broadcast packets
822 */
823 outb(sc->nic_addr + ED_P0_RCR, ED_RCR_AB);
824
825 /*
826 * Place NIC in internal loopback mode
827 */
828 outb(sc->nic_addr + ED_P0_TCR, ED_TCR_LB0);
829
830 /*
831 * Initialize transmit/receive (ring-buffer) Page Start
832 */
833 outb(sc->nic_addr + ED_P0_TPSR, sc->tx_page_start);
834 outb(sc->nic_addr + ED_P0_PSTART, sc->rec_page_start);
835
836 /*
837 * Initialize Receiver (ring-buffer) Page Stop and Boundry
838 */
839 outb(sc->nic_addr + ED_P0_PSTOP, sc->rec_page_stop);
840 outb(sc->nic_addr + ED_P0_BNRY, sc->rec_page_start);
841
842 /*
843 * Clear all interrupts. A '1' in each bit position clears the
844 * corresponding flag.
845 */
846 outb(sc->nic_addr + ED_P0_ISR, 0xff);
847
848 /*
849 * Enable the following interrupts: receive/transmit complete,
850 * receive/transmit error, and Receiver OverWrite.
851 *
852 * Counter overflow and Remote DMA complete are *not* enabled.
853 */
854 outb(sc->nic_addr + ED_P0_IMR,
855 ED_IMR_PRXE|ED_IMR_PTXE|ED_IMR_RXEE|ED_IMR_TXEE|ED_IMR_OVWE);
856
857 /*
858 * Program Command Register for page 1
859 */
860 outb(sc->nic_addr + ED_P0_CR, ED_CR_PAGE_1|ED_CR_RD2|ED_CR_STP);
861
862 /*
863 * Copy out our station address
864 */
865 for (i = 0; i < ETHER_ADDR_LEN; ++i)
866 outb(sc->nic_addr + ED_P1_PAR0 + i, sc->arpcom.ac_enaddr[i]);
867
868 #if NBPFILTER > 0
869 /*
870 * Initialize multicast address hashing registers to accept
871 * all multicasts (only used when in promiscuous mode)
872 */
873 for (i = 0; i < 8; ++i)
874 outb(sc->nic_addr + ED_P1_MAR0 + i, 0xff);
875 #endif
876
877 /*
878 * Set Current Page pointer to next_packet (initialized above)
879 */
880 outb(sc->nic_addr + ED_P1_CURR, sc->next_packet);
881
882 /*
883 * Set Command Register for page 0, Remote DMA complete,
884 * and interface Start.
885 */
886 outb(sc->nic_addr + ED_P1_CR, ED_CR_RD2|ED_CR_STA);
887
888 /*
889 * Take interface out of loopback
890 */
891 outb(sc->nic_addr + ED_P0_TCR, 0);
892
893 /*
894 * Set 'running' flag, and clear output active flag.
895 */
896 ifp->if_flags |= IFF_RUNNING;
897 ifp->if_flags &= ~IFF_OACTIVE;
898
899 /*
900 * ...and attempt to start output
901 */
902 ze_start(ifp);
903
904 (void) splx(s);
905 }
906
907 /*
908 * This routine actually starts the transmission on the interface
909 */
910 static __inline void
911 ze_xmit(ifp)
912 struct ifnet *ifp;
913 {
914 struct ze_softc *sc = ifp->if_softc;
915 u_short len = sc->txb_next_len;
916
917 /*
918 * Set NIC for page 0 register access
919 */
920 outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2|ED_CR_STA);
921
922 /*
923 * Set TX buffer start page
924 */
925 outb(sc->nic_addr + ED_P0_TPSR, sc->tx_page_start +
926 sc->txb_next * ED_TXBUF_SIZE);
927
928 /*
929 * Set TX length
930 */
931 outb(sc->nic_addr + ED_P0_TBCR0, len & 0xff);
932 outb(sc->nic_addr + ED_P0_TBCR1, len >> 8);
933
934 /*
935 * Set page 0, Remote DMA complete, Transmit Packet, and *Start*
936 */
937 outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2|ED_CR_TXP|ED_CR_STA);
938
939 sc->xmit_busy = 1;
940 sc->data_buffered = 0;
941
942 /*
943 * Switch buffers if we are doing double-buffered transmits
944 */
945 if ((sc->txb_next == 0) && (sc->txb_cnt > 1))
946 sc->txb_next = 1;
947 else
948 sc->txb_next = 0;
949
950 /*
951 * Set a timer just in case we never hear from the board again
952 */
953 ifp->if_timer = 2;
954 }
955
956 /*
957 * Start output on interface.
958 * We make two assumptions here:
959 * 1) that the current priority is set to splnet _before_ this code
960 * is called *and* is returned to the appropriate priority after
961 * return
962 * 2) that the IFF_OACTIVE flag is checked before this code is called
963 * (i.e. that the output part of the interface is idle)
964 */
965 static void
966 ze_start(ifp)
967 struct ifnet *ifp;
968 {
969 struct ze_softc *sc = ifp->if_softc;
970 struct mbuf *m0, *m;
971 caddr_t buffer;
972 int len;
973
974 outloop:
975 /*
976 * See if there is room to send more data (i.e. one or both of the
977 * buffers is empty).
978 */
979 if (sc->data_buffered)
980 if (sc->xmit_busy) {
981 /*
982 * No room. Indicate this to the outside world
983 * and exit.
984 */
985 ifp->if_flags |= IFF_OACTIVE;
986 return;
987 } else {
988 /*
989 * Data is buffered, but we're not transmitting, so
990 * start the xmit on the buffered data.
991 * Note that ze_xmit() resets the data_buffered flag
992 * before returning.
993 */
994 ze_xmit(ifp);
995 }
996
997 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
998 if (m == NULL) {
999 /*
1000 * The following isn't pretty; we are using the !OACTIVE flag to
1001 * indicate to the outside world that we can accept an additional
1002 * packet rather than that the transmitter is _actually_
1003 * active. Indeed, the transmitter may be active, but if we haven't
1004 * filled the secondary buffer with data then we still want to
1005 * accept more.
1006 * Note that it isn't necessary to test the data_buffered flag -
1007 * we wouldn't have tried to de-queue the packet in the first place
1008 * if it was set.
1009 */
1010 ifp->if_flags &= ~IFF_OACTIVE;
1011 return;
1012 }
1013
1014 /*
1015 * Copy the mbuf chain into the transmit buffer
1016 */
1017
1018 buffer = sc->smem_start + (sc->txb_next * ED_TXBUF_SIZE * ED_PAGE_SIZE);
1019 len = 0;
1020 for (m0 = m; m != 0; m = m->m_next) {
1021 bcopy(mtod(m, caddr_t), buffer, m->m_len);
1022 buffer += m->m_len;
1023 len += m->m_len;
1024 }
1025
1026 sc->txb_next_len = max(len, ETHER_MIN_LEN);
1027
1028 if (sc->txb_cnt > 1)
1029 /*
1030 * only set 'buffered' flag if doing multiple buffers
1031 */
1032 sc->data_buffered = 1;
1033
1034 if (sc->xmit_busy == 0)
1035 ze_xmit(ifp);
1036 /*
1037 * If there is BPF support in the configuration, tap off here.
1038 */
1039 #if NBPFILTER > 0
1040 if (ifp->if_bpf) {
1041 bpf_mtap(ifp, m0);
1042 }
1043 #endif
1044
1045 m_freem(m0);
1046
1047 /*
1048 * If we are doing double-buffering, a buffer might be free to
1049 * fill with another packet, so loop back to the top.
1050 */
1051 if (sc->txb_cnt > 1)
1052 goto outloop;
1053 else {
1054 ifp->if_flags |= IFF_OACTIVE;
1055 return;
1056 }
1057 }
1058
1059 /*
1060 * Ethernet interface receiver interrupt.
1061 */
1062 static __inline void /* only called from one place, so may as well integrate */
1063 ze_rint(unit)
1064 int unit;
1065 {
1066 register struct ze_softc *sc = &ze_softc[unit];
1067 u_char boundry;
1068 u_short len;
1069 struct ed_ring *packet_ptr;
1070
1071 /*
1072 * Set NIC to page 1 registers to get 'current' pointer
1073 */
1074 outb(sc->nic_addr + ED_P0_CR, ED_CR_PAGE_1|ED_CR_RD2|ED_CR_STA);
1075
1076 /*
1077 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
1078 * it points to where new data has been buffered. The 'CURR'
1079 * (current) register points to the logical end of the ring-buffer
1080 * - i.e. it points to where additional new data will be added.
1081 * We loop here until the logical beginning equals the logical
1082 * end (or in other words, until the ring-buffer is empty).
1083 */
1084 while (sc->next_packet != inb(sc->nic_addr + ED_P1_CURR)) {
1085
1086 /* get pointer to this buffer header structure */
1087 packet_ptr = (struct ed_ring *)(sc->smem_ring +
1088 (sc->next_packet - sc->rec_page_start) * ED_PAGE_SIZE);
1089
1090 /*
1091 * The byte count includes the FCS - Frame Check Sequence (a
1092 * 32 bit CRC).
1093 */
1094 len = packet_ptr->count;
1095 if ((len >= ETHER_MIN_LEN) && (len <= ETHER_MAX_LEN)) {
1096 /*
1097 * Go get packet. len - 4 removes CRC from length.
1098 * (packet_ptr + 1) points to data just after the packet ring
1099 * header (+4 bytes)
1100 */
1101 ze_get_packet(sc, (caddr_t)(packet_ptr + 1), len - 4);
1102 ++sc->arpcom.ac_if.if_ipackets;
1103 } else {
1104 /*
1105 * Really BAD...probably indicates that the ring pointers
1106 * are corrupted. Also seen on early rev chips under
1107 * high load - the byte order of the length gets switched.
1108 */
1109 log(LOG_ERR,
1110 "ze%d: shared memory corrupt - invalid packet length %d\n",
1111 unit, len);
1112 ze_reset(unit);
1113 return;
1114 }
1115
1116 /*
1117 * Update next packet pointer
1118 */
1119 sc->next_packet = packet_ptr->next_packet;
1120
1121 /*
1122 * Update NIC boundry pointer - being careful to keep it
1123 * one buffer behind. (as recommended by NS databook)
1124 */
1125 boundry = sc->next_packet - 1;
1126 if (boundry < sc->rec_page_start)
1127 boundry = sc->rec_page_stop - 1;
1128
1129 /*
1130 * Set NIC to page 0 registers to update boundry register
1131 */
1132 outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2|ED_CR_STA);
1133
1134 outb(sc->nic_addr + ED_P0_BNRY, boundry);
1135
1136 /*
1137 * Set NIC to page 1 registers before looping to top (prepare to
1138 * get 'CURR' current pointer)
1139 */
1140 outb(sc->nic_addr + ED_P0_CR, ED_CR_PAGE_1|ED_CR_RD2|ED_CR_STA);
1141 }
1142 }
1143
1144 /*
1145 * Ethernet interface interrupt processor
1146 */
1147 void
1148 zeintr(unit)
1149 int unit;
1150 {
1151 struct ze_softc *sc = &ze_softc[unit];
1152 u_char isr;
1153
1154 if(!(sc->arpcom.ac_if.if_flags & IFF_UP))
1155 return;
1156 /*
1157 * Set NIC to page 0 registers
1158 */
1159 outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2|ED_CR_STA);
1160
1161 /*
1162 * loop until there are no more new interrupts
1163 */
1164 while (isr = inb(sc->nic_addr + ED_P0_ISR)) {
1165
1166 /*
1167 * reset all the bits that we are 'acknowleging'
1168 * by writing a '1' to each bit position that was set
1169 * (writing a '1' *clears* the bit)
1170 */
1171 outb(sc->nic_addr + ED_P0_ISR, isr);
1172
1173 /*
1174 * Transmit error. If a TX completed with an error, we end up
1175 * throwing the packet away. Really the only error that is
1176 * possible is excessive collisions, and in this case it is
1177 * best to allow the automatic mechanisms of TCP to backoff
1178 * the flow. Of course, with UDP we're screwed, but this is
1179 * expected when a network is heavily loaded.
1180 */
1181 if (isr & ED_ISR_TXE) {
1182 u_char tsr = inb(sc->nic_addr + ED_P0_TSR);
1183 u_char ncr = inb(sc->nic_addr + ED_P0_NCR);
1184
1185 /*
1186 * Excessive collisions (16)
1187 */
1188 if ((tsr & ED_TSR_ABT) && (ncr == 0)) {
1189 /*
1190 * When collisions total 16, the P0_NCR will
1191 * indicate 0, and the TSR_ABT is set.
1192 */
1193 sc->arpcom.ac_if.if_collisions += 16;
1194 } else
1195 sc->arpcom.ac_if.if_collisions += ncr;
1196
1197 /*
1198 * update output errors counter
1199 */
1200 ++sc->arpcom.ac_if.if_oerrors;
1201
1202 /*
1203 * reset tx busy and output active flags
1204 */
1205 sc->xmit_busy = 0;
1206 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
1207
1208 /*
1209 * clear watchdog timer
1210 */
1211 sc->arpcom.ac_if.if_timer = 0;
1212 }
1213
1214
1215 /*
1216 * Receiver Error. One or more of: CRC error, frame alignment error
1217 * FIFO overrun, or missed packet.
1218 */
1219 if (isr & ED_ISR_RXE) {
1220 ++sc->arpcom.ac_if.if_ierrors;
1221 #ifdef ZE_DEBUG
1222 printf("ze%d: receive error %b\n", unit,
1223 inb(sc->nic_addr + ED_P0_RSR),
1224 "\2\8DEF\7REC DISAB\6PHY/MC\5MISSED\4OVR\3ALIGN\2FCS\1RCVD");
1225 #endif
1226 }
1227
1228 /*
1229 * Overwrite warning. In order to make sure that a lockup
1230 * of the local DMA hasn't occurred, we reset and
1231 * re-init the NIC. The NSC manual suggests only a
1232 * partial reset/re-init is necessary - but some
1233 * chips seem to want more. The DMA lockup has been
1234 * seen only with early rev chips - Methinks this
1235 * bug was fixed in later revs. -DG
1236 */
1237 if (isr & ED_ISR_OVW) {
1238 ++sc->arpcom.ac_if.if_ierrors;
1239 /*
1240 * Stop/reset/re-init NIC
1241 */
1242 ze_reset(unit);
1243 }
1244
1245 /*
1246 * Transmission completed normally.
1247 */
1248 if (isr & ED_ISR_PTX) {
1249
1250 /*
1251 * reset tx busy and output active flags
1252 */
1253 sc->xmit_busy = 0;
1254 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
1255
1256 /*
1257 * clear watchdog timer
1258 */
1259 sc->arpcom.ac_if.if_timer = 0;
1260
1261 /*
1262 * Update total number of successfully transmitted
1263 * packets.
1264 */
1265 ++sc->arpcom.ac_if.if_opackets;
1266
1267 /*
1268 * Add in total number of collisions on last
1269 * transmission.
1270 */
1271 sc->arpcom.ac_if.if_collisions += inb(sc->nic_addr +
1272 ED_P0_TBCR0);
1273 }
1274
1275 /*
1276 * Receive Completion. Go and get the packet.
1277 * XXX - Doing this on an error is dubious because there
1278 * shouldn't be any data to get (we've configured the
1279 * interface to not accept packets with errors).
1280 */
1281 if (isr & (ED_ISR_PRX|ED_ISR_RXE)) {
1282 ze_rint (unit);
1283 }
1284
1285 /*
1286 * If it looks like the transmitter can take more data,
1287 * attempt to start output on the interface. If data is
1288 * already buffered and ready to go, send it first.
1289 */
1290 if ((sc->arpcom.ac_if.if_flags & IFF_OACTIVE) == 0) {
1291 if (sc->data_buffered)
1292 ze_xmit(&sc->arpcom.ac_if);
1293 ze_start(&sc->arpcom.ac_if);
1294 }
1295
1296 /*
1297 * return NIC CR to standard state: page 0, remote DMA complete,
1298 * start (toggling the TXP bit off, even if was just set
1299 * in the transmit routine, is *okay* - it is 'edge'
1300 * triggered from low to high)
1301 */
1302 outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2|ED_CR_STA);
1303
1304 /*
1305 * If the Network Talley Counters overflow, read them to
1306 * reset them. It appears that old 8390's won't
1307 * clear the ISR flag otherwise - resulting in an
1308 * infinite loop.
1309 */
1310 if (isr & ED_ISR_CNT) {
1311 (void) inb(sc->nic_addr + ED_P0_CNTR0);
1312 (void) inb(sc->nic_addr + ED_P0_CNTR1);
1313 (void) inb(sc->nic_addr + ED_P0_CNTR2);
1314 }
1315 }
1316 }
1317
1318 /*
1319 * Process an ioctl request. This code needs some work - it looks
1320 * pretty ugly.
1321 */
1322 static int
1323 ze_ioctl(ifp, command, data)
1324 register struct ifnet *ifp;
1325 int command;
1326 caddr_t data;
1327 {
1328 register struct ifaddr *ifa = (struct ifaddr *)data;
1329 struct ze_softc *sc = ifp->if_softc;
1330 int s, error = 0;
1331
1332 s = splnet();
1333
1334 switch (command) {
1335
1336 case SIOCSIFADDR:
1337 ifp->if_flags |= IFF_UP;
1338
1339 switch (ifa->ifa_addr->sa_family) {
1340 #ifdef INET
1341 case AF_INET:
1342 ze_init(ifp->if_unit); /* before arpwhohas */
1343 arp_ifinit((struct arpcom*) ifp, ifa);
1344 break;
1345 #endif
1346 #ifdef IPX
1347 /*
1348 * XXX - This code is probably wrong
1349 */
1350 case AF_IPX:
1351 {
1352 register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1353
1354 if (ipx_nullhost(*ina))
1355 ina->x_host =
1356 *(union ipx_host *)(sc->arpcom.ac_enaddr);
1357 else {
1358 /*
1359 *
1360 */
1361 bcopy((caddr_t)ina->x_host.c_host,
1362 (caddr_t)sc->arpcom.ac_enaddr,
1363 sizeof(sc->arpcom.ac_enaddr));
1364 }
1365 /*
1366 * Set new address
1367 */
1368 ze_init(ifp->if_unit);
1369 break;
1370 }
1371 #endif
1372 #ifdef NS
1373 /*
1374 * XXX - This code is probably wrong
1375 */
1376 case AF_NS:
1377 {
1378 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
1379
1380 if (ns_nullhost(*ina))
1381 ina->x_host =
1382 *(union ns_host *)(sc->arpcom.ac_enaddr);
1383 else {
1384 /*
1385 *
1386 */
1387 bcopy((caddr_t)ina->x_host.c_host,
1388 (caddr_t)sc->arpcom.ac_enaddr,
1389 sizeof(sc->arpcom.ac_enaddr));
1390 }
1391 /*
1392 * Set new address
1393 */
1394 ze_init(ifp->if_unit);
1395 break;
1396 }
1397 #endif
1398 default:
1399 ze_init(ifp->if_unit);
1400 break;
1401 }
1402 break;
1403
1404 case SIOCSIFFLAGS:
1405 /*
1406 * When the card is offlined, `up' operation can't be permitted
1407 */
1408 if (!sc->last_alive) {
1409 int tmp;
1410 tmp = (ifp->if_flags & IFF_UP);
1411 if (!sc->last_up && (ifp->if_flags & IFF_UP)) {
1412 ifp->if_flags &= ~(IFF_UP);
1413 }
1414 sc->last_up = tmp;
1415 }
1416 /*
1417 * If interface is marked down and it is running, then stop it
1418 */
1419 if (((ifp->if_flags & IFF_UP) == 0) &&
1420 (ifp->if_flags & IFF_RUNNING)) {
1421 ze_stop(ifp->if_unit);
1422 ifp->if_flags &= ~IFF_RUNNING;
1423 } else {
1424 /*
1425 * If interface is marked up and it is stopped, then start it
1426 */
1427 if ((ifp->if_flags & IFF_UP) &&
1428 ((ifp->if_flags & IFF_RUNNING) == 0))
1429 ze_init(ifp->if_unit);
1430 }
1431 #if NBPFILTER > 0
1432 if (ifp->if_flags & IFF_PROMISC) {
1433 /*
1434 * Set promiscuous mode on interface.
1435 * XXX - for multicasts to work, we would need to
1436 * write 1's in all bits of multicast
1437 * hashing array. For now we assume that
1438 * this was done in ze_init().
1439 */
1440 outb(sc->nic_addr + ED_P0_RCR,
1441 ED_RCR_PRO|ED_RCR_AM|ED_RCR_AB);
1442 } else {
1443 /*
1444 * XXX - for multicasts to work, we would need to
1445 * rewrite the multicast hashing array with the
1446 * proper hash (would have been destroyed above).
1447 */
1448 outb(sc->nic_addr + ED_P0_RCR, ED_RCR_AB);
1449 }
1450 #endif
1451 break;
1452
1453 default:
1454 error = EINVAL;
1455 }
1456 (void) splx(s);
1457 return (error);
1458 }
1459
1460 /*
1461 * Macro to calculate a new address within shared memory when given an offset
1462 * from an address, taking into account ring-wrap.
1463 */
1464 #define ringoffset(sc, start, off, type) \
1465 ((type)( ((caddr_t)(start)+(off) >= (sc)->smem_end) ? \
1466 (((caddr_t)(start)+(off))) - (sc)->smem_end \
1467 + (sc)->smem_ring: \
1468 ((caddr_t)(start)+(off)) ))
1469
1470 /*
1471 * Retreive packet from shared memory and send to the next level up via
1472 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
1473 */
1474 static void
1475 ze_get_packet(sc, buf, len)
1476 struct ze_softc *sc;
1477 char *buf;
1478 u_short len;
1479 {
1480 struct ether_header *eh;
1481 struct mbuf *m, *head = NULL;
1482
1483 /* Allocate a header mbuf */
1484 MGETHDR(m, M_DONTWAIT, MT_DATA);
1485 if (m == NULL)
1486 goto bad;
1487 m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
1488 m->m_pkthdr.len = len;
1489 m->m_len = 0;
1490 head = m;
1491
1492 eh = (struct ether_header *)buf;
1493
1494 /* The following sillines is to make NFS happy */
1495 #define EROUND ((sizeof(struct ether_header) + 3) & ~3)
1496 #define EOFF (EROUND - sizeof(struct ether_header))
1497
1498 /*
1499 * The following assumes there is room for
1500 * the ether header in the header mbuf
1501 */
1502 head->m_data += EOFF;
1503 bcopy(buf, mtod(head, caddr_t), sizeof(struct ether_header));
1504 buf += sizeof(struct ether_header);
1505 head->m_len += sizeof(struct ether_header);
1506 len -= sizeof(struct ether_header);
1507
1508 /*
1509 * Pull packet off interface. Or if this was a trailer packet,
1510 * the data portion is appended.
1511 */
1512 m = ze_ring_to_mbuf(sc, buf, m, len);
1513 if (m == NULL) goto bad;
1514
1515 #if NBPFILTER > 0
1516 /*
1517 * Check if there's a BPF listener on this interface.
1518 * If so, hand off the raw packet to bpf.
1519 */
1520 if (sc->arpcom.ac_if.if_bpf) {
1521 bpf_mtap(&sc->arpcom.ac_if, head);
1522
1523 /*
1524 * Note that the interface cannot be in promiscuous mode if
1525 * there are no BPF listeners. And if we are in promiscuous
1526 * mode, we have to check if this packet is really ours.
1527 *
1528 * XXX This test does not support multicasts.
1529 */
1530 if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC) &&
1531 bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
1532 sizeof(eh->ether_dhost)) != 0 &&
1533 bcmp(eh->ether_dhost, etherbroadcastaddr,
1534 sizeof(eh->ether_dhost)) != 0) {
1535
1536 m_freem(head);
1537 return;
1538 }
1539 }
1540 #endif
1541
1542 /*
1543 * Fix up data start offset in mbuf to point past ether header
1544 */
1545 m_adj(head, sizeof(struct ether_header));
1546
1547 ether_input(&sc->arpcom.ac_if, eh, head);
1548 return;
1549
1550 bad: if (head)
1551 m_freem(head);
1552 return;
1553 }
1554
1555 /*
1556 * Supporting routines
1557 */
1558
1559 /*
1560 * Given a source and destination address, copy 'amount' of a packet from
1561 * the ring buffer into a linear destination buffer. Takes into account
1562 * ring-wrap.
1563 */
1564 static __inline char *
1565 ze_ring_copy(sc,src,dst,amount)
1566 struct ze_softc *sc;
1567 char *src;
1568 char *dst;
1569 u_short amount;
1570 {
1571 u_short tmp_amount;
1572
1573 /* does copy wrap to lower addr in ring buffer? */
1574 if (src + amount > sc->smem_end) {
1575 tmp_amount = sc->smem_end - src;
1576 bcopy(src,dst,tmp_amount); /* copy amount up to end of smem */
1577 amount -= tmp_amount;
1578 src = sc->smem_ring;
1579 dst += tmp_amount;
1580 }
1581
1582 bcopy(src, dst, amount);
1583
1584 return(src + amount);
1585 }
1586
1587 /*
1588 * Copy data from receive buffer to end of mbuf chain
1589 * allocate additional mbufs as needed. return pointer
1590 * to last mbuf in chain.
1591 * sc = ze info (softc)
1592 * src = pointer in ze ring buffer
1593 * dst = pointer to last mbuf in mbuf chain to copy to
1594 * amount = amount of data to copy
1595 */
1596 static struct mbuf *
1597 ze_ring_to_mbuf(sc,src,dst,total_len)
1598 struct ze_softc *sc;
1599 char *src;
1600 struct mbuf *dst;
1601 u_short total_len;
1602 {
1603 register struct mbuf *m = dst;
1604
1605 while (total_len) {
1606 register u_short amount = min(total_len, M_TRAILINGSPACE(m));
1607
1608 if (amount == 0) { /* no more data in this mbuf, alloc another */
1609 /*
1610 * If there is enough data for an mbuf cluster, attempt
1611 * to allocate one of those, otherwise, a regular
1612 * mbuf will do.
1613 * Note that a regular mbuf is always required, even if
1614 * we get a cluster - getting a cluster does not
1615 * allocate any mbufs, and one is needed to assign
1616 * the cluster to. The mbuf that has a cluster
1617 * extension can not be used to contain data - only
1618 * the cluster can contain data.
1619 */
1620 dst = m;
1621 MGET(m, M_DONTWAIT, MT_DATA);
1622 if (m == NULL)
1623 return (0);
1624
1625 if (total_len >= MINCLSIZE)
1626 MCLGET(m, M_DONTWAIT);
1627
1628 m->m_len = 0;
1629 dst->m_next = m;
1630 amount = min(total_len, M_TRAILINGSPACE(m));
1631 }
1632
1633 src = ze_ring_copy(sc, src, mtod(m, caddr_t) + m->m_len, amount);
1634
1635 m->m_len += amount;
1636 total_len -= amount;
1637
1638 }
1639 return (m);
1640 }
1641 #endif
1642
Cache object: 388b424ee20c9458308c6ee7cef250df
|