summaryrefslogtreecommitdiff
path: root/sys/arch/amiga/dev/if_ae.c
blob: c0d43f8e8a435ed521f04b00cefae62f6590864f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
/*	$NetBSD: if_ae.c,v 1.4 1995/12/24 02:29:52 mycroft Exp $	*/

/*
 * Copyright (c) 1995 Bernd Ernesti and Klaus Burkert. All rights reserved.
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California. All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Ralph Campbell and Rick Macklem.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Bernd Ernesti, by Klaus
 *	Burkert, by Michael van Elst, and by the University of California,
 *	Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)if_le.c	8.1 (Berkeley) 6/10/93
 *
 *	This is based on the original LANCE files, as the PCnet-ISA used on
 *	the Ariadne is a LANCE-descendant optimized for the PC-ISA bus.
 *	This causes some modifications, all data that is to go into registers
 *	or to structures (buffer-descriptors, init-block) has to be
 *	byte-swapped. In addition ALL write accesses to the board have to be
 *	WORD or LONG, BYTE-access is prohibited!!
 */

#include "ae.h"
#if NAE > 0

#include "bpfilter.h"

/*
 * AMD 79C960 PCnet-ISA
 *
 * This driver will generate and accept tailer encapsulated packets even
 * though it buys us nothing.  The motivation was to avoid incompatibilities
 * with VAXen, SUNs, and others that handle and benefit from them.
 * This reasoning is dubious.
 */
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/buf.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/device.h>

#include <net/if.h>
#include <net/netisr.h>
#include <net/route.h>

#ifdef INET
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#endif

#ifdef NS
#include <netns/ns.h>
#include <netns/ns_if.h>
#endif

#if defined(CCITT) && defined(LLC)
#include <sys/socketvar.h>  
#include <netccitt/x25.h>
extern llc_ctlinput(), cons_rtrequest();
#endif  

#if NBPFILTER > 0
#include <net/bpf.h>
#include <net/bpfdesc.h>
#endif  

#include <machine/cpu.h>
#include <machine/mtpr.h>
#include <amiga/amiga/device.h>
#include <amiga/amiga/isr.h>
#include <amiga/dev/zbusvar.h>
#include <amiga/dev/if_aereg.h>

/*
 * Ethernet software status per interface.
 *
 * Each interface is referenced by a network interface structure,
 * ae_if, which the routing code uses to locate the interface.
 * This structure contains the output queue for the interface, its address, ...
 */
struct	ae_softc {
	struct	device sc_dev;
	struct	isr sc_isr;
	struct	arpcom sc_arpcom;	/* common Ethernet structures */
	void	*sc_base;	/* base address of board */
	struct	aereg1 *sc_r1;	/* LANCE registers */
	struct	aereg2 *sc_r2;	/* dual-port RAM */
	int	sc_rmd;		/* predicted next rmd to process */
	int	sc_tmd;		/* next tmd to use */
	int	sc_no_td;	/* number of tmds in use */
} ae_softc[NAE];

/* offsets for:	   ID,   REGS,    MEM */
int	aestd[] = { 0, 0x0370, 0x8000 };
static u_int16_t	revision;

int	aematch __P((struct device *, void *, void *));
void	aeattach __P((struct device *, struct device *, void *));
void	aewatchdog __P((int));
void	aestop __P((struct ae_softc *));
void	aememinit __P((struct ae_softc *));
void	aereset __P((struct ae_softc *));
void	aeinit __P((struct ae_softc *));
void	aestart __P((struct ifnet *));
int	aeintr __P((struct ae_softc *));
void	aetint __P((struct ae_softc *));
void	aerint __P((struct ae_softc *));
void	aeread __P((struct ae_softc *, u_char *, int));
static	void wcopyfrom __P((char *, char *, int));
static	void wcopyto __P((char *, char *, int));
static	void wzero __P((char *, int));
int	aeput __P((char *, struct mbuf *));
struct	mbuf *aeget __P((struct ae_softc *,u_char *, int));
int	aeioctl __P((struct ifnet *, u_long, caddr_t));
void	aesetladrf __P((struct arpcom *, u_int16_t *));

struct cfdriver aecd = {
	NULL, "ae", aematch, aeattach, DV_IFNET, sizeof(struct ae_softc)
};

