gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / thunderbolt / xdomain.c
blob053f918e00e8bcf6b31e855f7a74ff2fcbd7db99
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Thunderbolt XDomain discovery protocol support
5 * Copyright (C) 2017, Intel Corporation
6 * Authors: Michael Jamet <michael.jamet@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
8 */
10 #include <linux/device.h>
11 #include <linux/kmod.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/utsname.h>
15 #include <linux/uuid.h>
16 #include <linux/workqueue.h>
18 #include "tb.h"
20 #define XDOMAIN_DEFAULT_TIMEOUT 5000 /* ms */
21 #define XDOMAIN_UUID_RETRIES 10
22 #define XDOMAIN_PROPERTIES_RETRIES 60
23 #define XDOMAIN_PROPERTIES_CHANGED_RETRIES 10
25 struct xdomain_request_work {
26 struct work_struct work;
27 struct tb_xdp_header *pkg;
28 struct tb *tb;
31 /* Serializes access to the properties and protocol handlers below */
32 static DEFINE_MUTEX(xdomain_lock);
34 /* Properties exposed to the remote domains */
35 static struct tb_property_dir *xdomain_property_dir;
36 static u32 *xdomain_property_block;
37 static u32 xdomain_property_block_len;
38 static u32 xdomain_property_block_gen;
40 /* Additional protocol handlers */
41 static LIST_HEAD(protocol_handlers);
43 /* UUID for XDomain discovery protocol: b638d70e-42ff-40bb-97c2-90e2c0b2ff07 */
44 static const uuid_t tb_xdp_uuid =
45 UUID_INIT(0xb638d70e, 0x42ff, 0x40bb,
46 0x97, 0xc2, 0x90, 0xe2, 0xc0, 0xb2, 0xff, 0x07);
48 static bool tb_xdomain_match(const struct tb_cfg_request *req,
49 const struct ctl_pkg *pkg)
51 switch (pkg->frame.eof) {
52 case TB_CFG_PKG_ERROR:
53 return true;
55 case TB_CFG_PKG_XDOMAIN_RESP: {
56 const struct tb_xdp_header *res_hdr = pkg->buffer;
57 const struct tb_xdp_header *req_hdr = req->request;
59 if (pkg->frame.size < req->response_size / 4)
60 return false;
62 /* Make sure route matches */
63 if ((res_hdr->xd_hdr.route_hi & ~BIT(31)) !=
64 req_hdr->xd_hdr.route_hi)
65 return false;
66 if ((res_hdr->xd_hdr.route_lo) != req_hdr->xd_hdr.route_lo)
67 return false;
69 /* Check that the XDomain protocol matches */
70 if (!uuid_equal(&res_hdr->uuid, &req_hdr->uuid))
71 return false;
73 return true;
76 default:
77 return false;
81 static bool tb_xdomain_copy(struct tb_cfg_request *req,
82 const struct ctl_pkg *pkg)
84 memcpy(req->response, pkg->buffer, req->response_size);
85 req->result.err = 0;
86 return true;
89 static void response_ready(void *data)
91 tb_cfg_request_put(data);
94 static int __tb_xdomain_response(struct tb_ctl *ctl, const void *response,
95 size_t size, enum tb_cfg_pkg_type type)
97 struct tb_cfg_request *req;
99 req = tb_cfg_request_alloc();
100 if (!req)
101 return -ENOMEM;
103 req->match = tb_xdomain_match;
104 req->copy = tb_xdomain_copy;
105 req->request = response;
106 req->request_size = size;
107 req->request_type = type;
109 return tb_cfg_request(ctl, req, response_ready, req);
113 * tb_xdomain_response() - Send a XDomain response message
114 * @xd: XDomain to send the message
115 * @response: Response to send
116 * @size: Size of the response
117 * @type: PDF type of the response
119 * This can be used to send a XDomain response message to the other
120 * domain. No response for the message is expected.
122 * Return: %0 in case of success and negative errno in case of failure
124 int tb_xdomain_response(struct tb_xdomain *xd, const void *response,
125 size_t size, enum tb_cfg_pkg_type type)
127 return __tb_xdomain_response(xd->tb->ctl, response, size, type);
129 EXPORT_SYMBOL_GPL(tb_xdomain_response);
131 static int __tb_xdomain_request(struct tb_ctl *ctl, const void *request,
132 size_t request_size, enum tb_cfg_pkg_type request_type, void *response,
133 size_t response_size, enum tb_cfg_pkg_type response_type,
134 unsigned int timeout_msec)
136 struct tb_cfg_request *req;
137 struct tb_cfg_result res;
139 req = tb_cfg_request_alloc();
140 if (!req)
141 return -ENOMEM;
143 req->match = tb_xdomain_match;
144 req->copy = tb_xdomain_copy;
145 req->request = request;
146 req->request_size = request_size;
147 req->request_type = request_type;
148 req->response = response;
149 req->response_size = response_size;
150 req->response_type = response_type;
152 res = tb_cfg_request_sync(ctl, req, timeout_msec);
154 tb_cfg_request_put(req);
156 return res.err == 1 ? -EIO : res.err;
160 * tb_xdomain_request() - Send a XDomain request
161 * @xd: XDomain to send the request
162 * @request: Request to send
163 * @request_size: Size of the request in bytes
164 * @request_type: PDF type of the request
165 * @response: Response is copied here
166 * @response_size: Expected size of the response in bytes
167 * @response_type: Expected PDF type of the response
168 * @timeout_msec: Timeout in milliseconds to wait for the response
170 * This function can be used to send XDomain control channel messages to
171 * the other domain. The function waits until the response is received
172 * or when timeout triggers. Whichever comes first.
174 * Return: %0 in case of success and negative errno in case of failure
176 int tb_xdomain_request(struct tb_xdomain *xd, const void *request,
177 size_t request_size, enum tb_cfg_pkg_type request_type,
178 void *response, size_t response_size,
179 enum tb_cfg_pkg_type response_type, unsigned int timeout_msec)
181 return __tb_xdomain_request(xd->tb->ctl, request, request_size,
182 request_type, response, response_size,
183 response_type, timeout_msec);
185 EXPORT_SYMBOL_GPL(tb_xdomain_request);
187 static inline void tb_xdp_fill_header(struct tb_xdp_header *hdr, u64 route,
188 u8 sequence, enum tb_xdp_type type, size_t size)
190 u32 length_sn;
192 length_sn = (size - sizeof(hdr->xd_hdr)) / 4;
193 length_sn |= (sequence << TB_XDOMAIN_SN_SHIFT) & TB_XDOMAIN_SN_MASK;
195 hdr->xd_hdr.route_hi = upper_32_bits(route);
196 hdr->xd_hdr.route_lo = lower_32_bits(route);
197 hdr->xd_hdr.length_sn = length_sn;
198 hdr->type = type;
199 memcpy(&hdr->uuid, &tb_xdp_uuid, sizeof(tb_xdp_uuid));
202 static int tb_xdp_handle_error(const struct tb_xdp_header *hdr)
204 const struct tb_xdp_error_response *error;
206 if (hdr->type != ERROR_RESPONSE)
207 return 0;
209 error = (const struct tb_xdp_error_response *)hdr;
211 switch (error->error) {
212 case ERROR_UNKNOWN_PACKET:
213 case ERROR_UNKNOWN_DOMAIN:
214 return -EIO;
215 case ERROR_NOT_SUPPORTED:
216 return -ENOTSUPP;
217 case ERROR_NOT_READY:
218 return -EAGAIN;
219 default:
220 break;
223 return 0;
226 static int tb_xdp_uuid_request(struct tb_ctl *ctl, u64 route, int retry,
227 uuid_t *uuid)
229 struct tb_xdp_uuid_response res;
230 struct tb_xdp_uuid req;
231 int ret;
233 memset(&req, 0, sizeof(req));
234 tb_xdp_fill_header(&req.hdr, route, retry % 4, UUID_REQUEST,
235 sizeof(req));
237 memset(&res, 0, sizeof(res));
238 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
239 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
240 TB_CFG_PKG_XDOMAIN_RESP,
241 XDOMAIN_DEFAULT_TIMEOUT);
242 if (ret)
243 return ret;
245 ret = tb_xdp_handle_error(&res.hdr);
246 if (ret)
247 return ret;
249 uuid_copy(uuid, &res.src_uuid);
250 return 0;
253 static int tb_xdp_uuid_response(struct tb_ctl *ctl, u64 route, u8 sequence,
254 const uuid_t *uuid)
256 struct tb_xdp_uuid_response res;
258 memset(&res, 0, sizeof(res));
259 tb_xdp_fill_header(&res.hdr, route, sequence, UUID_RESPONSE,
260 sizeof(res));
262 uuid_copy(&res.src_uuid, uuid);
263 res.src_route_hi = upper_32_bits(route);
264 res.src_route_lo = lower_32_bits(route);
266 return __tb_xdomain_response(ctl, &res, sizeof(res),
267 TB_CFG_PKG_XDOMAIN_RESP);
270 static int tb_xdp_error_response(struct tb_ctl *ctl, u64 route, u8 sequence,
271 enum tb_xdp_error error)
273 struct tb_xdp_error_response res;
275 memset(&res, 0, sizeof(res));
276 tb_xdp_fill_header(&res.hdr, route, sequence, ERROR_RESPONSE,
277 sizeof(res));
278 res.error = error;
280 return __tb_xdomain_response(ctl, &res, sizeof(res),
281 TB_CFG_PKG_XDOMAIN_RESP);
284 static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route,
285 const uuid_t *src_uuid, const uuid_t *dst_uuid, int retry,
286 u32 **block, u32 *generation)
288 struct tb_xdp_properties_response *res;
289 struct tb_xdp_properties req;
290 u16 data_len, len;
291 size_t total_size;
292 u32 *data = NULL;
293 int ret;
295 total_size = sizeof(*res) + TB_XDP_PROPERTIES_MAX_DATA_LENGTH * 4;
296 res = kzalloc(total_size, GFP_KERNEL);
297 if (!res)
298 return -ENOMEM;
300 memset(&req, 0, sizeof(req));
301 tb_xdp_fill_header(&req.hdr, route, retry % 4, PROPERTIES_REQUEST,
302 sizeof(req));
303 memcpy(&req.src_uuid, src_uuid, sizeof(*src_uuid));
304 memcpy(&req.dst_uuid, dst_uuid, sizeof(*dst_uuid));
306 len = 0;
307 data_len = 0;
309 do {
310 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
311 TB_CFG_PKG_XDOMAIN_REQ, res,
312 total_size, TB_CFG_PKG_XDOMAIN_RESP,
313 XDOMAIN_DEFAULT_TIMEOUT);
314 if (ret)
315 goto err;
317 ret = tb_xdp_handle_error(&res->hdr);
318 if (ret)
319 goto err;
322 * Package length includes the whole payload without the
323 * XDomain header. Validate first that the package is at
324 * least size of the response structure.
326 len = res->hdr.xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
327 if (len < sizeof(*res) / 4) {
328 ret = -EINVAL;
329 goto err;
332 len += sizeof(res->hdr.xd_hdr) / 4;
333 len -= sizeof(*res) / 4;
335 if (res->offset != req.offset) {
336 ret = -EINVAL;
337 goto err;
341 * First time allocate block that has enough space for
342 * the whole properties block.
344 if (!data) {
345 data_len = res->data_length;
346 if (data_len > TB_XDP_PROPERTIES_MAX_LENGTH) {
347 ret = -E2BIG;
348 goto err;
351 data = kcalloc(data_len, sizeof(u32), GFP_KERNEL);
352 if (!data) {
353 ret = -ENOMEM;
354 goto err;
358 memcpy(data + req.offset, res->data, len * 4);
359 req.offset += len;
360 } while (!data_len || req.offset < data_len);
362 *block = data;
363 *generation = res->generation;
365 kfree(res);
367 return data_len;
369 err:
370 kfree(data);
371 kfree(res);
373 return ret;
376 static int tb_xdp_properties_response(struct tb *tb, struct tb_ctl *ctl,
377 u64 route, u8 sequence, const uuid_t *src_uuid,
378 const struct tb_xdp_properties *req)
380 struct tb_xdp_properties_response *res;
381 size_t total_size;
382 u16 len;
383 int ret;
386 * Currently we expect all requests to be directed to us. The
387 * protocol supports forwarding, though which we might add
388 * support later on.
390 if (!uuid_equal(src_uuid, &req->dst_uuid)) {
391 tb_xdp_error_response(ctl, route, sequence,
392 ERROR_UNKNOWN_DOMAIN);
393 return 0;
396 mutex_lock(&xdomain_lock);
398 if (req->offset >= xdomain_property_block_len) {
399 mutex_unlock(&xdomain_lock);
400 return -EINVAL;
403 len = xdomain_property_block_len - req->offset;
404 len = min_t(u16, len, TB_XDP_PROPERTIES_MAX_DATA_LENGTH);
405 total_size = sizeof(*res) + len * 4;
407 res = kzalloc(total_size, GFP_KERNEL);
408 if (!res) {
409 mutex_unlock(&xdomain_lock);
410 return -ENOMEM;
413 tb_xdp_fill_header(&res->hdr, route, sequence, PROPERTIES_RESPONSE,
414 total_size);
415 res->generation = xdomain_property_block_gen;
416 res->data_length = xdomain_property_block_len;
417 res->offset = req->offset;
418 uuid_copy(&res->src_uuid, src_uuid);
419 uuid_copy(&res->dst_uuid, &req->src_uuid);
420 memcpy(res->data, &xdomain_property_block[req->offset], len * 4);
422 mutex_unlock(&xdomain_lock);
424 ret = __tb_xdomain_response(ctl, res, total_size,
425 TB_CFG_PKG_XDOMAIN_RESP);
427 kfree(res);
428 return ret;
431 static int tb_xdp_properties_changed_request(struct tb_ctl *ctl, u64 route,
432 int retry, const uuid_t *uuid)
434 struct tb_xdp_properties_changed_response res;
435 struct tb_xdp_properties_changed req;
436 int ret;
438 memset(&req, 0, sizeof(req));
439 tb_xdp_fill_header(&req.hdr, route, retry % 4,
440 PROPERTIES_CHANGED_REQUEST, sizeof(req));
441 uuid_copy(&req.src_uuid, uuid);
443 memset(&res, 0, sizeof(res));
444 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
445 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
446 TB_CFG_PKG_XDOMAIN_RESP,
447 XDOMAIN_DEFAULT_TIMEOUT);
448 if (ret)
449 return ret;
451 return tb_xdp_handle_error(&res.hdr);
454 static int
455 tb_xdp_properties_changed_response(struct tb_ctl *ctl, u64 route, u8 sequence)
457 struct tb_xdp_properties_changed_response res;
459 memset(&res, 0, sizeof(res));
460 tb_xdp_fill_header(&res.hdr, route, sequence,
461 PROPERTIES_CHANGED_RESPONSE, sizeof(res));
462 return __tb_xdomain_response(ctl, &res, sizeof(res),
463 TB_CFG_PKG_XDOMAIN_RESP);
467 * tb_register_protocol_handler() - Register protocol handler
468 * @handler: Handler to register
470 * This allows XDomain service drivers to hook into incoming XDomain
471 * messages. After this function is called the service driver needs to
472 * be able to handle calls to callback whenever a package with the
473 * registered protocol is received.
475 int tb_register_protocol_handler(struct tb_protocol_handler *handler)
477 if (!handler->uuid || !handler->callback)
478 return -EINVAL;
479 if (uuid_equal(handler->uuid, &tb_xdp_uuid))
480 return -EINVAL;
482 mutex_lock(&xdomain_lock);
483 list_add_tail(&handler->list, &protocol_handlers);
484 mutex_unlock(&xdomain_lock);
486 return 0;
488 EXPORT_SYMBOL_GPL(tb_register_protocol_handler);
491 * tb_unregister_protocol_handler() - Unregister protocol handler
492 * @handler: Handler to unregister
494 * Removes the previously registered protocol handler.
496 void tb_unregister_protocol_handler(struct tb_protocol_handler *handler)
498 mutex_lock(&xdomain_lock);
499 list_del_init(&handler->list);
500 mutex_unlock(&xdomain_lock);
502 EXPORT_SYMBOL_GPL(tb_unregister_protocol_handler);
504 static void tb_xdp_handle_request(struct work_struct *work)
506 struct xdomain_request_work *xw = container_of(work, typeof(*xw), work);
507 const struct tb_xdp_header *pkg = xw->pkg;
508 const struct tb_xdomain_header *xhdr = &pkg->xd_hdr;
509 struct tb *tb = xw->tb;
510 struct tb_ctl *ctl = tb->ctl;
511 const uuid_t *uuid;
512 int ret = 0;
513 u32 sequence;
514 u64 route;
516 route = ((u64)xhdr->route_hi << 32 | xhdr->route_lo) & ~BIT_ULL(63);
517 sequence = xhdr->length_sn & TB_XDOMAIN_SN_MASK;
518 sequence >>= TB_XDOMAIN_SN_SHIFT;
520 mutex_lock(&tb->lock);
521 if (tb->root_switch)
522 uuid = tb->root_switch->uuid;
523 else
524 uuid = NULL;
525 mutex_unlock(&tb->lock);
527 if (!uuid) {
528 tb_xdp_error_response(ctl, route, sequence, ERROR_NOT_READY);
529 goto out;
532 switch (pkg->type) {
533 case PROPERTIES_REQUEST:
534 ret = tb_xdp_properties_response(tb, ctl, route, sequence, uuid,
535 (const struct tb_xdp_properties *)pkg);
536 break;
538 case PROPERTIES_CHANGED_REQUEST: {
539 const struct tb_xdp_properties_changed *xchg =
540 (const struct tb_xdp_properties_changed *)pkg;
541 struct tb_xdomain *xd;
543 ret = tb_xdp_properties_changed_response(ctl, route, sequence);
546 * Since the properties have been changed, let's update
547 * the xdomain related to this connection as well in
548 * case there is a change in services it offers.
550 xd = tb_xdomain_find_by_uuid_locked(tb, &xchg->src_uuid);
551 if (xd) {
552 queue_delayed_work(tb->wq, &xd->get_properties_work,
553 msecs_to_jiffies(50));
554 tb_xdomain_put(xd);
557 break;
560 case UUID_REQUEST_OLD:
561 case UUID_REQUEST:
562 ret = tb_xdp_uuid_response(ctl, route, sequence, uuid);
563 break;
565 default:
566 tb_xdp_error_response(ctl, route, sequence,
567 ERROR_NOT_SUPPORTED);
568 break;
571 if (ret) {
572 tb_warn(tb, "failed to send XDomain response for %#x\n",
573 pkg->type);
576 out:
577 kfree(xw->pkg);
578 kfree(xw);
580 tb_domain_put(tb);
583 static bool
584 tb_xdp_schedule_request(struct tb *tb, const struct tb_xdp_header *hdr,
585 size_t size)
587 struct xdomain_request_work *xw;
589 xw = kmalloc(sizeof(*xw), GFP_KERNEL);
590 if (!xw)
591 return false;
593 INIT_WORK(&xw->work, tb_xdp_handle_request);
594 xw->pkg = kmemdup(hdr, size, GFP_KERNEL);
595 if (!xw->pkg) {
596 kfree(xw);
597 return false;
599 xw->tb = tb_domain_get(tb);
601 schedule_work(&xw->work);
602 return true;
606 * tb_register_service_driver() - Register XDomain service driver
607 * @drv: Driver to register
609 * Registers new service driver from @drv to the bus.
611 int tb_register_service_driver(struct tb_service_driver *drv)
613 drv->driver.bus = &tb_bus_type;
614 return driver_register(&drv->driver);
616 EXPORT_SYMBOL_GPL(tb_register_service_driver);
619 * tb_unregister_service_driver() - Unregister XDomain service driver
620 * @xdrv: Driver to unregister
622 * Unregisters XDomain service driver from the bus.
624 void tb_unregister_service_driver(struct tb_service_driver *drv)
626 driver_unregister(&drv->driver);
628 EXPORT_SYMBOL_GPL(tb_unregister_service_driver);
630 static ssize_t key_show(struct device *dev, struct device_attribute *attr,
631 char *buf)
633 struct tb_service *svc = container_of(dev, struct tb_service, dev);
636 * It should be null terminated but anything else is pretty much
637 * allowed.
639 return sprintf(buf, "%*pE\n", (int)strlen(svc->key), svc->key);
641 static DEVICE_ATTR_RO(key);
643 static int get_modalias(struct tb_service *svc, char *buf, size_t size)
645 return snprintf(buf, size, "tbsvc:k%sp%08Xv%08Xr%08X", svc->key,
646 svc->prtcid, svc->prtcvers, svc->prtcrevs);
649 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
650 char *buf)
652 struct tb_service *svc = container_of(dev, struct tb_service, dev);
654 /* Full buffer size except new line and null termination */
655 get_modalias(svc, buf, PAGE_SIZE - 2);
656 return sprintf(buf, "%s\n", buf);
658 static DEVICE_ATTR_RO(modalias);
660 static ssize_t prtcid_show(struct device *dev, struct device_attribute *attr,
661 char *buf)
663 struct tb_service *svc = container_of(dev, struct tb_service, dev);
665 return sprintf(buf, "%u\n", svc->prtcid);
667 static DEVICE_ATTR_RO(prtcid);
669 static ssize_t prtcvers_show(struct device *dev, struct device_attribute *attr,
670 char *buf)
672 struct tb_service *svc = container_of(dev, struct tb_service, dev);
674 return sprintf(buf, "%u\n", svc->prtcvers);
676 static DEVICE_ATTR_RO(prtcvers);
678 static ssize_t prtcrevs_show(struct device *dev, struct device_attribute *attr,
679 char *buf)
681 struct tb_service *svc = container_of(dev, struct tb_service, dev);
683 return sprintf(buf, "%u\n", svc->prtcrevs);
685 static DEVICE_ATTR_RO(prtcrevs);
687 static ssize_t prtcstns_show(struct device *dev, struct device_attribute *attr,
688 char *buf)
690 struct tb_service *svc = container_of(dev, struct tb_service, dev);
692 return sprintf(buf, "0x%08x\n", svc->prtcstns);
694 static DEVICE_ATTR_RO(prtcstns);
696 static struct attribute *tb_service_attrs[] = {
697 &dev_attr_key.attr,
698 &dev_attr_modalias.attr,
699 &dev_attr_prtcid.attr,
700 &dev_attr_prtcvers.attr,
701 &dev_attr_prtcrevs.attr,
702 &dev_attr_prtcstns.attr,
703 NULL,
706 static struct attribute_group tb_service_attr_group = {
707 .attrs = tb_service_attrs,
710 static const struct attribute_group *tb_service_attr_groups[] = {
711 &tb_service_attr_group,
712 NULL,
715 static int tb_service_uevent(struct device *dev, struct kobj_uevent_env *env)
717 struct tb_service *svc = container_of(dev, struct tb_service, dev);
718 char modalias[64];
720 get_modalias(svc, modalias, sizeof(modalias));
721 return add_uevent_var(env, "MODALIAS=%s", modalias);
724 static void tb_service_release(struct device *dev)
726 struct tb_service *svc = container_of(dev, struct tb_service, dev);
727 struct tb_xdomain *xd = tb_service_parent(svc);
729 ida_simple_remove(&xd->service_ids, svc->id);
730 kfree(svc->key);
731 kfree(svc);
734 struct device_type tb_service_type = {
735 .name = "thunderbolt_service",
736 .groups = tb_service_attr_groups,
737 .uevent = tb_service_uevent,
738 .release = tb_service_release,
740 EXPORT_SYMBOL_GPL(tb_service_type);
742 static int remove_missing_service(struct device *dev, void *data)
744 struct tb_xdomain *xd = data;
745 struct tb_service *svc;
747 svc = tb_to_service(dev);
748 if (!svc)
749 return 0;
751 if (!tb_property_find(xd->properties, svc->key,
752 TB_PROPERTY_TYPE_DIRECTORY))
753 device_unregister(dev);
755 return 0;
758 static int find_service(struct device *dev, void *data)
760 const struct tb_property *p = data;
761 struct tb_service *svc;
763 svc = tb_to_service(dev);
764 if (!svc)
765 return 0;
767 return !strcmp(svc->key, p->key);
770 static int populate_service(struct tb_service *svc,
771 struct tb_property *property)
773 struct tb_property_dir *dir = property->value.dir;
774 struct tb_property *p;
776 /* Fill in standard properties */
777 p = tb_property_find(dir, "prtcid", TB_PROPERTY_TYPE_VALUE);
778 if (p)
779 svc->prtcid = p->value.immediate;
780 p = tb_property_find(dir, "prtcvers", TB_PROPERTY_TYPE_VALUE);
781 if (p)
782 svc->prtcvers = p->value.immediate;
783 p = tb_property_find(dir, "prtcrevs", TB_PROPERTY_TYPE_VALUE);
784 if (p)
785 svc->prtcrevs = p->value.immediate;
786 p = tb_property_find(dir, "prtcstns", TB_PROPERTY_TYPE_VALUE);
787 if (p)
788 svc->prtcstns = p->value.immediate;
790 svc->key = kstrdup(property->key, GFP_KERNEL);
791 if (!svc->key)
792 return -ENOMEM;
794 return 0;
797 static void enumerate_services(struct tb_xdomain *xd)
799 struct tb_service *svc;
800 struct tb_property *p;
801 struct device *dev;
802 int id;
805 * First remove all services that are not available anymore in
806 * the updated property block.
808 device_for_each_child_reverse(&xd->dev, xd, remove_missing_service);
810 /* Then re-enumerate properties creating new services as we go */
811 tb_property_for_each(xd->properties, p) {
812 if (p->type != TB_PROPERTY_TYPE_DIRECTORY)
813 continue;
815 /* If the service exists already we are fine */
816 dev = device_find_child(&xd->dev, p, find_service);
817 if (dev) {
818 put_device(dev);
819 continue;
822 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
823 if (!svc)
824 break;
826 if (populate_service(svc, p)) {
827 kfree(svc);
828 break;
831 id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
832 if (id < 0) {
833 kfree(svc);
834 break;
836 svc->id = id;
837 svc->dev.bus = &tb_bus_type;
838 svc->dev.type = &tb_service_type;
839 svc->dev.parent = &xd->dev;
840 dev_set_name(&svc->dev, "%s.%d", dev_name(&xd->dev), svc->id);
842 if (device_register(&svc->dev)) {
843 put_device(&svc->dev);
844 break;
849 static int populate_properties(struct tb_xdomain *xd,
850 struct tb_property_dir *dir)
852 const struct tb_property *p;
854 /* Required properties */
855 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_VALUE);
856 if (!p)
857 return -EINVAL;
858 xd->device = p->value.immediate;
860 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_VALUE);
861 if (!p)
862 return -EINVAL;
863 xd->vendor = p->value.immediate;
865 kfree(xd->device_name);
866 xd->device_name = NULL;
867 kfree(xd->vendor_name);
868 xd->vendor_name = NULL;
870 /* Optional properties */
871 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_TEXT);
872 if (p)
873 xd->device_name = kstrdup(p->value.text, GFP_KERNEL);
874 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_TEXT);
875 if (p)
876 xd->vendor_name = kstrdup(p->value.text, GFP_KERNEL);
878 return 0;
881 /* Called with @xd->lock held */
882 static void tb_xdomain_restore_paths(struct tb_xdomain *xd)
884 if (!xd->resume)
885 return;
887 xd->resume = false;
888 if (xd->transmit_path) {
889 dev_dbg(&xd->dev, "re-establishing DMA path\n");
890 tb_domain_approve_xdomain_paths(xd->tb, xd);
894 static void tb_xdomain_get_uuid(struct work_struct *work)
896 struct tb_xdomain *xd = container_of(work, typeof(*xd),
897 get_uuid_work.work);
898 struct tb *tb = xd->tb;
899 uuid_t uuid;
900 int ret;
902 ret = tb_xdp_uuid_request(tb->ctl, xd->route, xd->uuid_retries, &uuid);
903 if (ret < 0) {
904 if (xd->uuid_retries-- > 0) {
905 queue_delayed_work(xd->tb->wq, &xd->get_uuid_work,
906 msecs_to_jiffies(100));
907 } else {
908 dev_dbg(&xd->dev, "failed to read remote UUID\n");
910 return;
913 if (uuid_equal(&uuid, xd->local_uuid)) {
914 dev_dbg(&xd->dev, "intra-domain loop detected\n");
915 return;
919 * If the UUID is different, there is another domain connected
920 * so mark this one unplugged and wait for the connection
921 * manager to replace it.
923 if (xd->remote_uuid && !uuid_equal(&uuid, xd->remote_uuid)) {
924 dev_dbg(&xd->dev, "remote UUID is different, unplugging\n");
925 xd->is_unplugged = true;
926 return;
929 /* First time fill in the missing UUID */
930 if (!xd->remote_uuid) {
931 xd->remote_uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL);
932 if (!xd->remote_uuid)
933 return;
936 /* Now we can start the normal properties exchange */
937 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
938 msecs_to_jiffies(100));
939 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
940 msecs_to_jiffies(1000));
943 static void tb_xdomain_get_properties(struct work_struct *work)
945 struct tb_xdomain *xd = container_of(work, typeof(*xd),
946 get_properties_work.work);
947 struct tb_property_dir *dir;
948 struct tb *tb = xd->tb;
949 bool update = false;
950 u32 *block = NULL;
951 u32 gen = 0;
952 int ret;
954 ret = tb_xdp_properties_request(tb->ctl, xd->route, xd->local_uuid,
955 xd->remote_uuid, xd->properties_retries,
956 &block, &gen);
957 if (ret < 0) {
958 if (xd->properties_retries-- > 0) {
959 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
960 msecs_to_jiffies(1000));
961 } else {
962 /* Give up now */
963 dev_err(&xd->dev,
964 "failed read XDomain properties from %pUb\n",
965 xd->remote_uuid);
967 return;
970 xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
972 mutex_lock(&xd->lock);
974 /* Only accept newer generation properties */
975 if (xd->properties && gen <= xd->property_block_gen) {
977 * On resume it is likely that the properties block is
978 * not changed (unless the other end added or removed
979 * services). However, we need to make sure the existing
980 * DMA paths are restored properly.
982 tb_xdomain_restore_paths(xd);
983 goto err_free_block;
986 dir = tb_property_parse_dir(block, ret);
987 if (!dir) {
988 dev_err(&xd->dev, "failed to parse XDomain properties\n");
989 goto err_free_block;
992 ret = populate_properties(xd, dir);
993 if (ret) {
994 dev_err(&xd->dev, "missing XDomain properties in response\n");
995 goto err_free_dir;
998 /* Release the existing one */
999 if (xd->properties) {
1000 tb_property_free_dir(xd->properties);
1001 update = true;
1004 xd->properties = dir;
1005 xd->property_block_gen = gen;
1007 tb_xdomain_restore_paths(xd);
1009 mutex_unlock(&xd->lock);
1011 kfree(block);
1014 * Now the device should be ready enough so we can add it to the
1015 * bus and let userspace know about it. If the device is already
1016 * registered, we notify the userspace that it has changed.
1018 if (!update) {
1019 if (device_add(&xd->dev)) {
1020 dev_err(&xd->dev, "failed to add XDomain device\n");
1021 return;
1023 } else {
1024 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
1027 enumerate_services(xd);
1028 return;
1030 err_free_dir:
1031 tb_property_free_dir(dir);
1032 err_free_block:
1033 kfree(block);
1034 mutex_unlock(&xd->lock);
1037 static void tb_xdomain_properties_changed(struct work_struct *work)
1039 struct tb_xdomain *xd = container_of(work, typeof(*xd),
1040 properties_changed_work.work);
1041 int ret;
1043 ret = tb_xdp_properties_changed_request(xd->tb->ctl, xd->route,
1044 xd->properties_changed_retries, xd->local_uuid);
1045 if (ret) {
1046 if (xd->properties_changed_retries-- > 0)
1047 queue_delayed_work(xd->tb->wq,
1048 &xd->properties_changed_work,
1049 msecs_to_jiffies(1000));
1050 return;
1053 xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
1056 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
1057 char *buf)
1059 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1061 return sprintf(buf, "%#x\n", xd->device);
1063 static DEVICE_ATTR_RO(device);
1065 static ssize_t
1066 device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1068 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1069 int ret;
1071 if (mutex_lock_interruptible(&xd->lock))
1072 return -ERESTARTSYS;
1073 ret = sprintf(buf, "%s\n", xd->device_name ? xd->device_name : "");
1074 mutex_unlock(&xd->lock);
1076 return ret;
1078 static DEVICE_ATTR_RO(device_name);
1080 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
1081 char *buf)
1083 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1085 return sprintf(buf, "%#x\n", xd->vendor);
1087 static DEVICE_ATTR_RO(vendor);
1089 static ssize_t
1090 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1092 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1093 int ret;
1095 if (mutex_lock_interruptible(&xd->lock))
1096 return -ERESTARTSYS;
1097 ret = sprintf(buf, "%s\n", xd->vendor_name ? xd->vendor_name : "");
1098 mutex_unlock(&xd->lock);
1100 return ret;
1102 static DEVICE_ATTR_RO(vendor_name);
1104 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1105 char *buf)
1107 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1109 return sprintf(buf, "%pUb\n", xd->remote_uuid);
1111 static DEVICE_ATTR_RO(unique_id);
1113 static struct attribute *xdomain_attrs[] = {
1114 &dev_attr_device.attr,
1115 &dev_attr_device_name.attr,
1116 &dev_attr_unique_id.attr,
1117 &dev_attr_vendor.attr,
1118 &dev_attr_vendor_name.attr,
1119 NULL,
1122 static struct attribute_group xdomain_attr_group = {
1123 .attrs = xdomain_attrs,
1126 static const struct attribute_group *xdomain_attr_groups[] = {
1127 &xdomain_attr_group,
1128 NULL,
1131 static void tb_xdomain_release(struct device *dev)
1133 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1135 put_device(xd->dev.parent);
1137 tb_property_free_dir(xd->properties);
1138 ida_destroy(&xd->service_ids);
1140 kfree(xd->local_uuid);
1141 kfree(xd->remote_uuid);
1142 kfree(xd->device_name);
1143 kfree(xd->vendor_name);
1144 kfree(xd);
1147 static void start_handshake(struct tb_xdomain *xd)
1149 xd->uuid_retries = XDOMAIN_UUID_RETRIES;
1150 xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
1151 xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
1153 if (xd->needs_uuid) {
1154 queue_delayed_work(xd->tb->wq, &xd->get_uuid_work,
1155 msecs_to_jiffies(100));
1156 } else {
1157 /* Start exchanging properties with the other host */
1158 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1159 msecs_to_jiffies(100));
1160 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
1161 msecs_to_jiffies(1000));
1165 static void stop_handshake(struct tb_xdomain *xd)
1167 xd->uuid_retries = 0;
1168 xd->properties_retries = 0;
1169 xd->properties_changed_retries = 0;
1171 cancel_delayed_work_sync(&xd->get_uuid_work);
1172 cancel_delayed_work_sync(&xd->get_properties_work);
1173 cancel_delayed_work_sync(&xd->properties_changed_work);
1176 static int __maybe_unused tb_xdomain_suspend(struct device *dev)
1178 stop_handshake(tb_to_xdomain(dev));
1179 return 0;
1182 static int __maybe_unused tb_xdomain_resume(struct device *dev)
1184 struct tb_xdomain *xd = tb_to_xdomain(dev);
1187 * Ask tb_xdomain_get_properties() restore any existing DMA
1188 * paths after properties are re-read.
1190 xd->resume = true;
1191 start_handshake(xd);
1193 return 0;
1196 static const struct dev_pm_ops tb_xdomain_pm_ops = {
1197 SET_SYSTEM_SLEEP_PM_OPS(tb_xdomain_suspend, tb_xdomain_resume)
1200 struct device_type tb_xdomain_type = {
1201 .name = "thunderbolt_xdomain",
1202 .release = tb_xdomain_release,
1203 .pm = &tb_xdomain_pm_ops,
1205 EXPORT_SYMBOL_GPL(tb_xdomain_type);
1208 * tb_xdomain_alloc() - Allocate new XDomain object
1209 * @tb: Domain where the XDomain belongs
1210 * @parent: Parent device (the switch through the connection to the
1211 * other domain is reached).
1212 * @route: Route string used to reach the other domain
1213 * @local_uuid: Our local domain UUID
1214 * @remote_uuid: UUID of the other domain (optional)
1216 * Allocates new XDomain structure and returns pointer to that. The
1217 * object must be released by calling tb_xdomain_put().
1219 struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
1220 u64 route, const uuid_t *local_uuid,
1221 const uuid_t *remote_uuid)
1223 struct tb_switch *parent_sw = tb_to_switch(parent);
1224 struct tb_xdomain *xd;
1225 struct tb_port *down;
1227 /* Make sure the downstream domain is accessible */
1228 down = tb_port_at(route, parent_sw);
1229 tb_port_unlock(down);
1231 xd = kzalloc(sizeof(*xd), GFP_KERNEL);
1232 if (!xd)
1233 return NULL;
1235 xd->tb = tb;
1236 xd->route = route;
1237 ida_init(&xd->service_ids);
1238 mutex_init(&xd->lock);
1239 INIT_DELAYED_WORK(&xd->get_uuid_work, tb_xdomain_get_uuid);
1240 INIT_DELAYED_WORK(&xd->get_properties_work, tb_xdomain_get_properties);
1241 INIT_DELAYED_WORK(&xd->properties_changed_work,
1242 tb_xdomain_properties_changed);
1244 xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL);
1245 if (!xd->local_uuid)
1246 goto err_free;
1248 if (remote_uuid) {
1249 xd->remote_uuid = kmemdup(remote_uuid, sizeof(uuid_t),
1250 GFP_KERNEL);
1251 if (!xd->remote_uuid)
1252 goto err_free_local_uuid;
1253 } else {
1254 xd->needs_uuid = true;
1257 device_initialize(&xd->dev);
1258 xd->dev.parent = get_device(parent);
1259 xd->dev.bus = &tb_bus_type;
1260 xd->dev.type = &tb_xdomain_type;
1261 xd->dev.groups = xdomain_attr_groups;
1262 dev_set_name(&xd->dev, "%u-%llx", tb->index, route);
1265 * This keeps the DMA powered on as long as we have active
1266 * connection to another host.
1268 pm_runtime_set_active(&xd->dev);
1269 pm_runtime_get_noresume(&xd->dev);
1270 pm_runtime_enable(&xd->dev);
1272 return xd;
1274 err_free_local_uuid:
1275 kfree(xd->local_uuid);
1276 err_free:
1277 kfree(xd);
1279 return NULL;
1283 * tb_xdomain_add() - Add XDomain to the bus
1284 * @xd: XDomain to add
1286 * This function starts XDomain discovery protocol handshake and
1287 * eventually adds the XDomain to the bus. After calling this function
1288 * the caller needs to call tb_xdomain_remove() in order to remove and
1289 * release the object regardless whether the handshake succeeded or not.
1291 void tb_xdomain_add(struct tb_xdomain *xd)
1293 /* Start exchanging properties with the other host */
1294 start_handshake(xd);
1297 static int unregister_service(struct device *dev, void *data)
1299 device_unregister(dev);
1300 return 0;
1304 * tb_xdomain_remove() - Remove XDomain from the bus
1305 * @xd: XDomain to remove
1307 * This will stop all ongoing configuration work and remove the XDomain
1308 * along with any services from the bus. When the last reference to @xd
1309 * is released the object will be released as well.
1311 void tb_xdomain_remove(struct tb_xdomain *xd)
1313 stop_handshake(xd);
1315 device_for_each_child_reverse(&xd->dev, xd, unregister_service);
1318 * Undo runtime PM here explicitly because it is possible that
1319 * the XDomain was never added to the bus and thus device_del()
1320 * is not called for it (device_del() would handle this otherwise).
1322 pm_runtime_disable(&xd->dev);
1323 pm_runtime_put_noidle(&xd->dev);
1324 pm_runtime_set_suspended(&xd->dev);
1326 if (!device_is_registered(&xd->dev))
1327 put_device(&xd->dev);
1328 else
1329 device_unregister(&xd->dev);
1333 * tb_xdomain_enable_paths() - Enable DMA paths for XDomain connection
1334 * @xd: XDomain connection
1335 * @transmit_path: HopID of the transmit path the other end is using to
1336 * send packets
1337 * @transmit_ring: DMA ring used to receive packets from the other end
1338 * @receive_path: HopID of the receive path the other end is using to
1339 * receive packets
1340 * @receive_ring: DMA ring used to send packets to the other end
1342 * The function enables DMA paths accordingly so that after successful
1343 * return the caller can send and receive packets using high-speed DMA
1344 * path.
1346 * Return: %0 in case of success and negative errno in case of error
1348 int tb_xdomain_enable_paths(struct tb_xdomain *xd, u16 transmit_path,
1349 u16 transmit_ring, u16 receive_path,
1350 u16 receive_ring)
1352 int ret;
1354 mutex_lock(&xd->lock);
1356 if (xd->transmit_path) {
1357 ret = xd->transmit_path == transmit_path ? 0 : -EBUSY;
1358 goto exit_unlock;
1361 xd->transmit_path = transmit_path;
1362 xd->transmit_ring = transmit_ring;
1363 xd->receive_path = receive_path;
1364 xd->receive_ring = receive_ring;
1366 ret = tb_domain_approve_xdomain_paths(xd->tb, xd);
1368 exit_unlock:
1369 mutex_unlock(&xd->lock);
1371 return ret;
1373 EXPORT_SYMBOL_GPL(tb_xdomain_enable_paths);
1376 * tb_xdomain_disable_paths() - Disable DMA paths for XDomain connection
1377 * @xd: XDomain connection
1379 * This does the opposite of tb_xdomain_enable_paths(). After call to
1380 * this the caller is not expected to use the rings anymore.
1382 * Return: %0 in case of success and negative errno in case of error
1384 int tb_xdomain_disable_paths(struct tb_xdomain *xd)
1386 int ret = 0;
1388 mutex_lock(&xd->lock);
1389 if (xd->transmit_path) {
1390 xd->transmit_path = 0;
1391 xd->transmit_ring = 0;
1392 xd->receive_path = 0;
1393 xd->receive_ring = 0;
1395 ret = tb_domain_disconnect_xdomain_paths(xd->tb, xd);
1397 mutex_unlock(&xd->lock);
1399 return ret;
1401 EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths);
1403 struct tb_xdomain_lookup {
1404 const uuid_t *uuid;
1405 u8 link;
1406 u8 depth;
1407 u64 route;
1410 static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw,
1411 const struct tb_xdomain_lookup *lookup)
1413 struct tb_port *port;
1415 tb_switch_for_each_port(sw, port) {
1416 struct tb_xdomain *xd;
1418 if (port->xdomain) {
1419 xd = port->xdomain;
1421 if (lookup->uuid) {
1422 if (xd->remote_uuid &&
1423 uuid_equal(xd->remote_uuid, lookup->uuid))
1424 return xd;
1425 } else if (lookup->link &&
1426 lookup->link == xd->link &&
1427 lookup->depth == xd->depth) {
1428 return xd;
1429 } else if (lookup->route &&
1430 lookup->route == xd->route) {
1431 return xd;
1433 } else if (tb_port_has_remote(port)) {
1434 xd = switch_find_xdomain(port->remote->sw, lookup);
1435 if (xd)
1436 return xd;
1440 return NULL;
1444 * tb_xdomain_find_by_uuid() - Find an XDomain by UUID
1445 * @tb: Domain where the XDomain belongs to
1446 * @uuid: UUID to look for
1448 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1449 * The returned XDomain will have its reference count increased so the
1450 * caller needs to call tb_xdomain_put() when it is done with the
1451 * object.
1453 * This will find all XDomains including the ones that are not yet added
1454 * to the bus (handshake is still in progress).
1456 * The caller needs to hold @tb->lock.
1458 struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid)
1460 struct tb_xdomain_lookup lookup;
1461 struct tb_xdomain *xd;
1463 memset(&lookup, 0, sizeof(lookup));
1464 lookup.uuid = uuid;
1466 xd = switch_find_xdomain(tb->root_switch, &lookup);
1467 return tb_xdomain_get(xd);
1469 EXPORT_SYMBOL_GPL(tb_xdomain_find_by_uuid);
1472 * tb_xdomain_find_by_link_depth() - Find an XDomain by link and depth
1473 * @tb: Domain where the XDomain belongs to
1474 * @link: Root switch link number
1475 * @depth: Depth in the link
1477 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1478 * The returned XDomain will have its reference count increased so the
1479 * caller needs to call tb_xdomain_put() when it is done with the
1480 * object.
1482 * This will find all XDomains including the ones that are not yet added
1483 * to the bus (handshake is still in progress).
1485 * The caller needs to hold @tb->lock.
1487 struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
1488 u8 depth)
1490 struct tb_xdomain_lookup lookup;
1491 struct tb_xdomain *xd;
1493 memset(&lookup, 0, sizeof(lookup));
1494 lookup.link = link;
1495 lookup.depth = depth;
1497 xd = switch_find_xdomain(tb->root_switch, &lookup);
1498 return tb_xdomain_get(xd);
1502 * tb_xdomain_find_by_route() - Find an XDomain by route string
1503 * @tb: Domain where the XDomain belongs to
1504 * @route: XDomain route string
1506 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1507 * The returned XDomain will have its reference count increased so the
1508 * caller needs to call tb_xdomain_put() when it is done with the
1509 * object.
1511 * This will find all XDomains including the ones that are not yet added
1512 * to the bus (handshake is still in progress).
1514 * The caller needs to hold @tb->lock.
1516 struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route)
1518 struct tb_xdomain_lookup lookup;
1519 struct tb_xdomain *xd;
1521 memset(&lookup, 0, sizeof(lookup));
1522 lookup.route = route;
1524 xd = switch_find_xdomain(tb->root_switch, &lookup);
1525 return tb_xdomain_get(xd);
1527 EXPORT_SYMBOL_GPL(tb_xdomain_find_by_route);
1529 bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
1530 const void *buf, size_t size)
1532 const struct tb_protocol_handler *handler, *tmp;
1533 const struct tb_xdp_header *hdr = buf;
1534 unsigned int length;
1535 int ret = 0;
1537 /* We expect the packet is at least size of the header */
1538 length = hdr->xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
1539 if (length != size / 4 - sizeof(hdr->xd_hdr) / 4)
1540 return true;
1541 if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4)
1542 return true;
1545 * Handle XDomain discovery protocol packets directly here. For
1546 * other protocols (based on their UUID) we call registered
1547 * handlers in turn.
1549 if (uuid_equal(&hdr->uuid, &tb_xdp_uuid)) {
1550 if (type == TB_CFG_PKG_XDOMAIN_REQ)
1551 return tb_xdp_schedule_request(tb, hdr, size);
1552 return false;
1555 mutex_lock(&xdomain_lock);
1556 list_for_each_entry_safe(handler, tmp, &protocol_handlers, list) {
1557 if (!uuid_equal(&hdr->uuid, handler->uuid))
1558 continue;
1560 mutex_unlock(&xdomain_lock);
1561 ret = handler->callback(buf, size, handler->data);
1562 mutex_lock(&xdomain_lock);
1564 if (ret)
1565 break;
1567 mutex_unlock(&xdomain_lock);
1569 return ret > 0;
1572 static int rebuild_property_block(void)
1574 u32 *block, len;
1575 int ret;
1577 ret = tb_property_format_dir(xdomain_property_dir, NULL, 0);
1578 if (ret < 0)
1579 return ret;
1581 len = ret;
1583 block = kcalloc(len, sizeof(u32), GFP_KERNEL);
1584 if (!block)
1585 return -ENOMEM;
1587 ret = tb_property_format_dir(xdomain_property_dir, block, len);
1588 if (ret) {
1589 kfree(block);
1590 return ret;
1593 kfree(xdomain_property_block);
1594 xdomain_property_block = block;
1595 xdomain_property_block_len = len;
1596 xdomain_property_block_gen++;
1598 return 0;
1601 static int update_xdomain(struct device *dev, void *data)
1603 struct tb_xdomain *xd;
1605 xd = tb_to_xdomain(dev);
1606 if (xd) {
1607 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1608 msecs_to_jiffies(50));
1611 return 0;
1614 static void update_all_xdomains(void)
1616 bus_for_each_dev(&tb_bus_type, NULL, NULL, update_xdomain);
1619 static bool remove_directory(const char *key, const struct tb_property_dir *dir)
1621 struct tb_property *p;
1623 p = tb_property_find(xdomain_property_dir, key,
1624 TB_PROPERTY_TYPE_DIRECTORY);
1625 if (p && p->value.dir == dir) {
1626 tb_property_remove(p);
1627 return true;
1629 return false;
1633 * tb_register_property_dir() - Register property directory to the host
1634 * @key: Key (name) of the directory to add
1635 * @dir: Directory to add
1637 * Service drivers can use this function to add new property directory
1638 * to the host available properties. The other connected hosts are
1639 * notified so they can re-read properties of this host if they are
1640 * interested.
1642 * Return: %0 on success and negative errno on failure
1644 int tb_register_property_dir(const char *key, struct tb_property_dir *dir)
1646 int ret;
1648 if (WARN_ON(!xdomain_property_dir))
1649 return -EAGAIN;
1651 if (!key || strlen(key) > 8)
1652 return -EINVAL;
1654 mutex_lock(&xdomain_lock);
1655 if (tb_property_find(xdomain_property_dir, key,
1656 TB_PROPERTY_TYPE_DIRECTORY)) {
1657 ret = -EEXIST;
1658 goto err_unlock;
1661 ret = tb_property_add_dir(xdomain_property_dir, key, dir);
1662 if (ret)
1663 goto err_unlock;
1665 ret = rebuild_property_block();
1666 if (ret) {
1667 remove_directory(key, dir);
1668 goto err_unlock;
1671 mutex_unlock(&xdomain_lock);
1672 update_all_xdomains();
1673 return 0;
1675 err_unlock:
1676 mutex_unlock(&xdomain_lock);
1677 return ret;
1679 EXPORT_SYMBOL_GPL(tb_register_property_dir);
1682 * tb_unregister_property_dir() - Removes property directory from host
1683 * @key: Key (name) of the directory
1684 * @dir: Directory to remove
1686 * This will remove the existing directory from this host and notify the
1687 * connected hosts about the change.
1689 void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir)
1691 int ret = 0;
1693 mutex_lock(&xdomain_lock);
1694 if (remove_directory(key, dir))
1695 ret = rebuild_property_block();
1696 mutex_unlock(&xdomain_lock);
1698 if (!ret)
1699 update_all_xdomains();
1701 EXPORT_SYMBOL_GPL(tb_unregister_property_dir);
1703 int tb_xdomain_init(void)
1705 int ret;
1707 xdomain_property_dir = tb_property_create_dir(NULL);
1708 if (!xdomain_property_dir)
1709 return -ENOMEM;
1712 * Initialize standard set of properties without any service
1713 * directories. Those will be added by service drivers
1714 * themselves when they are loaded.
1716 tb_property_add_immediate(xdomain_property_dir, "vendorid",
1717 PCI_VENDOR_ID_INTEL);
1718 tb_property_add_text(xdomain_property_dir, "vendorid", "Intel Corp.");
1719 tb_property_add_immediate(xdomain_property_dir, "deviceid", 0x1);
1720 tb_property_add_text(xdomain_property_dir, "deviceid",
1721 utsname()->nodename);
1722 tb_property_add_immediate(xdomain_property_dir, "devicerv", 0x80000100);
1724 ret = rebuild_property_block();
1725 if (ret) {
1726 tb_property_free_dir(xdomain_property_dir);
1727 xdomain_property_dir = NULL;
1730 return ret;
1733 void tb_xdomain_exit(void)
1735 kfree(xdomain_property_block);
1736 tb_property_free_dir(xdomain_property_dir);