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