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.11 2006/09/05 00:55:45 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>
53 #include <machine/bus.h>
54 #include <machine/resource.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;
202 struct lwkt_tokref tokref;
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(&tokref, &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(&tokref);
222 if (r->nr_func != NULL)
223 (*r->nr_func)(r->nr_arg);
225 lwkt_gettoken(&tokref, &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(&tokref);
236 /* Bail if we were told to shut down. */
247 ndis_create_kthreads(void)
252 lwkt_token_init(&ndis_thr_token);
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)
318 struct lwkt_tokref tokref;
320 if (t == NDIS_TASKQUEUE) {
322 td = ndis_tproc.np_td;
325 td = ndis_iproc.np_td;
328 /* Create and post a special 'exit' job. */
330 lwkt_gettoken(&tokref, &ndis_thr_token);
331 r = STAILQ_FIRST(&ndis_free);
332 STAILQ_REMOVE_HEAD(&ndis_free, link);
336 STAILQ_INSERT_TAIL(q, r, link);
337 lwkt_reltoken(&tokref);
341 /* wait for thread exit */
343 tsleep(r, PCATCH, "ndisthexit", hz * 60);
345 /* Now empty the job list. */
347 lwkt_gettoken(&tokref, &ndis_thr_token);
348 while ((r = STAILQ_FIRST(q)) != NULL) {
349 STAILQ_REMOVE_HEAD(q, link);
350 STAILQ_INSERT_HEAD(&ndis_free, r, link);
352 lwkt_reltoken(&tokref);
358 ndis_enlarge_thrqueue(int cnt)
362 struct lwkt_tokref tokref;
364 for (i = 0; i < cnt; i++) {
365 r = kmalloc(sizeof(struct ndis_req), M_DEVBUF, M_WAITOK);
366 lwkt_gettoken(&tokref, &ndis_thr_token);
367 STAILQ_INSERT_HEAD(&ndis_free, r, link);
369 lwkt_reltoken(&tokref);
376 ndis_shrink_thrqueue(int cnt)
380 struct lwkt_tokref tokref;
382 for (i = 0; i < cnt; i++) {
383 lwkt_gettoken(&tokref, &ndis_thr_token);
384 r = STAILQ_FIRST(&ndis_free);
386 lwkt_reltoken(&tokref);
389 STAILQ_REMOVE_HEAD(&ndis_free, link);
391 lwkt_reltoken(&tokref);
399 ndis_unsched(void (*func)(void *), void *arg, int t)
404 struct lwkt_tokref tokref;
406 if (t == NDIS_TASKQUEUE) {
408 td = ndis_tproc.np_td;
411 td = ndis_iproc.np_td;
414 lwkt_gettoken(&tokref, &ndis_thr_token);
415 STAILQ_FOREACH(r, q, link) {
416 if (r->nr_func == func && r->nr_arg == arg) {
417 STAILQ_REMOVE(q, r, ndis_req, link);
418 STAILQ_INSERT_HEAD(&ndis_free, r, link);
419 lwkt_reltoken(&tokref);
424 lwkt_reltoken(&tokref);
430 ndis_sched(void (*func)(void *), void *arg, int t)
436 struct lwkt_tokref tokref;
438 if (t == NDIS_TASKQUEUE) {
440 td = ndis_tproc.np_td;
443 td = ndis_iproc.np_td;
446 lwkt_gettoken(&tokref, &ndis_thr_token);
448 * Check to see if an instance of this job is already
449 * pending. If so, don't bother queuing it again.
451 STAILQ_FOREACH(r, q, link) {
452 if (r->nr_func == func && r->nr_arg == arg) {
453 lwkt_reltoken(&tokref);
457 r = STAILQ_FIRST(&ndis_free);
459 lwkt_reltoken(&tokref);
462 STAILQ_REMOVE_HEAD(&ndis_free, link);
466 STAILQ_INSERT_TAIL(q, r, link);
467 if (t == NDIS_TASKQUEUE)
468 s = ndis_tproc.np_state;
470 s = ndis_iproc.np_state;
471 lwkt_reltoken(&tokref);
474 * Post the job, but only if the thread is actually blocked
475 * on its own suspend call. If a driver queues up a job with
476 * NdisScheduleWorkItem() which happens to do a KeWaitForObject(),
477 * it may suspend there, and in that case we don't want to wake
478 * it up until KeWaitForObject() gets woken up on its own.
480 if (s == NDIS_PSTATE_SLEEPING)
487 ndis_thsuspend(thread_t td, int timo)
491 error = tsleep(td, 0, "ndissp", timo);
496 ndis_thresume(struct thread *td)
501 __stdcall static void
502 ndis_sendrsrcavail_func(ndis_handle adapter)
507 __stdcall static void
508 ndis_status_func(ndis_handle adapter, ndis_status status, void *sbuf,
511 ndis_miniport_block *block;
514 if (block->nmb_ifp->if_flags & IFF_DEBUG)
515 device_printf (block->nmb_dev, "status: %x\n", status);
519 __stdcall static void
520 ndis_statusdone_func(ndis_handle adapter)
522 ndis_miniport_block *block;
525 if (block->nmb_ifp->if_flags & IFF_DEBUG)
526 device_printf (block->nmb_dev, "status complete\n");
530 __stdcall static void
531 ndis_setdone_func(ndis_handle adapter, ndis_status status)
533 ndis_miniport_block *block;
536 block->nmb_setstat = status;
537 wakeup(&block->nmb_wkupdpctimer);
541 __stdcall static void
542 ndis_getdone_func(ndis_handle adapter, ndis_status status)
544 ndis_miniport_block *block;
547 block->nmb_getstat = status;
548 wakeup(&block->nmb_wkupdpctimer);
552 __stdcall static void
553 ndis_resetdone_func(ndis_handle adapter, ndis_status status,
554 uint8_t addressingreset)
556 ndis_miniport_block *block;
559 if (block->nmb_ifp->if_flags & IFF_DEBUG)
560 device_printf (block->nmb_dev, "reset done...\n");
561 wakeup(block->nmb_ifp);
566 ndis_create_sysctls(void *arg)
568 struct ndis_softc *sc;
576 vals = sc->ndis_regvals;
578 TAILQ_INIT(&sc->ndis_cfglist_head);
580 #if __FreeBSD_version < 502113
581 /* Create the sysctl tree. */
583 sc->ndis_tree = SYSCTL_ADD_NODE(&sc->ndis_ctx,
584 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
585 device_get_nameunit(sc->ndis_dev), CTLFLAG_RD, 0,
586 device_get_desc(sc->ndis_dev));
589 /* Add the driver-specific registry keys. */
591 vals = sc->ndis_regvals;
593 if (vals->nc_cfgkey == NULL)
595 if (vals->nc_idx != sc->ndis_devidx) {
600 SYSCTL_ADD_STRING(&sc->ndis_ctx,
601 SYSCTL_CHILDREN(sc->ndis_tree),
602 OID_AUTO, vals->nc_cfgkey,
603 CTLFLAG_RW, vals->nc_val,
604 sizeof(vals->nc_val),
607 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
608 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
609 OID_AUTO, vals->nc_cfgkey,
610 CTLFLAG_RW, vals->nc_val,
611 sizeof(vals->nc_val),
617 /* Now add a couple of builtin keys. */
620 * Environment can be either Windows (0) or WindowsNT (1).
621 * We qualify as the latter.
623 ndis_add_sysctl(sc, "Environment",
624 "Windows environment", "1", CTLFLAG_RD);
626 /* NDIS version should be 5.1. */
627 ndis_add_sysctl(sc, "NdisVersion",
628 "NDIS API Version", "0x00050001", CTLFLAG_RD);
630 /* Bus type (PCI, PCMCIA, etc...) */
631 sprintf(buf, "%d", (int)sc->ndis_iftype);
632 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
634 if (sc->ndis_res_io != NULL) {
635 sprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
636 ndis_add_sysctl(sc, "IOBaseAddress",
637 "Base I/O Address", buf, CTLFLAG_RD);
640 if (sc->ndis_irq != NULL) {
641 sprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
642 ndis_add_sysctl(sc, "InterruptNumber",
643 "Interrupt Number", buf, CTLFLAG_RD);
650 ndis_add_sysctl(void *arg, char *key, char *desc, char *val, int flag)
652 struct ndis_softc *sc;
653 struct ndis_cfglist *cfg;
658 cfg = kmalloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_WAITOK|M_ZERO);
659 cfg->ndis_cfg.nc_cfgkey = kstrdup(key, M_DEVBUF);
661 snprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
662 cfg->ndis_cfg.nc_cfgdesc = kstrdup(descstr, M_DEVBUF);
664 cfg->ndis_cfg.nc_cfgdesc = kstrdup(desc, M_DEVBUF);
665 strcpy(cfg->ndis_cfg.nc_val, val);
667 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
670 SYSCTL_ADD_STRING(&sc->ndis_ctx, SYSCTL_CHILDREN(sc->ndis_tree),
671 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
672 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
673 cfg->ndis_cfg.nc_cfgdesc);
675 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
676 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
677 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
678 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
679 cfg->ndis_cfg.nc_cfgdesc);
686 ndis_flush_sysctls(void *arg)
688 struct ndis_softc *sc;
689 struct ndis_cfglist *cfg;
693 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
694 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
695 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
696 kfree(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
697 kfree(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
698 kfree(cfg, M_DEVBUF);
705 ndis_return(void *arg)
707 struct ndis_softc *sc;
708 ndis_return_handler returnfunc;
715 adapter = sc->ndis_block.nmb_miniportadapterctx;
720 returnfunc = sc->ndis_chars.nmc_return_packet_func;
721 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
722 returnfunc(adapter, p);
723 FASTCALL1(hal_lower_irql, irql);
729 ndis_extref_packet(void *arg)
731 ndis_packet *p = arg;
737 ndis_extfree_packet(void *arg)
739 ndis_packet *p = arg;
744 /* Decrement refcount. */
747 /* Release packet when refcount hits zero, otherwise return. */
751 ndis_sched(ndis_return, p, NDIS_SWI);
757 ndis_return_packet(struct ndis_softc *sc, ndis_packet *p)
759 ndis_extfree_packet(p);
763 ndis_free_bufs(ndis_buffer *b0)
772 kfree(b0, M_NDIS_BUFFER);
780 ndis_free_packet(ndis_packet *p)
785 ndis_free_bufs(p->np_private.npp_head);
786 kfree(p, M_NDIS_PACKET);
792 ndis_convert_res(void *arg)
794 struct ndis_softc *sc;
795 ndis_resource_list *rl = NULL;
796 cm_partial_resource_desc *prd = NULL;
797 ndis_miniport_block *block;
799 struct resource_list *brl;
800 struct resource_list brl_rev;
801 struct resource_list_entry *brle, *n;
805 block = &sc->ndis_block;
808 SLIST_INIT(&brl_rev);
810 rl = malloc(sizeof(ndis_resource_list) +
811 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
812 M_DEVBUF, M_WAITOK|M_NULLOK|M_ZERO);
817 rl->cprl_version = 5;
818 rl->cprl_version = 1;
819 rl->cprl_count = sc->ndis_rescnt;
820 prd = rl->cprl_partial_descs;
822 brl = BUS_GET_RESOURCE_LIST(dev, dev);
827 * We have a small problem. Some PCI devices have
828 * multiple I/O ranges. Windows orders them starting
829 * from lowest numbered BAR to highest. We discover
830 * them in that order too, but insert them into a singly
831 * linked list head first, which means when time comes
832 * to traverse the list, we enumerate them in reverse
833 * order. This screws up some drivers which expect the
834 * BARs to be in ascending order so that they can choose
835 * the "first" one as their register space. Unfortunately,
836 * in order to fix this, we have to create our own
837 * temporary list with the entries in reverse order.
839 SLIST_FOREACH(brle, brl, link) {
840 n = malloc(sizeof(struct resource_list_entry),
841 M_TEMP, M_WAITOK|M_NULLOK);
846 bcopy((char *)brle, (char *)n,
847 sizeof(struct resource_list_entry));
848 SLIST_INSERT_HEAD(&brl_rev, n, link);
851 SLIST_FOREACH(brle, &brl_rev, link) {
852 switch (brle->type) {
854 prd->cprd_type = CmResourceTypePort;
855 prd->cprd_flags = CM_RESOURCE_PORT_IO;
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 = CmResourceTypeMemory;
865 CM_RESOURCE_MEMORY_READ_WRITE;
866 prd->cprd_sharedisp =
867 CmResourceShareDeviceExclusive;
868 prd->u.cprd_port.cprd_start.np_quad =
870 prd->u.cprd_port.cprd_len = brle->count;
873 prd->cprd_type = CmResourceTypeInterrupt;
875 prd->cprd_sharedisp =
876 CmResourceShareDeviceExclusive;
877 prd->u.cprd_intr.cprd_level = brle->start;
878 prd->u.cprd_intr.cprd_vector = brle->start;
879 prd->u.cprd_intr.cprd_affinity = 0;
888 block->nmb_rlist = rl;
892 while (!SLIST_EMPTY(&brl_rev)) {
893 n = SLIST_FIRST(&brl_rev);
894 SLIST_REMOVE_HEAD(&brl_rev, link);
902 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
903 * packet, it will hand it to us in the form of an ndis_packet,
904 * which we need to convert to an mbuf that is then handed off
905 * to the stack. Note: we configure the mbuf list so that it uses
906 * the memory regions specified by the ndis_buffer structures in
907 * the ndis_packet as external storage. In most cases, this will
908 * point to a memory region allocated by the driver (either by
909 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
910 * the driver to handle free()ing this region for is, so we set up
911 * a dummy no-op free handler for it.
915 ndis_ptom(struct mbuf **m0, ndis_packet *p)
917 struct mbuf *m, *prev = NULL;
919 ndis_packet_private *priv;
922 if (p == NULL || m0 == NULL)
925 priv = &p->np_private;
926 buf = priv->npp_head;
929 for (buf = priv->npp_head; buf != NULL; buf = buf->nb_next) {
930 if (buf == priv->npp_head)
931 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
933 MGET(m, MB_DONTWAIT, MT_DATA);
939 m->m_len = buf->nb_bytecount;
940 m->m_data = MDL_VA(buf);
941 m->m_ext.ext_free = ndis_extfree_packet;
942 m->m_ext.ext_ref = ndis_extref_packet;
943 m->m_ext.ext_arg = p;
944 m->m_ext.ext_buf = m->m_data;
945 m->m_ext.ext_size = m->m_len;
948 MEXTADD(m, m->m_data, m->m_len, ndis_free_packet,
953 if (m->m_flags & M_PKTHDR)
960 (*m0)->m_pkthdr.len = totlen;
966 * Create an mbuf chain from an NDIS packet chain.
967 * This is used mainly when transmitting packets, where we need
968 * to turn an mbuf off an interface's send queue and transform it
969 * into an NDIS packet which will be fed into the NDIS driver's
972 * NDIS packets consist of two parts: an ndis_packet structure,
973 * which is vaguely analagous to the pkthdr portion of an mbuf,
974 * and one or more ndis_buffer structures, which define the
975 * actual memory segments in which the packet data resides.
976 * We need to allocate one ndis_buffer for each mbuf in a chain,
977 * plus one ndis_packet as the header.
981 ndis_mtop(struct mbuf *m0, ndis_packet **p)
984 ndis_buffer *buf = NULL, *prev = NULL;
985 ndis_packet_private *priv;
987 if (p == NULL || m0 == NULL)
990 /* If caller didn't supply a packet, make one. */
992 *p = kmalloc(sizeof(ndis_packet), M_NDIS_PACKET, M_NOWAIT|M_ZERO);
997 priv = &(*p)->np_private;
998 priv->npp_totlen = m0->m_pkthdr.len;
999 priv->npp_packetooboffset = offsetof(ndis_packet, np_oob);
1000 priv->npp_ndispktflags = NDIS_PACKET_ALLOCATED_BY_NDIS;
1002 for (m = m0; m != NULL; m = m->m_next) {
1005 buf = kmalloc(sizeof(ndis_buffer), M_NDIS_BUFFER, M_NOWAIT|M_ZERO);
1007 ndis_free_packet(*p);
1012 MDL_INIT(buf, m->m_data, m->m_len);
1013 if (priv->npp_head == NULL)
1014 priv->npp_head = buf;
1016 prev->nb_next = buf;
1020 priv->npp_tail = buf;
1021 priv->npp_totlen = m0->m_pkthdr.len;
1027 ndis_get_supported_oids(void *arg, ndis_oid **oids, int *oidcnt)
1032 if (arg == NULL || oids == NULL || oidcnt == NULL)
1035 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
1037 o = kmalloc(len, M_DEVBUF, M_WAITOK);
1039 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
1053 ndis_set_info(void *arg, ndis_oid oid, void *buf, int *buflen)
1055 struct ndis_softc *sc;
1057 ndis_handle adapter;
1058 ndis_setinfo_handler setfunc;
1059 uint32_t byteswritten = 0, bytesneeded = 0;
1064 setfunc = sc->ndis_chars.nmc_setinfo_func;
1065 adapter = sc->ndis_block.nmb_miniportadapterctx;
1067 if (adapter == NULL || setfunc == NULL)
1070 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1071 rval = setfunc(adapter, oid, buf, *buflen,
1072 &byteswritten, &bytesneeded);
1073 FASTCALL1(hal_lower_irql, irql);
1075 if (rval == NDIS_STATUS_PENDING) {
1076 error = tsleep(&sc->ndis_block.nmb_wkupdpctimer,
1077 0, "ndisset", 5 * hz);
1078 rval = sc->ndis_block.nmb_setstat;
1082 *buflen = byteswritten;
1084 *buflen = bytesneeded;
1086 if (rval == NDIS_STATUS_INVALID_LENGTH)
1089 if (rval == NDIS_STATUS_INVALID_OID)
1092 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1093 rval == NDIS_STATUS_NOT_ACCEPTED)
1096 if (rval != NDIS_STATUS_SUCCESS)
1102 typedef __stdcall void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
1105 ndis_send_packets(void *arg, ndis_packet **packets, int cnt)
1107 struct ndis_softc *sc;
1108 ndis_handle adapter;
1109 ndis_sendmulti_handler sendfunc;
1110 ndis_senddone_func senddonefunc;
1116 adapter = sc->ndis_block.nmb_miniportadapterctx;
1117 if (adapter == NULL)
1119 sendfunc = sc->ndis_chars.nmc_sendmulti_func;
1120 senddonefunc = sc->ndis_block.nmb_senddone_func;
1121 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1122 sendfunc(adapter, packets, cnt);
1123 FASTCALL1(hal_lower_irql, irql);
1125 for (i = 0; i < cnt; i++) {
1128 * Either the driver already handed the packet to
1129 * ndis_txeof() due to a failure, or it wants to keep
1130 * it and release it asynchronously later. Skip to the
1133 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
1135 senddonefunc(&sc->ndis_block, p, p->np_oob.npo_status);
1142 ndis_send_packet(void *arg, ndis_packet *packet)
1144 struct ndis_softc *sc;
1145 ndis_handle adapter;
1147 ndis_sendsingle_handler sendfunc;
1148 ndis_senddone_func senddonefunc;
1152 adapter = sc->ndis_block.nmb_miniportadapterctx;
1153 if (adapter == NULL)
1155 sendfunc = sc->ndis_chars.nmc_sendsingle_func;
1156 senddonefunc = sc->ndis_block.nmb_senddone_func;
1158 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1159 status = sendfunc(adapter, packet, packet->np_private.npp_flags);
1160 FASTCALL1(hal_lower_irql, irql);
1162 if (status == NDIS_STATUS_PENDING)
1165 senddonefunc(&sc->ndis_block, packet, status);
1171 ndis_init_dma(void *arg)
1173 struct ndis_softc *sc;
1178 sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
1179 M_DEVBUF, M_WAITOK|M_ZERO);
1181 for (i = 0; i < sc->ndis_maxpkts; i++) {
1182 error = bus_dmamap_create(sc->ndis_ttag, 0,
1183 &sc->ndis_tmaps[i]);
1185 kfree(sc->ndis_tmaps, M_DEVBUF);
1194 ndis_destroy_dma(void *arg)
1196 struct ndis_softc *sc;
1198 ndis_packet *p = NULL;
1203 for (i = 0; i < sc->ndis_maxpkts; i++) {
1204 if (sc->ndis_txarray[i] != NULL) {
1205 p = sc->ndis_txarray[i];
1206 m = (struct mbuf *)p->np_rsvd[1];
1209 ndis_free_packet(sc->ndis_txarray[i]);
1211 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
1214 kfree(sc->ndis_tmaps, M_DEVBUF);
1215 bus_dma_tag_destroy(sc->ndis_ttag);
1221 ndis_reset_nic(void *arg)
1223 struct ndis_softc *sc;
1224 ndis_handle adapter;
1225 ndis_reset_handler resetfunc;
1226 uint8_t addressing_reset;
1231 adapter = sc->ndis_block.nmb_miniportadapterctx;
1232 resetfunc = sc->ndis_chars.nmc_reset_func;
1233 if (adapter == NULL || resetfunc == NULL)
1236 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1237 rval = resetfunc(&addressing_reset, adapter);
1238 FASTCALL1(hal_lower_irql, irql);
1240 if (rval == NDIS_STATUS_PENDING) {
1241 tsleep(sc, 0, "ndisrst", 0);
1248 ndis_halt_nic(void *arg)
1250 struct ndis_softc *sc;
1251 ndis_handle adapter;
1252 ndis_halt_handler haltfunc;
1256 adapter = sc->ndis_block.nmb_miniportadapterctx;
1257 if (adapter == NULL) {
1262 * The adapter context is only valid after the init
1263 * handler has been called, and is invalid once the
1264 * halt handler has been called.
1267 haltfunc = sc->ndis_chars.nmc_halt_func;
1271 sc->ndis_block.nmb_miniportadapterctx = NULL;
1277 ndis_shutdown_nic(void *arg)
1279 struct ndis_softc *sc;
1280 ndis_handle adapter;
1281 ndis_shutdown_handler shutdownfunc;
1284 adapter = sc->ndis_block.nmb_miniportadapterctx;
1285 shutdownfunc = sc->ndis_chars.nmc_shutdown_handler;
1286 if (adapter == NULL || shutdownfunc == NULL)
1289 if (sc->ndis_chars.nmc_rsvd0 == NULL)
1290 shutdownfunc(adapter);
1292 shutdownfunc(sc->ndis_chars.nmc_rsvd0);
1294 ndis_shrink_thrqueue(8);
1295 TAILQ_REMOVE(&ndis_devhead, &sc->ndis_block, link);
1301 ndis_init_nic(void *arg)
1303 struct ndis_softc *sc;
1304 ndis_miniport_block *block;
1305 ndis_init_handler initfunc;
1306 ndis_status status, openstatus = 0;
1307 ndis_medium mediumarray[NdisMediumMax];
1308 uint32_t chosenmedium, i;
1314 block = &sc->ndis_block;
1315 initfunc = sc->ndis_chars.nmc_init_func;
1317 TAILQ_INIT(&block->nmb_timerlist);
1319 for (i = 0; i < NdisMediumMax; i++)
1322 status = initfunc(&openstatus, &chosenmedium,
1323 mediumarray, NdisMediumMax, block, block);
1326 * If the init fails, blow away the other exported routines
1327 * we obtained from the driver so we can't call them later.
1328 * If the init failed, none of these will work.
1330 if (status != NDIS_STATUS_SUCCESS) {
1331 sc->ndis_block.nmb_miniportadapterctx = NULL;
1339 ndis_enable_intr(void *arg)
1341 struct ndis_softc *sc;
1342 ndis_handle adapter;
1343 ndis_enable_interrupts_handler intrenbfunc;
1346 adapter = sc->ndis_block.nmb_miniportadapterctx;
1347 intrenbfunc = sc->ndis_chars.nmc_enable_interrupts_func;
1348 if (adapter == NULL || intrenbfunc == NULL)
1350 intrenbfunc(adapter);
1356 ndis_disable_intr(void *arg)
1358 struct ndis_softc *sc;
1359 ndis_handle adapter;
1360 ndis_disable_interrupts_handler intrdisfunc;
1363 adapter = sc->ndis_block.nmb_miniportadapterctx;
1364 intrdisfunc = sc->ndis_chars.nmc_disable_interrupts_func;
1365 if (adapter == NULL || intrdisfunc == NULL)
1367 intrdisfunc(adapter);
1373 ndis_isr(void *arg, int *ourintr, int *callhandler)
1375 struct ndis_softc *sc;
1376 ndis_handle adapter;
1377 ndis_isr_handler isrfunc;
1378 uint8_t accepted, queue;
1380 if (arg == NULL || ourintr == NULL || callhandler == NULL)
1384 adapter = sc->ndis_block.nmb_miniportadapterctx;
1385 isrfunc = sc->ndis_chars.nmc_isr_func;
1386 if (adapter == NULL || isrfunc == NULL)
1389 isrfunc(&accepted, &queue, adapter);
1390 *ourintr = accepted;
1391 *callhandler = queue;
1397 ndis_intrhand(void *arg)
1399 struct ndis_softc *sc;
1400 ndis_handle adapter;
1401 ndis_interrupt_handler intrfunc;
1407 adapter = sc->ndis_block.nmb_miniportadapterctx;
1408 intrfunc = sc->ndis_chars.nmc_interrupt_func;
1409 if (adapter == NULL || intrfunc == NULL)
1418 ndis_get_info(void *arg, ndis_oid oid, void *buf, int *buflen)
1420 struct ndis_softc *sc;
1422 ndis_handle adapter;
1423 ndis_queryinfo_handler queryfunc;
1424 uint32_t byteswritten = 0, bytesneeded = 0;
1429 queryfunc = sc->ndis_chars.nmc_queryinfo_func;
1430 adapter = sc->ndis_block.nmb_miniportadapterctx;
1432 if (adapter == NULL || queryfunc == NULL)
1435 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1436 rval = queryfunc(adapter, oid, buf, *buflen,
1437 &byteswritten, &bytesneeded);
1438 FASTCALL1(hal_lower_irql, irql);
1440 /* Wait for requests that block. */
1442 if (rval == NDIS_STATUS_PENDING) {
1443 error = tsleep(&sc->ndis_block.nmb_wkupdpctimer,
1444 0, "ndisget", 5 * hz);
1445 rval = sc->ndis_block.nmb_getstat;
1449 *buflen = byteswritten;
1451 *buflen = bytesneeded;
1453 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1454 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1457 if (rval == NDIS_STATUS_INVALID_OID)
1460 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1461 rval == NDIS_STATUS_NOT_ACCEPTED)
1464 if (rval != NDIS_STATUS_SUCCESS)
1471 ndis_unload_driver(void *arg)
1473 struct ndis_softc *sc;
1477 kfree(sc->ndis_block.nmb_rlist, M_DEVBUF);
1479 ndis_flush_sysctls(sc);
1481 ndis_shrink_thrqueue(8);
1482 TAILQ_REMOVE(&ndis_devhead, &sc->ndis_block, link);
1487 #define NDIS_LOADED htonl(0x42534F44)
1490 ndis_load_driver(vm_offset_t img, void *arg)
1493 image_optional_header opt_hdr;
1494 image_import_descriptor imp_desc;
1495 ndis_unicode_string dummystr;
1496 ndis_miniport_block *block;
1500 struct ndis_softc *sc;
1505 * Only perform the relocation/linking phase once
1506 * since the binary image may be shared among multiple
1510 ptr = (uint32_t *)(img + 8);
1511 if (*ptr != NDIS_LOADED) {
1512 /* Perform text relocation */
1513 if (pe_relocate(img))
1516 /* Dynamically link the NDIS.SYS routines -- required. */
1517 if (pe_patch_imports(img, "NDIS", ndis_functbl))
1520 /* Dynamically link the HAL.dll routines -- also required. */
1521 if (pe_patch_imports(img, "HAL", hal_functbl))
1524 /* Dynamically link ntoskrnl.exe -- optional. */
1525 if (pe_get_import_descriptor(img,
1526 &imp_desc, "ntoskrnl") == 0) {
1527 if (pe_patch_imports(img,
1528 "ntoskrnl", ntoskrnl_functbl))
1534 /* Locate the driver entry point */
1535 pe_get_optional_header(img, &opt_hdr);
1536 entry = (driver_entry)pe_translate_addr(img, opt_hdr.ioh_entryaddr);
1538 dummystr.nus_len = strlen(NDIS_DUMMY_PATH) * 2;
1539 dummystr.nus_maxlen = strlen(NDIS_DUMMY_PATH) * 2;
1540 dummystr.nus_buf = NULL;
1541 ndis_ascii_to_unicode(NDIS_DUMMY_PATH, &dummystr.nus_buf);
1544 * Now that we have the miniport driver characteristics,
1545 * create an NDIS block and call the init handler.
1546 * This will cause the driver to try to probe for
1550 block = &sc->ndis_block;
1552 ptr = (uint32_t *)block;
1553 for (idx = 0; idx < sizeof(ndis_miniport_block) / 4; idx++) {
1554 *ptr = idx | 0xdead0000;
1558 block->nmb_signature = (void *)0xcafebabe;
1559 block->nmb_setdone_func = ndis_setdone_func;
1560 block->nmb_querydone_func = ndis_getdone_func;
1561 block->nmb_status_func = ndis_status_func;
1562 block->nmb_statusdone_func = ndis_statusdone_func;
1563 block->nmb_resetdone_func = ndis_resetdone_func;
1564 block->nmb_sendrsrc_func = ndis_sendrsrcavail_func;
1566 block->nmb_ifp = &sc->arpcom.ac_if;
1567 block->nmb_dev = sc->ndis_dev;
1568 block->nmb_img = img;
1569 block->nmb_devobj.do_rsvd = block;
1572 * Now call the DriverEntry() routine. This will cause
1573 * a callout to the NdisInitializeWrapper() and
1574 * NdisMRegisterMiniport() routines.
1576 status = entry(&block->nmb_devobj, &dummystr);
1578 kfree (dummystr.nus_buf, M_DEVBUF);
1580 if (status != NDIS_STATUS_SUCCESS)
1583 ndis_enlarge_thrqueue(8);
1585 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);