subr_ndis.c revision 151451
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: head/sys/compat/ndis/subr_ndis.c 151451 2005-10-18 19:52:15Z 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 114static char ndis_filepath[MAXPATHLEN]; 115 116SYSCTL_STRING(_hw, OID_AUTO, ndis_filepath, CTLFLAG_RW, ndis_filepath, 117 MAXPATHLEN, "Path used by NdisOpenFile() to search for files"); 118 119static void NdisInitializeWrapper(ndis_handle *, 120 driver_object *, void *, void *); 121static ndis_status NdisMRegisterMiniport(ndis_handle, 122 ndis_miniport_characteristics *, int); 123static ndis_status NdisAllocateMemoryWithTag(void **, 124 uint32_t, uint32_t); 125static ndis_status NdisAllocateMemory(void **, 126 uint32_t, uint32_t, ndis_physaddr); 127static void NdisFreeMemory(void *, uint32_t, uint32_t); 128static ndis_status NdisMSetAttributesEx(ndis_handle, ndis_handle, 129 uint32_t, uint32_t, ndis_interface_type); 130static void NdisOpenConfiguration(ndis_status *, 131 ndis_handle *, ndis_handle); 132static void NdisOpenConfigurationKeyByIndex(ndis_status *, 133 ndis_handle, uint32_t, unicode_string *, ndis_handle *); 134static void NdisOpenConfigurationKeyByName(ndis_status *, 135 ndis_handle, unicode_string *, ndis_handle *); 136static ndis_status ndis_encode_parm(ndis_miniport_block *, 137 struct sysctl_oid *, ndis_parm_type, ndis_config_parm **); 138static ndis_status ndis_decode_parm(ndis_miniport_block *, 139 ndis_config_parm *, char *); 140static void NdisReadConfiguration(ndis_status *, ndis_config_parm **, 141 ndis_handle, unicode_string *, ndis_parm_type); 142static void NdisWriteConfiguration(ndis_status *, ndis_handle, 143 unicode_string *, ndis_config_parm *); 144static void NdisCloseConfiguration(ndis_handle); 145static void NdisAllocateSpinLock(ndis_spin_lock *); 146static void NdisFreeSpinLock(ndis_spin_lock *); 147static void NdisAcquireSpinLock(ndis_spin_lock *); 148static void NdisReleaseSpinLock(ndis_spin_lock *); 149static void NdisDprAcquireSpinLock(ndis_spin_lock *); 150static void NdisDprReleaseSpinLock(ndis_spin_lock *); 151static void NdisInitializeReadWriteLock(ndis_rw_lock *); 152static void NdisAcquireReadWriteLock(ndis_rw_lock *, 153 uint8_t, ndis_lock_state *); 154static void NdisReleaseReadWriteLock(ndis_rw_lock *, ndis_lock_state *); 155static uint32_t NdisReadPciSlotInformation(ndis_handle, uint32_t, 156 uint32_t, void *, uint32_t); 157static uint32_t NdisWritePciSlotInformation(ndis_handle, uint32_t, 158 uint32_t, void *, uint32_t); 159static void NdisWriteErrorLogEntry(ndis_handle, ndis_error_code, uint32_t, ...); 160static void ndis_map_cb(void *, bus_dma_segment_t *, int, int); 161static void NdisMStartBufferPhysicalMapping(ndis_handle, 162 ndis_buffer *, uint32_t, uint8_t, ndis_paddr_unit *, uint32_t *); 163static void NdisMCompleteBufferPhysicalMapping(ndis_handle, 164 ndis_buffer *, uint32_t); 165static void NdisMInitializeTimer(ndis_miniport_timer *, ndis_handle, 166 ndis_timer_function, void *); 167static void NdisInitializeTimer(ndis_timer *, 168 ndis_timer_function, void *); 169static void NdisSetTimer(ndis_timer *, uint32_t); 170static void NdisMSetPeriodicTimer(ndis_miniport_timer *, uint32_t); 171static void NdisMCancelTimer(ndis_timer *, uint8_t *); 172static void ndis_timercall(kdpc *, ndis_miniport_timer *, 173 void *, void *); 174static void NdisMQueryAdapterResources(ndis_status *, ndis_handle, 175 ndis_resource_list *, uint32_t *); 176static ndis_status NdisMRegisterIoPortRange(void **, 177 ndis_handle, uint32_t, uint32_t); 178static void NdisMDeregisterIoPortRange(ndis_handle, 179 uint32_t, uint32_t, void *); 180static void NdisReadNetworkAddress(ndis_status *, void **, 181 uint32_t *, ndis_handle); 182static ndis_status NdisQueryMapRegisterCount(uint32_t, uint32_t *); 183static ndis_status NdisMAllocateMapRegisters(ndis_handle, 184 uint32_t, uint8_t, uint32_t, uint32_t); 185static void NdisMFreeMapRegisters(ndis_handle); 186static void ndis_mapshared_cb(void *, bus_dma_segment_t *, int, int); 187static void NdisMAllocateSharedMemory(ndis_handle, uint32_t, 188 uint8_t, void **, ndis_physaddr *); 189static void ndis_asyncmem_complete(device_object *, void *); 190static ndis_status NdisMAllocateSharedMemoryAsync(ndis_handle, 191 uint32_t, uint8_t, void *); 192static void NdisMFreeSharedMemory(ndis_handle, uint32_t, 193 uint8_t, void *, ndis_physaddr); 194static ndis_status NdisMMapIoSpace(void **, ndis_handle, 195 ndis_physaddr, uint32_t); 196static void NdisMUnmapIoSpace(ndis_handle, void *, uint32_t); 197static uint32_t NdisGetCacheFillSize(void); 198static uint32_t NdisMGetDmaAlignment(ndis_handle); 199static ndis_status NdisMInitializeScatterGatherDma(ndis_handle, 200 uint8_t, uint32_t); 201static void NdisUnchainBufferAtFront(ndis_packet *, ndis_buffer **); 202static void NdisUnchainBufferAtBack(ndis_packet *, ndis_buffer **); 203static void NdisAllocateBufferPool(ndis_status *, 204 ndis_handle *, uint32_t); 205static void NdisFreeBufferPool(ndis_handle); 206static void NdisAllocateBuffer(ndis_status *, ndis_buffer **, 207 ndis_handle, void *, uint32_t); 208static void NdisFreeBuffer(ndis_buffer *); 209static uint32_t NdisBufferLength(ndis_buffer *); 210static void NdisQueryBuffer(ndis_buffer *, void **, uint32_t *); 211static void NdisQueryBufferSafe(ndis_buffer *, void **, 212 uint32_t *, uint32_t); 213static void *NdisBufferVirtualAddress(ndis_buffer *); 214static void *NdisBufferVirtualAddressSafe(ndis_buffer *, uint32_t); 215static void NdisAdjustBufferLength(ndis_buffer *, int); 216static uint32_t NdisInterlockedIncrement(uint32_t *); 217static uint32_t NdisInterlockedDecrement(uint32_t *); 218static void NdisInitializeEvent(ndis_event *); 219static void NdisSetEvent(ndis_event *); 220static void NdisResetEvent(ndis_event *); 221static uint8_t NdisWaitEvent(ndis_event *, uint32_t); 222static ndis_status NdisUnicodeStringToAnsiString(ansi_string *, 223 unicode_string *); 224static ndis_status 225 NdisAnsiStringToUnicodeString(unicode_string *, ansi_string *); 226static ndis_status NdisMPciAssignResources(ndis_handle, 227 uint32_t, ndis_resource_list **); 228static ndis_status NdisMRegisterInterrupt(ndis_miniport_interrupt *, 229 ndis_handle, uint32_t, uint32_t, uint8_t, 230 uint8_t, ndis_interrupt_mode); 231static void NdisMDeregisterInterrupt(ndis_miniport_interrupt *); 232static void NdisMRegisterAdapterShutdownHandler(ndis_handle, void *, 233 ndis_shutdown_handler); 234static void NdisMDeregisterAdapterShutdownHandler(ndis_handle); 235static uint32_t NDIS_BUFFER_TO_SPAN_PAGES(ndis_buffer *); 236static void NdisGetBufferPhysicalArraySize(ndis_buffer *, 237 uint32_t *); 238static void NdisQueryBufferOffset(ndis_buffer *, 239 uint32_t *, uint32_t *); 240static uint32_t NdisReadPcmciaAttributeMemory(ndis_handle, 241 uint32_t, void *, uint32_t); 242static uint32_t NdisWritePcmciaAttributeMemory(ndis_handle, 243 uint32_t, void *, uint32_t); 244static list_entry *NdisInterlockedInsertHeadList(list_entry *, 245 list_entry *, ndis_spin_lock *); 246static list_entry *NdisInterlockedRemoveHeadList(list_entry *, 247 ndis_spin_lock *); 248static list_entry *NdisInterlockedInsertTailList(list_entry *, 249 list_entry *, ndis_spin_lock *); 250static uint8_t 251 NdisMSynchronizeWithInterrupt(ndis_miniport_interrupt *, 252 void *, void *); 253static void NdisGetCurrentSystemTime(uint64_t *); 254static void NdisGetSystemUpTime(uint32_t *); 255static void NdisInitializeString(unicode_string *, char *); 256static void NdisInitAnsiString(ansi_string *, char *); 257static void NdisInitUnicodeString(unicode_string *, uint16_t *); 258static void NdisFreeString(unicode_string *); 259static ndis_status NdisMRemoveMiniport(ndis_handle *); 260static void NdisTerminateWrapper(ndis_handle, void *); 261static void NdisMGetDeviceProperty(ndis_handle, device_object **, 262 device_object **, device_object **, cm_resource_list *, 263 cm_resource_list *); 264static void NdisGetFirstBufferFromPacket(ndis_packet *, 265 ndis_buffer **, void **, uint32_t *, uint32_t *); 266static void NdisGetFirstBufferFromPacketSafe(ndis_packet *, 267 ndis_buffer **, void **, uint32_t *, uint32_t *, uint32_t); 268static int ndis_find_sym(linker_file_t, char *, char *, caddr_t *); 269static void NdisOpenFile(ndis_status *, ndis_handle *, uint32_t *, 270 unicode_string *, ndis_physaddr); 271static void NdisMapFile(ndis_status *, void **, ndis_handle); 272static void NdisUnmapFile(ndis_handle); 273static void NdisCloseFile(ndis_handle); 274static uint8_t NdisSystemProcessorCount(void); 275static void NdisMIndicateStatusComplete(ndis_handle); 276static void NdisMIndicateStatus(ndis_handle, ndis_status, 277 void *, uint32_t); 278static void ndis_intr(void *); 279static void ndis_intrhand(kdpc *, ndis_miniport_interrupt *, void *, void *); 280static funcptr ndis_findwrap(funcptr); 281static void NdisCopyFromPacketToPacket(ndis_packet *, 282 uint32_t, uint32_t, ndis_packet *, uint32_t, uint32_t *); 283static void NdisCopyFromPacketToPacketSafe(ndis_packet *, 284 uint32_t, uint32_t, ndis_packet *, uint32_t, uint32_t *, uint32_t); 285static ndis_status NdisMRegisterDevice(ndis_handle, 286 unicode_string *, unicode_string *, driver_dispatch **, 287 void **, ndis_handle *); 288static ndis_status NdisMDeregisterDevice(ndis_handle); 289static ndis_status 290 NdisMQueryAdapterInstanceName(unicode_string *, ndis_handle); 291static void NdisMRegisterUnloadHandler(ndis_handle, void *); 292static 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 304int 305ndis_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 322int 323ndis_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 336static funcptr 337ndis_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 */ 357static void 358NdisInitializeWrapper(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 389static void 390NdisTerminateWrapper(handle, syspec) 391 ndis_handle handle; 392 void *syspec; 393{ 394 /* Nothing to see here, move along. */ 395 return; 396} 397 398static ndis_status 399NdisMRegisterMiniport(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 438static ndis_status 439NdisAllocateMemoryWithTag(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 455static ndis_status 456NdisAllocateMemory(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 472static void 473NdisFreeMemory(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 486static ndis_status 487NdisMSetAttributesEx(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 509static void 510NdisOpenConfiguration(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 521static void 522NdisOpenConfigurationKeyByName(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 534static void 535NdisOpenConfigurationKeyByIndex(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 547static ndis_status 548ndis_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 616int 617ndis_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 640int 641ndis_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 663static void 664NdisReadConfiguration(status, parm, cfg, key, type) 665 ndis_status *status; 666 ndis_config_parm **parm; 667 ndis_handle cfg; 668 unicode_string *key; 669 ndis_parm_type type; 670{ 671 char *keystr = NULL; 672 ndis_miniport_block *block; 673 struct ndis_softc *sc; 674 struct sysctl_oid *oidp; 675 struct sysctl_ctx_entry *e; 676 ansi_string as; 677 678 block = (ndis_miniport_block *)cfg; 679 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 680 681 if (key->us_len == 0 || key->us_buf == NULL) { 682 *status = NDIS_STATUS_FAILURE; 683 return; 684 } 685 686 if (RtlUnicodeStringToAnsiString(&as, key, TRUE)) { 687 *status = NDIS_STATUS_RESOURCES; 688 return; 689 } 690 691 keystr = as.as_buf; 692 693 /* 694 * See if registry key is already in a list of known keys 695 * included with the driver. 696 */ 697#if __FreeBSD_version < 502113 698 TAILQ_FOREACH(e, &sc->ndis_ctx, link) { 699#else 700 TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) { 701#endif 702 oidp = e->entry; 703 if (ndis_strcasecmp(oidp->oid_name, keystr) == 0) { 704 if (strcmp((char *)oidp->oid_arg1, "UNSET") == 0) { 705 RtlFreeAnsiString(&as); 706 *status = NDIS_STATUS_FAILURE; 707 return; 708 } 709 710 *status = ndis_encode_parm(block, oidp, type, parm); 711 RtlFreeAnsiString(&as); 712 return; 713 } 714 } 715 716 /* 717 * If the key didn't match, add it to the list of dynamically 718 * created ones. Sometimes, drivers refer to registry keys 719 * that aren't documented in their .INF files. These keys 720 * are supposed to be created by some sort of utility or 721 * control panel snap-in that comes with the driver software. 722 * Sometimes it's useful to be able to manipulate these. 723 * If the driver requests the key in the form of a string, 724 * make its default value an empty string, otherwise default 725 * it to "0". 726 */ 727 728 if (type == ndis_parm_int || type == ndis_parm_hexint) 729 ndis_add_sysctl(sc, keystr, "(dynamic integer key)", 730 "UNSET", CTLFLAG_RW); 731 else 732 ndis_add_sysctl(sc, keystr, "(dynamic string key)", 733 "UNSET", CTLFLAG_RW); 734 735 RtlFreeAnsiString(&as); 736 *status = NDIS_STATUS_FAILURE; 737 738 return; 739} 740 741static ndis_status 742ndis_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 771static void 772NdisWriteConfiguration(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 829static void 830NdisCloseConfiguration(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 */ 855static void 856NdisAllocateSpinLock(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 */ 873static void 874NdisFreeSpinLock(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 888static void 889NdisAcquireSpinLock(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 900static void 901NdisReleaseSpinLock(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 */ 911static void 912NdisDprAcquireSpinLock(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 */ 922static void 923NdisDprReleaseSpinLock(lock) 924 ndis_spin_lock *lock; 925{ 926 KeReleaseSpinLockFromDpcLevel(&lock->nsl_spinlock); 927 return; 928} 929 930static void 931NdisInitializeReadWriteLock(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 939static void 940NdisAcquireReadWriteLock(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 954static void 955NdisReleaseReadWriteLock(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 968static uint32_t 969NdisReadPciSlotInformation(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 1011static uint32_t 1012NdisWritePciSlotInformation(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 1045static void 1046NdisWriteErrorLogEntry(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 char msgbuf[ERRMSGLEN]; 1055 device_t dev; 1056 driver_object *drv; 1057 struct ndis_softc *sc; 1058 struct ifnet *ifp; 1059 unicode_string us; 1060 ansi_string as; 1061 1062 block = (ndis_miniport_block *)adapter; 1063 dev = block->nmb_physdeviceobj->do_devext; 1064 drv = block->nmb_deviceobj->do_drvobj; 1065 sc = device_get_softc(dev); 1066 ifp = sc->ifp; 1067 1068 error = pe_get_message((vm_offset_t)drv->dro_driverstart, 1069 code, &str, &i, &flags); 1070 if (error == 0 && flags & MESSAGE_RESOURCE_UNICODE && 1071 ifp->if_flags & IFF_DEBUG) { 1072 RtlInitUnicodeString(&us, (uint16_t *)msgbuf); 1073 if (RtlUnicodeStringToAnsiString(&as, &us, TRUE)) 1074 return; 1075 str = as.as_buf; 1076 } 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 (str != NULL) 1091 RtlFreeAnsiString(&as); 1092 1093 return; 1094} 1095 1096static void 1097ndis_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 1121static void 1122NdisMStartBufferPhysicalMapping(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 1163static void 1164NdisMCompleteBufferPhysicalMapping(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 1198static void 1199NdisInitializeTimer(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 1211static void 1212ndis_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 */ 1253static void 1254NdisMInitializeTimer(timer, handle, func, ctx) 1255 ndis_miniport_timer *timer; 1256 ndis_handle handle; 1257 ndis_timer_function func; 1258 void *ctx; 1259{ 1260 uint8_t irql; 1261 1262 /* Save the driver's funcptr and context */ 1263 1264 timer->nmt_timerfunc = func; 1265 timer->nmt_timerctx = ctx; 1266 timer->nmt_block = handle; 1267 1268 /* 1269 * Set up the timer so it will call our intermediate DPC. 1270 * Be sure to use the wrapped entry point, since 1271 * ntoskrnl_run_dpc() expects to invoke a function with 1272 * Microsoft calling conventions. 1273 */ 1274 KeInitializeTimer(&timer->nmt_ktimer); 1275 KeInitializeDpc(&timer->nmt_kdpc, 1276 ndis_findwrap((funcptr)ndis_timercall), timer); 1277 timer->nmt_ktimer.k_dpc = &timer->nmt_kdpc; 1278 1279 KeAcquireSpinLock(&timer->nmt_block->nmb_lock, &irql); 1280 1281 timer->nmt_nexttimer = timer->nmt_block->nmb_timerlist; 1282 timer->nmt_block->nmb_timerlist = timer; 1283 1284 KeReleaseSpinLock(&timer->nmt_block->nmb_lock, irql); 1285 1286 return; 1287} 1288 1289/* 1290 * In Windows, there's both an NdisMSetTimer() and an NdisSetTimer(), 1291 * but the former is just a macro wrapper around the latter. 1292 */ 1293static void 1294NdisSetTimer(timer, msecs) 1295 ndis_timer *timer; 1296 uint32_t msecs; 1297{ 1298 /* 1299 * KeSetTimer() wants the period in 1300 * hundred nanosecond intervals. 1301 */ 1302 KeSetTimer(&timer->nt_ktimer, 1303 ((int64_t)msecs * -10000), &timer->nt_kdpc); 1304 1305 return; 1306} 1307 1308static void 1309NdisMSetPeriodicTimer(timer, msecs) 1310 ndis_miniport_timer *timer; 1311 uint32_t msecs; 1312{ 1313 KeSetTimerEx(&timer->nmt_ktimer, 1314 ((int64_t)msecs * -10000), msecs, &timer->nmt_kdpc); 1315 1316 return; 1317} 1318 1319/* 1320 * Technically, this is really NdisCancelTimer(), but we also 1321 * (ab)use it for NdisMCancelTimer(), since in our implementation 1322 * we don't need the extra info in the ndis_miniport_timer 1323 * structure just to cancel a timer. 1324 */ 1325 1326static void 1327NdisMCancelTimer(timer, cancelled) 1328 ndis_timer *timer; 1329 uint8_t *cancelled; 1330{ 1331 *cancelled = KeCancelTimer(&timer->nt_ktimer); 1332 return; 1333} 1334 1335static void 1336NdisMQueryAdapterResources(status, adapter, list, buflen) 1337 ndis_status *status; 1338 ndis_handle adapter; 1339 ndis_resource_list *list; 1340 uint32_t *buflen; 1341{ 1342 ndis_miniport_block *block; 1343 struct ndis_softc *sc; 1344 int rsclen; 1345 1346 block = (ndis_miniport_block *)adapter; 1347 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 1348 1349 rsclen = sizeof(ndis_resource_list) + 1350 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)); 1351 if (*buflen < rsclen) { 1352 *buflen = rsclen; 1353 *status = NDIS_STATUS_INVALID_LENGTH; 1354 return; 1355 } 1356 1357 bcopy((char *)block->nmb_rlist, (char *)list, rsclen); 1358 *status = NDIS_STATUS_SUCCESS; 1359 1360 return; 1361} 1362 1363static ndis_status 1364NdisMRegisterIoPortRange(offset, adapter, port, numports) 1365 void **offset; 1366 ndis_handle adapter; 1367 uint32_t port; 1368 uint32_t numports; 1369{ 1370 struct ndis_miniport_block *block; 1371 struct ndis_softc *sc; 1372 1373 if (adapter == NULL) 1374 return(NDIS_STATUS_FAILURE); 1375 1376 block = (ndis_miniport_block *)adapter; 1377 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 1378 1379 if (sc->ndis_res_io == NULL) 1380 return(NDIS_STATUS_FAILURE); 1381 1382 /* Don't let the device map more ports than we have. */ 1383 if (rman_get_size(sc->ndis_res_io) < numports) 1384 return(NDIS_STATUS_INVALID_LENGTH); 1385 1386 *offset = (void *)rman_get_start(sc->ndis_res_io); 1387 1388 return(NDIS_STATUS_SUCCESS); 1389} 1390 1391static void 1392NdisMDeregisterIoPortRange(adapter, port, numports, offset) 1393 ndis_handle adapter; 1394 uint32_t port; 1395 uint32_t numports; 1396 void *offset; 1397{ 1398 return; 1399} 1400 1401static void 1402NdisReadNetworkAddress(status, addr, addrlen, adapter) 1403 ndis_status *status; 1404 void **addr; 1405 uint32_t *addrlen; 1406 ndis_handle adapter; 1407{ 1408 struct ndis_softc *sc; 1409 ndis_miniport_block *block; 1410 uint8_t empty[] = { 0, 0, 0, 0, 0, 0 }; 1411 1412 block = (ndis_miniport_block *)adapter; 1413 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 1414 1415#ifdef IFP2ENADDR 1416 if (bcmp(IFP2ENADDR(sc->ifp), empty, ETHER_ADDR_LEN) == 0) 1417#else 1418 if (bcmp(sc->arpcom.ac_enaddr, empty, ETHER_ADDR_LEN) == 0) 1419#endif 1420 *status = NDIS_STATUS_FAILURE; 1421 else { 1422#ifdef IFP2ENADDR 1423 *addr = IFP2ENADDR(sc->ifp); 1424#else 1425 *addr = sc->arpcom.ac_enaddr; 1426#endif 1427 *addrlen = ETHER_ADDR_LEN; 1428 *status = NDIS_STATUS_SUCCESS; 1429 } 1430 1431 return; 1432} 1433 1434static ndis_status 1435NdisQueryMapRegisterCount(bustype, cnt) 1436 uint32_t bustype; 1437 uint32_t *cnt; 1438{ 1439 *cnt = 8192; 1440 return(NDIS_STATUS_SUCCESS); 1441} 1442 1443static ndis_status 1444NdisMAllocateMapRegisters(adapter, dmachannel, dmasize, physmapneeded, maxmap) 1445 ndis_handle adapter; 1446 uint32_t dmachannel; 1447 uint8_t dmasize; 1448 uint32_t physmapneeded; 1449 uint32_t maxmap; 1450{ 1451 struct ndis_softc *sc; 1452 ndis_miniport_block *block; 1453 int error, i, nseg = NDIS_MAXSEG; 1454 1455 block = (ndis_miniport_block *)adapter; 1456 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 1457 1458 sc->ndis_mmaps = malloc(sizeof(bus_dmamap_t) * physmapneeded, 1459 M_DEVBUF, M_NOWAIT|M_ZERO); 1460 1461 if (sc->ndis_mmaps == NULL) 1462 return(NDIS_STATUS_RESOURCES); 1463 1464 error = bus_dma_tag_create(sc->ndis_parent_tag, ETHER_ALIGN, 0, 1465 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1466 NULL, maxmap * nseg, nseg, maxmap, BUS_DMA_ALLOCNOW, 1467 NULL, NULL, &sc->ndis_mtag); 1468 1469 if (error) { 1470 free(sc->ndis_mmaps, M_DEVBUF); 1471 return(NDIS_STATUS_RESOURCES); 1472 } 1473 1474 for (i = 0; i < physmapneeded; i++) 1475 bus_dmamap_create(sc->ndis_mtag, 0, &sc->ndis_mmaps[i]); 1476 1477 sc->ndis_mmapcnt = physmapneeded; 1478 1479 return(NDIS_STATUS_SUCCESS); 1480} 1481 1482static void 1483NdisMFreeMapRegisters(adapter) 1484 ndis_handle adapter; 1485{ 1486 struct ndis_softc *sc; 1487 ndis_miniport_block *block; 1488 int i; 1489 1490 block = (ndis_miniport_block *)adapter; 1491 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 1492 1493 for (i = 0; i < sc->ndis_mmapcnt; i++) 1494 bus_dmamap_destroy(sc->ndis_mtag, sc->ndis_mmaps[i]); 1495 1496 free(sc->ndis_mmaps, M_DEVBUF); 1497 1498 bus_dma_tag_destroy(sc->ndis_mtag); 1499 1500 return; 1501} 1502 1503static void 1504ndis_mapshared_cb(arg, segs, nseg, error) 1505 void *arg; 1506 bus_dma_segment_t *segs; 1507 int nseg; 1508 int error; 1509{ 1510 ndis_physaddr *p; 1511 1512 if (error || nseg > 1) 1513 return; 1514 1515 p = arg; 1516 1517 p->np_quad = segs[0].ds_addr; 1518 1519 return; 1520} 1521 1522/* 1523 * This maps to bus_dmamem_alloc(). 1524 */ 1525 1526static void 1527NdisMAllocateSharedMemory(adapter, len, cached, vaddr, paddr) 1528 ndis_handle adapter; 1529 uint32_t len; 1530 uint8_t cached; 1531 void **vaddr; 1532 ndis_physaddr *paddr; 1533{ 1534 ndis_miniport_block *block; 1535 struct ndis_softc *sc; 1536 struct ndis_shmem *sh; 1537 int error; 1538 1539 if (adapter == NULL) 1540 return; 1541 1542 block = (ndis_miniport_block *)adapter; 1543 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 1544 1545 sh = malloc(sizeof(struct ndis_shmem), M_DEVBUF, M_NOWAIT|M_ZERO); 1546 if (sh == NULL) 1547 return; 1548 1549 InitializeListHead(&sh->ndis_list); 1550 1551 /* 1552 * When performing shared memory allocations, create a tag 1553 * with a lowaddr limit that restricts physical memory mappings 1554 * so that they all fall within the first 1GB of memory. 1555 * At least one device/driver combination (Linksys Instant 1556 * Wireless PCI Card V2.7, Broadcom 802.11b) seems to have 1557 * problems with performing DMA operations with physical 1558 * addresses that lie above the 1GB mark. I don't know if this 1559 * is a hardware limitation or if the addresses are being 1560 * truncated within the driver, but this seems to be the only 1561 * way to make these cards work reliably in systems with more 1562 * than 1GB of physical memory. 1563 */ 1564 1565 error = bus_dma_tag_create(sc->ndis_parent_tag, 64, 1566 0, NDIS_BUS_SPACE_SHARED_MAXADDR, BUS_SPACE_MAXADDR, NULL, 1567 NULL, len, 1, len, BUS_DMA_ALLOCNOW, NULL, NULL, 1568 &sh->ndis_stag); 1569 1570 if (error) { 1571 free(sh, M_DEVBUF); 1572 return; 1573 } 1574 1575 error = bus_dmamem_alloc(sh->ndis_stag, vaddr, 1576 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sh->ndis_smap); 1577 1578 if (error) { 1579 bus_dma_tag_destroy(sh->ndis_stag); 1580 free(sh, M_DEVBUF); 1581 return; 1582 } 1583 1584 error = bus_dmamap_load(sh->ndis_stag, sh->ndis_smap, *vaddr, 1585 len, ndis_mapshared_cb, (void *)paddr, BUS_DMA_NOWAIT); 1586 1587 if (error) { 1588 bus_dmamem_free(sh->ndis_stag, *vaddr, sh->ndis_smap); 1589 bus_dma_tag_destroy(sh->ndis_stag); 1590 free(sh, M_DEVBUF); 1591 return; 1592 } 1593 1594 /* 1595 * Save the physical address along with the source address. 1596 * The AirGo MIMO driver will call NdisMFreeSharedMemory() 1597 * with a bogus virtual address sometimes, but with a valid 1598 * physical address. To keep this from causing trouble, we 1599 * use the physical address to as a sanity check in case 1600 * searching based on the virtual address fails. 1601 */ 1602 1603 NDIS_LOCK(sc); 1604 sh->ndis_paddr.np_quad = paddr->np_quad; 1605 sh->ndis_saddr = *vaddr; 1606 InsertHeadList((&sc->ndis_shlist), (&sh->ndis_list)); 1607 NDIS_UNLOCK(sc); 1608 1609 return; 1610} 1611 1612struct ndis_allocwork { 1613 uint32_t na_len; 1614 uint8_t na_cached; 1615 void *na_ctx; 1616 io_workitem *na_iw; 1617}; 1618 1619static void 1620ndis_asyncmem_complete(dobj, arg) 1621 device_object *dobj; 1622 void *arg; 1623{ 1624 ndis_miniport_block *block; 1625 struct ndis_softc *sc; 1626 struct ndis_allocwork *w; 1627 void *vaddr; 1628 ndis_physaddr paddr; 1629 ndis_allocdone_handler donefunc; 1630 1631 w = arg; 1632 block = (ndis_miniport_block *)dobj->do_devext; 1633 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 1634 1635 vaddr = NULL; 1636 paddr.np_quad = 0; 1637 1638 donefunc = sc->ndis_chars->nmc_allocate_complete_func; 1639 NdisMAllocateSharedMemory(block, w->na_len, 1640 w->na_cached, &vaddr, &paddr); 1641 MSCALL5(donefunc, block, vaddr, &paddr, w->na_len, w->na_ctx); 1642 1643 IoFreeWorkItem(w->na_iw); 1644 free(w, M_DEVBUF); 1645 1646 return; 1647} 1648 1649static ndis_status 1650NdisMAllocateSharedMemoryAsync(adapter, len, cached, ctx) 1651 ndis_handle adapter; 1652 uint32_t len; 1653 uint8_t cached; 1654 void *ctx; 1655{ 1656 ndis_miniport_block *block; 1657 struct ndis_allocwork *w; 1658 io_workitem *iw; 1659 io_workitem_func ifw; 1660 1661 if (adapter == NULL) 1662 return(NDIS_STATUS_FAILURE); 1663 1664 block = adapter; 1665 1666 iw = IoAllocateWorkItem(block->nmb_deviceobj); 1667 if (iw == NULL) 1668 return(NDIS_STATUS_FAILURE); 1669 1670 w = malloc(sizeof(struct ndis_allocwork), M_TEMP, M_NOWAIT); 1671 1672 if (w == NULL) 1673 return(NDIS_STATUS_FAILURE); 1674 1675 w->na_cached = cached; 1676 w->na_len = len; 1677 w->na_ctx = ctx; 1678 w->na_iw = iw; 1679 1680 ifw = (io_workitem_func)ndis_findwrap((funcptr)ndis_asyncmem_complete); 1681 IoQueueWorkItem(iw, ifw, WORKQUEUE_DELAYED, w); 1682 1683 return(NDIS_STATUS_PENDING); 1684} 1685 1686static void 1687NdisMFreeSharedMemory(adapter, len, cached, vaddr, paddr) 1688 ndis_handle adapter; 1689 uint32_t len; 1690 uint8_t cached; 1691 void *vaddr; 1692 ndis_physaddr paddr; 1693{ 1694 ndis_miniport_block *block; 1695 struct ndis_softc *sc; 1696 struct ndis_shmem *sh = NULL; 1697 list_entry *l; 1698 1699 if (vaddr == NULL || adapter == NULL) 1700 return; 1701 1702 block = (ndis_miniport_block *)adapter; 1703 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 1704 1705 /* Sanity check: is list empty? */ 1706 1707 if (IsListEmpty(&sc->ndis_shlist)) 1708 return; 1709 1710 NDIS_LOCK(sc); 1711 l = sc->ndis_shlist.nle_flink; 1712 while (l != &sc->ndis_shlist) { 1713 sh = CONTAINING_RECORD(l, struct ndis_shmem, ndis_list); 1714 if (sh->ndis_saddr == vaddr) 1715 break; 1716 /* 1717 * Check the physaddr too, just in case the driver lied 1718 * about the virtual address. 1719 */ 1720 if (sh->ndis_paddr.np_quad == paddr.np_quad) 1721 break; 1722 l = l->nle_flink; 1723 } 1724 1725 if (sh == NULL) { 1726 NDIS_UNLOCK(sc); 1727 printf("NDIS: buggy driver tried to free " 1728 "invalid shared memory: vaddr: %p paddr: 0x%jx\n", 1729 vaddr, (uintmax_t)paddr.np_quad); 1730 return; 1731 } 1732 1733 RemoveEntryList(&sh->ndis_list); 1734 1735 NDIS_UNLOCK(sc); 1736 1737 bus_dmamap_unload(sh->ndis_stag, sh->ndis_smap); 1738 bus_dmamem_free(sh->ndis_stag, sh->ndis_saddr, sh->ndis_smap); 1739 bus_dma_tag_destroy(sh->ndis_stag); 1740 1741 free(sh, M_DEVBUF); 1742 1743 return; 1744} 1745 1746static ndis_status 1747NdisMMapIoSpace(vaddr, adapter, paddr, len) 1748 void **vaddr; 1749 ndis_handle adapter; 1750 ndis_physaddr paddr; 1751 uint32_t len; 1752{ 1753 ndis_miniport_block *block; 1754 struct ndis_softc *sc; 1755 1756 if (adapter == NULL) 1757 return(NDIS_STATUS_FAILURE); 1758 1759 block = (ndis_miniport_block *)adapter; 1760 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 1761 1762 if (sc->ndis_res_mem != NULL && 1763 paddr.np_quad == rman_get_start(sc->ndis_res_mem)) 1764 *vaddr = (void *)rman_get_virtual(sc->ndis_res_mem); 1765 else if (sc->ndis_res_altmem != NULL && 1766 paddr.np_quad == rman_get_start(sc->ndis_res_altmem)) 1767 *vaddr = (void *)rman_get_virtual(sc->ndis_res_altmem); 1768 else if (sc->ndis_res_am != NULL && 1769 paddr.np_quad == rman_get_start(sc->ndis_res_am)) 1770 *vaddr = (void *)rman_get_virtual(sc->ndis_res_am); 1771 else 1772 return(NDIS_STATUS_FAILURE); 1773 1774 return(NDIS_STATUS_SUCCESS); 1775} 1776 1777static void 1778NdisMUnmapIoSpace(adapter, vaddr, len) 1779 ndis_handle adapter; 1780 void *vaddr; 1781 uint32_t len; 1782{ 1783 return; 1784} 1785 1786static uint32_t 1787NdisGetCacheFillSize(void) 1788{ 1789 return(128); 1790} 1791 1792static uint32_t 1793NdisMGetDmaAlignment(handle) 1794 ndis_handle handle; 1795{ 1796 return(16); 1797} 1798 1799/* 1800 * NDIS has two methods for dealing with NICs that support DMA. 1801 * One is to just pass packets to the driver and let it call 1802 * NdisMStartBufferPhysicalMapping() to map each buffer in the packet 1803 * all by itself, and the other is to let the NDIS library handle the 1804 * buffer mapping internally, and hand the driver an already populated 1805 * scatter/gather fragment list. If the driver calls 1806 * NdisMInitializeScatterGatherDma(), it wants to use the latter 1807 * method. 1808 */ 1809 1810static ndis_status 1811NdisMInitializeScatterGatherDma(adapter, is64, maxphysmap) 1812 ndis_handle adapter; 1813 uint8_t is64; 1814 uint32_t maxphysmap; 1815{ 1816 struct ndis_softc *sc; 1817 ndis_miniport_block *block; 1818 int error; 1819 1820 if (adapter == NULL) 1821 return(NDIS_STATUS_FAILURE); 1822 block = (ndis_miniport_block *)adapter; 1823 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 1824 1825 /* Don't do this twice. */ 1826 if (sc->ndis_sc == 1) 1827 return(NDIS_STATUS_SUCCESS); 1828 1829 error = bus_dma_tag_create(sc->ndis_parent_tag, ETHER_ALIGN, 0, 1830 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 1831 MCLBYTES * NDIS_MAXSEG, NDIS_MAXSEG, MCLBYTES, BUS_DMA_ALLOCNOW, 1832 NULL, NULL, &sc->ndis_ttag); 1833 1834 sc->ndis_sc = 1; 1835 1836 return(NDIS_STATUS_SUCCESS); 1837} 1838 1839void 1840NdisAllocatePacketPool(status, pool, descnum, protrsvdlen) 1841 ndis_status *status; 1842 ndis_handle *pool; 1843 uint32_t descnum; 1844 uint32_t protrsvdlen; 1845{ 1846 ndis_packet_pool *p; 1847 ndis_packet *packets; 1848 int i; 1849 1850 p = ExAllocatePoolWithTag(NonPagedPool, sizeof(ndis_packet_pool), 0); 1851 if (p == NULL) { 1852 *status = NDIS_STATUS_RESOURCES; 1853 return; 1854 } 1855 1856 p->np_cnt = descnum + NDIS_POOL_EXTRA; 1857 p->np_protrsvd = protrsvdlen; 1858 p->np_len = sizeof(ndis_packet) + protrsvdlen; 1859 1860 packets = ExAllocatePoolWithTag(NonPagedPool, p->np_cnt * 1861 p->np_len, 0); 1862 1863 1864 if (packets == NULL) { 1865 ExFreePool(p); 1866 *status = NDIS_STATUS_RESOURCES; 1867 return; 1868 } 1869 1870 p->np_pktmem = packets; 1871 1872 for (i = 0; i < p->np_cnt; i++) 1873 InterlockedPushEntrySList(&p->np_head, 1874 (struct slist_entry *)&packets[i]); 1875 1876#ifdef NDIS_DEBUG_PACKETS 1877 p->np_dead = 0; 1878 KeInitializeSpinLock(&p->np_lock); 1879 KeInitializeEvent(&p->np_event, EVENT_TYPE_NOTIFY, TRUE); 1880#endif 1881 1882 *pool = p; 1883 *status = NDIS_STATUS_SUCCESS; 1884 return; 1885} 1886 1887void 1888NdisAllocatePacketPoolEx(status, pool, descnum, oflowdescnum, protrsvdlen) 1889 ndis_status *status; 1890 ndis_handle *pool; 1891 uint32_t descnum; 1892 uint32_t oflowdescnum; 1893 uint32_t protrsvdlen; 1894{ 1895 return(NdisAllocatePacketPool(status, pool, 1896 descnum + oflowdescnum, protrsvdlen)); 1897} 1898 1899uint32_t 1900NdisPacketPoolUsage(pool) 1901 ndis_handle pool; 1902{ 1903 ndis_packet_pool *p; 1904 1905 p = (ndis_packet_pool *)pool; 1906 return(p->np_cnt - ExQueryDepthSList(&p->np_head)); 1907} 1908 1909void 1910NdisFreePacketPool(pool) 1911 ndis_handle pool; 1912{ 1913 ndis_packet_pool *p; 1914 int usage; 1915#ifdef NDIS_DEBUG_PACKETS 1916 uint8_t irql; 1917#endif 1918 1919 p = (ndis_packet_pool *)pool; 1920 1921#ifdef NDIS_DEBUG_PACKETS 1922 KeAcquireSpinLock(&p->np_lock, &irql); 1923#endif 1924 1925 usage = NdisPacketPoolUsage(pool); 1926 1927#ifdef NDIS_DEBUG_PACKETS 1928 if (usage) { 1929 p->np_dead = 1; 1930 KeResetEvent(&p->np_event); 1931 KeReleaseSpinLock(&p->np_lock, irql); 1932 KeWaitForSingleObject(&p->np_event, 0, 0, FALSE, NULL); 1933 } else 1934 KeReleaseSpinLock(&p->np_lock, irql); 1935#endif 1936 1937 ExFreePool(p->np_pktmem); 1938 ExFreePool(p); 1939 1940 return; 1941} 1942 1943void 1944NdisAllocatePacket(status, packet, pool) 1945 ndis_status *status; 1946 ndis_packet **packet; 1947 ndis_handle pool; 1948{ 1949 ndis_packet_pool *p; 1950 ndis_packet *pkt; 1951#ifdef NDIS_DEBUG_PACKETS 1952 uint8_t irql; 1953#endif 1954 1955 p = (ndis_packet_pool *)pool; 1956 1957#ifdef NDIS_DEBUG_PACKETS 1958 KeAcquireSpinLock(&p->np_lock, &irql); 1959 if (p->np_dead) { 1960 KeReleaseSpinLock(&p->np_lock, irql); 1961 printf("NDIS: tried to allocate packet from dead pool %p\n", 1962 pool); 1963 *status = NDIS_STATUS_RESOURCES; 1964 return; 1965 } 1966#endif 1967 1968 pkt = (ndis_packet *)InterlockedPopEntrySList(&p->np_head); 1969 1970#ifdef NDIS_DEBUG_PACKETS 1971 KeReleaseSpinLock(&p->np_lock, irql); 1972#endif 1973 1974 if (pkt == NULL) { 1975 *status = NDIS_STATUS_RESOURCES; 1976 return; 1977 } 1978 1979 1980 bzero((char *)pkt, sizeof(ndis_packet)); 1981 1982 /* Save pointer to the pool. */ 1983 pkt->np_private.npp_pool = pool; 1984 1985 /* Set the oob offset pointer. Lots of things expect this. */ 1986 pkt->np_private.npp_packetooboffset = offsetof(ndis_packet, np_oob); 1987 1988 /* 1989 * We must initialize the packet flags correctly in order 1990 * for the NDIS_SET_PACKET_MEDIA_SPECIFIC_INFO() and 1991 * NDIS_GET_PACKET_MEDIA_SPECIFIC_INFO() macros to work 1992 * correctly. 1993 */ 1994 pkt->np_private.npp_ndispktflags = NDIS_PACKET_ALLOCATED_BY_NDIS; 1995 pkt->np_private.npp_validcounts = FALSE; 1996 1997 *packet = pkt; 1998 1999 *status = NDIS_STATUS_SUCCESS; 2000 2001 return; 2002} 2003 2004void 2005NdisFreePacket(packet) 2006 ndis_packet *packet; 2007{ 2008 ndis_packet_pool *p; 2009#ifdef NDIS_DEBUG_PACKETS 2010 uint8_t irql; 2011#endif 2012 2013 p = (ndis_packet_pool *)packet->np_private.npp_pool; 2014 2015#ifdef NDIS_DEBUG_PACKETS 2016 KeAcquireSpinLock(&p->np_lock, &irql); 2017#endif 2018 2019 InterlockedPushEntrySList(&p->np_head, (slist_entry *)packet); 2020 2021#ifdef NDIS_DEBUG_PACKETS 2022 if (p->np_dead) { 2023 if (ExQueryDepthSList(&p->np_head) == p->np_cnt) 2024 KeSetEvent(&p->np_event, IO_NO_INCREMENT, FALSE); 2025 } 2026 KeReleaseSpinLock(&p->np_lock, irql); 2027#endif 2028 2029 return; 2030} 2031 2032static void 2033NdisUnchainBufferAtFront(packet, buf) 2034 ndis_packet *packet; 2035 ndis_buffer **buf; 2036{ 2037 ndis_packet_private *priv; 2038 2039 if (packet == NULL || buf == NULL) 2040 return; 2041 2042 priv = &packet->np_private; 2043 2044 priv->npp_validcounts = FALSE; 2045 2046 if (priv->npp_head == priv->npp_tail) { 2047 *buf = priv->npp_head; 2048 priv->npp_head = priv->npp_tail = NULL; 2049 } else { 2050 *buf = priv->npp_head; 2051 priv->npp_head = (*buf)->mdl_next; 2052 } 2053 2054 return; 2055} 2056 2057static void 2058NdisUnchainBufferAtBack(packet, buf) 2059 ndis_packet *packet; 2060 ndis_buffer **buf; 2061{ 2062 ndis_packet_private *priv; 2063 ndis_buffer *tmp; 2064 2065 if (packet == NULL || buf == NULL) 2066 return; 2067 2068 priv = &packet->np_private; 2069 2070 priv->npp_validcounts = FALSE; 2071 2072 if (priv->npp_head == priv->npp_tail) { 2073 *buf = priv->npp_head; 2074 priv->npp_head = priv->npp_tail = NULL; 2075 } else { 2076 *buf = priv->npp_tail; 2077 tmp = priv->npp_head; 2078 while (tmp->mdl_next != priv->npp_tail) 2079 tmp = tmp->mdl_next; 2080 priv->npp_tail = tmp; 2081 tmp->mdl_next = NULL; 2082 } 2083 2084 return; 2085} 2086 2087/* 2088 * The NDIS "buffer" is really an MDL (memory descriptor list) 2089 * which is used to describe a buffer in a way that allows it 2090 * to mapped into different contexts. We have to be careful how 2091 * we handle them: in some versions of Windows, the NdisFreeBuffer() 2092 * routine is an actual function in the NDIS API, but in others 2093 * it's just a macro wrapper around IoFreeMdl(). There's really 2094 * no way to use the 'descnum' parameter to count how many 2095 * "buffers" are allocated since in order to use IoFreeMdl() to 2096 * dispose of a buffer, we have to use IoAllocateMdl() to allocate 2097 * them, and IoAllocateMdl() just grabs them out of the heap. 2098 */ 2099 2100static void 2101NdisAllocateBufferPool(status, pool, descnum) 2102 ndis_status *status; 2103 ndis_handle *pool; 2104 uint32_t descnum; 2105{ 2106 2107 /* 2108 * The only thing we can really do here is verify that descnum 2109 * is a reasonable value, but I really don't know what to check 2110 * it against. 2111 */ 2112 2113 *pool = NonPagedPool; 2114 *status = NDIS_STATUS_SUCCESS; 2115 return; 2116} 2117 2118static void 2119NdisFreeBufferPool(pool) 2120 ndis_handle pool; 2121{ 2122 return; 2123} 2124 2125static void 2126NdisAllocateBuffer(status, buffer, pool, vaddr, len) 2127 ndis_status *status; 2128 ndis_buffer **buffer; 2129 ndis_handle pool; 2130 void *vaddr; 2131 uint32_t len; 2132{ 2133 ndis_buffer *buf; 2134 2135 buf = IoAllocateMdl(vaddr, len, FALSE, FALSE, NULL); 2136 if (buf == NULL) { 2137 *status = NDIS_STATUS_RESOURCES; 2138 return; 2139 } 2140 2141 MmBuildMdlForNonPagedPool(buf); 2142 2143 *buffer = buf; 2144 *status = NDIS_STATUS_SUCCESS; 2145 2146 return; 2147} 2148 2149static void 2150NdisFreeBuffer(buf) 2151 ndis_buffer *buf; 2152{ 2153 IoFreeMdl(buf); 2154 return; 2155} 2156 2157/* Aw c'mon. */ 2158 2159static uint32_t 2160NdisBufferLength(buf) 2161 ndis_buffer *buf; 2162{ 2163 return(MmGetMdlByteCount(buf)); 2164} 2165 2166/* 2167 * Get the virtual address and length of a buffer. 2168 * Note: the vaddr argument is optional. 2169 */ 2170 2171static void 2172NdisQueryBuffer(buf, vaddr, len) 2173 ndis_buffer *buf; 2174 void **vaddr; 2175 uint32_t *len; 2176{ 2177 if (vaddr != NULL) 2178 *vaddr = MmGetMdlVirtualAddress(buf); 2179 *len = MmGetMdlByteCount(buf); 2180 2181 return; 2182} 2183 2184/* Same as above -- we don't care about the priority. */ 2185 2186static void 2187NdisQueryBufferSafe(buf, vaddr, len, prio) 2188 ndis_buffer *buf; 2189 void **vaddr; 2190 uint32_t *len; 2191 uint32_t prio; 2192{ 2193 if (vaddr != NULL) 2194 *vaddr = MmGetMdlVirtualAddress(buf); 2195 *len = MmGetMdlByteCount(buf); 2196 2197 return; 2198} 2199 2200/* Damnit Microsoft!! How many ways can you do the same thing?! */ 2201 2202static void * 2203NdisBufferVirtualAddress(buf) 2204 ndis_buffer *buf; 2205{ 2206 return(MmGetMdlVirtualAddress(buf)); 2207} 2208 2209static void * 2210NdisBufferVirtualAddressSafe(buf, prio) 2211 ndis_buffer *buf; 2212 uint32_t prio; 2213{ 2214 return(MmGetMdlVirtualAddress(buf)); 2215} 2216 2217static void 2218NdisAdjustBufferLength(buf, len) 2219 ndis_buffer *buf; 2220 int len; 2221{ 2222 MmGetMdlByteCount(buf) = len; 2223 2224 return; 2225} 2226 2227static uint32_t 2228NdisInterlockedIncrement(addend) 2229 uint32_t *addend; 2230{ 2231 atomic_add_long((u_long *)addend, 1); 2232 return(*addend); 2233} 2234 2235static uint32_t 2236NdisInterlockedDecrement(addend) 2237 uint32_t *addend; 2238{ 2239 atomic_subtract_long((u_long *)addend, 1); 2240 return(*addend); 2241} 2242 2243static void 2244NdisInitializeEvent(event) 2245 ndis_event *event; 2246{ 2247 /* 2248 * NDIS events are always notification 2249 * events, and should be initialized to the 2250 * not signaled state. 2251 */ 2252 KeInitializeEvent(&event->ne_event, EVENT_TYPE_NOTIFY, FALSE); 2253 return; 2254} 2255 2256static void 2257NdisSetEvent(event) 2258 ndis_event *event; 2259{ 2260 KeSetEvent(&event->ne_event, IO_NO_INCREMENT, FALSE); 2261 return; 2262} 2263 2264static void 2265NdisResetEvent(event) 2266 ndis_event *event; 2267{ 2268 KeResetEvent(&event->ne_event); 2269 return; 2270} 2271 2272static uint8_t 2273NdisWaitEvent(event, msecs) 2274 ndis_event *event; 2275 uint32_t msecs; 2276{ 2277 int64_t duetime; 2278 uint32_t rval; 2279 2280 duetime = ((int64_t)msecs * -10000); 2281 rval = KeWaitForSingleObject(event, 2282 0, 0, TRUE, msecs ? & duetime : NULL); 2283 2284 if (rval == STATUS_TIMEOUT) 2285 return(FALSE); 2286 2287 return(TRUE); 2288} 2289 2290static ndis_status 2291NdisUnicodeStringToAnsiString(dstr, sstr) 2292 ansi_string *dstr; 2293 unicode_string *sstr; 2294{ 2295 uint32_t rval; 2296 2297 rval = RtlUnicodeStringToAnsiString(dstr, sstr, FALSE); 2298 2299 if (rval == STATUS_INSUFFICIENT_RESOURCES) 2300 return(NDIS_STATUS_RESOURCES); 2301 if (rval) 2302 return(NDIS_STATUS_FAILURE); 2303 2304 return (NDIS_STATUS_SUCCESS); 2305} 2306 2307static ndis_status 2308NdisAnsiStringToUnicodeString(dstr, sstr) 2309 unicode_string *dstr; 2310 ansi_string *sstr; 2311{ 2312 uint32_t rval; 2313 2314 rval = RtlAnsiStringToUnicodeString(dstr, sstr, FALSE); 2315 2316 if (rval == STATUS_INSUFFICIENT_RESOURCES) 2317 return(NDIS_STATUS_RESOURCES); 2318 if (rval) 2319 return(NDIS_STATUS_FAILURE); 2320 2321 return (NDIS_STATUS_SUCCESS); 2322} 2323 2324static ndis_status 2325NdisMPciAssignResources(adapter, slot, list) 2326 ndis_handle adapter; 2327 uint32_t slot; 2328 ndis_resource_list **list; 2329{ 2330 ndis_miniport_block *block; 2331 2332 if (adapter == NULL || list == NULL) 2333 return (NDIS_STATUS_FAILURE); 2334 2335 block = (ndis_miniport_block *)adapter; 2336 *list = block->nmb_rlist; 2337 2338 return (NDIS_STATUS_SUCCESS); 2339} 2340 2341static void 2342ndis_intr(arg) 2343 void *arg; 2344{ 2345 struct ndis_softc *sc; 2346 struct ifnet *ifp; 2347 int is_our_intr = 0; 2348 int call_isr = 0; 2349 ndis_miniport_interrupt *intr; 2350 2351 sc = arg; 2352 ifp = sc->ifp; 2353 intr = sc->ndis_block->nmb_interrupt; 2354 2355 if (intr == NULL || sc->ndis_block->nmb_miniportadapterctx == NULL) 2356 return; 2357 2358 if (sc->ndis_block->nmb_interrupt->ni_isrreq == TRUE) 2359 ndis_isr(sc, &is_our_intr, &call_isr); 2360 else { 2361 ndis_disable_intr(sc); 2362 call_isr = 1; 2363 } 2364 2365 if ((is_our_intr || call_isr)) 2366 IoRequestDpc(sc->ndis_block->nmb_deviceobj, NULL, sc); 2367 2368 return; 2369} 2370 2371static void 2372ndis_intrhand(dpc, intr, sysarg1, sysarg2) 2373 kdpc *dpc; 2374 ndis_miniport_interrupt *intr; 2375 void *sysarg1; 2376 void *sysarg2; 2377{ 2378 struct ndis_softc *sc; 2379 ndis_miniport_block *block; 2380 ndis_handle adapter; 2381 2382 block = intr->ni_block; 2383 adapter = block->nmb_miniportadapterctx; 2384 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 2385 2386 if (NDIS_SERIALIZED(sc->ndis_block)) 2387 KeAcquireSpinLockAtDpcLevel(&block->nmb_lock); 2388 2389 MSCALL1(intr->ni_isrfunc, adapter); 2390 2391 /* If there's a MiniportEnableInterrupt() routine, call it. */ 2392 2393 ndis_enable_intr(sc); 2394 2395 if (NDIS_SERIALIZED(sc->ndis_block)) 2396 KeReleaseSpinLockFromDpcLevel(&block->nmb_lock); 2397 2398 /* 2399 * Set the completion event if we've drained all 2400 * pending interrupts. 2401 */ 2402 2403 KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock); 2404 intr->ni_dpccnt--; 2405 if (intr->ni_dpccnt == 0) 2406 KeSetEvent(&intr->ni_dpcevt, IO_NO_INCREMENT, FALSE); 2407 KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock); 2408 2409 return; 2410} 2411 2412static ndis_status 2413NdisMRegisterInterrupt(intr, adapter, ivec, ilevel, reqisr, shared, imode) 2414 ndis_miniport_interrupt *intr; 2415 ndis_handle adapter; 2416 uint32_t ivec; 2417 uint32_t ilevel; 2418 uint8_t reqisr; 2419 uint8_t shared; 2420 ndis_interrupt_mode imode; 2421{ 2422 ndis_miniport_block *block; 2423 ndis_miniport_characteristics *ch; 2424 struct ndis_softc *sc; 2425 int error; 2426 2427 block = adapter; 2428 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 2429 ch = IoGetDriverObjectExtension(block->nmb_deviceobj->do_drvobj, 2430 (void *)1); 2431 2432 intr->ni_rsvd = ExAllocatePoolWithTag(NonPagedPool, 2433 sizeof(struct mtx), 0); 2434 if (intr->ni_rsvd == NULL) 2435 return(NDIS_STATUS_RESOURCES); 2436 2437 intr->ni_block = adapter; 2438 intr->ni_isrreq = reqisr; 2439 intr->ni_shared = shared; 2440 intr->ni_dpccnt = 0; 2441 intr->ni_isrfunc = ch->nmc_interrupt_func; 2442 intr->ni_dpcfunc = ch->nmc_isr_func; 2443 2444 KeInitializeEvent(&intr->ni_dpcevt, EVENT_TYPE_NOTIFY, TRUE); 2445 KeInitializeDpc(&intr->ni_dpc, 2446 ndis_findwrap((funcptr)ndis_intrhand), intr); 2447 KeSetImportanceDpc(&intr->ni_dpc, KDPC_IMPORTANCE_LOW); 2448 2449 error = IoConnectInterrupt(&intr->ni_introbj, 2450 ndis_findwrap((funcptr)ndis_intr), sc, NULL, 2451 ivec, ilevel, 0, imode, shared, 0, FALSE); 2452 2453 if (error != STATUS_SUCCESS) 2454 return(NDIS_STATUS_FAILURE); 2455 2456 block->nmb_interrupt = intr; 2457 2458 return(NDIS_STATUS_SUCCESS); 2459} 2460 2461static void 2462NdisMDeregisterInterrupt(intr) 2463 ndis_miniport_interrupt *intr; 2464{ 2465 ndis_miniport_block *block; 2466 struct ndis_softc *sc; 2467 uint8_t irql; 2468 2469 block = intr->ni_block; 2470 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 2471 2472 /* Should really be KeSynchronizeExecution() */ 2473 2474 KeAcquireSpinLock(intr->ni_introbj->ki_lock, &irql); 2475 block->nmb_interrupt = NULL; 2476 KeReleaseSpinLock(intr->ni_introbj->ki_lock, irql); 2477/* 2478 KeFlushQueuedDpcs(); 2479*/ 2480 /* Disconnect our ISR */ 2481 2482 IoDisconnectInterrupt(intr->ni_introbj); 2483 2484 KeWaitForSingleObject(&intr->ni_dpcevt, 0, 0, FALSE, NULL); 2485 KeResetEvent(&intr->ni_dpcevt); 2486 2487 return; 2488} 2489 2490static void 2491NdisMRegisterAdapterShutdownHandler(adapter, shutdownctx, shutdownfunc) 2492 ndis_handle adapter; 2493 void *shutdownctx; 2494 ndis_shutdown_handler shutdownfunc; 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 = shutdownfunc; 2508 chars->nmc_rsvd0 = shutdownctx; 2509 2510 return; 2511} 2512 2513static void 2514NdisMDeregisterAdapterShutdownHandler(adapter) 2515 ndis_handle adapter; 2516{ 2517 ndis_miniport_block *block; 2518 ndis_miniport_characteristics *chars; 2519 struct ndis_softc *sc; 2520 2521 if (adapter == NULL) 2522 return; 2523 2524 block = (ndis_miniport_block *)adapter; 2525 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 2526 chars = sc->ndis_chars; 2527 2528 chars->nmc_shutdown_handler = NULL; 2529 chars->nmc_rsvd0 = NULL; 2530 2531 return; 2532} 2533 2534static uint32_t 2535NDIS_BUFFER_TO_SPAN_PAGES(buf) 2536 ndis_buffer *buf; 2537{ 2538 if (buf == NULL) 2539 return(0); 2540 if (MmGetMdlByteCount(buf) == 0) 2541 return(1); 2542 return(SPAN_PAGES(MmGetMdlVirtualAddress(buf), 2543 MmGetMdlByteCount(buf))); 2544} 2545 2546static void 2547NdisGetBufferPhysicalArraySize(buf, pages) 2548 ndis_buffer *buf; 2549 uint32_t *pages; 2550{ 2551 if (buf == NULL) 2552 return; 2553 2554 *pages = NDIS_BUFFER_TO_SPAN_PAGES(buf); 2555 return; 2556} 2557 2558static void 2559NdisQueryBufferOffset(buf, off, len) 2560 ndis_buffer *buf; 2561 uint32_t *off; 2562 uint32_t *len; 2563{ 2564 if (buf == NULL) 2565 return; 2566 2567 *off = MmGetMdlByteOffset(buf); 2568 *len = MmGetMdlByteCount(buf); 2569 2570 return; 2571} 2572 2573void 2574NdisMSleep(usecs) 2575 uint32_t usecs; 2576{ 2577 ktimer timer; 2578 2579 /* 2580 * During system bootstrap, (i.e. cold == 1), we aren't 2581 * allowed to sleep, so we have to do a hard DELAY() 2582 * instead. 2583 */ 2584 2585 if (cold) 2586 DELAY(usecs); 2587 else { 2588 KeInitializeTimer(&timer); 2589 KeSetTimer(&timer, ((int64_t)usecs * -10), NULL); 2590 KeWaitForSingleObject(&timer, 0, 0, FALSE, NULL); 2591 } 2592 2593 return; 2594} 2595 2596static uint32_t 2597NdisReadPcmciaAttributeMemory(handle, offset, buf, len) 2598 ndis_handle handle; 2599 uint32_t offset; 2600 void *buf; 2601 uint32_t len; 2602{ 2603 struct ndis_softc *sc; 2604 ndis_miniport_block *block; 2605 bus_space_handle_t bh; 2606 bus_space_tag_t bt; 2607 char *dest; 2608 int i; 2609 2610 if (handle == NULL) 2611 return(0); 2612 2613 block = (ndis_miniport_block *)handle; 2614 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 2615 dest = buf; 2616 2617 bh = rman_get_bushandle(sc->ndis_res_am); 2618 bt = rman_get_bustag(sc->ndis_res_am); 2619 2620 for (i = 0; i < len; i++) 2621 dest[i] = bus_space_read_1(bt, bh, (offset + i) * 2); 2622 2623 return(i); 2624} 2625 2626static uint32_t 2627NdisWritePcmciaAttributeMemory(handle, offset, buf, len) 2628 ndis_handle handle; 2629 uint32_t offset; 2630 void *buf; 2631 uint32_t len; 2632{ 2633 struct ndis_softc *sc; 2634 ndis_miniport_block *block; 2635 bus_space_handle_t bh; 2636 bus_space_tag_t bt; 2637 char *src; 2638 int i; 2639 2640 if (handle == NULL) 2641 return(0); 2642 2643 block = (ndis_miniport_block *)handle; 2644 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 2645 src = buf; 2646 2647 bh = rman_get_bushandle(sc->ndis_res_am); 2648 bt = rman_get_bustag(sc->ndis_res_am); 2649 2650 for (i = 0; i < len; i++) 2651 bus_space_write_1(bt, bh, (offset + i) * 2, src[i]); 2652 2653 return(i); 2654} 2655 2656static list_entry * 2657NdisInterlockedInsertHeadList(head, entry, lock) 2658 list_entry *head; 2659 list_entry *entry; 2660 ndis_spin_lock *lock; 2661{ 2662 list_entry *flink; 2663 2664 KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql); 2665 flink = head->nle_flink; 2666 entry->nle_flink = flink; 2667 entry->nle_blink = head; 2668 flink->nle_blink = entry; 2669 head->nle_flink = entry; 2670 KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql); 2671 2672 return(flink); 2673} 2674 2675static list_entry * 2676NdisInterlockedRemoveHeadList(head, lock) 2677 list_entry *head; 2678 ndis_spin_lock *lock; 2679{ 2680 list_entry *flink; 2681 list_entry *entry; 2682 2683 KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql); 2684 entry = head->nle_flink; 2685 flink = entry->nle_flink; 2686 head->nle_flink = flink; 2687 flink->nle_blink = head; 2688 KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql); 2689 2690 return(entry); 2691} 2692 2693static list_entry * 2694NdisInterlockedInsertTailList(head, entry, lock) 2695 list_entry *head; 2696 list_entry *entry; 2697 ndis_spin_lock *lock; 2698{ 2699 list_entry *blink; 2700 2701 KeAcquireSpinLock(&lock->nsl_spinlock, &lock->nsl_kirql); 2702 blink = head->nle_blink; 2703 entry->nle_flink = head; 2704 entry->nle_blink = blink; 2705 blink->nle_flink = entry; 2706 head->nle_blink = entry; 2707 KeReleaseSpinLock(&lock->nsl_spinlock, lock->nsl_kirql); 2708 2709 return(blink); 2710} 2711 2712static uint8_t 2713NdisMSynchronizeWithInterrupt(intr, syncfunc, syncctx) 2714 ndis_miniport_interrupt *intr; 2715 void *syncfunc; 2716 void *syncctx; 2717{ 2718 return(KeSynchronizeExecution(intr->ni_introbj, syncfunc, syncctx)); 2719} 2720 2721/* 2722 * Return the number of 100 nanosecond intervals since 2723 * January 1, 1601. (?!?!) 2724 */ 2725static void 2726NdisGetCurrentSystemTime(tval) 2727 uint64_t *tval; 2728{ 2729 struct timespec ts; 2730 2731 nanotime(&ts); 2732 *tval = (uint64_t)ts.tv_nsec / 100 + (uint64_t)ts.tv_sec * 10000000 + 2733 11644473600; 2734 2735 return; 2736} 2737 2738/* 2739 * Return the number of milliseconds since the system booted. 2740 */ 2741static void 2742NdisGetSystemUpTime(tval) 2743 uint32_t *tval; 2744{ 2745 struct timespec ts; 2746 2747 nanouptime(&ts); 2748 *tval = ts.tv_nsec / 1000000 + ts.tv_sec * 1000; 2749 2750 return; 2751} 2752 2753static void 2754NdisInitializeString(dst, src) 2755 unicode_string *dst; 2756 char *src; 2757{ 2758 ansi_string as; 2759 RtlInitAnsiString(&as, src); 2760 RtlAnsiStringToUnicodeString(dst, &as, TRUE); 2761 return; 2762} 2763 2764static void 2765NdisFreeString(str) 2766 unicode_string *str; 2767{ 2768 RtlFreeUnicodeString(str); 2769 return; 2770} 2771 2772static ndis_status 2773NdisMRemoveMiniport(adapter) 2774 ndis_handle *adapter; 2775{ 2776 return(NDIS_STATUS_SUCCESS); 2777} 2778 2779static void 2780NdisInitAnsiString(dst, src) 2781 ansi_string *dst; 2782 char *src; 2783{ 2784 RtlInitAnsiString(dst, src); 2785 return; 2786} 2787 2788static void 2789NdisInitUnicodeString(dst, src) 2790 unicode_string *dst; 2791 uint16_t *src; 2792{ 2793 RtlInitUnicodeString(dst, src); 2794 return; 2795} 2796 2797static void NdisMGetDeviceProperty(adapter, phydevobj, 2798 funcdevobj, nextdevobj, resources, transresources) 2799 ndis_handle adapter; 2800 device_object **phydevobj; 2801 device_object **funcdevobj; 2802 device_object **nextdevobj; 2803 cm_resource_list *resources; 2804 cm_resource_list *transresources; 2805{ 2806 ndis_miniport_block *block; 2807 2808 block = (ndis_miniport_block *)adapter; 2809 2810 if (phydevobj != NULL) 2811 *phydevobj = block->nmb_physdeviceobj; 2812 if (funcdevobj != NULL) 2813 *funcdevobj = block->nmb_deviceobj; 2814 if (nextdevobj != NULL) 2815 *nextdevobj = block->nmb_nextdeviceobj; 2816 2817 return; 2818} 2819 2820static void 2821NdisGetFirstBufferFromPacket(packet, buf, firstva, firstlen, totlen) 2822 ndis_packet *packet; 2823 ndis_buffer **buf; 2824 void **firstva; 2825 uint32_t *firstlen; 2826 uint32_t *totlen; 2827{ 2828 ndis_buffer *tmp; 2829 2830 tmp = packet->np_private.npp_head; 2831 *buf = tmp; 2832 if (tmp == NULL) { 2833 *firstva = NULL; 2834 *firstlen = *totlen = 0; 2835 } else { 2836 *firstva = MmGetMdlVirtualAddress(tmp); 2837 *firstlen = *totlen = MmGetMdlByteCount(tmp); 2838 for (tmp = tmp->mdl_next; tmp != NULL; tmp = tmp->mdl_next) 2839 *totlen += MmGetMdlByteCount(tmp); 2840 } 2841 2842 return; 2843} 2844 2845static void 2846NdisGetFirstBufferFromPacketSafe(packet, buf, firstva, firstlen, totlen, prio) 2847 ndis_packet *packet; 2848 ndis_buffer **buf; 2849 void **firstva; 2850 uint32_t *firstlen; 2851 uint32_t *totlen; 2852 uint32_t prio; 2853{ 2854 NdisGetFirstBufferFromPacket(packet, buf, firstva, firstlen, totlen); 2855} 2856 2857static int 2858ndis_find_sym(lf, filename, suffix, sym) 2859 linker_file_t lf; 2860 char *filename; 2861 char *suffix; 2862 caddr_t *sym; 2863{ 2864 char *fullsym; 2865 char *suf; 2866 int i; 2867 2868 fullsym = ExAllocatePoolWithTag(NonPagedPool, MAXPATHLEN, 0); 2869 if (fullsym == NULL) 2870 return(ENOMEM); 2871 2872 bzero(fullsym, MAXPATHLEN); 2873 strncpy(fullsym, filename, MAXPATHLEN); 2874 if (strlen(filename) < 4) { 2875 ExFreePool(fullsym); 2876 return(EINVAL); 2877 } 2878 2879 /* If the filename has a .ko suffix, strip if off. */ 2880 suf = fullsym + (strlen(filename) - 3); 2881 if (strcmp(suf, ".ko") == 0) 2882 *suf = '\0'; 2883 2884 for (i = 0; i < strlen(fullsym); i++) { 2885 if (fullsym[i] == '.') 2886 fullsym[i] = '_'; 2887 else 2888 fullsym[i] = tolower(fullsym[i]); 2889 } 2890 strcat(fullsym, suffix); 2891 *sym = linker_file_lookup_symbol(lf, fullsym, 0); 2892 ExFreePool(fullsym); 2893 if (*sym == 0) 2894 return(ENOENT); 2895 2896 return(0); 2897} 2898 2899/* can also return NDIS_STATUS_RESOURCES/NDIS_STATUS_ERROR_READING_FILE */ 2900static void 2901NdisOpenFile(status, filehandle, filelength, filename, highestaddr) 2902 ndis_status *status; 2903 ndis_handle *filehandle; 2904 uint32_t *filelength; 2905 unicode_string *filename; 2906 ndis_physaddr highestaddr; 2907{ 2908 ansi_string as; 2909 char *afilename = NULL; 2910 struct thread *td = curthread; 2911 struct nameidata nd; 2912 int flags, error; 2913 struct vattr vat; 2914 struct vattr *vap = &vat; 2915 ndis_fh *fh; 2916 char *path; 2917 linker_file_t head, lf; 2918 caddr_t kldstart, kldend; 2919 2920 if (RtlUnicodeStringToAnsiString(&as, filename, TRUE)) { 2921 *status = NDIS_STATUS_RESOURCES; 2922 return; 2923 } 2924 2925 afilename = strdup(as.as_buf, M_DEVBUF); 2926 RtlFreeAnsiString(&as); 2927 2928 fh = ExAllocatePoolWithTag(NonPagedPool, sizeof(ndis_fh), 0); 2929 if (fh == NULL) { 2930 free(afilename, M_DEVBUF); 2931 *status = NDIS_STATUS_RESOURCES; 2932 return; 2933 } 2934 2935 fh->nf_name = afilename; 2936 2937 /* 2938 * During system bootstrap, it's impossible to load files 2939 * from the rootfs since it's not mounted yet. We therefore 2940 * offer the possibility of opening files that have been 2941 * preloaded as modules instead. Both choices will work 2942 * when kldloading a module from multiuser, but only the 2943 * module option will work during bootstrap. The module 2944 * loading option works by using the ndiscvt(8) utility 2945 * to convert the arbitrary file into a .ko using objcopy(1). 2946 * This file will contain two special symbols: filename_start 2947 * and filename_end. All we have to do is traverse the KLD 2948 * list in search of those symbols and we've found the file 2949 * data. As an added bonus, ndiscvt(8) will also generate 2950 * a normal .o file which can be linked statically with 2951 * the kernel. This means that the symbols will actual reside 2952 * in the kernel's symbol table, but that doesn't matter to 2953 * us since the kernel appears to us as just another module. 2954 */ 2955 2956 /* 2957 * This is an evil trick for getting the head of the linked 2958 * file list, which is not exported from kern_linker.o. It 2959 * happens that linker file #1 is always the kernel, and is 2960 * always the first element in the list. 2961 */ 2962 2963 head = linker_find_file_by_id(1); 2964 for (lf = head; lf != NULL; lf = TAILQ_NEXT(lf, link)) { 2965 if (ndis_find_sym(lf, afilename, "_start", &kldstart)) 2966 continue; 2967 if (ndis_find_sym(lf, afilename, "_end", &kldend)) 2968 continue; 2969 fh->nf_vp = lf; 2970 fh->nf_map = NULL; 2971 fh->nf_type = NDIS_FH_TYPE_MODULE; 2972 *filelength = fh->nf_maplen = (kldend - kldstart) & 0xFFFFFFFF; 2973 *filehandle = fh; 2974 *status = NDIS_STATUS_SUCCESS; 2975 return; 2976 } 2977 2978 if (TAILQ_EMPTY(&mountlist)) { 2979 ExFreePool(fh); 2980 *status = NDIS_STATUS_FILE_NOT_FOUND; 2981 printf("NDIS: could not find file %s in linker list\n", 2982 afilename); 2983 printf("NDIS: and no filesystems mounted yet, " 2984 "aborting NdisOpenFile()\n"); 2985 free(afilename, M_DEVBUF); 2986 return; 2987 } 2988 2989 path = ExAllocatePoolWithTag(NonPagedPool, MAXPATHLEN, 0); 2990 if (path == NULL) { 2991 ExFreePool(fh); 2992 free(afilename, M_DEVBUF); 2993 *status = NDIS_STATUS_RESOURCES; 2994 return; 2995 } 2996 2997 snprintf(path, MAXPATHLEN, "%s/%s", ndis_filepath, afilename); 2998 2999 mtx_lock(&Giant); 3000 3001 /* Some threads don't have a current working directory. */ 3002 3003 if (td->td_proc->p_fd->fd_rdir == NULL) 3004 td->td_proc->p_fd->fd_rdir = rootvnode; 3005 if (td->td_proc->p_fd->fd_cdir == NULL) 3006 td->td_proc->p_fd->fd_cdir = rootvnode; 3007 3008 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td); 3009 3010 flags = FREAD; 3011 error = vn_open(&nd, &flags, 0, -1); 3012 if (error) { 3013 mtx_unlock(&Giant); 3014 *status = NDIS_STATUS_FILE_NOT_FOUND; 3015 ExFreePool(fh); 3016 printf("NDIS: open file %s failed: %d\n", path, error); 3017 ExFreePool(path); 3018 free(afilename, M_DEVBUF); 3019 return; 3020 } 3021 3022 ExFreePool(path); 3023 3024 NDFREE(&nd, NDF_ONLY_PNBUF); 3025 3026 /* Get the file size. */ 3027 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred, td); 3028 VOP_UNLOCK(nd.ni_vp, 0, td); 3029 mtx_unlock(&Giant); 3030 3031 fh->nf_vp = nd.ni_vp; 3032 fh->nf_map = NULL; 3033 fh->nf_type = NDIS_FH_TYPE_VFS; 3034 *filehandle = fh; 3035 *filelength = fh->nf_maplen = vap->va_size & 0xFFFFFFFF; 3036 *status = NDIS_STATUS_SUCCESS; 3037 3038 return; 3039} 3040 3041static void 3042NdisMapFile(status, mappedbuffer, filehandle) 3043 ndis_status *status; 3044 void **mappedbuffer; 3045 ndis_handle filehandle; 3046{ 3047 ndis_fh *fh; 3048 struct thread *td = curthread; 3049 linker_file_t lf; 3050 caddr_t kldstart; 3051 int error, resid; 3052 3053 if (filehandle == NULL) { 3054 *status = NDIS_STATUS_FAILURE; 3055 return; 3056 } 3057 3058 fh = (ndis_fh *)filehandle; 3059 3060 if (fh->nf_vp == NULL) { 3061 *status = NDIS_STATUS_FAILURE; 3062 return; 3063 } 3064 3065 if (fh->nf_map != NULL) { 3066 *status = NDIS_STATUS_ALREADY_MAPPED; 3067 return; 3068 } 3069 3070 if (fh->nf_type == NDIS_FH_TYPE_MODULE) { 3071 lf = fh->nf_vp; 3072 if (ndis_find_sym(lf, fh->nf_name, "_start", &kldstart)) { 3073 *status = NDIS_STATUS_FAILURE; 3074 return; 3075 } 3076 fh->nf_map = kldstart; 3077 *status = NDIS_STATUS_SUCCESS; 3078 *mappedbuffer = fh->nf_map; 3079 return; 3080 } 3081 3082 fh->nf_map = ExAllocatePoolWithTag(NonPagedPool, fh->nf_maplen, 0); 3083 3084 if (fh->nf_map == NULL) { 3085 *status = NDIS_STATUS_RESOURCES; 3086 return; 3087 } 3088 3089 mtx_lock(&Giant); 3090 error = vn_rdwr(UIO_READ, fh->nf_vp, fh->nf_map, fh->nf_maplen, 0, 3091 UIO_SYSSPACE, 0, td->td_ucred, NOCRED, &resid, td); 3092 mtx_unlock(&Giant); 3093 3094 if (error) 3095 *status = NDIS_STATUS_FAILURE; 3096 else { 3097 *status = NDIS_STATUS_SUCCESS; 3098 *mappedbuffer = fh->nf_map; 3099 } 3100 3101 return; 3102} 3103 3104static void 3105NdisUnmapFile(filehandle) 3106 ndis_handle filehandle; 3107{ 3108 ndis_fh *fh; 3109 fh = (ndis_fh *)filehandle; 3110 3111 if (fh->nf_map == NULL) 3112 return; 3113 3114 if (fh->nf_type == NDIS_FH_TYPE_VFS) 3115 ExFreePool(fh->nf_map); 3116 fh->nf_map = NULL; 3117 3118 return; 3119} 3120 3121static void 3122NdisCloseFile(filehandle) 3123 ndis_handle filehandle; 3124{ 3125 struct thread *td = curthread; 3126 ndis_fh *fh; 3127 3128 if (filehandle == NULL) 3129 return; 3130 3131 fh = (ndis_fh *)filehandle; 3132 if (fh->nf_map != NULL) { 3133 if (fh->nf_type == NDIS_FH_TYPE_VFS) 3134 ExFreePool(fh->nf_map); 3135 fh->nf_map = NULL; 3136 } 3137 3138 if (fh->nf_vp == NULL) 3139 return; 3140 3141 if (fh->nf_type == NDIS_FH_TYPE_VFS) { 3142 mtx_lock(&Giant); 3143 vn_close(fh->nf_vp, FREAD, td->td_ucred, td); 3144 mtx_unlock(&Giant); 3145 } 3146 3147 fh->nf_vp = NULL; 3148 free(fh->nf_name, M_DEVBUF); 3149 ExFreePool(fh); 3150 3151 return; 3152} 3153 3154static uint8_t 3155NdisSystemProcessorCount() 3156{ 3157 return(mp_ncpus); 3158} 3159 3160typedef void (*ndis_statusdone_handler)(ndis_handle); 3161typedef void (*ndis_status_handler)(ndis_handle, ndis_status, 3162 void *, uint32_t); 3163 3164static void 3165NdisMIndicateStatusComplete(adapter) 3166 ndis_handle adapter; 3167{ 3168 ndis_miniport_block *block; 3169 ndis_statusdone_handler statusdonefunc; 3170 3171 block = (ndis_miniport_block *)adapter; 3172 statusdonefunc = block->nmb_statusdone_func; 3173 3174 MSCALL1(statusdonefunc, adapter); 3175 return; 3176} 3177 3178static void 3179NdisMIndicateStatus(adapter, status, sbuf, slen) 3180 ndis_handle adapter; 3181 ndis_status status; 3182 void *sbuf; 3183 uint32_t slen; 3184{ 3185 ndis_miniport_block *block; 3186 ndis_status_handler statusfunc; 3187 3188 block = (ndis_miniport_block *)adapter; 3189 statusfunc = block->nmb_status_func; 3190 3191 MSCALL4(statusfunc, adapter, status, sbuf, slen); 3192 return; 3193} 3194 3195/* 3196 * The DDK documentation says that you should use IoQueueWorkItem() 3197 * instead of ExQueueWorkItem(). The problem is, IoQueueWorkItem() 3198 * is fundamentally incompatible with NdisScheduleWorkItem(), which 3199 * depends on the API semantics of ExQueueWorkItem(). In our world, 3200 * ExQueueWorkItem() is implemented on top of IoAllocateQueueItem() 3201 * anyway. 3202 * 3203 * There are actually three distinct APIs here. NdisScheduleWorkItem() 3204 * takes a pointer to an NDIS_WORK_ITEM. ExQueueWorkItem() takes a pointer 3205 * to a WORK_QUEUE_ITEM. And finally, IoQueueWorkItem() takes a pointer 3206 * to an opaque work item thingie which you get from IoAllocateWorkItem(). 3207 * An NDIS_WORK_ITEM is not the same as a WORK_QUEUE_ITEM. However, 3208 * the NDIS_WORK_ITEM has some opaque storage at the end of it, and we 3209 * (ab)use this storage as a WORK_QUEUE_ITEM, which is what we submit 3210 * to ExQueueWorkItem(). 3211 * 3212 * Got all that? (Sheesh.) 3213 */ 3214 3215ndis_status 3216NdisScheduleWorkItem(work) 3217 ndis_work_item *work; 3218{ 3219 work_queue_item *wqi; 3220 3221 wqi = (work_queue_item *)work->nwi_wraprsvd; 3222 ExInitializeWorkItem(wqi, 3223 (work_item_func)work->nwi_func, work->nwi_ctx); 3224 ExQueueWorkItem(wqi, WORKQUEUE_DELAYED); 3225 3226 return(NDIS_STATUS_SUCCESS); 3227} 3228 3229static void 3230NdisCopyFromPacketToPacket(dpkt, doff, reqlen, spkt, soff, cpylen) 3231 ndis_packet *dpkt; 3232 uint32_t doff; 3233 uint32_t reqlen; 3234 ndis_packet *spkt; 3235 uint32_t soff; 3236 uint32_t *cpylen; 3237{ 3238 ndis_buffer *src, *dst; 3239 char *sptr, *dptr; 3240 int resid, copied, len, scnt, dcnt; 3241 3242 *cpylen = 0; 3243 3244 src = spkt->np_private.npp_head; 3245 dst = dpkt->np_private.npp_head; 3246 3247 sptr = MmGetMdlVirtualAddress(src); 3248 dptr = MmGetMdlVirtualAddress(dst); 3249 scnt = MmGetMdlByteCount(src); 3250 dcnt = MmGetMdlByteCount(dst); 3251 3252 while (soff) { 3253 if (MmGetMdlByteCount(src) > soff) { 3254 sptr += soff; 3255 scnt = MmGetMdlByteCount(src)- soff; 3256 break; 3257 } 3258 soff -= MmGetMdlByteCount(src); 3259 src = src->mdl_next; 3260 if (src == NULL) 3261 return; 3262 sptr = MmGetMdlVirtualAddress(src); 3263 } 3264 3265 while (doff) { 3266 if (MmGetMdlByteCount(dst) > doff) { 3267 dptr += doff; 3268 dcnt = MmGetMdlByteCount(dst) - doff; 3269 break; 3270 } 3271 doff -= MmGetMdlByteCount(dst); 3272 dst = dst->mdl_next; 3273 if (dst == NULL) 3274 return; 3275 dptr = MmGetMdlVirtualAddress(dst); 3276 } 3277 3278 resid = reqlen; 3279 copied = 0; 3280 3281 while(1) { 3282 if (resid < scnt) 3283 len = resid; 3284 else 3285 len = scnt; 3286 if (dcnt < len) 3287 len = dcnt; 3288 3289 bcopy(sptr, dptr, len); 3290 3291 copied += len; 3292 resid -= len; 3293 if (resid == 0) 3294 break; 3295 3296 dcnt -= len; 3297 if (dcnt == 0) { 3298 dst = dst->mdl_next; 3299 if (dst == NULL) 3300 break; 3301 dptr = MmGetMdlVirtualAddress(dst); 3302 dcnt = MmGetMdlByteCount(dst); 3303 } 3304 3305 scnt -= len; 3306 if (scnt == 0) { 3307 src = src->mdl_next; 3308 if (src == NULL) 3309 break; 3310 sptr = MmGetMdlVirtualAddress(src); 3311 scnt = MmGetMdlByteCount(src); 3312 } 3313 } 3314 3315 *cpylen = copied; 3316 return; 3317} 3318 3319static void 3320NdisCopyFromPacketToPacketSafe(dpkt, doff, reqlen, spkt, soff, cpylen, prio) 3321 ndis_packet *dpkt; 3322 uint32_t doff; 3323 uint32_t reqlen; 3324 ndis_packet *spkt; 3325 uint32_t soff; 3326 uint32_t *cpylen; 3327 uint32_t prio; 3328{ 3329 NdisCopyFromPacketToPacket(dpkt, doff, reqlen, spkt, soff, cpylen); 3330 return; 3331} 3332 3333static ndis_status 3334NdisMRegisterDevice(handle, devname, symname, majorfuncs, devobj, devhandle) 3335 ndis_handle handle; 3336 unicode_string *devname; 3337 unicode_string *symname; 3338 driver_dispatch *majorfuncs[]; 3339 void **devobj; 3340 ndis_handle *devhandle; 3341{ 3342 uint32_t status; 3343 device_object *dobj; 3344 3345 status = IoCreateDevice(handle, 0, devname, 3346 FILE_DEVICE_UNKNOWN, 0, FALSE, &dobj); 3347 3348 if (status == STATUS_SUCCESS) { 3349 *devobj = dobj; 3350 *devhandle = dobj; 3351 } 3352 3353 return(status); 3354} 3355 3356static ndis_status 3357NdisMDeregisterDevice(handle) 3358 ndis_handle handle; 3359{ 3360 IoDeleteDevice(handle); 3361 return(NDIS_STATUS_SUCCESS); 3362} 3363 3364static ndis_status 3365NdisMQueryAdapterInstanceName(name, handle) 3366 unicode_string *name; 3367 ndis_handle handle; 3368{ 3369 ndis_miniport_block *block; 3370 device_t dev; 3371 ansi_string as; 3372 3373 block = (ndis_miniport_block *)handle; 3374 dev = block->nmb_physdeviceobj->do_devext; 3375 3376 RtlInitAnsiString(&as, __DECONST(char *, device_get_nameunit(dev))); 3377 if (RtlAnsiStringToUnicodeString(name, &as, TRUE)) 3378 return(NDIS_STATUS_RESOURCES); 3379 3380 return(NDIS_STATUS_SUCCESS); 3381} 3382 3383static void 3384NdisMRegisterUnloadHandler(handle, func) 3385 ndis_handle handle; 3386 void *func; 3387{ 3388 return; 3389} 3390 3391static void 3392dummy() 3393{ 3394 printf ("NDIS dummy called...\n"); 3395 return; 3396} 3397 3398/* 3399 * Note: a couple of entries in this table specify the 3400 * number of arguments as "foo + 1". These are routines 3401 * that accept a 64-bit argument, passed by value. On 3402 * x86, these arguments consume two longwords on the stack, 3403 * so we lie and say there's one additional argument so 3404 * that the wrapping routines will do the right thing. 3405 */ 3406 3407image_patch_table ndis_functbl[] = { 3408 IMPORT_SFUNC(NdisCopyFromPacketToPacket, 6), 3409 IMPORT_SFUNC(NdisCopyFromPacketToPacketSafe, 7), 3410 IMPORT_SFUNC(NdisScheduleWorkItem, 1), 3411 IMPORT_SFUNC(NdisMIndicateStatusComplete, 1), 3412 IMPORT_SFUNC(NdisMIndicateStatus, 4), 3413 IMPORT_SFUNC(NdisSystemProcessorCount, 0), 3414 IMPORT_SFUNC(NdisUnchainBufferAtBack, 2), 3415 IMPORT_SFUNC(NdisGetFirstBufferFromPacket, 5), 3416 IMPORT_SFUNC(NdisGetFirstBufferFromPacketSafe, 6), 3417 IMPORT_SFUNC(NdisGetBufferPhysicalArraySize, 2), 3418 IMPORT_SFUNC(NdisMGetDeviceProperty, 6), 3419 IMPORT_SFUNC(NdisInitAnsiString, 2), 3420 IMPORT_SFUNC(NdisInitUnicodeString, 2), 3421 IMPORT_SFUNC(NdisWriteConfiguration, 4), 3422 IMPORT_SFUNC(NdisAnsiStringToUnicodeString, 2), 3423 IMPORT_SFUNC(NdisTerminateWrapper, 2), 3424 IMPORT_SFUNC(NdisOpenConfigurationKeyByName, 4), 3425 IMPORT_SFUNC(NdisOpenConfigurationKeyByIndex, 5), 3426 IMPORT_SFUNC(NdisMRemoveMiniport, 1), 3427 IMPORT_SFUNC(NdisInitializeString, 2), 3428 IMPORT_SFUNC(NdisFreeString, 1), 3429 IMPORT_SFUNC(NdisGetCurrentSystemTime, 1), 3430 IMPORT_SFUNC(NdisGetSystemUpTime, 1), 3431 IMPORT_SFUNC(NdisMSynchronizeWithInterrupt, 3), 3432 IMPORT_SFUNC(NdisMAllocateSharedMemoryAsync, 4), 3433 IMPORT_SFUNC(NdisInterlockedInsertHeadList, 3), 3434 IMPORT_SFUNC(NdisInterlockedInsertTailList, 3), 3435 IMPORT_SFUNC(NdisInterlockedRemoveHeadList, 2), 3436 IMPORT_SFUNC(NdisInitializeWrapper, 4), 3437 IMPORT_SFUNC(NdisMRegisterMiniport, 3), 3438 IMPORT_SFUNC(NdisAllocateMemoryWithTag, 3), 3439 IMPORT_SFUNC(NdisAllocateMemory, 4 + 1), 3440 IMPORT_SFUNC(NdisMSetAttributesEx, 5), 3441 IMPORT_SFUNC(NdisCloseConfiguration, 1), 3442 IMPORT_SFUNC(NdisReadConfiguration, 5), 3443 IMPORT_SFUNC(NdisOpenConfiguration, 3), 3444 IMPORT_SFUNC(NdisAcquireSpinLock, 1), 3445 IMPORT_SFUNC(NdisReleaseSpinLock, 1), 3446 IMPORT_SFUNC(NdisDprAcquireSpinLock, 1), 3447 IMPORT_SFUNC(NdisDprReleaseSpinLock, 1), 3448 IMPORT_SFUNC(NdisAllocateSpinLock, 1), 3449 IMPORT_SFUNC(NdisInitializeReadWriteLock, 1), 3450 IMPORT_SFUNC(NdisAcquireReadWriteLock, 3), 3451 IMPORT_SFUNC(NdisReleaseReadWriteLock, 2), 3452 IMPORT_SFUNC(NdisFreeSpinLock, 1), 3453 IMPORT_SFUNC(NdisFreeMemory, 3), 3454 IMPORT_SFUNC(NdisReadPciSlotInformation, 5), 3455 IMPORT_SFUNC(NdisWritePciSlotInformation, 5), 3456 IMPORT_SFUNC_MAP(NdisImmediateReadPciSlotInformation, 3457 NdisReadPciSlotInformation, 5), 3458 IMPORT_SFUNC_MAP(NdisImmediateWritePciSlotInformation, 3459 NdisWritePciSlotInformation, 5), 3460 IMPORT_CFUNC(NdisWriteErrorLogEntry, 0), 3461 IMPORT_SFUNC(NdisMStartBufferPhysicalMapping, 6), 3462 IMPORT_SFUNC(NdisMCompleteBufferPhysicalMapping, 3), 3463 IMPORT_SFUNC(NdisMInitializeTimer, 4), 3464 IMPORT_SFUNC(NdisInitializeTimer, 3), 3465 IMPORT_SFUNC(NdisSetTimer, 2), 3466 IMPORT_SFUNC(NdisMCancelTimer, 2), 3467 IMPORT_SFUNC_MAP(NdisCancelTimer, NdisMCancelTimer, 2), 3468 IMPORT_SFUNC(NdisMSetPeriodicTimer, 2), 3469 IMPORT_SFUNC(NdisMQueryAdapterResources, 4), 3470 IMPORT_SFUNC(NdisMRegisterIoPortRange, 4), 3471 IMPORT_SFUNC(NdisMDeregisterIoPortRange, 4), 3472 IMPORT_SFUNC(NdisReadNetworkAddress, 4), 3473 IMPORT_SFUNC(NdisQueryMapRegisterCount, 2), 3474 IMPORT_SFUNC(NdisMAllocateMapRegisters, 5), 3475 IMPORT_SFUNC(NdisMFreeMapRegisters, 1), 3476 IMPORT_SFUNC(NdisMAllocateSharedMemory, 5), 3477 IMPORT_SFUNC(NdisMMapIoSpace, 4 + 1), 3478 IMPORT_SFUNC(NdisMUnmapIoSpace, 3), 3479 IMPORT_SFUNC(NdisGetCacheFillSize, 0), 3480 IMPORT_SFUNC(NdisMGetDmaAlignment, 1), 3481 IMPORT_SFUNC(NdisMInitializeScatterGatherDma, 3), 3482 IMPORT_SFUNC(NdisAllocatePacketPool, 4), 3483 IMPORT_SFUNC(NdisAllocatePacketPoolEx, 5), 3484 IMPORT_SFUNC(NdisAllocatePacket, 3), 3485 IMPORT_SFUNC(NdisFreePacket, 1), 3486 IMPORT_SFUNC(NdisFreePacketPool, 1), 3487 IMPORT_SFUNC_MAP(NdisDprAllocatePacket, NdisAllocatePacket, 3), 3488 IMPORT_SFUNC_MAP(NdisDprFreePacket, NdisFreePacket, 1), 3489 IMPORT_SFUNC(NdisAllocateBufferPool, 3), 3490 IMPORT_SFUNC(NdisAllocateBuffer, 5), 3491 IMPORT_SFUNC(NdisQueryBuffer, 3), 3492 IMPORT_SFUNC(NdisQueryBufferSafe, 4), 3493 IMPORT_SFUNC(NdisBufferVirtualAddress, 1), 3494 IMPORT_SFUNC(NdisBufferVirtualAddressSafe, 2), 3495 IMPORT_SFUNC(NdisBufferLength, 1), 3496 IMPORT_SFUNC(NdisFreeBuffer, 1), 3497 IMPORT_SFUNC(NdisFreeBufferPool, 1), 3498 IMPORT_SFUNC(NdisInterlockedIncrement, 1), 3499 IMPORT_SFUNC(NdisInterlockedDecrement, 1), 3500 IMPORT_SFUNC(NdisInitializeEvent, 1), 3501 IMPORT_SFUNC(NdisSetEvent, 1), 3502 IMPORT_SFUNC(NdisResetEvent, 1), 3503 IMPORT_SFUNC(NdisWaitEvent, 2), 3504 IMPORT_SFUNC(NdisUnicodeStringToAnsiString, 2), 3505 IMPORT_SFUNC(NdisMPciAssignResources, 3), 3506 IMPORT_SFUNC(NdisMFreeSharedMemory, 5 + 1), 3507 IMPORT_SFUNC(NdisMRegisterInterrupt, 7), 3508 IMPORT_SFUNC(NdisMDeregisterInterrupt, 1), 3509 IMPORT_SFUNC(NdisMRegisterAdapterShutdownHandler, 3), 3510 IMPORT_SFUNC(NdisMDeregisterAdapterShutdownHandler, 1), 3511 IMPORT_SFUNC(NDIS_BUFFER_TO_SPAN_PAGES, 1), 3512 IMPORT_SFUNC(NdisQueryBufferOffset, 3), 3513 IMPORT_SFUNC(NdisAdjustBufferLength, 2), 3514 IMPORT_SFUNC(NdisPacketPoolUsage, 1), 3515 IMPORT_SFUNC(NdisMSleep, 1), 3516 IMPORT_SFUNC(NdisUnchainBufferAtFront, 2), 3517 IMPORT_SFUNC(NdisReadPcmciaAttributeMemory, 4), 3518 IMPORT_SFUNC(NdisWritePcmciaAttributeMemory, 4), 3519 IMPORT_SFUNC(NdisOpenFile, 5 + 1), 3520 IMPORT_SFUNC(NdisMapFile, 3), 3521 IMPORT_SFUNC(NdisUnmapFile, 1), 3522 IMPORT_SFUNC(NdisCloseFile, 1), 3523 IMPORT_SFUNC(NdisMRegisterDevice, 6), 3524 IMPORT_SFUNC(NdisMDeregisterDevice, 1), 3525 IMPORT_SFUNC(NdisMQueryAdapterInstanceName, 2), 3526 IMPORT_SFUNC(NdisMRegisterUnloadHandler, 2), 3527 IMPORT_SFUNC(ndis_timercall, 4), 3528 IMPORT_SFUNC(ndis_asyncmem_complete, 2), 3529 IMPORT_SFUNC(ndis_intr, 1), 3530 IMPORT_SFUNC(ndis_intrhand, 4), 3531 3532 /* 3533 * This last entry is a catch-all for any function we haven't 3534 * implemented yet. The PE import list patching routine will 3535 * use it for any function that doesn't have an explicit match 3536 * in this table. 3537 */ 3538 3539 { NULL, (FUNC)dummy, NULL, 0, WINDRV_WRAP_STDCALL }, 3540 3541 /* End of list. */ 3542 3543 { NULL, NULL, NULL } 3544}; 3545