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