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