3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
32 * $FreeBSD: head/sys/dev/if_ndis/if_ndis_pci.c 292411 2015-12-17 21:01:19Z glebius $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/socket.h>
41 #include <sys/queue.h>
42 #include <sys/sysctl.h>
46 #include <net/if_var.h>
47 #include <net/if_arp.h>
48 #include <net/if_media.h>
49 #include <net/ethernet.h>
51 #if !defined(__DragonFly__)
52 #include <machine/bus.h>
53 #include <machine/resource.h>
58 #if defined(__DragonFly__)
59 #include <netproto/802_11/ieee80211_var.h>
61 #include <net80211/ieee80211_var.h>
64 #if defined(__DragonFly__)
65 #include <bus/pci/pcireg.h>
66 #include <bus/pci/pcivar.h>
67 #include <bus/u4b/usb.h>
68 #include <bus/u4b/usbdi.h>
70 #include <dev/pci/pcireg.h>
71 #include <dev/pci/pcivar.h>
72 #include <dev/usb/usb.h>
73 #include <dev/usb/usbdi.h>
76 #if defined(__DragonFly__)
77 #include <emulation/ndis/pe_var.h>
78 #include <emulation/ndis/cfg_var.h>
79 #include <emulation/ndis/resource_var.h>
80 #include <emulation/ndis/ntoskrnl_var.h>
81 #include <emulation/ndis/ndis_var.h>
82 #include <dev/netif/ndis/if_ndisvar.h>
84 #include <compat/ndis/pe_var.h>
85 #include <compat/ndis/cfg_var.h>
86 #include <compat/ndis/resource_var.h>
87 #include <compat/ndis/ntoskrnl_var.h>
88 #include <compat/ndis/ndis_var.h>
89 #include <dev/if_ndis/if_ndisvar.h>
92 MODULE_DEPEND(if_ndis, pci, 1, 1, 1);
94 static int ndis_probe_pci (device_t);
95 static int ndis_attach_pci (device_t);
96 static struct resource_list *ndis_get_resource_list
98 static int ndis_devcompare (interface_type,
99 struct ndis_pci_type *, device_t);
100 extern int ndisdrv_modevent (module_t, int, void *);
101 extern int ndis_attach (device_t);
102 extern int ndis_shutdown (device_t);
103 extern int ndis_detach (device_t);
104 extern int ndis_suspend (device_t);
105 extern int ndis_resume (device_t);
107 static device_method_t ndis_methods[] = {
108 /* Device interface */
109 DEVMETHOD(device_probe, ndis_probe_pci),
110 DEVMETHOD(device_attach, ndis_attach_pci),
111 DEVMETHOD(device_detach, ndis_detach),
112 DEVMETHOD(device_shutdown, ndis_shutdown),
113 DEVMETHOD(device_suspend, ndis_suspend),
114 DEVMETHOD(device_resume, ndis_resume),
117 DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
122 static driver_t ndis_driver = {
125 sizeof(struct ndis_softc)
128 static devclass_t ndis_devclass;
130 DRIVER_MODULE(if_ndis, pci, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
133 ndis_devcompare(interface_type bustype, struct ndis_pci_type *t, device_t dev)
138 if (bustype != PCIBus)
141 vid = pci_get_vendor(dev);
142 did = pci_get_device(dev);
143 subsys = pci_get_subdevice(dev);
144 subsys = (subsys << 16) | pci_get_subvendor(dev);
146 while(t->ndis_name != NULL) {
147 if ((t->ndis_vid == vid) && (t->ndis_did == did) &&
148 (t->ndis_subsys == subsys || t->ndis_subsys == 0)) {
149 device_set_desc(dev, t->ndis_name);
159 * Probe for an NDIS device. Check the PCI vendor and device
160 * IDs against our list and return a device name if we find a match.
163 ndis_probe_pci(device_t dev)
166 struct drvdb_ent *db;
168 drv = windrv_lookup(0, "PCI Bus");
173 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
176 /* Create PDO for this device instance */
177 windrv_create_pdo(drv, dev);
185 * Attach the interface. Allocate softc structures, do ifmedia
186 * setup and ethernet/BPF attach.
189 ndis_attach_pci(device_t dev)
191 struct ndis_softc *sc;
192 int unit, error = 0, rid;
193 struct ndis_pci_type *t;
194 int devidx = 0, defidx = 0;
195 struct resource_list *rl;
196 struct resource_list_entry *rle;
197 struct drvdb_ent *db;
201 sc = device_get_softc(dev);
202 unit = device_get_unit(dev);
205 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
208 sc->ndis_dobj = db->windrv_object;
209 sc->ndis_regvals = db->windrv_regvals;
212 * Map control/status registers.
215 pci_enable_busmaster(dev);
217 rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
219 SLIST_FOREACH(rle, rl, link) {
222 sc->ndis_io_rid = rle->rid;
223 sc->ndis_res_io = bus_alloc_resource_any(dev,
224 SYS_RES_IOPORT, &sc->ndis_io_rid,
226 if (sc->ndis_res_io == NULL) {
228 "couldn't map iospace\n");
234 if (sc->ndis_res_altmem != NULL &&
235 sc->ndis_res_mem != NULL) {
237 "too many memory resources\n");
241 if (sc->ndis_res_mem) {
242 sc->ndis_altmem_rid = rle->rid;
243 sc->ndis_res_altmem =
244 bus_alloc_resource_any(dev,
246 &sc->ndis_altmem_rid,
248 if (sc->ndis_res_altmem == NULL) {
256 sc->ndis_mem_rid = rle->rid;
258 bus_alloc_resource_any(dev,
262 if (sc->ndis_res_mem == NULL) {
264 "couldn't map memory\n");
272 sc->ndis_irq = bus_alloc_resource_any(dev,
274 RF_SHAREABLE | RF_ACTIVE);
275 if (sc->ndis_irq == NULL) {
277 "couldn't map interrupt\n");
290 * If the BIOS did not set up an interrupt for this device,
291 * the resource traversal code above will fail to set up
292 * an IRQ resource. This is usually a bad thing, so try to
293 * force the allocation of an interrupt here. If one was
294 * not assigned to us by the BIOS, bus_alloc_resource()
295 * should route one for us.
297 if (sc->ndis_irq == NULL) {
299 sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
300 &rid, RF_SHAREABLE | RF_ACTIVE);
301 if (sc->ndis_irq == NULL) {
302 device_printf(dev, "couldn't route interrupt\n");
310 * Allocate the parent bus DMA tag appropriate for PCI.
312 #define NDIS_NSEG_NEW 32
313 error = bus_dma_tag_create(NULL, /* parent */
314 1, 0, /* alignment, boundary */
315 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
316 BUS_SPACE_MAXADDR, /* highaddr */
317 NULL, NULL, /* filter, filterarg */
318 DFLTPHYS, NDIS_NSEG_NEW,/* maxsize, nsegments */
319 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
320 BUS_DMA_ALLOCNOW, /* flags */
321 &sc->ndis_parent_tag);
326 sc->ndis_iftype = PCIBus;
328 /* Figure out exactly which device we matched. */
330 vid = pci_get_vendor(dev);
331 did = pci_get_device(dev);
332 subsys = pci_get_subdevice(dev);
333 subsys = (subsys << 16) | pci_get_subvendor(dev);
335 t = db->windrv_devlist;
337 while(t->ndis_name != NULL) {
338 if (t->ndis_vid == vid && t->ndis_did == did) {
339 if (t->ndis_subsys == 0)
341 else if (t->ndis_subsys == subsys)
348 if (t->ndis_name == NULL)
349 sc->ndis_devidx = defidx;
351 sc->ndis_devidx = devidx;
353 error = ndis_attach(dev);
359 static struct resource_list *
360 ndis_get_resource_list(device_t dev, device_t child)
362 struct ndis_softc *sc;
364 sc = device_get_softc(dev);
365 return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev));