Home | History | Annotate | Line # | Download | only in scsipi
if_se.c revision 1.96
      1 /*	$NetBSD: if_se.c,v 1.96 2018/06/22 04:17:42 msaitoh 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 <sys/cdefs.h>
     62 __KERNEL_RCSID(0, "$NetBSD: if_se.c,v 1.96 2018/06/22 04:17:42 msaitoh Exp $");
     63 
     64 #ifdef _KERNEL_OPT
     65 #include "opt_inet.h"
     66 #include "opt_atalk.h"
     67 #endif
     68 
     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 #include <net/bpf.h>
     99 
    100 #ifdef INET
    101 #include <netinet/in.h>
    102 #include <netinet/if_inarp.h>
    103 #endif
    104 
    105 
    106 #ifdef NETATALK
    107 #include <netatalk/at.h>
    108 #endif
    109 
    110 
    111 #define SETIMEOUT	1000
    112 #define	SEOUTSTANDING	4
    113 #define	SERETRIES	4
    114 #define SE_PREFIX	4
    115 #define ETHER_CRC	4
    116 #define SEMINSIZE	60
    117 
    118 /* Make this big enough for an ETHERMTU packet in promiscuous mode. */
    119 #define MAX_SNAP	(ETHERMTU + sizeof(struct ether_header) + \
    120 			 SE_PREFIX + ETHER_CRC)
    121 
    122 /* 10 full length packets appears to be the max ever returned. 16k is OK */
    123 #define RBUF_LEN	(16 * 1024)
    124 
    125 /* Tuning parameters:
    126  * The EA41x only returns a maximum of 10 packets (regardless of size).
    127  * We will attempt to adapt to polling fast enough to get RDATA_GOAL packets
    128  * per read
    129  */
    130 #define RDATA_MAX 10
    131 #define RDATA_GOAL 8
    132 
    133 /* se_poll and se_poll0 are the normal polling rate and the minimum
    134  * polling rate respectively. se_poll0 should be chosen so that at
    135  * maximum ethernet speed, we will read nearly RDATA_MAX packets. se_poll
    136  * should be chosen for reasonable maximum latency.
    137  * In practice, if we are being saturated with min length packets, we
    138  * can't poll fast enough. Polling with zero delay actually
    139  * worsens performance. se_poll0 is enforced to be always at least 1
    140  */
    141 #define SE_POLL 40		/* default in milliseconds */
    142 #define SE_POLL0 10		/* default in milliseconds */
    143 int se_poll = 0;		/* Delay in ticks set at attach time */
    144 int se_poll0 = 0;
    145 int se_max_received = 0;	/* Instrumentation */
    146 
    147 #define	PROTOCMD(p, d) \
    148 	((d) = (p))
    149 
    150 #define	PROTOCMD_DECL(name) \
    151 	static const struct scsi_ctron_ether_generic name
    152 
    153 #define	PROTOCMD_DECL_SPECIAL(name) \
    154 	static const struct __CONCAT(scsi_,name) name
    155 
    156 /* Command initializers for commands using scsi_ctron_ether_generic */
    157 PROTOCMD_DECL(ctron_ether_send)  = {CTRON_ETHER_SEND, 0, {0,0}, 0};
    158 PROTOCMD_DECL(ctron_ether_add_proto) = {CTRON_ETHER_ADD_PROTO, 0, {0,0}, 0};
    159 PROTOCMD_DECL(ctron_ether_get_addr) = {CTRON_ETHER_GET_ADDR, 0, {0,0}, 0};
    160 PROTOCMD_DECL(ctron_ether_set_media) = {CTRON_ETHER_SET_MEDIA, 0, {0,0}, 0};
    161 PROTOCMD_DECL(ctron_ether_set_addr) = {CTRON_ETHER_SET_ADDR, 0, {0,0}, 0};
    162 PROTOCMD_DECL(ctron_ether_set_multi) = {CTRON_ETHER_SET_MULTI, 0, {0,0}, 0};
    163 PROTOCMD_DECL(ctron_ether_remove_multi) =
    164     {CTRON_ETHER_REMOVE_MULTI, 0, {0,0}, 0};
    165 
    166 /* Command initializers for commands using their own structures */
    167 PROTOCMD_DECL_SPECIAL(ctron_ether_recv) = {CTRON_ETHER_RECV};
    168 PROTOCMD_DECL_SPECIAL(ctron_ether_set_mode) =
    169     {CTRON_ETHER_SET_MODE, 0, {0,0}, 0};
    170 
    171 struct se_softc {
    172 	device_t sc_dev;
    173 	struct ethercom sc_ethercom;	/* Ethernet common part */
    174 	struct scsipi_periph *sc_periph;/* contains our targ, lun, etc. */
    175 
    176 	struct callout sc_ifstart_ch;
    177 	struct callout sc_recv_ch;
    178 
    179 	char *sc_tbuf;
    180 	char *sc_rbuf;
    181 	int protos;
    182 #define PROTO_IP	0x01
    183 #define PROTO_ARP	0x02
    184 #define PROTO_REVARP	0x04
    185 #define PROTO_AT	0x08
    186 #define PROTO_AARP	0x10
    187 	int sc_debug;
    188 	int sc_flags;
    189 #define SE_NEED_RECV 0x1
    190 	int sc_last_timeout;
    191 	int sc_enabled;
    192 };
    193 
    194 static int	sematch(device_t, cfdata_t, void *);
    195 static void	seattach(device_t, device_t, void *);
    196 
    197 static void	se_ifstart(struct ifnet *);
    198 static void	sestart(struct scsipi_periph *);
    199 
    200 static void	sedone(struct scsipi_xfer *, int);
    201 static int	se_ioctl(struct ifnet *, u_long, void *);
    202 static void	sewatchdog(struct ifnet *);
    203 
    204 #if 0
    205 static inline u_int16_t ether_cmp(void *, void *);
    206 #endif
    207 static void	se_recv(void *);
    208 static struct mbuf *se_get(struct se_softc *, char *, int);
    209 static int	se_read(struct se_softc *, char *, int);
    210 static int	se_reset(struct se_softc *);
    211 static int	se_add_proto(struct se_softc *, int);
    212 static int	se_get_addr(struct se_softc *, u_int8_t *);
    213 static int	se_set_media(struct se_softc *, int);
    214 static int	se_init(struct se_softc *);
    215 static int	se_set_multi(struct se_softc *, u_int8_t *);
    216 static int	se_remove_multi(struct se_softc *, u_int8_t *);
    217 #if 0
    218 static int	sc_set_all_multi(struct se_softc *, int);
    219 #endif
    220 static void	se_stop(struct se_softc *);
    221 static inline int se_scsipi_cmd(struct scsipi_periph *periph,
    222 			struct scsipi_generic *scsipi_cmd,
    223 			int cmdlen, u_char *data_addr, int datalen,
    224 			int retries, int timeout, struct buf *bp,
    225 			int flags);
    226 static void	se_delayed_ifstart(void *);
    227 static int	se_set_mode(struct se_softc *, int, int);
    228 
    229 int	se_enable(struct se_softc *);
    230 void	se_disable(struct se_softc *);
    231 
    232 CFATTACH_DECL_NEW(se, sizeof(struct se_softc),
    233     sematch, seattach, NULL, NULL);
    234 
    235 extern struct cfdriver se_cd;
    236 
    237 dev_type_open(seopen);
    238 dev_type_close(seclose);
    239 dev_type_ioctl(seioctl);
    240 
    241 const struct cdevsw se_cdevsw = {
    242 	.d_open = seopen,
    243 	.d_close = seclose,
    244 	.d_read = noread,
    245 	.d_write = nowrite,
    246 	.d_ioctl = seioctl,
    247 	.d_stop = nostop,
    248 	.d_tty = notty,
    249 	.d_poll = nopoll,
    250 	.d_mmap = nommap,
    251 	.d_kqfilter = nokqfilter,
    252 	.d_discard = nodiscard,
    253 	.d_flag = D_OTHER
    254 };
    255 
    256 const struct scsipi_periphsw se_switch = {
    257 	NULL,			/* Use default error handler */
    258 	sestart,		/* have a queue, served by this */
    259 	NULL,			/* have no async handler */
    260 	sedone,			/* deal with stats at interrupt time */
    261 };
    262 
    263 const struct scsipi_inquiry_pattern se_patterns[] = {
    264 	{T_PROCESSOR, T_FIXED,
    265 	 "CABLETRN",         "EA412",                 ""},
    266 	{T_PROCESSOR, T_FIXED,
    267 	 "Cabletrn",         "EA412",                 ""},
    268 };
    269 
    270 #if 0
    271 /*
    272  * Compare two Ether/802 addresses for equality, inlined and
    273  * unrolled for speed.
    274  * Note: use this like memcmp()
    275  */
    276 static inline u_int16_t
    277 ether_cmp(void *one, void *two)
    278 {
    279 	u_int16_t *a = (u_int16_t *) one;
    280 	u_int16_t *b = (u_int16_t *) two;
    281 	u_int16_t diff;
    282 
    283 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
    284 
    285 	return (diff);
    286 }
    287 
    288 #define ETHER_CMP	ether_cmp
    289 #endif
    290 
    291 static int
    292 sematch(device_t parent, cfdata_t match, void *aux)
    293 {
    294 	struct scsipibus_attach_args *sa = aux;
    295 	int priority;
    296 
    297 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    298 	    se_patterns, sizeof(se_patterns) / sizeof(se_patterns[0]),
    299 	    sizeof(se_patterns[0]), &priority);
    300 	return (priority);
    301 }
    302 
    303 /*
    304  * The routine called by the low level scsi routine when it discovers
    305  * a device suitable for this driver.
    306  */
    307 static void
    308 seattach(device_t parent, device_t self, void *aux)
    309 {
    310 	struct se_softc *sc = device_private(self);
    311 	struct scsipibus_attach_args *sa = aux;
    312 	struct scsipi_periph *periph = sa->sa_periph;
    313 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    314 	u_int8_t myaddr[ETHER_ADDR_LEN];
    315 	int rv;
    316 
    317 	sc->sc_dev = self;
    318 
    319 	printf("\n");
    320 	SC_DEBUG(periph, SCSIPI_DB2, ("seattach: "));
    321 
    322 	callout_init(&sc->sc_ifstart_ch, 0);
    323 	callout_init(&sc->sc_recv_ch, 0);
    324 
    325 
    326 	/*
    327 	 * Store information needed to contact our base driver
    328 	 */
    329 	sc->sc_periph = periph;
    330 	periph->periph_dev = sc->sc_dev;
    331 	periph->periph_switch = &se_switch;
    332 
    333 	/* XXX increase openings? */
    334 
    335 	se_poll = (SE_POLL * hz) / 1000;
    336 	se_poll = se_poll? se_poll: 1;
    337 	se_poll0 = (SE_POLL0 * hz) / 1000;
    338 	se_poll0 = se_poll0? se_poll0: 1;
    339 
    340 	/*
    341 	 * Initialize and attach a buffer
    342 	 */
    343 	sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header),
    344 			     M_DEVBUF, M_NOWAIT);
    345 	if (sc->sc_tbuf == 0)
    346 		panic("seattach: can't allocate transmit buffer");
    347 
    348 	sc->sc_rbuf = malloc(RBUF_LEN, M_DEVBUF, M_NOWAIT);/* A Guess */
    349 	if (sc->sc_rbuf == 0)
    350 		panic("seattach: can't allocate receive buffer");
    351 
    352 	se_get_addr(sc, myaddr);
    353 
    354 	/* Initialize ifnet structure. */
    355 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), sizeof(ifp->if_xname));
    356 	ifp->if_softc = sc;
    357 	ifp->if_start = se_ifstart;
    358 	ifp->if_ioctl = se_ioctl;
    359 	ifp->if_watchdog = sewatchdog;
    360 	ifp->if_flags =
    361 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    362 	IFQ_SET_READY(&ifp->if_snd);
    363 
    364 	/* Attach the interface. */
    365 	rv = if_initialize(ifp);
    366 	if (rv != 0) {
    367 		free(sc->sc_tbuf, M_DEVBUF);
    368 		callout_destroy(&sc->sc_ifstart_ch);
    369 		callout_destroy(&sc->sc_recv_ch);
    370 		return; /* Error */
    371 	}
    372 	ether_ifattach(ifp, myaddr);
    373 	if_register(ifp);
    374 }
    375 
    376 
    377 static inline int
    378 se_scsipi_cmd(struct scsipi_periph *periph, struct scsipi_generic *cmd,
    379     int cmdlen, u_char *data_addr, int datalen, int retries, int timeout,
    380     struct buf *bp, int flags)
    381 {
    382 	int error;
    383 
    384 	error = scsipi_command(periph, cmd, cmdlen, data_addr,
    385 	    datalen, retries, timeout, bp, flags);
    386 	return (error);
    387 }
    388 
    389 /* Start routine for calling from scsi sub system */
    390 static void
    391 sestart(struct scsipi_periph *periph)
    392 {
    393 	struct se_softc *sc = device_private(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(void *v)
    403 {
    404 	struct ifnet *ifp = v;
    405 	struct se_softc *sc = ifp->if_softc;
    406 	int s;
    407 
    408 	s = splnet();
    409 	if (sc->sc_enabled) {
    410 		ifp->if_flags &= ~IFF_OACTIVE;
    411 		se_ifstart(ifp);
    412 	}
    413 	splx(s);
    414 }
    415 
    416 /*
    417  * Start transmission on the interface.
    418  * Always called at splnet().
    419  */
    420 static void
    421 se_ifstart(struct ifnet *ifp)
    422 {
    423 	struct se_softc *sc = ifp->if_softc;
    424 	struct scsi_ctron_ether_generic send_cmd;
    425 	struct mbuf *m, *m0;
    426 	int len, error;
    427 	u_char *cp;
    428 
    429 	/* Don't transmit if interface is busy or not running */
    430 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    431 		return;
    432 
    433 	IFQ_DEQUEUE(&ifp->if_snd, m0);
    434 	if (m0 == 0)
    435 		return;
    436 	/* If BPF is listening on this interface, let it see the
    437 	 * packet before we commit it to the wire.
    438 	 */
    439 	bpf_mtap(ifp, m0);
    440 
    441 	/* We need to use m->m_pkthdr.len, so require the header */
    442 	if ((m0->m_flags & M_PKTHDR) == 0)
    443 		panic("ctscstart: no header mbuf");
    444 	len = m0->m_pkthdr.len;
    445 
    446 	/* Mark the interface busy. */
    447 	ifp->if_flags |= IFF_OACTIVE;
    448 
    449 	/* Chain; copy into linear buffer we allocated at attach time. */
    450 	cp = sc->sc_tbuf;
    451 	for (m = m0; m != NULL; ) {
    452 		memcpy(cp, mtod(m, u_char *), m->m_len);
    453 		cp += m->m_len;
    454 		m = m0 = m_free(m);
    455 	}
    456 	if (len < SEMINSIZE) {
    457 #ifdef SEDEBUG
    458 		if (sc->sc_debug)
    459 			printf("se: packet size %d (%zu) < %d\n", len,
    460 			    cp - (u_char *)sc->sc_tbuf, SEMINSIZE);
    461 #endif
    462 		memset(cp, 0, SEMINSIZE - len);
    463 		len = SEMINSIZE;
    464 	}
    465 
    466 	/* Fill out SCSI command. */
    467 	PROTOCMD(ctron_ether_send, send_cmd);
    468 	_lto2b(len, send_cmd.length);
    469 
    470 	/* Send command to device. */
    471 	error = se_scsipi_cmd(sc->sc_periph,
    472 	    (void *)&send_cmd, sizeof(send_cmd),
    473 	    sc->sc_tbuf, len, SERETRIES,
    474 	    SETIMEOUT, NULL, XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_DATA_OUT);
    475 	if (error) {
    476 		aprint_error_dev(sc->sc_dev, "not queued, error %d\n", error);
    477 		ifp->if_oerrors++;
    478 		ifp->if_flags &= ~IFF_OACTIVE;
    479 	} else
    480 		ifp->if_opackets++;
    481 	if (sc->sc_flags & SE_NEED_RECV) {
    482 		sc->sc_flags &= ~SE_NEED_RECV;
    483 		se_recv((void *) sc);
    484 	}
    485 }
    486 
    487 
    488 /*
    489  * Called from the scsibus layer via our scsi device switch.
    490  */
    491 static void
    492 sedone(struct scsipi_xfer *xs, int error)
    493 {
    494 	struct se_softc *sc = device_private(xs->xs_periph->periph_dev);
    495 	struct scsipi_generic *cmd = xs->cmd;
    496 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    497 	int s;
    498 
    499 	s = splnet();
    500 	if(IS_SEND(cmd)) {
    501 		if (xs->error == XS_BUSY) {
    502 			printf("se: busy, retry txmit\n");
    503 			callout_reset(&sc->sc_ifstart_ch, hz,
    504 			    se_delayed_ifstart, ifp);
    505 		} else {
    506 			ifp->if_flags &= ~IFF_OACTIVE;
    507 			/* the generic scsipi_done will call
    508 			 * sestart (through scsipi_free_xs).
    509 			 */
    510 		}
    511 	} else if(IS_RECV(cmd)) {
    512 		/* RECV complete */
    513 		/* pass data up. reschedule a recv */
    514 		/* scsipi_free_xs will call start. Harmless. */
    515 		if (error) {
    516 			/* Reschedule after a delay */
    517 			callout_reset(&sc->sc_recv_ch, se_poll,
    518 			    se_recv, (void *)sc);
    519 		} else {
    520 			int n, ntimeo;
    521 			n = se_read(sc, xs->data, xs->datalen - xs->resid);
    522 			if (n > se_max_received)
    523 				se_max_received = n;
    524 			if (n == 0)
    525 				ntimeo = se_poll;
    526 			else if (n >= RDATA_MAX)
    527 				ntimeo = se_poll0;
    528 			else {
    529 				ntimeo = sc->sc_last_timeout;
    530 				ntimeo = (ntimeo * RDATA_GOAL)/n;
    531 				ntimeo = (ntimeo < se_poll0?
    532 					  se_poll0: ntimeo);
    533 				ntimeo = (ntimeo > se_poll?
    534 					  se_poll: ntimeo);
    535 			}
    536 			sc->sc_last_timeout = ntimeo;
    537 			if (ntimeo == se_poll0  &&
    538 			    IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    539 				/* Output is pending. Do next recv
    540 				 * after the next send.  */
    541 				sc->sc_flags |= SE_NEED_RECV;
    542 			else {
    543 				callout_reset(&sc->sc_recv_ch, ntimeo,
    544 				    se_recv, (void *)sc);
    545   			}
    546 		}
    547 	}
    548 	splx(s);
    549 }
    550 
    551 static void
    552 se_recv(void *v)
    553 {
    554 	/* do a recv command */
    555 	struct se_softc *sc = (struct se_softc *) v;
    556 	struct scsi_ctron_ether_recv recv_cmd;
    557 	int error;
    558 
    559 	if (sc->sc_enabled == 0)
    560 		return;
    561 
    562 	PROTOCMD(ctron_ether_recv, recv_cmd);
    563 
    564 	error = se_scsipi_cmd(sc->sc_periph,
    565 	    (void *)&recv_cmd, sizeof(recv_cmd),
    566 	    sc->sc_rbuf, RBUF_LEN, SERETRIES, SETIMEOUT, NULL,
    567 	    XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_DATA_IN);
    568 	if (error)
    569 		callout_reset(&sc->sc_recv_ch, se_poll, se_recv, (void *)sc);
    570 }
    571 
    572 /*
    573  * We copy the data into mbufs.  When full cluster sized units are present
    574  * we copy into clusters.
    575  */
    576 static struct mbuf *
    577 se_get(struct se_softc *sc, char *data, int totlen)
    578 {
    579 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    580 	struct mbuf *m, *m0, *newm;
    581 	int len;
    582 
    583 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    584 	if (m0 == 0)
    585 		return (0);
    586 	m_set_rcvif(m0, ifp);
    587 	m0->m_pkthdr.len = totlen;
    588 	len = MHLEN;
    589 	m = m0;
    590 
    591 	while (totlen > 0) {
    592 		if (totlen >= MINCLSIZE) {
    593 			MCLGET(m, M_DONTWAIT);
    594 			if ((m->m_flags & M_EXT) == 0)
    595 				goto bad;
    596 			len = MCLBYTES;
    597 		}
    598 
    599 		if (m == m0) {
    600 			char *newdata = (char *)
    601 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    602 			    sizeof(struct ether_header);
    603 			len -= newdata - m->m_data;
    604 			m->m_data = newdata;
    605 		}
    606 
    607 		m->m_len = len = min(totlen, len);
    608 		memcpy(mtod(m, void *), data, len);
    609 		data += len;
    610 
    611 		totlen -= len;
    612 		if (totlen > 0) {
    613 			MGET(newm, M_DONTWAIT, MT_DATA);
    614 			if (newm == 0)
    615 				goto bad;
    616 			len = MLEN;
    617 			m = m->m_next = newm;
    618 		}
    619 	}
    620 
    621 	return (m0);
    622 
    623 bad:
    624 	m_freem(m0);
    625 	return (0);
    626 }
    627 
    628 /*
    629  * Pass packets to higher levels.
    630  */
    631 static int
    632 se_read(struct se_softc *sc, char *data, int datalen)
    633 {
    634 	struct mbuf *m;
    635 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    636 	int n;
    637 
    638 	n = 0;
    639 	while (datalen >= 2) {
    640 		int len = _2btol(data);
    641 		data += 2;
    642 		datalen -= 2;
    643 
    644 		if (len == 0)
    645 			break;
    646 #ifdef SEDEBUG
    647 		if (sc->sc_debug) {
    648 			printf("se_read: datalen = %d, packetlen = %d, proto = 0x%04x\n", datalen, len,
    649 			 ntohs(((struct ether_header *)data)->ether_type));
    650 		}
    651 #endif
    652 		if (len <= sizeof(struct ether_header) ||
    653 		    len > MAX_SNAP) {
    654 #ifdef SEDEBUG
    655 			printf("%s: invalid packet size %d; dropping\n",
    656 			       device_xname(sc->sc_dev), len);
    657 #endif
    658 			ifp->if_ierrors++;
    659 			goto next_packet;
    660 		}
    661 
    662 		/* Don't need crc. Must keep ether header for BPF */
    663 		m = se_get(sc, data, len - ETHER_CRC);
    664 		if (m == 0) {
    665 #ifdef SEDEBUG
    666 			if (sc->sc_debug)
    667 				printf("se_read: se_get returned null\n");
    668 #endif
    669 			ifp->if_ierrors++;
    670 			goto next_packet;
    671 		}
    672 		if ((ifp->if_flags & IFF_PROMISC) != 0) {
    673 			m_adj(m, SE_PREFIX);
    674 		}
    675 
    676 		/* Pass the packet up. */
    677 		if_input(ifp, m);
    678 
    679 	next_packet:
    680 		data += len;
    681 		datalen -= len;
    682 		n++;
    683 	}
    684 	return (n);
    685 }
    686 
    687 
    688 static void
    689 sewatchdog(struct ifnet *ifp)
    690 {
    691 	struct se_softc *sc = ifp->if_softc;
    692 
    693 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
    694 	++ifp->if_oerrors;
    695 
    696 	se_reset(sc);
    697 }
    698 
    699 static int
    700 se_reset(struct se_softc *sc)
    701 {
    702 	int error;
    703 	int s = splnet();
    704 #if 0
    705 	/* Maybe we don't *really* want to reset the entire bus
    706 	 * because the ctron isn't working. We would like to send a
    707 	 * "BUS DEVICE RESET" message, but don't think the ctron
    708 	 * understands it.
    709 	 */
    710 	error = se_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, SERETRIES, 2000, NULL,
    711 	    XS_CTL_RESET);
    712 #endif
    713 	error = se_init(sc);
    714 	splx(s);
    715 	return (error);
    716 }
    717 
    718 static int
    719 se_add_proto(struct se_softc *sc, int proto)
    720 {
    721 	int error;
    722 	struct scsi_ctron_ether_generic add_proto_cmd;
    723 	u_int8_t data[2];
    724 	_lto2b(proto, data);
    725 #ifdef SEDEBUG
    726 	if (sc->sc_debug)
    727 		printf("se: adding proto 0x%02x%02x\n", data[0], data[1]);
    728 #endif
    729 
    730 	PROTOCMD(ctron_ether_add_proto, add_proto_cmd);
    731 	_lto2b(sizeof(data), add_proto_cmd.length);
    732 	error = se_scsipi_cmd(sc->sc_periph,
    733 	    (void *)&add_proto_cmd, sizeof(add_proto_cmd),
    734 	    data, sizeof(data), SERETRIES, SETIMEOUT, NULL,
    735 	    XS_CTL_DATA_OUT);
    736 	return (error);
    737 }
    738 
    739 static int
    740 se_get_addr(struct se_softc *sc, u_int8_t *myaddr)
    741 {
    742 	int error;
    743 	struct scsi_ctron_ether_generic get_addr_cmd;
    744 
    745 	PROTOCMD(ctron_ether_get_addr, get_addr_cmd);
    746 	_lto2b(ETHER_ADDR_LEN, get_addr_cmd.length);
    747 	error = se_scsipi_cmd(sc->sc_periph,
    748 	    (void *)&get_addr_cmd, sizeof(get_addr_cmd),
    749 	    myaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
    750 	    XS_CTL_DATA_IN);
    751 	printf("%s: ethernet address %s\n", device_xname(sc->sc_dev),
    752 	    ether_sprintf(myaddr));
    753 	return (error);
    754 }
    755 
    756 
    757 static int
    758 se_set_media(struct se_softc *sc, int type)
    759 {
    760 	int error;
    761 	struct scsi_ctron_ether_generic set_media_cmd;
    762 
    763 	PROTOCMD(ctron_ether_set_media, set_media_cmd);
    764 	set_media_cmd.byte3 = type;
    765 	error = se_scsipi_cmd(sc->sc_periph,
    766 	    (void *)&set_media_cmd, sizeof(set_media_cmd),
    767 	    0, 0, SERETRIES, SETIMEOUT, NULL, 0);
    768 	return (error);
    769 }
    770 
    771 static int
    772 se_set_mode(struct se_softc *sc, int len, int mode)
    773 {
    774 	int error;
    775 	struct scsi_ctron_ether_set_mode set_mode_cmd;
    776 
    777 	PROTOCMD(ctron_ether_set_mode, set_mode_cmd);
    778 	set_mode_cmd.mode = mode;
    779 	_lto2b(len, set_mode_cmd.length);
    780 	error = se_scsipi_cmd(sc->sc_periph,
    781 	    (void *)&set_mode_cmd, sizeof(set_mode_cmd),
    782 	    0, 0, SERETRIES, SETIMEOUT, NULL, 0);
    783 	return (error);
    784 }
    785 
    786 
    787 static int
    788 se_init(struct se_softc *sc)
    789 {
    790 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    791 	struct scsi_ctron_ether_generic set_addr_cmd;
    792 	uint8_t enaddr[ETHER_ADDR_LEN];
    793 	int error;
    794 
    795 	if (ifp->if_flags & IFF_PROMISC) {
    796 		error = se_set_mode(sc, MAX_SNAP, 1);
    797 	}
    798 	else
    799 		error = se_set_mode(sc, ETHERMTU + sizeof(struct ether_header),
    800 		    0);
    801 	if (error != 0)
    802 		return (error);
    803 
    804 	PROTOCMD(ctron_ether_set_addr, set_addr_cmd);
    805 	_lto2b(ETHER_ADDR_LEN, set_addr_cmd.length);
    806 	memcpy(enaddr, CLLADDR(ifp->if_sadl), sizeof(enaddr));
    807 	error = se_scsipi_cmd(sc->sc_periph,
    808 	    (void *)&set_addr_cmd, sizeof(set_addr_cmd),
    809 	    enaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
    810 	    XS_CTL_DATA_OUT);
    811 	if (error != 0)
    812 		return (error);
    813 
    814 	if ((sc->protos & PROTO_IP) &&
    815 	    (error = se_add_proto(sc, ETHERTYPE_IP)) != 0)
    816 		return (error);
    817 	if ((sc->protos & PROTO_ARP) &&
    818 	    (error = se_add_proto(sc, ETHERTYPE_ARP)) != 0)
    819 		return (error);
    820 	if ((sc->protos & PROTO_REVARP) &&
    821 	    (error = se_add_proto(sc, ETHERTYPE_REVARP)) != 0)
    822 		return (error);
    823 #ifdef NETATALK
    824 	if ((sc->protos & PROTO_AT) &&
    825 	    (error = se_add_proto(sc, ETHERTYPE_ATALK)) != 0)
    826 		return (error);
    827 	if ((sc->protos & PROTO_AARP) &&
    828 	    (error = se_add_proto(sc, ETHERTYPE_AARP)) != 0)
    829 		return (error);
    830 #endif
    831 
    832 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == IFF_UP) {
    833 		ifp->if_flags |= IFF_RUNNING;
    834 		se_recv(sc);
    835 		ifp->if_flags &= ~IFF_OACTIVE;
    836 		se_ifstart(ifp);
    837 	}
    838 	return (error);
    839 }
    840 
    841 static int
    842 se_set_multi(struct se_softc *sc, u_int8_t *addr)
    843 {
    844 	struct scsi_ctron_ether_generic set_multi_cmd;
    845 	int error;
    846 
    847 	if (sc->sc_debug)
    848 		printf("%s: set_set_multi: %s\n", device_xname(sc->sc_dev),
    849 		    ether_sprintf(addr));
    850 
    851 	PROTOCMD(ctron_ether_set_multi, set_multi_cmd);
    852 	_lto2b(sizeof(addr), set_multi_cmd.length);
    853 	/* XXX sizeof(addr) is the size of the pointer.  Surely it
    854 	 * is too small? --dyoung
    855 	 */
    856 	error = se_scsipi_cmd(sc->sc_periph,
    857 	    (void *)&set_multi_cmd, sizeof(set_multi_cmd),
    858 	    addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
    859 	return (error);
    860 }
    861 
    862 static int
    863 se_remove_multi(struct se_softc *sc, u_int8_t *addr)
    864 {
    865 	struct scsi_ctron_ether_generic remove_multi_cmd;
    866 	int error;
    867 
    868 	if (sc->sc_debug)
    869 		printf("%s: se_remove_multi: %s\n", device_xname(sc->sc_dev),
    870 		    ether_sprintf(addr));
    871 
    872 	PROTOCMD(ctron_ether_remove_multi, remove_multi_cmd);
    873 	_lto2b(sizeof(addr), remove_multi_cmd.length);
    874 	/* XXX sizeof(addr) is the size of the pointer.  Surely it
    875 	 * is too small? --dyoung
    876 	 */
    877 	error = se_scsipi_cmd(sc->sc_periph,
    878 	    (void *)&remove_multi_cmd, sizeof(remove_multi_cmd),
    879 	    addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
    880 	return (error);
    881 }
    882 
    883 #if 0	/* not used  --thorpej */
    884 static int
    885 sc_set_all_multi(struct se_softc *sc, int set)
    886 {
    887 	int error = 0;
    888 	u_int8_t *addr;
    889 	struct ethercom *ac = &sc->sc_ethercom;
    890 	struct ether_multi *enm;
    891 	struct ether_multistep step;
    892 
    893 	ETHER_FIRST_MULTI(step, ac, enm);
    894 	while (enm != NULL) {
    895 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    896 			/*
    897 			 * We must listen to a range of multicast addresses.
    898 			 * For now, just accept all multicasts, rather than
    899 			 * trying to set only those filter bits needed to match
    900 			 * the range.  (At this time, the only use of address
    901 			 * ranges is for IP multicast routing, for which the
    902 			 * range is big enough to require all bits set.)
    903 			 */
    904 			/* We have no way of adding a range to this device.
    905 			 * stepping through all addresses in the range is
    906 			 * typically not possible. The only real alternative
    907 			 * is to go into promicuous mode and filter by hand.
    908 			 */
    909 			return (ENODEV);
    910 
    911 		}
    912 
    913 		addr = enm->enm_addrlo;
    914 		if ((error = set ? se_set_multi(sc, addr) :
    915 		    se_remove_multi(sc, addr)) != 0)
    916 			return (error);
    917 		ETHER_NEXT_MULTI(step, enm);
    918 	}
    919 	return (error);
    920 }
    921 #endif /* not used */
    922 
    923 static void
    924 se_stop(struct se_softc *sc)
    925 {
    926 
    927 	/* Don't schedule any reads */
    928 	callout_stop(&sc->sc_recv_ch);
    929 
    930 	/* How can we abort any scsi cmds in progress? */
    931 }
    932 
    933 
    934 /*
    935  * Process an ioctl request.
    936  */
    937 static int
    938 se_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    939 {
    940 	struct se_softc *sc = ifp->if_softc;
    941 	struct ifaddr *ifa = (struct ifaddr *)data;
    942 	struct ifreq *ifr = (struct ifreq *)data;
    943 	struct sockaddr *sa;
    944 	int s, error = 0;
    945 
    946 	s = splnet();
    947 
    948 	switch (cmd) {
    949 
    950 	case SIOCINITIFADDR:
    951 		if ((error = se_enable(sc)) != 0)
    952 			break;
    953 		ifp->if_flags |= IFF_UP;
    954 
    955 		if ((error = se_set_media(sc, CMEDIA_AUTOSENSE)) != 0)
    956 			break;
    957 
    958 		switch (ifa->ifa_addr->sa_family) {
    959 #ifdef INET
    960 		case AF_INET:
    961 			sc->protos |= (PROTO_IP | PROTO_ARP | PROTO_REVARP);
    962 			if ((error = se_init(sc)) != 0)
    963 				break;
    964 			arp_ifinit(ifp, ifa);
    965 			break;
    966 #endif
    967 #ifdef NETATALK
    968 		case AF_APPLETALK:
    969 			sc->protos |= (PROTO_AT | PROTO_AARP);
    970 			if ((error = se_init(sc)) != 0)
    971 				break;
    972 			break;
    973 #endif
    974 		default:
    975 			error = se_init(sc);
    976 			break;
    977 		}
    978 		break;
    979 
    980 
    981 	case SIOCSIFFLAGS:
    982 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    983 			break;
    984 		/* XXX re-use ether_ioctl() */
    985 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    986 		case IFF_RUNNING:
    987 			/*
    988 			 * If interface is marked down and it is running, then
    989 			 * stop it.
    990 			 */
    991 			se_stop(sc);
    992 			ifp->if_flags &= ~IFF_RUNNING;
    993 			se_disable(sc);
    994 			break;
    995 		case IFF_UP:
    996 			/*
    997 			 * If interface is marked up and it is stopped, then
    998 			 * start it.
    999 			 */
   1000 			if ((error = se_enable(sc)) != 0)
   1001 				break;
   1002 			error = se_init(sc);
   1003 			break;
   1004 		default:
   1005 			/*
   1006 			 * Reset the interface to pick up changes in any other
   1007 			 * flags that affect hardware registers.
   1008 			 */
   1009 			if (sc->sc_enabled)
   1010 				error = se_init(sc);
   1011 			break;
   1012 		}
   1013 #ifdef SEDEBUG
   1014 		if (ifp->if_flags & IFF_DEBUG)
   1015 			sc->sc_debug = 1;
   1016 		else
   1017 			sc->sc_debug = 0;
   1018 #endif
   1019 		break;
   1020 
   1021 	case SIOCADDMULTI:
   1022 	case SIOCDELMULTI:
   1023 		sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_NOWAIT);
   1024 		if (sa == NULL) {
   1025 			error = ENOBUFS;
   1026 			break;
   1027 		}
   1028 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   1029 			if (ifp->if_flags & IFF_RUNNING) {
   1030 				error = (cmd == SIOCADDMULTI) ?
   1031 				   se_set_multi(sc, sa->sa_data) :
   1032 				   se_remove_multi(sc, sa->sa_data);
   1033 			} else
   1034 				error = 0;
   1035 		}
   1036 		sockaddr_free(sa);
   1037 		break;
   1038 
   1039 	default:
   1040 
   1041 		error = ether_ioctl(ifp, cmd, data);
   1042 		break;
   1043 	}
   1044 
   1045 	splx(s);
   1046 	return (error);
   1047 }
   1048 
   1049 /*
   1050  * Enable the network interface.
   1051  */
   1052 int
   1053 se_enable(struct se_softc *sc)
   1054 {
   1055 	struct scsipi_periph *periph = sc->sc_periph;
   1056 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1057 	int error = 0;
   1058 
   1059 	if (sc->sc_enabled == 0 &&
   1060 	    (error = scsipi_adapter_addref(adapt)) == 0)
   1061 		sc->sc_enabled = 1;
   1062 	else
   1063 		aprint_error_dev(sc->sc_dev, "device enable failed\n");
   1064 
   1065 	return (error);
   1066 }
   1067 
   1068 /*
   1069  * Disable the network interface.
   1070  */
   1071 void
   1072 se_disable(struct se_softc *sc)
   1073 {
   1074 	struct scsipi_periph *periph = sc->sc_periph;
   1075 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1076 
   1077 	if (sc->sc_enabled != 0) {
   1078 		scsipi_adapter_delref(adapt);
   1079 		sc->sc_enabled = 0;
   1080 	}
   1081 }
   1082 
   1083 #define	SEUNIT(z)	(minor(z))
   1084 /*
   1085  * open the device.
   1086  */
   1087 int
   1088 seopen(dev_t dev, int flag, int fmt, struct lwp *l)
   1089 {
   1090 	int unit, error;
   1091 	struct se_softc *sc;
   1092 	struct scsipi_periph *periph;
   1093 	struct scsipi_adapter *adapt;
   1094 
   1095 	unit = SEUNIT(dev);
   1096 	sc = device_lookup_private(&se_cd, unit);
   1097 	if (sc == NULL)
   1098 		return (ENXIO);
   1099 
   1100 	periph = sc->sc_periph;
   1101 	adapt = periph->periph_channel->chan_adapter;
   1102 
   1103 	if ((error = scsipi_adapter_addref(adapt)) != 0)
   1104 		return (error);
   1105 
   1106 	SC_DEBUG(periph, SCSIPI_DB1,
   1107 	    ("scopen: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit,
   1108 	    se_cd.cd_ndevs));
   1109 
   1110 	periph->periph_flags |= PERIPH_OPEN;
   1111 
   1112 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
   1113 	return (0);
   1114 }
   1115 
   1116 /*
   1117  * close the device.. only called if we are the LAST
   1118  * occurence of an open device
   1119  */
   1120 int
   1121 seclose(dev_t dev, int flag, int fmt, struct lwp *l)
   1122 {
   1123 	struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
   1124 	struct scsipi_periph *periph = sc->sc_periph;
   1125 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1126 
   1127 	SC_DEBUG(sc->sc_periph, SCSIPI_DB1, ("closing\n"));
   1128 
   1129 	scsipi_wait_drain(periph);
   1130 
   1131 	scsipi_adapter_delref(adapt);
   1132 	periph->periph_flags &= ~PERIPH_OPEN;
   1133 
   1134 	return (0);
   1135 }
   1136 
   1137 /*
   1138  * Perform special action on behalf of the user
   1139  * Only does generic scsi ioctls.
   1140  */
   1141 int
   1142 seioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1143 {
   1144 	struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
   1145 
   1146 	return (scsipi_do_ioctl(sc->sc_periph, dev, cmd, addr, flag, l));
   1147 }
   1148