1 /* $NetBSD: smc91cxx.c,v 1.78 2010/04/05 07:19:37 joerg Exp $ */ 2 3 /*- 4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1996 Gardner Buchanan <gbuchanan@shl.com> 35 * All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by Gardner Buchanan. 48 * 4. The name of Gardner Buchanan may not be used to endorse or promote 49 * products derived from this software without specific prior written 50 * permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 53 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 55 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 56 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 57 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 61 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 62 * 63 * from FreeBSD Id: if_sn.c,v 1.4 1996/03/18 15:47:16 gardner Exp 64 */ 65 66 /* 67 * Core driver for the SMC 91Cxx family of Ethernet chips. 68 * 69 * Memory allocation interrupt logic is drived from an SMC 91C90 driver 70 * written for NetBSD/amiga by Michael Hitch. 71 */ 72 73 #include <sys/cdefs.h> 74 __KERNEL_RCSID(0, "$NetBSD: smc91cxx.c,v 1.78 2010/04/05 07:19:37 joerg Exp $"); 75 76 #include "opt_inet.h" 77 #include "rnd.h" 78 79 #include <sys/param.h> 80 #include <sys/systm.h> 81 #include <sys/mbuf.h> 82 #include <sys/syslog.h> 83 #include <sys/socket.h> 84 #include <sys/device.h> 85 #include <sys/kernel.h> 86 #include <sys/malloc.h> 87 #include <sys/ioctl.h> 88 #include <sys/errno.h> 89 #if NRND > 0 90 #include <sys/rnd.h> 91 #endif 92 93 #include <sys/bus.h> 94 #include <sys/intr.h> 95 96 #include <uvm/uvm_extern.h> 97 98 #include <net/if.h> 99 #include <net/if_dl.h> 100 #include <net/if_ether.h> 101 #include <net/if_media.h> 102 103 #ifdef INET 104 #include <netinet/in.h> 105 #include <netinet/if_inarp.h> 106 #include <netinet/in_systm.h> 107 #include <netinet/in_var.h> 108 #include <netinet/ip.h> 109 #endif 110 111 #include <net/bpf.h> 112 #include <net/bpfdesc.h> 113 114 #include <dev/mii/mii.h> 115 #include <dev/mii/miivar.h> 116 #include <dev/mii/mii_bitbang.h> 117 118 #include <dev/ic/smc91cxxreg.h> 119 #include <dev/ic/smc91cxxvar.h> 120 121 #ifndef __BUS_SPACE_HAS_STREAM_METHODS 122 #define bus_space_write_multi_stream_2 bus_space_write_multi_2 123 #define bus_space_write_multi_stream_4 bus_space_write_multi_4 124 #define bus_space_read_multi_stream_2 bus_space_read_multi_2 125 #define bus_space_read_multi_stream_4 bus_space_read_multi_4 126 127 #define bus_space_write_stream_4 bus_space_write_4 128 #define bus_space_read_stream_4 bus_space_read_4 129 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */ 130 131 /* XXX Hardware padding doesn't work yet(?) */ 132 #define SMC91CXX_SW_PAD 133 134 const char *smc91cxx_idstrs[] = { 135 NULL, /* 0 */ 136 NULL, /* 1 */ 137 NULL, /* 2 */ 138 "SMC91C90/91C92", /* 3 */ 139 "SMC91C94/91C96", /* 4 */ 140 "SMC91C95", /* 5 */ 141 NULL, /* 6 */ 142 "SMC91C100", /* 7 */ 143 "SMC91C100FD", /* 8 */ 144 "SMC91C111", /* 9 */ 145 NULL, /* 10 */ 146 NULL, /* 11 */ 147 NULL, /* 12 */ 148 NULL, /* 13 */ 149 NULL, /* 14 */ 150 NULL, /* 15 */ 151 }; 152 153 /* Supported media types. */ 154 const int smc91cxx_media[] = { 155 IFM_ETHER|IFM_10_T, 156 IFM_ETHER|IFM_10_5, 157 }; 158 #define NSMC91CxxMEDIA (sizeof(smc91cxx_media) / sizeof(smc91cxx_media[0])) 159 160 /* 161 * MII bit-bang glue. 162 */ 163 u_int32_t smc91cxx_mii_bitbang_read(device_t); 164 void smc91cxx_mii_bitbang_write(device_t, u_int32_t); 165 166 const struct mii_bitbang_ops smc91cxx_mii_bitbang_ops = { 167 smc91cxx_mii_bitbang_read, 168 smc91cxx_mii_bitbang_write, 169 { 170 MR_MDO, /* MII_BIT_MDO */ 171 MR_MDI, /* MII_BIT_MDI */ 172 MR_MCLK, /* MII_BIT_MDC */ 173 MR_MDOE, /* MII_BIT_DIR_HOST_PHY */ 174 0, /* MII_BIT_DIR_PHY_HOST */ 175 } 176 }; 177 178 /* MII callbacks */ 179 int smc91cxx_mii_readreg(device_t, int, int); 180 void smc91cxx_mii_writereg(device_t, int, int, int); 181 void smc91cxx_statchg(device_t); 182 void smc91cxx_tick(void *); 183 184 int smc91cxx_mediachange(struct ifnet *); 185 void smc91cxx_mediastatus(struct ifnet *, struct ifmediareq *); 186 187 int smc91cxx_set_media(struct smc91cxx_softc *, int); 188 189 void smc91cxx_init(struct smc91cxx_softc *); 190 void smc91cxx_read(struct smc91cxx_softc *); 191 void smc91cxx_reset(struct smc91cxx_softc *); 192 void smc91cxx_start(struct ifnet *); 193 uint8_t smc91cxx_copy_tx_frame(struct smc91cxx_softc *, struct mbuf *); 194 void smc91cxx_resume(struct smc91cxx_softc *); 195 void smc91cxx_stop(struct smc91cxx_softc *); 196 void smc91cxx_watchdog(struct ifnet *); 197 int smc91cxx_ioctl(struct ifnet *, u_long, void *); 198 199 static inline int ether_cmp(const void *, const void *); 200 static inline int 201 ether_cmp(const void *va, const void *vb) 202 { 203 const u_int8_t *a = va; 204 const u_int8_t *b = vb; 205 206 return ((a[5] != b[5]) || (a[4] != b[4]) || (a[3] != b[3]) || 207 (a[2] != b[2]) || (a[1] != b[1]) || (a[0] != b[0])); 208 } 209 210 static inline void 211 smc91cxx_intr_mask_write(bus_space_tag_t bst, bus_space_handle_t bsh, 212 uint8_t mask) 213 { 214 KDASSERT((mask & IM_ERCV_INT) == 0); 215 #ifdef SMC91CXX_NO_BYTE_WRITE 216 #if BYTE_ORDER == LITTLE_ENDIAN 217 bus_space_write_2(bst, bsh, INTR_STAT_REG_B, mask << 8); 218 #else 219 bus_space_write_2(bst, bsh, INTR_STAT_REG_B, mask); 220 #endif 221 #else 222 bus_space_write_1(bst, bsh, INTR_MASK_REG_B, mask); 223 #endif 224 KDASSERT(!(bus_space_read_1(bst, bsh, INTR_MASK_REG_B) & IM_ERCV_INT)); 225 } 226 227 static inline void 228 smc91cxx_intr_ack_write(bus_space_tag_t bst, bus_space_handle_t bsh, 229 uint8_t mask) 230 { 231 #ifdef SMC91CXX_NO_BYTE_WRITE 232 #if BYTE_ORDER == LITTLE_ENDIAN 233 bus_space_write_2(bst, bsh, INTR_ACK_REG_B, 234 mask | (bus_space_read_2(bst, bsh, INTR_ACK_REG_B) & 0xff00)); 235 #else 236 bus_space_write_2(bst, bsh, INTR_ACK_REG_B, 237 (mask << 8) | (bus_space_read_2(bst, bsh, INTR_ACK_REG_B) & 0xff)); 238 #endif 239 #else 240 bus_space_write_1(bst, bsh, INTR_ACK_REG_B, mask); 241 #endif 242 KDASSERT(!(bus_space_read_1(bst, bsh, INTR_MASK_REG_B) & IM_ERCV_INT)); 243 } 244 245 void 246 smc91cxx_attach(struct smc91cxx_softc *sc, u_int8_t *myea) 247 { 248 struct ifnet *ifp = &sc->sc_ec.ec_if; 249 bus_space_tag_t bst = sc->sc_bst; 250 bus_space_handle_t bsh = sc->sc_bsh; 251 struct ifmedia *ifm = &sc->sc_mii.mii_media; 252 const char *idstr; 253 u_int32_t miicapabilities; 254 u_int16_t tmp; 255 u_int8_t enaddr[ETHER_ADDR_LEN]; 256 int i, aui, mult, scale, memsize; 257 char pbuf[9]; 258 259 tmp = bus_space_read_2(bst, bsh, BANK_SELECT_REG_W); 260 /* check magic number */ 261 if ((tmp & BSR_DETECT_MASK) != BSR_DETECT_VALUE) { 262 aprint_error_dev(&sc->sc_dev, "failed to detect chip, bsr=%04x\n", tmp); 263 return; 264 } 265 266 /* Make sure the chip is stopped. */ 267 smc91cxx_stop(sc); 268 269 SMC_SELECT_BANK(sc, 3); 270 tmp = bus_space_read_2(bst, bsh, REVISION_REG_W); 271 sc->sc_chipid = RR_ID(tmp); 272 idstr = smc91cxx_idstrs[sc->sc_chipid]; 273 274 aprint_normal_dev(&sc->sc_dev, ""); 275 if (idstr != NULL) 276 aprint_normal("%s, ", idstr); 277 else 278 aprint_normal("unknown chip id %d, ", sc->sc_chipid); 279 aprint_normal("revision %d, ", RR_REV(tmp)); 280 281 SMC_SELECT_BANK(sc, 0); 282 switch (sc->sc_chipid) { 283 default: 284 mult = MCR_MEM_MULT(bus_space_read_2(bst, bsh, MEM_CFG_REG_W)); 285 scale = MIR_SCALE_91C9x; 286 break; 287 288 case CHIP_91C111: 289 mult = MIR_MULT_91C111; 290 scale = MIR_SCALE_91C111; 291 } 292 memsize = bus_space_read_2(bst, bsh, MEM_INFO_REG_W) & MIR_TOTAL_MASK; 293 if (memsize == 255) memsize++; 294 memsize *= scale * mult; 295 296 format_bytes(pbuf, sizeof(pbuf), memsize); 297 aprint_normal("buffer size: %s\n", pbuf); 298 299 /* Read the station address from the chip. */ 300 SMC_SELECT_BANK(sc, 1); 301 if (myea == NULL) { 302 myea = enaddr; 303 for (i = 0; i < ETHER_ADDR_LEN; i += 2) { 304 tmp = bus_space_read_2(bst, bsh, IAR_ADDR0_REG_W + i); 305 myea[i + 1] = (tmp >> 8) & 0xff; 306 myea[i] = tmp & 0xff; 307 } 308 } 309 aprint_normal_dev(&sc->sc_dev, "MAC address %s, ", 310 ether_sprintf(myea)); 311 312 /* Initialize the ifnet structure. */ 313 strlcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ); 314 ifp->if_softc = sc; 315 ifp->if_start = smc91cxx_start; 316 ifp->if_ioctl = smc91cxx_ioctl; 317 ifp->if_watchdog = smc91cxx_watchdog; 318 ifp->if_flags = 319 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST; 320 IFQ_SET_READY(&ifp->if_snd); 321 322 /* Attach the interface. */ 323 if_attach(ifp); 324 ether_ifattach(ifp, myea); 325 326 /* 327 * Initialize our media structures and MII info. We will 328 * probe the MII if we are on the SMC91Cxx 329 */ 330 sc->sc_mii.mii_ifp = ifp; 331 sc->sc_mii.mii_readreg = smc91cxx_mii_readreg; 332 sc->sc_mii.mii_writereg = smc91cxx_mii_writereg; 333 sc->sc_mii.mii_statchg = smc91cxx_statchg; 334 ifmedia_init(ifm, IFM_IMASK, smc91cxx_mediachange, smc91cxx_mediastatus); 335 336 SMC_SELECT_BANK(sc, 1); 337 tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W); 338 339 miicapabilities = BMSR_MEDIAMASK|BMSR_ANEG; 340 switch (sc->sc_chipid) { 341 case CHIP_91100: 342 /* 343 * The 91100 does not have full-duplex capabilities, 344 * even if the PHY does. 345 */ 346 miicapabilities &= ~(BMSR_100TXFDX | BMSR_10TFDX); 347 case CHIP_91100FD: 348 case CHIP_91C111: 349 if (tmp & CR_MII_SELECT) { 350 aprint_normal("default media MII"); 351 if (sc->sc_chipid == CHIP_91C111) { 352 aprint_normal(" (%s PHY)\n", (tmp & CR_AUI_SELECT) ? 353 "external" : "internal"); 354 sc->sc_internal_phy = !(tmp & CR_AUI_SELECT); 355 } else 356 aprint_normal("\n"); 357 mii_attach(&sc->sc_dev, &sc->sc_mii, miicapabilities, 358 MII_PHY_ANY, MII_OFFSET_ANY, 0); 359 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 360 ifmedia_add(&sc->sc_mii.mii_media, 361 IFM_ETHER|IFM_NONE, 0, NULL); 362 ifmedia_set(&sc->sc_mii.mii_media, 363 IFM_ETHER|IFM_NONE); 364 } else { 365 ifmedia_set(&sc->sc_mii.mii_media, 366 IFM_ETHER|IFM_AUTO); 367 } 368 sc->sc_flags |= SMC_FLAGS_HAS_MII; 369 break; 370 } else 371 if (sc->sc_chipid == CHIP_91C111) { 372 /* 373 * XXX: Should bring it out of low-power mode 374 */ 375 aprint_normal("EPH interface in low power mode\n"); 376 sc->sc_internal_phy = 0; 377 return; 378 } 379 /*FALLTHROUGH*/ 380 default: 381 aprint_normal("default media %s\n", (aui = (tmp & CR_AUI_SELECT)) ? 382 "AUI" : "UTP"); 383 for (i = 0; i < NSMC91CxxMEDIA; i++) 384 ifmedia_add(ifm, smc91cxx_media[i], 0, NULL); 385 ifmedia_set(ifm, IFM_ETHER | (aui ? IFM_10_5 : IFM_10_T)); 386 break; 387 } 388 389 #if NRND > 0 390 rnd_attach_source(&sc->rnd_source, device_xname(&sc->sc_dev), 391 RND_TYPE_NET, 0); 392 #endif 393 394 callout_init(&sc->sc_mii_callout, 0); 395 396 /* The attach is successful. */ 397 sc->sc_flags |= SMC_FLAGS_ATTACHED; 398 } 399 400 /* 401 * Change media according to request. 402 */ 403 int 404 smc91cxx_mediachange(struct ifnet *ifp) 405 { 406 struct smc91cxx_softc *sc = ifp->if_softc; 407 408 return (smc91cxx_set_media(sc, sc->sc_mii.mii_media.ifm_media)); 409 } 410 411 int 412 smc91cxx_set_media(struct smc91cxx_softc *sc, int media) 413 { 414 bus_space_tag_t bst = sc->sc_bst; 415 bus_space_handle_t bsh = sc->sc_bsh; 416 u_int16_t tmp; 417 int rc; 418 419 /* 420 * If the interface is not currently powered on, just return. 421 * When it is enabled later, smc91cxx_init() will properly set 422 * up the media for us. 423 */ 424 if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0) 425 return (0); 426 427 if (IFM_TYPE(media) != IFM_ETHER) 428 return (EINVAL); 429 430 if ((sc->sc_flags & SMC_FLAGS_HAS_MII) == 0 || 431 (rc = mii_mediachg(&sc->sc_mii)) == ENXIO) 432 rc = 0; 433 434 switch (IFM_SUBTYPE(media)) { 435 case IFM_10_T: 436 case IFM_10_5: 437 SMC_SELECT_BANK(sc, 1); 438 tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W); 439 if (IFM_SUBTYPE(media) == IFM_10_5) 440 tmp |= CR_AUI_SELECT; 441 else 442 tmp &= ~CR_AUI_SELECT; 443 bus_space_write_2(bst, bsh, CONFIG_REG_W, tmp); 444 delay(20000); /* XXX is this needed? */ 445 break; 446 447 default: 448 return (EINVAL); 449 } 450 451 return rc; 452 } 453 454 /* 455 * Notify the world which media we're using. 456 */ 457 void 458 smc91cxx_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 459 { 460 struct smc91cxx_softc *sc = ifp->if_softc; 461 bus_space_tag_t bst = sc->sc_bst; 462 bus_space_handle_t bsh = sc->sc_bsh; 463 u_int16_t tmp; 464 465 if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0) { 466 ifmr->ifm_active = IFM_ETHER | IFM_NONE; 467 ifmr->ifm_status = 0; 468 return; 469 } 470 471 /* 472 * If we have MII, go ask the PHY what's going on. 473 */ 474 if (sc->sc_flags & SMC_FLAGS_HAS_MII) { 475 mii_pollstat(&sc->sc_mii); 476 ifmr->ifm_active = sc->sc_mii.mii_media_active; 477 ifmr->ifm_status = sc->sc_mii.mii_media_status; 478 return; 479 } 480 481 SMC_SELECT_BANK(sc, 1); 482 tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W); 483 ifmr->ifm_active = 484 IFM_ETHER | ((tmp & CR_AUI_SELECT) ? IFM_10_5 : IFM_10_T); 485 } 486 487 /* 488 * Reset and initialize the chip. 489 */ 490 void 491 smc91cxx_init(struct smc91cxx_softc *sc) 492 { 493 struct ifnet *ifp = &sc->sc_ec.ec_if; 494 bus_space_tag_t bst = sc->sc_bst; 495 bus_space_handle_t bsh = sc->sc_bsh; 496 u_int16_t tmp; 497 const u_int8_t *enaddr; 498 int s, i; 499 500 s = splnet(); 501 502 /* 503 * This resets the registers mostly to defaults, but doesn't 504 * affect the EEPROM. After the reset cycle, we pause briefly 505 * for the chip to recover. 506 * 507 * XXX how long are we really supposed to delay? --thorpej 508 */ 509 SMC_SELECT_BANK(sc, 0); 510 bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, RCR_SOFTRESET); 511 delay(100); 512 bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, 0); 513 delay(200); 514 515 bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, 0); 516 517 /* Set the Ethernet address. */ 518 SMC_SELECT_BANK(sc, 1); 519 enaddr = (const u_int8_t *)CLLADDR(ifp->if_sadl); 520 for (i = 0; i < ETHER_ADDR_LEN; i += 2) { 521 tmp = enaddr[i + 1] << 8 | enaddr[i]; 522 bus_space_write_2(bst, bsh, IAR_ADDR0_REG_W + i, tmp); 523 } 524 525 /* 526 * Set the control register to automatically release successfully 527 * transmitted packets (making the best use of our limited memory) 528 * and enable the EPH interrupt on certain TX errors. 529 */ 530 bus_space_write_2(bst, bsh, CONTROL_REG_W, (CTR_AUTO_RELEASE | 531 CTR_TE_ENABLE | CTR_CR_ENABLE | CTR_LE_ENABLE)); 532 533 /* 534 * Reset the MMU and wait for it to be un-busy. 535 */ 536 SMC_SELECT_BANK(sc, 2); 537 bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_RESET); 538 sc->sc_txpacketno = ARR_FAILED; 539 for (;;) { 540 tmp = bus_space_read_2(bst, bsh, MMU_CMD_REG_W); 541 if (tmp == 0xffff) /* card went away! */ 542 return; 543 if ((tmp & MMUCR_BUSY) == 0) 544 break; 545 } 546 547 /* 548 * Disable all interrupts. 549 */ 550 smc91cxx_intr_mask_write(bst, bsh, 0); 551 552 /* 553 * On the 91c111, enable auto-negotiation, and set the LED 554 * status pins to something sane. 555 * XXX: Should be some way for MD code to decide the latter. 556 */ 557 SMC_SELECT_BANK(sc, 0); 558 if (sc->sc_chipid == CHIP_91C111) { 559 bus_space_write_2(bst, bsh, RX_PHY_CONTROL_REG_W, 560 RPC_ANEG | 561 (RPC_LS_LINK_DETECT << RPC_LSA_SHIFT) | 562 (RPC_LS_TXRX << RPC_LSB_SHIFT)); 563 } 564 565 /* 566 * Set current media. 567 */ 568 smc91cxx_set_media(sc, sc->sc_mii.mii_media.ifm_cur->ifm_media); 569 570 /* 571 * Set the receive filter. We want receive enable and auto 572 * strip of CRC from received packet. If we are in promisc. mode, 573 * then set that bit as well. 574 * 575 * XXX Initialize multicast filter. For now, we just accept 576 * XXX all multicast. 577 */ 578 SMC_SELECT_BANK(sc, 0); 579 580 tmp = RCR_ENABLE | RCR_STRIP_CRC | RCR_ALMUL; 581 if (ifp->if_flags & IFF_PROMISC) 582 tmp |= RCR_PROMISC; 583 584 bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, tmp); 585 586 /* 587 * Set transmitter control to "enabled". 588 */ 589 tmp = TCR_ENABLE; 590 591 #ifndef SMC91CXX_SW_PAD 592 /* 593 * Enable hardware padding of transmitted packets. 594 * XXX doesn't work? 595 */ 596 tmp |= TCR_PAD_ENABLE; 597 #endif 598 599 bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, tmp); 600 601 /* 602 * Now, enable interrupts. 603 */ 604 SMC_SELECT_BANK(sc, 2); 605 606 sc->sc_intmask = IM_EPH_INT | IM_RX_OVRN_INT | IM_RCV_INT; 607 if (sc->sc_chipid == CHIP_91C111 && sc->sc_internal_phy) { 608 sc->sc_intmask |= IM_MD_INT; 609 } 610 smc91cxx_intr_mask_write(bst, bsh, sc->sc_intmask); 611 612 /* Interface is now running, with no output active. */ 613 ifp->if_flags |= IFF_RUNNING; 614 ifp->if_flags &= ~IFF_OACTIVE; 615 616 if (sc->sc_flags & SMC_FLAGS_HAS_MII) { 617 /* Start the one second clock. */ 618 callout_reset(&sc->sc_mii_callout, hz, smc91cxx_tick, sc); 619 } 620 621 /* 622 * Attempt to start any pending transmission. 623 */ 624 smc91cxx_start(ifp); 625 626 splx(s); 627 } 628 629 /* 630 * Start output on an interface. 631 * Must be called at splnet or interrupt level. 632 */ 633 void 634 smc91cxx_start(struct ifnet *ifp) 635 { 636 struct smc91cxx_softc *sc = ifp->if_softc; 637 bus_space_tag_t bst = sc->sc_bst; 638 bus_space_handle_t bsh = sc->sc_bsh; 639 u_int len; 640 struct mbuf *m; 641 u_int16_t length, npages; 642 u_int16_t oddbyte; 643 u_int8_t packetno; 644 int timo, pad; 645 646 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) 647 return; 648 649 again: 650 /* 651 * Peek at the next packet. 652 */ 653 IFQ_POLL(&ifp->if_snd, m); 654 if (m == NULL) 655 return; 656 657 /* 658 * Compute the frame length and set pad to give an overall even 659 * number of bytes. Below, we assume that the packet length 660 * is even. 661 */ 662 for (len = 0; m != NULL; m = m->m_next) 663 len += m->m_len; 664 pad = (len & 1); 665 666 /* 667 * We drop packets that are too large. Perhaps we should 668 * truncate them instead? 669 */ 670 if ((len + pad) > (ETHER_MAX_LEN - ETHER_CRC_LEN)) { 671 printf("%s: large packet discarded\n", device_xname(&sc->sc_dev)); 672 ifp->if_oerrors++; 673 IFQ_DEQUEUE(&ifp->if_snd, m); 674 m_freem(m); 675 goto readcheck; 676 } 677 678 #ifdef SMC91CXX_SW_PAD 679 /* 680 * Not using hardware padding; pad to ETHER_MIN_LEN. 681 */ 682 if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN)) 683 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len; 684 #endif 685 686 length = pad + len; 687 688 /* 689 * The MMU has a 256 byte page size. The MMU expects us to 690 * ask for "npages - 1". We include space for the status word, 691 * byte count, and control bytes in the allocation request. 692 */ 693 npages = ((length & ~1) + 6) >> 8; 694 695 /* 696 * Now allocate the memory. 697 */ 698 SMC_SELECT_BANK(sc, 2); 699 bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_ALLOC | npages); 700 701 timo = MEMORY_WAIT_TIME; 702 if (__predict_false((sc->sc_txpacketno & ARR_FAILED) == 0)) { 703 packetno = sc->sc_txpacketno; 704 sc->sc_txpacketno = ARR_FAILED; 705 } else { 706 do { 707 if (bus_space_read_1(bst, bsh, 708 INTR_STAT_REG_B) & IM_ALLOC_INT) 709 break; 710 delay(1); 711 } while (--timo); 712 } 713 714 packetno = bus_space_read_1(bst, bsh, ALLOC_RESULT_REG_B); 715 716 if (packetno & ARR_FAILED || timo == 0) { 717 /* 718 * No transmit memory is available. Record the number 719 * of requestd pages and enable the allocation completion 720 * interrupt. Set up the watchdog timer in case we miss 721 * the interrupt. Mark the interface as active so that 722 * no one else attempts to transmit while we're allocating 723 * memory. 724 */ 725 sc->sc_intmask |= IM_ALLOC_INT; 726 smc91cxx_intr_mask_write(bst, bsh, sc->sc_intmask); 727 ifp->if_timer = 5; 728 ifp->if_flags |= IFF_OACTIVE; 729 730 return; 731 } 732 733 /* 734 * We have a packet number - set the data window. 735 */ 736 bus_space_write_1(bst, bsh, PACKET_NUM_REG_B, packetno); 737 738 /* 739 * Point to the beginning of the packet. 740 */ 741 bus_space_write_2(bst, bsh, POINTER_REG_W, PTR_AUTOINC /* | 0x0000 */); 742 743 /* 744 * Send the packet length (+6 for stats, length, and control bytes) 745 * and the status word (set to zeros). 746 */ 747 bus_space_write_2(bst, bsh, DATA_REG_W, 0); 748 bus_space_write_2(bst, bsh, DATA_REG_W, (length + 6) & 0x7ff); 749 750 /* 751 * Get the packet from the kernel. This will include the Ethernet 752 * frame header, MAC address, etc. 753 */ 754 IFQ_DEQUEUE(&ifp->if_snd, m); 755 756 /* 757 * Push the packet out to the card. 758 */ 759 oddbyte = smc91cxx_copy_tx_frame(sc, m); 760 761 #ifdef SMC91CXX_SW_PAD 762 #ifdef SMC91CXX_NO_BYTE_WRITE 763 #if BYTE_ORDER == LITTLE_ENDIAN 764 if (pad > 1 && (pad & 1)) { 765 bus_space_write_2(bst, bsh, DATA_REG_W, oddbyte << 0); 766 oddbyte = 0; 767 } 768 #else 769 if (pad > 1 && (pad & 1)) { 770 bus_space_write_2(bst, bsh, DATA_REG_W, oddbyte << 8); 771 oddbyte = 0; 772 } 773 #endif 774 #endif 775 776 /* 777 * Push out padding. 778 */ 779 while (pad > 1) { 780 bus_space_write_2(bst, bsh, DATA_REG_W, 0); 781 pad -= 2; 782 } 783 #endif 784 785 #ifdef SMC91CXX_NO_BYTE_WRITE 786 /* 787 * Push out control byte and unused packet byte. The control byte 788 * is 0, meaning the packet is even lengthed and no special 789 * CRC handling is necessary. 790 */ 791 #if BYTE_ORDER == LITTLE_ENDIAN 792 bus_space_write_2(bst, bsh, DATA_REG_W, 793 oddbyte | (pad ? (CTLB_ODD << 8) : 0)); 794 #else 795 bus_space_write_2(bst, bsh, DATA_REG_W, 796 (oddbyte << 8) | (pad ? CTLB_ODD : 0)); 797 #endif 798 #else 799 if (pad) 800 bus_space_write_1(bst, bsh, DATA_REG_B, 0); 801 #endif 802 803 /* 804 * Enable transmit interrupts and let the chip go. Set a watchdog 805 * in case we miss the interrupt. 806 */ 807 sc->sc_intmask |= IM_TX_INT | IM_TX_EMPTY_INT; 808 smc91cxx_intr_mask_write(bst, bsh, sc->sc_intmask); 809 810 bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_ENQUEUE); 811 812 ifp->if_timer = 5; 813 814 /* Hand off a copy to the bpf. */ 815 bpf_mtap(ifp, m); 816 817 ifp->if_opackets++; 818 m_freem(m); 819 820 readcheck: 821 /* 822 * Check for incoming pcakets. We don't want to overflow the small 823 * RX FIFO. If nothing has arrived, attempt to queue another 824 * transmit packet. 825 */ 826 if (bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W) & FIFO_REMPTY) 827 goto again; 828 } 829 830 /* 831 * Squirt a (possibly misaligned) mbuf to the device 832 */ 833 uint8_t 834 smc91cxx_copy_tx_frame(struct smc91cxx_softc *sc, struct mbuf *m0) 835 { 836 bus_space_tag_t bst = sc->sc_bst; 837 bus_space_handle_t bsh = sc->sc_bsh; 838 struct mbuf *m; 839 int len, leftover; 840 u_int16_t dbuf; 841 u_int8_t *p; 842 #ifdef DIAGNOSTIC 843 u_int8_t *lim; 844 #endif 845 846 /* start out with no leftover data */ 847 leftover = 0; 848 dbuf = 0; 849 850 /* Process the chain of mbufs */ 851 for (m = m0; m != NULL; m = m->m_next) { 852 /* 853 * Process all of the data in a single mbuf. 854 */ 855 p = mtod(m, u_int8_t *); 856 len = m->m_len; 857 #ifdef DIAGNOSTIC 858 lim = p + len; 859 #endif 860 861 while (len > 0) { 862 if (leftover) { 863 /* 864 * Data left over (from mbuf or realignment). 865 * Buffer the next byte, and write it and 866 * the leftover data out. 867 */ 868 dbuf |= *p++ << 8; 869 len--; 870 bus_space_write_2(bst, bsh, DATA_REG_W, dbuf); 871 leftover = 0; 872 } else if ((long) p & 1) { 873 /* 874 * Misaligned data. Buffer the next byte. 875 */ 876 dbuf = *p++; 877 len--; 878 leftover = 1; 879 } else { 880 /* 881 * Aligned data. This is the case we like. 882 * 883 * Write-region out as much as we can, then 884 * buffer the remaining byte (if any). 885 */ 886 leftover = len & 1; 887 len &= ~1; 888 bus_space_write_multi_stream_2(bst, bsh, 889 DATA_REG_W, (u_int16_t *)p, len >> 1); 890 p += len; 891 892 if (leftover) 893 dbuf = *p++; 894 len = 0; 895 } 896 } 897 if (len < 0) 898 panic("smc91cxx_copy_tx_frame: negative len"); 899 #ifdef DIAGNOSTIC 900 if (p != lim) 901 panic("smc91cxx_copy_tx_frame: p != lim"); 902 #endif 903 } 904 #ifndef SMC91CXX_NO_BYTE_WRITE 905 if (leftover) 906 bus_space_write_1(bst, bsh, DATA_REG_B, dbuf); 907 #endif 908 return dbuf; 909 } 910 911 /* 912 * Interrupt service routine. 913 */ 914 int 915 smc91cxx_intr(void *arg) 916 { 917 struct smc91cxx_softc *sc = arg; 918 struct ifnet *ifp = &sc->sc_ec.ec_if; 919 bus_space_tag_t bst = sc->sc_bst; 920 bus_space_handle_t bsh = sc->sc_bsh; 921 u_int8_t mask, interrupts, status; 922 u_int16_t packetno, tx_status, card_stats; 923 #ifdef SMC91CXX_NO_BYTE_WRITE 924 u_int16_t v; 925 #endif 926 927 if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0 || 928 !device_is_active(&sc->sc_dev)) 929 return (0); 930 931 SMC_SELECT_BANK(sc, 2); 932 933 /* 934 * Obtain the current interrupt status and mask. 935 */ 936 #ifdef SMC91CXX_NO_BYTE_WRITE 937 v = bus_space_read_2(bst, bsh, INTR_STAT_REG_B); 938 939 /* 940 * Get the set of interrupt which occurred and eliminate any 941 * which are not enabled. 942 */ 943 #if BYTE_ORDER == LITTLE_ENDIAN 944 mask = v >> 8; 945 interrupts = v & 0xff; 946 #else 947 interrupts = v >> 8; 948 mask = v & 0xff; 949 #endif 950 KDASSERT(mask == sc->sc_intmask); 951 #else 952 mask = bus_space_read_1(bst, bsh, INTR_MASK_REG_B); 953 954 /* 955 * Get the set of interrupt which occurred and eliminate any 956 * which are not enabled. 957 */ 958 interrupts = bus_space_read_1(bst, bsh, INTR_STAT_REG_B); 959 #endif 960 status = interrupts & mask; 961 962 /* Ours? */ 963 if (status == 0) 964 return (0); 965 966 /* 967 * It's ours; disable all interrupts while we process them. 968 */ 969 smc91cxx_intr_mask_write(bst, bsh, 0); 970 971 /* 972 * Receive overrun interrupts. 973 */ 974 if (status & IM_RX_OVRN_INT) { 975 smc91cxx_intr_ack_write(bst, bsh, IM_RX_OVRN_INT); 976 ifp->if_ierrors++; 977 } 978 979 /* 980 * Receive interrupts. 981 */ 982 if (status & IM_RCV_INT) { 983 #if 1 /* DIAGNOSTIC */ 984 packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W); 985 if (packetno & FIFO_REMPTY) { 986 aprint_error_dev(&sc->sc_dev, "receive interrupt on empty fifo\n"); 987 goto out; 988 } else 989 #endif 990 smc91cxx_read(sc); 991 } 992 993 /* 994 * Memory allocation interrupts. 995 */ 996 if (status & IM_ALLOC_INT) { 997 /* Disable this interrupt. */ 998 mask &= ~IM_ALLOC_INT; 999 sc->sc_intmask &= ~IM_ALLOC_INT; 1000 1001 /* 1002 * Save allocated packet number for use in start 1003 */ 1004 packetno = bus_space_read_1(bst, bsh, ALLOC_RESULT_REG_B); 1005 KASSERT(sc->sc_txpacketno & ARR_FAILED); 1006 sc->sc_txpacketno = packetno; 1007 1008 /* 1009 * We can transmit again! 1010 */ 1011 ifp->if_flags &= ~IFF_OACTIVE; 1012 ifp->if_timer = 0; 1013 } 1014 1015 /* 1016 * Transmit complete interrupt. Handle transmission error messages. 1017 * This will only be called on error condition because of AUTO RELEASE 1018 * mode. 1019 */ 1020 if (status & IM_TX_INT) { 1021 smc91cxx_intr_ack_write(bst, bsh, IM_TX_INT); 1022 1023 packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W) & 1024 FIFO_TX_MASK; 1025 1026 /* 1027 * Select this as the packet to read from. 1028 */ 1029 bus_space_write_1(bst, bsh, PACKET_NUM_REG_B, packetno); 1030 1031 /* 1032 * Position the pointer to the beginning of the packet. 1033 */ 1034 bus_space_write_2(bst, bsh, POINTER_REG_W, 1035 PTR_AUTOINC | PTR_READ /* | 0x0000 */); 1036 1037 /* 1038 * Fetch the TX status word. This will be a copy of 1039 * the EPH_STATUS_REG_W at the time of the transmission 1040 * failure. 1041 */ 1042 tx_status = bus_space_read_2(bst, bsh, DATA_REG_W); 1043 1044 if (tx_status & EPHSR_TX_SUC) { 1045 static struct timeval txsuc_last; 1046 static int txsuc_count; 1047 if (ppsratecheck(&txsuc_last, &txsuc_count, 1)) 1048 printf("%s: successful packet caused TX" 1049 " interrupt?!\n", device_xname(&sc->sc_dev)); 1050 } else 1051 ifp->if_oerrors++; 1052 1053 if (tx_status & EPHSR_LATCOL) 1054 ifp->if_collisions++; 1055 1056 /* Disable this interrupt (start will reenable if needed). */ 1057 mask &= ~IM_TX_INT; 1058 sc->sc_intmask &= ~IM_TX_INT; 1059 1060 /* 1061 * Some of these errors disable the transmitter; reenable it. 1062 */ 1063 SMC_SELECT_BANK(sc, 0); 1064 #ifdef SMC91CXX_SW_PAD 1065 bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, TCR_ENABLE); 1066 #else 1067 bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, 1068 TCR_ENABLE | TCR_PAD_ENABLE); 1069 #endif 1070 1071 /* 1072 * Kill the failed packet and wait for the MMU to unbusy. 1073 */ 1074 SMC_SELECT_BANK(sc, 2); 1075 while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY) 1076 /* XXX bound this loop! */ ; 1077 bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_FREEPKT); 1078 1079 ifp->if_timer = 0; 1080 } 1081 1082 /* 1083 * Transmit underrun interrupts. We use this opportunity to 1084 * update transmit statistics from the card. 1085 */ 1086 if (status & IM_TX_EMPTY_INT) { 1087 smc91cxx_intr_ack_write(bst, bsh, IM_TX_EMPTY_INT); 1088 1089 /* Disable this interrupt. */ 1090 mask &= ~IM_TX_EMPTY_INT; 1091 sc->sc_intmask &= ~IM_TX_EMPTY_INT; 1092 1093 SMC_SELECT_BANK(sc, 0); 1094 card_stats = bus_space_read_2(bst, bsh, COUNTER_REG_W); 1095 1096 /* Single collisions. */ 1097 ifp->if_collisions += card_stats & ECR_COLN_MASK; 1098 1099 /* Multiple collisions. */ 1100 ifp->if_collisions += (card_stats & ECR_MCOLN_MASK) >> 4; 1101 1102 SMC_SELECT_BANK(sc, 2); 1103 1104 ifp->if_timer = 0; 1105 } 1106 1107 if (sc->sc_chipid == CHIP_91C111 && sc->sc_internal_phy && 1108 (status & IM_MD_INT)) { 1109 /* 1110 * Internal PHY status change 1111 */ 1112 mii_tick(&sc->sc_mii); 1113 } 1114 1115 /* 1116 * Other errors. Reset the interface. 1117 */ 1118 if (status & IM_EPH_INT) { 1119 smc91cxx_stop(sc); 1120 smc91cxx_init(sc); 1121 } 1122 1123 /* 1124 * Attempt to queue more packets for transmission. 1125 */ 1126 smc91cxx_start(ifp); 1127 1128 out: 1129 /* 1130 * Reenable the interrupts we wish to receive now that processing 1131 * is complete. 1132 */ 1133 mask |= sc->sc_intmask; 1134 smc91cxx_intr_mask_write(bst, bsh, mask); 1135 1136 #if NRND > 0 1137 if (status) 1138 rnd_add_uint32(&sc->rnd_source, status); 1139 #endif 1140 1141 return (1); 1142 } 1143 1144 /* 1145 * Read a packet from the card and pass it up to the kernel. 1146 * NOTE! WE EXPECT TO BE IN REGISTER WINDOW 2! 1147 */ 1148 void 1149 smc91cxx_read(struct smc91cxx_softc *sc) 1150 { 1151 struct ifnet *ifp = &sc->sc_ec.ec_if; 1152 bus_space_tag_t bst = sc->sc_bst; 1153 bus_space_handle_t bsh = sc->sc_bsh; 1154 struct ether_header *eh; 1155 struct mbuf *m; 1156 u_int16_t status, packetno, packetlen; 1157 u_int8_t *data; 1158 u_int32_t dr; 1159 1160 again: 1161 /* 1162 * Set data pointer to the beginning of the packet. Since 1163 * PTR_RCV is set, the packet number will be found automatically 1164 * in FIFO_PORTS_REG_W, FIFO_RX_MASK. 1165 */ 1166 packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W); 1167 if (packetno & FIFO_REMPTY) 1168 return; 1169 1170 bus_space_write_2(bst, bsh, POINTER_REG_W, 1171 PTR_READ | PTR_RCV | PTR_AUTOINC /* | 0x0000 */); 1172 1173 /* 1174 * First two words are status and packet length. 1175 */ 1176 if ((sc->sc_flags & SMC_FLAGS_32BIT_READ) == 0) { 1177 status = bus_space_read_2(bst, bsh, DATA_REG_W); 1178 packetlen = bus_space_read_2(bst, bsh, DATA_REG_W); 1179 } else { 1180 dr = bus_space_read_4(bst, bsh, DATA_REG_W); 1181 #if BYTE_ORDER == LITTLE_ENDIAN 1182 status = (u_int16_t)dr; 1183 packetlen = (u_int16_t)(dr >> 16); 1184 #else 1185 packetlen = (u_int16_t)dr; 1186 status = (u_int16_t)(dr >> 16); 1187 #endif 1188 } 1189 1190 packetlen &= RLEN_MASK; 1191 if (packetlen < ETHER_MIN_LEN - ETHER_CRC_LEN + 6 || packetlen > 1534) { 1192 ifp->if_ierrors++; 1193 goto out; 1194 } 1195 1196 /* 1197 * The packet length includes 3 extra words: status, length, 1198 * and an extra word that includes the control byte. 1199 */ 1200 packetlen -= 6; 1201 1202 /* 1203 * Account for receive errors and discard. 1204 */ 1205 if (status & RS_ERRORS) { 1206 ifp->if_ierrors++; 1207 goto out; 1208 } 1209 1210 /* 1211 * Adjust for odd-length packet. 1212 */ 1213 if (status & RS_ODDFRAME) 1214 packetlen++; 1215 1216 /* 1217 * Allocate a header mbuf. 1218 */ 1219 MGETHDR(m, M_DONTWAIT, MT_DATA); 1220 if (m == NULL) 1221 goto out; 1222 m->m_pkthdr.rcvif = ifp; 1223 m->m_pkthdr.len = packetlen; 1224 1225 /* 1226 * Always put the packet in a cluster. 1227 * XXX should chain small mbufs if less than threshold. 1228 */ 1229 MCLGET(m, M_DONTWAIT); 1230 if ((m->m_flags & M_EXT) == 0) { 1231 m_freem(m); 1232 ifp->if_ierrors++; 1233 aprint_error_dev(&sc->sc_dev, "can't allocate cluster for incoming packet\n"); 1234 goto out; 1235 } 1236 1237 /* 1238 * Pull the packet off the interface. Make sure the payload 1239 * is aligned. 1240 */ 1241 if ((sc->sc_flags & SMC_FLAGS_32BIT_READ) == 0) { 1242 m->m_data = (char *) ALIGN(mtod(m, char *) + 1243 sizeof(struct ether_header)) - sizeof(struct ether_header); 1244 1245 eh = mtod(m, struct ether_header *); 1246 data = mtod(m, u_int8_t *); 1247 KASSERT(trunc_page((uintptr_t)data) == trunc_page((uintptr_t)data + packetlen - 1)); 1248 if (packetlen > 1) 1249 bus_space_read_multi_stream_2(bst, bsh, DATA_REG_W, 1250 (u_int16_t *)data, packetlen >> 1); 1251 if (packetlen & 1) { 1252 data += packetlen & ~1; 1253 *data = bus_space_read_1(bst, bsh, DATA_REG_B); 1254 } 1255 } else { 1256 u_int8_t *dp; 1257 1258 m->m_data = (void *) ALIGN(mtod(m, void *)); 1259 eh = mtod(m, struct ether_header *); 1260 dp = data = mtod(m, u_int8_t *); 1261 KASSERT(trunc_page((uintptr_t)data) == trunc_page((uintptr_t)data + packetlen - 1)); 1262 if (packetlen > 3) 1263 bus_space_read_multi_stream_4(bst, bsh, DATA_REG_W, 1264 (u_int32_t *)data, packetlen >> 2); 1265 if (packetlen & 3) { 1266 data += packetlen & ~3; 1267 *((u_int32_t *)data) = 1268 bus_space_read_stream_4(bst, bsh, DATA_REG_W); 1269 } 1270 } 1271 1272 ifp->if_ipackets++; 1273 1274 /* 1275 * Make sure to behave as IFF_SIMPLEX in all cases. 1276 * This is to cope with SMC91C92 (Megahertz XJ10BT), which 1277 * loops back packets to itself on promiscuous mode. 1278 * (should be ensured by chipset configuration) 1279 */ 1280 if ((ifp->if_flags & IFF_PROMISC) != 0) { 1281 /* 1282 * Drop packet looped back from myself. 1283 */ 1284 if (ether_cmp(eh->ether_shost, CLLADDR(ifp->if_sadl)) == 0) { 1285 m_freem(m); 1286 goto out; 1287 } 1288 } 1289 1290 m->m_pkthdr.len = m->m_len = packetlen; 1291 1292 /* 1293 * Hand the packet off to bpf listeners. 1294 */ 1295 bpf_mtap(ifp, m); 1296 1297 (*ifp->if_input)(ifp, m); 1298 1299 out: 1300 /* 1301 * Tell the card to free the memory occupied by this packet. 1302 */ 1303 while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY) 1304 /* XXX bound this loop! */ ; 1305 bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_RELEASE); 1306 1307 /* 1308 * Check for another packet. 1309 */ 1310 packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W); 1311 if (packetno & FIFO_REMPTY) 1312 return; 1313 goto again; 1314 } 1315 1316 /* 1317 * Process an ioctl request. 1318 */ 1319 int 1320 smc91cxx_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1321 { 1322 struct smc91cxx_softc *sc = ifp->if_softc; 1323 struct ifaddr *ifa = (struct ifaddr *)data; 1324 struct ifreq *ifr = (struct ifreq *)data; 1325 int s, error = 0; 1326 1327 s = splnet(); 1328 1329 switch (cmd) { 1330 case SIOCINITIFADDR: 1331 if ((error = smc91cxx_enable(sc)) != 0) 1332 break; 1333 ifp->if_flags |= IFF_UP; 1334 smc91cxx_init(sc); 1335 switch (ifa->ifa_addr->sa_family) { 1336 #ifdef INET 1337 case AF_INET: 1338 arp_ifinit(ifp, ifa); 1339 break; 1340 #endif 1341 default: 1342 break; 1343 } 1344 break; 1345 1346 1347 case SIOCSIFFLAGS: 1348 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 1349 break; 1350 /* XXX re-use ether_ioctl() */ 1351 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { 1352 case IFF_RUNNING: 1353 /* 1354 * If interface is marked down and it is running, 1355 * stop it. 1356 */ 1357 smc91cxx_stop(sc); 1358 ifp->if_flags &= ~IFF_RUNNING; 1359 smc91cxx_disable(sc); 1360 break; 1361 case IFF_UP: 1362 /* 1363 * If interface is marked up and it is stopped, 1364 * start it. 1365 */ 1366 if ((error = smc91cxx_enable(sc)) != 0) 1367 break; 1368 smc91cxx_init(sc); 1369 break; 1370 case IFF_UP|IFF_RUNNING: 1371 /* 1372 * Reset the interface to pick up changes in any 1373 * other flags that affect hardware registers. 1374 */ 1375 smc91cxx_reset(sc); 1376 break; 1377 case 0: 1378 break; 1379 } 1380 break; 1381 1382 case SIOCADDMULTI: 1383 case SIOCDELMULTI: 1384 if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0) { 1385 error = EIO; 1386 break; 1387 } 1388 1389 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) { 1390 /* 1391 * Multicast list has changed; set the hardware 1392 * filter accordingly. 1393 */ 1394 if (ifp->if_flags & IFF_RUNNING) 1395 smc91cxx_reset(sc); 1396 error = 0; 1397 } 1398 break; 1399 1400 case SIOCGIFMEDIA: 1401 case SIOCSIFMEDIA: 1402 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd); 1403 break; 1404 1405 default: 1406 error = ether_ioctl(ifp, cmd, data); 1407 break; 1408 } 1409 1410 splx(s); 1411 return (error); 1412 } 1413 1414 /* 1415 * Reset the interface. 1416 */ 1417 void 1418 smc91cxx_reset(struct smc91cxx_softc *sc) 1419 { 1420 int s; 1421 1422 s = splnet(); 1423 smc91cxx_stop(sc); 1424 smc91cxx_init(sc); 1425 splx(s); 1426 } 1427 1428 /* 1429 * Watchdog timer. 1430 */ 1431 void 1432 smc91cxx_watchdog(struct ifnet *ifp) 1433 { 1434 struct smc91cxx_softc *sc = ifp->if_softc; 1435 1436 log(LOG_ERR, "%s: device timeout\n", device_xname(&sc->sc_dev)); 1437 ifp->if_oerrors++; 1438 smc91cxx_reset(sc); 1439 } 1440 1441 /* 1442 * Stop output on the interface. 1443 */ 1444 void 1445 smc91cxx_stop(struct smc91cxx_softc *sc) 1446 { 1447 bus_space_tag_t bst = sc->sc_bst; 1448 bus_space_handle_t bsh = sc->sc_bsh; 1449 1450 /* 1451 * Clear interrupt mask; disable all interrupts. 1452 */ 1453 SMC_SELECT_BANK(sc, 2); 1454 smc91cxx_intr_mask_write(bst, bsh, 0); 1455 1456 /* 1457 * Disable transmitter and receiver. 1458 */ 1459 SMC_SELECT_BANK(sc, 0); 1460 bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, 0); 1461 bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, 0); 1462 1463 /* 1464 * Cancel watchdog timer. 1465 */ 1466 sc->sc_ec.ec_if.if_timer = 0; 1467 } 1468 1469 /* 1470 * Enable power on the interface. 1471 */ 1472 int 1473 smc91cxx_enable(struct smc91cxx_softc *sc) 1474 { 1475 1476 if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0 && sc->sc_enable != NULL) { 1477 if ((*sc->sc_enable)(sc) != 0) { 1478 aprint_error_dev(&sc->sc_dev, "device enable failed\n"); 1479 return (EIO); 1480 } 1481 } 1482 1483 sc->sc_flags |= SMC_FLAGS_ENABLED; 1484 return (0); 1485 } 1486 1487 /* 1488 * Disable power on the interface. 1489 */ 1490 void 1491 smc91cxx_disable(struct smc91cxx_softc *sc) 1492 { 1493 1494 if ((sc->sc_flags & SMC_FLAGS_ENABLED) != 0 && sc->sc_disable != NULL) { 1495 (*sc->sc_disable)(sc); 1496 sc->sc_flags &= ~SMC_FLAGS_ENABLED; 1497 } 1498 } 1499 1500 int 1501 smc91cxx_activate(device_t self, enum devact act) 1502 { 1503 struct smc91cxx_softc *sc = device_private(self); 1504 1505 switch (act) { 1506 case DVACT_DEACTIVATE: 1507 if_deactivate(&sc->sc_ec.ec_if); 1508 return 0; 1509 default: 1510 return EOPNOTSUPP; 1511 } 1512 } 1513 1514 int 1515 smc91cxx_detach(device_t self, int flags) 1516 { 1517 struct smc91cxx_softc *sc = (struct smc91cxx_softc *)self; 1518 struct ifnet *ifp = &sc->sc_ec.ec_if; 1519 1520 /* Succeed now if there's no work to do. */ 1521 if ((sc->sc_flags & SMC_FLAGS_ATTACHED) == 0) 1522 return (0); 1523 1524 1525 /* smc91cxx_disable() checks SMC_FLAGS_ENABLED */ 1526 smc91cxx_disable(sc); 1527 1528 /* smc91cxx_attach() never fails */ 1529 1530 /* Delete all media. */ 1531 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); 1532 1533 #if NRND > 0 1534 rnd_detach_source(&sc->rnd_source); 1535 #endif 1536 ether_ifdetach(ifp); 1537 if_detach(ifp); 1538 1539 return (0); 1540 } 1541 1542 u_int32_t 1543 smc91cxx_mii_bitbang_read(device_t self) 1544 { 1545 struct smc91cxx_softc *sc = (void *) self; 1546 1547 /* We're already in bank 3. */ 1548 return (bus_space_read_2(sc->sc_bst, sc->sc_bsh, MGMT_REG_W)); 1549 } 1550 1551 void 1552 smc91cxx_mii_bitbang_write(device_t self, u_int32_t val) 1553 { 1554 struct smc91cxx_softc *sc = (void *) self; 1555 1556 /* We're already in bank 3. */ 1557 bus_space_write_2(sc->sc_bst, sc->sc_bsh, MGMT_REG_W, val); 1558 } 1559 1560 int 1561 smc91cxx_mii_readreg(device_t self, int phy, int reg) 1562 { 1563 struct smc91cxx_softc *sc = (void *) self; 1564 int val; 1565 1566 SMC_SELECT_BANK(sc, 3); 1567 1568 val = mii_bitbang_readreg(self, &smc91cxx_mii_bitbang_ops, phy, reg); 1569 1570 SMC_SELECT_BANK(sc, 2); 1571 1572 return (val); 1573 } 1574 1575 void 1576 smc91cxx_mii_writereg(device_t self, int phy, int reg, int val) 1577 { 1578 struct smc91cxx_softc *sc = (void *) self; 1579 1580 SMC_SELECT_BANK(sc, 3); 1581 1582 mii_bitbang_writereg(self, &smc91cxx_mii_bitbang_ops, phy, reg, val); 1583 1584 SMC_SELECT_BANK(sc, 2); 1585 } 1586 1587 void 1588 smc91cxx_statchg(device_t self) 1589 { 1590 struct smc91cxx_softc *sc = (struct smc91cxx_softc *)self; 1591 bus_space_tag_t bst = sc->sc_bst; 1592 bus_space_handle_t bsh = sc->sc_bsh; 1593 int mctl; 1594 1595 SMC_SELECT_BANK(sc, 0); 1596 mctl = bus_space_read_2(bst, bsh, TXMIT_CONTROL_REG_W); 1597 if (sc->sc_mii.mii_media_active & IFM_FDX) 1598 mctl |= TCR_SWFDUP; 1599 else 1600 mctl &= ~TCR_SWFDUP; 1601 bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, mctl); 1602 SMC_SELECT_BANK(sc, 2); /* back to operating window */ 1603 } 1604 1605 /* 1606 * One second timer, used to tick the MII. 1607 */ 1608 void 1609 smc91cxx_tick(void *arg) 1610 { 1611 struct smc91cxx_softc *sc = arg; 1612 int s; 1613 1614 #ifdef DIAGNOSTIC 1615 if ((sc->sc_flags & SMC_FLAGS_HAS_MII) == 0) 1616 panic("smc91cxx_tick"); 1617 #endif 1618 1619 if (!device_is_active(&sc->sc_dev)) 1620 return; 1621 1622 s = splnet(); 1623 mii_tick(&sc->sc_mii); 1624 splx(s); 1625 1626 callout_reset(&sc->sc_mii_callout, hz, smc91cxx_tick, sc); 1627 } 1628 1629