if_ndis_pci.c revision 194677
1139749Simp/*- 2126706Swpaul * Copyright (c) 2003 3126706Swpaul * Bill Paul <wpaul@windriver.com>. All rights reserved. 4126706Swpaul * 5126706Swpaul * Redistribution and use in source and binary forms, with or without 6126706Swpaul * modification, are permitted provided that the following conditions 7126706Swpaul * are met: 8126706Swpaul * 1. Redistributions of source code must retain the above copyright 9126706Swpaul * notice, this list of conditions and the following disclaimer. 10126706Swpaul * 2. Redistributions in binary form must reproduce the above copyright 11126706Swpaul * notice, this list of conditions and the following disclaimer in the 12126706Swpaul * documentation and/or other materials provided with the distribution. 13126706Swpaul * 3. All advertising materials mentioning features or use of this software 14126706Swpaul * must display the following acknowledgement: 15126706Swpaul * This product includes software developed by Bill Paul. 16126706Swpaul * 4. Neither the name of the author nor the names of any co-contributors 17126706Swpaul * may be used to endorse or promote products derived from this software 18126706Swpaul * without specific prior written permission. 19126706Swpaul * 20126706Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21126706Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22126706Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23126706Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24126706Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25126706Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26126706Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27126706Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28126706Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29126706Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30126706Swpaul * THE POSSIBILITY OF SUCH DAMAGE. 31126706Swpaul */ 32126706Swpaul 33126706Swpaul#include <sys/cdefs.h> 34126706Swpaul__FBSDID("$FreeBSD: head/sys/dev/if_ndis/if_ndis_pci.c 194677 2009-06-23 02:19:59Z thompsa $"); 35126706Swpaul 36126706Swpaul#include <sys/param.h> 37126706Swpaul#include <sys/systm.h> 38126706Swpaul#include <sys/kernel.h> 39129972Swpaul#include <sys/module.h> 40126706Swpaul#include <sys/socket.h> 41126706Swpaul#include <sys/queue.h> 42126706Swpaul#include <sys/sysctl.h> 43126706Swpaul 44126706Swpaul#include <net/if.h> 45126706Swpaul#include <net/if_arp.h> 46126706Swpaul#include <net/if_media.h> 47126706Swpaul 48126706Swpaul#include <machine/bus.h> 49126706Swpaul#include <machine/resource.h> 50126706Swpaul#include <sys/bus.h> 51126706Swpaul#include <sys/rman.h> 52126706Swpaul 53126706Swpaul#include <net80211/ieee80211_var.h> 54126706Swpaul 55126706Swpaul#include <dev/pci/pcireg.h> 56126706Swpaul#include <dev/pci/pcivar.h> 57189488Sweongyo#include <dev/usb/usb.h> 58194677Sthompsa#include <dev/usb/usbdi.h> 59126706Swpaul 60126706Swpaul#include <compat/ndis/pe_var.h> 61145485Swpaul#include <compat/ndis/cfg_var.h> 62126706Swpaul#include <compat/ndis/resource_var.h> 63126706Swpaul#include <compat/ndis/ntoskrnl_var.h> 64126706Swpaul#include <compat/ndis/ndis_var.h> 65126706Swpaul#include <dev/if_ndis/if_ndisvar.h> 66126706Swpaul 67126706SwpaulMODULE_DEPEND(ndis, pci, 1, 1, 1); 68126706Swpaul 69126706Swpaulstatic int ndis_probe_pci (device_t); 70126706Swpaulstatic int ndis_attach_pci (device_t); 71131953Swpaulstatic struct resource_list *ndis_get_resource_list 72131953Swpaul (device_t, device_t); 73146016Swpaulstatic int ndis_devcompare (interface_type, 74146016Swpaul struct ndis_pci_type *, device_t); 75141524Swpaulextern int ndisdrv_modevent (module_t, int, void *); 76126706Swpaulextern int ndis_attach (device_t); 77126706Swpaulextern int ndis_shutdown (device_t); 78126706Swpaulextern int ndis_detach (device_t); 79126706Swpaulextern int ndis_suspend (device_t); 80126706Swpaulextern int ndis_resume (device_t); 81126706Swpaul 82126706Swpaulstatic device_method_t ndis_methods[] = { 83126706Swpaul /* Device interface */ 84126706Swpaul DEVMETHOD(device_probe, ndis_probe_pci), 85126706Swpaul DEVMETHOD(device_attach, ndis_attach_pci), 86126706Swpaul DEVMETHOD(device_detach, ndis_detach), 87126706Swpaul DEVMETHOD(device_shutdown, ndis_shutdown), 88126706Swpaul DEVMETHOD(device_suspend, ndis_suspend), 89126706Swpaul DEVMETHOD(device_resume, ndis_resume), 90126706Swpaul 91131953Swpaul /* Bus interface */ 92131953Swpaul DEVMETHOD(bus_get_resource_list, ndis_get_resource_list), 93131953Swpaul 94126706Swpaul { 0, 0 } 95126706Swpaul}; 96126706Swpaul 97126706Swpaulstatic driver_t ndis_driver = { 98126706Swpaul "ndis", 99126706Swpaul ndis_methods, 100126706Swpaul sizeof(struct ndis_softc) 101126706Swpaul}; 102126706Swpaul 103126706Swpaulstatic devclass_t ndis_devclass; 104126706Swpaul 105141524SwpaulDRIVER_MODULE(ndis, pci, ndis_driver, ndis_devclass, ndisdrv_modevent, 0); 106126706Swpaul 107145485Swpaulstatic int 108146016Swpaulndis_devcompare(bustype, t, dev) 109146016Swpaul interface_type bustype; 110145485Swpaul struct ndis_pci_type *t; 111145485Swpaul device_t dev; 112145485Swpaul{ 113146016Swpaul if (bustype != PCIBus) 114146016Swpaul return(FALSE); 115146016Swpaul 116145485Swpaul while(t->ndis_name != NULL) { 117145485Swpaul if ((pci_get_vendor(dev) == t->ndis_vid) && 118145485Swpaul (pci_get_device(dev) == t->ndis_did) && 119145485Swpaul ((pci_read_config(dev, PCIR_SUBVEND_0, 4) == 120145485Swpaul t->ndis_subsys) || t->ndis_subsys == 0)) { 121145485Swpaul device_set_desc(dev, t->ndis_name); 122145485Swpaul return(TRUE); 123145485Swpaul } 124145485Swpaul t++; 125145485Swpaul } 126145485Swpaul 127145485Swpaul return(FALSE); 128145485Swpaul} 129145485Swpaul 130126706Swpaul/* 131126706Swpaul * Probe for an NDIS device. Check the PCI vendor and device 132126706Swpaul * IDs against our list and return a device name if we find a match. 133126706Swpaul */ 134126706Swpaulstatic int 135126706Swpaulndis_probe_pci(dev) 136126706Swpaul device_t dev; 137126706Swpaul{ 138141524Swpaul driver_object *drv; 139145485Swpaul struct drvdb_ent *db; 140126706Swpaul 141142804Swpaul drv = windrv_lookup(0, "PCI Bus"); 142126706Swpaul 143141524Swpaul if (drv == NULL) 144141524Swpaul return(ENXIO); 145141524Swpaul 146145485Swpaul db = windrv_match((matchfuncptr)ndis_devcompare, dev); 147141524Swpaul 148145485Swpaul if (db != NULL) { 149145485Swpaul /* Create PDO for this device instance */ 150145485Swpaul windrv_create_pdo(drv, dev); 151145485Swpaul return(0); 152126706Swpaul } 153126706Swpaul 154126706Swpaul return(ENXIO); 155126706Swpaul} 156126706Swpaul 157126706Swpaul/* 158126706Swpaul * Attach the interface. Allocate softc structures, do ifmedia 159126706Swpaul * setup and ethernet/BPF attach. 160126706Swpaul */ 161126706Swpaulstatic int 162126706Swpaulndis_attach_pci(dev) 163126706Swpaul device_t dev; 164126706Swpaul{ 165126706Swpaul struct ndis_softc *sc; 166126706Swpaul int unit, error = 0, rid; 167126706Swpaul struct ndis_pci_type *t; 168126706Swpaul int devidx = 0, defidx = 0; 169126706Swpaul struct resource_list *rl; 170126706Swpaul struct resource_list_entry *rle; 171145485Swpaul struct drvdb_ent *db; 172126706Swpaul 173126706Swpaul sc = device_get_softc(dev); 174126706Swpaul unit = device_get_unit(dev); 175126706Swpaul sc->ndis_dev = dev; 176126706Swpaul 177145485Swpaul db = windrv_match((matchfuncptr)ndis_devcompare, dev); 178145485Swpaul if (db == NULL) 179145485Swpaul return (ENXIO); 180145485Swpaul sc->ndis_dobj = db->windrv_object; 181145485Swpaul sc->ndis_regvals = db->windrv_regvals; 182145485Swpaul 183126706Swpaul /* 184126706Swpaul * Map control/status registers. 185126706Swpaul */ 186126706Swpaul 187126706Swpaul pci_enable_busmaster(dev); 188126706Swpaul 189126706Swpaul rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 190126706Swpaul if (rl != NULL) { 191143848Smaxim STAILQ_FOREACH(rle, rl, link) { 192126706Swpaul switch (rle->type) { 193126706Swpaul case SYS_RES_IOPORT: 194126706Swpaul sc->ndis_io_rid = rle->rid; 195127281Swpaul sc->ndis_res_io = bus_alloc_resource(dev, 196126706Swpaul SYS_RES_IOPORT, &sc->ndis_io_rid, 197127281Swpaul 0, ~0, 1, RF_ACTIVE); 198126706Swpaul if (sc->ndis_res_io == NULL) { 199126706Swpaul device_printf(dev, 200126706Swpaul "couldn't map iospace\n"); 201126706Swpaul error = ENXIO; 202126706Swpaul goto fail; 203126706Swpaul } 204145485Swpaul pci_enable_io(dev, SYS_RES_IOPORT); 205126706Swpaul break; 206126706Swpaul case SYS_RES_MEMORY: 207126706Swpaul if (sc->ndis_res_altmem != NULL && 208126706Swpaul sc->ndis_res_mem != NULL) { 209126706Swpaul device_printf(dev, 210126706Swpaul "too many memory resources\n"); 211126706Swpaul error = ENXIO; 212126706Swpaul goto fail; 213126706Swpaul } 214133876Swpaul if (sc->ndis_res_mem) { 215126706Swpaul sc->ndis_altmem_rid = rle->rid; 216126706Swpaul sc->ndis_res_altmem = 217127281Swpaul bus_alloc_resource(dev, 218126706Swpaul SYS_RES_MEMORY, 219126706Swpaul &sc->ndis_altmem_rid, 220127281Swpaul 0, ~0, 1, RF_ACTIVE); 221126706Swpaul if (sc->ndis_res_altmem == NULL) { 222126706Swpaul device_printf(dev, 223126706Swpaul "couldn't map alt " 224126706Swpaul "memory\n"); 225126706Swpaul error = ENXIO; 226126706Swpaul goto fail; 227126706Swpaul } 228126706Swpaul } else { 229126706Swpaul sc->ndis_mem_rid = rle->rid; 230126706Swpaul sc->ndis_res_mem = 231127281Swpaul bus_alloc_resource(dev, 232126706Swpaul SYS_RES_MEMORY, 233126706Swpaul &sc->ndis_mem_rid, 234127281Swpaul 0, ~0, 1, RF_ACTIVE); 235126706Swpaul if (sc->ndis_res_mem == NULL) { 236126706Swpaul device_printf(dev, 237126706Swpaul "couldn't map memory\n"); 238126706Swpaul error = ENXIO; 239126706Swpaul goto fail; 240126706Swpaul } 241126706Swpaul } 242145485Swpaul pci_enable_io(dev, SYS_RES_MEMORY); 243126706Swpaul break; 244126706Swpaul case SYS_RES_IRQ: 245126706Swpaul rid = rle->rid; 246127281Swpaul sc->ndis_irq = bus_alloc_resource(dev, 247127281Swpaul SYS_RES_IRQ, &rid, 0, ~0, 1, 248126706Swpaul RF_SHAREABLE | RF_ACTIVE); 249126706Swpaul if (sc->ndis_irq == NULL) { 250126706Swpaul device_printf(dev, 251126706Swpaul "couldn't map interrupt\n"); 252126706Swpaul error = ENXIO; 253126706Swpaul goto fail; 254126706Swpaul } 255126706Swpaul break; 256126706Swpaul default: 257126706Swpaul break; 258126706Swpaul } 259126706Swpaul sc->ndis_rescnt++; 260126706Swpaul } 261126706Swpaul } 262126706Swpaul 263126706Swpaul /* 264126780Swpaul * If the BIOS did not set up an interrupt for this device, 265126780Swpaul * the resource traversal code above will fail to set up 266126780Swpaul * an IRQ resource. This is usually a bad thing, so try to 267126780Swpaul * force the allocation of an interrupt here. If one was 268126780Swpaul * not assigned to us by the BIOS, bus_alloc_resource() 269126780Swpaul * should route one for us. 270126780Swpaul */ 271126780Swpaul if (sc->ndis_irq == NULL) { 272126780Swpaul rid = 0; 273126780Swpaul sc->ndis_irq = bus_alloc_resource(dev, SYS_RES_IRQ, 274126780Swpaul &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE); 275126780Swpaul if (sc->ndis_irq == NULL) { 276126780Swpaul device_printf(dev, "couldn't route interrupt\n"); 277126780Swpaul error = ENXIO; 278126780Swpaul goto fail; 279126780Swpaul } 280126780Swpaul sc->ndis_rescnt++; 281126780Swpaul } 282126780Swpaul 283126780Swpaul /* 284126706Swpaul * Allocate the parent bus DMA tag appropriate for PCI. 285126706Swpaul */ 286126706Swpaul#define NDIS_NSEG_NEW 32 287126706Swpaul error = bus_dma_tag_create(NULL, /* parent */ 288126706Swpaul 1, 0, /* alignment, boundary */ 289126706Swpaul BUS_SPACE_MAXADDR_32BIT,/* lowaddr */ 290126706Swpaul BUS_SPACE_MAXADDR, /* highaddr */ 291126706Swpaul NULL, NULL, /* filter, filterarg */ 292126706Swpaul MAXBSIZE, NDIS_NSEG_NEW,/* maxsize, nsegments */ 293126706Swpaul BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */ 294126706Swpaul BUS_DMA_ALLOCNOW, /* flags */ 295126706Swpaul NULL, NULL, /* lockfunc, lockarg */ 296126706Swpaul &sc->ndis_parent_tag); 297126706Swpaul 298126706Swpaul if (error) 299126706Swpaul goto fail; 300126706Swpaul 301126706Swpaul sc->ndis_iftype = PCIBus; 302126706Swpaul 303126706Swpaul /* Figure out exactly which device we matched. */ 304126706Swpaul 305145485Swpaul t = db->windrv_devlist; 306126706Swpaul 307126706Swpaul while(t->ndis_name != NULL) { 308126706Swpaul if ((pci_get_vendor(dev) == t->ndis_vid) && 309126706Swpaul (pci_get_device(dev) == t->ndis_did)) { 310126706Swpaul if (t->ndis_subsys == 0) 311126706Swpaul defidx = devidx; 312126706Swpaul else { 313126706Swpaul if (t->ndis_subsys == 314126706Swpaul pci_read_config(dev, PCIR_SUBVEND_0, 4)) 315126706Swpaul break; 316126706Swpaul } 317126706Swpaul } 318126706Swpaul t++; 319126706Swpaul devidx++; 320126706Swpaul } 321126706Swpaul 322145995Swpaul if (t->ndis_name == NULL) 323126706Swpaul sc->ndis_devidx = defidx; 324126706Swpaul else 325126706Swpaul sc->ndis_devidx = devidx; 326126706Swpaul 327126706Swpaul error = ndis_attach(dev); 328126706Swpaul 329126706Swpaulfail: 330126706Swpaul return(error); 331126706Swpaul} 332126706Swpaul 333131953Swpaulstatic struct resource_list * 334131953Swpaulndis_get_resource_list(dev, child) 335131953Swpaul device_t dev; 336131953Swpaul device_t child; 337131953Swpaul{ 338131953Swpaul struct ndis_softc *sc; 339131953Swpaul 340131953Swpaul sc = device_get_softc(dev); 341131953Swpaul return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev)); 342131953Swpaul} 343