One way to determinate the generation of a slot where a device is attached is by its bandwidth.
+------------------+------------------------------------+
| PCI Express Gen | Bandwidth per line (x4 slot width) |
+------------------+------------------------------------+
| PCIe Gen1 | 2 GT/s |
| PCIe Gen2 | 4 GT/s |
| PCIe Gen3 | 8 GT/s |
| PCIe Gen4 | 16 GT/s |
+------------------+------------------------------------+
First we need to identify at which PCIe slot the device is attached.
$ lspci | grep -i nvme
08:00.0 Non-Volatile memory controller: Samsung Electronics Co Ltd NVMe SSD Controller 980
Then we can get a detailed information about the slot and the device (and filter the necessary lines) by the following commands. Note using sudo is needed to get the verbose information.
1. Get the generation of the attached device.
$ sudo lspci -vv -s 08:00.0 | grep -w LnkCap
LnkCap: Port #0, Speed 8GT/s, Width x4, ASPM L1, Exit Latency L1 <64us
Here we can see the device supports up to 8 GT/s thence it is Gen3.
2. Get the generation of the PCIe itself.
$ sudo lspci -vv -s 08 | grep -w LnkCap
LnkCap: Port #0, Speed 16GT/s, Width x16, ASPM L0s L1, Exit Latency L0s <64ns, L1 <1us
LnkCap: Port #0, Speed 16GT/s, Width x16, ASPM L0s L1, Exit Latency L0s <64ns, L1 <1us
Here we can see the slot supports up to 16 GT/s thence it is Gen4.