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