Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*	$NetBSD: if_le_ebus.c,v 1.20 2019/05/29 10:07:28 msaitoh Exp $	*/

/*-
 * Copyright (c) 2010 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code was written by Alessandro Forin and Neil Pittman
 * at Microsoft Research and contributed to The NetBSD Foundation
 * by Microsoft Corporation.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_le_ebus.c,v 1.20 2019/05/29 10:07:28 msaitoh Exp $");

#include "opt_inet.h"



#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/syslog.h>
#include <sys/socket.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/ioctl.h>
#include <sys/errno.h>

#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_ether.h>
#include <net/if_media.h>
#include <net/bpf.h>

#ifdef INET
#include <netinet/in.h>
#include <netinet/if_inarp.h>
#endif

#include <sys/rndsource.h>

#include <emips/ebus/ebusvar.h>
#include <emips/emips/machdep.h>
#include <machine/emipsreg.h>

extern paddr_t kvtophys(vaddr_t);

struct bufmap {
	struct mbuf *mbuf;
	paddr_t phys;
};

struct enic_softc {
	device_t sc_dev;		/* base device glue */
	struct	ethercom sc_ethercom;	/* Ethernet common part */
	struct	ifmedia sc_media;	/* our supported media */

	struct _Enic *sc_regs;		/* hw registers */

	int	sc_havecarrier;		/* carrier status */
	void	*sc_sh;			/* shutdownhook cookie */
	int inited;

	int sc_no_rd;
	int sc_n_recv;
	int sc_recv_h;
	/* BUGBUG really should be malloc-ed */
#define SC_MAX_N_RECV 64
	struct bufmap sc_recv[SC_MAX_N_RECV];

	int sc_no_td;
	int sc_n_xmit;
	int sc_xmit_h;
	/* BUGBUG really should be malloc-ed */
#define SC_MAX_N_XMIT 16
	struct bufmap sc_xmit[SC_MAX_N_XMIT];

#if DEBUG
	int xhit;
	int xmiss;
	int tfull;
	int tfull2;
	int brh;
	int rf;
	int bxh;

	int it;
#endif

	uint8_t sc_enaddr[ETHER_ADDR_LEN];
	uint8_t sc_pad[2];
	krndsource_t	rnd_source;
};

void enic_reset(struct ifnet *);
int enic_init(struct ifnet *);
void enic_stop(struct ifnet *, int);
void enic_start(struct ifnet *);
void enic_shutdown(void *);
void enic_watchdog(struct ifnet *);
int enic_mediachange(struct ifnet *);
void enic_mediastatus(struct ifnet *, struct ifmediareq *);
int enic_ioctl(struct ifnet *, u_long, void *);
int enic_intr(void *, void *);
void enic_rint(struct enic_softc *, uint32_t, paddr_t);
void enic_tint(struct enic_softc *, uint32_t, paddr_t);
void enic_kill_xmit(struct enic_softc *);
void enic_post_recv(struct enic_softc *, struct mbuf *);
void enic_refill(struct enic_softc *);
static int enic_gethwinfo(struct enic_softc *);
int enic_put(struct enic_softc *, struct mbuf **);

static int enic_match(device_t, cfdata_t, void *);
static void enic_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(enic_emips, sizeof(struct enic_softc),
    enic_match, enic_attach, NULL, NULL);

int
enic_match(device_t parent, cfdata_t cf, void *aux)
{
	struct ebus_attach_args *d = aux;
	/* donno yet */
	struct _Enic *et = (struct _Enic *)d->ia_vaddr;

	if (strcmp("enic", d->ia_name) != 0)
		return 0;
	if ((et == NULL) || (et->Tag != PMTTAG_ETHERNET))
		return 0;
	return 1;
}

