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