]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/if_ndis/if_ndis_pci.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r305575, and update
[FreeBSD/FreeBSD.git] / sys / dev / if_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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.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_var.h>
47 #include <net/if_arp.h>
48 #include <net/if_media.h>
49 #include <net/ethernet.h>
50
51 #include <machine/bus.h>
52 #include <machine/resource.h>
53 #include <sys/bus.h>
54 #include <sys/rman.h>
55
56 #include <net80211/ieee80211_var.h>
57
58 #include <dev/pci/pcireg.h>
59 #include <dev/pci/pcivar.h>
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62
63 #include <compat/ndis/pe_var.h>
64 #include <compat/ndis/cfg_var.h>
65 #include <compat/ndis/resource_var.h>
66 #include <compat/ndis/ntoskrnl_var.h>
67 #include <compat/ndis/ndis_var.h>
68 #include <dev/if_ndis/if_ndisvar.h>
69
70 MODULE_DEPEND(ndis, pci, 1, 1, 1);
71
72 static int ndis_probe_pci       (device_t);
73 static int ndis_attach_pci      (device_t);
74 static struct resource_list *ndis_get_resource_list
75                                 (device_t, device_t);
76 static int ndis_devcompare      (interface_type,
77                                  struct ndis_pci_type *, device_t);
78 extern int ndisdrv_modevent     (module_t, int, void *);
79 extern int ndis_attach          (device_t);
80 extern int ndis_shutdown        (device_t);
81 extern int ndis_detach          (device_t);
82 extern int ndis_suspend         (device_t);
83 extern int ndis_resume          (device_t);
84
85 static device_method_t ndis_methods[] = {
86         /* Device interface */
87         DEVMETHOD(device_probe,         ndis_probe_pci),
88         DEVMETHOD(device_attach,        ndis_attach_pci),
89         DEVMETHOD(device_detach,        ndis_detach),
90         DEVMETHOD(device_shutdown,      ndis_shutdown),
91         DEVMETHOD(device_suspend,       ndis_suspend),
92         DEVMETHOD(device_resume,        ndis_resume),
93
94         /* Bus interface */
95         DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
96
97         { 0, 0 }
98 };
99
100 static driver_t ndis_driver = {
101         "ndis",
102         ndis_methods,
103         sizeof(struct ndis_softc)
104 };
105
106 static devclass_t ndis_devclass;
107
108 DRIVER_MODULE(ndis, pci, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
109
110 static int
111 ndis_devcompare(bustype, t, dev)
112         interface_type          bustype;
113         struct ndis_pci_type    *t;
114         device_t                dev;
115 {
116         uint16_t                vid, did;
117         uint32_t                subsys;
118
119         if (bustype != PCIBus)
120                 return(FALSE);
121
122         vid = pci_get_vendor(dev);
123         did = pci_get_device(dev);
124         subsys = pci_get_subdevice(dev);
125         subsys = (subsys << 16) | pci_get_subvendor(dev);
126
127         while(t->ndis_name != NULL) {
128                 if ((t->ndis_vid == vid) && (t->ndis_did == did) &&
129                     (t->ndis_subsys == subsys || t->ndis_subsys == 0)) {
130                         device_set_desc(dev, t->ndis_name);
131                         return(TRUE);
132                 }
133                 t++;
134         }
135
136         return(FALSE);
137 }
138
139 /*
140  * Probe for an NDIS device. Check the PCI vendor and device
141  * IDs against our list and return a device name if we find a match.
142  */
143 static int
144 ndis_probe_pci(dev)
145         device_t                dev;
146 {
147         driver_object           *drv;
148         struct drvdb_ent        *db;
149
150         drv = windrv_lookup(0, "PCI Bus");
151
152         if (drv == NULL)
153                 return(ENXIO);
154
155         db = windrv_match((matchfuncptr)ndis_devcompare, dev);
156
157         if (db != NULL) {
158                 /* Create PDO for this device instance */
159                 windrv_create_pdo(drv, dev);
160                 return(0);
161         }
162
163         return(ENXIO);
164 }
165
166 /*
167  * Attach the interface. Allocate softc structures, do ifmedia
168  * setup and ethernet/BPF attach.
169  */
170 static int
171 ndis_attach_pci(dev)
172         device_t                dev;
173 {
174         struct ndis_softc       *sc;
175         int                     unit, error = 0, rid;
176         struct ndis_pci_type    *t;
177         int                     devidx = 0, defidx = 0;
178         struct resource_list    *rl;
179         struct resource_list_entry      *rle;
180         struct drvdb_ent        *db;
181         uint16_t                vid, did;
182         uint32_t                subsys;
183
184         sc = device_get_softc(dev);
185         unit = device_get_unit(dev);
186         sc->ndis_dev = dev;
187
188         db = windrv_match((matchfuncptr)ndis_devcompare, dev);
189         if (db == NULL)
190                 return (ENXIO);
191         sc->ndis_dobj = db->windrv_object;
192         sc->ndis_regvals = db->windrv_regvals;
193
194         /*
195          * Map control/status registers.
196          */
197
198         pci_enable_busmaster(dev);
199
200         rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
201         if (rl != NULL) {
202                 STAILQ_FOREACH(rle, rl, link) {
203                         switch (rle->type) {
204                         case SYS_RES_IOPORT:
205                                 sc->ndis_io_rid = rle->rid;
206                                 sc->ndis_res_io = bus_alloc_resource_any(dev,
207                                     SYS_RES_IOPORT, &sc->ndis_io_rid,
208                                     RF_ACTIVE);
209                                 if (sc->ndis_res_io == NULL) {
210                                         device_printf(dev,
211                                             "couldn't map iospace\n");
212                                         error = ENXIO;
213                                         goto fail;
214                                 }
215                                 break;
216                         case SYS_RES_MEMORY:
217                                 if (sc->ndis_res_altmem != NULL &&
218                                     sc->ndis_res_mem != NULL) {
219                                         device_printf(dev,
220                                             "too many memory resources\n");
221                                         error = ENXIO;
222                                         goto fail;
223                                 }
224                                 if (sc->ndis_res_mem) {
225                                         sc->ndis_altmem_rid = rle->rid;
226                                         sc->ndis_res_altmem =
227                                             bus_alloc_resource_any(dev,
228                                                 SYS_RES_MEMORY,
229                                                 &sc->ndis_altmem_rid,
230                                                 RF_ACTIVE);
231                                         if (sc->ndis_res_altmem == NULL) {
232                                                 device_printf(dev,
233                                                     "couldn't map alt "
234                                                     "memory\n");
235                                                 error = ENXIO;
236                                                 goto fail;
237                                         }
238                                 } else {
239                                         sc->ndis_mem_rid = rle->rid;
240                                         sc->ndis_res_mem =
241                                             bus_alloc_resource_any(dev,
242                                                 SYS_RES_MEMORY,
243                                                 &sc->ndis_mem_rid,
244                                                 RF_ACTIVE);
245                                         if (sc->ndis_res_mem == NULL) {
246                                                 device_printf(dev,
247                                                     "couldn't map memory\n");
248                                                 error = ENXIO;
249                                                 goto fail;
250                                         }
251                                 }
252                                 break;
253                         case SYS_RES_IRQ:
254                                 rid = rle->rid;
255                                 sc->ndis_irq = bus_alloc_resource_any(dev,
256                                     SYS_RES_IRQ, &rid,
257                                     RF_SHAREABLE | RF_ACTIVE);
258                                 if (sc->ndis_irq == NULL) {
259                                         device_printf(dev,
260                                             "couldn't map interrupt\n");
261                                         error = ENXIO;
262                                         goto fail;
263                                 }
264                                 break;
265                         default:
266                                 break;
267                         }
268                         sc->ndis_rescnt++;
269                 }
270         }
271
272         /*
273          * If the BIOS did not set up an interrupt for this device,
274          * the resource traversal code above will fail to set up
275          * an IRQ resource. This is usually a bad thing, so try to
276          * force the allocation of an interrupt here. If one was
277          * not assigned to us by the BIOS, bus_alloc_resource()
278          * should route one for us.
279          */
280         if (sc->ndis_irq == NULL) {
281                 rid = 0;
282                 sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
283                     &rid, RF_SHAREABLE | RF_ACTIVE);
284                 if (sc->ndis_irq == NULL) {
285                         device_printf(dev, "couldn't route interrupt\n");
286                         error = ENXIO;
287                         goto fail;
288                 }
289                 sc->ndis_rescnt++;
290         }
291
292         /*
293          * Allocate the parent bus DMA tag appropriate for PCI.
294          */
295 #define NDIS_NSEG_NEW 32
296         error = bus_dma_tag_create(bus_get_dma_tag(dev),/* PCI parent */
297                         1, 0,                   /* alignment, boundary */
298                         BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
299                         BUS_SPACE_MAXADDR,      /* highaddr */
300                         NULL, NULL,             /* filter, filterarg */
301                         DFLTPHYS, NDIS_NSEG_NEW,/* maxsize, nsegments */
302                         BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
303                         BUS_DMA_ALLOCNOW,       /* flags */
304                         NULL, NULL,             /* lockfunc, lockarg */
305                         &sc->ndis_parent_tag);
306
307         if (error)
308                 goto fail;
309
310         sc->ndis_iftype = PCIBus;
311
312         /* Figure out exactly which device we matched. */
313
314         vid = pci_get_vendor(dev);
315         did = pci_get_device(dev);
316         subsys = pci_get_subdevice(dev);
317         subsys = (subsys << 16) | pci_get_subvendor(dev);
318
319         t = db->windrv_devlist;
320
321         while(t->ndis_name != NULL) {
322                 if (t->ndis_vid == vid && t->ndis_did == did) {
323                         if (t->ndis_subsys == 0)
324                                 defidx = devidx;
325                         else if (t->ndis_subsys == subsys)
326                                 break;
327                 }
328                 t++;
329                 devidx++;
330         }
331
332         if (t->ndis_name == NULL)
333                 sc->ndis_devidx = defidx;
334         else
335                 sc->ndis_devidx = devidx;
336
337         error = ndis_attach(dev);
338
339 fail:
340         return(error);
341 }
342
343 static struct resource_list *
344 ndis_get_resource_list(dev, child)
345         device_t                dev;
346         device_t                child;
347 {
348         struct ndis_softc       *sc;
349
350         sc = device_get_softc(dev);
351         return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev));
352 }