controller.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Surface System Aggregator Module (SSAM) controller interface.
  4. *
  5. * Main communication interface for the SSAM EC. Provides a controller
  6. * managing access and communication to and from the SSAM EC, as well as main
  7. * communication structures and definitions.
  8. *
  9. * Copyright (C) 2019-2021 Maximilian Luz <[email protected]>
  10. */
  11. #ifndef _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H
  12. #define _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H
  13. #include <linux/completion.h>
  14. #include <linux/device.h>
  15. #include <linux/types.h>
  16. #include <linux/surface_aggregator/serial_hub.h>
  17. /* -- Main data types and definitions --------------------------------------- */
  18. /**
  19. * enum ssam_event_flags - Flags for enabling/disabling SAM events
  20. * @SSAM_EVENT_SEQUENCED: The event will be sent via a sequenced data frame.
  21. */
  22. enum ssam_event_flags {
  23. SSAM_EVENT_SEQUENCED = BIT(0),
  24. };
  25. /**
  26. * struct ssam_event - SAM event sent from the EC to the host.
  27. * @target_category: Target category of the event source. See &enum ssam_ssh_tc.
  28. * @target_id: Target ID of the event source.
  29. * @command_id: Command ID of the event.
  30. * @instance_id: Instance ID of the event source.
  31. * @length: Length of the event payload in bytes.
  32. * @data: Event payload data.
  33. */
  34. struct ssam_event {
  35. u8 target_category;
  36. u8 target_id;
  37. u8 command_id;
  38. u8 instance_id;
  39. u16 length;
  40. u8 data[];
  41. };
  42. /**
  43. * enum ssam_request_flags - Flags for SAM requests.
  44. *
  45. * @SSAM_REQUEST_HAS_RESPONSE:
  46. * Specifies that the request expects a response. If not set, the request
  47. * will be directly completed after its underlying packet has been
  48. * transmitted. If set, the request transport system waits for a response
  49. * of the request.
  50. *
  51. * @SSAM_REQUEST_UNSEQUENCED:
  52. * Specifies that the request should be transmitted via an unsequenced
  53. * packet. If set, the request must not have a response, meaning that this
  54. * flag and the %SSAM_REQUEST_HAS_RESPONSE flag are mutually exclusive.
  55. */
  56. enum ssam_request_flags {
  57. SSAM_REQUEST_HAS_RESPONSE = BIT(0),
  58. SSAM_REQUEST_UNSEQUENCED = BIT(1),
  59. };
  60. /**
  61. * struct ssam_request - SAM request description.
  62. * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
  63. * @target_id: ID of the request's target.
  64. * @command_id: Command ID of the request.
  65. * @instance_id: Instance ID of the request's target.
  66. * @flags: Flags for the request. See &enum ssam_request_flags.
  67. * @length: Length of the request payload in bytes.
  68. * @payload: Request payload data.
  69. *
  70. * This struct fully describes a SAM request with payload. It is intended to
  71. * help set up the actual transport struct, e.g. &struct ssam_request_sync,
  72. * and specifically its raw message data via ssam_request_write_data().
  73. */
  74. struct ssam_request {
  75. u8 target_category;
  76. u8 target_id;
  77. u8 command_id;
  78. u8 instance_id;
  79. u16 flags;
  80. u16 length;
  81. const u8 *payload;
  82. };
  83. /**
  84. * struct ssam_response - Response buffer for SAM request.
  85. * @capacity: Capacity of the buffer, in bytes.
  86. * @length: Length of the actual data stored in the memory pointed to by
  87. * @pointer, in bytes. Set by the transport system.
  88. * @pointer: Pointer to the buffer's memory, storing the response payload data.
  89. */
  90. struct ssam_response {
  91. size_t capacity;
  92. size_t length;
  93. u8 *pointer;
  94. };
  95. struct ssam_controller;
  96. struct ssam_controller *ssam_get_controller(void);
  97. struct ssam_controller *ssam_client_bind(struct device *client);
  98. int ssam_client_link(struct ssam_controller *ctrl, struct device *client);
  99. struct device *ssam_controller_device(struct ssam_controller *c);
  100. struct ssam_controller *ssam_controller_get(struct ssam_controller *c);
  101. void ssam_controller_put(struct ssam_controller *c);
  102. void ssam_controller_statelock(struct ssam_controller *c);
  103. void ssam_controller_stateunlock(struct ssam_controller *c);
  104. ssize_t ssam_request_write_data(struct ssam_span *buf,
  105. struct ssam_controller *ctrl,
  106. const struct ssam_request *spec);
  107. /* -- Synchronous request interface. ---------------------------------------- */
  108. /**
  109. * struct ssam_request_sync - Synchronous SAM request struct.
  110. * @base: Underlying SSH request.
  111. * @comp: Completion used to signal full completion of the request. After the
  112. * request has been submitted, this struct may only be modified or
  113. * deallocated after the completion has been signaled.
  114. * request has been submitted,
  115. * @resp: Buffer to store the response.
  116. * @status: Status of the request, set after the base request has been
  117. * completed or has failed.
  118. */
  119. struct ssam_request_sync {
  120. struct ssh_request base;
  121. struct completion comp;
  122. struct ssam_response *resp;
  123. int status;
  124. };
  125. int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
  126. struct ssam_request_sync **rqst,
  127. struct ssam_span *buffer);
  128. void ssam_request_sync_free(struct ssam_request_sync *rqst);
  129. int ssam_request_sync_init(struct ssam_request_sync *rqst,
  130. enum ssam_request_flags flags);
  131. /**
  132. * ssam_request_sync_set_data - Set message data of a synchronous request.
  133. * @rqst: The request.
  134. * @ptr: Pointer to the request message data.
  135. * @len: Length of the request message data.
  136. *
  137. * Set the request message data of a synchronous request. The provided buffer
  138. * needs to live until the request has been completed.
  139. */
  140. static inline void ssam_request_sync_set_data(struct ssam_request_sync *rqst,
  141. u8 *ptr, size_t len)
  142. {
  143. ssh_request_set_data(&rqst->base, ptr, len);
  144. }
  145. /**
  146. * ssam_request_sync_set_resp - Set response buffer of a synchronous request.
  147. * @rqst: The request.
  148. * @resp: The response buffer.
  149. *
  150. * Sets the response buffer of a synchronous request. This buffer will store
  151. * the response of the request after it has been completed. May be %NULL if no
  152. * response is expected.
  153. */
  154. static inline void ssam_request_sync_set_resp(struct ssam_request_sync *rqst,
  155. struct ssam_response *resp)
  156. {
  157. rqst->resp = resp;
  158. }
  159. int ssam_request_sync_submit(struct ssam_controller *ctrl,
  160. struct ssam_request_sync *rqst);
  161. /**
  162. * ssam_request_sync_wait - Wait for completion of a synchronous request.
  163. * @rqst: The request to wait for.
  164. *
  165. * Wait for completion and release of a synchronous request. After this
  166. * function terminates, the request is guaranteed to have left the transport
  167. * system. After successful submission of a request, this function must be
  168. * called before accessing the response of the request, freeing the request,
  169. * or freeing any of the buffers associated with the request.
  170. *
  171. * This function must not be called if the request has not been submitted yet
  172. * and may lead to a deadlock/infinite wait if a subsequent request submission
  173. * fails in that case, due to the completion never triggering.
  174. *
  175. * Return: Returns the status of the given request, which is set on completion
  176. * of the packet. This value is zero on success and negative on failure.
  177. */
  178. static inline int ssam_request_sync_wait(struct ssam_request_sync *rqst)
  179. {
  180. wait_for_completion(&rqst->comp);
  181. return rqst->status;
  182. }
  183. int ssam_request_sync(struct ssam_controller *ctrl,
  184. const struct ssam_request *spec,
  185. struct ssam_response *rsp);
  186. int ssam_request_sync_with_buffer(struct ssam_controller *ctrl,
  187. const struct ssam_request *spec,
  188. struct ssam_response *rsp,
  189. struct ssam_span *buf);
  190. /**
  191. * ssam_request_sync_onstack - Execute a synchronous request on the stack.
  192. * @ctrl: The controller via which the request is submitted.
  193. * @rqst: The request specification.
  194. * @rsp: The response buffer.
  195. * @payload_len: The (maximum) request payload length.
  196. *
  197. * Allocates a synchronous request with specified payload length on the stack,
  198. * fully initializes it via the provided request specification, submits it,
  199. * and finally waits for its completion before returning its status. This
  200. * helper macro essentially allocates the request message buffer on the stack
  201. * and then calls ssam_request_sync_with_buffer().
  202. *
  203. * Note: The @payload_len parameter specifies the maximum payload length, used
  204. * for buffer allocation. The actual payload length may be smaller.
  205. *
  206. * Return: Returns the status of the request or any failure during setup, i.e.
  207. * zero on success and a negative value on failure.
  208. */
  209. #define ssam_request_sync_onstack(ctrl, rqst, rsp, payload_len) \
  210. ({ \
  211. u8 __data[SSH_COMMAND_MESSAGE_LENGTH(payload_len)]; \
  212. struct ssam_span __buf = { &__data[0], ARRAY_SIZE(__data) }; \
  213. \
  214. ssam_request_sync_with_buffer(ctrl, rqst, rsp, &__buf); \
  215. })
  216. /**
  217. * __ssam_retry - Retry request in case of I/O errors or timeouts.
  218. * @request: The request function to execute. Must return an integer.
  219. * @n: Number of tries.
  220. * @args: Arguments for the request function.
  221. *
  222. * Executes the given request function, i.e. calls @request. In case the
  223. * request returns %-EREMOTEIO (indicates I/O error) or %-ETIMEDOUT (request
  224. * or underlying packet timed out), @request will be re-executed again, up to
  225. * @n times in total.
  226. *
  227. * Return: Returns the return value of the last execution of @request.
  228. */
  229. #define __ssam_retry(request, n, args...) \
  230. ({ \
  231. int __i, __s = 0; \
  232. \
  233. for (__i = (n); __i > 0; __i--) { \
  234. __s = request(args); \
  235. if (__s != -ETIMEDOUT && __s != -EREMOTEIO) \
  236. break; \
  237. } \
  238. __s; \
  239. })
  240. /**
  241. * ssam_retry - Retry request in case of I/O errors or timeouts up to three
  242. * times in total.
  243. * @request: The request function to execute. Must return an integer.
  244. * @args: Arguments for the request function.
  245. *
  246. * Executes the given request function, i.e. calls @request. In case the
  247. * request returns %-EREMOTEIO (indicates I/O error) or -%ETIMEDOUT (request
  248. * or underlying packet timed out), @request will be re-executed again, up to
  249. * three times in total.
  250. *
  251. * See __ssam_retry() for a more generic macro for this purpose.
  252. *
  253. * Return: Returns the return value of the last execution of @request.
  254. */
  255. #define ssam_retry(request, args...) \
  256. __ssam_retry(request, 3, args)
  257. /**
  258. * struct ssam_request_spec - Blue-print specification of SAM request.
  259. * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
  260. * @target_id: ID of the request's target.
  261. * @command_id: Command ID of the request.
  262. * @instance_id: Instance ID of the request's target.
  263. * @flags: Flags for the request. See &enum ssam_request_flags.
  264. *
  265. * Blue-print specification for a SAM request. This struct describes the
  266. * unique static parameters of a request (i.e. type) without specifying any of
  267. * its instance-specific data (e.g. payload). It is intended to be used as base
  268. * for defining simple request functions via the
  269. * ``SSAM_DEFINE_SYNC_REQUEST_x()`` family of macros.
  270. */
  271. struct ssam_request_spec {
  272. u8 target_category;
  273. u8 target_id;
  274. u8 command_id;
  275. u8 instance_id;
  276. u8 flags;
  277. };
  278. /**
  279. * struct ssam_request_spec_md - Blue-print specification for multi-device SAM
  280. * request.
  281. * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
  282. * @command_id: Command ID of the request.
  283. * @flags: Flags for the request. See &enum ssam_request_flags.
  284. *
  285. * Blue-print specification for a multi-device SAM request, i.e. a request
  286. * that is applicable to multiple device instances, described by their
  287. * individual target and instance IDs. This struct describes the unique static
  288. * parameters of a request (i.e. type) without specifying any of its
  289. * instance-specific data (e.g. payload) and without specifying any of its
  290. * device specific IDs (i.e. target and instance ID). It is intended to be
  291. * used as base for defining simple multi-device request functions via the
  292. * ``SSAM_DEFINE_SYNC_REQUEST_MD_x()`` and ``SSAM_DEFINE_SYNC_REQUEST_CL_x()``
  293. * families of macros.
  294. */
  295. struct ssam_request_spec_md {
  296. u8 target_category;
  297. u8 command_id;
  298. u8 flags;
  299. };
  300. /**
  301. * SSAM_DEFINE_SYNC_REQUEST_N() - Define synchronous SAM request function
  302. * with neither argument nor return value.
  303. * @name: Name of the generated function.
  304. * @spec: Specification (&struct ssam_request_spec) defining the request.
  305. *
  306. * Defines a function executing the synchronous SAM request specified by
  307. * @spec, with the request having neither argument nor return value. The
  308. * generated function takes care of setting up the request struct and buffer
  309. * allocation, as well as execution of the request itself, returning once the
  310. * request has been fully completed. The required transport buffer will be
  311. * allocated on the stack.
  312. *
  313. * The generated function is defined as ``static int name(struct
  314. * ssam_controller *ctrl)``, returning the status of the request, which is
  315. * zero on success and negative on failure. The ``ctrl`` parameter is the
  316. * controller via which the request is being sent.
  317. *
  318. * Refer to ssam_request_sync_onstack() for more details on the behavior of
  319. * the generated function.
  320. */
  321. #define SSAM_DEFINE_SYNC_REQUEST_N(name, spec...) \
  322. static int name(struct ssam_controller *ctrl) \
  323. { \
  324. struct ssam_request_spec s = (struct ssam_request_spec)spec; \
  325. struct ssam_request rqst; \
  326. \
  327. rqst.target_category = s.target_category; \
  328. rqst.target_id = s.target_id; \
  329. rqst.command_id = s.command_id; \
  330. rqst.instance_id = s.instance_id; \
  331. rqst.flags = s.flags; \
  332. rqst.length = 0; \
  333. rqst.payload = NULL; \
  334. \
  335. return ssam_request_sync_onstack(ctrl, &rqst, NULL, 0); \
  336. }
  337. /**
  338. * SSAM_DEFINE_SYNC_REQUEST_W() - Define synchronous SAM request function with
  339. * argument.
  340. * @name: Name of the generated function.
  341. * @atype: Type of the request's argument.
  342. * @spec: Specification (&struct ssam_request_spec) defining the request.
  343. *
  344. * Defines a function executing the synchronous SAM request specified by
  345. * @spec, with the request taking an argument of type @atype and having no
  346. * return value. The generated function takes care of setting up the request
  347. * struct, buffer allocation, as well as execution of the request itself,
  348. * returning once the request has been fully completed. The required transport
  349. * buffer will be allocated on the stack.
  350. *
  351. * The generated function is defined as ``static int name(struct
  352. * ssam_controller *ctrl, const atype *arg)``, returning the status of the
  353. * request, which is zero on success and negative on failure. The ``ctrl``
  354. * parameter is the controller via which the request is sent. The request
  355. * argument is specified via the ``arg`` pointer.
  356. *
  357. * Refer to ssam_request_sync_onstack() for more details on the behavior of
  358. * the generated function.
  359. */
  360. #define SSAM_DEFINE_SYNC_REQUEST_W(name, atype, spec...) \
  361. static int name(struct ssam_controller *ctrl, const atype *arg) \
  362. { \
  363. struct ssam_request_spec s = (struct ssam_request_spec)spec; \
  364. struct ssam_request rqst; \
  365. \
  366. rqst.target_category = s.target_category; \
  367. rqst.target_id = s.target_id; \
  368. rqst.command_id = s.command_id; \
  369. rqst.instance_id = s.instance_id; \
  370. rqst.flags = s.flags; \
  371. rqst.length = sizeof(atype); \
  372. rqst.payload = (u8 *)arg; \
  373. \
  374. return ssam_request_sync_onstack(ctrl, &rqst, NULL, \
  375. sizeof(atype)); \
  376. }
  377. /**
  378. * SSAM_DEFINE_SYNC_REQUEST_R() - Define synchronous SAM request function with
  379. * return value.
  380. * @name: Name of the generated function.
  381. * @rtype: Type of the request's return value.
  382. * @spec: Specification (&struct ssam_request_spec) defining the request.
  383. *
  384. * Defines a function executing the synchronous SAM request specified by
  385. * @spec, with the request taking no argument but having a return value of
  386. * type @rtype. The generated function takes care of setting up the request
  387. * and response structs, buffer allocation, as well as execution of the
  388. * request itself, returning once the request has been fully completed. The
  389. * required transport buffer will be allocated on the stack.
  390. *
  391. * The generated function is defined as ``static int name(struct
  392. * ssam_controller *ctrl, rtype *ret)``, returning the status of the request,
  393. * which is zero on success and negative on failure. The ``ctrl`` parameter is
  394. * the controller via which the request is sent. The request's return value is
  395. * written to the memory pointed to by the ``ret`` parameter.
  396. *
  397. * Refer to ssam_request_sync_onstack() for more details on the behavior of
  398. * the generated function.
  399. */
  400. #define SSAM_DEFINE_SYNC_REQUEST_R(name, rtype, spec...) \
  401. static int name(struct ssam_controller *ctrl, rtype *ret) \
  402. { \
  403. struct ssam_request_spec s = (struct ssam_request_spec)spec; \
  404. struct ssam_request rqst; \
  405. struct ssam_response rsp; \
  406. int status; \
  407. \
  408. rqst.target_category = s.target_category; \
  409. rqst.target_id = s.target_id; \
  410. rqst.command_id = s.command_id; \
  411. rqst.instance_id = s.instance_id; \
  412. rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
  413. rqst.length = 0; \
  414. rqst.payload = NULL; \
  415. \
  416. rsp.capacity = sizeof(rtype); \
  417. rsp.length = 0; \
  418. rsp.pointer = (u8 *)ret; \
  419. \
  420. status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, 0); \
  421. if (status) \
  422. return status; \
  423. \
  424. if (rsp.length != sizeof(rtype)) { \
  425. struct device *dev = ssam_controller_device(ctrl); \
  426. dev_err(dev, \
  427. "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
  428. sizeof(rtype), rsp.length, rqst.target_category,\
  429. rqst.command_id); \
  430. return -EIO; \
  431. } \
  432. \
  433. return 0; \
  434. }
  435. /**
  436. * SSAM_DEFINE_SYNC_REQUEST_WR() - Define synchronous SAM request function with
  437. * both argument and return value.
  438. * @name: Name of the generated function.
  439. * @atype: Type of the request's argument.
  440. * @rtype: Type of the request's return value.
  441. * @spec: Specification (&struct ssam_request_spec) defining the request.
  442. *
  443. * Defines a function executing the synchronous SAM request specified by @spec,
  444. * with the request taking an argument of type @atype and having a return value
  445. * of type @rtype. The generated function takes care of setting up the request
  446. * and response structs, buffer allocation, as well as execution of the request
  447. * itself, returning once the request has been fully completed. The required
  448. * transport buffer will be allocated on the stack.
  449. *
  450. * The generated function is defined as ``static int name(struct
  451. * ssam_controller *ctrl, const atype *arg, rtype *ret)``, returning the status
  452. * of the request, which is zero on success and negative on failure. The
  453. * ``ctrl`` parameter is the controller via which the request is sent. The
  454. * request argument is specified via the ``arg`` pointer. The request's return
  455. * value is written to the memory pointed to by the ``ret`` parameter.
  456. *
  457. * Refer to ssam_request_sync_onstack() for more details on the behavior of
  458. * the generated function.
  459. */
  460. #define SSAM_DEFINE_SYNC_REQUEST_WR(name, atype, rtype, spec...) \
  461. static int name(struct ssam_controller *ctrl, const atype *arg, rtype *ret) \
  462. { \
  463. struct ssam_request_spec s = (struct ssam_request_spec)spec; \
  464. struct ssam_request rqst; \
  465. struct ssam_response rsp; \
  466. int status; \
  467. \
  468. rqst.target_category = s.target_category; \
  469. rqst.target_id = s.target_id; \
  470. rqst.command_id = s.command_id; \
  471. rqst.instance_id = s.instance_id; \
  472. rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
  473. rqst.length = sizeof(atype); \
  474. rqst.payload = (u8 *)arg; \
  475. \
  476. rsp.capacity = sizeof(rtype); \
  477. rsp.length = 0; \
  478. rsp.pointer = (u8 *)ret; \
  479. \
  480. status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, sizeof(atype)); \
  481. if (status) \
  482. return status; \
  483. \
  484. if (rsp.length != sizeof(rtype)) { \
  485. struct device *dev = ssam_controller_device(ctrl); \
  486. dev_err(dev, \
  487. "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
  488. sizeof(rtype), rsp.length, rqst.target_category,\
  489. rqst.command_id); \
  490. return -EIO; \
  491. } \
  492. \
  493. return 0; \
  494. }
  495. /**
  496. * SSAM_DEFINE_SYNC_REQUEST_MD_N() - Define synchronous multi-device SAM
  497. * request function with neither argument nor return value.
  498. * @name: Name of the generated function.
  499. * @spec: Specification (&struct ssam_request_spec_md) defining the request.
  500. *
  501. * Defines a function executing the synchronous SAM request specified by
  502. * @spec, with the request having neither argument nor return value. Device
  503. * specifying parameters are not hard-coded, but instead must be provided to
  504. * the function. The generated function takes care of setting up the request
  505. * struct, buffer allocation, as well as execution of the request itself,
  506. * returning once the request has been fully completed. The required transport
  507. * buffer will be allocated on the stack.
  508. *
  509. * The generated function is defined as ``static int name(struct
  510. * ssam_controller *ctrl, u8 tid, u8 iid)``, returning the status of the
  511. * request, which is zero on success and negative on failure. The ``ctrl``
  512. * parameter is the controller via which the request is sent, ``tid`` the
  513. * target ID for the request, and ``iid`` the instance ID.
  514. *
  515. * Refer to ssam_request_sync_onstack() for more details on the behavior of
  516. * the generated function.
  517. */
  518. #define SSAM_DEFINE_SYNC_REQUEST_MD_N(name, spec...) \
  519. static int name(struct ssam_controller *ctrl, u8 tid, u8 iid) \
  520. { \
  521. struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
  522. struct ssam_request rqst; \
  523. \
  524. rqst.target_category = s.target_category; \
  525. rqst.target_id = tid; \
  526. rqst.command_id = s.command_id; \
  527. rqst.instance_id = iid; \
  528. rqst.flags = s.flags; \
  529. rqst.length = 0; \
  530. rqst.payload = NULL; \
  531. \
  532. return ssam_request_sync_onstack(ctrl, &rqst, NULL, 0); \
  533. }
  534. /**
  535. * SSAM_DEFINE_SYNC_REQUEST_MD_W() - Define synchronous multi-device SAM
  536. * request function with argument.
  537. * @name: Name of the generated function.
  538. * @atype: Type of the request's argument.
  539. * @spec: Specification (&struct ssam_request_spec_md) defining the request.
  540. *
  541. * Defines a function executing the synchronous SAM request specified by
  542. * @spec, with the request taking an argument of type @atype and having no
  543. * return value. Device specifying parameters are not hard-coded, but instead
  544. * must be provided to the function. The generated function takes care of
  545. * setting up the request struct, buffer allocation, as well as execution of
  546. * the request itself, returning once the request has been fully completed.
  547. * The required transport buffer will be allocated on the stack.
  548. *
  549. * The generated function is defined as ``static int name(struct
  550. * ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg)``, returning the
  551. * status of the request, which is zero on success and negative on failure.
  552. * The ``ctrl`` parameter is the controller via which the request is sent,
  553. * ``tid`` the target ID for the request, and ``iid`` the instance ID. The
  554. * request argument is specified via the ``arg`` pointer.
  555. *
  556. * Refer to ssam_request_sync_onstack() for more details on the behavior of
  557. * the generated function.
  558. */
  559. #define SSAM_DEFINE_SYNC_REQUEST_MD_W(name, atype, spec...) \
  560. static int name(struct ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg) \
  561. { \
  562. struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
  563. struct ssam_request rqst; \
  564. \
  565. rqst.target_category = s.target_category; \
  566. rqst.target_id = tid; \
  567. rqst.command_id = s.command_id; \
  568. rqst.instance_id = iid; \
  569. rqst.flags = s.flags; \
  570. rqst.length = sizeof(atype); \
  571. rqst.payload = (u8 *)arg; \
  572. \
  573. return ssam_request_sync_onstack(ctrl, &rqst, NULL, \
  574. sizeof(atype)); \
  575. }
  576. /**
  577. * SSAM_DEFINE_SYNC_REQUEST_MD_R() - Define synchronous multi-device SAM
  578. * request function with return value.
  579. * @name: Name of the generated function.
  580. * @rtype: Type of the request's return value.
  581. * @spec: Specification (&struct ssam_request_spec_md) defining the request.
  582. *
  583. * Defines a function executing the synchronous SAM request specified by
  584. * @spec, with the request taking no argument but having a return value of
  585. * type @rtype. Device specifying parameters are not hard-coded, but instead
  586. * must be provided to the function. The generated function takes care of
  587. * setting up the request and response structs, buffer allocation, as well as
  588. * execution of the request itself, returning once the request has been fully
  589. * completed. The required transport buffer will be allocated on the stack.
  590. *
  591. * The generated function is defined as ``static int name(struct
  592. * ssam_controller *ctrl, u8 tid, u8 iid, rtype *ret)``, returning the status
  593. * of the request, which is zero on success and negative on failure. The
  594. * ``ctrl`` parameter is the controller via which the request is sent, ``tid``
  595. * the target ID for the request, and ``iid`` the instance ID. The request's
  596. * return value is written to the memory pointed to by the ``ret`` parameter.
  597. *
  598. * Refer to ssam_request_sync_onstack() for more details on the behavior of
  599. * the generated function.
  600. */
  601. #define SSAM_DEFINE_SYNC_REQUEST_MD_R(name, rtype, spec...) \
  602. static int name(struct ssam_controller *ctrl, u8 tid, u8 iid, rtype *ret) \
  603. { \
  604. struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
  605. struct ssam_request rqst; \
  606. struct ssam_response rsp; \
  607. int status; \
  608. \
  609. rqst.target_category = s.target_category; \
  610. rqst.target_id = tid; \
  611. rqst.command_id = s.command_id; \
  612. rqst.instance_id = iid; \
  613. rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
  614. rqst.length = 0; \
  615. rqst.payload = NULL; \
  616. \
  617. rsp.capacity = sizeof(rtype); \
  618. rsp.length = 0; \
  619. rsp.pointer = (u8 *)ret; \
  620. \
  621. status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, 0); \
  622. if (status) \
  623. return status; \
  624. \
  625. if (rsp.length != sizeof(rtype)) { \
  626. struct device *dev = ssam_controller_device(ctrl); \
  627. dev_err(dev, \
  628. "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
  629. sizeof(rtype), rsp.length, rqst.target_category,\
  630. rqst.command_id); \
  631. return -EIO; \
  632. } \
  633. \
  634. return 0; \
  635. }
  636. /**
  637. * SSAM_DEFINE_SYNC_REQUEST_MD_WR() - Define synchronous multi-device SAM
  638. * request function with both argument and return value.
  639. * @name: Name of the generated function.
  640. * @atype: Type of the request's argument.
  641. * @rtype: Type of the request's return value.
  642. * @spec: Specification (&struct ssam_request_spec_md) defining the request.
  643. *
  644. * Defines a function executing the synchronous SAM request specified by @spec,
  645. * with the request taking an argument of type @atype and having a return value
  646. * of type @rtype. Device specifying parameters are not hard-coded, but instead
  647. * must be provided to the function. The generated function takes care of
  648. * setting up the request and response structs, buffer allocation, as well as
  649. * execution of the request itself, returning once the request has been fully
  650. * completed. The required transport buffer will be allocated on the stack.
  651. *
  652. * The generated function is defined as ``static int name(struct
  653. * ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg, rtype *ret)``,
  654. * returning the status of the request, which is zero on success and negative
  655. * on failure. The ``ctrl`` parameter is the controller via which the request
  656. * is sent, ``tid`` the target ID for the request, and ``iid`` the instance ID.
  657. * The request argument is specified via the ``arg`` pointer. The request's
  658. * return value is written to the memory pointed to by the ``ret`` parameter.
  659. *
  660. * Refer to ssam_request_sync_onstack() for more details on the behavior of
  661. * the generated function.
  662. */
  663. #define SSAM_DEFINE_SYNC_REQUEST_MD_WR(name, atype, rtype, spec...) \
  664. static int name(struct ssam_controller *ctrl, u8 tid, u8 iid, \
  665. const atype *arg, rtype *ret) \
  666. { \
  667. struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
  668. struct ssam_request rqst; \
  669. struct ssam_response rsp; \
  670. int status; \
  671. \
  672. rqst.target_category = s.target_category; \
  673. rqst.target_id = tid; \
  674. rqst.command_id = s.command_id; \
  675. rqst.instance_id = iid; \
  676. rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
  677. rqst.length = sizeof(atype); \
  678. rqst.payload = (u8 *)arg; \
  679. \
  680. rsp.capacity = sizeof(rtype); \
  681. rsp.length = 0; \
  682. rsp.pointer = (u8 *)ret; \
  683. \
  684. status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, sizeof(atype)); \
  685. if (status) \
  686. return status; \
  687. \
  688. if (rsp.length != sizeof(rtype)) { \
  689. struct device *dev = ssam_controller_device(ctrl); \
  690. dev_err(dev, \
  691. "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
  692. sizeof(rtype), rsp.length, rqst.target_category,\
  693. rqst.command_id); \
  694. return -EIO; \
  695. } \
  696. \
  697. return 0; \
  698. }
  699. /* -- Event notifier/callbacks. --------------------------------------------- */
  700. #define SSAM_NOTIF_STATE_SHIFT 2
  701. #define SSAM_NOTIF_STATE_MASK ((1 << SSAM_NOTIF_STATE_SHIFT) - 1)
  702. /**
  703. * enum ssam_notif_flags - Flags used in return values from SSAM notifier
  704. * callback functions.
  705. *
  706. * @SSAM_NOTIF_HANDLED:
  707. * Indicates that the notification has been handled. This flag should be
  708. * set by the handler if the handler can act/has acted upon the event
  709. * provided to it. This flag should not be set if the handler is not a
  710. * primary handler intended for the provided event.
  711. *
  712. * If this flag has not been set by any handler after the notifier chain
  713. * has been traversed, a warning will be emitted, stating that the event
  714. * has not been handled.
  715. *
  716. * @SSAM_NOTIF_STOP:
  717. * Indicates that the notifier traversal should stop. If this flag is
  718. * returned from a notifier callback, notifier chain traversal will
  719. * immediately stop and any remaining notifiers will not be called. This
  720. * flag is automatically set when ssam_notifier_from_errno() is called
  721. * with a negative error value.
  722. */
  723. enum ssam_notif_flags {
  724. SSAM_NOTIF_HANDLED = BIT(0),
  725. SSAM_NOTIF_STOP = BIT(1),
  726. };
  727. struct ssam_event_notifier;
  728. typedef u32 (*ssam_notifier_fn_t)(struct ssam_event_notifier *nf,
  729. const struct ssam_event *event);
  730. /**
  731. * struct ssam_notifier_block - Base notifier block for SSAM event
  732. * notifications.
  733. * @node: The node for the list of notifiers.
  734. * @fn: The callback function of this notifier. This function takes the
  735. * respective notifier block and event as input and should return
  736. * a notifier value, which can either be obtained from the flags
  737. * provided in &enum ssam_notif_flags, converted from a standard
  738. * error value via ssam_notifier_from_errno(), or a combination of
  739. * both (e.g. ``ssam_notifier_from_errno(e) | SSAM_NOTIF_HANDLED``).
  740. * @priority: Priority value determining the order in which notifier callbacks
  741. * will be called. A higher value means higher priority, i.e. the
  742. * associated callback will be executed earlier than other (lower
  743. * priority) callbacks.
  744. */
  745. struct ssam_notifier_block {
  746. struct list_head node;
  747. ssam_notifier_fn_t fn;
  748. int priority;
  749. };
  750. /**
  751. * ssam_notifier_from_errno() - Convert standard error value to notifier
  752. * return code.
  753. * @err: The error code to convert, must be negative (in case of failure) or
  754. * zero (in case of success).
  755. *
  756. * Return: Returns the notifier return value obtained by converting the
  757. * specified @err value. In case @err is negative, the %SSAM_NOTIF_STOP flag
  758. * will be set, causing notifier call chain traversal to abort.
  759. */
  760. static inline u32 ssam_notifier_from_errno(int err)
  761. {
  762. if (WARN_ON(err > 0) || err == 0)
  763. return 0;
  764. else
  765. return ((-err) << SSAM_NOTIF_STATE_SHIFT) | SSAM_NOTIF_STOP;
  766. }
  767. /**
  768. * ssam_notifier_to_errno() - Convert notifier return code to standard error
  769. * value.
  770. * @ret: The notifier return value to convert.
  771. *
  772. * Return: Returns the negative error value encoded in @ret or zero if @ret
  773. * indicates success.
  774. */
  775. static inline int ssam_notifier_to_errno(u32 ret)
  776. {
  777. return -(ret >> SSAM_NOTIF_STATE_SHIFT);
  778. }
  779. /* -- Event/notification registry. ------------------------------------------ */
  780. /**
  781. * struct ssam_event_registry - Registry specification used for enabling events.
  782. * @target_category: Target category for the event registry requests.
  783. * @target_id: Target ID for the event registry requests.
  784. * @cid_enable: Command ID for the event-enable request.
  785. * @cid_disable: Command ID for the event-disable request.
  786. *
  787. * This struct describes a SAM event registry via the minimal collection of
  788. * SAM IDs specifying the requests to use for enabling and disabling an event.
  789. * The individual event to be enabled/disabled itself is specified via &struct
  790. * ssam_event_id.
  791. */
  792. struct ssam_event_registry {
  793. u8 target_category;
  794. u8 target_id;
  795. u8 cid_enable;
  796. u8 cid_disable;
  797. };
  798. /**
  799. * struct ssam_event_id - Unique event ID used for enabling events.
  800. * @target_category: Target category of the event source.
  801. * @instance: Instance ID of the event source.
  802. *
  803. * This struct specifies the event to be enabled/disabled via an externally
  804. * provided registry. It does not specify the registry to be used itself, this
  805. * is done via &struct ssam_event_registry.
  806. */
  807. struct ssam_event_id {
  808. u8 target_category;
  809. u8 instance;
  810. };
  811. /**
  812. * enum ssam_event_mask - Flags specifying how events are matched to notifiers.
  813. *
  814. * @SSAM_EVENT_MASK_NONE:
  815. * Run the callback for any event with matching target category. Do not
  816. * do any additional filtering.
  817. *
  818. * @SSAM_EVENT_MASK_TARGET:
  819. * In addition to filtering by target category, only execute the notifier
  820. * callback for events with a target ID matching to the one of the
  821. * registry used for enabling/disabling the event.
  822. *
  823. * @SSAM_EVENT_MASK_INSTANCE:
  824. * In addition to filtering by target category, only execute the notifier
  825. * callback for events with an instance ID matching to the instance ID
  826. * used when enabling the event.
  827. *
  828. * @SSAM_EVENT_MASK_STRICT:
  829. * Do all the filtering above.
  830. */
  831. enum ssam_event_mask {
  832. SSAM_EVENT_MASK_TARGET = BIT(0),
  833. SSAM_EVENT_MASK_INSTANCE = BIT(1),
  834. SSAM_EVENT_MASK_NONE = 0,
  835. SSAM_EVENT_MASK_STRICT =
  836. SSAM_EVENT_MASK_TARGET
  837. | SSAM_EVENT_MASK_INSTANCE,
  838. };
  839. /**
  840. * SSAM_EVENT_REGISTRY() - Define a new event registry.
  841. * @tc: Target category for the event registry requests.
  842. * @tid: Target ID for the event registry requests.
  843. * @cid_en: Command ID for the event-enable request.
  844. * @cid_dis: Command ID for the event-disable request.
  845. *
  846. * Return: Returns the &struct ssam_event_registry specified by the given
  847. * parameters.
  848. */
  849. #define SSAM_EVENT_REGISTRY(tc, tid, cid_en, cid_dis) \
  850. ((struct ssam_event_registry) { \
  851. .target_category = (tc), \
  852. .target_id = (tid), \
  853. .cid_enable = (cid_en), \
  854. .cid_disable = (cid_dis), \
  855. })
  856. #define SSAM_EVENT_REGISTRY_SAM \
  857. SSAM_EVENT_REGISTRY(SSAM_SSH_TC_SAM, 0x01, 0x0b, 0x0c)
  858. #define SSAM_EVENT_REGISTRY_KIP \
  859. SSAM_EVENT_REGISTRY(SSAM_SSH_TC_KIP, 0x02, 0x27, 0x28)
  860. #define SSAM_EVENT_REGISTRY_REG(tid)\
  861. SSAM_EVENT_REGISTRY(SSAM_SSH_TC_REG, tid, 0x01, 0x02)
  862. /**
  863. * enum ssam_event_notifier_flags - Flags for event notifiers.
  864. * @SSAM_EVENT_NOTIFIER_OBSERVER:
  865. * The corresponding notifier acts as observer. Registering a notifier
  866. * with this flag set will not attempt to enable any event. Equally,
  867. * unregistering will not attempt to disable any event. Note that a
  868. * notifier with this flag may not even correspond to a certain event at
  869. * all, only to a specific event target category. Event matching will not
  870. * be influenced by this flag.
  871. */
  872. enum ssam_event_notifier_flags {
  873. SSAM_EVENT_NOTIFIER_OBSERVER = BIT(0),
  874. };
  875. /**
  876. * struct ssam_event_notifier - Notifier block for SSAM events.
  877. * @base: The base notifier block with callback function and priority.
  878. * @event: The event for which this block will receive notifications.
  879. * @event.reg: Registry via which the event will be enabled/disabled.
  880. * @event.id: ID specifying the event.
  881. * @event.mask: Flags determining how events are matched to the notifier.
  882. * @event.flags: Flags used for enabling the event.
  883. * @flags: Notifier flags (see &enum ssam_event_notifier_flags).
  884. */
  885. struct ssam_event_notifier {
  886. struct ssam_notifier_block base;
  887. struct {
  888. struct ssam_event_registry reg;
  889. struct ssam_event_id id;
  890. enum ssam_event_mask mask;
  891. u8 flags;
  892. } event;
  893. unsigned long flags;
  894. };
  895. int ssam_notifier_register(struct ssam_controller *ctrl,
  896. struct ssam_event_notifier *n);
  897. int __ssam_notifier_unregister(struct ssam_controller *ctrl,
  898. struct ssam_event_notifier *n, bool disable);
  899. /**
  900. * ssam_notifier_unregister() - Unregister an event notifier.
  901. * @ctrl: The controller the notifier has been registered on.
  902. * @n: The event notifier to unregister.
  903. *
  904. * Unregister an event notifier. Decrement the usage counter of the associated
  905. * SAM event if the notifier is not marked as an observer. If the usage counter
  906. * reaches zero, the event will be disabled.
  907. *
  908. * Return: Returns zero on success, %-ENOENT if the given notifier block has
  909. * not been registered on the controller. If the given notifier block was the
  910. * last one associated with its specific event, returns the status of the
  911. * event-disable EC-command.
  912. */
  913. static inline int ssam_notifier_unregister(struct ssam_controller *ctrl,
  914. struct ssam_event_notifier *n)
  915. {
  916. return __ssam_notifier_unregister(ctrl, n, true);
  917. }
  918. int ssam_controller_event_enable(struct ssam_controller *ctrl,
  919. struct ssam_event_registry reg,
  920. struct ssam_event_id id, u8 flags);
  921. int ssam_controller_event_disable(struct ssam_controller *ctrl,
  922. struct ssam_event_registry reg,
  923. struct ssam_event_id id, u8 flags);
  924. #endif /* _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H */