Home | History | Annotate | Line # | Download | only in ic
      1  1.50  riastrad /*	$NetBSD: dp83932.c,v 1.50 2024/06/29 12:11:11 riastradh Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*-
      4   1.1   thorpej  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1   thorpej  * by Jason R. Thorpe.
      9   1.1   thorpej  *
     10   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     11   1.1   thorpej  * modification, are permitted provided that the following conditions
     12   1.1   thorpej  * are met:
     13   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     14   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     15   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     18   1.1   thorpej  *
     19   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1   thorpej  */
     31   1.1   thorpej 
     32   1.1   thorpej /*
     33   1.1   thorpej  * Device driver for the National Semiconductor DP83932
     34   1.1   thorpej  * Systems-Oriented Network Interface Controller (SONIC).
     35   1.1   thorpej  */
     36   1.5     lukem 
     37   1.5     lukem #include <sys/cdefs.h>
     38  1.50  riastrad __KERNEL_RCSID(0, "$NetBSD: dp83932.c,v 1.50 2024/06/29 12:11:11 riastradh Exp $");
     39   1.1   thorpej 
     40   1.1   thorpej 
     41   1.1   thorpej #include <sys/param.h>
     42   1.1   thorpej #include <sys/systm.h>
     43   1.1   thorpej #include <sys/mbuf.h>
     44   1.1   thorpej #include <sys/kernel.h>
     45   1.1   thorpej #include <sys/socket.h>
     46   1.1   thorpej #include <sys/ioctl.h>
     47   1.1   thorpej #include <sys/errno.h>
     48   1.1   thorpej #include <sys/device.h>
     49   1.1   thorpej 
     50  1.47       rin #include <sys/rndsource.h>
     51  1.47       rin 
     52   1.1   thorpej #include <net/if.h>
     53   1.1   thorpej #include <net/if_dl.h>
     54   1.1   thorpej #include <net/if_ether.h>
     55   1.1   thorpej 
     56   1.1   thorpej #include <net/bpf.h>
     57   1.1   thorpej 
     58  1.19        ad #include <sys/bus.h>
     59  1.19        ad #include <sys/intr.h>
     60   1.1   thorpej 
     61   1.1   thorpej #include <dev/ic/dp83932reg.h>
     62   1.1   thorpej #include <dev/ic/dp83932var.h>
     63   1.1   thorpej 
     64  1.31   tsutsui static void	sonic_start(struct ifnet *);
     65  1.31   tsutsui static void	sonic_watchdog(struct ifnet *);
     66  1.31   tsutsui static int	sonic_ioctl(struct ifnet *, u_long, void *);
     67  1.31   tsutsui static int	sonic_init(struct ifnet *);
     68  1.31   tsutsui static void	sonic_stop(struct ifnet *, int);
     69  1.31   tsutsui 
     70  1.31   tsutsui static bool	sonic_shutdown(device_t, int);
     71  1.31   tsutsui 
     72  1.31   tsutsui static void	sonic_reset(struct sonic_softc *);
     73  1.31   tsutsui static void	sonic_rxdrain(struct sonic_softc *);
     74  1.31   tsutsui static int	sonic_add_rxbuf(struct sonic_softc *, int);
     75  1.31   tsutsui static void	sonic_set_filter(struct sonic_softc *);
     76   1.1   thorpej 
     77  1.31   tsutsui static uint16_t sonic_txintr(struct sonic_softc *);
     78  1.31   tsutsui static void	sonic_rxintr(struct sonic_softc *);
     79   1.1   thorpej 
     80   1.1   thorpej int	sonic_copy_small = 0;
     81   1.1   thorpej 
     82   1.6    bouyer #define ETHER_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN)
     83   1.6    bouyer 
     84   1.1   thorpej /*
     85   1.1   thorpej  * sonic_attach:
     86   1.1   thorpej  *
     87   1.1   thorpej  *	Attach a SONIC interface to the system.
     88   1.1   thorpej  */
     89   1.1   thorpej void
     90   1.1   thorpej sonic_attach(struct sonic_softc *sc, const uint8_t *enaddr)
     91   1.1   thorpej {
     92   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
     93   1.1   thorpej 	int i, rseg, error;
     94   1.1   thorpej 	bus_dma_segment_t seg;
     95   1.1   thorpej 	size_t cdatasize;
     96  1.25   tsutsui 	uint8_t *nullbuf;
     97   1.1   thorpej 
     98   1.1   thorpej 	/*
     99   1.1   thorpej 	 * Allocate the control data structures, and create and load the
    100   1.1   thorpej 	 * DMA map for it.
    101   1.1   thorpej 	 */
    102   1.1   thorpej 	if (sc->sc_32bit)
    103   1.1   thorpej 		cdatasize = sizeof(struct sonic_control_data32);
    104   1.1   thorpej 	else
    105   1.1   thorpej 		cdatasize = sizeof(struct sonic_control_data16);
    106   1.1   thorpej 
    107   1.6    bouyer 	if ((error = bus_dmamem_alloc(sc->sc_dmat, cdatasize + ETHER_PAD_LEN,
    108   1.1   thorpej 	     PAGE_SIZE, (64 * 1024), &seg, 1, &rseg,
    109   1.1   thorpej 	     BUS_DMA_NOWAIT)) != 0) {
    110  1.25   tsutsui 		aprint_error_dev(sc->sc_dev,
    111  1.25   tsutsui 		    "unable to allocate control data, error = %d\n", error);
    112   1.1   thorpej 		goto fail_0;
    113   1.1   thorpej 	}
    114   1.1   thorpej 
    115   1.1   thorpej 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    116  1.14  christos 	    cdatasize + ETHER_PAD_LEN, (void **) &sc->sc_cdata16,
    117  1.43   msaitoh 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    118  1.25   tsutsui 		aprint_error_dev(sc->sc_dev,
    119  1.25   tsutsui 		    "unable to map control data, error = %d\n", error);
    120   1.1   thorpej 		goto fail_1;
    121   1.1   thorpej 	}
    122  1.25   tsutsui 	nullbuf = (uint8_t *)sc->sc_cdata16 + cdatasize;
    123   1.6    bouyer 	memset(nullbuf, 0, ETHER_PAD_LEN);
    124   1.1   thorpej 
    125   1.1   thorpej 	if ((error = bus_dmamap_create(sc->sc_dmat,
    126   1.1   thorpej 	     cdatasize, 1, cdatasize, 0, BUS_DMA_NOWAIT,
    127   1.1   thorpej 	     &sc->sc_cddmamap)) != 0) {
    128  1.25   tsutsui 		aprint_error_dev(sc->sc_dev,
    129  1.25   tsutsui 		    "unable to create control data DMA map, error = %d\n",
    130  1.25   tsutsui 		    error);
    131   1.1   thorpej 		goto fail_2;
    132   1.1   thorpej 	}
    133   1.1   thorpej 
    134   1.1   thorpej 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    135   1.1   thorpej 	     sc->sc_cdata16, cdatasize, NULL, BUS_DMA_NOWAIT)) != 0) {
    136  1.25   tsutsui 		aprint_error_dev(sc->sc_dev,
    137  1.25   tsutsui 		    "unable to load control data DMA map, error = %d\n", error);
    138   1.1   thorpej 		goto fail_3;
    139   1.1   thorpej 	}
    140   1.1   thorpej 
    141   1.1   thorpej 	/*
    142   1.1   thorpej 	 * Create the transmit buffer DMA maps.
    143   1.1   thorpej 	 */
    144   1.1   thorpej 	for (i = 0; i < SONIC_NTXDESC; i++) {
    145   1.1   thorpej 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    146   1.1   thorpej 		     SONIC_NTXFRAGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
    147   1.1   thorpej 		     &sc->sc_txsoft[i].ds_dmamap)) != 0) {
    148  1.25   tsutsui 			aprint_error_dev(sc->sc_dev,
    149  1.25   tsutsui 			    "unable to create tx DMA map %d, error = %d\n",
    150  1.25   tsutsui 			    i, error);
    151   1.1   thorpej 			goto fail_4;
    152   1.1   thorpej 		}
    153   1.1   thorpej 	}
    154   1.1   thorpej 
    155   1.1   thorpej 	/*
    156   1.1   thorpej 	 * Create the receive buffer DMA maps.
    157   1.1   thorpej 	 */
    158   1.1   thorpej 	for (i = 0; i < SONIC_NRXDESC; i++) {
    159   1.1   thorpej 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    160   1.1   thorpej 		     MCLBYTES, 0, BUS_DMA_NOWAIT,
    161   1.1   thorpej 		     &sc->sc_rxsoft[i].ds_dmamap)) != 0) {
    162  1.25   tsutsui 			aprint_error_dev(sc->sc_dev,
    163  1.25   tsutsui 			    "unable to create rx DMA map %d, error = %d\n",
    164  1.25   tsutsui 			    i, error);
    165   1.1   thorpej 			goto fail_5;
    166   1.1   thorpej 		}
    167   1.1   thorpej 		sc->sc_rxsoft[i].ds_mbuf = NULL;
    168   1.1   thorpej 	}
    169   1.1   thorpej 
    170   1.1   thorpej 	/*
    171   1.6    bouyer 	 * create and map the pad buffer
    172   1.6    bouyer 	 */
    173   1.6    bouyer 	if ((error = bus_dmamap_create(sc->sc_dmat, ETHER_PAD_LEN, 1,
    174   1.8   tsutsui 	    ETHER_PAD_LEN, 0, BUS_DMA_NOWAIT, &sc->sc_nulldmamap)) != 0) {
    175  1.25   tsutsui 		aprint_error_dev(sc->sc_dev,
    176  1.25   tsutsui 		    "unable to create pad buffer DMA map, error = %d\n", error);
    177   1.6    bouyer 		goto fail_5;
    178   1.6    bouyer 	}
    179   1.6    bouyer 
    180   1.6    bouyer 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_nulldmamap,
    181   1.6    bouyer 	    nullbuf, ETHER_PAD_LEN, NULL, BUS_DMA_NOWAIT)) != 0) {
    182  1.25   tsutsui 		aprint_error_dev(sc->sc_dev,
    183  1.25   tsutsui 		    "unable to load pad buffer DMA map, error = %d\n", error);
    184   1.6    bouyer 		goto fail_6;
    185   1.6    bouyer 	}
    186   1.6    bouyer 	bus_dmamap_sync(sc->sc_dmat, sc->sc_nulldmamap, 0, ETHER_PAD_LEN,
    187   1.6    bouyer 	    BUS_DMASYNC_PREWRITE);
    188   1.6    bouyer 
    189   1.6    bouyer 	/*
    190   1.1   thorpej 	 * Reset the chip to a known state.
    191   1.1   thorpej 	 */
    192   1.1   thorpej 	sonic_reset(sc);
    193   1.1   thorpej 
    194  1.25   tsutsui 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
    195   1.1   thorpej 	    ether_sprintf(enaddr));
    196   1.1   thorpej 
    197  1.25   tsutsui 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    198   1.1   thorpej 	ifp->if_softc = sc;
    199   1.1   thorpej 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    200   1.1   thorpej 	ifp->if_ioctl = sonic_ioctl;
    201   1.1   thorpej 	ifp->if_start = sonic_start;
    202   1.1   thorpej 	ifp->if_watchdog = sonic_watchdog;
    203   1.1   thorpej 	ifp->if_init = sonic_init;
    204   1.1   thorpej 	ifp->if_stop = sonic_stop;
    205   1.1   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    206   1.1   thorpej 
    207   1.1   thorpej 	/*
    208  1.27   tsutsui 	 * We can support 802.1Q VLAN-sized frames.
    209  1.22   tsutsui 	 */
    210  1.22   tsutsui 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    211  1.22   tsutsui 
    212  1.22   tsutsui 	/*
    213   1.1   thorpej 	 * Attach the interface.
    214   1.1   thorpej 	 */
    215   1.1   thorpej 	if_attach(ifp);
    216  1.40     ozaki 	if_deferred_start_init(ifp, NULL);
    217   1.1   thorpej 	ether_ifattach(ifp, enaddr);
    218   1.1   thorpej 
    219  1.47       rin 	rnd_attach_source(&sc->sc_rndsource, ifp->if_xname, RND_TYPE_NET,
    220  1.47       rin 	    RND_FLAG_DEFAULT);
    221  1.47       rin 
    222   1.1   thorpej 	/*
    223   1.1   thorpej 	 * Make sure the interface is shutdown during reboot.
    224   1.1   thorpej 	 */
    225  1.30   tsutsui 	if (pmf_device_register1(sc->sc_dev, NULL, NULL, sonic_shutdown))
    226  1.32   tsutsui 		pmf_class_network_register(sc->sc_dev, ifp);
    227  1.30   tsutsui 	else
    228  1.25   tsutsui 		aprint_error_dev(sc->sc_dev,
    229  1.29   tsutsui 		    "couldn't establish power handler\n");
    230  1.29   tsutsui 
    231   1.1   thorpej 	return;
    232   1.1   thorpej 
    233   1.1   thorpej 	/*
    234   1.1   thorpej 	 * Free any resources we've allocated during the failed attach
    235   1.1   thorpej 	 * attempt.  Do this in reverse order and fall through.
    236   1.1   thorpej 	 */
    237   1.6    bouyer  fail_6:
    238   1.6    bouyer 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_nulldmamap);
    239   1.1   thorpej  fail_5:
    240   1.1   thorpej 	for (i = 0; i < SONIC_NRXDESC; i++) {
    241   1.1   thorpej 		if (sc->sc_rxsoft[i].ds_dmamap != NULL)
    242   1.1   thorpej 			bus_dmamap_destroy(sc->sc_dmat,
    243   1.1   thorpej 			    sc->sc_rxsoft[i].ds_dmamap);
    244   1.1   thorpej 	}
    245   1.1   thorpej  fail_4:
    246   1.1   thorpej 	for (i = 0; i < SONIC_NTXDESC; i++) {
    247   1.1   thorpej 		if (sc->sc_txsoft[i].ds_dmamap != NULL)
    248   1.1   thorpej 			bus_dmamap_destroy(sc->sc_dmat,
    249   1.1   thorpej 			    sc->sc_txsoft[i].ds_dmamap);
    250   1.1   thorpej 	}
    251   1.1   thorpej 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    252   1.1   thorpej  fail_3:
    253   1.1   thorpej 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    254   1.1   thorpej  fail_2:
    255  1.25   tsutsui 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_cdata16, cdatasize);
    256   1.1   thorpej  fail_1:
    257   1.1   thorpej 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    258   1.1   thorpej  fail_0:
    259   1.1   thorpej 	return;
    260   1.1   thorpej }
    261   1.1   thorpej 
    262   1.1   thorpej /*
    263   1.1   thorpej  * sonic_shutdown:
    264   1.1   thorpej  *
    265   1.1   thorpej  *	Make sure the interface is stopped at reboot.
    266   1.1   thorpej  */
    267  1.28   tsutsui bool
    268  1.28   tsutsui sonic_shutdown(device_t self, int howto)
    269   1.1   thorpej {
    270  1.28   tsutsui 	struct sonic_softc *sc = device_private(self);
    271   1.1   thorpej 
    272   1.1   thorpej 	sonic_stop(&sc->sc_ethercom.ec_if, 1);
    273  1.28   tsutsui 
    274  1.28   tsutsui 	return true;
    275   1.1   thorpej }
    276   1.1   thorpej 
    277   1.1   thorpej /*
    278   1.1   thorpej  * sonic_start:		[ifnet interface function]
    279   1.1   thorpej  *
    280   1.1   thorpej  *	Start packet transmission on the interface.
    281   1.1   thorpej  */
    282   1.1   thorpej void
    283   1.1   thorpej sonic_start(struct ifnet *ifp)
    284   1.1   thorpej {
    285   1.1   thorpej 	struct sonic_softc *sc = ifp->if_softc;
    286   1.1   thorpej 	struct mbuf *m0, *m;
    287   1.1   thorpej 	struct sonic_tda16 *tda16;
    288   1.1   thorpej 	struct sonic_tda32 *tda32;
    289   1.1   thorpej 	struct sonic_descsoft *ds;
    290   1.1   thorpej 	bus_dmamap_t dmamap;
    291   1.9   tsutsui 	int error, olasttx, nexttx, opending, totlen, olseg;
    292   1.9   tsutsui 	int seg = 0;	/* XXX: gcc */
    293   1.1   thorpej 
    294  1.46   thorpej 	if ((ifp->if_flags & IFF_RUNNING) != IFF_RUNNING)
    295   1.1   thorpej 		return;
    296   1.1   thorpej 
    297   1.1   thorpej 	/*
    298   1.1   thorpej 	 * Remember the previous txpending and the current "last txdesc
    299   1.1   thorpej 	 * used" index.
    300   1.1   thorpej 	 */
    301   1.1   thorpej 	opending = sc->sc_txpending;
    302   1.1   thorpej 	olasttx = sc->sc_txlast;
    303   1.1   thorpej 
    304   1.1   thorpej 	/*
    305   1.1   thorpej 	 * Loop through the send queue, setting up transmit descriptors
    306   1.1   thorpej 	 * until we drain the queue, or use up all available transmit
    307   1.1   thorpej 	 * descriptors.  Leave one at the end for sanity's sake.
    308   1.1   thorpej 	 */
    309   1.1   thorpej 	while (sc->sc_txpending < (SONIC_NTXDESC - 1)) {
    310   1.1   thorpej 		/*
    311   1.1   thorpej 		 * Grab a packet off the queue.
    312   1.1   thorpej 		 */
    313   1.1   thorpej 		IFQ_POLL(&ifp->if_snd, m0);
    314   1.1   thorpej 		if (m0 == NULL)
    315   1.1   thorpej 			break;
    316   1.1   thorpej 		m = NULL;
    317   1.1   thorpej 
    318   1.1   thorpej 		/*
    319   1.1   thorpej 		 * Get the next available transmit descriptor.
    320   1.1   thorpej 		 */
    321   1.1   thorpej 		nexttx = SONIC_NEXTTX(sc->sc_txlast);
    322   1.1   thorpej 		ds = &sc->sc_txsoft[nexttx];
    323   1.1   thorpej 		dmamap = ds->ds_dmamap;
    324   1.1   thorpej 
    325   1.1   thorpej 		/*
    326   1.1   thorpej 		 * Load the DMA map.  If this fails, the packet either
    327   1.1   thorpej 		 * didn't fit in the allotted number of frags, or we were
    328   1.1   thorpej 		 * short on resources.  In this case, we'll copy and try
    329   1.1   thorpej 		 * again.
    330   1.1   thorpej 		 */
    331   1.6    bouyer 		if ((error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    332  1.43   msaitoh 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT)) != 0 ||
    333   1.6    bouyer 		    (m0->m_pkthdr.len < ETHER_PAD_LEN &&
    334   1.7   tsutsui 		    dmamap->dm_nsegs == SONIC_NTXFRAGS)) {
    335   1.6    bouyer 			if (error == 0)
    336   1.6    bouyer 				bus_dmamap_unload(sc->sc_dmat, dmamap);
    337   1.1   thorpej 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    338   1.1   thorpej 			if (m == NULL) {
    339  1.25   tsutsui 				printf("%s: unable to allocate Tx mbuf\n",
    340  1.25   tsutsui 				    device_xname(sc->sc_dev));
    341   1.1   thorpej 				break;
    342   1.1   thorpej 			}
    343   1.1   thorpej 			if (m0->m_pkthdr.len > MHLEN) {
    344   1.1   thorpej 				MCLGET(m, M_DONTWAIT);
    345   1.1   thorpej 				if ((m->m_flags & M_EXT) == 0) {
    346  1.25   tsutsui 					printf("%s: unable to allocate Tx "
    347  1.25   tsutsui 					    "cluster\n",
    348  1.25   tsutsui 					    device_xname(sc->sc_dev));
    349   1.1   thorpej 					m_freem(m);
    350   1.1   thorpej 					break;
    351   1.1   thorpej 				}
    352   1.1   thorpej 			}
    353  1.14  christos 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
    354   1.1   thorpej 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    355   1.1   thorpej 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    356  1.43   msaitoh 			    m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
    357   1.1   thorpej 			if (error) {
    358  1.25   tsutsui 				printf("%s: unable to load Tx buffer, "
    359  1.25   tsutsui 				    "error = %d\n", device_xname(sc->sc_dev),
    360  1.25   tsutsui 				    error);
    361   1.1   thorpej 				m_freem(m);
    362   1.1   thorpej 				break;
    363   1.1   thorpej 			}
    364   1.1   thorpej 		}
    365   1.1   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    366   1.1   thorpej 		if (m != NULL) {
    367   1.1   thorpej 			m_freem(m0);
    368   1.1   thorpej 			m0 = m;
    369   1.1   thorpej 		}
    370   1.1   thorpej 
    371   1.1   thorpej 		/*
    372   1.1   thorpej 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    373   1.1   thorpej 		 */
    374   1.1   thorpej 
    375   1.1   thorpej 		/* Sync the DMA map. */
    376   1.1   thorpej 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    377   1.1   thorpej 		    BUS_DMASYNC_PREWRITE);
    378   1.1   thorpej 
    379   1.1   thorpej 		/*
    380   1.1   thorpej 		 * Store a pointer to the packet so we can free it later.
    381   1.1   thorpej 		 */
    382   1.1   thorpej 		ds->ds_mbuf = m0;
    383   1.1   thorpej 
    384   1.1   thorpej 		/*
    385   1.1   thorpej 		 * Initialize the transmit descriptor.
    386   1.1   thorpej 		 */
    387   1.1   thorpej 		totlen = 0;
    388   1.1   thorpej 		if (sc->sc_32bit) {
    389   1.1   thorpej 			tda32 = &sc->sc_tda32[nexttx];
    390   1.1   thorpej 			for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
    391   1.1   thorpej 				tda32->tda_frags[seg].frag_ptr1 =
    392   1.1   thorpej 				    htosonic32(sc,
    393   1.1   thorpej 				    (dmamap->dm_segs[seg].ds_addr >> 16) &
    394   1.1   thorpej 				    0xffff);
    395   1.1   thorpej 				tda32->tda_frags[seg].frag_ptr0 =
    396   1.1   thorpej 				    htosonic32(sc,
    397   1.1   thorpej 				    dmamap->dm_segs[seg].ds_addr & 0xffff);
    398   1.1   thorpej 				tda32->tda_frags[seg].frag_size =
    399   1.1   thorpej 				    htosonic32(sc, dmamap->dm_segs[seg].ds_len);
    400   1.1   thorpej 				totlen += dmamap->dm_segs[seg].ds_len;
    401   1.1   thorpej 			}
    402   1.6    bouyer 			if (totlen < ETHER_PAD_LEN) {
    403   1.6    bouyer 				tda32->tda_frags[seg].frag_ptr1 =
    404   1.6    bouyer 				    htosonic32(sc,
    405   1.7   tsutsui 				    (sc->sc_nulldma >> 16) & 0xffff);
    406   1.6    bouyer 				tda32->tda_frags[seg].frag_ptr0 =
    407   1.8   tsutsui 				    htosonic32(sc, sc->sc_nulldma & 0xffff);
    408   1.6    bouyer 				tda32->tda_frags[seg].frag_size =
    409   1.6    bouyer 				    htosonic32(sc, ETHER_PAD_LEN - totlen);
    410   1.6    bouyer 				totlen = ETHER_PAD_LEN;
    411   1.8   tsutsui 				seg++;
    412   1.1   thorpej 			}
    413  1.12     perry 
    414   1.1   thorpej 			tda32->tda_status = 0;
    415   1.1   thorpej 			tda32->tda_pktconfig = 0;
    416   1.1   thorpej 			tda32->tda_pktsize = htosonic32(sc, totlen);
    417   1.1   thorpej 			tda32->tda_fragcnt = htosonic32(sc, seg);
    418   1.1   thorpej 
    419   1.1   thorpej 			/* Link it up. */
    420   1.1   thorpej 			tda32->tda_frags[seg].frag_ptr0 =
    421   1.2   thorpej 			    htosonic32(sc, SONIC_CDTXADDR32(sc,
    422   1.1   thorpej 			    SONIC_NEXTTX(nexttx)) & 0xffff);
    423   1.2   thorpej 
    424   1.2   thorpej 			/* Sync the Tx descriptor. */
    425   1.2   thorpej 			SONIC_CDTXSYNC32(sc, nexttx,
    426  1.43   msaitoh 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    427   1.1   thorpej 		} else {
    428   1.1   thorpej 			tda16 = &sc->sc_tda16[nexttx];
    429   1.1   thorpej 			for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
    430   1.1   thorpej 				tda16->tda_frags[seg].frag_ptr1 =
    431   1.1   thorpej 				    htosonic16(sc,
    432   1.1   thorpej 				    (dmamap->dm_segs[seg].ds_addr >> 16) &
    433   1.1   thorpej 				    0xffff);
    434   1.1   thorpej 				tda16->tda_frags[seg].frag_ptr0 =
    435   1.1   thorpej 				    htosonic16(sc,
    436   1.1   thorpej 				    dmamap->dm_segs[seg].ds_addr & 0xffff);
    437   1.1   thorpej 				tda16->tda_frags[seg].frag_size =
    438   1.1   thorpej 				    htosonic16(sc, dmamap->dm_segs[seg].ds_len);
    439   1.1   thorpej 				totlen += dmamap->dm_segs[seg].ds_len;
    440   1.1   thorpej 			}
    441   1.6    bouyer 			if (totlen < ETHER_PAD_LEN) {
    442   1.6    bouyer 				tda16->tda_frags[seg].frag_ptr1 =
    443   1.6    bouyer 				    htosonic16(sc,
    444   1.6    bouyer 				    (sc->sc_nulldma >> 16) & 0xffff);
    445   1.6    bouyer 				tda16->tda_frags[seg].frag_ptr0 =
    446   1.8   tsutsui 				    htosonic16(sc, sc->sc_nulldma & 0xffff);
    447   1.6    bouyer 				tda16->tda_frags[seg].frag_size =
    448   1.6    bouyer 				    htosonic16(sc, ETHER_PAD_LEN - totlen);
    449   1.6    bouyer 				totlen = ETHER_PAD_LEN;
    450   1.8   tsutsui 				seg++;
    451   1.1   thorpej 			}
    452   1.1   thorpej 
    453   1.1   thorpej 			tda16->tda_status = 0;
    454   1.1   thorpej 			tda16->tda_pktconfig = 0;
    455   1.1   thorpej 			tda16->tda_pktsize = htosonic16(sc, totlen);
    456   1.1   thorpej 			tda16->tda_fragcnt = htosonic16(sc, seg);
    457   1.1   thorpej 
    458   1.1   thorpej 			/* Link it up. */
    459   1.1   thorpej 			tda16->tda_frags[seg].frag_ptr0 =
    460   1.2   thorpej 			    htosonic16(sc, SONIC_CDTXADDR16(sc,
    461   1.1   thorpej 			    SONIC_NEXTTX(nexttx)) & 0xffff);
    462   1.2   thorpej 
    463   1.2   thorpej 			/* Sync the Tx descriptor. */
    464   1.2   thorpej 			SONIC_CDTXSYNC16(sc, nexttx,
    465  1.43   msaitoh 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    466   1.1   thorpej 		}
    467   1.1   thorpej 
    468   1.1   thorpej 		/* Advance the Tx pointer. */
    469   1.1   thorpej 		sc->sc_txpending++;
    470   1.1   thorpej 		sc->sc_txlast = nexttx;
    471   1.1   thorpej 
    472   1.1   thorpej 		/*
    473   1.1   thorpej 		 * Pass the packet to any BPF listeners.
    474   1.1   thorpej 		 */
    475  1.42   msaitoh 		bpf_mtap(ifp, m0, BPF_D_OUT);
    476   1.1   thorpej 	}
    477   1.1   thorpej 
    478   1.1   thorpej 	if (sc->sc_txpending != opending) {
    479   1.1   thorpej 		/*
    480   1.1   thorpej 		 * We enqueued packets.  If the transmitter was idle,
    481   1.1   thorpej 		 * reset the txdirty pointer.
    482   1.1   thorpej 		 */
    483   1.1   thorpej 		if (opending == 0)
    484   1.1   thorpej 			sc->sc_txdirty = SONIC_NEXTTX(olasttx);
    485   1.1   thorpej 
    486   1.1   thorpej 		/*
    487   1.1   thorpej 		 * Stop the SONIC on the last packet we've set up,
    488   1.1   thorpej 		 * and clear end-of-list on the descriptor previous
    489   1.1   thorpej 		 * to our new chain.
    490   1.1   thorpej 		 *
    491   1.1   thorpej 		 * NOTE: our `seg' variable should still be valid!
    492   1.1   thorpej 		 */
    493   1.1   thorpej 		if (sc->sc_32bit) {
    494   1.1   thorpej 			olseg =
    495   1.1   thorpej 			    sonic32toh(sc, sc->sc_tda32[olasttx].tda_fragcnt);
    496   1.1   thorpej 			sc->sc_tda32[sc->sc_txlast].tda_frags[seg].frag_ptr0 |=
    497   1.1   thorpej 			    htosonic32(sc, TDA_LINK_EOL);
    498   1.2   thorpej 			SONIC_CDTXSYNC32(sc, sc->sc_txlast,
    499  1.43   msaitoh 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    500   1.1   thorpej 			sc->sc_tda32[olasttx].tda_frags[olseg].frag_ptr0 &=
    501   1.1   thorpej 			    htosonic32(sc, ~TDA_LINK_EOL);
    502   1.2   thorpej 			SONIC_CDTXSYNC32(sc, olasttx,
    503  1.43   msaitoh 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    504   1.1   thorpej 		} else {
    505   1.1   thorpej 			olseg =
    506   1.1   thorpej 			    sonic16toh(sc, sc->sc_tda16[olasttx].tda_fragcnt);
    507   1.1   thorpej 			sc->sc_tda16[sc->sc_txlast].tda_frags[seg].frag_ptr0 |=
    508   1.1   thorpej 			    htosonic16(sc, TDA_LINK_EOL);
    509   1.2   thorpej 			SONIC_CDTXSYNC16(sc, sc->sc_txlast,
    510  1.43   msaitoh 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    511   1.1   thorpej 			sc->sc_tda16[olasttx].tda_frags[olseg].frag_ptr0 &=
    512   1.1   thorpej 			    htosonic16(sc, ~TDA_LINK_EOL);
    513   1.2   thorpej 			SONIC_CDTXSYNC16(sc, olasttx,
    514  1.43   msaitoh 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    515   1.1   thorpej 		}
    516   1.1   thorpej 
    517   1.1   thorpej 		/* Start the transmitter. */
    518   1.1   thorpej 		CSR_WRITE(sc, SONIC_CR, CR_TXP);
    519   1.1   thorpej 
    520   1.1   thorpej 		/* Set a watchdog timer in case the chip flakes out. */
    521   1.1   thorpej 		ifp->if_timer = 5;
    522   1.1   thorpej 	}
    523   1.1   thorpej }
    524   1.1   thorpej 
    525   1.1   thorpej /*
    526   1.1   thorpej  * sonic_watchdog:	[ifnet interface function]
    527   1.1   thorpej  *
    528   1.1   thorpej  *	Watchdog timer handler.
    529   1.1   thorpej  */
    530   1.1   thorpej void
    531   1.1   thorpej sonic_watchdog(struct ifnet *ifp)
    532   1.1   thorpej {
    533   1.1   thorpej 	struct sonic_softc *sc = ifp->if_softc;
    534   1.1   thorpej 
    535  1.25   tsutsui 	printf("%s: device timeout\n", device_xname(sc->sc_dev));
    536  1.45   thorpej 	if_statinc(ifp, if_oerrors);
    537   1.1   thorpej 
    538  1.25   tsutsui 	(void)sonic_init(ifp);
    539   1.1   thorpej }
    540   1.1   thorpej 
    541   1.1   thorpej /*
    542   1.1   thorpej  * sonic_ioctl:		[ifnet interface function]
    543   1.1   thorpej  *
    544   1.1   thorpej  *	Handle control requests from the operator.
    545   1.1   thorpej  */
    546   1.1   thorpej int
    547  1.14  christos sonic_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    548   1.1   thorpej {
    549   1.1   thorpej 	int s, error;
    550   1.1   thorpej 
    551   1.1   thorpej 	s = splnet();
    552   1.1   thorpej 
    553  1.20    dyoung 	error = ether_ioctl(ifp, cmd, data);
    554  1.20    dyoung 	if (error == ENETRESET) {
    555  1.20    dyoung 		/*
    556  1.20    dyoung 		 * Multicast list has changed; set the hardware
    557  1.20    dyoung 		 * filter accordingly.
    558  1.20    dyoung 		 */
    559  1.20    dyoung 		if (ifp->if_flags & IFF_RUNNING)
    560  1.25   tsutsui 			(void)sonic_init(ifp);
    561  1.20    dyoung 		error = 0;
    562   1.1   thorpej 	}
    563   1.1   thorpej 
    564   1.1   thorpej 	splx(s);
    565  1.25   tsutsui 	return error;
    566   1.1   thorpej }
    567   1.1   thorpej 
    568   1.1   thorpej /*
    569   1.1   thorpej  * sonic_intr:
    570   1.1   thorpej  *
    571   1.1   thorpej  *	Interrupt service routine.
    572   1.1   thorpej  */
    573   1.1   thorpej int
    574   1.1   thorpej sonic_intr(void *arg)
    575   1.1   thorpej {
    576   1.1   thorpej 	struct sonic_softc *sc = arg;
    577   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    578   1.1   thorpej 	uint16_t isr;
    579   1.1   thorpej 	int handled = 0, wantinit;
    580   1.1   thorpej 
    581   1.1   thorpej 	for (wantinit = 0; wantinit == 0;) {
    582   1.1   thorpej 		isr = CSR_READ(sc, SONIC_ISR) & sc->sc_imr;
    583   1.1   thorpej 		if (isr == 0)
    584   1.1   thorpej 			break;
    585   1.1   thorpej 		CSR_WRITE(sc, SONIC_ISR, isr);	/* ACK */
    586   1.1   thorpej 
    587   1.1   thorpej 		handled = 1;
    588   1.1   thorpej 
    589   1.1   thorpej 		if (isr & IMR_PRX)
    590   1.1   thorpej 			sonic_rxintr(sc);
    591   1.1   thorpej 
    592  1.43   msaitoh 		if (isr & (IMR_PTX | IMR_TXER)) {
    593   1.1   thorpej 			if (sonic_txintr(sc) & TCR_FU) {
    594   1.1   thorpej 				printf("%s: transmit FIFO underrun\n",
    595  1.25   tsutsui 				    device_xname(sc->sc_dev));
    596   1.1   thorpej 				wantinit = 1;
    597   1.1   thorpej 			}
    598   1.1   thorpej 		}
    599   1.1   thorpej 
    600  1.43   msaitoh 		if (isr & (IMR_RFO | IMR_RBA | IMR_RBE | IMR_RDE)) {
    601   1.1   thorpej #define	PRINTERR(bit, str)						\
    602   1.1   thorpej 			if (isr & (bit))				\
    603  1.25   tsutsui 				printf("%s: %s\n",device_xname(sc->sc_dev), str)
    604   1.1   thorpej 			PRINTERR(IMR_RFO, "receive FIFO overrun");
    605   1.1   thorpej 			PRINTERR(IMR_RBA, "receive buffer exceeded");
    606   1.1   thorpej 			PRINTERR(IMR_RBE, "receive buffers exhausted");
    607   1.1   thorpej 			PRINTERR(IMR_RDE, "receive descriptors exhausted");
    608   1.1   thorpej 			wantinit = 1;
    609   1.1   thorpej 		}
    610   1.1   thorpej 	}
    611   1.1   thorpej 
    612   1.1   thorpej 	if (handled) {
    613   1.1   thorpej 		if (wantinit)
    614  1.25   tsutsui 			(void)sonic_init(ifp);
    615  1.40     ozaki 		if_schedule_deferred_start(ifp);
    616   1.1   thorpej 	}
    617   1.1   thorpej 
    618  1.25   tsutsui 	return handled;
    619   1.1   thorpej }
    620   1.1   thorpej 
    621   1.1   thorpej /*
    622   1.1   thorpej  * sonic_txintr:
    623   1.1   thorpej  *
    624   1.1   thorpej  *	Helper; handle transmit complete interrupts.
    625   1.1   thorpej  */
    626   1.1   thorpej uint16_t
    627   1.1   thorpej sonic_txintr(struct sonic_softc *sc)
    628   1.1   thorpej {
    629   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    630   1.1   thorpej 	struct sonic_descsoft *ds;
    631   1.1   thorpej 	struct sonic_tda32 *tda32;
    632   1.1   thorpej 	struct sonic_tda16 *tda16;
    633   1.1   thorpej 	uint16_t status, totstat = 0;
    634  1.47       rin 	int i, count;
    635   1.1   thorpej 
    636  1.47       rin 	count = 0;
    637   1.1   thorpej 	for (i = sc->sc_txdirty; sc->sc_txpending != 0;
    638   1.1   thorpej 	     i = SONIC_NEXTTX(i), sc->sc_txpending--) {
    639   1.1   thorpej 		ds = &sc->sc_txsoft[i];
    640   1.1   thorpej 
    641   1.1   thorpej 		if (sc->sc_32bit) {
    642   1.2   thorpej 			SONIC_CDTXSYNC32(sc, i,
    643  1.43   msaitoh 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    644   1.1   thorpej 			tda32 = &sc->sc_tda32[i];
    645   1.1   thorpej 			status = sonic32toh(sc, tda32->tda_status);
    646  1.15   tsutsui 			SONIC_CDTXSYNC32(sc, i, BUS_DMASYNC_PREREAD);
    647   1.1   thorpej 		} else {
    648   1.2   thorpej 			SONIC_CDTXSYNC16(sc, i,
    649  1.43   msaitoh 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    650   1.1   thorpej 			tda16 = &sc->sc_tda16[i];
    651   1.1   thorpej 			status = sonic16toh(sc, tda16->tda_status);
    652  1.15   tsutsui 			SONIC_CDTXSYNC16(sc, i, BUS_DMASYNC_PREREAD);
    653   1.1   thorpej 		}
    654   1.1   thorpej 
    655  1.43   msaitoh 		if ((status & ~(TCR_EXDIS |TCR_CRCI |TCR_POWC |TCR_PINT)) == 0)
    656   1.1   thorpej 			break;
    657   1.1   thorpej 
    658   1.1   thorpej 		totstat |= status;
    659   1.1   thorpej 
    660   1.1   thorpej 		bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    661   1.1   thorpej 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    662   1.1   thorpej 		bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
    663   1.1   thorpej 		m_freem(ds->ds_mbuf);
    664   1.1   thorpej 		ds->ds_mbuf = NULL;
    665   1.1   thorpej 
    666   1.1   thorpej 		/*
    667   1.1   thorpej 		 * Check for errors and collisions.
    668   1.1   thorpej 		 */
    669  1.45   thorpej 		net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
    670  1.47       rin 		if (status & TCR_PTX) {
    671  1.50  riastrad 			if_statinc_ref(ifp, nsr, if_opackets);
    672  1.47       rin 			count++;
    673  1.50  riastrad 		} else {
    674  1.50  riastrad 			if_statinc_ref(ifp, nsr, if_oerrors);
    675  1.50  riastrad 		}
    676  1.50  riastrad 		if (TDA_STATUS_NCOL(status)) {
    677  1.50  riastrad 			if_statadd_ref(ifp, nsr, if_collisions,
    678  1.45   thorpej 			    TDA_STATUS_NCOL(status));
    679  1.50  riastrad 		}
    680  1.45   thorpej 		IF_STAT_PUTREF(ifp);
    681   1.1   thorpej 	}
    682   1.1   thorpej 
    683   1.1   thorpej 	/* Update the dirty transmit buffer pointer. */
    684   1.1   thorpej 	sc->sc_txdirty = i;
    685   1.1   thorpej 
    686   1.1   thorpej 	/*
    687   1.1   thorpej 	 * Cancel the watchdog timer if there are no pending
    688   1.1   thorpej 	 * transmissions.
    689   1.1   thorpej 	 */
    690   1.1   thorpej 	if (sc->sc_txpending == 0)
    691   1.1   thorpej 		ifp->if_timer = 0;
    692   1.1   thorpej 
    693  1.48   tsutsui 	if (totstat != 0)
    694  1.48   tsutsui 		rnd_add_uint32(&sc->sc_rndsource, totstat);
    695  1.47       rin 
    696  1.25   tsutsui 	return totstat;
    697   1.1   thorpej }
    698   1.1   thorpej 
    699   1.1   thorpej /*
    700   1.1   thorpej  * sonic_rxintr:
    701   1.1   thorpej  *
    702   1.1   thorpej  *	Helper; handle receive interrupts.
    703   1.1   thorpej  */
    704   1.1   thorpej void
    705   1.1   thorpej sonic_rxintr(struct sonic_softc *sc)
    706   1.1   thorpej {
    707   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    708   1.1   thorpej 	struct sonic_descsoft *ds;
    709   1.1   thorpej 	struct sonic_rda32 *rda32;
    710   1.1   thorpej 	struct sonic_rda16 *rda16;
    711   1.1   thorpej 	struct mbuf *m;
    712  1.47       rin 	int i, len, count;
    713  1.36    martin 	uint16_t status, bytecount /*, ptr0, ptr1, seqno */;
    714   1.1   thorpej 
    715  1.47       rin 	count = 0;
    716   1.1   thorpej 	for (i = sc->sc_rxptr;; i = SONIC_NEXTRX(i)) {
    717   1.1   thorpej 		ds = &sc->sc_rxsoft[i];
    718   1.1   thorpej 
    719   1.1   thorpej 		if (sc->sc_32bit) {
    720   1.2   thorpej 			SONIC_CDRXSYNC32(sc, i,
    721  1.43   msaitoh 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    722   1.1   thorpej 			rda32 = &sc->sc_rda32[i];
    723  1.15   tsutsui 			SONIC_CDRXSYNC32(sc, i, BUS_DMASYNC_PREREAD);
    724   1.1   thorpej 			if (rda32->rda_inuse != 0)
    725   1.1   thorpej 				break;
    726   1.1   thorpej 			status = sonic32toh(sc, rda32->rda_status);
    727   1.1   thorpej 			bytecount = sonic32toh(sc, rda32->rda_bytecount);
    728  1.36    martin 			/* ptr0 = sonic32toh(sc, rda32->rda_pkt_ptr0); */
    729  1.36    martin 			/* ptr1 = sonic32toh(sc, rda32->rda_pkt_ptr1); */
    730  1.36    martin 			/* seqno = sonic32toh(sc, rda32->rda_seqno); */
    731   1.1   thorpej 		} else {
    732   1.2   thorpej 			SONIC_CDRXSYNC16(sc, i,
    733  1.43   msaitoh 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    734   1.1   thorpej 			rda16 = &sc->sc_rda16[i];
    735  1.15   tsutsui 			SONIC_CDRXSYNC16(sc, i, BUS_DMASYNC_PREREAD);
    736   1.1   thorpej 			if (rda16->rda_inuse != 0)
    737   1.1   thorpej 				break;
    738   1.1   thorpej 			status = sonic16toh(sc, rda16->rda_status);
    739   1.1   thorpej 			bytecount = sonic16toh(sc, rda16->rda_bytecount);
    740  1.36    martin 			/* ptr0 = sonic16toh(sc, rda16->rda_pkt_ptr0); */
    741  1.36    martin 			/* ptr1 = sonic16toh(sc, rda16->rda_pkt_ptr1); */
    742  1.36    martin 			/* seqno = sonic16toh(sc, rda16->rda_seqno); */
    743   1.1   thorpej 		}
    744   1.1   thorpej 
    745   1.1   thorpej 		/*
    746   1.1   thorpej 		 * Make absolutely sure this is the only packet
    747   1.1   thorpej 		 * in this receive buffer.  Our entire Rx buffer
    748   1.1   thorpej 		 * management scheme depends on this, and if the
    749   1.1   thorpej 		 * SONIC didn't follow our rule, it means we've
    750   1.1   thorpej 		 * misconfigured it.
    751   1.1   thorpej 		 */
    752   1.1   thorpej 		KASSERT(status & RCR_LPKT);
    753   1.1   thorpej 
    754   1.1   thorpej 		/*
    755   1.1   thorpej 		 * Make sure the packet arrived OK.  If an error occurred,
    756   1.1   thorpej 		 * update stats and reset the descriptor.  The buffer will
    757   1.1   thorpej 		 * be reused the next time the descriptor comes up in the
    758   1.1   thorpej 		 * ring.
    759   1.1   thorpej 		 */
    760   1.1   thorpej 		if ((status & RCR_PRX) == 0) {
    761   1.1   thorpej 			if (status & RCR_FAER)
    762  1.25   tsutsui 				printf("%s: Rx frame alignment error\n",
    763  1.25   tsutsui 				    device_xname(sc->sc_dev));
    764   1.1   thorpej 			else if (status & RCR_CRCR)
    765  1.25   tsutsui 				printf("%s: Rx CRC error\n",
    766  1.25   tsutsui 				    device_xname(sc->sc_dev));
    767  1.45   thorpej 			if_statinc(ifp, if_ierrors);
    768   1.1   thorpej 			SONIC_INIT_RXDESC(sc, i);
    769   1.1   thorpej 			continue;
    770   1.1   thorpej 		}
    771   1.1   thorpej 
    772   1.1   thorpej 		bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    773   1.1   thorpej 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
    774   1.1   thorpej 
    775   1.1   thorpej 		/*
    776   1.1   thorpej 		 * The SONIC includes the CRC with every packet.
    777   1.1   thorpej 		 */
    778  1.11   thorpej 		len = bytecount - ETHER_CRC_LEN;
    779   1.1   thorpej 
    780   1.1   thorpej 		/*
    781   1.1   thorpej 		 * Ok, if the chip is in 32-bit mode, then receive
    782   1.1   thorpej 		 * buffers must be aligned to 32-bit boundaries,
    783   1.1   thorpej 		 * which means the payload is misaligned.  In this
    784   1.1   thorpej 		 * case, we must allocate a new mbuf, and copy the
    785   1.1   thorpej 		 * packet into it, scooted forward 2 bytes to ensure
    786   1.1   thorpej 		 * proper alignment.
    787   1.1   thorpej 		 *
    788   1.1   thorpej 		 * Note, in 16-bit mode, we can configure the SONIC
    789   1.1   thorpej 		 * to do what we want, and we have.
    790   1.1   thorpej 		 */
    791   1.1   thorpej #ifndef __NO_STRICT_ALIGNMENT
    792   1.1   thorpej 		if (sc->sc_32bit) {
    793   1.1   thorpej 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    794   1.1   thorpej 			if (m == NULL)
    795   1.1   thorpej 				goto dropit;
    796   1.1   thorpej 			if (len > (MHLEN - 2)) {
    797   1.1   thorpej 				MCLGET(m, M_DONTWAIT);
    798  1.41  riastrad 				if ((m->m_flags & M_EXT) == 0) {
    799  1.41  riastrad 					m_freem(m);
    800   1.1   thorpej 					goto dropit;
    801  1.41  riastrad 				}
    802   1.1   thorpej 			}
    803   1.1   thorpej 			m->m_data += 2;
    804   1.1   thorpej 			/*
    805   1.1   thorpej 			 * Note that we use a cluster for incoming frames,
    806   1.1   thorpej 			 * so the buffer is virtually contiguous.
    807   1.1   thorpej 			 */
    808  1.14  christos 			memcpy(mtod(m, void *), mtod(ds->ds_mbuf, void *),
    809   1.1   thorpej 			    len);
    810   1.1   thorpej 			SONIC_INIT_RXDESC(sc, i);
    811   1.1   thorpej 			bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    812   1.1   thorpej 			    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
    813   1.1   thorpej 		} else
    814   1.1   thorpej #endif /* ! __NO_STRICT_ALIGNMENT */
    815   1.1   thorpej 		/*
    816   1.1   thorpej 		 * If the packet is small enough to fit in a single
    817   1.1   thorpej 		 * header mbuf, allocate one and copy the data into
    818   1.1   thorpej 		 * it.  This greatly reduces memory consumption when
    819   1.1   thorpej 		 * we receive lots of small packets.
    820   1.1   thorpej 		 */
    821   1.1   thorpej 		if (sonic_copy_small != 0 && len <= (MHLEN - 2)) {
    822   1.1   thorpej 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    823   1.1   thorpej 			if (m == NULL)
    824   1.1   thorpej 				goto dropit;
    825   1.1   thorpej 			m->m_data += 2;
    826   1.1   thorpej 			/*
    827   1.1   thorpej 			 * Note that we use a cluster for incoming frames,
    828   1.1   thorpej 			 * so the buffer is virtually contiguous.
    829   1.1   thorpej 			 */
    830  1.14  christos 			memcpy(mtod(m, void *), mtod(ds->ds_mbuf, void *),
    831   1.1   thorpej 			    len);
    832   1.1   thorpej 			SONIC_INIT_RXDESC(sc, i);
    833   1.1   thorpej 			bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    834   1.1   thorpej 			    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
    835   1.1   thorpej 		} else {
    836   1.1   thorpej 			m = ds->ds_mbuf;
    837   1.1   thorpej 			if (sonic_add_rxbuf(sc, i) != 0) {
    838   1.1   thorpej  dropit:
    839  1.45   thorpej 				if_statinc(ifp, if_ierrors);
    840   1.1   thorpej 				SONIC_INIT_RXDESC(sc, i);
    841   1.1   thorpej 				bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    842   1.1   thorpej 				    ds->ds_dmamap->dm_mapsize,
    843   1.1   thorpej 				    BUS_DMASYNC_PREREAD);
    844   1.1   thorpej 				continue;
    845   1.1   thorpej 			}
    846   1.1   thorpej 		}
    847   1.1   thorpej 
    848  1.38     ozaki 		m_set_rcvif(m, ifp);
    849   1.1   thorpej 		m->m_pkthdr.len = m->m_len = len;
    850   1.1   thorpej 
    851   1.1   thorpej 		/* Pass it on. */
    852  1.37     ozaki 		if_percpuq_enqueue(ifp->if_percpuq, m);
    853  1.47       rin 
    854  1.47       rin 		count++;
    855   1.1   thorpej 	}
    856   1.1   thorpej 
    857   1.1   thorpej 	/* Update the receive pointer. */
    858   1.1   thorpej 	sc->sc_rxptr = i;
    859   1.1   thorpej 	CSR_WRITE(sc, SONIC_RWR, SONIC_CDRRADDR(sc, SONIC_PREVRX(i)));
    860  1.47       rin 
    861  1.47       rin 	if (count != 0)
    862  1.47       rin 		rnd_add_uint32(&sc->sc_rndsource, count);
    863   1.1   thorpej }
    864   1.1   thorpej 
    865   1.1   thorpej /*
    866   1.1   thorpej  * sonic_reset:
    867   1.1   thorpej  *
    868   1.1   thorpej  *	Perform a soft reset on the SONIC.
    869   1.1   thorpej  */
    870   1.1   thorpej void
    871   1.1   thorpej sonic_reset(struct sonic_softc *sc)
    872   1.1   thorpej {
    873   1.1   thorpej 
    874  1.16   tsutsui 	/* stop TX, RX and timer, and ensure RST is clear */
    875  1.16   tsutsui 	CSR_WRITE(sc, SONIC_CR, CR_STP | CR_RXDIS | CR_HTX);
    876  1.16   tsutsui 	delay(1000);
    877  1.16   tsutsui 
    878   1.1   thorpej 	CSR_WRITE(sc, SONIC_CR, CR_RST);
    879   1.1   thorpej 	delay(1000);
    880  1.16   tsutsui 
    881  1.16   tsutsui 	/* clear all interrupts */
    882  1.16   tsutsui 	CSR_WRITE(sc, SONIC_IMR, 0);
    883  1.16   tsutsui 	CSR_WRITE(sc, SONIC_ISR, IMR_ALL);
    884  1.16   tsutsui 
    885   1.1   thorpej 	CSR_WRITE(sc, SONIC_CR, 0);
    886   1.1   thorpej 	delay(1000);
    887   1.1   thorpej }
    888   1.1   thorpej 
    889   1.1   thorpej /*
    890   1.1   thorpej  * sonic_init:		[ifnet interface function]
    891   1.1   thorpej  *
    892   1.1   thorpej  *	Initialize the interface.  Must be called at splnet().
    893   1.1   thorpej  */
    894   1.1   thorpej int
    895   1.1   thorpej sonic_init(struct ifnet *ifp)
    896   1.1   thorpej {
    897   1.1   thorpej 	struct sonic_softc *sc = ifp->if_softc;
    898   1.1   thorpej 	struct sonic_descsoft *ds;
    899   1.1   thorpej 	int i, error = 0;
    900   1.1   thorpej 	uint16_t reg;
    901   1.1   thorpej 
    902   1.1   thorpej 	/*
    903   1.1   thorpej 	 * Cancel any pending I/O.
    904   1.1   thorpej 	 */
    905   1.1   thorpej 	sonic_stop(ifp, 0);
    906   1.1   thorpej 
    907   1.1   thorpej 	/*
    908   1.1   thorpej 	 * Reset the SONIC to a known state.
    909   1.1   thorpej 	 */
    910   1.1   thorpej 	sonic_reset(sc);
    911   1.1   thorpej 
    912   1.1   thorpej 	/*
    913   1.1   thorpej 	 * Bring the SONIC into reset state, and program the DCR.
    914   1.1   thorpej 	 *
    915   1.1   thorpej 	 * Note: We don't bother optimizing the transmit and receive
    916  1.17   tsutsui 	 * thresholds, here. TFT/RFT values should be set in MD attachments.
    917   1.1   thorpej 	 */
    918  1.17   tsutsui 	reg = sc->sc_dcr;
    919   1.1   thorpej 	if (sc->sc_32bit)
    920   1.1   thorpej 		reg |= DCR_DW;
    921   1.1   thorpej 	CSR_WRITE(sc, SONIC_CR, CR_RST);
    922   1.1   thorpej 	CSR_WRITE(sc, SONIC_DCR, reg);
    923   1.1   thorpej 	CSR_WRITE(sc, SONIC_DCR2, sc->sc_dcr2);
    924   1.1   thorpej 	CSR_WRITE(sc, SONIC_CR, 0);
    925   1.1   thorpej 
    926   1.1   thorpej 	/*
    927   1.1   thorpej 	 * Initialize the transmit descriptors.
    928   1.1   thorpej 	 */
    929   1.1   thorpej 	if (sc->sc_32bit) {
    930   1.1   thorpej 		for (i = 0; i < SONIC_NTXDESC; i++) {
    931   1.1   thorpej 			memset(&sc->sc_tda32[i], 0, sizeof(struct sonic_tda32));
    932   1.2   thorpej 			SONIC_CDTXSYNC32(sc, i,
    933  1.43   msaitoh 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    934   1.1   thorpej 		}
    935   1.1   thorpej 	} else {
    936   1.1   thorpej 		for (i = 0; i < SONIC_NTXDESC; i++) {
    937   1.1   thorpej 			memset(&sc->sc_tda16[i], 0, sizeof(struct sonic_tda16));
    938   1.2   thorpej 			SONIC_CDTXSYNC16(sc, i,
    939  1.43   msaitoh 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    940   1.1   thorpej 		}
    941   1.1   thorpej 	}
    942   1.1   thorpej 	sc->sc_txpending = 0;
    943   1.1   thorpej 	sc->sc_txdirty = 0;
    944   1.1   thorpej 	sc->sc_txlast = SONIC_NTXDESC - 1;
    945   1.1   thorpej 
    946   1.1   thorpej 	/*
    947   1.1   thorpej 	 * Initialize the receive descriptor ring.
    948   1.1   thorpej 	 */
    949   1.1   thorpej 	for (i = 0; i < SONIC_NRXDESC; i++) {
    950   1.1   thorpej 		ds = &sc->sc_rxsoft[i];
    951   1.1   thorpej 		if (ds->ds_mbuf == NULL) {
    952   1.1   thorpej 			if ((error = sonic_add_rxbuf(sc, i)) != 0) {
    953  1.25   tsutsui 				printf("%s: unable to allocate or map Rx "
    954   1.1   thorpej 				    "buffer %d, error = %d\n",
    955  1.25   tsutsui 				    device_xname(sc->sc_dev), i, error);
    956   1.1   thorpej 				/*
    957   1.1   thorpej 				 * XXX Should attempt to run with fewer receive
    958   1.1   thorpej 				 * XXX buffers instead of just failing.
    959   1.1   thorpej 				 */
    960   1.1   thorpej 				sonic_rxdrain(sc);
    961   1.1   thorpej 				goto out;
    962   1.1   thorpej 			}
    963   1.4   tsutsui 		} else
    964   1.4   tsutsui 			SONIC_INIT_RXDESC(sc, i);
    965   1.1   thorpej 	}
    966   1.1   thorpej 	sc->sc_rxptr = 0;
    967   1.1   thorpej 
    968   1.1   thorpej 	/* Give the transmit ring to the SONIC. */
    969   1.1   thorpej 	CSR_WRITE(sc, SONIC_UTDAR, (SONIC_CDTXADDR(sc, 0) >> 16) & 0xffff);
    970   1.1   thorpej 	CSR_WRITE(sc, SONIC_CTDAR, SONIC_CDTXADDR(sc, 0) & 0xffff);
    971   1.1   thorpej 
    972   1.1   thorpej 	/* Give the receive descriptor ring to the SONIC. */
    973   1.1   thorpej 	CSR_WRITE(sc, SONIC_URDAR, (SONIC_CDRXADDR(sc, 0) >> 16) & 0xffff);
    974   1.1   thorpej 	CSR_WRITE(sc, SONIC_CRDAR, SONIC_CDRXADDR(sc, 0) & 0xffff);
    975   1.1   thorpej 
    976   1.1   thorpej 	/* Give the receive buffer ring to the SONIC. */
    977   1.1   thorpej 	CSR_WRITE(sc, SONIC_URRAR, (SONIC_CDRRADDR(sc, 0) >> 16) & 0xffff);
    978   1.1   thorpej 	CSR_WRITE(sc, SONIC_RSAR, SONIC_CDRRADDR(sc, 0) & 0xffff);
    979   1.1   thorpej 	if (sc->sc_32bit)
    980   1.1   thorpej 		CSR_WRITE(sc, SONIC_REAR,
    981   1.1   thorpej 		    (SONIC_CDRRADDR(sc, SONIC_NRXDESC - 1) +
    982   1.1   thorpej 		    sizeof(struct sonic_rra32)) & 0xffff);
    983   1.1   thorpej 	else
    984   1.1   thorpej 		CSR_WRITE(sc, SONIC_REAR,
    985   1.1   thorpej 		    (SONIC_CDRRADDR(sc, SONIC_NRXDESC - 1) +
    986   1.1   thorpej 		    sizeof(struct sonic_rra16)) & 0xffff);
    987   1.1   thorpej 	CSR_WRITE(sc, SONIC_RRR, SONIC_CDRRADDR(sc, 0) & 0xffff);
    988   1.1   thorpej 	CSR_WRITE(sc, SONIC_RWR, SONIC_CDRRADDR(sc, SONIC_NRXDESC - 1));
    989   1.1   thorpej 
    990   1.1   thorpej 	/*
    991   1.1   thorpej 	 * Set the End-Of-Buffer counter such that only one packet
    992   1.1   thorpej 	 * will be placed into each buffer we provide.  Note we are
    993   1.1   thorpej 	 * following the recommendation of section 3.4.4 of the manual
    994   1.1   thorpej 	 * here, and have "lengthened" the receive buffers accordingly.
    995   1.1   thorpej 	 */
    996   1.1   thorpej 	if (sc->sc_32bit)
    997   1.1   thorpej 		CSR_WRITE(sc, SONIC_EOBC, (ETHER_MAX_LEN + 2) / 2);
    998   1.1   thorpej 	else
    999   1.1   thorpej 		CSR_WRITE(sc, SONIC_EOBC, (ETHER_MAX_LEN / 2));
   1000   1.1   thorpej 
   1001   1.1   thorpej 	/* Reset the receive sequence counter. */
   1002   1.1   thorpej 	CSR_WRITE(sc, SONIC_RSC, 0);
   1003   1.1   thorpej 
   1004   1.1   thorpej 	/* Clear the tally registers. */
   1005   1.1   thorpej 	CSR_WRITE(sc, SONIC_CRCETC, 0xffff);
   1006   1.1   thorpej 	CSR_WRITE(sc, SONIC_FAET, 0xffff);
   1007   1.1   thorpej 	CSR_WRITE(sc, SONIC_MPT, 0xffff);
   1008   1.1   thorpej 
   1009   1.1   thorpej 	/* Set the receive filter. */
   1010   1.1   thorpej 	sonic_set_filter(sc);
   1011   1.1   thorpej 
   1012   1.1   thorpej 	/*
   1013   1.1   thorpej 	 * Set the interrupt mask register.
   1014   1.1   thorpej 	 */
   1015   1.1   thorpej 	sc->sc_imr = IMR_RFO | IMR_RBA | IMR_RBE | IMR_RDE |
   1016   1.1   thorpej 	    IMR_TXER | IMR_PTX | IMR_PRX;
   1017   1.1   thorpej 	CSR_WRITE(sc, SONIC_IMR, sc->sc_imr);
   1018   1.1   thorpej 
   1019   1.1   thorpej 	/*
   1020   1.1   thorpej 	 * Start the receive process in motion.  Note, we don't
   1021   1.1   thorpej 	 * start the transmit process until we actually try to
   1022   1.1   thorpej 	 * transmit packets.
   1023   1.1   thorpej 	 */
   1024   1.1   thorpej 	CSR_WRITE(sc, SONIC_CR, CR_RXEN | CR_RRRA);
   1025   1.1   thorpej 
   1026   1.1   thorpej 	/*
   1027   1.1   thorpej 	 * ...all done!
   1028   1.1   thorpej 	 */
   1029   1.1   thorpej 	ifp->if_flags |= IFF_RUNNING;
   1030   1.1   thorpej 
   1031   1.1   thorpej  out:
   1032   1.1   thorpej 	if (error)
   1033  1.25   tsutsui 		printf("%s: interface not running\n", device_xname(sc->sc_dev));
   1034  1.25   tsutsui 	return error;
   1035   1.1   thorpej }
   1036   1.1   thorpej 
   1037   1.1   thorpej /*
   1038   1.1   thorpej  * sonic_rxdrain:
   1039   1.1   thorpej  *
   1040   1.1   thorpej  *	Drain the receive queue.
   1041   1.1   thorpej  */
   1042   1.1   thorpej void
   1043   1.1   thorpej sonic_rxdrain(struct sonic_softc *sc)
   1044   1.1   thorpej {
   1045   1.1   thorpej 	struct sonic_descsoft *ds;
   1046   1.1   thorpej 	int i;
   1047   1.1   thorpej 
   1048   1.1   thorpej 	for (i = 0; i < SONIC_NRXDESC; i++) {
   1049   1.1   thorpej 		ds = &sc->sc_rxsoft[i];
   1050   1.1   thorpej 		if (ds->ds_mbuf != NULL) {
   1051   1.1   thorpej 			bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1052   1.1   thorpej 			m_freem(ds->ds_mbuf);
   1053   1.1   thorpej 			ds->ds_mbuf = NULL;
   1054   1.1   thorpej 		}
   1055   1.1   thorpej 	}
   1056   1.1   thorpej }
   1057   1.1   thorpej 
   1058   1.1   thorpej /*
   1059   1.1   thorpej  * sonic_stop:		[ifnet interface function]
   1060   1.1   thorpej  *
   1061   1.1   thorpej  *	Stop transmission on the interface.
   1062   1.1   thorpej  */
   1063   1.1   thorpej void
   1064   1.1   thorpej sonic_stop(struct ifnet *ifp, int disable)
   1065   1.1   thorpej {
   1066   1.1   thorpej 	struct sonic_softc *sc = ifp->if_softc;
   1067   1.1   thorpej 	struct sonic_descsoft *ds;
   1068   1.1   thorpej 	int i;
   1069   1.1   thorpej 
   1070   1.1   thorpej 	/*
   1071   1.1   thorpej 	 * Disable interrupts.
   1072   1.1   thorpej 	 */
   1073   1.1   thorpej 	CSR_WRITE(sc, SONIC_IMR, 0);
   1074   1.1   thorpej 
   1075   1.1   thorpej 	/*
   1076   1.1   thorpej 	 * Stop the transmitter, receiver, and timer.
   1077   1.1   thorpej 	 */
   1078  1.43   msaitoh 	CSR_WRITE(sc, SONIC_CR, CR_HTX | CR_RXDIS | CR_STP);
   1079   1.1   thorpej 	for (i = 0; i < 1000; i++) {
   1080  1.43   msaitoh 		if ((CSR_READ(sc, SONIC_CR) & (CR_TXP | CR_RXEN | CR_ST)) == 0)
   1081   1.1   thorpej 			break;
   1082   1.1   thorpej 		delay(2);
   1083   1.1   thorpej 	}
   1084  1.43   msaitoh 	if ((CSR_READ(sc, SONIC_CR) & (CR_TXP | CR_RXEN | CR_ST)) != 0)
   1085  1.25   tsutsui 		printf("%s: SONIC failed to stop\n", device_xname(sc->sc_dev));
   1086   1.1   thorpej 
   1087   1.1   thorpej 	/*
   1088   1.1   thorpej 	 * Release any queued transmit buffers.
   1089   1.1   thorpej 	 */
   1090   1.1   thorpej 	for (i = 0; i < SONIC_NTXDESC; i++) {
   1091   1.1   thorpej 		ds = &sc->sc_txsoft[i];
   1092   1.1   thorpej 		if (ds->ds_mbuf != NULL) {
   1093   1.1   thorpej 			bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1094   1.1   thorpej 			m_freem(ds->ds_mbuf);
   1095   1.1   thorpej 			ds->ds_mbuf = NULL;
   1096   1.1   thorpej 		}
   1097   1.1   thorpej 	}
   1098   1.1   thorpej 
   1099   1.1   thorpej 	/*
   1100   1.1   thorpej 	 * Mark the interface down and cancel the watchdog timer.
   1101   1.1   thorpej 	 */
   1102  1.46   thorpej 	ifp->if_flags &= ~IFF_RUNNING;
   1103   1.1   thorpej 	ifp->if_timer = 0;
   1104  1.21    dyoung 
   1105  1.21    dyoung 	if (disable)
   1106  1.21    dyoung 		sonic_rxdrain(sc);
   1107   1.1   thorpej }
   1108   1.1   thorpej 
   1109   1.1   thorpej /*
   1110   1.1   thorpej  * sonic_add_rxbuf:
   1111   1.1   thorpej  *
   1112   1.1   thorpej  *	Add a receive buffer to the indicated descriptor.
   1113   1.1   thorpej  */
   1114   1.1   thorpej int
   1115   1.1   thorpej sonic_add_rxbuf(struct sonic_softc *sc, int idx)
   1116   1.1   thorpej {
   1117   1.1   thorpej 	struct sonic_descsoft *ds = &sc->sc_rxsoft[idx];
   1118   1.1   thorpej 	struct mbuf *m;
   1119   1.1   thorpej 	int error;
   1120   1.1   thorpej 
   1121   1.1   thorpej 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1122   1.1   thorpej 	if (m == NULL)
   1123  1.25   tsutsui 		return ENOBUFS;
   1124   1.1   thorpej 
   1125   1.1   thorpej 	MCLGET(m, M_DONTWAIT);
   1126   1.1   thorpej 	if ((m->m_flags & M_EXT) == 0) {
   1127   1.1   thorpej 		m_freem(m);
   1128  1.25   tsutsui 		return ENOBUFS;
   1129   1.1   thorpej 	}
   1130   1.1   thorpej 
   1131   1.1   thorpej 	if (ds->ds_mbuf != NULL)
   1132   1.1   thorpej 		bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1133   1.1   thorpej 
   1134   1.1   thorpej 	ds->ds_mbuf = m;
   1135   1.1   thorpej 
   1136   1.1   thorpej 	error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
   1137   1.3   thorpej 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   1138  1.43   msaitoh 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
   1139  1.12     perry 	if (error) {
   1140  1.25   tsutsui 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1141  1.25   tsutsui 		    device_xname(sc->sc_dev), idx, error);
   1142   1.1   thorpej 		panic("sonic_add_rxbuf");	/* XXX */
   1143   1.1   thorpej 	}
   1144   1.1   thorpej 
   1145   1.1   thorpej 	bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
   1146   1.1   thorpej 	    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1147   1.1   thorpej 
   1148   1.1   thorpej 	SONIC_INIT_RXDESC(sc, idx);
   1149   1.1   thorpej 
   1150  1.25   tsutsui 	return 0;
   1151   1.1   thorpej }
   1152   1.1   thorpej 
   1153   1.1   thorpej static void
   1154   1.1   thorpej sonic_set_camentry(struct sonic_softc *sc, int entry, const uint8_t *enaddr)
   1155   1.1   thorpej {
   1156   1.1   thorpej 
   1157   1.1   thorpej 	if (sc->sc_32bit) {
   1158   1.1   thorpej 		struct sonic_cda32 *cda = &sc->sc_cda32[entry];
   1159   1.1   thorpej 
   1160   1.1   thorpej 		cda->cda_entry = htosonic32(sc, entry);
   1161   1.1   thorpej 		cda->cda_addr0 = htosonic32(sc, enaddr[0] | (enaddr[1] << 8));
   1162   1.1   thorpej 		cda->cda_addr1 = htosonic32(sc, enaddr[2] | (enaddr[3] << 8));
   1163   1.1   thorpej 		cda->cda_addr2 = htosonic32(sc, enaddr[4] | (enaddr[5] << 8));
   1164   1.1   thorpej 	} else {
   1165   1.1   thorpej 		struct sonic_cda16 *cda = &sc->sc_cda16[entry];
   1166   1.1   thorpej 
   1167   1.1   thorpej 		cda->cda_entry = htosonic16(sc, entry);
   1168   1.1   thorpej 		cda->cda_addr0 = htosonic16(sc, enaddr[0] | (enaddr[1] << 8));
   1169   1.1   thorpej 		cda->cda_addr1 = htosonic16(sc, enaddr[2] | (enaddr[3] << 8));
   1170   1.1   thorpej 		cda->cda_addr2 = htosonic16(sc, enaddr[4] | (enaddr[5] << 8));
   1171   1.1   thorpej 	}
   1172   1.1   thorpej }
   1173   1.1   thorpej 
   1174   1.1   thorpej /*
   1175   1.1   thorpej  * sonic_set_filter:
   1176   1.1   thorpej  *
   1177   1.1   thorpej  *	Set the SONIC receive filter.
   1178   1.1   thorpej  */
   1179   1.1   thorpej void
   1180   1.1   thorpej sonic_set_filter(struct sonic_softc *sc)
   1181   1.1   thorpej {
   1182   1.1   thorpej 	struct ethercom *ec = &sc->sc_ethercom;
   1183   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1184   1.1   thorpej 	struct ether_multi *enm;
   1185   1.1   thorpej 	struct ether_multistep step;
   1186   1.1   thorpej 	int i, entry = 0;
   1187   1.1   thorpej 	uint16_t camvalid = 0;
   1188   1.1   thorpej 	uint16_t rcr = 0;
   1189   1.1   thorpej 
   1190   1.1   thorpej 	if (ifp->if_flags & IFF_BROADCAST)
   1191   1.1   thorpej 		rcr |= RCR_BRD;
   1192   1.1   thorpej 
   1193   1.1   thorpej 	if (ifp->if_flags & IFF_PROMISC) {
   1194   1.1   thorpej 		rcr |= RCR_PRO;
   1195   1.1   thorpej 		goto allmulti;
   1196   1.1   thorpej 	}
   1197   1.1   thorpej 
   1198   1.1   thorpej 	/* Put our station address in the first CAM slot. */
   1199  1.18    dyoung 	sonic_set_camentry(sc, entry, CLLADDR(ifp->if_sadl));
   1200   1.1   thorpej 	camvalid |= (1U << entry);
   1201   1.1   thorpej 	entry++;
   1202   1.1   thorpej 
   1203   1.1   thorpej 	/* Add the multicast addresses to the CAM. */
   1204  1.44   msaitoh 	ETHER_LOCK(ec);
   1205   1.1   thorpej 	ETHER_FIRST_MULTI(step, ec, enm);
   1206   1.1   thorpej 	while (enm != NULL) {
   1207   1.1   thorpej 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1208   1.1   thorpej 			/*
   1209   1.1   thorpej 			 * We must listen to a range of multicast addresses.
   1210   1.1   thorpej 			 * The only way to do this on the SONIC is to enable
   1211   1.1   thorpej 			 * reception of all multicast packets.
   1212   1.1   thorpej 			 */
   1213  1.44   msaitoh 			ETHER_UNLOCK(ec);
   1214   1.1   thorpej 			goto allmulti;
   1215   1.1   thorpej 		}
   1216   1.1   thorpej 
   1217  1.24   tsutsui 		if (entry == SONIC_NCAMENT) {
   1218   1.1   thorpej 			/*
   1219   1.1   thorpej 			 * Out of CAM slots.  Have to enable reception
   1220   1.1   thorpej 			 * of all multicast addresses.
   1221   1.1   thorpej 			 */
   1222  1.44   msaitoh 			ETHER_UNLOCK(ec);
   1223   1.1   thorpej 			goto allmulti;
   1224   1.1   thorpej 		}
   1225   1.1   thorpej 
   1226   1.1   thorpej 		sonic_set_camentry(sc, entry, enm->enm_addrlo);
   1227   1.1   thorpej 		camvalid |= (1U << entry);
   1228   1.1   thorpej 		entry++;
   1229   1.1   thorpej 
   1230   1.1   thorpej 		ETHER_NEXT_MULTI(step, enm);
   1231   1.1   thorpej 	}
   1232  1.44   msaitoh 	ETHER_UNLOCK(ec);
   1233   1.1   thorpej 
   1234   1.1   thorpej 	ifp->if_flags &= ~IFF_ALLMULTI;
   1235   1.1   thorpej 	goto setit;
   1236   1.1   thorpej 
   1237   1.1   thorpej  allmulti:
   1238   1.1   thorpej 	/* Use only the first CAM slot (station address). */
   1239   1.1   thorpej 	camvalid = 0x0001;
   1240   1.1   thorpej 	entry = 1;
   1241   1.1   thorpej 	rcr |= RCR_AMC;
   1242   1.1   thorpej 
   1243   1.1   thorpej  setit:
   1244  1.24   tsutsui 	/* set mask for the CAM Enable register */
   1245  1.24   tsutsui 	if (sc->sc_32bit) {
   1246  1.24   tsutsui 		if (entry == SONIC_NCAMENT)
   1247  1.24   tsutsui 			sc->sc_cdaenable32 = htosonic32(sc, camvalid);
   1248  1.24   tsutsui 		else
   1249  1.24   tsutsui 			sc->sc_cda32[entry].cda_entry =
   1250  1.24   tsutsui 			    htosonic32(sc, camvalid);
   1251  1.24   tsutsui 	} else {
   1252  1.24   tsutsui 		if (entry == SONIC_NCAMENT)
   1253  1.24   tsutsui 			sc->sc_cdaenable16 = htosonic16(sc, camvalid);
   1254  1.24   tsutsui 		else
   1255  1.24   tsutsui 			sc->sc_cda16[entry].cda_entry =
   1256  1.24   tsutsui 			    htosonic16(sc, camvalid);
   1257  1.24   tsutsui 	}
   1258  1.24   tsutsui 
   1259   1.1   thorpej 	/* Load the CAM. */
   1260   1.1   thorpej 	SONIC_CDCAMSYNC(sc, BUS_DMASYNC_PREWRITE);
   1261   1.1   thorpej 	CSR_WRITE(sc, SONIC_CDP, SONIC_CDCAMADDR(sc) & 0xffff);
   1262   1.1   thorpej 	CSR_WRITE(sc, SONIC_CDC, entry);
   1263   1.1   thorpej 	CSR_WRITE(sc, SONIC_CR, CR_LCAM);
   1264   1.1   thorpej 	for (i = 0; i < 10000; i++) {
   1265   1.1   thorpej 		if ((CSR_READ(sc, SONIC_CR) & CR_LCAM) == 0)
   1266   1.1   thorpej 			break;
   1267   1.1   thorpej 		delay(2);
   1268   1.1   thorpej 	}
   1269   1.1   thorpej 	if (CSR_READ(sc, SONIC_CR) & CR_LCAM)
   1270  1.25   tsutsui 		printf("%s: CAM load failed\n", device_xname(sc->sc_dev));
   1271   1.1   thorpej 	SONIC_CDCAMSYNC(sc, BUS_DMASYNC_POSTWRITE);
   1272   1.1   thorpej 
   1273   1.1   thorpej 	/* Set the receive control register. */
   1274   1.1   thorpej 	CSR_WRITE(sc, SONIC_RCR, rcr);
   1275   1.1   thorpej }
   1276