Home | History | Annotate | Line # | Download | only in ic
i82596.c revision 1.19.4.2
      1  1.19.4.2      yamt /* $NetBSD: i82596.c,v 1.19.4.2 2010/03/11 15:03:31 yamt Exp $ */
      2       1.1     jkunz 
      3       1.1     jkunz /*
      4       1.1     jkunz  * Copyright (c) 2003 Jochen Kunz.
      5       1.1     jkunz  * All rights reserved.
      6       1.1     jkunz  *
      7       1.1     jkunz  * Redistribution and use in source and binary forms, with or without
      8       1.1     jkunz  * modification, are permitted provided that the following conditions
      9       1.1     jkunz  * are met:
     10       1.1     jkunz  * 1. Redistributions of source code must retain the above copyright
     11       1.1     jkunz  *    notice, this list of conditions and the following disclaimer.
     12       1.1     jkunz  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1     jkunz  *    notice, this list of conditions and the following disclaimer in the
     14       1.1     jkunz  *    documentation and/or other materials provided with the distribution.
     15       1.1     jkunz  * 3. The name of Jochen Kunz may not be used to endorse or promote
     16       1.1     jkunz  *    products derived from this software without specific prior
     17       1.1     jkunz  *    written permission.
     18       1.1     jkunz  *
     19       1.1     jkunz  * THIS SOFTWARE IS PROVIDED BY JOCHEN KUNZ
     20       1.1     jkunz  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1     jkunz  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1     jkunz  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JOCHEN KUNZ
     23       1.1     jkunz  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1     jkunz  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1     jkunz  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1     jkunz  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1     jkunz  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1     jkunz  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1     jkunz  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1     jkunz  */
     31       1.1     jkunz 
     32       1.1     jkunz /*
     33      1.14     skrll  * Driver for the Intel i82596CA and i82596DX/SX 10MBit/s Ethernet chips.
     34      1.14     skrll  *
     35       1.1     jkunz  * It operates the i82596 in 32-Bit Linear Mode, opposed to the old i82586
     36       1.8     perry  * ie(4) driver (src/sys/dev/ic/i82586.c), that degrades the i82596 to
     37       1.1     jkunz  * i82586 compatibility mode.
     38      1.13     skrll  *
     39      1.14     skrll  * Documentation about these chips can be found at
     40      1.14     skrll  *
     41      1.14     skrll  *	http://developer.intel.com/design/network/datashts/290218.htm
     42      1.14     skrll  *	http://developer.intel.com/design/network/datashts/290219.htm
     43       1.1     jkunz  */
     44       1.1     jkunz 
     45       1.1     jkunz #include <sys/cdefs.h>
     46  1.19.4.2      yamt __KERNEL_RCSID(0, "$NetBSD: i82596.c,v 1.19.4.2 2010/03/11 15:03:31 yamt Exp $");
     47       1.1     jkunz 
     48       1.1     jkunz /* autoconfig and device stuff */
     49       1.1     jkunz #include <sys/param.h>
     50       1.1     jkunz #include <sys/device.h>
     51       1.1     jkunz #include <sys/conf.h>
     52       1.1     jkunz #include "locators.h"
     53       1.1     jkunz #include "ioconf.h"
     54       1.1     jkunz 
     55       1.1     jkunz /* bus_space / bus_dma etc. */
     56      1.18        ad #include <sys/bus.h>
     57      1.18        ad #include <sys/intr.h>
     58       1.1     jkunz 
     59       1.1     jkunz /* general system data and functions */
     60       1.1     jkunz #include <sys/systm.h>
     61       1.1     jkunz #include <sys/ioctl.h>
     62       1.1     jkunz 
     63       1.1     jkunz /* tsleep / sleep / wakeup */
     64       1.1     jkunz #include <sys/proc.h>
     65       1.1     jkunz /* hz for above */
     66       1.1     jkunz #include <sys/kernel.h>
     67       1.1     jkunz 
     68       1.1     jkunz /* network stuff */
     69       1.1     jkunz #include <net/if.h>
     70       1.1     jkunz #include <net/if_dl.h>
     71       1.1     jkunz #include <net/if_media.h>
     72       1.1     jkunz #include <net/if_ether.h>
     73       1.1     jkunz #include <sys/socket.h>
     74       1.1     jkunz #include <sys/mbuf.h>
     75       1.1     jkunz 
     76       1.1     jkunz #include <net/bpf.h>
     77       1.1     jkunz 
     78       1.1     jkunz #include <dev/ic/i82596reg.h>
     79       1.1     jkunz #include <dev/ic/i82596var.h>
     80       1.1     jkunz 
     81       1.1     jkunz /* Supported chip variants */
     82      1.10     skrll const char *i82596_typenames[] = { "unknown", "DX/SX", "CA" };
     83       1.1     jkunz 
     84       1.1     jkunz /* media change and status callback */
     85       1.1     jkunz static int iee_mediachange(struct ifnet *);
     86       1.1     jkunz static void iee_mediastatus(struct ifnet *, struct ifmediareq *);
     87       1.1     jkunz 
     88       1.1     jkunz /* interface routines to upper protocols */
     89       1.1     jkunz static void iee_start(struct ifnet *);			/* initiate output */
     90      1.15  christos static int iee_ioctl(struct ifnet *, u_long, void *);	/* ioctl routine */
     91       1.1     jkunz static int iee_init(struct ifnet *);			/* init routine */
     92       1.1     jkunz static void iee_stop(struct ifnet *, int);		/* stop routine */
     93       1.1     jkunz static void iee_watchdog(struct ifnet *);		/* timer routine */
     94       1.1     jkunz 
     95       1.1     jkunz /* internal helper functions */
     96       1.7   tsutsui static void iee_cb_setup(struct iee_softc *, uint32_t);
     97       1.1     jkunz 
     98       1.1     jkunz /*
     99      1.13     skrll  * Things a MD frontend has to provide:
    100      1.13     skrll  *
    101      1.13     skrll  * The functions via function pointers in the softc:
    102      1.13     skrll  *	int (*sc_iee_cmd)(struct iee_softc *sc, uint32_t cmd);
    103      1.13     skrll  *	int (*sc_iee_reset)(struct iee_softc *sc);
    104      1.13     skrll  *	void (*sc_mediastatus)(struct ifnet *, struct ifmediareq *);
    105      1.13     skrll  *	int (*sc_mediachange)(struct ifnet *);
    106      1.13     skrll  *
    107      1.13     skrll  * sc_iee_cmd(): send a command to the i82596 by writing the cmd parameter
    108      1.13     skrll  *	to the SCP cmd word and issuing a Channel Attention.
    109      1.13     skrll  * sc_iee_reset(): initiate a reset, supply the address of the SCP to the
    110      1.13     skrll  *	chip, wait for the chip to initialize and ACK interrupts that
    111      1.13     skrll  *	this may have caused by calling (sc->sc_iee_cmd)(sc, IEE_SCB_ACK);
    112      1.13     skrll  * This functions must carefully bus_dmamap_sync() all data they have touched!
    113      1.13     skrll  *
    114      1.13     skrll  * sc_mediastatus() and sc_mediachange() are just MD hooks to the according
    115      1.13     skrll  * MI functions. The MD frontend may set this pointers to NULL when they
    116      1.13     skrll  * are not needed.
    117      1.13     skrll  *
    118      1.13     skrll  * sc->sc_type has to be set to I82596_UNKNOWN or I82596_DX or I82596_CA.
    119      1.13     skrll  * This is for printing out the correct chip type at attach time only. The
    120      1.13     skrll  * MI backend doesn't distinguish different chip types when programming
    121      1.13     skrll  * the chip.
    122      1.13     skrll  *
    123  1.19.4.1      yamt  * IEE_NEED_SWAP in sc->sc_flags has to be cleared on little endian hardware
    124  1.19.4.1      yamt  * and set on big endian hardware, when endianess conversion is not done
    125  1.19.4.1      yamt  * by the bus attachment but done by i82596 chip itself.
    126  1.19.4.1      yamt  * Usually you need to set IEE_NEED_SWAP on big endian machines
    127  1.19.4.1      yamt  * where the hardware (the LE/~BE pin) is configured as BE mode.
    128  1.19.4.1      yamt  *
    129  1.19.4.1      yamt  * If the chip is configured as BE mode, all 8 bit (byte) and 16 bit (word)
    130  1.19.4.1      yamt  * entities can be written in big endian. But Rev A chip doesn't support
    131  1.19.4.1      yamt  * 32 bit (dword) entities with big endian byte ordering, so we have to
    132  1.19.4.1      yamt  * treat all 32 bit (dword) entities as two 16 bit big endian entities.
    133  1.19.4.1      yamt  * Rev B and C chips support big endian byte ordering for 32 bit entities,
    134  1.19.4.1      yamt  * and this new feature is enabled by IEE_SYSBUS_BE in the sysbus byte.
    135  1.19.4.1      yamt  *
    136  1.19.4.1      yamt  * With the IEE_SYSBUS_BE feature, all 32 bit address ponters are
    137  1.19.4.1      yamt  * treated as true 32 bit entities but the SCB absolute address and
    138  1.19.4.1      yamt  * statistical counters are still treated as two 16 bit big endian entities,
    139  1.19.4.1      yamt  * so we have to always swap high and low words for these entities.
    140  1.19.4.1      yamt  * IEE_SWAP32() should be used for the SCB address and statistical counters,
    141  1.19.4.1      yamt  * and IEE_SWAPA32() should be used for other 32 bit pointers in the shmem.
    142  1.19.4.1      yamt  *
    143  1.19.4.1      yamt  * IEE_REV_A flag must be set in sc->sc_flags if the IEE_SYSBUS_BE feature
    144  1.19.4.1      yamt  * is disabled even on big endian machines for the old Rev A chip in backend.
    145      1.13     skrll  *
    146      1.13     skrll  * sc->sc_cl_align must be set to 1 or to the cache line size. When set to
    147      1.13     skrll  * 1 no special alignment of DMA descriptors is done. If sc->sc_cl_align != 1
    148      1.13     skrll  * it forces alignment of the data structures in the shared memory to a multiple
    149      1.13     skrll  * of sc->sc_cl_align. This is needed on archs like hp700 that have non DMA
    150      1.13     skrll  * I/O coherent caches and are unable to map the shared memory uncachable.
    151      1.13     skrll  * (At least pre PA7100LC CPUs are unable to map memory uncachable.)
    152      1.13     skrll  *
    153  1.19.4.1      yamt  * The MD frontend also has to set sc->sc_cl_align and sc->sc_sysbus
    154  1.19.4.1      yamt  * to allocate and setup shared DMA memory in MI iee_attach().
    155  1.19.4.1      yamt  * All communication with the chip is done via this shared memory.
    156  1.19.4.1      yamt  * This memory is mapped with BUS_DMA_COHERENT so it will be uncached
    157  1.19.4.1      yamt  * if possible for archs with non DMA I/O coherent caches.
    158  1.19.4.1      yamt  * The base of the memory needs to be aligned to an even address
    159  1.19.4.1      yamt  * if sc->sc_cl_align == 1 and aligned to a cache line if sc->sc_cl_align != 1.
    160  1.19.4.1      yamt  * Each descriptor offsets are calculated in iee_attach() to handle this.
    161      1.13     skrll  *
    162      1.13     skrll  * An interrupt with iee_intr() as handler must be established.
    163      1.13     skrll  *
    164      1.13     skrll  * Call void iee_attach(struct iee_softc *sc, uint8_t *ether_address,
    165      1.13     skrll  * int *media, int nmedia, int defmedia); when everything is set up. First
    166      1.13     skrll  * parameter is a pointer to the MI softc, ether_address is an array that
    167      1.13     skrll  * contains the ethernet address. media is an array of the media types
    168      1.13     skrll  * provided by the hardware. The members of this array are supplied to
    169      1.13     skrll  * ifmedia_add() in sequence. nmedia is the count of elements in media.
    170      1.13     skrll  * defmedia is the default media that is set via ifmedia_set().
    171      1.13     skrll  * nmedia and defmedia are ignored when media == NULL.
    172      1.13     skrll  *
    173      1.13     skrll  * The MD backend may call iee_detach() to detach the device.
    174      1.13     skrll  *
    175      1.13     skrll  * See sys/arch/hp700/gsc/if_iee_gsc.c for an example.
    176      1.13     skrll  */
    177       1.1     jkunz 
    178       1.1     jkunz 
    179       1.1     jkunz /*
    180      1.13     skrll  * How frame reception is done:
    181      1.13     skrll  * Each Receive Frame Descriptor has one associated Receive Buffer Descriptor.
    182      1.13     skrll  * Each RBD points to the data area of an mbuf cluster. The RFDs are linked
    183      1.13     skrll  * together in a circular list. sc->sc_rx_done is the count of RFDs in the
    184      1.13     skrll  * list already processed / the number of the RFD that has to be checked for
    185      1.13     skrll  * a new frame first at the next RX interrupt. Upon successful reception of
    186      1.13     skrll  * a frame the mbuf cluster is handled to upper protocol layers, a new mbuf
    187      1.13     skrll  * cluster is allocated and the RFD / RBD are reinitialized accordingly.
    188      1.13     skrll  *
    189  1.19.4.1      yamt  * When a RFD list overrun occurred the whole RFD and RBD lists are
    190  1.19.4.1      yamt  * reinitialized and frame reception is started again.
    191      1.13     skrll  */
    192       1.1     jkunz int
    193       1.1     jkunz iee_intr(void *intarg)
    194       1.1     jkunz {
    195       1.1     jkunz 	struct iee_softc *sc = intarg;
    196       1.1     jkunz 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    197       1.1     jkunz 	struct iee_rfd *rfd;
    198       1.1     jkunz 	struct iee_rbd *rbd;
    199       1.1     jkunz 	bus_dmamap_t rx_map;
    200       1.1     jkunz 	struct mbuf *rx_mbuf;
    201       1.1     jkunz 	struct mbuf *new_mbuf;
    202       1.1     jkunz 	int scb_status;
    203       1.1     jkunz 	int scb_cmd;
    204       1.6   tsutsui 	int n, col;
    205  1.19.4.1      yamt 	uint16_t status, count, cmd;
    206       1.1     jkunz 
    207       1.1     jkunz 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    208       1.1     jkunz 		(sc->sc_iee_cmd)(sc, IEE_SCB_ACK);
    209      1.19   tsutsui 		return 1;
    210       1.1     jkunz 	}
    211  1.19.4.1      yamt 	IEE_SCBSYNC(sc, BUS_DMASYNC_POSTREAD);
    212  1.19.4.1      yamt 	scb_status = SC_SCB(sc)->scb_status;
    213  1.19.4.1      yamt 	scb_cmd = SC_SCB(sc)->scb_cmd;
    214  1.19.4.1      yamt 	for (;;) {
    215  1.19.4.1      yamt 		rfd = SC_RFD(sc, sc->sc_rx_done);
    216  1.19.4.1      yamt 		IEE_RFDSYNC(sc, sc->sc_rx_done,
    217  1.19.4.1      yamt 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    218  1.19.4.1      yamt 		status = rfd->rfd_status;
    219  1.19.4.1      yamt 		if ((status & IEE_RFD_C) == 0) {
    220  1.19.4.1      yamt 			IEE_RFDSYNC(sc, sc->sc_rx_done, BUS_DMASYNC_PREREAD);
    221  1.19.4.1      yamt 			break;
    222  1.19.4.1      yamt 		}
    223  1.19.4.1      yamt 		rfd->rfd_status = 0;
    224  1.19.4.1      yamt 		IEE_RFDSYNC(sc, sc->sc_rx_done,
    225  1.19.4.1      yamt 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    226  1.19.4.1      yamt 
    227       1.1     jkunz 		/* At least one packet was received. */
    228       1.1     jkunz 		rx_map = sc->sc_rx_map[sc->sc_rx_done];
    229       1.1     jkunz 		rx_mbuf = sc->sc_rx_mbuf[sc->sc_rx_done];
    230  1.19.4.1      yamt 		IEE_RBDSYNC(sc, (sc->sc_rx_done + IEE_NRFD - 1) % IEE_NRFD,
    231  1.19.4.1      yamt 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    232  1.19.4.1      yamt 		SC_RBD(sc, (sc->sc_rx_done + IEE_NRFD - 1) % IEE_NRFD)->rbd_size
    233       1.1     jkunz 		    &= ~IEE_RBD_EL;
    234  1.19.4.1      yamt 		IEE_RBDSYNC(sc, (sc->sc_rx_done + IEE_NRFD - 1) % IEE_NRFD,
    235  1.19.4.1      yamt 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    236  1.19.4.1      yamt 		rbd = SC_RBD(sc, sc->sc_rx_done);
    237  1.19.4.1      yamt 		IEE_RBDSYNC(sc, sc->sc_rx_done,
    238  1.19.4.1      yamt 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    239  1.19.4.1      yamt 		count = rbd->rbd_count;
    240  1.19.4.1      yamt 		if ((status & IEE_RFD_OK) == 0
    241  1.19.4.1      yamt 		    || (count & IEE_RBD_EOF) == 0
    242  1.19.4.1      yamt 		    || (count & IEE_RBD_F) == 0){
    243       1.1     jkunz 			/* Receive error, skip frame and reuse buffer. */
    244       1.1     jkunz 			rbd->rbd_count = 0;
    245       1.1     jkunz 			rbd->rbd_size = IEE_RBD_EL | rx_map->dm_segs[0].ds_len;
    246  1.19.4.1      yamt 			IEE_RBDSYNC(sc, sc->sc_rx_done,
    247  1.19.4.1      yamt 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    248       1.1     jkunz 			printf("%s: iee_intr: receive error %d, rfd_status="
    249      1.19   tsutsui 			    "0x%.4x, rfd_count=0x%.4x\n",
    250      1.19   tsutsui 			    device_xname(sc->sc_dev),
    251  1.19.4.1      yamt 			    ++sc->sc_rx_err, status, count);
    252       1.1     jkunz 			sc->sc_rx_done = (sc->sc_rx_done + 1) % IEE_NRFD;
    253       1.1     jkunz 			continue;
    254       1.1     jkunz 		}
    255  1.19.4.1      yamt 		bus_dmamap_sync(sc->sc_dmat, rx_map, 0, rx_map->dm_mapsize,
    256       1.1     jkunz 		    BUS_DMASYNC_POSTREAD);
    257       1.8     perry 		rx_mbuf->m_pkthdr.len = rx_mbuf->m_len =
    258  1.19.4.1      yamt 		    count & IEE_RBD_COUNT;
    259       1.1     jkunz 		rx_mbuf->m_pkthdr.rcvif = ifp;
    260       1.1     jkunz 		MGETHDR(new_mbuf, M_DONTWAIT, MT_DATA);
    261       1.1     jkunz 		if (new_mbuf == NULL) {
    262       1.1     jkunz 			printf("%s: iee_intr: can't allocate mbuf\n",
    263      1.19   tsutsui 			    device_xname(sc->sc_dev));
    264       1.1     jkunz 			break;
    265       1.1     jkunz 		}
    266       1.1     jkunz 		MCLAIM(new_mbuf, &sc->sc_ethercom.ec_rx_mowner);
    267       1.1     jkunz 		MCLGET(new_mbuf, M_DONTWAIT);
    268       1.1     jkunz 		if ((new_mbuf->m_flags & M_EXT) == 0) {
    269       1.8     perry 			printf("%s: iee_intr: can't alloc mbuf cluster\n",
    270      1.19   tsutsui 			    device_xname(sc->sc_dev));
    271       1.1     jkunz 			m_freem(new_mbuf);
    272       1.1     jkunz 			break;
    273       1.1     jkunz 		}
    274       1.1     jkunz 		bus_dmamap_unload(sc->sc_dmat, rx_map);
    275  1.19.4.1      yamt 		new_mbuf->m_len = new_mbuf->m_pkthdr.len = MCLBYTES - 2;
    276  1.19.4.1      yamt 		new_mbuf->m_data += 2;
    277  1.19.4.1      yamt 		if (bus_dmamap_load_mbuf(sc->sc_dmat, rx_map,
    278  1.19.4.1      yamt 		    new_mbuf, BUS_DMA_READ | BUS_DMA_NOWAIT) != 0)
    279       1.1     jkunz 			panic("%s: iee_intr: can't load RX DMA map\n",
    280      1.19   tsutsui 			    device_xname(sc->sc_dev));
    281       1.1     jkunz 		bus_dmamap_sync(sc->sc_dmat, rx_map, 0,
    282  1.19.4.1      yamt 		    rx_map->dm_mapsize, BUS_DMASYNC_PREREAD);
    283       1.1     jkunz 		if (ifp->if_bpf != 0)
    284  1.19.4.2      yamt 			bpf_ops->bpf_mtap(ifp->if_bpf, rx_mbuf);
    285       1.1     jkunz 		(*ifp->if_input)(ifp, rx_mbuf);
    286       1.1     jkunz 		ifp->if_ipackets++;
    287       1.1     jkunz 		sc->sc_rx_mbuf[sc->sc_rx_done] = new_mbuf;
    288       1.1     jkunz 		rbd->rbd_count = 0;
    289       1.1     jkunz 		rbd->rbd_size = IEE_RBD_EL | rx_map->dm_segs[0].ds_len;
    290  1.19.4.1      yamt 		rbd->rbd_rb_addr = IEE_SWAPA32(rx_map->dm_segs[0].ds_addr);
    291  1.19.4.1      yamt 		IEE_RBDSYNC(sc, sc->sc_rx_done,
    292  1.19.4.1      yamt 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    293       1.1     jkunz 		sc->sc_rx_done = (sc->sc_rx_done + 1) % IEE_NRFD;
    294       1.1     jkunz 	}
    295       1.1     jkunz 	if ((scb_status & IEE_SCB_RUS) == IEE_SCB_RUS_NR1
    296       1.1     jkunz 	    || (scb_status & IEE_SCB_RUS) == IEE_SCB_RUS_NR2
    297       1.1     jkunz 	    || (scb_status & IEE_SCB_RUS) == IEE_SCB_RUS_NR3) {
    298       1.1     jkunz 		/* Receive Overrun, reinit receive ring buffer. */
    299       1.1     jkunz 		for (n = 0 ; n < IEE_NRFD ; n++) {
    300  1.19.4.1      yamt 			rfd = SC_RFD(sc, n);
    301  1.19.4.1      yamt 			rbd = SC_RBD(sc, n);
    302  1.19.4.1      yamt 			rfd->rfd_cmd = IEE_RFD_SF;
    303  1.19.4.1      yamt 			rfd->rfd_link_addr =
    304  1.19.4.1      yamt 			    IEE_SWAPA32(IEE_PHYS_SHMEM(sc->sc_rfd_off
    305  1.19.4.1      yamt 			    + sc->sc_rfd_sz * ((n + 1) % IEE_NRFD)));
    306  1.19.4.1      yamt 			rbd->rbd_next_rbd =
    307  1.19.4.1      yamt 			    IEE_SWAPA32(IEE_PHYS_SHMEM(sc->sc_rbd_off
    308  1.19.4.1      yamt 			    + sc->sc_rbd_sz * ((n + 1) % IEE_NRFD)));
    309  1.19.4.1      yamt 			rbd->rbd_size = IEE_RBD_EL |
    310       1.1     jkunz 			    sc->sc_rx_map[n]->dm_segs[0].ds_len;
    311  1.19.4.1      yamt 			rbd->rbd_rb_addr =
    312  1.19.4.1      yamt 			    IEE_SWAPA32(sc->sc_rx_map[n]->dm_segs[0].ds_addr);
    313       1.1     jkunz 		}
    314  1.19.4.1      yamt 		SC_RFD(sc, 0)->rfd_rbd_addr =
    315  1.19.4.1      yamt 		    IEE_SWAPA32(IEE_PHYS_SHMEM(sc->sc_rbd_off));
    316       1.1     jkunz 		sc->sc_rx_done = 0;
    317  1.19.4.1      yamt 		bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, sc->sc_rfd_off,
    318  1.19.4.1      yamt 		    sc->sc_rfd_sz * IEE_NRFD + sc->sc_rbd_sz * IEE_NRFD,
    319  1.19.4.1      yamt 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    320       1.1     jkunz 		(sc->sc_iee_cmd)(sc, IEE_SCB_RUC_ST);
    321       1.8     perry 		printf("%s: iee_intr: receive ring buffer overrun\n",
    322      1.19   tsutsui 		    device_xname(sc->sc_dev));
    323       1.2     jkunz 	}
    324       1.1     jkunz 
    325  1.19.4.1      yamt 	if (sc->sc_next_cb != 0) {
    326  1.19.4.1      yamt 		IEE_CBSYNC(sc, sc->sc_next_cb - 1,
    327  1.19.4.1      yamt 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    328  1.19.4.1      yamt 		status = SC_CB(sc, sc->sc_next_cb - 1)->cb_status;
    329  1.19.4.1      yamt 		IEE_CBSYNC(sc, sc->sc_next_cb - 1,
    330  1.19.4.1      yamt 		    BUS_DMASYNC_PREREAD);
    331  1.19.4.1      yamt 		if ((status & IEE_CB_C) != 0) {
    332  1.19.4.1      yamt 			/* CMD list finished */
    333  1.19.4.1      yamt 			ifp->if_timer = 0;
    334  1.19.4.1      yamt 			if (sc->sc_next_tbd != 0) {
    335  1.19.4.1      yamt 				/* A TX CMD list finished, cleanup */
    336  1.19.4.1      yamt 				for (n = 0 ; n < sc->sc_next_cb ; n++) {
    337  1.19.4.1      yamt 					m_freem(sc->sc_tx_mbuf[n]);
    338  1.19.4.1      yamt 					sc->sc_tx_mbuf[n] = NULL;
    339  1.19.4.1      yamt 					bus_dmamap_unload(sc->sc_dmat,
    340  1.19.4.1      yamt 					    sc->sc_tx_map[n]);
    341  1.19.4.1      yamt 					IEE_CBSYNC(sc, n,
    342  1.19.4.1      yamt 				    	    BUS_DMASYNC_POSTREAD|
    343  1.19.4.1      yamt 					    BUS_DMASYNC_POSTWRITE);
    344  1.19.4.1      yamt 					status = SC_CB(sc, n)->cb_status;
    345  1.19.4.1      yamt 					IEE_CBSYNC(sc, n,
    346  1.19.4.1      yamt 				    	    BUS_DMASYNC_PREREAD);
    347  1.19.4.1      yamt 					if ((status & IEE_CB_COL) != 0 &&
    348  1.19.4.1      yamt 					    (status & IEE_CB_MAXCOL) == 0)
    349  1.19.4.1      yamt 						col = 16;
    350  1.19.4.1      yamt 					else
    351  1.19.4.1      yamt 						col = status
    352  1.19.4.1      yamt 						    & IEE_CB_MAXCOL;
    353  1.19.4.1      yamt 					sc->sc_tx_col += col;
    354  1.19.4.1      yamt 					if ((status & IEE_CB_OK) != 0) {
    355  1.19.4.1      yamt 						ifp->if_opackets++;
    356  1.19.4.1      yamt 						ifp->if_collisions += col;
    357  1.19.4.1      yamt 					}
    358       1.6   tsutsui 				}
    359  1.19.4.1      yamt 				sc->sc_next_tbd = 0;
    360  1.19.4.1      yamt 				ifp->if_flags &= ~IFF_OACTIVE;
    361       1.1     jkunz 			}
    362  1.19.4.1      yamt 			for (n = 0 ; n < sc->sc_next_cb; n++) {
    363  1.19.4.1      yamt 				/*
    364  1.19.4.1      yamt 				 * Check if a CMD failed, but ignore TX errors.
    365  1.19.4.1      yamt 				 */
    366  1.19.4.1      yamt 				IEE_CBSYNC(sc, n,
    367  1.19.4.1      yamt 				    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    368  1.19.4.1      yamt 				cmd = SC_CB(sc, n)->cb_cmd;
    369  1.19.4.1      yamt 				status = SC_CB(sc, n)->cb_status;
    370  1.19.4.1      yamt 				IEE_CBSYNC(sc, n, BUS_DMASYNC_PREREAD);
    371  1.19.4.1      yamt 				if ((cmd & IEE_CB_CMD) != IEE_CB_CMD_TR &&
    372  1.19.4.1      yamt 				    (status & IEE_CB_OK) == 0)
    373  1.19.4.1      yamt 					printf("%s: iee_intr: scb_status=0x%x "
    374  1.19.4.1      yamt 					    "scb_cmd=0x%x failed command %d: "
    375  1.19.4.1      yamt 					    "cb_status[%d]=0x%.4x "
    376  1.19.4.1      yamt 					    "cb_cmd[%d]=0x%.4x\n",
    377  1.19.4.1      yamt 					    device_xname(sc->sc_dev),
    378  1.19.4.1      yamt 					    scb_status, scb_cmd,
    379  1.19.4.1      yamt 					    ++sc->sc_cmd_err,
    380  1.19.4.1      yamt 					    n, status, n, cmd);
    381  1.19.4.1      yamt 			}
    382  1.19.4.1      yamt 			sc->sc_next_cb = 0;
    383  1.19.4.1      yamt 			if ((sc->sc_flags & IEE_WANT_MCAST) != 0) {
    384  1.19.4.1      yamt 				iee_cb_setup(sc, IEE_CB_CMD_MCS |
    385  1.19.4.1      yamt 				    IEE_CB_S | IEE_CB_EL | IEE_CB_I);
    386  1.19.4.1      yamt 				(sc->sc_iee_cmd)(sc, IEE_SCB_CUC_EXE);
    387  1.19.4.1      yamt 			} else
    388  1.19.4.1      yamt 				/* Try to get deferred packets going. */
    389  1.19.4.1      yamt 				iee_start(ifp);
    390       1.1     jkunz 		}
    391       1.1     jkunz 	}
    392  1.19.4.1      yamt 	if (IEE_SWAP32(SC_SCB(sc)->scb_crc_err) != sc->sc_crc_err) {
    393  1.19.4.1      yamt 		sc->sc_crc_err = IEE_SWAP32(SC_SCB(sc)->scb_crc_err);
    394      1.19   tsutsui 		printf("%s: iee_intr: crc_err=%d\n", device_xname(sc->sc_dev),
    395       1.1     jkunz 		    sc->sc_crc_err);
    396       1.1     jkunz 	}
    397  1.19.4.1      yamt 	if (IEE_SWAP32(SC_SCB(sc)->scb_align_err) != sc->sc_align_err) {
    398  1.19.4.1      yamt 		sc->sc_align_err = IEE_SWAP32(SC_SCB(sc)->scb_align_err);
    399      1.19   tsutsui 		printf("%s: iee_intr: align_err=%d\n", device_xname(sc->sc_dev),
    400       1.1     jkunz 		    sc->sc_align_err);
    401       1.1     jkunz 	}
    402  1.19.4.1      yamt 	if (IEE_SWAP32(SC_SCB(sc)->scb_resource_err) != sc->sc_resource_err) {
    403  1.19.4.1      yamt 		sc->sc_resource_err = IEE_SWAP32(SC_SCB(sc)->scb_resource_err);
    404      1.19   tsutsui 		printf("%s: iee_intr: resource_err=%d\n",
    405      1.19   tsutsui 		    device_xname(sc->sc_dev), sc->sc_resource_err);
    406       1.1     jkunz 	}
    407  1.19.4.1      yamt 	if (IEE_SWAP32(SC_SCB(sc)->scb_overrun_err) != sc->sc_overrun_err) {
    408  1.19.4.1      yamt 		sc->sc_overrun_err = IEE_SWAP32(SC_SCB(sc)->scb_overrun_err);
    409      1.19   tsutsui 		printf("%s: iee_intr: overrun_err=%d\n",
    410      1.19   tsutsui 		    device_xname(sc->sc_dev), sc->sc_overrun_err);
    411       1.1     jkunz 	}
    412  1.19.4.1      yamt 	if (IEE_SWAP32(SC_SCB(sc)->scb_rcvcdt_err) != sc->sc_rcvcdt_err) {
    413  1.19.4.1      yamt 		sc->sc_rcvcdt_err = IEE_SWAP32(SC_SCB(sc)->scb_rcvcdt_err);
    414      1.19   tsutsui 		printf("%s: iee_intr: rcvcdt_err=%d\n",
    415      1.19   tsutsui 		    device_xname(sc->sc_dev), sc->sc_rcvcdt_err);
    416       1.1     jkunz 	}
    417  1.19.4.1      yamt 	if (IEE_SWAP32(SC_SCB(sc)->scb_short_fr_err) != sc->sc_short_fr_err) {
    418  1.19.4.1      yamt 		sc->sc_short_fr_err = IEE_SWAP32(SC_SCB(sc)->scb_short_fr_err);
    419      1.19   tsutsui 		printf("%s: iee_intr: short_fr_err=%d\n",
    420      1.19   tsutsui 		    device_xname(sc->sc_dev), sc->sc_short_fr_err);
    421       1.1     jkunz 	}
    422  1.19.4.1      yamt 	IEE_SCBSYNC(sc, BUS_DMASYNC_PREREAD);
    423       1.1     jkunz 	(sc->sc_iee_cmd)(sc, IEE_SCB_ACK);
    424      1.19   tsutsui 	return 1;
    425       1.1     jkunz }
    426       1.1     jkunz 
    427       1.1     jkunz 
    428       1.1     jkunz 
    429       1.1     jkunz /*
    430      1.13     skrll  * How Command Block List Processing is done.
    431      1.13     skrll  *
    432      1.13     skrll  * A running CBL is never manipulated. If there is a CBL already running,
    433      1.13     skrll  * further CMDs are deferred until the current list is done. A new list is
    434      1.13     skrll  * setup when the old one has finished.
    435      1.13     skrll  * This eases programming. To manipulate a running CBL it is necessary to
    436      1.13     skrll  * suspend the Command Unit to avoid race conditions. After a suspend
    437      1.13     skrll  * is sent we have to wait for an interrupt that ACKs the suspend. Then
    438      1.13     skrll  * we can manipulate the CBL and resume operation. I am not sure that this
    439      1.13     skrll  * is more effective then the current, much simpler approach. => KISS
    440      1.13     skrll  * See i82596CA data sheet page 26.
    441      1.13     skrll  *
    442      1.13     skrll  * A CBL is running or on the way to be set up when (sc->sc_next_cb != 0).
    443      1.13     skrll  *
    444      1.13     skrll  * A CBL may consist of TX CMDs, and _only_ TX CMDs.
    445      1.13     skrll  * A TX CBL is running or on the way to be set up when
    446      1.13     skrll  * ((sc->sc_next_cb != 0) && (sc->sc_next_tbd != 0)).
    447      1.13     skrll  *
    448      1.13     skrll  * A CBL may consist of other non-TX CMDs like IAS or CONF, and _only_
    449      1.13     skrll  * non-TX CMDs.
    450      1.13     skrll  *
    451      1.13     skrll  * This comes mostly through the way how an Ethernet driver works and
    452      1.13     skrll  * because running CBLs are not manipulated when they are on the way. If
    453      1.13     skrll  * if_start() is called there will be TX CMDs enqueued so we have a running
    454      1.13     skrll  * CBL and other CMDs from e.g. if_ioctl() will be deferred and vice versa.
    455      1.13     skrll  *
    456      1.13     skrll  * The Multicast Setup Command is special. A MCS needs more space than
    457      1.13     skrll  * a single CB has. Actual space requirement depends on the length of the
    458      1.13     skrll  * multicast list. So we always defer MCS until other CBLs are finished,
    459      1.13     skrll  * then we setup a CONF CMD in the first CB. The CONF CMD is needed to
    460      1.13     skrll  * turn ALLMULTI on the hardware on or off. The MCS is the 2nd CB and may
    461      1.13     skrll  * use all the remaining space in the CBL and the Transmit Buffer Descriptor
    462      1.13     skrll  * List. (Therefore CBL and TBDL must be continuous in physical and virtual
    463      1.13     skrll  * memory. This is guaranteed through the definitions of the list offsets
    464      1.13     skrll  * in i82596reg.h and because it is only a single DMA segment used for all
    465      1.13     skrll  * lists.) When ALLMULTI is enabled via the CONF CMD, the MCS is run with
    466      1.13     skrll  * a multicast list length of 0, thus disabling the multicast filter.
    467      1.13     skrll  * A deferred MCS is signaled via ((sc->sc_flags & IEE_WANT_MCAST) != 0)
    468      1.13     skrll  */
    469       1.1     jkunz void
    470       1.7   tsutsui iee_cb_setup(struct iee_softc *sc, uint32_t cmd)
    471       1.1     jkunz {
    472  1.19.4.1      yamt 	struct iee_cb *cb = SC_CB(sc, sc->sc_next_cb);
    473       1.1     jkunz 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    474       1.1     jkunz 	struct ether_multistep step;
    475       1.1     jkunz 	struct ether_multi *enm;
    476       1.1     jkunz 
    477  1.19.4.1      yamt 	memset(cb, 0, sc->sc_cb_sz);
    478       1.1     jkunz 	cb->cb_cmd = cmd;
    479  1.19.4.1      yamt 	switch (cmd & IEE_CB_CMD) {
    480       1.1     jkunz 	case IEE_CB_CMD_NOP:	/* NOP CMD */
    481       1.1     jkunz 		break;
    482       1.1     jkunz 	case IEE_CB_CMD_IAS:	/* Individual Address Setup */
    483      1.17    dyoung 		memcpy(__UNVOLATILE(cb->cb_ind_addr), CLLADDR(ifp->if_sadl),
    484       1.1     jkunz 		    ETHER_ADDR_LEN);
    485       1.1     jkunz 		break;
    486       1.1     jkunz 	case IEE_CB_CMD_CONF:	/* Configure */
    487       1.9        he 		memcpy(__UNVOLATILE(cb->cb_cf), sc->sc_cf, sc->sc_cf[0]
    488       1.1     jkunz 		    & IEE_CF_0_CNT_M);
    489       1.1     jkunz 		break;
    490       1.1     jkunz 	case IEE_CB_CMD_MCS:	/* Multicast Setup */
    491       1.1     jkunz 		if (sc->sc_next_cb != 0) {
    492       1.1     jkunz 			sc->sc_flags |= IEE_WANT_MCAST;
    493       1.1     jkunz 			return;
    494       1.1     jkunz 		}
    495       1.1     jkunz 		sc->sc_flags &= ~IEE_WANT_MCAST;
    496       1.1     jkunz 		if ((sc->sc_cf[8] & IEE_CF_8_PRM) != 0) {
    497       1.1     jkunz 			/* Need no multicast filter in promisc mode. */
    498       1.8     perry 			iee_cb_setup(sc, IEE_CB_CMD_CONF | IEE_CB_S | IEE_CB_EL
    499       1.1     jkunz 			    | IEE_CB_I);
    500       1.1     jkunz 			return;
    501       1.1     jkunz 		}
    502       1.1     jkunz 		/* Leave room for a CONF CMD to en/dis-able ALLMULTI mode */
    503  1.19.4.1      yamt 		cb = SC_CB(sc, sc->sc_next_cb + 1);
    504       1.1     jkunz 		cb->cb_cmd = cmd;
    505       1.1     jkunz 		cb->cb_mcast.mc_size = 0;
    506       1.1     jkunz 		ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
    507       1.1     jkunz 		while (enm != NULL) {
    508       1.8     perry 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    509       1.8     perry 			    ETHER_ADDR_LEN) != 0 || cb->cb_mcast.mc_size
    510  1.19.4.1      yamt 			    * ETHER_ADDR_LEN + 2 * sc->sc_cb_sz >
    511  1.19.4.1      yamt 			    sc->sc_cb_sz * IEE_NCB +
    512  1.19.4.1      yamt 			    sc->sc_tbd_sz * IEE_NTBD * IEE_NCB) {
    513       1.1     jkunz 				cb->cb_mcast.mc_size = 0;
    514       1.1     jkunz 				break;
    515       1.1     jkunz 			}
    516       1.9        he 			memcpy(__UNVOLATILE(&cb->cb_mcast.mc_addrs[
    517       1.9        he 			    cb->cb_mcast.mc_size * ETHER_ADDR_LEN]),
    518       1.1     jkunz 			    enm->enm_addrlo, ETHER_ADDR_LEN);
    519       1.1     jkunz 			ETHER_NEXT_MULTI(step, enm);
    520       1.1     jkunz 			cb->cb_mcast.mc_size++;
    521       1.1     jkunz 		}
    522       1.1     jkunz 		if (cb->cb_mcast.mc_size == 0) {
    523       1.1     jkunz 			/* Can't do exact mcast filtering, do ALLMULTI mode. */
    524       1.1     jkunz 			ifp->if_flags |= IFF_ALLMULTI;
    525       1.1     jkunz 			sc->sc_cf[11] &= ~IEE_CF_11_MCALL;
    526       1.1     jkunz 		} else {
    527       1.1     jkunz 			/* disable ALLMULTI and load mcast list */
    528       1.1     jkunz 			ifp->if_flags &= ~IFF_ALLMULTI;
    529       1.1     jkunz 			sc->sc_cf[11] |= IEE_CF_11_MCALL;
    530  1.19.4.1      yamt 			/* Mcast setup may need more then sc->sc_cb_sz bytes. */
    531       1.8     perry 			bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map,
    532  1.19.4.1      yamt 			    sc->sc_cb_off,
    533  1.19.4.1      yamt 			    sc->sc_cb_sz * IEE_NCB +
    534  1.19.4.1      yamt 			    sc->sc_tbd_sz * IEE_NTBD * IEE_NCB,
    535       1.1     jkunz 			    BUS_DMASYNC_PREWRITE);
    536       1.1     jkunz 		}
    537       1.1     jkunz 		iee_cb_setup(sc, IEE_CB_CMD_CONF);
    538       1.1     jkunz 		break;
    539       1.1     jkunz 	case IEE_CB_CMD_TR:	/* Transmit */
    540  1.19.4.1      yamt 		cb->cb_transmit.tx_tbd_addr =
    541  1.19.4.1      yamt 		    IEE_SWAPA32(IEE_PHYS_SHMEM(sc->sc_tbd_off
    542  1.19.4.1      yamt 		    + sc->sc_tbd_sz * sc->sc_next_tbd));
    543      1.12     skrll 		cb->cb_cmd |= IEE_CB_SF; /* Always use Flexible Mode. */
    544       1.1     jkunz 		break;
    545       1.1     jkunz 	case IEE_CB_CMD_TDR:	/* Time Domain Reflectometry */
    546       1.1     jkunz 		break;
    547       1.1     jkunz 	case IEE_CB_CMD_DUMP:	/* Dump */
    548       1.1     jkunz 		break;
    549       1.1     jkunz 	case IEE_CB_CMD_DIAG:	/* Diagnose */
    550       1.1     jkunz 		break;
    551       1.1     jkunz 	default:
    552       1.1     jkunz 		/* can't happen */
    553       1.1     jkunz 		break;
    554       1.1     jkunz 	}
    555  1.19.4.1      yamt 	cb->cb_link_addr = IEE_SWAPA32(IEE_PHYS_SHMEM(sc->sc_cb_off +
    556  1.19.4.1      yamt 	    sc->sc_cb_sz * (sc->sc_next_cb + 1)));
    557  1.19.4.1      yamt 	IEE_CBSYNC(sc, sc->sc_next_cb,
    558  1.19.4.1      yamt 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    559       1.1     jkunz 	sc->sc_next_cb++;
    560       1.1     jkunz 	ifp->if_timer = 5;
    561       1.1     jkunz }
    562       1.1     jkunz 
    563       1.1     jkunz 
    564       1.1     jkunz 
    565       1.1     jkunz void
    566       1.8     perry iee_attach(struct iee_softc *sc, uint8_t *eth_addr, int *media, int nmedia,
    567       1.1     jkunz     int defmedia)
    568       1.1     jkunz {
    569       1.1     jkunz 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    570       1.1     jkunz 	int n;
    571       1.1     jkunz 
    572  1.19.4.1      yamt 	KASSERT(sc->sc_cl_align > 0 && powerof2(sc->sc_cl_align));
    573  1.19.4.1      yamt 
    574  1.19.4.1      yamt 	/*
    575  1.19.4.1      yamt 	 * Calculate DMA descriptor offsets and sizes in shmem
    576  1.19.4.1      yamt 	 * which should be cache line aligned.
    577  1.19.4.1      yamt 	 */
    578  1.19.4.1      yamt 	sc->sc_scp_off  = 0;
    579  1.19.4.1      yamt 	sc->sc_scp_sz   = roundup2(sizeof(struct iee_scp), sc->sc_cl_align);
    580  1.19.4.1      yamt 	sc->sc_iscp_off = sc->sc_scp_sz;
    581  1.19.4.1      yamt 	sc->sc_iscp_sz  = roundup2(sizeof(struct iee_iscp), sc->sc_cl_align);
    582  1.19.4.1      yamt 	sc->sc_scb_off  = sc->sc_iscp_off + sc->sc_iscp_sz;
    583  1.19.4.1      yamt 	sc->sc_scb_sz   = roundup2(sizeof(struct iee_scb), sc->sc_cl_align);
    584  1.19.4.1      yamt 	sc->sc_rfd_off  = sc->sc_scb_off + sc->sc_scb_sz;
    585  1.19.4.1      yamt 	sc->sc_rfd_sz   = roundup2(sizeof(struct iee_rfd), sc->sc_cl_align);
    586  1.19.4.1      yamt 	sc->sc_rbd_off  = sc->sc_rfd_off + sc->sc_rfd_sz * IEE_NRFD;
    587  1.19.4.1      yamt 	sc->sc_rbd_sz   = roundup2(sizeof(struct iee_rbd), sc->sc_cl_align);
    588  1.19.4.1      yamt 	sc->sc_cb_off   = sc->sc_rbd_off + sc->sc_rbd_sz * IEE_NRFD;
    589  1.19.4.1      yamt 	sc->sc_cb_sz    = roundup2(sizeof(struct iee_cb), sc->sc_cl_align);
    590  1.19.4.1      yamt 	sc->sc_tbd_off  = sc->sc_cb_off + sc->sc_cb_sz * IEE_NCB;
    591  1.19.4.1      yamt 	sc->sc_tbd_sz   = roundup2(sizeof(struct iee_tbd), sc->sc_cl_align);
    592  1.19.4.1      yamt 	sc->sc_shmem_sz = sc->sc_tbd_off + sc->sc_tbd_sz * IEE_NTBD * IEE_NCB;
    593  1.19.4.1      yamt 
    594  1.19.4.1      yamt 	/* allocate memory for shared DMA descriptors */
    595  1.19.4.1      yamt 	if (bus_dmamem_alloc(sc->sc_dmat, sc->sc_shmem_sz, PAGE_SIZE, 0,
    596  1.19.4.1      yamt 	    &sc->sc_dma_segs, 1, &sc->sc_dma_rsegs, BUS_DMA_NOWAIT) != 0) {
    597  1.19.4.1      yamt 		aprint_error(": can't allocate %d bytes of DMA memory\n",
    598  1.19.4.1      yamt 		    sc->sc_shmem_sz);
    599  1.19.4.1      yamt 		return;
    600  1.19.4.1      yamt 	}
    601  1.19.4.1      yamt 	if (bus_dmamem_map(sc->sc_dmat, &sc->sc_dma_segs, sc->sc_dma_rsegs,
    602  1.19.4.1      yamt 	    sc->sc_shmem_sz, (void **)&sc->sc_shmem_addr,
    603  1.19.4.1      yamt 	    BUS_DMA_COHERENT | BUS_DMA_NOWAIT) != 0) {
    604  1.19.4.1      yamt 		aprint_error(": can't map DMA memory\n");
    605  1.19.4.1      yamt 		bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_segs,
    606  1.19.4.1      yamt 		    sc->sc_dma_rsegs);
    607  1.19.4.1      yamt 		return;
    608  1.19.4.1      yamt 	}
    609  1.19.4.1      yamt 	if (bus_dmamap_create(sc->sc_dmat, sc->sc_shmem_sz, sc->sc_dma_rsegs,
    610  1.19.4.1      yamt 	    sc->sc_shmem_sz, 0, BUS_DMA_NOWAIT, &sc->sc_shmem_map) != 0) {
    611  1.19.4.1      yamt 		aprint_error(": can't create DMA map\n");
    612  1.19.4.1      yamt 		bus_dmamem_unmap(sc->sc_dmat, sc->sc_shmem_addr,
    613  1.19.4.1      yamt 		    sc->sc_shmem_sz);
    614  1.19.4.1      yamt 		bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_segs,
    615  1.19.4.1      yamt 		    sc->sc_dma_rsegs);
    616  1.19.4.1      yamt 		return;
    617  1.19.4.1      yamt 	}
    618  1.19.4.1      yamt 	if (bus_dmamap_load(sc->sc_dmat, sc->sc_shmem_map, sc->sc_shmem_addr,
    619  1.19.4.1      yamt 	    sc->sc_shmem_sz, NULL, BUS_DMA_NOWAIT) != 0) {
    620  1.19.4.1      yamt 		aprint_error(": can't load DMA map\n");
    621  1.19.4.1      yamt 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_shmem_map);
    622  1.19.4.1      yamt 		bus_dmamem_unmap(sc->sc_dmat, sc->sc_shmem_addr,
    623  1.19.4.1      yamt 		    sc->sc_shmem_sz);
    624  1.19.4.1      yamt 		bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_segs,
    625  1.19.4.1      yamt 		    sc->sc_dma_rsegs);
    626  1.19.4.1      yamt 		return;
    627  1.19.4.1      yamt 	}
    628  1.19.4.1      yamt 	memset(sc->sc_shmem_addr, 0, sc->sc_shmem_sz);
    629  1.19.4.1      yamt 
    630       1.1     jkunz 	/* Set pointer to Intermediate System Configuration Pointer. */
    631       1.1     jkunz 	/* Phys. addr. in big endian order. (Big endian as defined by Intel.) */
    632  1.19.4.1      yamt 	SC_SCP(sc)->scp_iscp_addr = IEE_SWAP32(IEE_PHYS_SHMEM(sc->sc_iscp_off));
    633  1.19.4.1      yamt 	SC_SCP(sc)->scp_sysbus = sc->sc_sysbus;
    634       1.1     jkunz 	/* Set pointer to System Control Block. */
    635       1.1     jkunz 	/* Phys. addr. in big endian order. (Big endian as defined by Intel.) */
    636  1.19.4.1      yamt 	SC_ISCP(sc)->iscp_scb_addr = IEE_SWAP32(IEE_PHYS_SHMEM(sc->sc_scb_off));
    637       1.1     jkunz 	/* Set pointer to Receive Frame Area. (physical address) */
    638  1.19.4.1      yamt 	SC_SCB(sc)->scb_rfa_addr = IEE_SWAPA32(IEE_PHYS_SHMEM(sc->sc_rfd_off));
    639       1.1     jkunz 	/* Set pointer to Command Block. (physical address) */
    640  1.19.4.1      yamt 	SC_SCB(sc)->scb_cmd_blk_addr =
    641  1.19.4.1      yamt 	    IEE_SWAPA32(IEE_PHYS_SHMEM(sc->sc_cb_off));
    642  1.19.4.1      yamt 
    643  1.19.4.1      yamt 	bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, 0, sc->sc_shmem_sz,
    644  1.19.4.1      yamt 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    645       1.1     jkunz 
    646       1.1     jkunz 	ifmedia_init(&sc->sc_ifmedia, 0, iee_mediachange, iee_mediastatus);
    647       1.1     jkunz 	if (media != NULL) {
    648       1.1     jkunz 		for (n = 0 ; n < nmedia ; n++)
    649       1.1     jkunz 			ifmedia_add(&sc->sc_ifmedia, media[n], 0, NULL);
    650       1.1     jkunz 		ifmedia_set(&sc->sc_ifmedia, defmedia);
    651       1.1     jkunz 	} else {
    652       1.1     jkunz 		ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_NONE, 0, NULL);
    653       1.1     jkunz 		ifmedia_set(&sc->sc_ifmedia, IFM_ETHER | IFM_NONE);
    654       1.1     jkunz 	}
    655       1.1     jkunz 
    656       1.1     jkunz 	ifp->if_softc = sc;
    657      1.19   tsutsui 	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
    658       1.1     jkunz 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    659       1.1     jkunz 	ifp->if_start = iee_start;	/* initiate output routine */
    660       1.1     jkunz 	ifp->if_ioctl = iee_ioctl;	/* ioctl routine */
    661       1.1     jkunz 	ifp->if_init = iee_init;	/* init routine */
    662       1.1     jkunz 	ifp->if_stop = iee_stop;	/* stop routine */
    663       1.1     jkunz 	ifp->if_watchdog = iee_watchdog;	/* timer routine */
    664       1.1     jkunz 	IFQ_SET_READY(&ifp->if_snd);
    665       1.1     jkunz 	/* iee supports IEEE 802.1Q Virtual LANs, see vlan(4). */
    666       1.1     jkunz 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    667       1.1     jkunz 
    668       1.1     jkunz 	if_attach(ifp);
    669       1.1     jkunz 	ether_ifattach(ifp, eth_addr);
    670       1.1     jkunz 
    671       1.1     jkunz 	aprint_normal(": Intel 82596%s address %s\n",
    672      1.19   tsutsui 	    i82596_typenames[sc->sc_type], ether_sprintf(eth_addr));
    673       1.1     jkunz 
    674       1.1     jkunz 	for (n = 0 ; n < IEE_NCB ; n++)
    675       1.1     jkunz 		sc->sc_tx_map[n] = NULL;
    676       1.1     jkunz 	for (n = 0 ; n < IEE_NRFD ; n++) {
    677       1.1     jkunz 		sc->sc_rx_mbuf[n] = NULL;
    678       1.1     jkunz 		sc->sc_rx_map[n] = NULL;
    679       1.1     jkunz 	}
    680       1.1     jkunz 	sc->sc_tx_timeout = 0;
    681       1.1     jkunz 	sc->sc_setup_timeout = 0;
    682       1.1     jkunz 	(sc->sc_iee_reset)(sc);
    683       1.1     jkunz }
    684       1.1     jkunz 
    685       1.1     jkunz 
    686       1.1     jkunz 
    687       1.1     jkunz void
    688       1.1     jkunz iee_detach(struct iee_softc *sc, int flags)
    689       1.1     jkunz {
    690       1.1     jkunz 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    691       1.1     jkunz 
    692       1.1     jkunz 	if ((ifp->if_flags & IFF_RUNNING) != 0)
    693       1.1     jkunz 		iee_stop(ifp, 1);
    694       1.1     jkunz 	ether_ifdetach(ifp);
    695       1.1     jkunz 	if_detach(ifp);
    696  1.19.4.1      yamt 	bus_dmamap_unload(sc->sc_dmat, sc->sc_shmem_map);
    697  1.19.4.1      yamt 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_shmem_map);
    698  1.19.4.1      yamt 	bus_dmamem_unmap(sc->sc_dmat, sc->sc_shmem_addr, sc->sc_shmem_sz);
    699  1.19.4.1      yamt 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_segs, sc->sc_dma_rsegs);
    700       1.1     jkunz }
    701       1.1     jkunz 
    702       1.1     jkunz 
    703       1.1     jkunz 
    704       1.1     jkunz /* media change and status callback */
    705       1.1     jkunz int
    706       1.1     jkunz iee_mediachange(struct ifnet *ifp)
    707       1.1     jkunz {
    708       1.1     jkunz 	struct iee_softc *sc = ifp->if_softc;
    709       1.8     perry 
    710       1.1     jkunz 	if (sc->sc_mediachange != NULL)
    711      1.19   tsutsui 		return (sc->sc_mediachange)(ifp);
    712      1.19   tsutsui 	return 0;
    713       1.1     jkunz }
    714       1.1     jkunz 
    715       1.1     jkunz 
    716       1.1     jkunz 
    717       1.1     jkunz void
    718       1.1     jkunz iee_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmreq)
    719       1.1     jkunz {
    720       1.1     jkunz 	struct iee_softc *sc = ifp->if_softc;
    721       1.1     jkunz 
    722       1.1     jkunz 	if (sc->sc_mediastatus != NULL)
    723      1.19   tsutsui 		(sc->sc_mediastatus)(ifp, ifmreq);
    724       1.1     jkunz }
    725       1.1     jkunz 
    726       1.1     jkunz 
    727       1.1     jkunz 
    728       1.1     jkunz /* initiate output routine */
    729       1.1     jkunz void
    730       1.1     jkunz iee_start(struct ifnet *ifp)
    731       1.1     jkunz {
    732       1.1     jkunz 	struct iee_softc *sc = ifp->if_softc;
    733       1.1     jkunz 	struct mbuf *m = NULL;
    734  1.19.4.1      yamt 	struct iee_tbd *tbd;
    735       1.1     jkunz 	int t;
    736       1.1     jkunz 	int n;
    737       1.1     jkunz 
    738       1.1     jkunz 	if (sc->sc_next_cb != 0)
    739      1.12     skrll 		/* There is already a CMD running. Defer packet enqueuing. */
    740       1.1     jkunz 		return;
    741       1.1     jkunz 	for (t = 0 ; t < IEE_NCB ; t++) {
    742       1.1     jkunz 		IFQ_DEQUEUE(&ifp->if_snd, sc->sc_tx_mbuf[t]);
    743       1.1     jkunz 		if (sc->sc_tx_mbuf[t] == NULL)
    744       1.1     jkunz 			break;
    745       1.1     jkunz 		if (bus_dmamap_load_mbuf(sc->sc_dmat, sc->sc_tx_map[t],
    746       1.1     jkunz 		    sc->sc_tx_mbuf[t], BUS_DMA_WRITE | BUS_DMA_NOWAIT) != 0) {
    747       1.1     jkunz 			/*
    748       1.8     perry 			 * The packet needs more TBD then we support.
    749       1.8     perry 			 * Copy the packet into a mbuf cluster to get it out.
    750       1.1     jkunz 			 */
    751       1.8     perry 			printf("%s: iee_start: failed to load DMA map\n",
    752      1.19   tsutsui 			    device_xname(sc->sc_dev));
    753       1.1     jkunz 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    754       1.1     jkunz 			if (m == NULL) {
    755       1.1     jkunz 				printf("%s: iee_start: can't allocate mbuf\n",
    756      1.19   tsutsui 				    device_xname(sc->sc_dev));
    757       1.1     jkunz 				m_freem(sc->sc_tx_mbuf[t]);
    758       1.1     jkunz 				t--;
    759       1.1     jkunz 				continue;
    760       1.1     jkunz 			}
    761       1.1     jkunz 			MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
    762       1.1     jkunz 			MCLGET(m, M_DONTWAIT);
    763       1.1     jkunz 			if ((m->m_flags & M_EXT) == 0) {
    764       1.1     jkunz 				printf("%s: iee_start: can't allocate mbuf "
    765      1.19   tsutsui 				    "cluster\n", device_xname(sc->sc_dev));
    766       1.1     jkunz 				m_freem(sc->sc_tx_mbuf[t]);
    767       1.1     jkunz 				m_freem(m);
    768       1.1     jkunz 				t--;
    769       1.1     jkunz 				continue;
    770       1.1     jkunz 			}
    771       1.8     perry 			m_copydata(sc->sc_tx_mbuf[t], 0,
    772      1.15  christos 			    sc->sc_tx_mbuf[t]->m_pkthdr.len, mtod(m, void *));
    773       1.1     jkunz 			m->m_pkthdr.len = sc->sc_tx_mbuf[t]->m_pkthdr.len;
    774       1.1     jkunz 			m->m_len = sc->sc_tx_mbuf[t]->m_pkthdr.len;
    775       1.1     jkunz 			m_freem(sc->sc_tx_mbuf[t]);
    776       1.1     jkunz 			sc->sc_tx_mbuf[t] = m;
    777  1.19.4.1      yamt 			if (bus_dmamap_load_mbuf(sc->sc_dmat, sc->sc_tx_map[t],
    778       1.1     jkunz 		    	    m, BUS_DMA_WRITE | BUS_DMA_NOWAIT) != 0) {
    779       1.1     jkunz 				printf("%s: iee_start: can't load TX DMA map\n",
    780      1.19   tsutsui 				    device_xname(sc->sc_dev));
    781       1.1     jkunz 				m_freem(sc->sc_tx_mbuf[t]);
    782       1.1     jkunz 				t--;
    783       1.1     jkunz 				continue;
    784       1.1     jkunz 			}
    785       1.1     jkunz 		}
    786       1.1     jkunz 		for (n = 0 ; n < sc->sc_tx_map[t]->dm_nsegs ; n++) {
    787  1.19.4.1      yamt 			tbd = SC_TBD(sc, sc->sc_next_tbd + n);
    788  1.19.4.1      yamt 			tbd->tbd_tb_addr =
    789  1.19.4.1      yamt 			    IEE_SWAPA32(sc->sc_tx_map[t]->dm_segs[n].ds_addr);
    790  1.19.4.1      yamt 			tbd->tbd_size =
    791       1.1     jkunz 			    sc->sc_tx_map[t]->dm_segs[n].ds_len;
    792  1.19.4.1      yamt 			tbd->tbd_link_addr =
    793  1.19.4.1      yamt 			    IEE_SWAPA32(IEE_PHYS_SHMEM(sc->sc_tbd_off +
    794  1.19.4.1      yamt 			    sc->sc_tbd_sz * (sc->sc_next_tbd + n + 1)));
    795       1.1     jkunz 		}
    796  1.19.4.1      yamt 		SC_TBD(sc, sc->sc_next_tbd + n - 1)->tbd_size |= IEE_CB_EL;
    797  1.19.4.1      yamt 		bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map,
    798  1.19.4.1      yamt 		    sc->sc_tbd_off + sc->sc_next_tbd * sc->sc_tbd_sz,
    799  1.19.4.1      yamt 		    sc->sc_tbd_sz * sc->sc_tx_map[t]->dm_nsegs,
    800  1.19.4.1      yamt 		    BUS_DMASYNC_PREWRITE);
    801       1.1     jkunz 		bus_dmamap_sync(sc->sc_dmat, sc->sc_tx_map[t], 0,
    802       1.1     jkunz 		    sc->sc_tx_map[t]->dm_mapsize, BUS_DMASYNC_PREWRITE);
    803       1.1     jkunz 		IFQ_POLL(&ifp->if_snd, m);
    804       1.1     jkunz 		if (m == NULL)
    805       1.1     jkunz 			iee_cb_setup(sc, IEE_CB_CMD_TR | IEE_CB_S | IEE_CB_EL
    806       1.1     jkunz 			    | IEE_CB_I);
    807       1.1     jkunz 		else
    808       1.1     jkunz 			iee_cb_setup(sc, IEE_CB_CMD_TR);
    809       1.1     jkunz 		sc->sc_next_tbd += n;
    810       1.1     jkunz 		/* Pass packet to bpf if someone listens. */
    811       1.1     jkunz 		if (ifp->if_bpf)
    812  1.19.4.2      yamt 			bpf_ops->bpf_mtap(ifp->if_bpf, sc->sc_tx_mbuf[t]);
    813       1.1     jkunz 	}
    814       1.1     jkunz 	if (t == 0)
    815       1.1     jkunz 		/* No packets got set up for TX. */
    816       1.1     jkunz 		return;
    817       1.1     jkunz 	if (t == IEE_NCB)
    818       1.1     jkunz 		ifp->if_flags |= IFF_OACTIVE;
    819       1.1     jkunz 	(sc->sc_iee_cmd)(sc, IEE_SCB_CUC_EXE);
    820       1.1     jkunz }
    821       1.1     jkunz 
    822       1.1     jkunz 
    823       1.1     jkunz 
    824       1.1     jkunz /* ioctl routine */
    825       1.1     jkunz int
    826      1.15  christos iee_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    827       1.1     jkunz {
    828       1.1     jkunz 	struct iee_softc *sc = ifp->if_softc;
    829       1.1     jkunz 	int s;
    830       1.1     jkunz 	int err;
    831       1.1     jkunz 
    832       1.1     jkunz 	s = splnet();
    833       1.4   thorpej 	switch (cmd) {
    834       1.4   thorpej 	case SIOCSIFMEDIA:
    835       1.4   thorpej 	case SIOCGIFMEDIA:
    836       1.4   thorpej 		err = ifmedia_ioctl(ifp, (struct ifreq *) data,
    837       1.4   thorpej 		    &sc->sc_ifmedia, cmd);
    838       1.4   thorpej 		break;
    839       1.4   thorpej 
    840       1.4   thorpej 	default:
    841       1.1     jkunz 		err = ether_ioctl(ifp, cmd, data);
    842       1.4   thorpej 		if (err == ENETRESET) {
    843       1.4   thorpej 			/*
    844       1.4   thorpej 			 * Multicast list as changed; set the hardware filter
    845       1.4   thorpej 			 * accordingly.
    846       1.4   thorpej 			 */
    847       1.4   thorpej 			if (ifp->if_flags & IFF_RUNNING) {
    848       1.4   thorpej 				iee_cb_setup(sc, IEE_CB_CMD_MCS | IEE_CB_S |
    849       1.4   thorpej 				    IEE_CB_EL | IEE_CB_I);
    850       1.4   thorpej 				if ((sc->sc_flags & IEE_WANT_MCAST) == 0)
    851       1.4   thorpej 					(*sc->sc_iee_cmd)(sc, IEE_SCB_CUC_EXE);
    852       1.4   thorpej 			}
    853       1.3   thorpej 			err = 0;
    854       1.4   thorpej 		}
    855       1.4   thorpej 		break;
    856       1.1     jkunz 	}
    857       1.1     jkunz 	splx(s);
    858      1.19   tsutsui 	return err;
    859       1.1     jkunz }
    860       1.1     jkunz 
    861       1.1     jkunz 
    862       1.1     jkunz 
    863       1.1     jkunz /* init routine */
    864       1.1     jkunz int
    865       1.1     jkunz iee_init(struct ifnet *ifp)
    866       1.1     jkunz {
    867       1.1     jkunz 	struct iee_softc *sc = ifp->if_softc;
    868       1.1     jkunz 	int r;
    869       1.1     jkunz 	int t;
    870       1.1     jkunz 	int n;
    871       1.1     jkunz 	int err;
    872       1.1     jkunz 
    873       1.1     jkunz 	sc->sc_next_cb = 0;
    874       1.1     jkunz 	sc->sc_next_tbd = 0;
    875       1.1     jkunz 	sc->sc_flags &= ~IEE_WANT_MCAST;
    876       1.1     jkunz 	sc->sc_rx_done = 0;
    877  1.19.4.1      yamt 	SC_SCB(sc)->scb_crc_err = 0;
    878  1.19.4.1      yamt 	SC_SCB(sc)->scb_align_err = 0;
    879  1.19.4.1      yamt 	SC_SCB(sc)->scb_resource_err = 0;
    880  1.19.4.1      yamt 	SC_SCB(sc)->scb_overrun_err = 0;
    881  1.19.4.1      yamt 	SC_SCB(sc)->scb_rcvcdt_err = 0;
    882  1.19.4.1      yamt 	SC_SCB(sc)->scb_short_fr_err = 0;
    883       1.1     jkunz 	sc->sc_crc_err = 0;
    884       1.1     jkunz 	sc->sc_align_err = 0;
    885       1.1     jkunz 	sc->sc_resource_err = 0;
    886       1.1     jkunz 	sc->sc_overrun_err = 0;
    887       1.1     jkunz 	sc->sc_rcvcdt_err = 0;
    888       1.1     jkunz 	sc->sc_short_fr_err = 0;
    889       1.1     jkunz 	sc->sc_tx_col = 0;
    890       1.1     jkunz 	sc->sc_rx_err = 0;
    891       1.1     jkunz 	sc->sc_cmd_err = 0;
    892       1.1     jkunz 	/* Create Transmit DMA maps. */
    893       1.1     jkunz 	for (t = 0 ; t < IEE_NCB ; t++) {
    894       1.1     jkunz 		if (sc->sc_tx_map[t] == NULL && bus_dmamap_create(sc->sc_dmat,
    895       1.8     perry 		    MCLBYTES, IEE_NTBD, MCLBYTES, 0, BUS_DMA_NOWAIT,
    896       1.1     jkunz 		    &sc->sc_tx_map[t]) != 0) {
    897       1.8     perry 			printf("%s: iee_init: can't create TX DMA map\n",
    898      1.19   tsutsui 			    device_xname(sc->sc_dev));
    899       1.1     jkunz 			for (n = 0 ; n < t ; n++)
    900       1.8     perry 				bus_dmamap_destroy(sc->sc_dmat,
    901       1.1     jkunz 				    sc->sc_tx_map[n]);
    902      1.19   tsutsui 			return ENOBUFS;
    903       1.1     jkunz 		}
    904       1.1     jkunz 	}
    905       1.1     jkunz 	/* Initialize Receive Frame and Receive Buffer Descriptors */
    906       1.1     jkunz 	err = 0;
    907  1.19.4.1      yamt 	memset(SC_RFD(sc, 0), 0, sc->sc_rfd_sz * IEE_NRFD);
    908  1.19.4.1      yamt 	memset(SC_RBD(sc, 0), 0, sc->sc_rbd_sz * IEE_NRFD);
    909       1.1     jkunz 	for (r = 0 ; r < IEE_NRFD ; r++) {
    910  1.19.4.1      yamt 		SC_RFD(sc, r)->rfd_cmd = IEE_RFD_SF;
    911  1.19.4.1      yamt 		SC_RFD(sc, r)->rfd_link_addr =
    912  1.19.4.1      yamt 		    IEE_SWAPA32(IEE_PHYS_SHMEM(sc->sc_rfd_off
    913  1.19.4.1      yamt 		    + sc->sc_rfd_sz * ((r + 1) % IEE_NRFD)));
    914  1.19.4.1      yamt 
    915  1.19.4.1      yamt 		SC_RBD(sc, r)->rbd_next_rbd =
    916  1.19.4.1      yamt 		    IEE_SWAPA32(IEE_PHYS_SHMEM(sc->sc_rbd_off
    917  1.19.4.1      yamt 		    + sc->sc_rbd_sz * ((r + 1) % IEE_NRFD)));
    918       1.1     jkunz 		if (sc->sc_rx_mbuf[r] == NULL) {
    919       1.1     jkunz 			MGETHDR(sc->sc_rx_mbuf[r], M_DONTWAIT, MT_DATA);
    920       1.1     jkunz 			if (sc->sc_rx_mbuf[r] == NULL) {
    921       1.8     perry 				printf("%s: iee_init: can't allocate mbuf\n",
    922      1.19   tsutsui 				    device_xname(sc->sc_dev));
    923       1.1     jkunz 				err = 1;
    924       1.1     jkunz 				break;
    925       1.1     jkunz 			}
    926       1.1     jkunz 			MCLAIM(sc->sc_rx_mbuf[r],&sc->sc_ethercom.ec_rx_mowner);
    927       1.1     jkunz 			MCLGET(sc->sc_rx_mbuf[r], M_DONTWAIT);
    928       1.1     jkunz 			if ((sc->sc_rx_mbuf[r]->m_flags & M_EXT) == 0) {
    929       1.1     jkunz 				printf("%s: iee_init: can't allocate mbuf"
    930      1.19   tsutsui 				    " cluster\n", device_xname(sc->sc_dev));
    931       1.1     jkunz 				m_freem(sc->sc_rx_mbuf[r]);
    932       1.1     jkunz 				err = 1;
    933       1.1     jkunz 				break;
    934       1.1     jkunz 			}
    935  1.19.4.1      yamt 			sc->sc_rx_mbuf[r]->m_len =
    936  1.19.4.1      yamt 			    sc->sc_rx_mbuf[r]->m_pkthdr.len = MCLBYTES - 2;
    937  1.19.4.1      yamt 			sc->sc_rx_mbuf[r]->m_data += 2;
    938       1.1     jkunz 		}
    939       1.1     jkunz 		if (sc->sc_rx_map[r] == NULL && bus_dmamap_create(sc->sc_dmat,
    940       1.8     perry 		    MCLBYTES, 1, MCLBYTES , 0, BUS_DMA_NOWAIT,
    941       1.1     jkunz 		    &sc->sc_rx_map[r]) != 0) {
    942       1.1     jkunz 				printf("%s: iee_init: can't create RX "
    943      1.19   tsutsui 				    "DMA map\n", device_xname(sc->sc_dev));
    944       1.1     jkunz 				m_freem(sc->sc_rx_mbuf[r]);
    945       1.1     jkunz 				err = 1;
    946       1.1     jkunz 				break;
    947       1.1     jkunz 			}
    948  1.19.4.1      yamt 		if (bus_dmamap_load_mbuf(sc->sc_dmat, sc->sc_rx_map[r],
    949  1.19.4.1      yamt 		    sc->sc_rx_mbuf[r], BUS_DMA_READ | BUS_DMA_NOWAIT) != 0) {
    950       1.1     jkunz 			printf("%s: iee_init: can't load RX DMA map\n",
    951      1.19   tsutsui 			    device_xname(sc->sc_dev));
    952       1.1     jkunz 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_rx_map[r]);
    953       1.1     jkunz 			m_freem(sc->sc_rx_mbuf[r]);
    954       1.1     jkunz 			err = 1;
    955       1.1     jkunz 			break;
    956       1.1     jkunz 		}
    957       1.1     jkunz 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_map[r], 0,
    958  1.19.4.1      yamt 		    sc->sc_rx_map[r]->dm_mapsize, BUS_DMASYNC_PREREAD);
    959  1.19.4.1      yamt 		SC_RBD(sc, r)->rbd_size = sc->sc_rx_map[r]->dm_segs[0].ds_len;
    960  1.19.4.1      yamt 		SC_RBD(sc, r)->rbd_rb_addr =
    961  1.19.4.1      yamt 		    IEE_SWAPA32(sc->sc_rx_map[r]->dm_segs[0].ds_addr);
    962       1.1     jkunz 	}
    963  1.19.4.1      yamt 	SC_RFD(sc, 0)->rfd_rbd_addr =
    964  1.19.4.1      yamt 	    IEE_SWAPA32(IEE_PHYS_SHMEM(sc->sc_rbd_off));
    965       1.1     jkunz 	if (err != 0) {
    966       1.1     jkunz 		for (n = 0 ; n < r; n++) {
    967       1.1     jkunz 			m_freem(sc->sc_rx_mbuf[n]);
    968       1.1     jkunz 			sc->sc_rx_mbuf[n] = NULL;
    969       1.1     jkunz 			bus_dmamap_unload(sc->sc_dmat, sc->sc_rx_map[n]);
    970       1.1     jkunz 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_rx_map[n]);
    971       1.1     jkunz 			sc->sc_rx_map[n] = NULL;
    972       1.1     jkunz 		}
    973       1.1     jkunz 		for (n = 0 ; n < t ; n++) {
    974       1.1     jkunz 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_tx_map[n]);
    975       1.1     jkunz 			sc->sc_tx_map[n] = NULL;
    976       1.1     jkunz 		}
    977      1.19   tsutsui 		return ENOBUFS;
    978       1.1     jkunz 	}
    979       1.1     jkunz 
    980       1.1     jkunz 	(sc->sc_iee_reset)(sc);
    981       1.1     jkunz 	iee_cb_setup(sc, IEE_CB_CMD_IAS);
    982       1.1     jkunz 	sc->sc_cf[0] = IEE_CF_0_DEF | IEE_CF_0_PREF;
    983       1.1     jkunz 	sc->sc_cf[1] = IEE_CF_1_DEF;
    984       1.1     jkunz 	sc->sc_cf[2] = IEE_CF_2_DEF;
    985       1.8     perry 	sc->sc_cf[3] = IEE_CF_3_ADDRLEN_DEF | IEE_CF_3_NSAI
    986       1.1     jkunz 	    | IEE_CF_3_PREAMLEN_DEF;
    987       1.1     jkunz 	sc->sc_cf[4] = IEE_CF_4_DEF;
    988       1.1     jkunz 	sc->sc_cf[5] = IEE_CF_5_DEF;
    989       1.1     jkunz 	sc->sc_cf[6] = IEE_CF_6_DEF;
    990       1.1     jkunz 	sc->sc_cf[7] = IEE_CF_7_DEF;
    991       1.1     jkunz 	sc->sc_cf[8] = IEE_CF_8_DEF;
    992       1.1     jkunz 	sc->sc_cf[9] = IEE_CF_9_DEF;
    993       1.1     jkunz 	sc->sc_cf[10] = IEE_CF_10_DEF;
    994       1.1     jkunz 	sc->sc_cf[11] = IEE_CF_11_DEF & ~IEE_CF_11_LNGFLD;
    995       1.1     jkunz 	sc->sc_cf[12] = IEE_CF_12_DEF;
    996       1.1     jkunz 	sc->sc_cf[13] = IEE_CF_13_DEF;
    997       1.1     jkunz 	iee_cb_setup(sc, IEE_CB_CMD_CONF | IEE_CB_S | IEE_CB_EL);
    998  1.19.4.1      yamt 	SC_SCB(sc)->scb_rfa_addr = IEE_SWAPA32(IEE_PHYS_SHMEM(sc->sc_rfd_off));
    999  1.19.4.1      yamt 	bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, 0, sc->sc_shmem_sz,
   1000  1.19.4.1      yamt 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1001       1.1     jkunz 	(sc->sc_iee_cmd)(sc, IEE_SCB_CUC_EXE | IEE_SCB_RUC_ST);
   1002       1.1     jkunz 	/* Issue a Channel Attention to ACK interrupts we may have caused. */
   1003       1.1     jkunz 	(sc->sc_iee_cmd)(sc, IEE_SCB_ACK);
   1004       1.1     jkunz 
   1005       1.1     jkunz 	/* Mark the interface as running and ready to RX/TX packets. */
   1006       1.1     jkunz 	ifp->if_flags |= IFF_RUNNING;
   1007       1.1     jkunz 	ifp->if_flags &= ~IFF_OACTIVE;
   1008      1.19   tsutsui 	return 0;
   1009       1.1     jkunz }
   1010       1.1     jkunz 
   1011       1.1     jkunz 
   1012       1.1     jkunz 
   1013       1.1     jkunz /* stop routine */
   1014       1.1     jkunz void
   1015       1.1     jkunz iee_stop(struct ifnet *ifp, int disable)
   1016       1.1     jkunz {
   1017       1.1     jkunz 	struct iee_softc *sc = ifp->if_softc;
   1018       1.1     jkunz 	int n;
   1019       1.1     jkunz 
   1020       1.1     jkunz 	ifp->if_flags &= ~IFF_RUNNING;
   1021       1.1     jkunz 	ifp->if_flags |= IFF_OACTIVE;
   1022       1.1     jkunz 	ifp->if_timer = 0;
   1023       1.1     jkunz 	/* Reset the chip to get it quiet. */
   1024       1.1     jkunz 	(sc->sc_iee_reset)(ifp->if_softc);
   1025       1.1     jkunz 	/* Issue a Channel Attention to ACK interrupts we may have caused. */
   1026       1.1     jkunz 	(sc->sc_iee_cmd)(ifp->if_softc, IEE_SCB_ACK);
   1027      1.12     skrll 	/* Release any dynamically allocated resources. */
   1028       1.1     jkunz 	for (n = 0 ; n < IEE_NCB ; n++) {
   1029       1.1     jkunz 		if (sc->sc_tx_map[n] != NULL)
   1030       1.1     jkunz 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_tx_map[n]);
   1031       1.1     jkunz 		sc->sc_tx_map[n] = NULL;
   1032       1.1     jkunz 	}
   1033       1.1     jkunz 	for (n = 0 ; n < IEE_NRFD ; n++) {
   1034       1.1     jkunz 		if (sc->sc_rx_mbuf[n] != NULL)
   1035       1.1     jkunz 			m_freem(sc->sc_rx_mbuf[n]);
   1036       1.1     jkunz 		sc->sc_rx_mbuf[n] = NULL;
   1037       1.1     jkunz 		if (sc->sc_rx_map[n] != NULL) {
   1038       1.1     jkunz 			bus_dmamap_unload(sc->sc_dmat, sc->sc_rx_map[n]);
   1039       1.1     jkunz 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_rx_map[n]);
   1040       1.1     jkunz 		}
   1041       1.1     jkunz 		sc->sc_rx_map[n] = NULL;
   1042       1.1     jkunz 	}
   1043       1.1     jkunz }
   1044       1.1     jkunz 
   1045       1.1     jkunz 
   1046       1.1     jkunz 
   1047       1.1     jkunz /* timer routine */
   1048       1.1     jkunz void
   1049       1.1     jkunz iee_watchdog(struct ifnet *ifp)
   1050       1.1     jkunz {
   1051       1.1     jkunz 	struct iee_softc *sc = ifp->if_softc;
   1052       1.1     jkunz 
   1053       1.1     jkunz 	(sc->sc_iee_reset)(sc);
   1054       1.1     jkunz 	if (sc->sc_next_tbd != 0)
   1055       1.8     perry 		printf("%s: iee_watchdog: transmit timeout %d\n",
   1056      1.19   tsutsui 		    device_xname(sc->sc_dev), ++sc->sc_tx_timeout);
   1057       1.1     jkunz 	else
   1058       1.8     perry 		printf("%s: iee_watchdog: setup timeout %d\n",
   1059      1.19   tsutsui 		    device_xname(sc->sc_dev), ++sc->sc_setup_timeout);
   1060       1.1     jkunz 	iee_init(ifp);
   1061       1.1     jkunz }
   1062