summaryrefslogtreecommitdiff
path: root/sys/src/9/omap/ether9221.c
blob: 9f7a571aa2979918da481c9042230b92623040f3 (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
/*
 * SMSC 9221 Ethernet driver
 * specifically for the ISEE IGEPv2 board,
 * where it is assigned to Chip Select 5,
 * its registers are at 0x2c000000 (inherited from u-boot),
 * and irq is 34 from gpio pin 176, thus gpio module 6.
 *
 * it's slow due to the use of fifos instead of buffer rings.
 * the slow system dma just makes it worse.
 *
 * igepv2 u-boot uses pin 64 on gpio 3 as an output pin to reset the 9221.
 */
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "../port/error.h"
#include "../port/netif.h"

#include "etherif.h"

/* currently using kprocs is a lot slower than not (87 s. to boot vs 60) */
#undef USE_KPROCS

enum {
	Vid9221	= 0x9221,
	Slop	= 4,			/* beyond ETHERMAXTU */
};

typedef struct Regs Regs;
struct Regs {
	/* fifo ports */
	ulong	rxdata;
	uchar	_pad0[0x20 - 4];
	ulong	txdata;
	uchar	_pad1[0x40 - 0x24];
	ulong	rxsts;
	ulong	rxstspeek;
	ulong	txsts;
	ulong	txstspeek;

	/* control & status */
	ushort	rev;			/* chip revision */
	ushort	id;			/* chip id, 0x9221 */
	ulong	irqcfg;
	ulong	intsts;
	ulong	inten;
	ulong	_pad2;
	ulong	bytetest;
	ulong	fifoint;		/* fifo level interrupts */
	ulong	rxcfg;
	ulong	txcfg;
	ulong	hwcfg;
	ulong	rxdpctl;		/* rx data path control */
	ulong	rxfifoinf;
	ulong	txfifoinf;
	ulong	pmtctl;			/* power mgmt. control */
	ulong	gpiocfg;
	ulong	gptcfg;			/* timer */
	ulong	gptcnt;
	ulong	_pad3;
	ulong	wordswap;
	ulong	freerun; 		/* counters */
	ulong	rxdrop;

	/*
	 * mac registers are accessed indirectly via the mac csr registers.
	 * phy registers are doubly indirect, via the mac csr mii_acc &
	 * mii_data mac csr registers.
	 */
	ulong	maccsrcmd;		/* mac csr synchronizer */
	ulong	maccsrdata;
	ulong	afccfg;			/* automatic flow control cfg. */
	ulong	eepcmd;			/* eeprom */
	ulong	eepdata;
	/* 0xb8 */
};

enum {
	Nstatistics	= 128,
};

enum {
	/* txcmda bits */
	Intcompl	= 1<<31,
	Bufendalign	= 3<<24,	/* mask */
	Datastoff	= 037<<16,	/* mask */
	Firstseg	= 1<<13,
	Lastseg		= 1<<12,
	Bufsize		= MASK(11),

	/* txcmdb bits */
	Pkttag		= MASK(16) << 16,
	Txcksumen	= 1<<14,
	Addcrcdis	= 1<<13,
	Framepaddis	= 1<<12,
	Pktlen		= (1<<1) - 1,	/* mask */

	/* txcfg bits */
	Txsdump		= 1<<15,	/* flush tx status fifo */
	Txddump		= 1<<14,	/* flush tx data fifo */
	Txon		= 1<<1,
	Stoptx		= 1<<0,

	/* hwcfg bits */
	Mbo		= 1<<20,	/* must be one */
	Srstto		= 1<<1,		/* soft reset time-out */
	Srst		= 1<<0,

	/* rxcfg bits */
	Rxdmacntshift	= 16,		/* ulong count, 12 bits wide */
	Rxdmacntmask	= MASK(12) << Rxdmacntshift,
	Rxdump		= 1<<15,	/* flush rx fifos */

	/* rxsts bits */
	Rxpktlenshift	= 16,		/* byte count */
	Rxpktlenmask	= MASK(14) << Rxpktlenshift,
	Rxerr		= 1<<15,

	/* rxfifoinf bits */
	Rxstsusedshift	= 16,		/* ulong count */
	Rxstsusedmask	= MASK(8) << Rxstsusedshift,
	Rxdatausedmask	= MASK(16),	/* byte count */

	/* txfifoinf bits */
	Txstsusedshift	= 16,		/* ulong count */
	Txstsusedmask	= MASK(8) << Txstsusedshift,
	Txdatafreemask	= MASK(16),	/* byte count */

	/* pmtctl bits */
	Dready		= 1<<0,

	/* maccsrcmd bits */
	Csrbusy		= 1<<31,
	Csrread		= 1<<30,	/* not write */
	Csraddrshift	= 0,
	Csraddrmask	= MASK(8) - 1,

	/* mac registers' indices */
	Maccr		= 1,
	Macaddrh,
	Macaddrl,
	Machashh,
	Machashl,
	Macmiiacc,			/* for doubly-indirect phy access */
	Macmiidata,
	Macflow,
	Macvlan1,
	Macvlan2,
	Macwuff,
	Macwucsr,
	Maccoe,

	/* Maccr bits */
	Rxall		= 1<<31,
	Rcvown		= 1<<23,	/* don't receive own transmissions */
	Fdpx		= 1<<20,	/* full duplex */
	Mcpas		= 1<<19,	/* pass all multicast */
	Prms		= 1<<18,	/* promiscuous */
	Ho		= 1<<15,	/* hash-only filtering */
	Hpfilt		= 1<<13,	/* hash/perfect filtering */
	Padstr		= 1<<8,		/* strip padding & fcs (crc) */
	Txen		= 1<<3,
	Rxen		= 1<<2,

	/* irqcfg bits */
	Irqdeasclr	= 1<<14,	/* deassertion intv'l clear */
	Irqdeassts	= 1<<13,	/* deassertion intv'l status */
	Irqint		= 1<<12,	/* intr being asserted? (ro) */
	Irqen		= 1<<8,
	Irqpol		= 1<<4,		/* irq output is active high */
	Irqpushpull	= 1<<0,		/* irq output is push/pull driver */

	/* intsts/inten bits */
	Swint		= 1<<31,	/* generate an interrupt */
	Txstop		= 1<<25,
	Rxstop		= 1<<24,
	Txioc		= 1<<21,
	Rxdma		= 1<<20,
	Gptimer		= 1<<19,
	Phy		= 1<<18,
	Rxe		= 1<<14,	/* errors */
	Txe		= 1<<13,
	Tdfo		= 1<<10,	/* tx data fifo overrun */
	Tdfa		= 1<<9,		/* tx data fifo available */
	Tsff		= 1<<8,		/* tx status fifo full */
	Tsfl		= 1<<7,		/* tx status fifo level */
	Rsff		= 1<<4,		/* rx status fifo full */
	Rsfl		= 1<<3,		/* rx status fifo level */

	/* eepcmd bits */
	Epcbusy		= 1<<31,
	Epccmdshift	= 28,		/* interesting one is Reload (7) */
	Epctimeout	= 1<<9,
	Epcmacloaded	= 1<<8,
	Epcaddrshift	= 0,
};

enum {
	Rxintrs		= Rsff | Rsfl | Rxe,
	Txintrs		= Tsff | Tsfl | Txe | Txioc,
};

/* wake-up frame filter */
struct Wakeup {
	ulong	bytemask[4];		/* index is filter # */
	uchar	filt0cmd;		/* filter 0 command */
	uchar	_pad0;
	uchar	filt1cmd;
	uchar	_pad1;
	uchar	filt2cmd;
	uchar	_pad2;
	uchar	filt3cmd;
	uchar	_pad3;
	uchar	offset[4];		/* index is filter # */
	ushort	crc16[4];		/* " */
};

typedef struct Ctlr Ctlr;
struct Ctlr {
	int	port;
	Ctlr*	next;
	Ether*	edev;
	Regs*	regs;
	int	active;
	int	started;
	int	inited;
	int	id;
	int	cls;
	ushort	eeprom[0x40];

	QLock	alock;			/* attach */
	int	nrb;			/* how many this Ctlr has in the pool */

	int*	nic;
	Lock	imlock;
	int	im;			/* interrupt mask */

//	Mii*	mii;
//	Rendez	lrendez;
	int	lim;

	int	link;

	QLock	slock;
	uint	statistics[Nstatistics];
	uint	lsleep;
	uint	lintr;
	uint	rsleep;
	uint	rintr;
	int	tsleep;
	uint	tintr;

	uchar	ra[Eaddrlen];		/* receive address */
	ulong	mta[128];		/* multicast table array */

	Rendez	rrendez;
	int	gotinput;
	int	rdcpydone;

	Rendez	trendez;
	int	gotoutput;
	int	wrcpydone;

	Lock	tlock;
};

#define csr32r(c, r)	(*((c)->nic+((r)/4)))
#define csr32w(c, r, v)	(*((c)->nic+((r)/4)) = (v))

static Ctlr *smcctlrhead, *smcctlrtail;

static char* statistics[Nstatistics] = { "dummy", };

static uchar mymac[] = { 0xb0, 0x0f, 0xba, 0xbe, 0x00, 0x00, };

static void etherclock(void);
static void smcreceive(Ether *edev);
static void smcinterrupt(Ureg*, void* arg);

static Ether *thisether;
static int attached;

static void
smconce(Ether *edev)
{
	static int beenhere;
	static Lock l;

	ilock(&l);
	if (!beenhere && edev != nil) {
		beenhere = 1;
		/* simulate interrupts if we don't know the irq */
		if (edev->irq < 0) {		/* poll as backup */
			thisether = edev;
			addclock0link(etherclock, 1000/HZ);
			iprint(" polling");
		}
	}
	iunlock(&l);
}

/*
 * indirect (mac) register access
 */

static void
macwait(Regs *regs)
{
	long bound;

	for (bound = 400*Mhz; regs->maccsrcmd & Csrbusy && bound > 0; bound--)
		;
	if (bound <= 0)
		iprint("smc: mac registers didn't come ready\n");
}

static ulong
macrd(Regs *regs, uchar index)
{
	macwait(regs);
	regs->maccsrcmd = Csrbusy | Csrread | index;
	coherence();		/* back-to-back write/read delay per §6.2.1 */
	macwait(regs);
	return regs->maccsrdata;
}

static void
macwr(Regs *regs, uchar index, ulong val)
{
	macwait(regs);
	regs->maccsrdata = val;
	regs->maccsrcmd = Csrbusy | index;	/* fire */
	macwait(regs);
}


static long
smcifstat(Ether* edev, void* a, long n, ulong offset)
{
	Ctlr *ctlr;
	char *p, *s;
	int i, l, r;

	ctlr = edev->ctlr;
	qlock(&ctlr->slock);
	p = malloc(READSTR);
	l = 0;
	for(i = 0; i < Nstatistics; i++){
		// read regs->rxdrop TODO
		r = 0;
		if((s = statistics[i]) == nil)
			continue;
		switch(i){
		default:
			ctlr->statistics[i] += r;
			if(ctlr->statistics[i] == 0)
				continue;
			l += snprint(p+l, READSTR-l, "%s: %ud %ud\n",
				s, ctlr->statistics[i], r);
			break;
		}
	}

	l += snprint(p+l, READSTR-l, "lintr: %ud %ud\n",
		ctlr->lintr, ctlr->lsleep);
	l += snprint(p+l, READSTR-l, "rintr: %ud %ud\n",
		ctlr->rintr, ctlr->rsleep);
	l += snprint(p+l, READSTR-l, "tintr: %ud %ud\n",
		ctlr->tintr, ctlr->tsleep);

	l += snprint(p+l, READSTR-l, "eeprom:");
	for(i = 0; i < 0x40; i++){
		if(i && ((i & 0x07) == 0))
			l += snprint(p+l, READSTR-l, "\n       ");
		l += snprint(p+l, READSTR-l, " %4.4uX", ctlr->eeprom[i]);
	}
	l += snprint(p+l, READSTR-l, "\n");
	USED(l);

	n = readstr(offset, a, n, p);
	free(p);
	qunlock(&ctlr->slock);

	return n;
}

static void
smcpromiscuous(void* arg, int on)
{
	int rctl;
	Ctlr *ctlr;
	Ether *edev;
	Regs *regs;

	edev = arg;
	ctlr = edev->ctlr;
	regs = ctlr->regs;
	rctl = macrd(regs, Maccr);
	if(on)
		rctl |= Prms;
	else
		rctl &= ~Prms;
	macwr(regs, Maccr, rctl);
}

static void
smcmulticast(void*, uchar*, int)
{
	/* nothing to do, we allow all multicast packets in */
}

static int
iswrcpydone(void *arg)
{
	return ((Ctlr *)arg)->wrcpydone;
}

static int
smctxstart(Ctlr *ctlr, uchar *ubuf, uint len)
{
	uint wds, ruplen;
	ulong *wdp, *txdp;
	Regs *regs;
	static ulong buf[ROUNDUP(ETHERMAXTU, sizeof(ulong)) / sizeof(ulong)];

	if (!ctlr->inited) {
		iprint("smctxstart: too soon to send\n");
		return -1;		/* toss it */
	}
	regs = ctlr->regs;

	/* is there room for a packet in the tx data fifo? */
	if (len < ETHERMINTU)
		iprint("sending too-short (%d) pkt\n", len);
	else if (len > ETHERMAXTU)
		iprint("sending jumbo (%d) pkt\n", len);

	ruplen = ROUNDUP(len, sizeof(ulong));
	coherence();	/* back-to-back read/read delay per §6.2.2 */
	if ((regs->txfifoinf & Txdatafreemask) < ruplen + 2*sizeof(ulong))
		return -1;	/* not enough room for data + command words */

	if ((uintptr)ubuf & MASK(2)) {		/* ensure word alignment */
		memmove(buf, ubuf, len);
		ubuf = (uchar *)buf;
	}

	/* tx cmd a: length is bytes in this buffer */
	txdp = &regs->txdata;
	*txdp = Intcompl | Firstseg | Lastseg | len;
	/* tx cmd b: length is bytes in this packet (could be multiple buf.s) */
	*txdp = len;

	/* shovel pkt into tx fifo, which triggers transmission due to Txon */
	wdp = (ulong *)ubuf;
	for (wds = ruplen / sizeof(ulong) + 1; --wds > 0; )
		*txdp = *wdp++;

	regs->intsts = Txintrs;		/* dismiss intr */
	coherence();
	regs->inten |= Txintrs;
	coherence();		/* back-to-back write/read delay per §6.2.1 */
	return 0;
}

static void
smctransmit(Ether* edev)
{
	Block *bp;
	Ctlr *ctlr;

	ctlr = edev->ctlr;
	if (ctlr == nil)
		panic("smctransmit: nil ctlr");
	ilock(&ctlr->tlock);
	/*
	 * Try to fill the chip's buffers back up, via the tx fifo.
	 */
	while ((bp = qget(edev->oq)) != nil)
		if (smctxstart(ctlr, bp->rp, BLEN(bp)) < 0) {
			qputback(edev->oq, bp);	/* retry the block later */
			iprint("smctransmit: tx data fifo full\n");
			break;
		} else
			freeb(bp);
	iunlock(&ctlr->tlock);
}

static void
smctransmitcall(Ether *edev)		/* called from devether.c */
{
	Ctlr *ctlr;

	ctlr = edev->ctlr;
	ctlr->gotoutput = 1;
#ifdef USE_KPROCS
	wakeup(&ctlr->trendez);
#else
	smctransmit(edev);
#endif
}

static int
smcrim(void* ctlr)
{
	return ((Ctlr*)ctlr)->gotinput;
}

static void
smcrproc(void* arg)
{
	Ctlr *ctlr;
	Ether *edev;

	edev = arg;
	ctlr = edev->ctlr;
	for(;;){
		ctlr->rsleep++;
		sleep(&ctlr->rrendez, smcrim, ctlr);

		/* process any newly-arrived packets and pass to etheriq */
		ctlr->gotinput = 0;
		smcreceive(edev);
	}
}

static int
smcgotout(void* ctlr)
{
	return ((Ctlr*)ctlr)->gotoutput;
}

static void
smctproc(void* arg)
{
	Ctlr *ctlr;
	Ether *edev;

	edev = arg;
	ctlr = edev->ctlr;
	for(;;){
		ctlr->tsleep++;
		sleep(&ctlr->trendez, smcgotout, ctlr);

		/* process any newly-arrived packets and pass to etheriq */
		ctlr->gotoutput = 0;
		smctransmit(edev);
	}
}

void	gpioirqclr(void);

static void
smcattach(Ether* edev)
{
#ifdef USE_KPROCS
	char name[KNAMELEN];
#endif
	Ctlr *ctlr;

	ctlr = edev->ctlr;
	qlock(&ctlr->alock);
	if(waserror()){
		qunlock(&ctlr->alock);
		nexterror();
	}
	if (!ctlr->inited) {
		ctlr->inited = 1;
#ifdef USE_KPROCS
		snprint(name, KNAMELEN, "#l%drproc", edev->ctlrno);
		kproc(name, smcrproc, edev);

		snprint(name, KNAMELEN, "#l%dtproc", edev->ctlrno);
		kproc(name, smctproc, edev);
#endif

iprint("smcattach:");
#ifdef USE_KPROCS
iprint(" with kprocs");
#else
iprint(" no kprocs");
#endif
iprint(", no dma");
		/* can now accept real or simulated interrupts */

		smconce(edev);
		attached = 1;
iprint("\n");
	}
	qunlock(&ctlr->alock);
	poperror();
}

static int
isrdcpydone(void *arg)
{
	return ((Ctlr *)arg)->rdcpydone;
}

static void
smcreceive(Ether *edev)
{
	uint wds, len, sts;
	ulong *wdp, *rxdp;
	Block *bp;
	Ctlr *ctlr;
	Regs *regs;

	ctlr = edev->ctlr;
	regs = ctlr->regs;
	coherence();		/* back-to-back read/read delay per §6.2.2 */
	/*
	 * is there a full packet in the rx data fifo?
	 */
	while (((regs->rxfifoinf & Rxstsusedmask) >> Rxstsusedshift) != 0) {
		coherence();
		sts = regs->rxsts;		/* pop rx status */
		if(sts & Rxerr)
			iprint("smcreceive: rx error\n");
		len = (sts & Rxpktlenmask) >> Rxpktlenshift;
		if (len > ETHERMAXTU + Slop)
			iprint("smcreceive: oversized rx pkt (%d)\n", len);
		else if (len < ETHERMINTU)
			iprint("smcreceive: too-short (%d) pkt\n", len);
		wds = ROUNDUP(len, sizeof(ulong)) / sizeof(ulong);
		if (wds > 0) {
			/* copy aligned words from rx fifo into a Block */
			bp = iallocb(len + sizeof(ulong) /* - 1 */);
			if (bp == nil)
				panic("smcreceive: nil Block*");

			/* bp->rp should be 32-byte aligned, more than we need */
			assert(((uintptr)bp->rp & (sizeof(ulong) - 1)) == 0);
			wdp = (ulong *)bp->rp;
			rxdp = &regs->rxdata;
			wds = ROUNDUP(len, sizeof(ulong)) / sizeof(ulong) + 1;
			while (--wds > 0)
				*wdp++ = *rxdp;
			bp->wp = bp->rp + len;

			/* and push the Block upstream */
			if (ctlr->inited)
				etheriq(edev, bp, 1);
			else
				freeb(bp);

			regs->intsts = Rxintrs;		/* dismiss intr */
			coherence();
			regs->inten |= Rxintrs;
		}
		coherence();
	}
	regs->inten |= Rxintrs;
	coherence();
}

/*
 * disable the stsclr bits in inten and write them to intsts to ack and dismiss
 * the interrupt source.
 */
void
ackintr(Regs *regs, ulong stsclr)
{
	if (stsclr == 0)
		return;

	regs->inten &= ~stsclr;
	coherence();

//	regs->intsts = stsclr;		/* acknowledge & clear intr(s) */
//	coherence();
}

static void
smcinterrupt(Ureg*, void* arg)
{
	int junk;
	unsigned intsts, intr;
	Ctlr *ctlr;
	Ether *edev;
	Regs *regs;

	edev = arg;
	ctlr = edev->ctlr;
	ilock(&ctlr->imlock);
	regs = ctlr->regs;

	gpioirqclr();

	coherence();		/* back-to-back read/read delay per §6.2.2 */
	intsts = regs->intsts;
	coherence();

	intsts &= ~MASK(3);		/* ignore gpio bits */
	if (0 && intsts == 0) {
		coherence();
		iprint("smc: interrupt without a cause; insts %#ux (vs inten %#lux)\n",
			intsts, regs->inten);
	}

	intr = intsts & Rxintrs;
	if(intr) {
		/* disable interrupt sources; kproc/smcreceive will reenable */
		ackintr(regs, intr);

		ctlr->rintr++;
		ctlr->gotinput = 1;
#ifdef USE_KPROCS
		wakeup(&ctlr->rrendez);
#else
		smcreceive(edev);
#endif
	}

	while(((regs->txfifoinf & Txstsusedmask) >> Txstsusedshift) != 0) {
		/* probably indicates tx completion, just toss it */
		junk = regs->txsts;		/* pop tx sts */
		USED(junk);
		coherence();
	}

	intr = intsts & Txintrs;
	if (ctlr->gotoutput || intr) {
		/* disable interrupt sources; kproc/smctransmit will reenable */
		ackintr(regs, intr);

		ctlr->tintr++;
		ctlr->gotoutput = 1;
#ifdef USE_KPROCS
		wakeup(&ctlr->trendez);
#else
		smctransmit(edev);
#endif
	}

	iunlock(&ctlr->imlock);
}

static void
etherclock(void)
{
	smcinterrupt(nil, thisether);
}

static int
smcmii(Ctlr *)
{
	return 0;
}

static int
smcdetach(Ctlr* ctlr)
{
	Regs *regs;

	if (ctlr == nil || ctlr->regs == nil)
		return -1;
	regs = ctlr->regs;
	/* verify that it's real by reading a few registers */
	switch (regs->id) {
	case Vid9221:
		break;
	default:
		print("smc: unknown chip id %#ux\n", regs->id);
		return -1;
	}
	regs->inten = 0;		/* no interrupts */
	regs->intsts = ~0;		/* clear any pending */
	regs->gptcfg = 0;
	coherence();
	regs->rxcfg = Rxdump;
	regs->txcfg = Txsdump | Txddump;
	regs->irqcfg &= ~Irqen;
	coherence();
	return 0;
}

static void
smcshutdown(Ether* ether)
{
	smcdetach(ether->ctlr);
}

static void
powerwait(Regs *regs)
{
	long bound;

	regs->bytetest = 0;			/* bring power on */
	for (bound = 400*Mhz; !(regs->pmtctl & Dready) && bound > 0; bound--)
		;
	if (bound <= 0)
		iprint("smc: pmtctl didn't come ready\n");
}

static int
smcreset(Ctlr* ctlr)
{
	int r;
	Regs *regs;
	static char zea[Eaddrlen];

	regs = ctlr->regs;
	powerwait(regs);

	if(smcdetach(ctlr))
		return -1;

	/* verify that it's real by reading a few registers */
	switch (regs->id) {
	case Vid9221:
		break;
	default:
		print("smc: unknown chip id %#ux\n", regs->id);
		return -1;
	}
	if (regs->bytetest != 0x87654321) {
		print("smc: bytetest reg %#p (%#lux) != 0x87654321\n",
			&regs->bytetest, regs->bytetest);
		return -1;
	}

#ifdef TODO			/* read MAC from EEPROM */
//	int ctrl, i, pause, swdpio, txcw;
	/*
	 * Snarf and set up the receive addresses.
	 * There are 16 addresses. The first should be the MAC address.
	 * The others are cleared and not marked valid (MS bit of Rah).
	 */
	for(i = Ea; i < Eaddrlen/2; i++){
		ctlr->ra[2*i] = ctlr->eeprom[i];
		ctlr->ra[2*i+1] = ctlr->eeprom[i]>>8;
	}

	/*
	 * Clear the Multicast Table Array.
	 * It's a 4096 bit vector accessed as 128 32-bit registers.
	 */
	memset(ctlr->mta, 0, sizeof(ctlr->mta));
	for(i = 0; i < 128; i++)
		csr32w(ctlr, Mta+i*4, 0);
#endif
	regs->hwcfg |= Mbo;

	/* don't overwrite existing ea */
//	if (memcmp(edev->ea, zea, Eaddrlen) == 0)
//		memmove(edev->ea, ctlr->ra, Eaddrlen);

	r = ctlr->ra[3]<<24 | ctlr->ra[2]<<16 | ctlr->ra[1]<<8 | ctlr->ra[0];
	macwr(regs, Macaddrl, r);
	macwr(regs, Macaddrh, ctlr->ra[5]<<8 | ctlr->ra[4]);

	/* turn on the controller */
	macwr(regs, Maccoe, 0);
	regs->inten = 0;		/* no interrupts yet */
	regs->intsts = ~0;		/* clear any pending */
	regs->gptcfg = 0;
	coherence();
	regs->rxcfg = Rxdump;
	regs->txcfg = Txsdump | Txddump | Txon;
	regs->fifoint = 72<<24;		/* default values */
	macwr(regs, Maccr, Rxall | Rcvown | Fdpx | Mcpas | Txen | Rxen);
	coherence();		/* back-to-back write/read delay per §6.2.1 */
	regs->irqcfg = 1<<24 | Irqen | Irqpushpull;  /* deas for 10µs (linux) */
	coherence();		/* back-to-back write/read delay per §6.2.1 */
	regs->inten = Rxintrs | Txintrs;
	coherence();

	if(smcmii(ctlr) < 0)
		return -1;
	return 0;
}

static void
smcpci(void)
{
	Ctlr *ctlr;
	static int beenhere;

	if (beenhere)
		return;
	beenhere = 1;

	if (probeaddr(PHYSETHER) < 0)
		return;
	ctlr = malloc(sizeof(Ctlr));
	ctlr->id = Vid9221<<16 | 0x0424;	/* smsc 9221 */
	ctlr->port = PHYSETHER;
	ctlr->nic = (int *)PHYSETHER;
	ctlr->regs = (Regs *)PHYSETHER;

	if(smcreset(ctlr)){
		free(ctlr);
		return;
	}
	if(smcctlrhead != nil)
		smcctlrtail->next = ctlr;
	else
		smcctlrhead = ctlr;
	smcctlrtail = ctlr;
}

static int
smcpnp(Ether* edev)
{
	Ctlr *ctlr;
	static char zea[Eaddrlen];

	if(smcctlrhead == nil)
		smcpci();

	/*
	 * Any adapter matches if no edev->port is supplied,
	 * otherwise the ports must match.
	 */
	for(ctlr = smcctlrhead; ctlr != nil; ctlr = ctlr->next){
		if(ctlr->active)
			continue;
		if(edev->port == 0 || edev->port == ctlr->port){
			ctlr->active = 1;
			break;
		}
	}
	if(ctlr == nil)
		return -1;

	edev->ctlr = ctlr;
	ctlr->edev = edev;			/* point back to Ether* */
	edev->port = ctlr->port;
	edev->irq = 34;
// TODO: verify speed (100Mb/s) and duplicity (full-duplex)
	edev->mbps = 100;

	/* don't overwrite existing ea */
	if (memcmp(edev->ea, zea, Eaddrlen) == 0)
		memmove(edev->ea, ctlr->ra, Eaddrlen);

	/*
	 * Linkage to the generic ethernet driver.
	 */
	edev->attach = smcattach;
	edev->transmit = smctransmitcall;
	edev->interrupt = smcinterrupt;
	edev->ifstat = smcifstat;
/*	edev->ctl = smcctl;			/* no ctl msgs supported */

	edev->arg = edev;
	edev->promiscuous = smcpromiscuous;
	edev->multicast = smcmulticast;
	edev->shutdown = smcshutdown;
	return 0;
}

void
ether9221link(void)
{
	addethercard("9221", smcpnp);
}