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: stable/11/sys/dev/if_ndis/if_ndis_pci.c 368650 2020-12-14 22:53:19Z brooks $"); 35126706Swpaul 36126706Swpaul#include <sys/param.h> 37126706Swpaul#include <sys/systm.h> 38126706Swpaul#include <sys/kernel.h> 39257176Sglebius#include <sys/malloc.h> 40129972Swpaul#include <sys/module.h> 41126706Swpaul#include <sys/socket.h> 42126706Swpaul#include <sys/queue.h> 43126706Swpaul#include <sys/sysctl.h> 44126706Swpaul 45126706Swpaul#include <net/if.h> 46257176Sglebius#include <net/if_var.h> 47126706Swpaul#include <net/if_arp.h> 48126706Swpaul#include <net/if_media.h> 49257241Sglebius#include <net/ethernet.h> 50126706Swpaul 51126706Swpaul#include <machine/bus.h> 52126706Swpaul#include <machine/resource.h> 53126706Swpaul#include <sys/bus.h> 54126706Swpaul#include <sys/rman.h> 55126706Swpaul 56126706Swpaul#include <net80211/ieee80211_var.h> 57126706Swpaul 58126706Swpaul#include <dev/pci/pcireg.h> 59126706Swpaul#include <dev/pci/pcivar.h> 60189488Sweongyo#include <dev/usb/usb.h> 61194677Sthompsa#include <dev/usb/usbdi.h> 62126706Swpaul 63126706Swpaul#include <compat/ndis/pe_var.h> 64145485Swpaul#include <compat/ndis/cfg_var.h> 65126706Swpaul#include <compat/ndis/resource_var.h> 66126706Swpaul#include <compat/ndis/ntoskrnl_var.h> 67126706Swpaul#include <compat/ndis/ndis_var.h> 68126706Swpaul#include <dev/if_ndis/if_ndisvar.h> 69126706Swpaul 70126706SwpaulMODULE_DEPEND(ndis, pci, 1, 1, 1); 71126706Swpaul 72126706Swpaulstatic int ndis_probe_pci (device_t); 73126706Swpaulstatic int ndis_attach_pci (device_t); 74131953Swpaulstatic struct resource_list *ndis_get_resource_list 75131953Swpaul (device_t, device_t); 76146016Swpaulstatic int ndis_devcompare (interface_type, 77146016Swpaul struct ndis_pci_type *, device_t); 78141524Swpaulextern int ndisdrv_modevent (module_t, int, void *); 79126706Swpaulextern int ndis_attach (device_t); 80126706Swpaulextern int ndis_shutdown (device_t); 81126706Swpaulextern int ndis_detach (device_t); 82126706Swpaulextern int ndis_suspend (device_t); 83126706Swpaulextern int ndis_resume (device_t); 84126706Swpaul 85126706Swpaulstatic device_method_t ndis_methods[] = { 86126706Swpaul /* Device interface */ 87126706Swpaul DEVMETHOD(device_probe, ndis_probe_pci), 88126706Swpaul DEVMETHOD(device_attach, ndis_attach_pci), 89126706Swpaul DEVMETHOD(device_detach, ndis_detach), 90126706Swpaul DEVMETHOD(device_shutdown, ndis_shutdown), 91126706Swpaul DEVMETHOD(device_suspend, ndis_suspend), 92126706Swpaul DEVMETHOD(device_resume, ndis_resume), 93126706Swpaul 94131953Swpaul /* Bus interface */ 95131953Swpaul DEVMETHOD(bus_get_resource_list, ndis_get_resource_list), 96131953Swpaul 97126706Swpaul { 0, 0 } 98126706Swpaul}; 99126706Swpaul 100126706Swpaulstatic driver_t ndis_driver = { 101126706Swpaul "ndis", 102126706Swpaul ndis_methods, 103126706Swpaul sizeof(struct ndis_softc) 104126706Swpaul}; 105126706Swpaul 106126706Swpaulstatic devclass_t ndis_devclass; 107126706Swpaul 108141524SwpaulDRIVER_MODULE(ndis, pci, ndis_driver, ndis_devclass, ndisdrv_modevent, 0); 109126706Swpaul 110145485Swpaulstatic int 111146016Swpaulndis_devcompare(bustype, t, dev) 112146016Swpaul interface_type bustype; 113145485Swpaul struct ndis_pci_type *t; 114145485Swpaul device_t dev; 115145485Swpaul{ 116216558Stijl uint16_t vid, did; 117216558Stijl uint32_t subsys; 118216558Stijl 119146016Swpaul if (bustype != PCIBus) 120146016Swpaul return(FALSE); 121146016Swpaul 122216558Stijl vid = pci_get_vendor(dev); 123216558Stijl did = pci_get_device(dev); 124216558Stijl subsys = pci_get_subdevice(dev); 125216558Stijl subsys = (subsys << 16) | pci_get_subvendor(dev); 126216558Stijl 127145485Swpaul while(t->ndis_name != NULL) { 128216558Stijl if ((t->ndis_vid == vid) && (t->ndis_did == did) && 129216558Stijl (t->ndis_subsys == subsys || t->ndis_subsys == 0)) { 130145485Swpaul device_set_desc(dev, t->ndis_name); 131145485Swpaul return(TRUE); 132145485Swpaul } 133145485Swpaul t++; 134145485Swpaul } 135145485Swpaul 136145485Swpaul return(FALSE); 137145485Swpaul} 138145485Swpaul 139126706Swpaul/* 140126706Swpaul * Probe for an NDIS device. Check the PCI vendor and device 141126706Swpaul * IDs against our list and return a device name if we find a match. 142126706Swpaul */ 143126706Swpaulstatic int 144126706Swpaulndis_probe_pci(dev) 145126706Swpaul device_t dev; 146126706Swpaul{ 147141524Swpaul driver_object *drv; 148145485Swpaul struct drvdb_ent *db; 149126706Swpaul 150142804Swpaul drv = windrv_lookup(0, "PCI Bus"); 151126706Swpaul 152141524Swpaul if (drv == NULL) 153141524Swpaul return(ENXIO); 154141524Swpaul 155145485Swpaul db = windrv_match((matchfuncptr)ndis_devcompare, dev); 156141524Swpaul 157145485Swpaul if (db != NULL) { 158145485Swpaul /* Create PDO for this device instance */ 159145485Swpaul windrv_create_pdo(drv, dev); 160145485Swpaul return(0); 161126706Swpaul } 162126706Swpaul 163126706Swpaul return(ENXIO); 164126706Swpaul} 165126706Swpaul 166126706Swpaul/* 167126706Swpaul * Attach the interface. Allocate softc structures, do ifmedia 168126706Swpaul * setup and ethernet/BPF attach. 169126706Swpaul */ 170126706Swpaulstatic int 171126706Swpaulndis_attach_pci(dev) 172126706Swpaul device_t dev; 173126706Swpaul{ 174126706Swpaul struct ndis_softc *sc; 175126706Swpaul int unit, error = 0, rid; 176126706Swpaul struct ndis_pci_type *t; 177126706Swpaul int devidx = 0, defidx = 0; 178126706Swpaul struct resource_list *rl; 179126706Swpaul struct resource_list_entry *rle; 180145485Swpaul struct drvdb_ent *db; 181216558Stijl uint16_t vid, did; 182216558Stijl uint32_t subsys; 183126706Swpaul 184126706Swpaul sc = device_get_softc(dev); 185126706Swpaul unit = device_get_unit(dev); 186126706Swpaul sc->ndis_dev = dev; 187126706Swpaul 188145485Swpaul db = windrv_match((matchfuncptr)ndis_devcompare, dev); 189145485Swpaul if (db == NULL) 190145485Swpaul return (ENXIO); 191145485Swpaul sc->ndis_dobj = db->windrv_object; 192145485Swpaul sc->ndis_regvals = db->windrv_regvals; 193145485Swpaul 194126706Swpaul /* 195126706Swpaul * Map control/status registers. 196126706Swpaul */ 197126706Swpaul 198126706Swpaul pci_enable_busmaster(dev); 199126706Swpaul 200126706Swpaul rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 201126706Swpaul if (rl != NULL) { 202143848Smaxim STAILQ_FOREACH(rle, rl, link) { 203126706Swpaul switch (rle->type) { 204126706Swpaul case SYS_RES_IOPORT: 205126706Swpaul sc->ndis_io_rid = rle->rid; 206216486Sjhb sc->ndis_res_io = bus_alloc_resource_any(dev, 207126706Swpaul SYS_RES_IOPORT, &sc->ndis_io_rid, 208216486Sjhb RF_ACTIVE); 209126706Swpaul if (sc->ndis_res_io == NULL) { 210126706Swpaul device_printf(dev, 211126706Swpaul "couldn't map iospace\n"); 212126706Swpaul error = ENXIO; 213126706Swpaul goto fail; 214126706Swpaul } 215126706Swpaul break; 216126706Swpaul case SYS_RES_MEMORY: 217126706Swpaul if (sc->ndis_res_altmem != NULL && 218126706Swpaul sc->ndis_res_mem != NULL) { 219126706Swpaul device_printf(dev, 220126706Swpaul "too many memory resources\n"); 221126706Swpaul error = ENXIO; 222126706Swpaul goto fail; 223126706Swpaul } 224133876Swpaul if (sc->ndis_res_mem) { 225126706Swpaul sc->ndis_altmem_rid = rle->rid; 226126706Swpaul sc->ndis_res_altmem = 227216486Sjhb bus_alloc_resource_any(dev, 228126706Swpaul SYS_RES_MEMORY, 229126706Swpaul &sc->ndis_altmem_rid, 230216486Sjhb RF_ACTIVE); 231126706Swpaul if (sc->ndis_res_altmem == NULL) { 232126706Swpaul device_printf(dev, 233126706Swpaul "couldn't map alt " 234126706Swpaul "memory\n"); 235126706Swpaul error = ENXIO; 236126706Swpaul goto fail; 237126706Swpaul } 238126706Swpaul } else { 239126706Swpaul sc->ndis_mem_rid = rle->rid; 240126706Swpaul sc->ndis_res_mem = 241216486Sjhb bus_alloc_resource_any(dev, 242126706Swpaul SYS_RES_MEMORY, 243126706Swpaul &sc->ndis_mem_rid, 244216486Sjhb RF_ACTIVE); 245126706Swpaul if (sc->ndis_res_mem == NULL) { 246126706Swpaul device_printf(dev, 247126706Swpaul "couldn't map memory\n"); 248126706Swpaul error = ENXIO; 249126706Swpaul goto fail; 250126706Swpaul } 251126706Swpaul } 252126706Swpaul break; 253126706Swpaul case SYS_RES_IRQ: 254126706Swpaul rid = rle->rid; 255216486Sjhb sc->ndis_irq = bus_alloc_resource_any(dev, 256216486Sjhb SYS_RES_IRQ, &rid, 257216486Sjhb RF_SHAREABLE | RF_ACTIVE); 258126706Swpaul if (sc->ndis_irq == NULL) { 259126706Swpaul device_printf(dev, 260126706Swpaul "couldn't map interrupt\n"); 261126706Swpaul error = ENXIO; 262126706Swpaul goto fail; 263126706Swpaul } 264126706Swpaul break; 265126706Swpaul default: 266126706Swpaul break; 267126706Swpaul } 268126706Swpaul sc->ndis_rescnt++; 269126706Swpaul } 270126706Swpaul } 271126706Swpaul 272126706Swpaul /* 273126780Swpaul * If the BIOS did not set up an interrupt for this device, 274126780Swpaul * the resource traversal code above will fail to set up 275126780Swpaul * an IRQ resource. This is usually a bad thing, so try to 276126780Swpaul * force the allocation of an interrupt here. If one was 277126780Swpaul * not assigned to us by the BIOS, bus_alloc_resource() 278126780Swpaul * should route one for us. 279126780Swpaul */ 280126780Swpaul if (sc->ndis_irq == NULL) { 281126780Swpaul rid = 0; 282216486Sjhb sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 283216486Sjhb &rid, RF_SHAREABLE | RF_ACTIVE); 284126780Swpaul if (sc->ndis_irq == NULL) { 285126780Swpaul device_printf(dev, "couldn't route interrupt\n"); 286126780Swpaul error = ENXIO; 287126780Swpaul goto fail; 288126780Swpaul } 289126780Swpaul sc->ndis_rescnt++; 290126780Swpaul } 291126780Swpaul 292126780Swpaul /* 293126706Swpaul * Allocate the parent bus DMA tag appropriate for PCI. 294126706Swpaul */ 295126706Swpaul#define NDIS_NSEG_NEW 32 296232854Sscottl error = bus_dma_tag_create(bus_get_dma_tag(dev),/* PCI parent */ 297126706Swpaul 1, 0, /* alignment, boundary */ 298126706Swpaul BUS_SPACE_MAXADDR_32BIT,/* lowaddr */ 299126706Swpaul BUS_SPACE_MAXADDR, /* highaddr */ 300126706Swpaul NULL, NULL, /* filter, filterarg */ 301292411Sglebius DFLTPHYS, NDIS_NSEG_NEW,/* maxsize, nsegments */ 302126706Swpaul BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */ 303126706Swpaul BUS_DMA_ALLOCNOW, /* flags */ 304126706Swpaul NULL, NULL, /* lockfunc, lockarg */ 305126706Swpaul &sc->ndis_parent_tag); 306126706Swpaul 307126706Swpaul if (error) 308126706Swpaul goto fail; 309126706Swpaul 310126706Swpaul sc->ndis_iftype = PCIBus; 311126706Swpaul 312126706Swpaul /* Figure out exactly which device we matched. */ 313126706Swpaul 314216558Stijl vid = pci_get_vendor(dev); 315216558Stijl did = pci_get_device(dev); 316216558Stijl subsys = pci_get_subdevice(dev); 317216558Stijl subsys = (subsys << 16) | pci_get_subvendor(dev); 318216558Stijl 319145485Swpaul t = db->windrv_devlist; 320126706Swpaul 321126706Swpaul while(t->ndis_name != NULL) { 322216558Stijl if (t->ndis_vid == vid && t->ndis_did == did) { 323126706Swpaul if (t->ndis_subsys == 0) 324126706Swpaul defidx = devidx; 325216558Stijl else if (t->ndis_subsys == subsys) 326216518Stijl break; 327126706Swpaul } 328126706Swpaul t++; 329126706Swpaul devidx++; 330126706Swpaul } 331126706Swpaul 332145995Swpaul if (t->ndis_name == NULL) 333126706Swpaul sc->ndis_devidx = defidx; 334126706Swpaul else 335126706Swpaul sc->ndis_devidx = devidx; 336126706Swpaul 337126706Swpaul error = ndis_attach(dev); 338368650Sbrooks if (error == 0) 339368650Sbrooks gone_in_dev(dev, 13, "ndis removed"); 340126706Swpaul 341368650Sbrooks 342126706Swpaulfail: 343126706Swpaul return(error); 344126706Swpaul} 345126706Swpaul 346131953Swpaulstatic struct resource_list * 347131953Swpaulndis_get_resource_list(dev, child) 348131953Swpaul device_t dev; 349131953Swpaul device_t child; 350131953Swpaul{ 351131953Swpaul struct ndis_softc *sc; 352131953Swpaul 353131953Swpaul sc = device_get_softc(dev); 354131953Swpaul return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev)); 355131953Swpaul} 356