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_arp.h>
61 #include <net/ethernet.h>
62 #include <net/if_dl.h>
63 #include <net/if_media.h>
65 #include <net80211/ieee80211_var.h>
66 #include <net80211/ieee80211_ioctl.h>
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
71 #include <compat/ndis/pe_var.h>
72 #include <compat/ndis/cfg_var.h>
73 #include <compat/ndis/resource_var.h>
74 #include <compat/ndis/ntoskrnl_var.h>
75 #include <compat/ndis/ndis_var.h>
76 #include <compat/ndis/hal_var.h>
77 #include <compat/ndis/usbd_var.h>
78 #include <dev/if_ndis/if_ndisvar.h>
80 #define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
82 static void ndis_status_func(ndis_handle, ndis_status, void *, uint32_t);
83 static void ndis_statusdone_func(ndis_handle);
84 static void ndis_setdone_func(ndis_handle, ndis_status);
85 static void ndis_getdone_func(ndis_handle, ndis_status);
86 static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
87 static void ndis_sendrsrcavail_func(ndis_handle);
88 static void ndis_intrsetup(kdpc *, device_object *,
89 irp *, struct ndis_softc *);
90 static void ndis_return(device_object *, void *);
92 static image_patch_table kernndis_functbl[] = {
93 IMPORT_SFUNC(ndis_status_func, 4),
94 IMPORT_SFUNC(ndis_statusdone_func, 1),
95 IMPORT_SFUNC(ndis_setdone_func, 2),
96 IMPORT_SFUNC(ndis_getdone_func, 2),
97 IMPORT_SFUNC(ndis_resetdone_func, 3),
98 IMPORT_SFUNC(ndis_sendrsrcavail_func, 1),
99 IMPORT_SFUNC(ndis_intrsetup, 4),
100 IMPORT_SFUNC(ndis_return, 1),
105 static struct nd_head ndis_devhead;
108 * This allows us to export our symbols to other modules.
109 * Note that we call ourselves 'ndisapi' to avoid a namespace
110 * collision with if_ndis.ko, which internally calls itself
113 * Note: some of the subsystems depend on each other, so the
114 * order in which they're started is important. The order of
117 * HAL - spinlocks and IRQL manipulation
118 * ntoskrnl - DPC and workitem threads, object waiting
119 * windrv - driver/device registration
121 * The HAL should also be the last thing shut down, since
122 * the ntoskrnl subsystem will use spinlocks right up until
123 * the DPC and workitem threads are terminated.
127 ndis_modevent(module_t mod, int cmd, void *arg)
130 image_patch_table *patch;
134 /* Initialize subsystems */
141 patch = kernndis_functbl;
142 while (patch->ipt_func != NULL) {
143 windrv_wrap((funcptr)patch->ipt_func,
144 (funcptr *)&patch->ipt_wrap,
145 patch->ipt_argcnt, patch->ipt_ftype);
149 TAILQ_INIT(&ndis_devhead);
152 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
153 /* Shut down subsystems */
160 patch = kernndis_functbl;
161 while (patch->ipt_func != NULL) {
162 windrv_unwrap(patch->ipt_wrap);
168 /* Shut down subsystems */
175 patch = kernndis_functbl;
176 while (patch->ipt_func != NULL) {
177 windrv_unwrap(patch->ipt_wrap);
189 DEV_MODULE(ndisapi, ndis_modevent, NULL);
190 MODULE_VERSION(ndisapi, 1);
193 ndis_sendrsrcavail_func(adapter)
199 ndis_status_func(adapter, status, sbuf, slen)
205 ndis_miniport_block *block;
206 struct ndis_softc *sc;
210 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
212 if (ifp->if_flags & IFF_DEBUG)
213 device_printf(sc->ndis_dev, "status: %x\n", status);
217 ndis_statusdone_func(adapter)
220 ndis_miniport_block *block;
221 struct ndis_softc *sc;
225 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
227 if (ifp->if_flags & IFF_DEBUG)
228 device_printf(sc->ndis_dev, "status complete\n");
232 ndis_setdone_func(adapter, status)
236 ndis_miniport_block *block;
239 block->nmb_setstat = status;
240 KeSetEvent(&block->nmb_setevent, IO_NO_INCREMENT, FALSE);
244 ndis_getdone_func(adapter, status)
248 ndis_miniport_block *block;
251 block->nmb_getstat = status;
252 KeSetEvent(&block->nmb_getevent, IO_NO_INCREMENT, FALSE);
256 ndis_resetdone_func(ndis_handle adapter, ndis_status status,
257 uint8_t addressingreset)
259 ndis_miniport_block *block;
260 struct ndis_softc *sc;
264 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
267 if (ifp->if_flags & IFF_DEBUG)
268 device_printf(sc->ndis_dev, "reset done...\n");
269 KeSetEvent(&block->nmb_resetevent, IO_NO_INCREMENT, FALSE);
273 ndis_create_sysctls(arg)
276 struct ndis_softc *sc;
279 struct sysctl_oid *oidp;
280 struct sysctl_ctx_entry *e;
286 vals = sc->ndis_regvals;
288 TAILQ_INIT(&sc->ndis_cfglist_head);
290 /* Add the driver-specific registry keys. */
293 if (vals->nc_cfgkey == NULL)
296 if (vals->nc_idx != sc->ndis_devidx) {
301 /* See if we already have a sysctl with this name */
304 TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
306 if (strcasecmp(oidp->oid_name, vals->nc_cfgkey) == 0)
316 ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
317 vals->nc_val, CTLFLAG_RW);
321 /* Now add a couple of builtin keys. */
324 * Environment can be either Windows (0) or WindowsNT (1).
325 * We qualify as the latter.
327 ndis_add_sysctl(sc, "Environment",
328 "Windows environment", "1", CTLFLAG_RD);
330 /* NDIS version should be 5.1. */
331 ndis_add_sysctl(sc, "NdisVersion",
332 "NDIS API Version", "0x00050001", CTLFLAG_RD);
335 * Some miniport drivers rely on the existence of the SlotNumber,
336 * NetCfgInstanceId and DriverDesc keys.
338 ndis_add_sysctl(sc, "SlotNumber", "Slot Numer", "01", CTLFLAG_RD);
339 ndis_add_sysctl(sc, "NetCfgInstanceId", "NetCfgInstanceId",
340 "{12345678-1234-5678-CAFE0-123456789ABC}", CTLFLAG_RD);
341 ndis_add_sysctl(sc, "DriverDesc", "Driver Description",
342 "NDIS Network Adapter", CTLFLAG_RD);
344 /* Bus type (PCI, PCMCIA, etc...) */
345 sprintf(buf, "%d", (int)sc->ndis_iftype);
346 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
348 if (sc->ndis_res_io != NULL) {
349 sprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
350 ndis_add_sysctl(sc, "IOBaseAddress",
351 "Base I/O Address", buf, CTLFLAG_RD);
354 if (sc->ndis_irq != NULL) {
355 sprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
356 ndis_add_sysctl(sc, "InterruptNumber",
357 "Interrupt Number", buf, CTLFLAG_RD);
364 ndis_add_sysctl(arg, key, desc, val, flag)
371 struct ndis_softc *sc;
372 struct ndis_cfglist *cfg;
377 cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO);
380 printf("failed for %s\n", key);
384 cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF);
386 snprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
387 cfg->ndis_cfg.nc_cfgdesc = strdup(descstr, M_DEVBUF);
389 cfg->ndis_cfg.nc_cfgdesc = strdup(desc, M_DEVBUF);
390 strcpy(cfg->ndis_cfg.nc_val, val);
392 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
395 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
396 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
397 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
398 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
399 cfg->ndis_cfg.nc_cfgdesc);
405 * Somewhere, somebody decided "hey, let's automatically create
406 * a sysctl tree for each device instance as it's created -- it'll
407 * make life so much easier!" Lies. Why must they turn the kernel
408 * into a house of lies?
412 ndis_flush_sysctls(arg)
415 struct ndis_softc *sc;
416 struct ndis_cfglist *cfg;
417 struct sysctl_ctx_list *clist;
421 clist = device_get_sysctl_ctx(sc->ndis_dev);
423 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
424 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
425 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
426 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
427 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
428 free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
429 free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
437 ndis_get_routine_address(functbl, name)
438 struct image_patch_table *functbl;
443 for (i = 0; functbl[i].ipt_name != NULL; i++)
444 if (strcmp(name, functbl[i].ipt_name) == 0)
445 return (functbl[i].ipt_wrap);
450 ndis_return(dobj, arg)
454 ndis_miniport_block *block;
455 ndis_miniport_characteristics *ch;
456 ndis_return_handler returnfunc;
463 ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
466 adapter = block->nmb_miniportadapterctx;
471 returnfunc = ch->nmc_return_packet_func;
473 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
474 while (!IsListEmpty(&block->nmb_returnlist)) {
475 l = RemoveHeadList((&block->nmb_returnlist));
476 p = CONTAINING_RECORD(l, ndis_packet, np_list);
477 InitializeListHead((&p->np_list));
478 KeReleaseSpinLock(&block->nmb_returnlock, irql);
479 MSCALL2(returnfunc, adapter, p);
480 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
482 KeReleaseSpinLock(&block->nmb_returnlock, irql);
486 ndis_return_packet(buf, arg)
487 void *buf; /* not used */
491 ndis_miniport_block *block;
498 /* Decrement refcount. */
501 /* Release packet when refcount hits zero, otherwise return. */
505 block = ((struct ndis_softc *)p->np_softc)->ndis_block;
507 KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
508 InitializeListHead((&p->np_list));
509 InsertHeadList((&block->nmb_returnlist), (&p->np_list));
510 KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
512 IoQueueWorkItem(block->nmb_returnitem,
513 (io_workitem_func)kernndis_functbl[7].ipt_wrap,
514 WORKQUEUE_CRITICAL, block);
540 ndis_free_bufs(p->np_private.npp_head);
545 ndis_convert_res(arg)
548 struct ndis_softc *sc;
549 ndis_resource_list *rl = NULL;
550 cm_partial_resource_desc *prd = NULL;
551 ndis_miniport_block *block;
553 struct resource_list *brl;
554 struct resource_list_entry *brle;
558 block = sc->ndis_block;
561 rl = malloc(sizeof(ndis_resource_list) +
562 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
563 M_DEVBUF, M_NOWAIT|M_ZERO);
568 rl->cprl_version = 5;
569 rl->cprl_revision = 1;
570 rl->cprl_count = sc->ndis_rescnt;
571 prd = rl->cprl_partial_descs;
573 brl = BUS_GET_RESOURCE_LIST(dev, dev);
577 STAILQ_FOREACH(brle, brl, link) {
578 switch (brle->type) {
580 prd->cprd_type = CmResourceTypePort;
581 prd->cprd_flags = CM_RESOURCE_PORT_IO;
582 prd->cprd_sharedisp =
583 CmResourceShareDeviceExclusive;
584 prd->u.cprd_port.cprd_start.np_quad =
586 prd->u.cprd_port.cprd_len = brle->count;
589 prd->cprd_type = CmResourceTypeMemory;
591 CM_RESOURCE_MEMORY_READ_WRITE;
592 prd->cprd_sharedisp =
593 CmResourceShareDeviceExclusive;
594 prd->u.cprd_mem.cprd_start.np_quad =
596 prd->u.cprd_mem.cprd_len = brle->count;
599 prd->cprd_type = CmResourceTypeInterrupt;
602 * Always mark interrupt resources as
603 * shared, since in our implementation,
606 prd->cprd_sharedisp =
607 CmResourceShareShared;
608 prd->u.cprd_intr.cprd_level = brle->start;
609 prd->u.cprd_intr.cprd_vector = brle->start;
610 prd->u.cprd_intr.cprd_affinity = 0;
619 block->nmb_rlist = rl;
625 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
626 * packet, it will hand it to us in the form of an ndis_packet,
627 * which we need to convert to an mbuf that is then handed off
628 * to the stack. Note: we configure the mbuf list so that it uses
629 * the memory regions specified by the ndis_buffer structures in
630 * the ndis_packet as external storage. In most cases, this will
631 * point to a memory region allocated by the driver (either by
632 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
633 * the driver to handle free()ing this region for is, so we set up
634 * a dummy no-op free handler for it.
642 struct mbuf *m = NULL, *prev = NULL;
644 ndis_packet_private *priv;
647 struct ether_header *eh;
650 if (p == NULL || m0 == NULL)
653 priv = &p->np_private;
654 buf = priv->npp_head;
657 for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
658 if (buf == priv->npp_head)
660 MGETHDR(m, M_DONTWAIT, MT_HEADER);
662 MGETHDR(m, M_DONTWAIT, MT_DATA);
665 MGET(m, M_DONTWAIT, MT_DATA);
671 m->m_len = MmGetMdlByteCount(buf);
672 m->m_data = MmGetMdlVirtualAddress(buf);
673 MEXTADD(m, m->m_data, m->m_len, ndis_return_packet,
674 m->m_data, p, 0, EXT_NDIS);
678 if (m->m_flags & M_PKTHDR)
686 * This is a hack to deal with the Marvell 8335 driver
687 * which, when associated with an AP in WPA-PSK mode,
688 * seems to overpad its frames by 8 bytes. I don't know
689 * that the extra 8 bytes are for, and they're not there
690 * in open mode, so for now clamp the frame size at 1514
691 * until I can figure out how to deal with this properly,
692 * otherwise if_ethersubr() will spank us by discarding
693 * the 'oversize' frames.
696 eh = mtod((*m0), struct ether_header *);
697 ifp = ((struct ndis_softc *)p->np_softc)->ifp;
698 if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
699 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
703 (*m0)->m_pkthdr.len = totlen;
709 * Create an NDIS packet from an mbuf chain.
710 * This is used mainly when transmitting packets, where we need
711 * to turn an mbuf off an interface's send queue and transform it
712 * into an NDIS packet which will be fed into the NDIS driver's
715 * NDIS packets consist of two parts: an ndis_packet structure,
716 * which is vaguely analagous to the pkthdr portion of an mbuf,
717 * and one or more ndis_buffer structures, which define the
718 * actual memory segments in which the packet data resides.
719 * We need to allocate one ndis_buffer for each mbuf in a chain,
720 * plus one ndis_packet as the header.
729 ndis_buffer *buf = NULL, *prev = NULL;
730 ndis_packet_private *priv;
732 if (p == NULL || *p == NULL || m0 == NULL)
735 priv = &(*p)->np_private;
736 priv->npp_totlen = m0->m_pkthdr.len;
738 for (m = m0; m != NULL; m = m->m_next) {
741 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
743 ndis_free_packet(*p);
747 MmBuildMdlForNonPagedPool(buf);
749 if (priv->npp_head == NULL)
750 priv->npp_head = buf;
752 prev->mdl_next = buf;
756 priv->npp_tail = buf;
762 ndis_get_supported_oids(arg, oids, oidcnt)
770 if (arg == NULL || oids == NULL || oidcnt == NULL)
773 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
775 o = malloc(len, M_DEVBUF, M_NOWAIT);
779 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
793 ndis_set_info(arg, oid, buf, buflen)
799 struct ndis_softc *sc;
802 ndis_setinfo_handler setfunc;
803 uint32_t byteswritten = 0, bytesneeded = 0;
808 * According to the NDIS spec, MiniportQueryInformation()
809 * and MiniportSetInformation() requests are handled serially:
810 * once one request has been issued, we must wait for it to
811 * finish before allowing another request to proceed.
816 KeResetEvent(&sc->ndis_block->nmb_setevent);
818 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
820 if (sc->ndis_block->nmb_pendingreq != NULL) {
821 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
822 panic("ndis_set_info() called while other request pending");
824 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
826 setfunc = sc->ndis_chars->nmc_setinfo_func;
827 adapter = sc->ndis_block->nmb_miniportadapterctx;
829 if (adapter == NULL || setfunc == NULL ||
830 sc->ndis_block->nmb_devicectx == NULL) {
831 sc->ndis_block->nmb_pendingreq = NULL;
832 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
836 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
837 &byteswritten, &bytesneeded);
839 sc->ndis_block->nmb_pendingreq = NULL;
841 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
843 if (rval == NDIS_STATUS_PENDING) {
844 /* Wait up to 5 seconds. */
845 duetime = (5 * 1000000) * -10;
846 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
847 0, 0, FALSE, &duetime);
848 rval = sc->ndis_block->nmb_setstat;
852 *buflen = byteswritten;
854 *buflen = bytesneeded;
856 if (rval == NDIS_STATUS_INVALID_LENGTH)
859 if (rval == NDIS_STATUS_INVALID_OID)
862 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
863 rval == NDIS_STATUS_NOT_ACCEPTED)
866 if (rval != NDIS_STATUS_SUCCESS)
872 typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
875 ndis_send_packets(arg, packets, cnt)
877 ndis_packet **packets;
880 struct ndis_softc *sc;
882 ndis_sendmulti_handler sendfunc;
883 ndis_senddone_func senddonefunc;
889 adapter = sc->ndis_block->nmb_miniportadapterctx;
892 sendfunc = sc->ndis_chars->nmc_sendmulti_func;
893 senddonefunc = sc->ndis_block->nmb_senddone_func;
895 if (NDIS_SERIALIZED(sc->ndis_block))
896 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
898 MSCALL3(sendfunc, adapter, packets, cnt);
900 for (i = 0; i < cnt; i++) {
903 * Either the driver already handed the packet to
904 * ndis_txeof() due to a failure, or it wants to keep
905 * it and release it asynchronously later. Skip to the
908 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
910 MSCALL3(senddonefunc, sc->ndis_block, p, p->np_oob.npo_status);
913 if (NDIS_SERIALIZED(sc->ndis_block))
914 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
920 ndis_send_packet(arg, packet)
924 struct ndis_softc *sc;
927 ndis_sendsingle_handler sendfunc;
928 ndis_senddone_func senddonefunc;
932 adapter = sc->ndis_block->nmb_miniportadapterctx;
935 sendfunc = sc->ndis_chars->nmc_sendsingle_func;
936 senddonefunc = sc->ndis_block->nmb_senddone_func;
938 if (NDIS_SERIALIZED(sc->ndis_block))
939 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
940 status = MSCALL3(sendfunc, adapter, packet,
941 packet->np_private.npp_flags);
943 if (status == NDIS_STATUS_PENDING) {
944 if (NDIS_SERIALIZED(sc->ndis_block))
945 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
949 MSCALL3(senddonefunc, sc->ndis_block, packet, status);
951 if (NDIS_SERIALIZED(sc->ndis_block))
952 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
961 struct ndis_softc *sc;
966 sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
967 M_DEVBUF, M_NOWAIT|M_ZERO);
969 if (sc->ndis_tmaps == NULL)
972 for (i = 0; i < sc->ndis_maxpkts; i++) {
973 error = bus_dmamap_create(sc->ndis_ttag, 0,
976 free(sc->ndis_tmaps, M_DEVBUF);
985 ndis_destroy_dma(arg)
988 struct ndis_softc *sc;
990 ndis_packet *p = NULL;
995 for (i = 0; i < sc->ndis_maxpkts; i++) {
996 if (sc->ndis_txarray[i] != NULL) {
997 p = sc->ndis_txarray[i];
998 m = (struct mbuf *)p->np_rsvd[1];
1001 ndis_free_packet(sc->ndis_txarray[i]);
1003 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
1006 free(sc->ndis_tmaps, M_DEVBUF);
1008 bus_dma_tag_destroy(sc->ndis_ttag);
1017 struct ndis_softc *sc;
1018 ndis_handle adapter;
1019 ndis_reset_handler resetfunc;
1020 uint8_t addressing_reset;
1027 adapter = sc->ndis_block->nmb_miniportadapterctx;
1028 resetfunc = sc->ndis_chars->nmc_reset_func;
1030 if (adapter == NULL || resetfunc == NULL ||
1031 sc->ndis_block->nmb_devicectx == NULL) {
1038 KeResetEvent(&sc->ndis_block->nmb_resetevent);
1040 if (NDIS_SERIALIZED(sc->ndis_block))
1041 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1043 rval = MSCALL2(resetfunc, &addressing_reset, adapter);
1045 if (NDIS_SERIALIZED(sc->ndis_block))
1046 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1048 if (rval == NDIS_STATUS_PENDING)
1049 KeWaitForSingleObject(&sc->ndis_block->nmb_resetevent,
1059 struct ndis_softc *sc;
1060 ndis_handle adapter;
1061 ndis_halt_handler haltfunc;
1062 ndis_miniport_block *block;
1067 block = sc->ndis_block;
1070 KeFlushQueuedDpcs();
1073 * Wait for all packets to be returned.
1077 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
1078 empty = IsListEmpty(&block->nmb_returnlist);
1079 KeReleaseSpinLock(&block->nmb_returnlock, irql);
1086 adapter = sc->ndis_block->nmb_miniportadapterctx;
1087 if (adapter == NULL) {
1092 sc->ndis_block->nmb_devicectx = NULL;
1095 * The adapter context is only valid after the init
1096 * handler has been called, and is invalid once the
1097 * halt handler has been called.
1100 haltfunc = sc->ndis_chars->nmc_halt_func;
1103 MSCALL1(haltfunc, adapter);
1106 sc->ndis_block->nmb_miniportadapterctx = NULL;
1113 ndis_shutdown_nic(arg)
1116 struct ndis_softc *sc;
1117 ndis_handle adapter;
1118 ndis_shutdown_handler shutdownfunc;
1122 adapter = sc->ndis_block->nmb_miniportadapterctx;
1123 shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1125 if (adapter == NULL || shutdownfunc == NULL)
1128 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1129 MSCALL1(shutdownfunc, adapter);
1131 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
1133 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1139 ndis_pnpevent_nic(arg, type)
1144 struct ndis_softc *sc;
1145 ndis_handle adapter;
1146 ndis_pnpevent_handler pnpeventfunc;
1149 sc = device_get_softc(arg);
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);
1169 struct ndis_softc *sc;
1170 ndis_miniport_block *block;
1171 ndis_init_handler initfunc;
1172 ndis_status status, openstatus = 0;
1173 ndis_medium mediumarray[NdisMediumMax];
1174 uint32_t chosenmedium, i;
1181 block = sc->ndis_block;
1182 initfunc = sc->ndis_chars->nmc_init_func;
1185 sc->ndis_block->nmb_timerlist = NULL;
1187 for (i = 0; i < NdisMediumMax; i++)
1190 status = MSCALL6(initfunc, &openstatus, &chosenmedium,
1191 mediumarray, NdisMediumMax, block, block);
1194 * If the init fails, blow away the other exported routines
1195 * we obtained from the driver so we can't call them later.
1196 * If the init failed, none of these will work.
1198 if (status != NDIS_STATUS_SUCCESS) {
1200 sc->ndis_block->nmb_miniportadapterctx = NULL;
1206 * This may look really goofy, but apparently it is possible
1207 * to halt a miniport too soon after it's been initialized.
1208 * After MiniportInitialize() finishes, pause for 1 second
1209 * to give the chip a chance to handle any short-lived timers
1210 * that were set in motion. If we call MiniportHalt() too soon,
1211 * some of the timers may not be cancelled, because the driver
1212 * expects them to fire before the halt is called.
1215 pause("ndwait", hz);
1218 sc->ndis_block->nmb_devicectx = sc;
1225 ndis_intrsetup(dpc, dobj, ip, sc)
1227 device_object *dobj;
1229 struct ndis_softc *sc;
1231 ndis_miniport_interrupt *intr;
1233 intr = sc->ndis_block->nmb_interrupt;
1240 KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
1241 KeResetEvent(&intr->ni_dpcevt);
1242 if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
1244 KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
1248 ndis_get_info(arg, oid, buf, buflen)
1254 struct ndis_softc *sc;
1256 ndis_handle adapter;
1257 ndis_queryinfo_handler queryfunc;
1258 uint32_t byteswritten = 0, bytesneeded = 0;
1264 KeResetEvent(&sc->ndis_block->nmb_getevent);
1266 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1268 if (sc->ndis_block->nmb_pendingreq != NULL) {
1269 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1270 panic("ndis_get_info() called while other request pending");
1272 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
1274 queryfunc = sc->ndis_chars->nmc_queryinfo_func;
1275 adapter = sc->ndis_block->nmb_miniportadapterctx;
1277 if (adapter == NULL || queryfunc == NULL ||
1278 sc->ndis_block->nmb_devicectx == NULL) {
1279 sc->ndis_block->nmb_pendingreq = NULL;
1280 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1284 rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
1285 &byteswritten, &bytesneeded);
1287 sc->ndis_block->nmb_pendingreq = NULL;
1289 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1291 /* Wait for requests that block. */
1293 if (rval == NDIS_STATUS_PENDING) {
1294 /* Wait up to 5 seconds. */
1295 duetime = (5 * 1000000) * -10;
1296 KeWaitForSingleObject(&sc->ndis_block->nmb_getevent,
1297 0, 0, FALSE, &duetime);
1298 rval = sc->ndis_block->nmb_getstat;
1302 *buflen = byteswritten;
1304 *buflen = bytesneeded;
1306 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1307 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1310 if (rval == NDIS_STATUS_INVALID_OID)
1313 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1314 rval == NDIS_STATUS_NOT_ACCEPTED)
1317 if (rval != NDIS_STATUS_SUCCESS)
1324 NdisAddDevice(drv, pdo)
1329 ndis_miniport_block *block;
1330 struct ndis_softc *sc;
1334 sc = device_get_softc(pdo->do_devext);
1336 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
1337 error = bus_setup_intr(sc->ndis_dev, sc->ndis_irq,
1338 INTR_TYPE_NET | INTR_MPSAFE,
1339 NULL, ntoskrnl_intr, NULL, &sc->ndis_intrhand);
1341 return (NDIS_STATUS_FAILURE);
1344 status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1345 FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
1347 if (status != STATUS_SUCCESS)
1350 block = fdo->do_devext;
1352 block->nmb_filterdbs.nf_ethdb = block;
1353 block->nmb_deviceobj = fdo;
1354 block->nmb_physdeviceobj = pdo;
1355 block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
1356 KeInitializeSpinLock(&block->nmb_lock);
1357 KeInitializeSpinLock(&block->nmb_returnlock);
1358 KeInitializeEvent(&block->nmb_getevent, EVENT_TYPE_NOTIFY, TRUE);
1359 KeInitializeEvent(&block->nmb_setevent, EVENT_TYPE_NOTIFY, TRUE);
1360 KeInitializeEvent(&block->nmb_resetevent, EVENT_TYPE_NOTIFY, TRUE);
1361 InitializeListHead(&block->nmb_parmlist);
1362 InitializeListHead(&block->nmb_returnlist);
1363 block->nmb_returnitem = IoAllocateWorkItem(fdo);
1366 * Stash pointers to the miniport block and miniport
1367 * characteristics info in the if_ndis softc so the
1368 * UNIX wrapper driver can get to them later.
1370 sc->ndis_block = block;
1371 sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1);
1374 * If the driver has a MiniportTransferData() function,
1375 * we should allocate a private RX packet pool.
1378 if (sc->ndis_chars->nmc_transferdata_func != NULL) {
1379 NdisAllocatePacketPool(&status, &block->nmb_rxpool,
1380 32, PROTOCOL_RESERVED_SIZE_IN_PACKET);
1381 if (status != NDIS_STATUS_SUCCESS) {
1382 IoDetachDevice(block->nmb_nextdeviceobj);
1383 IoDeleteDevice(fdo);
1386 InitializeListHead((&block->nmb_packetlist));
1389 /* Give interrupt handling priority over timers. */
1390 IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1391 KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
1393 /* Finish up BSD-specific setup. */
1395 block->nmb_signature = (void *)0xcafebabe;
1396 block->nmb_status_func = kernndis_functbl[0].ipt_wrap;
1397 block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap;
1398 block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap;
1399 block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap;
1400 block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap;
1401 block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap;
1402 block->nmb_pendingreq = NULL;
1404 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
1406 return (STATUS_SUCCESS);
1410 ndis_unload_driver(arg)
1413 struct ndis_softc *sc;
1418 if (sc->ndis_intrhand)
1419 bus_teardown_intr(sc->ndis_dev,
1420 sc->ndis_irq, sc->ndis_intrhand);
1422 if (sc->ndis_block->nmb_rlist != NULL)
1423 free(sc->ndis_block->nmb_rlist, M_DEVBUF);
1425 ndis_flush_sysctls(sc);
1427 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1429 if (sc->ndis_chars->nmc_transferdata_func != NULL)
1430 NdisFreePacketPool(sc->ndis_block->nmb_rxpool);
1431 fdo = sc->ndis_block->nmb_deviceobj;
1432 IoFreeWorkItem(sc->ndis_block->nmb_returnitem);
1433 IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj);
1434 IoDeleteDevice(fdo);