1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Surface System Aggregator Module (SSAM) controller interface.
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.
12 #ifndef _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H
13 #define _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H
15 #include <linux/completion.h>
16 #include <linux/device.h>
17 #include <linux/types.h>
19 #include <linux/surface_aggregator/serial_hub.h>
22 /* -- Main data types and definitions --------------------------------------- */
25 * enum ssam_event_flags - Flags for enabling/disabling SAM events
26 * @SSAM_EVENT_SEQUENCED: The event will be sent via a sequenced data frame.
28 enum ssam_event_flags {
29 SSAM_EVENT_SEQUENCED = BIT(0),
33 * struct ssam_event - SAM event sent from the EC to the host.
34 * @target_category: Target category of the event source. See &enum ssam_ssh_tc.
35 * @target_id: Target ID of the event source.
36 * @command_id: Command ID of the event.
37 * @instance_id: Instance ID of the event source.
38 * @length: Length of the event payload in bytes.
39 * @data: Event payload data.
51 * enum ssam_request_flags - Flags for SAM requests.
53 * @SSAM_REQUEST_HAS_RESPONSE:
54 * Specifies that the request expects a response. If not set, the request
55 * will be directly completed after its underlying packet has been
56 * transmitted. If set, the request transport system waits for a response
59 * @SSAM_REQUEST_UNSEQUENCED:
60 * Specifies that the request should be transmitted via an unsequenced
61 * packet. If set, the request must not have a response, meaning that this
62 * flag and the %SSAM_REQUEST_HAS_RESPONSE flag are mutually exclusive.
64 enum ssam_request_flags {
65 SSAM_REQUEST_HAS_RESPONSE = BIT(0),
66 SSAM_REQUEST_UNSEQUENCED = BIT(1),
70 * struct ssam_request - SAM request description.
71 * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
72 * @target_id: ID of the request's target.
73 * @command_id: Command ID of the request.
74 * @instance_id: Instance ID of the request's target.
75 * @flags: Flags for the request. See &enum ssam_request_flags.
76 * @length: Length of the request payload in bytes.
77 * @payload: Request payload data.
79 * This struct fully describes a SAM request with payload. It is intended to
80 * help set up the actual transport struct, e.g. &struct ssam_request_sync,
81 * and specifically its raw message data via ssam_request_write_data().
94 * struct ssam_response - Response buffer for SAM request.
95 * @capacity: Capacity of the buffer, in bytes.
96 * @length: Length of the actual data stored in the memory pointed to by
97 * @pointer, in bytes. Set by the transport system.
98 * @pointer: Pointer to the buffer's memory, storing the response payload data.
100 struct ssam_response {
106 struct ssam_controller;
108 struct ssam_controller *ssam_get_controller(void);
109 struct ssam_controller *ssam_client_bind(struct device *client);
110 int ssam_client_link(struct ssam_controller *ctrl, struct device *client);
112 struct device *ssam_controller_device(struct ssam_controller *c);
114 struct ssam_controller *ssam_controller_get(struct ssam_controller *c);
115 void ssam_controller_put(struct ssam_controller *c);
117 void ssam_controller_statelock(struct ssam_controller *c);
118 void ssam_controller_stateunlock(struct ssam_controller *c);
120 ssize_t ssam_request_write_data(struct ssam_span *buf,
121 struct ssam_controller *ctrl,
122 const struct ssam_request *spec);
125 /* -- Synchronous request interface. ---------------------------------------- */
128 * struct ssam_request_sync - Synchronous SAM request struct.
129 * @base: Underlying SSH request.
130 * @comp: Completion used to signal full completion of the request. After the
131 * request has been submitted, this struct may only be modified or
132 * deallocated after the completion has been signaled.
133 * request has been submitted,
134 * @resp: Buffer to store the response.
135 * @status: Status of the request, set after the base request has been
136 * completed or has failed.
138 struct ssam_request_sync {
139 struct ssh_request base;
140 struct completion comp;
141 struct ssam_response *resp;
145 int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
146 struct ssam_request_sync **rqst,
147 struct ssam_span *buffer);
149 void ssam_request_sync_free(struct ssam_request_sync *rqst);
151 int ssam_request_sync_init(struct ssam_request_sync *rqst,
152 enum ssam_request_flags flags);
155 * ssam_request_sync_set_data - Set message data of a synchronous request.
156 * @rqst: The request.
157 * @ptr: Pointer to the request message data.
158 * @len: Length of the request message data.
160 * Set the request message data of a synchronous request. The provided buffer
161 * needs to live until the request has been completed.
163 static inline void ssam_request_sync_set_data(struct ssam_request_sync *rqst,
166 ssh_request_set_data(&rqst->base, ptr, len);
170 * ssam_request_sync_set_resp - Set response buffer of a synchronous request.
171 * @rqst: The request.
172 * @resp: The response buffer.
174 * Sets the response buffer of a synchronous request. This buffer will store
175 * the response of the request after it has been completed. May be %NULL if no
176 * response is expected.
178 static inline void ssam_request_sync_set_resp(struct ssam_request_sync *rqst,
179 struct ssam_response *resp)
184 int ssam_request_sync_submit(struct ssam_controller *ctrl,
185 struct ssam_request_sync *rqst);
188 * ssam_request_sync_wait - Wait for completion of a synchronous request.
189 * @rqst: The request to wait for.
191 * Wait for completion and release of a synchronous request. After this
192 * function terminates, the request is guaranteed to have left the transport
193 * system. After successful submission of a request, this function must be
194 * called before accessing the response of the request, freeing the request,
195 * or freeing any of the buffers associated with the request.
197 * This function must not be called if the request has not been submitted yet
198 * and may lead to a deadlock/infinite wait if a subsequent request submission
199 * fails in that case, due to the completion never triggering.
201 * Return: Returns the status of the given request, which is set on completion
202 * of the packet. This value is zero on success and negative on failure.
204 static inline int ssam_request_sync_wait(struct ssam_request_sync *rqst)
206 wait_for_completion(&rqst->comp);
210 int ssam_request_do_sync(struct ssam_controller *ctrl,
211 const struct ssam_request *spec,
212 struct ssam_response *rsp);
214 int ssam_request_do_sync_with_buffer(struct ssam_controller *ctrl,
215 const struct ssam_request *spec,
216 struct ssam_response *rsp,
217 struct ssam_span *buf);
220 * ssam_request_do_sync_onstack - Execute a synchronous request on the stack.
221 * @ctrl: The controller via which the request is submitted.
222 * @rqst: The request specification.
223 * @rsp: The response buffer.
224 * @payload_len: The (maximum) request payload length.
226 * Allocates a synchronous request with specified payload length on the stack,
227 * fully initializes it via the provided request specification, submits it,
228 * and finally waits for its completion before returning its status. This
229 * helper macro essentially allocates the request message buffer on the stack
230 * and then calls ssam_request_do_sync_with_buffer().
232 * Note: The @payload_len parameter specifies the maximum payload length, used
233 * for buffer allocation. The actual payload length may be smaller.
235 * Return: Returns the status of the request or any failure during setup, i.e.
236 * zero on success and a negative value on failure.
238 #define ssam_request_do_sync_onstack(ctrl, rqst, rsp, payload_len) \
240 u8 __data[SSH_COMMAND_MESSAGE_LENGTH(payload_len)]; \
241 struct ssam_span __buf = { &__data[0], ARRAY_SIZE(__data) }; \
243 ssam_request_do_sync_with_buffer(ctrl, rqst, rsp, &__buf); \
247 * __ssam_retry - Retry request in case of I/O errors or timeouts.
248 * @request: The request function to execute. Must return an integer.
249 * @n: Number of tries.
250 * @args: Arguments for the request function.
252 * Executes the given request function, i.e. calls @request. In case the
253 * request returns %-EREMOTEIO (indicates I/O error) or %-ETIMEDOUT (request
254 * or underlying packet timed out), @request will be re-executed again, up to
257 * Return: Returns the return value of the last execution of @request.
259 #define __ssam_retry(request, n, args...) \
263 for (__i = (n); __i > 0; __i--) { \
264 __s = request(args); \
265 if (__s != -ETIMEDOUT && __s != -EREMOTEIO) \
272 * ssam_retry - Retry request in case of I/O errors or timeouts up to three
274 * @request: The request function to execute. Must return an integer.
275 * @args: Arguments for the request function.
277 * Executes the given request function, i.e. calls @request. In case the
278 * request returns %-EREMOTEIO (indicates I/O error) or -%ETIMEDOUT (request
279 * or underlying packet timed out), @request will be re-executed again, up to
280 * three times in total.
282 * See __ssam_retry() for a more generic macro for this purpose.
284 * Return: Returns the return value of the last execution of @request.
286 #define ssam_retry(request, args...) \
287 __ssam_retry(request, 3, args)
290 * struct ssam_request_spec - Blue-print specification of SAM request.
291 * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
292 * @target_id: ID of the request's target.
293 * @command_id: Command ID of the request.
294 * @instance_id: Instance ID of the request's target.
295 * @flags: Flags for the request. See &enum ssam_request_flags.
297 * Blue-print specification for a SAM request. This struct describes the
298 * unique static parameters of a request (i.e. type) without specifying any of
299 * its instance-specific data (e.g. payload). It is intended to be used as base
300 * for defining simple request functions via the
301 * ``SSAM_DEFINE_SYNC_REQUEST_x()`` family of macros.
303 struct ssam_request_spec {
312 * struct ssam_request_spec_md - Blue-print specification for multi-device SAM
314 * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
315 * @command_id: Command ID of the request.
316 * @flags: Flags for the request. See &enum ssam_request_flags.
318 * Blue-print specification for a multi-device SAM request, i.e. a request
319 * that is applicable to multiple device instances, described by their
320 * individual target and instance IDs. This struct describes the unique static
321 * parameters of a request (i.e. type) without specifying any of its
322 * instance-specific data (e.g. payload) and without specifying any of its
323 * device specific IDs (i.e. target and instance ID). It is intended to be
324 * used as base for defining simple multi-device request functions via the
325 * ``SSAM_DEFINE_SYNC_REQUEST_MD_x()`` and ``SSAM_DEFINE_SYNC_REQUEST_CL_x()``
326 * families of macros.
328 struct ssam_request_spec_md {
335 * SSAM_DEFINE_SYNC_REQUEST_N() - Define synchronous SAM request function
336 * with neither argument nor return value.
337 * @name: Name of the generated function.
338 * @spec: Specification (&struct ssam_request_spec) defining the request.
340 * Defines a function executing the synchronous SAM request specified by
341 * @spec, with the request having neither argument nor return value. The
342 * generated function takes care of setting up the request struct and buffer
343 * allocation, as well as execution of the request itself, returning once the
344 * request has been fully completed. The required transport buffer will be
345 * allocated on the stack.
347 * The generated function is defined as ``static int name(struct
348 * ssam_controller *ctrl)``, returning the status of the request, which is
349 * zero on success and negative on failure. The ``ctrl`` parameter is the
350 * controller via which the request is being sent.
352 * Refer to ssam_request_do_sync_onstack() for more details on the behavior of
353 * the generated function.
355 #define SSAM_DEFINE_SYNC_REQUEST_N(name, spec...) \
356 static int name(struct ssam_controller *ctrl) \
358 struct ssam_request_spec s = (struct ssam_request_spec)spec; \
359 struct ssam_request rqst; \
361 rqst.target_category = s.target_category; \
362 rqst.target_id = s.target_id; \
363 rqst.command_id = s.command_id; \
364 rqst.instance_id = s.instance_id; \
365 rqst.flags = s.flags; \
367 rqst.payload = NULL; \
369 return ssam_request_do_sync_onstack(ctrl, &rqst, NULL, 0); \
373 * SSAM_DEFINE_SYNC_REQUEST_W() - Define synchronous SAM request function with
375 * @name: Name of the generated function.
376 * @atype: Type of the request's argument.
377 * @spec: Specification (&struct ssam_request_spec) defining the request.
379 * Defines a function executing the synchronous SAM request specified by
380 * @spec, with the request taking an argument of type @atype and having no
381 * return value. The generated function takes care of setting up the request
382 * struct, buffer allocation, as well as execution of the request itself,
383 * returning once the request has been fully completed. The required transport
384 * buffer will be allocated on the stack.
386 * The generated function is defined as ``static int name(struct
387 * ssam_controller *ctrl, const atype *arg)``, returning the status of the
388 * request, which is zero on success and negative on failure. The ``ctrl``
389 * parameter is the controller via which the request is sent. The request
390 * argument is specified via the ``arg`` pointer.
392 * Refer to ssam_request_do_sync_onstack() for more details on the behavior of
393 * the generated function.
395 #define SSAM_DEFINE_SYNC_REQUEST_W(name, atype, spec...) \
396 static int name(struct ssam_controller *ctrl, const atype *arg) \
398 struct ssam_request_spec s = (struct ssam_request_spec)spec; \
399 struct ssam_request rqst; \
401 rqst.target_category = s.target_category; \
402 rqst.target_id = s.target_id; \
403 rqst.command_id = s.command_id; \
404 rqst.instance_id = s.instance_id; \
405 rqst.flags = s.flags; \
406 rqst.length = sizeof(atype); \
407 rqst.payload = (u8 *)arg; \
409 return ssam_request_do_sync_onstack(ctrl, &rqst, NULL, \
414 * SSAM_DEFINE_SYNC_REQUEST_R() - Define synchronous SAM request function with
416 * @name: Name of the generated function.
417 * @rtype: Type of the request's return value.
418 * @spec: Specification (&struct ssam_request_spec) defining the request.
420 * Defines a function executing the synchronous SAM request specified by
421 * @spec, with the request taking no argument but having a return value of
422 * type @rtype. The generated function takes care of setting up the request
423 * and response structs, buffer allocation, as well as execution of the
424 * request itself, returning once the request has been fully completed. The
425 * required transport buffer will be allocated on the stack.
427 * The generated function is defined as ``static int name(struct
428 * ssam_controller *ctrl, rtype *ret)``, returning the status of the request,
429 * which is zero on success and negative on failure. The ``ctrl`` parameter is
430 * the controller via which the request is sent. The request's return value is
431 * written to the memory pointed to by the ``ret`` parameter.
433 * Refer to ssam_request_do_sync_onstack() for more details on the behavior of
434 * the generated function.
436 #define SSAM_DEFINE_SYNC_REQUEST_R(name, rtype, spec...) \
437 static int name(struct ssam_controller *ctrl, rtype *ret) \
439 struct ssam_request_spec s = (struct ssam_request_spec)spec; \
440 struct ssam_request rqst; \
441 struct ssam_response rsp; \
444 rqst.target_category = s.target_category; \
445 rqst.target_id = s.target_id; \
446 rqst.command_id = s.command_id; \
447 rqst.instance_id = s.instance_id; \
448 rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
450 rqst.payload = NULL; \
452 rsp.capacity = sizeof(rtype); \
454 rsp.pointer = (u8 *)ret; \
456 status = ssam_request_do_sync_onstack(ctrl, &rqst, &rsp, 0); \
460 if (rsp.length != sizeof(rtype)) { \
461 struct device *dev = ssam_controller_device(ctrl); \
463 "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
464 sizeof(rtype), rsp.length, rqst.target_category,\
473 * SSAM_DEFINE_SYNC_REQUEST_WR() - Define synchronous SAM request function with
474 * both argument and return value.
475 * @name: Name of the generated function.
476 * @atype: Type of the request's argument.
477 * @rtype: Type of the request's return value.
478 * @spec: Specification (&struct ssam_request_spec) defining the request.
480 * Defines a function executing the synchronous SAM request specified by @spec,
481 * with the request taking an argument of type @atype and having a return value
482 * of type @rtype. The generated function takes care of setting up the request
483 * and response structs, buffer allocation, as well as execution of the request
484 * itself, returning once the request has been fully completed. The required
485 * transport buffer will be allocated on the stack.
487 * The generated function is defined as ``static int name(struct
488 * ssam_controller *ctrl, const atype *arg, rtype *ret)``, returning the status
489 * of the request, which is zero on success and negative on failure. The
490 * ``ctrl`` parameter is the controller via which the request is sent. The
491 * request argument is specified via the ``arg`` pointer. The request's return
492 * value is written to the memory pointed to by the ``ret`` parameter.
494 * Refer to ssam_request_do_sync_onstack() for more details on the behavior of
495 * the generated function.
497 #define SSAM_DEFINE_SYNC_REQUEST_WR(name, atype, rtype, spec...) \
498 static int name(struct ssam_controller *ctrl, const atype *arg, rtype *ret) \
500 struct ssam_request_spec s = (struct ssam_request_spec)spec; \
501 struct ssam_request rqst; \
502 struct ssam_response rsp; \
505 rqst.target_category = s.target_category; \
506 rqst.target_id = s.target_id; \
507 rqst.command_id = s.command_id; \
508 rqst.instance_id = s.instance_id; \
509 rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
510 rqst.length = sizeof(atype); \
511 rqst.payload = (u8 *)arg; \
513 rsp.capacity = sizeof(rtype); \
515 rsp.pointer = (u8 *)ret; \
517 status = ssam_request_do_sync_onstack(ctrl, &rqst, &rsp, sizeof(atype)); \
521 if (rsp.length != sizeof(rtype)) { \
522 struct device *dev = ssam_controller_device(ctrl); \
524 "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
525 sizeof(rtype), rsp.length, rqst.target_category,\
534 * SSAM_DEFINE_SYNC_REQUEST_MD_N() - Define synchronous multi-device SAM
535 * request function with neither argument nor return value.
536 * @name: Name of the generated function.
537 * @spec: Specification (&struct ssam_request_spec_md) defining the request.
539 * Defines a function executing the synchronous SAM request specified by
540 * @spec, with the request having neither argument nor return value. Device
541 * specifying parameters are not hard-coded, but instead must be provided to
542 * the function. The generated function takes care of setting up the request
543 * struct, buffer allocation, as well as execution of the request itself,
544 * returning once the request has been fully completed. The required transport
545 * buffer will be allocated on the stack.
547 * The generated function is defined as ``static int name(struct
548 * ssam_controller *ctrl, u8 tid, u8 iid)``, returning the status of the
549 * request, which is zero on success and negative on failure. The ``ctrl``
550 * parameter is the controller via which the request is sent, ``tid`` the
551 * target ID for the request, and ``iid`` the instance ID.
553 * Refer to ssam_request_do_sync_onstack() for more details on the behavior of
554 * the generated function.
556 #define SSAM_DEFINE_SYNC_REQUEST_MD_N(name, spec...) \
557 static int name(struct ssam_controller *ctrl, u8 tid, u8 iid) \
559 struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
560 struct ssam_request rqst; \
562 rqst.target_category = s.target_category; \
563 rqst.target_id = tid; \
564 rqst.command_id = s.command_id; \
565 rqst.instance_id = iid; \
566 rqst.flags = s.flags; \
568 rqst.payload = NULL; \
570 return ssam_request_do_sync_onstack(ctrl, &rqst, NULL, 0); \
574 * SSAM_DEFINE_SYNC_REQUEST_MD_W() - Define synchronous multi-device SAM
575 * request function with argument.
576 * @name: Name of the generated function.
577 * @atype: Type of the request's argument.
578 * @spec: Specification (&struct ssam_request_spec_md) defining the request.
580 * Defines a function executing the synchronous SAM request specified by
581 * @spec, with the request taking an argument of type @atype and having no
582 * return value. Device specifying parameters are not hard-coded, but instead
583 * must be provided to the function. The generated function takes care of
584 * setting up the request struct, buffer allocation, as well as execution of
585 * the request itself, returning once the request has been fully completed.
586 * The required transport buffer will be allocated on the stack.
588 * The generated function is defined as ``static int name(struct
589 * ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg)``, returning the
590 * status of the request, which is zero on success and negative on failure.
591 * The ``ctrl`` parameter is the controller via which the request is sent,
592 * ``tid`` the target ID for the request, and ``iid`` the instance ID. The
593 * request argument is specified via the ``arg`` pointer.
595 * Refer to ssam_request_do_sync_onstack() for more details on the behavior of
596 * the generated function.
598 #define SSAM_DEFINE_SYNC_REQUEST_MD_W(name, atype, spec...) \
599 static int name(struct ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg) \
601 struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
602 struct ssam_request rqst; \
604 rqst.target_category = s.target_category; \
605 rqst.target_id = tid; \
606 rqst.command_id = s.command_id; \
607 rqst.instance_id = iid; \
608 rqst.flags = s.flags; \
609 rqst.length = sizeof(atype); \
610 rqst.payload = (u8 *)arg; \
612 return ssam_request_do_sync_onstack(ctrl, &rqst, NULL, \
617 * SSAM_DEFINE_SYNC_REQUEST_MD_R() - Define synchronous multi-device SAM
618 * request function with return value.
619 * @name: Name of the generated function.
620 * @rtype: Type of the request's return value.
621 * @spec: Specification (&struct ssam_request_spec_md) defining the request.
623 * Defines a function executing the synchronous SAM request specified by
624 * @spec, with the request taking no argument but having a return value of
625 * type @rtype. Device specifying parameters are not hard-coded, but instead
626 * must be provided to the function. The generated function takes care of
627 * setting up the request and response structs, buffer allocation, as well as
628 * execution of the request itself, returning once the request has been fully
629 * completed. The required transport buffer will be allocated on the stack.
631 * The generated function is defined as ``static int name(struct
632 * ssam_controller *ctrl, u8 tid, u8 iid, rtype *ret)``, returning the status
633 * of the request, which is zero on success and negative on failure. The
634 * ``ctrl`` parameter is the controller via which the request is sent, ``tid``
635 * the target ID for the request, and ``iid`` the instance ID. The request's
636 * return value is written to the memory pointed to by the ``ret`` parameter.
638 * Refer to ssam_request_do_sync_onstack() for more details on the behavior of
639 * the generated function.
641 #define SSAM_DEFINE_SYNC_REQUEST_MD_R(name, rtype, spec...) \
642 static int name(struct ssam_controller *ctrl, u8 tid, u8 iid, rtype *ret) \
644 struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
645 struct ssam_request rqst; \
646 struct ssam_response rsp; \
649 rqst.target_category = s.target_category; \
650 rqst.target_id = tid; \
651 rqst.command_id = s.command_id; \
652 rqst.instance_id = iid; \
653 rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
655 rqst.payload = NULL; \
657 rsp.capacity = sizeof(rtype); \
659 rsp.pointer = (u8 *)ret; \
661 status = ssam_request_do_sync_onstack(ctrl, &rqst, &rsp, 0); \
665 if (rsp.length != sizeof(rtype)) { \
666 struct device *dev = ssam_controller_device(ctrl); \
668 "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
669 sizeof(rtype), rsp.length, rqst.target_category,\
678 * SSAM_DEFINE_SYNC_REQUEST_MD_WR() - Define synchronous multi-device SAM
679 * request function with both argument and return value.
680 * @name: Name of the generated function.
681 * @atype: Type of the request's argument.
682 * @rtype: Type of the request's return value.
683 * @spec: Specification (&struct ssam_request_spec_md) defining the request.
685 * Defines a function executing the synchronous SAM request specified by @spec,
686 * with the request taking an argument of type @atype and having a return value
687 * of type @rtype. Device specifying parameters are not hard-coded, but instead
688 * must be provided to the function. The generated function takes care of
689 * setting up the request and response structs, buffer allocation, as well as
690 * execution of the request itself, returning once the request has been fully
691 * completed. The required transport buffer will be allocated on the stack.
693 * The generated function is defined as ``static int name(struct
694 * ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg, rtype *ret)``,
695 * returning the status of the request, which is zero on success and negative
696 * on failure. The ``ctrl`` parameter is the controller via which the request
697 * is sent, ``tid`` the target ID for the request, and ``iid`` the instance ID.
698 * The request argument is specified via the ``arg`` pointer. The request's
699 * return value is written to the memory pointed to by the ``ret`` parameter.
701 * Refer to ssam_request_do_sync_onstack() for more details on the behavior of
702 * the generated function.
704 #define SSAM_DEFINE_SYNC_REQUEST_MD_WR(name, atype, rtype, spec...) \
705 static int name(struct ssam_controller *ctrl, u8 tid, u8 iid, \
706 const atype *arg, rtype *ret) \
708 struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
709 struct ssam_request rqst; \
710 struct ssam_response rsp; \
713 rqst.target_category = s.target_category; \
714 rqst.target_id = tid; \
715 rqst.command_id = s.command_id; \
716 rqst.instance_id = iid; \
717 rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
718 rqst.length = sizeof(atype); \
719 rqst.payload = (u8 *)arg; \
721 rsp.capacity = sizeof(rtype); \
723 rsp.pointer = (u8 *)ret; \
725 status = ssam_request_do_sync_onstack(ctrl, &rqst, &rsp, sizeof(atype)); \
729 if (rsp.length != sizeof(rtype)) { \
730 struct device *dev = ssam_controller_device(ctrl); \
732 "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
733 sizeof(rtype), rsp.length, rqst.target_category,\
742 /* -- Event notifier/callbacks. --------------------------------------------- */
744 #define SSAM_NOTIF_STATE_SHIFT 2
745 #define SSAM_NOTIF_STATE_MASK ((1 << SSAM_NOTIF_STATE_SHIFT) - 1)
748 * enum ssam_notif_flags - Flags used in return values from SSAM notifier
749 * callback functions.
751 * @SSAM_NOTIF_HANDLED:
752 * Indicates that the notification has been handled. This flag should be
753 * set by the handler if the handler can act/has acted upon the event
754 * provided to it. This flag should not be set if the handler is not a
755 * primary handler intended for the provided event.
757 * If this flag has not been set by any handler after the notifier chain
758 * has been traversed, a warning will be emitted, stating that the event
759 * has not been handled.
762 * Indicates that the notifier traversal should stop. If this flag is
763 * returned from a notifier callback, notifier chain traversal will
764 * immediately stop and any remaining notifiers will not be called. This
765 * flag is automatically set when ssam_notifier_from_errno() is called
766 * with a negative error value.
768 enum ssam_notif_flags {
769 SSAM_NOTIF_HANDLED = BIT(0),
770 SSAM_NOTIF_STOP = BIT(1),
773 struct ssam_event_notifier;
775 typedef u32 (*ssam_notifier_fn_t)(struct ssam_event_notifier *nf,
776 const struct ssam_event *event);
779 * struct ssam_notifier_block - Base notifier block for SSAM event
781 * @node: The node for the list of notifiers.
782 * @fn: The callback function of this notifier. This function takes the
783 * respective notifier block and event as input and should return
784 * a notifier value, which can either be obtained from the flags
785 * provided in &enum ssam_notif_flags, converted from a standard
786 * error value via ssam_notifier_from_errno(), or a combination of
787 * both (e.g. ``ssam_notifier_from_errno(e) | SSAM_NOTIF_HANDLED``).
788 * @priority: Priority value determining the order in which notifier callbacks
789 * will be called. A higher value means higher priority, i.e. the
790 * associated callback will be executed earlier than other (lower
791 * priority) callbacks.
793 struct ssam_notifier_block {
794 struct list_head node;
795 ssam_notifier_fn_t fn;
800 * ssam_notifier_from_errno() - Convert standard error value to notifier
802 * @err: The error code to convert, must be negative (in case of failure) or
803 * zero (in case of success).
805 * Return: Returns the notifier return value obtained by converting the
806 * specified @err value. In case @err is negative, the %SSAM_NOTIF_STOP flag
807 * will be set, causing notifier call chain traversal to abort.
809 static inline u32 ssam_notifier_from_errno(int err)
811 if (WARN_ON(err > 0) || err == 0)
814 return ((-err) << SSAM_NOTIF_STATE_SHIFT) | SSAM_NOTIF_STOP;
818 * ssam_notifier_to_errno() - Convert notifier return code to standard error
820 * @ret: The notifier return value to convert.
822 * Return: Returns the negative error value encoded in @ret or zero if @ret
825 static inline int ssam_notifier_to_errno(u32 ret)
827 return -(ret >> SSAM_NOTIF_STATE_SHIFT);
831 /* -- Event/notification registry. ------------------------------------------ */
834 * struct ssam_event_registry - Registry specification used for enabling events.
835 * @target_category: Target category for the event registry requests.
836 * @target_id: Target ID for the event registry requests.
837 * @cid_enable: Command ID for the event-enable request.
838 * @cid_disable: Command ID for the event-disable request.
840 * This struct describes a SAM event registry via the minimal collection of
841 * SAM IDs specifying the requests to use for enabling and disabling an event.
842 * The individual event to be enabled/disabled itself is specified via &struct
845 struct ssam_event_registry {
853 * struct ssam_event_id - Unique event ID used for enabling events.
854 * @target_category: Target category of the event source.
855 * @instance: Instance ID of the event source.
857 * This struct specifies the event to be enabled/disabled via an externally
858 * provided registry. It does not specify the registry to be used itself, this
859 * is done via &struct ssam_event_registry.
861 struct ssam_event_id {
867 * enum ssam_event_mask - Flags specifying how events are matched to notifiers.
869 * @SSAM_EVENT_MASK_NONE:
870 * Run the callback for any event with matching target category. Do not
871 * do any additional filtering.
873 * @SSAM_EVENT_MASK_TARGET:
874 * In addition to filtering by target category, only execute the notifier
875 * callback for events with a target ID matching to the one of the
876 * registry used for enabling/disabling the event.
878 * @SSAM_EVENT_MASK_INSTANCE:
879 * In addition to filtering by target category, only execute the notifier
880 * callback for events with an instance ID matching to the instance ID
881 * used when enabling the event.
883 * @SSAM_EVENT_MASK_STRICT:
884 * Do all the filtering above.
886 enum ssam_event_mask {
887 SSAM_EVENT_MASK_TARGET = BIT(0),
888 SSAM_EVENT_MASK_INSTANCE = BIT(1),
890 SSAM_EVENT_MASK_NONE = 0,
891 SSAM_EVENT_MASK_STRICT =
892 SSAM_EVENT_MASK_TARGET
893 | SSAM_EVENT_MASK_INSTANCE,
897 * SSAM_EVENT_REGISTRY() - Define a new event registry.
898 * @tc: Target category for the event registry requests.
899 * @tid: Target ID for the event registry requests.
900 * @cid_en: Command ID for the event-enable request.
901 * @cid_dis: Command ID for the event-disable request.
903 * Return: Returns the &struct ssam_event_registry specified by the given
906 #define SSAM_EVENT_REGISTRY(tc, tid, cid_en, cid_dis) \
907 ((struct ssam_event_registry) { \
908 .target_category = (tc), \
909 .target_id = (tid), \
910 .cid_enable = (cid_en), \
911 .cid_disable = (cid_dis), \
914 #define SSAM_EVENT_REGISTRY_SAM \
915 SSAM_EVENT_REGISTRY(SSAM_SSH_TC_SAM, SSAM_SSH_TID_SAM, 0x0b, 0x0c)
917 #define SSAM_EVENT_REGISTRY_KIP \
918 SSAM_EVENT_REGISTRY(SSAM_SSH_TC_KIP, SSAM_SSH_TID_KIP, 0x27, 0x28)
920 #define SSAM_EVENT_REGISTRY_REG(tid)\
921 SSAM_EVENT_REGISTRY(SSAM_SSH_TC_REG, tid, 0x01, 0x02)
924 * enum ssam_event_notifier_flags - Flags for event notifiers.
925 * @SSAM_EVENT_NOTIFIER_OBSERVER:
926 * The corresponding notifier acts as observer. Registering a notifier
927 * with this flag set will not attempt to enable any event. Equally,
928 * unregistering will not attempt to disable any event. Note that a
929 * notifier with this flag may not even correspond to a certain event at
930 * all, only to a specific event target category. Event matching will not
931 * be influenced by this flag.
933 enum ssam_event_notifier_flags {
934 SSAM_EVENT_NOTIFIER_OBSERVER = BIT(0),
938 * struct ssam_event_notifier - Notifier block for SSAM events.
939 * @base: The base notifier block with callback function and priority.
940 * @event: The event for which this block will receive notifications.
941 * @event.reg: Registry via which the event will be enabled/disabled.
942 * @event.id: ID specifying the event.
943 * @event.mask: Flags determining how events are matched to the notifier.
944 * @event.flags: Flags used for enabling the event.
945 * @flags: Notifier flags (see &enum ssam_event_notifier_flags).
947 struct ssam_event_notifier {
948 struct ssam_notifier_block base;
951 struct ssam_event_registry reg;
952 struct ssam_event_id id;
953 enum ssam_event_mask mask;
960 int ssam_notifier_register(struct ssam_controller *ctrl,
961 struct ssam_event_notifier *n);
963 int __ssam_notifier_unregister(struct ssam_controller *ctrl,
964 struct ssam_event_notifier *n, bool disable);
967 * ssam_notifier_unregister() - Unregister an event notifier.
968 * @ctrl: The controller the notifier has been registered on.
969 * @n: The event notifier to unregister.
971 * Unregister an event notifier. Decrement the usage counter of the associated
972 * SAM event if the notifier is not marked as an observer. If the usage counter
973 * reaches zero, the event will be disabled.
975 * Return: Returns zero on success, %-ENOENT if the given notifier block has
976 * not been registered on the controller. If the given notifier block was the
977 * last one associated with its specific event, returns the status of the
978 * event-disable EC-command.
980 static inline int ssam_notifier_unregister(struct ssam_controller *ctrl,
981 struct ssam_event_notifier *n)
983 return __ssam_notifier_unregister(ctrl, n, true);
986 int ssam_controller_event_enable(struct ssam_controller *ctrl,
987 struct ssam_event_registry reg,
988 struct ssam_event_id id, u8 flags);
990 int ssam_controller_event_disable(struct ssam_controller *ctrl,
991 struct ssam_event_registry reg,
992 struct ssam_event_id id, u8 flags);
994 #endif /* _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H */