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