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