kernel: Fix some void functions returning the results of void functions.
[dragonfly.git] / sys / emulation / ndis / subr_ndis.c
1 /*-
2  * Copyright (c) 2003
3  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
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.
31  *
32  * $FreeBSD: src/sys/compat/ndis/subr_ndis.c,v 1.125 2010/12/06 20:54:53 bschmidt Exp $
33  */
34
35 /*
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.
43  *
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
47  * expects.
48  */
49
50 #include "use_usb4bsd.h"
51
52 #include <sys/ctype.h>
53 #include <sys/param.h>
54 #include <sys/types.h>
55 #include <sys/errno.h>
56
57 #include <sys/callout.h>
58 #include <sys/kernel.h>
59 #include <sys/systm.h>
60 #include <sys/malloc.h>
61 #include <sys/lock.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>
67 #include <sys/proc.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>
76
77 #include <net/if.h>
78 #include <net/if_arp.h>
79 #include <net/ethernet.h>
80 #include <net/if_dl.h>
81 #include <net/if_media.h>
82
83 #include <machine/atomic.h>
84
85 #include <sys/bus.h>
86 #include <sys/rman.h>
87 #include <sys/mplock2.h>
88
89 #include <netproto/802_11/ieee80211_var.h>
90 #include <netproto/802_11/ieee80211_ioctl.h>
91
92 #include <bus/pci/pcireg.h>
93 #include <bus/pci/pcivar.h>
94 #if NUSB4BSD > 0
95 #include <bus/u4b/usb.h>
96 #include <bus/u4b/usbdi.h>
97 #else
98 #include <bus/usb/usb.h>
99 #include <bus/usb/usbdi.h>
100 #endif
101
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>
109
110 #include <vm/vm.h>
111 #include <vm/vm_param.h>
112 #include <vm/pmap.h>
113 #include <vm/vm_kern.h>
114 #include <vm/vm_map.h>
115
116 #include <stdarg.h>
117
118 static char ndis_filepath[MAXPATHLEN];
119
120 SYSCTL_STRING(_hw, OID_AUTO, ndis_filepath, CTLFLAG_RW, ndis_filepath,
121         MAXPATHLEN, "Path used by NdisOpenFile() to search for files");
122
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 **,
128         uint32_t, uint32_t);
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 *,
177         void *, void *);
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,
205         uint8_t, uint32_t);
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 *,
228         unicode_string *);
229 static ndis_status
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 *,
242         uint32_t *);
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 *,
252         ndis_spin_lock *);
253 static list_entry *NdisInterlockedInsertTailList(list_entry *,
254         list_entry *, ndis_spin_lock *);
255 static uint8_t
256         NdisMSynchronizeWithInterrupt(ndis_miniport_interrupt *,
257         void *, void *);
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 *,
269         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,
284         void *, uint32_t);
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);
297 static ndis_status
298         NdisMQueryAdapterInstanceName(unicode_string *, ndis_handle);
299 static void NdisMRegisterUnloadHandler(ndis_handle, void *);
300 static void dummy(void);
301
302 /*
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.
309  */
310 #define NDIS_POOL_EXTRA         16
311
312 int
313 ndis_libinit(void)
314 {
315         image_patch_table       *patch;
316
317         strcpy(ndis_filepath, "/compat/ndis");
318
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);
324                 patch++;
325         }
326
327         return (0);
328 }
329
330 int
331 ndis_libfini(void)
332 {
333         image_patch_table       *patch;
334
335         patch = ndis_functbl;
336         while (patch->ipt_func != NULL) {
337                 windrv_unwrap(patch->ipt_wrap);
338                 patch++;
339         }
340
341         return (0);
342 }
343
344 static funcptr
345 ndis_findwrap(funcptr func)
346 {
347         image_patch_table       *patch;
348
349         patch = ndis_functbl;
350         while (patch->ipt_func != NULL) {
351                 if ((funcptr)patch->ipt_func == func)
352                         return((funcptr)patch->ipt_wrap);
353                 patch++;
354         }
355
356         return (NULL);
357 }
358
359 /*
360  * This routine does the messy Windows Driver Model device attachment
361  * stuff on behalf of NDIS drivers. We register our own AddDevice
362  * routine here
363  */
364 static void
365 NdisInitializeWrapper(ndis_handle *wrapper, driver_object *drv, void *path,
366     void *unused)
367 {
368         /*
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.
376          */
377
378         *wrapper = drv;
379
380         /*
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
385          * instance appears.
386          */
387
388         drv->dro_driverext->dre_adddevicefunc = NdisAddDevice;
389 }
390
391 static void
392 NdisTerminateWrapper(ndis_handle handle, void *syspec)
393 {
394         /* Nothing to see here, move along. */
395 }
396
397 static ndis_status
398 NdisMRegisterMiniport(ndis_handle handle,
399     ndis_miniport_characteristics *characteristics, int len)
400 {
401         ndis_miniport_characteristics   *ch = NULL;
402         driver_object           *drv;
403
404         drv = (driver_object *)handle;
405
406         /*
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()).
414          */
415
416         if (IoAllocateDriverObjectExtension(drv, (void *)1,
417             sizeof(ndis_miniport_characteristics), (void **)&ch) !=
418             STATUS_SUCCESS) {
419                 return (NDIS_STATUS_RESOURCES);
420         }
421
422         bzero((char *)ch, sizeof(ndis_miniport_characteristics));
423
424         bcopy((char *)characteristics, (char *)ch, len);
425
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;
430         }
431
432         return (NDIS_STATUS_SUCCESS);
433 }
434
435 static ndis_status
436 NdisAllocateMemoryWithTag(void **vaddr, uint32_t len, uint32_t tag)
437 {
438         void                    *mem;
439
440         mem = ExAllocatePoolWithTag(NonPagedPool, len, tag);
441         if (mem == NULL) {
442                 return (NDIS_STATUS_RESOURCES);
443         }
444         *vaddr = mem;
445
446         return (NDIS_STATUS_SUCCESS);
447 }
448
449 static ndis_status
450 NdisAllocateMemory(void **vaddr, uint32_t len, uint32_t flags,
451     ndis_physaddr highaddr)
452 {
453         void                    *mem;
454
455         mem = ExAllocatePoolWithTag(NonPagedPool, len, 0);
456         if (mem == NULL)
457                 return (NDIS_STATUS_RESOURCES);
458         *vaddr = mem;
459
460         return (NDIS_STATUS_SUCCESS);
461 }
462
463 static void
464 NdisFreeMemory(void *vaddr, uint32_t len, uint32_t flags)
465 {
466         if (len == 0)
467                 return;
468
469         ExFreePool(vaddr);
470 }
471
472 static ndis_status
473 NdisMSetAttributesEx(ndis_handle adapter_handle, ndis_handle adapter_ctx,
474     uint32_t hangsecs, uint32_t flags, ndis_interface_type iftype)
475 {
476         ndis_miniport_block             *block;
477
478         /*
479          * Save the adapter context, we need it for calling
480          * the driver's internal functions.
481          */
482         block = (ndis_miniport_block *)adapter_handle;
483         block->nmb_miniportadapterctx = adapter_ctx;
484         block->nmb_checkforhangsecs = hangsecs;
485         block->nmb_flags = flags;
486
487         return (NDIS_STATUS_SUCCESS);
488 }
489
490 static void
491 NdisOpenConfiguration(ndis_status *status, ndis_handle *cfg,
492     ndis_handle wrapctx)
493 {
494         *cfg = wrapctx;
495         *status = NDIS_STATUS_SUCCESS;
496 }
497
498 static void
499 NdisOpenConfigurationKeyByName(ndis_status *status, ndis_handle cfg,
500     unicode_string *subkey, ndis_handle *subhandle)
501 {
502         *subhandle = cfg;
503         *status = NDIS_STATUS_SUCCESS;
504 }
505
506 static void
507 NdisOpenConfigurationKeyByIndex(ndis_status *status, ndis_handle cfg,
508     uint32_t idx, unicode_string *subkey, ndis_handle *subhandle)
509 {
510         *status = NDIS_STATUS_FAILURE;
511 }
512
513 static ndis_status
514 ndis_encode_parm(ndis_miniport_block *block, struct sysctl_oid *oid,
515     ndis_parm_type type, ndis_config_parm **parm)
516 {
517         ndis_config_parm        *p;
518         ndis_parmlist_entry     *np;
519         unicode_string          *us;
520         ansi_string             as;
521         int                     base = 0;
522         uint32_t                val;
523         char                    tmp[32];
524
525         np = ExAllocatePoolWithTag(NonPagedPool,
526             sizeof(ndis_parmlist_entry), 0);
527         if (np == NULL)
528                 return (NDIS_STATUS_RESOURCES);
529         InsertHeadList((&block->nmb_parmlist), (&np->np_list));
530         *parm = p = &np->np_parm;
531
532         switch(type) {
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;
538                 if (val) {
539                         ksnprintf(tmp, 32, "%x", val);
540                         RtlInitAnsiString(&as, tmp);
541                 } else {
542                         RtlInitAnsiString(&as, (char *)oid->oid_arg1);
543                 }
544
545                 if (RtlAnsiStringToUnicodeString(us, &as, TRUE)) {
546                         ExFreePool(np);
547                         return (NDIS_STATUS_RESOURCES);
548                 }
549                 break;
550         case ndis_parm_int:
551                 if (strncmp((char *)oid->oid_arg1, "0x", 2) == 0)
552                         base = 16;
553                 else
554                         base = 10;
555                 p->ncp_type = ndis_parm_int;
556                 p->ncp_parmdata.ncp_intdata =
557                     strtol((char *)oid->oid_arg1, NULL, base);
558                 break;
559         case ndis_parm_hexint:
560 #ifdef notdef
561                 if (strncmp((char *)oid->oid_arg1, "0x", 2) == 0)
562                         base = 16;
563                 else
564                         base = 10;
565 #endif
566                 base = 16;
567                 p->ncp_type = ndis_parm_hexint;
568                 p->ncp_parmdata.ncp_intdata =
569                     strtoul((char *)oid->oid_arg1, NULL, base);
570                 break;
571         default:
572                 return (NDIS_STATUS_FAILURE);
573                 break;
574         }
575
576         return (NDIS_STATUS_SUCCESS);
577 }
578
579 static void
580 NdisReadConfiguration(ndis_status *status, ndis_config_parm **parm,
581     ndis_handle cfg, unicode_string *key, ndis_parm_type type)
582 {
583         char                    *keystr = NULL;
584         ndis_miniport_block     *block;
585         struct ndis_softc       *sc;
586         struct sysctl_oid       *oidp;
587         struct sysctl_ctx_entry *e;
588         ansi_string             as;
589
590         block = (ndis_miniport_block *)cfg;
591         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
592
593         if (key->us_len == 0 || key->us_buf == NULL) {
594                 *status = NDIS_STATUS_FAILURE;
595                 return;
596         }
597
598         if (RtlUnicodeStringToAnsiString(&as, key, TRUE)) {
599                 *status = NDIS_STATUS_RESOURCES;
600                 return;
601         }
602
603         keystr = as.as_buf;
604
605         /*
606          * See if registry key is already in a list of known keys
607          * included with the driver.
608          */
609         TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
610                 oidp = e->entry;
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;
615                                 return;
616                         }
617
618                         *status = ndis_encode_parm(block, oidp, type, parm);
619                         RtlFreeAnsiString(&as);
620                         return;
621                 }
622         }
623
624         /*
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
633          * it to "0".
634          */
635
636         if (type == ndis_parm_int || type == ndis_parm_hexint)
637                 ndis_add_sysctl(sc, keystr, "(dynamic integer key)",
638                     "UNSET", CTLFLAG_RW);
639         else
640                 ndis_add_sysctl(sc, keystr, "(dynamic string key)",
641                     "UNSET", CTLFLAG_RW);
642
643         RtlFreeAnsiString(&as);
644         *status = NDIS_STATUS_FAILURE;
645 }
646
647 static ndis_status
648 ndis_decode_parm(ndis_miniport_block *block, ndis_config_parm *parm, char *val)
649 {
650         unicode_string          *ustr;
651         ansi_string             as;
652
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);
660                 break;
661         case ndis_parm_int:
662                 ksprintf(val, "%d", parm->ncp_parmdata.ncp_intdata);
663                 break;
664         case ndis_parm_hexint:
665                 ksprintf(val, "%xu", parm->ncp_parmdata.ncp_intdata);
666                 break;
667         default:
668                 return (NDIS_STATUS_FAILURE);
669                 break;
670         }
671         return (NDIS_STATUS_SUCCESS);
672 }
673
674 static void
675 NdisWriteConfiguration(ndis_status *status, ndis_handle cfg,
676     unicode_string *key, ndis_config_parm *parm)
677 {
678         ansi_string             as;
679         char                    *keystr = NULL;
680         ndis_miniport_block     *block;
681         struct ndis_softc       *sc;
682         struct sysctl_oid       *oidp;
683         struct sysctl_ctx_entry *e;
684         char                    val[256];
685
686         block = (ndis_miniport_block *)cfg;
687         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
688
689         if (RtlUnicodeStringToAnsiString(&as, key, TRUE)) {
690                 *status = NDIS_STATUS_RESOURCES;
691                 return;
692         }
693
694         keystr = as.as_buf;
695
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);
701                 return;
702         }
703
704         /* See if the key already exists. */
705
706         TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
707                 oidp = e->entry;
708                 if (strcasecmp(oidp->oid_name, keystr) == 0) {
709                         /* Found it, set the value. */
710                         strcpy((char *)oidp->oid_arg1, val);
711                         RtlFreeAnsiString(&as);
712                         return;
713                 }
714         }
715
716         /* Not found, add a new key with the specified value. */
717         ndis_add_sysctl(sc, keystr, "(dynamically set key)",
718                     val, CTLFLAG_RW);
719
720         RtlFreeAnsiString(&as);
721         *status = NDIS_STATUS_SUCCESS;
722 }
723
724 static void
725 NdisCloseConfiguration(ndis_handle cfg)
726 {
727         list_entry              *e;
728         ndis_parmlist_entry     *pe;
729         ndis_miniport_block     *block;
730         ndis_config_parm        *p;
731
732         block = (ndis_miniport_block *)cfg;
733
734         while (!IsListEmpty(&block->nmb_parmlist)) {
735                 e = RemoveHeadList(&block->nmb_parmlist);
736                 pe = CONTAINING_RECORD(e, ndis_parmlist_entry, np_list);
737                 p = &pe->np_parm;
738                 if (p->ncp_type == ndis_parm_string)
739                         RtlFreeUnicodeString(&p->ncp_parmdata.ncp_stringdata);
740                 ExFreePool(e);
741         }
742 }
743
744 /*
745  * Initialize a Windows spinlock.
746  */
747 static void
748 NdisAllocateSpinLock(ndis_spin_lock *lock)
749 {
750         KeInitializeSpinLock(&lock->nsl_spinlock);
751         lock->nsl_kirql = 0;
752 }
753
754 /*
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
760  * talking to you.)
761  */
762 static void
763 NdisFreeSpinLock(ndis_spin_lock *lock)
764 {
765 #ifdef notdef
766         KeInitializeSpinLock(&lock->nsl_spinlock);
767         lock->nsl_kirql = 0;
768 #endif
769 }
770
771 /*
772  * Acquire a spinlock from IRQL <= DISPATCH_LEVEL.
773  */
774
775 static void
776 NdisAcquireSpinLock(ndis_spin_lock *lock)
777 {
778         KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
779 }
780
781 /*
782  * Release a spinlock from IRQL == DISPATCH_LEVEL.
783  */
784
785 static void
786 NdisReleaseSpinLock(ndis_spin_lock *lock)
787 {
788         KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
789 }
790
791 /*
792  * Acquire a spinlock when already running at IRQL == DISPATCH_LEVEL.
793  */
794 static void
795 NdisDprAcquireSpinLock(ndis_spin_lock *lock)
796 {
797         KeAcquireSpinLockAtDpcLevel(&lock->nsl_spinlock);
798 }
799
800 /*
801  * Release a spinlock without leaving IRQL == DISPATCH_LEVEL.
802  */
803 static void
804 NdisDprReleaseSpinLock(ndis_spin_lock *lock)
805 {
806         KeReleaseSpinLockFromDpcLevel(&lock->nsl_spinlock);
807 }
808
809 static void
810 NdisInitializeReadWriteLock(ndis_rw_lock *lock)
811 {
812         KeInitializeSpinLock(&lock->nrl_spinlock);
813         bzero((char *)&lock->nrl_rsvd, sizeof(lock->nrl_rsvd));
814 }
815
816 static void
817 NdisAcquireReadWriteLock(ndis_rw_lock *lock, uint8_t writeacc,
818     ndis_lock_state *state)
819 {
820         if (writeacc == TRUE) {
821                 KeAcquireSpinLock(&lock->nrl_spinlock, &state->nls_oldirql);
822                 lock->nrl_rsvd[0]++;
823         } else
824                 lock->nrl_rsvd[1]++;
825 }
826
827 static void
828 NdisReleaseReadWriteLock(ndis_rw_lock *lock, ndis_lock_state *state)
829 {
830         if (lock->nrl_rsvd[0]) {
831                 lock->nrl_rsvd[0]--;
832                 KeReleaseSpinLock(&lock->nrl_spinlock, state->nls_oldirql);
833         } else
834                 lock->nrl_rsvd[1]--;
835 }
836
837 static uint32_t
838 NdisReadPciSlotInformation(ndis_handle adapter, uint32_t slot, uint32_t offset,
839     void *buf, uint32_t len)
840 {
841         ndis_miniport_block     *block;
842         int                     i;
843         char                    *dest;
844         device_t                dev;
845
846         block = (ndis_miniport_block *)adapter;
847         dest = buf;
848         if (block == NULL)
849                 return (0);
850
851         dev = block->nmb_physdeviceobj->do_devext;
852
853         /*
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.
866          */
867
868         for (i = 0; i < len; i++) {
869                 DELAY(1);
870                 dest[i] = pci_read_config(dev, i + offset, 1);
871         }
872
873         return (len);
874 }
875
876 static uint32_t
877 NdisWritePciSlotInformation(ndis_handle adapter, uint32_t slot,
878     uint32_t offset, void *buf, uint32_t len)
879 {
880         ndis_miniport_block     *block;
881         int                     i;
882         char                    *dest;
883         device_t                dev;
884
885         block = (ndis_miniport_block *)adapter;
886         dest = buf;
887
888         if (block == NULL)
889                 return (0);
890
891         dev = block->nmb_physdeviceobj->do_devext;
892         for (i = 0; i < len; i++) {
893                 DELAY(1);
894                 pci_write_config(dev, i + offset, dest[i], 1);
895         }
896
897         return (len);
898 }
899
900 /*
901  * The errorlog routine uses a variable argument list, so we
902  * have to declare it this way.
903  */
904
905 #define ERRMSGLEN 512
906 static void
907 NdisWriteErrorLogEntry(ndis_handle adapter, ndis_error_code code,
908         uint32_t numerrors, ...)
909 {
910         ndis_miniport_block     *block;
911         va_list                 ap;
912         int                     i, error;
913         char                    *str = NULL;
914         uint16_t                flags;
915         device_t                dev;
916         driver_object           *drv;
917         struct ndis_softc       *sc;
918         struct ifnet            *ifp;
919         unicode_string          us;
920         ansi_string             as = { 0, 0, NULL };
921
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);
926         ifp = sc->ifp;
927
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);
931                 if (error == 0) {
932                         if (flags & MESSAGE_RESOURCE_UNICODE) {
933                                 RtlInitUnicodeString(&us, (uint16_t *)str);
934                                 if (RtlUnicodeStringToAnsiString(&as,
935                                     &us, TRUE) == STATUS_SUCCESS)
936                                         str = as.as_buf;
937                                 else
938                                         str = NULL;
939                         }
940                 }
941         }
942
943         device_printf(dev, "NDIS ERROR: %x (%s)\n", code,
944             str == NULL ? "unknown error" : str);
945
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",
951                             va_arg(ap, void *));
952                 va_end(ap);
953         }
954
955         if (as.as_len)
956                 RtlFreeAnsiString(&as);
957 }
958
959 static void
960 ndis_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
961 {
962         struct ndis_map_arg     *ctx;
963         int                     i;
964
965         if (error)
966                 return;
967
968         ctx = arg;
969
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;
973         }
974
975         ctx->nma_cnt = nseg;
976 }
977
978 static void
979 NdisMStartBufferPhysicalMapping(ndis_handle adapter, ndis_buffer *buf,
980     uint32_t mapreg, uint8_t writedev, ndis_paddr_unit *addrarray,
981     uint32_t *arraysize)
982 {
983         ndis_miniport_block     *block;
984         struct ndis_softc       *sc;
985         struct ndis_map_arg     nma;
986         bus_dmamap_t            map;
987         int                     error;
988
989         if (adapter == NULL)
990                 return;
991
992         block = (ndis_miniport_block *)adapter;
993         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
994
995         if (mapreg > sc->ndis_mmapcnt)
996                 return;
997
998         map = sc->ndis_mmaps[mapreg];
999         nma.nma_fraglist = addrarray;
1000
1001         error = bus_dmamap_load(sc->ndis_mtag, map,
1002             MmGetMdlVirtualAddress(buf), MmGetMdlByteCount(buf), ndis_map_cb,
1003             (void *)&nma, BUS_DMA_NOWAIT);
1004
1005         if (error)
1006                 return;
1007
1008         bus_dmamap_sync(sc->ndis_mtag, map,
1009             writedev ? BUS_DMASYNC_PREWRITE : BUS_DMASYNC_PREREAD);
1010
1011         *arraysize = nma.nma_cnt;
1012 }
1013
1014 static void
1015 NdisMCompleteBufferPhysicalMapping(ndis_handle adapter, ndis_buffer *buf,
1016     uint32_t mapreg)
1017 {
1018         ndis_miniport_block     *block;
1019         struct ndis_softc       *sc;
1020         bus_dmamap_t            map;
1021
1022         if (adapter == NULL)
1023                 return;
1024
1025         block = (ndis_miniport_block *)adapter;
1026         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1027
1028         if (mapreg > sc->ndis_mmapcnt)
1029                 return;
1030
1031         map = sc->ndis_mmaps[mapreg];
1032
1033         bus_dmamap_sync(sc->ndis_mtag, map,
1034             BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1035
1036         bus_dmamap_unload(sc->ndis_mtag, map);
1037 }
1038
1039 /*
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.
1043  */
1044
1045 static void
1046 NdisInitializeTimer(ndis_timer *timer, ndis_timer_function func, void *ctx)
1047 {
1048         KeInitializeTimer(&timer->nt_ktimer);
1049         KeInitializeDpc(&timer->nt_kdpc, func, ctx);
1050         KeSetImportanceDpc(&timer->nt_kdpc, KDPC_IMPORTANCE_LOW);
1051 }
1052
1053 static void
1054 ndis_timercall(kdpc *dpc, ndis_miniport_timer *timer, void *sysarg1,
1055     void *sysarg2)
1056 {
1057         /*
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().
1062          */
1063         if (NDIS_SERIALIZED(timer->nmt_block))
1064                 KeAcquireSpinLockAtDpcLevel(&timer->nmt_block->nmb_lock);
1065
1066         MSCALL4(timer->nmt_timerfunc, dpc, timer->nmt_timerctx,
1067             sysarg1, sysarg2);
1068
1069         if (NDIS_SERIALIZED(timer->nmt_block))
1070                 KeReleaseSpinLockFromDpcLevel(&timer->nmt_block->nmb_lock);
1071 }
1072
1073 /*
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.
1089  */
1090 static void
1091 NdisMInitializeTimer(ndis_miniport_timer *timer, ndis_handle handle,
1092     ndis_timer_function func, void *ctx)
1093 {
1094         /* Save the driver's funcptr and context */
1095
1096         timer->nmt_timerfunc = func;
1097         timer->nmt_timerctx = ctx;
1098         timer->nmt_block = handle;
1099
1100         /*
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.
1105          */
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;
1110 }
1111
1112 /*
1113  * In Windows, there's both an NdisMSetTimer() and an NdisSetTimer(),
1114  * but the former is just a macro wrapper around the latter.
1115  */
1116 static void
1117 NdisSetTimer(ndis_timer *timer, uint32_t msecs)
1118 {
1119         /*
1120          * KeSetTimer() wants the period in
1121          * hundred nanosecond intervals.
1122          */
1123         KeSetTimer(&timer->nt_ktimer,
1124             ((int64_t)msecs * -10000), &timer->nt_kdpc);
1125 }
1126
1127 static void
1128 NdisMSetPeriodicTimer(ndis_miniport_timer *timer, uint32_t msecs)
1129 {
1130         KeSetTimerEx(&timer->nmt_ktimer,
1131             ((int64_t)msecs * -10000), msecs, &timer->nmt_kdpc);
1132 }
1133
1134 /*
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.
1139  */
1140
1141 static void
1142 NdisMCancelTimer(ndis_timer *timer, uint8_t *cancelled)
1143 {
1144
1145         *cancelled = KeCancelTimer(&timer->nt_ktimer);
1146 }
1147
1148 static void
1149 NdisMQueryAdapterResources(ndis_status *status, ndis_handle adapter,
1150     ndis_resource_list *list, uint32_t *buflen)
1151 {
1152         ndis_miniport_block     *block;
1153         struct ndis_softc       *sc;
1154         int                     rsclen;
1155
1156         block = (ndis_miniport_block *)adapter;
1157         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1158
1159         rsclen = sizeof(ndis_resource_list) +
1160             (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1));
1161         if (*buflen < rsclen) {
1162                 *buflen = rsclen;
1163                 *status = NDIS_STATUS_INVALID_LENGTH;
1164                 return;
1165         }
1166
1167         bcopy((char *)block->nmb_rlist, (char *)list, rsclen);
1168         *status = NDIS_STATUS_SUCCESS;
1169 }
1170
1171 static ndis_status
1172 NdisMRegisterIoPortRange(void **offset, ndis_handle adapter, uint32_t port,
1173     uint32_t numports)
1174 {
1175         struct ndis_miniport_block      *block;
1176         struct ndis_softc       *sc;
1177
1178         if (adapter == NULL)
1179                 return (NDIS_STATUS_FAILURE);
1180
1181         block = (ndis_miniport_block *)adapter;
1182         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1183
1184         if (sc->ndis_res_io == NULL)
1185                 return (NDIS_STATUS_FAILURE);
1186
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);
1190
1191         *offset = (void *)rman_get_start(sc->ndis_res_io);
1192
1193         return (NDIS_STATUS_SUCCESS);
1194 }
1195
1196 static void
1197 NdisMDeregisterIoPortRange(ndis_handle adapter, uint32_t port,
1198     uint32_t numports, void *offset)
1199 {
1200 }
1201
1202 static void
1203 NdisReadNetworkAddress(ndis_status *status, void **addr, uint32_t *addrlen,
1204     ndis_handle adapter)
1205 {
1206         struct ndis_softc       *sc;
1207         ndis_miniport_block     *block;
1208         uint8_t                 empty[] = { 0, 0, 0, 0, 0, 0 };
1209
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;
1214                 return;
1215         }
1216
1217         if (sc->ifp->if_lladdr == NULL ||
1218             bcmp(IF_LLADDR(sc->ifp), empty, ETHER_ADDR_LEN) == 0)
1219                 *status = NDIS_STATUS_FAILURE;
1220         else {
1221                 *addr = IF_LLADDR(sc->ifp);
1222                 *addrlen = ETHER_ADDR_LEN;
1223                 *status = NDIS_STATUS_SUCCESS;
1224         }
1225 }
1226
1227 static ndis_status
1228 NdisQueryMapRegisterCount(uint32_t bustype, uint32_t *cnt)
1229 {
1230         *cnt = 8192;
1231         return (NDIS_STATUS_SUCCESS);
1232 }
1233
1234 static ndis_status
1235 NdisMAllocateMapRegisters(ndis_handle adapter, uint32_t dmachannel,
1236     uint8_t dmasize, uint32_t physmapneeded, uint32_t maxmap)
1237 {
1238         struct ndis_softc       *sc;
1239         ndis_miniport_block     *block;
1240         int                     error, i, nseg = NDIS_MAXSEG;
1241
1242         block = (ndis_miniport_block *)adapter;
1243         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1244
1245         sc->ndis_mmaps = kmalloc(sizeof(bus_dmamap_t) * physmapneeded,
1246             M_DEVBUF, M_NOWAIT|M_ZERO);
1247
1248         if (sc->ndis_mmaps == NULL)
1249                 return (NDIS_STATUS_RESOURCES);
1250
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,
1254             &sc->ndis_mtag);
1255
1256         if (error) {
1257                 kfree(sc->ndis_mmaps, M_DEVBUF);
1258                 return (NDIS_STATUS_RESOURCES);
1259         }
1260
1261         for (i = 0; i < physmapneeded; i++)
1262                 bus_dmamap_create(sc->ndis_mtag, 0, &sc->ndis_mmaps[i]);
1263
1264         sc->ndis_mmapcnt = physmapneeded;
1265
1266         return (NDIS_STATUS_SUCCESS);
1267 }
1268
1269 static void
1270 NdisMFreeMapRegisters(ndis_handle adapter)
1271 {
1272         struct ndis_softc       *sc;
1273         ndis_miniport_block     *block;
1274         int                     i;
1275
1276         block = (ndis_miniport_block *)adapter;
1277         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1278
1279         for (i = 0; i < sc->ndis_mmapcnt; i++)
1280                 bus_dmamap_destroy(sc->ndis_mtag, sc->ndis_mmaps[i]);
1281
1282         kfree(sc->ndis_mmaps, M_DEVBUF);
1283
1284         bus_dma_tag_destroy(sc->ndis_mtag);
1285 }
1286
1287 static void
1288 ndis_mapshared_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1289 {
1290         ndis_physaddr           *p;
1291
1292         if (error || nseg > 1)
1293                 return;
1294
1295         p = arg;
1296
1297         p->np_quad = segs[0].ds_addr;
1298 }
1299
1300 /*
1301  * This maps to bus_dmamem_alloc().
1302  */
1303
1304 static void
1305 NdisMAllocateSharedMemory(ndis_handle adapter, uint32_t len, uint8_t cached,
1306     void **vaddr, ndis_physaddr *paddr)
1307 {
1308         ndis_miniport_block     *block;
1309         struct ndis_softc       *sc;
1310         struct ndis_shmem       *sh;
1311         int                     error;
1312
1313         if (adapter == NULL)
1314                 return;
1315
1316         block = (ndis_miniport_block *)adapter;
1317         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1318
1319         sh = kmalloc(sizeof(struct ndis_shmem), M_DEVBUF, M_NOWAIT|M_ZERO);
1320         if (sh == NULL)
1321                 return;
1322
1323         InitializeListHead(&sh->ndis_list);
1324
1325         /*
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.
1337          */
1338
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);
1342
1343         if (error) {
1344                 kfree(sh, M_DEVBUF);
1345                 return;
1346         }
1347
1348         error = bus_dmamem_alloc(sh->ndis_stag, vaddr,
1349             BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sh->ndis_smap);
1350
1351         if (error) {
1352                 bus_dma_tag_destroy(sh->ndis_stag);
1353                 kfree(sh, M_DEVBUF);
1354                 return;
1355         }
1356
1357         error = bus_dmamap_load(sh->ndis_stag, sh->ndis_smap, *vaddr,
1358             len, ndis_mapshared_cb, (void *)paddr, BUS_DMA_NOWAIT);
1359
1360         if (error) {
1361                 bus_dmamem_free(sh->ndis_stag, *vaddr, sh->ndis_smap);
1362                 bus_dma_tag_destroy(sh->ndis_stag);
1363                 kfree(sh, M_DEVBUF);
1364                 return;
1365         }
1366
1367         /*
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.
1374          */
1375
1376         NDIS_LOCK(sc);
1377         sh->ndis_paddr.np_quad = paddr->np_quad;
1378         sh->ndis_saddr = *vaddr;
1379         InsertHeadList((&sc->ndis_shlist), (&sh->ndis_list));
1380         NDIS_UNLOCK(sc);
1381 }
1382
1383 struct ndis_allocwork {
1384         uint32_t                na_len;
1385         uint8_t                 na_cached;
1386         void                    *na_ctx;
1387         io_workitem             *na_iw;
1388 };
1389
1390 static void
1391 ndis_asyncmem_complete(device_object *dobj, void *arg)
1392 {
1393         ndis_miniport_block     *block;
1394         struct ndis_softc       *sc;
1395         struct ndis_allocwork   *w;
1396         void                    *vaddr;
1397         ndis_physaddr           paddr;
1398         ndis_allocdone_handler  donefunc;
1399
1400         w = arg;
1401         block = (ndis_miniport_block *)dobj->do_devext;
1402         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1403
1404         vaddr = NULL;
1405         paddr.np_quad = 0;
1406
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);
1411
1412         IoFreeWorkItem(w->na_iw);
1413         kfree(w, M_DEVBUF);
1414 }
1415
1416 static ndis_status
1417 NdisMAllocateSharedMemoryAsync(ndis_handle adapter, uint32_t len,
1418     uint8_t cached, void *ctx)
1419 {
1420         ndis_miniport_block     *block;
1421         struct ndis_allocwork   *w;
1422         io_workitem             *iw;
1423         io_workitem_func        ifw;
1424
1425         if (adapter == NULL)
1426                 return (NDIS_STATUS_FAILURE);
1427
1428         block = adapter;
1429
1430         iw = IoAllocateWorkItem(block->nmb_deviceobj);
1431         if (iw == NULL)
1432                 return (NDIS_STATUS_FAILURE);
1433
1434         w = kmalloc(sizeof(struct ndis_allocwork), M_TEMP, M_NOWAIT);
1435
1436         if (w == NULL)
1437                 return (NDIS_STATUS_FAILURE);
1438
1439         w->na_cached = cached;
1440         w->na_len = len;
1441         w->na_ctx = ctx;
1442         w->na_iw = iw;
1443
1444         ifw = (io_workitem_func)ndis_findwrap((funcptr)ndis_asyncmem_complete);
1445         IoQueueWorkItem(iw, ifw, WORKQUEUE_DELAYED, w);
1446
1447         return (NDIS_STATUS_PENDING);
1448 }
1449
1450 static void
1451 NdisMFreeSharedMemory(ndis_handle adapter, uint32_t len, uint8_t cached,
1452     void *vaddr, ndis_physaddr paddr)
1453 {
1454         ndis_miniport_block     *block;
1455         struct ndis_softc       *sc;
1456         struct ndis_shmem       *sh = NULL;
1457         list_entry              *l;
1458
1459         if (vaddr == NULL || adapter == NULL)
1460                 return;
1461
1462         block = (ndis_miniport_block *)adapter;
1463         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1464
1465         /* Sanity check: is list empty? */
1466
1467         if (IsListEmpty(&sc->ndis_shlist))
1468                 return;
1469
1470         NDIS_LOCK(sc);
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)
1475                         break;
1476                 /*
1477                  * Check the physaddr too, just in case the driver lied
1478                  * about the virtual address.
1479                  */
1480                 if (sh->ndis_paddr.np_quad == paddr.np_quad)
1481                         break;
1482                 l = l->nle_flink;
1483         }
1484
1485         if (sh == NULL) {
1486                 NDIS_UNLOCK(sc);
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);
1490                 return;
1491         }
1492
1493         RemoveEntryList(&sh->ndis_list);
1494
1495         NDIS_UNLOCK(sc);
1496
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);
1500
1501         kfree(sh, M_DEVBUF);
1502 }
1503
1504 static ndis_status
1505 NdisMMapIoSpace(void **vaddr, ndis_handle adapter, ndis_physaddr paddr,
1506     uint32_t len)
1507 {
1508         if (adapter == NULL)
1509                 return (NDIS_STATUS_FAILURE);
1510
1511         *vaddr = MmMapIoSpace(paddr.np_quad, len, 0);
1512
1513         if (*vaddr == NULL)
1514                 return (NDIS_STATUS_FAILURE);
1515
1516         return (NDIS_STATUS_SUCCESS);
1517 }
1518
1519 static void
1520 NdisMUnmapIoSpace(ndis_handle adapter, void *vaddr, uint32_t len)
1521 {
1522         MmUnmapIoSpace(vaddr, len);
1523 }
1524
1525 static uint32_t
1526 NdisGetCacheFillSize(void)
1527 {
1528         return (128);
1529 }
1530
1531 static void *
1532 NdisGetRoutineAddress(unicode_string *ustr)
1533 {
1534         ansi_string             astr;
1535
1536         if (RtlUnicodeStringToAnsiString(&astr, ustr, TRUE))
1537                 return (NULL);
1538         return (ndis_get_routine_address(ndis_functbl, astr.as_buf));
1539 }
1540
1541 static uint32_t
1542 NdisMGetDmaAlignment(ndis_handle handle)
1543 {
1544         return (16);
1545 }
1546
1547 /*
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
1555  * method.
1556  */
1557
1558 static ndis_status
1559 NdisMInitializeScatterGatherDma(ndis_handle adapter, uint8_t is64,
1560     uint32_t maxphysmap)
1561 {
1562         struct ndis_softc       *sc;
1563         ndis_miniport_block     *block;
1564         int                     error;
1565
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);
1570
1571         /* Don't do this twice. */
1572         if (sc->ndis_sc == 1)
1573                 return (NDIS_STATUS_SUCCESS);
1574
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,
1578             &sc->ndis_ttag);
1579
1580         sc->ndis_sc = 1;
1581
1582         return (NDIS_STATUS_SUCCESS);
1583 }
1584
1585 void
1586 NdisAllocatePacketPool(ndis_status *status, ndis_handle *pool,
1587     uint32_t descnum, uint32_t protrsvdlen)
1588 {
1589         ndis_packet_pool        *p;
1590         ndis_packet             *packets;
1591         int                     i;
1592
1593         p = ExAllocatePoolWithTag(NonPagedPool, sizeof(ndis_packet_pool), 0);
1594         if (p == NULL) {
1595                 *status = NDIS_STATUS_RESOURCES;
1596                 return;
1597         }
1598
1599         p->np_cnt = descnum + NDIS_POOL_EXTRA;
1600         p->np_protrsvd = protrsvdlen;
1601         p->np_len = sizeof(ndis_packet) + protrsvdlen;
1602
1603         packets = ExAllocatePoolWithTag(NonPagedPool, p->np_cnt *
1604             p->np_len, 0);
1605
1606
1607         if (packets == NULL) {
1608                 ExFreePool(p);
1609                 *status = NDIS_STATUS_RESOURCES;
1610                 return;
1611         }
1612
1613         p->np_pktmem = packets;
1614
1615         for (i = 0; i < p->np_cnt; i++)
1616                 InterlockedPushEntrySList(&p->np_head,
1617                     (struct slist_entry *)&packets[i]);
1618
1619 #ifdef NDIS_DEBUG_PACKETS
1620         p->np_dead = 0;
1621         KeInitializeSpinLock(&p->np_lock);
1622         KeInitializeEvent(&p->np_event, EVENT_TYPE_NOTIFY, TRUE);
1623 #endif
1624
1625         *pool = p;
1626         *status = NDIS_STATUS_SUCCESS;
1627 }
1628
1629 void
1630 NdisAllocatePacketPoolEx(ndis_status *status, ndis_handle *pool,
1631     uint32_t descnum, uint32_t oflowdescnum, uint32_t protrsvdlen)
1632 {
1633         NdisAllocatePacketPool(status, pool, descnum + oflowdescnum,
1634             protrsvdlen);
1635 }
1636
1637 uint32_t
1638 NdisPacketPoolUsage(ndis_handle pool)
1639 {
1640         ndis_packet_pool        *p;
1641
1642         p = (ndis_packet_pool *)pool;
1643         return (p->np_cnt - ExQueryDepthSList(&p->np_head));
1644 }
1645
1646 void
1647 NdisFreePacketPool(ndis_handle pool)
1648 {
1649         ndis_packet_pool        *p;
1650         int                     usage;
1651 #ifdef NDIS_DEBUG_PACKETS
1652         uint8_t                 irql;
1653 #endif
1654
1655         p = (ndis_packet_pool *)pool;
1656
1657 #ifdef NDIS_DEBUG_PACKETS
1658         KeAcquireSpinLock(&p->np_lock, &irql);
1659 #endif
1660
1661         usage = NdisPacketPoolUsage(pool);
1662
1663 #ifdef NDIS_DEBUG_PACKETS
1664         if (usage) {
1665                 p->np_dead = 1;
1666                 KeResetEvent(&p->np_event);
1667                 KeReleaseSpinLock(&p->np_lock, irql);
1668                 KeWaitForSingleObject(&p->np_event, 0, 0, FALSE, NULL);
1669         } else
1670                 KeReleaseSpinLock(&p->np_lock, irql);
1671 #endif
1672
1673         ExFreePool(p->np_pktmem);
1674         ExFreePool(p);
1675 }
1676
1677 void
1678 NdisAllocatePacket(ndis_status *status, ndis_packet **packet, ndis_handle pool)
1679 {
1680         ndis_packet_pool        *p;
1681         ndis_packet             *pkt;
1682 #ifdef NDIS_DEBUG_PACKETS
1683         uint8_t                 irql;
1684 #endif
1685
1686         p = (ndis_packet_pool *)pool;
1687
1688 #ifdef NDIS_DEBUG_PACKETS
1689         KeAcquireSpinLock(&p->np_lock, &irql);
1690         if (p->np_dead) {
1691                 KeReleaseSpinLock(&p->np_lock, irql);
1692                 kprintf("NDIS: tried to allocate packet from dead pool %p\n",
1693                     pool);
1694                 *status = NDIS_STATUS_RESOURCES;
1695                 return;
1696         }
1697 #endif
1698
1699         pkt = (ndis_packet *)InterlockedPopEntrySList(&p->np_head);
1700
1701 #ifdef NDIS_DEBUG_PACKETS
1702         KeReleaseSpinLock(&p->np_lock, irql);
1703 #endif
1704
1705         if (pkt == NULL) {
1706                 *status = NDIS_STATUS_RESOURCES;
1707                 return;
1708         }
1709
1710
1711         bzero((char *)pkt, sizeof(ndis_packet));
1712
1713         /* Save pointer to the pool. */
1714         pkt->np_private.npp_pool = pool;
1715
1716         /* Set the oob offset pointer. Lots of things expect this. */
1717         pkt->np_private.npp_packetooboffset = offsetof(ndis_packet, np_oob);
1718
1719         /*
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
1723          * correctly.
1724          */
1725         pkt->np_private.npp_ndispktflags = NDIS_PACKET_ALLOCATED_BY_NDIS;
1726         pkt->np_private.npp_validcounts = FALSE;
1727
1728         *packet = pkt;
1729
1730         *status = NDIS_STATUS_SUCCESS;
1731 }
1732
1733 void
1734 NdisFreePacket(ndis_packet *packet)
1735 {
1736         ndis_packet_pool        *p;
1737 #ifdef NDIS_DEBUG_PACKETS
1738         uint8_t                 irql;
1739 #endif
1740
1741         p = (ndis_packet_pool *)packet->np_private.npp_pool;
1742
1743 #ifdef NDIS_DEBUG_PACKETS
1744         KeAcquireSpinLock(&p->np_lock, &irql);
1745 #endif
1746
1747         InterlockedPushEntrySList(&p->np_head, (slist_entry *)packet);
1748
1749 #ifdef NDIS_DEBUG_PACKETS
1750         if (p->np_dead) {
1751                 if (ExQueryDepthSList(&p->np_head) == p->np_cnt)
1752                         KeSetEvent(&p->np_event, IO_NO_INCREMENT, FALSE);
1753         }
1754         KeReleaseSpinLock(&p->np_lock, irql);
1755 #endif
1756 }
1757
1758 static void
1759 NdisUnchainBufferAtFront(ndis_packet *packet, ndis_buffer **buf)
1760 {
1761         ndis_packet_private     *priv;
1762
1763         if (packet == NULL || buf == NULL)
1764                 return;
1765
1766         priv = &packet->np_private;
1767
1768         priv->npp_validcounts = FALSE;
1769
1770         if (priv->npp_head == priv->npp_tail) {
1771                 *buf = priv->npp_head;
1772                 priv->npp_head = priv->npp_tail = NULL;
1773         } else {
1774                 *buf = priv->npp_head;
1775                 priv->npp_head = (*buf)->mdl_next;
1776         }
1777 }
1778
1779 static void
1780 NdisUnchainBufferAtBack(ndis_packet *packet, ndis_buffer **buf)
1781 {
1782         ndis_packet_private     *priv;
1783         ndis_buffer             *tmp;
1784
1785         if (packet == NULL || buf == NULL)
1786                 return;
1787
1788         priv = &packet->np_private;
1789
1790         priv->npp_validcounts = FALSE;
1791
1792         if (priv->npp_head == priv->npp_tail) {
1793                 *buf = priv->npp_head;
1794                 priv->npp_head = priv->npp_tail = NULL;
1795         } else {
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;
1802         }
1803 }
1804
1805 /*
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.
1816  */
1817
1818 static void
1819 NdisAllocateBufferPool(ndis_status *status, ndis_handle *pool,
1820     uint32_t descnum)
1821 {
1822
1823         /*
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
1826          * it against.
1827          */
1828
1829         *pool = NonPagedPool;
1830         *status = NDIS_STATUS_SUCCESS;
1831 }
1832
1833 static void
1834 NdisFreeBufferPool(ndis_handle pool)
1835 {
1836 }
1837
1838 static void
1839 NdisAllocateBuffer(ndis_status *status, ndis_buffer **buffer, ndis_handle pool,
1840     void *vaddr, uint32_t len)
1841 {
1842         ndis_buffer             *buf;
1843
1844         buf = IoAllocateMdl(vaddr, len, FALSE, FALSE, NULL);
1845         if (buf == NULL) {
1846                 *status = NDIS_STATUS_RESOURCES;
1847                 return;
1848         }
1849
1850         MmBuildMdlForNonPagedPool(buf);
1851
1852         *buffer = buf;
1853         *status = NDIS_STATUS_SUCCESS;
1854 }
1855
1856 static void
1857 NdisFreeBuffer(ndis_buffer *buf)
1858 {
1859         IoFreeMdl(buf);
1860 }
1861
1862 /* Aw c'mon. */
1863
1864 static uint32_t
1865 NdisBufferLength(ndis_buffer *buf)
1866 {
1867         return (MmGetMdlByteCount(buf));
1868 }
1869
1870 /*
1871  * Get the virtual address and length of a buffer.
1872  * Note: the vaddr argument is optional.
1873  */
1874
1875 static void
1876 NdisQueryBuffer(ndis_buffer *buf, void **vaddr, uint32_t *len)
1877 {
1878         if (vaddr != NULL)
1879                 *vaddr = MmGetMdlVirtualAddress(buf);
1880         *len = MmGetMdlByteCount(buf);
1881 }
1882
1883 /* Same as above -- we don't care about the priority. */
1884
1885 static void
1886 NdisQueryBufferSafe(ndis_buffer *buf, void **vaddr, uint32_t *len,
1887     uint32_t prio)
1888 {
1889         if (vaddr != NULL)
1890                 *vaddr = MmGetMdlVirtualAddress(buf);
1891         *len = MmGetMdlByteCount(buf);
1892 }
1893
1894 /* Damnit Microsoft!! How many ways can you do the same thing?! */
1895
1896 static void *
1897 NdisBufferVirtualAddress(ndis_buffer *buf)
1898 {
1899         return (MmGetMdlVirtualAddress(buf));
1900 }
1901
1902 static void *
1903 NdisBufferVirtualAddressSafe(ndis_buffer *buf, uint32_t prio)
1904 {
1905         return (MmGetMdlVirtualAddress(buf));
1906 }
1907
1908 static void
1909 NdisAdjustBufferLength(ndis_buffer *buf, int len)
1910 {
1911         MmGetMdlByteCount(buf) = len;
1912 }
1913
1914 static uint32_t
1915 NdisInterlockedIncrement(uint32_t *addend)
1916 {
1917         atomic_add_long((u_long *)addend, 1);
1918         return (*addend);
1919 }
1920
1921 static uint32_t
1922 NdisInterlockedDecrement(uint32_t *addend)
1923 {
1924         atomic_subtract_long((u_long *)addend, 1);
1925         return (*addend);
1926 }
1927
1928 static uint32_t
1929 NdisGetVersion(void)
1930 {
1931         return (0x00050001);
1932 }
1933
1934 static void
1935 NdisInitializeEvent(ndis_event *event)
1936 {
1937         /*
1938          * NDIS events are always notification
1939          * events, and should be initialized to the
1940          * not signaled state.
1941          */
1942         KeInitializeEvent(&event->ne_event, EVENT_TYPE_NOTIFY, FALSE);
1943 }
1944
1945 static void
1946 NdisSetEvent(ndis_event *event)
1947 {
1948         KeSetEvent(&event->ne_event, IO_NO_INCREMENT, FALSE);
1949 }
1950
1951 static void
1952 NdisResetEvent(ndis_event *event)
1953 {
1954         KeResetEvent(&event->ne_event);
1955 }
1956
1957 static uint8_t
1958 NdisWaitEvent(ndis_event *event, uint32_t msecs)
1959 {
1960         int64_t                 duetime;
1961         uint32_t                rval;
1962
1963         duetime = ((int64_t)msecs * -10000);
1964         rval = KeWaitForSingleObject(event,
1965             0, 0, TRUE, msecs ? & duetime : NULL);
1966
1967         if (rval == STATUS_TIMEOUT)
1968                 return (FALSE);
1969
1970         return (TRUE);
1971 }
1972
1973 static ndis_status
1974 NdisUnicodeStringToAnsiString(ansi_string *dstr, unicode_string *sstr)
1975 {
1976         uint32_t                rval;
1977
1978         rval = RtlUnicodeStringToAnsiString(dstr, sstr, FALSE);
1979
1980         if (rval == STATUS_INSUFFICIENT_RESOURCES)
1981                 return (NDIS_STATUS_RESOURCES);
1982         if (rval)
1983                 return (NDIS_STATUS_FAILURE);
1984
1985         return (NDIS_STATUS_SUCCESS);
1986 }
1987
1988 static ndis_status
1989 NdisAnsiStringToUnicodeString(unicode_string *dstr, ansi_string *sstr)
1990 {
1991         uint32_t                rval;
1992
1993         rval = RtlAnsiStringToUnicodeString(dstr, sstr, FALSE);
1994
1995         if (rval == STATUS_INSUFFICIENT_RESOURCES)
1996                 return (NDIS_STATUS_RESOURCES);
1997         if (rval)
1998                 return (NDIS_STATUS_FAILURE);
1999
2000         return (NDIS_STATUS_SUCCESS);
2001 }
2002
2003 static ndis_status
2004 NdisMPciAssignResources(ndis_handle adapter, uint32_t slot,
2005     ndis_resource_list **list)
2006 {
2007         ndis_miniport_block     *block;
2008
2009         if (adapter == NULL || list == NULL)
2010                 return (NDIS_STATUS_FAILURE);
2011
2012         block = (ndis_miniport_block *)adapter;
2013         *list = block->nmb_rlist;
2014
2015         return (NDIS_STATUS_SUCCESS);
2016 }
2017
2018 static uint8_t
2019 ndis_intr(kinterrupt *iobj, void *arg)
2020 {
2021         struct ndis_softc       *sc;
2022         uint8_t                 is_our_intr = FALSE;
2023         int                     call_isr = 0;
2024         ndis_miniport_interrupt *intr;
2025
2026         sc = arg;
2027         intr = sc->ndis_block->nmb_interrupt;
2028
2029         if (intr == NULL || sc->ndis_block->nmb_miniportadapterctx == NULL)
2030                 return (FALSE);
2031
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);
2035         else {
2036                 MSCALL1(sc->ndis_chars->nmc_disable_interrupts_func,
2037                     sc->ndis_block->nmb_miniportadapterctx);
2038                 call_isr = 1;
2039         }
2040
2041         if (call_isr)
2042                 IoRequestDpc(sc->ndis_block->nmb_deviceobj, NULL, sc);
2043
2044         return (is_our_intr);
2045 }
2046
2047 static void
2048 ndis_intrhand(kdpc *dpc, ndis_miniport_interrupt *intr, void *sysarg1,
2049     void *sysarg2)
2050 {
2051         struct ndis_softc       *sc;
2052         ndis_miniport_block     *block;
2053         ndis_handle             adapter;
2054
2055         block = intr->ni_block;
2056         adapter = block->nmb_miniportadapterctx;
2057         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2058
2059         if (NDIS_SERIALIZED(sc->ndis_block))
2060                 KeAcquireSpinLockAtDpcLevel(&block->nmb_lock);
2061
2062         MSCALL1(intr->ni_dpcfunc, adapter);
2063
2064         /* If there's a MiniportEnableInterrupt() routine, call it. */
2065
2066         if (sc->ndis_chars->nmc_enable_interrupts_func != NULL)
2067                 MSCALL1(sc->ndis_chars->nmc_enable_interrupts_func, adapter);
2068
2069         if (NDIS_SERIALIZED(sc->ndis_block))
2070                 KeReleaseSpinLockFromDpcLevel(&block->nmb_lock);
2071
2072         /*
2073          * Set the completion event if we've drained all
2074          * pending interrupts.
2075          */
2076
2077         KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
2078         intr->ni_dpccnt--;
2079         if (intr->ni_dpccnt == 0)
2080                 KeSetEvent(&intr->ni_dpcevt, IO_NO_INCREMENT, FALSE);
2081         KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
2082 }
2083
2084 static ndis_status
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)
2088 {
2089         ndis_miniport_block     *block;
2090         ndis_miniport_characteristics *ch;
2091         struct ndis_softc       *sc;
2092         int                     error;
2093
2094         block = adapter;
2095         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2096         ch = IoGetDriverObjectExtension(block->nmb_deviceobj->do_drvobj,
2097             (void *)1);
2098
2099         intr->ni_rsvd = ExAllocatePoolWithTag(NonPagedPool,
2100             sizeof(struct mtx), 0);
2101         if (intr->ni_rsvd == NULL)
2102                 return (NDIS_STATUS_RESOURCES);
2103
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;
2110
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);
2115
2116         error = IoConnectInterrupt(&intr->ni_introbj,
2117             ndis_findwrap((funcptr)ndis_intr), sc, NULL,
2118             ivec, ilevel, 0, imode, shared, 0, FALSE);
2119
2120         if (error != STATUS_SUCCESS)
2121                 return (NDIS_STATUS_FAILURE);
2122
2123         block->nmb_interrupt = intr;
2124
2125         return (NDIS_STATUS_SUCCESS);
2126 }
2127
2128 static void
2129 NdisMDeregisterInterrupt(ndis_miniport_interrupt *intr)
2130 {
2131         ndis_miniport_block     *block;
2132         uint8_t                 irql;
2133
2134         block = intr->ni_block;
2135
2136         /* Should really be KeSynchronizeExecution() */
2137
2138         KeAcquireSpinLock(intr->ni_introbj->ki_lock, &irql);
2139         block->nmb_interrupt = NULL;
2140         KeReleaseSpinLock(intr->ni_introbj->ki_lock, irql);
2141 /*
2142         KeFlushQueuedDpcs();
2143 */
2144         /* Disconnect our ISR */
2145
2146         IoDisconnectInterrupt(intr->ni_introbj);
2147
2148         KeWaitForSingleObject(&intr->ni_dpcevt, 0, 0, FALSE, NULL);
2149         KeResetEvent(&intr->ni_dpcevt);
2150 }
2151
2152 static void
2153 NdisMRegisterAdapterShutdownHandler(ndis_handle adapter, void *shutdownctx,
2154     ndis_shutdown_handler shutdownfunc)
2155 {
2156         ndis_miniport_block     *block;
2157         ndis_miniport_characteristics *chars;
2158         struct ndis_softc       *sc;
2159
2160         if (adapter == NULL)
2161                 return;
2162
2163         block = (ndis_miniport_block *)adapter;
2164         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2165         chars = sc->ndis_chars;
2166
2167         chars->nmc_shutdown_handler = shutdownfunc;
2168         chars->nmc_rsvd0 = shutdownctx;
2169 }
2170
2171 static void
2172 NdisMDeregisterAdapterShutdownHandler(ndis_handle adapter)
2173 {
2174         ndis_miniport_block     *block;
2175         ndis_miniport_characteristics *chars;
2176         struct ndis_softc       *sc;
2177
2178         if (adapter == NULL)
2179                 return;
2180
2181         block = (ndis_miniport_block *)adapter;
2182         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2183         chars = sc->ndis_chars;
2184
2185         chars->nmc_shutdown_handler = NULL;
2186         chars->nmc_rsvd0 = NULL;
2187 }
2188
2189 static uint32_t
2190 NDIS_BUFFER_TO_SPAN_PAGES(ndis_buffer *buf)
2191 {
2192         if (buf == NULL)
2193                 return (0);
2194         if (MmGetMdlByteCount(buf) == 0)
2195                 return (1);
2196         return (SPAN_PAGES(MmGetMdlVirtualAddress(buf),
2197             MmGetMdlByteCount(buf)));
2198 }
2199
2200 static void
2201 NdisGetBufferPhysicalArraySize(ndis_buffer *buf, uint32_t *pages)
2202 {
2203         if (buf == NULL)
2204                 return;
2205
2206         *pages = NDIS_BUFFER_TO_SPAN_PAGES(buf);
2207 }
2208
2209 static void
2210 NdisQueryBufferOffset(ndis_buffer *buf, uint32_t *off, uint32_t *len)
2211 {
2212         if (buf == NULL)
2213                 return;
2214
2215         *off = MmGetMdlByteOffset(buf);
2216         *len = MmGetMdlByteCount(buf);
2217 }
2218
2219 void
2220 NdisMSleep(uint32_t usecs)
2221 {
2222         ktimer                  timer;
2223
2224         /*
2225          * During system bootstrap, (i.e. cold == 1), we aren't
2226          * allowed to sleep, so we have to do a hard DELAY()
2227          * instead.
2228          */
2229
2230         if (cold)
2231                 DELAY(usecs);
2232         else {
2233                 KeInitializeTimer(&timer);
2234                 KeSetTimer(&timer, ((int64_t)usecs * -10), NULL);
2235                 KeWaitForSingleObject(&timer, 0, 0, FALSE, NULL);
2236         }
2237 }
2238
2239 static uint32_t
2240 NdisReadPcmciaAttributeMemory(ndis_handle handle, uint32_t offset, void *buf,
2241     uint32_t len)
2242 {
2243         struct ndis_softc       *sc;
2244         ndis_miniport_block     *block;
2245         bus_space_handle_t      bh;
2246         bus_space_tag_t         bt;
2247         char                    *dest;
2248         int                     i;
2249
2250         if (handle == NULL)
2251                 return (0);
2252
2253         block = (ndis_miniport_block *)handle;
2254         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2255         dest = buf;
2256
2257         bh = rman_get_bushandle(sc->ndis_res_am);
2258         bt = rman_get_bustag(sc->ndis_res_am);
2259
2260         for (i = 0; i < len; i++)
2261                 dest[i] = bus_space_read_1(bt, bh, (offset + i) * 2);
2262
2263         return (i);
2264 }
2265
2266 static uint32_t
2267 NdisWritePcmciaAttributeMemory(ndis_handle handle, uint32_t offset, void *buf,
2268     uint32_t len)
2269 {
2270         struct ndis_softc       *sc;
2271         ndis_miniport_block     *block;
2272         bus_space_handle_t      bh;
2273         bus_space_tag_t         bt;
2274         char                    *src;
2275         int                     i;
2276
2277         if (handle == NULL)
2278                 return (0);
2279
2280         block = (ndis_miniport_block *)handle;
2281         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2282         src = buf;
2283
2284         bh = rman_get_bushandle(sc->ndis_res_am);
2285         bt = rman_get_bustag(sc->ndis_res_am);
2286
2287         for (i = 0; i < len; i++)
2288                 bus_space_write_1(bt, bh, (offset + i) * 2, src[i]);
2289
2290         return (i);
2291 }
2292
2293 static list_entry *
2294 NdisInterlockedInsertHeadList(list_entry *head, list_entry *entry,
2295     ndis_spin_lock *lock)
2296 {
2297         list_entry              *flink;
2298
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);
2306
2307         return (flink);
2308 }
2309
2310 static list_entry *
2311 NdisInterlockedRemoveHeadList(list_entry *head, ndis_spin_lock *lock)
2312 {
2313         list_entry              *flink;
2314         list_entry              *entry;
2315
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);
2322
2323         return (entry);
2324 }
2325
2326 static list_entry *
2327 NdisInterlockedInsertTailList(list_entry *head, list_entry *entry,
2328     ndis_spin_lock *lock)
2329 {
2330         list_entry              *blink;
2331
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);
2339
2340         return (blink);
2341 }
2342
2343 static uint8_t
2344 NdisMSynchronizeWithInterrupt(ndis_miniport_interrupt *intr, void *syncfunc,
2345     void *syncctx)
2346 {
2347         return (KeSynchronizeExecution(intr->ni_introbj, syncfunc, syncctx));
2348 }
2349
2350 static void
2351 NdisGetCurrentSystemTime(uint64_t *tval)
2352 {
2353         ntoskrnl_time(tval);
2354 }
2355
2356 /*
2357  * Return the number of milliseconds since the system booted.
2358  */
2359 static void
2360 NdisGetSystemUpTime(uint32_t *tval)
2361 {
2362         struct timespec         ts;
2363
2364         nanouptime(&ts);
2365         *tval = ts.tv_nsec / 1000000 + ts.tv_sec * 1000;
2366 }
2367
2368 static void
2369 NdisInitializeString(unicode_string *dst, char *src)
2370 {
2371         ansi_string             as;
2372         RtlInitAnsiString(&as, src);
2373         RtlAnsiStringToUnicodeString(dst, &as, TRUE);
2374 }
2375
2376 static void
2377 NdisFreeString(unicode_string *str)
2378 {
2379         RtlFreeUnicodeString(str);
2380 }
2381
2382 static ndis_status
2383 NdisMRemoveMiniport(ndis_handle *adapter)
2384 {
2385         return (NDIS_STATUS_SUCCESS);
2386 }
2387
2388 static void
2389 NdisInitAnsiString(ansi_string *dst, char *src)
2390 {
2391         RtlInitAnsiString(dst, src);
2392 }
2393
2394 static void
2395 NdisInitUnicodeString(unicode_string *dst, uint16_t *src)
2396 {
2397         RtlInitUnicodeString(dst, src);
2398 }
2399
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)
2404 {
2405         ndis_miniport_block     *block;
2406
2407         block = (ndis_miniport_block *)adapter;
2408
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;
2415 }
2416
2417 static void
2418 NdisGetFirstBufferFromPacket(ndis_packet *packet, ndis_buffer **buf,
2419     void **firstva, uint32_t *firstlen, uint32_t *totlen)
2420 {
2421         ndis_buffer             *tmp;
2422
2423         tmp = packet->np_private.npp_head;
2424         *buf = tmp;
2425         if (tmp == NULL) {
2426                 *firstva = NULL;
2427                 *firstlen = *totlen = 0;
2428         } else {
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);
2433         }
2434 }
2435
2436 static void
2437 NdisGetFirstBufferFromPacketSafe(ndis_packet *packet, ndis_buffer **buf,
2438     void **firstva, uint32_t *firstlen, uint32_t *totlen, uint32_t prio)
2439 {
2440         NdisGetFirstBufferFromPacket(packet, buf, firstva, firstlen, totlen);
2441 }
2442
2443 static int
2444 ndis_find_sym(linker_file_t lf, char *filename, char *suffix, caddr_t *sym)
2445 {
2446         char                    *fullsym;
2447         char                    *suf;
2448         int                     i;
2449
2450         fullsym = ExAllocatePoolWithTag(NonPagedPool, MAXPATHLEN, 0);
2451         if (fullsym == NULL)
2452                 return (ENOMEM);
2453
2454         bzero(fullsym, MAXPATHLEN);
2455         strncpy(fullsym, filename, MAXPATHLEN);
2456         if (strlen(filename) < 4) {
2457                 ExFreePool(fullsym);
2458                 return (EINVAL);
2459         }
2460
2461         /* If the filename has a .ko suffix, strip if off. */
2462         suf = fullsym + (strlen(filename) - 3);
2463         if (strcmp(suf, ".ko") == 0)
2464                 *suf = '\0';
2465
2466         for (i = 0; i < strlen(fullsym); i++) {
2467                 if (fullsym[i] == '.')
2468                         fullsym[i] = '_';
2469                 else
2470                         fullsym[i] = tolower(fullsym[i]);
2471         }
2472         strcat(fullsym, suffix);
2473         linker_file_lookup_symbol(lf, fullsym, 0, sym);
2474         ExFreePool(fullsym);
2475         if (*sym == 0)
2476                 return (ENOENT);
2477
2478         return (0);
2479 }
2480
2481 struct ndis_checkmodule {
2482         char    *afilename;
2483         ndis_fh *fh;
2484 };
2485
2486 /*
2487  * See if a single module contains the symbols for a specified file.
2488  */
2489 static int
2490 NdisCheckModule(linker_file_t lf, void *context)
2491 {
2492         struct ndis_checkmodule *nc;
2493         caddr_t                 kldstart, kldend;
2494
2495         nc = (struct ndis_checkmodule *)context;
2496         if (ndis_find_sym(lf, nc->afilename, "_start", &kldstart))
2497                 return (0);
2498         if (ndis_find_sym(lf, nc->afilename, "_end", &kldend))
2499                 return (0);
2500         nc->fh->nf_vp = lf;
2501         nc->fh->nf_map = NULL;
2502         nc->fh->nf_type = NDIS_FH_TYPE_MODULE;
2503         nc->fh->nf_maplen = (kldend - kldstart) & 0xFFFFFFFF;
2504         return (1);
2505 }
2506
2507 /* can also return NDIS_STATUS_RESOURCES/NDIS_STATUS_ERROR_READING_FILE */
2508 static void
2509 NdisOpenFile(ndis_status *status, ndis_handle *filehandle,
2510     uint32_t *filelength, unicode_string *filename, ndis_physaddr highestaddr)
2511 {
2512         ansi_string             as;
2513         char                    *afilename = NULL;
2514         struct thread           *td = curthread;
2515         struct nlookupdata      nd;
2516         int                     error;
2517         struct vattr            vat;
2518         struct vattr            *vap = &vat;
2519         ndis_fh                 *fh;
2520         char                    *path;
2521         struct ndis_checkmodule nc;
2522         struct vnode            *vp;
2523
2524         if (RtlUnicodeStringToAnsiString(&as, filename, TRUE)) {
2525                 *status = NDIS_STATUS_RESOURCES;
2526                 return;
2527         }
2528
2529         afilename = kstrdup(as.as_buf, M_DEVBUF);
2530         RtlFreeAnsiString(&as);
2531
2532         fh = ExAllocatePoolWithTag(NonPagedPool, sizeof(ndis_fh), 0);
2533         if (fh == NULL) {
2534                 kfree(afilename, M_DEVBUF);
2535                 *status = NDIS_STATUS_RESOURCES;
2536                 return;
2537         }
2538
2539         fh->nf_name = afilename;
2540
2541         /*
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.
2558          */
2559
2560         nc.afilename = afilename;
2561         nc.fh = fh;
2562         if (linker_file_foreach(NdisCheckModule, &nc)) {
2563                 *filelength = fh->nf_maplen;
2564                 *filehandle = fh;
2565                 *status = NDIS_STATUS_SUCCESS;
2566                 return;
2567         }
2568
2569         if (mountlist_boot_getfirst() == NULL) {
2570                 ExFreePool(fh);
2571                 *status = NDIS_STATUS_FILE_NOT_FOUND;
2572                 kprintf("NDIS: could not find file %s in linker list\n",
2573                     afilename);
2574                 kprintf("NDIS: and no filesystems mounted yet, "
2575                     "aborting NdisOpenFile()\n");
2576                 kfree(afilename, M_DEVBUF);
2577                 return;
2578         }
2579
2580         path = ExAllocatePoolWithTag(NonPagedPool, MAXPATHLEN, 0);
2581         if (path == NULL) {
2582                 ExFreePool(fh);
2583                 kfree(afilename, M_DEVBUF);
2584                 *status = NDIS_STATUS_RESOURCES;
2585                 return;
2586         }
2587
2588         ksnprintf(path, MAXPATHLEN, "%s/%s", ndis_filepath, afilename);
2589
2590         /* Some threads don't have a current working directory. */
2591
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;
2596
2597         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
2598         if (error)
2599                 goto init_fail;
2600         error = vn_open(&nd, NULL, FREAD, 0);
2601         if (error)
2602                 goto open_fail;
2603
2604         ExFreePool(path);
2605
2606         vp = nd.nl_open_vp;
2607         nd.nl_open_vp = NULL;
2608
2609         /* Get the file size. */
2610         VOP_GETATTR(vp, vap);
2611         vn_unlock(vp);
2612
2613         fh->nf_vp = nd.nl_open_vp;
2614         fh->nf_map = NULL;
2615         fh->nf_type = NDIS_FH_TYPE_VFS;
2616         *filehandle = fh;
2617         *filelength = fh->nf_maplen = vap->va_size & 0xFFFFFFFF;
2618         *status = NDIS_STATUS_SUCCESS;
2619
2620         return;
2621
2622 open_fail:
2623         nlookup_done(&nd);
2624 init_fail:
2625         *status = NDIS_STATUS_FILE_NOT_FOUND;
2626         ExFreePool(fh);
2627         kprintf("NDIS: open file %s failed: %d\n", path, error);
2628         ExFreePool(path);
2629         kfree(afilename, M_DEVBUF);
2630 }
2631
2632 static void
2633 NdisMapFile(ndis_status *status, void **mappedbuffer, ndis_handle filehandle)
2634 {
2635         ndis_fh                 *fh;
2636         struct thread           *td = curthread;
2637         linker_file_t           lf;
2638         caddr_t                 kldstart;
2639         int                     error, resid;
2640         struct vnode            *vp;
2641
2642         if (filehandle == NULL) {
2643                 *status = NDIS_STATUS_FAILURE;
2644                 return;
2645         }
2646
2647         fh = (ndis_fh *)filehandle;
2648
2649         if (fh->nf_vp == NULL) {
2650                 *status = NDIS_STATUS_FAILURE;
2651                 return;
2652         }
2653
2654         if (fh->nf_map != NULL) {
2655                 *status = NDIS_STATUS_ALREADY_MAPPED;
2656                 return;
2657         }
2658
2659         if (fh->nf_type == NDIS_FH_TYPE_MODULE) {
2660                 lf = fh->nf_vp;
2661                 if (ndis_find_sym(lf, fh->nf_name, "_start", &kldstart)) {
2662                         *status = NDIS_STATUS_FAILURE;
2663                         return;
2664                 }
2665                 fh->nf_map = kldstart;
2666                 *status = NDIS_STATUS_SUCCESS;
2667                 *mappedbuffer = fh->nf_map;
2668                 return;
2669         }
2670
2671         fh->nf_map = ExAllocatePoolWithTag(NonPagedPool, fh->nf_maplen, 0);
2672
2673         if (fh->nf_map == NULL) {
2674                 *status = NDIS_STATUS_RESOURCES;
2675                 return;
2676         }
2677
2678         vp = fh->nf_vp;
2679         error = vn_rdwr(UIO_READ, vp, fh->nf_map, fh->nf_maplen, 0,
2680             UIO_SYSSPACE, 0, td->td_ucred, &resid);
2681
2682         if (error)
2683                 *status = NDIS_STATUS_FAILURE;
2684         else {
2685                 *status = NDIS_STATUS_SUCCESS;
2686                 *mappedbuffer = fh->nf_map;
2687         }
2688 }
2689
2690 static void
2691 NdisUnmapFile(ndis_handle filehandle)
2692 {
2693         ndis_fh                 *fh;
2694         fh = (ndis_fh *)filehandle;
2695
2696         if (fh->nf_map == NULL)
2697                 return;
2698
2699         if (fh->nf_type == NDIS_FH_TYPE_VFS)
2700                 ExFreePool(fh->nf_map);
2701         fh->nf_map = NULL;
2702 }
2703
2704 static void
2705 NdisCloseFile(ndis_handle filehandle)
2706 {
2707         ndis_fh                 *fh;
2708         struct vnode            *vp;
2709
2710         if (filehandle == NULL)
2711                 return;
2712
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);
2717                 fh->nf_map = NULL;
2718         }
2719
2720         if (fh->nf_vp == NULL)
2721                 return;
2722
2723         if (fh->nf_type == NDIS_FH_TYPE_VFS) {
2724                 vp = fh->nf_vp;
2725                 vn_close(vp, FREAD);
2726         }
2727
2728         fh->nf_vp = NULL;
2729         kfree(fh->nf_name, M_DEVBUF);
2730         ExFreePool(fh);
2731 }
2732
2733 static uint8_t
2734 NdisSystemProcessorCount(void)
2735 {
2736         return (ncpus);
2737 }
2738
2739 static void
2740 NdisGetCurrentProcessorCounts(uint32_t *idle_count, uint32_t *kernel_and_user,
2741     uint32_t *index)
2742 {
2743 #if 0 /* XXX swildner */
2744         struct pcpu             *pcpu;
2745
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];
2750 #else
2751         panic("%s", __func__);
2752 #endif
2753 }
2754
2755 typedef void (*ndis_statusdone_handler)(ndis_handle);
2756 typedef void (*ndis_status_handler)(ndis_handle, ndis_status,
2757         void *, uint32_t);
2758
2759 static void
2760 NdisMIndicateStatusComplete(ndis_handle adapter)
2761 {
2762         ndis_miniport_block     *block;
2763         ndis_statusdone_handler statusdonefunc;
2764
2765         block = (ndis_miniport_block *)adapter;
2766         statusdonefunc = block->nmb_statusdone_func;
2767
2768         MSCALL1(statusdonefunc, adapter);
2769 }
2770
2771 static void
2772 NdisMIndicateStatus(ndis_handle adapter, ndis_status status, void *sbuf,
2773     uint32_t slen)
2774 {
2775         ndis_miniport_block     *block;
2776         ndis_status_handler     statusfunc;
2777
2778         block = (ndis_miniport_block *)adapter;
2779         statusfunc = block->nmb_status_func;
2780
2781         MSCALL4(statusfunc, adapter, status, sbuf, slen);
2782 }
2783
2784 /*
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()
2790  * anyway.
2791  *
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().
2800  *
2801  * Got all that? (Sheesh.)
2802  */
2803
2804 ndis_status
2805 NdisScheduleWorkItem(ndis_work_item *work)
2806 {
2807         work_queue_item         *wqi;
2808
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);
2813
2814         return (NDIS_STATUS_SUCCESS);
2815 }
2816
2817 static void
2818 NdisCopyFromPacketToPacket(ndis_packet *dpkt, uint32_t doff, uint32_t reqlen,
2819     ndis_packet *spkt, uint32_t soff, uint32_t *cpylen)
2820 {
2821         ndis_buffer             *src, *dst;
2822         char                    *sptr, *dptr;
2823         int                     resid, copied, len, scnt, dcnt;
2824
2825         *cpylen = 0;
2826
2827         src = spkt->np_private.npp_head;
2828         dst = dpkt->np_private.npp_head;
2829
2830         sptr = MmGetMdlVirtualAddress(src);
2831         dptr = MmGetMdlVirtualAddress(dst);
2832         scnt = MmGetMdlByteCount(src);
2833         dcnt = MmGetMdlByteCount(dst);
2834
2835         while (soff) {
2836                 if (MmGetMdlByteCount(src) > soff) {
2837                         sptr += soff;
2838                         scnt = MmGetMdlByteCount(src)- soff;
2839                         break;
2840                 }
2841                 soff -= MmGetMdlByteCount(src);
2842                 src = src->mdl_next;
2843                 if (src == NULL)
2844                         return;
2845                 sptr = MmGetMdlVirtualAddress(src);
2846         }
2847
2848         while (doff) {
2849                 if (MmGetMdlByteCount(dst) > doff) {
2850                         dptr += doff;
2851                         dcnt = MmGetMdlByteCount(dst) - doff;
2852                         break;
2853                 }
2854                 doff -= MmGetMdlByteCount(dst);
2855                 dst = dst->mdl_next;
2856                 if (dst == NULL)
2857                         return;
2858                 dptr = MmGetMdlVirtualAddress(dst);
2859         }
2860
2861         resid = reqlen;
2862         copied = 0;
2863
2864         while(1) {
2865                 if (resid < scnt)
2866                         len = resid;
2867                 else
2868                         len = scnt;
2869                 if (dcnt < len)
2870                         len = dcnt;
2871
2872                 bcopy(sptr, dptr, len);
2873
2874                 copied += len;
2875                 resid -= len;
2876                 if (resid == 0)
2877                         break;
2878
2879                 dcnt -= len;
2880                 if (dcnt == 0) {
2881                         dst = dst->mdl_next;
2882                         if (dst == NULL)
2883                                 break;
2884                         dptr = MmGetMdlVirtualAddress(dst);
2885                         dcnt = MmGetMdlByteCount(dst);
2886                 }
2887
2888                 scnt -= len;
2889                 if (scnt == 0) {
2890                         src = src->mdl_next;
2891                         if (src == NULL)
2892                                 break;
2893                         sptr = MmGetMdlVirtualAddress(src);
2894                         scnt = MmGetMdlByteCount(src);
2895                 }
2896         }
2897
2898         *cpylen = copied;
2899 }
2900
2901 static void
2902 NdisCopyFromPacketToPacketSafe(ndis_packet *dpkt, uint32_t doff,
2903     uint32_t reqlen, ndis_packet *spkt, uint32_t soff, uint32_t *cpylen,
2904     uint32_t prio)
2905 {
2906         NdisCopyFromPacketToPacket(dpkt, doff, reqlen, spkt, soff, cpylen);
2907 }
2908
2909 static void
2910 NdisIMCopySendPerPacketInfo(ndis_packet *dpkt, ndis_packet *spkt)
2911 {
2912         memcpy(&dpkt->np_ext, &spkt->np_ext, sizeof(ndis_packet_extension));
2913 }
2914
2915 static ndis_status
2916 NdisMRegisterDevice(ndis_handle handle, unicode_string *devname,
2917     unicode_string *symname, driver_dispatch *majorfuncs[], void **devobj,
2918     ndis_handle *devhandle)
2919 {
2920         uint32_t                status;
2921         device_object           *dobj;
2922
2923         status = IoCreateDevice(handle, 0, devname,
2924             FILE_DEVICE_UNKNOWN, 0, FALSE, &dobj);
2925
2926         if (status == STATUS_SUCCESS) {
2927                 *devobj = dobj;
2928                 *devhandle = dobj;
2929         }
2930
2931         return (status);
2932 }
2933
2934 static ndis_status
2935 NdisMDeregisterDevice(ndis_handle handle)
2936 {
2937         IoDeleteDevice(handle);
2938         return (NDIS_STATUS_SUCCESS);
2939 }
2940
2941 static ndis_status
2942 NdisMQueryAdapterInstanceName(unicode_string *name, ndis_handle handle)
2943 {
2944         ndis_miniport_block     *block;
2945         device_t                dev;
2946         ansi_string             as;
2947
2948         block = (ndis_miniport_block *)handle;
2949         dev = block->nmb_physdeviceobj->do_devext;
2950
2951         RtlInitAnsiString(&as, __DECONST(char *, device_get_nameunit(dev)));
2952         if (RtlAnsiStringToUnicodeString(name, &as, TRUE))
2953                 return (NDIS_STATUS_RESOURCES);
2954
2955         return (NDIS_STATUS_SUCCESS);
2956 }
2957
2958 static void
2959 NdisMRegisterUnloadHandler(ndis_handle handle, void *func)
2960 {
2961 }
2962
2963 static void
2964 dummy(void)
2965 {
2966         kprintf("NDIS dummy called...\n");
2967 }
2968
2969 /*
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.
2976  */
2977
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),
3106
3107         /*
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
3111          * in this table.
3112          */
3113
3114         { NULL, (FUNC)dummy, NULL, 0, WINDRV_WRAP_STDCALL },
3115
3116         /* End of list. */
3117
3118         { NULL, NULL, NULL }
3119 };