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