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