Home | History | Annotate | Line # | Download | only in ic
hd64570.c revision 1.5
      1  1.5       erh /*	$NetBSD: hd64570.c,v 1.5 1999/03/16 21:29:23 erh Exp $	*/
      2  1.1  explorer 
      3  1.1  explorer /*
      4  1.1  explorer  * Copyright (c) 1998 Vixie Enterprises
      5  1.1  explorer  * All rights reserved.
      6  1.1  explorer  *
      7  1.1  explorer  * Redistribution and use in source and binary forms, with or without
      8  1.1  explorer  * modification, are permitted provided that the following conditions
      9  1.1  explorer  * are met:
     10  1.1  explorer  *
     11  1.1  explorer  * 1. Redistributions of source code must retain the above copyright
     12  1.1  explorer  *    notice, this list of conditions and the following disclaimer.
     13  1.1  explorer  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  explorer  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  explorer  *    documentation and/or other materials provided with the distribution.
     16  1.1  explorer  * 3. Neither the name of Vixie Enterprises nor the names
     17  1.1  explorer  *    of its contributors may be used to endorse or promote products derived
     18  1.1  explorer  *    from this software without specific prior written permission.
     19  1.1  explorer  *
     20  1.1  explorer  * THIS SOFTWARE IS PROVIDED BY VIXIE ENTERPRISES AND
     21  1.1  explorer  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     22  1.1  explorer  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     23  1.1  explorer  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24  1.1  explorer  * DISCLAIMED.  IN NO EVENT SHALL VIXIE ENTERPRISES OR
     25  1.1  explorer  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  1.1  explorer  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     27  1.1  explorer  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     28  1.1  explorer  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     29  1.1  explorer  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  1.1  explorer  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     31  1.1  explorer  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  explorer  * SUCH DAMAGE.
     33  1.1  explorer  *
     34  1.1  explorer  * This software has been written for Vixie Enterprises by Michael Graff
     35  1.1  explorer  * <explorer (at) flame.org>.  To learn more about Vixie Enterprises, see
     36  1.1  explorer  * ``http://www.vix.com''.
     37  1.1  explorer  */
     38  1.1  explorer 
     39  1.1  explorer /*
     40  1.1  explorer  * TODO:
     41  1.1  explorer  *
     42  1.1  explorer  *	o  teach the receive logic about errors, and about long frames that
     43  1.1  explorer  *         span more than one input buffer.  (Right now, receive/transmit is
     44  1.1  explorer  *	   limited to one descriptor's buffer space, which is MTU + 4 bytes.
     45  1.1  explorer  *	   This is currently 1504, which is large enough to hold the HDLC
     46  1.1  explorer  *	   header and the packet itself.  Packets which are too long are
     47  1.1  explorer  *	   silently dropped on transmit and silently dropped on receive.
     48  1.1  explorer  *	o  write code to handle the msci interrupts, needed only for CD
     49  1.1  explorer  *	   and CTS changes.
     50  1.1  explorer  *	o  consider switching back to a "queue tx with DMA active" model which
     51  1.1  explorer  *	   should help sustain outgoing traffic
     52  1.1  explorer  *	o  through clever use of bus_dma*() functions, it should be possible
     53  1.1  explorer  *	   to map the mbuf's data area directly into a descriptor transmit
     54  1.1  explorer  *	   buffer, removing the need to allocate extra memory.  If, however,
     55  1.1  explorer  *	   we run out of descriptors for this, we will need to then allocate
     56  1.1  explorer  *	   one large mbuf, copy the fragmented chain into it, and put it onto
     57  1.1  explorer  *	   a single descriptor.
     58  1.1  explorer  *	o  use bus_dmamap_sync() with the right offset and lengths, rather
     59  1.1  explorer  *	   than cheating and always sync'ing the whole region.
     60  1.1  explorer  */
     61  1.1  explorer 
     62  1.1  explorer #include "bpfilter.h"
     63  1.1  explorer 
     64  1.1  explorer #include <sys/param.h>
     65  1.1  explorer #include <sys/systm.h>
     66  1.1  explorer #include <sys/device.h>
     67  1.1  explorer #include <sys/mbuf.h>
     68  1.1  explorer #include <sys/socket.h>
     69  1.1  explorer #include <sys/sockio.h>
     70  1.1  explorer #include <sys/kernel.h>
     71  1.1  explorer 
     72  1.1  explorer #include <net/if.h>
     73  1.1  explorer #include <net/if_types.h>
     74  1.1  explorer #include <net/netisr.h>
     75  1.1  explorer 
     76  1.1  explorer #include <netinet/in.h>
     77  1.1  explorer #include <netinet/in_systm.h>
     78  1.1  explorer #include <netinet/in_var.h>
     79  1.1  explorer #include <netinet/ip.h>
     80  1.1  explorer 
     81  1.1  explorer #if NBPFILTER > 0
     82  1.1  explorer #include <net/bpf.h>
     83  1.1  explorer #endif
     84  1.1  explorer 
     85  1.1  explorer #include <machine/cpu.h>
     86  1.1  explorer #include <machine/bus.h>
     87  1.1  explorer #include <machine/intr.h>
     88  1.1  explorer 
     89  1.1  explorer #include <dev/pci/pcivar.h>
     90  1.1  explorer #include <dev/pci/pcireg.h>
     91  1.1  explorer #include <dev/pci/pcidevs.h>
     92  1.1  explorer 
     93  1.1  explorer #include <dev/ic/hd64570reg.h>
     94  1.1  explorer #include <dev/ic/hd64570var.h>
     95  1.1  explorer 
     96  1.1  explorer #define SCA_DEBUG_RX		0x0001
     97  1.1  explorer #define SCA_DEBUG_TX		0x0002
     98  1.1  explorer #define SCA_DEBUG_CISCO		0x0004
     99  1.1  explorer #define SCA_DEBUG_DMA		0x0008
    100  1.1  explorer #define SCA_DEBUG_RXPKT		0x0010
    101  1.1  explorer #define SCA_DEBUG_TXPKT		0x0020
    102  1.1  explorer #define SCA_DEBUG_INTR		0x0040
    103  1.1  explorer 
    104  1.1  explorer #if 0
    105  1.1  explorer #define SCA_DEBUG_LEVEL	( SCA_DEBUG_TX )
    106  1.1  explorer #else
    107  1.1  explorer #define SCA_DEBUG_LEVEL 0
    108  1.1  explorer #endif
    109  1.1  explorer 
    110  1.1  explorer u_int32_t sca_debug = SCA_DEBUG_LEVEL;
    111  1.1  explorer 
    112  1.1  explorer #if SCA_DEBUG_LEVEL > 0
    113  1.1  explorer #define SCA_DPRINTF(l, x) do { \
    114  1.1  explorer 	if ((l) & sca_debug) \
    115  1.1  explorer 		printf x;\
    116  1.1  explorer 	} while (0)
    117  1.1  explorer #else
    118  1.1  explorer #define SCA_DPRINTF(l, x)
    119  1.1  explorer #endif
    120  1.1  explorer 
    121  1.1  explorer #define SCA_MTU		1500	/* hard coded */
    122  1.1  explorer 
    123  1.1  explorer /*
    124  1.1  explorer  * buffers per tx and rx channels, per port, and the size of each.
    125  1.1  explorer  * Don't use these constants directly, as they are really only hints.
    126  1.1  explorer  * Use the calculated values stored in struct sca_softc instead.
    127  1.1  explorer  *
    128  1.1  explorer  * Each must be at least 2, receive would be better at around 20 or so.
    129  1.1  explorer  *
    130  1.1  explorer  * XXX Due to a damned near impossible to track down bug, transmit buffers
    131  1.1  explorer  * MUST be 2, no more, no less.
    132  1.1  explorer  */
    133  1.1  explorer #ifndef SCA_NtxBUFS
    134  1.1  explorer #define SCA_NtxBUFS	2
    135  1.1  explorer #endif
    136  1.1  explorer #ifndef SCA_NrxBUFS
    137  1.1  explorer #define SCA_NrxBUFS	20
    138  1.1  explorer #endif
    139  1.1  explorer #ifndef SCA_BSIZE
    140  1.1  explorer #define SCA_BSIZE	(SCA_MTU + 4)	/* room for HDLC as well */
    141  1.1  explorer #endif
    142  1.1  explorer 
    143  1.1  explorer #if 0
    144  1.1  explorer #define SCA_USE_FASTQ		/* use a split queue, one for fast traffic */
    145  1.1  explorer #endif
    146  1.1  explorer 
    147  1.1  explorer static inline void sca_write_1(struct sca_softc *, u_int, u_int8_t);
    148  1.1  explorer static inline void sca_write_2(struct sca_softc *, u_int, u_int16_t);
    149  1.1  explorer static inline u_int8_t sca_read_1(struct sca_softc *, u_int);
    150  1.1  explorer static inline u_int16_t sca_read_2(struct sca_softc *, u_int);
    151  1.1  explorer 
    152  1.1  explorer static inline void msci_write_1(sca_port_t *, u_int, u_int8_t);
    153  1.1  explorer static inline u_int8_t msci_read_1(sca_port_t *, u_int);
    154  1.1  explorer 
    155  1.1  explorer static inline void dmac_write_1(sca_port_t *, u_int, u_int8_t);
    156  1.1  explorer static inline void dmac_write_2(sca_port_t *, u_int, u_int16_t);
    157  1.1  explorer static inline u_int8_t dmac_read_1(sca_port_t *, u_int);
    158  1.1  explorer static inline u_int16_t dmac_read_2(sca_port_t *, u_int);
    159  1.1  explorer 
    160  1.1  explorer static	int sca_alloc_dma(struct sca_softc *);
    161  1.1  explorer static	void sca_setup_dma_memory(struct sca_softc *);
    162  1.1  explorer static	void sca_msci_init(struct sca_softc *, sca_port_t *);
    163  1.1  explorer static	void sca_dmac_init(struct sca_softc *, sca_port_t *);
    164  1.1  explorer static void sca_dmac_rxinit(sca_port_t *);
    165  1.1  explorer 
    166  1.1  explorer static	int sca_dmac_intr(sca_port_t *, u_int8_t);
    167  1.1  explorer static	int sca_msci_intr(struct sca_softc *, u_int8_t);
    168  1.1  explorer 
    169  1.1  explorer static	void sca_get_packets(sca_port_t *);
    170  1.1  explorer static	void sca_frame_process(sca_port_t *, sca_desc_t *, u_int8_t *);
    171  1.1  explorer static	int sca_frame_avail(sca_port_t *, int *);
    172  1.1  explorer static	void sca_frame_skip(sca_port_t *, int);
    173  1.1  explorer 
    174  1.1  explorer static	void sca_port_starttx(sca_port_t *);
    175  1.1  explorer 
    176  1.1  explorer static	void sca_port_up(sca_port_t *);
    177  1.1  explorer static	void sca_port_down(sca_port_t *);
    178  1.1  explorer 
    179  1.1  explorer static	int sca_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
    180  1.1  explorer 			    struct rtentry *));
    181  1.1  explorer static	int sca_ioctl __P((struct ifnet *, u_long, caddr_t));
    182  1.1  explorer static	void sca_start __P((struct ifnet *));
    183  1.1  explorer static	void sca_watchdog __P((struct ifnet *));
    184  1.1  explorer 
    185  1.1  explorer static struct mbuf *sca_mbuf_alloc(caddr_t, u_int);
    186  1.1  explorer 
    187  1.1  explorer #if SCA_DEBUG_LEVEL > 0
    188  1.1  explorer static	void sca_frame_print(sca_port_t *, sca_desc_t *, u_int8_t *);
    189  1.1  explorer #endif
    190  1.1  explorer 
    191  1.1  explorer static inline void
    192  1.1  explorer sca_write_1(struct sca_softc *sc, u_int reg, u_int8_t val)
    193  1.1  explorer {
    194  1.1  explorer 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SCADDR(reg), val);
    195  1.1  explorer }
    196  1.1  explorer 
    197  1.1  explorer static inline void
    198  1.1  explorer sca_write_2(struct sca_softc *sc, u_int reg, u_int16_t val)
    199  1.1  explorer {
    200  1.1  explorer 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCADDR(reg), val);
    201  1.1  explorer }
    202  1.1  explorer 
    203  1.1  explorer static inline u_int8_t
    204  1.1  explorer sca_read_1(struct sca_softc *sc, u_int reg)
    205  1.1  explorer {
    206  1.1  explorer 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, SCADDR(reg));
    207  1.1  explorer }
    208  1.1  explorer 
    209  1.1  explorer static inline u_int16_t
    210  1.1  explorer sca_read_2(struct sca_softc *sc, u_int reg)
    211  1.1  explorer {
    212  1.1  explorer 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCADDR(reg));
    213  1.1  explorer }
    214  1.1  explorer 
    215  1.1  explorer static inline void
    216  1.1  explorer msci_write_1(sca_port_t *scp, u_int reg, u_int8_t val)
    217  1.1  explorer {
    218  1.1  explorer 	sca_write_1(scp->sca, scp->msci_off + reg, val);
    219  1.1  explorer }
    220  1.1  explorer 
    221  1.1  explorer static inline u_int8_t
    222  1.1  explorer msci_read_1(sca_port_t *scp, u_int reg)
    223  1.1  explorer {
    224  1.1  explorer 	return sca_read_1(scp->sca, scp->msci_off + reg);
    225  1.1  explorer }
    226  1.1  explorer 
    227  1.1  explorer static inline void
    228  1.1  explorer dmac_write_1(sca_port_t *scp, u_int reg, u_int8_t val)
    229  1.1  explorer {
    230  1.1  explorer 	sca_write_1(scp->sca, scp->dmac_off + reg, val);
    231  1.1  explorer }
    232  1.1  explorer 
    233  1.1  explorer static inline void
    234  1.1  explorer dmac_write_2(sca_port_t *scp, u_int reg, u_int16_t val)
    235  1.1  explorer {
    236  1.1  explorer 	sca_write_2(scp->sca, scp->dmac_off + reg, val);
    237  1.1  explorer }
    238  1.1  explorer 
    239  1.1  explorer static inline u_int8_t
    240  1.1  explorer dmac_read_1(sca_port_t *scp, u_int reg)
    241  1.1  explorer {
    242  1.1  explorer 	return sca_read_1(scp->sca, scp->dmac_off + reg);
    243  1.1  explorer }
    244  1.1  explorer 
    245  1.1  explorer static inline u_int16_t
    246  1.1  explorer dmac_read_2(sca_port_t *scp, u_int reg)
    247  1.1  explorer {
    248  1.1  explorer 	return sca_read_2(scp->sca, scp->dmac_off + reg);
    249  1.1  explorer }
    250  1.1  explorer 
    251  1.1  explorer int
    252  1.1  explorer sca_init(struct sca_softc *sc, u_int nports)
    253  1.1  explorer {
    254  1.1  explorer 	/*
    255  1.1  explorer 	 * Do a little sanity check:  check number of ports.
    256  1.1  explorer 	 */
    257  1.1  explorer 	if (nports < 1 || nports > 2)
    258  1.1  explorer 		return 1;
    259  1.1  explorer 
    260  1.1  explorer 	/*
    261  1.1  explorer 	 * remember the details
    262  1.1  explorer 	 */
    263  1.1  explorer 	sc->sc_numports = nports;
    264  1.1  explorer 
    265  1.1  explorer 	/*
    266  1.1  explorer 	 * allocate the memory and chop it into bits.
    267  1.1  explorer 	 */
    268  1.1  explorer 	if (sca_alloc_dma(sc) != 0)
    269  1.1  explorer 		return 1;
    270  1.1  explorer 	sca_setup_dma_memory(sc);
    271  1.1  explorer 
    272  1.1  explorer 	/*
    273  1.1  explorer 	 * disable DMA and MSCI interrupts
    274  1.1  explorer 	 */
    275  1.1  explorer 	sca_write_1(sc, SCA_DMER, 0);
    276  1.1  explorer 	sca_write_1(sc, SCA_IER0, 0);
    277  1.1  explorer 	sca_write_1(sc, SCA_IER1, 0);
    278  1.1  explorer 	sca_write_1(sc, SCA_IER2, 0);
    279  1.1  explorer 
    280  1.1  explorer 	/*
    281  1.1  explorer 	 * configure interrupt system
    282  1.1  explorer 	 */
    283  1.1  explorer 	sca_write_1(sc, SCA_ITCR, 0);	/* use ivr, no int ack */
    284  1.1  explorer 	sca_write_1(sc, SCA_IVR, 0x40);
    285  1.1  explorer 	sca_write_1(sc, SCA_IMVR, 0x40);
    286  1.1  explorer 
    287  1.1  explorer 	/*
    288  1.1  explorer 	 * set wait control register to zero wait states
    289  1.1  explorer 	 */
    290  1.1  explorer 	sca_write_1(sc, SCA_PABR0, 0);
    291  1.1  explorer 	sca_write_1(sc, SCA_PABR1, 0);
    292  1.1  explorer 	sca_write_1(sc, SCA_WCRL, 0);
    293  1.1  explorer 	sca_write_1(sc, SCA_WCRM, 0);
    294  1.1  explorer 	sca_write_1(sc, SCA_WCRH, 0);
    295  1.1  explorer 
    296  1.1  explorer 	/*
    297  1.1  explorer 	 * disable DMA and reset status
    298  1.1  explorer 	 */
    299  1.1  explorer 	sca_write_1(sc, SCA_PCR, SCA_PCR_PR2);
    300  1.1  explorer 
    301  1.1  explorer 	/*
    302  1.1  explorer 	 * disable transmit DMA for all channels
    303  1.1  explorer 	 */
    304  1.1  explorer 	sca_write_1(sc, SCA_DSR0 + SCA_DMAC_OFF_0, 0);
    305  1.1  explorer 	sca_write_1(sc, SCA_DCR0 + SCA_DMAC_OFF_0, SCA_DCR_ABRT);
    306  1.1  explorer 	sca_write_1(sc, SCA_DSR1 + SCA_DMAC_OFF_0, 0);
    307  1.1  explorer 	sca_write_1(sc, SCA_DCR1 + SCA_DMAC_OFF_0, SCA_DCR_ABRT);
    308  1.1  explorer 	sca_write_1(sc, SCA_DSR0 + SCA_DMAC_OFF_1, 0);
    309  1.1  explorer 	sca_write_1(sc, SCA_DCR0 + SCA_DMAC_OFF_1, SCA_DCR_ABRT);
    310  1.1  explorer 	sca_write_1(sc, SCA_DSR1 + SCA_DMAC_OFF_1, 0);
    311  1.1  explorer 	sca_write_1(sc, SCA_DCR1 + SCA_DMAC_OFF_1, SCA_DCR_ABRT);
    312  1.1  explorer 
    313  1.1  explorer 	/*
    314  1.1  explorer 	 * enable DMA based on channel enable flags for each channel
    315  1.1  explorer 	 */
    316  1.1  explorer 	sca_write_1(sc, SCA_DMER, SCA_DMER_EN);
    317  1.1  explorer 
    318  1.1  explorer 	/*
    319  1.1  explorer 	 * Should check to see if the chip is responding, but for now
    320  1.1  explorer 	 * assume it is.
    321  1.1  explorer 	 */
    322  1.1  explorer 	return 0;
    323  1.1  explorer }
    324  1.1  explorer 
    325  1.1  explorer /*
    326  1.1  explorer  * initialize the port and attach it to the networking layer
    327  1.1  explorer  */
    328  1.1  explorer void
    329  1.1  explorer sca_port_attach(struct sca_softc *sc, u_int port)
    330  1.1  explorer {
    331  1.1  explorer 	sca_port_t *scp = &sc->sc_ports[port];
    332  1.1  explorer 	struct ifnet *ifp;
    333  1.1  explorer 	static u_int ntwo_unit = 0;
    334  1.1  explorer 
    335  1.1  explorer 	scp->sca = sc;  /* point back to the parent */
    336  1.1  explorer 
    337  1.1  explorer 	scp->sp_port = port;
    338  1.1  explorer 
    339  1.1  explorer 	if (port == 0) {
    340  1.1  explorer 		scp->msci_off = SCA_MSCI_OFF_0;
    341  1.1  explorer 		scp->dmac_off = SCA_DMAC_OFF_0;
    342  1.4       tls 		if(sc->parent != NULL)
    343  1.4       tls 			ntwo_unit=sc->parent->dv_unit * 2 + 0;
    344  1.4       tls 		else
    345  1.4       tls 			ntwo_unit = 0;	/* XXX */
    346  1.1  explorer 	} else {
    347  1.1  explorer 		scp->msci_off = SCA_MSCI_OFF_1;
    348  1.1  explorer 		scp->dmac_off = SCA_DMAC_OFF_1;
    349  1.4       tls 		if(sc->parent != NULL)
    350  1.4       tls 			ntwo_unit=sc->parent->dv_unit * 2 + 1;
    351  1.4       tls 		else
    352  1.4       tls 			ntwo_unit = 1;	/* XXX */
    353  1.1  explorer 	}
    354  1.1  explorer 
    355  1.1  explorer 	sca_msci_init(sc, scp);
    356  1.1  explorer 	sca_dmac_init(sc, scp);
    357  1.1  explorer 
    358  1.1  explorer 	/*
    359  1.1  explorer 	 * attach to the network layer
    360  1.1  explorer 	 */
    361  1.1  explorer 	ifp = &scp->sp_if;
    362  1.1  explorer 	sprintf(ifp->if_xname, "ntwo%d", ntwo_unit);
    363  1.1  explorer 	ifp->if_softc = scp;
    364  1.1  explorer 	ifp->if_mtu = SCA_MTU;
    365  1.1  explorer 	ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
    366  1.1  explorer 	ifp->if_type = IFT_OTHER;  /* Should be HDLC, but... */
    367  1.1  explorer 	ifp->if_hdrlen = HDLC_HDRLEN;
    368  1.1  explorer 	ifp->if_ioctl = sca_ioctl;
    369  1.1  explorer 	ifp->if_output = sca_output;
    370  1.1  explorer 	ifp->if_watchdog = sca_watchdog;
    371  1.1  explorer 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
    372  1.1  explorer 	scp->linkq.ifq_maxlen = 5; /* if we exceed this we are hosed already */
    373  1.1  explorer #ifdef SCA_USE_FASTQ
    374  1.1  explorer 	scp->fastq.ifq_maxlen = IFQ_MAXLEN;
    375  1.1  explorer #endif
    376  1.1  explorer 	if_attach(ifp);
    377  1.1  explorer 
    378  1.1  explorer #if NBPFILTER > 0
    379  1.1  explorer 	bpfattach(&scp->sp_bpf, ifp, DLT_HDLC, HDLC_HDRLEN);
    380  1.1  explorer #endif
    381  1.1  explorer 
    382  1.1  explorer 	if (sc->parent == NULL)
    383  1.1  explorer 		printf("%s: port %d\n", ifp->if_xname, port);
    384  1.1  explorer 	else
    385  1.1  explorer 		printf("%s at %s port %d\n",
    386  1.1  explorer 		       ifp->if_xname, sc->parent->dv_xname, port);
    387  1.1  explorer 
    388  1.1  explorer 	/*
    389  1.1  explorer 	 * reset the last seen times on the cisco keepalive protocol
    390  1.1  explorer 	 */
    391  1.1  explorer 	scp->cka_lasttx = time.tv_usec;
    392  1.1  explorer 	scp->cka_lastrx = 0;
    393  1.1  explorer }
    394  1.1  explorer 
    395  1.1  explorer /*
    396  1.1  explorer  * initialize the port's MSCI
    397  1.1  explorer  */
    398  1.1  explorer static void
    399  1.1  explorer sca_msci_init(struct sca_softc *sc, sca_port_t *scp)
    400  1.1  explorer {
    401  1.1  explorer 	msci_write_1(scp, SCA_CMD0, SCA_CMD_RESET);
    402  1.1  explorer 	msci_write_1(scp, SCA_MD00,
    403  1.1  explorer 		     (  SCA_MD0_CRC_1
    404  1.1  explorer 		      | SCA_MD0_CRC_CCITT
    405  1.1  explorer 		      | SCA_MD0_CRC_ENABLE
    406  1.1  explorer 		      | SCA_MD0_MODE_HDLC));
    407  1.1  explorer 	msci_write_1(scp, SCA_MD10, SCA_MD1_NOADDRCHK);
    408  1.1  explorer 	msci_write_1(scp, SCA_MD20,
    409  1.1  explorer 		     (SCA_MD2_DUPLEX | SCA_MD2_NRZ));
    410  1.1  explorer 
    411  1.1  explorer 	/*
    412  1.1  explorer 	 * reset the port (and lower RTS)
    413  1.1  explorer 	 */
    414  1.1  explorer 	msci_write_1(scp, SCA_CMD0, SCA_CMD_RXRESET);
    415  1.1  explorer 	msci_write_1(scp, SCA_CTL0,
    416  1.1  explorer 		     (SCA_CTL_IDLPAT | SCA_CTL_UDRNC | SCA_CTL_RTS));
    417  1.1  explorer 	msci_write_1(scp, SCA_CMD0, SCA_CMD_TXRESET);
    418  1.1  explorer 
    419  1.1  explorer 	/*
    420  1.1  explorer 	 * select the RX clock as the TX clock, and set for external
    421  1.1  explorer 	 * clock source.
    422  1.1  explorer 	 */
    423  1.1  explorer 	msci_write_1(scp, SCA_RXS0, 0);
    424  1.1  explorer 	msci_write_1(scp, SCA_TXS0, 0);
    425  1.1  explorer 
    426  1.1  explorer 	/*
    427  1.1  explorer 	 * XXX don't pay attention to CTS or CD changes right now.  I can't
    428  1.1  explorer 	 * simulate one, and the transmitter will try to transmit even if
    429  1.1  explorer 	 * CD isn't there anyway, so nothing bad SHOULD happen.
    430  1.1  explorer 	 */
    431  1.1  explorer 	msci_write_1(scp, SCA_IE00, 0);
    432  1.1  explorer 	msci_write_1(scp, SCA_IE10, 0); /* 0x0c == CD and CTS changes only */
    433  1.1  explorer 	msci_write_1(scp, SCA_IE20, 0);
    434  1.1  explorer 	msci_write_1(scp, SCA_FIE0, 0);
    435  1.1  explorer 
    436  1.1  explorer 	msci_write_1(scp, SCA_SA00, 0);
    437  1.1  explorer 	msci_write_1(scp, SCA_SA10, 0);
    438  1.1  explorer 
    439  1.1  explorer 	msci_write_1(scp, SCA_IDL0, 0x7e);
    440  1.1  explorer 
    441  1.1  explorer 	msci_write_1(scp, SCA_RRC0, 0x0e);
    442  1.1  explorer 	msci_write_1(scp, SCA_TRC00, 0x10);
    443  1.1  explorer 	msci_write_1(scp, SCA_TRC10, 0x1f);
    444  1.1  explorer }
    445  1.1  explorer 
    446  1.1  explorer /*
    447  1.1  explorer  * Take the memory for the port and construct two circular linked lists of
    448  1.1  explorer  * descriptors (one tx, one rx) and set the pointers in these descriptors
    449  1.1  explorer  * to point to the buffer space for this port.
    450  1.1  explorer  */
    451  1.1  explorer static void
    452  1.1  explorer sca_dmac_init(struct sca_softc *sc, sca_port_t *scp)
    453  1.1  explorer {
    454  1.1  explorer 	sca_desc_t *desc;
    455  1.1  explorer 	u_int32_t desc_p;
    456  1.1  explorer 	u_int32_t buf_p;
    457  1.1  explorer 	int i;
    458  1.1  explorer 
    459  1.1  explorer 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmam,
    460  1.1  explorer 			0, sc->sc_allocsize, BUS_DMASYNC_PREWRITE);
    461  1.1  explorer 
    462  1.1  explorer 	desc = scp->txdesc;
    463  1.1  explorer 	desc_p = scp->txdesc_p;
    464  1.1  explorer 	buf_p = scp->txbuf_p;
    465  1.1  explorer 	scp->txcur = 0;
    466  1.1  explorer 	scp->txinuse = 0;
    467  1.1  explorer 
    468  1.1  explorer 	for (i = 0 ; i < SCA_NtxBUFS ; i++) {
    469  1.1  explorer 		/*
    470  1.1  explorer 		 * desc_p points to the physcial address of the NEXT desc
    471  1.1  explorer 		 */
    472  1.1  explorer 		desc_p += sizeof(sca_desc_t);
    473  1.1  explorer 
    474  1.1  explorer 		desc->cp = desc_p & 0x0000ffff;
    475  1.1  explorer 		desc->bp = buf_p & 0x0000ffff;
    476  1.1  explorer 		desc->bpb = (buf_p & 0x00ff0000) >> 16;
    477  1.1  explorer 		desc->len = SCA_BSIZE;
    478  1.1  explorer 		desc->stat = 0;
    479  1.1  explorer 
    480  1.1  explorer 		desc++;  /* point to the next descriptor */
    481  1.1  explorer 		buf_p += SCA_BSIZE;
    482  1.1  explorer 	}
    483  1.1  explorer 
    484  1.1  explorer 	/*
    485  1.1  explorer 	 * "heal" the circular list by making the last entry point to the
    486  1.1  explorer 	 * first.
    487  1.1  explorer 	 */
    488  1.1  explorer 	desc--;
    489  1.1  explorer 	desc->cp = scp->txdesc_p & 0x0000ffff;
    490  1.1  explorer 
    491  1.1  explorer 	/*
    492  1.1  explorer 	 * Now, initialize the transmit DMA logic
    493  1.1  explorer 	 *
    494  1.1  explorer 	 * CPB == chain pointer base address
    495  1.1  explorer 	 */
    496  1.1  explorer 	dmac_write_1(scp, SCA_DSR1, 0);
    497  1.1  explorer 	dmac_write_1(scp, SCA_DCR1, SCA_DCR_ABRT);
    498  1.1  explorer 	dmac_write_1(scp, SCA_DMR1, SCA_DMR_TMOD | SCA_DMR_NF);
    499  1.1  explorer 	dmac_write_1(scp, SCA_DIR1,
    500  1.1  explorer 		     (SCA_DIR_EOT | SCA_DIR_BOF | SCA_DIR_COF));
    501  1.1  explorer 	dmac_write_1(scp, SCA_CPB1,
    502  1.1  explorer 		     (u_int8_t)((scp->txdesc_p & 0x00ff0000) >> 16));
    503  1.1  explorer 
    504  1.1  explorer 	/*
    505  1.1  explorer 	 * now, do the same thing for receive descriptors
    506  1.1  explorer 	 */
    507  1.1  explorer 	desc = scp->rxdesc;
    508  1.1  explorer 	desc_p = scp->rxdesc_p;
    509  1.1  explorer 	buf_p = scp->rxbuf_p;
    510  1.1  explorer 	scp->rxstart = 0;
    511  1.1  explorer 	scp->rxend = SCA_NrxBUFS - 1;
    512  1.1  explorer 
    513  1.1  explorer 	for (i = 0 ; i < SCA_NrxBUFS ; i++) {
    514  1.1  explorer 		/*
    515  1.1  explorer 		 * desc_p points to the physcial address of the NEXT desc
    516  1.1  explorer 		 */
    517  1.1  explorer 		desc_p += sizeof(sca_desc_t);
    518  1.1  explorer 
    519  1.1  explorer 		desc->cp = desc_p & 0x0000ffff;
    520  1.1  explorer 		desc->bp = buf_p & 0x0000ffff;
    521  1.1  explorer 		desc->bpb = (buf_p & 0x00ff0000) >> 16;
    522  1.1  explorer 		desc->len = SCA_BSIZE;
    523  1.1  explorer 		desc->stat = 0x00;
    524  1.1  explorer 
    525  1.1  explorer 		desc++;  /* point to the next descriptor */
    526  1.1  explorer 		buf_p += SCA_BSIZE;
    527  1.1  explorer 	}
    528  1.1  explorer 
    529  1.1  explorer 	/*
    530  1.1  explorer 	 * "heal" the circular list by making the last entry point to the
    531  1.1  explorer 	 * first.
    532  1.1  explorer 	 */
    533  1.1  explorer 	desc--;
    534  1.1  explorer 	desc->cp = scp->rxdesc_p & 0x0000ffff;
    535  1.1  explorer 
    536  1.1  explorer 	sca_dmac_rxinit(scp);
    537  1.1  explorer 
    538  1.1  explorer 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmam,
    539  1.1  explorer 			0, sc->sc_allocsize, BUS_DMASYNC_POSTWRITE);
    540  1.1  explorer }
    541  1.1  explorer 
    542  1.1  explorer /*
    543  1.1  explorer  * reset and reinitialize the receive DMA logic
    544  1.1  explorer  */
    545  1.1  explorer static void
    546  1.1  explorer sca_dmac_rxinit(sca_port_t *scp)
    547  1.1  explorer {
    548  1.1  explorer 	/*
    549  1.1  explorer 	 * ... and the receive DMA logic ...
    550  1.1  explorer 	 */
    551  1.1  explorer 	dmac_write_1(scp, SCA_DSR0, 0);  /* disable DMA */
    552  1.1  explorer 	dmac_write_1(scp, SCA_DCR0, SCA_DCR_ABRT);
    553  1.1  explorer 
    554  1.1  explorer 	dmac_write_1(scp, SCA_DMR0, SCA_DMR_TMOD | SCA_DMR_NF);
    555  1.1  explorer 	dmac_write_2(scp, SCA_BFLL0, SCA_BSIZE);
    556  1.1  explorer 
    557  1.1  explorer 	/*
    558  1.1  explorer 	 * CPB == chain pointer base
    559  1.1  explorer 	 * CDA == current descriptor address
    560  1.1  explorer 	 * EDA == error descriptor address (overwrite position)
    561  1.1  explorer 	 */
    562  1.1  explorer 	dmac_write_1(scp, SCA_CPB0,
    563  1.1  explorer 		     (u_int8_t)((scp->rxdesc_p & 0x00ff0000) >> 16));
    564  1.1  explorer 	dmac_write_2(scp, SCA_CDAL0,
    565  1.1  explorer 		     (u_int16_t)(scp->rxdesc_p & 0xffff));
    566  1.1  explorer 	dmac_write_2(scp, SCA_EDAL0,
    567  1.1  explorer 		     (u_int16_t)(scp->rxdesc_p
    568  1.1  explorer 				 + sizeof(sca_desc_t) * SCA_NrxBUFS));
    569  1.1  explorer 
    570  1.1  explorer 	/*
    571  1.1  explorer 	 * enable receiver DMA
    572  1.1  explorer 	 */
    573  1.1  explorer 	dmac_write_1(scp, SCA_DIR0,
    574  1.1  explorer 		     (SCA_DIR_EOT | SCA_DIR_EOM | SCA_DIR_BOF | SCA_DIR_COF));
    575  1.1  explorer 	dmac_write_1(scp, SCA_DSR0, SCA_DSR_DE);
    576  1.1  explorer }
    577  1.1  explorer 
    578  1.1  explorer static int
    579  1.1  explorer sca_alloc_dma(struct sca_softc *sc)
    580  1.1  explorer {
    581  1.1  explorer 	u_int	allocsize;
    582  1.1  explorer 	int	err;
    583  1.1  explorer 	int	rsegs;
    584  1.1  explorer 	u_int	bpp;
    585  1.1  explorer 
    586  1.1  explorer 	SCA_DPRINTF(SCA_DEBUG_DMA,
    587  1.1  explorer 		    ("sizeof sca_desc_t: %d bytes\n", sizeof (sca_desc_t)));
    588  1.1  explorer 
    589  1.1  explorer 	bpp = sc->sc_numports * (SCA_NtxBUFS + SCA_NrxBUFS);
    590  1.1  explorer 
    591  1.1  explorer 	allocsize = bpp * (SCA_BSIZE + sizeof (sca_desc_t));
    592  1.1  explorer 
    593  1.1  explorer 	/*
    594  1.1  explorer 	 * sanity checks:
    595  1.1  explorer 	 *
    596  1.1  explorer 	 * Check the total size of the data buffers, and so on.  The total
    597  1.1  explorer 	 * DMAable space needs to fit within a single 16M region, and the
    598  1.1  explorer 	 * descriptors need to fit within a 64K region.
    599  1.1  explorer 	 */
    600  1.1  explorer 	if (allocsize > 16 * 1024 * 1024)
    601  1.1  explorer 		return 1;
    602  1.1  explorer 	if (bpp * sizeof (sca_desc_t) > 64 * 1024)
    603  1.1  explorer 		return 1;
    604  1.1  explorer 
    605  1.1  explorer 	sc->sc_allocsize = allocsize;
    606  1.1  explorer 
    607  1.1  explorer 	/*
    608  1.1  explorer 	 * Allocate one huge chunk of memory.
    609  1.1  explorer 	 */
    610  1.1  explorer 	if (bus_dmamem_alloc(sc->sc_dmat,
    611  1.1  explorer 			     allocsize,
    612  1.1  explorer 			     SCA_DMA_ALIGNMENT,
    613  1.1  explorer 			     SCA_DMA_BOUNDRY,
    614  1.1  explorer 			     &sc->sc_seg, 1, &rsegs, BUS_DMA_NOWAIT) != 0) {
    615  1.1  explorer 		printf("Could not allocate DMA memory\n");
    616  1.1  explorer 		return 1;
    617  1.1  explorer 	}
    618  1.1  explorer 	SCA_DPRINTF(SCA_DEBUG_DMA,
    619  1.1  explorer 		    ("DMA memory allocated:  %d bytes\n", allocsize));
    620  1.1  explorer 
    621  1.1  explorer 	if (bus_dmamem_map(sc->sc_dmat, &sc->sc_seg, 1, allocsize,
    622  1.1  explorer 			   &sc->sc_dma_addr, BUS_DMA_NOWAIT) != 0) {
    623  1.1  explorer 		printf("Could not map DMA memory into kernel space\n");
    624  1.1  explorer 		return 1;
    625  1.1  explorer 	}
    626  1.1  explorer 	SCA_DPRINTF(SCA_DEBUG_DMA, ("DMA memory mapped\n"));
    627  1.1  explorer 
    628  1.1  explorer 	if (bus_dmamap_create(sc->sc_dmat, allocsize, 2,
    629  1.1  explorer 			      allocsize, SCA_DMA_BOUNDRY,
    630  1.1  explorer 			      BUS_DMA_NOWAIT, &sc->sc_dmam) != 0) {
    631  1.1  explorer 		printf("Could not create DMA map\n");
    632  1.1  explorer 		return 1;
    633  1.1  explorer 	}
    634  1.1  explorer 	SCA_DPRINTF(SCA_DEBUG_DMA, ("DMA map created\n"));
    635  1.1  explorer 
    636  1.1  explorer 	err = bus_dmamap_load(sc->sc_dmat, sc->sc_dmam, sc->sc_dma_addr,
    637  1.1  explorer 			      allocsize, NULL, BUS_DMA_NOWAIT);
    638  1.1  explorer 	if (err != 0) {
    639  1.1  explorer 		printf("Could not load DMA segment:  %d\n", err);
    640  1.1  explorer 		return 1;
    641  1.1  explorer 	}
    642  1.1  explorer 	SCA_DPRINTF(SCA_DEBUG_DMA, ("DMA map loaded\n"));
    643  1.1  explorer 
    644  1.1  explorer 	return 0;
    645  1.1  explorer }
    646  1.1  explorer 
    647  1.1  explorer /*
    648  1.1  explorer  * Take the memory allocated with sca_alloc_dma() and divide it among the
    649  1.1  explorer  * two ports.
    650  1.1  explorer  */
    651  1.1  explorer static void
    652  1.1  explorer sca_setup_dma_memory(struct sca_softc *sc)
    653  1.1  explorer {
    654  1.1  explorer 	sca_port_t *scp0, *scp1;
    655  1.1  explorer 	u_int8_t  *vaddr0;
    656  1.1  explorer 	u_int32_t paddr0;
    657  1.1  explorer 	u_long addroff;
    658  1.1  explorer 
    659  1.1  explorer 	/*
    660  1.1  explorer 	 * remember the physical address to 24 bits only, since the upper
    661  1.1  explorer 	 * 8 bits is programed into the device at a different layer.
    662  1.1  explorer 	 */
    663  1.1  explorer 	paddr0 = (sc->sc_dmam->dm_segs[0].ds_addr & 0x00ffffff);
    664  1.1  explorer 	vaddr0 = sc->sc_dma_addr;
    665  1.1  explorer 
    666  1.1  explorer 	/*
    667  1.1  explorer 	 * if we have only one port it gets the full range.  If we have
    668  1.1  explorer 	 * two we need to do a little magic to divide things up.
    669  1.1  explorer 	 *
    670  1.1  explorer 	 * The descriptors will all end up in the front of the area, while
    671  1.1  explorer 	 * the remainder of the buffer is used for transmit and receive
    672  1.1  explorer 	 * data.
    673  1.1  explorer 	 *
    674  1.1  explorer 	 * -------------------- start of memory
    675  1.1  explorer 	 *    tx desc port 0
    676  1.1  explorer 	 *    rx desc port 0
    677  1.1  explorer 	 *    tx desc port 1
    678  1.1  explorer 	 *    rx desc port 1
    679  1.1  explorer 	 *    tx buffer port 0
    680  1.1  explorer 	 *    rx buffer port 0
    681  1.1  explorer 	 *    tx buffer port 1
    682  1.1  explorer 	 *    rx buffer port 1
    683  1.1  explorer 	 * -------------------- end of memory
    684  1.1  explorer 	 */
    685  1.1  explorer 	scp0 = &sc->sc_ports[0];
    686  1.1  explorer 	scp1 = &sc->sc_ports[1];
    687  1.1  explorer 
    688  1.1  explorer 	scp0->txdesc_p = paddr0;
    689  1.1  explorer 	scp0->txdesc = (sca_desc_t *)vaddr0;
    690  1.1  explorer 	addroff = sizeof(sca_desc_t) * SCA_NtxBUFS;
    691  1.1  explorer 
    692  1.1  explorer 	/*
    693  1.1  explorer 	 * point to the range following the tx descriptors, and
    694  1.1  explorer 	 * set the rx descriptors there.
    695  1.1  explorer 	 */
    696  1.1  explorer 	scp0->rxdesc_p = paddr0 + addroff;
    697  1.1  explorer 	scp0->rxdesc = (sca_desc_t *)(vaddr0 + addroff);
    698  1.1  explorer 	addroff += sizeof(sca_desc_t) * SCA_NrxBUFS;
    699  1.1  explorer 
    700  1.1  explorer 	if (sc->sc_numports == 2) {
    701  1.1  explorer 		scp1->txdesc_p = paddr0 + addroff;
    702  1.1  explorer 		scp1->txdesc = (sca_desc_t *)(vaddr0 + addroff);
    703  1.1  explorer 		addroff += sizeof(sca_desc_t) * SCA_NtxBUFS;
    704  1.1  explorer 
    705  1.1  explorer 		scp1->rxdesc_p = paddr0 + addroff;
    706  1.1  explorer 		scp1->rxdesc = (sca_desc_t *)(vaddr0 + addroff);
    707  1.1  explorer 		addroff += sizeof(sca_desc_t) * SCA_NrxBUFS;
    708  1.1  explorer 	}
    709  1.1  explorer 
    710  1.1  explorer 	/*
    711  1.1  explorer 	 * point to the memory following the descriptors, and set the
    712  1.1  explorer 	 * transmit buffer there.
    713  1.1  explorer 	 */
    714  1.1  explorer 	scp0->txbuf_p = paddr0 + addroff;
    715  1.1  explorer 	scp0->txbuf = vaddr0 + addroff;
    716  1.1  explorer 	addroff += SCA_BSIZE * SCA_NtxBUFS;
    717  1.1  explorer 
    718  1.1  explorer 	/*
    719  1.1  explorer 	 * lastly, skip over the transmit buffer and set up pointers into
    720  1.1  explorer 	 * the receive buffer.
    721  1.1  explorer 	 */
    722  1.1  explorer 	scp0->rxbuf_p = paddr0 + addroff;
    723  1.1  explorer 	scp0->rxbuf = vaddr0 + addroff;
    724  1.1  explorer 	addroff += SCA_BSIZE * SCA_NrxBUFS;
    725  1.1  explorer 
    726  1.1  explorer 	if (sc->sc_numports == 2) {
    727  1.1  explorer 		scp1->txbuf_p = paddr0 + addroff;
    728  1.1  explorer 		scp1->txbuf = vaddr0 + addroff;
    729  1.1  explorer 		addroff += SCA_BSIZE * SCA_NtxBUFS;
    730  1.1  explorer 
    731  1.1  explorer 		scp1->rxbuf_p = paddr0 + addroff;
    732  1.1  explorer 		scp1->rxbuf = vaddr0 + addroff;
    733  1.1  explorer 		addroff += SCA_BSIZE * SCA_NrxBUFS;
    734  1.1  explorer 	}
    735  1.1  explorer 
    736  1.1  explorer 	/*
    737  1.1  explorer 	 * as a consistancy check, addroff should be equal to the allocation
    738  1.1  explorer 	 * size.
    739  1.1  explorer 	 */
    740  1.1  explorer 	if (sc->sc_allocsize != addroff)
    741  1.1  explorer 		printf("ERROR:  sc_allocsize != addroff: %lu != %lu\n",
    742  1.1  explorer 		       sc->sc_allocsize, addroff);
    743  1.1  explorer }
    744  1.1  explorer 
    745  1.1  explorer /*
    746  1.1  explorer  * Queue the packet for our start routine to transmit
    747  1.1  explorer  */
    748  1.1  explorer static int
    749  1.1  explorer sca_output(ifp, m, dst, rt0)
    750  1.1  explorer      struct ifnet *ifp;
    751  1.1  explorer      struct mbuf *m;
    752  1.1  explorer      struct sockaddr *dst;
    753  1.1  explorer      struct rtentry *rt0;
    754  1.1  explorer {
    755  1.1  explorer 	int error;
    756  1.1  explorer 	int s;
    757  1.1  explorer 	u_int16_t protocol;
    758  1.1  explorer 	hdlc_header_t *hdlc;
    759  1.1  explorer 	struct ifqueue *ifq;
    760  1.1  explorer #ifdef SCA_USE_FASTQ
    761  1.1  explorer 	struct ip *ip;
    762  1.1  explorer 	sca_port_t *scp = ifp->if_softc;
    763  1.1  explorer 	int highpri;
    764  1.1  explorer #endif
    765  1.1  explorer 
    766  1.1  explorer 	error = 0;
    767  1.1  explorer 	ifp->if_lastchange = time;
    768  1.1  explorer 
    769  1.1  explorer 	if ((ifp->if_flags & IFF_UP) != IFF_UP) {
    770  1.1  explorer 		error = ENETDOWN;
    771  1.1  explorer 		goto bad;
    772  1.1  explorer 	}
    773  1.1  explorer 
    774  1.1  explorer #ifdef SCA_USE_FASTQ
    775  1.1  explorer 	highpri = 0;
    776  1.1  explorer #endif
    777  1.1  explorer 
    778  1.1  explorer 	/*
    779  1.1  explorer 	 * determine address family, and priority for this packet
    780  1.1  explorer 	 */
    781  1.1  explorer 	switch (dst->sa_family) {
    782  1.1  explorer 	case AF_INET:
    783  1.1  explorer 		protocol = HDLC_PROTOCOL_IP;
    784  1.1  explorer 
    785  1.1  explorer #ifdef SCA_USE_FASTQ
    786  1.1  explorer 		ip = mtod(m, struct ip *);
    787  1.1  explorer 		if ((ip->ip_tos & IPTOS_LOWDELAY) == IPTOS_LOWDELAY)
    788  1.1  explorer 			highpri = 1;
    789  1.1  explorer #endif
    790  1.1  explorer 		break;
    791  1.1  explorer 
    792  1.1  explorer 	default:
    793  1.1  explorer 		printf("%s: address family %d unsupported\n",
    794  1.1  explorer 		       ifp->if_xname, dst->sa_family);
    795  1.1  explorer 		error = EAFNOSUPPORT;
    796  1.1  explorer 		goto bad;
    797  1.1  explorer 	}
    798  1.1  explorer 
    799  1.1  explorer 	if (M_LEADINGSPACE(m) < HDLC_HDRLEN) {
    800  1.1  explorer 		m = m_prepend(m, HDLC_HDRLEN, M_DONTWAIT);
    801  1.1  explorer 		if (m == NULL) {
    802  1.1  explorer 			error = ENOBUFS;
    803  1.1  explorer 			goto bad;
    804  1.1  explorer 		}
    805  1.1  explorer 		m->m_len = 0;
    806  1.1  explorer 	} else {
    807  1.1  explorer 		m->m_data -= HDLC_HDRLEN;
    808  1.1  explorer 	}
    809  1.1  explorer 
    810  1.1  explorer 	hdlc = mtod(m, hdlc_header_t *);
    811  1.1  explorer 	if ((m->m_flags & (M_BCAST | M_MCAST)) != 0)
    812  1.1  explorer 		hdlc->addr = CISCO_MULTICAST;
    813  1.1  explorer 	else
    814  1.1  explorer 		hdlc->addr = CISCO_UNICAST;
    815  1.1  explorer 	hdlc->control = 0;
    816  1.1  explorer 	hdlc->protocol = htons(protocol);
    817  1.1  explorer 	m->m_len += HDLC_HDRLEN;
    818  1.1  explorer 
    819  1.1  explorer 	/*
    820  1.1  explorer 	 * queue the packet.  If interactive, use the fast queue.
    821  1.1  explorer 	 */
    822  1.2   mycroft 	s = splnet();
    823  1.1  explorer #ifdef SCA_USE_FASTQ
    824  1.1  explorer 	ifq = (highpri == 1 ? &scp->fastq : &ifp->if_snd);
    825  1.1  explorer #else
    826  1.1  explorer 	ifq = &ifp->if_snd;
    827  1.1  explorer #endif
    828  1.1  explorer 	if (IF_QFULL(ifq)) {
    829  1.1  explorer 		IF_DROP(ifq);
    830  1.1  explorer 		ifp->if_oerrors++;
    831  1.1  explorer 		ifp->if_collisions++;
    832  1.1  explorer 		error = ENOBUFS;
    833  1.1  explorer 		splx(s);
    834  1.1  explorer 		goto bad;
    835  1.1  explorer 	}
    836  1.1  explorer 	ifp->if_obytes += m->m_pkthdr.len;
    837  1.1  explorer 	IF_ENQUEUE(ifq, m);
    838  1.1  explorer 
    839  1.1  explorer 	ifp->if_lastchange = time;
    840  1.1  explorer 
    841  1.1  explorer 	if (m->m_flags & M_MCAST)
    842  1.1  explorer 		ifp->if_omcasts++;
    843  1.1  explorer 
    844  1.1  explorer 	sca_start(ifp);
    845  1.1  explorer 	splx(s);
    846  1.1  explorer 
    847  1.1  explorer 	return (error);
    848  1.1  explorer 
    849  1.1  explorer  bad:
    850  1.1  explorer 	if (m)
    851  1.1  explorer 		m_freem(m);
    852  1.1  explorer 	return (error);
    853  1.1  explorer }
    854  1.1  explorer 
    855  1.1  explorer static int
    856  1.1  explorer sca_ioctl(ifp, cmd, addr)
    857  1.1  explorer      struct ifnet *ifp;
    858  1.1  explorer      u_long cmd;
    859  1.1  explorer      caddr_t addr;
    860  1.1  explorer {
    861  1.1  explorer 	struct ifreq *ifr;
    862  1.1  explorer 	struct ifaddr *ifa;
    863  1.1  explorer 	int error;
    864  1.1  explorer 	int s;
    865  1.1  explorer 
    866  1.2   mycroft 	s = splnet();
    867  1.1  explorer 
    868  1.1  explorer 	ifr = (struct ifreq *)addr;
    869  1.1  explorer 	ifa = (struct ifaddr *)addr;
    870  1.1  explorer 	error = 0;
    871  1.1  explorer 
    872  1.1  explorer 	switch (cmd) {
    873  1.1  explorer 	case SIOCSIFADDR:
    874  1.1  explorer 		if (ifa->ifa_addr->sa_family == AF_INET)
    875  1.1  explorer 			sca_port_up(ifp->if_softc);
    876  1.1  explorer 		else
    877  1.1  explorer 			error = EAFNOSUPPORT;
    878  1.1  explorer 		break;
    879  1.1  explorer 
    880  1.1  explorer 	case SIOCSIFDSTADDR:
    881  1.1  explorer 		if (ifa->ifa_addr->sa_family != AF_INET)
    882  1.1  explorer 			error = EAFNOSUPPORT;
    883  1.1  explorer 		break;
    884  1.1  explorer 
    885  1.1  explorer 	case SIOCADDMULTI:
    886  1.1  explorer 	case SIOCDELMULTI:
    887  1.1  explorer 		if (ifr == 0) {
    888  1.1  explorer 			error = EAFNOSUPPORT;		/* XXX */
    889  1.1  explorer 			break;
    890  1.1  explorer 		}
    891  1.1  explorer 		switch (ifr->ifr_addr.sa_family) {
    892  1.1  explorer 
    893  1.1  explorer #ifdef INET
    894  1.1  explorer 		case AF_INET:
    895  1.1  explorer 			break;
    896  1.1  explorer #endif
    897  1.1  explorer 
    898  1.1  explorer 		default:
    899  1.1  explorer 			error = EAFNOSUPPORT;
    900  1.1  explorer 			break;
    901  1.1  explorer 		}
    902  1.1  explorer 		break;
    903  1.1  explorer 
    904  1.1  explorer 	case SIOCSIFFLAGS:
    905  1.1  explorer 		if (ifr->ifr_flags & IFF_UP)
    906  1.1  explorer 			sca_port_up(ifp->if_softc);
    907  1.1  explorer 		else
    908  1.1  explorer 			sca_port_down(ifp->if_softc);
    909  1.1  explorer 
    910  1.1  explorer 		break;
    911  1.1  explorer 
    912  1.1  explorer 	default:
    913  1.1  explorer 		error = EINVAL;
    914  1.1  explorer 	}
    915  1.1  explorer 
    916  1.1  explorer 	splx(s);
    917  1.1  explorer 	return error;
    918  1.1  explorer }
    919  1.1  explorer 
    920  1.1  explorer /*
    921  1.1  explorer  * start packet transmission on the interface
    922  1.1  explorer  *
    923  1.2   mycroft  * MUST BE CALLED AT splnet()
    924  1.1  explorer  */
    925  1.1  explorer static void
    926  1.1  explorer sca_start(ifp)
    927  1.1  explorer 	struct ifnet *ifp;
    928  1.1  explorer {
    929  1.1  explorer 	sca_port_t *scp = ifp->if_softc;
    930  1.1  explorer 	struct sca_softc *sc = scp->sca;
    931  1.1  explorer 	struct mbuf *m, *mb_head;
    932  1.1  explorer 	sca_desc_t *desc;
    933  1.5       erh 	u_int8_t *buf;
    934  1.1  explorer 	u_int32_t buf_p;
    935  1.1  explorer 	int trigger_xmit;
    936  1.1  explorer 
    937  1.1  explorer 	/*
    938  1.1  explorer 	 * can't queue when we are full or transmitter is busy
    939  1.1  explorer 	 */
    940  1.1  explorer 	if ((scp->txinuse >= (SCA_NtxBUFS - 1))
    941  1.1  explorer 	    || ((ifp->if_flags & IFF_OACTIVE) == IFF_OACTIVE))
    942  1.1  explorer 		return;
    943  1.1  explorer 
    944  1.1  explorer 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmam,
    945  1.1  explorer 			0, sc->sc_allocsize,
    946  1.1  explorer 			BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    947  1.1  explorer 
    948  1.1  explorer 	trigger_xmit = 0;
    949  1.1  explorer 
    950  1.1  explorer  txloop:
    951  1.1  explorer 	IF_DEQUEUE(&scp->linkq, mb_head);
    952  1.1  explorer 	if (mb_head == NULL)
    953  1.1  explorer #ifdef SCA_USE_FASTQ
    954  1.1  explorer 		IF_DEQUEUE(&scp->fastq, mb_head);
    955  1.1  explorer 	if (mb_head == NULL)
    956  1.1  explorer #endif
    957  1.1  explorer 		IF_DEQUEUE(&ifp->if_snd, mb_head);
    958  1.1  explorer 	if (mb_head == NULL)
    959  1.1  explorer 		goto start_xmit;
    960  1.1  explorer 
    961  1.1  explorer 	desc = &scp->txdesc[scp->txcur];
    962  1.1  explorer 	if (scp->txinuse != 0) {
    963  1.5       erh 		/* Kill EOT interrupts on the previous packet. */
    964  1.1  explorer 		desc->stat &= ~SCA_DESC_EOT;
    965  1.5       erh 		desc = &scp->txdesc[scp->txcur+1];
    966  1.1  explorer 	}
    967  1.1  explorer 	buf = scp->txbuf + SCA_BSIZE * scp->txcur;
    968  1.1  explorer 	buf_p = scp->txbuf_p + SCA_BSIZE * scp->txcur;
    969  1.1  explorer 
    970  1.1  explorer 	desc->bp = (u_int16_t)(buf_p & 0x0000ffff);
    971  1.1  explorer 	desc->bpb = (u_int8_t)((buf_p & 0x00ff0000) >> 16);
    972  1.1  explorer 	desc->stat = SCA_DESC_EOT | SCA_DESC_EOM;  /* end of frame and xfer */
    973  1.1  explorer 	desc->len = 0;
    974  1.1  explorer 
    975  1.1  explorer 	/*
    976  1.1  explorer 	 * Run through the chain, copying data into the descriptor as we
    977  1.1  explorer 	 * go.  If it won't fit in one transmission block, drop the packet.
    978  1.1  explorer 	 * No, this isn't nice, but most of the time it _will_ fit.
    979  1.1  explorer 	 */
    980  1.1  explorer 	for (m = mb_head ; m != NULL ; m = m->m_next) {
    981  1.1  explorer 		if (m->m_len != 0) {
    982  1.1  explorer 			desc->len += m->m_len;
    983  1.1  explorer 			if (desc->len > SCA_BSIZE) {
    984  1.1  explorer 				m_freem(mb_head);
    985  1.1  explorer 				goto txloop;
    986  1.1  explorer 			}
    987  1.1  explorer 			bcopy(mtod(m, u_int8_t *), buf, m->m_len);
    988  1.1  explorer 			buf += m->m_len;
    989  1.1  explorer 		}
    990  1.1  explorer 	}
    991  1.1  explorer 
    992  1.1  explorer 	ifp->if_opackets++;
    993  1.1  explorer 
    994  1.1  explorer #if NBPFILTER > 0
    995  1.1  explorer 	/*
    996  1.1  explorer 	 * Pass packet to bpf if there is a listener.
    997  1.1  explorer 	 */
    998  1.1  explorer 	if (scp->sp_bpf)
    999  1.1  explorer 		bpf_mtap(scp->sp_bpf, mb_head);
   1000  1.1  explorer #endif
   1001  1.1  explorer 
   1002  1.1  explorer 	m_freem(mb_head);
   1003  1.1  explorer 
   1004  1.1  explorer 	if (scp->txinuse != 0) {
   1005  1.1  explorer 		scp->txcur++;
   1006  1.1  explorer 		if (scp->txcur == SCA_NtxBUFS)
   1007  1.1  explorer 			scp->txcur = 0;
   1008  1.1  explorer 	}
   1009  1.1  explorer 	scp->txinuse++;
   1010  1.1  explorer 	trigger_xmit = 1;
   1011  1.1  explorer 
   1012  1.1  explorer 	SCA_DPRINTF(SCA_DEBUG_TX,
   1013  1.1  explorer 		    ("TX: inuse %d index %d\n", scp->txinuse, scp->txcur));
   1014  1.1  explorer 
   1015  1.1  explorer 	if (scp->txinuse < (SCA_NtxBUFS - 1))
   1016  1.1  explorer 		goto txloop;
   1017  1.1  explorer 
   1018  1.1  explorer  start_xmit:
   1019  1.1  explorer 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmam,
   1020  1.1  explorer 			0, sc->sc_allocsize,
   1021  1.1  explorer 			BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1022  1.1  explorer 
   1023  1.1  explorer 	if (trigger_xmit != 0)
   1024  1.1  explorer 		sca_port_starttx(scp);
   1025  1.1  explorer }
   1026  1.1  explorer 
   1027  1.1  explorer static void
   1028  1.1  explorer sca_watchdog(ifp)
   1029  1.1  explorer 	struct ifnet *ifp;
   1030  1.1  explorer {
   1031  1.1  explorer }
   1032  1.1  explorer 
   1033  1.1  explorer int
   1034  1.1  explorer sca_hardintr(struct sca_softc *sc)
   1035  1.1  explorer {
   1036  1.1  explorer 	u_int8_t isr0, isr1, isr2;
   1037  1.1  explorer 	int	ret;
   1038  1.1  explorer 
   1039  1.1  explorer 	ret = 0;  /* non-zero means we processed at least one interrupt */
   1040  1.1  explorer 
   1041  1.1  explorer 	while (1) {
   1042  1.1  explorer 		/*
   1043  1.1  explorer 		 * read SCA interrupts
   1044  1.1  explorer 		 */
   1045  1.1  explorer 		isr0 = sca_read_1(sc, SCA_ISR0);
   1046  1.1  explorer 		isr1 = sca_read_1(sc, SCA_ISR1);
   1047  1.1  explorer 		isr2 = sca_read_1(sc, SCA_ISR2);
   1048  1.1  explorer 
   1049  1.1  explorer 		if (isr0 == 0 && isr1 == 0 && isr2 == 0)
   1050  1.1  explorer 			break;
   1051  1.1  explorer 
   1052  1.1  explorer 		SCA_DPRINTF(SCA_DEBUG_INTR,
   1053  1.1  explorer 			    ("isr0 = %02x, isr1 = %02x, isr2 = %02x\n",
   1054  1.1  explorer 			     isr0, isr1, isr2));
   1055  1.1  explorer 
   1056  1.1  explorer 		/*
   1057  1.1  explorer 		 * check DMA interrupt
   1058  1.1  explorer 		 */
   1059  1.1  explorer 		if (isr1 & 0x0f)
   1060  1.1  explorer 			ret += sca_dmac_intr(&sc->sc_ports[0],
   1061  1.1  explorer 					     isr1 & 0x0f);
   1062  1.1  explorer 		if (isr1 & 0xf0)
   1063  1.1  explorer 			ret += sca_dmac_intr(&sc->sc_ports[1],
   1064  1.1  explorer 					     (isr1 & 0xf0) >> 4);
   1065  1.1  explorer 
   1066  1.1  explorer 		if (isr0)
   1067  1.1  explorer 			ret += sca_msci_intr(sc, isr0);
   1068  1.1  explorer 
   1069  1.1  explorer #if 0 /* We don't GET timer interrupts, we have them disabled (msci IE20) */
   1070  1.1  explorer 		if (isr2)
   1071  1.1  explorer 			ret += sca_timer_intr(sc, isr2);
   1072  1.1  explorer #endif
   1073  1.1  explorer 	}
   1074  1.1  explorer 
   1075  1.1  explorer 	return (ret);
   1076  1.1  explorer }
   1077  1.1  explorer 
   1078  1.1  explorer static int
   1079  1.1  explorer sca_dmac_intr(sca_port_t *scp, u_int8_t isr)
   1080  1.1  explorer {
   1081  1.1  explorer 	u_int8_t	 dsr;
   1082  1.1  explorer 	int		 ret;
   1083  1.1  explorer 
   1084  1.1  explorer 	ret = 0;
   1085  1.1  explorer 
   1086  1.1  explorer 	/*
   1087  1.1  explorer 	 * Check transmit channel
   1088  1.1  explorer 	 */
   1089  1.1  explorer 	if (isr & 0x0c) {
   1090  1.1  explorer 		SCA_DPRINTF(SCA_DEBUG_INTR,
   1091  1.1  explorer 			    ("TX INTERRUPT port %d\n", scp->sp_port));
   1092  1.1  explorer 
   1093  1.1  explorer 		dsr = 1;
   1094  1.1  explorer 		while (dsr != 0) {
   1095  1.1  explorer 			ret++;
   1096  1.1  explorer 			/*
   1097  1.1  explorer 			 * reset interrupt
   1098  1.1  explorer 			 */
   1099  1.1  explorer 			dsr = dmac_read_1(scp, SCA_DSR1);
   1100  1.1  explorer 			dmac_write_1(scp, SCA_DSR1,
   1101  1.1  explorer 				     dsr | SCA_DSR_DEWD);
   1102  1.1  explorer 
   1103  1.1  explorer 			/*
   1104  1.1  explorer 			 * filter out the bits we don't care about
   1105  1.1  explorer 			 */
   1106  1.1  explorer 			dsr &= ( SCA_DSR_COF | SCA_DSR_BOF | SCA_DSR_EOT);
   1107  1.1  explorer 			if (dsr == 0)
   1108  1.1  explorer 				break;
   1109  1.1  explorer 
   1110  1.1  explorer 			/*
   1111  1.1  explorer 			 * check for counter overflow
   1112  1.1  explorer 			 */
   1113  1.1  explorer 			if (dsr & SCA_DSR_COF) {
   1114  1.1  explorer 				printf("%s: TXDMA counter overflow\n",
   1115  1.1  explorer 				       scp->sp_if.if_xname);
   1116  1.1  explorer 
   1117  1.1  explorer 				scp->sp_if.if_flags &= ~IFF_OACTIVE;
   1118  1.1  explorer 				scp->txcur = 0;
   1119  1.1  explorer 				scp->txinuse = 0;
   1120  1.1  explorer 			}
   1121  1.1  explorer 
   1122  1.1  explorer 			/*
   1123  1.1  explorer 			 * check for buffer overflow
   1124  1.1  explorer 			 */
   1125  1.1  explorer 			if (dsr & SCA_DSR_BOF) {
   1126  1.1  explorer 				printf("%s: TXDMA buffer overflow, cda 0x%04x, eda 0x%04x, cpb 0x%02x\n",
   1127  1.1  explorer 				       scp->sp_if.if_xname,
   1128  1.1  explorer 				       dmac_read_2(scp, SCA_CDAL1),
   1129  1.1  explorer 				       dmac_read_2(scp, SCA_EDAL1),
   1130  1.1  explorer 				       dmac_read_1(scp, SCA_CPB1));
   1131  1.1  explorer 
   1132  1.1  explorer 				/*
   1133  1.1  explorer 				 * Yikes.  Arrange for a full
   1134  1.1  explorer 				 * transmitter restart.
   1135  1.1  explorer 				 */
   1136  1.1  explorer 				scp->sp_if.if_flags &= ~IFF_OACTIVE;
   1137  1.1  explorer 				scp->txcur = 0;
   1138  1.1  explorer 				scp->txinuse = 0;
   1139  1.1  explorer 			}
   1140  1.1  explorer 
   1141  1.1  explorer 			/*
   1142  1.1  explorer 			 * check for end of transfer, which is not
   1143  1.1  explorer 			 * an error. It means that all data queued
   1144  1.1  explorer 			 * was transmitted, and we mark ourself as
   1145  1.1  explorer 			 * not in use and stop the watchdog timer.
   1146  1.1  explorer 			 */
   1147  1.1  explorer 			if (dsr & SCA_DSR_EOT) {
   1148  1.1  explorer 				SCA_DPRINTF(SCA_DEBUG_TX,
   1149  1.1  explorer 					    ("Transmit completed.\n"));
   1150  1.1  explorer 
   1151  1.1  explorer 				scp->sp_if.if_flags &= ~IFF_OACTIVE;
   1152  1.1  explorer 				scp->txcur = 0;
   1153  1.1  explorer 				scp->txinuse = 0;
   1154  1.1  explorer 
   1155  1.1  explorer 				/*
   1156  1.1  explorer 				 * check for more packets
   1157  1.1  explorer 				 */
   1158  1.1  explorer 				sca_start(&scp->sp_if);
   1159  1.1  explorer 			}
   1160  1.1  explorer 		}
   1161  1.1  explorer 	}
   1162  1.1  explorer 	/*
   1163  1.1  explorer 	 * receive channel check
   1164  1.1  explorer 	 */
   1165  1.1  explorer 	if (isr & 0x03) {
   1166  1.1  explorer 		SCA_DPRINTF(SCA_DEBUG_INTR,
   1167  1.1  explorer 			    ("RX INTERRUPT port %d\n", mch));
   1168  1.1  explorer 
   1169  1.1  explorer 		dsr = 1;
   1170  1.1  explorer 		while (dsr != 0) {
   1171  1.1  explorer 			ret++;
   1172  1.1  explorer 
   1173  1.1  explorer 			dsr = dmac_read_1(scp, SCA_DSR0);
   1174  1.1  explorer 			dmac_write_1(scp, SCA_DSR0, dsr | SCA_DSR_DEWD);
   1175  1.1  explorer 
   1176  1.1  explorer 			/*
   1177  1.1  explorer 			 * filter out the bits we don't care about
   1178  1.1  explorer 			 */
   1179  1.1  explorer 			dsr &= (SCA_DSR_EOM | SCA_DSR_COF
   1180  1.1  explorer 				| SCA_DSR_BOF | SCA_DSR_EOT);
   1181  1.1  explorer 			if (dsr == 0)
   1182  1.1  explorer 				break;
   1183  1.1  explorer 
   1184  1.1  explorer 			/*
   1185  1.1  explorer 			 * End of frame
   1186  1.1  explorer 			 */
   1187  1.1  explorer 			if (dsr & SCA_DSR_EOM) {
   1188  1.1  explorer 				SCA_DPRINTF(SCA_DEBUG_RX, ("Got a frame!\n"));
   1189  1.1  explorer 
   1190  1.1  explorer 				sca_get_packets(scp);
   1191  1.1  explorer 			}
   1192  1.1  explorer 
   1193  1.1  explorer 			/*
   1194  1.1  explorer 			 * check for counter overflow
   1195  1.1  explorer 			 */
   1196  1.1  explorer 			if (dsr & SCA_DSR_COF) {
   1197  1.1  explorer 				printf("%s: RXDMA counter overflow\n",
   1198  1.1  explorer 				       scp->sp_if.if_xname);
   1199  1.1  explorer 
   1200  1.1  explorer 				sca_dmac_rxinit(scp);
   1201  1.1  explorer 			}
   1202  1.1  explorer 
   1203  1.1  explorer 			/*
   1204  1.1  explorer 			 * check for end of transfer, which means we
   1205  1.1  explorer 			 * ran out of descriptors to receive into.
   1206  1.1  explorer 			 * This means the line is much faster than
   1207  1.1  explorer 			 * we can handle.
   1208  1.1  explorer 			 */
   1209  1.1  explorer 			if (dsr & (SCA_DSR_BOF | SCA_DSR_EOT)) {
   1210  1.1  explorer 				printf("%s: RXDMA buffer overflow\n",
   1211  1.1  explorer 				       scp->sp_if.if_xname);
   1212  1.1  explorer 
   1213  1.1  explorer 				sca_dmac_rxinit(scp);
   1214  1.1  explorer 			}
   1215  1.1  explorer 		}
   1216  1.1  explorer 	}
   1217  1.1  explorer 
   1218  1.1  explorer 	return ret;
   1219  1.1  explorer }
   1220  1.1  explorer 
   1221  1.1  explorer static int
   1222  1.1  explorer sca_msci_intr(struct sca_softc *sc, u_int8_t isr)
   1223  1.1  explorer {
   1224  1.1  explorer 	printf("Got msci interrupt XXX\n");
   1225  1.1  explorer 
   1226  1.1  explorer 	return 0;
   1227  1.1  explorer }
   1228  1.1  explorer 
   1229  1.1  explorer static void
   1230  1.1  explorer sca_get_packets(sca_port_t *scp)
   1231  1.1  explorer {
   1232  1.1  explorer 	int		 descidx;
   1233  1.1  explorer 	sca_desc_t	*desc;
   1234  1.1  explorer 	u_int8_t	*buf;
   1235  1.1  explorer 
   1236  1.1  explorer 	bus_dmamap_sync(scp->sca->sc_dmat, scp->sca->sc_dmam,
   1237  1.1  explorer 			0, scp->sca->sc_allocsize,
   1238  1.1  explorer 			BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1239  1.1  explorer 
   1240  1.1  explorer 	/*
   1241  1.1  explorer 	 * Loop while there are packets to receive.  After each is processed,
   1242  1.1  explorer 	 * call sca_frame_skip() to update the DMA registers to the new
   1243  1.1  explorer 	 * state.
   1244  1.1  explorer 	 */
   1245  1.1  explorer 	while (sca_frame_avail(scp, &descidx)) {
   1246  1.1  explorer 		desc = &scp->rxdesc[descidx];
   1247  1.1  explorer 		buf = scp->rxbuf + SCA_BSIZE * descidx;
   1248  1.1  explorer 
   1249  1.1  explorer 		sca_frame_process(scp, desc, buf);
   1250  1.1  explorer #if SCA_DEBUG_LEVEL > 0
   1251  1.1  explorer 		if (sca_debug & SCA_DEBUG_RXPKT)
   1252  1.1  explorer 			sca_frame_print(scp, desc, buf);
   1253  1.1  explorer #endif
   1254  1.1  explorer 		sca_frame_skip(scp, descidx);
   1255  1.1  explorer 	}
   1256  1.1  explorer 
   1257  1.1  explorer 	bus_dmamap_sync(scp->sca->sc_dmat, scp->sca->sc_dmam,
   1258  1.1  explorer 			0, scp->sca->sc_allocsize,
   1259  1.1  explorer 			BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1260  1.1  explorer }
   1261  1.1  explorer 
   1262  1.1  explorer /*
   1263  1.1  explorer  * Starting with the first descriptor we wanted to read into, up to but
   1264  1.1  explorer  * not including the current SCA read descriptor, look for a packet.
   1265  1.1  explorer  */
   1266  1.1  explorer static int
   1267  1.1  explorer sca_frame_avail(sca_port_t *scp, int *descindx)
   1268  1.1  explorer {
   1269  1.1  explorer 	u_int16_t	 cda;
   1270  1.1  explorer 	int		 cdaidx;
   1271  1.1  explorer 	u_int32_t	 desc_p;	/* physical address (lower 16 bits) */
   1272  1.1  explorer 	sca_desc_t	*desc;
   1273  1.1  explorer 	u_int8_t	 rxstat;
   1274  1.1  explorer 
   1275  1.1  explorer 	/*
   1276  1.1  explorer 	 * Read the current descriptor from the SCA.
   1277  1.1  explorer 	 */
   1278  1.1  explorer 	cda = dmac_read_2(scp, SCA_CDAL0);
   1279  1.1  explorer 
   1280  1.1  explorer 	/*
   1281  1.1  explorer 	 * calculate the index of the current descriptor
   1282  1.1  explorer 	 */
   1283  1.1  explorer 	desc_p = cda - (u_int16_t)(scp->rxdesc_p & 0x0000ffff);
   1284  1.1  explorer 	cdaidx = desc_p / sizeof(sca_desc_t);
   1285  1.1  explorer 
   1286  1.1  explorer 	if (cdaidx >= SCA_NrxBUFS)
   1287  1.1  explorer 		return 0;
   1288  1.1  explorer 
   1289  1.1  explorer 	for (;;) {
   1290  1.1  explorer 		/*
   1291  1.1  explorer 		 * if the SCA is reading into the first descriptor, we somehow
   1292  1.1  explorer 		 * got this interrupt incorrectly.  Just return that there are
   1293  1.1  explorer 		 * no packets ready.
   1294  1.1  explorer 		 */
   1295  1.1  explorer 		if (cdaidx == scp->rxstart)
   1296  1.1  explorer 			return 0;
   1297  1.1  explorer 
   1298  1.1  explorer 		/*
   1299  1.1  explorer 		 * We might have a valid descriptor.  Set up a pointer
   1300  1.1  explorer 		 * to the kva address for it so we can more easily examine
   1301  1.1  explorer 		 * the contents.
   1302  1.1  explorer 		 */
   1303  1.1  explorer 		desc = &scp->rxdesc[scp->rxstart];
   1304  1.1  explorer 
   1305  1.1  explorer 		rxstat = desc->stat;
   1306  1.1  explorer 
   1307  1.1  explorer 		/*
   1308  1.1  explorer 		 * check for errors
   1309  1.1  explorer 		 */
   1310  1.1  explorer 		if (rxstat & SCA_DESC_ERRORS)
   1311  1.1  explorer 			goto nextpkt;
   1312  1.1  explorer 
   1313  1.1  explorer 		/*
   1314  1.1  explorer 		 * full packet?  Good.
   1315  1.1  explorer 		 */
   1316  1.1  explorer 		if (rxstat & SCA_DESC_EOM) {
   1317  1.1  explorer 			*descindx = scp->rxstart;
   1318  1.1  explorer 			return 1;
   1319  1.1  explorer 		}
   1320  1.1  explorer 
   1321  1.1  explorer 		/*
   1322  1.1  explorer 		 * increment the rxstart address, since this frame is
   1323  1.1  explorer 		 * somehow damaged.  Skip over it in later calls.
   1324  1.1  explorer 		 * XXX This breaks multidescriptor receives, so each
   1325  1.1  explorer 		 * frame HAS to fit within one descriptor's buffer
   1326  1.1  explorer 		 * space now...
   1327  1.1  explorer 		 */
   1328  1.1  explorer 	nextpkt:
   1329  1.1  explorer 		scp->rxstart++;
   1330  1.1  explorer 		if (scp->rxstart == SCA_NrxBUFS)
   1331  1.1  explorer 			scp->rxstart = 0;
   1332  1.1  explorer 	}
   1333  1.1  explorer 
   1334  1.1  explorer 	return 0;
   1335  1.1  explorer }
   1336  1.1  explorer 
   1337  1.1  explorer /*
   1338  1.1  explorer  * Pass the packet up to the kernel if it is a packet we want to pay
   1339  1.1  explorer  * attention to.
   1340  1.1  explorer  *
   1341  1.2   mycroft  * MUST BE CALLED AT splnet()
   1342  1.1  explorer  */
   1343  1.1  explorer static void
   1344  1.1  explorer sca_frame_process(sca_port_t *scp, sca_desc_t *desc, u_int8_t *p)
   1345  1.1  explorer {
   1346  1.1  explorer 	hdlc_header_t	*hdlc;
   1347  1.1  explorer 	cisco_pkt_t	*cisco, *ncisco;
   1348  1.1  explorer 	u_int16_t	 len;
   1349  1.1  explorer 	struct mbuf	*m;
   1350  1.1  explorer 	u_int8_t	*nbuf;
   1351  1.1  explorer 	u_int32_t	 t = (time.tv_sec - boottime.tv_sec) * 1000;
   1352  1.1  explorer 	struct ifqueue *ifq;
   1353  1.1  explorer 
   1354  1.1  explorer 	len = desc->len;
   1355  1.1  explorer 
   1356  1.1  explorer 	/*
   1357  1.1  explorer 	 * skip packets that are too short
   1358  1.1  explorer 	 */
   1359  1.1  explorer 	if (len < sizeof(hdlc_header_t))
   1360  1.1  explorer 		return;
   1361  1.1  explorer 
   1362  1.1  explorer #if NBPFILTER > 0
   1363  1.1  explorer 	if (scp->sp_bpf)
   1364  1.1  explorer 		bpf_tap(scp->sp_bpf, p, len);
   1365  1.1  explorer #endif
   1366  1.1  explorer 
   1367  1.1  explorer 	/*
   1368  1.1  explorer 	 * read and then strip off the HDLC information
   1369  1.1  explorer 	 */
   1370  1.1  explorer 	hdlc = (hdlc_header_t *)p;
   1371  1.1  explorer 
   1372  1.1  explorer 	scp->sp_if.if_ipackets++;
   1373  1.1  explorer 	scp->sp_if.if_lastchange = time;
   1374  1.1  explorer 
   1375  1.1  explorer 	switch (ntohs(hdlc->protocol)) {
   1376  1.1  explorer 	case HDLC_PROTOCOL_IP:
   1377  1.1  explorer 		SCA_DPRINTF(SCA_DEBUG_RX, ("Received IP packet\n"));
   1378  1.1  explorer 
   1379  1.1  explorer 		m = sca_mbuf_alloc(p, len);
   1380  1.1  explorer 		if (m == NULL) {
   1381  1.1  explorer 			scp->sp_if.if_iqdrops++;
   1382  1.1  explorer 			return;
   1383  1.1  explorer 		}
   1384  1.1  explorer 		m->m_pkthdr.rcvif = &scp->sp_if;
   1385  1.1  explorer 
   1386  1.1  explorer 		if (IF_QFULL(&ipintrq)) {
   1387  1.1  explorer 			IF_DROP(&ipintrq);
   1388  1.1  explorer 			scp->sp_if.if_ierrors++;
   1389  1.1  explorer 			scp->sp_if.if_iqdrops++;
   1390  1.1  explorer 			m_freem(m);
   1391  1.1  explorer 		} else {
   1392  1.1  explorer 			/*
   1393  1.1  explorer 			 * strip off the HDLC header and hand off to IP stack
   1394  1.1  explorer 			 */
   1395  1.1  explorer 			m->m_pkthdr.len -= HDLC_HDRLEN;
   1396  1.1  explorer 			m->m_data += HDLC_HDRLEN;
   1397  1.1  explorer 			m->m_len -= HDLC_HDRLEN;
   1398  1.1  explorer 			IF_ENQUEUE(&ipintrq, m);
   1399  1.1  explorer 			schednetisr(NETISR_IP);
   1400  1.1  explorer 		}
   1401  1.1  explorer 
   1402  1.1  explorer 		break;
   1403  1.1  explorer 
   1404  1.1  explorer 	case CISCO_KEEPALIVE:
   1405  1.1  explorer 		SCA_DPRINTF(SCA_DEBUG_CISCO,
   1406  1.1  explorer 			    ("Received CISCO keepalive packet\n"));
   1407  1.1  explorer 
   1408  1.1  explorer 		if (len < CISCO_PKT_LEN) {
   1409  1.1  explorer 			SCA_DPRINTF(SCA_DEBUG_CISCO,
   1410  1.1  explorer 				    ("short CISCO packet %d, wanted %d\n",
   1411  1.1  explorer 				     len, CISCO_PKT_LEN));
   1412  1.1  explorer 			return;
   1413  1.1  explorer 		}
   1414  1.1  explorer 
   1415  1.1  explorer 		/*
   1416  1.1  explorer 		 * allocate an mbuf and copy the important bits of data
   1417  1.1  explorer 		 * into it.
   1418  1.1  explorer 		 */
   1419  1.1  explorer 		m = sca_mbuf_alloc(p, HDLC_HDRLEN + CISCO_PKT_LEN);
   1420  1.1  explorer 		if (m == NULL)
   1421  1.1  explorer 			return;
   1422  1.1  explorer 
   1423  1.1  explorer 		nbuf = mtod(m, u_int8_t *);
   1424  1.1  explorer 		ncisco = (cisco_pkt_t *)(nbuf + HDLC_HDRLEN);
   1425  1.1  explorer 		m->m_pkthdr.rcvif = &scp->sp_if;
   1426  1.1  explorer 
   1427  1.1  explorer 		cisco = (cisco_pkt_t *)(p + HDLC_HDRLEN);
   1428  1.1  explorer 
   1429  1.1  explorer 		switch (ntohl(cisco->type)) {
   1430  1.1  explorer 		case CISCO_ADDR_REQ:
   1431  1.1  explorer 			printf("Got CISCO addr_req, ignoring\n");
   1432  1.1  explorer 			m_freem(m);
   1433  1.1  explorer 			break;
   1434  1.1  explorer 
   1435  1.1  explorer 		case CISCO_ADDR_REPLY:
   1436  1.1  explorer 			printf("Got CISCO addr_reply, ignoring\n");
   1437  1.1  explorer 			m_freem(m);
   1438  1.1  explorer 			break;
   1439  1.1  explorer 
   1440  1.1  explorer 		case CISCO_KEEPALIVE_REQ:
   1441  1.1  explorer 			SCA_DPRINTF(SCA_DEBUG_CISCO,
   1442  1.1  explorer 				    ("Received KA, mseq %d,"
   1443  1.1  explorer 				     " yseq %d, rel 0x%04x, t0"
   1444  1.1  explorer 				     " %04x, t1 %04x\n",
   1445  1.1  explorer 				     ntohl(cisco->par1), ntohl(cisco->par2),
   1446  1.1  explorer 				     ntohs(cisco->rel), ntohs(cisco->time0),
   1447  1.1  explorer 				     ntohs(cisco->time1)));
   1448  1.1  explorer 
   1449  1.1  explorer 			scp->cka_lastrx = ntohl(cisco->par1);
   1450  1.1  explorer 			scp->cka_lasttx++;
   1451  1.1  explorer 
   1452  1.1  explorer 			/*
   1453  1.1  explorer 			 * schedule the transmit right here.
   1454  1.1  explorer 			 */
   1455  1.1  explorer 			ncisco->par2 = cisco->par1;
   1456  1.1  explorer 			ncisco->par1 = htonl(scp->cka_lasttx);
   1457  1.1  explorer 			ncisco->time0 = htons((u_int16_t)(t >> 16));
   1458  1.1  explorer 			ncisco->time1 = htons((u_int16_t)(t & 0x0000ffff));
   1459  1.1  explorer 
   1460  1.1  explorer 			ifq = &scp->linkq;
   1461  1.1  explorer 			if (IF_QFULL(ifq)) {
   1462  1.1  explorer 				IF_DROP(ifq);
   1463  1.1  explorer 				m_freem(m);
   1464  1.1  explorer 				return;
   1465  1.1  explorer 			}
   1466  1.1  explorer 			IF_ENQUEUE(ifq, m);
   1467  1.1  explorer 
   1468  1.1  explorer 			sca_start(&scp->sp_if);
   1469  1.1  explorer 
   1470  1.1  explorer 			break;
   1471  1.1  explorer 
   1472  1.1  explorer 		default:
   1473  1.1  explorer 			m_freem(m);
   1474  1.1  explorer 			SCA_DPRINTF(SCA_DEBUG_CISCO,
   1475  1.1  explorer 				    ("Unknown CISCO keepalive protocol 0x%04x\n",
   1476  1.1  explorer 				     ntohl(cisco->type)));
   1477  1.1  explorer 			return;
   1478  1.1  explorer 		}
   1479  1.1  explorer 
   1480  1.1  explorer 		break;
   1481  1.1  explorer 
   1482  1.1  explorer 	default:
   1483  1.1  explorer 		SCA_DPRINTF(SCA_DEBUG_RX,
   1484  1.1  explorer 			    ("Unknown/unexpected ethertype 0x%04x\n",
   1485  1.1  explorer 			     ntohs(hdlc->protocol)));
   1486  1.1  explorer 	}
   1487  1.1  explorer }
   1488  1.1  explorer 
   1489  1.1  explorer #if SCA_DEBUG_LEVEL > 0
   1490  1.1  explorer /*
   1491  1.1  explorer  * do a hex dump of the packet received into descriptor "desc" with
   1492  1.1  explorer  * data buffer "p"
   1493  1.1  explorer  */
   1494  1.1  explorer static void
   1495  1.1  explorer sca_frame_print(sca_port_t *scp, sca_desc_t *desc, u_int8_t *p)
   1496  1.1  explorer {
   1497  1.1  explorer 	int i;
   1498  1.1  explorer 	int nothing_yet = 1;
   1499  1.1  explorer 
   1500  1.1  explorer 	printf("descriptor va %p: cp 0x%x bpb 0x%0x bp 0x%0x stat 0x%0x len %d\n",
   1501  1.1  explorer 	       desc, desc->cp, desc->bpb, desc->bp, desc->stat, desc->len);
   1502  1.1  explorer 
   1503  1.1  explorer 	for (i = 0 ; i < desc->len ; i++) {
   1504  1.1  explorer 		if (nothing_yet == 1 && *p == 0) {
   1505  1.1  explorer 			p++;
   1506  1.1  explorer 			continue;
   1507  1.1  explorer 		}
   1508  1.1  explorer 		nothing_yet = 0;
   1509  1.1  explorer 		if (i % 16 == 0)
   1510  1.1  explorer 			printf("\n");
   1511  1.1  explorer 		printf("%02x ", *p++);
   1512  1.1  explorer 	}
   1513  1.1  explorer 
   1514  1.1  explorer 	if (i % 16 != 1)
   1515  1.1  explorer 		printf("\n");
   1516  1.1  explorer }
   1517  1.1  explorer #endif
   1518  1.1  explorer 
   1519  1.1  explorer /*
   1520  1.1  explorer  * skip all frames before the descriptor index "indx" -- we do this by
   1521  1.1  explorer  * moving the rxstart pointer to the index following this one, and
   1522  1.1  explorer  * setting the end descriptor to this index.
   1523  1.1  explorer  */
   1524  1.1  explorer static void
   1525  1.1  explorer sca_frame_skip(sca_port_t *scp, int indx)
   1526  1.1  explorer {
   1527  1.1  explorer 	u_int32_t	desc_p;
   1528  1.1  explorer 
   1529  1.1  explorer 	scp->rxstart++;
   1530  1.1  explorer 	if (scp->rxstart == SCA_NrxBUFS)
   1531  1.1  explorer 		scp->rxstart = 0;
   1532  1.1  explorer 
   1533  1.1  explorer 	desc_p = scp->rxdesc_p * sizeof(sca_desc_t) * indx;
   1534  1.1  explorer 	dmac_write_2(scp, SCA_EDAL0,
   1535  1.1  explorer 		     (u_int16_t)(desc_p & 0x0000ffff));
   1536  1.1  explorer }
   1537  1.1  explorer 
   1538  1.1  explorer /*
   1539  1.1  explorer  * set a port to the "up" state
   1540  1.1  explorer  */
   1541  1.1  explorer static void
   1542  1.1  explorer sca_port_up(sca_port_t *scp)
   1543  1.1  explorer {
   1544  1.1  explorer 	struct sca_softc *sc = scp->sca;
   1545  1.1  explorer 
   1546  1.1  explorer 	/*
   1547  1.1  explorer 	 * reset things
   1548  1.1  explorer 	 */
   1549  1.1  explorer #if 0
   1550  1.1  explorer 	msci_write_1(scp, SCA_CMD0, SCA_CMD_TXRESET);
   1551  1.1  explorer 	msci_write_1(scp, SCA_CMD0, SCA_CMD_RXRESET);
   1552  1.1  explorer #endif
   1553  1.1  explorer 	/*
   1554  1.1  explorer 	 * clear in-use flag
   1555  1.1  explorer 	 */
   1556  1.1  explorer 	scp->sp_if.if_flags &= ~IFF_OACTIVE;
   1557  1.1  explorer 
   1558  1.1  explorer 	/*
   1559  1.1  explorer 	 * raise DTR
   1560  1.1  explorer 	 */
   1561  1.1  explorer 	sc->dtr_callback(sc->dtr_aux, scp->sp_port, 1);
   1562  1.1  explorer 
   1563  1.1  explorer 	/*
   1564  1.1  explorer 	 * raise RTS
   1565  1.1  explorer 	 */
   1566  1.1  explorer 	msci_write_1(scp, SCA_CTL0,
   1567  1.1  explorer 		     msci_read_1(scp, SCA_CTL0) & ~SCA_CTL_RTS);
   1568  1.1  explorer 
   1569  1.1  explorer 	/*
   1570  1.1  explorer 	 * enable interrupts
   1571  1.1  explorer 	 */
   1572  1.1  explorer 	if (scp->sp_port == 0) {
   1573  1.1  explorer 		sca_write_1(sc, SCA_IER0, sca_read_1(sc, SCA_IER0) | 0x0f);
   1574  1.1  explorer 		sca_write_1(sc, SCA_IER1, sca_read_1(sc, SCA_IER1) | 0x0f);
   1575  1.1  explorer 	} else {
   1576  1.1  explorer 		sca_write_1(sc, SCA_IER0, sca_read_1(sc, SCA_IER0) | 0xf0);
   1577  1.1  explorer 		sca_write_1(sc, SCA_IER1, sca_read_1(sc, SCA_IER1) | 0xf0);
   1578  1.1  explorer 	}
   1579  1.1  explorer 
   1580  1.1  explorer 	/*
   1581  1.1  explorer 	 * enable transmit and receive
   1582  1.1  explorer 	 */
   1583  1.1  explorer 	msci_write_1(scp, SCA_CMD0, SCA_CMD_TXENABLE);
   1584  1.1  explorer 	msci_write_1(scp, SCA_CMD0, SCA_CMD_RXENABLE);
   1585  1.1  explorer 
   1586  1.1  explorer 	/*
   1587  1.1  explorer 	 * reset internal state
   1588  1.1  explorer 	 */
   1589  1.1  explorer 	scp->txinuse = 0;
   1590  1.1  explorer 	scp->txcur = 0;
   1591  1.1  explorer 	scp->cka_lasttx = time.tv_usec;
   1592  1.1  explorer 	scp->cka_lastrx = 0;
   1593  1.1  explorer }
   1594  1.1  explorer 
   1595  1.1  explorer /*
   1596  1.1  explorer  * set a port to the "down" state
   1597  1.1  explorer  */
   1598  1.1  explorer static void
   1599  1.1  explorer sca_port_down(sca_port_t *scp)
   1600  1.1  explorer {
   1601  1.1  explorer 	struct sca_softc *sc = scp->sca;
   1602  1.1  explorer 
   1603  1.1  explorer 	/*
   1604  1.1  explorer 	 * lower DTR
   1605  1.1  explorer 	 */
   1606  1.1  explorer 	sc->dtr_callback(sc->dtr_aux, scp->sp_port, 0);
   1607  1.1  explorer 
   1608  1.1  explorer 	/*
   1609  1.1  explorer 	 * lower RTS
   1610  1.1  explorer 	 */
   1611  1.1  explorer 	msci_write_1(scp, SCA_CTL0,
   1612  1.1  explorer 		     msci_read_1(scp, SCA_CTL0) | SCA_CTL_RTS);
   1613  1.1  explorer 
   1614  1.1  explorer 	/*
   1615  1.1  explorer 	 * disable interrupts
   1616  1.1  explorer 	 */
   1617  1.1  explorer 	if (scp->sp_port == 0) {
   1618  1.1  explorer 		sca_write_1(sc, SCA_IER0, sca_read_1(sc, SCA_IER0) & 0xf0);
   1619  1.1  explorer 		sca_write_1(sc, SCA_IER1, sca_read_1(sc, SCA_IER1) & 0xf0);
   1620  1.1  explorer 	} else {
   1621  1.1  explorer 		sca_write_1(sc, SCA_IER0, sca_read_1(sc, SCA_IER0) & 0x0f);
   1622  1.1  explorer 		sca_write_1(sc, SCA_IER1, sca_read_1(sc, SCA_IER1) & 0x0f);
   1623  1.1  explorer 	}
   1624  1.1  explorer 
   1625  1.1  explorer 	/*
   1626  1.1  explorer 	 * disable transmit and receive
   1627  1.1  explorer 	 */
   1628  1.1  explorer 	msci_write_1(scp, SCA_CMD0, SCA_CMD_RXDISABLE);
   1629  1.1  explorer 	msci_write_1(scp, SCA_CMD0, SCA_CMD_TXDISABLE);
   1630  1.1  explorer 
   1631  1.1  explorer 	/*
   1632  1.1  explorer 	 * no, we're not in use anymore
   1633  1.1  explorer 	 */
   1634  1.1  explorer 	scp->sp_if.if_flags &= ~IFF_OACTIVE;
   1635  1.1  explorer }
   1636  1.1  explorer 
   1637  1.1  explorer /*
   1638  1.1  explorer  * disable all DMA and interrupts for all ports at once.
   1639  1.1  explorer  */
   1640  1.1  explorer void
   1641  1.1  explorer sca_shutdown(struct sca_softc *sca)
   1642  1.1  explorer {
   1643  1.1  explorer 	/*
   1644  1.1  explorer 	 * disable DMA and interrupts
   1645  1.1  explorer 	 */
   1646  1.1  explorer 	sca_write_1(sca, SCA_DMER, 0);
   1647  1.1  explorer 	sca_write_1(sca, SCA_IER0, 0);
   1648  1.1  explorer 	sca_write_1(sca, SCA_IER1, 0);
   1649  1.1  explorer }
   1650  1.1  explorer 
   1651  1.1  explorer /*
   1652  1.1  explorer  * If there are packets to transmit, start the transmit DMA logic.
   1653  1.1  explorer  */
   1654  1.1  explorer static void
   1655  1.1  explorer sca_port_starttx(sca_port_t *scp)
   1656  1.1  explorer {
   1657  1.1  explorer 	struct sca_softc *sc;
   1658  1.1  explorer 	u_int32_t	startdesc_p, enddesc_p;
   1659  1.1  explorer 	int enddesc;
   1660  1.1  explorer 
   1661  1.1  explorer 	sc = scp->sca;
   1662  1.1  explorer 
   1663  1.1  explorer 	if (((scp->sp_if.if_flags & IFF_OACTIVE) == IFF_OACTIVE)
   1664  1.1  explorer 	    || scp->txinuse == 0)
   1665  1.1  explorer 		return;
   1666  1.1  explorer 	scp->sp_if.if_flags |= IFF_OACTIVE;
   1667  1.1  explorer 
   1668  1.1  explorer 	/*
   1669  1.1  explorer 	 * We have something to do, since we have at least one packet
   1670  1.1  explorer 	 * waiting, and we are not already marked as active.
   1671  1.1  explorer 	 */
   1672  1.1  explorer 	enddesc = scp->txcur;
   1673  1.1  explorer 	enddesc++;
   1674  1.1  explorer 	if (enddesc == SCA_NtxBUFS)
   1675  1.1  explorer 		enddesc = 0;
   1676  1.1  explorer 
   1677  1.1  explorer 	startdesc_p = scp->txdesc_p;
   1678  1.1  explorer 	enddesc_p = scp->txdesc_p + sizeof(sca_desc_t) * enddesc;
   1679  1.1  explorer 
   1680  1.1  explorer 	dmac_write_2(scp, SCA_EDAL1, (u_int16_t)(enddesc_p & 0x0000ffff));
   1681  1.1  explorer 	dmac_write_2(scp, SCA_CDAL1,
   1682  1.1  explorer 		     (u_int16_t)(startdesc_p & 0x0000ffff));
   1683  1.1  explorer 
   1684  1.1  explorer 	/*
   1685  1.1  explorer 	 * enable the DMA
   1686  1.1  explorer 	 */
   1687  1.1  explorer 	dmac_write_1(scp, SCA_DSR1, SCA_DSR_DE);
   1688  1.1  explorer }
   1689  1.1  explorer 
   1690  1.1  explorer /*
   1691  1.1  explorer  * allocate an mbuf at least long enough to hold "len" bytes.
   1692  1.1  explorer  * If "p" is non-NULL, copy "len" bytes from it into the new mbuf,
   1693  1.1  explorer  * otherwise let the caller handle copying the data in.
   1694  1.1  explorer  */
   1695  1.1  explorer static struct mbuf *
   1696  1.1  explorer sca_mbuf_alloc(caddr_t p, u_int len)
   1697  1.1  explorer {
   1698  1.1  explorer 	struct mbuf *m;
   1699  1.1  explorer 
   1700  1.1  explorer 	/*
   1701  1.1  explorer 	 * allocate an mbuf and copy the important bits of data
   1702  1.1  explorer 	 * into it.  If the packet won't fit in the header,
   1703  1.1  explorer 	 * allocate a cluster for it and store it there.
   1704  1.1  explorer 	 */
   1705  1.1  explorer 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1706  1.1  explorer 	if (m == NULL)
   1707  1.1  explorer 		return NULL;
   1708  1.1  explorer 	if (len > MHLEN) {
   1709  1.1  explorer 		if (len > MCLBYTES) {
   1710  1.1  explorer 			m_freem(m);
   1711  1.1  explorer 			return NULL;
   1712  1.1  explorer 		}
   1713  1.1  explorer 		MCLGET(m, M_DONTWAIT);
   1714  1.1  explorer 		if ((m->m_flags & M_EXT) == 0) {
   1715  1.1  explorer 			m_freem(m);
   1716  1.1  explorer 			return NULL;
   1717  1.1  explorer 		}
   1718  1.1  explorer 	}
   1719  1.1  explorer 	if (p != NULL)
   1720  1.1  explorer 		bcopy(p, mtod(m, caddr_t), len);
   1721  1.1  explorer 	m->m_len = len;
   1722  1.1  explorer 	m->m_pkthdr.len = len;
   1723  1.1  explorer 
   1724  1.1  explorer 	return (m);
   1725  1.1  explorer }
   1726