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