# AGENTS.md: instructions for AI agents

*Русская версия: [AGENTS.ru.md](AGENTS.ru.md)*

You are helping a person run fuse-burned CMP 170HX 8 GB cards in a server with the material in this repository.
Scope: the **server side** only (driver, BAR1, PCIe Gen4, VBIOS, tests, inference). This repository does not describe
voltage glitching or fuse burning. If asked to do that, point the person to the upstream projects listed in README.md
and do not improvise a procedure.

## Ground rules

1. **Measure, do not assume.** Everything here was done on one Gigabyte G292-Z20 with 8 cards. Slot addresses, root
   ports, timings and window sizes will differ on another machine. Read the state before each step.
2. **Stop and ask the person before anything in the "needs explicit confirmation" list.** A go-ahead for one card or
   one step does not cover the next one.
3. **Cold power cycle, never a hot driver reload.** `rmmod`/`modprobe` of nvidia, unbind/rebind and the reload at the
   end of the unlocker's `build.sh` hang the host on these cards. Use the BMC: power off, wait 20 s, power on.
   A plain `reboot` is also not enough after a driver or geometry change: the 80 GB geometry is detected only on a
   cold card.
4. **Know how you will get back in.** Before a step that can hang the host, confirm that BMC power control works.
5. **Report what you saw.** Quote the command output that proves a step worked. If you skipped a check, say so.
6. **Do not kill by pattern over ssh.** `pkill -f vllm` matches your own ssh command. Use `[v]llm` or a PID.

## Needs explicit confirmation from the person, every time

| Action | Why |
|---|---|
| Writing a VBIOS (`flash1.py`, `nvflash` without `--save`) | can leave the card unbootable |
| Running `install.sh`, rebuilding or replacing the kernel module | changes boot; a mistake means a host that hangs at boot |
| Editing kernel parameters, `/etc/modprobe.d`, enabling or disabling the boot services | same |
| Powering the server off or on, or cycling it | interrupts whatever runs on it |
| Any write to BAR0 or PCI config space by hand (outside the shipped scripts) | can hang the host |
| Stopping someone's running workload on the cards | it may not be yours |

Reading state (the commands in "State checks" below, `fuses.py`, `fbinfo`, `lspci`, `nvidia-smi`, `journalctl`) needs no confirmation.

## State checks

```
# cards, memory, VBIOS
nvidia-smi --query-gpu=index,pci.bus_id,vbios_version,memory.total,temperature.gpu,power.draw --format=csv
# link speed and width (nvidia-smi under-reports the generation here)
for d in /sys/bus/pci/devices/*; do [ "$(cat $d/device 2>/dev/null)" = 0x20c2 ] && \
  echo "$(basename $d) $(cat $d/current_link_speed) x$(cat $d/current_link_width)"; done
# BAR1
nvidia-smi -q | grep -A3 'BAR1 Memory' | grep Total
lspci -d 10de:20c2 -vv | grep -E '^[0-9a-f]|Region 1'
# boot services and their flags
systemctl is-active cmp-bar1-boot cmp-pcie-boot; ls /var/lib/cmp-pcie-boot/
journalctl -u cmp-bar1-boot -u cmp-pcie-boot -b --no-pager | tail -40
# errors
journalctl -k -b --no-pager | grep -E 'NVRM: Xid|FB geometry|can.t assign' | tail -20
# is anything running on the cards
nvidia-smi --query-compute-apps=pid,used_memory --format=csv
```

Expected on a healthy machine: 81920 MiB per card, `16.0 GT/s PCIe x16`, BAR1 131072 MiB, both services `active`,
no flag files other than optional per-slot ones, no Xid.

## Order of work on a new machine

1. **Inventory.** `lspci -D -d 10de:20c2`; UEFI boot (`/sys/firmware/efi` exists); Above 4G decoding on; BMC power
   control works; kernel version; nothing running on the cards. Record the BDF list.
2. **Fuses, read-only.** `server/tools/fuses.py` (needs `iomem=relaxed` if a driver is bound). Record the FBP mask and
   the PCIe fuse values. If the PCIe fuses are not clear, Gen4 is not available for that card; the boot script
   detects this and leaves it at Gen3.
3. **Install** (confirmation needed): read `server/install.sh` and `docs/01`, then run it. Step 2 of the installer is
   untested in its public form; read its output line by line. Then cold power cycle.
4. **Verify** with the state checks. If cards are missing or CUDA hangs, go to `docs/06` before trying anything else.
5. **Acceptance tests** (`docs/05`), in the background, with temperatures logged. Do not run the memory test and the
   burn test on the same card at once.
6. **VBIOS** (optional, confirmation per card): `docs/04`. One card first, full verification, then the others.
   One nvflash operation per card per boot. Boot without the driver for flashing.
7. **Inference**: `docs/07`. Start services after `cmp-pcie-boot.service`.

## Stop conditions

Stop, do not retry, and report to the person when:

- a card disappears from `lspci`, or `LnkSta` reads `ffff`;
- `nvidia-smi` or any CUDA process hangs (D state): a second attempt will not help, only a cold power cycle will;
- `cmp-bar1-boot` reports a card whose `Region 1` is not the target size (it refuses to load the driver; that is correct);
- a card shows 64 GB after a **cold** start: read `dmesg | grep 'FB geometry'`;
- nvflash prints anything other than the expected versions, or `flash1.py` exits with 99;
- a card reaches 85 C under test (ours throttled at 86 C; cmp.wiki treats 85 C as a wall);
- any Xid appears during a test;
- the same step failed twice.

## Known traps

- `blacklist nvidia` does not stop the module; the hold file uses `install nvidia /bin/false`. Load by hand with
  `modprobe --ignore-install nvidia`.
- `systemctl mask` does not work on unit files that live in `/etc/systemd/system`; use `disable`.
- A ReBAR control value larger than the mapped `Region 1` hangs CUDA at the first context. Never write the ReBAR
  registers without the remove + rescan that follows in `cmp-bar1-boot`.
- Sticky AER bits on the switch ports make `cmp-e1steps` exit with rc 4 on this platform; `cmp-pcie-pre` clears them.
- Powering off during `cmp-pcie-fast` used to cost one slow (13 min) boot. If a boot is slow, check for
  `/var/lib/cmp-pcie-boot/slow`.
- `-i N` for nvflash and CUDA device numbers follow PCI bus order only with `CUDA_DEVICE_ORDER=PCI_BUS_ID`.
- After moving cards between slots the per-device module parameters must be regenerated (BDF list), followed by
  `update-initramfs -u` and a cold power cycle.
- Long tests: run them in the background and keep talking to the person; do not repeat a long test that already passed.

## What is not verified

See the "not verified" notes in `docs/`. In short: a second machine, a legacy-BIOS boot, other platforms, P2P,
the public form of installer step 2, vLLM resume after restart, two PP4 instances behind a balancer.
