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