fwctl.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===============
  3. fwctl subsystem
  4. ===============
  5. :Author: Jason Gunthorpe
  6. Overview
  7. ========
  8. Modern devices contain extensive amounts of FW, and in many cases, are largely
  9. software-defined pieces of hardware. The evolution of this approach is largely a
  10. reaction to Moore's Law where a chip tape out is now highly expensive, and the
  11. chip design is extremely large. Replacing fixed HW logic with a flexible and
  12. tightly coupled FW/HW combination is an effective risk mitigation against chip
  13. respin. Problems in the HW design can be counteracted in device FW. This is
  14. especially true for devices which present a stable and backwards compatible
  15. interface to the operating system driver (such as NVMe).
  16. The FW layer in devices has grown to incredible size and devices frequently
  17. integrate clusters of fast processors to run it. For example, mlx5 devices have
  18. over 30MB of FW code, and big configurations operate with over 1GB of FW managed
  19. runtime state.
  20. The availability of such a flexible layer has created quite a variety in the
  21. industry where single pieces of silicon are now configurable software-defined
  22. devices and can operate in substantially different ways depending on the need.
  23. Further, we often see cases where specific sites wish to operate devices in ways
  24. that are highly specialized and require applications that have been tailored to
  25. their unique configuration.
  26. Further, devices have become multi-functional and integrated to the point they
  27. no longer fit neatly into the kernel's division of subsystems. Modern
  28. multi-functional devices have drivers, such as bnxt/ice/mlx5/pds, that span many
  29. subsystems while sharing the underlying hardware using the auxiliary device
  30. system.
  31. All together this creates a challenge for the operating system, where devices
  32. have an expansive FW environment that needs robust device-specific debugging
  33. support, and FW-driven functionality that is not well suited to “generic”
  34. interfaces. fwctl seeks to allow access to the full device functionality from
  35. user space in the areas of debuggability, management, and first-boot/nth-boot
  36. provisioning.
  37. fwctl is aimed at the common device design pattern where the OS and FW
  38. communicate via an RPC message layer constructed with a queue or mailbox scheme.
  39. In this case the driver will typically have some layer to deliver RPC messages
  40. and collect RPC responses from device FW. The in-kernel subsystem drivers that
  41. operate the device for its primary purposes will use these RPCs to build their
  42. drivers, but devices also usually have a set of ancillary RPCs that don't really
  43. fit into any specific subsystem. For example, a HW RAID controller is primarily
  44. operated by the block layer but also comes with a set of RPCs to administer the
  45. construction of drives within the HW RAID.
  46. In the past when devices were more single function, individual subsystems would
  47. grow different approaches to solving some of these common problems. For instance,
  48. monitoring device health, manipulating its FLASH, debugging the FW,
  49. provisioning, all have various unique interfaces across the kernel.
  50. fwctl's purpose is to define a common set of limited rules, described below,
  51. that allow user space to securely construct and execute RPCs inside device FW.
  52. The rules serve as an agreement between the operating system and FW on how to
  53. correctly design the RPC interface. As a uAPI the subsystem provides a thin
  54. layer of discovery and a generic uAPI to deliver the RPCs and collect the
  55. response. It supports a system of user space libraries and tools which will
  56. use this interface to control the device using the device native protocols.
  57. Scope of Action
  58. ---------------
  59. fwctl drivers are strictly restricted to being a way to operate the device FW.
  60. It is not an avenue to access random kernel internals, or other operating system
  61. SW states.
  62. fwctl instances must operate on a well-defined device function, and the device
  63. should have a well-defined security model for what scope within the physical
  64. device the function is permitted to access. For instance, the most complex PCIe
  65. device today may broadly have several function-level scopes:
  66. 1. A privileged function with full access to the on-device global state and
  67. configuration
  68. 2. Multiple hypervisor functions with control over itself and child functions
  69. used with VMs
  70. 3. Multiple VM functions tightly scoped within the VM
  71. The device may create a logical parent/child relationship between these scopes.
  72. For instance, a child VM's FW may be within the scope of the hypervisor FW. It is
  73. quite common in the VFIO world that the hypervisor environment has a complex
  74. provisioning/profiling/configuration responsibility for the function VFIO
  75. assigns to the VM.
  76. Further, within the function, devices often have RPC commands that fall within
  77. some general scopes of action (see enum fwctl_rpc_scope):
  78. 1. Access to function & child configuration, FLASH, etc. that becomes live at a
  79. function reset. Access to function & child runtime configuration that is
  80. transparent or non-disruptive to any driver or VM.
  81. 2. Read-only access to function debug information that may report on FW objects
  82. in the function & child, including FW objects owned by other kernel
  83. subsystems.
  84. 3. Write access to function & child debug information strictly compatible with
  85. the principles of kernel lockdown and kernel integrity protection. Triggers
  86. a kernel taint.
  87. 4. Full debug device access. Triggers a kernel taint, requires CAP_SYS_RAWIO.
  88. User space will provide a scope label on each RPC and the kernel must enforce the
  89. above CAPs and taints based on that scope. A combination of kernel and FW can
  90. enforce that RPCs are placed in the correct scope by user space.
  91. Disallowed behavior
  92. -------------------
  93. There are many things this interface must not allow user space to do (without a
  94. taint or CAP), broadly derived from the principles of kernel lockdown. Some
  95. examples:
  96. 1. DMA to/from arbitrary memory, hang the system, compromise FW integrity with
  97. untrusted code, or otherwise compromise device or system security and
  98. integrity.
  99. 2. Provide an abnormal “back door” to kernel drivers. No manipulation of kernel
  100. objects owned by kernel drivers.
  101. 3. Directly configure or otherwise control kernel drivers. A subsystem kernel
  102. driver can react to the device configuration at function reset/driver load
  103. time, but otherwise must not be coupled to fwctl.
  104. 4. Operate the HW in a way that overlaps with the core purpose of another
  105. primary kernel subsystem, such as read/write to LBAs, send/receive of
  106. network packets, or operate an accelerator's data plane.
  107. fwctl is not a replacement for device direct access subsystems like uacce or
  108. VFIO.
  109. Operations exposed through fwctl's non-tainting interfaces should be fully
  110. sharable with other users of the device. For instance, exposing a RPC through
  111. fwctl should never prevent a kernel subsystem from also concurrently using that
  112. same RPC or hardware unit down the road. In such cases fwctl will be less
  113. important than proper kernel subsystems that eventually emerge. Mistakes in this
  114. area resulting in clashes will be resolved in favour of a kernel implementation.
  115. fwctl User API
  116. ==============
  117. .. kernel-doc:: include/uapi/fwctl/fwctl.h
  118. .. kernel-doc:: include/uapi/fwctl/mlx5.h
  119. .. kernel-doc:: include/uapi/fwctl/pds.h
  120. sysfs Class
  121. -----------
  122. fwctl has a sysfs class (/sys/class/fwctl/fwctlNN/) and character devices
  123. (/dev/fwctl/fwctlNN) with a simple numbered scheme. The character device
  124. operates the iotcl uAPI described above.
  125. fwctl devices can be related to driver components in other subsystems through
  126. sysfs::
  127. $ ls /sys/class/fwctl/fwctl0/device/infiniband/
  128. ibp0s10f0
  129. $ ls /sys/class/infiniband/ibp0s10f0/device/fwctl/
  130. fwctl0/
  131. $ ls /sys/devices/pci0000:00/0000:00:0a.0/fwctl/fwctl0
  132. dev device power subsystem uevent
  133. User space Community
  134. --------------------
  135. Drawing inspiration from nvme-cli, participating in the kernel side must come
  136. with a user space in a common TBD git tree, at a minimum to usefully operate the
  137. kernel driver. Providing such an implementation is a pre-condition to merging a
  138. kernel driver.
  139. The goal is to build user space community around some of the shared problems
  140. we all have, and ideally develop some common user space programs with some
  141. starting themes of:
  142. - Device in-field debugging
  143. - HW provisioning
  144. - VFIO child device profiling before VM boot
  145. - Confidential Compute topics (attestation, secure provisioning)
  146. that stretch across all subsystems in the kernel. fwupd is a great example of
  147. how an excellent user space experience can emerge out of kernel-side diversity.
  148. fwctl Kernel API
  149. ================
  150. .. kernel-doc:: drivers/fwctl/main.c
  151. :export:
  152. .. kernel-doc:: include/linux/fwctl.h
  153. fwctl Driver design
  154. -------------------
  155. In many cases a fwctl driver is going to be part of a larger cross-subsystem
  156. device possibly using the auxiliary_device mechanism. In that case several
  157. subsystems are going to be sharing the same device and FW interface layer so the
  158. device design must already provide for isolation and cooperation between kernel
  159. subsystems. fwctl should fit into that same model.
  160. Part of the driver should include a description of how its scope restrictions
  161. and security model work. The driver and FW together must ensure that RPCs
  162. provided by user space are mapped to the appropriate scope. If the validation is
  163. done in the driver then the validation can read a 'command effects' report from
  164. the device, or hardwire the enforcement. If the validation is done in the FW,
  165. then the driver should pass the fwctl_rpc_scope to the FW along with the command.
  166. The driver and FW must cooperate to ensure that either fwctl cannot allocate
  167. any FW resources, or any resources it does allocate are freed on FD closure. A
  168. driver primarily constructed around FW RPCs may find that its core PCI function
  169. and RPC layer belongs under fwctl with auxiliary devices connecting to other
  170. subsystems.
  171. Each device type must be mindful of Linux's philosophy for stable ABI. The FW
  172. RPC interface does not have to meet a strictly stable ABI, but it does need to
  173. meet an expectation that user space tools that are deployed and in significant
  174. use don't needlessly break. FW upgrade and kernel upgrade should keep widely
  175. deployed tooling working.
  176. Development and debugging focused RPCs under more permissive scopes can have
  177. less stability if the tools using them are only run under exceptional
  178. circumstances and not for every day use of the device. Debugging tools may even
  179. require exact version matching as they may require something similar to DWARF
  180. debug information from the FW binary.
  181. Security Response
  182. =================
  183. The kernel remains the gatekeeper for this interface. If violations of the
  184. scopes, security or isolation principles are found, we have options to let
  185. devices fix them with a FW update, push a kernel patch to parse and block RPC
  186. commands or push a kernel patch to block entire firmware versions/devices.
  187. While the kernel can always directly parse and restrict RPCs, it is expected
  188. that the existing kernel pattern of allowing drivers to delegate validation to
  189. FW to be a useful design.
  190. Existing Similar Examples
  191. =========================
  192. The approach described in this document is not a new idea. Direct, or near
  193. direct device access has been offered by the kernel in different areas for
  194. decades. With more devices wanting to follow this design pattern it is becoming
  195. clear that it is not entirely well understood and, more importantly, the
  196. security considerations are not well defined or agreed upon.
  197. Some examples:
  198. - HW RAID controllers. This includes RPCs to do things like compose drives into
  199. a RAID volume, configure RAID parameters, monitor the HW and more.
  200. - Baseboard managers. RPCs for configuring settings in the device and more.
  201. - NVMe vendor command capsules. nvme-cli provides access to some monitoring
  202. functions that different products have defined, but more exist.
  203. - CXL also has a NVMe-like vendor command system.
  204. - DRM allows user space drivers to send commands to the device via kernel
  205. mediation.
  206. - RDMA allows user space drivers to directly push commands to the device
  207. without kernel involvement.
  208. - Various “raw” APIs, raw HID (SDL2), raw USB, NVMe Generic Interface, etc.
  209. The first 4 are examples of areas that fwctl intends to cover. The latter three
  210. are examples of disallowed behavior as they fully overlap with the primary purpose
  211. of a kernel subsystem.
  212. Some key lessons learned from these past efforts are the importance of having a
  213. common user space project to use as a pre-condition for obtaining a kernel
  214. driver. Developing good community around useful software in user space is key to
  215. getting companies to fund participation to enable their products.