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