int
aematch(parent, match, aux)
	struct device *parent;
	void *match, *aux;
{
	struct zbus_args *zap;

	zap = (struct zbus_args *)aux;

	/* Ariadne ethernet card */
	if (zap->manid == 2167 && zap->prodid == 201)
		return (1);

	return (0);
}

/*
 * Interface exists: make available by filling in network interface
 * record.  System will initialize the interface when it is ready
 * to accept packets.
 */
void
aeattach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	register struct aereg2 *aer2;
	struct zbus_args *zap;
	struct ae_softc *sc = (void *)self;
	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
	unsigned long ser;
	int s = splhigh ();

	zap =(struct zbus_args *)aux;

	/*
	 * Make config msgs look nicer.
	 */
	printf("\n");

	sc->sc_base = zap->va;
	sc->sc_r1 = (struct aereg1 *)(aestd[1] + (int)zap->va);
	aer2 = sc->sc_r2 = (struct aereg2 *)(aestd[2] + (int)zap->va);

	/*
	 * Serial number for board is used as host ID.
	 */
	ser = (unsigned long) zap->serno;

	/*
	 * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
	 */

	/*
	 * currently borrowed from C= 
	 * the next four lines will soon have to be altered 
	 */

	sc->sc_arpcom.ac_enaddr[0] = 0x00;
	sc->sc_arpcom.ac_enaddr[1] = 0x80;
	sc->sc_arpcom.ac_enaddr[2] = 0x10;

	sc->sc_arpcom.ac_enaddr[3] = ((ser >> 16) & 0x0f) | 0xf0; /* to diff from A2065 */
	sc->sc_arpcom.ac_enaddr[4] = (ser >>  8 ) & 0xff;
	sc->sc_arpcom.ac_enaddr[5] = (ser       ) & 0xff;

	printf("%s: hardware address %s 32K", sc->sc_dev.dv_xname,
		ether_sprintf(sc->sc_arpcom.ac_enaddr));

	aestop(sc);
	delay(100);

	/* get the chip version of the lance chip */
	sc->sc_r1->aer1_rap = 0x5900;
	revision = ((sc->sc_r1->aer1_rdp >> 4) -2);
	printf("  chip-revision: B%x\n", revision);

	splx (s);

	ifp->if_unit = sc->sc_dev.dv_unit;
	ifp->if_name = aecd.cd_name;
	ifp->if_ioctl = aeioctl;
	ifp->if_watchdog = aewatchdog;
	ifp->if_output = ether_output;
	ifp->if_start = aestart;
	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;

	if_attach(ifp);
	ether_ifattach(ifp);

#if NBPFILTER > 0
	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
#endif

	sc->sc_isr.isr_intr = aeintr;
	sc->sc_isr.isr_arg = sc;
	sc->sc_isr.isr_ipl = 2;
	add_isr (&sc->sc_isr);
	return;
}

void
aewatchdog(unit)
	short unit;
{
	struct ae_softc *sc = aecd.cd_devs[unit];

	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
	++sc->sc_arpcom.ac_if.if_oerrors;

	aereset(sc);
}

void
aestop(sc)
	struct ae_softc *sc;
{
	sc->sc_r1->aer1_rap =  AE_CSR0;
	sc->sc_r1->aer1_rdp =  AE_STOP;
}

	
/*    
 * Set up the initialization block and the descriptor rings.
 */     
