Home | History | Annotate | Line # | Download | only in qbus
if_dmc.c revision 1.9.4.2
      1  1.9.4.2     yamt /*	$NetBSD: if_dmc.c,v 1.9.4.2 2007/09/03 14:38:10 yamt Exp $	*/
      2      1.1    ragge /*
      3      1.1    ragge  * Copyright (c) 1982, 1986 Regents of the University of California.
      4      1.1    ragge  * All rights reserved.
      5      1.1    ragge  *
      6      1.1    ragge  * Redistribution and use in source and binary forms, with or without
      7      1.1    ragge  * modification, are permitted provided that the following conditions
      8      1.1    ragge  * are met:
      9      1.1    ragge  * 1. Redistributions of source code must retain the above copyright
     10      1.1    ragge  *    notice, this list of conditions and the following disclaimer.
     11      1.1    ragge  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1    ragge  *    notice, this list of conditions and the following disclaimer in the
     13      1.1    ragge  *    documentation and/or other materials provided with the distribution.
     14      1.7      agc  * 3. Neither the name of the University nor the names of its contributors
     15      1.1    ragge  *    may be used to endorse or promote products derived from this software
     16      1.1    ragge  *    without specific prior written permission.
     17      1.1    ragge  *
     18      1.1    ragge  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     19      1.1    ragge  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20      1.1    ragge  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21      1.1    ragge  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     22      1.1    ragge  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23      1.1    ragge  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24      1.1    ragge  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25      1.1    ragge  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26      1.1    ragge  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27      1.1    ragge  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28      1.1    ragge  * SUCH DAMAGE.
     29      1.1    ragge  *
     30      1.1    ragge  *	@(#)if_dmc.c	7.10 (Berkeley) 12/16/90
     31      1.1    ragge  */
     32      1.1    ragge 
     33      1.1    ragge /*
     34      1.1    ragge  * DMC11 device driver, internet version
     35      1.1    ragge  *
     36      1.1    ragge  *	Bill Nesheim
     37      1.1    ragge  *	Cornell University
     38      1.1    ragge  *
     39      1.1    ragge  *	Lou Salkind
     40      1.1    ragge  *	New York University
     41      1.1    ragge  */
     42      1.2    lukem 
     43      1.2    lukem #include <sys/cdefs.h>
     44  1.9.4.2     yamt __KERNEL_RCSID(0, "$NetBSD: if_dmc.c,v 1.9.4.2 2007/09/03 14:38:10 yamt Exp $");
     45      1.1    ragge 
     46      1.1    ragge #undef DMCDEBUG	/* for base table dump on fatal error */
     47      1.1    ragge 
     48      1.1    ragge #include "opt_inet.h"
     49      1.1    ragge 
     50      1.1    ragge #include <sys/param.h>
     51      1.1    ragge #include <sys/systm.h>
     52      1.1    ragge #include <sys/mbuf.h>
     53      1.1    ragge #include <sys/ioctl.h>
     54      1.1    ragge #include <sys/socket.h>
     55      1.1    ragge #include <sys/syslog.h>
     56      1.1    ragge #include <sys/device.h>
     57      1.1    ragge 
     58      1.1    ragge #include <net/if.h>
     59      1.1    ragge #include <net/netisr.h>
     60      1.1    ragge 
     61      1.1    ragge #ifdef	INET
     62      1.1    ragge #include <netinet/in.h>
     63      1.1    ragge #include <netinet/in_var.h>
     64      1.1    ragge #endif
     65      1.1    ragge 
     66      1.1    ragge #include <machine/bus.h>
     67      1.1    ragge 
     68      1.1    ragge #include <dev/qbus/ubareg.h>
     69      1.1    ragge #include <dev/qbus/ubavar.h>
     70      1.1    ragge #include <dev/qbus/if_uba.h>
     71      1.1    ragge 
     72      1.1    ragge #include <dev/qbus/if_dmcreg.h>
     73      1.1    ragge 
     74      1.1    ragge 
     75      1.1    ragge /*
     76      1.1    ragge  * output timeout value, sec.; should depend on line speed.
     77      1.1    ragge  */
     78      1.1    ragge static int dmc_timeout = 20;
     79      1.1    ragge 
     80      1.1    ragge #define NRCV 7
     81      1.9   simonb #define NXMT 3
     82      1.1    ragge #define NCMDS	(NRCV+NXMT+4)	/* size of command queue */
     83      1.1    ragge 
     84      1.1    ragge #define DMC_WBYTE(csr, val) \
     85      1.1    ragge 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, csr, val)
     86      1.1    ragge #define DMC_WWORD(csr, val) \
     87      1.1    ragge 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
     88      1.1    ragge #define DMC_RBYTE(csr) \
     89      1.1    ragge 	bus_space_read_1(sc->sc_iot, sc->sc_ioh, csr)
     90      1.1    ragge #define DMC_RWORD(csr) \
     91      1.1    ragge 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
     92      1.1    ragge 
     93      1.1    ragge 
     94      1.1    ragge #ifdef DMCDEBUG
     95      1.1    ragge #define printd if(dmcdebug)printf
     96      1.1    ragge int dmcdebug = 0;
     97      1.1    ragge #endif
     98      1.1    ragge 
     99      1.1    ragge /* error reporting intervals */
    100      1.1    ragge #define DMC_RPNBFS	50
    101      1.1    ragge #define DMC_RPDSC	1
    102      1.1    ragge #define DMC_RPTMO	10
    103      1.1    ragge #define DMC_RPDCK	10
    104      1.1    ragge 
    105      1.1    ragge struct  dmc_command {
    106      1.1    ragge 	char	qp_cmd;		/* command */
    107      1.1    ragge 	short	qp_ubaddr;	/* buffer address */
    108      1.1    ragge 	short	qp_cc;		/* character count || XMEM */
    109      1.1    ragge 	struct	dmc_command *qp_next;	/* next command on queue */
    110      1.1    ragge };
    111      1.1    ragge 
    112      1.1    ragge struct dmcbufs {
    113      1.1    ragge 	int	ubinfo;		/* from uballoc */
    114      1.1    ragge 	short	cc;		/* buffer size */
    115      1.1    ragge 	short	flags;		/* access control */
    116      1.1    ragge };
    117      1.1    ragge #define	DBUF_OURS	0	/* buffer is available */
    118      1.1    ragge #define	DBUF_DMCS	1	/* buffer claimed by somebody */
    119      1.1    ragge #define	DBUF_XMIT	4	/* transmit buffer */
    120      1.1    ragge #define	DBUF_RCV	8	/* receive buffer */
    121      1.1    ragge 
    122      1.1    ragge 
    123      1.1    ragge /*
    124      1.1    ragge  * DMC software status per interface.
    125      1.1    ragge  *
    126      1.1    ragge  * Each interface is referenced by a network interface structure,
    127      1.1    ragge  * sc_if, which the routing code uses to locate the interface.
    128      1.1    ragge  * This structure contains the output queue for the interface, its address, ...
    129      1.1    ragge  * We also have, for each interface, a  set of 7 UBA interface structures
    130      1.1    ragge  * for each, which
    131      1.1    ragge  * contain information about the UNIBUS resources held by the interface:
    132      1.1    ragge  * map registers, buffered data paths, etc.  Information is cached in this
    133      1.1    ragge  * structure for use by the if_uba.c routines in running the interface
    134      1.1    ragge  * efficiently.
    135      1.1    ragge  */
    136      1.1    ragge struct dmc_softc {
    137      1.1    ragge 	struct	device sc_dev;		/* Configuration common part */
    138      1.1    ragge 	struct	ifnet sc_if;		/* network-visible interface */
    139      1.1    ragge 	short	sc_oused;		/* output buffers currently in use */
    140      1.1    ragge 	short	sc_iused;		/* input buffers given to DMC */
    141      1.1    ragge 	short	sc_flag;		/* flags */
    142      1.1    ragge 	struct	ubinfo sc_ui;		/* UBA mapping info for base table */
    143      1.1    ragge 	int	sc_errors[4];		/* non-fatal error counters */
    144      1.1    ragge 	bus_space_tag_t sc_iot;
    145      1.1    ragge 	bus_addr_t sc_ioh;
    146      1.1    ragge 	bus_dma_tag_t sc_dmat;
    147      1.1    ragge 	struct	evcnt sc_rintrcnt;	/* Interrupt counting */
    148      1.1    ragge 	struct	evcnt sc_tintrcnt;	/* Interrupt counting */
    149      1.1    ragge #define sc_datck sc_errors[0]
    150      1.1    ragge #define sc_timeo sc_errors[1]
    151      1.1    ragge #define sc_nobuf sc_errors[2]
    152      1.1    ragge #define sc_disc  sc_errors[3]
    153      1.1    ragge 	struct	dmcbufs sc_rbufs[NRCV];	/* receive buffer info */
    154      1.1    ragge 	struct	dmcbufs sc_xbufs[NXMT];	/* transmit buffer info */
    155      1.1    ragge 	struct	ifubinfo sc_ifuba;	/* UNIBUS resources */
    156      1.1    ragge 	struct	ifrw sc_ifr[NRCV];	/* UNIBUS receive buffer maps */
    157      1.1    ragge 	struct	ifxmt sc_ifw[NXMT];	/* UNIBUS receive buffer maps */
    158      1.1    ragge 	/* command queue stuff */
    159      1.1    ragge 	struct	dmc_command sc_cmdbuf[NCMDS];
    160      1.1    ragge 	struct	dmc_command *sc_qhead;	/* head of command queue */
    161      1.1    ragge 	struct	dmc_command *sc_qtail;	/* tail of command queue */
    162      1.1    ragge 	struct	dmc_command *sc_qactive;	/* command in progress */
    163      1.1    ragge 	struct	dmc_command *sc_qfreeh;	/* head of list of free cmd buffers */
    164      1.1    ragge 	struct	dmc_command *sc_qfreet;	/* tail of list of free cmd buffers */
    165      1.1    ragge 	/* end command queue stuff */
    166      1.1    ragge 	struct dmc_base {
    167      1.1    ragge 		short	d_base[128];		/* DMC base table */
    168      1.1    ragge 	} dmc_base;
    169      1.1    ragge };
    170      1.1    ragge 
    171      1.1    ragge static  int dmcmatch(struct device *, struct cfdata *, void *);
    172      1.1    ragge static  void dmcattach(struct device *, struct device *, void *);
    173      1.1    ragge static  int dmcinit(struct ifnet *);
    174      1.1    ragge static  void dmcrint(void *);
    175      1.1    ragge static  void dmcxint(void *);
    176      1.1    ragge static  void dmcdown(struct dmc_softc *sc);
    177      1.1    ragge static  void dmcrestart(struct dmc_softc *);
    178      1.1    ragge static  void dmcload(struct dmc_softc *, int, u_short, u_short);
    179      1.1    ragge static  void dmcstart(struct ifnet *);
    180      1.1    ragge static  void dmctimeout(struct ifnet *);
    181  1.9.4.2     yamt static  int dmcioctl(struct ifnet *, u_long, void *);
    182      1.1    ragge static  int dmcoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
    183      1.1    ragge 	struct rtentry *);
    184      1.1    ragge static  void dmcreset(struct device *);
    185      1.1    ragge 
    186      1.5  thorpej CFATTACH_DECL(dmc, sizeof(struct dmc_softc),
    187      1.6  thorpej     dmcmatch, dmcattach, NULL, NULL);
    188      1.1    ragge 
    189      1.1    ragge /* flags */
    190      1.1    ragge #define DMC_RUNNING	0x01		/* device initialized */
    191      1.1    ragge #define DMC_BMAPPED	0x02		/* base table mapped */
    192      1.1    ragge #define DMC_RESTART	0x04		/* software restart in progress */
    193      1.1    ragge #define DMC_ONLINE	0x08		/* device running (had a RDYO) */
    194      1.1    ragge 
    195      1.1    ragge 
    196      1.1    ragge /* queue manipulation macros */
    197      1.1    ragge #define	QUEUE_AT_HEAD(qp, head, tail) \
    198      1.1    ragge 	(qp)->qp_next = (head); \
    199      1.1    ragge 	(head) = (qp); \
    200      1.1    ragge 	if ((tail) == (struct dmc_command *) 0) \
    201      1.9   simonb 		(tail) = (head)
    202      1.1    ragge 
    203      1.1    ragge #define QUEUE_AT_TAIL(qp, head, tail) \
    204      1.1    ragge 	if ((tail)) \
    205      1.1    ragge 		(tail)->qp_next = (qp); \
    206      1.1    ragge 	else \
    207      1.1    ragge 		(head) = (qp); \
    208      1.1    ragge 	(qp)->qp_next = (struct dmc_command *) 0; \
    209      1.1    ragge 	(tail) = (qp)
    210      1.1    ragge 
    211      1.1    ragge #define DEQUEUE(head, tail) \
    212      1.1    ragge 	(head) = (head)->qp_next;\
    213      1.1    ragge 	if ((head) == (struct dmc_command *) 0)\
    214      1.1    ragge 		(tail) = (head)
    215      1.1    ragge 
    216      1.1    ragge int
    217      1.1    ragge dmcmatch(struct device *parent, struct cfdata *cf, void *aux)
    218      1.1    ragge {
    219      1.1    ragge 	struct uba_attach_args *ua = aux;
    220      1.1    ragge 	struct dmc_softc ssc;
    221      1.1    ragge 	struct dmc_softc *sc = &ssc;
    222      1.1    ragge 	int i;
    223      1.1    ragge 
    224      1.1    ragge 	sc->sc_iot = ua->ua_iot;
    225      1.1    ragge 	sc->sc_ioh = ua->ua_ioh;
    226      1.1    ragge 
    227      1.1    ragge 	DMC_WBYTE(DMC_BSEL1, DMC_MCLR);
    228      1.1    ragge 	for (i = 100000; i && (DMC_RBYTE(DMC_BSEL1) & DMC_RUN) == 0; i--)
    229      1.1    ragge 		;
    230      1.1    ragge 	if ((DMC_RBYTE(DMC_BSEL1) & DMC_RUN) == 0) {
    231      1.1    ragge 		printf("dmcprobe: can't start device\n" );
    232      1.1    ragge 		return (0);
    233      1.1    ragge 	}
    234      1.1    ragge 	DMC_WBYTE(DMC_BSEL0, DMC_RQI|DMC_IEI);
    235      1.1    ragge 	/* let's be paranoid */
    236      1.1    ragge 	DMC_WBYTE(DMC_BSEL0, DMC_RBYTE(DMC_BSEL0) | DMC_RQI|DMC_IEI);
    237      1.1    ragge 	DELAY(1000000);
    238      1.1    ragge 	DMC_WBYTE(DMC_BSEL1, DMC_MCLR);
    239      1.1    ragge 	for (i = 100000; i && (DMC_RBYTE(DMC_BSEL1) & DMC_RUN) == 0; i--)
    240      1.1    ragge 		;
    241      1.1    ragge 	return (1);
    242      1.1    ragge }
    243      1.1    ragge 
    244      1.1    ragge /*
    245      1.1    ragge  * Interface exists: make available by filling in network interface
    246      1.1    ragge  * record.  System will initialize the interface when it is ready
    247      1.1    ragge  * to accept packets.
    248      1.1    ragge  */
    249      1.1    ragge void
    250      1.1    ragge dmcattach(struct device *parent, struct device *self, void *aux)
    251      1.1    ragge {
    252      1.1    ragge 	struct uba_attach_args *ua = aux;
    253  1.9.4.1     yamt 	struct dmc_softc *sc = device_private(self);
    254      1.1    ragge 
    255      1.1    ragge 	sc->sc_iot = ua->ua_iot;
    256      1.1    ragge 	sc->sc_ioh = ua->ua_ioh;
    257      1.1    ragge 	sc->sc_dmat = ua->ua_dmat;
    258      1.1    ragge 
    259      1.1    ragge 	strcpy(sc->sc_if.if_xname, sc->sc_dev.dv_xname);
    260      1.1    ragge 	sc->sc_if.if_mtu = DMCMTU;
    261      1.1    ragge 	sc->sc_if.if_init = dmcinit;
    262      1.1    ragge 	sc->sc_if.if_output = dmcoutput;
    263      1.1    ragge 	sc->sc_if.if_ioctl = dmcioctl;
    264      1.1    ragge 	sc->sc_if.if_watchdog = dmctimeout;
    265      1.1    ragge 	sc->sc_if.if_flags = IFF_POINTOPOINT;
    266      1.1    ragge 	sc->sc_if.if_softc = sc;
    267      1.3   itojun 	IFQ_SET_READY(&sc->sc_if.if_snd);
    268      1.1    ragge 
    269      1.1    ragge 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec, dmcrint, sc,
    270      1.1    ragge 	    &sc->sc_rintrcnt);
    271      1.1    ragge 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec+4, dmcxint, sc,
    272      1.1    ragge 	    &sc->sc_tintrcnt);
    273      1.1    ragge 	uba_reset_establish(dmcreset, &sc->sc_dev);
    274      1.1    ragge 	evcnt_attach_dynamic(&sc->sc_rintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
    275      1.1    ragge 	    sc->sc_dev.dv_xname, "intr");
    276      1.1    ragge 	evcnt_attach_dynamic(&sc->sc_tintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
    277      1.1    ragge 	    sc->sc_dev.dv_xname, "intr");
    278      1.1    ragge 
    279      1.1    ragge 	if_attach(&sc->sc_if);
    280      1.1    ragge }
    281      1.1    ragge 
    282      1.1    ragge /*
    283      1.1    ragge  * Reset of interface after UNIBUS reset.
    284      1.1    ragge  * If interface is on specified UBA, reset its state.
    285      1.1    ragge  */
    286      1.1    ragge void
    287      1.1    ragge dmcreset(struct device *dev)
    288      1.1    ragge {
    289      1.1    ragge 	struct dmc_softc *sc = (struct dmc_softc *)dev;
    290      1.1    ragge 
    291      1.1    ragge 	sc->sc_flag = 0;
    292      1.1    ragge 	sc->sc_if.if_flags &= ~IFF_RUNNING;
    293      1.1    ragge 	dmcinit(&sc->sc_if);
    294      1.1    ragge }
    295      1.1    ragge 
    296      1.1    ragge /*
    297      1.1    ragge  * Initialization of interface; reinitialize UNIBUS usage.
    298      1.1    ragge  */
    299      1.1    ragge int
    300      1.1    ragge dmcinit(struct ifnet *ifp)
    301      1.1    ragge {
    302      1.1    ragge 	struct dmc_softc *sc = ifp->if_softc;
    303      1.1    ragge 	struct ifrw *ifrw;
    304      1.1    ragge 	struct ifxmt *ifxp;
    305      1.1    ragge 	struct dmcbufs *rp;
    306      1.1    ragge 	struct dmc_command *qp;
    307      1.1    ragge 	struct ifaddr *ifa;
    308  1.9.4.1     yamt 	struct cfdata *ui = device_cfdata(&sc->sc_dev);
    309      1.1    ragge 	int base;
    310      1.1    ragge 	int s;
    311      1.1    ragge 
    312      1.1    ragge 	/*
    313      1.1    ragge 	 * Check to see that an address has been set
    314      1.1    ragge 	 * (both local and destination for an address family).
    315      1.1    ragge 	 */
    316      1.8     matt 	IFADDR_FOREACH(ifa, ifp)
    317      1.1    ragge 		if (ifa->ifa_addr->sa_family && ifa->ifa_dstaddr->sa_family)
    318      1.1    ragge 			break;
    319      1.1    ragge 	if (ifa == (struct ifaddr *) 0)
    320      1.1    ragge 		return 0;
    321      1.1    ragge 
    322      1.1    ragge 	if ((DMC_RBYTE(DMC_BSEL1) & DMC_RUN) == 0) {
    323      1.1    ragge 		printf("dmcinit: DMC not running\n");
    324      1.1    ragge 		ifp->if_flags &= ~IFF_UP;
    325      1.1    ragge 		return 0;
    326      1.1    ragge 	}
    327      1.1    ragge 	/* map base table */
    328      1.1    ragge 	if ((sc->sc_flag & DMC_BMAPPED) == 0) {
    329      1.1    ragge 		sc->sc_ui.ui_size = sizeof(struct dmc_base);
    330  1.9.4.2     yamt 		sc->sc_ui.ui_vaddr = (void *)&sc->dmc_base;
    331  1.9.4.1     yamt 		uballoc((void *)device_parent(&sc->sc_dev), &sc->sc_ui, 0);
    332      1.1    ragge 		sc->sc_flag |= DMC_BMAPPED;
    333      1.1    ragge 	}
    334      1.1    ragge 	/* initialize UNIBUS resources */
    335      1.1    ragge 	sc->sc_iused = sc->sc_oused = 0;
    336      1.1    ragge 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    337  1.9.4.1     yamt 		if (if_ubaminit(&sc->sc_ifuba,
    338  1.9.4.1     yamt 		    (void *)device_parent(&sc->sc_dev),
    339      1.1    ragge 		    sizeof(struct dmc_header) + DMCMTU,
    340      1.1    ragge 		    sc->sc_ifr, NRCV, sc->sc_ifw, NXMT) == 0) {
    341      1.1    ragge 			printf("%s: can't allocate uba resources\n",
    342      1.1    ragge 			    sc->sc_dev.dv_xname);
    343      1.1    ragge 			ifp->if_flags &= ~IFF_UP;
    344      1.1    ragge 			return 0;
    345      1.1    ragge 		}
    346      1.1    ragge 		ifp->if_flags |= IFF_RUNNING;
    347      1.1    ragge 	}
    348      1.1    ragge 	sc->sc_flag &= ~DMC_ONLINE;
    349      1.1    ragge 	sc->sc_flag |= DMC_RUNNING;
    350      1.1    ragge 	/*
    351      1.1    ragge 	 * Limit packets enqueued until we see if we're on the air.
    352      1.1    ragge 	 */
    353      1.1    ragge 	ifp->if_snd.ifq_maxlen = 3;
    354      1.1    ragge 
    355      1.1    ragge 	/* initialize buffer pool */
    356      1.1    ragge 	/* receives */
    357      1.1    ragge 	ifrw = &sc->sc_ifr[0];
    358      1.1    ragge 	for (rp = &sc->sc_rbufs[0]; rp < &sc->sc_rbufs[NRCV]; rp++) {
    359      1.1    ragge 		rp->ubinfo = ifrw->ifrw_info;
    360      1.1    ragge 		rp->cc = DMCMTU + sizeof (struct dmc_header);
    361      1.1    ragge 		rp->flags = DBUF_OURS|DBUF_RCV;
    362      1.9   simonb 		ifrw++;
    363      1.1    ragge 	}
    364      1.1    ragge 	/* transmits */
    365      1.1    ragge 	ifxp = &sc->sc_ifw[0];
    366      1.1    ragge 	for (rp = &sc->sc_xbufs[0]; rp < &sc->sc_xbufs[NXMT]; rp++) {
    367      1.1    ragge 		rp->ubinfo = ifxp->ifw_info;
    368      1.1    ragge 		rp->cc = 0;
    369      1.1    ragge 		rp->flags = DBUF_OURS|DBUF_XMIT;
    370      1.9   simonb 		ifxp++;
    371      1.1    ragge 	}
    372      1.1    ragge 
    373      1.1    ragge 	/* set up command queues */
    374      1.1    ragge 	sc->sc_qfreeh = sc->sc_qfreet
    375      1.1    ragge 		 = sc->sc_qhead = sc->sc_qtail = sc->sc_qactive =
    376      1.1    ragge 		(struct dmc_command *)0;
    377      1.1    ragge 	/* set up free command buffer list */
    378      1.1    ragge 	for (qp = &sc->sc_cmdbuf[0]; qp < &sc->sc_cmdbuf[NCMDS]; qp++) {
    379      1.1    ragge 		QUEUE_AT_HEAD(qp, sc->sc_qfreeh, sc->sc_qfreet);
    380      1.1    ragge 	}
    381      1.1    ragge 
    382      1.1    ragge 	/* base in */
    383      1.1    ragge 	base = sc->sc_ui.ui_baddr;
    384      1.1    ragge 	dmcload(sc, DMC_BASEI, (u_short)base, (base>>2) & DMC_XMEM);
    385      1.1    ragge 	/* specify half duplex operation, flags tell if primary */
    386      1.1    ragge 	/* or secondary station */
    387      1.1    ragge 	if (ui->cf_flags == 0)
    388      1.1    ragge 		/* use DDCMP mode in full duplex */
    389      1.1    ragge 		dmcload(sc, DMC_CNTLI, 0, 0);
    390      1.1    ragge 	else if (ui->cf_flags == 1)
    391      1.1    ragge 		/* use MAINTENENCE mode */
    392      1.1    ragge 		dmcload(sc, DMC_CNTLI, 0, DMC_MAINT );
    393      1.1    ragge 	else if (ui->cf_flags == 2)
    394      1.1    ragge 		/* use DDCMP half duplex as primary station */
    395      1.1    ragge 		dmcload(sc, DMC_CNTLI, 0, DMC_HDPLX);
    396      1.1    ragge 	else if (ui->cf_flags == 3)
    397      1.1    ragge 		/* use DDCMP half duplex as secondary station */
    398      1.1    ragge 		dmcload(sc, DMC_CNTLI, 0, DMC_HDPLX | DMC_SEC);
    399      1.1    ragge 
    400      1.1    ragge 	/* enable operation done interrupts */
    401      1.1    ragge 	while ((DMC_RBYTE(DMC_BSEL2) & DMC_IEO) == 0)
    402      1.1    ragge 		DMC_WBYTE(DMC_BSEL2, DMC_RBYTE(DMC_BSEL2) | DMC_IEO);
    403      1.1    ragge 	s = splnet();
    404      1.1    ragge 	/* queue first NRCV buffers for DMC to fill */
    405      1.1    ragge 	for (rp = &sc->sc_rbufs[0]; rp < &sc->sc_rbufs[NRCV]; rp++) {
    406      1.1    ragge 		rp->flags |= DBUF_DMCS;
    407      1.1    ragge 		dmcload(sc, DMC_READ, rp->ubinfo,
    408      1.1    ragge 			(((rp->ubinfo>>2)&DMC_XMEM) | rp->cc));
    409      1.1    ragge 		sc->sc_iused++;
    410      1.1    ragge 	}
    411      1.1    ragge 	splx(s);
    412      1.1    ragge 	return 0;
    413      1.1    ragge }
    414      1.1    ragge 
    415      1.1    ragge /*
    416      1.1    ragge  * Start output on interface.  Get another datagram
    417      1.1    ragge  * to send from the interface queue and map it to
    418      1.1    ragge  * the interface before starting output.
    419      1.1    ragge  *
    420      1.1    ragge  * Must be called at spl 5
    421      1.1    ragge  */
    422      1.1    ragge void
    423      1.1    ragge dmcstart(struct ifnet *ifp)
    424      1.1    ragge {
    425      1.1    ragge 	struct dmc_softc *sc = ifp->if_softc;
    426      1.1    ragge 	struct mbuf *m;
    427      1.1    ragge 	struct dmcbufs *rp;
    428      1.1    ragge 	int n;
    429      1.1    ragge 
    430      1.1    ragge 	/*
    431      1.1    ragge 	 * Dequeue up to NXMT requests and map them to the UNIBUS.
    432      1.1    ragge 	 * If no more requests, or no dmc buffers available, just return.
    433      1.1    ragge 	 */
    434      1.1    ragge 	n = 0;
    435      1.1    ragge 	for (rp = &sc->sc_xbufs[0]; rp < &sc->sc_xbufs[NXMT]; rp++ ) {
    436      1.1    ragge 		/* find an available buffer */
    437      1.1    ragge 		if ((rp->flags & DBUF_DMCS) == 0) {
    438      1.3   itojun 			IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
    439      1.1    ragge 			if (m == 0)
    440      1.1    ragge 				return;
    441      1.1    ragge 			/* mark it dmcs */
    442      1.1    ragge 			rp->flags |= (DBUF_DMCS);
    443      1.1    ragge 			/*
    444      1.1    ragge 			 * Have request mapped to UNIBUS for transmission
    445      1.1    ragge 			 * and start the output.
    446      1.1    ragge 			 */
    447      1.1    ragge 			rp->cc = if_ubaput(&sc->sc_ifuba, &sc->sc_ifw[n], m);
    448      1.1    ragge 			rp->cc &= DMC_CCOUNT;
    449      1.1    ragge 			if (++sc->sc_oused == 1)
    450      1.1    ragge 				sc->sc_if.if_timer = dmc_timeout;
    451      1.9   simonb 			dmcload(sc, DMC_WRITE, rp->ubinfo,
    452      1.1    ragge 				rp->cc | ((rp->ubinfo>>2)&DMC_XMEM));
    453      1.1    ragge 		}
    454      1.1    ragge 		n++;
    455      1.1    ragge 	}
    456      1.1    ragge }
    457      1.1    ragge 
    458      1.1    ragge /*
    459      1.1    ragge  * Utility routine to load the DMC device registers.
    460      1.1    ragge  */
    461      1.1    ragge void
    462      1.1    ragge dmcload(struct dmc_softc *sc, int type, u_short w0, u_short w1)
    463      1.1    ragge {
    464      1.1    ragge 	struct dmc_command *qp;
    465      1.1    ragge 	int sps;
    466      1.1    ragge 
    467      1.1    ragge 	sps = splnet();
    468      1.1    ragge 
    469      1.1    ragge 	/* grab a command buffer from the free list */
    470      1.1    ragge 	if ((qp = sc->sc_qfreeh) == (struct dmc_command *)0)
    471      1.1    ragge 		panic("dmc command queue overflow");
    472      1.1    ragge 	DEQUEUE(sc->sc_qfreeh, sc->sc_qfreet);
    473      1.1    ragge 
    474      1.1    ragge 	/* fill in requested info */
    475      1.1    ragge 	qp->qp_cmd = (type | DMC_RQI);
    476      1.1    ragge 	qp->qp_ubaddr = w0;
    477      1.1    ragge 	qp->qp_cc = w1;
    478      1.9   simonb 
    479      1.1    ragge 	if (sc->sc_qactive) {	/* command in progress */
    480      1.1    ragge 		if (type == DMC_READ) {
    481      1.1    ragge 			QUEUE_AT_HEAD(qp, sc->sc_qhead, sc->sc_qtail);
    482      1.1    ragge 		} else {
    483      1.1    ragge 			QUEUE_AT_TAIL(qp, sc->sc_qhead, sc->sc_qtail);
    484      1.1    ragge 		}
    485      1.1    ragge 	} else {	/* command port free */
    486      1.1    ragge 		sc->sc_qactive = qp;
    487      1.1    ragge 		DMC_WBYTE(DMC_BSEL0, qp->qp_cmd);
    488      1.1    ragge 		dmcrint(sc);
    489      1.1    ragge 	}
    490      1.1    ragge 	splx(sps);
    491      1.1    ragge }
    492      1.1    ragge 
    493      1.1    ragge /*
    494      1.1    ragge  * DMC interface receiver interrupt.
    495      1.1    ragge  * Ready to accept another command,
    496      1.1    ragge  * pull one off the command queue.
    497      1.1    ragge  */
    498      1.1    ragge void
    499      1.1    ragge dmcrint(void *arg)
    500      1.1    ragge {
    501      1.1    ragge 	struct dmc_softc *sc = arg;
    502      1.1    ragge 	struct dmc_command *qp;
    503      1.1    ragge 	int n;
    504      1.1    ragge 
    505      1.1    ragge 	if ((qp = sc->sc_qactive) == (struct dmc_command *) 0) {
    506      1.1    ragge 		printf("%s: dmcrint no command\n", sc->sc_dev.dv_xname);
    507      1.1    ragge 		return;
    508      1.1    ragge 	}
    509      1.1    ragge 	while (DMC_RBYTE(DMC_BSEL0) & DMC_RDYI) {
    510      1.1    ragge 		DMC_WWORD(DMC_SEL4, qp->qp_ubaddr);
    511      1.1    ragge 		DMC_WWORD(DMC_SEL6, qp->qp_cc);
    512      1.1    ragge 		DMC_WBYTE(DMC_BSEL0, DMC_RBYTE(DMC_BSEL0) & ~(DMC_IEI|DMC_RQI));
    513      1.1    ragge 		/* free command buffer */
    514      1.1    ragge 		QUEUE_AT_HEAD(qp, sc->sc_qfreeh, sc->sc_qfreet);
    515      1.1    ragge 		while (DMC_RBYTE(DMC_BSEL0) & DMC_RDYI) {
    516      1.1    ragge 			/*
    517      1.1    ragge 			 * Can't check for RDYO here 'cause
    518      1.1    ragge 			 * this routine isn't reentrant!
    519      1.1    ragge 			 */
    520      1.1    ragge 			DELAY(5);
    521      1.1    ragge 		}
    522      1.1    ragge 		/* move on to next command */
    523      1.1    ragge 		if ((sc->sc_qactive = sc->sc_qhead) == (struct dmc_command *)0)
    524      1.1    ragge 			break;		/* all done */
    525      1.1    ragge 		/* more commands to do, start the next one */
    526      1.1    ragge 		qp = sc->sc_qactive;
    527      1.1    ragge 		DEQUEUE(sc->sc_qhead, sc->sc_qtail);
    528      1.1    ragge 		DMC_WBYTE(DMC_BSEL0, qp->qp_cmd);
    529      1.1    ragge 		n = RDYSCAN;
    530      1.1    ragge 		while (n-- > 0)
    531      1.1    ragge 			if ((DMC_RBYTE(DMC_BSEL0) & DMC_RDYI) ||
    532      1.1    ragge 			    (DMC_RBYTE(DMC_BSEL2) & DMC_RDYO))
    533      1.1    ragge 				break;
    534      1.1    ragge 	}
    535      1.1    ragge 	if (sc->sc_qactive) {
    536      1.1    ragge 		DMC_WBYTE(DMC_BSEL0, DMC_RBYTE(DMC_BSEL0) & (DMC_IEI|DMC_RQI));
    537      1.1    ragge 		/* VMS does it twice !*$%@# */
    538      1.1    ragge 		DMC_WBYTE(DMC_BSEL0, DMC_RBYTE(DMC_BSEL0) & (DMC_IEI|DMC_RQI));
    539      1.1    ragge 	}
    540      1.1    ragge 
    541      1.1    ragge }
    542      1.1    ragge 
    543      1.1    ragge /*
    544      1.1    ragge  * DMC interface transmitter interrupt.
    545      1.1    ragge  * A transfer may have completed, check for errors.
    546      1.1    ragge  * If it was a read, notify appropriate protocol.
    547      1.1    ragge  * If it was a write, pull the next one off the queue.
    548      1.1    ragge  */
    549      1.1    ragge void
    550      1.1    ragge dmcxint(void *a)
    551      1.1    ragge {
    552      1.9   simonb 	struct dmc_softc *sc = a;
    553      1.1    ragge 
    554      1.1    ragge 	struct ifnet *ifp;
    555      1.1    ragge 	struct mbuf *m;
    556      1.1    ragge 	struct ifqueue *inq;
    557      1.1    ragge 	int arg, pkaddr, cmd, len, s;
    558      1.1    ragge 	struct ifrw *ifrw;
    559      1.1    ragge 	struct dmcbufs *rp;
    560      1.1    ragge 	struct ifxmt *ifxp;
    561      1.1    ragge 	struct dmc_header *dh;
    562      1.1    ragge 	char buf[64];
    563      1.1    ragge 
    564      1.1    ragge 	ifp = &sc->sc_if;
    565      1.1    ragge 
    566      1.1    ragge 	while (DMC_RBYTE(DMC_BSEL2) & DMC_RDYO) {
    567      1.1    ragge 
    568      1.1    ragge 		cmd = DMC_RBYTE(DMC_BSEL2) & 0xff;
    569      1.1    ragge 		arg = DMC_RWORD(DMC_SEL6) & 0xffff;
    570      1.1    ragge 		/* reconstruct UNIBUS address of buffer returned to us */
    571      1.1    ragge 		pkaddr = ((arg&DMC_XMEM)<<2) | (DMC_RWORD(DMC_SEL4) & 0xffff);
    572      1.1    ragge 		/* release port */
    573      1.1    ragge 		DMC_WBYTE(DMC_BSEL2, DMC_RBYTE(DMC_BSEL2) & ~DMC_RDYO);
    574      1.1    ragge 		switch (cmd & 07) {
    575      1.1    ragge 
    576      1.1    ragge 		case DMC_OUR:
    577      1.1    ragge 			/*
    578      1.9   simonb 			 * A read has completed.
    579      1.1    ragge 			 * Pass packet to type specific
    580      1.1    ragge 			 * higher-level input routine.
    581      1.1    ragge 			 */
    582      1.1    ragge 			ifp->if_ipackets++;
    583      1.1    ragge 			/* find location in dmcuba struct */
    584      1.1    ragge 			ifrw= &sc->sc_ifr[0];
    585      1.1    ragge 			for (rp = &sc->sc_rbufs[0]; rp < &sc->sc_rbufs[NRCV]; rp++) {
    586      1.1    ragge 				if(rp->ubinfo == pkaddr)
    587      1.1    ragge 					break;
    588      1.1    ragge 				ifrw++;
    589      1.1    ragge 			}
    590      1.1    ragge 			if (rp >= &sc->sc_rbufs[NRCV])
    591      1.1    ragge 				panic("dmc rcv");
    592      1.1    ragge 			if ((rp->flags & DBUF_DMCS) == 0)
    593      1.1    ragge 				printf("%s: done unalloc rbuf\n",
    594      1.1    ragge 				    sc->sc_dev.dv_xname);
    595      1.1    ragge 
    596      1.1    ragge 			len = (arg & DMC_CCOUNT) - sizeof (struct dmc_header);
    597      1.1    ragge 			if (len < 0 || len > DMCMTU) {
    598      1.1    ragge 				ifp->if_ierrors++;
    599      1.1    ragge #ifdef DMCDEBUG
    600      1.1    ragge 				printd("%s: bad rcv pkt addr 0x%x len 0x%x\n",
    601      1.1    ragge 				    sc->sc_dev.dv_xname, pkaddr, len);
    602      1.1    ragge #endif
    603      1.1    ragge 				goto setup;
    604      1.1    ragge 			}
    605      1.1    ragge 			/*
    606      1.1    ragge 			 * Deal with trailer protocol: if type is trailer
    607      1.1    ragge 			 * get true type from first 16-bit word past data.
    608      1.1    ragge 			 * Remember that type was trailer by setting off.
    609      1.1    ragge 			 */
    610      1.1    ragge 			dh = (struct dmc_header *)ifrw->ifrw_addr;
    611      1.1    ragge 			dh->dmc_type = ntohs((u_short)dh->dmc_type);
    612      1.1    ragge 			if (len == 0)
    613      1.1    ragge 				goto setup;
    614      1.1    ragge 
    615      1.1    ragge 			/*
    616      1.1    ragge 			 * Pull packet off interface.  Off is nonzero if
    617      1.1    ragge 			 * packet has trailing header; dmc_get will then
    618      1.1    ragge 			 * force this header information to be at the front,
    619      1.1    ragge 			 * but we still have to drop the type and length
    620      1.1    ragge 			 * which are at the front of any trailer data.
    621      1.1    ragge 			 */
    622      1.1    ragge 			m = if_ubaget(&sc->sc_ifuba, ifrw, ifp, len);
    623      1.1    ragge 			if (m == 0)
    624      1.1    ragge 				goto setup;
    625      1.1    ragge 			/* Shave off dmc_header */
    626      1.1    ragge 			m_adj(m, sizeof(struct dmc_header));
    627      1.1    ragge 			switch (dh->dmc_type) {
    628      1.1    ragge 
    629      1.1    ragge #ifdef INET
    630      1.1    ragge 			case DMC_IPTYPE:
    631      1.1    ragge 				schednetisr(NETISR_IP);
    632      1.1    ragge 				inq = &ipintrq;
    633      1.1    ragge 				break;
    634      1.1    ragge #endif
    635      1.1    ragge 			default:
    636      1.1    ragge 				m_freem(m);
    637      1.1    ragge 				goto setup;
    638      1.1    ragge 			}
    639      1.1    ragge 
    640      1.1    ragge 			s = splnet();
    641      1.1    ragge 			if (IF_QFULL(inq)) {
    642      1.1    ragge 				IF_DROP(inq);
    643      1.1    ragge 				m_freem(m);
    644      1.1    ragge 			} else
    645      1.1    ragge 				IF_ENQUEUE(inq, m);
    646      1.1    ragge 			splx(s);
    647      1.1    ragge 
    648      1.1    ragge 	setup:
    649      1.1    ragge 			/* is this needed? */
    650      1.1    ragge 			rp->ubinfo = ifrw->ifrw_info;
    651      1.1    ragge 
    652      1.9   simonb 			dmcload(sc, DMC_READ, rp->ubinfo,
    653      1.1    ragge 			    ((rp->ubinfo >> 2) & DMC_XMEM) | rp->cc);
    654      1.1    ragge 			break;
    655      1.1    ragge 
    656      1.1    ragge 		case DMC_OUX:
    657      1.1    ragge 			/*
    658      1.1    ragge 			 * A write has completed, start another
    659      1.1    ragge 			 * transfer if there is more data to send.
    660      1.1    ragge 			 */
    661      1.1    ragge 			ifp->if_opackets++;
    662      1.1    ragge 			/* find associated dmcbuf structure */
    663      1.1    ragge 			ifxp = &sc->sc_ifw[0];
    664      1.1    ragge 			for (rp = &sc->sc_xbufs[0]; rp < &sc->sc_xbufs[NXMT]; rp++) {
    665      1.1    ragge 				if(rp->ubinfo == pkaddr)
    666      1.1    ragge 					break;
    667      1.1    ragge 				ifxp++;
    668      1.1    ragge 			}
    669      1.1    ragge 			if (rp >= &sc->sc_xbufs[NXMT]) {
    670      1.1    ragge 				printf("%s: bad packet address 0x%x\n",
    671      1.1    ragge 				    sc->sc_dev.dv_xname, pkaddr);
    672      1.1    ragge 				break;
    673      1.1    ragge 			}
    674      1.1    ragge 			if ((rp->flags & DBUF_DMCS) == 0)
    675      1.1    ragge 				printf("%s: unallocated packet 0x%x\n",
    676      1.1    ragge 				    sc->sc_dev.dv_xname, pkaddr);
    677      1.1    ragge 			/* mark buffer free */
    678      1.1    ragge 			if_ubaend(&sc->sc_ifuba, ifxp);
    679      1.1    ragge 			rp->flags &= ~DBUF_DMCS;
    680      1.1    ragge 			if (--sc->sc_oused == 0)
    681      1.1    ragge 				sc->sc_if.if_timer = 0;
    682      1.1    ragge 			else
    683      1.1    ragge 				sc->sc_if.if_timer = dmc_timeout;
    684      1.1    ragge 			if ((sc->sc_flag & DMC_ONLINE) == 0) {
    685      1.1    ragge 				extern int ifqmaxlen;
    686      1.1    ragge 
    687      1.1    ragge 				/*
    688      1.1    ragge 				 * We're on the air.
    689      1.1    ragge 				 * Open the queue to the usual value.
    690      1.1    ragge 				 */
    691      1.1    ragge 				sc->sc_flag |= DMC_ONLINE;
    692      1.1    ragge 				ifp->if_snd.ifq_maxlen = ifqmaxlen;
    693      1.1    ragge 			}
    694      1.1    ragge 			break;
    695      1.1    ragge 
    696      1.1    ragge 		case DMC_CNTLO:
    697      1.1    ragge 			arg &= DMC_CNTMASK;
    698      1.1    ragge 			if (arg & DMC_FATAL) {
    699      1.1    ragge 				if (arg != DMC_START) {
    700      1.1    ragge 					bitmask_snprintf(arg, CNTLO_BITS,
    701      1.1    ragge 					    buf, sizeof(buf));
    702      1.1    ragge 					log(LOG_ERR,
    703      1.1    ragge 					    "%s: fatal error, flags=%s\n",
    704      1.1    ragge 					    sc->sc_dev.dv_xname, buf);
    705      1.1    ragge 				}
    706      1.1    ragge 				dmcrestart(sc);
    707      1.1    ragge 				break;
    708      1.1    ragge 			}
    709      1.1    ragge 			/* ACCUMULATE STATISTICS */
    710      1.1    ragge 			switch(arg) {
    711      1.1    ragge 			case DMC_NOBUFS:
    712      1.1    ragge 				ifp->if_ierrors++;
    713      1.1    ragge 				if ((sc->sc_nobuf++ % DMC_RPNBFS) == 0)
    714      1.1    ragge 					goto report;
    715      1.1    ragge 				break;
    716      1.1    ragge 			case DMC_DISCONN:
    717      1.1    ragge 				if ((sc->sc_disc++ % DMC_RPDSC) == 0)
    718      1.1    ragge 					goto report;
    719      1.1    ragge 				break;
    720      1.1    ragge 			case DMC_TIMEOUT:
    721      1.1    ragge 				if ((sc->sc_timeo++ % DMC_RPTMO) == 0)
    722      1.1    ragge 					goto report;
    723      1.1    ragge 				break;
    724      1.1    ragge 			case DMC_DATACK:
    725      1.1    ragge 				ifp->if_oerrors++;
    726      1.1    ragge 				if ((sc->sc_datck++ % DMC_RPDCK) == 0)
    727      1.1    ragge 					goto report;
    728      1.1    ragge 				break;
    729      1.1    ragge 			default:
    730      1.1    ragge 				goto report;
    731      1.1    ragge 			}
    732      1.1    ragge 			break;
    733      1.1    ragge 		report:
    734      1.1    ragge #ifdef DMCDEBUG
    735      1.1    ragge 			bitmask_snprintf(arg, CNTLO_BITS, buf, sizeof(buf));
    736      1.1    ragge 			printd("%s: soft error, flags=%s\n",
    737      1.1    ragge 			    sc->sc_dev.dv_xname, buf);
    738      1.1    ragge #endif
    739      1.1    ragge 			if ((sc->sc_flag & DMC_RESTART) == 0) {
    740      1.1    ragge 				/*
    741      1.1    ragge 				 * kill off the dmc to get things
    742      1.1    ragge 				 * going again by generating a
    743      1.1    ragge 				 * procedure error
    744      1.1    ragge 				 */
    745      1.1    ragge 				sc->sc_flag |= DMC_RESTART;
    746      1.1    ragge 				arg = sc->sc_ui.ui_baddr;
    747      1.1    ragge 				dmcload(sc, DMC_BASEI, arg, (arg>>2)&DMC_XMEM);
    748      1.1    ragge 			}
    749      1.1    ragge 			break;
    750      1.1    ragge 
    751      1.1    ragge 		default:
    752      1.9   simonb 			printf("%s: bad control %o\n",
    753      1.1    ragge 			    sc->sc_dev.dv_xname, cmd);
    754      1.1    ragge 			break;
    755      1.1    ragge 		}
    756      1.1    ragge 	}
    757      1.1    ragge 	dmcstart(ifp);
    758      1.1    ragge }
    759      1.1    ragge 
    760      1.1    ragge /*
    761      1.1    ragge  * DMC output routine.
    762      1.1    ragge  * Encapsulate a packet of type family for the dmc.
    763      1.1    ragge  * Use trailer local net encapsulation if enough data in first
    764      1.1    ragge  * packet leaves a multiple of 512 bytes of data in remainder.
    765      1.1    ragge  */
    766      1.1    ragge int
    767      1.1    ragge dmcoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
    768      1.1    ragge     struct rtentry *rt)
    769      1.1    ragge {
    770      1.1    ragge 	int type, error, s;
    771      1.1    ragge 	struct mbuf *m = m0;
    772      1.1    ragge 	struct dmc_header *dh;
    773      1.3   itojun 	ALTQ_DECL(struct altq_pktattr pktattr;)
    774      1.1    ragge 
    775      1.1    ragge 	if ((ifp->if_flags & IFF_UP) == 0) {
    776      1.1    ragge 		error = ENETDOWN;
    777      1.1    ragge 		goto bad;
    778      1.1    ragge 	}
    779      1.1    ragge 
    780      1.3   itojun 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
    781      1.3   itojun 
    782      1.1    ragge 	switch (dst->sa_family) {
    783      1.1    ragge #ifdef	INET
    784      1.1    ragge 	case AF_INET:
    785      1.1    ragge 		type = DMC_IPTYPE;
    786      1.1    ragge 		break;
    787      1.1    ragge #endif
    788      1.1    ragge 
    789      1.1    ragge 	case AF_UNSPEC:
    790      1.1    ragge 		dh = (struct dmc_header *)dst->sa_data;
    791      1.1    ragge 		type = dh->dmc_type;
    792      1.1    ragge 		break;
    793      1.1    ragge 
    794      1.1    ragge 	default:
    795      1.1    ragge 		printf("%s: can't handle af%d\n", ifp->if_xname,
    796      1.1    ragge 			dst->sa_family);
    797      1.1    ragge 		error = EAFNOSUPPORT;
    798      1.1    ragge 		goto bad;
    799      1.1    ragge 	}
    800      1.1    ragge 
    801      1.1    ragge 	/*
    802      1.1    ragge 	 * Add local network header
    803      1.1    ragge 	 * (there is space for a uba on a vax to step on)
    804      1.1    ragge 	 */
    805      1.1    ragge 	M_PREPEND(m, sizeof(struct dmc_header), M_DONTWAIT);
    806      1.1    ragge 	if (m == 0) {
    807      1.1    ragge 		error = ENOBUFS;
    808      1.1    ragge 		goto bad;
    809      1.1    ragge 	}
    810      1.1    ragge 	dh = mtod(m, struct dmc_header *);
    811      1.1    ragge 	dh->dmc_type = htons((u_short)type);
    812      1.1    ragge 
    813      1.1    ragge 	/*
    814      1.1    ragge 	 * Queue message on interface, and start output if interface
    815      1.1    ragge 	 * not yet active.
    816      1.1    ragge 	 */
    817      1.1    ragge 	s = splnet();
    818      1.3   itojun 	IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    819      1.3   itojun 	if (error) {
    820      1.3   itojun 		/* mbuf is already freed */
    821      1.1    ragge 		splx(s);
    822      1.3   itojun 		return (error);
    823      1.1    ragge 	}
    824      1.1    ragge 	dmcstart(ifp);
    825      1.1    ragge 	splx(s);
    826      1.1    ragge 	return (0);
    827      1.1    ragge 
    828      1.1    ragge bad:
    829      1.1    ragge 	m_freem(m0);
    830      1.1    ragge 	return (error);
    831      1.1    ragge }
    832      1.1    ragge 
    833      1.1    ragge 
    834      1.1    ragge /*
    835      1.1    ragge  * Process an ioctl request.
    836      1.1    ragge  */
    837      1.1    ragge /* ARGSUSED */
    838      1.1    ragge int
    839  1.9.4.2     yamt dmcioctl(struct ifnet *ifp, u_long cmd, void *data)
    840      1.1    ragge {
    841      1.1    ragge 	int s = splnet(), error = 0;
    842      1.1    ragge 	register struct dmc_softc *sc = ifp->if_softc;
    843      1.1    ragge 
    844      1.1    ragge 	switch (cmd) {
    845      1.1    ragge 
    846      1.1    ragge 	case SIOCSIFADDR:
    847      1.1    ragge 		ifp->if_flags |= IFF_UP;
    848      1.1    ragge 		if ((ifp->if_flags & IFF_RUNNING) == 0)
    849      1.9   simonb 			dmcinit(ifp);
    850      1.1    ragge 		break;
    851      1.1    ragge 
    852      1.1    ragge 	case SIOCSIFDSTADDR:
    853      1.1    ragge 		if ((ifp->if_flags & IFF_RUNNING) == 0)
    854      1.9   simonb 			dmcinit(ifp);
    855      1.1    ragge 		break;
    856      1.9   simonb 
    857      1.1    ragge 	case SIOCSIFFLAGS:
    858      1.1    ragge 		if ((ifp->if_flags & IFF_UP) == 0 &&
    859      1.1    ragge 		    sc->sc_flag & DMC_RUNNING)
    860      1.1    ragge 			dmcdown(sc);
    861      1.1    ragge 		else if (ifp->if_flags & IFF_UP &&
    862      1.1    ragge 		    (sc->sc_flag & DMC_RUNNING) == 0)
    863      1.1    ragge 			dmcrestart(sc);
    864      1.1    ragge 		break;
    865      1.1    ragge 
    866      1.1    ragge 	default:
    867      1.1    ragge 		error = EINVAL;
    868      1.1    ragge 	}
    869      1.1    ragge 	splx(s);
    870      1.1    ragge 	return (error);
    871      1.1    ragge }
    872      1.1    ragge 
    873      1.1    ragge /*
    874      1.1    ragge  * Restart after a fatal error.
    875      1.1    ragge  * Clear device and reinitialize.
    876      1.1    ragge  */
    877      1.1    ragge void
    878      1.1    ragge dmcrestart(struct dmc_softc *sc)
    879      1.1    ragge {
    880      1.1    ragge 	int s, i;
    881      1.9   simonb 
    882      1.1    ragge #ifdef DMCDEBUG
    883      1.1    ragge 	/* dump base table */
    884      1.1    ragge 	printf("%s base table:\n", sc->sc_dev.dv_xname);
    885      1.1    ragge 	for (i = 0; i < sizeof (struct dmc_base); i++)
    886      1.1    ragge 		printf("%o\n" ,dmc_base[unit].d_base[i]);
    887      1.1    ragge #endif
    888      1.1    ragge 
    889      1.1    ragge 	dmcdown(sc);
    890      1.1    ragge 
    891      1.1    ragge 	/*
    892      1.1    ragge 	 * Let the DMR finish the MCLR.	 At 1 Mbit, it should do so
    893      1.1    ragge 	 * in about a max of 6.4 milliseconds with diagnostics enabled.
    894      1.1    ragge 	 */
    895      1.1    ragge 	for (i = 100000; i && (DMC_RBYTE(DMC_BSEL1) & DMC_RUN) == 0; i--)
    896      1.1    ragge 		;
    897      1.1    ragge 	/* Did the timer expire or did the DMR finish? */
    898      1.1    ragge 	if ((DMC_RBYTE(DMC_BSEL1) & DMC_RUN) == 0) {
    899      1.1    ragge 		log(LOG_ERR, "%s: M820 Test Failed\n", sc->sc_dev.dv_xname);
    900      1.1    ragge 		return;
    901      1.1    ragge 	}
    902      1.1    ragge 
    903      1.1    ragge 	/* restart DMC */
    904      1.1    ragge 	dmcinit(&sc->sc_if);
    905      1.1    ragge 	sc->sc_flag &= ~DMC_RESTART;
    906      1.1    ragge 	s = splnet();
    907      1.1    ragge 	dmcstart(&sc->sc_if);
    908      1.1    ragge 	splx(s);
    909      1.1    ragge 	sc->sc_if.if_collisions++;	/* why not? */
    910      1.1    ragge }
    911      1.1    ragge 
    912      1.1    ragge /*
    913      1.1    ragge  * Reset a device and mark down.
    914      1.1    ragge  * Flush output queue and drop queue limit.
    915      1.1    ragge  */
    916      1.1    ragge void
    917      1.1    ragge dmcdown(struct dmc_softc *sc)
    918      1.1    ragge {
    919      1.1    ragge 	struct ifxmt *ifxp;
    920      1.1    ragge 
    921      1.1    ragge 	DMC_WBYTE(DMC_BSEL1, DMC_MCLR);
    922      1.1    ragge 	sc->sc_flag &= ~(DMC_RUNNING | DMC_ONLINE);
    923      1.1    ragge 
    924      1.1    ragge 	for (ifxp = sc->sc_ifw; ifxp < &sc->sc_ifw[NXMT]; ifxp++) {
    925      1.1    ragge #ifdef notyet
    926      1.1    ragge 		if (ifxp->ifw_xtofree) {
    927      1.1    ragge 			(void) m_freem(ifxp->ifw_xtofree);
    928      1.1    ragge 			ifxp->ifw_xtofree = 0;
    929      1.1    ragge 		}
    930      1.1    ragge #endif
    931      1.1    ragge 	}
    932      1.1    ragge 	IF_PURGE(&sc->sc_if.if_snd);
    933      1.1    ragge }
    934      1.1    ragge 
    935      1.1    ragge /*
    936      1.1    ragge  * Watchdog timeout to see that transmitted packets don't
    937      1.1    ragge  * lose interrupts.  The device has to be online (the first
    938      1.1    ragge  * transmission may block until the other side comes up).
    939      1.1    ragge  */
    940      1.1    ragge void
    941      1.1    ragge dmctimeout(struct ifnet *ifp)
    942      1.1    ragge {
    943      1.1    ragge 	struct dmc_softc *sc = ifp->if_softc;
    944      1.1    ragge 	char buf1[64], buf2[64];
    945      1.1    ragge 
    946      1.1    ragge 	if (sc->sc_flag & DMC_ONLINE) {
    947      1.1    ragge 		bitmask_snprintf(DMC_RBYTE(DMC_BSEL0) & 0xff, DMC0BITS,
    948      1.1    ragge 		    buf1, sizeof(buf1));
    949      1.1    ragge 		bitmask_snprintf(DMC_RBYTE(DMC_BSEL2) & 0xff, DMC2BITS,
    950      1.1    ragge 		    buf2, sizeof(buf2));
    951      1.1    ragge 		log(LOG_ERR, "%s: output timeout, bsel0=%s bsel2=%s\n",
    952      1.1    ragge 		    sc->sc_dev.dv_xname, buf1, buf2);
    953      1.1    ragge 		dmcrestart(sc);
    954      1.1    ragge 	}
    955      1.1    ragge }
    956