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_header *hdr)
219 const struct tb_xdp_error_response *error;
221 if (hdr->type != ERROR_RESPONSE)
224 error = (const struct tb_xdp_error_response *)hdr;
226 switch (error->error) {
227 case ERROR_UNKNOWN_PACKET:
228 case ERROR_UNKNOWN_DOMAIN:
230 case ERROR_NOT_SUPPORTED:
232 case ERROR_NOT_READY:
241 static int tb_xdp_uuid_request(struct tb_ctl *ctl, u64 route, int retry,
244 struct tb_xdp_uuid_response res;
245 struct tb_xdp_uuid req;
248 memset(&req, 0, sizeof(req));
249 tb_xdp_fill_header(&req.hdr, route, retry % 4, UUID_REQUEST,
252 memset(&res, 0, sizeof(res));
253 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
254 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
255 TB_CFG_PKG_XDOMAIN_RESP,
256 XDOMAIN_DEFAULT_TIMEOUT);
260 ret = tb_xdp_handle_error(&res.hdr);
264 uuid_copy(uuid, &res.src_uuid);
268 static int tb_xdp_uuid_response(struct tb_ctl *ctl, u64 route, u8 sequence,
271 struct tb_xdp_uuid_response res;
273 memset(&res, 0, sizeof(res));
274 tb_xdp_fill_header(&res.hdr, route, sequence, UUID_RESPONSE,
277 uuid_copy(&res.src_uuid, uuid);
278 res.src_route_hi = upper_32_bits(route);
279 res.src_route_lo = lower_32_bits(route);
281 return __tb_xdomain_response(ctl, &res, sizeof(res),
282 TB_CFG_PKG_XDOMAIN_RESP);
285 static int tb_xdp_error_response(struct tb_ctl *ctl, u64 route, u8 sequence,
286 enum tb_xdp_error error)
288 struct tb_xdp_error_response res;
290 memset(&res, 0, sizeof(res));
291 tb_xdp_fill_header(&res.hdr, route, sequence, ERROR_RESPONSE,
295 return __tb_xdomain_response(ctl, &res, sizeof(res),
296 TB_CFG_PKG_XDOMAIN_RESP);
299 static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route,
300 const uuid_t *src_uuid, const uuid_t *dst_uuid, int retry,
301 u32 **block, u32 *generation)
303 struct tb_xdp_properties_response *res;
304 struct tb_xdp_properties req;
310 total_size = sizeof(*res) + TB_XDP_PROPERTIES_MAX_DATA_LENGTH * 4;
311 res = kzalloc(total_size, GFP_KERNEL);
315 memset(&req, 0, sizeof(req));
316 tb_xdp_fill_header(&req.hdr, route, retry % 4, PROPERTIES_REQUEST,
318 memcpy(&req.src_uuid, src_uuid, sizeof(*src_uuid));
319 memcpy(&req.dst_uuid, dst_uuid, sizeof(*dst_uuid));
325 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
326 TB_CFG_PKG_XDOMAIN_REQ, res,
327 total_size, TB_CFG_PKG_XDOMAIN_RESP,
328 XDOMAIN_DEFAULT_TIMEOUT);
332 ret = tb_xdp_handle_error(&res->hdr);
337 * Package length includes the whole payload without the
338 * XDomain header. Validate first that the package is at
339 * least size of the response structure.
341 len = res->hdr.xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
342 if (len < sizeof(*res) / 4) {
347 len += sizeof(res->hdr.xd_hdr) / 4;
348 len -= sizeof(*res) / 4;
350 if (res->offset != req.offset) {
356 * First time allocate block that has enough space for
357 * the whole properties block.
360 data_len = res->data_length;
361 if (data_len > TB_XDP_PROPERTIES_MAX_LENGTH) {
366 data = kcalloc(data_len, sizeof(u32), GFP_KERNEL);
373 memcpy(data + req.offset, res->data, len * 4);
375 } while (!data_len || req.offset < data_len);
378 *generation = res->generation;
391 static int tb_xdp_properties_response(struct tb *tb, struct tb_ctl *ctl,
392 struct tb_xdomain *xd, u8 sequence, const struct tb_xdp_properties *req)
394 struct tb_xdp_properties_response *res;
400 * Currently we expect all requests to be directed to us. The
401 * protocol supports forwarding, though which we might add
404 if (!uuid_equal(xd->local_uuid, &req->dst_uuid)) {
405 tb_xdp_error_response(ctl, xd->route, sequence,
406 ERROR_UNKNOWN_DOMAIN);
410 mutex_lock(&xd->lock);
412 if (req->offset >= xd->local_property_block_len) {
413 mutex_unlock(&xd->lock);
417 len = xd->local_property_block_len - req->offset;
418 len = min_t(u16, len, TB_XDP_PROPERTIES_MAX_DATA_LENGTH);
419 total_size = sizeof(*res) + len * 4;
421 res = kzalloc(total_size, GFP_KERNEL);
423 mutex_unlock(&xd->lock);
427 tb_xdp_fill_header(&res->hdr, xd->route, sequence, PROPERTIES_RESPONSE,
429 res->generation = xd->local_property_block_gen;
430 res->data_length = xd->local_property_block_len;
431 res->offset = req->offset;
432 uuid_copy(&res->src_uuid, xd->local_uuid);
433 uuid_copy(&res->dst_uuid, &req->src_uuid);
434 memcpy(res->data, &xd->local_property_block[req->offset], len * 4);
436 mutex_unlock(&xd->lock);
438 ret = __tb_xdomain_response(ctl, res, total_size,
439 TB_CFG_PKG_XDOMAIN_RESP);
445 static int tb_xdp_properties_changed_request(struct tb_ctl *ctl, u64 route,
446 int retry, const uuid_t *uuid)
448 struct tb_xdp_properties_changed_response res;
449 struct tb_xdp_properties_changed req;
452 memset(&req, 0, sizeof(req));
453 tb_xdp_fill_header(&req.hdr, route, retry % 4,
454 PROPERTIES_CHANGED_REQUEST, sizeof(req));
455 uuid_copy(&req.src_uuid, uuid);
457 memset(&res, 0, sizeof(res));
458 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
459 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
460 TB_CFG_PKG_XDOMAIN_RESP,
461 XDOMAIN_DEFAULT_TIMEOUT);
465 return tb_xdp_handle_error(&res.hdr);
469 tb_xdp_properties_changed_response(struct tb_ctl *ctl, u64 route, u8 sequence)
471 struct tb_xdp_properties_changed_response res;
473 memset(&res, 0, sizeof(res));
474 tb_xdp_fill_header(&res.hdr, route, sequence,
475 PROPERTIES_CHANGED_RESPONSE, sizeof(res));
476 return __tb_xdomain_response(ctl, &res, sizeof(res),
477 TB_CFG_PKG_XDOMAIN_RESP);
481 * tb_register_protocol_handler() - Register protocol handler
482 * @handler: Handler to register
484 * This allows XDomain service drivers to hook into incoming XDomain
485 * messages. After this function is called the service driver needs to
486 * be able to handle calls to callback whenever a package with the
487 * registered protocol is received.
489 int tb_register_protocol_handler(struct tb_protocol_handler *handler)
491 if (!handler->uuid || !handler->callback)
493 if (uuid_equal(handler->uuid, &tb_xdp_uuid))
496 mutex_lock(&xdomain_lock);
497 list_add_tail(&handler->list, &protocol_handlers);
498 mutex_unlock(&xdomain_lock);
502 EXPORT_SYMBOL_GPL(tb_register_protocol_handler);
505 * tb_unregister_protocol_handler() - Unregister protocol handler
506 * @handler: Handler to unregister
508 * Removes the previously registered protocol handler.
510 void tb_unregister_protocol_handler(struct tb_protocol_handler *handler)
512 mutex_lock(&xdomain_lock);
513 list_del_init(&handler->list);
514 mutex_unlock(&xdomain_lock);
516 EXPORT_SYMBOL_GPL(tb_unregister_protocol_handler);
518 static void update_property_block(struct tb_xdomain *xd)
520 mutex_lock(&xdomain_lock);
521 mutex_lock(&xd->lock);
523 * If the local property block is not up-to-date, rebuild it now
524 * based on the global property template.
526 if (!xd->local_property_block ||
527 xd->local_property_block_gen < xdomain_property_block_gen) {
528 struct tb_property_dir *dir;
532 dir = tb_property_copy_dir(xdomain_property_dir);
534 dev_warn(&xd->dev, "failed to copy properties\n");
538 /* Fill in non-static properties now */
539 tb_property_add_text(dir, "deviceid", utsname()->nodename);
540 tb_property_add_immediate(dir, "maxhopid", xd->local_max_hopid);
542 ret = tb_property_format_dir(dir, NULL, 0);
544 dev_warn(&xd->dev, "local property block creation failed\n");
545 tb_property_free_dir(dir);
550 block = kcalloc(block_len, sizeof(*block), GFP_KERNEL);
552 tb_property_free_dir(dir);
556 ret = tb_property_format_dir(dir, block, block_len);
558 dev_warn(&xd->dev, "property block generation failed\n");
559 tb_property_free_dir(dir);
564 tb_property_free_dir(dir);
565 /* Release the previous block */
566 kfree(xd->local_property_block);
568 xd->local_property_block = block;
569 xd->local_property_block_len = block_len;
570 xd->local_property_block_gen = xdomain_property_block_gen;
574 mutex_unlock(&xd->lock);
575 mutex_unlock(&xdomain_lock);
578 static void tb_xdp_handle_request(struct work_struct *work)
580 struct xdomain_request_work *xw = container_of(work, typeof(*xw), work);
581 const struct tb_xdp_header *pkg = xw->pkg;
582 const struct tb_xdomain_header *xhdr = &pkg->xd_hdr;
583 struct tb *tb = xw->tb;
584 struct tb_ctl *ctl = tb->ctl;
585 struct tb_xdomain *xd;
591 route = ((u64)xhdr->route_hi << 32 | xhdr->route_lo) & ~BIT_ULL(63);
592 sequence = xhdr->length_sn & TB_XDOMAIN_SN_MASK;
593 sequence >>= TB_XDOMAIN_SN_SHIFT;
595 mutex_lock(&tb->lock);
597 uuid = tb->root_switch->uuid;
600 mutex_unlock(&tb->lock);
603 tb_xdp_error_response(ctl, route, sequence, ERROR_NOT_READY);
607 tb_dbg(tb, "%llx: received XDomain request %#x\n", route, pkg->type);
609 xd = tb_xdomain_find_by_route_locked(tb, route);
611 update_property_block(xd);
614 case PROPERTIES_REQUEST:
616 ret = tb_xdp_properties_response(tb, ctl, xd, sequence,
617 (const struct tb_xdp_properties *)pkg);
621 case PROPERTIES_CHANGED_REQUEST:
622 ret = tb_xdp_properties_changed_response(ctl, route, sequence);
625 * Since the properties have been changed, let's update
626 * the xdomain related to this connection as well in
627 * case there is a change in services it offers.
629 if (xd && device_is_registered(&xd->dev)) {
630 queue_delayed_work(tb->wq, &xd->get_properties_work,
631 msecs_to_jiffies(50));
635 case UUID_REQUEST_OLD:
637 ret = tb_xdp_uuid_response(ctl, route, sequence, uuid);
641 tb_xdp_error_response(ctl, route, sequence,
642 ERROR_NOT_SUPPORTED);
649 tb_warn(tb, "failed to send XDomain response for %#x\n",
661 tb_xdp_schedule_request(struct tb *tb, const struct tb_xdp_header *hdr,
664 struct xdomain_request_work *xw;
666 xw = kmalloc(sizeof(*xw), GFP_KERNEL);
670 INIT_WORK(&xw->work, tb_xdp_handle_request);
671 xw->pkg = kmemdup(hdr, size, GFP_KERNEL);
676 xw->tb = tb_domain_get(tb);
678 schedule_work(&xw->work);
683 * tb_register_service_driver() - Register XDomain service driver
684 * @drv: Driver to register
686 * Registers new service driver from @drv to the bus.
688 int tb_register_service_driver(struct tb_service_driver *drv)
690 drv->driver.bus = &tb_bus_type;
691 return driver_register(&drv->driver);
693 EXPORT_SYMBOL_GPL(tb_register_service_driver);
696 * tb_unregister_service_driver() - Unregister XDomain service driver
697 * @drv: Driver to unregister
699 * Unregisters XDomain service driver from the bus.
701 void tb_unregister_service_driver(struct tb_service_driver *drv)
703 driver_unregister(&drv->driver);
705 EXPORT_SYMBOL_GPL(tb_unregister_service_driver);
707 static ssize_t key_show(struct device *dev, struct device_attribute *attr,
710 struct tb_service *svc = container_of(dev, struct tb_service, dev);
713 * It should be null terminated but anything else is pretty much
716 return sprintf(buf, "%*pE\n", (int)strlen(svc->key), svc->key);
718 static DEVICE_ATTR_RO(key);
720 static int get_modalias(struct tb_service *svc, char *buf, size_t size)
722 return snprintf(buf, size, "tbsvc:k%sp%08Xv%08Xr%08X", svc->key,
723 svc->prtcid, svc->prtcvers, svc->prtcrevs);
726 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
729 struct tb_service *svc = container_of(dev, struct tb_service, dev);
731 /* Full buffer size except new line and null termination */
732 get_modalias(svc, buf, PAGE_SIZE - 2);
733 return sprintf(buf, "%s\n", buf);
735 static DEVICE_ATTR_RO(modalias);
737 static ssize_t prtcid_show(struct device *dev, struct device_attribute *attr,
740 struct tb_service *svc = container_of(dev, struct tb_service, dev);
742 return sprintf(buf, "%u\n", svc->prtcid);
744 static DEVICE_ATTR_RO(prtcid);
746 static ssize_t prtcvers_show(struct device *dev, struct device_attribute *attr,
749 struct tb_service *svc = container_of(dev, struct tb_service, dev);
751 return sprintf(buf, "%u\n", svc->prtcvers);
753 static DEVICE_ATTR_RO(prtcvers);
755 static ssize_t prtcrevs_show(struct device *dev, struct device_attribute *attr,
758 struct tb_service *svc = container_of(dev, struct tb_service, dev);
760 return sprintf(buf, "%u\n", svc->prtcrevs);
762 static DEVICE_ATTR_RO(prtcrevs);
764 static ssize_t prtcstns_show(struct device *dev, struct device_attribute *attr,
767 struct tb_service *svc = container_of(dev, struct tb_service, dev);
769 return sprintf(buf, "0x%08x\n", svc->prtcstns);
771 static DEVICE_ATTR_RO(prtcstns);
773 static struct attribute *tb_service_attrs[] = {
775 &dev_attr_modalias.attr,
776 &dev_attr_prtcid.attr,
777 &dev_attr_prtcvers.attr,
778 &dev_attr_prtcrevs.attr,
779 &dev_attr_prtcstns.attr,
783 static const struct attribute_group tb_service_attr_group = {
784 .attrs = tb_service_attrs,
787 static const struct attribute_group *tb_service_attr_groups[] = {
788 &tb_service_attr_group,
792 static int tb_service_uevent(struct device *dev, struct kobj_uevent_env *env)
794 struct tb_service *svc = container_of(dev, struct tb_service, dev);
797 get_modalias(svc, modalias, sizeof(modalias));
798 return add_uevent_var(env, "MODALIAS=%s", modalias);
801 static void tb_service_release(struct device *dev)
803 struct tb_service *svc = container_of(dev, struct tb_service, dev);
804 struct tb_xdomain *xd = tb_service_parent(svc);
806 tb_service_debugfs_remove(svc);
807 ida_simple_remove(&xd->service_ids, svc->id);
812 struct device_type tb_service_type = {
813 .name = "thunderbolt_service",
814 .groups = tb_service_attr_groups,
815 .uevent = tb_service_uevent,
816 .release = tb_service_release,
818 EXPORT_SYMBOL_GPL(tb_service_type);
820 static int remove_missing_service(struct device *dev, void *data)
822 struct tb_xdomain *xd = data;
823 struct tb_service *svc;
825 svc = tb_to_service(dev);
829 if (!tb_property_find(xd->remote_properties, svc->key,
830 TB_PROPERTY_TYPE_DIRECTORY))
831 device_unregister(dev);
836 static int find_service(struct device *dev, void *data)
838 const struct tb_property *p = data;
839 struct tb_service *svc;
841 svc = tb_to_service(dev);
845 return !strcmp(svc->key, p->key);
848 static int populate_service(struct tb_service *svc,
849 struct tb_property *property)
851 struct tb_property_dir *dir = property->value.dir;
852 struct tb_property *p;
854 /* Fill in standard properties */
855 p = tb_property_find(dir, "prtcid", TB_PROPERTY_TYPE_VALUE);
857 svc->prtcid = p->value.immediate;
858 p = tb_property_find(dir, "prtcvers", TB_PROPERTY_TYPE_VALUE);
860 svc->prtcvers = p->value.immediate;
861 p = tb_property_find(dir, "prtcrevs", TB_PROPERTY_TYPE_VALUE);
863 svc->prtcrevs = p->value.immediate;
864 p = tb_property_find(dir, "prtcstns", TB_PROPERTY_TYPE_VALUE);
866 svc->prtcstns = p->value.immediate;
868 svc->key = kstrdup(property->key, GFP_KERNEL);
875 static void enumerate_services(struct tb_xdomain *xd)
877 struct tb_service *svc;
878 struct tb_property *p;
883 * First remove all services that are not available anymore in
884 * the updated property block.
886 device_for_each_child_reverse(&xd->dev, xd, remove_missing_service);
888 /* Then re-enumerate properties creating new services as we go */
889 tb_property_for_each(xd->remote_properties, p) {
890 if (p->type != TB_PROPERTY_TYPE_DIRECTORY)
893 /* If the service exists already we are fine */
894 dev = device_find_child(&xd->dev, p, find_service);
900 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
904 if (populate_service(svc, p)) {
909 id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
916 svc->dev.bus = &tb_bus_type;
917 svc->dev.type = &tb_service_type;
918 svc->dev.parent = &xd->dev;
919 dev_set_name(&svc->dev, "%s.%d", dev_name(&xd->dev), svc->id);
921 tb_service_debugfs_init(svc);
923 if (device_register(&svc->dev)) {
924 put_device(&svc->dev);
930 static int populate_properties(struct tb_xdomain *xd,
931 struct tb_property_dir *dir)
933 const struct tb_property *p;
935 /* Required properties */
936 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_VALUE);
939 xd->device = p->value.immediate;
941 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_VALUE);
944 xd->vendor = p->value.immediate;
946 p = tb_property_find(dir, "maxhopid", TB_PROPERTY_TYPE_VALUE);
948 * USB4 inter-domain spec suggests using 15 as HopID if the
949 * other end does not announce it in a property. This is for
950 * TBT3 compatibility.
952 xd->remote_max_hopid = p ? p->value.immediate : XDOMAIN_DEFAULT_MAX_HOPID;
954 kfree(xd->device_name);
955 xd->device_name = NULL;
956 kfree(xd->vendor_name);
957 xd->vendor_name = NULL;
959 /* Optional properties */
960 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_TEXT);
962 xd->device_name = kstrdup(p->value.text, GFP_KERNEL);
963 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_TEXT);
965 xd->vendor_name = kstrdup(p->value.text, GFP_KERNEL);
970 static inline struct tb_switch *tb_xdomain_parent(struct tb_xdomain *xd)
972 return tb_to_switch(xd->dev.parent);
975 static int tb_xdomain_update_link_attributes(struct tb_xdomain *xd)
978 struct tb_port *port;
981 port = tb_port_at(xd->route, tb_xdomain_parent(xd));
983 ret = tb_port_get_link_speed(port);
987 if (xd->link_speed != ret)
990 xd->link_speed = ret;
992 ret = tb_port_get_link_width(port);
996 if (xd->link_width != ret)
999 xd->link_width = ret;
1002 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
1007 static void tb_xdomain_get_uuid(struct work_struct *work)
1009 struct tb_xdomain *xd = container_of(work, typeof(*xd),
1010 get_uuid_work.work);
1011 struct tb *tb = xd->tb;
1015 dev_dbg(&xd->dev, "requesting remote UUID\n");
1017 ret = tb_xdp_uuid_request(tb->ctl, xd->route, xd->uuid_retries, &uuid);
1019 if (xd->uuid_retries-- > 0) {
1020 dev_dbg(&xd->dev, "failed to request UUID, retrying\n");
1021 queue_delayed_work(xd->tb->wq, &xd->get_uuid_work,
1022 msecs_to_jiffies(100));
1024 dev_dbg(&xd->dev, "failed to read remote UUID\n");
1029 dev_dbg(&xd->dev, "got remote UUID %pUb\n", &uuid);
1031 if (uuid_equal(&uuid, xd->local_uuid))
1032 dev_dbg(&xd->dev, "intra-domain loop detected\n");
1035 * If the UUID is different, there is another domain connected
1036 * so mark this one unplugged and wait for the connection
1037 * manager to replace it.
1039 if (xd->remote_uuid && !uuid_equal(&uuid, xd->remote_uuid)) {
1040 dev_dbg(&xd->dev, "remote UUID is different, unplugging\n");
1041 xd->is_unplugged = true;
1045 /* First time fill in the missing UUID */
1046 if (!xd->remote_uuid) {
1047 xd->remote_uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL);
1048 if (!xd->remote_uuid)
1052 /* Now we can start the normal properties exchange */
1053 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1054 msecs_to_jiffies(100));
1055 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
1056 msecs_to_jiffies(1000));
1059 static void tb_xdomain_get_properties(struct work_struct *work)
1061 struct tb_xdomain *xd = container_of(work, typeof(*xd),
1062 get_properties_work.work);
1063 struct tb_property_dir *dir;
1064 struct tb *tb = xd->tb;
1065 bool update = false;
1070 dev_dbg(&xd->dev, "requesting remote properties\n");
1072 ret = tb_xdp_properties_request(tb->ctl, xd->route, xd->local_uuid,
1073 xd->remote_uuid, xd->properties_retries,
1076 if (xd->properties_retries-- > 0) {
1078 "failed to request remote properties, retrying\n");
1079 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
1080 msecs_to_jiffies(1000));
1084 "failed read XDomain properties from %pUb\n",
1090 xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
1092 mutex_lock(&xd->lock);
1094 /* Only accept newer generation properties */
1095 if (xd->remote_properties && gen <= xd->remote_property_block_gen)
1096 goto err_free_block;
1098 dir = tb_property_parse_dir(block, ret);
1100 dev_err(&xd->dev, "failed to parse XDomain properties\n");
1101 goto err_free_block;
1104 ret = populate_properties(xd, dir);
1106 dev_err(&xd->dev, "missing XDomain properties in response\n");
1110 /* Release the existing one */
1111 if (xd->remote_properties) {
1112 tb_property_free_dir(xd->remote_properties);
1116 xd->remote_properties = dir;
1117 xd->remote_property_block_gen = gen;
1119 tb_xdomain_update_link_attributes(xd);
1121 mutex_unlock(&xd->lock);
1126 * Now the device should be ready enough so we can add it to the
1127 * bus and let userspace know about it. If the device is already
1128 * registered, we notify the userspace that it has changed.
1131 if (device_add(&xd->dev)) {
1132 dev_err(&xd->dev, "failed to add XDomain device\n");
1135 dev_info(&xd->dev, "new host found, vendor=%#x device=%#x\n",
1136 xd->vendor, xd->device);
1137 if (xd->vendor_name && xd->device_name)
1138 dev_info(&xd->dev, "%s %s\n", xd->vendor_name,
1141 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
1144 enumerate_services(xd);
1148 tb_property_free_dir(dir);
1151 mutex_unlock(&xd->lock);
1154 static void tb_xdomain_properties_changed(struct work_struct *work)
1156 struct tb_xdomain *xd = container_of(work, typeof(*xd),
1157 properties_changed_work.work);
1160 dev_dbg(&xd->dev, "sending properties changed notification\n");
1162 ret = tb_xdp_properties_changed_request(xd->tb->ctl, xd->route,
1163 xd->properties_changed_retries, xd->local_uuid);
1165 if (xd->properties_changed_retries-- > 0) {
1167 "failed to send properties changed notification, retrying\n");
1168 queue_delayed_work(xd->tb->wq,
1169 &xd->properties_changed_work,
1170 msecs_to_jiffies(1000));
1172 dev_err(&xd->dev, "failed to send properties changed notification\n");
1176 xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
1179 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
1182 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1184 return sprintf(buf, "%#x\n", xd->device);
1186 static DEVICE_ATTR_RO(device);
1189 device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1191 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1194 if (mutex_lock_interruptible(&xd->lock))
1195 return -ERESTARTSYS;
1196 ret = sprintf(buf, "%s\n", xd->device_name ? xd->device_name : "");
1197 mutex_unlock(&xd->lock);
1201 static DEVICE_ATTR_RO(device_name);
1203 static ssize_t maxhopid_show(struct device *dev, struct device_attribute *attr,
1206 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1208 return sprintf(buf, "%d\n", xd->remote_max_hopid);
1210 static DEVICE_ATTR_RO(maxhopid);
1212 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
1215 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1217 return sprintf(buf, "%#x\n", xd->vendor);
1219 static DEVICE_ATTR_RO(vendor);
1222 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1224 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1227 if (mutex_lock_interruptible(&xd->lock))
1228 return -ERESTARTSYS;
1229 ret = sprintf(buf, "%s\n", xd->vendor_name ? xd->vendor_name : "");
1230 mutex_unlock(&xd->lock);
1234 static DEVICE_ATTR_RO(vendor_name);
1236 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1239 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1241 return sprintf(buf, "%pUb\n", xd->remote_uuid);
1243 static DEVICE_ATTR_RO(unique_id);
1245 static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
1248 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1250 return sprintf(buf, "%u.0 Gb/s\n", xd->link_speed);
1253 static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL);
1254 static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL);
1256 static ssize_t lanes_show(struct device *dev, struct device_attribute *attr,
1259 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1261 return sprintf(buf, "%u\n", xd->link_width);
1264 static DEVICE_ATTR(rx_lanes, 0444, lanes_show, NULL);
1265 static DEVICE_ATTR(tx_lanes, 0444, lanes_show, NULL);
1267 static struct attribute *xdomain_attrs[] = {
1268 &dev_attr_device.attr,
1269 &dev_attr_device_name.attr,
1270 &dev_attr_maxhopid.attr,
1271 &dev_attr_rx_lanes.attr,
1272 &dev_attr_rx_speed.attr,
1273 &dev_attr_tx_lanes.attr,
1274 &dev_attr_tx_speed.attr,
1275 &dev_attr_unique_id.attr,
1276 &dev_attr_vendor.attr,
1277 &dev_attr_vendor_name.attr,
1281 static const struct attribute_group xdomain_attr_group = {
1282 .attrs = xdomain_attrs,
1285 static const struct attribute_group *xdomain_attr_groups[] = {
1286 &xdomain_attr_group,
1290 static void tb_xdomain_release(struct device *dev)
1292 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1294 put_device(xd->dev.parent);
1296 kfree(xd->local_property_block);
1297 tb_property_free_dir(xd->remote_properties);
1298 ida_destroy(&xd->out_hopids);
1299 ida_destroy(&xd->in_hopids);
1300 ida_destroy(&xd->service_ids);
1302 kfree(xd->local_uuid);
1303 kfree(xd->remote_uuid);
1304 kfree(xd->device_name);
1305 kfree(xd->vendor_name);
1309 static void start_handshake(struct tb_xdomain *xd)
1311 xd->uuid_retries = XDOMAIN_UUID_RETRIES;
1312 xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
1313 xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
1315 if (xd->needs_uuid) {
1316 queue_delayed_work(xd->tb->wq, &xd->get_uuid_work,
1317 msecs_to_jiffies(100));
1319 /* Start exchanging properties with the other host */
1320 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1321 msecs_to_jiffies(100));
1322 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
1323 msecs_to_jiffies(1000));
1327 static void stop_handshake(struct tb_xdomain *xd)
1329 xd->uuid_retries = 0;
1330 xd->properties_retries = 0;
1331 xd->properties_changed_retries = 0;
1333 cancel_delayed_work_sync(&xd->get_uuid_work);
1334 cancel_delayed_work_sync(&xd->get_properties_work);
1335 cancel_delayed_work_sync(&xd->properties_changed_work);
1338 static int __maybe_unused tb_xdomain_suspend(struct device *dev)
1340 stop_handshake(tb_to_xdomain(dev));
1344 static int __maybe_unused tb_xdomain_resume(struct device *dev)
1346 start_handshake(tb_to_xdomain(dev));
1350 static const struct dev_pm_ops tb_xdomain_pm_ops = {
1351 SET_SYSTEM_SLEEP_PM_OPS(tb_xdomain_suspend, tb_xdomain_resume)
1354 struct device_type tb_xdomain_type = {
1355 .name = "thunderbolt_xdomain",
1356 .release = tb_xdomain_release,
1357 .pm = &tb_xdomain_pm_ops,
1359 EXPORT_SYMBOL_GPL(tb_xdomain_type);
1362 * tb_xdomain_alloc() - Allocate new XDomain object
1363 * @tb: Domain where the XDomain belongs
1364 * @parent: Parent device (the switch through the connection to the
1365 * other domain is reached).
1366 * @route: Route string used to reach the other domain
1367 * @local_uuid: Our local domain UUID
1368 * @remote_uuid: UUID of the other domain (optional)
1370 * Allocates new XDomain structure and returns pointer to that. The
1371 * object must be released by calling tb_xdomain_put().
1373 struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
1374 u64 route, const uuid_t *local_uuid,
1375 const uuid_t *remote_uuid)
1377 struct tb_switch *parent_sw = tb_to_switch(parent);
1378 struct tb_xdomain *xd;
1379 struct tb_port *down;
1381 /* Make sure the downstream domain is accessible */
1382 down = tb_port_at(route, parent_sw);
1383 tb_port_unlock(down);
1385 xd = kzalloc(sizeof(*xd), GFP_KERNEL);
1391 xd->local_max_hopid = down->config.max_in_hop_id;
1392 ida_init(&xd->service_ids);
1393 ida_init(&xd->in_hopids);
1394 ida_init(&xd->out_hopids);
1395 mutex_init(&xd->lock);
1396 INIT_DELAYED_WORK(&xd->get_uuid_work, tb_xdomain_get_uuid);
1397 INIT_DELAYED_WORK(&xd->get_properties_work, tb_xdomain_get_properties);
1398 INIT_DELAYED_WORK(&xd->properties_changed_work,
1399 tb_xdomain_properties_changed);
1401 xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL);
1402 if (!xd->local_uuid)
1406 xd->remote_uuid = kmemdup(remote_uuid, sizeof(uuid_t),
1408 if (!xd->remote_uuid)
1409 goto err_free_local_uuid;
1411 xd->needs_uuid = true;
1414 device_initialize(&xd->dev);
1415 xd->dev.parent = get_device(parent);
1416 xd->dev.bus = &tb_bus_type;
1417 xd->dev.type = &tb_xdomain_type;
1418 xd->dev.groups = xdomain_attr_groups;
1419 dev_set_name(&xd->dev, "%u-%llx", tb->index, route);
1421 dev_dbg(&xd->dev, "local UUID %pUb\n", local_uuid);
1423 dev_dbg(&xd->dev, "remote UUID %pUb\n", remote_uuid);
1426 * This keeps the DMA powered on as long as we have active
1427 * connection to another host.
1429 pm_runtime_set_active(&xd->dev);
1430 pm_runtime_get_noresume(&xd->dev);
1431 pm_runtime_enable(&xd->dev);
1435 err_free_local_uuid:
1436 kfree(xd->local_uuid);
1444 * tb_xdomain_add() - Add XDomain to the bus
1445 * @xd: XDomain to add
1447 * This function starts XDomain discovery protocol handshake and
1448 * eventually adds the XDomain to the bus. After calling this function
1449 * the caller needs to call tb_xdomain_remove() in order to remove and
1450 * release the object regardless whether the handshake succeeded or not.
1452 void tb_xdomain_add(struct tb_xdomain *xd)
1454 /* Start exchanging properties with the other host */
1455 start_handshake(xd);
1458 static int unregister_service(struct device *dev, void *data)
1460 device_unregister(dev);
1465 * tb_xdomain_remove() - Remove XDomain from the bus
1466 * @xd: XDomain to remove
1468 * This will stop all ongoing configuration work and remove the XDomain
1469 * along with any services from the bus. When the last reference to @xd
1470 * is released the object will be released as well.
1472 void tb_xdomain_remove(struct tb_xdomain *xd)
1476 device_for_each_child_reverse(&xd->dev, xd, unregister_service);
1479 * Undo runtime PM here explicitly because it is possible that
1480 * the XDomain was never added to the bus and thus device_del()
1481 * is not called for it (device_del() would handle this otherwise).
1483 pm_runtime_disable(&xd->dev);
1484 pm_runtime_put_noidle(&xd->dev);
1485 pm_runtime_set_suspended(&xd->dev);
1487 if (!device_is_registered(&xd->dev)) {
1488 put_device(&xd->dev);
1490 dev_info(&xd->dev, "host disconnected\n");
1491 device_unregister(&xd->dev);
1496 * tb_xdomain_lane_bonding_enable() - Enable lane bonding on XDomain
1497 * @xd: XDomain connection
1499 * Lane bonding is disabled by default for XDomains. This function tries
1500 * to enable bonding by first enabling the port and waiting for the CL0
1503 * Return: %0 in case of success and negative errno in case of error.
1505 int tb_xdomain_lane_bonding_enable(struct tb_xdomain *xd)
1507 struct tb_port *port;
1510 port = tb_port_at(xd->route, tb_xdomain_parent(xd));
1511 if (!port->dual_link_port)
1514 ret = tb_port_enable(port->dual_link_port);
1518 ret = tb_wait_for_port(port->dual_link_port, true);
1524 ret = tb_port_lane_bonding_enable(port);
1526 tb_port_warn(port, "failed to enable lane bonding\n");
1530 ret = tb_port_wait_for_link_width(port, 2, 100);
1532 tb_port_warn(port, "timeout enabling lane bonding\n");
1536 tb_port_update_credits(port);
1537 tb_xdomain_update_link_attributes(xd);
1539 dev_dbg(&xd->dev, "lane bonding enabled\n");
1542 EXPORT_SYMBOL_GPL(tb_xdomain_lane_bonding_enable);
1545 * tb_xdomain_lane_bonding_disable() - Disable lane bonding
1546 * @xd: XDomain connection
1548 * Lane bonding is disabled by default for XDomains. If bonding has been
1549 * enabled, this function can be used to disable it.
1551 void tb_xdomain_lane_bonding_disable(struct tb_xdomain *xd)
1553 struct tb_port *port;
1555 port = tb_port_at(xd->route, tb_xdomain_parent(xd));
1556 if (port->dual_link_port) {
1557 tb_port_lane_bonding_disable(port);
1558 if (tb_port_wait_for_link_width(port, 1, 100) == -ETIMEDOUT)
1559 tb_port_warn(port, "timeout disabling lane bonding\n");
1560 tb_port_disable(port->dual_link_port);
1561 tb_port_update_credits(port);
1562 tb_xdomain_update_link_attributes(xd);
1564 dev_dbg(&xd->dev, "lane bonding disabled\n");
1567 EXPORT_SYMBOL_GPL(tb_xdomain_lane_bonding_disable);
1570 * tb_xdomain_alloc_in_hopid() - Allocate input HopID for tunneling
1571 * @xd: XDomain connection
1572 * @hopid: Preferred HopID or %-1 for next available
1574 * Returns allocated HopID or negative errno. Specifically returns
1575 * %-ENOSPC if there are no more available HopIDs. Returned HopID is
1576 * guaranteed to be within range supported by the input lane adapter.
1577 * Call tb_xdomain_release_in_hopid() to release the allocated HopID.
1579 int tb_xdomain_alloc_in_hopid(struct tb_xdomain *xd, int hopid)
1582 hopid = TB_PATH_MIN_HOPID;
1583 if (hopid < TB_PATH_MIN_HOPID || hopid > xd->local_max_hopid)
1586 return ida_alloc_range(&xd->in_hopids, hopid, xd->local_max_hopid,
1589 EXPORT_SYMBOL_GPL(tb_xdomain_alloc_in_hopid);
1592 * tb_xdomain_alloc_out_hopid() - Allocate output HopID for tunneling
1593 * @xd: XDomain connection
1594 * @hopid: Preferred HopID or %-1 for next available
1596 * Returns allocated HopID or negative errno. Specifically returns
1597 * %-ENOSPC if there are no more available HopIDs. Returned HopID is
1598 * guaranteed to be within range supported by the output lane adapter.
1599 * Call tb_xdomain_release_in_hopid() to release the allocated HopID.
1601 int tb_xdomain_alloc_out_hopid(struct tb_xdomain *xd, int hopid)
1604 hopid = TB_PATH_MIN_HOPID;
1605 if (hopid < TB_PATH_MIN_HOPID || hopid > xd->remote_max_hopid)
1608 return ida_alloc_range(&xd->out_hopids, hopid, xd->remote_max_hopid,
1611 EXPORT_SYMBOL_GPL(tb_xdomain_alloc_out_hopid);
1614 * tb_xdomain_release_in_hopid() - Release input HopID
1615 * @xd: XDomain connection
1616 * @hopid: HopID to release
1618 void tb_xdomain_release_in_hopid(struct tb_xdomain *xd, int hopid)
1620 ida_free(&xd->in_hopids, hopid);
1622 EXPORT_SYMBOL_GPL(tb_xdomain_release_in_hopid);
1625 * tb_xdomain_release_out_hopid() - Release output HopID
1626 * @xd: XDomain connection
1627 * @hopid: HopID to release
1629 void tb_xdomain_release_out_hopid(struct tb_xdomain *xd, int hopid)
1631 ida_free(&xd->out_hopids, hopid);
1633 EXPORT_SYMBOL_GPL(tb_xdomain_release_out_hopid);
1636 * tb_xdomain_enable_paths() - Enable DMA paths for XDomain connection
1637 * @xd: XDomain connection
1638 * @transmit_path: HopID we are using to send out packets
1639 * @transmit_ring: DMA ring used to send out packets
1640 * @receive_path: HopID the other end is using to send packets to us
1641 * @receive_ring: DMA ring used to receive packets from @receive_path
1643 * The function enables DMA paths accordingly so that after successful
1644 * return the caller can send and receive packets using high-speed DMA
1645 * path. If a transmit or receive path is not needed, pass %-1 for those
1648 * Return: %0 in case of success and negative errno in case of error
1650 int tb_xdomain_enable_paths(struct tb_xdomain *xd, int transmit_path,
1651 int transmit_ring, int receive_path,
1654 return tb_domain_approve_xdomain_paths(xd->tb, xd, transmit_path,
1655 transmit_ring, receive_path,
1658 EXPORT_SYMBOL_GPL(tb_xdomain_enable_paths);
1661 * tb_xdomain_disable_paths() - Disable DMA paths for XDomain connection
1662 * @xd: XDomain connection
1663 * @transmit_path: HopID we are using to send out packets
1664 * @transmit_ring: DMA ring used to send out packets
1665 * @receive_path: HopID the other end is using to send packets to us
1666 * @receive_ring: DMA ring used to receive packets from @receive_path
1668 * This does the opposite of tb_xdomain_enable_paths(). After call to
1669 * this the caller is not expected to use the rings anymore. Passing %-1
1670 * as path/ring parameter means don't care. Normally the callers should
1671 * pass the same values here as they do when paths are enabled.
1673 * Return: %0 in case of success and negative errno in case of error
1675 int tb_xdomain_disable_paths(struct tb_xdomain *xd, int transmit_path,
1676 int transmit_ring, int receive_path,
1679 return tb_domain_disconnect_xdomain_paths(xd->tb, xd, transmit_path,
1680 transmit_ring, receive_path,
1683 EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths);
1685 struct tb_xdomain_lookup {
1692 static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw,
1693 const struct tb_xdomain_lookup *lookup)
1695 struct tb_port *port;
1697 tb_switch_for_each_port(sw, port) {
1698 struct tb_xdomain *xd;
1700 if (port->xdomain) {
1704 if (xd->remote_uuid &&
1705 uuid_equal(xd->remote_uuid, lookup->uuid))
1707 } else if (lookup->link &&
1708 lookup->link == xd->link &&
1709 lookup->depth == xd->depth) {
1711 } else if (lookup->route &&
1712 lookup->route == xd->route) {
1715 } else if (tb_port_has_remote(port)) {
1716 xd = switch_find_xdomain(port->remote->sw, lookup);
1726 * tb_xdomain_find_by_uuid() - Find an XDomain by UUID
1727 * @tb: Domain where the XDomain belongs to
1728 * @uuid: UUID to look for
1730 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1731 * The returned XDomain will have its reference count increased so the
1732 * caller needs to call tb_xdomain_put() when it is done with the
1735 * This will find all XDomains including the ones that are not yet added
1736 * to the bus (handshake is still in progress).
1738 * The caller needs to hold @tb->lock.
1740 struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid)
1742 struct tb_xdomain_lookup lookup;
1743 struct tb_xdomain *xd;
1745 memset(&lookup, 0, sizeof(lookup));
1748 xd = switch_find_xdomain(tb->root_switch, &lookup);
1749 return tb_xdomain_get(xd);
1751 EXPORT_SYMBOL_GPL(tb_xdomain_find_by_uuid);
1754 * tb_xdomain_find_by_link_depth() - Find an XDomain by link and depth
1755 * @tb: Domain where the XDomain belongs to
1756 * @link: Root switch link number
1757 * @depth: Depth in the link
1759 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1760 * The returned XDomain will have its reference count increased so the
1761 * caller needs to call tb_xdomain_put() when it is done with the
1764 * This will find all XDomains including the ones that are not yet added
1765 * to the bus (handshake is still in progress).
1767 * The caller needs to hold @tb->lock.
1769 struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
1772 struct tb_xdomain_lookup lookup;
1773 struct tb_xdomain *xd;
1775 memset(&lookup, 0, sizeof(lookup));
1777 lookup.depth = depth;
1779 xd = switch_find_xdomain(tb->root_switch, &lookup);
1780 return tb_xdomain_get(xd);
1784 * tb_xdomain_find_by_route() - Find an XDomain by route string
1785 * @tb: Domain where the XDomain belongs to
1786 * @route: XDomain route string
1788 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1789 * The returned XDomain will have its reference count increased so the
1790 * caller needs to call tb_xdomain_put() when it is done with the
1793 * This will find all XDomains including the ones that are not yet added
1794 * to the bus (handshake is still in progress).
1796 * The caller needs to hold @tb->lock.
1798 struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route)
1800 struct tb_xdomain_lookup lookup;
1801 struct tb_xdomain *xd;
1803 memset(&lookup, 0, sizeof(lookup));
1804 lookup.route = route;
1806 xd = switch_find_xdomain(tb->root_switch, &lookup);
1807 return tb_xdomain_get(xd);
1809 EXPORT_SYMBOL_GPL(tb_xdomain_find_by_route);
1811 bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
1812 const void *buf, size_t size)
1814 const struct tb_protocol_handler *handler, *tmp;
1815 const struct tb_xdp_header *hdr = buf;
1816 unsigned int length;
1819 /* We expect the packet is at least size of the header */
1820 length = hdr->xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
1821 if (length != size / 4 - sizeof(hdr->xd_hdr) / 4)
1823 if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4)
1827 * Handle XDomain discovery protocol packets directly here. For
1828 * other protocols (based on their UUID) we call registered
1831 if (uuid_equal(&hdr->uuid, &tb_xdp_uuid)) {
1832 if (type == TB_CFG_PKG_XDOMAIN_REQ)
1833 return tb_xdp_schedule_request(tb, hdr, size);
1837 mutex_lock(&xdomain_lock);
1838 list_for_each_entry_safe(handler, tmp, &protocol_handlers, list) {
1839 if (!uuid_equal(&hdr->uuid, handler->uuid))
1842 mutex_unlock(&xdomain_lock);
1843 ret = handler->callback(buf, size, handler->data);
1844 mutex_lock(&xdomain_lock);
1849 mutex_unlock(&xdomain_lock);
1854 static int update_xdomain(struct device *dev, void *data)
1856 struct tb_xdomain *xd;
1858 xd = tb_to_xdomain(dev);
1860 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1861 msecs_to_jiffies(50));
1867 static void update_all_xdomains(void)
1869 bus_for_each_dev(&tb_bus_type, NULL, NULL, update_xdomain);
1872 static bool remove_directory(const char *key, const struct tb_property_dir *dir)
1874 struct tb_property *p;
1876 p = tb_property_find(xdomain_property_dir, key,
1877 TB_PROPERTY_TYPE_DIRECTORY);
1878 if (p && p->value.dir == dir) {
1879 tb_property_remove(p);
1886 * tb_register_property_dir() - Register property directory to the host
1887 * @key: Key (name) of the directory to add
1888 * @dir: Directory to add
1890 * Service drivers can use this function to add new property directory
1891 * to the host available properties. The other connected hosts are
1892 * notified so they can re-read properties of this host if they are
1895 * Return: %0 on success and negative errno on failure
1897 int tb_register_property_dir(const char *key, struct tb_property_dir *dir)
1901 if (WARN_ON(!xdomain_property_dir))
1904 if (!key || strlen(key) > 8)
1907 mutex_lock(&xdomain_lock);
1908 if (tb_property_find(xdomain_property_dir, key,
1909 TB_PROPERTY_TYPE_DIRECTORY)) {
1914 ret = tb_property_add_dir(xdomain_property_dir, key, dir);
1918 xdomain_property_block_gen++;
1920 mutex_unlock(&xdomain_lock);
1921 update_all_xdomains();
1925 mutex_unlock(&xdomain_lock);
1928 EXPORT_SYMBOL_GPL(tb_register_property_dir);
1931 * tb_unregister_property_dir() - Removes property directory from host
1932 * @key: Key (name) of the directory
1933 * @dir: Directory to remove
1935 * This will remove the existing directory from this host and notify the
1936 * connected hosts about the change.
1938 void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir)
1942 mutex_lock(&xdomain_lock);
1943 if (remove_directory(key, dir))
1944 xdomain_property_block_gen++;
1945 mutex_unlock(&xdomain_lock);
1948 update_all_xdomains();
1950 EXPORT_SYMBOL_GPL(tb_unregister_property_dir);
1952 int tb_xdomain_init(void)
1954 xdomain_property_dir = tb_property_create_dir(NULL);
1955 if (!xdomain_property_dir)
1959 * Initialize standard set of properties without any service
1960 * directories. Those will be added by service drivers
1961 * themselves when they are loaded.
1963 * Rest of the properties are filled dynamically based on these
1964 * when the P2P connection is made.
1966 tb_property_add_immediate(xdomain_property_dir, "vendorid",
1967 PCI_VENDOR_ID_INTEL);
1968 tb_property_add_text(xdomain_property_dir, "vendorid", "Intel Corp.");
1969 tb_property_add_immediate(xdomain_property_dir, "deviceid", 0x1);
1970 tb_property_add_immediate(xdomain_property_dir, "devicerv", 0x80000100);
1972 xdomain_property_block_gen = prandom_u32();
1976 void tb_xdomain_exit(void)
1978 tb_property_free_dir(xdomain_property_dir);