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
 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
/* $NetBSD: if_aumac.c,v 1.47 2019/05/28 07:41:47 msaitoh Exp $ */

/*
 * Copyright (c) 2001 Wasabi Systems, Inc.
 * All rights reserved.
 *
 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
 *
 * 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 for the NetBSD Project by
 *	Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC
 * 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.
 */

/*
 * Device driver for Alchemy Semiconductor Au1x00 Ethernet Media
 * Access Controller.
 *
 * TODO:
 *
 *	Better Rx buffer management; we want to get new Rx buffers
 *	to the chip more quickly than we currently do.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_aumac.c,v 1.47 2019/05/28 07:41:47 msaitoh Exp $");



#include <sys/param.h>
#include <sys/bus.h>
#include <sys/callout.h>
#include <sys/device.h>
#include <sys/endian.h>
#include <sys/errno.h>
#include <sys/intr.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/socket.h>

#include <uvm/uvm.h>		/* for PAGE_SIZE */

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

#include <net/bpf.h>
#include <sys/rndsource.h>

#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>

#include <mips/alchemy/include/aureg.h>
#include <mips/alchemy/include/auvar.h>
#include <mips/alchemy/include/aubusvar.h>
#include <mips/alchemy/dev/if_aumacreg.h>

/*
 * The Au1X00 MAC has 4 transmit and receive descriptors.  Each buffer
 * must consist of a single DMA segment, and must be aligned to a 2K
 * boundary.  Therefore, this driver does not perform DMA directly
 * to/from mbufs.  Instead, we copy the data to/from buffers allocated
 * at device attach time.
 *
 * We also skip the bus_dma dance.  The MAC is built in to the CPU, so
 * there's little point in not making assumptions based on the CPU type.
 * We also program the Au1X00 cache to be DMA coherent, so the buffers
 * are accessed via KSEG0 addresses.
 */
#define	AUMAC_NTXDESC		4
#define	AUMAC_NTXDESC_MASK	(AUMAC_NTXDESC - 1)

#define	AUMAC_NRXDESC		4
#define	AUMAC_NRXDESC_MASK	(AUMAC_NRXDESC - 1)

#define	AUMAC_NEXTTX(x)		(((x) + 1) & AUMAC_NTXDESC_MASK)
#define	AUMAC_NEXTRX(x)		(((x) + 1) & AUMAC_NRXDESC_MASK)

#define	AUMAC_TXBUF_OFFSET	0
#define	AUMAC_RXBUF_OFFSET	(MAC_BUFLEN * AUMAC_NTXDESC)
#define	AUMAC_BUFSIZE		(MAC_BUFLEN * (AUMAC_NTXDESC + AUMAC_NRXDESC))

struct aumac_buf {
	vaddr_t buf_vaddr;		/* virtual address of buffer */
	bus_addr_t buf_paddr;		/* DMA address of buffer */
};

/*
 * Software state per device.
 */
struct aumac_softc {
	device_t sc_dev;		/* generic device information */
	bus_space_tag_t sc_st;		/* bus space tag */
	bus_space_handle_t sc_mac_sh;	/* MAC space handle */
	bus_space_handle_t sc_macen_sh;	/* MAC enable space handle */
	bus_space_handle_t sc_dma_sh;	/* DMA space handle */
	struct ethercom sc_ethercom;	/* Ethernet common data */
	void *sc_sdhook;		/* shutdown hook */

	int sc_irq;
	void *sc_ih;			/* interrupt cookie */

	struct mii_data sc_mii;		/* MII/media information */

	struct callout sc_tick_ch;	/* tick callout */

	/* Transmit and receive buffers */
	struct aumac_buf sc_txbufs[AUMAC_NTXDESC];
	struct aumac_buf sc_rxbufs[AUMAC_NRXDESC];
	void *sc_bufaddr;

