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