if_ae.c revision 1.33 1 /* $NetBSD: if_ae.c,v 1.33 1995/07/30 13:38:04 briggs Exp $ */
2
3 /*
4 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
5 * adapters.
6 *
7 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
8 *
9 * Copyright (C) 1993, David Greenman. This software may be used, modified,
10 * copied, distributed, and sold, in both source and binary form provided that
11 * the above copyright and these terms are retained. Under no circumstances is
12 * the author responsible for the proper functioning of this software, nor does
13 * the author assume any responsibility for damages incurred with its use.
14 *
15 * Adapted for MacBSD by Brad Parker <brad (at) fcr.com>.
16 *
17 * Currently supports:
18 * Apples NB Ethernet card
19 * Interlan A310 Nubus Ethernet card
20 * Cayman Systems GatorCard
21 * Asante MacCon II/E
22 */
23
24 #include "bpfilter.h"
25
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/systm.h>
29 #include <sys/errno.h>
30 #include <sys/ioctl.h>
31 #include <sys/mbuf.h>
32 #include <sys/socket.h>
33 #include <sys/syslog.h>
34 #include <sys/device.h>
35
36 #include <net/if.h>
37 #include <net/if_dl.h>
38 #include <net/if_types.h>
39 #include <net/netisr.h>
40
41 #ifdef INET
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/in_var.h>
45 #include <netinet/ip.h>
46 #include <netinet/if_ether.h>
47 #endif
48
49 #ifdef NS
50 #include <netns/ns.h>
51 #include <netns/ns_if.h>
52 #endif
53
54 #if NBPFILTER > 0
55 #include <net/bpf.h>
56 #include <net/bpfdesc.h>
57 #endif
58
59 #include "../mac68k/via.h"
60 #include "nubus.h"
61 #include <dev/ic/dp8390reg.h>
62 #include "if_aereg.h"
63
64 #define INTERFACE_NAME_LEN 32
65
66 /*
67 * ae_softc: per line info and status
68 */
69 struct ae_softc {
70 struct device sc_dev;
71 nubus_slot sc_slot;
72 /* struct intrhand sc_ih; */
73
74 struct arpcom sc_arpcom;/* ethernet common */
75
76 char type_str[INTERFACE_NAME_LEN]; /* type string */
77 u_short type; /* interface type code */
78 u_char vendor; /* interface vendor */
79 u_char regs_rev; /* registers are reversed */
80
81 #define REG_MAP(sc, reg) ((sc)->regs_rev ? (0x0f-(reg))<<2 : (reg)<<2)
82 #define NIC_GET(sc, reg) ((sc)->nic_addr[REG_MAP(sc, reg)])
83 #define NIC_PUT(sc, reg, val) ((sc)->nic_addr[REG_MAP(sc, reg)] = (val))
84 volatile caddr_t nic_addr; /* NIC (DS8390) I/O bus address */
85 caddr_t rom_addr; /* on board prom address */
86
87 u_char cr_proto; /* values always set in CR */
88
89 caddr_t mem_start; /* shared memory start address */
90 caddr_t mem_end; /* shared memory end address */
91 u_long mem_size; /* total shared memory size */
92 caddr_t mem_ring; /* start of RX ring-buffer (in smem) */
93
94 u_char mem_wr_short; /* card memory requires int16 writes */
95
96 u_char xmit_busy; /* transmitter is busy */
97 u_char txb_cnt; /* Number of transmit buffers */
98 u_char txb_inuse; /* number of TX buffers currently in-use */
99
100 u_char txb_new; /* pointer to where new buffer will be added */
101 u_char txb_next_tx; /* pointer to next buffer ready to xmit */
102 u_short txb_len[8]; /* buffered xmit buffer lengths */
103 u_char tx_page_start; /* first page of TX buffer area */
104 u_char rec_page_start; /* first page of RX ring-buffer */
105 u_char rec_page_stop; /* last page of RX ring-buffer */
106 u_char next_packet; /* pointer to next unread RX packet */
107 };
108
109 int aeprobe __P((struct device *, void *, void *));
110 void aeattach __P((struct device *, struct device *, void *));
111 void aeintr __P((struct ae_softc *));
112 int ae_ioctl __P((struct ifnet *, u_long, caddr_t));
113 void ae_start __P((struct ifnet *));
114 void ae_watchdog __P(( /* short */ ));
115 void ae_reset __P((struct ae_softc *));
116 void ae_init __P((struct ae_softc *));
117 void ae_stop __P((struct ae_softc *));
118 void ae_getmcaf __P((struct arpcom *, u_char *));
119 u_short ae_put __P((struct ae_softc *, struct mbuf *, caddr_t));
120
121 #define inline /* XXX for debugging porpoises */
122
123 void ae_get_packet __P(( /* struct ae_softc *, caddr_t, u_short */ ));
124 static inline void ae_rint __P((struct ae_softc *));
125 static inline void ae_xmit __P((struct ae_softc *));
126 static inline caddr_t ae_ring_copy __P((
127 /* struct ae_softc *, caddr_t, caddr_t, u_short */ ));
128
129 struct cfdriver aecd = {
130 NULL, "ae", aeprobe, aeattach, DV_IFNET, sizeof(struct ae_softc)
131 };
132
133 #define ETHER_MIN_LEN 64
134 #define ETHER_MAX_LEN 1518
135 #define ETHER_ADDR_LEN 6
136
137 static char zero = 0;
138 static u_char ones = 0xff;
139
140 /*
141 * XXX These two should be moved to locore, and maybe changed to use shorts
142 * instead of bytes. The reason for these is that bcopy and bzero use longs,
143 * which the ethernet cards can't handle.
144 */
145
146 void
147 bszero(u_short * addr, int len)
148 {
149 while (len--)
150 *addr++ = 0;
151 }
152
153 /*
154 * Memory copy, copies word at time.
155 */
156 static inline void
157 word_copy(a, b, len)
158 caddr_t a, b;
159 int len;
160 {
161 u_short *x = (u_short *) a, *y = (u_short *) b;
162
163 len >>= 1;
164 while (len--)
165 *y++ = *x++;
166 }
167
168 /*
169 * Memory copy, copies bytes at time.
170 */
171 static inline void
172 byte_copy(a, b, len)
173 caddr_t a, b;
174 int len;
175 {
176 while (len--)
177 *b++ = *a++;
178 }
179
180 static int
181 ae_id_card(slot, sc)
182 nubus_slot *slot;
183 struct ae_softc *sc;
184 {
185 nubus_dir dir;
186 nubus_dirent dirent;
187 nubus_type slottype;
188
189 nubus_get_main_dir(slot, &dir);
190
191 if (nubus_find_rsrc(slot, &dir, 0x80, &dirent) <= 0)
192 return 0;
193
194 nubus_get_dir_from_rsrc(slot, &dirent, &dir);
195
196 if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_TYPE, &dirent) <= 0)
197 return 0;
198
199 if (nubus_get_ind_data(slot, &dirent,
200 (caddr_t) &slottype, sizeof(nubus_type)) <= 0)
201 return 0;
202
203 if (slottype.category != NUBUS_CATEGORY_NETWORK)
204 return 0;
205
206 if (slottype.type != NUBUS_TYPE_ETHERNET)
207 return 0;
208
209 switch (slottype.drsw) {
210 case NUBUS_DRSW_3COM:
211 case NUBUS_DRSW_APPLE:
212 sc->vendor = AE_VENDOR_APPLE;
213 break;
214 case NUBUS_DRSW_ASANTE:
215 sc->vendor = AE_VENDOR_ASANTE;
216 break;
217 case NUBUS_DRSW_FARALLON:
218 sc->vendor = AE_VENDOR_FARALLON;
219 break;
220 case NUBUS_DRSW_GATOR:
221 switch (slottype.drhw) {
222 default:
223 case NUBUS_DRHW_INTERLAN:
224 sc->vendor = AE_VENDOR_INTERLAN;
225 break;
226 case NUBUS_DRHW_KINETICS:
227 sc->vendor = AE_VENDOR_DAYNA;
228 break;
229 }
230 break;
231 default:
232 sc->vendor = AE_VENDOR_UNKNOWN;
233 return 0;
234 }
235
236 strncpy(sc->type_str, nubus_get_card_name(slot), INTERFACE_NAME_LEN);
237
238 sc->type_str[INTERFACE_NAME_LEN-1] = '\0';
239
240 return 1;
241 }
242
243 int
244 ae_size_card_memory(sc)
245 struct ae_softc *sc;
246 {
247 u_short *p;
248 u_short i1, i2, i3, i4;
249 int size;
250
251 p = (u_short *) sc->mem_start;
252
253 /*
254 * very simple size memory, assuming it's installed in 8k
255 * banks; also assume it will generally mirror in upper banks
256 * if not installed.
257 */
258 i1 = (8192 * 0) / 2;
259 i2 = (8192 * 1) / 2;
260 i3 = (8192 * 2) / 2;
261 i4 = (8192 * 3) / 2;
262
263 p[i1] = 0x1111;
264 p[i2] = 0x2222;
265 p[i3] = 0x3333;
266 p[i4] = 0x4444;
267
268 if (p[i1] == 0x1111 && p[i2] == 0x2222 &&
269 p[i3] == 0x3333 && p[i4] == 0x4444)
270 return 8192 * 4;
271
272 if ((p[i1] == 0x1111 && p[i2] == 0x2222) ||
273 (p[i1] == 0x3333 && p[i2] == 0x4444))
274 return 8192 * 2;
275
276 if (p[i1] == 0x1111 || p[i1] == 0x4444)
277 return 8192;
278
279 return 0;
280 }
281
282 int
283 aeprobe(parent, match, aux)
284 struct device *parent;
285 void *match, *aux;
286 {
287 struct ae_softc *sc = match;
288 nubus_slot *nu = (nubus_slot *) aux;
289 caddr_t addr;
290 int i, memsize;
291 int flags = 0;
292
293 if (ae_id_card(nu, sc) <= 0)
294 return 0;
295
296 sc->regs_rev = 0;
297 sc->mem_wr_short = 0;
298
299 addr = (caddr_t) NUBUS_SLOT_TO_BASE(nu->slot);
300
301 switch (sc->vendor) {
302 case AE_VENDOR_INTERLAN:
303 sc->nic_addr = addr + GC_NIC_OFFSET;
304 sc->rom_addr = addr + GC_ROM_OFFSET;
305 sc->mem_start = addr + GC_DATA_OFFSET;
306 if ((memsize = ae_size_card_memory(sc)) == 0)
307 return 0;
308
309 /* reset the NIC chip */
310 *((caddr_t) addr + GC_RESET_OFFSET) = (char) zero;
311
312 /* Get station address from on-board ROM */
313 for (i = 0; i < ETHER_ADDR_LEN; ++i)
314 sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 4);
315 break;
316
317 case AE_VENDOR_ASANTE:
318 /* memory writes require *(u_short *) */
319 sc->mem_wr_short = 1;
320 /* otherwise, pretend to be an apple card (fall through) */
321
322 case AE_VENDOR_APPLE:
323 sc->regs_rev = 1;
324 sc->nic_addr = addr + AE_NIC_OFFSET;
325 sc->rom_addr = addr + AE_ROM_OFFSET;
326 sc->mem_start = addr + AE_DATA_OFFSET;
327 if ((memsize = ae_size_card_memory(sc)) == 0)
328 return (0);
329
330 /* Get station address from on-board ROM */
331 for (i = 0; i < ETHER_ADDR_LEN; ++i)
332 sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 2);
333 break;
334
335 case AE_VENDOR_DAYNA:
336 printf("We think we are a Dayna card, but ");
337 sc->nic_addr = addr + DP_NIC_OFFSET;
338 sc->rom_addr = addr + DP_ROM_OFFSET;
339 sc->mem_start = addr + DP_DATA_OFFSET;
340 memsize = 8192;
341
342 /* Get station address from on-board ROM */
343 for (i = 0; i < ETHER_ADDR_LEN; ++i)
344 sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 2);
345 printf("it is dangerous to continue.\n");
346 return (0); /* Since we don't work yet... */
347 break;
348
349 case AE_VENDOR_FARALLON:
350 sc->regs_rev = 1;
351 sc->rom_addr = addr + FE_ROM_OFFSET;
352 sc->nic_addr = addr + AE_NIC_OFFSET;
353 sc->mem_start = addr + AE_DATA_OFFSET;
354 if ((memsize = ae_size_card_memory(sc)) == 0)
355 return (0);
356
357 /* Get station address from on-board ROM */
358 for (i = 0; i < ETHER_ADDR_LEN; ++i)
359 sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i);
360 break;
361
362 default:
363 return (0);
364 break;
365 }
366
367 sc->cr_proto = ED_CR_RD2;
368
369 /* Allocate one xmit buffer if < 16k, two buffers otherwise. */
370 if ((memsize < 16384) || (flags & AE_FLAGS_NO_DOUBLE_BUFFERING))
371 sc->txb_cnt = 1;
372 else
373 sc->txb_cnt = 2;
374
375 sc->tx_page_start = 0;
376 sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
377 sc->rec_page_stop = sc->tx_page_start + (memsize >> ED_PAGE_SHIFT);
378 sc->mem_ring = sc->mem_start + (sc->rec_page_start << ED_PAGE_SHIFT);
379 sc->mem_size = memsize;
380 sc->mem_end = sc->mem_start + memsize;
381
382 /* Now zero memory and verify that it is clear. */
383 bszero((u_short *) sc->mem_start, memsize / 2);
384
385 for (i = 0; i < memsize; ++i)
386 if (sc->mem_start[i]) {
387 printf("%s: failed to clear shared memory at %x - check configuration\n",
388 sc->sc_dev.dv_xname,
389 sc->mem_start + i);
390 return (0);
391 }
392
393 bcopy(nu, &sc->sc_slot, sizeof(nubus_slot));
394
395 return (1);
396 }
397
398 /*
399 * Install interface into kernel networking data structures
400 */
401 void
402 aeattach(parent, self, aux)
403 struct device *parent, *self;
404 void *aux;
405 {
406 struct ae_softc *sc = (void *) self;
407 struct nubus_hw *nu = aux;
408 struct cfdata *cf = sc->sc_dev.dv_cfdata;
409 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
410
411 /* Set interface to stopped condition (reset). */
412 ae_stop(sc);
413
414 /* Initialize ifnet structure. */
415 ifp->if_unit = sc->sc_dev.dv_unit;
416 ifp->if_name = aecd.cd_name;
417 ifp->if_start = ae_start;
418 ifp->if_ioctl = ae_ioctl;
419 ifp->if_watchdog = ae_watchdog;
420 ifp->if_flags =
421 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
422
423 /* Attach the interface. */
424 if_attach(ifp);
425 ether_ifattach(ifp);
426
427 /* Print additional info when attached. */
428 printf(": address %s, ", ether_sprintf(sc->sc_arpcom.ac_enaddr));
429
430 printf("type %s, %dk mem.\n", sc->type_str, sc->mem_size / 1024);
431
432 #if NBPFILTER > 0
433 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
434 #endif
435
436 /* make sure interrupts are vectored to us */
437 add_nubus_intr(sc->sc_slot.slot, aeintr, sc);
438
439 /*
440 * XXX -- enable nubus interrupts here. Should be done elsewhere,
441 * but that currently breaks with some nubus video cards'
442 * interrupts. So we only enable nubus interrupts if we
443 * have an ethernet card... i.e., we do it here.
444 */
445 enable_nubus_intr();
446 }
447 /*
448 * Reset interface.
449 */
450 void
451 ae_reset(sc)
452 struct ae_softc *sc;
453 {
454 int s;
455
456 s = splimp();
457 ae_stop(sc);
458 ae_init(sc);
459 splx(s);
460 }
461 /*
462 * Take interface offline.
463 */
464 void
465 ae_stop(sc)
466 struct ae_softc *sc;
467 {
468 int n = 5000;
469
470 /* Stop everything on the interface, and select page 0 registers. */
471 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
472
473 /*
474 * Wait for interface to enter stopped state, but limit # of checks to
475 * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but
476 * just in case it's an old one.
477 */
478 while (((NIC_GET(sc, ED_P0_ISR) & ED_ISR_RST) == 0) && --n);
479 }
480 /*
481 * Device timeout/watchdog routine. Entered if the device neglects to generate
482 * an interrupt after a transmit has been started on it.
483 */
484 static int aeintr_ctr = 0;
485 void
486 ae_watchdog(unit)
487 int unit;
488 {
489 struct ae_softc *sc = aecd.cd_devs[unit];
490
491 #if 1
492 /*
493 * This is a kludge! The via code seems to miss slot interrupts
494 * sometimes. This kludges around that by calling the handler
495 * by hand if the watchdog is activated. -- XXX (akb)
496 */
497 int i;
498
499 i = aeintr_ctr;
500
501 (*via2itab[1]) (1);
502
503 if (i != aeintr_ctr) {
504 log(LOG_ERR, "ae%d: device timeout, recovered\n", unit);
505 return;
506 }
507 #endif
508
509 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
510 ++sc->sc_arpcom.ac_if.if_oerrors;
511
512 ae_reset(sc);
513 }
514 /*
515 * Initialize device.
516 */
517 void
518 ae_init(sc)
519 struct ae_softc *sc;
520 {
521 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
522 int i, s;
523 u_char command;
524 u_char mcaf[8];
525
526 /*
527 * Initialize the NIC in the exact order outlined in the NS manual.
528 * This init procedure is "mandatory"...don't change what or when
529 * things happen.
530 */
531 s = splimp();
532
533 /* Reset transmitter flags. */
534 sc->xmit_busy = 0;
535 sc->sc_arpcom.ac_if.if_timer = 0;
536
537 sc->txb_inuse = 0;
538 sc->txb_new = 0;
539 sc->txb_next_tx = 0;
540
541 /* Set interface for page 0, remote DMA complete, stopped. */
542 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
543
544 /*
545 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
546 * order=80x86, word-wide DMA xfers,
547 */
548 NIC_PUT(sc, ED_P0_DCR,
549 ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
550
551 /* Clear remote byte count registers. */
552 NIC_PUT(sc, ED_P0_RBCR0, 0);
553 NIC_PUT(sc, ED_P0_RBCR1, 0);
554
555 /* Tell RCR to do nothing for now. */
556 NIC_PUT(sc, ED_P0_RCR, ED_RCR_MON);
557
558 /* Place NIC in internal loopback mode. */
559 NIC_PUT(sc, ED_P0_TCR, ED_TCR_LB0);
560
561 /* Initialize receive buffer ring. */
562 NIC_PUT(sc, ED_P0_TPSR, sc->rec_page_start);
563 NIC_PUT(sc, ED_P0_PSTART, sc->rec_page_start);
564
565 NIC_PUT(sc, ED_P0_PSTOP, sc->rec_page_stop);
566 NIC_PUT(sc, ED_P0_BNRY, sc->rec_page_start);
567
568 /*
569 * Clear all interrupts. A '1' in each bit position clears the
570 * corresponding flag.
571 */
572 NIC_PUT(sc, ED_P0_ISR, 0xff);
573
574 /*
575 * Enable the following interrupts: receive/transmit complete,
576 * receive/transmit error, and Receiver OverWrite.
577 *
578 * Counter overflow and Remote DMA complete are *not* enabled.
579 */
580 NIC_PUT(sc, ED_P0_IMR,
581 ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE |
582 ED_IMR_OVWE);
583
584 /* Program command register for page 1. */
585 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
586
587 /* Copy out our station address. */
588 for (i = 0; i < ETHER_ADDR_LEN; ++i)
589 NIC_PUT(sc, ED_P1_PAR0 + i, sc->sc_arpcom.ac_enaddr[i]);
590
591 /* Set multicast filter on chip. */
592 ae_getmcaf(&sc->sc_arpcom, mcaf);
593 for (i = 0; i < 8; i++)
594 NIC_PUT(sc, ED_P1_MAR0 + i, mcaf[i]);
595
596 /*
597 * Set current page pointer to one page after the boundary pointer, as
598 * recommended in the National manual.
599 */
600 sc->next_packet = sc->rec_page_start + 1;
601 NIC_PUT(sc, ED_P1_CURR, sc->next_packet);
602
603 /* Program command register for page 0. */
604 NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
605
606 i = ED_RCR_AB | ED_RCR_AM;
607 if (ifp->if_flags & IFF_PROMISC) {
608 /*
609 * Set promiscuous mode. Multicast filter was set earlier so
610 * that we should receive all multicast packets.
611 */
612 i |= ED_RCR_PRO | ED_RCR_AR | ED_RCR_SEP;
613 }
614 NIC_PUT(sc, ED_P0_RCR, i);
615
616 /* Take interface out of loopback. */
617 NIC_PUT(sc, ED_P0_TCR, 0);
618
619 /* Fire up the interface. */
620 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
621
622 /* Set 'running' flag, and clear output active flag. */
623 ifp->if_flags |= IFF_RUNNING;
624 ifp->if_flags &= ~IFF_OACTIVE;
625
626 /* ...and attempt to start output. */
627 ae_start(ifp);
628
629 splx(s);
630 }
631 /*
632 * This routine actually starts the transmission on the interface.
633 */
634 static inline void
635 ae_xmit(sc)
636 struct ae_softc *sc;
637 {
638 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
639 u_short len;
640
641 len = sc->txb_len[sc->txb_next_tx];
642
643 /* Set NIC for page 0 register access. */
644 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
645
646 /* Set TX buffer start page. */
647 NIC_PUT(sc, ED_P0_TPSR, sc->tx_page_start +
648 sc->txb_next_tx * ED_TXBUF_SIZE);
649
650 /* Set TX length. */
651 NIC_PUT(sc, ED_P0_TBCR0, len);
652 NIC_PUT(sc, ED_P0_TBCR1, len >> 8);
653
654 /* Set page 0, remote DMA complete, transmit packet, and *start*. */
655 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA);
656 sc->xmit_busy = 1;
657
658 /* Point to next transmit buffer slot and wrap if necessary. */
659 sc->txb_next_tx++;
660 if (sc->txb_next_tx == sc->txb_cnt)
661 sc->txb_next_tx = 0;
662
663 /* Set a timer just in case we never hear from the board again. */
664 ifp->if_timer = 2;
665 }
666 /*
667 * Start output on interface.
668 * We make two assumptions here:
669 * 1) that the current priority is set to splimp _before_ this code
670 * is called *and* is returned to the appropriate priority after
671 * return
672 * 2) that the IFF_OACTIVE flag is checked before this code is called
673 * (i.e. that the output part of the interface is idle)
674 */
675 void
676 ae_start(ifp)
677 struct ifnet *ifp;
678 {
679 struct ae_softc *sc = aecd.cd_devs[ifp->if_unit];
680 struct mbuf *m0, *m;
681 caddr_t buffer;
682 int len;
683
684 outloop:
685 /*
686 * First, see if there are buffered packets and an idle transmitter -
687 * should never happen at this point.
688 */
689 if (sc->txb_inuse && (sc->xmit_busy == 0)) {
690 printf("%s: packets buffered, but transmitter idle\n",
691 sc->sc_dev.dv_xname);
692 ae_xmit(sc);
693 }
694 /* See if there is room to put another packet in the buffer. */
695 if (sc->txb_inuse == sc->txb_cnt) {
696 /* No room. Indicate this to the outside world and exit. */
697 ifp->if_flags |= IFF_OACTIVE;
698 return;
699 }
700 IF_DEQUEUE(&sc->sc_arpcom.ac_if.if_snd, m);
701 if (m == 0) {
702 /*
703 * We are using the !OACTIVE flag to indicate to the outside
704 * world that we can accept an additional packet rather than
705 * that the transmitter is _actually_ active. Indeed, the
706 * transmitter may be active, but if we haven't filled all the
707 * buffers with data then we still want to accept more.
708 */
709 ifp->if_flags &= ~IFF_OACTIVE;
710 return;
711 }
712 /* Copy the mbuf chain into the transmit buffer. */
713 m0 = m;
714
715 /* txb_new points to next open buffer slot. */
716 buffer = sc->mem_start + ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
717
718 len = ae_put(sc, m, buffer);
719
720 sc->txb_len[sc->txb_new] = max(len, ETHER_MIN_LEN);
721 sc->txb_inuse++;
722
723 /* Point to next buffer slot and wrap if necessary. */
724 if (++sc->txb_new == sc->txb_cnt)
725 sc->txb_new = 0;
726
727 if (sc->xmit_busy == 0)
728 ae_xmit(sc);
729
730 #if NBPFILTER > 0
731 /* Tap off here if there is a BPF listener. */
732 if (sc->sc_arpcom.ac_if.if_bpf)
733 bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m0);
734 #endif
735
736 m_freem(m0);
737
738 /* Loop back to the top to possibly buffer more packets. */
739 goto outloop;
740 }
741 /*
742 * Ethernet interface receiver interrupt.
743 */
744 static inline void
745 ae_rint(sc)
746 struct ae_softc *sc;
747 {
748 u_char boundary, current;
749 u_short len;
750 u_char nlen;
751 struct ae_ring packet_hdr;
752 caddr_t packet_ptr, lenp;
753
754 loop:
755 /* Set NIC to page 1 registers to get 'current' pointer. */
756 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
757
758 /*
759 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
760 * it points to where new data has been buffered. The 'CURR' (current)
761 * register points to the logical end of the ring-buffer - i.e. it
762 * points to where additional new data will be added. We loop here
763 * until the logical beginning equals the logical end (or in other
764 * words, until the ring-buffer is empty).
765 */
766 current = NIC_GET(sc, ED_P1_CURR);
767 if (sc->next_packet == current)
768 return;
769
770 /* Set NIC to page 0 registers to update boundary register. */
771 NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
772
773 do {
774 /* Get pointer to this buffer's header structure. */
775 packet_ptr = sc->mem_ring +
776 ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
777
778 /*
779 * The byte count includes a 4 byte header that was added by
780 * the NIC.
781 */
782 packet_hdr = *(struct ae_ring *) packet_ptr;
783 lenp = (caddr_t) &((struct ae_ring *) packet_ptr)->count;
784 packet_hdr.count = lenp[0] | ((u_short)lenp[1] << 8);
785 len = packet_hdr.count;
786
787 /*
788 * Try do deal with old, buggy chips that sometimes duplicate
789 * the low byte of the length into the high byte. We do this
790 * by simply ignoring the high byte of the length and always
791 * recalculating it.
792 *
793 * NOTE: sc->next_packet is pointing at the current packet.
794 */
795 if (packet_hdr.next_packet >= sc->next_packet)
796 nlen = (packet_hdr.next_packet - sc->next_packet);
797 else
798 nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
799 (sc->rec_page_stop - sc->next_packet));
800 --nlen;
801 if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
802 --nlen;
803 len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
804 #ifdef DIAGNOSTIC
805 if (len != packet_hdr.count) {
806 printf("%s: length does not match next packet pointer\n",
807 sc->sc_dev.dv_xname);
808 printf("%s: len %04x nlen %04x start %02x first %02x curr %02x next %02x stop %02x\n",
809 sc->sc_dev.dv_xname, packet_hdr.count, len,
810 sc->rec_page_start, sc->next_packet, current,
811 packet_hdr.next_packet, sc->rec_page_stop);
812 }
813 #endif
814
815 /*
816 * Be fairly liberal about what we allow as a "reasonable"
817 * length so that a [crufty] packet will make it to BPF (and
818 * can thus be analyzed). Note that all that is really
819 * important is that we have a length that will fit into one
820 * mbuf cluster or less; the upper layer protocols can then
821 * figure out the length from their own length field(s).
822 */
823 if (len <= MCLBYTES &&
824 packet_hdr.next_packet >= sc->rec_page_start &&
825 packet_hdr.next_packet < sc->rec_page_stop) {
826 /* Go get packet. */
827 ae_get_packet(sc, packet_ptr + sizeof(struct ae_ring),
828 len - sizeof(struct ae_ring));
829 ++sc->sc_arpcom.ac_if.if_ipackets;
830 } else {
831 /* Really BAD. The ring pointers are corrupted. */
832 log(LOG_ERR,
833 "%s: NIC memory corrupt - invalid packet length %d\n",
834 sc->sc_dev.dv_xname, len);
835 ++sc->sc_arpcom.ac_if.if_ierrors;
836 ae_reset(sc);
837 return;
838 }
839
840 /* Update next packet pointer. */
841 sc->next_packet = packet_hdr.next_packet;
842
843 /*
844 * Update NIC boundary pointer - being careful to keep it one
845 * buffer behind (as recommended by NS databook).
846 */
847 boundary = sc->next_packet - 1;
848 if (boundary < sc->rec_page_start)
849 boundary = sc->rec_page_stop - 1;
850 NIC_PUT(sc, ED_P0_BNRY, boundary);
851 } while (sc->next_packet != current);
852
853 goto loop;
854 }
855 /* Ethernet interface interrupt processor. */
856 void
857 aeintr(sc)
858 struct ae_softc *sc;
859 {
860 u_char isr;
861
862 aeintr_ctr++;
863
864 /* Set NIC to page 0 registers. */
865 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
866
867 isr = NIC_GET(sc, ED_P0_ISR);
868 if (!isr)
869 return;
870
871 /* Loop until there are no more new interrupts. */
872 for (;;) {
873 /*
874 * Reset all the bits that we are 'acknowledging' by writing a
875 * '1' to each bit position that was set.
876 * (Writing a '1' *clears* the bit.)
877 */
878 NIC_PUT(sc, ED_P0_ISR, isr);
879
880 /*
881 * Handle transmitter interrupts. Handle these first because
882 * the receiver will reset the board under some conditions.
883 */
884 if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
885 u_char collisions = NIC_GET(sc, ED_P0_NCR) & 0x0f;
886
887 /*
888 * Check for transmit error. If a TX completed with an
889 * error, we end up throwing the packet away. Really
890 * the only error that is possible is excessive
891 * collisions, and in this case it is best to allow the
892 * automatic mechanisms of TCP to backoff the flow. Of
893 * course, with UDP we're screwed, but this is expected
894 * when a network is heavily loaded.
895 */
896 (void) NIC_GET(sc, ED_P0_TSR);
897 if (isr & ED_ISR_TXE) {
898 /*
899 * Excessive collisions (16).
900 */
901 if ((NIC_GET(sc, ED_P0_TSR) & ED_TSR_ABT)
902 && (collisions == 0)) {
903 /*
904 * When collisions total 16, the P0_NCR
905 * will indicate 0, and the TSR_ABT is
906 * set.
907 */
908 collisions = 16;
909 }
910 /* Update output errors counter. */
911 ++sc->sc_arpcom.ac_if.if_oerrors;
912 } else {
913 /*
914 * Update total number of successfully
915 * transmitted packets.
916 */
917 ++sc->sc_arpcom.ac_if.if_opackets;
918 }
919
920 /* Reset TX busy and output active flags. */
921 sc->xmit_busy = 0;
922 sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
923
924 /* Clear watchdog timer. */
925 sc->sc_arpcom.ac_if.if_timer = 0;
926
927 /*
928 * Add in total number of collisions on last
929 * transmission.
930 */
931 sc->sc_arpcom.ac_if.if_collisions += collisions;
932
933 /*
934 * Decrement buffer in-use count if not zero (can only
935 * be zero if a transmitter interrupt occured while not
936 * actually transmitting).
937 * If data is ready to transmit, start it transmitting,
938 * otherwise defer until after handling receiver.
939 */
940 if (sc->txb_inuse && --sc->txb_inuse)
941 ae_xmit(sc);
942 }
943 /* Handle receiver interrupts. */
944 if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
945 /*
946 * Overwrite warning. In order to make sure that a
947 * lockup of the local DMA hasn't occurred, we reset
948 * and re-init the NIC. The NSC manual suggests only a
949 * partial reset/re-init is necessary - but some chips
950 * seem to want more. The DMA lockup has been seen
951 * only with early rev chips - Methinks this bug was
952 * fixed in later revs. -DG
953 */
954 if (isr & ED_ISR_OVW) {
955 ++sc->sc_arpcom.ac_if.if_ierrors;
956 #ifdef DIAGNOSTIC
957 log(LOG_WARNING,
958 "%s: warning - receiver ring buffer overrun\n",
959 sc->sc_dev.dv_xname);
960 #endif
961 /* Stop/reset/re-init NIC. */
962 ae_reset(sc);
963 } else {
964 /*
965 * Receiver Error. One or more of: CRC error,
966 * frame alignment error FIFO overrun, or
967 * missed packet.
968 */
969 if (isr & ED_ISR_RXE) {
970 ++sc->sc_arpcom.ac_if.if_ierrors;
971 #ifdef AE_DEBUG
972 printf("%s: receive error %x\n",
973 sc->sc_dev.dv_xname,
974 NIC_GET(sc, ED_P0_RSR));
975 #endif
976 }
977 /*
978 * Go get the packet(s)
979 * XXX - Doing this on an error is dubious
980 * because there shouldn't be any data to get
981 * (we've configured the interface to not
982 * accept packets with errors).
983 */
984 ae_rint(sc);
985 }
986 }
987 /*
988 * If it looks like the transmitter can take more data, attempt
989 * to start output on the interface. This is done after
990 * handling the receiver to give the receiver priority.
991 */
992 if ((sc->sc_arpcom.ac_if.if_flags & IFF_OACTIVE) == 0)
993 ae_start(&sc->sc_arpcom.ac_if);
994
995 /*
996 * Return NIC CR to standard state: page 0, remote DMA
997 * complete, start (toggling the TXP bit off, even if was just
998 * set in the transmit routine, is *okay* - it is 'edge'
999 * triggered from low to high).
1000 */
1001 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
1002
1003 /*
1004 * If the Network Talley Counters overflow, read them to reset
1005 * them. It appears that old 8390's won't clear the ISR flag
1006 * otherwise - resulting in an infinite loop.
1007 */
1008 if (isr & ED_ISR_CNT) {
1009 (void) NIC_GET(sc, ED_P0_CNTR0);
1010 (void) NIC_GET(sc, ED_P0_CNTR1);
1011 (void) NIC_GET(sc, ED_P0_CNTR2);
1012 }
1013 isr = NIC_GET(sc, ED_P0_ISR);
1014 if (!isr)
1015 return;
1016 }
1017 }
1018 /*
1019 * Process an ioctl request. This code needs some work - it looks pretty ugly.
1020 */
1021 int
1022 ae_ioctl(ifp, command, data)
1023 register struct ifnet *ifp;
1024 u_long command;
1025 caddr_t data;
1026 {
1027 struct ae_softc *sc = aecd.cd_devs[ifp->if_unit];
1028 register struct ifaddr *ifa = (struct ifaddr *) data;
1029 struct ifreq *ifr = (struct ifreq *) data;
1030 int s, error = 0;
1031
1032 s = splimp();
1033
1034 switch (command) {
1035
1036 case SIOCSIFADDR:
1037 ifp->if_flags |= IFF_UP;
1038
1039 switch (ifa->ifa_addr->sa_family) {
1040 #ifdef INET
1041 case AF_INET:
1042 ae_init(sc);
1043 arp_ifinit(&sc->sc_arpcom, ifa);
1044 break;
1045 #endif
1046 #ifdef NS
1047 /* XXX - This code is probably wrong. */
1048 case AF_NS:
1049 {
1050 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1051
1052 if (ns_nullhost(*ina))
1053 ina->x_host =
1054 *(union ns_host *) (sc->sc_arpcom.ac_enaddr);
1055 else
1056 bcopy(ina->x_host.c_host,
1057 sc->sc_arpcom.ac_enaddr,
1058 sizeof(sc->sc_arpcom.ac_enaddr));
1059 /* Set new address. */
1060 ae_init(sc);
1061 break;
1062 }
1063 #endif
1064 default:
1065 ae_init(sc);
1066 break;
1067 }
1068 break;
1069
1070 case SIOCSIFFLAGS:
1071 if ((ifp->if_flags & IFF_UP) == 0 &&
1072 (ifp->if_flags & IFF_RUNNING) != 0) {
1073 /*
1074 * If interface is marked down and it is running, then
1075 * stop it.
1076 */
1077 ae_stop(sc);
1078 ifp->if_flags &= ~IFF_RUNNING;
1079 } else
1080 if ((ifp->if_flags & IFF_UP) != 0 &&
1081 (ifp->if_flags & IFF_RUNNING) == 0) {
1082 /*
1083 * If interface is marked up and it is stopped, then
1084 * start it.
1085 */
1086 ae_init(sc);
1087 } else {
1088 /*
1089 * Reset the interface to pick up changes in any other
1090 * flags that affect hardware registers.
1091 */
1092 ae_stop(sc);
1093 ae_init(sc);
1094 }
1095 break;
1096
1097 case SIOCADDMULTI:
1098 case SIOCDELMULTI:
1099 /* Update our multicast list. */
1100 error = (command == SIOCADDMULTI) ?
1101 ether_addmulti(ifr, &sc->sc_arpcom) :
1102 ether_delmulti(ifr, &sc->sc_arpcom);
1103
1104 if (error == ENETRESET) {
1105 /*
1106 * Multicast list has changed; set the hardware filter
1107 * accordingly.
1108 */
1109 ae_stop(sc); /* XXX for ds_setmcaf? */
1110 ae_init(sc);
1111 error = 0;
1112 }
1113 break;
1114
1115 default:
1116 error = EINVAL;
1117 }
1118
1119 splx(s);
1120 return (error);
1121 }
1122 /*
1123 * Retreive packet from shared memory and send to the next level up via
1124 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
1125 */
1126 void
1127 ae_get_packet(sc, buf, len)
1128 struct ae_softc *sc;
1129 caddr_t buf;
1130 u_short len;
1131 {
1132 struct ether_header *eh;
1133 struct mbuf *m, *ae_ring_to_mbuf();
1134
1135 /* Allocate a header mbuf. */
1136 MGETHDR(m, M_DONTWAIT, MT_DATA);
1137 if (m == 0)
1138 return;
1139 m->m_pkthdr.rcvif = &sc->sc_arpcom.ac_if;
1140 m->m_pkthdr.len = len;
1141 m->m_len = 0;
1142
1143 /* The following silliness is to make NFS happy. */
1144 #define EROUND ((sizeof(struct ether_header) + 3) & ~3)
1145 #define EOFF (EROUND - sizeof(struct ether_header))
1146
1147 /*
1148 * The following assumes there is room for the ether header in the
1149 * header mbuf.
1150 */
1151 m->m_data += EOFF;
1152 eh = mtod(m, struct ether_header *);
1153
1154 word_copy(buf, mtod(m, caddr_t), sizeof(struct ether_header));
1155 buf += sizeof(struct ether_header);
1156 m->m_len += sizeof(struct ether_header);
1157 len -= sizeof(struct ether_header);
1158
1159 /* Pull packet off interface. */
1160 if (ae_ring_to_mbuf(sc, buf, m, len) == 0) {
1161 m_freem(m);
1162 return;
1163 }
1164 #if NBPFILTER > 0
1165 /*
1166 * Check if there's a BPF listener on this interface. If so, hand off
1167 * the raw packet to bpf.
1168 */
1169 if (sc->sc_arpcom.ac_if.if_bpf) {
1170 bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m);
1171
1172 /*
1173 * Note that the interface cannot be in promiscuous mode if
1174 * there are no BPF listeners. And if we are in promiscuous
1175 * mode, we have to check if this packet is really ours.
1176 */
1177 if ((sc->sc_arpcom.ac_if.if_flags & IFF_PROMISC) &&
1178 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
1179 bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
1180 sizeof(eh->ether_dhost)) != 0) {
1181 m_freem(m);
1182 return;
1183 }
1184 }
1185 #endif
1186
1187 /* Fix up data start offset in mbuf to point past ether header. */
1188 m_adj(m, sizeof(struct ether_header));
1189 ether_input(&sc->sc_arpcom.ac_if, eh, m);
1190 }
1191 /*
1192 * Supporting routines.
1193 */
1194
1195 /*
1196 * Given a source and destination address, copy 'amount' of a packet from the
1197 * ring buffer into a linear destination buffer. Takes into account ring-wrap.
1198 */
1199 static inline caddr_t
1200 ae_ring_copy(sc, src, dst, amount)
1201 struct ae_softc *sc;
1202 caddr_t src, dst;
1203 u_short amount;
1204 {
1205 u_short tmp_amount;
1206
1207 /* Does copy wrap to lower addr in ring buffer? */
1208 if (src + amount > sc->mem_end) {
1209 tmp_amount = sc->mem_end - src;
1210
1211 /* Copy amount up to end of NIC memory. */
1212 byte_copy(src, dst, tmp_amount);
1213
1214 amount -= tmp_amount;
1215 src = sc->mem_ring;
1216 dst += tmp_amount;
1217 }
1218 byte_copy(src, dst, amount);
1219
1220 return (src + amount);
1221 }
1222 /*
1223 * Copy data from receive buffer to end of mbuf chain allocate additional mbufs
1224 * as needed. Return pointer to last mbuf in chain.
1225 * sc = ae info (softc)
1226 * src = pointer in ae ring buffer
1227 * dst = pointer to last mbuf in mbuf chain to copy to
1228 * amount = amount of data to copy
1229 */
1230 struct mbuf *
1231 ae_ring_to_mbuf(sc, src, dst, total_len)
1232 struct ae_softc *sc;
1233 caddr_t src;
1234 struct mbuf *dst;
1235 u_short total_len;
1236 {
1237 register struct mbuf *m = dst;
1238
1239 while (total_len) {
1240 register u_short amount = min(total_len, M_TRAILINGSPACE(m));
1241
1242 if (amount == 0) {
1243 /*
1244 * No more data in this mbuf; alloc another.
1245 *
1246 * If there is enough data for an mbuf cluster, attempt
1247 * to allocate one of those, otherwise, a regular mbuf
1248 * will do.
1249 * Note that a regular mbuf is always required, even if
1250 * we get a cluster - getting a cluster does not
1251 * allocate any mbufs, and one is needed to assign the
1252 * cluster to. The mbuf that has a cluster extension
1253 * can not be used to contain data - only the cluster
1254 * can contain data.
1255 */
1256 dst = m;
1257 MGET(m, M_DONTWAIT, MT_DATA);
1258 if (m == 0)
1259 return (0);
1260
1261 if (total_len >= MINCLSIZE)
1262 MCLGET(m, M_DONTWAIT);
1263
1264 m->m_len = 0;
1265 dst->m_next = m;
1266 amount = min(total_len, M_TRAILINGSPACE(m));
1267 }
1268 src = ae_ring_copy(sc, src, mtod(m, caddr_t) + m->m_len,
1269 amount);
1270
1271 m->m_len += amount;
1272 total_len -= amount;
1273 }
1274 return (m);
1275 }
1276 /*
1277 * Compute the multicast address filter from the list of multicast addresses we
1278 * need to listen to.
1279 */
1280 void
1281 ae_getmcaf(ac, af)
1282 struct arpcom *ac;
1283 u_char *af;
1284 {
1285 struct ifnet *ifp = &ac->ac_if;
1286 struct ether_multi *enm;
1287 register u_char *cp, c;
1288 register u_long crc;
1289 register int i, len;
1290 struct ether_multistep step;
1291
1292 /*
1293 * Set up multicast address filter by passing all multicast addresses
1294 * through a crc generator, and then using the high order 6 bits as an
1295 * index into the 64 bit logical address filter. The high order bit
1296 * selects the word, while the rest of the bits select the bit within
1297 * the word.
1298 */
1299
1300 if (ifp->if_flags & IFF_PROMISC) {
1301 ifp->if_flags |= IFF_ALLMULTI;
1302 for (i = 0; i < 8; i++)
1303 af[i] = 0xff;
1304 return;
1305 }
1306 for (i = 0; i < 8; i++)
1307 af[i] = 0;
1308 ETHER_FIRST_MULTI(step, ac, enm);
1309 while (enm != NULL) {
1310 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
1311 sizeof(enm->enm_addrlo)) != 0) {
1312 /*
1313 * We must listen to a range of multicast addresses.
1314 * For now, just accept all multicasts, rather than
1315 * trying to set only those filter bits needed to match
1316 * the range. (At this time, the only use of address
1317 * ranges is for IP multicast routing, for which the
1318 * range is big enough to require all bits set.)
1319 */
1320 ifp->if_flags |= IFF_ALLMULTI;
1321 for (i = 0; i < 8; i++)
1322 af[i] = 0xff;
1323 return;
1324 }
1325 cp = enm->enm_addrlo;
1326 crc = 0xffffffff;
1327 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1328 c = *cp++;
1329 for (i = 8; --i >= 0;) {
1330 if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
1331 crc <<= 1;
1332 crc ^= 0x04c11db6 | 1;
1333 } else
1334 crc <<= 1;
1335 c >>= 1;
1336 }
1337 }
1338 /* Just want the 6 most significant bits. */
1339 crc >>= 26;
1340
1341 /* Turn on the corresponding bit in the filter. */
1342 af[crc >> 3] |= 1 << (crc & 0x7);
1343
1344 ETHER_NEXT_MULTI(step, enm);
1345 }
1346 ifp->if_flags &= ~IFF_ALLMULTI;
1347 }
1348 /*
1349 * Copy packet from mbuf to the board memory
1350 *
1351 * Currently uses an extra buffer/extra memory copy,
1352 * unless the whole packet fits in one mbuf.
1353 *
1354 */
1355 u_short
1356 ae_put(sc, m, buf)
1357 struct ae_softc *sc;
1358 struct mbuf *m;
1359 caddr_t buf;
1360 {
1361 u_char *data, savebyte[2];
1362 int len, wantbyte;
1363 u_short totlen = 0;
1364
1365 wantbyte = 0;
1366
1367 for (; m != 0; m = m->m_next) {
1368 data = mtod(m, u_char *);
1369 len = m->m_len;
1370 totlen += len;
1371 if (len > 0) {
1372 /* Finish the last word. */
1373 if (wantbyte) {
1374 savebyte[1] = *data;
1375 word_copy(savebyte, buf, 2);
1376 buf += 2;
1377 data++;
1378 len--;
1379 wantbyte = 0;
1380 }
1381 /* Output contiguous words. */
1382 if (len > 1) {
1383 word_copy(data, buf, len);
1384 buf += len & ~1;
1385 data += len & ~1;
1386 len &= 1;
1387 }
1388 /* Save last byte, if necessary. */
1389 if (len == 1) {
1390 savebyte[0] = *data;
1391 wantbyte = 1;
1392 }
1393 }
1394 }
1395
1396 if (wantbyte) {
1397 savebyte[1] = 0;
1398 word_copy(savebyte, buf, 2);
1399 }
1400 return (totlen);
1401 }
1402