Sync ndis(4) and tools with FreeBSD and hook it all back into the build.
[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
51 #include <sys/ctype.h>
52 #include <sys/param.h>
53 #include <sys/types.h>
54 #include <sys/errno.h>
55
56 #include <sys/callout.h>
57 #include <sys/kernel.h>
58 #include <sys/systm.h>
59 #include <sys/malloc.h>
60 #include <sys/lock.h>
61 #include <sys/mutex.h>
62 #include <sys/socket.h>
63 #include <sys/sysctl.h>
64 #include <sys/_timespec.h>
65 #include <sys/queue.h>
66 #include <sys/proc.h>
67 #include <sys/filedesc.h>
68 #include <sys/nlookup.h>
69 #include <sys/fcntl.h>
70 #include <sys/vnode.h>
71 #include <sys/kthread.h>
72 #include <sys/linker.h>
73 #include <sys/mount.h>
74 #include <sys/sysproto.h>
75
76 #include <net/if.h>
77 #include <net/if_arp.h>
78 #include <net/ethernet.h>
79 #include <net/if_dl.h>
80 #include <net/if_media.h>
81
82 #include <machine/atomic.h>
83
84 #include <sys/bus.h>
85 #include <sys/rman.h>
86 #include <sys/mplock2.h>
87
88 #include <netproto/802_11/ieee80211_var.h>
89 #include <netproto/802_11/ieee80211_ioctl.h>
90
91 #include <bus/pci/pcireg.h>
92 #include <bus/pci/pcivar.h>
93 #include <bus/usb/usb.h>
94 #include <bus/usb/usbdi.h>
95
96 #include <emulation/ndis/pe_var.h>
97 #include <emulation/ndis/cfg_var.h>
98 #include <emulation/ndis/resource_var.h>
99 #include <emulation/ndis/ntoskrnl_var.h>
100 #include <emulation/ndis/hal_var.h>
101 #include <emulation/ndis/ndis_var.h>
102 #include <dev/netif/ndis/if_ndisvar.h>
103
104 #include <vm/vm.h>
105 #include <vm/vm_param.h>
106 #include <vm/pmap.h>
107 #include <vm/vm_kern.h>
108 #include <vm/vm_map.h>
109
110 #include <stdarg.h>
111
112 static char ndis_filepath[MAXPATHLEN];
113
114 SYSCTL_STRING(_hw, OID_AUTO, ndis_filepath, CTLFLAG_RW, ndis_filepath,
115         MAXPATHLEN, "Path used by NdisOpenFile() to search for files");
116
117 static void NdisInitializeWrapper(ndis_handle *,
118         driver_object *, void *, void *);
119 static ndis_status NdisMRegisterMiniport(ndis_handle,
120         ndis_miniport_characteristics *, int);
121 static ndis_status NdisAllocateMemoryWithTag(void **,
122         uint32_t, uint32_t);
123 static ndis_status NdisAllocateMemory(void **,
124         uint32_t, uint32_t, ndis_physaddr);
125 static void NdisFreeMemory(void *, uint32_t, uint32_t);
126 static ndis_status NdisMSetAttributesEx(ndis_handle, ndis_handle,
127         uint32_t, uint32_t, ndis_interface_type);
128 static void NdisOpenConfiguration(ndis_status *,
129         ndis_handle *, ndis_handle);
130 static void NdisOpenConfigurationKeyByIndex(ndis_status *,
131         ndis_handle, uint32_t, unicode_string *, ndis_handle *);
132 static void NdisOpenConfigurationKeyByName(ndis_status *,
133         ndis_handle, unicode_string *, ndis_handle *);
134 static ndis_status ndis_encode_parm(ndis_miniport_block *,
135         struct sysctl_oid *, ndis_parm_type, ndis_config_parm **);
136 static ndis_status ndis_decode_parm(ndis_miniport_block *,
137         ndis_config_parm *, char *);
138 static void NdisReadConfiguration(ndis_status *, ndis_config_parm **,
139         ndis_handle, unicode_string *, ndis_parm_type);
140 static void NdisWriteConfiguration(ndis_status *, ndis_handle,
141         unicode_string *, ndis_config_parm *);
142 static void NdisCloseConfiguration(ndis_handle);
143 static void NdisAllocateSpinLock(ndis_spin_lock *);
144 static void NdisFreeSpinLock(ndis_spin_lock *);
145 static void NdisAcquireSpinLock(ndis_spin_lock *);
146 static void NdisReleaseSpinLock(ndis_spin_lock *);
147 static void NdisDprAcquireSpinLock(ndis_spin_lock *);
148 static void NdisDprReleaseSpinLock(ndis_spin_lock *);
149 static void NdisInitializeReadWriteLock(ndis_rw_lock *);
150 static void NdisAcquireReadWriteLock(ndis_rw_lock *,
151         uint8_t, ndis_lock_state *);
152 static void NdisReleaseReadWriteLock(ndis_rw_lock *, ndis_lock_state *);
153 static uint32_t NdisReadPciSlotInformation(ndis_handle, uint32_t,
154         uint32_t, void *, uint32_t);
155 static uint32_t NdisWritePciSlotInformation(ndis_handle, uint32_t,
156         uint32_t, void *, uint32_t);
157 static void NdisWriteErrorLogEntry(ndis_handle, ndis_error_code, uint32_t, ...);
158 static void ndis_map_cb(void *, bus_dma_segment_t *, int, int);
159 static void NdisMStartBufferPhysicalMapping(ndis_handle,
160         ndis_buffer *, uint32_t, uint8_t, ndis_paddr_unit *, uint32_t *);
161 static void NdisMCompleteBufferPhysicalMapping(ndis_handle,
162         ndis_buffer *, uint32_t);
163 static void NdisMInitializeTimer(ndis_miniport_timer *, ndis_handle,
164         ndis_timer_function, void *);
165 static void NdisInitializeTimer(ndis_timer *,
166         ndis_timer_function, void *);
167 static void NdisSetTimer(ndis_timer *, uint32_t);
168 static void NdisMSetPeriodicTimer(ndis_miniport_timer *, uint32_t);
169 static void NdisMCancelTimer(ndis_timer *, uint8_t *);
170 static void ndis_timercall(kdpc *, ndis_miniport_timer *,
171         void *, void *);
172 static void NdisMQueryAdapterResources(ndis_status *, ndis_handle,
173         ndis_resource_list *, uint32_t *);
174 static ndis_status NdisMRegisterIoPortRange(void **,
175         ndis_handle, uint32_t, uint32_t);
176 static void NdisMDeregisterIoPortRange(ndis_handle,
177         uint32_t, uint32_t, void *);
178 static void NdisReadNetworkAddress(ndis_status *, void **,
179         uint32_t *, ndis_handle);
180 static ndis_status NdisQueryMapRegisterCount(uint32_t, uint32_t *);
181 static ndis_status NdisMAllocateMapRegisters(ndis_handle,
182         uint32_t, uint8_t, uint32_t, uint32_t);
183 static void NdisMFreeMapRegisters(ndis_handle);
184 static void ndis_mapshared_cb(void *, bus_dma_segment_t *, int, int);
185 static void NdisMAllocateSharedMemory(ndis_handle, uint32_t,
186         uint8_t, void **, ndis_physaddr *);
187 static void ndis_asyncmem_complete(device_object *, void *);
188 static ndis_status NdisMAllocateSharedMemoryAsync(ndis_handle,
189         uint32_t, uint8_t, void *);
190 static void NdisMFreeSharedMemory(ndis_handle, uint32_t,
191         uint8_t, void *, ndis_physaddr);
192 static ndis_status NdisMMapIoSpace(void **, ndis_handle,
193         ndis_physaddr, uint32_t);
194 static void NdisMUnmapIoSpace(ndis_handle, void *, uint32_t);
195 static uint32_t NdisGetCacheFillSize(void);
196 static void *NdisGetRoutineAddress(unicode_string *);
197 static uint32_t NdisMGetDmaAlignment(ndis_handle);
198 static ndis_status NdisMInitializeScatterGatherDma(ndis_handle,
199         uint8_t, uint32_t);
200 static void NdisUnchainBufferAtFront(ndis_packet *, ndis_buffer **);
201 static void NdisUnchainBufferAtBack(ndis_packet *, ndis_buffer **);
202 static void NdisAllocateBufferPool(ndis_status *,
203         ndis_handle *, uint32_t);
204 static void NdisFreeBufferPool(ndis_handle);
205 static void NdisAllocateBuffer(ndis_status *, ndis_buffer **,
206         ndis_handle, void *, uint32_t);
207 static void NdisFreeBuffer(ndis_buffer *);
208 static uint32_t NdisBufferLength(ndis_buffer *);
209 static void NdisQueryBuffer(ndis_buffer *, void **, uint32_t *);
210 static void NdisQueryBufferSafe(ndis_buffer *, void **,
211         uint32_t *, uint32_t);
212 static void *NdisBufferVirtualAddress(ndis_buffer *);
213 static void *NdisBufferVirtualAddressSafe(ndis_buffer *, uint32_t);
214 static void NdisAdjustBufferLength(ndis_buffer *, int);
215 static uint32_t NdisInterlockedIncrement(uint32_t *);
216 static uint32_t NdisInterlockedDecrement(uint32_t *);
217 static void NdisInitializeEvent(ndis_event *);
218 static void NdisSetEvent(ndis_event *);
219 static void NdisResetEvent(ndis_event *);
220 static uint8_t NdisWaitEvent(ndis_event *, uint32_t);
221 static ndis_status NdisUnicodeStringToAnsiString(ansi_string *,
222         unicode_string *);
223 static ndis_status
224         NdisAnsiStringToUnicodeString(unicode_string *, ansi_string *);
225 static ndis_status NdisMPciAssignResources(ndis_handle,
226         uint32_t, ndis_resource_list **);
227 static ndis_status NdisMRegisterInterrupt(ndis_miniport_interrupt *,
228         ndis_handle, uint32_t, uint32_t, uint8_t,
229         uint8_t, ndis_interrupt_mode);
230 static void NdisMDeregisterInterrupt(ndis_miniport_interrupt *);
231 static void NdisMRegisterAdapterShutdownHandler(ndis_handle, void *,
232         ndis_shutdown_handler);
233 static void NdisMDeregisterAdapterShutdownHandler(ndis_handle);
234 static uint32_t NDIS_BUFFER_TO_SPAN_PAGES(ndis_buffer *);
235 static void NdisGetBufferPhysicalArraySize(ndis_buffer *,
236         uint32_t *);
237 static void NdisQueryBufferOffset(ndis_buffer *,
238         uint32_t *, uint32_t *);
239 static uint32_t NdisReadPcmciaAttributeMemory(ndis_handle,
240         uint32_t, void *, uint32_t);
241 static uint32_t NdisWritePcmciaAttributeMemory(ndis_handle,
242         uint32_t, void *, uint32_t);
243 static list_entry *NdisInterlockedInsertHeadList(list_entry *,
244         list_entry *, ndis_spin_lock *);
245 static list_entry *NdisInterlockedRemoveHeadList(list_entry *,
246         ndis_spin_lock *);
247 static list_entry *NdisInterlockedInsertTailList(list_entry *,
248         list_entry *, ndis_spin_lock *);
249 static uint8_t
250         NdisMSynchronizeWithInterrupt(ndis_miniport_interrupt *,
251         void *, void *);
252 static void NdisGetCurrentSystemTime(uint64_t *);
253 static void NdisGetSystemUpTime(uint32_t *);
254 static uint32_t NdisGetVersion(void);
255 static void NdisInitializeString(unicode_string *, char *);
256 static void NdisInitAnsiString(ansi_string *, char *);
257 static void NdisInitUnicodeString(unicode_string *, uint16_t *);
258 static void NdisFreeString(unicode_string *);
259 static ndis_status NdisMRemoveMiniport(ndis_handle *);
260 static void NdisTerminateWrapper(ndis_handle, void *);
261 static void NdisMGetDeviceProperty(ndis_handle, device_object **,
262         device_object **, device_object **, cm_resource_list *,
263         cm_resource_list *);
264 static void NdisGetFirstBufferFromPacket(ndis_packet *,
265         ndis_buffer **, void **, uint32_t *, uint32_t *);
266 static void NdisGetFirstBufferFromPacketSafe(ndis_packet *,
267         ndis_buffer **, void **, uint32_t *, uint32_t *, uint32_t);
268 static int ndis_find_sym(linker_file_t, char *, char *, caddr_t *);
269 static void NdisOpenFile(ndis_status *, ndis_handle *, uint32_t *,
270         unicode_string *, ndis_physaddr);
271 static void NdisMapFile(ndis_status *, void **, ndis_handle);
272 static void NdisUnmapFile(ndis_handle);
273 static void NdisCloseFile(ndis_handle);
274 static uint8_t NdisSystemProcessorCount(void);
275 static void NdisGetCurrentProcessorCounts(uint32_t *, uint32_t *, uint32_t *);
276 static void NdisMIndicateStatusComplete(ndis_handle);
277 static void NdisMIndicateStatus(ndis_handle, ndis_status,
278         void *, uint32_t);
279 static uint8_t ndis_intr(kinterrupt *, void *);
280 static void ndis_intrhand(kdpc *, ndis_miniport_interrupt *, void *, void *);
281 static funcptr ndis_findwrap(funcptr);
282 static void NdisCopyFromPacketToPacket(ndis_packet *,
283         uint32_t, uint32_t, ndis_packet *, uint32_t, uint32_t *);
284 static void NdisCopyFromPacketToPacketSafe(ndis_packet *,
285         uint32_t, uint32_t, ndis_packet *, uint32_t, uint32_t *, uint32_t);
286 static void NdisIMCopySendPerPacketInfo(ndis_packet *, ndis_packet *);
287 static ndis_status NdisMRegisterDevice(ndis_handle,
288         unicode_string *, unicode_string *, driver_dispatch **,
289         void **, ndis_handle *);
290 static ndis_status NdisMDeregisterDevice(ndis_handle);
291 static ndis_status
292         NdisMQueryAdapterInstanceName(unicode_string *, ndis_handle);
293 static void NdisMRegisterUnloadHandler(ndis_handle, void *);
294 static void dummy(void);
295
296 /*
297  * Some really old drivers do not properly check the return value
298  * from NdisAllocatePacket() and NdisAllocateBuffer() and will
299  * sometimes allocate few more buffers/packets that they originally
300  * requested when they created the pool. To prevent this from being
301  * a problem, we allocate a few extra buffers/packets beyond what
302  * the driver asks for. This #define controls how many.
303  */
304 #define NDIS_POOL_EXTRA         16
305
306 int
307 ndis_libinit(void)
308 {
309         image_patch_table       *patch;
310
311         strcpy(ndis_filepath, "/compat/ndis");
312
313         patch = ndis_functbl;
314         while (patch->ipt_func != NULL) {
315                 windrv_wrap((funcptr)patch->ipt_func,
316                     (funcptr *)&patch->ipt_wrap,
317                     patch->ipt_argcnt, patch->ipt_ftype);
318                 patch++;
319         }
320
321         return (0);
322 }
323
324 int
325 ndis_libfini(void)
326 {
327         image_patch_table       *patch;
328
329         patch = ndis_functbl;
330         while (patch->ipt_func != NULL) {
331                 windrv_unwrap(patch->ipt_wrap);
332                 patch++;
333         }
334
335         return (0);
336 }
337
338 static funcptr
339 ndis_findwrap(funcptr func)
340 {
341         image_patch_table       *patch;
342
343         patch = ndis_functbl;
344         while (patch->ipt_func != NULL) {
345                 if ((funcptr)patch->ipt_func == func)
346                         return((funcptr)patch->ipt_wrap);
347                 patch++;
348         }
349
350         return (NULL);
351 }
352
353 /*
354  * This routine does the messy Windows Driver Model device attachment
355  * stuff on behalf of NDIS drivers. We register our own AddDevice
356  * routine here
357  */
358 static void
359 NdisInitializeWrapper(ndis_handle *wrapper, driver_object *drv, void *path,
360     void *unused)
361 {
362         /*
363          * As of yet, I haven't come up with a compelling
364          * reason to define a private NDIS wrapper structure,
365          * so we use a pointer to the driver object as the
366          * wrapper handle. The driver object has the miniport
367          * characteristics struct for this driver hung off it
368          * via IoAllocateDriverObjectExtension(), and that's
369          * really all the private data we need.
370          */
371
372         *wrapper = drv;
373
374         /*
375          * If this was really Windows, we'd be registering dispatch
376          * routines for the NDIS miniport module here, but we're
377          * not Windows so all we really need to do is set up an
378          * AddDevice function that'll be invoked when a new device
379          * instance appears.
380          */
381
382         drv->dro_driverext->dre_adddevicefunc = NdisAddDevice;
383 }
384
385 static void
386 NdisTerminateWrapper(ndis_handle handle, void *syspec)
387 {
388         /* Nothing to see here, move along. */
389 }
390
391 static ndis_status
392 NdisMRegisterMiniport(ndis_handle handle,
393     ndis_miniport_characteristics *characteristics, int len)
394 {
395         ndis_miniport_characteristics   *ch = NULL;
396         driver_object           *drv;
397
398         drv = (driver_object *)handle;
399
400         /*
401          * We need to save the NDIS miniport characteristics
402          * somewhere. This data is per-driver, not per-device
403          * (all devices handled by the same driver have the
404          * same characteristics) so we hook it onto the driver
405          * object using IoAllocateDriverObjectExtension().
406          * The extra extension info is automagically deleted when
407          * the driver is unloaded (see windrv_unload()).
408          */
409
410         if (IoAllocateDriverObjectExtension(drv, (void *)1,
411             sizeof(ndis_miniport_characteristics), (void **)&ch) !=
412             STATUS_SUCCESS) {
413                 return (NDIS_STATUS_RESOURCES);
414         }
415
416         bzero((char *)ch, sizeof(ndis_miniport_characteristics));
417
418         bcopy((char *)characteristics, (char *)ch, len);
419
420         if (ch->nmc_version_major < 5 || ch->nmc_version_minor < 1) {
421                 ch->nmc_shutdown_handler = NULL;
422                 ch->nmc_canceltxpkts_handler = NULL;
423                 ch->nmc_pnpevent_handler = NULL;
424         }
425
426         return (NDIS_STATUS_SUCCESS);
427 }
428
429 static ndis_status
430 NdisAllocateMemoryWithTag(void **vaddr, uint32_t len, uint32_t tag)
431 {
432         void                    *mem;
433
434         mem = ExAllocatePoolWithTag(NonPagedPool, len, tag);
435         if (mem == NULL) {
436                 return (NDIS_STATUS_RESOURCES);
437         }
438         *vaddr = mem;
439
440         return (NDIS_STATUS_SUCCESS);
441 }
442
443 static ndis_status
444 NdisAllocateMemory(void **vaddr, uint32_t len, uint32_t flags,
445     ndis_physaddr highaddr)
446 {
447         void                    *mem;
448
449         mem = ExAllocatePoolWithTag(NonPagedPool, len, 0);
450         if (mem == NULL)
451                 return (NDIS_STATUS_RESOURCES);
452         *vaddr = mem;
453
454         return (NDIS_STATUS_SUCCESS);
455 }
456
457 static void
458 NdisFreeMemory(void *vaddr, uint32_t len, uint32_t flags)
459 {
460         if (len == 0)
461                 return;
462
463         ExFreePool(vaddr);
464 }
465
466 static ndis_status
467 NdisMSetAttributesEx(ndis_handle adapter_handle, ndis_handle adapter_ctx,
468     uint32_t hangsecs, uint32_t flags, ndis_interface_type iftype)
469 {
470         ndis_miniport_block             *block;
471
472         /*
473          * Save the adapter context, we need it for calling
474          * the driver's internal functions.
475          */
476         block = (ndis_miniport_block *)adapter_handle;
477         block->nmb_miniportadapterctx = adapter_ctx;
478         block->nmb_checkforhangsecs = hangsecs;
479         block->nmb_flags = flags;
480
481         return (NDIS_STATUS_SUCCESS);
482 }
483
484 static void
485 NdisOpenConfiguration(ndis_status *status, ndis_handle *cfg,
486     ndis_handle wrapctx)
487 {
488         *cfg = wrapctx;
489         *status = NDIS_STATUS_SUCCESS;
490 }
491
492 static void
493 NdisOpenConfigurationKeyByName(ndis_status *status, ndis_handle cfg,
494     unicode_string *subkey, ndis_handle *subhandle)
495 {
496         *subhandle = cfg;
497         *status = NDIS_STATUS_SUCCESS;
498 }
499
500 static void
501 NdisOpenConfigurationKeyByIndex(ndis_status *status, ndis_handle cfg,
502     uint32_t idx, unicode_string *subkey, ndis_handle *subhandle)
503 {
504         *status = NDIS_STATUS_FAILURE;
505 }
506
507 static ndis_status
508 ndis_encode_parm(ndis_miniport_block *block, struct sysctl_oid *oid,
509     ndis_parm_type type, ndis_config_parm **parm)
510 {
511         ndis_config_parm        *p;
512         ndis_parmlist_entry     *np;
513         unicode_string          *us;
514         ansi_string             as;
515         int                     base = 0;
516         uint32_t                val;
517         char                    tmp[32];
518
519         np = ExAllocatePoolWithTag(NonPagedPool,
520             sizeof(ndis_parmlist_entry), 0);
521         if (np == NULL)
522                 return (NDIS_STATUS_RESOURCES);
523         InsertHeadList((&block->nmb_parmlist), (&np->np_list));
524         *parm = p = &np->np_parm;
525
526         switch(type) {
527         case ndis_parm_string:
528                 /* See if this might be a number. */
529                 val = strtoul((char *)oid->oid_arg1, NULL, 10);
530                 us = &p->ncp_parmdata.ncp_stringdata;
531                 p->ncp_type = ndis_parm_string;
532                 if (val) {
533                         ksnprintf(tmp, 32, "%x", val);
534                         RtlInitAnsiString(&as, tmp);
535                 } else {
536                         RtlInitAnsiString(&as, (char *)oid->oid_arg1);
537                 }
538
539                 if (RtlAnsiStringToUnicodeString(us, &as, TRUE)) {
540                         ExFreePool(np);
541                         return (NDIS_STATUS_RESOURCES);
542                 }
543                 break;
544         case ndis_parm_int:
545                 if (strncmp((char *)oid->oid_arg1, "0x", 2) == 0)
546                         base = 16;
547                 else
548                         base = 10;
549                 p->ncp_type = ndis_parm_int;
550                 p->ncp_parmdata.ncp_intdata =
551                     strtol((char *)oid->oid_arg1, NULL, base);
552                 break;
553         case ndis_parm_hexint:
554 #ifdef notdef
555                 if (strncmp((char *)oid->oid_arg1, "0x", 2) == 0)
556                         base = 16;
557                 else
558                         base = 10;
559 #endif
560                 base = 16;
561                 p->ncp_type = ndis_parm_hexint;
562                 p->ncp_parmdata.ncp_intdata =
563                     strtoul((char *)oid->oid_arg1, NULL, base);
564                 break;
565         default:
566                 return (NDIS_STATUS_FAILURE);
567                 break;
568         }
569
570         return (NDIS_STATUS_SUCCESS);
571 }
572
573 static void
574 NdisReadConfiguration(ndis_status *status, ndis_config_parm **parm,
575     ndis_handle cfg, unicode_string *key, ndis_parm_type type)
576 {
577         char                    *keystr = NULL;
578         ndis_miniport_block     *block;
579         struct ndis_softc       *sc;
580         struct sysctl_oid       *oidp;
581         struct sysctl_ctx_entry *e;
582         ansi_string             as;
583
584         block = (ndis_miniport_block *)cfg;
585         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
586
587         if (key->us_len == 0 || key->us_buf == NULL) {
588                 *status = NDIS_STATUS_FAILURE;
589                 return;
590         }
591
592         if (RtlUnicodeStringToAnsiString(&as, key, TRUE)) {
593                 *status = NDIS_STATUS_RESOURCES;
594                 return;
595         }
596
597         keystr = as.as_buf;
598
599         /*
600          * See if registry key is already in a list of known keys
601          * included with the driver.
602          */
603         TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
604                 oidp = e->entry;
605                 if (strcasecmp(oidp->oid_name, keystr) == 0) {
606                         if (strcmp((char *)oidp->oid_arg1, "UNSET") == 0) {
607                                 RtlFreeAnsiString(&as);
608                                 *status = NDIS_STATUS_FAILURE;
609                                 return;
610                         }
611
612                         *status = ndis_encode_parm(block, oidp, type, parm);
613                         RtlFreeAnsiString(&as);
614                         return;
615                 }
616         }
617
618         /*
619          * If the key didn't match, add it to the list of dynamically
620          * created ones. Sometimes, drivers refer to registry keys
621          * that aren't documented in their .INF files. These keys
622          * are supposed to be created by some sort of utility or
623          * control panel snap-in that comes with the driver software.
624          * Sometimes it's useful to be able to manipulate these.
625          * If the driver requests the key in the form of a string,
626          * make its default value an empty string, otherwise default
627          * it to "0".
628          */
629
630         if (type == ndis_parm_int || type == ndis_parm_hexint)
631                 ndis_add_sysctl(sc, keystr, "(dynamic integer key)",
632                     "UNSET", CTLFLAG_RW);
633         else
634                 ndis_add_sysctl(sc, keystr, "(dynamic string key)",
635                     "UNSET", CTLFLAG_RW);
636
637         RtlFreeAnsiString(&as);
638         *status = NDIS_STATUS_FAILURE;
639 }
640
641 static ndis_status
642 ndis_decode_parm(ndis_miniport_block *block, ndis_config_parm *parm, char *val)
643 {
644         unicode_string          *ustr;
645         ansi_string             as;
646
647         switch(parm->ncp_type) {
648         case ndis_parm_string:
649                 ustr = &parm->ncp_parmdata.ncp_stringdata;
650                 if (RtlUnicodeStringToAnsiString(&as, ustr, TRUE))
651                         return (NDIS_STATUS_RESOURCES);
652                 bcopy(as.as_buf, val, as.as_len);
653                 RtlFreeAnsiString(&as);
654                 break;
655         case ndis_parm_int:
656                 ksprintf(val, "%d", parm->ncp_parmdata.ncp_intdata);
657                 break;
658         case ndis_parm_hexint:
659                 ksprintf(val, "%xu", parm->ncp_parmdata.ncp_intdata);
660                 break;
661         default:
662                 return (NDIS_STATUS_FAILURE);
663                 break;
664         }
665         return (NDIS_STATUS_SUCCESS);
666 }
667
668 static void
669 NdisWriteConfiguration(ndis_status *status, ndis_handle cfg,
670     unicode_string *key, ndis_config_parm *parm)
671 {
672         ansi_string             as;
673         char                    *keystr = NULL;
674         ndis_miniport_block     *block;
675         struct ndis_softc       *sc;
676         struct sysctl_oid       *oidp;
677         struct sysctl_ctx_entry *e;
678         char                    val[256];
679
680         block = (ndis_miniport_block *)cfg;
681         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
682
683         if (RtlUnicodeStringToAnsiString(&as, key, TRUE)) {
684                 *status = NDIS_STATUS_RESOURCES;
685                 return;
686         }
687
688         keystr = as.as_buf;
689
690         /* Decode the parameter into a string. */
691         bzero(val, sizeof(val));
692         *status = ndis_decode_parm(block, parm, val);
693         if (*status != NDIS_STATUS_SUCCESS) {
694                 RtlFreeAnsiString(&as);
695                 return;
696         }
697
698         /* See if the key already exists. */
699
700         TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
701                 oidp = e->entry;
702                 if (strcasecmp(oidp->oid_name, keystr) == 0) {
703                         /* Found it, set the value. */
704                         strcpy((char *)oidp->oid_arg1, val);
705                         RtlFreeAnsiString(&as);
706                         return;
707                 }
708         }
709
710         /* Not found, add a new key with the specified value. */
711         ndis_add_sysctl(sc, keystr, "(dynamically set key)",
712                     val, CTLFLAG_RW);
713
714         RtlFreeAnsiString(&as);
715         *status = NDIS_STATUS_SUCCESS;
716 }
717
718 static void
719 NdisCloseConfiguration(ndis_handle cfg)
720 {
721         list_entry              *e;
722         ndis_parmlist_entry     *pe;
723         ndis_miniport_block     *block;
724         ndis_config_parm        *p;
725
726         block = (ndis_miniport_block *)cfg;
727
728         while (!IsListEmpty(&block->nmb_parmlist)) {
729                 e = RemoveHeadList(&block->nmb_parmlist);
730                 pe = CONTAINING_RECORD(e, ndis_parmlist_entry, np_list);
731                 p = &pe->np_parm;
732                 if (p->ncp_type == ndis_parm_string)
733                         RtlFreeUnicodeString(&p->ncp_parmdata.ncp_stringdata);
734                 ExFreePool(e);
735         }
736 }
737
738 /*
739  * Initialize a Windows spinlock.
740  */
741 static void
742 NdisAllocateSpinLock(ndis_spin_lock *lock)
743 {
744         KeInitializeSpinLock(&lock->nsl_spinlock);
745         lock->nsl_kirql = 0;
746 }
747
748 /*
749  * Destroy a Windows spinlock. This is a no-op for now. There are two reasons
750  * for this. One is that it's sort of superfluous: we don't have to do anything
751  * special to deallocate the spinlock. The other is that there are some buggy
752  * drivers which call NdisFreeSpinLock() _after_ calling NdisFreeMemory() on
753  * the block of memory in which the spinlock resides. (Yes, ADMtek, I'm
754  * talking to you.)
755  */
756 static void
757 NdisFreeSpinLock(ndis_spin_lock *lock)
758 {
759 #ifdef notdef
760         KeInitializeSpinLock(&lock->nsl_spinlock);
761         lock->nsl_kirql = 0;
762 #endif
763 }
764
765 /*
766  * Acquire a spinlock from IRQL <= DISPATCH_LEVEL.
767  */
768
769 static void
770 NdisAcquireSpinLock(ndis_spin_lock *lock)
771 {
772         KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
773 }
774
775 /*
776  * Release a spinlock from IRQL == DISPATCH_LEVEL.
777  */
778
779 static void
780 NdisReleaseSpinLock(ndis_spin_lock *lock)
781 {
782         KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
783 }
784
785 /*
786  * Acquire a spinlock when already running at IRQL == DISPATCH_LEVEL.
787  */
788 static void
789 NdisDprAcquireSpinLock(ndis_spin_lock *lock)
790 {
791         KeAcquireSpinLockAtDpcLevel(&lock->nsl_spinlock);
792 }
793
794 /*
795  * Release a spinlock without leaving IRQL == DISPATCH_LEVEL.
796  */
797 static void
798 NdisDprReleaseSpinLock(ndis_spin_lock *lock)
799 {
800         KeReleaseSpinLockFromDpcLevel(&lock->nsl_spinlock);
801 }
802
803 static void
804 NdisInitializeReadWriteLock(ndis_rw_lock *lock)
805 {
806         KeInitializeSpinLock(&lock->nrl_spinlock);
807         bzero((char *)&lock->nrl_rsvd, sizeof(lock->nrl_rsvd));
808 }
809
810 static void
811 NdisAcquireReadWriteLock(ndis_rw_lock *lock, uint8_t writeacc,
812     ndis_lock_state *state)
813 {
814         if (writeacc == TRUE) {
815                 KeAcquireSpinLock(&lock->nrl_spinlock, &state->nls_oldirql);
816                 lock->nrl_rsvd[0]++;
817         } else
818                 lock->nrl_rsvd[1]++;
819 }
820
821 static void
822 NdisReleaseReadWriteLock(ndis_rw_lock *lock, ndis_lock_state *state)
823 {
824         if (lock->nrl_rsvd[0]) {
825                 lock->nrl_rsvd[0]--;
826                 KeReleaseSpinLock(&lock->nrl_spinlock, state->nls_oldirql);
827         } else
828                 lock->nrl_rsvd[1]--;
829 }
830
831 static uint32_t
832 NdisReadPciSlotInformation(ndis_handle adapter, uint32_t slot, uint32_t offset,
833     void *buf, uint32_t len)
834 {
835         ndis_miniport_block     *block;
836         int                     i;
837         char                    *dest;
838         device_t                dev;
839
840         block = (ndis_miniport_block *)adapter;
841         dest = buf;
842         if (block == NULL)
843                 return (0);
844
845         dev = block->nmb_physdeviceobj->do_devext;
846
847         /*
848          * I have a test system consisting of a Sun w2100z
849          * dual 2.4Ghz Opteron machine and an Atheros 802.11a/b/g
850          * "Aries" miniPCI NIC. (The NIC is installed in the
851          * machine using a miniPCI to PCI bus adapter card.)
852          * When running in SMP mode, I found that
853          * performing a large number of consecutive calls to
854          * NdisReadPciSlotInformation() would result in a
855          * sudden system reset (or in some cases a freeze).
856          * My suspicion is that the multiple reads are somehow
857          * triggering a fatal PCI bus error that leads to a
858          * machine check. The 1us delay in the loop below
859          * seems to prevent this problem.
860          */
861
862         for (i = 0; i < len; i++) {
863                 DELAY(1);
864                 dest[i] = pci_read_config(dev, i + offset, 1);
865         }
866
867         return (len);
868 }
869
870 static uint32_t
871 NdisWritePciSlotInformation(ndis_handle adapter, uint32_t slot,
872     uint32_t offset, void *buf, uint32_t len)
873 {
874         ndis_miniport_block     *block;
875         int                     i;
876         char                    *dest;
877         device_t                dev;
878
879         block = (ndis_miniport_block *)adapter;
880         dest = buf;
881
882         if (block == NULL)
883                 return (0);
884
885         dev = block->nmb_physdeviceobj->do_devext;
886         for (i = 0; i < len; i++) {
887                 DELAY(1);
888                 pci_write_config(dev, i + offset, dest[i], 1);
889         }
890
891         return (len);
892 }
893
894 /*
895  * The errorlog routine uses a variable argument list, so we
896  * have to declare it this way.
897  */
898
899 #define ERRMSGLEN 512
900 static void
901 NdisWriteErrorLogEntry(ndis_handle adapter, ndis_error_code code,
902         uint32_t numerrors, ...)
903 {
904         ndis_miniport_block     *block;
905         va_list                 ap;
906         int                     i, error;
907         char                    *str = NULL;
908         uint16_t                flags;
909         device_t                dev;
910         driver_object           *drv;
911         struct ndis_softc       *sc;
912         struct ifnet            *ifp;
913         unicode_string          us;
914         ansi_string             as = { 0, 0, NULL };
915
916         block = (ndis_miniport_block *)adapter;
917         dev = block->nmb_physdeviceobj->do_devext;
918         drv = block->nmb_deviceobj->do_drvobj;
919         sc = device_get_softc(dev);
920         ifp = sc->ifp;
921
922         if (ifp != NULL && ifp->if_flags & IFF_DEBUG) {
923                 error = pe_get_message((vm_offset_t)drv->dro_driverstart,
924                     code, &str, &i, &flags);
925                 if (error == 0) {
926                         if (flags & MESSAGE_RESOURCE_UNICODE) {
927                                 RtlInitUnicodeString(&us, (uint16_t *)str);
928                                 if (RtlUnicodeStringToAnsiString(&as,
929                                     &us, TRUE) == STATUS_SUCCESS)
930                                         str = as.as_buf;
931                                 else
932                                         str = NULL;
933                         }
934                 }
935         }
936
937         device_printf(dev, "NDIS ERROR: %x (%s)\n", code,
938             str == NULL ? "unknown error" : str);
939
940         if (ifp != NULL && ifp->if_flags & IFF_DEBUG) {
941                 device_printf(dev, "NDIS NUMERRORS: %x\n", numerrors);
942                 va_start(ap, numerrors);
943                 for (i = 0; i < numerrors; i++)
944                         device_printf(dev, "argptr: %p\n",
945                             va_arg(ap, void *));
946                 va_end(ap);
947         }
948
949         if (as.as_len)
950                 RtlFreeAnsiString(&as);
951 }
952
953 static void
954 ndis_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
955 {
956         struct ndis_map_arg     *ctx;
957         int                     i;
958
959         if (error)
960                 return;
961
962         ctx = arg;
963
964         for (i = 0; i < nseg; i++) {
965                 ctx->nma_fraglist[i].npu_physaddr.np_quad = segs[i].ds_addr;
966                 ctx->nma_fraglist[i].npu_len = segs[i].ds_len;
967         }
968
969         ctx->nma_cnt = nseg;
970 }
971
972 static void
973 NdisMStartBufferPhysicalMapping(ndis_handle adapter, ndis_buffer *buf,
974     uint32_t mapreg, uint8_t writedev, ndis_paddr_unit *addrarray,
975     uint32_t *arraysize)
976 {
977         ndis_miniport_block     *block;
978         struct ndis_softc       *sc;
979         struct ndis_map_arg     nma;
980         bus_dmamap_t            map;
981         int                     error;
982
983         if (adapter == NULL)
984                 return;
985
986         block = (ndis_miniport_block *)adapter;
987         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
988
989         if (mapreg > sc->ndis_mmapcnt)
990                 return;
991
992         map = sc->ndis_mmaps[mapreg];
993         nma.nma_fraglist = addrarray;
994
995         error = bus_dmamap_load(sc->ndis_mtag, map,
996             MmGetMdlVirtualAddress(buf), MmGetMdlByteCount(buf), ndis_map_cb,
997             (void *)&nma, BUS_DMA_NOWAIT);
998
999         if (error)
1000                 return;
1001
1002         bus_dmamap_sync(sc->ndis_mtag, map,
1003             writedev ? BUS_DMASYNC_PREWRITE : BUS_DMASYNC_PREREAD);
1004
1005         *arraysize = nma.nma_cnt;
1006 }
1007
1008 static void
1009 NdisMCompleteBufferPhysicalMapping(ndis_handle adapter, ndis_buffer *buf,
1010     uint32_t mapreg)
1011 {
1012         ndis_miniport_block     *block;
1013         struct ndis_softc       *sc;
1014         bus_dmamap_t            map;
1015
1016         if (adapter == NULL)
1017                 return;
1018
1019         block = (ndis_miniport_block *)adapter;
1020         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1021
1022         if (mapreg > sc->ndis_mmapcnt)
1023                 return;
1024
1025         map = sc->ndis_mmaps[mapreg];
1026
1027         bus_dmamap_sync(sc->ndis_mtag, map,
1028             BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1029
1030         bus_dmamap_unload(sc->ndis_mtag, map);
1031 }
1032
1033 /*
1034  * This is an older (?) timer init routine which doesn't
1035  * accept a miniport context handle. Serialized miniports should
1036  * never call this function.
1037  */
1038
1039 static void
1040 NdisInitializeTimer(ndis_timer *timer, ndis_timer_function func, void *ctx)
1041 {
1042         KeInitializeTimer(&timer->nt_ktimer);
1043         KeInitializeDpc(&timer->nt_kdpc, func, ctx);
1044         KeSetImportanceDpc(&timer->nt_kdpc, KDPC_IMPORTANCE_LOW);
1045 }
1046
1047 static void
1048 ndis_timercall(kdpc *dpc, ndis_miniport_timer *timer, void *sysarg1,
1049     void *sysarg2)
1050 {
1051         /*
1052          * Since we're called as a DPC, we should be running
1053          * at DISPATCH_LEVEL here. This means to acquire the
1054          * spinlock, we can use KeAcquireSpinLockAtDpcLevel()
1055          * rather than KeAcquireSpinLock().
1056          */
1057         if (NDIS_SERIALIZED(timer->nmt_block))
1058                 KeAcquireSpinLockAtDpcLevel(&timer->nmt_block->nmb_lock);
1059
1060         MSCALL4(timer->nmt_timerfunc, dpc, timer->nmt_timerctx,
1061             sysarg1, sysarg2);
1062
1063         if (NDIS_SERIALIZED(timer->nmt_block))
1064                 KeReleaseSpinLockFromDpcLevel(&timer->nmt_block->nmb_lock);
1065 }
1066
1067 /*
1068  * For a long time I wondered why there were two NDIS timer initialization
1069  * routines, and why this one needed an NDIS_MINIPORT_TIMER and the
1070  * MiniportAdapterHandle. The NDIS_MINIPORT_TIMER has its own callout
1071  * function and context pointers separate from those in the DPC, which
1072  * allows for another level of indirection: when the timer fires, we
1073  * can have our own timer function invoked, and from there we can call
1074  * the driver's function. But why go to all that trouble? Then it hit
1075  * me: for serialized miniports, the timer callouts are not re-entrant.
1076  * By trapping the callouts and having access to the MiniportAdapterHandle,
1077  * we can protect the driver callouts by acquiring the NDIS serialization
1078  * lock. This is essential for allowing serialized miniports to work
1079  * correctly on SMP systems. On UP hosts, setting IRQL to DISPATCH_LEVEL
1080  * is enough to prevent other threads from pre-empting you, but with
1081  * SMP, you must acquire a lock as well, otherwise the other CPU is
1082  * free to clobber you.
1083  */
1084 static void
1085 NdisMInitializeTimer(ndis_miniport_timer *timer, ndis_handle handle,
1086     ndis_timer_function func, void *ctx)
1087 {
1088         ndis_miniport_block     *block;
1089         struct ndis_softc       *sc;
1090
1091         block = (ndis_miniport_block *)handle;
1092         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
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         return (NdisAllocatePacketPool(status, pool,
1634             descnum + oflowdescnum, 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 };