kernel: Remove <sys/mutex.h> from all files that don't need it (2/2).
[dragonfly.git] / sys / emulation / ndis / kern_ndis.c
blob6cbba4f2167c3dffd0e22840180965b055416ffd
1 /*-
2 * Copyright (c) 2003
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
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
32 * $FreeBSD: src/sys/compat/ndis/kern_ndis.c,v 1.111 2011/02/23 21:45:28 brucec Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/unistd.h>
38 #include <sys/types.h>
39 #include <sys/errno.h>
40 #include <sys/callout.h>
41 #include <sys/socket.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44 #include <sys/proc.h>
45 #include <sys/malloc.h>
46 #include <sys/lock.h>
47 #include <sys/conf.h>
49 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/kthread.h>
52 #include <sys/bus.h>
53 #include <sys/rman.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/ethernet.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
61 #include <netproto/802_11/ieee80211_var.h>
62 #include <netproto/802_11/ieee80211_ioctl.h>
64 #include <bus/u4b/usb.h>
65 #include <bus/u4b/usbdi.h>
67 #include <emulation/ndis/pe_var.h>
68 #include <emulation/ndis/cfg_var.h>
69 #include <emulation/ndis/resource_var.h>
70 #include <emulation/ndis/ntoskrnl_var.h>
71 #include <emulation/ndis/ndis_var.h>
72 #include <emulation/ndis/hal_var.h>
73 #include <emulation/ndis/u4bd_var.h>
74 #include <dev/netif/ndis/if_ndisvar.h>
76 #define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
78 static void ndis_status_func(ndis_handle, ndis_status, void *, uint32_t);
79 static void ndis_statusdone_func(ndis_handle);
80 static void ndis_setdone_func(ndis_handle, ndis_status);
81 static void ndis_getdone_func(ndis_handle, ndis_status);
82 static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
83 static void ndis_sendrsrcavail_func(ndis_handle);
84 static void ndis_intrsetup(kdpc *, device_object *,
85 irp *, struct ndis_softc *);
86 static void ndis_return(device_object *, void *);
88 static image_patch_table kernndis_functbl[] = {
89 IMPORT_SFUNC(ndis_status_func, 4),
90 IMPORT_SFUNC(ndis_statusdone_func, 1),
91 IMPORT_SFUNC(ndis_setdone_func, 2),
92 IMPORT_SFUNC(ndis_getdone_func, 2),
93 IMPORT_SFUNC(ndis_resetdone_func, 3),
94 IMPORT_SFUNC(ndis_sendrsrcavail_func, 1),
95 IMPORT_SFUNC(ndis_intrsetup, 4),
96 IMPORT_SFUNC(ndis_return, 1),
98 { NULL, NULL, NULL }
101 static struct nd_head ndis_devhead;
104 * This allows us to export our symbols to other modules.
106 * Note: some of the subsystems depend on each other, so the
107 * order in which they're started is important. The order of
108 * importance is:
110 * HAL - spinlocks and IRQL manipulation
111 * ntoskrnl - DPC and workitem threads, object waiting
112 * windrv - driver/device registration
114 * The HAL should also be the last thing shut down, since
115 * the ntoskrnl subsystem will use spinlocks right up until
116 * the DPC and workitem threads are terminated.
119 static int
120 ndis_modevent(module_t mod, int cmd, void *arg)
122 int error = 0;
123 image_patch_table *patch;
125 switch (cmd) {
126 case MOD_LOAD:
127 /* Initialize subsystems */
128 hal_libinit();
129 ntoskrnl_libinit();
130 windrv_libinit();
131 ndis_libinit();
132 usbd_libinit();
134 patch = kernndis_functbl;
135 while (patch->ipt_func != NULL) {
136 windrv_wrap((funcptr)patch->ipt_func,
137 (funcptr *)&patch->ipt_wrap,
138 patch->ipt_argcnt, patch->ipt_ftype);
139 patch++;
142 TAILQ_INIT(&ndis_devhead);
143 break;
144 case MOD_SHUTDOWN:
145 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
146 /* Shut down subsystems */
147 ndis_libfini();
148 usbd_libfini();
149 windrv_libfini();
150 ntoskrnl_libfini();
151 hal_libfini();
153 patch = kernndis_functbl;
154 while (patch->ipt_func != NULL) {
155 windrv_unwrap(patch->ipt_wrap);
156 patch++;
159 break;
160 case MOD_UNLOAD:
161 /* Shut down subsystems */
162 ndis_libfini();
163 usbd_libfini();
164 windrv_libfini();
165 ntoskrnl_libfini();
166 hal_libfini();
168 patch = kernndis_functbl;
169 while (patch->ipt_func != NULL) {
170 windrv_unwrap(patch->ipt_wrap);
171 patch++;
174 break;
175 default:
176 error = EINVAL;
177 break;
180 return (error);
182 DEV_MODULE(ndis, ndis_modevent, NULL);
183 MODULE_VERSION(ndis, 1);
185 static void
186 ndis_sendrsrcavail_func(ndis_handle adapter)
190 static void
191 ndis_status_func(ndis_handle adapter, ndis_status status, void *sbuf,
192 uint32_t slen)
194 ndis_miniport_block *block;
195 struct ndis_softc *sc;
196 struct ifnet *ifp;
198 block = adapter;
199 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
200 ifp = sc->ifp;
201 if (ifp->if_flags & IFF_DEBUG)
202 device_printf(sc->ndis_dev, "status: %x\n", status);
205 static void
206 ndis_statusdone_func(ndis_handle adapter)
208 ndis_miniport_block *block;
209 struct ndis_softc *sc;
210 struct ifnet *ifp;
212 block = adapter;
213 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
214 ifp = sc->ifp;
215 if (ifp->if_flags & IFF_DEBUG)
216 device_printf(sc->ndis_dev, "status complete\n");
219 static void
220 ndis_setdone_func(ndis_handle adapter, ndis_status status)
222 ndis_miniport_block *block;
223 block = adapter;
225 block->nmb_setstat = status;
226 KeSetEvent(&block->nmb_setevent, IO_NO_INCREMENT, FALSE);
229 static void
230 ndis_getdone_func(ndis_handle adapter, ndis_status status)
232 ndis_miniport_block *block;
233 block = adapter;
235 block->nmb_getstat = status;
236 KeSetEvent(&block->nmb_getevent, IO_NO_INCREMENT, FALSE);
239 static void
240 ndis_resetdone_func(ndis_handle adapter, ndis_status status,
241 uint8_t addressingreset)
243 ndis_miniport_block *block;
244 struct ndis_softc *sc;
245 struct ifnet *ifp;
247 block = adapter;
248 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
249 ifp = sc->ifp;
251 if (ifp->if_flags & IFF_DEBUG)
252 device_printf(sc->ndis_dev, "reset done...\n");
253 KeSetEvent(&block->nmb_resetevent, IO_NO_INCREMENT, FALSE);
257 ndis_create_sysctls(void *arg)
259 struct ndis_softc *sc;
260 ndis_cfg *vals;
261 char buf[256];
262 struct sysctl_oid *oidp;
263 struct sysctl_ctx_entry *e;
265 if (arg == NULL)
266 return (EINVAL);
268 sc = arg;
269 vals = sc->ndis_regvals;
271 TAILQ_INIT(&sc->ndis_cfglist_head);
273 /* Add the driver-specific registry keys. */
275 while(1) {
276 if (vals->nc_cfgkey == NULL)
277 break;
279 if (vals->nc_idx != sc->ndis_devidx) {
280 vals++;
281 continue;
284 /* See if we already have a sysctl with this name */
286 oidp = NULL;
287 TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
288 oidp = e->entry;
289 if (strcasecmp(oidp->oid_name, vals->nc_cfgkey) == 0)
290 break;
291 oidp = NULL;
294 if (oidp != NULL) {
295 vals++;
296 continue;
299 ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
300 vals->nc_val, CTLFLAG_RW);
301 vals++;
304 /* Now add a couple of builtin keys. */
307 * Environment can be either Windows (0) or WindowsNT (1).
308 * We qualify as the latter.
310 ndis_add_sysctl(sc, "Environment",
311 "Windows environment", "1", CTLFLAG_RD);
313 /* NDIS version should be 5.1. */
314 ndis_add_sysctl(sc, "NdisVersion",
315 "NDIS API Version", "0x00050001", CTLFLAG_RD);
318 * Some miniport drivers rely on the existence of the SlotNumber,
319 * NetCfgInstanceId and DriverDesc keys.
321 ndis_add_sysctl(sc, "SlotNumber", "Slot Number", "01", CTLFLAG_RD);
322 ndis_add_sysctl(sc, "NetCfgInstanceId", "NetCfgInstanceId",
323 "{12345678-1234-5678-CAFE0-123456789ABC}", CTLFLAG_RD);
324 ndis_add_sysctl(sc, "DriverDesc", "Driver Description",
325 "NDIS Network Adapter", CTLFLAG_RD);
327 /* Bus type (PCI, PCMCIA, etc...) */
328 ksprintf(buf, "%d", (int)sc->ndis_iftype);
329 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
331 if (sc->ndis_res_io != NULL) {
332 ksprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
333 ndis_add_sysctl(sc, "IOBaseAddress",
334 "Base I/O Address", buf, CTLFLAG_RD);
337 if (sc->ndis_irq != NULL) {
338 ksprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
339 ndis_add_sysctl(sc, "InterruptNumber",
340 "Interrupt Number", buf, CTLFLAG_RD);
343 return (0);
347 ndis_add_sysctl(void *arg, char *key, char *desc, char *val, int flag)
349 struct ndis_softc *sc;
350 struct ndis_cfglist *cfg;
351 char descstr[256];
353 sc = arg;
355 cfg = kmalloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_WAITOK|M_ZERO);
356 cfg->ndis_cfg.nc_cfgkey = kstrdup(key, M_DEVBUF);
357 if (desc == NULL) {
358 ksnprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
359 cfg->ndis_cfg.nc_cfgdesc = kstrdup(descstr, M_DEVBUF);
360 } else
361 cfg->ndis_cfg.nc_cfgdesc = kstrdup(desc, M_DEVBUF);
362 strcpy(cfg->ndis_cfg.nc_val, val);
364 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
366 cfg->ndis_oid =
367 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
368 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
369 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
370 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
371 cfg->ndis_cfg.nc_cfgdesc);
373 return (0);
377 * Somewhere, somebody decided "hey, let's automatically create
378 * a sysctl tree for each device instance as it's created -- it'll
379 * make life so much easier!" Lies. Why must they turn the kernel
380 * into a house of lies?
384 ndis_flush_sysctls(void *arg)
386 struct ndis_softc *sc;
387 struct ndis_cfglist *cfg;
388 struct sysctl_ctx_list *clist;
390 sc = arg;
392 clist = device_get_sysctl_ctx(sc->ndis_dev);
394 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
395 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
396 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
397 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
398 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
399 kfree(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
400 kfree(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
401 kfree(cfg, M_DEVBUF);
404 return (0);
407 void *
408 ndis_get_routine_address(struct image_patch_table *functbl, char *name)
410 int i;
412 for (i = 0; functbl[i].ipt_name != NULL; i++)
413 if (strcmp(name, functbl[i].ipt_name) == 0)
414 return (functbl[i].ipt_wrap);
415 return (NULL);
418 static void
419 ndis_return(device_object *dobj, void *arg)
421 ndis_miniport_block *block;
422 ndis_miniport_characteristics *ch;
423 ndis_return_handler returnfunc;
424 ndis_handle adapter;
425 ndis_packet *p;
426 uint8_t irql;
427 list_entry *l;
429 block = arg;
430 ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
432 p = arg;
433 adapter = block->nmb_miniportadapterctx;
435 if (adapter == NULL)
436 return;
438 returnfunc = ch->nmc_return_packet_func;
440 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
441 while (!IsListEmpty(&block->nmb_returnlist)) {
442 l = RemoveHeadList((&block->nmb_returnlist));
443 p = CONTAINING_RECORD(l, ndis_packet, np_list);
444 InitializeListHead((&p->np_list));
445 KeReleaseSpinLock(&block->nmb_returnlock, irql);
446 MSCALL2(returnfunc, adapter, p);
447 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
449 KeReleaseSpinLock(&block->nmb_returnlock, irql);
452 static void
453 ndis_reference_packet(void *arg)
455 ndis_packet *p;
457 if (arg == NULL)
458 return;
460 p = arg;
462 /* Increment refcount. */
463 atomic_add_int(&p->np_refcnt, 1);
466 void
467 ndis_return_packet(void *arg)
469 ndis_packet *p;
470 ndis_miniport_block *block;
472 if (arg == NULL)
473 return;
475 p = arg;
477 /* Release packet when refcount hits zero, otherwise return. */
478 if (atomic_fetchadd_int(&p->np_refcnt, -1) > 1)
479 return;
481 block = ((struct ndis_softc *)p->np_softc)->ndis_block;
483 KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
484 InitializeListHead((&p->np_list));
485 InsertHeadList((&block->nmb_returnlist), (&p->np_list));
486 KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
488 IoQueueWorkItem(block->nmb_returnitem,
489 (io_workitem_func)kernndis_functbl[7].ipt_wrap,
490 WORKQUEUE_CRITICAL, block);
493 void
494 ndis_free_bufs(ndis_buffer *b0)
496 ndis_buffer *next;
498 if (b0 == NULL)
499 return;
501 while(b0 != NULL) {
502 next = b0->mdl_next;
503 IoFreeMdl(b0);
504 b0 = next;
508 void
509 ndis_free_packet(ndis_packet *p)
511 if (p == NULL)
512 return;
514 ndis_free_bufs(p->np_private.npp_head);
515 NdisFreePacket(p);
519 ndis_convert_res(void *arg)
521 struct ndis_softc *sc;
522 ndis_resource_list *rl = NULL;
523 cm_partial_resource_desc *prd = NULL;
524 ndis_miniport_block *block;
525 device_t dev;
526 struct resource_list *brl;
527 struct resource_list_entry *brle;
528 struct resource_list brl_rev;
529 struct resource_list_entry *n;
530 int error = 0;
532 sc = arg;
533 block = sc->ndis_block;
534 dev = sc->ndis_dev;
536 SLIST_INIT(&brl_rev);
538 rl = kmalloc(sizeof(ndis_resource_list) +
539 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
540 M_DEVBUF, M_WAITOK|M_NULLOK|M_ZERO);
542 if (rl == NULL)
543 return (ENOMEM);
545 rl->cprl_version = 5;
546 rl->cprl_revision = 1;
547 rl->cprl_count = sc->ndis_rescnt;
548 prd = rl->cprl_partial_descs;
550 brl = BUS_GET_RESOURCE_LIST(dev, dev);
552 if (brl != NULL) {
555 * We have a small problem. Some PCI devices have
556 * multiple I/O ranges. Windows orders them starting
557 * from lowest numbered BAR to highest. We discover
558 * them in that order too, but insert them into a singly
559 * linked list head first, which means when time comes
560 * to traverse the list, we enumerate them in reverse
561 * order. This screws up some drivers which expect the
562 * BARs to be in ascending order so that they can choose
563 * the "first" one as their register space. Unfortunately,
564 * in order to fix this, we have to create our own
565 * temporary list with the entries in reverse order.
568 SLIST_FOREACH(brle, brl, link) {
569 n = kmalloc(sizeof(struct resource_list_entry),
570 M_TEMP, M_WAITOK|M_NULLOK);
571 if (n == NULL) {
572 error = ENOMEM;
573 goto bad;
575 bcopy((char *)brle, (char *)n,
576 sizeof(struct resource_list_entry));
577 SLIST_INSERT_HEAD(&brl_rev, n, link);
580 SLIST_FOREACH(brle, &brl_rev, link) {
581 switch (brle->type) {
582 case SYS_RES_IOPORT:
583 prd->cprd_type = CmResourceTypePort;
584 prd->cprd_flags = CM_RESOURCE_PORT_IO;
585 prd->cprd_sharedisp =
586 CmResourceShareDeviceExclusive;
587 prd->u.cprd_port.cprd_start.np_quad =
588 brle->start;
589 prd->u.cprd_port.cprd_len = brle->count;
590 break;
591 case SYS_RES_MEMORY:
592 prd->cprd_type = CmResourceTypeMemory;
593 prd->cprd_flags =
594 CM_RESOURCE_MEMORY_READ_WRITE;
595 prd->cprd_sharedisp =
596 CmResourceShareDeviceExclusive;
597 prd->u.cprd_mem.cprd_start.np_quad =
598 brle->start;
599 prd->u.cprd_mem.cprd_len = brle->count;
600 break;
601 case SYS_RES_IRQ:
602 prd->cprd_type = CmResourceTypeInterrupt;
603 prd->cprd_flags = 0;
605 * Always mark interrupt resources as
606 * shared, since in our implementation,
607 * they will be.
609 prd->cprd_sharedisp =
610 CmResourceShareShared;
611 prd->u.cprd_intr.cprd_level = brle->start;
612 prd->u.cprd_intr.cprd_vector = brle->start;
613 prd->u.cprd_intr.cprd_affinity = 0;
614 break;
615 default:
616 break;
618 prd++;
622 block->nmb_rlist = rl;
624 bad:
626 while (!SLIST_EMPTY(&brl_rev)) {
627 n = SLIST_FIRST(&brl_rev);
628 SLIST_REMOVE_HEAD(&brl_rev, link);
629 kfree (n, M_TEMP);
632 return (error);
636 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
637 * packet, it will hand it to us in the form of an ndis_packet,
638 * which we need to convert to an mbuf that is then handed off
639 * to the stack. Note: we configure the mbuf list so that it uses
640 * the memory regions specified by the ndis_buffer structures in
641 * the ndis_packet as external storage. In most cases, this will
642 * point to a memory region allocated by the driver (either by
643 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
644 * the driver to handle kfree()ing this region for is, so we set up
645 * a dummy no-op free handler for it.
649 ndis_ptom(struct mbuf **m0, ndis_packet *p)
651 struct mbuf *m = NULL, *prev = NULL;
652 ndis_buffer *buf;
653 ndis_packet_private *priv;
654 uint32_t totlen = 0;
655 struct ifnet *ifp;
656 struct ether_header *eh;
657 int diff;
659 if (p == NULL || m0 == NULL)
660 return (EINVAL);
662 priv = &p->np_private;
663 buf = priv->npp_head;
664 p->np_refcnt = 0;
666 for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
667 if (buf == priv->npp_head) {
668 /* XXX swildner: why not MT_HEADER? (see FreeBSD) */
669 MGETHDR(m, M_NOWAIT, MT_DATA);
670 } else {
671 MGET(m, M_NOWAIT, MT_DATA);
673 if (m == NULL) {
674 m_freem(*m0);
675 *m0 = NULL;
676 return (ENOBUFS);
678 m->m_len = MmGetMdlByteCount(buf);
679 m->m_data = MmGetMdlVirtualAddress(buf);
680 m_extadd(m, m->m_data, m->m_len, ndis_reference_packet,
681 ndis_return_packet, p);
682 // p->np_refcnt++;
684 totlen += m->m_len;
685 if (m->m_flags & M_PKTHDR)
686 *m0 = m;
687 else
688 prev->m_next = m;
689 prev = m;
693 * This is a hack to deal with the Marvell 8335 driver
694 * which, when associated with an AP in WPA-PSK mode,
695 * seems to overpad its frames by 8 bytes. I don't know
696 * that the extra 8 bytes are for, and they're not there
697 * in open mode, so for now clamp the frame size at 1514
698 * until I can figure out how to deal with this properly,
699 * otherwise if_ethersubr() will spank us by discarding
700 * the 'oversize' frames.
703 eh = mtod((*m0), struct ether_header *);
704 ifp = ((struct ndis_softc *)p->np_softc)->ifp;
705 if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
706 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
707 totlen -= diff;
708 m->m_len -= diff;
710 (*m0)->m_pkthdr.len = totlen;
712 return (0);
716 * Create an NDIS packet from an mbuf chain.
717 * This is used mainly when transmitting packets, where we need
718 * to turn an mbuf off an interface's send queue and transform it
719 * into an NDIS packet which will be fed into the NDIS driver's
720 * send routine.
722 * NDIS packets consist of two parts: an ndis_packet structure,
723 * which is vaguely analagous to the pkthdr portion of an mbuf,
724 * and one or more ndis_buffer structures, which define the
725 * actual memory segments in which the packet data resides.
726 * We need to allocate one ndis_buffer for each mbuf in a chain,
727 * plus one ndis_packet as the header.
731 ndis_mtop(struct mbuf *m0, ndis_packet **p)
733 struct mbuf *m;
734 ndis_buffer *buf = NULL, *prev = NULL;
735 ndis_packet_private *priv;
737 if (p == NULL || *p == NULL || m0 == NULL)
738 return (EINVAL);
740 priv = &(*p)->np_private;
741 priv->npp_totlen = m0->m_pkthdr.len;
743 for (m = m0; m != NULL; m = m->m_next) {
744 if (m->m_len == 0)
745 continue;
746 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
747 if (buf == NULL) {
748 ndis_free_packet(*p);
749 *p = NULL;
750 return (ENOMEM);
752 MmBuildMdlForNonPagedPool(buf);
754 if (priv->npp_head == NULL)
755 priv->npp_head = buf;
756 else
757 prev->mdl_next = buf;
758 prev = buf;
761 priv->npp_tail = buf;
763 return (0);
767 ndis_get_supported_oids(void *arg, ndis_oid **oids, int *oidcnt)
769 int len, rval;
770 ndis_oid *o;
772 if (arg == NULL || oids == NULL || oidcnt == NULL)
773 return (EINVAL);
774 len = 0;
775 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
777 o = kmalloc(len, M_DEVBUF, M_WAITOK);
779 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
781 if (rval) {
782 kfree(o, M_DEVBUF);
783 return (rval);
786 *oids = o;
787 *oidcnt = len / 4;
789 return (0);
793 ndis_set_info(void *arg, ndis_oid oid, void *buf, int *buflen)
795 struct ndis_softc *sc;
796 ndis_status rval;
797 ndis_handle adapter;
798 ndis_setinfo_handler setfunc;
799 uint32_t byteswritten = 0, bytesneeded = 0;
800 uint8_t irql;
801 uint64_t duetime;
804 * According to the NDIS spec, MiniportQueryInformation()
805 * and MiniportSetInformation() requests are handled serially:
806 * once one request has been issued, we must wait for it to
807 * finish before allowing another request to proceed.
810 sc = arg;
812 KeResetEvent(&sc->ndis_block->nmb_setevent);
814 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
816 if (sc->ndis_block->nmb_pendingreq != NULL) {
817 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
818 panic("ndis_set_info() called while other request pending");
819 } else
820 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
822 setfunc = sc->ndis_chars->nmc_setinfo_func;
823 adapter = sc->ndis_block->nmb_miniportadapterctx;
825 if (adapter == NULL || setfunc == NULL ||
826 sc->ndis_block->nmb_devicectx == NULL) {
827 sc->ndis_block->nmb_pendingreq = NULL;
828 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
829 return (ENXIO);
832 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
833 &byteswritten, &bytesneeded);
835 sc->ndis_block->nmb_pendingreq = NULL;
837 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
839 if (rval == NDIS_STATUS_PENDING) {
840 /* Wait up to 5 seconds. */
841 duetime = (5 * 1000000) * -10;
842 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
843 0, 0, FALSE, &duetime);
844 rval = sc->ndis_block->nmb_setstat;
847 if (byteswritten)
848 *buflen = byteswritten;
849 if (bytesneeded)
850 *buflen = bytesneeded;
852 if (rval == NDIS_STATUS_INVALID_LENGTH)
853 return (ENOSPC);
855 if (rval == NDIS_STATUS_INVALID_OID)
856 return (EINVAL);
858 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
859 rval == NDIS_STATUS_NOT_ACCEPTED)
860 return (ENOTSUP);
862 if (rval != NDIS_STATUS_SUCCESS)
863 return (ENODEV);
865 return (0);
868 typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
871 ndis_send_packets(void *arg, ndis_packet **packets, int cnt)
873 struct ndis_softc *sc;
874 ndis_handle adapter;
875 ndis_sendmulti_handler sendfunc;
876 ndis_senddone_func senddonefunc;
877 int i;
878 ndis_packet *p;
879 uint8_t irql = 0;
881 sc = arg;
882 adapter = sc->ndis_block->nmb_miniportadapterctx;
883 if (adapter == NULL)
884 return (ENXIO);
885 sendfunc = sc->ndis_chars->nmc_sendmulti_func;
886 senddonefunc = sc->ndis_block->nmb_senddone_func;
888 if (NDIS_SERIALIZED(sc->ndis_block))
889 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
891 MSCALL3(sendfunc, adapter, packets, cnt);
893 for (i = 0; i < cnt; i++) {
894 p = packets[i];
896 * Either the driver already handed the packet to
897 * ndis_txeof() due to a failure, or it wants to keep
898 * it and release it asynchronously later. Skip to the
899 * next one.
901 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
902 continue;
903 MSCALL3(senddonefunc, sc->ndis_block, p, p->np_oob.npo_status);
906 if (NDIS_SERIALIZED(sc->ndis_block))
907 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
909 return(0);
913 ndis_send_packet(void *arg, ndis_packet *packet)
915 struct ndis_softc *sc;
916 ndis_handle adapter;
917 ndis_status status;
918 ndis_sendsingle_handler sendfunc;
919 ndis_senddone_func senddonefunc;
920 uint8_t irql = 0;
922 sc = arg;
923 adapter = sc->ndis_block->nmb_miniportadapterctx;
924 if (adapter == NULL)
925 return (ENXIO);
926 sendfunc = sc->ndis_chars->nmc_sendsingle_func;
927 senddonefunc = sc->ndis_block->nmb_senddone_func;
929 if (NDIS_SERIALIZED(sc->ndis_block))
930 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
931 status = MSCALL3(sendfunc, adapter, packet,
932 packet->np_private.npp_flags);
934 if (status == NDIS_STATUS_PENDING) {
935 if (NDIS_SERIALIZED(sc->ndis_block))
936 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
937 return (0);
940 MSCALL3(senddonefunc, sc->ndis_block, packet, status);
942 if (NDIS_SERIALIZED(sc->ndis_block))
943 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
945 return (0);
949 ndis_init_dma(void *arg)
951 struct ndis_softc *sc;
952 int i, error;
954 sc = arg;
956 sc->ndis_tmaps = kmalloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
957 M_DEVBUF, M_WAITOK|M_ZERO);
959 for (i = 0; i < sc->ndis_maxpkts; i++) {
960 error = bus_dmamap_create(sc->ndis_ttag, 0,
961 &sc->ndis_tmaps[i]);
962 if (error) {
963 kfree(sc->ndis_tmaps, M_DEVBUF);
964 return (ENODEV);
968 return (0);
972 ndis_destroy_dma(void *arg)
974 struct ndis_softc *sc;
975 struct mbuf *m;
976 ndis_packet *p = NULL;
977 int i;
979 sc = arg;
981 for (i = 0; i < sc->ndis_maxpkts; i++) {
982 if (sc->ndis_txarray[i] != NULL) {
983 p = sc->ndis_txarray[i];
984 m = (struct mbuf *)p->np_rsvd[1];
985 if (m != NULL)
986 m_freem(m);
987 ndis_free_packet(sc->ndis_txarray[i]);
989 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
992 kfree(sc->ndis_tmaps, M_DEVBUF);
994 bus_dma_tag_destroy(sc->ndis_ttag);
996 return (0);
1000 ndis_reset_nic(void *arg)
1002 struct ndis_softc *sc;
1003 ndis_handle adapter;
1004 ndis_reset_handler resetfunc;
1005 uint8_t addressing_reset;
1006 int rval;
1007 uint8_t irql = 0;
1009 sc = arg;
1011 NDIS_LOCK(sc);
1012 adapter = sc->ndis_block->nmb_miniportadapterctx;
1013 resetfunc = sc->ndis_chars->nmc_reset_func;
1015 if (adapter == NULL || resetfunc == NULL ||
1016 sc->ndis_block->nmb_devicectx == NULL) {
1017 NDIS_UNLOCK(sc);
1018 return(EIO);
1021 NDIS_UNLOCK(sc);
1023 KeResetEvent(&sc->ndis_block->nmb_resetevent);
1025 if (NDIS_SERIALIZED(sc->ndis_block))
1026 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1028 rval = MSCALL2(resetfunc, &addressing_reset, adapter);
1030 if (NDIS_SERIALIZED(sc->ndis_block))
1031 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1033 if (rval == NDIS_STATUS_PENDING)
1034 KeWaitForSingleObject(&sc->ndis_block->nmb_resetevent,
1035 0, 0, FALSE, NULL);
1037 return (0);
1041 ndis_halt_nic(void *arg)
1043 struct ndis_softc *sc;
1044 ndis_handle adapter;
1045 ndis_halt_handler haltfunc;
1046 ndis_miniport_block *block;
1047 int empty = 0;
1048 uint8_t irql;
1050 sc = arg;
1051 block = sc->ndis_block;
1053 if (!cold)
1054 KeFlushQueuedDpcs();
1057 * Wait for all packets to be returned.
1060 while (1) {
1061 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
1062 empty = IsListEmpty(&block->nmb_returnlist);
1063 KeReleaseSpinLock(&block->nmb_returnlock, irql);
1064 if (empty)
1065 break;
1066 NdisMSleep(1000);
1069 NDIS_LOCK(sc);
1070 adapter = sc->ndis_block->nmb_miniportadapterctx;
1071 if (adapter == NULL) {
1072 NDIS_UNLOCK(sc);
1073 return (EIO);
1076 sc->ndis_block->nmb_devicectx = NULL;
1079 * The adapter context is only valid after the init
1080 * handler has been called, and is invalid once the
1081 * halt handler has been called.
1084 haltfunc = sc->ndis_chars->nmc_halt_func;
1085 NDIS_UNLOCK(sc);
1087 MSCALL1(haltfunc, adapter);
1089 NDIS_LOCK(sc);
1090 sc->ndis_block->nmb_miniportadapterctx = NULL;
1091 NDIS_UNLOCK(sc);
1093 return (0);
1097 ndis_shutdown_nic(void *arg)
1099 struct ndis_softc *sc;
1100 ndis_handle adapter;
1101 ndis_shutdown_handler shutdownfunc;
1103 sc = arg;
1104 NDIS_LOCK(sc);
1105 adapter = sc->ndis_block->nmb_miniportadapterctx;
1106 shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1107 NDIS_UNLOCK(sc);
1108 if (adapter == NULL || shutdownfunc == NULL)
1109 return (EIO);
1111 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1112 MSCALL1(shutdownfunc, adapter);
1113 else
1114 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
1116 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1118 return(0);
1122 ndis_pnpevent_nic(void *arg, int type)
1124 device_t dev;
1125 struct ndis_softc *sc;
1126 ndis_handle adapter;
1127 ndis_pnpevent_handler pnpeventfunc;
1129 dev = arg;
1130 sc = device_get_softc(dev);
1131 NDIS_LOCK(sc);
1132 adapter = sc->ndis_block->nmb_miniportadapterctx;
1133 pnpeventfunc = sc->ndis_chars->nmc_pnpevent_handler;
1134 NDIS_UNLOCK(sc);
1135 if (adapter == NULL || pnpeventfunc == NULL)
1136 return (EIO);
1138 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1139 MSCALL4(pnpeventfunc, adapter, type, NULL, 0);
1140 else
1141 MSCALL4(pnpeventfunc, sc->ndis_chars->nmc_rsvd0, type, NULL, 0);
1143 return (0);
1147 ndis_init_nic(void *arg)
1149 struct ndis_softc *sc;
1150 ndis_miniport_block *block;
1151 ndis_init_handler initfunc;
1152 ndis_status status, openstatus = 0;
1153 ndis_medium mediumarray[NdisMediumMax];
1154 uint32_t chosenmedium, i;
1156 if (arg == NULL)
1157 return (EINVAL);
1159 sc = arg;
1160 NDIS_LOCK(sc);
1161 block = sc->ndis_block;
1162 initfunc = sc->ndis_chars->nmc_init_func;
1163 NDIS_UNLOCK(sc);
1165 sc->ndis_block->nmb_timerlist = NULL;
1167 for (i = 0; i < NdisMediumMax; i++)
1168 mediumarray[i] = i;
1170 status = MSCALL6(initfunc, &openstatus, &chosenmedium,
1171 mediumarray, NdisMediumMax, block, block);
1174 * If the init fails, blow away the other exported routines
1175 * we obtained from the driver so we can't call them later.
1176 * If the init failed, none of these will work.
1178 if (status != NDIS_STATUS_SUCCESS) {
1179 NDIS_LOCK(sc);
1180 sc->ndis_block->nmb_miniportadapterctx = NULL;
1181 NDIS_UNLOCK(sc);
1182 return (ENXIO);
1186 * This may look really goofy, but apparently it is possible
1187 * to halt a miniport too soon after it's been initialized.
1188 * After MiniportInitialize() finishes, pause for 1 second
1189 * to give the chip a chance to handle any short-lived timers
1190 * that were set in motion. If we call MiniportHalt() too soon,
1191 * some of the timers may not be cancelled, because the driver
1192 * expects them to fire before the halt is called.
1195 tsleep(arg, 0, "ndwait", hz);
1197 NDIS_LOCK(sc);
1198 sc->ndis_block->nmb_devicectx = sc;
1199 NDIS_UNLOCK(sc);
1201 return (0);
1204 static void
1205 ndis_intrsetup(kdpc *dpc, device_object *dobj, irp *ip, struct ndis_softc *sc)
1207 ndis_miniport_interrupt *intr;
1209 intr = sc->ndis_block->nmb_interrupt;
1211 /* Sanity check. */
1213 if (intr == NULL)
1214 return;
1216 KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
1217 KeResetEvent(&intr->ni_dpcevt);
1218 if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
1219 intr->ni_dpccnt++;
1220 KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
1224 ndis_get_info(void *arg, ndis_oid oid, void *buf, int *buflen)
1226 struct ndis_softc *sc;
1227 ndis_status rval;
1228 ndis_handle adapter;
1229 ndis_queryinfo_handler queryfunc;
1230 uint32_t byteswritten = 0, bytesneeded = 0;
1231 uint8_t irql;
1232 uint64_t duetime;
1234 sc = arg;
1236 KeResetEvent(&sc->ndis_block->nmb_getevent);
1238 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1240 if (sc->ndis_block->nmb_pendingreq != NULL) {
1241 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1242 panic("ndis_get_info() called while other request pending");
1243 } else
1244 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
1246 queryfunc = sc->ndis_chars->nmc_queryinfo_func;
1247 adapter = sc->ndis_block->nmb_miniportadapterctx;
1249 if (adapter == NULL || queryfunc == NULL ||
1250 sc->ndis_block->nmb_devicectx == NULL) {
1251 sc->ndis_block->nmb_pendingreq = NULL;
1252 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1253 return (ENXIO);
1256 rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
1257 &byteswritten, &bytesneeded);
1259 sc->ndis_block->nmb_pendingreq = NULL;
1261 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1263 /* Wait for requests that block. */
1265 if (rval == NDIS_STATUS_PENDING) {
1266 /* Wait up to 5 seconds. */
1267 duetime = (5 * 1000000) * -10;
1268 KeWaitForSingleObject(&sc->ndis_block->nmb_getevent,
1269 0, 0, FALSE, &duetime);
1270 rval = sc->ndis_block->nmb_getstat;
1273 if (byteswritten)
1274 *buflen = byteswritten;
1275 if (bytesneeded)
1276 *buflen = bytesneeded;
1278 if (rval == NDIS_STATUS_INVALID_LENGTH ||
1279 rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1280 return (ENOSPC);
1282 if (rval == NDIS_STATUS_INVALID_OID)
1283 return (EINVAL);
1285 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1286 rval == NDIS_STATUS_NOT_ACCEPTED)
1287 return (ENOTSUP);
1289 if (rval != NDIS_STATUS_SUCCESS)
1290 return (ENODEV);
1292 return (0);
1295 uint32_t
1296 NdisAddDevice(driver_object *drv, device_object *pdo)
1298 device_object *fdo;
1299 ndis_miniport_block *block;
1300 struct ndis_softc *sc;
1301 uint32_t status;
1302 int error;
1304 sc = device_get_softc(pdo->do_devext);
1306 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
1307 error = bus_setup_intr(sc->ndis_dev, sc->ndis_irq,
1308 INTR_MPSAFE,
1309 ntoskrnl_intr, sc, &sc->ndis_intrhand, NULL);
1310 if (error)
1311 return (NDIS_STATUS_FAILURE);
1314 status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1315 FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
1317 if (status != STATUS_SUCCESS)
1318 return (status);
1320 block = fdo->do_devext;
1322 block->nmb_filterdbs.nf_ethdb = block;
1323 block->nmb_deviceobj = fdo;
1324 block->nmb_physdeviceobj = pdo;
1325 block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
1326 KeInitializeSpinLock(&block->nmb_lock);
1327 KeInitializeSpinLock(&block->nmb_returnlock);
1328 KeInitializeEvent(&block->nmb_getevent, EVENT_TYPE_NOTIFY, TRUE);
1329 KeInitializeEvent(&block->nmb_setevent, EVENT_TYPE_NOTIFY, TRUE);
1330 KeInitializeEvent(&block->nmb_resetevent, EVENT_TYPE_NOTIFY, TRUE);
1331 InitializeListHead(&block->nmb_parmlist);
1332 InitializeListHead(&block->nmb_returnlist);
1333 block->nmb_returnitem = IoAllocateWorkItem(fdo);
1336 * Stash pointers to the miniport block and miniport
1337 * characteristics info in the if_ndis softc so the
1338 * UNIX wrapper driver can get to them later.
1340 sc->ndis_block = block;
1341 sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1);
1344 * If the driver has a MiniportTransferData() function,
1345 * we should allocate a private RX packet pool.
1348 if (sc->ndis_chars->nmc_transferdata_func != NULL) {
1349 NdisAllocatePacketPool(&status, &block->nmb_rxpool,
1350 32, PROTOCOL_RESERVED_SIZE_IN_PACKET);
1351 if (status != NDIS_STATUS_SUCCESS) {
1352 IoDetachDevice(block->nmb_nextdeviceobj);
1353 IoDeleteDevice(fdo);
1354 return (status);
1356 InitializeListHead((&block->nmb_packetlist));
1359 /* Give interrupt handling priority over timers. */
1360 IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1361 KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
1363 /* Finish up BSD-specific setup. */
1365 block->nmb_signature = (void *)0xcafebabe;
1366 block->nmb_status_func = kernndis_functbl[0].ipt_wrap;
1367 block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap;
1368 block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap;
1369 block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap;
1370 block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap;
1371 block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap;
1372 block->nmb_pendingreq = NULL;
1374 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
1376 return (STATUS_SUCCESS);
1380 ndis_unload_driver(void *arg)
1382 struct ndis_softc *sc;
1383 device_object *fdo;
1385 sc = arg;
1387 if (sc->ndis_intrhand)
1388 bus_teardown_intr(sc->ndis_dev,
1389 sc->ndis_irq, sc->ndis_intrhand);
1391 if (sc->ndis_block->nmb_rlist != NULL)
1392 kfree(sc->ndis_block->nmb_rlist, M_DEVBUF);
1394 ndis_flush_sysctls(sc);
1396 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1398 if (sc->ndis_chars->nmc_transferdata_func != NULL)
1399 NdisFreePacketPool(sc->ndis_block->nmb_rxpool);
1400 fdo = sc->ndis_block->nmb_deviceobj;
1401 IoFreeWorkItem(sc->ndis_block->nmb_returnitem);
1402 IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj);
1403 IoDeleteDevice(fdo);
1405 return (0);