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