Create an Adapter Context Area for NDIS 6.0 (Compact 2013)

3/26/2014

When NDIS calls your driver's MiniportInitializeEx function, you must create a context area to store driver-specific information about the miniport adapter. Because your driver may control more than one miniport adapter (and each miniport adapter has a different state), your driver must maintain a separate context area for each miniport adapter that it controls.

To create an adapter context area

  1. Allocate the adapter context area. You can call NdisAllocateMemoryWithTagPriority to allocate this area with a memory tag for ease of debugging.

  2. Clear the context area. You can call NdisZeroMemory to fill the context area with zeroes (0’s).

  3. Initialize the context area. Typically, you store the following information in the adapter context area:

    • The adapter handle provided by NDIS
    • Send and receive data structures
    • Spin locks
    • Timers
    • Event objects
    • Send and receive packet counts
    • Adapter error counts

    Each adapter context area may also store hardware resource information that is not present in the NDIS-provided CM_PARTIAL_RESOURCE_LIST structure. For more information about this structure, see CM_PARTIAL_RESOURCE_LIST.

The following code example allocates and initializes a simple adapter context area.

Important

For readability, the following code example does not contain security checking or error handling. Do not use the following code in a production environment.

#define MEM_TAG ((ULONG('DCBA'))
MY_ADAPTER *pMyAdapter;
. . .
// Allocate an adapter context area:
(PVOID)pMyAdapter = NdisAllocateMemoryWithTagPriority (
                    NdisMiniportDriverHandle, sizeof(MY_ADAPTER),
                    MEM_TAG, LowPoolPriority);

// Clear the context area:
NdisZeroMemory(pMyAdapter, sizeof(MY_ADAPTER));

// Initialize data structures for this adapter:
NdisInitializeEvent(&pMyAdapter->ExitEvent);
NdisInitializeEvent(&pMyAdapter->AllPacketsReturnedEvent);
NdisAllocateSpinLock(&pMyAdapter->Lock);
NdisAllocateSpinLock(&pMyAdapter->SendLock);
NdisAllocateSpinLock(&pMyAdapter->RcvLock);

In the preceding example, MY_ADAPTER is a driver-specific structure (one that you define) that holds the miniport adapter context. MEM_TAG is a tag that you declare to allocate a pool of memory from the non-paged pool; in this example, the tag declaration results in the debug string "ABCD". For more information about how to use the NDIS memory interface to allocate memory from the non-paged pool, see NdisAllocateMemoryWithTagPriority.

After you allocate an adapter context area and assign it to pMyAdapter, the preceding example code uses pMyAdapter to store two events and three spin locks. In Register the Context Area and Miniport Adapter Attributes with NDIS, another code example shows you how to register this adapter context area with NDIS. Note that these simplified examples do no error checking and do not handle reference counting.

For information about NDIS functions that you can use to protect driver data structures, see NDIS Interlocked Functions Interface. For more information about how to allocate memory, see Allocate Memory in Your NDIS 6.0 Miniport Driver.

See Also

Concepts

Modify Adapter Initialization Functionality for NDIS 6.0