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