	int sc_txfree;			/* number of free Tx descriptors */
	int sc_txnext;			/* next Tx descriptor to use */
	int sc_txdirty;			/* first dirty Tx descriptor */

	int sc_rxptr;			/* next ready Rx descriptor */

	krndsource_t rnd_source;

#ifdef AUMAC_EVENT_COUNTERS
	struct evcnt sc_ev_txstall;	/* Tx stalled */
	struct evcnt sc_ev_rxstall;	/* Rx stalled */
	struct evcnt sc_ev_txintr;	/* Tx interrupts */
	struct evcnt sc_ev_rxintr;	/* Rx interrupts */
#endif

	uint32_t sc_control;		/* MAC_CONTROL contents */
	uint32_t sc_flowctrl;		/* MAC_FLOWCTRL contents */
};

#ifdef AUMAC_EVENT_COUNTERS
#define	AUMAC_EVCNT_INCR(ev)	(ev)->ev_count++
#else
#define	AUMAC_EVCNT_INCR(ev)	/* nothing */
#endif

#define	AUMAC_INIT_RXDESC(sc, x)					\
do {									\
	bus_space_write_4((sc)->sc_st, (sc)->sc_dma_sh,			\
	    MACDMA_RX_STAT((x)), 0);					\
	bus_space_write_4((sc)->sc_st, (sc)->sc_dma_sh,			\
	    MACDMA_RX_ADDR((x)),					\
	    (sc)->sc_rxbufs[(x)].buf_paddr | RX_ADDR_EN);		\
} while (/*CONSTCOND*/0)

static void	aumac_start(struct ifnet *);
static void	aumac_watchdog(struct ifnet *);
static int	aumac_ioctl(struct ifnet *, u_long, void *);
static int	aumac_init(struct ifnet *);
static void	aumac_stop(struct ifnet *, int);

static void	aumac_shutdown(void *);

static void	aumac_tick(void *);

static void	aumac_set_filter(struct aumac_softc *);

static void	aumac_powerup(struct aumac_softc *);
static void	aumac_powerdown(struct aumac_softc *);

static int	aumac_intr(void *);
static int	aumac_txintr(struct aumac_softc *);
static int	aumac_rxintr(struct aumac_softc *);

static int	aumac_mii_readreg(device_t, int, int, uint16_t *);
static int	aumac_mii_writereg(device_t, int, int, uint16_t);
static void	aumac_mii_statchg(struct ifnet *);
static int	aumac_mii_wait(struct aumac_softc *, const char *);

static int	aumac_match(device_t, struct cfdata *, void *);
static void	aumac_attach(device_t, device_t, void *);

int	aumac_copy_small = 0;

CFATTACH_DECL_NEW(aumac, sizeof(struct aumac_softc),
    aumac_match, aumac_attach, NULL, NULL);

static int
aumac_match(device_t parent, struct cfdata *cf, void *aux)
{
	struct aubus_attach_args *aa = aux;

	if (strcmp(aa->aa_name, cf->cf_name) == 0)
		return 1;

	return 0;
}

