Home | History | Annotate | Line # | Download | only in scsipi
if_se.c revision 1.3
      1  1.3  thorpej /*	$NetBSD: if_se.c,v 1.3 1997/03/24 00:04:53 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 1997 Ian W. Dall <ian.dall (at) dsto.defence.gov.au>
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     16  1.1  thorpej  *    must display the following acknowledgement:
     17  1.1  thorpej  *	This product includes software developed by Ian W. Dall.
     18  1.1  thorpej  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  thorpej  *    derived from this software without specific prior written permission.
     20  1.1  thorpej  *
     21  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1  thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  thorpej  */
     32  1.1  thorpej 
     33  1.1  thorpej /*
     34  1.1  thorpej  * Written by Ian Dall <ian.dall (at) dsto.defence.gov.au> Feb 3, 1997
     35  1.1  thorpej  */
     36  1.1  thorpej /*
     37  1.1  thorpej  * Driver for Cabletron EA41x scsi ethernet adaptor.
     38  1.1  thorpej  *
     39  1.1  thorpej  * This is a weird device! It doesn't conform to the scsi spec in much
     40  1.1  thorpej  * at all. About the only standard command supported in inquiry. Most
     41  1.1  thorpej  * commands are 6 bytes long, but the recv data is only 1 byte.  Data
     42  1.1  thorpej  * must be received by periodically polling the device with the recv
     43  1.1  thorpej  * command.
     44  1.1  thorpej  *
     45  1.1  thorpej  * This driver is also a bit unusual. It must look like a network
     46  1.1  thorpej  * interface and it must also appear to be a scsi device to the scsi
     47  1.1  thorpej  * system. Hence there are cases where there are two entry points. eg
     48  1.1  thorpej  * sestart is to be called from the scsi subsytem and se_ifstart from
     49  1.1  thorpej  * the network interface subsystem.  In addition, to facilitate scsi
     50  1.1  thorpej  * commands issued by userland programs, there are open, close and
     51  1.1  thorpej  * ioctl entry points. This allows a user program to, for example,
     52  1.1  thorpej  * display the ea41x stats and download new code into the adaptor ---
     53  1.1  thorpej  * functions which can't be performed through the ifconfig interface.
     54  1.1  thorpej  * Normal operation does not require any special userland program.
     55  1.1  thorpej  */
     56  1.1  thorpej 
     57  1.1  thorpej #include "bpfilter.h"
     58  1.1  thorpej 
     59  1.1  thorpej #include <sys/types.h>
     60  1.1  thorpej #include <sys/param.h>
     61  1.1  thorpej #include <sys/systm.h>
     62  1.1  thorpej #include <sys/syslog.h>
     63  1.1  thorpej #include <sys/kernel.h>
     64  1.1  thorpej #include <sys/file.h>
     65  1.1  thorpej #include <sys/stat.h>
     66  1.1  thorpej #include <sys/ioctl.h>
     67  1.1  thorpej #include <sys/buf.h>
     68  1.1  thorpej #include <sys/uio.h>
     69  1.1  thorpej #include <sys/malloc.h>
     70  1.1  thorpej #include <sys/errno.h>
     71  1.1  thorpej #include <sys/device.h>
     72  1.1  thorpej #include <sys/disklabel.h>
     73  1.1  thorpej #include <sys/disk.h>
     74  1.1  thorpej #include <sys/proc.h>
     75  1.1  thorpej #include <sys/conf.h>
     76  1.1  thorpej 
     77  1.1  thorpej #include <scsi/scsi_all.h>
     78  1.1  thorpej #include <scsi/scsi_ctron_ether.h>
     79  1.1  thorpej #include <scsi/scsiconf.h>
     80  1.1  thorpej 
     81  1.1  thorpej #include <sys/mbuf.h>
     82  1.1  thorpej 
     83  1.1  thorpej #include <sys/socket.h>
     84  1.1  thorpej #include <net/if.h>
     85  1.1  thorpej #include <net/if_dl.h>
     86  1.1  thorpej #include <net/if_ether.h>
     87  1.1  thorpej 
     88  1.1  thorpej #ifdef INET
     89  1.1  thorpej #include <netinet/in.h>
     90  1.1  thorpej #include <netinet/if_inarp.h>
     91  1.2      cgd #endif
     92  1.2      cgd 
     93  1.2      cgd #ifdef NS
     94  1.2      cgd #include <netns/ns.h>
     95  1.2      cgd #include <netns/ns_if.h>
     96  1.2      cgd #endif
     97  1.2      cgd 
     98  1.2      cgd #if defined(CCITT) && defined(LLC)
     99  1.2      cgd #include <sys/socketvar.h>
    100  1.2      cgd #include <netccitt/x25.h>
    101  1.2      cgd #include <netccitt/pk.h>
    102  1.2      cgd #include <netccitt/pk_var.h>
    103  1.2      cgd #include <netccitt/pk_extern.h>
    104  1.1  thorpej #endif
    105  1.1  thorpej 
    106  1.1  thorpej #if NBPFILTER > 0
    107  1.1  thorpej #include <net/bpf.h>
    108  1.1  thorpej #include <net/bpfdesc.h>
    109  1.1  thorpej #endif
    110  1.1  thorpej 
    111  1.1  thorpej #define SETIMEOUT	1000
    112  1.1  thorpej #define	SEOUTSTANDING	4
    113  1.1  thorpej #define	SERETRIES	4
    114  1.1  thorpej #define SE_PREFIX	4
    115  1.1  thorpej #define ETHER_CRC	4
    116  1.1  thorpej #define SEMINSIZE	60
    117  1.1  thorpej 
    118  1.1  thorpej /* Make this big enough for an ETHERMTU packet in promiscuous mode. */
    119  1.1  thorpej #define MAX_SNAP	(ETHERMTU + sizeof(struct ether_header) + \
    120  1.1  thorpej 			 SE_PREFIX + ETHER_CRC)
    121  1.1  thorpej #define RBUF_LEN     (16 * 1024)
    122  1.1  thorpej 
    123  1.1  thorpej      /* se_poll and se_poll0 are the normal polling rate and the
    124  1.1  thorpej       * minimum polling rate respectively. If se_poll0 should be
    125  1.1  thorpej       * chosen so that at maximum ethernet speed, we will read nearly
    126  1.1  thorpej       * full buffers. se_poll should be chosen for reasonable maximum
    127  1.1  thorpej       * latency.
    128  1.1  thorpej       */
    129  1.1  thorpej #define SE_POLL 40		/* default in milliseconds */
    130  1.1  thorpej #define SE_POLL0 20		/* Default in milliseconds */
    131  1.1  thorpej int se_poll = 0; /* Delay in ticks set at attach time */
    132  1.1  thorpej int se_poll0 = 0;
    133  1.1  thorpej 
    134  1.1  thorpej #define	PROTOCMD(p, d) \
    135  1.3  thorpej 	((d) = (p))
    136  1.1  thorpej 
    137  1.1  thorpej #define	PROTOCMD_DECL(name, val) \
    138  1.1  thorpej 	static const struct scsi_ctron_ether_generic name = val
    139  1.1  thorpej 
    140  1.3  thorpej #define	PROTOCMD_DECL_SPECIAL(name, val) \
    141  1.3  thorpej 	static const struct __CONCAT(scsi_,name) name = val
    142  1.3  thorpej 
    143  1.3  thorpej /* Command initializers for commands using scsi_ctron_ether_generic */
    144  1.1  thorpej PROTOCMD_DECL(ctron_ether_send, {CTRON_ETHER_SEND});
    145  1.1  thorpej PROTOCMD_DECL(ctron_ether_add_proto, {CTRON_ETHER_ADD_PROTO});
    146  1.1  thorpej PROTOCMD_DECL(ctron_ether_get_addr, {CTRON_ETHER_GET_ADDR});
    147  1.1  thorpej PROTOCMD_DECL(ctron_ether_set_media, {CTRON_ETHER_SET_MEDIA});
    148  1.1  thorpej PROTOCMD_DECL(ctron_ether_set_addr, {CTRON_ETHER_SET_ADDR});
    149  1.1  thorpej PROTOCMD_DECL(ctron_ether_set_multi, {CTRON_ETHER_SET_MULTI});
    150  1.1  thorpej PROTOCMD_DECL(ctron_ether_remove_multi, {CTRON_ETHER_REMOVE_MULTI});
    151  1.3  thorpej 
    152  1.3  thorpej /* Command initializers for commands using their own structures */
    153  1.3  thorpej PROTOCMD_DECL_SPECIAL(ctron_ether_recv, {CTRON_ETHER_RECV});
    154  1.3  thorpej PROTOCMD_DECL_SPECIAL(ctron_ether_set_mode, {CTRON_ETHER_SET_MODE});
    155  1.1  thorpej 
    156  1.1  thorpej struct se_softc {
    157  1.1  thorpej 	struct device sc_dev;
    158  1.1  thorpej 	struct ethercom sc_ethercom;	/* Ethernet common part */
    159  1.1  thorpej 	struct scsi_link *sc_link;	/* contains our targ, lun, etc. */
    160  1.1  thorpej 	char *sc_tbuf;
    161  1.1  thorpej 	char *sc_rbuf;
    162  1.1  thorpej 	int protos;
    163  1.1  thorpej #define PROTO_IP	0x1
    164  1.1  thorpej #define PROTO_ARP	0x2
    165  1.1  thorpej #define PROTO_REVARP	0x4
    166  1.1  thorpej 	int sc_debug;
    167  1.1  thorpej 	int sc_flags;
    168  1.1  thorpej #define SE_NEED_RECV 0x1
    169  1.1  thorpej };
    170  1.1  thorpej 
    171  1.1  thorpej cdev_decl(se);
    172  1.1  thorpej 
    173  1.1  thorpej #ifdef __BROKEN_INDIRECT_CONFIG
    174  1.1  thorpej int	sematch __P((struct device *, void *, void *));
    175  1.1  thorpej #else
    176  1.1  thorpej int	sematch __P((struct device *, struct cfdata *, void *));
    177  1.1  thorpej #endif
    178  1.1  thorpej void	seattach __P((struct device *, struct device *, void *));
    179  1.1  thorpej 
    180  1.1  thorpej void	se_ifstart __P((struct ifnet *));
    181  1.1  thorpej void	sestart __P((void *));
    182  1.1  thorpej 
    183  1.1  thorpej static	int sedone __P((struct scsi_xfer *, int));
    184  1.1  thorpej int	se_ioctl __P((struct ifnet *, u_long, caddr_t));
    185  1.1  thorpej void	sewatchdog __P((struct ifnet *));
    186  1.1  thorpej 
    187  1.1  thorpej static void se_recv __P((void *));
    188  1.1  thorpej static struct mbuf *se_get __P((struct se_softc *, char *, int));
    189  1.1  thorpej static int se_read __P((struct se_softc *, char *, int));
    190  1.1  thorpej void sewatchdog __P((struct ifnet *));
    191  1.1  thorpej static int se_reset __P((struct se_softc *));
    192  1.1  thorpej static int se_add_proto __P((struct se_softc *, int));
    193  1.1  thorpej static int se_get_addr __P((struct se_softc *, u_int8_t *));
    194  1.1  thorpej static int se_set_media __P((struct se_softc *, int));
    195  1.1  thorpej static int se_init __P((struct se_softc *));
    196  1.1  thorpej static int se_set_multi __P((struct se_softc *, u_int8_t *));
    197  1.1  thorpej static int se_remove_multi __P((struct se_softc *, u_int8_t *));
    198  1.1  thorpej #if 0
    199  1.1  thorpej static int sc_set_all_multi __P((struct se_softc *, int));
    200  1.1  thorpej #endif
    201  1.1  thorpej static void se_stop __P((struct se_softc *));
    202  1.1  thorpej static __inline__  int se_scsi_cmd __P((struct scsi_link *sc_link,
    203  1.1  thorpej 			    struct scsi_generic *scsi_cmd,
    204  1.1  thorpej 			    int cmdlen, u_char *data_addr, int datalen,
    205  1.1  thorpej 			    int retries, int timeout, struct buf *bp,
    206  1.1  thorpej 			    int flags));
    207  1.1  thorpej static void se_delayed_ifstart __P((void *));
    208  1.1  thorpej static int se_set_mode(struct se_softc *, int, int);
    209  1.1  thorpej 
    210  1.1  thorpej struct cfattach se_ca = {
    211  1.1  thorpej 	sizeof(struct se_softc), sematch, seattach
    212  1.1  thorpej };
    213  1.1  thorpej 
    214  1.1  thorpej struct cfdriver se_cd = {
    215  1.1  thorpej 	NULL, "se", DV_IFNET
    216  1.1  thorpej };
    217  1.1  thorpej 
    218  1.1  thorpej struct scsi_device se_switch = {
    219  1.1  thorpej 	NULL,			/* Use default error handler */
    220  1.1  thorpej 	sestart,		/* have a queue, served by this */
    221  1.1  thorpej 	NULL,			/* have no async handler */
    222  1.1  thorpej 	sedone,			/* deal with stats at interrupt time */
    223  1.1  thorpej };
    224  1.1  thorpej 
    225  1.1  thorpej struct scsi_inquiry_pattern se_patterns[] = {
    226  1.1  thorpej 	{T_PROCESSOR, T_FIXED,
    227  1.1  thorpej 	 "CABLETRN",         "EA412/14/19",                 ""},
    228  1.1  thorpej 	{T_PROCESSOR, T_FIXED,
    229  1.1  thorpej 	 "Cabletrn",         "EA412/14/19",                 ""},
    230  1.1  thorpej };
    231  1.1  thorpej 
    232  1.1  thorpej static __inline__ u_int16_t ether_cmp __P((void *, void *));
    233  1.1  thorpej 
    234  1.1  thorpej /*
    235  1.1  thorpej  * Compare two Ether/802 addresses for equality, inlined and
    236  1.1  thorpej  * unrolled for speed.
    237  1.1  thorpej  * Note: use this like bcmp()
    238  1.1  thorpej  */
    239  1.1  thorpej static __inline__ u_int16_t
    240  1.1  thorpej ether_cmp(one, two)
    241  1.1  thorpej 	void *one, *two;
    242  1.1  thorpej {
    243  1.1  thorpej 	register u_int16_t *a = (u_int16_t *) one;
    244  1.1  thorpej 	register u_int16_t *b = (u_int16_t *) two;
    245  1.1  thorpej 	register u_int16_t diff;
    246  1.1  thorpej 
    247  1.1  thorpej 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
    248  1.1  thorpej 
    249  1.1  thorpej 	return (diff);
    250  1.1  thorpej }
    251  1.1  thorpej 
    252  1.1  thorpej #define ETHER_CMP	ether_cmp
    253  1.1  thorpej 
    254  1.1  thorpej int
    255  1.1  thorpej sematch(parent, match, aux)
    256  1.1  thorpej 	struct device *parent;
    257  1.1  thorpej #ifdef __BROKEN_INDIRECT_CONFIG
    258  1.1  thorpej 	void *match;
    259  1.1  thorpej #else
    260  1.1  thorpej 	struct cfdata *match;
    261  1.1  thorpej #endif
    262  1.1  thorpej 	void *aux;
    263  1.1  thorpej {
    264  1.1  thorpej 	struct scsibus_attach_args *sa = aux;
    265  1.1  thorpej 	int priority;
    266  1.1  thorpej 
    267  1.1  thorpej 	(void)scsi_inqmatch(sa->sa_inqbuf,
    268  1.1  thorpej 	    (caddr_t)se_patterns, sizeof(se_patterns)/sizeof(se_patterns[0]),
    269  1.1  thorpej 	    sizeof(se_patterns[0]), &priority);
    270  1.1  thorpej 	return (priority);
    271  1.1  thorpej }
    272  1.1  thorpej 
    273  1.1  thorpej /*
    274  1.1  thorpej  * The routine called by the low level scsi routine when it discovers
    275  1.1  thorpej  * a device suitable for this driver.
    276  1.1  thorpej  */
    277  1.1  thorpej void
    278  1.1  thorpej seattach(parent, self, aux)
    279  1.1  thorpej 	struct device *parent, *self;
    280  1.1  thorpej 	void *aux;
    281  1.1  thorpej {
    282  1.1  thorpej 	struct se_softc *sc = (void *)self;
    283  1.1  thorpej 	struct scsibus_attach_args *sa = aux;
    284  1.1  thorpej 	struct scsi_link *sc_link = sa->sa_sc_link;
    285  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    286  1.1  thorpej 	u_int8_t myaddr[ETHER_ADDR_LEN];
    287  1.1  thorpej 
    288  1.1  thorpej 	SC_DEBUG(sc_link, SDEV_DB2, ("seattach: "));
    289  1.1  thorpej 
    290  1.1  thorpej 	/*
    291  1.1  thorpej 	 * Store information needed to contact our base driver
    292  1.1  thorpej 	 */
    293  1.1  thorpej 	sc->sc_link = sc_link;
    294  1.1  thorpej 	sc_link->device = &se_switch;
    295  1.1  thorpej 	sc_link->device_softc = sc;
    296  1.1  thorpej 	if (sc_link->openings > SEOUTSTANDING)
    297  1.1  thorpej 		sc_link->openings = SEOUTSTANDING;
    298  1.1  thorpej 
    299  1.1  thorpej 	se_poll = (SE_POLL * hz) / 1000;
    300  1.1  thorpej 	se_poll = se_poll? se_poll: 1;
    301  1.1  thorpej 	se_poll0 = (SE_POLL0 * hz) / 1000;
    302  1.1  thorpej 	se_poll0 = se_poll0? se_poll0: 1;
    303  1.1  thorpej 
    304  1.1  thorpej 	/*
    305  1.1  thorpej 	 * Initialize and attach a buffer
    306  1.1  thorpej 	 */
    307  1.1  thorpej 	sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header),
    308  1.1  thorpej 			     M_DEVBUF, M_NOWAIT);
    309  1.1  thorpej 	if(sc->sc_tbuf == 0)
    310  1.1  thorpej 		panic("seattach: can't allocate transmit buffer");
    311  1.1  thorpej 
    312  1.1  thorpej 	sc->sc_rbuf = malloc(RBUF_LEN, M_DEVBUF, M_NOWAIT);/* A Guess */
    313  1.1  thorpej 	if(sc->sc_rbuf == 0)
    314  1.1  thorpej 		panic("seattach: can't allocate receive buffer");
    315  1.1  thorpej 
    316  1.1  thorpej 	se_get_addr(sc, myaddr);
    317  1.1  thorpej 
    318  1.1  thorpej 	/* Initialize ifnet structure. */
    319  1.1  thorpej 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    320  1.1  thorpej 	ifp->if_softc = sc;
    321  1.1  thorpej 	ifp->if_start = se_ifstart;
    322  1.1  thorpej 	ifp->if_ioctl = se_ioctl;
    323  1.1  thorpej 	ifp->if_watchdog = sewatchdog;
    324  1.1  thorpej 	ifp->if_flags =
    325  1.1  thorpej 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    326  1.1  thorpej 
    327  1.1  thorpej 	/* Attach the interface. */
    328  1.1  thorpej 	if_attach(ifp);
    329  1.1  thorpej 	ether_ifattach(ifp, myaddr);
    330  1.1  thorpej 
    331  1.1  thorpej #if NBPFILTER > 0
    332  1.1  thorpej 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    333  1.1  thorpej #endif
    334  1.1  thorpej }
    335  1.1  thorpej 
    336  1.1  thorpej 
    337  1.1  thorpej static __inline__ int se_scsi_cmd(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
    338  1.1  thorpej 		       retries, timeout, bp, flags)
    339  1.1  thorpej 	struct scsi_link *sc_link;
    340  1.1  thorpej 	struct scsi_generic *scsi_cmd;
    341  1.1  thorpej 	int cmdlen;
    342  1.1  thorpej 	u_char *data_addr;
    343  1.1  thorpej 	int datalen;
    344  1.1  thorpej 	int retries;
    345  1.1  thorpej 	int timeout;
    346  1.1  thorpej 	struct buf *bp;
    347  1.1  thorpej 	int flags;
    348  1.1  thorpej {
    349  1.1  thorpej 	int error;
    350  1.1  thorpej 	int s = splbio();
    351  1.1  thorpej 	error = scsi_scsi_cmd(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
    352  1.1  thorpej 		      retries, timeout, bp, flags);
    353  1.1  thorpej 	splx(s);
    354  1.1  thorpej 	return error;
    355  1.1  thorpej }
    356  1.1  thorpej /* Start routine for calling from scsi sub system */
    357  1.1  thorpej void sestart(void *v)
    358  1.1  thorpej {
    359  1.1  thorpej 	struct se_softc *sc = (struct se_softc *) v;
    360  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    361  1.1  thorpej 	int s = splnet();
    362  1.1  thorpej 	se_ifstart(ifp);
    363  1.1  thorpej 	(void) splx(s);
    364  1.1  thorpej }
    365  1.1  thorpej 
    366  1.1  thorpej static void se_delayed_ifstart(void *v)
    367  1.1  thorpej {
    368  1.1  thorpej 	struct ifnet *ifp = v;
    369  1.1  thorpej 	int s = splnet();
    370  1.1  thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
    371  1.1  thorpej 	se_ifstart(ifp);
    372  1.1  thorpej 	splx(s);
    373  1.1  thorpej }
    374  1.1  thorpej 
    375  1.1  thorpej /*
    376  1.1  thorpej  * Start transmission on the interface.
    377  1.1  thorpej  * Always called at splnet().
    378  1.1  thorpej  */
    379  1.1  thorpej void
    380  1.1  thorpej se_ifstart(ifp)
    381  1.1  thorpej 	struct ifnet *ifp;
    382  1.1  thorpej {
    383  1.1  thorpej 	struct se_softc *sc = ifp->if_softc;
    384  1.1  thorpej 	struct scsi_ctron_ether_generic send_cmd;
    385  1.1  thorpej 	struct mbuf *m, *m0;
    386  1.1  thorpej 	int len, error;
    387  1.1  thorpej 	u_char *cp;
    388  1.1  thorpej 
    389  1.1  thorpej 	/* Don't transmit if interface is busy or not running */
    390  1.1  thorpej 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    391  1.1  thorpej 		return;
    392  1.1  thorpej 
    393  1.1  thorpej 	IF_DEQUEUE(&ifp->if_snd, m0);
    394  1.1  thorpej 	if (m0 == 0)
    395  1.1  thorpej 		return;
    396  1.1  thorpej #if NBPFILTER > 0
    397  1.1  thorpej 	/* If BPF is listening on this interface, let it see the
    398  1.1  thorpej 	 * packet before we commit it to the wire.
    399  1.1  thorpej 	 */
    400  1.1  thorpej 	if (ifp->if_bpf)
    401  1.1  thorpej 		bpf_mtap(ifp->if_bpf, m0);
    402  1.1  thorpej #endif
    403  1.1  thorpej 
    404  1.1  thorpej 	/* We need to use m->m_pkthdr.len, so require the header */
    405  1.1  thorpej 	if ((m0->m_flags & M_PKTHDR) == 0)
    406  1.1  thorpej 		panic("ctscstart: no header mbuf");
    407  1.1  thorpej 	len = m0->m_pkthdr.len;
    408  1.1  thorpej 
    409  1.1  thorpej 	/* Mark the interface busy. */
    410  1.1  thorpej 	ifp->if_flags |= IFF_OACTIVE;
    411  1.1  thorpej 
    412  1.1  thorpej 	/* Chain; copy into linear buffer we allocated at attach time. */
    413  1.1  thorpej 	cp = sc->sc_tbuf;
    414  1.1  thorpej 	for (m = m0; m != NULL; ) {
    415  1.1  thorpej 		bcopy(mtod(m, u_char *), cp, m->m_len);
    416  1.1  thorpej 		cp += m->m_len;
    417  1.1  thorpej 		MFREE(m, m0);
    418  1.1  thorpej 		m = m0;
    419  1.1  thorpej 	}
    420  1.1  thorpej 	if (len < SEMINSIZE) {
    421  1.1  thorpej #ifdef SEDEBUG
    422  1.1  thorpej 		if (sc->sc_debug)
    423  1.1  thorpej 			printf("se: packet size %d (%d) < %d\n", len,
    424  1.1  thorpej 			       cp - (u_char *)sc->sc_tbuf, SEMINSIZE);
    425  1.1  thorpej #endif
    426  1.1  thorpej 		bzero(cp, SEMINSIZE - len);
    427  1.1  thorpej 		len = SEMINSIZE;
    428  1.1  thorpej 	}
    429  1.1  thorpej 
    430  1.1  thorpej 	/* Fill out SCSI command. */
    431  1.1  thorpej 	PROTOCMD(ctron_ether_send, send_cmd);
    432  1.1  thorpej 	_lto2b(len, send_cmd.length);
    433  1.1  thorpej 
    434  1.1  thorpej 	/* Send command to device. */
    435  1.1  thorpej 	error = se_scsi_cmd(sc->sc_link,
    436  1.1  thorpej 			    (struct scsi_generic *)&send_cmd, sizeof(send_cmd),
    437  1.1  thorpej 			    sc->sc_tbuf, len, SERETRIES,
    438  1.1  thorpej 			    SETIMEOUT, NULL, SCSI_NOSLEEP|SCSI_DATA_OUT);
    439  1.1  thorpej 	if (error) {
    440  1.1  thorpej 		printf("%s: not queued, error %d\n",
    441  1.1  thorpej 		       sc->sc_dev.dv_xname, error);
    442  1.1  thorpej 		ifp->if_oerrors++;
    443  1.1  thorpej 		ifp->if_flags &= ~IFF_OACTIVE;
    444  1.1  thorpej 	} else
    445  1.1  thorpej 		ifp->if_opackets++;
    446  1.1  thorpej 	if (sc->sc_flags & SE_NEED_RECV) {
    447  1.1  thorpej 		sc->sc_flags &= ~SE_NEED_RECV;
    448  1.1  thorpej 		se_recv((void *) sc);
    449  1.1  thorpej 	}
    450  1.1  thorpej }
    451  1.1  thorpej 
    452  1.1  thorpej 
    453  1.1  thorpej /*
    454  1.1  thorpej  * Called from the scsibus layer via our scsi device switch.
    455  1.1  thorpej  */
    456  1.1  thorpej static int
    457  1.1  thorpej sedone(xs, complete)
    458  1.1  thorpej 	struct scsi_xfer *xs;
    459  1.1  thorpej 	int complete;
    460  1.1  thorpej {
    461  1.1  thorpej 	int error;
    462  1.1  thorpej 	struct se_softc *sc = xs->sc_link->device_softc;
    463  1.1  thorpej 	struct scsi_generic *cmd = xs->cmd;
    464  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    465  1.1  thorpej 	if(complete) {
    466  1.1  thorpej 		/*
    467  1.1  thorpej 		 * "complete" indicates we're freeing resources used
    468  1.1  thorpej 		 * by the transfer.
    469  1.1  thorpej 		 */
    470  1.1  thorpej 		int s = splnet();
    471  1.1  thorpej 		error = !(xs->error == XS_NOERROR);
    472  1.1  thorpej 
    473  1.1  thorpej 		if(IS_SEND(cmd)) {
    474  1.1  thorpej 			if (xs->error == XS_BUSY) {
    475  1.1  thorpej 				printf("se: busy, retry txmit\n");
    476  1.1  thorpej 				timeout(se_delayed_ifstart, ifp, hz);
    477  1.1  thorpej 			} else {
    478  1.1  thorpej 				ifp->if_flags &= ~IFF_OACTIVE;
    479  1.1  thorpej 				/* the generic scsi_done will call
    480  1.1  thorpej 				 * sestart (through scsi_free_xs).
    481  1.1  thorpej 				 */
    482  1.1  thorpej 			}
    483  1.1  thorpej 		} else if(IS_RECV(cmd)) {
    484  1.1  thorpej 			/* RECV complete */
    485  1.1  thorpej 			/* pass data up. reschedule a recv */
    486  1.1  thorpej 			/* scsi_free_xs will call start. Harmless. */
    487  1.1  thorpej 			if (error) {
    488  1.1  thorpej 				/* Reschedule after a delay */
    489  1.1  thorpej 				timeout(se_recv, (void *)sc, se_poll);
    490  1.1  thorpej 			} else {
    491  1.1  thorpej 				int n;
    492  1.1  thorpej 				n = se_read(sc, xs->data, xs->datalen - xs->resid);
    493  1.1  thorpej 
    494  1.1  thorpej 				if(n > 0) {
    495  1.1  thorpej #ifdef SEDEBUG
    496  1.1  thorpej 					if (sc->sc_debug)
    497  1.1  thorpej 						printf("sedone: received %d packets \n", n);
    498  1.1  thorpej #endif
    499  1.1  thorpej 					if(ifp->if_snd.ifq_head)
    500  1.1  thorpej 						/* Output is
    501  1.1  thorpej 						 * pending. Do next
    502  1.1  thorpej 						 * recv after the next
    503  1.1  thorpej 						 * send. */
    504  1.1  thorpej 						sc->sc_flags |= SE_NEED_RECV;
    505  1.1  thorpej 					else {
    506  1.1  thorpej 						timeout(se_recv, (void *)sc, se_poll0);
    507  1.1  thorpej 					}
    508  1.1  thorpej 				} else {
    509  1.1  thorpej 					/* Reschedule after a delay */
    510  1.1  thorpej 					timeout(se_recv, (void *)sc, se_poll);
    511  1.1  thorpej 				}
    512  1.1  thorpej 			}
    513  1.1  thorpej 		}
    514  1.1  thorpej 		splx(s);
    515  1.1  thorpej 	}
    516  1.1  thorpej 	return (0);
    517  1.1  thorpej }
    518  1.1  thorpej 
    519  1.1  thorpej static void se_recv(v)
    520  1.1  thorpej      void *v;
    521  1.1  thorpej {
    522  1.1  thorpej 	/* do a recv command */
    523  1.1  thorpej 	struct se_softc *sc = (struct se_softc *) v;
    524  1.1  thorpej 	struct scsi_ctron_ether_recv recv_cmd;
    525  1.1  thorpej 	int error;
    526  1.1  thorpej 
    527  1.1  thorpej 	PROTOCMD(ctron_ether_recv, recv_cmd);
    528  1.1  thorpej 
    529  1.1  thorpej 	error = se_scsi_cmd(sc->sc_link,
    530  1.1  thorpej 				  (struct scsi_generic *)&recv_cmd,
    531  1.1  thorpej 				  sizeof(recv_cmd),
    532  1.1  thorpej 				  sc->sc_rbuf, RBUF_LEN, SERETRIES,
    533  1.1  thorpej 				  SETIMEOUT, NULL, SCSI_NOSLEEP|SCSI_DATA_IN);
    534  1.1  thorpej 	if (error)
    535  1.1  thorpej 		timeout(se_recv, (void *)sc, se_poll);
    536  1.1  thorpej 
    537  1.1  thorpej }
    538  1.1  thorpej 
    539  1.1  thorpej /*
    540  1.1  thorpej  * We copy the data into mbufs.  When full cluster sized units are present
    541  1.1  thorpej  * we copy into clusters.
    542  1.1  thorpej  */
    543  1.1  thorpej static struct mbuf *
    544  1.1  thorpej se_get(sc, data, totlen)
    545  1.1  thorpej 	struct se_softc *sc;
    546  1.1  thorpej 	char *data;
    547  1.1  thorpej 	int totlen;
    548  1.1  thorpej {
    549  1.1  thorpej 	struct mbuf *m;
    550  1.1  thorpej 	struct mbuf *top, **mp;
    551  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    552  1.1  thorpej 	int len, pad;
    553  1.1  thorpej 
    554  1.1  thorpej 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    555  1.1  thorpej 	if (m == 0)
    556  1.1  thorpej 		return (0);
    557  1.1  thorpej 	m->m_pkthdr.rcvif = ifp;
    558  1.1  thorpej 	m->m_pkthdr.len = totlen;
    559  1.1  thorpej 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
    560  1.1  thorpej 	m->m_data += pad;
    561  1.1  thorpej 	len = MHLEN - pad;
    562  1.1  thorpej 	top = 0;
    563  1.1  thorpej 	mp = &top;
    564  1.1  thorpej 
    565  1.1  thorpej 	while (totlen > 0) {
    566  1.1  thorpej 		if (top) {
    567  1.1  thorpej 			MGET(m, M_DONTWAIT, MT_DATA);
    568  1.1  thorpej 			if (m == 0) {
    569  1.1  thorpej 				m_freem(top);
    570  1.1  thorpej 				return 0;
    571  1.1  thorpej 			}
    572  1.1  thorpej 			len = MLEN;
    573  1.1  thorpej 		}
    574  1.1  thorpej 		if (top && totlen >= MINCLSIZE) {
    575  1.1  thorpej 			MCLGET(m, M_DONTWAIT);
    576  1.1  thorpej 			if (m->m_flags & M_EXT)
    577  1.1  thorpej 				len = MCLBYTES;
    578  1.1  thorpej 		}
    579  1.1  thorpej 		m->m_len = len = min(totlen, len);
    580  1.1  thorpej 		bcopy(data, mtod(m, caddr_t), len);
    581  1.1  thorpej 		data += len;
    582  1.1  thorpej 		totlen -= len;
    583  1.1  thorpej 		*mp = m;
    584  1.1  thorpej 		mp = &m->m_next;
    585  1.1  thorpej 	}
    586  1.1  thorpej 
    587  1.1  thorpej 	return (top);
    588  1.1  thorpej }
    589  1.1  thorpej 
    590  1.1  thorpej /*
    591  1.1  thorpej  * Pass packets to higher levels.
    592  1.1  thorpej  */
    593  1.1  thorpej static int
    594  1.1  thorpej se_read(sc, data, datalen)
    595  1.1  thorpej 	register struct se_softc *sc;
    596  1.1  thorpej 	char *data;
    597  1.1  thorpej 	int datalen;
    598  1.1  thorpej {
    599  1.1  thorpej 	struct mbuf *m;
    600  1.1  thorpej 	struct ether_header *eh;
    601  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    602  1.1  thorpej 	int n;
    603  1.1  thorpej 
    604  1.1  thorpej 	n = 0;
    605  1.1  thorpej 	while (datalen >= 2) {
    606  1.1  thorpej 		int len = _2btol(data);
    607  1.1  thorpej 		data += 2;
    608  1.1  thorpej 		datalen -= 2;
    609  1.1  thorpej 
    610  1.1  thorpej 		if (len == 0)
    611  1.1  thorpej 			break;
    612  1.1  thorpej #ifdef SEDEBUG
    613  1.1  thorpej 		if (sc->sc_debug) {
    614  1.1  thorpej 			printf("se_read: datalen = %d, packetlen = %d, proto = 0x%04x\n", datalen, len,
    615  1.1  thorpej 			 ntohs(((struct ether_header *)data)->ether_type));
    616  1.1  thorpej 		}
    617  1.1  thorpej #endif
    618  1.1  thorpej 		if (len <= sizeof(struct ether_header) ||
    619  1.1  thorpej 		    len > MAX_SNAP) {
    620  1.1  thorpej #ifdef SEDEBUG
    621  1.1  thorpej 			printf("%s: invalid packet size %d; dropping\n",
    622  1.1  thorpej 			       sc->sc_dev.dv_xname, len);
    623  1.1  thorpej #endif
    624  1.1  thorpej 			ifp->if_ierrors++;
    625  1.1  thorpej 			goto next_packet;
    626  1.1  thorpej 		}
    627  1.1  thorpej 
    628  1.1  thorpej 		/* Don't need crc. Must keep ether header for BPF */
    629  1.1  thorpej 		m = se_get(sc, data, len - ETHER_CRC);
    630  1.1  thorpej 		if (m == 0) {
    631  1.1  thorpej #ifdef SEDEBUG
    632  1.1  thorpej 			if (sc->sc_debug) {
    633  1.1  thorpej 				printf("se_read: se_get returned null\n");
    634  1.1  thorpej 			}
    635  1.1  thorpej #endif
    636  1.1  thorpej 			ifp->if_ierrors++;
    637  1.1  thorpej 			goto next_packet;
    638  1.1  thorpej 		}
    639  1.1  thorpej 		if ((ifp->if_flags & IFF_PROMISC) != 0) {
    640  1.1  thorpej 			m_adj(m, SE_PREFIX);
    641  1.1  thorpej 		}
    642  1.1  thorpej 		ifp->if_ipackets++;
    643  1.1  thorpej 
    644  1.1  thorpej 		/* We assume that the header fit entirely in one mbuf. */
    645  1.1  thorpej 		eh = mtod(m, struct ether_header *);
    646  1.1  thorpej 
    647  1.1  thorpej #if NBPFILTER > 0
    648  1.1  thorpej 		/*
    649  1.1  thorpej 		 * Check if there's a BPF listener on this interface.
    650  1.1  thorpej 		 * If so, hand off the raw packet to BPF.
    651  1.1  thorpej 		 */
    652  1.1  thorpej 		if (ifp->if_bpf) {
    653  1.1  thorpej 			bpf_mtap(ifp->if_bpf, m);
    654  1.1  thorpej 
    655  1.1  thorpej 			/* Note that the interface cannot be in
    656  1.1  thorpej 			 * promiscuous mode if there are no BPF
    657  1.1  thorpej 			 * listeners.  And if we are in promiscuous
    658  1.1  thorpej 			 * mode, we have to check if this packet is
    659  1.1  thorpej 			 * really ours.
    660  1.1  thorpej 			 */
    661  1.1  thorpej 			if ((ifp->if_flags & IFF_PROMISC) != 0 &&
    662  1.1  thorpej 			    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    663  1.1  thorpej 			    ETHER_CMP(eh->ether_dhost, LLADDR(ifp->if_sadl))) {
    664  1.1  thorpej 				m_freem(m);
    665  1.1  thorpej 				goto next_packet;
    666  1.1  thorpej 			}
    667  1.1  thorpej 		}
    668  1.1  thorpej #endif
    669  1.1  thorpej 
    670  1.1  thorpej 		/* Pass the packet up, with the ether header sort-of removed. */
    671  1.1  thorpej 		m_adj(m, sizeof(struct ether_header));
    672  1.1  thorpej 		ether_input(ifp, eh, m);
    673  1.1  thorpej 
    674  1.1  thorpej 	next_packet:
    675  1.1  thorpej 		data += len;
    676  1.1  thorpej 		datalen -= len;
    677  1.1  thorpej 		n++;
    678  1.1  thorpej 	}
    679  1.1  thorpej 	return n;
    680  1.1  thorpej }
    681  1.1  thorpej 
    682  1.1  thorpej 
    683  1.1  thorpej void
    684  1.1  thorpej sewatchdog(ifp)
    685  1.1  thorpej 	struct ifnet *ifp;
    686  1.1  thorpej {
    687  1.1  thorpej 	struct se_softc *sc = ifp->if_softc;
    688  1.1  thorpej 
    689  1.1  thorpej 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    690  1.1  thorpej 	++ifp->if_oerrors;
    691  1.1  thorpej 
    692  1.1  thorpej 	se_reset(sc);
    693  1.1  thorpej }
    694  1.1  thorpej 
    695  1.1  thorpej static int
    696  1.1  thorpej se_reset(sc)
    697  1.1  thorpej 	struct se_softc *sc;
    698  1.1  thorpej {
    699  1.1  thorpej 	int error = 0;
    700  1.1  thorpej 	int s = splnet();
    701  1.1  thorpej #if 0
    702  1.1  thorpej 	/* Maybe we don't *really* want to reset the entire bus
    703  1.1  thorpej 	 * because the ctron isn't working. We would like to send a
    704  1.1  thorpej 	 * "BUS DEVICE RESET" message, but don't think the ctron
    705  1.1  thorpej 	 * understands it.
    706  1.1  thorpej 	 */
    707  1.1  thorpej 	error = se_scsi_cmd(sc->sc_link, 0, 0, 0, 0, SERETRIES, 2000, NULL,
    708  1.1  thorpej 	    SCSI_RESET);
    709  1.1  thorpej #endif
    710  1.1  thorpej 	error = se_init(sc);
    711  1.1  thorpej 	splx(s);
    712  1.1  thorpej 	return error;
    713  1.1  thorpej }
    714  1.1  thorpej 
    715  1.1  thorpej static int se_add_proto(sc, proto)
    716  1.1  thorpej      struct se_softc *sc;
    717  1.1  thorpej      int proto;
    718  1.1  thorpej {
    719  1.1  thorpej 	int error = 0;
    720  1.1  thorpej 	struct scsi_ctron_ether_generic add_proto_cmd;
    721  1.1  thorpej 	u_int8_t data[2];
    722  1.1  thorpej 	_lto2b(proto, data);
    723  1.1  thorpej #ifdef SEDEBUG
    724  1.1  thorpej 	if (sc->sc_debug)
    725  1.1  thorpej 		printf("se: adding proto 0x%02x%02x\n", data[0], data[1]);
    726  1.1  thorpej #endif
    727  1.1  thorpej 
    728  1.1  thorpej 	PROTOCMD(ctron_ether_add_proto, add_proto_cmd);
    729  1.1  thorpej 	_lto2b(sizeof(data), add_proto_cmd.length);
    730  1.1  thorpej 	error = se_scsi_cmd(sc->sc_link,
    731  1.1  thorpej 			      (struct scsi_generic *) &add_proto_cmd,
    732  1.1  thorpej 			      sizeof(add_proto_cmd),
    733  1.1  thorpej 			      data,
    734  1.1  thorpej 			      sizeof(data),
    735  1.1  thorpej 			      SERETRIES, SETIMEOUT, NULL,
    736  1.1  thorpej 			      SCSI_DATA_OUT);
    737  1.1  thorpej 	return error;
    738  1.1  thorpej 
    739  1.1  thorpej }
    740  1.1  thorpej 
    741  1.1  thorpej static int se_get_addr(sc, myaddr)
    742  1.1  thorpej      struct se_softc *sc;
    743  1.1  thorpej      u_int8_t *myaddr;
    744  1.1  thorpej {
    745  1.1  thorpej 	int error = 0;
    746  1.1  thorpej 	struct scsi_ctron_ether_generic get_addr_cmd;
    747  1.1  thorpej 
    748  1.1  thorpej 	PROTOCMD(ctron_ether_get_addr, get_addr_cmd);
    749  1.1  thorpej 	_lto2b(ETHER_ADDR_LEN, get_addr_cmd.length);
    750  1.1  thorpej 	error = se_scsi_cmd(sc->sc_link,
    751  1.1  thorpej 			      (struct scsi_generic *) &get_addr_cmd,
    752  1.1  thorpej 			      sizeof(get_addr_cmd),
    753  1.1  thorpej 			      myaddr, ETHER_ADDR_LEN,
    754  1.1  thorpej 			      SERETRIES, SETIMEOUT, NULL,
    755  1.1  thorpej 			      SCSI_DATA_IN);
    756  1.1  thorpej 	printf("%s: ethernet address %s\n", sc->sc_dev.dv_xname,
    757  1.1  thorpej 	    ether_sprintf(myaddr));
    758  1.1  thorpej 	return error;
    759  1.1  thorpej }
    760  1.1  thorpej 
    761  1.1  thorpej 
    762  1.1  thorpej static int se_set_media(sc, type)
    763  1.1  thorpej      struct se_softc *sc;
    764  1.1  thorpej      int type;
    765  1.1  thorpej {
    766  1.1  thorpej 	int error = 0;
    767  1.1  thorpej 	struct scsi_ctron_ether_generic set_media_cmd;
    768  1.1  thorpej 
    769  1.1  thorpej 	PROTOCMD(ctron_ether_set_media, set_media_cmd);
    770  1.1  thorpej 	set_media_cmd.byte3 = type;
    771  1.1  thorpej 	error = se_scsi_cmd(sc->sc_link,
    772  1.1  thorpej 			      (struct scsi_generic *) &set_media_cmd,
    773  1.1  thorpej 			      sizeof(set_media_cmd),
    774  1.1  thorpej 			      0,
    775  1.1  thorpej 			      0,
    776  1.1  thorpej 			      SERETRIES, SETIMEOUT, NULL,
    777  1.1  thorpej 			      0);
    778  1.1  thorpej 	return error;
    779  1.1  thorpej }
    780  1.1  thorpej 
    781  1.1  thorpej static int se_set_mode(sc, len, mode)
    782  1.1  thorpej      struct se_softc *sc;
    783  1.1  thorpej      int len;
    784  1.1  thorpej      int mode;
    785  1.1  thorpej {
    786  1.1  thorpej 	int error = 0;
    787  1.1  thorpej 	struct scsi_ctron_ether_set_mode set_mode_cmd;
    788  1.1  thorpej 
    789  1.1  thorpej 	PROTOCMD(ctron_ether_set_mode, set_mode_cmd);
    790  1.1  thorpej 	set_mode_cmd.mode = mode;
    791  1.1  thorpej 	_lto2b(len, set_mode_cmd.length);
    792  1.1  thorpej 	error = se_scsi_cmd(sc->sc_link,
    793  1.1  thorpej 			      (struct scsi_generic *) &set_mode_cmd,
    794  1.1  thorpej 			      sizeof(set_mode_cmd),
    795  1.1  thorpej 			      0,
    796  1.1  thorpej 			      0,
    797  1.1  thorpej 			      SERETRIES, SETIMEOUT, NULL,
    798  1.1  thorpej 			      0);
    799  1.1  thorpej 	return error;
    800  1.1  thorpej }
    801  1.1  thorpej 
    802  1.1  thorpej 
    803  1.1  thorpej 
    804  1.1  thorpej static int se_init(sc)
    805  1.1  thorpej      struct se_softc *sc;
    806  1.1  thorpej {
    807  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    808  1.1  thorpej 	struct scsi_ctron_ether_generic set_addr_cmd;
    809  1.1  thorpej 	int error;
    810  1.1  thorpej 
    811  1.1  thorpej #if NBPFILTER > 0
    812  1.1  thorpej 	if (ifp->if_flags & IFF_PROMISC) {
    813  1.1  thorpej 		error = se_set_mode(sc, MAX_SNAP, 1);
    814  1.1  thorpej 	}
    815  1.1  thorpej 	else
    816  1.1  thorpej #endif
    817  1.1  thorpej 		error = se_set_mode(sc, ETHERMTU + sizeof(struct ether_header),
    818  1.1  thorpej 				    0);
    819  1.1  thorpej 	if(error != 0)
    820  1.1  thorpej 		return error;
    821  1.1  thorpej 
    822  1.1  thorpej 	PROTOCMD(ctron_ether_set_addr, set_addr_cmd);
    823  1.1  thorpej 	_lto2b(ETHER_ADDR_LEN, set_addr_cmd.length);
    824  1.1  thorpej 	error = se_scsi_cmd(sc->sc_link,
    825  1.1  thorpej 			      (struct scsi_generic *) &set_addr_cmd,
    826  1.1  thorpej 			      sizeof(set_addr_cmd),
    827  1.1  thorpej 			      LLADDR(ifp->if_sadl), ETHER_ADDR_LEN,
    828  1.1  thorpej 			      SERETRIES, SETIMEOUT, NULL,
    829  1.1  thorpej 			      SCSI_DATA_OUT);
    830  1.1  thorpej 	if(error != 0) {
    831  1.1  thorpej 		return error;
    832  1.1  thorpej 	}
    833  1.1  thorpej 
    834  1.1  thorpej 	if ((sc->protos & PROTO_IP) &&
    835  1.1  thorpej 	    (error = se_add_proto(sc, ETHERTYPE_IP)) != 0)
    836  1.1  thorpej 		return error;
    837  1.1  thorpej 	if ((sc->protos & PROTO_ARP) &&
    838  1.1  thorpej 	    (error = se_add_proto(sc, ETHERTYPE_ARP)) != 0)
    839  1.1  thorpej 		return error;
    840  1.1  thorpej 	if ((sc->protos & PROTO_REVARP) &&
    841  1.1  thorpej 	    (error = se_add_proto(sc, ETHERTYPE_REVARP)) != 0)
    842  1.1  thorpej 		return error;
    843  1.1  thorpej 
    844  1.1  thorpej 	if((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == IFF_UP) {
    845  1.1  thorpej 		ifp->if_flags |= IFF_RUNNING;
    846  1.1  thorpej 		se_recv(sc);
    847  1.1  thorpej 		ifp->if_flags &= ~IFF_OACTIVE;
    848  1.1  thorpej 		se_ifstart(ifp);
    849  1.1  thorpej 	}
    850  1.1  thorpej 	return error;
    851  1.1  thorpej }
    852  1.1  thorpej 
    853  1.1  thorpej static int se_set_multi(sc, addr)
    854  1.1  thorpej      struct se_softc *sc;
    855  1.1  thorpej      u_int8_t *addr;
    856  1.1  thorpej {
    857  1.1  thorpej 	struct scsi_ctron_ether_generic set_multi_cmd;
    858  1.1  thorpej 	int error;
    859  1.1  thorpej 
    860  1.1  thorpej 	if (sc->sc_debug)
    861  1.1  thorpej 		printf("%s: set_set_multi: %s\n", sc->sc_dev.dv_xname,
    862  1.1  thorpej 		    ether_sprintf(addr));
    863  1.1  thorpej 
    864  1.1  thorpej 	PROTOCMD(ctron_ether_set_multi, set_multi_cmd);
    865  1.1  thorpej 	_lto2b(sizeof(addr), set_multi_cmd.length);
    866  1.1  thorpej 	error = se_scsi_cmd(sc->sc_link,
    867  1.1  thorpej 			      (struct scsi_generic *) &set_multi_cmd,
    868  1.1  thorpej 			      sizeof(set_multi_cmd),
    869  1.1  thorpej 			      addr,
    870  1.1  thorpej 			      sizeof(addr),
    871  1.1  thorpej 			      SERETRIES, SETIMEOUT, NULL,
    872  1.1  thorpej 			      SCSI_DATA_OUT);
    873  1.1  thorpej 	return error;
    874  1.1  thorpej }
    875  1.1  thorpej 
    876  1.1  thorpej static int se_remove_multi(sc, addr)
    877  1.1  thorpej      struct se_softc *sc;
    878  1.1  thorpej      u_int8_t *addr;
    879  1.1  thorpej {
    880  1.1  thorpej 	struct scsi_ctron_ether_generic remove_multi_cmd;
    881  1.1  thorpej 	int error;
    882  1.1  thorpej 
    883  1.1  thorpej 	if (sc->sc_debug)
    884  1.1  thorpej 		printf("%s: se_remove_multi: %s\n", sc->sc_dev.dv_xname,
    885  1.1  thorpej 		    ether_sprintf(addr));
    886  1.1  thorpej 
    887  1.1  thorpej 	PROTOCMD(ctron_ether_remove_multi, remove_multi_cmd);
    888  1.1  thorpej 	_lto2b(sizeof(addr), remove_multi_cmd.length);
    889  1.1  thorpej 	error = se_scsi_cmd(sc->sc_link,
    890  1.1  thorpej 			      (struct scsi_generic *) &remove_multi_cmd,
    891  1.1  thorpej 			      sizeof(remove_multi_cmd),
    892  1.1  thorpej 			      addr,
    893  1.1  thorpej 			      sizeof(addr),
    894  1.1  thorpej 			      SERETRIES, SETIMEOUT, NULL,
    895  1.1  thorpej 			      SCSI_DATA_OUT);
    896  1.1  thorpej 	return error;
    897  1.1  thorpej }
    898  1.1  thorpej 
    899  1.1  thorpej #if 0	/* not used  --thorpej */
    900  1.1  thorpej static int sc_set_all_multi(sc, set)
    901  1.1  thorpej      struct se_softc *sc;
    902  1.1  thorpej      int set;
    903  1.1  thorpej {
    904  1.1  thorpej 	int error = 0;
    905  1.1  thorpej 	u_int8_t *addr;
    906  1.1  thorpej 	struct ethercom *ac = &sc->sc_ethercom;
    907  1.1  thorpej 	struct ether_multi *enm;
    908  1.1  thorpej 	struct ether_multistep step;
    909  1.1  thorpej 	ETHER_FIRST_MULTI(step, ac, enm);
    910  1.1  thorpej 	while (enm != NULL) {
    911  1.1  thorpej 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    912  1.1  thorpej 			/*
    913  1.1  thorpej 			 * We must listen to a range of multicast addresses.
    914  1.1  thorpej 			 * For now, just accept all multicasts, rather than
    915  1.1  thorpej 			 * trying to set only those filter bits needed to match
    916  1.1  thorpej 			 * the range.  (At this time, the only use of address
    917  1.1  thorpej 			 * ranges is for IP multicast routing, for which the
    918  1.1  thorpej 			 * range is big enough to require all bits set.)
    919  1.1  thorpej 			 */
    920  1.1  thorpej 			/* We have no way of adding a range to this device.
    921  1.1  thorpej 			 * stepping through all addresses in the range is
    922  1.1  thorpej 			 * typically not possible. The only real alternative
    923  1.1  thorpej 			 * is to go into promicuous mode and filter by hand.
    924  1.1  thorpej 			 */
    925  1.1  thorpej 			return ENODEV;
    926  1.1  thorpej 
    927  1.1  thorpej 		}
    928  1.1  thorpej 
    929  1.1  thorpej 		addr = enm->enm_addrlo;
    930  1.1  thorpej 		if((error = set? se_set_multi(sc, addr): se_remove_multi(sc, addr)) != 0)
    931  1.1  thorpej 			return error;
    932  1.1  thorpej 		ETHER_NEXT_MULTI(step, enm);
    933  1.1  thorpej 	}
    934  1.1  thorpej 	return error;
    935  1.1  thorpej }
    936  1.1  thorpej #endif /* not used */
    937  1.1  thorpej 
    938  1.1  thorpej static void se_stop(sc)
    939  1.1  thorpej      struct se_softc *sc;
    940  1.1  thorpej {
    941  1.1  thorpej 	/* Don't schedule any reads */
    942  1.1  thorpej 	untimeout(se_recv, sc);
    943  1.1  thorpej 
    944  1.1  thorpej 	/* How can we abort any scsi cmds in progress? */
    945  1.1  thorpej }
    946  1.1  thorpej 
    947  1.1  thorpej 
    948  1.1  thorpej /*
    949  1.1  thorpej  * Process an ioctl request.
    950  1.1  thorpej  */
    951  1.1  thorpej int
    952  1.1  thorpej se_ioctl(ifp, cmd, data)
    953  1.1  thorpej 	register struct ifnet *ifp;
    954  1.1  thorpej 	u_long cmd;
    955  1.1  thorpej 	caddr_t data;
    956  1.1  thorpej {
    957  1.1  thorpej 	register struct se_softc *sc = ifp->if_softc;
    958  1.1  thorpej 	struct ifaddr *ifa = (struct ifaddr *)data;
    959  1.1  thorpej 	struct ifreq *ifr = (struct ifreq *)data;
    960  1.1  thorpej 	int s, error = 0;
    961  1.1  thorpej 
    962  1.1  thorpej 	s = splnet();
    963  1.1  thorpej 
    964  1.1  thorpej 	switch (cmd) {
    965  1.1  thorpej 
    966  1.1  thorpej 	case SIOCSIFADDR:
    967  1.1  thorpej 		ifp->if_flags |= IFF_UP;
    968  1.1  thorpej 
    969  1.1  thorpej 		if ((error = se_set_media(sc, CMEDIA_AUTOSENSE) != 0))
    970  1.1  thorpej 			return error;
    971  1.1  thorpej 
    972  1.1  thorpej 		switch (ifa->ifa_addr->sa_family) {
    973  1.1  thorpej #ifdef INET
    974  1.1  thorpej 		case AF_INET:
    975  1.1  thorpej 			sc->protos |= (PROTO_IP | PROTO_ARP | PROTO_REVARP);
    976  1.1  thorpej 			if ((error = se_init(sc)) != 0)
    977  1.1  thorpej 				break;
    978  1.1  thorpej 			arp_ifinit(ifp, ifa);
    979  1.1  thorpej 			break;
    980  1.1  thorpej #endif
    981  1.1  thorpej #ifdef NS
    982  1.1  thorpej 		case AF_NS:
    983  1.1  thorpej 		    {
    984  1.1  thorpej 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    985  1.1  thorpej 
    986  1.1  thorpej 			if (ns_nullhost(*ina))
    987  1.1  thorpej 				ina->x_host =
    988  1.1  thorpej 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    989  1.1  thorpej 			else
    990  1.1  thorpej 				bcopy(ina->x_host.c_host,
    991  1.1  thorpej 				    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
    992  1.1  thorpej 			/* Set new address. */
    993  1.1  thorpej 
    994  1.1  thorpej 			error = se_init(sc);
    995  1.1  thorpej 			break;
    996  1.1  thorpej 		    }
    997  1.1  thorpej #endif
    998  1.1  thorpej 		default:
    999  1.1  thorpej 			error = se_init(sc);
   1000  1.1  thorpej 			break;
   1001  1.1  thorpej 		}
   1002  1.1  thorpej 		break;
   1003  1.1  thorpej 
   1004  1.1  thorpej #if defined(CCITT) && defined(LLC)
   1005  1.1  thorpej 	case SIOCSIFCONF_X25:
   1006  1.1  thorpej 		ifp->if_flags |= IFF_UP;
   1007  1.1  thorpej 		ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
   1008  1.1  thorpej 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
   1009  1.1  thorpej 		if (error == 0)
   1010  1.1  thorpej 			error = se_init(sc);
   1011  1.1  thorpej 		break;
   1012  1.1  thorpej #endif /* CCITT && LLC */
   1013  1.1  thorpej 
   1014  1.1  thorpej 	case SIOCSIFFLAGS:
   1015  1.1  thorpej 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1016  1.1  thorpej 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1017  1.1  thorpej 			/*
   1018  1.1  thorpej 			 * If interface is marked down and it is running, then
   1019  1.1  thorpej 			 * stop it.
   1020  1.1  thorpej 			 */
   1021  1.1  thorpej 			se_stop(sc);
   1022  1.1  thorpej 			ifp->if_flags &= ~IFF_RUNNING;
   1023  1.1  thorpej 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1024  1.1  thorpej 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
   1025  1.1  thorpej 			/*
   1026  1.1  thorpej 			 * If interface is marked up and it is stopped, then
   1027  1.1  thorpej 			 * start it.
   1028  1.1  thorpej 			 */
   1029  1.1  thorpej 			error = se_init(sc);
   1030  1.1  thorpej 		} else {
   1031  1.1  thorpej 			/*
   1032  1.1  thorpej 			 * Reset the interface to pick up changes in any other
   1033  1.1  thorpej 			 * flags that affect hardware registers.
   1034  1.1  thorpej 			 */
   1035  1.1  thorpej 			error = se_init(sc);
   1036  1.1  thorpej 		}
   1037  1.1  thorpej #ifdef SEDEBUG
   1038  1.1  thorpej 		if (ifp->if_flags & IFF_DEBUG)
   1039  1.1  thorpej 			sc->sc_debug = 1;
   1040  1.1  thorpej 		else
   1041  1.1  thorpej 			sc->sc_debug = 0;
   1042  1.1  thorpej #endif
   1043  1.1  thorpej 		break;
   1044  1.1  thorpej 
   1045  1.1  thorpej 	case SIOCADDMULTI:
   1046  1.1  thorpej 		if (ether_addmulti(ifr, &sc->sc_ethercom) == ENETRESET) {
   1047  1.1  thorpej 			error = se_set_multi(sc, ifr->ifr_addr.sa_data);
   1048  1.1  thorpej 		} else {
   1049  1.1  thorpej 			error = 0;
   1050  1.1  thorpej 		}
   1051  1.1  thorpej 		break;
   1052  1.1  thorpej 	case SIOCDELMULTI:
   1053  1.1  thorpej 		if (ether_delmulti(ifr, &sc->sc_ethercom) == ENETRESET) {
   1054  1.1  thorpej 			error = se_remove_multi(sc, ifr->ifr_addr.sa_data);
   1055  1.1  thorpej 		} else {
   1056  1.1  thorpej 			error = 0;
   1057  1.1  thorpej 		}
   1058  1.1  thorpej 		break;
   1059  1.1  thorpej 
   1060  1.1  thorpej 	default:
   1061  1.1  thorpej 
   1062  1.1  thorpej 		error = EINVAL;
   1063  1.1  thorpej 		break;
   1064  1.1  thorpej 	}
   1065  1.1  thorpej 
   1066  1.1  thorpej 	splx(s);
   1067  1.1  thorpej 	return (error);
   1068  1.1  thorpej }
   1069  1.1  thorpej 
   1070  1.1  thorpej #define	SEUNIT(z)	(minor(z))
   1071  1.1  thorpej /*
   1072  1.1  thorpej  * open the device.
   1073  1.1  thorpej  */
   1074  1.1  thorpej int
   1075  1.1  thorpej seopen(dev, flag, fmt, p)
   1076  1.1  thorpej 	dev_t dev;
   1077  1.1  thorpej 	int flag, fmt;
   1078  1.1  thorpej 	struct proc *p;
   1079  1.1  thorpej {
   1080  1.1  thorpej 	int unit;
   1081  1.1  thorpej 	struct se_softc *sc;
   1082  1.1  thorpej 	struct scsi_link *sc_link;
   1083  1.1  thorpej 
   1084  1.1  thorpej 	unit = SEUNIT(dev);
   1085  1.1  thorpej 	if (unit >= se_cd.cd_ndevs)
   1086  1.1  thorpej 		return ENXIO;
   1087  1.1  thorpej 	sc = se_cd.cd_devs[unit];
   1088  1.1  thorpej 	if (!sc)
   1089  1.1  thorpej 		return ENXIO;
   1090  1.1  thorpej 
   1091  1.1  thorpej 	sc_link = sc->sc_link;
   1092  1.1  thorpej 
   1093  1.1  thorpej 	SC_DEBUG(sc_link, SDEV_DB1,
   1094  1.1  thorpej 	    ("scopen: dev=0x%x (unit %d (of %d))\n", dev, unit, se_cd.cd_ndevs));
   1095  1.1  thorpej 
   1096  1.1  thorpej 	sc_link->flags |= SDEV_OPEN;
   1097  1.1  thorpej 
   1098  1.1  thorpej 	SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
   1099  1.1  thorpej 	return 0;
   1100  1.1  thorpej }
   1101  1.1  thorpej 
   1102  1.1  thorpej /*
   1103  1.1  thorpej  * close the device.. only called if we are the LAST
   1104  1.1  thorpej  * occurence of an open device
   1105  1.1  thorpej  */
   1106  1.1  thorpej int
   1107  1.1  thorpej seclose(dev, flag, fmt, p)
   1108  1.1  thorpej 	dev_t dev;
   1109  1.1  thorpej 	int flag, fmt;
   1110  1.1  thorpej 	struct proc *p;
   1111  1.1  thorpej {
   1112  1.1  thorpej 	struct se_softc *sc = se_cd.cd_devs[SEUNIT(dev)];
   1113  1.1  thorpej 
   1114  1.1  thorpej 	SC_DEBUG(sc->sc_link, SDEV_DB1, ("closing\n"));
   1115  1.1  thorpej 	sc->sc_link->flags &= ~SDEV_OPEN;
   1116  1.1  thorpej 
   1117  1.1  thorpej 	return 0;
   1118  1.1  thorpej }
   1119  1.1  thorpej 
   1120  1.1  thorpej /*
   1121  1.1  thorpej  * Perform special action on behalf of the user
   1122  1.1  thorpej  * Only does generic scsi ioctls.
   1123  1.1  thorpej  */
   1124  1.1  thorpej int
   1125  1.1  thorpej seioctl(dev, cmd, addr, flag, p)
   1126  1.1  thorpej 	dev_t dev;
   1127  1.1  thorpej 	u_long cmd;
   1128  1.1  thorpej 	caddr_t addr;
   1129  1.1  thorpej 	int flag;
   1130  1.1  thorpej 	struct proc *p;
   1131  1.1  thorpej {
   1132  1.1  thorpej 	register struct se_softc *sc = se_cd.cd_devs[SEUNIT(dev)];
   1133  1.1  thorpej 
   1134  1.1  thorpej 	return scsi_do_ioctl(sc->sc_link, dev, cmd, addr, flag, p);
   1135  1.1  thorpej }
   1136