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"
81 #define NDIS_FLAG_RDONLY 1
83 static void ndis_status_func(ndis_handle, ndis_status, void *, uint32_t);
84 static void ndis_statusdone_func(ndis_handle);
85 static void ndis_setdone_func(ndis_handle, ndis_status);
86 static void ndis_getdone_func(ndis_handle, ndis_status);
87 static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
88 static void ndis_sendrsrcavail_func(ndis_handle);
89 static void ndis_intrsetup(kdpc *, device_object *,
90 irp *, struct ndis_softc *);
91 static void ndis_return(device_object *, void *);
93 static image_patch_table kernndis_functbl[] = {
94 IMPORT_SFUNC(ndis_status_func, 4),
95 IMPORT_SFUNC(ndis_statusdone_func, 1),
96 IMPORT_SFUNC(ndis_setdone_func, 2),
97 IMPORT_SFUNC(ndis_getdone_func, 2),
98 IMPORT_SFUNC(ndis_resetdone_func, 3),
99 IMPORT_SFUNC(ndis_sendrsrcavail_func, 1),
100 IMPORT_SFUNC(ndis_intrsetup, 4),
101 IMPORT_SFUNC(ndis_return, 1),
106 static struct nd_head ndis_devhead;
109 * This allows us to export our symbols to other modules.
110 * Note that we call ourselves 'ndisapi' to avoid a namespace
111 * collision with if_ndis.ko, which internally calls itself
114 * Note: some of the subsystems depend on each other, so the
115 * order in which they're started is important. The order of
118 * HAL - spinlocks and IRQL manipulation
119 * ntoskrnl - DPC and workitem threads, object waiting
120 * windrv - driver/device registration
122 * The HAL should also be the last thing shut down, since
123 * the ntoskrnl subsystem will use spinlocks right up until
124 * the DPC and workitem threads are terminated.
128 ndis_modevent(module_t mod, int cmd, void *arg)
131 image_patch_table *patch;
135 /* Initialize subsystems */
142 patch = kernndis_functbl;
143 while (patch->ipt_func != NULL) {
144 windrv_wrap((funcptr)patch->ipt_func,
145 (funcptr *)&patch->ipt_wrap,
146 patch->ipt_argcnt, patch->ipt_ftype);
150 TAILQ_INIT(&ndis_devhead);
153 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
154 /* Shut down subsystems */
161 patch = kernndis_functbl;
162 while (patch->ipt_func != NULL) {
163 windrv_unwrap(patch->ipt_wrap);
169 /* Shut down subsystems */
176 patch = kernndis_functbl;
177 while (patch->ipt_func != NULL) {
178 windrv_unwrap(patch->ipt_wrap);
190 DEV_MODULE(ndisapi, ndis_modevent, NULL);
191 MODULE_VERSION(ndisapi, 1);
194 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);
218 ndis_statusdone_func(adapter)
221 ndis_miniport_block *block;
222 struct ndis_softc *sc;
226 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
228 if (ifp->if_flags & IFF_DEBUG)
229 device_printf(sc->ndis_dev, "status complete\n");
233 ndis_setdone_func(adapter, status)
237 ndis_miniport_block *block;
240 block->nmb_setstat = status;
241 KeSetEvent(&block->nmb_setevent, IO_NO_INCREMENT, FALSE);
245 ndis_getdone_func(adapter, status)
249 ndis_miniport_block *block;
252 block->nmb_getstat = status;
253 KeSetEvent(&block->nmb_getevent, IO_NO_INCREMENT, FALSE);
257 ndis_resetdone_func(ndis_handle adapter, ndis_status status,
258 uint8_t addressingreset)
260 ndis_miniport_block *block;
261 struct ndis_softc *sc;
265 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
268 if (ifp->if_flags & IFF_DEBUG)
269 device_printf(sc->ndis_dev, "reset done...\n");
270 KeSetEvent(&block->nmb_resetevent, IO_NO_INCREMENT, FALSE);
274 ndis_create_sysctls(arg)
277 struct ndis_softc *sc;
280 struct sysctl_oid *oidp;
281 struct sysctl_ctx_entry *e;
287 vals = sc->ndis_regvals;
289 TAILQ_INIT(&sc->ndis_cfglist_head);
291 /* Add the driver-specific registry keys. */
294 if (vals->nc_cfgkey == NULL)
297 if (vals->nc_idx != sc->ndis_devidx) {
302 /* See if we already have a sysctl with this name */
305 TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
307 if (strcasecmp(oidp->oid_name, vals->nc_cfgkey) == 0)
317 ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
318 vals->nc_val, CTLFLAG_RW);
322 /* Now add a couple of builtin keys. */
325 * Environment can be either Windows (0) or WindowsNT (1).
326 * We qualify as the latter.
328 ndis_add_sysctl(sc, "Environment",
329 "Windows environment", "1", NDIS_FLAG_RDONLY);
331 /* NDIS version should be 5.1. */
332 ndis_add_sysctl(sc, "NdisVersion",
333 "NDIS API Version", "0x00050001", NDIS_FLAG_RDONLY);
336 * Some miniport drivers rely on the existence of the SlotNumber,
337 * NetCfgInstanceId and DriverDesc keys.
339 ndis_add_sysctl(sc, "SlotNumber", "Slot Numer", "01", NDIS_FLAG_RDONLY);
340 ndis_add_sysctl(sc, "NetCfgInstanceId", "NetCfgInstanceId",
341 "{12345678-1234-5678-CAFE0-123456789ABC}", NDIS_FLAG_RDONLY);
342 ndis_add_sysctl(sc, "DriverDesc", "Driver Description",
343 "NDIS Network Adapter", NDIS_FLAG_RDONLY);
345 /* Bus type (PCI, PCMCIA, etc...) */
346 sprintf(buf, "%d", (int)sc->ndis_iftype);
347 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, NDIS_FLAG_RDONLY);
349 if (sc->ndis_res_io != NULL) {
350 sprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
351 ndis_add_sysctl(sc, "IOBaseAddress",
352 "Base I/O Address", buf, NDIS_FLAG_RDONLY);
355 if (sc->ndis_irq != NULL) {
356 sprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
357 ndis_add_sysctl(sc, "InterruptNumber",
358 "Interrupt Number", buf, NDIS_FLAG_RDONLY);
365 ndis_add_sysctl(arg, key, desc, val, flag_rdonly)
372 struct ndis_softc *sc;
373 struct ndis_cfglist *cfg;
378 cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO);
381 printf("failed for %s\n", key);
385 cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF);
387 snprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
388 cfg->ndis_cfg.nc_cfgdesc = strdup(descstr, M_DEVBUF);
390 cfg->ndis_cfg.nc_cfgdesc = strdup(desc, M_DEVBUF);
391 strcpy(cfg->ndis_cfg.nc_val, val);
393 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
395 if (flag_rdonly != 0) {
397 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
398 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
399 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, CTLFLAG_RD,
400 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
401 cfg->ndis_cfg.nc_cfgdesc);
404 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
405 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
406 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, CTLFLAG_RW,
407 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
408 cfg->ndis_cfg.nc_cfgdesc);
414 * Somewhere, somebody decided "hey, let's automatically create
415 * a sysctl tree for each device instance as it's created -- it'll
416 * make life so much easier!" Lies. Why must they turn the kernel
417 * into a house of lies?
421 ndis_flush_sysctls(arg)
424 struct ndis_softc *sc;
425 struct ndis_cfglist *cfg;
426 struct sysctl_ctx_list *clist;
430 clist = device_get_sysctl_ctx(sc->ndis_dev);
432 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
433 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
434 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
435 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
436 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
437 free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
438 free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
446 ndis_get_routine_address(functbl, name)
447 struct image_patch_table *functbl;
452 for (i = 0; functbl[i].ipt_name != NULL; i++)
453 if (strcmp(name, functbl[i].ipt_name) == 0)
454 return (functbl[i].ipt_wrap);
459 ndis_return(dobj, arg)
463 ndis_miniport_block *block;
464 ndis_miniport_characteristics *ch;
465 ndis_return_handler returnfunc;
472 ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
475 adapter = block->nmb_miniportadapterctx;
480 returnfunc = ch->nmc_return_packet_func;
482 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
483 while (!IsListEmpty(&block->nmb_returnlist)) {
484 l = RemoveHeadList((&block->nmb_returnlist));
485 p = CONTAINING_RECORD(l, ndis_packet, np_list);
486 InitializeListHead((&p->np_list));
487 KeReleaseSpinLock(&block->nmb_returnlock, irql);
488 MSCALL2(returnfunc, adapter, p);
489 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
491 KeReleaseSpinLock(&block->nmb_returnlock, irql);
495 ndis_return_packet(buf, arg)
496 void *buf; /* not used */
500 ndis_miniport_block *block;
507 /* Decrement refcount. */
510 /* Release packet when refcount hits zero, otherwise return. */
514 block = ((struct ndis_softc *)p->np_softc)->ndis_block;
516 KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
517 InitializeListHead((&p->np_list));
518 InsertHeadList((&block->nmb_returnlist), (&p->np_list));
519 KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
521 IoQueueWorkItem(block->nmb_returnitem,
522 (io_workitem_func)kernndis_functbl[7].ipt_wrap,
523 WORKQUEUE_CRITICAL, block);
549 ndis_free_bufs(p->np_private.npp_head);
554 ndis_convert_res(arg)
557 struct ndis_softc *sc;
558 ndis_resource_list *rl = NULL;
559 cm_partial_resource_desc *prd = NULL;
560 ndis_miniport_block *block;
562 struct resource_list *brl;
563 struct resource_list_entry *brle;
567 block = sc->ndis_block;
570 rl = malloc(sizeof(ndis_resource_list) +
571 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
572 M_DEVBUF, M_NOWAIT|M_ZERO);
577 rl->cprl_version = 5;
578 rl->cprl_revision = 1;
579 rl->cprl_count = sc->ndis_rescnt;
580 prd = rl->cprl_partial_descs;
582 brl = BUS_GET_RESOURCE_LIST(dev, dev);
586 STAILQ_FOREACH(brle, brl, link) {
587 switch (brle->type) {
589 prd->cprd_type = CmResourceTypePort;
590 prd->cprd_flags = CM_RESOURCE_PORT_IO;
591 prd->cprd_sharedisp =
592 CmResourceShareDeviceExclusive;
593 prd->u.cprd_port.cprd_start.np_quad =
595 prd->u.cprd_port.cprd_len = brle->count;
598 prd->cprd_type = CmResourceTypeMemory;
600 CM_RESOURCE_MEMORY_READ_WRITE;
601 prd->cprd_sharedisp =
602 CmResourceShareDeviceExclusive;
603 prd->u.cprd_mem.cprd_start.np_quad =
605 prd->u.cprd_mem.cprd_len = brle->count;
608 prd->cprd_type = CmResourceTypeInterrupt;
611 * Always mark interrupt resources as
612 * shared, since in our implementation,
615 prd->cprd_sharedisp =
616 CmResourceShareShared;
617 prd->u.cprd_intr.cprd_level = brle->start;
618 prd->u.cprd_intr.cprd_vector = brle->start;
619 prd->u.cprd_intr.cprd_affinity = 0;
628 block->nmb_rlist = rl;
634 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
635 * packet, it will hand it to us in the form of an ndis_packet,
636 * which we need to convert to an mbuf that is then handed off
637 * to the stack. Note: we configure the mbuf list so that it uses
638 * the memory regions specified by the ndis_buffer structures in
639 * the ndis_packet as external storage. In most cases, this will
640 * point to a memory region allocated by the driver (either by
641 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
642 * the driver to handle free()ing this region for is, so we set up
643 * a dummy no-op free handler for it.
651 struct mbuf *m = NULL, *prev = NULL;
653 ndis_packet_private *priv;
656 struct ether_header *eh;
659 if (p == NULL || m0 == NULL)
662 priv = &p->np_private;
663 buf = priv->npp_head;
666 for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
667 if (buf == priv->npp_head)
669 MGETHDR(m, M_DONTWAIT, MT_HEADER);
671 MGETHDR(m, M_DONTWAIT, MT_DATA);
674 MGET(m, M_DONTWAIT, MT_DATA);
680 m->m_len = MmGetMdlByteCount(buf);
681 m->m_data = MmGetMdlVirtualAddress(buf);
682 MEXTADD(m, m->m_data, m->m_len, ndis_return_packet,
683 m->m_data, p, 0, EXT_NDIS);
687 if (m->m_flags & M_PKTHDR)
695 * This is a hack to deal with the Marvell 8335 driver
696 * which, when associated with an AP in WPA-PSK mode,
697 * seems to overpad its frames by 8 bytes. I don't know
698 * that the extra 8 bytes are for, and they're not there
699 * in open mode, so for now clamp the frame size at 1514
700 * until I can figure out how to deal with this properly,
701 * otherwise if_ethersubr() will spank us by discarding
702 * the 'oversize' frames.
705 eh = mtod((*m0), struct ether_header *);
706 ifp = ((struct ndis_softc *)p->np_softc)->ifp;
707 if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
708 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
712 (*m0)->m_pkthdr.len = totlen;
718 * Create an NDIS packet from an mbuf chain.
719 * This is used mainly when transmitting packets, where we need
720 * to turn an mbuf off an interface's send queue and transform it
721 * into an NDIS packet which will be fed into the NDIS driver's
724 * NDIS packets consist of two parts: an ndis_packet structure,
725 * which is vaguely analagous to the pkthdr portion of an mbuf,
726 * and one or more ndis_buffer structures, which define the
727 * actual memory segments in which the packet data resides.
728 * We need to allocate one ndis_buffer for each mbuf in a chain,
729 * plus one ndis_packet as the header.
738 ndis_buffer *buf = NULL, *prev = NULL;
739 ndis_packet_private *priv;
741 if (p == NULL || *p == NULL || m0 == NULL)
744 priv = &(*p)->np_private;
745 priv->npp_totlen = m0->m_pkthdr.len;
747 for (m = m0; m != NULL; m = m->m_next) {
750 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
752 ndis_free_packet(*p);
756 MmBuildMdlForNonPagedPool(buf);
758 if (priv->npp_head == NULL)
759 priv->npp_head = buf;
761 prev->mdl_next = buf;
765 priv->npp_tail = buf;
771 ndis_get_supported_oids(arg, oids, oidcnt)
779 if (arg == NULL || oids == NULL || oidcnt == NULL)
782 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
784 o = malloc(len, M_DEVBUF, M_NOWAIT);
788 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
802 ndis_set_info(arg, oid, buf, buflen)
808 struct ndis_softc *sc;
811 ndis_setinfo_handler setfunc;
812 uint32_t byteswritten = 0, bytesneeded = 0;
817 * According to the NDIS spec, MiniportQueryInformation()
818 * and MiniportSetInformation() requests are handled serially:
819 * once one request has been issued, we must wait for it to
820 * finish before allowing another request to proceed.
825 KeResetEvent(&sc->ndis_block->nmb_setevent);
827 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
829 if (sc->ndis_block->nmb_pendingreq != NULL) {
830 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
831 panic("ndis_set_info() called while other request pending");
833 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
835 setfunc = sc->ndis_chars->nmc_setinfo_func;
836 adapter = sc->ndis_block->nmb_miniportadapterctx;
838 if (adapter == NULL || setfunc == NULL ||
839 sc->ndis_block->nmb_devicectx == NULL) {
840 sc->ndis_block->nmb_pendingreq = NULL;
841 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
845 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
846 &byteswritten, &bytesneeded);
848 sc->ndis_block->nmb_pendingreq = NULL;
850 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
852 if (rval == NDIS_STATUS_PENDING) {
853 /* Wait up to 5 seconds. */
854 duetime = (5 * 1000000) * -10;
855 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
856 0, 0, FALSE, &duetime);
857 rval = sc->ndis_block->nmb_setstat;
861 *buflen = byteswritten;
863 *buflen = bytesneeded;
865 if (rval == NDIS_STATUS_INVALID_LENGTH)
868 if (rval == NDIS_STATUS_INVALID_OID)
871 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
872 rval == NDIS_STATUS_NOT_ACCEPTED)
875 if (rval != NDIS_STATUS_SUCCESS)
881 typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
884 ndis_send_packets(arg, packets, cnt)
886 ndis_packet **packets;
889 struct ndis_softc *sc;
891 ndis_sendmulti_handler sendfunc;
892 ndis_senddone_func senddonefunc;
898 adapter = sc->ndis_block->nmb_miniportadapterctx;
901 sendfunc = sc->ndis_chars->nmc_sendmulti_func;
902 senddonefunc = sc->ndis_block->nmb_senddone_func;
904 if (NDIS_SERIALIZED(sc->ndis_block))
905 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
907 MSCALL3(sendfunc, adapter, packets, cnt);
909 for (i = 0; i < cnt; i++) {
912 * Either the driver already handed the packet to
913 * ndis_txeof() due to a failure, or it wants to keep
914 * it and release it asynchronously later. Skip to the
917 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
919 MSCALL3(senddonefunc, sc->ndis_block, p, p->np_oob.npo_status);
922 if (NDIS_SERIALIZED(sc->ndis_block))
923 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
929 ndis_send_packet(arg, packet)
933 struct ndis_softc *sc;
936 ndis_sendsingle_handler sendfunc;
937 ndis_senddone_func senddonefunc;
941 adapter = sc->ndis_block->nmb_miniportadapterctx;
944 sendfunc = sc->ndis_chars->nmc_sendsingle_func;
945 senddonefunc = sc->ndis_block->nmb_senddone_func;
947 if (NDIS_SERIALIZED(sc->ndis_block))
948 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
949 status = MSCALL3(sendfunc, adapter, packet,
950 packet->np_private.npp_flags);
952 if (status == NDIS_STATUS_PENDING) {
953 if (NDIS_SERIALIZED(sc->ndis_block))
954 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
958 MSCALL3(senddonefunc, sc->ndis_block, packet, status);
960 if (NDIS_SERIALIZED(sc->ndis_block))
961 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
970 struct ndis_softc *sc;
975 sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
976 M_DEVBUF, M_NOWAIT|M_ZERO);
978 if (sc->ndis_tmaps == NULL)
981 for (i = 0; i < sc->ndis_maxpkts; i++) {
982 error = bus_dmamap_create(sc->ndis_ttag, 0,
985 free(sc->ndis_tmaps, M_DEVBUF);
994 ndis_destroy_dma(arg)
997 struct ndis_softc *sc;
999 ndis_packet *p = NULL;
1004 for (i = 0; i < sc->ndis_maxpkts; i++) {
1005 if (sc->ndis_txarray[i] != NULL) {
1006 p = sc->ndis_txarray[i];
1007 m = (struct mbuf *)p->np_rsvd[1];
1010 ndis_free_packet(sc->ndis_txarray[i]);
1012 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
1015 free(sc->ndis_tmaps, M_DEVBUF);
1017 bus_dma_tag_destroy(sc->ndis_ttag);
1026 struct ndis_softc *sc;
1027 ndis_handle adapter;
1028 ndis_reset_handler resetfunc;
1029 uint8_t addressing_reset;
1036 adapter = sc->ndis_block->nmb_miniportadapterctx;
1037 resetfunc = sc->ndis_chars->nmc_reset_func;
1039 if (adapter == NULL || resetfunc == NULL ||
1040 sc->ndis_block->nmb_devicectx == NULL) {
1047 KeResetEvent(&sc->ndis_block->nmb_resetevent);
1049 if (NDIS_SERIALIZED(sc->ndis_block))
1050 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1052 rval = MSCALL2(resetfunc, &addressing_reset, adapter);
1054 if (NDIS_SERIALIZED(sc->ndis_block))
1055 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1057 if (rval == NDIS_STATUS_PENDING)
1058 KeWaitForSingleObject(&sc->ndis_block->nmb_resetevent,
1068 struct ndis_softc *sc;
1069 ndis_handle adapter;
1070 ndis_halt_handler haltfunc;
1071 ndis_miniport_block *block;
1076 block = sc->ndis_block;
1079 KeFlushQueuedDpcs();
1082 * Wait for all packets to be returned.
1086 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
1087 empty = IsListEmpty(&block->nmb_returnlist);
1088 KeReleaseSpinLock(&block->nmb_returnlock, irql);
1095 adapter = sc->ndis_block->nmb_miniportadapterctx;
1096 if (adapter == NULL) {
1101 sc->ndis_block->nmb_devicectx = NULL;
1104 * The adapter context is only valid after the init
1105 * handler has been called, and is invalid once the
1106 * halt handler has been called.
1109 haltfunc = sc->ndis_chars->nmc_halt_func;
1112 MSCALL1(haltfunc, adapter);
1115 sc->ndis_block->nmb_miniportadapterctx = NULL;
1122 ndis_shutdown_nic(arg)
1125 struct ndis_softc *sc;
1126 ndis_handle adapter;
1127 ndis_shutdown_handler shutdownfunc;
1131 adapter = sc->ndis_block->nmb_miniportadapterctx;
1132 shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1134 if (adapter == NULL || shutdownfunc == NULL)
1137 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1138 MSCALL1(shutdownfunc, adapter);
1140 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
1142 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1148 ndis_pnpevent_nic(arg, type)
1153 struct ndis_softc *sc;
1154 ndis_handle adapter;
1155 ndis_pnpevent_handler pnpeventfunc;
1158 sc = device_get_softc(arg);
1160 adapter = sc->ndis_block->nmb_miniportadapterctx;
1161 pnpeventfunc = sc->ndis_chars->nmc_pnpevent_handler;
1163 if (adapter == NULL || pnpeventfunc == NULL)
1166 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1167 MSCALL4(pnpeventfunc, adapter, type, NULL, 0);
1169 MSCALL4(pnpeventfunc, sc->ndis_chars->nmc_rsvd0, type, NULL, 0);
1178 struct ndis_softc *sc;
1179 ndis_miniport_block *block;
1180 ndis_init_handler initfunc;
1181 ndis_status status, openstatus = 0;
1182 ndis_medium mediumarray[NdisMediumMax];
1183 uint32_t chosenmedium, i;
1190 block = sc->ndis_block;
1191 initfunc = sc->ndis_chars->nmc_init_func;
1194 sc->ndis_block->nmb_timerlist = NULL;
1196 for (i = 0; i < NdisMediumMax; i++)
1199 status = MSCALL6(initfunc, &openstatus, &chosenmedium,
1200 mediumarray, NdisMediumMax, block, block);
1203 * If the init fails, blow away the other exported routines
1204 * we obtained from the driver so we can't call them later.
1205 * If the init failed, none of these will work.
1207 if (status != NDIS_STATUS_SUCCESS) {
1209 sc->ndis_block->nmb_miniportadapterctx = NULL;
1215 * This may look really goofy, but apparently it is possible
1216 * to halt a miniport too soon after it's been initialized.
1217 * After MiniportInitialize() finishes, pause for 1 second
1218 * to give the chip a chance to handle any short-lived timers
1219 * that were set in motion. If we call MiniportHalt() too soon,
1220 * some of the timers may not be cancelled, because the driver
1221 * expects them to fire before the halt is called.
1224 pause("ndwait", hz);
1227 sc->ndis_block->nmb_devicectx = sc;
1234 ndis_intrsetup(dpc, dobj, ip, sc)
1236 device_object *dobj;
1238 struct ndis_softc *sc;
1240 ndis_miniport_interrupt *intr;
1242 intr = sc->ndis_block->nmb_interrupt;
1249 KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
1250 KeResetEvent(&intr->ni_dpcevt);
1251 if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
1253 KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
1257 ndis_get_info(arg, oid, buf, buflen)
1263 struct ndis_softc *sc;
1265 ndis_handle adapter;
1266 ndis_queryinfo_handler queryfunc;
1267 uint32_t byteswritten = 0, bytesneeded = 0;
1273 KeResetEvent(&sc->ndis_block->nmb_getevent);
1275 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1277 if (sc->ndis_block->nmb_pendingreq != NULL) {
1278 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1279 panic("ndis_get_info() called while other request pending");
1281 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
1283 queryfunc = sc->ndis_chars->nmc_queryinfo_func;
1284 adapter = sc->ndis_block->nmb_miniportadapterctx;
1286 if (adapter == NULL || queryfunc == NULL ||
1287 sc->ndis_block->nmb_devicectx == NULL) {
1288 sc->ndis_block->nmb_pendingreq = NULL;
1289 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1293 rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
1294 &byteswritten, &bytesneeded);
1296 sc->ndis_block->nmb_pendingreq = NULL;
1298 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1300 /* Wait for requests that block. */
1302 if (rval == NDIS_STATUS_PENDING) {
1303 /* Wait up to 5 seconds. */
1304 duetime = (5 * 1000000) * -10;
1305 KeWaitForSingleObject(&sc->ndis_block->nmb_getevent,
1306 0, 0, FALSE, &duetime);
1307 rval = sc->ndis_block->nmb_getstat;
1311 *buflen = byteswritten;
1313 *buflen = bytesneeded;
1315 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1316 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1319 if (rval == NDIS_STATUS_INVALID_OID)
1322 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1323 rval == NDIS_STATUS_NOT_ACCEPTED)
1326 if (rval != NDIS_STATUS_SUCCESS)
1333 NdisAddDevice(drv, pdo)
1338 ndis_miniport_block *block;
1339 struct ndis_softc *sc;
1343 sc = device_get_softc(pdo->do_devext);
1345 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
1346 error = bus_setup_intr(sc->ndis_dev, sc->ndis_irq,
1347 INTR_TYPE_NET | INTR_MPSAFE,
1348 NULL, ntoskrnl_intr, NULL, &sc->ndis_intrhand);
1350 return (NDIS_STATUS_FAILURE);
1353 status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1354 FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
1356 if (status != STATUS_SUCCESS)
1359 block = fdo->do_devext;
1361 block->nmb_filterdbs.nf_ethdb = block;
1362 block->nmb_deviceobj = fdo;
1363 block->nmb_physdeviceobj = pdo;
1364 block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
1365 KeInitializeSpinLock(&block->nmb_lock);
1366 KeInitializeSpinLock(&block->nmb_returnlock);
1367 KeInitializeEvent(&block->nmb_getevent, EVENT_TYPE_NOTIFY, TRUE);
1368 KeInitializeEvent(&block->nmb_setevent, EVENT_TYPE_NOTIFY, TRUE);
1369 KeInitializeEvent(&block->nmb_resetevent, EVENT_TYPE_NOTIFY, TRUE);
1370 InitializeListHead(&block->nmb_parmlist);
1371 InitializeListHead(&block->nmb_returnlist);
1372 block->nmb_returnitem = IoAllocateWorkItem(fdo);
1375 * Stash pointers to the miniport block and miniport
1376 * characteristics info in the if_ndis softc so the
1377 * UNIX wrapper driver can get to them later.
1379 sc->ndis_block = block;
1380 sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1);
1383 * If the driver has a MiniportTransferData() function,
1384 * we should allocate a private RX packet pool.
1387 if (sc->ndis_chars->nmc_transferdata_func != NULL) {
1388 NdisAllocatePacketPool(&status, &block->nmb_rxpool,
1389 32, PROTOCOL_RESERVED_SIZE_IN_PACKET);
1390 if (status != NDIS_STATUS_SUCCESS) {
1391 IoDetachDevice(block->nmb_nextdeviceobj);
1392 IoDeleteDevice(fdo);
1395 InitializeListHead((&block->nmb_packetlist));
1398 /* Give interrupt handling priority over timers. */
1399 IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1400 KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
1402 /* Finish up BSD-specific setup. */
1404 block->nmb_signature = (void *)0xcafebabe;
1405 block->nmb_status_func = kernndis_functbl[0].ipt_wrap;
1406 block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap;
1407 block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap;
1408 block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap;
1409 block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap;
1410 block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap;
1411 block->nmb_pendingreq = NULL;
1413 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
1415 return (STATUS_SUCCESS);
1419 ndis_unload_driver(arg)
1422 struct ndis_softc *sc;
1427 if (sc->ndis_intrhand)
1428 bus_teardown_intr(sc->ndis_dev,
1429 sc->ndis_irq, sc->ndis_intrhand);
1431 if (sc->ndis_block->nmb_rlist != NULL)
1432 free(sc->ndis_block->nmb_rlist, M_DEVBUF);
1434 ndis_flush_sysctls(sc);
1436 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1438 if (sc->ndis_chars->nmc_transferdata_func != NULL)
1439 NdisFreePacketPool(sc->ndis_block->nmb_rxpool);
1440 fdo = sc->ndis_block->nmb_deviceobj;
1441 IoFreeWorkItem(sc->ndis_block->nmb_returnitem);
1442 IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj);
1443 IoDeleteDevice(fdo);