1 /* $OpenBSD: am7990.c,v 1.47 2013/09/24 20:10:55 miod Exp $ */ 2 /* $NetBSD: am7990.c,v 1.74 2012/02/02 19:43:02 tls Exp $ */ 3 4 /*- 5 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace 10 * Simulation Facility, NASA Ames Research Center. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /*- 35 * Copyright (c) 1992, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * This code is derived from software contributed to Berkeley by 39 * Ralph Campbell and Rick Macklem. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 3. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)if_le.c 8.2 (Berkeley) 11/16/93 66 */ 67 68 #include "bpfilter.h" 69 70 #include <sys/param.h> 71 #include <sys/systm.h> 72 #include <sys/mbuf.h> 73 #include <sys/syslog.h> 74 #include <sys/socket.h> 75 #include <sys/device.h> 76 #include <sys/malloc.h> 77 #include <sys/ioctl.h> 78 #include <sys/errno.h> 79 80 #include <net/if.h> 81 #include <net/if_media.h> 82 83 #ifdef INET 84 #include <netinet/in.h> 85 #include <netinet/if_ether.h> 86 #include <netinet/in_systm.h> 87 #include <netinet/ip.h> 88 #endif 89 90 #if NBPFILTER > 0 91 #include <net/bpf.h> 92 #endif 93 94 #include <dev/ic/lancereg.h> 95 #include <dev/ic/lancevar.h> 96 #include <dev/ic/am7990reg.h> 97 #include <dev/ic/am7990var.h> 98 99 void am7990_meminit(struct lance_softc *); 100 void am7990_start(struct ifnet *); 101 102 void am7990_rint(struct lance_softc *); 103 void am7990_tint(struct lance_softc *); 104 105 #ifdef LEDEBUG 106 void am7990_recv_print(struct lance_softc *, int); 107 void am7990_xmit_print(struct lance_softc *, int); 108 #endif 109 110 /* 111 * am7990 configuration driver. Attachments are provided by 112 * machine-dependent driver front-ends. 113 */ 114 void 115 am7990_config(struct am7990_softc *sc) 116 { 117 int mem, i; 118 119 sc->lsc.sc_meminit = am7990_meminit; 120 sc->lsc.sc_start = am7990_start; 121 122 lance_config(&sc->lsc); 123 124 mem = 0; 125 sc->lsc.sc_initaddr = mem; 126 mem += sizeof(struct leinit); 127 sc->lsc.sc_rmdaddr = mem; 128 mem += sizeof(struct lermd) * sc->lsc.sc_nrbuf; 129 sc->lsc.sc_tmdaddr = mem; 130 mem += sizeof(struct letmd) * sc->lsc.sc_ntbuf; 131 for (i = 0; i < sc->lsc.sc_nrbuf; i++, mem += LEBLEN) 132 sc->lsc.sc_rbufaddr[i] = mem; 133 for (i = 0; i < sc->lsc.sc_ntbuf; i++, mem += LEBLEN) 134 sc->lsc.sc_tbufaddr[i] = mem; 135 #ifdef notyet 136 if (mem > ...) 137 panic(...); 138 #endif 139 } 140 141 /* 142 * Set up the initialization block and the descriptor rings. 143 */ 144 void 145 am7990_meminit(struct lance_softc *sc) 146 { 147 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 148 u_long a; 149 int bix; 150 struct leinit init; 151 struct lermd rmd; 152 struct letmd tmd; 153 uint8_t *myaddr; 154 155 if (ifp->if_flags & IFF_PROMISC) 156 init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM; 157 else 158 init.init_mode = LE_MODE_NORMAL; 159 if (sc->sc_initmodemedia == 1) 160 init.init_mode |= LE_MODE_PSEL0; 161 162 /* 163 * Update our private copy of the Ethernet address. 164 * We NEED the copy so we can ensure its alignment! 165 */ 166 memcpy(sc->sc_enaddr, sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN); 167 myaddr = sc->sc_enaddr; 168 169 init.init_padr[0] = (myaddr[1] << 8) | myaddr[0]; 170 init.init_padr[1] = (myaddr[3] << 8) | myaddr[2]; 171 init.init_padr[2] = (myaddr[5] << 8) | myaddr[4]; 172 lance_setladrf(&sc->sc_arpcom, init.init_ladrf); 173 174 sc->sc_last_rd = 0; 175 sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0; 176 177 a = sc->sc_addr + LE_RMDADDR(sc, 0); 178 init.init_rdra = a; 179 init.init_rlen = (a >> 16) | ((ffs(sc->sc_nrbuf) - 1) << 13); 180 181 a = sc->sc_addr + LE_TMDADDR(sc, 0); 182 init.init_tdra = a; 183 init.init_tlen = (a >> 16) | ((ffs(sc->sc_ntbuf) - 1) << 13); 184 185 (*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init)); 186 187 /* 188 * Set up receive ring descriptors. 189 */ 190 for (bix = 0; bix < sc->sc_nrbuf; bix++) { 191 a = sc->sc_addr + LE_RBUFADDR(sc, bix); 192 rmd.rmd0 = a; 193 rmd.rmd1_hadr = a >> 16; 194 rmd.rmd1_bits = LE_R1_OWN; 195 rmd.rmd2 = -LEBLEN | LE_XMD2_ONES; 196 rmd.rmd3 = 0; 197 (*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix), 198 sizeof(rmd)); 199 } 200 201 /* 202 * Set up transmit ring descriptors. 203 */ 204 for (bix = 0; bix < sc->sc_ntbuf; bix++) { 205 a = sc->sc_addr + LE_TBUFADDR(sc, bix); 206 tmd.tmd0 = a; 207 tmd.tmd1_hadr = a >> 16; 208 tmd.tmd1_bits = 0; 209 tmd.tmd2 = 0 | LE_XMD2_ONES; 210 tmd.tmd3 = 0; 211 (*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix), 212 sizeof(tmd)); 213 } 214 } 215 216 void 217 am7990_rint(struct lance_softc *sc) 218 { 219 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 220 int bix; 221 int rp; 222 struct lermd rmd; 223 224 bix = sc->sc_last_rd; 225 226 /* Process all buffers with valid data. */ 227 for (;;) { 228 rp = LE_RMDADDR(sc, bix); 229 (*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd)); 230 231 if (rmd.rmd1_bits & LE_R1_OWN) 232 break; 233 234 if (rmd.rmd1_bits & LE_R1_ERR) { 235 if (rmd.rmd1_bits & LE_R1_ENP) { 236 #ifdef LEDEBUG 237 if ((rmd.rmd1_bits & LE_R1_OFLO) == 0) { 238 if (rmd.rmd1_bits & LE_R1_FRAM) 239 printf("%s: framing error\n", 240 sc->sc_dev.dv_xname); 241 if (rmd.rmd1_bits & LE_R1_CRC) 242 printf("%s: crc mismatch\n", 243 sc->sc_dev.dv_xname); 244 } 245 #endif 246 } else { 247 if (rmd.rmd1_bits & LE_R1_OFLO) 248 printf("%s: overflow\n", 249 sc->sc_dev.dv_xname); 250 } 251 if (rmd.rmd1_bits & LE_R1_BUFF) 252 printf("%s: receive buffer error\n", 253 sc->sc_dev.dv_xname); 254 ifp->if_ierrors++; 255 } else if ((rmd.rmd1_bits & (LE_R1_STP | LE_R1_ENP)) != 256 (LE_R1_STP | LE_R1_ENP)) { 257 printf("%s: dropping chained buffer\n", 258 sc->sc_dev.dv_xname); 259 ifp->if_ierrors++; 260 } else { 261 #ifdef LEDEBUG 262 if (sc->sc_debug > 1) 263 am7990_recv_print(sc, sc->sc_last_rd); 264 #endif 265 lance_read(sc, LE_RBUFADDR(sc, bix), 266 (int)rmd.rmd3 - 4); 267 } 268 269 rmd.rmd1_bits = LE_R1_OWN; 270 rmd.rmd2 = -LEBLEN | LE_XMD2_ONES; 271 rmd.rmd3 = 0; 272 (*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd)); 273 274 #ifdef LEDEBUG 275 if (sc->sc_debug) 276 printf("sc->sc_last_rd = %x, rmd: " 277 "ladr %04x, hadr %02x, flags %02x, " 278 "bcnt %04x, mcnt %04x\n", 279 sc->sc_last_rd, 280 rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, 281 rmd.rmd2, rmd.rmd3); 282 #endif 283 284 if (++bix == sc->sc_nrbuf) 285 bix = 0; 286 } 287 288 sc->sc_last_rd = bix; 289 } 290 291 void 292 am7990_tint(struct lance_softc *sc) 293 { 294 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 295 int bix; 296 struct letmd tmd; 297 298 bix = sc->sc_first_td; 299 300 for (;;) { 301 if (sc->sc_no_td <= 0) 302 break; 303 304 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix), 305 sizeof(tmd)); 306 307 #ifdef LEDEBUG 308 if (sc->sc_debug) 309 printf("trans tmd: " 310 "ladr %04x, hadr %02x, flags %02x, " 311 "bcnt %04x, mcnt %04x\n", 312 tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, 313 tmd.tmd2, tmd.tmd3); 314 #endif 315 316 if (tmd.tmd1_bits & LE_T1_OWN) 317 break; 318 319 ifp->if_flags &= ~IFF_OACTIVE; 320 321 if (tmd.tmd1_bits & LE_T1_ERR) { 322 if (tmd.tmd3 & LE_T3_BUFF) 323 printf("%s: transmit buffer error\n", 324 sc->sc_dev.dv_xname); 325 else if (tmd.tmd3 & LE_T3_UFLO) 326 printf("%s: underflow\n", sc->sc_dev.dv_xname); 327 if (tmd.tmd3 & (LE_T3_BUFF | LE_T3_UFLO)) { 328 lance_reset(sc); 329 return; 330 } 331 if (tmd.tmd3 & LE_T3_LCAR) { 332 sc->sc_havecarrier = 0; 333 if (sc->sc_nocarrier) 334 (*sc->sc_nocarrier)(sc); 335 else 336 printf("%s: lost carrier\n", 337 sc->sc_dev.dv_xname); 338 } 339 if (tmd.tmd3 & LE_T3_LCOL) 340 ifp->if_collisions++; 341 if (tmd.tmd3 & LE_T3_RTRY) { 342 #ifdef LEDEBUG 343 printf("%s: excessive collisions, tdr %d\n", 344 sc->sc_dev.dv_xname, 345 tmd.tmd3 & LE_T3_TDR_MASK); 346 #endif 347 ifp->if_collisions += 16; 348 } 349 ifp->if_oerrors++; 350 } else { 351 if (tmd.tmd1_bits & LE_T1_ONE) 352 ifp->if_collisions++; 353 else if (tmd.tmd1_bits & LE_T1_MORE) 354 /* Real number is unknown. */ 355 ifp->if_collisions += 2; 356 ifp->if_opackets++; 357 } 358 359 if (++bix == sc->sc_ntbuf) 360 bix = 0; 361 362 --sc->sc_no_td; 363 } 364 365 sc->sc_first_td = bix; 366 367 am7990_start(ifp); 368 369 if (sc->sc_no_td == 0) 370 ifp->if_timer = 0; 371 } 372 373 /* 374 * Controller interrupt. 375 */ 376 int 377 am7990_intr(void *arg) 378 { 379 struct lance_softc *sc = arg; 380 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 381 uint16_t isr; 382 383 isr = (*sc->sc_rdcsr)(sc, LE_CSR0) | sc->sc_saved_csr0; 384 sc->sc_saved_csr0 = 0; 385 #if defined(LEDEBUG) && LEDEBUG > 1 386 if (sc->sc_debug) 387 printf("%s: am7990_intr entering with isr=%04x\n", 388 sc->sc_dev.dv_xname, isr); 389 #endif 390 if ((isr & LE_C0_INTR) == 0) 391 return (0); 392 393 /* 394 * After receiving an interrupt, we need to toggle the interrupt 395 * enable bit in order to keep receiving them (some chips works 396 * without this, some do not) 397 */ 398 (*sc->sc_wrcsr)(sc, LE_CSR0, isr & ~LE_C0_INEA); 399 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA); 400 401 if (isr & LE_C0_ERR) { 402 if (isr & LE_C0_BABL) { 403 #ifdef LEDEBUG 404 printf("%s: babble\n", sc->sc_dev.dv_xname); 405 #endif 406 ifp->if_oerrors++; 407 } 408 #if 0 409 if (isr & LE_C0_CERR) { 410 printf("%s: collision error\n", sc->sc_dev.dv_xname); 411 ifp->if_collisions++; 412 } 413 #endif 414 if (isr & LE_C0_MISS) { 415 #ifdef LEDEBUG 416 printf("%s: missed packet\n", sc->sc_dev.dv_xname); 417 #endif 418 ifp->if_ierrors++; 419 } 420 if (isr & LE_C0_MERR) { 421 printf("%s: memory error\n", sc->sc_dev.dv_xname); 422 lance_reset(sc); 423 return (1); 424 } 425 } 426 427 if ((isr & LE_C0_RXON) == 0) { 428 printf("%s: receiver disabled\n", sc->sc_dev.dv_xname); 429 ifp->if_ierrors++; 430 lance_reset(sc); 431 return (1); 432 } 433 if ((isr & LE_C0_TXON) == 0) { 434 printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname); 435 ifp->if_oerrors++; 436 lance_reset(sc); 437 return (1); 438 } 439 440 /* 441 * Pretend we have carrier; if we don't this will be cleared 442 * shortly. 443 */ 444 sc->sc_havecarrier = 1; 445 446 if (isr & LE_C0_RINT) 447 am7990_rint(sc); 448 if (isr & LE_C0_TINT) 449 am7990_tint(sc); 450 451 return (1); 452 } 453 454 /* 455 * Setup output on interface. 456 * Get another datagram to send off of the interface queue, and map it to the 457 * interface before starting the output. 458 * Called only at splnet or interrupt level. 459 */ 460 void 461 am7990_start(struct ifnet *ifp) 462 { 463 struct lance_softc *sc = ifp->if_softc; 464 int bix; 465 struct mbuf *m; 466 struct letmd tmd; 467 int rp; 468 int len; 469 470 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 471 return; 472 473 bix = sc->sc_last_td; 474 475 for (;;) { 476 rp = LE_TMDADDR(sc, bix); 477 (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd)); 478 479 if (tmd.tmd1_bits & LE_T1_OWN) { 480 ifp->if_flags |= IFF_OACTIVE; 481 printf("missing buffer, no_td = %d, last_td = %d\n", 482 sc->sc_no_td, sc->sc_last_td); 483 } 484 485 IFQ_DEQUEUE(&ifp->if_snd, m); 486 if (m == 0) 487 break; 488 489 #if NBPFILTER > 0 490 /* 491 * If BPF is listening on this interface, let it see the packet 492 * before we commit it to the wire. 493 */ 494 if (ifp->if_bpf) 495 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT); 496 #endif 497 498 /* 499 * Copy the mbuf chain into the transmit buffer. 500 */ 501 len = lance_put(sc, LE_TBUFADDR(sc, bix), m); 502 503 #ifdef LEDEBUG 504 if (len > ETHERMTU + sizeof(struct ether_header)) 505 printf("packet length %d\n", len); 506 #endif 507 508 ifp->if_timer = 5; 509 510 /* 511 * Init transmit registers, and set transmit start flag. 512 */ 513 tmd.tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP; 514 tmd.tmd2 = -len | LE_XMD2_ONES; 515 tmd.tmd3 = 0; 516 517 (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd)); 518 519 #ifdef LEDEBUG 520 if (sc->sc_debug > 1) 521 am7990_xmit_print(sc, sc->sc_last_td); 522 #endif 523 524 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD); 525 526 if (++bix == sc->sc_ntbuf) 527 bix = 0; 528 529 if (++sc->sc_no_td == sc->sc_ntbuf) { 530 ifp->if_flags |= IFF_OACTIVE; 531 break; 532 } 533 534 } 535 536 sc->sc_last_td = bix; 537 } 538 539 #ifdef LEDEBUG 540 void 541 am7990_recv_print(struct lance_softc *sc, int no) 542 { 543 struct lermd rmd; 544 uint16_t len; 545 struct ether_header eh; 546 547 (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd)); 548 len = rmd.rmd3; 549 printf("%s: receive buffer %d, len = %d\n", 550 sc->sc_dev.dv_xname, no, len); 551 printf("%s: status %04x\n", sc->sc_dev.dv_xname, 552 (*sc->sc_rdcsr)(sc, LE_CSR0)); 553 printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n", 554 sc->sc_dev.dv_xname, 555 rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, rmd.rmd2, rmd.rmd3); 556 if (len >= sizeof(eh)) { 557 (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh)); 558 printf("%s: dst %s", sc->sc_dev.dv_xname, 559 ether_sprintf(eh.ether_dhost)); 560 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost), 561 ntohs(eh.ether_type)); 562 } 563 } 564 565 void 566 am7990_xmit_print(struct lance_softc *sc, int no) 567 { 568 struct letmd tmd; 569 uint16_t len; 570 struct ether_header eh; 571 572 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd)); 573 len = -tmd.tmd2; 574 printf("%s: transmit buffer %d, len = %d\n", 575 sc->sc_dev.dv_xname, no, len); 576 printf("%s: status %04x\n", sc->sc_dev.dv_xname, 577 (*sc->sc_rdcsr)(sc, LE_CSR0)); 578 printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n", 579 sc->sc_dev.dv_xname, 580 tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, tmd.tmd2, tmd.tmd3); 581 if (len >= sizeof(eh)) { 582 (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh)); 583 printf("%s: dst %s", sc->sc_dev.dv_xname, 584 ether_sprintf(eh.ether_dhost)); 585 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost), 586 ntohs(eh.ether_type)); 587 } 588 } 589 #endif /* LEDEBUG */ 590