Home | History | Annotate | Line # | Download | only in dev
if_temac.c revision 1.4
      1 /* 	$NetBSD: if_temac.c,v 1.4 2008/02/12 18:03:43 dyoung Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2006 Jachym Holecek
      5  * All rights reserved.
      6  *
      7  * Written for DFC Design, s.r.o.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  *
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  *
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Driver for Xilinx LocalLink TEMAC as wired on the GSRD platform.
     34  *
     35  * TODO:
     36  * 	- Optimize
     37  * 	- Checksum offload
     38  * 	- Address filters
     39  * 	- Support jumbo frames
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: if_temac.c,v 1.4 2008/02/12 18:03:43 dyoung Exp $");
     44 
     45 #include "bpfilter.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/kernel.h>
     51 #include <sys/socket.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/device.h>
     54 
     55 #include <uvm/uvm_extern.h>
     56 
     57 #include <net/if.h>
     58 #include <net/if_dl.h>
     59 #include <net/if_media.h>
     60 #include <net/if_ether.h>
     61 
     62 #if NBPFILTER > 0
     63 #include <net/bpf.h>
     64 #endif
     65 
     66 #include <machine/bus.h>
     67 
     68 #include <evbppc/virtex/idcr.h>
     69 #include <evbppc/virtex/dev/xcvbusvar.h>
     70 #include <evbppc/virtex/dev/cdmacreg.h>
     71 #include <evbppc/virtex/dev/temacreg.h>
     72 #include <evbppc/virtex/dev/temacvar.h>
     73 
     74 #include <dev/mii/miivar.h>
     75 
     76 
     77 /* This is outside of TEMAC's DCR window, we have to hardcode it... */
     78 #define DCR_ETH_BASE 		0x0030
     79 
     80 #define	TEMAC_REGDEBUG 		0
     81 #define	TEMAC_RXDEBUG 		0
     82 #define	TEMAC_TXDEBUG 		0
     83 
     84 #if TEMAC_RXDEBUG > 0 || TEMAC_TXDEBUG > 0
     85 #define	TEMAC_DEBUG 		1
     86 #else
     87 #define	TEMAC_DEBUG 		0
     88 #endif
     89 
     90 #if TEMAC_REGDEBUG > 0
     91 #define	TRACEREG(arg) 		printf arg
     92 #else
     93 #define	TRACEREG(arg) 		/* nop */
     94 #endif
     95 
     96 /* DMA control chains take up one (16KB) page. */
     97 #define TEMAC_NTXDESC 		256
     98 #define TEMAC_NRXDESC 		256
     99 
    100 #define TEMAC_TXQLEN 		64 	/* Software Tx queue length */
    101 #define TEMAC_NTXSEG 		16 	/* Maximum Tx segments per packet */
    102 
    103 #define TEMAC_NRXSEG 		1 	/* Maximum Rx segments per packet */
    104 #define TEMAC_RXPERIOD 		1 	/* Interrupt every N descriptors. */
    105 #define TEMAC_RXTIMO_HZ 	100 	/* Rx reaper frequency */
    106 
    107 /* Next Tx descriptor and descriptor's offset WRT sc_cdaddr. */
    108 #define TEMAC_TXSINC(n, i) 	(((n) + TEMAC_TXQLEN + (i)) % TEMAC_TXQLEN)
    109 #define TEMAC_TXINC(n, i) 	(((n) + TEMAC_NTXDESC + (i)) % TEMAC_NTXDESC)
    110 
    111 #define TEMAC_TXSNEXT(n) 	TEMAC_TXSINC((n), 1)
    112 #define TEMAC_TXNEXT(n) 	TEMAC_TXINC((n), 1)
    113 #define TEMAC_TXDOFF(n) 	(offsetof(struct temac_control, cd_txdesc) + \
    114 				 (n) * sizeof(struct cdmac_descr))
    115 
    116 /* Next Rx descriptor and descriptor's offset WRT sc_cdaddr. */
    117 #define TEMAC_RXINC(n, i) 	(((n) + TEMAC_NRXDESC + (i)) % TEMAC_NRXDESC)
    118 #define TEMAC_RXNEXT(n) 	TEMAC_RXINC((n), 1)
    119 #define TEMAC_RXDOFF(n) 	(offsetof(struct temac_control, cd_rxdesc) + \
    120 				 (n) * sizeof(struct cdmac_descr))
    121 #define TEMAC_ISINTR(i) 	(((i) % TEMAC_RXPERIOD) == 0)
    122 #define TEMAC_ISLAST(i) 	((i) == (TEMAC_NRXDESC - 1))
    123 
    124 
    125 struct temac_control {
    126 	struct cdmac_descr 	cd_txdesc[TEMAC_NTXDESC];
    127 	struct cdmac_descr 	cd_rxdesc[TEMAC_NRXDESC];
    128 };
    129 
    130 struct temac_txsoft {
    131 	bus_dmamap_t 		txs_dmap;
    132 	struct mbuf 		*txs_mbuf;
    133 	int 			txs_last;
    134 };
    135 
    136 struct temac_rxsoft {
    137 	bus_dmamap_t 		rxs_dmap;
    138 	struct mbuf 		*rxs_mbuf;
    139 };
    140 
    141 struct temac_softc {
    142 	struct device 		sc_dev;
    143 	struct ethercom 	sc_ec;
    144 #define sc_if 			sc_ec.ec_if
    145 
    146 	/* Peripheral registers */
    147 	bus_space_tag_t 	sc_iot;
    148 	bus_space_handle_t 	sc_ioh;
    149 
    150 	/* CDMAC channel registers */
    151 	bus_space_tag_t 	sc_dma_rxt;
    152 	bus_space_handle_t 	sc_dma_rxh; 	/* Rx channel */
    153 	bus_space_handle_t 	sc_dma_rsh; 	/* Rx status */
    154 
    155 	bus_space_tag_t 	sc_dma_txt;
    156 	bus_space_handle_t 	sc_dma_txh; 	/* Tx channel */
    157 	bus_space_handle_t 	sc_dma_tsh; 	/* Tx status */
    158 
    159 	struct temac_txsoft 	sc_txsoft[TEMAC_TXQLEN];
    160 	struct temac_rxsoft 	sc_rxsoft[TEMAC_NRXDESC];
    161 
    162 	struct callout 		sc_rx_timo;
    163 	struct callout 		sc_mii_tick;
    164 	struct mii_data 	sc_mii;
    165 
    166 	bus_dmamap_t 		sc_control_dmap;
    167 #define sc_cdaddr 		sc_control_dmap->dm_segs[0].ds_addr
    168 
    169 	struct temac_control 	*sc_control_data;
    170 #define sc_rxdescs 		sc_control_data->cd_rxdesc
    171 #define sc_txdescs 		sc_control_data->cd_txdesc
    172 
    173 	int 			sc_txbusy;
    174 
    175 	int 			sc_txfree;
    176 	int 			sc_txcur;
    177 	int 			sc_txreap;
    178 
    179 	int 			sc_rxreap;
    180 
    181 	int 			sc_txsfree;
    182 	int 			sc_txscur;
    183 	int 			sc_txsreap;
    184 
    185 	int 			sc_dead; 	/* Rx/Tx DMA error (fatal) */
    186 	int 			sc_rx_drained;
    187 
    188 	int 			sc_rx_chan;
    189 	int 			sc_tx_chan;
    190 
    191 	void 			*sc_sdhook;
    192 	void 			*sc_rx_ih;
    193 	void 			*sc_tx_ih;
    194 
    195 	bus_dma_tag_t 		sc_dmat;
    196 };
    197 
    198 /* Device interface. */
    199 static void 	temac_attach(struct device *, struct device *, void *);
    200 
    201 /* Ifnet interface. */
    202 static int 	temac_init(struct ifnet *);
    203 static int 	temac_ioctl(struct ifnet *, u_long, void *);
    204 static void 	temac_start(struct ifnet *);
    205 static void 	temac_stop(struct ifnet *, int);
    206 
    207 /* Media management. */
    208 static int	temac_mii_readreg(struct device *, int, int);
    209 static void	temac_mii_statchg(struct device *);
    210 static void	temac_mii_tick(void *);
    211 static void	temac_mii_writereg(struct device *, int, int, int);
    212 
    213 /* Indirect hooks. */
    214 static void 	temac_shutdown(void *);
    215 static void 	temac_rx_intr(void *);
    216 static void 	temac_tx_intr(void *);
    217 
    218 /* Tools. */
    219 static inline void 	temac_rxcdsync(struct temac_softc *, int, int, int);
    220 static inline void 	temac_txcdsync(struct temac_softc *, int, int, int);
    221 static void 		temac_txreap(struct temac_softc *);
    222 static void 		temac_rxreap(struct temac_softc *);
    223 static int 		temac_rxalloc(struct temac_softc *, int, int);
    224 static void 		temac_rxtimo(void *);
    225 static void 		temac_rxdrain(struct temac_softc *);
    226 static void 		temac_reset(struct temac_softc *);
    227 static void 		temac_txkick(struct temac_softc *);
    228 
    229 /* Register access. */
    230 static inline void 	gmi_write_8(uint32_t, uint32_t, uint32_t);
    231 static inline void 	gmi_write_4(uint32_t, uint32_t);
    232 static inline void 	gmi_read_8(uint32_t, uint32_t *, uint32_t *);
    233 static inline uint32_t 	gmi_read_4(uint32_t);
    234 static inline void 	hif_wait_stat(uint32_t);
    235 
    236 #define cdmac_rx_stat(sc) \
    237     bus_space_read_4((sc)->sc_dma_rxt, (sc)->sc_dma_rsh, 0 /* XXX hack */)
    238 
    239 #define cdmac_rx_reset(sc) \
    240     bus_space_write_4((sc)->sc_dma_rxt, (sc)->sc_dma_rsh, 0, CDMAC_STAT_RESET)
    241 
    242 #define cdmac_rx_start(sc, val) \
    243     bus_space_write_4((sc)->sc_dma_rxt, (sc)->sc_dma_rxh, CDMAC_CURDESC, (val))
    244 
    245 #define cdmac_tx_stat(sc) \
    246     bus_space_read_4((sc)->sc_dma_txt, (sc)->sc_dma_tsh, 0 /* XXX hack */)
    247 
    248 #define cdmac_tx_reset(sc) \
    249     bus_space_write_4((sc)->sc_dma_txt, (sc)->sc_dma_tsh, 0, CDMAC_STAT_RESET)
    250 
    251 #define cdmac_tx_start(sc, val) \
    252     bus_space_write_4((sc)->sc_dma_txt, (sc)->sc_dma_txh, CDMAC_CURDESC, (val))
    253 
    254 
    255 CFATTACH_DECL(temac, sizeof(struct temac_softc),
    256     xcvbus_child_match, temac_attach, NULL, NULL);
    257 
    258 
    259 /*
    260  * Private bus utilities.
    261  */
    262 static inline void
    263 hif_wait_stat(uint32_t mask)
    264 {
    265 	int 			i = 0;
    266 
    267 	while (mask != (mfidcr(IDCR_HIF_STAT) & mask)) {
    268 		if (i++ > 100) {
    269 			printf("%s: timeout waiting for 0x%08x\n",
    270 			    __func__, mask);
    271 			break;
    272 		}
    273 		delay(5);
    274 	}
    275 
    276 	TRACEREG(("%s: stat %#08x loops %d\n", __func__, mask, i));
    277 }
    278 
    279 static inline void
    280 gmi_write_4(uint32_t addr, uint32_t lo)
    281 {
    282 	mtidcr(IDCR_HIF_ARG0, lo);
    283 	mtidcr(IDCR_HIF_CTRL, (addr & HIF_CTRL_GMIADDR) | HIF_CTRL_WRITE);
    284 	hif_wait_stat(HIF_STAT_GMIWR);
    285 
    286 	TRACEREG(("%s: %#08x <- %#08x\n", __func__, addr, lo));
    287 }
    288 
    289 static inline void
    290 gmi_write_8(uint32_t addr, uint32_t lo, uint32_t hi)
    291 {
    292 	mtidcr(IDCR_HIF_ARG1, hi);
    293 	gmi_write_4(addr, lo);
    294 }
    295 
    296 static inline void
    297 gmi_read_8(uint32_t addr, uint32_t *lo, uint32_t *hi)
    298 {
    299 	*lo = gmi_read_4(addr);
    300 	*hi = mfidcr(IDCR_HIF_ARG1);
    301 }
    302 
    303 static inline uint32_t
    304 gmi_read_4(uint32_t addr)
    305 {
    306 	uint32_t 		res;
    307 
    308 	mtidcr(IDCR_HIF_CTRL, addr & HIF_CTRL_GMIADDR);
    309 	hif_wait_stat(HIF_STAT_GMIRR);
    310 
    311 	res = mfidcr(IDCR_HIF_ARG0);
    312 	TRACEREG(("%s:  %#08x -> %#08x\n", __func__, addr, res));
    313 	return (res);
    314 }
    315 
    316 /*
    317  * Generic device.
    318  */
    319 static void
    320 temac_attach(struct device *parent, struct device *self, void *aux)
    321 {
    322 	struct xcvbus_attach_args *vaa = aux;
    323 	struct ll_dmac 		*rx = vaa->vaa_rx_dmac;
    324 	struct ll_dmac 		*tx = vaa->vaa_tx_dmac;
    325 	struct temac_softc 	*sc = (struct temac_softc *)self;
    326 	struct ifnet 		*ifp = &sc->sc_if;
    327 	struct mii_data 	*mii = &sc->sc_mii;
    328 	uint8_t 		enaddr[ETHER_ADDR_LEN];
    329 	bus_dma_segment_t 	seg;
    330 	int 			error, nseg, i;
    331 
    332 	printf(": TEMAC\n"); 	/* XXX will be LL_TEMAC, PLB_TEMAC */
    333 
    334 	KASSERT(rx);
    335 	KASSERT(tx);
    336 
    337 	sc->sc_dmat = vaa->vaa_dmat;
    338 	sc->sc_dead = 0;
    339 	sc->sc_rx_drained = 1;
    340 	sc->sc_txbusy = 0;
    341 	sc->sc_iot = vaa->vaa_iot;
    342 	sc->sc_dma_rxt = rx->dmac_iot;
    343 	sc->sc_dma_txt = tx->dmac_iot;
    344 
    345 	/*
    346 	 * Map HIF and receive/transmit dmac registers.
    347 	 */
    348 	if ((error = bus_space_map(vaa->vaa_iot, vaa->vaa_addr, TEMAC_SIZE, 0,
    349 	    &sc->sc_ioh)) != 0) {
    350 		printf("%s: could not map registers\n", device_xname(self));
    351 		goto fail_0;
    352 	}
    353 
    354 	if ((error = bus_space_map(sc->sc_dma_rxt, rx->dmac_ctrl_addr,
    355 	    CDMAC_CTRL_SIZE, 0, &sc->sc_dma_rxh)) != 0) {
    356 		printf("%s: could not map Rx control registers\n",
    357 		    device_xname(self));
    358 		goto fail_0;
    359 	}
    360 	if ((error = bus_space_map(sc->sc_dma_rxt, rx->dmac_stat_addr,
    361 	    CDMAC_STAT_SIZE, 0, &sc->sc_dma_rsh)) != 0) {
    362 		printf("%s: could not map Rx status register\n",
    363 		    device_xname(self));
    364 		goto fail_0;
    365 	}
    366 
    367 	if ((error = bus_space_map(sc->sc_dma_txt, tx->dmac_ctrl_addr,
    368 	    CDMAC_CTRL_SIZE, 0, &sc->sc_dma_txh)) != 0) {
    369 		printf("%s: could not map Tx control registers\n",
    370 		    device_xname(self));
    371 		goto fail_0;
    372 	}
    373 	if ((error = bus_space_map(sc->sc_dma_txt, tx->dmac_stat_addr,
    374 	    CDMAC_STAT_SIZE, 0, &sc->sc_dma_tsh)) != 0) {
    375 		printf("%s: could not map Tx status register\n",
    376 		    device_xname(self));
    377 		goto fail_0;
    378 	}
    379 
    380 	/*
    381 	 * Allocate and initialize DMA control chains.
    382 	 */
    383 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    384 	    sizeof(struct temac_control), 8, 0, &seg, 1, &nseg, 0)) != 0) {
    385 	    	printf("%s: could not allocate control data\n",
    386 	    	    sc->sc_dev.dv_xname);
    387 		goto fail_0;
    388 	}
    389 
    390 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, nseg,
    391 	    sizeof(struct temac_control),
    392 	    (void **)&sc->sc_control_data, BUS_DMA_COHERENT)) != 0) {
    393 	    	printf("%s: could not map control data\n",
    394 	    	    sc->sc_dev.dv_xname);
    395 		goto fail_1;
    396 	}
    397 
    398 	if ((error = bus_dmamap_create(sc->sc_dmat,
    399 	    sizeof(struct temac_control), 1,
    400 	    sizeof(struct temac_control), 0, 0, &sc->sc_control_dmap)) != 0) {
    401 	    	printf("%s: could not create control data DMA map\n",
    402 	    	    sc->sc_dev.dv_xname);
    403 		goto fail_2;
    404 	}
    405 
    406 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_control_dmap,
    407 	    sc->sc_control_data, sizeof(struct temac_control), NULL, 0)) != 0) {
    408 	    	printf("%s: could not load control data DMA map\n",
    409 	    	    sc->sc_dev.dv_xname);
    410 		goto fail_3;
    411 	}
    412 
    413 	/*
    414 	 * Link descriptor chains.
    415 	 */
    416 	memset(sc->sc_control_data, 0, sizeof(struct temac_control));
    417 
    418 	for (i = 0; i < TEMAC_NTXDESC; i++) {
    419 		sc->sc_txdescs[i].desc_next = sc->sc_cdaddr +
    420 		    TEMAC_TXDOFF(TEMAC_TXNEXT(i));
    421 		sc->sc_txdescs[i].desc_stat = CDMAC_STAT_DONE;
    422 	}
    423 	for (i = 0; i < TEMAC_NRXDESC; i++) {
    424 		sc->sc_rxdescs[i].desc_next = sc->sc_cdaddr +
    425 		    TEMAC_RXDOFF(TEMAC_RXNEXT(i));
    426 		sc->sc_txdescs[i].desc_stat = CDMAC_STAT_DONE;
    427 	}
    428 
    429 	bus_dmamap_sync(sc->sc_dmat, sc->sc_control_dmap, 0,
    430 	    sizeof(struct temac_control),
    431 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    432 
    433 	/*
    434 	 * Initialize software state for transmit/receive jobs.
    435 	 */
    436 	for (i = 0; i < TEMAC_TXQLEN; i++) {
    437 		if ((error = bus_dmamap_create(sc->sc_dmat,
    438 		    ETHER_MAX_LEN_JUMBO, TEMAC_NTXSEG, ETHER_MAX_LEN_JUMBO,
    439 		    0, 0, &sc->sc_txsoft[i].txs_dmap)) != 0) {
    440 		    	printf("%s: could not create Tx DMA map %d\n",
    441 		    	    sc->sc_dev.dv_xname, i);
    442 			goto fail_4;
    443 		}
    444 		sc->sc_txsoft[i].txs_mbuf = NULL;
    445 		sc->sc_txsoft[i].txs_last = 0;
    446 	}
    447 
    448 	for (i = 0; i < TEMAC_NRXDESC; i++) {
    449 		if ((error = bus_dmamap_create(sc->sc_dmat,
    450 		    MCLBYTES, TEMAC_NRXSEG, MCLBYTES, 0, 0,
    451 		    &sc->sc_rxsoft[i].rxs_dmap)) != 0) {
    452 		    	printf("%s: could not create Rx DMA map %d\n",
    453 		    	    sc->sc_dev.dv_xname, i);
    454 			goto fail_5;
    455 		}
    456 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    457 	}
    458 
    459 	/*
    460 	 * Setup transfer interrupt handlers.
    461 	 */
    462 	error = ENOMEM;
    463 
    464 	sc->sc_rx_ih = ll_dmac_intr_establish(rx->dmac_chan,
    465 	    temac_rx_intr, sc);
    466 	if (sc->sc_rx_ih == NULL) {
    467 		printf("%s: could not establish Rx interrupt\n",
    468 		    device_xname(self));
    469 		goto fail_5;
    470 	}
    471 
    472 	sc->sc_tx_ih = ll_dmac_intr_establish(tx->dmac_chan,
    473 	    temac_tx_intr, sc);
    474 	if (sc->sc_tx_ih == NULL) {
    475 		printf("%s: could not establish Tx interrupt\n",
    476 		    device_xname(self));
    477 		goto fail_6;
    478 	}
    479 
    480 	/* XXXFreza: faked, should read unicast address filter. */
    481 	enaddr[0] = 0x00;
    482 	enaddr[1] = 0x11;
    483 	enaddr[2] = 0x17;
    484 	enaddr[3] = 0xff;
    485 	enaddr[4] = 0xff;
    486 	enaddr[5] = 0x01;
    487 
    488 	/*
    489 	 * Initialize the TEMAC.
    490 	 */
    491 	temac_reset(sc);
    492 
    493 	/* Configure MDIO link. */
    494 	gmi_write_4(TEMAC_GMI_MGMTCF, GMI_MGMT_CLKDIV_100MHz | GMI_MGMT_MDIO);
    495 
    496 	/* Initialize PHY. */
    497 	mii->mii_ifp = ifp;
    498 	mii->mii_readreg = temac_mii_readreg;
    499 	mii->mii_writereg = temac_mii_writereg;
    500 	mii->mii_statchg = temac_mii_statchg;
    501 	sc->sc_ec.ec_mii = mii;
    502 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
    503 
    504 	mii_attach(&sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
    505 	    MII_OFFSET_ANY, 0);
    506 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    507 		ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    508 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_NONE);
    509 	} else {
    510 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
    511 	}
    512 
    513 	/* Hold PHY in reset. */
    514 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, TEMAC_RESET, TEMAC_RESET_PHY);
    515 
    516 	/* Reset EMAC. */
    517 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, TEMAC_RESET,
    518 	    TEMAC_RESET_EMAC);
    519 	delay(10000);
    520 
    521 	/* Reset peripheral, awakes PHY and EMAC. */
    522 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, TEMAC_RESET,
    523 	    TEMAC_RESET_PERIPH);
    524 	delay(40000);
    525 
    526 	/* (Re-)Configure MDIO link. */
    527 	gmi_write_4(TEMAC_GMI_MGMTCF, GMI_MGMT_CLKDIV_100MHz | GMI_MGMT_MDIO);
    528 
    529 	/*
    530 	 * Hook up with network stack.
    531 	 */
    532 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    533 	ifp->if_softc = sc;
    534 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    535 	ifp->if_ioctl = temac_ioctl;
    536 	ifp->if_start = temac_start;
    537 	ifp->if_init = temac_init;
    538 	ifp->if_stop = temac_stop;
    539 	ifp->if_watchdog = NULL;
    540 	IFQ_SET_READY(&ifp->if_snd);
    541 	IFQ_SET_MAXLEN(&ifp->if_snd, TEMAC_TXQLEN);
    542 
    543 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
    544 
    545 	if_attach(ifp);
    546 	ether_ifattach(ifp, enaddr);
    547 
    548 	sc->sc_sdhook = shutdownhook_establish(temac_shutdown, sc);
    549 	if (sc->sc_sdhook == NULL)
    550 		printf("%s: WARNING: unable to establish shutdown hook\n",
    551 		    device_xname(self));
    552 
    553 	callout_setfunc(&sc->sc_mii_tick, temac_mii_tick, sc);
    554 	callout_setfunc(&sc->sc_rx_timo, temac_rxtimo, sc);
    555 
    556 	return ;
    557 
    558  fail_6:
    559 	ll_dmac_intr_disestablish(rx->dmac_chan, sc->sc_rx_ih);
    560 	i = TEMAC_NRXDESC;
    561  fail_5:
    562  	for (--i; i >= 0; i--)
    563  		bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxsoft[i].rxs_dmap);
    564 	i = TEMAC_TXQLEN;
    565  fail_4:
    566  	for (--i; i >= 0; i--)
    567  		bus_dmamap_destroy(sc->sc_dmat, sc->sc_txsoft[i].txs_dmap);
    568  fail_3:
    569 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_control_dmap);
    570  fail_2:
    571 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
    572 	    sizeof(struct temac_control));
    573  fail_1:
    574 	bus_dmamem_free(sc->sc_dmat, &seg, nseg);
    575  fail_0:
    576  	printf("%s: error = %d\n", device_xname(self), error);
    577 }
    578 
    579 /*
    580  * Network device.
    581  */
    582 static int
    583 temac_init(struct ifnet *ifp)
    584 {
    585 	struct temac_softc 	*sc = (struct temac_softc *)ifp->if_softc;
    586 	uint32_t 		rcr, tcr;
    587 	int 			i, error;
    588 
    589 	/* Reset DMA channels. */
    590 	cdmac_tx_reset(sc);
    591 	cdmac_rx_reset(sc);
    592 
    593 	/* Set current media. */
    594 	if ((error = ether_mediachange(ifp)) != 0)
    595 		return error;
    596 
    597 	callout_schedule(&sc->sc_mii_tick, hz);
    598 
    599 	/* Enable EMAC engine. */
    600 	rcr = (gmi_read_4(TEMAC_GMI_RXCF1) | GMI_RX_ENABLE) &
    601 	    ~(GMI_RX_JUMBO | GMI_RX_FCS);
    602 	gmi_write_4(TEMAC_GMI_RXCF1, rcr);
    603 
    604 	tcr = (gmi_read_4(TEMAC_GMI_TXCF) | GMI_TX_ENABLE) &
    605 	    ~(GMI_TX_JUMBO | GMI_TX_FCS);
    606 	gmi_write_4(TEMAC_GMI_TXCF, tcr);
    607 
    608 	/* XXXFreza: Force promiscuous mode, for now. */
    609 	gmi_write_4(TEMAC_GMI_AFM, GMI_AFM_PROMISC);
    610 	ifp->if_flags |= IFF_PROMISC;
    611 
    612 	/* Rx/Tx queues are drained -- either from attach() or stop(). */
    613 	sc->sc_txsfree = TEMAC_TXQLEN;
    614 	sc->sc_txsreap = 0;
    615 	sc->sc_txscur = 0;
    616 
    617 	sc->sc_txfree = TEMAC_NTXDESC;
    618 	sc->sc_txreap = 0;
    619 	sc->sc_txcur = 0;
    620 
    621 	sc->sc_rxreap = 0;
    622 
    623 	/* Allocate and map receive buffers. */
    624 	if (sc->sc_rx_drained) {
    625 		for (i = 0; i < TEMAC_NRXDESC; i++) {
    626 			if ((error = temac_rxalloc(sc, i, 1)) != 0) {
    627 				printf("%s: failed to allocate Rx "
    628 				    "descriptor %d\n",
    629 				    sc->sc_dev.dv_xname, i);
    630 
    631 				temac_rxdrain(sc);
    632 				return (error);
    633 			}
    634 		}
    635 		sc->sc_rx_drained = 0;
    636 
    637 		temac_rxcdsync(sc, 0, TEMAC_NRXDESC,
    638 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    639 		cdmac_rx_start(sc, sc->sc_cdaddr + TEMAC_RXDOFF(0));
    640 	}
    641 
    642 	ifp->if_flags |= IFF_RUNNING;
    643 	ifp->if_flags &= ~IFF_OACTIVE;
    644 
    645 	return (0);
    646 }
    647 
    648 static int
    649 temac_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    650 {
    651 	struct temac_softc 	*sc = (struct temac_softc *)ifp->if_softc;
    652 	struct ifreq 		*ifr = (struct ifreq *)data;
    653 	int 			s, ret;
    654 
    655 	s = splnet();
    656 	if (sc->sc_dead)
    657 		ret = EIO;
    658 	else
    659 		ret = ether_ioctl(ifp, cmd, data);
    660 	splx(s);
    661 	return (ret);
    662 }
    663 
    664 static void
    665 temac_start(struct ifnet *ifp)
    666 {
    667 	struct temac_softc 	*sc = (struct temac_softc *)ifp->if_softc;
    668 	struct temac_txsoft 	*txs;
    669 	struct mbuf 		*m;
    670 	bus_dmamap_t 		dmap;
    671 	int 			error, head, nsegs, i;
    672 
    673 	nsegs = 0;
    674 	head = sc->sc_txcur;
    675 	txs = NULL; 		/* gcc */
    676 
    677 	if (sc->sc_dead)
    678 		return;
    679 
    680 	KASSERT(sc->sc_txfree >= 0);
    681 	KASSERT(sc->sc_txsfree >= 0);
    682 
    683 	/*
    684 	 * Push mbufs into descriptor chain until we drain the interface
    685 	 * queue or run out of descriptors. We'll mark the first segment
    686 	 * as "done" in hope that we might put CDMAC interrupt above IPL_NET
    687 	 * and have it start jobs & mark packets for GC preemtively for
    688 	 * us -- creativity due to limitations in CDMAC transfer engine
    689 	 * (it really consumes lists, not circular queues, AFAICS).
    690 	 *
    691 	 * We schedule one interrupt per Tx batch.
    692 	 */
    693 	while (1) {
    694 		IFQ_POLL(&ifp->if_snd, m);
    695 		if (m == NULL)
    696 			break;
    697 
    698 		if (sc->sc_txsfree == 0) {
    699 			ifp->if_flags |= IFF_OACTIVE;
    700 			break;
    701 		}
    702 
    703 		txs = &sc->sc_txsoft[sc->sc_txscur];
    704 		dmap = txs->txs_dmap;
    705 
    706 		if (txs->txs_mbuf != NULL)
    707 			printf("FOO\n");
    708 		if (txs->txs_last)
    709 			printf("BAR\n");
    710 
    711 		if ((error = bus_dmamap_load_mbuf(sc->sc_dmat, dmap, m,
    712 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT)) != 0) {
    713 		    	if (error == EFBIG) {
    714 		    		printf("%s: Tx consumes too many segments, "
    715 		    		    "dropped\n", sc->sc_dev.dv_xname);
    716 				IFQ_DEQUEUE(&ifp->if_snd, m);
    717 				m_freem(m);
    718 				continue;
    719 		    	} else {
    720 		    		printf("%s: Tx stall due to resource "
    721 		    		    "shortage\n", sc->sc_dev.dv_xname);
    722 		    		break;
    723 			}
    724 		}
    725 
    726 		/*
    727 		 * If we're short on DMA descriptors, notify upper layers
    728 		 * and leave this packet for later.
    729 		 */
    730 		if (dmap->dm_nsegs > sc->sc_txfree) {
    731 			bus_dmamap_unload(sc->sc_dmat, dmap);
    732 			ifp->if_flags |= IFF_OACTIVE;
    733 			break;
    734 		}
    735 
    736 		IFQ_DEQUEUE(&ifp->if_snd, m);
    737 
    738 		bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
    739 		    BUS_DMASYNC_PREWRITE);
    740 		txs->txs_mbuf = m;
    741 
    742 		/*
    743 		 * Map the packet into descriptor chain. XXX We'll want
    744 		 * to fill checksum offload commands here.
    745 		 *
    746 		 * We would be in a race if we weren't blocking CDMAC intr
    747 		 * at this point -- we need to be locked against txreap()
    748 		 * because of dmasync ops.
    749 		 */
    750 
    751 		temac_txcdsync(sc, sc->sc_txcur, dmap->dm_nsegs,
    752 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    753 
    754 		for (i = 0; i < dmap->dm_nsegs; i++) {
    755 			sc->sc_txdescs[sc->sc_txcur].desc_addr =
    756 			    dmap->dm_segs[i].ds_addr;
    757 			sc->sc_txdescs[sc->sc_txcur].desc_size =
    758 			    dmap->dm_segs[i].ds_len;
    759 			sc->sc_txdescs[sc->sc_txcur].desc_stat =
    760 			    (i == 0 			? CDMAC_STAT_SOP : 0) |
    761 			    (i == (dmap->dm_nsegs - 1) 	? CDMAC_STAT_EOP : 0);
    762 
    763 			sc->sc_txcur = TEMAC_TXNEXT(sc->sc_txcur);
    764 		}
    765 
    766 		sc->sc_txfree -= dmap->dm_nsegs;
    767 		nsegs += dmap->dm_nsegs;
    768 
    769 		sc->sc_txscur = TEMAC_TXSNEXT(sc->sc_txscur);
    770 		sc->sc_txsfree--;
    771 	}
    772 
    773 	/* Get data running if we queued any. */
    774 	if (nsegs > 0) {
    775 		int 		tail = TEMAC_TXINC(sc->sc_txcur, -1);
    776 
    777 		/* Mark the last packet in this job. */
    778 		txs->txs_last = 1;
    779 
    780 		/* Mark the last descriptor in this job. */
    781 		sc->sc_txdescs[tail].desc_stat |= CDMAC_STAT_STOP |
    782 		    CDMAC_STAT_INTR;
    783 		temac_txcdsync(sc, head, nsegs,
    784 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    785 
    786 		temac_txkick(sc);
    787 #if TEMAC_TXDEBUG > 0
    788 		printf("%s: start:  txcur  %03d -> %03d, nseg %03d\n",
    789 		    sc->sc_dev.dv_xname, head, sc->sc_txcur, nsegs);
    790 #endif
    791 	}
    792 }
    793 
    794 static void
    795 temac_stop(struct ifnet *ifp, int disable)
    796 {
    797 	struct temac_softc 	*sc = (struct temac_softc *)ifp->if_softc;
    798 	struct temac_txsoft 	*txs;
    799 	int 			i;
    800 
    801 #if TEMAC_DEBUG > 0
    802 	printf("%s: stop\n", device_xname(&sc->sc_dev));
    803 #endif
    804 
    805 	/* Down the MII. */
    806 	callout_stop(&sc->sc_mii_tick);
    807 	mii_down(&sc->sc_mii);
    808 
    809 	/* Stop the engine. */
    810 	temac_reset(sc);
    811 
    812 	/* Drain buffers queues (unconditionally). */
    813 	temac_rxdrain(sc);
    814 
    815 	for (i = 0; i < TEMAC_TXQLEN; i++) {
    816 		txs = &sc->sc_txsoft[i];
    817 
    818 		if (txs->txs_mbuf != NULL) {
    819 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmap);
    820 			m_freem(txs->txs_mbuf);
    821 			txs->txs_mbuf = NULL;
    822 			txs->txs_last = 0;
    823 		}
    824 	}
    825 	sc->sc_txbusy = 0;
    826 
    827 	/* Acknowledge we're down. */
    828 	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
    829 }
    830 
    831 static int
    832 temac_mii_readreg(struct device *self, int phy, int reg)
    833 {
    834 	mtidcr(IDCR_HIF_ARG0, (phy << 5) | reg);
    835 	mtidcr(IDCR_HIF_CTRL, TEMAC_GMI_MII_ADDR);
    836 	hif_wait_stat(HIF_STAT_MIIRR);
    837 
    838 	return (int)mfidcr(IDCR_HIF_ARG0);
    839 }
    840 
    841 static void
    842 temac_mii_writereg(struct device *self, int phy, int reg, int val)
    843 {
    844 	mtidcr(IDCR_HIF_ARG0, val);
    845 	mtidcr(IDCR_HIF_CTRL, TEMAC_GMI_MII_WRVAL | HIF_CTRL_WRITE);
    846 	mtidcr(IDCR_HIF_ARG0, (phy << 5) | reg);
    847 	mtidcr(IDCR_HIF_CTRL, TEMAC_GMI_MII_ADDR | HIF_CTRL_WRITE);
    848 	hif_wait_stat(HIF_STAT_MIIWR);
    849 }
    850 
    851 static void
    852 temac_mii_statchg(struct device *self)
    853 {
    854 	struct temac_softc 	*sc = (struct temac_softc *)self;
    855 	uint32_t 		rcf, tcf, mmc;
    856 
    857 	/* Full/half duplex link. */
    858 	rcf = gmi_read_4(TEMAC_GMI_RXCF1);
    859 	tcf = gmi_read_4(TEMAC_GMI_TXCF);
    860 
    861 	if (sc->sc_mii.mii_media_active & IFM_FDX) {
    862 		gmi_write_4(TEMAC_GMI_RXCF1, rcf & ~GMI_RX_HDX);
    863 		gmi_write_4(TEMAC_GMI_TXCF, tcf & ~GMI_TX_HDX);
    864 	} else {
    865 		gmi_write_4(TEMAC_GMI_RXCF1, rcf | GMI_RX_HDX);
    866 		gmi_write_4(TEMAC_GMI_TXCF, tcf | GMI_TX_HDX);
    867 	}
    868 
    869 	/* Link speed. */
    870 	mmc = gmi_read_4(TEMAC_GMI_MMC) & ~GMI_MMC_SPEED_MASK;
    871 
    872 	switch (IFM_SUBTYPE(sc->sc_mii.mii_media_active)) {
    873 	case IFM_10_T:
    874 		/*
    875 		 * XXXFreza: the GMAC is not happy with 10Mbit ethernet,
    876 		 * although the documentation claims it's supported. Maybe
    877 		 * it's just my equipment...
    878 		 */
    879 		mmc |= GMI_MMC_SPEED_10;
    880 		break;
    881 	case IFM_100_TX:
    882 		mmc |= GMI_MMC_SPEED_100;
    883 		break;
    884 	case IFM_1000_T:
    885 		mmc |= GMI_MMC_SPEED_1000;
    886 		break;
    887 	}
    888 
    889 	gmi_write_4(TEMAC_GMI_MMC, mmc);
    890 }
    891 
    892 static void
    893 temac_mii_tick(void *arg)
    894 {
    895 	struct temac_softc 	*sc = (struct temac_softc *)arg;
    896 	int 			s;
    897 
    898 	if (!device_is_active(&sc->sc_dev))
    899 		return;
    900 
    901 	s = splnet();
    902 	mii_tick(&sc->sc_mii);
    903 	splx(s);
    904 
    905 	callout_schedule(&sc->sc_mii_tick, hz);
    906 }
    907 
    908 /*
    909  * External hooks.
    910  */
    911 static void
    912 temac_shutdown(void *arg)
    913 {
    914 	struct temac_softc 	*sc = (struct temac_softc *)arg;
    915 
    916 	temac_reset(sc);
    917 }
    918 
    919 static void
    920 temac_tx_intr(void *arg)
    921 {
    922 	struct temac_softc 	*sc = (struct temac_softc *)arg;
    923 	uint32_t 		stat;
    924 
    925 	/* XXX: We may need to splnet() here if cdmac(4) changes. */
    926 
    927 	if ((stat = cdmac_tx_stat(sc)) & CDMAC_STAT_ERROR) {
    928 		printf("%s: transmit DMA is toast (%#08x), halted!\n",
    929 		    sc->sc_dev.dv_xname, stat);
    930 
    931 		/* XXXFreza: how to signal this upstream? */
    932 		temac_stop(&sc->sc_if, 1);
    933 		sc->sc_dead = 1;
    934 	}
    935 
    936 #if TEMAC_DEBUG > 0
    937 	printf("%s: tx intr 0x%08x\n", device_xname(&sc->sc_dev), stat);
    938 #endif
    939 	temac_txreap(sc);
    940 }
    941 
    942 static void
    943 temac_rx_intr(void *arg)
    944 {
    945 	struct temac_softc 	*sc = (struct temac_softc *)arg;
    946 	uint32_t 		stat;
    947 
    948 	/* XXX: We may need to splnet() here if cdmac(4) changes. */
    949 
    950 	if ((stat = cdmac_rx_stat(sc)) & CDMAC_STAT_ERROR) {
    951 		printf("%s: receive DMA is toast (%#08x), halted!\n",
    952 		    sc->sc_dev.dv_xname, stat);
    953 
    954 		/* XXXFreza: how to signal this upstream? */
    955 		temac_stop(&sc->sc_if, 1);
    956 		sc->sc_dead = 1;
    957 	}
    958 
    959 #if TEMAC_DEBUG > 0
    960 	printf("%s: rx intr 0x%08x\n", device_xname(&sc->sc_dev), stat);
    961 #endif
    962 	temac_rxreap(sc);
    963 }
    964 
    965 /*
    966  * Utils.
    967  */
    968 static inline void
    969 temac_txcdsync(struct temac_softc *sc, int first, int cnt, int flag)
    970 {
    971 	if ((first + cnt) > TEMAC_NTXDESC) {
    972 		bus_dmamap_sync(sc->sc_dmat, sc->sc_control_dmap,
    973 		    TEMAC_TXDOFF(first),
    974 		    sizeof(struct cdmac_descr) * (TEMAC_NTXDESC - first),
    975 		    flag);
    976 		cnt = (first + cnt) % TEMAC_NTXDESC;
    977 		first = 0;
    978 	}
    979 
    980 	bus_dmamap_sync(sc->sc_dmat, sc->sc_control_dmap,
    981 	    TEMAC_TXDOFF(first),
    982 	    sizeof(struct cdmac_descr) * cnt,
    983 	    flag);
    984 }
    985 
    986 static inline void
    987 temac_rxcdsync(struct temac_softc *sc, int first, int cnt, int flag)
    988 {
    989 	if ((first + cnt) > TEMAC_NRXDESC) {
    990 		bus_dmamap_sync(sc->sc_dmat, sc->sc_control_dmap,
    991 		    TEMAC_RXDOFF(first),
    992 		    sizeof(struct cdmac_descr) * (TEMAC_NRXDESC - first),
    993 		    flag);
    994 		cnt = (first + cnt) % TEMAC_NRXDESC;
    995 		first = 0;
    996 	}
    997 
    998 	bus_dmamap_sync(sc->sc_dmat, sc->sc_control_dmap,
    999 	    TEMAC_RXDOFF(first),
   1000 	    sizeof(struct cdmac_descr) * cnt,
   1001 	    flag);
   1002 }
   1003 
   1004 static void
   1005 temac_txreap(struct temac_softc *sc)
   1006 {
   1007 	struct temac_txsoft 	*txs;
   1008 	bus_dmamap_t 		dmap;
   1009 	int 			sent = 0;
   1010 
   1011 	/*
   1012 	 * Transmit interrupts happen on the last descriptor of Tx jobs.
   1013 	 * Hence, every time we're called (and we assume txintr is our
   1014 	 * only caller!), we reap packets upto and including the one
   1015 	 * marked as last-in-batch.
   1016 	 *
   1017 	 * XXX we rely on that we make EXACTLY one batch per intr, no more
   1018 	 */
   1019 	while (sc->sc_txsfree != TEMAC_TXQLEN) {
   1020 		txs = &sc->sc_txsoft[sc->sc_txsreap];
   1021 		dmap = txs->txs_dmap;
   1022 
   1023 		sc->sc_txreap = TEMAC_TXINC(sc->sc_txreap, dmap->dm_nsegs);
   1024 		sc->sc_txfree += dmap->dm_nsegs;
   1025 
   1026 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmap);
   1027 		m_freem(txs->txs_mbuf);
   1028 		txs->txs_mbuf = NULL;
   1029 
   1030 		sc->sc_if.if_opackets++;
   1031 		sent = 1;
   1032 
   1033 		sc->sc_txsreap = TEMAC_TXSNEXT(sc->sc_txsreap);
   1034 		sc->sc_txsfree++;
   1035 
   1036 		if (txs->txs_last) {
   1037 			txs->txs_last = 0;
   1038 			sc->sc_txbusy = 0; 	/* channel stopped now */
   1039 
   1040 			temac_txkick(sc);
   1041 			break;
   1042 		}
   1043 	}
   1044 
   1045 	if (sent && (sc->sc_if.if_flags & IFF_OACTIVE))
   1046 		sc->sc_if.if_flags &= ~IFF_OACTIVE;
   1047 }
   1048 
   1049 static int
   1050 temac_rxalloc(struct temac_softc *sc, int which, int verbose)
   1051 {
   1052 	struct temac_rxsoft 	*rxs;
   1053 	struct mbuf 		*m;
   1054 	uint32_t 		stat;
   1055 	int 			error;
   1056 
   1057 	rxs = &sc->sc_rxsoft[which];
   1058 
   1059 	/* The mbuf itself is not our problem, just clear DMA related stuff. */
   1060 	if (rxs->rxs_mbuf != NULL) {
   1061 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmap);
   1062 		rxs->rxs_mbuf = NULL;
   1063 	}
   1064 
   1065 	/*
   1066 	 * We would like to store mbuf and dmap in application specific
   1067 	 * fields of the descriptor, but that doesn't work for Rx. Shame
   1068 	 * on Xilinx for this (and for the useless timer architecture).
   1069 	 *
   1070 	 * Hence each descriptor needs its own soft state. We may want
   1071 	 * to merge multiple rxs's into a monster mbuf when we support
   1072 	 * jumbo frames though. Also, we use single set of indexing
   1073 	 * variables for both sc_rxdescs[] and sc_rxsoft[].
   1074 	 */
   1075 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1076 	if (m == NULL) {
   1077 		if (verbose)
   1078 			printf("%s: out of Rx header mbufs\n",
   1079 			    sc->sc_dev.dv_xname);
   1080 		return (ENOBUFS);
   1081 	}
   1082 	MCLAIM(m, &sc->sc_ec.ec_rx_mowner);
   1083 
   1084 	MCLGET(m, M_DONTWAIT);
   1085 	if ((m->m_flags & M_EXT) == 0) {
   1086 		if (verbose)
   1087 			printf("%s: out of Rx cluster mbufs\n",
   1088 			    sc->sc_dev.dv_xname);
   1089 		m_freem(m);
   1090 		return (ENOBUFS);
   1091 	}
   1092 
   1093 	rxs->rxs_mbuf = m;
   1094 	m->m_pkthdr.len = m->m_len = MCLBYTES;
   1095 
   1096 	/* Make sure the payload after ethernet header is 4-aligned. */
   1097 	m_adj(m, 2);
   1098 
   1099 	error = bus_dmamap_load_mbuf(sc->sc_dmat, rxs->rxs_dmap, m,
   1100 	    BUS_DMA_NOWAIT);
   1101 	if (error) {
   1102 		if (verbose)
   1103 			printf("%s: could not map Rx descriptor %d, "
   1104 			    "error = %d\n", sc->sc_dev.dv_xname, which, error);
   1105 
   1106 		rxs->rxs_mbuf = NULL;
   1107 		m_freem(m);
   1108 
   1109 		return (error);
   1110 	}
   1111 
   1112 	stat = \
   1113 	    (TEMAC_ISINTR(which) ? CDMAC_STAT_INTR : 0) |
   1114 	    (TEMAC_ISLAST(which) ? CDMAC_STAT_STOP : 0);
   1115 
   1116 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmap, 0,
   1117 	    rxs->rxs_dmap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1118 
   1119 	/* Descriptor post-sync, if needed, left to the caller. */
   1120 
   1121 	sc->sc_rxdescs[which].desc_addr = rxs->rxs_dmap->dm_segs[0].ds_addr;
   1122 	sc->sc_rxdescs[which].desc_size  = rxs->rxs_dmap->dm_segs[0].ds_len;
   1123 	sc->sc_rxdescs[which].desc_stat = stat;
   1124 
   1125 	/* Descriptor pre-sync, if needed, left to the caller. */
   1126 
   1127 	return (0);
   1128 }
   1129 
   1130 static void
   1131 temac_rxreap(struct temac_softc *sc)
   1132 {
   1133 	struct ifnet 		*ifp = &sc->sc_if;
   1134 	uint32_t 		stat, rxstat, rxsize;
   1135 	struct mbuf 		*m;
   1136 	int 			nseg, head, tail;
   1137 
   1138 	head = sc->sc_rxreap;
   1139 	tail = 0; 		/* gcc */
   1140 	nseg = 0;
   1141 
   1142 	/*
   1143 	 * Collect finished entries on the Rx list, kick DMA if we hit
   1144 	 * the end. DMA will always stop on the last descriptor in chain,
   1145 	 * so it will never hit a reap-in-progress descriptor.
   1146 	 */
   1147 	while (1) {
   1148 		/* Maybe we previously failed to refresh this one? */
   1149 		if (sc->sc_rxsoft[sc->sc_rxreap].rxs_mbuf == NULL) {
   1150 			if (temac_rxalloc(sc, sc->sc_rxreap, 0) != 0)
   1151 				break;
   1152 
   1153 			sc->sc_rxreap = TEMAC_RXNEXT(sc->sc_rxreap);
   1154 			continue;
   1155 		}
   1156 		temac_rxcdsync(sc, sc->sc_rxreap, 1,
   1157 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1158 
   1159 		stat = sc->sc_rxdescs[sc->sc_rxreap].desc_stat;
   1160 		m = NULL;
   1161 
   1162 		if ((stat & CDMAC_STAT_DONE) == 0)
   1163 			break;
   1164 
   1165 		/* Count any decriptor we've collected, regardless of status. */
   1166 		nseg ++;
   1167 
   1168 		/* XXXFreza: This won't work for jumbo frames. */
   1169 
   1170 		if ((stat & (CDMAC_STAT_EOP | CDMAC_STAT_SOP)) !=
   1171 		    (CDMAC_STAT_EOP | CDMAC_STAT_SOP)) {
   1172 		    	printf("%s: Rx packet doesn't fit in "
   1173 		    	    "one descriptor, stat = %#08x\n",
   1174 		    	    sc->sc_dev.dv_xname, stat);
   1175 			goto badframe;
   1176 		}
   1177 
   1178 		/* Dissect TEMAC footer if this is end of packet. */
   1179 		rxstat = sc->sc_rxdescs[sc->sc_rxreap].desc_rxstat;
   1180 		rxsize = sc->sc_rxdescs[sc->sc_rxreap].desc_rxsize &
   1181 		    RXSIZE_MASK;
   1182 
   1183 		if ((rxstat & RXSTAT_GOOD) == 0 ||
   1184 		    (rxstat & RXSTAT_SICK) != 0) {
   1185 		    	printf("%s: corrupt Rx packet, rxstat = %#08x\n",
   1186 		    	    sc->sc_dev.dv_xname, rxstat);
   1187 			goto badframe;
   1188 		}
   1189 
   1190 		/* We are now bound to succeed. */
   1191 		bus_dmamap_sync(sc->sc_dmat,
   1192 		    sc->sc_rxsoft[sc->sc_rxreap].rxs_dmap, 0,
   1193 		    sc->sc_rxsoft[sc->sc_rxreap].rxs_dmap->dm_mapsize,
   1194 		    BUS_DMASYNC_POSTREAD);
   1195 
   1196 		m = sc->sc_rxsoft[sc->sc_rxreap].rxs_mbuf;
   1197 		m->m_pkthdr.rcvif = ifp;
   1198 		m->m_pkthdr.len = m->m_len = rxsize;
   1199 
   1200  badframe:
   1201  		/* Get ready for more work. */
   1202 		tail = sc->sc_rxreap;
   1203 		sc->sc_rxreap = TEMAC_RXNEXT(sc->sc_rxreap);
   1204 
   1205  		/* On failures we reuse the descriptor and go ahead. */
   1206  		if (m == NULL) {
   1207 			sc->sc_rxdescs[tail].desc_stat =
   1208 			    (TEMAC_ISINTR(tail) ? CDMAC_STAT_INTR : 0) |
   1209 			    (TEMAC_ISLAST(tail) ? CDMAC_STAT_STOP : 0);
   1210 
   1211 			ifp->if_ierrors++;
   1212 			continue;
   1213  		}
   1214 
   1215 #if NBPFILTER > 0
   1216 		if (ifp->if_bpf != NULL)
   1217 			bpf_mtap(ifp->if_bpf, m);
   1218 #endif
   1219 
   1220 		ifp->if_ipackets++;
   1221 		(ifp->if_input)(ifp, m);
   1222 
   1223 		/* Refresh descriptor, bail out if we're out of buffers. */
   1224 		if (temac_rxalloc(sc, tail, 1) != 0) {
   1225  			sc->sc_rxreap = TEMAC_RXINC(sc->sc_rxreap, -1);
   1226  			printf("%s: Rx give up for now\n", sc->sc_dev.dv_xname);
   1227 			break;
   1228 		}
   1229 	}
   1230 
   1231 	/* We may now have a contiguous ready-to-go chunk of descriptors. */
   1232 	if (nseg > 0) {
   1233 #if TEMAC_RXDEBUG > 0
   1234 		printf("%s: rxreap: rxreap %03d -> %03d, nseg %03d\n",
   1235 		    sc->sc_dev.dv_xname, head, sc->sc_rxreap, nseg);
   1236 #endif
   1237 		temac_rxcdsync(sc, head, nseg,
   1238 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1239 
   1240 		if (TEMAC_ISLAST(tail))
   1241 			cdmac_rx_start(sc, sc->sc_cdaddr + TEMAC_RXDOFF(0));
   1242 	}
   1243 
   1244 	/* Ensure maximum Rx latency is kept under control. */
   1245 	callout_schedule(&sc->sc_rx_timo, hz / TEMAC_RXTIMO_HZ);
   1246 }
   1247 
   1248 static void
   1249 temac_rxtimo(void *arg)
   1250 {
   1251 	struct temac_softc 	*sc = (struct temac_softc *)arg;
   1252 	int 			s;
   1253 
   1254 	/* We run TEMAC_RXTIMO_HZ times/sec to ensure Rx doesn't stall. */
   1255 	s = splnet();
   1256 	temac_rxreap(sc);
   1257 	splx(s);
   1258 }
   1259 
   1260 static void
   1261 temac_reset(struct temac_softc *sc)
   1262 {
   1263 	uint32_t 		rcr, tcr;
   1264 
   1265 	/* Kill CDMAC channels. */
   1266 	cdmac_tx_reset(sc);
   1267 	cdmac_rx_reset(sc);
   1268 
   1269 	/* Disable receiver. */
   1270 	rcr = gmi_read_4(TEMAC_GMI_RXCF1) & ~GMI_RX_ENABLE;
   1271 	gmi_write_4(TEMAC_GMI_RXCF1, rcr);
   1272 
   1273 	/* Disable transmitter. */
   1274 	tcr = gmi_read_4(TEMAC_GMI_TXCF) & ~GMI_TX_ENABLE;
   1275 	gmi_write_4(TEMAC_GMI_TXCF, tcr);
   1276 }
   1277 
   1278 static void
   1279 temac_rxdrain(struct temac_softc *sc)
   1280 {
   1281 	struct temac_rxsoft 	*rxs;
   1282 	int 			i;
   1283 
   1284 	for (i = 0; i < TEMAC_NRXDESC; i++) {
   1285 		rxs = &sc->sc_rxsoft[i];
   1286 
   1287 		if (rxs->rxs_mbuf != NULL) {
   1288 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmap);
   1289 			m_freem(rxs->rxs_mbuf);
   1290 			rxs->rxs_mbuf = NULL;
   1291 		}
   1292 	}
   1293 
   1294 	sc->sc_rx_drained = 1;
   1295 }
   1296 
   1297 static void
   1298 temac_txkick(struct temac_softc *sc)
   1299 {
   1300 	if (sc->sc_txsoft[sc->sc_txsreap].txs_mbuf != NULL &&
   1301 	    sc->sc_txbusy == 0) {
   1302 		cdmac_tx_start(sc, sc->sc_cdaddr + TEMAC_TXDOFF(sc->sc_txreap));
   1303 		sc->sc_txbusy = 1;
   1304 	}
   1305 }
   1306