]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/thunderbolt/ctl.h
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-jammy-kernel.git] / drivers / thunderbolt / ctl.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
f25bf6fc
AN
2/*
3 * Thunderbolt Cactus Ridge driver - control channel and configuration commands
4 *
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 */
7
8#ifndef _TB_CFG
9#define _TB_CFG
10
d7f781bf
MW
11#include <linux/kref.h>
12
f25bf6fc 13#include "nhi.h"
32af9434 14#include "tb_msgs.h"
f25bf6fc
AN
15
16/* control channel */
17struct tb_ctl;
18
81a54b5e
MW
19typedef void (*event_cb)(void *data, enum tb_cfg_pkg_type type,
20 const void *buf, size_t size);
f25bf6fc 21
81a54b5e 22struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, event_cb cb, void *cb_data);
f25bf6fc
AN
23void tb_ctl_start(struct tb_ctl *ctl);
24void tb_ctl_stop(struct tb_ctl *ctl);
25void tb_ctl_free(struct tb_ctl *ctl);
26
27/* configuration commands */
28
29#define TB_CFG_DEFAULT_TIMEOUT 5000 /* msec */
30
f25bf6fc
AN
31struct 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
d7f781bf
MW
45struct 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 */
75struct 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
99struct tb_cfg_request *tb_cfg_request_alloc(void);
100void tb_cfg_request_get(struct tb_cfg_request *req);
101void tb_cfg_request_put(struct tb_cfg_request *req);
102int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
103 void (*callback)(void *), void *callback_data);
104void tb_cfg_request_cancel(struct tb_cfg_request *req, int err);
105struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
106 struct tb_cfg_request *req, int timeout_msec);
107
ac6c44de
MW
108static inline u64 tb_cfg_get_route(const struct tb_cfg_header *header)
109{
110 return (u64) header->route_hi << 32 | header->route_lo;
111}
f25bf6fc 112
05c242e9
MW
113static 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
f25bf6fc
AN
124int tb_cfg_error(struct tb_ctl *ctl, u64 route, u32 port,
125 enum tb_cfg_error error);
126struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route,
127 int timeout_msec);
128struct 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);
16a1258a 132struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
f25bf6fc
AN
133 u64 route, u32 port,
134 enum tb_cfg_space space, u32 offset,
135 u32 length, int timeout_msec);
136int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
137 enum tb_cfg_space space, u32 offset, u32 length);
16a1258a 138int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
f25bf6fc
AN
139 enum tb_cfg_space space, u32 offset, u32 length);
140int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route);
141
142
143#endif