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