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