1 /* $NetBSD: mb86960.c,v 1.50 2001/11/13 13:14:41 lukem Exp $ */ 2 3 /* 4 * All Rights Reserved, Copyright (C) Fujitsu Limited 1995 5 * 6 * This software may be used, modified, copied, distributed, and sold, in 7 * both source and binary form provided that the above copyright, these 8 * terms and the following disclaimer are retained. The name of the author 9 * and/or the contributor may not be used to endorse or promote products 10 * derived from this software without specific prior written permission. 11 * 12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND 13 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 15 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE 16 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 17 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 18 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION. 19 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 20 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 22 * SUCH DAMAGE. 23 */ 24 25 /* 26 * Portions copyright (C) 1993, David Greenman. This software may be used, 27 * modified, copied, distributed, and sold, in both source and binary form 28 * provided that the above copyright and these terms are retained. Under no 29 * circumstances is the author responsible for the proper functioning of this 30 * software, nor does the author assume any responsibility for damages 31 * incurred with its use. 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: mb86960.c,v 1.50 2001/11/13 13:14:41 lukem Exp $"); 36 37 #define FE_VERSION "if_fe.c ver. 0.8" 38 39 /* 40 * Device driver for Fujitsu MB86960A/MB86965A based Ethernet cards. 41 * Contributed by M.S. <seki@sysrap.cs.fujitsu.co.jp> 42 * 43 * This version is intended to be a generic template for various 44 * MB86960A/MB86965A based Ethernet cards. It currently supports 45 * Fujitsu FMV-180 series (i.e., FMV-181 and FMV-182) and Allied- 46 * Telesis AT1700 series and RE2000 series. There are some 47 * unnecessary hooks embedded, which are primarily intended to support 48 * other types of Ethernet cards, but the author is not sure whether 49 * they are useful. 50 */ 51 52 #include "opt_inet.h" 53 #include "opt_ns.h" 54 #include "bpfilter.h" 55 #include "rnd.h" 56 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/errno.h> 60 #include <sys/ioctl.h> 61 #include <sys/mbuf.h> 62 #include <sys/socket.h> 63 #include <sys/syslog.h> 64 #include <sys/device.h> 65 #if NRND > 0 66 #include <sys/rnd.h> 67 #endif 68 69 #include <net/if.h> 70 #include <net/if_dl.h> 71 #include <net/if_types.h> 72 #include <net/if_media.h> 73 #include <net/if_ether.h> 74 75 #ifdef INET 76 #include <netinet/in.h> 77 #include <netinet/in_systm.h> 78 #include <netinet/in_var.h> 79 #include <netinet/ip.h> 80 #include <netinet/if_inarp.h> 81 #endif 82 83 #ifdef NS 84 #include <netns/ns.h> 85 #include <netns/ns_if.h> 86 #endif 87 88 #if NBPFILTER > 0 89 #include <net/bpf.h> 90 #include <net/bpfdesc.h> 91 #endif 92 93 #include <machine/bus.h> 94 95 #include <dev/ic/mb86960reg.h> 96 #include <dev/ic/mb86960var.h> 97 98 #ifndef __BUS_SPACE_HAS_STREAM_METHODS 99 #define bus_space_write_multi_stream_2 bus_space_write_multi_2 100 #define bus_space_read_multi_stream_2 bus_space_read_multi_2 101 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */ 102 103 /* Standard driver entry points. These can be static. */ 104 void mb86960_init __P((struct mb86960_softc *)); 105 int mb86960_ioctl __P((struct ifnet *, u_long, caddr_t)); 106 void mb86960_start __P((struct ifnet *)); 107 void mb86960_reset __P((struct mb86960_softc *)); 108 void mb86960_watchdog __P((struct ifnet *)); 109 110 /* Local functions. Order of declaration is confused. FIXME. */ 111 int mb86960_get_packet __P((struct mb86960_softc *, int)); 112 void mb86960_stop __P((struct mb86960_softc *)); 113 void mb86960_tint __P((struct mb86960_softc *, u_char)); 114 void mb86960_rint __P((struct mb86960_softc *, u_char)); 115 static __inline__ 116 void mb86960_xmit __P((struct mb86960_softc *)); 117 void mb86960_write_mbufs __P((struct mb86960_softc *, struct mbuf *)); 118 static __inline__ 119 void mb86960_droppacket __P((struct mb86960_softc *)); 120 void mb86960_getmcaf __P((struct ethercom *, u_char *)); 121 void mb86960_setmode __P((struct mb86960_softc *)); 122 void mb86960_loadmar __P((struct mb86960_softc *)); 123 124 int mb86960_mediachange __P((struct ifnet *)); 125 void mb86960_mediastatus __P((struct ifnet *, struct ifmediareq *)); 126 127 #if FE_DEBUG >= 1 128 void mb86960_dump __P((int, struct mb86960_softc *)); 129 #endif 130 131 void 132 mb86960_attach(sc, type, myea) 133 struct mb86960_softc *sc; 134 enum mb86960_type type; 135 u_int8_t *myea; 136 { 137 bus_space_tag_t bst = sc->sc_bst; 138 bus_space_handle_t bsh = sc->sc_bsh; 139 140 sc->type = type; 141 142 /* Register values which depend on board design. */ 143 sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL; 144 sc->proto_dlcr5 = 0; 145 sc->proto_bmpr13 = FE_B13_TPTYPE_UTP | FE_B13_PORT_AUTO; 146 147 switch (sc->type) { 148 case MB86960_TYPE_86960: 149 sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_EC; 150 break; 151 case MB86960_TYPE_86965: 152 sc->proto_dlcr7 = FE_D7_BYTSWP_LH; 153 break; 154 } 155 156 /* 157 * Program the 86960 as follows: 158 * SRAM: 32KB, 100ns, byte-wide access. 159 * Transmission buffer: 4KB x 2. 160 * System bus interface: 16 bits. 161 * We cannot change these values but TXBSIZE, because they 162 * are hard-wired on the board. Modifying TXBSIZE will affect 163 * the driver performance. 164 */ 165 sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB | 166 FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns; 167 168 /* 169 * Minimum initialization of the hardware. 170 * We write into registers; hope I/O ports have no 171 * overlap with other boards. 172 */ 173 174 /* Initialize 86960. */ 175 bus_space_write_1(bst, bsh, FE_DLCR6, 176 sc->proto_dlcr6 | FE_D6_DLC_DISABLE); 177 delay(200); 178 179 #ifdef DIAGNOSTIC 180 if (myea == NULL) { 181 printf("%s: ethernet address shouldn't be NULL\n", 182 sc->sc_dev.dv_xname); 183 panic("NULL ethernet address"); 184 } 185 #endif 186 memcpy(sc->sc_enaddr, myea, sizeof(sc->sc_enaddr)); 187 188 /* Disable all interrupts. */ 189 bus_space_write_1(bst, bsh, FE_DLCR2, 0); 190 bus_space_write_1(bst, bsh, FE_DLCR3, 0); 191 } 192 193 /* 194 * Install interface into kernel networking data structures 195 */ 196 void 197 mb86960_config(sc, media, nmedia, defmedia) 198 struct mb86960_softc *sc; 199 int *media, nmedia, defmedia; 200 { 201 struct cfdata *cf = sc->sc_dev.dv_cfdata; 202 struct ifnet *ifp = &sc->sc_ec.ec_if; 203 int i; 204 205 /* Stop the 86960. */ 206 mb86960_stop(sc); 207 208 /* Initialize ifnet structure. */ 209 strcpy(ifp->if_xname, sc->sc_dev.dv_xname); 210 ifp->if_softc = sc; 211 ifp->if_start = mb86960_start; 212 ifp->if_ioctl = mb86960_ioctl; 213 ifp->if_watchdog = mb86960_watchdog; 214 ifp->if_flags = 215 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST; 216 IFQ_SET_READY(&ifp->if_snd); 217 218 #if FE_DEBUG >= 3 219 log(LOG_INFO, "%s: mb86960_config()\n", sc->sc_dev.dv_xname); 220 mb86960_dump(LOG_INFO, sc); 221 #endif 222 223 #if FE_SINGLE_TRANSMISSION 224 /* Override txb config to allocate minimum. */ 225 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ 226 sc->proto_dlcr6 |= FE_D6_TXBSIZ_2x2KB; 227 #endif 228 229 /* Modify hardware config if it is requested. */ 230 if ((cf->cf_flags & FE_FLAGS_OVERRIDE_DLCR6) != 0) 231 sc->proto_dlcr6 = cf->cf_flags & FE_FLAGS_DLCR6_VALUE; 232 233 /* Find TX buffer size, based on the hardware dependent proto. */ 234 switch (sc->proto_dlcr6 & FE_D6_TXBSIZ) { 235 case FE_D6_TXBSIZ_2x2KB: 236 sc->txb_size = 2048; 237 break; 238 case FE_D6_TXBSIZ_2x4KB: 239 sc->txb_size = 4096; 240 break; 241 case FE_D6_TXBSIZ_2x8KB: 242 sc->txb_size = 8192; 243 break; 244 default: 245 /* Oops, we can't work with single buffer configuration. */ 246 #if FE_DEBUG >= 2 247 log(LOG_WARNING, "%s: strange TXBSIZ config; fixing\n", 248 sc->sc_dev.dv_xname); 249 #endif 250 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ; 251 sc->proto_dlcr6 |= FE_D6_TXBSIZ_2x2KB; 252 sc->txb_size = 2048; 253 break; 254 } 255 256 /* Initialize media goo. */ 257 ifmedia_init(&sc->sc_media, 0, mb86960_mediachange, 258 mb86960_mediastatus); 259 if (media != NULL) { 260 for (i = 0; i < nmedia; i++) 261 ifmedia_add(&sc->sc_media, media[i], 0, NULL); 262 ifmedia_set(&sc->sc_media, defmedia); 263 } else { 264 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 265 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL); 266 } 267 268 /* Attach the interface. */ 269 if_attach(ifp); 270 ether_ifattach(ifp, sc->sc_enaddr); 271 272 #if NRND > 0 273 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname, 274 RND_TYPE_NET, 0); 275 #endif 276 /* Print additional info when attached. */ 277 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname, 278 ether_sprintf(sc->sc_enaddr)); 279 280 #if FE_DEBUG >= 3 281 { 282 int buf, txb, bbw, sbw, ram; 283 284 buf = txb = bbw = sbw = ram = -1; 285 switch (sc->proto_dlcr6 & FE_D6_BUFSIZ) { 286 case FE_D6_BUFSIZ_8KB: 287 buf = 8; 288 break; 289 case FE_D6_BUFSIZ_16KB: 290 buf = 16; 291 break; 292 case FE_D6_BUFSIZ_32KB: 293 buf = 32; 294 break; 295 case FE_D6_BUFSIZ_64KB: 296 buf = 64; 297 break; 298 } 299 switch (sc->proto_dlcr6 & FE_D6_TXBSIZ) { 300 case FE_D6_TXBSIZ_2x2KB: 301 txb = 2; 302 break; 303 case FE_D6_TXBSIZ_2x4KB: 304 txb = 4; 305 break; 306 case FE_D6_TXBSIZ_2x8KB: 307 txb = 8; 308 break; 309 } 310 switch (sc->proto_dlcr6 & FE_D6_BBW) { 311 case FE_D6_BBW_BYTE: 312 bbw = 8; 313 break; 314 case FE_D6_BBW_WORD: 315 bbw = 16; 316 break; 317 } 318 switch (sc->proto_dlcr6 & FE_D6_SBW) { 319 case FE_D6_SBW_BYTE: 320 sbw = 8; 321 break; 322 case FE_D6_SBW_WORD: 323 sbw = 16; 324 break; 325 } 326 switch (sc->proto_dlcr6 & FE_D6_SRAM) { 327 case FE_D6_SRAM_100ns: 328 ram = 100; 329 break; 330 case FE_D6_SRAM_150ns: 331 ram = 150; 332 break; 333 } 334 printf("%s: SRAM %dKB %dbit %dns, TXB %dKBx2, %dbit I/O\n", 335 sc->sc_dev.dv_xname, buf, bbw, ram, txb, sbw); 336 } 337 #endif 338 339 /* The attach is successful. */ 340 sc->sc_flags |= FE_FLAGS_ATTACHED; 341 } 342 343 /* 344 * Media change callback. 345 */ 346 int 347 mb86960_mediachange(ifp) 348 struct ifnet *ifp; 349 { 350 struct mb86960_softc *sc = ifp->if_softc; 351 352 if (sc->sc_mediachange) 353 return ((*sc->sc_mediachange)(sc)); 354 return (0); 355 } 356 357 /* 358 * Media status callback. 359 */ 360 void 361 mb86960_mediastatus(ifp, ifmr) 362 struct ifnet *ifp; 363 struct ifmediareq *ifmr; 364 { 365 struct mb86960_softc *sc = ifp->if_softc; 366 367 if ((sc->sc_flags & FE_FLAGS_ENABLED) == 0) { 368 ifmr->ifm_active = IFM_ETHER | IFM_NONE; 369 ifmr->ifm_status = 0; 370 return; 371 } 372 373 if (sc->sc_mediastatus) 374 (*sc->sc_mediastatus)(sc, ifmr); 375 } 376 377 /* 378 * Reset interface. 379 */ 380 void 381 mb86960_reset(sc) 382 struct mb86960_softc *sc; 383 { 384 int s; 385 386 s = splnet(); 387 mb86960_stop(sc); 388 mb86960_init(sc); 389 splx(s); 390 } 391 392 /* 393 * Stop everything on the interface. 394 * 395 * All buffered packets, both transmitting and receiving, 396 * if any, will be lost by stopping the interface. 397 */ 398 void 399 mb86960_stop(sc) 400 struct mb86960_softc *sc; 401 { 402 bus_space_tag_t bst = sc->sc_bst; 403 bus_space_handle_t bsh = sc->sc_bsh; 404 405 #if FE_DEBUG >= 3 406 log(LOG_INFO, "%s: top of mb86960_stop()\n", sc->sc_dev.dv_xname); 407 mb86960_dump(LOG_INFO, sc); 408 #endif 409 410 /* Disable interrupts. */ 411 bus_space_write_1(bst, bsh, FE_DLCR2, 0x00); 412 bus_space_write_1(bst, bsh, FE_DLCR3, 0x00); 413 414 /* Stop interface hardware. */ 415 delay(200); 416 bus_space_write_1(bst, bsh, FE_DLCR6, 417 sc->proto_dlcr6 | FE_D6_DLC_DISABLE); 418 delay(200); 419 420 /* Clear all interrupt status. */ 421 bus_space_write_1(bst, bsh, FE_DLCR0, 0xFF); 422 bus_space_write_1(bst, bsh, FE_DLCR1, 0xFF); 423 424 /* Put the chip in stand-by mode. */ 425 delay(200); 426 bus_space_write_1(bst, bsh, FE_DLCR7, 427 sc->proto_dlcr7 | FE_D7_POWER_DOWN); 428 delay(200); 429 430 /* MAR loading can be delayed. */ 431 sc->filter_change = 0; 432 433 /* Call a hook. */ 434 if (sc->stop_card) 435 (*sc->stop_card)(sc); 436 437 #if FE_DEBUG >= 3 438 log(LOG_INFO, "%s: end of mb86960_stop()\n", sc->sc_dev.dv_xname); 439 mb86960_dump(LOG_INFO, sc); 440 #endif 441 } 442 443 /* 444 * Device timeout/watchdog routine. Entered if the device neglects to 445 * generate an interrupt after a transmit has been started on it. 446 */ 447 void 448 mb86960_watchdog(ifp) 449 struct ifnet *ifp; 450 { 451 struct mb86960_softc *sc = ifp->if_softc; 452 453 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname); 454 #if FE_DEBUG >= 3 455 mb86960_dump(LOG_INFO, sc); 456 #endif 457 458 /* Record how many packets are lost by this accident. */ 459 sc->sc_ec.ec_if.if_oerrors += sc->txb_sched + sc->txb_count; 460 461 mb86960_reset(sc); 462 } 463 464 /* 465 * Drop (skip) a packet from receive buffer in 86960 memory. 466 */ 467 static __inline__ void 468 mb86960_droppacket(sc) 469 struct mb86960_softc *sc; 470 { 471 bus_space_tag_t bst = sc->sc_bst; 472 bus_space_handle_t bsh = sc->sc_bsh; 473 474 bus_space_write_1(bst, bsh, FE_BMPR14, FE_B14_FILTER | FE_B14_SKIP); 475 } 476 477 /* 478 * Initialize device. 479 */ 480 void 481 mb86960_init(sc) 482 struct mb86960_softc *sc; 483 { 484 bus_space_tag_t bst = sc->sc_bst; 485 bus_space_handle_t bsh = sc->sc_bsh; 486 struct ifnet *ifp = &sc->sc_ec.ec_if; 487 int i; 488 489 #if FE_DEBUG >= 3 490 log(LOG_INFO, "%s: top of mb86960_init()\n", sc->sc_dev.dv_xname); 491 mb86960_dump(LOG_INFO, sc); 492 #endif 493 494 /* Reset transmitter flags. */ 495 ifp->if_flags &= ~IFF_OACTIVE; 496 ifp->if_timer = 0; 497 498 sc->txb_free = sc->txb_size; 499 sc->txb_count = 0; 500 sc->txb_sched = 0; 501 502 /* Do any card-specific initialization, if applicable. */ 503 if (sc->init_card) 504 (*sc->init_card)(sc); 505 506 #if FE_DEBUG >= 3 507 log(LOG_INFO, "%s: after init hook\n", sc->sc_dev.dv_xname); 508 mb86960_dump(LOG_INFO, sc); 509 #endif 510 511 /* 512 * Make sure to disable the chip, also. 513 * This may also help re-programming the chip after 514 * hot insertion of PCMCIAs. 515 */ 516 bus_space_write_1(bst, bsh, FE_DLCR6, 517 sc->proto_dlcr6 | FE_D6_DLC_DISABLE); 518 delay(200); 519 520 /* Power up the chip and select register bank for DLCRs. */ 521 bus_space_write_1(bst, bsh, FE_DLCR7, 522 sc->proto_dlcr7 | FE_D7_RBS_DLCR | FE_D7_POWER_UP); 523 delay(200); 524 525 /* Feed the station address. */ 526 bus_space_write_region_1(bst, bsh, FE_DLCR8, 527 sc->sc_enaddr, ETHER_ADDR_LEN); 528 529 /* Select the BMPR bank for runtime register access. */ 530 bus_space_write_1(bst, bsh, FE_DLCR7, 531 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP); 532 533 /* Initialize registers. */ 534 bus_space_write_1(bst, bsh, FE_DLCR0, 0xFF); /* Clear all bits. */ 535 bus_space_write_1(bst, bsh, FE_DLCR1, 0xFF); /* ditto. */ 536 bus_space_write_1(bst, bsh, FE_DLCR2, 0x00); 537 bus_space_write_1(bst, bsh, FE_DLCR3, 0x00); 538 bus_space_write_1(bst, bsh, FE_DLCR4, sc->proto_dlcr4); 539 bus_space_write_1(bst, bsh, FE_DLCR5, sc->proto_dlcr5); 540 bus_space_write_1(bst, bsh, FE_BMPR10, 0x00); 541 bus_space_write_1(bst, bsh, FE_BMPR11, FE_B11_CTRL_SKIP); 542 bus_space_write_1(bst, bsh, FE_BMPR12, 0x00); 543 bus_space_write_1(bst, bsh, FE_BMPR13, sc->proto_bmpr13); 544 bus_space_write_1(bst, bsh, FE_BMPR14, FE_B14_FILTER); 545 bus_space_write_1(bst, bsh, FE_BMPR15, 0x00); 546 547 #if FE_DEBUG >= 3 548 log(LOG_INFO, "%s: just before enabling DLC\n", sc->sc_dev.dv_xname); 549 mb86960_dump(LOG_INFO, sc); 550 #endif 551 552 /* Enable interrupts. */ 553 bus_space_write_1(bst, bsh, FE_DLCR2, FE_TMASK); 554 bus_space_write_1(bst, bsh, FE_DLCR3, FE_RMASK); 555 556 /* Enable transmitter and receiver. */ 557 delay(200); 558 bus_space_write_1(bst, bsh, FE_DLCR6, 559 sc->proto_dlcr6 | FE_D6_DLC_ENABLE); 560 delay(200); 561 562 #if FE_DEBUG >= 3 563 log(LOG_INFO, "%s: just after enabling DLC\n", sc->sc_dev.dv_xname); 564 mb86960_dump(LOG_INFO, sc); 565 #endif 566 567 /* 568 * Make sure to empty the receive buffer. 569 * 570 * This may be redundant, but *if* the receive buffer were full 571 * at this point, the driver would hang. I have experienced 572 * some strange hangups just after UP. I hope the following 573 * code solve the problem. 574 * 575 * I have changed the order of hardware initialization. 576 * I think the receive buffer cannot have any packets at this 577 * point in this version. The following code *must* be 578 * redundant now. FIXME. 579 */ 580 for (i = 0; i < FE_MAX_RECV_COUNT; i++) { 581 if (bus_space_read_1(bst, bsh, FE_DLCR5) & FE_D5_BUFEMP) 582 break; 583 mb86960_droppacket(sc); 584 } 585 #if FE_DEBUG >= 1 586 if (i >= FE_MAX_RECV_COUNT) 587 log(LOG_ERR, "%s: cannot empty receive buffer\n", 588 sc->sc_dev.dv_xname); 589 #endif 590 #if FE_DEBUG >= 3 591 if (i < FE_MAX_RECV_COUNT) 592 log(LOG_INFO, "%s: receive buffer emptied (%d)\n", 593 sc->sc_dev.dv_xname, i); 594 #endif 595 596 #if FE_DEBUG >= 3 597 log(LOG_INFO, "%s: after ERB loop\n", sc->sc_dev.dv_xname); 598 mb86960_dump(LOG_INFO, sc); 599 #endif 600 601 /* Do we need this here? */ 602 bus_space_write_1(bst, bsh, FE_DLCR0, 0xFF); /* Clear all bits. */ 603 bus_space_write_1(bst, bsh, FE_DLCR1, 0xFF); /* ditto. */ 604 605 #if FE_DEBUG >= 3 606 log(LOG_INFO, "%s: after FIXME\n", sc->sc_dev.dv_xname); 607 mb86960_dump(LOG_INFO, sc); 608 #endif 609 610 /* Set 'running' flag. */ 611 ifp->if_flags |= IFF_RUNNING; 612 613 /* 614 * At this point, the interface is runnung properly, 615 * except that it receives *no* packets. we then call 616 * mb86960_setmode() to tell the chip what packets to be 617 * received, based on the if_flags and multicast group 618 * list. It completes the initialization process. 619 */ 620 mb86960_setmode(sc); 621 622 #if FE_DEBUG >= 3 623 log(LOG_INFO, "%s: after setmode\n", sc->sc_dev.dv_xname); 624 mb86960_dump(LOG_INFO, sc); 625 #endif 626 627 /* ...and attempt to start output. */ 628 mb86960_start(ifp); 629 630 #if FE_DEBUG >= 3 631 log(LOG_INFO, "%s: end of mb86960_init()\n", sc->sc_dev.dv_xname); 632 mb86960_dump(LOG_INFO, sc); 633 #endif 634 } 635 636 /* 637 * This routine actually starts the transmission on the interface 638 */ 639 static __inline__ void 640 mb86960_xmit(sc) 641 struct mb86960_softc *sc; 642 { 643 bus_space_tag_t bst = sc->sc_bst; 644 bus_space_handle_t bsh = sc->sc_bsh; 645 646 /* 647 * Set a timer just in case we never hear from the board again. 648 * We use longer timeout for multiple packet transmission. 649 * I'm not sure this timer value is appropriate. FIXME. 650 */ 651 sc->sc_ec.ec_if.if_timer = 1 + sc->txb_count; 652 653 /* Update txb variables. */ 654 sc->txb_sched = sc->txb_count; 655 sc->txb_count = 0; 656 sc->txb_free = sc->txb_size; 657 658 #if FE_DELAYED_PADDING 659 /* Omit the postponed padding process. */ 660 sc->txb_padding = 0; 661 #endif 662 663 /* Start transmitter, passing packets in TX buffer. */ 664 bus_space_write_1(bst, bsh, FE_BMPR10, sc->txb_sched | FE_B10_START); 665 } 666 667 /* 668 * Start output on interface. 669 * We make two assumptions here: 670 * 1) that the current priority is set to splnet _before_ this code 671 * is called *and* is returned to the appropriate priority after 672 * return 673 * 2) that the IFF_OACTIVE flag is checked before this code is called 674 * (i.e. that the output part of the interface is idle) 675 */ 676 void 677 mb86960_start(ifp) 678 struct ifnet *ifp; 679 { 680 struct mb86960_softc *sc = ifp->if_softc; 681 struct mbuf *m; 682 683 #if FE_DEBUG >= 1 684 /* Just a sanity check. */ 685 if ((sc->txb_count == 0) != (sc->txb_free == sc->txb_size)) { 686 /* 687 * Txb_count and txb_free co-works to manage the 688 * transmission buffer. Txb_count keeps track of the 689 * used potion of the buffer, while txb_free does unused 690 * potion. So, as long as the driver runs properly, 691 * txb_count is zero if and only if txb_free is same 692 * as txb_size (which represents whole buffer.) 693 */ 694 log(LOG_ERR, "%s: inconsistent txb variables (%d, %d)\n", 695 sc->sc_dev.dv_xname, sc->txb_count, sc->txb_free); 696 /* 697 * So, what should I do, then? 698 * 699 * We now know txb_count and txb_free contradicts. We 700 * cannot, however, tell which is wrong. More 701 * over, we cannot peek 86960 transmission buffer or 702 * reset the transmission buffer. (In fact, we can 703 * reset the entire interface. I don't want to do it.) 704 * 705 * If txb_count is incorrect, leaving it as is will cause 706 * sending of gabages after next interrupt. We have to 707 * avoid it. Hence, we reset the txb_count here. If 708 * txb_free was incorrect, resetting txb_count just loose 709 * some packets. We can live with it. 710 */ 711 sc->txb_count = 0; 712 } 713 #endif 714 715 #if FE_DEBUG >= 1 716 /* 717 * First, see if there are buffered packets and an idle 718 * transmitter - should never happen at this point. 719 */ 720 if ((sc->txb_count > 0) && (sc->txb_sched == 0)) { 721 log(LOG_ERR, "%s: transmitter idle with %d buffered packets\n", 722 sc->sc_dev.dv_xname, sc->txb_count); 723 mb86960_xmit(sc); 724 } 725 #endif 726 727 /* 728 * Stop accepting more transmission packets temporarily, when 729 * a filter change request is delayed. Updating the MARs on 730 * 86960 flushes the transmisstion buffer, so it is delayed 731 * until all buffered transmission packets have been sent 732 * out. 733 */ 734 if (sc->filter_change) { 735 /* 736 * Filter change requst is delayed only when the DLC is 737 * working. DLC soon raise an interrupt after finishing 738 * the work. 739 */ 740 goto indicate_active; 741 } 742 743 for (;;) { 744 /* 745 * See if there is room to put another packet in the buffer. 746 * We *could* do better job by peeking the send queue to 747 * know the length of the next packet. Current version just 748 * tests against the worst case (i.e., longest packet). FIXME. 749 * 750 * When adding the packet-peek feature, don't forget adding a 751 * test on txb_count against QUEUEING_MAX. 752 * There is a little chance the packet count exceeds 753 * the limit. Assume transmission buffer is 8KB (2x8KB 754 * configuration) and an application sends a bunch of small 755 * (i.e., minimum packet sized) packets rapidly. An 8KB 756 * buffer can hold 130 blocks of 62 bytes long... 757 */ 758 if (sc->txb_free < 759 (ETHER_MAX_LEN - ETHER_CRC_LEN) + FE_DATA_LEN_LEN) { 760 /* No room. */ 761 goto indicate_active; 762 } 763 764 #if FE_SINGLE_TRANSMISSION 765 if (sc->txb_count > 0) { 766 /* Just one packet per a transmission buffer. */ 767 goto indicate_active; 768 } 769 #endif 770 771 /* 772 * Get the next mbuf chain for a packet to send. 773 */ 774 IFQ_DEQUEUE(&ifp->if_snd, m); 775 if (m == 0) { 776 /* No more packets to send. */ 777 goto indicate_inactive; 778 } 779 780 #if NBPFILTER > 0 781 /* Tap off here if there is a BPF listener. */ 782 if (ifp->if_bpf) 783 bpf_mtap(ifp->if_bpf, m); 784 #endif 785 786 /* 787 * Copy the mbuf chain into the transmission buffer. 788 * txb_* variables are updated as necessary. 789 */ 790 mb86960_write_mbufs(sc, m); 791 792 m_freem(m); 793 794 /* Start transmitter if it's idle. */ 795 if (sc->txb_sched == 0) 796 mb86960_xmit(sc); 797 } 798 799 indicate_inactive: 800 /* 801 * We are using the !OACTIVE flag to indicate to 802 * the outside world that we can accept an 803 * additional packet rather than that the 804 * transmitter is _actually_ active. Indeed, the 805 * transmitter may be active, but if we haven't 806 * filled all the buffers with data then we still 807 * want to accept more. 808 */ 809 ifp->if_flags &= ~IFF_OACTIVE; 810 return; 811 812 indicate_active: 813 /* 814 * The transmitter is active, and there are no room for 815 * more outgoing packets in the transmission buffer. 816 */ 817 ifp->if_flags |= IFF_OACTIVE; 818 return; 819 } 820 821 /* 822 * Transmission interrupt handler 823 * The control flow of this function looks silly. FIXME. 824 */ 825 void 826 mb86960_tint(sc, tstat) 827 struct mb86960_softc *sc; 828 u_char tstat; 829 { 830 bus_space_tag_t bst = sc->sc_bst; 831 bus_space_handle_t bsh = sc->sc_bsh; 832 struct ifnet *ifp = &sc->sc_ec.ec_if; 833 int left; 834 int col; 835 836 /* 837 * Handle "excessive collision" interrupt. 838 */ 839 if (tstat & FE_D0_COLL16) { 840 /* 841 * Find how many packets (including this collided one) 842 * are left unsent in transmission buffer. 843 */ 844 left = bus_space_read_1(bst, bsh, FE_BMPR10); 845 846 #if FE_DEBUG >= 2 847 log(LOG_WARNING, "%s: excessive collision (%d/%d)\n", 848 sc->sc_dev.dv_xname, left, sc->txb_sched); 849 #endif 850 #if FE_DEBUG >= 3 851 mb86960_dump(LOG_INFO, sc); 852 #endif 853 854 /* 855 * Update statistics. 856 */ 857 ifp->if_collisions += 16; 858 ifp->if_oerrors++; 859 ifp->if_opackets += sc->txb_sched - left; 860 861 /* 862 * Collision statistics has been updated. 863 * Clear the collision flag on 86960 now to avoid confusion. 864 */ 865 bus_space_write_1(bst, bsh, FE_DLCR0, FE_D0_COLLID); 866 867 /* 868 * Restart transmitter, skipping the 869 * collided packet. 870 * 871 * We *must* skip the packet to keep network running 872 * properly. Excessive collision error is an 873 * indication of the network overload. If we 874 * tried sending the same packet after excessive 875 * collision, the network would be filled with 876 * out-of-time packets. Packets belonging 877 * to reliable transport (such as TCP) are resent 878 * by some upper layer. 879 */ 880 bus_space_write_1(bst, bsh, FE_BMPR11, 881 FE_B11_CTRL_SKIP | FE_B11_MODE1); 882 sc->txb_sched = left - 1; 883 } 884 885 /* 886 * Handle "transmission complete" interrupt. 887 */ 888 if (tstat & FE_D0_TXDONE) { 889 /* 890 * Add in total number of collisions on last 891 * transmission. We also clear "collision occurred" flag 892 * here. 893 * 894 * 86960 has a design flow on collision count on multiple 895 * packet transmission. When we send two or more packets 896 * with one start command (that's what we do when the 897 * transmission queue is clauded), 86960 informs us number 898 * of collisions occurred on the last packet on the 899 * transmission only. Number of collisions on previous 900 * packets are lost. I have told that the fact is clearly 901 * stated in the Fujitsu document. 902 * 903 * I considered not to mind it seriously. Collision 904 * count is not so important, anyway. Any comments? FIXME. 905 */ 906 907 if (bus_space_read_1(bst, bsh, FE_DLCR0) & FE_D0_COLLID) { 908 /* Clear collision flag. */ 909 bus_space_write_1(bst, bsh, FE_DLCR0, FE_D0_COLLID); 910 911 /* Extract collision count from 86960. */ 912 col = bus_space_read_1(bst, bsh, FE_DLCR4) & FE_D4_COL; 913 if (col == 0) { 914 /* 915 * Status register indicates collisions, 916 * while the collision count is zero. 917 * This can happen after multiple packet 918 * transmission, indicating that one or more 919 * previous packet(s) had been collided. 920 * 921 * Since the accurate number of collisions 922 * has been lost, we just guess it as 1; 923 * Am I too optimistic? FIXME. 924 */ 925 col = 1; 926 } else 927 col >>= FE_D4_COL_SHIFT; 928 ifp->if_collisions += col; 929 #if FE_DEBUG >= 4 930 log(LOG_WARNING, "%s: %d collision%s (%d)\n", 931 sc->sc_dev.dv_xname, col, col == 1 ? "" : "s", 932 sc->txb_sched); 933 #endif 934 } 935 936 /* 937 * Update total number of successfully 938 * transmitted packets. 939 */ 940 ifp->if_opackets += sc->txb_sched; 941 sc->txb_sched = 0; 942 } 943 944 if (sc->txb_sched == 0) { 945 /* 946 * The transmitter is no more active. 947 * Reset output active flag and watchdog timer. 948 */ 949 ifp->if_flags &= ~IFF_OACTIVE; 950 ifp->if_timer = 0; 951 952 /* 953 * If more data is ready to transmit in the buffer, start 954 * transmitting them. Otherwise keep transmitter idle, 955 * even if more data is queued. This gives receive 956 * process a slight priority. 957 */ 958 if (sc->txb_count > 0) 959 mb86960_xmit(sc); 960 } 961 } 962 963 /* 964 * Ethernet interface receiver interrupt. 965 */ 966 void 967 mb86960_rint(sc, rstat) 968 struct mb86960_softc *sc; 969 u_char rstat; 970 { 971 bus_space_tag_t bst = sc->sc_bst; 972 bus_space_handle_t bsh = sc->sc_bsh; 973 struct ifnet *ifp = &sc->sc_ec.ec_if; 974 int len; 975 u_char status; 976 int i; 977 978 /* 979 * Update statistics if this interrupt is caused by an error. 980 */ 981 if (rstat & (FE_D1_OVRFLO | FE_D1_CRCERR | FE_D1_ALGERR | 982 FE_D1_SRTPKT)) { 983 #if FE_DEBUG >= 3 984 char sbuf[sizeof(FE_D1_ERRBITS) + 64]; 985 986 bitmask_snprintf(rstat, FE_D1_ERRBITS, sbuf, sizeof(sbuf)); 987 log(LOG_WARNING, "%s: receive error: %s\n", 988 sc->sc_dev.dv_xname, sbuf); 989 #endif 990 ifp->if_ierrors++; 991 } 992 993 /* 994 * MB86960 has a flag indicating "receive queue empty." 995 * We just loop cheking the flag to pull out all received 996 * packets. 997 * 998 * We limit the number of iterrations to avoid infinite loop. 999 * It can be caused by a very slow CPU (some broken 1000 * peripheral may insert incredible number of wait cycles) 1001 * or, worse, by a broken MB86960 chip. 1002 */ 1003 for (i = 0; i < FE_MAX_RECV_COUNT; i++) { 1004 /* Stop the iterration if 86960 indicates no packets. */ 1005 if (bus_space_read_1(bst, bsh, FE_DLCR5) & FE_D5_BUFEMP) 1006 break; 1007 1008 /* 1009 * Extract A receive status byte. 1010 * As our 86960 is in 16 bit bus access mode, we have to 1011 * use inw() to get the status byte. The significant 1012 * value is returned in lower 8 bits. 1013 */ 1014 status = (u_char)bus_space_read_2(bst, bsh, FE_BMPR8); 1015 #if FE_DEBUG >= 4 1016 log(LOG_INFO, "%s: receive status = %02x\n", 1017 sc->sc_dev.dv_xname, status); 1018 #endif 1019 1020 /* 1021 * If there was an error, update statistics and drop 1022 * the packet, unless the interface is in promiscuous 1023 * mode. 1024 */ 1025 if ((status & 0xF0) != 0x20) { /* XXXX ? */ 1026 if ((ifp->if_flags & IFF_PROMISC) == 0) { 1027 ifp->if_ierrors++; 1028 mb86960_droppacket(sc); 1029 continue; 1030 } 1031 } 1032 1033 /* 1034 * Extract the packet length. 1035 * It is a sum of a header (14 bytes) and a payload. 1036 * CRC has been stripped off by the 86960. 1037 */ 1038 len = bus_space_read_2(bst, bsh, FE_BMPR8); 1039 1040 /* 1041 * MB86965 checks the packet length and drop big packet 1042 * before passing it to us. There are no chance we can 1043 * get [crufty] packets. Hence, if the length exceeds 1044 * the specified limit, it means some serious failure, 1045 * such as out-of-sync on receive buffer management. 1046 * 1047 * Is this statement true? FIXME. 1048 */ 1049 if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN) || 1050 len < ETHER_HDR_LEN) { 1051 #if FE_DEBUG >= 2 1052 log(LOG_WARNING, 1053 "%s: received a %s packet? (%u bytes)\n", 1054 sc->sc_dev.dv_xname, 1055 len < ETHER_HDR_LEN ? "partial" : "big", len); 1056 #endif 1057 ifp->if_ierrors++; 1058 mb86960_droppacket(sc); 1059 continue; 1060 } 1061 1062 /* 1063 * Check for a short (RUNT) packet. We *do* check 1064 * but do nothing other than print a message. 1065 * Short packets are illegal, but does nothing bad 1066 * if it carries data for upper layer. 1067 */ 1068 #if FE_DEBUG >= 2 1069 if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN)) { 1070 log(LOG_WARNING, 1071 "%s: received a short packet? (%u bytes)\n", 1072 sc->sc_dev.dv_xname, len); 1073 } 1074 #endif 1075 1076 /* 1077 * Go get a packet. 1078 */ 1079 if (!mb86960_get_packet(sc, len)) { 1080 /* Skip a packet, updating statistics. */ 1081 #if FE_DEBUG >= 2 1082 log(LOG_WARNING, 1083 "%s: out of mbufs; dropping packet (%u bytes)\n", 1084 sc->sc_dev.dv_xname, len); 1085 #endif 1086 ifp->if_ierrors++; 1087 mb86960_droppacket(sc); 1088 1089 /* 1090 * We stop receiving packets, even if there are 1091 * more in the buffer. We hope we can get more 1092 * mbufs next time. 1093 */ 1094 return; 1095 } 1096 1097 /* Successfully received a packet. Update stat. */ 1098 ifp->if_ipackets++; 1099 } 1100 } 1101 1102 /* 1103 * Ethernet interface interrupt processor 1104 */ 1105 int 1106 mb86960_intr(arg) 1107 void *arg; 1108 { 1109 struct mb86960_softc *sc = arg; 1110 bus_space_tag_t bst = sc->sc_bst; 1111 bus_space_handle_t bsh = sc->sc_bsh; 1112 struct ifnet *ifp = &sc->sc_ec.ec_if; 1113 u_char tstat, rstat; 1114 1115 if ((sc->sc_flags & FE_FLAGS_ENABLED) == 0 || 1116 (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0) 1117 return (0); 1118 1119 #if FE_DEBUG >= 4 1120 log(LOG_INFO, "%s: mb86960_intr()\n", sc->sc_dev.dv_xname); 1121 mb86960_dump(LOG_INFO, sc); 1122 #endif 1123 1124 /* 1125 * Get interrupt conditions, masking unneeded flags. 1126 */ 1127 tstat = bus_space_read_1(bst, bsh, FE_DLCR0) & FE_TMASK; 1128 rstat = bus_space_read_1(bst, bsh, FE_DLCR1) & FE_RMASK; 1129 if (tstat == 0 && rstat == 0) 1130 return (0); 1131 1132 /* 1133 * Loop until there are no more new interrupt conditions. 1134 */ 1135 for (;;) { 1136 /* 1137 * Reset the conditions we are acknowledging. 1138 */ 1139 bus_space_write_1(bst, bsh, FE_DLCR0, tstat); 1140 bus_space_write_1(bst, bsh, FE_DLCR1, rstat); 1141 1142 /* 1143 * Handle transmitter interrupts. Handle these first because 1144 * the receiver will reset the board under some conditions. 1145 */ 1146 if (tstat != 0) 1147 mb86960_tint(sc, tstat); 1148 1149 /* 1150 * Handle receiver interrupts. 1151 */ 1152 if (rstat != 0) 1153 mb86960_rint(sc, rstat); 1154 1155 /* 1156 * Update the multicast address filter if it is 1157 * needed and possible. We do it now, because 1158 * we can make sure the transmission buffer is empty, 1159 * and there is a good chance that the receive queue 1160 * is empty. It will minimize the possibility of 1161 * packet lossage. 1162 */ 1163 if (sc->filter_change && 1164 sc->txb_count == 0 && sc->txb_sched == 0) { 1165 mb86960_loadmar(sc); 1166 ifp->if_flags &= ~IFF_OACTIVE; 1167 } 1168 1169 /* 1170 * If it looks like the transmitter can take more data, 1171 * attempt to start output on the interface. This is done 1172 * after handling the receiver interrupt to give the 1173 * receive operation priority. 1174 */ 1175 if ((ifp->if_flags & IFF_OACTIVE) == 0) 1176 mb86960_start(ifp); 1177 1178 #if NRND > 0 1179 if (rstat != 0 || tstat != 0) 1180 rnd_add_uint32(&sc->rnd_source, rstat + tstat); 1181 #endif 1182 1183 /* 1184 * Get interrupt conditions, masking unneeded flags. 1185 */ 1186 tstat = bus_space_read_1(bst, bsh, FE_DLCR0) & FE_TMASK; 1187 rstat = bus_space_read_1(bst, bsh, FE_DLCR1) & FE_RMASK; 1188 if (tstat == 0 && rstat == 0) 1189 return (1); 1190 } 1191 } 1192 1193 /* 1194 * Process an ioctl request. This code needs some work - it looks pretty ugly. 1195 */ 1196 int 1197 mb86960_ioctl(ifp, cmd, data) 1198 struct ifnet *ifp; 1199 u_long cmd; 1200 caddr_t data; 1201 { 1202 struct mb86960_softc *sc = ifp->if_softc; 1203 struct ifaddr *ifa = (struct ifaddr *)data; 1204 struct ifreq *ifr = (struct ifreq *)data; 1205 int s, error = 0; 1206 1207 #if FE_DEBUG >= 3 1208 log(LOG_INFO, "%s: ioctl(%lx)\n", sc->sc_dev.dv_xname, cmd); 1209 #endif 1210 1211 s = splnet(); 1212 1213 switch (cmd) { 1214 case SIOCSIFADDR: 1215 if ((error = mb86960_enable(sc)) != 0) 1216 break; 1217 ifp->if_flags |= IFF_UP; 1218 1219 switch (ifa->ifa_addr->sa_family) { 1220 #ifdef INET 1221 case AF_INET: 1222 mb86960_init(sc); 1223 arp_ifinit(ifp, ifa); 1224 break; 1225 #endif 1226 #ifdef NS 1227 case AF_NS: 1228 { 1229 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr; 1230 1231 if (ns_nullhost(*ina)) 1232 ina->x_host = 1233 *(union ns_host *)LLADDR(ifp->if_sadl); 1234 else { 1235 memcpy(LLADDR(ifp->if_sadl), 1236 ina->x_host.c_host, ETHER_ADDR_LEN); 1237 } 1238 /* Set new address. */ 1239 mb86960_init(sc); 1240 break; 1241 } 1242 #endif 1243 default: 1244 mb86960_init(sc); 1245 break; 1246 } 1247 break; 1248 1249 case SIOCSIFFLAGS: 1250 if ((ifp->if_flags & IFF_UP) == 0 && 1251 (ifp->if_flags & IFF_RUNNING) != 0) { 1252 /* 1253 * If interface is marked down and it is running, then 1254 * stop it. 1255 */ 1256 mb86960_stop(sc); 1257 ifp->if_flags &= ~IFF_RUNNING; 1258 mb86960_disable(sc); 1259 } else if ((ifp->if_flags & IFF_UP) != 0 && 1260 (ifp->if_flags & IFF_RUNNING) == 0) { 1261 /* 1262 * If interface is marked up and it is stopped, then 1263 * start it. 1264 */ 1265 if ((error = mb86960_enable(sc)) != 0) 1266 break; 1267 mb86960_init(sc); 1268 } else if ((ifp->if_flags & IFF_UP) != 0) { 1269 /* 1270 * Reset the interface to pick up changes in any other 1271 * flags that affect hardware registers. 1272 */ 1273 mb86960_setmode(sc); 1274 } 1275 #if FE_DEBUG >= 1 1276 /* "ifconfig fe0 debug" to print register dump. */ 1277 if (ifp->if_flags & IFF_DEBUG) { 1278 log(LOG_INFO, "%s: SIOCSIFFLAGS(DEBUG)\n", 1279 sc->sc_dev.dv_xname); 1280 mb86960_dump(LOG_DEBUG, sc); 1281 } 1282 #endif 1283 break; 1284 1285 case SIOCADDMULTI: 1286 case SIOCDELMULTI: 1287 if ((sc->sc_flags & FE_FLAGS_ENABLED) == 0) { 1288 error = EIO; 1289 break; 1290 } 1291 1292 /* Update our multicast list. */ 1293 error = (cmd == SIOCADDMULTI) ? 1294 ether_addmulti(ifr, &sc->sc_ec) : 1295 ether_delmulti(ifr, &sc->sc_ec); 1296 1297 if (error == ENETRESET) { 1298 /* 1299 * Multicast list has changed; set the hardware filter 1300 * accordingly. 1301 */ 1302 mb86960_setmode(sc); 1303 error = 0; 1304 } 1305 break; 1306 1307 case SIOCGIFMEDIA: 1308 case SIOCSIFMEDIA: 1309 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 1310 break; 1311 1312 default: 1313 error = EINVAL; 1314 break; 1315 } 1316 1317 splx(s); 1318 return (error); 1319 } 1320 1321 /* 1322 * Retrieve packet from receive buffer and send to the next level up via 1323 * ether_input(). If there is a BPF listener, give a copy to BPF, too. 1324 * Returns 0 if success, -1 if error (i.e., mbuf allocation failure). 1325 */ 1326 int 1327 mb86960_get_packet(sc, len) 1328 struct mb86960_softc *sc; 1329 int len; 1330 { 1331 bus_space_tag_t bst = sc->sc_bst; 1332 bus_space_handle_t bsh = sc->sc_bsh; 1333 struct ifnet *ifp = &sc->sc_ec.ec_if; 1334 struct mbuf *m; 1335 1336 /* Allocate a header mbuf. */ 1337 MGETHDR(m, M_DONTWAIT, MT_DATA); 1338 if (m == 0) 1339 return (0); 1340 m->m_pkthdr.rcvif = ifp; 1341 m->m_pkthdr.len = len; 1342 1343 /* The following silliness is to make NFS happy. */ 1344 #define EROUND ((sizeof(struct ether_header) + 3) & ~3) 1345 #define EOFF (EROUND - sizeof(struct ether_header)) 1346 1347 /* 1348 * Our strategy has one more problem. There is a policy on 1349 * mbuf cluster allocation. It says that we must have at 1350 * least MINCLSIZE (208 bytes) to allocate a cluster. For a 1351 * packet of a size between (MHLEN - 2) to (MINCLSIZE - 2), 1352 * our code violates the rule... 1353 * On the other hand, the current code is short, simle, 1354 * and fast, however. It does no harmful thing, just waists 1355 * some memory. Any comments? FIXME. 1356 */ 1357 1358 /* Attach a cluster if this packet doesn't fit in a normal mbuf. */ 1359 if (len > MHLEN - EOFF) { 1360 MCLGET(m, M_DONTWAIT); 1361 if ((m->m_flags & M_EXT) == 0) { 1362 m_freem(m); 1363 return (0); 1364 } 1365 } 1366 1367 /* 1368 * The following assumes there is room for the ether header in the 1369 * header mbuf. 1370 */ 1371 m->m_data += EOFF; 1372 1373 /* Set the length of this packet. */ 1374 m->m_len = len; 1375 1376 /* Get a packet. */ 1377 bus_space_read_multi_stream_2(bst, bsh, FE_BMPR8, mtod(m, u_int16_t *), 1378 (len + 1) >> 1); 1379 1380 #if NBPFILTER > 0 1381 /* 1382 * Check if there's a BPF listener on this interface. If so, hand off 1383 * the raw packet to bpf. 1384 */ 1385 if (ifp->if_bpf) 1386 bpf_mtap(ifp->if_bpf, m); 1387 #endif 1388 1389 (*ifp->if_input)(ifp, m); 1390 return (1); 1391 } 1392 1393 /* 1394 * Write an mbuf chain to the transmission buffer memory using 16 bit PIO. 1395 * Returns number of bytes actually written, including length word. 1396 * 1397 * If an mbuf chain is too long for an Ethernet frame, it is not sent. 1398 * Packets shorter than Ethernet minimum are legal, and we pad them 1399 * before sending out. An exception is "partial" packets which are 1400 * shorter than mandatory Ethernet header. 1401 * 1402 * I wrote a code for an experimental "delayed padding" technique. 1403 * When employed, it postpones the padding process for short packets. 1404 * If xmit() occurred at the moment, the padding process is omitted, and 1405 * garbages are sent as pad data. If next packet is stored in the 1406 * transmission buffer before xmit(), write_mbuf() pads the previous 1407 * packet before transmitting new packet. This *may* gain the 1408 * system performance (slightly). 1409 */ 1410 void 1411 mb86960_write_mbufs(sc, m) 1412 struct mb86960_softc *sc; 1413 struct mbuf *m; 1414 { 1415 bus_space_tag_t bst = sc->sc_bst; 1416 bus_space_handle_t bsh = sc->sc_bsh; 1417 u_char *data; 1418 u_short savebyte; /* WARNING: Architecture dependent! */ 1419 int totlen, len, wantbyte; 1420 #if FE_DEBUG >= 2 1421 struct mbuf *mp; 1422 #endif 1423 1424 /* XXX thorpej 960116 - quiet bogus compiler warning. */ 1425 savebyte = 0; 1426 1427 #if FE_DELAYED_PADDING 1428 /* Do the "delayed padding." */ 1429 len = sc->txb_padding >> 1; 1430 if (len > 0) { 1431 while (--len >= 0) 1432 bus_space_write_2(bst, bsh, FE_BMPR8, 0); 1433 sc->txb_padding = 0; 1434 } 1435 #endif 1436 1437 /* We need to use m->m_pkthdr.len, so require the header */ 1438 if ((m->m_flags & M_PKTHDR) == 0) 1439 panic("mb86960_write_mbufs: no header mbuf"); 1440 1441 #if FE_DEBUG >= 2 1442 /* First, count up the total number of bytes to copy. */ 1443 for (totlen = 0, mp = m; mp != 0; mp = mp->m_next) 1444 totlen += mp->m_len; 1445 /* Check if this matches the one in the packet header. */ 1446 if (totlen != m->m_pkthdr.len) 1447 log(LOG_WARNING, "%s: packet length mismatch? (%d/%d)\n", 1448 sc->sc_dev.dv_xname, totlen, m->m_pkthdr.len); 1449 #else 1450 /* Just use the length value in the packet header. */ 1451 totlen = m->m_pkthdr.len; 1452 #endif 1453 1454 #if FE_DEBUG >= 1 1455 /* 1456 * Should never send big packets. If such a packet is passed, 1457 * it should be a bug of upper layer. We just ignore it. 1458 * ... Partial (too short) packets, neither. 1459 */ 1460 if (totlen > (ETHER_MAX_LEN - ETHER_CRC_LEN) || 1461 totlen < ETHER_HDR_LEN) { 1462 log(LOG_ERR, "%s: got a %s packet (%u bytes) to send\n", 1463 sc->sc_dev.dv_xname, 1464 totlen < ETHER_HDR_LEN ? "partial" : "big", totlen); 1465 sc->sc_ec.ec_if.if_oerrors++; 1466 return; 1467 } 1468 #endif 1469 1470 /* 1471 * Put the length word for this frame. 1472 * Does 86960 accept odd length? -- Yes. 1473 * Do we need to pad the length to minimum size by ourselves? 1474 * -- Generally yes. But for (or will be) the last 1475 * packet in the transmission buffer, we can skip the 1476 * padding process. It may gain performance slightly. FIXME. 1477 */ 1478 bus_space_write_2(bst, bsh, FE_BMPR8, 1479 max(totlen, (ETHER_MIN_LEN - ETHER_CRC_LEN))); 1480 1481 /* 1482 * Update buffer status now. 1483 * Truncate the length up to an even number, since we use outw(). 1484 */ 1485 totlen = (totlen + 1) & ~1; 1486 sc->txb_free -= FE_DATA_LEN_LEN + 1487 max(totlen, (ETHER_MIN_LEN - ETHER_CRC_LEN)); 1488 sc->txb_count++; 1489 1490 #if FE_DELAYED_PADDING 1491 /* Postpone the packet padding if necessary. */ 1492 if (totlen < (ETHER_MIN_LEN - ETHER_CRC_LEN)) 1493 sc->txb_padding = (ETHER_MIN_LEN - ETHER_CRC_LEN) - totlen; 1494 #endif 1495 1496 /* 1497 * Transfer the data from mbuf chain to the transmission buffer. 1498 * MB86960 seems to require that data be transferred as words, and 1499 * only words. So that we require some extra code to patch 1500 * over odd-length mbufs. 1501 */ 1502 wantbyte = 0; 1503 for (; m != 0; m = m->m_next) { 1504 /* Ignore empty mbuf. */ 1505 len = m->m_len; 1506 if (len == 0) 1507 continue; 1508 1509 /* Find the actual data to send. */ 1510 data = mtod(m, caddr_t); 1511 1512 /* Finish the last byte. */ 1513 if (wantbyte) { 1514 bus_space_write_2(bst, bsh, FE_BMPR8, 1515 savebyte | (*data << 8)); 1516 data++; 1517 len--; 1518 wantbyte = 0; 1519 } 1520 1521 /* Output contiguous words. */ 1522 if (len > 1) 1523 bus_space_write_multi_stream_2(bst, bsh, FE_BMPR8, 1524 (u_int16_t *)data, len >> 1); 1525 1526 /* Save remaining byte, if there is one. */ 1527 if (len & 1) { 1528 data += len & ~1; 1529 savebyte = *data; 1530 wantbyte = 1; 1531 } 1532 } 1533 1534 /* Spit the last byte, if the length is odd. */ 1535 if (wantbyte) 1536 bus_space_write_2(bst, bsh, FE_BMPR8, savebyte); 1537 1538 #if ! FE_DELAYED_PADDING 1539 /* 1540 * Pad the packet to the minimum length if necessary. 1541 */ 1542 len = ((ETHER_MIN_LEN - ETHER_CRC_LEN) >> 1) - (totlen >> 1); 1543 while (--len >= 0) 1544 bus_space_write_2(bst, bsh, FE_BMPR8, 0); 1545 #endif 1546 } 1547 1548 /* 1549 * Compute the multicast address filter from the 1550 * list of multicast addresses we need to listen to. 1551 */ 1552 void 1553 mb86960_getmcaf(ec, af) 1554 struct ethercom *ec; 1555 u_char *af; 1556 { 1557 struct ifnet *ifp = &ec->ec_if; 1558 struct ether_multi *enm; 1559 u_int32_t crc; 1560 struct ether_multistep step; 1561 1562 /* 1563 * Set up multicast address filter by passing all multicast addresses 1564 * through a crc generator, and then using the high order 6 bits as an 1565 * index into the 64 bit logical address filter. The high order bit 1566 * selects the word, while the rest of the bits select the bit within 1567 * the word. 1568 */ 1569 1570 if ((ifp->if_flags & IFF_PROMISC) != 0) 1571 goto allmulti; 1572 1573 af[0] = af[1] = af[2] = af[3] = af[4] = af[5] = af[6] = af[7] = 0x00; 1574 ETHER_FIRST_MULTI(step, ec, enm); 1575 while (enm != NULL) { 1576 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 1577 sizeof(enm->enm_addrlo)) != 0) { 1578 /* 1579 * We must listen to a range of multicast addresses. 1580 * For now, just accept all multicasts, rather than 1581 * trying to set only those filter bits needed to match 1582 * the range. (At this time, the only use of address 1583 * ranges is for IP multicast routing, for which the 1584 * range is big enough to require all bits set.) 1585 */ 1586 goto allmulti; 1587 } 1588 1589 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN); 1590 1591 /* Just want the 6 most significant bits. */ 1592 crc >>= 26; 1593 1594 /* Turn on the corresponding bit in the filter. */ 1595 af[crc >> 3] |= 1 << (crc & 7); 1596 1597 ETHER_NEXT_MULTI(step, enm); 1598 } 1599 ifp->if_flags &= ~IFF_ALLMULTI; 1600 return; 1601 1602 allmulti: 1603 ifp->if_flags |= IFF_ALLMULTI; 1604 af[0] = af[1] = af[2] = af[3] = af[4] = af[5] = af[6] = af[7] = 0xff; 1605 } 1606 1607 /* 1608 * Calculate a new "multicast packet filter" and put the 86960 1609 * receiver in appropriate mode. 1610 */ 1611 void 1612 mb86960_setmode(sc) 1613 struct mb86960_softc *sc; 1614 { 1615 bus_space_tag_t bst = sc->sc_bst; 1616 bus_space_handle_t bsh = sc->sc_bsh; 1617 int flags = sc->sc_ec.ec_if.if_flags; 1618 1619 /* 1620 * If the interface is not running, we postpone the update 1621 * process for receive modes and multicast address filter 1622 * until the interface is restarted. It reduces some 1623 * complicated job on maintaining chip states. (Earlier versions 1624 * of this driver had a bug on that point...) 1625 * 1626 * To complete the trick, mb86960_init() calls mb86960_setmode() after 1627 * restarting the interface. 1628 */ 1629 if ((flags & IFF_RUNNING) == 0) 1630 return; 1631 1632 /* 1633 * Promiscuous mode is handled separately. 1634 */ 1635 if ((flags & IFF_PROMISC) != 0) { 1636 /* 1637 * Program 86960 to receive all packets on the segment 1638 * including those directed to other stations. 1639 * Multicast filter stored in MARs are ignored 1640 * under this setting, so we don't need to update it. 1641 * 1642 * Promiscuous mode is used solely by BPF, and BPF only 1643 * listens to valid (no error) packets. So, we ignore 1644 * errornous ones even in this mode. 1645 */ 1646 bus_space_write_1(bst, bsh, FE_DLCR5, 1647 sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1); 1648 sc->filter_change = 0; 1649 1650 #if FE_DEBUG >= 3 1651 log(LOG_INFO, "%s: promiscuous mode\n", sc->sc_dev.dv_xname); 1652 #endif 1653 return; 1654 } 1655 1656 /* 1657 * Turn the chip to the normal (non-promiscuous) mode. 1658 */ 1659 bus_space_write_1(bst, bsh, FE_DLCR5, sc->proto_dlcr5 | FE_D5_AFM1); 1660 1661 /* 1662 * Find the new multicast filter value. 1663 */ 1664 mb86960_getmcaf(&sc->sc_ec, sc->filter); 1665 sc->filter_change = 1; 1666 1667 #if FE_DEBUG >= 3 1668 log(LOG_INFO, 1669 "%s: address filter: [%02x %02x %02x %02x %02x %02x %02x %02x]\n", 1670 sc->sc_dev.dv_xname, 1671 sc->filter[0], sc->filter[1], sc->filter[2], sc->filter[3], 1672 sc->filter[4], sc->filter[5], sc->filter[6], sc->filter[7]); 1673 #endif 1674 1675 /* 1676 * We have to update the multicast filter in the 86960, A.S.A.P. 1677 * 1678 * Note that the DLC (Data Linc Control unit, i.e. transmitter 1679 * and receiver) must be stopped when feeding the filter, and 1680 * DLC trushes all packets in both transmission and receive 1681 * buffers when stopped. 1682 * 1683 * ... Are the above sentenses correct? I have to check the 1684 * manual of the MB86960A. FIXME. 1685 * 1686 * To reduce the packet lossage, we delay the filter update 1687 * process until buffers are empty. 1688 */ 1689 if (sc->txb_sched == 0 && sc->txb_count == 0 && 1690 (bus_space_read_1(bst, bsh, FE_DLCR1) & FE_D1_PKTRDY) == 0) { 1691 /* 1692 * Buffers are (apparently) empty. Load 1693 * the new filter value into MARs now. 1694 */ 1695 mb86960_loadmar(sc); 1696 } else { 1697 /* 1698 * Buffers are not empty. Mark that we have to update 1699 * the MARs. The new filter will be loaded by mb86960_intr() 1700 * later. 1701 */ 1702 #if FE_DEBUG >= 4 1703 log(LOG_INFO, "%s: filter change delayed\n", 1704 sc->sc_dev.dv_xname); 1705 #endif 1706 } 1707 } 1708 1709 /* 1710 * Load a new multicast address filter into MARs. 1711 * 1712 * The caller must have splnet'ed befor mb86960_loadmar. 1713 * This function starts the DLC upon return. So it can be called only 1714 * when the chip is working, i.e., from the driver's point of view, when 1715 * a device is RUNNING. (I mistook the point in previous versions.) 1716 */ 1717 void 1718 mb86960_loadmar(sc) 1719 struct mb86960_softc *sc; 1720 { 1721 bus_space_tag_t bst = sc->sc_bst; 1722 bus_space_handle_t bsh = sc->sc_bsh; 1723 1724 /* Stop the DLC (transmitter and receiver). */ 1725 bus_space_write_1(bst, bsh, FE_DLCR6, 1726 sc->proto_dlcr6 | FE_D6_DLC_DISABLE); 1727 1728 /* Select register bank 1 for MARs. */ 1729 bus_space_write_1(bst, bsh, FE_DLCR7, 1730 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP); 1731 1732 /* Copy filter value into the registers. */ 1733 bus_space_write_region_1(bst, bsh, FE_MAR8, sc->filter, FE_FILTER_LEN); 1734 1735 /* Restore the bank selection for BMPRs (i.e., runtime registers). */ 1736 bus_space_write_1(bst, bsh, FE_DLCR7, 1737 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP); 1738 1739 /* Restart the DLC. */ 1740 bus_space_write_1(bst, bsh, FE_DLCR6, 1741 sc->proto_dlcr6 | FE_D6_DLC_ENABLE); 1742 1743 /* We have just updated the filter. */ 1744 sc->filter_change = 0; 1745 1746 #if FE_DEBUG >= 3 1747 log(LOG_INFO, "%s: address filter changed\n", sc->sc_dev.dv_xname); 1748 #endif 1749 } 1750 1751 /* 1752 * Enable power on the interface. 1753 */ 1754 int 1755 mb86960_enable(sc) 1756 struct mb86960_softc *sc; 1757 { 1758 1759 #if FE_DEBUG >= 3 1760 log(LOG_INFO, "%s: mb86960_enable()\n", sc->sc_dev.dv_xname); 1761 #endif 1762 1763 if ((sc->sc_flags & FE_FLAGS_ENABLED) == 0 && sc->sc_enable != NULL) { 1764 if ((*sc->sc_enable)(sc) != 0) { 1765 printf("%s: device enable failed\n", 1766 sc->sc_dev.dv_xname); 1767 return (EIO); 1768 } 1769 } 1770 1771 sc->sc_flags |= FE_FLAGS_ENABLED; 1772 return (0); 1773 } 1774 1775 /* 1776 * Disable power on the interface. 1777 */ 1778 void 1779 mb86960_disable(sc) 1780 struct mb86960_softc *sc; 1781 { 1782 1783 #if FE_DEBUG >= 3 1784 log(LOG_INFO, "%s: mb86960_disable()\n", sc->sc_dev.dv_xname); 1785 #endif 1786 1787 if ((sc->sc_flags & FE_FLAGS_ENABLED) != 0 && sc->sc_disable != NULL) { 1788 (*sc->sc_disable)(sc); 1789 sc->sc_flags &= ~FE_FLAGS_ENABLED; 1790 } 1791 } 1792 1793 /* 1794 * mbe_activate: 1795 * 1796 * Handle device activation/deactivation requests. 1797 */ 1798 int 1799 mb86960_activate(self, act) 1800 struct device *self; 1801 enum devact act; 1802 { 1803 struct mb86960_softc *sc = (struct mb86960_softc *)self; 1804 int rv = 0, s; 1805 1806 s = splnet(); 1807 switch (act) { 1808 case DVACT_ACTIVATE: 1809 rv = EOPNOTSUPP; 1810 break; 1811 1812 case DVACT_DEACTIVATE: 1813 if_deactivate(&sc->sc_ec.ec_if); 1814 break; 1815 } 1816 splx(s); 1817 return (rv); 1818 } 1819 1820 /* 1821 * mb86960_detach: 1822 * 1823 * Detach a MB86960 interface. 1824 */ 1825 int 1826 mb86960_detach(sc) 1827 struct mb86960_softc *sc; 1828 { 1829 struct ifnet *ifp = &sc->sc_ec.ec_if; 1830 1831 /* Succeed now if there's no work to do. */ 1832 if ((sc->sc_flags & FE_FLAGS_ATTACHED) == 0) 1833 return (0); 1834 1835 /* Delete all media. */ 1836 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY); 1837 1838 #if NRND > 0 1839 /* Unhook the entropy source. */ 1840 rnd_detach_source(&sc->rnd_source); 1841 #endif 1842 ether_ifdetach(ifp); 1843 if_detach(ifp); 1844 1845 mb86960_disable(sc); 1846 return (0); 1847 } 1848 1849 #if FE_DEBUG >= 1 1850 void 1851 mb86960_dump(level, sc) 1852 int level; 1853 struct mb86960_softc *sc; 1854 { 1855 bus_space_tag_t bst = sc->sc_bst; 1856 bus_space_handle_t bsh = sc->sc_bsh; 1857 u_char save_dlcr7; 1858 1859 save_dlcr7 = bus_space_read_1(bst, bsh, FE_DLCR7); 1860 1861 log(level, "\tDLCR = %02x %02x %02x %02x %02x %02x %02x %02x\n", 1862 bus_space_read_1(bst, bsh, FE_DLCR0), 1863 bus_space_read_1(bst, bsh, FE_DLCR1), 1864 bus_space_read_1(bst, bsh, FE_DLCR2), 1865 bus_space_read_1(bst, bsh, FE_DLCR3), 1866 bus_space_read_1(bst, bsh, FE_DLCR4), 1867 bus_space_read_1(bst, bsh, FE_DLCR5), 1868 bus_space_read_1(bst, bsh, FE_DLCR6), 1869 bus_space_read_1(bst, bsh, FE_DLCR7)); 1870 1871 bus_space_write_1(bst, bsh, FE_DLCR7, 1872 (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_DLCR); 1873 log(level, "\t %02x %02x %02x %02x %02x %02x %02x %02x\n", 1874 bus_space_read_1(bst, bsh, FE_DLCR8), 1875 bus_space_read_1(bst, bsh, FE_DLCR9), 1876 bus_space_read_1(bst, bsh, FE_DLCR10), 1877 bus_space_read_1(bst, bsh, FE_DLCR11), 1878 bus_space_read_1(bst, bsh, FE_DLCR12), 1879 bus_space_read_1(bst, bsh, FE_DLCR13), 1880 bus_space_read_1(bst, bsh, FE_DLCR14), 1881 bus_space_read_1(bst, bsh, FE_DLCR15)); 1882 1883 bus_space_write_1(bst, bsh, FE_DLCR7, 1884 (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_MAR); 1885 log(level, "\tMAR = %02x %02x %02x %02x %02x %02x %02x %02x\n", 1886 bus_space_read_1(bst, bsh, FE_MAR8), 1887 bus_space_read_1(bst, bsh, FE_MAR9), 1888 bus_space_read_1(bst, bsh, FE_MAR10), 1889 bus_space_read_1(bst, bsh, FE_MAR11), 1890 bus_space_read_1(bst, bsh, FE_MAR12), 1891 bus_space_read_1(bst, bsh, FE_MAR13), 1892 bus_space_read_1(bst, bsh, FE_MAR14), 1893 bus_space_read_1(bst, bsh, FE_MAR15)); 1894 1895 bus_space_write_1(bst, bsh, FE_DLCR7, 1896 (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_BMPR); 1897 log(level, 1898 "\tBMPR = xx xx %02x %02x %02x %02x %02x %02x %02x %02x xx %02x\n", 1899 bus_space_read_1(bst, bsh, FE_BMPR10), 1900 bus_space_read_1(bst, bsh, FE_BMPR11), 1901 bus_space_read_1(bst, bsh, FE_BMPR12), 1902 bus_space_read_1(bst, bsh, FE_BMPR13), 1903 bus_space_read_1(bst, bsh, FE_BMPR14), 1904 bus_space_read_1(bst, bsh, FE_BMPR15), 1905 bus_space_read_1(bst, bsh, FE_BMPR16), 1906 bus_space_read_1(bst, bsh, FE_BMPR17), 1907 bus_space_read_1(bst, bsh, FE_BMPR19)); 1908 1909 bus_space_write_1(bst, bsh, FE_DLCR7, save_dlcr7); 1910 } 1911 #endif 1912