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.57 2004/07/11 00:19:30 wpaul Exp $
33 * $DragonFly: src/sys/emulation/ndis/kern_ndis.c,v 1.14 2006/12/20 18:14:41 dillon Exp $
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>
50 #include <sys/kernel.h>
51 #include <sys/module.h>
52 #include <sys/kthread.h>
56 #include <sys/mplock2.h>
59 #include <net/if_arp.h>
60 #include <net/ethernet.h>
61 #include <net/if_dl.h>
62 #include <net/if_media.h>
64 #include <netproto/802_11/ieee80211_var.h>
65 #include <netproto/802_11/ieee80211_ioctl.h>
69 #include "resource_var.h"
70 #include "ntoskrnl_var.h"
74 #include <dev/netif/ndis/if_ndisvar.h>
76 #define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
78 __stdcall static void ndis_status_func(ndis_handle, ndis_status,
80 __stdcall static void ndis_statusdone_func(ndis_handle);
81 __stdcall static void ndis_setdone_func(ndis_handle, ndis_status);
82 __stdcall static void ndis_getdone_func(ndis_handle, ndis_status);
83 __stdcall static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
84 __stdcall static void ndis_sendrsrcavail_func(ndis_handle);
86 struct nd_head ndis_devhead;
89 void (*nr_func)(void *);
92 STAILQ_ENTRY(ndis_req) link;
96 struct ndisqhead *np_q;
101 static void ndis_return(void *);
102 static int ndis_create_kthreads(void);
103 static void ndis_destroy_kthreads(void);
104 static void ndis_stop_thread(int);
105 static int ndis_enlarge_thrqueue(int);
106 static int ndis_shrink_thrqueue(int);
107 static void ndis_runq(void *);
109 static MALLOC_DEFINE(M_NDIS_PACKET, "ndis_packet", "ndis packet slosh");
110 static MALLOC_DEFINE(M_NDIS_BUFFER, "ndis_buffer", "ndis buffer slosh");
111 struct lwkt_token ndis_thr_token;
112 static STAILQ_HEAD(ndisqhead, ndis_req) ndis_ttodo;
113 struct ndisqhead ndis_itodo;
114 struct ndisqhead ndis_free;
115 static int ndis_jobs = 32;
117 static struct ndisproc ndis_tproc;
118 static struct ndisproc ndis_iproc;
121 * This allows us to export our symbols to other modules.
122 * Note that we call ourselves 'ndisapi' to avoid a namespace
123 * collision with if_ndis.ko, which internally calls itself
127 ndis_modevent(module_t mod, int cmd, void *arg)
133 /* Initialize subsystems */
137 /* Initialize TX buffer UMA zone. */
138 ndis_create_kthreads();
140 TAILQ_INIT(&ndis_devhead);
145 ndis_destroy_kthreads();
146 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
147 /* Shut down subsystems */
153 malloc_uninit(M_NDIS_PACKET);
154 malloc_uninit(M_NDIS_BUFFER);
160 ndis_destroy_kthreads();
162 /* Shut down subsystems */
168 malloc_uninit(M_NDIS_PACKET);
169 malloc_uninit(M_NDIS_BUFFER);
179 DEV_MODULE(ndisapi, ndis_modevent, NULL);
180 MODULE_VERSION(ndisapi, 1);
183 * We create two kthreads for the NDIS subsystem. One of them is a task
184 * queue for performing various odd jobs. The other is an swi thread
185 * reserved exclusively for running interrupt handlers. The reason we
186 * have our own task queue is that there are some cases where we may
187 * need to sleep for a significant amount of time, and if we were to
188 * use one of the taskqueue threads, we might delay the processing
189 * of other pending tasks which might need to run right away. We have
190 * a separate swi thread because we don't want our interrupt handling
191 * to be delayed either.
193 * By default there are 32 jobs available to start, and another 8
194 * are added to the free list each time a new device is created.
200 struct ndis_req *r = NULL, *die = NULL;
208 /* Sleep, but preserve our original priority. */
209 ndis_thsuspend(p->np_td, 0);
211 /* Look for any jobs on the work queue. */
213 lwkt_gettoken(&ndis_thr_token);
214 p->np_state = NDIS_PSTATE_RUNNING;
215 while(STAILQ_FIRST(p->np_q) != NULL) {
216 r = STAILQ_FIRST(p->np_q);
217 STAILQ_REMOVE_HEAD(p->np_q, link);
218 lwkt_reltoken(&ndis_thr_token);
222 if (r->nr_func != NULL)
223 (*r->nr_func)(r->nr_arg);
225 lwkt_gettoken(&ndis_thr_token);
226 STAILQ_INSERT_HEAD(&ndis_free, r, link);
228 /* Check for a shutdown request */
230 if (r->nr_exit == TRUE)
233 p->np_state = NDIS_PSTATE_SLEEPING;
234 lwkt_reltoken(&ndis_thr_token);
236 /* Bail if we were told to shut down. */
247 ndis_create_kthreads(void)
252 lwkt_token_init(&ndis_thr_token, "ndis");
254 STAILQ_INIT(&ndis_ttodo);
255 STAILQ_INIT(&ndis_itodo);
256 STAILQ_INIT(&ndis_free);
258 for (i = 0; i < ndis_jobs; i++) {
259 r = kmalloc(sizeof(struct ndis_req), M_DEVBUF, M_WAITOK);
260 STAILQ_INSERT_HEAD(&ndis_free, r, link);
264 ndis_tproc.np_q = &ndis_ttodo;
265 ndis_tproc.np_state = NDIS_PSTATE_SLEEPING;
266 error = kthread_create_stk(ndis_runq, &ndis_tproc,
267 &ndis_tproc.np_td, NDIS_KSTACK_PAGES * PAGE_SIZE,
272 ndis_iproc.np_q = &ndis_itodo;
273 ndis_iproc.np_state = NDIS_PSTATE_SLEEPING;
274 error = kthread_create_stk(ndis_runq, &ndis_iproc,
275 &ndis_iproc.np_td, NDIS_KSTACK_PAGES * PAGE_SIZE,
280 while ((r = STAILQ_FIRST(&ndis_free)) != NULL) {
281 STAILQ_REMOVE_HEAD(&ndis_free, link);
291 ndis_destroy_kthreads(void)
295 /* Stop the threads. */
297 ndis_stop_thread(NDIS_TASKQUEUE);
298 ndis_stop_thread(NDIS_SWI);
300 /* Destroy request structures. */
302 while ((r = STAILQ_FIRST(&ndis_free)) != NULL) {
303 STAILQ_REMOVE_HEAD(&ndis_free, link);
307 lwkt_token_uninit(&ndis_thr_token);
313 ndis_stop_thread(int t)
319 if (t == NDIS_TASKQUEUE) {
321 td = ndis_tproc.np_td;
324 td = ndis_iproc.np_td;
327 /* Create and post a special 'exit' job. */
329 lwkt_gettoken(&ndis_thr_token);
330 r = STAILQ_FIRST(&ndis_free);
331 STAILQ_REMOVE_HEAD(&ndis_free, link);
335 STAILQ_INSERT_TAIL(q, r, link);
336 lwkt_reltoken(&ndis_thr_token);
340 /* wait for thread exit */
342 tsleep(r, PCATCH, "ndisthexit", hz * 60);
344 /* Now empty the job list. */
346 lwkt_gettoken(&ndis_thr_token);
347 while ((r = STAILQ_FIRST(q)) != NULL) {
348 STAILQ_REMOVE_HEAD(q, link);
349 STAILQ_INSERT_HEAD(&ndis_free, r, link);
351 lwkt_reltoken(&ndis_thr_token);
355 ndis_enlarge_thrqueue(int cnt)
360 for (i = 0; i < cnt; i++) {
361 r = kmalloc(sizeof(struct ndis_req), M_DEVBUF, M_WAITOK);
362 lwkt_gettoken(&ndis_thr_token);
363 STAILQ_INSERT_HEAD(&ndis_free, r, link);
365 lwkt_reltoken(&ndis_thr_token);
372 ndis_shrink_thrqueue(int cnt)
377 for (i = 0; i < cnt; i++) {
378 lwkt_gettoken(&ndis_thr_token);
379 r = STAILQ_FIRST(&ndis_free);
381 lwkt_reltoken(&ndis_thr_token);
384 STAILQ_REMOVE_HEAD(&ndis_free, link);
386 lwkt_reltoken(&ndis_thr_token);
394 ndis_unsched(void (*func)(void *), void *arg, int t)
400 if (t == NDIS_TASKQUEUE) {
402 td = ndis_tproc.np_td;
405 td = ndis_iproc.np_td;
408 lwkt_gettoken(&ndis_thr_token);
409 STAILQ_FOREACH(r, q, link) {
410 if (r->nr_func == func && r->nr_arg == arg) {
411 STAILQ_REMOVE(q, r, ndis_req, link);
412 STAILQ_INSERT_HEAD(&ndis_free, r, link);
413 lwkt_reltoken(&ndis_thr_token);
418 lwkt_reltoken(&ndis_thr_token);
424 ndis_sched(void (*func)(void *), void *arg, int t)
431 if (t == NDIS_TASKQUEUE) {
433 td = ndis_tproc.np_td;
436 td = ndis_iproc.np_td;
439 lwkt_gettoken(&ndis_thr_token);
441 * Check to see if an instance of this job is already
442 * pending. If so, don't bother queuing it again.
444 STAILQ_FOREACH(r, q, link) {
445 if (r->nr_func == func && r->nr_arg == arg) {
446 lwkt_reltoken(&ndis_thr_token);
450 r = STAILQ_FIRST(&ndis_free);
452 lwkt_reltoken(&ndis_thr_token);
455 STAILQ_REMOVE_HEAD(&ndis_free, link);
459 STAILQ_INSERT_TAIL(q, r, link);
460 if (t == NDIS_TASKQUEUE)
461 s = ndis_tproc.np_state;
463 s = ndis_iproc.np_state;
464 lwkt_reltoken(&ndis_thr_token);
467 * Post the job, but only if the thread is actually blocked
468 * on its own suspend call. If a driver queues up a job with
469 * NdisScheduleWorkItem() which happens to do a KeWaitForObject(),
470 * it may suspend there, and in that case we don't want to wake
471 * it up until KeWaitForObject() gets woken up on its own.
473 if (s == NDIS_PSTATE_SLEEPING)
480 ndis_thsuspend(thread_t td, int timo)
484 error = tsleep(td, 0, "ndissp", timo);
489 ndis_thresume(struct thread *td)
494 __stdcall static void
495 ndis_sendrsrcavail_func(ndis_handle adapter)
500 __stdcall static void
501 ndis_status_func(ndis_handle adapter, ndis_status status, void *sbuf,
504 ndis_miniport_block *block;
507 if (block->nmb_ifp->if_flags & IFF_DEBUG)
508 device_printf (block->nmb_dev, "status: %x\n", status);
512 __stdcall static void
513 ndis_statusdone_func(ndis_handle adapter)
515 ndis_miniport_block *block;
518 if (block->nmb_ifp->if_flags & IFF_DEBUG)
519 device_printf (block->nmb_dev, "status complete\n");
523 __stdcall static void
524 ndis_setdone_func(ndis_handle adapter, ndis_status status)
526 ndis_miniport_block *block;
529 block->nmb_setstat = status;
530 wakeup(&block->nmb_wkupdpctimer);
534 __stdcall static void
535 ndis_getdone_func(ndis_handle adapter, ndis_status status)
537 ndis_miniport_block *block;
540 block->nmb_getstat = status;
541 wakeup(&block->nmb_wkupdpctimer);
545 __stdcall static void
546 ndis_resetdone_func(ndis_handle adapter, ndis_status status,
547 uint8_t addressingreset)
549 ndis_miniport_block *block;
552 if (block->nmb_ifp->if_flags & IFF_DEBUG)
553 device_printf (block->nmb_dev, "reset done...\n");
554 wakeup(block->nmb_ifp);
559 ndis_create_sysctls(void *arg)
561 struct ndis_softc *sc;
569 vals = sc->ndis_regvals;
571 TAILQ_INIT(&sc->ndis_cfglist_head);
573 #if __FreeBSD_version < 502113
574 /* Create the sysctl tree. */
576 sc->ndis_tree = SYSCTL_ADD_NODE(&sc->ndis_ctx,
577 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
578 device_get_nameunit(sc->ndis_dev), CTLFLAG_RD, 0,
579 device_get_desc(sc->ndis_dev));
582 /* Add the driver-specific registry keys. */
584 vals = sc->ndis_regvals;
586 if (vals->nc_cfgkey == NULL)
588 if (vals->nc_idx != sc->ndis_devidx) {
593 SYSCTL_ADD_STRING(&sc->ndis_ctx,
594 SYSCTL_CHILDREN(sc->ndis_tree),
595 OID_AUTO, vals->nc_cfgkey,
596 CTLFLAG_RW, vals->nc_val,
597 sizeof(vals->nc_val),
600 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
601 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
602 OID_AUTO, vals->nc_cfgkey,
603 CTLFLAG_RW, vals->nc_val,
604 sizeof(vals->nc_val),
610 /* Now add a couple of builtin keys. */
613 * Environment can be either Windows (0) or WindowsNT (1).
614 * We qualify as the latter.
616 ndis_add_sysctl(sc, "Environment",
617 "Windows environment", "1", CTLFLAG_RD);
619 /* NDIS version should be 5.1. */
620 ndis_add_sysctl(sc, "NdisVersion",
621 "NDIS API Version", "0x00050001", CTLFLAG_RD);
623 /* Bus type (PCI, PCMCIA, etc...) */
624 ksprintf(buf, "%d", (int)sc->ndis_iftype);
625 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
627 if (sc->ndis_res_io != NULL) {
628 ksprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
629 ndis_add_sysctl(sc, "IOBaseAddress",
630 "Base I/O Address", buf, CTLFLAG_RD);
633 if (sc->ndis_irq != NULL) {
634 ksprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
635 ndis_add_sysctl(sc, "InterruptNumber",
636 "Interrupt Number", buf, CTLFLAG_RD);
643 ndis_add_sysctl(void *arg, char *key, char *desc, char *val, int flag)
645 struct ndis_softc *sc;
646 struct ndis_cfglist *cfg;
651 cfg = kmalloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_WAITOK|M_ZERO);
652 cfg->ndis_cfg.nc_cfgkey = kstrdup(key, M_DEVBUF);
654 ksnprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
655 cfg->ndis_cfg.nc_cfgdesc = kstrdup(descstr, M_DEVBUF);
657 cfg->ndis_cfg.nc_cfgdesc = kstrdup(desc, M_DEVBUF);
658 strcpy(cfg->ndis_cfg.nc_val, val);
660 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
663 SYSCTL_ADD_STRING(&sc->ndis_ctx, SYSCTL_CHILDREN(sc->ndis_tree),
664 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
665 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
666 cfg->ndis_cfg.nc_cfgdesc);
668 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
669 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
670 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
671 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
672 cfg->ndis_cfg.nc_cfgdesc);
679 ndis_flush_sysctls(void *arg)
681 struct ndis_softc *sc;
682 struct ndis_cfglist *cfg;
686 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
687 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
688 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
689 kfree(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
690 kfree(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
691 kfree(cfg, M_DEVBUF);
698 ndis_return(void *arg)
700 struct ndis_softc *sc;
701 ndis_return_handler returnfunc;
708 adapter = sc->ndis_block.nmb_miniportadapterctx;
713 returnfunc = sc->ndis_chars.nmc_return_packet_func;
714 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
715 returnfunc(adapter, p);
716 FASTCALL1(hal_lower_irql, irql);
722 ndis_extref_packet(void *arg)
724 ndis_packet *p = arg;
730 ndis_extfree_packet(void *arg)
732 ndis_packet *p = arg;
737 /* Decrement refcount. */
740 /* Release packet when refcount hits zero, otherwise return. */
744 ndis_sched(ndis_return, p, NDIS_SWI);
750 ndis_return_packet(struct ndis_softc *sc, ndis_packet *p)
752 ndis_extfree_packet(p);
756 ndis_free_bufs(ndis_buffer *b0)
765 kfree(b0, M_NDIS_BUFFER);
773 ndis_free_packet(ndis_packet *p)
778 ndis_free_bufs(p->np_private.npp_head);
779 kfree(p, M_NDIS_PACKET);
785 ndis_convert_res(void *arg)
787 struct ndis_softc *sc;
788 ndis_resource_list *rl = NULL;
789 cm_partial_resource_desc *prd = NULL;
790 ndis_miniport_block *block;
792 struct resource_list *brl;
793 struct resource_list brl_rev;
794 struct resource_list_entry *brle, *n;
798 block = &sc->ndis_block;
801 SLIST_INIT(&brl_rev);
803 rl = kmalloc(sizeof(ndis_resource_list) +
804 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
805 M_DEVBUF, M_WAITOK|M_NULLOK|M_ZERO);
810 rl->cprl_version = 5;
811 rl->cprl_version = 1;
812 rl->cprl_count = sc->ndis_rescnt;
813 prd = rl->cprl_partial_descs;
815 brl = BUS_GET_RESOURCE_LIST(dev, dev);
820 * We have a small problem. Some PCI devices have
821 * multiple I/O ranges. Windows orders them starting
822 * from lowest numbered BAR to highest. We discover
823 * them in that order too, but insert them into a singly
824 * linked list head first, which means when time comes
825 * to traverse the list, we enumerate them in reverse
826 * order. This screws up some drivers which expect the
827 * BARs to be in ascending order so that they can choose
828 * the "first" one as their register space. Unfortunately,
829 * in order to fix this, we have to create our own
830 * temporary list with the entries in reverse order.
832 SLIST_FOREACH(brle, brl, link) {
833 n = kmalloc(sizeof(struct resource_list_entry),
834 M_TEMP, M_WAITOK|M_NULLOK);
839 bcopy((char *)brle, (char *)n,
840 sizeof(struct resource_list_entry));
841 SLIST_INSERT_HEAD(&brl_rev, n, link);
844 SLIST_FOREACH(brle, &brl_rev, link) {
845 switch (brle->type) {
847 prd->cprd_type = CmResourceTypePort;
848 prd->cprd_flags = CM_RESOURCE_PORT_IO;
849 prd->cprd_sharedisp =
850 CmResourceShareDeviceExclusive;
851 prd->u.cprd_port.cprd_start.np_quad =
853 prd->u.cprd_port.cprd_len = brle->count;
856 prd->cprd_type = CmResourceTypeMemory;
858 CM_RESOURCE_MEMORY_READ_WRITE;
859 prd->cprd_sharedisp =
860 CmResourceShareDeviceExclusive;
861 prd->u.cprd_port.cprd_start.np_quad =
863 prd->u.cprd_port.cprd_len = brle->count;
866 prd->cprd_type = CmResourceTypeInterrupt;
868 prd->cprd_sharedisp =
869 CmResourceShareDeviceExclusive;
870 prd->u.cprd_intr.cprd_level = brle->start;
871 prd->u.cprd_intr.cprd_vector = brle->start;
872 prd->u.cprd_intr.cprd_affinity = 0;
881 block->nmb_rlist = rl;
885 while (!SLIST_EMPTY(&brl_rev)) {
886 n = SLIST_FIRST(&brl_rev);
887 SLIST_REMOVE_HEAD(&brl_rev, link);
895 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
896 * packet, it will hand it to us in the form of an ndis_packet,
897 * which we need to convert to an mbuf that is then handed off
898 * to the stack. Note: we configure the mbuf list so that it uses
899 * the memory regions specified by the ndis_buffer structures in
900 * the ndis_packet as external storage. In most cases, this will
901 * point to a memory region allocated by the driver (either by
902 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
903 * the driver to handle free()ing this region for is, so we set up
904 * a dummy no-op free handler for it.
908 ndis_ptom(struct mbuf **m0, ndis_packet *p)
910 struct mbuf *m, *prev = NULL;
912 ndis_packet_private *priv;
915 if (p == NULL || m0 == NULL)
918 priv = &p->np_private;
919 buf = priv->npp_head;
922 for (buf = priv->npp_head; buf != NULL; buf = buf->nb_next) {
923 if (buf == priv->npp_head)
924 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
926 MGET(m, MB_DONTWAIT, MT_DATA);
932 m->m_len = buf->nb_bytecount;
933 m->m_data = MDL_VA(buf);
934 m->m_ext.ext_free = ndis_extfree_packet;
935 m->m_ext.ext_ref = ndis_extref_packet;
936 m->m_ext.ext_arg = p;
937 m->m_ext.ext_buf = m->m_data;
938 m->m_ext.ext_size = m->m_len;
941 MEXTADD(m, m->m_data, m->m_len, ndis_free_packet,
946 if (m->m_flags & M_PKTHDR)
953 (*m0)->m_pkthdr.len = totlen;
959 * Create an mbuf chain from an NDIS packet chain.
960 * This is used mainly when transmitting packets, where we need
961 * to turn an mbuf off an interface's send queue and transform it
962 * into an NDIS packet which will be fed into the NDIS driver's
965 * NDIS packets consist of two parts: an ndis_packet structure,
966 * which is vaguely analagous to the pkthdr portion of an mbuf,
967 * and one or more ndis_buffer structures, which define the
968 * actual memory segments in which the packet data resides.
969 * We need to allocate one ndis_buffer for each mbuf in a chain,
970 * plus one ndis_packet as the header.
974 ndis_mtop(struct mbuf *m0, ndis_packet **p)
977 ndis_buffer *buf = NULL, *prev = NULL;
978 ndis_packet_private *priv;
980 if (p == NULL || m0 == NULL)
983 /* If caller didn't supply a packet, make one. */
985 *p = kmalloc(sizeof(ndis_packet), M_NDIS_PACKET, M_NOWAIT|M_ZERO);
990 priv = &(*p)->np_private;
991 priv->npp_totlen = m0->m_pkthdr.len;
992 priv->npp_packetooboffset = offsetof(ndis_packet, np_oob);
993 priv->npp_ndispktflags = NDIS_PACKET_ALLOCATED_BY_NDIS;
995 for (m = m0; m != NULL; m = m->m_next) {
998 buf = kmalloc(sizeof(ndis_buffer), M_NDIS_BUFFER, M_NOWAIT|M_ZERO);
1000 ndis_free_packet(*p);
1005 MDL_INIT(buf, m->m_data, m->m_len);
1006 if (priv->npp_head == NULL)
1007 priv->npp_head = buf;
1009 prev->nb_next = buf;
1013 priv->npp_tail = buf;
1014 priv->npp_totlen = m0->m_pkthdr.len;
1020 ndis_get_supported_oids(void *arg, ndis_oid **oids, int *oidcnt)
1025 if (arg == NULL || oids == NULL || oidcnt == NULL)
1028 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
1030 o = kmalloc(len, M_DEVBUF, M_WAITOK);
1032 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
1046 ndis_set_info(void *arg, ndis_oid oid, void *buf, int *buflen)
1048 struct ndis_softc *sc;
1050 ndis_handle adapter;
1051 ndis_setinfo_handler setfunc;
1052 uint32_t byteswritten = 0, bytesneeded = 0;
1057 setfunc = sc->ndis_chars.nmc_setinfo_func;
1058 adapter = sc->ndis_block.nmb_miniportadapterctx;
1060 if (adapter == NULL || setfunc == NULL)
1063 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1064 rval = setfunc(adapter, oid, buf, *buflen,
1065 &byteswritten, &bytesneeded);
1066 FASTCALL1(hal_lower_irql, irql);
1068 if (rval == NDIS_STATUS_PENDING) {
1069 error = tsleep(&sc->ndis_block.nmb_wkupdpctimer,
1070 0, "ndisset", 5 * hz);
1071 rval = sc->ndis_block.nmb_setstat;
1075 *buflen = byteswritten;
1077 *buflen = bytesneeded;
1079 if (rval == NDIS_STATUS_INVALID_LENGTH)
1082 if (rval == NDIS_STATUS_INVALID_OID)
1085 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1086 rval == NDIS_STATUS_NOT_ACCEPTED)
1089 if (rval != NDIS_STATUS_SUCCESS)
1095 typedef __stdcall void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
1098 ndis_send_packets(void *arg, ndis_packet **packets, int cnt)
1100 struct ndis_softc *sc;
1101 ndis_handle adapter;
1102 ndis_sendmulti_handler sendfunc;
1103 ndis_senddone_func senddonefunc;
1109 adapter = sc->ndis_block.nmb_miniportadapterctx;
1110 if (adapter == NULL)
1112 sendfunc = sc->ndis_chars.nmc_sendmulti_func;
1113 senddonefunc = sc->ndis_block.nmb_senddone_func;
1114 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1115 sendfunc(adapter, packets, cnt);
1116 FASTCALL1(hal_lower_irql, irql);
1118 for (i = 0; i < cnt; i++) {
1121 * Either the driver already handed the packet to
1122 * ndis_txeof() due to a failure, or it wants to keep
1123 * it and release it asynchronously later. Skip to the
1126 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
1128 senddonefunc(&sc->ndis_block, p, p->np_oob.npo_status);
1135 ndis_send_packet(void *arg, ndis_packet *packet)
1137 struct ndis_softc *sc;
1138 ndis_handle adapter;
1140 ndis_sendsingle_handler sendfunc;
1141 ndis_senddone_func senddonefunc;
1145 adapter = sc->ndis_block.nmb_miniportadapterctx;
1146 if (adapter == NULL)
1148 sendfunc = sc->ndis_chars.nmc_sendsingle_func;
1149 senddonefunc = sc->ndis_block.nmb_senddone_func;
1151 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1152 status = sendfunc(adapter, packet, packet->np_private.npp_flags);
1153 FASTCALL1(hal_lower_irql, irql);
1155 if (status == NDIS_STATUS_PENDING)
1158 senddonefunc(&sc->ndis_block, packet, status);
1164 ndis_init_dma(void *arg)
1166 struct ndis_softc *sc;
1171 sc->ndis_tmaps = kmalloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
1172 M_DEVBUF, M_WAITOK|M_ZERO);
1174 for (i = 0; i < sc->ndis_maxpkts; i++) {
1175 error = bus_dmamap_create(sc->ndis_ttag, 0,
1176 &sc->ndis_tmaps[i]);
1178 kfree(sc->ndis_tmaps, M_DEVBUF);
1187 ndis_destroy_dma(void *arg)
1189 struct ndis_softc *sc;
1191 ndis_packet *p = NULL;
1196 for (i = 0; i < sc->ndis_maxpkts; i++) {
1197 if (sc->ndis_txarray[i] != NULL) {
1198 p = sc->ndis_txarray[i];
1199 m = (struct mbuf *)p->np_rsvd[1];
1202 ndis_free_packet(sc->ndis_txarray[i]);
1204 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
1207 kfree(sc->ndis_tmaps, M_DEVBUF);
1208 bus_dma_tag_destroy(sc->ndis_ttag);
1214 ndis_reset_nic(void *arg)
1216 struct ndis_softc *sc;
1217 ndis_handle adapter;
1218 ndis_reset_handler resetfunc;
1219 uint8_t addressing_reset;
1224 adapter = sc->ndis_block.nmb_miniportadapterctx;
1225 resetfunc = sc->ndis_chars.nmc_reset_func;
1226 if (adapter == NULL || resetfunc == NULL)
1229 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1230 rval = resetfunc(&addressing_reset, adapter);
1231 FASTCALL1(hal_lower_irql, irql);
1233 if (rval == NDIS_STATUS_PENDING) {
1234 tsleep(sc, 0, "ndisrst", 0);
1241 ndis_halt_nic(void *arg)
1243 struct ndis_softc *sc;
1244 ndis_handle adapter;
1245 ndis_halt_handler haltfunc;
1249 adapter = sc->ndis_block.nmb_miniportadapterctx;
1250 if (adapter == NULL) {
1255 * The adapter context is only valid after the init
1256 * handler has been called, and is invalid once the
1257 * halt handler has been called.
1260 haltfunc = sc->ndis_chars.nmc_halt_func;
1264 sc->ndis_block.nmb_miniportadapterctx = NULL;
1270 ndis_shutdown_nic(void *arg)
1272 struct ndis_softc *sc;
1273 ndis_handle adapter;
1274 ndis_shutdown_handler shutdownfunc;
1277 adapter = sc->ndis_block.nmb_miniportadapterctx;
1278 shutdownfunc = sc->ndis_chars.nmc_shutdown_handler;
1279 if (adapter == NULL || shutdownfunc == NULL)
1282 if (sc->ndis_chars.nmc_rsvd0 == NULL)
1283 shutdownfunc(adapter);
1285 shutdownfunc(sc->ndis_chars.nmc_rsvd0);
1287 ndis_shrink_thrqueue(8);
1288 TAILQ_REMOVE(&ndis_devhead, &sc->ndis_block, link);
1294 ndis_init_nic(void *arg)
1296 struct ndis_softc *sc;
1297 ndis_miniport_block *block;
1298 ndis_init_handler initfunc;
1299 ndis_status status, openstatus = 0;
1300 ndis_medium mediumarray[NdisMediumMax];
1301 uint32_t chosenmedium, i;
1307 block = &sc->ndis_block;
1308 initfunc = sc->ndis_chars.nmc_init_func;
1310 TAILQ_INIT(&block->nmb_timerlist);
1312 for (i = 0; i < NdisMediumMax; i++)
1315 status = initfunc(&openstatus, &chosenmedium,
1316 mediumarray, NdisMediumMax, block, block);
1319 * If the init fails, blow away the other exported routines
1320 * we obtained from the driver so we can't call them later.
1321 * If the init failed, none of these will work.
1323 if (status != NDIS_STATUS_SUCCESS) {
1324 sc->ndis_block.nmb_miniportadapterctx = NULL;
1332 ndis_enable_intr(void *arg)
1334 struct ndis_softc *sc;
1335 ndis_handle adapter;
1336 ndis_enable_interrupts_handler intrenbfunc;
1339 adapter = sc->ndis_block.nmb_miniportadapterctx;
1340 intrenbfunc = sc->ndis_chars.nmc_enable_interrupts_func;
1341 if (adapter == NULL || intrenbfunc == NULL)
1343 intrenbfunc(adapter);
1349 ndis_disable_intr(void *arg)
1351 struct ndis_softc *sc;
1352 ndis_handle adapter;
1353 ndis_disable_interrupts_handler intrdisfunc;
1356 adapter = sc->ndis_block.nmb_miniportadapterctx;
1357 intrdisfunc = sc->ndis_chars.nmc_disable_interrupts_func;
1358 if (adapter == NULL || intrdisfunc == NULL)
1360 intrdisfunc(adapter);
1366 ndis_isr(void *arg, int *ourintr, int *callhandler)
1368 struct ndis_softc *sc;
1369 ndis_handle adapter;
1370 ndis_isr_handler isrfunc;
1371 uint8_t accepted, queue;
1373 if (arg == NULL || ourintr == NULL || callhandler == NULL)
1377 adapter = sc->ndis_block.nmb_miniportadapterctx;
1378 isrfunc = sc->ndis_chars.nmc_isr_func;
1379 if (adapter == NULL || isrfunc == NULL)
1382 isrfunc(&accepted, &queue, adapter);
1383 *ourintr = accepted;
1384 *callhandler = queue;
1390 ndis_intrhand(void *arg)
1392 struct ndis_softc *sc;
1393 ndis_handle adapter;
1394 ndis_interrupt_handler intrfunc;
1400 adapter = sc->ndis_block.nmb_miniportadapterctx;
1401 intrfunc = sc->ndis_chars.nmc_interrupt_func;
1402 if (adapter == NULL || intrfunc == NULL)
1411 ndis_get_info(void *arg, ndis_oid oid, void *buf, int *buflen)
1413 struct ndis_softc *sc;
1415 ndis_handle adapter;
1416 ndis_queryinfo_handler queryfunc;
1417 uint32_t byteswritten = 0, bytesneeded = 0;
1422 queryfunc = sc->ndis_chars.nmc_queryinfo_func;
1423 adapter = sc->ndis_block.nmb_miniportadapterctx;
1425 if (adapter == NULL || queryfunc == NULL)
1428 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1429 rval = queryfunc(adapter, oid, buf, *buflen,
1430 &byteswritten, &bytesneeded);
1431 FASTCALL1(hal_lower_irql, irql);
1433 /* Wait for requests that block. */
1435 if (rval == NDIS_STATUS_PENDING) {
1436 error = tsleep(&sc->ndis_block.nmb_wkupdpctimer,
1437 0, "ndisget", 5 * hz);
1438 rval = sc->ndis_block.nmb_getstat;
1442 *buflen = byteswritten;
1444 *buflen = bytesneeded;
1446 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1447 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1450 if (rval == NDIS_STATUS_INVALID_OID)
1453 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1454 rval == NDIS_STATUS_NOT_ACCEPTED)
1457 if (rval != NDIS_STATUS_SUCCESS)
1464 ndis_unload_driver(void *arg)
1466 struct ndis_softc *sc;
1470 kfree(sc->ndis_block.nmb_rlist, M_DEVBUF);
1472 ndis_flush_sysctls(sc);
1474 ndis_shrink_thrqueue(8);
1475 TAILQ_REMOVE(&ndis_devhead, &sc->ndis_block, link);
1480 #define NDIS_LOADED htonl(0x42534F44)
1483 ndis_load_driver(vm_offset_t img, void *arg)
1486 image_optional_header opt_hdr;
1487 image_import_descriptor imp_desc;
1488 ndis_unicode_string dummystr;
1489 ndis_miniport_block *block;
1493 struct ndis_softc *sc;
1498 * Only perform the relocation/linking phase once
1499 * since the binary image may be shared among multiple
1503 ptr = (uint32_t *)(img + 8);
1504 if (*ptr != NDIS_LOADED) {
1505 /* Perform text relocation */
1506 if (pe_relocate(img))
1509 /* Dynamically link the NDIS.SYS routines -- required. */
1510 if (pe_patch_imports(img, "NDIS", ndis_functbl))
1513 /* Dynamically link the HAL.dll routines -- also required. */
1514 if (pe_patch_imports(img, "HAL", hal_functbl))
1517 /* Dynamically link ntoskrnl.exe -- optional. */
1518 if (pe_get_import_descriptor(img,
1519 &imp_desc, "ntoskrnl") == 0) {
1520 if (pe_patch_imports(img,
1521 "ntoskrnl", ntoskrnl_functbl))
1527 /* Locate the driver entry point */
1528 pe_get_optional_header(img, &opt_hdr);
1529 entry = (driver_entry)pe_translate_addr(img, opt_hdr.ioh_entryaddr);
1531 dummystr.nus_len = strlen(NDIS_DUMMY_PATH) * 2;
1532 dummystr.nus_maxlen = strlen(NDIS_DUMMY_PATH) * 2;
1533 dummystr.nus_buf = NULL;
1534 ndis_ascii_to_unicode(NDIS_DUMMY_PATH, &dummystr.nus_buf);
1537 * Now that we have the miniport driver characteristics,
1538 * create an NDIS block and call the init handler.
1539 * This will cause the driver to try to probe for
1543 block = &sc->ndis_block;
1545 ptr = (uint32_t *)block;
1546 for (idx = 0; idx < sizeof(ndis_miniport_block) / 4; idx++) {
1547 *ptr = idx | 0xdead0000;
1551 block->nmb_signature = (void *)0xcafebabe;
1552 block->nmb_setdone_func = ndis_setdone_func;
1553 block->nmb_querydone_func = ndis_getdone_func;
1554 block->nmb_status_func = ndis_status_func;
1555 block->nmb_statusdone_func = ndis_statusdone_func;
1556 block->nmb_resetdone_func = ndis_resetdone_func;
1557 block->nmb_sendrsrc_func = ndis_sendrsrcavail_func;
1559 block->nmb_ifp = &sc->arpcom.ac_if;
1560 block->nmb_dev = sc->ndis_dev;
1561 block->nmb_img = img;
1562 block->nmb_devobj.do_rsvd = block;
1565 * Now call the DriverEntry() routine. This will cause
1566 * a callout to the NdisInitializeWrapper() and
1567 * NdisMRegisterMiniport() routines.
1569 status = entry(&block->nmb_devobj, &dummystr);
1571 kfree (dummystr.nus_buf, M_DEVBUF);
1573 if (status != NDIS_STATUS_SUCCESS)
1576 ndis_enlarge_thrqueue(8);
1578 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);