]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/compat/ndis/subr_ndis.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / compat / 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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 /*
37  * This file implements a translation layer between the BSD networking
38  * infrasturcture and Windows(R) NDIS network driver modules. A Windows
39  * NDIS driver calls into several functions in the NDIS.SYS Windows
40  * kernel module and exports a table of functions designed to be called
41  * by the NDIS subsystem. Using the PE loader, we can patch our own
42  * versions of the NDIS routines into a given Windows driver module and
43  * convince the driver that it is in fact running on Windows.
44  *
45  * We provide a table of all our implemented NDIS routines which is patched
46  * into the driver object code. All our exported routines must use the
47  * _stdcall calling convention, since that's what the Windows object code
48  * expects.
49  */
50
51
52 #include <sys/ctype.h>
53 #include <sys/param.h>
54 #include <sys/types.h>
55 #include <sys/errno.h>
56
57 #include <sys/callout.h>
58 #include <sys/kernel.h>
59 #include <sys/systm.h>
60 #include <sys/malloc.h>
61 #include <sys/lock.h>
62 #include <sys/mutex.h>
63 #include <sys/socket.h>
64 #include <sys/sysctl.h>
65 #include <sys/timespec.h>
66 #include <sys/smp.h>
67 #include <sys/queue.h>
68 #include <sys/proc.h>
69 #include <sys/filedesc.h>
70 #include <sys/namei.h>
71 #include <sys/fcntl.h>
72 #include <sys/vnode.h>
73 #include <sys/kthread.h>
74 #include <sys/linker.h>
75 #include <sys/mount.h>
76 #include <sys/sysproto.h>
77
78 #include <net/if.h>
79 #include <net/if_arp.h>
80 #include <net/ethernet.h>
81 #include <net/if_dl.h>
82 #include <net/if_media.h>
83
84 #include <machine/atomic.h>
85 #include <machine/bus.h>
86 #include <machine/resource.h>
87
88 #include <sys/bus.h>
89 #include <sys/rman.h>
90
91 #include <machine/stdarg.h>
92
93 #include <net80211/ieee80211_var.h>
94 #include <net80211/ieee80211_ioctl.h>
95
96 #include <dev/pci/pcireg.h>
97 #include <dev/pci/pcivar.h>
98 #include <dev/usb/usb.h>
99 #include <dev/usb/usbdi.h>
100
101 #include <compat/ndis/pe_var.h>
102 #include <compat/ndis/cfg_var.h>
103 #include <compat/ndis/resource_var.h>
104 #include <compat/ndis/ntoskrnl_var.h>
105 #include <compat/ndis/hal_var.h>
106 #include <compat/ndis/ndis_var.h>
107 #include <dev/if_ndis/if_ndisvar.h>
108
109 #include <vm/vm.h>
110 #include <vm/vm_param.h>
111 #include <vm/pmap.h>
112 #include <vm/uma.h>
113 #include <vm/vm_kern.h>
114 #include <vm/vm_map.h>
115
116 static char ndis_filepath[MAXPATHLEN];
117
118 SYSCTL_STRING(_hw, OID_AUTO, ndis_filepath, CTLFLAG_RW, ndis_filepath,
119     MAXPATHLEN, "Path used by NdisOpenFile() to search for files");
120
121 static void NdisInitializeWrapper(ndis_handle *,
122         driver_object *, void *, void *);
123 static ndis_status NdisMRegisterMiniport(ndis_handle,
124         ndis_miniport_characteristics *, int);
125 static ndis_status NdisAllocateMemoryWithTag(void **,
126         uint32_t, uint32_t);
127 static ndis_status NdisAllocateMemory(void **,
128         uint32_t, uint32_t, ndis_physaddr);
129 static void NdisFreeMemory(void *, uint32_t, uint32_t);
130 static ndis_status NdisMSetAttributesEx(ndis_handle, ndis_handle,
131         uint32_t, uint32_t, ndis_interface_type);
132 static void NdisOpenConfiguration(ndis_status *,
133         ndis_handle *, ndis_handle);
134 static void NdisOpenConfigurationKeyByIndex(ndis_status *,
135         ndis_handle, uint32_t, unicode_string *, ndis_handle *);
136 static void NdisOpenConfigurationKeyByName(ndis_status *,
137         ndis_handle, unicode_string *, ndis_handle *);
138 static ndis_status ndis_encode_parm(ndis_miniport_block *,
139         struct sysctl_oid *, ndis_parm_type, ndis_config_parm **);
140 static ndis_status ndis_decode_parm(ndis_miniport_block *,
141         ndis_config_parm *, char *);
142 static void NdisReadConfiguration(ndis_status *, ndis_config_parm **,
143         ndis_handle, unicode_string *, ndis_parm_type);
144 static void NdisWriteConfiguration(ndis_status *, ndis_handle,
145         unicode_string *, ndis_config_parm *);
146 static void NdisCloseConfiguration(ndis_handle);
147 static void NdisAllocateSpinLock(ndis_spin_lock *);
148 static void NdisFreeSpinLock(ndis_spin_lock *);
149 static void NdisAcquireSpinLock(ndis_spin_lock *);
150 static void NdisReleaseSpinLock(ndis_spin_lock *);
151 static void NdisDprAcquireSpinLock(ndis_spin_lock *);
152 static void NdisDprReleaseSpinLock(ndis_spin_lock *);
153 static void NdisInitializeReadWriteLock(ndis_rw_lock *);
154 static void NdisAcquireReadWriteLock(ndis_rw_lock *,
155         uint8_t, ndis_lock_state *);
156 static void NdisReleaseReadWriteLock(ndis_rw_lock *, ndis_lock_state *);
157 static uint32_t NdisReadPciSlotInformation(ndis_handle, uint32_t,
158         uint32_t, void *, uint32_t);
159 static uint32_t NdisWritePciSlotInformation(ndis_handle, uint32_t,
160         uint32_t, void *, uint32_t);
161 static void NdisWriteErrorLogEntry(ndis_handle, ndis_error_code, uint32_t, ...);
162 static void ndis_map_cb(void *, bus_dma_segment_t *, int, int);
163 static void NdisMStartBufferPhysicalMapping(ndis_handle,
164         ndis_buffer *, uint32_t, uint8_t, ndis_paddr_unit *, uint32_t *);
165 static void NdisMCompleteBufferPhysicalMapping(ndis_handle,
166         ndis_buffer *, uint32_t);
167 static void NdisMInitializeTimer(ndis_miniport_timer *, ndis_handle,
168         ndis_timer_function, void *);
169 static void NdisInitializeTimer(ndis_timer *,
170         ndis_timer_function, void *);
171 static void NdisSetTimer(ndis_timer *, uint32_t);
172 static void NdisMSetPeriodicTimer(ndis_miniport_timer *, uint32_t);
173 static void NdisMCancelTimer(ndis_timer *, uint8_t *);
174 static void ndis_timercall(kdpc *, ndis_miniport_timer *,
175         void *, void *);
176 static void NdisMQueryAdapterResources(ndis_status *, ndis_handle,
177         ndis_resource_list *, uint32_t *);
178 static ndis_status NdisMRegisterIoPortRange(void **,
179         ndis_handle, uint32_t, uint32_t);
180 static void NdisMDeregisterIoPortRange(ndis_handle,
181         uint32_t, uint32_t, void *);
182 static void NdisReadNetworkAddress(ndis_status *, void **,
183         uint32_t *, ndis_handle);
184 static ndis_status NdisQueryMapRegisterCount(uint32_t, uint32_t *);
185 static ndis_status NdisMAllocateMapRegisters(ndis_handle,
186         uint32_t, uint8_t, uint32_t, uint32_t);
187 static void NdisMFreeMapRegisters(ndis_handle);
188 static void ndis_mapshared_cb(void *, bus_dma_segment_t *, int, int);
189 static void NdisMAllocateSharedMemory(ndis_handle, uint32_t,
190         uint8_t, void **, ndis_physaddr *);
191 static void ndis_asyncmem_complete(device_object *, void *);
192 static ndis_status NdisMAllocateSharedMemoryAsync(ndis_handle,
193         uint32_t, uint8_t, void *);
194 static void NdisMFreeSharedMemory(ndis_handle, uint32_t,
195         uint8_t, void *, ndis_physaddr);
196 static ndis_status NdisMMapIoSpace(void **, ndis_handle,
197         ndis_physaddr, uint32_t);
198 static void NdisMUnmapIoSpace(ndis_handle, void *, uint32_t);
199 static uint32_t NdisGetCacheFillSize(void);
200 static uint32_t NdisMGetDmaAlignment(ndis_handle);
201 static ndis_status NdisMInitializeScatterGatherDma(ndis_handle,
202         uint8_t, uint32_t);
203 static void NdisUnchainBufferAtFront(ndis_packet *, ndis_buffer **);
204 static void NdisUnchainBufferAtBack(ndis_packet *, ndis_buffer **);
205 static void NdisAllocateBufferPool(ndis_status *,
206         ndis_handle *, uint32_t);
207 static void NdisFreeBufferPool(ndis_handle);
208 static void NdisAllocateBuffer(ndis_status *, ndis_buffer **,
209         ndis_handle, void *, uint32_t);
210 static void NdisFreeBuffer(ndis_buffer *);
211 static uint32_t NdisBufferLength(ndis_buffer *);
212 static void NdisQueryBuffer(ndis_buffer *, void **, uint32_t *);
213 static void NdisQueryBufferSafe(ndis_buffer *, void **,
214         uint32_t *, uint32_t);
215 static void *NdisBufferVirtualAddress(ndis_buffer *);
216 static void *NdisBufferVirtualAddressSafe(ndis_buffer *, uint32_t);
217 static void NdisAdjustBufferLength(ndis_buffer *, int);
218 static uint32_t NdisInterlockedIncrement(uint32_t *);
219 static uint32_t NdisInterlockedDecrement(uint32_t *);
220 static void NdisInitializeEvent(ndis_event *);
221 static void NdisSetEvent(ndis_event *);
222 static void NdisResetEvent(ndis_event *);
223 static uint8_t NdisWaitEvent(ndis_event *, uint32_t);
224 static ndis_status NdisUnicodeStringToAnsiString(ansi_string *,
225         unicode_string *);
226 static ndis_status
227         NdisAnsiStringToUnicodeString(unicode_string *, ansi_string *);
228 static ndis_status NdisMPciAssignResources(ndis_handle,
229         uint32_t, ndis_resource_list **);
230 static ndis_status NdisMRegisterInterrupt(ndis_miniport_interrupt *,
231         ndis_handle, uint32_t, uint32_t, uint8_t,
232         uint8_t, ndis_interrupt_mode);
233 static void NdisMDeregisterInterrupt(ndis_miniport_interrupt *);
234 static void NdisMRegisterAdapterShutdownHandler(ndis_handle, void *,
235         ndis_shutdown_handler);
236 static void NdisMDeregisterAdapterShutdownHandler(ndis_handle);
237 static uint32_t NDIS_BUFFER_TO_SPAN_PAGES(ndis_buffer *);
238 static void NdisGetBufferPhysicalArraySize(ndis_buffer *,
239         uint32_t *);
240 static void NdisQueryBufferOffset(ndis_buffer *,
241         uint32_t *, uint32_t *);
242 static uint32_t NdisReadPcmciaAttributeMemory(ndis_handle,
243         uint32_t, void *, uint32_t);
244 static uint32_t NdisWritePcmciaAttributeMemory(ndis_handle,
245         uint32_t, void *, uint32_t);
246 static list_entry *NdisInterlockedInsertHeadList(list_entry *,
247         list_entry *, ndis_spin_lock *);
248 static list_entry *NdisInterlockedRemoveHeadList(list_entry *,
249         ndis_spin_lock *);
250 static list_entry *NdisInterlockedInsertTailList(list_entry *,
251         list_entry *, ndis_spin_lock *);
252 static uint8_t
253         NdisMSynchronizeWithInterrupt(ndis_miniport_interrupt *,
254         void *, void *);
255 static void NdisGetCurrentSystemTime(uint64_t *);
256 static void NdisGetSystemUpTime(uint32_t *);
257 static void NdisInitializeString(unicode_string *, char *);
258 static void NdisInitAnsiString(ansi_string *, char *);
259 static void NdisInitUnicodeString(unicode_string *, uint16_t *);
260 static void NdisFreeString(unicode_string *);
261 static ndis_status NdisMRemoveMiniport(ndis_handle *);
262 static void NdisTerminateWrapper(ndis_handle, void *);
263 static void NdisMGetDeviceProperty(ndis_handle, device_object **,
264         device_object **, device_object **, cm_resource_list *,
265         cm_resource_list *);
266 static void NdisGetFirstBufferFromPacket(ndis_packet *,
267         ndis_buffer **, void **, uint32_t *, uint32_t *);
268 static void NdisGetFirstBufferFromPacketSafe(ndis_packet *,
269         ndis_buffer **, void **, uint32_t *, uint32_t *, uint32_t);
270 static int ndis_find_sym(linker_file_t, char *, char *, caddr_t *);
271 static void NdisOpenFile(ndis_status *, ndis_handle *, uint32_t *,
272         unicode_string *, ndis_physaddr);
273 static void NdisMapFile(ndis_status *, void **, ndis_handle);
274 static void NdisUnmapFile(ndis_handle);
275 static void NdisCloseFile(ndis_handle);
276 static uint8_t NdisSystemProcessorCount(void);
277 static void NdisMIndicateStatusComplete(ndis_handle);
278 static void NdisMIndicateStatus(ndis_handle, ndis_status,
279     void *, uint32_t);
280 static uint8_t ndis_intr(kinterrupt *, void *);
281 static void ndis_intrhand(kdpc *, ndis_miniport_interrupt *, void *, void *);
282 static funcptr ndis_findwrap(funcptr);
283 static void NdisCopyFromPacketToPacket(ndis_packet *,
284         uint32_t, uint32_t, ndis_packet *, uint32_t, uint32_t *);
285 static void NdisCopyFromPacketToPacketSafe(ndis_packet *,
286         uint32_t, uint32_t, ndis_packet *, uint32_t, uint32_t *, uint32_t);
287 static void NdisIMCopySendPerPacketInfo(ndis_packet *, ndis_packet *);
288 static ndis_status NdisMRegisterDevice(ndis_handle,
289         unicode_string *, unicode_string *, driver_dispatch **,
290         void **, ndis_handle *);
291 static ndis_status NdisMDeregisterDevice(ndis_handle);
292 static ndis_status
293         NdisMQueryAdapterInstanceName(unicode_string *, ndis_handle);
294 static void NdisMRegisterUnloadHandler(ndis_handle, void *);
295 static void dummy(void);
296
297 /*
298  * Some really old drivers do not properly check the return value
299  * from NdisAllocatePacket() and NdisAllocateBuffer() and will
300  * sometimes allocate few more buffers/packets that they originally
301  * requested when they created the pool. To prevent this from being
302  * a problem, we allocate a few extra buffers/packets beyond what
303  * the driver asks for. This #define controls how many.
304  */
305 #define NDIS_POOL_EXTRA         16
306
307 int
308 ndis_libinit()
309 {
310         image_patch_table       *patch;
311
312         strcpy(ndis_filepath, "/compat/ndis");
313
314         patch = ndis_functbl;
315         while (patch->ipt_func != NULL) {
316                 windrv_wrap((funcptr)patch->ipt_func,
317                     (funcptr *)&patch->ipt_wrap,
318                     patch->ipt_argcnt, patch->ipt_ftype);
319                 patch++;
320         }
321
322         return(0);
323 }
324
325 int
326 ndis_libfini()
327 {
328         image_patch_table       *patch;
329
330         patch = ndis_functbl;
331         while (patch->ipt_func != NULL) {
332                 windrv_unwrap(patch->ipt_wrap);
333                 patch++;
334         }
335
336         return(0);
337 }
338
339 static funcptr
340 ndis_findwrap(func)
341         funcptr                 func;
342 {
343         image_patch_table       *patch;
344
345         patch = ndis_functbl;
346         while (patch->ipt_func != NULL) {
347                 if ((funcptr)patch->ipt_func == func)
348                         return((funcptr)patch->ipt_wrap);
349                 patch++;
350         }
351
352         return(NULL);
353 }
354
355 /*
356  * This routine does the messy Windows Driver Model device attachment
357  * stuff on behalf of NDIS drivers. We register our own AddDevice
358  * routine here
359  */
360 static void
361 NdisInitializeWrapper(wrapper, drv, path, unused)
362         ndis_handle             *wrapper;
363         driver_object           *drv;
364         void                    *path;
365         void                    *unused;
366 {
367         /*
368          * As of yet, I haven't come up with a compelling
369          * reason to define a private NDIS wrapper structure,
370          * so we use a pointer to the driver object as the
371          * wrapper handle. The driver object has the miniport
372          * characteristics struct for this driver hung off it
373          * via IoAllocateDriverObjectExtension(), and that's
374          * really all the private data we need.
375          */
376
377         *wrapper = drv;
378
379         /*
380          * If this was really Windows, we'd be registering dispatch
381          * routines for the NDIS miniport module here, but we're
382          * not Windows so all we really need to do is set up an
383          * AddDevice function that'll be invoked when a new device
384          * instance appears.
385          */
386
387         drv->dro_driverext->dre_adddevicefunc = NdisAddDevice;
388
389         return;
390 }
391
392 static void
393 NdisTerminateWrapper(handle, syspec)
394         ndis_handle             handle;
395         void                    *syspec;
396 {
397         /* Nothing to see here, move along. */
398         return;
399 }
400
401 static ndis_status
402 NdisMRegisterMiniport(handle, characteristics, len)
403         ndis_handle             handle;
404         ndis_miniport_characteristics *characteristics;
405         int                     len;
406 {
407         ndis_miniport_characteristics   *ch = NULL;
408         driver_object           *drv;
409
410         drv = (driver_object *)handle;
411
412         /*
413          * We need to save the NDIS miniport characteristics
414          * somewhere. This data is per-driver, not per-device
415          * (all devices handled by the same driver have the
416          * same characteristics) so we hook it onto the driver
417          * object using IoAllocateDriverObjectExtension().
418          * The extra extension info is automagically deleted when
419          * the driver is unloaded (see windrv_unload()).
420          */
421
422         if (IoAllocateDriverObjectExtension(drv, (void *)1,
423             sizeof(ndis_miniport_characteristics), (void **)&ch) !=
424             STATUS_SUCCESS) {
425                 return(NDIS_STATUS_RESOURCES);
426         }
427
428         bzero((char *)ch, sizeof(ndis_miniport_characteristics));
429
430         bcopy((char *)characteristics, (char *)ch, len);
431
432         if (ch->nmc_version_major < 5 || ch->nmc_version_minor < 1) {
433                 ch->nmc_shutdown_handler = NULL;
434                 ch->nmc_canceltxpkts_handler = NULL;
435                 ch->nmc_pnpevent_handler = NULL;
436         }
437
438         return(NDIS_STATUS_SUCCESS);
439 }
440
441 static ndis_status
442 NdisAllocateMemoryWithTag(vaddr, len, tag)
443         void                    **vaddr;
444         uint32_t                len;
445         uint32_t                tag;
446 {
447         void                    *mem;
448
449         mem = ExAllocatePoolWithTag(NonPagedPool, len, tag);
450         if (mem == NULL) {
451                 return(NDIS_STATUS_RESOURCES);
452         }
453         *vaddr = mem;
454
455         return(NDIS_STATUS_SUCCESS);
456 }
457
458 static ndis_status
459 NdisAllocateMemory(vaddr, len, flags, highaddr)
460         void                    **vaddr;
461         uint32_t                len;
462         uint32_t                flags;
463         ndis_physaddr           highaddr;
464 {
465         void                    *mem;
466
467         mem = ExAllocatePoolWithTag(NonPagedPool, len, 0);
468         if (mem == NULL)
469                 return(NDIS_STATUS_RESOURCES);
470         *vaddr = mem;
471
472         return(NDIS_STATUS_SUCCESS);
473 }
474
475 static void
476 NdisFreeMemory(vaddr, len, flags)
477         void                    *vaddr;
478         uint32_t                len;
479         uint32_t                flags;
480 {
481         if (len == 0)
482                 return;
483
484         ExFreePool(vaddr);
485
486         return;
487 }
488
489 static ndis_status
490 NdisMSetAttributesEx(adapter_handle, adapter_ctx, hangsecs,
491                         flags, iftype)
492         ndis_handle                     adapter_handle;
493         ndis_handle                     adapter_ctx;
494         uint32_t                        hangsecs;
495         uint32_t                        flags;
496         ndis_interface_type             iftype;
497 {
498         ndis_miniport_block             *block;
499
500         /*
501          * Save the adapter context, we need it for calling
502          * the driver's internal functions.
503          */
504         block = (ndis_miniport_block *)adapter_handle;
505         block->nmb_miniportadapterctx = adapter_ctx;
506         block->nmb_checkforhangsecs = hangsecs;
507         block->nmb_flags = flags;
508
509         return(NDIS_STATUS_SUCCESS);
510 }
511
512 static void
513 NdisOpenConfiguration(status, cfg, wrapctx)
514         ndis_status             *status;
515         ndis_handle             *cfg;
516         ndis_handle             wrapctx;
517 {
518         *cfg = wrapctx;
519         *status = NDIS_STATUS_SUCCESS;
520
521         return;
522 }
523
524 static void
525 NdisOpenConfigurationKeyByName(status, cfg, subkey, subhandle)
526         ndis_status             *status;
527         ndis_handle             cfg;
528         unicode_string          *subkey;
529         ndis_handle             *subhandle;
530 {
531         *subhandle = cfg;
532         *status = NDIS_STATUS_SUCCESS;
533
534         return;
535 }
536
537 static void
538 NdisOpenConfigurationKeyByIndex(status, cfg, idx, subkey, subhandle)
539         ndis_status             *status;
540         ndis_handle             cfg;
541         uint32_t                idx;
542         unicode_string          *subkey;
543         ndis_handle             *subhandle;
544 {
545         *status = NDIS_STATUS_FAILURE;
546
547         return;
548 }
549
550 static ndis_status
551 ndis_encode_parm(block, oid, type, parm)
552         ndis_miniport_block     *block;
553         struct sysctl_oid       *oid;
554         ndis_parm_type          type;
555         ndis_config_parm        **parm;
556 {
557         ndis_config_parm        *p;
558         ndis_parmlist_entry     *np;
559         unicode_string          *us;
560         ansi_string             as;
561         int                     base = 0;
562         uint32_t                val;
563         char                    tmp[32];
564
565         np = ExAllocatePoolWithTag(NonPagedPool,
566             sizeof(ndis_parmlist_entry), 0);
567         if (np == NULL)
568                 return(NDIS_STATUS_RESOURCES);
569         InsertHeadList((&block->nmb_parmlist), (&np->np_list));
570         *parm = p = &np->np_parm;
571
572         switch(type) {
573         case ndis_parm_string:
574                 /* See if this might be a number. */
575                 val = strtoul((char *)oid->oid_arg1, NULL, 10);
576                 us = &p->ncp_parmdata.ncp_stringdata;
577                 p->ncp_type = ndis_parm_string;
578                 if (val) {
579                         snprintf(tmp, 32, "%x", val);
580                         RtlInitAnsiString(&as, tmp);
581                 } else {
582                         RtlInitAnsiString(&as, (char *)oid->oid_arg1);
583                 }
584
585                 if (RtlAnsiStringToUnicodeString(us, &as, TRUE)) {
586                         ExFreePool(np);
587                         return(NDIS_STATUS_RESOURCES);
588                 }
589                 break;
590         case ndis_parm_int:
591                 if (strncmp((char *)oid->oid_arg1, "0x", 2) == 0)
592                         base = 16;
593                 else
594                         base = 10;
595                 p->ncp_type = ndis_parm_int;
596                 p->ncp_parmdata.ncp_intdata =
597                     strtol((char *)oid->oid_arg1, NULL, base);
598                 break;
599         case ndis_parm_hexint:
600 #ifdef notdef
601                 if (strncmp((char *)oid->oid_arg1, "0x", 2) == 0)
602                         base = 16;
603                 else
604                         base = 10;
605 #endif
606                 base = 16;
607                 p->ncp_type = ndis_parm_hexint;
608                 p->ncp_parmdata.ncp_intdata =
609                     strtoul((char *)oid->oid_arg1, NULL, base);
610                 break;
611         default:
612                 return(NDIS_STATUS_FAILURE);
613                 break;
614         }
615
616         return(NDIS_STATUS_SUCCESS);
617 }
618
619 static void
620 NdisReadConfiguration(status, parm, cfg, key, type)
621         ndis_status             *status;
622         ndis_config_parm        **parm;
623         ndis_handle             cfg;
624         unicode_string          *key;
625         ndis_parm_type          type;
626 {
627         char                    *keystr = NULL;
628         ndis_miniport_block     *block;
629         struct ndis_softc       *sc;
630         struct sysctl_oid       *oidp;
631         struct sysctl_ctx_entry *e;
632         ansi_string             as;
633
634         block = (ndis_miniport_block *)cfg;
635         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
636
637         if (key->us_len == 0 || key->us_buf == NULL) {
638                 *status = NDIS_STATUS_FAILURE;
639                 return;
640         }
641
642         if (RtlUnicodeStringToAnsiString(&as, key, TRUE)) {
643                 *status = NDIS_STATUS_RESOURCES;
644                 return;
645         }
646
647         keystr = as.as_buf;
648
649         /*
650          * See if registry key is already in a list of known keys
651          * included with the driver.
652          */
653 #if __FreeBSD_version < 502113
654         TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
655 #else
656         TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
657 #endif
658                 oidp = e->entry;
659                 if (strcasecmp(oidp->oid_name, keystr) == 0) {
660                         if (strcmp((char *)oidp->oid_arg1, "UNSET") == 0) {
661                                 RtlFreeAnsiString(&as);
662                                 *status = NDIS_STATUS_FAILURE;
663                                 return;
664                         }
665
666                         *status = ndis_encode_parm(block, oidp, type, parm);
667                         RtlFreeAnsiString(&as);
668                         return;
669                 }
670         }
671
672         /*
673          * If the key didn't match, add it to the list of dynamically
674          * created ones. Sometimes, drivers refer to registry keys
675          * that aren't documented in their .INF files. These keys
676          * are supposed to be created by some sort of utility or
677          * control panel snap-in that comes with the driver software.
678          * Sometimes it's useful to be able to manipulate these.
679          * If the driver requests the key in the form of a string,
680          * make its default value an empty string, otherwise default
681          * it to "0".
682          */
683
684         if (type == ndis_parm_int || type == ndis_parm_hexint)
685                 ndis_add_sysctl(sc, keystr, "(dynamic integer key)",
686                     "UNSET", CTLFLAG_RW);
687         else
688                 ndis_add_sysctl(sc, keystr, "(dynamic string key)",
689                     "UNSET", CTLFLAG_RW);
690
691         RtlFreeAnsiString(&as);
692         *status = NDIS_STATUS_FAILURE;
693
694         return;
695 }
696
697 static ndis_status
698 ndis_decode_parm(block, parm, val)
699         ndis_miniport_block     *block;
700         ndis_config_parm        *parm;
701         char                    *val;
702 {
703         unicode_string          *ustr;
704         ansi_string             as;
705
706         switch(parm->ncp_type) {
707         case ndis_parm_string:
708                 ustr = &parm->ncp_parmdata.ncp_stringdata;
709                 if (RtlUnicodeStringToAnsiString(&as, ustr, TRUE))
710                         return(NDIS_STATUS_RESOURCES);
711                 bcopy(as.as_buf, val, as.as_len);
712                 RtlFreeAnsiString(&as);
713                 break;
714         case ndis_parm_int:
715                 sprintf(val, "%d", parm->ncp_parmdata.ncp_intdata);
716                 break;
717         case ndis_parm_hexint:
718                 sprintf(val, "%xu", parm->ncp_parmdata.ncp_intdata);
719                 break;
720         default:
721                 return(NDIS_STATUS_FAILURE);
722                 break;
723         }
724         return(NDIS_STATUS_SUCCESS);
725 }
726
727 static void
728 NdisWriteConfiguration(status, cfg, key, parm)
729         ndis_status             *status;
730         ndis_handle             cfg;
731         unicode_string          *key;
732         ndis_config_parm        *parm;
733 {
734         ansi_string             as;
735         char                    *keystr = NULL;
736         ndis_miniport_block     *block;
737         struct ndis_softc       *sc;
738         struct sysctl_oid       *oidp;
739         struct sysctl_ctx_entry *e;
740         char                    val[256];
741
742         block = (ndis_miniport_block *)cfg;
743         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
744
745         if (RtlUnicodeStringToAnsiString(&as, key, TRUE)) {
746                 *status = NDIS_STATUS_RESOURCES;
747                 return;
748         }
749
750         keystr = as.as_buf;
751
752         /* Decode the parameter into a string. */
753         bzero(val, sizeof(val));
754         *status = ndis_decode_parm(block, parm, val);
755         if (*status != NDIS_STATUS_SUCCESS) {
756                 RtlFreeAnsiString(&as);
757                 return;
758         }
759
760         /* See if the key already exists. */
761
762 #if __FreeBSD_version < 502113
763         TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
764 #else
765         TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
766 #endif
767                 oidp = e->entry;
768                 if (strcasecmp(oidp->oid_name, keystr) == 0) {
769                         /* Found it, set the value. */
770                         strcpy((char *)oidp->oid_arg1, val);
771                         RtlFreeAnsiString(&as);
772                         return;
773                 }
774         }
775
776         /* Not found, add a new key with the specified value. */
777         ndis_add_sysctl(sc, keystr, "(dynamically set key)",
778                     val, CTLFLAG_RW);
779
780         RtlFreeAnsiString(&as);
781         *status = NDIS_STATUS_SUCCESS;
782         return;
783 }
784
785 static void
786 NdisCloseConfiguration(cfg)
787         ndis_handle             cfg;
788 {
789         list_entry              *e;
790         ndis_parmlist_entry     *pe;
791         ndis_miniport_block     *block;
792         ndis_config_parm        *p;
793
794         block = (ndis_miniport_block *)cfg;
795
796         while (!IsListEmpty(&block->nmb_parmlist)) {
797                 e = RemoveHeadList(&block->nmb_parmlist);
798                 pe = CONTAINING_RECORD(e, ndis_parmlist_entry, np_list);
799                 p = &pe->np_parm;
800                 if (p->ncp_type == ndis_parm_string)
801                         RtlFreeUnicodeString(&p->ncp_parmdata.ncp_stringdata);
802                 ExFreePool(e);
803         }
804
805         return;
806 }
807
808 /*
809  * Initialize a Windows spinlock.
810  */
811 static void
812 NdisAllocateSpinLock(lock)
813         ndis_spin_lock          *lock;
814 {
815         KeInitializeSpinLock(&lock->nsl_spinlock);
816         lock->nsl_kirql = 0;
817
818         return;
819 }
820
821 /*
822  * Destroy a Windows spinlock. This is a no-op for now. There are two reasons
823  * for this. One is that it's sort of superfluous: we don't have to do anything
824  * special to deallocate the spinlock. The other is that there are some buggy
825  * drivers which call NdisFreeSpinLock() _after_ calling NdisFreeMemory() on
826  * the block of memory in which the spinlock resides. (Yes, ADMtek, I'm
827  * talking to you.)
828  */
829 static void
830 NdisFreeSpinLock(lock)
831         ndis_spin_lock          *lock;
832 {
833 #ifdef notdef
834         KeInitializeSpinLock(&lock->nsl_spinlock);
835         lock->nsl_kirql = 0;
836 #endif
837         return;
838 }
839
840 /*
841  * Acquire a spinlock from IRQL <= DISPATCH_LEVEL.
842  */
843
844 static void
845 NdisAcquireSpinLock(lock)
846         ndis_spin_lock          *lock;
847 {
848         KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
849         return;
850 }
851
852 /*
853  * Release a spinlock from IRQL == DISPATCH_LEVEL.
854  */
855
856 static void
857 NdisReleaseSpinLock(lock)
858         ndis_spin_lock          *lock;
859 {
860         KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
861         return;
862 }
863
864 /*
865  * Acquire a spinlock when already running at IRQL == DISPATCH_LEVEL.
866  */
867 static void
868 NdisDprAcquireSpinLock(lock)
869         ndis_spin_lock          *lock;
870 {
871         KeAcquireSpinLockAtDpcLevel(&lock->nsl_spinlock);
872         return;
873 }
874
875 /*
876  * Release a spinlock without leaving IRQL == DISPATCH_LEVEL.
877  */
878 static void
879 NdisDprReleaseSpinLock(lock)
880         ndis_spin_lock          *lock;
881 {
882         KeReleaseSpinLockFromDpcLevel(&lock->nsl_spinlock);
883         return;
884 }
885
886 static void
887 NdisInitializeReadWriteLock(lock)
888         ndis_rw_lock            *lock;
889 {
890         KeInitializeSpinLock(&lock->nrl_spinlock);
891         bzero((char *)&lock->nrl_rsvd, sizeof(lock->nrl_rsvd));
892         return;
893 }
894
895 static void
896 NdisAcquireReadWriteLock(ndis_rw_lock *lock, uint8_t writeacc,
897     ndis_lock_state *state)
898 {
899         if (writeacc == TRUE) {
900                 KeAcquireSpinLock(&lock->nrl_spinlock, &state->nls_oldirql);
901                 lock->nrl_rsvd[0]++;
902         } else
903                 lock->nrl_rsvd[1]++;
904
905         return;
906 }
907
908 static void
909 NdisReleaseReadWriteLock(lock, state)
910         ndis_rw_lock            *lock;
911         ndis_lock_state         *state;
912 {
913         if (lock->nrl_rsvd[0]) {
914                 lock->nrl_rsvd[0]--;
915                 KeReleaseSpinLock(&lock->nrl_spinlock, state->nls_oldirql);
916         } else
917                 lock->nrl_rsvd[1]--;
918
919         return;
920 }
921
922 static uint32_t
923 NdisReadPciSlotInformation(adapter, slot, offset, buf, len)
924         ndis_handle             adapter;
925         uint32_t                slot;
926         uint32_t                offset;
927         void                    *buf;
928         uint32_t                len;
929 {
930         ndis_miniport_block     *block;
931         int                     i;
932         char                    *dest;
933         device_t                dev;
934
935         block = (ndis_miniport_block *)adapter;
936         dest = buf;
937         if (block == NULL)
938                 return(0);
939
940         dev = block->nmb_physdeviceobj->do_devext;
941
942         /*
943          * I have a test system consisting of a Sun w2100z
944          * dual 2.4Ghz Opteron machine and an Atheros 802.11a/b/g
945          * "Aries" miniPCI NIC. (The NIC is installed in the
946          * machine using a miniPCI to PCI bus adapter card.)
947          * When running in SMP mode, I found that
948          * performing a large number of consecutive calls to
949          * NdisReadPciSlotInformation() would result in a
950          * sudden system reset (or in some cases a freeze).
951          * My suspicion is that the multiple reads are somehow
952          * triggering a fatal PCI bus error that leads to a
953          * machine check. The 1us delay in the loop below
954          * seems to prevent this problem.
955          */
956
957         for (i = 0; i < len; i++) {
958                 DELAY(1);
959                 dest[i] = pci_read_config(dev, i + offset, 1);
960         }
961
962         return(len);
963 }
964
965 static uint32_t
966 NdisWritePciSlotInformation(adapter, slot, offset, buf, len)
967         ndis_handle             adapter;
968         uint32_t                slot;
969         uint32_t                offset;
970         void                    *buf;
971         uint32_t                len;
972 {
973         ndis_miniport_block     *block;
974         int                     i;
975         char                    *dest;
976         device_t                dev;
977
978         block = (ndis_miniport_block *)adapter;
979         dest = buf;
980
981         if (block == NULL)
982                 return(0);
983
984         dev = block->nmb_physdeviceobj->do_devext;
985         for (i = 0; i < len; i++) {
986                 DELAY(1);
987                 pci_write_config(dev, i + offset, dest[i], 1);
988         }
989
990         return(len);
991 }
992
993 /*
994  * The errorlog routine uses a variable argument list, so we
995  * have to declare it this way.
996  */
997
998 #define ERRMSGLEN 512
999 static void
1000 NdisWriteErrorLogEntry(ndis_handle adapter, ndis_error_code code,
1001         uint32_t numerrors, ...)
1002 {
1003         ndis_miniport_block     *block;
1004         va_list                 ap;
1005         int                     i, error;
1006         char                    *str = NULL;
1007         uint16_t                flags;
1008         device_t                dev;
1009         driver_object           *drv;
1010         struct ndis_softc       *sc;
1011         struct ifnet            *ifp;
1012         unicode_string          us;
1013         ansi_string             as = { 0, 0, NULL };
1014
1015         block = (ndis_miniport_block *)adapter;
1016         dev = block->nmb_physdeviceobj->do_devext;
1017         drv = block->nmb_deviceobj->do_drvobj;
1018         sc = device_get_softc(dev);
1019         ifp = sc->ifp;
1020
1021         if (ifp != NULL && ifp->if_flags & IFF_DEBUG) {
1022                 error = pe_get_message((vm_offset_t)drv->dro_driverstart,
1023                     code, &str, &i, &flags);
1024                 if (error == 0) {
1025                         if (flags & MESSAGE_RESOURCE_UNICODE) {
1026                                 RtlInitUnicodeString(&us, (uint16_t *)str);
1027                                 if (RtlUnicodeStringToAnsiString(&as,
1028                                     &us, TRUE) == STATUS_SUCCESS)
1029                                         str = as.as_buf;
1030                                 else
1031                                         str = NULL;
1032                         }
1033                 }
1034         }
1035
1036         device_printf (dev, "NDIS ERROR: %x (%s)\n", code,
1037             str == NULL ? "unknown error" : str);
1038
1039         if (ifp != NULL && ifp->if_flags & IFF_DEBUG) {
1040                 device_printf (dev, "NDIS NUMERRORS: %x\n", numerrors);
1041                 va_start(ap, numerrors);
1042                 for (i = 0; i < numerrors; i++)
1043                         device_printf (dev, "argptr: %p\n",
1044                             va_arg(ap, void *));
1045                 va_end(ap);
1046         }
1047
1048         if (as.as_len)
1049                 RtlFreeAnsiString(&as);
1050
1051         return;
1052 }
1053
1054 static void
1055 ndis_map_cb(arg, segs, nseg, error)
1056         void                    *arg;
1057         bus_dma_segment_t       *segs;
1058         int                     nseg;
1059         int                     error;
1060 {
1061         struct ndis_map_arg     *ctx;
1062         int                     i;
1063
1064         if (error)
1065                 return;
1066
1067         ctx = arg;
1068
1069         for (i = 0; i < nseg; i++) {
1070                 ctx->nma_fraglist[i].npu_physaddr.np_quad = segs[i].ds_addr;
1071                 ctx->nma_fraglist[i].npu_len = segs[i].ds_len;
1072         }
1073
1074         ctx->nma_cnt = nseg;
1075
1076         return;
1077 }
1078
1079 static void
1080 NdisMStartBufferPhysicalMapping(ndis_handle adapter, ndis_buffer *buf,
1081     uint32_t mapreg, uint8_t writedev, ndis_paddr_unit *addrarray,
1082     uint32_t *arraysize)
1083 {
1084         ndis_miniport_block     *block;
1085         struct ndis_softc       *sc;
1086         struct ndis_map_arg     nma;
1087         bus_dmamap_t            map;
1088         int                     error;
1089
1090         if (adapter == NULL)
1091                 return;
1092
1093         block = (ndis_miniport_block *)adapter;
1094         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1095
1096         if (mapreg > sc->ndis_mmapcnt)
1097                 return;
1098
1099         map = sc->ndis_mmaps[mapreg];
1100         nma.nma_fraglist = addrarray;
1101
1102         error = bus_dmamap_load(sc->ndis_mtag, map,
1103             MmGetMdlVirtualAddress(buf), MmGetMdlByteCount(buf), ndis_map_cb,
1104             (void *)&nma, BUS_DMA_NOWAIT);
1105
1106         if (error)
1107                 return;
1108
1109         bus_dmamap_sync(sc->ndis_mtag, map,
1110             writedev ? BUS_DMASYNC_PREWRITE : BUS_DMASYNC_PREREAD);
1111
1112         *arraysize = nma.nma_cnt;
1113
1114         return;
1115 }
1116
1117 static void
1118 NdisMCompleteBufferPhysicalMapping(adapter, buf, mapreg)
1119         ndis_handle             adapter;
1120         ndis_buffer             *buf;
1121         uint32_t                mapreg;
1122 {
1123         ndis_miniport_block     *block;
1124         struct ndis_softc       *sc;
1125         bus_dmamap_t            map;
1126
1127         if (adapter == NULL)
1128                 return;
1129
1130         block = (ndis_miniport_block *)adapter;
1131         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1132
1133         if (mapreg > sc->ndis_mmapcnt)
1134                 return;
1135
1136         map = sc->ndis_mmaps[mapreg];
1137
1138         bus_dmamap_sync(sc->ndis_mtag, map,
1139             BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1140
1141         bus_dmamap_unload(sc->ndis_mtag, map);
1142
1143         return;
1144 }
1145
1146 /*
1147  * This is an older (?) timer init routine which doesn't
1148  * accept a miniport context handle. Serialized miniports should
1149  * never call this function.
1150  */
1151
1152 static void
1153 NdisInitializeTimer(timer, func, ctx)
1154         ndis_timer              *timer;
1155         ndis_timer_function     func;
1156         void                    *ctx;
1157 {
1158         KeInitializeTimer(&timer->nt_ktimer);
1159         KeInitializeDpc(&timer->nt_kdpc, func, ctx);
1160         KeSetImportanceDpc(&timer->nt_kdpc, KDPC_IMPORTANCE_LOW);
1161
1162         return;
1163 }
1164
1165 static void
1166 ndis_timercall(dpc, timer, sysarg1, sysarg2)
1167         kdpc                    *dpc;
1168         ndis_miniport_timer     *timer;
1169         void                    *sysarg1;
1170         void                    *sysarg2;
1171 {
1172         /*
1173          * Since we're called as a DPC, we should be running
1174          * at DISPATCH_LEVEL here. This means to acquire the
1175          * spinlock, we can use KeAcquireSpinLockAtDpcLevel()
1176          * rather than KeAcquireSpinLock().
1177          */
1178         if (NDIS_SERIALIZED(timer->nmt_block))
1179                 KeAcquireSpinLockAtDpcLevel(&timer->nmt_block->nmb_lock);
1180
1181         MSCALL4(timer->nmt_timerfunc, dpc, timer->nmt_timerctx,
1182             sysarg1, sysarg2);
1183
1184         if (NDIS_SERIALIZED(timer->nmt_block))
1185                 KeReleaseSpinLockFromDpcLevel(&timer->nmt_block->nmb_lock);
1186
1187         return;
1188 }
1189
1190 /*
1191  * For a long time I wondered why there were two NDIS timer initialization
1192  * routines, and why this one needed an NDIS_MINIPORT_TIMER and the
1193  * MiniportAdapterHandle. The NDIS_MINIPORT_TIMER has its own callout
1194  * function and context pointers separate from those in the DPC, which
1195  * allows for another level of indirection: when the timer fires, we
1196  * can have our own timer function invoked, and from there we can call
1197  * the driver's function. But why go to all that trouble? Then it hit
1198  * me: for serialized miniports, the timer callouts are not re-entrant.
1199  * By trapping the callouts and having access to the MiniportAdapterHandle,
1200  * we can protect the driver callouts by acquiring the NDIS serialization
1201  * lock. This is essential for allowing serialized miniports to work
1202  * correctly on SMP systems. On UP hosts, setting IRQL to DISPATCH_LEVEL
1203  * is enough to prevent other threads from pre-empting you, but with
1204  * SMP, you must acquire a lock as well, otherwise the other CPU is
1205  * free to clobber you.
1206  */
1207 static void
1208 NdisMInitializeTimer(timer, handle, func, ctx)
1209         ndis_miniport_timer     *timer;
1210         ndis_handle             handle;
1211         ndis_timer_function     func;
1212         void                    *ctx;
1213 {
1214         ndis_miniport_block     *block;
1215         struct ndis_softc       *sc;
1216
1217         block = (ndis_miniport_block *)handle;
1218         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1219
1220         /* Save the driver's funcptr and context */
1221
1222         timer->nmt_timerfunc = func;
1223         timer->nmt_timerctx = ctx;
1224         timer->nmt_block = handle;
1225
1226         /*
1227          * Set up the timer so it will call our intermediate DPC.
1228          * Be sure to use the wrapped entry point, since
1229          * ntoskrnl_run_dpc() expects to invoke a function with
1230          * Microsoft calling conventions.
1231          */
1232         KeInitializeTimer(&timer->nmt_ktimer);
1233         KeInitializeDpc(&timer->nmt_kdpc,
1234             ndis_findwrap((funcptr)ndis_timercall), timer);
1235         timer->nmt_ktimer.k_dpc = &timer->nmt_kdpc;
1236 }
1237
1238 /*
1239  * In Windows, there's both an NdisMSetTimer() and an NdisSetTimer(),
1240  * but the former is just a macro wrapper around the latter.
1241  */
1242 static void
1243 NdisSetTimer(timer, msecs)
1244         ndis_timer              *timer;
1245         uint32_t                msecs;
1246 {
1247         /*
1248          * KeSetTimer() wants the period in
1249          * hundred nanosecond intervals.
1250          */
1251         KeSetTimer(&timer->nt_ktimer,
1252             ((int64_t)msecs * -10000), &timer->nt_kdpc);
1253
1254         return;
1255 }
1256
1257 static void
1258 NdisMSetPeriodicTimer(timer, msecs)
1259         ndis_miniport_timer     *timer;
1260         uint32_t                msecs;
1261 {
1262         KeSetTimerEx(&timer->nmt_ktimer,
1263             ((int64_t)msecs * -10000), msecs, &timer->nmt_kdpc);
1264
1265         return;
1266 }
1267
1268 /*
1269  * Technically, this is really NdisCancelTimer(), but we also
1270  * (ab)use it for NdisMCancelTimer(), since in our implementation
1271  * we don't need the extra info in the ndis_miniport_timer
1272  * structure just to cancel a timer.
1273  */
1274
1275 static void
1276 NdisMCancelTimer(timer, cancelled)
1277         ndis_timer              *timer;
1278         uint8_t                 *cancelled;
1279 {
1280
1281         *cancelled = KeCancelTimer(&timer->nt_ktimer);
1282         return;
1283 }
1284
1285 static void
1286 NdisMQueryAdapterResources(status, adapter, list, buflen)
1287         ndis_status             *status;
1288         ndis_handle             adapter;
1289         ndis_resource_list      *list;
1290         uint32_t                *buflen;
1291 {
1292         ndis_miniport_block     *block;
1293         struct ndis_softc       *sc;
1294         int                     rsclen;
1295
1296         block = (ndis_miniport_block *)adapter;
1297         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1298
1299         rsclen = sizeof(ndis_resource_list) +
1300             (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1));
1301         if (*buflen < rsclen) {
1302                 *buflen = rsclen;
1303                 *status = NDIS_STATUS_INVALID_LENGTH;
1304                 return;
1305         }
1306
1307         bcopy((char *)block->nmb_rlist, (char *)list, rsclen);
1308         *status = NDIS_STATUS_SUCCESS;
1309
1310         return;
1311 }
1312
1313 static ndis_status
1314 NdisMRegisterIoPortRange(offset, adapter, port, numports)
1315         void                    **offset;
1316         ndis_handle             adapter;
1317         uint32_t                port;
1318         uint32_t                numports;
1319 {
1320         struct ndis_miniport_block      *block;
1321         struct ndis_softc       *sc;
1322
1323         if (adapter == NULL)
1324                 return(NDIS_STATUS_FAILURE);
1325
1326         block = (ndis_miniport_block *)adapter;
1327         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1328
1329         if (sc->ndis_res_io == NULL)
1330                 return(NDIS_STATUS_FAILURE);
1331
1332         /* Don't let the device map more ports than we have. */
1333         if (rman_get_size(sc->ndis_res_io) < numports)
1334                 return(NDIS_STATUS_INVALID_LENGTH);
1335
1336         *offset = (void *)rman_get_start(sc->ndis_res_io);
1337
1338         return(NDIS_STATUS_SUCCESS);
1339 }
1340
1341 static void
1342 NdisMDeregisterIoPortRange(adapter, port, numports, offset)
1343         ndis_handle             adapter;
1344         uint32_t                port;
1345         uint32_t                numports;
1346         void                    *offset;
1347 {
1348         return;
1349 }
1350
1351 static void
1352 NdisReadNetworkAddress(status, addr, addrlen, adapter)
1353         ndis_status             *status;
1354         void                    **addr;
1355         uint32_t                *addrlen;
1356         ndis_handle             adapter;
1357 {
1358         struct ndis_softc       *sc;
1359         ndis_miniport_block     *block;
1360         uint8_t                 empty[] = { 0, 0, 0, 0, 0, 0 };
1361
1362         block = (ndis_miniport_block *)adapter;
1363         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1364         if (sc->ifp == NULL) {
1365                 *status = NDIS_STATUS_FAILURE;
1366                 return;
1367         }
1368
1369 #ifdef IFP2ENADDR
1370         if (bcmp(IFP2ENADDR(sc->ifp), empty, ETHER_ADDR_LEN) == 0)
1371 #elif __FreeBSD_version >= 700000
1372         if (sc->ifp->if_addr == NULL ||
1373             bcmp(IF_LLADDR(sc->ifp), empty, ETHER_ADDR_LEN) == 0)
1374 #else
1375         if (bcmp(sc->arpcom.ac_enaddr, empty, ETHER_ADDR_LEN) == 0)
1376 #endif
1377                 *status = NDIS_STATUS_FAILURE;
1378         else {
1379 #ifdef IFP2ENADDR
1380                 *addr = IFP2ENADDR(sc->ifp);
1381 #elif __FreeBSD_version >= 700000
1382                 *addr = IF_LLADDR(sc->ifp);
1383 #else
1384                 *addr = sc->arpcom.ac_enaddr;
1385 #endif
1386                 *addrlen = ETHER_ADDR_LEN;
1387                 *status = NDIS_STATUS_SUCCESS;
1388         }
1389
1390         return;
1391 }
1392
1393 static ndis_status
1394 NdisQueryMapRegisterCount(bustype, cnt)
1395         uint32_t                bustype;
1396         uint32_t                *cnt;
1397 {
1398         *cnt = 8192;
1399         return(NDIS_STATUS_SUCCESS);
1400 }
1401
1402 static ndis_status
1403 NdisMAllocateMapRegisters(ndis_handle adapter, uint32_t dmachannel,
1404     uint8_t dmasize, uint32_t physmapneeded, uint32_t maxmap)
1405 {
1406         struct ndis_softc       *sc;
1407         ndis_miniport_block     *block;
1408         int                     error, i, nseg = NDIS_MAXSEG;
1409
1410         block = (ndis_miniport_block *)adapter;
1411         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1412
1413         sc->ndis_mmaps = malloc(sizeof(bus_dmamap_t) * physmapneeded,
1414             M_DEVBUF, M_NOWAIT|M_ZERO);
1415
1416         if (sc->ndis_mmaps == NULL)
1417                 return(NDIS_STATUS_RESOURCES);
1418
1419         error = bus_dma_tag_create(sc->ndis_parent_tag, ETHER_ALIGN, 0,
1420             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1421             NULL, maxmap * nseg, nseg, maxmap, BUS_DMA_ALLOCNOW,
1422             NULL, NULL, &sc->ndis_mtag);
1423
1424         if (error) {
1425                 free(sc->ndis_mmaps, M_DEVBUF);
1426                 return(NDIS_STATUS_RESOURCES);
1427         }
1428
1429         for (i = 0; i < physmapneeded; i++)
1430                 bus_dmamap_create(sc->ndis_mtag, 0, &sc->ndis_mmaps[i]);
1431
1432         sc->ndis_mmapcnt = physmapneeded;
1433
1434         return(NDIS_STATUS_SUCCESS);
1435 }
1436
1437 static void
1438 NdisMFreeMapRegisters(adapter)
1439         ndis_handle             adapter;
1440 {
1441         struct ndis_softc       *sc;
1442         ndis_miniport_block     *block;
1443         int                     i;
1444
1445         block = (ndis_miniport_block *)adapter;
1446         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1447
1448         for (i = 0; i < sc->ndis_mmapcnt; i++)
1449                 bus_dmamap_destroy(sc->ndis_mtag, sc->ndis_mmaps[i]);
1450
1451         free(sc->ndis_mmaps, M_DEVBUF);
1452
1453         bus_dma_tag_destroy(sc->ndis_mtag);
1454
1455         return;
1456 }
1457
1458 static void
1459 ndis_mapshared_cb(arg, segs, nseg, error)
1460         void                    *arg;
1461         bus_dma_segment_t       *segs;
1462         int                     nseg;
1463         int                     error;
1464 {
1465         ndis_physaddr           *p;
1466
1467         if (error || nseg > 1)
1468                 return;
1469
1470         p = arg;
1471
1472         p->np_quad = segs[0].ds_addr;
1473
1474         return;
1475 }
1476
1477 /*
1478  * This maps to bus_dmamem_alloc().
1479  */
1480
1481 static void
1482 NdisMAllocateSharedMemory(ndis_handle adapter, uint32_t len, uint8_t cached,
1483     void **vaddr, ndis_physaddr *paddr)
1484 {
1485         ndis_miniport_block     *block;
1486         struct ndis_softc       *sc;
1487         struct ndis_shmem       *sh;
1488         int                     error;
1489
1490         if (adapter == NULL)
1491                 return;
1492
1493         block = (ndis_miniport_block *)adapter;
1494         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1495
1496         sh = malloc(sizeof(struct ndis_shmem), M_DEVBUF, M_NOWAIT|M_ZERO);
1497         if (sh == NULL)
1498                 return;
1499
1500         InitializeListHead(&sh->ndis_list);
1501
1502         /*
1503          * When performing shared memory allocations, create a tag
1504          * with a lowaddr limit that restricts physical memory mappings
1505          * so that they all fall within the first 1GB of memory.
1506          * At least one device/driver combination (Linksys Instant
1507          * Wireless PCI Card V2.7, Broadcom 802.11b) seems to have
1508          * problems with performing DMA operations with physical
1509          * addresses that lie above the 1GB mark. I don't know if this
1510          * is a hardware limitation or if the addresses are being
1511          * truncated within the driver, but this seems to be the only
1512          * way to make these cards work reliably in systems with more
1513          * than 1GB of physical memory.
1514          */
1515
1516         error = bus_dma_tag_create(sc->ndis_parent_tag, 64,
1517             0, NDIS_BUS_SPACE_SHARED_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1518             NULL, len, 1, len, BUS_DMA_ALLOCNOW, NULL, NULL,
1519             &sh->ndis_stag);
1520
1521         if (error) {
1522                 free(sh, M_DEVBUF);
1523                 return;
1524         }
1525
1526         error = bus_dmamem_alloc(sh->ndis_stag, vaddr,
1527             BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sh->ndis_smap);
1528
1529         if (error) {
1530                 bus_dma_tag_destroy(sh->ndis_stag);
1531                 free(sh, M_DEVBUF);
1532                 return;
1533         }
1534
1535         error = bus_dmamap_load(sh->ndis_stag, sh->ndis_smap, *vaddr,
1536             len, ndis_mapshared_cb, (void *)paddr, BUS_DMA_NOWAIT);
1537
1538         if (error) {
1539                 bus_dmamem_free(sh->ndis_stag, *vaddr, sh->ndis_smap);
1540                 bus_dma_tag_destroy(sh->ndis_stag);
1541                 free(sh, M_DEVBUF);
1542                 return;
1543         }
1544
1545         /*
1546          * Save the physical address along with the source address.
1547          * The AirGo MIMO driver will call NdisMFreeSharedMemory()
1548          * with a bogus virtual address sometimes, but with a valid
1549          * physical address. To keep this from causing trouble, we
1550          * use the physical address to as a sanity check in case
1551          * searching based on the virtual address fails.
1552          */
1553
1554         NDIS_LOCK(sc);
1555         sh->ndis_paddr.np_quad = paddr->np_quad;
1556         sh->ndis_saddr = *vaddr;
1557         InsertHeadList((&sc->ndis_shlist), (&sh->ndis_list));
1558         NDIS_UNLOCK(sc);
1559
1560         return;
1561 }
1562
1563 struct ndis_allocwork {
1564         uint32_t                na_len;
1565         uint8_t                 na_cached;
1566         void                    *na_ctx;
1567         io_workitem             *na_iw;
1568 };
1569
1570 static void
1571 ndis_asyncmem_complete(dobj, arg)
1572         device_object           *dobj;
1573         void                    *arg;
1574 {
1575         ndis_miniport_block     *block;
1576         struct ndis_softc       *sc;
1577         struct ndis_allocwork   *w;
1578         void                    *vaddr;
1579         ndis_physaddr           paddr;
1580         ndis_allocdone_handler  donefunc;
1581
1582         w = arg;
1583         block = (ndis_miniport_block *)dobj->do_devext;
1584         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1585
1586         vaddr = NULL;
1587         paddr.np_quad = 0;
1588
1589         donefunc = sc->ndis_chars->nmc_allocate_complete_func;
1590         NdisMAllocateSharedMemory(block, w->na_len,
1591             w->na_cached, &vaddr, &paddr);
1592         MSCALL5(donefunc, block, vaddr, &paddr, w->na_len, w->na_ctx);
1593
1594         IoFreeWorkItem(w->na_iw);
1595         free(w, M_DEVBUF);
1596
1597         return;
1598 }
1599
1600 static ndis_status
1601 NdisMAllocateSharedMemoryAsync(ndis_handle adapter, uint32_t len,
1602     uint8_t cached, void *ctx)
1603 {
1604         ndis_miniport_block     *block;
1605         struct ndis_allocwork   *w;
1606         io_workitem             *iw;
1607         io_workitem_func        ifw;
1608
1609         if (adapter == NULL)
1610                 return(NDIS_STATUS_FAILURE);
1611
1612         block = adapter;
1613
1614         iw = IoAllocateWorkItem(block->nmb_deviceobj);
1615         if (iw == NULL)
1616                 return(NDIS_STATUS_FAILURE);
1617
1618         w = malloc(sizeof(struct ndis_allocwork), M_TEMP, M_NOWAIT);
1619
1620         if (w == NULL)
1621                 return(NDIS_STATUS_FAILURE);
1622
1623         w->na_cached = cached;
1624         w->na_len = len;
1625         w->na_ctx = ctx;
1626         w->na_iw = iw;
1627
1628         ifw = (io_workitem_func)ndis_findwrap((funcptr)ndis_asyncmem_complete);
1629         IoQueueWorkItem(iw, ifw, WORKQUEUE_DELAYED, w);
1630
1631         return(NDIS_STATUS_PENDING);
1632 }
1633
1634 static void
1635 NdisMFreeSharedMemory(ndis_handle adapter, uint32_t len, uint8_t cached,
1636     void *vaddr, ndis_physaddr paddr)
1637 {
1638         ndis_miniport_block     *block;
1639         struct ndis_softc       *sc;
1640         struct ndis_shmem       *sh = NULL;
1641         list_entry              *l;
1642
1643         if (vaddr == NULL || adapter == NULL)
1644                 return;
1645
1646         block = (ndis_miniport_block *)adapter;
1647         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1648
1649         /* Sanity check: is list empty? */
1650
1651         if (IsListEmpty(&sc->ndis_shlist))
1652                 return;
1653
1654         NDIS_LOCK(sc);
1655         l = sc->ndis_shlist.nle_flink;
1656         while (l != &sc->ndis_shlist) {
1657                 sh = CONTAINING_RECORD(l, struct ndis_shmem, ndis_list);
1658                 if (sh->ndis_saddr == vaddr)
1659                         break;
1660                 /*
1661                  * Check the physaddr too, just in case the driver lied
1662                  * about the virtual address.
1663                  */
1664                 if (sh->ndis_paddr.np_quad == paddr.np_quad)
1665                         break;
1666                 l = l->nle_flink;
1667         }
1668
1669         if (sh == NULL) {
1670                 NDIS_UNLOCK(sc);
1671                 printf("NDIS: buggy driver tried to free "
1672                     "invalid shared memory: vaddr: %p paddr: 0x%jx\n",
1673                     vaddr, (uintmax_t)paddr.np_quad);
1674                 return;
1675         }
1676
1677         RemoveEntryList(&sh->ndis_list);
1678
1679         NDIS_UNLOCK(sc);
1680
1681         bus_dmamap_unload(sh->ndis_stag, sh->ndis_smap);
1682         bus_dmamem_free(sh->ndis_stag, sh->ndis_saddr, sh->ndis_smap);
1683         bus_dma_tag_destroy(sh->ndis_stag);
1684
1685         free(sh, M_DEVBUF);
1686
1687         return;
1688 }
1689
1690 static ndis_status
1691 NdisMMapIoSpace(vaddr, adapter, paddr, len)
1692         void                    **vaddr;
1693         ndis_handle             adapter;
1694         ndis_physaddr           paddr;
1695         uint32_t                len;
1696 {
1697         if (adapter == NULL)
1698                 return(NDIS_STATUS_FAILURE);
1699
1700         *vaddr = MmMapIoSpace(paddr.np_quad, len, 0);
1701
1702         if (*vaddr == NULL)
1703                 return(NDIS_STATUS_FAILURE);
1704
1705         return(NDIS_STATUS_SUCCESS);
1706 }
1707
1708 static void
1709 NdisMUnmapIoSpace(adapter, vaddr, len)
1710         ndis_handle             adapter;
1711         void                    *vaddr;
1712         uint32_t                len;
1713 {
1714         MmUnmapIoSpace(vaddr, len);
1715         return;
1716 }
1717
1718 static uint32_t
1719 NdisGetCacheFillSize(void)
1720 {
1721         return(128);
1722 }
1723
1724 static uint32_t
1725 NdisMGetDmaAlignment(handle)
1726         ndis_handle             handle;
1727 {
1728         return(16);
1729 }
1730
1731 /*
1732  * NDIS has two methods for dealing with NICs that support DMA.
1733  * One is to just pass packets to the driver and let it call
1734  * NdisMStartBufferPhysicalMapping() to map each buffer in the packet
1735  * all by itself, and the other is to let the NDIS library handle the
1736  * buffer mapping internally, and hand the driver an already populated
1737  * scatter/gather fragment list. If the driver calls
1738  * NdisMInitializeScatterGatherDma(), it wants to use the latter
1739  * method.
1740  */
1741
1742 static ndis_status
1743 NdisMInitializeScatterGatherDma(ndis_handle adapter, uint8_t is64,
1744     uint32_t maxphysmap)
1745 {
1746         struct ndis_softc       *sc;
1747         ndis_miniport_block     *block;
1748         int                     error;
1749
1750         if (adapter == NULL)
1751                 return(NDIS_STATUS_FAILURE);
1752         block = (ndis_miniport_block *)adapter;
1753         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1754
1755         /* Don't do this twice. */
1756         if (sc->ndis_sc == 1)
1757                 return(NDIS_STATUS_SUCCESS);
1758
1759         error = bus_dma_tag_create(sc->ndis_parent_tag, ETHER_ALIGN, 0,
1760             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1761             MCLBYTES * NDIS_MAXSEG, NDIS_MAXSEG, MCLBYTES, BUS_DMA_ALLOCNOW,
1762             NULL, NULL, &sc->ndis_ttag);
1763
1764         sc->ndis_sc = 1;
1765
1766         return(NDIS_STATUS_SUCCESS);
1767 }
1768
1769 void
1770 NdisAllocatePacketPool(status, pool, descnum, protrsvdlen)
1771         ndis_status             *status;
1772         ndis_handle             *pool;
1773         uint32_t                descnum;
1774         uint32_t                protrsvdlen;
1775 {
1776         ndis_packet_pool        *p;
1777         ndis_packet             *packets;
1778         int                     i;
1779
1780         p = ExAllocatePoolWithTag(NonPagedPool, sizeof(ndis_packet_pool), 0);
1781         if (p == NULL) {
1782                 *status = NDIS_STATUS_RESOURCES;
1783                 return;
1784         }
1785
1786         p->np_cnt = descnum + NDIS_POOL_EXTRA;
1787         p->np_protrsvd = protrsvdlen;
1788         p->np_len = sizeof(ndis_packet) + protrsvdlen;
1789
1790         packets = ExAllocatePoolWithTag(NonPagedPool, p->np_cnt *
1791             p->np_len, 0);
1792
1793
1794         if (packets == NULL) {
1795                 ExFreePool(p);
1796                 *status = NDIS_STATUS_RESOURCES;
1797                 return;
1798         }
1799
1800         p->np_pktmem = packets;
1801
1802         for (i = 0; i < p->np_cnt; i++)
1803                 InterlockedPushEntrySList(&p->np_head,
1804                     (struct slist_entry *)&packets[i]);
1805
1806 #ifdef NDIS_DEBUG_PACKETS 
1807         p->np_dead = 0; 
1808         KeInitializeSpinLock(&p->np_lock);
1809         KeInitializeEvent(&p->np_event, EVENT_TYPE_NOTIFY, TRUE);
1810 #endif
1811
1812         *pool = p; 
1813         *status = NDIS_STATUS_SUCCESS;
1814         return;
1815 }
1816
1817 void
1818 NdisAllocatePacketPoolEx(status, pool, descnum, oflowdescnum, protrsvdlen)
1819         ndis_status             *status;
1820         ndis_handle             *pool;
1821         uint32_t                descnum;
1822         uint32_t                oflowdescnum;
1823         uint32_t                protrsvdlen;
1824 {
1825         return(NdisAllocatePacketPool(status, pool,
1826             descnum + oflowdescnum, protrsvdlen));
1827 }
1828
1829 uint32_t
1830 NdisPacketPoolUsage(pool)
1831         ndis_handle             pool;
1832 {
1833         ndis_packet_pool        *p;
1834
1835         p = (ndis_packet_pool *)pool;
1836         return(p->np_cnt - ExQueryDepthSList(&p->np_head));
1837 }
1838
1839 void
1840 NdisFreePacketPool(pool)
1841         ndis_handle             pool;
1842 {
1843         ndis_packet_pool        *p;
1844         int                     usage;
1845 #ifdef NDIS_DEBUG_PACKETS
1846         uint8_t                 irql;
1847 #endif
1848
1849         p = (ndis_packet_pool *)pool;
1850
1851 #ifdef NDIS_DEBUG_PACKETS
1852         KeAcquireSpinLock(&p->np_lock, &irql);
1853 #endif
1854
1855         usage = NdisPacketPoolUsage(pool);
1856
1857 #ifdef NDIS_DEBUG_PACKETS
1858         if (usage) {
1859                 p->np_dead = 1;
1860                 KeResetEvent(&p->np_event);
1861                 KeReleaseSpinLock(&p->np_lock, irql);
1862                 KeWaitForSingleObject(&p->np_event, 0, 0, FALSE, NULL);
1863         } else
1864                 KeReleaseSpinLock(&p->np_lock, irql);
1865 #endif
1866
1867         ExFreePool(p->np_pktmem);
1868         ExFreePool(p);
1869
1870         return;
1871 }
1872
1873 void
1874 NdisAllocatePacket(status, packet, pool)
1875         ndis_status             *status;
1876         ndis_packet             **packet;
1877         ndis_handle             pool;
1878 {
1879         ndis_packet_pool        *p;
1880         ndis_packet             *pkt;
1881 #ifdef NDIS_DEBUG_PACKETS
1882         uint8_t                 irql;
1883 #endif
1884
1885         p = (ndis_packet_pool *)pool;
1886
1887 #ifdef NDIS_DEBUG_PACKETS
1888         KeAcquireSpinLock(&p->np_lock, &irql);
1889         if (p->np_dead) {
1890                 KeReleaseSpinLock(&p->np_lock, irql);
1891                 printf("NDIS: tried to allocate packet from dead pool %p\n",
1892                     pool);
1893                 *status = NDIS_STATUS_RESOURCES;
1894                 return;
1895         }
1896 #endif
1897
1898         pkt = (ndis_packet *)InterlockedPopEntrySList(&p->np_head);
1899
1900 #ifdef NDIS_DEBUG_PACKETS
1901         KeReleaseSpinLock(&p->np_lock, irql);
1902 #endif
1903
1904         if (pkt == NULL) {
1905                 *status = NDIS_STATUS_RESOURCES;
1906                 return;
1907         }
1908
1909
1910         bzero((char *)pkt, sizeof(ndis_packet));
1911
1912         /* Save pointer to the pool. */
1913         pkt->np_private.npp_pool = pool;
1914
1915         /* Set the oob offset pointer. Lots of things expect this. */
1916         pkt->np_private.npp_packetooboffset = offsetof(ndis_packet, np_oob);
1917
1918         /*
1919          * We must initialize the packet flags correctly in order
1920          * for the NDIS_SET_PACKET_MEDIA_SPECIFIC_INFO() and
1921          * NDIS_GET_PACKET_MEDIA_SPECIFIC_INFO() macros to work
1922          * correctly.
1923          */
1924         pkt->np_private.npp_ndispktflags = NDIS_PACKET_ALLOCATED_BY_NDIS;
1925         pkt->np_private.npp_validcounts = FALSE;
1926
1927         *packet = pkt;
1928
1929         *status = NDIS_STATUS_SUCCESS;
1930
1931         return;
1932 }
1933
1934 void
1935 NdisFreePacket(packet)
1936         ndis_packet             *packet;
1937 {
1938         ndis_packet_pool        *p;
1939 #ifdef NDIS_DEBUG_PACKETS
1940         uint8_t                 irql;
1941 #endif
1942
1943         p = (ndis_packet_pool *)packet->np_private.npp_pool;
1944
1945 #ifdef NDIS_DEBUG_PACKETS
1946         KeAcquireSpinLock(&p->np_lock, &irql);
1947 #endif
1948
1949         InterlockedPushEntrySList(&p->np_head, (slist_entry *)packet);
1950
1951 #ifdef NDIS_DEBUG_PACKETS
1952         if (p->np_dead) {
1953                 if (ExQueryDepthSList(&p->np_head) == p->np_cnt)
1954                         KeSetEvent(&p->np_event, IO_NO_INCREMENT, FALSE);
1955         }
1956         KeReleaseSpinLock(&p->np_lock, irql);
1957 #endif
1958
1959         return;
1960 }
1961
1962 static void
1963 NdisUnchainBufferAtFront(packet, buf)
1964         ndis_packet             *packet;
1965         ndis_buffer             **buf;
1966 {
1967         ndis_packet_private     *priv;
1968
1969         if (packet == NULL || buf == NULL)
1970                 return;
1971
1972         priv = &packet->np_private;
1973
1974         priv->npp_validcounts = FALSE;
1975
1976         if (priv->npp_head == priv->npp_tail) {
1977                 *buf = priv->npp_head;
1978                 priv->npp_head = priv->npp_tail = NULL;
1979         } else {
1980                 *buf = priv->npp_head;
1981                 priv->npp_head = (*buf)->mdl_next;
1982         }
1983
1984         return;
1985 }
1986
1987 static void
1988 NdisUnchainBufferAtBack(packet, buf)
1989         ndis_packet             *packet;
1990         ndis_buffer             **buf;
1991 {
1992         ndis_packet_private     *priv;
1993         ndis_buffer             *tmp;
1994
1995         if (packet == NULL || buf == NULL)
1996                 return;
1997
1998         priv = &packet->np_private;
1999
2000         priv->npp_validcounts = FALSE;
2001
2002         if (priv->npp_head == priv->npp_tail) {
2003                 *buf = priv->npp_head;
2004                 priv->npp_head = priv->npp_tail = NULL;
2005         } else {
2006                 *buf = priv->npp_tail;
2007                 tmp = priv->npp_head;
2008                 while (tmp->mdl_next != priv->npp_tail)
2009                         tmp = tmp->mdl_next;
2010                 priv->npp_tail = tmp;
2011                 tmp->mdl_next = NULL;
2012         }
2013
2014         return;
2015 }
2016
2017 /*
2018  * The NDIS "buffer" is really an MDL (memory descriptor list)
2019  * which is used to describe a buffer in a way that allows it
2020  * to mapped into different contexts. We have to be careful how
2021  * we handle them: in some versions of Windows, the NdisFreeBuffer()
2022  * routine is an actual function in the NDIS API, but in others
2023  * it's just a macro wrapper around IoFreeMdl(). There's really
2024  * no way to use the 'descnum' parameter to count how many
2025  * "buffers" are allocated since in order to use IoFreeMdl() to
2026  * dispose of a buffer, we have to use IoAllocateMdl() to allocate
2027  * them, and IoAllocateMdl() just grabs them out of the heap.
2028  */
2029
2030 static void
2031 NdisAllocateBufferPool(status, pool, descnum)
2032         ndis_status             *status;
2033         ndis_handle             *pool;
2034         uint32_t                descnum;
2035 {
2036
2037         /*
2038          * The only thing we can really do here is verify that descnum
2039          * is a reasonable value, but I really don't know what to check
2040          * it against.
2041          */
2042
2043         *pool = NonPagedPool;
2044         *status = NDIS_STATUS_SUCCESS;
2045         return;
2046 }
2047
2048 static void
2049 NdisFreeBufferPool(pool)
2050         ndis_handle             pool;
2051 {
2052         return;
2053 }
2054
2055 static void
2056 NdisAllocateBuffer(status, buffer, pool, vaddr, len)
2057         ndis_status             *status;
2058         ndis_buffer             **buffer;
2059         ndis_handle             pool;
2060         void                    *vaddr;
2061         uint32_t                len;
2062 {
2063         ndis_buffer             *buf;
2064
2065         buf = IoAllocateMdl(vaddr, len, FALSE, FALSE, NULL);
2066         if (buf == NULL) {
2067                 *status = NDIS_STATUS_RESOURCES;
2068                 return;
2069         }
2070
2071         MmBuildMdlForNonPagedPool(buf);
2072
2073         *buffer = buf;
2074         *status = NDIS_STATUS_SUCCESS;
2075
2076         return;
2077 }
2078
2079 static void
2080 NdisFreeBuffer(buf)
2081         ndis_buffer             *buf;
2082 {
2083         IoFreeMdl(buf);
2084         return;
2085 }
2086
2087 /* Aw c'mon. */
2088
2089 static uint32_t
2090 NdisBufferLength(buf)
2091         ndis_buffer             *buf;
2092 {
2093         return(MmGetMdlByteCount(buf));
2094 }
2095
2096 /*
2097  * Get the virtual address and length of a buffer.
2098  * Note: the vaddr argument is optional.
2099  */
2100
2101 static void
2102 NdisQueryBuffer(buf, vaddr, len)
2103         ndis_buffer             *buf;
2104         void                    **vaddr;
2105         uint32_t                *len;
2106 {
2107         if (vaddr != NULL)
2108                 *vaddr = MmGetMdlVirtualAddress(buf);
2109         *len = MmGetMdlByteCount(buf);
2110
2111         return;
2112 }
2113
2114 /* Same as above -- we don't care about the priority. */
2115
2116 static void
2117 NdisQueryBufferSafe(buf, vaddr, len, prio)
2118         ndis_buffer             *buf;
2119         void                    **vaddr;
2120         uint32_t                *len;
2121         uint32_t                prio;
2122 {
2123         if (vaddr != NULL)
2124                 *vaddr = MmGetMdlVirtualAddress(buf);
2125         *len = MmGetMdlByteCount(buf);
2126
2127         return;
2128 }
2129
2130 /* Damnit Microsoft!! How many ways can you do the same thing?! */
2131
2132 static void *
2133 NdisBufferVirtualAddress(buf)
2134         ndis_buffer             *buf;
2135 {
2136         return(MmGetMdlVirtualAddress(buf));
2137 }
2138
2139 static void *
2140 NdisBufferVirtualAddressSafe(buf, prio)
2141         ndis_buffer             *buf;
2142         uint32_t                prio;
2143 {
2144         return(MmGetMdlVirtualAddress(buf));
2145 }
2146
2147 static void
2148 NdisAdjustBufferLength(buf, len)
2149         ndis_buffer             *buf;
2150         int                     len;
2151 {
2152         MmGetMdlByteCount(buf) = len;
2153
2154         return;
2155 }
2156
2157 static uint32_t
2158 NdisInterlockedIncrement(addend)
2159         uint32_t                *addend;
2160 {
2161         atomic_add_long((u_long *)addend, 1);
2162         return(*addend);
2163 }
2164
2165 static uint32_t
2166 NdisInterlockedDecrement(addend)
2167         uint32_t                *addend;
2168 {
2169         atomic_subtract_long((u_long *)addend, 1);
2170         return(*addend);
2171 }
2172
2173 static void
2174 NdisInitializeEvent(event)
2175         ndis_event              *event;
2176 {
2177         /*
2178          * NDIS events are always notification
2179          * events, and should be initialized to the
2180          * not signaled state.
2181          */
2182         KeInitializeEvent(&event->ne_event, EVENT_TYPE_NOTIFY, FALSE);
2183         return;
2184 }
2185
2186 static void
2187 NdisSetEvent(event)
2188         ndis_event              *event;
2189 {
2190         KeSetEvent(&event->ne_event, IO_NO_INCREMENT, FALSE);
2191         return;
2192 }
2193
2194 static void
2195 NdisResetEvent(event)
2196         ndis_event              *event;
2197 {
2198         KeResetEvent(&event->ne_event);
2199         return;
2200 }
2201
2202 static uint8_t
2203 NdisWaitEvent(event, msecs)
2204         ndis_event              *event;
2205         uint32_t                msecs;
2206 {
2207         int64_t                 duetime;
2208         uint32_t                rval;
2209
2210         duetime = ((int64_t)msecs * -10000);
2211         rval = KeWaitForSingleObject(event,
2212             0, 0, TRUE, msecs ? & duetime : NULL);
2213
2214         if (rval == STATUS_TIMEOUT)
2215                 return(FALSE);
2216
2217         return(TRUE);
2218 }
2219
2220 static ndis_status
2221 NdisUnicodeStringToAnsiString(dstr, sstr)
2222         ansi_string             *dstr;
2223         unicode_string          *sstr;
2224 {
2225         uint32_t                rval;
2226
2227         rval = RtlUnicodeStringToAnsiString(dstr, sstr, FALSE);
2228
2229         if (rval == STATUS_INSUFFICIENT_RESOURCES)
2230                 return(NDIS_STATUS_RESOURCES);
2231         if (rval)
2232                 return(NDIS_STATUS_FAILURE);
2233
2234         return (NDIS_STATUS_SUCCESS);
2235 }
2236
2237 static ndis_status
2238 NdisAnsiStringToUnicodeString(dstr, sstr)
2239         unicode_string          *dstr;
2240         ansi_string             *sstr;
2241 {
2242         uint32_t                rval;
2243
2244         rval = RtlAnsiStringToUnicodeString(dstr, sstr, FALSE);
2245
2246         if (rval == STATUS_INSUFFICIENT_RESOURCES)
2247                 return(NDIS_STATUS_RESOURCES);
2248         if (rval)
2249                 return(NDIS_STATUS_FAILURE);
2250
2251         return (NDIS_STATUS_SUCCESS);
2252 }
2253
2254 static ndis_status
2255 NdisMPciAssignResources(adapter, slot, list)
2256         ndis_handle             adapter;
2257         uint32_t                slot;
2258         ndis_resource_list      **list;
2259 {
2260         ndis_miniport_block     *block;
2261
2262         if (adapter == NULL || list == NULL)
2263                 return (NDIS_STATUS_FAILURE);
2264
2265         block = (ndis_miniport_block *)adapter;
2266         *list = block->nmb_rlist;
2267
2268         return (NDIS_STATUS_SUCCESS);
2269 }
2270
2271 static uint8_t
2272 ndis_intr(iobj, arg)
2273         kinterrupt              *iobj;
2274         void                    *arg;
2275 {
2276         struct ndis_softc       *sc;
2277         uint8_t                 is_our_intr = FALSE;
2278         int                     call_isr = 0;
2279         ndis_miniport_interrupt *intr;
2280
2281         sc = arg;
2282         intr = sc->ndis_block->nmb_interrupt;
2283
2284         if (intr == NULL || sc->ndis_block->nmb_miniportadapterctx == NULL)
2285                 return(FALSE);
2286
2287         if (sc->ndis_block->nmb_interrupt->ni_isrreq == TRUE)
2288                 MSCALL3(intr->ni_isrfunc, &is_our_intr, &call_isr,
2289                     sc->ndis_block->nmb_miniportadapterctx);
2290         else {
2291                 MSCALL1(sc->ndis_chars->nmc_disable_interrupts_func,
2292                     sc->ndis_block->nmb_miniportadapterctx);
2293                 call_isr = 1;
2294         }
2295  
2296         if (call_isr)
2297                 IoRequestDpc(sc->ndis_block->nmb_deviceobj, NULL, sc);
2298
2299         return(is_our_intr);
2300 }
2301
2302 static void
2303 ndis_intrhand(dpc, intr, sysarg1, sysarg2)
2304         kdpc                    *dpc;
2305         ndis_miniport_interrupt *intr;
2306         void                    *sysarg1;
2307         void                    *sysarg2;
2308 {
2309         struct ndis_softc       *sc;
2310         ndis_miniport_block     *block;
2311         ndis_handle             adapter;
2312
2313         block = intr->ni_block;
2314         adapter = block->nmb_miniportadapterctx;
2315         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2316
2317         if (NDIS_SERIALIZED(sc->ndis_block))
2318                 KeAcquireSpinLockAtDpcLevel(&block->nmb_lock);
2319
2320         MSCALL1(intr->ni_dpcfunc, adapter);
2321
2322         /* If there's a MiniportEnableInterrupt() routine, call it. */
2323
2324         if (sc->ndis_chars->nmc_enable_interrupts_func != NULL)
2325                 MSCALL1(sc->ndis_chars->nmc_enable_interrupts_func, adapter);
2326
2327         if (NDIS_SERIALIZED(sc->ndis_block))
2328                 KeReleaseSpinLockFromDpcLevel(&block->nmb_lock);
2329
2330         /*
2331          * Set the completion event if we've drained all
2332          * pending interrupts.
2333          */
2334
2335         KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
2336         intr->ni_dpccnt--;
2337         if (intr->ni_dpccnt == 0)
2338                 KeSetEvent(&intr->ni_dpcevt, IO_NO_INCREMENT, FALSE);
2339         KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
2340
2341         return;
2342 }
2343
2344 static ndis_status
2345 NdisMRegisterInterrupt(ndis_miniport_interrupt *intr, ndis_handle adapter,
2346     uint32_t ivec, uint32_t ilevel, uint8_t reqisr, uint8_t shared,
2347     ndis_interrupt_mode imode)
2348 {
2349         ndis_miniport_block     *block;
2350         ndis_miniport_characteristics *ch;
2351         struct ndis_softc       *sc;
2352         int                     error;
2353
2354         block = adapter;
2355         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2356         ch = IoGetDriverObjectExtension(block->nmb_deviceobj->do_drvobj,
2357             (void *)1);
2358
2359         intr->ni_rsvd = ExAllocatePoolWithTag(NonPagedPool,
2360             sizeof(struct mtx), 0);
2361         if (intr->ni_rsvd == NULL)
2362                 return(NDIS_STATUS_RESOURCES);
2363
2364         intr->ni_block = adapter;
2365         intr->ni_isrreq = reqisr;
2366         intr->ni_shared = shared;
2367         intr->ni_dpccnt = 0;
2368         intr->ni_isrfunc = ch->nmc_isr_func;
2369         intr->ni_dpcfunc = ch->nmc_interrupt_func;
2370
2371         KeInitializeEvent(&intr->ni_dpcevt, EVENT_TYPE_NOTIFY, TRUE);
2372         KeInitializeDpc(&intr->ni_dpc,
2373             ndis_findwrap((funcptr)ndis_intrhand), intr);
2374         KeSetImportanceDpc(&intr->ni_dpc, KDPC_IMPORTANCE_LOW);
2375
2376         error = IoConnectInterrupt(&intr->ni_introbj,
2377             ndis_findwrap((funcptr)ndis_intr), sc, NULL,
2378             ivec, ilevel, 0, imode, shared, 0, FALSE);
2379
2380         if (error != STATUS_SUCCESS)
2381                 return(NDIS_STATUS_FAILURE);
2382
2383         block->nmb_interrupt = intr;
2384
2385         return(NDIS_STATUS_SUCCESS);
2386 }
2387
2388 static void
2389 NdisMDeregisterInterrupt(intr)
2390         ndis_miniport_interrupt *intr;
2391 {
2392         ndis_miniport_block     *block;
2393         uint8_t                 irql;
2394
2395         block = intr->ni_block;
2396
2397         /* Should really be KeSynchronizeExecution() */
2398
2399         KeAcquireSpinLock(intr->ni_introbj->ki_lock, &irql);
2400         block->nmb_interrupt = NULL;
2401         KeReleaseSpinLock(intr->ni_introbj->ki_lock, irql);
2402 /*
2403         KeFlushQueuedDpcs();
2404 */
2405         /* Disconnect our ISR */
2406
2407         IoDisconnectInterrupt(intr->ni_introbj);
2408
2409         KeWaitForSingleObject(&intr->ni_dpcevt, 0, 0, FALSE, NULL);
2410         KeResetEvent(&intr->ni_dpcevt);
2411
2412         return;
2413 }
2414
2415 static void
2416 NdisMRegisterAdapterShutdownHandler(adapter, shutdownctx, shutdownfunc)
2417         ndis_handle             adapter;
2418         void                    *shutdownctx;
2419         ndis_shutdown_handler   shutdownfunc;
2420 {
2421         ndis_miniport_block     *block;
2422         ndis_miniport_characteristics *chars;
2423         struct ndis_softc       *sc;
2424
2425         if (adapter == NULL)
2426                 return;
2427
2428         block = (ndis_miniport_block *)adapter;
2429         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2430         chars = sc->ndis_chars;
2431
2432         chars->nmc_shutdown_handler = shutdownfunc;
2433         chars->nmc_rsvd0 = shutdownctx;
2434
2435         return;
2436 }
2437
2438 static void
2439 NdisMDeregisterAdapterShutdownHandler(adapter)
2440         ndis_handle             adapter;
2441 {
2442         ndis_miniport_block     *block;
2443         ndis_miniport_characteristics *chars;
2444         struct ndis_softc       *sc;
2445
2446         if (adapter == NULL)
2447                 return;
2448
2449         block = (ndis_miniport_block *)adapter;
2450         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2451         chars = sc->ndis_chars;
2452
2453         chars->nmc_shutdown_handler = NULL;
2454         chars->nmc_rsvd0 = NULL;
2455
2456         return;
2457 }
2458
2459 static uint32_t
2460 NDIS_BUFFER_TO_SPAN_PAGES(buf)
2461         ndis_buffer             *buf;
2462 {
2463         if (buf == NULL)
2464                 return(0);
2465         if (MmGetMdlByteCount(buf) == 0)
2466                 return(1);
2467         return(SPAN_PAGES(MmGetMdlVirtualAddress(buf),
2468             MmGetMdlByteCount(buf)));
2469 }
2470
2471 static void
2472 NdisGetBufferPhysicalArraySize(buf, pages)
2473         ndis_buffer             *buf;
2474         uint32_t                *pages;
2475 {
2476         if (buf == NULL)
2477                 return;
2478
2479         *pages = NDIS_BUFFER_TO_SPAN_PAGES(buf);
2480         return;
2481 }
2482
2483 static void
2484 NdisQueryBufferOffset(buf, off, len)
2485         ndis_buffer             *buf;
2486         uint32_t                *off;
2487         uint32_t                *len;
2488 {
2489         if (buf == NULL)
2490                 return;
2491
2492         *off = MmGetMdlByteOffset(buf);
2493         *len = MmGetMdlByteCount(buf);
2494
2495         return;
2496 }
2497
2498 void
2499 NdisMSleep(usecs)
2500         uint32_t                usecs;
2501 {
2502         ktimer                  timer;
2503
2504         /*
2505          * During system bootstrap, (i.e. cold == 1), we aren't
2506          * allowed to sleep, so we have to do a hard DELAY()
2507          * instead.
2508          */
2509
2510         if (cold)
2511                 DELAY(usecs);
2512         else {
2513                 KeInitializeTimer(&timer);
2514                 KeSetTimer(&timer, ((int64_t)usecs * -10), NULL);
2515                 KeWaitForSingleObject(&timer, 0, 0, FALSE, NULL);
2516         }
2517
2518         return;
2519 }
2520
2521 static uint32_t
2522 NdisReadPcmciaAttributeMemory(handle, offset, buf, len)
2523         ndis_handle             handle;
2524         uint32_t                offset;
2525         void                    *buf;
2526         uint32_t                len;
2527 {
2528         struct ndis_softc       *sc;
2529         ndis_miniport_block     *block;
2530         bus_space_handle_t      bh;
2531         bus_space_tag_t         bt;
2532         char                    *dest;
2533         int                     i;
2534
2535         if (handle == NULL)
2536                 return(0);
2537
2538         block = (ndis_miniport_block *)handle;
2539         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2540         dest = buf;
2541
2542         bh = rman_get_bushandle(sc->ndis_res_am);
2543         bt = rman_get_bustag(sc->ndis_res_am);
2544
2545         for (i = 0; i < len; i++)
2546                 dest[i] = bus_space_read_1(bt, bh, (offset + i) * 2);
2547
2548         return(i);
2549 }
2550
2551 static uint32_t
2552 NdisWritePcmciaAttributeMemory(handle, offset, buf, len)
2553         ndis_handle             handle;
2554         uint32_t                offset;
2555         void                    *buf;
2556         uint32_t                len;
2557 {
2558         struct ndis_softc       *sc;
2559         ndis_miniport_block     *block;
2560         bus_space_handle_t      bh;
2561         bus_space_tag_t         bt;
2562         char                    *src;
2563         int                     i;
2564
2565         if (handle == NULL)
2566                 return(0);
2567
2568         block = (ndis_miniport_block *)handle;
2569         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2570         src = buf;
2571
2572         bh = rman_get_bushandle(sc->ndis_res_am);
2573         bt = rman_get_bustag(sc->ndis_res_am);
2574
2575         for (i = 0; i < len; i++)
2576                 bus_space_write_1(bt, bh, (offset + i) * 2, src[i]);
2577
2578         return(i);
2579 }
2580
2581 static list_entry *
2582 NdisInterlockedInsertHeadList(head, entry, lock)
2583         list_entry              *head;
2584         list_entry              *entry;
2585         ndis_spin_lock          *lock;
2586 {
2587         list_entry              *flink;
2588
2589         KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
2590         flink = head->nle_flink;
2591         entry->nle_flink = flink;
2592         entry->nle_blink = head;
2593         flink->nle_blink = entry;
2594         head->nle_flink = entry;
2595         KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
2596
2597         return(flink);
2598 }
2599
2600 static list_entry *
2601 NdisInterlockedRemoveHeadList(head, lock)
2602         list_entry              *head;
2603         ndis_spin_lock          *lock;
2604 {
2605         list_entry              *flink;
2606         list_entry              *entry;
2607
2608         KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
2609         entry = head->nle_flink;
2610         flink = entry->nle_flink;
2611         head->nle_flink = flink;
2612         flink->nle_blink = head;
2613         KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
2614
2615         return(entry);
2616 }
2617
2618 static list_entry *
2619 NdisInterlockedInsertTailList(head, entry, lock)
2620         list_entry              *head;
2621         list_entry              *entry;
2622         ndis_spin_lock          *lock;
2623 {
2624         list_entry              *blink;
2625
2626         KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
2627         blink = head->nle_blink;
2628         entry->nle_flink = head;
2629         entry->nle_blink = blink;
2630         blink->nle_flink = entry;
2631         head->nle_blink = entry;
2632         KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
2633
2634         return(blink);
2635 }
2636
2637 static uint8_t
2638 NdisMSynchronizeWithInterrupt(intr, syncfunc, syncctx)
2639         ndis_miniport_interrupt *intr;
2640         void                    *syncfunc;
2641         void                    *syncctx;
2642 {
2643         return(KeSynchronizeExecution(intr->ni_introbj, syncfunc, syncctx));
2644 }
2645
2646 static void
2647 NdisGetCurrentSystemTime(tval)
2648         uint64_t                *tval;
2649 {
2650         ntoskrnl_time(tval);
2651         return;
2652 }
2653
2654 /*
2655  * Return the number of milliseconds since the system booted.
2656  */
2657 static void
2658 NdisGetSystemUpTime(tval)
2659         uint32_t                *tval;
2660 {
2661         struct timespec         ts;
2662
2663         nanouptime(&ts);
2664         *tval = ts.tv_nsec / 1000000 + ts.tv_sec * 1000;
2665
2666         return;
2667 }
2668
2669 static void
2670 NdisInitializeString(dst, src)
2671         unicode_string          *dst;
2672         char                    *src;
2673 {
2674         ansi_string             as;
2675         RtlInitAnsiString(&as, src);
2676         RtlAnsiStringToUnicodeString(dst, &as, TRUE);
2677         return;
2678 }
2679
2680 static void
2681 NdisFreeString(str)
2682         unicode_string          *str;
2683 {
2684         RtlFreeUnicodeString(str);
2685         return;
2686 }
2687
2688 static ndis_status
2689 NdisMRemoveMiniport(adapter)
2690         ndis_handle             *adapter;
2691 {
2692         return(NDIS_STATUS_SUCCESS);
2693 }
2694
2695 static void
2696 NdisInitAnsiString(dst, src)
2697         ansi_string             *dst;
2698         char                    *src;
2699 {
2700         RtlInitAnsiString(dst, src);
2701         return;
2702 }
2703
2704 static void
2705 NdisInitUnicodeString(dst, src)
2706         unicode_string          *dst;
2707         uint16_t                *src;
2708 {
2709         RtlInitUnicodeString(dst, src);
2710         return;
2711 }
2712
2713 static void NdisMGetDeviceProperty(adapter, phydevobj,
2714         funcdevobj, nextdevobj, resources, transresources)
2715         ndis_handle             adapter;
2716         device_object           **phydevobj;
2717         device_object           **funcdevobj;
2718         device_object           **nextdevobj;
2719         cm_resource_list        *resources;
2720         cm_resource_list        *transresources;
2721 {
2722         ndis_miniport_block     *block;
2723
2724         block = (ndis_miniport_block *)adapter;
2725
2726         if (phydevobj != NULL)
2727                 *phydevobj = block->nmb_physdeviceobj;
2728         if (funcdevobj != NULL)
2729                 *funcdevobj = block->nmb_deviceobj;
2730         if (nextdevobj != NULL)
2731                 *nextdevobj = block->nmb_nextdeviceobj;
2732
2733         return;
2734 }
2735
2736 static void
2737 NdisGetFirstBufferFromPacket(packet, buf, firstva, firstlen, totlen)
2738         ndis_packet             *packet;
2739         ndis_buffer             **buf;
2740         void                    **firstva;
2741         uint32_t                *firstlen;
2742         uint32_t                *totlen;
2743 {
2744         ndis_buffer             *tmp;
2745
2746         tmp = packet->np_private.npp_head;
2747         *buf = tmp;
2748         if (tmp == NULL) {
2749                 *firstva = NULL;
2750                 *firstlen = *totlen = 0;
2751         } else {
2752                 *firstva = MmGetMdlVirtualAddress(tmp);
2753                 *firstlen = *totlen = MmGetMdlByteCount(tmp);
2754                 for (tmp = tmp->mdl_next; tmp != NULL; tmp = tmp->mdl_next)
2755                         *totlen += MmGetMdlByteCount(tmp);
2756         }
2757
2758         return;
2759 }
2760
2761 static void
2762 NdisGetFirstBufferFromPacketSafe(packet, buf, firstva, firstlen, totlen, prio)
2763         ndis_packet             *packet;
2764         ndis_buffer             **buf;
2765         void                    **firstva;
2766         uint32_t                *firstlen;
2767         uint32_t                *totlen;
2768         uint32_t                prio;
2769 {
2770         NdisGetFirstBufferFromPacket(packet, buf, firstva, firstlen, totlen);
2771 }
2772
2773 static int
2774 ndis_find_sym(lf, filename, suffix, sym)
2775         linker_file_t           lf;
2776         char                    *filename;
2777         char                    *suffix;
2778         caddr_t                 *sym;
2779 {
2780         char                    *fullsym;
2781         char                    *suf;
2782         int                     i;
2783
2784         fullsym = ExAllocatePoolWithTag(NonPagedPool, MAXPATHLEN, 0);
2785         if (fullsym == NULL)
2786                 return(ENOMEM);
2787
2788         bzero(fullsym, MAXPATHLEN);
2789         strncpy(fullsym, filename, MAXPATHLEN);
2790         if (strlen(filename) < 4) {
2791                 ExFreePool(fullsym);
2792                 return(EINVAL);
2793         }
2794
2795         /* If the filename has a .ko suffix, strip if off. */
2796         suf = fullsym + (strlen(filename) - 3);
2797         if (strcmp(suf, ".ko") == 0)
2798                 *suf = '\0';
2799
2800         for (i = 0; i < strlen(fullsym); i++) {
2801                 if (fullsym[i] == '.')
2802                         fullsym[i] = '_';
2803                 else
2804                         fullsym[i] = tolower(fullsym[i]);
2805         }
2806         strcat(fullsym, suffix);
2807         *sym = linker_file_lookup_symbol(lf, fullsym, 0);
2808         ExFreePool(fullsym);
2809         if (*sym == 0)
2810                 return(ENOENT);
2811
2812         return(0);
2813 }
2814
2815 struct ndis_checkmodule {
2816         char    *afilename;
2817         ndis_fh *fh;
2818 };
2819
2820 /*
2821  * See if a single module contains the symbols for a specified file.
2822  */
2823 static int
2824 NdisCheckModule(linker_file_t lf, void *context)
2825 {
2826         struct ndis_checkmodule *nc;
2827         caddr_t                 kldstart, kldend;
2828
2829         nc = (struct ndis_checkmodule *)context;
2830         if (ndis_find_sym(lf, nc->afilename, "_start", &kldstart))
2831                 return (0);
2832         if (ndis_find_sym(lf, nc->afilename, "_end", &kldend))
2833                 return (0);
2834         nc->fh->nf_vp = lf;
2835         nc->fh->nf_map = NULL;
2836         nc->fh->nf_type = NDIS_FH_TYPE_MODULE;
2837         nc->fh->nf_maplen = (kldend - kldstart) & 0xFFFFFFFF;
2838         return (1);
2839 }
2840
2841 /* can also return NDIS_STATUS_RESOURCES/NDIS_STATUS_ERROR_READING_FILE */
2842 static void
2843 NdisOpenFile(status, filehandle, filelength, filename, highestaddr)
2844         ndis_status             *status;
2845         ndis_handle             *filehandle;
2846         uint32_t                *filelength;
2847         unicode_string          *filename;
2848         ndis_physaddr           highestaddr;
2849 {
2850         ansi_string             as;
2851         char                    *afilename = NULL;
2852         struct thread           *td = curthread;
2853         struct nameidata        nd;
2854         int                     flags, error, vfslocked;
2855         struct vattr            vat;
2856         struct vattr            *vap = &vat;
2857         ndis_fh                 *fh;
2858         char                    *path;
2859         struct ndis_checkmodule nc;
2860
2861         if (RtlUnicodeStringToAnsiString(&as, filename, TRUE)) {
2862                 *status = NDIS_STATUS_RESOURCES;
2863                 return;
2864         }
2865
2866         afilename = strdup(as.as_buf, M_DEVBUF);
2867         RtlFreeAnsiString(&as);
2868
2869         fh = ExAllocatePoolWithTag(NonPagedPool, sizeof(ndis_fh), 0);
2870         if (fh == NULL) {
2871                 free(afilename, M_DEVBUF);
2872                 *status = NDIS_STATUS_RESOURCES;
2873                 return;
2874         }
2875
2876         fh->nf_name = afilename;
2877
2878         /*
2879          * During system bootstrap, it's impossible to load files
2880          * from the rootfs since it's not mounted yet. We therefore
2881          * offer the possibility of opening files that have been
2882          * preloaded as modules instead. Both choices will work
2883          * when kldloading a module from multiuser, but only the
2884          * module option will work during bootstrap. The module
2885          * loading option works by using the ndiscvt(8) utility
2886          * to convert the arbitrary file into a .ko using objcopy(1).
2887          * This file will contain two special symbols: filename_start
2888          * and filename_end. All we have to do is traverse the KLD
2889          * list in search of those symbols and we've found the file
2890          * data. As an added bonus, ndiscvt(8) will also generate
2891          * a normal .o file which can be linked statically with
2892          * the kernel. This means that the symbols will actual reside
2893          * in the kernel's symbol table, but that doesn't matter to
2894          * us since the kernel appears to us as just another module.
2895          */
2896
2897         nc.afilename = afilename;
2898         nc.fh = fh;
2899         if (linker_file_foreach(NdisCheckModule, &nc)) {
2900                 *filelength = fh->nf_maplen;
2901                 *filehandle = fh;
2902                 *status = NDIS_STATUS_SUCCESS;
2903                 return;
2904         }
2905
2906         if (TAILQ_EMPTY(&mountlist)) {
2907                 ExFreePool(fh);
2908                 *status = NDIS_STATUS_FILE_NOT_FOUND;
2909                 printf("NDIS: could not find file %s in linker list\n",
2910                     afilename);
2911                 printf("NDIS: and no filesystems mounted yet, "
2912                     "aborting NdisOpenFile()\n");
2913                 free(afilename, M_DEVBUF);
2914                 return;
2915         }
2916
2917         path = ExAllocatePoolWithTag(NonPagedPool, MAXPATHLEN, 0);
2918         if (path == NULL) {
2919                 ExFreePool(fh);
2920                 free(afilename, M_DEVBUF);
2921                 *status = NDIS_STATUS_RESOURCES;
2922                 return;
2923         }
2924
2925         snprintf(path, MAXPATHLEN, "%s/%s", ndis_filepath, afilename);
2926
2927         /* Some threads don't have a current working directory. */
2928
2929         if (td->td_proc->p_fd->fd_rdir == NULL)
2930                 td->td_proc->p_fd->fd_rdir = rootvnode;
2931         if (td->td_proc->p_fd->fd_cdir == NULL)
2932                 td->td_proc->p_fd->fd_cdir = rootvnode;
2933
2934         NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, path, td);
2935
2936         flags = FREAD;
2937         error = vn_open(&nd, &flags, 0, NULL);
2938         if (error) {
2939                 *status = NDIS_STATUS_FILE_NOT_FOUND;
2940                 ExFreePool(fh);
2941                 printf("NDIS: open file %s failed: %d\n", path, error);
2942                 ExFreePool(path);
2943                 free(afilename, M_DEVBUF);
2944                 return;
2945         }
2946         vfslocked = NDHASGIANT(&nd);
2947
2948         ExFreePool(path);
2949
2950         NDFREE(&nd, NDF_ONLY_PNBUF);
2951
2952         /* Get the file size. */
2953         VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
2954         VOP_UNLOCK(nd.ni_vp, 0);
2955         VFS_UNLOCK_GIANT(vfslocked);
2956
2957         fh->nf_vp = nd.ni_vp;
2958         fh->nf_map = NULL;
2959         fh->nf_type = NDIS_FH_TYPE_VFS;
2960         *filehandle = fh;
2961         *filelength = fh->nf_maplen = vap->va_size & 0xFFFFFFFF;
2962         *status = NDIS_STATUS_SUCCESS;
2963
2964         return;
2965 }
2966
2967 static void
2968 NdisMapFile(status, mappedbuffer, filehandle)
2969         ndis_status             *status;
2970         void                    **mappedbuffer;
2971         ndis_handle             filehandle;
2972 {
2973         ndis_fh                 *fh;
2974         struct thread           *td = curthread;
2975         linker_file_t           lf;
2976         caddr_t                 kldstart;
2977         int                     error, resid, vfslocked;
2978         struct vnode            *vp;
2979
2980         if (filehandle == NULL) {
2981                 *status = NDIS_STATUS_FAILURE;
2982                 return;
2983         }
2984
2985         fh = (ndis_fh *)filehandle;
2986
2987         if (fh->nf_vp == NULL) {
2988                 *status = NDIS_STATUS_FAILURE;
2989                 return;
2990         }
2991
2992         if (fh->nf_map != NULL) {
2993                 *status = NDIS_STATUS_ALREADY_MAPPED;
2994                 return;
2995         }
2996
2997         if (fh->nf_type == NDIS_FH_TYPE_MODULE) {
2998                 lf = fh->nf_vp;
2999                 if (ndis_find_sym(lf, fh->nf_name, "_start", &kldstart)) {
3000                         *status = NDIS_STATUS_FAILURE;
3001                         return;
3002                 }
3003                 fh->nf_map = kldstart;
3004                 *status = NDIS_STATUS_SUCCESS;
3005                 *mappedbuffer = fh->nf_map;
3006                 return;
3007         }
3008
3009         fh->nf_map = ExAllocatePoolWithTag(NonPagedPool, fh->nf_maplen, 0);
3010
3011         if (fh->nf_map == NULL) {
3012                 *status = NDIS_STATUS_RESOURCES;
3013                 return;
3014         }
3015
3016         vp = fh->nf_vp;
3017         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3018         error = vn_rdwr(UIO_READ, vp, fh->nf_map, fh->nf_maplen, 0,
3019             UIO_SYSSPACE, 0, td->td_ucred, NOCRED, &resid, td);
3020         VFS_UNLOCK_GIANT(vfslocked);
3021
3022         if (error)
3023                 *status = NDIS_STATUS_FAILURE;
3024         else {
3025                 *status = NDIS_STATUS_SUCCESS;
3026                 *mappedbuffer = fh->nf_map;
3027         }
3028
3029         return;
3030 }
3031
3032 static void
3033 NdisUnmapFile(filehandle)
3034         ndis_handle             filehandle;
3035 {
3036         ndis_fh                 *fh;
3037         fh = (ndis_fh *)filehandle;
3038
3039         if (fh->nf_map == NULL)
3040                 return;
3041
3042         if (fh->nf_type == NDIS_FH_TYPE_VFS)
3043                 ExFreePool(fh->nf_map);
3044         fh->nf_map = NULL;
3045
3046         return;
3047 }
3048
3049 static void
3050 NdisCloseFile(filehandle)
3051         ndis_handle             filehandle;
3052 {
3053         struct thread           *td = curthread;
3054         ndis_fh                 *fh;
3055         int                     vfslocked;
3056         struct vnode            *vp;
3057
3058         if (filehandle == NULL)
3059                 return;
3060
3061         fh = (ndis_fh *)filehandle;
3062         if (fh->nf_map != NULL) {
3063                 if (fh->nf_type == NDIS_FH_TYPE_VFS)
3064                         ExFreePool(fh->nf_map);
3065                 fh->nf_map = NULL;
3066         }
3067
3068         if (fh->nf_vp == NULL)
3069                 return;
3070
3071         if (fh->nf_type == NDIS_FH_TYPE_VFS) {
3072                 vp = fh->nf_vp;
3073                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3074                 vn_close(vp, FREAD, td->td_ucred, td);
3075                 VFS_UNLOCK_GIANT(vfslocked);
3076         }
3077
3078         fh->nf_vp = NULL;
3079         free(fh->nf_name, M_DEVBUF);
3080         ExFreePool(fh);
3081
3082         return;
3083 }
3084
3085 static uint8_t
3086 NdisSystemProcessorCount()
3087 {
3088         return(mp_ncpus);
3089 }
3090
3091 typedef void (*ndis_statusdone_handler)(ndis_handle);
3092 typedef void (*ndis_status_handler)(ndis_handle, ndis_status,
3093     void *, uint32_t);
3094
3095 static void
3096 NdisMIndicateStatusComplete(adapter)
3097         ndis_handle             adapter;
3098 {
3099         ndis_miniport_block     *block;
3100         ndis_statusdone_handler statusdonefunc;
3101
3102         block = (ndis_miniport_block *)adapter;
3103         statusdonefunc = block->nmb_statusdone_func;
3104
3105         MSCALL1(statusdonefunc, adapter);
3106         return;
3107 }
3108
3109 static void
3110 NdisMIndicateStatus(adapter, status, sbuf, slen)
3111         ndis_handle             adapter;
3112         ndis_status             status;
3113         void                    *sbuf;
3114         uint32_t                slen;
3115 {
3116         ndis_miniport_block     *block;
3117         ndis_status_handler     statusfunc;
3118
3119         block = (ndis_miniport_block *)adapter;
3120         statusfunc = block->nmb_status_func;
3121
3122         MSCALL4(statusfunc, adapter, status, sbuf, slen);
3123         return;
3124 }
3125
3126 /*
3127  * The DDK documentation says that you should use IoQueueWorkItem()
3128  * instead of ExQueueWorkItem(). The problem is, IoQueueWorkItem()
3129  * is fundamentally incompatible with NdisScheduleWorkItem(), which
3130  * depends on the API semantics of ExQueueWorkItem(). In our world,
3131  * ExQueueWorkItem() is implemented on top of IoAllocateQueueItem()
3132  * anyway.
3133  *
3134  * There are actually three distinct APIs here. NdisScheduleWorkItem()
3135  * takes a pointer to an NDIS_WORK_ITEM. ExQueueWorkItem() takes a pointer
3136  * to a WORK_QUEUE_ITEM. And finally, IoQueueWorkItem() takes a pointer
3137  * to an opaque work item thingie which you get from IoAllocateWorkItem().
3138  * An NDIS_WORK_ITEM is not the same as a WORK_QUEUE_ITEM. However,
3139  * the NDIS_WORK_ITEM has some opaque storage at the end of it, and we
3140  * (ab)use this storage as a WORK_QUEUE_ITEM, which is what we submit
3141  * to ExQueueWorkItem().
3142  *
3143  * Got all that? (Sheesh.)
3144  */
3145
3146 ndis_status
3147 NdisScheduleWorkItem(work)
3148         ndis_work_item          *work;
3149 {
3150         work_queue_item         *wqi;
3151
3152         wqi = (work_queue_item *)work->nwi_wraprsvd;
3153         ExInitializeWorkItem(wqi,
3154             (work_item_func)work->nwi_func, work->nwi_ctx);
3155         ExQueueWorkItem(wqi, WORKQUEUE_DELAYED);
3156
3157         return(NDIS_STATUS_SUCCESS);
3158 }
3159
3160 static void
3161 NdisCopyFromPacketToPacket(dpkt, doff, reqlen, spkt, soff, cpylen)
3162         ndis_packet             *dpkt;
3163         uint32_t                doff;
3164         uint32_t                reqlen;
3165         ndis_packet             *spkt;
3166         uint32_t                soff;
3167         uint32_t                *cpylen;
3168 {
3169         ndis_buffer             *src, *dst;
3170         char                    *sptr, *dptr;
3171         int                     resid, copied, len, scnt, dcnt;
3172
3173         *cpylen = 0;
3174
3175         src = spkt->np_private.npp_head;
3176         dst = dpkt->np_private.npp_head;
3177
3178         sptr = MmGetMdlVirtualAddress(src);
3179         dptr = MmGetMdlVirtualAddress(dst);
3180         scnt = MmGetMdlByteCount(src);
3181         dcnt = MmGetMdlByteCount(dst);
3182
3183         while (soff) {
3184                 if (MmGetMdlByteCount(src) > soff) {
3185                         sptr += soff;
3186                         scnt = MmGetMdlByteCount(src)- soff;
3187                         break;
3188                 }
3189                 soff -= MmGetMdlByteCount(src);
3190                 src = src->mdl_next;
3191                 if (src == NULL)
3192                         return;
3193                 sptr = MmGetMdlVirtualAddress(src);
3194         }
3195
3196         while (doff) {
3197                 if (MmGetMdlByteCount(dst) > doff) {
3198                         dptr += doff;
3199                         dcnt = MmGetMdlByteCount(dst) - doff;
3200                         break;
3201                 }
3202                 doff -= MmGetMdlByteCount(dst);
3203                 dst = dst->mdl_next;
3204                 if (dst == NULL)
3205                         return;
3206                 dptr = MmGetMdlVirtualAddress(dst);
3207         }
3208
3209         resid = reqlen;
3210         copied = 0;
3211
3212         while(1) {
3213                 if (resid < scnt)
3214                         len = resid;
3215                 else
3216                         len = scnt;
3217                 if (dcnt < len)
3218                         len = dcnt;
3219
3220                 bcopy(sptr, dptr, len);
3221
3222                 copied += len;
3223                 resid -= len;
3224                 if (resid == 0)
3225                         break;
3226
3227                 dcnt -= len;
3228                 if (dcnt == 0) {
3229                         dst = dst->mdl_next;
3230                         if (dst == NULL)
3231                                 break;
3232                         dptr = MmGetMdlVirtualAddress(dst);
3233                         dcnt = MmGetMdlByteCount(dst);
3234                 }
3235
3236                 scnt -= len;
3237                 if (scnt == 0) {
3238                         src = src->mdl_next;
3239                         if (src == NULL)
3240                                 break;
3241                         sptr = MmGetMdlVirtualAddress(src);
3242                         scnt = MmGetMdlByteCount(src);
3243                 }
3244         }
3245
3246         *cpylen = copied;
3247         return;
3248 }
3249
3250 static void
3251 NdisCopyFromPacketToPacketSafe(dpkt, doff, reqlen, spkt, soff, cpylen, prio)
3252         ndis_packet             *dpkt;
3253         uint32_t                doff;
3254         uint32_t                reqlen;
3255         ndis_packet             *spkt;
3256         uint32_t                soff;
3257         uint32_t                *cpylen;
3258         uint32_t                prio;
3259 {
3260         NdisCopyFromPacketToPacket(dpkt, doff, reqlen, spkt, soff, cpylen);
3261         return;
3262 }
3263
3264 static void
3265 NdisIMCopySendPerPacketInfo(dpkt, spkt)
3266         ndis_packet             *dpkt;
3267         ndis_packet             *spkt;
3268 {
3269         memcpy(&dpkt->np_ext, &spkt->np_ext, sizeof(ndis_packet_extension));
3270 }
3271
3272 static ndis_status
3273 NdisMRegisterDevice(handle, devname, symname, majorfuncs, devobj, devhandle)
3274         ndis_handle             handle;
3275         unicode_string          *devname;
3276         unicode_string          *symname;
3277         driver_dispatch         *majorfuncs[];
3278         void                    **devobj;
3279         ndis_handle             *devhandle;
3280 {
3281         uint32_t                status;
3282         device_object           *dobj;
3283
3284         status = IoCreateDevice(handle, 0, devname,
3285             FILE_DEVICE_UNKNOWN, 0, FALSE, &dobj);
3286
3287         if (status == STATUS_SUCCESS) {
3288                 *devobj = dobj;
3289                 *devhandle = dobj;
3290         }
3291
3292         return(status);
3293 }
3294
3295 static ndis_status
3296 NdisMDeregisterDevice(handle)
3297         ndis_handle             handle;
3298 {
3299         IoDeleteDevice(handle);
3300         return(NDIS_STATUS_SUCCESS);
3301 }
3302
3303 static ndis_status
3304 NdisMQueryAdapterInstanceName(name, handle)
3305         unicode_string          *name;
3306         ndis_handle             handle;
3307 {
3308         ndis_miniport_block     *block;
3309         device_t                dev;
3310         ansi_string             as;
3311
3312         block = (ndis_miniport_block *)handle;
3313         dev = block->nmb_physdeviceobj->do_devext;
3314
3315         RtlInitAnsiString(&as, __DECONST(char *, device_get_nameunit(dev)));
3316         if (RtlAnsiStringToUnicodeString(name, &as, TRUE))
3317                 return(NDIS_STATUS_RESOURCES);
3318
3319         return(NDIS_STATUS_SUCCESS);
3320 }
3321
3322 static void
3323 NdisMRegisterUnloadHandler(handle, func)
3324         ndis_handle             handle;
3325         void                    *func;
3326 {
3327         return;
3328 }
3329
3330 static void
3331 dummy()
3332 {
3333         printf ("NDIS dummy called...\n");
3334         return;
3335 }
3336
3337 /*
3338  * Note: a couple of entries in this table specify the
3339  * number of arguments as "foo + 1". These are routines
3340  * that accept a 64-bit argument, passed by value. On
3341  * x86, these arguments consume two longwords on the stack,
3342  * so we lie and say there's one additional argument so
3343  * that the wrapping routines will do the right thing.
3344  */
3345
3346 image_patch_table ndis_functbl[] = {
3347         IMPORT_SFUNC(NdisCopyFromPacketToPacket, 6),
3348         IMPORT_SFUNC(NdisCopyFromPacketToPacketSafe, 7),
3349         IMPORT_SFUNC(NdisIMCopySendPerPacketInfo, 2),
3350         IMPORT_SFUNC(NdisScheduleWorkItem, 1),
3351         IMPORT_SFUNC(NdisMIndicateStatusComplete, 1),
3352         IMPORT_SFUNC(NdisMIndicateStatus, 4),
3353         IMPORT_SFUNC(NdisSystemProcessorCount, 0),
3354         IMPORT_SFUNC(NdisUnchainBufferAtBack, 2),
3355         IMPORT_SFUNC(NdisGetFirstBufferFromPacket, 5),
3356         IMPORT_SFUNC(NdisGetFirstBufferFromPacketSafe, 6),
3357         IMPORT_SFUNC(NdisGetBufferPhysicalArraySize, 2),
3358         IMPORT_SFUNC(NdisMGetDeviceProperty, 6),
3359         IMPORT_SFUNC(NdisInitAnsiString, 2),
3360         IMPORT_SFUNC(NdisInitUnicodeString, 2),
3361         IMPORT_SFUNC(NdisWriteConfiguration, 4),
3362         IMPORT_SFUNC(NdisAnsiStringToUnicodeString, 2),
3363         IMPORT_SFUNC(NdisTerminateWrapper, 2),
3364         IMPORT_SFUNC(NdisOpenConfigurationKeyByName, 4),
3365         IMPORT_SFUNC(NdisOpenConfigurationKeyByIndex, 5),
3366         IMPORT_SFUNC(NdisMRemoveMiniport, 1),
3367         IMPORT_SFUNC(NdisInitializeString, 2),
3368         IMPORT_SFUNC(NdisFreeString, 1),
3369         IMPORT_SFUNC(NdisGetCurrentSystemTime, 1),
3370         IMPORT_SFUNC(NdisGetSystemUpTime, 1),
3371         IMPORT_SFUNC(NdisMSynchronizeWithInterrupt, 3),
3372         IMPORT_SFUNC(NdisMAllocateSharedMemoryAsync, 4),
3373         IMPORT_SFUNC(NdisInterlockedInsertHeadList, 3),
3374         IMPORT_SFUNC(NdisInterlockedInsertTailList, 3),
3375         IMPORT_SFUNC(NdisInterlockedRemoveHeadList, 2),
3376         IMPORT_SFUNC(NdisInitializeWrapper, 4),
3377         IMPORT_SFUNC(NdisMRegisterMiniport, 3),
3378         IMPORT_SFUNC(NdisAllocateMemoryWithTag, 3),
3379         IMPORT_SFUNC(NdisAllocateMemory, 4 + 1),
3380         IMPORT_SFUNC(NdisMSetAttributesEx, 5),
3381         IMPORT_SFUNC(NdisCloseConfiguration, 1),
3382         IMPORT_SFUNC(NdisReadConfiguration, 5),
3383         IMPORT_SFUNC(NdisOpenConfiguration, 3),
3384         IMPORT_SFUNC(NdisAcquireSpinLock, 1),
3385         IMPORT_SFUNC(NdisReleaseSpinLock, 1),
3386         IMPORT_SFUNC(NdisDprAcquireSpinLock, 1),
3387         IMPORT_SFUNC(NdisDprReleaseSpinLock, 1),
3388         IMPORT_SFUNC(NdisAllocateSpinLock, 1),
3389         IMPORT_SFUNC(NdisInitializeReadWriteLock, 1),
3390         IMPORT_SFUNC(NdisAcquireReadWriteLock, 3),
3391         IMPORT_SFUNC(NdisReleaseReadWriteLock, 2),
3392         IMPORT_SFUNC(NdisFreeSpinLock, 1),
3393         IMPORT_SFUNC(NdisFreeMemory, 3),
3394         IMPORT_SFUNC(NdisReadPciSlotInformation, 5),
3395         IMPORT_SFUNC(NdisWritePciSlotInformation, 5),
3396         IMPORT_SFUNC_MAP(NdisImmediateReadPciSlotInformation,
3397             NdisReadPciSlotInformation, 5),
3398         IMPORT_SFUNC_MAP(NdisImmediateWritePciSlotInformation,
3399             NdisWritePciSlotInformation, 5),
3400         IMPORT_CFUNC(NdisWriteErrorLogEntry, 0),
3401         IMPORT_SFUNC(NdisMStartBufferPhysicalMapping, 6),
3402         IMPORT_SFUNC(NdisMCompleteBufferPhysicalMapping, 3),
3403         IMPORT_SFUNC(NdisMInitializeTimer, 4),
3404         IMPORT_SFUNC(NdisInitializeTimer, 3),
3405         IMPORT_SFUNC(NdisSetTimer, 2),
3406         IMPORT_SFUNC(NdisMCancelTimer, 2),
3407         IMPORT_SFUNC_MAP(NdisCancelTimer, NdisMCancelTimer, 2),
3408         IMPORT_SFUNC(NdisMSetPeriodicTimer, 2),
3409         IMPORT_SFUNC(NdisMQueryAdapterResources, 4),
3410         IMPORT_SFUNC(NdisMRegisterIoPortRange, 4),
3411         IMPORT_SFUNC(NdisMDeregisterIoPortRange, 4),
3412         IMPORT_SFUNC(NdisReadNetworkAddress, 4),
3413         IMPORT_SFUNC(NdisQueryMapRegisterCount, 2),
3414         IMPORT_SFUNC(NdisMAllocateMapRegisters, 5),
3415         IMPORT_SFUNC(NdisMFreeMapRegisters, 1),
3416         IMPORT_SFUNC(NdisMAllocateSharedMemory, 5),
3417         IMPORT_SFUNC(NdisMMapIoSpace, 4 + 1),
3418         IMPORT_SFUNC(NdisMUnmapIoSpace, 3),
3419         IMPORT_SFUNC(NdisGetCacheFillSize, 0),
3420         IMPORT_SFUNC(NdisMGetDmaAlignment, 1),
3421         IMPORT_SFUNC(NdisMInitializeScatterGatherDma, 3),
3422         IMPORT_SFUNC(NdisAllocatePacketPool, 4),
3423         IMPORT_SFUNC(NdisAllocatePacketPoolEx, 5),
3424         IMPORT_SFUNC(NdisAllocatePacket, 3),
3425         IMPORT_SFUNC(NdisFreePacket, 1),
3426         IMPORT_SFUNC(NdisFreePacketPool, 1),
3427         IMPORT_SFUNC_MAP(NdisDprAllocatePacket, NdisAllocatePacket, 3),
3428         IMPORT_SFUNC_MAP(NdisDprFreePacket, NdisFreePacket, 1),
3429         IMPORT_SFUNC(NdisAllocateBufferPool, 3),
3430         IMPORT_SFUNC(NdisAllocateBuffer, 5),
3431         IMPORT_SFUNC(NdisQueryBuffer, 3),
3432         IMPORT_SFUNC(NdisQueryBufferSafe, 4),
3433         IMPORT_SFUNC(NdisBufferVirtualAddress, 1),
3434         IMPORT_SFUNC(NdisBufferVirtualAddressSafe, 2),
3435         IMPORT_SFUNC(NdisBufferLength, 1),
3436         IMPORT_SFUNC(NdisFreeBuffer, 1),
3437         IMPORT_SFUNC(NdisFreeBufferPool, 1),
3438         IMPORT_SFUNC(NdisInterlockedIncrement, 1),
3439         IMPORT_SFUNC(NdisInterlockedDecrement, 1),
3440         IMPORT_SFUNC(NdisInitializeEvent, 1),
3441         IMPORT_SFUNC(NdisSetEvent, 1),
3442         IMPORT_SFUNC(NdisResetEvent, 1),
3443         IMPORT_SFUNC(NdisWaitEvent, 2),
3444         IMPORT_SFUNC(NdisUnicodeStringToAnsiString, 2),
3445         IMPORT_SFUNC(NdisMPciAssignResources, 3),
3446         IMPORT_SFUNC(NdisMFreeSharedMemory, 5 + 1),
3447         IMPORT_SFUNC(NdisMRegisterInterrupt, 7),
3448         IMPORT_SFUNC(NdisMDeregisterInterrupt, 1),
3449         IMPORT_SFUNC(NdisMRegisterAdapterShutdownHandler, 3),
3450         IMPORT_SFUNC(NdisMDeregisterAdapterShutdownHandler, 1),
3451         IMPORT_SFUNC(NDIS_BUFFER_TO_SPAN_PAGES, 1),
3452         IMPORT_SFUNC(NdisQueryBufferOffset, 3),
3453         IMPORT_SFUNC(NdisAdjustBufferLength, 2),
3454         IMPORT_SFUNC(NdisPacketPoolUsage, 1),
3455         IMPORT_SFUNC(NdisMSleep, 1),
3456         IMPORT_SFUNC(NdisUnchainBufferAtFront, 2),
3457         IMPORT_SFUNC(NdisReadPcmciaAttributeMemory, 4),
3458         IMPORT_SFUNC(NdisWritePcmciaAttributeMemory, 4),
3459         IMPORT_SFUNC(NdisOpenFile, 5 + 1),
3460         IMPORT_SFUNC(NdisMapFile, 3),
3461         IMPORT_SFUNC(NdisUnmapFile, 1),
3462         IMPORT_SFUNC(NdisCloseFile, 1),
3463         IMPORT_SFUNC(NdisMRegisterDevice, 6),
3464         IMPORT_SFUNC(NdisMDeregisterDevice, 1),
3465         IMPORT_SFUNC(NdisMQueryAdapterInstanceName, 2),
3466         IMPORT_SFUNC(NdisMRegisterUnloadHandler, 2),
3467         IMPORT_SFUNC(ndis_timercall, 4),
3468         IMPORT_SFUNC(ndis_asyncmem_complete, 2),
3469         IMPORT_SFUNC(ndis_intr, 2),
3470         IMPORT_SFUNC(ndis_intrhand, 4),
3471
3472         /*
3473          * This last entry is a catch-all for any function we haven't
3474          * implemented yet. The PE import list patching routine will
3475          * use it for any function that doesn't have an explicit match
3476          * in this table.
3477          */
3478
3479         { NULL, (FUNC)dummy, NULL, 0, WINDRV_WRAP_STDCALL },
3480
3481         /* End of list. */
3482
3483         { NULL, NULL, NULL }
3484 };