static void
aumac_attach(device_t parent, device_t self, void *aux)
{
	const uint8_t *enaddr;
	prop_data_t ea;
	struct aumac_softc *sc = device_private(self);
	struct aubus_attach_args *aa = aux;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct mii_data * const mii = &sc->sc_mii;
	struct pglist pglist;
	paddr_t bufaddr;
	vaddr_t vbufaddr;
	int i;

	callout_init(&sc->sc_tick_ch, 0);

	aprint_normal(": Au1X00 10/100 Ethernet\n");
	aprint_naive("\n");

	sc->sc_dev = self;
	sc->sc_st = aa->aa_st;

	/* Get the MAC address. */
	ea = prop_dictionary_get(device_properties(self), "mac-address");
	if (ea == NULL) {
		aprint_error_dev(self, "unable to get mac-addr property\n");
		return;
	}
	KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
	KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
	enaddr = prop_data_data_nocopy(ea);

	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(enaddr));

	/* Map the device. */
	if (bus_space_map(sc->sc_st, aa->aa_addrs[AA_MAC_BASE],
	    MACx_SIZE, 0, &sc->sc_mac_sh) != 0) {
		aprint_error_dev(self, "unable to map MAC registers\n");
		return;
	}
	if (bus_space_map(sc->sc_st, aa->aa_addrs[AA_MAC_ENABLE],
	    MACENx_SIZE, 0, &sc->sc_macen_sh) != 0) {
		aprint_error_dev(self, "unable to map MACEN registers\n");
		return;
	}
	if (bus_space_map(sc->sc_st, aa->aa_addrs[AA_MAC_DMA_BASE],
	    MACx_DMA_SIZE, 0, &sc->sc_dma_sh) != 0) {
		aprint_error_dev(self, "unable to map MACDMA registers\n");
		return;
	}

	/* Make sure the MAC is powered off. */
	aumac_powerdown(sc);

	/* Hook up the interrupt handler. */
	sc->sc_ih = au_intr_establish(aa->aa_irq[0], 1, IPL_NET, IST_LEVEL,
	    aumac_intr, sc);
	if (sc->sc_ih == NULL) {
		aprint_error_dev(self,
		    "unable to register interrupt handler\n");
		return;
	}
	sc->sc_irq = aa->aa_irq[0];
	au_intr_disable(sc->sc_irq);

	/*
	 * Allocate space for the transmit and receive buffers.
	 */
	if (uvm_pglistalloc(AUMAC_BUFSIZE, 0, ctob(physmem), PAGE_SIZE, 0,
	    &pglist, 1, 0))
		return;

	bufaddr = VM_PAGE_TO_PHYS(TAILQ_FIRST(&pglist));
	vbufaddr = MIPS_PHYS_TO_KSEG0(bufaddr);

	for (i = 0; i < AUMAC_NTXDESC; i++) {
		int offset = AUMAC_TXBUF_OFFSET + (i * MAC_BUFLEN);

		sc->sc_txbufs[i].buf_vaddr = vbufaddr + offset;
		sc->sc_txbufs[i].buf_paddr = bufaddr + offset;
	}

	for (i = 0; i < AUMAC_NRXDESC; i++) {
		int offset = AUMAC_RXBUF_OFFSET + (i * MAC_BUFLEN);

		sc->sc_rxbufs[i].buf_vaddr = vbufaddr + offset;
		sc->sc_rxbufs[i].buf_paddr = bufaddr + offset;
	}

	/*
	 * Power up the MAC before accessing any MAC registers (including
	 * MII configuration.
	 */
	aumac_powerup(sc);

	/*
	 * Initialize the media structures and probe the MII.
	 */
	mii->mii_ifp = ifp;
	mii->mii_readreg = aumac_mii_readreg;
	mii->mii_writereg = aumac_mii_writereg;
	mii->mii_statchg = aumac_mii_statchg;
	sc->sc_ethercom.ec_mii = mii;
	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);

	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY,
	    MII_OFFSET_ANY, 0);

	if (LIST_FIRST(&mii->mii_phys) == NULL) {
		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE,
		    0, NULL);
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
	} else
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);

	strcpy(ifp->if_xname, device_xname(self));
	ifp->if_softc = sc;
	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
	ifp->if_ioctl = aumac_ioctl;
	ifp->if_start = aumac_start;
	ifp->if_watchdog = aumac_watchdog;
	ifp->if_init = aumac_init;
	ifp->if_stop = aumac_stop;
	IFQ_SET_READY(&ifp->if_snd);

	/* Attach the interface. */
	if_attach(ifp);
	if_deferred_start_init(ifp, NULL);
	ether_ifattach(ifp, enaddr);

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

