if_ath_pci.c revision 116743
1/*- 2 * Copyright (c) 2002, 2003 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 37#include <sys/cdefs.h> 38__FBSDID("$FreeBSD: head/sys/dev/ath/if_ath_pci.c 116743 2003-06-23 17:01:19Z sam $"); 39 40/* 41 * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver. 42 */ 43 44#include "opt_inet.h" 45 46#include <sys/param.h> 47#include <sys/systm.h> 48#include <sys/mbuf.h> 49#include <sys/malloc.h> 50#include <sys/module.h> 51#include <sys/kernel.h> 52#include <sys/lock.h> 53#include <sys/mutex.h> 54#include <sys/socket.h> 55#include <sys/sockio.h> 56#include <sys/errno.h> 57 58#include <machine/bus.h> 59#include <machine/resource.h> 60#include <sys/bus.h> 61#include <sys/rman.h> 62 63#include <net/if.h> 64#include <net/if_dl.h> 65#include <net/if_media.h> 66#include <net/ethernet.h> 67#include <net/if_llc.h> 68#include <net/if_arp.h> 69 70#include <net80211/ieee80211.h> 71#include <net80211/ieee80211_crypto.h> 72#include <net80211/ieee80211_node.h> 73#include <net80211/ieee80211_proto.h> 74#include <net80211/ieee80211_var.h> 75 76#ifdef INET 77#include <netinet/in.h> 78#include <netinet/if_ether.h> 79#endif 80 81#include <dev/ath/if_athvar.h> 82#include <contrib/dev/ath/ah.h> 83 84#include <dev/pci/pcivar.h> 85#include <dev/pci/pcireg.h> 86 87/* 88 * PCI glue. 89 */ 90 91struct ath_pci_softc { 92 struct ath_softc sc_sc; 93 struct resource *sc_sr; /* memory resource */ 94 struct resource *sc_irq; /* irq resource */ 95 void *sc_ih; /* intererupt handler */ 96 u_int8_t sc_saved_intline; 97 u_int8_t sc_saved_cachelinesz; 98 u_int8_t sc_saved_lattimer; 99}; 100 101#define BS_BAR 0x10 102 103static int 104ath_pci_probe(device_t dev) 105{ 106 const char* devname; 107 108 devname = ath_hal_probe(pci_get_vendor(dev), pci_get_device(dev)); 109 if (devname) { 110 device_set_desc(dev, devname); 111 return 0; 112 } 113 return ENXIO; 114} 115 116static int 117ath_pci_attach(device_t dev) 118{ 119 struct ath_pci_softc *psc = device_get_softc(dev); 120 struct ath_softc *sc = &psc->sc_sc; 121 u_int32_t cmd; 122 int error = ENXIO; 123 int rid; 124 125 bzero(psc, sizeof (*psc)); 126 sc->sc_dev = dev; 127 128 cmd = pci_read_config(dev, PCIR_COMMAND, 4); 129 cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN; 130 pci_write_config(dev, PCIR_COMMAND, cmd, 4); 131 cmd = pci_read_config(dev, PCIR_COMMAND, 4); 132 133 if ((cmd & PCIM_CMD_MEMEN) == 0) { 134 device_printf(dev, "failed to enable memory mapping\n"); 135 goto bad; 136 } 137 138 if ((cmd & PCIM_CMD_BUSMASTEREN) == 0) { 139 device_printf(dev, "failed to enable bus mastering\n"); 140 goto bad; 141 } 142 143 /* 144 * Setup memory-mapping of PCI registers. 145 */ 146 rid = BS_BAR; 147 psc->sc_sr = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 148 0, ~0, 1, RF_ACTIVE); 149 if (psc->sc_sr == NULL) { 150 device_printf(dev, "cannot map register space\n"); 151 goto bad; 152 } 153 sc->sc_st = rman_get_bustag(psc->sc_sr); 154 sc->sc_sh = rman_get_bushandle(psc->sc_sr); 155 156 /* 157 * Arrange interrupt line. 158 */ 159 rid = 0; 160 psc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 161 0, ~0, 1, RF_SHAREABLE|RF_ACTIVE); 162 if (psc->sc_irq == NULL) { 163 device_printf(dev, "could not map interrupt\n"); 164 goto bad1; 165 } 166 if (bus_setup_intr(dev, psc->sc_irq, 167 INTR_TYPE_NET | INTR_MPSAFE, 168 ath_intr, sc, &psc->sc_ih)) { 169 device_printf(dev, "could not establish interrupt\n"); 170 goto bad2; 171 } 172 173 /* 174 * Setup DMA descriptor area. 175 */ 176 if (bus_dma_tag_create(NULL, /* parent */ 177 1, 0, /* alignment, bounds */ 178 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 179 BUS_SPACE_MAXADDR, /* highaddr */ 180 NULL, NULL, /* filter, filterarg */ 181 0x3ffff, /* maxsize XXX */ 182 ATH_MAX_SCATTER, /* nsegments */ 183 0xffff, /* maxsegsize XXX */ 184 BUS_DMA_ALLOCNOW, /* flags */ 185 &sc->sc_dmat)) { 186 device_printf(dev, "cannot allocate DMA tag\n"); 187 goto bad3; 188 } 189 190 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), 191 MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE); 192 193 error = ath_attach(pci_get_device(dev), sc); 194 if (error == 0) 195 return error; 196 197 mtx_destroy(&sc->sc_mtx); 198 bus_dma_tag_destroy(sc->sc_dmat); 199bad3: 200 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih); 201bad2: 202 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq); 203bad1: 204 bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr); 205bad: 206 return (error); 207} 208 209static int 210ath_pci_detach(device_t dev) 211{ 212 struct ath_pci_softc *psc = device_get_softc(dev); 213 struct ath_softc *sc = &psc->sc_sc; 214 215 /* check if device was removed */ 216 sc->sc_invalid = !bus_child_present(dev); 217 218 ath_detach(sc); 219 220 bus_generic_detach(dev); 221 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih); 222 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq); 223 224 bus_dma_tag_destroy(sc->sc_dmat); 225 bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr); 226 227 mtx_destroy(&sc->sc_mtx); 228 229 return (0); 230} 231 232static int 233ath_pci_shutdown(device_t dev) 234{ 235 struct ath_pci_softc *psc = device_get_softc(dev); 236 237 ath_shutdown(&psc->sc_sc); 238 return (0); 239} 240 241static int 242ath_pci_suspend(device_t dev) 243{ 244 struct ath_pci_softc *psc = device_get_softc(dev); 245 246 ath_suspend(&psc->sc_sc); 247 248 psc->sc_saved_intline = pci_read_config(dev, PCIR_INTLINE, 1); 249 psc->sc_saved_cachelinesz= pci_read_config(dev, PCIR_CACHELNSZ, 1); 250 psc->sc_saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1); 251 252 return (0); 253} 254 255static int 256ath_pci_resume(device_t dev) 257{ 258 struct ath_pci_softc *psc = device_get_softc(dev); 259 u_int16_t cmd; 260 261 pci_write_config(dev, PCIR_INTLINE, psc->sc_saved_intline, 1); 262 pci_write_config(dev, PCIR_CACHELNSZ, psc->sc_saved_cachelinesz, 1); 263 pci_write_config(dev, PCIR_LATTIMER, psc->sc_saved_lattimer, 1); 264 265 /* re-enable mem-map and busmastering */ 266 cmd = pci_read_config(dev, PCIR_COMMAND, 2); 267 cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN; 268 pci_write_config(dev, PCIR_COMMAND, cmd, 2); 269 270 ath_resume(&psc->sc_sc); 271 272 return (0); 273} 274 275static device_method_t ath_pci_methods[] = { 276 /* Device interface */ 277 DEVMETHOD(device_probe, ath_pci_probe), 278 DEVMETHOD(device_attach, ath_pci_attach), 279 DEVMETHOD(device_detach, ath_pci_detach), 280 DEVMETHOD(device_shutdown, ath_pci_shutdown), 281 DEVMETHOD(device_suspend, ath_pci_suspend), 282 DEVMETHOD(device_resume, ath_pci_resume), 283 284 { 0,0 } 285}; 286static driver_t ath_pci_driver = { 287 "ath", 288 ath_pci_methods, 289 sizeof (struct ath_pci_softc) 290}; 291static devclass_t ath_devclass; 292DRIVER_MODULE(if_ath, pci, ath_pci_driver, ath_devclass, 0, 0); 293DRIVER_MODULE(if_ath, cardbus, ath_pci_driver, ath_devclass, 0, 0); 294MODULE_VERSION(if_ath, 1); 295MODULE_DEPEND(if_ath, ath_hal, 1, 1, 1); /* Atheros HAL */ 296MODULE_DEPEND(if_ath, wlan, 1, 1, 1); /* 802.11 media layer */ 297