if_ae.c revision 1.32 1 /* $NetBSD: if_ae.c,v 1.32 1995/07/30 02:44:27 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_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, lenp;
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 lenp = (caddr_t) &((struct ae_ring *) packet_ptr)->count;
768 packet_hdr.count = lenp[0] | ((u_short)lenp[1] << 8);
769 len = packet_hdr.count;
770
771 /*
772 * Try do deal with old, buggy chips that sometimes duplicate
773 * the low byte of the length into the high byte. We do this
774 * by simply ignoring the high byte of the length and always
775 * recalculating it.
776 *
777 * NOTE: sc->next_packet is pointing at the current packet.
778 */
779 if (packet_hdr.next_packet >= sc->next_packet)
780 nlen = (packet_hdr.next_packet - sc->next_packet);
781 else
782 nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
783 (sc->rec_page_stop - sc->next_packet));
784 --nlen;
785 if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
786 --nlen;
787 len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
788 #ifdef DIAGNOSTIC
789 if (len != packet_hdr.count) {
790 printf("%s: length does not match next packet pointer\n",
791 sc->sc_dev.dv_xname);
792 printf("%s: len %04x nlen %04x start %02x first %02x curr %02x next %02x stop %02x\n",
793 sc->sc_dev.dv_xname, packet_hdr.count, len,
794 sc->rec_page_start, sc->next_packet, current,
795 packet_hdr.next_packet, sc->rec_page_stop);
796 }
797 #endif
798
799 /*
800 * Be fairly liberal about what we allow as a "reasonable"
801 * length so that a [crufty] packet will make it to BPF (and
802 * can thus be analyzed). Note that all that is really
803 * important is that we have a length that will fit into one
804 * mbuf cluster or less; the upper layer protocols can then
805 * figure out the length from their own length field(s).
806 */
807 if (len <= MCLBYTES &&
808 packet_hdr.next_packet >= sc->rec_page_start &&
809 packet_hdr.next_packet < sc->rec_page_stop) {
810 /* Go get packet. */
811 ae_get_packet(sc, packet_ptr + sizeof(struct ae_ring),
812 len - sizeof(struct ae_ring));
813 ++sc->sc_arpcom.ac_if.if_ipackets;
814 } else {
815 /* Really BAD. The ring pointers are corrupted. */
816 log(LOG_ERR,
817 "%s: NIC memory corrupt - invalid packet length %d\n",
818 sc->sc_dev.dv_xname, len);
819 ++sc->sc_arpcom.ac_if.if_ierrors;
820 ae_reset(sc);
821 return;
822 }
823
824 /* Update next packet pointer. */
825 sc->next_packet = packet_hdr.next_packet;
826
827 /*
828 * Update NIC boundary pointer - being careful to keep it one
829 * buffer behind (as recommended by NS databook).
830 */
831 boundary = sc->next_packet - 1;
832 if (boundary < sc->rec_page_start)
833 boundary = sc->rec_page_stop - 1;
834 NIC_PUT(sc, ED_P0_BNRY, boundary);
835 } while (sc->next_packet != current);
836
837 goto loop;
838 }
839 /* Ethernet interface interrupt processor. */
840 void
841 aeintr(sc)
842 struct ae_softc *sc;
843 {
844 u_char isr;
845
846 aeintr_ctr++;
847
848 /* Set NIC to page 0 registers. */
849 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
850
851 isr = NIC_GET(sc, ED_P0_ISR);
852 if (!isr)
853 return;
854
855 /* Loop until there are no more new interrupts. */
856 for (;;) {
857 /*
858 * Reset all the bits that we are 'acknowledging' by writing a
859 * '1' to each bit position that was set.
860 * (Writing a '1' *clears* the bit.)
861 */
862 NIC_PUT(sc, ED_P0_ISR, isr);
863
864 /*
865 * Handle transmitter interrupts. Handle these first because
866 * the receiver will reset the board under some conditions.
867 */
868 if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
869 u_char collisions = NIC_GET(sc, ED_P0_NCR) & 0x0f;
870
871 /*
872 * Check for transmit error. If a TX completed with an
873 * error, we end up throwing the packet away. Really
874 * the only error that is possible is excessive
875 * collisions, and in this case it is best to allow the
876 * automatic mechanisms of TCP to backoff the flow. Of
877 * course, with UDP we're screwed, but this is expected
878 * when a network is heavily loaded.
879 */
880 (void) NIC_GET(sc, ED_P0_TSR);
881 if (isr & ED_ISR_TXE) {
882 /*
883 * Excessive collisions (16).
884 */
885 if ((NIC_GET(sc, ED_P0_TSR) & ED_TSR_ABT)
886 && (collisions == 0)) {
887 /*
888 * When collisions total 16, the P0_NCR
889 * will indicate 0, and the TSR_ABT is
890 * set.
891 */
892 collisions = 16;
893 }
894 /* Update output errors counter. */
895 ++sc->sc_arpcom.ac_if.if_oerrors;
896 } else {
897 /*
898 * Update total number of successfully
899 * transmitted packets.
900 */
901 ++sc->sc_arpcom.ac_if.if_opackets;
902 }
903
904 /* Reset TX busy and output active flags. */
905 sc->xmit_busy = 0;
906 sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
907
908 /* Clear watchdog timer. */
909 sc->sc_arpcom.ac_if.if_timer = 0;
910
911 /*
912 * Add in total number of collisions on last
913 * transmission.
914 */
915 sc->sc_arpcom.ac_if.if_collisions += collisions;
916
917 /*
918 * Decrement buffer in-use count if not zero (can only
919 * be zero if a transmitter interrupt occured while not
920 * actually transmitting).
921 * If data is ready to transmit, start it transmitting,
922 * otherwise defer until after handling receiver.
923 */
924 if (sc->txb_inuse && --sc->txb_inuse)
925 ae_xmit(sc);
926 }
927 /* Handle receiver interrupts. */
928 if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
929 /*
930 * Overwrite warning. In order to make sure that a
931 * lockup of the local DMA hasn't occurred, we reset
932 * and re-init the NIC. The NSC manual suggests only a
933 * partial reset/re-init is necessary - but some chips
934 * seem to want more. The DMA lockup has been seen
935 * only with early rev chips - Methinks this bug was
936 * fixed in later revs. -DG
937 */
938 if (isr & ED_ISR_OVW) {
939 ++sc->sc_arpcom.ac_if.if_ierrors;
940 #ifdef DIAGNOSTIC
941 log(LOG_WARNING,
942 "%s: warning - receiver ring buffer overrun\n",
943 sc->sc_dev.dv_xname);
944 #endif
945 /* Stop/reset/re-init NIC. */
946 ae_reset(sc);
947 } else {
948 /*
949 * Receiver Error. One or more of: CRC error,
950 * frame alignment error FIFO overrun, or
951 * missed packet.
952 */
953 if (isr & ED_ISR_RXE) {
954 ++sc->sc_arpcom.ac_if.if_ierrors;
955 #ifdef AE_DEBUG
956 printf("%s: receive error %x\n",
957 sc->sc_dev.dv_xname,
958 NIC_GET(sc, ED_P0_RSR));
959 #endif
960 }
961 /*
962 * Go get the packet(s)
963 * XXX - Doing this on an error is dubious
964 * because there shouldn't be any data to get
965 * (we've configured the interface to not
966 * accept packets with errors).
967 */
968 ae_rint(sc);
969 }
970 }
971 /*
972 * If it looks like the transmitter can take more data, attempt
973 * to start output on the interface. This is done after
974 * handling the receiver to give the receiver priority.
975 */
976 if ((sc->sc_arpcom.ac_if.if_flags & IFF_OACTIVE) == 0)
977 ae_start(&sc->sc_arpcom.ac_if);
978
979 /*
980 * Return NIC CR to standard state: page 0, remote DMA
981 * complete, start (toggling the TXP bit off, even if was just
982 * set in the transmit routine, is *okay* - it is 'edge'
983 * triggered from low to high).
984 */
985 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
986
987 /*
988 * If the Network Talley Counters overflow, read them to reset
989 * them. It appears that old 8390's won't clear the ISR flag
990 * otherwise - resulting in an infinite loop.
991 */
992 if (isr & ED_ISR_CNT) {
993 (void) NIC_GET(sc, ED_P0_CNTR0);
994 (void) NIC_GET(sc, ED_P0_CNTR1);
995 (void) NIC_GET(sc, ED_P0_CNTR2);
996 }
997 isr = NIC_GET(sc, ED_P0_ISR);
998 if (!isr)
999 return;
1000 }
1001 }
1002 /*
1003 * Process an ioctl request. This code needs some work - it looks pretty ugly.
1004 */
1005 int
1006 ae_ioctl(ifp, command, data)
1007 register struct ifnet *ifp;
1008 u_long command;
1009 caddr_t data;
1010 {
1011 struct ae_softc *sc = aecd.cd_devs[ifp->if_unit];
1012 register struct ifaddr *ifa = (struct ifaddr *) data;
1013 struct ifreq *ifr = (struct ifreq *) data;
1014 int s, error = 0;
1015
1016 s = splimp();
1017
1018 switch (command) {
1019
1020 case SIOCSIFADDR:
1021 ifp->if_flags |= IFF_UP;
1022
1023 switch (ifa->ifa_addr->sa_family) {
1024 #ifdef INET
1025 case AF_INET:
1026 ae_init(sc);
1027 arp_ifinit(&sc->sc_arpcom, ifa);
1028 break;
1029 #endif
1030 #ifdef NS
1031 /* XXX - This code is probably wrong. */
1032 case AF_NS:
1033 {
1034 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1035
1036 if (ns_nullhost(*ina))
1037 ina->x_host =
1038 *(union ns_host *) (sc->sc_arpcom.ac_enaddr);
1039 else
1040 bcopy(ina->x_host.c_host,
1041 sc->sc_arpcom.ac_enaddr,
1042 sizeof(sc->sc_arpcom.ac_enaddr));
1043 /* Set new address. */
1044 ae_init(sc);
1045 break;
1046 }
1047 #endif
1048 default:
1049 ae_init(sc);
1050 break;
1051 }
1052 break;
1053
1054 case SIOCSIFFLAGS:
1055 if ((ifp->if_flags & IFF_UP) == 0 &&
1056 (ifp->if_flags & IFF_RUNNING) != 0) {
1057 /*
1058 * If interface is marked down and it is running, then
1059 * stop it.
1060 */
1061 ae_stop(sc);
1062 ifp->if_flags &= ~IFF_RUNNING;
1063 } else
1064 if ((ifp->if_flags & IFF_UP) != 0 &&
1065 (ifp->if_flags & IFF_RUNNING) == 0) {
1066 /*
1067 * If interface is marked up and it is stopped, then
1068 * start it.
1069 */
1070 ae_init(sc);
1071 } else {
1072 /*
1073 * Reset the interface to pick up changes in any other
1074 * flags that affect hardware registers.
1075 */
1076 ae_stop(sc);
1077 ae_init(sc);
1078 }
1079 break;
1080
1081 case SIOCADDMULTI:
1082 case SIOCDELMULTI:
1083 /* Update our multicast list. */
1084 error = (command == SIOCADDMULTI) ?
1085 ether_addmulti(ifr, &sc->sc_arpcom) :
1086 ether_delmulti(ifr, &sc->sc_arpcom);
1087
1088 if (error == ENETRESET) {
1089 /*
1090 * Multicast list has changed; set the hardware filter
1091 * accordingly.
1092 */
1093 ae_stop(sc); /* XXX for ds_setmcaf? */
1094 ae_init(sc);
1095 error = 0;
1096 }
1097 break;
1098
1099 default:
1100 error = EINVAL;
1101 }
1102
1103 splx(s);
1104 return (error);
1105 }
1106 /*
1107 * Retreive packet from shared memory and send to the next level up via
1108 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
1109 */
1110 void
1111 ae_get_packet(sc, buf, len)
1112 struct ae_softc *sc;
1113 caddr_t buf;
1114 u_short len;
1115 {
1116 struct ether_header *eh;
1117 struct mbuf *m, *ae_ring_to_mbuf();
1118
1119 /* Allocate a header mbuf. */
1120 MGETHDR(m, M_DONTWAIT, MT_DATA);
1121 if (m == 0)
1122 return;
1123 m->m_pkthdr.rcvif = &sc->sc_arpcom.ac_if;
1124 m->m_pkthdr.len = len;
1125 m->m_len = 0;
1126
1127 /* The following silliness is to make NFS happy. */
1128 #define EROUND ((sizeof(struct ether_header) + 3) & ~3)
1129 #define EOFF (EROUND - sizeof(struct ether_header))
1130
1131 /*
1132 * The following assumes there is room for the ether header in the
1133 * header mbuf.
1134 */
1135 m->m_data += EOFF;
1136 eh = mtod(m, struct ether_header *);
1137
1138 word_copy(buf, mtod(m, caddr_t), sizeof(struct ether_header));
1139 buf += sizeof(struct ether_header);
1140 m->m_len += sizeof(struct ether_header);
1141 len -= sizeof(struct ether_header);
1142
1143 /* Pull packet off interface. */
1144 if (ae_ring_to_mbuf(sc, buf, m, len) == 0) {
1145 m_freem(m);
1146 return;
1147 }
1148 #if NBPFILTER > 0
1149 /*
1150 * Check if there's a BPF listener on this interface. If so, hand off
1151 * the raw packet to bpf.
1152 */
1153 if (sc->sc_arpcom.ac_if.if_bpf) {
1154 bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m);
1155
1156 /*
1157 * Note that the interface cannot be in promiscuous mode if
1158 * there are no BPF listeners. And if we are in promiscuous
1159 * mode, we have to check if this packet is really ours.
1160 */
1161 if ((sc->sc_arpcom.ac_if.if_flags & IFF_PROMISC) &&
1162 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
1163 bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
1164 sizeof(eh->ether_dhost)) != 0) {
1165 m_freem(m);
1166 return;
1167 }
1168 }
1169 #endif
1170
1171 /* Fix up data start offset in mbuf to point past ether header. */
1172 m_adj(m, sizeof(struct ether_header));
1173 ether_input(&sc->sc_arpcom.ac_if, eh, m);
1174 }
1175 /*
1176 * Supporting routines.
1177 */
1178
1179 /*
1180 * Given a source and destination address, copy 'amount' of a packet from the
1181 * ring buffer into a linear destination buffer. Takes into account ring-wrap.
1182 */
1183 static inline caddr_t
1184 ae_ring_copy(sc, src, dst, amount)
1185 struct ae_softc *sc;
1186 caddr_t src, dst;
1187 u_short amount;
1188 {
1189 u_short tmp_amount;
1190
1191 /* Does copy wrap to lower addr in ring buffer? */
1192 if (src + amount > sc->mem_end) {
1193 tmp_amount = sc->mem_end - src;
1194
1195 /* Copy amount up to end of NIC memory. */
1196 byte_copy(src, dst, tmp_amount);
1197
1198 amount -= tmp_amount;
1199 src = sc->mem_ring;
1200 dst += tmp_amount;
1201 }
1202 byte_copy(src, dst, amount);
1203
1204 return (src + amount);
1205 }
1206 /*
1207 * Copy data from receive buffer to end of mbuf chain allocate additional mbufs
1208 * as needed. Return pointer to last mbuf in chain.
1209 * sc = ae info (softc)
1210 * src = pointer in ae ring buffer
1211 * dst = pointer to last mbuf in mbuf chain to copy to
1212 * amount = amount of data to copy
1213 */
1214 struct mbuf *
1215 ae_ring_to_mbuf(sc, src, dst, total_len)
1216 struct ae_softc *sc;
1217 caddr_t src;
1218 struct mbuf *dst;
1219 u_short total_len;
1220 {
1221 register struct mbuf *m = dst;
1222
1223 while (total_len) {
1224 register u_short amount = min(total_len, M_TRAILINGSPACE(m));
1225
1226 if (amount == 0) {
1227 /*
1228 * No more data in this mbuf; alloc another.
1229 *
1230 * If there is enough data for an mbuf cluster, attempt
1231 * to allocate one of those, otherwise, a regular mbuf
1232 * will do.
1233 * Note that a regular mbuf is always required, even if
1234 * we get a cluster - getting a cluster does not
1235 * allocate any mbufs, and one is needed to assign the
1236 * cluster to. The mbuf that has a cluster extension
1237 * can not be used to contain data - only the cluster
1238 * can contain data.
1239 */
1240 dst = m;
1241 MGET(m, M_DONTWAIT, MT_DATA);
1242 if (m == 0)
1243 return (0);
1244
1245 if (total_len >= MINCLSIZE)
1246 MCLGET(m, M_DONTWAIT);
1247
1248 m->m_len = 0;
1249 dst->m_next = m;
1250 amount = min(total_len, M_TRAILINGSPACE(m));
1251 }
1252 src = ae_ring_copy(sc, src, mtod(m, caddr_t) + m->m_len,
1253 amount);
1254
1255 m->m_len += amount;
1256 total_len -= amount;
1257 }
1258 return (m);
1259 }
1260 /*
1261 * Compute the multicast address filter from the list of multicast addresses we
1262 * need to listen to.
1263 */
1264 void
1265 ae_getmcaf(ac, af)
1266 struct arpcom *ac;
1267 u_char *af;
1268 {
1269 struct ifnet *ifp = &ac->ac_if;
1270 struct ether_multi *enm;
1271 register u_char *cp, c;
1272 register u_long crc;
1273 register int i, len;
1274 struct ether_multistep step;
1275
1276 /*
1277 * Set up multicast address filter by passing all multicast addresses
1278 * through a crc generator, and then using the high order 6 bits as an
1279 * index into the 64 bit logical address filter. The high order bit
1280 * selects the word, while the rest of the bits select the bit within
1281 * the word.
1282 */
1283
1284 if (ifp->if_flags & IFF_PROMISC) {
1285 ifp->if_flags |= IFF_ALLMULTI;
1286 for (i = 0; i < 8; i++)
1287 af[i] = 0xff;
1288 return;
1289 }
1290 for (i = 0; i < 8; i++)
1291 af[i] = 0;
1292 ETHER_FIRST_MULTI(step, ac, enm);
1293 while (enm != NULL) {
1294 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
1295 sizeof(enm->enm_addrlo)) != 0) {
1296 /*
1297 * We must listen to a range of multicast addresses.
1298 * For now, just accept all multicasts, rather than
1299 * trying to set only those filter bits needed to match
1300 * the range. (At this time, the only use of address
1301 * ranges is for IP multicast routing, for which the
1302 * range is big enough to require all bits set.)
1303 */
1304 ifp->if_flags |= IFF_ALLMULTI;
1305 for (i = 0; i < 8; i++)
1306 af[i] = 0xff;
1307 return;
1308 }
1309 cp = enm->enm_addrlo;
1310 crc = 0xffffffff;
1311 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1312 c = *cp++;
1313 for (i = 8; --i >= 0;) {
1314 if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
1315 crc <<= 1;
1316 crc ^= 0x04c11db6 | 1;
1317 } else
1318 crc <<= 1;
1319 c >>= 1;
1320 }
1321 }
1322 /* Just want the 6 most significant bits. */
1323 crc >>= 26;
1324
1325 /* Turn on the corresponding bit in the filter. */
1326 af[crc >> 3] |= 1 << (crc & 0x7);
1327
1328 ETHER_NEXT_MULTI(step, enm);
1329 }
1330 ifp->if_flags &= ~IFF_ALLMULTI;
1331 }
1332 /*
1333 * Copy packet from mbuf to the board memory
1334 *
1335 * Currently uses an extra buffer/extra memory copy,
1336 * unless the whole packet fits in one mbuf.
1337 *
1338 */
1339 u_short
1340 ae_put(sc, m, buf)
1341 struct ae_softc *sc;
1342 struct mbuf *m;
1343 caddr_t buf;
1344 {
1345 u_char *data, savebyte[2];
1346 int len, wantbyte;
1347 u_short totlen = 0;
1348
1349 wantbyte = 0;
1350
1351 for (; m != 0; m = m->m_next) {
1352 data = mtod(m, u_char *);
1353 len = m->m_len;
1354 totlen += len;
1355 if (len > 0) {
1356 /* Finish the last word. */
1357 if (wantbyte) {
1358 savebyte[1] = *data;
1359 word_copy(savebyte, buf, 2);
1360 buf += 2;
1361 data++;
1362 len--;
1363 wantbyte = 0;
1364 }
1365 /* Output contiguous words. */
1366 if (len > 1) {
1367 word_copy(data, buf, len);
1368 buf += len & ~1;
1369 data += len & ~1;
1370 len &= 1;
1371 }
1372 /* Save last byte, if necessary. */
1373 if (len == 1) {
1374 savebyte[0] = *data;
1375 wantbyte = 1;
1376 }
1377 }
1378 }
1379
1380 if (wantbyte) {
1381 savebyte[1] = 0;
1382 word_copy(savebyte, buf, 2);
1383 }
1384 return (totlen);
1385 }
1386