summaryrefslogtreecommitdiffstats
path: root/sys/compat/ndis/kern_ndis.c
diff options
context:
space:
mode:
authorwpaul <wpaul@FreeBSD.org>2005-10-10 16:46:39 +0000
committerwpaul <wpaul@FreeBSD.org>2005-10-10 16:46:39 +0000
commitef07dbe57fe381dac96f0b1470d99bd3a2ccd3aa (patch)
tree899e14a42961670f9a34b64d8df4d8a3abe94542 /sys/compat/ndis/kern_ndis.c
parent01a20da53d2f99b5654208b24ad8d6ebfe324da9 (diff)
downloadFreeBSD-src-ef07dbe57fe381dac96f0b1470d99bd3a2ccd3aa.zip
FreeBSD-src-ef07dbe57fe381dac96f0b1470d99bd3a2ccd3aa.tar.gz
This commit makes a big round of updates and fixes many, many things.
First and most importantly, I threw out the thread priority-twiddling implementation of KeRaiseIrql()/KeLowerIrq()/KeGetCurrentIrql() in favor of a new scheme that uses sleep mutexes. The old scheme was really very naughty and sought to provide the same behavior as Windows spinlocks (i.e. blocking pre-emption) but in a way that wouldn't raise the ire of WITNESS. The new scheme represents 'DISPATCH_LEVEL' as the acquisition of a per-cpu sleep mutex. If a thread on cpu0 acquires the 'dispatcher mutex,' it will block any other thread on the same processor that tries to acquire it, in effect only allowing one thread on the processor to be at 'DISPATCH_LEVEL' at any given time. It can then do the 'atomic sit and spin' routine on the spinlock variable itself. If a thread on cpu1 wants to acquire the same spinlock, it acquires the 'dispatcher mutex' for cpu1 and then it too does an atomic sit and spin to try acquiring the spinlock. Unlike real spinlocks, this does not disable pre-emption of all threads on the CPU, but it does put any threads involved with the NDISulator to sleep, which is just as good for our purposes. This means I can now play nice with WITNESS, and I can safely do things like call malloc() when I'm at 'DISPATCH_LEVEL,' which you're allowed to do in Windows. Next, I completely re-wrote most of the event/timer/mutex handling and wait code. KeWaitForSingleObject() and KeWaitForMultipleObjects() have been re-written to use condition variables instead of msleep(). This allows us to use the Windows convention whereby thread A can tell thread B "wake up with a boosted priority." (With msleep(), you instead have thread B saying "when I get woken up, I'll use this priority here," and thread A can't tell it to do otherwise.) The new KeWaitForMultipleObjects() has been better tested and better duplicates the semantics of its Windows counterpart. I also overhauled the IoQueueWorkItem() API and underlying code. Like KeInsertQueueDpc(), IoQueueWorkItem() must insure that the same work item isn't put on the queue twice. ExQueueWorkItem(), which in my implementation is built on top of IoQueueWorkItem(), was also modified to perform a similar test. I renamed the doubly-linked list macros to give them the same names as their Windows counterparts and fixed RemoveListTail() and RemoveListHead() so they properly return the removed item. I also corrected the list handling code in ntoskrnl_dpc_thread() and ntoskrnl_workitem_thread(). I realized that the original logic did not correctly handle the case where a DPC callout tries to queue up another DPC. It works correctly now. I implemented IoConnectInterrupt() and IoDisconnectInterrupt() and modified NdisMRegisterInterrupt() and NdisMDisconnectInterrupt() to use them. I also tried to duplicate the interrupt handling scheme used in Windows. The interrupt handling is now internal to ndis.ko, and the ndis_intr() function has been removed from if_ndis.c. (In the USB case, interrupt handling isn't needed in if_ndis.c anyway.) NdisMSleep() has been rewritten to use a KeWaitForSingleObject() and a KeTimer, which is how it works in Windows. (This is mainly to insure that the NDISulator uses the KeTimer API so I can spot any problems with it that may arise.) KeCancelTimer() has been changed so that it only cancels timers, and does not attempt to cancel a DPC if the timer managed to fire and queue one up before KeCancelTimer() was called. The Windows DDK documentation seems to imply that KeCantelTimer() will also call KeRemoveQueueDpc() if necessary, but it really doesn't. The KeTimer implementation has been rewritten to use the callout API directly instead of timeout()/untimeout(). I still cheat a little in that I have to manage my own small callout timer wheel, but the timer code works more smoothly now. I discovered a race condition using timeout()/untimeout() with periodic timers where untimeout() fails to actually cancel a timer. I don't quite understand where the race is, using callout_init()/callout_reset()/callout_stop() directly seems to fix it. I also discovered and fixed a bug in winx32_wrap.S related to translating _stdcall calls. There are a couple of routines (i.e. the 64-bit arithmetic intrinsics in subr_ntoskrnl) that return 64-bit quantities. On the x86 arch, 64-bit values are returned in the %eax and %edx registers. However, it happens that the ctxsw_utow() routine uses %edx as a scratch register, and x86_stdcall_wrap() and x86_stdcall_call() were only preserving %eax before branching to ctxsw_utow(). This means %edx was getting clobbered in some cases. Curiously, the most noticeable effect of this bug is that the driver for the TI AXC110 chipset would constantly drop and reacquire its link for no apparent reason. Both %eax and %edx are preserved on the stack now. The _fastcall and _regparm wrappers already handled everything correctly. I changed if_ndis to use IoAllocateWorkItem() and IoQueueWorkItem() instead of the NdisScheduleWorkItem() API. This is to avoid possible deadlocks with any drivers that use NdisScheduleWorkItem() themselves. The unicode/ansi conversion handling code has been cleaned up. The internal routines have been moved to subr_ntoskrnl and the RtlXXX routines have been exported so that subr_ndis can call them. This removes the incestuous relationship between the two modules regarding this code and fixes the implementation so that it honors the 'maxlen' fields correctly. (Previously it was possible for NdisUnicodeStringToAnsiString() to possibly clobber memory it didn't own, which was causing many mysterious crashes in the Marvell 8335 driver.) The registry handling code (NdisOpen/Close/ReadConfiguration()) has been fixed to allocate memory for all the parameters it hands out to callers and delete whem when NdisCloseConfiguration() is called. (Previously, it would secretly use a single static buffer.) I also substantially updated if_ndis so that the source can now be built on FreeBSD 7, 6 and 5 without any changes. On FreeBSD 5, only WEP support is enabled. On FreeBSD 6 and 7, WPA-PSK support is enabled. The original WPA code has been updated to fit in more cleanly with the net80211 API, and to eleminate the use of magic numbers. The ndis_80211_setstate() routine now sets a default authmode of OPEN and initializes the RTS threshold and fragmentation threshold. The WPA routines were changed so that the authentication mode is always set first, followed by the cipher. Some drivers depend on the operations being performed in this order. I also added passthrough ioctls that allow application code to directly call the MiniportSetInformation()/MiniportQueryInformation() methods via ndis_set_info() and ndis_get_info(). The ndis_linksts() routine also caches the last 4 events signalled by the driver via NdisMIndicateStatus(), and they can be queried by an application via a separate ioctl. This is done to allow wpa_supplicant to directly program the various crypto and key management options in the driver, allowing things like WPA2 support to work. Whew.
Diffstat (limited to 'sys/compat/ndis/kern_ndis.c')
-rw-r--r--sys/compat/ndis/kern_ndis.c142
1 files changed, 72 insertions, 70 deletions
diff --git a/sys/compat/ndis/kern_ndis.c b/sys/compat/ndis/kern_ndis.c
index 573d78f..97f63f7 100644
--- a/sys/compat/ndis/kern_ndis.c
+++ b/sys/compat/ndis/kern_ndis.c
@@ -82,7 +82,7 @@ static void ndis_setdone_func(ndis_handle, ndis_status);
static void ndis_getdone_func(ndis_handle, ndis_status);
static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
static void ndis_sendrsrcavail_func(ndis_handle);
-static void ndis_intrhand(kdpc *, device_object *,
+static void ndis_intrsetup(kdpc *, device_object *,
irp *, struct ndis_softc *);
static void ndis_return(kdpc *, void *, void *, void *);
@@ -93,7 +93,7 @@ static image_patch_table kernndis_functbl[] = {
IMPORT_SFUNC(ndis_getdone_func, 2),
IMPORT_SFUNC(ndis_resetdone_func, 3),
IMPORT_SFUNC(ndis_sendrsrcavail_func, 1),
- IMPORT_SFUNC(ndis_intrhand, 4),
+ IMPORT_SFUNC(ndis_intrsetup, 4),
IMPORT_SFUNC(ndis_return, 1),
{ NULL, NULL, NULL }
@@ -183,34 +183,6 @@ ndis_modevent(module_t mod, int cmd, void *arg)
DEV_MODULE(ndisapi, ndis_modevent, NULL);
MODULE_VERSION(ndisapi, 1);
-int
-ndis_thsuspend(p, m, timo)
- struct proc *p;
- struct mtx *m;
- int timo;
-{
- int error;
-
- if (m != NULL) {
- error = msleep(&p->p_siglist, m,
- curthread->td_priority, "ndissp", timo);
- } else {
- PROC_LOCK(p);
- error = msleep(&p->p_siglist, &p->p_mtx,
- curthread->td_priority|PDROP, "ndissp", timo);
- }
-
- return(error);
-}
-
-void
-ndis_thresume(p)
- struct proc *p;
-{
- wakeup(&p->p_siglist);
- return;
-}
-
static void
ndis_sendrsrcavail_func(adapter)
ndis_handle adapter;
@@ -328,7 +300,6 @@ ndis_create_sysctls(arg)
#endif
/* Add the driver-specific registry keys. */
- vals = sc->ndis_regvals;
while(1) {
if (vals->nc_cfgkey == NULL)
break;
@@ -358,17 +329,8 @@ ndis_create_sysctls(arg)
continue;
}
-#if __FreeBSD_version < 502113
- SYSCTL_ADD_STRING(&sc->ndis_ctx,
- SYSCTL_CHILDREN(sc->ndis_tree),
-#else
- SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
- SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
-#endif
- OID_AUTO, vals->nc_cfgkey,
- CTLFLAG_RW, vals->nc_val,
- sizeof(vals->nc_val),
- vals->nc_cfgdesc);
+ ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
+ vals->nc_val, CTLFLAG_RW);
vals++;
}
@@ -420,8 +382,10 @@ ndis_add_sysctl(arg, key, desc, val, flag)
cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO);
- if (cfg == NULL)
+ if (cfg == NULL) {
+ printf("failed for %s\n", key);
return(ENOMEM);
+ }
cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF);
if (desc == NULL) {
@@ -433,6 +397,7 @@ ndis_add_sysctl(arg, key, desc, val, flag)
TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
+ cfg->ndis_oid =
#if __FreeBSD_version < 502113
SYSCTL_ADD_STRING(&sc->ndis_ctx, SYSCTL_CHILDREN(sc->ndis_tree),
#else
@@ -446,18 +411,34 @@ ndis_add_sysctl(arg, key, desc, val, flag)
return(0);
}
+/*
+ * Somewhere, somebody decided "hey, let's automatically create
+ * a sysctl tree for each device instance as it's created -- it'll
+ * make life so much easier!" Lies. Why must they turn the kernel
+ * into a house of lies?
+ */
+
int
ndis_flush_sysctls(arg)
void *arg;
{
struct ndis_softc *sc;
struct ndis_cfglist *cfg;
+ struct sysctl_ctx_list *clist;
sc = arg;
+#if __FreeBSD_version < 502113
+ clist = &sc->ndis_ctx;
+#else
+ clist = device_get_sysctl_ctx(sc->ndis_dev);
+#endif
+
while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
+ sysctl_ctx_entry_del(clist, cfg->ndis_oid);
+ sysctl_remove_oid(cfg->ndis_oid, 1, 0);
free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
free(cfg, M_DEVBUF);
@@ -488,9 +469,11 @@ ndis_return(dpc, arg, sysarg1, sysarg2)
returnfunc = sc->ndis_chars->nmc_return_packet_func;
- KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
+ if (NDIS_SERIALIZED(sc->ndis_block))
+ KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
MSCALL2(returnfunc, adapter, p);
- KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
+ if (NDIS_SERIALIZED(sc->ndis_block))
+ KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
return;
}
@@ -691,10 +674,13 @@ ndis_ptom(m0, p)
struct mbuf **m0;
ndis_packet *p;
{
- struct mbuf *m, *prev = NULL;
+ struct mbuf *m = NULL, *prev = NULL;
ndis_buffer *buf;
ndis_packet_private *priv;
uint32_t totlen = 0;
+ struct ifnet *ifp;
+ struct ether_header *eh;
+ int diff;
if (p == NULL || m0 == NULL)
return(EINVAL);
@@ -718,6 +704,7 @@ ndis_ptom(m0, p)
MEXTADD(m, m->m_data, m->m_len, ndis_return_packet,
p, 0, EXT_NDIS);
p->np_refcnt++;
+
totlen += m->m_len;
if (m->m_flags & M_PKTHDR)
*m0 = m;
@@ -726,6 +713,24 @@ ndis_ptom(m0, p)
prev = m;
}
+ /*
+ * This is a hack to deal with the Marvell 8335 driver
+ * which, when associated with an AP in WPA-PSK mode,
+ * seems to overpad its frames by 8 bytes. I don't know
+ * that the extra 8 bytes are for, and they're not there
+ * in open mode, so for now clamp the frame size at 1514
+ * until I can figure out how to deal with this properly,
+ * otherwise if_ethersubr() will spank us by discarding
+ * the 'oversize' frames.
+ */
+
+ eh = mtod((*m0), struct ether_header *);
+ ifp = ((struct ndis_softc *)p->np_softc)->ifp;
+ if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
+ diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
+ totlen -= diff;
+ m->m_len -= diff;
+ }
(*m0)->m_pkthdr.len = totlen;
return(0);
@@ -770,6 +775,7 @@ ndis_mtop(m0, p)
*p = NULL;
return(ENOMEM);
}
+ MmBuildMdlForNonPagedPool(buf);
if (priv->npp_head == NULL)
priv->npp_head = buf;
@@ -875,7 +881,6 @@ ndis_set_info(arg, oid, buf, buflen)
rval = sc->ndis_block->nmb_setstat;
}
-
if (byteswritten)
*buflen = byteswritten;
if (bytesneeded)
@@ -1080,7 +1085,7 @@ ndis_reset_nic(arg)
return(0);
}
-#define NDIS_REAP_TIMERS
+#undef NDIS_REAP_TIMERS
int
ndis_halt_nic(arg)
@@ -1112,9 +1117,9 @@ ndis_halt_nic(arg)
n->nmt_nexttimer = NULL;
}
sc->ndis_block->nmb_timerlist = NULL;
+#endif
if (!cold)
KeFlushQueuedDpcs();
-#endif
NDIS_LOCK(sc);
adapter = sc->ndis_block->nmb_miniportadapterctx;
@@ -1123,7 +1128,6 @@ ndis_halt_nic(arg)
return(EIO);
}
- sc->ndis_block->nmb_miniportadapterctx = NULL;
sc->ndis_block->nmb_devicectx = NULL;
/*
@@ -1137,6 +1141,10 @@ ndis_halt_nic(arg)
MSCALL1(haltfunc, adapter);
+ NDIS_LOCK(sc);
+ sc->ndis_block->nmb_miniportadapterctx = NULL;
+ NDIS_UNLOCK(sc);
+
return(0);
}
@@ -1216,7 +1224,7 @@ ndis_init_nic(arg)
* expects them to fire before the halt is called.
*/
- ndis_thsuspend(curthread->td_proc, NULL, hz);
+ tsleep(curthread->td_proc, PWAIT, "ndwait", hz);
NDIS_LOCK(sc);
sc->ndis_block->nmb_devicectx = sc;
@@ -1292,33 +1300,26 @@ ndis_isr(arg, ourintr, callhandler)
}
static void
-ndis_intrhand(dpc, dobj, ip, sc)
+ndis_intrsetup(dpc, dobj, ip, sc)
kdpc *dpc;
device_object *dobj;
irp *ip;
struct ndis_softc *sc;
{
- ndis_handle adapter;
- ndis_interrupt_handler intrfunc;
- uint8_t irql;
-
- adapter = sc->ndis_block->nmb_miniportadapterctx;
- intrfunc = sc->ndis_chars->nmc_interrupt_func;
-
- if (adapter == NULL || intrfunc == NULL)
- return;
-
- if (NDIS_SERIALIZED(sc->ndis_block))
- KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
+ ndis_miniport_interrupt *intr;
- MSCALL1(intrfunc, adapter);
+ intr = sc->ndis_block->nmb_interrupt;
- /* If there's a MiniportEnableInterrupt() routine, call it. */
+ /* Sanity check. */
- ndis_enable_intr(sc);
+ if (intr == NULL)
+ return;
- if (NDIS_SERIALIZED(sc->ndis_block))
- KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
+ KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
+ KeResetEvent(&intr->ni_dpcevt);
+ if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
+ intr->ni_dpccnt++;
+ KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
return;
}
@@ -1424,6 +1425,7 @@ NdisAddDevice(drv, pdo)
block->nmb_physdeviceobj = pdo;
block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
KeInitializeSpinLock(&block->nmb_lock);
+ InitializeListHead(&block->nmb_parmlist);
/*
* Stash pointers to the miniport block and miniport
@@ -1447,7 +1449,7 @@ NdisAddDevice(drv, pdo)
IoDeleteDevice(fdo);
return(status);
}
- INIT_LIST_HEAD((&block->nmb_packetlist));
+ InitializeListHead((&block->nmb_packetlist));
}
/* Give interrupt handling priority over timers. */
OpenPOWER on IntegriCloud