Deleted Added
full compact
kern_ndis.c (216049) kern_ndis.c (216242)
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>
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: head/sys/compat/ndis/kern_ndis.c 216049 2010-11-29 10:10:56Z bschmidt $");
34__FBSDID("$FreeBSD: head/sys/compat/ndis/kern_ndis.c 216242 2010-12-06 20:54:53Z bschmidt $");
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/unistd.h>
39#include <sys/types.h>
40#include <sys/errno.h>
41#include <sys/callout.h>
42#include <sys/socket.h>
43#include <sys/queue.h>
44#include <sys/sysctl.h>
45#include <sys/proc.h>
46#include <sys/malloc.h>
47#include <sys/lock.h>
48#include <sys/mutex.h>
49#include <sys/conf.h>
50
51#include <sys/kernel.h>
52#include <sys/module.h>
53#include <sys/kthread.h>
54#include <machine/bus.h>
55#include <machine/resource.h>
56#include <sys/bus.h>
57#include <sys/rman.h>
58
59#include <net/if.h>
60#include <net/if_arp.h>
61#include <net/ethernet.h>
62#include <net/if_dl.h>
63#include <net/if_media.h>
64
65#include <net80211/ieee80211_var.h>
66#include <net80211/ieee80211_ioctl.h>
67
68#include <dev/usb/usb.h>
69#include <dev/usb/usbdi.h>
70
71#include <compat/ndis/pe_var.h>
72#include <compat/ndis/cfg_var.h>
73#include <compat/ndis/resource_var.h>
74#include <compat/ndis/ntoskrnl_var.h>
75#include <compat/ndis/ndis_var.h>
76#include <compat/ndis/hal_var.h>
77#include <compat/ndis/usbd_var.h>
78#include <dev/if_ndis/if_ndisvar.h>
79
80#define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
81
82static void ndis_status_func(ndis_handle, ndis_status, void *, uint32_t);
83static void ndis_statusdone_func(ndis_handle);
84static void ndis_setdone_func(ndis_handle, ndis_status);
85static void ndis_getdone_func(ndis_handle, ndis_status);
86static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
87static void ndis_sendrsrcavail_func(ndis_handle);
88static void ndis_intrsetup(kdpc *, device_object *,
89 irp *, struct ndis_softc *);
90static void ndis_return(device_object *, void *);
91
92static image_patch_table kernndis_functbl[] = {
93 IMPORT_SFUNC(ndis_status_func, 4),
94 IMPORT_SFUNC(ndis_statusdone_func, 1),
95 IMPORT_SFUNC(ndis_setdone_func, 2),
96 IMPORT_SFUNC(ndis_getdone_func, 2),
97 IMPORT_SFUNC(ndis_resetdone_func, 3),
98 IMPORT_SFUNC(ndis_sendrsrcavail_func, 1),
99 IMPORT_SFUNC(ndis_intrsetup, 4),
100 IMPORT_SFUNC(ndis_return, 1),
101
102 { NULL, NULL, NULL }
103};
104
105static struct nd_head ndis_devhead;
106
107/*
108 * This allows us to export our symbols to other modules.
109 * Note that we call ourselves 'ndisapi' to avoid a namespace
110 * collision with if_ndis.ko, which internally calls itself
111 * 'ndis.'
112 *
113 * Note: some of the subsystems depend on each other, so the
114 * order in which they're started is important. The order of
115 * importance is:
116 *
117 * HAL - spinlocks and IRQL manipulation
118 * ntoskrnl - DPC and workitem threads, object waiting
119 * windrv - driver/device registration
120 *
121 * The HAL should also be the last thing shut down, since
122 * the ntoskrnl subsystem will use spinlocks right up until
123 * the DPC and workitem threads are terminated.
124 */
125
126static int
127ndis_modevent(module_t mod, int cmd, void *arg)
128{
129 int error = 0;
130 image_patch_table *patch;
131
132 switch (cmd) {
133 case MOD_LOAD:
134 /* Initialize subsystems */
135 hal_libinit();
136 ntoskrnl_libinit();
137 windrv_libinit();
138 ndis_libinit();
139 usbd_libinit();
140
141 patch = kernndis_functbl;
142 while (patch->ipt_func != NULL) {
143 windrv_wrap((funcptr)patch->ipt_func,
144 (funcptr *)&patch->ipt_wrap,
145 patch->ipt_argcnt, patch->ipt_ftype);
146 patch++;
147 }
148
149 TAILQ_INIT(&ndis_devhead);
150 break;
151 case MOD_SHUTDOWN:
152 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
153 /* Shut down subsystems */
154 ndis_libfini();
155 usbd_libfini();
156 windrv_libfini();
157 ntoskrnl_libfini();
158 hal_libfini();
159
160 patch = kernndis_functbl;
161 while (patch->ipt_func != NULL) {
162 windrv_unwrap(patch->ipt_wrap);
163 patch++;
164 }
165 }
166 break;
167 case MOD_UNLOAD:
168 /* Shut down subsystems */
169 ndis_libfini();
170 usbd_libfini();
171 windrv_libfini();
172 ntoskrnl_libfini();
173 hal_libfini();
174
175 patch = kernndis_functbl;
176 while (patch->ipt_func != NULL) {
177 windrv_unwrap(patch->ipt_wrap);
178 patch++;
179 }
180
181 break;
182 default:
183 error = EINVAL;
184 break;
185 }
186
187 return (error);
188}
189DEV_MODULE(ndisapi, ndis_modevent, NULL);
190MODULE_VERSION(ndisapi, 1);
191
192static void
193ndis_sendrsrcavail_func(adapter)
194 ndis_handle adapter;
195{
196}
197
198static void
199ndis_status_func(adapter, status, sbuf, slen)
200 ndis_handle adapter;
201 ndis_status status;
202 void *sbuf;
203 uint32_t slen;
204{
205 ndis_miniport_block *block;
206 struct ndis_softc *sc;
207 struct ifnet *ifp;
208
209 block = adapter;
210 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
211 ifp = sc->ifp;
212 if (ifp->if_flags & IFF_DEBUG)
213 device_printf(sc->ndis_dev, "status: %x\n", status);
214}
215
216static void
217ndis_statusdone_func(adapter)
218 ndis_handle adapter;
219{
220 ndis_miniport_block *block;
221 struct ndis_softc *sc;
222 struct ifnet *ifp;
223
224 block = adapter;
225 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
226 ifp = sc->ifp;
227 if (ifp->if_flags & IFF_DEBUG)
228 device_printf(sc->ndis_dev, "status complete\n");
229}
230
231static void
232ndis_setdone_func(adapter, status)
233 ndis_handle adapter;
234 ndis_status status;
235{
236 ndis_miniport_block *block;
237 block = adapter;
238
239 block->nmb_setstat = status;
240 KeSetEvent(&block->nmb_setevent, IO_NO_INCREMENT, FALSE);
241}
242
243static void
244ndis_getdone_func(adapter, status)
245 ndis_handle adapter;
246 ndis_status status;
247{
248 ndis_miniport_block *block;
249 block = adapter;
250
251 block->nmb_getstat = status;
252 KeSetEvent(&block->nmb_getevent, IO_NO_INCREMENT, FALSE);
253}
254
255static void
256ndis_resetdone_func(ndis_handle adapter, ndis_status status,
257 uint8_t addressingreset)
258{
259 ndis_miniport_block *block;
260 struct ndis_softc *sc;
261 struct ifnet *ifp;
262
263 block = adapter;
264 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
265 ifp = sc->ifp;
266
267 if (ifp->if_flags & IFF_DEBUG)
268 device_printf(sc->ndis_dev, "reset done...\n");
269 KeSetEvent(&block->nmb_resetevent, IO_NO_INCREMENT, FALSE);
270}
271
272int
273ndis_create_sysctls(arg)
274 void *arg;
275{
276 struct ndis_softc *sc;
277 ndis_cfg *vals;
278 char buf[256];
279 struct sysctl_oid *oidp;
280 struct sysctl_ctx_entry *e;
281
282 if (arg == NULL)
283 return (EINVAL);
284
285 sc = arg;
286 vals = sc->ndis_regvals;
287
288 TAILQ_INIT(&sc->ndis_cfglist_head);
289
290 /* Add the driver-specific registry keys. */
291
292 while(1) {
293 if (vals->nc_cfgkey == NULL)
294 break;
295
296 if (vals->nc_idx != sc->ndis_devidx) {
297 vals++;
298 continue;
299 }
300
301 /* See if we already have a sysctl with this name */
302
303 oidp = NULL;
304 TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
305 oidp = e->entry;
306 if (strcasecmp(oidp->oid_name, vals->nc_cfgkey) == 0)
307 break;
308 oidp = NULL;
309 }
310
311 if (oidp != NULL) {
312 vals++;
313 continue;
314 }
315
316 ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
317 vals->nc_val, CTLFLAG_RW);
318 vals++;
319 }
320
321 /* Now add a couple of builtin keys. */
322
323 /*
324 * Environment can be either Windows (0) or WindowsNT (1).
325 * We qualify as the latter.
326 */
327 ndis_add_sysctl(sc, "Environment",
328 "Windows environment", "1", CTLFLAG_RD);
329
330 /* NDIS version should be 5.1. */
331 ndis_add_sysctl(sc, "NdisVersion",
332 "NDIS API Version", "0x00050001", CTLFLAG_RD);
333
334 /*
335 * Some miniport drivers rely on the existence of the SlotNumber,
336 * NetCfgInstanceId and DriverDesc keys.
337 */
338 ndis_add_sysctl(sc, "SlotNumber", "Slot Numer", "01", CTLFLAG_RD);
339 ndis_add_sysctl(sc, "NetCfgInstanceId", "NetCfgInstanceId",
340 "{12345678-1234-5678-CAFE0-123456789ABC}", CTLFLAG_RD);
341 ndis_add_sysctl(sc, "DriverDesc", "Driver Description",
342 "NDIS Network Adapter", CTLFLAG_RD);
343
344 /* Bus type (PCI, PCMCIA, etc...) */
345 sprintf(buf, "%d", (int)sc->ndis_iftype);
346 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
347
348 if (sc->ndis_res_io != NULL) {
349 sprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
350 ndis_add_sysctl(sc, "IOBaseAddress",
351 "Base I/O Address", buf, CTLFLAG_RD);
352 }
353
354 if (sc->ndis_irq != NULL) {
355 sprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
356 ndis_add_sysctl(sc, "InterruptNumber",
357 "Interrupt Number", buf, CTLFLAG_RD);
358 }
359
360 return (0);
361}
362
363int
364ndis_add_sysctl(arg, key, desc, val, flag)
365 void *arg;
366 char *key;
367 char *desc;
368 char *val;
369 int flag;
370{
371 struct ndis_softc *sc;
372 struct ndis_cfglist *cfg;
373 char descstr[256];
374
375 sc = arg;
376
377 cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO);
378
379 if (cfg == NULL) {
380 printf("failed for %s\n", key);
381 return (ENOMEM);
382 }
383
384 cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF);
385 if (desc == NULL) {
386 snprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
387 cfg->ndis_cfg.nc_cfgdesc = strdup(descstr, M_DEVBUF);
388 } else
389 cfg->ndis_cfg.nc_cfgdesc = strdup(desc, M_DEVBUF);
390 strcpy(cfg->ndis_cfg.nc_val, val);
391
392 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
393
394 cfg->ndis_oid =
395 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
396 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
397 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
398 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
399 cfg->ndis_cfg.nc_cfgdesc);
400
401 return (0);
402}
403
404/*
405 * Somewhere, somebody decided "hey, let's automatically create
406 * a sysctl tree for each device instance as it's created -- it'll
407 * make life so much easier!" Lies. Why must they turn the kernel
408 * into a house of lies?
409 */
410
411int
412ndis_flush_sysctls(arg)
413 void *arg;
414{
415 struct ndis_softc *sc;
416 struct ndis_cfglist *cfg;
417 struct sysctl_ctx_list *clist;
418
419 sc = arg;
420
421 clist = device_get_sysctl_ctx(sc->ndis_dev);
422
423 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
424 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
425 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
426 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
427 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
428 free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
429 free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
430 free(cfg, M_DEVBUF);
431 }
432
433 return (0);
434}
435
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/unistd.h>
39#include <sys/types.h>
40#include <sys/errno.h>
41#include <sys/callout.h>
42#include <sys/socket.h>
43#include <sys/queue.h>
44#include <sys/sysctl.h>
45#include <sys/proc.h>
46#include <sys/malloc.h>
47#include <sys/lock.h>
48#include <sys/mutex.h>
49#include <sys/conf.h>
50
51#include <sys/kernel.h>
52#include <sys/module.h>
53#include <sys/kthread.h>
54#include <machine/bus.h>
55#include <machine/resource.h>
56#include <sys/bus.h>
57#include <sys/rman.h>
58
59#include <net/if.h>
60#include <net/if_arp.h>
61#include <net/ethernet.h>
62#include <net/if_dl.h>
63#include <net/if_media.h>
64
65#include <net80211/ieee80211_var.h>
66#include <net80211/ieee80211_ioctl.h>
67
68#include <dev/usb/usb.h>
69#include <dev/usb/usbdi.h>
70
71#include <compat/ndis/pe_var.h>
72#include <compat/ndis/cfg_var.h>
73#include <compat/ndis/resource_var.h>
74#include <compat/ndis/ntoskrnl_var.h>
75#include <compat/ndis/ndis_var.h>
76#include <compat/ndis/hal_var.h>
77#include <compat/ndis/usbd_var.h>
78#include <dev/if_ndis/if_ndisvar.h>
79
80#define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
81
82static void ndis_status_func(ndis_handle, ndis_status, void *, uint32_t);
83static void ndis_statusdone_func(ndis_handle);
84static void ndis_setdone_func(ndis_handle, ndis_status);
85static void ndis_getdone_func(ndis_handle, ndis_status);
86static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
87static void ndis_sendrsrcavail_func(ndis_handle);
88static void ndis_intrsetup(kdpc *, device_object *,
89 irp *, struct ndis_softc *);
90static void ndis_return(device_object *, void *);
91
92static image_patch_table kernndis_functbl[] = {
93 IMPORT_SFUNC(ndis_status_func, 4),
94 IMPORT_SFUNC(ndis_statusdone_func, 1),
95 IMPORT_SFUNC(ndis_setdone_func, 2),
96 IMPORT_SFUNC(ndis_getdone_func, 2),
97 IMPORT_SFUNC(ndis_resetdone_func, 3),
98 IMPORT_SFUNC(ndis_sendrsrcavail_func, 1),
99 IMPORT_SFUNC(ndis_intrsetup, 4),
100 IMPORT_SFUNC(ndis_return, 1),
101
102 { NULL, NULL, NULL }
103};
104
105static struct nd_head ndis_devhead;
106
107/*
108 * This allows us to export our symbols to other modules.
109 * Note that we call ourselves 'ndisapi' to avoid a namespace
110 * collision with if_ndis.ko, which internally calls itself
111 * 'ndis.'
112 *
113 * Note: some of the subsystems depend on each other, so the
114 * order in which they're started is important. The order of
115 * importance is:
116 *
117 * HAL - spinlocks and IRQL manipulation
118 * ntoskrnl - DPC and workitem threads, object waiting
119 * windrv - driver/device registration
120 *
121 * The HAL should also be the last thing shut down, since
122 * the ntoskrnl subsystem will use spinlocks right up until
123 * the DPC and workitem threads are terminated.
124 */
125
126static int
127ndis_modevent(module_t mod, int cmd, void *arg)
128{
129 int error = 0;
130 image_patch_table *patch;
131
132 switch (cmd) {
133 case MOD_LOAD:
134 /* Initialize subsystems */
135 hal_libinit();
136 ntoskrnl_libinit();
137 windrv_libinit();
138 ndis_libinit();
139 usbd_libinit();
140
141 patch = kernndis_functbl;
142 while (patch->ipt_func != NULL) {
143 windrv_wrap((funcptr)patch->ipt_func,
144 (funcptr *)&patch->ipt_wrap,
145 patch->ipt_argcnt, patch->ipt_ftype);
146 patch++;
147 }
148
149 TAILQ_INIT(&ndis_devhead);
150 break;
151 case MOD_SHUTDOWN:
152 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
153 /* Shut down subsystems */
154 ndis_libfini();
155 usbd_libfini();
156 windrv_libfini();
157 ntoskrnl_libfini();
158 hal_libfini();
159
160 patch = kernndis_functbl;
161 while (patch->ipt_func != NULL) {
162 windrv_unwrap(patch->ipt_wrap);
163 patch++;
164 }
165 }
166 break;
167 case MOD_UNLOAD:
168 /* Shut down subsystems */
169 ndis_libfini();
170 usbd_libfini();
171 windrv_libfini();
172 ntoskrnl_libfini();
173 hal_libfini();
174
175 patch = kernndis_functbl;
176 while (patch->ipt_func != NULL) {
177 windrv_unwrap(patch->ipt_wrap);
178 patch++;
179 }
180
181 break;
182 default:
183 error = EINVAL;
184 break;
185 }
186
187 return (error);
188}
189DEV_MODULE(ndisapi, ndis_modevent, NULL);
190MODULE_VERSION(ndisapi, 1);
191
192static void
193ndis_sendrsrcavail_func(adapter)
194 ndis_handle adapter;
195{
196}
197
198static void
199ndis_status_func(adapter, status, sbuf, slen)
200 ndis_handle adapter;
201 ndis_status status;
202 void *sbuf;
203 uint32_t slen;
204{
205 ndis_miniport_block *block;
206 struct ndis_softc *sc;
207 struct ifnet *ifp;
208
209 block = adapter;
210 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
211 ifp = sc->ifp;
212 if (ifp->if_flags & IFF_DEBUG)
213 device_printf(sc->ndis_dev, "status: %x\n", status);
214}
215
216static void
217ndis_statusdone_func(adapter)
218 ndis_handle adapter;
219{
220 ndis_miniport_block *block;
221 struct ndis_softc *sc;
222 struct ifnet *ifp;
223
224 block = adapter;
225 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
226 ifp = sc->ifp;
227 if (ifp->if_flags & IFF_DEBUG)
228 device_printf(sc->ndis_dev, "status complete\n");
229}
230
231static void
232ndis_setdone_func(adapter, status)
233 ndis_handle adapter;
234 ndis_status status;
235{
236 ndis_miniport_block *block;
237 block = adapter;
238
239 block->nmb_setstat = status;
240 KeSetEvent(&block->nmb_setevent, IO_NO_INCREMENT, FALSE);
241}
242
243static void
244ndis_getdone_func(adapter, status)
245 ndis_handle adapter;
246 ndis_status status;
247{
248 ndis_miniport_block *block;
249 block = adapter;
250
251 block->nmb_getstat = status;
252 KeSetEvent(&block->nmb_getevent, IO_NO_INCREMENT, FALSE);
253}
254
255static void
256ndis_resetdone_func(ndis_handle adapter, ndis_status status,
257 uint8_t addressingreset)
258{
259 ndis_miniport_block *block;
260 struct ndis_softc *sc;
261 struct ifnet *ifp;
262
263 block = adapter;
264 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
265 ifp = sc->ifp;
266
267 if (ifp->if_flags & IFF_DEBUG)
268 device_printf(sc->ndis_dev, "reset done...\n");
269 KeSetEvent(&block->nmb_resetevent, IO_NO_INCREMENT, FALSE);
270}
271
272int
273ndis_create_sysctls(arg)
274 void *arg;
275{
276 struct ndis_softc *sc;
277 ndis_cfg *vals;
278 char buf[256];
279 struct sysctl_oid *oidp;
280 struct sysctl_ctx_entry *e;
281
282 if (arg == NULL)
283 return (EINVAL);
284
285 sc = arg;
286 vals = sc->ndis_regvals;
287
288 TAILQ_INIT(&sc->ndis_cfglist_head);
289
290 /* Add the driver-specific registry keys. */
291
292 while(1) {
293 if (vals->nc_cfgkey == NULL)
294 break;
295
296 if (vals->nc_idx != sc->ndis_devidx) {
297 vals++;
298 continue;
299 }
300
301 /* See if we already have a sysctl with this name */
302
303 oidp = NULL;
304 TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
305 oidp = e->entry;
306 if (strcasecmp(oidp->oid_name, vals->nc_cfgkey) == 0)
307 break;
308 oidp = NULL;
309 }
310
311 if (oidp != NULL) {
312 vals++;
313 continue;
314 }
315
316 ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
317 vals->nc_val, CTLFLAG_RW);
318 vals++;
319 }
320
321 /* Now add a couple of builtin keys. */
322
323 /*
324 * Environment can be either Windows (0) or WindowsNT (1).
325 * We qualify as the latter.
326 */
327 ndis_add_sysctl(sc, "Environment",
328 "Windows environment", "1", CTLFLAG_RD);
329
330 /* NDIS version should be 5.1. */
331 ndis_add_sysctl(sc, "NdisVersion",
332 "NDIS API Version", "0x00050001", CTLFLAG_RD);
333
334 /*
335 * Some miniport drivers rely on the existence of the SlotNumber,
336 * NetCfgInstanceId and DriverDesc keys.
337 */
338 ndis_add_sysctl(sc, "SlotNumber", "Slot Numer", "01", CTLFLAG_RD);
339 ndis_add_sysctl(sc, "NetCfgInstanceId", "NetCfgInstanceId",
340 "{12345678-1234-5678-CAFE0-123456789ABC}", CTLFLAG_RD);
341 ndis_add_sysctl(sc, "DriverDesc", "Driver Description",
342 "NDIS Network Adapter", CTLFLAG_RD);
343
344 /* Bus type (PCI, PCMCIA, etc...) */
345 sprintf(buf, "%d", (int)sc->ndis_iftype);
346 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
347
348 if (sc->ndis_res_io != NULL) {
349 sprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
350 ndis_add_sysctl(sc, "IOBaseAddress",
351 "Base I/O Address", buf, CTLFLAG_RD);
352 }
353
354 if (sc->ndis_irq != NULL) {
355 sprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
356 ndis_add_sysctl(sc, "InterruptNumber",
357 "Interrupt Number", buf, CTLFLAG_RD);
358 }
359
360 return (0);
361}
362
363int
364ndis_add_sysctl(arg, key, desc, val, flag)
365 void *arg;
366 char *key;
367 char *desc;
368 char *val;
369 int flag;
370{
371 struct ndis_softc *sc;
372 struct ndis_cfglist *cfg;
373 char descstr[256];
374
375 sc = arg;
376
377 cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO);
378
379 if (cfg == NULL) {
380 printf("failed for %s\n", key);
381 return (ENOMEM);
382 }
383
384 cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF);
385 if (desc == NULL) {
386 snprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
387 cfg->ndis_cfg.nc_cfgdesc = strdup(descstr, M_DEVBUF);
388 } else
389 cfg->ndis_cfg.nc_cfgdesc = strdup(desc, M_DEVBUF);
390 strcpy(cfg->ndis_cfg.nc_val, val);
391
392 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
393
394 cfg->ndis_oid =
395 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
396 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
397 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
398 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
399 cfg->ndis_cfg.nc_cfgdesc);
400
401 return (0);
402}
403
404/*
405 * Somewhere, somebody decided "hey, let's automatically create
406 * a sysctl tree for each device instance as it's created -- it'll
407 * make life so much easier!" Lies. Why must they turn the kernel
408 * into a house of lies?
409 */
410
411int
412ndis_flush_sysctls(arg)
413 void *arg;
414{
415 struct ndis_softc *sc;
416 struct ndis_cfglist *cfg;
417 struct sysctl_ctx_list *clist;
418
419 sc = arg;
420
421 clist = device_get_sysctl_ctx(sc->ndis_dev);
422
423 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
424 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
425 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
426 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
427 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
428 free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
429 free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
430 free(cfg, M_DEVBUF);
431 }
432
433 return (0);
434}
435
436void *
437ndis_get_routine_address(functbl, name)
438 struct image_patch_table *functbl;
439 char *name;
440{
441 int i;
442
443 for (i = 0; functbl[i].ipt_name != NULL; i++)
444 if (strcmp(name, functbl[i].ipt_name) == 0)
445 return (functbl[i].ipt_wrap);
446 return (NULL);
447}
448
436static void
437ndis_return(dobj, arg)
438 device_object *dobj;
439 void *arg;
440{
441 ndis_miniport_block *block;
442 ndis_miniport_characteristics *ch;
443 ndis_return_handler returnfunc;
444 ndis_handle adapter;
445 ndis_packet *p;
446 uint8_t irql;
447 list_entry *l;
448
449 block = arg;
450 ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
451
452 p = arg;
453 adapter = block->nmb_miniportadapterctx;
454
455 if (adapter == NULL)
456 return;
457
458 returnfunc = ch->nmc_return_packet_func;
459
460 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
461 while (!IsListEmpty(&block->nmb_returnlist)) {
462 l = RemoveHeadList((&block->nmb_returnlist));
463 p = CONTAINING_RECORD(l, ndis_packet, np_list);
464 InitializeListHead((&p->np_list));
465 KeReleaseSpinLock(&block->nmb_returnlock, irql);
466 MSCALL2(returnfunc, adapter, p);
467 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
468 }
469 KeReleaseSpinLock(&block->nmb_returnlock, irql);
470}
471
472void
473ndis_return_packet(buf, arg)
474 void *buf; /* not used */
475 void *arg;
476{
477 ndis_packet *p;
478 ndis_miniport_block *block;
479
480 if (arg == NULL)
481 return;
482
483 p = arg;
484
485 /* Decrement refcount. */
486 p->np_refcnt--;
487
488 /* Release packet when refcount hits zero, otherwise return. */
489 if (p->np_refcnt)
490 return;
491
492 block = ((struct ndis_softc *)p->np_softc)->ndis_block;
493
494 KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
495 InitializeListHead((&p->np_list));
496 InsertHeadList((&block->nmb_returnlist), (&p->np_list));
497 KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
498
499 IoQueueWorkItem(block->nmb_returnitem,
500 (io_workitem_func)kernndis_functbl[7].ipt_wrap,
501 WORKQUEUE_CRITICAL, block);
502}
503
504void
505ndis_free_bufs(b0)
506 ndis_buffer *b0;
507{
508 ndis_buffer *next;
509
510 if (b0 == NULL)
511 return;
512
513 while(b0 != NULL) {
514 next = b0->mdl_next;
515 IoFreeMdl(b0);
516 b0 = next;
517 }
518}
519
520void
521ndis_free_packet(p)
522 ndis_packet *p;
523{
524 if (p == NULL)
525 return;
526
527 ndis_free_bufs(p->np_private.npp_head);
528 NdisFreePacket(p);
529}
530
531int
532ndis_convert_res(arg)
533 void *arg;
534{
535 struct ndis_softc *sc;
536 ndis_resource_list *rl = NULL;
537 cm_partial_resource_desc *prd = NULL;
538 ndis_miniport_block *block;
539 device_t dev;
540 struct resource_list *brl;
541 struct resource_list_entry *brle;
542 int error = 0;
543
544 sc = arg;
545 block = sc->ndis_block;
546 dev = sc->ndis_dev;
547
548 rl = malloc(sizeof(ndis_resource_list) +
549 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
550 M_DEVBUF, M_NOWAIT|M_ZERO);
551
552 if (rl == NULL)
553 return (ENOMEM);
554
555 rl->cprl_version = 5;
556 rl->cprl_version = 1;
557 rl->cprl_count = sc->ndis_rescnt;
558 prd = rl->cprl_partial_descs;
559
560 brl = BUS_GET_RESOURCE_LIST(dev, dev);
561
562 if (brl != NULL) {
563
564 STAILQ_FOREACH(brle, brl, link) {
565 switch (brle->type) {
566 case SYS_RES_IOPORT:
567 prd->cprd_type = CmResourceTypePort;
568 prd->cprd_flags = CM_RESOURCE_PORT_IO;
569 prd->cprd_sharedisp =
570 CmResourceShareDeviceExclusive;
571 prd->u.cprd_port.cprd_start.np_quad =
572 brle->start;
573 prd->u.cprd_port.cprd_len = brle->count;
574 break;
575 case SYS_RES_MEMORY:
576 prd->cprd_type = CmResourceTypeMemory;
577 prd->cprd_flags =
578 CM_RESOURCE_MEMORY_READ_WRITE;
579 prd->cprd_sharedisp =
580 CmResourceShareDeviceExclusive;
581 prd->u.cprd_port.cprd_start.np_quad =
582 brle->start;
583 prd->u.cprd_port.cprd_len = brle->count;
584 break;
585 case SYS_RES_IRQ:
586 prd->cprd_type = CmResourceTypeInterrupt;
587 prd->cprd_flags = 0;
588 /*
589 * Always mark interrupt resources as
590 * shared, since in our implementation,
591 * they will be.
592 */
593 prd->cprd_sharedisp =
594 CmResourceShareShared;
595 prd->u.cprd_intr.cprd_level = brle->start;
596 prd->u.cprd_intr.cprd_vector = brle->start;
597 prd->u.cprd_intr.cprd_affinity = 0;
598 break;
599 default:
600 break;
601 }
602 prd++;
603 }
604 }
605
606 block->nmb_rlist = rl;
607
608 return (error);
609}
610
611/*
612 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
613 * packet, it will hand it to us in the form of an ndis_packet,
614 * which we need to convert to an mbuf that is then handed off
615 * to the stack. Note: we configure the mbuf list so that it uses
616 * the memory regions specified by the ndis_buffer structures in
617 * the ndis_packet as external storage. In most cases, this will
618 * point to a memory region allocated by the driver (either by
619 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
620 * the driver to handle free()ing this region for is, so we set up
621 * a dummy no-op free handler for it.
622 */
623
624int
625ndis_ptom(m0, p)
626 struct mbuf **m0;
627 ndis_packet *p;
628{
629 struct mbuf *m = NULL, *prev = NULL;
630 ndis_buffer *buf;
631 ndis_packet_private *priv;
632 uint32_t totlen = 0;
633 struct ifnet *ifp;
634 struct ether_header *eh;
635 int diff;
636
637 if (p == NULL || m0 == NULL)
638 return (EINVAL);
639
640 priv = &p->np_private;
641 buf = priv->npp_head;
642 p->np_refcnt = 0;
643
644 for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
645 if (buf == priv->npp_head)
646#ifdef MT_HEADER
647 MGETHDR(m, M_DONTWAIT, MT_HEADER);
648#else
649 MGETHDR(m, M_DONTWAIT, MT_DATA);
650#endif
651 else
652 MGET(m, M_DONTWAIT, MT_DATA);
653 if (m == NULL) {
654 m_freem(*m0);
655 *m0 = NULL;
656 return (ENOBUFS);
657 }
658 m->m_len = MmGetMdlByteCount(buf);
659 m->m_data = MmGetMdlVirtualAddress(buf);
660 MEXTADD(m, m->m_data, m->m_len, ndis_return_packet,
661 m->m_data, p, 0, EXT_NDIS);
662 p->np_refcnt++;
663
664 totlen += m->m_len;
665 if (m->m_flags & M_PKTHDR)
666 *m0 = m;
667 else
668 prev->m_next = m;
669 prev = m;
670 }
671
672 /*
673 * This is a hack to deal with the Marvell 8335 driver
674 * which, when associated with an AP in WPA-PSK mode,
675 * seems to overpad its frames by 8 bytes. I don't know
676 * that the extra 8 bytes are for, and they're not there
677 * in open mode, so for now clamp the frame size at 1514
678 * until I can figure out how to deal with this properly,
679 * otherwise if_ethersubr() will spank us by discarding
680 * the 'oversize' frames.
681 */
682
683 eh = mtod((*m0), struct ether_header *);
684 ifp = ((struct ndis_softc *)p->np_softc)->ifp;
685 if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
686 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
687 totlen -= diff;
688 m->m_len -= diff;
689 }
690 (*m0)->m_pkthdr.len = totlen;
691
692 return (0);
693}
694
695/*
696 * Create an NDIS packet from an mbuf chain.
697 * This is used mainly when transmitting packets, where we need
698 * to turn an mbuf off an interface's send queue and transform it
699 * into an NDIS packet which will be fed into the NDIS driver's
700 * send routine.
701 *
702 * NDIS packets consist of two parts: an ndis_packet structure,
703 * which is vaguely analagous to the pkthdr portion of an mbuf,
704 * and one or more ndis_buffer structures, which define the
705 * actual memory segments in which the packet data resides.
706 * We need to allocate one ndis_buffer for each mbuf in a chain,
707 * plus one ndis_packet as the header.
708 */
709
710int
711ndis_mtop(m0, p)
712 struct mbuf *m0;
713 ndis_packet **p;
714{
715 struct mbuf *m;
716 ndis_buffer *buf = NULL, *prev = NULL;
717 ndis_packet_private *priv;
718
719 if (p == NULL || *p == NULL || m0 == NULL)
720 return (EINVAL);
721
722 priv = &(*p)->np_private;
723 priv->npp_totlen = m0->m_pkthdr.len;
724
725 for (m = m0; m != NULL; m = m->m_next) {
726 if (m->m_len == 0)
727 continue;
728 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
729 if (buf == NULL) {
730 ndis_free_packet(*p);
731 *p = NULL;
732 return (ENOMEM);
733 }
734 MmBuildMdlForNonPagedPool(buf);
735
736 if (priv->npp_head == NULL)
737 priv->npp_head = buf;
738 else
739 prev->mdl_next = buf;
740 prev = buf;
741 }
742
743 priv->npp_tail = buf;
744
745 return (0);
746}
747
748int
749ndis_get_supported_oids(arg, oids, oidcnt)
750 void *arg;
751 ndis_oid **oids;
752 int *oidcnt;
753{
754 int len, rval;
755 ndis_oid *o;
756
757 if (arg == NULL || oids == NULL || oidcnt == NULL)
758 return (EINVAL);
759 len = 0;
760 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
761
762 o = malloc(len, M_DEVBUF, M_NOWAIT);
763 if (o == NULL)
764 return (ENOMEM);
765
766 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
767
768 if (rval) {
769 free(o, M_DEVBUF);
770 return (rval);
771 }
772
773 *oids = o;
774 *oidcnt = len / 4;
775
776 return (0);
777}
778
779int
780ndis_set_info(arg, oid, buf, buflen)
781 void *arg;
782 ndis_oid oid;
783 void *buf;
784 int *buflen;
785{
786 struct ndis_softc *sc;
787 ndis_status rval;
788 ndis_handle adapter;
789 ndis_setinfo_handler setfunc;
790 uint32_t byteswritten = 0, bytesneeded = 0;
791 uint8_t irql;
792 uint64_t duetime;
793
794 /*
795 * According to the NDIS spec, MiniportQueryInformation()
796 * and MiniportSetInformation() requests are handled serially:
797 * once one request has been issued, we must wait for it to
798 * finish before allowing another request to proceed.
799 */
800
801 sc = arg;
802
803 KeResetEvent(&sc->ndis_block->nmb_setevent);
804
805 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
806
807 if (sc->ndis_block->nmb_pendingreq != NULL) {
808 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
809 panic("ndis_set_info() called while other request pending");
810 } else
811 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
812
813 setfunc = sc->ndis_chars->nmc_setinfo_func;
814 adapter = sc->ndis_block->nmb_miniportadapterctx;
815
816 if (adapter == NULL || setfunc == NULL ||
817 sc->ndis_block->nmb_devicectx == NULL) {
818 sc->ndis_block->nmb_pendingreq = NULL;
819 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
820 return (ENXIO);
821 }
822
823 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
824 &byteswritten, &bytesneeded);
825
826 sc->ndis_block->nmb_pendingreq = NULL;
827
828 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
829
830 if (rval == NDIS_STATUS_PENDING) {
831 /* Wait up to 5 seconds. */
832 duetime = (5 * 1000000) * -10;
833 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
834 0, 0, FALSE, &duetime);
835 rval = sc->ndis_block->nmb_setstat;
836 }
837
838 if (byteswritten)
839 *buflen = byteswritten;
840 if (bytesneeded)
841 *buflen = bytesneeded;
842
843 if (rval == NDIS_STATUS_INVALID_LENGTH)
844 return (ENOSPC);
845
846 if (rval == NDIS_STATUS_INVALID_OID)
847 return (EINVAL);
848
849 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
850 rval == NDIS_STATUS_NOT_ACCEPTED)
851 return (ENOTSUP);
852
853 if (rval != NDIS_STATUS_SUCCESS)
854 return (ENODEV);
855
856 return (0);
857}
858
859typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
860
861int
862ndis_send_packets(arg, packets, cnt)
863 void *arg;
864 ndis_packet **packets;
865 int cnt;
866{
867 struct ndis_softc *sc;
868 ndis_handle adapter;
869 ndis_sendmulti_handler sendfunc;
870 ndis_senddone_func senddonefunc;
871 int i;
872 ndis_packet *p;
873 uint8_t irql = 0;
874
875 sc = arg;
876 adapter = sc->ndis_block->nmb_miniportadapterctx;
877 if (adapter == NULL)
878 return (ENXIO);
879 sendfunc = sc->ndis_chars->nmc_sendmulti_func;
880 senddonefunc = sc->ndis_block->nmb_senddone_func;
881
882 if (NDIS_SERIALIZED(sc->ndis_block))
883 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
884
885 MSCALL3(sendfunc, adapter, packets, cnt);
886
887 for (i = 0; i < cnt; i++) {
888 p = packets[i];
889 /*
890 * Either the driver already handed the packet to
891 * ndis_txeof() due to a failure, or it wants to keep
892 * it and release it asynchronously later. Skip to the
893 * next one.
894 */
895 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
896 continue;
897 MSCALL3(senddonefunc, sc->ndis_block, p, p->np_oob.npo_status);
898 }
899
900 if (NDIS_SERIALIZED(sc->ndis_block))
901 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
902
903 return (0);
904}
905
906int
907ndis_send_packet(arg, packet)
908 void *arg;
909 ndis_packet *packet;
910{
911 struct ndis_softc *sc;
912 ndis_handle adapter;
913 ndis_status status;
914 ndis_sendsingle_handler sendfunc;
915 ndis_senddone_func senddonefunc;
916 uint8_t irql = 0;
917
918 sc = arg;
919 adapter = sc->ndis_block->nmb_miniportadapterctx;
920 if (adapter == NULL)
921 return (ENXIO);
922 sendfunc = sc->ndis_chars->nmc_sendsingle_func;
923 senddonefunc = sc->ndis_block->nmb_senddone_func;
924
925 if (NDIS_SERIALIZED(sc->ndis_block))
926 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
927 status = MSCALL3(sendfunc, adapter, packet,
928 packet->np_private.npp_flags);
929
930 if (status == NDIS_STATUS_PENDING) {
931 if (NDIS_SERIALIZED(sc->ndis_block))
932 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
933 return (0);
934 }
935
936 MSCALL3(senddonefunc, sc->ndis_block, packet, status);
937
938 if (NDIS_SERIALIZED(sc->ndis_block))
939 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
940
941 return (0);
942}
943
944int
945ndis_init_dma(arg)
946 void *arg;
947{
948 struct ndis_softc *sc;
949 int i, error;
950
951 sc = arg;
952
953 sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
954 M_DEVBUF, M_NOWAIT|M_ZERO);
955
956 if (sc->ndis_tmaps == NULL)
957 return (ENOMEM);
958
959 for (i = 0; i < sc->ndis_maxpkts; i++) {
960 error = bus_dmamap_create(sc->ndis_ttag, 0,
961 &sc->ndis_tmaps[i]);
962 if (error) {
963 free(sc->ndis_tmaps, M_DEVBUF);
964 return (ENODEV);
965 }
966 }
967
968 return (0);
969}
970
971int
972ndis_destroy_dma(arg)
973 void *arg;
974{
975 struct ndis_softc *sc;
976 struct mbuf *m;
977 ndis_packet *p = NULL;
978 int i;
979
980 sc = arg;
981
982 for (i = 0; i < sc->ndis_maxpkts; i++) {
983 if (sc->ndis_txarray[i] != NULL) {
984 p = sc->ndis_txarray[i];
985 m = (struct mbuf *)p->np_rsvd[1];
986 if (m != NULL)
987 m_freem(m);
988 ndis_free_packet(sc->ndis_txarray[i]);
989 }
990 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
991 }
992
993 free(sc->ndis_tmaps, M_DEVBUF);
994
995 bus_dma_tag_destroy(sc->ndis_ttag);
996
997 return (0);
998}
999
1000int
1001ndis_reset_nic(arg)
1002 void *arg;
1003{
1004 struct ndis_softc *sc;
1005 ndis_handle adapter;
1006 ndis_reset_handler resetfunc;
1007 uint8_t addressing_reset;
1008 int rval;
1009 uint8_t irql = 0;
1010
1011 sc = arg;
1012
1013 NDIS_LOCK(sc);
1014 adapter = sc->ndis_block->nmb_miniportadapterctx;
1015 resetfunc = sc->ndis_chars->nmc_reset_func;
1016
1017 if (adapter == NULL || resetfunc == NULL ||
1018 sc->ndis_block->nmb_devicectx == NULL) {
1019 NDIS_UNLOCK(sc);
1020 return (EIO);
1021 }
1022
1023 NDIS_UNLOCK(sc);
1024
1025 KeResetEvent(&sc->ndis_block->nmb_resetevent);
1026
1027 if (NDIS_SERIALIZED(sc->ndis_block))
1028 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1029
1030 rval = MSCALL2(resetfunc, &addressing_reset, adapter);
1031
1032 if (NDIS_SERIALIZED(sc->ndis_block))
1033 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1034
1035 if (rval == NDIS_STATUS_PENDING)
1036 KeWaitForSingleObject(&sc->ndis_block->nmb_resetevent,
1037 0, 0, FALSE, NULL);
1038
1039 return (0);
1040}
1041
1042int
1043ndis_halt_nic(arg)
1044 void *arg;
1045{
1046 struct ndis_softc *sc;
1047 ndis_handle adapter;
1048 ndis_halt_handler haltfunc;
1049 ndis_miniport_block *block;
1050 int empty = 0;
1051 uint8_t irql;
1052
1053 sc = arg;
1054 block = sc->ndis_block;
1055
1056 if (!cold)
1057 KeFlushQueuedDpcs();
1058
1059 /*
1060 * Wait for all packets to be returned.
1061 */
1062
1063 while (1) {
1064 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
1065 empty = IsListEmpty(&block->nmb_returnlist);
1066 KeReleaseSpinLock(&block->nmb_returnlock, irql);
1067 if (empty)
1068 break;
1069 NdisMSleep(1000);
1070 }
1071
1072 NDIS_LOCK(sc);
1073 adapter = sc->ndis_block->nmb_miniportadapterctx;
1074 if (adapter == NULL) {
1075 NDIS_UNLOCK(sc);
1076 return (EIO);
1077 }
1078
1079 sc->ndis_block->nmb_devicectx = NULL;
1080
1081 /*
1082 * The adapter context is only valid after the init
1083 * handler has been called, and is invalid once the
1084 * halt handler has been called.
1085 */
1086
1087 haltfunc = sc->ndis_chars->nmc_halt_func;
1088 NDIS_UNLOCK(sc);
1089
1090 MSCALL1(haltfunc, adapter);
1091
1092 NDIS_LOCK(sc);
1093 sc->ndis_block->nmb_miniportadapterctx = NULL;
1094 NDIS_UNLOCK(sc);
1095
1096 return (0);
1097}
1098
1099int
1100ndis_shutdown_nic(arg)
1101 void *arg;
1102{
1103 struct ndis_softc *sc;
1104 ndis_handle adapter;
1105 ndis_shutdown_handler shutdownfunc;
1106
1107 sc = arg;
1108 NDIS_LOCK(sc);
1109 adapter = sc->ndis_block->nmb_miniportadapterctx;
1110 shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1111 NDIS_UNLOCK(sc);
1112 if (adapter == NULL || shutdownfunc == NULL)
1113 return (EIO);
1114
1115 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1116 MSCALL1(shutdownfunc, adapter);
1117 else
1118 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
1119
1120 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1121
1122 return (0);
1123}
1124
1125int
1126ndis_pnpevent_nic(arg, type)
1127 void *arg;
1128 int type;
1129{
1130 device_t dev;
1131 struct ndis_softc *sc;
1132 ndis_handle adapter;
1133 ndis_pnpevent_handler pnpeventfunc;
1134
1135 dev = arg;
1136 sc = device_get_softc(arg);
1137 NDIS_LOCK(sc);
1138 adapter = sc->ndis_block->nmb_miniportadapterctx;
1139 pnpeventfunc = sc->ndis_chars->nmc_pnpevent_handler;
1140 NDIS_UNLOCK(sc);
1141 if (adapter == NULL || pnpeventfunc == NULL)
1142 return (EIO);
1143
1144 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1145 MSCALL4(pnpeventfunc, adapter, type, NULL, 0);
1146 else
1147 MSCALL4(pnpeventfunc, sc->ndis_chars->nmc_rsvd0, type, NULL, 0);
1148
1149 return (0);
1150}
1151
1152int
1153ndis_init_nic(arg)
1154 void *arg;
1155{
1156 struct ndis_softc *sc;
1157 ndis_miniport_block *block;
1158 ndis_init_handler initfunc;
1159 ndis_status status, openstatus = 0;
1160 ndis_medium mediumarray[NdisMediumMax];
1161 uint32_t chosenmedium, i;
1162
1163 if (arg == NULL)
1164 return (EINVAL);
1165
1166 sc = arg;
1167 NDIS_LOCK(sc);
1168 block = sc->ndis_block;
1169 initfunc = sc->ndis_chars->nmc_init_func;
1170 NDIS_UNLOCK(sc);
1171
1172 sc->ndis_block->nmb_timerlist = NULL;
1173
1174 for (i = 0; i < NdisMediumMax; i++)
1175 mediumarray[i] = i;
1176
1177 status = MSCALL6(initfunc, &openstatus, &chosenmedium,
1178 mediumarray, NdisMediumMax, block, block);
1179
1180 /*
1181 * If the init fails, blow away the other exported routines
1182 * we obtained from the driver so we can't call them later.
1183 * If the init failed, none of these will work.
1184 */
1185 if (status != NDIS_STATUS_SUCCESS) {
1186 NDIS_LOCK(sc);
1187 sc->ndis_block->nmb_miniportadapterctx = NULL;
1188 NDIS_UNLOCK(sc);
1189 return (ENXIO);
1190 }
1191
1192 /*
1193 * This may look really goofy, but apparently it is possible
1194 * to halt a miniport too soon after it's been initialized.
1195 * After MiniportInitialize() finishes, pause for 1 second
1196 * to give the chip a chance to handle any short-lived timers
1197 * that were set in motion. If we call MiniportHalt() too soon,
1198 * some of the timers may not be cancelled, because the driver
1199 * expects them to fire before the halt is called.
1200 */
1201
1202 pause("ndwait", hz);
1203
1204 NDIS_LOCK(sc);
1205 sc->ndis_block->nmb_devicectx = sc;
1206 NDIS_UNLOCK(sc);
1207
1208 return (0);
1209}
1210
1211static void
1212ndis_intrsetup(dpc, dobj, ip, sc)
1213 kdpc *dpc;
1214 device_object *dobj;
1215 irp *ip;
1216 struct ndis_softc *sc;
1217{
1218 ndis_miniport_interrupt *intr;
1219
1220 intr = sc->ndis_block->nmb_interrupt;
1221
1222 /* Sanity check. */
1223
1224 if (intr == NULL)
1225 return;
1226
1227 KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
1228 KeResetEvent(&intr->ni_dpcevt);
1229 if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
1230 intr->ni_dpccnt++;
1231 KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
1232}
1233
1234int
1235ndis_get_info(arg, oid, buf, buflen)
1236 void *arg;
1237 ndis_oid oid;
1238 void *buf;
1239 int *buflen;
1240{
1241 struct ndis_softc *sc;
1242 ndis_status rval;
1243 ndis_handle adapter;
1244 ndis_queryinfo_handler queryfunc;
1245 uint32_t byteswritten = 0, bytesneeded = 0;
1246 uint8_t irql;
1247 uint64_t duetime;
1248
1249 sc = arg;
1250
1251 KeResetEvent(&sc->ndis_block->nmb_getevent);
1252
1253 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1254
1255 if (sc->ndis_block->nmb_pendingreq != NULL) {
1256 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1257 panic("ndis_get_info() called while other request pending");
1258 } else
1259 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
1260
1261 queryfunc = sc->ndis_chars->nmc_queryinfo_func;
1262 adapter = sc->ndis_block->nmb_miniportadapterctx;
1263
1264 if (adapter == NULL || queryfunc == NULL ||
1265 sc->ndis_block->nmb_devicectx == NULL) {
1266 sc->ndis_block->nmb_pendingreq = NULL;
1267 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1268 return (ENXIO);
1269 }
1270
1271 rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
1272 &byteswritten, &bytesneeded);
1273
1274 sc->ndis_block->nmb_pendingreq = NULL;
1275
1276 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1277
1278 /* Wait for requests that block. */
1279
1280 if (rval == NDIS_STATUS_PENDING) {
1281 /* Wait up to 5 seconds. */
1282 duetime = (5 * 1000000) * -10;
1283 KeWaitForSingleObject(&sc->ndis_block->nmb_getevent,
1284 0, 0, FALSE, &duetime);
1285 rval = sc->ndis_block->nmb_getstat;
1286 }
1287
1288 if (byteswritten)
1289 *buflen = byteswritten;
1290 if (bytesneeded)
1291 *buflen = bytesneeded;
1292
1293 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1294 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1295 return (ENOSPC);
1296
1297 if (rval == NDIS_STATUS_INVALID_OID)
1298 return (EINVAL);
1299
1300 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1301 rval == NDIS_STATUS_NOT_ACCEPTED)
1302 return (ENOTSUP);
1303
1304 if (rval != NDIS_STATUS_SUCCESS)
1305 return (ENODEV);
1306
1307 return (0);
1308}
1309
1310uint32_t
1311NdisAddDevice(drv, pdo)
1312 driver_object *drv;
1313 device_object *pdo;
1314{
1315 device_object *fdo;
1316 ndis_miniport_block *block;
1317 struct ndis_softc *sc;
1318 uint32_t status;
1319 int error;
1320
1321 sc = device_get_softc(pdo->do_devext);
1322
1323 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
1324 error = bus_setup_intr(sc->ndis_dev, sc->ndis_irq,
1325 INTR_TYPE_NET | INTR_MPSAFE,
1326 NULL, ntoskrnl_intr, NULL, &sc->ndis_intrhand);
1327 if (error)
1328 return (NDIS_STATUS_FAILURE);
1329 }
1330
1331 status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1332 FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
1333
1334 if (status != STATUS_SUCCESS)
1335 return (status);
1336
1337 block = fdo->do_devext;
1338
1339 block->nmb_filterdbs.nf_ethdb = block;
1340 block->nmb_deviceobj = fdo;
1341 block->nmb_physdeviceobj = pdo;
1342 block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
1343 KeInitializeSpinLock(&block->nmb_lock);
1344 KeInitializeSpinLock(&block->nmb_returnlock);
1345 KeInitializeEvent(&block->nmb_getevent, EVENT_TYPE_NOTIFY, TRUE);
1346 KeInitializeEvent(&block->nmb_setevent, EVENT_TYPE_NOTIFY, TRUE);
1347 KeInitializeEvent(&block->nmb_resetevent, EVENT_TYPE_NOTIFY, TRUE);
1348 InitializeListHead(&block->nmb_parmlist);
1349 InitializeListHead(&block->nmb_returnlist);
1350 block->nmb_returnitem = IoAllocateWorkItem(fdo);
1351
1352 /*
1353 * Stash pointers to the miniport block and miniport
1354 * characteristics info in the if_ndis softc so the
1355 * UNIX wrapper driver can get to them later.
1356 */
1357 sc->ndis_block = block;
1358 sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1);
1359
1360 /*
1361 * If the driver has a MiniportTransferData() function,
1362 * we should allocate a private RX packet pool.
1363 */
1364
1365 if (sc->ndis_chars->nmc_transferdata_func != NULL) {
1366 NdisAllocatePacketPool(&status, &block->nmb_rxpool,
1367 32, PROTOCOL_RESERVED_SIZE_IN_PACKET);
1368 if (status != NDIS_STATUS_SUCCESS) {
1369 IoDetachDevice(block->nmb_nextdeviceobj);
1370 IoDeleteDevice(fdo);
1371 return (status);
1372 }
1373 InitializeListHead((&block->nmb_packetlist));
1374 }
1375
1376 /* Give interrupt handling priority over timers. */
1377 IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1378 KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
1379
1380 /* Finish up BSD-specific setup. */
1381
1382 block->nmb_signature = (void *)0xcafebabe;
1383 block->nmb_status_func = kernndis_functbl[0].ipt_wrap;
1384 block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap;
1385 block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap;
1386 block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap;
1387 block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap;
1388 block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap;
1389 block->nmb_pendingreq = NULL;
1390
1391 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
1392
1393 return (STATUS_SUCCESS);
1394}
1395
1396int
1397ndis_unload_driver(arg)
1398 void *arg;
1399{
1400 struct ndis_softc *sc;
1401 device_object *fdo;
1402
1403 sc = arg;
1404
1405 if (sc->ndis_intrhand)
1406 bus_teardown_intr(sc->ndis_dev,
1407 sc->ndis_irq, sc->ndis_intrhand);
1408
1409 if (sc->ndis_block->nmb_rlist != NULL)
1410 free(sc->ndis_block->nmb_rlist, M_DEVBUF);
1411
1412 ndis_flush_sysctls(sc);
1413
1414 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1415
1416 if (sc->ndis_chars->nmc_transferdata_func != NULL)
1417 NdisFreePacketPool(sc->ndis_block->nmb_rxpool);
1418 fdo = sc->ndis_block->nmb_deviceobj;
1419 IoFreeWorkItem(sc->ndis_block->nmb_returnitem);
1420 IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj);
1421 IoDeleteDevice(fdo);
1422
1423 return (0);
1424}
449static void
450ndis_return(dobj, arg)
451 device_object *dobj;
452 void *arg;
453{
454 ndis_miniport_block *block;
455 ndis_miniport_characteristics *ch;
456 ndis_return_handler returnfunc;
457 ndis_handle adapter;
458 ndis_packet *p;
459 uint8_t irql;
460 list_entry *l;
461
462 block = arg;
463 ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
464
465 p = arg;
466 adapter = block->nmb_miniportadapterctx;
467
468 if (adapter == NULL)
469 return;
470
471 returnfunc = ch->nmc_return_packet_func;
472
473 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
474 while (!IsListEmpty(&block->nmb_returnlist)) {
475 l = RemoveHeadList((&block->nmb_returnlist));
476 p = CONTAINING_RECORD(l, ndis_packet, np_list);
477 InitializeListHead((&p->np_list));
478 KeReleaseSpinLock(&block->nmb_returnlock, irql);
479 MSCALL2(returnfunc, adapter, p);
480 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
481 }
482 KeReleaseSpinLock(&block->nmb_returnlock, irql);
483}
484
485void
486ndis_return_packet(buf, arg)
487 void *buf; /* not used */
488 void *arg;
489{
490 ndis_packet *p;
491 ndis_miniport_block *block;
492
493 if (arg == NULL)
494 return;
495
496 p = arg;
497
498 /* Decrement refcount. */
499 p->np_refcnt--;
500
501 /* Release packet when refcount hits zero, otherwise return. */
502 if (p->np_refcnt)
503 return;
504
505 block = ((struct ndis_softc *)p->np_softc)->ndis_block;
506
507 KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
508 InitializeListHead((&p->np_list));
509 InsertHeadList((&block->nmb_returnlist), (&p->np_list));
510 KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
511
512 IoQueueWorkItem(block->nmb_returnitem,
513 (io_workitem_func)kernndis_functbl[7].ipt_wrap,
514 WORKQUEUE_CRITICAL, block);
515}
516
517void
518ndis_free_bufs(b0)
519 ndis_buffer *b0;
520{
521 ndis_buffer *next;
522
523 if (b0 == NULL)
524 return;
525
526 while(b0 != NULL) {
527 next = b0->mdl_next;
528 IoFreeMdl(b0);
529 b0 = next;
530 }
531}
532
533void
534ndis_free_packet(p)
535 ndis_packet *p;
536{
537 if (p == NULL)
538 return;
539
540 ndis_free_bufs(p->np_private.npp_head);
541 NdisFreePacket(p);
542}
543
544int
545ndis_convert_res(arg)
546 void *arg;
547{
548 struct ndis_softc *sc;
549 ndis_resource_list *rl = NULL;
550 cm_partial_resource_desc *prd = NULL;
551 ndis_miniport_block *block;
552 device_t dev;
553 struct resource_list *brl;
554 struct resource_list_entry *brle;
555 int error = 0;
556
557 sc = arg;
558 block = sc->ndis_block;
559 dev = sc->ndis_dev;
560
561 rl = malloc(sizeof(ndis_resource_list) +
562 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
563 M_DEVBUF, M_NOWAIT|M_ZERO);
564
565 if (rl == NULL)
566 return (ENOMEM);
567
568 rl->cprl_version = 5;
569 rl->cprl_version = 1;
570 rl->cprl_count = sc->ndis_rescnt;
571 prd = rl->cprl_partial_descs;
572
573 brl = BUS_GET_RESOURCE_LIST(dev, dev);
574
575 if (brl != NULL) {
576
577 STAILQ_FOREACH(brle, brl, link) {
578 switch (brle->type) {
579 case SYS_RES_IOPORT:
580 prd->cprd_type = CmResourceTypePort;
581 prd->cprd_flags = CM_RESOURCE_PORT_IO;
582 prd->cprd_sharedisp =
583 CmResourceShareDeviceExclusive;
584 prd->u.cprd_port.cprd_start.np_quad =
585 brle->start;
586 prd->u.cprd_port.cprd_len = brle->count;
587 break;
588 case SYS_RES_MEMORY:
589 prd->cprd_type = CmResourceTypeMemory;
590 prd->cprd_flags =
591 CM_RESOURCE_MEMORY_READ_WRITE;
592 prd->cprd_sharedisp =
593 CmResourceShareDeviceExclusive;
594 prd->u.cprd_port.cprd_start.np_quad =
595 brle->start;
596 prd->u.cprd_port.cprd_len = brle->count;
597 break;
598 case SYS_RES_IRQ:
599 prd->cprd_type = CmResourceTypeInterrupt;
600 prd->cprd_flags = 0;
601 /*
602 * Always mark interrupt resources as
603 * shared, since in our implementation,
604 * they will be.
605 */
606 prd->cprd_sharedisp =
607 CmResourceShareShared;
608 prd->u.cprd_intr.cprd_level = brle->start;
609 prd->u.cprd_intr.cprd_vector = brle->start;
610 prd->u.cprd_intr.cprd_affinity = 0;
611 break;
612 default:
613 break;
614 }
615 prd++;
616 }
617 }
618
619 block->nmb_rlist = rl;
620
621 return (error);
622}
623
624/*
625 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
626 * packet, it will hand it to us in the form of an ndis_packet,
627 * which we need to convert to an mbuf that is then handed off
628 * to the stack. Note: we configure the mbuf list so that it uses
629 * the memory regions specified by the ndis_buffer structures in
630 * the ndis_packet as external storage. In most cases, this will
631 * point to a memory region allocated by the driver (either by
632 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
633 * the driver to handle free()ing this region for is, so we set up
634 * a dummy no-op free handler for it.
635 */
636
637int
638ndis_ptom(m0, p)
639 struct mbuf **m0;
640 ndis_packet *p;
641{
642 struct mbuf *m = NULL, *prev = NULL;
643 ndis_buffer *buf;
644 ndis_packet_private *priv;
645 uint32_t totlen = 0;
646 struct ifnet *ifp;
647 struct ether_header *eh;
648 int diff;
649
650 if (p == NULL || m0 == NULL)
651 return (EINVAL);
652
653 priv = &p->np_private;
654 buf = priv->npp_head;
655 p->np_refcnt = 0;
656
657 for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
658 if (buf == priv->npp_head)
659#ifdef MT_HEADER
660 MGETHDR(m, M_DONTWAIT, MT_HEADER);
661#else
662 MGETHDR(m, M_DONTWAIT, MT_DATA);
663#endif
664 else
665 MGET(m, M_DONTWAIT, MT_DATA);
666 if (m == NULL) {
667 m_freem(*m0);
668 *m0 = NULL;
669 return (ENOBUFS);
670 }
671 m->m_len = MmGetMdlByteCount(buf);
672 m->m_data = MmGetMdlVirtualAddress(buf);
673 MEXTADD(m, m->m_data, m->m_len, ndis_return_packet,
674 m->m_data, p, 0, EXT_NDIS);
675 p->np_refcnt++;
676
677 totlen += m->m_len;
678 if (m->m_flags & M_PKTHDR)
679 *m0 = m;
680 else
681 prev->m_next = m;
682 prev = m;
683 }
684
685 /*
686 * This is a hack to deal with the Marvell 8335 driver
687 * which, when associated with an AP in WPA-PSK mode,
688 * seems to overpad its frames by 8 bytes. I don't know
689 * that the extra 8 bytes are for, and they're not there
690 * in open mode, so for now clamp the frame size at 1514
691 * until I can figure out how to deal with this properly,
692 * otherwise if_ethersubr() will spank us by discarding
693 * the 'oversize' frames.
694 */
695
696 eh = mtod((*m0), struct ether_header *);
697 ifp = ((struct ndis_softc *)p->np_softc)->ifp;
698 if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
699 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
700 totlen -= diff;
701 m->m_len -= diff;
702 }
703 (*m0)->m_pkthdr.len = totlen;
704
705 return (0);
706}
707
708/*
709 * Create an NDIS packet from an mbuf chain.
710 * This is used mainly when transmitting packets, where we need
711 * to turn an mbuf off an interface's send queue and transform it
712 * into an NDIS packet which will be fed into the NDIS driver's
713 * send routine.
714 *
715 * NDIS packets consist of two parts: an ndis_packet structure,
716 * which is vaguely analagous to the pkthdr portion of an mbuf,
717 * and one or more ndis_buffer structures, which define the
718 * actual memory segments in which the packet data resides.
719 * We need to allocate one ndis_buffer for each mbuf in a chain,
720 * plus one ndis_packet as the header.
721 */
722
723int
724ndis_mtop(m0, p)
725 struct mbuf *m0;
726 ndis_packet **p;
727{
728 struct mbuf *m;
729 ndis_buffer *buf = NULL, *prev = NULL;
730 ndis_packet_private *priv;
731
732 if (p == NULL || *p == NULL || m0 == NULL)
733 return (EINVAL);
734
735 priv = &(*p)->np_private;
736 priv->npp_totlen = m0->m_pkthdr.len;
737
738 for (m = m0; m != NULL; m = m->m_next) {
739 if (m->m_len == 0)
740 continue;
741 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
742 if (buf == NULL) {
743 ndis_free_packet(*p);
744 *p = NULL;
745 return (ENOMEM);
746 }
747 MmBuildMdlForNonPagedPool(buf);
748
749 if (priv->npp_head == NULL)
750 priv->npp_head = buf;
751 else
752 prev->mdl_next = buf;
753 prev = buf;
754 }
755
756 priv->npp_tail = buf;
757
758 return (0);
759}
760
761int
762ndis_get_supported_oids(arg, oids, oidcnt)
763 void *arg;
764 ndis_oid **oids;
765 int *oidcnt;
766{
767 int len, rval;
768 ndis_oid *o;
769
770 if (arg == NULL || oids == NULL || oidcnt == NULL)
771 return (EINVAL);
772 len = 0;
773 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
774
775 o = malloc(len, M_DEVBUF, M_NOWAIT);
776 if (o == NULL)
777 return (ENOMEM);
778
779 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
780
781 if (rval) {
782 free(o, M_DEVBUF);
783 return (rval);
784 }
785
786 *oids = o;
787 *oidcnt = len / 4;
788
789 return (0);
790}
791
792int
793ndis_set_info(arg, oid, buf, buflen)
794 void *arg;
795 ndis_oid oid;
796 void *buf;
797 int *buflen;
798{
799 struct ndis_softc *sc;
800 ndis_status rval;
801 ndis_handle adapter;
802 ndis_setinfo_handler setfunc;
803 uint32_t byteswritten = 0, bytesneeded = 0;
804 uint8_t irql;
805 uint64_t duetime;
806
807 /*
808 * According to the NDIS spec, MiniportQueryInformation()
809 * and MiniportSetInformation() requests are handled serially:
810 * once one request has been issued, we must wait for it to
811 * finish before allowing another request to proceed.
812 */
813
814 sc = arg;
815
816 KeResetEvent(&sc->ndis_block->nmb_setevent);
817
818 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
819
820 if (sc->ndis_block->nmb_pendingreq != NULL) {
821 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
822 panic("ndis_set_info() called while other request pending");
823 } else
824 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
825
826 setfunc = sc->ndis_chars->nmc_setinfo_func;
827 adapter = sc->ndis_block->nmb_miniportadapterctx;
828
829 if (adapter == NULL || setfunc == NULL ||
830 sc->ndis_block->nmb_devicectx == NULL) {
831 sc->ndis_block->nmb_pendingreq = NULL;
832 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
833 return (ENXIO);
834 }
835
836 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
837 &byteswritten, &bytesneeded);
838
839 sc->ndis_block->nmb_pendingreq = NULL;
840
841 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
842
843 if (rval == NDIS_STATUS_PENDING) {
844 /* Wait up to 5 seconds. */
845 duetime = (5 * 1000000) * -10;
846 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
847 0, 0, FALSE, &duetime);
848 rval = sc->ndis_block->nmb_setstat;
849 }
850
851 if (byteswritten)
852 *buflen = byteswritten;
853 if (bytesneeded)
854 *buflen = bytesneeded;
855
856 if (rval == NDIS_STATUS_INVALID_LENGTH)
857 return (ENOSPC);
858
859 if (rval == NDIS_STATUS_INVALID_OID)
860 return (EINVAL);
861
862 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
863 rval == NDIS_STATUS_NOT_ACCEPTED)
864 return (ENOTSUP);
865
866 if (rval != NDIS_STATUS_SUCCESS)
867 return (ENODEV);
868
869 return (0);
870}
871
872typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
873
874int
875ndis_send_packets(arg, packets, cnt)
876 void *arg;
877 ndis_packet **packets;
878 int cnt;
879{
880 struct ndis_softc *sc;
881 ndis_handle adapter;
882 ndis_sendmulti_handler sendfunc;
883 ndis_senddone_func senddonefunc;
884 int i;
885 ndis_packet *p;
886 uint8_t irql = 0;
887
888 sc = arg;
889 adapter = sc->ndis_block->nmb_miniportadapterctx;
890 if (adapter == NULL)
891 return (ENXIO);
892 sendfunc = sc->ndis_chars->nmc_sendmulti_func;
893 senddonefunc = sc->ndis_block->nmb_senddone_func;
894
895 if (NDIS_SERIALIZED(sc->ndis_block))
896 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
897
898 MSCALL3(sendfunc, adapter, packets, cnt);
899
900 for (i = 0; i < cnt; i++) {
901 p = packets[i];
902 /*
903 * Either the driver already handed the packet to
904 * ndis_txeof() due to a failure, or it wants to keep
905 * it and release it asynchronously later. Skip to the
906 * next one.
907 */
908 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
909 continue;
910 MSCALL3(senddonefunc, sc->ndis_block, p, p->np_oob.npo_status);
911 }
912
913 if (NDIS_SERIALIZED(sc->ndis_block))
914 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
915
916 return (0);
917}
918
919int
920ndis_send_packet(arg, packet)
921 void *arg;
922 ndis_packet *packet;
923{
924 struct ndis_softc *sc;
925 ndis_handle adapter;
926 ndis_status status;
927 ndis_sendsingle_handler sendfunc;
928 ndis_senddone_func senddonefunc;
929 uint8_t irql = 0;
930
931 sc = arg;
932 adapter = sc->ndis_block->nmb_miniportadapterctx;
933 if (adapter == NULL)
934 return (ENXIO);
935 sendfunc = sc->ndis_chars->nmc_sendsingle_func;
936 senddonefunc = sc->ndis_block->nmb_senddone_func;
937
938 if (NDIS_SERIALIZED(sc->ndis_block))
939 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
940 status = MSCALL3(sendfunc, adapter, packet,
941 packet->np_private.npp_flags);
942
943 if (status == NDIS_STATUS_PENDING) {
944 if (NDIS_SERIALIZED(sc->ndis_block))
945 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
946 return (0);
947 }
948
949 MSCALL3(senddonefunc, sc->ndis_block, packet, status);
950
951 if (NDIS_SERIALIZED(sc->ndis_block))
952 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
953
954 return (0);
955}
956
957int
958ndis_init_dma(arg)
959 void *arg;
960{
961 struct ndis_softc *sc;
962 int i, error;
963
964 sc = arg;
965
966 sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
967 M_DEVBUF, M_NOWAIT|M_ZERO);
968
969 if (sc->ndis_tmaps == NULL)
970 return (ENOMEM);
971
972 for (i = 0; i < sc->ndis_maxpkts; i++) {
973 error = bus_dmamap_create(sc->ndis_ttag, 0,
974 &sc->ndis_tmaps[i]);
975 if (error) {
976 free(sc->ndis_tmaps, M_DEVBUF);
977 return (ENODEV);
978 }
979 }
980
981 return (0);
982}
983
984int
985ndis_destroy_dma(arg)
986 void *arg;
987{
988 struct ndis_softc *sc;
989 struct mbuf *m;
990 ndis_packet *p = NULL;
991 int i;
992
993 sc = arg;
994
995 for (i = 0; i < sc->ndis_maxpkts; i++) {
996 if (sc->ndis_txarray[i] != NULL) {
997 p = sc->ndis_txarray[i];
998 m = (struct mbuf *)p->np_rsvd[1];
999 if (m != NULL)
1000 m_freem(m);
1001 ndis_free_packet(sc->ndis_txarray[i]);
1002 }
1003 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
1004 }
1005
1006 free(sc->ndis_tmaps, M_DEVBUF);
1007
1008 bus_dma_tag_destroy(sc->ndis_ttag);
1009
1010 return (0);
1011}
1012
1013int
1014ndis_reset_nic(arg)
1015 void *arg;
1016{
1017 struct ndis_softc *sc;
1018 ndis_handle adapter;
1019 ndis_reset_handler resetfunc;
1020 uint8_t addressing_reset;
1021 int rval;
1022 uint8_t irql = 0;
1023
1024 sc = arg;
1025
1026 NDIS_LOCK(sc);
1027 adapter = sc->ndis_block->nmb_miniportadapterctx;
1028 resetfunc = sc->ndis_chars->nmc_reset_func;
1029
1030 if (adapter == NULL || resetfunc == NULL ||
1031 sc->ndis_block->nmb_devicectx == NULL) {
1032 NDIS_UNLOCK(sc);
1033 return (EIO);
1034 }
1035
1036 NDIS_UNLOCK(sc);
1037
1038 KeResetEvent(&sc->ndis_block->nmb_resetevent);
1039
1040 if (NDIS_SERIALIZED(sc->ndis_block))
1041 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1042
1043 rval = MSCALL2(resetfunc, &addressing_reset, adapter);
1044
1045 if (NDIS_SERIALIZED(sc->ndis_block))
1046 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1047
1048 if (rval == NDIS_STATUS_PENDING)
1049 KeWaitForSingleObject(&sc->ndis_block->nmb_resetevent,
1050 0, 0, FALSE, NULL);
1051
1052 return (0);
1053}
1054
1055int
1056ndis_halt_nic(arg)
1057 void *arg;
1058{
1059 struct ndis_softc *sc;
1060 ndis_handle adapter;
1061 ndis_halt_handler haltfunc;
1062 ndis_miniport_block *block;
1063 int empty = 0;
1064 uint8_t irql;
1065
1066 sc = arg;
1067 block = sc->ndis_block;
1068
1069 if (!cold)
1070 KeFlushQueuedDpcs();
1071
1072 /*
1073 * Wait for all packets to be returned.
1074 */
1075
1076 while (1) {
1077 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
1078 empty = IsListEmpty(&block->nmb_returnlist);
1079 KeReleaseSpinLock(&block->nmb_returnlock, irql);
1080 if (empty)
1081 break;
1082 NdisMSleep(1000);
1083 }
1084
1085 NDIS_LOCK(sc);
1086 adapter = sc->ndis_block->nmb_miniportadapterctx;
1087 if (adapter == NULL) {
1088 NDIS_UNLOCK(sc);
1089 return (EIO);
1090 }
1091
1092 sc->ndis_block->nmb_devicectx = NULL;
1093
1094 /*
1095 * The adapter context is only valid after the init
1096 * handler has been called, and is invalid once the
1097 * halt handler has been called.
1098 */
1099
1100 haltfunc = sc->ndis_chars->nmc_halt_func;
1101 NDIS_UNLOCK(sc);
1102
1103 MSCALL1(haltfunc, adapter);
1104
1105 NDIS_LOCK(sc);
1106 sc->ndis_block->nmb_miniportadapterctx = NULL;
1107 NDIS_UNLOCK(sc);
1108
1109 return (0);
1110}
1111
1112int
1113ndis_shutdown_nic(arg)
1114 void *arg;
1115{
1116 struct ndis_softc *sc;
1117 ndis_handle adapter;
1118 ndis_shutdown_handler shutdownfunc;
1119
1120 sc = arg;
1121 NDIS_LOCK(sc);
1122 adapter = sc->ndis_block->nmb_miniportadapterctx;
1123 shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1124 NDIS_UNLOCK(sc);
1125 if (adapter == NULL || shutdownfunc == NULL)
1126 return (EIO);
1127
1128 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1129 MSCALL1(shutdownfunc, adapter);
1130 else
1131 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
1132
1133 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1134
1135 return (0);
1136}
1137
1138int
1139ndis_pnpevent_nic(arg, type)
1140 void *arg;
1141 int type;
1142{
1143 device_t dev;
1144 struct ndis_softc *sc;
1145 ndis_handle adapter;
1146 ndis_pnpevent_handler pnpeventfunc;
1147
1148 dev = arg;
1149 sc = device_get_softc(arg);
1150 NDIS_LOCK(sc);
1151 adapter = sc->ndis_block->nmb_miniportadapterctx;
1152 pnpeventfunc = sc->ndis_chars->nmc_pnpevent_handler;
1153 NDIS_UNLOCK(sc);
1154 if (adapter == NULL || pnpeventfunc == NULL)
1155 return (EIO);
1156
1157 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1158 MSCALL4(pnpeventfunc, adapter, type, NULL, 0);
1159 else
1160 MSCALL4(pnpeventfunc, sc->ndis_chars->nmc_rsvd0, type, NULL, 0);
1161
1162 return (0);
1163}
1164
1165int
1166ndis_init_nic(arg)
1167 void *arg;
1168{
1169 struct ndis_softc *sc;
1170 ndis_miniport_block *block;
1171 ndis_init_handler initfunc;
1172 ndis_status status, openstatus = 0;
1173 ndis_medium mediumarray[NdisMediumMax];
1174 uint32_t chosenmedium, i;
1175
1176 if (arg == NULL)
1177 return (EINVAL);
1178
1179 sc = arg;
1180 NDIS_LOCK(sc);
1181 block = sc->ndis_block;
1182 initfunc = sc->ndis_chars->nmc_init_func;
1183 NDIS_UNLOCK(sc);
1184
1185 sc->ndis_block->nmb_timerlist = NULL;
1186
1187 for (i = 0; i < NdisMediumMax; i++)
1188 mediumarray[i] = i;
1189
1190 status = MSCALL6(initfunc, &openstatus, &chosenmedium,
1191 mediumarray, NdisMediumMax, block, block);
1192
1193 /*
1194 * If the init fails, blow away the other exported routines
1195 * we obtained from the driver so we can't call them later.
1196 * If the init failed, none of these will work.
1197 */
1198 if (status != NDIS_STATUS_SUCCESS) {
1199 NDIS_LOCK(sc);
1200 sc->ndis_block->nmb_miniportadapterctx = NULL;
1201 NDIS_UNLOCK(sc);
1202 return (ENXIO);
1203 }
1204
1205 /*
1206 * This may look really goofy, but apparently it is possible
1207 * to halt a miniport too soon after it's been initialized.
1208 * After MiniportInitialize() finishes, pause for 1 second
1209 * to give the chip a chance to handle any short-lived timers
1210 * that were set in motion. If we call MiniportHalt() too soon,
1211 * some of the timers may not be cancelled, because the driver
1212 * expects them to fire before the halt is called.
1213 */
1214
1215 pause("ndwait", hz);
1216
1217 NDIS_LOCK(sc);
1218 sc->ndis_block->nmb_devicectx = sc;
1219 NDIS_UNLOCK(sc);
1220
1221 return (0);
1222}
1223
1224static void
1225ndis_intrsetup(dpc, dobj, ip, sc)
1226 kdpc *dpc;
1227 device_object *dobj;
1228 irp *ip;
1229 struct ndis_softc *sc;
1230{
1231 ndis_miniport_interrupt *intr;
1232
1233 intr = sc->ndis_block->nmb_interrupt;
1234
1235 /* Sanity check. */
1236
1237 if (intr == NULL)
1238 return;
1239
1240 KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
1241 KeResetEvent(&intr->ni_dpcevt);
1242 if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
1243 intr->ni_dpccnt++;
1244 KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
1245}
1246
1247int
1248ndis_get_info(arg, oid, buf, buflen)
1249 void *arg;
1250 ndis_oid oid;
1251 void *buf;
1252 int *buflen;
1253{
1254 struct ndis_softc *sc;
1255 ndis_status rval;
1256 ndis_handle adapter;
1257 ndis_queryinfo_handler queryfunc;
1258 uint32_t byteswritten = 0, bytesneeded = 0;
1259 uint8_t irql;
1260 uint64_t duetime;
1261
1262 sc = arg;
1263
1264 KeResetEvent(&sc->ndis_block->nmb_getevent);
1265
1266 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1267
1268 if (sc->ndis_block->nmb_pendingreq != NULL) {
1269 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1270 panic("ndis_get_info() called while other request pending");
1271 } else
1272 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
1273
1274 queryfunc = sc->ndis_chars->nmc_queryinfo_func;
1275 adapter = sc->ndis_block->nmb_miniportadapterctx;
1276
1277 if (adapter == NULL || queryfunc == NULL ||
1278 sc->ndis_block->nmb_devicectx == NULL) {
1279 sc->ndis_block->nmb_pendingreq = NULL;
1280 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1281 return (ENXIO);
1282 }
1283
1284 rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
1285 &byteswritten, &bytesneeded);
1286
1287 sc->ndis_block->nmb_pendingreq = NULL;
1288
1289 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1290
1291 /* Wait for requests that block. */
1292
1293 if (rval == NDIS_STATUS_PENDING) {
1294 /* Wait up to 5 seconds. */
1295 duetime = (5 * 1000000) * -10;
1296 KeWaitForSingleObject(&sc->ndis_block->nmb_getevent,
1297 0, 0, FALSE, &duetime);
1298 rval = sc->ndis_block->nmb_getstat;
1299 }
1300
1301 if (byteswritten)
1302 *buflen = byteswritten;
1303 if (bytesneeded)
1304 *buflen = bytesneeded;
1305
1306 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1307 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1308 return (ENOSPC);
1309
1310 if (rval == NDIS_STATUS_INVALID_OID)
1311 return (EINVAL);
1312
1313 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1314 rval == NDIS_STATUS_NOT_ACCEPTED)
1315 return (ENOTSUP);
1316
1317 if (rval != NDIS_STATUS_SUCCESS)
1318 return (ENODEV);
1319
1320 return (0);
1321}
1322
1323uint32_t
1324NdisAddDevice(drv, pdo)
1325 driver_object *drv;
1326 device_object *pdo;
1327{
1328 device_object *fdo;
1329 ndis_miniport_block *block;
1330 struct ndis_softc *sc;
1331 uint32_t status;
1332 int error;
1333
1334 sc = device_get_softc(pdo->do_devext);
1335
1336 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
1337 error = bus_setup_intr(sc->ndis_dev, sc->ndis_irq,
1338 INTR_TYPE_NET | INTR_MPSAFE,
1339 NULL, ntoskrnl_intr, NULL, &sc->ndis_intrhand);
1340 if (error)
1341 return (NDIS_STATUS_FAILURE);
1342 }
1343
1344 status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1345 FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
1346
1347 if (status != STATUS_SUCCESS)
1348 return (status);
1349
1350 block = fdo->do_devext;
1351
1352 block->nmb_filterdbs.nf_ethdb = block;
1353 block->nmb_deviceobj = fdo;
1354 block->nmb_physdeviceobj = pdo;
1355 block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
1356 KeInitializeSpinLock(&block->nmb_lock);
1357 KeInitializeSpinLock(&block->nmb_returnlock);
1358 KeInitializeEvent(&block->nmb_getevent, EVENT_TYPE_NOTIFY, TRUE);
1359 KeInitializeEvent(&block->nmb_setevent, EVENT_TYPE_NOTIFY, TRUE);
1360 KeInitializeEvent(&block->nmb_resetevent, EVENT_TYPE_NOTIFY, TRUE);
1361 InitializeListHead(&block->nmb_parmlist);
1362 InitializeListHead(&block->nmb_returnlist);
1363 block->nmb_returnitem = IoAllocateWorkItem(fdo);
1364
1365 /*
1366 * Stash pointers to the miniport block and miniport
1367 * characteristics info in the if_ndis softc so the
1368 * UNIX wrapper driver can get to them later.
1369 */
1370 sc->ndis_block = block;
1371 sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1);
1372
1373 /*
1374 * If the driver has a MiniportTransferData() function,
1375 * we should allocate a private RX packet pool.
1376 */
1377
1378 if (sc->ndis_chars->nmc_transferdata_func != NULL) {
1379 NdisAllocatePacketPool(&status, &block->nmb_rxpool,
1380 32, PROTOCOL_RESERVED_SIZE_IN_PACKET);
1381 if (status != NDIS_STATUS_SUCCESS) {
1382 IoDetachDevice(block->nmb_nextdeviceobj);
1383 IoDeleteDevice(fdo);
1384 return (status);
1385 }
1386 InitializeListHead((&block->nmb_packetlist));
1387 }
1388
1389 /* Give interrupt handling priority over timers. */
1390 IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1391 KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
1392
1393 /* Finish up BSD-specific setup. */
1394
1395 block->nmb_signature = (void *)0xcafebabe;
1396 block->nmb_status_func = kernndis_functbl[0].ipt_wrap;
1397 block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap;
1398 block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap;
1399 block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap;
1400 block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap;
1401 block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap;
1402 block->nmb_pendingreq = NULL;
1403
1404 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
1405
1406 return (STATUS_SUCCESS);
1407}
1408
1409int
1410ndis_unload_driver(arg)
1411 void *arg;
1412{
1413 struct ndis_softc *sc;
1414 device_object *fdo;
1415
1416 sc = arg;
1417
1418 if (sc->ndis_intrhand)
1419 bus_teardown_intr(sc->ndis_dev,
1420 sc->ndis_irq, sc->ndis_intrhand);
1421
1422 if (sc->ndis_block->nmb_rlist != NULL)
1423 free(sc->ndis_block->nmb_rlist, M_DEVBUF);
1424
1425 ndis_flush_sysctls(sc);
1426
1427 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1428
1429 if (sc->ndis_chars->nmc_transferdata_func != NULL)
1430 NdisFreePacketPool(sc->ndis_block->nmb_rxpool);
1431 fdo = sc->ndis_block->nmb_deviceobj;
1432 IoFreeWorkItem(sc->ndis_block->nmb_returnitem);
1433 IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj);
1434 IoDeleteDevice(fdo);
1435
1436 return (0);
1437}