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