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