if_ndis_pci.c revision 216518
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 216518 2010-12-18 14:21:28Z tijl $");
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) &&
119216518Stijl		    (pci_get_subvendor(dev) == t->ndis_subsys ||
120216518Stijl		     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;
195216486Sjhb				sc->ndis_res_io = bus_alloc_resource_any(dev,
196126706Swpaul				    SYS_RES_IOPORT, &sc->ndis_io_rid,
197216486Sjhb				    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				}
204126706Swpaul				break;
205126706Swpaul			case SYS_RES_MEMORY:
206126706Swpaul				if (sc->ndis_res_altmem != NULL &&
207126706Swpaul				    sc->ndis_res_mem != NULL) {
208126706Swpaul					device_printf(dev,
209126706Swpaul					    "too many memory resources\n");
210126706Swpaul					error = ENXIO;
211126706Swpaul					goto fail;
212126706Swpaul				}
213133876Swpaul				if (sc->ndis_res_mem) {
214126706Swpaul					sc->ndis_altmem_rid = rle->rid;
215126706Swpaul					sc->ndis_res_altmem =
216216486Sjhb					    bus_alloc_resource_any(dev,
217126706Swpaul					        SYS_RES_MEMORY,
218126706Swpaul						&sc->ndis_altmem_rid,
219216486Sjhb						RF_ACTIVE);
220126706Swpaul					if (sc->ndis_res_altmem == NULL) {
221126706Swpaul						device_printf(dev,
222126706Swpaul						    "couldn't map alt "
223126706Swpaul						    "memory\n");
224126706Swpaul						error = ENXIO;
225126706Swpaul						goto fail;
226126706Swpaul					}
227126706Swpaul				} else {
228126706Swpaul					sc->ndis_mem_rid = rle->rid;
229126706Swpaul					sc->ndis_res_mem =
230216486Sjhb					    bus_alloc_resource_any(dev,
231126706Swpaul					        SYS_RES_MEMORY,
232126706Swpaul						&sc->ndis_mem_rid,
233216486Sjhb						RF_ACTIVE);
234126706Swpaul					if (sc->ndis_res_mem == NULL) {
235126706Swpaul						device_printf(dev,
236126706Swpaul						    "couldn't map memory\n");
237126706Swpaul						error = ENXIO;
238126706Swpaul						goto fail;
239126706Swpaul					}
240126706Swpaul				}
241126706Swpaul				break;
242126706Swpaul			case SYS_RES_IRQ:
243126706Swpaul				rid = rle->rid;
244216486Sjhb				sc->ndis_irq = bus_alloc_resource_any(dev,
245216486Sjhb				    SYS_RES_IRQ, &rid,
246216486Sjhb				    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;
271216486Sjhb		sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
272216486Sjhb		    &rid, 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;
310216518Stijl			else if (pci_get_subvendor(dev) == t->ndis_subsys)
311216518Stijl				break;
312126706Swpaul		}
313126706Swpaul		t++;
314126706Swpaul		devidx++;
315126706Swpaul	}
316126706Swpaul
317145995Swpaul	if (t->ndis_name == NULL)
318126706Swpaul		sc->ndis_devidx = defidx;
319126706Swpaul	else
320126706Swpaul		sc->ndis_devidx = devidx;
321126706Swpaul
322126706Swpaul	error = ndis_attach(dev);
323126706Swpaul
324126706Swpaulfail:
325126706Swpaul	return(error);
326126706Swpaul}
327126706Swpaul
328131953Swpaulstatic struct resource_list *
329131953Swpaulndis_get_resource_list(dev, child)
330131953Swpaul	device_t		dev;
331131953Swpaul	device_t		child;
332131953Swpaul{
333131953Swpaul	struct ndis_softc	*sc;
334131953Swpaul
335131953Swpaul	sc = device_get_softc(dev);
336131953Swpaul	return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev));
337131953Swpaul}
338