initial commit
[freebsd-arm:freebsd-arm.git] / compat / ndis / subr_ndis.c
1 /*-
2  * Copyright (c) 2003
3  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 /*
37  * This file implements a translation layer between the BSD networking
38  * infrasturcture and Windows(R) NDIS network driver modules. A Windows
39  * NDIS driver calls into several functions in the NDIS.SYS Windows
40  * kernel module and exports a table of functions designed to be called
41  * by the NDIS subsystem. Using the PE loader, we can patch our own
42  * versions of the NDIS routines into a given Windows driver module and
43  * convince the driver that it is in fact running on Windows.
44  *
45  * We provide a table of all our implemented NDIS routines which is patched
46  * into the driver object code. All our exported routines must use the
47  * _stdcall calling convention, since that's what the Windows object code
48  * expects.
49  */
50
51
52 #include <sys/ctype.h>
53 #include <sys/param.h>
54 #include <sys/types.h>
55 #include <sys/errno.h>
56
57 #include <sys/callout.h>
58 #include <sys/kernel.h>
59 #include <sys/systm.h>
60 #include <sys/malloc.h>
61 #include <sys/lock.h>
62 #include <sys/mutex.h>
63 #include <sys/socket.h>
64 #include <sys/sysctl.h>
65 #include <sys/timespec.h>
66 #include <sys/smp.h>
67 #include <sys/queue.h>
68 #include <sys/proc.h>
69 #include <sys/filedesc.h>
70 #include <sys/namei.h>
71 #include <sys/fcntl.h>
72 #include <sys/vnode.h>
73 #include <sys/kthread.h>
74 #include <sys/linker.h>
75 #include <sys/mount.h>
76 #include <sys/sysproto.h>
77
78 #include <net/if.h>
79 #include <net/if_arp.h>
80 #include <net/ethernet.h>
81 #include <net/if_dl.h>
82 #include <net/if_media.h>
83
84 #include <machine/atomic.h>
85 #include <machine/bus.h>
86 #include <machine/resource.h>
87
88 #include <sys/bus.h>
89 #include <sys/rman.h>
90
91 #include <machine/stdarg.h>
92
93 #include <net80211/ieee80211_var.h>
94 #include <net80211/ieee80211_ioctl.h>
95
96 #include <dev/pci/pcireg.h>
97 #include <dev/pci/pcivar.h>
98 #include <dev/usb/usb.h>
99 #include <dev/usb/usbdi.h>
100
101 #include <compat/ndis/pe_var.h>
102 #include <compat/ndis/cfg_var.h>
103 #include <compat/ndis/resource_var.h>
104 #include <compat/ndis/ntoskrnl_var.h>
105 #include <compat/ndis/hal_var.h>
106 #include <compat/ndis/ndis_var.h>
107 #include <dev/if_ndis/if_ndisvar.h>
108
109 #include <vm/vm.h>
110 #include <vm/vm_param.h>
111 #include <vm/pmap.h>
112 #include <vm/uma.h>
113 #include <vm/vm_kern.h>
114 #include <vm/vm_map.h>
115
116 static char ndis_filepath[MAXPATHLEN];
117
118 SYSCTL_STRING(_hw, OID_AUTO, ndis_filepath, CTLFLAG_RW, ndis_filepath,
119     MAXPATHLEN, "Path used by NdisOpenFile() to search for files");
120
121 static void NdisInitializeWrapper(ndis_handle *,
122         driver_object *, void *, void *);
123 static ndis_status NdisMRegisterMiniport(ndis_handle,
124         ndis_miniport_characteristics *, int);
125 static ndis_status NdisAllocateMemoryWithTag(void **,
126         uint32_t, uint32_t);
127 static ndis_status NdisAllocateMemory(void **,
128         uint32_t, uint32_t, ndis_physaddr);
129 static void NdisFreeMemory(void *, uint32_t, uint32_t);
130 static ndis_status NdisMSetAttributesEx(ndis_handle, ndis_handle,
131         uint32_t, uint32_t, ndis_interface_type);
132 static void NdisOpenConfiguration(ndis_status *,
133         ndis_handle *, ndis_handle);
134 static void NdisOpenConfigurationKeyByIndex(ndis_status *,
135         ndis_handle, uint32_t, unicode_string *, ndis_handle *);
136 static void NdisOpenConfigurationKeyByName(ndis_status *,
137         ndis_handle, unicode_string *, ndis_handle *);
138 static ndis_status ndis_encode_parm(ndis_miniport_block *,
139         struct sysctl_oid *, ndis_parm_type, ndis_config_parm **);
140 static ndis_status ndis_decode_parm(ndis_miniport_block *,
141         ndis_config_parm *, char *);
142 static void NdisReadConfiguration(ndis_status *, ndis_config_parm **,
143         ndis_handle, unicode_string *, ndis_parm_type);
144 static void NdisWriteConfiguration(ndis_status *, ndis_handle,
145         unicode_string *, ndis_config_parm *);
146 static void NdisCloseConfiguration(ndis_handle);
147 static void NdisAllocateSpinLock(ndis_spin_lock *);
148 static void NdisFreeSpinLock(ndis_spin_lock *);
149 static void NdisAcquireSpinLock(ndis_spin_lock *);
150 static void NdisReleaseSpinLock(ndis_spin_lock *);
151 static void NdisDprAcquireSpinLock(ndis_spin_lock *);
152 static void NdisDprReleaseSpinLock(ndis_spin_lock *);
153 static void NdisInitializeReadWriteLock(ndis_rw_lock *);
154 static void NdisAcquireReadWriteLock(ndis_rw_lock *,
155         uint8_t, ndis_lock_state *);
156 static void NdisReleaseReadWriteLock(ndis_rw_lock *, ndis_lock_state *);
157 static uint32_t NdisReadPciSlotInformation(ndis_handle, uint32_t,
158         uint32_t, void *, uint32_t);
159 static uint32_t NdisWritePciSlotInformation(ndis_handle, uint32_t,
160         uint32_t, void *, uint32_t);
161 static void NdisWriteErrorLogEntry(ndis_handle, ndis_error_code, uint32_t, ...);
162 static void ndis_map_cb(void *, bus_dma_segment_t *, int, int);
163 static void NdisMStartBufferPhysicalMapping(ndis_handle,
164         ndis_buffer *, uint32_t, uint8_t, ndis_paddr_unit *, uint32_t *);
165 static void NdisMCompleteBufferPhysicalMapping(ndis_handle,
166         ndis_buffer *, uint32_t);
167 static void NdisMInitializeTimer(ndis_miniport_timer *, ndis_handle,
168         ndis_timer_function, void *);
169 static void NdisInitializeTimer(ndis_timer *,
170         ndis_timer_function, void *);
171 static void NdisSetTimer(ndis_timer *, uint32_t);
172 static void NdisMSetPeriodicTimer(ndis_miniport_timer *, uint32_t);
173 static void NdisMCancelTimer(ndis_timer *, uint8_t *);
174 static void ndis_timercall(kdpc *, ndis_miniport_timer *,
175         void *, void *);
176 static void NdisMQueryAdapterResources(ndis_status *, ndis_handle,
177         ndis_resource_list *, uint32_t *);
178 static ndis_status NdisMRegisterIoPortRange(void **,
179         ndis_handle, uint32_t, uint32_t);
180 static void NdisMDeregisterIoPortRange(ndis_handle,
181         uint32_t, uint32_t, void *);
182 static void NdisReadNetworkAddress(ndis_status *, void **,
183         uint32_t *, ndis_handle);
184 static ndis_status NdisQueryMapRegisterCount(uint32_t, uint32_t *);
185 static ndis_status NdisMAllocateMapRegisters(ndis_handle,
186         uint32_t, uint8_t, uint32_t, uint32_t);
187 static void NdisMFreeMapRegisters(ndis_handle);
188 static void ndis_mapshared_cb(void *, bus_dma_segment_t *, int, int);
189 static void NdisMAllocateSharedMemory(ndis_handle, uint32_t,
190         uint8_t, void **, ndis_physaddr *);
191 static void ndis_asyncmem_complete(device_object *, void *);
192 static ndis_status NdisMAllocateSharedMemoryAsync(ndis_handle,
193         uint32_t, uint8_t, void *);
194 static void NdisMFreeSharedMemory(ndis_handle, uint32_t,
195         uint8_t, void *, ndis_physaddr);
196 static ndis_status NdisMMapIoSpace(void **, ndis_handle,
197         ndis_physaddr, uint32_t);
198 static void NdisMUnmapIoSpace(ndis_handle, void *, uint32_t);
199 static uint32_t NdisGetCacheFillSize(void);
200 static uint32_t NdisMGetDmaAlignment(ndis_handle);
201 static ndis_status NdisMInitializeScatterGatherDma(ndis_handle,
202         uint8_t, uint32_t);
203 static void NdisUnchainBufferAtFront(ndis_packet *, ndis_buffer **);
204 static void NdisUnchainBufferAtBack(ndis_packet *, ndis_buffer **);
205 static void NdisAllocateBufferPool(ndis_status *,
206         ndis_handle *, uint32_t);
207 static void NdisFreeBufferPool(ndis_handle);
208 static void NdisAllocateBuffer(ndis_status *, ndis_buffer **,
209         ndis_handle, void *, uint32_t);
210 static void NdisFreeBuffer(ndis_buffer *);
211 static uint32_t NdisBufferLength(ndis_buffer *);
212 static void NdisQueryBuffer(ndis_buffer *, void **, uint32_t *);
213 static void NdisQueryBufferSafe(ndis_buffer *, void **,
214         uint32_t *, uint32_t);
215 static void *NdisBufferVirtualAddress(ndis_buffer *);
216 static void *NdisBufferVirtualAddressSafe(ndis_buffer *, uint32_t);
217 static void NdisAdjustBufferLength(ndis_buffer *, int);
218 static uint32_t NdisInterlockedIncrement(uint32_t *);
219 static uint32_t NdisInterlockedDecrement(uint32_t *);
220 static void NdisInitializeEvent(ndis_event *);
221 static void NdisSetEvent(ndis_event *);
222 static void NdisResetEvent(ndis_event *);
223 static uint8_t NdisWaitEvent(ndis_event *, uint32_t);
224 static ndis_status NdisUnicodeStringToAnsiString(ansi_string *,
225         unicode_string *);
226 static ndis_status
227         NdisAnsiStringToUnicodeString(unicode_string *, ansi_string *);
228 static ndis_status NdisMPciAssignResources(ndis_handle,
229         uint32_t, ndis_resource_list **);
230 static ndis_status NdisMRegisterInterrupt(ndis_miniport_interrupt *,
231         ndis_handle, uint32_t, uint32_t, uint8_t,
232         uint8_t, ndis_interrupt_mode);
233 static void NdisMDeregisterInterrupt(ndis_miniport_interrupt *);
234 static void NdisMRegisterAdapterShutdownHandler(ndis_handle, void *,
235         ndis_shutdown_handler);
236 static void NdisMDeregisterAdapterShutdownHandler(ndis_handle);
237 static uint32_t NDIS_BUFFER_TO_SPAN_PAGES(ndis_buffer *);
238 static void NdisGetBufferPhysicalArraySize(ndis_buffer *,
239         uint32_t *);
240 static void NdisQueryBufferOffset(ndis_buffer *,
241         uint32_t *, uint32_t *);
242 static uint32_t NdisReadPcmciaAttributeMemory(ndis_handle,
243         uint32_t, void *, uint32_t);
244 static uint32_t NdisWritePcmciaAttributeMemory(ndis_handle,
245         uint32_t, void *, uint32_t);
246 static list_entry *NdisInterlockedInsertHeadList(list_entry *,
247         list_entry *, ndis_spin_lock *);
248 static list_entry *NdisInterlockedRemoveHeadList(list_entry *,
249         ndis_spin_lock *);
250 static list_entry *NdisInterlockedInsertTailList(list_entry *,
251         list_entry *, ndis_spin_lock *);
252 static uint8_t
253         NdisMSynchronizeWithInterrupt(ndis_miniport_interrupt *,
254         void *, void *);
255 static void NdisGetCurrentSystemTime(uint64_t *);
256 static void NdisGetSystemUpTime(uint32_t *);
257 static void NdisInitializeString(unicode_string *, char *);
258 static void NdisInitAnsiString(ansi_string *, char *);
259 static void NdisInitUnicodeString(unicode_string *, uint16_t *);
260 static void NdisFreeString(unicode_string *);
261 static ndis_status NdisMRemoveMiniport(ndis_handle *);
262 static void NdisTerminateWrapper(ndis_handle, void *);
263 static void NdisMGetDeviceProperty(ndis_handle, device_object **,
264         device_object **, device_object **, cm_resource_list *,
265         cm_resource_list *);
266 static void NdisGetFirstBufferFromPacket(ndis_packet *,
267         ndis_buffer **, void **, uint32_t *, uint32_t *);
268 static void NdisGetFirstBufferFromPacketSafe(ndis_packet *,
269         ndis_buffer **, void **, uint32_t *, uint32_t *, uint32_t);
270 static int ndis_find_sym(linker_file_t, char *, char *, caddr_t *);
271 static void NdisOpenFile(ndis_status *, ndis_handle *, uint32_t *,
272         unicode_string *, ndis_physaddr);
273 static void NdisMapFile(ndis_status *, void **, ndis_handle);
274 static void NdisUnmapFile(ndis_handle);
275 static void NdisCloseFile(ndis_handle);
276 static uint8_t NdisSystemProcessorCount(void);
277 static void NdisMIndicateStatusComplete(ndis_handle);
278 static void NdisMIndicateStatus(ndis_handle, ndis_status,
279     void *, uint32_t);
280 static uint8_t ndis_intr(kinterrupt *, void *);
281 static void ndis_intrhand(kdpc *, ndis_miniport_interrupt *, void *, void *);
282 static funcptr ndis_findwrap(funcptr);
283 static void NdisCopyFromPacketToPacket(ndis_packet *,
284         uint32_t, uint32_t, ndis_packet *, uint32_t, uint32_t *);
285 static void NdisCopyFromPacketToPacketSafe(ndis_packet *,
286         uint32_t, uint32_t, ndis_packet *, uint32_t, uint32_t *, uint32_t);
287 static void NdisIMCopySendPerPacketInfo(ndis_packet *, ndis_packet *);
288 static ndis_status NdisMRegisterDevice(ndis_handle,
289         unicode_string *, unicode_string *, driver_dispatch **,
290         void **, ndis_handle *);
291 static ndis_status NdisMDeregisterDevice(ndis_handle);
292 static ndis_status
293         NdisMQueryAdapterInstanceName(unicode_string *, ndis_handle);
294 static void NdisMRegisterUnloadHandler(ndis_handle, void *);
295 static void dummy(void);
296
297 /*
298  * Some really old drivers do not properly check the return value
299  * from NdisAllocatePacket() and NdisAllocateBuffer() and will
300  * sometimes allocate few more buffers/packets that they originally
301  * requested when they created the pool. To prevent this from being
302  * a problem, we allocate a few extra buffers/packets beyond what
303  * the driver asks for. This #define controls how many.
304  */
305 #define NDIS_POOL_EXTRA         16
306
307 int
308 ndis_libinit()
309 {
310         image_patch_table       *patch;
311
312         strcpy(ndis_filepath, "/compat/ndis");
313
314         patch = ndis_functbl;
315         while (patch->ipt_func != NULL) {
316                 windrv_wrap((funcptr)patch->ipt_func,
317                     (funcptr *)&patch->ipt_wrap,
318                     patch->ipt_argcnt, patch->ipt_ftype);
319                 patch++;
320         }
321
322         return (0);
323 }
324
325 int
326 ndis_libfini()
327 {
328         image_patch_table       *patch;
329
330         patch = ndis_functbl;
331         while (patch->ipt_func != NULL) {
332                 windrv_unwrap(patch->ipt_wrap);
333                 patch++;
334         }
335
336         return (0);
337 }
338
339 static funcptr
340 ndis_findwrap(func)
341         funcptr                 func;
342 {
343         image_patch_table       *patch;
344
345         patch = ndis_functbl;
346         while (patch->ipt_func != NULL) {
347                 if ((funcptr)patch->ipt_func == func)
348                         return ((funcptr)patch->ipt_wrap);
349                 patch++;
350         }
351
352         return (NULL);
353 }
354
355 /*
356  * This routine does the messy Windows Driver Model device attachment
357  * stuff on behalf of NDIS drivers. We register our own AddDevice
358  * routine here
359  */
360 static void
361 NdisInitializeWrapper(wrapper, drv, path, unused)
362         ndis_handle             *wrapper;
363         driver_object           *drv;
364         void                    *path;
365         void                    *unused;
366 {
367         /*
368          * As of yet, I haven't come up with a compelling
369          * reason to define a private NDIS wrapper structure,
370          * so we use a pointer to the driver object as the
371          * wrapper handle. The driver object has the miniport
372          * characteristics struct for this driver hung off it
373          * via IoAllocateDriverObjectExtension(), and that's
374          * really all the private data we need.
375          */
376
377         *wrapper = drv;
378
379         /*
380          * If this was really Windows, we'd be registering dispatch
381          * routines for the NDIS miniport module here, but we're
382          * not Windows so all we really need to do is set up an
383          * AddDevice function that'll be invoked when a new device
384          * instance appears.
385          */
386
387         drv->dro_driverext->dre_adddevicefunc = NdisAddDevice;
388 }
389
390 static void
391 NdisTerminateWrapper(handle, syspec)
392         ndis_handle             handle;
393         void                    *syspec;
394 {
395         /* Nothing to see here, move along. */
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
484 static ndis_status
485 NdisMSetAttributesEx(adapter_handle, adapter_ctx, hangsecs,
486                         flags, iftype)
487         ndis_handle                     adapter_handle;
488         ndis_handle                     adapter_ctx;
489         uint32_t                        hangsecs;
490         uint32_t                        flags;
491         ndis_interface_type             iftype;
492 {
493         ndis_miniport_block             *block;
494
495         /*
496          * Save the adapter context, we need it for calling
497          * the driver's internal functions.
498          */
499         block = (ndis_miniport_block *)adapter_handle;
500         block->nmb_miniportadapterctx = adapter_ctx;
501         block->nmb_checkforhangsecs = hangsecs;
502         block->nmb_flags = flags;
503
504         return (NDIS_STATUS_SUCCESS);
505 }
506
507 static void
508 NdisOpenConfiguration(status, cfg, wrapctx)
509         ndis_status             *status;
510         ndis_handle             *cfg;
511         ndis_handle             wrapctx;
512 {
513         *cfg = wrapctx;
514         *status = NDIS_STATUS_SUCCESS;
515 }
516
517 static void
518 NdisOpenConfigurationKeyByName(status, cfg, subkey, subhandle)
519         ndis_status             *status;
520         ndis_handle             cfg;
521         unicode_string          *subkey;
522         ndis_handle             *subhandle;
523 {
524         *subhandle = cfg;
525         *status = NDIS_STATUS_SUCCESS;
526 }
527
528 static void
529 NdisOpenConfigurationKeyByIndex(status, cfg, idx, subkey, subhandle)
530         ndis_status             *status;
531         ndis_handle             cfg;
532         uint32_t                idx;
533         unicode_string          *subkey;
534         ndis_handle             *subhandle;
535 {
536         *status = NDIS_STATUS_FAILURE;
537 }
538
539 static ndis_status
540 ndis_encode_parm(block, oid, type, parm)
541         ndis_miniport_block     *block;
542         struct sysctl_oid       *oid;
543         ndis_parm_type          type;
544         ndis_config_parm        **parm;
545 {
546         ndis_config_parm        *p;
547         ndis_parmlist_entry     *np;
548         unicode_string          *us;
549         ansi_string             as;
550         int                     base = 0;
551         uint32_t                val;
552         char                    tmp[32];
553
554         np = ExAllocatePoolWithTag(NonPagedPool,
555             sizeof(ndis_parmlist_entry), 0);
556         if (np == NULL)
557                 return (NDIS_STATUS_RESOURCES);
558         InsertHeadList((&block->nmb_parmlist), (&np->np_list));
559         *parm = p = &np->np_parm;
560
561         switch(type) {
562         case ndis_parm_string:
563                 /* See if this might be a number. */
564                 val = strtoul((char *)oid->oid_arg1, NULL, 10);
565                 us = &p->ncp_parmdata.ncp_stringdata;
566                 p->ncp_type = ndis_parm_string;
567                 if (val) {
568                         snprintf(tmp, 32, "%x", val);
569                         RtlInitAnsiString(&as, tmp);
570                 } else {
571                         RtlInitAnsiString(&as, (char *)oid->oid_arg1);
572                 }
573
574                 if (RtlAnsiStringToUnicodeString(us, &as, TRUE)) {
575                         ExFreePool(np);
576                         return (NDIS_STATUS_RESOURCES);
577                 }
578                 break;
579         case ndis_parm_int:
580                 if (strncmp((char *)oid->oid_arg1, "0x", 2) == 0)
581                         base = 16;
582                 else
583                         base = 10;
584                 p->ncp_type = ndis_parm_int;
585                 p->ncp_parmdata.ncp_intdata =
586                     strtol((char *)oid->oid_arg1, NULL, base);
587                 break;
588         case ndis_parm_hexint:
589 #ifdef notdef
590                 if (strncmp((char *)oid->oid_arg1, "0x", 2) == 0)
591                         base = 16;
592                 else
593                         base = 10;
594 #endif
595                 base = 16;
596                 p->ncp_type = ndis_parm_hexint;
597                 p->ncp_parmdata.ncp_intdata =
598                     strtoul((char *)oid->oid_arg1, NULL, base);
599                 break;
600         default:
601                 return (NDIS_STATUS_FAILURE);
602                 break;
603         }
604
605         return (NDIS_STATUS_SUCCESS);
606 }
607
608 static void
609 NdisReadConfiguration(status, parm, cfg, key, type)
610         ndis_status             *status;
611         ndis_config_parm        **parm;
612         ndis_handle             cfg;
613         unicode_string          *key;
614         ndis_parm_type          type;
615 {
616         char                    *keystr = NULL;
617         ndis_miniport_block     *block;
618         struct ndis_softc       *sc;
619         struct sysctl_oid       *oidp;
620         struct sysctl_ctx_entry *e;
621         ansi_string             as;
622
623         block = (ndis_miniport_block *)cfg;
624         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
625
626         if (key->us_len == 0 || key->us_buf == NULL) {
627                 *status = NDIS_STATUS_FAILURE;
628                 return;
629         }
630
631         if (RtlUnicodeStringToAnsiString(&as, key, TRUE)) {
632                 *status = NDIS_STATUS_RESOURCES;
633                 return;
634         }
635
636         keystr = as.as_buf;
637
638         /*
639          * See if registry key is already in a list of known keys
640          * included with the driver.
641          */
642 #if __FreeBSD_version < 502113
643         TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
644 #else
645         TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
646 #endif
647                 oidp = e->entry;
648                 if (strcasecmp(oidp->oid_name, keystr) == 0) {
649                         if (strcmp((char *)oidp->oid_arg1, "UNSET") == 0) {
650                                 RtlFreeAnsiString(&as);
651                                 *status = NDIS_STATUS_FAILURE;
652                                 return;
653                         }
654
655                         *status = ndis_encode_parm(block, oidp, type, parm);
656                         RtlFreeAnsiString(&as);
657                         return;
658                 }
659         }
660
661         /*
662          * If the key didn't match, add it to the list of dynamically
663          * created ones. Sometimes, drivers refer to registry keys
664          * that aren't documented in their .INF files. These keys
665          * are supposed to be created by some sort of utility or
666          * control panel snap-in that comes with the driver software.
667          * Sometimes it's useful to be able to manipulate these.
668          * If the driver requests the key in the form of a string,
669          * make its default value an empty string, otherwise default
670          * it to "0".
671          */
672
673         if (type == ndis_parm_int || type == ndis_parm_hexint)
674                 ndis_add_sysctl(sc, keystr, "(dynamic integer key)",
675                     "UNSET", CTLFLAG_RW);
676         else
677                 ndis_add_sysctl(sc, keystr, "(dynamic string key)",
678                     "UNSET", CTLFLAG_RW);
679
680         RtlFreeAnsiString(&as);
681         *status = NDIS_STATUS_FAILURE;
682 }
683
684 static ndis_status
685 ndis_decode_parm(block, parm, val)
686         ndis_miniport_block     *block;
687         ndis_config_parm        *parm;
688         char                    *val;
689 {
690         unicode_string          *ustr;
691         ansi_string             as;
692
693         switch(parm->ncp_type) {
694         case ndis_parm_string:
695                 ustr = &parm->ncp_parmdata.ncp_stringdata;
696                 if (RtlUnicodeStringToAnsiString(&as, ustr, TRUE))
697                         return (NDIS_STATUS_RESOURCES);
698                 bcopy(as.as_buf, val, as.as_len);
699                 RtlFreeAnsiString(&as);
700                 break;
701         case ndis_parm_int:
702                 sprintf(val, "%d", parm->ncp_parmdata.ncp_intdata);
703                 break;
704         case ndis_parm_hexint:
705                 sprintf(val, "%xu", parm->ncp_parmdata.ncp_intdata);
706                 break;
707         default:
708                 return (NDIS_STATUS_FAILURE);
709                 break;
710         }
711         return (NDIS_STATUS_SUCCESS);
712 }
713
714 static void
715 NdisWriteConfiguration(status, cfg, key, parm)
716         ndis_status             *status;
717         ndis_handle             cfg;
718         unicode_string          *key;
719         ndis_config_parm        *parm;
720 {
721         ansi_string             as;
722         char                    *keystr = NULL;
723         ndis_miniport_block     *block;
724         struct ndis_softc       *sc;
725         struct sysctl_oid       *oidp;
726         struct sysctl_ctx_entry *e;
727         char                    val[256];
728
729         block = (ndis_miniport_block *)cfg;
730         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
731
732         if (RtlUnicodeStringToAnsiString(&as, key, TRUE)) {
733                 *status = NDIS_STATUS_RESOURCES;
734                 return;
735         }
736
737         keystr = as.as_buf;
738
739         /* Decode the parameter into a string. */
740         bzero(val, sizeof(val));
741         *status = ndis_decode_parm(block, parm, val);
742         if (*status != NDIS_STATUS_SUCCESS) {
743                 RtlFreeAnsiString(&as);
744                 return;
745         }
746
747         /* See if the key already exists. */
748
749 #if __FreeBSD_version < 502113
750         TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
751 #else
752         TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
753 #endif
754                 oidp = e->entry;
755                 if (strcasecmp(oidp->oid_name, keystr) == 0) {
756                         /* Found it, set the value. */
757                         strcpy((char *)oidp->oid_arg1, val);
758                         RtlFreeAnsiString(&as);
759                         return;
760                 }
761         }
762
763         /* Not found, add a new key with the specified value. */
764         ndis_add_sysctl(sc, keystr, "(dynamically set key)",
765                     val, CTLFLAG_RW);
766
767         RtlFreeAnsiString(&as);
768         *status = NDIS_STATUS_SUCCESS;
769 }
770
771 static void
772 NdisCloseConfiguration(cfg)
773         ndis_handle             cfg;
774 {
775         list_entry              *e;
776         ndis_parmlist_entry     *pe;
777         ndis_miniport_block     *block;
778         ndis_config_parm        *p;
779
780         block = (ndis_miniport_block *)cfg;
781
782         while (!IsListEmpty(&block->nmb_parmlist)) {
783                 e = RemoveHeadList(&block->nmb_parmlist);
784                 pe = CONTAINING_RECORD(e, ndis_parmlist_entry, np_list);
785                 p = &pe->np_parm;
786                 if (p->ncp_type == ndis_parm_string)
787                         RtlFreeUnicodeString(&p->ncp_parmdata.ncp_stringdata);
788                 ExFreePool(e);
789         }
790 }
791
792 /*
793  * Initialize a Windows spinlock.
794  */
795 static void
796 NdisAllocateSpinLock(lock)
797         ndis_spin_lock          *lock;
798 {
799         KeInitializeSpinLock(&lock->nsl_spinlock);
800         lock->nsl_kirql = 0;
801 }
802
803 /*
804  * Destroy a Windows spinlock. This is a no-op for now. There are two reasons
805  * for this. One is that it's sort of superfluous: we don't have to do anything
806  * special to deallocate the spinlock. The other is that there are some buggy
807  * drivers which call NdisFreeSpinLock() _after_ calling NdisFreeMemory() on
808  * the block of memory in which the spinlock resides. (Yes, ADMtek, I'm
809  * talking to you.)
810  */
811 static void
812 NdisFreeSpinLock(lock)
813         ndis_spin_lock          *lock;
814 {
815 #ifdef notdef
816         KeInitializeSpinLock(&lock->nsl_spinlock);
817         lock->nsl_kirql = 0;
818 #endif
819 }
820
821 /*
822  * Acquire a spinlock from IRQL <= DISPATCH_LEVEL.
823  */
824
825 static void
826 NdisAcquireSpinLock(lock)
827         ndis_spin_lock          *lock;
828 {
829         KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
830 }
831
832 /*
833  * Release a spinlock from IRQL == DISPATCH_LEVEL.
834  */
835
836 static void
837 NdisReleaseSpinLock(lock)
838         ndis_spin_lock          *lock;
839 {
840         KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
841 }
842
843 /*
844  * Acquire a spinlock when already running at IRQL == DISPATCH_LEVEL.
845  */
846 static void
847 NdisDprAcquireSpinLock(lock)
848         ndis_spin_lock          *lock;
849 {
850         KeAcquireSpinLockAtDpcLevel(&lock->nsl_spinlock);
851 }
852
853 /*
854  * Release a spinlock without leaving IRQL == DISPATCH_LEVEL.
855  */
856 static void
857 NdisDprReleaseSpinLock(lock)
858         ndis_spin_lock          *lock;
859 {
860         KeReleaseSpinLockFromDpcLevel(&lock->nsl_spinlock);
861 }
862
863 static void
864 NdisInitializeReadWriteLock(lock)
865         ndis_rw_lock            *lock;
866 {
867         KeInitializeSpinLock(&lock->nrl_spinlock);
868         bzero((char *)&lock->nrl_rsvd, sizeof(lock->nrl_rsvd));
869 }
870
871 static void
872 NdisAcquireReadWriteLock(ndis_rw_lock *lock, uint8_t writeacc,
873     ndis_lock_state *state)
874 {
875         if (writeacc == TRUE) {
876                 KeAcquireSpinLock(&lock->nrl_spinlock, &state->nls_oldirql);
877                 lock->nrl_rsvd[0]++;
878         } else
879                 lock->nrl_rsvd[1]++;
880 }
881
882 static void
883 NdisReleaseReadWriteLock(lock, state)
884         ndis_rw_lock            *lock;
885         ndis_lock_state         *state;
886 {
887         if (lock->nrl_rsvd[0]) {
888                 lock->nrl_rsvd[0]--;
889                 KeReleaseSpinLock(&lock->nrl_spinlock, state->nls_oldirql);
890         } else
891                 lock->nrl_rsvd[1]--;
892 }
893
894 static uint32_t
895 NdisReadPciSlotInformation(adapter, slot, offset, buf, len)
896         ndis_handle             adapter;
897         uint32_t                slot;
898         uint32_t                offset;
899         void                    *buf;
900         uint32_t                len;
901 {
902         ndis_miniport_block     *block;
903         int                     i;
904         char                    *dest;
905         device_t                dev;
906
907         block = (ndis_miniport_block *)adapter;
908         dest = buf;
909         if (block == NULL)
910                 return (0);
911
912         dev = block->nmb_physdeviceobj->do_devext;
913
914         /*
915          * I have a test system consisting of a Sun w2100z
916          * dual 2.4Ghz Opteron machine and an Atheros 802.11a/b/g
917          * "Aries" miniPCI NIC. (The NIC is installed in the
918          * machine using a miniPCI to PCI bus adapter card.)
919          * When running in SMP mode, I found that
920          * performing a large number of consecutive calls to
921          * NdisReadPciSlotInformation() would result in a
922          * sudden system reset (or in some cases a freeze).
923          * My suspicion is that the multiple reads are somehow
924          * triggering a fatal PCI bus error that leads to a
925          * machine check. The 1us delay in the loop below
926          * seems to prevent this problem.
927          */
928
929         for (i = 0; i < len; i++) {
930                 DELAY(1);
931                 dest[i] = pci_read_config(dev, i + offset, 1);
932         }
933
934         return (len);
935 }
936
937 static uint32_t
938 NdisWritePciSlotInformation(adapter, slot, offset, buf, len)
939         ndis_handle             adapter;
940         uint32_t                slot;
941         uint32_t                offset;
942         void                    *buf;
943         uint32_t                len;
944 {
945         ndis_miniport_block     *block;
946         int                     i;
947         char                    *dest;
948         device_t                dev;
949
950         block = (ndis_miniport_block *)adapter;
951         dest = buf;
952
953         if (block == NULL)
954                 return (0);
955
956         dev = block->nmb_physdeviceobj->do_devext;
957         for (i = 0; i < len; i++) {
958                 DELAY(1);
959                 pci_write_config(dev, i + offset, dest[i], 1);
960         }
961
962         return (len);
963 }
964
965 /*
966  * The errorlog routine uses a variable argument list, so we
967  * have to declare it this way.
968  */
969
970 #define ERRMSGLEN 512
971 static void
972 NdisWriteErrorLogEntry(ndis_handle adapter, ndis_error_code code,
973         uint32_t numerrors, ...)
974 {
975         ndis_miniport_block     *block;
976         va_list                 ap;
977         int                     i, error;
978         char                    *str = NULL;
979         uint16_t                flags;
980         device_t                dev;
981         driver_object           *drv;
982         struct ndis_softc       *sc;
983         struct ifnet            *ifp;
984         unicode_string          us;
985         ansi_string             as = { 0, 0, NULL };
986
987         block = (ndis_miniport_block *)adapter;
988         dev = block->nmb_physdeviceobj->do_devext;
989         drv = block->nmb_deviceobj->do_drvobj;
990         sc = device_get_softc(dev);
991         ifp = sc->ifp;
992
993         if (ifp != NULL && ifp->if_flags & IFF_DEBUG) {
994                 error = pe_get_message((vm_offset_t)drv->dro_driverstart,
995                     code, &str, &i, &flags);
996                 if (error == 0) {
997                         if (flags & MESSAGE_RESOURCE_UNICODE) {
998                                 RtlInitUnicodeString(&us, (uint16_t *)str);
999                                 if (RtlUnicodeStringToAnsiString(&as,
1000                                     &us, TRUE) == STATUS_SUCCESS)
1001                                         str = as.as_buf;
1002                                 else
1003                                         str = NULL;
1004                         }
1005                 }
1006         }
1007
1008         device_printf(dev, "NDIS ERROR: %x (%s)\n", code,
1009             str == NULL ? "unknown error" : str);
1010
1011         if (ifp != NULL && ifp->if_flags & IFF_DEBUG) {
1012                 device_printf(dev, "NDIS NUMERRORS: %x\n", numerrors);
1013                 va_start(ap, numerrors);
1014                 for (i = 0; i < numerrors; i++)
1015                         device_printf(dev, "argptr: %p\n",
1016                             va_arg(ap, void *));
1017                 va_end(ap);
1018         }
1019
1020         if (as.as_len)
1021                 RtlFreeAnsiString(&as);
1022 }
1023
1024 static void
1025 ndis_map_cb(arg, segs, nseg, error)
1026         void                    *arg;
1027         bus_dma_segment_t       *segs;
1028         int                     nseg;
1029         int                     error;
1030 {
1031         struct ndis_map_arg     *ctx;
1032         int                     i;
1033
1034         if (error)
1035                 return;
1036
1037         ctx = arg;
1038
1039         for (i = 0; i < nseg; i++) {
1040                 ctx->nma_fraglist[i].npu_physaddr.np_quad = segs[i].ds_addr;
1041                 ctx->nma_fraglist[i].npu_len = segs[i].ds_len;
1042         }
1043
1044         ctx->nma_cnt = nseg;
1045 }
1046
1047 static void
1048 NdisMStartBufferPhysicalMapping(ndis_handle adapter, ndis_buffer *buf,
1049     uint32_t mapreg, uint8_t writedev, ndis_paddr_unit *addrarray,
1050     uint32_t *arraysize)
1051 {
1052         ndis_miniport_block     *block;
1053         struct ndis_softc       *sc;
1054         struct ndis_map_arg     nma;
1055         bus_dmamap_t            map;
1056         int                     error;
1057
1058         if (adapter == NULL)
1059                 return;
1060
1061         block = (ndis_miniport_block *)adapter;
1062         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1063
1064         if (mapreg > sc->ndis_mmapcnt)
1065                 return;
1066
1067         map = sc->ndis_mmaps[mapreg];
1068         nma.nma_fraglist = addrarray;
1069
1070         error = bus_dmamap_load(sc->ndis_mtag, map,
1071             MmGetMdlVirtualAddress(buf), MmGetMdlByteCount(buf), ndis_map_cb,
1072             (void *)&nma, BUS_DMA_NOWAIT);
1073
1074         if (error)
1075                 return;
1076
1077         bus_dmamap_sync(sc->ndis_mtag, map,
1078             writedev ? BUS_DMASYNC_PREWRITE : BUS_DMASYNC_PREREAD);
1079
1080         *arraysize = nma.nma_cnt;
1081 }
1082
1083 static void
1084 NdisMCompleteBufferPhysicalMapping(adapter, buf, mapreg)
1085         ndis_handle             adapter;
1086         ndis_buffer             *buf;
1087         uint32_t                mapreg;
1088 {
1089         ndis_miniport_block     *block;
1090         struct ndis_softc       *sc;
1091         bus_dmamap_t            map;
1092
1093         if (adapter == NULL)
1094                 return;
1095
1096         block = (ndis_miniport_block *)adapter;
1097         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1098
1099         if (mapreg > sc->ndis_mmapcnt)
1100                 return;
1101
1102         map = sc->ndis_mmaps[mapreg];
1103
1104         bus_dmamap_sync(sc->ndis_mtag, map,
1105             BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1106
1107         bus_dmamap_unload(sc->ndis_mtag, map);
1108 }
1109
1110 /*
1111  * This is an older (?) timer init routine which doesn't
1112  * accept a miniport context handle. Serialized miniports should
1113  * never call this function.
1114  */
1115
1116 static void
1117 NdisInitializeTimer(timer, func, ctx)
1118         ndis_timer              *timer;
1119         ndis_timer_function     func;
1120         void                    *ctx;
1121 {
1122         KeInitializeTimer(&timer->nt_ktimer);
1123         KeInitializeDpc(&timer->nt_kdpc, func, ctx);
1124         KeSetImportanceDpc(&timer->nt_kdpc, KDPC_IMPORTANCE_LOW);
1125 }
1126
1127 static void
1128 ndis_timercall(dpc, timer, sysarg1, sysarg2)
1129         kdpc                    *dpc;
1130         ndis_miniport_timer     *timer;
1131         void                    *sysarg1;
1132         void                    *sysarg2;
1133 {
1134         /*
1135          * Since we're called as a DPC, we should be running
1136          * at DISPATCH_LEVEL here. This means to acquire the
1137          * spinlock, we can use KeAcquireSpinLockAtDpcLevel()
1138          * rather than KeAcquireSpinLock().
1139          */
1140         if (NDIS_SERIALIZED(timer->nmt_block))
1141                 KeAcquireSpinLockAtDpcLevel(&timer->nmt_block->nmb_lock);
1142
1143         MSCALL4(timer->nmt_timerfunc, dpc, timer->nmt_timerctx,
1144             sysarg1, sysarg2);
1145
1146         if (NDIS_SERIALIZED(timer->nmt_block))
1147                 KeReleaseSpinLockFromDpcLevel(&timer->nmt_block->nmb_lock);
1148 }
1149
1150 /*
1151  * For a long time I wondered why there were two NDIS timer initialization
1152  * routines, and why this one needed an NDIS_MINIPORT_TIMER and the
1153  * MiniportAdapterHandle. The NDIS_MINIPORT_TIMER has its own callout
1154  * function and context pointers separate from those in the DPC, which
1155  * allows for another level of indirection: when the timer fires, we
1156  * can have our own timer function invoked, and from there we can call
1157  * the driver's function. But why go to all that trouble? Then it hit
1158  * me: for serialized miniports, the timer callouts are not re-entrant.
1159  * By trapping the callouts and having access to the MiniportAdapterHandle,
1160  * we can protect the driver callouts by acquiring the NDIS serialization
1161  * lock. This is essential for allowing serialized miniports to work
1162  * correctly on SMP systems. On UP hosts, setting IRQL to DISPATCH_LEVEL
1163  * is enough to prevent other threads from pre-empting you, but with
1164  * SMP, you must acquire a lock as well, otherwise the other CPU is
1165  * free to clobber you.
1166  */
1167 static void
1168 NdisMInitializeTimer(timer, handle, func, ctx)
1169         ndis_miniport_timer     *timer;
1170         ndis_handle             handle;
1171         ndis_timer_function     func;
1172         void                    *ctx;
1173 {
1174         ndis_miniport_block     *block;
1175         struct ndis_softc       *sc;
1176
1177         block = (ndis_miniport_block *)handle;
1178         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1179
1180         /* Save the driver's funcptr and context */
1181
1182         timer->nmt_timerfunc = func;
1183         timer->nmt_timerctx = ctx;
1184         timer->nmt_block = handle;
1185
1186         /*
1187          * Set up the timer so it will call our intermediate DPC.
1188          * Be sure to use the wrapped entry point, since
1189          * ntoskrnl_run_dpc() expects to invoke a function with
1190          * Microsoft calling conventions.
1191          */
1192         KeInitializeTimer(&timer->nmt_ktimer);
1193         KeInitializeDpc(&timer->nmt_kdpc,
1194             ndis_findwrap((funcptr)ndis_timercall), timer);
1195         timer->nmt_ktimer.k_dpc = &timer->nmt_kdpc;
1196 }
1197
1198 /*
1199  * In Windows, there's both an NdisMSetTimer() and an NdisSetTimer(),
1200  * but the former is just a macro wrapper around the latter.
1201  */
1202 static void
1203 NdisSetTimer(timer, msecs)
1204         ndis_timer              *timer;
1205         uint32_t                msecs;
1206 {
1207         /*
1208          * KeSetTimer() wants the period in
1209          * hundred nanosecond intervals.
1210          */
1211         KeSetTimer(&timer->nt_ktimer,
1212             ((int64_t)msecs * -10000), &timer->nt_kdpc);
1213 }
1214
1215 static void
1216 NdisMSetPeriodicTimer(timer, msecs)
1217         ndis_miniport_timer     *timer;
1218         uint32_t                msecs;
1219 {
1220         KeSetTimerEx(&timer->nmt_ktimer,
1221             ((int64_t)msecs * -10000), msecs, &timer->nmt_kdpc);
1222 }
1223
1224 /*
1225  * Technically, this is really NdisCancelTimer(), but we also
1226  * (ab)use it for NdisMCancelTimer(), since in our implementation
1227  * we don't need the extra info in the ndis_miniport_timer
1228  * structure just to cancel a timer.
1229  */
1230
1231 static void
1232 NdisMCancelTimer(timer, cancelled)
1233         ndis_timer              *timer;
1234         uint8_t                 *cancelled;
1235 {
1236
1237         *cancelled = KeCancelTimer(&timer->nt_ktimer);
1238 }
1239
1240 static void
1241 NdisMQueryAdapterResources(status, adapter, list, buflen)
1242         ndis_status             *status;
1243         ndis_handle             adapter;
1244         ndis_resource_list      *list;
1245         uint32_t                *buflen;
1246 {
1247         ndis_miniport_block     *block;
1248         struct ndis_softc       *sc;
1249         int                     rsclen;
1250
1251         block = (ndis_miniport_block *)adapter;
1252         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1253
1254         rsclen = sizeof(ndis_resource_list) +
1255             (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1));
1256         if (*buflen < rsclen) {
1257                 *buflen = rsclen;
1258                 *status = NDIS_STATUS_INVALID_LENGTH;
1259                 return;
1260         }
1261
1262         bcopy((char *)block->nmb_rlist, (char *)list, rsclen);
1263         *status = NDIS_STATUS_SUCCESS;
1264 }
1265
1266 static ndis_status
1267 NdisMRegisterIoPortRange(offset, adapter, port, numports)
1268         void                    **offset;
1269         ndis_handle             adapter;
1270         uint32_t                port;
1271         uint32_t                numports;
1272 {
1273         struct ndis_miniport_block      *block;
1274         struct ndis_softc       *sc;
1275
1276         if (adapter == NULL)
1277                 return (NDIS_STATUS_FAILURE);
1278
1279         block = (ndis_miniport_block *)adapter;
1280         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1281
1282         if (sc->ndis_res_io == NULL)
1283                 return (NDIS_STATUS_FAILURE);
1284
1285         /* Don't let the device map more ports than we have. */
1286         if (rman_get_size(sc->ndis_res_io) < numports)
1287                 return (NDIS_STATUS_INVALID_LENGTH);
1288
1289         *offset = (void *)rman_get_start(sc->ndis_res_io);
1290
1291         return (NDIS_STATUS_SUCCESS);
1292 }
1293
1294 static void
1295 NdisMDeregisterIoPortRange(adapter, port, numports, offset)
1296         ndis_handle             adapter;
1297         uint32_t                port;
1298         uint32_t                numports;
1299         void                    *offset;
1300 {
1301 }
1302
1303 static void
1304 NdisReadNetworkAddress(status, addr, addrlen, adapter)
1305         ndis_status             *status;
1306         void                    **addr;
1307         uint32_t                *addrlen;
1308         ndis_handle             adapter;
1309 {
1310         struct ndis_softc       *sc;
1311         ndis_miniport_block     *block;
1312         uint8_t                 empty[] = { 0, 0, 0, 0, 0, 0 };
1313
1314         block = (ndis_miniport_block *)adapter;
1315         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1316         if (sc->ifp == NULL) {
1317                 *status = NDIS_STATUS_FAILURE;
1318                 return;
1319         }
1320
1321 #ifdef IFP2ENADDR
1322         if (bcmp(IFP2ENADDR(sc->ifp), empty, ETHER_ADDR_LEN) == 0)
1323 #elif __FreeBSD_version >= 700000
1324         if (sc->ifp->if_addr == NULL ||
1325             bcmp(IF_LLADDR(sc->ifp), empty, ETHER_ADDR_LEN) == 0)
1326 #else
1327         if (bcmp(sc->arpcom.ac_enaddr, empty, ETHER_ADDR_LEN) == 0)
1328 #endif
1329                 *status = NDIS_STATUS_FAILURE;
1330         else {
1331 #ifdef IFP2ENADDR
1332                 *addr = IFP2ENADDR(sc->ifp);
1333 #elif __FreeBSD_version >= 700000
1334                 *addr = IF_LLADDR(sc->ifp);
1335 #else
1336                 *addr = sc->arpcom.ac_enaddr;
1337 #endif
1338                 *addrlen = ETHER_ADDR_LEN;
1339                 *status = NDIS_STATUS_SUCCESS;
1340         }
1341 }
1342
1343 static ndis_status
1344 NdisQueryMapRegisterCount(bustype, cnt)
1345         uint32_t                bustype;
1346         uint32_t                *cnt;
1347 {
1348         *cnt = 8192;
1349         return (NDIS_STATUS_SUCCESS);
1350 }
1351
1352 static ndis_status
1353 NdisMAllocateMapRegisters(ndis_handle adapter, uint32_t dmachannel,
1354     uint8_t dmasize, uint32_t physmapneeded, uint32_t maxmap)
1355 {
1356         struct ndis_softc       *sc;
1357         ndis_miniport_block     *block;
1358         int                     error, i, nseg = NDIS_MAXSEG;
1359
1360         block = (ndis_miniport_block *)adapter;
1361         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1362
1363         sc->ndis_mmaps = malloc(sizeof(bus_dmamap_t) * physmapneeded,
1364             M_DEVBUF, M_NOWAIT|M_ZERO);
1365
1366         if (sc->ndis_mmaps == NULL)
1367                 return (NDIS_STATUS_RESOURCES);
1368
1369         error = bus_dma_tag_create(sc->ndis_parent_tag, ETHER_ALIGN, 0,
1370             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1371             NULL, maxmap * nseg, nseg, maxmap, BUS_DMA_ALLOCNOW,
1372             NULL, NULL, &sc->ndis_mtag);
1373
1374         if (error) {
1375                 free(sc->ndis_mmaps, M_DEVBUF);
1376                 return (NDIS_STATUS_RESOURCES);
1377         }
1378
1379         for (i = 0; i < physmapneeded; i++)
1380                 bus_dmamap_create(sc->ndis_mtag, 0, &sc->ndis_mmaps[i]);
1381
1382         sc->ndis_mmapcnt = physmapneeded;
1383
1384         return (NDIS_STATUS_SUCCESS);
1385 }
1386
1387 static void
1388 NdisMFreeMapRegisters(adapter)
1389         ndis_handle             adapter;
1390 {
1391         struct ndis_softc       *sc;
1392         ndis_miniport_block     *block;
1393         int                     i;
1394
1395         block = (ndis_miniport_block *)adapter;
1396         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1397
1398         for (i = 0; i < sc->ndis_mmapcnt; i++)
1399                 bus_dmamap_destroy(sc->ndis_mtag, sc->ndis_mmaps[i]);
1400
1401         free(sc->ndis_mmaps, M_DEVBUF);
1402
1403         bus_dma_tag_destroy(sc->ndis_mtag);
1404 }
1405
1406 static void
1407 ndis_mapshared_cb(arg, segs, nseg, error)
1408         void                    *arg;
1409         bus_dma_segment_t       *segs;
1410         int                     nseg;
1411         int                     error;
1412 {
1413         ndis_physaddr           *p;
1414
1415         if (error || nseg > 1)
1416                 return;
1417
1418         p = arg;
1419
1420         p->np_quad = segs[0].ds_addr;
1421 }
1422
1423 /*
1424  * This maps to bus_dmamem_alloc().
1425  */
1426
1427 static void
1428 NdisMAllocateSharedMemory(ndis_handle adapter, uint32_t len, uint8_t cached,
1429     void **vaddr, ndis_physaddr *paddr)
1430 {
1431         ndis_miniport_block     *block;
1432         struct ndis_softc       *sc;
1433         struct ndis_shmem       *sh;
1434         int                     error;
1435
1436         if (adapter == NULL)
1437                 return;
1438
1439         block = (ndis_miniport_block *)adapter;
1440         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1441
1442         sh = malloc(sizeof(struct ndis_shmem), M_DEVBUF, M_NOWAIT|M_ZERO);
1443         if (sh == NULL)
1444                 return;
1445
1446         InitializeListHead(&sh->ndis_list);
1447
1448         /*
1449          * When performing shared memory allocations, create a tag
1450          * with a lowaddr limit that restricts physical memory mappings
1451          * so that they all fall within the first 1GB of memory.
1452          * At least one device/driver combination (Linksys Instant
1453          * Wireless PCI Card V2.7, Broadcom 802.11b) seems to have
1454          * problems with performing DMA operations with physical
1455          * addresses that lie above the 1GB mark. I don't know if this
1456          * is a hardware limitation or if the addresses are being
1457          * truncated within the driver, but this seems to be the only
1458          * way to make these cards work reliably in systems with more
1459          * than 1GB of physical memory.
1460          */
1461
1462         error = bus_dma_tag_create(sc->ndis_parent_tag, 64,
1463             0, NDIS_BUS_SPACE_SHARED_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1464             NULL, len, 1, len, BUS_DMA_ALLOCNOW, NULL, NULL,
1465             &sh->ndis_stag);
1466
1467         if (error) {
1468                 free(sh, M_DEVBUF);
1469                 return;
1470         }
1471
1472         error = bus_dmamem_alloc(sh->ndis_stag, vaddr,
1473             BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sh->ndis_smap);
1474
1475         if (error) {
1476                 bus_dma_tag_destroy(sh->ndis_stag);
1477                 free(sh, M_DEVBUF);
1478                 return;
1479         }
1480
1481         error = bus_dmamap_load(sh->ndis_stag, sh->ndis_smap, *vaddr,
1482             len, ndis_mapshared_cb, (void *)paddr, BUS_DMA_NOWAIT);
1483
1484         if (error) {
1485                 bus_dmamem_free(sh->ndis_stag, *vaddr, sh->ndis_smap);
1486                 bus_dma_tag_destroy(sh->ndis_stag);
1487                 free(sh, M_DEVBUF);
1488                 return;
1489         }
1490
1491         /*
1492          * Save the physical address along with the source address.
1493          * The AirGo MIMO driver will call NdisMFreeSharedMemory()
1494          * with a bogus virtual address sometimes, but with a valid
1495          * physical address. To keep this from causing trouble, we
1496          * use the physical address to as a sanity check in case
1497          * searching based on the virtual address fails.
1498          */
1499
1500         NDIS_LOCK(sc);
1501         sh->ndis_paddr.np_quad = paddr->np_quad;
1502         sh->ndis_saddr = *vaddr;
1503         InsertHeadList((&sc->ndis_shlist), (&sh->ndis_list));
1504         NDIS_UNLOCK(sc);
1505 }
1506
1507 struct ndis_allocwork {
1508         uint32_t                na_len;
1509         uint8_t                 na_cached;
1510         void                    *na_ctx;
1511         io_workitem             *na_iw;
1512 };
1513
1514 static void
1515 ndis_asyncmem_complete(dobj, arg)
1516         device_object           *dobj;
1517         void                    *arg;
1518 {
1519         ndis_miniport_block     *block;
1520         struct ndis_softc       *sc;
1521         struct ndis_allocwork   *w;
1522         void                    *vaddr;
1523         ndis_physaddr           paddr;
1524         ndis_allocdone_handler  donefunc;
1525
1526         w = arg;
1527         block = (ndis_miniport_block *)dobj->do_devext;
1528         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1529
1530         vaddr = NULL;
1531         paddr.np_quad = 0;
1532
1533         donefunc = sc->ndis_chars->nmc_allocate_complete_func;
1534         NdisMAllocateSharedMemory(block, w->na_len,
1535             w->na_cached, &vaddr, &paddr);
1536         MSCALL5(donefunc, block, vaddr, &paddr, w->na_len, w->na_ctx);
1537
1538         IoFreeWorkItem(w->na_iw);
1539         free(w, M_DEVBUF);
1540 }
1541
1542 static ndis_status
1543 NdisMAllocateSharedMemoryAsync(ndis_handle adapter, uint32_t len,
1544     uint8_t cached, void *ctx)
1545 {
1546         ndis_miniport_block     *block;
1547         struct ndis_allocwork   *w;
1548         io_workitem             *iw;
1549         io_workitem_func        ifw;
1550
1551         if (adapter == NULL)
1552                 return (NDIS_STATUS_FAILURE);
1553
1554         block = adapter;
1555
1556         iw = IoAllocateWorkItem(block->nmb_deviceobj);
1557         if (iw == NULL)
1558                 return (NDIS_STATUS_FAILURE);
1559
1560         w = malloc(sizeof(struct ndis_allocwork), M_TEMP, M_NOWAIT);
1561
1562         if (w == NULL)
1563                 return (NDIS_STATUS_FAILURE);
1564
1565         w->na_cached = cached;
1566         w->na_len = len;
1567         w->na_ctx = ctx;
1568         w->na_iw = iw;
1569
1570         ifw = (io_workitem_func)ndis_findwrap((funcptr)ndis_asyncmem_complete);
1571         IoQueueWorkItem(iw, ifw, WORKQUEUE_DELAYED, w);
1572
1573         return (NDIS_STATUS_PENDING);
1574 }
1575
1576 static void
1577 NdisMFreeSharedMemory(ndis_handle adapter, uint32_t len, uint8_t cached,
1578     void *vaddr, ndis_physaddr paddr)
1579 {
1580         ndis_miniport_block     *block;
1581         struct ndis_softc       *sc;
1582         struct ndis_shmem       *sh = NULL;
1583         list_entry              *l;
1584
1585         if (vaddr == NULL || adapter == NULL)
1586                 return;
1587
1588         block = (ndis_miniport_block *)adapter;
1589         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1590
1591         /* Sanity check: is list empty? */
1592
1593         if (IsListEmpty(&sc->ndis_shlist))
1594                 return;
1595
1596         NDIS_LOCK(sc);
1597         l = sc->ndis_shlist.nle_flink;
1598         while (l != &sc->ndis_shlist) {
1599                 sh = CONTAINING_RECORD(l, struct ndis_shmem, ndis_list);
1600                 if (sh->ndis_saddr == vaddr)
1601                         break;
1602                 /*
1603                  * Check the physaddr too, just in case the driver lied
1604                  * about the virtual address.
1605                  */
1606                 if (sh->ndis_paddr.np_quad == paddr.np_quad)
1607                         break;
1608                 l = l->nle_flink;
1609         }
1610
1611         if (sh == NULL) {
1612                 NDIS_UNLOCK(sc);
1613                 printf("NDIS: buggy driver tried to free "
1614                     "invalid shared memory: vaddr: %p paddr: 0x%jx\n",
1615                     vaddr, (uintmax_t)paddr.np_quad);
1616                 return;
1617         }
1618
1619         RemoveEntryList(&sh->ndis_list);
1620
1621         NDIS_UNLOCK(sc);
1622
1623         bus_dmamap_unload(sh->ndis_stag, sh->ndis_smap);
1624         bus_dmamem_free(sh->ndis_stag, sh->ndis_saddr, sh->ndis_smap);
1625         bus_dma_tag_destroy(sh->ndis_stag);
1626
1627         free(sh, M_DEVBUF);
1628 }
1629
1630 static ndis_status
1631 NdisMMapIoSpace(vaddr, adapter, paddr, len)
1632         void                    **vaddr;
1633         ndis_handle             adapter;
1634         ndis_physaddr           paddr;
1635         uint32_t                len;
1636 {
1637         if (adapter == NULL)
1638                 return (NDIS_STATUS_FAILURE);
1639
1640         *vaddr = MmMapIoSpace(paddr.np_quad, len, 0);
1641
1642         if (*vaddr == NULL)
1643                 return (NDIS_STATUS_FAILURE);
1644
1645         return (NDIS_STATUS_SUCCESS);
1646 }
1647
1648 static void
1649 NdisMUnmapIoSpace(adapter, vaddr, len)
1650         ndis_handle             adapter;
1651         void                    *vaddr;
1652         uint32_t                len;
1653 {
1654         MmUnmapIoSpace(vaddr, len);
1655 }
1656
1657 static uint32_t
1658 NdisGetCacheFillSize(void)
1659 {
1660         return (128);
1661 }
1662
1663 static uint32_t
1664 NdisMGetDmaAlignment(handle)
1665         ndis_handle             handle;
1666 {
1667         return (16);
1668 }
1669
1670 /*
1671  * NDIS has two methods for dealing with NICs that support DMA.
1672  * One is to just pass packets to the driver and let it call
1673  * NdisMStartBufferPhysicalMapping() to map each buffer in the packet
1674  * all by itself, and the other is to let the NDIS library handle the
1675  * buffer mapping internally, and hand the driver an already populated
1676  * scatter/gather fragment list. If the driver calls
1677  * NdisMInitializeScatterGatherDma(), it wants to use the latter
1678  * method.
1679  */
1680
1681 static ndis_status
1682 NdisMInitializeScatterGatherDma(ndis_handle adapter, uint8_t is64,
1683     uint32_t maxphysmap)
1684 {
1685         struct ndis_softc       *sc;
1686         ndis_miniport_block     *block;
1687         int                     error;
1688
1689         if (adapter == NULL)
1690                 return (NDIS_STATUS_FAILURE);
1691         block = (ndis_miniport_block *)adapter;
1692         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1693
1694         /* Don't do this twice. */
1695         if (sc->ndis_sc == 1)
1696                 return (NDIS_STATUS_SUCCESS);
1697
1698         error = bus_dma_tag_create(sc->ndis_parent_tag, ETHER_ALIGN, 0,
1699             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1700             MCLBYTES * NDIS_MAXSEG, NDIS_MAXSEG, MCLBYTES, BUS_DMA_ALLOCNOW,
1701             NULL, NULL, &sc->ndis_ttag);
1702
1703         sc->ndis_sc = 1;
1704
1705         return (NDIS_STATUS_SUCCESS);
1706 }
1707
1708 void
1709 NdisAllocatePacketPool(status, pool, descnum, protrsvdlen)
1710         ndis_status             *status;
1711         ndis_handle             *pool;
1712         uint32_t                descnum;
1713         uint32_t                protrsvdlen;
1714 {
1715         ndis_packet_pool        *p;
1716         ndis_packet             *packets;
1717         int                     i;
1718
1719         p = ExAllocatePoolWithTag(NonPagedPool, sizeof(ndis_packet_pool), 0);
1720         if (p == NULL) {
1721                 *status = NDIS_STATUS_RESOURCES;
1722                 return;
1723         }
1724
1725         p->np_cnt = descnum + NDIS_POOL_EXTRA;
1726         p->np_protrsvd = protrsvdlen;
1727         p->np_len = sizeof(ndis_packet) + protrsvdlen;
1728
1729         packets = ExAllocatePoolWithTag(NonPagedPool, p->np_cnt *
1730             p->np_len, 0);
1731
1732
1733         if (packets == NULL) {
1734                 ExFreePool(p);
1735                 *status = NDIS_STATUS_RESOURCES;
1736                 return;
1737         }
1738
1739         p->np_pktmem = packets;
1740
1741         for (i = 0; i < p->np_cnt; i++)
1742                 InterlockedPushEntrySList(&p->np_head,
1743                     (struct slist_entry *)&packets[i]);
1744
1745 #ifdef NDIS_DEBUG_PACKETS 
1746         p->np_dead = 0; 
1747         KeInitializeSpinLock(&p->np_lock);
1748         KeInitializeEvent(&p->np_event, EVENT_TYPE_NOTIFY, TRUE);
1749 #endif
1750
1751         *pool = p; 
1752         *status = NDIS_STATUS_SUCCESS;
1753 }
1754
1755 void
1756 NdisAllocatePacketPoolEx(status, pool, descnum, oflowdescnum, protrsvdlen)
1757         ndis_status             *status;
1758         ndis_handle             *pool;
1759         uint32_t                descnum;
1760         uint32_t                oflowdescnum;
1761         uint32_t                protrsvdlen;
1762 {
1763         return (NdisAllocatePacketPool(status, pool,
1764             descnum + oflowdescnum, protrsvdlen));
1765 }
1766
1767 uint32_t
1768 NdisPacketPoolUsage(pool)
1769         ndis_handle             pool;
1770 {
1771         ndis_packet_pool        *p;
1772
1773         p = (ndis_packet_pool *)pool;
1774         return (p->np_cnt - ExQueryDepthSList(&p->np_head));
1775 }
1776
1777 void
1778 NdisFreePacketPool(pool)
1779         ndis_handle             pool;
1780 {
1781         ndis_packet_pool        *p;
1782         int                     usage;
1783 #ifdef NDIS_DEBUG_PACKETS
1784         uint8_t                 irql;
1785 #endif
1786
1787         p = (ndis_packet_pool *)pool;
1788
1789 #ifdef NDIS_DEBUG_PACKETS
1790         KeAcquireSpinLock(&p->np_lock, &irql);
1791 #endif
1792
1793         usage = NdisPacketPoolUsage(pool);
1794
1795 #ifdef NDIS_DEBUG_PACKETS
1796         if (usage) {
1797                 p->np_dead = 1;
1798                 KeResetEvent(&p->np_event);
1799                 KeReleaseSpinLock(&p->np_lock, irql);
1800                 KeWaitForSingleObject(&p->np_event, 0, 0, FALSE, NULL);
1801         } else
1802                 KeReleaseSpinLock(&p->np_lock, irql);
1803 #endif
1804
1805         ExFreePool(p->np_pktmem);
1806         ExFreePool(p);
1807 }
1808
1809 void
1810 NdisAllocatePacket(status, packet, pool)
1811         ndis_status             *status;
1812         ndis_packet             **packet;
1813         ndis_handle             pool;
1814 {
1815         ndis_packet_pool        *p;
1816         ndis_packet             *pkt;
1817 #ifdef NDIS_DEBUG_PACKETS
1818         uint8_t                 irql;
1819 #endif
1820
1821         p = (ndis_packet_pool *)pool;
1822
1823 #ifdef NDIS_DEBUG_PACKETS
1824         KeAcquireSpinLock(&p->np_lock, &irql);
1825         if (p->np_dead) {
1826                 KeReleaseSpinLock(&p->np_lock, irql);
1827                 printf("NDIS: tried to allocate packet from dead pool %p\n",
1828                     pool);
1829                 *status = NDIS_STATUS_RESOURCES;
1830                 return;
1831         }
1832 #endif
1833
1834         pkt = (ndis_packet *)InterlockedPopEntrySList(&p->np_head);
1835
1836 #ifdef NDIS_DEBUG_PACKETS
1837         KeReleaseSpinLock(&p->np_lock, irql);
1838 #endif
1839
1840         if (pkt == NULL) {
1841                 *status = NDIS_STATUS_RESOURCES;
1842                 return;
1843         }
1844
1845
1846         bzero((char *)pkt, sizeof(ndis_packet));
1847
1848         /* Save pointer to the pool. */
1849         pkt->np_private.npp_pool = pool;
1850
1851         /* Set the oob offset pointer. Lots of things expect this. */
1852         pkt->np_private.npp_packetooboffset = offsetof(ndis_packet, np_oob);
1853
1854         /*
1855          * We must initialize the packet flags correctly in order
1856          * for the NDIS_SET_PACKET_MEDIA_SPECIFIC_INFO() and
1857          * NDIS_GET_PACKET_MEDIA_SPECIFIC_INFO() macros to work
1858          * correctly.
1859          */
1860         pkt->np_private.npp_ndispktflags = NDIS_PACKET_ALLOCATED_BY_NDIS;
1861         pkt->np_private.npp_validcounts = FALSE;
1862
1863         *packet = pkt;
1864
1865         *status = NDIS_STATUS_SUCCESS;
1866 }
1867
1868 void
1869 NdisFreePacket(packet)
1870         ndis_packet             *packet;
1871 {
1872         ndis_packet_pool        *p;
1873 #ifdef NDIS_DEBUG_PACKETS
1874         uint8_t                 irql;
1875 #endif
1876
1877         p = (ndis_packet_pool *)packet->np_private.npp_pool;
1878
1879 #ifdef NDIS_DEBUG_PACKETS
1880         KeAcquireSpinLock(&p->np_lock, &irql);
1881 #endif
1882
1883         InterlockedPushEntrySList(&p->np_head, (slist_entry *)packet);
1884
1885 #ifdef NDIS_DEBUG_PACKETS
1886         if (p->np_dead) {
1887                 if (ExQueryDepthSList(&p->np_head) == p->np_cnt)
1888                         KeSetEvent(&p->np_event, IO_NO_INCREMENT, FALSE);
1889         }
1890         KeReleaseSpinLock(&p->np_lock, irql);
1891 #endif
1892 }
1893
1894 static void
1895 NdisUnchainBufferAtFront(packet, buf)
1896         ndis_packet             *packet;
1897         ndis_buffer             **buf;
1898 {
1899         ndis_packet_private     *priv;
1900
1901         if (packet == NULL || buf == NULL)
1902                 return;
1903
1904         priv = &packet->np_private;
1905
1906         priv->npp_validcounts = FALSE;
1907
1908         if (priv->npp_head == priv->npp_tail) {
1909                 *buf = priv->npp_head;
1910                 priv->npp_head = priv->npp_tail = NULL;
1911         } else {
1912                 *buf = priv->npp_head;
1913                 priv->npp_head = (*buf)->mdl_next;
1914         }
1915 }
1916
1917 static void
1918 NdisUnchainBufferAtBack(packet, buf)
1919         ndis_packet             *packet;
1920         ndis_buffer             **buf;
1921 {
1922         ndis_packet_private     *priv;
1923         ndis_buffer             *tmp;
1924
1925         if (packet == NULL || buf == NULL)
1926                 return;
1927
1928         priv = &packet->np_private;
1929
1930         priv->npp_validcounts = FALSE;
1931
1932         if (priv->npp_head == priv->npp_tail) {
1933                 *buf = priv->npp_head;
1934                 priv->npp_head = priv->npp_tail = NULL;
1935         } else {
1936                 *buf = priv->npp_tail;
1937                 tmp = priv->npp_head;
1938                 while (tmp->mdl_next != priv->npp_tail)
1939                         tmp = tmp->mdl_next;
1940                 priv->npp_tail = tmp;
1941                 tmp->mdl_next = NULL;
1942         }
1943 }
1944
1945 /*
1946  * The NDIS "buffer" is really an MDL (memory descriptor list)
1947  * which is used to describe a buffer in a way that allows it
1948  * to mapped into different contexts. We have to be careful how
1949  * we handle them: in some versions of Windows, the NdisFreeBuffer()
1950  * routine is an actual function in the NDIS API, but in others
1951  * it's just a macro wrapper around IoFreeMdl(). There's really
1952  * no way to use the 'descnum' parameter to count how many
1953  * "buffers" are allocated since in order to use IoFreeMdl() to
1954  * dispose of a buffer, we have to use IoAllocateMdl() to allocate
1955  * them, and IoAllocateMdl() just grabs them out of the heap.
1956  */
1957
1958 static void
1959 NdisAllocateBufferPool(status, pool, descnum)
1960         ndis_status             *status;
1961         ndis_handle             *pool;
1962         uint32_t                descnum;
1963 {
1964
1965         /*
1966          * The only thing we can really do here is verify that descnum
1967          * is a reasonable value, but I really don't know what to check
1968          * it against.
1969          */
1970
1971         *pool = NonPagedPool;
1972         *status = NDIS_STATUS_SUCCESS;
1973 }
1974
1975 static void
1976 NdisFreeBufferPool(pool)
1977         ndis_handle             pool;
1978 {
1979 }
1980
1981 static void
1982 NdisAllocateBuffer(status, buffer, pool, vaddr, len)
1983         ndis_status             *status;
1984         ndis_buffer             **buffer;
1985         ndis_handle             pool;
1986         void                    *vaddr;
1987         uint32_t                len;
1988 {
1989         ndis_buffer             *buf;
1990
1991         buf = IoAllocateMdl(vaddr, len, FALSE, FALSE, NULL);
1992         if (buf == NULL) {
1993                 *status = NDIS_STATUS_RESOURCES;
1994                 return;
1995         }
1996
1997         MmBuildMdlForNonPagedPool(buf);
1998
1999         *buffer = buf;
2000         *status = NDIS_STATUS_SUCCESS;
2001 }
2002
2003 static void
2004 NdisFreeBuffer(buf)
2005         ndis_buffer             *buf;
2006 {
2007         IoFreeMdl(buf);
2008 }
2009
2010 /* Aw c'mon. */
2011
2012 static uint32_t
2013 NdisBufferLength(buf)
2014         ndis_buffer             *buf;
2015 {
2016         return (MmGetMdlByteCount(buf));
2017 }
2018
2019 /*
2020  * Get the virtual address and length of a buffer.
2021  * Note: the vaddr argument is optional.
2022  */
2023
2024 static void
2025 NdisQueryBuffer(buf, vaddr, len)
2026         ndis_buffer             *buf;
2027         void                    **vaddr;
2028         uint32_t                *len;
2029 {
2030         if (vaddr != NULL)
2031                 *vaddr = MmGetMdlVirtualAddress(buf);
2032         *len = MmGetMdlByteCount(buf);
2033 }
2034
2035 /* Same as above -- we don't care about the priority. */
2036
2037 static void
2038 NdisQueryBufferSafe(buf, vaddr, len, prio)
2039         ndis_buffer             *buf;
2040         void                    **vaddr;
2041         uint32_t                *len;
2042         uint32_t                prio;
2043 {
2044         if (vaddr != NULL)
2045                 *vaddr = MmGetMdlVirtualAddress(buf);
2046         *len = MmGetMdlByteCount(buf);
2047 }
2048
2049 /* Damnit Microsoft!! How many ways can you do the same thing?! */
2050
2051 static void *
2052 NdisBufferVirtualAddress(buf)
2053         ndis_buffer             *buf;
2054 {
2055         return (MmGetMdlVirtualAddress(buf));
2056 }
2057
2058 static void *
2059 NdisBufferVirtualAddressSafe(buf, prio)
2060         ndis_buffer             *buf;
2061         uint32_t                prio;
2062 {
2063         return (MmGetMdlVirtualAddress(buf));
2064 }
2065
2066 static void
2067 NdisAdjustBufferLength(buf, len)
2068         ndis_buffer             *buf;
2069         int                     len;
2070 {
2071         MmGetMdlByteCount(buf) = len;
2072 }
2073
2074 static uint32_t
2075 NdisInterlockedIncrement(addend)
2076         uint32_t                *addend;
2077 {
2078         atomic_add_long((u_long *)addend, 1);
2079         return (*addend);
2080 }
2081
2082 static uint32_t
2083 NdisInterlockedDecrement(addend)
2084         uint32_t                *addend;
2085 {
2086         atomic_subtract_long((u_long *)addend, 1);
2087         return (*addend);
2088 }
2089
2090 static void
2091 NdisInitializeEvent(event)
2092         ndis_event              *event;
2093 {
2094         /*
2095          * NDIS events are always notification
2096          * events, and should be initialized to the
2097          * not signaled state.
2098          */
2099         KeInitializeEvent(&event->ne_event, EVENT_TYPE_NOTIFY, FALSE);
2100 }
2101
2102 static void
2103 NdisSetEvent(event)
2104         ndis_event              *event;
2105 {
2106         KeSetEvent(&event->ne_event, IO_NO_INCREMENT, FALSE);
2107 }
2108
2109 static void
2110 NdisResetEvent(event)
2111         ndis_event              *event;
2112 {
2113         KeResetEvent(&event->ne_event);
2114 }
2115
2116 static uint8_t
2117 NdisWaitEvent(event, msecs)
2118         ndis_event              *event;
2119         uint32_t                msecs;
2120 {
2121         int64_t                 duetime;
2122         uint32_t                rval;
2123
2124         duetime = ((int64_t)msecs * -10000);
2125         rval = KeWaitForSingleObject(event,
2126             0, 0, TRUE, msecs ? & duetime : NULL);
2127
2128         if (rval == STATUS_TIMEOUT)
2129                 return (FALSE);
2130
2131         return (TRUE);
2132 }
2133
2134 static ndis_status
2135 NdisUnicodeStringToAnsiString(dstr, sstr)
2136         ansi_string             *dstr;
2137         unicode_string          *sstr;
2138 {
2139         uint32_t                rval;
2140
2141         rval = RtlUnicodeStringToAnsiString(dstr, sstr, FALSE);
2142
2143         if (rval == STATUS_INSUFFICIENT_RESOURCES)
2144                 return (NDIS_STATUS_RESOURCES);
2145         if (rval)
2146                 return (NDIS_STATUS_FAILURE);
2147
2148         return (NDIS_STATUS_SUCCESS);
2149 }
2150
2151 static ndis_status
2152 NdisAnsiStringToUnicodeString(dstr, sstr)
2153         unicode_string          *dstr;
2154         ansi_string             *sstr;
2155 {
2156         uint32_t                rval;
2157
2158         rval = RtlAnsiStringToUnicodeString(dstr, sstr, FALSE);
2159
2160         if (rval == STATUS_INSUFFICIENT_RESOURCES)
2161                 return (NDIS_STATUS_RESOURCES);
2162         if (rval)
2163                 return (NDIS_STATUS_FAILURE);
2164
2165         return (NDIS_STATUS_SUCCESS);
2166 }
2167
2168 static ndis_status
2169 NdisMPciAssignResources(adapter, slot, list)
2170         ndis_handle             adapter;
2171         uint32_t                slot;
2172         ndis_resource_list      **list;
2173 {
2174         ndis_miniport_block     *block;
2175
2176         if (adapter == NULL || list == NULL)
2177                 return (NDIS_STATUS_FAILURE);
2178
2179         block = (ndis_miniport_block *)adapter;
2180         *list = block->nmb_rlist;
2181
2182         return (NDIS_STATUS_SUCCESS);
2183 }
2184
2185 static uint8_t
2186 ndis_intr(iobj, arg)
2187         kinterrupt              *iobj;
2188         void                    *arg;
2189 {
2190         struct ndis_softc       *sc;
2191         uint8_t                 is_our_intr = FALSE;
2192         int                     call_isr = 0;
2193         ndis_miniport_interrupt *intr;
2194
2195         sc = arg;
2196         intr = sc->ndis_block->nmb_interrupt;
2197
2198         if (intr == NULL || sc->ndis_block->nmb_miniportadapterctx == NULL)
2199                 return (FALSE);
2200
2201         if (sc->ndis_block->nmb_interrupt->ni_isrreq == TRUE)
2202                 MSCALL3(intr->ni_isrfunc, &is_our_intr, &call_isr,
2203                     sc->ndis_block->nmb_miniportadapterctx);
2204         else {
2205                 MSCALL1(sc->ndis_chars->nmc_disable_interrupts_func,
2206                     sc->ndis_block->nmb_miniportadapterctx);
2207                 call_isr = 1;
2208         }
2209  
2210         if (call_isr)
2211                 IoRequestDpc(sc->ndis_block->nmb_deviceobj, NULL, sc);
2212
2213         return (is_our_intr);
2214 }
2215
2216 static void
2217 ndis_intrhand(dpc, intr, sysarg1, sysarg2)
2218         kdpc                    *dpc;
2219         ndis_miniport_interrupt *intr;
2220         void                    *sysarg1;
2221         void                    *sysarg2;
2222 {
2223         struct ndis_softc       *sc;
2224         ndis_miniport_block     *block;
2225         ndis_handle             adapter;
2226
2227         block = intr->ni_block;
2228         adapter = block->nmb_miniportadapterctx;
2229         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2230
2231         if (NDIS_SERIALIZED(sc->ndis_block))
2232                 KeAcquireSpinLockAtDpcLevel(&block->nmb_lock);
2233
2234         MSCALL1(intr->ni_dpcfunc, adapter);
2235
2236         /* If there's a MiniportEnableInterrupt() routine, call it. */
2237
2238         if (sc->ndis_chars->nmc_enable_interrupts_func != NULL)
2239                 MSCALL1(sc->ndis_chars->nmc_enable_interrupts_func, adapter);
2240
2241         if (NDIS_SERIALIZED(sc->ndis_block))
2242                 KeReleaseSpinLockFromDpcLevel(&block->nmb_lock);
2243
2244         /*
2245          * Set the completion event if we've drained all
2246          * pending interrupts.
2247          */
2248
2249         KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
2250         intr->ni_dpccnt--;
2251         if (intr->ni_dpccnt == 0)
2252                 KeSetEvent(&intr->ni_dpcevt, IO_NO_INCREMENT, FALSE);
2253         KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
2254 }
2255
2256 static ndis_status
2257 NdisMRegisterInterrupt(ndis_miniport_interrupt *intr, ndis_handle adapter,
2258     uint32_t ivec, uint32_t ilevel, uint8_t reqisr, uint8_t shared,
2259     ndis_interrupt_mode imode)
2260 {
2261         ndis_miniport_block     *block;
2262         ndis_miniport_characteristics *ch;
2263         struct ndis_softc       *sc;
2264         int                     error;
2265
2266         block = adapter;
2267         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2268         ch = IoGetDriverObjectExtension(block->nmb_deviceobj->do_drvobj,
2269             (void *)1);
2270
2271         intr->ni_rsvd = ExAllocatePoolWithTag(NonPagedPool,
2272             sizeof(struct mtx), 0);
2273         if (intr->ni_rsvd == NULL)
2274                 return (NDIS_STATUS_RESOURCES);
2275
2276         intr->ni_block = adapter;
2277         intr->ni_isrreq = reqisr;
2278         intr->ni_shared = shared;
2279         intr->ni_dpccnt = 0;
2280         intr->ni_isrfunc = ch->nmc_isr_func;
2281         intr->ni_dpcfunc = ch->nmc_interrupt_func;
2282
2283         KeInitializeEvent(&intr->ni_dpcevt, EVENT_TYPE_NOTIFY, TRUE);
2284         KeInitializeDpc(&intr->ni_dpc,
2285             ndis_findwrap((funcptr)ndis_intrhand), intr);
2286         KeSetImportanceDpc(&intr->ni_dpc, KDPC_IMPORTANCE_LOW);
2287
2288         error = IoConnectInterrupt(&intr->ni_introbj,
2289             ndis_findwrap((funcptr)ndis_intr), sc, NULL,
2290             ivec, ilevel, 0, imode, shared, 0, FALSE);
2291
2292         if (error != STATUS_SUCCESS)
2293                 return (NDIS_STATUS_FAILURE);
2294
2295         block->nmb_interrupt = intr;
2296
2297         return (NDIS_STATUS_SUCCESS);
2298 }
2299
2300 static void
2301 NdisMDeregisterInterrupt(intr)
2302         ndis_miniport_interrupt *intr;
2303 {
2304         ndis_miniport_block     *block;
2305         uint8_t                 irql;
2306
2307         block = intr->ni_block;
2308
2309         /* Should really be KeSynchronizeExecution() */
2310
2311         KeAcquireSpinLock(intr->ni_introbj->ki_lock, &irql);
2312         block->nmb_interrupt = NULL;
2313         KeReleaseSpinLock(intr->ni_introbj->ki_lock, irql);
2314 /*
2315         KeFlushQueuedDpcs();
2316 */
2317         /* Disconnect our ISR */
2318
2319         IoDisconnectInterrupt(intr->ni_introbj);
2320
2321         KeWaitForSingleObject(&intr->ni_dpcevt, 0, 0, FALSE, NULL);
2322         KeResetEvent(&intr->ni_dpcevt);
2323 }
2324
2325 static void
2326 NdisMRegisterAdapterShutdownHandler(adapter, shutdownctx, shutdownfunc)
2327         ndis_handle             adapter;
2328         void                    *shutdownctx;
2329         ndis_shutdown_handler   shutdownfunc;
2330 {
2331         ndis_miniport_block     *block;
2332         ndis_miniport_characteristics *chars;
2333         struct ndis_softc       *sc;
2334
2335         if (adapter == NULL)
2336                 return;
2337
2338         block = (ndis_miniport_block *)adapter;
2339         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2340         chars = sc->ndis_chars;
2341
2342         chars->nmc_shutdown_handler = shutdownfunc;
2343         chars->nmc_rsvd0 = shutdownctx;
2344 }
2345
2346 static void
2347 NdisMDeregisterAdapterShutdownHandler(adapter)
2348         ndis_handle             adapter;
2349 {
2350         ndis_miniport_block     *block;
2351         ndis_miniport_characteristics *chars;
2352         struct ndis_softc       *sc;
2353
2354         if (adapter == NULL)
2355                 return;
2356
2357         block = (ndis_miniport_block *)adapter;
2358         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2359         chars = sc->ndis_chars;
2360
2361         chars->nmc_shutdown_handler = NULL;
2362         chars->nmc_rsvd0 = NULL;
2363 }
2364
2365 static uint32_t
2366 NDIS_BUFFER_TO_SPAN_PAGES(buf)
2367         ndis_buffer             *buf;
2368 {
2369         if (buf == NULL)
2370                 return (0);
2371         if (MmGetMdlByteCount(buf) == 0)
2372                 return (1);
2373         return (SPAN_PAGES(MmGetMdlVirtualAddress(buf),
2374             MmGetMdlByteCount(buf)));
2375 }
2376
2377 static void
2378 NdisGetBufferPhysicalArraySize(buf, pages)
2379         ndis_buffer             *buf;
2380         uint32_t                *pages;
2381 {
2382         if (buf == NULL)
2383                 return;
2384
2385         *pages = NDIS_BUFFER_TO_SPAN_PAGES(buf);
2386 }
2387
2388 static void
2389 NdisQueryBufferOffset(buf, off, len)
2390         ndis_buffer             *buf;
2391         uint32_t                *off;
2392         uint32_t                *len;
2393 {
2394         if (buf == NULL)
2395                 return;
2396
2397         *off = MmGetMdlByteOffset(buf);
2398         *len = MmGetMdlByteCount(buf);
2399 }
2400
2401 void
2402 NdisMSleep(usecs)
2403         uint32_t                usecs;
2404 {
2405         ktimer                  timer;
2406
2407         /*
2408          * During system bootstrap, (i.e. cold == 1), we aren't
2409          * allowed to sleep, so we have to do a hard DELAY()
2410          * instead.
2411          */
2412
2413         if (cold)
2414                 DELAY(usecs);
2415         else {
2416                 KeInitializeTimer(&timer);
2417                 KeSetTimer(&timer, ((int64_t)usecs * -10), NULL);
2418                 KeWaitForSingleObject(&timer, 0, 0, FALSE, NULL);
2419         }
2420 }
2421
2422 static uint32_t
2423 NdisReadPcmciaAttributeMemory(handle, offset, buf, len)
2424         ndis_handle             handle;
2425         uint32_t                offset;
2426         void                    *buf;
2427         uint32_t                len;
2428 {
2429         struct ndis_softc       *sc;
2430         ndis_miniport_block     *block;
2431         bus_space_handle_t      bh;
2432         bus_space_tag_t         bt;
2433         char                    *dest;
2434         int                     i;
2435
2436         if (handle == NULL)
2437                 return (0);
2438
2439         block = (ndis_miniport_block *)handle;
2440         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2441         dest = buf;
2442
2443         bh = rman_get_bushandle(sc->ndis_res_am);
2444         bt = rman_get_bustag(sc->ndis_res_am);
2445
2446         for (i = 0; i < len; i++)
2447                 dest[i] = bus_space_read_1(bt, bh, (offset + i) * 2);
2448
2449         return (i);
2450 }
2451
2452 static uint32_t
2453 NdisWritePcmciaAttributeMemory(handle, offset, buf, len)
2454         ndis_handle             handle;
2455         uint32_t                offset;
2456         void                    *buf;
2457         uint32_t                len;
2458 {
2459         struct ndis_softc       *sc;
2460         ndis_miniport_block     *block;
2461         bus_space_handle_t      bh;
2462         bus_space_tag_t         bt;
2463         char                    *src;
2464         int                     i;
2465
2466         if (handle == NULL)
2467                 return (0);
2468
2469         block = (ndis_miniport_block *)handle;
2470         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
2471         src = buf;
2472
2473         bh = rman_get_bushandle(sc->ndis_res_am);
2474         bt = rman_get_bustag(sc->ndis_res_am);
2475
2476         for (i = 0; i < len; i++)
2477                 bus_space_write_1(bt, bh, (offset + i) * 2, src[i]);
2478
2479         return (i);
2480 }
2481
2482 static list_entry *
2483 NdisInterlockedInsertHeadList(head, entry, lock)
2484         list_entry              *head;
2485         list_entry              *entry;
2486         ndis_spin_lock          *lock;
2487 {
2488         list_entry              *flink;
2489
2490         KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
2491         flink = head->nle_flink;
2492         entry->nle_flink = flink;
2493         entry->nle_blink = head;
2494         flink->nle_blink = entry;
2495         head->nle_flink = entry;
2496         KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
2497
2498         return (flink);
2499 }
2500
2501 static list_entry *
2502 NdisInterlockedRemoveHeadList(head, lock)
2503         list_entry              *head;
2504         ndis_spin_lock          *lock;
2505 {
2506         list_entry              *flink;
2507         list_entry              *entry;
2508
2509         KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
2510         entry = head->nle_flink;
2511         flink = entry->nle_flink;
2512         head->nle_flink = flink;
2513         flink->nle_blink = head;
2514         KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
2515
2516         return (entry);
2517 }
2518
2519 static list_entry *
2520 NdisInterlockedInsertTailList(head, entry, lock)
2521         list_entry              *head;
2522         list_entry              *entry;
2523         ndis_spin_lock          *lock;
2524 {
2525         list_entry              *blink;
2526
2527         KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql);
2528         blink = head->nle_blink;
2529         entry->nle_flink = head;
2530         entry->nle_blink = blink;
2531         blink->nle_flink = entry;
2532         head->nle_blink = entry;
2533         KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql);
2534
2535         return (blink);
2536 }
2537
2538 static uint8_t
2539 NdisMSynchronizeWithInterrupt(intr, syncfunc, syncctx)
2540         ndis_miniport_interrupt *intr;
2541         void                    *syncfunc;
2542         void                    *syncctx;
2543 {
2544         return (KeSynchronizeExecution(intr->ni_introbj, syncfunc, syncctx));
2545 }
2546
2547 static void
2548 NdisGetCurrentSystemTime(tval)
2549         uint64_t                *tval;
2550 {
2551         ntoskrnl_time(tval);
2552 }
2553
2554 /*
2555  * Return the number of milliseconds since the system booted.
2556  */
2557 static void
2558 NdisGetSystemUpTime(tval)
2559         uint32_t                *tval;
2560 {
2561         struct timespec         ts;
2562
2563         nanouptime(&ts);
2564         *tval = ts.tv_nsec / 1000000 + ts.tv_sec * 1000;
2565 }
2566
2567 static void
2568 NdisInitializeString(dst, src)
2569         unicode_string          *dst;
2570         char                    *src;
2571 {
2572         ansi_string             as;
2573         RtlInitAnsiString(&as, src);
2574         RtlAnsiStringToUnicodeString(dst, &as, TRUE);
2575 }
2576
2577 static void
2578 NdisFreeString(str)
2579         unicode_string          *str;
2580 {
2581         RtlFreeUnicodeString(str);
2582 }
2583
2584 static ndis_status
2585 NdisMRemoveMiniport(adapter)
2586         ndis_handle             *adapter;
2587 {
2588         return (NDIS_STATUS_SUCCESS);
2589 }
2590
2591 static void
2592 NdisInitAnsiString(dst, src)
2593         ansi_string             *dst;
2594         char                    *src;
2595 {
2596         RtlInitAnsiString(dst, src);
2597 }
2598
2599 static void
2600 NdisInitUnicodeString(dst, src)
2601         unicode_string          *dst;
2602         uint16_t                *src;
2603 {
2604         RtlInitUnicodeString(dst, src);
2605 }
2606
2607 static void NdisMGetDeviceProperty(adapter, phydevobj,
2608         funcdevobj, nextdevobj, resources, transresources)
2609         ndis_handle             adapter;
2610         device_object           **phydevobj;
2611         device_object           **funcdevobj;
2612         device_object           **nextdevobj;
2613         cm_resource_list        *resources;
2614         cm_resource_list        *transresources;
2615 {
2616         ndis_miniport_block     *block;
2617
2618         block = (ndis_miniport_block *)adapter;
2619
2620         if (phydevobj != NULL)
2621                 *phydevobj = block->nmb_physdeviceobj;
2622         if (funcdevobj != NULL)
2623                 *funcdevobj = block->nmb_deviceobj;
2624         if (nextdevobj != NULL)
2625                 *nextdevobj = block->nmb_nextdeviceobj;
2626 }
2627
2628 static void
2629 NdisGetFirstBufferFromPacket(packet, buf, firstva, firstlen, totlen)
2630         ndis_packet             *packet;
2631         ndis_buffer             **buf;
2632         void                    **firstva;
2633         uint32_t                *firstlen;
2634         uint32_t                *totlen;
2635 {
2636         ndis_buffer             *tmp;
2637
2638         tmp = packet->np_private.npp_head;
2639         *buf = tmp;
2640         if (tmp == NULL) {
2641                 *firstva = NULL;
2642                 *firstlen = *totlen = 0;
2643         } else {
2644                 *firstva = MmGetMdlVirtualAddress(tmp);
2645                 *firstlen = *totlen = MmGetMdlByteCount(tmp);
2646                 for (tmp = tmp->mdl_next; tmp != NULL; tmp = tmp->mdl_next)
2647                         *totlen += MmGetMdlByteCount(tmp);
2648         }
2649 }
2650
2651 static void
2652 NdisGetFirstBufferFromPacketSafe(packet, buf, firstva, firstlen, totlen, prio)
2653         ndis_packet             *packet;
2654         ndis_buffer             **buf;
2655         void                    **firstva;
2656         uint32_t                *firstlen;
2657         uint32_t                *totlen;
2658         uint32_t                prio;
2659 {
2660         NdisGetFirstBufferFromPacket(packet, buf, firstva, firstlen, totlen);
2661 }
2662
2663 static int
2664 ndis_find_sym(lf, filename, suffix, sym)
2665         linker_file_t           lf;
2666         char                    *filename;
2667         char                    *suffix;
2668         caddr_t                 *sym;
2669 {
2670         char                    *fullsym;
2671         char                    *suf;
2672         int                     i;
2673
2674         fullsym = ExAllocatePoolWithTag(NonPagedPool, MAXPATHLEN, 0);
2675         if (fullsym == NULL)
2676                 return (ENOMEM);
2677
2678         bzero(fullsym, MAXPATHLEN);
2679         strncpy(fullsym, filename, MAXPATHLEN);
2680         if (strlen(filename) < 4) {
2681                 ExFreePool(fullsym);
2682                 return (EINVAL);
2683         }
2684
2685         /* If the filename has a .ko suffix, strip if off. */
2686         suf = fullsym + (strlen(filename) - 3);
2687         if (strcmp(suf, ".ko") == 0)
2688                 *suf = '\0';
2689
2690         for (i = 0; i < strlen(fullsym); i++) {
2691                 if (fullsym[i] == '.')
2692                         fullsym[i] = '_';
2693                 else
2694                         fullsym[i] = tolower(fullsym[i]);
2695         }
2696         strcat(fullsym, suffix);
2697         *sym = linker_file_lookup_symbol(lf, fullsym, 0);
2698         ExFreePool(fullsym);
2699         if (*sym == 0)
2700                 return (ENOENT);
2701
2702         return (0);
2703 }
2704
2705 struct ndis_checkmodule {
2706         char    *afilename;
2707         ndis_fh *fh;
2708 };
2709
2710 /*
2711  * See if a single module contains the symbols for a specified file.
2712  */
2713 static int
2714 NdisCheckModule(linker_file_t lf, void *context)
2715 {
2716         struct ndis_checkmodule *nc;
2717         caddr_t                 kldstart, kldend;
2718
2719         nc = (struct ndis_checkmodule *)context;
2720         if (ndis_find_sym(lf, nc->afilename, "_start", &kldstart))
2721                 return (0);
2722         if (ndis_find_sym(lf, nc->afilename, "_end", &kldend))
2723                 return (0);
2724         nc->fh->nf_vp = lf;
2725         nc->fh->nf_map = NULL;
2726         nc->fh->nf_type = NDIS_FH_TYPE_MODULE;
2727         nc->fh->nf_maplen = (kldend - kldstart) & 0xFFFFFFFF;
2728         return (1);
2729 }
2730
2731 /* can also return NDIS_STATUS_RESOURCES/NDIS_STATUS_ERROR_READING_FILE */
2732 static void
2733 NdisOpenFile(status, filehandle, filelength, filename, highestaddr)
2734         ndis_status             *status;
2735         ndis_handle             *filehandle;
2736         uint32_t                *filelength;
2737         unicode_string          *filename;
2738         ndis_physaddr           highestaddr;
2739 {
2740         ansi_string             as;
2741         char                    *afilename = NULL;
2742         struct thread           *td = curthread;
2743         struct nameidata        nd;
2744         int                     flags, error, vfslocked;
2745         struct vattr            vat;
2746         struct vattr            *vap = &vat;
2747         ndis_fh                 *fh;
2748         char                    *path;
2749         struct ndis_checkmodule nc;
2750
2751         if (RtlUnicodeStringToAnsiString(&as, filename, TRUE)) {
2752                 *status = NDIS_STATUS_RESOURCES;
2753                 return;
2754         }
2755
2756         afilename = strdup(as.as_buf, M_DEVBUF);
2757         RtlFreeAnsiString(&as);
2758
2759         fh = ExAllocatePoolWithTag(NonPagedPool, sizeof(ndis_fh), 0);
2760         if (fh == NULL) {
2761                 free(afilename, M_DEVBUF);
2762                 *status = NDIS_STATUS_RESOURCES;
2763                 return;
2764         }
2765
2766         fh->nf_name = afilename;
2767
2768         /*
2769          * D