#ifdef AUMAC_EVENT_COUNTERS
	evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
	    NULL, device_xname(self), "txstall");
	evcnt_attach_dynamic(&sc->sc_ev_rxstall, EVCNT_TYPE_MISC,
	    NULL, device_xname(self), "rxstall");
	evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_MISC,
	    NULL, device_xname(self), "txintr");
	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_MISC,
	    NULL, device_xname(self), "rxintr");
#endif

	/* Make sure the interface is shutdown during reboot. */
	sc->sc_sdhook = shutdownhook_establish(aumac_shutdown, sc);
	if (sc->sc_sdhook == NULL)
		aprint_error_dev(self,
		    "WARNING: unable to establish shutdown hook\n");
	return;
}

/*
 * aumac_shutdown:
 *
 *	Make sure the interface is stopped at reboot time.
 */
static void
aumac_shutdown(void *arg)
{
	struct aumac_softc *sc = arg;

	aumac_stop(&sc->sc_ethercom.ec_if, 1);

	/*
	 * XXX aumac_stop leaves device powered up at the moment
	 * XXX but this still isn't enough to keep yamon happy... :-(
	 */
	bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0, 0);
}

/*
 * aumac_start:		[ifnet interface function]
 *
 *	Start packet transmission on the interface.
 */
static void
aumac_start(struct ifnet *ifp)
{
	struct aumac_softc *sc = ifp->if_softc;
	struct mbuf *m;
	int nexttx;

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

	/*
	 * Loop through the send queue, setting up transmit descriptors
	 * unitl we drain the queue, or use up all available transmit
	 * descriptors.
	 */
	for (;;) {
		/* Grab a packet off the queue. */
		IFQ_POLL(&ifp->if_snd, m);
		if (m == NULL)
			return;

		/* Get a spare descriptor. */
		if (sc->sc_txfree == 0) {
			/* No more slots left; notify upper layer. */
			ifp->if_flags |= IFF_OACTIVE;
			AUMAC_EVCNT_INCR(&sc->sc_ev_txstall);
			return;
		}
		nexttx = sc->sc_txnext;

		IFQ_DEQUEUE(&ifp->if_snd, m);

		/*
		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
		 */

		m_copydata(m, 0, m->m_pkthdr.len,
		    (void *)sc->sc_txbufs[nexttx].buf_vaddr);

		/* Zero out the remainder of any short packets. */
		if (m->m_pkthdr.len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
			memset((char *)sc->sc_txbufs[nexttx].buf_vaddr +
			    m->m_pkthdr.len, 0,
			    ETHER_MIN_LEN - ETHER_CRC_LEN - m->m_pkthdr.len);

		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
		    MACDMA_TX_STAT(nexttx), 0);
		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
		    MACDMA_TX_LEN(nexttx),
		    m->m_pkthdr.len < (ETHER_MIN_LEN - ETHER_CRC_LEN) ?
		    ETHER_MIN_LEN - ETHER_CRC_LEN : m->m_pkthdr.len);
		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
		    MACDMA_TX_ADDR(nexttx),
		    sc->sc_txbufs[nexttx].buf_paddr | TX_ADDR_EN);
		/* XXX - needed??  we should be coherent */
		bus_space_barrier(sc->sc_st, sc->sc_dma_sh, 0 /* XXX */,
		    0 /* XXX */, BUS_SPACE_BARRIER_WRITE);

		/* Advance the Tx pointer. */
		sc->sc_txfree--;
		sc->sc_txnext = AUMAC_NEXTTX(nexttx);

		/* Pass the packet to any BPF listeners. */
		bpf_mtap(ifp, m, BPF_D_OUT);

		m_freem(m);

		/* Set a watchdog timer in case the chip flakes out. */
		ifp->if_timer = 5;
	}
	/* NOTREACHED */
}

/*
 * aumac_watchdog:	[ifnet interface function]
 *
 *	Watchdog timer handler.
 */
