xref: /linux/drivers/thunderbolt/ctl.h (revision 05c242e9e47d210ed6cbef31f2c441fa6ee325c6)
1 /*
2  * Thunderbolt Cactus Ridge driver - control channel and configuration commands
3  *
4  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
5  */
6 
7 #ifndef _TB_CFG
8 #define _TB_CFG
9 
10 #include "nhi.h"
11 #include "tb_msgs.h"
12 
13 /* control channel */
14 struct tb_ctl;
15 
16 typedef void (*hotplug_cb)(void *data, u64 route, u8 port, bool unplug);
17 
18 struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, hotplug_cb cb, void *cb_data);
19 void tb_ctl_start(struct tb_ctl *ctl);
20 void tb_ctl_stop(struct tb_ctl *ctl);
21 void tb_ctl_free(struct tb_ctl *ctl);
22 
23 /* configuration commands */
24 
25 #define TB_CFG_DEFAULT_TIMEOUT 5000 /* msec */
26 
27 struct tb_cfg_result {
28 	u64 response_route;
29 	u32 response_port; /*
30 			    * If err = 1 then this is the port that send the
31 			    * error.
32 			    * If err = 0 and if this was a cfg_read/write then
33 			    * this is the the upstream port of the responding
34 			    * switch.
35 			    * Otherwise the field is set to zero.
36 			    */
37 	int err; /* negative errors, 0 for success, 1 for tb errors */
38 	enum tb_cfg_error tb_error; /* valid if err == 1 */
39 };
40 
41 static inline u64 tb_cfg_get_route(const struct tb_cfg_header *header)
42 {
43 	return (u64) header->route_hi << 32 | header->route_lo;
44 }
45 
46 static inline struct tb_cfg_header tb_cfg_make_header(u64 route)
47 {
48 	struct tb_cfg_header header = {
49 		.route_hi = route >> 32,
50 		.route_lo = route,
51 	};
52 	/* check for overflow, route_hi is not 32 bits! */
53 	WARN_ON(tb_cfg_get_route(&header) != route);
54 	return header;
55 }
56 
57 int tb_cfg_error(struct tb_ctl *ctl, u64 route, u32 port,
58 		 enum tb_cfg_error error);
59 struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route,
60 				  int timeout_msec);
61 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
62 				     u64 route, u32 port,
63 				     enum tb_cfg_space space, u32 offset,
64 				     u32 length, int timeout_msec);
65 struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
66 				      u64 route, u32 port,
67 				      enum tb_cfg_space space, u32 offset,
68 				      u32 length, int timeout_msec);
69 int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
70 		enum tb_cfg_space space, u32 offset, u32 length);
71 int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
72 		 enum tb_cfg_space space, u32 offset, u32 length);
73 int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route);
74 
75 
76 #endif
77