ssh.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. .. SPDX-License-Identifier: GPL-2.0+
  2. .. |u8| replace:: :c:type:`u8 <u8>`
  3. .. |u16| replace:: :c:type:`u16 <u16>`
  4. .. |TYPE| replace:: ``TYPE``
  5. .. |LEN| replace:: ``LEN``
  6. .. |SEQ| replace:: ``SEQ``
  7. .. |SYN| replace:: ``SYN``
  8. .. |NAK| replace:: ``NAK``
  9. .. |ACK| replace:: ``ACK``
  10. .. |DATA| replace:: ``DATA``
  11. .. |DATA_SEQ| replace:: ``DATA_SEQ``
  12. .. |DATA_NSQ| replace:: ``DATA_NSQ``
  13. .. |TC| replace:: ``TC``
  14. .. |TID| replace:: ``TID``
  15. .. |SID| replace:: ``SID``
  16. .. |IID| replace:: ``IID``
  17. .. |RQID| replace:: ``RQID``
  18. .. |CID| replace:: ``CID``
  19. ===========================
  20. Surface Serial Hub Protocol
  21. ===========================
  22. The Surface Serial Hub (SSH) is the central communication interface for the
  23. embedded Surface Aggregator Module controller (SAM or EC), found on newer
  24. Surface generations. We will refer to this protocol and interface as
  25. SAM-over-SSH, as opposed to SAM-over-HID for the older generations.
  26. On Surface devices with SAM-over-SSH, SAM is connected to the host via UART
  27. and defined in ACPI as device with ID ``MSHW0084``. On these devices,
  28. significant functionality is provided via SAM, including access to battery
  29. and power information and events, thermal read-outs and events, and many
  30. more. For Surface Laptops, keyboard input is handled via HID directed
  31. through SAM, on the Surface Laptop 3 and Surface Book 3 this also includes
  32. touchpad input.
  33. Note that the standard disclaimer for this subsystem also applies to this
  34. document: All of this has been reverse-engineered and may thus be erroneous
  35. and/or incomplete.
  36. All CRCs used in the following are two-byte ``crc_itu_t(0xffff, ...)``.
  37. All multi-byte values are little-endian, there is no implicit padding between
  38. values.
  39. SSH Packet Protocol: Definitions
  40. ================================
  41. The fundamental communication unit of the SSH protocol is a frame
  42. (:c:type:`struct ssh_frame <ssh_frame>`). A frame consists of the following
  43. fields, packed together and in order:
  44. .. flat-table:: SSH Frame
  45. :widths: 1 1 4
  46. :header-rows: 1
  47. * - Field
  48. - Type
  49. - Description
  50. * - |TYPE|
  51. - |u8|
  52. - Type identifier of the frame.
  53. * - |LEN|
  54. - |u16|
  55. - Length of the payload associated with the frame.
  56. * - |SEQ|
  57. - |u8|
  58. - Sequence ID (see explanation below).
  59. Each frame structure is followed by a CRC over this structure. The CRC over
  60. the frame structure (|TYPE|, |LEN|, and |SEQ| fields) is placed directly
  61. after the frame structure and before the payload. The payload is followed by
  62. its own CRC (over all payload bytes). If the payload is not present (i.e.
  63. the frame has ``LEN=0``), the CRC of the payload is still present and will
  64. evaluate to ``0xffff``. The |LEN| field does not include any of the CRCs, it
  65. equals the number of bytes between the CRC of the frame and the CRC of the
  66. payload.
  67. Additionally, the following fixed two-byte sequences are used:
  68. .. flat-table:: SSH Byte Sequences
  69. :widths: 1 1 4
  70. :header-rows: 1
  71. * - Name
  72. - Value
  73. - Description
  74. * - |SYN|
  75. - ``[0xAA, 0x55]``
  76. - Synchronization bytes.
  77. A message consists of |SYN|, followed by the frame (|TYPE|, |LEN|, |SEQ| and
  78. CRC) and, if specified in the frame (i.e. ``LEN > 0``), payload bytes,
  79. followed finally, regardless if the payload is present, the payload CRC. The
  80. messages corresponding to an exchange are, in part, identified by having the
  81. same sequence ID (|SEQ|), stored inside the frame (more on this in the next
  82. section). The sequence ID is a wrapping counter.
  83. A frame can have the following types
  84. (:c:type:`enum ssh_frame_type <ssh_frame_type>`):
  85. .. flat-table:: SSH Frame Types
  86. :widths: 1 1 4
  87. :header-rows: 1
  88. * - Name
  89. - Value
  90. - Short Description
  91. * - |NAK|
  92. - ``0x04``
  93. - Sent on error in previously received message.
  94. * - |ACK|
  95. - ``0x40``
  96. - Sent to acknowledge receival of |DATA| frame.
  97. * - |DATA_SEQ|
  98. - ``0x80``
  99. - Sent to transfer data. Sequenced.
  100. * - |DATA_NSQ|
  101. - ``0x00``
  102. - Same as |DATA_SEQ|, but does not need to be ACKed.
  103. Both |NAK|- and |ACK|-type frames are used to control flow of messages and
  104. thus do not carry a payload. |DATA_SEQ|- and |DATA_NSQ|-type frames on the
  105. other hand must carry a payload. The flow sequence and interaction of
  106. different frame types will be described in more depth in the next section.
  107. SSH Packet Protocol: Flow Sequence
  108. ==================================
  109. Each exchange begins with |SYN|, followed by a |DATA_SEQ|- or
  110. |DATA_NSQ|-type frame, followed by its CRC, payload, and payload CRC. In
  111. case of a |DATA_NSQ|-type frame, the exchange is then finished. In case of a
  112. |DATA_SEQ|-type frame, the receiving party has to acknowledge receival of
  113. the frame by responding with a message containing an |ACK|-type frame with
  114. the same sequence ID of the |DATA| frame. In other words, the sequence ID of
  115. the |ACK| frame specifies the |DATA| frame to be acknowledged. In case of an
  116. error, e.g. an invalid CRC, the receiving party responds with a message
  117. containing an |NAK|-type frame. As the sequence ID of the previous data
  118. frame, for which an error is indicated via the |NAK| frame, cannot be relied
  119. upon, the sequence ID of the |NAK| frame should not be used and is set to
  120. zero. After receival of an |NAK| frame, the sending party should re-send all
  121. outstanding (non-ACKed) messages.
  122. Sequence IDs are not synchronized between the two parties, meaning that they
  123. are managed independently for each party. Identifying the messages
  124. corresponding to a single exchange thus relies on the sequence ID as well as
  125. the type of the message, and the context. Specifically, the sequence ID is
  126. used to associate an ``ACK`` with its ``DATA_SEQ``-type frame, but not
  127. ``DATA_SEQ``- or ``DATA_NSQ``-type frames with other ``DATA``- type frames.
  128. An example exchange might look like this:
  129. ::
  130. tx: -- SYN FRAME(D) CRC(F) PAYLOAD CRC(P) -----------------------------
  131. rx: ------------------------------------- SYN FRAME(A) CRC(F) CRC(P) --
  132. where both frames have the same sequence ID (``SEQ``). Here, ``FRAME(D)``
  133. indicates a |DATA_SEQ|-type frame, ``FRAME(A)`` an ``ACK``-type frame,
  134. ``CRC(F)`` the CRC over the previous frame, ``CRC(P)`` the CRC over the
  135. previous payload. In case of an error, the exchange would look like this:
  136. ::
  137. tx: -- SYN FRAME(D) CRC(F) PAYLOAD CRC(P) -----------------------------
  138. rx: ------------------------------------- SYN FRAME(N) CRC(F) CRC(P) --
  139. upon which the sender should re-send the message. ``FRAME(N)`` indicates an
  140. |NAK|-type frame. Note that the sequence ID of the |NAK|-type frame is fixed
  141. to zero. For |DATA_NSQ|-type frames, both exchanges are the same:
  142. ::
  143. tx: -- SYN FRAME(DATA_NSQ) CRC(F) PAYLOAD CRC(P) ----------------------
  144. rx: -------------------------------------------------------------------
  145. Here, an error can be detected, but not corrected or indicated to the
  146. sending party. These exchanges are symmetric, i.e. switching ``rx`` and
  147. ``tx`` results again in a valid exchange. Currently, no longer exchanges are
  148. known.
  149. Commands: Requests, Responses, and Events
  150. =========================================
  151. Commands are sent as payload inside a data frame. Currently, this is the
  152. only known payload type of |DATA| frames, with a payload-type value of
  153. ``0x80`` (:c:type:`SSH_PLD_TYPE_CMD <ssh_payload_type>`).
  154. The command-type payload (:c:type:`struct ssh_command <ssh_command>`)
  155. consists of an eight-byte command structure, followed by optional and
  156. variable length command data. The length of this optional data is derived
  157. from the frame payload length given in the corresponding frame, i.e. it is
  158. ``frame.len - sizeof(struct ssh_command)``. The command struct contains the
  159. following fields, packed together and in order:
  160. .. flat-table:: SSH Command
  161. :widths: 1 1 4
  162. :header-rows: 1
  163. * - Field
  164. - Type
  165. - Description
  166. * - |TYPE|
  167. - |u8|
  168. - Type of the payload. For commands always ``0x80``.
  169. * - |TC|
  170. - |u8|
  171. - Target category.
  172. * - |TID|
  173. - |u8|
  174. - Target ID for commands/messages.
  175. * - |SID|
  176. - |u8|
  177. - Source ID for commands/messages.
  178. * - |IID|
  179. - |u8|
  180. - Instance ID.
  181. * - |RQID|
  182. - |u16|
  183. - Request ID.
  184. * - |CID|
  185. - |u8|
  186. - Command ID.
  187. The command struct and data, in general, does not contain any failure
  188. detection mechanism (e.g. CRCs), this is solely done on the frame level.
  189. Command-type payloads are used by the host to send commands and requests to
  190. the EC as well as by the EC to send responses and events back to the host.
  191. We differentiate between requests (sent by the host), responses (sent by the
  192. EC in response to a request), and events (sent by the EC without a preceding
  193. request).
  194. Commands and events are uniquely identified by their target category
  195. (``TC``) and command ID (``CID``). The target category specifies a general
  196. category for the command (e.g. system in general, vs. battery and AC, vs.
  197. temperature, and so on), while the command ID specifies the command inside
  198. that category. Only the combination of |TC| + |CID| is unique. Additionally,
  199. commands have an instance ID (``IID``), which is used to differentiate
  200. between different sub-devices. For example ``TC=3`` ``CID=1`` is a
  201. request to get the temperature on a thermal sensor, where |IID| specifies
  202. the respective sensor. If the instance ID is not used, it should be set to
  203. zero. If instance IDs are used, they, in general, start with a value of one,
  204. whereas zero may be used for instance independent queries, if applicable. A
  205. response to a request should have the same target category, command ID, and
  206. instance ID as the corresponding request.
  207. Responses are matched to their corresponding request via the request ID
  208. (``RQID``) field. This is a 16 bit wrapping counter similar to the sequence
  209. ID on the frames. Note that the sequence ID of the frames for a
  210. request-response pair does not match. Only the request ID has to match.
  211. Frame-protocol wise these are two separate exchanges, and may even be
  212. separated, e.g. by an event being sent after the request but before the
  213. response. Not all commands produce a response, and this is not detectable by
  214. |TC| + |CID|. It is the responsibility of the issuing party to wait for a
  215. response (or signal this to the communication framework, as is done in
  216. SAN/ACPI via the ``SNC`` flag).
  217. Events are identified by unique and reserved request IDs. These IDs should
  218. not be used by the host when sending a new request. They are used on the
  219. host to, first, detect events and, second, match them with a registered
  220. event handler. Request IDs for events are chosen by the host and directed to
  221. the EC when setting up and enabling an event source (via the
  222. enable-event-source request). The EC then uses the specified request ID for
  223. events sent from the respective source. Note that an event should still be
  224. identified by its target category, command ID, and, if applicable, instance
  225. ID, as a single event source can send multiple different event types. In
  226. general, however, a single target category should map to a single reserved
  227. event request ID.
  228. Furthermore, requests, responses, and events have an associated target ID
  229. (``TID``) and source ID (``SID``). These two fields indicate where a message
  230. originates from (``SID``) and what the intended target of the message is
  231. (``TID``). Note that a response to a specific request therefore has the source
  232. and target IDs swapped when compared to the original request (i.e. the request
  233. target is the response source and the request source is the response target).
  234. See (:c:type:`enum ssh_request_id <ssh_request_id>`) for possible values of
  235. both.
  236. Note that, even though requests and events should be uniquely identifiable by
  237. target category and command ID alone, the EC may require specific target ID and
  238. instance ID values to accept a command. A command that is accepted for
  239. ``TID=1``, for example, may not be accepted for ``TID=2`` and vice versa. While
  240. this may not always hold in reality, you can think of different target/source
  241. IDs indicating different physical ECs with potentially different feature sets.
  242. Limitations and Observations
  243. ============================
  244. The protocol can, in theory, handle up to ``U8_MAX`` frames in parallel,
  245. with up to ``U16_MAX`` pending requests (neglecting request IDs reserved for
  246. events). In practice, however, this is more limited. From our testing
  247. (although via a python and thus a user-space program), it seems that the EC
  248. can handle up to four requests (mostly) reliably in parallel at a certain
  249. time. With five or more requests in parallel, consistent discarding of
  250. commands (ACKed frame but no command response) has been observed. For five
  251. simultaneous commands, this reproducibly resulted in one command being
  252. dropped and four commands being handled.
  253. However, it has also been noted that, even with three requests in parallel,
  254. occasional frame drops happen. Apart from this, with a limit of three
  255. pending requests, no dropped commands (i.e. command being dropped but frame
  256. carrying command being ACKed) have been observed. In any case, frames (and
  257. possibly also commands) should be re-sent by the host if a certain timeout
  258. is exceeded. This is done by the EC for frames with a timeout of one second,
  259. up to two re-tries (i.e. three transmissions in total). The limit of
  260. re-tries also applies to received NAKs, and, in a worst case scenario, can
  261. lead to entire messages being dropped.
  262. While this also seems to work fine for pending data frames as long as no
  263. transmission failures occur, implementation and handling of these seems to
  264. depend on the assumption that there is only one non-acknowledged data frame.
  265. In particular, the detection of repeated frames relies on the last sequence
  266. number. This means that, if a frame that has been successfully received by
  267. the EC is sent again, e.g. due to the host not receiving an |ACK|, the EC
  268. will only detect this if it has the sequence ID of the last frame received
  269. by the EC. As an example: Sending two frames with ``SEQ=0`` and ``SEQ=1``
  270. followed by a repetition of ``SEQ=0`` will not detect the second ``SEQ=0``
  271. frame as such, and thus execute the command in this frame each time it has
  272. been received, i.e. twice in this example. Sending ``SEQ=0``, ``SEQ=1`` and
  273. then repeating ``SEQ=1`` will detect the second ``SEQ=1`` as repetition of
  274. the first one and ignore it, thus executing the contained command only once.
  275. In conclusion, this suggests a limit of at most one pending un-ACKed frame
  276. (per party, effectively leading to synchronous communication regarding
  277. frames) and at most three pending commands. The limit to synchronous frame
  278. transfers seems to be consistent with behavior observed on Windows.