Blame view

drivers/thunderbolt/ctl.h 4.63 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  /* SPDX-License-Identifier: GPL-2.0 */
f25bf6fcb   Andreas Noever   thunderbolt: Add ...
2
  /*
15c6784c7   Mika Westerberg   thunderbolt: Add ...
3
   * Thunderbolt driver - control channel and configuration commands
f25bf6fcb   Andreas Noever   thunderbolt: Add ...
4
5
   *
   * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
15c6784c7   Mika Westerberg   thunderbolt: Add ...
6
   * Copyright (C) 2018, Intel Corporation
f25bf6fcb   Andreas Noever   thunderbolt: Add ...
7
8
9
10
   */
  
  #ifndef _TB_CFG
  #define _TB_CFG
d7f781bfd   Mika Westerberg   thunderbolt: Rewo...
11
  #include <linux/kref.h>
eaf8ff35a   Mika Westerberg   thunderbolt: Move...
12
  #include <linux/thunderbolt.h>
d7f781bfd   Mika Westerberg   thunderbolt: Rewo...
13

f25bf6fcb   Andreas Noever   thunderbolt: Add ...
14
  #include "nhi.h"
32af9434f   Mika Westerberg   thunderbolt: Move...
15
  #include "tb_msgs.h"
f25bf6fcb   Andreas Noever   thunderbolt: Add ...
16
17
18
  
  /* control channel */
  struct tb_ctl;
d1ff70241   Mika Westerberg   thunderbolt: Add ...
19
  typedef bool (*event_cb)(void *data, enum tb_cfg_pkg_type type,
81a54b5e1   Mika Westerberg   thunderbolt: Let ...
20
  			 const void *buf, size_t size);
f25bf6fcb   Andreas Noever   thunderbolt: Add ...
21

81a54b5e1   Mika Westerberg   thunderbolt: Let ...
22
  struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, event_cb cb, void *cb_data);
f25bf6fcb   Andreas Noever   thunderbolt: Add ...
23
24
25
26
27
28
29
  void tb_ctl_start(struct tb_ctl *ctl);
  void tb_ctl_stop(struct tb_ctl *ctl);
  void tb_ctl_free(struct tb_ctl *ctl);
  
  /* configuration commands */
  
  #define TB_CFG_DEFAULT_TIMEOUT 5000 /* msec */
f25bf6fcb   Andreas Noever   thunderbolt: Add ...
30
31
32
33
34
35
36
37
38
39
40
41
42
  struct tb_cfg_result {
  	u64 response_route;
  	u32 response_port; /*
  			    * If err = 1 then this is the port that send the
  			    * error.
  			    * If err = 0 and if this was a cfg_read/write then
  			    * this is the the upstream port of the responding
  			    * switch.
  			    * Otherwise the field is set to zero.
  			    */
  	int err; /* negative errors, 0 for success, 1 for tb errors */
  	enum tb_cfg_error tb_error; /* valid if err == 1 */
  };
d7f781bfd   Mika Westerberg   thunderbolt: Rewo...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  struct ctl_pkg {
  	struct tb_ctl *ctl;
  	void *buffer;
  	struct ring_frame frame;
  };
  
  /**
   * struct tb_cfg_request - Control channel request
   * @kref: Reference count
   * @ctl: Pointer to the control channel structure. Only set when the
   *	 request is queued.
   * @request_size: Size of the request packet (in bytes)
   * @request_type: Type of the request packet
   * @response: Response is stored here
   * @response_size: Maximum size of one response packet
   * @response_type: Expected type of the response packet
   * @npackets: Number of packets expected to be returned with this request
   * @match: Function used to match the incoming packet
   * @copy: Function used to copy the incoming packet to @response
   * @callback: Callback called when the request is finished successfully
   * @callback_data: Data to be passed to @callback
   * @flags: Flags for the request
   * @work: Work item used to complete the request
   * @result: Result after the request has been completed
   * @list: Requests are queued using this field
   *
   * An arbitrary request over Thunderbolt control channel. For standard
   * control channel message, one should use tb_cfg_read/write() and
   * friends if possible.
   */
  struct tb_cfg_request {
  	struct kref kref;
  	struct tb_ctl *ctl;
  	const void *request;
  	size_t request_size;
  	enum tb_cfg_pkg_type request_type;
  	void *response;
  	size_t response_size;
  	enum tb_cfg_pkg_type response_type;
  	size_t npackets;
  	bool (*match)(const struct tb_cfg_request *req,
  		      const struct ctl_pkg *pkg);
  	bool (*copy)(struct tb_cfg_request *req, const struct ctl_pkg *pkg);
  	void (*callback)(void *callback_data);
  	void *callback_data;
  	unsigned long flags;
  	struct work_struct work;
  	struct tb_cfg_result result;
  	struct list_head list;
  };
  
  #define TB_CFG_REQUEST_ACTIVE		0
  #define TB_CFG_REQUEST_CANCELED		1
  
  struct tb_cfg_request *tb_cfg_request_alloc(void);
  void tb_cfg_request_get(struct tb_cfg_request *req);
  void tb_cfg_request_put(struct tb_cfg_request *req);
  int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
  		   void (*callback)(void *), void *callback_data);
  void tb_cfg_request_cancel(struct tb_cfg_request *req, int err);
  struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
  			struct tb_cfg_request *req, int timeout_msec);
ac6c44de5   Mika Westerberg   thunderbolt: Expo...
105
106
107
108
  static inline u64 tb_cfg_get_route(const struct tb_cfg_header *header)
  {
  	return (u64) header->route_hi << 32 | header->route_lo;
  }
f25bf6fcb   Andreas Noever   thunderbolt: Add ...
109

05c242e9e   Mika Westerberg   thunderbolt: Expo...
110
111
112
113
114
115
116
117
118
119
  static inline struct tb_cfg_header tb_cfg_make_header(u64 route)
  {
  	struct tb_cfg_header header = {
  		.route_hi = route >> 32,
  		.route_lo = route,
  	};
  	/* check for overflow, route_hi is not 32 bits! */
  	WARN_ON(tb_cfg_get_route(&header) != route);
  	return header;
  }
210e9f56e   Mika Westerberg   thunderbolt: Popu...
120
  int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug);
f25bf6fcb   Andreas Noever   thunderbolt: Add ...
121
122
123
124
125
126
  struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route,
  				  int timeout_msec);
  struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
  				     u64 route, u32 port,
  				     enum tb_cfg_space space, u32 offset,
  				     u32 length, int timeout_msec);
16a1258af   Mika Westerberg   thunderbolt: Use ...
127
  struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
f25bf6fcb   Andreas Noever   thunderbolt: Add ...
128
129
130
131
132
  				      u64 route, u32 port,
  				      enum tb_cfg_space space, u32 offset,
  				      u32 length, int timeout_msec);
  int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
  		enum tb_cfg_space space, u32 offset, u32 length);
16a1258af   Mika Westerberg   thunderbolt: Use ...
133
  int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
f25bf6fcb   Andreas Noever   thunderbolt: Add ...
134
135
136
137
138
  		 enum tb_cfg_space space, u32 offset, u32 length);
  int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route);
  
  
  #endif