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