1 /* $NetBSD: if_se.c,v 1.85 2012/10/27 17:18:38 chs 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.85 2012/10/27 17:18:38 chs 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 device_t 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_NEW(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 sc->sc_dev = self; 303 304 printf("\n"); 305 SC_DEBUG(periph, SCSIPI_DB2, ("seattach: ")); 306 307 callout_init(&sc->sc_ifstart_ch, 0); 308 callout_init(&sc->sc_recv_ch, 0); 309 310 311 /* 312 * Store information needed to contact our base driver 313 */ 314 sc->sc_periph = periph; 315 periph->periph_dev = sc->sc_dev; 316 periph->periph_switch = &se_switch; 317 318 /* XXX increase openings? */ 319 320 se_poll = (SE_POLL * hz) / 1000; 321 se_poll = se_poll? se_poll: 1; 322 se_poll0 = (SE_POLL0 * hz) / 1000; 323 se_poll0 = se_poll0? se_poll0: 1; 324 325 /* 326 * Initialize and attach a buffer 327 */ 328 sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header), 329 M_DEVBUF, M_NOWAIT); 330 if (sc->sc_tbuf == 0) 331 panic("seattach: can't allocate transmit buffer"); 332 333 sc->sc_rbuf = malloc(RBUF_LEN, M_DEVBUF, M_NOWAIT);/* A Guess */ 334 if (sc->sc_rbuf == 0) 335 panic("seattach: can't allocate receive buffer"); 336 337 se_get_addr(sc, myaddr); 338 339 /* Initialize ifnet structure. */ 340 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), sizeof(ifp->if_xname)); 341 ifp->if_softc = sc; 342 ifp->if_start = se_ifstart; 343 ifp->if_ioctl = se_ioctl; 344 ifp->if_watchdog = sewatchdog; 345 ifp->if_flags = 346 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST; 347 IFQ_SET_READY(&ifp->if_snd); 348 349 /* Attach the interface. */ 350 if_attach(ifp); 351 ether_ifattach(ifp, myaddr); 352 } 353 354 355 static inline int 356 se_scsipi_cmd(struct scsipi_periph *periph, struct scsipi_generic *cmd, 357 int cmdlen, u_char *data_addr, int datalen, int retries, int timeout, 358 struct buf *bp, int flags) 359 { 360 int error; 361 int s = splbio(); 362 363 error = scsipi_command(periph, cmd, cmdlen, data_addr, 364 datalen, retries, timeout, bp, flags); 365 splx(s); 366 return (error); 367 } 368 369 /* Start routine for calling from scsi sub system */ 370 static void 371 sestart(struct scsipi_periph *periph) 372 { 373 struct se_softc *sc = device_private(periph->periph_dev); 374 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 375 int s = splnet(); 376 377 se_ifstart(ifp); 378 (void) splx(s); 379 } 380 381 static void 382 se_delayed_ifstart(void *v) 383 { 384 struct ifnet *ifp = v; 385 struct se_softc *sc = ifp->if_softc; 386 int s; 387 388 s = splnet(); 389 if (sc->sc_enabled) { 390 ifp->if_flags &= ~IFF_OACTIVE; 391 se_ifstart(ifp); 392 } 393 splx(s); 394 } 395 396 /* 397 * Start transmission on the interface. 398 * Always called at splnet(). 399 */ 400 static void 401 se_ifstart(struct ifnet *ifp) 402 { 403 struct se_softc *sc = ifp->if_softc; 404 struct scsi_ctron_ether_generic send_cmd; 405 struct mbuf *m, *m0; 406 int len, error; 407 u_char *cp; 408 409 /* Don't transmit if interface is busy or not running */ 410 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) 411 return; 412 413 IFQ_DEQUEUE(&ifp->if_snd, m0); 414 if (m0 == 0) 415 return; 416 /* If BPF is listening on this interface, let it see the 417 * packet before we commit it to the wire. 418 */ 419 bpf_mtap(ifp, m0); 420 421 /* We need to use m->m_pkthdr.len, so require the header */ 422 if ((m0->m_flags & M_PKTHDR) == 0) 423 panic("ctscstart: no header mbuf"); 424 len = m0->m_pkthdr.len; 425 426 /* Mark the interface busy. */ 427 ifp->if_flags |= IFF_OACTIVE; 428 429 /* Chain; copy into linear buffer we allocated at attach time. */ 430 cp = sc->sc_tbuf; 431 for (m = m0; m != NULL; ) { 432 memcpy(cp, mtod(m, u_char *), m->m_len); 433 cp += m->m_len; 434 MFREE(m, m0); 435 m = m0; 436 } 437 if (len < SEMINSIZE) { 438 #ifdef SEDEBUG 439 if (sc->sc_debug) 440 printf("se: packet size %d (%zu) < %d\n", len, 441 cp - (u_char *)sc->sc_tbuf, SEMINSIZE); 442 #endif 443 memset(cp, 0, SEMINSIZE - len); 444 len = SEMINSIZE; 445 } 446 447 /* Fill out SCSI command. */ 448 PROTOCMD(ctron_ether_send, send_cmd); 449 _lto2b(len, send_cmd.length); 450 451 /* Send command to device. */ 452 error = se_scsipi_cmd(sc->sc_periph, 453 (void *)&send_cmd, sizeof(send_cmd), 454 sc->sc_tbuf, len, SERETRIES, 455 SETIMEOUT, NULL, XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_DATA_OUT); 456 if (error) { 457 aprint_error_dev(sc->sc_dev, "not queued, error %d\n", error); 458 ifp->if_oerrors++; 459 ifp->if_flags &= ~IFF_OACTIVE; 460 } else 461 ifp->if_opackets++; 462 if (sc->sc_flags & SE_NEED_RECV) { 463 sc->sc_flags &= ~SE_NEED_RECV; 464 se_recv((void *) sc); 465 } 466 } 467 468 469 /* 470 * Called from the scsibus layer via our scsi device switch. 471 */ 472 static void 473 sedone(struct scsipi_xfer *xs, int error) 474 { 475 struct se_softc *sc = device_private(xs->xs_periph->periph_dev); 476 struct scsipi_generic *cmd = xs->cmd; 477 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 478 int s; 479 480 s = splnet(); 481 if(IS_SEND(cmd)) { 482 if (xs->error == XS_BUSY) { 483 printf("se: busy, retry txmit\n"); 484 callout_reset(&sc->sc_ifstart_ch, hz, 485 se_delayed_ifstart, ifp); 486 } else { 487 ifp->if_flags &= ~IFF_OACTIVE; 488 /* the generic scsipi_done will call 489 * sestart (through scsipi_free_xs). 490 */ 491 } 492 } else if(IS_RECV(cmd)) { 493 /* RECV complete */ 494 /* pass data up. reschedule a recv */ 495 /* scsipi_free_xs will call start. Harmless. */ 496 if (error) { 497 /* Reschedule after a delay */ 498 callout_reset(&sc->sc_recv_ch, se_poll, 499 se_recv, (void *)sc); 500 } else { 501 int n, ntimeo; 502 n = se_read(sc, xs->data, xs->datalen - xs->resid); 503 if (n > se_max_received) 504 se_max_received = n; 505 if (n == 0) 506 ntimeo = se_poll; 507 else if (n >= RDATA_MAX) 508 ntimeo = se_poll0; 509 else { 510 ntimeo = sc->sc_last_timeout; 511 ntimeo = (ntimeo * RDATA_GOAL)/n; 512 ntimeo = (ntimeo < se_poll0? 513 se_poll0: ntimeo); 514 ntimeo = (ntimeo > se_poll? 515 se_poll: ntimeo); 516 } 517 sc->sc_last_timeout = ntimeo; 518 if (ntimeo == se_poll0 && 519 IFQ_IS_EMPTY(&ifp->if_snd) == 0) 520 /* Output is pending. Do next recv 521 * after the next send. */ 522 sc->sc_flags |= SE_NEED_RECV; 523 else { 524 callout_reset(&sc->sc_recv_ch, ntimeo, 525 se_recv, (void *)sc); 526 } 527 } 528 } 529 splx(s); 530 } 531 532 static void 533 se_recv(void *v) 534 { 535 /* do a recv command */ 536 struct se_softc *sc = (struct se_softc *) v; 537 struct scsi_ctron_ether_recv recv_cmd; 538 int error; 539 540 if (sc->sc_enabled == 0) 541 return; 542 543 PROTOCMD(ctron_ether_recv, recv_cmd); 544 545 error = se_scsipi_cmd(sc->sc_periph, 546 (void *)&recv_cmd, sizeof(recv_cmd), 547 sc->sc_rbuf, RBUF_LEN, SERETRIES, SETIMEOUT, NULL, 548 XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_DATA_IN); 549 if (error) 550 callout_reset(&sc->sc_recv_ch, se_poll, se_recv, (void *)sc); 551 } 552 553 /* 554 * We copy the data into mbufs. When full cluster sized units are present 555 * we copy into clusters. 556 */ 557 static struct mbuf * 558 se_get(struct se_softc *sc, char *data, int totlen) 559 { 560 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 561 struct mbuf *m, *m0, *newm; 562 int len; 563 564 MGETHDR(m0, M_DONTWAIT, MT_DATA); 565 if (m0 == 0) 566 return (0); 567 m0->m_pkthdr.rcvif = ifp; 568 m0->m_pkthdr.len = totlen; 569 len = MHLEN; 570 m = m0; 571 572 while (totlen > 0) { 573 if (totlen >= MINCLSIZE) { 574 MCLGET(m, M_DONTWAIT); 575 if ((m->m_flags & M_EXT) == 0) 576 goto bad; 577 len = MCLBYTES; 578 } 579 580 if (m == m0) { 581 char *newdata = (char *) 582 ALIGN(m->m_data + sizeof(struct ether_header)) - 583 sizeof(struct ether_header); 584 len -= newdata - m->m_data; 585 m->m_data = newdata; 586 } 587 588 m->m_len = len = min(totlen, len); 589 memcpy(mtod(m, void *), data, len); 590 data += len; 591 592 totlen -= len; 593 if (totlen > 0) { 594 MGET(newm, M_DONTWAIT, MT_DATA); 595 if (newm == 0) 596 goto bad; 597 len = MLEN; 598 m = m->m_next = newm; 599 } 600 } 601 602 return (m0); 603 604 bad: 605 m_freem(m0); 606 return (0); 607 } 608 609 /* 610 * Pass packets to higher levels. 611 */ 612 static int 613 se_read(struct se_softc *sc, char *data, int datalen) 614 { 615 struct mbuf *m; 616 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 617 int n; 618 619 n = 0; 620 while (datalen >= 2) { 621 int len = _2btol(data); 622 data += 2; 623 datalen -= 2; 624 625 if (len == 0) 626 break; 627 #ifdef SEDEBUG 628 if (sc->sc_debug) { 629 printf("se_read: datalen = %d, packetlen = %d, proto = 0x%04x\n", datalen, len, 630 ntohs(((struct ether_header *)data)->ether_type)); 631 } 632 #endif 633 if (len <= sizeof(struct ether_header) || 634 len > MAX_SNAP) { 635 #ifdef SEDEBUG 636 printf("%s: invalid packet size %d; dropping\n", 637 device_xname(sc->sc_dev), len); 638 #endif 639 ifp->if_ierrors++; 640 goto next_packet; 641 } 642 643 /* Don't need crc. Must keep ether header for BPF */ 644 m = se_get(sc, data, len - ETHER_CRC); 645 if (m == 0) { 646 #ifdef SEDEBUG 647 if (sc->sc_debug) 648 printf("se_read: se_get returned null\n"); 649 #endif 650 ifp->if_ierrors++; 651 goto next_packet; 652 } 653 if ((ifp->if_flags & IFF_PROMISC) != 0) { 654 m_adj(m, SE_PREFIX); 655 } 656 ifp->if_ipackets++; 657 658 /* 659 * Check if there's a BPF listener on this interface. 660 * If so, hand off the raw packet to BPF. 661 */ 662 bpf_mtap(ifp, m); 663 664 /* Pass the packet up. */ 665 (*ifp->if_input)(ifp, m); 666 667 next_packet: 668 data += len; 669 datalen -= len; 670 n++; 671 } 672 return (n); 673 } 674 675 676 static void 677 sewatchdog(struct ifnet *ifp) 678 { 679 struct se_softc *sc = ifp->if_softc; 680 681 log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev)); 682 ++ifp->if_oerrors; 683 684 se_reset(sc); 685 } 686 687 static int 688 se_reset(struct se_softc *sc) 689 { 690 int error; 691 int s = splnet(); 692 #if 0 693 /* Maybe we don't *really* want to reset the entire bus 694 * because the ctron isn't working. We would like to send a 695 * "BUS DEVICE RESET" message, but don't think the ctron 696 * understands it. 697 */ 698 error = se_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, SERETRIES, 2000, NULL, 699 XS_CTL_RESET); 700 #endif 701 error = se_init(sc); 702 splx(s); 703 return (error); 704 } 705 706 static int 707 se_add_proto(struct se_softc *sc, int proto) 708 { 709 int error; 710 struct scsi_ctron_ether_generic add_proto_cmd; 711 u_int8_t data[2]; 712 _lto2b(proto, data); 713 #ifdef SEDEBUG 714 if (sc->sc_debug) 715 printf("se: adding proto 0x%02x%02x\n", data[0], data[1]); 716 #endif 717 718 PROTOCMD(ctron_ether_add_proto, add_proto_cmd); 719 _lto2b(sizeof(data), add_proto_cmd.length); 720 error = se_scsipi_cmd(sc->sc_periph, 721 (void *)&add_proto_cmd, sizeof(add_proto_cmd), 722 data, sizeof(data), SERETRIES, SETIMEOUT, NULL, 723 XS_CTL_DATA_OUT); 724 return (error); 725 } 726 727 static int 728 se_get_addr(struct se_softc *sc, u_int8_t *myaddr) 729 { 730 int error; 731 struct scsi_ctron_ether_generic get_addr_cmd; 732 733 PROTOCMD(ctron_ether_get_addr, get_addr_cmd); 734 _lto2b(ETHER_ADDR_LEN, get_addr_cmd.length); 735 error = se_scsipi_cmd(sc->sc_periph, 736 (void *)&get_addr_cmd, sizeof(get_addr_cmd), 737 myaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL, 738 XS_CTL_DATA_IN); 739 printf("%s: ethernet address %s\n", device_xname(sc->sc_dev), 740 ether_sprintf(myaddr)); 741 return (error); 742 } 743 744 745 static int 746 se_set_media(struct se_softc *sc, int type) 747 { 748 int error; 749 struct scsi_ctron_ether_generic set_media_cmd; 750 751 PROTOCMD(ctron_ether_set_media, set_media_cmd); 752 set_media_cmd.byte3 = type; 753 error = se_scsipi_cmd(sc->sc_periph, 754 (void *)&set_media_cmd, sizeof(set_media_cmd), 755 0, 0, SERETRIES, SETIMEOUT, NULL, 0); 756 return (error); 757 } 758 759 static int 760 se_set_mode(struct se_softc *sc, int len, int mode) 761 { 762 int error; 763 struct scsi_ctron_ether_set_mode set_mode_cmd; 764 765 PROTOCMD(ctron_ether_set_mode, set_mode_cmd); 766 set_mode_cmd.mode = mode; 767 _lto2b(len, set_mode_cmd.length); 768 error = se_scsipi_cmd(sc->sc_periph, 769 (void *)&set_mode_cmd, sizeof(set_mode_cmd), 770 0, 0, SERETRIES, SETIMEOUT, NULL, 0); 771 return (error); 772 } 773 774 775 static int 776 se_init(struct se_softc *sc) 777 { 778 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 779 struct scsi_ctron_ether_generic set_addr_cmd; 780 uint8_t enaddr[ETHER_ADDR_LEN]; 781 int error; 782 783 if (ifp->if_flags & IFF_PROMISC) { 784 error = se_set_mode(sc, MAX_SNAP, 1); 785 } 786 else 787 error = se_set_mode(sc, ETHERMTU + sizeof(struct ether_header), 788 0); 789 if (error != 0) 790 return (error); 791 792 PROTOCMD(ctron_ether_set_addr, set_addr_cmd); 793 _lto2b(ETHER_ADDR_LEN, set_addr_cmd.length); 794 memcpy(enaddr, CLLADDR(ifp->if_sadl), sizeof(enaddr)); 795 error = se_scsipi_cmd(sc->sc_periph, 796 (void *)&set_addr_cmd, sizeof(set_addr_cmd), 797 enaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL, 798 XS_CTL_DATA_OUT); 799 if (error != 0) 800 return (error); 801 802 if ((sc->protos & PROTO_IP) && 803 (error = se_add_proto(sc, ETHERTYPE_IP)) != 0) 804 return (error); 805 if ((sc->protos & PROTO_ARP) && 806 (error = se_add_proto(sc, ETHERTYPE_ARP)) != 0) 807 return (error); 808 if ((sc->protos & PROTO_REVARP) && 809 (error = se_add_proto(sc, ETHERTYPE_REVARP)) != 0) 810 return (error); 811 #ifdef NETATALK 812 if ((sc->protos & PROTO_AT) && 813 (error = se_add_proto(sc, ETHERTYPE_ATALK)) != 0) 814 return (error); 815 if ((sc->protos & PROTO_AARP) && 816 (error = se_add_proto(sc, ETHERTYPE_AARP)) != 0) 817 return (error); 818 #endif 819 820 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == IFF_UP) { 821 ifp->if_flags |= IFF_RUNNING; 822 se_recv(sc); 823 ifp->if_flags &= ~IFF_OACTIVE; 824 se_ifstart(ifp); 825 } 826 return (error); 827 } 828 829 static int 830 se_set_multi(struct se_softc *sc, u_int8_t *addr) 831 { 832 struct scsi_ctron_ether_generic set_multi_cmd; 833 int error; 834 835 if (sc->sc_debug) 836 printf("%s: set_set_multi: %s\n", device_xname(sc->sc_dev), 837 ether_sprintf(addr)); 838 839 PROTOCMD(ctron_ether_set_multi, set_multi_cmd); 840 _lto2b(sizeof(addr), set_multi_cmd.length); 841 /* XXX sizeof(addr) is the size of the pointer. Surely it 842 * is too small? --dyoung 843 */ 844 error = se_scsipi_cmd(sc->sc_periph, 845 (void *)&set_multi_cmd, sizeof(set_multi_cmd), 846 addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT); 847 return (error); 848 } 849 850 static int 851 se_remove_multi(struct se_softc *sc, u_int8_t *addr) 852 { 853 struct scsi_ctron_ether_generic remove_multi_cmd; 854 int error; 855 856 if (sc->sc_debug) 857 printf("%s: se_remove_multi: %s\n", device_xname(sc->sc_dev), 858 ether_sprintf(addr)); 859 860 PROTOCMD(ctron_ether_remove_multi, remove_multi_cmd); 861 _lto2b(sizeof(addr), remove_multi_cmd.length); 862 /* XXX sizeof(addr) is the size of the pointer. Surely it 863 * is too small? --dyoung 864 */ 865 error = se_scsipi_cmd(sc->sc_periph, 866 (void *)&remove_multi_cmd, sizeof(remove_multi_cmd), 867 addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT); 868 return (error); 869 } 870 871 #if 0 /* not used --thorpej */ 872 static int 873 sc_set_all_multi(struct se_softc *sc, int set) 874 { 875 int error = 0; 876 u_int8_t *addr; 877 struct ethercom *ac = &sc->sc_ethercom; 878 struct ether_multi *enm; 879 struct ether_multistep step; 880 881 ETHER_FIRST_MULTI(step, ac, enm); 882 while (enm != NULL) { 883 if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) { 884 /* 885 * We must listen to a range of multicast addresses. 886 * For now, just accept all multicasts, rather than 887 * trying to set only those filter bits needed to match 888 * the range. (At this time, the only use of address 889 * ranges is for IP multicast routing, for which the 890 * range is big enough to require all bits set.) 891 */ 892 /* We have no way of adding a range to this device. 893 * stepping through all addresses in the range is 894 * typically not possible. The only real alternative 895 * is to go into promicuous mode and filter by hand. 896 */ 897 return (ENODEV); 898 899 } 900 901 addr = enm->enm_addrlo; 902 if ((error = set ? se_set_multi(sc, addr) : 903 se_remove_multi(sc, addr)) != 0) 904 return (error); 905 ETHER_NEXT_MULTI(step, enm); 906 } 907 return (error); 908 } 909 #endif /* not used */ 910 911 static void 912 se_stop(struct se_softc *sc) 913 { 914 915 /* Don't schedule any reads */ 916 callout_stop(&sc->sc_recv_ch); 917 918 /* How can we abort any scsi cmds in progress? */ 919 } 920 921 922 /* 923 * Process an ioctl request. 924 */ 925 static int 926 se_ioctl(struct ifnet *ifp, u_long cmd, void *data) 927 { 928 struct se_softc *sc = ifp->if_softc; 929 struct ifaddr *ifa = (struct ifaddr *)data; 930 struct ifreq *ifr = (struct ifreq *)data; 931 struct sockaddr *sa; 932 int s, error = 0; 933 934 s = splnet(); 935 936 switch (cmd) { 937 938 case SIOCINITIFADDR: 939 if ((error = se_enable(sc)) != 0) 940 break; 941 ifp->if_flags |= IFF_UP; 942 943 if ((error = se_set_media(sc, CMEDIA_AUTOSENSE)) != 0) 944 break; 945 946 switch (ifa->ifa_addr->sa_family) { 947 #ifdef INET 948 case AF_INET: 949 sc->protos |= (PROTO_IP | PROTO_ARP | PROTO_REVARP); 950 if ((error = se_init(sc)) != 0) 951 break; 952 arp_ifinit(ifp, ifa); 953 break; 954 #endif 955 #ifdef NETATALK 956 case AF_APPLETALK: 957 sc->protos |= (PROTO_AT | PROTO_AARP); 958 if ((error = se_init(sc)) != 0) 959 break; 960 break; 961 #endif 962 default: 963 error = se_init(sc); 964 break; 965 } 966 break; 967 968 969 case SIOCSIFFLAGS: 970 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 971 break; 972 /* XXX re-use ether_ioctl() */ 973 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { 974 case IFF_RUNNING: 975 /* 976 * If interface is marked down and it is running, then 977 * stop it. 978 */ 979 se_stop(sc); 980 ifp->if_flags &= ~IFF_RUNNING; 981 se_disable(sc); 982 break; 983 case IFF_UP: 984 /* 985 * If interface is marked up and it is stopped, then 986 * start it. 987 */ 988 if ((error = se_enable(sc)) != 0) 989 break; 990 error = se_init(sc); 991 break; 992 default: 993 /* 994 * Reset the interface to pick up changes in any other 995 * flags that affect hardware registers. 996 */ 997 if (sc->sc_enabled) 998 error = se_init(sc); 999 break; 1000 } 1001 #ifdef SEDEBUG 1002 if (ifp->if_flags & IFF_DEBUG) 1003 sc->sc_debug = 1; 1004 else 1005 sc->sc_debug = 0; 1006 #endif 1007 break; 1008 1009 case SIOCADDMULTI: 1010 case SIOCDELMULTI: 1011 sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_NOWAIT); 1012 if (sa == NULL) { 1013 error = ENOBUFS; 1014 break; 1015 } 1016 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) { 1017 if (ifp->if_flags & IFF_RUNNING) { 1018 error = (cmd == SIOCADDMULTI) ? 1019 se_set_multi(sc, sa->sa_data) : 1020 se_remove_multi(sc, sa->sa_data); 1021 } else 1022 error = 0; 1023 } 1024 sockaddr_free(sa); 1025 break; 1026 1027 default: 1028 1029 error = ether_ioctl(ifp, cmd, data); 1030 break; 1031 } 1032 1033 splx(s); 1034 return (error); 1035 } 1036 1037 /* 1038 * Enable the network interface. 1039 */ 1040 int 1041 se_enable(struct se_softc *sc) 1042 { 1043 struct scsipi_periph *periph = sc->sc_periph; 1044 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 1045 int error = 0; 1046 1047 if (sc->sc_enabled == 0 && 1048 (error = scsipi_adapter_addref(adapt)) == 0) 1049 sc->sc_enabled = 1; 1050 else 1051 aprint_error_dev(sc->sc_dev, "device enable failed\n"); 1052 1053 return (error); 1054 } 1055 1056 /* 1057 * Disable the network interface. 1058 */ 1059 void 1060 se_disable(struct se_softc *sc) 1061 { 1062 struct scsipi_periph *periph = sc->sc_periph; 1063 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 1064 1065 if (sc->sc_enabled != 0) { 1066 scsipi_adapter_delref(adapt); 1067 sc->sc_enabled = 0; 1068 } 1069 } 1070 1071 #define SEUNIT(z) (minor(z)) 1072 /* 1073 * open the device. 1074 */ 1075 int 1076 seopen(dev_t dev, int flag, int fmt, struct lwp *l) 1077 { 1078 int unit, error; 1079 struct se_softc *sc; 1080 struct scsipi_periph *periph; 1081 struct scsipi_adapter *adapt; 1082 1083 unit = SEUNIT(dev); 1084 sc = device_lookup_private(&se_cd, unit); 1085 if (sc == NULL) 1086 return (ENXIO); 1087 1088 periph = sc->sc_periph; 1089 adapt = periph->periph_channel->chan_adapter; 1090 1091 if ((error = scsipi_adapter_addref(adapt)) != 0) 1092 return (error); 1093 1094 SC_DEBUG(periph, SCSIPI_DB1, 1095 ("scopen: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit, 1096 se_cd.cd_ndevs)); 1097 1098 periph->periph_flags |= PERIPH_OPEN; 1099 1100 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n")); 1101 return (0); 1102 } 1103 1104 /* 1105 * close the device.. only called if we are the LAST 1106 * occurence of an open device 1107 */ 1108 int 1109 seclose(dev_t dev, int flag, int fmt, struct lwp *l) 1110 { 1111 struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev)); 1112 struct scsipi_periph *periph = sc->sc_periph; 1113 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 1114 1115 SC_DEBUG(sc->sc_periph, SCSIPI_DB1, ("closing\n")); 1116 1117 scsipi_wait_drain(periph); 1118 1119 scsipi_adapter_delref(adapt); 1120 periph->periph_flags &= ~PERIPH_OPEN; 1121 1122 return (0); 1123 } 1124 1125 /* 1126 * Perform special action on behalf of the user 1127 * Only does generic scsi ioctls. 1128 */ 1129 int 1130 seioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 1131 { 1132 struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev)); 1133 1134 return (scsipi_do_ioctl(sc->sc_periph, dev, cmd, addr, flag, l)); 1135 } 1136