void
enic_attach(device_t parent, device_t self, void *aux)
{
	struct enic_softc *sc = device_private(self);
	struct ebus_attach_args *ia = aux;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;

	sc->sc_regs = (struct _Enic *)(ia->ia_vaddr);
#if DEBUG
	printf(" virt=%p ", (void *)sc->sc_regs);
#endif

	/* Get the MAC and the depth of the FIFOs */
	if (!enic_gethwinfo(sc)) {
		printf(" <cannot get hw info> DISABLED.\n");
		/*
		 * NB: caveat maintainer: make sure what we
		 * did NOT do below does not hurt the system
		 */
		return;
	}

	sc->sc_dev = self;
	sc->sc_no_td = 0;
	sc->sc_havecarrier = 1; /* BUGBUG */
	sc->sc_recv_h = 0;
	sc->sc_xmit_h = 0;
	/* uhmm do I need to do this? */
	memset(sc->sc_recv, 0, sizeof sc->sc_recv);
	memset(sc->sc_xmit, 0, sizeof sc->sc_xmit);

	/* Initialize ifnet structure. */
	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
	ifp->if_softc = sc;
	ifp->if_start = enic_start;
	ifp->if_ioctl = enic_ioctl;
	ifp->if_watchdog = enic_watchdog;
	ifp->if_init = enic_init;
	ifp->if_stop = enic_stop;
	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
	IFQ_SET_READY(&ifp->if_snd);

	/* Initialize ifmedia structures. */
	sc->sc_ethercom.ec_ifmedia = &sc->sc_media;
	ifmedia_init(&sc->sc_media, 0, enic_mediachange, enic_mediastatus);
	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_MANUAL);

	/* Make sure the chip is stopped. */
	enic_stop(ifp, 0);
	sc->inited = 0;

	/* Get the mac address and print it */
	printf(": eNIC [%d %d], address %s\n",
	    sc->sc_n_recv, sc->sc_n_xmit, ether_sprintf(sc->sc_enaddr));

	/* claim 802.1q capability */
#if 0
	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
#endif

	/* Attach the interface. */
	if_attach(ifp);
	if_deferred_start_init(ifp, NULL);
	ether_ifattach(ifp, sc->sc_enaddr);

	sc->sc_sh = shutdownhook_establish(enic_shutdown, ifp);
	if (sc->sc_sh == NULL)
		panic("enic_attach: cannot establish shutdown hook");

	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
			  RND_TYPE_NET, RND_FLAG_DEFAULT);

	ebus_intr_establish(parent, (void *)ia->ia_cookie, IPL_NET,
	    enic_intr, sc);
}

/*
 * Beware: does not work while the nic is running
 */
static int enic_gethwinfo(struct enic_softc *sc)
{
	uint8_t buffer[8];	/* 64bits max */
	PENIC_INFO hw = (PENIC_INFO)buffer;
	paddr_t phys = kvtophys((vaddr_t)&buffer[0]), phys2;
	int i;

	/*
	 * First thing first, get the MAC address
	 */
	memset(buffer, 0, sizeof buffer);
	buffer[0] = ENIC_CMD_GET_ADDRESS;
	buffer[3] = ENIC_CMD_GET_ADDRESS;	/* bswap bug */
	sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
	sc->sc_regs->BufferAddressHi32 = 0;
	sc->sc_regs->BufferAddressLo32 = phys; /* go! */

	for (i = 0; i < 100; i++) {
		DELAY(100);
		if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0)
			break;
	}
	if (i == 100)
		return 0;

	phys2 = sc->sc_regs->BufferAddressLo32;
	if (phys2 != phys) {
		printf("enic uhu? %llx != %llx?\n",
		    (long long)phys, (long long)phys2);
		return 0;
	}
	memcpy(sc->sc_enaddr, buffer, ETHER_ADDR_LEN);

	/*
	 * Next get the HW parameters
	 */
	memset(buffer, 0, sizeof buffer);
	buffer[0] = ENIC_CMD_GET_INFO;
	buffer[3] = ENIC_CMD_GET_INFO;	/* bswap bug */
	sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
	sc->sc_regs->BufferAddressHi32 = 0;
	sc->sc_regs->BufferAddressLo32 = phys; /* go! */

	for (i = 0; i < 100; i++) {
		DELAY(100);
		if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0)
			break;
	}
	if (i == 100)
		return 0;

	phys2 = sc->sc_regs->BufferAddressLo32;
	if (phys2 != phys) {
		printf("enic uhu2? %llx != %llx?\n",
		    (long long)phys, (long long)phys2);
		return 0;
	}
