Home | History | Annotate | Line # | Download | only in scsipi
if_se.c revision 1.101
      1 /*	$NetBSD: if_se.c,v 1.101 2019/05/23 13:10:52 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.101 2019/05/23 13:10:52 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 uint16_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 *, uint8_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 *, uint8_t *);
    216 static int	se_remove_multi(struct se_softc *, uint8_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 uint16_t
    277 ether_cmp(void *one, void *two)
    278 {
    279 	uint16_t *a = (uint16_t *) one;
    280 	uint16_t *b = (uint16_t *) two;
    281 	uint16_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 	uint8_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 	 * Store information needed to contact our base driver
    327 	 */
    328 	sc->sc_periph = periph;
    329 	periph->periph_dev = sc->sc_dev;
    330 	periph->periph_switch = &se_switch;
    331 
    332 	/* XXX increase openings? */
    333 
    334 	se_poll = (SE_POLL * hz) / 1000;
    335 	se_poll = se_poll? se_poll: 1;
    336 	se_poll0 = (SE_POLL0 * hz) / 1000;
    337 	se_poll0 = se_poll0? se_poll0: 1;
    338 
    339 	/*
    340 	 * Initialize and attach a buffer
    341 	 */
    342 	sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header),
    343 			     M_DEVBUF, M_NOWAIT);
    344 	if (sc->sc_tbuf == 0)
    345 		panic("seattach: can't allocate transmit buffer");
    346 
    347 	sc->sc_rbuf = malloc(RBUF_LEN, M_DEVBUF, M_NOWAIT);/* A Guess */
    348 	if (sc->sc_rbuf == 0)
    349 		panic("seattach: can't allocate receive buffer");
    350 
    351 	se_get_addr(sc, myaddr);
    352 
    353 	/* Initialize ifnet structure. */
    354 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), sizeof(ifp->if_xname));
    355 	ifp->if_softc = sc;
    356 	ifp->if_start = se_ifstart;
    357 	ifp->if_ioctl = se_ioctl;
    358 	ifp->if_watchdog = sewatchdog;
    359 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    360 	IFQ_SET_READY(&ifp->if_snd);
    361 
    362 	/* Attach the interface. */
    363 	rv = if_initialize(ifp);
    364 	if (rv != 0) {
    365 		free(sc->sc_tbuf, M_DEVBUF);
    366 		callout_destroy(&sc->sc_ifstart_ch);
    367 		callout_destroy(&sc->sc_recv_ch);
    368 		return; /* Error */
    369 	}
    370 	ether_ifattach(ifp, myaddr);
    371 	if_register(ifp);
    372 }
    373 
    374 
    375 static inline int
    376 se_scsipi_cmd(struct scsipi_periph *periph, struct scsipi_generic *cmd,
    377     int cmdlen, u_char *data_addr, int datalen, int retries, int timeout,
    378     struct buf *bp, int flags)
    379 {
    380 	int error;
    381 
    382 	error = scsipi_command(periph, cmd, cmdlen, data_addr,
    383 	    datalen, retries, timeout, bp, flags);
    384 	return (error);
    385 }
    386 
    387 /* Start routine for calling from scsi sub system */
    388 static void
    389 sestart(struct scsipi_periph *periph)
    390 {
    391 	struct se_softc *sc = device_private(periph->periph_dev);
    392 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    393 	int s = splnet();
    394 
    395 	se_ifstart(ifp);
    396 	(void) splx(s);
    397 }
    398 
    399 static void
    400 se_delayed_ifstart(void *v)
    401 {
    402 	struct ifnet *ifp = v;
    403 	struct se_softc *sc = ifp->if_softc;
    404 	int s;
    405 
    406 	s = splnet();
    407 	if (sc->sc_enabled) {
    408 		ifp->if_flags &= ~IFF_OACTIVE;
    409 		se_ifstart(ifp);
    410 	}
    411 	splx(s);
    412 }
    413 
    414 /*
    415  * Start transmission on the interface.
    416  * Always called at splnet().
    417  */
    418 static void
    419 se_ifstart(struct ifnet *ifp)
    420 {
    421 	struct se_softc *sc = ifp->if_softc;
    422 	struct scsi_ctron_ether_generic send_cmd;
    423 	struct mbuf *m, *m0;
    424 	int len, error;
    425 	u_char *cp;
    426 
    427 	/* Don't transmit if interface is busy or not running */
    428 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    429 		return;
    430 
    431 	IFQ_DEQUEUE(&ifp->if_snd, m0);
    432 	if (m0 == 0)
    433 		return;
    434 	/* If BPF is listening on this interface, let it see the
    435 	 * packet before we commit it to the wire.
    436 	 */
    437 	bpf_mtap(ifp, m0, BPF_D_OUT);
    438 
    439 	/* We need to use m->m_pkthdr.len, so require the header */
    440 	if ((m0->m_flags & M_PKTHDR) == 0)
    441 		panic("ctscstart: no header mbuf");
    442 	len = m0->m_pkthdr.len;
    443 
    444 	/* Mark the interface busy. */
    445 	ifp->if_flags |= IFF_OACTIVE;
    446 
    447 	/* Chain; copy into linear buffer we allocated at attach time. */
    448 	cp = sc->sc_tbuf;
    449 	for (m = m0; m != NULL; ) {
    450 		memcpy(cp, mtod(m, u_char *), m->m_len);
    451 		cp += m->m_len;
    452 		m = m0 = m_free(m);
    453 	}
    454 	if (len < SEMINSIZE) {
    455 #ifdef SEDEBUG
    456 		if (sc->sc_debug)
    457 			printf("se: packet size %d (%zu) < %d\n", len,
    458 			    cp - (u_char *)sc->sc_tbuf, SEMINSIZE);
    459 #endif
    460 		memset(cp, 0, SEMINSIZE - len);
    461 		len = SEMINSIZE;
    462 	}
    463 
    464 	/* Fill out SCSI command. */
    465 	PROTOCMD(ctron_ether_send, send_cmd);
    466 	_lto2b(len, send_cmd.length);
    467 
    468 	/* Send command to device. */
    469 	error = se_scsipi_cmd(sc->sc_periph,
    470 	    (void *)&send_cmd, sizeof(send_cmd),
    471 	    sc->sc_tbuf, len, SERETRIES,
    472 	    SETIMEOUT, NULL, XS_CTL_NOSLEEP | XS_CTL_ASYNC | XS_CTL_DATA_OUT);
    473 	if (error) {
    474 		aprint_error_dev(sc->sc_dev, "not queued, error %d\n", error);
    475 		ifp->if_oerrors++;
    476 		ifp->if_flags &= ~IFF_OACTIVE;
    477 	} else
    478 		ifp->if_opackets++;
    479 	if (sc->sc_flags & SE_NEED_RECV) {
    480 		sc->sc_flags &= ~SE_NEED_RECV;
    481 		se_recv((void *) sc);
    482 	}
    483 }
    484 
    485 
    486 /*
    487  * Called from the scsibus layer via our scsi device switch.
    488  */
    489 static void
    490 sedone(struct scsipi_xfer *xs, int error)
    491 {
    492 	struct se_softc *sc = device_private(xs->xs_periph->periph_dev);
    493 	struct scsipi_generic *cmd = xs->cmd;
    494 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    495 	int s;
    496 
    497 	s = splnet();
    498 	if (IS_SEND(cmd)) {
    499 		if (xs->error == XS_BUSY) {
    500 			printf("se: busy, retry txmit\n");
    501 			callout_reset(&sc->sc_ifstart_ch, hz,
    502 			    se_delayed_ifstart, ifp);
    503 		} else {
    504 			ifp->if_flags &= ~IFF_OACTIVE;
    505 			/* the generic scsipi_done will call
    506 			 * sestart (through scsipi_free_xs).
    507 			 */
    508 		}
    509 	} else if (IS_RECV(cmd)) {
    510 		/* RECV complete */
    511 		/* pass data up. reschedule a recv */
    512 		/* scsipi_free_xs will call start. Harmless. */
    513 		if (error) {
    514 			/* Reschedule after a delay */
    515 			callout_reset(&sc->sc_recv_ch, se_poll,
    516 			    se_recv, (void *)sc);
    517 		} else {
    518 			int n, ntimeo;
    519 			n = se_read(sc, xs->data, xs->datalen - xs->resid);
    520 			if (n > se_max_received)
    521 				se_max_received = n;
    522 			if (n == 0)
    523 				ntimeo = se_poll;
    524 			else if (n >= RDATA_MAX)
    525 				ntimeo = se_poll0;
    526 			else {
    527 				ntimeo = sc->sc_last_timeout;
    528 				ntimeo = (ntimeo * RDATA_GOAL)/n;
    529 				ntimeo = (ntimeo < se_poll0?
    530 					  se_poll0: ntimeo);
    531 				ntimeo = (ntimeo > se_poll?
    532 					  se_poll: ntimeo);
    533 			}
    534 			sc->sc_last_timeout = ntimeo;
    535 			if (ntimeo == se_poll0	&&
    536 			    IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    537 				/* Output is pending. Do next recv
    538 				 * after the next send. */
    539 				sc->sc_flags |= SE_NEED_RECV;
    540 			else {
    541 				callout_reset(&sc->sc_recv_ch, ntimeo,
    542 				    se_recv, (void *)sc);
    543 			}
    544 		}
    545 	}
    546 	splx(s);
    547 }
    548 
    549 static void
    550 se_recv(void *v)
    551 {
    552 	/* do a recv command */
    553 	struct se_softc *sc = (struct se_softc *) v;
    554 	struct scsi_ctron_ether_recv recv_cmd;
    555 	int error;
    556 
    557 	if (sc->sc_enabled == 0)
    558 		return;
    559 
    560 	PROTOCMD(ctron_ether_recv, recv_cmd);
    561 
    562 	error = se_scsipi_cmd(sc->sc_periph,
    563 	    (void *)&recv_cmd, sizeof(recv_cmd),
    564 	    sc->sc_rbuf, RBUF_LEN, SERETRIES, SETIMEOUT, NULL,
    565 	    XS_CTL_NOSLEEP | XS_CTL_ASYNC | XS_CTL_DATA_IN);
    566 	if (error)
    567 		callout_reset(&sc->sc_recv_ch, se_poll, se_recv, (void *)sc);
    568 }
    569 
    570 /*
    571  * We copy the data into mbufs.  When full cluster sized units are present
    572  * we copy into clusters.
    573  */
    574 static struct mbuf *
    575 se_get(struct se_softc *sc, char *data, int totlen)
    576 {
    577 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    578 	struct mbuf *m, *m0, *newm;
    579 	int len;
    580 
    581 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    582 	if (m0 == 0)
    583 		return (0);
    584 	m_set_rcvif(m0, ifp);
    585 	m0->m_pkthdr.len = totlen;
    586 	len = MHLEN;
    587 	m = m0;
    588 
    589 	while (totlen > 0) {
    590 		if (totlen >= MINCLSIZE) {
    591 			MCLGET(m, M_DONTWAIT);
    592 			if ((m->m_flags & M_EXT) == 0)
    593 				goto bad;
    594 			len = MCLBYTES;
    595 		}
    596 
    597 		if (m == m0) {
    598 			char *newdata = (char *)
    599 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    600 			    sizeof(struct ether_header);
    601 			len -= newdata - m->m_data;
    602 			m->m_data = newdata;
    603 		}
    604 
    605 		m->m_len = len = uimin(totlen, len);
    606 		memcpy(mtod(m, void *), data, len);
    607 		data += len;
    608 
    609 		totlen -= len;
    610 		if (totlen > 0) {
    611 			MGET(newm, M_DONTWAIT, MT_DATA);
    612 			if (newm == 0)
    613 				goto bad;
    614 			len = MLEN;
    615 			m = m->m_next = newm;
    616 		}
    617 	}
    618 
    619 	return (m0);
    620 
    621 bad:
    622 	m_freem(m0);
    623 	return (0);
    624 }
    625 
    626 /*
    627  * Pass packets to higher levels.
    628  */
    629 static int
    630 se_read(struct se_softc *sc, char *data, int datalen)
    631 {
    632 	struct mbuf *m;
    633 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    634 	int n;
    635 
    636 	n = 0;
    637 	while (datalen >= 2) {
    638 		int len = _2btol(data);
    639 		data += 2;
    640 		datalen -= 2;
    641 
    642 		if (len == 0)
    643 			break;
    644 #ifdef SEDEBUG
    645 		if (sc->sc_debug) {
    646 			printf("se_read: datalen = %d, packetlen = %d, proto = 0x%04x\n", datalen, len,
    647 			 ntohs(((struct ether_header *)data)->ether_type));
    648 		}
    649 #endif
    650 		if (len <= sizeof(struct ether_header) ||
    651 		    len > MAX_SNAP) {
    652 #ifdef SEDEBUG
    653 			printf("%s: invalid packet size %d; dropping\n",
    654 			       device_xname(sc->sc_dev), len);
    655 #endif
    656 			ifp->if_ierrors++;
    657 			goto next_packet;
    658 		}
    659 
    660 		/* Don't need crc. Must keep ether header for BPF */
    661 		m = se_get(sc, data, len - ETHER_CRC);
    662 		if (m == 0) {
    663 #ifdef SEDEBUG
    664 			if (sc->sc_debug)
    665 				printf("se_read: se_get returned null\n");
    666 #endif
    667 			ifp->if_ierrors++;
    668 			goto next_packet;
    669 		}
    670 		if ((ifp->if_flags & IFF_PROMISC) != 0) {
    671 			m_adj(m, SE_PREFIX);
    672 		}
    673 
    674 		/* Pass the packet up. */
    675 		if_input(ifp, m);
    676 
    677 	next_packet:
    678 		data += len;
    679 		datalen -= len;
    680 		n++;
    681 	}
    682 	return (n);
    683 }
    684 
    685 
    686 static void
    687 sewatchdog(struct ifnet *ifp)
    688 {
    689 	struct se_softc *sc = ifp->if_softc;
    690 
    691 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
    692 	++ifp->if_oerrors;
    693 
    694 	se_reset(sc);
    695 }
    696 
    697 static int
    698 se_reset(struct se_softc *sc)
    699 {
    700 	int error;
    701 	int s = splnet();
    702 #if 0
    703 	/* Maybe we don't *really* want to reset the entire bus
    704 	 * because the ctron isn't working. We would like to send a
    705 	 * "BUS DEVICE RESET" message, but don't think the ctron
    706 	 * understands it.
    707 	 */
    708 	error = se_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, SERETRIES, 2000, NULL,
    709 	    XS_CTL_RESET);
    710 #endif
    711 	error = se_init(sc);
    712 	splx(s);
    713 	return (error);
    714 }
    715 
    716 static int
    717 se_add_proto(struct se_softc *sc, int proto)
    718 {
    719 	int error;
    720 	struct scsi_ctron_ether_generic add_proto_cmd;
    721 	uint8_t data[2];
    722 	_lto2b(proto, data);
    723 #ifdef SEDEBUG
    724 	if (sc->sc_debug)
    725 		printf("se: adding proto 0x%02x%02x\n", data[0], data[1]);
    726 #endif
    727 
    728 	PROTOCMD(ctron_ether_add_proto, add_proto_cmd);
    729 	_lto2b(sizeof(data), add_proto_cmd.length);
    730 	error = se_scsipi_cmd(sc->sc_periph,
    731 	    (void *)&add_proto_cmd, sizeof(add_proto_cmd),
    732 	    data, sizeof(data), SERETRIES, SETIMEOUT, NULL,
    733 	    XS_CTL_DATA_OUT);
    734 	return (error);
    735 }
    736 
    737 static int
    738 se_get_addr(struct se_softc *sc, uint8_t *myaddr)
    739 {
    740 	int error;
    741 	struct scsi_ctron_ether_generic get_addr_cmd;
    742 
    743 	PROTOCMD(ctron_ether_get_addr, get_addr_cmd);
    744 	_lto2b(ETHER_ADDR_LEN, get_addr_cmd.length);
    745 	error = se_scsipi_cmd(sc->sc_periph,
    746 	    (void *)&get_addr_cmd, sizeof(get_addr_cmd),
    747 	    myaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
    748 	    XS_CTL_DATA_IN);
    749 	printf("%s: ethernet address %s\n", device_xname(sc->sc_dev),
    750 	    ether_sprintf(myaddr));
    751 	return (error);
    752 }
    753 
    754 
    755 static int
    756 se_set_media(struct se_softc *sc, int type)
    757 {
    758 	int error;
    759 	struct scsi_ctron_ether_generic set_media_cmd;
    760 
    761 	PROTOCMD(ctron_ether_set_media, set_media_cmd);
    762 	set_media_cmd.byte3 = type;
    763 	error = se_scsipi_cmd(sc->sc_periph,
    764 	    (void *)&set_media_cmd, sizeof(set_media_cmd),
    765 	    0, 0, SERETRIES, SETIMEOUT, NULL, 0);
    766 	return (error);
    767 }
    768 
    769 static int
    770 se_set_mode(struct se_softc *sc, int len, int mode)
    771 {
    772 	int error;
    773 	struct scsi_ctron_ether_set_mode set_mode_cmd;
    774 
    775 	PROTOCMD(ctron_ether_set_mode, set_mode_cmd);
    776 	set_mode_cmd.mode = mode;
    777 	_lto2b(len, set_mode_cmd.length);
    778 	error = se_scsipi_cmd(sc->sc_periph,
    779 	    (void *)&set_mode_cmd, sizeof(set_mode_cmd),
    780 	    0, 0, SERETRIES, SETIMEOUT, NULL, 0);
    781 	return (error);
    782 }
    783 
    784 
    785 static int
    786 se_init(struct se_softc *sc)
    787 {
    788 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    789 	struct scsi_ctron_ether_generic set_addr_cmd;
    790 	uint8_t enaddr[ETHER_ADDR_LEN];
    791 	int error;
    792 
    793 	if (ifp->if_flags & IFF_PROMISC) {
    794 		error = se_set_mode(sc, MAX_SNAP, 1);
    795 	}
    796 	else
    797 		error = se_set_mode(sc, ETHERMTU + sizeof(struct ether_header),
    798 		    0);
    799 	if (error != 0)
    800 		return (error);
    801 
    802 	PROTOCMD(ctron_ether_set_addr, set_addr_cmd);
    803 	_lto2b(ETHER_ADDR_LEN, set_addr_cmd.length);
    804 	memcpy(enaddr, CLLADDR(ifp->if_sadl), sizeof(enaddr));
    805 	error = se_scsipi_cmd(sc->sc_periph,
    806 	    (void *)&set_addr_cmd, sizeof(set_addr_cmd),
    807 	    enaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
    808 	    XS_CTL_DATA_OUT);
    809 	if (error != 0)
    810 		return (error);
    811 
    812 	if ((sc->protos & PROTO_IP) &&
    813 	    (error = se_add_proto(sc, ETHERTYPE_IP)) != 0)
    814 		return (error);
    815 	if ((sc->protos & PROTO_ARP) &&
    816 	    (error = se_add_proto(sc, ETHERTYPE_ARP)) != 0)
    817 		return (error);
    818 	if ((sc->protos & PROTO_REVARP) &&
    819 	    (error = se_add_proto(sc, ETHERTYPE_REVARP)) != 0)
    820 		return (error);
    821 #ifdef NETATALK
    822 	if ((sc->protos & PROTO_AT) &&
    823 	    (error = se_add_proto(sc, ETHERTYPE_ATALK)) != 0)
    824 		return (error);
    825 	if ((sc->protos & PROTO_AARP) &&
    826 	    (error = se_add_proto(sc, ETHERTYPE_AARP)) != 0)
    827 		return (error);
    828 #endif
    829 
    830 	if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) == IFF_UP) {
    831 		ifp->if_flags |= IFF_RUNNING;
    832 		se_recv(sc);
    833 		ifp->if_flags &= ~IFF_OACTIVE;
    834 		se_ifstart(ifp);
    835 	}
    836 	return (error);
    837 }
    838 
    839 static int
    840 se_set_multi(struct se_softc *sc, uint8_t *addr)
    841 {
    842 	struct scsi_ctron_ether_generic set_multi_cmd;
    843 	int error;
    844 
    845 	if (sc->sc_debug)
    846 		printf("%s: set_set_multi: %s\n", device_xname(sc->sc_dev),
    847 		    ether_sprintf(addr));
    848 
    849 	PROTOCMD(ctron_ether_set_multi, set_multi_cmd);
    850 	_lto2b(sizeof(addr), set_multi_cmd.length);
    851 	/* XXX sizeof(addr) is the size of the pointer.  Surely it
    852 	 * is too small? --dyoung
    853 	 */
    854 	error = se_scsipi_cmd(sc->sc_periph,
    855 	    (void *)&set_multi_cmd, sizeof(set_multi_cmd),
    856 	    addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
    857 	return (error);
    858 }
    859 
    860 static int
    861 se_remove_multi(struct se_softc *sc, uint8_t *addr)
    862 {
    863 	struct scsi_ctron_ether_generic remove_multi_cmd;
    864 	int error;
    865 
    866 	if (sc->sc_debug)
    867 		printf("%s: se_remove_multi: %s\n", device_xname(sc->sc_dev),
    868 		    ether_sprintf(addr));
    869 
    870 	PROTOCMD(ctron_ether_remove_multi, remove_multi_cmd);
    871 	_lto2b(sizeof(addr), remove_multi_cmd.length);
    872 	/* XXX sizeof(addr) is the size of the pointer.  Surely it
    873 	 * is too small? --dyoung
    874 	 */
    875 	error = se_scsipi_cmd(sc->sc_periph,
    876 	    (void *)&remove_multi_cmd, sizeof(remove_multi_cmd),
    877 	    addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
    878 	return (error);
    879 }
    880 
    881 #if 0	/* not used  --thorpej */
    882 static int
    883 sc_set_all_multi(struct se_softc *sc, int set)
    884 {
    885 	int error = 0;
    886 	uint8_t *addr;
    887 	struct ethercom *ec = &sc->sc_ethercom;
    888 	struct ether_multi *enm;
    889 	struct ether_multistep step;
    890 
    891 	ETHER_FIRST_MULTI(step, ec, enm);
    892 	while (enm != NULL) {
    893 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    894 			/*
    895 			 * We must listen to a range of multicast addresses.
    896 			 * For now, just accept all multicasts, rather than
    897 			 * trying to set only those filter bits needed to match
    898 			 * the range.  (At this time, the only use of address
    899 			 * ranges is for IP multicast routing, for which the
    900 			 * range is big enough to require all bits set.)
    901 			 */
    902 			/* We have no way of adding a range to this device.
    903 			 * stepping through all addresses in the range is
    904 			 * typically not possible. The only real alternative
    905 			 * is to go into promicuous mode and filter by hand.
    906 			 */
    907 			return (ENODEV);
    908 
    909 		}
    910 
    911 		addr = enm->enm_addrlo;
    912 		if ((error = set ? se_set_multi(sc, addr) :
    913 		    se_remove_multi(sc, addr)) != 0)
    914 			return (error);
    915 		ETHER_NEXT_MULTI(step, enm);
    916 	}
    917 	return (error);
    918 }
    919 #endif /* not used */
    920 
    921 static void
    922 se_stop(struct se_softc *sc)
    923 {
    924 
    925 	/* Don't schedule any reads */
    926 	callout_stop(&sc->sc_recv_ch);
    927 
    928 	/* How can we abort any scsi cmds in progress? */
    929 }
    930 
    931 
    932 /*
    933  * Process an ioctl request.
    934  */
    935 static int
    936 se_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    937 {
    938 	struct se_softc *sc = ifp->if_softc;
    939 	struct ifaddr *ifa = (struct ifaddr *)data;
    940 	struct ifreq *ifr = (struct ifreq *)data;
    941 	struct sockaddr *sa;
    942 	int s, error = 0;
    943 
    944 	s = splnet();
    945 
    946 	switch (cmd) {
    947 
    948 	case SIOCINITIFADDR:
    949 		if ((error = se_enable(sc)) != 0)
    950 			break;
    951 		ifp->if_flags |= IFF_UP;
    952 
    953 		if ((error = se_set_media(sc, CMEDIA_AUTOSENSE)) != 0)
    954 			break;
    955 
    956 		switch (ifa->ifa_addr->sa_family) {
    957 #ifdef INET
    958 		case AF_INET:
    959 			sc->protos |= (PROTO_IP | PROTO_ARP | PROTO_REVARP);
    960 			if ((error = se_init(sc)) != 0)
    961 				break;
    962 			arp_ifinit(ifp, ifa);
    963 			break;
    964 #endif
    965 #ifdef NETATALK
    966 		case AF_APPLETALK:
    967 			sc->protos |= (PROTO_AT | PROTO_AARP);
    968 			if ((error = se_init(sc)) != 0)
    969 				break;
    970 			break;
    971 #endif
    972 		default:
    973 			error = se_init(sc);
    974 			break;
    975 		}
    976 		break;
    977 
    978 
    979 	case SIOCSIFFLAGS:
    980 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    981 			break;
    982 		/* XXX re-use ether_ioctl() */
    983 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
    984 		case IFF_RUNNING:
    985 			/*
    986 			 * If interface is marked down and it is running, then
    987 			 * stop it.
    988 			 */
    989 			se_stop(sc);
    990 			ifp->if_flags &= ~IFF_RUNNING;
    991 			se_disable(sc);
    992 			break;
    993 		case IFF_UP:
    994 			/*
    995 			 * If interface is marked up and it is stopped, then
    996 			 * start it.
    997 			 */
    998 			if ((error = se_enable(sc)) != 0)
    999 				break;
   1000 			error = se_init(sc);
   1001 			break;
   1002 		default:
   1003 			/*
   1004 			 * Reset the interface to pick up changes in any other
   1005 			 * flags that affect hardware registers.
   1006 			 */
   1007 			if (sc->sc_enabled)
   1008 				error = se_init(sc);
   1009 			break;
   1010 		}
   1011 #ifdef SEDEBUG
   1012 		if (ifp->if_flags & IFF_DEBUG)
   1013 			sc->sc_debug = 1;
   1014 		else
   1015 			sc->sc_debug = 0;
   1016 #endif
   1017 		break;
   1018 
   1019 	case SIOCADDMULTI:
   1020 	case SIOCDELMULTI:
   1021 		sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_NOWAIT);
   1022 		if (sa == NULL) {
   1023 			error = ENOBUFS;
   1024 			break;
   1025 		}
   1026 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   1027 			if (ifp->if_flags & IFF_RUNNING) {
   1028 				error = (cmd == SIOCADDMULTI) ?
   1029 				   se_set_multi(sc, sa->sa_data) :
   1030 				   se_remove_multi(sc, sa->sa_data);
   1031 			} else
   1032 				error = 0;
   1033 		}
   1034 		sockaddr_free(sa);
   1035 		break;
   1036 
   1037 	default:
   1038 
   1039 		error = ether_ioctl(ifp, cmd, data);
   1040 		break;
   1041 	}
   1042 
   1043 	splx(s);
   1044 	return (error);
   1045 }
   1046 
   1047 /*
   1048  * Enable the network interface.
   1049  */
   1050 int
   1051 se_enable(struct se_softc *sc)
   1052 {
   1053 	struct scsipi_periph *periph = sc->sc_periph;
   1054 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1055 	int error = 0;
   1056 
   1057 	if (sc->sc_enabled == 0 &&
   1058 	    (error = scsipi_adapter_addref(adapt)) == 0)
   1059 		sc->sc_enabled = 1;
   1060 	else
   1061 		aprint_error_dev(sc->sc_dev, "device enable failed\n");
   1062 
   1063 	return (error);
   1064 }
   1065 
   1066 /*
   1067  * Disable the network interface.
   1068  */
   1069 void
   1070 se_disable(struct se_softc *sc)
   1071 {
   1072 	struct scsipi_periph *periph = sc->sc_periph;
   1073 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1074 
   1075 	if (sc->sc_enabled != 0) {
   1076 		scsipi_adapter_delref(adapt);
   1077 		sc->sc_enabled = 0;
   1078 	}
   1079 }
   1080 
   1081 #define	SEUNIT(z)	(minor(z))
   1082 /*
   1083  * open the device.
   1084  */
   1085 int
   1086 seopen(dev_t dev, int flag, int fmt, struct lwp *l)
   1087 {
   1088 	int unit, error;
   1089 	struct se_softc *sc;
   1090 	struct scsipi_periph *periph;
   1091 	struct scsipi_adapter *adapt;
   1092 
   1093 	unit = SEUNIT(dev);
   1094 	sc = device_lookup_private(&se_cd, unit);
   1095 	if (sc == NULL)
   1096 		return (ENXIO);
   1097 
   1098 	periph = sc->sc_periph;
   1099 	adapt = periph->periph_channel->chan_adapter;
   1100 
   1101 	if ((error = scsipi_adapter_addref(adapt)) != 0)
   1102 		return (error);
   1103 
   1104 	SC_DEBUG(periph, SCSIPI_DB1,
   1105 	    ("scopen: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit,
   1106 	    se_cd.cd_ndevs));
   1107 
   1108 	periph->periph_flags |= PERIPH_OPEN;
   1109 
   1110 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
   1111 	return (0);
   1112 }
   1113 
   1114 /*
   1115  * close the device.. only called if we are the LAST
   1116  * occurence of an open device
   1117  */
   1118 int
   1119 seclose(dev_t dev, int flag, int fmt, struct lwp *l)
   1120 {
   1121 	struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
   1122 	struct scsipi_periph *periph = sc->sc_periph;
   1123 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1124 
   1125 	SC_DEBUG(sc->sc_periph, SCSIPI_DB1, ("closing\n"));
   1126 
   1127 	scsipi_wait_drain(periph);
   1128 
   1129 	scsipi_adapter_delref(adapt);
   1130 	periph->periph_flags &= ~PERIPH_OPEN;
   1131 
   1132 	return (0);
   1133 }
   1134 
   1135 /*
   1136  * Perform special action on behalf of the user
   1137  * Only does generic scsi ioctls.
   1138  */
   1139 int
   1140 seioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1141 {
   1142 	struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
   1143 
   1144 	return (scsipi_do_ioctl(sc->sc_periph, dev, cmd, addr, flag, l));
   1145 }
   1146