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