1 /*- 2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTABILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /* 34 * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver. 35 */ 36 #include "opt_ath.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 #include <sys/kernel.h> 43 #include <sys/lock.h> 44 #include <sys/errno.h> 45 46 #if defined(__DragonFly__) 47 /* empty */ 48 #else 49 #include <machine/bus.h> 50 #include <machine/resource.h> 51 #endif 52 #include <sys/bus.h> 53 #include <sys/rman.h> 54 55 #include <sys/socket.h> 56 57 #include <net/if.h> 58 #include <net/if_media.h> 59 #include <net/if_arp.h> 60 #include <net/ethernet.h> 61 62 #include <netproto/802_11/ieee80211_var.h> 63 64 #include <dev/netif/ath/ath/if_athvar.h> 65 66 #if defined(__DragonFly__) 67 #include <bus/pci/pcivar.h> 68 #include <bus/pci/pcireg.h> 69 #else 70 #include <dev/pci/pcivar.h> 71 #include <dev/pci/pcireg.h> 72 #endif 73 74 /* For EEPROM firmware */ 75 #ifdef ATH_EEPROM_FIRMWARE 76 #include <sys/linker.h> 77 #include <sys/firmware.h> 78 #endif /* ATH_EEPROM_FIRMWARE */ 79 80 /* 81 * PCI glue. 82 */ 83 84 struct ath_pci_softc { 85 struct ath_softc sc_sc; 86 struct resource *sc_sr; /* memory resource */ 87 struct resource *sc_irq; /* irq resource */ 88 void *sc_ih; /* interrupt handler */ 89 }; 90 91 /* 92 * XXX eventually this should be some system level definition 93 * so modules will hvae probe/attach information like USB. 94 * But for now.. 95 */ 96 struct pci_device_id { 97 int vendor_id; 98 int device_id; 99 100 int sub_vendor_id; 101 int sub_device_id; 102 103 int driver_data; 104 105 int match_populated:1; 106 int match_vendor_id:1; 107 int match_device_id:1; 108 int match_sub_vendor_id:1; 109 int match_sub_device_id:1; 110 }; 111 112 #define PCI_VDEVICE(v, s) \ 113 .vendor_id = (v), \ 114 .device_id = (s), \ 115 .match_populated = 1, \ 116 .match_vendor_id = 1, \ 117 .match_device_id = 1 118 119 #define PCI_DEVICE_SUB(v, d, dv, ds) \ 120 .match_populated = 1, \ 121 .vendor_id = (v), .match_vendor_id = 1, \ 122 .device_id = (d), .match_device_id = 1, \ 123 .sub_vendor_id = (dv), .match_sub_vendor_id = 1, \ 124 .sub_device_id = (ds), .match_sub_device_id = 1 125 126 #define PCI_VENDOR_ID_ATHEROS 0x168c 127 #define PCI_VENDOR_ID_SAMSUNG 0x144d 128 #define PCI_VENDOR_ID_AZWAVE 0x1a3b 129 #define PCI_VENDOR_ID_FOXCONN 0x105b 130 #define PCI_VENDOR_ID_ATTANSIC 0x1969 131 #define PCI_VENDOR_ID_ASUSTEK 0x1043 132 #define PCI_VENDOR_ID_DELL 0x1028 133 #define PCI_VENDOR_ID_QMI 0x1a32 134 #define PCI_VENDOR_ID_LENOVO 0x17aa 135 #define PCI_VENDOR_ID_HP 0x103c 136 137 #include "if_ath_pci_devlist.h" 138 139 /* 140 * Attempt to find a match for the given device in 141 * the given device table. 142 * 143 * Returns the device structure or NULL if no matching 144 * PCI device is found. 145 */ 146 static const struct pci_device_id * 147 ath_pci_probe_device(device_t dev, const struct pci_device_id *dev_table, int nentries) 148 { 149 int i; 150 int vendor_id, device_id; 151 int sub_vendor_id, sub_device_id; 152 153 vendor_id = pci_get_vendor(dev); 154 device_id = pci_get_device(dev); 155 sub_vendor_id = pci_get_subvendor(dev); 156 sub_device_id = pci_get_subdevice(dev); 157 158 for (i = 0; i < nentries; i++) { 159 /* Don't match on non-populated (eg empty) entries */ 160 if (! dev_table[i].match_populated) 161 continue; 162 163 if (dev_table[i].match_vendor_id && 164 (dev_table[i].vendor_id != vendor_id)) 165 continue; 166 if (dev_table[i].match_device_id && 167 (dev_table[i].device_id != device_id)) 168 continue; 169 if (dev_table[i].match_sub_vendor_id && 170 (dev_table[i].sub_vendor_id != sub_vendor_id)) 171 continue; 172 if (dev_table[i].match_sub_device_id && 173 (dev_table[i].sub_device_id != sub_device_id)) 174 continue; 175 176 /* Match */ 177 return (&dev_table[i]); 178 } 179 180 return (NULL); 181 } 182 183 #define BS_BAR 0x10 184 #define PCIR_RETRY_TIMEOUT 0x41 185 #define PCIR_CFG_PMCSR 0x48 186 187 #define DEFAULT_CACHESIZE 32 188 189 static void 190 ath_pci_setup(device_t dev) 191 { 192 uint8_t cz; 193 194 /* XXX TODO: need to override the _system_ saved copies of this */ 195 196 /* 197 * If the cache line size is 0, force it to a reasonable 198 * value. 199 */ 200 cz = pci_read_config(dev, PCIR_CACHELNSZ, 1); 201 if (cz == 0) { 202 pci_write_config(dev, PCIR_CACHELNSZ, 203 DEFAULT_CACHESIZE / 4, 1); 204 } 205 206 /* Override the system latency timer */ 207 pci_write_config(dev, PCIR_LATTIMER, 0xa8, 1); 208 209 /* If a PCI NIC, force wakeup */ 210 #ifdef ATH_PCI_WAKEUP_WAR 211 /* XXX TODO: don't do this for non-PCI (ie, PCIe, Cardbus!) */ 212 if (1) { 213 uint16_t pmcsr; 214 pmcsr = pci_read_config(dev, PCIR_CFG_PMCSR, 2); 215 pmcsr |= 3; 216 pci_write_config(dev, PCIR_CFG_PMCSR, pmcsr, 2); 217 pmcsr &= ~3; 218 pci_write_config(dev, PCIR_CFG_PMCSR, pmcsr, 2); 219 } 220 #endif 221 222 /* 223 * Disable retry timeout to keep PCI Tx retries from 224 * interfering with C3 CPU state. 225 */ 226 pci_write_config(dev, PCIR_RETRY_TIMEOUT, 0, 1); 227 } 228 229 static int 230 ath_pci_probe(device_t dev) 231 { 232 const char* devname; 233 234 devname = ath_hal_probe(pci_get_vendor(dev), pci_get_device(dev)); 235 if (devname != NULL) { 236 device_set_desc(dev, devname); 237 return BUS_PROBE_DEFAULT; 238 } 239 return ENXIO; 240 } 241 242 static int 243 ath_pci_attach(device_t dev) 244 { 245 struct ath_pci_softc *psc = device_get_softc(dev); 246 struct ath_softc *sc = &psc->sc_sc; 247 int error = ENXIO; 248 int rid; 249 #ifdef ATH_EEPROM_FIRMWARE 250 const struct firmware *fw = NULL; 251 const char *buf; 252 #endif 253 const struct pci_device_id *pd; 254 255 sc->sc_dev = dev; 256 257 /* Do this lookup anyway; figure out what to do with it later */ 258 pd = ath_pci_probe_device(dev, ath_pci_id_table, nitems(ath_pci_id_table)); 259 if (pd) 260 sc->sc_pci_devinfo = pd->driver_data; 261 262 /* 263 * Enable bus mastering. 264 */ 265 pci_enable_busmaster(dev); 266 267 /* 268 * Setup other PCI bus configuration parameters. 269 */ 270 ath_pci_setup(dev); 271 272 /* 273 * Setup memory-mapping of PCI registers. 274 */ 275 rid = BS_BAR; 276 psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 277 RF_ACTIVE); 278 if (psc->sc_sr == NULL) { 279 device_printf(dev, "cannot map register space\n"); 280 goto bad; 281 } 282 sc->sc_st = (HAL_BUS_TAG) rman_get_bustag(psc->sc_sr); 283 sc->sc_sh = (HAL_BUS_HANDLE) rman_get_bushandle(psc->sc_sr); 284 /* 285 * Mark device invalid so any interrupts (shared or otherwise) 286 * that arrive before the HAL is setup are discarded. 287 */ 288 sc->sc_invalid = 1; 289 290 ATH_LOCK_INIT(sc); 291 ATH_PCU_LOCK_INIT(sc); 292 ATH_RX_LOCK_INIT(sc); 293 ATH_TX_LOCK_INIT(sc); 294 ATH_TXSTATUS_LOCK_INIT(sc); 295 296 /* 297 * Arrange interrupt line. 298 */ 299 rid = 0; 300 psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 301 RF_SHAREABLE|RF_ACTIVE); 302 if (psc->sc_irq == NULL) { 303 device_printf(dev, "could not map interrupt\n"); 304 goto bad1; 305 } 306 #if defined(__DragonFly__) 307 if (bus_setup_intr(dev, psc->sc_irq, 308 INTR_MPSAFE, 309 ath_intr, sc, &psc->sc_ih, 310 &wlan_global_serializer)) { 311 device_printf(dev, "could not establish interrupt\n"); 312 goto bad2; 313 } 314 #else 315 if (bus_setup_intr(dev, psc->sc_irq, 316 INTR_TYPE_NET | INTR_MPSAFE, 317 NULL, ath_intr, sc, &psc->sc_ih)) { 318 device_printf(dev, "could not establish interrupt\n"); 319 goto bad2; 320 } 321 #endif 322 323 /* 324 * Setup DMA descriptor area. 325 */ 326 if (bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 327 #if defined(__DragonFly__) 328 16, 0, /* alignment, bounds */ 329 #else 330 1, 0, /* alignment, bounds */ 331 #endif 332 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 333 BUS_SPACE_MAXADDR, /* highaddr */ 334 NULL, NULL, /* filter, filterarg */ 335 0x3ffff, /* maxsize XXX */ 336 ATH_MAX_SCATTER, /* nsegments */ 337 0x3ffff, /* maxsegsize XXX */ 338 BUS_DMA_ALLOCNOW, /* flags */ 339 #if defined(__DragonFly__) 340 #else 341 NULL, /* lockfunc */ 342 NULL, /* lockarg */ 343 #endif 344 &sc->sc_dmat)) { 345 device_printf(dev, "cannot allocate DMA tag\n"); 346 goto bad3; 347 } 348 349 #ifdef ATH_EEPROM_FIRMWARE 350 /* 351 * If there's an EEPROM firmware image, load that in. 352 */ 353 if (resource_string_value(device_get_name(dev), device_get_unit(dev), 354 "eeprom_firmware", &buf) == 0) { 355 if (bootverbose) 356 device_printf(dev, "%s: looking up firmware @ '%s'\n", 357 __func__, buf); 358 359 fw = firmware_get(buf); 360 if (fw == NULL) { 361 device_printf(dev, "%s: couldn't find firmware\n", 362 __func__); 363 goto bad4; 364 } 365 366 device_printf(dev, "%s: EEPROM firmware @ %p\n", 367 __func__, fw->data); 368 sc->sc_eepromdata = 369 kmalloc(fw->datasize, M_TEMP, M_WAITOK | M_ZERO); 370 if (! sc->sc_eepromdata) { 371 device_printf(dev, "%s: can't malloc eepromdata\n", 372 __func__); 373 goto bad4; 374 } 375 memcpy(sc->sc_eepromdata, fw->data, fw->datasize); 376 firmware_put(fw, 0); 377 } 378 #endif /* ATH_EEPROM_FIRMWARE */ 379 380 error = ath_attach(pci_get_device(dev), sc); 381 if (error == 0) /* success */ 382 return 0; 383 384 #ifdef ATH_EEPROM_FIRMWARE 385 bad4: 386 #endif 387 bus_dma_tag_destroy(sc->sc_dmat); 388 bad3: 389 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih); 390 bad2: 391 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq); 392 bad1: 393 bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr); 394 395 ATH_TXSTATUS_LOCK_DESTROY(sc); 396 ATH_PCU_LOCK_DESTROY(sc); 397 ATH_RX_LOCK_DESTROY(sc); 398 ATH_TX_LOCK_DESTROY(sc); 399 ATH_LOCK_DESTROY(sc); 400 bad: 401 return (error); 402 } 403 404 static int 405 ath_pci_detach(device_t dev) 406 { 407 struct ath_pci_softc *psc = device_get_softc(dev); 408 struct ath_softc *sc = &psc->sc_sc; 409 410 /* check if device was removed */ 411 sc->sc_invalid = !bus_child_present(dev); 412 413 /* 414 * Do a config read to clear pre-existing pci error status. 415 */ 416 (void) pci_read_config(dev, PCIR_COMMAND, 4); 417 418 ath_detach(sc); 419 420 bus_generic_detach(dev); 421 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih); 422 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq); 423 424 bus_dma_tag_destroy(sc->sc_dmat); 425 bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr); 426 427 if (sc->sc_eepromdata) 428 kfree(sc->sc_eepromdata, M_TEMP); 429 430 ATH_TXSTATUS_LOCK_DESTROY(sc); 431 ATH_PCU_LOCK_DESTROY(sc); 432 ATH_RX_LOCK_DESTROY(sc); 433 ATH_TX_LOCK_DESTROY(sc); 434 ATH_LOCK_DESTROY(sc); 435 436 return (0); 437 } 438 439 static int 440 ath_pci_shutdown(device_t dev) 441 { 442 struct ath_pci_softc *psc = device_get_softc(dev); 443 444 ath_shutdown(&psc->sc_sc); 445 return (0); 446 } 447 448 static int 449 ath_pci_suspend(device_t dev) 450 { 451 struct ath_pci_softc *psc = device_get_softc(dev); 452 453 ath_suspend(&psc->sc_sc); 454 455 return (0); 456 } 457 458 static int 459 ath_pci_resume(device_t dev) 460 { 461 struct ath_pci_softc *psc = device_get_softc(dev); 462 463 /* 464 * Suspend/resume resets the PCI configuration space. 465 */ 466 ath_pci_setup(dev); 467 468 ath_resume(&psc->sc_sc); 469 470 return (0); 471 } 472 473 static device_method_t ath_pci_methods[] = { 474 /* Device interface */ 475 DEVMETHOD(device_probe, ath_pci_probe), 476 DEVMETHOD(device_attach, ath_pci_attach), 477 DEVMETHOD(device_detach, ath_pci_detach), 478 DEVMETHOD(device_shutdown, ath_pci_shutdown), 479 DEVMETHOD(device_suspend, ath_pci_suspend), 480 DEVMETHOD(device_resume, ath_pci_resume), 481 482 { 0,0 } 483 }; 484 static driver_t ath_pci_driver = { 485 "ath", 486 ath_pci_methods, 487 sizeof (struct ath_pci_softc) 488 }; 489 static devclass_t ath_devclass; 490 DRIVER_MODULE(ath_pci, pci, ath_pci_driver, ath_devclass, NULL, NULL); 491 MODULE_VERSION(ath_pci, 1); 492 MODULE_DEPEND(ath_pci, wlan, 1, 1, 1); /* 802.11 media layer */ 493 MODULE_DEPEND(ath_pci, if_ath, 1, 1, 1); /* if_ath driver */ 494