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