void
aememinit(sc)
	register struct ae_softc *sc;
{        
	register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
	/*
	 * This structure is referenced from the CARD's/PCnet-ISA's point
	 * of view, thus the 0x8000 address which is the buffer RAM area
	 * of the Ariadne card. This pointer is manipulated
	 * with the PCnet-ISA's view of memory and NOT the Amiga's. FYI.
	 */
	register struct aereg2 *aemem = (struct aereg2 *) 0x8000;
	register struct aereg2 *aer2 = sc->sc_r2;
	register int i;


#if NBPFILTER > 0
	if (ifp->if_flags & IFF_PROMISC)
		/* set the promiscuous bit */
		aer2->aer2_mode = AE_MODE | AE_PROM;
	else    
#endif
		aer2->aer2_mode = AE_MODE;
	/* you know: no BYTE access.... */
	aer2->aer2_padr[0] =
		(sc->sc_arpcom.ac_enaddr[0] << 8) | sc->sc_arpcom.ac_enaddr[1];
	aer2->aer2_padr[1] =
		(sc->sc_arpcom.ac_enaddr[2] << 8) | sc->sc_arpcom.ac_enaddr[3];
	aer2->aer2_padr[2] =
		(sc->sc_arpcom.ac_enaddr[4] << 8) | sc->sc_arpcom.ac_enaddr[5];
	aesetladrf(&sc->sc_arpcom, aer2->aer2_ladrf);

	sc->sc_no_td = sc->sc_tmd = sc->sc_rmd = 0;

	aer2->aer2_rlen = SWAP(AE_RLEN);
	aer2->aer2_rdra = SWAP((int)aemem->aer2_rmd); 
	aer2->aer2_tlen = SWAP(AE_TLEN);
	aer2->aer2_tdra = SWAP((int)aemem->aer2_tmd);

	for (i = 0; i < AERBUF; i++) {  
		aer2->aer2_rmd[i].rmd0 = SWAP((int)aemem->aer2_rbuf[i]);
		aer2->aer2_rmd[i].rmd1 = AE_OWN; 
		aer2->aer2_rmd[i].rmd2 = SWAP(-ETHER_MAX_LEN);
		aer2->aer2_rmd[i].rmd3 = 0;
	}

	for (i = 0; i < AETBUF; i++) {
		aer2->aer2_tmd[i].tmd0 = SWAP((int)aemem->aer2_tbuf[i]);
		aer2->aer2_tmd[i].tmd1 = 0;
		aer2->aer2_tmd[i].tmd2 = 0;
		aer2->aer2_tmd[i].tmd3 = 0;
	}
}

void
aereset(sc)
	struct ae_softc *sc;
{
	int s;

	s = splnet();
	aeinit(sc);
	splx(s);
}

/*
 * Initialization of interface; set up initialization block
 * and transmit/receive descriptor rings.
 */     
void
aeinit(sc)
	struct ae_softc *sc;
{
	register struct aereg1 *aer1 = sc->sc_r1;
	register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
	register struct aereg2 *aemem = (struct aereg2 *) 0x8000;

	register int timo = 0;
	volatile int dummy;

	aestop(sc);
	delay(100);

	dummy = aer1->aer1_reset;    /* Reset PCNet-ISA */

	aememinit(sc);

	/* Give LANCE the physical address of its init block. */
	aer1->aer1_rap =  AE_CSR1;
	aer1->aer1_rdp =  SWAP((int)&aemem->aer2_mode);
	aer1->aer1_rap =  AE_CSR2;
	aer1->aer1_rdp =  0;

/*
 *	re-program LEDs to match meaning used on the Ariadne board
 */
	aer1->aer1_rap = 0x0500;
	aer1->aer1_idp = 0x9000;
	aer1->aer1_rap = 0x0600;
	aer1->aer1_idp = 0x8100;
	aer1->aer1_rap = 0x0700;
	aer1->aer1_idp = 0x8400;

/*
 * you can `ifconfig (link0|-link0) ae0' to get the following
 * behaviour:    
 *	-link0	enable autoselect between 10Base-T (UTP) and 10Base2 (BNC)
 *		if an active 10Base-T line is connected then 10Base-T
 *		is used otherwise 10Base2.
 *		this is the default behaviour, so there is no need to set
 *		-link0 when you want autoselect.
 *	link0 -link1	disable autoselect. enable BNC.
 *	link0 link1	disable autoselect. enable UTP.
 */
	if (!(ifp->if_flags & IFF_LINK0)) {
		/* enable autoselect */
		aer1->aer1_rap = 0x0200;
		aer1->aer1_idp = 0x0200;
	} else {
		/* disable autoselect */
		aer1->aer1_rap = 0x0200;
		aer1->aer1_idp = 0x0000;
		if (!(ifp->if_flags & IFF_LINK1)) {
			/* enable BNC */
			sc->sc_r2->aer2_mode = 0x0000;
		} else {
			/* enable UTP */
			sc->sc_r2->aer2_mode = 0x8000;
		}
	}

