9a3767a829428b1093e86d6029b23a118306cea0
[dragonfly.git] / sys / dev / netif / ndis / if_ndis_pci.c
1 /*
2  * Copyright (c) 2003
3  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
19  *
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.
31  *
32  * $FreeBSD: src/sys/dev/if_ndis/if_ndis_pci.c,v 1.7 2004/07/11 00:19:30 wpaul Exp $
33  * $DragonFly: src/sys/dev/netif/ndis/if_ndis_pci.c,v 1.5 2006/10/25 20:55:58 dillon Exp $
34  */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/module.h>
41 #include <sys/socket.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44 #include <sys/bus.h>
45 #include <sys/rman.h>
46
47 #include <net/if.h>
48 #include <net/if_arp.h>
49 #include <net/if_media.h>
50
51 #include <netproto/802_11/ieee80211_var.h>
52
53 #include <bus/pci/pcireg.h>
54 #include <bus/pci/pcivar.h>
55
56 #include <emulation/ndis/regcall.h>
57 #include <emulation/ndis/pe_var.h>
58 #include <emulation/ndis/resource_var.h>
59 #include <emulation/ndis/ntoskrnl_var.h>
60 #include <emulation/ndis/ndis_var.h>
61 #include <emulation/ndis/cfg_var.h>
62 #include "if_ndisvar.h"
63
64 #include "ndis_driver_data.h"
65
66 #ifdef NDIS_PCI_DEV_TABLE 
67
68 MODULE_DEPEND(ndis, pci, 1, 1, 1);
69 MODULE_DEPEND(ndis, wlan, 1, 1, 1);
70 MODULE_DEPEND(ndis, ndisapi, 1, 1, 1);
71
72 /*
73  * Various supported device vendors/types and their names.
74  * These are defined in the ndis_driver_data.h file.
75  */
76 static struct ndis_pci_type ndis_devs[] = {
77 #ifdef NDIS_PCI_DEV_TABLE
78         NDIS_PCI_DEV_TABLE
79 #endif
80         { 0, 0, 0, NULL }
81 };
82
83 static int ndis_probe_pci       (device_t);
84 static int ndis_attach_pci      (device_t);
85 static struct resource_list *ndis_get_resource_list
86                                 (device_t, device_t);
87 extern int ndis_attach          (device_t);
88 extern int ndis_shutdown        (device_t);
89 extern int ndis_detach          (device_t);
90 extern int ndis_suspend         (device_t);
91 extern int ndis_resume          (device_t);
92
93 static device_method_t ndis_methods[] = {
94         /* Device interface */
95         DEVMETHOD(device_probe,         ndis_probe_pci),
96         DEVMETHOD(device_attach,        ndis_attach_pci),
97         DEVMETHOD(device_detach,        ndis_detach),
98         DEVMETHOD(device_shutdown,      ndis_shutdown),
99         DEVMETHOD(device_suspend,       ndis_suspend),
100         DEVMETHOD(device_resume,        ndis_resume),
101
102         /* Bus interface */
103         DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
104
105         { 0, 0 }
106 };
107
108 static driver_t ndis_driver = {
109 #ifdef NDIS_DEVNAME
110         NDIS_DEVNAME,
111 #else
112         "ndis",
113 #endif
114         ndis_methods,
115         sizeof(struct ndis_softc)
116 };
117
118 static devclass_t ndis_devclass;
119
120 #ifdef NDIS_MODNAME
121 #define NDIS_MODNAME_OVERRIDE_PCI(x)                                    \
122         DRIVER_MODULE(x, pci, ndis_driver, ndis_devclass, 0, 0)
123 #define NDIS_MODNAME_OVERRIDE_CARDBUS(x)                                \
124         DRIVER_MODULE(x, cardbus, ndis_driver, ndis_devclass, 0, 0)
125 NDIS_MODNAME_OVERRIDE_PCI(NDIS_MODNAME);
126 NDIS_MODNAME_OVERRIDE_CARDBUS(NDIS_MODNAME);
127 #else
128 DRIVER_MODULE(ndis, pci, ndis_driver, ndis_devclass, 0, 0);
129 DRIVER_MODULE(ndis, cardbus, ndis_driver, ndis_devclass, 0, 0);
130 #endif
131
132 /*
133  * Probe for an NDIS device. Check the PCI vendor and device
134  * IDs against our list and return a device name if we find a match.
135  */
136 static int
137 ndis_probe_pci(device_t dev)
138 {
139         struct ndis_pci_type    *t;
140
141         t = ndis_devs;
142
143         while(t->ndis_name != NULL) {
144                 if ((pci_get_vendor(dev) == t->ndis_vid) &&
145                     (pci_get_device(dev) == t->ndis_did) &&
146                     ((pci_read_config(dev, PCIR_SUBVEND_0, 4) ==
147                     t->ndis_subsys) || t->ndis_subsys == 0)) {
148                         device_set_desc(dev, t->ndis_name);
149                         return(0);
150                 }
151                 t++;
152         }
153
154         return(ENXIO);
155 }
156
157 /*
158  * Attach the interface. Allocate softc structures, do ifmedia
159  * setup and ethernet/BPF attach.
160  */
161 static int
162 ndis_attach_pci(device_t dev)
163 {
164         struct ndis_softc       *sc;
165         int                     unit, error = 0, rid;
166         struct ndis_pci_type    *t;
167         int                     devidx = 0, defidx = 0;
168         struct resource_list    *rl;
169         struct resource_list_entry      *rle;
170
171         sc = device_get_softc(dev);
172         unit = device_get_unit(dev);
173         sc->ndis_dev = dev;
174
175         /*
176          * Map control/status registers.
177          */
178
179         pci_enable_busmaster(dev);
180
181         rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
182         if (rl != NULL) {
183                 SLIST_FOREACH(rle, rl, link) {
184                         switch (rle->type) {
185                         case SYS_RES_IOPORT:
186                                 sc->ndis_io_rid = rle->rid;
187                                 sc->ndis_res_io = bus_alloc_resource_any(dev,
188                                     SYS_RES_IOPORT, &sc->ndis_io_rid,
189                                     RF_ACTIVE);
190                                 if (sc->ndis_res_io == NULL) {
191                                         device_printf(dev,
192                                             "couldn't map iospace\n");
193                                         error = ENXIO;
194                                         goto fail;
195                                 }
196                                 break;
197                         case SYS_RES_MEMORY:
198                                 if (sc->ndis_res_altmem != NULL &&
199                                     sc->ndis_res_mem != NULL) {
200                                         device_printf(dev,
201                                             "too many memory resources\n");
202                                         error = ENXIO;
203                                         goto fail;
204                                 }
205                                 if (rle->rid == PCIR_BAR(2)) {
206                                         sc->ndis_altmem_rid = rle->rid;
207                                         sc->ndis_res_altmem =
208                                             bus_alloc_resource_any(dev,
209                                                 SYS_RES_MEMORY,
210                                                 &sc->ndis_altmem_rid,
211                                                 RF_ACTIVE);
212                                         if (sc->ndis_res_altmem == NULL) {
213                                                 device_printf(dev,
214                                                     "couldn't map alt "
215                                                     "memory\n");
216                                                 error = ENXIO;
217                                                 goto fail;
218                                         }
219                                 } else {
220                                         sc->ndis_mem_rid = rle->rid;
221                                         sc->ndis_res_mem =
222                                             bus_alloc_resource_any(dev,
223                                                 SYS_RES_MEMORY,
224                                                 &sc->ndis_mem_rid,
225                                                 RF_ACTIVE);
226                                         if (sc->ndis_res_mem == NULL) {
227                                                 device_printf(dev,
228                                                     "couldn't map memory\n");
229                                                 error = ENXIO;
230                                                 goto fail;
231                                         }
232                                 }
233                                 break;
234                         case SYS_RES_IRQ:
235                                 rid = rle->rid;
236                                 sc->ndis_irq = bus_alloc_resource_any(dev,
237                                     SYS_RES_IRQ, &rid,
238                                     RF_SHAREABLE | RF_ACTIVE);
239                                 if (sc->ndis_irq == NULL) {
240                                         device_printf(dev,
241                                             "couldn't map interrupt\n");
242                                         error = ENXIO;
243                                         goto fail;
244                                 }
245                                 break;
246                         default:
247                                 break;
248                         }
249                         sc->ndis_rescnt++;
250                 }
251         }
252
253         /*
254          * If the BIOS did not set up an interrupt for this device,
255          * the resource traversal code above will fail to set up
256          * an IRQ resource. This is usually a bad thing, so try to
257          * force the allocation of an interrupt here. If one was
258          * not assigned to us by the BIOS, bus_alloc_resource()
259          * should route one for us.
260          */
261         if (sc->ndis_irq == NULL) {
262                 rid = 0;
263                 sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
264                     &rid, RF_SHAREABLE | RF_ACTIVE);
265                 if (sc->ndis_irq == NULL) {
266                         device_printf(dev, "couldn't route interrupt\n");
267                         error = ENXIO;
268                         goto fail;
269                 }
270                 sc->ndis_rescnt++;
271         }
272
273         /*
274          * Allocate the parent bus DMA tag appropriate for PCI.
275          */
276 #define NDIS_NSEG_NEW 32
277         error = bus_dma_tag_create(NULL,        /* parent */
278                         1, 0,                   /* alignment, boundary */
279                         BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
280                         BUS_SPACE_MAXADDR,      /* highaddr */
281                         NULL, NULL,             /* filter, filterarg */
282                         MAXBSIZE, NDIS_NSEG_NEW,/* maxsize, nsegments */
283                         BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
284                         BUS_DMA_ALLOCNOW,       /* flags */
285                         &sc->ndis_parent_tag);
286
287         if (error)
288                 goto fail;
289
290         sc->ndis_iftype = PCIBus;
291
292         /* Figure out exactly which device we matched. */
293
294         t = ndis_devs;
295
296         while(t->ndis_name != NULL) {
297                 if ((pci_get_vendor(dev) == t->ndis_vid) &&
298                     (pci_get_device(dev) == t->ndis_did)) {
299                         if (t->ndis_subsys == 0)
300                                 defidx = devidx;
301                         else {
302                                 if (t->ndis_subsys ==
303                                     pci_read_config(dev, PCIR_SUBVEND_0, 4))
304                                         break;
305                         }
306                 }
307                 t++;
308                 devidx++;
309         }
310
311         if (ndis_devs[devidx].ndis_name == NULL)
312                 sc->ndis_devidx = defidx;
313         else
314                 sc->ndis_devidx = devidx;
315
316         error = ndis_attach(dev);
317
318 fail:
319         return(error);
320 }
321
322 static struct resource_list *
323 ndis_get_resource_list(device_t dev, device_t child)
324 {
325         struct ndis_softc       *sc;
326
327         sc = device_get_softc(dev);
328         return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev));
329 }
330
331 #endif /* NDIS_PCI_DEV_TABLE */