xref: /linux/drivers/thunderbolt/ctl.h (revision c4b3630afffca55faab67da1d347a22d27318a39)
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 <linux/kref.h>
11 #include <linux/thunderbolt.h>
12 
13 #include "nhi.h"
14 #include "tb_msgs.h"
15 
16 /* control channel */
17 struct tb_ctl;
18 
19 typedef bool (*event_cb)(void *data, enum tb_cfg_pkg_type type,
20 			 const void *buf, size_t size);
21 
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);
26 
27 /* configuration commands */
28 
29 #define TB_CFG_DEFAULT_TIMEOUT 5000 /* msec */
30 
31 struct tb_cfg_result {
32 	u64 response_route;
33 	u32 response_port; /*
34 			    * If err = 1 then this is the port that send the
35 			    * error.
36 			    * If err = 0 and if this was a cfg_read/write then
37 			    * this is the the upstream port of the responding
38 			    * switch.
39 			    * Otherwise the field is set to zero.
40 			    */
41 	int err; /* negative errors, 0 for success, 1 for tb errors */
42 	enum tb_cfg_error tb_error; /* valid if err == 1 */
43 };
44 
45 struct ctl_pkg {
46 	struct tb_ctl *ctl;
47 	void *buffer;
48 	struct ring_frame frame;
49 };
50 
51 /**
52  * struct tb_cfg_request - Control channel request
53  * @kref: Reference count
54  * @ctl: Pointer to the control channel structure. Only set when the
55  *	 request is queued.
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
70  *
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.
74  */
75 struct tb_cfg_request {
76 	struct kref kref;
77 	struct tb_ctl *ctl;
78 	const void *request;
79 	size_t request_size;
80 	enum tb_cfg_pkg_type request_type;
81 	void *response;
82 	size_t response_size;
83 	enum tb_cfg_pkg_type response_type;
84 	size_t npackets;
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);
89 	void *callback_data;
90 	unsigned long flags;
91 	struct work_struct work;
92 	struct tb_cfg_result result;
93 	struct list_head list;
94 };
95 
96 #define TB_CFG_REQUEST_ACTIVE		0
97 #define TB_CFG_REQUEST_CANCELED		1
98 
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);
107 
108 static inline u64 tb_cfg_get_route(const struct tb_cfg_header *header)
109 {
110 	return (u64) header->route_hi << 32 | header->route_lo;
111 }
112 
113 static inline struct tb_cfg_header tb_cfg_make_header(u64 route)
114 {
115 	struct tb_cfg_header header = {
116 		.route_hi = route >> 32,
117 		.route_lo = route,
118 	};
119 	/* check for overflow, route_hi is not 32 bits! */
120 	WARN_ON(tb_cfg_get_route(&header) != route);
121 	return header;
122 }
123 
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,
127 				  int timeout_msec);
128 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
129 				     u64 route, u32 port,
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,
133 				      u64 route, u32 port,
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);
141 
142 
143 #endif
144