if_se.c revision 1.14 1 /* $NetBSD: if_se.c,v 1.14 1998/07/05 00:51:25 jonathan Exp $ */
2
3 /*
4 * Copyright (c) 1997 Ian W. Dall <ian.dall (at) dsto.defence.gov.au>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Ian W. Dall.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Driver for Cabletron EA41x scsi ethernet adaptor.
35 *
36 * Written by Ian Dall <ian.dall (at) dsto.defence.gov.au> Feb 3, 1997
37 *
38 * Acknowledgement: Thanks are due to Philip L. Budne <budd (at) cs.bu.edu>
39 * who reverse engineered the the EA41x. In developing this code,
40 * Phil's userland daemon "etherd", was refered to extensively in lieu
41 * of accurate documentation for the device.
42 *
43 * This is a weird device! It doesn't conform to the scsi spec in much
44 * at all. About the only standard command supported in inquiry. Most
45 * commands are 6 bytes long, but the recv data is only 1 byte. Data
46 * must be received by periodically polling the device with the recv
47 * command.
48 *
49 * This driver is also a bit unusual. It must look like a network
50 * interface and it must also appear to be a scsi device to the scsi
51 * system. Hence there are cases where there are two entry points. eg
52 * sestart is to be called from the scsi subsytem and se_ifstart from
53 * the network interface subsystem. In addition, to facilitate scsi
54 * commands issued by userland programs, there are open, close and
55 * ioctl entry points. This allows a user program to, for example,
56 * display the ea41x stats and download new code into the adaptor ---
57 * functions which can't be performed through the ifconfig interface.
58 * Normal operation does not require any special userland program.
59 */
60
61 #include "opt_inet.h"
62 #include "opt_atalk.h"
63 #include "bpfilter.h"
64
65 #include <sys/types.h>
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/syslog.h>
69 #include <sys/kernel.h>
70 #include <sys/file.h>
71 #include <sys/stat.h>
72 #include <sys/ioctl.h>
73 #include <sys/buf.h>
74 #include <sys/uio.h>
75 #include <sys/malloc.h>
76 #include <sys/errno.h>
77 #include <sys/device.h>
78 #include <sys/disklabel.h>
79 #include <sys/disk.h>
80 #include <sys/proc.h>
81 #include <sys/conf.h>
82
83 #include <dev/scsipi/scsipi_all.h>
84 #include <dev/scsipi/scsi_ctron_ether.h>
85 #include <dev/scsipi/scsiconf.h>
86
87 #include <sys/mbuf.h>
88
89 #include <sys/socket.h>
90 #include <net/if.h>
91 #include <net/if_dl.h>
92 #include <net/if_ether.h>
93 #include <net/if_media.h>
94
95 #ifdef INET
96 #include <netinet/in.h>
97 #include <netinet/if_inarp.h>
98 #endif
99
100 #ifdef NS
101 #include <netns/ns.h>
102 #include <netns/ns_if.h>
103 #endif
104
105 #ifdef NETATALK
106 #include <netatalk/at.h>
107 #endif
108
109 #if defined(CCITT) && defined(LLC)
110 #include <sys/socketvar.h>
111 #include <netccitt/x25.h>
112 #include <netccitt/pk.h>
113 #include <netccitt/pk_var.h>
114 #include <netccitt/pk_extern.h>
115 #endif
116
117 #if NBPFILTER > 0
118 #include <net/bpf.h>
119 #include <net/bpfdesc.h>
120 #endif
121
122 #define SETIMEOUT 1000
123 #define SEOUTSTANDING 4
124 #define SERETRIES 4
125 #define SE_PREFIX 4
126 #define ETHER_CRC 4
127 #define SEMINSIZE 60
128
129 /* Make this big enough for an ETHERMTU packet in promiscuous mode. */
130 #define MAX_SNAP (ETHERMTU + sizeof(struct ether_header) + \
131 SE_PREFIX + ETHER_CRC)
132
133 /* 10 full length packets appears to be the max ever returned. 16k is OK */
134 #define RBUF_LEN (16 * 1024)
135
136 /* Tuning parameters:
137 * The EA41x only returns a maximum of 10 packets (regardless of size).
138 * We will attempt to adapt to polling fast enough to get RDATA_GOAL packets
139 * per read
140 */
141 #define RDATA_MAX 10
142 #define RDATA_GOAL 8
143
144 /* se_poll and se_poll0 are the normal polling rate and the minimum
145 * polling rate respectively. se_poll0 should be chosen so that at
146 * maximum ethernet speed, we will read nearly RDATA_MAX packets. se_poll
147 * should be chosen for reasonable maximum latency.
148 * In practice, if we are being saturated with min length packets, we
149 * can't poll fast enough. Polling with zero delay actually
150 * worsens performance. se_poll0 is enforced to be always at least 1
151 */
152 #define SE_POLL 40 /* default in milliseconds */
153 #define SE_POLL0 10 /* default in milliseconds */
154 int se_poll = 0; /* Delay in ticks set at attach time */
155 int se_poll0 = 0;
156 int se_max_received = 0; /* Instrumentation */
157
158 #define PROTOCMD(p, d) \
159 ((d) = (p))
160
161 #define PROTOCMD_DECL(name, val) \
162 static const struct scsi_ctron_ether_generic name = val
163
164 #define PROTOCMD_DECL_SPECIAL(name, val) \
165 static const struct __CONCAT(scsi_,name) name = val
166
167 /* Command initializers for commands using scsi_ctron_ether_generic */
168 PROTOCMD_DECL(ctron_ether_send, {CTRON_ETHER_SEND});
169 PROTOCMD_DECL(ctron_ether_add_proto, {CTRON_ETHER_ADD_PROTO});
170 PROTOCMD_DECL(ctron_ether_get_addr, {CTRON_ETHER_GET_ADDR});
171 PROTOCMD_DECL(ctron_ether_set_media, {CTRON_ETHER_SET_MEDIA});
172 PROTOCMD_DECL(ctron_ether_set_addr, {CTRON_ETHER_SET_ADDR});
173 PROTOCMD_DECL(ctron_ether_set_multi, {CTRON_ETHER_SET_MULTI});
174 PROTOCMD_DECL(ctron_ether_remove_multi, {CTRON_ETHER_REMOVE_MULTI});
175
176 /* Command initializers for commands using their own structures */
177 PROTOCMD_DECL_SPECIAL(ctron_ether_recv, {CTRON_ETHER_RECV});
178 PROTOCMD_DECL_SPECIAL(ctron_ether_set_mode, {CTRON_ETHER_SET_MODE});
179
180 struct se_softc {
181 struct device sc_dev;
182 struct ethercom sc_ethercom; /* Ethernet common part */
183 struct scsipi_link *sc_link; /* contains our targ, lun, etc. */
184 char *sc_tbuf;
185 char *sc_rbuf;
186 int protos;
187 #define PROTO_IP 0x01
188 #define PROTO_ARP 0x02
189 #define PROTO_REVARP 0x04
190 #define PROTO_AT 0x08
191 #define PROTO_AARP 0x10
192 int sc_debug;
193 int sc_flags;
194 #define SE_NEED_RECV 0x1
195 int sc_last_timeout;
196 };
197
198 cdev_decl(se);
199
200 #ifdef __BROKEN_INDIRECT_CONFIG
201 static int sematch __P((struct device *, void *, void *));
202 #else
203 static int sematch __P((struct device *, struct cfdata *, void *));
204 #endif
205 static void seattach __P((struct device *, struct device *, void *));
206
207 static void se_ifstart __P((struct ifnet *));
208 static void sestart __P((void *));
209
210 static void sedone __P((struct scsipi_xfer *));
211 static int se_ioctl __P((struct ifnet *, u_long, caddr_t));
212 static void sewatchdog __P((struct ifnet *));
213
214 static __inline u_int16_t ether_cmp __P((void *, void *));
215 static void se_recv __P((void *));
216 static struct mbuf *se_get __P((struct se_softc *, char *, int));
217 static int se_read __P((struct se_softc *, char *, int));
218 static int se_reset __P((struct se_softc *));
219 static int se_add_proto __P((struct se_softc *, int));
220 static int se_get_addr __P((struct se_softc *, u_int8_t *));
221 static int se_set_media __P((struct se_softc *, int));
222 static int se_init __P((struct se_softc *));
223 static int se_set_multi __P((struct se_softc *, u_int8_t *));
224 static int se_remove_multi __P((struct se_softc *, u_int8_t *));
225 #if 0
226 static int sc_set_all_multi __P((struct se_softc *, int));
227 #endif
228 static void se_stop __P((struct se_softc *));
229 static __inline int se_scsipi_cmd __P((struct scsipi_link *sc_link,
230 struct scsipi_generic *scsipi_cmd,
231 int cmdlen, u_char *data_addr, int datalen,
232 int retries, int timeout, struct buf *bp,
233 int flags));
234 static void se_delayed_ifstart __P((void *));
235 static int se_set_mode(struct se_softc *, int, int);
236
237 struct cfattach se_ca = {
238 sizeof(struct se_softc), sematch, seattach
239 };
240
241 extern struct cfdriver se_cd;
242
243 struct scsipi_device se_switch = {
244 NULL, /* Use default error handler */
245 sestart, /* have a queue, served by this */
246 NULL, /* have no async handler */
247 sedone, /* deal with stats at interrupt time */
248 };
249
250 struct scsipi_inquiry_pattern se_patterns[] = {
251 {T_PROCESSOR, T_FIXED,
252 "CABLETRN", "EA412", ""},
253 {T_PROCESSOR, T_FIXED,
254 "Cabletrn", "EA412", ""},
255 };
256
257 /*
258 * Compare two Ether/802 addresses for equality, inlined and
259 * unrolled for speed.
260 * Note: use this like bcmp()
261 */
262 static __inline u_int16_t
263 ether_cmp(one, two)
264 void *one, *two;
265 {
266 register u_int16_t *a = (u_int16_t *) one;
267 register u_int16_t *b = (u_int16_t *) two;
268 register u_int16_t diff;
269
270 diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
271
272 return (diff);
273 }
274
275 #define ETHER_CMP ether_cmp
276
277 static int
278 sematch(parent, match, aux)
279 struct device *parent;
280 #ifdef __BROKEN_INDIRECT_CONFIG
281 void *match;
282 #else
283 struct cfdata *match;
284 #endif
285 void *aux;
286 {
287 struct scsipibus_attach_args *sa = aux;
288 int priority;
289
290 (void)scsipi_inqmatch(&sa->sa_inqbuf,
291 (caddr_t)se_patterns, sizeof(se_patterns) / sizeof(se_patterns[0]),
292 sizeof(se_patterns[0]), &priority);
293 return (priority);
294 }
295
296 /*
297 * The routine called by the low level scsi routine when it discovers
298 * a device suitable for this driver.
299 */
300 static void
301 seattach(parent, self, aux)
302 struct device *parent, *self;
303 void *aux;
304 {
305 struct se_softc *sc = (void *)self;
306 struct scsipibus_attach_args *sa = aux;
307 struct scsipi_link *sc_link = sa->sa_sc_link;
308 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
309 u_int8_t myaddr[ETHER_ADDR_LEN];
310
311 printf("\n");
312 SC_DEBUG(sc_link, SDEV_DB2, ("seattach: "));
313
314 /*
315 * Store information needed to contact our base driver
316 */
317 sc->sc_link = sc_link;
318 sc_link->device = &se_switch;
319 sc_link->device_softc = sc;
320 if (sc_link->openings > SEOUTSTANDING)
321 sc_link->openings = SEOUTSTANDING;
322
323 se_poll = (SE_POLL * hz) / 1000;
324 se_poll = se_poll? se_poll: 1;
325 se_poll0 = (SE_POLL0 * hz) / 1000;
326 se_poll0 = se_poll0? se_poll0: 1;
327
328 /*
329 * Initialize and attach a buffer
330 */
331 sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header),
332 M_DEVBUF, M_NOWAIT);
333 if (sc->sc_tbuf == 0)
334 panic("seattach: can't allocate transmit buffer");
335
336 sc->sc_rbuf = malloc(RBUF_LEN, M_DEVBUF, M_NOWAIT);/* A Guess */
337 if (sc->sc_rbuf == 0)
338 panic("seattach: can't allocate receive buffer");
339
340 se_get_addr(sc, myaddr);
341
342 /* Initialize ifnet structure. */
343 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
344 ifp->if_softc = sc;
345 ifp->if_start = se_ifstart;
346 ifp->if_ioctl = se_ioctl;
347 ifp->if_watchdog = sewatchdog;
348 ifp->if_flags =
349 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
350
351 /* Attach the interface. */
352 if_attach(ifp);
353 ether_ifattach(ifp, myaddr);
354
355 #if NBPFILTER > 0
356 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
357 #endif
358 }
359
360
361 static __inline int
362 se_scsipi_cmd(sc_link, scsipi_cmd, cmdlen, data_addr, datalen,
363 retries, timeout, bp, flags)
364 struct scsipi_link *sc_link;
365 struct scsipi_generic *scsipi_cmd;
366 int cmdlen;
367 u_char *data_addr;
368 int datalen;
369 int retries;
370 int timeout;
371 struct buf *bp;
372 int flags;
373 {
374 int error;
375 int s = splbio();
376
377 error = scsipi_command(sc_link, scsipi_cmd, cmdlen, data_addr,
378 datalen, retries, timeout, bp, flags);
379 splx(s);
380 return (error);
381 }
382
383 /* Start routine for calling from scsi sub system */
384 static void
385 sestart(v)
386 void *v;
387 {
388 struct se_softc *sc = (struct se_softc *) v;
389 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
390 int s = splnet();
391
392 se_ifstart(ifp);
393 (void) splx(s);
394 }
395
396 static void
397 se_delayed_ifstart(v)
398 void *v;
399 {
400 struct ifnet *ifp = v;
401 int s = splnet();
402
403 ifp->if_flags &= ~IFF_OACTIVE;
404 se_ifstart(ifp);
405 splx(s);
406 }
407
408 /*
409 * Start transmission on the interface.
410 * Always called at splnet().
411 */
412 static void
413 se_ifstart(ifp)
414 struct ifnet *ifp;
415 {
416 struct se_softc *sc = ifp->if_softc;
417 struct scsi_ctron_ether_generic send_cmd;
418 struct mbuf *m, *m0;
419 int len, error;
420 u_char *cp;
421
422 /* Don't transmit if interface is busy or not running */
423 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
424 return;
425
426 IF_DEQUEUE(&ifp->if_snd, m0);
427 if (m0 == 0)
428 return;
429 #if NBPFILTER > 0
430 /* If BPF is listening on this interface, let it see the
431 * packet before we commit it to the wire.
432 */
433 if (ifp->if_bpf)
434 bpf_mtap(ifp->if_bpf, m0);
435 #endif
436
437 /* We need to use m->m_pkthdr.len, so require the header */
438 if ((m0->m_flags & M_PKTHDR) == 0)
439 panic("ctscstart: no header mbuf");
440 len = m0->m_pkthdr.len;
441
442 /* Mark the interface busy. */
443 ifp->if_flags |= IFF_OACTIVE;
444
445 /* Chain; copy into linear buffer we allocated at attach time. */
446 cp = sc->sc_tbuf;
447 for (m = m0; m != NULL; ) {
448 bcopy(mtod(m, u_char *), cp, m->m_len);
449 cp += m->m_len;
450 MFREE(m, m0);
451 m = m0;
452 }
453 if (len < SEMINSIZE) {
454 #ifdef SEDEBUG
455 if (sc->sc_debug)
456 printf("se: packet size %d (%d) < %d\n", len,
457 cp - (u_char *)sc->sc_tbuf, SEMINSIZE);
458 #endif
459 bzero(cp, SEMINSIZE - len);
460 len = SEMINSIZE;
461 }
462
463 /* Fill out SCSI command. */
464 PROTOCMD(ctron_ether_send, send_cmd);
465 _lto2b(len, send_cmd.length);
466
467 /* Send command to device. */
468 error = se_scsipi_cmd(sc->sc_link,
469 (struct scsipi_generic *)&send_cmd, sizeof(send_cmd),
470 sc->sc_tbuf, len, SERETRIES,
471 SETIMEOUT, NULL, SCSI_NOSLEEP|SCSI_DATA_OUT);
472 if (error) {
473 printf("%s: not queued, error %d\n",
474 sc->sc_dev.dv_xname, error);
475 ifp->if_oerrors++;
476 ifp->if_flags &= ~IFF_OACTIVE;
477 } else
478 ifp->if_opackets++;
479 if (sc->sc_flags & SE_NEED_RECV) {
480 sc->sc_flags &= ~SE_NEED_RECV;
481 se_recv((void *) sc);
482 }
483 }
484
485
486 /*
487 * Called from the scsibus layer via our scsi device switch.
488 */
489 static void
490 sedone(xs)
491 struct scsipi_xfer *xs;
492 {
493 int error;
494 struct se_softc *sc = xs->sc_link->device_softc;
495 struct scsipi_generic *cmd = xs->cmd;
496 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
497 int s;
498
499 error = !(xs->error == XS_NOERROR);
500
501 s = splnet();
502 if(IS_SEND(cmd)) {
503 if (xs->error == XS_BUSY) {
504 printf("se: busy, retry txmit\n");
505 timeout(se_delayed_ifstart, ifp, hz);
506 } else {
507 ifp->if_flags &= ~IFF_OACTIVE;
508 /* the generic scsipi_done will call
509 * sestart (through scsipi_free_xs).
510 */
511 }
512 } else if(IS_RECV(cmd)) {
513 /* RECV complete */
514 /* pass data up. reschedule a recv */
515 /* scsipi_free_xs will call start. Harmless. */
516 if (error) {
517 /* Reschedule after a delay */
518 timeout(se_recv, (void *)sc, se_poll);
519 } else {
520 int n, ntimeo;
521 n = se_read(sc, xs->data, xs->datalen - xs->resid);
522 if (n > se_max_received)
523 se_max_received = n;
524 if (n == 0)
525 ntimeo = se_poll;
526 else if (n >= RDATA_MAX)
527 ntimeo = se_poll0;
528 else {
529 ntimeo = sc->sc_last_timeout;
530 ntimeo = (ntimeo * RDATA_GOAL)/n;
531 ntimeo = (ntimeo < se_poll0?
532 se_poll0: ntimeo);
533 ntimeo = (ntimeo > se_poll?
534 se_poll: ntimeo);
535 }
536 sc->sc_last_timeout = ntimeo;
537 if (ntimeo == se_poll0 &&
538 ifp->if_snd.ifq_head)
539 /* Output is pending. Do next recv
540 * after the next send. */
541 sc->sc_flags |= SE_NEED_RECV;
542 else {
543 timeout(se_recv, (void *)sc, ntimeo);
544 }
545 }
546 }
547 splx(s);
548 }
549
550 static void
551 se_recv(v)
552 void *v;
553 {
554 /* do a recv command */
555 struct se_softc *sc = (struct se_softc *) v;
556 struct scsi_ctron_ether_recv recv_cmd;
557 int error;
558
559 PROTOCMD(ctron_ether_recv, recv_cmd);
560
561 error = se_scsipi_cmd(sc->sc_link,
562 (struct scsipi_generic *)&recv_cmd, sizeof(recv_cmd),
563 sc->sc_rbuf, RBUF_LEN, SERETRIES, SETIMEOUT, NULL,
564 SCSI_NOSLEEP|SCSI_DATA_IN);
565 if (error)
566 timeout(se_recv, (void *)sc, se_poll);
567 }
568
569 /*
570 * We copy the data into mbufs. When full cluster sized units are present
571 * we copy into clusters.
572 */
573 static struct mbuf *
574 se_get(sc, data, totlen)
575 struct se_softc *sc;
576 char *data;
577 int totlen;
578 {
579 struct mbuf *m;
580 struct mbuf *top, **mp;
581 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
582 int len, pad;
583
584 MGETHDR(m, M_DONTWAIT, MT_DATA);
585 if (m == 0)
586 return (0);
587 m->m_pkthdr.rcvif = ifp;
588 m->m_pkthdr.len = totlen;
589 pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
590 m->m_data += pad;
591 len = MHLEN - pad;
592 top = 0;
593 mp = ⊤
594
595 while (totlen > 0) {
596 if (top) {
597 MGET(m, M_DONTWAIT, MT_DATA);
598 if (m == 0) {
599 m_freem(top);
600 return (0);
601 }
602 len = MLEN;
603 }
604 if (totlen >= MINCLSIZE) {
605 MCLGET(m, M_DONTWAIT);
606 if ((m->m_flags & M_EXT) == 0) {
607 m_free(m);
608 m_freem(top);
609 return (0);
610 }
611 len = MCLBYTES;
612 }
613 m->m_len = len = min(totlen, len);
614 bcopy(data, mtod(m, caddr_t), len);
615 data += len;
616 totlen -= len;
617 *mp = m;
618 mp = &m->m_next;
619 }
620
621 return (top);
622 }
623
624 /*
625 * Pass packets to higher levels.
626 */
627 static int
628 se_read(sc, data, datalen)
629 register struct se_softc *sc;
630 char *data;
631 int datalen;
632 {
633 struct mbuf *m;
634 struct ether_header *eh;
635 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
636 int n;
637
638 n = 0;
639 while (datalen >= 2) {
640 int len = _2btol(data);
641 data += 2;
642 datalen -= 2;
643
644 if (len == 0)
645 break;
646 #ifdef SEDEBUG
647 if (sc->sc_debug) {
648 printf("se_read: datalen = %d, packetlen = %d, proto = 0x%04x\n", datalen, len,
649 ntohs(((struct ether_header *)data)->ether_type));
650 }
651 #endif
652 if (len <= sizeof(struct ether_header) ||
653 len > MAX_SNAP) {
654 #ifdef SEDEBUG
655 printf("%s: invalid packet size %d; dropping\n",
656 sc->sc_dev.dv_xname, len);
657 #endif
658 ifp->if_ierrors++;
659 goto next_packet;
660 }
661
662 /* Don't need crc. Must keep ether header for BPF */
663 m = se_get(sc, data, len - ETHER_CRC);
664 if (m == 0) {
665 #ifdef SEDEBUG
666 if (sc->sc_debug)
667 printf("se_read: se_get returned null\n");
668 #endif
669 ifp->if_ierrors++;
670 goto next_packet;
671 }
672 if ((ifp->if_flags & IFF_PROMISC) != 0) {
673 m_adj(m, SE_PREFIX);
674 }
675 ifp->if_ipackets++;
676
677 /* We assume that the header fit entirely in one mbuf. */
678 eh = mtod(m, struct ether_header *);
679
680 #if NBPFILTER > 0
681 /*
682 * Check if there's a BPF listener on this interface.
683 * If so, hand off the raw packet to BPF.
684 */
685 if (ifp->if_bpf) {
686 bpf_mtap(ifp->if_bpf, m);
687
688 /* Note that the interface cannot be in
689 * promiscuous mode if there are no BPF
690 * listeners. And if we are in promiscuous
691 * mode, we have to check if this packet is
692 * really ours.
693 */
694 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
695 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
696 ETHER_CMP(eh->ether_dhost, LLADDR(ifp->if_sadl))) {
697 m_freem(m);
698 goto next_packet;
699 }
700 }
701 #endif
702
703 /* Pass the packet up, with the ether header sort-of removed. */
704 m_adj(m, sizeof(struct ether_header));
705 ether_input(ifp, eh, m);
706
707 next_packet:
708 data += len;
709 datalen -= len;
710 n++;
711 }
712 return (n);
713 }
714
715
716 static void
717 sewatchdog(ifp)
718 struct ifnet *ifp;
719 {
720 struct se_softc *sc = ifp->if_softc;
721
722 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
723 ++ifp->if_oerrors;
724
725 se_reset(sc);
726 }
727
728 static int
729 se_reset(sc)
730 struct se_softc *sc;
731 {
732 int error;
733 int s = splnet();
734 #if 0
735 /* Maybe we don't *really* want to reset the entire bus
736 * because the ctron isn't working. We would like to send a
737 * "BUS DEVICE RESET" message, but don't think the ctron
738 * understands it.
739 */
740 error = se_scsipi_cmd(sc->sc_link, 0, 0, 0, 0, SERETRIES, 2000, NULL,
741 SCSI_RESET);
742 #endif
743 error = se_init(sc);
744 splx(s);
745 return (error);
746 }
747
748 static int
749 se_add_proto(sc, proto)
750 struct se_softc *sc;
751 int proto;
752 {
753 int error;
754 struct scsi_ctron_ether_generic add_proto_cmd;
755 u_int8_t data[2];
756 _lto2b(proto, data);
757 #ifdef SEDEBUG
758 if (sc->sc_debug)
759 printf("se: adding proto 0x%02x%02x\n", data[0], data[1]);
760 #endif
761
762 PROTOCMD(ctron_ether_add_proto, add_proto_cmd);
763 _lto2b(sizeof(data), add_proto_cmd.length);
764 error = se_scsipi_cmd(sc->sc_link,
765 (struct scsipi_generic *) &add_proto_cmd, sizeof(add_proto_cmd),
766 data, sizeof(data), SERETRIES, SETIMEOUT, NULL, SCSI_DATA_OUT);
767 return (error);
768 }
769
770 static int
771 se_get_addr(sc, myaddr)
772 struct se_softc *sc;
773 u_int8_t *myaddr;
774 {
775 int error;
776 struct scsi_ctron_ether_generic get_addr_cmd;
777
778 PROTOCMD(ctron_ether_get_addr, get_addr_cmd);
779 _lto2b(ETHER_ADDR_LEN, get_addr_cmd.length);
780 error = se_scsipi_cmd(sc->sc_link,
781 (struct scsipi_generic *) &get_addr_cmd, sizeof(get_addr_cmd),
782 myaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL, SCSI_DATA_IN);
783 printf("%s: ethernet address %s\n", sc->sc_dev.dv_xname,
784 ether_sprintf(myaddr));
785 return (error);
786 }
787
788
789 static int
790 se_set_media(sc, type)
791 struct se_softc *sc;
792 int type;
793 {
794 int error;
795 struct scsi_ctron_ether_generic set_media_cmd;
796
797 PROTOCMD(ctron_ether_set_media, set_media_cmd);
798 set_media_cmd.byte3 = type;
799 error = se_scsipi_cmd(sc->sc_link,
800 (struct scsipi_generic *) &set_media_cmd, sizeof(set_media_cmd),
801 0, 0, SERETRIES, SETIMEOUT, NULL, 0);
802 return (error);
803 }
804
805 static int
806 se_set_mode(sc, len, mode)
807 struct se_softc *sc;
808 int len;
809 int mode;
810 {
811 int error;
812 struct scsi_ctron_ether_set_mode set_mode_cmd;
813
814 PROTOCMD(ctron_ether_set_mode, set_mode_cmd);
815 set_mode_cmd.mode = mode;
816 _lto2b(len, set_mode_cmd.length);
817 error = se_scsipi_cmd(sc->sc_link,
818 (struct scsipi_generic *) &set_mode_cmd, sizeof(set_mode_cmd),
819 0, 0, SERETRIES, SETIMEOUT, NULL, 0);
820 return (error);
821 }
822
823
824 static int
825 se_init(sc)
826 struct se_softc *sc;
827 {
828 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
829 struct scsi_ctron_ether_generic set_addr_cmd;
830 int error;
831
832 #if NBPFILTER > 0
833 if (ifp->if_flags & IFF_PROMISC) {
834 error = se_set_mode(sc, MAX_SNAP, 1);
835 }
836 else
837 #endif
838 error = se_set_mode(sc, ETHERMTU + sizeof(struct ether_header),
839 0);
840 if (error != 0)
841 return (error);
842
843 PROTOCMD(ctron_ether_set_addr, set_addr_cmd);
844 _lto2b(ETHER_ADDR_LEN, set_addr_cmd.length);
845 error = se_scsipi_cmd(sc->sc_link,
846 (struct scsipi_generic *) &set_addr_cmd, sizeof(set_addr_cmd),
847 LLADDR(ifp->if_sadl), ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
848 SCSI_DATA_OUT);
849 if (error != 0)
850 return (error);
851
852 if ((sc->protos & PROTO_IP) &&
853 (error = se_add_proto(sc, ETHERTYPE_IP)) != 0)
854 return (error);
855 if ((sc->protos & PROTO_ARP) &&
856 (error = se_add_proto(sc, ETHERTYPE_ARP)) != 0)
857 return (error);
858 if ((sc->protos & PROTO_REVARP) &&
859 (error = se_add_proto(sc, ETHERTYPE_REVARP)) != 0)
860 return (error);
861 #ifdef NETATALK
862 if ((sc->protos & PROTO_AT) &&
863 (error = se_add_proto(sc, ETHERTYPE_AT)) != 0)
864 return (error);
865 if ((sc->protos & PROTO_AARP) &&
866 (error = se_add_proto(sc, ETHERTYPE_AARP)) != 0)
867 return (error);
868 #endif
869
870 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == IFF_UP) {
871 ifp->if_flags |= IFF_RUNNING;
872 se_recv(sc);
873 ifp->if_flags &= ~IFF_OACTIVE;
874 se_ifstart(ifp);
875 }
876 return (error);
877 }
878
879 static int
880 se_set_multi(sc, addr)
881 struct se_softc *sc;
882 u_int8_t *addr;
883 {
884 struct scsi_ctron_ether_generic set_multi_cmd;
885 int error;
886
887 if (sc->sc_debug)
888 printf("%s: set_set_multi: %s\n", sc->sc_dev.dv_xname,
889 ether_sprintf(addr));
890
891 PROTOCMD(ctron_ether_set_multi, set_multi_cmd);
892 _lto2b(sizeof(addr), set_multi_cmd.length);
893 error = se_scsipi_cmd(sc->sc_link,
894 (struct scsipi_generic *) &set_multi_cmd, sizeof(set_multi_cmd),
895 addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, 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, sizeof(addr), SERETRIES, SETIMEOUT, NULL, SCSI_DATA_OUT);
917 return (error);
918 }
919
920 #if 0 /* not used --thorpej */
921 static int
922 sc_set_all_multi(sc, set)
923 struct se_softc *sc;
924 int set;
925 {
926 int error = 0;
927 u_int8_t *addr;
928 struct ethercom *ac = &sc->sc_ethercom;
929 struct ether_multi *enm;
930 struct ether_multistep step;
931
932 ETHER_FIRST_MULTI(step, ac, enm);
933 while (enm != NULL) {
934 if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
935 /*
936 * We must listen to a range of multicast addresses.
937 * For now, just accept all multicasts, rather than
938 * trying to set only those filter bits needed to match
939 * the range. (At this time, the only use of address
940 * ranges is for IP multicast routing, for which the
941 * range is big enough to require all bits set.)
942 */
943 /* We have no way of adding a range to this device.
944 * stepping through all addresses in the range is
945 * typically not possible. The only real alternative
946 * is to go into promicuous mode and filter by hand.
947 */
948 return (ENODEV);
949
950 }
951
952 addr = enm->enm_addrlo;
953 if ((error = set ? se_set_multi(sc, addr) :
954 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
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 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 break;
1090
1091 default:
1092
1093 error = EINVAL;
1094 break;
1095 }
1096
1097 splx(s);
1098 return (error);
1099 }
1100
1101 #define SEUNIT(z) (minor(z))
1102 /*
1103 * open the device.
1104 */
1105 int
1106 seopen(dev, flag, fmt, p)
1107 dev_t dev;
1108 int flag, fmt;
1109 struct proc *p;
1110 {
1111 int unit;
1112 struct se_softc *sc;
1113 struct scsipi_link *sc_link;
1114
1115 unit = SEUNIT(dev);
1116 if (unit >= se_cd.cd_ndevs)
1117 return (ENXIO);
1118 sc = se_cd.cd_devs[unit];
1119 if (sc == NULL)
1120 return (ENXIO);
1121
1122 sc_link = sc->sc_link;
1123
1124 SC_DEBUG(sc_link, SDEV_DB1,
1125 ("scopen: dev=0x%x (unit %d (of %d))\n", dev, unit,
1126 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 (scsipi_do_ioctl(sc->sc_link, dev, cmd, addr, flag, p));
1167 }
1168