Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Thunderbolt driver - control channel and configuration commands
0004  *
0005  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
0006  * Copyright (C) 2018, Intel Corporation
0007  */
0008 
0009 #ifndef _TB_CFG
0010 #define _TB_CFG
0011 
0012 #include <linux/kref.h>
0013 #include <linux/thunderbolt.h>
0014 
0015 #include "nhi.h"
0016 #include "tb_msgs.h"
0017 
0018 /* control channel */
0019 struct tb_ctl;
0020 
0021 typedef bool (*event_cb)(void *data, enum tb_cfg_pkg_type type,
0022              const void *buf, size_t size);
0023 
0024 struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, int timeout_msec, event_cb cb,
0025                 void *cb_data);
0026 void tb_ctl_start(struct tb_ctl *ctl);
0027 void tb_ctl_stop(struct tb_ctl *ctl);
0028 void tb_ctl_free(struct tb_ctl *ctl);
0029 
0030 /* configuration commands */
0031 
0032 struct tb_cfg_result {
0033     u64 response_route;
0034     u32 response_port; /*
0035                 * If err = 1 then this is the port that send the
0036                 * error.
0037                 * If err = 0 and if this was a cfg_read/write then
0038                 * this is the upstream port of the responding
0039                 * switch.
0040                 * Otherwise the field is set to zero.
0041                 */
0042     int err; /* negative errors, 0 for success, 1 for tb errors */
0043     enum tb_cfg_error tb_error; /* valid if err == 1 */
0044 };
0045 
0046 struct ctl_pkg {
0047     struct tb_ctl *ctl;
0048     void *buffer;
0049     struct ring_frame frame;
0050 };
0051 
0052 /**
0053  * struct tb_cfg_request - Control channel request
0054  * @kref: Reference count
0055  * @ctl: Pointer to the control channel structure. Only set when the
0056  *   request is queued.
0057  * @request_size: Size of the request packet (in bytes)
0058  * @request_type: Type of the request packet
0059  * @response: Response is stored here
0060  * @response_size: Maximum size of one response packet
0061  * @response_type: Expected type of the response packet
0062  * @npackets: Number of packets expected to be returned with this request
0063  * @match: Function used to match the incoming packet
0064  * @copy: Function used to copy the incoming packet to @response
0065  * @callback: Callback called when the request is finished successfully
0066  * @callback_data: Data to be passed to @callback
0067  * @flags: Flags for the request
0068  * @work: Work item used to complete the request
0069  * @result: Result after the request has been completed
0070  * @list: Requests are queued using this field
0071  *
0072  * An arbitrary request over Thunderbolt control channel. For standard
0073  * control channel message, one should use tb_cfg_read/write() and
0074  * friends if possible.
0075  */
0076 struct tb_cfg_request {
0077     struct kref kref;
0078     struct tb_ctl *ctl;
0079     const void *request;
0080     size_t request_size;
0081     enum tb_cfg_pkg_type request_type;
0082     void *response;
0083     size_t response_size;
0084     enum tb_cfg_pkg_type response_type;
0085     size_t npackets;
0086     bool (*match)(const struct tb_cfg_request *req,
0087               const struct ctl_pkg *pkg);
0088     bool (*copy)(struct tb_cfg_request *req, const struct ctl_pkg *pkg);
0089     void (*callback)(void *callback_data);
0090     void *callback_data;
0091     unsigned long flags;
0092     struct work_struct work;
0093     struct tb_cfg_result result;
0094     struct list_head list;
0095 };
0096 
0097 #define TB_CFG_REQUEST_ACTIVE       0
0098 #define TB_CFG_REQUEST_CANCELED     1
0099 
0100 struct tb_cfg_request *tb_cfg_request_alloc(void);
0101 void tb_cfg_request_get(struct tb_cfg_request *req);
0102 void tb_cfg_request_put(struct tb_cfg_request *req);
0103 int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
0104            void (*callback)(void *), void *callback_data);
0105 void tb_cfg_request_cancel(struct tb_cfg_request *req, int err);
0106 struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
0107             struct tb_cfg_request *req, int timeout_msec);
0108 
0109 static inline u64 tb_cfg_get_route(const struct tb_cfg_header *header)
0110 {
0111     return (u64) header->route_hi << 32 | header->route_lo;
0112 }
0113 
0114 static inline struct tb_cfg_header tb_cfg_make_header(u64 route)
0115 {
0116     struct tb_cfg_header header = {
0117         .route_hi = route >> 32,
0118         .route_lo = route,
0119     };
0120     /* check for overflow, route_hi is not 32 bits! */
0121     WARN_ON(tb_cfg_get_route(&header) != route);
0122     return header;
0123 }
0124 
0125 int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug);
0126 struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route);
0127 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
0128                      u64 route, u32 port,
0129                      enum tb_cfg_space space, u32 offset,
0130                      u32 length, int timeout_msec);
0131 struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
0132                       u64 route, u32 port,
0133                       enum tb_cfg_space space, u32 offset,
0134                       u32 length, int timeout_msec);
0135 int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
0136         enum tb_cfg_space space, u32 offset, u32 length);
0137 int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
0138          enum tb_cfg_space space, u32 offset, u32 length);
0139 int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route);
0140 
0141 
0142 #endif