1 /* $NetBSD: if_tl.c,v 1.52 2002/04/17 09:16:27 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Manuel Bouyer. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Texas Instruments ThunderLAN ethernet controller 34 * ThunderLAN Programmer's Guide (TI Literature Number SPWU013A) 35 * available from www.ti.com 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: if_tl.c,v 1.52 2002/04/17 09:16:27 wiz Exp $"); 40 41 #undef TLDEBUG 42 #define TL_PRIV_STATS 43 #undef TLDEBUG_RX 44 #undef TLDEBUG_TX 45 #undef TLDEBUG_ADDR 46 47 #include "opt_inet.h" 48 #include "opt_ns.h" 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/mbuf.h> 53 #include <sys/protosw.h> 54 #include <sys/socket.h> 55 #include <sys/ioctl.h> 56 #include <sys/errno.h> 57 #include <sys/malloc.h> 58 #include <sys/kernel.h> 59 #include <sys/proc.h> /* only for declaration of wakeup() used by vm.h */ 60 #include <sys/device.h> 61 62 #include <net/if.h> 63 #if defined(SIOCSIFMEDIA) 64 #include <net/if_media.h> 65 #endif 66 #include <net/if_types.h> 67 #include <net/if_dl.h> 68 #include <net/route.h> 69 #include <net/netisr.h> 70 71 #include "bpfilter.h" 72 #if NBPFILTER > 0 73 #include <net/bpf.h> 74 #include <net/bpfdesc.h> 75 #endif 76 77 #ifdef INET 78 #include <netinet/in.h> 79 #include <netinet/in_systm.h> 80 #include <netinet/in_var.h> 81 #include <netinet/ip.h> 82 #endif 83 84 #ifdef NS 85 #include <netns/ns.h> 86 #include <netns/ns_if.h> 87 #endif 88 89 #if defined(__NetBSD__) 90 #include <net/if_ether.h> 91 #include <uvm/uvm_extern.h> 92 #if defined(INET) 93 #include <netinet/if_inarp.h> 94 #endif 95 96 #include <machine/bus.h> 97 #include <machine/intr.h> 98 99 #include <dev/pci/pcireg.h> 100 #include <dev/pci/pcivar.h> 101 #include <dev/pci/pcidevs.h> 102 103 #include <dev/i2c/i2c_bus.h> 104 #include <dev/i2c/i2c_eeprom.h> 105 106 #include <dev/mii/mii.h> 107 #include <dev/mii/miivar.h> 108 109 #include <dev/mii/tlphyvar.h> 110 111 #include <dev/pci/if_tlregs.h> 112 #include <dev/pci/if_tlvar.h> 113 #endif /* __NetBSD__ */ 114 115 /* number of transmit/receive buffers */ 116 #ifndef TL_NBUF 117 #define TL_NBUF 10 118 #endif 119 120 static int tl_pci_match __P((struct device *, struct cfdata *, void *)); 121 static void tl_pci_attach __P((struct device *, struct device *, void *)); 122 static int tl_intr __P((void *)); 123 124 static int tl_ifioctl __P((struct ifnet *, ioctl_cmd_t, caddr_t)); 125 static int tl_mediachange __P((struct ifnet *)); 126 static void tl_mediastatus __P((struct ifnet *, struct ifmediareq *)); 127 static void tl_ifwatchdog __P((struct ifnet *)); 128 static void tl_shutdown __P((void*)); 129 130 static void tl_ifstart __P((struct ifnet *)); 131 static void tl_reset __P((tl_softc_t*)); 132 static int tl_init __P((struct ifnet *)); 133 static void tl_stop __P((struct ifnet *, int)); 134 static void tl_restart __P((void *)); 135 static int tl_add_RxBuff __P((tl_softc_t*, struct Rx_list*, struct mbuf*)); 136 static void tl_read_stats __P((tl_softc_t*)); 137 static void tl_ticks __P((void*)); 138 static int tl_multicast_hash __P((u_int8_t*)); 139 static void tl_addr_filter __P((tl_softc_t*)); 140 141 static u_int32_t tl_intreg_read __P((tl_softc_t*, u_int32_t)); 142 static void tl_intreg_write __P((tl_softc_t*, u_int32_t, u_int32_t)); 143 static u_int8_t tl_intreg_read_byte __P((tl_softc_t*, u_int32_t)); 144 static void tl_intreg_write_byte __P((tl_softc_t*, u_int32_t, u_int8_t)); 145 146 void tl_mii_sync __P((struct tl_softc *)); 147 void tl_mii_sendbits __P((struct tl_softc *, u_int32_t, int)); 148 149 150 #if defined(TLDEBUG_RX) 151 static void ether_printheader __P((struct ether_header*)); 152 #endif 153 154 int tl_mii_read __P((struct device *, int, int)); 155 void tl_mii_write __P((struct device *, int, int, int)); 156 157 void tl_statchg __P((struct device *)); 158 159 void tl_i2c_set __P((void*, u_int8_t)); 160 void tl_i2c_clr __P((void*, u_int8_t)); 161 int tl_i2c_read __P((void*, u_int8_t)); 162 163 static __inline void netsio_clr __P((tl_softc_t*, u_int8_t)); 164 static __inline void netsio_set __P((tl_softc_t*, u_int8_t)); 165 static __inline u_int8_t netsio_read __P((tl_softc_t*, u_int8_t)); 166 static __inline void netsio_clr(sc, bits) 167 tl_softc_t* sc; 168 u_int8_t bits; 169 { 170 tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetSio, 171 tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio) & (~bits)); 172 } 173 static __inline void netsio_set(sc, bits) 174 tl_softc_t* sc; 175 u_int8_t bits; 176 { 177 tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetSio, 178 tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio) | bits); 179 } 180 static __inline u_int8_t netsio_read(sc, bits) 181 tl_softc_t* sc; 182 u_int8_t bits; 183 { 184 return (tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio) & bits); 185 } 186 187 struct cfattach tl_ca = { 188 sizeof(tl_softc_t), tl_pci_match, tl_pci_attach 189 }; 190 191 const struct tl_product_desc tl_compaq_products[] = { 192 { PCI_PRODUCT_COMPAQ_N100TX, TLPHY_MEDIA_NO_10_T, 193 "Compaq Netelligent 10/100 TX" }, 194 { PCI_PRODUCT_COMPAQ_N10T, TLPHY_MEDIA_10_5, 195 "Compaq Netelligent 10 T" }, 196 { PCI_PRODUCT_COMPAQ_IntNF3P, TLPHY_MEDIA_10_2, 197 "Compaq Integrated NetFlex 3/P" }, 198 { PCI_PRODUCT_COMPAQ_IntPL100TX, TLPHY_MEDIA_10_2|TLPHY_MEDIA_NO_10_T, 199 "Compaq ProLiant Integrated Netelligent 10/100 TX" }, 200 { PCI_PRODUCT_COMPAQ_DPNet100TX, TLPHY_MEDIA_10_5|TLPHY_MEDIA_NO_10_T, 201 "Compaq Dual Port Netelligent 10/100 TX" }, 202 { PCI_PRODUCT_COMPAQ_DP4000, TLPHY_MEDIA_10_5|TLPHY_MEDIA_NO_10_T, 203 "Compaq Deskpro 4000 5233MMX" }, 204 { PCI_PRODUCT_COMPAQ_NF3P_BNC, TLPHY_MEDIA_10_2, 205 "Compaq NetFlex 3/P w/ BNC" }, 206 { PCI_PRODUCT_COMPAQ_NF3P, TLPHY_MEDIA_10_5, 207 "Compaq NetFlex 3/P" }, 208 { 0, 0, NULL }, 209 }; 210 211 const struct tl_product_desc tl_ti_products[] = { 212 /* 213 * Built-in Ethernet on the TI TravelMate 5000 214 * docking station; better product description? 215 */ 216 { PCI_PRODUCT_TI_TLAN, 0, 217 "Texas Instruments ThunderLAN" }, 218 { 0, 0, NULL }, 219 }; 220 221 struct tl_vendor_desc { 222 u_int32_t tv_vendor; 223 const struct tl_product_desc *tv_products; 224 }; 225 226 const struct tl_vendor_desc tl_vendors[] = { 227 { PCI_VENDOR_COMPAQ, tl_compaq_products }, 228 { PCI_VENDOR_TI, tl_ti_products }, 229 { 0, NULL }, 230 }; 231 232 const struct tl_product_desc *tl_lookup_product __P((u_int32_t)); 233 234 const struct tl_product_desc * 235 tl_lookup_product(id) 236 u_int32_t id; 237 { 238 const struct tl_product_desc *tp; 239 const struct tl_vendor_desc *tv; 240 241 for (tv = tl_vendors; tv->tv_products != NULL; tv++) 242 if (PCI_VENDOR(id) == tv->tv_vendor) 243 break; 244 245 if ((tp = tv->tv_products) == NULL) 246 return (NULL); 247 248 for (; tp->tp_desc != NULL; tp++) 249 if (PCI_PRODUCT(id) == tp->tp_product) 250 break; 251 252 if (tp->tp_desc == NULL) 253 return (NULL); 254 255 return (tp); 256 } 257 258 static int 259 tl_pci_match(parent, match, aux) 260 struct device *parent; 261 struct cfdata *match; 262 void *aux; 263 { 264 struct pci_attach_args *pa = (struct pci_attach_args *) aux; 265 266 if (tl_lookup_product(pa->pa_id) != NULL) 267 return (1); 268 269 return (0); 270 } 271 272 static void 273 tl_pci_attach(parent, self, aux) 274 struct device * parent; 275 struct device * self; 276 void * aux; 277 { 278 tl_softc_t *sc = (tl_softc_t *)self; 279 struct pci_attach_args * const pa = (struct pci_attach_args *) aux; 280 const struct tl_product_desc *tp; 281 struct ifnet * const ifp = &sc->tl_if; 282 bus_space_tag_t iot, memt; 283 bus_space_handle_t ioh, memh; 284 pci_intr_handle_t intrhandle; 285 const char *intrstr; 286 int i, tmp, ioh_valid, memh_valid; 287 int reg_io, reg_mem; 288 pcireg_t reg10, reg14; 289 pcireg_t csr; 290 291 printf("\n"); 292 293 callout_init(&sc->tl_tick_ch); 294 callout_init(&sc->tl_restart_ch); 295 296 tp = tl_lookup_product(pa->pa_id); 297 if (tp == NULL) 298 panic("tl_pci_attach: impossible"); 299 sc->tl_product = tp; 300 301 /* 302 * Map the card space. First we have to find the I/O and MEM 303 * registers. I/O is supposed to be at 0x10, MEM at 0x14, 304 * but some boards (Compaq Netflex 3/P PCI) seem to have it reversed. 305 * The ThunderLAN manual is not consistent about this either (there 306 * are both cases in code examples). 307 */ 308 reg10 = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x10); 309 reg14 = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x14); 310 if (PCI_MAPREG_TYPE(reg10) == PCI_MAPREG_TYPE_IO) 311 reg_io = 0x10; 312 else if (PCI_MAPREG_TYPE(reg14) == PCI_MAPREG_TYPE_IO) 313 reg_io = 0x14; 314 else 315 reg_io = 0; 316 if (PCI_MAPREG_TYPE(reg10) == PCI_MAPREG_TYPE_MEM) 317 reg_mem = 0x10; 318 else if (PCI_MAPREG_TYPE(reg14) == PCI_MAPREG_TYPE_MEM) 319 reg_mem = 0x14; 320 else 321 reg_mem = 0; 322 323 if (reg_io != 0) 324 ioh_valid = (pci_mapreg_map(pa, reg_io, PCI_MAPREG_TYPE_IO, 325 0, &iot, &ioh, NULL, NULL) == 0); 326 else 327 ioh_valid = 0; 328 if (reg_mem != 0) 329 memh_valid = (pci_mapreg_map(pa, PCI_CBMA, 330 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 331 0, &memt, &memh, NULL, NULL) == 0); 332 else 333 memh_valid = 0; 334 335 if (ioh_valid) { 336 sc->tl_bustag = iot; 337 sc->tl_bushandle = ioh; 338 } else if (memh_valid) { 339 sc->tl_bustag = memt; 340 sc->tl_bushandle = memh; 341 } else { 342 printf("%s: unable to map device registers\n", 343 sc->sc_dev.dv_xname); 344 return; 345 } 346 sc->tl_dmatag = pa->pa_dmat; 347 348 /* Enable the device. */ 349 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 350 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 351 csr | PCI_COMMAND_MASTER_ENABLE); 352 353 printf("%s: %s\n", sc->sc_dev.dv_xname, tp->tp_desc); 354 355 tl_reset(sc); 356 357 /* fill in the i2c struct */ 358 sc->i2cbus.adapter_softc = sc; 359 sc->i2cbus.set_bit = tl_i2c_set; 360 sc->i2cbus.clr_bit = tl_i2c_clr; 361 sc->i2cbus.read_bit = tl_i2c_read; 362 363 #ifdef TLDEBUG 364 printf("default values of INTreg: 0x%x\n", 365 tl_intreg_read(sc, TL_INT_Defaults)); 366 #endif 367 368 /* read mac addr */ 369 for (i=0; i<ETHER_ADDR_LEN; i++) { 370 tmp = i2c_eeprom_read(&sc->i2cbus, 0x83 + i); 371 if (tmp < 0) { 372 printf("%s: error reading Ethernet adress\n", 373 sc->sc_dev.dv_xname); 374 return; 375 } else { 376 sc->tl_enaddr[i] = tmp; 377 } 378 } 379 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname, 380 ether_sprintf(sc->tl_enaddr)); 381 382 /* Map and establish interrupts */ 383 if (pci_intr_map(pa, &intrhandle)) { 384 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname); 385 return; 386 } 387 intrstr = pci_intr_string(pa->pa_pc, intrhandle); 388 sc->tl_if.if_softc = sc; 389 sc->tl_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET, 390 tl_intr, sc); 391 if (sc->tl_ih == NULL) { 392 printf("%s: couldn't establish interrupt", 393 sc->sc_dev.dv_xname); 394 if (intrstr != NULL) 395 printf(" at %s", intrstr); 396 printf("\n"); 397 return; 398 } 399 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr); 400 401 /* init these pointers, so that tl_shutdown won't try to read them */ 402 sc->Rx_list = NULL; 403 sc->Tx_list = NULL; 404 405 /* allocate DMA-safe memory for control structs */ 406 if (bus_dmamem_alloc(sc->tl_dmatag, 407 PAGE_SIZE, 0, PAGE_SIZE, 408 &sc->ctrl_segs, 1, &sc->ctrl_nsegs, BUS_DMA_NOWAIT) != 0 || 409 bus_dmamem_map(sc->tl_dmatag, &sc->ctrl_segs, 410 sc->ctrl_nsegs, PAGE_SIZE, (caddr_t*)&sc->ctrl, 411 BUS_DMA_NOWAIT | BUS_DMA_COHERENT) != 0) { 412 printf("%s: can't allocate DMA memory for lists\n", 413 sc->sc_dev.dv_xname); 414 return; 415 } 416 /* 417 * Add shutdown hook so that DMA is disabled prior to reboot. Not 418 * doing 419 * reboot before the driver initializes. 420 */ 421 (void) shutdownhook_establish(tl_shutdown, ifp); 422 423 /* 424 * Initialize our media structures and probe the MII. 425 * 426 * Note that we don't care about the media instance. We 427 * are expecting to have multiple PHYs on the 10/100 cards, 428 * and on those cards we exclude the internal PHY from providing 429 * 10baseT. By ignoring the instance, it allows us to not have 430 * to specify it on the command line when switching media. 431 */ 432 sc->tl_mii.mii_ifp = ifp; 433 sc->tl_mii.mii_readreg = tl_mii_read; 434 sc->tl_mii.mii_writereg = tl_mii_write; 435 sc->tl_mii.mii_statchg = tl_statchg; 436 ifmedia_init(&sc->tl_mii.mii_media, IFM_IMASK, tl_mediachange, 437 tl_mediastatus); 438 mii_attach(self, &sc->tl_mii, 0xffffffff, MII_PHY_ANY, 439 MII_OFFSET_ANY, 0); 440 if (LIST_FIRST(&sc->tl_mii.mii_phys) == NULL) { 441 ifmedia_add(&sc->tl_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 442 ifmedia_set(&sc->tl_mii.mii_media, IFM_ETHER|IFM_NONE); 443 } else 444 ifmedia_set(&sc->tl_mii.mii_media, IFM_ETHER|IFM_AUTO); 445 446 strcpy(ifp->if_xname, sc->sc_dev.dv_xname); 447 ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST; 448 ifp->if_ioctl = tl_ifioctl; 449 ifp->if_start = tl_ifstart; 450 ifp->if_watchdog = tl_ifwatchdog; 451 ifp->if_init = tl_init; 452 ifp->if_stop = tl_stop; 453 ifp->if_timer = 0; 454 IFQ_SET_READY(&ifp->if_snd); 455 if_attach(ifp); 456 ether_ifattach(&(sc)->tl_if, (sc)->tl_enaddr); 457 } 458 459 static void 460 tl_reset(sc) 461 tl_softc_t *sc; 462 { 463 int i; 464 465 /* read stats */ 466 if (sc->tl_if.if_flags & IFF_RUNNING) { 467 callout_stop(&sc->tl_tick_ch); 468 tl_read_stats(sc); 469 } 470 /* Reset adapter */ 471 TL_HR_WRITE(sc, TL_HOST_CMD, 472 TL_HR_READ(sc, TL_HOST_CMD) | HOST_CMD_Ad_Rst); 473 DELAY(100000); 474 /* Disable interrupts */ 475 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff); 476 /* setup aregs & hash */ 477 for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4) 478 tl_intreg_write(sc, i, 0); 479 #ifdef TLDEBUG_ADDR 480 printf("Areg & hash registers: \n"); 481 for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4) 482 printf(" reg %x: %x\n", i, tl_intreg_read(sc, i)); 483 #endif 484 /* Setup NetConfig */ 485 tl_intreg_write(sc, TL_INT_NetConfig, 486 TL_NETCONFIG_1F | TL_NETCONFIG_1chn | TL_NETCONFIG_PHY_EN); 487 /* Bsize: accept default */ 488 /* TX commit in Acommit: accept default */ 489 /* Load Ld_tmr and Ld_thr */ 490 /* Ld_tmr = 3 */ 491 TL_HR_WRITE(sc, TL_HOST_CMD, 0x3 | HOST_CMD_LdTmr); 492 /* Ld_thr = 0 */ 493 TL_HR_WRITE(sc, TL_HOST_CMD, 0x0 | HOST_CMD_LdThr); 494 /* Unreset MII */ 495 netsio_set(sc, TL_NETSIO_NMRST); 496 DELAY(100000); 497 sc->tl_mii.mii_media_status &= ~IFM_ACTIVE; 498 } 499 500 static void tl_shutdown(v) 501 void *v; 502 { 503 tl_stop(v, 1); 504 } 505 506 static void tl_stop(ifp, disable) 507 struct ifnet *ifp; 508 int disable; 509 { 510 tl_softc_t *sc = ifp->if_softc; 511 struct Tx_list *Tx; 512 int i; 513 514 if ((ifp->if_flags & IFF_RUNNING) == 0) 515 return; 516 /* disable interrupts */ 517 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff); 518 /* stop TX and RX channels */ 519 TL_HR_WRITE(sc, TL_HOST_CMD, 520 HOST_CMD_STOP | HOST_CMD_RT | HOST_CMD_Nes); 521 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_STOP); 522 DELAY(100000); 523 524 /* stop statistics reading loop, read stats */ 525 callout_stop(&sc->tl_tick_ch); 526 tl_read_stats(sc); 527 528 /* Down the MII. */ 529 mii_down(&sc->tl_mii); 530 531 /* deallocate memory allocations */ 532 if (sc->Rx_list) { 533 for (i=0; i< TL_NBUF; i++) { 534 if (sc->Rx_list[i].m) { 535 bus_dmamap_unload(sc->tl_dmatag, 536 sc->Rx_list[i].m_dmamap); 537 m_freem(sc->Rx_list[i].m); 538 } 539 bus_dmamap_destroy(sc->tl_dmatag, 540 sc->Rx_list[i].m_dmamap); 541 sc->Rx_list[i].m = NULL; 542 } 543 free(sc->Rx_list, M_DEVBUF); 544 sc->Rx_list = NULL; 545 bus_dmamap_unload(sc->tl_dmatag, sc->Rx_dmamap); 546 bus_dmamap_destroy(sc->tl_dmatag, sc->Rx_dmamap); 547 sc->hw_Rx_list = NULL; 548 while ((Tx = sc->active_Tx) != NULL) { 549 Tx->hw_list->stat = 0; 550 bus_dmamap_unload(sc->tl_dmatag, Tx->m_dmamap); 551 bus_dmamap_destroy(sc->tl_dmatag, Tx->m_dmamap); 552 m_freem(Tx->m); 553 sc->active_Tx = Tx->next; 554 Tx->next = sc->Free_Tx; 555 sc->Free_Tx = Tx; 556 } 557 sc->last_Tx = NULL; 558 free(sc->Tx_list, M_DEVBUF); 559 sc->Tx_list = NULL; 560 bus_dmamap_unload(sc->tl_dmatag, sc->Tx_dmamap); 561 bus_dmamap_destroy(sc->tl_dmatag, sc->Tx_dmamap); 562 sc->hw_Tx_list = NULL; 563 } 564 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 565 ifp->if_timer = 0; 566 sc->tl_mii.mii_media_status &= ~IFM_ACTIVE; 567 } 568 569 static void tl_restart(v) 570 void *v; 571 { 572 tl_init(v); 573 } 574 575 static int tl_init(ifp) 576 struct ifnet *ifp; 577 { 578 tl_softc_t *sc = ifp->if_softc; 579 int i, s, error; 580 char *errstring; 581 char *nullbuf; 582 583 s = splnet(); 584 /* cancel any pending IO */ 585 tl_stop(ifp, 1); 586 tl_reset(sc); 587 if ((sc->tl_if.if_flags & IFF_UP) == 0) { 588 splx(s); 589 return 0; 590 } 591 /* Set various register to reasonable value */ 592 /* setup NetCmd in promisc mode if needed */ 593 i = (ifp->if_flags & IFF_PROMISC) ? TL_NETCOMMAND_CAF : 0; 594 tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetCmd, 595 TL_NETCOMMAND_NRESET | TL_NETCOMMAND_NWRAP | i); 596 /* Max receive size : MCLBYTES */ 597 tl_intreg_write_byte(sc, TL_INT_MISC + TL_MISC_MaxRxL, MCLBYTES & 0xff); 598 tl_intreg_write_byte(sc, TL_INT_MISC + TL_MISC_MaxRxH, 599 (MCLBYTES >> 8) & 0xff); 600 601 /* init MAC addr */ 602 for (i = 0; i < ETHER_ADDR_LEN; i++) 603 tl_intreg_write_byte(sc, TL_INT_Areg0 + i , sc->tl_enaddr[i]); 604 /* add multicast filters */ 605 tl_addr_filter(sc); 606 #ifdef TLDEBUG_ADDR 607 printf("Wrote Mac addr, Areg & hash registers are now: \n"); 608 for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4) 609 printf(" reg %x: %x\n", i, tl_intreg_read(sc, i)); 610 #endif 611 612 /* Pre-allocate receivers mbuf, make the lists */ 613 sc->Rx_list = malloc(sizeof(struct Rx_list) * TL_NBUF, M_DEVBUF, 614 M_NOWAIT|M_ZERO); 615 sc->Tx_list = malloc(sizeof(struct Tx_list) * TL_NBUF, M_DEVBUF, 616 M_NOWAIT|M_ZERO); 617 if (sc->Rx_list == NULL || sc->Tx_list == NULL) { 618 errstring = "out of memory for lists"; 619 error = ENOMEM; 620 goto bad; 621 } 622 error = bus_dmamap_create(sc->tl_dmatag, 623 sizeof(struct tl_Rx_list) * TL_NBUF, 1, 624 sizeof(struct tl_Rx_list) * TL_NBUF, 0, BUS_DMA_WAITOK, 625 &sc->Rx_dmamap); 626 if (error == 0) 627 error = bus_dmamap_create(sc->tl_dmatag, 628 sizeof(struct tl_Tx_list) * TL_NBUF, 1, 629 sizeof(struct tl_Tx_list) * TL_NBUF, 0, BUS_DMA_WAITOK, 630 &sc->Tx_dmamap); 631 if (error == 0) 632 error = bus_dmamap_create(sc->tl_dmatag, ETHER_MIN_TX, 1, 633 ETHER_MIN_TX, 0, BUS_DMA_WAITOK, 634 &sc->null_dmamap); 635 if (error) { 636 errstring = "can't allocate DMA maps for lists"; 637 goto bad; 638 } 639 memset(sc->ctrl, 0, PAGE_SIZE); 640 sc->hw_Rx_list = (void *)sc->ctrl; 641 sc->hw_Tx_list = 642 (void *)(sc->ctrl + sizeof(struct tl_Rx_list) * TL_NBUF); 643 nullbuf = sc->ctrl + sizeof(struct tl_Rx_list) * TL_NBUF + 644 sizeof(struct tl_Tx_list) * TL_NBUF; 645 error = bus_dmamap_load(sc->tl_dmatag, sc->Rx_dmamap, 646 sc->hw_Rx_list, sizeof(struct tl_Rx_list) * TL_NBUF, NULL, 647 BUS_DMA_WAITOK); 648 if (error == 0) 649 error = bus_dmamap_load(sc->tl_dmatag, sc->Tx_dmamap, 650 sc->hw_Tx_list, sizeof(struct tl_Tx_list) * TL_NBUF, NULL, 651 BUS_DMA_WAITOK); 652 if (error == 0) 653 error = bus_dmamap_load(sc->tl_dmatag, sc->null_dmamap, 654 nullbuf, ETHER_MIN_TX, NULL, BUS_DMA_WAITOK); 655 if (error) { 656 errstring = "can't DMA map DMA memory for lists"; 657 goto bad; 658 } 659 for (i=0; i< TL_NBUF; i++) { 660 error = bus_dmamap_create(sc->tl_dmatag, MCLBYTES, 661 1, MCLBYTES, 0, BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, 662 &sc->Rx_list[i].m_dmamap); 663 if (error == 0) { 664 error = bus_dmamap_create(sc->tl_dmatag, MCLBYTES, 665 TL_NSEG, MCLBYTES, 0, 666 BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, 667 &sc->Tx_list[i].m_dmamap); 668 } 669 if (error) { 670 errstring = "can't allocate DMA maps for mbufs"; 671 goto bad; 672 } 673 sc->Rx_list[i].hw_list = &sc->hw_Rx_list[i]; 674 sc->Rx_list[i].hw_listaddr = sc->Rx_dmamap->dm_segs[0].ds_addr 675 + sizeof(struct tl_Rx_list) * i; 676 sc->Tx_list[i].hw_list = &sc->hw_Tx_list[i]; 677 sc->Tx_list[i].hw_listaddr = sc->Tx_dmamap->dm_segs[0].ds_addr 678 + sizeof(struct tl_Tx_list) * i; 679 if (tl_add_RxBuff(sc, &sc->Rx_list[i], NULL) == 0) { 680 errstring = "out of mbuf for receive list"; 681 error = ENOMEM; 682 goto bad; 683 } 684 if (i > 0) { /* chain the list */ 685 sc->Rx_list[i-1].next = &sc->Rx_list[i]; 686 sc->hw_Rx_list[i-1].fwd = 687 htole32(sc->Rx_list[i].hw_listaddr); 688 sc->Tx_list[i-1].next = &sc->Tx_list[i]; 689 } 690 } 691 sc->hw_Rx_list[TL_NBUF-1].fwd = 0; 692 sc->Rx_list[TL_NBUF-1].next = NULL; 693 sc->hw_Tx_list[TL_NBUF-1].fwd = 0; 694 sc->Tx_list[TL_NBUF-1].next = NULL; 695 696 sc->active_Rx = &sc->Rx_list[0]; 697 sc->last_Rx = &sc->Rx_list[TL_NBUF-1]; 698 sc->active_Tx = sc->last_Tx = NULL; 699 sc->Free_Tx = &sc->Tx_list[0]; 700 bus_dmamap_sync(sc->tl_dmatag, sc->Rx_dmamap, 0, 701 sizeof(struct tl_Rx_list) * TL_NBUF, 702 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 703 bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0, 704 sizeof(struct tl_Tx_list) * TL_NBUF, 705 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 706 bus_dmamap_sync(sc->tl_dmatag, sc->null_dmamap, 0, ETHER_MIN_TX, 707 BUS_DMASYNC_PREWRITE); 708 709 /* set media */ 710 mii_mediachg(&sc->tl_mii); 711 712 /* start ticks calls */ 713 callout_reset(&sc->tl_tick_ch, hz, tl_ticks, sc); 714 /* write adress of Rx list and enable interrupts */ 715 TL_HR_WRITE(sc, TL_HOST_CH_PARM, sc->Rx_list[0].hw_listaddr); 716 TL_HR_WRITE(sc, TL_HOST_CMD, 717 HOST_CMD_GO | HOST_CMD_RT | HOST_CMD_Nes | HOST_CMD_IntOn); 718 sc->tl_if.if_flags |= IFF_RUNNING; 719 sc->tl_if.if_flags &= ~IFF_OACTIVE; 720 return 0; 721 bad: 722 printf("%s: %s\n", sc->sc_dev.dv_xname, errstring); 723 splx(s); 724 return error; 725 } 726 727 728 static u_int32_t 729 tl_intreg_read(sc, reg) 730 tl_softc_t *sc; 731 u_int32_t reg; 732 { 733 TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR, reg & TL_HOST_DIOADR_MASK); 734 return TL_HR_READ(sc, TL_HOST_DIO_DATA); 735 } 736 737 static u_int8_t 738 tl_intreg_read_byte(sc, reg) 739 tl_softc_t *sc; 740 u_int32_t reg; 741 { 742 TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR, 743 (reg & (~0x07)) & TL_HOST_DIOADR_MASK); 744 return TL_HR_READ_BYTE(sc, TL_HOST_DIO_DATA + (reg & 0x07)); 745 } 746 747 static void 748 tl_intreg_write(sc, reg, val) 749 tl_softc_t *sc; 750 u_int32_t reg; 751 u_int32_t val; 752 { 753 TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR, reg & TL_HOST_DIOADR_MASK); 754 TL_HR_WRITE(sc, TL_HOST_DIO_DATA, val); 755 } 756 757 static void 758 tl_intreg_write_byte(sc, reg, val) 759 tl_softc_t *sc; 760 u_int32_t reg; 761 u_int8_t val; 762 { 763 TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR, 764 (reg & (~0x03)) & TL_HOST_DIOADR_MASK); 765 TL_HR_WRITE_BYTE(sc, TL_HOST_DIO_DATA + (reg & 0x03), val); 766 } 767 768 void 769 tl_mii_sync(sc) 770 struct tl_softc *sc; 771 { 772 int i; 773 774 netsio_clr(sc, TL_NETSIO_MTXEN); 775 for (i = 0; i < 32; i++) { 776 netsio_clr(sc, TL_NETSIO_MCLK); 777 netsio_set(sc, TL_NETSIO_MCLK); 778 } 779 } 780 781 void 782 tl_mii_sendbits(sc, data, nbits) 783 struct tl_softc *sc; 784 u_int32_t data; 785 int nbits; 786 { 787 int i; 788 789 netsio_set(sc, TL_NETSIO_MTXEN); 790 for (i = 1 << (nbits - 1); i; i = i >> 1) { 791 netsio_clr(sc, TL_NETSIO_MCLK); 792 netsio_read(sc, TL_NETSIO_MCLK); 793 if (data & i) 794 netsio_set(sc, TL_NETSIO_MDATA); 795 else 796 netsio_clr(sc, TL_NETSIO_MDATA); 797 netsio_set(sc, TL_NETSIO_MCLK); 798 netsio_read(sc, TL_NETSIO_MCLK); 799 } 800 } 801 802 int 803 tl_mii_read(self, phy, reg) 804 struct device *self; 805 int phy, reg; 806 { 807 struct tl_softc *sc = (struct tl_softc *)self; 808 int val = 0, i, err; 809 810 /* 811 * Read the PHY register by manually driving the MII control lines. 812 */ 813 814 tl_mii_sync(sc); 815 tl_mii_sendbits(sc, MII_COMMAND_START, 2); 816 tl_mii_sendbits(sc, MII_COMMAND_READ, 2); 817 tl_mii_sendbits(sc, phy, 5); 818 tl_mii_sendbits(sc, reg, 5); 819 820 netsio_clr(sc, TL_NETSIO_MTXEN); 821 netsio_clr(sc, TL_NETSIO_MCLK); 822 netsio_set(sc, TL_NETSIO_MCLK); 823 netsio_clr(sc, TL_NETSIO_MCLK); 824 825 err = netsio_read(sc, TL_NETSIO_MDATA); 826 netsio_set(sc, TL_NETSIO_MCLK); 827 828 /* Even if an error occurs, must still clock out the cycle. */ 829 for (i = 0; i < 16; i++) { 830 val <<= 1; 831 netsio_clr(sc, TL_NETSIO_MCLK); 832 if (err == 0 && netsio_read(sc, TL_NETSIO_MDATA)) 833 val |= 1; 834 netsio_set(sc, TL_NETSIO_MCLK); 835 } 836 netsio_clr(sc, TL_NETSIO_MCLK); 837 netsio_set(sc, TL_NETSIO_MCLK); 838 839 return (err ? 0 : val); 840 } 841 842 void 843 tl_mii_write(self, phy, reg, val) 844 struct device *self; 845 int phy, reg, val; 846 { 847 struct tl_softc *sc = (struct tl_softc *)self; 848 849 /* 850 * Write the PHY register by manually driving the MII control lines. 851 */ 852 853 tl_mii_sync(sc); 854 tl_mii_sendbits(sc, MII_COMMAND_START, 2); 855 tl_mii_sendbits(sc, MII_COMMAND_WRITE, 2); 856 tl_mii_sendbits(sc, phy, 5); 857 tl_mii_sendbits(sc, reg, 5); 858 tl_mii_sendbits(sc, MII_COMMAND_ACK, 2); 859 tl_mii_sendbits(sc, val, 16); 860 861 netsio_clr(sc, TL_NETSIO_MCLK); 862 netsio_set(sc, TL_NETSIO_MCLK); 863 } 864 865 void 866 tl_statchg(self) 867 struct device *self; 868 { 869 tl_softc_t *sc = (struct tl_softc *)self; 870 u_int32_t reg; 871 872 #ifdef TLDEBUG 873 printf("tl_statchg, media %x\n", sc->tl_ifmedia.ifm_media); 874 #endif 875 876 /* 877 * We must keep the ThunderLAN and the PHY in sync as 878 * to the status of full-duplex! 879 */ 880 reg = tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetCmd); 881 if (sc->tl_mii.mii_media_active & IFM_FDX) 882 reg |= TL_NETCOMMAND_DUPLEX; 883 else 884 reg &= ~TL_NETCOMMAND_DUPLEX; 885 tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetCmd, reg); 886 } 887 888 void tl_i2c_set(v, bit) 889 void *v; 890 u_int8_t bit; 891 { 892 tl_softc_t *sc = v; 893 894 switch (bit) { 895 case I2C_DATA: 896 netsio_set(sc, TL_NETSIO_EDATA); 897 break; 898 case I2C_CLOCK: 899 netsio_set(sc, TL_NETSIO_ECLOCK); 900 break; 901 case I2C_TXEN: 902 netsio_set(sc, TL_NETSIO_ETXEN); 903 break; 904 default: 905 printf("tl_i2c_set: unknown bit %d\n", bit); 906 } 907 return; 908 } 909 910 void tl_i2c_clr(v, bit) 911 void *v; 912 u_int8_t bit; 913 { 914 tl_softc_t *sc = v; 915 916 switch (bit) { 917 case I2C_DATA: 918 netsio_clr(sc, TL_NETSIO_EDATA); 919 break; 920 case I2C_CLOCK: 921 netsio_clr(sc, TL_NETSIO_ECLOCK); 922 break; 923 case I2C_TXEN: 924 netsio_clr(sc, TL_NETSIO_ETXEN); 925 break; 926 default: 927 printf("tl_i2c_clr: unknown bit %d\n", bit); 928 } 929 return; 930 } 931 932 int tl_i2c_read(v, bit) 933 void *v; 934 u_int8_t bit; 935 { 936 tl_softc_t *sc = v; 937 938 switch (bit) { 939 case I2C_DATA: 940 return netsio_read(sc, TL_NETSIO_EDATA); 941 break; 942 case I2C_CLOCK: 943 return netsio_read(sc, TL_NETSIO_ECLOCK); 944 break; 945 case I2C_TXEN: 946 return netsio_read(sc, TL_NETSIO_ETXEN); 947 break; 948 default: 949 printf("tl_i2c_read: unknown bit %d\n", bit); 950 return -1; 951 } 952 } 953 954 static int 955 tl_intr(v) 956 void *v; 957 { 958 tl_softc_t *sc = v; 959 struct ifnet *ifp = &sc->tl_if; 960 struct Rx_list *Rx; 961 struct Tx_list *Tx; 962 struct mbuf *m; 963 u_int32_t int_type, int_reg; 964 int ack = 0; 965 int size; 966 967 int_reg = TL_HR_READ(sc, TL_HOST_INTR_DIOADR); 968 int_type = int_reg & TL_INTR_MASK; 969 if (int_type == 0) 970 return 0; 971 #if defined(TLDEBUG_RX) || defined(TLDEBUG_TX) 972 printf("%s: interrupt type %x, intr_reg %x\n", sc->sc_dev.dv_xname, 973 int_type, int_reg); 974 #endif 975 /* disable interrupts */ 976 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff); 977 switch(int_type & TL_INTR_MASK) { 978 case TL_INTR_RxEOF: 979 bus_dmamap_sync(sc->tl_dmatag, sc->Rx_dmamap, 0, 980 sizeof(struct tl_Rx_list) * TL_NBUF, 981 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 982 while(le32toh(sc->active_Rx->hw_list->stat) & 983 TL_RX_CSTAT_CPLT) { 984 /* dequeue and requeue at end of list */ 985 ack++; 986 Rx = sc->active_Rx; 987 sc->active_Rx = Rx->next; 988 bus_dmamap_sync(sc->tl_dmatag, Rx->m_dmamap, 0, 989 MCLBYTES, BUS_DMASYNC_POSTREAD); 990 bus_dmamap_unload(sc->tl_dmatag, Rx->m_dmamap); 991 m = Rx->m; 992 size = le32toh(Rx->hw_list->stat) >> 16; 993 #ifdef TLDEBUG_RX 994 printf("tl_intr: RX list complete, Rx %p, size=%d\n", 995 Rx, size); 996 #endif 997 if (tl_add_RxBuff(sc, Rx, m ) == 0) { 998 /* 999 * No new mbuf, reuse the same. This means 1000 * that this packet 1001 * is lost 1002 */ 1003 m = NULL; 1004 #ifdef TL_PRIV_STATS 1005 sc->ierr_nomem++; 1006 #endif 1007 #ifdef TLDEBUG 1008 printf("%s: out of mbuf, lost input packet\n", 1009 sc->sc_dev.dv_xname); 1010 #endif 1011 } 1012 Rx->next = NULL; 1013 Rx->hw_list->fwd = 0; 1014 sc->last_Rx->hw_list->fwd = htole32(Rx->hw_listaddr); 1015 sc->last_Rx->next = Rx; 1016 sc->last_Rx = Rx; 1017 1018 /* deliver packet */ 1019 if (m) { 1020 if (size < sizeof(struct ether_header)) { 1021 m_freem(m); 1022 continue; 1023 } 1024 m->m_pkthdr.rcvif = ifp; 1025 m->m_pkthdr.len = m->m_len = size; 1026 #ifdef TLDEBUG_RX 1027 { struct ether_header *eh = 1028 mtod(m, struct ether_header *); 1029 printf("tl_intr: Rx packet:\n"); 1030 ether_printheader(eh); } 1031 #endif 1032 #if NBPFILTER > 0 1033 if (ifp->if_bpf) 1034 bpf_mtap(ifp->if_bpf, m); 1035 #endif /* NBPFILTER > 0 */ 1036 (*ifp->if_input)(ifp, m); 1037 } 1038 } 1039 bus_dmamap_sync(sc->tl_dmatag, sc->Rx_dmamap, 0, 1040 sizeof(struct tl_Rx_list) * TL_NBUF, 1041 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1042 #ifdef TLDEBUG_RX 1043 printf("TL_INTR_RxEOF: ack %d\n", ack); 1044 #else 1045 if (ack == 0) { 1046 printf("%s: EOF intr without anything to read !\n", 1047 sc->sc_dev.dv_xname); 1048 tl_reset(sc); 1049 /* shedule reinit of the board */ 1050 callout_reset(&sc->tl_restart_ch, 1, tl_restart, sc); 1051 return(1); 1052 } 1053 #endif 1054 break; 1055 case TL_INTR_RxEOC: 1056 ack++; 1057 bus_dmamap_sync(sc->tl_dmatag, sc->Rx_dmamap, 0, 1058 sizeof(struct tl_Rx_list) * TL_NBUF, 1059 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1060 #ifdef TLDEBUG_RX 1061 printf("TL_INTR_RxEOC: ack %d\n", ack); 1062 #endif 1063 #ifdef DIAGNOSTIC 1064 if (le32toh(sc->active_Rx->hw_list->stat) & TL_RX_CSTAT_CPLT) { 1065 printf("%s: Rx EOC interrupt and active Tx list not " 1066 "cleared\n", sc->sc_dev.dv_xname); 1067 return 0; 1068 } else 1069 #endif 1070 { 1071 /* 1072 * write adress of Rx list and send Rx GO command, ack 1073 * interrupt and enable interrupts in one command 1074 */ 1075 TL_HR_WRITE(sc, TL_HOST_CH_PARM, sc->active_Rx->hw_listaddr); 1076 TL_HR_WRITE(sc, TL_HOST_CMD, 1077 HOST_CMD_GO | HOST_CMD_RT | HOST_CMD_Nes | ack | int_type | 1078 HOST_CMD_ACK | HOST_CMD_IntOn); 1079 return 1; 1080 } 1081 case TL_INTR_TxEOF: 1082 case TL_INTR_TxEOC: 1083 bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0, 1084 sizeof(struct tl_Tx_list) * TL_NBUF, 1085 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1086 while ((Tx = sc->active_Tx) != NULL) { 1087 if((le32toh(Tx->hw_list->stat) & TL_TX_CSTAT_CPLT) == 0) 1088 break; 1089 ack++; 1090 #ifdef TLDEBUG_TX 1091 printf("TL_INTR_TxEOC: list 0x%x done\n", 1092 (int)Tx->hw_listaddr); 1093 #endif 1094 Tx->hw_list->stat = 0; 1095 bus_dmamap_sync(sc->tl_dmatag, Tx->m_dmamap, 0, 1096 MCLBYTES, BUS_DMASYNC_POSTWRITE); 1097 bus_dmamap_unload(sc->tl_dmatag, Tx->m_dmamap); 1098 m_freem(Tx->m); 1099 Tx->m = NULL; 1100 sc->active_Tx = Tx->next; 1101 if (sc->active_Tx == NULL) 1102 sc->last_Tx = NULL; 1103 Tx->next = sc->Free_Tx; 1104 sc->Free_Tx = Tx; 1105 } 1106 bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0, 1107 sizeof(struct tl_Tx_list) * TL_NBUF, 1108 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1109 /* if this was an EOC, ACK immediatly */ 1110 if (ack) 1111 sc->tl_if.if_flags &= ~IFF_OACTIVE; 1112 if (int_type == TL_INTR_TxEOC) { 1113 #ifdef TLDEBUG_TX 1114 printf("TL_INTR_TxEOC: ack %d (will be set to 1)\n", 1115 ack); 1116 #endif 1117 TL_HR_WRITE(sc, TL_HOST_CMD, 1 | int_type | 1118 HOST_CMD_ACK | HOST_CMD_IntOn); 1119 if ( sc->active_Tx != NULL) { 1120 /* needs a Tx go command */ 1121 TL_HR_WRITE(sc, TL_HOST_CH_PARM, 1122 sc->active_Tx->hw_listaddr); 1123 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_GO); 1124 } 1125 sc->tl_if.if_timer = 0; 1126 if (IFQ_IS_EMPTY(&sc->tl_if.if_snd) == 0) 1127 tl_ifstart(&sc->tl_if); 1128 return 1; 1129 } 1130 #ifdef TLDEBUG 1131 else { 1132 printf("TL_INTR_TxEOF: ack %d\n", ack); 1133 } 1134 #endif 1135 sc->tl_if.if_timer = 0; 1136 if (IFQ_IS_EMPTY(&sc->tl_if.if_snd) == 0) 1137 tl_ifstart(&sc->tl_if); 1138 break; 1139 case TL_INTR_Stat: 1140 ack++; 1141 #ifdef TLDEBUG 1142 printf("TL_INTR_Stat: ack %d\n", ack); 1143 #endif 1144 tl_read_stats(sc); 1145 break; 1146 case TL_INTR_Adc: 1147 if (int_reg & TL_INTVec_MASK) { 1148 /* adapter check conditions */ 1149 printf("%s: check condition, intvect=0x%x, " 1150 "ch_param=0x%x\n", sc->sc_dev.dv_xname, 1151 int_reg & TL_INTVec_MASK, 1152 TL_HR_READ(sc, TL_HOST_CH_PARM)); 1153 tl_reset(sc); 1154 /* shedule reinit of the board */ 1155 callout_reset(&sc->tl_restart_ch, 1, tl_restart, sc); 1156 return(1); 1157 } else { 1158 u_int8_t netstat; 1159 /* Network status */ 1160 netstat = 1161 tl_intreg_read_byte(sc, TL_INT_NET+TL_INT_NetSts); 1162 printf("%s: network status, NetSts=%x\n", 1163 sc->sc_dev.dv_xname, netstat); 1164 /* Ack interrupts */ 1165 tl_intreg_write_byte(sc, TL_INT_NET+TL_INT_NetSts, 1166 netstat); 1167 ack++; 1168 } 1169 break; 1170 default: 1171 printf("%s: unhandled interrupt code %x!\n", 1172 sc->sc_dev.dv_xname, int_type); 1173 ack++; 1174 } 1175 1176 if (ack) { 1177 /* Ack the interrupt and enable interrupts */ 1178 TL_HR_WRITE(sc, TL_HOST_CMD, ack | int_type | HOST_CMD_ACK | 1179 HOST_CMD_IntOn); 1180 return 1; 1181 } 1182 /* ack = 0 ; interrupt was perhaps not our. Just enable interrupts */ 1183 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOn); 1184 return 0; 1185 } 1186 1187 static int 1188 tl_ifioctl(ifp, cmd, data) 1189 struct ifnet *ifp; 1190 ioctl_cmd_t cmd; 1191 caddr_t data; 1192 { 1193 struct tl_softc *sc = ifp->if_softc; 1194 struct ifreq *ifr = (struct ifreq *)data; 1195 int s, error; 1196 1197 s = splnet(); 1198 switch(cmd) { 1199 case SIOCSIFMEDIA: 1200 case SIOCGIFMEDIA: 1201 error = ifmedia_ioctl(ifp, ifr, &sc->tl_mii.mii_media, cmd); 1202 break; 1203 default: 1204 error = ether_ioctl(ifp, cmd, data); 1205 if (error == ENETRESET) { 1206 tl_addr_filter(sc); 1207 error = 0; 1208 } 1209 } 1210 splx(s); 1211 return error; 1212 } 1213 1214 static void 1215 tl_ifstart(ifp) 1216 struct ifnet *ifp; 1217 { 1218 tl_softc_t *sc = ifp->if_softc; 1219 struct mbuf *mb_head; 1220 struct Tx_list *Tx; 1221 int segment, size; 1222 int again, error; 1223 1224 if ((sc->tl_if.if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) 1225 return; 1226 txloop: 1227 /* If we don't have more space ... */ 1228 if (sc->Free_Tx == NULL) { 1229 #ifdef TLDEBUG 1230 printf("tl_ifstart: No free TX list\n"); 1231 #endif 1232 sc->tl_if.if_flags |= IFF_OACTIVE; 1233 return; 1234 } 1235 /* Grab a paquet for output */ 1236 IFQ_DEQUEUE(&ifp->if_snd, mb_head); 1237 if (mb_head == NULL) { 1238 #ifdef TLDEBUG_TX 1239 printf("tl_ifstart: nothing to send\n"); 1240 #endif 1241 return; 1242 } 1243 Tx = sc->Free_Tx; 1244 sc->Free_Tx = Tx->next; 1245 Tx->next = NULL; 1246 again = 0; 1247 /* 1248 * Go through each of the mbufs in the chain and initialize 1249 * the transmit list descriptors with the physical address 1250 * and size of the mbuf. 1251 */ 1252 tbdinit: 1253 memset(Tx->hw_list, 0, sizeof(struct tl_Tx_list)); 1254 Tx->m = mb_head; 1255 size = mb_head->m_pkthdr.len; 1256 if ((error = bus_dmamap_load_mbuf(sc->tl_dmatag, Tx->m_dmamap, mb_head, 1257 BUS_DMA_NOWAIT)) || (size < ETHER_MIN_TX && 1258 Tx->m_dmamap->dm_nsegs == TL_NSEG)) { 1259 struct mbuf *mn; 1260 /* 1261 * We ran out of segments, or we will. We have to recopy this 1262 * mbuf chain first. 1263 */ 1264 if (error == 0) 1265 bus_dmamap_unload(sc->tl_dmatag, Tx->m_dmamap); 1266 if (again) { 1267 /* already copyed, can't do much more */ 1268 m_freem(mb_head); 1269 goto bad; 1270 } 1271 again = 1; 1272 #ifdef TLDEBUG_TX 1273 printf("tl_ifstart: need to copy mbuf\n"); 1274 #endif 1275 #ifdef TL_PRIV_STATS 1276 sc->oerr_mcopy++; 1277 #endif 1278 MGETHDR(mn, M_DONTWAIT, MT_DATA); 1279 if (mn == NULL) { 1280 m_freem(mb_head); 1281 goto bad; 1282 } 1283 if (mb_head->m_pkthdr.len > MHLEN) { 1284 MCLGET(mn, M_DONTWAIT); 1285 if ((mn->m_flags & M_EXT) == 0) { 1286 m_freem(mn); 1287 m_freem(mb_head); 1288 goto bad; 1289 } 1290 } 1291 m_copydata(mb_head, 0, mb_head->m_pkthdr.len, 1292 mtod(mn, caddr_t)); 1293 mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len; 1294 m_freem(mb_head); 1295 mb_head = mn; 1296 goto tbdinit; 1297 } 1298 for (segment = 0; segment < Tx->m_dmamap->dm_nsegs; segment++) { 1299 Tx->hw_list->seg[segment].data_addr = 1300 htole32(Tx->m_dmamap->dm_segs[segment].ds_addr); 1301 Tx->hw_list->seg[segment].data_count = 1302 htole32(Tx->m_dmamap->dm_segs[segment].ds_len); 1303 } 1304 bus_dmamap_sync(sc->tl_dmatag, Tx->m_dmamap, 0, size, 1305 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1306 /* We are at end of mbuf chain. check the size and 1307 * see if it needs to be extended 1308 */ 1309 if (size < ETHER_MIN_TX) { 1310 #ifdef DIAGNOSTIC 1311 if (segment >= TL_NSEG) { 1312 panic("tl_ifstart: to much segmets (%d)\n", segment); 1313 } 1314 #endif 1315 /* 1316 * add the nullbuf in the seg 1317 */ 1318 Tx->hw_list->seg[segment].data_count = 1319 htole32(ETHER_MIN_TX - size); 1320 Tx->hw_list->seg[segment].data_addr = 1321 htole32(sc->null_dmamap->dm_segs[0].ds_addr); 1322 size = ETHER_MIN_TX; 1323 segment++; 1324 } 1325 /* The list is done, finish the list init */ 1326 Tx->hw_list->seg[segment-1].data_count |= 1327 htole32(TL_LAST_SEG); 1328 Tx->hw_list->stat = htole32((size << 16) | 0x3000); 1329 #ifdef TLDEBUG_TX 1330 printf("%s: sending, Tx : stat = 0x%x\n", sc->sc_dev.dv_xname, 1331 le32toh(Tx->hw_list->stat)); 1332 #if 0 1333 for(segment = 0; segment < TL_NSEG; segment++) { 1334 printf(" seg %d addr 0x%x len 0x%x\n", 1335 segment, 1336 le32toh(Tx->hw_list->seg[segment].data_addr), 1337 le32toh(Tx->hw_list->seg[segment].data_count)); 1338 } 1339 #endif 1340 #endif 1341 if (sc->active_Tx == NULL) { 1342 sc->active_Tx = sc->last_Tx = Tx; 1343 #ifdef TLDEBUG_TX 1344 printf("%s: Tx GO, addr=0x%ux\n", sc->sc_dev.dv_xname, 1345 (int)Tx->hw_listaddr); 1346 #endif 1347 bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0, 1348 sizeof(struct tl_Tx_list) * TL_NBUF, 1349 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1350 TL_HR_WRITE(sc, TL_HOST_CH_PARM, Tx->hw_listaddr); 1351 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_GO); 1352 } else { 1353 #ifdef TLDEBUG_TX 1354 printf("%s: Tx addr=0x%ux queued\n", sc->sc_dev.dv_xname, 1355 (int)Tx->hw_listaddr); 1356 #endif 1357 sc->last_Tx->hw_list->fwd = htole32(Tx->hw_listaddr); 1358 bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0, 1359 sizeof(struct tl_Tx_list) * TL_NBUF, 1360 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1361 sc->last_Tx->next = Tx; 1362 sc->last_Tx = Tx; 1363 #ifdef DIAGNOSTIC 1364 if (sc->last_Tx->hw_list->fwd & 0x7) 1365 printf("%s: physical addr 0x%x of list not properly " 1366 "aligned\n", 1367 sc->sc_dev.dv_xname, sc->last_Rx->hw_list->fwd); 1368 #endif 1369 } 1370 #if NBPFILTER > 0 1371 /* Pass packet to bpf if there is a listener */ 1372 if (ifp->if_bpf) 1373 bpf_mtap(ifp->if_bpf, mb_head); 1374 #endif 1375 /* 1376 * Set a 5 second timer just in case we don't hear from the card again. 1377 */ 1378 ifp->if_timer = 5; 1379 goto txloop; 1380 bad: 1381 #ifdef TLDEBUG 1382 printf("tl_ifstart: Out of mbuf, Tx pkt lost\n"); 1383 #endif 1384 Tx->next = sc->Free_Tx; 1385 sc->Free_Tx = Tx; 1386 return; 1387 } 1388 1389 static void 1390 tl_ifwatchdog(ifp) 1391 struct ifnet *ifp; 1392 { 1393 tl_softc_t *sc = ifp->if_softc; 1394 1395 if ((ifp->if_flags & IFF_RUNNING) == 0) 1396 return; 1397 printf("%s: device timeout\n", sc->sc_dev.dv_xname); 1398 ifp->if_oerrors++; 1399 tl_init(ifp); 1400 } 1401 1402 static int 1403 tl_mediachange(ifp) 1404 struct ifnet *ifp; 1405 { 1406 1407 if (ifp->if_flags & IFF_UP) 1408 tl_init(ifp); 1409 return (0); 1410 } 1411 1412 static void 1413 tl_mediastatus(ifp, ifmr) 1414 struct ifnet *ifp; 1415 struct ifmediareq *ifmr; 1416 { 1417 tl_softc_t *sc = ifp->if_softc; 1418 1419 mii_pollstat(&sc->tl_mii); 1420 ifmr->ifm_active = sc->tl_mii.mii_media_active; 1421 ifmr->ifm_status = sc->tl_mii.mii_media_status; 1422 } 1423 1424 static int tl_add_RxBuff(sc, Rx, oldm) 1425 tl_softc_t *sc; 1426 struct Rx_list *Rx; 1427 struct mbuf *oldm; 1428 { 1429 struct mbuf *m; 1430 int error; 1431 1432 MGETHDR(m, M_DONTWAIT, MT_DATA); 1433 if (m != NULL) { 1434 MCLGET(m, M_DONTWAIT); 1435 if ((m->m_flags & M_EXT) == 0) { 1436 m_freem(m); 1437 if (oldm == NULL) 1438 return 0; 1439 m = oldm; 1440 m->m_data = m->m_ext.ext_buf; 1441 } 1442 } else { 1443 if (oldm == NULL) 1444 return 0; 1445 m = oldm; 1446 m->m_data = m->m_ext.ext_buf; 1447 } 1448 1449 /* (re)init the Rx_list struct */ 1450 1451 Rx->m = m; 1452 if ((error = bus_dmamap_load(sc->tl_dmatag, Rx->m_dmamap, 1453 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT)) != 0) { 1454 printf("%s: bus_dmamap_load() failed (error %d) for " 1455 "tl_add_RxBuff\n", sc->sc_dev.dv_xname, error); 1456 printf("size %d (%d)\n", m->m_pkthdr.len, MCLBYTES); 1457 m_freem(m); 1458 Rx->m = NULL; 1459 return 0; 1460 } 1461 bus_dmamap_sync(sc->tl_dmatag, Rx->m_dmamap, 0, 1462 MCLBYTES, BUS_DMASYNC_PREREAD); 1463 /* 1464 * Move the data pointer up so that the incoming data packet 1465 * will be 32-bit aligned. 1466 */ 1467 m->m_data += 2; 1468 1469 Rx->hw_list->stat = 1470 htole32(((Rx->m_dmamap->dm_segs[0].ds_len -2) << 16) | 0x3000); 1471 Rx->hw_list->seg.data_count = 1472 htole32(Rx->m_dmamap->dm_segs[0].ds_len -2); 1473 Rx->hw_list->seg.data_addr = 1474 htole32(Rx->m_dmamap->dm_segs[0].ds_addr + 2); 1475 return (m != oldm); 1476 } 1477 1478 static void tl_ticks(v) 1479 void *v; 1480 { 1481 tl_softc_t *sc = v; 1482 1483 tl_read_stats(sc); 1484 1485 /* Tick the MII. */ 1486 mii_tick(&sc->tl_mii); 1487 1488 /* read statistics every seconds */ 1489 callout_reset(&sc->tl_tick_ch, hz, tl_ticks, sc); 1490 } 1491 1492 static void 1493 tl_read_stats(sc) 1494 tl_softc_t *sc; 1495 { 1496 u_int32_t reg; 1497 int ierr_overr; 1498 int ierr_code; 1499 int ierr_crc; 1500 int oerr_underr; 1501 int oerr_deffered; 1502 int oerr_coll; 1503 int oerr_multicoll; 1504 int oerr_exesscoll; 1505 int oerr_latecoll; 1506 int oerr_carrloss; 1507 struct ifnet *ifp = &sc->tl_if; 1508 1509 reg = tl_intreg_read(sc, TL_INT_STATS_TX); 1510 ifp->if_opackets += reg & 0x00ffffff; 1511 oerr_underr = reg >> 24; 1512 1513 reg = tl_intreg_read(sc, TL_INT_STATS_RX); 1514 ifp->if_ipackets += reg & 0x00ffffff; 1515 ierr_overr = reg >> 24; 1516 1517 reg = tl_intreg_read(sc, TL_INT_STATS_FERR); 1518 ierr_crc = (reg & TL_FERR_CRC) >> 16; 1519 ierr_code = (reg & TL_FERR_CODE) >> 24; 1520 oerr_deffered = (reg & TL_FERR_DEF); 1521 1522 reg = tl_intreg_read(sc, TL_INT_STATS_COLL); 1523 oerr_multicoll = (reg & TL_COL_MULTI); 1524 oerr_coll = (reg & TL_COL_SINGLE) >> 16; 1525 1526 reg = tl_intreg_read(sc, TL_INT_LERR); 1527 oerr_exesscoll = (reg & TL_LERR_ECOLL); 1528 oerr_latecoll = (reg & TL_LERR_LCOLL) >> 8; 1529 oerr_carrloss = (reg & TL_LERR_CL) >> 16; 1530 1531 1532 ifp->if_oerrors += oerr_underr + oerr_exesscoll + oerr_latecoll + 1533 oerr_carrloss; 1534 ifp->if_collisions += oerr_coll + oerr_multicoll; 1535 ifp->if_ierrors += ierr_overr + ierr_code + ierr_crc; 1536 1537 if (ierr_overr) 1538 printf("%s: receiver ring buffer overrun\n", 1539 sc->sc_dev.dv_xname); 1540 if (oerr_underr) 1541 printf("%s: transmit buffer underrun\n", 1542 sc->sc_dev.dv_xname); 1543 #ifdef TL_PRIV_STATS 1544 sc->ierr_overr += ierr_overr; 1545 sc->ierr_code += ierr_code; 1546 sc->ierr_crc += ierr_crc; 1547 sc->oerr_underr += oerr_underr; 1548 sc->oerr_deffered += oerr_deffered; 1549 sc->oerr_coll += oerr_coll; 1550 sc->oerr_multicoll += oerr_multicoll; 1551 sc->oerr_exesscoll += oerr_exesscoll; 1552 sc->oerr_latecoll += oerr_latecoll; 1553 sc->oerr_carrloss += oerr_carrloss; 1554 #endif 1555 } 1556 1557 static void tl_addr_filter(sc) 1558 tl_softc_t *sc; 1559 { 1560 struct ether_multistep step; 1561 struct ether_multi *enm; 1562 u_int32_t hash[2] = {0, 0}; 1563 int i; 1564 1565 sc->tl_if.if_flags &= ~IFF_ALLMULTI; 1566 ETHER_FIRST_MULTI(step, &sc->tl_ec, enm); 1567 while (enm != NULL) { 1568 #ifdef TLDEBUG 1569 printf("tl_addr_filter: addrs %s %s\n", 1570 ether_sprintf(enm->enm_addrlo), 1571 ether_sprintf(enm->enm_addrhi)); 1572 #endif 1573 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) == 0) { 1574 i = tl_multicast_hash(enm->enm_addrlo); 1575 hash[i/32] |= 1 << (i%32); 1576 } else { 1577 hash[0] = hash[1] = 0xffffffff; 1578 sc->tl_if.if_flags |= IFF_ALLMULTI; 1579 break; 1580 } 1581 ETHER_NEXT_MULTI(step, enm); 1582 } 1583 #ifdef TLDEBUG 1584 printf("tl_addr_filer: hash1 %x has2 %x\n", hash[0], hash[1]); 1585 #endif 1586 tl_intreg_write(sc, TL_INT_HASH1, hash[0]); 1587 tl_intreg_write(sc, TL_INT_HASH2, hash[1]); 1588 } 1589 1590 static int tl_multicast_hash(a) 1591 u_int8_t *a; 1592 { 1593 int hash; 1594 1595 #define DA(addr,bit) (addr[5 - (bit/8)] & (1 << bit%8)) 1596 #define xor8(a,b,c,d,e,f,g,h) (((a != 0) + (b != 0) + (c != 0) + (d != 0) + (e != 0) + (f != 0) + (g != 0) + (h != 0)) & 1) 1597 1598 hash = xor8( DA(a,0), DA(a, 6), DA(a,12), DA(a,18), DA(a,24), DA(a,30), 1599 DA(a,36), DA(a,42)); 1600 hash |= xor8( DA(a,1), DA(a, 7), DA(a,13), DA(a,19), DA(a,25), DA(a,31), 1601 DA(a,37), DA(a,43)) << 1; 1602 hash |= xor8( DA(a,2), DA(a, 8), DA(a,14), DA(a,20), DA(a,26), DA(a,32), 1603 DA(a,38), DA(a,44)) << 2; 1604 hash |= xor8( DA(a,3), DA(a, 9), DA(a,15), DA(a,21), DA(a,27), DA(a,33), 1605 DA(a,39), DA(a,45)) << 3; 1606 hash |= xor8( DA(a,4), DA(a,10), DA(a,16), DA(a,22), DA(a,28), DA(a,34), 1607 DA(a,40), DA(a,46)) << 4; 1608 hash |= xor8( DA(a,5), DA(a,11), DA(a,17), DA(a,23), DA(a,29), DA(a,35), 1609 DA(a,41), DA(a,47)) << 5; 1610 1611 return hash; 1612 } 1613 1614 #if defined(TLDEBUG_RX) 1615 void 1616 ether_printheader(eh) 1617 struct ether_header *eh; 1618 { 1619 u_char *c = (char*)eh; 1620 int i; 1621 for (i=0; i<sizeof(struct ether_header); i++) 1622 printf("%x ", (u_int)c[i]); 1623 printf("\n"); 1624 } 1625 #endif 1626