	/* Try to initialize the LANCE. */
	delay(100);
	aer1->aer1_rap =  AE_CSR0;
	aer1->aer1_rdp =  AE_INIT;

	/* Wait for initialization to finish. */
	do {
		if (++timo == 10000) {
			printf("%s: card failed to initialize\n", sc->sc_dev.dv_xname);
			break;
		}
	} while ((aer1->aer1_rdp & AE_IDON) == 0);

	/* Start the LANCE. */
	aer1->aer1_rap =  AE_CSR0;
	aer1->aer1_rdp =  AE_STRT | AE_INEA | AE_IDON;
	ifp->if_flags |= IFF_RUNNING;
	ifp->if_flags &= ~IFF_OACTIVE;
	ifp->if_timer = 0;
	aestart(ifp);
}

#define AENEXTTMP \
	if (++bix == AETBUF) bix = 0, tmd = sc->sc_r2->aer2_tmd; else ++tmd

/*
 * Start output on interface.  Get another datagram to send
 * off of the interface queue, and copy it to the interface
 * before starting the output.
 */
void
aestart(ifp)
	struct ifnet *ifp;
{
	register struct ae_softc *sc = aecd.cd_devs[ifp->if_unit];
	register int bix;
	register struct aetmd *tmd;
	register struct mbuf *m;
	int len;

	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
		return;

	bix = sc->sc_tmd;
	tmd = &sc->sc_r2->aer2_tmd[bix];

	for (;;) {
		if (sc->sc_no_td >= AETBUF) {
			ifp->if_flags |= IFF_OACTIVE;
			break;
		}

		IF_DEQUEUE(&ifp->if_snd, m);
		if (m == 0)
			break;

		++sc->sc_no_td;

#if NBPFILTER > 0
		if (ifp->if_bpf)
			bpf_mtap(ifp->if_bpf, m);
#endif

		sc->sc_r1->aer1_rdp =  AE_TINT | AE_INEA;
		len = aeput(sc->sc_r2->aer2_tbuf[bix], m);

#ifdef AEDEBUG
		if (len > ETHER_MAX_LEN)
			printf("packet length %d\n", len);
#endif

		ifp->if_timer = 5;

		tmd->tmd1 = AE_OWN | AE_STP | AE_ENP;
		tmd->tmd2 = SWAP(-len);
		tmd->tmd3 = 0;

		sc->sc_r1->aer1_rdp = AE_INEA | AE_TDMD;

		AENEXTTMP;
	}

	sc->sc_tmd = bix;
}

int
aeintr(sc)
	register struct ae_softc *sc;
{
	register struct aereg1 *aer1;
	register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
	register u_int16_t stat;

	/* if not even initialized, don't do anything further.. */
	if (sc->sc_base == 0)
		return (0);

	aer1 = sc->sc_r1;
	stat =  aer1->aer1_rdp;

	if ((stat & AE_INTR) == 0)
		return (0);

	aer1->aer1_rdp = (stat & (AE_INEA | AE_BABL | AE_MISS | AE_MERR |
				AE_RINT | AE_TINT | AE_IDON));
	if (stat & AE_SERR) {
			if (stat & AE_MERR) {
				printf("%s: memory error\n", sc->sc_dev.dv_xname);
				aereset(sc);
				return (1);
			}
			if (stat & AE_BABL) {
				printf("%s: babble\n", sc->sc_dev.dv_xname);
				ifp->if_oerrors++;
			}
#if 0
			if (stat & AE_CERR) {
				printf("%s: collision error\n", sc->sc_dev.dv_xname);
				ifp->if_collisions++;
			}
#endif
			if (stat & AE_MISS) {
				printf("%s: missed packet\n", sc->sc_dev.dv_xname);
				ifp->if_ierrors++;
			}
			aer1->aer1_rdp =  AE_BABL | AE_CERR | AE_MISS | AE_INEA;
		}
		if ((stat & AE_RXON) == 0) {
			printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
			ifp->if_ierrors++;
			aereset(sc);
			return (1);
		}
		if ((stat & AE_TXON) == 0) {
			printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
			ifp->if_oerrors++;
			aereset(sc);
			return (1);
		}
		if (stat & AE_RINT) {
			/* Reset watchdog timer. */
			ifp->if_timer = 0;
			aerint(sc);
		}
		if (stat & AE_TINT) {
			/* Reset watchdog timer. */
			ifp->if_timer = 0;
			aetint(sc);
		}

	return (1);
}

