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