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