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$");
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{
113216558Stijl	uint16_t		vid, did;
114216558Stijl	uint32_t		subsys;
115216558Stijl
116146016Swpaul	if (bustype != PCIBus)
117146016Swpaul		return(FALSE);
118146016Swpaul
119216558Stijl	vid = pci_get_vendor(dev);
120216558Stijl	did = pci_get_device(dev);
121216558Stijl	subsys = pci_get_subdevice(dev);
122216558Stijl	subsys = (subsys << 16) | pci_get_subvendor(dev);
123216558Stijl
124145485Swpaul	while(t->ndis_name != NULL) {
125216558Stijl		if ((t->ndis_vid == vid) && (t->ndis_did == did) &&
126216558Stijl		    (t->ndis_subsys == subsys || t->ndis_subsys == 0)) {
127145485Swpaul			device_set_desc(dev, t->ndis_name);
128145485Swpaul			return(TRUE);
129145485Swpaul		}
130145485Swpaul		t++;
131145485Swpaul	}
132145485Swpaul
133145485Swpaul	return(FALSE);
134145485Swpaul}
135145485Swpaul
136126706Swpaul/*
137126706Swpaul * Probe for an NDIS device. Check the PCI vendor and device
138126706Swpaul * IDs against our list and return a device name if we find a match.
139126706Swpaul */
140126706Swpaulstatic int
141126706Swpaulndis_probe_pci(dev)
142126706Swpaul	device_t		dev;
143126706Swpaul{
144141524Swpaul	driver_object		*drv;
145145485Swpaul	struct drvdb_ent	*db;
146126706Swpaul
147142804Swpaul	drv = windrv_lookup(0, "PCI Bus");
148126706Swpaul
149141524Swpaul	if (drv == NULL)
150141524Swpaul		return(ENXIO);
151141524Swpaul
152145485Swpaul	db = windrv_match((matchfuncptr)ndis_devcompare, dev);
153141524Swpaul
154145485Swpaul	if (db != NULL) {
155145485Swpaul		/* Create PDO for this device instance */
156145485Swpaul		windrv_create_pdo(drv, dev);
157145485Swpaul		return(0);
158126706Swpaul	}
159126706Swpaul
160126706Swpaul	return(ENXIO);
161126706Swpaul}
162126706Swpaul
163126706Swpaul/*
164126706Swpaul * Attach the interface. Allocate softc structures, do ifmedia
165126706Swpaul * setup and ethernet/BPF attach.
166126706Swpaul */
167126706Swpaulstatic int
168126706Swpaulndis_attach_pci(dev)
169126706Swpaul	device_t		dev;
170126706Swpaul{
171126706Swpaul	struct ndis_softc	*sc;
172126706Swpaul	int			unit, error = 0, rid;
173126706Swpaul	struct ndis_pci_type	*t;
174126706Swpaul	int			devidx = 0, defidx = 0;
175126706Swpaul	struct resource_list	*rl;
176126706Swpaul	struct resource_list_entry	*rle;
177145485Swpaul	struct drvdb_ent	*db;
178216558Stijl	uint16_t		vid, did;
179216558Stijl	uint32_t		subsys;
180126706Swpaul
181126706Swpaul	sc = device_get_softc(dev);
182126706Swpaul	unit = device_get_unit(dev);
183126706Swpaul	sc->ndis_dev = dev;
184126706Swpaul
185145485Swpaul	db = windrv_match((matchfuncptr)ndis_devcompare, dev);
186145485Swpaul	if (db == NULL)
187145485Swpaul		return (ENXIO);
188145485Swpaul	sc->ndis_dobj = db->windrv_object;
189145485Swpaul	sc->ndis_regvals = db->windrv_regvals;
190145485Swpaul
191126706Swpaul	/*
192126706Swpaul	 * Map control/status registers.
193126706Swpaul	 */
194126706Swpaul
195126706Swpaul	pci_enable_busmaster(dev);
196126706Swpaul
197126706Swpaul	rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
198126706Swpaul	if (rl != NULL) {
199143848Smaxim		STAILQ_FOREACH(rle, rl, link) {
200126706Swpaul			switch (rle->type) {
201126706Swpaul			case SYS_RES_IOPORT:
202126706Swpaul				sc->ndis_io_rid = rle->rid;
203216486Sjhb				sc->ndis_res_io = bus_alloc_resource_any(dev,
204126706Swpaul				    SYS_RES_IOPORT, &sc->ndis_io_rid,
205216486Sjhb				    RF_ACTIVE);
206126706Swpaul				if (sc->ndis_res_io == NULL) {
207126706Swpaul					device_printf(dev,
208126706Swpaul					    "couldn't map iospace\n");
209126706Swpaul					error = ENXIO;
210126706Swpaul					goto fail;
211126706Swpaul				}
212126706Swpaul				break;
213126706Swpaul			case SYS_RES_MEMORY:
214126706Swpaul				if (sc->ndis_res_altmem != NULL &&
215126706Swpaul				    sc->ndis_res_mem != NULL) {
216126706Swpaul					device_printf(dev,
217126706Swpaul					    "too many memory resources\n");
218126706Swpaul					error = ENXIO;
219126706Swpaul					goto fail;
220126706Swpaul				}
221133876Swpaul				if (sc->ndis_res_mem) {
222126706Swpaul					sc->ndis_altmem_rid = rle->rid;
223126706Swpaul					sc->ndis_res_altmem =
224216486Sjhb					    bus_alloc_resource_any(dev,
225126706Swpaul					        SYS_RES_MEMORY,
226126706Swpaul						&sc->ndis_altmem_rid,
227216486Sjhb						RF_ACTIVE);
228126706Swpaul					if (sc->ndis_res_altmem == NULL) {
229126706Swpaul						device_printf(dev,
230126706Swpaul						    "couldn't map alt "
231126706Swpaul						    "memory\n");
232126706Swpaul						error = ENXIO;
233126706Swpaul						goto fail;
234126706Swpaul					}
235126706Swpaul				} else {
236126706Swpaul					sc->ndis_mem_rid = rle->rid;
237126706Swpaul					sc->ndis_res_mem =
238216486Sjhb					    bus_alloc_resource_any(dev,
239126706Swpaul					        SYS_RES_MEMORY,
240126706Swpaul						&sc->ndis_mem_rid,
241216486Sjhb						RF_ACTIVE);
242126706Swpaul					if (sc->ndis_res_mem == NULL) {
243126706Swpaul						device_printf(dev,
244126706Swpaul						    "couldn't map memory\n");
245126706Swpaul						error = ENXIO;
246126706Swpaul						goto fail;
247126706Swpaul					}
248126706Swpaul				}
249126706Swpaul				break;
250126706Swpaul			case SYS_RES_IRQ:
251126706Swpaul				rid = rle->rid;
252216486Sjhb				sc->ndis_irq = bus_alloc_resource_any(dev,
253216486Sjhb				    SYS_RES_IRQ, &rid,
254216486Sjhb				    RF_SHAREABLE | RF_ACTIVE);
255126706Swpaul				if (sc->ndis_irq == NULL) {
256126706Swpaul					device_printf(dev,
257126706Swpaul					    "couldn't map interrupt\n");
258126706Swpaul					error = ENXIO;
259126706Swpaul					goto fail;
260126706Swpaul				}
261126706Swpaul				break;
262126706Swpaul			default:
263126706Swpaul				break;
264126706Swpaul			}
265126706Swpaul			sc->ndis_rescnt++;
266126706Swpaul		}
267126706Swpaul	}
268126706Swpaul
269126706Swpaul	/*
270126780Swpaul	 * If the BIOS did not set up an interrupt for this device,
271126780Swpaul	 * the resource traversal code above will fail to set up
272126780Swpaul	 * an IRQ resource. This is usually a bad thing, so try to
273126780Swpaul	 * force the allocation of an interrupt here. If one was
274126780Swpaul	 * not assigned to us by the BIOS, bus_alloc_resource()
275126780Swpaul	 * should route one for us.
276126780Swpaul	 */
277126780Swpaul	if (sc->ndis_irq == NULL) {
278126780Swpaul		rid = 0;
279216486Sjhb		sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
280216486Sjhb		    &rid, RF_SHAREABLE | RF_ACTIVE);
281126780Swpaul		if (sc->ndis_irq == NULL) {
282126780Swpaul			device_printf(dev, "couldn't route interrupt\n");
283126780Swpaul			error = ENXIO;
284126780Swpaul			goto fail;
285126780Swpaul		}
286126780Swpaul		sc->ndis_rescnt++;
287126780Swpaul	}
288126780Swpaul
289126780Swpaul	/*
290126706Swpaul	 * Allocate the parent bus DMA tag appropriate for PCI.
291126706Swpaul	 */
292126706Swpaul#define NDIS_NSEG_NEW 32
293233024Sscottl	error = bus_dma_tag_create(bus_get_dma_tag(dev),/* PCI parent */
294126706Swpaul			1, 0,			/* alignment, boundary */
295126706Swpaul			BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
296126706Swpaul                        BUS_SPACE_MAXADDR,	/* highaddr */
297126706Swpaul			NULL, NULL,		/* filter, filterarg */
298126706Swpaul			MAXBSIZE, NDIS_NSEG_NEW,/* maxsize, nsegments */
299126706Swpaul			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
300126706Swpaul			BUS_DMA_ALLOCNOW,       /* flags */
301126706Swpaul			NULL, NULL,		/* lockfunc, lockarg */
302126706Swpaul			&sc->ndis_parent_tag);
303126706Swpaul
304126706Swpaul        if (error)
305126706Swpaul                goto fail;
306126706Swpaul
307126706Swpaul	sc->ndis_iftype = PCIBus;
308126706Swpaul
309126706Swpaul	/* Figure out exactly which device we matched. */
310126706Swpaul
311216558Stijl	vid = pci_get_vendor(dev);
312216558Stijl	did = pci_get_device(dev);
313216558Stijl	subsys = pci_get_subdevice(dev);
314216558Stijl	subsys = (subsys << 16) | pci_get_subvendor(dev);
315216558Stijl
316145485Swpaul	t = db->windrv_devlist;
317126706Swpaul
318126706Swpaul	while(t->ndis_name != NULL) {
319216558Stijl		if (t->ndis_vid == vid && t->ndis_did == did) {
320126706Swpaul			if (t->ndis_subsys == 0)
321126706Swpaul				defidx = devidx;
322216558Stijl			else if (t->ndis_subsys == subsys)
323216518Stijl				break;
324126706Swpaul		}
325126706Swpaul		t++;
326126706Swpaul		devidx++;
327126706Swpaul	}
328126706Swpaul
329145995Swpaul	if (t->ndis_name == NULL)
330126706Swpaul		sc->ndis_devidx = defidx;
331126706Swpaul	else
332126706Swpaul		sc->ndis_devidx = devidx;
333126706Swpaul
334126706Swpaul	error = ndis_attach(dev);
335126706Swpaul
336126706Swpaulfail:
337126706Swpaul	return(error);
338126706Swpaul}
339126706Swpaul
340131953Swpaulstatic struct resource_list *
341131953Swpaulndis_get_resource_list(dev, child)
342131953Swpaul	device_t		dev;
343131953Swpaul	device_t		child;
344131953Swpaul{
345131953Swpaul	struct ndis_softc	*sc;
346131953Swpaul
347131953Swpaul	sc = device_get_softc(dev);
348131953Swpaul	return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev));
349131953Swpaul}
350