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