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