if_ndis_pci.c revision 145485
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 145485 2005-04-24 20:21:22Z wpaul $"); 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> 57126706Swpaul 58126706Swpaul#include <compat/ndis/pe_var.h> 59145485Swpaul#include <compat/ndis/cfg_var.h> 60126706Swpaul#include <compat/ndis/resource_var.h> 61126706Swpaul#include <compat/ndis/ntoskrnl_var.h> 62126706Swpaul#include <compat/ndis/ndis_var.h> 63126706Swpaul#include <dev/if_ndis/if_ndisvar.h> 64126706Swpaul 65126706SwpaulMODULE_DEPEND(ndis, pci, 1, 1, 1); 66126706Swpaul 67126706Swpaulstatic int ndis_probe_pci (device_t); 68126706Swpaulstatic int ndis_attach_pci (device_t); 69131953Swpaulstatic struct resource_list *ndis_get_resource_list 70131953Swpaul (device_t, device_t); 71145485Swpaulstatic int ndis_devcompare (struct ndis_pci_type *, device_t); 72141524Swpaulextern int ndisdrv_modevent (module_t, int, void *); 73126706Swpaulextern int ndis_attach (device_t); 74126706Swpaulextern int ndis_shutdown (device_t); 75126706Swpaulextern int ndis_detach (device_t); 76126706Swpaulextern int ndis_suspend (device_t); 77126706Swpaulextern int ndis_resume (device_t); 78126706Swpaul 79126706Swpaulstatic device_method_t ndis_methods[] = { 80126706Swpaul /* Device interface */ 81126706Swpaul DEVMETHOD(device_probe, ndis_probe_pci), 82126706Swpaul DEVMETHOD(device_attach, ndis_attach_pci), 83126706Swpaul DEVMETHOD(device_detach, ndis_detach), 84126706Swpaul DEVMETHOD(device_shutdown, ndis_shutdown), 85126706Swpaul DEVMETHOD(device_suspend, ndis_suspend), 86126706Swpaul DEVMETHOD(device_resume, ndis_resume), 87126706Swpaul 88131953Swpaul /* Bus interface */ 89131953Swpaul DEVMETHOD(bus_get_resource_list, ndis_get_resource_list), 90131953Swpaul 91126706Swpaul { 0, 0 } 92126706Swpaul}; 93126706Swpaul 94126706Swpaulstatic driver_t ndis_driver = { 95126706Swpaul "ndis", 96126706Swpaul ndis_methods, 97126706Swpaul sizeof(struct ndis_softc) 98126706Swpaul}; 99126706Swpaul 100126706Swpaulstatic devclass_t ndis_devclass; 101126706Swpaul 102141524SwpaulDRIVER_MODULE(ndis, pci, ndis_driver, ndis_devclass, ndisdrv_modevent, 0); 103141524SwpaulDRIVER_MODULE(ndis, cardbus, ndis_driver, ndis_devclass, ndisdrv_modevent, 0); 104126706Swpaul 105145485Swpaulstatic int 106145485Swpaulndis_devcompare(t, dev) 107145485Swpaul struct ndis_pci_type *t; 108145485Swpaul device_t dev; 109145485Swpaul{ 110145485Swpaul while(t->ndis_name != NULL) { 111145485Swpaul if ((pci_get_vendor(dev) == t->ndis_vid) && 112145485Swpaul (pci_get_device(dev) == t->ndis_did) && 113145485Swpaul ((pci_read_config(dev, PCIR_SUBVEND_0, 4) == 114145485Swpaul t->ndis_subsys) || t->ndis_subsys == 0)) { 115145485Swpaul device_set_desc(dev, t->ndis_name); 116145485Swpaul return(TRUE); 117145485Swpaul } 118145485Swpaul t++; 119145485Swpaul } 120145485Swpaul 121145485Swpaul return(FALSE); 122145485Swpaul} 123145485Swpaul 124126706Swpaul/* 125126706Swpaul * Probe for an NDIS device. Check the PCI vendor and device 126126706Swpaul * IDs against our list and return a device name if we find a match. 127126706Swpaul */ 128126706Swpaulstatic int 129126706Swpaulndis_probe_pci(dev) 130126706Swpaul device_t dev; 131126706Swpaul{ 132141524Swpaul driver_object *drv; 133145485Swpaul struct drvdb_ent *db; 134126706Swpaul 135142804Swpaul drv = windrv_lookup(0, "PCI Bus"); 136126706Swpaul 137141524Swpaul if (drv == NULL) 138141524Swpaul return(ENXIO); 139141524Swpaul 140145485Swpaul db = windrv_match((matchfuncptr)ndis_devcompare, dev); 141141524Swpaul 142145485Swpaul if (db != NULL) { 143145485Swpaul /* Create PDO for this device instance */ 144145485Swpaul windrv_create_pdo(drv, dev); 145145485Swpaul return(0); 146126706Swpaul } 147126706Swpaul 148126706Swpaul return(ENXIO); 149126706Swpaul} 150126706Swpaul 151126706Swpaul/* 152126706Swpaul * Attach the interface. Allocate softc structures, do ifmedia 153126706Swpaul * setup and ethernet/BPF attach. 154126706Swpaul */ 155126706Swpaulstatic int 156126706Swpaulndis_attach_pci(dev) 157126706Swpaul device_t dev; 158126706Swpaul{ 159126706Swpaul struct ndis_softc *sc; 160126706Swpaul int unit, error = 0, rid; 161126706Swpaul struct ndis_pci_type *t; 162126706Swpaul int devidx = 0, defidx = 0; 163126706Swpaul struct resource_list *rl; 164126706Swpaul struct resource_list_entry *rle; 165145485Swpaul struct drvdb_ent *db; 166126706Swpaul 167126706Swpaul sc = device_get_softc(dev); 168126706Swpaul unit = device_get_unit(dev); 169126706Swpaul sc->ndis_dev = dev; 170126706Swpaul 171145485Swpaul db = windrv_match((matchfuncptr)ndis_devcompare, dev); 172145485Swpaul if (db == NULL) 173145485Swpaul return (ENXIO); 174145485Swpaul sc->ndis_dobj = db->windrv_object; 175145485Swpaul sc->ndis_regvals = db->windrv_regvals; 176145485Swpaul 177126706Swpaul /* 178126706Swpaul * Map control/status registers. 179126706Swpaul */ 180126706Swpaul 181126706Swpaul pci_enable_busmaster(dev); 182126706Swpaul 183126706Swpaul rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 184126706Swpaul if (rl != NULL) { 185144176Swpaul#if __FreeBSD_version < 600022 186144176Swpaul SLIST_FOREACH(rle, rl, link) { 187144176Swpaul#else 188143848Smaxim STAILQ_FOREACH(rle, rl, link) { 189144176Swpaul#endif 190126706Swpaul switch (rle->type) { 191126706Swpaul case SYS_RES_IOPORT: 192126706Swpaul sc->ndis_io_rid = rle->rid; 193127281Swpaul sc->ndis_res_io = bus_alloc_resource(dev, 194126706Swpaul SYS_RES_IOPORT, &sc->ndis_io_rid, 195127281Swpaul 0, ~0, 1, RF_ACTIVE); 196126706Swpaul if (sc->ndis_res_io == NULL) { 197126706Swpaul device_printf(dev, 198126706Swpaul "couldn't map iospace\n"); 199126706Swpaul error = ENXIO; 200126706Swpaul goto fail; 201126706Swpaul } 202145485Swpaul pci_enable_io(dev, SYS_RES_IOPORT); 203126706Swpaul break; 204126706Swpaul case SYS_RES_MEMORY: 205126706Swpaul if (sc->ndis_res_altmem != NULL && 206126706Swpaul sc->ndis_res_mem != NULL) { 207126706Swpaul device_printf(dev, 208126706Swpaul "too many memory resources\n"); 209126706Swpaul error = ENXIO; 210126706Swpaul goto fail; 211126706Swpaul } 212133876Swpaul if (sc->ndis_res_mem) { 213126706Swpaul sc->ndis_altmem_rid = rle->rid; 214126706Swpaul sc->ndis_res_altmem = 215127281Swpaul bus_alloc_resource(dev, 216126706Swpaul SYS_RES_MEMORY, 217126706Swpaul &sc->ndis_altmem_rid, 218127281Swpaul 0, ~0, 1, RF_ACTIVE); 219126706Swpaul if (sc->ndis_res_altmem == NULL) { 220126706Swpaul device_printf(dev, 221126706Swpaul "couldn't map alt " 222126706Swpaul "memory\n"); 223126706Swpaul error = ENXIO; 224126706Swpaul goto fail; 225126706Swpaul } 226126706Swpaul } else { 227126706Swpaul sc->ndis_mem_rid = rle->rid; 228126706Swpaul sc->ndis_res_mem = 229127281Swpaul bus_alloc_resource(dev, 230126706Swpaul SYS_RES_MEMORY, 231126706Swpaul &sc->ndis_mem_rid, 232127281Swpaul 0, ~0, 1, RF_ACTIVE); 233126706Swpaul if (sc->ndis_res_mem == NULL) { 234126706Swpaul device_printf(dev, 235126706Swpaul "couldn't map memory\n"); 236126706Swpaul error = ENXIO; 237126706Swpaul goto fail; 238126706Swpaul } 239126706Swpaul } 240145485Swpaul pci_enable_io(dev, SYS_RES_MEMORY); 241126706Swpaul break; 242126706Swpaul case SYS_RES_IRQ: 243126706Swpaul rid = rle->rid; 244127281Swpaul sc->ndis_irq = bus_alloc_resource(dev, 245127281Swpaul SYS_RES_IRQ, &rid, 0, ~0, 1, 246126706Swpaul RF_SHAREABLE | RF_ACTIVE); 247126706Swpaul if (sc->ndis_irq == NULL) { 248126706Swpaul device_printf(dev, 249126706Swpaul "couldn't map interrupt\n"); 250126706Swpaul error = ENXIO; 251126706Swpaul goto fail; 252126706Swpaul } 253126706Swpaul break; 254126706Swpaul default: 255126706Swpaul break; 256126706Swpaul } 257126706Swpaul sc->ndis_rescnt++; 258126706Swpaul } 259126706Swpaul } 260126706Swpaul 261126706Swpaul /* 262126780Swpaul * If the BIOS did not set up an interrupt for this device, 263126780Swpaul * the resource traversal code above will fail to set up 264126780Swpaul * an IRQ resource. This is usually a bad thing, so try to 265126780Swpaul * force the allocation of an interrupt here. If one was 266126780Swpaul * not assigned to us by the BIOS, bus_alloc_resource() 267126780Swpaul * should route one for us. 268126780Swpaul */ 269126780Swpaul if (sc->ndis_irq == NULL) { 270126780Swpaul rid = 0; 271126780Swpaul sc->ndis_irq = bus_alloc_resource(dev, SYS_RES_IRQ, 272126780Swpaul &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE); 273126780Swpaul if (sc->ndis_irq == NULL) { 274126780Swpaul device_printf(dev, "couldn't route interrupt\n"); 275126780Swpaul error = ENXIO; 276126780Swpaul goto fail; 277126780Swpaul } 278126780Swpaul sc->ndis_rescnt++; 279126780Swpaul } 280126780Swpaul 281126780Swpaul /* 282126706Swpaul * Allocate the parent bus DMA tag appropriate for PCI. 283126706Swpaul */ 284126706Swpaul#define NDIS_NSEG_NEW 32 285126706Swpaul error = bus_dma_tag_create(NULL, /* parent */ 286126706Swpaul 1, 0, /* alignment, boundary */ 287126706Swpaul BUS_SPACE_MAXADDR_32BIT,/* lowaddr */ 288126706Swpaul BUS_SPACE_MAXADDR, /* highaddr */ 289126706Swpaul NULL, NULL, /* filter, filterarg */ 290126706Swpaul MAXBSIZE, NDIS_NSEG_NEW,/* maxsize, nsegments */ 291126706Swpaul BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */ 292126706Swpaul BUS_DMA_ALLOCNOW, /* flags */ 293126706Swpaul NULL, NULL, /* lockfunc, lockarg */ 294126706Swpaul &sc->ndis_parent_tag); 295126706Swpaul 296126706Swpaul if (error) 297126706Swpaul goto fail; 298126706Swpaul 299126706Swpaul sc->ndis_iftype = PCIBus; 300126706Swpaul 301126706Swpaul /* Figure out exactly which device we matched. */ 302126706Swpaul 303145485Swpaul t = db->windrv_devlist; 304126706Swpaul 305126706Swpaul while(t->ndis_name != NULL) { 306126706Swpaul if ((pci_get_vendor(dev) == t->ndis_vid) && 307126706Swpaul (pci_get_device(dev) == t->ndis_did)) { 308126706Swpaul if (t->ndis_subsys == 0) 309126706Swpaul defidx = devidx; 310126706Swpaul else { 311126706Swpaul if (t->ndis_subsys == 312126706Swpaul pci_read_config(dev, PCIR_SUBVEND_0, 4)) 313126706Swpaul break; 314126706Swpaul } 315126706Swpaul } 316126706Swpaul t++; 317126706Swpaul devidx++; 318126706Swpaul } 319126706Swpaul 320145485Swpaul if (t[devidx].ndis_name == NULL) 321126706Swpaul sc->ndis_devidx = defidx; 322126706Swpaul else 323126706Swpaul sc->ndis_devidx = devidx; 324126706Swpaul 325126706Swpaul error = ndis_attach(dev); 326126706Swpaul 327126706Swpaulfail: 328126706Swpaul return(error); 329126706Swpaul} 330126706Swpaul 331131953Swpaulstatic struct resource_list * 332131953Swpaulndis_get_resource_list(dev, child) 333131953Swpaul device_t dev; 334131953Swpaul device_t child; 335131953Swpaul{ 336131953Swpaul struct ndis_softc *sc; 337131953Swpaul 338131953Swpaul sc = device_get_softc(dev); 339131953Swpaul return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev)); 340131953Swpaul} 341