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