/*
 * Ethernet interface transmitter interrupt.
 * Start another output if more data to send.
 */
void
aetint(sc)
	struct ae_softc *sc;
{
	register int bix = (sc->sc_tmd - sc->sc_no_td + AETBUF) % AETBUF;
	struct aetmd *tmd = &sc->sc_r2->aer2_tmd[bix];
	struct ifnet *ifp = &sc->sc_arpcom.ac_if;

	if (tmd->tmd1 & AE_OWN) {
#ifdef AEDEBUG
		printf("%s: extra tint\n", sc->sc_dev.dv_xname);
#endif
		return;
	}
	ifp->if_flags &= ~IFF_OACTIVE;

	do {
		if (sc->sc_no_td <= 0)
			break;
		ifp->if_opackets++;
		--sc->sc_no_td;

		if (tmd->tmd1 & AE_ERR) {
			if (tmd->tmd3 & AE_TBUFF)
				printf("%s: transmit buffer error\n", sc->sc_dev.dv_xname);
			if (tmd->tmd3 & AE_UFLO)
				printf("%s: underflow\n", sc->sc_dev.dv_xname);
			if (tmd->tmd3 & (AE_TBUFF | AE_UFLO)) {
				aereset(sc);
				return;
			}
			if (tmd->tmd3 & AE_LCAR)
				printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
			if (tmd->tmd3 & AE_LCOL) {
				printf("%s: late collision\n", sc->sc_dev.dv_xname);
				ifp->if_collisions++;
			}
			if (tmd->tmd3 & AE_RTRY) {
				printf("%s: excessive collisions, tdr %d\n",
					sc->sc_dev.dv_xname, tmd->tmd3 & AE_TDR_MASK);
				ifp->if_collisions += 16;
			}
		} else if (tmd->tmd1 & AE_ONE) {
			ifp->if_collisions++;
		}
		else if (tmd->tmd1 & AE_MORE) {
			/* Real number is unknown. */
			ifp->if_collisions += 2;
		}
		AENEXTTMP;
	} while ((tmd->tmd1 & AE_OWN) == 0);

	aestart(ifp);
	if (sc->sc_no_td == 0)
		ifp->if_timer = 0;
}

#define	AENEXTRMP \
	if (++bix == AERBUF) bix = 0, rmd = sc->sc_r2->aer2_rmd; else ++rmd

/*
 * Ethernet interface receiver interrupt.
 * If input error just drop packet.
 * Decapsulate packet based on type and pass to type specific
 * higher-level input routine.
 */
void
aerint(sc)
	struct ae_softc *sc;
{
	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
	register int bix = sc->sc_rmd;
	register struct aermd *rmd = &sc->sc_r2->aer2_rmd[bix];

	/*
	 * Out of sync with hardware, should never happen?
	 */
	if (rmd->rmd1 & AE_OWN) {
#ifdef AEDEBUG
		printf("%s: extra rint\n", sc->sc_dev.dv_xname);
#endif
		return;
	}

