1ea81896dSMika Westerberg.. SPDX-License-Identifier: GPL-2.0 2ea81896dSMika Westerberg 3ea81896dSMika Westerberg====================== 4ea81896dSMika Westerberg USB4 and Thunderbolt 5ea81896dSMika Westerberg====================== 6ea81896dSMika WesterbergUSB4 is the public specification based on Thunderbolt 3 protocol with 7ea81896dSMika Westerbergsome differences at the register level among other things. Connection 8ea81896dSMika Westerbergmanager is an entity running on the host router (host controller) 9ea81896dSMika Westerbergresponsible for enumerating routers and establishing tunnels. A 10ea81896dSMika Westerbergconnection manager can be implemented either in firmware or software. 11ea81896dSMika WesterbergTypically PCs come with a firmware connection manager for Thunderbolt 3 12ea81896dSMika Westerbergand early USB4 capable systems. Apple systems on the other hand use 13ea81896dSMika Westerbergsoftware connection manager and the later USB4 compliant devices follow 14ea81896dSMika Westerbergthe suit. 15ea81896dSMika Westerberg 16ea81896dSMika WesterbergThe Linux Thunderbolt driver supports both and can detect at runtime which 17ea81896dSMika Westerbergconnection manager implementation is to be used. To be on the safe side the 18ea81896dSMika Westerbergsoftware connection manager in Linux also advertises security level 19ea81896dSMika Westerberg``user`` which means PCIe tunneling is disabled by default. The 20ea81896dSMika Westerbergdocumentation below applies to both implementations with the exception that 21ea81896dSMika Westerbergthe software connection manager only supports ``user`` security level and 22ea81896dSMika Westerbergis expected to be accompanied with an IOMMU based DMA protection. 23ea81896dSMika Westerberg 24ea81896dSMika WesterbergSecurity levels and how to use them 25ea81896dSMika Westerberg----------------------------------- 26163f1511SMika WesterbergThe interface presented here is not meant for end users. Instead there 27163f1511SMika Westerbergshould be a userspace tool that handles all the low-level details, keeps 2854e36a2dSRandy Dunlapa database of the authorized devices and prompts users for new connections. 29163f1511SMika Westerberg 30163f1511SMika WesterbergMore details about the sysfs interface for Thunderbolt devices can be 31163f1511SMika Westerbergfound in ``Documentation/ABI/testing/sysfs-bus-thunderbolt``. 32163f1511SMika Westerberg 33163f1511SMika WesterbergThose users who just want to connect any device without any sort of 3454e36a2dSRandy Dunlapmanual work can add following line to 35163f1511SMika Westerberg``/etc/udev/rules.d/99-local.rules``:: 36163f1511SMika Westerberg 37163f1511SMika Westerberg ACTION=="add", SUBSYSTEM=="thunderbolt", ATTR{authorized}=="0", ATTR{authorized}="1" 38163f1511SMika Westerberg 39163f1511SMika WesterbergThis will authorize all devices automatically when they appear. However, 40163f1511SMika Westerbergkeep in mind that this bypasses the security levels and makes the system 41163f1511SMika Westerbergvulnerable to DMA attacks. 42163f1511SMika Westerberg 4354e36a2dSRandy DunlapStarting with Intel Falcon Ridge Thunderbolt controller there are 4 446fc14e1aSMika Westerbergsecurity levels available. Intel Titan Ridge added one more security level 456fc14e1aSMika Westerberg(usbonly). The reason for these is the fact that the connected devices can 466fc14e1aSMika Westerbergbe DMA masters and thus read contents of the host memory without CPU and OS 476fc14e1aSMika Westerbergknowing about it. There are ways to prevent this by setting up an IOMMU but 486fc14e1aSMika Westerbergit is not always available for various reasons. 49163f1511SMika Westerberg 50163f1511SMika WesterbergThe security levels are as follows: 51163f1511SMika Westerberg 52163f1511SMika Westerberg none 53163f1511SMika Westerberg All devices are automatically connected by the firmware. No user 54163f1511SMika Westerberg approval is needed. In BIOS settings this is typically called 55163f1511SMika Westerberg *Legacy mode*. 56163f1511SMika Westerberg 57163f1511SMika Westerberg user 58163f1511SMika Westerberg User is asked whether the device is allowed to be connected. 59163f1511SMika Westerberg Based on the device identification information available through 6054e36a2dSRandy Dunlap ``/sys/bus/thunderbolt/devices``, the user then can make the decision. 61163f1511SMika Westerberg In BIOS settings this is typically called *Unique ID*. 62163f1511SMika Westerberg 63163f1511SMika Westerberg secure 64163f1511SMika Westerberg User is asked whether the device is allowed to be connected. In 65163f1511SMika Westerberg addition to UUID the device (if it supports secure connect) is sent 66163f1511SMika Westerberg a challenge that should match the expected one based on a random key 6754e36a2dSRandy Dunlap written to the ``key`` sysfs attribute. In BIOS settings this is 68163f1511SMika Westerberg typically called *One time saved key*. 69163f1511SMika Westerberg 70163f1511SMika Westerberg dponly 71163f1511SMika Westerberg The firmware automatically creates tunnels for Display Port and 72163f1511SMika Westerberg USB. No PCIe tunneling is done. In BIOS settings this is 73163f1511SMika Westerberg typically called *Display Port Only*. 74163f1511SMika Westerberg 756fc14e1aSMika Westerberg usbonly 766fc14e1aSMika Westerberg The firmware automatically creates tunnels for the USB controller and 776fc14e1aSMika Westerberg Display Port in a dock. All PCIe links downstream of the dock are 786fc14e1aSMika Westerberg removed. 796fc14e1aSMika Westerberg 80163f1511SMika WesterbergThe current security level can be read from 81163f1511SMika Westerberg``/sys/bus/thunderbolt/devices/domainX/security`` where ``domainX`` is 82163f1511SMika Westerbergthe Thunderbolt domain the host controller manages. There is typically 83163f1511SMika Westerbergone domain per Thunderbolt host controller. 84163f1511SMika Westerberg 85163f1511SMika WesterbergIf the security level reads as ``user`` or ``secure`` the connected 86163f1511SMika Westerbergdevice must be authorized by the user before PCIe tunnels are created 87163f1511SMika Westerberg(e.g the PCIe device appears). 88163f1511SMika Westerberg 89163f1511SMika WesterbergEach Thunderbolt device plugged in will appear in sysfs under 90163f1511SMika Westerberg``/sys/bus/thunderbolt/devices``. The device directory carries 91163f1511SMika Westerberginformation that can be used to identify the particular device, 92163f1511SMika Westerbergincluding its name and UUID. 93163f1511SMika Westerberg 94163f1511SMika WesterbergAuthorizing devices when security level is ``user`` or ``secure`` 95163f1511SMika Westerberg----------------------------------------------------------------- 96163f1511SMika WesterbergWhen a device is plugged in it will appear in sysfs as follows:: 97163f1511SMika Westerberg 98163f1511SMika Westerberg /sys/bus/thunderbolt/devices/0-1/authorized - 0 99163f1511SMika Westerberg /sys/bus/thunderbolt/devices/0-1/device - 0x8004 100163f1511SMika Westerberg /sys/bus/thunderbolt/devices/0-1/device_name - Thunderbolt to FireWire Adapter 101163f1511SMika Westerberg /sys/bus/thunderbolt/devices/0-1/vendor - 0x1 102163f1511SMika Westerberg /sys/bus/thunderbolt/devices/0-1/vendor_name - Apple, Inc. 103163f1511SMika Westerberg /sys/bus/thunderbolt/devices/0-1/unique_id - e0376f00-0300-0100-ffff-ffffffffffff 104163f1511SMika Westerberg 105163f1511SMika WesterbergThe ``authorized`` attribute reads 0 which means no PCIe tunnels are 10654e36a2dSRandy Dunlapcreated yet. The user can authorize the device by simply entering:: 107163f1511SMika Westerberg 108163f1511SMika Westerberg # echo 1 > /sys/bus/thunderbolt/devices/0-1/authorized 109163f1511SMika Westerberg 110163f1511SMika WesterbergThis will create the PCIe tunnels and the device is now connected. 111163f1511SMika Westerberg 112163f1511SMika WesterbergIf the device supports secure connect, and the domain security level is 113163f1511SMika Westerbergset to ``secure``, it has an additional attribute ``key`` which can hold 11454e36a2dSRandy Dunlapa random 32-byte value used for authorization and challenging the device in 115163f1511SMika Westerbergfuture connects:: 116163f1511SMika Westerberg 117163f1511SMika Westerberg /sys/bus/thunderbolt/devices/0-3/authorized - 0 118163f1511SMika Westerberg /sys/bus/thunderbolt/devices/0-3/device - 0x305 119163f1511SMika Westerberg /sys/bus/thunderbolt/devices/0-3/device_name - AKiTiO Thunder3 PCIe Box 120163f1511SMika Westerberg /sys/bus/thunderbolt/devices/0-3/key - 121163f1511SMika Westerberg /sys/bus/thunderbolt/devices/0-3/vendor - 0x41 122163f1511SMika Westerberg /sys/bus/thunderbolt/devices/0-3/vendor_name - inXtron 123163f1511SMika Westerberg /sys/bus/thunderbolt/devices/0-3/unique_id - dc010000-0000-8508-a22d-32ca6421cb16 124163f1511SMika Westerberg 125163f1511SMika WesterbergNotice the key is empty by default. 126163f1511SMika Westerberg 12754e36a2dSRandy DunlapIf the user does not want to use secure connect they can just ``echo 1`` 128163f1511SMika Westerbergto the ``authorized`` attribute and the PCIe tunnels will be created in 12954e36a2dSRandy Dunlapthe same way as in the ``user`` security level. 130163f1511SMika Westerberg 131163f1511SMika WesterbergIf the user wants to use secure connect, the first time the device is 13254e36a2dSRandy Dunlapplugged a key needs to be created and sent to the device:: 133163f1511SMika Westerberg 134163f1511SMika Westerberg # key=$(openssl rand -hex 32) 135163f1511SMika Westerberg # echo $key > /sys/bus/thunderbolt/devices/0-3/key 136163f1511SMika Westerberg # echo 1 > /sys/bus/thunderbolt/devices/0-3/authorized 137163f1511SMika Westerberg 138163f1511SMika WesterbergNow the device is connected (PCIe tunnels are created) and in addition 139163f1511SMika Westerbergthe key is stored on the device NVM. 140163f1511SMika Westerberg 141163f1511SMika WesterbergNext time the device is plugged in the user can verify (challenge) the 142163f1511SMika Westerbergdevice using the same key:: 143163f1511SMika Westerberg 144163f1511SMika Westerberg # echo $key > /sys/bus/thunderbolt/devices/0-3/key 145163f1511SMika Westerberg # echo 2 > /sys/bus/thunderbolt/devices/0-3/authorized 146163f1511SMika Westerberg 147163f1511SMika WesterbergIf the challenge the device returns back matches the one we expect based 148163f1511SMika Westerbergon the key, the device is connected and the PCIe tunnels are created. 14954e36a2dSRandy DunlapHowever, if the challenge fails no tunnels are created and error is 150163f1511SMika Westerbergreturned to the user. 151163f1511SMika Westerberg 15254e36a2dSRandy DunlapIf the user still wants to connect the device they can either approve 15354e36a2dSRandy Dunlapthe device without a key or write a new key and write 1 to the 154163f1511SMika Westerberg``authorized`` file to get the new key stored on the device NVM. 155163f1511SMika Westerberg 156dcc3c9e3SMika WesterbergDMA protection utilizing IOMMU 157dcc3c9e3SMika Westerberg------------------------------ 158dcc3c9e3SMika WesterbergRecent systems from 2018 and forward with Thunderbolt ports may natively 159dcc3c9e3SMika Westerbergsupport IOMMU. This means that Thunderbolt security is handled by an IOMMU 160dcc3c9e3SMika Westerbergso connected devices cannot access memory regions outside of what is 161dcc3c9e3SMika Westerbergallocated for them by drivers. When Linux is running on such system it 162dcc3c9e3SMika Westerbergautomatically enables IOMMU if not enabled by the user already. These 163dcc3c9e3SMika Westerbergsystems can be identified by reading ``1`` from 164dcc3c9e3SMika Westerberg``/sys/bus/thunderbolt/devices/domainX/iommu_dma_protection`` attribute. 165dcc3c9e3SMika Westerberg 166dcc3c9e3SMika WesterbergThe driver does not do anything special in this case but because DMA 167dcc3c9e3SMika Westerbergprotection is handled by the IOMMU, security levels (if set) are 168dcc3c9e3SMika Westerbergredundant. For this reason some systems ship with security level set to 169dcc3c9e3SMika Westerberg``none``. Other systems have security level set to ``user`` in order to 170dcc3c9e3SMika Westerbergsupport downgrade to older OS, so users who want to automatically 171dcc3c9e3SMika Westerbergauthorize devices when IOMMU DMA protection is enabled can use the 172dcc3c9e3SMika Westerbergfollowing ``udev`` rule:: 173dcc3c9e3SMika Westerberg 174dcc3c9e3SMika Westerberg ACTION=="add", SUBSYSTEM=="thunderbolt", ATTRS{iommu_dma_protection}=="1", ATTR{authorized}=="0", ATTR{authorized}="1" 175dcc3c9e3SMika Westerberg 176*dacb1287SKranthi KuntalaUpgrading NVM on Thunderbolt device, host or retimer 177*dacb1287SKranthi Kuntala---------------------------------------------------- 17854e36a2dSRandy DunlapSince most of the functionality is handled in firmware running on a 179163f1511SMika Westerberghost controller or a device, it is important that the firmware can be 180163f1511SMika Westerbergupgraded to the latest where possible bugs in it have been fixed. 181163f1511SMika WesterbergTypically OEMs provide this firmware from their support site. 182163f1511SMika Westerberg 18354e36a2dSRandy DunlapThere is also a central site which has links where to download firmware 184163f1511SMika Westerbergfor some machines: 185163f1511SMika Westerberg 186163f1511SMika Westerberg `Thunderbolt Updates <https://thunderbolttechnology.net/updates>`_ 187163f1511SMika Westerberg 188*dacb1287SKranthi KuntalaBefore you upgrade firmware on a device, host or retimer, please make 189*dacb1287SKranthi Kuntalasure it is a suitable upgrade. Failing to do that may render the device 190*dacb1287SKranthi Kuntalain a state where it cannot be used properly anymore without special 191*dacb1287SKranthi Kuntalatools! 192163f1511SMika Westerberg 193163f1511SMika WesterbergHost NVM upgrade on Apple Macs is not supported. 194163f1511SMika Westerberg 195163f1511SMika WesterbergOnce the NVM image has been downloaded, you need to plug in a 196163f1511SMika WesterbergThunderbolt device so that the host controller appears. It does not 197163f1511SMika Westerbergmatter which device is connected (unless you are upgrading NVM on a 198163f1511SMika Westerbergdevice - then you need to connect that particular device). 199163f1511SMika Westerberg 20054e36a2dSRandy DunlapNote an OEM-specific method to power the controller up ("force power") may 201163f1511SMika Westerbergbe available for your system in which case there is no need to plug in a 202163f1511SMika WesterbergThunderbolt device. 203163f1511SMika Westerberg 204163f1511SMika WesterbergAfter that we can write the firmware to the non-active parts of the NVM 205163f1511SMika Westerbergof the host or device. As an example here is how Intel NUC6i7KYK (Skull 206163f1511SMika WesterbergCanyon) Thunderbolt controller NVM is upgraded:: 207163f1511SMika Westerberg 208163f1511SMika Westerberg # dd if=KYK_TBT_FW_0018.bin of=/sys/bus/thunderbolt/devices/0-0/nvm_non_active0/nvmem 209163f1511SMika Westerberg 210163f1511SMika WesterbergOnce the operation completes we can trigger NVM authentication and 211163f1511SMika Westerbergupgrade process as follows:: 212163f1511SMika Westerberg 213163f1511SMika Westerberg # echo 1 > /sys/bus/thunderbolt/devices/0-0/nvm_authenticate 214163f1511SMika Westerberg 215163f1511SMika WesterbergIf no errors are returned, the host controller shortly disappears. Once 216163f1511SMika Westerbergit comes back the driver notices it and initiates a full power cycle. 217163f1511SMika WesterbergAfter a while the host controller appears again and this time it should 218163f1511SMika Westerbergbe fully functional. 219163f1511SMika Westerberg 22054e36a2dSRandy DunlapWe can verify that the new NVM firmware is active by running the following 221163f1511SMika Westerbergcommands:: 222163f1511SMika Westerberg 223163f1511SMika Westerberg # cat /sys/bus/thunderbolt/devices/0-0/nvm_authenticate 224163f1511SMika Westerberg 0x0 225163f1511SMika Westerberg # cat /sys/bus/thunderbolt/devices/0-0/nvm_version 226163f1511SMika Westerberg 18.0 227163f1511SMika Westerberg 22854e36a2dSRandy DunlapIf ``nvm_authenticate`` contains anything other than 0x0 it is the error 229163f1511SMika Westerbergcode from the last authentication cycle, which means the authentication 230163f1511SMika Westerbergof the NVM image failed. 231163f1511SMika Westerberg 232163f1511SMika WesterbergNote names of the NVMem devices ``nvm_activeN`` and ``nvm_non_activeN`` 23354e36a2dSRandy Dunlapdepend on the order they are registered in the NVMem subsystem. N in 234163f1511SMika Westerbergthe name is the identifier added by the NVMem subsystem. 235163f1511SMika Westerberg 236163f1511SMika WesterbergUpgrading NVM when host controller is in safe mode 237163f1511SMika Westerberg-------------------------------------------------- 238163f1511SMika WesterbergIf the existing NVM is not properly authenticated (or is missing) the 23954e36a2dSRandy Dunlaphost controller goes into safe mode which means that the only available 24054e36a2dSRandy Dunlapfunctionality is flashing a new NVM image. When in this mode, reading 241163f1511SMika Westerberg``nvm_version`` fails with ``ENODATA`` and the device identification 242163f1511SMika Westerberginformation is missing. 243163f1511SMika Westerberg 244163f1511SMika WesterbergTo recover from this mode, one needs to flash a valid NVM image to the 24554e36a2dSRandy Dunlaphost controller in the same way it is done in the previous chapter. 246e69b6c02SAmir Levy 247e69b6c02SAmir LevyNetworking over Thunderbolt cable 248e69b6c02SAmir Levy--------------------------------- 24954e36a2dSRandy DunlapThunderbolt technology allows software communication between two hosts 250e69b6c02SAmir Levyconnected by a Thunderbolt cable. 251e69b6c02SAmir Levy 25254e36a2dSRandy DunlapIt is possible to tunnel any kind of traffic over a Thunderbolt link but 253e69b6c02SAmir Levycurrently we only support Apple ThunderboltIP protocol. 254e69b6c02SAmir Levy 25554e36a2dSRandy DunlapIf the other host is running Windows or macOS, the only thing you need to 25654e36a2dSRandy Dunlapdo is to connect a Thunderbolt cable between the two hosts; the 25754e36a2dSRandy Dunlap``thunderbolt-net`` driver is loaded automatically. If the other host is 25854e36a2dSRandy Dunlapalso Linux you should load ``thunderbolt-net`` manually on one host (it 25954e36a2dSRandy Dunlapdoes not matter which one):: 260e69b6c02SAmir Levy 261e69b6c02SAmir Levy # modprobe thunderbolt-net 262e69b6c02SAmir Levy 263e69b6c02SAmir LevyThis triggers module load on the other host automatically. If the driver 264e69b6c02SAmir Levyis built-in to the kernel image, there is no need to do anything. 265e69b6c02SAmir Levy 266e69b6c02SAmir LevyThe driver will create one virtual ethernet interface per Thunderbolt 267e69b6c02SAmir Levyport which are named like ``thunderbolt0`` and so on. From this point 268e69b6c02SAmir Levyyou can either use standard userspace tools like ``ifconfig`` to 26954e36a2dSRandy Dunlapconfigure the interface or let your GUI handle it automatically. 27007c455eeSLinus Torvalds 271ce6a9002SMario LimoncielloForcing power 272ce6a9002SMario Limonciello------------- 273ce6a9002SMario LimoncielloMany OEMs include a method that can be used to force the power of a 27454e36a2dSRandy DunlapThunderbolt controller to an "On" state even if nothing is connected. 275ce6a9002SMario LimoncielloIf supported by your machine this will be exposed by the WMI bus with 276ce6a9002SMario Limoncielloa sysfs attribute called "force_power". 277ce6a9002SMario Limonciello 278ce6a9002SMario LimoncielloFor example the intel-wmi-thunderbolt driver exposes this attribute in: 279cce1fea5SAndy Shevchenko /sys/bus/wmi/devices/86CCFD48-205E-4A77-9C48-2021CBEDE341/force_power 280ce6a9002SMario Limonciello 281ce6a9002SMario Limonciello To force the power to on, write 1 to this attribute file. 282ce6a9002SMario Limonciello To disable force power, write 0 to this attribute file. 283ce6a9002SMario Limonciello 284ce6a9002SMario LimoncielloNote: it's currently not possible to query the force power state of a platform. 285