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
,
134 ndis_devcompare(interface_type bustype
, struct ndis_pci_type
*t
, device_t dev
)
139 if (bustype
!= PCIBus
)
142 vid
= pci_get_vendor(dev
);
143 did
= pci_get_device(dev
);
144 subsys
= pci_get_subdevice(dev
);
145 subsys
= (subsys
<< 16) | pci_get_subvendor(dev
);
147 while(t
->ndis_name
!= NULL
) {
148 if ((t
->ndis_vid
== vid
) && (t
->ndis_did
== did
) &&
149 (t
->ndis_subsys
== subsys
|| t
->ndis_subsys
== 0)) {
150 device_set_desc(dev
, t
->ndis_name
);
160 * Probe for an NDIS device. Check the PCI vendor and device
161 * IDs against our list and return a device name if we find a match.
164 ndis_probe_pci(device_t dev
)
167 struct drvdb_ent
*db
;
169 drv
= windrv_lookup(0, "PCI Bus");
174 db
= windrv_match((matchfuncptr
)ndis_devcompare
, dev
);
177 /* Create PDO for this device instance */
178 windrv_create_pdo(drv
, dev
);
186 * Attach the interface. Allocate softc structures, do ifmedia
187 * setup and ethernet/BPF attach.
190 ndis_attach_pci(device_t dev
)
192 struct ndis_softc
*sc
;
193 int unit
, error
= 0, rid
;
194 struct ndis_pci_type
*t
;
195 int devidx
= 0, defidx
= 0;
196 struct resource_list
*rl
;
197 struct resource_list_entry
*rle
;
198 struct drvdb_ent
*db
;
202 sc
= device_get_softc(dev
);
203 unit
= device_get_unit(dev
);
206 db
= windrv_match((matchfuncptr
)ndis_devcompare
, dev
);
209 sc
->ndis_dobj
= db
->windrv_object
;
210 sc
->ndis_regvals
= db
->windrv_regvals
;
213 * Map control/status registers.
216 pci_enable_busmaster(dev
);
218 rl
= BUS_GET_RESOURCE_LIST(device_get_parent(dev
), dev
);
220 SLIST_FOREACH(rle
, rl
, link
) {
223 sc
->ndis_io_rid
= rle
->rid
;
224 sc
->ndis_res_io
= bus_alloc_resource_any(dev
,
225 SYS_RES_IOPORT
, &sc
->ndis_io_rid
,
227 if (sc
->ndis_res_io
== NULL
) {
229 "couldn't map iospace\n");
235 if (sc
->ndis_res_altmem
!= NULL
&&
236 sc
->ndis_res_mem
!= NULL
) {
238 "too many memory resources\n");
242 if (sc
->ndis_res_mem
) {
243 sc
->ndis_altmem_rid
= rle
->rid
;
244 sc
->ndis_res_altmem
=
245 bus_alloc_resource_any(dev
,
247 &sc
->ndis_altmem_rid
,
249 if (sc
->ndis_res_altmem
== NULL
) {
257 sc
->ndis_mem_rid
= rle
->rid
;
259 bus_alloc_resource_any(dev
,
263 if (sc
->ndis_res_mem
== NULL
) {
265 "couldn't map memory\n");
273 sc
->ndis_irq
= bus_alloc_resource_any(dev
,
275 RF_SHAREABLE
| RF_ACTIVE
);
276 if (sc
->ndis_irq
== NULL
) {
278 "couldn't map interrupt\n");
291 * If the BIOS did not set up an interrupt for this device,
292 * the resource traversal code above will fail to set up
293 * an IRQ resource. This is usually a bad thing, so try to
294 * force the allocation of an interrupt here. If one was
295 * not assigned to us by the BIOS, bus_alloc_resource()
296 * should route one for us.
298 if (sc
->ndis_irq
== NULL
) {
300 sc
->ndis_irq
= bus_alloc_resource_any(dev
, SYS_RES_IRQ
,
301 &rid
, RF_SHAREABLE
| RF_ACTIVE
);
302 if (sc
->ndis_irq
== NULL
) {
303 device_printf(dev
, "couldn't route interrupt\n");
311 * Allocate the parent bus DMA tag appropriate for PCI.
313 #define NDIS_NSEG_NEW 32
314 error
= bus_dma_tag_create(NULL
, /* parent */
315 1, 0, /* alignment, boundary */
316 BUS_SPACE_MAXADDR_32BIT
,/* lowaddr */
317 BUS_SPACE_MAXADDR
, /* highaddr */
318 NULL
, NULL
, /* filter, filterarg */
319 DFLTPHYS
, NDIS_NSEG_NEW
,/* maxsize, nsegments */
320 BUS_SPACE_MAXSIZE_32BIT
,/* maxsegsize */
321 BUS_DMA_ALLOCNOW
, /* flags */
322 &sc
->ndis_parent_tag
);
327 sc
->ndis_iftype
= PCIBus
;
329 /* Figure out exactly which device we matched. */
331 vid
= pci_get_vendor(dev
);
332 did
= pci_get_device(dev
);
333 subsys
= pci_get_subdevice(dev
);
334 subsys
= (subsys
<< 16) | pci_get_subvendor(dev
);
336 t
= db
->windrv_devlist
;
338 while(t
->ndis_name
!= NULL
) {
339 if (t
->ndis_vid
== vid
&& t
->ndis_did
== did
) {
340 if (t
->ndis_subsys
== 0)
342 else if (t
->ndis_subsys
== subsys
)
349 if (t
->ndis_name
== NULL
)
350 sc
->ndis_devidx
= defidx
;
352 sc
->ndis_devidx
= devidx
;
354 error
= ndis_attach(dev
);
360 static struct resource_list
*
361 ndis_get_resource_list(device_t dev
, device_t child
)
363 struct ndis_softc
*sc
;
365 sc
= device_get_softc(dev
);
366 return (BUS_GET_RESOURCE_LIST(device_get_parent(sc
->ndis_dev
), dev
));