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