1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt XDomain discovery protocol support
5 * Copyright (C) 2017, Intel Corporation
10 #include <linux/device.h>
11 #include <linux/kmod.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/utsname.h>
15 #include <linux/uuid.h>
16 #include <linux/workqueue.h>
20 #define XDOMAIN_DEFAULT_TIMEOUT 5000 /* ms */
21 #define XDOMAIN_UUID_RETRIES 10
22 #define XDOMAIN_PROPERTIES_RETRIES 60
23 #define XDOMAIN_PROPERTIES_CHANGED_RETRIES 10
25 struct xdomain_request_work {
26 struct work_struct work;
27 struct tb_xdp_header *pkg;
31 /* Serializes access to the properties and protocol handlers below */
32 static DEFINE_MUTEX(xdomain_lock);
34 /* Properties exposed to the remote domains */
35 static struct tb_property_dir *xdomain_property_dir;
36 static u32 *xdomain_property_block;
37 static u32 xdomain_property_block_len;
38 static u32 xdomain_property_block_gen;
40 /* Additional protocol handlers */
41 static LIST_HEAD(protocol_handlers);
43 /* UUID for XDomain discovery protocol: b638d70e-42ff-40bb-97c2-90e2c0b2ff07 */
44 static const uuid_t tb_xdp_uuid =
45 UUID_INIT(0xb638d70e, 0x42ff, 0x40bb,
46 0x97, 0xc2, 0x90, 0xe2, 0xc0, 0xb2, 0xff, 0x07);
48 static bool tb_xdomain_match(const struct tb_cfg_request *req,
49 const struct ctl_pkg *pkg)
51 switch (pkg->frame.eof) {
52 case TB_CFG_PKG_ERROR:
55 case TB_CFG_PKG_XDOMAIN_RESP: {
56 const struct tb_xdp_header *res_hdr = pkg->buffer;
57 const struct tb_xdp_header *req_hdr = req->request;
59 if (pkg->frame.size < req->response_size / 4)
62 /* Make sure route matches */
63 if ((res_hdr->xd_hdr.route_hi & ~BIT(31)) !=
64 req_hdr->xd_hdr.route_hi)
66 if ((res_hdr->xd_hdr.route_lo) != req_hdr->xd_hdr.route_lo)
69 /* Check that the XDomain protocol matches */
70 if (!uuid_equal(&res_hdr->uuid, &req_hdr->uuid))
81 static bool tb_xdomain_copy(struct tb_cfg_request *req,
82 const struct ctl_pkg *pkg)
84 memcpy(req->response, pkg->buffer, req->response_size);
89 static void response_ready(void *data)
91 tb_cfg_request_put(data);
94 static int __tb_xdomain_response(struct tb_ctl *ctl, const void *response,
95 size_t size, enum tb_cfg_pkg_type type)
97 struct tb_cfg_request *req;
99 req = tb_cfg_request_alloc();
103 req->match = tb_xdomain_match;
104 req->copy = tb_xdomain_copy;
105 req->request = response;
106 req->request_size = size;
107 req->request_type = type;
109 return tb_cfg_request(ctl, req, response_ready, req);
113 * tb_xdomain_response() - Send a XDomain response message
114 * @xd: XDomain to send the message
115 * @response: Response to send
116 * @size: Size of the response
117 * @type: PDF type of the response
119 * This can be used to send a XDomain response message to the other
120 * domain. No response for the message is expected.
122 * Return: %0 in case of success and negative errno in case of failure
124 int tb_xdomain_response(struct tb_xdomain *xd, const void *response,
125 size_t size, enum tb_cfg_pkg_type type)
127 return __tb_xdomain_response(xd->tb->ctl, response, size, type);
129 EXPORT_SYMBOL_GPL(tb_xdomain_response);
131 static int __tb_xdomain_request(struct tb_ctl *ctl, const void *request,
132 size_t request_size, enum tb_cfg_pkg_type request_type, void *response,
133 size_t response_size, enum tb_cfg_pkg_type response_type,
134 unsigned int timeout_msec)
136 struct tb_cfg_request *req;
137 struct tb_cfg_result res;
139 req = tb_cfg_request_alloc();
143 req->match = tb_xdomain_match;
144 req->copy = tb_xdomain_copy;
145 req->request = request;
146 req->request_size = request_size;
147 req->request_type = request_type;
148 req->response = response;
149 req->response_size = response_size;
150 req->response_type = response_type;
152 res = tb_cfg_request_sync(ctl, req, timeout_msec);
154 tb_cfg_request_put(req);
156 return res.err == 1 ? -EIO : res.err;
160 * tb_xdomain_request() - Send a XDomain request
161 * @xd: XDomain to send the request
162 * @request: Request to send
163 * @request_size: Size of the request in bytes
164 * @request_type: PDF type of the request
165 * @response: Response is copied here
166 * @response_size: Expected size of the response in bytes
167 * @response_type: Expected PDF type of the response
168 * @timeout_msec: Timeout in milliseconds to wait for the response
170 * This function can be used to send XDomain control channel messages to
171 * the other domain. The function waits until the response is received
172 * or when timeout triggers. Whichever comes first.
174 * Return: %0 in case of success and negative errno in case of failure
176 int tb_xdomain_request(struct tb_xdomain *xd, const void *request,
177 size_t request_size, enum tb_cfg_pkg_type request_type,
178 void *response, size_t response_size,
179 enum tb_cfg_pkg_type response_type, unsigned int timeout_msec)
181 return __tb_xdomain_request(xd->tb->ctl, request, request_size,
182 request_type, response, response_size,
183 response_type, timeout_msec);
185 EXPORT_SYMBOL_GPL(tb_xdomain_request);
187 static inline void tb_xdp_fill_header(struct tb_xdp_header *hdr, u64 route,
188 u8 sequence, enum tb_xdp_type type, size_t size)
192 length_sn = (size - sizeof(hdr->xd_hdr)) / 4;
193 length_sn |= (sequence << TB_XDOMAIN_SN_SHIFT) & TB_XDOMAIN_SN_MASK;
195 hdr->xd_hdr.route_hi = upper_32_bits(route);
196 hdr->xd_hdr.route_lo = lower_32_bits(route);
197 hdr->xd_hdr.length_sn = length_sn;
199 memcpy(&hdr->uuid, &tb_xdp_uuid, sizeof(tb_xdp_uuid));
202 static int tb_xdp_handle_error(const struct tb_xdp_header *hdr)
204 const struct tb_xdp_error_response *error;
206 if (hdr->type != ERROR_RESPONSE)
209 error = (const struct tb_xdp_error_response *)hdr;
211 switch (error->error) {
212 case ERROR_UNKNOWN_PACKET:
213 case ERROR_UNKNOWN_DOMAIN:
215 case ERROR_NOT_SUPPORTED:
217 case ERROR_NOT_READY:
226 static int tb_xdp_uuid_request(struct tb_ctl *ctl, u64 route, int retry,
229 struct tb_xdp_uuid_response res;
230 struct tb_xdp_uuid req;
233 memset(&req, 0, sizeof(req));
234 tb_xdp_fill_header(&req.hdr, route, retry % 4, UUID_REQUEST,
237 memset(&res, 0, sizeof(res));
238 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
239 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
240 TB_CFG_PKG_XDOMAIN_RESP,
241 XDOMAIN_DEFAULT_TIMEOUT);
245 ret = tb_xdp_handle_error(&res.hdr);
249 uuid_copy(uuid, &res.src_uuid);
253 static int tb_xdp_uuid_response(struct tb_ctl *ctl, u64 route, u8 sequence,
256 struct tb_xdp_uuid_response res;
258 memset(&res, 0, sizeof(res));
259 tb_xdp_fill_header(&res.hdr, route, sequence, UUID_RESPONSE,
262 uuid_copy(&res.src_uuid, uuid);
263 res.src_route_hi = upper_32_bits(route);
264 res.src_route_lo = lower_32_bits(route);
266 return __tb_xdomain_response(ctl, &res, sizeof(res),
267 TB_CFG_PKG_XDOMAIN_RESP);
270 static int tb_xdp_error_response(struct tb_ctl *ctl, u64 route, u8 sequence,
271 enum tb_xdp_error error)
273 struct tb_xdp_error_response res;
275 memset(&res, 0, sizeof(res));
276 tb_xdp_fill_header(&res.hdr, route, sequence, ERROR_RESPONSE,
280 return __tb_xdomain_response(ctl, &res, sizeof(res),
281 TB_CFG_PKG_XDOMAIN_RESP);
284 static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route,
285 const uuid_t *src_uuid, const uuid_t *dst_uuid, int retry,
286 u32 **block, u32 *generation)
288 struct tb_xdp_properties_response *res;
289 struct tb_xdp_properties req;
295 total_size = sizeof(*res) + TB_XDP_PROPERTIES_MAX_DATA_LENGTH * 4;
296 res = kzalloc(total_size, GFP_KERNEL);
300 memset(&req, 0, sizeof(req));
301 tb_xdp_fill_header(&req.hdr, route, retry % 4, PROPERTIES_REQUEST,
303 memcpy(&req.src_uuid, src_uuid, sizeof(*src_uuid));
304 memcpy(&req.dst_uuid, dst_uuid, sizeof(*dst_uuid));
310 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
311 TB_CFG_PKG_XDOMAIN_REQ, res,
312 total_size, TB_CFG_PKG_XDOMAIN_RESP,
313 XDOMAIN_DEFAULT_TIMEOUT);
317 ret = tb_xdp_handle_error(&res->hdr);
322 * Package length includes the whole payload without the
323 * XDomain header. Validate first that the package is at
324 * least size of the response structure.
326 len = res->hdr.xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
327 if (len < sizeof(*res) / 4) {
332 len += sizeof(res->hdr.xd_hdr) / 4;
333 len -= sizeof(*res) / 4;
335 if (res->offset != req.offset) {
341 * First time allocate block that has enough space for
342 * the whole properties block.
345 data_len = res->data_length;
346 if (data_len > TB_XDP_PROPERTIES_MAX_LENGTH) {
351 data = kcalloc(data_len, sizeof(u32), GFP_KERNEL);
358 memcpy(data + req.offset, res->data, len * 4);
360 } while (!data_len || req.offset < data_len);
363 *generation = res->generation;
376 static int tb_xdp_properties_response(struct tb *tb, struct tb_ctl *ctl,
377 u64 route, u8 sequence, const uuid_t *src_uuid,
378 const struct tb_xdp_properties *req)
380 struct tb_xdp_properties_response *res;
386 * Currently we expect all requests to be directed to us. The
387 * protocol supports forwarding, though which we might add
390 if (!uuid_equal(src_uuid, &req->dst_uuid)) {
391 tb_xdp_error_response(ctl, route, sequence,
392 ERROR_UNKNOWN_DOMAIN);
396 mutex_lock(&xdomain_lock);
398 if (req->offset >= xdomain_property_block_len) {
399 mutex_unlock(&xdomain_lock);
403 len = xdomain_property_block_len - req->offset;
404 len = min_t(u16, len, TB_XDP_PROPERTIES_MAX_DATA_LENGTH);
405 total_size = sizeof(*res) + len * 4;
407 res = kzalloc(total_size, GFP_KERNEL);
409 mutex_unlock(&xdomain_lock);
413 tb_xdp_fill_header(&res->hdr, route, sequence, PROPERTIES_RESPONSE,
415 res->generation = xdomain_property_block_gen;
416 res->data_length = xdomain_property_block_len;
417 res->offset = req->offset;
418 uuid_copy(&res->src_uuid, src_uuid);
419 uuid_copy(&res->dst_uuid, &req->src_uuid);
420 memcpy(res->data, &xdomain_property_block[req->offset], len * 4);
422 mutex_unlock(&xdomain_lock);
424 ret = __tb_xdomain_response(ctl, res, total_size,
425 TB_CFG_PKG_XDOMAIN_RESP);
431 static int tb_xdp_properties_changed_request(struct tb_ctl *ctl, u64 route,
432 int retry, const uuid_t *uuid)
434 struct tb_xdp_properties_changed_response res;
435 struct tb_xdp_properties_changed req;
438 memset(&req, 0, sizeof(req));
439 tb_xdp_fill_header(&req.hdr, route, retry % 4,
440 PROPERTIES_CHANGED_REQUEST, sizeof(req));
441 uuid_copy(&req.src_uuid, uuid);
443 memset(&res, 0, sizeof(res));
444 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
445 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
446 TB_CFG_PKG_XDOMAIN_RESP,
447 XDOMAIN_DEFAULT_TIMEOUT);
451 return tb_xdp_handle_error(&res.hdr);
455 tb_xdp_properties_changed_response(struct tb_ctl *ctl, u64 route, u8 sequence)
457 struct tb_xdp_properties_changed_response res;
459 memset(&res, 0, sizeof(res));
460 tb_xdp_fill_header(&res.hdr, route, sequence,
461 PROPERTIES_CHANGED_RESPONSE, sizeof(res));
462 return __tb_xdomain_response(ctl, &res, sizeof(res),
463 TB_CFG_PKG_XDOMAIN_RESP);
467 * tb_register_protocol_handler() - Register protocol handler
468 * @handler: Handler to register
470 * This allows XDomain service drivers to hook into incoming XDomain
471 * messages. After this function is called the service driver needs to
472 * be able to handle calls to callback whenever a package with the
473 * registered protocol is received.
475 int tb_register_protocol_handler(struct tb_protocol_handler *handler)
477 if (!handler->uuid || !handler->callback)
479 if (uuid_equal(handler->uuid, &tb_xdp_uuid))
482 mutex_lock(&xdomain_lock);
483 list_add_tail(&handler->list, &protocol_handlers);
484 mutex_unlock(&xdomain_lock);
488 EXPORT_SYMBOL_GPL(tb_register_protocol_handler);
491 * tb_unregister_protocol_handler() - Unregister protocol handler
492 * @handler: Handler to unregister
494 * Removes the previously registered protocol handler.
496 void tb_unregister_protocol_handler(struct tb_protocol_handler *handler)
498 mutex_lock(&xdomain_lock);
499 list_del_init(&handler->list);
500 mutex_unlock(&xdomain_lock);
502 EXPORT_SYMBOL_GPL(tb_unregister_protocol_handler);
504 static int rebuild_property_block(void)
509 ret = tb_property_format_dir(xdomain_property_dir, NULL, 0);
515 block = kcalloc(len, sizeof(u32), GFP_KERNEL);
519 ret = tb_property_format_dir(xdomain_property_dir, block, len);
525 kfree(xdomain_property_block);
526 xdomain_property_block = block;
527 xdomain_property_block_len = len;
528 xdomain_property_block_gen++;
533 static void finalize_property_block(void)
535 const struct tb_property *nodename;
538 * On first XDomain connection we set up the the system
539 * nodename. This delayed here because userspace may not have it
540 * set when the driver is first probed.
542 mutex_lock(&xdomain_lock);
543 nodename = tb_property_find(xdomain_property_dir, "deviceid",
544 TB_PROPERTY_TYPE_TEXT);
546 tb_property_add_text(xdomain_property_dir, "deviceid",
547 utsname()->nodename);
548 rebuild_property_block();
550 mutex_unlock(&xdomain_lock);
553 static void tb_xdp_handle_request(struct work_struct *work)
555 struct xdomain_request_work *xw = container_of(work, typeof(*xw), work);
556 const struct tb_xdp_header *pkg = xw->pkg;
557 const struct tb_xdomain_header *xhdr = &pkg->xd_hdr;
558 struct tb *tb = xw->tb;
559 struct tb_ctl *ctl = tb->ctl;
565 route = ((u64)xhdr->route_hi << 32 | xhdr->route_lo) & ~BIT_ULL(63);
566 sequence = xhdr->length_sn & TB_XDOMAIN_SN_MASK;
567 sequence >>= TB_XDOMAIN_SN_SHIFT;
569 mutex_lock(&tb->lock);
571 uuid = tb->root_switch->uuid;
574 mutex_unlock(&tb->lock);
577 tb_xdp_error_response(ctl, route, sequence, ERROR_NOT_READY);
581 finalize_property_block();
584 case PROPERTIES_REQUEST:
585 ret = tb_xdp_properties_response(tb, ctl, route, sequence, uuid,
586 (const struct tb_xdp_properties *)pkg);
589 case PROPERTIES_CHANGED_REQUEST: {
590 const struct tb_xdp_properties_changed *xchg =
591 (const struct tb_xdp_properties_changed *)pkg;
592 struct tb_xdomain *xd;
594 ret = tb_xdp_properties_changed_response(ctl, route, sequence);
597 * Since the properties have been changed, let's update
598 * the xdomain related to this connection as well in
599 * case there is a change in services it offers.
601 xd = tb_xdomain_find_by_uuid_locked(tb, &xchg->src_uuid);
603 queue_delayed_work(tb->wq, &xd->get_properties_work,
604 msecs_to_jiffies(50));
611 case UUID_REQUEST_OLD:
613 ret = tb_xdp_uuid_response(ctl, route, sequence, uuid);
617 tb_xdp_error_response(ctl, route, sequence,
618 ERROR_NOT_SUPPORTED);
623 tb_warn(tb, "failed to send XDomain response for %#x\n",
635 tb_xdp_schedule_request(struct tb *tb, const struct tb_xdp_header *hdr,
638 struct xdomain_request_work *xw;
640 xw = kmalloc(sizeof(*xw), GFP_KERNEL);
644 INIT_WORK(&xw->work, tb_xdp_handle_request);
645 xw->pkg = kmemdup(hdr, size, GFP_KERNEL);
650 xw->tb = tb_domain_get(tb);
652 schedule_work(&xw->work);
657 * tb_register_service_driver() - Register XDomain service driver
658 * @drv: Driver to register
660 * Registers new service driver from @drv to the bus.
662 int tb_register_service_driver(struct tb_service_driver *drv)
664 drv->driver.bus = &tb_bus_type;
665 return driver_register(&drv->driver);
667 EXPORT_SYMBOL_GPL(tb_register_service_driver);
670 * tb_unregister_service_driver() - Unregister XDomain service driver
671 * @xdrv: Driver to unregister
673 * Unregisters XDomain service driver from the bus.
675 void tb_unregister_service_driver(struct tb_service_driver *drv)
677 driver_unregister(&drv->driver);
679 EXPORT_SYMBOL_GPL(tb_unregister_service_driver);
681 static ssize_t key_show(struct device *dev, struct device_attribute *attr,
684 struct tb_service *svc = container_of(dev, struct tb_service, dev);
687 * It should be null terminated but anything else is pretty much
690 return sprintf(buf, "%*pE\n", (int)strlen(svc->key), svc->key);
692 static DEVICE_ATTR_RO(key);
694 static int get_modalias(struct tb_service *svc, char *buf, size_t size)
696 return snprintf(buf, size, "tbsvc:k%sp%08Xv%08Xr%08X", svc->key,
697 svc->prtcid, svc->prtcvers, svc->prtcrevs);
700 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
703 struct tb_service *svc = container_of(dev, struct tb_service, dev);
705 /* Full buffer size except new line and null termination */
706 get_modalias(svc, buf, PAGE_SIZE - 2);
707 return sprintf(buf, "%s\n", buf);
709 static DEVICE_ATTR_RO(modalias);
711 static ssize_t prtcid_show(struct device *dev, struct device_attribute *attr,
714 struct tb_service *svc = container_of(dev, struct tb_service, dev);
716 return sprintf(buf, "%u\n", svc->prtcid);
718 static DEVICE_ATTR_RO(prtcid);
720 static ssize_t prtcvers_show(struct device *dev, struct device_attribute *attr,
723 struct tb_service *svc = container_of(dev, struct tb_service, dev);
725 return sprintf(buf, "%u\n", svc->prtcvers);
727 static DEVICE_ATTR_RO(prtcvers);
729 static ssize_t prtcrevs_show(struct device *dev, struct device_attribute *attr,
732 struct tb_service *svc = container_of(dev, struct tb_service, dev);
734 return sprintf(buf, "%u\n", svc->prtcrevs);
736 static DEVICE_ATTR_RO(prtcrevs);
738 static ssize_t prtcstns_show(struct device *dev, struct device_attribute *attr,
741 struct tb_service *svc = container_of(dev, struct tb_service, dev);
743 return sprintf(buf, "0x%08x\n", svc->prtcstns);
745 static DEVICE_ATTR_RO(prtcstns);
747 static struct attribute *tb_service_attrs[] = {
749 &dev_attr_modalias.attr,
750 &dev_attr_prtcid.attr,
751 &dev_attr_prtcvers.attr,
752 &dev_attr_prtcrevs.attr,
753 &dev_attr_prtcstns.attr,
757 static struct attribute_group tb_service_attr_group = {
758 .attrs = tb_service_attrs,
761 static const struct attribute_group *tb_service_attr_groups[] = {
762 &tb_service_attr_group,
766 static int tb_service_uevent(struct device *dev, struct kobj_uevent_env *env)
768 struct tb_service *svc = container_of(dev, struct tb_service, dev);
771 get_modalias(svc, modalias, sizeof(modalias));
772 return add_uevent_var(env, "MODALIAS=%s", modalias);
775 static void tb_service_release(struct device *dev)
777 struct tb_service *svc = container_of(dev, struct tb_service, dev);
778 struct tb_xdomain *xd = tb_service_parent(svc);
780 ida_simple_remove(&xd->service_ids, svc->id);
785 struct device_type tb_service_type = {
786 .name = "thunderbolt_service",
787 .groups = tb_service_attr_groups,
788 .uevent = tb_service_uevent,
789 .release = tb_service_release,
791 EXPORT_SYMBOL_GPL(tb_service_type);
793 static int remove_missing_service(struct device *dev, void *data)
795 struct tb_xdomain *xd = data;
796 struct tb_service *svc;
798 svc = tb_to_service(dev);
802 if (!tb_property_find(xd->properties, svc->key,
803 TB_PROPERTY_TYPE_DIRECTORY))
804 device_unregister(dev);
809 static int find_service(struct device *dev, void *data)
811 const struct tb_property *p = data;
812 struct tb_service *svc;
814 svc = tb_to_service(dev);
818 return !strcmp(svc->key, p->key);
821 static int populate_service(struct tb_service *svc,
822 struct tb_property *property)
824 struct tb_property_dir *dir = property->value.dir;
825 struct tb_property *p;
827 /* Fill in standard properties */
828 p = tb_property_find(dir, "prtcid", TB_PROPERTY_TYPE_VALUE);
830 svc->prtcid = p->value.immediate;
831 p = tb_property_find(dir, "prtcvers", TB_PROPERTY_TYPE_VALUE);
833 svc->prtcvers = p->value.immediate;
834 p = tb_property_find(dir, "prtcrevs", TB_PROPERTY_TYPE_VALUE);
836 svc->prtcrevs = p->value.immediate;
837 p = tb_property_find(dir, "prtcstns", TB_PROPERTY_TYPE_VALUE);
839 svc->prtcstns = p->value.immediate;
841 svc->key = kstrdup(property->key, GFP_KERNEL);
848 static void enumerate_services(struct tb_xdomain *xd)
850 struct tb_service *svc;
851 struct tb_property *p;
856 * First remove all services that are not available anymore in
857 * the updated property block.
859 device_for_each_child_reverse(&xd->dev, xd, remove_missing_service);
861 /* Then re-enumerate properties creating new services as we go */
862 tb_property_for_each(xd->properties, p) {
863 if (p->type != TB_PROPERTY_TYPE_DIRECTORY)
866 /* If the service exists already we are fine */
867 dev = device_find_child(&xd->dev, p, find_service);
873 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
877 if (populate_service(svc, p)) {
882 id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
888 svc->dev.bus = &tb_bus_type;
889 svc->dev.type = &tb_service_type;
890 svc->dev.parent = &xd->dev;
891 dev_set_name(&svc->dev, "%s.%d", dev_name(&xd->dev), svc->id);
893 if (device_register(&svc->dev)) {
894 put_device(&svc->dev);
900 static int populate_properties(struct tb_xdomain *xd,
901 struct tb_property_dir *dir)
903 const struct tb_property *p;
905 /* Required properties */
906 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_VALUE);
909 xd->device = p->value.immediate;
911 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_VALUE);
914 xd->vendor = p->value.immediate;
916 kfree(xd->device_name);
917 xd->device_name = NULL;
918 kfree(xd->vendor_name);
919 xd->vendor_name = NULL;
921 /* Optional properties */
922 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_TEXT);
924 xd->device_name = kstrdup(p->value.text, GFP_KERNEL);
925 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_TEXT);
927 xd->vendor_name = kstrdup(p->value.text, GFP_KERNEL);
932 /* Called with @xd->lock held */
933 static void tb_xdomain_restore_paths(struct tb_xdomain *xd)
939 if (xd->transmit_path) {
940 dev_dbg(&xd->dev, "re-establishing DMA path\n");
941 tb_domain_approve_xdomain_paths(xd->tb, xd);
945 static void tb_xdomain_get_uuid(struct work_struct *work)
947 struct tb_xdomain *xd = container_of(work, typeof(*xd),
949 struct tb *tb = xd->tb;
953 ret = tb_xdp_uuid_request(tb->ctl, xd->route, xd->uuid_retries, &uuid);
955 if (xd->uuid_retries-- > 0) {
956 queue_delayed_work(xd->tb->wq, &xd->get_uuid_work,
957 msecs_to_jiffies(100));
959 dev_dbg(&xd->dev, "failed to read remote UUID\n");
964 if (uuid_equal(&uuid, xd->local_uuid)) {
965 dev_dbg(&xd->dev, "intra-domain loop detected\n");
970 * If the UUID is different, there is another domain connected
971 * so mark this one unplugged and wait for the connection
972 * manager to replace it.
974 if (xd->remote_uuid && !uuid_equal(&uuid, xd->remote_uuid)) {
975 dev_dbg(&xd->dev, "remote UUID is different, unplugging\n");
976 xd->is_unplugged = true;
980 /* First time fill in the missing UUID */
981 if (!xd->remote_uuid) {
982 xd->remote_uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL);
983 if (!xd->remote_uuid)
987 /* Now we can start the normal properties exchange */
988 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
989 msecs_to_jiffies(100));
990 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
991 msecs_to_jiffies(1000));
994 static void tb_xdomain_get_properties(struct work_struct *work)
996 struct tb_xdomain *xd = container_of(work, typeof(*xd),
997 get_properties_work.work);
998 struct tb_property_dir *dir;
999 struct tb *tb = xd->tb;
1000 bool update = false;
1005 ret = tb_xdp_properties_request(tb->ctl, xd->route, xd->local_uuid,
1006 xd->remote_uuid, xd->properties_retries,
1009 if (xd->properties_retries-- > 0) {
1010 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
1011 msecs_to_jiffies(1000));
1015 "failed read XDomain properties from %pUb\n",
1021 xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
1023 mutex_lock(&xd->lock);
1025 /* Only accept newer generation properties */
1026 if (xd->properties && gen <= xd->property_block_gen) {
1028 * On resume it is likely that the properties block is
1029 * not changed (unless the other end added or removed
1030 * services). However, we need to make sure the existing
1031 * DMA paths are restored properly.
1033 tb_xdomain_restore_paths(xd);
1034 goto err_free_block;
1037 dir = tb_property_parse_dir(block, ret);
1039 dev_err(&xd->dev, "failed to parse XDomain properties\n");
1040 goto err_free_block;
1043 ret = populate_properties(xd, dir);
1045 dev_err(&xd->dev, "missing XDomain properties in response\n");
1049 /* Release the existing one */
1050 if (xd->properties) {
1051 tb_property_free_dir(xd->properties);
1055 xd->properties = dir;
1056 xd->property_block_gen = gen;
1058 tb_xdomain_restore_paths(xd);
1060 mutex_unlock(&xd->lock);
1065 * Now the device should be ready enough so we can add it to the
1066 * bus and let userspace know about it. If the device is already
1067 * registered, we notify the userspace that it has changed.
1070 if (device_add(&xd->dev)) {
1071 dev_err(&xd->dev, "failed to add XDomain device\n");
1075 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
1078 enumerate_services(xd);
1082 tb_property_free_dir(dir);
1085 mutex_unlock(&xd->lock);
1088 static void tb_xdomain_properties_changed(struct work_struct *work)
1090 struct tb_xdomain *xd = container_of(work, typeof(*xd),
1091 properties_changed_work.work);
1094 ret = tb_xdp_properties_changed_request(xd->tb->ctl, xd->route,
1095 xd->properties_changed_retries, xd->local_uuid);
1097 if (xd->properties_changed_retries-- > 0)
1098 queue_delayed_work(xd->tb->wq,
1099 &xd->properties_changed_work,
1100 msecs_to_jiffies(1000));
1104 xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
1107 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
1110 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1112 return sprintf(buf, "%#x\n", xd->device);
1114 static DEVICE_ATTR_RO(device);
1117 device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1119 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1122 if (mutex_lock_interruptible(&xd->lock))
1123 return -ERESTARTSYS;
1124 ret = sprintf(buf, "%s\n", xd->device_name ? xd->device_name : "");
1125 mutex_unlock(&xd->lock);
1129 static DEVICE_ATTR_RO(device_name);
1131 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
1134 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1136 return sprintf(buf, "%#x\n", xd->vendor);
1138 static DEVICE_ATTR_RO(vendor);
1141 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1143 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1146 if (mutex_lock_interruptible(&xd->lock))
1147 return -ERESTARTSYS;
1148 ret = sprintf(buf, "%s\n", xd->vendor_name ? xd->vendor_name : "");
1149 mutex_unlock(&xd->lock);
1153 static DEVICE_ATTR_RO(vendor_name);
1155 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1158 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1160 return sprintf(buf, "%pUb\n", xd->remote_uuid);
1162 static DEVICE_ATTR_RO(unique_id);
1164 static struct attribute *xdomain_attrs[] = {
1165 &dev_attr_device.attr,
1166 &dev_attr_device_name.attr,
1167 &dev_attr_unique_id.attr,
1168 &dev_attr_vendor.attr,
1169 &dev_attr_vendor_name.attr,
1173 static struct attribute_group xdomain_attr_group = {
1174 .attrs = xdomain_attrs,
1177 static const struct attribute_group *xdomain_attr_groups[] = {
1178 &xdomain_attr_group,
1182 static void tb_xdomain_release(struct device *dev)
1184 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1186 put_device(xd->dev.parent);
1188 tb_property_free_dir(xd->properties);
1189 ida_destroy(&xd->service_ids);
1191 kfree(xd->local_uuid);
1192 kfree(xd->remote_uuid);
1193 kfree(xd->device_name);
1194 kfree(xd->vendor_name);
1198 static void start_handshake(struct tb_xdomain *xd)
1200 xd->uuid_retries = XDOMAIN_UUID_RETRIES;
1201 xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
1202 xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
1204 if (xd->needs_uuid) {
1205 queue_delayed_work(xd->tb->wq, &xd->get_uuid_work,
1206 msecs_to_jiffies(100));
1208 /* Start exchanging properties with the other host */
1209 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1210 msecs_to_jiffies(100));
1211 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
1212 msecs_to_jiffies(1000));
1216 static void stop_handshake(struct tb_xdomain *xd)
1218 xd->uuid_retries = 0;
1219 xd->properties_retries = 0;
1220 xd->properties_changed_retries = 0;
1222 cancel_delayed_work_sync(&xd->get_uuid_work);
1223 cancel_delayed_work_sync(&xd->get_properties_work);
1224 cancel_delayed_work_sync(&xd->properties_changed_work);
1227 static int __maybe_unused tb_xdomain_suspend(struct device *dev)
1229 stop_handshake(tb_to_xdomain(dev));
1233 static int __maybe_unused tb_xdomain_resume(struct device *dev)
1235 struct tb_xdomain *xd = tb_to_xdomain(dev);
1238 * Ask tb_xdomain_get_properties() restore any existing DMA
1239 * paths after properties are re-read.
1242 start_handshake(xd);
1247 static const struct dev_pm_ops tb_xdomain_pm_ops = {
1248 SET_SYSTEM_SLEEP_PM_OPS(tb_xdomain_suspend, tb_xdomain_resume)
1251 struct device_type tb_xdomain_type = {
1252 .name = "thunderbolt_xdomain",
1253 .release = tb_xdomain_release,
1254 .pm = &tb_xdomain_pm_ops,
1256 EXPORT_SYMBOL_GPL(tb_xdomain_type);
1259 * tb_xdomain_alloc() - Allocate new XDomain object
1260 * @tb: Domain where the XDomain belongs
1261 * @parent: Parent device (the switch through the connection to the
1262 * other domain is reached).
1263 * @route: Route string used to reach the other domain
1264 * @local_uuid: Our local domain UUID
1265 * @remote_uuid: UUID of the other domain (optional)
1267 * Allocates new XDomain structure and returns pointer to that. The
1268 * object must be released by calling tb_xdomain_put().
1270 struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
1271 u64 route, const uuid_t *local_uuid,
1272 const uuid_t *remote_uuid)
1274 struct tb_switch *parent_sw = tb_to_switch(parent);
1275 struct tb_xdomain *xd;
1276 struct tb_port *down;
1278 /* Make sure the downstream domain is accessible */
1279 down = tb_port_at(route, parent_sw);
1280 tb_port_unlock(down);
1282 xd = kzalloc(sizeof(*xd), GFP_KERNEL);
1288 ida_init(&xd->service_ids);
1289 mutex_init(&xd->lock);
1290 INIT_DELAYED_WORK(&xd->get_uuid_work, tb_xdomain_get_uuid);
1291 INIT_DELAYED_WORK(&xd->get_properties_work, tb_xdomain_get_properties);
1292 INIT_DELAYED_WORK(&xd->properties_changed_work,
1293 tb_xdomain_properties_changed);
1295 xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL);
1296 if (!xd->local_uuid)
1300 xd->remote_uuid = kmemdup(remote_uuid, sizeof(uuid_t),
1302 if (!xd->remote_uuid)
1303 goto err_free_local_uuid;
1305 xd->needs_uuid = true;
1308 device_initialize(&xd->dev);
1309 xd->dev.parent = get_device(parent);
1310 xd->dev.bus = &tb_bus_type;
1311 xd->dev.type = &tb_xdomain_type;
1312 xd->dev.groups = xdomain_attr_groups;
1313 dev_set_name(&xd->dev, "%u-%llx", tb->index, route);
1316 * This keeps the DMA powered on as long as we have active
1317 * connection to another host.
1319 pm_runtime_set_active(&xd->dev);
1320 pm_runtime_get_noresume(&xd->dev);
1321 pm_runtime_enable(&xd->dev);
1325 err_free_local_uuid:
1326 kfree(xd->local_uuid);
1334 * tb_xdomain_add() - Add XDomain to the bus
1335 * @xd: XDomain to add
1337 * This function starts XDomain discovery protocol handshake and
1338 * eventually adds the XDomain to the bus. After calling this function
1339 * the caller needs to call tb_xdomain_remove() in order to remove and
1340 * release the object regardless whether the handshake succeeded or not.
1342 void tb_xdomain_add(struct tb_xdomain *xd)
1344 /* Start exchanging properties with the other host */
1345 start_handshake(xd);
1348 static int unregister_service(struct device *dev, void *data)
1350 device_unregister(dev);
1355 * tb_xdomain_remove() - Remove XDomain from the bus
1356 * @xd: XDomain to remove
1358 * This will stop all ongoing configuration work and remove the XDomain
1359 * along with any services from the bus. When the last reference to @xd
1360 * is released the object will be released as well.
1362 void tb_xdomain_remove(struct tb_xdomain *xd)
1366 device_for_each_child_reverse(&xd->dev, xd, unregister_service);
1369 * Undo runtime PM here explicitly because it is possible that
1370 * the XDomain was never added to the bus and thus device_del()
1371 * is not called for it (device_del() would handle this otherwise).
1373 pm_runtime_disable(&xd->dev);
1374 pm_runtime_put_noidle(&xd->dev);
1375 pm_runtime_set_suspended(&xd->dev);
1377 if (!device_is_registered(&xd->dev))
1378 put_device(&xd->dev);
1380 device_unregister(&xd->dev);
1384 * tb_xdomain_enable_paths() - Enable DMA paths for XDomain connection
1385 * @xd: XDomain connection
1386 * @transmit_path: HopID of the transmit path the other end is using to
1388 * @transmit_ring: DMA ring used to receive packets from the other end
1389 * @receive_path: HopID of the receive path the other end is using to
1391 * @receive_ring: DMA ring used to send packets to the other end
1393 * The function enables DMA paths accordingly so that after successful
1394 * return the caller can send and receive packets using high-speed DMA
1397 * Return: %0 in case of success and negative errno in case of error
1399 int tb_xdomain_enable_paths(struct tb_xdomain *xd, u16 transmit_path,
1400 u16 transmit_ring, u16 receive_path,
1405 mutex_lock(&xd->lock);
1407 if (xd->transmit_path) {
1408 ret = xd->transmit_path == transmit_path ? 0 : -EBUSY;
1412 xd->transmit_path = transmit_path;
1413 xd->transmit_ring = transmit_ring;
1414 xd->receive_path = receive_path;
1415 xd->receive_ring = receive_ring;
1417 ret = tb_domain_approve_xdomain_paths(xd->tb, xd);
1420 mutex_unlock(&xd->lock);
1424 EXPORT_SYMBOL_GPL(tb_xdomain_enable_paths);
1427 * tb_xdomain_disable_paths() - Disable DMA paths for XDomain connection
1428 * @xd: XDomain connection
1430 * This does the opposite of tb_xdomain_enable_paths(). After call to
1431 * this the caller is not expected to use the rings anymore.
1433 * Return: %0 in case of success and negative errno in case of error
1435 int tb_xdomain_disable_paths(struct tb_xdomain *xd)
1439 mutex_lock(&xd->lock);
1440 if (xd->transmit_path) {
1441 xd->transmit_path = 0;
1442 xd->transmit_ring = 0;
1443 xd->receive_path = 0;
1444 xd->receive_ring = 0;
1446 ret = tb_domain_disconnect_xdomain_paths(xd->tb, xd);
1448 mutex_unlock(&xd->lock);
1452 EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths);
1454 struct tb_xdomain_lookup {
1461 static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw,
1462 const struct tb_xdomain_lookup *lookup)
1464 struct tb_port *port;
1466 tb_switch_for_each_port(sw, port) {
1467 struct tb_xdomain *xd;
1469 if (port->xdomain) {
1473 if (xd->remote_uuid &&
1474 uuid_equal(xd->remote_uuid, lookup->uuid))
1476 } else if (lookup->link &&
1477 lookup->link == xd->link &&
1478 lookup->depth == xd->depth) {
1480 } else if (lookup->route &&
1481 lookup->route == xd->route) {
1484 } else if (tb_port_has_remote(port)) {
1485 xd = switch_find_xdomain(port->remote->sw, lookup);
1495 * tb_xdomain_find_by_uuid() - Find an XDomain by UUID
1496 * @tb: Domain where the XDomain belongs to
1497 * @uuid: UUID to look for
1499 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1500 * The returned XDomain will have its reference count increased so the
1501 * caller needs to call tb_xdomain_put() when it is done with the
1504 * This will find all XDomains including the ones that are not yet added
1505 * to the bus (handshake is still in progress).
1507 * The caller needs to hold @tb->lock.
1509 struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid)
1511 struct tb_xdomain_lookup lookup;
1512 struct tb_xdomain *xd;
1514 memset(&lookup, 0, sizeof(lookup));
1517 xd = switch_find_xdomain(tb->root_switch, &lookup);
1518 return tb_xdomain_get(xd);
1520 EXPORT_SYMBOL_GPL(tb_xdomain_find_by_uuid);
1523 * tb_xdomain_find_by_link_depth() - Find an XDomain by link and depth
1524 * @tb: Domain where the XDomain belongs to
1525 * @link: Root switch link number
1526 * @depth: Depth in the link
1528 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1529 * The returned XDomain will have its reference count increased so the
1530 * caller needs to call tb_xdomain_put() when it is done with the
1533 * This will find all XDomains including the ones that are not yet added
1534 * to the bus (handshake is still in progress).
1536 * The caller needs to hold @tb->lock.
1538 struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
1541 struct tb_xdomain_lookup lookup;
1542 struct tb_xdomain *xd;
1544 memset(&lookup, 0, sizeof(lookup));
1546 lookup.depth = depth;
1548 xd = switch_find_xdomain(tb->root_switch, &lookup);
1549 return tb_xdomain_get(xd);
1553 * tb_xdomain_find_by_route() - Find an XDomain by route string
1554 * @tb: Domain where the XDomain belongs to
1555 * @route: XDomain route string
1557 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1558 * The returned XDomain will have its reference count increased so the
1559 * caller needs to call tb_xdomain_put() when it is done with the
1562 * This will find all XDomains including the ones that are not yet added
1563 * to the bus (handshake is still in progress).
1565 * The caller needs to hold @tb->lock.
1567 struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route)
1569 struct tb_xdomain_lookup lookup;
1570 struct tb_xdomain *xd;
1572 memset(&lookup, 0, sizeof(lookup));
1573 lookup.route = route;
1575 xd = switch_find_xdomain(tb->root_switch, &lookup);
1576 return tb_xdomain_get(xd);
1578 EXPORT_SYMBOL_GPL(tb_xdomain_find_by_route);
1580 bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
1581 const void *buf, size_t size)
1583 const struct tb_protocol_handler *handler, *tmp;
1584 const struct tb_xdp_header *hdr = buf;
1585 unsigned int length;
1588 /* We expect the packet is at least size of the header */
1589 length = hdr->xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
1590 if (length != size / 4 - sizeof(hdr->xd_hdr) / 4)
1592 if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4)
1596 * Handle XDomain discovery protocol packets directly here. For
1597 * other protocols (based on their UUID) we call registered
1600 if (uuid_equal(&hdr->uuid, &tb_xdp_uuid)) {
1601 if (type == TB_CFG_PKG_XDOMAIN_REQ)
1602 return tb_xdp_schedule_request(tb, hdr, size);
1606 mutex_lock(&xdomain_lock);
1607 list_for_each_entry_safe(handler, tmp, &protocol_handlers, list) {
1608 if (!uuid_equal(&hdr->uuid, handler->uuid))
1611 mutex_unlock(&xdomain_lock);
1612 ret = handler->callback(buf, size, handler->data);
1613 mutex_lock(&xdomain_lock);
1618 mutex_unlock(&xdomain_lock);
1623 static int update_xdomain(struct device *dev, void *data)
1625 struct tb_xdomain *xd;
1627 xd = tb_to_xdomain(dev);
1629 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1630 msecs_to_jiffies(50));
1636 static void update_all_xdomains(void)
1638 bus_for_each_dev(&tb_bus_type, NULL, NULL, update_xdomain);
1641 static bool remove_directory(const char *key, const struct tb_property_dir *dir)
1643 struct tb_property *p;
1645 p = tb_property_find(xdomain_property_dir, key,
1646 TB_PROPERTY_TYPE_DIRECTORY);
1647 if (p && p->value.dir == dir) {
1648 tb_property_remove(p);
1655 * tb_register_property_dir() - Register property directory to the host
1656 * @key: Key (name) of the directory to add
1657 * @dir: Directory to add
1659 * Service drivers can use this function to add new property directory
1660 * to the host available properties. The other connected hosts are
1661 * notified so they can re-read properties of this host if they are
1664 * Return: %0 on success and negative errno on failure
1666 int tb_register_property_dir(const char *key, struct tb_property_dir *dir)
1670 if (WARN_ON(!xdomain_property_dir))
1673 if (!key || strlen(key) > 8)
1676 mutex_lock(&xdomain_lock);
1677 if (tb_property_find(xdomain_property_dir, key,
1678 TB_PROPERTY_TYPE_DIRECTORY)) {
1683 ret = tb_property_add_dir(xdomain_property_dir, key, dir);
1687 ret = rebuild_property_block();
1689 remove_directory(key, dir);
1693 mutex_unlock(&xdomain_lock);
1694 update_all_xdomains();
1698 mutex_unlock(&xdomain_lock);
1701 EXPORT_SYMBOL_GPL(tb_register_property_dir);
1704 * tb_unregister_property_dir() - Removes property directory from host
1705 * @key: Key (name) of the directory
1706 * @dir: Directory to remove
1708 * This will remove the existing directory from this host and notify the
1709 * connected hosts about the change.
1711 void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir)
1715 mutex_lock(&xdomain_lock);
1716 if (remove_directory(key, dir))
1717 ret = rebuild_property_block();
1718 mutex_unlock(&xdomain_lock);
1721 update_all_xdomains();
1723 EXPORT_SYMBOL_GPL(tb_unregister_property_dir);
1725 int tb_xdomain_init(void)
1727 xdomain_property_dir = tb_property_create_dir(NULL);
1728 if (!xdomain_property_dir)
1732 * Initialize standard set of properties without any service
1733 * directories. Those will be added by service drivers
1734 * themselves when they are loaded.
1736 * We also add node name later when first connection is made.
1738 tb_property_add_immediate(xdomain_property_dir, "vendorid",
1739 PCI_VENDOR_ID_INTEL);
1740 tb_property_add_text(xdomain_property_dir, "vendorid", "Intel Corp.");
1741 tb_property_add_immediate(xdomain_property_dir, "deviceid", 0x1);
1742 tb_property_add_immediate(xdomain_property_dir, "devicerv", 0x80000100);
1747 void tb_xdomain_exit(void)
1749 kfree(xdomain_property_block);
1750 tb_property_free_dir(xdomain_property_dir);