static void
aumac_watchdog(struct ifnet *ifp)
{
	struct aumac_softc *sc = ifp->if_softc;

	printf("%s: device timeout\n", device_xname(sc->sc_dev));
	(void) aumac_init(ifp);

	/* Try to get more packets going. */
	aumac_start(ifp);
}

/*
 * aumac_ioctl:		[ifnet interface function]
 *
 *	Handle control requests from the operator.
 */
static int
aumac_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
	struct aumac_softc *sc = ifp->if_softc;
	int s, error;

	s = splnet();

	error = ether_ioctl(ifp, cmd, data);
	if (error == ENETRESET) {
		/*
		 * Multicast list has changed; set the hardware filter
		 * accordingly.
		 */
		if (ifp->if_flags & IFF_RUNNING)
			aumac_set_filter(sc);
		error = 0;
	}

	/* Try to get more packets going. */
	aumac_start(ifp);

	splx(s);
	return error;
}

/*
 * aumac_intr:
 *
 *	Interrupt service routine.
 */
static int
aumac_intr(void *arg)
{
	struct aumac_softc *sc = arg;
	int status;

	/*
	 * There aren't really any interrupt status bits on the
	 * Au1X00 MAC, and each MAC has a dedicated interrupt
	 * in the CPU's built-in interrupt controller.  Just
	 * check for new incoming packets, and then Tx completions
	 * (for status updating).
	 */
	if ((sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING) == 0)
		return 0;

	status = aumac_rxintr(sc);
	status += aumac_txintr(sc);

	rnd_add_uint32(&sc->rnd_source, status);

	return status;
}

/*
 * aumac_txintr:
 *
 *	Helper; handle transmit interrupts.
 */
static int
aumac_txintr(struct aumac_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	uint32_t stat;
	int i;
	int pkts = 0;

	for (i = sc->sc_txdirty; sc->sc_txfree != AUMAC_NTXDESC;
	     i = AUMAC_NEXTTX(i)) {
		if ((bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
		     MACDMA_TX_ADDR(i)) & TX_ADDR_DN) == 0)
			break;
		pkts++;

		/* ACK interrupt. */
		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
		    MACDMA_TX_ADDR(i), 0);

		stat = bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
		    MACDMA_TX_STAT(i));

		if (stat & TX_STAT_FA) {
			/* XXX STATS */
			ifp->if_oerrors++;
		} else
			ifp->if_opackets++;

		if (stat & TX_STAT_EC)
			ifp->if_collisions += 16;
		else
			ifp->if_collisions += TX_STAT_CC(stat);

		sc->sc_txfree++;
		ifp->if_flags &= ~IFF_OACTIVE;

		/* Try to queue more packets. */
		if_schedule_deferred_start(ifp);
	}

	if (pkts)
		AUMAC_EVCNT_INCR(&sc->sc_ev_txintr);

	/* Update the dirty descriptor pointer. */
	sc->sc_txdirty = i;

	/*
	 * If there are no more pending transmissions, cancel the watchdog
	 * timer.
	 */
	if (sc->sc_txfree == AUMAC_NTXDESC)
		ifp->if_timer = 0;

	return pkts;
}

/*
 * aumac_rxintr:
 *
 *	Helper; handle receive interrupts.
 */
static int
aumac_rxintr(struct aumac_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct mbuf *m;
	uint32_t stat;
	int i, len;
	int pkts = 0;

	for (i = sc->sc_rxptr;; i = AUMAC_NEXTRX(i)) {
		if ((bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
		     MACDMA_RX_ADDR(i)) & RX_ADDR_DN) == 0)
			break;
		pkts++;

		stat = bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
		    MACDMA_RX_STAT(i));

