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