0001 .. SPDX-License-Identifier: GPL-2.0
0002
0003 ======================
0004 USB4 and Thunderbolt
0005 ======================
0006 USB4 is the public specification based on Thunderbolt 3 protocol with
0007 some differences at the register level among other things. Connection
0008 manager is an entity running on the host router (host controller)
0009 responsible for enumerating routers and establishing tunnels. A
0010 connection manager can be implemented either in firmware or software.
0011 Typically PCs come with a firmware connection manager for Thunderbolt 3
0012 and early USB4 capable systems. Apple systems on the other hand use
0013 software connection manager and the later USB4 compliant devices follow
0014 the suit.
0015
0016 The Linux Thunderbolt driver supports both and can detect at runtime which
0017 connection manager implementation is to be used. To be on the safe side the
0018 software connection manager in Linux also advertises security level
0019 ``user`` which means PCIe tunneling is disabled by default. The
0020 documentation below applies to both implementations with the exception that
0021 the software connection manager only supports ``user`` security level and
0022 is expected to be accompanied with an IOMMU based DMA protection.
0023
0024 Security levels and how to use them
0025 -----------------------------------
0026 The interface presented here is not meant for end users. Instead there
0027 should be a userspace tool that handles all the low-level details, keeps
0028 a database of the authorized devices and prompts users for new connections.
0029
0030 More details about the sysfs interface for Thunderbolt devices can be
0031 found in ``Documentation/ABI/testing/sysfs-bus-thunderbolt``.
0032
0033 Those users who just want to connect any device without any sort of
0034 manual work can add following line to
0035 ``/etc/udev/rules.d/99-local.rules``::
0036
0037 ACTION=="add", SUBSYSTEM=="thunderbolt", ATTR{authorized}=="0", ATTR{authorized}="1"
0038
0039 This will authorize all devices automatically when they appear. However,
0040 keep in mind that this bypasses the security levels and makes the system
0041 vulnerable to DMA attacks.
0042
0043 Starting with Intel Falcon Ridge Thunderbolt controller there are 4
0044 security levels available. Intel Titan Ridge added one more security level
0045 (usbonly). The reason for these is the fact that the connected devices can
0046 be DMA masters and thus read contents of the host memory without CPU and OS
0047 knowing about it. There are ways to prevent this by setting up an IOMMU but
0048 it is not always available for various reasons.
0049
0050 Some USB4 systems have a BIOS setting to disable PCIe tunneling. This is
0051 treated as another security level (nopcie).
0052
0053 The security levels are as follows:
0054
0055 none
0056 All devices are automatically connected by the firmware. No user
0057 approval is needed. In BIOS settings this is typically called
0058 *Legacy mode*.
0059
0060 user
0061 User is asked whether the device is allowed to be connected.
0062 Based on the device identification information available through
0063 ``/sys/bus/thunderbolt/devices``, the user then can make the decision.
0064 In BIOS settings this is typically called *Unique ID*.
0065
0066 secure
0067 User is asked whether the device is allowed to be connected. In
0068 addition to UUID the device (if it supports secure connect) is sent
0069 a challenge that should match the expected one based on a random key
0070 written to the ``key`` sysfs attribute. In BIOS settings this is
0071 typically called *One time saved key*.
0072
0073 dponly
0074 The firmware automatically creates tunnels for Display Port and
0075 USB. No PCIe tunneling is done. In BIOS settings this is
0076 typically called *Display Port Only*.
0077
0078 usbonly
0079 The firmware automatically creates tunnels for the USB controller and
0080 Display Port in a dock. All PCIe links downstream of the dock are
0081 removed.
0082
0083 nopcie
0084 PCIe tunneling is disabled/forbidden from the BIOS. Available in some
0085 USB4 systems.
0086
0087 The current security level can be read from
0088 ``/sys/bus/thunderbolt/devices/domainX/security`` where ``domainX`` is
0089 the Thunderbolt domain the host controller manages. There is typically
0090 one domain per Thunderbolt host controller.
0091
0092 If the security level reads as ``user`` or ``secure`` the connected
0093 device must be authorized by the user before PCIe tunnels are created
0094 (e.g the PCIe device appears).
0095
0096 Each Thunderbolt device plugged in will appear in sysfs under
0097 ``/sys/bus/thunderbolt/devices``. The device directory carries
0098 information that can be used to identify the particular device,
0099 including its name and UUID.
0100
0101 Authorizing devices when security level is ``user`` or ``secure``
0102 -----------------------------------------------------------------
0103 When a device is plugged in it will appear in sysfs as follows::
0104
0105 /sys/bus/thunderbolt/devices/0-1/authorized - 0
0106 /sys/bus/thunderbolt/devices/0-1/device - 0x8004
0107 /sys/bus/thunderbolt/devices/0-1/device_name - Thunderbolt to FireWire Adapter
0108 /sys/bus/thunderbolt/devices/0-1/vendor - 0x1
0109 /sys/bus/thunderbolt/devices/0-1/vendor_name - Apple, Inc.
0110 /sys/bus/thunderbolt/devices/0-1/unique_id - e0376f00-0300-0100-ffff-ffffffffffff
0111
0112 The ``authorized`` attribute reads 0 which means no PCIe tunnels are
0113 created yet. The user can authorize the device by simply entering::
0114
0115 # echo 1 > /sys/bus/thunderbolt/devices/0-1/authorized
0116
0117 This will create the PCIe tunnels and the device is now connected.
0118
0119 If the device supports secure connect, and the domain security level is
0120 set to ``secure``, it has an additional attribute ``key`` which can hold
0121 a random 32-byte value used for authorization and challenging the device in
0122 future connects::
0123
0124 /sys/bus/thunderbolt/devices/0-3/authorized - 0
0125 /sys/bus/thunderbolt/devices/0-3/device - 0x305
0126 /sys/bus/thunderbolt/devices/0-3/device_name - AKiTiO Thunder3 PCIe Box
0127 /sys/bus/thunderbolt/devices/0-3/key -
0128 /sys/bus/thunderbolt/devices/0-3/vendor - 0x41
0129 /sys/bus/thunderbolt/devices/0-3/vendor_name - inXtron
0130 /sys/bus/thunderbolt/devices/0-3/unique_id - dc010000-0000-8508-a22d-32ca6421cb16
0131
0132 Notice the key is empty by default.
0133
0134 If the user does not want to use secure connect they can just ``echo 1``
0135 to the ``authorized`` attribute and the PCIe tunnels will be created in
0136 the same way as in the ``user`` security level.
0137
0138 If the user wants to use secure connect, the first time the device is
0139 plugged a key needs to be created and sent to the device::
0140
0141 # key=$(openssl rand -hex 32)
0142 # echo $key > /sys/bus/thunderbolt/devices/0-3/key
0143 # echo 1 > /sys/bus/thunderbolt/devices/0-3/authorized
0144
0145 Now the device is connected (PCIe tunnels are created) and in addition
0146 the key is stored on the device NVM.
0147
0148 Next time the device is plugged in the user can verify (challenge) the
0149 device using the same key::
0150
0151 # echo $key > /sys/bus/thunderbolt/devices/0-3/key
0152 # echo 2 > /sys/bus/thunderbolt/devices/0-3/authorized
0153
0154 If the challenge the device returns back matches the one we expect based
0155 on the key, the device is connected and the PCIe tunnels are created.
0156 However, if the challenge fails no tunnels are created and error is
0157 returned to the user.
0158
0159 If the user still wants to connect the device they can either approve
0160 the device without a key or write a new key and write 1 to the
0161 ``authorized`` file to get the new key stored on the device NVM.
0162
0163 De-authorizing devices
0164 ----------------------
0165 It is possible to de-authorize devices by writing ``0`` to their
0166 ``authorized`` attribute. This requires support from the connection
0167 manager implementation and can be checked by reading domain
0168 ``deauthorization`` attribute. If it reads ``1`` then the feature is
0169 supported.
0170
0171 When a device is de-authorized the PCIe tunnel from the parent device
0172 PCIe downstream (or root) port to the device PCIe upstream port is torn
0173 down. This is essentially the same thing as PCIe hot-remove and the PCIe
0174 toplogy in question will not be accessible anymore until the device is
0175 authorized again. If there is storage such as NVMe or similar involved,
0176 there is a risk for data loss if the filesystem on that storage is not
0177 properly shut down. You have been warned!
0178
0179 DMA protection utilizing IOMMU
0180 ------------------------------
0181 Recent systems from 2018 and forward with Thunderbolt ports may natively
0182 support IOMMU. This means that Thunderbolt security is handled by an IOMMU
0183 so connected devices cannot access memory regions outside of what is
0184 allocated for them by drivers. When Linux is running on such system it
0185 automatically enables IOMMU if not enabled by the user already. These
0186 systems can be identified by reading ``1`` from
0187 ``/sys/bus/thunderbolt/devices/domainX/iommu_dma_protection`` attribute.
0188
0189 The driver does not do anything special in this case but because DMA
0190 protection is handled by the IOMMU, security levels (if set) are
0191 redundant. For this reason some systems ship with security level set to
0192 ``none``. Other systems have security level set to ``user`` in order to
0193 support downgrade to older OS, so users who want to automatically
0194 authorize devices when IOMMU DMA protection is enabled can use the
0195 following ``udev`` rule::
0196
0197 ACTION=="add", SUBSYSTEM=="thunderbolt", ATTRS{iommu_dma_protection}=="1", ATTR{authorized}=="0", ATTR{authorized}="1"
0198
0199 Upgrading NVM on Thunderbolt device, host or retimer
0200 ----------------------------------------------------
0201 Since most of the functionality is handled in firmware running on a
0202 host controller or a device, it is important that the firmware can be
0203 upgraded to the latest where possible bugs in it have been fixed.
0204 Typically OEMs provide this firmware from their support site.
0205
0206 There is also a central site which has links where to download firmware
0207 for some machines:
0208
0209 `Thunderbolt Updates <https://thunderbolttechnology.net/updates>`_
0210
0211 Before you upgrade firmware on a device, host or retimer, please make
0212 sure it is a suitable upgrade. Failing to do that may render the device
0213 in a state where it cannot be used properly anymore without special
0214 tools!
0215
0216 Host NVM upgrade on Apple Macs is not supported.
0217
0218 Once the NVM image has been downloaded, you need to plug in a
0219 Thunderbolt device so that the host controller appears. It does not
0220 matter which device is connected (unless you are upgrading NVM on a
0221 device - then you need to connect that particular device).
0222
0223 Note an OEM-specific method to power the controller up ("force power") may
0224 be available for your system in which case there is no need to plug in a
0225 Thunderbolt device.
0226
0227 After that we can write the firmware to the non-active parts of the NVM
0228 of the host or device. As an example here is how Intel NUC6i7KYK (Skull
0229 Canyon) Thunderbolt controller NVM is upgraded::
0230
0231 # dd if=KYK_TBT_FW_0018.bin of=/sys/bus/thunderbolt/devices/0-0/nvm_non_active0/nvmem
0232
0233 Once the operation completes we can trigger NVM authentication and
0234 upgrade process as follows::
0235
0236 # echo 1 > /sys/bus/thunderbolt/devices/0-0/nvm_authenticate
0237
0238 If no errors are returned, the host controller shortly disappears. Once
0239 it comes back the driver notices it and initiates a full power cycle.
0240 After a while the host controller appears again and this time it should
0241 be fully functional.
0242
0243 We can verify that the new NVM firmware is active by running the following
0244 commands::
0245
0246 # cat /sys/bus/thunderbolt/devices/0-0/nvm_authenticate
0247 0x0
0248 # cat /sys/bus/thunderbolt/devices/0-0/nvm_version
0249 18.0
0250
0251 If ``nvm_authenticate`` contains anything other than 0x0 it is the error
0252 code from the last authentication cycle, which means the authentication
0253 of the NVM image failed.
0254
0255 Note names of the NVMem devices ``nvm_activeN`` and ``nvm_non_activeN``
0256 depend on the order they are registered in the NVMem subsystem. N in
0257 the name is the identifier added by the NVMem subsystem.
0258
0259 Upgrading on-board retimer NVM when there is no cable connected
0260 ---------------------------------------------------------------
0261 If the platform supports, it may be possible to upgrade the retimer NVM
0262 firmware even when there is nothing connected to the USB4
0263 ports. When this is the case the ``usb4_portX`` devices have two special
0264 attributes: ``offline`` and ``rescan``. The way to upgrade the firmware
0265 is to first put the USB4 port into offline mode::
0266
0267 # echo 1 > /sys/bus/thunderbolt/devices/0-0/usb4_port1/offline
0268
0269 This step makes sure the port does not respond to any hotplug events,
0270 and also ensures the retimers are powered on. The next step is to scan
0271 for the retimers::
0272
0273 # echo 1 > /sys/bus/thunderbolt/devices/0-0/usb4_port1/rescan
0274
0275 This enumerates and adds the on-board retimers. Now retimer NVM can be
0276 upgraded in the same way than with cable connected (see previous
0277 section). However, the retimer is not disconnected as we are offline
0278 mode) so after writing ``1`` to ``nvm_authenticate`` one should wait for
0279 5 or more seconds before running rescan again::
0280
0281 # echo 1 > /sys/bus/thunderbolt/devices/0-0/usb4_port1/rescan
0282
0283 This point if everything went fine, the port can be put back to
0284 functional state again::
0285
0286 # echo 0 > /sys/bus/thunderbolt/devices/0-0/usb4_port1/offline
0287
0288 Upgrading NVM when host controller is in safe mode
0289 --------------------------------------------------
0290 If the existing NVM is not properly authenticated (or is missing) the
0291 host controller goes into safe mode which means that the only available
0292 functionality is flashing a new NVM image. When in this mode, reading
0293 ``nvm_version`` fails with ``ENODATA`` and the device identification
0294 information is missing.
0295
0296 To recover from this mode, one needs to flash a valid NVM image to the
0297 host controller in the same way it is done in the previous chapter.
0298
0299 Networking over Thunderbolt cable
0300 ---------------------------------
0301 Thunderbolt technology allows software communication between two hosts
0302 connected by a Thunderbolt cable.
0303
0304 It is possible to tunnel any kind of traffic over a Thunderbolt link but
0305 currently we only support Apple ThunderboltIP protocol.
0306
0307 If the other host is running Windows or macOS, the only thing you need to
0308 do is to connect a Thunderbolt cable between the two hosts; the
0309 ``thunderbolt-net`` driver is loaded automatically. If the other host is
0310 also Linux you should load ``thunderbolt-net`` manually on one host (it
0311 does not matter which one)::
0312
0313 # modprobe thunderbolt-net
0314
0315 This triggers module load on the other host automatically. If the driver
0316 is built-in to the kernel image, there is no need to do anything.
0317
0318 The driver will create one virtual ethernet interface per Thunderbolt
0319 port which are named like ``thunderbolt0`` and so on. From this point
0320 you can either use standard userspace tools like ``ifconfig`` to
0321 configure the interface or let your GUI handle it automatically.
0322
0323 Forcing power
0324 -------------
0325 Many OEMs include a method that can be used to force the power of a
0326 Thunderbolt controller to an "On" state even if nothing is connected.
0327 If supported by your machine this will be exposed by the WMI bus with
0328 a sysfs attribute called "force_power".
0329
0330 For example the intel-wmi-thunderbolt driver exposes this attribute in:
0331 /sys/bus/wmi/devices/86CCFD48-205E-4A77-9C48-2021CBEDE341/force_power
0332
0333 To force the power to on, write 1 to this attribute file.
0334 To disable force power, write 0 to this attribute file.
0335
0336 Note: it's currently not possible to query the force power state of a platform.