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.
32 * $FreeBSD: src/sys/compat/ndis/kern_ndis.c,v 1.111 2011/02/23 21:45:28 brucec Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/unistd.h>
38 #include <sys/types.h>
39 #include <sys/errno.h>
40 #include <sys/callout.h>
41 #include <sys/socket.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
45 #include <sys/malloc.h>
47 #include <sys/mutex.h>
50 #include <sys/kernel.h>
51 #include <sys/module.h>
52 #include <sys/kthread.h>
55 #include <sys/mplock2.h>
58 #include <net/if_arp.h>
59 #include <net/ethernet.h>
60 #include <net/if_dl.h>
61 #include <net/if_media.h>
63 #include <netproto/802_11/ieee80211_var.h>
64 #include <netproto/802_11/ieee80211_ioctl.h>
66 #include <bus/usb/usb.h>
67 #include <bus/usb/usbdi.h>
69 #include <emulation/ndis/pe_var.h>
70 #include <emulation/ndis/cfg_var.h>
71 #include <emulation/ndis/resource_var.h>
72 #include <emulation/ndis/ntoskrnl_var.h>
73 #include <emulation/ndis/ndis_var.h>
74 #include <emulation/ndis/hal_var.h>
75 #include <emulation/ndis/usbd_var.h>
76 #include <dev/netif/ndis/if_ndisvar.h>
78 #define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
80 static void ndis_status_func(ndis_handle, ndis_status, void *, uint32_t);
81 static void ndis_statusdone_func(ndis_handle);
82 static void ndis_setdone_func(ndis_handle, ndis_status);
83 static void ndis_getdone_func(ndis_handle, ndis_status);
84 static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
85 static void ndis_sendrsrcavail_func(ndis_handle);
86 static void ndis_intrsetup(kdpc *, device_object *,
87 irp *, struct ndis_softc *);
88 static void ndis_return(device_object *, void *);
90 static image_patch_table kernndis_functbl[] = {
91 IMPORT_SFUNC(ndis_status_func, 4),
92 IMPORT_SFUNC(ndis_statusdone_func, 1),
93 IMPORT_SFUNC(ndis_setdone_func, 2),
94 IMPORT_SFUNC(ndis_getdone_func, 2),
95 IMPORT_SFUNC(ndis_resetdone_func, 3),
96 IMPORT_SFUNC(ndis_sendrsrcavail_func, 1),
97 IMPORT_SFUNC(ndis_intrsetup, 4),
98 IMPORT_SFUNC(ndis_return, 1),
103 static struct nd_head ndis_devhead;
106 * This allows us to export our symbols to other modules.
107 * Note that we call ourselves 'ndisapi' to avoid a namespace
108 * collision with if_ndis.ko, which internally calls itself
111 * Note: some of the subsystems depend on each other, so the
112 * order in which they're started is important. The order of
115 * HAL - spinlocks and IRQL manipulation
116 * ntoskrnl - DPC and workitem threads, object waiting
117 * windrv - driver/device registration
119 * The HAL should also be the last thing shut down, since
120 * the ntoskrnl subsystem will use spinlocks right up until
121 * the DPC and workitem threads are terminated.
125 ndis_modevent(module_t mod, int cmd, void *arg)
128 image_patch_table *patch;
132 /* Initialize subsystems */
139 patch = kernndis_functbl;
140 while (patch->ipt_func != NULL) {
141 windrv_wrap((funcptr)patch->ipt_func,
142 (funcptr *)&patch->ipt_wrap,
143 patch->ipt_argcnt, patch->ipt_ftype);
147 TAILQ_INIT(&ndis_devhead);
150 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
151 /* Shut down subsystems */
158 patch = kernndis_functbl;
159 while (patch->ipt_func != NULL) {
160 windrv_unwrap(patch->ipt_wrap);
166 /* Shut down subsystems */
173 patch = kernndis_functbl;
174 while (patch->ipt_func != NULL) {
175 windrv_unwrap(patch->ipt_wrap);
187 DEV_MODULE(ndis, ndis_modevent, NULL);
188 MODULE_VERSION(ndis, 1);
191 ndis_sendrsrcavail_func(ndis_handle adapter)
196 ndis_status_func(ndis_handle adapter, ndis_status status, void *sbuf,
199 ndis_miniport_block *block;
200 struct ndis_softc *sc;
204 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
206 if (ifp->if_flags & IFF_DEBUG)
207 device_printf(sc->ndis_dev, "status: %x\n", status);
211 ndis_statusdone_func(ndis_handle adapter)
213 ndis_miniport_block *block;
214 struct ndis_softc *sc;
218 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
220 if (ifp->if_flags & IFF_DEBUG)
221 device_printf(sc->ndis_dev, "status complete\n");
225 ndis_setdone_func(ndis_handle adapter, ndis_status status)
227 ndis_miniport_block *block;
230 block->nmb_setstat = status;
231 KeSetEvent(&block->nmb_setevent, IO_NO_INCREMENT, FALSE);
235 ndis_getdone_func(ndis_handle adapter, ndis_status status)
237 ndis_miniport_block *block;
240 block->nmb_getstat = status;
241 KeSetEvent(&block->nmb_getevent, IO_NO_INCREMENT, FALSE);
245 ndis_resetdone_func(ndis_handle adapter, ndis_status status,
246 uint8_t addressingreset)
248 ndis_miniport_block *block;
249 struct ndis_softc *sc;
253 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
256 if (ifp->if_flags & IFF_DEBUG)
257 device_printf(sc->ndis_dev, "reset done...\n");
258 KeSetEvent(&block->nmb_resetevent, IO_NO_INCREMENT, FALSE);
262 ndis_create_sysctls(void *arg)
264 struct ndis_softc *sc;
267 struct sysctl_oid *oidp;
268 struct sysctl_ctx_entry *e;
274 vals = sc->ndis_regvals;
276 TAILQ_INIT(&sc->ndis_cfglist_head);
278 /* Create the sysctl tree. */
280 sc->ndis_tree = SYSCTL_ADD_NODE(&sc->ndis_ctx,
281 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
282 device_get_nameunit(sc->ndis_dev), CTLFLAG_RD, 0,
283 device_get_desc(sc->ndis_dev));
285 /* Add the driver-specific registry keys. */
288 if (vals->nc_cfgkey == NULL)
291 if (vals->nc_idx != sc->ndis_devidx) {
296 /* See if we already have a sysctl with this name */
299 TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
301 if (strcasecmp(oidp->oid_name, vals->nc_cfgkey) == 0)
311 ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
312 vals->nc_val, CTLFLAG_RW);
316 /* Now add a couple of builtin keys. */
319 * Environment can be either Windows (0) or WindowsNT (1).
320 * We qualify as the latter.
322 ndis_add_sysctl(sc, "Environment",
323 "Windows environment", "1", CTLFLAG_RD);
325 /* NDIS version should be 5.1. */
326 ndis_add_sysctl(sc, "NdisVersion",
327 "NDIS API Version", "0x00050001", CTLFLAG_RD);
330 * Some miniport drivers rely on the existence of the SlotNumber,
331 * NetCfgInstanceId and DriverDesc keys.
333 ndis_add_sysctl(sc, "SlotNumber", "Slot Numer", "01", CTLFLAG_RD);
334 ndis_add_sysctl(sc, "NetCfgInstanceId", "NetCfgInstanceId",
335 "{12345678-1234-5678-CAFE0-123456789ABC}", CTLFLAG_RD);
336 ndis_add_sysctl(sc, "DriverDesc", "Driver Description",
337 "NDIS Network Adapter", CTLFLAG_RD);
339 /* Bus type (PCI, PCMCIA, etc...) */
340 ksprintf(buf, "%d", (int)sc->ndis_iftype);
341 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
343 if (sc->ndis_res_io != NULL) {
344 ksprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
345 ndis_add_sysctl(sc, "IOBaseAddress",
346 "Base I/O Address", buf, CTLFLAG_RD);
349 if (sc->ndis_irq != NULL) {
350 ksprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
351 ndis_add_sysctl(sc, "InterruptNumber",
352 "Interrupt Number", buf, CTLFLAG_RD);
359 ndis_add_sysctl(void *arg, char *key, char *desc, char *val, int flag)
361 struct ndis_softc *sc;
362 struct ndis_cfglist *cfg;
367 cfg = kmalloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_WAITOK|M_ZERO);
368 cfg->ndis_cfg.nc_cfgkey = kstrdup(key, M_DEVBUF);
370 ksnprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
371 cfg->ndis_cfg.nc_cfgdesc = kstrdup(descstr, M_DEVBUF);
373 cfg->ndis_cfg.nc_cfgdesc = kstrdup(desc, M_DEVBUF);
374 strcpy(cfg->ndis_cfg.nc_val, val);
376 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
379 SYSCTL_ADD_STRING(&sc->ndis_ctx, SYSCTL_CHILDREN(sc->ndis_tree),
380 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
381 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
382 cfg->ndis_cfg.nc_cfgdesc);
388 * Somewhere, somebody decided "hey, let's automatically create
389 * a sysctl tree for each device instance as it's created -- it'll
390 * make life so much easier!" Lies. Why must they turn the kernel
391 * into a house of lies?
395 ndis_flush_sysctls(void *arg)
397 struct ndis_softc *sc;
398 struct ndis_cfglist *cfg;
399 struct sysctl_ctx_list *clist;
403 clist = &sc->ndis_ctx;
405 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
406 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
407 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
408 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
409 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
410 kfree(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
411 kfree(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
412 kfree(cfg, M_DEVBUF);
419 ndis_get_routine_address(struct image_patch_table *functbl, char *name)
423 for (i = 0; functbl[i].ipt_name != NULL; i++)
424 if (strcmp(name, functbl[i].ipt_name) == 0)
425 return (functbl[i].ipt_wrap);
430 ndis_return(device_object *dobj, void *arg)
432 ndis_miniport_block *block;
433 ndis_miniport_characteristics *ch;
434 ndis_return_handler returnfunc;
441 ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
444 adapter = block->nmb_miniportadapterctx;
449 returnfunc = ch->nmc_return_packet_func;
451 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
452 while (!IsListEmpty(&block->nmb_returnlist)) {
453 l = RemoveHeadList((&block->nmb_returnlist));
454 p = CONTAINING_RECORD(l, ndis_packet, np_list);
455 InitializeListHead((&p->np_list));
456 KeReleaseSpinLock(&block->nmb_returnlock, irql);
457 MSCALL2(returnfunc, adapter, p);
458 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
460 KeReleaseSpinLock(&block->nmb_returnlock, irql);
464 ndis_reference_packet(void *arg)
473 /* Increment refcount. */
474 atomic_add_int(&p->np_refcnt, 1);
478 ndis_return_packet(void *arg)
481 ndis_miniport_block *block;
488 /* Release packet when refcount hits zero, otherwise return. */
489 if (atomic_fetchadd_int(&p->np_refcnt, -1) > 1)
492 block = ((struct ndis_softc *)p->np_softc)->ndis_block;
494 KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
495 InitializeListHead((&p->np_list));
496 InsertHeadList((&block->nmb_returnlist), (&p->np_list));
497 KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
499 IoQueueWorkItem(block->nmb_returnitem,
500 (io_workitem_func)kernndis_functbl[7].ipt_wrap,
501 WORKQUEUE_CRITICAL, block);
505 ndis_free_bufs(ndis_buffer *b0)
520 ndis_free_packet(ndis_packet *p)
525 ndis_free_bufs(p->np_private.npp_head);
530 ndis_convert_res(void *arg)
532 struct ndis_softc *sc;
533 ndis_resource_list *rl = NULL;
534 cm_partial_resource_desc *prd = NULL;
535 ndis_miniport_block *block;
537 struct resource_list *brl;
538 struct resource_list_entry *brle;
539 struct resource_list brl_rev;
540 struct resource_list_entry *n;
544 block = sc->ndis_block;
547 SLIST_INIT(&brl_rev);
549 rl = kmalloc(sizeof(ndis_resource_list) +
550 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
551 M_DEVBUF, M_WAITOK|M_NULLOK|M_ZERO);
556 rl->cprl_version = 5;
557 rl->cprl_version = 1;
558 rl->cprl_count = sc->ndis_rescnt;
559 prd = rl->cprl_partial_descs;
561 brl = BUS_GET_RESOURCE_LIST(dev, dev);
566 * We have a small problem. Some PCI devices have
567 * multiple I/O ranges. Windows orders them starting
568 * from lowest numbered BAR to highest. We discover
569 * them in that order too, but insert them into a singly
570 * linked list head first, which means when time comes
571 * to traverse the list, we enumerate them in reverse
572 * order. This screws up some drivers which expect the
573 * BARs to be in ascending order so that they can choose
574 * the "first" one as their register space. Unfortunately,
575 * in order to fix this, we have to create our own
576 * temporary list with the entries in reverse order.
579 SLIST_FOREACH(brle, brl, link) {
580 n = kmalloc(sizeof(struct resource_list_entry),
581 M_TEMP, M_WAITOK|M_NULLOK);
586 bcopy((char *)brle, (char *)n,
587 sizeof(struct resource_list_entry));
588 SLIST_INSERT_HEAD(&brl_rev, n, link);
591 SLIST_FOREACH(brle, &brl_rev, link) {
592 switch (brle->type) {
594 prd->cprd_type = CmResourceTypePort;
595 prd->cprd_flags = CM_RESOURCE_PORT_IO;
596 prd->cprd_sharedisp =
597 CmResourceShareDeviceExclusive;
598 prd->u.cprd_port.cprd_start.np_quad =
600 prd->u.cprd_port.cprd_len = brle->count;
603 prd->cprd_type = CmResourceTypeMemory;
605 CM_RESOURCE_MEMORY_READ_WRITE;
606 prd->cprd_sharedisp =
607 CmResourceShareDeviceExclusive;
608 prd->u.cprd_mem.cprd_start.np_quad =
610 prd->u.cprd_mem.cprd_len = brle->count;
613 prd->cprd_type = CmResourceTypeInterrupt;
616 * Always mark interrupt resources as
617 * shared, since in our implementation,
620 prd->cprd_sharedisp =
621 CmResourceShareShared;
622 prd->u.cprd_intr.cprd_level = brle->start;
623 prd->u.cprd_intr.cprd_vector = brle->start;
624 prd->u.cprd_intr.cprd_affinity = 0;
633 block->nmb_rlist = rl;
637 while (!SLIST_EMPTY(&brl_rev)) {
638 n = SLIST_FIRST(&brl_rev);
639 SLIST_REMOVE_HEAD(&brl_rev, link);
647 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
648 * packet, it will hand it to us in the form of an ndis_packet,
649 * which we need to convert to an mbuf that is then handed off
650 * to the stack. Note: we configure the mbuf list so that it uses
651 * the memory regions specified by the ndis_buffer structures in
652 * the ndis_packet as external storage. In most cases, this will
653 * point to a memory region allocated by the driver (either by
654 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
655 * the driver to handle kfree()ing this region for is, so we set up
656 * a dummy no-op free handler for it.
660 ndis_ptom(struct mbuf **m0, ndis_packet *p)
662 struct mbuf *m = NULL, *prev = NULL;
664 ndis_packet_private *priv;
667 struct ether_header *eh;
670 if (p == NULL || m0 == NULL)
673 priv = &p->np_private;
674 buf = priv->npp_head;
677 for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
678 if (buf == priv->npp_head) {
679 /* XXX swildner: why not MT_HEADER? (see FreeBSD) */
680 MGETHDR(m, MB_DONTWAIT, MT_DATA);
682 MGET(m, MB_DONTWAIT, MT_DATA);
689 m->m_len = MmGetMdlByteCount(buf);
690 m->m_data = MmGetMdlVirtualAddress(buf);
691 m_extadd(m, m->m_data, m->m_len, ndis_reference_packet,
692 ndis_return_packet, p);
696 if (m->m_flags & M_PKTHDR)
704 * This is a hack to deal with the Marvell 8335 driver
705 * which, when associated with an AP in WPA-PSK mode,
706 * seems to overpad its frames by 8 bytes. I don't know
707 * that the extra 8 bytes are for, and they're not there
708 * in open mode, so for now clamp the frame size at 1514
709 * until I can figure out how to deal with this properly,
710 * otherwise if_ethersubr() will spank us by discarding
711 * the 'oversize' frames.
714 eh = mtod((*m0), struct ether_header *);
715 ifp = ((struct ndis_softc *)p->np_softc)->ifp;
716 if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
717 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
721 (*m0)->m_pkthdr.len = totlen;
727 * Create an NDIS packet from an mbuf chain.
728 * This is used mainly when transmitting packets, where we need
729 * to turn an mbuf off an interface's send queue and transform it
730 * into an NDIS packet which will be fed into the NDIS driver's
733 * NDIS packets consist of two parts: an ndis_packet structure,
734 * which is vaguely analagous to the pkthdr portion of an mbuf,
735 * and one or more ndis_buffer structures, which define the
736 * actual memory segments in which the packet data resides.
737 * We need to allocate one ndis_buffer for each mbuf in a chain,
738 * plus one ndis_packet as the header.
742 ndis_mtop(struct mbuf *m0, ndis_packet **p)
745 ndis_buffer *buf = NULL, *prev = NULL;
746 ndis_packet_private *priv;
748 if (p == NULL || *p == NULL || m0 == NULL)
751 priv = &(*p)->np_private;
752 priv->npp_totlen = m0->m_pkthdr.len;
754 for (m = m0; m != NULL; m = m->m_next) {
757 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
759 ndis_free_packet(*p);
763 MmBuildMdlForNonPagedPool(buf);
765 if (priv->npp_head == NULL)
766 priv->npp_head = buf;
768 prev->mdl_next = buf;
772 priv->npp_tail = buf;
778 ndis_get_supported_oids(void *arg, ndis_oid **oids, int *oidcnt)
783 if (arg == NULL || oids == NULL || oidcnt == NULL)
786 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
788 o = kmalloc(len, M_DEVBUF, M_WAITOK);
790 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
804 ndis_set_info(void *arg, ndis_oid oid, void *buf, int *buflen)
806 struct ndis_softc *sc;
809 ndis_setinfo_handler setfunc;
810 uint32_t byteswritten = 0, bytesneeded = 0;
815 * According to the NDIS spec, MiniportQueryInformation()
816 * and MiniportSetInformation() requests are handled serially:
817 * once one request has been issued, we must wait for it to
818 * finish before allowing another request to proceed.
823 KeResetEvent(&sc->ndis_block->nmb_setevent);
825 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
827 if (sc->ndis_block->nmb_pendingreq != NULL) {
828 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
829 panic("ndis_set_info() called while other request pending");
831 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
833 setfunc = sc->ndis_chars->nmc_setinfo_func;
834 adapter = sc->ndis_block->nmb_miniportadapterctx;
836 if (adapter == NULL || setfunc == NULL ||
837 sc->ndis_block->nmb_devicectx == NULL) {
838 sc->ndis_block->nmb_pendingreq = NULL;
839 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
843 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
844 &byteswritten, &bytesneeded);
846 sc->ndis_block->nmb_pendingreq = NULL;
848 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
850 if (rval == NDIS_STATUS_PENDING) {
851 /* Wait up to 5 seconds. */
852 duetime = (5 * 1000000) * -10;
853 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
854 0, 0, FALSE, &duetime);
855 rval = sc->ndis_block->nmb_setstat;
859 *buflen = byteswritten;
861 *buflen = bytesneeded;
863 if (rval == NDIS_STATUS_INVALID_LENGTH)
866 if (rval == NDIS_STATUS_INVALID_OID)
869 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
870 rval == NDIS_STATUS_NOT_ACCEPTED)
873 if (rval != NDIS_STATUS_SUCCESS)
879 typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
882 ndis_send_packets(void *arg, ndis_packet **packets, int cnt)
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(void *arg, ndis_packet *packet)
926 struct ndis_softc *sc;
929 ndis_sendsingle_handler sendfunc;
930 ndis_senddone_func senddonefunc;
934 adapter = sc->ndis_block->nmb_miniportadapterctx;
937 sendfunc = sc->ndis_chars->nmc_sendsingle_func;
938 senddonefunc = sc->ndis_block->nmb_senddone_func;
940 if (NDIS_SERIALIZED(sc->ndis_block))
941 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
942 status = MSCALL3(sendfunc, adapter, packet,
943 packet->np_private.npp_flags);
945 if (status == NDIS_STATUS_PENDING) {
946 if (NDIS_SERIALIZED(sc->ndis_block))
947 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
951 MSCALL3(senddonefunc, sc->ndis_block, packet, status);
953 if (NDIS_SERIALIZED(sc->ndis_block))
954 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
960 ndis_init_dma(void *arg)
962 struct ndis_softc *sc;
967 sc->ndis_tmaps = kmalloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
968 M_DEVBUF, M_WAITOK|M_ZERO);
970 for (i = 0; i < sc->ndis_maxpkts; i++) {
971 error = bus_dmamap_create(sc->ndis_ttag, 0,
974 kfree(sc->ndis_tmaps, M_DEVBUF);
983 ndis_destroy_dma(void *arg)
985 struct ndis_softc *sc;
987 ndis_packet *p = NULL;
992 for (i = 0; i < sc->ndis_maxpkts; i++) {
993 if (sc->ndis_txarray[i] != NULL) {
994 p = sc->ndis_txarray[i];
995 m = (struct mbuf *)p->np_rsvd[1];
998 ndis_free_packet(sc->ndis_txarray[i]);
1000 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
1003 kfree(sc->ndis_tmaps, M_DEVBUF);
1005 bus_dma_tag_destroy(sc->ndis_ttag);
1011 ndis_reset_nic(void *arg)
1013 struct ndis_softc *sc;
1014 ndis_handle adapter;
1015 ndis_reset_handler resetfunc;
1016 uint8_t addressing_reset;
1023 adapter = sc->ndis_block->nmb_miniportadapterctx;
1024 resetfunc = sc->ndis_chars->nmc_reset_func;
1026 if (adapter == NULL || resetfunc == NULL ||
1027 sc->ndis_block->nmb_devicectx == NULL) {
1034 KeResetEvent(&sc->ndis_block->nmb_resetevent);
1036 if (NDIS_SERIALIZED(sc->ndis_block))
1037 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1039 rval = MSCALL2(resetfunc, &addressing_reset, adapter);
1041 if (NDIS_SERIALIZED(sc->ndis_block))
1042 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1044 if (rval == NDIS_STATUS_PENDING)
1045 KeWaitForSingleObject(&sc->ndis_block->nmb_resetevent,
1052 ndis_halt_nic(void *arg)
1054 struct ndis_softc *sc;
1055 ndis_handle adapter;
1056 ndis_halt_handler haltfunc;
1057 ndis_miniport_block *block;
1062 block = sc->ndis_block;
1065 KeFlushQueuedDpcs();
1068 * Wait for all packets to be returned.
1072 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
1073 empty = IsListEmpty(&block->nmb_returnlist);
1074 KeReleaseSpinLock(&block->nmb_returnlock, irql);
1081 adapter = sc->ndis_block->nmb_miniportadapterctx;
1082 if (adapter == NULL) {
1087 sc->ndis_block->nmb_devicectx = NULL;
1090 * The adapter context is only valid after the init
1091 * handler has been called, and is invalid once the
1092 * halt handler has been called.
1095 haltfunc = sc->ndis_chars->nmc_halt_func;
1098 MSCALL1(haltfunc, adapter);
1101 sc->ndis_block->nmb_miniportadapterctx = NULL;
1108 ndis_shutdown_nic(void *arg)
1110 struct ndis_softc *sc;
1111 ndis_handle adapter;
1112 ndis_shutdown_handler shutdownfunc;
1116 adapter = sc->ndis_block->nmb_miniportadapterctx;
1117 shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1119 if (adapter == NULL || shutdownfunc == NULL)
1122 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1123 MSCALL1(shutdownfunc, adapter);
1125 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
1127 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1133 ndis_pnpevent_nic(void *arg, int type)
1136 struct ndis_softc *sc;
1137 ndis_handle adapter;
1138 ndis_pnpevent_handler pnpeventfunc;
1141 sc = device_get_softc(arg);
1143 adapter = sc->ndis_block->nmb_miniportadapterctx;
1144 pnpeventfunc = sc->ndis_chars->nmc_pnpevent_handler;
1146 if (adapter == NULL || pnpeventfunc == NULL)
1149 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1150 MSCALL4(pnpeventfunc, adapter, type, NULL, 0);
1152 MSCALL4(pnpeventfunc, sc->ndis_chars->nmc_rsvd0, type, NULL, 0);
1158 ndis_init_nic(void *arg)
1160 struct ndis_softc *sc;
1161 ndis_miniport_block *block;
1162 ndis_init_handler initfunc;
1163 ndis_status status, openstatus = 0;
1164 ndis_medium mediumarray[NdisMediumMax];
1165 uint32_t chosenmedium, i;
1172 block = sc->ndis_block;
1173 initfunc = sc->ndis_chars->nmc_init_func;
1176 sc->ndis_block->nmb_timerlist = NULL;
1178 for (i = 0; i < NdisMediumMax; i++)
1181 status = MSCALL6(initfunc, &openstatus, &chosenmedium,
1182 mediumarray, NdisMediumMax, block, block);
1185 * If the init fails, blow away the other exported routines
1186 * we obtained from the driver so we can't call them later.
1187 * If the init failed, none of these will work.
1189 if (status != NDIS_STATUS_SUCCESS) {
1191 sc->ndis_block->nmb_miniportadapterctx = NULL;
1197 * This may look really goofy, but apparently it is possible
1198 * to halt a miniport too soon after it's been initialized.
1199 * After MiniportInitialize() finishes, pause for 1 second
1200 * to give the chip a chance to handle any short-lived timers
1201 * that were set in motion. If we call MiniportHalt() too soon,
1202 * some of the timers may not be cancelled, because the driver
1203 * expects them to fire before the halt is called.
1206 tsleep(arg, 0, "ndwait", hz);
1209 sc->ndis_block->nmb_devicectx = sc;
1216 ndis_intrsetup(kdpc *dpc, device_object *dobj, irp *ip, struct ndis_softc *sc)
1218 ndis_miniport_interrupt *intr;
1220 intr = sc->ndis_block->nmb_interrupt;
1227 KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
1228 KeResetEvent(&intr->ni_dpcevt);
1229 if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
1231 KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
1235 ndis_get_info(void *arg, ndis_oid oid, void *buf, int *buflen)
1237 struct ndis_softc *sc;
1239 ndis_handle adapter;
1240 ndis_queryinfo_handler queryfunc;
1241 uint32_t byteswritten = 0, bytesneeded = 0;
1247 KeResetEvent(&sc->ndis_block->nmb_getevent);
1249 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1251 if (sc->ndis_block->nmb_pendingreq != NULL) {
1252 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1253 panic("ndis_get_info() called while other request pending");
1255 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
1257 queryfunc = sc->ndis_chars->nmc_queryinfo_func;
1258 adapter = sc->ndis_block->nmb_miniportadapterctx;
1260 if (adapter == NULL || queryfunc == NULL ||
1261 sc->ndis_block->nmb_devicectx == NULL) {
1262 sc->ndis_block->nmb_pendingreq = NULL;
1263 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1267 rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
1268 &byteswritten, &bytesneeded);
1270 sc->ndis_block->nmb_pendingreq = NULL;
1272 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1274 /* Wait for requests that block. */
1276 if (rval == NDIS_STATUS_PENDING) {
1277 /* Wait up to 5 seconds. */
1278 duetime = (5 * 1000000) * -10;
1279 KeWaitForSingleObject(&sc->ndis_block->nmb_getevent,
1280 0, 0, FALSE, &duetime);
1281 rval = sc->ndis_block->nmb_getstat;
1285 *buflen = byteswritten;
1287 *buflen = bytesneeded;
1289 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1290 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1293 if (rval == NDIS_STATUS_INVALID_OID)
1296 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1297 rval == NDIS_STATUS_NOT_ACCEPTED)
1300 if (rval != NDIS_STATUS_SUCCESS)
1307 NdisAddDevice(driver_object *drv, device_object *pdo)
1310 ndis_miniport_block *block;
1311 struct ndis_softc *sc;
1315 sc = device_get_softc(pdo->do_devext);
1317 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
1318 error = bus_setup_intr(sc->ndis_dev, sc->ndis_irq,
1320 ntoskrnl_intr, sc, &sc->ndis_intrhand, NULL);
1322 return (NDIS_STATUS_FAILURE);
1325 status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1326 FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
1328 if (status != STATUS_SUCCESS)
1331 block = fdo->do_devext;
1333 block->nmb_filterdbs.nf_ethdb = block;
1334 block->nmb_deviceobj = fdo;
1335 block->nmb_physdeviceobj = pdo;
1336 block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
1337 KeInitializeSpinLock(&block->nmb_lock);
1338 KeInitializeSpinLock(&block->nmb_returnlock);
1339 KeInitializeEvent(&block->nmb_getevent, EVENT_TYPE_NOTIFY, TRUE);
1340 KeInitializeEvent(&block->nmb_setevent, EVENT_TYPE_NOTIFY, TRUE);
1341 KeInitializeEvent(&block->nmb_resetevent, EVENT_TYPE_NOTIFY, TRUE);
1342 InitializeListHead(&block->nmb_parmlist);
1343 InitializeListHead(&block->nmb_returnlist);
1344 block->nmb_returnitem = IoAllocateWorkItem(fdo);
1347 * Stash pointers to the miniport block and miniport
1348 * characteristics info in the if_ndis softc so the
1349 * UNIX wrapper driver can get to them later.
1351 sc->ndis_block = block;
1352 sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1);
1355 * If the driver has a MiniportTransferData() function,
1356 * we should allocate a private RX packet pool.
1359 if (sc->ndis_chars->nmc_transferdata_func != NULL) {
1360 NdisAllocatePacketPool(&status, &block->nmb_rxpool,
1361 32, PROTOCOL_RESERVED_SIZE_IN_PACKET);
1362 if (status != NDIS_STATUS_SUCCESS) {
1363 IoDetachDevice(block->nmb_nextdeviceobj);
1364 IoDeleteDevice(fdo);
1367 InitializeListHead((&block->nmb_packetlist));
1370 /* Give interrupt handling priority over timers. */
1371 IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1372 KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
1374 /* Finish up BSD-specific setup. */
1376 block->nmb_signature = (void *)0xcafebabe;
1377 block->nmb_status_func = kernndis_functbl[0].ipt_wrap;
1378 block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap;
1379 block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap;
1380 block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap;
1381 block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap;
1382 block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap;
1383 block->nmb_pendingreq = NULL;
1385 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
1387 return (STATUS_SUCCESS);
1391 ndis_unload_driver(void *arg)
1393 struct ndis_softc *sc;
1398 if (sc->ndis_intrhand)
1399 bus_teardown_intr(sc->ndis_dev,
1400 sc->ndis_irq, sc->ndis_intrhand);
1402 if (sc->ndis_block->nmb_rlist != NULL)
1403 kfree(sc->ndis_block->nmb_rlist, M_DEVBUF);
1405 ndis_flush_sysctls(sc);
1407 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1409 if (sc->ndis_chars->nmc_transferdata_func != NULL)
1410 NdisFreePacketPool(sc->ndis_block->nmb_rxpool);
1411 fdo = sc->ndis_block->nmb_deviceobj;
1412 IoFreeWorkItem(sc->ndis_block->nmb_returnitem);
1413 IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj);
1414 IoDeleteDevice(fdo);