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