#if 0
	printf("enic: hwinfo: %x %x %x %x %x %x \n",
	    hw->InputFifoSize, hw->OutputFifoSize, hw->CompletionFifoSize,
	    hw->ErrorCount, hw->FramesDropped, hw->Reserved);
#endif

	/*
	 * Get FIFO depths and cap them
	 */
	sc->sc_n_recv = hw->InputFifoSize;
	if (sc->sc_n_recv > SC_MAX_N_RECV)
		sc->sc_n_recv = SC_MAX_N_RECV;
	if (sc->sc_n_recv == 0) { /* sanity and compat with old hw/simulator */
		sc->sc_n_recv = 8;
		sc->sc_n_xmit = 4;
	} else {
		sc->sc_n_xmit = hw->OutputFifoSize;
		if (sc->sc_n_xmit > SC_MAX_N_XMIT)
			sc->sc_n_xmit = SC_MAX_N_XMIT;
	}

	return 1;
}

void
enic_reset(struct ifnet *ifp)
{
	int s;

	s = splnet();
	enic_stop(ifp, 0);
	enic_init(ifp);
	splx(s);
}

void
enic_stop(struct ifnet *ifp, int suspend)
{
	struct enic_softc *sc = ifp->if_softc;

#if 0
	printf("enic_stop %x\n", sc->sc_regs->Control);
#endif

	/*
	 * NB: only "ifconfig down" says suspend=1 (then "up" calls init)
	 * Could simply set RXDIS in this case
	 */
	sc->inited = 2;
	sc->sc_regs->Control = EC_RESET;
	sc->sc_no_rd = 0; /* they are gone */
	sc->sc_no_td = 0; /* they are gone */
}

void
enic_shutdown(void *arg)
{
	struct ifnet *ifp = arg;

	enic_stop(ifp, 0);
}

void
enic_kill_xmit(struct enic_softc *sc)
{
	int i;
	struct mbuf *m;

	for (i = 0; i < sc->sc_n_xmit; i++) {
		if ((m = sc->sc_xmit[i].mbuf) != NULL) {
			sc->sc_xmit[i].mbuf = NULL;
			sc->sc_xmit[i].phys = ~0;
			m_freem(m);
		}
	}
	sc->sc_no_td = 0;
	sc->sc_xmit_h = 0;
}

