qntb: virtual PCIe NTB device specification¶
Version: 0.3 (draft)
This document defines the guest-visible behaviour of qntb, a device that connects two virtual machines: PCI identity, BAR layout, registers, interrupts, link state machine and DMA engine. It is the common source for two implementations:
ntbd: a prototype based on libvfio-user (outside QEMU, over vfio-user; one process serves both ends).qntb, a QEMU-internal device (qemu/hw/misc/qntb.c); two QEMU processes are connected by a UNIX socket (docs/protocol.md).
The transport between the two ends (socket protocol, memory sharing) is out
of the scope of this document, but the guarantees the transport must provide
are normative in §13. Requirements use the keywords MUST / SHOULD /
MAY in the sense of RFC 2119. §10 contains normative rules for the driver.
1. Model¶
There are two ends (sides): A and B. Each end is a PCI function in its own VM. The ends are symmetric; "local" means this function, "peer" means the other end.
flowchart LR
subgraph A["VM A"]
direction TB
aps["BAR0: PEER_SPAD[i]"]
adb["BAR0: PEER_DB_SET"]
amw["MW BAR i (outbound)"]
ax["XLAT[i] → A's memory"]
end
subgraph B["VM B"]
direction TB
bs["SPAD[i]"]
bdb["DB_STATUS → MSI-X"]
bx["XLAT[i] → B's memory"]
bmw["MW BAR i (outbound)"]
end
aps -->|peer spad| bs
adb -->|peer doorbell| bdb
amw -->|MW i| bx
bmw -->|MW i| ax
- Scratchpad (spad): 32-bit registers. A value one end writes to the peer lands in the peer's local spad.
- Doorbell (db): one end sets doorbell status bits of the peer. Unmasked bits raise an MSI-X interrupt at the peer.
- Memory window (MW): for each MW there is a local outbound BAR window
and an inbound translation (XLAT) at the peer. An access to offset
oof A's MW i BAR becomes an access to bus addressXLAT_ADDR[i] + oin B. - DMA engine: copies between local memory and peer memory (through an MW) and within local memory (§9).
Translation exists only in the inbound direction: each end decides which
part of its own memory it exposes (Linux mw_set_trans). The outbound window
is fixed and shows the whole of the peer's inbound window.
Memory isolation invariant (MUST): an end can reach the peer's memory only through the peer's currently active XLAT windows, and only while the peer device accepts inbound accesses (§6.4). No path, DMA included, bypasses this rule. The check is done by the end that owns the memory (the target).
2. PCI identity¶
| Field | Value |
|---|---|
| Vendor ID | 0x1af4 |
| Device ID | 0x10f0 (QEMU docs/specs/pci-ids.rst: for experimental use, unregistered). An official ID must be obtained before upstreaming. Note: Linux virtio-pci probes every device with vendor 0x1af4 and rejects this ID; harmless. |
| Revision ID | 0x00 (0.x versions) |
| Class code | 0x068000 (bridge, other; same as Intel NTB) |
| Subsystem vendor / ID | 0x1af4 / 0x1100 |
| Header type | 0 (endpoint) |
| Interrupt pin | 0 (no INTx) |
Capability list:
- MSI-X (MUST).
- PCI Express endpoint capability (SHOULD). If present, DEVCAP.FLR = 1 (MUST).
- Power management capability (MAY).
Effects of PCI_COMMAND (MUST):
- MEM = 0: BAR accesses do not reach the device, per PCI rules.
- BME = 0 (bus mastering off): the device makes no access to local memory.
Running DMA channels abandon the chunk in progress and stop: no status is
written to the ring, TAIL does not advance, only registers change
(STATUS.ERR, ERR_INFO = 7, §9.5). Inbound MW accesses to the local end
are refused (§6.4). No MSI-X message is sent; event and doorbell status bits
stay set. The effect of writing BME = 0 is guaranteed at the completion
point defined in §4.
3. BAR layout¶
| BAR | Type | Size | Contents |
|---|---|---|---|
| BAR0 | 32-bit MMIO, non-prefetchable | 16 KiB | Control registers (§4) |
| BAR1 | 32-bit MMIO, non-prefetchable | 4 KiB | MSI-X table (0x000) and PBA (0x800). If the platform (e.g. QEMU vfio) emulates the table and PBA itself, the device answers accesses to this BAR by reading 0 and ignoring writes; it does not model mask or pending bits and does not set BAD_ACCESS. |
| BAR2/3 | 64-bit MMIO, prefetchable | mw0_size |
MW0 outbound window |
| BAR4/5 | 64-bit MMIO, prefetchable | mw1_size |
MW1 outbound window (if mw_count = 2) |
- MW sizes are powers of two, at least 4 KiB and at least the MW's
XLAT_ALIGN(MUST; 1 MiB for the highest-index MW, §7.1). - With
mw_count = 1there is no BAR4/5. There are at most 2 MWs. - MW BARs are prefetchable; a driver may map them write-combining. MW reads
therefore have no side effects:
DEV_STATUS.PEER_DROPin §7.4 is for diagnostics only. - The MSI-X table has
1 + n_dbvec + n_spareentries (§5.2), at most 128 (the table takes 2 KiB, which fits below the PBA at 0x800).
4. Control registers (BAR0)¶
All registers are little-endian and 32 bits wide.
Access size:
- An aligned 32-bit access is defined for every register.
- An aligned 64-bit access is defined at every 8-byte-aligned offset and is
equivalent to two 32-bit accesses, lower offset first; no other state
change is observed between the two halves (MUST).
- Any other access (8/16-bit, unaligned), whether or not the offset is
defined: a read returns all ones at the access size, a write is ignored,
and DEV_STATUS.BAD_ACCESS is set. This rule takes precedence over the
next one.
For accesses of a valid size, undefined offsets and reserved fields read as 0
and ignore writes (this is not a BAD_ACCESS). Undefined bits of a register
read as 0.
Completion of writes. BAR writes are posted: after the guest writes, the
device may not have processed the write yet (this is also the case with QEMU
vfio-user). In this document "the effect of a write is guaranteed" means: once
a later read from BAR0 of the same function has returned, all effects of
the write (including those on the peer) have taken place (MUST). Where a
barrier is needed the driver does this read (§10.4). The recommended read is
MAGIC.
Access types:
| Type | Read | Write |
|---|---|---|
| RO | value | ignored |
| RW | value | writes the value |
| W1C | value | bits written as 1 are cleared in this register |
| SET | 0 | bits written as 1 are set in the target register |
| CLR | 0 | bits written as 1 are cleared in the target register |
| WO | 0 | command (see description) |
4.1 Map¶
| Offset | Name | Type | Reset | Description |
|---|---|---|---|---|
| 0x000 | MAGIC |
RO | 0x42544E51 |
"QNTB" (LE) |
| 0x004 | VERSION |
RO | 0x00000003 |
major << 16 | minor; this document is 0.3 |
| 0x008 | FEATURES |
RO | §4.2 | |
| 0x00C | DEV_STATUS |
W1C | 0 | bit0 BAD_ACCESS; bit1 PEER_DROP (§7.4) |
| 0x010 | LINK_CTRL |
RW | 0 | bit0 ENABLE (§6) |
| 0x014 | LINK_STATUS |
RO | — | Derived from current state, not written by reset. bit0 UP; bit1 PEER_PRESENT; bit2 PEER_ENABLED; bit3 CFG_MISMATCH; bit4 SIDE (0 = A, 1 = B; informational); bits 11:8 SPEED; bits 21:16 WIDTH |
| 0x018 | EVENT_STATUS |
W1C | 0 | bit0 LINK_UP; bit1 LINK_DOWN; bit2 DMA_DONE; bit3 DMA_ERR (§5.4) |
| 0x01C | EVENT_MASK |
RW | 0x0000000F |
Same bits as EVENT_STATUS; 1 = masked |
| 0x020 | RESET |
WO | Writing the 32-bit integer 0x52535421 resets the BAR0 state (§8). Other values are ignored. |
|
| 0x024 | LINK_GEN |
RO | — | 32-bit counter, incremented on every change of UP (wraps). Zero only when the device is created; no reset clears it. |
| 0x040 | DB_VALID_LO/HI |
RO | Valid doorbell bits: the low db_count bits |
|
| 0x048 | DB_STATUS_LO/HI |
W1C | 0 | Local doorbell status bits |
| 0x050 | DB_MASK_LO/HI |
RO | DB_VALID |
Local doorbell mask (1 = masked) |
| 0x058 | DB_MASK_SET_LO/HI |
SET | Target: DB_MASK |
|
| 0x060 | DB_MASK_CLR_LO/HI |
CLR | Target: DB_MASK (resend rule in §5.1) |
|
| 0x068 | DB_SET_LO/HI |
SET | Target: local DB_STATUS (Linux db_set) |
|
| 0x070 | PEER_DB_SET_LO/HI |
SET | Target: the peer's DB_STATUS (§5.1) |
|
| 0x078 | DB_VEC_COUNT |
RO | n_dbvec (§5.2) |
|
| 0x07C | MSIX_SPARE |
RO | n_spare: number of extra MSI-X entries the driver does not use (§5.2). The value of the msix_spare parameter if MSI_FWD = 1, 0 if MSI_FWD = 0 (MUST). The table size uses this effective value. |
|
| 0x080 | SPAD_COUNT |
RO | n_spad |
|
| 0x084 | MW_COUNT |
RO | 1 or 2 | |
| 0x088 | DMA_CHAN_COUNT |
RO | 0–8 | |
| 0x100 + 0x40·i | MW[i] block |
§7.1, i < MW_COUNT |
||
| 0x200 + 0x40·c | DMA[c] block |
§9.2, c < DMA_CHAN_COUNT |
||
| 0x400 + 4·k | SPAD[k] |
RW | 0 | Local spad k, k < n_spad |
| 0x600 + 4·k | PEER_SPAD[k] |
RW | The peer's SPAD[k] (§6.3), k < n_spad |
|
| 0x800–0x3FFF | Reserved |
Bits outside DB_VALID are ignored in writes to the DB_* registers. For
k ≥ n_spad, SPAD[k]/PEER_SPAD[k] are reserved offsets.
LINK_STATUS.SPEED and WIDTH are fixed numbers: SPEED = 3 and
WIDTH = 16 while UP = 1; both 0 while UP = 0. They are informational.
4.2 FEATURES¶
| Bit | Name | Meaning |
|---|---|---|
| 0 | SPAD |
Scratchpads present (always 1 in 0.x) |
| 1 | PEER_SPAD_READ |
PEER_SPAD is readable (always 1 in 0.x) |
| 2 | DMA |
DMA_CHAN_COUNT ≥ 1 |
| 3 | MSG |
Reserved, 0 (no message registers) |
| 4 | MSI_FWD |
Inbound MSI forwarding present (§7.3). Needed for ntb_msi. |
A driver ignores bits it does not know (MUST). The device does not change
the meaning of existing bits unless VERSION.major changes.
5. Doorbells and interrupts¶
5.1 Doorbell semantics¶
- Valid bits written to
PEER_DB_SETare ORed into the peer'sDB_STATUS(subject to §6.4). - When a
DB_STATUSbit goes from 0 to 1 and is not masked, an MSI-X message is sent to the bit's vector. - Setting a bit that is already 1 does not send a new message (
MUST NOT). - If one write (a 64-bit write counts as one) sets several bits of the same vector, at most one message is sent to that vector.
- No message is sent when a masked bit is set. If a bit unmasked through
DB_MASK_CLRis still 1 inDB_STATUS, a message is sent to its vector (MUST). Bits whose mask does not change are not affected by this rule. - If the MSI-X entry or the function is masked, PCI rules apply (the message pends in the PBA).
5.2 MSI-X vectors¶
The table has 1 + n_dbvec + n_spare entries:
| Entry | Use |
|---|---|
| 0 | Events (EVENT_STATUS) |
1 + v, 0 ≤ v < n_dbvec |
Doorbell bits i with i mod n_dbvec == v |
1 + n_dbvec + s, 0 ≤ s < n_spare |
The device triggers these only through MSI forwarding (§7.3) |
Linux db_vector_count = n_dbvec; db_vector_mask(v) is the bit set above.
The n_spare entries are the free vectors to which Linux ntb_msi attaches
its own interrupts (§10).
5.3 Peer doorbell address¶
0.x does not provide peer_db_addr. (No target Linux client uses it.)
5.4 Event interrupt (vector 0)¶
EVENT_STATUSbits are set when an event happens and cleared by W1C.- When a new bit appears in
(EVENT_STATUS & ~EVENT_MASK)(a new event, or a pending event whose mask is cleared), a message is sent to vector 0 (MUST). An event that happens again while its bit is set sends no new message. LINK_UP/LINK_DOWNare separate and sticky: after a fast down–up both can be set. The driver can also detect the change throughLINK_GEN.DMA_DONE: when a descriptor with theIRQflag completes;DMA_ERR: when a channel stops with an error (§9.5). They are shared by all channels; on the interrupt the driver readsTAIL/STATUSof every channel.
5.5 Undeliverable interrupts¶
Events that happen while MSI-X is disabled or BME = 0 send no message, and
there is no guarantee they are sent later; their status bits stay set. After
enabling MSI-X and unmasking, the driver must read EVENT_STATUS and
DB_STATUS and handle anything pending (§10).
6. Link, spads and inbound acceptance¶
6.1 Link state machine¶
PEER_PRESENT: the peer end exists, the transport is connected and the configurations are compatible (§11).PEER_ENABLED:PEER_PRESENTand the peer'sLINK_CTRL.ENABLEis 1.CFG_MISMATCH: the peer is connected but the configuration is incompatible;PEER_PRESENTstays 0.PEER_PRESENTis symmetric: it becomes 1 on both ends when the connection handshake completes and 0 on both ends when the connection is lost (§13).PEER_PRESENTandPEER_ENABLEDare valid and current even while the localENABLE = 0.
UP = LINK_CTRL.ENABLE (local) ∧ PEER_ENABLED
- Each end computes
UPfrom the state it sees. The change does not have to happen at the same time on both ends; the ordering guarantees of §13 apply. - When
UPchanges,LINK_GENis incremented and theLINK_UPorLINK_DOWNevent is set. - After
UPfalls from 1 to 0 it does not become 1 again for at least 100 ms (SHOULD). (This avoids a race with the delayed cleanup in Linuxntb_transport.) - If the peer disappears (VM stopped, reset, transport lost),
PEER_PRESENT = 0andUP = 0.
6.2 link_disable is a barrier¶
Once the effect of the write that changes LINK_CTRL.ENABLE from 1 to 0 is
guaranteed (§4: a later BAR0 read has returned) (MUST):
- the local end accepts no new inbound accesses (§6.4),
- the peer knows about the change: the peer's next LINK_STATUS read
returns UP = 0.
6.3 Scratchpads¶
SPAD[k]: reads/writes the local value.- A
PEER_SPAD[k]write writes the peer'sSPAD[k](subject to §6.4). - If an end writes
PEER_SPAD[k]and then readsPEER_SPAD[k](while the link isUP), it sees the value it wrote or a value the peer wrote later (read-your-writes). - A
PEER_SPAD[k]read returns the peer'sSPAD[k]. If the localUP = 0, it returns0xFFFFFFFFand setsPEER_DROP. Since0xFFFFFFFFcan also be a valid spad value, the driver checks the link state separately. - Spad writes raise no interrupt.
- Spad values are kept when the link goes down; the driver clears them if needed.
- An implementation may serve
PEER_SPADreads from a local copy of the peer's values; it must then provide the visibility guarantee of §13.
6.4 Acceptance of peer-directed and inbound operations¶
A peer-directed operation (MW access, PEER_SPAD write, PEER_DB_SET, DMA
to the peer) is checked at two points:
- Sender: if the local
UP = 0, the operation is dropped (§7.4). - Target (the owner of the memory): the operation is applied only if all of these hold at the target, otherwise it is dropped:
- the target's
LINK_CTRL.ENABLE = 1, - the target sees the sender as
PEER_PRESENT, - the target's
BME = 1(only for MW and DMA accesses that read or write memory), - for an MW access: the target's
MW[i].XLAT_STATUS.ACTIVE = 1and the access range[o, o + n)lies entirely within[0, XLAT_SIZE)(overflow-safe check:o < XLAT_SIZE ∧ n ≤ XLAT_SIZE − o).
The target accepts the access even if it has not seen UP yet (as long as
ENABLE = 1). This way the first writes the sender makes as soon as it sees
UP are not lost.
Writes dropped at the target are lost silently; dropped reads return all ones
to the sender. The target sets its own DEV_STATUS.PEER_DROP (MAY).
7. Memory windows¶
7.1 MW[i] block (BAR0 0x100 + 0x40·i)¶
| Offset | Name | Type | Reset | Description |
|---|---|---|---|---|
| +0x00 | MW_BAR |
RO | BAR index of the outbound window (MW0 → 2, MW1 → 4) | |
| +0x04 | MW_FLAGS |
RO | 0 | Reserved |
| +0x08 | MW_SIZE_LO/HI |
RO | Window size = BAR size = largest inbound size | |
| +0x10 | XLAT_ALIGN |
RO | Alignment of XLAT_ADDR (bytes, power of two) |
|
| +0x14 | XLAT_SIZE_ALIGN |
RO | Alignment of XLAT_SIZE (bytes). 4096 |
|
| +0x18 | XLAT_ADDR_LO/HI |
RW | 0 | Local bus address of the inbound translation |
| +0x20 | XLAT_SIZE_LO/HI |
RW | 0 | Size of the inbound translation |
| +0x28 | XLAT_CTRL |
RW | 0 | bit0 ENABLE |
| +0x2C | XLAT_STATUS |
RO | 0 | bit0 ACTIVE; bit1 ERR |
| +0x30–0x3F | Reserved |
XLAT_ALIGN: 1 MiB for the highest-index MW, 4096 for the others (SHOULD;
§10, note on Linux ntb_msi). MW_SIZE ≥ XLAT_ALIGN must hold.
7.2 Inbound translation state machine¶
| State | XLAT_CTRL.ENABLE reads |
ACTIVE |
ERR |
|---|---|---|---|
| Off | 0 | 0 | 0 |
| Rejected | 0 | 0 | 1 |
| Active | 1 | 1 | 0 |
- Off/Rejected → write
ENABLE = 1: the device checksXLAT_ADDR % XLAT_ALIGN == 0,XLAT_SIZE % XLAT_SIZE_ALIGN == 0,0 < XLAT_SIZE ≤ MW_SIZE, and thatXLAT_ADDR + XLAT_SIZEdoes not exceed 2^64. If valid: Active, otherwise Rejected. - Active → write
ENABLE = 1: no effect. - Any → write
ENABLE = 0: Off (ERRcleared). Once the effect of this write is guaranteed (§4: a later BAR0 read has returned), the peer makes no further access to this window and accesses in progress have finished (MUST). The driver frees the memory only after that read (§10.4). - Writing
XLAT_ADDR/XLAT_SIZEwhile Active: ignored. - The result of an
ENABLEwrite is synchronous:XLAT_STATUSread right after it is current.
A window may span several memory regions of the target VM. The implementation resolves the address at each access; if a region changes during an access (e.g. a memory mapping was removed), the access is dropped.
7.3 Outbound accesses¶
An access of n bytes at offset o of A's MW i BAR:
- Supported accesses (MUST): aligned n ∈ {1, 2, 4, 8}. If the platform
delivers unaligned accesses or accesses larger than 8 bytes (n ≤ 64), the
device accepts them too (SHOULD). (QEMU vfio regions do not deliver
unaligned accesses and split large ones into 8 bytes; a QEMU-internal device
can enable .valid.unaligned if it wants.) The order in which split parts
become visible at the peer is undefined, but all of them are visible before
later operations (§13).
- Aligned 1/2/4/8-byte accesses are performed at the target as single-copy
atomic accesses (SHOULD).
- Subject to §6.4, the access becomes a read/write of the same size at bus
address B.XLAT_ADDR[i] + o in B.
MSI forwarding (if FEATURES.MSI_FWD = 1, MUST): if an inbound 4-byte
write to B targets a bus address equal to the message address of entry k of
B's own MSI-X table, with data equal to that entry's message data, the device
does not write memory; it triggers vector k instead, under PCI masking/PBA
rules. The match uses the table contents at the time of the write. The
triggered interrupt is visible after earlier writes from the same end (§13).
In this document RAM is guest memory the implementation can access
directly (for ntbd: memory mapped as a vfio-user DMA region; for a
QEMU-internal device: regions that resolve to RAM in the address_space).
Other inbound accesses to non-RAM targets (all of them with MSI_FWD = 0;
with MSI_FWD = 1, writes that are not an MSI match, and all reads) count as
refused at the target: the write is dropped, the read returns all ones (§6.4).
(Note: in the ntbd prototype the MSI-X table is managed by QEMU, so
MSI_FWD = 0; see PLAN P6/P7.)
7.4 Unreachable peer (sender side)¶
When the sender's UP = 0, peer-directed operations:
- writes are dropped,
- reads return all ones at the access size,
- the local DEV_STATUS.PEER_DROP is set.
In no case does the device raise an error interrupt or stop the VM.
8. Reset¶
There are two kinds of reset:
- Function reset: PCI FLR, VM (system) reset, and creation of the
device. PCI config space (COMMAND, BAR addresses, MSI-X capability) is
reset per PCI rules; the BAR0 state is reset as below. (The peer
disconnecting and reconnecting is not a reset of the local end; the local
end only sees
PEER_PRESENTchange. The local client, i.e. the local QEMU, reconnecting to the device does count as a function reset.) RESETregister: only the BAR0 state is reset; PCI config space is not touched.
Resetting the BAR0 state: the "Reset" column of the tables in §4.1, §7.1 and
§9.2 applies. In short: LINK_CTRL = 0, EVENT_STATUS = 0,
EVENT_MASK = 0xF, DEV_STATUS = 0, DB_STATUS = 0, DB_MASK = DB_VALID,
SPAD[*] = 0, every XLAT Off with address/size 0, every DMA channel in its
reset state. LINK_STATUS is derived state; LINK_GEN is not reset.
- Reset stops local DMA in progress. Once the effect of the reset is
guaranteed (for FLR/VM reset: when the reset is over; for the
RESETregister: when a later BAR0 read has returned), the device does not access local memory and accepts no access from the peer to local memory (MUST). - Since
ENABLE = 0, the link goes down. IfUPwas 1, the localLINK_GENis incremented; the localEVENT_STATUSis 0 after the reset, so noLINK_DOWNis visible locally. At the peer theLINK_DOWNevent is set andLINK_GENincremented. The peer's other state does not change. - DMA writes to the device's own BARs are refused (§9.4), so
RESETcannot be triggered by DMA.
9. DMA engine (FEATURES.DMA)¶
A simple copy engine meant to be used as a Linux dmaengine provider.
Channels are independent; there is no ordering between channels.
9.1 Address types¶
Each address in a descriptor is either a local bus address or an MW offset (into the peer's memory exposed by that MW's inbound translation). Flags state explicitly which one it is. The address type is never inferred by comparing with the device's BAR addresses; that comparison is only used to refuse (§9.4).
Allowed combinations: local→local, local→MW, MW→local. MW→MW is invalid.
9.2 DMA[c] block (BAR0 0x200 + 0x40·c)¶
| Offset | Name | Type | Reset | Description |
|---|---|---|---|---|
| +0x00 | RING_BASE_LO/HI |
RW | 0 | Local bus address of the ring, 64-byte aligned. Writable only while ENABLE = 0 |
| +0x08 | RING_SIZE |
RW | 0 | Number of entries, power of two, 2..4096. Writable only while ENABLE = 0 |
| +0x0C | CTRL |
RW | 0 | bit0 ENABLE; bit1 RESET (§9.6, reads as 0) |
| +0x10 | HEAD |
RW | 0 | Producer counter (driver) |
| +0x14 | TAIL |
RO | 0 | Consumer counter (device) |
| +0x18 | STATUS |
RO | IDLE |
bit0 IDLE; bit1 ERR |
| +0x1C | ERR_INFO |
RO | 0 | Error code (§9.5) |
| +0x20–0x3F | Reserved |
HEAD and TAIL are 32-bit counters and wrap; the entry index is
counter & (RING_SIZE − 1). The number of pending descriptors is
(HEAD − TAIL) mod 2^32.
9.3 Descriptor (32 bytes, little-endian)¶
| Offset | Size | Field |
|---|---|---|
| 0 | 8 | src |
| 8 | 8 | dst |
| 16 | 4 | len (bytes, 1..16 MiB) |
| 20 | 2 | flags |
| 22 | 1 | src_mw: MW index if the SRC_MW flag is set |
| 23 | 1 | dst_mw: MW index if the DST_MW flag is set |
| 24 | 4 | status (written by the device) |
| 28 | 4 | reserved; ignored by the device |
flags: bit0 IRQ; bit1 SRC_MW (src is an MW offset); bit2 DST_MW
(dst is an MW offset). Other bits must be 0; otherwise error 1. Without
SRC_MW/DST_MW, src_mw/dst_mw are ignored.
src, dst and len have byte granularity: no alignment is required.
status: 1 = done, 2 = error. The driver writes status = 0 before
publishing a descriptor.
9.4 Processing¶
- A channel runs while
ENABLE = 1,ERR = 0andHEAD ≠ TAIL. - The device reads each descriptor from the ring once and works from that
copy (
MUST); later changes by the driver have no effect. - Descriptors are processed one at a time in
TAILorder. The data writes of a descriptor are visible at the destination before the data writes of the next descriptor. - Validation (each failure gives an error code, §9.5):
- flags,
lenrange, MW indices (< MW_COUNT), the MW→MW ban; - local address range:
addr + lenmust not exceed 2^64; - local addresses inside the device's own BAR ranges (BAR0, BAR1, MW BARs) are refused;
- non-RAM local addresses (definition in §7.3) are refused (error 2);
- the MW side is checked at the target per §6.4 for every chunk.
- The implementation may split large descriptors into chunks (
SHOULD, ≤ 64 KiB); the MW window and the link are checked again for every chunk. - Completion order (
MUST): (1) all data is visible at the destination (for an MW: in peer memory), (2)status = 1is written to the ring, (3)TAILadvances, (4) if theIRQflag is set,DMA_DONEis set and an interrupt is sent per §5.4. - Several completions may be reported with one interrupt. On the interrupt
the driver treats all descriptors up to
TAILas complete. - No operation, whatever its length, may delay the device's answers to BAR
accesses (
MUST): DMA progresses independently of BAR accesses.
9.5 Errors¶
On an error: ERR_INFO is set, STATUS = ERR (IDLE = 0), the channel
stops, TAIL does not advance, and the DMA_ERR event is set. Only for
errors 1–4 (errors that belong to the descriptor being processed) does the
device try to write status = 2 to that descriptor; for the other errors
nothing is written to the ring. CTRL.ENABLE keeps its value from before the
error (it stays 0 for error 8, and for error 6 raised while ENABLE = 0);
the channel only runs again after CTRL.RESET. Partially written data may
remain at the destination. Later descriptors are not processed. The driver
resets the channel with CTRL.RESET (§9.6).
ERR_INFO |
Meaning |
|---|---|
| 1 | Invalid descriptor (flags, len, MW index, MW→MW) |
| 2 | Local address not accessible, overflow, or the device's own BAR |
| 3 | MW access refused at the target (outside the translation, window closed) |
| 4 | Link down (sender UP = 0, or the target does not accept) |
| 5 | Ring could not be read or written |
| 6 | Invalid HEAD (HEAD − TAIL > RING_SIZE, or HEAD = TAIL while a descriptor is in progress: the descriptor in progress counts as pending). Checked when HEAD is written (also while ENABLE = 0); the value is not stored and the channel enters the error state. The device also checks before reading each descriptor |
| 7 | BME = 0 |
| 8 | Invalid ring configuration (when ENABLE is written) |
9.6 Channel control¶
- Writing
CTRL.ENABLE = 1: ifRING_BASEis not 64-byte aligned,RING_SIZEis invalid,RING_BASE + 32·RING_SIZEexceeds 2^64, orHEAD − TAIL > RING_SIZE(ifRING_SIZEwas reduced while the channel was off), the channel is not enabled (ENABLEreads 0),ERR,ERR_INFO = 8. - Writing
CTRL.ENABLE = 0: the channel stops after the chunk in progress; if that descriptor did not complete, nostatusis written andTAILdoes not advance. Once the effect of the write is guaranteed (§4), the channel no longer accesses memory (MUST).HEADkeeps its value. HEADmay be written whileENABLE = 0; processing starts whenENABLE = 1.- Writing 1 to
CTRL.RESET(synchronous): the channel stops,ENABLE = 0,HEAD = TAIL = 0,STATUS = IDLE,ERR_INFO = 0.RING_BASEandRING_SIZEare kept. Reads as 0.
10. Rules for the Linux driver¶
This section is normative for the ntb_hw_qntb driver.
10.1 ntb_dev_ops mapping¶
| Op | Register / behaviour |
|---|---|
link_is_up |
Returns exactly 1 if LINK_STATUS.UP (Linux ntb_transport compares with == 1), otherwise 0. speed/width may be NULL. |
link_enable / link_disable |
LINK_CTRL.ENABLE = 1 / 0 |
mw_count, peer_mw_count |
MW_COUNT |
mw_get_align |
XLAT_ALIGN, XLAT_SIZE_ALIGN, MW_SIZE |
mw_set_trans |
ENABLE = 0, address/size, ENABLE = 1, check XLAT_STATUS; -EINVAL on ERR |
mw_clear_trans |
ENABLE = 0 |
peer_mw_get_addr |
MW BAR base, MW_SIZE |
db_valid_mask |
DB_VALID |
db_vector_count / db_vector_mask |
n_dbvec, §5.2 |
db_read / db_clear |
DB_STATUS / W1C |
db_set |
DB_SET |
db_read_mask / db_set_mask / db_clear_mask |
DB_MASK / DB_MASK_SET / DB_MASK_CLR |
peer_db_set |
PEER_DB_SET |
spad_count / spad_read / spad_write |
SPAD_COUNT / SPAD[k] |
peer_spad_read / peer_spad_write |
PEER_SPAD[k] |
get_dma_dev |
The PCI device's struct device |
Not provided (MUST NOT): port_number, peer_port_* (the driver sets
ntb->topo = NTB_TOPO_NONE, like Linux ntb_hw_epf), peer_mw_set_trans,
peer_mw_clear_trans, peer_db_addr, peer_spad_addr (no client calls it;
its address would only make sense as a DMA target, which §9.4 refuses),
peer_db_read/peer_db_clear/peer_db_*mask, msg_*,
db_is_unsafe/spad_is_unsafe.
10.2 Interrupts¶
- The driver allocates exactly
1 + n_dbvec + n_spareMSI-X vectors (minimum = maximum); if fewer are available, probe fails. Partial allocation is not supported. It uses1 + n_dbvecvectors with its own handlers and does not attach a handler to then_sparevectors (Linuxntb_msiuses free vectors itself). WithMSI_FWD = 0,n_spare = 0. - The handler of vector
1 + vcallsntb_db_event(ntb, v); it does not clear or mask doorbell bits (the client does). - The vector 0 handler reads
EVENT_STATUSand clears it with W1C, then callsntb_link_event()forLINK_UP/LINK_DOWN, making sure the client sees the link go down first if both are set, and handles the DMA channels for the DMA bits. - The driver unmasks the event bits it uses in
EVENT_MASK(the reset value masks all of them). - After enabling MSI-X the driver reads
EVENT_STATUSandDB_STATUSand handles anything pending (§5.5).
10.3 DMA provider¶
dma_device.dev = &pdev->dev(NUMA node filters rely on it), a 64-bit DMA mask,copy_align = DMAENGINE_ALIGN_1_BYTE,DMA_MEMCPYandDMA_PRIVATEcapabilities.- Provides
device_prep_dma_memcpy,device_issue_pending,device_tx_status,device_terminate_all,device_synchronize. - On completion
dma_descriptor_unmap()is called; errors are reported throughcallback_resultasDMA_TRANS_WRITE_FAILED/DMA_TRANS_READ_FAILED, and descriptors not processed because the channel stopped asDMA_TRANS_ABORTED. - If a source/destination address falls inside the driver's MW BAR range
(
pci_resource_start..+ MW_SIZE), the driver turns it intoDST_MW/SRC_MWplus an offset. This conversion is valid without an IOMMU (address = physical address). In 0.x, DMA is not supported with a guest IOMMU (vIOMMU): ifiommu_get_domain_for_dev(&pdev->dev)is not NULL and its type is notIOMMU_DOMAIN_IDENTITY, the driver does not register the DMA channels.
10.4 Barrier reads and lifecycle¶
Because BAR writes are posted (§4), the driver does a BAR0 read (MAGIC)
after these writes and only continues once the read has returned (MUST):
- mw_clear_trans (and the ENABLE = 0 inside mw_set_trans): before the
memory is freed,
- link_disable,
- CTRL.ENABLE = 0 or CTRL.RESET inside device_terminate_all,
- the RESET register.
The barrier read must return MAGIC. Any other value (e.g. all ones: a
platform timeout or a lost connection) does not guarantee that the write was
processed. The driver then retries the read a few times. If the device still
does not answer, the driver:
- clears PCI_COMMAND.BME. Config writes are not posted; once this write has
reached the device, the device accesses no local memory and refuses
accesses from the peer (§2, §6.4). This is the last-resort replacement for
the barrier.
- marks the device "dead"; later operations fail immediately.
Linux clients (ntb_transport, ntb_tool, ntb_perf) free the buffer
without checking the return value of mw_clear_trans, so the driver cannot
prevent the memory from being freed. The BME rule above covers this case.
Lifecycle:
- Probe: the driver does not enable the link (LINK_CTRL.ENABLE stays 0);
clients enable it with ntb_link_enable. At the start of probe the driver
writes RESET and does a BAR0 read.
- Remove: after the clients have detached, LINK_CTRL = 0, every XLAT is
closed, the DMA channels are reset, a BAR0 read is done, then the
interrupts are freed.
10.5 Testing notes¶
- Contiguous allocations larger than 4 MiB in the guest need CMA. If an MW is
larger than 4 MiB, give the guest kernel
cma=or use the clients'max_mw_sizeparameter. ntb_msitests need a guest with at most 8 vCPUs (the MSI addresses must fit in a 1 MiB window).- The
ntb_test.shMSI test reports "Passed" even when 0 interrupts were registered. On devices withMSI_FWD = 1the test harness also checks that the number of registered interrupts is > 0; on devices withMSI_FWD = 0(ntbd) the MSI test is reported as "unsupported", not as "passed".
11. Configuration parameters and compatibility¶
| Parameter | Default | Range | Between ends |
|---|---|---|---|
mw_count |
2 | 1–2 | equal (MUST) |
mw0_size, mw1_size |
4 MiB, 1 MiB | power of two, at most 1 GiB, ≥ XLAT_ALIGN (≥ 1 MiB for the highest-index MW; with mw_count = 1, mw0_size ≥ 1 MiB), others ≥ 4 KiB |
equal (MUST) |
spads |
16 | 9–128 | equal (MUST) |
doorbells (db_count) |
32 | 4–64 | equal (MUST) |
db_vectors (n_dbvec) |
4 | 1–32, ≤ db_count |
free |
msix_spare (n_spare) |
4 | 0–32 | free |
dma_channels |
4 | 0–8 | free |
Table size limit: 1 + n_dbvec + n_spare ≤ 128.
"Equal" parameters are compared when the connection is set up. On a
mismatch, CFG_MISMATCH = 1, PEER_PRESENT stays 0 and the link does not
come up (MUST).
12. Deliberately out of scope (0.x)¶
- Message registers.
- More than two ends, multi-port topologies.
- Outbound translation (
peer_mw_set_trans),peer_db_addr. - INTx and MSI (MSI-X only).
- Live migration: implementations block migration (
MUST). (Known exception: QEMU 11.1.3's vfio-user client adds no blocker for servers that cannot migrate, and ntbd cannot block it on its side. Seedocs/UPSTREAM-NOTES.md§3. The QEMU-internal device usesmigrate_add_blocker().) - The DMA engine with a guest IOMMU (§10.3). MW accesses and XLAT addresses work with an IOMMU in principle (addresses are bus addresses as the device sees them), but this has not been verified.
13. Ordering and visibility guarantees¶
The operations from one end (A) towards the peer (B) are: MW writes, A's
local SPAD writes (visible through B's PEER_SPAD reads), PEER_SPAD
writes, PEER_DB_SET writes, and the data writes of completed descriptors of
A's DMA channels.
- Program order (
MUST): such operations of a CPU in A that have reached the device become visible in B in the same order. In particular: if A writes to an MW and then writesPEER_DB_SET, the data is visible in B's memory when B receives the interrupt. Operations A makes after seeing a DMA descriptor complete are visible after that descriptor's data. - Write combining: the guarantee starts when a write reaches the device.
After write-combined MW writes, the driver must use a write barrier
(
wmb()) before ringing a doorbell. - MW reads are non-posted: they are performed after the writes A sent earlier to the same peer.
- Link: operations A sends after seeing
UP = 1are applied in B after A's enable has been processed (transport order). The §6.2 barrier holds in both directions. - MSI forwarding (
MSI_FWD): the triggered interrupt is visible after earlier writes from the same end. - Connections and sessions:
PEER_PRESENTbecomes 1 on both ends only after the two-way handshake has completed. - Operations left over from an earlier session: the target end has an
acceptance number that increments whenever its
ENABLEbit goes to 1 and on every reset; the number belongs to the target and is sent to the peer through the transport. In a two-process implementation, the sender tags every peer-directed operation with the latest acceptance number it knows; the target drops operations whose number differs from its current one (MUST). This rule cannot cover the guest's own posted writes: a write still queued in the sending VM's platform may be processed after the target was closed and opened again. ntbd mitigates this with the minimum 100 ms down time of §6.1 (MUSTfor ntbd). This is not an isolation breach (the write can only land in a window the target has open at that moment), but, as with real hardware, drivers must reinitialise shared buffers when the link comes back.
If the transport uses a fast path (e.g. a direct eventfd for doorbells, a
shared memory mapping for MWs), it must not break these guarantees (MUST).
Before sending a control message, the implementation makes its earlier writes
to shared memory visible with a memory barrier.
Annex A. Implementation notes (non-normative)¶
- ntbd (libvfio-user): one process, one thread, one
vfu_ctxper VM. Peer memory is accessed only through mapped DMA regions; libvfio-user's message-based DMA path is not used (it can race with posted messages on the same socket). BAR access callbacks always complete the full count. Long DMAs are processed in chunks in the main loop (QEMU waits at most 5 s for a reply). When the memory mapping used by the peer is removed, the corresponding accesses are dropped. The MSI-X table is emulated by QEMU, soMSI_FWD = 0. Guest RAM must be shareable (memory-backend-memfd,share=on); otherwise ntbd cannot access guest memory: it logs this once per connection and refuses MW accesses into that memory (§6.4). ntbd meets the barrier rule of §4 as follows: a VM's write and read messages arrive in order on the same socket; when a read is answered, the earlier writes have been processed, and since ntbd runs in one thread, later accesses from the other VM see the new state. Before answering a register read, ntbd also processes (bounded) the requests already waiting on the other end's socket, so that a guest that writes to one end and then reads the other end (for examplePEER_DB_SETfollowed byDB_STATUSin a loopback setup) sees its own write. ntbd offers no mmap areas for any region (accesses would otherwise bypass its checks), uses libvfio-user in non-blocking mode and registers no quiesce callback (libvfio-user then always treats the device as quiesced; all work is done synchronously in the same thread). - QEMU-internal device (
qntb): all accesses to the peer are messages; guest RAM need not be shared. Messages from the peer are applied by a thread that never takes the BQL (device state has its own lock, guest RAM is accessed directly, interrupts are raised from a bottom half), so a vCPU may wait for the peer while holding the BQL without two QEMUs deadlocking. The barrier rule of §4 is met by a SYNC round trip on every BAR0 read. A peer that sends nothing for 2 s, keepalives included, counts as lost. MSI forwarding compares inbound 4-byte writes with the table throughmsix_get_message(). Withfast-mw=onand KVM, a window onto shared RAM is mapped into the sender's MW BAR (the target passes the RAM's file descriptor). §7.2 is kept without waiting in the write: the target sends REVOKE before its next SYNC, the sender's receive thread replaces the mapping as soon as it reads it (no BQL needed), so the barrier read returns only after the mapping is gone. A reset and BME = 0 do the SYNC themselves. A peer process that is frozen cannot confirm; after 2 s the link is dropped (seedocs/qemu-device.md, Limitations).
Annex B. Change history¶
| Version | Date | Change |
|---|---|---|
| 0.1 | 2026-11-24 | First draft |
| 0.2 | 2026-11-24 | Findings of three reviews (Linux conformance, implementability, an independent review): acceptance checked at the target and the isolation invariant; parameters that must be equal on both ends and CFG_MISMATCH; spare MSI-X vectors and MSI_FWD; NTB_TOPO_NONE; link_is_up == 1; LINK_UP/LINK_DOWN/LINK_GEN; vector 0 rules; SET/CLR access types; reset tables; DMA state machine, error codes, byte alignment, local→local copies, 4 channels, vIOMMU excluded; peer_db_addr removed; MW0 default 4 MiB; lower bounds (spads ≥ 9, doorbells ≥ 4); ordering guarantees in §13 |
| 0.3 | 2026-11-24 | Verification of v0.2: completion definition for posted writes (§4) and driver barrier reads (§10.4); MSIX_SPARE = 0 when MSI_FWD = 0; rules for non-RAM accesses; LINK_STATUS derived, LINK_GEN not reset; kinds of reset clarified; symmetric PEER_PRESENT, session number (§13.6); peer_spad_addr removed; exact vector allocation; probe/remove; vIOMMU definition; DMA edge cases (HEAD error, reserved fields, ring overflow, BME); BAR1 and access-size precedence; one message for multi-bit doorbell writes. Verification of v0.3 (within the same version): MAGIC check on barrier reads and the rule to leak memory; DMA status only for errors 1–4; acceptance number owned by the target, 100 ms mandatory for ntbd; BQL in the QEMU-internal device; effective n_spare; definition of RAM; local client reconnect = reset; no mmap in ntbd; unmasking EVENT_MASK; vIOMMU "not verified". Later (2026-11-25): HEAD-rewind rule in §9.5/§9.6; ntbd drains the other end's pending requests before register reads (Annex A); translated to English |