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