1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Thunderbolt Cactus Ridge driver - control channel and configuration commands
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
11 #include <linux/kref.h>
19 typedef void (*event_cb
)(void *data
, enum tb_cfg_pkg_type type
,
20 const void *buf
, size_t size
);
22 struct tb_ctl
*tb_ctl_alloc(struct tb_nhi
*nhi
, event_cb cb
, void *cb_data
);
23 void tb_ctl_start(struct tb_ctl
*ctl
);
24 void tb_ctl_stop(struct tb_ctl
*ctl
);
25 void tb_ctl_free(struct tb_ctl
*ctl
);
27 /* configuration commands */
29 #define TB_CFG_DEFAULT_TIMEOUT 5000 /* msec */
31 struct tb_cfg_result
{
34 * If err = 1 then this is the port that send the
36 * If err = 0 and if this was a cfg_read/write then
37 * this is the the upstream port of the responding
39 * Otherwise the field is set to zero.
41 int err
; /* negative errors, 0 for success, 1 for tb errors */
42 enum tb_cfg_error tb_error
; /* valid if err == 1 */
48 struct ring_frame frame
;
52 * struct tb_cfg_request - Control channel request
53 * @kref: Reference count
54 * @ctl: Pointer to the control channel structure. Only set when the
56 * @request_size: Size of the request packet (in bytes)
57 * @request_type: Type of the request packet
58 * @response: Response is stored here
59 * @response_size: Maximum size of one response packet
60 * @response_type: Expected type of the response packet
61 * @npackets: Number of packets expected to be returned with this request
62 * @match: Function used to match the incoming packet
63 * @copy: Function used to copy the incoming packet to @response
64 * @callback: Callback called when the request is finished successfully
65 * @callback_data: Data to be passed to @callback
66 * @flags: Flags for the request
67 * @work: Work item used to complete the request
68 * @result: Result after the request has been completed
69 * @list: Requests are queued using this field
71 * An arbitrary request over Thunderbolt control channel. For standard
72 * control channel message, one should use tb_cfg_read/write() and
73 * friends if possible.
75 struct tb_cfg_request
{
80 enum tb_cfg_pkg_type request_type
;
83 enum tb_cfg_pkg_type response_type
;
85 bool (*match
)(const struct tb_cfg_request
*req
,
86 const struct ctl_pkg
*pkg
);
87 bool (*copy
)(struct tb_cfg_request
*req
, const struct ctl_pkg
*pkg
);
88 void (*callback
)(void *callback_data
);
91 struct work_struct work
;
92 struct tb_cfg_result result
;
93 struct list_head list
;
96 #define TB_CFG_REQUEST_ACTIVE 0
97 #define TB_CFG_REQUEST_CANCELED 1
99 struct tb_cfg_request
*tb_cfg_request_alloc(void);
100 void tb_cfg_request_get(struct tb_cfg_request
*req
);
101 void tb_cfg_request_put(struct tb_cfg_request
*req
);
102 int tb_cfg_request(struct tb_ctl
*ctl
, struct tb_cfg_request
*req
,
103 void (*callback
)(void *), void *callback_data
);
104 void tb_cfg_request_cancel(struct tb_cfg_request
*req
, int err
);
105 struct tb_cfg_result
tb_cfg_request_sync(struct tb_ctl
*ctl
,
106 struct tb_cfg_request
*req
, int timeout_msec
);
108 static inline u64
tb_cfg_get_route(const struct tb_cfg_header
*header
)
110 return (u64
) header
->route_hi
<< 32 | header
->route_lo
;
113 static inline struct tb_cfg_header
tb_cfg_make_header(u64 route
)
115 struct tb_cfg_header header
= {
116 .route_hi
= route
>> 32,
119 /* check for overflow, route_hi is not 32 bits! */
120 WARN_ON(tb_cfg_get_route(&header
) != route
);
124 int tb_cfg_error(struct tb_ctl
*ctl
, u64 route
, u32 port
,
125 enum tb_cfg_error error
);
126 struct tb_cfg_result
tb_cfg_reset(struct tb_ctl
*ctl
, u64 route
,
128 struct tb_cfg_result
tb_cfg_read_raw(struct tb_ctl
*ctl
, void *buffer
,
130 enum tb_cfg_space space
, u32 offset
,
131 u32 length
, int timeout_msec
);
132 struct tb_cfg_result
tb_cfg_write_raw(struct tb_ctl
*ctl
, const void *buffer
,
134 enum tb_cfg_space space
, u32 offset
,
135 u32 length
, int timeout_msec
);
136 int tb_cfg_read(struct tb_ctl
*ctl
, void *buffer
, u64 route
, u32 port
,
137 enum tb_cfg_space space
, u32 offset
, u32 length
);
138 int tb_cfg_write(struct tb_ctl
*ctl
, const void *buffer
, u64 route
, u32 port
,
139 enum tb_cfg_space space
, u32 offset
, u32 length
);
140 int tb_cfg_get_upstream_port(struct tb_ctl
*ctl
, u64 route
);