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)
200 ndis_status_func(adapter, status, sbuf, slen)
206 ndis_miniport_block *block;
207 struct ndis_softc *sc;
211 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
213 if (ifp->if_flags & IFF_DEBUG)
214 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");
235 ndis_setdone_func(adapter, status)
239 ndis_miniport_block *block;
242 block->nmb_setstat = status;
243 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);
261 ndis_resetdone_func(ndis_handle adapter, ndis_status status,
262 uint8_t addressingreset)
264 ndis_miniport_block *block;
265 struct ndis_softc *sc;
269 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
272 if (ifp->if_flags & IFF_DEBUG)
273 device_printf (sc->ndis_dev, "reset done...\n");
274 KeSetEvent(&block->nmb_resetevent, IO_NO_INCREMENT, FALSE);
280 ndis_create_sysctls(arg)
283 struct ndis_softc *sc;
286 struct sysctl_oid *oidp;
287 struct sysctl_ctx_entry *e;
293 vals = sc->ndis_regvals;
295 TAILQ_INIT(&sc->ndis_cfglist_head);
297 #if __FreeBSD_version < 502113
298 /* Create the sysctl tree. */
300 sc->ndis_tree = SYSCTL_ADD_NODE(&sc->ndis_ctx,
301 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
302 device_get_nameunit(sc->ndis_dev), CTLFLAG_RD, 0,
303 device_get_desc(sc->ndis_dev));
306 /* Add the driver-specific registry keys. */
309 if (vals->nc_cfgkey == NULL)
312 if (vals->nc_idx != sc->ndis_devidx) {
317 /* See if we already have a sysctl with this name */
320 #if __FreeBSD_version < 502113
321 TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
323 TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
326 if (strcasecmp(oidp->oid_name, vals->nc_cfgkey) == 0)
336 ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
337 vals->nc_val, CTLFLAG_RW);
341 /* Now add a couple of builtin keys. */
344 * Environment can be either Windows (0) or WindowsNT (1).
345 * We qualify as the latter.
347 ndis_add_sysctl(sc, "Environment",
348 "Windows environment", "1", CTLFLAG_RD);
350 /* NDIS version should be 5.1. */
351 ndis_add_sysctl(sc, "NdisVersion",
352 "NDIS API Version", "0x00050001", CTLFLAG_RD);
354 /* Bus type (PCI, PCMCIA, etc...) */
355 sprintf(buf, "%d", (int)sc->ndis_iftype);
356 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
358 if (sc->ndis_res_io != NULL) {
359 sprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
360 ndis_add_sysctl(sc, "IOBaseAddress",
361 "Base I/O Address", buf, CTLFLAG_RD);
364 if (sc->ndis_irq != NULL) {
365 sprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
366 ndis_add_sysctl(sc, "InterruptNumber",
367 "Interrupt Number", buf, CTLFLAG_RD);
374 ndis_add_sysctl(arg, key, desc, val, flag)
381 struct ndis_softc *sc;
382 struct ndis_cfglist *cfg;
387 cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO);
390 printf("failed for %s\n", key);
394 cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF);
396 snprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
397 cfg->ndis_cfg.nc_cfgdesc = strdup(descstr, M_DEVBUF);
399 cfg->ndis_cfg.nc_cfgdesc = strdup(desc, M_DEVBUF);
400 strcpy(cfg->ndis_cfg.nc_val, val);
402 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
405 #if __FreeBSD_version < 502113
406 SYSCTL_ADD_STRING(&sc->ndis_ctx, SYSCTL_CHILDREN(sc->ndis_tree),
407 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
408 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
409 cfg->ndis_cfg.nc_cfgdesc);
411 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
412 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
413 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
414 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
415 cfg->ndis_cfg.nc_cfgdesc);
422 * Somewhere, somebody decided "hey, let's automatically create
423 * a sysctl tree for each device instance as it's created -- it'll
424 * make life so much easier!" Lies. Why must they turn the kernel
425 * into a house of lies?
429 ndis_flush_sysctls(arg)
432 struct ndis_softc *sc;
433 struct ndis_cfglist *cfg;
434 struct sysctl_ctx_list *clist;
438 #if __FreeBSD_version < 502113
439 clist = &sc->ndis_ctx;
441 clist = device_get_sysctl_ctx(sc->ndis_dev);
444 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
445 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
446 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
447 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
448 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
449 free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
450 free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
458 ndis_return(dobj, arg)
462 ndis_miniport_block *block;
463 ndis_miniport_characteristics *ch;
464 ndis_return_handler returnfunc;
471 ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
474 adapter = block->nmb_miniportadapterctx;
479 returnfunc = ch->nmc_return_packet_func;
481 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
482 while (!IsListEmpty(&block->nmb_returnlist)) {
483 l = RemoveHeadList((&block->nmb_returnlist));
484 p = CONTAINING_RECORD(l, ndis_packet, np_list);
485 InitializeListHead((&p->np_list));
486 KeReleaseSpinLock(&block->nmb_returnlock, irql);
487 MSCALL2(returnfunc, adapter, p);
488 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
490 KeReleaseSpinLock(&block->nmb_returnlock, irql);
496 ndis_return_packet(buf, arg)
497 void *buf; /* not used */
501 ndis_miniport_block *block;
508 /* Decrement refcount. */
511 /* Release packet when refcount hits zero, otherwise return. */
515 block = ((struct ndis_softc *)p->np_softc)->ndis_block;
517 KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
518 InitializeListHead((&p->np_list));
519 InsertHeadList((&block->nmb_returnlist), (&p->np_list));
520 KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
522 IoQueueWorkItem(block->nmb_returnitem,
523 (io_workitem_func)kernndis_functbl[7].ipt_wrap,
524 WORKQUEUE_CRITICAL, block);
554 ndis_free_bufs(p->np_private.npp_head);
560 ndis_convert_res(arg)
563 struct ndis_softc *sc;
564 ndis_resource_list *rl = NULL;
565 cm_partial_resource_desc *prd = NULL;
566 ndis_miniport_block *block;
568 struct resource_list *brl;
569 struct resource_list_entry *brle;
570 #if __FreeBSD_version < 600022
571 struct resource_list brl_rev;
572 struct resource_list_entry *n;
577 block = sc->ndis_block;
580 #if __FreeBSD_version < 600022
581 SLIST_INIT(&brl_rev);
584 rl = malloc(sizeof(ndis_resource_list) +
585 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
586 M_DEVBUF, M_NOWAIT|M_ZERO);
591 rl->cprl_version = 5;
592 rl->cprl_version = 1;
593 rl->cprl_count = sc->ndis_rescnt;
594 prd = rl->cprl_partial_descs;
596 brl = BUS_GET_RESOURCE_LIST(dev, dev);
600 #if __FreeBSD_version < 600022
602 * We have a small problem. Some PCI devices have
603 * multiple I/O ranges. Windows orders them starting
604 * from lowest numbered BAR to highest. We discover
605 * them in that order too, but insert them into a singly
606 * linked list head first, which means when time comes
607 * to traverse the list, we enumerate them in reverse
608 * order. This screws up some drivers which expect the
609 * BARs to be in ascending order so that they can choose
610 * the "first" one as their register space. Unfortunately,
611 * in order to fix this, we have to create our own
612 * temporary list with the entries in reverse order.
615 SLIST_FOREACH(brle, brl, link) {
616 n = malloc(sizeof(struct resource_list_entry),
622 bcopy((char *)brle, (char *)n,
623 sizeof(struct resource_list_entry));
624 SLIST_INSERT_HEAD(&brl_rev, n, link);
627 SLIST_FOREACH(brle, &brl_rev, link) {
629 STAILQ_FOREACH(brle, brl, link) {
631 switch (brle->type) {
633 prd->cprd_type = CmResourceTypePort;
634 prd->cprd_flags = CM_RESOURCE_PORT_IO;
635 prd->cprd_sharedisp =
636 CmResourceShareDeviceExclusive;
637 prd->u.cprd_port.cprd_start.np_quad =
639 prd->u.cprd_port.cprd_len = brle->count;
642 prd->cprd_type = CmResourceTypeMemory;
644 CM_RESOURCE_MEMORY_READ_WRITE;
645 prd->cprd_sharedisp =
646 CmResourceShareDeviceExclusive;
647 prd->u.cprd_port.cprd_start.np_quad =
649 prd->u.cprd_port.cprd_len = brle->count;
652 prd->cprd_type = CmResourceTypeInterrupt;
655 * Always mark interrupt resources as
656 * shared, since in our implementation,
659 prd->cprd_sharedisp =
660 CmResourceShareShared;
661 prd->u.cprd_intr.cprd_level = brle->start;
662 prd->u.cprd_intr.cprd_vector = brle->start;
663 prd->u.cprd_intr.cprd_affinity = 0;
672 block->nmb_rlist = rl;
674 #if __FreeBSD_version < 600022
677 while (!SLIST_EMPTY(&brl_rev)) {
678 n = SLIST_FIRST(&brl_rev);
679 SLIST_REMOVE_HEAD(&brl_rev, link);
688 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
689 * packet, it will hand it to us in the form of an ndis_packet,
690 * which we need to convert to an mbuf that is then handed off
691 * to the stack. Note: we configure the mbuf list so that it uses
692 * the memory regions specified by the ndis_buffer structures in
693 * the ndis_packet as external storage. In most cases, this will
694 * point to a memory region allocated by the driver (either by
695 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
696 * the driver to handle free()ing this region for is, so we set up
697 * a dummy no-op free handler for it.
705 struct mbuf *m = NULL, *prev = NULL;
707 ndis_packet_private *priv;
710 struct ether_header *eh;
713 if (p == NULL || m0 == NULL)
716 priv = &p->np_private;
717 buf = priv->npp_head;
720 for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
721 if (buf == priv->npp_head)
723 MGETHDR(m, M_DONTWAIT, MT_HEADER);
725 MGETHDR(m, M_DONTWAIT, MT_DATA);
728 MGET(m, M_DONTWAIT, MT_DATA);
734 m->m_len = MmGetMdlByteCount(buf);
735 m->m_data = MmGetMdlVirtualAddress(buf);
736 MEXTADD(m, m->m_data, m->m_len, ndis_return_packet,
737 m->m_data, p, 0, EXT_NDIS);
741 if (m->m_flags & M_PKTHDR)
749 * This is a hack to deal with the Marvell 8335 driver
750 * which, when associated with an AP in WPA-PSK mode,
751 * seems to overpad its frames by 8 bytes. I don't know
752 * that the extra 8 bytes are for, and they're not there
753 * in open mode, so for now clamp the frame size at 1514
754 * until I can figure out how to deal with this properly,
755 * otherwise if_ethersubr() will spank us by discarding
756 * the 'oversize' frames.
759 eh = mtod((*m0), struct ether_header *);
760 ifp = ((struct ndis_softc *)p->np_softc)->ifp;
761 if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
762 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
766 (*m0)->m_pkthdr.len = totlen;
772 * Create an NDIS packet from an mbuf chain.
773 * This is used mainly when transmitting packets, where we need
774 * to turn an mbuf off an interface's send queue and transform it
775 * into an NDIS packet which will be fed into the NDIS driver's
778 * NDIS packets consist of two parts: an ndis_packet structure,
779 * which is vaguely analagous to the pkthdr portion of an mbuf,
780 * and one or more ndis_buffer structures, which define the
781 * actual memory segments in which the packet data resides.
782 * We need to allocate one ndis_buffer for each mbuf in a chain,
783 * plus one ndis_packet as the header.
792 ndis_buffer *buf = NULL, *prev = NULL;
793 ndis_packet_private *priv;
795 if (p == NULL || *p == NULL || m0 == NULL)
798 priv = &(*p)->np_private;
799 priv->npp_totlen = m0->m_pkthdr.len;
801 for (m = m0; m != NULL; m = m->m_next) {
804 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
806 ndis_free_packet(*p);
810 MmBuildMdlForNonPagedPool(buf);
812 if (priv->npp_head == NULL)
813 priv->npp_head = buf;
815 prev->mdl_next = buf;
819 priv->npp_tail = buf;
825 ndis_get_supported_oids(arg, oids, oidcnt)
833 if (arg == NULL || oids == NULL || oidcnt == NULL)
836 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
838 o = malloc(len, M_DEVBUF, M_NOWAIT);
842 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
856 ndis_set_info(arg, oid, buf, buflen)
862 struct ndis_softc *sc;
865 ndis_setinfo_handler setfunc;
866 uint32_t byteswritten = 0, bytesneeded = 0;
871 * According to the NDIS spec, MiniportQueryInformation()
872 * and MiniportSetInformation() requests are handled serially:
873 * once one request has been issued, we must wait for it to
874 * finish before allowing another request to proceed.
879 KeResetEvent(&sc->ndis_block->nmb_setevent);
881 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
883 if (sc->ndis_block->nmb_pendingreq != NULL) {
884 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
885 panic("ndis_set_info() called while other request pending");
887 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
889 setfunc = sc->ndis_chars->nmc_setinfo_func;
890 adapter = sc->ndis_block->nmb_miniportadapterctx;
892 if (adapter == NULL || setfunc == NULL ||
893 sc->ndis_block->nmb_devicectx == NULL) {
894 sc->ndis_block->nmb_pendingreq = NULL;
895 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
899 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
900 &byteswritten, &bytesneeded);
902 sc->ndis_block->nmb_pendingreq = NULL;
904 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
906 if (rval == NDIS_STATUS_PENDING) {
907 /* Wait up to 5 seconds. */
908 duetime = (5 * 1000000) * -10;
909 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
910 0, 0, FALSE, &duetime);
911 rval = sc->ndis_block->nmb_setstat;
915 *buflen = byteswritten;
917 *buflen = bytesneeded;
919 if (rval == NDIS_STATUS_INVALID_LENGTH)
922 if (rval == NDIS_STATUS_INVALID_OID)
925 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
926 rval == NDIS_STATUS_NOT_ACCEPTED)
929 if (rval != NDIS_STATUS_SUCCESS)
935 typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
938 ndis_send_packets(arg, packets, cnt)
940 ndis_packet **packets;
943 struct ndis_softc *sc;
945 ndis_sendmulti_handler sendfunc;
946 ndis_senddone_func senddonefunc;
952 adapter = sc->ndis_block->nmb_miniportadapterctx;
955 sendfunc = sc->ndis_chars->nmc_sendmulti_func;
956 senddonefunc = sc->ndis_block->nmb_senddone_func;
958 if (NDIS_SERIALIZED(sc->ndis_block))
959 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
961 MSCALL3(sendfunc, adapter, packets, cnt);
963 for (i = 0; i < cnt; i++) {
966 * Either the driver already handed the packet to
967 * ndis_txeof() due to a failure, or it wants to keep
968 * it and release it asynchronously later. Skip to the
971 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
973 MSCALL3(senddonefunc, sc->ndis_block, p, p->np_oob.npo_status);
976 if (NDIS_SERIALIZED(sc->ndis_block))
977 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
983 ndis_send_packet(arg, packet)
987 struct ndis_softc *sc;
990 ndis_sendsingle_handler sendfunc;
991 ndis_senddone_func senddonefunc;
995 adapter = sc->ndis_block->nmb_miniportadapterctx;
998 sendfunc = sc->ndis_chars->nmc_sendsingle_func;
999 senddonefunc = sc->ndis_block->nmb_senddone_func;
1001 if (NDIS_SERIALIZED(sc->ndis_block))
1002 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1003 status = MSCALL3(sendfunc, adapter, packet,
1004 packet->np_private.npp_flags);
1006 if (status == NDIS_STATUS_PENDING) {
1007 if (NDIS_SERIALIZED(sc->ndis_block))
1008 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1012 MSCALL3(senddonefunc, sc->ndis_block, packet, status);
1014 if (NDIS_SERIALIZED(sc->ndis_block))
1015 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1024 struct ndis_softc *sc;
1029 sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
1030 M_DEVBUF, M_NOWAIT|M_ZERO);
1032 if (sc->ndis_tmaps == NULL)
1035 for (i = 0; i < sc->ndis_maxpkts; i++) {
1036 error = bus_dmamap_create(sc->ndis_ttag, 0,
1037 &sc->ndis_tmaps[i]);
1039 free(sc->ndis_tmaps, M_DEVBUF);
1048 ndis_destroy_dma(arg)
1051 struct ndis_softc *sc;
1053 ndis_packet *p = NULL;
1058 for (i = 0; i < sc->ndis_maxpkts; i++) {
1059 if (sc->ndis_txarray[i] != NULL) {
1060 p = sc->ndis_txarray[i];
1061 m = (struct mbuf *)p->np_rsvd[1];
1064 ndis_free_packet(sc->ndis_txarray[i]);
1066 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
1069 free(sc->ndis_tmaps, M_DEVBUF);
1071 bus_dma_tag_destroy(sc->ndis_ttag);
1080 struct ndis_softc *sc;
1081 ndis_handle adapter;
1082 ndis_reset_handler resetfunc;
1083 uint8_t addressing_reset;
1090 adapter = sc->ndis_block->nmb_miniportadapterctx;
1091 resetfunc = sc->ndis_chars->nmc_reset_func;
1093 if (adapter == NULL || resetfunc == NULL ||
1094 sc->ndis_block->nmb_devicectx == NULL) {
1101 KeResetEvent(&sc->ndis_block->nmb_resetevent);
1103 if (NDIS_SERIALIZED(sc->ndis_block))
1104 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1106 rval = MSCALL2(resetfunc, &addressing_reset, adapter);
1108 if (NDIS_SERIALIZED(sc->ndis_block))
1109 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1111 if (rval == NDIS_STATUS_PENDING)
1112 KeWaitForSingleObject(&sc->ndis_block->nmb_resetevent,
1122 struct ndis_softc *sc;
1123 ndis_handle adapter;
1124 ndis_halt_handler haltfunc;
1125 ndis_miniport_block *block;
1130 block = sc->ndis_block;
1133 KeFlushQueuedDpcs();
1136 * Wait for all packets to be returned.
1140 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
1141 empty = IsListEmpty(&block->nmb_returnlist);
1142 KeReleaseSpinLock(&block->nmb_returnlock, irql);
1149 adapter = sc->ndis_block->nmb_miniportadapterctx;
1150 if (adapter == NULL) {
1155 sc->ndis_block->nmb_devicectx = NULL;
1158 * The adapter context is only valid after the init
1159 * handler has been called, and is invalid once the
1160 * halt handler has been called.
1163 haltfunc = sc->ndis_chars->nmc_halt_func;
1166 MSCALL1(haltfunc, adapter);
1169 sc->ndis_block->nmb_miniportadapterctx = NULL;
1176 ndis_shutdown_nic(arg)
1179 struct ndis_softc *sc;
1180 ndis_handle adapter;
1181 ndis_shutdown_handler shutdownfunc;
1185 adapter = sc->ndis_block->nmb_miniportadapterctx;
1186 shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1188 if (adapter == NULL || shutdownfunc == NULL)
1191 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1192 MSCALL1(shutdownfunc, adapter);
1194 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
1196 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1202 ndis_pnpevent_nic(arg, type)
1207 struct ndis_softc *sc;
1208 ndis_handle adapter;
1209 ndis_pnpevent_handler pnpeventfunc;
1212 sc = device_get_softc(arg);
1214 adapter = sc->ndis_block->nmb_miniportadapterctx;
1215 pnpeventfunc = sc->ndis_chars->nmc_pnpevent_handler;
1217 if (adapter == NULL || pnpeventfunc == NULL)
1220 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1221 MSCALL4(pnpeventfunc, adapter, type, NULL, 0);
1223 MSCALL4(pnpeventfunc, sc->ndis_chars->nmc_rsvd0, type, NULL, 0);
1232 struct ndis_softc *sc;
1233 ndis_miniport_block *block;
1234 ndis_init_handler initfunc;
1235 ndis_status status, openstatus = 0;
1236 ndis_medium mediumarray[NdisMediumMax];
1237 uint32_t chosenmedium, i;
1244 block = sc->ndis_block;
1245 initfunc = sc->ndis_chars->nmc_init_func;
1248 sc->ndis_block->nmb_timerlist = NULL;
1250 for (i = 0; i < NdisMediumMax; i++)
1253 status = MSCALL6(initfunc, &openstatus, &chosenmedium,
1254 mediumarray, NdisMediumMax, block, block);
1257 * If the init fails, blow away the other exported routines
1258 * we obtained from the driver so we can't call them later.
1259 * If the init failed, none of these will work.
1261 if (status != NDIS_STATUS_SUCCESS) {
1263 sc->ndis_block->nmb_miniportadapterctx = NULL;
1269 * This may look really goofy, but apparently it is possible
1270 * to halt a miniport too soon after it's been initialized.
1271 * After MiniportInitialize() finishes, pause for 1 second
1272 * to give the chip a chance to handle any short-lived timers
1273 * that were set in motion. If we call MiniportHalt() too soon,
1274 * some of the timers may not be cancelled, because the driver
1275 * expects them to fire before the halt is called.
1278 pause("ndwait", hz);
1281 sc->ndis_block->nmb_devicectx = sc;
1288 ndis_intrsetup(dpc, dobj, ip, sc)
1290 device_object *dobj;
1292 struct ndis_softc *sc;
1294 ndis_miniport_interrupt *intr;
1296 intr = sc->ndis_block->nmb_interrupt;
1303 KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
1304 KeResetEvent(&intr->ni_dpcevt);
1305 if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
1307 KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
1313 ndis_get_info(arg, oid, buf, buflen)
1319 struct ndis_softc *sc;
1321 ndis_handle adapter;
1322 ndis_queryinfo_handler queryfunc;
1323 uint32_t byteswritten = 0, bytesneeded = 0;
1329 KeResetEvent(&sc->ndis_block->nmb_getevent);
1331 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1333 if (sc->ndis_block->nmb_pendingreq != NULL) {
1334 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1335 panic("ndis_get_info() called while other request pending");
1337 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
1339 queryfunc = sc->ndis_chars->nmc_queryinfo_func;
1340 adapter = sc->ndis_block->nmb_miniportadapterctx;
1342 if (adapter == NULL || queryfunc == NULL ||
1343 sc->ndis_block->nmb_devicectx == NULL) {
1344 sc->ndis_block->nmb_pendingreq = NULL;
1345 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1349 rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
1350 &byteswritten, &bytesneeded);
1352 sc->ndis_block->nmb_pendingreq = NULL;
1354 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1356 /* Wait for requests that block. */
1358 if (rval == NDIS_STATUS_PENDING) {
1359 /* Wait up to 5 seconds. */
1360 duetime = (5 * 1000000) * -10;
1361 KeWaitForSingleObject(&sc->ndis_block->nmb_getevent,
1362 0, 0, FALSE, &duetime);
1363 rval = sc->ndis_block->nmb_getstat;
1367 *buflen = byteswritten;
1369 *buflen = bytesneeded;
1371 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1372 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1375 if (rval == NDIS_STATUS_INVALID_OID)
1378 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1379 rval == NDIS_STATUS_NOT_ACCEPTED)
1382 if (rval != NDIS_STATUS_SUCCESS)
1389 NdisAddDevice(drv, pdo)
1394 ndis_miniport_block *block;
1395 struct ndis_softc *sc;
1399 sc = device_get_softc(pdo->do_devext);
1401 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
1402 error = bus_setup_intr(sc->ndis_dev, sc->ndis_irq,
1403 INTR_TYPE_NET | INTR_MPSAFE,
1404 NULL, ntoskrnl_intr, NULL, &sc->ndis_intrhand);
1406 return(NDIS_STATUS_FAILURE);
1409 status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1410 FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
1412 if (status != STATUS_SUCCESS)
1415 block = fdo->do_devext;
1417 block->nmb_filterdbs.nf_ethdb = block;
1418 block->nmb_deviceobj = fdo;
1419 block->nmb_physdeviceobj = pdo;
1420 block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
1421 KeInitializeSpinLock(&block->nmb_lock);
1422 KeInitializeSpinLock(&block->nmb_returnlock);
1423 KeInitializeEvent(&block->nmb_getevent, EVENT_TYPE_NOTIFY, TRUE);
1424 KeInitializeEvent(&block->nmb_setevent, EVENT_TYPE_NOTIFY, TRUE);
1425 KeInitializeEvent(&block->nmb_resetevent, EVENT_TYPE_NOTIFY, TRUE);
1426 InitializeListHead(&block->nmb_parmlist);
1427 InitializeListHead(&block->nmb_returnlist);
1428 block->nmb_returnitem = IoAllocateWorkItem(fdo);
1431 * Stash pointers to the miniport block and miniport
1432 * characteristics info in the if_ndis softc so the
1433 * UNIX wrapper driver can get to them later.
1435 sc->ndis_block = block;
1436 sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1);
1439 * If the driver has a MiniportTransferData() function,
1440 * we should allocate a private RX packet pool.
1443 if (sc->ndis_chars->nmc_transferdata_func != NULL) {
1444 NdisAllocatePacketPool(&status, &block->nmb_rxpool,
1445 32, PROTOCOL_RESERVED_SIZE_IN_PACKET);
1446 if (status != NDIS_STATUS_SUCCESS) {
1447 IoDetachDevice(block->nmb_nextdeviceobj);
1448 IoDeleteDevice(fdo);
1451 InitializeListHead((&block->nmb_packetlist));
1454 /* Give interrupt handling priority over timers. */
1455 IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1456 KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
1458 /* Finish up BSD-specific setup. */
1460 block->nmb_signature = (void *)0xcafebabe;
1461 block->nmb_status_func = kernndis_functbl[0].ipt_wrap;
1462 block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap;
1463 block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap;
1464 block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap;
1465 block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap;
1466 block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap;
1467 block->nmb_pendingreq = NULL;
1469 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
1471 return (STATUS_SUCCESS);
1475 ndis_unload_driver(arg)
1478 struct ndis_softc *sc;
1483 if (sc->ndis_intrhand)
1484 bus_teardown_intr(sc->ndis_dev,
1485 sc->ndis_irq, sc->ndis_intrhand);
1487 if (sc->ndis_block->nmb_rlist != NULL)
1488 free(sc->ndis_block->nmb_rlist, M_DEVBUF);
1490 ndis_flush_sysctls(sc);
1492 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1494 if (sc->ndis_chars->nmc_transferdata_func != NULL)
1495 NdisFreePacketPool(sc->ndis_block->nmb_rxpool);
1496 fdo = sc->ndis_block->nmb_deviceobj;
1497 IoFreeWorkItem(sc->ndis_block->nmb_returnitem);
1498 IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj);
1499 IoDeleteDevice(fdo);