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