Hardware interview practice
DMA descriptor-ring submission
Firmware produces descriptors for an eight-entry DMA ring. Hardware advances head, firmware advances tail, and one slot stays empty to distinguish full from empty. Implement validation, wraparound, full detection, and device-visible publication ordering.
Starting point
Question code
int submit(struct ring *r, uint64_t addr, uint32_t len);
ring has desc[8], head, tail, and MMIO doorbellReviewed example
Work through one case
Input
tail=7, head=3, addr is 64-byte aligned, len=1024, and the ring has spaceExpected output
Write desc[7], execute dma_wmb, set tail=0, write doorbell=0, and return 0The mask implements modulo-eight wraparound, and the barrier ensures the descriptor is visible before hardware learns the new producer index.
What to cover
Requirements
- Reject a null ring, null head or doorbell register pointer, len outside 1..4096, or addr not aligned to 64 bytes with -EINVAL and no ring or MMIO change.
- Read hardware head, compute next=(tail+1)&7, and return -EAGAIN without writing a descriptor when next==head.
- Write addr and len into desc[tail], execute a DMA write barrier, then publish tail=next and doorbell=next in that order.
- Use modulo-eight wraparound, submit exactly one descriptor per successful call, and return 0 only after the doorbell write.
