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