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