if_ndis_pci.c revision 142804
1/*- 2 * Copyright (c) 2003 3 * Bill Paul <wpaul@windriver.com>. 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 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 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 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/dev/if_ndis/if_ndis_pci.c 142804 2005-02-28 16:47:54Z wpaul $"); 35 36#include <sys/param.h> 37#include <sys/systm.h> 38#include <sys/kernel.h> 39#include <sys/module.h> 40#include <sys/socket.h> 41#include <sys/queue.h> 42#include <sys/sysctl.h> 43 44#include <net/if.h> 45#include <net/if_arp.h> 46#include <net/if_media.h> 47 48#include <machine/bus.h> 49#include <machine/resource.h> 50#include <sys/bus.h> 51#include <sys/rman.h> 52 53#include <net80211/ieee80211_var.h> 54 55#include <dev/pci/pcireg.h> 56#include <dev/pci/pcivar.h> 57 58#include <compat/ndis/pe_var.h> 59#include <compat/ndis/resource_var.h> 60#include <compat/ndis/ntoskrnl_var.h> 61#include <compat/ndis/ndis_var.h> 62#include <compat/ndis/cfg_var.h> 63#include <dev/if_ndis/if_ndisvar.h> 64 65#include "ndis_driver_data.h" 66 67#ifdef NDIS_PCI_DEV_TABLE 68 69MODULE_DEPEND(ndis, pci, 1, 1, 1); 70MODULE_DEPEND(ndis, ether, 1, 1, 1); 71MODULE_DEPEND(ndis, wlan, 1, 1, 1); 72MODULE_DEPEND(ndis, ndisapi, 1, 1, 1); 73 74/* 75 * Various supported device vendors/types and their names. 76 * These are defined in the ndis_driver_data.h file. 77 */ 78static struct ndis_pci_type ndis_devs[] = { 79#ifdef NDIS_PCI_DEV_TABLE 80 NDIS_PCI_DEV_TABLE 81#endif 82 { 0, 0, 0, NULL } 83}; 84 85static int ndis_probe_pci (device_t); 86static int ndis_attach_pci (device_t); 87static struct resource_list *ndis_get_resource_list 88 (device_t, device_t); 89extern int ndisdrv_modevent (module_t, int, void *); 90extern int ndis_attach (device_t); 91extern int ndis_shutdown (device_t); 92extern int ndis_detach (device_t); 93extern int ndis_suspend (device_t); 94extern int ndis_resume (device_t); 95 96extern unsigned char drv_data[]; 97 98static device_method_t ndis_methods[] = { 99 /* Device interface */ 100 DEVMETHOD(device_probe, ndis_probe_pci), 101 DEVMETHOD(device_attach, ndis_attach_pci), 102 DEVMETHOD(device_detach, ndis_detach), 103 DEVMETHOD(device_shutdown, ndis_shutdown), 104 DEVMETHOD(device_suspend, ndis_suspend), 105 DEVMETHOD(device_resume, ndis_resume), 106 107 /* Bus interface */ 108 DEVMETHOD(bus_get_resource_list, ndis_get_resource_list), 109 110 { 0, 0 } 111}; 112 113static driver_t ndis_driver = { 114#ifdef NDIS_DEVNAME 115 NDIS_DEVNAME, 116#else 117 "ndis", 118#endif 119 ndis_methods, 120 sizeof(struct ndis_softc) 121}; 122 123static devclass_t ndis_devclass; 124 125#ifdef NDIS_MODNAME 126#define NDIS_MODNAME_OVERRIDE_PCI(x) \ 127 DRIVER_MODULE(x, pci, ndis_driver, ndis_devclass, ndisdrv_modevent, 0) 128#define NDIS_MODNAME_OVERRIDE_CARDBUS(x) \ 129 DRIVER_MODULE(x, cardbus, ndis_driver, ndis_devclass, \ 130 ndisdrv_modevent, 0) 131NDIS_MODNAME_OVERRIDE_PCI(NDIS_MODNAME); 132NDIS_MODNAME_OVERRIDE_CARDBUS(NDIS_MODNAME); 133#else 134DRIVER_MODULE(ndis, pci, ndis_driver, ndis_devclass, ndisdrv_modevent, 0); 135DRIVER_MODULE(ndis, cardbus, ndis_driver, ndis_devclass, ndisdrv_modevent, 0); 136#endif 137 138/* 139 * Probe for an NDIS device. Check the PCI vendor and device 140 * IDs against our list and return a device name if we find a match. 141 */ 142static int 143ndis_probe_pci(dev) 144 device_t dev; 145{ 146 struct ndis_pci_type *t; 147 driver_object *drv; 148 149 t = ndis_devs; 150 drv = windrv_lookup(0, "PCI Bus"); 151 152 if (drv == NULL) 153 return(ENXIO); 154 155 while(t->ndis_name != NULL) { 156 if ((pci_get_vendor(dev) == t->ndis_vid) && 157 (pci_get_device(dev) == t->ndis_did) && 158 ((pci_read_config(dev, PCIR_SUBVEND_0, 4) == 159 t->ndis_subsys) || t->ndis_subsys == 0)) { 160 device_set_desc(dev, t->ndis_name); 161 162 /* Create PDO for this device instance */ 163 windrv_create_pdo(drv, dev); 164 return(0); 165 } 166 t++; 167 } 168 169 return(ENXIO); 170} 171 172/* 173 * Attach the interface. Allocate softc structures, do ifmedia 174 * setup and ethernet/BPF attach. 175 */ 176static int 177ndis_attach_pci(dev) 178 device_t dev; 179{ 180 struct ndis_softc *sc; 181 int unit, error = 0, rid; 182 struct ndis_pci_type *t; 183 int devidx = 0, defidx = 0; 184 struct resource_list *rl; 185 struct resource_list_entry *rle; 186 187 sc = device_get_softc(dev); 188 unit = device_get_unit(dev); 189 sc->ndis_dev = dev; 190 191 /* 192 * Map control/status registers. 193 */ 194 195 pci_enable_busmaster(dev); 196 197 rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 198 if (rl != NULL) { 199 SLIST_FOREACH(rle, rl, link) { 200 switch (rle->type) { 201 case SYS_RES_IOPORT: 202 sc->ndis_io_rid = rle->rid; 203 sc->ndis_res_io = bus_alloc_resource(dev, 204 SYS_RES_IOPORT, &sc->ndis_io_rid, 205 0, ~0, 1, RF_ACTIVE); 206 if (sc->ndis_res_io == NULL) { 207 device_printf(dev, 208 "couldn't map iospace\n"); 209 error = ENXIO; 210 goto fail; 211 } 212 break; 213 case SYS_RES_MEMORY: 214 if (sc->ndis_res_altmem != NULL && 215 sc->ndis_res_mem != NULL) { 216 device_printf(dev, 217 "too many memory resources\n"); 218 error = ENXIO; 219 goto fail; 220 } 221 if (sc->ndis_res_mem) { 222 sc->ndis_altmem_rid = rle->rid; 223 sc->ndis_res_altmem = 224 bus_alloc_resource(dev, 225 SYS_RES_MEMORY, 226 &sc->ndis_altmem_rid, 227 0, ~0, 1, RF_ACTIVE); 228 if (sc->ndis_res_altmem == NULL) { 229 device_printf(dev, 230 "couldn't map alt " 231 "memory\n"); 232 error = ENXIO; 233 goto fail; 234 } 235 } else { 236 sc->ndis_mem_rid = rle->rid; 237 sc->ndis_res_mem = 238 bus_alloc_resource(dev, 239 SYS_RES_MEMORY, 240 &sc->ndis_mem_rid, 241 0, ~0, 1, RF_ACTIVE); 242 if (sc->ndis_res_mem == NULL) { 243 device_printf(dev, 244 "couldn't map memory\n"); 245 error = ENXIO; 246 goto fail; 247 } 248 } 249 break; 250 case SYS_RES_IRQ: 251 rid = rle->rid; 252 sc->ndis_irq = bus_alloc_resource(dev, 253 SYS_RES_IRQ, &rid, 0, ~0, 1, 254 RF_SHAREABLE | RF_ACTIVE); 255 if (sc->ndis_irq == NULL) { 256 device_printf(dev, 257 "couldn't map interrupt\n"); 258 error = ENXIO; 259 goto fail; 260 } 261 break; 262 default: 263 break; 264 } 265 sc->ndis_rescnt++; 266 } 267 } 268 269 /* 270 * If the BIOS did not set up an interrupt for this device, 271 * the resource traversal code above will fail to set up 272 * an IRQ resource. This is usually a bad thing, so try to 273 * force the allocation of an interrupt here. If one was 274 * not assigned to us by the BIOS, bus_alloc_resource() 275 * should route one for us. 276 */ 277 if (sc->ndis_irq == NULL) { 278 rid = 0; 279 sc->ndis_irq = bus_alloc_resource(dev, SYS_RES_IRQ, 280 &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE); 281 if (sc->ndis_irq == NULL) { 282 device_printf(dev, "couldn't route interrupt\n"); 283 error = ENXIO; 284 goto fail; 285 } 286 sc->ndis_rescnt++; 287 } 288 289 /* 290 * Allocate the parent bus DMA tag appropriate for PCI. 291 */ 292#define NDIS_NSEG_NEW 32 293 error = bus_dma_tag_create(NULL, /* parent */ 294 1, 0, /* alignment, boundary */ 295 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */ 296 BUS_SPACE_MAXADDR, /* highaddr */ 297 NULL, NULL, /* filter, filterarg */ 298 MAXBSIZE, NDIS_NSEG_NEW,/* maxsize, nsegments */ 299 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */ 300 BUS_DMA_ALLOCNOW, /* flags */ 301 NULL, NULL, /* lockfunc, lockarg */ 302 &sc->ndis_parent_tag); 303 304 if (error) 305 goto fail; 306 307 sc->ndis_iftype = PCIBus; 308 309 /* Figure out exactly which device we matched. */ 310 311 t = ndis_devs; 312 313 while(t->ndis_name != NULL) { 314 if ((pci_get_vendor(dev) == t->ndis_vid) && 315 (pci_get_device(dev) == t->ndis_did)) { 316 if (t->ndis_subsys == 0) 317 defidx = devidx; 318 else { 319 if (t->ndis_subsys == 320 pci_read_config(dev, PCIR_SUBVEND_0, 4)) 321 break; 322 } 323 } 324 t++; 325 devidx++; 326 } 327 328 if (ndis_devs[devidx].ndis_name == NULL) 329 sc->ndis_devidx = defidx; 330 else 331 sc->ndis_devidx = devidx; 332 333 error = ndis_attach(dev); 334 335fail: 336 return(error); 337} 338 339static struct resource_list * 340ndis_get_resource_list(dev, child) 341 device_t dev; 342 device_t child; 343{ 344 struct ndis_softc *sc; 345 346 sc = device_get_softc(dev); 347 return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev)); 348} 349 350#endif /* NDIS_PCI_DEV_TABLE */ 351