The idea: a bus made of packets
Old PCI was a shared bus: every card sat on the same wires and took turns. PCI Express (PCIe) keeps PCI's software view (configuration space, BARs, bus:device.function numbers) but replaces the wires with point-to-point links and a tree of switches. Everything that crosses a link is a packet. A read, a write, a reply and even an interrupt are packets called TLPs (transaction layer packets).
The tree's top is the root complex: it connects the CPU and DRAM to the links through root ports. At the bottom are endpoints, the devices: here an NVMe SSD and a network card (NIC). Every function has an ID, written bus:device.function: the SSD is 03:00.0 (bus 3, device 0, function 0). At boot the firmware gives each device's BAR (base address register) a range of physical addresses, and programs each switch port with a window that covers the BARs below it.
Three layers and the TLP header
PCIe has three layers, like a small network stack:
- The transaction layer builds the TLP: a 3 or 4 DW header (1 DW = 4 bytes) and the data. It is the only part that switches look at end to end.
- The data link layer works on one link only. It adds a 12-bit sequence number in front and a 32-bit LCRC behind, and makes sure every TLP arrives once and in order.
- The physical layer sends the bytes over the lanes. Gen3 runs 8 GT/s per lane with 128b/130b encoding, so an x4 link carries about 3.9 GB/s each way.
The header's first byte says what the TLP is: Fmt (header size, with or without data) and Type. The three TLPs on this page:
| TLP | Fmt / Type | What it is | Routed by |
|---|---|---|---|
| MRd | 000 / 00000 | Memory Read Request, no data, non-posted | address |
| MWr | 010 / 00000 | Memory Write Request with data, posted | address |
| CplD | 010 / 01010 | Completion with Data | ID (requester's bus) |
A request carries the requester ID (who asked) and a tag. The completion copies both, so the requester can match the answer to the question even when several reads are in flight.
Routing by address and by ID
A switch is a set of PCI-to-PCI bridges. Each downstream port has a memory base/limit window and a secondary/subordinate bus range. When a request arrives, the switch compares its address with the windows: 0xFE00001C is inside FE000000–FE0FFFFF, so it goes out of port 02:00.0. An address that no window covers goes upstream. A completion has no address. The switch reads the bus number of its requester ID instead: bus 0 is outside buses 2–4, so it goes up. Demo 1: CPU reads an NVMe register shows both decisions.
Posted and non-posted
A read is non-posted: the requester waits for a completion. A memory write is posted: once it is sent, the writer is done, and no completion ever comes back. That is why DMA writes are fast: the NIC can stream frames into memory without waiting for a round trip (Demo 2: NIC DMA write). It also means the writer gets no error report; ordering rules make sure a later read sees the write.
Ack, Nak and the replay buffer
The sender of each link keeps every TLP it sends in a replay buffer, under its sequence number (NEXT_TRANSMIT_SEQ counts up). The receiver checks the LCRC and that the sequence number is the one it expects (NEXT_RCV_SEQ). If both are fine it answers with an Ack DLLP (data link layer packet); Ack 7 means "7 and everything before it", and the sender drops them from the replay buffer. If the LCRC is wrong the receiver throws the TLP away and sends a Nak with the last good number. The sender then replays everything after it, unchanged (Demo 3: corrupted TLP). Software never sees this. A replay timer also resends if no Ack comes at all.
Credit-based flow control
A PCIe receiver never drops a TLP because it is full. Instead, before anything is sent, each receiver tells the other end how much buffer it has, in credits, separately for posted (PH, PD), non-posted (NPH, NPD) and completion (CplH, CplD) TLPs. One header credit holds one header; one data credit holds 16 bytes. A transmitter may send a TLP only when it has enough credits left. When the receiver has passed a TLP on and freed its buffer, it returns the credits with an UpdateFC DLLP.
On the page the root port has room for 2 posted headers and 8 data credits (128 bytes), small on purpose. A 128-byte write needs 1 PH and 8 PD, so after one write the second must wait at the switch until UpdateFC-P arrives (Demo 4: out of credits). Endpoints must advertise infinite completion credits, and the root port does here too (∞): a device that asks for data must have room for the answer.
Link training
Before any TLP can flow, the two ends of a link train it with ordered sets (TS1, TS2) in the physical layer's LTSSM: Detect (is there a receiver on each lane?), Polling (bit and symbol lock), Configuration (agree on the width, here x4, and the lane numbers), then L0, the working state, at Gen1 speed (2.5 GT/s). Both ends then advertise their speeds and go through Recovery to change to Gen3, 8 GT/s, with equalization. The data link layer then exchanges InitFC DLLPs to set the starting credits. The page starts with all links already up at x4 Gen3.
See also DMA, which follows a device writing into memory from the driver's side, and CRC, the kind of checksum the LCRC is.
What the page leaves out
The page draws the data link layer and credits only for link 1; links 2 and 3 do the same. It leaves out configuration requests (CfgRd0/CfgWr0, routed by ID during enumeration), 4 DW headers for 64-bit addresses, NPD credits, messages and MSI interrupts in detail, the replay timer and replay number rollover, ordering rules (a posted write may pass a read, not the other way), relaxed ordering and ID-based ordering, ECRC, poisoned TLPs and error reporting (AER), Max Read Request Size and split completions, the 8b/10b and 128b/130b encodings, power states (L0s, L1) and Gen4–Gen6 (PAM4, FLIT mode).