1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Thunderbolt driver - control channel and configuration commands
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6*4882a593Smuzhiyun * Copyright (C) 2018, Intel Corporation
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #ifndef _TB_CFG
10*4882a593Smuzhiyun #define _TB_CFG
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/kref.h>
13*4882a593Smuzhiyun #include <linux/thunderbolt.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include "nhi.h"
16*4882a593Smuzhiyun #include "tb_msgs.h"
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /* control channel */
19*4882a593Smuzhiyun struct tb_ctl;
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun typedef bool (*event_cb)(void *data, enum tb_cfg_pkg_type type,
22*4882a593Smuzhiyun const void *buf, size_t size);
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, event_cb cb, void *cb_data);
25*4882a593Smuzhiyun void tb_ctl_start(struct tb_ctl *ctl);
26*4882a593Smuzhiyun void tb_ctl_stop(struct tb_ctl *ctl);
27*4882a593Smuzhiyun void tb_ctl_free(struct tb_ctl *ctl);
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /* configuration commands */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define TB_CFG_DEFAULT_TIMEOUT 5000 /* msec */
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun struct tb_cfg_result {
34*4882a593Smuzhiyun u64 response_route;
35*4882a593Smuzhiyun u32 response_port; /*
36*4882a593Smuzhiyun * If err = 1 then this is the port that send the
37*4882a593Smuzhiyun * error.
38*4882a593Smuzhiyun * If err = 0 and if this was a cfg_read/write then
39*4882a593Smuzhiyun * this is the the upstream port of the responding
40*4882a593Smuzhiyun * switch.
41*4882a593Smuzhiyun * Otherwise the field is set to zero.
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun int err; /* negative errors, 0 for success, 1 for tb errors */
44*4882a593Smuzhiyun enum tb_cfg_error tb_error; /* valid if err == 1 */
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun struct ctl_pkg {
48*4882a593Smuzhiyun struct tb_ctl *ctl;
49*4882a593Smuzhiyun void *buffer;
50*4882a593Smuzhiyun struct ring_frame frame;
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun * struct tb_cfg_request - Control channel request
55*4882a593Smuzhiyun * @kref: Reference count
56*4882a593Smuzhiyun * @ctl: Pointer to the control channel structure. Only set when the
57*4882a593Smuzhiyun * request is queued.
58*4882a593Smuzhiyun * @request_size: Size of the request packet (in bytes)
59*4882a593Smuzhiyun * @request_type: Type of the request packet
60*4882a593Smuzhiyun * @response: Response is stored here
61*4882a593Smuzhiyun * @response_size: Maximum size of one response packet
62*4882a593Smuzhiyun * @response_type: Expected type of the response packet
63*4882a593Smuzhiyun * @npackets: Number of packets expected to be returned with this request
64*4882a593Smuzhiyun * @match: Function used to match the incoming packet
65*4882a593Smuzhiyun * @copy: Function used to copy the incoming packet to @response
66*4882a593Smuzhiyun * @callback: Callback called when the request is finished successfully
67*4882a593Smuzhiyun * @callback_data: Data to be passed to @callback
68*4882a593Smuzhiyun * @flags: Flags for the request
69*4882a593Smuzhiyun * @work: Work item used to complete the request
70*4882a593Smuzhiyun * @result: Result after the request has been completed
71*4882a593Smuzhiyun * @list: Requests are queued using this field
72*4882a593Smuzhiyun *
73*4882a593Smuzhiyun * An arbitrary request over Thunderbolt control channel. For standard
74*4882a593Smuzhiyun * control channel message, one should use tb_cfg_read/write() and
75*4882a593Smuzhiyun * friends if possible.
76*4882a593Smuzhiyun */
77*4882a593Smuzhiyun struct tb_cfg_request {
78*4882a593Smuzhiyun struct kref kref;
79*4882a593Smuzhiyun struct tb_ctl *ctl;
80*4882a593Smuzhiyun const void *request;
81*4882a593Smuzhiyun size_t request_size;
82*4882a593Smuzhiyun enum tb_cfg_pkg_type request_type;
83*4882a593Smuzhiyun void *response;
84*4882a593Smuzhiyun size_t response_size;
85*4882a593Smuzhiyun enum tb_cfg_pkg_type response_type;
86*4882a593Smuzhiyun size_t npackets;
87*4882a593Smuzhiyun bool (*match)(const struct tb_cfg_request *req,
88*4882a593Smuzhiyun const struct ctl_pkg *pkg);
89*4882a593Smuzhiyun bool (*copy)(struct tb_cfg_request *req, const struct ctl_pkg *pkg);
90*4882a593Smuzhiyun void (*callback)(void *callback_data);
91*4882a593Smuzhiyun void *callback_data;
92*4882a593Smuzhiyun unsigned long flags;
93*4882a593Smuzhiyun struct work_struct work;
94*4882a593Smuzhiyun struct tb_cfg_result result;
95*4882a593Smuzhiyun struct list_head list;
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun #define TB_CFG_REQUEST_ACTIVE 0
99*4882a593Smuzhiyun #define TB_CFG_REQUEST_CANCELED 1
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun struct tb_cfg_request *tb_cfg_request_alloc(void);
102*4882a593Smuzhiyun void tb_cfg_request_get(struct tb_cfg_request *req);
103*4882a593Smuzhiyun void tb_cfg_request_put(struct tb_cfg_request *req);
104*4882a593Smuzhiyun int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
105*4882a593Smuzhiyun void (*callback)(void *), void *callback_data);
106*4882a593Smuzhiyun void tb_cfg_request_cancel(struct tb_cfg_request *req, int err);
107*4882a593Smuzhiyun struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
108*4882a593Smuzhiyun struct tb_cfg_request *req, int timeout_msec);
109*4882a593Smuzhiyun
tb_cfg_get_route(const struct tb_cfg_header * header)110*4882a593Smuzhiyun static inline u64 tb_cfg_get_route(const struct tb_cfg_header *header)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun return (u64) header->route_hi << 32 | header->route_lo;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
tb_cfg_make_header(u64 route)115*4882a593Smuzhiyun static inline struct tb_cfg_header tb_cfg_make_header(u64 route)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun struct tb_cfg_header header = {
118*4882a593Smuzhiyun .route_hi = route >> 32,
119*4882a593Smuzhiyun .route_lo = route,
120*4882a593Smuzhiyun };
121*4882a593Smuzhiyun /* check for overflow, route_hi is not 32 bits! */
122*4882a593Smuzhiyun WARN_ON(tb_cfg_get_route(&header) != route);
123*4882a593Smuzhiyun return header;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug);
127*4882a593Smuzhiyun struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route,
128*4882a593Smuzhiyun int timeout_msec);
129*4882a593Smuzhiyun struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
130*4882a593Smuzhiyun u64 route, u32 port,
131*4882a593Smuzhiyun enum tb_cfg_space space, u32 offset,
132*4882a593Smuzhiyun u32 length, int timeout_msec);
133*4882a593Smuzhiyun struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
134*4882a593Smuzhiyun u64 route, u32 port,
135*4882a593Smuzhiyun enum tb_cfg_space space, u32 offset,
136*4882a593Smuzhiyun u32 length, int timeout_msec);
137*4882a593Smuzhiyun int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
138*4882a593Smuzhiyun enum tb_cfg_space space, u32 offset, u32 length);
139*4882a593Smuzhiyun int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
140*4882a593Smuzhiyun enum tb_cfg_space space, u32 offset, u32 length);
141*4882a593Smuzhiyun int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route);
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun #endif
145