#define PRINTERR(str)							\
	do {								\
		error++;						\
		printf("%s: %s\n", device_xname(sc->sc_dev), str);	\
	} while (0)

		if (stat & RX_STAT_ERRS) {
			int error = 0;

#if 0	/*
	 * Missed frames are a semi-frequent occurence with this hardware,
	 * and reporting of them just makes everything run slower and fills
	 * the system log.  Be silent.
	 * 
	 * Additionally, this missed bit indicates an error with the previous
	 * packet, and not with this one!  So PRINTERR is definitely wrong
	 * here.
	 *
	 * These should probably all be converted to evcnt counters anyway.
	 */
			if (stat & RX_STAT_MI)
				PRINTERR("missed frame");
#endif
			if (stat & RX_STAT_UC)
				PRINTERR("unknown control frame");
			if (stat & RX_STAT_LE)
				PRINTERR("short frame");
			if (stat & RX_STAT_CR)
				PRINTERR("CRC error");
			if (stat & RX_STAT_ME)
				PRINTERR("medium error");
			if (stat & RX_STAT_CS)
				PRINTERR("late collision");
			if (stat & RX_STAT_FL)
				PRINTERR("frame too big");
			if (stat & RX_STAT_RF)
				PRINTERR("runt frame (collision)");
			if (stat & RX_STAT_WT)
				PRINTERR("watch dog");
			if (stat & RX_STAT_DB) {
				if (stat & (RX_STAT_CS | RX_STAT_RF |
				    RX_STAT_CR)) {
					if (!error)
						goto pktok;
				} else
					PRINTERR("dribbling bit");
			}
#undef PRINTERR
			ifp->if_ierrors++;

 dropit:
			/* reuse the current descriptor */
			AUMAC_INIT_RXDESC(sc, i);
			continue;
		}
 pktok:
		len = RX_STAT_L(stat);

		/*
		 * The Au1X00 MAC includes the CRC with every packet;
		 * trim it off here.
		 */
		len -= ETHER_CRC_LEN;

		/*
		 * Truncate the packet if it's too big to fit in
		 * a single mbuf cluster.
		 */
		if (len > MCLBYTES - 2)
			len = MCLBYTES - 2;

		MGETHDR(m, M_DONTWAIT, MT_DATA);
		if (m == NULL) {
			printf("%s: unable to allocate Rx mbuf\n",
			    device_xname(sc->sc_dev));
			goto dropit;
		}
		if (len > MHLEN - 2) {
			MCLGET(m, M_DONTWAIT);
			if ((m->m_flags & M_EXT) == 0) {
				printf("%s: unable to allocate Rx cluster\n",
				    device_xname(sc->sc_dev));
				m_freem(m);
				goto dropit;
			}
		}

		m->m_data += 2;		/* align payload */
		memcpy(mtod(m, void *),
		    (void *)sc->sc_rxbufs[i].buf_vaddr, len);
		AUMAC_INIT_RXDESC(sc, i);

		m_set_rcvif(m, ifp);
		m->m_pkthdr.len = m->m_len = len;

		/* Pass it on. */
		if_percpuq_enqueue(ifp->if_percpuq, m);
	}
	if (pkts)
		AUMAC_EVCNT_INCR(&sc->sc_ev_rxintr);
	if (pkts == AUMAC_NRXDESC)
		AUMAC_EVCNT_INCR(&sc->sc_ev_rxstall);

	/* Update the receive pointer. */
	sc->sc_rxptr = i;

	return pkts;
}

/*
 * aumac_tick:
 *
 *	One second timer, used to tick the MII.
 */
static void
aumac_tick(void *arg)
{
	struct aumac_softc *sc = arg;
	int s;

	s = splnet();
	mii_tick(&sc->sc_mii);
	splx(s);

	callout_reset(&sc->sc_tick_ch, hz, aumac_tick, sc);
}

/*
 * aumac_init:		[ifnet interface function]
 *
 *	Initialize the interface.  Must be called at splnet().
 */
