if_se.c revision 1.99 1 /* $NetBSD: if_se.c,v 1.99 2019/02/05 06:17:03 msaitoh Exp $ */
2
3 /*
4 * Copyright (c) 1997 Ian W. Dall <ian.dall (at) 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 (at) dsto.defence.gov.au> Feb 3, 1997
37 *
38 * Acknowledgement: Thanks are due to Philip L. Budne <budd (at) 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.99 2019/02/05 06:17:03 msaitoh 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 u_int16_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 *, 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 #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 u_int16_t
277 ether_cmp(void *one, void *two)
278 {
279 u_int16_t *a = (u_int16_t *) one;
280 u_int16_t *b = (u_int16_t *) two;
281 u_int16_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 u_int8_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 /*
327 * Store information needed to contact our base driver
328 */
329 sc->sc_periph = periph;
330 periph->periph_dev = sc->sc_dev;
331 periph->periph_switch = &se_switch;
332
333 /* XXX increase openings? */
334
335 se_poll = (SE_POLL * hz) / 1000;
336 se_poll = se_poll? se_poll: 1;
337 se_poll0 = (SE_POLL0 * hz) / 1000;
338 se_poll0 = se_poll0? se_poll0: 1;
339
340 /*
341 * Initialize and attach a buffer
342 */
343 sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header),
344 M_DEVBUF, M_NOWAIT);
345 if (sc->sc_tbuf == 0)
346 panic("seattach: can't allocate transmit buffer");
347
348 sc->sc_rbuf = malloc(RBUF_LEN, M_DEVBUF, M_NOWAIT);/* A Guess */
349 if (sc->sc_rbuf == 0)
350 panic("seattach: can't allocate receive buffer");
351
352 se_get_addr(sc, myaddr);
353
354 /* Initialize ifnet structure. */
355 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), sizeof(ifp->if_xname));
356 ifp->if_softc = sc;
357 ifp->if_start = se_ifstart;
358 ifp->if_ioctl = se_ioctl;
359 ifp->if_watchdog = sewatchdog;
360 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
361 IFQ_SET_READY(&ifp->if_snd);
362
363 /* Attach the interface. */
364 rv = if_initialize(ifp);
365 if (rv != 0) {
366 free(sc->sc_tbuf, M_DEVBUF);
367 callout_destroy(&sc->sc_ifstart_ch);
368 callout_destroy(&sc->sc_recv_ch);
369 return; /* Error */
370 }
371 ether_ifattach(ifp, myaddr);
372 if_register(ifp);
373 }
374
375
376 static inline int
377 se_scsipi_cmd(struct scsipi_periph *periph, struct scsipi_generic *cmd,
378 int cmdlen, u_char *data_addr, int datalen, int retries, int timeout,
379 struct buf *bp, int flags)
380 {
381 int error;
382
383 error = scsipi_command(periph, cmd, cmdlen, data_addr,
384 datalen, retries, timeout, bp, flags);
385 return (error);
386 }
387
388 /* Start routine for calling from scsi sub system */
389 static void
390 sestart(struct scsipi_periph *periph)
391 {
392 struct se_softc *sc = device_private(periph->periph_dev);
393 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
394 int s = splnet();
395
396 se_ifstart(ifp);
397 (void) splx(s);
398 }
399
400 static void
401 se_delayed_ifstart(void *v)
402 {
403 struct ifnet *ifp = v;
404 struct se_softc *sc = ifp->if_softc;
405 int s;
406
407 s = splnet();
408 if (sc->sc_enabled) {
409 ifp->if_flags &= ~IFF_OACTIVE;
410 se_ifstart(ifp);
411 }
412 splx(s);
413 }
414
415 /*
416 * Start transmission on the interface.
417 * Always called at splnet().
418 */
419 static void
420 se_ifstart(struct ifnet *ifp)
421 {
422 struct se_softc *sc = ifp->if_softc;
423 struct scsi_ctron_ether_generic send_cmd;
424 struct mbuf *m, *m0;
425 int len, error;
426 u_char *cp;
427
428 /* Don't transmit if interface is busy or not running */
429 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
430 return;
431
432 IFQ_DEQUEUE(&ifp->if_snd, m0);
433 if (m0 == 0)
434 return;
435 /* If BPF is listening on this interface, let it see the
436 * packet before we commit it to the wire.
437 */
438 bpf_mtap(ifp, m0, BPF_D_OUT);
439
440 /* We need to use m->m_pkthdr.len, so require the header */
441 if ((m0->m_flags & M_PKTHDR) == 0)
442 panic("ctscstart: no header mbuf");
443 len = m0->m_pkthdr.len;
444
445 /* Mark the interface busy. */
446 ifp->if_flags |= IFF_OACTIVE;
447
448 /* Chain; copy into linear buffer we allocated at attach time. */
449 cp = sc->sc_tbuf;
450 for (m = m0; m != NULL; ) {
451 memcpy(cp, mtod(m, u_char *), m->m_len);
452 cp += m->m_len;
453 m = m0 = m_free(m);
454 }
455 if (len < SEMINSIZE) {
456 #ifdef SEDEBUG
457 if (sc->sc_debug)
458 printf("se: packet size %d (%zu) < %d\n", len,
459 cp - (u_char *)sc->sc_tbuf, SEMINSIZE);
460 #endif
461 memset(cp, 0, SEMINSIZE - len);
462 len = SEMINSIZE;
463 }
464
465 /* Fill out SCSI command. */
466 PROTOCMD(ctron_ether_send, send_cmd);
467 _lto2b(len, send_cmd.length);
468
469 /* Send command to device. */
470 error = se_scsipi_cmd(sc->sc_periph,
471 (void *)&send_cmd, sizeof(send_cmd),
472 sc->sc_tbuf, len, SERETRIES,
473 SETIMEOUT, NULL, XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_DATA_OUT);
474 if (error) {
475 aprint_error_dev(sc->sc_dev, "not queued, error %d\n", error);
476 ifp->if_oerrors++;
477 ifp->if_flags &= ~IFF_OACTIVE;
478 } else
479 ifp->if_opackets++;
480 if (sc->sc_flags & SE_NEED_RECV) {
481 sc->sc_flags &= ~SE_NEED_RECV;
482 se_recv((void *) sc);
483 }
484 }
485
486
487 /*
488 * Called from the scsibus layer via our scsi device switch.
489 */
490 static void
491 sedone(struct scsipi_xfer *xs, int error)
492 {
493 struct se_softc *sc = device_private(xs->xs_periph->periph_dev);
494 struct scsipi_generic *cmd = xs->cmd;
495 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
496 int s;
497
498 s = splnet();
499 if(IS_SEND(cmd)) {
500 if (xs->error == XS_BUSY) {
501 printf("se: busy, retry txmit\n");
502 callout_reset(&sc->sc_ifstart_ch, hz,
503 se_delayed_ifstart, ifp);
504 } else {
505 ifp->if_flags &= ~IFF_OACTIVE;
506 /* the generic scsipi_done will call
507 * sestart (through scsipi_free_xs).
508 */
509 }
510 } else if(IS_RECV(cmd)) {
511 /* RECV complete */
512 /* pass data up. reschedule a recv */
513 /* scsipi_free_xs will call start. Harmless. */
514 if (error) {
515 /* Reschedule after a delay */
516 callout_reset(&sc->sc_recv_ch, se_poll,
517 se_recv, (void *)sc);
518 } else {
519 int n, ntimeo;
520 n = se_read(sc, xs->data, xs->datalen - xs->resid);
521 if (n > se_max_received)
522 se_max_received = n;
523 if (n == 0)
524 ntimeo = se_poll;
525 else if (n >= RDATA_MAX)
526 ntimeo = se_poll0;
527 else {
528 ntimeo = sc->sc_last_timeout;
529 ntimeo = (ntimeo * RDATA_GOAL)/n;
530 ntimeo = (ntimeo < se_poll0?
531 se_poll0: ntimeo);
532 ntimeo = (ntimeo > se_poll?
533 se_poll: ntimeo);
534 }
535 sc->sc_last_timeout = ntimeo;
536 if (ntimeo == se_poll0 &&
537 IFQ_IS_EMPTY(&ifp->if_snd) == 0)
538 /* Output is pending. Do next recv
539 * after the next send. */
540 sc->sc_flags |= SE_NEED_RECV;
541 else {
542 callout_reset(&sc->sc_recv_ch, ntimeo,
543 se_recv, (void *)sc);
544 }
545 }
546 }
547 splx(s);
548 }
549
550 static void
551 se_recv(void *v)
552 {
553 /* do a recv command */
554 struct se_softc *sc = (struct se_softc *) v;
555 struct scsi_ctron_ether_recv recv_cmd;
556 int error;
557
558 if (sc->sc_enabled == 0)
559 return;
560
561 PROTOCMD(ctron_ether_recv, recv_cmd);
562
563 error = se_scsipi_cmd(sc->sc_periph,
564 (void *)&recv_cmd, sizeof(recv_cmd),
565 sc->sc_rbuf, RBUF_LEN, SERETRIES, SETIMEOUT, NULL,
566 XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_DATA_IN);
567 if (error)
568 callout_reset(&sc->sc_recv_ch, se_poll, se_recv, (void *)sc);
569 }
570
571 /*
572 * We copy the data into mbufs. When full cluster sized units are present
573 * we copy into clusters.
574 */
575 static struct mbuf *
576 se_get(struct se_softc *sc, char *data, int totlen)
577 {
578 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
579 struct mbuf *m, *m0, *newm;
580 int len;
581
582 MGETHDR(m0, M_DONTWAIT, MT_DATA);
583 if (m0 == 0)
584 return (0);
585 m_set_rcvif(m0, ifp);
586 m0->m_pkthdr.len = totlen;
587 len = MHLEN;
588 m = m0;
589
590 while (totlen > 0) {
591 if (totlen >= MINCLSIZE) {
592 MCLGET(m, M_DONTWAIT);
593 if ((m->m_flags & M_EXT) == 0)
594 goto bad;
595 len = MCLBYTES;
596 }
597
598 if (m == m0) {
599 char *newdata = (char *)
600 ALIGN(m->m_data + sizeof(struct ether_header)) -
601 sizeof(struct ether_header);
602 len -= newdata - m->m_data;
603 m->m_data = newdata;
604 }
605
606 m->m_len = len = uimin(totlen, len);
607 memcpy(mtod(m, void *), data, len);
608 data += len;
609
610 totlen -= len;
611 if (totlen > 0) {
612 MGET(newm, M_DONTWAIT, MT_DATA);
613 if (newm == 0)
614 goto bad;
615 len = MLEN;
616 m = m->m_next = newm;
617 }
618 }
619
620 return (m0);
621
622 bad:
623 m_freem(m0);
624 return (0);
625 }
626
627 /*
628 * Pass packets to higher levels.
629 */
630 static int
631 se_read(struct se_softc *sc, char *data, int datalen)
632 {
633 struct mbuf *m;
634 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
635 int n;
636
637 n = 0;
638 while (datalen >= 2) {
639 int len = _2btol(data);
640 data += 2;
641 datalen -= 2;
642
643 if (len == 0)
644 break;
645 #ifdef SEDEBUG
646 if (sc->sc_debug) {
647 printf("se_read: datalen = %d, packetlen = %d, proto = 0x%04x\n", datalen, len,
648 ntohs(((struct ether_header *)data)->ether_type));
649 }
650 #endif
651 if (len <= sizeof(struct ether_header) ||
652 len > MAX_SNAP) {
653 #ifdef SEDEBUG
654 printf("%s: invalid packet size %d; dropping\n",
655 device_xname(sc->sc_dev), len);
656 #endif
657 ifp->if_ierrors++;
658 goto next_packet;
659 }
660
661 /* Don't need crc. Must keep ether header for BPF */
662 m = se_get(sc, data, len - ETHER_CRC);
663 if (m == 0) {
664 #ifdef SEDEBUG
665 if (sc->sc_debug)
666 printf("se_read: se_get returned null\n");
667 #endif
668 ifp->if_ierrors++;
669 goto next_packet;
670 }
671 if ((ifp->if_flags & IFF_PROMISC) != 0) {
672 m_adj(m, SE_PREFIX);
673 }
674
675 /* Pass the packet up. */
676 if_input(ifp, m);
677
678 next_packet:
679 data += len;
680 datalen -= len;
681 n++;
682 }
683 return (n);
684 }
685
686
687 static void
688 sewatchdog(struct ifnet *ifp)
689 {
690 struct se_softc *sc = ifp->if_softc;
691
692 log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
693 ++ifp->if_oerrors;
694
695 se_reset(sc);
696 }
697
698 static int
699 se_reset(struct se_softc *sc)
700 {
701 int error;
702 int s = splnet();
703 #if 0
704 /* Maybe we don't *really* want to reset the entire bus
705 * because the ctron isn't working. We would like to send a
706 * "BUS DEVICE RESET" message, but don't think the ctron
707 * understands it.
708 */
709 error = se_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, SERETRIES, 2000, NULL,
710 XS_CTL_RESET);
711 #endif
712 error = se_init(sc);
713 splx(s);
714 return (error);
715 }
716
717 static int
718 se_add_proto(struct se_softc *sc, int proto)
719 {
720 int error;
721 struct scsi_ctron_ether_generic add_proto_cmd;
722 u_int8_t data[2];
723 _lto2b(proto, data);
724 #ifdef SEDEBUG
725 if (sc->sc_debug)
726 printf("se: adding proto 0x%02x%02x\n", data[0], data[1]);
727 #endif
728
729 PROTOCMD(ctron_ether_add_proto, add_proto_cmd);
730 _lto2b(sizeof(data), add_proto_cmd.length);
731 error = se_scsipi_cmd(sc->sc_periph,
732 (void *)&add_proto_cmd, sizeof(add_proto_cmd),
733 data, sizeof(data), SERETRIES, SETIMEOUT, NULL,
734 XS_CTL_DATA_OUT);
735 return (error);
736 }
737
738 static int
739 se_get_addr(struct se_softc *sc, u_int8_t *myaddr)
740 {
741 int error;
742 struct scsi_ctron_ether_generic get_addr_cmd;
743
744 PROTOCMD(ctron_ether_get_addr, get_addr_cmd);
745 _lto2b(ETHER_ADDR_LEN, get_addr_cmd.length);
746 error = se_scsipi_cmd(sc->sc_periph,
747 (void *)&get_addr_cmd, sizeof(get_addr_cmd),
748 myaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
749 XS_CTL_DATA_IN);
750 printf("%s: ethernet address %s\n", device_xname(sc->sc_dev),
751 ether_sprintf(myaddr));
752 return (error);
753 }
754
755
756 static int
757 se_set_media(struct se_softc *sc, int type)
758 {
759 int error;
760 struct scsi_ctron_ether_generic set_media_cmd;
761
762 PROTOCMD(ctron_ether_set_media, set_media_cmd);
763 set_media_cmd.byte3 = type;
764 error = se_scsipi_cmd(sc->sc_periph,
765 (void *)&set_media_cmd, sizeof(set_media_cmd),
766 0, 0, SERETRIES, SETIMEOUT, NULL, 0);
767 return (error);
768 }
769
770 static int
771 se_set_mode(struct se_softc *sc, int len, int mode)
772 {
773 int error;
774 struct scsi_ctron_ether_set_mode set_mode_cmd;
775
776 PROTOCMD(ctron_ether_set_mode, set_mode_cmd);
777 set_mode_cmd.mode = mode;
778 _lto2b(len, set_mode_cmd.length);
779 error = se_scsipi_cmd(sc->sc_periph,
780 (void *)&set_mode_cmd, sizeof(set_mode_cmd),
781 0, 0, SERETRIES, SETIMEOUT, NULL, 0);
782 return (error);
783 }
784
785
786 static int
787 se_init(struct se_softc *sc)
788 {
789 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
790 struct scsi_ctron_ether_generic set_addr_cmd;
791 uint8_t enaddr[ETHER_ADDR_LEN];
792 int error;
793
794 if (ifp->if_flags & IFF_PROMISC) {
795 error = se_set_mode(sc, MAX_SNAP, 1);
796 }
797 else
798 error = se_set_mode(sc, ETHERMTU + sizeof(struct ether_header),
799 0);
800 if (error != 0)
801 return (error);
802
803 PROTOCMD(ctron_ether_set_addr, set_addr_cmd);
804 _lto2b(ETHER_ADDR_LEN, set_addr_cmd.length);
805 memcpy(enaddr, CLLADDR(ifp->if_sadl), sizeof(enaddr));
806 error = se_scsipi_cmd(sc->sc_periph,
807 (void *)&set_addr_cmd, sizeof(set_addr_cmd),
808 enaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
809 XS_CTL_DATA_OUT);
810 if (error != 0)
811 return (error);
812
813 if ((sc->protos & PROTO_IP) &&
814 (error = se_add_proto(sc, ETHERTYPE_IP)) != 0)
815 return (error);
816 if ((sc->protos & PROTO_ARP) &&
817 (error = se_add_proto(sc, ETHERTYPE_ARP)) != 0)
818 return (error);
819 if ((sc->protos & PROTO_REVARP) &&
820 (error = se_add_proto(sc, ETHERTYPE_REVARP)) != 0)
821 return (error);
822 #ifdef NETATALK
823 if ((sc->protos & PROTO_AT) &&
824 (error = se_add_proto(sc, ETHERTYPE_ATALK)) != 0)
825 return (error);
826 if ((sc->protos & PROTO_AARP) &&
827 (error = se_add_proto(sc, ETHERTYPE_AARP)) != 0)
828 return (error);
829 #endif
830
831 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == IFF_UP) {
832 ifp->if_flags |= IFF_RUNNING;
833 se_recv(sc);
834 ifp->if_flags &= ~IFF_OACTIVE;
835 se_ifstart(ifp);
836 }
837 return (error);
838 }
839
840 static int
841 se_set_multi(struct se_softc *sc, u_int8_t *addr)
842 {
843 struct scsi_ctron_ether_generic set_multi_cmd;
844 int error;
845
846 if (sc->sc_debug)
847 printf("%s: set_set_multi: %s\n", device_xname(sc->sc_dev),
848 ether_sprintf(addr));
849
850 PROTOCMD(ctron_ether_set_multi, set_multi_cmd);
851 _lto2b(sizeof(addr), set_multi_cmd.length);
852 /* XXX sizeof(addr) is the size of the pointer. Surely it
853 * is too small? --dyoung
854 */
855 error = se_scsipi_cmd(sc->sc_periph,
856 (void *)&set_multi_cmd, sizeof(set_multi_cmd),
857 addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
858 return (error);
859 }
860
861 static int
862 se_remove_multi(struct se_softc *sc, u_int8_t *addr)
863 {
864 struct scsi_ctron_ether_generic remove_multi_cmd;
865 int error;
866
867 if (sc->sc_debug)
868 printf("%s: se_remove_multi: %s\n", device_xname(sc->sc_dev),
869 ether_sprintf(addr));
870
871 PROTOCMD(ctron_ether_remove_multi, remove_multi_cmd);
872 _lto2b(sizeof(addr), remove_multi_cmd.length);
873 /* XXX sizeof(addr) is the size of the pointer. Surely it
874 * is too small? --dyoung
875 */
876 error = se_scsipi_cmd(sc->sc_periph,
877 (void *)&remove_multi_cmd, sizeof(remove_multi_cmd),
878 addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
879 return (error);
880 }
881
882 #if 0 /* not used --thorpej */
883 static int
884 sc_set_all_multi(struct se_softc *sc, int set)
885 {
886 int error = 0;
887 u_int8_t *addr;
888 struct ethercom *ac = &sc->sc_ethercom;
889 struct ether_multi *enm;
890 struct ether_multistep step;
891
892 ETHER_FIRST_MULTI(step, ac, enm);
893 while (enm != NULL) {
894 if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
895 /*
896 * We must listen to a range of multicast addresses.
897 * For now, just accept all multicasts, rather than
898 * trying to set only those filter bits needed to match
899 * the range. (At this time, the only use of address
900 * ranges is for IP multicast routing, for which the
901 * range is big enough to require all bits set.)
902 */
903 /* We have no way of adding a range to this device.
904 * stepping through all addresses in the range is
905 * typically not possible. The only real alternative
906 * is to go into promicuous mode and filter by hand.
907 */
908 return (ENODEV);
909
910 }
911
912 addr = enm->enm_addrlo;
913 if ((error = set ? se_set_multi(sc, addr) :
914 se_remove_multi(sc, addr)) != 0)
915 return (error);
916 ETHER_NEXT_MULTI(step, enm);
917 }
918 return (error);
919 }
920 #endif /* not used */
921
922 static void
923 se_stop(struct se_softc *sc)
924 {
925
926 /* Don't schedule any reads */
927 callout_stop(&sc->sc_recv_ch);
928
929 /* How can we abort any scsi cmds in progress? */
930 }
931
932
933 /*
934 * Process an ioctl request.
935 */
936 static int
937 se_ioctl(struct ifnet *ifp, u_long cmd, void *data)
938 {
939 struct se_softc *sc = ifp->if_softc;
940 struct ifaddr *ifa = (struct ifaddr *)data;
941 struct ifreq *ifr = (struct ifreq *)data;
942 struct sockaddr *sa;
943 int s, error = 0;
944
945 s = splnet();
946
947 switch (cmd) {
948
949 case SIOCINITIFADDR:
950 if ((error = se_enable(sc)) != 0)
951 break;
952 ifp->if_flags |= IFF_UP;
953
954 if ((error = se_set_media(sc, CMEDIA_AUTOSENSE)) != 0)
955 break;
956
957 switch (ifa->ifa_addr->sa_family) {
958 #ifdef INET
959 case AF_INET:
960 sc->protos |= (PROTO_IP | PROTO_ARP | PROTO_REVARP);
961 if ((error = se_init(sc)) != 0)
962 break;
963 arp_ifinit(ifp, ifa);
964 break;
965 #endif
966 #ifdef NETATALK
967 case AF_APPLETALK:
968 sc->protos |= (PROTO_AT | PROTO_AARP);
969 if ((error = se_init(sc)) != 0)
970 break;
971 break;
972 #endif
973 default:
974 error = se_init(sc);
975 break;
976 }
977 break;
978
979
980 case SIOCSIFFLAGS:
981 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
982 break;
983 /* XXX re-use ether_ioctl() */
984 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
985 case IFF_RUNNING:
986 /*
987 * If interface is marked down and it is running, then
988 * stop it.
989 */
990 se_stop(sc);
991 ifp->if_flags &= ~IFF_RUNNING;
992 se_disable(sc);
993 break;
994 case IFF_UP:
995 /*
996 * If interface is marked up and it is stopped, then
997 * start it.
998 */
999 if ((error = se_enable(sc)) != 0)
1000 break;
1001 error = se_init(sc);
1002 break;
1003 default:
1004 /*
1005 * Reset the interface to pick up changes in any other
1006 * flags that affect hardware registers.
1007 */
1008 if (sc->sc_enabled)
1009 error = se_init(sc);
1010 break;
1011 }
1012 #ifdef SEDEBUG
1013 if (ifp->if_flags & IFF_DEBUG)
1014 sc->sc_debug = 1;
1015 else
1016 sc->sc_debug = 0;
1017 #endif
1018 break;
1019
1020 case SIOCADDMULTI:
1021 case SIOCDELMULTI:
1022 sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_NOWAIT);
1023 if (sa == NULL) {
1024 error = ENOBUFS;
1025 break;
1026 }
1027 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1028 if (ifp->if_flags & IFF_RUNNING) {
1029 error = (cmd == SIOCADDMULTI) ?
1030 se_set_multi(sc, sa->sa_data) :
1031 se_remove_multi(sc, sa->sa_data);
1032 } else
1033 error = 0;
1034 }
1035 sockaddr_free(sa);
1036 break;
1037
1038 default:
1039
1040 error = ether_ioctl(ifp, cmd, data);
1041 break;
1042 }
1043
1044 splx(s);
1045 return (error);
1046 }
1047
1048 /*
1049 * Enable the network interface.
1050 */
1051 int
1052 se_enable(struct se_softc *sc)
1053 {
1054 struct scsipi_periph *periph = sc->sc_periph;
1055 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
1056 int error = 0;
1057
1058 if (sc->sc_enabled == 0 &&
1059 (error = scsipi_adapter_addref(adapt)) == 0)
1060 sc->sc_enabled = 1;
1061 else
1062 aprint_error_dev(sc->sc_dev, "device enable failed\n");
1063
1064 return (error);
1065 }
1066
1067 /*
1068 * Disable the network interface.
1069 */
1070 void
1071 se_disable(struct se_softc *sc)
1072 {
1073 struct scsipi_periph *periph = sc->sc_periph;
1074 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
1075
1076 if (sc->sc_enabled != 0) {
1077 scsipi_adapter_delref(adapt);
1078 sc->sc_enabled = 0;
1079 }
1080 }
1081
1082 #define SEUNIT(z) (minor(z))
1083 /*
1084 * open the device.
1085 */
1086 int
1087 seopen(dev_t dev, int flag, int fmt, struct lwp *l)
1088 {
1089 int unit, error;
1090 struct se_softc *sc;
1091 struct scsipi_periph *periph;
1092 struct scsipi_adapter *adapt;
1093
1094 unit = SEUNIT(dev);
1095 sc = device_lookup_private(&se_cd, unit);
1096 if (sc == NULL)
1097 return (ENXIO);
1098
1099 periph = sc->sc_periph;
1100 adapt = periph->periph_channel->chan_adapter;
1101
1102 if ((error = scsipi_adapter_addref(adapt)) != 0)
1103 return (error);
1104
1105 SC_DEBUG(periph, SCSIPI_DB1,
1106 ("scopen: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit,
1107 se_cd.cd_ndevs));
1108
1109 periph->periph_flags |= PERIPH_OPEN;
1110
1111 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
1112 return (0);
1113 }
1114
1115 /*
1116 * close the device.. only called if we are the LAST
1117 * occurence of an open device
1118 */
1119 int
1120 seclose(dev_t dev, int flag, int fmt, struct lwp *l)
1121 {
1122 struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
1123 struct scsipi_periph *periph = sc->sc_periph;
1124 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
1125
1126 SC_DEBUG(sc->sc_periph, SCSIPI_DB1, ("closing\n"));
1127
1128 scsipi_wait_drain(periph);
1129
1130 scsipi_adapter_delref(adapt);
1131 periph->periph_flags &= ~PERIPH_OPEN;
1132
1133 return (0);
1134 }
1135
1136 /*
1137 * Perform special action on behalf of the user
1138 * Only does generic scsi ioctls.
1139 */
1140 int
1141 seioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1142 {
1143 struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
1144
1145 return (scsipi_do_ioctl(sc->sc_periph, dev, cmd, addr, flag, l));
1146 }
1147