	/*
	 * Process all buffers with valid data
	 */
	do {
		sc->sc_r1->aer1_rdp =  AE_RINT | AE_INEA;
		if (rmd->rmd1 & (AE_FRAM | AE_OFLO | AE_CRC | AE_RBUFF)) {
			ifp->if_ierrors++;
			if ((rmd->rmd1 & (AE_FRAM | AE_OFLO | AE_ENP)) == (AE_FRAM | AE_ENP))
				printf("%s: framing error\n", sc->sc_dev.dv_xname);
			if ((rmd->rmd1 & (AE_OFLO | AE_ENP)) == AE_OFLO)
				printf("%s: overflow\n", sc->sc_dev.dv_xname);
			if ((rmd->rmd1 & (AE_CRC | AE_OFLO | AE_ENP)) == (AE_CRC | AE_ENP))
				printf("%s: crc mismatch\n", sc->sc_dev.dv_xname);
			if (rmd->rmd1 & AE_RBUFF)
				printf("%s: receive buffer error\n", sc->sc_dev.dv_xname);
		} else if ((rmd->rmd1 & (AE_STP | AE_ENP)) != (AE_STP | AE_ENP)) {
			do {
				rmd->rmd3 = 0;
				rmd->rmd1 = AE_OWN;
				AENEXTRMP;
			} while ((rmd->rmd1 & (AE_OWN | AE_ERR | AE_STP | AE_ENP)) == 0);

			sc->sc_rmd = bix;
			printf("%s: chained buffer\n", sc->sc_dev.dv_xname);
			if ((rmd->rmd1 & (AE_OWN | AE_ERR | AE_STP | AE_ENP)) != AE_ENP) {
				aereset(sc);
				return;
			}
		} else
			aeread(sc, sc->sc_r2->aer2_rbuf[bix], SWAP(rmd->rmd3) - 4);

		rmd->rmd1 = AE_OWN;
		rmd->rmd2 = SWAP(-ETHER_MAX_LEN);
		rmd->rmd3 = 0;

		AENEXTRMP;
	} while ((rmd->rmd1 & AE_OWN) == 0);

	sc->sc_rmd = bix;
}

void
aeread(sc, buf, len)
	register struct ae_softc *sc;
	u_char *buf;
	int len;
{
	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    	struct mbuf *m;
	struct ether_header *eh;

	if (len <= sizeof(struct ether_header) || len > ETHER_MAX_LEN) {
		printf("%s: invalid packet size %d; dropping\n",
			sc->sc_dev.dv_xname, len);
		ifp->if_ierrors++;
		return;
	}

	/* Pull packet off interface. */
	m = aeget(sc, buf, len);
	if (m == 0) {
		ifp->if_ierrors++;
		return;
	}

	ifp->if_ipackets++;

	/* We assume that the header fit entirely in one mbuf. */
	eh = mtod(m, struct ether_header *);

#if NBPFILTER > 0
	/*
	 * Check if there's a bpf filter listening on this interface.
	 * If so, hand off the raw packet to bpf, which must deal with
	 * trailers in its own way.
	 */
	if (ifp->if_bpf) {
		bpf_mtap(ifp->if_bpf, m);

		/*
		 * Note that the interface cannot be in promiscuous mode if
		 * there are no bpf listeners.  And if we are in promiscuous
		 * mode, we have to check if this packet is really ours.
		 */
		if ((ifp->if_flags & IFF_PROMISC) &&
		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
			sizeof(eh->ether_dhost)) != 0) {
			    m_freem(m);
			    return;
		    }
	}
#endif

	/* We assume that the header fit entirely in one mbuf. */
	m_adj(m, sizeof(struct ether_header));
	ether_input(ifp, eh, m);
}

/*
 *	Here come the two replacements for bcopy() and bzero() as
 *	WORD-access for writing to the board is absolutely required!
 *	They could use some tuning as this is time-critical (copying
 *	packet-data) and should be processed as fast as possible.
 *
 *	Thanks to Michael van Elst for pointing me to the problems
 *	that kept the 1.3 code from running under NetBSD current
 *	although it did mystically under 1.0 ...
 */

#define isodd(p) (((size_t)(p)) &  1)
#define align(p) ((ushort *)(((size_t)(p)) & ~1))

/*
 *	the first function copies WORD-aligned from the source to
 *	an arbitrary destination. It assumes that the CPU can handle
 *	mis-aligned writes to the destination itself.
 */
static void
wcopyfrom(a1, a2, length) /* bcopy() word-wise */
	char *a1, *a2;
	int length;
{
	ushort i, *b1, *b2;

	if (length > 0 && isodd(a1)) {
		b1 = align(a1);
		*a2 = *b1 & 0x00ff;	/* copy first byte with word access */
		++a2;
		++b1;
		--length;
	} else
		b1 = (ushort *)a1;
	b2 = (ushort *)a2;

	i = length / 2;

	while(i--)	/* copy all words */
		*b2++ = *b1++;

	if (length & 0x0001)	/* copy trailing byte */
		a2[length-1] = *b1 >> 8;
}

/*
 *	the second function copies WORD-aligned from an arbitrary
 *	source to the destination. It assumes that the CPU can handle
 *	mis-aligned reads from the source itself.
 */
