1 /* 2 * Copyright (c) 2002-2005 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 * 3. Neither the names of the above-listed copyright holders nor the names 16 * of any contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * Alternatively, this software may be distributed under the terms of the 20 * GNU General Public License ("GPL") version 2 as published by the Free 21 * Software Foundation. 22 * 23 * NO WARRANTY 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 28 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 29 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 32 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 34 * THE POSSIBILITY OF SUCH DAMAGES. 35 * 36 * $FreeBSD: src/sys/dev/ath/if_ath_pci.c,v 1.12 2005/03/05 19:06:12 imp Exp $ 37 * $DragonFly: src/sys/dev/netif/ath/ath/if_ath_pci.c,v 1.4 2007/02/22 05:17:09 sephe Exp $ 38 */ 39 40 /* 41 * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver. 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/module.h> 47 #include <sys/kernel.h> 48 #include <sys/errno.h> 49 #include <sys/sysctl.h> 50 #include <sys/bus.h> 51 #include <sys/rman.h> 52 53 #include <sys/socket.h> 54 55 #include <net/if.h> 56 #include <net/if_media.h> 57 #include <net/if_arp.h> 58 59 #include <netproto/802_11/ieee80211_var.h> 60 61 #include <bus/pci/pcivar.h> 62 #include <bus/pci/pcireg.h> 63 64 #include <dev/netif/ath/ath/if_athvar.h> 65 #include <contrib/dev/ath/ah.h> 66 67 static int ath_pci_probe(device_t); 68 static int ath_pci_attach(device_t); 69 static int ath_pci_detach(device_t); 70 static int ath_pci_shutdown(device_t); 71 static int ath_pci_suspend(device_t); 72 static int ath_pci_resume(device_t); 73 74 static void ath_pci_setup(device_t); 75 76 /* 77 * PCI glue. 78 */ 79 80 struct ath_pci_softc { 81 struct ath_softc sc_sc; 82 struct resource *sc_sr; /* memory resource */ 83 int sc_srid; 84 }; 85 86 #define BS_BAR 0x10 87 #define PCIR_RETRY_TIMEOUT 0x41 88 89 static int 90 ath_pci_probe(device_t dev) 91 { 92 const char* devname; 93 94 devname = ath_hal_probe(pci_get_vendor(dev), pci_get_device(dev)); 95 if (devname != NULL) { 96 device_set_desc(dev, devname); 97 return 0; 98 } 99 return ENXIO; 100 } 101 102 static void 103 ath_pci_setup(device_t dev) 104 { 105 /* 106 * Enable memory mapping and bus mastering. 107 */ 108 pci_enable_busmaster(dev); 109 110 /* 111 * Disable retry timeout to keep PCI Tx retries from 112 * interfering with C3 CPU state. 113 */ 114 pci_write_config(dev, PCIR_RETRY_TIMEOUT, 0, 1); 115 } 116 117 static int 118 ath_pci_attach(device_t dev) 119 { 120 struct ath_pci_softc *psc = device_get_softc(dev); 121 struct ath_softc *sc = &psc->sc_sc; 122 int error; 123 124 sc->sc_dev = dev; 125 126 ath_pci_setup(dev); 127 128 /* 129 * Setup memory-mapping of PCI registers. 130 */ 131 psc->sc_srid = BS_BAR; 132 psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->sc_srid, 133 RF_ACTIVE); 134 if (psc->sc_sr == NULL) { 135 device_printf(dev, "cannot map register space\n"); 136 return ENXIO; 137 } 138 /* NB: these casts are known to be safe */ 139 sc->sc_st = (HAL_BUS_TAG)rman_get_bustag(psc->sc_sr); 140 sc->sc_sh = (HAL_BUS_HANDLE)rman_get_bushandle(psc->sc_sr); 141 142 /* 143 * Setup DMA descriptor area. 144 */ 145 error = bus_dma_tag_create(NULL, 1, 0, 146 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, 147 NULL, NULL, 0x3ffff/* maxsize XXX */, 148 ATH_MAX_SCATTER, 0x3ffff/* maxsegsize XXX */, 149 BUS_DMA_ALLOCNOW, &sc->sc_dmat); 150 if (error) { 151 device_printf(dev, "cannot allocate DMA tag\n"); 152 goto back; 153 } 154 155 error = ath_attach(pci_get_device(dev), sc); 156 157 back: 158 if (error) 159 ath_pci_detach(dev); 160 return error; 161 } 162 163 static int 164 ath_pci_detach(device_t dev) 165 { 166 struct ath_pci_softc *psc = device_get_softc(dev); 167 struct ath_softc *sc = &psc->sc_sc; 168 169 /* XXX check if device was removed */ 170 sc->sc_invalid = !bus_child_present(dev); 171 172 if (device_is_attached(dev)) 173 ath_detach(sc); 174 175 bus_generic_detach(dev); 176 177 if (sc->sc_dmat != NULL) 178 bus_dma_tag_destroy(sc->sc_dmat); 179 180 if (psc->sc_sr != NULL) { 181 bus_release_resource(dev, SYS_RES_MEMORY, psc->sc_srid, 182 psc->sc_sr); 183 } 184 return 0; 185 } 186 187 static int 188 ath_pci_shutdown(device_t dev) 189 { 190 struct ath_pci_softc *psc = device_get_softc(dev); 191 192 ath_shutdown(&psc->sc_sc); 193 return (0); 194 } 195 196 static int 197 ath_pci_suspend(device_t dev) 198 { 199 struct ath_pci_softc *psc = device_get_softc(dev); 200 201 ath_suspend(&psc->sc_sc); 202 return (0); 203 } 204 205 static int 206 ath_pci_resume(device_t dev) 207 { 208 struct ath_pci_softc *psc = device_get_softc(dev); 209 210 ath_pci_setup(dev); 211 ath_resume(&psc->sc_sc); 212 return (0); 213 } 214 215 static device_method_t ath_pci_methods[] = { 216 /* Device interface */ 217 DEVMETHOD(device_probe, ath_pci_probe), 218 DEVMETHOD(device_attach, ath_pci_attach), 219 DEVMETHOD(device_detach, ath_pci_detach), 220 DEVMETHOD(device_shutdown, ath_pci_shutdown), 221 DEVMETHOD(device_suspend, ath_pci_suspend), 222 DEVMETHOD(device_resume, ath_pci_resume), 223 224 { 0, 0 } 225 }; 226 227 static driver_t ath_pci_driver = { 228 "ath", 229 ath_pci_methods, 230 sizeof (struct ath_pci_softc) 231 }; 232 233 static devclass_t ath_devclass; 234 235 DRIVER_MODULE(if_ath, pci, ath_pci_driver, ath_devclass, 0, 0); 236 DRIVER_MODULE(if_ath, cardbus, ath_pci_driver, ath_devclass, 0, 0); 237 238 MODULE_VERSION(if_ath, 1); 239 240 MODULE_DEPEND(if_ath, ath_hal, 1, 1, 1); /* Atheros HAL */ 241 MODULE_DEPEND(if_ath, ath_rate, 1, 1, 1); /* rate control algorithm */ 242 MODULE_DEPEND(if_ath, wlan, 1, 1, 1); /* 802.11 media layer */ 243