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