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