What is DMA (Part 5) - Packet Based DMA

Packet Based DMA

Last time i talked about using common buffer to stage your DMA operations. Doing this allows you to coallesce very fragmented packets, which can be very valuable, but it does complicate your DMA operations. After all someone has to manage the common buffer that you allocated.

The alternative is known as Packet Based DMA in the DDK. In Packet Based DMA you ask Windows to prepare each DMA "packet" for a transfer to or from your device. You provide the buffer you want to map and a callback "ExecutionRoutine". Windows will look at how many map registers the transfer requires and how many you have available. If there aren't enough Windows will queue your request until they free up.

Once there are enough free registers, Windows will invoke your "ExecutionRoutine". This is your driver's cue to start the DMA transfer. When you are done with the transfer you call the DMA DDI again to return all of the map registers to the pool. At this point Windows may invoke the ExecutionRoutine for a subsequent request.

So what is a "packet" then? It's any unit of transfer that is:

  • Uni-directional (Packet based DDI doesn't support bi-directional transfers)
  • Fits within the number of map registers you were granted when you created the DMA_ADAPTER (NumberOfMapRegisters * PAGE_SIZE)
  • Represents a reasonable unit of transfer for your device

I realize the last one is pretty vague. Unfortunately i can't answer what is a good unit for a given device. For most SCSI controllers, the unit of transfer is a single SCSI operation. For a network controller a unit of transfer might be a single packet, or might be a whole sequence of packets.

The DMA DDI has two pairs of functions for this allocate/release pairing. The older set of DDIs are:

  • AllocateAdapterChannel
  • MapTransfer
  • FlushAdapterBuffers
  • FreeAdapterChannel

The newer set of DDIs are:

  • GetScatterGatherList (or BuildScatterGatherList)
  • PutScatterGatherList

Each set has its own strengths and weakneses. The first set works with slave-mode DMA as well as bus-mastering DMA. However the second set are much simpler to use as they generate an entire scatter-gather list for you automatically.

I'll talk more about the two options next time.