static int
aumac_init(struct ifnet *ifp)
{
	struct aumac_softc *sc = ifp->if_softc;
	int i, error = 0;

	/* Cancel any pending I/O, reset MAC. */
	aumac_stop(ifp, 0);

	/* Set up the transmit ring. */
	for (i = 0; i < AUMAC_NTXDESC; i++) {
		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
		    MACDMA_TX_STAT(i), 0);
		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
		    MACDMA_TX_LEN(i), 0);
		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
		    MACDMA_TX_ADDR(i), sc->sc_txbufs[i].buf_paddr);
	}
	sc->sc_txfree = AUMAC_NTXDESC;
	sc->sc_txnext = TX_ADDR_CB(bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
	    MACDMA_TX_ADDR(0)));
	sc->sc_txdirty = sc->sc_txnext;

	/* Set up the receive ring. */
	for (i = 0; i < AUMAC_NRXDESC; i++)
			AUMAC_INIT_RXDESC(sc, i);
	sc->sc_rxptr = RX_ADDR_CB(bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
	    MACDMA_RX_ADDR(0)));

	/*
	 * Power up the MAC.
	 */
	aumac_powerup(sc);

	sc->sc_control |= CONTROL_DO | CONTROL_TE | CONTROL_RE;
#if _BYTE_ORDER == _BIG_ENDIAN
	sc->sc_control |= CONTROL_EM;
#endif

	/* Set the media. */
	if ((error = ether_mediachange(ifp)) != 0)
		goto out;

	/*
	 * Set the receive filter.  This will actually start the transmit
	 * and receive processes.
	 */
	aumac_set_filter(sc);

	/* Start the one second clock. */
	callout_reset(&sc->sc_tick_ch, hz, aumac_tick, sc);

	/* ...all done! */
	ifp->if_flags |= IFF_RUNNING;
	ifp->if_flags &= ~IFF_OACTIVE;

	au_intr_enable(sc->sc_irq);
out:
	if (error)
		printf("%s: interface not running\n", device_xname(sc->sc_dev));
	return error;
}

/*
 * aumac_stop:		[ifnet interface function]
 *
 *	Stop transmission on the interface.
 */
static void
aumac_stop(struct ifnet *ifp, int disable)
{
	struct aumac_softc *sc = ifp->if_softc;

	/* Stop the one-second clock. */
	callout_stop(&sc->sc_tick_ch);

	/* Down the MII. */
	mii_down(&sc->sc_mii);

	/* Stop the transmit and receive processes. */
	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL, 0);

	/* Power down/reset the MAC. */
	aumac_powerdown(sc);

	au_intr_disable(sc->sc_irq);

	/* Mark the interface as down and cancel the watchdog timer. */
	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
	ifp->if_timer = 0;
}

/*
 * aumac_powerdown:
 *
 *	Power down the MAC.
 */
static void
aumac_powerdown(struct aumac_softc *sc)
{

	/* Disable the MAC clocks, and place the device in reset. */
	// bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0, MACEN_JP);

	// delay(10000);
}

/*
 * aumac_powerup:
 *
 *	Bring the device out of reset.
 */
static void
aumac_powerup(struct aumac_softc *sc)
{

	/* Enable clocks to the MAC. */
	bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0, MACEN_JP | MACEN_CE);

	/* Enable MAC, coherent transactions, pass only valid frames. */
	bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0,
	    MACEN_E2 | MACEN_E1 | MACEN_E0 | MACEN_CE);

	delay(20000);
}

/*
 * aumac_set_filter:
 *
 *	Set up the receive filter.
 */
