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