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 "use_oldusb.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/unistd.h>
40 #include <sys/types.h>
41 #include <sys/errno.h>
42 #include <sys/callout.h>
43 #include <sys/socket.h>
44 #include <sys/queue.h>
45 #include <sys/sysctl.h>
47 #include <sys/malloc.h>
49 #include <sys/mutex.h>
52 #include <sys/kernel.h>
53 #include <sys/module.h>
54 #include <sys/kthread.h>
57 #include <sys/mplock2.h>
60 #include <net/if_arp.h>
61 #include <net/ethernet.h>
62 #include <net/if_dl.h>
63 #include <net/if_media.h>
65 #include <netproto/802_11/ieee80211_var.h>
66 #include <netproto/802_11/ieee80211_ioctl.h>
69 #include <bus/u4b/usb.h>
70 #include <bus/u4b/usbdi.h>
72 #include <bus/usb/usb.h>
73 #include <bus/usb/usbdi.h>
76 #include <emulation/ndis/pe_var.h>
77 #include <emulation/ndis/cfg_var.h>
78 #include <emulation/ndis/resource_var.h>
79 #include <emulation/ndis/ntoskrnl_var.h>
80 #include <emulation/ndis/ndis_var.h>
81 #include <emulation/ndis/hal_var.h>
83 #include <emulation/ndis/u4bd_var.h>
85 #include <emulation/ndis/usbd_var.h>
87 #include <dev/netif/ndis/if_ndisvar.h>
89 #define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
91 static void ndis_status_func(ndis_handle, ndis_status, void *, uint32_t);
92 static void ndis_statusdone_func(ndis_handle);
93 static void ndis_setdone_func(ndis_handle, ndis_status);
94 static void ndis_getdone_func(ndis_handle, ndis_status);
95 static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
96 static void ndis_sendrsrcavail_func(ndis_handle);
97 static void ndis_intrsetup(kdpc *, device_object *,
98 irp *, struct ndis_softc *);
99 static void ndis_return(device_object *, void *);
101 static image_patch_table kernndis_functbl[] = {
102 IMPORT_SFUNC(ndis_status_func, 4),
103 IMPORT_SFUNC(ndis_statusdone_func, 1),
104 IMPORT_SFUNC(ndis_setdone_func, 2),
105 IMPORT_SFUNC(ndis_getdone_func, 2),
106 IMPORT_SFUNC(ndis_resetdone_func, 3),
107 IMPORT_SFUNC(ndis_sendrsrcavail_func, 1),
108 IMPORT_SFUNC(ndis_intrsetup, 4),
109 IMPORT_SFUNC(ndis_return, 1),
114 static struct nd_head ndis_devhead;
117 * This allows us to export our symbols to other modules.
119 * Note: some of the subsystems depend on each other, so the
120 * order in which they're started is important. The order of
123 * HAL - spinlocks and IRQL manipulation
124 * ntoskrnl - DPC and workitem threads, object waiting
125 * windrv - driver/device registration
127 * The HAL should also be the last thing shut down, since
128 * the ntoskrnl subsystem will use spinlocks right up until
129 * the DPC and workitem threads are terminated.
133 ndis_modevent(module_t mod, int cmd, void *arg)
136 image_patch_table *patch;
140 /* Initialize subsystems */
147 patch = kernndis_functbl;
148 while (patch->ipt_func != NULL) {
149 windrv_wrap((funcptr)patch->ipt_func,
150 (funcptr *)&patch->ipt_wrap,
151 patch->ipt_argcnt, patch->ipt_ftype);
155 TAILQ_INIT(&ndis_devhead);
158 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
159 /* Shut down subsystems */
166 patch = kernndis_functbl;
167 while (patch->ipt_func != NULL) {
168 windrv_unwrap(patch->ipt_wrap);
174 /* Shut down subsystems */
181 patch = kernndis_functbl;
182 while (patch->ipt_func != NULL) {
183 windrv_unwrap(patch->ipt_wrap);
195 DEV_MODULE(ndis, ndis_modevent, NULL);
196 MODULE_VERSION(ndis, 1);
199 ndis_sendrsrcavail_func(ndis_handle adapter)
204 ndis_status_func(ndis_handle adapter, ndis_status status, void *sbuf,
207 ndis_miniport_block *block;
208 struct ndis_softc *sc;
212 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
214 if (ifp->if_flags & IFF_DEBUG)
215 device_printf(sc->ndis_dev, "status: %x\n", status);
219 ndis_statusdone_func(ndis_handle adapter)
221 ndis_miniport_block *block;
222 struct ndis_softc *sc;
226 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
228 if (ifp->if_flags & IFF_DEBUG)
229 device_printf(sc->ndis_dev, "status complete\n");
233 ndis_setdone_func(ndis_handle adapter, ndis_status status)
235 ndis_miniport_block *block;
238 block->nmb_setstat = status;
239 KeSetEvent(&block->nmb_setevent, IO_NO_INCREMENT, FALSE);
243 ndis_getdone_func(ndis_handle adapter, ndis_status status)
245 ndis_miniport_block *block;
248 block->nmb_getstat = status;
249 KeSetEvent(&block->nmb_getevent, IO_NO_INCREMENT, FALSE);
253 ndis_resetdone_func(ndis_handle adapter, ndis_status status,
254 uint8_t addressingreset)
256 ndis_miniport_block *block;
257 struct ndis_softc *sc;
261 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
264 if (ifp->if_flags & IFF_DEBUG)
265 device_printf(sc->ndis_dev, "reset done...\n");
266 KeSetEvent(&block->nmb_resetevent, IO_NO_INCREMENT, FALSE);
270 ndis_create_sysctls(void *arg)
272 struct ndis_softc *sc;
275 struct sysctl_oid *oidp;
276 struct sysctl_ctx_entry *e;
282 vals = sc->ndis_regvals;
284 TAILQ_INIT(&sc->ndis_cfglist_head);
286 /* Create the sysctl tree. */
288 sc->ndis_tree = SYSCTL_ADD_NODE(&sc->ndis_ctx,
289 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
290 device_get_nameunit(sc->ndis_dev), CTLFLAG_RD, 0,
291 device_get_desc(sc->ndis_dev));
293 /* Add the driver-specific registry keys. */
296 if (vals->nc_cfgkey == NULL)
299 if (vals->nc_idx != sc->ndis_devidx) {
304 /* See if we already have a sysctl with this name */
307 TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
309 if (strcasecmp(oidp->oid_name, vals->nc_cfgkey) == 0)
319 ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
320 vals->nc_val, CTLFLAG_RW);
324 /* Now add a couple of builtin keys. */
327 * Environment can be either Windows (0) or WindowsNT (1).
328 * We qualify as the latter.
330 ndis_add_sysctl(sc, "Environment",
331 "Windows environment", "1", CTLFLAG_RD);
333 /* NDIS version should be 5.1. */
334 ndis_add_sysctl(sc, "NdisVersion",
335 "NDIS API Version", "0x00050001", CTLFLAG_RD);
338 * Some miniport drivers rely on the existence of the SlotNumber,
339 * NetCfgInstanceId and DriverDesc keys.
341 ndis_add_sysctl(sc, "SlotNumber", "Slot Number", "01", CTLFLAG_RD);
342 ndis_add_sysctl(sc, "NetCfgInstanceId", "NetCfgInstanceId",
343 "{12345678-1234-5678-CAFE0-123456789ABC}", CTLFLAG_RD);
344 ndis_add_sysctl(sc, "DriverDesc", "Driver Description",
345 "NDIS Network Adapter", CTLFLAG_RD);
347 /* Bus type (PCI, PCMCIA, etc...) */
348 ksprintf(buf, "%d", (int)sc->ndis_iftype);
349 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
351 if (sc->ndis_res_io != NULL) {
352 ksprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
353 ndis_add_sysctl(sc, "IOBaseAddress",
354 "Base I/O Address", buf, CTLFLAG_RD);
357 if (sc->ndis_irq != NULL) {
358 ksprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
359 ndis_add_sysctl(sc, "InterruptNumber",
360 "Interrupt Number", buf, CTLFLAG_RD);
367 ndis_add_sysctl(void *arg, char *key, char *desc, char *val, int flag)
369 struct ndis_softc *sc;
370 struct ndis_cfglist *cfg;
375 cfg = kmalloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_WAITOK|M_ZERO);
376 cfg->ndis_cfg.nc_cfgkey = kstrdup(key, M_DEVBUF);
378 ksnprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
379 cfg->ndis_cfg.nc_cfgdesc = kstrdup(descstr, M_DEVBUF);
381 cfg->ndis_cfg.nc_cfgdesc = kstrdup(desc, M_DEVBUF);
382 strcpy(cfg->ndis_cfg.nc_val, val);
384 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
387 SYSCTL_ADD_STRING(&sc->ndis_ctx, SYSCTL_CHILDREN(sc->ndis_tree),
388 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
389 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
390 cfg->ndis_cfg.nc_cfgdesc);
396 * Somewhere, somebody decided "hey, let's automatically create
397 * a sysctl tree for each device instance as it's created -- it'll
398 * make life so much easier!" Lies. Why must they turn the kernel
399 * into a house of lies?
403 ndis_flush_sysctls(void *arg)
405 struct ndis_softc *sc;
406 struct ndis_cfglist *cfg;
407 struct sysctl_ctx_list *clist;
411 clist = &sc->ndis_ctx;
413 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
414 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
415 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
416 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
417 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
418 kfree(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
419 kfree(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
420 kfree(cfg, M_DEVBUF);
427 ndis_get_routine_address(struct image_patch_table *functbl, char *name)
431 for (i = 0; functbl[i].ipt_name != NULL; i++)
432 if (strcmp(name, functbl[i].ipt_name) == 0)
433 return (functbl[i].ipt_wrap);
438 ndis_return(device_object *dobj, void *arg)
440 ndis_miniport_block *block;
441 ndis_miniport_characteristics *ch;
442 ndis_return_handler returnfunc;
449 ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
452 adapter = block->nmb_miniportadapterctx;
457 returnfunc = ch->nmc_return_packet_func;
459 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
460 while (!IsListEmpty(&block->nmb_returnlist)) {
461 l = RemoveHeadList((&block->nmb_returnlist));
462 p = CONTAINING_RECORD(l, ndis_packet, np_list);
463 InitializeListHead((&p->np_list));
464 KeReleaseSpinLock(&block->nmb_returnlock, irql);
465 MSCALL2(returnfunc, adapter, p);
466 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
468 KeReleaseSpinLock(&block->nmb_returnlock, irql);
472 ndis_reference_packet(void *arg)
481 /* Increment refcount. */
482 atomic_add_int(&p->np_refcnt, 1);
486 ndis_return_packet(void *arg)
489 ndis_miniport_block *block;
496 /* Release packet when refcount hits zero, otherwise return. */
497 if (atomic_fetchadd_int(&p->np_refcnt, -1) > 1)
500 block = ((struct ndis_softc *)p->np_softc)->ndis_block;
502 KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
503 InitializeListHead((&p->np_list));
504 InsertHeadList((&block->nmb_returnlist), (&p->np_list));
505 KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
507 IoQueueWorkItem(block->nmb_returnitem,
508 (io_workitem_func)kernndis_functbl[7].ipt_wrap,
509 WORKQUEUE_CRITICAL, block);
513 ndis_free_bufs(ndis_buffer *b0)
528 ndis_free_packet(ndis_packet *p)
533 ndis_free_bufs(p->np_private.npp_head);
538 ndis_convert_res(void *arg)
540 struct ndis_softc *sc;
541 ndis_resource_list *rl = NULL;
542 cm_partial_resource_desc *prd = NULL;
543 ndis_miniport_block *block;
545 struct resource_list *brl;
546 struct resource_list_entry *brle;
547 struct resource_list brl_rev;
548 struct resource_list_entry *n;
552 block = sc->ndis_block;
555 SLIST_INIT(&brl_rev);
557 rl = kmalloc(sizeof(ndis_resource_list) +
558 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
559 M_DEVBUF, M_WAITOK|M_NULLOK|M_ZERO);
564 rl->cprl_version = 5;
565 rl->cprl_revision = 1;
566 rl->cprl_count = sc->ndis_rescnt;
567 prd = rl->cprl_partial_descs;
569 brl = BUS_GET_RESOURCE_LIST(dev, dev);
574 * We have a small problem. Some PCI devices have
575 * multiple I/O ranges. Windows orders them starting
576 * from lowest numbered BAR to highest. We discover
577 * them in that order too, but insert them into a singly
578 * linked list head first, which means when time comes
579 * to traverse the list, we enumerate them in reverse
580 * order. This screws up some drivers which expect the
581 * BARs to be in ascending order so that they can choose
582 * the "first" one as their register space. Unfortunately,
583 * in order to fix this, we have to create our own
584 * temporary list with the entries in reverse order.
587 SLIST_FOREACH(brle, brl, link) {
588 n = kmalloc(sizeof(struct resource_list_entry),
589 M_TEMP, M_WAITOK|M_NULLOK);
594 bcopy((char *)brle, (char *)n,
595 sizeof(struct resource_list_entry));
596 SLIST_INSERT_HEAD(&brl_rev, n, link);
599 SLIST_FOREACH(brle, &brl_rev, link) {
600 switch (brle->type) {
602 prd->cprd_type = CmResourceTypePort;
603 prd->cprd_flags = CM_RESOURCE_PORT_IO;
604 prd->cprd_sharedisp =
605 CmResourceShareDeviceExclusive;
606 prd->u.cprd_port.cprd_start.np_quad =
608 prd->u.cprd_port.cprd_len = brle->count;
611 prd->cprd_type = CmResourceTypeMemory;
613 CM_RESOURCE_MEMORY_READ_WRITE;
614 prd->cprd_sharedisp =
615 CmResourceShareDeviceExclusive;
616 prd->u.cprd_mem.cprd_start.np_quad =
618 prd->u.cprd_mem.cprd_len = brle->count;
621 prd->cprd_type = CmResourceTypeInterrupt;
624 * Always mark interrupt resources as
625 * shared, since in our implementation,
628 prd->cprd_sharedisp =
629 CmResourceShareShared;
630 prd->u.cprd_intr.cprd_level = brle->start;
631 prd->u.cprd_intr.cprd_vector = brle->start;
632 prd->u.cprd_intr.cprd_affinity = 0;
641 block->nmb_rlist = rl;
645 while (!SLIST_EMPTY(&brl_rev)) {
646 n = SLIST_FIRST(&brl_rev);
647 SLIST_REMOVE_HEAD(&brl_rev, link);
655 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
656 * packet, it will hand it to us in the form of an ndis_packet,
657 * which we need to convert to an mbuf that is then handed off
658 * to the stack. Note: we configure the mbuf list so that it uses
659 * the memory regions specified by the ndis_buffer structures in
660 * the ndis_packet as external storage. In most cases, this will
661 * point to a memory region allocated by the driver (either by
662 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
663 * the driver to handle kfree()ing this region for is, so we set up
664 * a dummy no-op free handler for it.
668 ndis_ptom(struct mbuf **m0, ndis_packet *p)
670 struct mbuf *m = NULL, *prev = NULL;
672 ndis_packet_private *priv;
675 struct ether_header *eh;
678 if (p == NULL || m0 == NULL)
681 priv = &p->np_private;
682 buf = priv->npp_head;
685 for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
686 if (buf == priv->npp_head) {
687 /* XXX swildner: why not MT_HEADER? (see FreeBSD) */
688 MGETHDR(m, MB_DONTWAIT, MT_DATA);
690 MGET(m, MB_DONTWAIT, MT_DATA);
697 m->m_len = MmGetMdlByteCount(buf);
698 m->m_data = MmGetMdlVirtualAddress(buf);
699 m_extadd(m, m->m_data, m->m_len, ndis_reference_packet,
700 ndis_return_packet, p);
704 if (m->m_flags & M_PKTHDR)
712 * This is a hack to deal with the Marvell 8335 driver
713 * which, when associated with an AP in WPA-PSK mode,
714 * seems to overpad its frames by 8 bytes. I don't know
715 * that the extra 8 bytes are for, and they're not there
716 * in open mode, so for now clamp the frame size at 1514
717 * until I can figure out how to deal with this properly,
718 * otherwise if_ethersubr() will spank us by discarding
719 * the 'oversize' frames.
722 eh = mtod((*m0), struct ether_header *);
723 ifp = ((struct ndis_softc *)p->np_softc)->ifp;
724 if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
725 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
729 (*m0)->m_pkthdr.len = totlen;
735 * Create an NDIS packet from an mbuf chain.
736 * This is used mainly when transmitting packets, where we need
737 * to turn an mbuf off an interface's send queue and transform it
738 * into an NDIS packet which will be fed into the NDIS driver's
741 * NDIS packets consist of two parts: an ndis_packet structure,
742 * which is vaguely analagous to the pkthdr portion of an mbuf,
743 * and one or more ndis_buffer structures, which define the
744 * actual memory segments in which the packet data resides.
745 * We need to allocate one ndis_buffer for each mbuf in a chain,
746 * plus one ndis_packet as the header.
750 ndis_mtop(struct mbuf *m0, ndis_packet **p)
753 ndis_buffer *buf = NULL, *prev = NULL;
754 ndis_packet_private *priv;
756 if (p == NULL || *p == NULL || m0 == NULL)
759 priv = &(*p)->np_private;
760 priv->npp_totlen = m0->m_pkthdr.len;
762 for (m = m0; m != NULL; m = m->m_next) {
765 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
767 ndis_free_packet(*p);
771 MmBuildMdlForNonPagedPool(buf);
773 if (priv->npp_head == NULL)
774 priv->npp_head = buf;
776 prev->mdl_next = buf;
780 priv->npp_tail = buf;
786 ndis_get_supported_oids(void *arg, ndis_oid **oids, int *oidcnt)
791 if (arg == NULL || oids == NULL || oidcnt == NULL)
794 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
796 o = kmalloc(len, M_DEVBUF, M_WAITOK);
798 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
812 ndis_set_info(void *arg, ndis_oid oid, void *buf, int *buflen)
814 struct ndis_softc *sc;
817 ndis_setinfo_handler setfunc;
818 uint32_t byteswritten = 0, bytesneeded = 0;
823 * According to the NDIS spec, MiniportQueryInformation()
824 * and MiniportSetInformation() requests are handled serially:
825 * once one request has been issued, we must wait for it to
826 * finish before allowing another request to proceed.
831 KeResetEvent(&sc->ndis_block->nmb_setevent);
833 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
835 if (sc->ndis_block->nmb_pendingreq != NULL) {
836 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
837 panic("ndis_set_info() called while other request pending");
839 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
841 setfunc = sc->ndis_chars->nmc_setinfo_func;
842 adapter = sc->ndis_block->nmb_miniportadapterctx;
844 if (adapter == NULL || setfunc == NULL ||
845 sc->ndis_block->nmb_devicectx == NULL) {
846 sc->ndis_block->nmb_pendingreq = NULL;
847 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
851 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
852 &byteswritten, &bytesneeded);
854 sc->ndis_block->nmb_pendingreq = NULL;
856 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
858 if (rval == NDIS_STATUS_PENDING) {
859 /* Wait up to 5 seconds. */
860 duetime = (5 * 1000000) * -10;
861 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
862 0, 0, FALSE, &duetime);
863 rval = sc->ndis_block->nmb_setstat;
867 *buflen = byteswritten;
869 *buflen = bytesneeded;
871 if (rval == NDIS_STATUS_INVALID_LENGTH)
874 if (rval == NDIS_STATUS_INVALID_OID)
877 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
878 rval == NDIS_STATUS_NOT_ACCEPTED)
881 if (rval != NDIS_STATUS_SUCCESS)
887 typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
890 ndis_send_packets(void *arg, ndis_packet **packets, int cnt)
892 struct ndis_softc *sc;
894 ndis_sendmulti_handler sendfunc;
895 ndis_senddone_func senddonefunc;
901 adapter = sc->ndis_block->nmb_miniportadapterctx;
904 sendfunc = sc->ndis_chars->nmc_sendmulti_func;
905 senddonefunc = sc->ndis_block->nmb_senddone_func;
907 if (NDIS_SERIALIZED(sc->ndis_block))
908 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
910 MSCALL3(sendfunc, adapter, packets, cnt);
912 for (i = 0; i < cnt; i++) {
915 * Either the driver already handed the packet to
916 * ndis_txeof() due to a failure, or it wants to keep
917 * it and release it asynchronously later. Skip to the
920 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
922 MSCALL3(senddonefunc, sc->ndis_block, p, p->np_oob.npo_status);
925 if (NDIS_SERIALIZED(sc->ndis_block))
926 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
932 ndis_send_packet(void *arg, ndis_packet *packet)
934 struct ndis_softc *sc;
937 ndis_sendsingle_handler sendfunc;
938 ndis_senddone_func senddonefunc;
942 adapter = sc->ndis_block->nmb_miniportadapterctx;
945 sendfunc = sc->ndis_chars->nmc_sendsingle_func;
946 senddonefunc = sc->ndis_block->nmb_senddone_func;
948 if (NDIS_SERIALIZED(sc->ndis_block))
949 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
950 status = MSCALL3(sendfunc, adapter, packet,
951 packet->np_private.npp_flags);
953 if (status == NDIS_STATUS_PENDING) {
954 if (NDIS_SERIALIZED(sc->ndis_block))
955 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
959 MSCALL3(senddonefunc, sc->ndis_block, packet, status);
961 if (NDIS_SERIALIZED(sc->ndis_block))
962 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
968 ndis_init_dma(void *arg)
970 struct ndis_softc *sc;
975 sc->ndis_tmaps = kmalloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
976 M_DEVBUF, M_WAITOK|M_ZERO);
978 for (i = 0; i < sc->ndis_maxpkts; i++) {
979 error = bus_dmamap_create(sc->ndis_ttag, 0,
982 kfree(sc->ndis_tmaps, M_DEVBUF);
991 ndis_destroy_dma(void *arg)
993 struct ndis_softc *sc;
995 ndis_packet *p = NULL;
1000 for (i = 0; i < sc->ndis_maxpkts; i++) {
1001 if (sc->ndis_txarray[i] != NULL) {
1002 p = sc->ndis_txarray[i];
1003 m = (struct mbuf *)p->np_rsvd[1];
1006 ndis_free_packet(sc->ndis_txarray[i]);
1008 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
1011 kfree(sc->ndis_tmaps, M_DEVBUF);
1013 bus_dma_tag_destroy(sc->ndis_ttag);
1019 ndis_reset_nic(void *arg)
1021 struct ndis_softc *sc;
1022 ndis_handle adapter;
1023 ndis_reset_handler resetfunc;
1024 uint8_t addressing_reset;
1031 adapter = sc->ndis_block->nmb_miniportadapterctx;
1032 resetfunc = sc->ndis_chars->nmc_reset_func;
1034 if (adapter == NULL || resetfunc == NULL ||
1035 sc->ndis_block->nmb_devicectx == NULL) {
1042 KeResetEvent(&sc->ndis_block->nmb_resetevent);
1044 if (NDIS_SERIALIZED(sc->ndis_block))
1045 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1047 rval = MSCALL2(resetfunc, &addressing_reset, adapter);
1049 if (NDIS_SERIALIZED(sc->ndis_block))
1050 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1052 if (rval == NDIS_STATUS_PENDING)
1053 KeWaitForSingleObject(&sc->ndis_block->nmb_resetevent,
1060 ndis_halt_nic(void *arg)
1062 struct ndis_softc *sc;
1063 ndis_handle adapter;
1064 ndis_halt_handler haltfunc;
1065 ndis_miniport_block *block;
1070 block = sc->ndis_block;
1073 KeFlushQueuedDpcs();
1076 * Wait for all packets to be returned.
1080 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
1081 empty = IsListEmpty(&block->nmb_returnlist);
1082 KeReleaseSpinLock(&block->nmb_returnlock, irql);
1089 adapter = sc->ndis_block->nmb_miniportadapterctx;
1090 if (adapter == NULL) {
1095 sc->ndis_block->nmb_devicectx = NULL;
1098 * The adapter context is only valid after the init
1099 * handler has been called, and is invalid once the
1100 * halt handler has been called.
1103 haltfunc = sc->ndis_chars->nmc_halt_func;
1106 MSCALL1(haltfunc, adapter);
1109 sc->ndis_block->nmb_miniportadapterctx = NULL;
1116 ndis_shutdown_nic(void *arg)
1118 struct ndis_softc *sc;
1119 ndis_handle adapter;
1120 ndis_shutdown_handler shutdownfunc;
1124 adapter = sc->ndis_block->nmb_miniportadapterctx;
1125 shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1127 if (adapter == NULL || shutdownfunc == NULL)
1130 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1131 MSCALL1(shutdownfunc, adapter);
1133 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
1135 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1141 ndis_pnpevent_nic(void *arg, int type)
1144 struct ndis_softc *sc;
1145 ndis_handle adapter;
1146 ndis_pnpevent_handler pnpeventfunc;
1149 sc = device_get_softc(dev);
1151 adapter = sc->ndis_block->nmb_miniportadapterctx;
1152 pnpeventfunc = sc->ndis_chars->nmc_pnpevent_handler;
1154 if (adapter == NULL || pnpeventfunc == NULL)
1157 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1158 MSCALL4(pnpeventfunc, adapter, type, NULL, 0);
1160 MSCALL4(pnpeventfunc, sc->ndis_chars->nmc_rsvd0, type, NULL, 0);
1166 ndis_init_nic(void *arg)
1168 struct ndis_softc *sc;
1169 ndis_miniport_block *block;
1170 ndis_init_handler initfunc;
1171 ndis_status status, openstatus = 0;
1172 ndis_medium mediumarray[NdisMediumMax];
1173 uint32_t chosenmedium, i;
1180 block = sc->ndis_block;
1181 initfunc = sc->ndis_chars->nmc_init_func;
1184 sc->ndis_block->nmb_timerlist = NULL;
1186 for (i = 0; i < NdisMediumMax; i++)
1189 status = MSCALL6(initfunc, &openstatus, &chosenmedium,
1190 mediumarray, NdisMediumMax, block, block);
1193 * If the init fails, blow away the other exported routines
1194 * we obtained from the driver so we can't call them later.
1195 * If the init failed, none of these will work.
1197 if (status != NDIS_STATUS_SUCCESS) {
1199 sc->ndis_block->nmb_miniportadapterctx = NULL;
1205 * This may look really goofy, but apparently it is possible
1206 * to halt a miniport too soon after it's been initialized.
1207 * After MiniportInitialize() finishes, pause for 1 second
1208 * to give the chip a chance to handle any short-lived timers
1209 * that were set in motion. If we call MiniportHalt() too soon,
1210 * some of the timers may not be cancelled, because the driver
1211 * expects them to fire before the halt is called.
1214 tsleep(arg, 0, "ndwait", hz);
1217 sc->ndis_block->nmb_devicectx = sc;
1224 ndis_intrsetup(kdpc *dpc, device_object *dobj, irp *ip, struct ndis_softc *sc)
1226 ndis_miniport_interrupt *intr;
1228 intr = sc->ndis_block->nmb_interrupt;
1235 KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
1236 KeResetEvent(&intr->ni_dpcevt);
1237 if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
1239 KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
1243 ndis_get_info(void *arg, ndis_oid oid, void *buf, int *buflen)
1245 struct ndis_softc *sc;
1247 ndis_handle adapter;
1248 ndis_queryinfo_handler queryfunc;
1249 uint32_t byteswritten = 0, bytesneeded = 0;
1255 KeResetEvent(&sc->ndis_block->nmb_getevent);
1257 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1259 if (sc->ndis_block->nmb_pendingreq != NULL) {
1260 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1261 panic("ndis_get_info() called while other request pending");
1263 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
1265 queryfunc = sc->ndis_chars->nmc_queryinfo_func;
1266 adapter = sc->ndis_block->nmb_miniportadapterctx;
1268 if (adapter == NULL || queryfunc == NULL ||
1269 sc->ndis_block->nmb_devicectx == NULL) {
1270 sc->ndis_block->nmb_pendingreq = NULL;
1271 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1275 rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
1276 &byteswritten, &bytesneeded);
1278 sc->ndis_block->nmb_pendingreq = NULL;
1280 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1282 /* Wait for requests that block. */
1284 if (rval == NDIS_STATUS_PENDING) {
1285 /* Wait up to 5 seconds. */
1286 duetime = (5 * 1000000) * -10;
1287 KeWaitForSingleObject(&sc->ndis_block->nmb_getevent,
1288 0, 0, FALSE, &duetime);
1289 rval = sc->ndis_block->nmb_getstat;
1293 *buflen = byteswritten;
1295 *buflen = bytesneeded;
1297 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1298 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1301 if (rval == NDIS_STATUS_INVALID_OID)
1304 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1305 rval == NDIS_STATUS_NOT_ACCEPTED)
1308 if (rval != NDIS_STATUS_SUCCESS)
1315 NdisAddDevice(driver_object *drv, device_object *pdo)
1318 ndis_miniport_block *block;
1319 struct ndis_softc *sc;
1323 sc = device_get_softc(pdo->do_devext);
1325 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
1326 error = bus_setup_intr(sc->ndis_dev, sc->ndis_irq,
1328 ntoskrnl_intr, sc, &sc->ndis_intrhand, NULL);
1330 return (NDIS_STATUS_FAILURE);
1333 status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1334 FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
1336 if (status != STATUS_SUCCESS)
1339 block = fdo->do_devext;
1341 block->nmb_filterdbs.nf_ethdb = block;
1342 block->nmb_deviceobj = fdo;
1343 block->nmb_physdeviceobj = pdo;
1344 block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
1345 KeInitializeSpinLock(&block->nmb_lock);
1346 KeInitializeSpinLock(&block->nmb_returnlock);
1347 KeInitializeEvent(&block->nmb_getevent, EVENT_TYPE_NOTIFY, TRUE);
1348 KeInitializeEvent(&block->nmb_setevent, EVENT_TYPE_NOTIFY, TRUE);
1349 KeInitializeEvent(&block->nmb_resetevent, EVENT_TYPE_NOTIFY, TRUE);
1350 InitializeListHead(&block->nmb_parmlist);
1351 InitializeListHead(&block->nmb_returnlist);
1352 block->nmb_returnitem = IoAllocateWorkItem(fdo);
1355 * Stash pointers to the miniport block and miniport
1356 * characteristics info in the if_ndis softc so the
1357 * UNIX wrapper driver can get to them later.
1359 sc->ndis_block = block;
1360 sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1);
1363 * If the driver has a MiniportTransferData() function,
1364 * we should allocate a private RX packet pool.
1367 if (sc->ndis_chars->nmc_transferdata_func != NULL) {
1368 NdisAllocatePacketPool(&status, &block->nmb_rxpool,
1369 32, PROTOCOL_RESERVED_SIZE_IN_PACKET);
1370 if (status != NDIS_STATUS_SUCCESS) {
1371 IoDetachDevice(block->nmb_nextdeviceobj);
1372 IoDeleteDevice(fdo);
1375 InitializeListHead((&block->nmb_packetlist));
1378 /* Give interrupt handling priority over timers. */
1379 IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1380 KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
1382 /* Finish up BSD-specific setup. */
1384 block->nmb_signature = (void *)0xcafebabe;
1385 block->nmb_status_func = kernndis_functbl[0].ipt_wrap;
1386 block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap;
1387 block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap;
1388 block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap;
1389 block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap;
1390 block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap;
1391 block->nmb_pendingreq = NULL;
1393 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
1395 return (STATUS_SUCCESS);
1399 ndis_unload_driver(void *arg)
1401 struct ndis_softc *sc;
1406 if (sc->ndis_intrhand)
1407 bus_teardown_intr(sc->ndis_dev,
1408 sc->ndis_irq, sc->ndis_intrhand);
1410 if (sc->ndis_block->nmb_rlist != NULL)
1411 kfree(sc->ndis_block->nmb_rlist, M_DEVBUF);
1413 ndis_flush_sysctls(sc);
1415 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1417 if (sc->ndis_chars->nmc_transferdata_func != NULL)
1418 NdisFreePacketPool(sc->ndis_block->nmb_rxpool);
1419 fdo = sc->ndis_block->nmb_deviceobj;
1420 IoFreeWorkItem(sc->ndis_block->nmb_returnitem);
1421 IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj);
1422 IoDeleteDevice(fdo);