void
enic_post_recv(struct enic_softc *sc, struct mbuf *m)
{
	int i, waitmode = M_DONTWAIT;
	paddr_t phys;

#define rpostone(_p_) \
    sc->sc_regs->SizeAndFlags = ES_F_RECV | MCLBYTES; \
    sc->sc_regs->BufferAddressHi32 = 0; \
    sc->sc_regs->BufferAddressLo32 = _p_;
#define tpostone(_p_,_s_) \
    sc->sc_regs->SizeAndFlags = ES_F_XMIT | (_s_); \
    sc->sc_regs->BufferAddressHi32 = 0; \
    sc->sc_regs->BufferAddressLo32 = _p_;

	/* Operational reload? */
	if (m != NULL) {
		/* But is the hw ready for it */
		if (sc->sc_regs->Control & EC_IF_FULL)
			goto no_room;
		/* Yes, find a spot. Include empty q case. */
		for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
			if (sc->sc_recv[i].mbuf == NULL)
				goto found;
		for (i = 0; i < sc->sc_recv_h; i++)
			if (sc->sc_recv[i].mbuf == NULL)
				goto found;
		/* no spot, drop it (sigh) */
 no_room:
#if DEBUG
		sc->rf++;
#endif
		m_freem(m);
		return;
 found:
		phys = kvtophys((vaddr_t)m->m_data);
		sc->sc_recv[i].mbuf = m;
		sc->sc_recv[i].phys = phys;
		rpostone(phys);
		sc->sc_no_rd++;
		return;
	}

	/* Repost after reset? */
	if (sc->inited) {
		/* order doesnt matter, might as well keep it clean */
		int j = 0;
		sc->sc_recv_h = 0;
		for (i = 0; i < sc->sc_n_recv; i++)
			if ((m = sc->sc_recv[i].mbuf) != NULL) {
				phys = sc->sc_recv[i].phys;
				sc->sc_recv[i].mbuf = NULL;
				sc->sc_recv[i].phys = ~0;
				sc->sc_recv[j].mbuf = m;
				sc->sc_recv[j].phys = phys;
#if DEBUG
				if (sc->sc_regs->Control & EC_IF_FULL)
					printf("?uhu? postrecv full? %d\n",
					    sc->sc_no_rd);
#endif
				sc->sc_no_rd++;
				rpostone(phys);
				j++;
			}
		/* Any holes left? */
		sc->inited = 1;
		if (j >= sc->sc_n_recv)
			return;	/* no, we are done */
		/* continue on with the loop below */
		i = j; m = NULL;
		goto fillem;
	}

	/* Initial refill, we can wait */
	waitmode = M_WAIT;
	sc->sc_recv_h = 0;
	memset(sc->sc_recv, 0, sizeof(sc->sc_recv[0]) * sc->sc_n_recv);
	i = 0;
 fillem:
	for (; i < sc->sc_n_recv; i++) {
		MGETHDR(m, waitmode, MT_DATA);
		if (m == 0)
			break;
		m_set_rcvif(m, &sc->sc_ethercom.ec_if);
		m->m_pkthdr.len = 0;

		MCLGET(m, waitmode);
		if ((m->m_flags & M_EXT) == 0)
			break;

		/*
		 * This offset aligns IP/TCP headers and helps performance
		 */
#if 1
#define ADJUST_MBUF_OFFSET(_m_) { \
	(_m_)->m_data += 2; \
	(_m_)->m_len -= 2; \
}
#else
#define ADJUST_MBUF_OFFSET(_m_)
#endif

		m->m_len = MCLBYTES;

		ADJUST_MBUF_OFFSET(m);
		phys = kvtophys((vaddr_t)m->m_data);
		sc->sc_recv[i].mbuf = m;
		sc->sc_recv[i].phys = phys;
#if DEBUG
		if (sc->sc_regs->Control & EC_IF_FULL)
			printf("?uhu? postrecv2 full? %d\n", sc->sc_no_rd);
#endif
		sc->sc_no_rd++;
		rpostone(phys);
		m = NULL;
	}

	if (m)
		m_freem(m);
	sc->inited = 1;
}

void enic_refill(struct enic_softc *sc)
{
	struct mbuf *m;
	int waitmode = M_DONTWAIT;

	MGETHDR(m, waitmode, MT_DATA);
	if (m == NULL)
		return;
	m_set_rcvif(m, &sc->sc_ethercom.ec_if);
	m->m_pkthdr.len = 0;

	MCLGET(m, waitmode);
	if ((m->m_flags & M_EXT) == 0) {
		m_freem(m);
		return;
	}

	m->m_len = MCLBYTES;
	ADJUST_MBUF_OFFSET(m);

	enic_post_recv(sc, m);
}

int
enic_init(struct ifnet *ifp)
{
	struct enic_softc *sc = ifp->if_softc;
	uint32_t ctl;

	/* no need to init many times unless we are in reset */
	if (sc->inited != 1) {

		/* Cancel all xmit buffers */
		enic_kill_xmit(sc);

		/* Re-post all recv buffers */
		enic_post_recv(sc, NULL);
	}

	/* Start the eNIC */
	ifp->if_flags |= IFF_RUNNING;
	ifp->if_flags &= ~IFF_OACTIVE;
	ifp->if_timer = 0;
	ctl = sc->sc_regs->Control | EC_INTEN;
	ctl &= ~EC_RXDIS;
	sc->sc_regs->Control = ctl;
#if 0
	printf("enic_init <- %x\n", ctl);
#endif

	if_schedule_deferred_start(ifp);

	return 0;
}


