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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/unistd.h>
39 #include <sys/types.h>
40 #include <sys/errno.h>
41 #include <sys/callout.h>
42 #include <sys/socket.h>
43 #include <sys/queue.h>
44 #include <sys/sysctl.h>
46 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/kernel.h>
52 #include <sys/module.h>
53 #include <sys/kthread.h>
54 #include <machine/bus.h>
55 #include <machine/resource.h>
60 #include <net/if_var.h>
61 #include <net/if_arp.h>
62 #include <net/ethernet.h>
63 #include <net/if_dl.h>
64 #include <net/if_media.h>
66 #include <net80211/ieee80211_var.h>
67 #include <net80211/ieee80211_ioctl.h>
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
72 #include <compat/ndis/pe_var.h>
73 #include <compat/ndis/cfg_var.h>
74 #include <compat/ndis/resource_var.h>
75 #include <compat/ndis/ntoskrnl_var.h>
76 #include <compat/ndis/ndis_var.h>
77 #include <compat/ndis/hal_var.h>
78 #include <compat/ndis/usbd_var.h>
79 #include <dev/if_ndis/if_ndisvar.h>
81 #define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
82 #define NDIS_FLAG_RDONLY 1
84 static void ndis_status_func(ndis_handle, ndis_status, void *, uint32_t);
85 static void ndis_statusdone_func(ndis_handle);
86 static void ndis_setdone_func(ndis_handle, ndis_status);
87 static void ndis_getdone_func(ndis_handle, ndis_status);
88 static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
89 static void ndis_sendrsrcavail_func(ndis_handle);
90 static void ndis_intrsetup(kdpc *, device_object *,
91 irp *, struct ndis_softc *);
92 static void ndis_return(device_object *, void *);
94 static image_patch_table kernndis_functbl[] = {
95 IMPORT_SFUNC(ndis_status_func, 4),
96 IMPORT_SFUNC(ndis_statusdone_func, 1),
97 IMPORT_SFUNC(ndis_setdone_func, 2),
98 IMPORT_SFUNC(ndis_getdone_func, 2),
99 IMPORT_SFUNC(ndis_resetdone_func, 3),
100 IMPORT_SFUNC(ndis_sendrsrcavail_func, 1),
101 IMPORT_SFUNC(ndis_intrsetup, 4),
102 IMPORT_SFUNC(ndis_return, 1),
107 static struct nd_head ndis_devhead;
110 * This allows us to export our symbols to other modules.
111 * Note that we call ourselves 'ndisapi' to avoid a namespace
112 * collision with if_ndis.ko, which internally calls itself
115 * Note: some of the subsystems depend on each other, so the
116 * order in which they're started is important. The order of
119 * HAL - spinlocks and IRQL manipulation
120 * ntoskrnl - DPC and workitem threads, object waiting
121 * windrv - driver/device registration
123 * The HAL should also be the last thing shut down, since
124 * the ntoskrnl subsystem will use spinlocks right up until
125 * the DPC and workitem threads are terminated.
129 ndis_modevent(module_t mod, int cmd, void *arg)
132 image_patch_table *patch;
136 /* Initialize subsystems */
143 patch = kernndis_functbl;
144 while (patch->ipt_func != NULL) {
145 windrv_wrap((funcptr)patch->ipt_func,
146 (funcptr *)&patch->ipt_wrap,
147 patch->ipt_argcnt, patch->ipt_ftype);
151 TAILQ_INIT(&ndis_devhead);
154 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
155 /* Shut down subsystems */
162 patch = kernndis_functbl;
163 while (patch->ipt_func != NULL) {
164 windrv_unwrap(patch->ipt_wrap);
170 /* Shut down subsystems */
177 patch = kernndis_functbl;
178 while (patch->ipt_func != NULL) {
179 windrv_unwrap(patch->ipt_wrap);
191 DEV_MODULE(ndisapi, ndis_modevent, NULL);
192 MODULE_VERSION(ndisapi, 1);
195 ndis_sendrsrcavail_func(adapter)
201 ndis_status_func(adapter, status, sbuf, slen)
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(adapter)
222 ndis_miniport_block *block;
223 struct ndis_softc *sc;
227 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
229 if (ifp->if_flags & IFF_DEBUG)
230 device_printf(sc->ndis_dev, "status complete\n");
234 ndis_setdone_func(adapter, status)
238 ndis_miniport_block *block;
241 block->nmb_setstat = status;
242 KeSetEvent(&block->nmb_setevent, IO_NO_INCREMENT, FALSE);
246 ndis_getdone_func(adapter, status)
250 ndis_miniport_block *block;
253 block->nmb_getstat = status;
254 KeSetEvent(&block->nmb_getevent, IO_NO_INCREMENT, FALSE);
258 ndis_resetdone_func(ndis_handle adapter, ndis_status status,
259 uint8_t addressingreset)
261 ndis_miniport_block *block;
262 struct ndis_softc *sc;
266 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
269 if (ifp->if_flags & IFF_DEBUG)
270 device_printf(sc->ndis_dev, "reset done...\n");
271 KeSetEvent(&block->nmb_resetevent, IO_NO_INCREMENT, FALSE);
275 ndis_create_sysctls(arg)
278 struct ndis_softc *sc;
281 struct sysctl_oid *oidp;
282 struct sysctl_ctx_entry *e;
288 vals = sc->ndis_regvals;
290 TAILQ_INIT(&sc->ndis_cfglist_head);
292 /* Add the driver-specific registry keys. */
295 if (vals->nc_cfgkey == NULL)
298 if (vals->nc_idx != sc->ndis_devidx) {
303 /* See if we already have a sysctl with this name */
306 TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
308 if (strcasecmp(oidp->oid_name, vals->nc_cfgkey) == 0)
318 ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
319 vals->nc_val, CTLFLAG_RW);
323 /* Now add a couple of builtin keys. */
326 * Environment can be either Windows (0) or WindowsNT (1).
327 * We qualify as the latter.
329 ndis_add_sysctl(sc, "Environment",
330 "Windows environment", "1", NDIS_FLAG_RDONLY);
332 /* NDIS version should be 5.1. */
333 ndis_add_sysctl(sc, "NdisVersion",
334 "NDIS API Version", "0x00050001", NDIS_FLAG_RDONLY);
337 * Some miniport drivers rely on the existence of the SlotNumber,
338 * NetCfgInstanceId and DriverDesc keys.
340 ndis_add_sysctl(sc, "SlotNumber", "Slot Numer", "01", NDIS_FLAG_RDONLY);
341 ndis_add_sysctl(sc, "NetCfgInstanceId", "NetCfgInstanceId",
342 "{12345678-1234-5678-CAFE0-123456789ABC}", NDIS_FLAG_RDONLY);
343 ndis_add_sysctl(sc, "DriverDesc", "Driver Description",
344 "NDIS Network Adapter", NDIS_FLAG_RDONLY);
346 /* Bus type (PCI, PCMCIA, etc...) */
347 sprintf(buf, "%d", (int)sc->ndis_iftype);
348 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, NDIS_FLAG_RDONLY);
350 if (sc->ndis_res_io != NULL) {
351 sprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
352 ndis_add_sysctl(sc, "IOBaseAddress",
353 "Base I/O Address", buf, NDIS_FLAG_RDONLY);
356 if (sc->ndis_irq != NULL) {
357 sprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
358 ndis_add_sysctl(sc, "InterruptNumber",
359 "Interrupt Number", buf, NDIS_FLAG_RDONLY);
366 ndis_add_sysctl(arg, key, desc, val, flag_rdonly)
373 struct ndis_softc *sc;
374 struct ndis_cfglist *cfg;
379 cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO);
382 printf("failed for %s\n", key);
386 cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF);
388 snprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
389 cfg->ndis_cfg.nc_cfgdesc = strdup(descstr, M_DEVBUF);
391 cfg->ndis_cfg.nc_cfgdesc = strdup(desc, M_DEVBUF);
392 strcpy(cfg->ndis_cfg.nc_val, val);
394 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
396 if (flag_rdonly != 0) {
398 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
399 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
400 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, CTLFLAG_RD,
401 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
402 cfg->ndis_cfg.nc_cfgdesc);
405 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
406 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
407 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, CTLFLAG_RW,
408 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
409 cfg->ndis_cfg.nc_cfgdesc);
415 * Somewhere, somebody decided "hey, let's automatically create
416 * a sysctl tree for each device instance as it's created -- it'll
417 * make life so much easier!" Lies. Why must they turn the kernel
418 * into a house of lies?
422 ndis_flush_sysctls(arg)
425 struct ndis_softc *sc;
426 struct ndis_cfglist *cfg;
427 struct sysctl_ctx_list *clist;
431 clist = device_get_sysctl_ctx(sc->ndis_dev);
433 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
434 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
435 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
436 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
437 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
438 free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
439 free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
447 ndis_get_routine_address(functbl, name)
448 struct image_patch_table *functbl;
453 for (i = 0; functbl[i].ipt_name != NULL; i++)
454 if (strcmp(name, functbl[i].ipt_name) == 0)
455 return (functbl[i].ipt_wrap);
460 ndis_return(dobj, arg)
464 ndis_miniport_block *block;
465 ndis_miniport_characteristics *ch;
466 ndis_return_handler returnfunc;
473 ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
476 adapter = block->nmb_miniportadapterctx;
481 returnfunc = ch->nmc_return_packet_func;
483 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
484 while (!IsListEmpty(&block->nmb_returnlist)) {
485 l = RemoveHeadList((&block->nmb_returnlist));
486 p = CONTAINING_RECORD(l, ndis_packet, np_list);
487 InitializeListHead((&p->np_list));
488 KeReleaseSpinLock(&block->nmb_returnlock, irql);
489 MSCALL2(returnfunc, adapter, p);
490 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
492 KeReleaseSpinLock(&block->nmb_returnlock, irql);
496 ndis_return_packet(struct mbuf *m, void *buf, void *arg)
499 ndis_miniport_block *block;
506 /* Decrement refcount. */
509 /* Release packet when refcount hits zero, otherwise return. */
513 block = ((struct ndis_softc *)p->np_softc)->ndis_block;
515 KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
516 InitializeListHead((&p->np_list));
517 InsertHeadList((&block->nmb_returnlist), (&p->np_list));
518 KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
520 IoQueueWorkItem(block->nmb_returnitem,
521 (io_workitem_func)kernndis_functbl[7].ipt_wrap,
522 WORKQUEUE_CRITICAL, block);
548 ndis_free_bufs(p->np_private.npp_head);
553 ndis_convert_res(arg)
556 struct ndis_softc *sc;
557 ndis_resource_list *rl = NULL;
558 cm_partial_resource_desc *prd = NULL;
559 ndis_miniport_block *block;
561 struct resource_list *brl;
562 struct resource_list_entry *brle;
566 block = sc->ndis_block;
569 rl = malloc(sizeof(ndis_resource_list) +
570 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
571 M_DEVBUF, M_NOWAIT|M_ZERO);
576 rl->cprl_version = 5;
577 rl->cprl_revision = 1;
578 rl->cprl_count = sc->ndis_rescnt;
579 prd = rl->cprl_partial_descs;
581 brl = BUS_GET_RESOURCE_LIST(dev, dev);
585 STAILQ_FOREACH(brle, brl, link) {
586 switch (brle->type) {
588 prd->cprd_type = CmResourceTypePort;
589 prd->cprd_flags = CM_RESOURCE_PORT_IO;
590 prd->cprd_sharedisp =
591 CmResourceShareDeviceExclusive;
592 prd->u.cprd_port.cprd_start.np_quad =
594 prd->u.cprd_port.cprd_len = brle->count;
597 prd->cprd_type = CmResourceTypeMemory;
599 CM_RESOURCE_MEMORY_READ_WRITE;
600 prd->cprd_sharedisp =
601 CmResourceShareDeviceExclusive;
602 prd->u.cprd_mem.cprd_start.np_quad =
604 prd->u.cprd_mem.cprd_len = brle->count;
607 prd->cprd_type = CmResourceTypeInterrupt;
610 * Always mark interrupt resources as
611 * shared, since in our implementation,
614 prd->cprd_sharedisp =
615 CmResourceShareShared;
616 prd->u.cprd_intr.cprd_level = brle->start;
617 prd->u.cprd_intr.cprd_vector = brle->start;
618 prd->u.cprd_intr.cprd_affinity = 0;
627 block->nmb_rlist = rl;
633 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
634 * packet, it will hand it to us in the form of an ndis_packet,
635 * which we need to convert to an mbuf that is then handed off
636 * to the stack. Note: we configure the mbuf list so that it uses
637 * the memory regions specified by the ndis_buffer structures in
638 * the ndis_packet as external storage. In most cases, this will
639 * point to a memory region allocated by the driver (either by
640 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
641 * the driver to handle free()ing this region for is, so we set up
642 * a dummy no-op free handler for it.
650 struct mbuf *m = NULL, *prev = NULL;
652 ndis_packet_private *priv;
655 struct ether_header *eh;
658 if (p == NULL || m0 == NULL)
661 priv = &p->np_private;
662 buf = priv->npp_head;
665 for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
666 if (buf == priv->npp_head)
667 m = m_gethdr(M_NOWAIT, MT_DATA);
669 m = m_get(M_NOWAIT, MT_DATA);
675 m->m_len = MmGetMdlByteCount(buf);
676 m->m_data = MmGetMdlVirtualAddress(buf);
677 MEXTADD(m, m->m_data, m->m_len, ndis_return_packet,
678 m->m_data, p, 0, EXT_NDIS);
682 if (m->m_flags & M_PKTHDR)
690 * This is a hack to deal with the Marvell 8335 driver
691 * which, when associated with an AP in WPA-PSK mode,
692 * seems to overpad its frames by 8 bytes. I don't know
693 * that the extra 8 bytes are for, and they're not there
694 * in open mode, so for now clamp the frame size at 1514
695 * until I can figure out how to deal with this properly,
696 * otherwise if_ethersubr() will spank us by discarding
697 * the 'oversize' frames.
700 eh = mtod((*m0), struct ether_header *);
701 ifp = ((struct ndis_softc *)p->np_softc)->ifp;
702 if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
703 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
707 (*m0)->m_pkthdr.len = totlen;
713 * Create an NDIS packet from an mbuf chain.
714 * This is used mainly when transmitting packets, where we need
715 * to turn an mbuf off an interface's send queue and transform it
716 * into an NDIS packet which will be fed into the NDIS driver's
719 * NDIS packets consist of two parts: an ndis_packet structure,
720 * which is vaguely analagous to the pkthdr portion of an mbuf,
721 * and one or more ndis_buffer structures, which define the
722 * actual memory segments in which the packet data resides.
723 * We need to allocate one ndis_buffer for each mbuf in a chain,
724 * plus one ndis_packet as the header.
733 ndis_buffer *buf = NULL, *prev = NULL;
734 ndis_packet_private *priv;
736 if (p == NULL || *p == NULL || m0 == NULL)
739 priv = &(*p)->np_private;
740 priv->npp_totlen = m0->m_pkthdr.len;
742 for (m = m0; m != NULL; m = m->m_next) {
745 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
747 ndis_free_packet(*p);
751 MmBuildMdlForNonPagedPool(buf);
753 if (priv->npp_head == NULL)
754 priv->npp_head = buf;
756 prev->mdl_next = buf;
760 priv->npp_tail = buf;
766 ndis_get_supported_oids(arg, oids, oidcnt)
774 if (arg == NULL || oids == NULL || oidcnt == NULL)
777 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
779 o = malloc(len, M_DEVBUF, M_NOWAIT);
783 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
797 ndis_set_info(arg, oid, buf, buflen)
803 struct ndis_softc *sc;
806 ndis_setinfo_handler setfunc;
807 uint32_t byteswritten = 0, bytesneeded = 0;
812 * According to the NDIS spec, MiniportQueryInformation()
813 * and MiniportSetInformation() requests are handled serially:
814 * once one request has been issued, we must wait for it to
815 * finish before allowing another request to proceed.
820 KeResetEvent(&sc->ndis_block->nmb_setevent);
822 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
824 if (sc->ndis_block->nmb_pendingreq != NULL) {
825 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
826 panic("ndis_set_info() called while other request pending");
828 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
830 setfunc = sc->ndis_chars->nmc_setinfo_func;
831 adapter = sc->ndis_block->nmb_miniportadapterctx;
833 if (adapter == NULL || setfunc == NULL ||
834 sc->ndis_block->nmb_devicectx == NULL) {
835 sc->ndis_block->nmb_pendingreq = NULL;
836 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
840 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
841 &byteswritten, &bytesneeded);
843 sc->ndis_block->nmb_pendingreq = NULL;
845 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
847 if (rval == NDIS_STATUS_PENDING) {
848 /* Wait up to 5 seconds. */
849 duetime = (5 * 1000000) * -10;
850 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
851 0, 0, FALSE, &duetime);
852 rval = sc->ndis_block->nmb_setstat;
856 *buflen = byteswritten;
858 *buflen = bytesneeded;
860 if (rval == NDIS_STATUS_INVALID_LENGTH)
863 if (rval == NDIS_STATUS_INVALID_OID)
866 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
867 rval == NDIS_STATUS_NOT_ACCEPTED)
870 if (rval != NDIS_STATUS_SUCCESS)
876 typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
879 ndis_send_packets(arg, packets, cnt)
881 ndis_packet **packets;
884 struct ndis_softc *sc;
886 ndis_sendmulti_handler sendfunc;
887 ndis_senddone_func senddonefunc;
893 adapter = sc->ndis_block->nmb_miniportadapterctx;
896 sendfunc = sc->ndis_chars->nmc_sendmulti_func;
897 senddonefunc = sc->ndis_block->nmb_senddone_func;
899 if (NDIS_SERIALIZED(sc->ndis_block))
900 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
902 MSCALL3(sendfunc, adapter, packets, cnt);
904 for (i = 0; i < cnt; i++) {
907 * Either the driver already handed the packet to
908 * ndis_txeof() due to a failure, or it wants to keep
909 * it and release it asynchronously later. Skip to the
912 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
914 MSCALL3(senddonefunc, sc->ndis_block, p, p->np_oob.npo_status);
917 if (NDIS_SERIALIZED(sc->ndis_block))
918 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
924 ndis_send_packet(arg, packet)
928 struct ndis_softc *sc;
931 ndis_sendsingle_handler sendfunc;
932 ndis_senddone_func senddonefunc;
936 adapter = sc->ndis_block->nmb_miniportadapterctx;
939 sendfunc = sc->ndis_chars->nmc_sendsingle_func;
940 senddonefunc = sc->ndis_block->nmb_senddone_func;
942 if (NDIS_SERIALIZED(sc->ndis_block))
943 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
944 status = MSCALL3(sendfunc, adapter, packet,
945 packet->np_private.npp_flags);
947 if (status == NDIS_STATUS_PENDING) {
948 if (NDIS_SERIALIZED(sc->ndis_block))
949 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
953 MSCALL3(senddonefunc, sc->ndis_block, packet, status);
955 if (NDIS_SERIALIZED(sc->ndis_block))
956 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
965 struct ndis_softc *sc;
970 sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
971 M_DEVBUF, M_NOWAIT|M_ZERO);
973 if (sc->ndis_tmaps == NULL)
976 for (i = 0; i < sc->ndis_maxpkts; i++) {
977 error = bus_dmamap_create(sc->ndis_ttag, 0,
980 free(sc->ndis_tmaps, M_DEVBUF);
989 ndis_destroy_dma(arg)
992 struct ndis_softc *sc;
994 ndis_packet *p = NULL;
999 for (i = 0; i < sc->ndis_maxpkts; i++) {
1000 if (sc->ndis_txarray[i] != NULL) {
1001 p = sc->ndis_txarray[i];
1002 m = (struct mbuf *)p->np_rsvd[1];
1005 ndis_free_packet(sc->ndis_txarray[i]);
1007 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
1010 free(sc->ndis_tmaps, M_DEVBUF);
1012 bus_dma_tag_destroy(sc->ndis_ttag);
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,
1063 struct ndis_softc *sc;
1064 ndis_handle adapter;
1065 ndis_halt_handler haltfunc;
1066 ndis_miniport_block *block;
1071 block = sc->ndis_block;
1074 KeFlushQueuedDpcs();
1077 * Wait for all packets to be returned.
1081 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
1082 empty = IsListEmpty(&block->nmb_returnlist);
1083 KeReleaseSpinLock(&block->nmb_returnlock, irql);
1090 adapter = sc->ndis_block->nmb_miniportadapterctx;
1091 if (adapter == NULL) {
1096 sc->ndis_block->nmb_devicectx = NULL;
1099 * The adapter context is only valid after the init
1100 * handler has been called, and is invalid once the
1101 * halt handler has been called.
1104 haltfunc = sc->ndis_chars->nmc_halt_func;
1107 MSCALL1(haltfunc, adapter);
1110 sc->ndis_block->nmb_miniportadapterctx = NULL;
1117 ndis_shutdown_nic(arg)
1120 struct ndis_softc *sc;
1121 ndis_handle adapter;
1122 ndis_shutdown_handler shutdownfunc;
1126 adapter = sc->ndis_block->nmb_miniportadapterctx;
1127 shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1129 if (adapter == NULL || shutdownfunc == NULL)
1132 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1133 MSCALL1(shutdownfunc, adapter);
1135 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
1137 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1143 ndis_pnpevent_nic(arg, type)
1148 struct ndis_softc *sc;
1149 ndis_handle adapter;
1150 ndis_pnpevent_handler pnpeventfunc;
1153 sc = device_get_softc(arg);
1155 adapter = sc->ndis_block->nmb_miniportadapterctx;
1156 pnpeventfunc = sc->ndis_chars->nmc_pnpevent_handler;
1158 if (adapter == NULL || pnpeventfunc == NULL)
1161 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1162 MSCALL4(pnpeventfunc, adapter, type, NULL, 0);
1164 MSCALL4(pnpeventfunc, sc->ndis_chars->nmc_rsvd0, type, NULL, 0);
1173 struct ndis_softc *sc;
1174 ndis_miniport_block *block;
1175 ndis_init_handler initfunc;
1176 ndis_status status, openstatus = 0;
1177 ndis_medium mediumarray[NdisMediumMax];
1178 uint32_t chosenmedium, i;
1185 block = sc->ndis_block;
1186 initfunc = sc->ndis_chars->nmc_init_func;
1189 sc->ndis_block->nmb_timerlist = NULL;
1191 for (i = 0; i < NdisMediumMax; i++)
1194 status = MSCALL6(initfunc, &openstatus, &chosenmedium,
1195 mediumarray, NdisMediumMax, block, block);
1198 * If the init fails, blow away the other exported routines
1199 * we obtained from the driver so we can't call them later.
1200 * If the init failed, none of these will work.
1202 if (status != NDIS_STATUS_SUCCESS) {
1204 sc->ndis_block->nmb_miniportadapterctx = NULL;
1210 * This may look really goofy, but apparently it is possible
1211 * to halt a miniport too soon after it's been initialized.
1212 * After MiniportInitialize() finishes, pause for 1 second
1213 * to give the chip a chance to handle any short-lived timers
1214 * that were set in motion. If we call MiniportHalt() too soon,
1215 * some of the timers may not be cancelled, because the driver
1216 * expects them to fire before the halt is called.
1219 pause("ndwait", hz);
1222 sc->ndis_block->nmb_devicectx = sc;
1229 ndis_intrsetup(dpc, dobj, ip, sc)
1231 device_object *dobj;
1233 struct ndis_softc *sc;
1235 ndis_miniport_interrupt *intr;
1237 intr = sc->ndis_block->nmb_interrupt;
1244 KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
1245 KeResetEvent(&intr->ni_dpcevt);
1246 if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
1248 KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
1252 ndis_get_info(arg, oid, buf, buflen)
1258 struct ndis_softc *sc;
1260 ndis_handle adapter;
1261 ndis_queryinfo_handler queryfunc;
1262 uint32_t byteswritten = 0, bytesneeded = 0;
1268 KeResetEvent(&sc->ndis_block->nmb_getevent);
1270 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1272 if (sc->ndis_block->nmb_pendingreq != NULL) {
1273 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1274 panic("ndis_get_info() called while other request pending");
1276 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
1278 queryfunc = sc->ndis_chars->nmc_queryinfo_func;
1279 adapter = sc->ndis_block->nmb_miniportadapterctx;
1281 if (adapter == NULL || queryfunc == NULL ||
1282 sc->ndis_block->nmb_devicectx == NULL) {
1283 sc->ndis_block->nmb_pendingreq = NULL;
1284 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1288 rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
1289 &byteswritten, &bytesneeded);
1291 sc->ndis_block->nmb_pendingreq = NULL;
1293 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1295 /* Wait for requests that block. */
1297 if (rval == NDIS_STATUS_PENDING) {
1298 /* Wait up to 5 seconds. */
1299 duetime = (5 * 1000000) * -10;
1300 KeWaitForSingleObject(&sc->ndis_block->nmb_getevent,
1301 0, 0, FALSE, &duetime);
1302 rval = sc->ndis_block->nmb_getstat;
1306 *buflen = byteswritten;
1308 *buflen = bytesneeded;
1310 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1311 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1314 if (rval == NDIS_STATUS_INVALID_OID)
1317 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1318 rval == NDIS_STATUS_NOT_ACCEPTED)
1321 if (rval != NDIS_STATUS_SUCCESS)
1328 NdisAddDevice(drv, pdo)
1333 ndis_miniport_block *block;
1334 struct ndis_softc *sc;
1338 sc = device_get_softc(pdo->do_devext);
1340 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
1341 error = bus_setup_intr(sc->ndis_dev, sc->ndis_irq,
1342 INTR_TYPE_NET | INTR_MPSAFE,
1343 NULL, ntoskrnl_intr, NULL, &sc->ndis_intrhand);
1345 return (NDIS_STATUS_FAILURE);
1348 status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1349 FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
1351 if (status != STATUS_SUCCESS)
1354 block = fdo->do_devext;
1356 block->nmb_filterdbs.nf_ethdb = block;
1357 block->nmb_deviceobj = fdo;
1358 block->nmb_physdeviceobj = pdo;
1359 block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
1360 KeInitializeSpinLock(&block->nmb_lock);
1361 KeInitializeSpinLock(&block->nmb_returnlock);
1362 KeInitializeEvent(&block->nmb_getevent, EVENT_TYPE_NOTIFY, TRUE);
1363 KeInitializeEvent(&block->nmb_setevent, EVENT_TYPE_NOTIFY, TRUE);
1364 KeInitializeEvent(&block->nmb_resetevent, EVENT_TYPE_NOTIFY, TRUE);
1365 InitializeListHead(&block->nmb_parmlist);
1366 InitializeListHead(&block->nmb_returnlist);
1367 block->nmb_returnitem = IoAllocateWorkItem(fdo);
1370 * Stash pointers to the miniport block and miniport
1371 * characteristics info in the if_ndis softc so the
1372 * UNIX wrapper driver can get to them later.
1374 sc->ndis_block = block;
1375 sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1);
1378 * If the driver has a MiniportTransferData() function,
1379 * we should allocate a private RX packet pool.
1382 if (sc->ndis_chars->nmc_transferdata_func != NULL) {
1383 NdisAllocatePacketPool(&status, &block->nmb_rxpool,
1384 32, PROTOCOL_RESERVED_SIZE_IN_PACKET);
1385 if (status != NDIS_STATUS_SUCCESS) {
1386 IoDetachDevice(block->nmb_nextdeviceobj);
1387 IoDeleteDevice(fdo);
1390 InitializeListHead((&block->nmb_packetlist));
1393 /* Give interrupt handling priority over timers. */
1394 IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1395 KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
1397 /* Finish up BSD-specific setup. */
1399 block->nmb_signature = (void *)0xcafebabe;
1400 block->nmb_status_func = kernndis_functbl[0].ipt_wrap;
1401 block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap;
1402 block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap;
1403 block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap;
1404 block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap;
1405 block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap;
1406 block->nmb_pendingreq = NULL;
1408 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
1410 return (STATUS_SUCCESS);
1414 ndis_unload_driver(arg)
1417 struct ndis_softc *sc;
1422 if (sc->ndis_intrhand)
1423 bus_teardown_intr(sc->ndis_dev,
1424 sc->ndis_irq, sc->ndis_intrhand);
1426 if (sc->ndis_block->nmb_rlist != NULL)
1427 free(sc->ndis_block->nmb_rlist, M_DEVBUF);
1429 ndis_flush_sysctls(sc);
1431 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1433 if (sc->ndis_chars->nmc_transferdata_func != NULL)
1434 NdisFreePacketPool(sc->ndis_block->nmb_rxpool);
1435 fdo = sc->ndis_block->nmb_deviceobj;
1436 IoFreeWorkItem(sc->ndis_block->nmb_returnitem);
1437 IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj);
1438 IoDeleteDevice(fdo);