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.
108 * Note: some of the subsystems depend on each other, so the
109 * order in which they're started is important. The order of
112 * HAL - spinlocks and IRQL manipulation
113 * ntoskrnl - DPC and workitem threads, object waiting
114 * windrv - driver/device registration
116 * The HAL should also be the last thing shut down, since
117 * the ntoskrnl subsystem will use spinlocks right up until
118 * the DPC and workitem threads are terminated.
122 ndis_modevent(module_t mod, int cmd, void *arg)
125 image_patch_table *patch;
129 /* Initialize subsystems */
136 patch = kernndis_functbl;
137 while (patch->ipt_func != NULL) {
138 windrv_wrap((funcptr)patch->ipt_func,
139 (funcptr *)&patch->ipt_wrap,
140 patch->ipt_argcnt, patch->ipt_ftype);
144 TAILQ_INIT(&ndis_devhead);
147 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
148 /* Shut down subsystems */
155 patch = kernndis_functbl;
156 while (patch->ipt_func != NULL) {
157 windrv_unwrap(patch->ipt_wrap);
163 /* Shut down subsystems */
170 patch = kernndis_functbl;
171 while (patch->ipt_func != NULL) {
172 windrv_unwrap(patch->ipt_wrap);
184 DEV_MODULE(ndis, ndis_modevent, NULL);
185 MODULE_VERSION(ndis, 1);
188 ndis_sendrsrcavail_func(ndis_handle adapter)
193 ndis_status_func(ndis_handle adapter, ndis_status status, void *sbuf,
196 ndis_miniport_block *block;
197 struct ndis_softc *sc;
201 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
203 if (ifp->if_flags & IFF_DEBUG)
204 device_printf(sc->ndis_dev, "status: %x\n", status);
208 ndis_statusdone_func(ndis_handle adapter)
210 ndis_miniport_block *block;
211 struct ndis_softc *sc;
215 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
217 if (ifp->if_flags & IFF_DEBUG)
218 device_printf(sc->ndis_dev, "status complete\n");
222 ndis_setdone_func(ndis_handle adapter, ndis_status status)
224 ndis_miniport_block *block;
227 block->nmb_setstat = status;
228 KeSetEvent(&block->nmb_setevent, IO_NO_INCREMENT, FALSE);
232 ndis_getdone_func(ndis_handle adapter, ndis_status status)
234 ndis_miniport_block *block;
237 block->nmb_getstat = status;
238 KeSetEvent(&block->nmb_getevent, IO_NO_INCREMENT, FALSE);
242 ndis_resetdone_func(ndis_handle adapter, ndis_status status,
243 uint8_t addressingreset)
245 ndis_miniport_block *block;
246 struct ndis_softc *sc;
250 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
253 if (ifp->if_flags & IFF_DEBUG)
254 device_printf(sc->ndis_dev, "reset done...\n");
255 KeSetEvent(&block->nmb_resetevent, IO_NO_INCREMENT, FALSE);
259 ndis_create_sysctls(void *arg)
261 struct ndis_softc *sc;
264 struct sysctl_oid *oidp;
265 struct sysctl_ctx_entry *e;
271 vals = sc->ndis_regvals;
273 TAILQ_INIT(&sc->ndis_cfglist_head);
275 /* Create the sysctl tree. */
277 sc->ndis_tree = SYSCTL_ADD_NODE(&sc->ndis_ctx,
278 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
279 device_get_nameunit(sc->ndis_dev), CTLFLAG_RD, 0,
280 device_get_desc(sc->ndis_dev));
282 /* Add the driver-specific registry keys. */
285 if (vals->nc_cfgkey == NULL)
288 if (vals->nc_idx != sc->ndis_devidx) {
293 /* See if we already have a sysctl with this name */
296 TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
298 if (strcasecmp(oidp->oid_name, vals->nc_cfgkey) == 0)
308 ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
309 vals->nc_val, CTLFLAG_RW);
313 /* Now add a couple of builtin keys. */
316 * Environment can be either Windows (0) or WindowsNT (1).
317 * We qualify as the latter.
319 ndis_add_sysctl(sc, "Environment",
320 "Windows environment", "1", CTLFLAG_RD);
322 /* NDIS version should be 5.1. */
323 ndis_add_sysctl(sc, "NdisVersion",
324 "NDIS API Version", "0x00050001", CTLFLAG_RD);
327 * Some miniport drivers rely on the existence of the SlotNumber,
328 * NetCfgInstanceId and DriverDesc keys.
330 ndis_add_sysctl(sc, "SlotNumber", "Slot Numer", "01", CTLFLAG_RD);
331 ndis_add_sysctl(sc, "NetCfgInstanceId", "NetCfgInstanceId",
332 "{12345678-1234-5678-CAFE0-123456789ABC}", CTLFLAG_RD);
333 ndis_add_sysctl(sc, "DriverDesc", "Driver Description",
334 "NDIS Network Adapter", CTLFLAG_RD);
336 /* Bus type (PCI, PCMCIA, etc...) */
337 ksprintf(buf, "%d", (int)sc->ndis_iftype);
338 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
340 if (sc->ndis_res_io != NULL) {
341 ksprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
342 ndis_add_sysctl(sc, "IOBaseAddress",
343 "Base I/O Address", buf, CTLFLAG_RD);
346 if (sc->ndis_irq != NULL) {
347 ksprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
348 ndis_add_sysctl(sc, "InterruptNumber",
349 "Interrupt Number", buf, CTLFLAG_RD);
356 ndis_add_sysctl(void *arg, char *key, char *desc, char *val, int flag)
358 struct ndis_softc *sc;
359 struct ndis_cfglist *cfg;
364 cfg = kmalloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_WAITOK|M_ZERO);
365 cfg->ndis_cfg.nc_cfgkey = kstrdup(key, M_DEVBUF);
367 ksnprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
368 cfg->ndis_cfg.nc_cfgdesc = kstrdup(descstr, M_DEVBUF);
370 cfg->ndis_cfg.nc_cfgdesc = kstrdup(desc, M_DEVBUF);
371 strcpy(cfg->ndis_cfg.nc_val, val);
373 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
376 SYSCTL_ADD_STRING(&sc->ndis_ctx, SYSCTL_CHILDREN(sc->ndis_tree),
377 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
378 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
379 cfg->ndis_cfg.nc_cfgdesc);
385 * Somewhere, somebody decided "hey, let's automatically create
386 * a sysctl tree for each device instance as it's created -- it'll
387 * make life so much easier!" Lies. Why must they turn the kernel
388 * into a house of lies?
392 ndis_flush_sysctls(void *arg)
394 struct ndis_softc *sc;
395 struct ndis_cfglist *cfg;
396 struct sysctl_ctx_list *clist;
400 clist = &sc->ndis_ctx;
402 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
403 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
404 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
405 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
406 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
407 kfree(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
408 kfree(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
409 kfree(cfg, M_DEVBUF);
416 ndis_get_routine_address(struct image_patch_table *functbl, char *name)
420 for (i = 0; functbl[i].ipt_name != NULL; i++)
421 if (strcmp(name, functbl[i].ipt_name) == 0)
422 return (functbl[i].ipt_wrap);
427 ndis_return(device_object *dobj, void *arg)
429 ndis_miniport_block *block;
430 ndis_miniport_characteristics *ch;
431 ndis_return_handler returnfunc;
438 ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
441 adapter = block->nmb_miniportadapterctx;
446 returnfunc = ch->nmc_return_packet_func;
448 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
449 while (!IsListEmpty(&block->nmb_returnlist)) {
450 l = RemoveHeadList((&block->nmb_returnlist));
451 p = CONTAINING_RECORD(l, ndis_packet, np_list);
452 InitializeListHead((&p->np_list));
453 KeReleaseSpinLock(&block->nmb_returnlock, irql);
454 MSCALL2(returnfunc, adapter, p);
455 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
457 KeReleaseSpinLock(&block->nmb_returnlock, irql);
461 ndis_reference_packet(void *arg)
470 /* Increment refcount. */
471 atomic_add_int(&p->np_refcnt, 1);
475 ndis_return_packet(void *arg)
478 ndis_miniport_block *block;
485 /* Release packet when refcount hits zero, otherwise return. */
486 if (atomic_fetchadd_int(&p->np_refcnt, -1) > 1)
489 block = ((struct ndis_softc *)p->np_softc)->ndis_block;
491 KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
492 InitializeListHead((&p->np_list));
493 InsertHeadList((&block->nmb_returnlist), (&p->np_list));
494 KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
496 IoQueueWorkItem(block->nmb_returnitem,
497 (io_workitem_func)kernndis_functbl[7].ipt_wrap,
498 WORKQUEUE_CRITICAL, block);
502 ndis_free_bufs(ndis_buffer *b0)
517 ndis_free_packet(ndis_packet *p)
522 ndis_free_bufs(p->np_private.npp_head);
527 ndis_convert_res(void *arg)
529 struct ndis_softc *sc;
530 ndis_resource_list *rl = NULL;
531 cm_partial_resource_desc *prd = NULL;
532 ndis_miniport_block *block;
534 struct resource_list *brl;
535 struct resource_list_entry *brle;
536 struct resource_list brl_rev;
537 struct resource_list_entry *n;
541 block = sc->ndis_block;
544 SLIST_INIT(&brl_rev);
546 rl = kmalloc(sizeof(ndis_resource_list) +
547 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
548 M_DEVBUF, M_WAITOK|M_NULLOK|M_ZERO);
553 rl->cprl_version = 5;
554 rl->cprl_version = 1;
555 rl->cprl_count = sc->ndis_rescnt;
556 prd = rl->cprl_partial_descs;
558 brl = BUS_GET_RESOURCE_LIST(dev, dev);
563 * We have a small problem. Some PCI devices have
564 * multiple I/O ranges. Windows orders them starting
565 * from lowest numbered BAR to highest. We discover
566 * them in that order too, but insert them into a singly
567 * linked list head first, which means when time comes
568 * to traverse the list, we enumerate them in reverse
569 * order. This screws up some drivers which expect the
570 * BARs to be in ascending order so that they can choose
571 * the "first" one as their register space. Unfortunately,
572 * in order to fix this, we have to create our own
573 * temporary list with the entries in reverse order.
576 SLIST_FOREACH(brle, brl, link) {
577 n = kmalloc(sizeof(struct resource_list_entry),
578 M_TEMP, M_WAITOK|M_NULLOK);
583 bcopy((char *)brle, (char *)n,
584 sizeof(struct resource_list_entry));
585 SLIST_INSERT_HEAD(&brl_rev, n, link);
588 SLIST_FOREACH(brle, &brl_rev, link) {
589 switch (brle->type) {
591 prd->cprd_type = CmResourceTypePort;
592 prd->cprd_flags = CM_RESOURCE_PORT_IO;
593 prd->cprd_sharedisp =
594 CmResourceShareDeviceExclusive;
595 prd->u.cprd_port.cprd_start.np_quad =
597 prd->u.cprd_port.cprd_len = brle->count;
600 prd->cprd_type = CmResourceTypeMemory;
602 CM_RESOURCE_MEMORY_READ_WRITE;
603 prd->cprd_sharedisp =
604 CmResourceShareDeviceExclusive;
605 prd->u.cprd_mem.cprd_start.np_quad =
607 prd->u.cprd_mem.cprd_len = brle->count;
610 prd->cprd_type = CmResourceTypeInterrupt;
613 * Always mark interrupt resources as
614 * shared, since in our implementation,
617 prd->cprd_sharedisp =
618 CmResourceShareShared;
619 prd->u.cprd_intr.cprd_level = brle->start;
620 prd->u.cprd_intr.cprd_vector = brle->start;
621 prd->u.cprd_intr.cprd_affinity = 0;
630 block->nmb_rlist = rl;
634 while (!SLIST_EMPTY(&brl_rev)) {
635 n = SLIST_FIRST(&brl_rev);
636 SLIST_REMOVE_HEAD(&brl_rev, link);
644 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
645 * packet, it will hand it to us in the form of an ndis_packet,
646 * which we need to convert to an mbuf that is then handed off
647 * to the stack. Note: we configure the mbuf list so that it uses
648 * the memory regions specified by the ndis_buffer structures in
649 * the ndis_packet as external storage. In most cases, this will
650 * point to a memory region allocated by the driver (either by
651 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
652 * the driver to handle kfree()ing this region for is, so we set up
653 * a dummy no-op free handler for it.
657 ndis_ptom(struct mbuf **m0, ndis_packet *p)
659 struct mbuf *m = NULL, *prev = NULL;
661 ndis_packet_private *priv;
664 struct ether_header *eh;
667 if (p == NULL || m0 == NULL)
670 priv = &p->np_private;
671 buf = priv->npp_head;
674 for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
675 if (buf == priv->npp_head) {
676 /* XXX swildner: why not MT_HEADER? (see FreeBSD) */
677 MGETHDR(m, MB_DONTWAIT, MT_DATA);
679 MGET(m, MB_DONTWAIT, MT_DATA);
686 m->m_len = MmGetMdlByteCount(buf);
687 m->m_data = MmGetMdlVirtualAddress(buf);
688 m_extadd(m, m->m_data, m->m_len, ndis_reference_packet,
689 ndis_return_packet, p);
693 if (m->m_flags & M_PKTHDR)
701 * This is a hack to deal with the Marvell 8335 driver
702 * which, when associated with an AP in WPA-PSK mode,
703 * seems to overpad its frames by 8 bytes. I don't know
704 * that the extra 8 bytes are for, and they're not there
705 * in open mode, so for now clamp the frame size at 1514
706 * until I can figure out how to deal with this properly,
707 * otherwise if_ethersubr() will spank us by discarding
708 * the 'oversize' frames.
711 eh = mtod((*m0), struct ether_header *);
712 ifp = ((struct ndis_softc *)p->np_softc)->ifp;
713 if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
714 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
718 (*m0)->m_pkthdr.len = totlen;
724 * Create an NDIS packet from an mbuf chain.
725 * This is used mainly when transmitting packets, where we need
726 * to turn an mbuf off an interface's send queue and transform it
727 * into an NDIS packet which will be fed into the NDIS driver's
730 * NDIS packets consist of two parts: an ndis_packet structure,
731 * which is vaguely analagous to the pkthdr portion of an mbuf,
732 * and one or more ndis_buffer structures, which define the
733 * actual memory segments in which the packet data resides.
734 * We need to allocate one ndis_buffer for each mbuf in a chain,
735 * plus one ndis_packet as the header.
739 ndis_mtop(struct mbuf *m0, ndis_packet **p)
742 ndis_buffer *buf = NULL, *prev = NULL;
743 ndis_packet_private *priv;
745 if (p == NULL || *p == NULL || m0 == NULL)
748 priv = &(*p)->np_private;
749 priv->npp_totlen = m0->m_pkthdr.len;
751 for (m = m0; m != NULL; m = m->m_next) {
754 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
756 ndis_free_packet(*p);
760 MmBuildMdlForNonPagedPool(buf);
762 if (priv->npp_head == NULL)
763 priv->npp_head = buf;
765 prev->mdl_next = buf;
769 priv->npp_tail = buf;
775 ndis_get_supported_oids(void *arg, ndis_oid **oids, int *oidcnt)
780 if (arg == NULL || oids == NULL || oidcnt == NULL)
783 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
785 o = kmalloc(len, M_DEVBUF, M_WAITOK);
787 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
801 ndis_set_info(void *arg, ndis_oid oid, void *buf, int *buflen)
803 struct ndis_softc *sc;
806 ndis_setinfo_handler setfunc;
807 uint32_t byteswritten = 0, bytesneeded = 0;
812 * According to the NDIS spec, MiniportQueryInformation()
813 * and MiniportSetInformation() requests are handled serially:
814 * once one request has been issued, we must wait for it to
815 * finish before allowing another request to proceed.
820 KeResetEvent(&sc->ndis_block->nmb_setevent);
822 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
824 if (sc->ndis_block->nmb_pendingreq != NULL) {
825 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
826 panic("ndis_set_info() called while other request pending");
828 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
830 setfunc = sc->ndis_chars->nmc_setinfo_func;
831 adapter = sc->ndis_block->nmb_miniportadapterctx;
833 if (adapter == NULL || setfunc == NULL ||
834 sc->ndis_block->nmb_devicectx == NULL) {
835 sc->ndis_block->nmb_pendingreq = NULL;
836 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
840 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
841 &byteswritten, &bytesneeded);
843 sc->ndis_block->nmb_pendingreq = NULL;
845 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
847 if (rval == NDIS_STATUS_PENDING) {
848 /* Wait up to 5 seconds. */
849 duetime = (5 * 1000000) * -10;
850 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
851 0, 0, FALSE, &duetime);
852 rval = sc->ndis_block->nmb_setstat;
856 *buflen = byteswritten;
858 *buflen = bytesneeded;
860 if (rval == NDIS_STATUS_INVALID_LENGTH)
863 if (rval == NDIS_STATUS_INVALID_OID)
866 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
867 rval == NDIS_STATUS_NOT_ACCEPTED)
870 if (rval != NDIS_STATUS_SUCCESS)
876 typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
879 ndis_send_packets(void *arg, ndis_packet **packets, int cnt)
881 struct ndis_softc *sc;
883 ndis_sendmulti_handler sendfunc;
884 ndis_senddone_func senddonefunc;
890 adapter = sc->ndis_block->nmb_miniportadapterctx;
893 sendfunc = sc->ndis_chars->nmc_sendmulti_func;
894 senddonefunc = sc->ndis_block->nmb_senddone_func;
896 if (NDIS_SERIALIZED(sc->ndis_block))
897 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
899 MSCALL3(sendfunc, adapter, packets, cnt);
901 for (i = 0; i < cnt; i++) {
904 * Either the driver already handed the packet to
905 * ndis_txeof() due to a failure, or it wants to keep
906 * it and release it asynchronously later. Skip to the
909 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
911 MSCALL3(senddonefunc, sc->ndis_block, p, p->np_oob.npo_status);
914 if (NDIS_SERIALIZED(sc->ndis_block))
915 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
921 ndis_send_packet(void *arg, ndis_packet *packet)
923 struct ndis_softc *sc;
926 ndis_sendsingle_handler sendfunc;
927 ndis_senddone_func senddonefunc;
931 adapter = sc->ndis_block->nmb_miniportadapterctx;
934 sendfunc = sc->ndis_chars->nmc_sendsingle_func;
935 senddonefunc = sc->ndis_block->nmb_senddone_func;
937 if (NDIS_SERIALIZED(sc->ndis_block))
938 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
939 status = MSCALL3(sendfunc, adapter, packet,
940 packet->np_private.npp_flags);
942 if (status == NDIS_STATUS_PENDING) {
943 if (NDIS_SERIALIZED(sc->ndis_block))
944 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
948 MSCALL3(senddonefunc, sc->ndis_block, packet, status);
950 if (NDIS_SERIALIZED(sc->ndis_block))
951 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
957 ndis_init_dma(void *arg)
959 struct ndis_softc *sc;
964 sc->ndis_tmaps = kmalloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
965 M_DEVBUF, M_WAITOK|M_ZERO);
967 for (i = 0; i < sc->ndis_maxpkts; i++) {
968 error = bus_dmamap_create(sc->ndis_ttag, 0,
971 kfree(sc->ndis_tmaps, M_DEVBUF);
980 ndis_destroy_dma(void *arg)
982 struct ndis_softc *sc;
984 ndis_packet *p = NULL;
989 for (i = 0; i < sc->ndis_maxpkts; i++) {
990 if (sc->ndis_txarray[i] != NULL) {
991 p = sc->ndis_txarray[i];
992 m = (struct mbuf *)p->np_rsvd[1];
995 ndis_free_packet(sc->ndis_txarray[i]);
997 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
1000 kfree(sc->ndis_tmaps, M_DEVBUF);
1002 bus_dma_tag_destroy(sc->ndis_ttag);
1008 ndis_reset_nic(void *arg)
1010 struct ndis_softc *sc;
1011 ndis_handle adapter;
1012 ndis_reset_handler resetfunc;
1013 uint8_t addressing_reset;
1020 adapter = sc->ndis_block->nmb_miniportadapterctx;
1021 resetfunc = sc->ndis_chars->nmc_reset_func;
1023 if (adapter == NULL || resetfunc == NULL ||
1024 sc->ndis_block->nmb_devicectx == NULL) {
1031 KeResetEvent(&sc->ndis_block->nmb_resetevent);
1033 if (NDIS_SERIALIZED(sc->ndis_block))
1034 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1036 rval = MSCALL2(resetfunc, &addressing_reset, adapter);
1038 if (NDIS_SERIALIZED(sc->ndis_block))
1039 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1041 if (rval == NDIS_STATUS_PENDING)
1042 KeWaitForSingleObject(&sc->ndis_block->nmb_resetevent,
1049 ndis_halt_nic(void *arg)
1051 struct ndis_softc *sc;
1052 ndis_handle adapter;
1053 ndis_halt_handler haltfunc;
1054 ndis_miniport_block *block;
1059 block = sc->ndis_block;
1062 KeFlushQueuedDpcs();
1065 * Wait for all packets to be returned.
1069 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
1070 empty = IsListEmpty(&block->nmb_returnlist);
1071 KeReleaseSpinLock(&block->nmb_returnlock, irql);
1078 adapter = sc->ndis_block->nmb_miniportadapterctx;
1079 if (adapter == NULL) {
1084 sc->ndis_block->nmb_devicectx = NULL;
1087 * The adapter context is only valid after the init
1088 * handler has been called, and is invalid once the
1089 * halt handler has been called.
1092 haltfunc = sc->ndis_chars->nmc_halt_func;
1095 MSCALL1(haltfunc, adapter);
1098 sc->ndis_block->nmb_miniportadapterctx = NULL;
1105 ndis_shutdown_nic(void *arg)
1107 struct ndis_softc *sc;
1108 ndis_handle adapter;
1109 ndis_shutdown_handler shutdownfunc;
1113 adapter = sc->ndis_block->nmb_miniportadapterctx;
1114 shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1116 if (adapter == NULL || shutdownfunc == NULL)
1119 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1120 MSCALL1(shutdownfunc, adapter);
1122 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
1124 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1130 ndis_pnpevent_nic(void *arg, int type)
1133 struct ndis_softc *sc;
1134 ndis_handle adapter;
1135 ndis_pnpevent_handler pnpeventfunc;
1138 sc = device_get_softc(arg);
1140 adapter = sc->ndis_block->nmb_miniportadapterctx;
1141 pnpeventfunc = sc->ndis_chars->nmc_pnpevent_handler;
1143 if (adapter == NULL || pnpeventfunc == NULL)
1146 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1147 MSCALL4(pnpeventfunc, adapter, type, NULL, 0);
1149 MSCALL4(pnpeventfunc, sc->ndis_chars->nmc_rsvd0, type, NULL, 0);
1155 ndis_init_nic(void *arg)
1157 struct ndis_softc *sc;
1158 ndis_miniport_block *block;
1159 ndis_init_handler initfunc;
1160 ndis_status status, openstatus = 0;
1161 ndis_medium mediumarray[NdisMediumMax];
1162 uint32_t chosenmedium, i;
1169 block = sc->ndis_block;
1170 initfunc = sc->ndis_chars->nmc_init_func;
1173 sc->ndis_block->nmb_timerlist = NULL;
1175 for (i = 0; i < NdisMediumMax; i++)
1178 status = MSCALL6(initfunc, &openstatus, &chosenmedium,
1179 mediumarray, NdisMediumMax, block, block);
1182 * If the init fails, blow away the other exported routines
1183 * we obtained from the driver so we can't call them later.
1184 * If the init failed, none of these will work.
1186 if (status != NDIS_STATUS_SUCCESS) {
1188 sc->ndis_block->nmb_miniportadapterctx = NULL;
1194 * This may look really goofy, but apparently it is possible
1195 * to halt a miniport too soon after it's been initialized.
1196 * After MiniportInitialize() finishes, pause for 1 second
1197 * to give the chip a chance to handle any short-lived timers
1198 * that were set in motion. If we call MiniportHalt() too soon,
1199 * some of the timers may not be cancelled, because the driver
1200 * expects them to fire before the halt is called.
1203 tsleep(arg, 0, "ndwait", hz);
1206 sc->ndis_block->nmb_devicectx = sc;
1213 ndis_intrsetup(kdpc *dpc, device_object *dobj, irp *ip, struct ndis_softc *sc)
1215 ndis_miniport_interrupt *intr;
1217 intr = sc->ndis_block->nmb_interrupt;
1224 KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
1225 KeResetEvent(&intr->ni_dpcevt);
1226 if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
1228 KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
1232 ndis_get_info(void *arg, ndis_oid oid, void *buf, int *buflen)
1234 struct ndis_softc *sc;
1236 ndis_handle adapter;
1237 ndis_queryinfo_handler queryfunc;
1238 uint32_t byteswritten = 0, bytesneeded = 0;
1244 KeResetEvent(&sc->ndis_block->nmb_getevent);
1246 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1248 if (sc->ndis_block->nmb_pendingreq != NULL) {
1249 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1250 panic("ndis_get_info() called while other request pending");
1252 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
1254 queryfunc = sc->ndis_chars->nmc_queryinfo_func;
1255 adapter = sc->ndis_block->nmb_miniportadapterctx;
1257 if (adapter == NULL || queryfunc == NULL ||
1258 sc->ndis_block->nmb_devicectx == NULL) {
1259 sc->ndis_block->nmb_pendingreq = NULL;
1260 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1264 rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
1265 &byteswritten, &bytesneeded);
1267 sc->ndis_block->nmb_pendingreq = NULL;
1269 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1271 /* Wait for requests that block. */
1273 if (rval == NDIS_STATUS_PENDING) {
1274 /* Wait up to 5 seconds. */
1275 duetime = (5 * 1000000) * -10;
1276 KeWaitForSingleObject(&sc->ndis_block->nmb_getevent,
1277 0, 0, FALSE, &duetime);
1278 rval = sc->ndis_block->nmb_getstat;
1282 *buflen = byteswritten;
1284 *buflen = bytesneeded;
1286 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1287 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1290 if (rval == NDIS_STATUS_INVALID_OID)
1293 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1294 rval == NDIS_STATUS_NOT_ACCEPTED)
1297 if (rval != NDIS_STATUS_SUCCESS)
1304 NdisAddDevice(driver_object *drv, device_object *pdo)
1307 ndis_miniport_block *block;
1308 struct ndis_softc *sc;
1312 sc = device_get_softc(pdo->do_devext);
1314 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
1315 error = bus_setup_intr(sc->ndis_dev, sc->ndis_irq,
1317 ntoskrnl_intr, sc, &sc->ndis_intrhand, NULL);
1319 return (NDIS_STATUS_FAILURE);
1322 status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1323 FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
1325 if (status != STATUS_SUCCESS)
1328 block = fdo->do_devext;
1330 block->nmb_filterdbs.nf_ethdb = block;
1331 block->nmb_deviceobj = fdo;
1332 block->nmb_physdeviceobj = pdo;
1333 block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
1334 KeInitializeSpinLock(&block->nmb_lock);
1335 KeInitializeSpinLock(&block->nmb_returnlock);
1336 KeInitializeEvent(&block->nmb_getevent, EVENT_TYPE_NOTIFY, TRUE);
1337 KeInitializeEvent(&block->nmb_setevent, EVENT_TYPE_NOTIFY, TRUE);
1338 KeInitializeEvent(&block->nmb_resetevent, EVENT_TYPE_NOTIFY, TRUE);
1339 InitializeListHead(&block->nmb_parmlist);
1340 InitializeListHead(&block->nmb_returnlist);
1341 block->nmb_returnitem = IoAllocateWorkItem(fdo);
1344 * Stash pointers to the miniport block and miniport
1345 * characteristics info in the if_ndis softc so the
1346 * UNIX wrapper driver can get to them later.
1348 sc->ndis_block = block;
1349 sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1);
1352 * If the driver has a MiniportTransferData() function,
1353 * we should allocate a private RX packet pool.
1356 if (sc->ndis_chars->nmc_transferdata_func != NULL) {
1357 NdisAllocatePacketPool(&status, &block->nmb_rxpool,
1358 32, PROTOCOL_RESERVED_SIZE_IN_PACKET);
1359 if (status != NDIS_STATUS_SUCCESS) {
1360 IoDetachDevice(block->nmb_nextdeviceobj);
1361 IoDeleteDevice(fdo);
1364 InitializeListHead((&block->nmb_packetlist));
1367 /* Give interrupt handling priority over timers. */
1368 IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1369 KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
1371 /* Finish up BSD-specific setup. */
1373 block->nmb_signature = (void *)0xcafebabe;
1374 block->nmb_status_func = kernndis_functbl[0].ipt_wrap;
1375 block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap;
1376 block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap;
1377 block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap;
1378 block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap;
1379 block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap;
1380 block->nmb_pendingreq = NULL;
1382 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
1384 return (STATUS_SUCCESS);
1388 ndis_unload_driver(void *arg)
1390 struct ndis_softc *sc;
1395 if (sc->ndis_intrhand)
1396 bus_teardown_intr(sc->ndis_dev,
1397 sc->ndis_irq, sc->ndis_intrhand);
1399 if (sc->ndis_block->nmb_rlist != NULL)
1400 kfree(sc->ndis_block->nmb_rlist, M_DEVBUF);
1402 ndis_flush_sysctls(sc);
1404 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1406 if (sc->ndis_chars->nmc_transferdata_func != NULL)
1407 NdisFreePacketPool(sc->ndis_block->nmb_rxpool);
1408 fdo = sc->ndis_block->nmb_deviceobj;
1409 IoFreeWorkItem(sc->ndis_block->nmb_returnitem);
1410 IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj);
1411 IoDeleteDevice(fdo);