static void
wcopyto(a1, a2, length) /* bcopy() word-wise */
	char *a1, *a2;
	int length;
{
	ushort i, *b1, *b2;

	if (length > 0 && isodd(a2)) {
		b2 = align(a2);
		i = (*b2 & 0xff00) | (*a1 & 0x00ff);	/* copy first byte with word access */
		*b2 = i;
		++a1;
		++b2;
		--length;
	} else
		b2 = (ushort *)a2;
	b1 = (ushort *)a1;

	i = length / 2;

	while(i--)			/* copy all words */
		*b2++ = *b1++;

	if (length & 0x0001) {
		i = (*b2 & 0x00ff) | (*b1 & 0xff00);	/* copy trailing byte */
		*b2 = i;
	}
}

static void
wzero(a1, length) /* bzero() word-wise */
	char *a1;
	int length;
{
	ushort i, *b1;

	/*
	 *  Is the destination word-aligned?
	 *  If not, handle the leading byte...
	 */

	if((length > 0) && ((size_t)a1 & 1)) {
		b1 = (ushort *)((size_t)a1 & ~1);
		*b1 &= 0xff00;
		--length;
		++a1;
	}

	/*
	 *  Perform the main zeroing word-wise...
	 */

	b1 = (ushort *)a1;
	i = length / 2;
	while(i--)
		*b1++ = 0;

	/*
	 *  Do we have to handle a trailing byte?
	 */

	if (length & 0x0001)
		*b1 &= 0x00ff;
}

/*
 * Routine to copy from mbuf chain to transmit
 * buffer in board local memory. 
 */
int
aeput(buffer, m)
	register char *buffer; 
	register struct mbuf *m;
{
	register struct mbuf *n;
	register int len = 0, tlen = 0;  
 
	for (; m; m = n) {
		len = m->m_len;
		if (len == 0) {
			MFREE(m, n);
			continue;
		}
		wcopyto(mtod(m, char *), buffer, len);
		buffer += len;
		tlen += len;
		MFREE(m, n);
	}
 
	if (tlen < ETHER_MIN_LEN) {
		wzero(buffer, ETHER_MIN_LEN - tlen);
		tlen = ETHER_MIN_LEN;
	}

	return(tlen);
}

/*
 * Routine to copy from board local memory into mbufs.
 */
struct mbuf *
aeget(sc, buffer, totlen)
	struct ae_softc *sc;
	u_char *buffer;
	int totlen;
{
	register struct mbuf *m;
	struct mbuf *top, **mp;
	int len;

	MGETHDR(m, M_DONTWAIT, MT_DATA);
	if (m == 0)
		return (0);
	m->m_pkthdr.rcvif = &sc->sc_arpcom.ac_if;
	m->m_pkthdr.len = totlen;
	len = MHLEN;
	top = 0;
	mp = &top;

	while (totlen > 0) {
		if (top) {
			MGET(m, M_DONTWAIT, MT_DATA);
			if (m == 0) {
				m_freem(top);
				return (0);
			}
			len = MLEN;
		}

		if (totlen >= MINCLSIZE) {
			MCLGET(m, M_DONTWAIT);
			if (m->m_flags & M_EXT)
				len = MCLBYTES;
		}
		m->m_len = len = min(totlen, len);
		wcopyfrom((caddr_t)buffer, mtod(m, caddr_t), len);
		buffer += len;
		totlen -= len;
		*mp = m;
		mp = &m->m_next;
	}

	return (top);
}

/*
 * Process an ioctl request.
 */
