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