static void
aumac_set_filter(struct aumac_softc *sc)
{
	struct ethercom *ec = &sc->sc_ethercom;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct ether_multi *enm;
	struct ether_multistep step;
	const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
	uint32_t mchash[2], crc;

	sc->sc_control &= ~(CONTROL_PM | CONTROL_PR);

	/* Stop the receiver. */
	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL,
	    sc->sc_control & ~CONTROL_RE);

	if (ifp->if_flags & IFF_PROMISC) {
		sc->sc_control |= CONTROL_PR;
		goto allmulti;
	}

	/* Set the station address. */
	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_ADDRHIGH,
	    enaddr[4] | (enaddr[5] << 8));
	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_ADDRLOW,
	    enaddr[0] | (enaddr[1] << 8) | (enaddr[2] << 16) |
	    (enaddr[3] << 24));

	sc->sc_control |= CONTROL_HP;

	mchash[0] = mchash[1] = 0;

	/*
	 * Set up the 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 multicast hash table.
	 * The high order bits select the word, while the rest of the bits
	 * select the bit within the word.
	 */
	ETHER_LOCK(ec);
	ETHER_FIRST_MULTI(step, ec, enm);
	while (enm != NULL) {
		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
			/*
			 * 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 large enough to require all bits set.)
			 */
			ETHER_UNLOCK(ec);
			goto allmulti;
		}

		crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);

		/* Just want the 6 most significant bits. */
		crc >>= 26;

		/* Set the corresponding bit in the filter. */
		mchash[crc >> 5] |= 1U << (crc & 0x1f);

		ETHER_NEXT_MULTI(step, enm);
	}
	ETHER_UNLOCK(ec);

	ifp->if_flags &= ~IFF_ALLMULTI;

	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_HASHHIGH,
	    mchash[1]);
	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_HASHLOW,
	    mchash[0]);

	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL,
	    sc->sc_control);
	return;

 allmulti:
	sc->sc_control |= CONTROL_PM;
	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL,
	    sc->sc_control);
}

/*
 * aumac_mii_wait:
 *
 *	Wait for the MII interface to not be busy.
 */
static int
aumac_mii_wait(struct aumac_softc *sc, const char *msg)
{
	int i;

	for (i = 0; i < 10000; i++) {
		if ((bus_space_read_4(sc->sc_st, sc->sc_mac_sh,
		     MAC_MIICTRL) & MIICTRL_MB) == 0)
			return 0;
		delay(10);
	}

	printf("%s: MII failed to %s\n", device_xname(sc->sc_dev), msg);
	return ETIMEDOUT;
}

/*
 * aumac_mii_readreg:	[mii interface function]
 *
 *	Read a PHY register on the MII.
 */
static int
aumac_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
{
	struct aumac_softc *sc = device_private(self);
	int rv;

	if ((rv = aumac_mii_wait(sc, "become ready")) != 0)
		return rv;

	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_MIICTRL,
	    MIICTRL_PHYADDR(phy) | MIICTRL_MIIREG(reg));

	if ((rv = aumac_mii_wait(sc, "complete")) != 0)
		return rv;

	*val = bus_space_read_4(sc->sc_st, sc->sc_mac_sh, MAC_MIIDATA)
	    & MIIDATA_MASK;
	return 0;
}

/*
 * aumac_mii_writereg:	[mii interface function]
 *
 *	Write a PHY register on the MII.
 */
static int
aumac_mii_writereg(device_t self, int phy, int reg, uint16_t val)
{
	struct aumac_softc *sc = device_private(self);
	int rv;

	if ((rv = aumac_mii_wait(sc, "become ready")) != 0)
		return rv;

	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_MIIDATA, val);
	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_MIICTRL,
	    MIICTRL_PHYADDR(phy) | MIICTRL_MIIREG(reg) | MIICTRL_MW);

	return aumac_mii_wait(sc, "complete");
}

/*
 * aumac_mii_statchg:	[mii interface function]
 *
 *	Callback from MII layer when media changes.
 */
static void
aumac_mii_statchg(struct ifnet *ifp)
{
	struct aumac_softc *sc = ifp->if_softc;

	if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
		sc->sc_control |= CONTROL_F;
	else
		sc->sc_control &= ~CONTROL_F;

	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL,
	    sc->sc_control);
}