Home | History | Annotate | Line # | Download | only in scsipi
if_se.c revision 1.17
      1 /*	$NetBSD: if_se.c,v 1.17 1998/07/05 06:49:16 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 "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 };
    200 
    201 cdev_decl(se);
    202 
    203 #ifdef __BROKEN_INDIRECT_CONFIG
    204 static int	sematch __P((struct device *, void *, void *));
    205 #else
    206 static int	sematch __P((struct device *, struct cfdata *, void *));
    207 #endif
    208 static void	seattach __P((struct device *, struct device *, void *));
    209 
    210 static void	se_ifstart __P((struct ifnet *));
    211 static void	sestart __P((void *));
    212 
    213 static void	sedone __P((struct scsipi_xfer *));
    214 static int	se_ioctl __P((struct ifnet *, u_long, caddr_t));
    215 static void	sewatchdog __P((struct ifnet *));
    216 
    217 static __inline u_int16_t ether_cmp __P((void *, void *));
    218 static void	se_recv __P((void *));
    219 static struct mbuf *se_get __P((struct se_softc *, char *, int));
    220 static int	se_read __P((struct se_softc *, char *, int));
    221 static int	se_reset __P((struct se_softc *));
    222 static int	se_add_proto __P((struct se_softc *, int));
    223 static int	se_get_addr __P((struct se_softc *, u_int8_t *));
    224 static int	se_set_media __P((struct se_softc *, int));
    225 static int	se_init __P((struct se_softc *));
    226 static int	se_set_multi __P((struct se_softc *, u_int8_t *));
    227 static int	se_remove_multi __P((struct se_softc *, u_int8_t *));
    228 #if 0
    229 static int	sc_set_all_multi __P((struct se_softc *, int));
    230 #endif
    231 static void	se_stop __P((struct se_softc *));
    232 static __inline int se_scsipi_cmd __P((struct scsipi_link *sc_link,
    233 			struct scsipi_generic *scsipi_cmd,
    234 			int cmdlen, u_char *data_addr, int datalen,
    235 			int retries, int timeout, struct buf *bp,
    236 			int flags));
    237 static void	se_delayed_ifstart __P((void *));
    238 static int	se_set_mode(struct se_softc *, int, int);
    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 #ifdef __BROKEN_INDIRECT_CONFIG
    284 	void *match;
    285 #else
    286 	struct cfdata *match;
    287 #endif
    288 	void *aux;
    289 {
    290 	struct scsipibus_attach_args *sa = aux;
    291 	int priority;
    292 
    293 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    294 	    (caddr_t)se_patterns, sizeof(se_patterns) / sizeof(se_patterns[0]),
    295 	    sizeof(se_patterns[0]), &priority);
    296 	return (priority);
    297 }
    298 
    299 /*
    300  * The routine called by the low level scsi routine when it discovers
    301  * a device suitable for this driver.
    302  */
    303 static void
    304 seattach(parent, self, aux)
    305 	struct device *parent, *self;
    306 	void *aux;
    307 {
    308 	struct se_softc *sc = (void *)self;
    309 	struct scsipibus_attach_args *sa = aux;
    310 	struct scsipi_link *sc_link = sa->sa_sc_link;
    311 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    312 	u_int8_t myaddr[ETHER_ADDR_LEN];
    313 
    314 	printf("\n");
    315 	SC_DEBUG(sc_link, SDEV_DB2, ("seattach: "));
    316 
    317 	/*
    318 	 * Store information needed to contact our base driver
    319 	 */
    320 	sc->sc_link = sc_link;
    321 	sc_link->device = &se_switch;
    322 	sc_link->device_softc = sc;
    323 	if (sc_link->openings > SEOUTSTANDING)
    324 		sc_link->openings = SEOUTSTANDING;
    325 
    326 	se_poll = (SE_POLL * hz) / 1000;
    327 	se_poll = se_poll? se_poll: 1;
    328 	se_poll0 = (SE_POLL0 * hz) / 1000;
    329 	se_poll0 = se_poll0? se_poll0: 1;
    330 
    331 	/*
    332 	 * Initialize and attach a buffer
    333 	 */
    334 	sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header),
    335 			     M_DEVBUF, M_NOWAIT);
    336 	if (sc->sc_tbuf == 0)
    337 		panic("seattach: can't allocate transmit buffer");
    338 
    339 	sc->sc_rbuf = malloc(RBUF_LEN, M_DEVBUF, M_NOWAIT);/* A Guess */
    340 	if (sc->sc_rbuf == 0)
    341 		panic("seattach: can't allocate receive buffer");
    342 
    343 	se_get_addr(sc, myaddr);
    344 
    345 	/* Initialize ifnet structure. */
    346 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    347 	ifp->if_softc = sc;
    348 	ifp->if_start = se_ifstart;
    349 	ifp->if_ioctl = se_ioctl;
    350 	ifp->if_watchdog = sewatchdog;
    351 	ifp->if_flags =
    352 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    353 
    354 	/* Attach the interface. */
    355 	if_attach(ifp);
    356 	ether_ifattach(ifp, myaddr);
    357 
    358 #if NBPFILTER > 0
    359 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    360 #endif
    361 }
    362 
    363 
    364 static __inline int
    365 se_scsipi_cmd(sc_link, scsipi_cmd, cmdlen, data_addr, datalen,
    366 		       retries, timeout, bp, flags)
    367 	struct scsipi_link *sc_link;
    368 	struct scsipi_generic *scsipi_cmd;
    369 	int cmdlen;
    370 	u_char *data_addr;
    371 	int datalen;
    372 	int retries;
    373 	int timeout;
    374 	struct buf *bp;
    375 	int flags;
    376 {
    377 	int error;
    378 	int s = splbio();
    379 
    380 	error = scsipi_command(sc_link, scsipi_cmd, cmdlen, data_addr,
    381 	    datalen, retries, timeout, bp, flags);
    382 	splx(s);
    383 	return (error);
    384 }
    385 
    386 /* Start routine for calling from scsi sub system */
    387 static void
    388 sestart(v)
    389 	void *v;
    390 {
    391 	struct se_softc *sc = (struct se_softc *) v;
    392 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    393 	int s = splnet();
    394 
    395 	se_ifstart(ifp);
    396 	(void) splx(s);
    397 }
    398 
    399 static void
    400 se_delayed_ifstart(v)
    401 	void *v;
    402 {
    403 	struct ifnet *ifp = v;
    404 	int s = splnet();
    405 
    406 	ifp->if_flags &= ~IFF_OACTIVE;
    407 	se_ifstart(ifp);
    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 	PROTOCMD(ctron_ether_recv, recv_cmd);
    563 
    564 	error = se_scsipi_cmd(sc->sc_link,
    565 	    (struct scsipi_generic *)&recv_cmd, sizeof(recv_cmd),
    566 	    sc->sc_rbuf, RBUF_LEN, SERETRIES, SETIMEOUT, NULL,
    567 	    SCSI_NOSLEEP|SCSI_DATA_IN);
    568 	if (error)
    569 		timeout(se_recv, (void *)sc, se_poll);
    570 }
    571 
    572 /*
    573  * We copy the data into mbufs.  When full cluster sized units are present
    574  * we copy into clusters.
    575  */
    576 static struct mbuf *
    577 se_get(sc, data, totlen)
    578 	struct se_softc *sc;
    579 	char *data;
    580 	int totlen;
    581 {
    582 	struct mbuf *m;
    583 	struct mbuf *top, **mp;
    584 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    585 	int len, pad;
    586 
    587 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    588 	if (m == 0)
    589 		return (0);
    590 	m->m_pkthdr.rcvif = ifp;
    591 	m->m_pkthdr.len = totlen;
    592 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
    593 	m->m_data += pad;
    594 	len = MHLEN - pad;
    595 	top = 0;
    596 	mp = &top;
    597 
    598 	while (totlen > 0) {
    599 		if (top) {
    600 			MGET(m, M_DONTWAIT, MT_DATA);
    601 			if (m == 0) {
    602 				m_freem(top);
    603 				return (0);
    604 			}
    605 			len = MLEN;
    606 		}
    607 		if (totlen >= MINCLSIZE) {
    608 			MCLGET(m, M_DONTWAIT);
    609 			if ((m->m_flags & M_EXT) == 0) {
    610 				m_free(m);
    611 				m_freem(top);
    612 				return (0);
    613 			}
    614 			len = MCLBYTES;
    615 		}
    616 		m->m_len = len = min(totlen, len);
    617 		bcopy(data, mtod(m, caddr_t), len);
    618 		data += len;
    619 		totlen -= len;
    620 		*mp = m;
    621 		mp = &m->m_next;
    622 	}
    623 
    624 	return (top);
    625 }
    626 
    627 /*
    628  * Pass packets to higher levels.
    629  */
    630 static int
    631 se_read(sc, data, datalen)
    632 	register struct se_softc *sc;
    633 	char *data;
    634 	int datalen;
    635 {
    636 	struct mbuf *m;
    637 	struct ether_header *eh;
    638 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    639 	int n;
    640 
    641 	n = 0;
    642 	while (datalen >= 2) {
    643 		int len = _2btol(data);
    644 		data += 2;
    645 		datalen -= 2;
    646 
    647 		if (len == 0)
    648 			break;
    649 #ifdef SEDEBUG
    650 		if (sc->sc_debug) {
    651 			printf("se_read: datalen = %d, packetlen = %d, proto = 0x%04x\n", datalen, len,
    652 			 ntohs(((struct ether_header *)data)->ether_type));
    653 		}
    654 #endif
    655 		if (len <= sizeof(struct ether_header) ||
    656 		    len > MAX_SNAP) {
    657 #ifdef SEDEBUG
    658 			printf("%s: invalid packet size %d; dropping\n",
    659 			       sc->sc_dev.dv_xname, len);
    660 #endif
    661 			ifp->if_ierrors++;
    662 			goto next_packet;
    663 		}
    664 
    665 		/* Don't need crc. Must keep ether header for BPF */
    666 		m = se_get(sc, data, len - ETHER_CRC);
    667 		if (m == 0) {
    668 #ifdef SEDEBUG
    669 			if (sc->sc_debug)
    670 				printf("se_read: se_get returned null\n");
    671 #endif
    672 			ifp->if_ierrors++;
    673 			goto next_packet;
    674 		}
    675 		if ((ifp->if_flags & IFF_PROMISC) != 0) {
    676 			m_adj(m, SE_PREFIX);
    677 		}
    678 		ifp->if_ipackets++;
    679 
    680 		/* We assume that the header fit entirely in one mbuf. */
    681 		eh = mtod(m, struct ether_header *);
    682 
    683 #if NBPFILTER > 0
    684 		/*
    685 		 * Check if there's a BPF listener on this interface.
    686 		 * If so, hand off the raw packet to BPF.
    687 		 */
    688 		if (ifp->if_bpf) {
    689 			bpf_mtap(ifp->if_bpf, m);
    690 
    691 			/* Note that the interface cannot be in
    692 			 * promiscuous mode if there are no BPF
    693 			 * listeners.  And if we are in promiscuous
    694 			 * mode, we have to check if this packet is
    695 			 * really ours.
    696 			 */
    697 			if ((ifp->if_flags & IFF_PROMISC) != 0 &&
    698 			    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    699 			    ETHER_CMP(eh->ether_dhost, LLADDR(ifp->if_sadl))) {
    700 				m_freem(m);
    701 				goto next_packet;
    702 			}
    703 		}
    704 #endif
    705 
    706 		/* Pass the packet up, with the ether header sort-of removed. */
    707 		m_adj(m, sizeof(struct ether_header));
    708 		ether_input(ifp, eh, m);
    709 
    710 	next_packet:
    711 		data += len;
    712 		datalen -= len;
    713 		n++;
    714 	}
    715 	return (n);
    716 }
    717 
    718 
    719 static void
    720 sewatchdog(ifp)
    721 	struct ifnet *ifp;
    722 {
    723 	struct se_softc *sc = ifp->if_softc;
    724 
    725 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    726 	++ifp->if_oerrors;
    727 
    728 	se_reset(sc);
    729 }
    730 
    731 static int
    732 se_reset(sc)
    733 	struct se_softc *sc;
    734 {
    735 	int error;
    736 	int s = splnet();
    737 #if 0
    738 	/* Maybe we don't *really* want to reset the entire bus
    739 	 * because the ctron isn't working. We would like to send a
    740 	 * "BUS DEVICE RESET" message, but don't think the ctron
    741 	 * understands it.
    742 	 */
    743 	error = se_scsipi_cmd(sc->sc_link, 0, 0, 0, 0, SERETRIES, 2000, NULL,
    744 	    SCSI_RESET);
    745 #endif
    746 	error = se_init(sc);
    747 	splx(s);
    748 	return (error);
    749 }
    750 
    751 static int
    752 se_add_proto(sc, proto)
    753 	struct se_softc *sc;
    754 	int proto;
    755 {
    756 	int error;
    757 	struct scsi_ctron_ether_generic add_proto_cmd;
    758 	u_int8_t data[2];
    759 	_lto2b(proto, data);
    760 #ifdef SEDEBUG
    761 	if (sc->sc_debug)
    762 		printf("se: adding proto 0x%02x%02x\n", data[0], data[1]);
    763 #endif
    764 
    765 	PROTOCMD(ctron_ether_add_proto, add_proto_cmd);
    766 	_lto2b(sizeof(data), add_proto_cmd.length);
    767 	error = se_scsipi_cmd(sc->sc_link,
    768 	    (struct scsipi_generic *) &add_proto_cmd, sizeof(add_proto_cmd),
    769 	    data, sizeof(data), SERETRIES, SETIMEOUT, NULL, SCSI_DATA_OUT);
    770 	return (error);
    771 }
    772 
    773 static int
    774 se_get_addr(sc, myaddr)
    775 	struct se_softc *sc;
    776 	u_int8_t *myaddr;
    777 {
    778 	int error;
    779 	struct scsi_ctron_ether_generic get_addr_cmd;
    780 
    781 	PROTOCMD(ctron_ether_get_addr, get_addr_cmd);
    782 	_lto2b(ETHER_ADDR_LEN, get_addr_cmd.length);
    783 	error = se_scsipi_cmd(sc->sc_link,
    784 	    (struct scsipi_generic *) &get_addr_cmd, sizeof(get_addr_cmd),
    785 	    myaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL, SCSI_DATA_IN);
    786 	printf("%s: ethernet address %s\n", sc->sc_dev.dv_xname,
    787 	    ether_sprintf(myaddr));
    788 	return (error);
    789 }
    790 
    791 
    792 static int
    793 se_set_media(sc, type)
    794 	struct se_softc *sc;
    795 	int type;
    796 {
    797 	int error;
    798 	struct scsi_ctron_ether_generic set_media_cmd;
    799 
    800 	PROTOCMD(ctron_ether_set_media, set_media_cmd);
    801 	set_media_cmd.byte3 = type;
    802 	error = se_scsipi_cmd(sc->sc_link,
    803 	    (struct scsipi_generic *) &set_media_cmd, sizeof(set_media_cmd),
    804 	    0, 0, SERETRIES, SETIMEOUT, NULL, 0);
    805 	return (error);
    806 }
    807 
    808 static int
    809 se_set_mode(sc, len, mode)
    810 	struct se_softc *sc;
    811 	int len;
    812 	int mode;
    813 {
    814 	int error;
    815 	struct scsi_ctron_ether_set_mode set_mode_cmd;
    816 
    817 	PROTOCMD(ctron_ether_set_mode, set_mode_cmd);
    818 	set_mode_cmd.mode = mode;
    819 	_lto2b(len, set_mode_cmd.length);
    820 	error = se_scsipi_cmd(sc->sc_link,
    821 	    (struct scsipi_generic *) &set_mode_cmd, sizeof(set_mode_cmd),
    822 	    0, 0, SERETRIES, SETIMEOUT, NULL, 0);
    823 	return (error);
    824 }
    825 
    826 
    827 static int
    828 se_init(sc)
    829 	struct se_softc *sc;
    830 {
    831 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    832 	struct scsi_ctron_ether_generic set_addr_cmd;
    833 	int error;
    834 
    835 #if NBPFILTER > 0
    836 	if (ifp->if_flags & IFF_PROMISC) {
    837 		error = se_set_mode(sc, MAX_SNAP, 1);
    838 	}
    839 	else
    840 #endif
    841 		error = se_set_mode(sc, ETHERMTU + sizeof(struct ether_header),
    842 		    0);
    843 	if (error != 0)
    844 		return (error);
    845 
    846 	PROTOCMD(ctron_ether_set_addr, set_addr_cmd);
    847 	_lto2b(ETHER_ADDR_LEN, set_addr_cmd.length);
    848 	error = se_scsipi_cmd(sc->sc_link,
    849 	    (struct scsipi_generic *) &set_addr_cmd, sizeof(set_addr_cmd),
    850 	    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
    851 	    SCSI_DATA_OUT);
    852 	if (error != 0)
    853 		return (error);
    854 
    855 	if ((sc->protos & PROTO_IP) &&
    856 	    (error = se_add_proto(sc, ETHERTYPE_IP)) != 0)
    857 		return (error);
    858 	if ((sc->protos & PROTO_ARP) &&
    859 	    (error = se_add_proto(sc, ETHERTYPE_ARP)) != 0)
    860 		return (error);
    861 	if ((sc->protos & PROTO_REVARP) &&
    862 	    (error = se_add_proto(sc, ETHERTYPE_REVARP)) != 0)
    863 		return (error);
    864 #ifdef NETATALK
    865 	if ((sc->protos & PROTO_AT) &&
    866 	    (error = se_add_proto(sc, ETHERTYPE_AT)) != 0)
    867 		return (error);
    868 	if ((sc->protos & PROTO_AARP) &&
    869 	    (error = se_add_proto(sc, ETHERTYPE_AARP)) != 0)
    870 		return (error);
    871 #endif
    872 
    873 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == IFF_UP) {
    874 		ifp->if_flags |= IFF_RUNNING;
    875 		se_recv(sc);
    876 		ifp->if_flags &= ~IFF_OACTIVE;
    877 		se_ifstart(ifp);
    878 	}
    879 	return (error);
    880 }
    881 
    882 static int
    883 se_set_multi(sc, addr)
    884 	struct se_softc *sc;
    885 	u_int8_t *addr;
    886 {
    887 	struct scsi_ctron_ether_generic set_multi_cmd;
    888 	int error;
    889 
    890 	if (sc->sc_debug)
    891 		printf("%s: set_set_multi: %s\n", sc->sc_dev.dv_xname,
    892 		    ether_sprintf(addr));
    893 
    894 	PROTOCMD(ctron_ether_set_multi, set_multi_cmd);
    895 	_lto2b(sizeof(addr), set_multi_cmd.length);
    896 	error = se_scsipi_cmd(sc->sc_link,
    897 	    (struct scsipi_generic *) &set_multi_cmd, sizeof(set_multi_cmd),
    898 	    addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, SCSI_DATA_OUT);
    899 	return (error);
    900 }
    901 
    902 static int
    903 se_remove_multi(sc, addr)
    904 	struct se_softc *sc;
    905 	u_int8_t *addr;
    906 {
    907 	struct scsi_ctron_ether_generic remove_multi_cmd;
    908 	int error;
    909 
    910 	if (sc->sc_debug)
    911 		printf("%s: se_remove_multi: %s\n", sc->sc_dev.dv_xname,
    912 		    ether_sprintf(addr));
    913 
    914 	PROTOCMD(ctron_ether_remove_multi, remove_multi_cmd);
    915 	_lto2b(sizeof(addr), remove_multi_cmd.length);
    916 	error = se_scsipi_cmd(sc->sc_link,
    917 	    (struct scsipi_generic *) &remove_multi_cmd,
    918 	    sizeof(remove_multi_cmd),
    919 	    addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, SCSI_DATA_OUT);
    920 	return (error);
    921 }
    922 
    923 #if 0	/* not used  --thorpej */
    924 static int
    925 sc_set_all_multi(sc, set)
    926 	struct se_softc *sc;
    927 	int set;
    928 {
    929 	int error = 0;
    930 	u_int8_t *addr;
    931 	struct ethercom *ac = &sc->sc_ethercom;
    932 	struct ether_multi *enm;
    933 	struct ether_multistep step;
    934 
    935 	ETHER_FIRST_MULTI(step, ac, enm);
    936 	while (enm != NULL) {
    937 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    938 			/*
    939 			 * We must listen to a range of multicast addresses.
    940 			 * For now, just accept all multicasts, rather than
    941 			 * trying to set only those filter bits needed to match
    942 			 * the range.  (At this time, the only use of address
    943 			 * ranges is for IP multicast routing, for which the
    944 			 * range is big enough to require all bits set.)
    945 			 */
    946 			/* We have no way of adding a range to this device.
    947 			 * stepping through all addresses in the range is
    948 			 * typically not possible. The only real alternative
    949 			 * is to go into promicuous mode and filter by hand.
    950 			 */
    951 			return (ENODEV);
    952 
    953 		}
    954 
    955 		addr = enm->enm_addrlo;
    956 		if ((error = set ? se_set_multi(sc, addr) :
    957 		    se_remove_multi(sc, addr)) != 0)
    958 			return (error);
    959 		ETHER_NEXT_MULTI(step, enm);
    960 	}
    961 	return (error);
    962 }
    963 #endif /* not used */
    964 
    965 static void
    966 se_stop(sc)
    967 	struct se_softc *sc;
    968 {
    969 
    970 	/* Don't schedule any reads */
    971 	untimeout(se_recv, sc);
    972 
    973 	/* How can we abort any scsi cmds in progress? */
    974 }
    975 
    976 
    977 /*
    978  * Process an ioctl request.
    979  */
    980 static int
    981 se_ioctl(ifp, cmd, data)
    982 	register struct ifnet *ifp;
    983 	u_long cmd;
    984 	caddr_t data;
    985 {
    986 	register struct se_softc *sc = ifp->if_softc;
    987 	struct ifaddr *ifa = (struct ifaddr *)data;
    988 	struct ifreq *ifr = (struct ifreq *)data;
    989 	int s, error = 0;
    990 
    991 	s = splnet();
    992 
    993 	switch (cmd) {
    994 
    995 	case SIOCSIFADDR:
    996 		ifp->if_flags |= IFF_UP;
    997 
    998 		if ((error = se_set_media(sc, CMEDIA_AUTOSENSE) != 0))
    999 			return (error);
   1000 
   1001 		switch (ifa->ifa_addr->sa_family) {
   1002 #ifdef INET
   1003 		case AF_INET:
   1004 			sc->protos |= (PROTO_IP | PROTO_ARP | PROTO_REVARP);
   1005 			if ((error = se_init(sc)) != 0)
   1006 				break;
   1007 			arp_ifinit(ifp, ifa);
   1008 			break;
   1009 #endif
   1010 #ifdef NS
   1011 		case AF_NS:
   1012 		    {
   1013 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1014 
   1015 			if (ns_nullhost(*ina))
   1016 				ina->x_host =
   1017 				    *(union ns_host *)LLADDR(ifp->if_sadl);
   1018 			else
   1019 				bcopy(ina->x_host.c_host,
   1020 				    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   1021 			/* Set new address. */
   1022 
   1023 			error = se_init(sc);
   1024 			break;
   1025 		    }
   1026 #endif
   1027 #ifdef NETATALK
   1028 		case AF_APPLETALK:
   1029 			sc->protos |= (PROTO_AT | PROTO_AARP);
   1030 			if ((error = se_init(sc)) != 0)
   1031 				break;
   1032 			break;
   1033 #endif
   1034 		default:
   1035 			error = se_init(sc);
   1036 			break;
   1037 		}
   1038 		break;
   1039 
   1040 #if defined(CCITT) && defined(LLC)
   1041 	case SIOCSIFCONF_X25:
   1042 		ifp->if_flags |= IFF_UP;
   1043 		ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
   1044 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
   1045 		if (error == 0)
   1046 			error = se_init(sc);
   1047 		break;
   1048 #endif /* CCITT && LLC */
   1049 
   1050 	case SIOCSIFFLAGS:
   1051 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1052 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1053 			/*
   1054 			 * If interface is marked down and it is running, then
   1055 			 * stop it.
   1056 			 */
   1057 			se_stop(sc);
   1058 			ifp->if_flags &= ~IFF_RUNNING;
   1059 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1060 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
   1061 			/*
   1062 			 * If interface is marked up and it is stopped, then
   1063 			 * start it.
   1064 			 */
   1065 			error = se_init(sc);
   1066 		} else {
   1067 			/*
   1068 			 * Reset the interface to pick up changes in any other
   1069 			 * flags that affect hardware registers.
   1070 			 */
   1071 			error = se_init(sc);
   1072 		}
   1073 #ifdef SEDEBUG
   1074 		if (ifp->if_flags & IFF_DEBUG)
   1075 			sc->sc_debug = 1;
   1076 		else
   1077 			sc->sc_debug = 0;
   1078 #endif
   1079 		break;
   1080 
   1081 	case SIOCADDMULTI:
   1082 		if (ether_addmulti(ifr, &sc->sc_ethercom) == ENETRESET)
   1083 			error = se_set_multi(sc, ifr->ifr_addr.sa_data);
   1084 		else
   1085 			error = 0;
   1086 		break;
   1087 	case SIOCDELMULTI:
   1088 		if (ether_delmulti(ifr, &sc->sc_ethercom) == ENETRESET)
   1089 			error = se_remove_multi(sc, ifr->ifr_addr.sa_data);
   1090 		else
   1091 			error = 0;
   1092 		break;
   1093 
   1094 	default:
   1095 
   1096 		error = EINVAL;
   1097 		break;
   1098 	}
   1099 
   1100 	splx(s);
   1101 	return (error);
   1102 }
   1103 
   1104 #define	SEUNIT(z)	(minor(z))
   1105 /*
   1106  * open the device.
   1107  */
   1108 int
   1109 seopen(dev, flag, fmt, p)
   1110 	dev_t dev;
   1111 	int flag, fmt;
   1112 	struct proc *p;
   1113 {
   1114 	int unit;
   1115 	struct se_softc *sc;
   1116 	struct scsipi_link *sc_link;
   1117 
   1118 	unit = SEUNIT(dev);
   1119 	if (unit >= se_cd.cd_ndevs)
   1120 		return (ENXIO);
   1121 	sc = se_cd.cd_devs[unit];
   1122 	if (sc == NULL)
   1123 		return (ENXIO);
   1124 
   1125 	sc_link = sc->sc_link;
   1126 
   1127 	SC_DEBUG(sc_link, SDEV_DB1,
   1128 	    ("scopen: dev=0x%x (unit %d (of %d))\n", dev, unit,
   1129 	    se_cd.cd_ndevs));
   1130 
   1131 	sc_link->flags |= SDEV_OPEN;
   1132 
   1133 	SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
   1134 	return (0);
   1135 }
   1136 
   1137 /*
   1138  * close the device.. only called if we are the LAST
   1139  * occurence of an open device
   1140  */
   1141 int
   1142 seclose(dev, flag, fmt, p)
   1143 	dev_t dev;
   1144 	int flag, fmt;
   1145 	struct proc *p;
   1146 {
   1147 	struct se_softc *sc = se_cd.cd_devs[SEUNIT(dev)];
   1148 
   1149 	SC_DEBUG(sc->sc_link, SDEV_DB1, ("closing\n"));
   1150 	sc->sc_link->flags &= ~SDEV_OPEN;
   1151 
   1152 	return (0);
   1153 }
   1154 
   1155 /*
   1156  * Perform special action on behalf of the user
   1157  * Only does generic scsi ioctls.
   1158  */
   1159 int
   1160 seioctl(dev, cmd, addr, flag, p)
   1161 	dev_t dev;
   1162 	u_long cmd;
   1163 	caddr_t addr;
   1164 	int flag;
   1165 	struct proc *p;
   1166 {
   1167 	register struct se_softc *sc = se_cd.cd_devs[SEUNIT(dev)];
   1168 
   1169 	return (scsipi_do_ioctl(sc->sc_link, dev, cmd, addr, flag, p));
   1170 }
   1171