if_se.c revision 1.7 1 /* $NetBSD: if_se.c,v 1.7 1997/04/24 08:06:15 mycroft 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 <scsi/scsi_all.h>
78 #include <scsi/scsi_ctron_ether.h>
79 #include <scsi/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 scsi_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 scsi_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_scsi_cmd __P((struct scsi_link *sc_link,
211 struct scsi_generic *scsi_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 scsi_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 scsi_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 scsibus_attach_args *sa = aux;
271 int priority;
272
273 (void)scsi_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 scsibus_attach_args *sa = aux;
290 struct scsi_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_scsi_cmd(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
346 retries, timeout, bp, flags)
347 struct scsi_link *sc_link;
348 struct scsi_generic *scsi_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 = scsi_scsi_cmd(sc_link, scsi_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_scsi_cmd(sc->sc_link,
449 (struct scsi_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 scsi_xfer *xs;
472 {
473 int error;
474 struct se_softc *sc = xs->sc_link->device_softc;
475 struct scsi_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 scsi_done will call
489 * sestart (through scsi_free_xs).
490 */
491 }
492 } else if(IS_RECV(cmd)) {
493 /* RECV complete */
494 /* pass data up. reschedule a recv */
495 /* scsi_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_scsi_cmd(sc->sc_link,
538 (struct scsi_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_freem(top);
585 return 0;
586 }
587 len = MCLBYTES;
588 }
589 m->m_len = len = min(totlen, len);
590 bcopy(data, mtod(m, caddr_t), len);
591 data += len;
592 totlen -= len;
593 *mp = m;
594 mp = &m->m_next;
595 }
596
597 return (top);
598 }
599
600 /*
601 * Pass packets to higher levels.
602 */
603 static int
604 se_read(sc, data, datalen)
605 register struct se_softc *sc;
606 char *data;
607 int datalen;
608 {
609 struct mbuf *m;
610 struct ether_header *eh;
611 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
612 int n;
613
614 n = 0;
615 while (datalen >= 2) {
616 int len = _2btol(data);
617 data += 2;
618 datalen -= 2;
619
620 if (len == 0)
621 break;
622 #ifdef SEDEBUG
623 if (sc->sc_debug) {
624 printf("se_read: datalen = %d, packetlen = %d, proto = 0x%04x\n", datalen, len,
625 ntohs(((struct ether_header *)data)->ether_type));
626 }
627 #endif
628 if (len <= sizeof(struct ether_header) ||
629 len > MAX_SNAP) {
630 #ifdef SEDEBUG
631 printf("%s: invalid packet size %d; dropping\n",
632 sc->sc_dev.dv_xname, len);
633 #endif
634 ifp->if_ierrors++;
635 goto next_packet;
636 }
637
638 /* Don't need crc. Must keep ether header for BPF */
639 m = se_get(sc, data, len - ETHER_CRC);
640 if (m == 0) {
641 #ifdef SEDEBUG
642 if (sc->sc_debug) {
643 printf("se_read: se_get returned null\n");
644 }
645 #endif
646 ifp->if_ierrors++;
647 goto next_packet;
648 }
649 if ((ifp->if_flags & IFF_PROMISC) != 0) {
650 m_adj(m, SE_PREFIX);
651 }
652 ifp->if_ipackets++;
653
654 /* We assume that the header fit entirely in one mbuf. */
655 eh = mtod(m, struct ether_header *);
656
657 #if NBPFILTER > 0
658 /*
659 * Check if there's a BPF listener on this interface.
660 * If so, hand off the raw packet to BPF.
661 */
662 if (ifp->if_bpf) {
663 bpf_mtap(ifp->if_bpf, m);
664
665 /* Note that the interface cannot be in
666 * promiscuous mode if there are no BPF
667 * listeners. And if we are in promiscuous
668 * mode, we have to check if this packet is
669 * really ours.
670 */
671 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
672 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
673 ETHER_CMP(eh->ether_dhost, LLADDR(ifp->if_sadl))) {
674 m_freem(m);
675 goto next_packet;
676 }
677 }
678 #endif
679
680 /* Pass the packet up, with the ether header sort-of removed. */
681 m_adj(m, sizeof(struct ether_header));
682 ether_input(ifp, eh, m);
683
684 next_packet:
685 data += len;
686 datalen -= len;
687 n++;
688 }
689 return n;
690 }
691
692
693 static void
694 sewatchdog(ifp)
695 struct ifnet *ifp;
696 {
697 struct se_softc *sc = ifp->if_softc;
698
699 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
700 ++ifp->if_oerrors;
701
702 se_reset(sc);
703 }
704
705 static int
706 se_reset(sc)
707 struct se_softc *sc;
708 {
709 int error = 0;
710 int s = splnet();
711 #if 0
712 /* Maybe we don't *really* want to reset the entire bus
713 * because the ctron isn't working. We would like to send a
714 * "BUS DEVICE RESET" message, but don't think the ctron
715 * understands it.
716 */
717 error = se_scsi_cmd(sc->sc_link, 0, 0, 0, 0, SERETRIES, 2000, NULL,
718 SCSI_RESET);
719 #endif
720 error = se_init(sc);
721 splx(s);
722 return error;
723 }
724
725 static int
726 se_add_proto(sc, proto)
727 struct se_softc *sc;
728 int proto;
729 {
730 int error = 0;
731 struct scsi_ctron_ether_generic add_proto_cmd;
732 u_int8_t data[2];
733 _lto2b(proto, data);
734 #ifdef SEDEBUG
735 if (sc->sc_debug)
736 printf("se: adding proto 0x%02x%02x\n", data[0], data[1]);
737 #endif
738
739 PROTOCMD(ctron_ether_add_proto, add_proto_cmd);
740 _lto2b(sizeof(data), add_proto_cmd.length);
741 error = se_scsi_cmd(sc->sc_link,
742 (struct scsi_generic *) &add_proto_cmd,
743 sizeof(add_proto_cmd),
744 data,
745 sizeof(data),
746 SERETRIES, SETIMEOUT, NULL,
747 SCSI_DATA_OUT);
748 return error;
749 }
750
751 static int
752 se_get_addr(sc, myaddr)
753 struct se_softc *sc;
754 u_int8_t *myaddr;
755 {
756 int error = 0;
757 struct scsi_ctron_ether_generic get_addr_cmd;
758
759 PROTOCMD(ctron_ether_get_addr, get_addr_cmd);
760 _lto2b(ETHER_ADDR_LEN, get_addr_cmd.length);
761 error = se_scsi_cmd(sc->sc_link,
762 (struct scsi_generic *) &get_addr_cmd,
763 sizeof(get_addr_cmd),
764 myaddr, ETHER_ADDR_LEN,
765 SERETRIES, SETIMEOUT, NULL,
766 SCSI_DATA_IN);
767 printf("%s: ethernet address %s\n", sc->sc_dev.dv_xname,
768 ether_sprintf(myaddr));
769 return error;
770 }
771
772
773 static int
774 se_set_media(sc, type)
775 struct se_softc *sc;
776 int type;
777 {
778 int error = 0;
779 struct scsi_ctron_ether_generic set_media_cmd;
780
781 PROTOCMD(ctron_ether_set_media, set_media_cmd);
782 set_media_cmd.byte3 = type;
783 error = se_scsi_cmd(sc->sc_link,
784 (struct scsi_generic *) &set_media_cmd,
785 sizeof(set_media_cmd),
786 0,
787 0,
788 SERETRIES, SETIMEOUT, NULL,
789 0);
790 return error;
791 }
792
793 static int
794 se_set_mode(sc, len, mode)
795 struct se_softc *sc;
796 int len;
797 int mode;
798 {
799 int error = 0;
800 struct scsi_ctron_ether_set_mode set_mode_cmd;
801
802 PROTOCMD(ctron_ether_set_mode, set_mode_cmd);
803 set_mode_cmd.mode = mode;
804 _lto2b(len, set_mode_cmd.length);
805 error = se_scsi_cmd(sc->sc_link,
806 (struct scsi_generic *) &set_mode_cmd,
807 sizeof(set_mode_cmd),
808 0,
809 0,
810 SERETRIES, SETIMEOUT, NULL,
811 0);
812 return error;
813 }
814
815
816 static int
817 se_init(sc)
818 struct se_softc *sc;
819 {
820 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
821 struct scsi_ctron_ether_generic set_addr_cmd;
822 int error;
823
824 #if NBPFILTER > 0
825 if (ifp->if_flags & IFF_PROMISC) {
826 error = se_set_mode(sc, MAX_SNAP, 1);
827 }
828 else
829 #endif
830 error = se_set_mode(sc, ETHERMTU + sizeof(struct ether_header),
831 0);
832 if (error != 0)
833 return error;
834
835 PROTOCMD(ctron_ether_set_addr, set_addr_cmd);
836 _lto2b(ETHER_ADDR_LEN, set_addr_cmd.length);
837 error = se_scsi_cmd(sc->sc_link,
838 (struct scsi_generic *) &set_addr_cmd,
839 sizeof(set_addr_cmd),
840 LLADDR(ifp->if_sadl), ETHER_ADDR_LEN,
841 SERETRIES, SETIMEOUT, NULL,
842 SCSI_DATA_OUT);
843 if (error != 0) {
844 return error;
845 }
846
847 if ((sc->protos & PROTO_IP) &&
848 (error = se_add_proto(sc, ETHERTYPE_IP)) != 0)
849 return error;
850 if ((sc->protos & PROTO_ARP) &&
851 (error = se_add_proto(sc, ETHERTYPE_ARP)) != 0)
852 return error;
853 if ((sc->protos & PROTO_REVARP) &&
854 (error = se_add_proto(sc, ETHERTYPE_REVARP)) != 0)
855 return error;
856 #ifdef NETATALK
857 if ((sc->protos & PROTO_AT) &&
858 (error = se_add_proto(sc, ETHERTYPE_AT)) != 0)
859 return error;
860 if ((sc->protos & PROTO_AARP) &&
861 (error = se_add_proto(sc, ETHERTYPE_AARP)) != 0)
862 return error;
863 #endif
864
865 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == IFF_UP) {
866 ifp->if_flags |= IFF_RUNNING;
867 se_recv(sc);
868 ifp->if_flags &= ~IFF_OACTIVE;
869 se_ifstart(ifp);
870 }
871 return error;
872 }
873
874 static int
875 se_set_multi(sc, addr)
876 struct se_softc *sc;
877 u_int8_t *addr;
878 {
879 struct scsi_ctron_ether_generic set_multi_cmd;
880 int error;
881
882 if (sc->sc_debug)
883 printf("%s: set_set_multi: %s\n", sc->sc_dev.dv_xname,
884 ether_sprintf(addr));
885
886 PROTOCMD(ctron_ether_set_multi, set_multi_cmd);
887 _lto2b(sizeof(addr), set_multi_cmd.length);
888 error = se_scsi_cmd(sc->sc_link,
889 (struct scsi_generic *) &set_multi_cmd,
890 sizeof(set_multi_cmd),
891 addr,
892 sizeof(addr),
893 SERETRIES, SETIMEOUT, NULL,
894 SCSI_DATA_OUT);
895 return error;
896 }
897
898 static int
899 se_remove_multi(sc, addr)
900 struct se_softc *sc;
901 u_int8_t *addr;
902 {
903 struct scsi_ctron_ether_generic remove_multi_cmd;
904 int error;
905
906 if (sc->sc_debug)
907 printf("%s: se_remove_multi: %s\n", sc->sc_dev.dv_xname,
908 ether_sprintf(addr));
909
910 PROTOCMD(ctron_ether_remove_multi, remove_multi_cmd);
911 _lto2b(sizeof(addr), remove_multi_cmd.length);
912 error = se_scsi_cmd(sc->sc_link,
913 (struct scsi_generic *) &remove_multi_cmd,
914 sizeof(remove_multi_cmd),
915 addr,
916 sizeof(addr),
917 SERETRIES, SETIMEOUT, NULL,
918 SCSI_DATA_OUT);
919 return error;
920 }
921
922 #if 0 /* not used --thorpej */
923 static int
924 sc_set_all_multi(sc, set)
925 struct se_softc *sc;
926 int set;
927 {
928 int error = 0;
929 u_int8_t *addr;
930 struct ethercom *ac = &sc->sc_ethercom;
931 struct ether_multi *enm;
932 struct ether_multistep step;
933 ETHER_FIRST_MULTI(step, ac, enm);
934 while (enm != NULL) {
935 if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
936 /*
937 * We must listen to a range of multicast addresses.
938 * For now, just accept all multicasts, rather than
939 * trying to set only those filter bits needed to match
940 * the range. (At this time, the only use of address
941 * ranges is for IP multicast routing, for which the
942 * range is big enough to require all bits set.)
943 */
944 /* We have no way of adding a range to this device.
945 * stepping through all addresses in the range is
946 * typically not possible. The only real alternative
947 * is to go into promicuous mode and filter by hand.
948 */
949 return ENODEV;
950
951 }
952
953 addr = enm->enm_addrlo;
954 if ((error = set? se_set_multi(sc, addr): se_remove_multi(sc, addr)) != 0)
955 return error;
956 ETHER_NEXT_MULTI(step, enm);
957 }
958 return error;
959 }
960 #endif /* not used */
961
962 static void
963 se_stop(sc)
964 struct se_softc *sc;
965 {
966 /* Don't schedule any reads */
967 untimeout(se_recv, sc);
968
969 /* How can we abort any scsi cmds in progress? */
970 }
971
972
973 /*
974 * Process an ioctl request.
975 */
976 static int
977 se_ioctl(ifp, cmd, data)
978 register struct ifnet *ifp;
979 u_long cmd;
980 caddr_t data;
981 {
982 register struct se_softc *sc = ifp->if_softc;
983 struct ifaddr *ifa = (struct ifaddr *)data;
984 struct ifreq *ifr = (struct ifreq *)data;
985 int s, error = 0;
986
987 s = splnet();
988
989 switch (cmd) {
990
991 case SIOCSIFADDR:
992 ifp->if_flags |= IFF_UP;
993
994 if ((error = se_set_media(sc, CMEDIA_AUTOSENSE) != 0))
995 return error;
996
997 switch (ifa->ifa_addr->sa_family) {
998 #ifdef INET
999 case AF_INET:
1000 sc->protos |= (PROTO_IP | PROTO_ARP | PROTO_REVARP);
1001 if ((error = se_init(sc)) != 0)
1002 break;
1003 arp_ifinit(ifp, ifa);
1004 break;
1005 #endif
1006 #ifdef NS
1007 case AF_NS:
1008 {
1009 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1010
1011 if (ns_nullhost(*ina))
1012 ina->x_host =
1013 *(union ns_host *)LLADDR(ifp->if_sadl);
1014 else
1015 bcopy(ina->x_host.c_host,
1016 LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1017 /* Set new address. */
1018
1019 error = se_init(sc);
1020 break;
1021 }
1022 #endif
1023 #ifdef NETATALK
1024 case AF_APPLETALK:
1025 sc->protos |= (PROTO_AT | PROTO_AARP);
1026 if ((error = se_init(sc)) != 0)
1027 break;
1028 break;
1029 #endif
1030 default:
1031 error = se_init(sc);
1032 break;
1033 }
1034 break;
1035
1036 #if defined(CCITT) && defined(LLC)
1037 case SIOCSIFCONF_X25:
1038 ifp->if_flags |= IFF_UP;
1039 ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
1040 error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
1041 if (error == 0)
1042 error = se_init(sc);
1043 break;
1044 #endif /* CCITT && LLC */
1045
1046 case SIOCSIFFLAGS:
1047 if ((ifp->if_flags & IFF_UP) == 0 &&
1048 (ifp->if_flags & IFF_RUNNING) != 0) {
1049 /*
1050 * If interface is marked down and it is running, then
1051 * stop it.
1052 */
1053 se_stop(sc);
1054 ifp->if_flags &= ~IFF_RUNNING;
1055 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1056 (ifp->if_flags & IFF_RUNNING) == 0) {
1057 /*
1058 * If interface is marked up and it is stopped, then
1059 * start it.
1060 */
1061 error = se_init(sc);
1062 } else {
1063 /*
1064 * Reset the interface to pick up changes in any other
1065 * flags that affect hardware registers.
1066 */
1067 error = se_init(sc);
1068 }
1069 #ifdef SEDEBUG
1070 if (ifp->if_flags & IFF_DEBUG)
1071 sc->sc_debug = 1;
1072 else
1073 sc->sc_debug = 0;
1074 #endif
1075 break;
1076
1077 case SIOCADDMULTI:
1078 if (ether_addmulti(ifr, &sc->sc_ethercom) == ENETRESET) {
1079 error = se_set_multi(sc, ifr->ifr_addr.sa_data);
1080 } else {
1081 error = 0;
1082 }
1083 break;
1084 case SIOCDELMULTI:
1085 if (ether_delmulti(ifr, &sc->sc_ethercom) == ENETRESET) {
1086 error = se_remove_multi(sc, ifr->ifr_addr.sa_data);
1087 } else {
1088 error = 0;
1089 }
1090 break;
1091
1092 default:
1093
1094 error = EINVAL;
1095 break;
1096 }
1097
1098 splx(s);
1099 return (error);
1100 }
1101
1102 #define SEUNIT(z) (minor(z))
1103 /*
1104 * open the device.
1105 */
1106 int
1107 seopen(dev, flag, fmt, p)
1108 dev_t dev;
1109 int flag, fmt;
1110 struct proc *p;
1111 {
1112 int unit;
1113 struct se_softc *sc;
1114 struct scsi_link *sc_link;
1115
1116 unit = SEUNIT(dev);
1117 if (unit >= se_cd.cd_ndevs)
1118 return ENXIO;
1119 sc = se_cd.cd_devs[unit];
1120 if (!sc)
1121 return ENXIO;
1122
1123 sc_link = sc->sc_link;
1124
1125 SC_DEBUG(sc_link, SDEV_DB1,
1126 ("scopen: dev=0x%x (unit %d (of %d))\n", dev, unit, se_cd.cd_ndevs));
1127
1128 sc_link->flags |= SDEV_OPEN;
1129
1130 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
1131 return 0;
1132 }
1133
1134 /*
1135 * close the device.. only called if we are the LAST
1136 * occurence of an open device
1137 */
1138 int
1139 seclose(dev, flag, fmt, p)
1140 dev_t dev;
1141 int flag, fmt;
1142 struct proc *p;
1143 {
1144 struct se_softc *sc = se_cd.cd_devs[SEUNIT(dev)];
1145
1146 SC_DEBUG(sc->sc_link, SDEV_DB1, ("closing\n"));
1147 sc->sc_link->flags &= ~SDEV_OPEN;
1148
1149 return 0;
1150 }
1151
1152 /*
1153 * Perform special action on behalf of the user
1154 * Only does generic scsi ioctls.
1155 */
1156 int
1157 seioctl(dev, cmd, addr, flag, p)
1158 dev_t dev;
1159 u_long cmd;
1160 caddr_t addr;
1161 int flag;
1162 struct proc *p;
1163 {
1164 register struct se_softc *sc = se_cd.cd_devs[SEUNIT(dev)];
1165
1166 return scsi_do_ioctl(sc->sc_link, dev, cmd, addr, flag, p);
1167 }
1168