Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ */
0002 /*
0003  * Surface System Aggregator Module (SSAM) controller interface.
0004  *
0005  * Main communication interface for the SSAM EC. Provides a controller
0006  * managing access and communication to and from the SSAM EC, as well as main
0007  * communication structures and definitions.
0008  *
0009  * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
0010  */
0011 
0012 #ifndef _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H
0013 #define _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H
0014 
0015 #include <linux/completion.h>
0016 #include <linux/device.h>
0017 #include <linux/types.h>
0018 
0019 #include <linux/surface_aggregator/serial_hub.h>
0020 
0021 
0022 /* -- Main data types and definitions --------------------------------------- */
0023 
0024 /**
0025  * enum ssam_event_flags - Flags for enabling/disabling SAM events
0026  * @SSAM_EVENT_SEQUENCED: The event will be sent via a sequenced data frame.
0027  */
0028 enum ssam_event_flags {
0029     SSAM_EVENT_SEQUENCED = BIT(0),
0030 };
0031 
0032 /**
0033  * struct ssam_event - SAM event sent from the EC to the host.
0034  * @target_category: Target category of the event source. See &enum ssam_ssh_tc.
0035  * @target_id:       Target ID of the event source.
0036  * @command_id:      Command ID of the event.
0037  * @instance_id:     Instance ID of the event source.
0038  * @length:          Length of the event payload in bytes.
0039  * @data:            Event payload data.
0040  */
0041 struct ssam_event {
0042     u8 target_category;
0043     u8 target_id;
0044     u8 command_id;
0045     u8 instance_id;
0046     u16 length;
0047     u8 data[];
0048 };
0049 
0050 /**
0051  * enum ssam_request_flags - Flags for SAM requests.
0052  *
0053  * @SSAM_REQUEST_HAS_RESPONSE:
0054  *  Specifies that the request expects a response. If not set, the request
0055  *  will be directly completed after its underlying packet has been
0056  *  transmitted. If set, the request transport system waits for a response
0057  *  of the request.
0058  *
0059  * @SSAM_REQUEST_UNSEQUENCED:
0060  *  Specifies that the request should be transmitted via an unsequenced
0061  *  packet. If set, the request must not have a response, meaning that this
0062  *  flag and the %SSAM_REQUEST_HAS_RESPONSE flag are mutually exclusive.
0063  */
0064 enum ssam_request_flags {
0065     SSAM_REQUEST_HAS_RESPONSE = BIT(0),
0066     SSAM_REQUEST_UNSEQUENCED  = BIT(1),
0067 };
0068 
0069 /**
0070  * struct ssam_request - SAM request description.
0071  * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
0072  * @target_id:       ID of the request's target.
0073  * @command_id:      Command ID of the request.
0074  * @instance_id:     Instance ID of the request's target.
0075  * @flags:           Flags for the request. See &enum ssam_request_flags.
0076  * @length:          Length of the request payload in bytes.
0077  * @payload:         Request payload data.
0078  *
0079  * This struct fully describes a SAM request with payload. It is intended to
0080  * help set up the actual transport struct, e.g. &struct ssam_request_sync,
0081  * and specifically its raw message data via ssam_request_write_data().
0082  */
0083 struct ssam_request {
0084     u8 target_category;
0085     u8 target_id;
0086     u8 command_id;
0087     u8 instance_id;
0088     u16 flags;
0089     u16 length;
0090     const u8 *payload;
0091 };
0092 
0093 /**
0094  * struct ssam_response - Response buffer for SAM request.
0095  * @capacity: Capacity of the buffer, in bytes.
0096  * @length:   Length of the actual data stored in the memory pointed to by
0097  *            @pointer, in bytes. Set by the transport system.
0098  * @pointer:  Pointer to the buffer's memory, storing the response payload data.
0099  */
0100 struct ssam_response {
0101     size_t capacity;
0102     size_t length;
0103     u8 *pointer;
0104 };
0105 
0106 struct ssam_controller;
0107 
0108 struct ssam_controller *ssam_get_controller(void);
0109 struct ssam_controller *ssam_client_bind(struct device *client);
0110 int ssam_client_link(struct ssam_controller *ctrl, struct device *client);
0111 
0112 struct device *ssam_controller_device(struct ssam_controller *c);
0113 
0114 struct ssam_controller *ssam_controller_get(struct ssam_controller *c);
0115 void ssam_controller_put(struct ssam_controller *c);
0116 
0117 void ssam_controller_statelock(struct ssam_controller *c);
0118 void ssam_controller_stateunlock(struct ssam_controller *c);
0119 
0120 ssize_t ssam_request_write_data(struct ssam_span *buf,
0121                 struct ssam_controller *ctrl,
0122                 const struct ssam_request *spec);
0123 
0124 
0125 /* -- Synchronous request interface. ---------------------------------------- */
0126 
0127 /**
0128  * struct ssam_request_sync - Synchronous SAM request struct.
0129  * @base:   Underlying SSH request.
0130  * @comp:   Completion used to signal full completion of the request. After the
0131  *          request has been submitted, this struct may only be modified or
0132  *          deallocated after the completion has been signaled.
0133  *          request has been submitted,
0134  * @resp:   Buffer to store the response.
0135  * @status: Status of the request, set after the base request has been
0136  *          completed or has failed.
0137  */
0138 struct ssam_request_sync {
0139     struct ssh_request base;
0140     struct completion comp;
0141     struct ssam_response *resp;
0142     int status;
0143 };
0144 
0145 int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
0146                 struct ssam_request_sync **rqst,
0147                 struct ssam_span *buffer);
0148 
0149 void ssam_request_sync_free(struct ssam_request_sync *rqst);
0150 
0151 int ssam_request_sync_init(struct ssam_request_sync *rqst,
0152                enum ssam_request_flags flags);
0153 
0154 /**
0155  * ssam_request_sync_set_data - Set message data of a synchronous request.
0156  * @rqst: The request.
0157  * @ptr:  Pointer to the request message data.
0158  * @len:  Length of the request message data.
0159  *
0160  * Set the request message data of a synchronous request. The provided buffer
0161  * needs to live until the request has been completed.
0162  */
0163 static inline void ssam_request_sync_set_data(struct ssam_request_sync *rqst,
0164                           u8 *ptr, size_t len)
0165 {
0166     ssh_request_set_data(&rqst->base, ptr, len);
0167 }
0168 
0169 /**
0170  * ssam_request_sync_set_resp - Set response buffer of a synchronous request.
0171  * @rqst: The request.
0172  * @resp: The response buffer.
0173  *
0174  * Sets the response buffer of a synchronous request. This buffer will store
0175  * the response of the request after it has been completed. May be %NULL if no
0176  * response is expected.
0177  */
0178 static inline void ssam_request_sync_set_resp(struct ssam_request_sync *rqst,
0179                           struct ssam_response *resp)
0180 {
0181     rqst->resp = resp;
0182 }
0183 
0184 int ssam_request_sync_submit(struct ssam_controller *ctrl,
0185                  struct ssam_request_sync *rqst);
0186 
0187 /**
0188  * ssam_request_sync_wait - Wait for completion of a synchronous request.
0189  * @rqst: The request to wait for.
0190  *
0191  * Wait for completion and release of a synchronous request. After this
0192  * function terminates, the request is guaranteed to have left the transport
0193  * system. After successful submission of a request, this function must be
0194  * called before accessing the response of the request, freeing the request,
0195  * or freeing any of the buffers associated with the request.
0196  *
0197  * This function must not be called if the request has not been submitted yet
0198  * and may lead to a deadlock/infinite wait if a subsequent request submission
0199  * fails in that case, due to the completion never triggering.
0200  *
0201  * Return: Returns the status of the given request, which is set on completion
0202  * of the packet. This value is zero on success and negative on failure.
0203  */
0204 static inline int ssam_request_sync_wait(struct ssam_request_sync *rqst)
0205 {
0206     wait_for_completion(&rqst->comp);
0207     return rqst->status;
0208 }
0209 
0210 int ssam_request_sync(struct ssam_controller *ctrl,
0211               const struct ssam_request *spec,
0212               struct ssam_response *rsp);
0213 
0214 int ssam_request_sync_with_buffer(struct ssam_controller *ctrl,
0215                   const struct ssam_request *spec,
0216                   struct ssam_response *rsp,
0217                   struct ssam_span *buf);
0218 
0219 /**
0220  * ssam_request_sync_onstack - Execute a synchronous request on the stack.
0221  * @ctrl: The controller via which the request is submitted.
0222  * @rqst: The request specification.
0223  * @rsp:  The response buffer.
0224  * @payload_len: The (maximum) request payload length.
0225  *
0226  * Allocates a synchronous request with specified payload length on the stack,
0227  * fully initializes it via the provided request specification, submits it,
0228  * and finally waits for its completion before returning its status. This
0229  * helper macro essentially allocates the request message buffer on the stack
0230  * and then calls ssam_request_sync_with_buffer().
0231  *
0232  * Note: The @payload_len parameter specifies the maximum payload length, used
0233  * for buffer allocation. The actual payload length may be smaller.
0234  *
0235  * Return: Returns the status of the request or any failure during setup, i.e.
0236  * zero on success and a negative value on failure.
0237  */
0238 #define ssam_request_sync_onstack(ctrl, rqst, rsp, payload_len)         \
0239     ({                                  \
0240         u8 __data[SSH_COMMAND_MESSAGE_LENGTH(payload_len)];     \
0241         struct ssam_span __buf = { &__data[0], ARRAY_SIZE(__data) };    \
0242                                         \
0243         ssam_request_sync_with_buffer(ctrl, rqst, rsp, &__buf);     \
0244     })
0245 
0246 /**
0247  * __ssam_retry - Retry request in case of I/O errors or timeouts.
0248  * @request: The request function to execute. Must return an integer.
0249  * @n:       Number of tries.
0250  * @args:    Arguments for the request function.
0251  *
0252  * Executes the given request function, i.e. calls @request. In case the
0253  * request returns %-EREMOTEIO (indicates I/O error) or %-ETIMEDOUT (request
0254  * or underlying packet timed out), @request will be re-executed again, up to
0255  * @n times in total.
0256  *
0257  * Return: Returns the return value of the last execution of @request.
0258  */
0259 #define __ssam_retry(request, n, args...)               \
0260     ({                              \
0261         int __i, __s = 0;                   \
0262                                     \
0263         for (__i = (n); __i > 0; __i--) {           \
0264             __s = request(args);                \
0265             if (__s != -ETIMEDOUT && __s != -EREMOTEIO) \
0266                 break;                  \
0267         }                           \
0268         __s;                            \
0269     })
0270 
0271 /**
0272  * ssam_retry - Retry request in case of I/O errors or timeouts up to three
0273  * times in total.
0274  * @request: The request function to execute. Must return an integer.
0275  * @args:    Arguments for the request function.
0276  *
0277  * Executes the given request function, i.e. calls @request. In case the
0278  * request returns %-EREMOTEIO (indicates I/O error) or -%ETIMEDOUT (request
0279  * or underlying packet timed out), @request will be re-executed again, up to
0280  * three times in total.
0281  *
0282  * See __ssam_retry() for a more generic macro for this purpose.
0283  *
0284  * Return: Returns the return value of the last execution of @request.
0285  */
0286 #define ssam_retry(request, args...) \
0287     __ssam_retry(request, 3, args)
0288 
0289 /**
0290  * struct ssam_request_spec - Blue-print specification of SAM request.
0291  * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
0292  * @target_id:       ID of the request's target.
0293  * @command_id:      Command ID of the request.
0294  * @instance_id:     Instance ID of the request's target.
0295  * @flags:           Flags for the request. See &enum ssam_request_flags.
0296  *
0297  * Blue-print specification for a SAM request. This struct describes the
0298  * unique static parameters of a request (i.e. type) without specifying any of
0299  * its instance-specific data (e.g. payload). It is intended to be used as base
0300  * for defining simple request functions via the
0301  * ``SSAM_DEFINE_SYNC_REQUEST_x()`` family of macros.
0302  */
0303 struct ssam_request_spec {
0304     u8 target_category;
0305     u8 target_id;
0306     u8 command_id;
0307     u8 instance_id;
0308     u8 flags;
0309 };
0310 
0311 /**
0312  * struct ssam_request_spec_md - Blue-print specification for multi-device SAM
0313  * request.
0314  * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
0315  * @command_id:      Command ID of the request.
0316  * @flags:           Flags for the request. See &enum ssam_request_flags.
0317  *
0318  * Blue-print specification for a multi-device SAM request, i.e. a request
0319  * that is applicable to multiple device instances, described by their
0320  * individual target and instance IDs. This struct describes the unique static
0321  * parameters of a request (i.e. type) without specifying any of its
0322  * instance-specific data (e.g. payload) and without specifying any of its
0323  * device specific IDs (i.e. target and instance ID). It is intended to be
0324  * used as base for defining simple multi-device request functions via the
0325  * ``SSAM_DEFINE_SYNC_REQUEST_MD_x()`` and ``SSAM_DEFINE_SYNC_REQUEST_CL_x()``
0326  * families of macros.
0327  */
0328 struct ssam_request_spec_md {
0329     u8 target_category;
0330     u8 command_id;
0331     u8 flags;
0332 };
0333 
0334 /**
0335  * SSAM_DEFINE_SYNC_REQUEST_N() - Define synchronous SAM request function
0336  * with neither argument nor return value.
0337  * @name: Name of the generated function.
0338  * @spec: Specification (&struct ssam_request_spec) defining the request.
0339  *
0340  * Defines a function executing the synchronous SAM request specified by
0341  * @spec, with the request having neither argument nor return value. The
0342  * generated function takes care of setting up the request struct and buffer
0343  * allocation, as well as execution of the request itself, returning once the
0344  * request has been fully completed. The required transport buffer will be
0345  * allocated on the stack.
0346  *
0347  * The generated function is defined as ``static int name(struct
0348  * ssam_controller *ctrl)``, returning the status of the request, which is
0349  * zero on success and negative on failure. The ``ctrl`` parameter is the
0350  * controller via which the request is being sent.
0351  *
0352  * Refer to ssam_request_sync_onstack() for more details on the behavior of
0353  * the generated function.
0354  */
0355 #define SSAM_DEFINE_SYNC_REQUEST_N(name, spec...)               \
0356     static int name(struct ssam_controller *ctrl)               \
0357     {                                   \
0358         struct ssam_request_spec s = (struct ssam_request_spec)spec;    \
0359         struct ssam_request rqst;                   \
0360                                         \
0361         rqst.target_category = s.target_category;           \
0362         rqst.target_id = s.target_id;                   \
0363         rqst.command_id = s.command_id;                 \
0364         rqst.instance_id = s.instance_id;               \
0365         rqst.flags = s.flags;                       \
0366         rqst.length = 0;                        \
0367         rqst.payload = NULL;                        \
0368                                         \
0369         return ssam_request_sync_onstack(ctrl, &rqst, NULL, 0);     \
0370     }
0371 
0372 /**
0373  * SSAM_DEFINE_SYNC_REQUEST_W() - Define synchronous SAM request function with
0374  * argument.
0375  * @name:  Name of the generated function.
0376  * @atype: Type of the request's argument.
0377  * @spec:  Specification (&struct ssam_request_spec) defining the request.
0378  *
0379  * Defines a function executing the synchronous SAM request specified by
0380  * @spec, with the request taking an argument of type @atype and having no
0381  * return value. The generated function takes care of setting up the request
0382  * struct, buffer allocation, as well as execution of the request itself,
0383  * returning once the request has been fully completed. The required transport
0384  * buffer will be allocated on the stack.
0385  *
0386  * The generated function is defined as ``static int name(struct
0387  * ssam_controller *ctrl, const atype *arg)``, returning the status of the
0388  * request, which is zero on success and negative on failure. The ``ctrl``
0389  * parameter is the controller via which the request is sent. The request
0390  * argument is specified via the ``arg`` pointer.
0391  *
0392  * Refer to ssam_request_sync_onstack() for more details on the behavior of
0393  * the generated function.
0394  */
0395 #define SSAM_DEFINE_SYNC_REQUEST_W(name, atype, spec...)            \
0396     static int name(struct ssam_controller *ctrl, const atype *arg)     \
0397     {                                   \
0398         struct ssam_request_spec s = (struct ssam_request_spec)spec;    \
0399         struct ssam_request rqst;                   \
0400                                         \
0401         rqst.target_category = s.target_category;           \
0402         rqst.target_id = s.target_id;                   \
0403         rqst.command_id = s.command_id;                 \
0404         rqst.instance_id = s.instance_id;               \
0405         rqst.flags = s.flags;                       \
0406         rqst.length = sizeof(atype);                    \
0407         rqst.payload = (u8 *)arg;                   \
0408                                         \
0409         return ssam_request_sync_onstack(ctrl, &rqst, NULL,     \
0410                          sizeof(atype));        \
0411     }
0412 
0413 /**
0414  * SSAM_DEFINE_SYNC_REQUEST_R() - Define synchronous SAM request function with
0415  * return value.
0416  * @name:  Name of the generated function.
0417  * @rtype: Type of the request's return value.
0418  * @spec:  Specification (&struct ssam_request_spec) defining the request.
0419  *
0420  * Defines a function executing the synchronous SAM request specified by
0421  * @spec, with the request taking no argument but having a return value of
0422  * type @rtype. The generated function takes care of setting up the request
0423  * and response structs, buffer allocation, as well as execution of the
0424  * request itself, returning once the request has been fully completed. The
0425  * required transport buffer will be allocated on the stack.
0426  *
0427  * The generated function is defined as ``static int name(struct
0428  * ssam_controller *ctrl, rtype *ret)``, returning the status of the request,
0429  * which is zero on success and negative on failure. The ``ctrl`` parameter is
0430  * the controller via which the request is sent. The request's return value is
0431  * written to the memory pointed to by the ``ret`` parameter.
0432  *
0433  * Refer to ssam_request_sync_onstack() for more details on the behavior of
0434  * the generated function.
0435  */
0436 #define SSAM_DEFINE_SYNC_REQUEST_R(name, rtype, spec...)            \
0437     static int name(struct ssam_controller *ctrl, rtype *ret)       \
0438     {                                   \
0439         struct ssam_request_spec s = (struct ssam_request_spec)spec;    \
0440         struct ssam_request rqst;                   \
0441         struct ssam_response rsp;                   \
0442         int status;                         \
0443                                         \
0444         rqst.target_category = s.target_category;           \
0445         rqst.target_id = s.target_id;                   \
0446         rqst.command_id = s.command_id;                 \
0447         rqst.instance_id = s.instance_id;               \
0448         rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE;       \
0449         rqst.length = 0;                        \
0450         rqst.payload = NULL;                        \
0451                                         \
0452         rsp.capacity = sizeof(rtype);                   \
0453         rsp.length = 0;                         \
0454         rsp.pointer = (u8 *)ret;                    \
0455                                         \
0456         status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, 0);   \
0457         if (status)                         \
0458             return status;                      \
0459                                         \
0460         if (rsp.length != sizeof(rtype)) {              \
0461             struct device *dev = ssam_controller_device(ctrl);  \
0462             dev_err(dev,                        \
0463                 "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
0464                 sizeof(rtype), rsp.length, rqst.target_category,\
0465                 rqst.command_id);               \
0466             return -EIO;                        \
0467         }                               \
0468                                         \
0469         return 0;                           \
0470     }
0471 
0472 /**
0473  * SSAM_DEFINE_SYNC_REQUEST_WR() - Define synchronous SAM request function with
0474  * both argument and return value.
0475  * @name:  Name of the generated function.
0476  * @atype: Type of the request's argument.
0477  * @rtype: Type of the request's return value.
0478  * @spec:  Specification (&struct ssam_request_spec) defining the request.
0479  *
0480  * Defines a function executing the synchronous SAM request specified by @spec,
0481  * with the request taking an argument of type @atype and having a return value
0482  * of type @rtype. The generated function takes care of setting up the request
0483  * and response structs, buffer allocation, as well as execution of the request
0484  * itself, returning once the request has been fully completed. The required
0485  * transport buffer will be allocated on the stack.
0486  *
0487  * The generated function is defined as ``static int name(struct
0488  * ssam_controller *ctrl, const atype *arg, rtype *ret)``, returning the status
0489  * of the request, which is zero on success and negative on failure. The
0490  * ``ctrl`` parameter is the controller via which the request is sent. The
0491  * request argument is specified via the ``arg`` pointer. The request's return
0492  * value is written to the memory pointed to by the ``ret`` parameter.
0493  *
0494  * Refer to ssam_request_sync_onstack() for more details on the behavior of
0495  * the generated function.
0496  */
0497 #define SSAM_DEFINE_SYNC_REQUEST_WR(name, atype, rtype, spec...)        \
0498     static int name(struct ssam_controller *ctrl, const atype *arg, rtype *ret) \
0499     {                                   \
0500         struct ssam_request_spec s = (struct ssam_request_spec)spec;    \
0501         struct ssam_request rqst;                   \
0502         struct ssam_response rsp;                   \
0503         int status;                         \
0504                                         \
0505         rqst.target_category = s.target_category;           \
0506         rqst.target_id = s.target_id;                   \
0507         rqst.command_id = s.command_id;                 \
0508         rqst.instance_id = s.instance_id;               \
0509         rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE;       \
0510         rqst.length = sizeof(atype);                    \
0511         rqst.payload = (u8 *)arg;                   \
0512                                         \
0513         rsp.capacity = sizeof(rtype);                   \
0514         rsp.length = 0;                         \
0515         rsp.pointer = (u8 *)ret;                    \
0516                                         \
0517         status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, sizeof(atype)); \
0518         if (status)                         \
0519             return status;                      \
0520                                         \
0521         if (rsp.length != sizeof(rtype)) {              \
0522             struct device *dev = ssam_controller_device(ctrl);  \
0523             dev_err(dev,                        \
0524                 "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
0525                 sizeof(rtype), rsp.length, rqst.target_category,\
0526                 rqst.command_id);               \
0527             return -EIO;                        \
0528         }                               \
0529                                         \
0530         return 0;                           \
0531     }
0532 
0533 /**
0534  * SSAM_DEFINE_SYNC_REQUEST_MD_N() - Define synchronous multi-device SAM
0535  * request function with neither argument nor return value.
0536  * @name: Name of the generated function.
0537  * @spec: Specification (&struct ssam_request_spec_md) defining the request.
0538  *
0539  * Defines a function executing the synchronous SAM request specified by
0540  * @spec, with the request having neither argument nor return value. Device
0541  * specifying parameters are not hard-coded, but instead must be provided to
0542  * the function. The generated function takes care of setting up the request
0543  * struct, buffer allocation, as well as execution of the request itself,
0544  * returning once the request has been fully completed. The required transport
0545  * buffer will be allocated on the stack.
0546  *
0547  * The generated function is defined as ``static int name(struct
0548  * ssam_controller *ctrl, u8 tid, u8 iid)``, returning the status of the
0549  * request, which is zero on success and negative on failure. The ``ctrl``
0550  * parameter is the controller via which the request is sent, ``tid`` the
0551  * target ID for the request, and ``iid`` the instance ID.
0552  *
0553  * Refer to ssam_request_sync_onstack() for more details on the behavior of
0554  * the generated function.
0555  */
0556 #define SSAM_DEFINE_SYNC_REQUEST_MD_N(name, spec...)                \
0557     static int name(struct ssam_controller *ctrl, u8 tid, u8 iid)       \
0558     {                                   \
0559         struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
0560         struct ssam_request rqst;                   \
0561                                         \
0562         rqst.target_category = s.target_category;           \
0563         rqst.target_id = tid;                       \
0564         rqst.command_id = s.command_id;                 \
0565         rqst.instance_id = iid;                     \
0566         rqst.flags = s.flags;                       \
0567         rqst.length = 0;                        \
0568         rqst.payload = NULL;                        \
0569                                         \
0570         return ssam_request_sync_onstack(ctrl, &rqst, NULL, 0);     \
0571     }
0572 
0573 /**
0574  * SSAM_DEFINE_SYNC_REQUEST_MD_W() - Define synchronous multi-device SAM
0575  * request function with argument.
0576  * @name:  Name of the generated function.
0577  * @atype: Type of the request's argument.
0578  * @spec:  Specification (&struct ssam_request_spec_md) defining the request.
0579  *
0580  * Defines a function executing the synchronous SAM request specified by
0581  * @spec, with the request taking an argument of type @atype and having no
0582  * return value. Device specifying parameters are not hard-coded, but instead
0583  * must be provided to the function. The generated function takes care of
0584  * setting up the request struct, buffer allocation, as well as execution of
0585  * the request itself, returning once the request has been fully completed.
0586  * The required transport buffer will be allocated on the stack.
0587  *
0588  * The generated function is defined as ``static int name(struct
0589  * ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg)``, returning the
0590  * status of the request, which is zero on success and negative on failure.
0591  * The ``ctrl`` parameter is the controller via which the request is sent,
0592  * ``tid`` the target ID for the request, and ``iid`` the instance ID. The
0593  * request argument is specified via the ``arg`` pointer.
0594  *
0595  * Refer to ssam_request_sync_onstack() for more details on the behavior of
0596  * the generated function.
0597  */
0598 #define SSAM_DEFINE_SYNC_REQUEST_MD_W(name, atype, spec...)         \
0599     static int name(struct ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg) \
0600     {                                   \
0601         struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
0602         struct ssam_request rqst;                   \
0603                                         \
0604         rqst.target_category = s.target_category;           \
0605         rqst.target_id = tid;                       \
0606         rqst.command_id = s.command_id;                 \
0607         rqst.instance_id = iid;                     \
0608         rqst.flags = s.flags;                       \
0609         rqst.length = sizeof(atype);                    \
0610         rqst.payload = (u8 *)arg;                   \
0611                                         \
0612         return ssam_request_sync_onstack(ctrl, &rqst, NULL,     \
0613                          sizeof(atype));        \
0614     }
0615 
0616 /**
0617  * SSAM_DEFINE_SYNC_REQUEST_MD_R() - Define synchronous multi-device SAM
0618  * request function with return value.
0619  * @name:  Name of the generated function.
0620  * @rtype: Type of the request's return value.
0621  * @spec:  Specification (&struct ssam_request_spec_md) defining the request.
0622  *
0623  * Defines a function executing the synchronous SAM request specified by
0624  * @spec, with the request taking no argument but having a return value of
0625  * type @rtype. Device specifying parameters are not hard-coded, but instead
0626  * must be provided to the function. The generated function takes care of
0627  * setting up the request and response structs, buffer allocation, as well as
0628  * execution of the request itself, returning once the request has been fully
0629  * completed. The required transport buffer will be allocated on the stack.
0630  *
0631  * The generated function is defined as ``static int name(struct
0632  * ssam_controller *ctrl, u8 tid, u8 iid, rtype *ret)``, returning the status
0633  * of the request, which is zero on success and negative on failure. The
0634  * ``ctrl`` parameter is the controller via which the request is sent, ``tid``
0635  * the target ID for the request, and ``iid`` the instance ID. The request's
0636  * return value is written to the memory pointed to by the ``ret`` parameter.
0637  *
0638  * Refer to ssam_request_sync_onstack() for more details on the behavior of
0639  * the generated function.
0640  */
0641 #define SSAM_DEFINE_SYNC_REQUEST_MD_R(name, rtype, spec...)         \
0642     static int name(struct ssam_controller *ctrl, u8 tid, u8 iid, rtype *ret) \
0643     {                                   \
0644         struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
0645         struct ssam_request rqst;                   \
0646         struct ssam_response rsp;                   \
0647         int status;                         \
0648                                         \
0649         rqst.target_category = s.target_category;           \
0650         rqst.target_id = tid;                       \
0651         rqst.command_id = s.command_id;                 \
0652         rqst.instance_id = iid;                     \
0653         rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE;       \
0654         rqst.length = 0;                        \
0655         rqst.payload = NULL;                        \
0656                                         \
0657         rsp.capacity = sizeof(rtype);                   \
0658         rsp.length = 0;                         \
0659         rsp.pointer = (u8 *)ret;                    \
0660                                         \
0661         status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, 0);   \
0662         if (status)                         \
0663             return status;                      \
0664                                         \
0665         if (rsp.length != sizeof(rtype)) {              \
0666             struct device *dev = ssam_controller_device(ctrl);  \
0667             dev_err(dev,                        \
0668                 "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
0669                 sizeof(rtype), rsp.length, rqst.target_category,\
0670                 rqst.command_id);               \
0671             return -EIO;                        \
0672         }                               \
0673                                         \
0674         return 0;                           \
0675     }
0676 
0677 /**
0678  * SSAM_DEFINE_SYNC_REQUEST_MD_WR() - Define synchronous multi-device SAM
0679  * request function with both argument and return value.
0680  * @name:  Name of the generated function.
0681  * @atype: Type of the request's argument.
0682  * @rtype: Type of the request's return value.
0683  * @spec:  Specification (&struct ssam_request_spec_md) defining the request.
0684  *
0685  * Defines a function executing the synchronous SAM request specified by @spec,
0686  * with the request taking an argument of type @atype and having a return value
0687  * of type @rtype. Device specifying parameters are not hard-coded, but instead
0688  * must be provided to the function. The generated function takes care of
0689  * setting up the request and response structs, buffer allocation, as well as
0690  * execution of the request itself, returning once the request has been fully
0691  * completed. The required transport buffer will be allocated on the stack.
0692  *
0693  * The generated function is defined as ``static int name(struct
0694  * ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg, rtype *ret)``,
0695  * returning the status of the request, which is zero on success and negative
0696  * on failure. The ``ctrl`` parameter is the controller via which the request
0697  * is sent, ``tid`` the target ID for the request, and ``iid`` the instance ID.
0698  * The request argument is specified via the ``arg`` pointer. The request's
0699  * return value is written to the memory pointed to by the ``ret`` parameter.
0700  *
0701  * Refer to ssam_request_sync_onstack() for more details on the behavior of
0702  * the generated function.
0703  */
0704 #define SSAM_DEFINE_SYNC_REQUEST_MD_WR(name, atype, rtype, spec...)     \
0705     static int name(struct ssam_controller *ctrl, u8 tid, u8 iid,       \
0706             const atype *arg, rtype *ret)               \
0707     {                                   \
0708         struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
0709         struct ssam_request rqst;                   \
0710         struct ssam_response rsp;                   \
0711         int status;                         \
0712                                         \
0713         rqst.target_category = s.target_category;           \
0714         rqst.target_id = tid;                       \
0715         rqst.command_id = s.command_id;                 \
0716         rqst.instance_id = iid;                     \
0717         rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE;       \
0718         rqst.length = sizeof(atype);                    \
0719         rqst.payload = (u8 *)arg;                   \
0720                                         \
0721         rsp.capacity = sizeof(rtype);                   \
0722         rsp.length = 0;                         \
0723         rsp.pointer = (u8 *)ret;                    \
0724                                         \
0725         status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, sizeof(atype)); \
0726         if (status)                         \
0727             return status;                      \
0728                                         \
0729         if (rsp.length != sizeof(rtype)) {              \
0730             struct device *dev = ssam_controller_device(ctrl);  \
0731             dev_err(dev,                        \
0732                 "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
0733                 sizeof(rtype), rsp.length, rqst.target_category,\
0734                 rqst.command_id);               \
0735             return -EIO;                        \
0736         }                               \
0737                                         \
0738         return 0;                           \
0739     }
0740 
0741 
0742 /* -- Event notifier/callbacks. --------------------------------------------- */
0743 
0744 #define SSAM_NOTIF_STATE_SHIFT      2
0745 #define SSAM_NOTIF_STATE_MASK       ((1 << SSAM_NOTIF_STATE_SHIFT) - 1)
0746 
0747 /**
0748  * enum ssam_notif_flags - Flags used in return values from SSAM notifier
0749  * callback functions.
0750  *
0751  * @SSAM_NOTIF_HANDLED:
0752  *  Indicates that the notification has been handled. This flag should be
0753  *  set by the handler if the handler can act/has acted upon the event
0754  *  provided to it. This flag should not be set if the handler is not a
0755  *  primary handler intended for the provided event.
0756  *
0757  *  If this flag has not been set by any handler after the notifier chain
0758  *  has been traversed, a warning will be emitted, stating that the event
0759  *  has not been handled.
0760  *
0761  * @SSAM_NOTIF_STOP:
0762  *  Indicates that the notifier traversal should stop. If this flag is
0763  *  returned from a notifier callback, notifier chain traversal will
0764  *  immediately stop and any remaining notifiers will not be called. This
0765  *  flag is automatically set when ssam_notifier_from_errno() is called
0766  *  with a negative error value.
0767  */
0768 enum ssam_notif_flags {
0769     SSAM_NOTIF_HANDLED = BIT(0),
0770     SSAM_NOTIF_STOP    = BIT(1),
0771 };
0772 
0773 struct ssam_event_notifier;
0774 
0775 typedef u32 (*ssam_notifier_fn_t)(struct ssam_event_notifier *nf,
0776                   const struct ssam_event *event);
0777 
0778 /**
0779  * struct ssam_notifier_block - Base notifier block for SSAM event
0780  * notifications.
0781  * @node:     The node for the list of notifiers.
0782  * @fn:       The callback function of this notifier. This function takes the
0783  *            respective notifier block and event as input and should return
0784  *            a notifier value, which can either be obtained from the flags
0785  *            provided in &enum ssam_notif_flags, converted from a standard
0786  *            error value via ssam_notifier_from_errno(), or a combination of
0787  *            both (e.g. ``ssam_notifier_from_errno(e) | SSAM_NOTIF_HANDLED``).
0788  * @priority: Priority value determining the order in which notifier callbacks
0789  *            will be called. A higher value means higher priority, i.e. the
0790  *            associated callback will be executed earlier than other (lower
0791  *            priority) callbacks.
0792  */
0793 struct ssam_notifier_block {
0794     struct list_head node;
0795     ssam_notifier_fn_t fn;
0796     int priority;
0797 };
0798 
0799 /**
0800  * ssam_notifier_from_errno() - Convert standard error value to notifier
0801  * return code.
0802  * @err: The error code to convert, must be negative (in case of failure) or
0803  *       zero (in case of success).
0804  *
0805  * Return: Returns the notifier return value obtained by converting the
0806  * specified @err value. In case @err is negative, the %SSAM_NOTIF_STOP flag
0807  * will be set, causing notifier call chain traversal to abort.
0808  */
0809 static inline u32 ssam_notifier_from_errno(int err)
0810 {
0811     if (WARN_ON(err > 0) || err == 0)
0812         return 0;
0813     else
0814         return ((-err) << SSAM_NOTIF_STATE_SHIFT) | SSAM_NOTIF_STOP;
0815 }
0816 
0817 /**
0818  * ssam_notifier_to_errno() - Convert notifier return code to standard error
0819  * value.
0820  * @ret: The notifier return value to convert.
0821  *
0822  * Return: Returns the negative error value encoded in @ret or zero if @ret
0823  * indicates success.
0824  */
0825 static inline int ssam_notifier_to_errno(u32 ret)
0826 {
0827     return -(ret >> SSAM_NOTIF_STATE_SHIFT);
0828 }
0829 
0830 
0831 /* -- Event/notification registry. ------------------------------------------ */
0832 
0833 /**
0834  * struct ssam_event_registry - Registry specification used for enabling events.
0835  * @target_category: Target category for the event registry requests.
0836  * @target_id:       Target ID for the event registry requests.
0837  * @cid_enable:      Command ID for the event-enable request.
0838  * @cid_disable:     Command ID for the event-disable request.
0839  *
0840  * This struct describes a SAM event registry via the minimal collection of
0841  * SAM IDs specifying the requests to use for enabling and disabling an event.
0842  * The individual event to be enabled/disabled itself is specified via &struct
0843  * ssam_event_id.
0844  */
0845 struct ssam_event_registry {
0846     u8 target_category;
0847     u8 target_id;
0848     u8 cid_enable;
0849     u8 cid_disable;
0850 };
0851 
0852 /**
0853  * struct ssam_event_id - Unique event ID used for enabling events.
0854  * @target_category: Target category of the event source.
0855  * @instance:        Instance ID of the event source.
0856  *
0857  * This struct specifies the event to be enabled/disabled via an externally
0858  * provided registry. It does not specify the registry to be used itself, this
0859  * is done via &struct ssam_event_registry.
0860  */
0861 struct ssam_event_id {
0862     u8 target_category;
0863     u8 instance;
0864 };
0865 
0866 /**
0867  * enum ssam_event_mask - Flags specifying how events are matched to notifiers.
0868  *
0869  * @SSAM_EVENT_MASK_NONE:
0870  *  Run the callback for any event with matching target category. Do not
0871  *  do any additional filtering.
0872  *
0873  * @SSAM_EVENT_MASK_TARGET:
0874  *  In addition to filtering by target category, only execute the notifier
0875  *  callback for events with a target ID matching to the one of the
0876  *  registry used for enabling/disabling the event.
0877  *
0878  * @SSAM_EVENT_MASK_INSTANCE:
0879  *  In addition to filtering by target category, only execute the notifier
0880  *  callback for events with an instance ID matching to the instance ID
0881  *  used when enabling the event.
0882  *
0883  * @SSAM_EVENT_MASK_STRICT:
0884  *  Do all the filtering above.
0885  */
0886 enum ssam_event_mask {
0887     SSAM_EVENT_MASK_TARGET   = BIT(0),
0888     SSAM_EVENT_MASK_INSTANCE = BIT(1),
0889 
0890     SSAM_EVENT_MASK_NONE = 0,
0891     SSAM_EVENT_MASK_STRICT =
0892           SSAM_EVENT_MASK_TARGET
0893         | SSAM_EVENT_MASK_INSTANCE,
0894 };
0895 
0896 /**
0897  * SSAM_EVENT_REGISTRY() - Define a new event registry.
0898  * @tc:      Target category for the event registry requests.
0899  * @tid:     Target ID for the event registry requests.
0900  * @cid_en:  Command ID for the event-enable request.
0901  * @cid_dis: Command ID for the event-disable request.
0902  *
0903  * Return: Returns the &struct ssam_event_registry specified by the given
0904  * parameters.
0905  */
0906 #define SSAM_EVENT_REGISTRY(tc, tid, cid_en, cid_dis)   \
0907     ((struct ssam_event_registry) {         \
0908         .target_category = (tc),        \
0909         .target_id = (tid),         \
0910         .cid_enable = (cid_en),         \
0911         .cid_disable = (cid_dis),       \
0912     })
0913 
0914 #define SSAM_EVENT_REGISTRY_SAM \
0915     SSAM_EVENT_REGISTRY(SSAM_SSH_TC_SAM, 0x01, 0x0b, 0x0c)
0916 
0917 #define SSAM_EVENT_REGISTRY_KIP \
0918     SSAM_EVENT_REGISTRY(SSAM_SSH_TC_KIP, 0x02, 0x27, 0x28)
0919 
0920 #define SSAM_EVENT_REGISTRY_REG(tid)\
0921     SSAM_EVENT_REGISTRY(SSAM_SSH_TC_REG, tid, 0x01, 0x02)
0922 
0923 /**
0924  * enum ssam_event_notifier_flags - Flags for event notifiers.
0925  * @SSAM_EVENT_NOTIFIER_OBSERVER:
0926  *  The corresponding notifier acts as observer. Registering a notifier
0927  *  with this flag set will not attempt to enable any event. Equally,
0928  *  unregistering will not attempt to disable any event. Note that a
0929  *  notifier with this flag may not even correspond to a certain event at
0930  *  all, only to a specific event target category. Event matching will not
0931  *  be influenced by this flag.
0932  */
0933 enum ssam_event_notifier_flags {
0934     SSAM_EVENT_NOTIFIER_OBSERVER = BIT(0),
0935 };
0936 
0937 /**
0938  * struct ssam_event_notifier - Notifier block for SSAM events.
0939  * @base:        The base notifier block with callback function and priority.
0940  * @event:       The event for which this block will receive notifications.
0941  * @event.reg:   Registry via which the event will be enabled/disabled.
0942  * @event.id:    ID specifying the event.
0943  * @event.mask:  Flags determining how events are matched to the notifier.
0944  * @event.flags: Flags used for enabling the event.
0945  * @flags:       Notifier flags (see &enum ssam_event_notifier_flags).
0946  */
0947 struct ssam_event_notifier {
0948     struct ssam_notifier_block base;
0949 
0950     struct {
0951         struct ssam_event_registry reg;
0952         struct ssam_event_id id;
0953         enum ssam_event_mask mask;
0954         u8 flags;
0955     } event;
0956 
0957     unsigned long flags;
0958 };
0959 
0960 int ssam_notifier_register(struct ssam_controller *ctrl,
0961                struct ssam_event_notifier *n);
0962 
0963 int __ssam_notifier_unregister(struct ssam_controller *ctrl,
0964                    struct ssam_event_notifier *n, bool disable);
0965 
0966 /**
0967  * ssam_notifier_unregister() - Unregister an event notifier.
0968  * @ctrl:    The controller the notifier has been registered on.
0969  * @n:       The event notifier to unregister.
0970  *
0971  * Unregister an event notifier. Decrement the usage counter of the associated
0972  * SAM event if the notifier is not marked as an observer. If the usage counter
0973  * reaches zero, the event will be disabled.
0974  *
0975  * Return: Returns zero on success, %-ENOENT if the given notifier block has
0976  * not been registered on the controller. If the given notifier block was the
0977  * last one associated with its specific event, returns the status of the
0978  * event-disable EC-command.
0979  */
0980 static inline int ssam_notifier_unregister(struct ssam_controller *ctrl,
0981                        struct ssam_event_notifier *n)
0982 {
0983     return __ssam_notifier_unregister(ctrl, n, true);
0984 }
0985 
0986 int ssam_controller_event_enable(struct ssam_controller *ctrl,
0987                  struct ssam_event_registry reg,
0988                  struct ssam_event_id id, u8 flags);
0989 
0990 int ssam_controller_event_disable(struct ssam_controller *ctrl,
0991                   struct ssam_event_registry reg,
0992                   struct ssam_event_id id, u8 flags);
0993 
0994 #endif /* _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H */