2 * SPDX-License-Identifier: BSD-4-Clause
5 * Bill Paul <wpaul@windriver.com>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/unistd.h>
41 #include <sys/types.h>
42 #include <sys/errno.h>
43 #include <sys/callout.h>
44 #include <sys/socket.h>
45 #include <sys/queue.h>
46 #include <sys/sysctl.h>
48 #include <sys/malloc.h>
50 #include <sys/mutex.h>
53 #include <sys/kernel.h>
54 #include <sys/module.h>
55 #include <sys/kthread.h>
56 #include <machine/bus.h>
57 #include <machine/resource.h>
62 #include <net/if_var.h>
63 #include <net/if_arp.h>
64 #include <net/ethernet.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
68 #include <net80211/ieee80211_var.h>
69 #include <net80211/ieee80211_ioctl.h>
71 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
74 #include <compat/ndis/pe_var.h>
75 #include <compat/ndis/cfg_var.h>
76 #include <compat/ndis/resource_var.h>
77 #include <compat/ndis/ntoskrnl_var.h>
78 #include <compat/ndis/ndis_var.h>
79 #include <compat/ndis/hal_var.h>
80 #include <compat/ndis/usbd_var.h>
81 #include <dev/if_ndis/if_ndisvar.h>
83 #define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
84 #define NDIS_FLAG_RDONLY 1
86 static void ndis_status_func(ndis_handle, ndis_status, void *, uint32_t);
87 static void ndis_statusdone_func(ndis_handle);
88 static void ndis_setdone_func(ndis_handle, ndis_status);
89 static void ndis_getdone_func(ndis_handle, ndis_status);
90 static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
91 static void ndis_sendrsrcavail_func(ndis_handle);
92 static void ndis_intrsetup(kdpc *, device_object *,
93 irp *, struct ndis_softc *);
94 static void ndis_return(device_object *, void *);
96 static image_patch_table kernndis_functbl[] = {
97 IMPORT_SFUNC(ndis_status_func, 4),
98 IMPORT_SFUNC(ndis_statusdone_func, 1),
99 IMPORT_SFUNC(ndis_setdone_func, 2),
100 IMPORT_SFUNC(ndis_getdone_func, 2),
101 IMPORT_SFUNC(ndis_resetdone_func, 3),
102 IMPORT_SFUNC(ndis_sendrsrcavail_func, 1),
103 IMPORT_SFUNC(ndis_intrsetup, 4),
104 IMPORT_SFUNC(ndis_return, 1),
109 static struct nd_head ndis_devhead;
112 * This allows us to export our symbols to other modules.
113 * Note that we call ourselves 'ndisapi' to avoid a namespace
114 * collision with if_ndis.ko, which internally calls itself
117 * Note: some of the subsystems depend on each other, so the
118 * order in which they're started is important. The order of
121 * HAL - spinlocks and IRQL manipulation
122 * ntoskrnl - DPC and workitem threads, object waiting
123 * windrv - driver/device registration
125 * The HAL should also be the last thing shut down, since
126 * the ntoskrnl subsystem will use spinlocks right up until
127 * the DPC and workitem threads are terminated.
131 ndis_modevent(module_t mod, int cmd, void *arg)
134 image_patch_table *patch;
138 /* Initialize subsystems */
145 patch = kernndis_functbl;
146 while (patch->ipt_func != NULL) {
147 windrv_wrap((funcptr)patch->ipt_func,
148 (funcptr *)&patch->ipt_wrap,
149 patch->ipt_argcnt, patch->ipt_ftype);
153 TAILQ_INIT(&ndis_devhead);
156 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
157 /* Shut down subsystems */
164 patch = kernndis_functbl;
165 while (patch->ipt_func != NULL) {
166 windrv_unwrap(patch->ipt_wrap);
172 /* Shut down subsystems */
179 patch = kernndis_functbl;
180 while (patch->ipt_func != NULL) {
181 windrv_unwrap(patch->ipt_wrap);
193 DEV_MODULE(ndisapi, ndis_modevent, NULL);
194 MODULE_VERSION(ndisapi, 1);
197 ndis_sendrsrcavail_func(adapter)
203 ndis_status_func(adapter, status, sbuf, slen)
209 ndis_miniport_block *block;
210 struct ndis_softc *sc;
214 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
215 ifp = NDISUSB_GET_IFNET(sc);
216 if ( ifp && ifp->if_flags & IFF_DEBUG)
217 device_printf(sc->ndis_dev, "status: %x\n", status);
221 ndis_statusdone_func(adapter)
224 ndis_miniport_block *block;
225 struct ndis_softc *sc;
229 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
230 ifp = NDISUSB_GET_IFNET(sc);
231 if (ifp && ifp->if_flags & IFF_DEBUG)
232 device_printf(sc->ndis_dev, "status complete\n");
236 ndis_setdone_func(adapter, status)
240 ndis_miniport_block *block;
243 block->nmb_setstat = status;
244 KeSetEvent(&block->nmb_setevent, IO_NO_INCREMENT, FALSE);
248 ndis_getdone_func(adapter, status)
252 ndis_miniport_block *block;
255 block->nmb_getstat = status;
256 KeSetEvent(&block->nmb_getevent, IO_NO_INCREMENT, FALSE);
260 ndis_resetdone_func(ndis_handle adapter, ndis_status status,
261 uint8_t addressingreset)
263 ndis_miniport_block *block;
264 struct ndis_softc *sc;
268 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
269 ifp = NDISUSB_GET_IFNET(sc);
271 if (ifp && ifp->if_flags & IFF_DEBUG)
272 device_printf(sc->ndis_dev, "reset done...\n");
273 KeSetEvent(&block->nmb_resetevent, IO_NO_INCREMENT, FALSE);
277 ndis_create_sysctls(arg)
280 struct ndis_softc *sc;
283 struct sysctl_oid *oidp;
284 struct sysctl_ctx_entry *e;
291 device_printf(sc->ndis_dev, "ndis_create_sysctls() sc=%p\n", sc);
293 vals = sc->ndis_regvals;
295 TAILQ_INIT(&sc->ndis_cfglist_head);
297 /* Add the driver-specific registry keys. */
300 if (vals->nc_cfgkey == NULL)
303 if (vals->nc_idx != sc->ndis_devidx) {
308 /* See if we already have a sysctl with this name */
311 TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
313 if (strcasecmp(oidp->oid_name, vals->nc_cfgkey) == 0)
323 ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
324 vals->nc_val, CTLFLAG_RW);
328 /* Now add a couple of builtin keys. */
331 * Environment can be either Windows (0) or WindowsNT (1).
332 * We qualify as the latter.
334 ndis_add_sysctl(sc, "Environment",
335 "Windows environment", "1", NDIS_FLAG_RDONLY);
337 /* NDIS version should be 5.1. */
338 ndis_add_sysctl(sc, "NdisVersion",
339 "NDIS API Version", "0x00050001", NDIS_FLAG_RDONLY);
342 * Some miniport drivers rely on the existence of the SlotNumber,
343 * NetCfgInstanceId and DriverDesc keys.
345 ndis_add_sysctl(sc, "SlotNumber", "Slot Numer", "01", NDIS_FLAG_RDONLY);
346 ndis_add_sysctl(sc, "NetCfgInstanceId", "NetCfgInstanceId",
347 "{12345678-1234-5678-CAFE0-123456789ABC}", NDIS_FLAG_RDONLY);
348 ndis_add_sysctl(sc, "DriverDesc", "Driver Description",
349 "NDIS Network Adapter", NDIS_FLAG_RDONLY);
351 /* Bus type (PCI, PCMCIA, etc...) */
352 sprintf(buf, "%d", (int)sc->ndis_iftype);
353 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, NDIS_FLAG_RDONLY);
355 if (sc->ndis_res_io != NULL) {
356 sprintf(buf, "0x%jx", rman_get_start(sc->ndis_res_io));
357 ndis_add_sysctl(sc, "IOBaseAddress",
358 "Base I/O Address", buf, NDIS_FLAG_RDONLY);
361 if (sc->ndis_irq != NULL) {
362 sprintf(buf, "%ju", rman_get_start(sc->ndis_irq));
363 ndis_add_sysctl(sc, "InterruptNumber",
364 "Interrupt Number", buf, NDIS_FLAG_RDONLY);
371 ndis_add_sysctl(arg, key, desc, val, flag_rdonly)
378 struct ndis_softc *sc;
379 struct ndis_cfglist *cfg;
384 cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO);
387 printf("failed for %s\n", key);
391 cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF);
393 snprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
394 cfg->ndis_cfg.nc_cfgdesc = strdup(descstr, M_DEVBUF);
396 cfg->ndis_cfg.nc_cfgdesc = strdup(desc, M_DEVBUF);
397 strcpy(cfg->ndis_cfg.nc_val, val);
399 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
401 if (flag_rdonly != 0) {
403 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
404 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
405 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, CTLFLAG_RD,
406 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
407 cfg->ndis_cfg.nc_cfgdesc);
410 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
411 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
412 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, CTLFLAG_RW,
413 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
414 cfg->ndis_cfg.nc_cfgdesc);
420 * Somewhere, somebody decided "hey, let's automatically create
421 * a sysctl tree for each device instance as it's created -- it'll
422 * make life so much easier!" Lies. Why must they turn the kernel
423 * into a house of lies?
427 ndis_flush_sysctls(arg)
430 struct ndis_softc *sc;
431 struct ndis_cfglist *cfg;
432 struct sysctl_ctx_list *clist;
436 clist = device_get_sysctl_ctx(sc->ndis_dev);
438 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
439 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
440 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
441 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
442 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
443 free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
444 free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
452 ndis_get_routine_address(functbl, name)
453 struct image_patch_table *functbl;
458 for (i = 0; functbl[i].ipt_name != NULL; i++)
459 if (strcmp(name, functbl[i].ipt_name) == 0)
460 return (functbl[i].ipt_wrap);
465 ndis_return(dobj, arg)
469 ndis_miniport_block *block;
470 ndis_miniport_characteristics *ch;
471 ndis_return_handler returnfunc;
478 ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
481 adapter = block->nmb_miniportadapterctx;
486 returnfunc = ch->nmc_return_packet_func;
488 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
489 while (!IsListEmpty(&block->nmb_returnlist)) {
490 l = RemoveHeadList((&block->nmb_returnlist));
491 p = CONTAINING_RECORD(l, ndis_packet, np_list);
492 InitializeListHead((&p->np_list));
493 KeReleaseSpinLock(&block->nmb_returnlock, irql);
494 MSCALL2(returnfunc, adapter, p);
495 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
497 KeReleaseSpinLock(&block->nmb_returnlock, irql);
501 ndis_ext_free(struct mbuf *m)
504 return (ndis_return_packet(m->m_ext.ext_arg1));
508 ndis_return_packet(ndis_packet *p)
510 ndis_miniport_block *block;
515 /* Decrement refcount. */
518 /* Release packet when refcount hits zero, otherwise return. */
522 block = ((struct ndis_softc *)p->np_softc)->ndis_block;
524 KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
525 InitializeListHead((&p->np_list));
526 InsertHeadList((&block->nmb_returnlist), (&p->np_list));
527 KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
529 IoQueueWorkItem(block->nmb_returnitem,
530 (io_workitem_func)kernndis_functbl[7].ipt_wrap,
531 WORKQUEUE_CRITICAL, block);
557 ndis_free_bufs(p->np_private.npp_head);
562 ndis_convert_res(arg)
565 struct ndis_softc *sc;
566 ndis_resource_list *rl = NULL;
567 cm_partial_resource_desc *prd = NULL;
568 ndis_miniport_block *block;
570 struct resource_list *brl;
571 struct resource_list_entry *brle;
575 block = sc->ndis_block;
578 rl = malloc(sizeof(ndis_resource_list) +
579 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
580 M_DEVBUF, M_NOWAIT|M_ZERO);
585 rl->cprl_version = 5;
586 rl->cprl_revision = 1;
587 rl->cprl_count = sc->ndis_rescnt;
588 prd = rl->cprl_partial_descs;
590 brl = BUS_GET_RESOURCE_LIST(dev, dev);
594 STAILQ_FOREACH(brle, brl, link) {
595 switch (brle->type) {
597 prd->cprd_type = CmResourceTypePort;
598 prd->cprd_flags = CM_RESOURCE_PORT_IO;
599 prd->cprd_sharedisp =
600 CmResourceShareDeviceExclusive;
601 prd->u.cprd_port.cprd_start.np_quad =
603 prd->u.cprd_port.cprd_len = brle->count;
606 prd->cprd_type = CmResourceTypeMemory;
608 CM_RESOURCE_MEMORY_READ_WRITE;
609 prd->cprd_sharedisp =
610 CmResourceShareDeviceExclusive;
611 prd->u.cprd_mem.cprd_start.np_quad =
613 prd->u.cprd_mem.cprd_len = brle->count;
616 prd->cprd_type = CmResourceTypeInterrupt;
619 * Always mark interrupt resources as
620 * shared, since in our implementation,
623 prd->cprd_sharedisp =
624 CmResourceShareShared;
625 prd->u.cprd_intr.cprd_level = brle->start;
626 prd->u.cprd_intr.cprd_vector = brle->start;
627 prd->u.cprd_intr.cprd_affinity = 0;
636 block->nmb_rlist = rl;
642 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
643 * packet, it will hand it to us in the form of an ndis_packet,
644 * which we need to convert to an mbuf that is then handed off
645 * to the stack. Note: we configure the mbuf list so that it uses
646 * the memory regions specified by the ndis_buffer structures in
647 * the ndis_packet as external storage. In most cases, this will
648 * point to a memory region allocated by the driver (either by
649 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
650 * the driver to handle free()ing this region for is, so we set up
651 * a dummy no-op free handler for it.
659 struct mbuf *m = NULL, *prev = NULL;
661 ndis_packet_private *priv;
664 struct ether_header *eh;
667 if (p == NULL || m0 == NULL)
670 priv = &p->np_private;
671 buf = priv->npp_head;
674 for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
675 if (buf == priv->npp_head)
676 m = m_gethdr(M_NOWAIT, MT_DATA);
678 m = m_get(M_NOWAIT, MT_DATA);
684 m->m_len = MmGetMdlByteCount(buf);
685 m_extadd(m, MmGetMdlVirtualAddress(buf), m->m_len,
686 ndis_ext_free, p, NULL, 0, EXT_NDIS);
690 if (m->m_flags & M_PKTHDR)
698 * This is a hack to deal with the Marvell 8335 driver
699 * which, when associated with an AP in WPA-PSK mode,
700 * seems to overpad its frames by 8 bytes. I don't know
701 * that the extra 8 bytes are for, and they're not there
702 * in open mode, so for now clamp the frame size at 1514
703 * until I can figure out how to deal with this properly,
704 * otherwise if_ethersubr() will spank us by discarding
705 * the 'oversize' frames.
708 eh = mtod((*m0), struct ether_header *);
709 ifp = NDISUSB_GET_IFNET((struct ndis_softc *)p->np_softc);
710 if (ifp && totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
711 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
715 (*m0)->m_pkthdr.len = totlen;
721 * Create an NDIS packet from an mbuf chain.
722 * This is used mainly when transmitting packets, where we need
723 * to turn an mbuf off an interface's send queue and transform it
724 * into an NDIS packet which will be fed into the NDIS driver's
727 * NDIS packets consist of two parts: an ndis_packet structure,
728 * which is vaguely analogous to the pkthdr portion of an mbuf,
729 * and one or more ndis_buffer structures, which define the
730 * actual memory segments in which the packet data resides.
731 * We need to allocate one ndis_buffer for each mbuf in a chain,
732 * plus one ndis_packet as the header.
741 ndis_buffer *buf = NULL, *prev = NULL;
742 ndis_packet_private *priv;
744 if (p == NULL || *p == NULL || m0 == NULL)
747 priv = &(*p)->np_private;
748 priv->npp_totlen = m0->m_pkthdr.len;
750 for (m = m0; m != NULL; m = m->m_next) {
753 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
755 ndis_free_packet(*p);
759 MmBuildMdlForNonPagedPool(buf);
761 if (priv->npp_head == NULL)
762 priv->npp_head = buf;
764 prev->mdl_next = buf;
768 priv->npp_tail = buf;
774 ndis_get_supported_oids(arg, oids, oidcnt)
782 if (arg == NULL || oids == NULL || oidcnt == NULL)
785 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
787 o = malloc(len, M_DEVBUF, M_NOWAIT);
791 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
805 ndis_set_info(arg, oid, buf, buflen)
811 struct ndis_softc *sc;
814 ndis_setinfo_handler setfunc;
815 uint32_t byteswritten = 0, bytesneeded = 0;
820 * According to the NDIS spec, MiniportQueryInformation()
821 * and MiniportSetInformation() requests are handled serially:
822 * once one request has been issued, we must wait for it to
823 * finish before allowing another request to proceed.
828 KeResetEvent(&sc->ndis_block->nmb_setevent);
830 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
832 if (sc->ndis_block->nmb_pendingreq != NULL) {
833 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
834 panic("ndis_set_info() called while other request pending");
836 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
838 setfunc = sc->ndis_chars->nmc_setinfo_func;
839 adapter = sc->ndis_block->nmb_miniportadapterctx;
841 if (adapter == NULL || setfunc == NULL ||
842 sc->ndis_block->nmb_devicectx == NULL) {
843 sc->ndis_block->nmb_pendingreq = NULL;
844 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
848 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
849 &byteswritten, &bytesneeded);
851 sc->ndis_block->nmb_pendingreq = NULL;
853 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
855 if (rval == NDIS_STATUS_PENDING) {
856 /* Wait up to 5 seconds. */
857 duetime = (5 * 1000000) * -10;
858 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
859 0, 0, FALSE, &duetime);
860 rval = sc->ndis_block->nmb_setstat;
864 *buflen = byteswritten;
866 *buflen = bytesneeded;
868 if (rval == NDIS_STATUS_INVALID_LENGTH)
871 if (rval == NDIS_STATUS_INVALID_OID)
874 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
875 rval == NDIS_STATUS_NOT_ACCEPTED)
878 if (rval != NDIS_STATUS_SUCCESS)
884 typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
887 ndis_send_packets(arg, packets, cnt)
889 ndis_packet **packets;
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(arg, packet)
936 struct ndis_softc *sc;
939 ndis_sendsingle_handler sendfunc;
940 ndis_senddone_func senddonefunc;
944 adapter = sc->ndis_block->nmb_miniportadapterctx;
947 sendfunc = sc->ndis_chars->nmc_sendsingle_func;
948 senddonefunc = sc->ndis_block->nmb_senddone_func;
950 if (NDIS_SERIALIZED(sc->ndis_block))
951 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
952 status = MSCALL3(sendfunc, adapter, packet,
953 packet->np_private.npp_flags);
955 if (status == NDIS_STATUS_PENDING) {
956 if (NDIS_SERIALIZED(sc->ndis_block))
957 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
961 MSCALL3(senddonefunc, sc->ndis_block, packet, status);
963 if (NDIS_SERIALIZED(sc->ndis_block))
964 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
973 struct ndis_softc *sc;
978 sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
979 M_DEVBUF, M_NOWAIT|M_ZERO);
981 if (sc->ndis_tmaps == NULL)
984 for (i = 0; i < sc->ndis_maxpkts; i++) {
985 error = bus_dmamap_create(sc->ndis_ttag, 0,
988 free(sc->ndis_tmaps, M_DEVBUF);
997 ndis_destroy_dma(arg)
1000 struct ndis_softc *sc;
1002 ndis_packet *p = NULL;
1007 for (i = 0; i < sc->ndis_maxpkts; i++) {
1008 if (sc->ndis_txarray[i] != NULL) {
1009 p = sc->ndis_txarray[i];
1010 m = (struct mbuf *)p->np_rsvd[1];
1013 ndis_free_packet(sc->ndis_txarray[i]);
1015 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
1018 free(sc->ndis_tmaps, M_DEVBUF);
1020 bus_dma_tag_destroy(sc->ndis_ttag);
1029 struct ndis_softc *sc;
1030 ndis_handle adapter;
1031 ndis_reset_handler resetfunc;
1032 uint8_t addressing_reset;
1039 adapter = sc->ndis_block->nmb_miniportadapterctx;
1040 resetfunc = sc->ndis_chars->nmc_reset_func;
1042 if (adapter == NULL || resetfunc == NULL ||
1043 sc->ndis_block->nmb_devicectx == NULL) {
1050 KeResetEvent(&sc->ndis_block->nmb_resetevent);
1052 if (NDIS_SERIALIZED(sc->ndis_block))
1053 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1055 rval = MSCALL2(resetfunc, &addressing_reset, adapter);
1057 if (NDIS_SERIALIZED(sc->ndis_block))
1058 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1060 if (rval == NDIS_STATUS_PENDING)
1061 KeWaitForSingleObject(&sc->ndis_block->nmb_resetevent,
1071 struct ndis_softc *sc;
1072 ndis_handle adapter;
1073 ndis_halt_handler haltfunc;
1074 ndis_miniport_block *block;
1079 block = sc->ndis_block;
1082 KeFlushQueuedDpcs();
1085 * Wait for all packets to be returned.
1089 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
1090 empty = IsListEmpty(&block->nmb_returnlist);
1091 KeReleaseSpinLock(&block->nmb_returnlock, irql);
1098 adapter = sc->ndis_block->nmb_miniportadapterctx;
1099 if (adapter == NULL) {
1104 sc->ndis_block->nmb_devicectx = NULL;
1107 * The adapter context is only valid after the init
1108 * handler has been called, and is invalid once the
1109 * halt handler has been called.
1112 haltfunc = sc->ndis_chars->nmc_halt_func;
1115 MSCALL1(haltfunc, adapter);
1118 sc->ndis_block->nmb_miniportadapterctx = NULL;
1125 ndis_shutdown_nic(arg)
1128 struct ndis_softc *sc;
1129 ndis_handle adapter;
1130 ndis_shutdown_handler shutdownfunc;
1134 adapter = sc->ndis_block->nmb_miniportadapterctx;
1135 shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1137 if (adapter == NULL || shutdownfunc == NULL)
1140 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1141 MSCALL1(shutdownfunc, adapter);
1143 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
1145 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1151 ndis_pnpevent_nic(arg, type)
1156 struct ndis_softc *sc;
1157 ndis_handle adapter;
1158 ndis_pnpevent_handler pnpeventfunc;
1161 sc = device_get_softc(arg);
1163 adapter = sc->ndis_block->nmb_miniportadapterctx;
1164 pnpeventfunc = sc->ndis_chars->nmc_pnpevent_handler;
1166 if (adapter == NULL || pnpeventfunc == NULL)
1169 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1170 MSCALL4(pnpeventfunc, adapter, type, NULL, 0);
1172 MSCALL4(pnpeventfunc, sc->ndis_chars->nmc_rsvd0, type, NULL, 0);
1181 struct ndis_softc *sc;
1182 ndis_miniport_block *block;
1183 ndis_init_handler initfunc;
1184 ndis_status status, openstatus = 0;
1185 ndis_medium mediumarray[NdisMediumMax];
1186 uint32_t chosenmedium, i;
1193 block = sc->ndis_block;
1194 initfunc = sc->ndis_chars->nmc_init_func;
1197 sc->ndis_block->nmb_timerlist = NULL;
1199 for (i = 0; i < NdisMediumMax; i++)
1202 status = MSCALL6(initfunc, &openstatus, &chosenmedium,
1203 mediumarray, NdisMediumMax, block, block);
1206 * If the init fails, blow away the other exported routines
1207 * we obtained from the driver so we can't call them later.
1208 * If the init failed, none of these will work.
1210 if (status != NDIS_STATUS_SUCCESS) {
1212 sc->ndis_block->nmb_miniportadapterctx = NULL;
1218 * This may look really goofy, but apparently it is possible
1219 * to halt a miniport too soon after it's been initialized.
1220 * After MiniportInitialize() finishes, pause for 1 second
1221 * to give the chip a chance to handle any short-lived timers
1222 * that were set in motion. If we call MiniportHalt() too soon,
1223 * some of the timers may not be cancelled, because the driver
1224 * expects them to fire before the halt is called.
1227 pause("ndwait", hz);
1230 sc->ndis_block->nmb_devicectx = sc;
1237 ndis_intrsetup(dpc, dobj, ip, sc)
1239 device_object *dobj;
1241 struct ndis_softc *sc;
1243 ndis_miniport_interrupt *intr;
1245 intr = sc->ndis_block->nmb_interrupt;
1252 KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
1253 KeResetEvent(&intr->ni_dpcevt);
1254 if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
1256 KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
1260 ndis_get_info(arg, oid, buf, buflen)
1266 struct ndis_softc *sc;
1268 ndis_handle adapter;
1269 ndis_queryinfo_handler queryfunc;
1270 uint32_t byteswritten = 0, bytesneeded = 0;
1276 KeResetEvent(&sc->ndis_block->nmb_getevent);
1278 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1280 if (sc->ndis_block->nmb_pendingreq != NULL) {
1281 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1282 panic("ndis_get_info() called while other request pending");
1284 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
1286 queryfunc = sc->ndis_chars->nmc_queryinfo_func;
1287 adapter = sc->ndis_block->nmb_miniportadapterctx;
1289 if (adapter == NULL || queryfunc == NULL ||
1290 sc->ndis_block->nmb_devicectx == NULL) {
1291 sc->ndis_block->nmb_pendingreq = NULL;
1292 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1296 rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
1297 &byteswritten, &bytesneeded);
1299 sc->ndis_block->nmb_pendingreq = NULL;
1301 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1303 /* Wait for requests that block. */
1305 if (rval == NDIS_STATUS_PENDING) {
1306 /* Wait up to 5 seconds. */
1307 duetime = (5 * 1000000) * -10;
1308 KeWaitForSingleObject(&sc->ndis_block->nmb_getevent,
1309 0, 0, FALSE, &duetime);
1310 rval = sc->ndis_block->nmb_getstat;
1314 *buflen = byteswritten;
1316 *buflen = bytesneeded;
1318 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1319 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1322 if (rval == NDIS_STATUS_INVALID_OID)
1325 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1326 rval == NDIS_STATUS_NOT_ACCEPTED)
1329 if (rval != NDIS_STATUS_SUCCESS)
1336 NdisAddDevice(drv, pdo)
1341 ndis_miniport_block *block;
1342 struct ndis_softc *sc;
1346 sc = device_get_softc(pdo->do_devext);
1348 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
1349 error = bus_setup_intr(sc->ndis_dev, sc->ndis_irq,
1350 INTR_TYPE_NET | INTR_MPSAFE,
1351 NULL, ntoskrnl_intr, NULL, &sc->ndis_intrhand);
1353 return (NDIS_STATUS_FAILURE);
1356 status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1357 FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
1359 if (status != STATUS_SUCCESS)
1362 block = fdo->do_devext;
1364 block->nmb_filterdbs.nf_ethdb = block;
1365 block->nmb_deviceobj = fdo;
1366 block->nmb_physdeviceobj = pdo;
1367 block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
1368 KeInitializeSpinLock(&block->nmb_lock);
1369 KeInitializeSpinLock(&block->nmb_returnlock);
1370 KeInitializeEvent(&block->nmb_getevent, EVENT_TYPE_NOTIFY, TRUE);
1371 KeInitializeEvent(&block->nmb_setevent, EVENT_TYPE_NOTIFY, TRUE);
1372 KeInitializeEvent(&block->nmb_resetevent, EVENT_TYPE_NOTIFY, TRUE);
1373 InitializeListHead(&block->nmb_parmlist);
1374 InitializeListHead(&block->nmb_returnlist);
1375 block->nmb_returnitem = IoAllocateWorkItem(fdo);
1378 * Stash pointers to the miniport block and miniport
1379 * characteristics info in the if_ndis softc so the
1380 * UNIX wrapper driver can get to them later.
1382 sc->ndis_block = block;
1383 sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1);
1386 * If the driver has a MiniportTransferData() function,
1387 * we should allocate a private RX packet pool.
1390 if (sc->ndis_chars->nmc_transferdata_func != NULL) {
1391 NdisAllocatePacketPool(&status, &block->nmb_rxpool,
1392 32, PROTOCOL_RESERVED_SIZE_IN_PACKET);
1393 if (status != NDIS_STATUS_SUCCESS) {
1394 IoDetachDevice(block->nmb_nextdeviceobj);
1395 IoDeleteDevice(fdo);
1398 InitializeListHead((&block->nmb_packetlist));
1401 /* Give interrupt handling priority over timers. */
1402 IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1403 KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
1405 /* Finish up BSD-specific setup. */
1407 block->nmb_signature = (void *)0xcafebabe;
1408 block->nmb_status_func = kernndis_functbl[0].ipt_wrap;
1409 block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap;
1410 block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap;
1411 block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap;
1412 block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap;
1413 block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap;
1414 block->nmb_pendingreq = NULL;
1416 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
1418 return (STATUS_SUCCESS);
1422 ndis_unload_driver(arg)
1425 struct ndis_softc *sc;
1430 if (sc->ndis_intrhand)
1431 bus_teardown_intr(sc->ndis_dev,
1432 sc->ndis_irq, sc->ndis_intrhand);
1434 if (sc->ndis_block->nmb_rlist != NULL)
1435 free(sc->ndis_block->nmb_rlist, M_DEVBUF);
1437 ndis_flush_sysctls(sc);
1439 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1441 if (sc->ndis_chars->nmc_transferdata_func != NULL)
1442 NdisFreePacketPool(sc->ndis_block->nmb_rxpool);
1443 fdo = sc->ndis_block->nmb_deviceobj;
1444 IoFreeWorkItem(sc->ndis_block->nmb_returnitem);
1445 IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj);
1446 IoDeleteDevice(fdo);