void
enic_watchdog(struct ifnet *ifp)
{
	struct enic_softc *sc = ifp->if_softc;

#if 0
	printf("enic_watch ctl=%x\n", sc->sc_regs->Control);
#endif
	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
	++ifp->if_oerrors;

	enic_reset(ifp);
}

int
enic_mediachange(struct ifnet *ifp)
{
#if 0
	struct enic_softc *sc = ifp->if_softc;
#endif
	/* more code here.. */

	return 0;
}

void
enic_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
{
	struct enic_softc *sc = ifp->if_softc;

	if ((ifp->if_flags & IFF_UP) == 0)
		return;

	ifmr->ifm_status = IFM_AVALID;
	if (sc->sc_havecarrier)
		ifmr->ifm_status |= IFM_ACTIVE;

	/* more code here someday.. */
}

/*
 * Process an ioctl request.
 */
int
enic_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
	struct enic_softc *sc = ifp->if_softc;
	struct ifreq *ifr = (struct ifreq *)data;
	int s, error = 0;

	s = splnet();

	switch (cmd) {
	case SIOCSIFMEDIA:
#if 0 /*DEBUG*/
	    {
		extern int ei_drops[];
		static int flip = 0;
		if (flip++ == 2) {
			int i;
			flip = 0;
			printf("enic_ioctl(%x) %qd/%qd %qd/%qd %d/%d %d:%d "
			    "%d+%d %d/%d/%d\n", ifp->if_flags,
			    ifp->if_ierrors, ifp->if_oerrors,
			    ifp->if_ipackets, ifp->if_opackets,
			    sc->sc_no_rd, sc->sc_no_td,
			    sc->xhit, sc->xmiss,
			    sc->tfull, sc->tfull2,
			    sc->brh, sc->rf, sc->bxh);
			printf(" Ctl %x lt %x tim %d\n",
			    sc->sc_regs->Control, sc->it, ifp->if_timer);

			for (i = 0; i < 64; i++)
				if (ei_drops[i])
					printf(" %d.%d", i, ei_drops[i]);
			printf("\n");
		}
	    }
#endif
		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
		break;

	default:
		error = ether_ioctl(ifp, cmd, data);
		if (cmd == SIOCSIFADDR) {
			/*
			 * hackattack: NFS does not turn us back
			 * on after a stop. So.
			 */
#if 0
			printf("enic_ioctl(%lx)\n", cmd);
#endif
			enic_init(ifp);
		}
		if (error != ENETRESET)
			break;
		error = 0;
		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
			break;
		if (ifp->if_flags & IFF_RUNNING) {
			enic_reset(ifp);
		}
		break;
	}
	splx(s);

	return error;
}

int
enic_intr(void *cookie, void *f)
{
	struct enic_softc *sc = cookie;
	uint32_t isr, saf, hi, lo, fl;

	isr = sc->sc_regs->Control;

	/* Make sure there is one and that we should take it */
	if ((isr & (EC_INTEN | EC_DONE)) != (EC_INTEN | EC_DONE))
		return 0;

	if (isr & EC_ERROR) {
		printf("%s: internal error\n", device_xname(sc->sc_dev));
		enic_reset(&sc->sc_ethercom.ec_if);
		return 1;
	}

	/*
	 * pull out all completed buffers
	 */
	while ((isr & EC_OF_EMPTY) == 0) {

		/* beware, order matters */
		saf = sc->sc_regs->SizeAndFlags;
		hi  = sc->sc_regs->BufferAddressHi32; /* BUGBUG 64bit */
		lo  = sc->sc_regs->BufferAddressLo32; /* this pops the fifo */
		__USE(hi);

		fl = saf & (ES_F_MASK &~ ES_F_DONE);
		if (fl == ES_F_RECV)
			enic_rint(sc, saf, lo);
		else if (fl == ES_F_XMIT)
			enic_tint(sc, saf, lo);
		else {
			/*
			 * we do not currently expect or care for
			 * command completions?
			 */
			if (fl != ES_F_CMD)
				printf("%s: invalid saf=x%x (lo=%x)\n",
				    device_xname(sc->sc_dev), saf, lo);
		}

		isr = sc->sc_regs->Control;
	}

	rnd_add_uint32(&sc->rnd_source, isr);

	return 1;
}

