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