if_ndis_pci.c revision 146016
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 146016 2005-05-08 23:19:20Z 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);
71146016Swpaulstatic int ndis_devcompare	(interface_type,
72146016Swpaul				 struct ndis_pci_type *, device_t);
73141524Swpaulextern int ndisdrv_modevent	(module_t, int, void *);
74126706Swpaulextern int ndis_attach		(device_t);
75126706Swpaulextern int ndis_shutdown	(device_t);
76126706Swpaulextern int ndis_detach		(device_t);
77126706Swpaulextern int ndis_suspend		(device_t);
78126706Swpaulextern int ndis_resume		(device_t);
79126706Swpaul
80126706Swpaulstatic device_method_t ndis_methods[] = {
81126706Swpaul	/* Device interface */
82126706Swpaul	DEVMETHOD(device_probe,		ndis_probe_pci),
83126706Swpaul	DEVMETHOD(device_attach,	ndis_attach_pci),
84126706Swpaul	DEVMETHOD(device_detach,	ndis_detach),
85126706Swpaul	DEVMETHOD(device_shutdown,	ndis_shutdown),
86126706Swpaul	DEVMETHOD(device_suspend,	ndis_suspend),
87126706Swpaul	DEVMETHOD(device_resume,	ndis_resume),
88126706Swpaul
89131953Swpaul	/* Bus interface */
90131953Swpaul	DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
91131953Swpaul
92126706Swpaul	{ 0, 0 }
93126706Swpaul};
94126706Swpaul
95126706Swpaulstatic driver_t ndis_driver = {
96126706Swpaul	"ndis",
97126706Swpaul	ndis_methods,
98126706Swpaul	sizeof(struct ndis_softc)
99126706Swpaul};
100126706Swpaul
101126706Swpaulstatic devclass_t ndis_devclass;
102126706Swpaul
103141524SwpaulDRIVER_MODULE(ndis, pci, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
104141524SwpaulDRIVER_MODULE(ndis, cardbus, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
105126706Swpaul
106145485Swpaulstatic int
107146016Swpaulndis_devcompare(bustype, t, dev)
108146016Swpaul	interface_type		bustype;
109145485Swpaul	struct ndis_pci_type	*t;
110145485Swpaul	device_t		dev;
111145485Swpaul{
112146016Swpaul	if (bustype != PCIBus)
113146016Swpaul		return(FALSE);
114146016Swpaul
115145485Swpaul	while(t->ndis_name != NULL) {
116145485Swpaul		if ((pci_get_vendor(dev) == t->ndis_vid) &&
117145485Swpaul		    (pci_get_device(dev) == t->ndis_did) &&
118145485Swpaul		    ((pci_read_config(dev, PCIR_SUBVEND_0, 4) ==
119145485Swpaul		    t->ndis_subsys) || t->ndis_subsys == 0)) {
120145485Swpaul			device_set_desc(dev, t->ndis_name);
121145485Swpaul			return(TRUE);
122145485Swpaul		}
123145485Swpaul		t++;
124145485Swpaul	}
125145485Swpaul
126145485Swpaul	return(FALSE);
127145485Swpaul}
128145485Swpaul
129126706Swpaul/*
130126706Swpaul * Probe for an NDIS device. Check the PCI vendor and device
131126706Swpaul * IDs against our list and return a device name if we find a match.
132126706Swpaul */
133126706Swpaulstatic int
134126706Swpaulndis_probe_pci(dev)
135126706Swpaul	device_t		dev;
136126706Swpaul{
137141524Swpaul	driver_object		*drv;
138145485Swpaul	struct drvdb_ent	*db;
139126706Swpaul
140142804Swpaul	drv = windrv_lookup(0, "PCI Bus");
141126706Swpaul
142141524Swpaul	if (drv == NULL)
143141524Swpaul		return(ENXIO);
144141524Swpaul
145145485Swpaul	db = windrv_match((matchfuncptr)ndis_devcompare, dev);
146141524Swpaul
147145485Swpaul	if (db != NULL) {
148145485Swpaul		/* Create PDO for this device instance */
149145485Swpaul		windrv_create_pdo(drv, dev);
150145485Swpaul		return(0);
151126706Swpaul	}
152126706Swpaul
153126706Swpaul	return(ENXIO);
154126706Swpaul}
155126706Swpaul
156126706Swpaul/*
157126706Swpaul * Attach the interface. Allocate softc structures, do ifmedia
158126706Swpaul * setup and ethernet/BPF attach.
159126706Swpaul */
160126706Swpaulstatic int
161126706Swpaulndis_attach_pci(dev)
162126706Swpaul	device_t		dev;
163126706Swpaul{
164126706Swpaul	struct ndis_softc	*sc;
165126706Swpaul	int			unit, error = 0, rid;
166126706Swpaul	struct ndis_pci_type	*t;
167126706Swpaul	int			devidx = 0, defidx = 0;
168126706Swpaul	struct resource_list	*rl;
169126706Swpaul	struct resource_list_entry	*rle;
170145485Swpaul	struct drvdb_ent	*db;
171126706Swpaul
172126706Swpaul	sc = device_get_softc(dev);
173126706Swpaul	unit = device_get_unit(dev);
174126706Swpaul	sc->ndis_dev = dev;
175126706Swpaul
176145485Swpaul	db = windrv_match((matchfuncptr)ndis_devcompare, dev);
177145485Swpaul	if (db == NULL)
178145485Swpaul		return (ENXIO);
179145485Swpaul	sc->ndis_dobj = db->windrv_object;
180145485Swpaul	sc->ndis_regvals = db->windrv_regvals;
181145485Swpaul
182126706Swpaul	/*
183126706Swpaul	 * Map control/status registers.
184126706Swpaul	 */
185126706Swpaul
186126706Swpaul	pci_enable_busmaster(dev);
187126706Swpaul
188126706Swpaul	rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
189126706Swpaul	if (rl != NULL) {
190144176Swpaul#if __FreeBSD_version < 600022
191144176Swpaul		SLIST_FOREACH(rle, rl, link) {
192144176Swpaul#else
193143848Smaxim		STAILQ_FOREACH(rle, rl, link) {
194144176Swpaul#endif
195126706Swpaul			switch (rle->type) {
196126706Swpaul			case SYS_RES_IOPORT:
197126706Swpaul				sc->ndis_io_rid = rle->rid;
198127281Swpaul				sc->ndis_res_io = bus_alloc_resource(dev,
199126706Swpaul				    SYS_RES_IOPORT, &sc->ndis_io_rid,
200127281Swpaul				    0, ~0, 1, RF_ACTIVE);
201126706Swpaul				if (sc->ndis_res_io == NULL) {
202126706Swpaul					device_printf(dev,
203126706Swpaul					    "couldn't map iospace\n");
204126706Swpaul					error = ENXIO;
205126706Swpaul					goto fail;
206126706Swpaul				}
207145485Swpaul				pci_enable_io(dev, SYS_RES_IOPORT);
208126706Swpaul				break;
209126706Swpaul			case SYS_RES_MEMORY:
210126706Swpaul				if (sc->ndis_res_altmem != NULL &&
211126706Swpaul				    sc->ndis_res_mem != NULL) {
212126706Swpaul					device_printf(dev,
213126706Swpaul					    "too many memory resources\n");
214126706Swpaul					error = ENXIO;
215126706Swpaul					goto fail;
216126706Swpaul				}
217133876Swpaul				if (sc->ndis_res_mem) {
218126706Swpaul					sc->ndis_altmem_rid = rle->rid;
219126706Swpaul					sc->ndis_res_altmem =
220127281Swpaul					    bus_alloc_resource(dev,
221126706Swpaul					        SYS_RES_MEMORY,
222126706Swpaul						&sc->ndis_altmem_rid,
223127281Swpaul						0, ~0, 1, RF_ACTIVE);
224126706Swpaul					if (sc->ndis_res_altmem == NULL) {
225126706Swpaul						device_printf(dev,
226126706Swpaul						    "couldn't map alt "
227126706Swpaul						    "memory\n");
228126706Swpaul						error = ENXIO;
229126706Swpaul						goto fail;
230126706Swpaul					}
231126706Swpaul				} else {
232126706Swpaul					sc->ndis_mem_rid = rle->rid;
233126706Swpaul					sc->ndis_res_mem =
234127281Swpaul					    bus_alloc_resource(dev,
235126706Swpaul					        SYS_RES_MEMORY,
236126706Swpaul						&sc->ndis_mem_rid,
237127281Swpaul						0, ~0, 1, RF_ACTIVE);
238126706Swpaul					if (sc->ndis_res_mem == NULL) {
239126706Swpaul						device_printf(dev,
240126706Swpaul						    "couldn't map memory\n");
241126706Swpaul						error = ENXIO;
242126706Swpaul						goto fail;
243126706Swpaul					}
244126706Swpaul				}
245145485Swpaul				pci_enable_io(dev, SYS_RES_MEMORY);
246126706Swpaul				break;
247126706Swpaul			case SYS_RES_IRQ:
248126706Swpaul				rid = rle->rid;
249127281Swpaul				sc->ndis_irq = bus_alloc_resource(dev,
250127281Swpaul				    SYS_RES_IRQ, &rid, 0, ~0, 1,
251126706Swpaul	    			    RF_SHAREABLE | RF_ACTIVE);
252126706Swpaul				if (sc->ndis_irq == NULL) {
253126706Swpaul					device_printf(dev,
254126706Swpaul					    "couldn't map interrupt\n");
255126706Swpaul					error = ENXIO;
256126706Swpaul					goto fail;
257126706Swpaul				}
258126706Swpaul				break;
259126706Swpaul			default:
260126706Swpaul				break;
261126706Swpaul			}
262126706Swpaul			sc->ndis_rescnt++;
263126706Swpaul		}
264126706Swpaul	}
265126706Swpaul
266126706Swpaul	/*
267126780Swpaul	 * If the BIOS did not set up an interrupt for this device,
268126780Swpaul	 * the resource traversal code above will fail to set up
269126780Swpaul	 * an IRQ resource. This is usually a bad thing, so try to
270126780Swpaul	 * force the allocation of an interrupt here. If one was
271126780Swpaul	 * not assigned to us by the BIOS, bus_alloc_resource()
272126780Swpaul	 * should route one for us.
273126780Swpaul	 */
274126780Swpaul	if (sc->ndis_irq == NULL) {
275126780Swpaul		rid = 0;
276126780Swpaul		sc->ndis_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
277126780Swpaul		    &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
278126780Swpaul		if (sc->ndis_irq == NULL) {
279126780Swpaul			device_printf(dev, "couldn't route interrupt\n");
280126780Swpaul			error = ENXIO;
281126780Swpaul			goto fail;
282126780Swpaul		}
283126780Swpaul		sc->ndis_rescnt++;
284126780Swpaul	}
285126780Swpaul
286126780Swpaul	/*
287126706Swpaul	 * Allocate the parent bus DMA tag appropriate for PCI.
288126706Swpaul	 */
289126706Swpaul#define NDIS_NSEG_NEW 32
290126706Swpaul	error = bus_dma_tag_create(NULL,	/* parent */
291126706Swpaul			1, 0,			/* alignment, boundary */
292126706Swpaul			BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
293126706Swpaul                        BUS_SPACE_MAXADDR,	/* highaddr */
294126706Swpaul			NULL, NULL,		/* filter, filterarg */
295126706Swpaul			MAXBSIZE, NDIS_NSEG_NEW,/* maxsize, nsegments */
296126706Swpaul			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
297126706Swpaul			BUS_DMA_ALLOCNOW,       /* flags */
298126706Swpaul			NULL, NULL,		/* lockfunc, lockarg */
299126706Swpaul			&sc->ndis_parent_tag);
300126706Swpaul
301126706Swpaul        if (error)
302126706Swpaul                goto fail;
303126706Swpaul
304126706Swpaul	sc->ndis_iftype = PCIBus;
305126706Swpaul
306126706Swpaul	/* Figure out exactly which device we matched. */
307126706Swpaul
308145485Swpaul	t = db->windrv_devlist;
309126706Swpaul
310126706Swpaul	while(t->ndis_name != NULL) {
311126706Swpaul		if ((pci_get_vendor(dev) == t->ndis_vid) &&
312126706Swpaul		    (pci_get_device(dev) == t->ndis_did)) {
313126706Swpaul			if (t->ndis_subsys == 0)
314126706Swpaul				defidx = devidx;
315126706Swpaul			else {
316126706Swpaul				if (t->ndis_subsys ==
317126706Swpaul				    pci_read_config(dev, PCIR_SUBVEND_0, 4))
318126706Swpaul					break;
319126706Swpaul			}
320126706Swpaul		}
321126706Swpaul		t++;
322126706Swpaul		devidx++;
323126706Swpaul	}
324126706Swpaul
325145995Swpaul	if (t->ndis_name == NULL)
326126706Swpaul		sc->ndis_devidx = defidx;
327126706Swpaul	else
328126706Swpaul		sc->ndis_devidx = devidx;
329126706Swpaul
330126706Swpaul	error = ndis_attach(dev);
331126706Swpaul
332126706Swpaulfail:
333126706Swpaul	return(error);
334126706Swpaul}
335126706Swpaul
336131953Swpaulstatic struct resource_list *
337131953Swpaulndis_get_resource_list(dev, child)
338131953Swpaul	device_t		dev;
339131953Swpaul	device_t		child;
340131953Swpaul{
341131953Swpaul	struct ndis_softc	*sc;
342131953Swpaul
343131953Swpaul	sc = device_get_softc(dev);
344131953Swpaul	return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev));
345131953Swpaul}
346