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