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