int
aeioctl(ifp, cmd, data)
	register struct ifnet *ifp;
	u_long cmd;
	caddr_t data;
{
	struct ae_softc *sc = aecd.cd_devs[ifp->if_unit];
	struct ifaddr *ifa = (struct ifaddr *)data;
	struct ifreq *ifr = (struct ifreq *)data;
	int s, error = 0;

	s = splnet();

	switch (cmd) {

	case SIOCSIFADDR:
		ifp->if_flags |= IFF_UP;

		switch (ifa->ifa_addr->sa_family) {
#ifdef INET
		case AF_INET:
			aeinit(sc);	
			arp_ifinit(&sc->sc_arpcom, ifa);
			break;
#endif
#ifdef NS
		case AF_NS:
		    {
			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;

			if (ns_nullhost(*ina))
				ina->x_host =
				    *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
			else
				wcopyto(ina->x_host.c_host,
				    sc->sc_arpcom.ac_enaddr,
				    sizeof(sc->sc_arpcom.ac_enaddr));
			aeinit(sc); /* does ae_setaddr() */
			break;
		    }
#endif
		default:
			aeinit(sc);
			break;
		}
		break;

#if defined(CCITT) && defined(LLC)
	case SIOCSIFCONF_X25:
		ifp->if_flags |= IFF_UP;
		ifa->ifa_rtrequest = (void (*)())cons_rtrequest; /* XXX */
		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
		if (error == 0)
			aeinit(sc);
		break;
#endif /* CCITT && LLC */

	case SIOCSIFFLAGS:
		if ((ifp->if_flags & IFF_UP) == 0 &&
		    (ifp->if_flags & IFF_RUNNING) != 0) {
			/*
			 * If interface is marked down and it is running, then
			 * stop it.
			 */
			aestop(sc);
			ifp->if_flags &= ~IFF_RUNNING;
		} else if ((ifp->if_flags & IFF_UP) != 0 &&
			   (ifp->if_flags & IFF_RUNNING) == 0) {
			/*
			 * If interface is marked up and it is stopped, then
			 * start it.
			 */
			aeinit(sc);
		} else {
			/*
			 * Reset the interface to pick up changes in any other
			 * flags that affect hardware registers.
			 */
			aeinit(sc);
		}
		break;

	case SIOCADDMULTI:
	case SIOCDELMULTI:
		error = (cmd == SIOCADDMULTI) ?
		    ether_addmulti(ifr, &sc->sc_arpcom):
		    ether_delmulti(ifr, &sc->sc_arpcom);

		if (error == ENETRESET) {
			/*
			 * Multicast list has changed; set the hardware filter
			 * accordingly.
			 */
			aereset(sc);
			error = 0;
		}
		break;

	default:
		error = EINVAL;
		break;
	}
	splx(s);
	return (error);
}

/*
 * Set up the logical address filter.
 */
void 
aesetladrf(ac, af)
	struct arpcom *ac;
	u_int16_t *af;
{
	struct ifnet *ifp = &ac->ac_if; 
	struct ether_multi *enm;
	register u_char *cp, c;
	register u_int32_t crc;
	register int i, len;
	struct ether_multistep step;

	/*
	 * Set up multicast address filter by passing all multicast addresses
	 * through a crc generator, and then using the high order 6 bits as an
	 * index into the 64 bit logical address filter.  The high order bit
	 * selects the word, while the rest of the bits select the bit within
	 * the word.
	 */

	if (ifp->if_flags & IFF_PROMISC)
		goto allmulti;

	af[0] = af[1] = af[2] = af[3] = 0x0000;
	ETHER_FIRST_MULTI(step, ac, enm);
	while (enm != NULL) {
		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
		    sizeof(enm->enm_addrlo)) != 0) {
			/*
			 * We must listen to a range of multicast addresses.
			 * For now, just accept all multicasts, rather than
			 * trying to set only those filter bits needed to match
			 * the range.  (At this time, the only use of address
			 * ranges is for IP multicast routing, for which the
			 * range is big enough to require all bits set.)
			 */
			goto allmulti;
		}

		cp = enm->enm_addrlo;
		crc = 0xffffffff;
		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
			c = *cp++;
			for (i = 8; --i >= 0;) {
				if ((crc & 0x01) ^ (c & 0x01)) {
					crc >>= 1;
					crc ^= 0xedb88320;
				} else
					crc >>= 1;
				c >>= 1;
			}
		}
		/* Just want the 6 most significant bits. */
		crc >>= 26;

		/* Turn on the corresponding bit in the filter. */
		af[crc >> 4] |= 1 << SWAP(crc & 0x1f);

		ETHER_NEXT_MULTI(step, enm);
	}
	ifp->if_flags &= ~IFF_ALLMULTI;
	return;

allmulti:
	ifp->if_flags |= IFF_ALLMULTI;
	af[0] = af[1] = af[2] = af[3] = 0xffff;
}

#endif /* NAE > 0 */