The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/ndis/subr_ndis.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 5e0d2b9575c920be47499021ff74293c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.