Home | History | Annotate | Line # | Download | only in hpc
if_sq.c revision 1.1
      1 /*	$NetBSD: if_sq.c,v 1.1 2001/05/11 03:22:21 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 Rafal K. Boni
      5  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * Portions of this code are derived from software contributed to The
      9  * NetBSD Foundation by Jason R. Thorpe of the Numerical Aerospace
     10  * Simulation Facility, NASA Ames Research Center.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include "opt_inet.h"
     36 #include "opt_ns.h"
     37 #include "bpfilter.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/device.h>
     42 #include <sys/callout.h>
     43 #include <sys/mbuf.h>
     44 #include <sys/malloc.h>
     45 #include <sys/kernel.h>
     46 #include <sys/socket.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/errno.h>
     49 #include <sys/syslog.h>
     50 
     51 #include <uvm/uvm_extern.h>
     52 
     53 #include <machine/endian.h>
     54 
     55 #include <net/if.h>
     56 #include <net/if_dl.h>
     57 #include <net/if_media.h>
     58 #include <net/if_ether.h>
     59 
     60 #if NBPFILTER > 0
     61 #include <net/bpf.h>
     62 #endif
     63 
     64 #ifdef INET
     65 #include <netinet/in.h>
     66 #include <netinet/if_inarp.h>
     67 #endif
     68 
     69 #ifdef NS
     70 #include <netns/ns.h>
     71 #include <netns/ns_if.h>
     72 #endif
     73 
     74 /* XXXrkb: cheap hack until parents pass in DMA tags */
     75 #define _SGIMIPS_BUS_DMA_PRIVATE
     76 
     77 #include <machine/bus.h>
     78 #include <machine/arcs.h>
     79 #include <machine/intr.h>
     80 
     81 #include <dev/ic/seeq8003reg.h>
     82 
     83 #include <sgimips/hpc/sqvar.h>
     84 #include <sgimips/hpc/hpcvar.h>
     85 #include <sgimips/hpc/hpcreg.h>
     86 
     87 #define static
     88 
     89 /*
     90  * Short TODO list:
     91  *	(1) Do counters for bad-RX packets.
     92  *	(2) Inherit DMA tag via config machinery, don't hard-code it.
     93  *	(3) Allow multi-segment transmits, instead of copying to a single,
     94  *	    contiguous mbuf.
     95  *	(4) Verify sq_stop() turns off enough stuff; I was still getting
     96  *	    seeq interrupts after sq_stop().
     97  *	(5) Fix up printfs in driver (most should only fire ifdef SQ_DEBUG
     98  *	    or something similar.
     99  *	(6) Implement EDLC modes: especially packet auto-pad and simplex
    100  *	    mode.
    101  *	(7) Should the driver filter out its own transmissions in non-EDLC
    102  *	    mode?
    103  *	(8) Multicast support -- multicast filter, address management, ...
    104  *	(9) Deal with RB0 (recv buffer overflow) on reception.  Will need
    105  *	    to figure out if RB0 is read-only as stated in one spot in the
    106  *	    HPC spec or read-write (ie, is the 'write a one to clear it')
    107  *	    the correct thing?
    108  */
    109 
    110 static int	sq_match(struct device *, struct cfdata *, void *);
    111 static void	sq_attach(struct device *, struct device *, void *);
    112 static int	sq_init(struct ifnet *);
    113 static void	sq_start(struct ifnet *);
    114 static void	sq_stop(struct ifnet *, int);
    115 static void	sq_watchdog(struct ifnet *);
    116 static int	sq_ioctl(struct ifnet *, u_long, caddr_t);
    117 
    118 static int	sq_intr(void *);
    119 static int	sq_rxintr(struct sq_softc *);
    120 static int	sq_txintr(struct sq_softc *);
    121 static void	sq_reset(struct sq_softc *);
    122 static int 	sq_add_rxbuf(struct sq_softc *, int);
    123 static void 	sq_dump_buffer(u_int32_t addr, u_int32_t len);
    124 
    125 static void	enaddr_aton(const char*, u_int8_t*);
    126 
    127 /* Actions */
    128 #define SQ_RESET		1
    129 #define SQ_ADD_TO_DMA		2
    130 #define SQ_START_DMA		3
    131 #define SQ_DONE_DMA		4
    132 #define SQ_RESTART_DMA		5
    133 #define SQ_TXINTR_ENTER		6
    134 #define SQ_TXINTR_EXIT		7
    135 #define SQ_TXINTR_BUSY		8
    136 
    137 struct sq_action_trace {
    138 	int action;
    139 	int bufno;
    140 	int status;
    141 	int freebuf;
    142 };
    143 
    144 int sq_trace_idx = 0;
    145 struct sq_action_trace sq_trace[100];
    146 
    147 void sq_trace_dump(struct sq_softc* sc);
    148 
    149 #define SQ_TRACE(act, buf, stat, free) do {				\
    150 	sq_trace[sq_trace_idx].action = (act);				\
    151 	sq_trace[sq_trace_idx].bufno = (buf);				\
    152 	sq_trace[sq_trace_idx].status = (stat);				\
    153 	sq_trace[sq_trace_idx].freebuf = (free);			\
    154 	if (++sq_trace_idx == 100) {					\
    155 		bzero(&sq_trace, sizeof(sq_trace));			\
    156 		sq_trace_idx = 0;					\
    157 	}								\
    158 } while (0)
    159 
    160 struct cfattach sq_ca = {
    161 	sizeof(struct sq_softc), sq_match, sq_attach
    162 };
    163 
    164 static int
    165 sq_match(struct device *parent, struct cfdata *match, void *aux)
    166 {
    167 	/* XXX! */
    168 	return 1;
    169 }
    170 
    171 static void
    172 sq_attach(struct device *parent, struct device *self, void *aux)
    173 {
    174 	int i, err;
    175 	char* macaddr;
    176 	struct sq_softc *sc = (void *)self;
    177 	struct hpc_attach_args *haa = aux;
    178 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    179 
    180 	sc->sc_hpct = haa->ha_iot;
    181 	if ((err = bus_space_subregion(haa->ha_iot, haa->ha_ioh,
    182 				       HPC_ENET_REGS,
    183 				       HPC_ENET_REGS_SIZE,
    184 				       &sc->sc_hpch)) != 0) {
    185 		printf(": unable to map HPC DMA registers, error = %d\n", err);
    186 		goto fail_0;
    187 	}
    188 
    189 	sc->sc_regt = haa->ha_iot;
    190 	if ((err = bus_space_subregion(haa->ha_iot, haa->ha_ioh,
    191 				       HPC_ENET_DEVREGS,
    192 				       HPC_ENET_DEVREGS_SIZE,
    193 				       &sc->sc_regh)) != 0) {
    194 		printf(": unable to map Seeq registers, error = %d\n", err);
    195 		goto fail_0;
    196 	}
    197 
    198 	/* XXXrkb: should be inherited from parent bus, but works for now */
    199 	sc->sc_dmat = &sgimips_default_bus_dma_tag;
    200 
    201 	if ((err = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct sq_control),
    202 				    PAGE_SIZE, PAGE_SIZE, &sc->sc_cdseg,
    203 				    1, &sc->sc_ncdseg, BUS_DMA_NOWAIT)) != 0) {
    204 		printf(": unable to allocate control data, error = %d\n", err);
    205 		goto fail_0;
    206 	}
    207 
    208 	if ((err = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_ncdseg,
    209 				  sizeof(struct sq_control),
    210 				  (caddr_t *)&sc->sc_control,
    211 				  BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    212 		printf(": unable to map control data, error = %d\n", err);
    213 		goto fail_1;
    214 	}
    215 
    216 	if ((err = bus_dmamap_create(sc->sc_dmat, sizeof(struct sq_control),
    217 				     1, sizeof(struct sq_control), PAGE_SIZE,
    218 				     BUS_DMA_NOWAIT, &sc->sc_cdmap)) != 0) {
    219 		printf(": unable to create DMA map for control data, error "
    220 			"= %d\n", err);
    221 		goto fail_2;
    222 	}
    223 
    224 	if ((err = bus_dmamap_load(sc->sc_dmat, sc->sc_cdmap, sc->sc_control,
    225 				   sizeof(struct sq_control),
    226 				   NULL, BUS_DMA_NOWAIT)) != 0) {
    227 		printf(": unable to load DMA map for control data, error "
    228 			"= %d\n", err);
    229 		goto fail_3;
    230 	}
    231 
    232 	bzero(sc->sc_control, sizeof(struct sq_control));
    233 
    234 	/* Create transmit buffer DMA maps */
    235 	for (i = 0; i < SQ_NTXDESC; i++) {
    236 	    if ((err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
    237 					 0, BUS_DMA_NOWAIT,
    238 					 &sc->sc_txmap[i])) != 0) {
    239 		    printf(": unable to create tx DMA map %d, error = %d\n",
    240 			   i, err);
    241 		    goto fail_4;
    242 	    }
    243 	}
    244 
    245 	/* Create transmit buffer DMA maps */
    246 	for (i = 0; i < SQ_NRXDESC; i++) {
    247 	    if ((err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
    248 					 0, BUS_DMA_NOWAIT,
    249 					 &sc->sc_rxmap[i])) != 0) {
    250 		    printf(": unable to create rx DMA map %d, error = %d\n",
    251 			   i, err);
    252 		    goto fail_5;
    253 	    }
    254 	}
    255 
    256 	/* Pre-allocate the receive buffers.  */
    257 	for (i = 0; i < SQ_NRXDESC; i++) {
    258 		if ((err = sq_add_rxbuf(sc, i)) != 0) {
    259 			printf(": unable to allocate or map rx buffer %d\n,"
    260 			       " error = %d\n", i, err);
    261 			goto fail_6;
    262 		}
    263 	}
    264 
    265 	if ((macaddr = ARCS->GetEnvironmentVariable("eaddr")) == NULL) {
    266 		printf(": unable to get MAC address!\n");
    267 		goto fail_6;
    268 	}
    269 
    270 	if ((cpu_intr_establish(3, IPL_NET, sq_intr, sc)) == NULL) {
    271 		printf(": unable to establish interrupt!\n");
    272 		goto fail_6;
    273 	}
    274 
    275 
    276 	printf(": SGI Seeq-8003\n");
    277 
    278 	enaddr_aton(macaddr, sc->sc_enaddr);
    279 
    280 	printf("%s: station address %s\n", sc->sc_dev.dv_xname,
    281 					   ether_sprintf(sc->sc_enaddr));
    282 
    283 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    284 	ifp->if_softc = sc;
    285 	ifp->if_mtu = ETHERMTU;
    286 	ifp->if_init = sq_init;
    287 	ifp->if_stop = sq_stop;
    288 	ifp->if_start = sq_start;
    289 	ifp->if_ioctl = sq_ioctl;
    290 	ifp->if_watchdog = sq_watchdog;
    291 	ifp->if_flags =
    292 		IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    293 	IFQ_SET_READY(&ifp->if_snd);
    294 
    295 	if_attach(ifp);
    296 	ether_ifattach(ifp, sc->sc_enaddr);
    297 
    298 	bzero(&sq_trace, sizeof(sq_trace));
    299 	/* Done! */
    300 	return;
    301 
    302 	/*
    303 	 * Free any resources we've allocated during the failed attach
    304 	 * attempt.  Do this in reverse order and fall through.
    305 	 */
    306 fail_6:
    307 	for (i = 0; i < SQ_NRXDESC; i++) {
    308 		if (sc->sc_rxmbuf[i] != NULL) {
    309 			bus_dmamap_unload(sc->sc_dmat, sc->sc_rxmap[i]);
    310 			m_freem(sc->sc_rxmbuf[i]);
    311 		}
    312 	}
    313 fail_5:
    314 	for (i = 0; i < SQ_NRXDESC; i++) {
    315 	    if (sc->sc_rxmap[i] !=  NULL)
    316 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmap[i]);
    317 	}
    318 fail_4:
    319 	for (i = 0; i < SQ_NTXDESC; i++) {
    320 	    if (sc->sc_txmap[i] !=  NULL)
    321 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_txmap[i]);
    322 	}
    323 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cdmap);
    324 fail_3:
    325 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cdmap);
    326 fail_2:
    327 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t) sc->sc_control,
    328 				      sizeof(struct sq_control));
    329 fail_1:
    330 	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_ncdseg);
    331 fail_0:
    332 	return;
    333 }
    334 
    335 /* Set up data to get the interface up and running. */
    336 int
    337 sq_init(struct ifnet *ifp)
    338 {
    339 	int i;
    340 	u_int32_t reg;
    341 	struct sq_softc *sc = ifp->if_softc;
    342 
    343 	/* Cancel any in-progress I/O */
    344 	sq_stop(ifp, 0);
    345 
    346 	sc->sc_nextrx = 0;
    347 
    348 	sc->sc_nfreetx = SQ_NTXDESC;
    349 	sc->sc_nexttx = sc->sc_prevtx = 0;
    350 
    351 	SQ_TRACE(SQ_RESET, 0, 0, sc->sc_nfreetx);
    352 
    353 	/* Set into 8003 mode, bank 0 to program ethernet address */
    354 	bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCMD, TXCMD_BANK0);
    355 
    356 	/* Now write the address */
    357 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    358 	    bus_space_write_1(sc->sc_regt, sc->sc_regh, i, sc->sc_enaddr[i]);
    359 
    360 	/* Set up Seeq transmit command register */
    361 	bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCMD,
    362 						    TXCMD_IE_UFLOW |
    363 						    TXCMD_IE_COLL |
    364 						    TXCMD_IE_16COLL |
    365 						    TXCMD_IE_GOOD);
    366 
    367 	/* And the receive command register */
    368 	bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_RXCMD,
    369 						    RXCMD_REC_BROAD |
    370 						    RXCMD_IE_CRC |
    371 						    RXCMD_IE_DRIB |
    372 						    RXCMD_IE_SHORT |
    373 						    RXCMD_IE_END |
    374 						    RXCMD_IE_GOOD);
    375 
    376 	/* Set up HPC ethernet DMA config */
    377 	reg = bus_space_read_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_DMACFG);
    378 	bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_DMACFG,
    379 						    reg | 0xe000);
    380 
    381 	/* Pass the start of the receive ring to the HPC */
    382         bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_NDBP,
    383 						    SQ_CDRXADDR(sc, 0));
    384 
    385 	/* And turn on the HPC ethernet receive channel */
    386 	bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_CTL, 0x200);
    387 
    388         ifp->if_flags |= IFF_RUNNING;
    389 	ifp->if_flags &= ~IFF_OACTIVE;
    390 
    391 	return 0;
    392 }
    393 
    394 int
    395 sq_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    396 {
    397 	int s, error = 0;
    398 
    399 	s = splnet();
    400 
    401 	error = ether_ioctl(ifp, cmd, data);
    402 	if (error == ENETRESET) {
    403 		/*
    404 		 * Multicast list has changed; set the hardware filter
    405 		 * accordingly.
    406 		 */
    407 		error = 0;
    408 	}
    409 
    410 	splx(s);
    411 	return (error);
    412 }
    413 
    414 void
    415 sq_start(struct ifnet *ifp)
    416 {
    417 	struct sq_softc *sc = ifp->if_softc;
    418 	u_int32_t status;
    419 	struct mbuf *m0, *m;
    420 	bus_dmamap_t dmamap;
    421 	int err, totlen, nexttx, firsttx, lasttx, ofree, seg;
    422 
    423 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    424 		return;
    425 
    426 	/*
    427 	 * Remember the previous number of free descriptors and
    428 	 * the first descriptor we'll use.
    429 	 */
    430 	ofree = sc->sc_nfreetx;
    431 	firsttx = sc->sc_nexttx;
    432 
    433 	/*
    434 	 * Loop through the send queue, setting up transmit descriptors
    435 	 * until we drain the queue, or use up all available transmit
    436 	 * descriptors.
    437 	 */
    438 	while (sc->sc_nfreetx != 0) {
    439 		/*
    440 		 * Grab a packet off the queue.
    441 		 */
    442 		IFQ_POLL(&ifp->if_snd, m0);
    443 		if (m0 == NULL)
    444 			break;
    445 		m = NULL;
    446 
    447 		dmamap = sc->sc_txmap[sc->sc_nexttx];
    448 
    449 		/*
    450 		 * Load the DMA map.  If this fails, the packet either
    451 		 * didn't fit in the alloted number of segments, or we were
    452 		 * short on resources.  In this case, we'll copy and try
    453 		 * again.
    454 		 */
    455 		if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    456 						      BUS_DMA_NOWAIT) != 0) {
    457 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    458 			if (m == NULL) {
    459 				printf("%s: unable to allocate Tx mbuf\n",
    460 				    sc->sc_dev.dv_xname);
    461 				break;
    462 			}
    463 			if (m0->m_pkthdr.len > MHLEN) {
    464 				MCLGET(m, M_DONTWAIT);
    465 				if ((m->m_flags & M_EXT) == 0) {
    466 					printf("%s: unable to allocate Tx "
    467 					    "cluster\n", sc->sc_dev.dv_xname);
    468 					m_freem(m);
    469 					break;
    470 				}
    471 			}
    472 
    473 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
    474 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    475 
    476 			if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    477 						m, BUS_DMA_NOWAIT)) != 0) {
    478 				printf("%s: unable to load Tx buffer, "
    479 				    "error = %d\n", sc->sc_dev.dv_xname, err);
    480 				break;
    481 			}
    482 		}
    483 
    484 		/*
    485 		 * Ensure we have enough descriptors free to describe
    486 		 * the packet.
    487 		 */
    488 		if (dmamap->dm_nsegs > sc->sc_nfreetx) {
    489 			/*
    490 			 * Not enough free descriptors to transmit this
    491 			 * packet.  We haven't committed to anything yet,
    492 			 * so just unload the DMA map, put the packet
    493 			 * back on the queue, and punt.  Notify the upper
    494 			 * layer that there are no more slots left.
    495 			 *
    496 			 * XXX We could allocate an mbuf and copy, but
    497 			 * XXX it is worth it?
    498 			 */
    499 			ifp->if_flags |= IFF_OACTIVE;
    500 			bus_dmamap_unload(sc->sc_dmat, dmamap);
    501 			if (m != NULL)
    502 				m_freem(m);
    503 			break;
    504 		}
    505 
    506 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    507 		if (m != NULL) {
    508 			m_freem(m0);
    509 			m0 = m;
    510 		}
    511 
    512 		/*
    513 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    514 		 */
    515 
    516 		/* Sync the DMA map. */
    517 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    518 		    BUS_DMASYNC_PREWRITE);
    519 
    520 		/*
    521 		 * Initialize the transmit descriptors.
    522 		 */
    523 		for (nexttx = sc->sc_nexttx, seg = 0, totlen = 0;
    524 		     seg < dmamap->dm_nsegs;
    525 		     seg++, nexttx = SQ_NEXTTX(nexttx)) {
    526 			sc->sc_txdesc[nexttx].hdd_bufptr =
    527 					    dmamap->dm_segs[seg].ds_addr;
    528 			sc->sc_txdesc[nexttx].hdd_ctl =
    529 					    dmamap->dm_segs[seg].ds_len;
    530 			sc->sc_txdesc[nexttx].hdd_descptr=
    531 					    SQ_CDTXADDR(sc, SQ_NEXTTX(nexttx));
    532 			lasttx = nexttx;
    533 			totlen += dmamap->dm_segs[seg].ds_len;
    534 		}
    535 
    536 		/* Last descriptor gets end-of-packet */
    537 		sc->sc_txdesc[lasttx].hdd_ctl |= HDD_CTL_EOPACKET;
    538 
    539 		/* XXXrkb: if not EDLC, pad to min len manually */
    540 		if (totlen < ETHER_MIN_LEN) {
    541 		    sc->sc_txdesc[lasttx].hdd_ctl += (ETHER_MIN_LEN - totlen);
    542 		    totlen = ETHER_MIN_LEN;
    543 		}
    544 
    545 #if 0
    546 		printf("%s: transmit %d-%d, len %d\n", sc->sc_dev.dv_xname,
    547 						       sc->sc_nexttx, lasttx,
    548 						       totlen);
    549 #endif
    550 
    551 		if (ifp->if_flags & IFF_DEBUG) {
    552 			printf("     transmit chain:\n");
    553 			for (seg = sc->sc_nexttx;; seg = SQ_NEXTTX(seg)) {
    554 				printf("     descriptor %d:\n", seg);
    555 				printf("       hdd_bufptr:      0x%08x\n",
    556 					sc->sc_txdesc[seg].hdd_bufptr);
    557 				printf("       hdd_ctl: 0x%08x\n",
    558 					sc->sc_txdesc[seg].hdd_ctl);
    559 				printf("       hdd_descptr:      0x%08x\n",
    560 					sc->sc_txdesc[seg].hdd_descptr);
    561 
    562 				if (seg == lasttx)
    563 					break;
    564 			}
    565 		}
    566 
    567 		/* Sync the descriptors we're using. */
    568 		SQ_CDTXSYNC(sc, sc->sc_nexttx, dmamap->dm_nsegs,
    569 				BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    570 
    571 		/* Store a pointer to the packet so we can free it later */
    572 		sc->sc_txmbuf[sc->sc_nexttx] = m0;
    573 
    574 		/* Advance the tx pointer. */
    575 		sc->sc_nfreetx -= dmamap->dm_nsegs;
    576 		sc->sc_nexttx = nexttx;
    577 
    578 #if NBPFILTER > 0
    579 		/*
    580 		 * Pass the packet to any BPF listeners.
    581 		 */
    582 		if (ifp->if_bpf)
    583 			bpf_mtap(ifp->if_bpf, m0);
    584 #endif /* NBPFILTER > 0 */
    585 	}
    586 
    587 	/* All transmit descriptors used up, let upper layers know */
    588 	if (sc->sc_nfreetx == 0)
    589 		ifp->if_flags |= IFF_OACTIVE;
    590 
    591 	if (sc->sc_nfreetx != ofree) {
    592 #if 0
    593 		printf("%s: %d packets enqueued, first %d, INTR on %d\n",
    594 			    sc->sc_dev.dv_xname, lasttx - firsttx + 1,
    595 			    firsttx, lasttx);
    596 #endif
    597 
    598 		/*
    599 		 * Cause a transmit interrupt to happen on the
    600 		 * last packet we enqueued, mark it as the last
    601 		 * descriptor.
    602 		 */
    603 		sc->sc_txdesc[lasttx].hdd_ctl |= (HDD_CTL_INTR |
    604 						  HDD_CTL_EOCHAIN);
    605 		SQ_CDTXSYNC(sc, lasttx, 1,
    606 				BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    607 
    608 		/*
    609 		 * There is a potential race condition here if the HPC
    610 		 * DMA channel is active and we try and either update
    611 		 * the 'next descriptor' pointer in the HPC PIO space
    612 		 * or the 'next descriptor' pointer in a previous desc-
    613 		 * riptor.
    614 		 *
    615 		 * To avoid this, if the channel is active, we rely on
    616 		 * the transmit interrupt routine noticing that there
    617 		 * are more packets to send and restarting the HPC DMA
    618 		 * engine, rather than mucking with the DMA state here.
    619 		 */
    620 		status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
    621 						       HPC_ENETX_CTL);
    622 
    623 		if ((status & 0x200) != 0) {
    624 		    SQ_TRACE(SQ_ADD_TO_DMA, firsttx, status, sc->sc_nfreetx);
    625 
    626 		    sc->sc_txdesc[SQ_PREVTX(firsttx)].hdd_ctl &=
    627 						      	~HDD_CTL_EOCHAIN;
    628 		    SQ_CDTXSYNC(sc, SQ_PREVTX(firsttx),  1,
    629 				BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    630 		} else {
    631 		    SQ_TRACE(SQ_START_DMA, firsttx, status, sc->sc_nfreetx);
    632 
    633 		    bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
    634 				  HPC_ENETX_NDBP, SQ_CDTXADDR(sc, firsttx));
    635 
    636 		    /* Kick DMA channel into life */
    637 		    bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
    638 				      HPC_ENETX_CTL, 0x200);
    639 
    640 		    /* Set a watchdog timer in case the chip flakes out. */
    641 		    ifp->if_timer = 5;
    642 		}
    643 	}
    644 }
    645 
    646 void
    647 sq_stop(struct ifnet *ifp, int disable)
    648 {
    649 	int i;
    650 	struct sq_softc *sc = ifp->if_softc;
    651 
    652 	for (i =0; i < SQ_NTXDESC; i++) {
    653 		if (sc->sc_txmbuf[i] != NULL) {
    654 			bus_dmamap_unload(sc->sc_dmat, sc->sc_txmap[i]);
    655 			m_freem(sc->sc_txmbuf[i]);
    656 			sc->sc_txmbuf[i] = NULL;
    657 		}
    658 	}
    659 
    660 	/* Clear Seeq transmit/receive command registers */
    661 	bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCMD, 0);
    662 	bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_RXCMD, 0);
    663 
    664 	sq_reset(sc);
    665 
    666         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    667 	ifp->if_timer = 0;
    668 }
    669 
    670 /* Device timeout/watchdog routine. */
    671 void
    672 sq_watchdog(struct ifnet *ifp)
    673 {
    674 	u_int32_t status;
    675 	struct sq_softc *sc = ifp->if_softc;
    676 
    677 	status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETX_CTL);
    678 	log(LOG_ERR, "%s: device timeout (prev %d, next %d, free %d, "
    679 		     "status %08x)\n", sc->sc_dev.dv_xname, sc->sc_prevtx,
    680 				       sc->sc_nexttx, sc->sc_nfreetx, status);
    681 
    682 	sq_trace_dump(sc);
    683 
    684 	bzero(&sq_trace, sizeof(sq_trace));
    685 	sq_trace_idx = 0;
    686 
    687 	++ifp->if_oerrors;
    688 
    689 	sq_init(ifp);
    690 }
    691 
    692 void sq_trace_dump(struct sq_softc* sc)
    693 {
    694 	int i;
    695 
    696 	for(i = 0; i < sq_trace_idx; i++) {
    697 		printf("%s: [%d] action %d, buf %d, free %d, status %08x\n",
    698 			sc->sc_dev.dv_xname, i, sq_trace[i].action,
    699 			sq_trace[i].bufno, sq_trace[i].freebuf,
    700 			sq_trace[i].status);
    701 	}
    702 }
    703 
    704 static int
    705 sq_intr(void * arg)
    706 {
    707 	struct sq_softc *sc = arg;
    708 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    709 	int handled = 0;
    710 	u_int32_t stat;
    711 
    712         stat = bus_space_read_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_RESET);
    713 
    714 	if ((stat & 2) == 0) {
    715 		printf("%s: Unexpected interrupt!\n", sc->sc_dev.dv_xname);
    716 		return 0;
    717 	}
    718 
    719 	bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_RESET, 2);
    720 
    721 	/*
    722 	 * If the interface isn't running, the interrupt couldn't
    723 	 * possibly have come from us.
    724 	 */
    725 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    726 		return 0;
    727 
    728 	/* Always check for received packets */
    729 	if (sq_rxintr(sc) != 0)
    730 		handled++;
    731 
    732 	/* Only handle transmit interrupts if we actually sent something */
    733 	if (sc->sc_nfreetx < SQ_NTXDESC) {
    734 		sq_txintr(sc);
    735 		handled++;
    736 	}
    737 
    738 #if NRND > 0
    739 	if (handled)
    740 		rnd_add_uint32(&sc->sc_rnd_source, status);
    741 #endif
    742 	return (handled);
    743 }
    744 
    745 static int
    746 sq_rxintr(struct sq_softc *sc)
    747 {
    748 	int count = 0;
    749 	struct mbuf* m;
    750 	int i, framelen;
    751 	u_int8_t pktstat;
    752 	u_int32_t status;
    753 	int new_end, orig_end;
    754 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    755 
    756 	for(i = sc->sc_nextrx;; i = SQ_NEXTRX(i)) {
    757 	    SQ_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    758 
    759 	    /* If this is a CPU-owned buffer, we're at the end of the list */
    760 	    if (sc->sc_rxdesc[i].hdd_ctl & HDD_CTL_OWN) {
    761 		u_int32_t reg;
    762 
    763 		reg = bus_space_read_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_CTL);
    764 #if 0
    765 		printf("%s: rxintr: done at %d (ctl %08x)\n",
    766 				sc->sc_dev.dv_xname, i, reg);
    767 #endif
    768 		break;
    769 	    }
    770 
    771 	    count++;
    772 
    773 	    m = sc->sc_rxmbuf[i];
    774 	    framelen = m->m_ext.ext_size -
    775 			HDD_CTL_BYTECNT(sc->sc_rxdesc[i].hdd_ctl) - 3;
    776 
    777 	    /* Now sync the actual packet data */
    778 	    bus_dmamap_sync(sc->sc_dmat, sc->sc_rxmap[i], 0,
    779 			    sc->sc_rxmap[i]->dm_mapsize, BUS_DMASYNC_POSTREAD);
    780 
    781 	    pktstat = *((u_int8_t*)m->m_data + framelen + 2);
    782 
    783 	    if ((pktstat & RXSTAT_GOOD) == 0) {
    784 		/* XXXrkb: increment error counters based on error type */
    785 		printf("%s: rxintr: bad packet, buf %d, stat %02x (dexc ctl "
    786 		       "%08x)\n", sc->sc_dev.dv_xname, i, pktstat,
    787 				  sc->sc_rxdesc[i].hdd_ctl);
    788 
    789 	    	sq_dump_buffer((u_int32_t)m->m_data + 2, framelen);
    790 		SQ_INIT_RXDESC(sc, i);
    791 		continue;
    792 	    }
    793 
    794 	    if (sq_add_rxbuf(sc, i) != 0) {
    795 		ifp->if_ierrors++;
    796 		SQ_INIT_RXDESC(sc, i);
    797 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rxmap[i], 0,
    798 				sc->sc_rxmap[i]->dm_mapsize,
    799 				BUS_DMASYNC_PREREAD);
    800 		continue;
    801 	    }
    802 
    803 
    804 	    m->m_data += 2;
    805 	    m->m_pkthdr.rcvif = ifp;
    806 	    m->m_pkthdr.len = m->m_len = framelen;
    807 
    808 	    ifp->if_ipackets++;
    809 
    810 #if 0
    811 	    printf("%s: sq_rxintr: buf %d len %d\n", sc->sc_dev.dv_xname,
    812 						     i, framelen);
    813 #endif
    814 
    815 #if NBPFILTER > 0
    816 	    if (ifp->if_bpf)
    817 		    bpf_mtap(ifp->if_bpf, m);
    818 #endif
    819 	    (*ifp->if_input)(ifp, m);
    820 	}
    821 
    822 
    823 	/* If anything happened, move ring start/end pointers to new spot */
    824 	if (i != sc->sc_nextrx) {
    825 	    new_end = SQ_PREVRX(i);
    826 	    sc->sc_rxdesc[new_end].hdd_ctl |= HDD_CTL_EOCHAIN;
    827 	    SQ_CDRXSYNC(sc, new_end, BUS_DMASYNC_PREREAD |
    828 				     BUS_DMASYNC_PREWRITE);
    829 
    830 	    orig_end = SQ_PREVRX(sc->sc_nextrx);
    831 	    sc->sc_rxdesc[orig_end].hdd_ctl &= ~HDD_CTL_EOCHAIN;
    832 	    SQ_CDRXSYNC(sc, orig_end, BUS_DMASYNC_PREREAD |
    833 				      BUS_DMASYNC_PREWRITE);
    834 
    835 	    sc->sc_nextrx = i;
    836 	}
    837 
    838 	status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
    839 					       HPC_ENETR_CTL);
    840 
    841 	/* If receive channel is stopped, restart it... */
    842 	if ((status & 0x200) == 0) {
    843 	    /* Pass the start of the receive ring to the HPC */
    844 	    bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
    845 			      HPC_ENETR_NDBP, SQ_CDRXADDR(sc, sc->sc_nextrx));
    846 
    847 	    /* And turn on the HPC ethernet receive channel */
    848 	    bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_CTL, 0x200);
    849 	}
    850 
    851 	return count;
    852 }
    853 
    854 static int
    855 sq_txintr(struct sq_softc *sc)
    856 {
    857 	int i;
    858 	u_int32_t status;
    859 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    860 
    861 	status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETX_CTL);
    862 
    863 	SQ_TRACE(SQ_TXINTR_ENTER, sc->sc_prevtx, status, sc->sc_nfreetx);
    864 
    865 	if ((status & (0x200 | TXSTAT_GOOD)) == 0) {
    866 		if (status & TXSTAT_COLL)
    867 		    ifp->if_collisions++;
    868 
    869 		if (status & TXSTAT_UFLOW) {
    870 		    printf("%s: transmit underflow\n", sc->sc_dev.dv_xname);
    871 		    ifp->if_oerrors++;
    872 		}
    873 
    874 		if (status & TXSTAT_16COLL) {
    875 		    printf("%s: max collisions reached\n", sc->sc_dev.dv_xname);
    876 		    ifp->if_oerrors++;
    877 		    ifp->if_collisions += 16;
    878 		}
    879 	}
    880 
    881 	i = sc->sc_prevtx;
    882 	while (sc->sc_nfreetx < SQ_NTXDESC) {
    883 		SQ_CDTXSYNC(sc, i, sc->sc_txmap[i]->dm_nsegs,
    884 				BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    885 
    886 		/* If not yet transmitted, try and start DMA engine again */
    887 		if ((sc->sc_txdesc[i].hdd_ctl & HDD_CTL_XMITDONE) == 0) {
    888 		    status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
    889 							   HPC_ENETX_CTL);
    890 
    891 		    if ((status & 0x200) == 0) {
    892 			SQ_TRACE(SQ_RESTART_DMA, i, status, sc->sc_nfreetx);
    893 
    894 			bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
    895 					  HPC_ENETX_NDBP, SQ_CDTXADDR(sc, i));
    896 
    897 			/* Kick DMA channel into life */
    898 			bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
    899 					  HPC_ENETX_CTL, 0x200);
    900 
    901 			/* Set a watchdog timer in case the chip flakes out. */
    902 			ifp->if_timer = 5;
    903 		    } else {
    904 			SQ_TRACE(SQ_TXINTR_BUSY, i, status, sc->sc_nfreetx);
    905 		    }
    906 		    break;
    907 		}
    908 
    909 		/* Sync the packet data, unload DMA map, free mbuf */
    910 		bus_dmamap_sync(sc->sc_dmat, sc->sc_txmap[i], 0,
    911 				sc->sc_txmap[i]->dm_mapsize,
    912 				BUS_DMASYNC_POSTWRITE);
    913 		bus_dmamap_unload(sc->sc_dmat, sc->sc_txmap[i]);
    914 		m_freem(sc->sc_txmbuf[i]);
    915 		sc->sc_txmbuf[i] = NULL;
    916 
    917 		ifp->if_opackets++;
    918 		sc->sc_nfreetx++;
    919 
    920 		SQ_TRACE(SQ_DONE_DMA, i, status, sc->sc_nfreetx);
    921 		i = SQ_NEXTTX(i);
    922 	}
    923 
    924 	/* prevtx now points to next xmit packet not yet finished */
    925 	sc->sc_prevtx = i;
    926 
    927 	/* If we have buffers free, let upper layers know */
    928 	if (sc->sc_nfreetx > 0)
    929 	    ifp->if_flags &= ~IFF_OACTIVE;
    930 
    931 	/* If all packets have left the coop, cancel watchdog */
    932 	if (sc->sc_nfreetx == SQ_NTXDESC)
    933 	    ifp->if_timer = 0;
    934 
    935 	SQ_TRACE(SQ_TXINTR_EXIT, sc->sc_prevtx, status, sc->sc_nfreetx);
    936     	sq_start(ifp);
    937 
    938 	return 1;
    939 }
    940 
    941 
    942 void
    943 sq_reset(struct sq_softc *sc)
    944 {
    945 	/* Stop HPC dma channels */
    946 	bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_CTL, 0);
    947 	bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETX_CTL, 0);
    948 
    949         bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_RESET, 3);
    950         delay(20);
    951         bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_RESET, 0);
    952 }
    953 
    954 /* sq_add_rxbuf: Add a receive buffer to the indicated descriptor.  */
    955 int
    956 sq_add_rxbuf(struct sq_softc *sc, int idx)
    957 {
    958 	int err;
    959 	struct mbuf *m;
    960 
    961 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    962 	if (m == NULL)
    963 		return (ENOBUFS);
    964 
    965 	MCLGET(m, M_DONTWAIT);
    966 	if ((m->m_flags & M_EXT) == 0) {
    967 		m_freem(m);
    968 		return (ENOBUFS);
    969 	}
    970 
    971 	if (sc->sc_rxmbuf[idx] != NULL)
    972 		bus_dmamap_unload(sc->sc_dmat, sc->sc_rxmap[idx]);
    973 
    974 	sc->sc_rxmbuf[idx] = m;
    975 
    976 	if ((err = bus_dmamap_load(sc->sc_dmat, sc->sc_rxmap[idx],
    977 				   m->m_ext.ext_buf, m->m_ext.ext_size,
    978 				   NULL, BUS_DMA_NOWAIT)) != 0) {
    979 		printf("%s: can't load rx DMA map %d, error = %d\n",
    980 		    sc->sc_dev.dv_xname, idx, err);
    981 		panic("sq_add_rxbuf");	/* XXX */
    982 	}
    983 
    984 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rxmap[idx], 0,
    985 			sc->sc_rxmap[idx]->dm_mapsize, BUS_DMASYNC_PREREAD);
    986 
    987 	SQ_INIT_RXDESC(sc, idx);
    988 
    989 	return 0;
    990 }
    991 
    992 void
    993 sq_dump_buffer(u_int32_t addr, u_int32_t len)
    994 {
    995 	int i;
    996 	u_char* physaddr = (char*) MIPS_PHYS_TO_KSEG1((caddr_t)addr);
    997 
    998 	if (len == 0)
    999 		return;
   1000 
   1001 	printf("%p: ", physaddr);
   1002 
   1003 	for(i = 0; i < len; i++) {
   1004 		printf("%02x ", *(physaddr + i) & 0xff);
   1005 		if ((i % 16) ==  15 && i != len - 1)
   1006 		    printf("\n%p: ", physaddr + i);
   1007 	}
   1008 
   1009 	printf("\n");
   1010 }
   1011 
   1012 
   1013 void
   1014 enaddr_aton(const char* str, u_int8_t* eaddr)
   1015 {
   1016 	int i;
   1017 	char c;
   1018 
   1019 	for(i = 0; i < ETHER_ADDR_LEN; i++) {
   1020 		if (*str == ':')
   1021 			str++;
   1022 
   1023 		c = *str++;
   1024 		if (isdigit(c)) {
   1025 			eaddr[i] = (c - '0');
   1026 		} else if (isxdigit(c)) {
   1027 			eaddr[i] = (toupper(c) + 10 - 'A');
   1028 		}
   1029 
   1030 		c = *str++;
   1031 		if (isdigit(c)) {
   1032 			eaddr[i] = (eaddr[i] << 4) | (c - '0');
   1033 		} else if (isxdigit(c)) {
   1034 			eaddr[i] = (eaddr[i] << 4) | (toupper(c) + 10 - 'A');
   1035 		}
   1036 	}
   1037 }
   1038 
   1039