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