if_ndis_pci.c revision 216558
1209195Snetchild/*- 2209195Snetchild * Copyright (c) 2003 3209195Snetchild * Bill Paul <wpaul@windriver.com>. All rights reserved. 4209195Snetchild * 5209195Snetchild * Redistribution and use in source and binary forms, with or without 6209195Snetchild * modification, are permitted provided that the following conditions 7209195Snetchild * are met: 8209195Snetchild * 1. Redistributions of source code must retain the above copyright 9209195Snetchild * notice, this list of conditions and the following disclaimer. 10209195Snetchild * 2. Redistributions in binary form must reproduce the above copyright 11209195Snetchild * notice, this list of conditions and the following disclaimer in the 12209195Snetchild * documentation and/or other materials provided with the distribution. 13209195Snetchild * 3. All advertising materials mentioning features or use of this software 14209195Snetchild * must display the following acknowledgement: 15209195Snetchild * This product includes software developed by Bill Paul. 16209195Snetchild * 4. Neither the name of the author nor the names of any co-contributors 17209195Snetchild * may be used to endorse or promote products derived from this software 18209195Snetchild * without specific prior written permission. 19209195Snetchild * 20209195Snetchild * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21209195Snetchild * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22209195Snetchild * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23209195Snetchild * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24209195Snetchild * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25209195Snetchild * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26209195Snetchild * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27209195Snetchild * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28209195Snetchild * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29209195Snetchild * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30209195Snetchild * THE POSSIBILITY OF SUCH DAMAGE. 31209195Snetchild */ 32209195Snetchild 33209195Snetchild#include <sys/cdefs.h> 34209195Snetchild__FBSDID("$FreeBSD: head/sys/dev/if_ndis/if_ndis_pci.c 216558 2010-12-19 11:14:34Z tijl $"); 35209195Snetchild 36209195Snetchild#include <sys/param.h> 37209195Snetchild#include <sys/systm.h> 38209195Snetchild#include <sys/kernel.h> 39209195Snetchild#include <sys/module.h> 40209195Snetchild#include <sys/socket.h> 41209195Snetchild#include <sys/queue.h> 42209195Snetchild#include <sys/sysctl.h> 43209195Snetchild 44209195Snetchild#include <net/if.h> 45209195Snetchild#include <net/if_arp.h> 46209195Snetchild#include <net/if_media.h> 47209195Snetchild 48209195Snetchild#include <machine/bus.h> 49209195Snetchild#include <machine/resource.h> 50209195Snetchild#include <sys/bus.h> 51209195Snetchild#include <sys/rman.h> 52209195Snetchild 53209195Snetchild#include <net80211/ieee80211_var.h> 54209195Snetchild 55209195Snetchild#include <dev/pci/pcireg.h> 56211800Snetchild#include <dev/pci/pcivar.h> 57209195Snetchild#include <dev/usb/usb.h> 58209195Snetchild#include <dev/usb/usbdi.h> 59209195Snetchild 60209195Snetchild#include <compat/ndis/pe_var.h> 61209195Snetchild#include <compat/ndis/cfg_var.h> 62209195Snetchild#include <compat/ndis/resource_var.h> 63209195Snetchild#include <compat/ndis/ntoskrnl_var.h> 64209195Snetchild#include <compat/ndis/ndis_var.h> 65209195Snetchild#include <dev/if_ndis/if_ndisvar.h> 66209195Snetchild 67209195SnetchildMODULE_DEPEND(ndis, pci, 1, 1, 1); 68211800Snetchild 69209195Snetchildstatic int ndis_probe_pci (device_t); 70209195Snetchildstatic int ndis_attach_pci (device_t); 71209195Snetchildstatic struct resource_list *ndis_get_resource_list 72211800Snetchild (device_t, device_t); 73209195Snetchildstatic int ndis_devcompare (interface_type, 74209195Snetchild struct ndis_pci_type *, device_t); 75209195Snetchildextern int ndisdrv_modevent (module_t, int, void *); 76209195Snetchildextern int ndis_attach (device_t); 77209195Snetchildextern int ndis_shutdown (device_t); 78209195Snetchildextern int ndis_detach (device_t); 79209195Snetchildextern int ndis_suspend (device_t); 80209195Snetchildextern int ndis_resume (device_t); 81209195Snetchild 82209195Snetchildstatic device_method_t ndis_methods[] = { 83209195Snetchild /* Device interface */ 84209195Snetchild DEVMETHOD(device_probe, ndis_probe_pci), 85209195Snetchild DEVMETHOD(device_attach, ndis_attach_pci), 86209195Snetchild DEVMETHOD(device_detach, ndis_detach), 87 DEVMETHOD(device_shutdown, ndis_shutdown), 88 DEVMETHOD(device_suspend, ndis_suspend), 89 DEVMETHOD(device_resume, ndis_resume), 90 91 /* Bus interface */ 92 DEVMETHOD(bus_get_resource_list, ndis_get_resource_list), 93 94 { 0, 0 } 95}; 96 97static driver_t ndis_driver = { 98 "ndis", 99 ndis_methods, 100 sizeof(struct ndis_softc) 101}; 102 103static devclass_t ndis_devclass; 104 105DRIVER_MODULE(ndis, pci, ndis_driver, ndis_devclass, ndisdrv_modevent, 0); 106 107static int 108ndis_devcompare(bustype, t, dev) 109 interface_type bustype; 110 struct ndis_pci_type *t; 111 device_t dev; 112{ 113 uint16_t vid, did; 114 uint32_t subsys; 115 116 if (bustype != PCIBus) 117 return(FALSE); 118 119 vid = pci_get_vendor(dev); 120 did = pci_get_device(dev); 121 subsys = pci_get_subdevice(dev); 122 subsys = (subsys << 16) | pci_get_subvendor(dev); 123 124 while(t->ndis_name != NULL) { 125 if ((t->ndis_vid == vid) && (t->ndis_did == did) && 126 (t->ndis_subsys == subsys || t->ndis_subsys == 0)) { 127 device_set_desc(dev, t->ndis_name); 128 return(TRUE); 129 } 130 t++; 131 } 132 133 return(FALSE); 134} 135 136/* 137 * Probe for an NDIS device. Check the PCI vendor and device 138 * IDs against our list and return a device name if we find a match. 139 */ 140static int 141ndis_probe_pci(dev) 142 device_t dev; 143{ 144 driver_object *drv; 145 struct drvdb_ent *db; 146 147 drv = windrv_lookup(0, "PCI Bus"); 148 149 if (drv == NULL) 150 return(ENXIO); 151 152 db = windrv_match((matchfuncptr)ndis_devcompare, dev); 153 154 if (db != NULL) { 155 /* Create PDO for this device instance */ 156 windrv_create_pdo(drv, dev); 157 return(0); 158 } 159 160 return(ENXIO); 161} 162 163/* 164 * Attach the interface. Allocate softc structures, do ifmedia 165 * setup and ethernet/BPF attach. 166 */ 167static int 168ndis_attach_pci(dev) 169 device_t dev; 170{ 171 struct ndis_softc *sc; 172 int unit, error = 0, rid; 173 struct ndis_pci_type *t; 174 int devidx = 0, defidx = 0; 175 struct resource_list *rl; 176 struct resource_list_entry *rle; 177 struct drvdb_ent *db; 178 uint16_t vid, did; 179 uint32_t subsys; 180 181 sc = device_get_softc(dev); 182 unit = device_get_unit(dev); 183 sc->ndis_dev = dev; 184 185 db = windrv_match((matchfuncptr)ndis_devcompare, dev); 186 if (db == NULL) 187 return (ENXIO); 188 sc->ndis_dobj = db->windrv_object; 189 sc->ndis_regvals = db->windrv_regvals; 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 STAILQ_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_any(dev, 204 SYS_RES_IOPORT, &sc->ndis_io_rid, 205 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_any(dev, 225 SYS_RES_MEMORY, 226 &sc->ndis_altmem_rid, 227 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_any(dev, 239 SYS_RES_MEMORY, 240 &sc->ndis_mem_rid, 241 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_any(dev, 253 SYS_RES_IRQ, &rid, 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_any(dev, SYS_RES_IRQ, 280 &rid, 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 vid = pci_get_vendor(dev); 312 did = pci_get_device(dev); 313 subsys = pci_get_subdevice(dev); 314 subsys = (subsys << 16) | pci_get_subvendor(dev); 315 316 t = db->windrv_devlist; 317 318 while(t->ndis_name != NULL) { 319 if (t->ndis_vid == vid && t->ndis_did == did) { 320 if (t->ndis_subsys == 0) 321 defidx = devidx; 322 else if (t->ndis_subsys == subsys) 323 break; 324 } 325 t++; 326 devidx++; 327 } 328 329 if (t->ndis_name == NULL) 330 sc->ndis_devidx = defidx; 331 else 332 sc->ndis_devidx = devidx; 333 334 error = ndis_attach(dev); 335 336fail: 337 return(error); 338} 339 340static struct resource_list * 341ndis_get_resource_list(dev, child) 342 device_t dev; 343 device_t child; 344{ 345 struct ndis_softc *sc; 346 347 sc = device_get_softc(dev); 348 return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev)); 349} 350