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