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