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>
57 #include <net/if_arp.h>
58 #include <net/ethernet.h>
59 #include <net/if_dl.h>
60 #include <net/if_media.h>
62 #include <netproto/802_11/ieee80211_var.h>
63 #include <netproto/802_11/ieee80211_ioctl.h>
67 #include "resource_var.h"
68 #include "ntoskrnl_var.h"
72 #include <dev/netif/ndis/if_ndisvar.h>
74 #define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
76 __stdcall static void ndis_status_func(ndis_handle, ndis_status,
78 __stdcall static void ndis_statusdone_func(ndis_handle);
79 __stdcall static void ndis_setdone_func(ndis_handle, ndis_status);
80 __stdcall static void ndis_getdone_func(ndis_handle, ndis_status);
81 __stdcall static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
82 __stdcall static void ndis_sendrsrcavail_func(ndis_handle);
84 struct nd_head ndis_devhead;
87 void (*nr_func)(void *);
90 STAILQ_ENTRY(ndis_req) link;
94 struct ndisqhead *np_q;
99 static void ndis_return(void *);
100 static int ndis_create_kthreads(void);
101 static void ndis_destroy_kthreads(void);
102 static void ndis_stop_thread(int);
103 static int ndis_enlarge_thrqueue(int);
104 static int ndis_shrink_thrqueue(int);
105 static void ndis_runq(void *);
107 static MALLOC_DEFINE(M_NDIS_PACKET, "ndis_packet", "ndis packet slosh");
108 static MALLOC_DEFINE(M_NDIS_BUFFER, "ndis_buffer", "ndis buffer slosh");
109 struct lwkt_token ndis_thr_token;
110 static STAILQ_HEAD(ndisqhead, ndis_req) ndis_ttodo;
111 struct ndisqhead ndis_itodo;
112 struct ndisqhead ndis_free;
113 static int ndis_jobs = 32;
115 static struct ndisproc ndis_tproc;
116 static struct ndisproc ndis_iproc;
119 * This allows us to export our symbols to other modules.
120 * Note that we call ourselves 'ndisapi' to avoid a namespace
121 * collision with if_ndis.ko, which internally calls itself
125 ndis_modevent(module_t mod, int cmd, void *arg)
131 /* Initialize subsystems */
135 /* Initialize TX buffer UMA zone. */
136 ndis_create_kthreads();
138 TAILQ_INIT(&ndis_devhead);
143 ndis_destroy_kthreads();
144 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
145 /* Shut down subsystems */
151 malloc_uninit(M_NDIS_PACKET);
152 malloc_uninit(M_NDIS_BUFFER);
158 ndis_destroy_kthreads();
160 /* Shut down subsystems */
166 malloc_uninit(M_NDIS_PACKET);
167 malloc_uninit(M_NDIS_BUFFER);
177 DEV_MODULE(ndisapi, ndis_modevent, NULL);
178 MODULE_VERSION(ndisapi, 1);
181 * We create two kthreads for the NDIS subsystem. One of them is a task
182 * queue for performing various odd jobs. The other is an swi thread
183 * reserved exclusively for running interrupt handlers. The reason we
184 * have our own task queue is that there are some cases where we may
185 * need to sleep for a significant amount of time, and if we were to
186 * use one of the taskqueue threads, we might delay the processing
187 * of other pending tasks which might need to run right away. We have
188 * a separate swi thread because we don't want our interrupt handling
189 * to be delayed either.
191 * By default there are 32 jobs available to start, and another 8
192 * are added to the free list each time a new device is created.
198 struct ndis_req *r = NULL, *die = NULL;
205 /* Sleep, but preserve our original priority. */
206 ndis_thsuspend(p->np_td, 0);
208 /* Look for any jobs on the work queue. */
210 lwkt_gettoken(&ndis_thr_token);
211 p->np_state = NDIS_PSTATE_RUNNING;
212 while(STAILQ_FIRST(p->np_q) != NULL) {
213 r = STAILQ_FIRST(p->np_q);
214 STAILQ_REMOVE_HEAD(p->np_q, link);
215 lwkt_reltoken(&ndis_thr_token);
219 if (r->nr_func != NULL)
220 (*r->nr_func)(r->nr_arg);
222 lwkt_gettoken(&ndis_thr_token);
223 STAILQ_INSERT_HEAD(&ndis_free, r, link);
225 /* Check for a shutdown request */
227 if (r->nr_exit == TRUE)
230 p->np_state = NDIS_PSTATE_SLEEPING;
231 lwkt_reltoken(&ndis_thr_token);
233 /* Bail if we were told to shut down. */
244 ndis_create_kthreads(void)
249 lwkt_token_init(&ndis_thr_token, 1, "ndis");
251 STAILQ_INIT(&ndis_ttodo);
252 STAILQ_INIT(&ndis_itodo);
253 STAILQ_INIT(&ndis_free);
255 for (i = 0; i < ndis_jobs; i++) {
256 r = kmalloc(sizeof(struct ndis_req), M_DEVBUF, M_WAITOK);
257 STAILQ_INSERT_HEAD(&ndis_free, r, link);
261 ndis_tproc.np_q = &ndis_ttodo;
262 ndis_tproc.np_state = NDIS_PSTATE_SLEEPING;
263 error = kthread_create_stk(ndis_runq, &ndis_tproc,
264 &ndis_tproc.np_td, NDIS_KSTACK_PAGES * PAGE_SIZE,
269 ndis_iproc.np_q = &ndis_itodo;
270 ndis_iproc.np_state = NDIS_PSTATE_SLEEPING;
271 error = kthread_create_stk(ndis_runq, &ndis_iproc,
272 &ndis_iproc.np_td, NDIS_KSTACK_PAGES * PAGE_SIZE,
277 while ((r = STAILQ_FIRST(&ndis_free)) != NULL) {
278 STAILQ_REMOVE_HEAD(&ndis_free, link);
288 ndis_destroy_kthreads(void)
292 /* Stop the threads. */
294 ndis_stop_thread(NDIS_TASKQUEUE);
295 ndis_stop_thread(NDIS_SWI);
297 /* Destroy request structures. */
299 while ((r = STAILQ_FIRST(&ndis_free)) != NULL) {
300 STAILQ_REMOVE_HEAD(&ndis_free, link);
304 lwkt_token_uninit(&ndis_thr_token);
310 ndis_stop_thread(int t)
316 if (t == NDIS_TASKQUEUE) {
318 td = ndis_tproc.np_td;
321 td = ndis_iproc.np_td;
324 /* Create and post a special 'exit' job. */
326 lwkt_gettoken(&ndis_thr_token);
327 r = STAILQ_FIRST(&ndis_free);
328 STAILQ_REMOVE_HEAD(&ndis_free, link);
332 STAILQ_INSERT_TAIL(q, r, link);
333 lwkt_reltoken(&ndis_thr_token);
337 /* wait for thread exit */
339 tsleep(r, PCATCH, "ndisthexit", hz * 60);
341 /* Now empty the job list. */
343 lwkt_gettoken(&ndis_thr_token);
344 while ((r = STAILQ_FIRST(q)) != NULL) {
345 STAILQ_REMOVE_HEAD(q, link);
346 STAILQ_INSERT_HEAD(&ndis_free, r, link);
348 lwkt_reltoken(&ndis_thr_token);
352 ndis_enlarge_thrqueue(int cnt)
357 for (i = 0; i < cnt; i++) {
358 r = kmalloc(sizeof(struct ndis_req), M_DEVBUF, M_WAITOK);
359 lwkt_gettoken(&ndis_thr_token);
360 STAILQ_INSERT_HEAD(&ndis_free, r, link);
362 lwkt_reltoken(&ndis_thr_token);
369 ndis_shrink_thrqueue(int cnt)
374 for (i = 0; i < cnt; i++) {
375 lwkt_gettoken(&ndis_thr_token);
376 r = STAILQ_FIRST(&ndis_free);
378 lwkt_reltoken(&ndis_thr_token);
381 STAILQ_REMOVE_HEAD(&ndis_free, link);
383 lwkt_reltoken(&ndis_thr_token);
391 ndis_unsched(void (*func)(void *), void *arg, int t)
397 if (t == NDIS_TASKQUEUE) {
399 td = ndis_tproc.np_td;
402 td = ndis_iproc.np_td;
405 lwkt_gettoken(&ndis_thr_token);
406 STAILQ_FOREACH(r, q, link) {
407 if (r->nr_func == func && r->nr_arg == arg) {
408 STAILQ_REMOVE(q, r, ndis_req, link);
409 STAILQ_INSERT_HEAD(&ndis_free, r, link);
410 lwkt_reltoken(&ndis_thr_token);
415 lwkt_reltoken(&ndis_thr_token);
421 ndis_sched(void (*func)(void *), void *arg, int t)
428 if (t == NDIS_TASKQUEUE) {
430 td = ndis_tproc.np_td;
433 td = ndis_iproc.np_td;
436 lwkt_gettoken(&ndis_thr_token);
438 * Check to see if an instance of this job is already
439 * pending. If so, don't bother queuing it again.
441 STAILQ_FOREACH(r, q, link) {
442 if (r->nr_func == func && r->nr_arg == arg) {
443 lwkt_reltoken(&ndis_thr_token);
447 r = STAILQ_FIRST(&ndis_free);
449 lwkt_reltoken(&ndis_thr_token);
452 STAILQ_REMOVE_HEAD(&ndis_free, link);
456 STAILQ_INSERT_TAIL(q, r, link);
457 if (t == NDIS_TASKQUEUE)
458 s = ndis_tproc.np_state;
460 s = ndis_iproc.np_state;
461 lwkt_reltoken(&ndis_thr_token);
464 * Post the job, but only if the thread is actually blocked
465 * on its own suspend call. If a driver queues up a job with
466 * NdisScheduleWorkItem() which happens to do a KeWaitForObject(),
467 * it may suspend there, and in that case we don't want to wake
468 * it up until KeWaitForObject() gets woken up on its own.
470 if (s == NDIS_PSTATE_SLEEPING)
477 ndis_thsuspend(thread_t td, int timo)
481 error = tsleep(td, 0, "ndissp", timo);
486 ndis_thresume(struct thread *td)
491 __stdcall static void
492 ndis_sendrsrcavail_func(ndis_handle adapter)
497 __stdcall static void
498 ndis_status_func(ndis_handle adapter, ndis_status status, void *sbuf,
501 ndis_miniport_block *block;
504 if (block->nmb_ifp->if_flags & IFF_DEBUG)
505 device_printf (block->nmb_dev, "status: %x\n", status);
509 __stdcall static void
510 ndis_statusdone_func(ndis_handle adapter)
512 ndis_miniport_block *block;
515 if (block->nmb_ifp->if_flags & IFF_DEBUG)
516 device_printf (block->nmb_dev, "status complete\n");
520 __stdcall static void
521 ndis_setdone_func(ndis_handle adapter, ndis_status status)
523 ndis_miniport_block *block;
526 block->nmb_setstat = status;
527 wakeup(&block->nmb_wkupdpctimer);
531 __stdcall static void
532 ndis_getdone_func(ndis_handle adapter, ndis_status status)
534 ndis_miniport_block *block;
537 block->nmb_getstat = status;
538 wakeup(&block->nmb_wkupdpctimer);
542 __stdcall static void
543 ndis_resetdone_func(ndis_handle adapter, ndis_status status,
544 uint8_t addressingreset)
546 ndis_miniport_block *block;
549 if (block->nmb_ifp->if_flags & IFF_DEBUG)
550 device_printf (block->nmb_dev, "reset done...\n");
551 wakeup(block->nmb_ifp);
556 ndis_create_sysctls(void *arg)
558 struct ndis_softc *sc;
566 vals = sc->ndis_regvals;
568 TAILQ_INIT(&sc->ndis_cfglist_head);
570 #if __FreeBSD_version < 502113
571 /* Create the sysctl tree. */
573 sc->ndis_tree = SYSCTL_ADD_NODE(&sc->ndis_ctx,
574 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
575 device_get_nameunit(sc->ndis_dev), CTLFLAG_RD, 0,
576 device_get_desc(sc->ndis_dev));
579 /* Add the driver-specific registry keys. */
581 vals = sc->ndis_regvals;
583 if (vals->nc_cfgkey == NULL)
585 if (vals->nc_idx != sc->ndis_devidx) {
590 SYSCTL_ADD_STRING(&sc->ndis_ctx,
591 SYSCTL_CHILDREN(sc->ndis_tree),
592 OID_AUTO, vals->nc_cfgkey,
593 CTLFLAG_RW, vals->nc_val,
594 sizeof(vals->nc_val),
597 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
598 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
599 OID_AUTO, vals->nc_cfgkey,
600 CTLFLAG_RW, vals->nc_val,
601 sizeof(vals->nc_val),
607 /* Now add a couple of builtin keys. */
610 * Environment can be either Windows (0) or WindowsNT (1).
611 * We qualify as the latter.
613 ndis_add_sysctl(sc, "Environment",
614 "Windows environment", "1", CTLFLAG_RD);
616 /* NDIS version should be 5.1. */
617 ndis_add_sysctl(sc, "NdisVersion",
618 "NDIS API Version", "0x00050001", CTLFLAG_RD);
620 /* Bus type (PCI, PCMCIA, etc...) */
621 ksprintf(buf, "%d", (int)sc->ndis_iftype);
622 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
624 if (sc->ndis_res_io != NULL) {
625 ksprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
626 ndis_add_sysctl(sc, "IOBaseAddress",
627 "Base I/O Address", buf, CTLFLAG_RD);
630 if (sc->ndis_irq != NULL) {
631 ksprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
632 ndis_add_sysctl(sc, "InterruptNumber",
633 "Interrupt Number", buf, CTLFLAG_RD);
640 ndis_add_sysctl(void *arg, char *key, char *desc, char *val, int flag)
642 struct ndis_softc *sc;
643 struct ndis_cfglist *cfg;
648 cfg = kmalloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_WAITOK|M_ZERO);
649 cfg->ndis_cfg.nc_cfgkey = kstrdup(key, M_DEVBUF);
651 ksnprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
652 cfg->ndis_cfg.nc_cfgdesc = kstrdup(descstr, M_DEVBUF);
654 cfg->ndis_cfg.nc_cfgdesc = kstrdup(desc, M_DEVBUF);
655 strcpy(cfg->ndis_cfg.nc_val, val);
657 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
660 SYSCTL_ADD_STRING(&sc->ndis_ctx, SYSCTL_CHILDREN(sc->ndis_tree),
661 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
662 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
663 cfg->ndis_cfg.nc_cfgdesc);
665 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
666 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
667 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
668 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
669 cfg->ndis_cfg.nc_cfgdesc);
676 ndis_flush_sysctls(void *arg)
678 struct ndis_softc *sc;
679 struct ndis_cfglist *cfg;
683 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
684 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
685 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
686 kfree(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
687 kfree(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
688 kfree(cfg, M_DEVBUF);
695 ndis_return(void *arg)
697 struct ndis_softc *sc;
698 ndis_return_handler returnfunc;
705 adapter = sc->ndis_block.nmb_miniportadapterctx;
710 returnfunc = sc->ndis_chars.nmc_return_packet_func;
711 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
712 returnfunc(adapter, p);
713 FASTCALL1(hal_lower_irql, irql);
719 ndis_extref_packet(void *arg)
721 ndis_packet *p = arg;
727 ndis_extfree_packet(void *arg)
729 ndis_packet *p = arg;
734 /* Decrement refcount. */
737 /* Release packet when refcount hits zero, otherwise return. */
741 ndis_sched(ndis_return, p, NDIS_SWI);
747 ndis_return_packet(struct ndis_softc *sc, ndis_packet *p)
749 ndis_extfree_packet(p);
753 ndis_free_bufs(ndis_buffer *b0)
762 kfree(b0, M_NDIS_BUFFER);
770 ndis_free_packet(ndis_packet *p)
775 ndis_free_bufs(p->np_private.npp_head);
776 kfree(p, M_NDIS_PACKET);
782 ndis_convert_res(void *arg)
784 struct ndis_softc *sc;
785 ndis_resource_list *rl = NULL;
786 cm_partial_resource_desc *prd = NULL;
787 ndis_miniport_block *block;
789 struct resource_list *brl;
790 struct resource_list brl_rev;
791 struct resource_list_entry *brle, *n;
795 block = &sc->ndis_block;
798 SLIST_INIT(&brl_rev);
800 rl = kmalloc(sizeof(ndis_resource_list) +
801 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
802 M_DEVBUF, M_WAITOK|M_NULLOK|M_ZERO);
807 rl->cprl_version = 5;
808 rl->cprl_version = 1;
809 rl->cprl_count = sc->ndis_rescnt;
810 prd = rl->cprl_partial_descs;
812 brl = BUS_GET_RESOURCE_LIST(dev, dev);
817 * We have a small problem. Some PCI devices have
818 * multiple I/O ranges. Windows orders them starting
819 * from lowest numbered BAR to highest. We discover
820 * them in that order too, but insert them into a singly
821 * linked list head first, which means when time comes
822 * to traverse the list, we enumerate them in reverse
823 * order. This screws up some drivers which expect the
824 * BARs to be in ascending order so that they can choose
825 * the "first" one as their register space. Unfortunately,
826 * in order to fix this, we have to create our own
827 * temporary list with the entries in reverse order.
829 SLIST_FOREACH(brle, brl, link) {
830 n = kmalloc(sizeof(struct resource_list_entry),
831 M_TEMP, M_WAITOK|M_NULLOK);
836 bcopy((char *)brle, (char *)n,
837 sizeof(struct resource_list_entry));
838 SLIST_INSERT_HEAD(&brl_rev, n, link);
841 SLIST_FOREACH(brle, &brl_rev, link) {
842 switch (brle->type) {
844 prd->cprd_type = CmResourceTypePort;
845 prd->cprd_flags = CM_RESOURCE_PORT_IO;
846 prd->cprd_sharedisp =
847 CmResourceShareDeviceExclusive;
848 prd->u.cprd_port.cprd_start.np_quad =
850 prd->u.cprd_port.cprd_len = brle->count;
853 prd->cprd_type = CmResourceTypeMemory;
855 CM_RESOURCE_MEMORY_READ_WRITE;
856 prd->cprd_sharedisp =
857 CmResourceShareDeviceExclusive;
858 prd->u.cprd_port.cprd_start.np_quad =
860 prd->u.cprd_port.cprd_len = brle->count;
863 prd->cprd_type = CmResourceTypeInterrupt;
865 prd->cprd_sharedisp =
866 CmResourceShareDeviceExclusive;
867 prd->u.cprd_intr.cprd_level = brle->start;
868 prd->u.cprd_intr.cprd_vector = brle->start;
869 prd->u.cprd_intr.cprd_affinity = 0;
878 block->nmb_rlist = rl;
882 while (!SLIST_EMPTY(&brl_rev)) {
883 n = SLIST_FIRST(&brl_rev);
884 SLIST_REMOVE_HEAD(&brl_rev, link);
892 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
893 * packet, it will hand it to us in the form of an ndis_packet,
894 * which we need to convert to an mbuf that is then handed off
895 * to the stack. Note: we configure the mbuf list so that it uses
896 * the memory regions specified by the ndis_buffer structures in
897 * the ndis_packet as external storage. In most cases, this will
898 * point to a memory region allocated by the driver (either by
899 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
900 * the driver to handle free()ing this region for is, so we set up
901 * a dummy no-op free handler for it.
905 ndis_ptom(struct mbuf **m0, ndis_packet *p)
907 struct mbuf *m, *prev = NULL;
909 ndis_packet_private *priv;
912 if (p == NULL || m0 == NULL)
915 priv = &p->np_private;
916 buf = priv->npp_head;
919 for (buf = priv->npp_head; buf != NULL; buf = buf->nb_next) {
920 if (buf == priv->npp_head)
921 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
923 MGET(m, MB_DONTWAIT, MT_DATA);
929 m->m_len = buf->nb_bytecount;
930 m->m_data = MDL_VA(buf);
931 m->m_ext.ext_free = ndis_extfree_packet;
932 m->m_ext.ext_ref = ndis_extref_packet;
933 m->m_ext.ext_arg = p;
934 m->m_ext.ext_buf = m->m_data;
935 m->m_ext.ext_size = m->m_len;
938 MEXTADD(m, m->m_data, m->m_len, ndis_free_packet,
943 if (m->m_flags & M_PKTHDR)
950 (*m0)->m_pkthdr.len = totlen;
956 * Create an mbuf chain from an NDIS packet chain.
957 * This is used mainly when transmitting packets, where we need
958 * to turn an mbuf off an interface's send queue and transform it
959 * into an NDIS packet which will be fed into the NDIS driver's
962 * NDIS packets consist of two parts: an ndis_packet structure,
963 * which is vaguely analagous to the pkthdr portion of an mbuf,
964 * and one or more ndis_buffer structures, which define the
965 * actual memory segments in which the packet data resides.
966 * We need to allocate one ndis_buffer for each mbuf in a chain,
967 * plus one ndis_packet as the header.
971 ndis_mtop(struct mbuf *m0, ndis_packet **p)
974 ndis_buffer *buf = NULL, *prev = NULL;
975 ndis_packet_private *priv;
977 if (p == NULL || m0 == NULL)
980 /* If caller didn't supply a packet, make one. */
982 *p = kmalloc(sizeof(ndis_packet), M_NDIS_PACKET, M_NOWAIT|M_ZERO);
987 priv = &(*p)->np_private;
988 priv->npp_totlen = m0->m_pkthdr.len;
989 priv->npp_packetooboffset = offsetof(ndis_packet, np_oob);
990 priv->npp_ndispktflags = NDIS_PACKET_ALLOCATED_BY_NDIS;
992 for (m = m0; m != NULL; m = m->m_next) {
995 buf = kmalloc(sizeof(ndis_buffer), M_NDIS_BUFFER, M_NOWAIT|M_ZERO);
997 ndis_free_packet(*p);
1002 MDL_INIT(buf, m->m_data, m->m_len);
1003 if (priv->npp_head == NULL)
1004 priv->npp_head = buf;
1006 prev->nb_next = buf;
1010 priv->npp_tail = buf;
1011 priv->npp_totlen = m0->m_pkthdr.len;
1017 ndis_get_supported_oids(void *arg, ndis_oid **oids, int *oidcnt)
1022 if (arg == NULL || oids == NULL || oidcnt == NULL)
1025 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
1027 o = kmalloc(len, M_DEVBUF, M_WAITOK);
1029 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
1043 ndis_set_info(void *arg, ndis_oid oid, void *buf, int *buflen)
1045 struct ndis_softc *sc;
1047 ndis_handle adapter;
1048 ndis_setinfo_handler setfunc;
1049 uint32_t byteswritten = 0, bytesneeded = 0;
1054 setfunc = sc->ndis_chars.nmc_setinfo_func;
1055 adapter = sc->ndis_block.nmb_miniportadapterctx;
1057 if (adapter == NULL || setfunc == NULL)
1060 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1061 rval = setfunc(adapter, oid, buf, *buflen,
1062 &byteswritten, &bytesneeded);
1063 FASTCALL1(hal_lower_irql, irql);
1065 if (rval == NDIS_STATUS_PENDING) {
1066 error = tsleep(&sc->ndis_block.nmb_wkupdpctimer,
1067 0, "ndisset", 5 * hz);
1068 rval = sc->ndis_block.nmb_setstat;
1072 *buflen = byteswritten;
1074 *buflen = bytesneeded;
1076 if (rval == NDIS_STATUS_INVALID_LENGTH)
1079 if (rval == NDIS_STATUS_INVALID_OID)
1082 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1083 rval == NDIS_STATUS_NOT_ACCEPTED)
1086 if (rval != NDIS_STATUS_SUCCESS)
1092 typedef __stdcall void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
1095 ndis_send_packets(void *arg, ndis_packet **packets, int cnt)
1097 struct ndis_softc *sc;
1098 ndis_handle adapter;
1099 ndis_sendmulti_handler sendfunc;
1100 ndis_senddone_func senddonefunc;
1106 adapter = sc->ndis_block.nmb_miniportadapterctx;
1107 if (adapter == NULL)
1109 sendfunc = sc->ndis_chars.nmc_sendmulti_func;
1110 senddonefunc = sc->ndis_block.nmb_senddone_func;
1111 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1112 sendfunc(adapter, packets, cnt);
1113 FASTCALL1(hal_lower_irql, irql);
1115 for (i = 0; i < cnt; i++) {
1118 * Either the driver already handed the packet to
1119 * ndis_txeof() due to a failure, or it wants to keep
1120 * it and release it asynchronously later. Skip to the
1123 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
1125 senddonefunc(&sc->ndis_block, p, p->np_oob.npo_status);
1132 ndis_send_packet(void *arg, ndis_packet *packet)
1134 struct ndis_softc *sc;
1135 ndis_handle adapter;
1137 ndis_sendsingle_handler sendfunc;
1138 ndis_senddone_func senddonefunc;
1142 adapter = sc->ndis_block.nmb_miniportadapterctx;
1143 if (adapter == NULL)
1145 sendfunc = sc->ndis_chars.nmc_sendsingle_func;
1146 senddonefunc = sc->ndis_block.nmb_senddone_func;
1148 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1149 status = sendfunc(adapter, packet, packet->np_private.npp_flags);
1150 FASTCALL1(hal_lower_irql, irql);
1152 if (status == NDIS_STATUS_PENDING)
1155 senddonefunc(&sc->ndis_block, packet, status);
1161 ndis_init_dma(void *arg)
1163 struct ndis_softc *sc;
1168 sc->ndis_tmaps = kmalloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
1169 M_DEVBUF, M_WAITOK|M_ZERO);
1171 for (i = 0; i < sc->ndis_maxpkts; i++) {
1172 error = bus_dmamap_create(sc->ndis_ttag, 0,
1173 &sc->ndis_tmaps[i]);
1175 kfree(sc->ndis_tmaps, M_DEVBUF);
1184 ndis_destroy_dma(void *arg)
1186 struct ndis_softc *sc;
1188 ndis_packet *p = NULL;
1193 for (i = 0; i < sc->ndis_maxpkts; i++) {
1194 if (sc->ndis_txarray[i] != NULL) {
1195 p = sc->ndis_txarray[i];
1196 m = (struct mbuf *)p->np_rsvd[1];
1199 ndis_free_packet(sc->ndis_txarray[i]);
1201 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
1204 kfree(sc->ndis_tmaps, M_DEVBUF);
1205 bus_dma_tag_destroy(sc->ndis_ttag);
1211 ndis_reset_nic(void *arg)
1213 struct ndis_softc *sc;
1214 ndis_handle adapter;
1215 ndis_reset_handler resetfunc;
1216 uint8_t addressing_reset;
1221 adapter = sc->ndis_block.nmb_miniportadapterctx;
1222 resetfunc = sc->ndis_chars.nmc_reset_func;
1223 if (adapter == NULL || resetfunc == NULL)
1226 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1227 rval = resetfunc(&addressing_reset, adapter);
1228 FASTCALL1(hal_lower_irql, irql);
1230 if (rval == NDIS_STATUS_PENDING) {
1231 tsleep(sc, 0, "ndisrst", 0);
1238 ndis_halt_nic(void *arg)
1240 struct ndis_softc *sc;
1241 ndis_handle adapter;
1242 ndis_halt_handler haltfunc;
1246 adapter = sc->ndis_block.nmb_miniportadapterctx;
1247 if (adapter == NULL) {
1252 * The adapter context is only valid after the init
1253 * handler has been called, and is invalid once the
1254 * halt handler has been called.
1257 haltfunc = sc->ndis_chars.nmc_halt_func;
1261 sc->ndis_block.nmb_miniportadapterctx = NULL;
1267 ndis_shutdown_nic(void *arg)
1269 struct ndis_softc *sc;
1270 ndis_handle adapter;
1271 ndis_shutdown_handler shutdownfunc;
1274 adapter = sc->ndis_block.nmb_miniportadapterctx;
1275 shutdownfunc = sc->ndis_chars.nmc_shutdown_handler;
1276 if (adapter == NULL || shutdownfunc == NULL)
1279 if (sc->ndis_chars.nmc_rsvd0 == NULL)
1280 shutdownfunc(adapter);
1282 shutdownfunc(sc->ndis_chars.nmc_rsvd0);
1284 ndis_shrink_thrqueue(8);
1285 TAILQ_REMOVE(&ndis_devhead, &sc->ndis_block, link);
1291 ndis_init_nic(void *arg)
1293 struct ndis_softc *sc;
1294 ndis_miniport_block *block;
1295 ndis_init_handler initfunc;
1296 ndis_status status, openstatus = 0;
1297 ndis_medium mediumarray[NdisMediumMax];
1298 uint32_t chosenmedium, i;
1304 block = &sc->ndis_block;
1305 initfunc = sc->ndis_chars.nmc_init_func;
1307 TAILQ_INIT(&block->nmb_timerlist);
1309 for (i = 0; i < NdisMediumMax; i++)
1312 status = initfunc(&openstatus, &chosenmedium,
1313 mediumarray, NdisMediumMax, block, block);
1316 * If the init fails, blow away the other exported routines
1317 * we obtained from the driver so we can't call them later.
1318 * If the init failed, none of these will work.
1320 if (status != NDIS_STATUS_SUCCESS) {
1321 sc->ndis_block.nmb_miniportadapterctx = NULL;
1329 ndis_enable_intr(void *arg)
1331 struct ndis_softc *sc;
1332 ndis_handle adapter;
1333 ndis_enable_interrupts_handler intrenbfunc;
1336 adapter = sc->ndis_block.nmb_miniportadapterctx;
1337 intrenbfunc = sc->ndis_chars.nmc_enable_interrupts_func;
1338 if (adapter == NULL || intrenbfunc == NULL)
1340 intrenbfunc(adapter);
1346 ndis_disable_intr(void *arg)
1348 struct ndis_softc *sc;
1349 ndis_handle adapter;
1350 ndis_disable_interrupts_handler intrdisfunc;
1353 adapter = sc->ndis_block.nmb_miniportadapterctx;
1354 intrdisfunc = sc->ndis_chars.nmc_disable_interrupts_func;
1355 if (adapter == NULL || intrdisfunc == NULL)
1357 intrdisfunc(adapter);
1363 ndis_isr(void *arg, int *ourintr, int *callhandler)
1365 struct ndis_softc *sc;
1366 ndis_handle adapter;
1367 ndis_isr_handler isrfunc;
1368 uint8_t accepted, queue;
1370 if (arg == NULL || ourintr == NULL || callhandler == NULL)
1374 adapter = sc->ndis_block.nmb_miniportadapterctx;
1375 isrfunc = sc->ndis_chars.nmc_isr_func;
1376 if (adapter == NULL || isrfunc == NULL)
1379 isrfunc(&accepted, &queue, adapter);
1380 *ourintr = accepted;
1381 *callhandler = queue;
1387 ndis_intrhand(void *arg)
1389 struct ndis_softc *sc;
1390 ndis_handle adapter;
1391 ndis_interrupt_handler intrfunc;
1397 adapter = sc->ndis_block.nmb_miniportadapterctx;
1398 intrfunc = sc->ndis_chars.nmc_interrupt_func;
1399 if (adapter == NULL || intrfunc == NULL)
1408 ndis_get_info(void *arg, ndis_oid oid, void *buf, int *buflen)
1410 struct ndis_softc *sc;
1412 ndis_handle adapter;
1413 ndis_queryinfo_handler queryfunc;
1414 uint32_t byteswritten = 0, bytesneeded = 0;
1419 queryfunc = sc->ndis_chars.nmc_queryinfo_func;
1420 adapter = sc->ndis_block.nmb_miniportadapterctx;
1422 if (adapter == NULL || queryfunc == NULL)
1425 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1426 rval = queryfunc(adapter, oid, buf, *buflen,
1427 &byteswritten, &bytesneeded);
1428 FASTCALL1(hal_lower_irql, irql);
1430 /* Wait for requests that block. */
1432 if (rval == NDIS_STATUS_PENDING) {
1433 error = tsleep(&sc->ndis_block.nmb_wkupdpctimer,
1434 0, "ndisget", 5 * hz);
1435 rval = sc->ndis_block.nmb_getstat;
1439 *buflen = byteswritten;
1441 *buflen = bytesneeded;
1443 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1444 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1447 if (rval == NDIS_STATUS_INVALID_OID)
1450 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1451 rval == NDIS_STATUS_NOT_ACCEPTED)
1454 if (rval != NDIS_STATUS_SUCCESS)
1461 ndis_unload_driver(void *arg)
1463 struct ndis_softc *sc;
1467 kfree(sc->ndis_block.nmb_rlist, M_DEVBUF);
1469 ndis_flush_sysctls(sc);
1471 ndis_shrink_thrqueue(8);
1472 TAILQ_REMOVE(&ndis_devhead, &sc->ndis_block, link);
1477 #define NDIS_LOADED htonl(0x42534F44)
1480 ndis_load_driver(vm_offset_t img, void *arg)
1483 image_optional_header opt_hdr;
1484 image_import_descriptor imp_desc;
1485 ndis_unicode_string dummystr;
1486 ndis_miniport_block *block;
1490 struct ndis_softc *sc;
1495 * Only perform the relocation/linking phase once
1496 * since the binary image may be shared among multiple
1500 ptr = (uint32_t *)(img + 8);
1501 if (*ptr != NDIS_LOADED) {
1502 /* Perform text relocation */
1503 if (pe_relocate(img))
1506 /* Dynamically link the NDIS.SYS routines -- required. */
1507 if (pe_patch_imports(img, "NDIS", ndis_functbl))
1510 /* Dynamically link the HAL.dll routines -- also required. */
1511 if (pe_patch_imports(img, "HAL", hal_functbl))
1514 /* Dynamically link ntoskrnl.exe -- optional. */
1515 if (pe_get_import_descriptor(img,
1516 &imp_desc, "ntoskrnl") == 0) {
1517 if (pe_patch_imports(img,
1518 "ntoskrnl", ntoskrnl_functbl))
1524 /* Locate the driver entry point */
1525 pe_get_optional_header(img, &opt_hdr);
1526 entry = (driver_entry)pe_translate_addr(img, opt_hdr.ioh_entryaddr);
1528 dummystr.nus_len = strlen(NDIS_DUMMY_PATH) * 2;
1529 dummystr.nus_maxlen = strlen(NDIS_DUMMY_PATH) * 2;
1530 dummystr.nus_buf = NULL;
1531 ndis_ascii_to_unicode(NDIS_DUMMY_PATH, &dummystr.nus_buf);
1534 * Now that we have the miniport driver characteristics,
1535 * create an NDIS block and call the init handler.
1536 * This will cause the driver to try to probe for
1540 block = &sc->ndis_block;
1542 ptr = (uint32_t *)block;
1543 for (idx = 0; idx < sizeof(ndis_miniport_block) / 4; idx++) {
1544 *ptr = idx | 0xdead0000;
1548 block->nmb_signature = (void *)0xcafebabe;
1549 block->nmb_setdone_func = ndis_setdone_func;
1550 block->nmb_querydone_func = ndis_getdone_func;
1551 block->nmb_status_func = ndis_status_func;
1552 block->nmb_statusdone_func = ndis_statusdone_func;
1553 block->nmb_resetdone_func = ndis_resetdone_func;
1554 block->nmb_sendrsrc_func = ndis_sendrsrcavail_func;
1556 block->nmb_ifp = &sc->arpcom.ac_if;
1557 block->nmb_dev = sc->ndis_dev;
1558 block->nmb_img = img;
1559 block->nmb_devobj.do_rsvd = block;
1562 * Now call the DriverEntry() routine. This will cause
1563 * a callout to the NdisInitializeWrapper() and
1564 * NdisMRegisterMiniport() routines.
1566 status = entry(&block->nmb_devobj, &dummystr);
1568 kfree (dummystr.nus_buf, M_DEVBUF);
1570 if (status != NDIS_STATUS_SUCCESS)
1573 ndis_enlarge_thrqueue(8);
1575 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);