thunderbolt.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ======================
  3. USB4 and Thunderbolt
  4. ======================
  5. USB4 is the public specification based on Thunderbolt 3 protocol with
  6. some differences at the register level among other things. Connection
  7. manager is an entity running on the host router (host controller)
  8. responsible for enumerating routers and establishing tunnels. A
  9. connection manager can be implemented either in firmware or software.
  10. Typically PCs come with a firmware connection manager for Thunderbolt 3
  11. and early USB4 capable systems. Apple systems on the other hand use
  12. software connection manager and the later USB4 compliant devices follow
  13. the suit.
  14. The Linux Thunderbolt driver supports both and can detect at runtime which
  15. connection manager implementation is to be used. To be on the safe side the
  16. software connection manager in Linux also advertises security level
  17. ``user`` which means PCIe tunneling is disabled by default. The
  18. documentation below applies to both implementations with the exception that
  19. the software connection manager only supports ``user`` security level and
  20. is expected to be accompanied with an IOMMU based DMA protection.
  21. Security levels and how to use them
  22. -----------------------------------
  23. The interface presented here is not meant for end users. Instead there
  24. should be a userspace tool that handles all the low-level details, keeps
  25. a database of the authorized devices and prompts users for new connections.
  26. More details about the sysfs interface for Thunderbolt devices can be
  27. found in ``Documentation/ABI/testing/sysfs-bus-thunderbolt``.
  28. Those users who just want to connect any device without any sort of
  29. manual work can add following line to
  30. ``/etc/udev/rules.d/99-local.rules``::
  31. ACTION=="add", SUBSYSTEM=="thunderbolt", ATTR{authorized}=="0", ATTR{authorized}="1"
  32. This will authorize all devices automatically when they appear. However,
  33. keep in mind that this bypasses the security levels and makes the system
  34. vulnerable to DMA attacks.
  35. Starting with Intel Falcon Ridge Thunderbolt controller there are 4
  36. security levels available. Intel Titan Ridge added one more security level
  37. (usbonly). The reason for these is the fact that the connected devices can
  38. be DMA masters and thus read contents of the host memory without CPU and OS
  39. knowing about it. There are ways to prevent this by setting up an IOMMU but
  40. it is not always available for various reasons.
  41. Some USB4 systems have a BIOS setting to disable PCIe tunneling. This is
  42. treated as another security level (nopcie).
  43. The security levels are as follows:
  44. none
  45. All devices are automatically connected by the firmware. No user
  46. approval is needed. In BIOS settings this is typically called
  47. *Legacy mode*.
  48. user
  49. User is asked whether the device is allowed to be connected.
  50. Based on the device identification information available through
  51. ``/sys/bus/thunderbolt/devices``, the user then can make the decision.
  52. In BIOS settings this is typically called *Unique ID*.
  53. secure
  54. User is asked whether the device is allowed to be connected. In
  55. addition to UUID the device (if it supports secure connect) is sent
  56. a challenge that should match the expected one based on a random key
  57. written to the ``key`` sysfs attribute. In BIOS settings this is
  58. typically called *One time saved key*.
  59. dponly
  60. The firmware automatically creates tunnels for Display Port and
  61. USB. No PCIe tunneling is done. In BIOS settings this is
  62. typically called *Display Port Only*.
  63. usbonly
  64. The firmware automatically creates tunnels for the USB controller and
  65. Display Port in a dock. All PCIe links downstream of the dock are
  66. removed.
  67. nopcie
  68. PCIe tunneling is disabled/forbidden from the BIOS. Available in some
  69. USB4 systems.
  70. The current security level can be read from
  71. ``/sys/bus/thunderbolt/devices/domainX/security`` where ``domainX`` is
  72. the Thunderbolt domain the host controller manages. There is typically
  73. one domain per Thunderbolt host controller.
  74. If the security level reads as ``user`` or ``secure`` the connected
  75. device must be authorized by the user before PCIe tunnels are created
  76. (e.g the PCIe device appears).
  77. Each Thunderbolt device plugged in will appear in sysfs under
  78. ``/sys/bus/thunderbolt/devices``. The device directory carries
  79. information that can be used to identify the particular device,
  80. including its name and UUID.
  81. Authorizing devices when security level is ``user`` or ``secure``
  82. -----------------------------------------------------------------
  83. When a device is plugged in it will appear in sysfs as follows::
  84. /sys/bus/thunderbolt/devices/0-1/authorized - 0
  85. /sys/bus/thunderbolt/devices/0-1/device - 0x8004
  86. /sys/bus/thunderbolt/devices/0-1/device_name - Thunderbolt to FireWire Adapter
  87. /sys/bus/thunderbolt/devices/0-1/vendor - 0x1
  88. /sys/bus/thunderbolt/devices/0-1/vendor_name - Apple, Inc.
  89. /sys/bus/thunderbolt/devices/0-1/unique_id - e0376f00-0300-0100-ffff-ffffffffffff
  90. The ``authorized`` attribute reads 0 which means no PCIe tunnels are
  91. created yet. The user can authorize the device by simply entering::
  92. # echo 1 > /sys/bus/thunderbolt/devices/0-1/authorized
  93. This will create the PCIe tunnels and the device is now connected.
  94. If the device supports secure connect, and the domain security level is
  95. set to ``secure``, it has an additional attribute ``key`` which can hold
  96. a random 32-byte value used for authorization and challenging the device in
  97. future connects::
  98. /sys/bus/thunderbolt/devices/0-3/authorized - 0
  99. /sys/bus/thunderbolt/devices/0-3/device - 0x305
  100. /sys/bus/thunderbolt/devices/0-3/device_name - AKiTiO Thunder3 PCIe Box
  101. /sys/bus/thunderbolt/devices/0-3/key -
  102. /sys/bus/thunderbolt/devices/0-3/vendor - 0x41
  103. /sys/bus/thunderbolt/devices/0-3/vendor_name - inXtron
  104. /sys/bus/thunderbolt/devices/0-3/unique_id - dc010000-0000-8508-a22d-32ca6421cb16
  105. Notice the key is empty by default.
  106. If the user does not want to use secure connect they can just ``echo 1``
  107. to the ``authorized`` attribute and the PCIe tunnels will be created in
  108. the same way as in the ``user`` security level.
  109. If the user wants to use secure connect, the first time the device is
  110. plugged a key needs to be created and sent to the device::
  111. # key=$(openssl rand -hex 32)
  112. # echo $key > /sys/bus/thunderbolt/devices/0-3/key
  113. # echo 1 > /sys/bus/thunderbolt/devices/0-3/authorized
  114. Now the device is connected (PCIe tunnels are created) and in addition
  115. the key is stored on the device NVM.
  116. Next time the device is plugged in the user can verify (challenge) the
  117. device using the same key::
  118. # echo $key > /sys/bus/thunderbolt/devices/0-3/key
  119. # echo 2 > /sys/bus/thunderbolt/devices/0-3/authorized
  120. If the challenge the device returns back matches the one we expect based
  121. on the key, the device is connected and the PCIe tunnels are created.
  122. However, if the challenge fails no tunnels are created and error is
  123. returned to the user.
  124. If the user still wants to connect the device they can either approve
  125. the device without a key or write a new key and write 1 to the
  126. ``authorized`` file to get the new key stored on the device NVM.
  127. De-authorizing devices
  128. ----------------------
  129. It is possible to de-authorize devices by writing ``0`` to their
  130. ``authorized`` attribute. This requires support from the connection
  131. manager implementation and can be checked by reading domain
  132. ``deauthorization`` attribute. If it reads ``1`` then the feature is
  133. supported.
  134. When a device is de-authorized the PCIe tunnel from the parent device
  135. PCIe downstream (or root) port to the device PCIe upstream port is torn
  136. down. This is essentially the same thing as PCIe hot-remove and the PCIe
  137. toplogy in question will not be accessible anymore until the device is
  138. authorized again. If there is storage such as NVMe or similar involved,
  139. there is a risk for data loss if the filesystem on that storage is not
  140. properly shut down. You have been warned!
  141. DMA protection utilizing IOMMU
  142. ------------------------------
  143. Recent systems from 2018 and forward with Thunderbolt ports may natively
  144. support IOMMU. This means that Thunderbolt security is handled by an IOMMU
  145. so connected devices cannot access memory regions outside of what is
  146. allocated for them by drivers. When Linux is running on such system it
  147. automatically enables IOMMU if not enabled by the user already. These
  148. systems can be identified by reading ``1`` from
  149. ``/sys/bus/thunderbolt/devices/domainX/iommu_dma_protection`` attribute.
  150. The driver does not do anything special in this case but because DMA
  151. protection is handled by the IOMMU, security levels (if set) are
  152. redundant. For this reason some systems ship with security level set to
  153. ``none``. Other systems have security level set to ``user`` in order to
  154. support downgrade to older OS, so users who want to automatically
  155. authorize devices when IOMMU DMA protection is enabled can use the
  156. following ``udev`` rule::
  157. ACTION=="add", SUBSYSTEM=="thunderbolt", ATTRS{iommu_dma_protection}=="1", ATTR{authorized}=="0", ATTR{authorized}="1"
  158. Upgrading NVM on Thunderbolt device, host or retimer
  159. ----------------------------------------------------
  160. Since most of the functionality is handled in firmware running on a
  161. host controller or a device, it is important that the firmware can be
  162. upgraded to the latest where possible bugs in it have been fixed.
  163. Typically OEMs provide this firmware from their support site.
  164. There is also a central site which has links where to download firmware
  165. for some machines:
  166. `Thunderbolt Updates <https://thunderbolttechnology.net/updates>`_
  167. Before you upgrade firmware on a device, host or retimer, please make
  168. sure it is a suitable upgrade. Failing to do that may render the device
  169. in a state where it cannot be used properly anymore without special
  170. tools!
  171. Host NVM upgrade on Apple Macs is not supported.
  172. Once the NVM image has been downloaded, you need to plug in a
  173. Thunderbolt device so that the host controller appears. It does not
  174. matter which device is connected (unless you are upgrading NVM on a
  175. device - then you need to connect that particular device).
  176. Note an OEM-specific method to power the controller up ("force power") may
  177. be available for your system in which case there is no need to plug in a
  178. Thunderbolt device.
  179. After that we can write the firmware to the non-active parts of the NVM
  180. of the host or device. As an example here is how Intel NUC6i7KYK (Skull
  181. Canyon) Thunderbolt controller NVM is upgraded::
  182. # dd if=KYK_TBT_FW_0018.bin of=/sys/bus/thunderbolt/devices/0-0/nvm_non_active0/nvmem
  183. Once the operation completes we can trigger NVM authentication and
  184. upgrade process as follows::
  185. # echo 1 > /sys/bus/thunderbolt/devices/0-0/nvm_authenticate
  186. If no errors are returned, the host controller shortly disappears. Once
  187. it comes back the driver notices it and initiates a full power cycle.
  188. After a while the host controller appears again and this time it should
  189. be fully functional.
  190. We can verify that the new NVM firmware is active by running the following
  191. commands::
  192. # cat /sys/bus/thunderbolt/devices/0-0/nvm_authenticate
  193. 0x0
  194. # cat /sys/bus/thunderbolt/devices/0-0/nvm_version
  195. 18.0
  196. If ``nvm_authenticate`` contains anything other than 0x0 it is the error
  197. code from the last authentication cycle, which means the authentication
  198. of the NVM image failed.
  199. Note names of the NVMem devices ``nvm_activeN`` and ``nvm_non_activeN``
  200. depend on the order they are registered in the NVMem subsystem. N in
  201. the name is the identifier added by the NVMem subsystem.
  202. Upgrading on-board retimer NVM when there is no cable connected
  203. ---------------------------------------------------------------
  204. If the platform supports, it may be possible to upgrade the retimer NVM
  205. firmware even when there is nothing connected to the USB4
  206. ports. When this is the case the ``usb4_portX`` devices have two special
  207. attributes: ``offline`` and ``rescan``. The way to upgrade the firmware
  208. is to first put the USB4 port into offline mode::
  209. # echo 1 > /sys/bus/thunderbolt/devices/0-0/usb4_port1/offline
  210. This step makes sure the port does not respond to any hotplug events,
  211. and also ensures the retimers are powered on. The next step is to scan
  212. for the retimers::
  213. # echo 1 > /sys/bus/thunderbolt/devices/0-0/usb4_port1/rescan
  214. This enumerates and adds the on-board retimers. Now retimer NVM can be
  215. upgraded in the same way than with cable connected (see previous
  216. section). However, the retimer is not disconnected as we are offline
  217. mode) so after writing ``1`` to ``nvm_authenticate`` one should wait for
  218. 5 or more seconds before running rescan again::
  219. # echo 1 > /sys/bus/thunderbolt/devices/0-0/usb4_port1/rescan
  220. This point if everything went fine, the port can be put back to
  221. functional state again::
  222. # echo 0 > /sys/bus/thunderbolt/devices/0-0/usb4_port1/offline
  223. Upgrading NVM when host controller is in safe mode
  224. --------------------------------------------------
  225. If the existing NVM is not properly authenticated (or is missing) the
  226. host controller goes into safe mode which means that the only available
  227. functionality is flashing a new NVM image. When in this mode, reading
  228. ``nvm_version`` fails with ``ENODATA`` and the device identification
  229. information is missing.
  230. To recover from this mode, one needs to flash a valid NVM image to the
  231. host controller in the same way it is done in the previous chapter.
  232. Networking over Thunderbolt cable
  233. ---------------------------------
  234. Thunderbolt technology allows software communication between two hosts
  235. connected by a Thunderbolt cable.
  236. It is possible to tunnel any kind of traffic over a Thunderbolt link but
  237. currently we only support Apple ThunderboltIP protocol.
  238. If the other host is running Windows or macOS, the only thing you need to
  239. do is to connect a Thunderbolt cable between the two hosts; the
  240. ``thunderbolt-net`` driver is loaded automatically. If the other host is
  241. also Linux you should load ``thunderbolt-net`` manually on one host (it
  242. does not matter which one)::
  243. # modprobe thunderbolt-net
  244. This triggers module load on the other host automatically. If the driver
  245. is built-in to the kernel image, there is no need to do anything.
  246. The driver will create one virtual ethernet interface per Thunderbolt
  247. port which are named like ``thunderbolt0`` and so on. From this point
  248. you can either use standard userspace tools like ``ifconfig`` to
  249. configure the interface or let your GUI handle it automatically.
  250. Forcing power
  251. -------------
  252. Many OEMs include a method that can be used to force the power of a
  253. Thunderbolt controller to an "On" state even if nothing is connected.
  254. If supported by your machine this will be exposed by the WMI bus with
  255. a sysfs attribute called "force_power".
  256. For example the intel-wmi-thunderbolt driver exposes this attribute in:
  257. /sys/bus/wmi/devices/86CCFD48-205E-4A77-9C48-2021CBEDE341/force_power
  258. To force the power to on, write 1 to this attribute file.
  259. To disable force power, write 0 to this attribute file.
  260. Note: it's currently not possible to query the force power state of a platform.