1 // SPDX-License-Identifier: GPL-2.0
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>
10 #include <linux/device.h>
11 #include <linux/delay.h>
12 #include <linux/kmod.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/prandom.h>
16 #include <linux/utsname.h>
17 #include <linux/uuid.h>
18 #include <linux/workqueue.h>
22 #define XDOMAIN_DEFAULT_TIMEOUT 1000 /* ms */
23 #define XDOMAIN_UUID_RETRIES 10
24 #define XDOMAIN_PROPERTIES_RETRIES 10
25 #define XDOMAIN_PROPERTIES_CHANGED_RETRIES 10
26 #define XDOMAIN_BONDING_WAIT 100 /* ms */
27 #define XDOMAIN_DEFAULT_MAX_HOPID 15
29 struct xdomain_request_work {
30 struct work_struct work;
31 struct tb_xdp_header *pkg;
35 static bool tb_xdomain_enabled = true;
36 module_param_named(xdomain, tb_xdomain_enabled, bool, 0444);
37 MODULE_PARM_DESC(xdomain, "allow XDomain protocol (default: true)");
40 * Serializes access to the properties and protocol handlers below. If
41 * you need to take both this lock and the struct tb_xdomain lock, take
44 static DEFINE_MUTEX(xdomain_lock);
46 /* Properties exposed to the remote domains */
47 static struct tb_property_dir *xdomain_property_dir;
48 static u32 xdomain_property_block_gen;
50 /* Additional protocol handlers */
51 static LIST_HEAD(protocol_handlers);
53 /* UUID for XDomain discovery protocol: b638d70e-42ff-40bb-97c2-90e2c0b2ff07 */
54 static const uuid_t tb_xdp_uuid =
55 UUID_INIT(0xb638d70e, 0x42ff, 0x40bb,
56 0x97, 0xc2, 0x90, 0xe2, 0xc0, 0xb2, 0xff, 0x07);
58 bool tb_is_xdomain_enabled(void)
60 return tb_xdomain_enabled && tb_acpi_is_xdomain_allowed();
63 static bool tb_xdomain_match(const struct tb_cfg_request *req,
64 const struct ctl_pkg *pkg)
66 switch (pkg->frame.eof) {
67 case TB_CFG_PKG_ERROR:
70 case TB_CFG_PKG_XDOMAIN_RESP: {
71 const struct tb_xdp_header *res_hdr = pkg->buffer;
72 const struct tb_xdp_header *req_hdr = req->request;
74 if (pkg->frame.size < req->response_size / 4)
77 /* Make sure route matches */
78 if ((res_hdr->xd_hdr.route_hi & ~BIT(31)) !=
79 req_hdr->xd_hdr.route_hi)
81 if ((res_hdr->xd_hdr.route_lo) != req_hdr->xd_hdr.route_lo)
84 /* Check that the XDomain protocol matches */
85 if (!uuid_equal(&res_hdr->uuid, &req_hdr->uuid))
96 static bool tb_xdomain_copy(struct tb_cfg_request *req,
97 const struct ctl_pkg *pkg)
99 memcpy(req->response, pkg->buffer, req->response_size);
104 static void response_ready(void *data)
106 tb_cfg_request_put(data);
109 static int __tb_xdomain_response(struct tb_ctl *ctl, const void *response,
110 size_t size, enum tb_cfg_pkg_type type)
112 struct tb_cfg_request *req;
114 req = tb_cfg_request_alloc();
118 req->match = tb_xdomain_match;
119 req->copy = tb_xdomain_copy;
120 req->request = response;
121 req->request_size = size;
122 req->request_type = type;
124 return tb_cfg_request(ctl, req, response_ready, req);
128 * tb_xdomain_response() - Send a XDomain response message
129 * @xd: XDomain to send the message
130 * @response: Response to send
131 * @size: Size of the response
132 * @type: PDF type of the response
134 * This can be used to send a XDomain response message to the other
135 * domain. No response for the message is expected.
137 * Return: %0 in case of success and negative errno in case of failure
139 int tb_xdomain_response(struct tb_xdomain *xd, const void *response,
140 size_t size, enum tb_cfg_pkg_type type)
142 return __tb_xdomain_response(xd->tb->ctl, response, size, type);
144 EXPORT_SYMBOL_GPL(tb_xdomain_response);
146 static int __tb_xdomain_request(struct tb_ctl *ctl, const void *request,
147 size_t request_size, enum tb_cfg_pkg_type request_type, void *response,
148 size_t response_size, enum tb_cfg_pkg_type response_type,
149 unsigned int timeout_msec)
151 struct tb_cfg_request *req;
152 struct tb_cfg_result res;
154 req = tb_cfg_request_alloc();
158 req->match = tb_xdomain_match;
159 req->copy = tb_xdomain_copy;
160 req->request = request;
161 req->request_size = request_size;
162 req->request_type = request_type;
163 req->response = response;
164 req->response_size = response_size;
165 req->response_type = response_type;
167 res = tb_cfg_request_sync(ctl, req, timeout_msec);
169 tb_cfg_request_put(req);
171 return res.err == 1 ? -EIO : res.err;
175 * tb_xdomain_request() - Send a XDomain request
176 * @xd: XDomain to send the request
177 * @request: Request to send
178 * @request_size: Size of the request in bytes
179 * @request_type: PDF type of the request
180 * @response: Response is copied here
181 * @response_size: Expected size of the response in bytes
182 * @response_type: Expected PDF type of the response
183 * @timeout_msec: Timeout in milliseconds to wait for the response
185 * This function can be used to send XDomain control channel messages to
186 * the other domain. The function waits until the response is received
187 * or when timeout triggers. Whichever comes first.
189 * Return: %0 in case of success and negative errno in case of failure
191 int tb_xdomain_request(struct tb_xdomain *xd, const void *request,
192 size_t request_size, enum tb_cfg_pkg_type request_type,
193 void *response, size_t response_size,
194 enum tb_cfg_pkg_type response_type, unsigned int timeout_msec)
196 return __tb_xdomain_request(xd->tb->ctl, request, request_size,
197 request_type, response, response_size,
198 response_type, timeout_msec);
200 EXPORT_SYMBOL_GPL(tb_xdomain_request);
202 static inline void tb_xdp_fill_header(struct tb_xdp_header *hdr, u64 route,
203 u8 sequence, enum tb_xdp_type type, size_t size)
207 length_sn = (size - sizeof(hdr->xd_hdr)) / 4;
208 length_sn |= (sequence << TB_XDOMAIN_SN_SHIFT) & TB_XDOMAIN_SN_MASK;
210 hdr->xd_hdr.route_hi = upper_32_bits(route);
211 hdr->xd_hdr.route_lo = lower_32_bits(route);
212 hdr->xd_hdr.length_sn = length_sn;
214 memcpy(&hdr->uuid, &tb_xdp_uuid, sizeof(tb_xdp_uuid));
217 static int tb_xdp_handle_error(const struct tb_xdp_error_response *res)
219 if (res->hdr.type != ERROR_RESPONSE)
222 switch (res->error) {
223 case ERROR_UNKNOWN_PACKET:
224 case ERROR_UNKNOWN_DOMAIN:
226 case ERROR_NOT_SUPPORTED:
228 case ERROR_NOT_READY:
237 static int tb_xdp_uuid_request(struct tb_ctl *ctl, u64 route, int retry,
240 struct tb_xdp_uuid_response res;
241 struct tb_xdp_uuid req;
244 memset(&req, 0, sizeof(req));
245 tb_xdp_fill_header(&req.hdr, route, retry % 4, UUID_REQUEST,
248 memset(&res, 0, sizeof(res));
249 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
250 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
251 TB_CFG_PKG_XDOMAIN_RESP,
252 XDOMAIN_DEFAULT_TIMEOUT);
256 ret = tb_xdp_handle_error(&res.err);
260 uuid_copy(uuid, &res.src_uuid);
264 static int tb_xdp_uuid_response(struct tb_ctl *ctl, u64 route, u8 sequence,
267 struct tb_xdp_uuid_response res;
269 memset(&res, 0, sizeof(res));
270 tb_xdp_fill_header(&res.hdr, route, sequence, UUID_RESPONSE,
273 uuid_copy(&res.src_uuid, uuid);
274 res.src_route_hi = upper_32_bits(route);
275 res.src_route_lo = lower_32_bits(route);
277 return __tb_xdomain_response(ctl, &res, sizeof(res),
278 TB_CFG_PKG_XDOMAIN_RESP);
281 static int tb_xdp_error_response(struct tb_ctl *ctl, u64 route, u8 sequence,
282 enum tb_xdp_error error)
284 struct tb_xdp_error_response res;
286 memset(&res, 0, sizeof(res));
287 tb_xdp_fill_header(&res.hdr, route, sequence, ERROR_RESPONSE,
291 return __tb_xdomain_response(ctl, &res, sizeof(res),
292 TB_CFG_PKG_XDOMAIN_RESP);
295 static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route,
296 const uuid_t *src_uuid, const uuid_t *dst_uuid, int retry,
297 u32 **block, u32 *generation)
299 struct tb_xdp_properties_response *res;
300 struct tb_xdp_properties req;
306 total_size = sizeof(*res) + TB_XDP_PROPERTIES_MAX_DATA_LENGTH * 4;
307 res = kzalloc(total_size, GFP_KERNEL);
311 memset(&req, 0, sizeof(req));
312 tb_xdp_fill_header(&req.hdr, route, retry % 4, PROPERTIES_REQUEST,
314 memcpy(&req.src_uuid, src_uuid, sizeof(*src_uuid));
315 memcpy(&req.dst_uuid, dst_uuid, sizeof(*dst_uuid));
321 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
322 TB_CFG_PKG_XDOMAIN_REQ, res,
323 total_size, TB_CFG_PKG_XDOMAIN_RESP,
324 XDOMAIN_DEFAULT_TIMEOUT);
328 ret = tb_xdp_handle_error(&res->err);
333 * Package length includes the whole payload without the
334 * XDomain header. Validate first that the package is at
335 * least size of the response structure.
337 len = res->hdr.xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
338 if (len < sizeof(*res) / 4) {
343 len += sizeof(res->hdr.xd_hdr) / 4;
344 len -= sizeof(*res) / 4;
346 if (res->offset != req.offset) {
352 * First time allocate block that has enough space for
353 * the whole properties block.
356 data_len = res->data_length;
357 if (data_len > TB_XDP_PROPERTIES_MAX_LENGTH) {
362 data = kcalloc(data_len, sizeof(u32), GFP_KERNEL);
369 memcpy(data + req.offset, res->data, len * 4);
371 } while (!data_len || req.offset < data_len);
374 *generation = res->generation;
387 static int tb_xdp_properties_response(struct tb *tb, struct tb_ctl *ctl,
388 struct tb_xdomain *xd, u8 sequence, const struct tb_xdp_properties *req)
390 struct tb_xdp_properties_response *res;
396 * Currently we expect all requests to be directed to us. The
397 * protocol supports forwarding, though which we might add
400 if (!uuid_equal(xd->local_uuid, &req->dst_uuid)) {
401 tb_xdp_error_response(ctl, xd->route, sequence,
402 ERROR_UNKNOWN_DOMAIN);
406 mutex_lock(&xd->lock);
408 if (req->offset >= xd->local_property_block_len) {
409 mutex_unlock(&xd->lock);
413 len = xd->local_property_block_len - req->offset;
414 len = min_t(u16, len, TB_XDP_PROPERTIES_MAX_DATA_LENGTH);
415 total_size = sizeof(*res) + len * 4;
417 res = kzalloc(total_size, GFP_KERNEL);
419 mutex_unlock(&xd->lock);
423 tb_xdp_fill_header(&res->hdr, xd->route, sequence, PROPERTIES_RESPONSE,
425 res->generation = xd->local_property_block_gen;
426 res->data_length = xd->local_property_block_len;
427 res->offset = req->offset;
428 uuid_copy(&res->src_uuid, xd->local_uuid);
429 uuid_copy(&res->dst_uuid, &req->src_uuid);
430 memcpy(res->data, &xd->local_property_block[req->offset], len * 4);
432 mutex_unlock(&xd->lock);
434 ret = __tb_xdomain_response(ctl, res, total_size,
435 TB_CFG_PKG_XDOMAIN_RESP);
441 static int tb_xdp_properties_changed_request(struct tb_ctl *ctl, u64 route,
442 int retry, const uuid_t *uuid)
444 struct tb_xdp_properties_changed_response res;
445 struct tb_xdp_properties_changed req;
448 memset(&req, 0, sizeof(req));
449 tb_xdp_fill_header(&req.hdr, route, retry % 4,
450 PROPERTIES_CHANGED_REQUEST, sizeof(req));
451 uuid_copy(&req.src_uuid, uuid);
453 memset(&res, 0, sizeof(res));
454 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
455 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
456 TB_CFG_PKG_XDOMAIN_RESP,
457 XDOMAIN_DEFAULT_TIMEOUT);
461 return tb_xdp_handle_error(&res.err);
465 tb_xdp_properties_changed_response(struct tb_ctl *ctl, u64 route, u8 sequence)
467 struct tb_xdp_properties_changed_response res;
469 memset(&res, 0, sizeof(res));
470 tb_xdp_fill_header(&res.hdr, route, sequence,
471 PROPERTIES_CHANGED_RESPONSE, sizeof(res));
472 return __tb_xdomain_response(ctl, &res, sizeof(res),
473 TB_CFG_PKG_XDOMAIN_RESP);
477 * tb_register_protocol_handler() - Register protocol handler
478 * @handler: Handler to register
480 * This allows XDomain service drivers to hook into incoming XDomain
481 * messages. After this function is called the service driver needs to
482 * be able to handle calls to callback whenever a package with the
483 * registered protocol is received.
485 int tb_register_protocol_handler(struct tb_protocol_handler *handler)
487 if (!handler->uuid || !handler->callback)
489 if (uuid_equal(handler->uuid, &tb_xdp_uuid))
492 mutex_lock(&xdomain_lock);
493 list_add_tail(&handler->list, &protocol_handlers);
494 mutex_unlock(&xdomain_lock);
498 EXPORT_SYMBOL_GPL(tb_register_protocol_handler);
501 * tb_unregister_protocol_handler() - Unregister protocol handler
502 * @handler: Handler to unregister
504 * Removes the previously registered protocol handler.
506 void tb_unregister_protocol_handler(struct tb_protocol_handler *handler)
508 mutex_lock(&xdomain_lock);
509 list_del_init(&handler->list);
510 mutex_unlock(&xdomain_lock);
512 EXPORT_SYMBOL_GPL(tb_unregister_protocol_handler);
514 static void update_property_block(struct tb_xdomain *xd)
516 mutex_lock(&xdomain_lock);
517 mutex_lock(&xd->lock);
519 * If the local property block is not up-to-date, rebuild it now
520 * based on the global property template.
522 if (!xd->local_property_block ||
523 xd->local_property_block_gen < xdomain_property_block_gen) {
524 struct tb_property_dir *dir;
528 dir = tb_property_copy_dir(xdomain_property_dir);
530 dev_warn(&xd->dev, "failed to copy properties\n");
534 /* Fill in non-static properties now */
535 tb_property_add_text(dir, "deviceid", utsname()->nodename);
536 tb_property_add_immediate(dir, "maxhopid", xd->local_max_hopid);
538 ret = tb_property_format_dir(dir, NULL, 0);
540 dev_warn(&xd->dev, "local property block creation failed\n");
541 tb_property_free_dir(dir);
546 block = kcalloc(block_len, sizeof(*block), GFP_KERNEL);
548 tb_property_free_dir(dir);
552 ret = tb_property_format_dir(dir, block, block_len);
554 dev_warn(&xd->dev, "property block generation failed\n");
555 tb_property_free_dir(dir);
560 tb_property_free_dir(dir);
561 /* Release the previous block */
562 kfree(xd->local_property_block);
564 xd->local_property_block = block;
565 xd->local_property_block_len = block_len;
566 xd->local_property_block_gen = xdomain_property_block_gen;
570 mutex_unlock(&xd->lock);
571 mutex_unlock(&xdomain_lock);
574 static void tb_xdp_handle_request(struct work_struct *work)
576 struct xdomain_request_work *xw = container_of(work, typeof(*xw), work);
577 const struct tb_xdp_header *pkg = xw->pkg;
578 const struct tb_xdomain_header *xhdr = &pkg->xd_hdr;
579 struct tb *tb = xw->tb;
580 struct tb_ctl *ctl = tb->ctl;
581 struct tb_xdomain *xd;
587 route = ((u64)xhdr->route_hi << 32 | xhdr->route_lo) & ~BIT_ULL(63);
588 sequence = xhdr->length_sn & TB_XDOMAIN_SN_MASK;
589 sequence >>= TB_XDOMAIN_SN_SHIFT;
591 mutex_lock(&tb->lock);
593 uuid = tb->root_switch->uuid;
596 mutex_unlock(&tb->lock);
599 tb_xdp_error_response(ctl, route, sequence, ERROR_NOT_READY);
603 tb_dbg(tb, "%llx: received XDomain request %#x\n", route, pkg->type);
605 xd = tb_xdomain_find_by_route_locked(tb, route);
607 update_property_block(xd);
610 case PROPERTIES_REQUEST:
612 ret = tb_xdp_properties_response(tb, ctl, xd, sequence,
613 (const struct tb_xdp_properties *)pkg);
617 case PROPERTIES_CHANGED_REQUEST:
618 ret = tb_xdp_properties_changed_response(ctl, route, sequence);
621 * Since the properties have been changed, let's update
622 * the xdomain related to this connection as well in
623 * case there is a change in services it offers.
625 if (xd && device_is_registered(&xd->dev)) {
626 queue_delayed_work(tb->wq, &xd->get_properties_work,
627 msecs_to_jiffies(50));
631 case UUID_REQUEST_OLD:
633 ret = tb_xdp_uuid_response(ctl, route, sequence, uuid);
637 tb_xdp_error_response(ctl, route, sequence,
638 ERROR_NOT_SUPPORTED);
645 tb_warn(tb, "failed to send XDomain response for %#x\n",
657 tb_xdp_schedule_request(struct tb *tb, const struct tb_xdp_header *hdr,
660 struct xdomain_request_work *xw;
662 xw = kmalloc(sizeof(*xw), GFP_KERNEL);
666 INIT_WORK(&xw->work, tb_xdp_handle_request);
667 xw->pkg = kmemdup(hdr, size, GFP_KERNEL);
672 xw->tb = tb_domain_get(tb);
674 schedule_work(&xw->work);
679 * tb_register_service_driver() - Register XDomain service driver
680 * @drv: Driver to register
682 * Registers new service driver from @drv to the bus.
684 int tb_register_service_driver(struct tb_service_driver *drv)
686 drv->driver.bus = &tb_bus_type;
687 return driver_register(&drv->driver);
689 EXPORT_SYMBOL_GPL(tb_register_service_driver);
692 * tb_unregister_service_driver() - Unregister XDomain service driver
693 * @drv: Driver to unregister
695 * Unregisters XDomain service driver from the bus.
697 void tb_unregister_service_driver(struct tb_service_driver *drv)
699 driver_unregister(&drv->driver);
701 EXPORT_SYMBOL_GPL(tb_unregister_service_driver);
703 static ssize_t key_show(struct device *dev, struct device_attribute *attr,
706 struct tb_service *svc = container_of(dev, struct tb_service, dev);
709 * It should be null terminated but anything else is pretty much
712 return sprintf(buf, "%*pE\n", (int)strlen(svc->key), svc->key);
714 static DEVICE_ATTR_RO(key);
716 static int get_modalias(struct tb_service *svc, char *buf, size_t size)
718 return snprintf(buf, size, "tbsvc:k%sp%08Xv%08Xr%08X", svc->key,
719 svc->prtcid, svc->prtcvers, svc->prtcrevs);
722 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
725 struct tb_service *svc = container_of(dev, struct tb_service, dev);
727 /* Full buffer size except new line and null termination */
728 get_modalias(svc, buf, PAGE_SIZE - 2);
729 return strlen(strcat(buf, "\n"));
731 static DEVICE_ATTR_RO(modalias);
733 static ssize_t prtcid_show(struct device *dev, struct device_attribute *attr,
736 struct tb_service *svc = container_of(dev, struct tb_service, dev);
738 return sprintf(buf, "%u\n", svc->prtcid);
740 static DEVICE_ATTR_RO(prtcid);
742 static ssize_t prtcvers_show(struct device *dev, struct device_attribute *attr,
745 struct tb_service *svc = container_of(dev, struct tb_service, dev);
747 return sprintf(buf, "%u\n", svc->prtcvers);
749 static DEVICE_ATTR_RO(prtcvers);
751 static ssize_t prtcrevs_show(struct device *dev, struct device_attribute *attr,
754 struct tb_service *svc = container_of(dev, struct tb_service, dev);
756 return sprintf(buf, "%u\n", svc->prtcrevs);
758 static DEVICE_ATTR_RO(prtcrevs);
760 static ssize_t prtcstns_show(struct device *dev, struct device_attribute *attr,
763 struct tb_service *svc = container_of(dev, struct tb_service, dev);
765 return sprintf(buf, "0x%08x\n", svc->prtcstns);
767 static DEVICE_ATTR_RO(prtcstns);
769 static struct attribute *tb_service_attrs[] = {
771 &dev_attr_modalias.attr,
772 &dev_attr_prtcid.attr,
773 &dev_attr_prtcvers.attr,
774 &dev_attr_prtcrevs.attr,
775 &dev_attr_prtcstns.attr,
779 static const struct attribute_group tb_service_attr_group = {
780 .attrs = tb_service_attrs,
783 static const struct attribute_group *tb_service_attr_groups[] = {
784 &tb_service_attr_group,
788 static int tb_service_uevent(struct device *dev, struct kobj_uevent_env *env)
790 struct tb_service *svc = container_of(dev, struct tb_service, dev);
793 get_modalias(svc, modalias, sizeof(modalias));
794 return add_uevent_var(env, "MODALIAS=%s", modalias);
797 static void tb_service_release(struct device *dev)
799 struct tb_service *svc = container_of(dev, struct tb_service, dev);
800 struct tb_xdomain *xd = tb_service_parent(svc);
802 tb_service_debugfs_remove(svc);
803 ida_simple_remove(&xd->service_ids, svc->id);
808 struct device_type tb_service_type = {
809 .name = "thunderbolt_service",
810 .groups = tb_service_attr_groups,
811 .uevent = tb_service_uevent,
812 .release = tb_service_release,
814 EXPORT_SYMBOL_GPL(tb_service_type);
816 static int remove_missing_service(struct device *dev, void *data)
818 struct tb_xdomain *xd = data;
819 struct tb_service *svc;
821 svc = tb_to_service(dev);
825 if (!tb_property_find(xd->remote_properties, svc->key,
826 TB_PROPERTY_TYPE_DIRECTORY))
827 device_unregister(dev);
832 static int find_service(struct device *dev, void *data)
834 const struct tb_property *p = data;
835 struct tb_service *svc;
837 svc = tb_to_service(dev);
841 return !strcmp(svc->key, p->key);
844 static int populate_service(struct tb_service *svc,
845 struct tb_property *property)
847 struct tb_property_dir *dir = property->value.dir;
848 struct tb_property *p;
850 /* Fill in standard properties */
851 p = tb_property_find(dir, "prtcid", TB_PROPERTY_TYPE_VALUE);
853 svc->prtcid = p->value.immediate;
854 p = tb_property_find(dir, "prtcvers", TB_PROPERTY_TYPE_VALUE);
856 svc->prtcvers = p->value.immediate;
857 p = tb_property_find(dir, "prtcrevs", TB_PROPERTY_TYPE_VALUE);
859 svc->prtcrevs = p->value.immediate;
860 p = tb_property_find(dir, "prtcstns", TB_PROPERTY_TYPE_VALUE);
862 svc->prtcstns = p->value.immediate;
864 svc->key = kstrdup(property->key, GFP_KERNEL);
871 static void enumerate_services(struct tb_xdomain *xd)
873 struct tb_service *svc;
874 struct tb_property *p;
879 * First remove all services that are not available anymore in
880 * the updated property block.
882 device_for_each_child_reverse(&xd->dev, xd, remove_missing_service);
884 /* Then re-enumerate properties creating new services as we go */
885 tb_property_for_each(xd->remote_properties, p) {
886 if (p->type != TB_PROPERTY_TYPE_DIRECTORY)
889 /* If the service exists already we are fine */
890 dev = device_find_child(&xd->dev, p, find_service);
896 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
900 if (populate_service(svc, p)) {
905 id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
912 svc->dev.bus = &tb_bus_type;
913 svc->dev.type = &tb_service_type;
914 svc->dev.parent = &xd->dev;
915 dev_set_name(&svc->dev, "%s.%d", dev_name(&xd->dev), svc->id);
917 tb_service_debugfs_init(svc);
919 if (device_register(&svc->dev)) {
920 put_device(&svc->dev);
926 static int populate_properties(struct tb_xdomain *xd,
927 struct tb_property_dir *dir)
929 const struct tb_property *p;
931 /* Required properties */
932 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_VALUE);
935 xd->device = p->value.immediate;
937 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_VALUE);
940 xd->vendor = p->value.immediate;
942 p = tb_property_find(dir, "maxhopid", TB_PROPERTY_TYPE_VALUE);
944 * USB4 inter-domain spec suggests using 15 as HopID if the
945 * other end does not announce it in a property. This is for
946 * TBT3 compatibility.
948 xd->remote_max_hopid = p ? p->value.immediate : XDOMAIN_DEFAULT_MAX_HOPID;
950 kfree(xd->device_name);
951 xd->device_name = NULL;
952 kfree(xd->vendor_name);
953 xd->vendor_name = NULL;
955 /* Optional properties */
956 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_TEXT);
958 xd->device_name = kstrdup(p->value.text, GFP_KERNEL);
959 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_TEXT);
961 xd->vendor_name = kstrdup(p->value.text, GFP_KERNEL);
966 static inline struct tb_switch *tb_xdomain_parent(struct tb_xdomain *xd)
968 return tb_to_switch(xd->dev.parent);
971 static int tb_xdomain_update_link_attributes(struct tb_xdomain *xd)
974 struct tb_port *port;
977 port = tb_port_at(xd->route, tb_xdomain_parent(xd));
979 ret = tb_port_get_link_speed(port);
983 if (xd->link_speed != ret)
986 xd->link_speed = ret;
988 ret = tb_port_get_link_width(port);
992 if (xd->link_width != ret)
995 xd->link_width = ret;
998 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
1003 static void tb_xdomain_get_uuid(struct work_struct *work)
1005 struct tb_xdomain *xd = container_of(work, typeof(*xd),
1006 get_uuid_work.work);
1007 struct tb *tb = xd->tb;
1011 dev_dbg(&xd->dev, "requesting remote UUID\n");
1013 ret = tb_xdp_uuid_request(tb->ctl, xd->route, xd->uuid_retries, &uuid);
1015 if (xd->uuid_retries-- > 0) {
1016 dev_dbg(&xd->dev, "failed to request UUID, retrying\n");
1017 queue_delayed_work(xd->tb->wq, &xd->get_uuid_work,
1018 msecs_to_jiffies(100));
1020 dev_dbg(&xd->dev, "failed to read remote UUID\n");
1025 dev_dbg(&xd->dev, "got remote UUID %pUb\n", &uuid);
1027 if (uuid_equal(&uuid, xd->local_uuid))
1028 dev_dbg(&xd->dev, "intra-domain loop detected\n");
1031 * If the UUID is different, there is another domain connected
1032 * so mark this one unplugged and wait for the connection
1033 * manager to replace it.
1035 if (xd->remote_uuid && !uuid_equal(&uuid, xd->remote_uuid)) {
1036 dev_dbg(&xd->dev, "remote UUID is different, unplugging\n");
1037 xd->is_unplugged = true;
1041 /* First time fill in the missing UUID */
1042 if (!xd->remote_uuid) {
1043 xd->remote_uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL);
1044 if (!xd->remote_uuid)
1048 /* Now we can start the normal properties exchange */
1049 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1050 msecs_to_jiffies(100));
1051 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
1052 msecs_to_jiffies(1000));
1055 static void tb_xdomain_get_properties(struct work_struct *work)
1057 struct tb_xdomain *xd = container_of(work, typeof(*xd),
1058 get_properties_work.work);
1059 struct tb_property_dir *dir;
1060 struct tb *tb = xd->tb;
1061 bool update = false;
1066 dev_dbg(&xd->dev, "requesting remote properties\n");
1068 ret = tb_xdp_properties_request(tb->ctl, xd->route, xd->local_uuid,
1069 xd->remote_uuid, xd->properties_retries,
1072 if (xd->properties_retries-- > 0) {
1074 "failed to request remote properties, retrying\n");
1075 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
1076 msecs_to_jiffies(1000));
1080 "failed read XDomain properties from %pUb\n",
1086 xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
1088 mutex_lock(&xd->lock);
1090 /* Only accept newer generation properties */
1091 if (xd->remote_properties && gen <= xd->remote_property_block_gen)
1092 goto err_free_block;
1094 dir = tb_property_parse_dir(block, ret);
1096 dev_err(&xd->dev, "failed to parse XDomain properties\n");
1097 goto err_free_block;
1100 ret = populate_properties(xd, dir);
1102 dev_err(&xd->dev, "missing XDomain properties in response\n");
1106 /* Release the existing one */
1107 if (xd->remote_properties) {
1108 tb_property_free_dir(xd->remote_properties);
1112 xd->remote_properties = dir;
1113 xd->remote_property_block_gen = gen;
1115 tb_xdomain_update_link_attributes(xd);
1117 mutex_unlock(&xd->lock);
1122 * Now the device should be ready enough so we can add it to the
1123 * bus and let userspace know about it. If the device is already
1124 * registered, we notify the userspace that it has changed.
1127 if (device_add(&xd->dev)) {
1128 dev_err(&xd->dev, "failed to add XDomain device\n");
1131 dev_info(&xd->dev, "new host found, vendor=%#x device=%#x\n",
1132 xd->vendor, xd->device);
1133 if (xd->vendor_name && xd->device_name)
1134 dev_info(&xd->dev, "%s %s\n", xd->vendor_name,
1137 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
1140 enumerate_services(xd);
1144 tb_property_free_dir(dir);
1147 mutex_unlock(&xd->lock);
1150 static void tb_xdomain_properties_changed(struct work_struct *work)
1152 struct tb_xdomain *xd = container_of(work, typeof(*xd),
1153 properties_changed_work.work);
1156 dev_dbg(&xd->dev, "sending properties changed notification\n");
1158 ret = tb_xdp_properties_changed_request(xd->tb->ctl, xd->route,
1159 xd->properties_changed_retries, xd->local_uuid);
1161 if (xd->properties_changed_retries-- > 0) {
1163 "failed to send properties changed notification, retrying\n");
1164 queue_delayed_work(xd->tb->wq,
1165 &xd->properties_changed_work,
1166 msecs_to_jiffies(1000));
1168 dev_err(&xd->dev, "failed to send properties changed notification\n");
1172 xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
1175 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
1178 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1180 return sprintf(buf, "%#x\n", xd->device);
1182 static DEVICE_ATTR_RO(device);
1185 device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1187 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1190 if (mutex_lock_interruptible(&xd->lock))
1191 return -ERESTARTSYS;
1192 ret = sprintf(buf, "%s\n", xd->device_name ? xd->device_name : "");
1193 mutex_unlock(&xd->lock);
1197 static DEVICE_ATTR_RO(device_name);
1199 static ssize_t maxhopid_show(struct device *dev, struct device_attribute *attr,
1202 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1204 return sprintf(buf, "%d\n", xd->remote_max_hopid);
1206 static DEVICE_ATTR_RO(maxhopid);
1208 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
1211 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1213 return sprintf(buf, "%#x\n", xd->vendor);
1215 static DEVICE_ATTR_RO(vendor);
1218 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1220 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1223 if (mutex_lock_interruptible(&xd->lock))
1224 return -ERESTARTSYS;
1225 ret = sprintf(buf, "%s\n", xd->vendor_name ? xd->vendor_name : "");
1226 mutex_unlock(&xd->lock);
1230 static DEVICE_ATTR_RO(vendor_name);
1232 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1235 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1237 return sprintf(buf, "%pUb\n", xd->remote_uuid);
1239 static DEVICE_ATTR_RO(unique_id);
1241 static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
1244 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1246 return sprintf(buf, "%u.0 Gb/s\n", xd->link_speed);
1249 static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL);
1250 static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL);
1252 static ssize_t lanes_show(struct device *dev, struct device_attribute *attr,
1255 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1257 return sprintf(buf, "%u\n", xd->link_width);
1260 static DEVICE_ATTR(rx_lanes, 0444, lanes_show, NULL);
1261 static DEVICE_ATTR(tx_lanes, 0444, lanes_show, NULL);
1263 static struct attribute *xdomain_attrs[] = {
1264 &dev_attr_device.attr,
1265 &dev_attr_device_name.attr,
1266 &dev_attr_maxhopid.attr,
1267 &dev_attr_rx_lanes.attr,
1268 &dev_attr_rx_speed.attr,
1269 &dev_attr_tx_lanes.attr,
1270 &dev_attr_tx_speed.attr,
1271 &dev_attr_unique_id.attr,
1272 &dev_attr_vendor.attr,
1273 &dev_attr_vendor_name.attr,
1277 static const struct attribute_group xdomain_attr_group = {
1278 .attrs = xdomain_attrs,
1281 static const struct attribute_group *xdomain_attr_groups[] = {
1282 &xdomain_attr_group,
1286 static void tb_xdomain_release(struct device *dev)
1288 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1290 put_device(xd->dev.parent);
1292 kfree(xd->local_property_block);
1293 tb_property_free_dir(xd->remote_properties);
1294 ida_destroy(&xd->out_hopids);
1295 ida_destroy(&xd->in_hopids);
1296 ida_destroy(&xd->service_ids);
1298 kfree(xd->local_uuid);
1299 kfree(xd->remote_uuid);
1300 kfree(xd->device_name);
1301 kfree(xd->vendor_name);
1305 static void start_handshake(struct tb_xdomain *xd)
1307 xd->uuid_retries = XDOMAIN_UUID_RETRIES;
1308 xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
1309 xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
1311 if (xd->needs_uuid) {
1312 queue_delayed_work(xd->tb->wq, &xd->get_uuid_work,
1313 msecs_to_jiffies(100));
1315 /* Start exchanging properties with the other host */
1316 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1317 msecs_to_jiffies(100));
1318 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
1319 msecs_to_jiffies(1000));
1323 static void stop_handshake(struct tb_xdomain *xd)
1325 xd->uuid_retries = 0;
1326 xd->properties_retries = 0;
1327 xd->properties_changed_retries = 0;
1329 cancel_delayed_work_sync(&xd->get_uuid_work);
1330 cancel_delayed_work_sync(&xd->get_properties_work);
1331 cancel_delayed_work_sync(&xd->properties_changed_work);
1334 static int __maybe_unused tb_xdomain_suspend(struct device *dev)
1336 stop_handshake(tb_to_xdomain(dev));
1340 static int __maybe_unused tb_xdomain_resume(struct device *dev)
1342 start_handshake(tb_to_xdomain(dev));
1346 static const struct dev_pm_ops tb_xdomain_pm_ops = {
1347 SET_SYSTEM_SLEEP_PM_OPS(tb_xdomain_suspend, tb_xdomain_resume)
1350 struct device_type tb_xdomain_type = {
1351 .name = "thunderbolt_xdomain",
1352 .release = tb_xdomain_release,
1353 .pm = &tb_xdomain_pm_ops,
1355 EXPORT_SYMBOL_GPL(tb_xdomain_type);
1358 * tb_xdomain_alloc() - Allocate new XDomain object
1359 * @tb: Domain where the XDomain belongs
1360 * @parent: Parent device (the switch through the connection to the
1361 * other domain is reached).
1362 * @route: Route string used to reach the other domain
1363 * @local_uuid: Our local domain UUID
1364 * @remote_uuid: UUID of the other domain (optional)
1366 * Allocates new XDomain structure and returns pointer to that. The
1367 * object must be released by calling tb_xdomain_put().
1369 struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
1370 u64 route, const uuid_t *local_uuid,
1371 const uuid_t *remote_uuid)
1373 struct tb_switch *parent_sw = tb_to_switch(parent);
1374 struct tb_xdomain *xd;
1375 struct tb_port *down;
1377 /* Make sure the downstream domain is accessible */
1378 down = tb_port_at(route, parent_sw);
1379 tb_port_unlock(down);
1381 xd = kzalloc(sizeof(*xd), GFP_KERNEL);
1387 xd->local_max_hopid = down->config.max_in_hop_id;
1388 ida_init(&xd->service_ids);
1389 ida_init(&xd->in_hopids);
1390 ida_init(&xd->out_hopids);
1391 mutex_init(&xd->lock);
1392 INIT_DELAYED_WORK(&xd->get_uuid_work, tb_xdomain_get_uuid);
1393 INIT_DELAYED_WORK(&xd->get_properties_work, tb_xdomain_get_properties);
1394 INIT_DELAYED_WORK(&xd->properties_changed_work,
1395 tb_xdomain_properties_changed);
1397 xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL);
1398 if (!xd->local_uuid)
1402 xd->remote_uuid = kmemdup(remote_uuid, sizeof(uuid_t),
1404 if (!xd->remote_uuid)
1405 goto err_free_local_uuid;
1407 xd->needs_uuid = true;
1410 device_initialize(&xd->dev);
1411 xd->dev.parent = get_device(parent);
1412 xd->dev.bus = &tb_bus_type;
1413 xd->dev.type = &tb_xdomain_type;
1414 xd->dev.groups = xdomain_attr_groups;
1415 dev_set_name(&xd->dev, "%u-%llx", tb->index, route);
1417 dev_dbg(&xd->dev, "local UUID %pUb\n", local_uuid);
1419 dev_dbg(&xd->dev, "remote UUID %pUb\n", remote_uuid);
1422 * This keeps the DMA powered on as long as we have active
1423 * connection to another host.
1425 pm_runtime_set_active(&xd->dev);
1426 pm_runtime_get_noresume(&xd->dev);
1427 pm_runtime_enable(&xd->dev);
1431 err_free_local_uuid:
1432 kfree(xd->local_uuid);
1440 * tb_xdomain_add() - Add XDomain to the bus
1441 * @xd: XDomain to add
1443 * This function starts XDomain discovery protocol handshake and
1444 * eventually adds the XDomain to the bus. After calling this function
1445 * the caller needs to call tb_xdomain_remove() in order to remove and
1446 * release the object regardless whether the handshake succeeded or not.
1448 void tb_xdomain_add(struct tb_xdomain *xd)
1450 /* Start exchanging properties with the other host */
1451 start_handshake(xd);
1454 static int unregister_service(struct device *dev, void *data)
1456 device_unregister(dev);
1461 * tb_xdomain_remove() - Remove XDomain from the bus
1462 * @xd: XDomain to remove
1464 * This will stop all ongoing configuration work and remove the XDomain
1465 * along with any services from the bus. When the last reference to @xd
1466 * is released the object will be released as well.
1468 void tb_xdomain_remove(struct tb_xdomain *xd)
1472 device_for_each_child_reverse(&xd->dev, xd, unregister_service);
1475 * Undo runtime PM here explicitly because it is possible that
1476 * the XDomain was never added to the bus and thus device_del()
1477 * is not called for it (device_del() would handle this otherwise).
1479 pm_runtime_disable(&xd->dev);
1480 pm_runtime_put_noidle(&xd->dev);
1481 pm_runtime_set_suspended(&xd->dev);
1483 if (!device_is_registered(&xd->dev)) {
1484 put_device(&xd->dev);
1486 dev_info(&xd->dev, "host disconnected\n");
1487 device_unregister(&xd->dev);
1492 * tb_xdomain_lane_bonding_enable() - Enable lane bonding on XDomain
1493 * @xd: XDomain connection
1495 * Lane bonding is disabled by default for XDomains. This function tries
1496 * to enable bonding by first enabling the port and waiting for the CL0
1499 * Return: %0 in case of success and negative errno in case of error.
1501 int tb_xdomain_lane_bonding_enable(struct tb_xdomain *xd)
1503 struct tb_port *port;
1506 port = tb_port_at(xd->route, tb_xdomain_parent(xd));
1507 if (!port->dual_link_port)
1510 ret = tb_port_enable(port->dual_link_port);
1514 ret = tb_wait_for_port(port->dual_link_port, true);
1520 ret = tb_port_lane_bonding_enable(port);
1522 tb_port_warn(port, "failed to enable lane bonding\n");
1526 ret = tb_port_wait_for_link_width(port, 2, 100);
1528 tb_port_warn(port, "timeout enabling lane bonding\n");
1532 tb_port_update_credits(port);
1533 tb_xdomain_update_link_attributes(xd);
1535 dev_dbg(&xd->dev, "lane bonding enabled\n");
1538 EXPORT_SYMBOL_GPL(tb_xdomain_lane_bonding_enable);
1541 * tb_xdomain_lane_bonding_disable() - Disable lane bonding
1542 * @xd: XDomain connection
1544 * Lane bonding is disabled by default for XDomains. If bonding has been
1545 * enabled, this function can be used to disable it.
1547 void tb_xdomain_lane_bonding_disable(struct tb_xdomain *xd)
1549 struct tb_port *port;
1551 port = tb_port_at(xd->route, tb_xdomain_parent(xd));
1552 if (port->dual_link_port) {
1553 tb_port_lane_bonding_disable(port);
1554 if (tb_port_wait_for_link_width(port, 1, 100) == -ETIMEDOUT)
1555 tb_port_warn(port, "timeout disabling lane bonding\n");
1556 tb_port_disable(port->dual_link_port);
1557 tb_port_update_credits(port);
1558 tb_xdomain_update_link_attributes(xd);
1560 dev_dbg(&xd->dev, "lane bonding disabled\n");
1563 EXPORT_SYMBOL_GPL(tb_xdomain_lane_bonding_disable);
1566 * tb_xdomain_alloc_in_hopid() - Allocate input HopID for tunneling
1567 * @xd: XDomain connection
1568 * @hopid: Preferred HopID or %-1 for next available
1570 * Returns allocated HopID or negative errno. Specifically returns
1571 * %-ENOSPC if there are no more available HopIDs. Returned HopID is
1572 * guaranteed to be within range supported by the input lane adapter.
1573 * Call tb_xdomain_release_in_hopid() to release the allocated HopID.
1575 int tb_xdomain_alloc_in_hopid(struct tb_xdomain *xd, int hopid)
1578 hopid = TB_PATH_MIN_HOPID;
1579 if (hopid < TB_PATH_MIN_HOPID || hopid > xd->local_max_hopid)
1582 return ida_alloc_range(&xd->in_hopids, hopid, xd->local_max_hopid,
1585 EXPORT_SYMBOL_GPL(tb_xdomain_alloc_in_hopid);
1588 * tb_xdomain_alloc_out_hopid() - Allocate output HopID for tunneling
1589 * @xd: XDomain connection
1590 * @hopid: Preferred HopID or %-1 for next available
1592 * Returns allocated HopID or negative errno. Specifically returns
1593 * %-ENOSPC if there are no more available HopIDs. Returned HopID is
1594 * guaranteed to be within range supported by the output lane adapter.
1595 * Call tb_xdomain_release_in_hopid() to release the allocated HopID.
1597 int tb_xdomain_alloc_out_hopid(struct tb_xdomain *xd, int hopid)
1600 hopid = TB_PATH_MIN_HOPID;
1601 if (hopid < TB_PATH_MIN_HOPID || hopid > xd->remote_max_hopid)
1604 return ida_alloc_range(&xd->out_hopids, hopid, xd->remote_max_hopid,
1607 EXPORT_SYMBOL_GPL(tb_xdomain_alloc_out_hopid);
1610 * tb_xdomain_release_in_hopid() - Release input HopID
1611 * @xd: XDomain connection
1612 * @hopid: HopID to release
1614 void tb_xdomain_release_in_hopid(struct tb_xdomain *xd, int hopid)
1616 ida_free(&xd->in_hopids, hopid);
1618 EXPORT_SYMBOL_GPL(tb_xdomain_release_in_hopid);
1621 * tb_xdomain_release_out_hopid() - Release output HopID
1622 * @xd: XDomain connection
1623 * @hopid: HopID to release
1625 void tb_xdomain_release_out_hopid(struct tb_xdomain *xd, int hopid)
1627 ida_free(&xd->out_hopids, hopid);
1629 EXPORT_SYMBOL_GPL(tb_xdomain_release_out_hopid);
1632 * tb_xdomain_enable_paths() - Enable DMA paths for XDomain connection
1633 * @xd: XDomain connection
1634 * @transmit_path: HopID we are using to send out packets
1635 * @transmit_ring: DMA ring used to send out packets
1636 * @receive_path: HopID the other end is using to send packets to us
1637 * @receive_ring: DMA ring used to receive packets from @receive_path
1639 * The function enables DMA paths accordingly so that after successful
1640 * return the caller can send and receive packets using high-speed DMA
1641 * path. If a transmit or receive path is not needed, pass %-1 for those
1644 * Return: %0 in case of success and negative errno in case of error
1646 int tb_xdomain_enable_paths(struct tb_xdomain *xd, int transmit_path,
1647 int transmit_ring, int receive_path,
1650 return tb_domain_approve_xdomain_paths(xd->tb, xd, transmit_path,
1651 transmit_ring, receive_path,
1654 EXPORT_SYMBOL_GPL(tb_xdomain_enable_paths);
1657 * tb_xdomain_disable_paths() - Disable DMA paths for XDomain connection
1658 * @xd: XDomain connection
1659 * @transmit_path: HopID we are using to send out packets
1660 * @transmit_ring: DMA ring used to send out packets
1661 * @receive_path: HopID the other end is using to send packets to us
1662 * @receive_ring: DMA ring used to receive packets from @receive_path
1664 * This does the opposite of tb_xdomain_enable_paths(). After call to
1665 * this the caller is not expected to use the rings anymore. Passing %-1
1666 * as path/ring parameter means don't care. Normally the callers should
1667 * pass the same values here as they do when paths are enabled.
1669 * Return: %0 in case of success and negative errno in case of error
1671 int tb_xdomain_disable_paths(struct tb_xdomain *xd, int transmit_path,
1672 int transmit_ring, int receive_path,
1675 return tb_domain_disconnect_xdomain_paths(xd->tb, xd, transmit_path,
1676 transmit_ring, receive_path,
1679 EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths);
1681 struct tb_xdomain_lookup {
1688 static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw,
1689 const struct tb_xdomain_lookup *lookup)
1691 struct tb_port *port;
1693 tb_switch_for_each_port(sw, port) {
1694 struct tb_xdomain *xd;
1696 if (port->xdomain) {
1700 if (xd->remote_uuid &&
1701 uuid_equal(xd->remote_uuid, lookup->uuid))
1703 } else if (lookup->link &&
1704 lookup->link == xd->link &&
1705 lookup->depth == xd->depth) {
1707 } else if (lookup->route &&
1708 lookup->route == xd->route) {
1711 } else if (tb_port_has_remote(port)) {
1712 xd = switch_find_xdomain(port->remote->sw, lookup);
1722 * tb_xdomain_find_by_uuid() - Find an XDomain by UUID
1723 * @tb: Domain where the XDomain belongs to
1724 * @uuid: UUID to look for
1726 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1727 * The returned XDomain will have its reference count increased so the
1728 * caller needs to call tb_xdomain_put() when it is done with the
1731 * This will find all XDomains including the ones that are not yet added
1732 * to the bus (handshake is still in progress).
1734 * The caller needs to hold @tb->lock.
1736 struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid)
1738 struct tb_xdomain_lookup lookup;
1739 struct tb_xdomain *xd;
1741 memset(&lookup, 0, sizeof(lookup));
1744 xd = switch_find_xdomain(tb->root_switch, &lookup);
1745 return tb_xdomain_get(xd);
1747 EXPORT_SYMBOL_GPL(tb_xdomain_find_by_uuid);
1750 * tb_xdomain_find_by_link_depth() - Find an XDomain by link and depth
1751 * @tb: Domain where the XDomain belongs to
1752 * @link: Root switch link number
1753 * @depth: Depth in the link
1755 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1756 * The returned XDomain will have its reference count increased so the
1757 * caller needs to call tb_xdomain_put() when it is done with the
1760 * This will find all XDomains including the ones that are not yet added
1761 * to the bus (handshake is still in progress).
1763 * The caller needs to hold @tb->lock.
1765 struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
1768 struct tb_xdomain_lookup lookup;
1769 struct tb_xdomain *xd;
1771 memset(&lookup, 0, sizeof(lookup));
1773 lookup.depth = depth;
1775 xd = switch_find_xdomain(tb->root_switch, &lookup);
1776 return tb_xdomain_get(xd);
1780 * tb_xdomain_find_by_route() - Find an XDomain by route string
1781 * @tb: Domain where the XDomain belongs to
1782 * @route: XDomain route string
1784 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1785 * The returned XDomain will have its reference count increased so the
1786 * caller needs to call tb_xdomain_put() when it is done with the
1789 * This will find all XDomains including the ones that are not yet added
1790 * to the bus (handshake is still in progress).
1792 * The caller needs to hold @tb->lock.
1794 struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route)
1796 struct tb_xdomain_lookup lookup;
1797 struct tb_xdomain *xd;
1799 memset(&lookup, 0, sizeof(lookup));
1800 lookup.route = route;
1802 xd = switch_find_xdomain(tb->root_switch, &lookup);
1803 return tb_xdomain_get(xd);
1805 EXPORT_SYMBOL_GPL(tb_xdomain_find_by_route);
1807 bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
1808 const void *buf, size_t size)
1810 const struct tb_protocol_handler *handler, *tmp;
1811 const struct tb_xdp_header *hdr = buf;
1812 unsigned int length;
1815 /* We expect the packet is at least size of the header */
1816 length = hdr->xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
1817 if (length != size / 4 - sizeof(hdr->xd_hdr) / 4)
1819 if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4)
1823 * Handle XDomain discovery protocol packets directly here. For
1824 * other protocols (based on their UUID) we call registered
1827 if (uuid_equal(&hdr->uuid, &tb_xdp_uuid)) {
1828 if (type == TB_CFG_PKG_XDOMAIN_REQ)
1829 return tb_xdp_schedule_request(tb, hdr, size);
1833 mutex_lock(&xdomain_lock);
1834 list_for_each_entry_safe(handler, tmp, &protocol_handlers, list) {
1835 if (!uuid_equal(&hdr->uuid, handler->uuid))
1838 mutex_unlock(&xdomain_lock);
1839 ret = handler->callback(buf, size, handler->data);
1840 mutex_lock(&xdomain_lock);
1845 mutex_unlock(&xdomain_lock);
1850 static int update_xdomain(struct device *dev, void *data)
1852 struct tb_xdomain *xd;
1854 xd = tb_to_xdomain(dev);
1856 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1857 msecs_to_jiffies(50));
1863 static void update_all_xdomains(void)
1865 bus_for_each_dev(&tb_bus_type, NULL, NULL, update_xdomain);
1868 static bool remove_directory(const char *key, const struct tb_property_dir *dir)
1870 struct tb_property *p;
1872 p = tb_property_find(xdomain_property_dir, key,
1873 TB_PROPERTY_TYPE_DIRECTORY);
1874 if (p && p->value.dir == dir) {
1875 tb_property_remove(p);
1882 * tb_register_property_dir() - Register property directory to the host
1883 * @key: Key (name) of the directory to add
1884 * @dir: Directory to add
1886 * Service drivers can use this function to add new property directory
1887 * to the host available properties. The other connected hosts are
1888 * notified so they can re-read properties of this host if they are
1891 * Return: %0 on success and negative errno on failure
1893 int tb_register_property_dir(const char *key, struct tb_property_dir *dir)
1897 if (WARN_ON(!xdomain_property_dir))
1900 if (!key || strlen(key) > 8)
1903 mutex_lock(&xdomain_lock);
1904 if (tb_property_find(xdomain_property_dir, key,
1905 TB_PROPERTY_TYPE_DIRECTORY)) {
1910 ret = tb_property_add_dir(xdomain_property_dir, key, dir);
1914 xdomain_property_block_gen++;
1916 mutex_unlock(&xdomain_lock);
1917 update_all_xdomains();
1921 mutex_unlock(&xdomain_lock);
1924 EXPORT_SYMBOL_GPL(tb_register_property_dir);
1927 * tb_unregister_property_dir() - Removes property directory from host
1928 * @key: Key (name) of the directory
1929 * @dir: Directory to remove
1931 * This will remove the existing directory from this host and notify the
1932 * connected hosts about the change.
1934 void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir)
1938 mutex_lock(&xdomain_lock);
1939 if (remove_directory(key, dir))
1940 xdomain_property_block_gen++;
1941 mutex_unlock(&xdomain_lock);
1944 update_all_xdomains();
1946 EXPORT_SYMBOL_GPL(tb_unregister_property_dir);
1948 int tb_xdomain_init(void)
1950 xdomain_property_dir = tb_property_create_dir(NULL);
1951 if (!xdomain_property_dir)
1955 * Initialize standard set of properties without any service
1956 * directories. Those will be added by service drivers
1957 * themselves when they are loaded.
1959 * Rest of the properties are filled dynamically based on these
1960 * when the P2P connection is made.
1962 tb_property_add_immediate(xdomain_property_dir, "vendorid",
1963 PCI_VENDOR_ID_INTEL);
1964 tb_property_add_text(xdomain_property_dir, "vendorid", "Intel Corp.");
1965 tb_property_add_immediate(xdomain_property_dir, "deviceid", 0x1);
1966 tb_property_add_immediate(xdomain_property_dir, "devicerv", 0x80000100);
1968 xdomain_property_block_gen = prandom_u32();
1972 void tb_xdomain_exit(void)
1974 tb_property_free_dir(xdomain_property_dir);