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/subr_ndis.c,v 1.125 2010/12/06 20:54:53 bschmidt Exp $
36 * This file implements a translation layer between the BSD networking
37 * infrasturcture and Windows(R) NDIS network driver modules. A Windows
38 * NDIS driver calls into several functions in the NDIS.SYS Windows
39 * kernel module and exports a table of functions designed to be called
40 * by the NDIS subsystem. Using the PE loader, we can patch our own
41 * versions of the NDIS routines into a given Windows driver module and
42 * convince the driver that it is in fact running on Windows.
44 * We provide a table of all our implemented NDIS routines which is patched
45 * into the driver object code. All our exported routines must use the
46 * _stdcall calling convention, since that's what the Windows object code
50 #include "use_usb4bsd.h"
52 #include <sys/ctype.h>
53 #include <sys/param.h>
54 #include <sys/types.h>
55 #include <sys/errno.h>
57 #include <sys/callout.h>
58 #include <sys/kernel.h>
59 #include <sys/systm.h>
60 #include <sys/malloc.h>
62 #include <sys/mutex.h>
63 #include <sys/socket.h>
64 #include <sys/sysctl.h>
65 #include <sys/_timespec.h>
66 #include <sys/queue.h>
68 #include <sys/filedesc.h>
69 #include <sys/nlookup.h>
70 #include <sys/fcntl.h>
71 #include <sys/vnode.h>
72 #include <sys/kthread.h>
73 #include <sys/linker.h>
74 #include <sys/mount.h>
75 #include <sys/sysproto.h>
78 #include <net/if_arp.h>
79 #include <net/ethernet.h>
80 #include <net/if_dl.h>
81 #include <net/if_media.h>
83 #include <machine/atomic.h>
87 #include <sys/mplock2.h>
89 #include <netproto/802_11/ieee80211_var.h>
90 #include <netproto/802_11/ieee80211_ioctl.h>
92 #include <bus/pci/pcireg.h>
93 #include <bus/pci/pcivar.h>
95 #include <bus/u4b/usb.h>
96 #include <bus/u4b/usbdi.h>
98 #include <bus/usb/usb.h>
99 #include <bus/usb/usbdi.h>
102 #include <emulation/ndis/pe_var.h>
103 #include <emulation/ndis/cfg_var.h>
104 #include <emulation/ndis/resource_var.h>
105 #include <emulation/ndis/ntoskrnl_var.h>
106 #include <emulation/ndis/hal_var.h>
107 #include <emulation/ndis/ndis_var.h>
108 #include <dev/netif/ndis/if_ndisvar.h>
111 #include <vm/vm_param.h>
113 #include <vm/vm_kern.h>
114 #include <vm/vm_map.h>
118 static char ndis_filepath[MAXPATHLEN];
120 SYSCTL_STRING(_hw, OID_AUTO, ndis_filepath, CTLFLAG_RW, ndis_filepath,
121 MAXPATHLEN, "Path used by NdisOpenFile() to search for files");
123 static void NdisInitializeWrapper(ndis_handle *,
124 driver_object *, void *, void *);
125 static ndis_status NdisMRegisterMiniport(ndis_handle,
126 ndis_miniport_characteristics *, int);
127 static ndis_status NdisAllocateMemoryWithTag(void **,
129 static ndis_status NdisAllocateMemory(void **,
130 uint32_t, uint32_t, ndis_physaddr);
131 static void NdisFreeMemory(void *, uint32_t, uint32_t);
132 static ndis_status NdisMSetAttributesEx(ndis_handle, ndis_handle,
133 uint32_t, uint32_t, ndis_interface_type);
134 static void NdisOpenConfiguration(ndis_status *,
135 ndis_handle *, ndis_handle);
136 static void NdisOpenConfigurationKeyByIndex(ndis_status *,
137 ndis_handle, uint32_t, unicode_string *, ndis_handle *);
138 static void NdisOpenConfigurationKeyByName(ndis_status *,
139 ndis_handle, unicode_string *, ndis_handle *);
140 static ndis_status ndis_encode_parm(ndis_miniport_block *,
141 struct sysctl_oid *, ndis_parm_type, ndis_config_parm **);
142 static ndis_status ndis_decode_parm(ndis_miniport_block *,
143 ndis_config_parm *, char *);
144 static void NdisReadConfiguration(ndis_status *, ndis_config_parm **,
145 ndis_handle, unicode_string *, ndis_parm_type);
146 static void NdisWriteConfiguration(ndis_status *, ndis_handle,
147 unicode_string *, ndis_config_parm *);
148 static void NdisCloseConfiguration(ndis_handle);
149 static void NdisAllocateSpinLock(ndis_spin_lock *);
150 static void NdisFreeSpinLock(ndis_spin_lock *);
151 static void NdisAcquireSpinLock(ndis_spin_lock *);
152 static void NdisReleaseSpinLock(ndis_spin_lock *);
153 static void NdisDprAcquireSpinLock(ndis_spin_lock *);
154 static void NdisDprReleaseSpinLock(ndis_spin_lock *);
155 static void NdisInitializeReadWriteLock(ndis_rw_lock *);
156 static void NdisAcquireReadWriteLock(ndis_rw_lock *,
157 uint8_t, ndis_lock_state *);
158 static void NdisReleaseReadWriteLock(ndis_rw_lock *, ndis_lock_state *);
159 static uint32_t NdisReadPciSlotInformation(ndis_handle, uint32_t,
160 uint32_t, void *, uint32_t);
161 static uint32_t NdisWritePciSlotInformation(ndis_handle, uint32_t,
162 uint32_t, void *, uint32_t);
163 static void NdisWriteErrorLogEntry(ndis_handle, ndis_error_code, uint32_t, ...);
164 static void ndis_map_cb(void *, bus_dma_segment_t *, int, int);
165 static void NdisMStartBufferPhysicalMapping(ndis_handle,
166 ndis_buffer *, uint32_t, uint8_t, ndis_paddr_unit *, uint32_t *);
167 static void NdisMCompleteBufferPhysicalMapping(ndis_handle,
168 ndis_buffer *, uint32_t);
169 static void NdisMInitializeTimer(ndis_miniport_timer *, ndis_handle,
170 ndis_timer_function, void *);
171 static void NdisInitializeTimer(ndis_timer *,
172 ndis_timer_function, void *);
173 static void NdisSetTimer(ndis_timer *, uint32_t);
174 static void NdisMSetPeriodicTimer(ndis_miniport_timer *, uint32_t);
175 static void NdisMCancelTimer(ndis_timer *, uint8_t *);
176 static void ndis_timercall(kdpc *, ndis_miniport_timer *,
178 static void NdisMQueryAdapterResources(ndis_status *, ndis_handle,
179 ndis_resource_list *, uint32_t *);
180 static ndis_status NdisMRegisterIoPortRange(void **,
181 ndis_handle, uint32_t, uint32_t);
182 static void NdisMDeregisterIoPortRange(ndis_handle,
183 uint32_t, uint32_t, void *);
184 static void NdisReadNetworkAddress(ndis_status *, void **,
185 uint32_t *, ndis_handle);
186 static ndis_status NdisQueryMapRegisterCount(uint32_t, uint32_t *);
187 static ndis_status NdisMAllocateMapRegisters(ndis_handle,
188 uint32_t, uint8_t, uint32_t, uint32_t);
189 static void NdisMFreeMapRegisters(ndis_handle);
190 static void ndis_mapshared_cb(void *, bus_dma_segment_t *, int, int);
191 static void NdisMAllocateSharedMemory(ndis_handle, uint32_t,
192 uint8_t, void **, ndis_physaddr *);
193 static void ndis_asyncmem_complete(device_object *, void *);
194 static ndis_status NdisMAllocateSharedMemoryAsync(ndis_handle,
195 uint32_t, uint8_t, void *);
196 static void NdisMFreeSharedMemory(ndis_handle, uint32_t,
197 uint8_t, void *, ndis_physaddr);
198 static ndis_status NdisMMapIoSpace(void **, ndis_handle,
199 ndis_physaddr, uint32_t);
200 static void NdisMUnmapIoSpace(ndis_handle, void *, uint32_t);
201 static uint32_t NdisGetCacheFillSize(void);
202 static void *NdisGetRoutineAddress(unicode_string *);
203 static uint32_t NdisMGetDmaAlignment(ndis_handle);
204 static ndis_status NdisMInitializeScatterGatherDma(ndis_handle,
206 static void NdisUnchainBufferAtFront(ndis_packet *, ndis_buffer **);
207 static void NdisUnchainBufferAtBack(ndis_packet *, ndis_buffer **);
208 static void NdisAllocateBufferPool(ndis_status *,
209 ndis_handle *, uint32_t);
210 static void NdisFreeBufferPool(ndis_handle);
211 static void NdisAllocateBuffer(ndis_status *, ndis_buffer **,
212 ndis_handle, void *, uint32_t);
213 static void NdisFreeBuffer(ndis_buffer *);
214 static uint32_t NdisBufferLength(ndis_buffer *);
215 static void NdisQueryBuffer(ndis_buffer *, void **, uint32_t *);
216 static void NdisQueryBufferSafe(ndis_buffer *, void **,
217 uint32_t *, uint32_t);
218 static void *NdisBufferVirtualAddress(ndis_buffer *);
219 static void *NdisBufferVirtualAddressSafe(ndis_buffer *, uint32_t);
220 static void NdisAdjustBufferLength(ndis_buffer *, int);
221 static uint32_t NdisInterlockedIncrement(uint32_t *);
222 static uint32_t NdisInterlockedDecrement(uint32_t *);
223 static void NdisInitializeEvent(ndis_event *);
224 static void NdisSetEvent(ndis_event *);
225 static void NdisResetEvent(ndis_event *);
226 static uint8_t NdisWaitEvent(ndis_event *, uint32_t);
227 static ndis_status NdisUnicodeStringToAnsiString(ansi_string *,
230 NdisAnsiStringToUnicodeString(unicode_string *, ansi_string *);
231 static ndis_status NdisMPciAssignResources(ndis_handle,
232 uint32_t, ndis_resource_list **);
233 static ndis_status NdisMRegisterInterrupt(ndis_miniport_interrupt *,
234 ndis_handle, uint32_t, uint32_t, uint8_t,
235 uint8_t, ndis_interrupt_mode);
236 static void NdisMDeregisterInterrupt(ndis_miniport_interrupt *);
237 static void NdisMRegisterAdapterShutdownHandler(ndis_handle, void *,
238 ndis_shutdown_handler);
239 static void NdisMDeregisterAdapterShutdownHandler(ndis_handle);
240 static uint32_t NDIS_BUFFER_TO_SPAN_PAGES(ndis_buffer *);
241 static void NdisGetBufferPhysicalArraySize(ndis_buffer *,
243 static void NdisQueryBufferOffset(ndis_buffer *,
244 uint32_t *, uint32_t *);
245 static uint32_t NdisReadPcmciaAttributeMemory(ndis_handle,
246 uint32_t, void *, uint32_t);
247 static uint32_t NdisWritePcmciaAttributeMemory(ndis_handle,
248 uint32_t, void *, uint32_t);
249 static list_entry *NdisInterlockedInsertHeadList(list_entry *,
250 list_entry *, ndis_spin_lock *);
251 static list_entry *NdisInterlockedRemoveHeadList(list_entry *,
253 static list_entry *NdisInterlockedInsertTailList(list_entry *,
254 list_entry *, ndis_spin_lock *);
256 NdisMSynchronizeWithInterrupt(ndis_miniport_interrupt *,
258 static void NdisGetCurrentSystemTime(uint64_t *);
259 static void NdisGetSystemUpTime(uint32_t *);
260 static uint32_t NdisGetVersion(void);
261 static void NdisInitializeString(unicode_string *, char *);
262 static void NdisInitAnsiString(ansi_string *, char *);
263 static void NdisInitUnicodeString(unicode_string *, uint16_t *);
264 static void NdisFreeString(unicode_string *);
265 static ndis_status NdisMRemoveMiniport(ndis_handle *);
266 static void NdisTerminateWrapper(ndis_handle, void *);
267 static void NdisMGetDeviceProperty(ndis_handle, device_object **,
268 device_object **, device_object **, cm_resource_list *,
270 static void NdisGetFirstBufferFromPacket(ndis_packet *,
271 ndis_buffer **, void **, uint32_t *, uint32_t *);
272 static void NdisGetFirstBufferFromPacketSafe(ndis_packet *,
273 ndis_buffer **, void **, uint32_t *, uint32_t *, uint32_t);
274 static int ndis_find_sym(linker_file_t, char *, char *, caddr_t *);
275 static void NdisOpenFile(ndis_status *, ndis_handle *, uint32_t *,
276 unicode_string *, ndis_physaddr);
277 static void NdisMapFile(ndis_status *, void **, ndis_handle);
278 static void NdisUnmapFile(ndis_handle);
279 static void NdisCloseFile(ndis_handle);
280 static uint8_t NdisSystemProcessorCount(void);
281 static void NdisGetCurrentProcessorCounts(uint32_t *, uint32_t *, uint32_t *);
282 static void NdisMIndicateStatusComplete(ndis_handle);
283 static void NdisMIndicateStatus(ndis_handle, ndis_status,
285 static uint8_t ndis_intr(kinterrupt *, void *);
286 static void ndis_intrhand(kdpc *, ndis_miniport_interrupt *, void *, void *);
287 static funcptr ndis_findwrap(funcptr);
288 static void NdisCopyFromPacketToPacket(ndis_packet *,
289 uint32_t, uint32_t, ndis_packet *, uint32_t, uint32_t *);
290 static void NdisCopyFromPacketToPacketSafe(ndis_packet *,
291 uint32_t, uint32_t, ndis_packet *, uint32_t, uint32_t *, uint32_t);
292 static void NdisIMCopySendPerPacketInfo(ndis_packet *, ndis_packet *);
293 static ndis_status NdisMRegisterDevice(ndis_handle,
294 unicode_string *, unicode_string *, driver_dispatch **,
295 void **, ndis_handle *);
296 static ndis_status NdisMDeregisterDevice(ndis_handle);
298 NdisMQueryAdapterInstanceName(unicode_string *, ndis_handle);
299 static void NdisMRegisterUnloadHandler(ndis_handle, void *);
300 static void dummy(void);
303 * Some really old drivers do not properly check the return value
304 * from NdisAllocatePacket() and NdisAllocateBuffer() and will
305 * sometimes allocate few more buffers/packets that they originally
306 * requested when they created the pool. To prevent this from being
307 * a problem, we allocate a few extra buffers/packets beyond what
308 * the driver asks for. This #define controls how many.
310 #define NDIS_POOL_EXTRA 16
315 image_patch_table *patch;
317 strcpy(ndis_filepath, "/compat/ndis");
319 patch = ndis_functbl;
320 while (patch->ipt_func != NULL) {
321 windrv_wrap((funcptr)patch->ipt_func,
322 (funcptr *)&patch->ipt_wrap,
323 patch->ipt_argcnt, patch->ipt_ftype);
333 image_patch_table *patch;
335 patch = ndis_functbl;
336 while (patch->ipt_func != NULL) {
337 windrv_unwrap(patch->ipt_wrap);
345 ndis_findwrap(funcptr func)
347 image_patch_table *patch;
349 patch = ndis_functbl;
350 while (patch->ipt_func != NULL) {
351 if ((funcptr)patch->ipt_func == func)
352 return((funcptr)patch->ipt_wrap);
360 * This routine does the messy Windows Driver Model device attachment
361 * stuff on behalf of NDIS drivers. We register our own AddDevice
365 NdisInitializeWrapper(ndis_handle *wrapper, driver_object *drv, void *path,
369 * As of yet, I haven't come up with a compelling
370 * reason to define a private NDIS wrapper structure,
371 * so we use a pointer to the driver object as the
372 * wrapper handle. The driver object has the miniport
373 * characteristics struct for this driver hung off it
374 * via IoAllocateDriverObjectExtension(), and that's
375 * really all the private data we need.
381 * If this was really Windows, we'd be registering dispatch
382 * routines for the NDIS miniport module here, but we're
383 * not Windows so all we really need to do is set up an
384 * AddDevice function that'll be invoked when a new device
388 drv->dro_driverext->dre_adddevicefunc = NdisAddDevice;
392 NdisTerminateWrapper(ndis_handle handle, void *syspec)
394 /* Nothing to see here, move along. */
398 NdisMRegisterMiniport(ndis_handle handle,
399 ndis_miniport_characteristics *characteristics, int len)
401 ndis_miniport_characteristics *ch = NULL;
404 drv = (driver_object *)handle;
407 * We need to save the NDIS miniport characteristics
408 * somewhere. This data is per-driver, not per-device
409 * (all devices handled by the same driver have the
410 * same characteristics) so we hook it onto the driver
411 * object using IoAllocateDriverObjectExtension().
412 * The extra extension info is automagically deleted when
413 * the driver is unloaded (see windrv_unload()).
416 if (IoAllocateDriverObjectExtension(drv, (void *)1,
417 sizeof(ndis_miniport_characteristics), (void **)&ch) !=
419 return (NDIS_STATUS_RESOURCES);
422 bzero((char *)ch, sizeof(ndis_miniport_characteristics));
424 bcopy((char *)characteristics, (char *)ch, len);
426 if (ch->nmc_version_major < 5 || ch->nmc_version_minor < 1) {
427 ch->nmc_shutdown_handler = NULL;
428 ch->nmc_canceltxpkts_handler = NULL;
429 ch->nmc_pnpevent_handler = NULL;
432 return (NDIS_STATUS_SUCCESS);
436 NdisAllocateMemoryWithTag(void **vaddr, uint32_t len, uint32_t tag)
440 mem = ExAllocatePoolWithTag(NonPagedPool, len, tag);
442 return (NDIS_STATUS_RESOURCES);
446 return (NDIS_STATUS_SUCCESS);
450 NdisAllocateMemory(void **vaddr, uint32_t len, uint32_t flags,
451 ndis_physaddr highaddr)
455 mem = ExAllocatePoolWithTag(NonPagedPool, len, 0);
457 return (NDIS_STATUS_RESOURCES);
460 return (NDIS_STATUS_SUCCESS);
464 NdisFreeMemory(void *vaddr, uint32_t len, uint32_t flags)
473 NdisMSetAttributesEx(ndis_handle adapter_handle, ndis_handle adapter_ctx,
474 uint32_t hangsecs, uint32_t flags, ndis_interface_type iftype)
476 ndis_miniport_block *block;
479 * Save the adapter context, we need it for calling
480 * the driver's internal functions.
482 block = (ndis_miniport_block *)adapter_handle;
483 block->nmb_miniportadapterctx = adapter_ctx;
484 block->nmb_checkforhangsecs = hangsecs;
485 block->nmb_flags = flags;
487 return (NDIS_STATUS_SUCCESS);
491 NdisOpenConfiguration(ndis_status *status, ndis_handle *cfg,
495 *status = NDIS_STATUS_SUCCESS;
499 NdisOpenConfigurationKeyByName(ndis_status *status, ndis_handle cfg,
500 unicode_string *subkey, ndis_handle *subhandle)
503 *status = NDIS_STATUS_SUCCESS;
507 NdisOpenConfigurationKeyByIndex(ndis_status *status, ndis_handle cfg,
508 uint32_t idx, unicode_string *subkey, ndis_handle *subhandle)
510 *status = NDIS_STATUS_FAILURE;
514 ndis_encode_parm(ndis_miniport_block *block, struct sysctl_oid *oid,
515 ndis_parm_type type, ndis_config_parm **parm)
518 ndis_parmlist_entry *np;
525 np = ExAllocatePoolWithTag(NonPagedPool,
526 sizeof(ndis_parmlist_entry), 0);
528 return (NDIS_STATUS_RESOURCES);
529 InsertHeadList((&block->nmb_parmlist), (&np->np_list));
530 *parm = p = &np->np_parm;
533 case ndis_parm_string:
534 /* See if this might be a number. */
535 val = strtoul((char *)oid->oid_arg1, NULL, 10);
536 us = &p->ncp_parmdata.ncp_stringdata;
537 p->ncp_type = ndis_parm_string;
539 ksnprintf(tmp, 32, "%x", val);
540 RtlInitAnsiString(&as, tmp);
542 RtlInitAnsiString(&as, (char *)oid->oid_arg1);
545 if (RtlAnsiStringToUnicodeString(us, &as, TRUE)) {
547 return (NDIS_STATUS_RESOURCES);
551 if (strncmp((char *)oid->oid_arg1, "0x", 2) == 0)
555 p->ncp_type = ndis_parm_int;
556 p->ncp_parmdata.ncp_intdata =
557 strtol((char *)oid->oid_arg1, NULL, base);
559 case ndis_parm_hexint:
561 if (strncmp((char *)oid->oid_arg1, "0x", 2) == 0)
567 p->ncp_type = ndis_parm_hexint;
568 p->ncp_parmdata.ncp_intdata =
569 strtoul((char *)oid->oid_arg1, NULL, base);
572 return (NDIS_STATUS_FAILURE);
576 return (NDIS_STATUS_SUCCESS);
580 NdisReadConfiguration(ndis_status *status, ndis_config_parm **parm,
581 ndis_handle cfg, unicode_string *key, ndis_parm_type type)
584 ndis_miniport_block *block;
585 struct ndis_softc *sc;
586 struct sysctl_oid *oidp;
587 struct sysctl_ctx_entry *e;
590 block = (ndis_miniport_block *)cfg;
591 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
593 if (key->us_len == 0 || key->us_buf == NULL) {
594 *status = NDIS_STATUS_FAILURE;
598 if (RtlUnicodeStringToAnsiString(&as, key, TRUE)) {
599 *status = NDIS_STATUS_RESOURCES;
606 * See if registry key is already in a list of known keys
607 * included with the driver.
609 TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
611 if (strcasecmp(oidp->oid_name, keystr) == 0) {
612 if (strcmp((char *)oidp->oid_arg1, "UNSET") == 0) {
613 RtlFreeAnsiString(&as);
614 *status = NDIS_STATUS_FAILURE;
618 *status = ndis_encode_parm(block, oidp, type, parm);
619 RtlFreeAnsiString(&as);
625 * If the key didn't match, add it to the list of dynamically
626 * created ones. Sometimes, drivers refer to registry keys
627 * that aren't documented in their .INF files. These keys
628 * are supposed to be created by some sort of utility or
629 * control panel snap-in that comes with the driver software.
630 * Sometimes it's useful to be able to manipulate these.
631 * If the driver requests the key in the form of a string,
632 * make its default value an empty string, otherwise default
636 if (type == ndis_parm_int || type == ndis_parm_hexint)
637 ndis_add_sysctl(sc, keystr, "(dynamic integer key)",
638 "UNSET", CTLFLAG_RW);
640 ndis_add_sysctl(sc, keystr, "(dynamic string key)",
641 "UNSET", CTLFLAG_RW);
643 RtlFreeAnsiString(&as);
644 *status = NDIS_STATUS_FAILURE;
648 ndis_decode_parm(ndis_miniport_block *block, ndis_config_parm *parm, char *val)
650 unicode_string *ustr;
653 switch(parm->ncp_type) {
654 case ndis_parm_string:
655 ustr = &parm->ncp_parmdata.ncp_stringdata;
656 if (RtlUnicodeStringToAnsiString(&as, ustr, TRUE))
657 return (NDIS_STATUS_RESOURCES);
658 bcopy(as.as_buf, val, as.as_len);
659 RtlFreeAnsiString(&as);
662 ksprintf(val, "%d", parm->ncp_parmdata.ncp_intdata);
664 case ndis_parm_hexint:
665 ksprintf(val, "%xu", parm->ncp_parmdata.ncp_intdata);
668 return (NDIS_STATUS_FAILURE);
671 return (NDIS_STATUS_SUCCESS);
675 NdisWriteConfiguration(ndis_status *status, ndis_handle cfg,
676 unicode_string *key, ndis_config_parm *parm)
680 ndis_miniport_block *block;
681 struct ndis_softc *sc;
682 struct sysctl_oid *oidp;
683 struct sysctl_ctx_entry *e;
686 block = (ndis_miniport_block *)cfg;
687 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
689 if (RtlUnicodeStringToAnsiString(&as, key, TRUE)) {
690 *status = NDIS_STATUS_RESOURCES;
696 /* Decode the parameter into a string. */
697 bzero(val, sizeof(val));
698 *status = ndis_decode_parm(block, parm, val);
699 if (*status != NDIS_STATUS_SUCCESS) {
700 RtlFreeAnsiString(&as);
704 /* See if the key already exists. */
706 TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
708 if (strcasecmp(oidp->oid_name, keystr) == 0) {
709 /* Found it, set the value. */
710 strcpy((char *)oidp->oid_arg1, val);
711 RtlFreeAnsiString(&as);
716 /* Not found, add a new key with the specified value. */
717 ndis_add_sysctl(sc, keystr, "(dynamically set key)",
720 RtlFreeAnsiString(&as);
721 *status = NDIS_STATUS_SUCCESS;
725 NdisCloseConfiguration(ndis_handle cfg)
728 ndis_parmlist_entry *pe;
729 ndis_miniport_block *block;
732 block = (ndis_miniport_block *)cfg;
734 while (!IsListEmpty(&block->nmb_parmlist)) {
735 e = RemoveHeadList(&block->nmb_parmlist);
736 pe = CONTAINING_RECORD(e, ndis_parmlist_entry, np_list);
738 if (p->ncp_type == ndis_parm_string)
739 RtlFreeUnicodeString(&p->ncp_parmdata.ncp_stringdata);
745 * Initialize a Windows spinlock.
748 NdisAllocateSpinLock(ndis_spin_lock *lock)
750 KeInitializeSpinLock(&lock->nsl_spinlock);
755 * Destroy a Windows spinlock. This is a no-op for now. There are two reasons
756 * for this. One is that it's sort of superfluous: we don't have to do anything
757 * special to deallocate the spinlock. The other is that there are some buggy
758 * drivers which call NdisFreeSpinLock() _after_ calling NdisFreeMemory() on
759 * the block of memory in which the spinlock resides. (Yes, ADMtek, I'm
763 NdisFreeSpinLock(ndis_spin_lock *lock)
766 KeInitializeSpinLock(&lock->nsl_spinlock);
772 * Acquire a spinlock from IRQL <= DISPATCH_LEVEL.
776 NdisAcquireSpinLock(ndis_spin_lock *lock)
778 KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
782 * Release a spinlock from IRQL == DISPATCH_LEVEL.
786 NdisReleaseSpinLock(ndis_spin_lock *lock)
788 KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
792 * Acquire a spinlock when already running at IRQL == DISPATCH_LEVEL.
795 NdisDprAcquireSpinLock(ndis_spin_lock *lock)
797 KeAcquireSpinLockAtDpcLevel(&lock->nsl_spinlock);
801 * Release a spinlock without leaving IRQL == DISPATCH_LEVEL.
804 NdisDprReleaseSpinLock(ndis_spin_lock *lock)
806 KeReleaseSpinLockFromDpcLevel(&lock->nsl_spinlock);
810 NdisInitializeReadWriteLock(ndis_rw_lock *lock)
812 KeInitializeSpinLock(&lock->nrl_spinlock);
813 bzero((char *)&lock->nrl_rsvd, sizeof(lock->nrl_rsvd));
817 NdisAcquireReadWriteLock(ndis_rw_lock *lock, uint8_t writeacc,
818 ndis_lock_state *state)
820 if (writeacc == TRUE) {
821 KeAcquireSpinLock(&lock->nrl_spinlock, &state->nls_oldirql);
828 NdisReleaseReadWriteLock(ndis_rw_lock *lock, ndis_lock_state *state)
830 if (lock->nrl_rsvd[0]) {
832 KeReleaseSpinLock(&lock->nrl_spinlock, state->nls_oldirql);
838 NdisReadPciSlotInformation(ndis_handle adapter, uint32_t slot, uint32_t offset,
839 void *buf, uint32_t len)
841 ndis_miniport_block *block;
846 block = (ndis_miniport_block *)adapter;
851 dev = block->nmb_physdeviceobj->do_devext;
854 * I have a test system consisting of a Sun w2100z
855 * dual 2.4Ghz Opteron machine and an Atheros 802.11a/b/g
856 * "Aries" miniPCI NIC. (The NIC is installed in the
857 * machine using a miniPCI to PCI bus adapter card.)
858 * When running in SMP mode, I found that
859 * performing a large number of consecutive calls to
860 * NdisReadPciSlotInformation() would result in a
861 * sudden system reset (or in some cases a freeze).
862 * My suspicion is that the multiple reads are somehow
863 * triggering a fatal PCI bus error that leads to a
864 * machine check. The 1us delay in the loop below
865 * seems to prevent this problem.
868 for (i = 0; i < len; i++) {
870 dest[i] = pci_read_config(dev, i + offset, 1);
877 NdisWritePciSlotInformation(ndis_handle adapter, uint32_t slot,
878 uint32_t offset, void *buf, uint32_t len)
880 ndis_miniport_block *block;
885 block = (ndis_miniport_block *)adapter;
891 dev = block->nmb_physdeviceobj->do_devext;
892 for (i = 0; i < len; i++) {
894 pci_write_config(dev, i + offset, dest[i], 1);
901 * The errorlog routine uses a variable argument list, so we
902 * have to declare it this way.
905 #define ERRMSGLEN 512
907 NdisWriteErrorLogEntry(ndis_handle adapter, ndis_error_code code,
908 uint32_t numerrors, ...)
910 ndis_miniport_block *block;
917 struct ndis_softc *sc;
920 ansi_string as = { 0, 0, NULL };
922 block = (ndis_miniport_block *)adapter;
923 dev = block->nmb_physdeviceobj->do_devext;
924 drv = block->nmb_deviceobj->do_drvobj;
925 sc = device_get_softc(dev);
928 if (ifp != NULL && ifp->if_flags & IFF_DEBUG) {
929 error = pe_get_message((vm_offset_t)drv->dro_driverstart,
930 code, &str, &i, &flags);
932 if (flags & MESSAGE_RESOURCE_UNICODE) {
933 RtlInitUnicodeString(&us, (uint16_t *)str);
934 if (RtlUnicodeStringToAnsiString(&as,
935 &us, TRUE) == STATUS_SUCCESS)
943 device_printf(dev, "NDIS ERROR: %x (%s)\n", code,
944 str == NULL ? "unknown error" : str);
946 if (ifp != NULL && ifp->if_flags & IFF_DEBUG) {
947 device_printf(dev, "NDIS NUMERRORS: %x\n", numerrors);
948 va_start(ap, numerrors);
949 for (i = 0; i < numerrors; i++)
950 device_printf(dev, "argptr: %p\n",
956 RtlFreeAnsiString(&as);
960 ndis_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
962 struct ndis_map_arg *ctx;
970 for (i = 0; i < nseg; i++) {
971 ctx->nma_fraglist[i].npu_physaddr.np_quad = segs[i].ds_addr;
972 ctx->nma_fraglist[i].npu_len = segs[i].ds_len;
979 NdisMStartBufferPhysicalMapping(ndis_handle adapter, ndis_buffer *buf,
980 uint32_t mapreg, uint8_t writedev, ndis_paddr_unit *addrarray,
983 ndis_miniport_block *block;
984 struct ndis_softc *sc;
985 struct ndis_map_arg nma;
992 block = (ndis_miniport_block *)adapter;
993 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
995 if (mapreg > sc->ndis_mmapcnt)
998 map = sc->ndis_mmaps[mapreg];
999 nma.nma_fraglist = addrarray;
1001 error = bus_dmamap_load(sc->ndis_mtag, map,
1002 MmGetMdlVirtualAddress(buf), MmGetMdlByteCount(buf), ndis_map_cb,
1003 (void *)&nma, BUS_DMA_NOWAIT);
1008 bus_dmamap_sync(sc->ndis_mtag, map,
1009 writedev ? BUS_DMASYNC_PREWRITE : BUS_DMASYNC_PREREAD);
1011 *arraysize = nma.nma_cnt;
1015 NdisMCompleteBufferPhysicalMapping(ndis_handle adapter, ndis_buffer *buf,
1018 ndis_miniport_block *block;
1019 struct ndis_softc *sc;
1022 if (adapter == NULL)
1025 block = (ndis_miniport_block *)adapter;
1026 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1028 if (mapreg > sc->ndis_mmapcnt)
1031 map = sc->ndis_mmaps[mapreg];
1033 bus_dmamap_sync(sc->ndis_mtag, map,
1034 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1036 bus_dmamap_unload(sc->ndis_mtag, map);
1040 * This is an older (?) timer init routine which doesn't
1041 * accept a miniport context handle. Serialized miniports should
1042 * never call this function.
1046 NdisInitializeTimer(ndis_timer *timer, ndis_timer_function func, void *ctx)
1048 KeInitializeTimer(&timer->nt_ktimer);
1049 KeInitializeDpc(&timer->nt_kdpc, func, ctx);
1050 KeSetImportanceDpc(&timer->nt_kdpc, KDPC_IMPORTANCE_LOW);
1054 ndis_timercall(kdpc *dpc, ndis_miniport_timer *timer, void *sysarg1,
1058 * Since we're called as a DPC, we should be running
1059 * at DISPATCH_LEVEL here. This means to acquire the
1060 * spinlock, we can use KeAcquireSpinLockAtDpcLevel()
1061 * rather than KeAcquireSpinLock().
1063 if (NDIS_SERIALIZED(timer->nmt_block))
1064 KeAcquireSpinLockAtDpcLevel(&timer->nmt_block->nmb_lock);
1066 MSCALL4(timer->nmt_timerfunc, dpc, timer->nmt_timerctx,
1069 if (NDIS_SERIALIZED(timer->nmt_block))
1070 KeReleaseSpinLockFromDpcLevel(&timer->nmt_block->nmb_lock);
1074 * For a long time I wondered why there were two NDIS timer initialization
1075 * routines, and why this one needed an NDIS_MINIPORT_TIMER and the
1076 * MiniportAdapterHandle. The NDIS_MINIPORT_TIMER has its own callout
1077 * function and context pointers separate from those in the DPC, which
1078 * allows for another level of indirection: when the timer fires, we
1079 * can have our own timer function invoked, and from there we can call
1080 * the driver's function. But why go to all that trouble? Then it hit
1081 * me: for serialized miniports, the timer callouts are not re-entrant.
1082 * By trapping the callouts and having access to the MiniportAdapterHandle,
1083 * we can protect the driver callouts by acquiring the NDIS serialization
1084 * lock. This is essential for allowing serialized miniports to work
1085 * correctly on SMP systems. On UP hosts, setting IRQL to DISPATCH_LEVEL
1086 * is enough to prevent other threads from pre-empting you, but with
1087 * SMP, you must acquire a lock as well, otherwise the other CPU is
1088 * free to clobber you.
1091 NdisMInitializeTimer(ndis_miniport_timer *timer, ndis_handle handle,
1092 ndis_timer_function func, void *ctx)
1094 /* Save the driver's funcptr and context */
1096 timer->nmt_timerfunc = func;
1097 timer->nmt_timerctx = ctx;
1098 timer->nmt_block = handle;
1101 * Set up the timer so it will call our intermediate DPC.
1102 * Be sure to use the wrapped entry point, since
1103 * ntoskrnl_run_dpc() expects to invoke a function with
1104 * Microsoft calling conventions.
1106 KeInitializeTimer(&timer->nmt_ktimer);
1107 KeInitializeDpc(&timer->nmt_kdpc,
1108 ndis_findwrap((funcptr)ndis_timercall), timer);
1109 timer->nmt_ktimer.k_dpc = &timer->nmt_kdpc;
1113 * In Windows, there's both an NdisMSetTimer() and an NdisSetTimer(),
1114 * but the former is just a macro wrapper around the latter.
1117 NdisSetTimer(ndis_timer *timer, uint32_t msecs)
1120 * KeSetTimer() wants the period in
1121 * hundred nanosecond intervals.
1123 KeSetTimer(&timer->nt_ktimer,
1124 ((int64_t)msecs * -10000), &timer->nt_kdpc);
1128 NdisMSetPeriodicTimer(ndis_miniport_timer *timer, uint32_t msecs)
1130 KeSetTimerEx(&timer->nmt_ktimer,
1131 ((int64_t)msecs * -10000), msecs, &timer->nmt_kdpc);
1135 * Technically, this is really NdisCancelTimer(), but we also
1136 * (ab)use it for NdisMCancelTimer(), since in our implementation
1137 * we don't need the extra info in the ndis_miniport_timer
1138 * structure just to cancel a timer.
1142 NdisMCancelTimer(ndis_timer *timer, uint8_t *cancelled)
1145 *cancelled = KeCancelTimer(&timer->nt_ktimer);
1149 NdisMQueryAdapterResources(ndis_status *status, ndis_handle adapter,
1150 ndis_resource_list *list, uint32_t *buflen)
1152 ndis_miniport_block *block;
1153 struct ndis_softc *sc;
1156 block = (ndis_miniport_block *)adapter;
1157 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1159 rsclen = sizeof(ndis_resource_list) +
1160 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1));
1161 if (*buflen < rsclen) {
1163 *status = NDIS_STATUS_INVALID_LENGTH;
1167 bcopy((char *)block->nmb_rlist, (char *)list, rsclen);
1168 *status = NDIS_STATUS_SUCCESS;
1172 NdisMRegisterIoPortRange(void **offset, ndis_handle adapter, uint32_t port,
1175 struct ndis_miniport_block *block;
1176 struct ndis_softc *sc;
1178 if (adapter == NULL)
1179 return (NDIS_STATUS_FAILURE);
1181 block = (ndis_miniport_block *)adapter;
1182 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1184 if (sc->ndis_res_io == NULL)
1185 return (NDIS_STATUS_FAILURE);
1187 /* Don't let the device map more ports than we have. */
1188 if (rman_get_size(sc->ndis_res_io) < numports)
1189 return (NDIS_STATUS_INVALID_LENGTH);
1191 *offset = (void *)rman_get_start(sc->ndis_res_io);
1193 return (NDIS_STATUS_SUCCESS);
1197 NdisMDeregisterIoPortRange(ndis_handle adapter, uint32_t port,
1198 uint32_t numports, void *offset)
1203 NdisReadNetworkAddress(ndis_status *status, void **addr, uint32_t *addrlen,
1204 ndis_handle adapter)
1206 struct ndis_softc *sc;
1207 ndis_miniport_block *block;
1208 uint8_t empty[] = { 0, 0, 0, 0, 0, 0 };
1210 block = (ndis_miniport_block *)adapter;
1211 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1212 if (sc->ifp == NULL) {
1213 *status = NDIS_STATUS_FAILURE;
1217 if (sc->ifp->if_lladdr == NULL ||
1218 bcmp(IF_LLADDR(sc->ifp), empty, ETHER_ADDR_LEN) == 0)
1219 *status = NDIS_STATUS_FAILURE;
1221 *addr = IF_LLADDR(sc->ifp);
1222 *addrlen = ETHER_ADDR_LEN;
1223 *status = NDIS_STATUS_SUCCESS;
1228 NdisQueryMapRegisterCount(uint32_t bustype, uint32_t *cnt)
1231 return (NDIS_STATUS_SUCCESS);
1235 NdisMAllocateMapRegisters(ndis_handle adapter, uint32_t dmachannel,
1236 uint8_t dmasize, uint32_t physmapneeded, uint32_t maxmap)
1238 struct ndis_softc *sc;
1239 ndis_miniport_block *block;
1240 int error, i, nseg = NDIS_MAXSEG;
1242 block = (ndis_miniport_block *)adapter;
1243 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1245 sc->ndis_mmaps = kmalloc(sizeof(bus_dmamap_t) * physmapneeded,
1246 M_DEVBUF, M_NOWAIT|M_ZERO);
1248 if (sc->ndis_mmaps == NULL)
1249 return (NDIS_STATUS_RESOURCES);
1251 error = bus_dma_tag_create(sc->ndis_parent_tag, ETHER_ALIGN, 0,
1252 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1253 NULL, maxmap * nseg, nseg, maxmap, BUS_DMA_ALLOCNOW,
1257 kfree(sc->ndis_mmaps, M_DEVBUF);
1258 return (NDIS_STATUS_RESOURCES);
1261 for (i = 0; i < physmapneeded; i++)
1262 bus_dmamap_create(sc->ndis_mtag, 0, &sc->ndis_mmaps[i]);
1264 sc->ndis_mmapcnt = physmapneeded;
1266 return (NDIS_STATUS_SUCCESS);
1270 NdisMFreeMapRegisters(ndis_handle adapter)
1272 struct ndis_softc *sc;
1273 ndis_miniport_block *block;
1276 block = (ndis_miniport_block *)adapter;
1277 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1279 for (i = 0; i < sc->ndis_mmapcnt; i++)
1280 bus_dmamap_destroy(sc->ndis_mtag, sc->ndis_mmaps[i]);
1282 kfree(sc->ndis_mmaps, M_DEVBUF);
1284 bus_dma_tag_destroy(sc->ndis_mtag);
1288 ndis_mapshared_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1292 if (error || nseg > 1)
1297 p->np_quad = segs[0].ds_addr;
1301 * This maps to bus_dmamem_alloc().
1305 NdisMAllocateSharedMemory(ndis_handle adapter, uint32_t len, uint8_t cached,
1306 void **vaddr, ndis_physaddr *paddr)
1308 ndis_miniport_block *block;
1309 struct ndis_softc *sc;
1310 struct ndis_shmem *sh;
1313 if (adapter == NULL)
1316 block = (ndis_miniport_block *)adapter;
1317 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1319 sh = kmalloc(sizeof(struct ndis_shmem), M_DEVBUF, M_NOWAIT|M_ZERO);
1323 InitializeListHead(&sh->ndis_list);
1326 * When performing shared memory allocations, create a tag
1327 * with a lowaddr limit that restricts physical memory mappings
1328 * so that they all fall within the first 1GB of memory.
1329 * At least one device/driver combination (Linksys Instant
1330 * Wireless PCI Card V2.7, Broadcom 802.11b) seems to have
1331 * problems with performing DMA operations with physical
1332 * addresses that lie above the 1GB mark. I don't know if this
1333 * is a hardware limitation or if the addresses are being
1334 * truncated within the driver, but this seems to be the only
1335 * way to make these cards work reliably in systems with more
1336 * than 1GB of physical memory.
1339 error = bus_dma_tag_create(sc->ndis_parent_tag, 64,
1340 0, NDIS_BUS_SPACE_SHARED_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1341 NULL, len, 1, len, BUS_DMA_ALLOCNOW, &sh->ndis_stag);
1344 kfree(sh, M_DEVBUF);
1348 error = bus_dmamem_alloc(sh->ndis_stag, vaddr,
1349 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sh->ndis_smap);
1352 bus_dma_tag_destroy(sh->ndis_stag);
1353 kfree(sh, M_DEVBUF);
1357 error = bus_dmamap_load(sh->ndis_stag, sh->ndis_smap, *vaddr,
1358 len, ndis_mapshared_cb, (void *)paddr, BUS_DMA_NOWAIT);
1361 bus_dmamem_free(sh->ndis_stag, *vaddr, sh->ndis_smap);
1362 bus_dma_tag_destroy(sh->ndis_stag);
1363 kfree(sh, M_DEVBUF);
1368 * Save the physical address along with the source address.
1369 * The AirGo MIMO driver will call NdisMFreeSharedMemory()
1370 * with a bogus virtual address sometimes, but with a valid
1371 * physical address. To keep this from causing trouble, we
1372 * use the physical address to as a sanity check in case
1373 * searching based on the virtual address fails.
1377 sh->ndis_paddr.np_quad = paddr->np_quad;
1378 sh->ndis_saddr = *vaddr;
1379 InsertHeadList((&sc->ndis_shlist), (&sh->ndis_list));
1383 struct ndis_allocwork {
1391 ndis_asyncmem_complete(device_object *dobj, void *arg)
1393 ndis_miniport_block *block;
1394 struct ndis_softc *sc;
1395 struct ndis_allocwork *w;
1397 ndis_physaddr paddr;
1398 ndis_allocdone_handler donefunc;
1401 block = (ndis_miniport_block *)dobj->do_devext;
1402 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1407 donefunc = sc->ndis_chars->nmc_allocate_complete_func;
1408 NdisMAllocateSharedMemory(block, w->na_len,
1409 w->na_cached, &vaddr, &paddr);
1410 MSCALL5(donefunc, block, vaddr, &paddr, w->na_len, w->na_ctx);
1412 IoFreeWorkItem(w->na_iw);
1417 NdisMAllocateSharedMemoryAsync(ndis_handle adapter, uint32_t len,
1418 uint8_t cached, void *ctx)
1420 ndis_miniport_block *block;
1421 struct ndis_allocwork *w;
1423 io_workitem_func ifw;
1425 if (adapter == NULL)
1426 return (NDIS_STATUS_FAILURE);
1430 iw = IoAllocateWorkItem(block->nmb_deviceobj);
1432 return (NDIS_STATUS_FAILURE);
1434 w = kmalloc(sizeof(struct ndis_allocwork), M_TEMP, M_NOWAIT);
1437 return (NDIS_STATUS_FAILURE);
1439 w->na_cached = cached;
1444 ifw = (io_workitem_func)ndis_findwrap((funcptr)ndis_asyncmem_complete);
1445 IoQueueWorkItem(iw, ifw, WORKQUEUE_DELAYED, w);
1447 return (NDIS_STATUS_PENDING);
1451 NdisMFreeSharedMemory(ndis_handle adapter, uint32_t len, uint8_t cached,
1452 void *vaddr, ndis_physaddr paddr)
1454 ndis_miniport_block *block;
1455 struct ndis_softc *sc;
1456 struct ndis_shmem *sh = NULL;
1459 if (vaddr == NULL || adapter == NULL)
1462 block = (ndis_miniport_block *)adapter;
1463 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1465 /* Sanity check: is list empty? */
1467 if (IsListEmpty(&sc->ndis_shlist))
1471 l = sc->ndis_shlist.nle_flink;
1472 while (l != &sc->ndis_shlist) {
1473 sh = CONTAINING_RECORD(l, struct ndis_shmem, ndis_list);
1474 if (sh->ndis_saddr == vaddr)
1477 * Check the physaddr too, just in case the driver lied
1478 * about the virtual address.
1480 if (sh->ndis_paddr.np_quad == paddr.np_quad)
1487 kprintf("NDIS: buggy driver tried to free "
1488 "invalid shared memory: vaddr: %p paddr: 0x%jx\n",
1489 vaddr, (uintmax_t)paddr.np_quad);
1493 RemoveEntryList(&sh->ndis_list);
1497 bus_dmamap_unload(sh->ndis_stag, sh->ndis_smap);
1498 bus_dmamem_free(sh->ndis_stag, sh->ndis_saddr, sh->ndis_smap);
1499 bus_dma_tag_destroy(sh->ndis_stag);
1501 kfree(sh, M_DEVBUF);
1505 NdisMMapIoSpace(void **vaddr, ndis_handle adapter, ndis_physaddr paddr,
1508 if (adapter == NULL)
1509 return (NDIS_STATUS_FAILURE);
1511 *vaddr = MmMapIoSpace(paddr.np_quad, len, 0);
1514 return (NDIS_STATUS_FAILURE);
1516 return (NDIS_STATUS_SUCCESS);
1520 NdisMUnmapIoSpace(ndis_handle adapter, void *vaddr, uint32_t len)
1522 MmUnmapIoSpace(vaddr, len);
1526 NdisGetCacheFillSize(void)
1532 NdisGetRoutineAddress(unicode_string *ustr)
1536 if (RtlUnicodeStringToAnsiString(&astr, ustr, TRUE))
1538 return (ndis_get_routine_address(ndis_functbl, astr.as_buf));
1542 NdisMGetDmaAlignment(ndis_handle handle)
1548 * NDIS has two methods for dealing with NICs that support DMA.
1549 * One is to just pass packets to the driver and let it call
1550 * NdisMStartBufferPhysicalMapping() to map each buffer in the packet
1551 * all by itself, and the other is to let the NDIS library handle the
1552 * buffer mapping internally, and hand the driver an already populated
1553 * scatter/gather fragment list. If the driver calls
1554 * NdisMInitializeScatterGatherDma(), it wants to use the latter
1559 NdisMInitializeScatterGatherDma(ndis_handle adapter, uint8_t is64,
1560 uint32_t maxphysmap)
1562 struct ndis_softc *sc;
1563 ndis_miniport_block *block;
1566 if (adapter == NULL)
1567 return (NDIS_STATUS_FAILURE);
1568 block = (ndis_miniport_block *)adapter;
1569 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1571 /* Don't do this twice. */
1572 if (sc->ndis_sc == 1)
1573 return (NDIS_STATUS_SUCCESS);
1575 error = bus_dma_tag_create(sc->ndis_parent_tag, ETHER_ALIGN, 0,
1576 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1577 MCLBYTES * NDIS_MAXSEG, NDIS_MAXSEG, MCLBYTES, BUS_DMA_ALLOCNOW,
1582 return (NDIS_STATUS_SUCCESS);
1586 NdisAllocatePacketPool(ndis_status *status, ndis_handle *pool,
1587 uint32_t descnum, uint32_t protrsvdlen)
1589 ndis_packet_pool *p;
1590 ndis_packet *packets;
1593 p = ExAllocatePoolWithTag(NonPagedPool, sizeof(ndis_packet_pool), 0);
1595 *status = NDIS_STATUS_RESOURCES;
1599 p->np_cnt = descnum + NDIS_POOL_EXTRA;
1600 p->np_protrsvd = protrsvdlen;
1601 p->np_len = sizeof(ndis_packet) + protrsvdlen;
1603 packets = ExAllocatePoolWithTag(NonPagedPool, p->np_cnt *
1607 if (packets == NULL) {
1609 *status = NDIS_STATUS_RESOURCES;
1613 p->np_pktmem = packets;
1615 for (i = 0; i < p->np_cnt; i++)
1616 InterlockedPushEntrySList(&p->np_head,
1617 (struct slist_entry *)&packets[i]);
1619 #ifdef NDIS_DEBUG_PACKETS
1621 KeInitializeSpinLock(&p->np_lock);
1622 KeInitializeEvent(&p->np_event, EVENT_TYPE_NOTIFY, TRUE);
1626 *status = NDIS_STATUS_SUCCESS;
1630 NdisAllocatePacketPoolEx(ndis_status *status, ndis_handle *pool,
1631 uint32_t descnum, uint32_t oflowdescnum, uint32_t protrsvdlen)
1633 return (NdisAllocatePacketPool(status, pool,
1634 descnum + oflowdescnum, protrsvdlen));
1638 NdisPacketPoolUsage(ndis_handle pool)
1640 ndis_packet_pool *p;
1642 p = (ndis_packet_pool *)pool;
1643 return (p->np_cnt - ExQueryDepthSList(&p->np_head));
1647 NdisFreePacketPool(ndis_handle pool)
1649 ndis_packet_pool *p;
1651 #ifdef NDIS_DEBUG_PACKETS
1655 p = (ndis_packet_pool *)pool;
1657 #ifdef NDIS_DEBUG_PACKETS
1658 KeAcquireSpinLock(&p->np_lock, &irql);
1661 usage = NdisPacketPoolUsage(pool);
1663 #ifdef NDIS_DEBUG_PACKETS
1666 KeResetEvent(&p->np_event);
1667 KeReleaseSpinLock(&p->np_lock, irql);
1668 KeWaitForSingleObject(&p->np_event, 0, 0, FALSE, NULL);
1670 KeReleaseSpinLock(&p->np_lock, irql);
1673 ExFreePool(p->np_pktmem);
1678 NdisAllocatePacket(ndis_status *status, ndis_packet **packet, ndis_handle pool)
1680 ndis_packet_pool *p;
1682 #ifdef NDIS_DEBUG_PACKETS
1686 p = (ndis_packet_pool *)pool;
1688 #ifdef NDIS_DEBUG_PACKETS
1689 KeAcquireSpinLock(&p->np_lock, &irql);
1691 KeReleaseSpinLock(&p->np_lock, irql);
1692 kprintf("NDIS: tried to allocate packet from dead pool %p\n",
1694 *status = NDIS_STATUS_RESOURCES;
1699 pkt = (ndis_packet *)InterlockedPopEntrySList(&p->np_head);
1701 #ifdef NDIS_DEBUG_PACKETS
1702 KeReleaseSpinLock(&p->np_lock, irql);
1706 *status = NDIS_STATUS_RESOURCES;
1711 bzero((char *)pkt, sizeof(ndis_packet));
1713 /* Save pointer to the pool. */
1714 pkt->np_private.npp_pool = pool;
1716 /* Set the oob offset pointer. Lots of things expect this. */
1717 pkt->np_private.npp_packetooboffset = offsetof(ndis_packet, np_oob);
1720 * We must initialize the packet flags correctly in order
1721 * for the NDIS_SET_PACKET_MEDIA_SPECIFIC_INFO() and
1722 * NDIS_GET_PACKET_MEDIA_SPECIFIC_INFO() macros to work
1725 pkt->np_private.npp_ndispktflags = NDIS_PACKET_ALLOCATED_BY_NDIS;
1726 pkt->np_private.npp_validcounts = FALSE;
1730 *status = NDIS_STATUS_SUCCESS;
1734 NdisFreePacket(ndis_packet *packet)
1736 ndis_packet_pool *p;
1737 #ifdef NDIS_DEBUG_PACKETS
1741 p = (ndis_packet_pool *)packet->np_private.npp_pool;
1743 #ifdef NDIS_DEBUG_PACKETS
1744 KeAcquireSpinLock(&p->np_lock, &irql);
1747 InterlockedPushEntrySList(&p->np_head, (slist_entry *)packet);
1749 #ifdef NDIS_DEBUG_PACKETS
1751 if (ExQueryDepthSList(&p->np_head) == p->np_cnt)
1752 KeSetEvent(&p->np_event, IO_NO_INCREMENT, FALSE);
1754 KeReleaseSpinLock(&p->np_lock, irql);
1759 NdisUnchainBufferAtFront(ndis_packet *packet, ndis_buffer **buf)
1761 ndis_packet_private *priv;
1763 if (packet == NULL || buf == NULL)
1766 priv = &packet->np_private;
1768 priv->npp_validcounts = FALSE;
1770 if (priv->npp_head == priv->npp_tail) {
1771 *buf = priv->npp_head;
1772 priv->npp_head = priv->npp_tail = NULL;
1774 *buf = priv->npp_head;
1775 priv->npp_head = (*buf)->mdl_next;
1780 NdisUnchainBufferAtBack(ndis_packet *packet, ndis_buffer **buf)
1782 ndis_packet_private *priv;
1785 if (packet == NULL || buf == NULL)
1788 priv = &packet->np_private;
1790 priv->npp_validcounts = FALSE;
1792 if (priv->npp_head == priv->npp_tail) {
1793 *buf = priv->npp_head;
1794 priv->npp_head = priv->npp_tail = NULL;
1796 *buf = priv->npp_tail;
1797 tmp = priv->npp_head;
1798 while (tmp->mdl_next != priv->npp_tail)
1799 tmp = tmp->mdl_next;
1800 priv->npp_tail = tmp;
1801 tmp->mdl_next = NULL;
1806 * The NDIS "buffer" is really an MDL (memory descriptor list)
1807 * which is used to describe a buffer in a way that allows it
1808 * to mapped into different contexts. We have to be careful how
1809 * we handle them: in some versions of Windows, the NdisFreeBuffer()
1810 * routine is an actual function in the NDIS API, but in others
1811 * it's just a macro wrapper around IoFreeMdl(). There's really
1812 * no way to use the 'descnum' parameter to count how many
1813 * "buffers" are allocated since in order to use IoFreeMdl() to
1814 * dispose of a buffer, we have to use IoAllocateMdl() to allocate
1815 * them, and IoAllocateMdl() just grabs them out of the heap.
1819 NdisAllocateBufferPool(ndis_status *status, ndis_handle *pool,
1824 * The only thing we can really do here is verify that descnum
1825 * is a reasonable value, but I really don't know what to check
1829 *pool = NonPagedPool;
1830 *status = NDIS_STATUS_SUCCESS;
1834 NdisFreeBufferPool(ndis_handle pool)
1839 NdisAllocateBuffer(ndis_status *status, ndis_buffer **buffer, ndis_handle pool,
1840 void *vaddr, uint32_t len)
1844 buf = IoAllocateMdl(vaddr, len, FALSE, FALSE, NULL);
1846 *status = NDIS_STATUS_RESOURCES;
1850 MmBuildMdlForNonPagedPool(buf);
1853 *status = NDIS_STATUS_SUCCESS;
1857 NdisFreeBuffer(ndis_buffer *buf)
1865 NdisBufferLength(ndis_buffer *buf)
1867 return (MmGetMdlByteCount(buf));
1871 * Get the virtual address and length of a buffer.
1872 * Note: the vaddr argument is optional.
1876 NdisQueryBuffer(ndis_buffer *buf, void **vaddr, uint32_t *len)
1879 *vaddr = MmGetMdlVirtualAddress(buf);
1880 *len = MmGetMdlByteCount(buf);
1883 /* Same as above -- we don't care about the priority. */
1886 NdisQueryBufferSafe(ndis_buffer *buf, void **vaddr, uint32_t *len,
1890 *vaddr = MmGetMdlVirtualAddress(buf);
1891 *len = MmGetMdlByteCount(buf);
1894 /* Damnit Microsoft!! How many ways can you do the same thing?! */
1897 NdisBufferVirtualAddress(ndis_buffer *buf)
1899 return (MmGetMdlVirtualAddress(buf));
1903 NdisBufferVirtualAddressSafe(ndis_buffer *buf, uint32_t prio)
1905 return (MmGetMdlVirtualAddress(buf));
1909 NdisAdjustBufferLength(ndis_buffer *buf, int len)
1911 MmGetMdlByteCount(buf) = len;
1915 NdisInterlockedIncrement(uint32_t *addend)
1917 atomic_add_long((u_long *)addend, 1);
1922 NdisInterlockedDecrement(uint32_t *addend)
1924 atomic_subtract_long((u_long *)addend, 1);
1929 NdisGetVersion(void)
1931 return (0x00050001);
1935 NdisInitializeEvent(ndis_event *event)
1938 * NDIS events are always notification
1939 * events, and should be initialized to the
1940 * not signaled state.
1942 KeInitializeEvent(&event->ne_event, EVENT_TYPE_NOTIFY, FALSE);
1946 NdisSetEvent(ndis_event *event)
1948 KeSetEvent(&event->ne_event, IO_NO_INCREMENT, FALSE);
1952 NdisResetEvent(ndis_event *event)
1954 KeResetEvent(&event->ne_event);
1958 NdisWaitEvent(ndis_event *event, uint32_t msecs)
1963 duetime = ((int64_t)msecs * -10000);
1964 rval = KeWaitForSingleObject(event,
1965 0, 0, TRUE, msecs ? & duetime : NULL);
1967 if (rval == STATUS_TIMEOUT)
1974 NdisUnicodeStringToAnsiString(ansi_string *dstr, unicode_string *sstr)
1978 rval = RtlUnicodeStringToAnsiString(dstr, sstr, FALSE);
1980 if (rval == STATUS_INSUFFICIENT_RESOURCES)
1981 return (NDIS_STATUS_RESOURCES);
1983 return (NDIS_STATUS_FAILURE);
1985 return (NDIS_STATUS_SUCCESS);
1989 NdisAnsiStringToUnicodeString(unicode_string *dstr, ansi_string *sstr)
1993 rval = RtlAnsiStringToUnicodeString(dstr, sstr, FALSE);
1995 if (rval == STATUS_INSUFFICIENT_RESOURCES)
1996 return (NDIS_STATUS_RESOURCES);
1998 return (NDIS_STATUS_FAILURE);
2000 return (NDIS_STATUS_SUCCESS);
2004 NdisMPciAssignResources(ndis_handle adapter, uint32_t slot,
2005 ndis_resource_list **list)
2007 ndis_miniport_block *block;
2009 if (adapter == NULL || list == NULL)
2010 return (NDIS_STATUS_FAILURE);
2012 block = (ndis_miniport_block *)adapter;
2013 *list = block->nmb_rlist;
2015 return (NDIS_STATUS_SUCCESS);
2019 ndis_intr(kinterrupt *iobj, void *arg)
2021 struct ndis_softc *sc;
2022 uint8_t is_our_intr = FALSE;
2024 ndis_miniport_interrupt *intr;
2027 intr = sc->ndis_block->nmb_interrupt;
2029 if (intr == NULL || sc->ndis_block->nmb_miniportadapterctx == NULL)
2032 if (sc->ndis_block->nmb_interrupt->ni_isrreq == TRUE)
2033 MSCALL3(intr->ni_isrfunc, &is_our_intr, &call_isr,
2034 sc->ndis_block->nmb_miniportadapterctx);
2036 MSCALL1(sc->ndis_chars->nmc_disable_interrupts_func,
2037 sc->ndis_block->nmb_miniportadapterctx);
2042 IoRequestDpc(sc->ndis_block->nmb_deviceobj, NULL, sc);
2044 return (is_our_intr);
2048 ndis_intrhand(kdpc *dpc, ndis_miniport_interrupt *intr, void *sysarg1,
2051 struct ndis_softc *sc;
2052 ndis_miniport_block *block;
2053 ndis_handle adapter;
2055 block = intr->ni_block;
2056 adapter = block->nmb_miniportadapterctx;
2057 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2059 if (NDIS_SERIALIZED(sc->ndis_block))
2060 KeAcquireSpinLockAtDpcLevel(&block->nmb_lock);
2062 MSCALL1(intr->ni_dpcfunc, adapter);
2064 /* If there's a MiniportEnableInterrupt() routine, call it. */
2066 if (sc->ndis_chars->nmc_enable_interrupts_func != NULL)
2067 MSCALL1(sc->ndis_chars->nmc_enable_interrupts_func, adapter);
2069 if (NDIS_SERIALIZED(sc->ndis_block))
2070 KeReleaseSpinLockFromDpcLevel(&block->nmb_lock);
2073 * Set the completion event if we've drained all
2074 * pending interrupts.
2077 KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
2079 if (intr->ni_dpccnt == 0)
2080 KeSetEvent(&intr->ni_dpcevt, IO_NO_INCREMENT, FALSE);
2081 KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
2085 NdisMRegisterInterrupt(ndis_miniport_interrupt *intr, ndis_handle adapter,
2086 uint32_t ivec, uint32_t ilevel, uint8_t reqisr, uint8_t shared,
2087 ndis_interrupt_mode imode)
2089 ndis_miniport_block *block;
2090 ndis_miniport_characteristics *ch;
2091 struct ndis_softc *sc;
2095 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2096 ch = IoGetDriverObjectExtension(block->nmb_deviceobj->do_drvobj,
2099 intr->ni_rsvd = ExAllocatePoolWithTag(NonPagedPool,
2100 sizeof(struct mtx), 0);
2101 if (intr->ni_rsvd == NULL)
2102 return (NDIS_STATUS_RESOURCES);
2104 intr->ni_block = adapter;
2105 intr->ni_isrreq = reqisr;
2106 intr->ni_shared = shared;
2107 intr->ni_dpccnt = 0;
2108 intr->ni_isrfunc = ch->nmc_isr_func;
2109 intr->ni_dpcfunc = ch->nmc_interrupt_func;
2111 KeInitializeEvent(&intr->ni_dpcevt, EVENT_TYPE_NOTIFY, TRUE);
2112 KeInitializeDpc(&intr->ni_dpc,
2113 ndis_findwrap((funcptr)ndis_intrhand), intr);
2114 KeSetImportanceDpc(&intr->ni_dpc, KDPC_IMPORTANCE_LOW);
2116 error = IoConnectInterrupt(&intr->ni_introbj,
2117 ndis_findwrap((funcptr)ndis_intr), sc, NULL,
2118 ivec, ilevel, 0, imode, shared, 0, FALSE);
2120 if (error != STATUS_SUCCESS)
2121 return (NDIS_STATUS_FAILURE);
2123 block->nmb_interrupt = intr;
2125 return (NDIS_STATUS_SUCCESS);
2129 NdisMDeregisterInterrupt(ndis_miniport_interrupt *intr)
2131 ndis_miniport_block *block;
2134 block = intr->ni_block;
2136 /* Should really be KeSynchronizeExecution() */
2138 KeAcquireSpinLock(intr->ni_introbj->ki_lock, &irql);
2139 block->nmb_interrupt = NULL;
2140 KeReleaseSpinLock(intr->ni_introbj->ki_lock, irql);
2142 KeFlushQueuedDpcs();
2144 /* Disconnect our ISR */
2146 IoDisconnectInterrupt(intr->ni_introbj);
2148 KeWaitForSingleObject(&intr->ni_dpcevt, 0, 0, FALSE, NULL);
2149 KeResetEvent(&intr->ni_dpcevt);
2153 NdisMRegisterAdapterShutdownHandler(ndis_handle adapter, void *shutdownctx,
2154 ndis_shutdown_handler shutdownfunc)
2156 ndis_miniport_block *block;
2157 ndis_miniport_characteristics *chars;
2158 struct ndis_softc *sc;
2160 if (adapter == NULL)
2163 block = (ndis_miniport_block *)adapter;
2164 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2165 chars = sc->ndis_chars;
2167 chars->nmc_shutdown_handler = shutdownfunc;
2168 chars->nmc_rsvd0 = shutdownctx;
2172 NdisMDeregisterAdapterShutdownHandler(ndis_handle adapter)
2174 ndis_miniport_block *block;
2175 ndis_miniport_characteristics *chars;
2176 struct ndis_softc *sc;
2178 if (adapter == NULL)
2181 block = (ndis_miniport_block *)adapter;
2182 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2183 chars = sc->ndis_chars;
2185 chars->nmc_shutdown_handler = NULL;
2186 chars->nmc_rsvd0 = NULL;
2190 NDIS_BUFFER_TO_SPAN_PAGES(ndis_buffer *buf)
2194 if (MmGetMdlByteCount(buf) == 0)
2196 return (SPAN_PAGES(MmGetMdlVirtualAddress(buf),
2197 MmGetMdlByteCount(buf)));
2201 NdisGetBufferPhysicalArraySize(ndis_buffer *buf, uint32_t *pages)
2206 *pages = NDIS_BUFFER_TO_SPAN_PAGES(buf);
2210 NdisQueryBufferOffset(ndis_buffer *buf, uint32_t *off, uint32_t *len)
2215 *off = MmGetMdlByteOffset(buf);
2216 *len = MmGetMdlByteCount(buf);
2220 NdisMSleep(uint32_t usecs)
2225 * During system bootstrap, (i.e. cold == 1), we aren't
2226 * allowed to sleep, so we have to do a hard DELAY()
2233 KeInitializeTimer(&timer);
2234 KeSetTimer(&timer, ((int64_t)usecs * -10), NULL);
2235 KeWaitForSingleObject(&timer, 0, 0, FALSE, NULL);
2240 NdisReadPcmciaAttributeMemory(ndis_handle handle, uint32_t offset, void *buf,
2243 struct ndis_softc *sc;
2244 ndis_miniport_block *block;
2245 bus_space_handle_t bh;
2253 block = (ndis_miniport_block *)handle;
2254 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2257 bh = rman_get_bushandle(sc->ndis_res_am);
2258 bt = rman_get_bustag(sc->ndis_res_am);
2260 for (i = 0; i < len; i++)
2261 dest[i] = bus_space_read_1(bt, bh, (offset + i) * 2);
2267 NdisWritePcmciaAttributeMemory(ndis_handle handle, uint32_t offset, void *buf,
2270 struct ndis_softc *sc;
2271 ndis_miniport_block *block;
2272 bus_space_handle_t bh;
2280 block = (ndis_miniport_block *)handle;
2281 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2284 bh = rman_get_bushandle(sc->ndis_res_am);
2285 bt = rman_get_bustag(sc->ndis_res_am);
2287 for (i = 0; i < len; i++)
2288 bus_space_write_1(bt, bh, (offset + i) * 2, src[i]);
2294 NdisInterlockedInsertHeadList(list_entry *head, list_entry *entry,
2295 ndis_spin_lock *lock)
2299 KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
2300 flink = head->nle_flink;
2301 entry->nle_flink = flink;
2302 entry->nle_blink = head;
2303 flink->nle_blink = entry;
2304 head->nle_flink = entry;
2305 KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
2311 NdisInterlockedRemoveHeadList(list_entry *head, ndis_spin_lock *lock)
2316 KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
2317 entry = head->nle_flink;
2318 flink = entry->nle_flink;
2319 head->nle_flink = flink;
2320 flink->nle_blink = head;
2321 KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
2327 NdisInterlockedInsertTailList(list_entry *head, list_entry *entry,
2328 ndis_spin_lock *lock)
2332 KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
2333 blink = head->nle_blink;
2334 entry->nle_flink = head;
2335 entry->nle_blink = blink;
2336 blink->nle_flink = entry;
2337 head->nle_blink = entry;
2338 KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
2344 NdisMSynchronizeWithInterrupt(ndis_miniport_interrupt *intr, void *syncfunc,
2347 return (KeSynchronizeExecution(intr->ni_introbj, syncfunc, syncctx));
2351 NdisGetCurrentSystemTime(uint64_t *tval)
2353 ntoskrnl_time(tval);
2357 * Return the number of milliseconds since the system booted.
2360 NdisGetSystemUpTime(uint32_t *tval)
2365 *tval = ts.tv_nsec / 1000000 + ts.tv_sec * 1000;
2369 NdisInitializeString(unicode_string *dst, char *src)
2372 RtlInitAnsiString(&as, src);
2373 RtlAnsiStringToUnicodeString(dst, &as, TRUE);
2377 NdisFreeString(unicode_string *str)
2379 RtlFreeUnicodeString(str);
2383 NdisMRemoveMiniport(ndis_handle *adapter)
2385 return (NDIS_STATUS_SUCCESS);
2389 NdisInitAnsiString(ansi_string *dst, char *src)
2391 RtlInitAnsiString(dst, src);
2395 NdisInitUnicodeString(unicode_string *dst, uint16_t *src)
2397 RtlInitUnicodeString(dst, src);
2400 static void NdisMGetDeviceProperty(ndis_handle adapter,
2401 device_object **phydevobj, device_object **funcdevobj,
2402 device_object **nextdevobj, cm_resource_list *resources,
2403 cm_resource_list *transresources)
2405 ndis_miniport_block *block;
2407 block = (ndis_miniport_block *)adapter;
2409 if (phydevobj != NULL)
2410 *phydevobj = block->nmb_physdeviceobj;
2411 if (funcdevobj != NULL)
2412 *funcdevobj = block->nmb_deviceobj;
2413 if (nextdevobj != NULL)
2414 *nextdevobj = block->nmb_nextdeviceobj;
2418 NdisGetFirstBufferFromPacket(ndis_packet *packet, ndis_buffer **buf,
2419 void **firstva, uint32_t *firstlen, uint32_t *totlen)
2423 tmp = packet->np_private.npp_head;
2427 *firstlen = *totlen = 0;
2429 *firstva = MmGetMdlVirtualAddress(tmp);
2430 *firstlen = *totlen = MmGetMdlByteCount(tmp);
2431 for (tmp = tmp->mdl_next; tmp != NULL; tmp = tmp->mdl_next)
2432 *totlen += MmGetMdlByteCount(tmp);
2437 NdisGetFirstBufferFromPacketSafe(ndis_packet *packet, ndis_buffer **buf,
2438 void **firstva, uint32_t *firstlen, uint32_t *totlen, uint32_t prio)
2440 NdisGetFirstBufferFromPacket(packet, buf, firstva, firstlen, totlen);
2444 ndis_find_sym(linker_file_t lf, char *filename, char *suffix, caddr_t *sym)
2450 fullsym = ExAllocatePoolWithTag(NonPagedPool, MAXPATHLEN, 0);
2451 if (fullsym == NULL)
2454 bzero(fullsym, MAXPATHLEN);
2455 strncpy(fullsym, filename, MAXPATHLEN);
2456 if (strlen(filename) < 4) {
2457 ExFreePool(fullsym);
2461 /* If the filename has a .ko suffix, strip if off. */
2462 suf = fullsym + (strlen(filename) - 3);
2463 if (strcmp(suf, ".ko") == 0)
2466 for (i = 0; i < strlen(fullsym); i++) {
2467 if (fullsym[i] == '.')
2470 fullsym[i] = tolower(fullsym[i]);
2472 strcat(fullsym, suffix);
2473 linker_file_lookup_symbol(lf, fullsym, 0, sym);
2474 ExFreePool(fullsym);
2481 struct ndis_checkmodule {
2487 * See if a single module contains the symbols for a specified file.
2490 NdisCheckModule(linker_file_t lf, void *context)
2492 struct ndis_checkmodule *nc;
2493 caddr_t kldstart, kldend;
2495 nc = (struct ndis_checkmodule *)context;
2496 if (ndis_find_sym(lf, nc->afilename, "_start", &kldstart))
2498 if (ndis_find_sym(lf, nc->afilename, "_end", &kldend))
2501 nc->fh->nf_map = NULL;
2502 nc->fh->nf_type = NDIS_FH_TYPE_MODULE;
2503 nc->fh->nf_maplen = (kldend - kldstart) & 0xFFFFFFFF;
2507 /* can also return NDIS_STATUS_RESOURCES/NDIS_STATUS_ERROR_READING_FILE */
2509 NdisOpenFile(ndis_status *status, ndis_handle *filehandle,
2510 uint32_t *filelength, unicode_string *filename, ndis_physaddr highestaddr)
2513 char *afilename = NULL;
2514 struct thread *td = curthread;
2515 struct nlookupdata nd;
2518 struct vattr *vap = &vat;
2521 struct ndis_checkmodule nc;
2524 if (RtlUnicodeStringToAnsiString(&as, filename, TRUE)) {
2525 *status = NDIS_STATUS_RESOURCES;
2529 afilename = kstrdup(as.as_buf, M_DEVBUF);
2530 RtlFreeAnsiString(&as);
2532 fh = ExAllocatePoolWithTag(NonPagedPool, sizeof(ndis_fh), 0);
2534 kfree(afilename, M_DEVBUF);
2535 *status = NDIS_STATUS_RESOURCES;
2539 fh->nf_name = afilename;
2542 * During system bootstrap, it's impossible to load files
2543 * from the rootfs since it's not mounted yet. We therefore
2544 * offer the possibility of opening files that have been
2545 * preloaded as modules instead. Both choices will work
2546 * when kldloading a module from multiuser, but only the
2547 * module option will work during bootstrap. The module
2548 * loading option works by using the ndiscvt(8) utility
2549 * to convert the arbitrary file into a .ko using objcopy(1).
2550 * This file will contain two special symbols: filename_start
2551 * and filename_end. All we have to do is traverse the KLD
2552 * list in search of those symbols and we've found the file
2553 * data. As an added bonus, ndiscvt(8) will also generate
2554 * a normal .o file which can be linked statically with
2555 * the kernel. This means that the symbols will actual reside
2556 * in the kernel's symbol table, but that doesn't matter to
2557 * us since the kernel appears to us as just another module.
2560 nc.afilename = afilename;
2562 if (linker_file_foreach(NdisCheckModule, &nc)) {
2563 *filelength = fh->nf_maplen;
2565 *status = NDIS_STATUS_SUCCESS;
2569 if (mountlist_boot_getfirst() == NULL) {
2571 *status = NDIS_STATUS_FILE_NOT_FOUND;
2572 kprintf("NDIS: could not find file %s in linker list\n",
2574 kprintf("NDIS: and no filesystems mounted yet, "
2575 "aborting NdisOpenFile()\n");
2576 kfree(afilename, M_DEVBUF);
2580 path = ExAllocatePoolWithTag(NonPagedPool, MAXPATHLEN, 0);
2583 kfree(afilename, M_DEVBUF);
2584 *status = NDIS_STATUS_RESOURCES;
2588 ksnprintf(path, MAXPATHLEN, "%s/%s", ndis_filepath, afilename);
2590 /* Some threads don't have a current working directory. */
2592 if (td->td_proc && td->td_proc->p_fd->fd_rdir == NULL)
2593 td->td_proc->p_fd->fd_rdir = rootvnode;
2594 if (td->td_proc && td->td_proc->p_fd->fd_cdir == NULL)
2595 td->td_proc->p_fd->fd_cdir = rootvnode;
2597 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
2600 error = vn_open(&nd, NULL, FREAD, 0);
2607 nd.nl_open_vp = NULL;
2609 /* Get the file size. */
2610 VOP_GETATTR(vp, vap);
2613 fh->nf_vp = nd.nl_open_vp;
2615 fh->nf_type = NDIS_FH_TYPE_VFS;
2617 *filelength = fh->nf_maplen = vap->va_size & 0xFFFFFFFF;
2618 *status = NDIS_STATUS_SUCCESS;
2625 *status = NDIS_STATUS_FILE_NOT_FOUND;
2627 kprintf("NDIS: open file %s failed: %d\n", path, error);
2629 kfree(afilename, M_DEVBUF);
2633 NdisMapFile(ndis_status *status, void **mappedbuffer, ndis_handle filehandle)
2636 struct thread *td = curthread;
2642 if (filehandle == NULL) {
2643 *status = NDIS_STATUS_FAILURE;
2647 fh = (ndis_fh *)filehandle;
2649 if (fh->nf_vp == NULL) {
2650 *status = NDIS_STATUS_FAILURE;
2654 if (fh->nf_map != NULL) {
2655 *status = NDIS_STATUS_ALREADY_MAPPED;
2659 if (fh->nf_type == NDIS_FH_TYPE_MODULE) {
2661 if (ndis_find_sym(lf, fh->nf_name, "_start", &kldstart)) {
2662 *status = NDIS_STATUS_FAILURE;
2665 fh->nf_map = kldstart;
2666 *status = NDIS_STATUS_SUCCESS;
2667 *mappedbuffer = fh->nf_map;
2671 fh->nf_map = ExAllocatePoolWithTag(NonPagedPool, fh->nf_maplen, 0);
2673 if (fh->nf_map == NULL) {
2674 *status = NDIS_STATUS_RESOURCES;
2679 error = vn_rdwr(UIO_READ, vp, fh->nf_map, fh->nf_maplen, 0,
2680 UIO_SYSSPACE, 0, td->td_ucred, &resid);
2683 *status = NDIS_STATUS_FAILURE;
2685 *status = NDIS_STATUS_SUCCESS;
2686 *mappedbuffer = fh->nf_map;
2691 NdisUnmapFile(ndis_handle filehandle)
2694 fh = (ndis_fh *)filehandle;
2696 if (fh->nf_map == NULL)
2699 if (fh->nf_type == NDIS_FH_TYPE_VFS)
2700 ExFreePool(fh->nf_map);
2705 NdisCloseFile(ndis_handle filehandle)
2710 if (filehandle == NULL)
2713 fh = (ndis_fh *)filehandle;
2714 if (fh->nf_map != NULL) {
2715 if (fh->nf_type == NDIS_FH_TYPE_VFS)
2716 ExFreePool(fh->nf_map);
2720 if (fh->nf_vp == NULL)
2723 if (fh->nf_type == NDIS_FH_TYPE_VFS) {
2725 vn_close(vp, FREAD);
2729 kfree(fh->nf_name, M_DEVBUF);
2734 NdisSystemProcessorCount(void)
2740 NdisGetCurrentProcessorCounts(uint32_t *idle_count, uint32_t *kernel_and_user,
2743 #if 0 /* XXX swildner */
2746 pcpu = pcpu_find(curthread->td_oncpu);
2747 *index = pcpu->pc_cpuid;
2748 *idle_count = pcpu->pc_cp_time[CP_IDLE];
2749 *kernel_and_user = pcpu->pc_cp_time[CP_INTR];
2751 panic("%s", __func__);
2755 typedef void (*ndis_statusdone_handler)(ndis_handle);
2756 typedef void (*ndis_status_handler)(ndis_handle, ndis_status,
2760 NdisMIndicateStatusComplete(ndis_handle adapter)
2762 ndis_miniport_block *block;
2763 ndis_statusdone_handler statusdonefunc;
2765 block = (ndis_miniport_block *)adapter;
2766 statusdonefunc = block->nmb_statusdone_func;
2768 MSCALL1(statusdonefunc, adapter);
2772 NdisMIndicateStatus(ndis_handle adapter, ndis_status status, void *sbuf,
2775 ndis_miniport_block *block;
2776 ndis_status_handler statusfunc;
2778 block = (ndis_miniport_block *)adapter;
2779 statusfunc = block->nmb_status_func;
2781 MSCALL4(statusfunc, adapter, status, sbuf, slen);
2785 * The DDK documentation says that you should use IoQueueWorkItem()
2786 * instead of ExQueueWorkItem(). The problem is, IoQueueWorkItem()
2787 * is fundamentally incompatible with NdisScheduleWorkItem(), which
2788 * depends on the API semantics of ExQueueWorkItem(). In our world,
2789 * ExQueueWorkItem() is implemented on top of IoAllocateQueueItem()
2792 * There are actually three distinct APIs here. NdisScheduleWorkItem()
2793 * takes a pointer to an NDIS_WORK_ITEM. ExQueueWorkItem() takes a pointer
2794 * to a WORK_QUEUE_ITEM. And finally, IoQueueWorkItem() takes a pointer
2795 * to an opaque work item thingie which you get from IoAllocateWorkItem().
2796 * An NDIS_WORK_ITEM is not the same as a WORK_QUEUE_ITEM. However,
2797 * the NDIS_WORK_ITEM has some opaque storage at the end of it, and we
2798 * (ab)use this storage as a WORK_QUEUE_ITEM, which is what we submit
2799 * to ExQueueWorkItem().
2801 * Got all that? (Sheesh.)
2805 NdisScheduleWorkItem(ndis_work_item *work)
2807 work_queue_item *wqi;
2809 wqi = (work_queue_item *)work->nwi_wraprsvd;
2810 ExInitializeWorkItem(wqi,
2811 (work_item_func)work->nwi_func, work->nwi_ctx);
2812 ExQueueWorkItem(wqi, WORKQUEUE_DELAYED);
2814 return (NDIS_STATUS_SUCCESS);
2818 NdisCopyFromPacketToPacket(ndis_packet *dpkt, uint32_t doff, uint32_t reqlen,
2819 ndis_packet *spkt, uint32_t soff, uint32_t *cpylen)
2821 ndis_buffer *src, *dst;
2823 int resid, copied, len, scnt, dcnt;
2827 src = spkt->np_private.npp_head;
2828 dst = dpkt->np_private.npp_head;
2830 sptr = MmGetMdlVirtualAddress(src);
2831 dptr = MmGetMdlVirtualAddress(dst);
2832 scnt = MmGetMdlByteCount(src);
2833 dcnt = MmGetMdlByteCount(dst);
2836 if (MmGetMdlByteCount(src) > soff) {
2838 scnt = MmGetMdlByteCount(src)- soff;
2841 soff -= MmGetMdlByteCount(src);
2842 src = src->mdl_next;
2845 sptr = MmGetMdlVirtualAddress(src);
2849 if (MmGetMdlByteCount(dst) > doff) {
2851 dcnt = MmGetMdlByteCount(dst) - doff;
2854 doff -= MmGetMdlByteCount(dst);
2855 dst = dst->mdl_next;
2858 dptr = MmGetMdlVirtualAddress(dst);
2872 bcopy(sptr, dptr, len);
2881 dst = dst->mdl_next;
2884 dptr = MmGetMdlVirtualAddress(dst);
2885 dcnt = MmGetMdlByteCount(dst);
2890 src = src->mdl_next;
2893 sptr = MmGetMdlVirtualAddress(src);
2894 scnt = MmGetMdlByteCount(src);
2902 NdisCopyFromPacketToPacketSafe(ndis_packet *dpkt, uint32_t doff,
2903 uint32_t reqlen, ndis_packet *spkt, uint32_t soff, uint32_t *cpylen,
2906 NdisCopyFromPacketToPacket(dpkt, doff, reqlen, spkt, soff, cpylen);
2910 NdisIMCopySendPerPacketInfo(ndis_packet *dpkt, ndis_packet *spkt)
2912 memcpy(&dpkt->np_ext, &spkt->np_ext, sizeof(ndis_packet_extension));
2916 NdisMRegisterDevice(ndis_handle handle, unicode_string *devname,
2917 unicode_string *symname, driver_dispatch *majorfuncs[], void **devobj,
2918 ndis_handle *devhandle)
2921 device_object *dobj;
2923 status = IoCreateDevice(handle, 0, devname,
2924 FILE_DEVICE_UNKNOWN, 0, FALSE, &dobj);
2926 if (status == STATUS_SUCCESS) {
2935 NdisMDeregisterDevice(ndis_handle handle)
2937 IoDeleteDevice(handle);
2938 return (NDIS_STATUS_SUCCESS);
2942 NdisMQueryAdapterInstanceName(unicode_string *name, ndis_handle handle)
2944 ndis_miniport_block *block;
2948 block = (ndis_miniport_block *)handle;
2949 dev = block->nmb_physdeviceobj->do_devext;
2951 RtlInitAnsiString(&as, __DECONST(char *, device_get_nameunit(dev)));
2952 if (RtlAnsiStringToUnicodeString(name, &as, TRUE))
2953 return (NDIS_STATUS_RESOURCES);
2955 return (NDIS_STATUS_SUCCESS);
2959 NdisMRegisterUnloadHandler(ndis_handle handle, void *func)
2966 kprintf("NDIS dummy called...\n");
2970 * Note: a couple of entries in this table specify the
2971 * number of arguments as "foo + 1". These are routines
2972 * that accept a 64-bit argument, passed by value. On
2973 * x86, these arguments consume two longwords on the stack,
2974 * so we lie and say there's one additional argument so
2975 * that the wrapping routines will do the right thing.
2978 image_patch_table ndis_functbl[] = {
2979 IMPORT_SFUNC(NdisCopyFromPacketToPacket, 6),
2980 IMPORT_SFUNC(NdisCopyFromPacketToPacketSafe, 7),
2981 IMPORT_SFUNC(NdisIMCopySendPerPacketInfo, 2),
2982 IMPORT_SFUNC(NdisScheduleWorkItem, 1),
2983 IMPORT_SFUNC(NdisMIndicateStatusComplete, 1),
2984 IMPORT_SFUNC(NdisMIndicateStatus, 4),
2985 IMPORT_SFUNC(NdisSystemProcessorCount, 0),
2986 IMPORT_SFUNC(NdisGetCurrentProcessorCounts, 3),
2987 IMPORT_SFUNC(NdisUnchainBufferAtBack, 2),
2988 IMPORT_SFUNC(NdisGetFirstBufferFromPacket, 5),
2989 IMPORT_SFUNC(NdisGetFirstBufferFromPacketSafe, 6),
2990 IMPORT_SFUNC(NdisGetBufferPhysicalArraySize, 2),
2991 IMPORT_SFUNC(NdisMGetDeviceProperty, 6),
2992 IMPORT_SFUNC(NdisInitAnsiString, 2),
2993 IMPORT_SFUNC(NdisInitUnicodeString, 2),
2994 IMPORT_SFUNC(NdisWriteConfiguration, 4),
2995 IMPORT_SFUNC(NdisAnsiStringToUnicodeString, 2),
2996 IMPORT_SFUNC(NdisTerminateWrapper, 2),
2997 IMPORT_SFUNC(NdisOpenConfigurationKeyByName, 4),
2998 IMPORT_SFUNC(NdisOpenConfigurationKeyByIndex, 5),
2999 IMPORT_SFUNC(NdisMRemoveMiniport, 1),
3000 IMPORT_SFUNC(NdisInitializeString, 2),
3001 IMPORT_SFUNC(NdisFreeString, 1),
3002 IMPORT_SFUNC(NdisGetCurrentSystemTime, 1),
3003 IMPORT_SFUNC(NdisGetRoutineAddress, 1),
3004 IMPORT_SFUNC(NdisGetSystemUpTime, 1),
3005 IMPORT_SFUNC(NdisGetVersion, 0),
3006 IMPORT_SFUNC(NdisMSynchronizeWithInterrupt, 3),
3007 IMPORT_SFUNC(NdisMAllocateSharedMemoryAsync, 4),
3008 IMPORT_SFUNC(NdisInterlockedInsertHeadList, 3),
3009 IMPORT_SFUNC(NdisInterlockedInsertTailList, 3),
3010 IMPORT_SFUNC(NdisInterlockedRemoveHeadList, 2),
3011 IMPORT_SFUNC(NdisInitializeWrapper, 4),
3012 IMPORT_SFUNC(NdisMRegisterMiniport, 3),
3013 IMPORT_SFUNC(NdisAllocateMemoryWithTag, 3),
3014 IMPORT_SFUNC(NdisAllocateMemory, 4 + 1),
3015 IMPORT_SFUNC(NdisMSetAttributesEx, 5),
3016 IMPORT_SFUNC(NdisCloseConfiguration, 1),
3017 IMPORT_SFUNC(NdisReadConfiguration, 5),
3018 IMPORT_SFUNC(NdisOpenConfiguration, 3),
3019 IMPORT_SFUNC(NdisAcquireSpinLock, 1),
3020 IMPORT_SFUNC(NdisReleaseSpinLock, 1),
3021 IMPORT_SFUNC(NdisDprAcquireSpinLock, 1),
3022 IMPORT_SFUNC(NdisDprReleaseSpinLock, 1),
3023 IMPORT_SFUNC(NdisAllocateSpinLock, 1),
3024 IMPORT_SFUNC(NdisInitializeReadWriteLock, 1),
3025 IMPORT_SFUNC(NdisAcquireReadWriteLock, 3),
3026 IMPORT_SFUNC(NdisReleaseReadWriteLock, 2),
3027 IMPORT_SFUNC(NdisFreeSpinLock, 1),
3028 IMPORT_SFUNC(NdisFreeMemory, 3),
3029 IMPORT_SFUNC(NdisReadPciSlotInformation, 5),
3030 IMPORT_SFUNC(NdisWritePciSlotInformation, 5),
3031 IMPORT_SFUNC_MAP(NdisImmediateReadPciSlotInformation,
3032 NdisReadPciSlotInformation, 5),
3033 IMPORT_SFUNC_MAP(NdisImmediateWritePciSlotInformation,
3034 NdisWritePciSlotInformation, 5),
3035 IMPORT_CFUNC(NdisWriteErrorLogEntry, 0),
3036 IMPORT_SFUNC(NdisMStartBufferPhysicalMapping, 6),
3037 IMPORT_SFUNC(NdisMCompleteBufferPhysicalMapping, 3),
3038 IMPORT_SFUNC(NdisMInitializeTimer, 4),
3039 IMPORT_SFUNC(NdisInitializeTimer, 3),
3040 IMPORT_SFUNC(NdisSetTimer, 2),
3041 IMPORT_SFUNC(NdisMCancelTimer, 2),
3042 IMPORT_SFUNC_MAP(NdisCancelTimer, NdisMCancelTimer, 2),
3043 IMPORT_SFUNC(NdisMSetPeriodicTimer, 2),
3044 IMPORT_SFUNC(NdisMQueryAdapterResources, 4),
3045 IMPORT_SFUNC(NdisMRegisterIoPortRange, 4),
3046 IMPORT_SFUNC(NdisMDeregisterIoPortRange, 4),
3047 IMPORT_SFUNC(NdisReadNetworkAddress, 4),
3048 IMPORT_SFUNC(NdisQueryMapRegisterCount, 2),
3049 IMPORT_SFUNC(NdisMAllocateMapRegisters, 5),
3050 IMPORT_SFUNC(NdisMFreeMapRegisters, 1),
3051 IMPORT_SFUNC(NdisMAllocateSharedMemory, 5),
3052 IMPORT_SFUNC(NdisMMapIoSpace, 4 + 1),
3053 IMPORT_SFUNC(NdisMUnmapIoSpace, 3),
3054 IMPORT_SFUNC(NdisGetCacheFillSize, 0),
3055 IMPORT_SFUNC(NdisMGetDmaAlignment, 1),
3056 IMPORT_SFUNC(NdisMInitializeScatterGatherDma, 3),
3057 IMPORT_SFUNC(NdisAllocatePacketPool, 4),
3058 IMPORT_SFUNC(NdisAllocatePacketPoolEx, 5),
3059 IMPORT_SFUNC(NdisAllocatePacket, 3),
3060 IMPORT_SFUNC(NdisFreePacket, 1),
3061 IMPORT_SFUNC(NdisFreePacketPool, 1),
3062 IMPORT_SFUNC_MAP(NdisDprAllocatePacket, NdisAllocatePacket, 3),
3063 IMPORT_SFUNC_MAP(NdisDprFreePacket, NdisFreePacket, 1),
3064 IMPORT_SFUNC(NdisAllocateBufferPool, 3),
3065 IMPORT_SFUNC(NdisAllocateBuffer, 5),
3066 IMPORT_SFUNC(NdisQueryBuffer, 3),
3067 IMPORT_SFUNC(NdisQueryBufferSafe, 4),
3068 IMPORT_SFUNC(NdisBufferVirtualAddress, 1),
3069 IMPORT_SFUNC(NdisBufferVirtualAddressSafe, 2),
3070 IMPORT_SFUNC(NdisBufferLength, 1),
3071 IMPORT_SFUNC(NdisFreeBuffer, 1),
3072 IMPORT_SFUNC(NdisFreeBufferPool, 1),
3073 IMPORT_SFUNC(NdisInterlockedIncrement, 1),
3074 IMPORT_SFUNC(NdisInterlockedDecrement, 1),
3075 IMPORT_SFUNC(NdisInitializeEvent, 1),
3076 IMPORT_SFUNC(NdisSetEvent, 1),
3077 IMPORT_SFUNC(NdisResetEvent, 1),
3078 IMPORT_SFUNC(NdisWaitEvent, 2),
3079 IMPORT_SFUNC(NdisUnicodeStringToAnsiString, 2),
3080 IMPORT_SFUNC(NdisMPciAssignResources, 3),
3081 IMPORT_SFUNC(NdisMFreeSharedMemory, 5 + 1),
3082 IMPORT_SFUNC(NdisMRegisterInterrupt, 7),
3083 IMPORT_SFUNC(NdisMDeregisterInterrupt, 1),
3084 IMPORT_SFUNC(NdisMRegisterAdapterShutdownHandler, 3),
3085 IMPORT_SFUNC(NdisMDeregisterAdapterShutdownHandler, 1),
3086 IMPORT_SFUNC(NDIS_BUFFER_TO_SPAN_PAGES, 1),
3087 IMPORT_SFUNC(NdisQueryBufferOffset, 3),
3088 IMPORT_SFUNC(NdisAdjustBufferLength, 2),
3089 IMPORT_SFUNC(NdisPacketPoolUsage, 1),
3090 IMPORT_SFUNC(NdisMSleep, 1),
3091 IMPORT_SFUNC(NdisUnchainBufferAtFront, 2),
3092 IMPORT_SFUNC(NdisReadPcmciaAttributeMemory, 4),
3093 IMPORT_SFUNC(NdisWritePcmciaAttributeMemory, 4),
3094 IMPORT_SFUNC(NdisOpenFile, 5 + 1),
3095 IMPORT_SFUNC(NdisMapFile, 3),
3096 IMPORT_SFUNC(NdisUnmapFile, 1),
3097 IMPORT_SFUNC(NdisCloseFile, 1),
3098 IMPORT_SFUNC(NdisMRegisterDevice, 6),
3099 IMPORT_SFUNC(NdisMDeregisterDevice, 1),
3100 IMPORT_SFUNC(NdisMQueryAdapterInstanceName, 2),
3101 IMPORT_SFUNC(NdisMRegisterUnloadHandler, 2),
3102 IMPORT_SFUNC(ndis_timercall, 4),
3103 IMPORT_SFUNC(ndis_asyncmem_complete, 2),
3104 IMPORT_SFUNC(ndis_intr, 2),
3105 IMPORT_SFUNC(ndis_intrhand, 4),
3108 * This last entry is a catch-all for any function we haven't
3109 * implemented yet. The PE import list patching routine will
3110 * use it for any function that doesn't have an explicit match
3114 { NULL, (FUNC)dummy, NULL, 0, WINDRV_WRAP_STDCALL },
3118 { NULL, NULL, NULL }