1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Thunderbolt XDomain discovery protocol support
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2017, Intel Corporation
6*4882a593Smuzhiyun * Authors: Michael Jamet <michael.jamet@intel.com>
7*4882a593Smuzhiyun * Mika Westerberg <mika.westerberg@linux.intel.com>
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/device.h>
11*4882a593Smuzhiyun #include <linux/kmod.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/pm_runtime.h>
14*4882a593Smuzhiyun #include <linux/utsname.h>
15*4882a593Smuzhiyun #include <linux/uuid.h>
16*4882a593Smuzhiyun #include <linux/workqueue.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include "tb.h"
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #define XDOMAIN_DEFAULT_TIMEOUT 5000 /* ms */
21*4882a593Smuzhiyun #define XDOMAIN_UUID_RETRIES 10
22*4882a593Smuzhiyun #define XDOMAIN_PROPERTIES_RETRIES 60
23*4882a593Smuzhiyun #define XDOMAIN_PROPERTIES_CHANGED_RETRIES 10
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun struct xdomain_request_work {
26*4882a593Smuzhiyun struct work_struct work;
27*4882a593Smuzhiyun struct tb_xdp_header *pkg;
28*4882a593Smuzhiyun struct tb *tb;
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /* Serializes access to the properties and protocol handlers below */
32*4882a593Smuzhiyun static DEFINE_MUTEX(xdomain_lock);
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /* Properties exposed to the remote domains */
35*4882a593Smuzhiyun static struct tb_property_dir *xdomain_property_dir;
36*4882a593Smuzhiyun static u32 *xdomain_property_block;
37*4882a593Smuzhiyun static u32 xdomain_property_block_len;
38*4882a593Smuzhiyun static u32 xdomain_property_block_gen;
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /* Additional protocol handlers */
41*4882a593Smuzhiyun static LIST_HEAD(protocol_handlers);
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* UUID for XDomain discovery protocol: b638d70e-42ff-40bb-97c2-90e2c0b2ff07 */
44*4882a593Smuzhiyun static const uuid_t tb_xdp_uuid =
45*4882a593Smuzhiyun UUID_INIT(0xb638d70e, 0x42ff, 0x40bb,
46*4882a593Smuzhiyun 0x97, 0xc2, 0x90, 0xe2, 0xc0, 0xb2, 0xff, 0x07);
47*4882a593Smuzhiyun
tb_xdomain_match(const struct tb_cfg_request * req,const struct ctl_pkg * pkg)48*4882a593Smuzhiyun static bool tb_xdomain_match(const struct tb_cfg_request *req,
49*4882a593Smuzhiyun const struct ctl_pkg *pkg)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun switch (pkg->frame.eof) {
52*4882a593Smuzhiyun case TB_CFG_PKG_ERROR:
53*4882a593Smuzhiyun return true;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun case TB_CFG_PKG_XDOMAIN_RESP: {
56*4882a593Smuzhiyun const struct tb_xdp_header *res_hdr = pkg->buffer;
57*4882a593Smuzhiyun const struct tb_xdp_header *req_hdr = req->request;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun if (pkg->frame.size < req->response_size / 4)
60*4882a593Smuzhiyun return false;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* Make sure route matches */
63*4882a593Smuzhiyun if ((res_hdr->xd_hdr.route_hi & ~BIT(31)) !=
64*4882a593Smuzhiyun req_hdr->xd_hdr.route_hi)
65*4882a593Smuzhiyun return false;
66*4882a593Smuzhiyun if ((res_hdr->xd_hdr.route_lo) != req_hdr->xd_hdr.route_lo)
67*4882a593Smuzhiyun return false;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* Check that the XDomain protocol matches */
70*4882a593Smuzhiyun if (!uuid_equal(&res_hdr->uuid, &req_hdr->uuid))
71*4882a593Smuzhiyun return false;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun return true;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun default:
77*4882a593Smuzhiyun return false;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
tb_xdomain_copy(struct tb_cfg_request * req,const struct ctl_pkg * pkg)81*4882a593Smuzhiyun static bool tb_xdomain_copy(struct tb_cfg_request *req,
82*4882a593Smuzhiyun const struct ctl_pkg *pkg)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun memcpy(req->response, pkg->buffer, req->response_size);
85*4882a593Smuzhiyun req->result.err = 0;
86*4882a593Smuzhiyun return true;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
response_ready(void * data)89*4882a593Smuzhiyun static void response_ready(void *data)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun tb_cfg_request_put(data);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
__tb_xdomain_response(struct tb_ctl * ctl,const void * response,size_t size,enum tb_cfg_pkg_type type)94*4882a593Smuzhiyun static int __tb_xdomain_response(struct tb_ctl *ctl, const void *response,
95*4882a593Smuzhiyun size_t size, enum tb_cfg_pkg_type type)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun struct tb_cfg_request *req;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun req = tb_cfg_request_alloc();
100*4882a593Smuzhiyun if (!req)
101*4882a593Smuzhiyun return -ENOMEM;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun req->match = tb_xdomain_match;
104*4882a593Smuzhiyun req->copy = tb_xdomain_copy;
105*4882a593Smuzhiyun req->request = response;
106*4882a593Smuzhiyun req->request_size = size;
107*4882a593Smuzhiyun req->request_type = type;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun return tb_cfg_request(ctl, req, response_ready, req);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /**
113*4882a593Smuzhiyun * tb_xdomain_response() - Send a XDomain response message
114*4882a593Smuzhiyun * @xd: XDomain to send the message
115*4882a593Smuzhiyun * @response: Response to send
116*4882a593Smuzhiyun * @size: Size of the response
117*4882a593Smuzhiyun * @type: PDF type of the response
118*4882a593Smuzhiyun *
119*4882a593Smuzhiyun * This can be used to send a XDomain response message to the other
120*4882a593Smuzhiyun * domain. No response for the message is expected.
121*4882a593Smuzhiyun *
122*4882a593Smuzhiyun * Return: %0 in case of success and negative errno in case of failure
123*4882a593Smuzhiyun */
tb_xdomain_response(struct tb_xdomain * xd,const void * response,size_t size,enum tb_cfg_pkg_type type)124*4882a593Smuzhiyun int tb_xdomain_response(struct tb_xdomain *xd, const void *response,
125*4882a593Smuzhiyun size_t size, enum tb_cfg_pkg_type type)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun return __tb_xdomain_response(xd->tb->ctl, response, size, type);
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_xdomain_response);
130*4882a593Smuzhiyun
__tb_xdomain_request(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,unsigned int timeout_msec)131*4882a593Smuzhiyun static int __tb_xdomain_request(struct tb_ctl *ctl, const void *request,
132*4882a593Smuzhiyun size_t request_size, enum tb_cfg_pkg_type request_type, void *response,
133*4882a593Smuzhiyun size_t response_size, enum tb_cfg_pkg_type response_type,
134*4882a593Smuzhiyun unsigned int timeout_msec)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun struct tb_cfg_request *req;
137*4882a593Smuzhiyun struct tb_cfg_result res;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun req = tb_cfg_request_alloc();
140*4882a593Smuzhiyun if (!req)
141*4882a593Smuzhiyun return -ENOMEM;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun req->match = tb_xdomain_match;
144*4882a593Smuzhiyun req->copy = tb_xdomain_copy;
145*4882a593Smuzhiyun req->request = request;
146*4882a593Smuzhiyun req->request_size = request_size;
147*4882a593Smuzhiyun req->request_type = request_type;
148*4882a593Smuzhiyun req->response = response;
149*4882a593Smuzhiyun req->response_size = response_size;
150*4882a593Smuzhiyun req->response_type = response_type;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun res = tb_cfg_request_sync(ctl, req, timeout_msec);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun tb_cfg_request_put(req);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun return res.err == 1 ? -EIO : res.err;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /**
160*4882a593Smuzhiyun * tb_xdomain_request() - Send a XDomain request
161*4882a593Smuzhiyun * @xd: XDomain to send the request
162*4882a593Smuzhiyun * @request: Request to send
163*4882a593Smuzhiyun * @request_size: Size of the request in bytes
164*4882a593Smuzhiyun * @request_type: PDF type of the request
165*4882a593Smuzhiyun * @response: Response is copied here
166*4882a593Smuzhiyun * @response_size: Expected size of the response in bytes
167*4882a593Smuzhiyun * @response_type: Expected PDF type of the response
168*4882a593Smuzhiyun * @timeout_msec: Timeout in milliseconds to wait for the response
169*4882a593Smuzhiyun *
170*4882a593Smuzhiyun * This function can be used to send XDomain control channel messages to
171*4882a593Smuzhiyun * the other domain. The function waits until the response is received
172*4882a593Smuzhiyun * or when timeout triggers. Whichever comes first.
173*4882a593Smuzhiyun *
174*4882a593Smuzhiyun * Return: %0 in case of success and negative errno in case of failure
175*4882a593Smuzhiyun */
tb_xdomain_request(struct tb_xdomain * xd,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,unsigned int timeout_msec)176*4882a593Smuzhiyun int tb_xdomain_request(struct tb_xdomain *xd, const void *request,
177*4882a593Smuzhiyun size_t request_size, enum tb_cfg_pkg_type request_type,
178*4882a593Smuzhiyun void *response, size_t response_size,
179*4882a593Smuzhiyun enum tb_cfg_pkg_type response_type, unsigned int timeout_msec)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun return __tb_xdomain_request(xd->tb->ctl, request, request_size,
182*4882a593Smuzhiyun request_type, response, response_size,
183*4882a593Smuzhiyun response_type, timeout_msec);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_xdomain_request);
186*4882a593Smuzhiyun
tb_xdp_fill_header(struct tb_xdp_header * hdr,u64 route,u8 sequence,enum tb_xdp_type type,size_t size)187*4882a593Smuzhiyun static inline void tb_xdp_fill_header(struct tb_xdp_header *hdr, u64 route,
188*4882a593Smuzhiyun u8 sequence, enum tb_xdp_type type, size_t size)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun u32 length_sn;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun length_sn = (size - sizeof(hdr->xd_hdr)) / 4;
193*4882a593Smuzhiyun length_sn |= (sequence << TB_XDOMAIN_SN_SHIFT) & TB_XDOMAIN_SN_MASK;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun hdr->xd_hdr.route_hi = upper_32_bits(route);
196*4882a593Smuzhiyun hdr->xd_hdr.route_lo = lower_32_bits(route);
197*4882a593Smuzhiyun hdr->xd_hdr.length_sn = length_sn;
198*4882a593Smuzhiyun hdr->type = type;
199*4882a593Smuzhiyun memcpy(&hdr->uuid, &tb_xdp_uuid, sizeof(tb_xdp_uuid));
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
tb_xdp_handle_error(const struct tb_xdp_header * hdr)202*4882a593Smuzhiyun static int tb_xdp_handle_error(const struct tb_xdp_header *hdr)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun const struct tb_xdp_error_response *error;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun if (hdr->type != ERROR_RESPONSE)
207*4882a593Smuzhiyun return 0;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun error = (const struct tb_xdp_error_response *)hdr;
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun switch (error->error) {
212*4882a593Smuzhiyun case ERROR_UNKNOWN_PACKET:
213*4882a593Smuzhiyun case ERROR_UNKNOWN_DOMAIN:
214*4882a593Smuzhiyun return -EIO;
215*4882a593Smuzhiyun case ERROR_NOT_SUPPORTED:
216*4882a593Smuzhiyun return -ENOTSUPP;
217*4882a593Smuzhiyun case ERROR_NOT_READY:
218*4882a593Smuzhiyun return -EAGAIN;
219*4882a593Smuzhiyun default:
220*4882a593Smuzhiyun break;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun return 0;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
tb_xdp_uuid_request(struct tb_ctl * ctl,u64 route,int retry,uuid_t * uuid)226*4882a593Smuzhiyun static int tb_xdp_uuid_request(struct tb_ctl *ctl, u64 route, int retry,
227*4882a593Smuzhiyun uuid_t *uuid)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun struct tb_xdp_uuid_response res;
230*4882a593Smuzhiyun struct tb_xdp_uuid req;
231*4882a593Smuzhiyun int ret;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun memset(&req, 0, sizeof(req));
234*4882a593Smuzhiyun tb_xdp_fill_header(&req.hdr, route, retry % 4, UUID_REQUEST,
235*4882a593Smuzhiyun sizeof(req));
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun memset(&res, 0, sizeof(res));
238*4882a593Smuzhiyun ret = __tb_xdomain_request(ctl, &req, sizeof(req),
239*4882a593Smuzhiyun TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
240*4882a593Smuzhiyun TB_CFG_PKG_XDOMAIN_RESP,
241*4882a593Smuzhiyun XDOMAIN_DEFAULT_TIMEOUT);
242*4882a593Smuzhiyun if (ret)
243*4882a593Smuzhiyun return ret;
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun ret = tb_xdp_handle_error(&res.hdr);
246*4882a593Smuzhiyun if (ret)
247*4882a593Smuzhiyun return ret;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun uuid_copy(uuid, &res.src_uuid);
250*4882a593Smuzhiyun return 0;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
tb_xdp_uuid_response(struct tb_ctl * ctl,u64 route,u8 sequence,const uuid_t * uuid)253*4882a593Smuzhiyun static int tb_xdp_uuid_response(struct tb_ctl *ctl, u64 route, u8 sequence,
254*4882a593Smuzhiyun const uuid_t *uuid)
255*4882a593Smuzhiyun {
256*4882a593Smuzhiyun struct tb_xdp_uuid_response res;
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun memset(&res, 0, sizeof(res));
259*4882a593Smuzhiyun tb_xdp_fill_header(&res.hdr, route, sequence, UUID_RESPONSE,
260*4882a593Smuzhiyun sizeof(res));
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun uuid_copy(&res.src_uuid, uuid);
263*4882a593Smuzhiyun res.src_route_hi = upper_32_bits(route);
264*4882a593Smuzhiyun res.src_route_lo = lower_32_bits(route);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun return __tb_xdomain_response(ctl, &res, sizeof(res),
267*4882a593Smuzhiyun TB_CFG_PKG_XDOMAIN_RESP);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
tb_xdp_error_response(struct tb_ctl * ctl,u64 route,u8 sequence,enum tb_xdp_error error)270*4882a593Smuzhiyun static int tb_xdp_error_response(struct tb_ctl *ctl, u64 route, u8 sequence,
271*4882a593Smuzhiyun enum tb_xdp_error error)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun struct tb_xdp_error_response res;
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun memset(&res, 0, sizeof(res));
276*4882a593Smuzhiyun tb_xdp_fill_header(&res.hdr, route, sequence, ERROR_RESPONSE,
277*4882a593Smuzhiyun sizeof(res));
278*4882a593Smuzhiyun res.error = error;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun return __tb_xdomain_response(ctl, &res, sizeof(res),
281*4882a593Smuzhiyun TB_CFG_PKG_XDOMAIN_RESP);
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
tb_xdp_properties_request(struct tb_ctl * ctl,u64 route,const uuid_t * src_uuid,const uuid_t * dst_uuid,int retry,u32 ** block,u32 * generation)284*4882a593Smuzhiyun static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route,
285*4882a593Smuzhiyun const uuid_t *src_uuid, const uuid_t *dst_uuid, int retry,
286*4882a593Smuzhiyun u32 **block, u32 *generation)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun struct tb_xdp_properties_response *res;
289*4882a593Smuzhiyun struct tb_xdp_properties req;
290*4882a593Smuzhiyun u16 data_len, len;
291*4882a593Smuzhiyun size_t total_size;
292*4882a593Smuzhiyun u32 *data = NULL;
293*4882a593Smuzhiyun int ret;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun total_size = sizeof(*res) + TB_XDP_PROPERTIES_MAX_DATA_LENGTH * 4;
296*4882a593Smuzhiyun res = kzalloc(total_size, GFP_KERNEL);
297*4882a593Smuzhiyun if (!res)
298*4882a593Smuzhiyun return -ENOMEM;
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun memset(&req, 0, sizeof(req));
301*4882a593Smuzhiyun tb_xdp_fill_header(&req.hdr, route, retry % 4, PROPERTIES_REQUEST,
302*4882a593Smuzhiyun sizeof(req));
303*4882a593Smuzhiyun memcpy(&req.src_uuid, src_uuid, sizeof(*src_uuid));
304*4882a593Smuzhiyun memcpy(&req.dst_uuid, dst_uuid, sizeof(*dst_uuid));
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun len = 0;
307*4882a593Smuzhiyun data_len = 0;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun do {
310*4882a593Smuzhiyun ret = __tb_xdomain_request(ctl, &req, sizeof(req),
311*4882a593Smuzhiyun TB_CFG_PKG_XDOMAIN_REQ, res,
312*4882a593Smuzhiyun total_size, TB_CFG_PKG_XDOMAIN_RESP,
313*4882a593Smuzhiyun XDOMAIN_DEFAULT_TIMEOUT);
314*4882a593Smuzhiyun if (ret)
315*4882a593Smuzhiyun goto err;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun ret = tb_xdp_handle_error(&res->hdr);
318*4882a593Smuzhiyun if (ret)
319*4882a593Smuzhiyun goto err;
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun /*
322*4882a593Smuzhiyun * Package length includes the whole payload without the
323*4882a593Smuzhiyun * XDomain header. Validate first that the package is at
324*4882a593Smuzhiyun * least size of the response structure.
325*4882a593Smuzhiyun */
326*4882a593Smuzhiyun len = res->hdr.xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
327*4882a593Smuzhiyun if (len < sizeof(*res) / 4) {
328*4882a593Smuzhiyun ret = -EINVAL;
329*4882a593Smuzhiyun goto err;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun len += sizeof(res->hdr.xd_hdr) / 4;
333*4882a593Smuzhiyun len -= sizeof(*res) / 4;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun if (res->offset != req.offset) {
336*4882a593Smuzhiyun ret = -EINVAL;
337*4882a593Smuzhiyun goto err;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun /*
341*4882a593Smuzhiyun * First time allocate block that has enough space for
342*4882a593Smuzhiyun * the whole properties block.
343*4882a593Smuzhiyun */
344*4882a593Smuzhiyun if (!data) {
345*4882a593Smuzhiyun data_len = res->data_length;
346*4882a593Smuzhiyun if (data_len > TB_XDP_PROPERTIES_MAX_LENGTH) {
347*4882a593Smuzhiyun ret = -E2BIG;
348*4882a593Smuzhiyun goto err;
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun data = kcalloc(data_len, sizeof(u32), GFP_KERNEL);
352*4882a593Smuzhiyun if (!data) {
353*4882a593Smuzhiyun ret = -ENOMEM;
354*4882a593Smuzhiyun goto err;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun memcpy(data + req.offset, res->data, len * 4);
359*4882a593Smuzhiyun req.offset += len;
360*4882a593Smuzhiyun } while (!data_len || req.offset < data_len);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun *block = data;
363*4882a593Smuzhiyun *generation = res->generation;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun kfree(res);
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun return data_len;
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun err:
370*4882a593Smuzhiyun kfree(data);
371*4882a593Smuzhiyun kfree(res);
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun return ret;
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
tb_xdp_properties_response(struct tb * tb,struct tb_ctl * ctl,u64 route,u8 sequence,const uuid_t * src_uuid,const struct tb_xdp_properties * req)376*4882a593Smuzhiyun static int tb_xdp_properties_response(struct tb *tb, struct tb_ctl *ctl,
377*4882a593Smuzhiyun u64 route, u8 sequence, const uuid_t *src_uuid,
378*4882a593Smuzhiyun const struct tb_xdp_properties *req)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun struct tb_xdp_properties_response *res;
381*4882a593Smuzhiyun size_t total_size;
382*4882a593Smuzhiyun u16 len;
383*4882a593Smuzhiyun int ret;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun /*
386*4882a593Smuzhiyun * Currently we expect all requests to be directed to us. The
387*4882a593Smuzhiyun * protocol supports forwarding, though which we might add
388*4882a593Smuzhiyun * support later on.
389*4882a593Smuzhiyun */
390*4882a593Smuzhiyun if (!uuid_equal(src_uuid, &req->dst_uuid)) {
391*4882a593Smuzhiyun tb_xdp_error_response(ctl, route, sequence,
392*4882a593Smuzhiyun ERROR_UNKNOWN_DOMAIN);
393*4882a593Smuzhiyun return 0;
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun mutex_lock(&xdomain_lock);
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun if (req->offset >= xdomain_property_block_len) {
399*4882a593Smuzhiyun mutex_unlock(&xdomain_lock);
400*4882a593Smuzhiyun return -EINVAL;
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun len = xdomain_property_block_len - req->offset;
404*4882a593Smuzhiyun len = min_t(u16, len, TB_XDP_PROPERTIES_MAX_DATA_LENGTH);
405*4882a593Smuzhiyun total_size = sizeof(*res) + len * 4;
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun res = kzalloc(total_size, GFP_KERNEL);
408*4882a593Smuzhiyun if (!res) {
409*4882a593Smuzhiyun mutex_unlock(&xdomain_lock);
410*4882a593Smuzhiyun return -ENOMEM;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun tb_xdp_fill_header(&res->hdr, route, sequence, PROPERTIES_RESPONSE,
414*4882a593Smuzhiyun total_size);
415*4882a593Smuzhiyun res->generation = xdomain_property_block_gen;
416*4882a593Smuzhiyun res->data_length = xdomain_property_block_len;
417*4882a593Smuzhiyun res->offset = req->offset;
418*4882a593Smuzhiyun uuid_copy(&res->src_uuid, src_uuid);
419*4882a593Smuzhiyun uuid_copy(&res->dst_uuid, &req->src_uuid);
420*4882a593Smuzhiyun memcpy(res->data, &xdomain_property_block[req->offset], len * 4);
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun mutex_unlock(&xdomain_lock);
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun ret = __tb_xdomain_response(ctl, res, total_size,
425*4882a593Smuzhiyun TB_CFG_PKG_XDOMAIN_RESP);
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun kfree(res);
428*4882a593Smuzhiyun return ret;
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun
tb_xdp_properties_changed_request(struct tb_ctl * ctl,u64 route,int retry,const uuid_t * uuid)431*4882a593Smuzhiyun static int tb_xdp_properties_changed_request(struct tb_ctl *ctl, u64 route,
432*4882a593Smuzhiyun int retry, const uuid_t *uuid)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun struct tb_xdp_properties_changed_response res;
435*4882a593Smuzhiyun struct tb_xdp_properties_changed req;
436*4882a593Smuzhiyun int ret;
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun memset(&req, 0, sizeof(req));
439*4882a593Smuzhiyun tb_xdp_fill_header(&req.hdr, route, retry % 4,
440*4882a593Smuzhiyun PROPERTIES_CHANGED_REQUEST, sizeof(req));
441*4882a593Smuzhiyun uuid_copy(&req.src_uuid, uuid);
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun memset(&res, 0, sizeof(res));
444*4882a593Smuzhiyun ret = __tb_xdomain_request(ctl, &req, sizeof(req),
445*4882a593Smuzhiyun TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
446*4882a593Smuzhiyun TB_CFG_PKG_XDOMAIN_RESP,
447*4882a593Smuzhiyun XDOMAIN_DEFAULT_TIMEOUT);
448*4882a593Smuzhiyun if (ret)
449*4882a593Smuzhiyun return ret;
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun return tb_xdp_handle_error(&res.hdr);
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun static int
tb_xdp_properties_changed_response(struct tb_ctl * ctl,u64 route,u8 sequence)455*4882a593Smuzhiyun tb_xdp_properties_changed_response(struct tb_ctl *ctl, u64 route, u8 sequence)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun struct tb_xdp_properties_changed_response res;
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun memset(&res, 0, sizeof(res));
460*4882a593Smuzhiyun tb_xdp_fill_header(&res.hdr, route, sequence,
461*4882a593Smuzhiyun PROPERTIES_CHANGED_RESPONSE, sizeof(res));
462*4882a593Smuzhiyun return __tb_xdomain_response(ctl, &res, sizeof(res),
463*4882a593Smuzhiyun TB_CFG_PKG_XDOMAIN_RESP);
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun /**
467*4882a593Smuzhiyun * tb_register_protocol_handler() - Register protocol handler
468*4882a593Smuzhiyun * @handler: Handler to register
469*4882a593Smuzhiyun *
470*4882a593Smuzhiyun * This allows XDomain service drivers to hook into incoming XDomain
471*4882a593Smuzhiyun * messages. After this function is called the service driver needs to
472*4882a593Smuzhiyun * be able to handle calls to callback whenever a package with the
473*4882a593Smuzhiyun * registered protocol is received.
474*4882a593Smuzhiyun */
tb_register_protocol_handler(struct tb_protocol_handler * handler)475*4882a593Smuzhiyun int tb_register_protocol_handler(struct tb_protocol_handler *handler)
476*4882a593Smuzhiyun {
477*4882a593Smuzhiyun if (!handler->uuid || !handler->callback)
478*4882a593Smuzhiyun return -EINVAL;
479*4882a593Smuzhiyun if (uuid_equal(handler->uuid, &tb_xdp_uuid))
480*4882a593Smuzhiyun return -EINVAL;
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun mutex_lock(&xdomain_lock);
483*4882a593Smuzhiyun list_add_tail(&handler->list, &protocol_handlers);
484*4882a593Smuzhiyun mutex_unlock(&xdomain_lock);
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun return 0;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_register_protocol_handler);
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun /**
491*4882a593Smuzhiyun * tb_unregister_protocol_handler() - Unregister protocol handler
492*4882a593Smuzhiyun * @handler: Handler to unregister
493*4882a593Smuzhiyun *
494*4882a593Smuzhiyun * Removes the previously registered protocol handler.
495*4882a593Smuzhiyun */
tb_unregister_protocol_handler(struct tb_protocol_handler * handler)496*4882a593Smuzhiyun void tb_unregister_protocol_handler(struct tb_protocol_handler *handler)
497*4882a593Smuzhiyun {
498*4882a593Smuzhiyun mutex_lock(&xdomain_lock);
499*4882a593Smuzhiyun list_del_init(&handler->list);
500*4882a593Smuzhiyun mutex_unlock(&xdomain_lock);
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_unregister_protocol_handler);
503*4882a593Smuzhiyun
rebuild_property_block(void)504*4882a593Smuzhiyun static int rebuild_property_block(void)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun u32 *block, len;
507*4882a593Smuzhiyun int ret;
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun ret = tb_property_format_dir(xdomain_property_dir, NULL, 0);
510*4882a593Smuzhiyun if (ret < 0)
511*4882a593Smuzhiyun return ret;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun len = ret;
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun block = kcalloc(len, sizeof(u32), GFP_KERNEL);
516*4882a593Smuzhiyun if (!block)
517*4882a593Smuzhiyun return -ENOMEM;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun ret = tb_property_format_dir(xdomain_property_dir, block, len);
520*4882a593Smuzhiyun if (ret) {
521*4882a593Smuzhiyun kfree(block);
522*4882a593Smuzhiyun return ret;
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun kfree(xdomain_property_block);
526*4882a593Smuzhiyun xdomain_property_block = block;
527*4882a593Smuzhiyun xdomain_property_block_len = len;
528*4882a593Smuzhiyun xdomain_property_block_gen++;
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun return 0;
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun
finalize_property_block(void)533*4882a593Smuzhiyun static void finalize_property_block(void)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun const struct tb_property *nodename;
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun /*
538*4882a593Smuzhiyun * On first XDomain connection we set up the the system
539*4882a593Smuzhiyun * nodename. This delayed here because userspace may not have it
540*4882a593Smuzhiyun * set when the driver is first probed.
541*4882a593Smuzhiyun */
542*4882a593Smuzhiyun mutex_lock(&xdomain_lock);
543*4882a593Smuzhiyun nodename = tb_property_find(xdomain_property_dir, "deviceid",
544*4882a593Smuzhiyun TB_PROPERTY_TYPE_TEXT);
545*4882a593Smuzhiyun if (!nodename) {
546*4882a593Smuzhiyun tb_property_add_text(xdomain_property_dir, "deviceid",
547*4882a593Smuzhiyun utsname()->nodename);
548*4882a593Smuzhiyun rebuild_property_block();
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun mutex_unlock(&xdomain_lock);
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun
tb_xdp_handle_request(struct work_struct * work)553*4882a593Smuzhiyun static void tb_xdp_handle_request(struct work_struct *work)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun struct xdomain_request_work *xw = container_of(work, typeof(*xw), work);
556*4882a593Smuzhiyun const struct tb_xdp_header *pkg = xw->pkg;
557*4882a593Smuzhiyun const struct tb_xdomain_header *xhdr = &pkg->xd_hdr;
558*4882a593Smuzhiyun struct tb *tb = xw->tb;
559*4882a593Smuzhiyun struct tb_ctl *ctl = tb->ctl;
560*4882a593Smuzhiyun const uuid_t *uuid;
561*4882a593Smuzhiyun int ret = 0;
562*4882a593Smuzhiyun u32 sequence;
563*4882a593Smuzhiyun u64 route;
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun route = ((u64)xhdr->route_hi << 32 | xhdr->route_lo) & ~BIT_ULL(63);
566*4882a593Smuzhiyun sequence = xhdr->length_sn & TB_XDOMAIN_SN_MASK;
567*4882a593Smuzhiyun sequence >>= TB_XDOMAIN_SN_SHIFT;
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun mutex_lock(&tb->lock);
570*4882a593Smuzhiyun if (tb->root_switch)
571*4882a593Smuzhiyun uuid = tb->root_switch->uuid;
572*4882a593Smuzhiyun else
573*4882a593Smuzhiyun uuid = NULL;
574*4882a593Smuzhiyun mutex_unlock(&tb->lock);
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun if (!uuid) {
577*4882a593Smuzhiyun tb_xdp_error_response(ctl, route, sequence, ERROR_NOT_READY);
578*4882a593Smuzhiyun goto out;
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun finalize_property_block();
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun switch (pkg->type) {
584*4882a593Smuzhiyun case PROPERTIES_REQUEST:
585*4882a593Smuzhiyun ret = tb_xdp_properties_response(tb, ctl, route, sequence, uuid,
586*4882a593Smuzhiyun (const struct tb_xdp_properties *)pkg);
587*4882a593Smuzhiyun break;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun case PROPERTIES_CHANGED_REQUEST: {
590*4882a593Smuzhiyun const struct tb_xdp_properties_changed *xchg =
591*4882a593Smuzhiyun (const struct tb_xdp_properties_changed *)pkg;
592*4882a593Smuzhiyun struct tb_xdomain *xd;
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun ret = tb_xdp_properties_changed_response(ctl, route, sequence);
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun /*
597*4882a593Smuzhiyun * Since the properties have been changed, let's update
598*4882a593Smuzhiyun * the xdomain related to this connection as well in
599*4882a593Smuzhiyun * case there is a change in services it offers.
600*4882a593Smuzhiyun */
601*4882a593Smuzhiyun xd = tb_xdomain_find_by_uuid_locked(tb, &xchg->src_uuid);
602*4882a593Smuzhiyun if (xd) {
603*4882a593Smuzhiyun queue_delayed_work(tb->wq, &xd->get_properties_work,
604*4882a593Smuzhiyun msecs_to_jiffies(50));
605*4882a593Smuzhiyun tb_xdomain_put(xd);
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun break;
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun case UUID_REQUEST_OLD:
612*4882a593Smuzhiyun case UUID_REQUEST:
613*4882a593Smuzhiyun ret = tb_xdp_uuid_response(ctl, route, sequence, uuid);
614*4882a593Smuzhiyun break;
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun default:
617*4882a593Smuzhiyun tb_xdp_error_response(ctl, route, sequence,
618*4882a593Smuzhiyun ERROR_NOT_SUPPORTED);
619*4882a593Smuzhiyun break;
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun if (ret) {
623*4882a593Smuzhiyun tb_warn(tb, "failed to send XDomain response for %#x\n",
624*4882a593Smuzhiyun pkg->type);
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun out:
628*4882a593Smuzhiyun kfree(xw->pkg);
629*4882a593Smuzhiyun kfree(xw);
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun tb_domain_put(tb);
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun static bool
tb_xdp_schedule_request(struct tb * tb,const struct tb_xdp_header * hdr,size_t size)635*4882a593Smuzhiyun tb_xdp_schedule_request(struct tb *tb, const struct tb_xdp_header *hdr,
636*4882a593Smuzhiyun size_t size)
637*4882a593Smuzhiyun {
638*4882a593Smuzhiyun struct xdomain_request_work *xw;
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun xw = kmalloc(sizeof(*xw), GFP_KERNEL);
641*4882a593Smuzhiyun if (!xw)
642*4882a593Smuzhiyun return false;
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun INIT_WORK(&xw->work, tb_xdp_handle_request);
645*4882a593Smuzhiyun xw->pkg = kmemdup(hdr, size, GFP_KERNEL);
646*4882a593Smuzhiyun if (!xw->pkg) {
647*4882a593Smuzhiyun kfree(xw);
648*4882a593Smuzhiyun return false;
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun xw->tb = tb_domain_get(tb);
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun schedule_work(&xw->work);
653*4882a593Smuzhiyun return true;
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun
656*4882a593Smuzhiyun /**
657*4882a593Smuzhiyun * tb_register_service_driver() - Register XDomain service driver
658*4882a593Smuzhiyun * @drv: Driver to register
659*4882a593Smuzhiyun *
660*4882a593Smuzhiyun * Registers new service driver from @drv to the bus.
661*4882a593Smuzhiyun */
tb_register_service_driver(struct tb_service_driver * drv)662*4882a593Smuzhiyun int tb_register_service_driver(struct tb_service_driver *drv)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun drv->driver.bus = &tb_bus_type;
665*4882a593Smuzhiyun return driver_register(&drv->driver);
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_register_service_driver);
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun /**
670*4882a593Smuzhiyun * tb_unregister_service_driver() - Unregister XDomain service driver
671*4882a593Smuzhiyun * @xdrv: Driver to unregister
672*4882a593Smuzhiyun *
673*4882a593Smuzhiyun * Unregisters XDomain service driver from the bus.
674*4882a593Smuzhiyun */
tb_unregister_service_driver(struct tb_service_driver * drv)675*4882a593Smuzhiyun void tb_unregister_service_driver(struct tb_service_driver *drv)
676*4882a593Smuzhiyun {
677*4882a593Smuzhiyun driver_unregister(&drv->driver);
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_unregister_service_driver);
680*4882a593Smuzhiyun
key_show(struct device * dev,struct device_attribute * attr,char * buf)681*4882a593Smuzhiyun static ssize_t key_show(struct device *dev, struct device_attribute *attr,
682*4882a593Smuzhiyun char *buf)
683*4882a593Smuzhiyun {
684*4882a593Smuzhiyun struct tb_service *svc = container_of(dev, struct tb_service, dev);
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun /*
687*4882a593Smuzhiyun * It should be null terminated but anything else is pretty much
688*4882a593Smuzhiyun * allowed.
689*4882a593Smuzhiyun */
690*4882a593Smuzhiyun return sprintf(buf, "%*pE\n", (int)strlen(svc->key), svc->key);
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun static DEVICE_ATTR_RO(key);
693*4882a593Smuzhiyun
get_modalias(struct tb_service * svc,char * buf,size_t size)694*4882a593Smuzhiyun static int get_modalias(struct tb_service *svc, char *buf, size_t size)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun return snprintf(buf, size, "tbsvc:k%sp%08Xv%08Xr%08X", svc->key,
697*4882a593Smuzhiyun svc->prtcid, svc->prtcvers, svc->prtcrevs);
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)700*4882a593Smuzhiyun static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
701*4882a593Smuzhiyun char *buf)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun struct tb_service *svc = container_of(dev, struct tb_service, dev);
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun /* Full buffer size except new line and null termination */
706*4882a593Smuzhiyun get_modalias(svc, buf, PAGE_SIZE - 2);
707*4882a593Smuzhiyun return sprintf(buf, "%s\n", buf);
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun static DEVICE_ATTR_RO(modalias);
710*4882a593Smuzhiyun
prtcid_show(struct device * dev,struct device_attribute * attr,char * buf)711*4882a593Smuzhiyun static ssize_t prtcid_show(struct device *dev, struct device_attribute *attr,
712*4882a593Smuzhiyun char *buf)
713*4882a593Smuzhiyun {
714*4882a593Smuzhiyun struct tb_service *svc = container_of(dev, struct tb_service, dev);
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun return sprintf(buf, "%u\n", svc->prtcid);
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun static DEVICE_ATTR_RO(prtcid);
719*4882a593Smuzhiyun
prtcvers_show(struct device * dev,struct device_attribute * attr,char * buf)720*4882a593Smuzhiyun static ssize_t prtcvers_show(struct device *dev, struct device_attribute *attr,
721*4882a593Smuzhiyun char *buf)
722*4882a593Smuzhiyun {
723*4882a593Smuzhiyun struct tb_service *svc = container_of(dev, struct tb_service, dev);
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun return sprintf(buf, "%u\n", svc->prtcvers);
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun static DEVICE_ATTR_RO(prtcvers);
728*4882a593Smuzhiyun
prtcrevs_show(struct device * dev,struct device_attribute * attr,char * buf)729*4882a593Smuzhiyun static ssize_t prtcrevs_show(struct device *dev, struct device_attribute *attr,
730*4882a593Smuzhiyun char *buf)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun struct tb_service *svc = container_of(dev, struct tb_service, dev);
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun return sprintf(buf, "%u\n", svc->prtcrevs);
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun static DEVICE_ATTR_RO(prtcrevs);
737*4882a593Smuzhiyun
prtcstns_show(struct device * dev,struct device_attribute * attr,char * buf)738*4882a593Smuzhiyun static ssize_t prtcstns_show(struct device *dev, struct device_attribute *attr,
739*4882a593Smuzhiyun char *buf)
740*4882a593Smuzhiyun {
741*4882a593Smuzhiyun struct tb_service *svc = container_of(dev, struct tb_service, dev);
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun return sprintf(buf, "0x%08x\n", svc->prtcstns);
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun static DEVICE_ATTR_RO(prtcstns);
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun static struct attribute *tb_service_attrs[] = {
748*4882a593Smuzhiyun &dev_attr_key.attr,
749*4882a593Smuzhiyun &dev_attr_modalias.attr,
750*4882a593Smuzhiyun &dev_attr_prtcid.attr,
751*4882a593Smuzhiyun &dev_attr_prtcvers.attr,
752*4882a593Smuzhiyun &dev_attr_prtcrevs.attr,
753*4882a593Smuzhiyun &dev_attr_prtcstns.attr,
754*4882a593Smuzhiyun NULL,
755*4882a593Smuzhiyun };
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun static struct attribute_group tb_service_attr_group = {
758*4882a593Smuzhiyun .attrs = tb_service_attrs,
759*4882a593Smuzhiyun };
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun static const struct attribute_group *tb_service_attr_groups[] = {
762*4882a593Smuzhiyun &tb_service_attr_group,
763*4882a593Smuzhiyun NULL,
764*4882a593Smuzhiyun };
765*4882a593Smuzhiyun
tb_service_uevent(struct device * dev,struct kobj_uevent_env * env)766*4882a593Smuzhiyun static int tb_service_uevent(struct device *dev, struct kobj_uevent_env *env)
767*4882a593Smuzhiyun {
768*4882a593Smuzhiyun struct tb_service *svc = container_of(dev, struct tb_service, dev);
769*4882a593Smuzhiyun char modalias[64];
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun get_modalias(svc, modalias, sizeof(modalias));
772*4882a593Smuzhiyun return add_uevent_var(env, "MODALIAS=%s", modalias);
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun
tb_service_release(struct device * dev)775*4882a593Smuzhiyun static void tb_service_release(struct device *dev)
776*4882a593Smuzhiyun {
777*4882a593Smuzhiyun struct tb_service *svc = container_of(dev, struct tb_service, dev);
778*4882a593Smuzhiyun struct tb_xdomain *xd = tb_service_parent(svc);
779*4882a593Smuzhiyun
780*4882a593Smuzhiyun ida_simple_remove(&xd->service_ids, svc->id);
781*4882a593Smuzhiyun kfree(svc->key);
782*4882a593Smuzhiyun kfree(svc);
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun struct device_type tb_service_type = {
786*4882a593Smuzhiyun .name = "thunderbolt_service",
787*4882a593Smuzhiyun .groups = tb_service_attr_groups,
788*4882a593Smuzhiyun .uevent = tb_service_uevent,
789*4882a593Smuzhiyun .release = tb_service_release,
790*4882a593Smuzhiyun };
791*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_service_type);
792*4882a593Smuzhiyun
remove_missing_service(struct device * dev,void * data)793*4882a593Smuzhiyun static int remove_missing_service(struct device *dev, void *data)
794*4882a593Smuzhiyun {
795*4882a593Smuzhiyun struct tb_xdomain *xd = data;
796*4882a593Smuzhiyun struct tb_service *svc;
797*4882a593Smuzhiyun
798*4882a593Smuzhiyun svc = tb_to_service(dev);
799*4882a593Smuzhiyun if (!svc)
800*4882a593Smuzhiyun return 0;
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun if (!tb_property_find(xd->properties, svc->key,
803*4882a593Smuzhiyun TB_PROPERTY_TYPE_DIRECTORY))
804*4882a593Smuzhiyun device_unregister(dev);
805*4882a593Smuzhiyun
806*4882a593Smuzhiyun return 0;
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun
find_service(struct device * dev,void * data)809*4882a593Smuzhiyun static int find_service(struct device *dev, void *data)
810*4882a593Smuzhiyun {
811*4882a593Smuzhiyun const struct tb_property *p = data;
812*4882a593Smuzhiyun struct tb_service *svc;
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun svc = tb_to_service(dev);
815*4882a593Smuzhiyun if (!svc)
816*4882a593Smuzhiyun return 0;
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun return !strcmp(svc->key, p->key);
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun
populate_service(struct tb_service * svc,struct tb_property * property)821*4882a593Smuzhiyun static int populate_service(struct tb_service *svc,
822*4882a593Smuzhiyun struct tb_property *property)
823*4882a593Smuzhiyun {
824*4882a593Smuzhiyun struct tb_property_dir *dir = property->value.dir;
825*4882a593Smuzhiyun struct tb_property *p;
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun /* Fill in standard properties */
828*4882a593Smuzhiyun p = tb_property_find(dir, "prtcid", TB_PROPERTY_TYPE_VALUE);
829*4882a593Smuzhiyun if (p)
830*4882a593Smuzhiyun svc->prtcid = p->value.immediate;
831*4882a593Smuzhiyun p = tb_property_find(dir, "prtcvers", TB_PROPERTY_TYPE_VALUE);
832*4882a593Smuzhiyun if (p)
833*4882a593Smuzhiyun svc->prtcvers = p->value.immediate;
834*4882a593Smuzhiyun p = tb_property_find(dir, "prtcrevs", TB_PROPERTY_TYPE_VALUE);
835*4882a593Smuzhiyun if (p)
836*4882a593Smuzhiyun svc->prtcrevs = p->value.immediate;
837*4882a593Smuzhiyun p = tb_property_find(dir, "prtcstns", TB_PROPERTY_TYPE_VALUE);
838*4882a593Smuzhiyun if (p)
839*4882a593Smuzhiyun svc->prtcstns = p->value.immediate;
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun svc->key = kstrdup(property->key, GFP_KERNEL);
842*4882a593Smuzhiyun if (!svc->key)
843*4882a593Smuzhiyun return -ENOMEM;
844*4882a593Smuzhiyun
845*4882a593Smuzhiyun return 0;
846*4882a593Smuzhiyun }
847*4882a593Smuzhiyun
enumerate_services(struct tb_xdomain * xd)848*4882a593Smuzhiyun static void enumerate_services(struct tb_xdomain *xd)
849*4882a593Smuzhiyun {
850*4882a593Smuzhiyun struct tb_service *svc;
851*4882a593Smuzhiyun struct tb_property *p;
852*4882a593Smuzhiyun struct device *dev;
853*4882a593Smuzhiyun int id;
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun /*
856*4882a593Smuzhiyun * First remove all services that are not available anymore in
857*4882a593Smuzhiyun * the updated property block.
858*4882a593Smuzhiyun */
859*4882a593Smuzhiyun device_for_each_child_reverse(&xd->dev, xd, remove_missing_service);
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun /* Then re-enumerate properties creating new services as we go */
862*4882a593Smuzhiyun tb_property_for_each(xd->properties, p) {
863*4882a593Smuzhiyun if (p->type != TB_PROPERTY_TYPE_DIRECTORY)
864*4882a593Smuzhiyun continue;
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun /* If the service exists already we are fine */
867*4882a593Smuzhiyun dev = device_find_child(&xd->dev, p, find_service);
868*4882a593Smuzhiyun if (dev) {
869*4882a593Smuzhiyun put_device(dev);
870*4882a593Smuzhiyun continue;
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun svc = kzalloc(sizeof(*svc), GFP_KERNEL);
874*4882a593Smuzhiyun if (!svc)
875*4882a593Smuzhiyun break;
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun if (populate_service(svc, p)) {
878*4882a593Smuzhiyun kfree(svc);
879*4882a593Smuzhiyun break;
880*4882a593Smuzhiyun }
881*4882a593Smuzhiyun
882*4882a593Smuzhiyun id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
883*4882a593Smuzhiyun if (id < 0) {
884*4882a593Smuzhiyun kfree(svc->key);
885*4882a593Smuzhiyun kfree(svc);
886*4882a593Smuzhiyun break;
887*4882a593Smuzhiyun }
888*4882a593Smuzhiyun svc->id = id;
889*4882a593Smuzhiyun svc->dev.bus = &tb_bus_type;
890*4882a593Smuzhiyun svc->dev.type = &tb_service_type;
891*4882a593Smuzhiyun svc->dev.parent = &xd->dev;
892*4882a593Smuzhiyun dev_set_name(&svc->dev, "%s.%d", dev_name(&xd->dev), svc->id);
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun if (device_register(&svc->dev)) {
895*4882a593Smuzhiyun put_device(&svc->dev);
896*4882a593Smuzhiyun break;
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun }
900*4882a593Smuzhiyun
populate_properties(struct tb_xdomain * xd,struct tb_property_dir * dir)901*4882a593Smuzhiyun static int populate_properties(struct tb_xdomain *xd,
902*4882a593Smuzhiyun struct tb_property_dir *dir)
903*4882a593Smuzhiyun {
904*4882a593Smuzhiyun const struct tb_property *p;
905*4882a593Smuzhiyun
906*4882a593Smuzhiyun /* Required properties */
907*4882a593Smuzhiyun p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_VALUE);
908*4882a593Smuzhiyun if (!p)
909*4882a593Smuzhiyun return -EINVAL;
910*4882a593Smuzhiyun xd->device = p->value.immediate;
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_VALUE);
913*4882a593Smuzhiyun if (!p)
914*4882a593Smuzhiyun return -EINVAL;
915*4882a593Smuzhiyun xd->vendor = p->value.immediate;
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun kfree(xd->device_name);
918*4882a593Smuzhiyun xd->device_name = NULL;
919*4882a593Smuzhiyun kfree(xd->vendor_name);
920*4882a593Smuzhiyun xd->vendor_name = NULL;
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun /* Optional properties */
923*4882a593Smuzhiyun p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_TEXT);
924*4882a593Smuzhiyun if (p)
925*4882a593Smuzhiyun xd->device_name = kstrdup(p->value.text, GFP_KERNEL);
926*4882a593Smuzhiyun p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_TEXT);
927*4882a593Smuzhiyun if (p)
928*4882a593Smuzhiyun xd->vendor_name = kstrdup(p->value.text, GFP_KERNEL);
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun return 0;
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun
933*4882a593Smuzhiyun /* Called with @xd->lock held */
tb_xdomain_restore_paths(struct tb_xdomain * xd)934*4882a593Smuzhiyun static void tb_xdomain_restore_paths(struct tb_xdomain *xd)
935*4882a593Smuzhiyun {
936*4882a593Smuzhiyun if (!xd->resume)
937*4882a593Smuzhiyun return;
938*4882a593Smuzhiyun
939*4882a593Smuzhiyun xd->resume = false;
940*4882a593Smuzhiyun if (xd->transmit_path) {
941*4882a593Smuzhiyun dev_dbg(&xd->dev, "re-establishing DMA path\n");
942*4882a593Smuzhiyun tb_domain_approve_xdomain_paths(xd->tb, xd);
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun }
945*4882a593Smuzhiyun
tb_xdomain_get_uuid(struct work_struct * work)946*4882a593Smuzhiyun static void tb_xdomain_get_uuid(struct work_struct *work)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun struct tb_xdomain *xd = container_of(work, typeof(*xd),
949*4882a593Smuzhiyun get_uuid_work.work);
950*4882a593Smuzhiyun struct tb *tb = xd->tb;
951*4882a593Smuzhiyun uuid_t uuid;
952*4882a593Smuzhiyun int ret;
953*4882a593Smuzhiyun
954*4882a593Smuzhiyun ret = tb_xdp_uuid_request(tb->ctl, xd->route, xd->uuid_retries, &uuid);
955*4882a593Smuzhiyun if (ret < 0) {
956*4882a593Smuzhiyun if (xd->uuid_retries-- > 0) {
957*4882a593Smuzhiyun queue_delayed_work(xd->tb->wq, &xd->get_uuid_work,
958*4882a593Smuzhiyun msecs_to_jiffies(100));
959*4882a593Smuzhiyun } else {
960*4882a593Smuzhiyun dev_dbg(&xd->dev, "failed to read remote UUID\n");
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun return;
963*4882a593Smuzhiyun }
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun if (uuid_equal(&uuid, xd->local_uuid)) {
966*4882a593Smuzhiyun dev_dbg(&xd->dev, "intra-domain loop detected\n");
967*4882a593Smuzhiyun return;
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun
970*4882a593Smuzhiyun /*
971*4882a593Smuzhiyun * If the UUID is different, there is another domain connected
972*4882a593Smuzhiyun * so mark this one unplugged and wait for the connection
973*4882a593Smuzhiyun * manager to replace it.
974*4882a593Smuzhiyun */
975*4882a593Smuzhiyun if (xd->remote_uuid && !uuid_equal(&uuid, xd->remote_uuid)) {
976*4882a593Smuzhiyun dev_dbg(&xd->dev, "remote UUID is different, unplugging\n");
977*4882a593Smuzhiyun xd->is_unplugged = true;
978*4882a593Smuzhiyun return;
979*4882a593Smuzhiyun }
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun /* First time fill in the missing UUID */
982*4882a593Smuzhiyun if (!xd->remote_uuid) {
983*4882a593Smuzhiyun xd->remote_uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL);
984*4882a593Smuzhiyun if (!xd->remote_uuid)
985*4882a593Smuzhiyun return;
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun /* Now we can start the normal properties exchange */
989*4882a593Smuzhiyun queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
990*4882a593Smuzhiyun msecs_to_jiffies(100));
991*4882a593Smuzhiyun queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
992*4882a593Smuzhiyun msecs_to_jiffies(1000));
993*4882a593Smuzhiyun }
994*4882a593Smuzhiyun
tb_xdomain_get_properties(struct work_struct * work)995*4882a593Smuzhiyun static void tb_xdomain_get_properties(struct work_struct *work)
996*4882a593Smuzhiyun {
997*4882a593Smuzhiyun struct tb_xdomain *xd = container_of(work, typeof(*xd),
998*4882a593Smuzhiyun get_properties_work.work);
999*4882a593Smuzhiyun struct tb_property_dir *dir;
1000*4882a593Smuzhiyun struct tb *tb = xd->tb;
1001*4882a593Smuzhiyun bool update = false;
1002*4882a593Smuzhiyun u32 *block = NULL;
1003*4882a593Smuzhiyun u32 gen = 0;
1004*4882a593Smuzhiyun int ret;
1005*4882a593Smuzhiyun
1006*4882a593Smuzhiyun ret = tb_xdp_properties_request(tb->ctl, xd->route, xd->local_uuid,
1007*4882a593Smuzhiyun xd->remote_uuid, xd->properties_retries,
1008*4882a593Smuzhiyun &block, &gen);
1009*4882a593Smuzhiyun if (ret < 0) {
1010*4882a593Smuzhiyun if (xd->properties_retries-- > 0) {
1011*4882a593Smuzhiyun queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
1012*4882a593Smuzhiyun msecs_to_jiffies(1000));
1013*4882a593Smuzhiyun } else {
1014*4882a593Smuzhiyun /* Give up now */
1015*4882a593Smuzhiyun dev_err(&xd->dev,
1016*4882a593Smuzhiyun "failed read XDomain properties from %pUb\n",
1017*4882a593Smuzhiyun xd->remote_uuid);
1018*4882a593Smuzhiyun }
1019*4882a593Smuzhiyun return;
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
1023*4882a593Smuzhiyun
1024*4882a593Smuzhiyun mutex_lock(&xd->lock);
1025*4882a593Smuzhiyun
1026*4882a593Smuzhiyun /* Only accept newer generation properties */
1027*4882a593Smuzhiyun if (xd->properties && gen <= xd->property_block_gen) {
1028*4882a593Smuzhiyun /*
1029*4882a593Smuzhiyun * On resume it is likely that the properties block is
1030*4882a593Smuzhiyun * not changed (unless the other end added or removed
1031*4882a593Smuzhiyun * services). However, we need to make sure the existing
1032*4882a593Smuzhiyun * DMA paths are restored properly.
1033*4882a593Smuzhiyun */
1034*4882a593Smuzhiyun tb_xdomain_restore_paths(xd);
1035*4882a593Smuzhiyun goto err_free_block;
1036*4882a593Smuzhiyun }
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun dir = tb_property_parse_dir(block, ret);
1039*4882a593Smuzhiyun if (!dir) {
1040*4882a593Smuzhiyun dev_err(&xd->dev, "failed to parse XDomain properties\n");
1041*4882a593Smuzhiyun goto err_free_block;
1042*4882a593Smuzhiyun }
1043*4882a593Smuzhiyun
1044*4882a593Smuzhiyun ret = populate_properties(xd, dir);
1045*4882a593Smuzhiyun if (ret) {
1046*4882a593Smuzhiyun dev_err(&xd->dev, "missing XDomain properties in response\n");
1047*4882a593Smuzhiyun goto err_free_dir;
1048*4882a593Smuzhiyun }
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun /* Release the existing one */
1051*4882a593Smuzhiyun if (xd->properties) {
1052*4882a593Smuzhiyun tb_property_free_dir(xd->properties);
1053*4882a593Smuzhiyun update = true;
1054*4882a593Smuzhiyun }
1055*4882a593Smuzhiyun
1056*4882a593Smuzhiyun xd->properties = dir;
1057*4882a593Smuzhiyun xd->property_block_gen = gen;
1058*4882a593Smuzhiyun
1059*4882a593Smuzhiyun tb_xdomain_restore_paths(xd);
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun mutex_unlock(&xd->lock);
1062*4882a593Smuzhiyun
1063*4882a593Smuzhiyun kfree(block);
1064*4882a593Smuzhiyun
1065*4882a593Smuzhiyun /*
1066*4882a593Smuzhiyun * Now the device should be ready enough so we can add it to the
1067*4882a593Smuzhiyun * bus and let userspace know about it. If the device is already
1068*4882a593Smuzhiyun * registered, we notify the userspace that it has changed.
1069*4882a593Smuzhiyun */
1070*4882a593Smuzhiyun if (!update) {
1071*4882a593Smuzhiyun if (device_add(&xd->dev)) {
1072*4882a593Smuzhiyun dev_err(&xd->dev, "failed to add XDomain device\n");
1073*4882a593Smuzhiyun return;
1074*4882a593Smuzhiyun }
1075*4882a593Smuzhiyun } else {
1076*4882a593Smuzhiyun kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun enumerate_services(xd);
1080*4882a593Smuzhiyun return;
1081*4882a593Smuzhiyun
1082*4882a593Smuzhiyun err_free_dir:
1083*4882a593Smuzhiyun tb_property_free_dir(dir);
1084*4882a593Smuzhiyun err_free_block:
1085*4882a593Smuzhiyun kfree(block);
1086*4882a593Smuzhiyun mutex_unlock(&xd->lock);
1087*4882a593Smuzhiyun }
1088*4882a593Smuzhiyun
tb_xdomain_properties_changed(struct work_struct * work)1089*4882a593Smuzhiyun static void tb_xdomain_properties_changed(struct work_struct *work)
1090*4882a593Smuzhiyun {
1091*4882a593Smuzhiyun struct tb_xdomain *xd = container_of(work, typeof(*xd),
1092*4882a593Smuzhiyun properties_changed_work.work);
1093*4882a593Smuzhiyun int ret;
1094*4882a593Smuzhiyun
1095*4882a593Smuzhiyun ret = tb_xdp_properties_changed_request(xd->tb->ctl, xd->route,
1096*4882a593Smuzhiyun xd->properties_changed_retries, xd->local_uuid);
1097*4882a593Smuzhiyun if (ret) {
1098*4882a593Smuzhiyun if (xd->properties_changed_retries-- > 0)
1099*4882a593Smuzhiyun queue_delayed_work(xd->tb->wq,
1100*4882a593Smuzhiyun &xd->properties_changed_work,
1101*4882a593Smuzhiyun msecs_to_jiffies(1000));
1102*4882a593Smuzhiyun return;
1103*4882a593Smuzhiyun }
1104*4882a593Smuzhiyun
1105*4882a593Smuzhiyun xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
1106*4882a593Smuzhiyun }
1107*4882a593Smuzhiyun
device_show(struct device * dev,struct device_attribute * attr,char * buf)1108*4882a593Smuzhiyun static ssize_t device_show(struct device *dev, struct device_attribute *attr,
1109*4882a593Smuzhiyun char *buf)
1110*4882a593Smuzhiyun {
1111*4882a593Smuzhiyun struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1112*4882a593Smuzhiyun
1113*4882a593Smuzhiyun return sprintf(buf, "%#x\n", xd->device);
1114*4882a593Smuzhiyun }
1115*4882a593Smuzhiyun static DEVICE_ATTR_RO(device);
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun static ssize_t
device_name_show(struct device * dev,struct device_attribute * attr,char * buf)1118*4882a593Smuzhiyun device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1119*4882a593Smuzhiyun {
1120*4882a593Smuzhiyun struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1121*4882a593Smuzhiyun int ret;
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun if (mutex_lock_interruptible(&xd->lock))
1124*4882a593Smuzhiyun return -ERESTARTSYS;
1125*4882a593Smuzhiyun ret = sprintf(buf, "%s\n", xd->device_name ? xd->device_name : "");
1126*4882a593Smuzhiyun mutex_unlock(&xd->lock);
1127*4882a593Smuzhiyun
1128*4882a593Smuzhiyun return ret;
1129*4882a593Smuzhiyun }
1130*4882a593Smuzhiyun static DEVICE_ATTR_RO(device_name);
1131*4882a593Smuzhiyun
vendor_show(struct device * dev,struct device_attribute * attr,char * buf)1132*4882a593Smuzhiyun static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
1133*4882a593Smuzhiyun char *buf)
1134*4882a593Smuzhiyun {
1135*4882a593Smuzhiyun struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1136*4882a593Smuzhiyun
1137*4882a593Smuzhiyun return sprintf(buf, "%#x\n", xd->vendor);
1138*4882a593Smuzhiyun }
1139*4882a593Smuzhiyun static DEVICE_ATTR_RO(vendor);
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun static ssize_t
vendor_name_show(struct device * dev,struct device_attribute * attr,char * buf)1142*4882a593Smuzhiyun vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1143*4882a593Smuzhiyun {
1144*4882a593Smuzhiyun struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1145*4882a593Smuzhiyun int ret;
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun if (mutex_lock_interruptible(&xd->lock))
1148*4882a593Smuzhiyun return -ERESTARTSYS;
1149*4882a593Smuzhiyun ret = sprintf(buf, "%s\n", xd->vendor_name ? xd->vendor_name : "");
1150*4882a593Smuzhiyun mutex_unlock(&xd->lock);
1151*4882a593Smuzhiyun
1152*4882a593Smuzhiyun return ret;
1153*4882a593Smuzhiyun }
1154*4882a593Smuzhiyun static DEVICE_ATTR_RO(vendor_name);
1155*4882a593Smuzhiyun
unique_id_show(struct device * dev,struct device_attribute * attr,char * buf)1156*4882a593Smuzhiyun static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1157*4882a593Smuzhiyun char *buf)
1158*4882a593Smuzhiyun {
1159*4882a593Smuzhiyun struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun return sprintf(buf, "%pUb\n", xd->remote_uuid);
1162*4882a593Smuzhiyun }
1163*4882a593Smuzhiyun static DEVICE_ATTR_RO(unique_id);
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun static struct attribute *xdomain_attrs[] = {
1166*4882a593Smuzhiyun &dev_attr_device.attr,
1167*4882a593Smuzhiyun &dev_attr_device_name.attr,
1168*4882a593Smuzhiyun &dev_attr_unique_id.attr,
1169*4882a593Smuzhiyun &dev_attr_vendor.attr,
1170*4882a593Smuzhiyun &dev_attr_vendor_name.attr,
1171*4882a593Smuzhiyun NULL,
1172*4882a593Smuzhiyun };
1173*4882a593Smuzhiyun
1174*4882a593Smuzhiyun static struct attribute_group xdomain_attr_group = {
1175*4882a593Smuzhiyun .attrs = xdomain_attrs,
1176*4882a593Smuzhiyun };
1177*4882a593Smuzhiyun
1178*4882a593Smuzhiyun static const struct attribute_group *xdomain_attr_groups[] = {
1179*4882a593Smuzhiyun &xdomain_attr_group,
1180*4882a593Smuzhiyun NULL,
1181*4882a593Smuzhiyun };
1182*4882a593Smuzhiyun
tb_xdomain_release(struct device * dev)1183*4882a593Smuzhiyun static void tb_xdomain_release(struct device *dev)
1184*4882a593Smuzhiyun {
1185*4882a593Smuzhiyun struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1186*4882a593Smuzhiyun
1187*4882a593Smuzhiyun put_device(xd->dev.parent);
1188*4882a593Smuzhiyun
1189*4882a593Smuzhiyun tb_property_free_dir(xd->properties);
1190*4882a593Smuzhiyun ida_destroy(&xd->service_ids);
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun kfree(xd->local_uuid);
1193*4882a593Smuzhiyun kfree(xd->remote_uuid);
1194*4882a593Smuzhiyun kfree(xd->device_name);
1195*4882a593Smuzhiyun kfree(xd->vendor_name);
1196*4882a593Smuzhiyun kfree(xd);
1197*4882a593Smuzhiyun }
1198*4882a593Smuzhiyun
start_handshake(struct tb_xdomain * xd)1199*4882a593Smuzhiyun static void start_handshake(struct tb_xdomain *xd)
1200*4882a593Smuzhiyun {
1201*4882a593Smuzhiyun xd->uuid_retries = XDOMAIN_UUID_RETRIES;
1202*4882a593Smuzhiyun xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
1203*4882a593Smuzhiyun xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
1204*4882a593Smuzhiyun
1205*4882a593Smuzhiyun if (xd->needs_uuid) {
1206*4882a593Smuzhiyun queue_delayed_work(xd->tb->wq, &xd->get_uuid_work,
1207*4882a593Smuzhiyun msecs_to_jiffies(100));
1208*4882a593Smuzhiyun } else {
1209*4882a593Smuzhiyun /* Start exchanging properties with the other host */
1210*4882a593Smuzhiyun queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1211*4882a593Smuzhiyun msecs_to_jiffies(100));
1212*4882a593Smuzhiyun queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
1213*4882a593Smuzhiyun msecs_to_jiffies(1000));
1214*4882a593Smuzhiyun }
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun
stop_handshake(struct tb_xdomain * xd)1217*4882a593Smuzhiyun static void stop_handshake(struct tb_xdomain *xd)
1218*4882a593Smuzhiyun {
1219*4882a593Smuzhiyun xd->uuid_retries = 0;
1220*4882a593Smuzhiyun xd->properties_retries = 0;
1221*4882a593Smuzhiyun xd->properties_changed_retries = 0;
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun cancel_delayed_work_sync(&xd->get_uuid_work);
1224*4882a593Smuzhiyun cancel_delayed_work_sync(&xd->get_properties_work);
1225*4882a593Smuzhiyun cancel_delayed_work_sync(&xd->properties_changed_work);
1226*4882a593Smuzhiyun }
1227*4882a593Smuzhiyun
tb_xdomain_suspend(struct device * dev)1228*4882a593Smuzhiyun static int __maybe_unused tb_xdomain_suspend(struct device *dev)
1229*4882a593Smuzhiyun {
1230*4882a593Smuzhiyun stop_handshake(tb_to_xdomain(dev));
1231*4882a593Smuzhiyun return 0;
1232*4882a593Smuzhiyun }
1233*4882a593Smuzhiyun
tb_xdomain_resume(struct device * dev)1234*4882a593Smuzhiyun static int __maybe_unused tb_xdomain_resume(struct device *dev)
1235*4882a593Smuzhiyun {
1236*4882a593Smuzhiyun struct tb_xdomain *xd = tb_to_xdomain(dev);
1237*4882a593Smuzhiyun
1238*4882a593Smuzhiyun /*
1239*4882a593Smuzhiyun * Ask tb_xdomain_get_properties() restore any existing DMA
1240*4882a593Smuzhiyun * paths after properties are re-read.
1241*4882a593Smuzhiyun */
1242*4882a593Smuzhiyun xd->resume = true;
1243*4882a593Smuzhiyun start_handshake(xd);
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun return 0;
1246*4882a593Smuzhiyun }
1247*4882a593Smuzhiyun
1248*4882a593Smuzhiyun static const struct dev_pm_ops tb_xdomain_pm_ops = {
1249*4882a593Smuzhiyun SET_SYSTEM_SLEEP_PM_OPS(tb_xdomain_suspend, tb_xdomain_resume)
1250*4882a593Smuzhiyun };
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun struct device_type tb_xdomain_type = {
1253*4882a593Smuzhiyun .name = "thunderbolt_xdomain",
1254*4882a593Smuzhiyun .release = tb_xdomain_release,
1255*4882a593Smuzhiyun .pm = &tb_xdomain_pm_ops,
1256*4882a593Smuzhiyun };
1257*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_xdomain_type);
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun /**
1260*4882a593Smuzhiyun * tb_xdomain_alloc() - Allocate new XDomain object
1261*4882a593Smuzhiyun * @tb: Domain where the XDomain belongs
1262*4882a593Smuzhiyun * @parent: Parent device (the switch through the connection to the
1263*4882a593Smuzhiyun * other domain is reached).
1264*4882a593Smuzhiyun * @route: Route string used to reach the other domain
1265*4882a593Smuzhiyun * @local_uuid: Our local domain UUID
1266*4882a593Smuzhiyun * @remote_uuid: UUID of the other domain (optional)
1267*4882a593Smuzhiyun *
1268*4882a593Smuzhiyun * Allocates new XDomain structure and returns pointer to that. The
1269*4882a593Smuzhiyun * object must be released by calling tb_xdomain_put().
1270*4882a593Smuzhiyun */
tb_xdomain_alloc(struct tb * tb,struct device * parent,u64 route,const uuid_t * local_uuid,const uuid_t * remote_uuid)1271*4882a593Smuzhiyun struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
1272*4882a593Smuzhiyun u64 route, const uuid_t *local_uuid,
1273*4882a593Smuzhiyun const uuid_t *remote_uuid)
1274*4882a593Smuzhiyun {
1275*4882a593Smuzhiyun struct tb_switch *parent_sw = tb_to_switch(parent);
1276*4882a593Smuzhiyun struct tb_xdomain *xd;
1277*4882a593Smuzhiyun struct tb_port *down;
1278*4882a593Smuzhiyun
1279*4882a593Smuzhiyun /* Make sure the downstream domain is accessible */
1280*4882a593Smuzhiyun down = tb_port_at(route, parent_sw);
1281*4882a593Smuzhiyun tb_port_unlock(down);
1282*4882a593Smuzhiyun
1283*4882a593Smuzhiyun xd = kzalloc(sizeof(*xd), GFP_KERNEL);
1284*4882a593Smuzhiyun if (!xd)
1285*4882a593Smuzhiyun return NULL;
1286*4882a593Smuzhiyun
1287*4882a593Smuzhiyun xd->tb = tb;
1288*4882a593Smuzhiyun xd->route = route;
1289*4882a593Smuzhiyun ida_init(&xd->service_ids);
1290*4882a593Smuzhiyun mutex_init(&xd->lock);
1291*4882a593Smuzhiyun INIT_DELAYED_WORK(&xd->get_uuid_work, tb_xdomain_get_uuid);
1292*4882a593Smuzhiyun INIT_DELAYED_WORK(&xd->get_properties_work, tb_xdomain_get_properties);
1293*4882a593Smuzhiyun INIT_DELAYED_WORK(&xd->properties_changed_work,
1294*4882a593Smuzhiyun tb_xdomain_properties_changed);
1295*4882a593Smuzhiyun
1296*4882a593Smuzhiyun xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL);
1297*4882a593Smuzhiyun if (!xd->local_uuid)
1298*4882a593Smuzhiyun goto err_free;
1299*4882a593Smuzhiyun
1300*4882a593Smuzhiyun if (remote_uuid) {
1301*4882a593Smuzhiyun xd->remote_uuid = kmemdup(remote_uuid, sizeof(uuid_t),
1302*4882a593Smuzhiyun GFP_KERNEL);
1303*4882a593Smuzhiyun if (!xd->remote_uuid)
1304*4882a593Smuzhiyun goto err_free_local_uuid;
1305*4882a593Smuzhiyun } else {
1306*4882a593Smuzhiyun xd->needs_uuid = true;
1307*4882a593Smuzhiyun }
1308*4882a593Smuzhiyun
1309*4882a593Smuzhiyun device_initialize(&xd->dev);
1310*4882a593Smuzhiyun xd->dev.parent = get_device(parent);
1311*4882a593Smuzhiyun xd->dev.bus = &tb_bus_type;
1312*4882a593Smuzhiyun xd->dev.type = &tb_xdomain_type;
1313*4882a593Smuzhiyun xd->dev.groups = xdomain_attr_groups;
1314*4882a593Smuzhiyun dev_set_name(&xd->dev, "%u-%llx", tb->index, route);
1315*4882a593Smuzhiyun
1316*4882a593Smuzhiyun /*
1317*4882a593Smuzhiyun * This keeps the DMA powered on as long as we have active
1318*4882a593Smuzhiyun * connection to another host.
1319*4882a593Smuzhiyun */
1320*4882a593Smuzhiyun pm_runtime_set_active(&xd->dev);
1321*4882a593Smuzhiyun pm_runtime_get_noresume(&xd->dev);
1322*4882a593Smuzhiyun pm_runtime_enable(&xd->dev);
1323*4882a593Smuzhiyun
1324*4882a593Smuzhiyun return xd;
1325*4882a593Smuzhiyun
1326*4882a593Smuzhiyun err_free_local_uuid:
1327*4882a593Smuzhiyun kfree(xd->local_uuid);
1328*4882a593Smuzhiyun err_free:
1329*4882a593Smuzhiyun kfree(xd);
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun return NULL;
1332*4882a593Smuzhiyun }
1333*4882a593Smuzhiyun
1334*4882a593Smuzhiyun /**
1335*4882a593Smuzhiyun * tb_xdomain_add() - Add XDomain to the bus
1336*4882a593Smuzhiyun * @xd: XDomain to add
1337*4882a593Smuzhiyun *
1338*4882a593Smuzhiyun * This function starts XDomain discovery protocol handshake and
1339*4882a593Smuzhiyun * eventually adds the XDomain to the bus. After calling this function
1340*4882a593Smuzhiyun * the caller needs to call tb_xdomain_remove() in order to remove and
1341*4882a593Smuzhiyun * release the object regardless whether the handshake succeeded or not.
1342*4882a593Smuzhiyun */
tb_xdomain_add(struct tb_xdomain * xd)1343*4882a593Smuzhiyun void tb_xdomain_add(struct tb_xdomain *xd)
1344*4882a593Smuzhiyun {
1345*4882a593Smuzhiyun /* Start exchanging properties with the other host */
1346*4882a593Smuzhiyun start_handshake(xd);
1347*4882a593Smuzhiyun }
1348*4882a593Smuzhiyun
unregister_service(struct device * dev,void * data)1349*4882a593Smuzhiyun static int unregister_service(struct device *dev, void *data)
1350*4882a593Smuzhiyun {
1351*4882a593Smuzhiyun device_unregister(dev);
1352*4882a593Smuzhiyun return 0;
1353*4882a593Smuzhiyun }
1354*4882a593Smuzhiyun
1355*4882a593Smuzhiyun /**
1356*4882a593Smuzhiyun * tb_xdomain_remove() - Remove XDomain from the bus
1357*4882a593Smuzhiyun * @xd: XDomain to remove
1358*4882a593Smuzhiyun *
1359*4882a593Smuzhiyun * This will stop all ongoing configuration work and remove the XDomain
1360*4882a593Smuzhiyun * along with any services from the bus. When the last reference to @xd
1361*4882a593Smuzhiyun * is released the object will be released as well.
1362*4882a593Smuzhiyun */
tb_xdomain_remove(struct tb_xdomain * xd)1363*4882a593Smuzhiyun void tb_xdomain_remove(struct tb_xdomain *xd)
1364*4882a593Smuzhiyun {
1365*4882a593Smuzhiyun stop_handshake(xd);
1366*4882a593Smuzhiyun
1367*4882a593Smuzhiyun device_for_each_child_reverse(&xd->dev, xd, unregister_service);
1368*4882a593Smuzhiyun
1369*4882a593Smuzhiyun /*
1370*4882a593Smuzhiyun * Undo runtime PM here explicitly because it is possible that
1371*4882a593Smuzhiyun * the XDomain was never added to the bus and thus device_del()
1372*4882a593Smuzhiyun * is not called for it (device_del() would handle this otherwise).
1373*4882a593Smuzhiyun */
1374*4882a593Smuzhiyun pm_runtime_disable(&xd->dev);
1375*4882a593Smuzhiyun pm_runtime_put_noidle(&xd->dev);
1376*4882a593Smuzhiyun pm_runtime_set_suspended(&xd->dev);
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun if (!device_is_registered(&xd->dev))
1379*4882a593Smuzhiyun put_device(&xd->dev);
1380*4882a593Smuzhiyun else
1381*4882a593Smuzhiyun device_unregister(&xd->dev);
1382*4882a593Smuzhiyun }
1383*4882a593Smuzhiyun
1384*4882a593Smuzhiyun /**
1385*4882a593Smuzhiyun * tb_xdomain_enable_paths() - Enable DMA paths for XDomain connection
1386*4882a593Smuzhiyun * @xd: XDomain connection
1387*4882a593Smuzhiyun * @transmit_path: HopID of the transmit path the other end is using to
1388*4882a593Smuzhiyun * send packets
1389*4882a593Smuzhiyun * @transmit_ring: DMA ring used to receive packets from the other end
1390*4882a593Smuzhiyun * @receive_path: HopID of the receive path the other end is using to
1391*4882a593Smuzhiyun * receive packets
1392*4882a593Smuzhiyun * @receive_ring: DMA ring used to send packets to the other end
1393*4882a593Smuzhiyun *
1394*4882a593Smuzhiyun * The function enables DMA paths accordingly so that after successful
1395*4882a593Smuzhiyun * return the caller can send and receive packets using high-speed DMA
1396*4882a593Smuzhiyun * path.
1397*4882a593Smuzhiyun *
1398*4882a593Smuzhiyun * Return: %0 in case of success and negative errno in case of error
1399*4882a593Smuzhiyun */
tb_xdomain_enable_paths(struct tb_xdomain * xd,u16 transmit_path,u16 transmit_ring,u16 receive_path,u16 receive_ring)1400*4882a593Smuzhiyun int tb_xdomain_enable_paths(struct tb_xdomain *xd, u16 transmit_path,
1401*4882a593Smuzhiyun u16 transmit_ring, u16 receive_path,
1402*4882a593Smuzhiyun u16 receive_ring)
1403*4882a593Smuzhiyun {
1404*4882a593Smuzhiyun int ret;
1405*4882a593Smuzhiyun
1406*4882a593Smuzhiyun mutex_lock(&xd->lock);
1407*4882a593Smuzhiyun
1408*4882a593Smuzhiyun if (xd->transmit_path) {
1409*4882a593Smuzhiyun ret = xd->transmit_path == transmit_path ? 0 : -EBUSY;
1410*4882a593Smuzhiyun goto exit_unlock;
1411*4882a593Smuzhiyun }
1412*4882a593Smuzhiyun
1413*4882a593Smuzhiyun xd->transmit_path = transmit_path;
1414*4882a593Smuzhiyun xd->transmit_ring = transmit_ring;
1415*4882a593Smuzhiyun xd->receive_path = receive_path;
1416*4882a593Smuzhiyun xd->receive_ring = receive_ring;
1417*4882a593Smuzhiyun
1418*4882a593Smuzhiyun ret = tb_domain_approve_xdomain_paths(xd->tb, xd);
1419*4882a593Smuzhiyun
1420*4882a593Smuzhiyun exit_unlock:
1421*4882a593Smuzhiyun mutex_unlock(&xd->lock);
1422*4882a593Smuzhiyun
1423*4882a593Smuzhiyun return ret;
1424*4882a593Smuzhiyun }
1425*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_xdomain_enable_paths);
1426*4882a593Smuzhiyun
1427*4882a593Smuzhiyun /**
1428*4882a593Smuzhiyun * tb_xdomain_disable_paths() - Disable DMA paths for XDomain connection
1429*4882a593Smuzhiyun * @xd: XDomain connection
1430*4882a593Smuzhiyun *
1431*4882a593Smuzhiyun * This does the opposite of tb_xdomain_enable_paths(). After call to
1432*4882a593Smuzhiyun * this the caller is not expected to use the rings anymore.
1433*4882a593Smuzhiyun *
1434*4882a593Smuzhiyun * Return: %0 in case of success and negative errno in case of error
1435*4882a593Smuzhiyun */
tb_xdomain_disable_paths(struct tb_xdomain * xd)1436*4882a593Smuzhiyun int tb_xdomain_disable_paths(struct tb_xdomain *xd)
1437*4882a593Smuzhiyun {
1438*4882a593Smuzhiyun int ret = 0;
1439*4882a593Smuzhiyun
1440*4882a593Smuzhiyun mutex_lock(&xd->lock);
1441*4882a593Smuzhiyun if (xd->transmit_path) {
1442*4882a593Smuzhiyun xd->transmit_path = 0;
1443*4882a593Smuzhiyun xd->transmit_ring = 0;
1444*4882a593Smuzhiyun xd->receive_path = 0;
1445*4882a593Smuzhiyun xd->receive_ring = 0;
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun ret = tb_domain_disconnect_xdomain_paths(xd->tb, xd);
1448*4882a593Smuzhiyun }
1449*4882a593Smuzhiyun mutex_unlock(&xd->lock);
1450*4882a593Smuzhiyun
1451*4882a593Smuzhiyun return ret;
1452*4882a593Smuzhiyun }
1453*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths);
1454*4882a593Smuzhiyun
1455*4882a593Smuzhiyun struct tb_xdomain_lookup {
1456*4882a593Smuzhiyun const uuid_t *uuid;
1457*4882a593Smuzhiyun u8 link;
1458*4882a593Smuzhiyun u8 depth;
1459*4882a593Smuzhiyun u64 route;
1460*4882a593Smuzhiyun };
1461*4882a593Smuzhiyun
switch_find_xdomain(struct tb_switch * sw,const struct tb_xdomain_lookup * lookup)1462*4882a593Smuzhiyun static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw,
1463*4882a593Smuzhiyun const struct tb_xdomain_lookup *lookup)
1464*4882a593Smuzhiyun {
1465*4882a593Smuzhiyun struct tb_port *port;
1466*4882a593Smuzhiyun
1467*4882a593Smuzhiyun tb_switch_for_each_port(sw, port) {
1468*4882a593Smuzhiyun struct tb_xdomain *xd;
1469*4882a593Smuzhiyun
1470*4882a593Smuzhiyun if (port->xdomain) {
1471*4882a593Smuzhiyun xd = port->xdomain;
1472*4882a593Smuzhiyun
1473*4882a593Smuzhiyun if (lookup->uuid) {
1474*4882a593Smuzhiyun if (xd->remote_uuid &&
1475*4882a593Smuzhiyun uuid_equal(xd->remote_uuid, lookup->uuid))
1476*4882a593Smuzhiyun return xd;
1477*4882a593Smuzhiyun } else if (lookup->link &&
1478*4882a593Smuzhiyun lookup->link == xd->link &&
1479*4882a593Smuzhiyun lookup->depth == xd->depth) {
1480*4882a593Smuzhiyun return xd;
1481*4882a593Smuzhiyun } else if (lookup->route &&
1482*4882a593Smuzhiyun lookup->route == xd->route) {
1483*4882a593Smuzhiyun return xd;
1484*4882a593Smuzhiyun }
1485*4882a593Smuzhiyun } else if (tb_port_has_remote(port)) {
1486*4882a593Smuzhiyun xd = switch_find_xdomain(port->remote->sw, lookup);
1487*4882a593Smuzhiyun if (xd)
1488*4882a593Smuzhiyun return xd;
1489*4882a593Smuzhiyun }
1490*4882a593Smuzhiyun }
1491*4882a593Smuzhiyun
1492*4882a593Smuzhiyun return NULL;
1493*4882a593Smuzhiyun }
1494*4882a593Smuzhiyun
1495*4882a593Smuzhiyun /**
1496*4882a593Smuzhiyun * tb_xdomain_find_by_uuid() - Find an XDomain by UUID
1497*4882a593Smuzhiyun * @tb: Domain where the XDomain belongs to
1498*4882a593Smuzhiyun * @uuid: UUID to look for
1499*4882a593Smuzhiyun *
1500*4882a593Smuzhiyun * Finds XDomain by walking through the Thunderbolt topology below @tb.
1501*4882a593Smuzhiyun * The returned XDomain will have its reference count increased so the
1502*4882a593Smuzhiyun * caller needs to call tb_xdomain_put() when it is done with the
1503*4882a593Smuzhiyun * object.
1504*4882a593Smuzhiyun *
1505*4882a593Smuzhiyun * This will find all XDomains including the ones that are not yet added
1506*4882a593Smuzhiyun * to the bus (handshake is still in progress).
1507*4882a593Smuzhiyun *
1508*4882a593Smuzhiyun * The caller needs to hold @tb->lock.
1509*4882a593Smuzhiyun */
tb_xdomain_find_by_uuid(struct tb * tb,const uuid_t * uuid)1510*4882a593Smuzhiyun struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid)
1511*4882a593Smuzhiyun {
1512*4882a593Smuzhiyun struct tb_xdomain_lookup lookup;
1513*4882a593Smuzhiyun struct tb_xdomain *xd;
1514*4882a593Smuzhiyun
1515*4882a593Smuzhiyun memset(&lookup, 0, sizeof(lookup));
1516*4882a593Smuzhiyun lookup.uuid = uuid;
1517*4882a593Smuzhiyun
1518*4882a593Smuzhiyun xd = switch_find_xdomain(tb->root_switch, &lookup);
1519*4882a593Smuzhiyun return tb_xdomain_get(xd);
1520*4882a593Smuzhiyun }
1521*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_xdomain_find_by_uuid);
1522*4882a593Smuzhiyun
1523*4882a593Smuzhiyun /**
1524*4882a593Smuzhiyun * tb_xdomain_find_by_link_depth() - Find an XDomain by link and depth
1525*4882a593Smuzhiyun * @tb: Domain where the XDomain belongs to
1526*4882a593Smuzhiyun * @link: Root switch link number
1527*4882a593Smuzhiyun * @depth: Depth in the link
1528*4882a593Smuzhiyun *
1529*4882a593Smuzhiyun * Finds XDomain by walking through the Thunderbolt topology below @tb.
1530*4882a593Smuzhiyun * The returned XDomain will have its reference count increased so the
1531*4882a593Smuzhiyun * caller needs to call tb_xdomain_put() when it is done with the
1532*4882a593Smuzhiyun * object.
1533*4882a593Smuzhiyun *
1534*4882a593Smuzhiyun * This will find all XDomains including the ones that are not yet added
1535*4882a593Smuzhiyun * to the bus (handshake is still in progress).
1536*4882a593Smuzhiyun *
1537*4882a593Smuzhiyun * The caller needs to hold @tb->lock.
1538*4882a593Smuzhiyun */
tb_xdomain_find_by_link_depth(struct tb * tb,u8 link,u8 depth)1539*4882a593Smuzhiyun struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
1540*4882a593Smuzhiyun u8 depth)
1541*4882a593Smuzhiyun {
1542*4882a593Smuzhiyun struct tb_xdomain_lookup lookup;
1543*4882a593Smuzhiyun struct tb_xdomain *xd;
1544*4882a593Smuzhiyun
1545*4882a593Smuzhiyun memset(&lookup, 0, sizeof(lookup));
1546*4882a593Smuzhiyun lookup.link = link;
1547*4882a593Smuzhiyun lookup.depth = depth;
1548*4882a593Smuzhiyun
1549*4882a593Smuzhiyun xd = switch_find_xdomain(tb->root_switch, &lookup);
1550*4882a593Smuzhiyun return tb_xdomain_get(xd);
1551*4882a593Smuzhiyun }
1552*4882a593Smuzhiyun
1553*4882a593Smuzhiyun /**
1554*4882a593Smuzhiyun * tb_xdomain_find_by_route() - Find an XDomain by route string
1555*4882a593Smuzhiyun * @tb: Domain where the XDomain belongs to
1556*4882a593Smuzhiyun * @route: XDomain route string
1557*4882a593Smuzhiyun *
1558*4882a593Smuzhiyun * Finds XDomain by walking through the Thunderbolt topology below @tb.
1559*4882a593Smuzhiyun * The returned XDomain will have its reference count increased so the
1560*4882a593Smuzhiyun * caller needs to call tb_xdomain_put() when it is done with the
1561*4882a593Smuzhiyun * object.
1562*4882a593Smuzhiyun *
1563*4882a593Smuzhiyun * This will find all XDomains including the ones that are not yet added
1564*4882a593Smuzhiyun * to the bus (handshake is still in progress).
1565*4882a593Smuzhiyun *
1566*4882a593Smuzhiyun * The caller needs to hold @tb->lock.
1567*4882a593Smuzhiyun */
tb_xdomain_find_by_route(struct tb * tb,u64 route)1568*4882a593Smuzhiyun struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route)
1569*4882a593Smuzhiyun {
1570*4882a593Smuzhiyun struct tb_xdomain_lookup lookup;
1571*4882a593Smuzhiyun struct tb_xdomain *xd;
1572*4882a593Smuzhiyun
1573*4882a593Smuzhiyun memset(&lookup, 0, sizeof(lookup));
1574*4882a593Smuzhiyun lookup.route = route;
1575*4882a593Smuzhiyun
1576*4882a593Smuzhiyun xd = switch_find_xdomain(tb->root_switch, &lookup);
1577*4882a593Smuzhiyun return tb_xdomain_get(xd);
1578*4882a593Smuzhiyun }
1579*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_xdomain_find_by_route);
1580*4882a593Smuzhiyun
tb_xdomain_handle_request(struct tb * tb,enum tb_cfg_pkg_type type,const void * buf,size_t size)1581*4882a593Smuzhiyun bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
1582*4882a593Smuzhiyun const void *buf, size_t size)
1583*4882a593Smuzhiyun {
1584*4882a593Smuzhiyun const struct tb_protocol_handler *handler, *tmp;
1585*4882a593Smuzhiyun const struct tb_xdp_header *hdr = buf;
1586*4882a593Smuzhiyun unsigned int length;
1587*4882a593Smuzhiyun int ret = 0;
1588*4882a593Smuzhiyun
1589*4882a593Smuzhiyun /* We expect the packet is at least size of the header */
1590*4882a593Smuzhiyun length = hdr->xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
1591*4882a593Smuzhiyun if (length != size / 4 - sizeof(hdr->xd_hdr) / 4)
1592*4882a593Smuzhiyun return true;
1593*4882a593Smuzhiyun if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4)
1594*4882a593Smuzhiyun return true;
1595*4882a593Smuzhiyun
1596*4882a593Smuzhiyun /*
1597*4882a593Smuzhiyun * Handle XDomain discovery protocol packets directly here. For
1598*4882a593Smuzhiyun * other protocols (based on their UUID) we call registered
1599*4882a593Smuzhiyun * handlers in turn.
1600*4882a593Smuzhiyun */
1601*4882a593Smuzhiyun if (uuid_equal(&hdr->uuid, &tb_xdp_uuid)) {
1602*4882a593Smuzhiyun if (type == TB_CFG_PKG_XDOMAIN_REQ)
1603*4882a593Smuzhiyun return tb_xdp_schedule_request(tb, hdr, size);
1604*4882a593Smuzhiyun return false;
1605*4882a593Smuzhiyun }
1606*4882a593Smuzhiyun
1607*4882a593Smuzhiyun mutex_lock(&xdomain_lock);
1608*4882a593Smuzhiyun list_for_each_entry_safe(handler, tmp, &protocol_handlers, list) {
1609*4882a593Smuzhiyun if (!uuid_equal(&hdr->uuid, handler->uuid))
1610*4882a593Smuzhiyun continue;
1611*4882a593Smuzhiyun
1612*4882a593Smuzhiyun mutex_unlock(&xdomain_lock);
1613*4882a593Smuzhiyun ret = handler->callback(buf, size, handler->data);
1614*4882a593Smuzhiyun mutex_lock(&xdomain_lock);
1615*4882a593Smuzhiyun
1616*4882a593Smuzhiyun if (ret)
1617*4882a593Smuzhiyun break;
1618*4882a593Smuzhiyun }
1619*4882a593Smuzhiyun mutex_unlock(&xdomain_lock);
1620*4882a593Smuzhiyun
1621*4882a593Smuzhiyun return ret > 0;
1622*4882a593Smuzhiyun }
1623*4882a593Smuzhiyun
update_xdomain(struct device * dev,void * data)1624*4882a593Smuzhiyun static int update_xdomain(struct device *dev, void *data)
1625*4882a593Smuzhiyun {
1626*4882a593Smuzhiyun struct tb_xdomain *xd;
1627*4882a593Smuzhiyun
1628*4882a593Smuzhiyun xd = tb_to_xdomain(dev);
1629*4882a593Smuzhiyun if (xd) {
1630*4882a593Smuzhiyun queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1631*4882a593Smuzhiyun msecs_to_jiffies(50));
1632*4882a593Smuzhiyun }
1633*4882a593Smuzhiyun
1634*4882a593Smuzhiyun return 0;
1635*4882a593Smuzhiyun }
1636*4882a593Smuzhiyun
update_all_xdomains(void)1637*4882a593Smuzhiyun static void update_all_xdomains(void)
1638*4882a593Smuzhiyun {
1639*4882a593Smuzhiyun bus_for_each_dev(&tb_bus_type, NULL, NULL, update_xdomain);
1640*4882a593Smuzhiyun }
1641*4882a593Smuzhiyun
remove_directory(const char * key,const struct tb_property_dir * dir)1642*4882a593Smuzhiyun static bool remove_directory(const char *key, const struct tb_property_dir *dir)
1643*4882a593Smuzhiyun {
1644*4882a593Smuzhiyun struct tb_property *p;
1645*4882a593Smuzhiyun
1646*4882a593Smuzhiyun p = tb_property_find(xdomain_property_dir, key,
1647*4882a593Smuzhiyun TB_PROPERTY_TYPE_DIRECTORY);
1648*4882a593Smuzhiyun if (p && p->value.dir == dir) {
1649*4882a593Smuzhiyun tb_property_remove(p);
1650*4882a593Smuzhiyun return true;
1651*4882a593Smuzhiyun }
1652*4882a593Smuzhiyun return false;
1653*4882a593Smuzhiyun }
1654*4882a593Smuzhiyun
1655*4882a593Smuzhiyun /**
1656*4882a593Smuzhiyun * tb_register_property_dir() - Register property directory to the host
1657*4882a593Smuzhiyun * @key: Key (name) of the directory to add
1658*4882a593Smuzhiyun * @dir: Directory to add
1659*4882a593Smuzhiyun *
1660*4882a593Smuzhiyun * Service drivers can use this function to add new property directory
1661*4882a593Smuzhiyun * to the host available properties. The other connected hosts are
1662*4882a593Smuzhiyun * notified so they can re-read properties of this host if they are
1663*4882a593Smuzhiyun * interested.
1664*4882a593Smuzhiyun *
1665*4882a593Smuzhiyun * Return: %0 on success and negative errno on failure
1666*4882a593Smuzhiyun */
tb_register_property_dir(const char * key,struct tb_property_dir * dir)1667*4882a593Smuzhiyun int tb_register_property_dir(const char *key, struct tb_property_dir *dir)
1668*4882a593Smuzhiyun {
1669*4882a593Smuzhiyun int ret;
1670*4882a593Smuzhiyun
1671*4882a593Smuzhiyun if (WARN_ON(!xdomain_property_dir))
1672*4882a593Smuzhiyun return -EAGAIN;
1673*4882a593Smuzhiyun
1674*4882a593Smuzhiyun if (!key || strlen(key) > 8)
1675*4882a593Smuzhiyun return -EINVAL;
1676*4882a593Smuzhiyun
1677*4882a593Smuzhiyun mutex_lock(&xdomain_lock);
1678*4882a593Smuzhiyun if (tb_property_find(xdomain_property_dir, key,
1679*4882a593Smuzhiyun TB_PROPERTY_TYPE_DIRECTORY)) {
1680*4882a593Smuzhiyun ret = -EEXIST;
1681*4882a593Smuzhiyun goto err_unlock;
1682*4882a593Smuzhiyun }
1683*4882a593Smuzhiyun
1684*4882a593Smuzhiyun ret = tb_property_add_dir(xdomain_property_dir, key, dir);
1685*4882a593Smuzhiyun if (ret)
1686*4882a593Smuzhiyun goto err_unlock;
1687*4882a593Smuzhiyun
1688*4882a593Smuzhiyun ret = rebuild_property_block();
1689*4882a593Smuzhiyun if (ret) {
1690*4882a593Smuzhiyun remove_directory(key, dir);
1691*4882a593Smuzhiyun goto err_unlock;
1692*4882a593Smuzhiyun }
1693*4882a593Smuzhiyun
1694*4882a593Smuzhiyun mutex_unlock(&xdomain_lock);
1695*4882a593Smuzhiyun update_all_xdomains();
1696*4882a593Smuzhiyun return 0;
1697*4882a593Smuzhiyun
1698*4882a593Smuzhiyun err_unlock:
1699*4882a593Smuzhiyun mutex_unlock(&xdomain_lock);
1700*4882a593Smuzhiyun return ret;
1701*4882a593Smuzhiyun }
1702*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_register_property_dir);
1703*4882a593Smuzhiyun
1704*4882a593Smuzhiyun /**
1705*4882a593Smuzhiyun * tb_unregister_property_dir() - Removes property directory from host
1706*4882a593Smuzhiyun * @key: Key (name) of the directory
1707*4882a593Smuzhiyun * @dir: Directory to remove
1708*4882a593Smuzhiyun *
1709*4882a593Smuzhiyun * This will remove the existing directory from this host and notify the
1710*4882a593Smuzhiyun * connected hosts about the change.
1711*4882a593Smuzhiyun */
tb_unregister_property_dir(const char * key,struct tb_property_dir * dir)1712*4882a593Smuzhiyun void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir)
1713*4882a593Smuzhiyun {
1714*4882a593Smuzhiyun int ret = 0;
1715*4882a593Smuzhiyun
1716*4882a593Smuzhiyun mutex_lock(&xdomain_lock);
1717*4882a593Smuzhiyun if (remove_directory(key, dir))
1718*4882a593Smuzhiyun ret = rebuild_property_block();
1719*4882a593Smuzhiyun mutex_unlock(&xdomain_lock);
1720*4882a593Smuzhiyun
1721*4882a593Smuzhiyun if (!ret)
1722*4882a593Smuzhiyun update_all_xdomains();
1723*4882a593Smuzhiyun }
1724*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tb_unregister_property_dir);
1725*4882a593Smuzhiyun
tb_xdomain_init(void)1726*4882a593Smuzhiyun int tb_xdomain_init(void)
1727*4882a593Smuzhiyun {
1728*4882a593Smuzhiyun xdomain_property_dir = tb_property_create_dir(NULL);
1729*4882a593Smuzhiyun if (!xdomain_property_dir)
1730*4882a593Smuzhiyun return -ENOMEM;
1731*4882a593Smuzhiyun
1732*4882a593Smuzhiyun /*
1733*4882a593Smuzhiyun * Initialize standard set of properties without any service
1734*4882a593Smuzhiyun * directories. Those will be added by service drivers
1735*4882a593Smuzhiyun * themselves when they are loaded.
1736*4882a593Smuzhiyun *
1737*4882a593Smuzhiyun * We also add node name later when first connection is made.
1738*4882a593Smuzhiyun */
1739*4882a593Smuzhiyun tb_property_add_immediate(xdomain_property_dir, "vendorid",
1740*4882a593Smuzhiyun PCI_VENDOR_ID_INTEL);
1741*4882a593Smuzhiyun tb_property_add_text(xdomain_property_dir, "vendorid", "Intel Corp.");
1742*4882a593Smuzhiyun tb_property_add_immediate(xdomain_property_dir, "deviceid", 0x1);
1743*4882a593Smuzhiyun tb_property_add_immediate(xdomain_property_dir, "devicerv", 0x80000100);
1744*4882a593Smuzhiyun
1745*4882a593Smuzhiyun return 0;
1746*4882a593Smuzhiyun }
1747*4882a593Smuzhiyun
tb_xdomain_exit(void)1748*4882a593Smuzhiyun void tb_xdomain_exit(void)
1749*4882a593Smuzhiyun {
1750*4882a593Smuzhiyun kfree(xdomain_property_block);
1751*4882a593Smuzhiyun tb_property_free_dir(xdomain_property_dir);
1752*4882a593Smuzhiyun }
1753