void
enic_rint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
{
	struct mbuf *m;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	int len = saf & ES_S_MASK, i;

	/* Find what buffer it is. Should be the first. */
	for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
		if (sc->sc_recv[i].phys == phys)
			goto found;
	for (i = 0; i < sc->sc_recv_h; i++)
		if (sc->sc_recv[i].phys == phys)
			goto found;

	/* uhu?? */
	printf("%s: bad recv phys %llx\n", device_xname(sc->sc_dev),
	    (long long)phys);
	ifp->if_ierrors++;
	return;

	/* got it, pop it */
 found:
	sc->sc_no_rd--;
	m = sc->sc_recv[i].mbuf;
	sc->sc_recv[i].mbuf = NULL;
	sc->sc_recv[i].phys = ~0;
	if (i == sc->sc_recv_h) { /* should be */
		sc->sc_recv_h = (++i == sc->sc_n_recv) ? 0 : i;
	}
#if DEBUG
	else
		sc->brh++;
#endif

	if (len <= sizeof(struct ether_header) ||
	    len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
		ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
		ETHERMTU + sizeof(struct ether_header))) {
		ifp->if_ierrors++;

		/* reuse it */
		enic_post_recv(sc, m);
		return;
	}

	/* Adjust size */
	m->m_pkthdr.len = len;
	m->m_len = len; /* recheck */

	/* Pass the packet up. */
	if_percpuq_enqueue(ifp->if_percpuq, m);

	/* Need to refill now */
	enic_refill(sc);
}

void enic_tint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
{
	struct mbuf *m;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	int i;

#if DEBUG
	sc->it = 1;
#endif

	/* BUGBUG should there be a per-buffer error bit in SAF? */

	/* Find what buffer it is. Should be the first. */
	for (i = sc->sc_xmit_h; i < sc->sc_n_xmit; i++)
		if (sc->sc_xmit[i].phys == phys)
			goto found;
	for (i = 0; i < sc->sc_xmit_h; i++)
		if (sc->sc_xmit[i].phys == phys)
			goto found;

	/* uhu?? */
	printf("%s: bad xmit phys %llx\n", device_xname(sc->sc_dev),
	    (long long)phys);
	ifp->if_oerrors++;
	return;

	/* got it, pop it */
 found:
	m = sc->sc_xmit[i].mbuf;
	sc->sc_xmit[i].mbuf = NULL;
	sc->sc_xmit[i].phys = ~0;
	if (i == sc->sc_xmit_h) { /* should be */
		sc->sc_xmit_h = (++i == sc->sc_n_xmit) ? 0 : i;
	}
#if DEBUG
	else
		sc->bxh++;
#endif
	m_freem(m);
	ifp->if_opackets++;

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

	ifp->if_flags &= ~IFF_OACTIVE;
	if_schedule_deferred_start(ifp);
#if DEBUG
	sc->it = 1;
#endif
}

/*
 * Setup output on interface.
 * Get another datagram to send off of the interface queue, and map it to the
 * interface before starting the output.
 * Called only at splnet or interrupt level.
 */
