xref: /netbsd-src/sys/dev/scsipi/if_se.c (revision 404ee5b9334f618040b6cdef96a0ff35a6fc4636)
1 /*	$NetBSD: if_se.c,v 1.103 2019/11/10 21:16:37 chs 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.103 2019/11/10 21:16:37 chs 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 #include <net/bpf.h>
99 
100 #ifdef INET
101 #include <netinet/in.h>
102 #include <netinet/if_inarp.h>
103 #endif
104 
105 
106 #ifdef NETATALK
107 #include <netatalk/at.h>
108 #endif
109 
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 #if 0
205 static inline uint16_t ether_cmp(void *, void *);
206 #endif
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 *, uint8_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 *, uint8_t *);
216 static int	se_remove_multi(struct se_softc *, uint8_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 #if 0
271 /*
272  * Compare two Ether/802 addresses for equality, inlined and
273  * unrolled for speed.
274  * Note: use this like memcmp()
275  */
276 static inline uint16_t
277 ether_cmp(void *one, void *two)
278 {
279 	uint16_t *a = (uint16_t *) one;
280 	uint16_t *b = (uint16_t *) two;
281 	uint16_t diff;
282 
283 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
284 
285 	return (diff);
286 }
287 
288 #define ETHER_CMP	ether_cmp
289 #endif
290 
291 static int
292 sematch(device_t parent, cfdata_t match, void *aux)
293 {
294 	struct scsipibus_attach_args *sa = aux;
295 	int priority;
296 
297 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
298 	    se_patterns, sizeof(se_patterns) / sizeof(se_patterns[0]),
299 	    sizeof(se_patterns[0]), &priority);
300 	return (priority);
301 }
302 
303 /*
304  * The routine called by the low level scsi routine when it discovers
305  * a device suitable for this driver.
306  */
307 static void
308 seattach(device_t parent, device_t self, void *aux)
309 {
310 	struct se_softc *sc = device_private(self);
311 	struct scsipibus_attach_args *sa = aux;
312 	struct scsipi_periph *periph = sa->sa_periph;
313 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
314 	uint8_t myaddr[ETHER_ADDR_LEN];
315 	int rv;
316 
317 	sc->sc_dev = self;
318 
319 	printf("\n");
320 	SC_DEBUG(periph, SCSIPI_DB2, ("seattach: "));
321 
322 	callout_init(&sc->sc_ifstart_ch, 0);
323 	callout_init(&sc->sc_recv_ch, 0);
324 
325 	/*
326 	 * Store information needed to contact our base driver
327 	 */
328 	sc->sc_periph = periph;
329 	periph->periph_dev = sc->sc_dev;
330 	periph->periph_switch = &se_switch;
331 
332 	/* XXX increase openings? */
333 
334 	se_poll = (SE_POLL * hz) / 1000;
335 	se_poll = se_poll? se_poll: 1;
336 	se_poll0 = (SE_POLL0 * hz) / 1000;
337 	se_poll0 = se_poll0? se_poll0: 1;
338 
339 	/*
340 	 * Initialize and attach a buffer
341 	 */
342 	sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header),
343 			     M_DEVBUF, M_WAITOK);
344 	sc->sc_rbuf = malloc(RBUF_LEN, M_DEVBUF, M_WAITOK);/* A Guess */
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 = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
355 	IFQ_SET_READY(&ifp->if_snd);
356 
357 	/* Attach the interface. */
358 	rv = if_initialize(ifp);
359 	if (rv != 0) {
360 		free(sc->sc_tbuf, M_DEVBUF);
361 		callout_destroy(&sc->sc_ifstart_ch);
362 		callout_destroy(&sc->sc_recv_ch);
363 		return; /* Error */
364 	}
365 	ether_ifattach(ifp, myaddr);
366 	if_register(ifp);
367 }
368 
369 
370 static inline int
371 se_scsipi_cmd(struct scsipi_periph *periph, struct scsipi_generic *cmd,
372     int cmdlen, u_char *data_addr, int datalen, int retries, int timeout,
373     struct buf *bp, int flags)
374 {
375 	int error;
376 
377 	error = scsipi_command(periph, cmd, cmdlen, data_addr,
378 	    datalen, retries, timeout, bp, flags);
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, BPF_D_OUT);
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 		m = m0 = m_free(m);
448 	}
449 	if (len < SEMINSIZE) {
450 #ifdef SEDEBUG
451 		if (sc->sc_debug)
452 			printf("se: packet size %d (%zu) < %d\n", len,
453 			    cp - (u_char *)sc->sc_tbuf, SEMINSIZE);
454 #endif
455 		memset(cp, 0, SEMINSIZE - len);
456 		len = SEMINSIZE;
457 	}
458 
459 	/* Fill out SCSI command. */
460 	PROTOCMD(ctron_ether_send, send_cmd);
461 	_lto2b(len, send_cmd.length);
462 
463 	/* Send command to device. */
464 	error = se_scsipi_cmd(sc->sc_periph,
465 	    (void *)&send_cmd, sizeof(send_cmd),
466 	    sc->sc_tbuf, len, SERETRIES,
467 	    SETIMEOUT, NULL, XS_CTL_NOSLEEP | XS_CTL_ASYNC | XS_CTL_DATA_OUT);
468 	if (error) {
469 		aprint_error_dev(sc->sc_dev, "not queued, error %d\n", error);
470 		ifp->if_oerrors++;
471 		ifp->if_flags &= ~IFF_OACTIVE;
472 	} else
473 		ifp->if_opackets++;
474 	if (sc->sc_flags & SE_NEED_RECV) {
475 		sc->sc_flags &= ~SE_NEED_RECV;
476 		se_recv((void *) sc);
477 	}
478 }
479 
480 
481 /*
482  * Called from the scsibus layer via our scsi device switch.
483  */
484 static void
485 sedone(struct scsipi_xfer *xs, int error)
486 {
487 	struct se_softc *sc = device_private(xs->xs_periph->periph_dev);
488 	struct scsipi_generic *cmd = xs->cmd;
489 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
490 	int s;
491 
492 	s = splnet();
493 	if (IS_SEND(cmd)) {
494 		if (xs->error == XS_BUSY) {
495 			printf("se: busy, retry txmit\n");
496 			callout_reset(&sc->sc_ifstart_ch, hz,
497 			    se_delayed_ifstart, ifp);
498 		} else {
499 			ifp->if_flags &= ~IFF_OACTIVE;
500 			/* the generic scsipi_done will call
501 			 * sestart (through scsipi_free_xs).
502 			 */
503 		}
504 	} else if (IS_RECV(cmd)) {
505 		/* RECV complete */
506 		/* pass data up. reschedule a recv */
507 		/* scsipi_free_xs will call start. Harmless. */
508 		if (error) {
509 			/* Reschedule after a delay */
510 			callout_reset(&sc->sc_recv_ch, se_poll,
511 			    se_recv, (void *)sc);
512 		} else {
513 			int n, ntimeo;
514 			n = se_read(sc, xs->data, xs->datalen - xs->resid);
515 			if (n > se_max_received)
516 				se_max_received = n;
517 			if (n == 0)
518 				ntimeo = se_poll;
519 			else if (n >= RDATA_MAX)
520 				ntimeo = se_poll0;
521 			else {
522 				ntimeo = sc->sc_last_timeout;
523 				ntimeo = (ntimeo * RDATA_GOAL)/n;
524 				ntimeo = (ntimeo < se_poll0?
525 					  se_poll0: ntimeo);
526 				ntimeo = (ntimeo > se_poll?
527 					  se_poll: ntimeo);
528 			}
529 			sc->sc_last_timeout = ntimeo;
530 			if (ntimeo == se_poll0	&&
531 			    IFQ_IS_EMPTY(&ifp->if_snd) == 0)
532 				/* Output is pending. Do next recv
533 				 * after the next send. */
534 				sc->sc_flags |= SE_NEED_RECV;
535 			else {
536 				callout_reset(&sc->sc_recv_ch, ntimeo,
537 				    se_recv, (void *)sc);
538 			}
539 		}
540 	}
541 	splx(s);
542 }
543 
544 static void
545 se_recv(void *v)
546 {
547 	/* do a recv command */
548 	struct se_softc *sc = (struct se_softc *) v;
549 	struct scsi_ctron_ether_recv recv_cmd;
550 	int error;
551 
552 	if (sc->sc_enabled == 0)
553 		return;
554 
555 	PROTOCMD(ctron_ether_recv, recv_cmd);
556 
557 	error = se_scsipi_cmd(sc->sc_periph,
558 	    (void *)&recv_cmd, sizeof(recv_cmd),
559 	    sc->sc_rbuf, RBUF_LEN, SERETRIES, SETIMEOUT, NULL,
560 	    XS_CTL_NOSLEEP | XS_CTL_ASYNC | XS_CTL_DATA_IN);
561 	if (error)
562 		callout_reset(&sc->sc_recv_ch, se_poll, se_recv, (void *)sc);
563 }
564 
565 /*
566  * We copy the data into mbufs.  When full cluster sized units are present
567  * we copy into clusters.
568  */
569 static struct mbuf *
570 se_get(struct se_softc *sc, char *data, int totlen)
571 {
572 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
573 	struct mbuf *m, *m0, *newm;
574 	int len;
575 
576 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
577 	if (m0 == 0)
578 		return (0);
579 	m_set_rcvif(m0, ifp);
580 	m0->m_pkthdr.len = totlen;
581 	len = MHLEN;
582 	m = m0;
583 
584 	while (totlen > 0) {
585 		if (totlen >= MINCLSIZE) {
586 			MCLGET(m, M_DONTWAIT);
587 			if ((m->m_flags & M_EXT) == 0)
588 				goto bad;
589 			len = MCLBYTES;
590 		}
591 
592 		if (m == m0) {
593 			char *newdata = (char *)
594 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
595 			    sizeof(struct ether_header);
596 			len -= newdata - m->m_data;
597 			m->m_data = newdata;
598 		}
599 
600 		m->m_len = len = uimin(totlen, len);
601 		memcpy(mtod(m, void *), data, len);
602 		data += len;
603 
604 		totlen -= len;
605 		if (totlen > 0) {
606 			MGET(newm, M_DONTWAIT, MT_DATA);
607 			if (newm == 0)
608 				goto bad;
609 			len = MLEN;
610 			m = m->m_next = newm;
611 		}
612 	}
613 
614 	return (m0);
615 
616 bad:
617 	m_freem(m0);
618 	return (0);
619 }
620 
621 /*
622  * Pass packets to higher levels.
623  */
624 static int
625 se_read(struct se_softc *sc, char *data, int datalen)
626 {
627 	struct mbuf *m;
628 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
629 	int n;
630 
631 	n = 0;
632 	while (datalen >= 2) {
633 		int len = _2btol(data);
634 		data += 2;
635 		datalen -= 2;
636 
637 		if (len == 0)
638 			break;
639 #ifdef SEDEBUG
640 		if (sc->sc_debug) {
641 			printf("se_read: datalen = %d, packetlen = %d, proto = 0x%04x\n", datalen, len,
642 			 ntohs(((struct ether_header *)data)->ether_type));
643 		}
644 #endif
645 		if (len <= sizeof(struct ether_header) ||
646 		    len > MAX_SNAP) {
647 #ifdef SEDEBUG
648 			printf("%s: invalid packet size %d; dropping\n",
649 			       device_xname(sc->sc_dev), len);
650 #endif
651 			ifp->if_ierrors++;
652 			goto next_packet;
653 		}
654 
655 		/* Don't need crc. Must keep ether header for BPF */
656 		m = se_get(sc, data, len - ETHER_CRC);
657 		if (m == 0) {
658 #ifdef SEDEBUG
659 			if (sc->sc_debug)
660 				printf("se_read: se_get returned null\n");
661 #endif
662 			ifp->if_ierrors++;
663 			goto next_packet;
664 		}
665 		if ((ifp->if_flags & IFF_PROMISC) != 0) {
666 			m_adj(m, SE_PREFIX);
667 		}
668 
669 		/* Pass the packet up. */
670 		if_input(ifp, m);
671 
672 	next_packet:
673 		data += len;
674 		datalen -= len;
675 		n++;
676 	}
677 	return (n);
678 }
679 
680 
681 static void
682 sewatchdog(struct ifnet *ifp)
683 {
684 	struct se_softc *sc = ifp->if_softc;
685 
686 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
687 	++ifp->if_oerrors;
688 
689 	se_reset(sc);
690 }
691 
692 static int
693 se_reset(struct se_softc *sc)
694 {
695 	int error;
696 	int s = splnet();
697 #if 0
698 	/* Maybe we don't *really* want to reset the entire bus
699 	 * because the ctron isn't working. We would like to send a
700 	 * "BUS DEVICE RESET" message, but don't think the ctron
701 	 * understands it.
702 	 */
703 	error = se_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, SERETRIES, 2000, NULL,
704 	    XS_CTL_RESET);
705 #endif
706 	error = se_init(sc);
707 	splx(s);
708 	return (error);
709 }
710 
711 static int
712 se_add_proto(struct se_softc *sc, int proto)
713 {
714 	int error;
715 	struct scsi_ctron_ether_generic add_proto_cmd;
716 	uint8_t data[2];
717 	_lto2b(proto, data);
718 #ifdef SEDEBUG
719 	if (sc->sc_debug)
720 		printf("se: adding proto 0x%02x%02x\n", data[0], data[1]);
721 #endif
722 
723 	PROTOCMD(ctron_ether_add_proto, add_proto_cmd);
724 	_lto2b(sizeof(data), add_proto_cmd.length);
725 	error = se_scsipi_cmd(sc->sc_periph,
726 	    (void *)&add_proto_cmd, sizeof(add_proto_cmd),
727 	    data, sizeof(data), SERETRIES, SETIMEOUT, NULL,
728 	    XS_CTL_DATA_OUT);
729 	return (error);
730 }
731 
732 static int
733 se_get_addr(struct se_softc *sc, uint8_t *myaddr)
734 {
735 	int error;
736 	struct scsi_ctron_ether_generic get_addr_cmd;
737 
738 	PROTOCMD(ctron_ether_get_addr, get_addr_cmd);
739 	_lto2b(ETHER_ADDR_LEN, get_addr_cmd.length);
740 	error = se_scsipi_cmd(sc->sc_periph,
741 	    (void *)&get_addr_cmd, sizeof(get_addr_cmd),
742 	    myaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
743 	    XS_CTL_DATA_IN);
744 	printf("%s: ethernet address %s\n", device_xname(sc->sc_dev),
745 	    ether_sprintf(myaddr));
746 	return (error);
747 }
748 
749 
750 static int
751 se_set_media(struct se_softc *sc, int type)
752 {
753 	int error;
754 	struct scsi_ctron_ether_generic set_media_cmd;
755 
756 	PROTOCMD(ctron_ether_set_media, set_media_cmd);
757 	set_media_cmd.byte3 = type;
758 	error = se_scsipi_cmd(sc->sc_periph,
759 	    (void *)&set_media_cmd, sizeof(set_media_cmd),
760 	    0, 0, SERETRIES, SETIMEOUT, NULL, 0);
761 	return (error);
762 }
763 
764 static int
765 se_set_mode(struct se_softc *sc, int len, int mode)
766 {
767 	int error;
768 	struct scsi_ctron_ether_set_mode set_mode_cmd;
769 
770 	PROTOCMD(ctron_ether_set_mode, set_mode_cmd);
771 	set_mode_cmd.mode = mode;
772 	_lto2b(len, set_mode_cmd.length);
773 	error = se_scsipi_cmd(sc->sc_periph,
774 	    (void *)&set_mode_cmd, sizeof(set_mode_cmd),
775 	    0, 0, SERETRIES, SETIMEOUT, NULL, 0);
776 	return (error);
777 }
778 
779 
780 static int
781 se_init(struct se_softc *sc)
782 {
783 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
784 	struct scsi_ctron_ether_generic set_addr_cmd;
785 	uint8_t enaddr[ETHER_ADDR_LEN];
786 	int error;
787 
788 	if (ifp->if_flags & IFF_PROMISC) {
789 		error = se_set_mode(sc, MAX_SNAP, 1);
790 	}
791 	else
792 		error = se_set_mode(sc, ETHERMTU + sizeof(struct ether_header),
793 		    0);
794 	if (error != 0)
795 		return (error);
796 
797 	PROTOCMD(ctron_ether_set_addr, set_addr_cmd);
798 	_lto2b(ETHER_ADDR_LEN, set_addr_cmd.length);
799 	memcpy(enaddr, CLLADDR(ifp->if_sadl), sizeof(enaddr));
800 	error = se_scsipi_cmd(sc->sc_periph,
801 	    (void *)&set_addr_cmd, sizeof(set_addr_cmd),
802 	    enaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
803 	    XS_CTL_DATA_OUT);
804 	if (error != 0)
805 		return (error);
806 
807 	if ((sc->protos & PROTO_IP) &&
808 	    (error = se_add_proto(sc, ETHERTYPE_IP)) != 0)
809 		return (error);
810 	if ((sc->protos & PROTO_ARP) &&
811 	    (error = se_add_proto(sc, ETHERTYPE_ARP)) != 0)
812 		return (error);
813 	if ((sc->protos & PROTO_REVARP) &&
814 	    (error = se_add_proto(sc, ETHERTYPE_REVARP)) != 0)
815 		return (error);
816 #ifdef NETATALK
817 	if ((sc->protos & PROTO_AT) &&
818 	    (error = se_add_proto(sc, ETHERTYPE_ATALK)) != 0)
819 		return (error);
820 	if ((sc->protos & PROTO_AARP) &&
821 	    (error = se_add_proto(sc, ETHERTYPE_AARP)) != 0)
822 		return (error);
823 #endif
824 
825 	if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) == IFF_UP) {
826 		ifp->if_flags |= IFF_RUNNING;
827 		se_recv(sc);
828 		ifp->if_flags &= ~IFF_OACTIVE;
829 		se_ifstart(ifp);
830 	}
831 	return (error);
832 }
833 
834 static int
835 se_set_multi(struct se_softc *sc, uint8_t *addr)
836 {
837 	struct scsi_ctron_ether_generic set_multi_cmd;
838 	int error;
839 
840 	if (sc->sc_debug)
841 		printf("%s: set_set_multi: %s\n", device_xname(sc->sc_dev),
842 		    ether_sprintf(addr));
843 
844 	PROTOCMD(ctron_ether_set_multi, set_multi_cmd);
845 	_lto2b(sizeof(addr), set_multi_cmd.length);
846 	/* XXX sizeof(addr) is the size of the pointer.  Surely it
847 	 * is too small? --dyoung
848 	 */
849 	error = se_scsipi_cmd(sc->sc_periph,
850 	    (void *)&set_multi_cmd, sizeof(set_multi_cmd),
851 	    addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
852 	return (error);
853 }
854 
855 static int
856 se_remove_multi(struct se_softc *sc, uint8_t *addr)
857 {
858 	struct scsi_ctron_ether_generic remove_multi_cmd;
859 	int error;
860 
861 	if (sc->sc_debug)
862 		printf("%s: se_remove_multi: %s\n", device_xname(sc->sc_dev),
863 		    ether_sprintf(addr));
864 
865 	PROTOCMD(ctron_ether_remove_multi, remove_multi_cmd);
866 	_lto2b(sizeof(addr), remove_multi_cmd.length);
867 	/* XXX sizeof(addr) is the size of the pointer.  Surely it
868 	 * is too small? --dyoung
869 	 */
870 	error = se_scsipi_cmd(sc->sc_periph,
871 	    (void *)&remove_multi_cmd, sizeof(remove_multi_cmd),
872 	    addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
873 	return (error);
874 }
875 
876 #if 0	/* not used  --thorpej */
877 static int
878 sc_set_all_multi(struct se_softc *sc, int set)
879 {
880 	int error = 0;
881 	uint8_t *addr;
882 	struct ethercom *ec = &sc->sc_ethercom;
883 	struct ether_multi *enm;
884 	struct ether_multistep step;
885 
886 	ETHER_LOCK(ec);
887 	ETHER_FIRST_MULTI(step, ec, enm);
888 	while (enm != NULL) {
889 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
890 			/*
891 			 * We must listen to a range of multicast addresses.
892 			 * For now, just accept all multicasts, rather than
893 			 * trying to set only those filter bits needed to match
894 			 * the range.  (At this time, the only use of address
895 			 * ranges is for IP multicast routing, for which the
896 			 * range is big enough to require all bits set.)
897 			 */
898 			/* We have no way of adding a range to this device.
899 			 * stepping through all addresses in the range is
900 			 * typically not possible. The only real alternative
901 			 * is to go into promicuous mode and filter by hand.
902 			 */
903 			ETHER_UNLOCK(ec);
904 			return (ENODEV);
905 
906 		}
907 
908 		addr = enm->enm_addrlo;
909 		if ((error = set ? se_set_multi(sc, addr) :
910 		    se_remove_multi(sc, addr)) != 0)
911 			return (error);
912 		ETHER_NEXT_MULTI(step, enm);
913 	}
914 	ETHER_UNLOCK(ec);
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_WAITOK);
1021 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1022 			if (ifp->if_flags & IFF_RUNNING) {
1023 				error = (cmd == SIOCADDMULTI) ?
1024 				   se_set_multi(sc, sa->sa_data) :
1025 				   se_remove_multi(sc, sa->sa_data);
1026 			} else
1027 				error = 0;
1028 		}
1029 		sockaddr_free(sa);
1030 		break;
1031 
1032 	default:
1033 
1034 		error = ether_ioctl(ifp, cmd, data);
1035 		break;
1036 	}
1037 
1038 	splx(s);
1039 	return (error);
1040 }
1041 
1042 /*
1043  * Enable the network interface.
1044  */
1045 int
1046 se_enable(struct se_softc *sc)
1047 {
1048 	struct scsipi_periph *periph = sc->sc_periph;
1049 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
1050 	int error = 0;
1051 
1052 	if (sc->sc_enabled == 0 &&
1053 	    (error = scsipi_adapter_addref(adapt)) == 0)
1054 		sc->sc_enabled = 1;
1055 	else
1056 		aprint_error_dev(sc->sc_dev, "device enable failed\n");
1057 
1058 	return (error);
1059 }
1060 
1061 /*
1062  * Disable the network interface.
1063  */
1064 void
1065 se_disable(struct se_softc *sc)
1066 {
1067 	struct scsipi_periph *periph = sc->sc_periph;
1068 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
1069 
1070 	if (sc->sc_enabled != 0) {
1071 		scsipi_adapter_delref(adapt);
1072 		sc->sc_enabled = 0;
1073 	}
1074 }
1075 
1076 #define	SEUNIT(z)	(minor(z))
1077 /*
1078  * open the device.
1079  */
1080 int
1081 seopen(dev_t dev, int flag, int fmt, struct lwp *l)
1082 {
1083 	int unit, error;
1084 	struct se_softc *sc;
1085 	struct scsipi_periph *periph;
1086 	struct scsipi_adapter *adapt;
1087 
1088 	unit = SEUNIT(dev);
1089 	sc = device_lookup_private(&se_cd, unit);
1090 	if (sc == NULL)
1091 		return (ENXIO);
1092 
1093 	periph = sc->sc_periph;
1094 	adapt = periph->periph_channel->chan_adapter;
1095 
1096 	if ((error = scsipi_adapter_addref(adapt)) != 0)
1097 		return (error);
1098 
1099 	SC_DEBUG(periph, SCSIPI_DB1,
1100 	    ("scopen: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit,
1101 	    se_cd.cd_ndevs));
1102 
1103 	periph->periph_flags |= PERIPH_OPEN;
1104 
1105 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
1106 	return (0);
1107 }
1108 
1109 /*
1110  * close the device.. only called if we are the LAST
1111  * occurence of an open device
1112  */
1113 int
1114 seclose(dev_t dev, int flag, int fmt, struct lwp *l)
1115 {
1116 	struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
1117 	struct scsipi_periph *periph = sc->sc_periph;
1118 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
1119 
1120 	SC_DEBUG(sc->sc_periph, SCSIPI_DB1, ("closing\n"));
1121 
1122 	scsipi_wait_drain(periph);
1123 
1124 	scsipi_adapter_delref(adapt);
1125 	periph->periph_flags &= ~PERIPH_OPEN;
1126 
1127 	return (0);
1128 }
1129 
1130 /*
1131  * Perform special action on behalf of the user
1132  * Only does generic scsi ioctls.
1133  */
1134 int
1135 seioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1136 {
1137 	struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
1138 
1139 	return (scsipi_do_ioctl(sc->sc_periph, dev, cmd, addr, flag, l));
1140 }
1141