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