void
enic_start(struct ifnet *ifp)
{
	struct enic_softc *sc = ifp->if_softc;
	struct mbuf *m;
	int len, ix, s;
	paddr_t phys;

#if DEBUG
	sc->it = 0;
#endif

#if 0
	printf("enic_start(%x)\n", ifp->if_flags);
#endif

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

	s = splnet();	/* I know, I dont trust people.. */

	ix = sc->sc_xmit_h;
	for (;;) {

		/* Anything to do? */
		IFQ_POLL(&ifp->if_snd, m);
		if (m == NULL)
			break;

		/* find a spot, if any */
		for (; ix < sc->sc_n_xmit; ix++)
			if (sc->sc_xmit[ix].mbuf == NULL)
				goto found;
		for (ix = 0; ix < sc->sc_xmit_h; ix++)
			if (sc->sc_xmit[ix].mbuf == NULL)
				goto found;
		/* oh well */
		ifp->if_flags |= IFF_OACTIVE;
#if DEBUG
		sc->tfull++;
#endif
		break;

 found:
		IFQ_DEQUEUE(&ifp->if_snd, m);
		if (m == NULL)
			break;

		/*
		 * If BPF is listening on this interface, let it see the packet
		 * before we commit it to the wire.
		 */
		bpf_mtap(ifp, m, BPF_D_OUT);

		/*
		 * Copy the mbuf chain into a contiguous transmit buffer.
		 */
		len = enic_put(sc, &m);
		if (len == 0)
			break;	/* sanity */
		if (len > (ETHERMTU + sizeof(struct ether_header))) {
			printf("enic? tlen %d > %d\n",
			    len, ETHERMTU + sizeof(struct ether_header));
			len = ETHERMTU + sizeof(struct ether_header);
		}

		ifp->if_timer = 5;

		/*
		 * Remember and post the buffer
		 */
		phys = kvtophys((vaddr_t)m->m_data);
		sc->sc_xmit[ix].mbuf = m;
		sc->sc_xmit[ix].phys = phys;

		sc->sc_no_td++;

		tpostone(phys, len);

		if (sc->sc_regs->Control & EC_IF_FULL) {
			ifp->if_flags |= IFF_OACTIVE;
#if DEBUG
			sc->tfull2++;
#endif
			break;
		}

		ix++;
	}

	splx(s);
}

int enic_put(struct enic_softc *sc, struct mbuf **pm)
{
	struct mbuf *n, *m = *pm, *mm;
	int len, tlen = 0, xlen = m->m_pkthdr.len;
	uint8_t *cp;

#if 0
	/* drop garbage */
	tlen = xlen;
	for (; m; m = n) {
		len = m->m_len;
		if (len == 0) {
			n = m_free(m);
			if (m == *pm)
				*pm = n;
			continue;
		}
		tlen -= len;
		KASSERT(m != m->m_next);
		n = m->m_next;
		if (tlen <= 0)
			break;
	}

	/*
	 * We might be done:
	 * (a) empty chain (b) only one segment (c) bad chain
	 */
	if (((m = *pm) == NULL) || (tlen > 0))
		xlen = 0;
#endif

	if ((xlen == 0) || (xlen <= m->m_len)) {
#if DEBUG
		sc->xhit++;
#endif
		return xlen;
	}

	/* Nope, true chain. Copy to contig :-(( */
	tlen = xlen;
	MGETHDR(n, M_NOWAIT, MT_DATA);
	if (n == NULL)
		goto Bad;
	m_set_rcvif(n, &sc->sc_ethercom.ec_if);
	n->m_pkthdr.len = tlen;

	MCLGET(n, M_NOWAIT);
	if ((n->m_flags & M_EXT) == 0) {
		m_freem(n);
		goto Bad;
	}

	n->m_len = tlen;
	cp = mtod(n, uint8_t *);
	for (; m && tlen; m = mm) {

		len = m->m_len;
		if (len > tlen)
			len = tlen;
		if (len)
			memcpy(cp, mtod(m, void *), len);

		cp += len;
		tlen -= len;
		mm = m_free(m);

	}

	*pm = n;
#if DEBUG
	sc->xmiss++;
#endif
	return (xlen);

 Bad:
	printf("enic_put: no mem?\n");
	m_freem(m);
	return 0;
}