3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
32 * $FreeBSD: src/sys/compat/ndis/kern_ndis.c,v 1.111 2011/02/23 21:45:28 brucec Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/unistd.h>
38 #include <sys/types.h>
39 #include <sys/errno.h>
40 #include <sys/callout.h>
41 #include <sys/socket.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
45 #include <sys/malloc.h>
49 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/kthread.h>
56 #include <net/if_arp.h>
57 #include <net/ethernet.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
61 #include <netproto/802_11/ieee80211_var.h>
62 #include <netproto/802_11/ieee80211_ioctl.h>
64 #include <bus/u4b/usb.h>
65 #include <bus/u4b/usbdi.h>
67 #include <emulation/ndis/pe_var.h>
68 #include <emulation/ndis/cfg_var.h>
69 #include <emulation/ndis/resource_var.h>
70 #include <emulation/ndis/ntoskrnl_var.h>
71 #include <emulation/ndis/ndis_var.h>
72 #include <emulation/ndis/hal_var.h>
73 #include <emulation/ndis/u4bd_var.h>
74 #include <dev/netif/ndis/if_ndisvar.h>
76 #define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
78 static void ndis_status_func(ndis_handle
, ndis_status
, void *, uint32_t);
79 static void ndis_statusdone_func(ndis_handle
);
80 static void ndis_setdone_func(ndis_handle
, ndis_status
);
81 static void ndis_getdone_func(ndis_handle
, ndis_status
);
82 static void ndis_resetdone_func(ndis_handle
, ndis_status
, uint8_t);
83 static void ndis_sendrsrcavail_func(ndis_handle
);
84 static void ndis_intrsetup(kdpc
*, device_object
*,
85 irp
*, struct ndis_softc
*);
86 static void ndis_return(device_object
*, void *);
88 static image_patch_table kernndis_functbl
[] = {
89 IMPORT_SFUNC(ndis_status_func
, 4),
90 IMPORT_SFUNC(ndis_statusdone_func
, 1),
91 IMPORT_SFUNC(ndis_setdone_func
, 2),
92 IMPORT_SFUNC(ndis_getdone_func
, 2),
93 IMPORT_SFUNC(ndis_resetdone_func
, 3),
94 IMPORT_SFUNC(ndis_sendrsrcavail_func
, 1),
95 IMPORT_SFUNC(ndis_intrsetup
, 4),
96 IMPORT_SFUNC(ndis_return
, 1),
101 static struct nd_head ndis_devhead
;
104 * This allows us to export our symbols to other modules.
106 * Note: some of the subsystems depend on each other, so the
107 * order in which they're started is important. The order of
110 * HAL - spinlocks and IRQL manipulation
111 * ntoskrnl - DPC and workitem threads, object waiting
112 * windrv - driver/device registration
114 * The HAL should also be the last thing shut down, since
115 * the ntoskrnl subsystem will use spinlocks right up until
116 * the DPC and workitem threads are terminated.
120 ndis_modevent(module_t mod
, int cmd
, void *arg
)
123 image_patch_table
*patch
;
127 /* Initialize subsystems */
134 patch
= kernndis_functbl
;
135 while (patch
->ipt_func
!= NULL
) {
136 windrv_wrap((funcptr
)patch
->ipt_func
,
137 (funcptr
*)&patch
->ipt_wrap
,
138 patch
->ipt_argcnt
, patch
->ipt_ftype
);
142 TAILQ_INIT(&ndis_devhead
);
145 if (TAILQ_FIRST(&ndis_devhead
) == NULL
) {
146 /* Shut down subsystems */
153 patch
= kernndis_functbl
;
154 while (patch
->ipt_func
!= NULL
) {
155 windrv_unwrap(patch
->ipt_wrap
);
161 /* Shut down subsystems */
168 patch
= kernndis_functbl
;
169 while (patch
->ipt_func
!= NULL
) {
170 windrv_unwrap(patch
->ipt_wrap
);
182 DEV_MODULE(ndis
, ndis_modevent
, NULL
);
183 MODULE_VERSION(ndis
, 1);
186 ndis_sendrsrcavail_func(ndis_handle adapter
)
191 ndis_status_func(ndis_handle adapter
, ndis_status status
, void *sbuf
,
194 ndis_miniport_block
*block
;
195 struct ndis_softc
*sc
;
199 sc
= device_get_softc(block
->nmb_physdeviceobj
->do_devext
);
201 if (ifp
->if_flags
& IFF_DEBUG
)
202 device_printf(sc
->ndis_dev
, "status: %x\n", status
);
206 ndis_statusdone_func(ndis_handle adapter
)
208 ndis_miniport_block
*block
;
209 struct ndis_softc
*sc
;
213 sc
= device_get_softc(block
->nmb_physdeviceobj
->do_devext
);
215 if (ifp
->if_flags
& IFF_DEBUG
)
216 device_printf(sc
->ndis_dev
, "status complete\n");
220 ndis_setdone_func(ndis_handle adapter
, ndis_status status
)
222 ndis_miniport_block
*block
;
225 block
->nmb_setstat
= status
;
226 KeSetEvent(&block
->nmb_setevent
, IO_NO_INCREMENT
, FALSE
);
230 ndis_getdone_func(ndis_handle adapter
, ndis_status status
)
232 ndis_miniport_block
*block
;
235 block
->nmb_getstat
= status
;
236 KeSetEvent(&block
->nmb_getevent
, IO_NO_INCREMENT
, FALSE
);
240 ndis_resetdone_func(ndis_handle adapter
, ndis_status status
,
241 uint8_t addressingreset
)
243 ndis_miniport_block
*block
;
244 struct ndis_softc
*sc
;
248 sc
= device_get_softc(block
->nmb_physdeviceobj
->do_devext
);
251 if (ifp
->if_flags
& IFF_DEBUG
)
252 device_printf(sc
->ndis_dev
, "reset done...\n");
253 KeSetEvent(&block
->nmb_resetevent
, IO_NO_INCREMENT
, FALSE
);
257 ndis_create_sysctls(void *arg
)
259 struct ndis_softc
*sc
;
262 struct sysctl_oid
*oidp
;
263 struct sysctl_ctx_entry
*e
;
269 vals
= sc
->ndis_regvals
;
271 TAILQ_INIT(&sc
->ndis_cfglist_head
);
273 /* Add the driver-specific registry keys. */
276 if (vals
->nc_cfgkey
== NULL
)
279 if (vals
->nc_idx
!= sc
->ndis_devidx
) {
284 /* See if we already have a sysctl with this name */
287 TAILQ_FOREACH(e
, device_get_sysctl_ctx(sc
->ndis_dev
), link
) {
289 if (strcasecmp(oidp
->oid_name
, vals
->nc_cfgkey
) == 0)
299 ndis_add_sysctl(sc
, vals
->nc_cfgkey
, vals
->nc_cfgdesc
,
300 vals
->nc_val
, CTLFLAG_RW
);
304 /* Now add a couple of builtin keys. */
307 * Environment can be either Windows (0) or WindowsNT (1).
308 * We qualify as the latter.
310 ndis_add_sysctl(sc
, "Environment",
311 "Windows environment", "1", CTLFLAG_RD
);
313 /* NDIS version should be 5.1. */
314 ndis_add_sysctl(sc
, "NdisVersion",
315 "NDIS API Version", "0x00050001", CTLFLAG_RD
);
318 * Some miniport drivers rely on the existence of the SlotNumber,
319 * NetCfgInstanceId and DriverDesc keys.
321 ndis_add_sysctl(sc
, "SlotNumber", "Slot Number", "01", CTLFLAG_RD
);
322 ndis_add_sysctl(sc
, "NetCfgInstanceId", "NetCfgInstanceId",
323 "{12345678-1234-5678-CAFE0-123456789ABC}", CTLFLAG_RD
);
324 ndis_add_sysctl(sc
, "DriverDesc", "Driver Description",
325 "NDIS Network Adapter", CTLFLAG_RD
);
327 /* Bus type (PCI, PCMCIA, etc...) */
328 ksprintf(buf
, "%d", (int)sc
->ndis_iftype
);
329 ndis_add_sysctl(sc
, "BusType", "Bus Type", buf
, CTLFLAG_RD
);
331 if (sc
->ndis_res_io
!= NULL
) {
332 ksprintf(buf
, "0x%lx", rman_get_start(sc
->ndis_res_io
));
333 ndis_add_sysctl(sc
, "IOBaseAddress",
334 "Base I/O Address", buf
, CTLFLAG_RD
);
337 if (sc
->ndis_irq
!= NULL
) {
338 ksprintf(buf
, "%lu", rman_get_start(sc
->ndis_irq
));
339 ndis_add_sysctl(sc
, "InterruptNumber",
340 "Interrupt Number", buf
, CTLFLAG_RD
);
347 ndis_add_sysctl(void *arg
, char *key
, char *desc
, char *val
, int flag
)
349 struct ndis_softc
*sc
;
350 struct ndis_cfglist
*cfg
;
355 cfg
= kmalloc(sizeof(struct ndis_cfglist
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
356 cfg
->ndis_cfg
.nc_cfgkey
= kstrdup(key
, M_DEVBUF
);
358 ksnprintf(descstr
, sizeof(descstr
), "%s (dynamic)", key
);
359 cfg
->ndis_cfg
.nc_cfgdesc
= kstrdup(descstr
, M_DEVBUF
);
361 cfg
->ndis_cfg
.nc_cfgdesc
= kstrdup(desc
, M_DEVBUF
);
362 strcpy(cfg
->ndis_cfg
.nc_val
, val
);
364 TAILQ_INSERT_TAIL(&sc
->ndis_cfglist_head
, cfg
, link
);
367 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc
->ndis_dev
),
368 SYSCTL_CHILDREN(device_get_sysctl_tree(sc
->ndis_dev
)),
369 OID_AUTO
, cfg
->ndis_cfg
.nc_cfgkey
, flag
,
370 cfg
->ndis_cfg
.nc_val
, sizeof(cfg
->ndis_cfg
.nc_val
),
371 cfg
->ndis_cfg
.nc_cfgdesc
);
377 * Somewhere, somebody decided "hey, let's automatically create
378 * a sysctl tree for each device instance as it's created -- it'll
379 * make life so much easier!" Lies. Why must they turn the kernel
380 * into a house of lies?
384 ndis_flush_sysctls(void *arg
)
386 struct ndis_softc
*sc
;
387 struct ndis_cfglist
*cfg
;
388 struct sysctl_ctx_list
*clist
;
392 clist
= device_get_sysctl_ctx(sc
->ndis_dev
);
394 while (!TAILQ_EMPTY(&sc
->ndis_cfglist_head
)) {
395 cfg
= TAILQ_FIRST(&sc
->ndis_cfglist_head
);
396 TAILQ_REMOVE(&sc
->ndis_cfglist_head
, cfg
, link
);
397 sysctl_ctx_entry_del(clist
, cfg
->ndis_oid
);
398 sysctl_remove_oid(cfg
->ndis_oid
, 1, 0);
399 kfree(cfg
->ndis_cfg
.nc_cfgkey
, M_DEVBUF
);
400 kfree(cfg
->ndis_cfg
.nc_cfgdesc
, M_DEVBUF
);
401 kfree(cfg
, M_DEVBUF
);
408 ndis_get_routine_address(struct image_patch_table
*functbl
, char *name
)
412 for (i
= 0; functbl
[i
].ipt_name
!= NULL
; i
++)
413 if (strcmp(name
, functbl
[i
].ipt_name
) == 0)
414 return (functbl
[i
].ipt_wrap
);
419 ndis_return(device_object
*dobj
, void *arg
)
421 ndis_miniport_block
*block
;
422 ndis_miniport_characteristics
*ch
;
423 ndis_return_handler returnfunc
;
430 ch
= IoGetDriverObjectExtension(dobj
->do_drvobj
, (void *)1);
433 adapter
= block
->nmb_miniportadapterctx
;
438 returnfunc
= ch
->nmc_return_packet_func
;
440 KeAcquireSpinLock(&block
->nmb_returnlock
, &irql
);
441 while (!IsListEmpty(&block
->nmb_returnlist
)) {
442 l
= RemoveHeadList((&block
->nmb_returnlist
));
443 p
= CONTAINING_RECORD(l
, ndis_packet
, np_list
);
444 InitializeListHead((&p
->np_list
));
445 KeReleaseSpinLock(&block
->nmb_returnlock
, irql
);
446 MSCALL2(returnfunc
, adapter
, p
);
447 KeAcquireSpinLock(&block
->nmb_returnlock
, &irql
);
449 KeReleaseSpinLock(&block
->nmb_returnlock
, irql
);
453 ndis_reference_packet(void *arg
)
462 /* Increment refcount. */
463 atomic_add_int(&p
->np_refcnt
, 1);
467 ndis_return_packet(void *arg
)
470 ndis_miniport_block
*block
;
477 /* Release packet when refcount hits zero, otherwise return. */
478 if (atomic_fetchadd_int(&p
->np_refcnt
, -1) > 1)
481 block
= ((struct ndis_softc
*)p
->np_softc
)->ndis_block
;
483 KeAcquireSpinLockAtDpcLevel(&block
->nmb_returnlock
);
484 InitializeListHead((&p
->np_list
));
485 InsertHeadList((&block
->nmb_returnlist
), (&p
->np_list
));
486 KeReleaseSpinLockFromDpcLevel(&block
->nmb_returnlock
);
488 IoQueueWorkItem(block
->nmb_returnitem
,
489 (io_workitem_func
)kernndis_functbl
[7].ipt_wrap
,
490 WORKQUEUE_CRITICAL
, block
);
494 ndis_free_bufs(ndis_buffer
*b0
)
509 ndis_free_packet(ndis_packet
*p
)
514 ndis_free_bufs(p
->np_private
.npp_head
);
519 ndis_convert_res(void *arg
)
521 struct ndis_softc
*sc
;
522 ndis_resource_list
*rl
= NULL
;
523 cm_partial_resource_desc
*prd
= NULL
;
524 ndis_miniport_block
*block
;
526 struct resource_list
*brl
;
527 struct resource_list_entry
*brle
;
528 struct resource_list brl_rev
;
529 struct resource_list_entry
*n
;
533 block
= sc
->ndis_block
;
536 SLIST_INIT(&brl_rev
);
538 rl
= kmalloc(sizeof(ndis_resource_list
) +
539 (sizeof(cm_partial_resource_desc
) * (sc
->ndis_rescnt
- 1)),
540 M_DEVBUF
, M_WAITOK
|M_NULLOK
|M_ZERO
);
545 rl
->cprl_version
= 5;
546 rl
->cprl_revision
= 1;
547 rl
->cprl_count
= sc
->ndis_rescnt
;
548 prd
= rl
->cprl_partial_descs
;
550 brl
= BUS_GET_RESOURCE_LIST(dev
, dev
);
555 * We have a small problem. Some PCI devices have
556 * multiple I/O ranges. Windows orders them starting
557 * from lowest numbered BAR to highest. We discover
558 * them in that order too, but insert them into a singly
559 * linked list head first, which means when time comes
560 * to traverse the list, we enumerate them in reverse
561 * order. This screws up some drivers which expect the
562 * BARs to be in ascending order so that they can choose
563 * the "first" one as their register space. Unfortunately,
564 * in order to fix this, we have to create our own
565 * temporary list with the entries in reverse order.
568 SLIST_FOREACH(brle
, brl
, link
) {
569 n
= kmalloc(sizeof(struct resource_list_entry
),
570 M_TEMP
, M_WAITOK
|M_NULLOK
);
575 bcopy((char *)brle
, (char *)n
,
576 sizeof(struct resource_list_entry
));
577 SLIST_INSERT_HEAD(&brl_rev
, n
, link
);
580 SLIST_FOREACH(brle
, &brl_rev
, link
) {
581 switch (brle
->type
) {
583 prd
->cprd_type
= CmResourceTypePort
;
584 prd
->cprd_flags
= CM_RESOURCE_PORT_IO
;
585 prd
->cprd_sharedisp
=
586 CmResourceShareDeviceExclusive
;
587 prd
->u
.cprd_port
.cprd_start
.np_quad
=
589 prd
->u
.cprd_port
.cprd_len
= brle
->count
;
592 prd
->cprd_type
= CmResourceTypeMemory
;
594 CM_RESOURCE_MEMORY_READ_WRITE
;
595 prd
->cprd_sharedisp
=
596 CmResourceShareDeviceExclusive
;
597 prd
->u
.cprd_mem
.cprd_start
.np_quad
=
599 prd
->u
.cprd_mem
.cprd_len
= brle
->count
;
602 prd
->cprd_type
= CmResourceTypeInterrupt
;
605 * Always mark interrupt resources as
606 * shared, since in our implementation,
609 prd
->cprd_sharedisp
=
610 CmResourceShareShared
;
611 prd
->u
.cprd_intr
.cprd_level
= brle
->start
;
612 prd
->u
.cprd_intr
.cprd_vector
= brle
->start
;
613 prd
->u
.cprd_intr
.cprd_affinity
= 0;
622 block
->nmb_rlist
= rl
;
626 while (!SLIST_EMPTY(&brl_rev
)) {
627 n
= SLIST_FIRST(&brl_rev
);
628 SLIST_REMOVE_HEAD(&brl_rev
, link
);
636 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
637 * packet, it will hand it to us in the form of an ndis_packet,
638 * which we need to convert to an mbuf that is then handed off
639 * to the stack. Note: we configure the mbuf list so that it uses
640 * the memory regions specified by the ndis_buffer structures in
641 * the ndis_packet as external storage. In most cases, this will
642 * point to a memory region allocated by the driver (either by
643 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
644 * the driver to handle kfree()ing this region for is, so we set up
645 * a dummy no-op free handler for it.
649 ndis_ptom(struct mbuf
**m0
, ndis_packet
*p
)
651 struct mbuf
*m
= NULL
, *prev
= NULL
;
653 ndis_packet_private
*priv
;
656 struct ether_header
*eh
;
659 if (p
== NULL
|| m0
== NULL
)
662 priv
= &p
->np_private
;
663 buf
= priv
->npp_head
;
666 for (buf
= priv
->npp_head
; buf
!= NULL
; buf
= buf
->mdl_next
) {
667 if (buf
== priv
->npp_head
) {
668 /* XXX swildner: why not MT_HEADER? (see FreeBSD) */
669 MGETHDR(m
, M_NOWAIT
, MT_DATA
);
671 MGET(m
, M_NOWAIT
, MT_DATA
);
678 m
->m_len
= MmGetMdlByteCount(buf
);
679 m
->m_data
= MmGetMdlVirtualAddress(buf
);
680 m_extadd(m
, m
->m_data
, m
->m_len
, ndis_reference_packet
,
681 ndis_return_packet
, p
);
685 if (m
->m_flags
& M_PKTHDR
)
693 * This is a hack to deal with the Marvell 8335 driver
694 * which, when associated with an AP in WPA-PSK mode,
695 * seems to overpad its frames by 8 bytes. I don't know
696 * that the extra 8 bytes are for, and they're not there
697 * in open mode, so for now clamp the frame size at 1514
698 * until I can figure out how to deal with this properly,
699 * otherwise if_ethersubr() will spank us by discarding
700 * the 'oversize' frames.
703 eh
= mtod((*m0
), struct ether_header
*);
704 ifp
= ((struct ndis_softc
*)p
->np_softc
)->ifp
;
705 if (totlen
> ETHER_MAX_FRAME(ifp
, eh
->ether_type
, FALSE
)) {
706 diff
= totlen
- ETHER_MAX_FRAME(ifp
, eh
->ether_type
, FALSE
);
710 (*m0
)->m_pkthdr
.len
= totlen
;
716 * Create an NDIS packet from an mbuf chain.
717 * This is used mainly when transmitting packets, where we need
718 * to turn an mbuf off an interface's send queue and transform it
719 * into an NDIS packet which will be fed into the NDIS driver's
722 * NDIS packets consist of two parts: an ndis_packet structure,
723 * which is vaguely analagous to the pkthdr portion of an mbuf,
724 * and one or more ndis_buffer structures, which define the
725 * actual memory segments in which the packet data resides.
726 * We need to allocate one ndis_buffer for each mbuf in a chain,
727 * plus one ndis_packet as the header.
731 ndis_mtop(struct mbuf
*m0
, ndis_packet
**p
)
734 ndis_buffer
*buf
= NULL
, *prev
= NULL
;
735 ndis_packet_private
*priv
;
737 if (p
== NULL
|| *p
== NULL
|| m0
== NULL
)
740 priv
= &(*p
)->np_private
;
741 priv
->npp_totlen
= m0
->m_pkthdr
.len
;
743 for (m
= m0
; m
!= NULL
; m
= m
->m_next
) {
746 buf
= IoAllocateMdl(m
->m_data
, m
->m_len
, FALSE
, FALSE
, NULL
);
748 ndis_free_packet(*p
);
752 MmBuildMdlForNonPagedPool(buf
);
754 if (priv
->npp_head
== NULL
)
755 priv
->npp_head
= buf
;
757 prev
->mdl_next
= buf
;
761 priv
->npp_tail
= buf
;
767 ndis_get_supported_oids(void *arg
, ndis_oid
**oids
, int *oidcnt
)
772 if (arg
== NULL
|| oids
== NULL
|| oidcnt
== NULL
)
775 ndis_get_info(arg
, OID_GEN_SUPPORTED_LIST
, NULL
, &len
);
777 o
= kmalloc(len
, M_DEVBUF
, M_WAITOK
);
779 rval
= ndis_get_info(arg
, OID_GEN_SUPPORTED_LIST
, o
, &len
);
793 ndis_set_info(void *arg
, ndis_oid oid
, void *buf
, int *buflen
)
795 struct ndis_softc
*sc
;
798 ndis_setinfo_handler setfunc
;
799 uint32_t byteswritten
= 0, bytesneeded
= 0;
804 * According to the NDIS spec, MiniportQueryInformation()
805 * and MiniportSetInformation() requests are handled serially:
806 * once one request has been issued, we must wait for it to
807 * finish before allowing another request to proceed.
812 KeResetEvent(&sc
->ndis_block
->nmb_setevent
);
814 KeAcquireSpinLock(&sc
->ndis_block
->nmb_lock
, &irql
);
816 if (sc
->ndis_block
->nmb_pendingreq
!= NULL
) {
817 KeReleaseSpinLock(&sc
->ndis_block
->nmb_lock
, irql
);
818 panic("ndis_set_info() called while other request pending");
820 sc
->ndis_block
->nmb_pendingreq
= (ndis_request
*)sc
;
822 setfunc
= sc
->ndis_chars
->nmc_setinfo_func
;
823 adapter
= sc
->ndis_block
->nmb_miniportadapterctx
;
825 if (adapter
== NULL
|| setfunc
== NULL
||
826 sc
->ndis_block
->nmb_devicectx
== NULL
) {
827 sc
->ndis_block
->nmb_pendingreq
= NULL
;
828 KeReleaseSpinLock(&sc
->ndis_block
->nmb_lock
, irql
);
832 rval
= MSCALL6(setfunc
, adapter
, oid
, buf
, *buflen
,
833 &byteswritten
, &bytesneeded
);
835 sc
->ndis_block
->nmb_pendingreq
= NULL
;
837 KeReleaseSpinLock(&sc
->ndis_block
->nmb_lock
, irql
);
839 if (rval
== NDIS_STATUS_PENDING
) {
840 /* Wait up to 5 seconds. */
841 duetime
= (5 * 1000000) * -10;
842 KeWaitForSingleObject(&sc
->ndis_block
->nmb_setevent
,
843 0, 0, FALSE
, &duetime
);
844 rval
= sc
->ndis_block
->nmb_setstat
;
848 *buflen
= byteswritten
;
850 *buflen
= bytesneeded
;
852 if (rval
== NDIS_STATUS_INVALID_LENGTH
)
855 if (rval
== NDIS_STATUS_INVALID_OID
)
858 if (rval
== NDIS_STATUS_NOT_SUPPORTED
||
859 rval
== NDIS_STATUS_NOT_ACCEPTED
)
862 if (rval
!= NDIS_STATUS_SUCCESS
)
868 typedef void (*ndis_senddone_func
)(ndis_handle
, ndis_packet
*, ndis_status
);
871 ndis_send_packets(void *arg
, ndis_packet
**packets
, int cnt
)
873 struct ndis_softc
*sc
;
875 ndis_sendmulti_handler sendfunc
;
876 ndis_senddone_func senddonefunc
;
882 adapter
= sc
->ndis_block
->nmb_miniportadapterctx
;
885 sendfunc
= sc
->ndis_chars
->nmc_sendmulti_func
;
886 senddonefunc
= sc
->ndis_block
->nmb_senddone_func
;
888 if (NDIS_SERIALIZED(sc
->ndis_block
))
889 KeAcquireSpinLock(&sc
->ndis_block
->nmb_lock
, &irql
);
891 MSCALL3(sendfunc
, adapter
, packets
, cnt
);
893 for (i
= 0; i
< cnt
; i
++) {
896 * Either the driver already handed the packet to
897 * ndis_txeof() due to a failure, or it wants to keep
898 * it and release it asynchronously later. Skip to the
901 if (p
== NULL
|| p
->np_oob
.npo_status
== NDIS_STATUS_PENDING
)
903 MSCALL3(senddonefunc
, sc
->ndis_block
, p
, p
->np_oob
.npo_status
);
906 if (NDIS_SERIALIZED(sc
->ndis_block
))
907 KeReleaseSpinLock(&sc
->ndis_block
->nmb_lock
, irql
);
913 ndis_send_packet(void *arg
, ndis_packet
*packet
)
915 struct ndis_softc
*sc
;
918 ndis_sendsingle_handler sendfunc
;
919 ndis_senddone_func senddonefunc
;
923 adapter
= sc
->ndis_block
->nmb_miniportadapterctx
;
926 sendfunc
= sc
->ndis_chars
->nmc_sendsingle_func
;
927 senddonefunc
= sc
->ndis_block
->nmb_senddone_func
;
929 if (NDIS_SERIALIZED(sc
->ndis_block
))
930 KeAcquireSpinLock(&sc
->ndis_block
->nmb_lock
, &irql
);
931 status
= MSCALL3(sendfunc
, adapter
, packet
,
932 packet
->np_private
.npp_flags
);
934 if (status
== NDIS_STATUS_PENDING
) {
935 if (NDIS_SERIALIZED(sc
->ndis_block
))
936 KeReleaseSpinLock(&sc
->ndis_block
->nmb_lock
, irql
);
940 MSCALL3(senddonefunc
, sc
->ndis_block
, packet
, status
);
942 if (NDIS_SERIALIZED(sc
->ndis_block
))
943 KeReleaseSpinLock(&sc
->ndis_block
->nmb_lock
, irql
);
949 ndis_init_dma(void *arg
)
951 struct ndis_softc
*sc
;
956 sc
->ndis_tmaps
= kmalloc(sizeof(bus_dmamap_t
) * sc
->ndis_maxpkts
,
957 M_DEVBUF
, M_WAITOK
|M_ZERO
);
959 for (i
= 0; i
< sc
->ndis_maxpkts
; i
++) {
960 error
= bus_dmamap_create(sc
->ndis_ttag
, 0,
963 kfree(sc
->ndis_tmaps
, M_DEVBUF
);
972 ndis_destroy_dma(void *arg
)
974 struct ndis_softc
*sc
;
976 ndis_packet
*p
= NULL
;
981 for (i
= 0; i
< sc
->ndis_maxpkts
; i
++) {
982 if (sc
->ndis_txarray
[i
] != NULL
) {
983 p
= sc
->ndis_txarray
[i
];
984 m
= (struct mbuf
*)p
->np_rsvd
[1];
987 ndis_free_packet(sc
->ndis_txarray
[i
]);
989 bus_dmamap_destroy(sc
->ndis_ttag
, sc
->ndis_tmaps
[i
]);
992 kfree(sc
->ndis_tmaps
, M_DEVBUF
);
994 bus_dma_tag_destroy(sc
->ndis_ttag
);
1000 ndis_reset_nic(void *arg
)
1002 struct ndis_softc
*sc
;
1003 ndis_handle adapter
;
1004 ndis_reset_handler resetfunc
;
1005 uint8_t addressing_reset
;
1012 adapter
= sc
->ndis_block
->nmb_miniportadapterctx
;
1013 resetfunc
= sc
->ndis_chars
->nmc_reset_func
;
1015 if (adapter
== NULL
|| resetfunc
== NULL
||
1016 sc
->ndis_block
->nmb_devicectx
== NULL
) {
1023 KeResetEvent(&sc
->ndis_block
->nmb_resetevent
);
1025 if (NDIS_SERIALIZED(sc
->ndis_block
))
1026 KeAcquireSpinLock(&sc
->ndis_block
->nmb_lock
, &irql
);
1028 rval
= MSCALL2(resetfunc
, &addressing_reset
, adapter
);
1030 if (NDIS_SERIALIZED(sc
->ndis_block
))
1031 KeReleaseSpinLock(&sc
->ndis_block
->nmb_lock
, irql
);
1033 if (rval
== NDIS_STATUS_PENDING
)
1034 KeWaitForSingleObject(&sc
->ndis_block
->nmb_resetevent
,
1041 ndis_halt_nic(void *arg
)
1043 struct ndis_softc
*sc
;
1044 ndis_handle adapter
;
1045 ndis_halt_handler haltfunc
;
1046 ndis_miniport_block
*block
;
1051 block
= sc
->ndis_block
;
1054 KeFlushQueuedDpcs();
1057 * Wait for all packets to be returned.
1061 KeAcquireSpinLock(&block
->nmb_returnlock
, &irql
);
1062 empty
= IsListEmpty(&block
->nmb_returnlist
);
1063 KeReleaseSpinLock(&block
->nmb_returnlock
, irql
);
1070 adapter
= sc
->ndis_block
->nmb_miniportadapterctx
;
1071 if (adapter
== NULL
) {
1076 sc
->ndis_block
->nmb_devicectx
= NULL
;
1079 * The adapter context is only valid after the init
1080 * handler has been called, and is invalid once the
1081 * halt handler has been called.
1084 haltfunc
= sc
->ndis_chars
->nmc_halt_func
;
1087 MSCALL1(haltfunc
, adapter
);
1090 sc
->ndis_block
->nmb_miniportadapterctx
= NULL
;
1097 ndis_shutdown_nic(void *arg
)
1099 struct ndis_softc
*sc
;
1100 ndis_handle adapter
;
1101 ndis_shutdown_handler shutdownfunc
;
1105 adapter
= sc
->ndis_block
->nmb_miniportadapterctx
;
1106 shutdownfunc
= sc
->ndis_chars
->nmc_shutdown_handler
;
1108 if (adapter
== NULL
|| shutdownfunc
== NULL
)
1111 if (sc
->ndis_chars
->nmc_rsvd0
== NULL
)
1112 MSCALL1(shutdownfunc
, adapter
);
1114 MSCALL1(shutdownfunc
, sc
->ndis_chars
->nmc_rsvd0
);
1116 TAILQ_REMOVE(&ndis_devhead
, sc
->ndis_block
, link
);
1122 ndis_pnpevent_nic(void *arg
, int type
)
1125 struct ndis_softc
*sc
;
1126 ndis_handle adapter
;
1127 ndis_pnpevent_handler pnpeventfunc
;
1130 sc
= device_get_softc(dev
);
1132 adapter
= sc
->ndis_block
->nmb_miniportadapterctx
;
1133 pnpeventfunc
= sc
->ndis_chars
->nmc_pnpevent_handler
;
1135 if (adapter
== NULL
|| pnpeventfunc
== NULL
)
1138 if (sc
->ndis_chars
->nmc_rsvd0
== NULL
)
1139 MSCALL4(pnpeventfunc
, adapter
, type
, NULL
, 0);
1141 MSCALL4(pnpeventfunc
, sc
->ndis_chars
->nmc_rsvd0
, type
, NULL
, 0);
1147 ndis_init_nic(void *arg
)
1149 struct ndis_softc
*sc
;
1150 ndis_miniport_block
*block
;
1151 ndis_init_handler initfunc
;
1152 ndis_status status
, openstatus
= 0;
1153 ndis_medium mediumarray
[NdisMediumMax
];
1154 uint32_t chosenmedium
, i
;
1161 block
= sc
->ndis_block
;
1162 initfunc
= sc
->ndis_chars
->nmc_init_func
;
1165 sc
->ndis_block
->nmb_timerlist
= NULL
;
1167 for (i
= 0; i
< NdisMediumMax
; i
++)
1170 status
= MSCALL6(initfunc
, &openstatus
, &chosenmedium
,
1171 mediumarray
, NdisMediumMax
, block
, block
);
1174 * If the init fails, blow away the other exported routines
1175 * we obtained from the driver so we can't call them later.
1176 * If the init failed, none of these will work.
1178 if (status
!= NDIS_STATUS_SUCCESS
) {
1180 sc
->ndis_block
->nmb_miniportadapterctx
= NULL
;
1186 * This may look really goofy, but apparently it is possible
1187 * to halt a miniport too soon after it's been initialized.
1188 * After MiniportInitialize() finishes, pause for 1 second
1189 * to give the chip a chance to handle any short-lived timers
1190 * that were set in motion. If we call MiniportHalt() too soon,
1191 * some of the timers may not be cancelled, because the driver
1192 * expects them to fire before the halt is called.
1195 tsleep(arg
, 0, "ndwait", hz
);
1198 sc
->ndis_block
->nmb_devicectx
= sc
;
1205 ndis_intrsetup(kdpc
*dpc
, device_object
*dobj
, irp
*ip
, struct ndis_softc
*sc
)
1207 ndis_miniport_interrupt
*intr
;
1209 intr
= sc
->ndis_block
->nmb_interrupt
;
1216 KeAcquireSpinLockAtDpcLevel(&intr
->ni_dpccountlock
);
1217 KeResetEvent(&intr
->ni_dpcevt
);
1218 if (KeInsertQueueDpc(&intr
->ni_dpc
, NULL
, NULL
) == TRUE
)
1220 KeReleaseSpinLockFromDpcLevel(&intr
->ni_dpccountlock
);
1224 ndis_get_info(void *arg
, ndis_oid oid
, void *buf
, int *buflen
)
1226 struct ndis_softc
*sc
;
1228 ndis_handle adapter
;
1229 ndis_queryinfo_handler queryfunc
;
1230 uint32_t byteswritten
= 0, bytesneeded
= 0;
1236 KeResetEvent(&sc
->ndis_block
->nmb_getevent
);
1238 KeAcquireSpinLock(&sc
->ndis_block
->nmb_lock
, &irql
);
1240 if (sc
->ndis_block
->nmb_pendingreq
!= NULL
) {
1241 KeReleaseSpinLock(&sc
->ndis_block
->nmb_lock
, irql
);
1242 panic("ndis_get_info() called while other request pending");
1244 sc
->ndis_block
->nmb_pendingreq
= (ndis_request
*)sc
;
1246 queryfunc
= sc
->ndis_chars
->nmc_queryinfo_func
;
1247 adapter
= sc
->ndis_block
->nmb_miniportadapterctx
;
1249 if (adapter
== NULL
|| queryfunc
== NULL
||
1250 sc
->ndis_block
->nmb_devicectx
== NULL
) {
1251 sc
->ndis_block
->nmb_pendingreq
= NULL
;
1252 KeReleaseSpinLock(&sc
->ndis_block
->nmb_lock
, irql
);
1256 rval
= MSCALL6(queryfunc
, adapter
, oid
, buf
, *buflen
,
1257 &byteswritten
, &bytesneeded
);
1259 sc
->ndis_block
->nmb_pendingreq
= NULL
;
1261 KeReleaseSpinLock(&sc
->ndis_block
->nmb_lock
, irql
);
1263 /* Wait for requests that block. */
1265 if (rval
== NDIS_STATUS_PENDING
) {
1266 /* Wait up to 5 seconds. */
1267 duetime
= (5 * 1000000) * -10;
1268 KeWaitForSingleObject(&sc
->ndis_block
->nmb_getevent
,
1269 0, 0, FALSE
, &duetime
);
1270 rval
= sc
->ndis_block
->nmb_getstat
;
1274 *buflen
= byteswritten
;
1276 *buflen
= bytesneeded
;
1278 if (rval
== NDIS_STATUS_INVALID_LENGTH
||
1279 rval
== NDIS_STATUS_BUFFER_TOO_SHORT
)
1282 if (rval
== NDIS_STATUS_INVALID_OID
)
1285 if (rval
== NDIS_STATUS_NOT_SUPPORTED
||
1286 rval
== NDIS_STATUS_NOT_ACCEPTED
)
1289 if (rval
!= NDIS_STATUS_SUCCESS
)
1296 NdisAddDevice(driver_object
*drv
, device_object
*pdo
)
1299 ndis_miniport_block
*block
;
1300 struct ndis_softc
*sc
;
1304 sc
= device_get_softc(pdo
->do_devext
);
1306 if (sc
->ndis_iftype
== PCMCIABus
|| sc
->ndis_iftype
== PCIBus
) {
1307 error
= bus_setup_intr(sc
->ndis_dev
, sc
->ndis_irq
,
1309 ntoskrnl_intr
, sc
, &sc
->ndis_intrhand
, NULL
);
1311 return (NDIS_STATUS_FAILURE
);
1314 status
= IoCreateDevice(drv
, sizeof(ndis_miniport_block
), NULL
,
1315 FILE_DEVICE_UNKNOWN
, 0, FALSE
, &fdo
);
1317 if (status
!= STATUS_SUCCESS
)
1320 block
= fdo
->do_devext
;
1322 block
->nmb_filterdbs
.nf_ethdb
= block
;
1323 block
->nmb_deviceobj
= fdo
;
1324 block
->nmb_physdeviceobj
= pdo
;
1325 block
->nmb_nextdeviceobj
= IoAttachDeviceToDeviceStack(fdo
, pdo
);
1326 KeInitializeSpinLock(&block
->nmb_lock
);
1327 KeInitializeSpinLock(&block
->nmb_returnlock
);
1328 KeInitializeEvent(&block
->nmb_getevent
, EVENT_TYPE_NOTIFY
, TRUE
);
1329 KeInitializeEvent(&block
->nmb_setevent
, EVENT_TYPE_NOTIFY
, TRUE
);
1330 KeInitializeEvent(&block
->nmb_resetevent
, EVENT_TYPE_NOTIFY
, TRUE
);
1331 InitializeListHead(&block
->nmb_parmlist
);
1332 InitializeListHead(&block
->nmb_returnlist
);
1333 block
->nmb_returnitem
= IoAllocateWorkItem(fdo
);
1336 * Stash pointers to the miniport block and miniport
1337 * characteristics info in the if_ndis softc so the
1338 * UNIX wrapper driver can get to them later.
1340 sc
->ndis_block
= block
;
1341 sc
->ndis_chars
= IoGetDriverObjectExtension(drv
, (void *)1);
1344 * If the driver has a MiniportTransferData() function,
1345 * we should allocate a private RX packet pool.
1348 if (sc
->ndis_chars
->nmc_transferdata_func
!= NULL
) {
1349 NdisAllocatePacketPool(&status
, &block
->nmb_rxpool
,
1350 32, PROTOCOL_RESERVED_SIZE_IN_PACKET
);
1351 if (status
!= NDIS_STATUS_SUCCESS
) {
1352 IoDetachDevice(block
->nmb_nextdeviceobj
);
1353 IoDeleteDevice(fdo
);
1356 InitializeListHead((&block
->nmb_packetlist
));
1359 /* Give interrupt handling priority over timers. */
1360 IoInitializeDpcRequest(fdo
, kernndis_functbl
[6].ipt_wrap
);
1361 KeSetImportanceDpc(&fdo
->do_dpc
, KDPC_IMPORTANCE_HIGH
);
1363 /* Finish up BSD-specific setup. */
1365 block
->nmb_signature
= (void *)0xcafebabe;
1366 block
->nmb_status_func
= kernndis_functbl
[0].ipt_wrap
;
1367 block
->nmb_statusdone_func
= kernndis_functbl
[1].ipt_wrap
;
1368 block
->nmb_setdone_func
= kernndis_functbl
[2].ipt_wrap
;
1369 block
->nmb_querydone_func
= kernndis_functbl
[3].ipt_wrap
;
1370 block
->nmb_resetdone_func
= kernndis_functbl
[4].ipt_wrap
;
1371 block
->nmb_sendrsrc_func
= kernndis_functbl
[5].ipt_wrap
;
1372 block
->nmb_pendingreq
= NULL
;
1374 TAILQ_INSERT_TAIL(&ndis_devhead
, block
, link
);
1376 return (STATUS_SUCCESS
);
1380 ndis_unload_driver(void *arg
)
1382 struct ndis_softc
*sc
;
1387 if (sc
->ndis_intrhand
)
1388 bus_teardown_intr(sc
->ndis_dev
,
1389 sc
->ndis_irq
, sc
->ndis_intrhand
);
1391 if (sc
->ndis_block
->nmb_rlist
!= NULL
)
1392 kfree(sc
->ndis_block
->nmb_rlist
, M_DEVBUF
);
1394 ndis_flush_sysctls(sc
);
1396 TAILQ_REMOVE(&ndis_devhead
, sc
->ndis_block
, link
);
1398 if (sc
->ndis_chars
->nmc_transferdata_func
!= NULL
)
1399 NdisFreePacketPool(sc
->ndis_block
->nmb_rxpool
);
1400 fdo
= sc
->ndis_block
->nmb_deviceobj
;
1401 IoFreeWorkItem(sc
->ndis_block
->nmb_returnitem
);
1402 IoDetachDevice(sc
->ndis_block
->nmb_nextdeviceobj
);
1403 IoDeleteDevice(fdo
);