2 * Thunderbolt XDomain discovery protocol support
4 * Copyright (C) 2017, Intel Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/device.h>
14 #include <linux/kmod.h>
15 #include <linux/module.h>
16 #include <linux/utsname.h>
17 #include <linux/uuid.h>
18 #include <linux/workqueue.h>
22 #define XDOMAIN_DEFAULT_TIMEOUT 5000 /* ms */
23 #define XDOMAIN_PROPERTIES_RETRIES 60
24 #define XDOMAIN_PROPERTIES_CHANGED_RETRIES 10
26 struct xdomain_request_work {
27 struct work_struct work;
28 struct tb_xdp_header *pkg;
32 /* Serializes access to the properties and protocol handlers below */
33 static DEFINE_MUTEX(xdomain_lock);
35 /* Properties exposed to the remote domains */
36 static struct tb_property_dir *xdomain_property_dir;
37 static u32 *xdomain_property_block;
38 static u32 xdomain_property_block_len;
39 static u32 xdomain_property_block_gen;
41 /* Additional protocol handlers */
42 static LIST_HEAD(protocol_handlers);
44 /* UUID for XDomain discovery protocol: b638d70e-42ff-40bb-97c2-90e2c0b2ff07 */
45 static const uuid_t tb_xdp_uuid =
46 UUID_INIT(0xb638d70e, 0x42ff, 0x40bb,
47 0x97, 0xc2, 0x90, 0xe2, 0xc0, 0xb2, 0xff, 0x07);
49 static bool tb_xdomain_match(const struct tb_cfg_request *req,
50 const struct ctl_pkg *pkg)
52 switch (pkg->frame.eof) {
53 case TB_CFG_PKG_ERROR:
56 case TB_CFG_PKG_XDOMAIN_RESP: {
57 const struct tb_xdp_header *res_hdr = pkg->buffer;
58 const struct tb_xdp_header *req_hdr = req->request;
61 if (pkg->frame.size < req->response_size / 4)
64 /* Make sure route matches */
65 if ((res_hdr->xd_hdr.route_hi & ~BIT(31)) !=
66 req_hdr->xd_hdr.route_hi)
68 if ((res_hdr->xd_hdr.route_lo) != req_hdr->xd_hdr.route_lo)
71 /* Then check that the sequence number matches */
72 res_seq = res_hdr->xd_hdr.length_sn & TB_XDOMAIN_SN_MASK;
73 res_seq >>= TB_XDOMAIN_SN_SHIFT;
74 req_seq = req_hdr->xd_hdr.length_sn & TB_XDOMAIN_SN_MASK;
75 req_seq >>= TB_XDOMAIN_SN_SHIFT;
76 if (res_seq != req_seq)
79 /* Check that the XDomain protocol matches */
80 if (!uuid_equal(&res_hdr->uuid, &req_hdr->uuid))
91 static bool tb_xdomain_copy(struct tb_cfg_request *req,
92 const struct ctl_pkg *pkg)
94 memcpy(req->response, pkg->buffer, req->response_size);
99 static void response_ready(void *data)
101 tb_cfg_request_put(data);
104 static int __tb_xdomain_response(struct tb_ctl *ctl, const void *response,
105 size_t size, enum tb_cfg_pkg_type type)
107 struct tb_cfg_request *req;
109 req = tb_cfg_request_alloc();
113 req->match = tb_xdomain_match;
114 req->copy = tb_xdomain_copy;
115 req->request = response;
116 req->request_size = size;
117 req->request_type = type;
119 return tb_cfg_request(ctl, req, response_ready, req);
123 * tb_xdomain_response() - Send a XDomain response message
124 * @xd: XDomain to send the message
125 * @response: Response to send
126 * @size: Size of the response
127 * @type: PDF type of the response
129 * This can be used to send a XDomain response message to the other
130 * domain. No response for the message is expected.
132 * Return: %0 in case of success and negative errno in case of failure
134 int tb_xdomain_response(struct tb_xdomain *xd, const void *response,
135 size_t size, enum tb_cfg_pkg_type type)
137 return __tb_xdomain_response(xd->tb->ctl, response, size, type);
139 EXPORT_SYMBOL_GPL(tb_xdomain_response);
141 static int __tb_xdomain_request(struct tb_ctl *ctl, const void *request,
142 size_t request_size, enum tb_cfg_pkg_type request_type, void *response,
143 size_t response_size, enum tb_cfg_pkg_type response_type,
144 unsigned int timeout_msec)
146 struct tb_cfg_request *req;
147 struct tb_cfg_result res;
149 req = tb_cfg_request_alloc();
153 req->match = tb_xdomain_match;
154 req->copy = tb_xdomain_copy;
155 req->request = request;
156 req->request_size = request_size;
157 req->request_type = request_type;
158 req->response = response;
159 req->response_size = response_size;
160 req->response_type = response_type;
162 res = tb_cfg_request_sync(ctl, req, timeout_msec);
164 tb_cfg_request_put(req);
166 return res.err == 1 ? -EIO : res.err;
170 * tb_xdomain_request() - Send a XDomain request
171 * @xd: XDomain to send the request
172 * @request: Request to send
173 * @request_size: Size of the request in bytes
174 * @request_type: PDF type of the request
175 * @response: Response is copied here
176 * @response_size: Expected size of the response in bytes
177 * @response_type: Expected PDF type of the response
178 * @timeout_msec: Timeout in milliseconds to wait for the response
180 * This function can be used to send XDomain control channel messages to
181 * the other domain. The function waits until the response is received
182 * or when timeout triggers. Whichever comes first.
184 * Return: %0 in case of success and negative errno in case of failure
186 int tb_xdomain_request(struct tb_xdomain *xd, const void *request,
187 size_t request_size, enum tb_cfg_pkg_type request_type,
188 void *response, size_t response_size,
189 enum tb_cfg_pkg_type response_type, unsigned int timeout_msec)
191 return __tb_xdomain_request(xd->tb->ctl, request, request_size,
192 request_type, response, response_size,
193 response_type, timeout_msec);
195 EXPORT_SYMBOL_GPL(tb_xdomain_request);
197 static inline void tb_xdp_fill_header(struct tb_xdp_header *hdr, u64 route,
198 u8 sequence, enum tb_xdp_type type, size_t size)
202 length_sn = (size - sizeof(hdr->xd_hdr)) / 4;
203 length_sn |= (sequence << TB_XDOMAIN_SN_SHIFT) & TB_XDOMAIN_SN_MASK;
205 hdr->xd_hdr.route_hi = upper_32_bits(route);
206 hdr->xd_hdr.route_lo = lower_32_bits(route);
207 hdr->xd_hdr.length_sn = length_sn;
209 memcpy(&hdr->uuid, &tb_xdp_uuid, sizeof(tb_xdp_uuid));
212 static int tb_xdp_handle_error(const struct tb_xdp_header *hdr)
214 const struct tb_xdp_error_response *error;
216 if (hdr->type != ERROR_RESPONSE)
219 error = (const struct tb_xdp_error_response *)hdr;
221 switch (error->error) {
222 case ERROR_UNKNOWN_PACKET:
223 case ERROR_UNKNOWN_DOMAIN:
225 case ERROR_NOT_SUPPORTED:
227 case ERROR_NOT_READY:
236 static int tb_xdp_error_response(struct tb_ctl *ctl, u64 route, u8 sequence,
237 enum tb_xdp_error error)
239 struct tb_xdp_error_response res;
241 memset(&res, 0, sizeof(res));
242 tb_xdp_fill_header(&res.hdr, route, sequence, ERROR_RESPONSE,
246 return __tb_xdomain_response(ctl, &res, sizeof(res),
247 TB_CFG_PKG_XDOMAIN_RESP);
250 static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route,
251 const uuid_t *src_uuid, const uuid_t *dst_uuid, int retry,
252 u32 **block, u32 *generation)
254 struct tb_xdp_properties_response *res;
255 struct tb_xdp_properties req;
261 total_size = sizeof(*res) + TB_XDP_PROPERTIES_MAX_DATA_LENGTH * 4;
262 res = kzalloc(total_size, GFP_KERNEL);
266 memset(&req, 0, sizeof(req));
267 tb_xdp_fill_header(&req.hdr, route, retry % 4, PROPERTIES_REQUEST,
269 memcpy(&req.src_uuid, src_uuid, sizeof(*src_uuid));
270 memcpy(&req.dst_uuid, dst_uuid, sizeof(*dst_uuid));
276 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
277 TB_CFG_PKG_XDOMAIN_REQ, res,
278 total_size, TB_CFG_PKG_XDOMAIN_RESP,
279 XDOMAIN_DEFAULT_TIMEOUT);
283 ret = tb_xdp_handle_error(&res->hdr);
288 * Package length includes the whole payload without the
289 * XDomain header. Validate first that the package is at
290 * least size of the response structure.
292 len = res->hdr.xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
293 if (len < sizeof(*res) / 4) {
298 len += sizeof(res->hdr.xd_hdr) / 4;
299 len -= sizeof(*res) / 4;
301 if (res->offset != req.offset) {
307 * First time allocate block that has enough space for
308 * the whole properties block.
311 data_len = res->data_length;
312 if (data_len > TB_XDP_PROPERTIES_MAX_LENGTH) {
317 data = kcalloc(data_len, sizeof(u32), GFP_KERNEL);
324 memcpy(data + req.offset, res->data, len * 4);
326 } while (!data_len || req.offset < data_len);
329 *generation = res->generation;
342 static int tb_xdp_properties_response(struct tb *tb, struct tb_ctl *ctl,
343 u64 route, u8 sequence, const uuid_t *src_uuid,
344 const struct tb_xdp_properties *req)
346 struct tb_xdp_properties_response *res;
352 * Currently we expect all requests to be directed to us. The
353 * protocol supports forwarding, though which we might add
356 if (!uuid_equal(src_uuid, &req->dst_uuid)) {
357 tb_xdp_error_response(ctl, route, sequence,
358 ERROR_UNKNOWN_DOMAIN);
362 mutex_lock(&xdomain_lock);
364 if (req->offset >= xdomain_property_block_len) {
365 mutex_unlock(&xdomain_lock);
369 len = xdomain_property_block_len - req->offset;
370 len = min_t(u16, len, TB_XDP_PROPERTIES_MAX_DATA_LENGTH);
371 total_size = sizeof(*res) + len * 4;
373 res = kzalloc(total_size, GFP_KERNEL);
375 mutex_unlock(&xdomain_lock);
379 tb_xdp_fill_header(&res->hdr, route, sequence, PROPERTIES_RESPONSE,
381 res->generation = xdomain_property_block_gen;
382 res->data_length = xdomain_property_block_len;
383 res->offset = req->offset;
384 uuid_copy(&res->src_uuid, src_uuid);
385 uuid_copy(&res->dst_uuid, &req->src_uuid);
386 memcpy(res->data, &xdomain_property_block[req->offset], len * 4);
388 mutex_unlock(&xdomain_lock);
390 ret = __tb_xdomain_response(ctl, res, total_size,
391 TB_CFG_PKG_XDOMAIN_RESP);
397 static int tb_xdp_properties_changed_request(struct tb_ctl *ctl, u64 route,
398 int retry, const uuid_t *uuid)
400 struct tb_xdp_properties_changed_response res;
401 struct tb_xdp_properties_changed req;
404 memset(&req, 0, sizeof(req));
405 tb_xdp_fill_header(&req.hdr, route, retry % 4,
406 PROPERTIES_CHANGED_REQUEST, sizeof(req));
407 uuid_copy(&req.src_uuid, uuid);
409 memset(&res, 0, sizeof(res));
410 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
411 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
412 TB_CFG_PKG_XDOMAIN_RESP,
413 XDOMAIN_DEFAULT_TIMEOUT);
417 return tb_xdp_handle_error(&res.hdr);
421 tb_xdp_properties_changed_response(struct tb_ctl *ctl, u64 route, u8 sequence)
423 struct tb_xdp_properties_changed_response res;
425 memset(&res, 0, sizeof(res));
426 tb_xdp_fill_header(&res.hdr, route, sequence,
427 PROPERTIES_CHANGED_RESPONSE, sizeof(res));
428 return __tb_xdomain_response(ctl, &res, sizeof(res),
429 TB_CFG_PKG_XDOMAIN_RESP);
433 * tb_register_protocol_handler() - Register protocol handler
434 * @handler: Handler to register
436 * This allows XDomain service drivers to hook into incoming XDomain
437 * messages. After this function is called the service driver needs to
438 * be able to handle calls to callback whenever a package with the
439 * registered protocol is received.
441 int tb_register_protocol_handler(struct tb_protocol_handler *handler)
443 if (!handler->uuid || !handler->callback)
445 if (uuid_equal(handler->uuid, &tb_xdp_uuid))
448 mutex_lock(&xdomain_lock);
449 list_add_tail(&handler->list, &protocol_handlers);
450 mutex_unlock(&xdomain_lock);
454 EXPORT_SYMBOL_GPL(tb_register_protocol_handler);
457 * tb_unregister_protocol_handler() - Unregister protocol handler
458 * @handler: Handler to unregister
460 * Removes the previously registered protocol handler.
462 void tb_unregister_protocol_handler(struct tb_protocol_handler *handler)
464 mutex_lock(&xdomain_lock);
465 list_del_init(&handler->list);
466 mutex_unlock(&xdomain_lock);
468 EXPORT_SYMBOL_GPL(tb_unregister_protocol_handler);
470 static void tb_xdp_handle_request(struct work_struct *work)
472 struct xdomain_request_work *xw = container_of(work, typeof(*xw), work);
473 const struct tb_xdp_header *pkg = xw->pkg;
474 const struct tb_xdomain_header *xhdr = &pkg->xd_hdr;
475 struct tb *tb = xw->tb;
476 struct tb_ctl *ctl = tb->ctl;
482 route = ((u64)xhdr->route_hi << 32 | xhdr->route_lo) & ~BIT_ULL(63);
483 sequence = xhdr->length_sn & TB_XDOMAIN_SN_MASK;
484 sequence >>= TB_XDOMAIN_SN_SHIFT;
486 mutex_lock(&tb->lock);
488 uuid = tb->root_switch->uuid;
491 mutex_unlock(&tb->lock);
494 tb_xdp_error_response(ctl, route, sequence, ERROR_NOT_READY);
499 case PROPERTIES_REQUEST:
500 ret = tb_xdp_properties_response(tb, ctl, route, sequence, uuid,
501 (const struct tb_xdp_properties *)pkg);
504 case PROPERTIES_CHANGED_REQUEST: {
505 const struct tb_xdp_properties_changed *xchg =
506 (const struct tb_xdp_properties_changed *)pkg;
507 struct tb_xdomain *xd;
509 ret = tb_xdp_properties_changed_response(ctl, route, sequence);
512 * Since the properties have been changed, let's update
513 * the xdomain related to this connection as well in
514 * case there is a change in services it offers.
516 xd = tb_xdomain_find_by_uuid_locked(tb, &xchg->src_uuid);
518 queue_delayed_work(tb->wq, &xd->get_properties_work,
519 msecs_to_jiffies(50));
531 tb_warn(tb, "failed to send XDomain response for %#x\n",
541 tb_xdp_schedule_request(struct tb *tb, const struct tb_xdp_header *hdr,
544 struct xdomain_request_work *xw;
546 xw = kmalloc(sizeof(*xw), GFP_KERNEL);
550 INIT_WORK(&xw->work, tb_xdp_handle_request);
551 xw->pkg = kmemdup(hdr, size, GFP_KERNEL);
554 queue_work(tb->wq, &xw->work);
558 * tb_register_service_driver() - Register XDomain service driver
559 * @drv: Driver to register
561 * Registers new service driver from @drv to the bus.
563 int tb_register_service_driver(struct tb_service_driver *drv)
565 drv->driver.bus = &tb_bus_type;
566 return driver_register(&drv->driver);
568 EXPORT_SYMBOL_GPL(tb_register_service_driver);
571 * tb_unregister_service_driver() - Unregister XDomain service driver
572 * @xdrv: Driver to unregister
574 * Unregisters XDomain service driver from the bus.
576 void tb_unregister_service_driver(struct tb_service_driver *drv)
578 driver_unregister(&drv->driver);
580 EXPORT_SYMBOL_GPL(tb_unregister_service_driver);
582 static ssize_t key_show(struct device *dev, struct device_attribute *attr,
585 struct tb_service *svc = container_of(dev, struct tb_service, dev);
588 * It should be null terminated but anything else is pretty much
591 return sprintf(buf, "%*pEp\n", (int)strlen(svc->key), svc->key);
593 static DEVICE_ATTR_RO(key);
595 static int get_modalias(struct tb_service *svc, char *buf, size_t size)
597 return snprintf(buf, size, "tbsvc:k%sp%08Xv%08Xr%08X", svc->key,
598 svc->prtcid, svc->prtcvers, svc->prtcrevs);
601 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
604 struct tb_service *svc = container_of(dev, struct tb_service, dev);
606 /* Full buffer size except new line and null termination */
607 get_modalias(svc, buf, PAGE_SIZE - 2);
608 return sprintf(buf, "%s\n", buf);
610 static DEVICE_ATTR_RO(modalias);
612 static ssize_t prtcid_show(struct device *dev, struct device_attribute *attr,
615 struct tb_service *svc = container_of(dev, struct tb_service, dev);
617 return sprintf(buf, "%u\n", svc->prtcid);
619 static DEVICE_ATTR_RO(prtcid);
621 static ssize_t prtcvers_show(struct device *dev, struct device_attribute *attr,
624 struct tb_service *svc = container_of(dev, struct tb_service, dev);
626 return sprintf(buf, "%u\n", svc->prtcvers);
628 static DEVICE_ATTR_RO(prtcvers);
630 static ssize_t prtcrevs_show(struct device *dev, struct device_attribute *attr,
633 struct tb_service *svc = container_of(dev, struct tb_service, dev);
635 return sprintf(buf, "%u\n", svc->prtcrevs);
637 static DEVICE_ATTR_RO(prtcrevs);
639 static ssize_t prtcstns_show(struct device *dev, struct device_attribute *attr,
642 struct tb_service *svc = container_of(dev, struct tb_service, dev);
644 return sprintf(buf, "0x%08x\n", svc->prtcstns);
646 static DEVICE_ATTR_RO(prtcstns);
648 static struct attribute *tb_service_attrs[] = {
650 &dev_attr_modalias.attr,
651 &dev_attr_prtcid.attr,
652 &dev_attr_prtcvers.attr,
653 &dev_attr_prtcrevs.attr,
654 &dev_attr_prtcstns.attr,
658 static struct attribute_group tb_service_attr_group = {
659 .attrs = tb_service_attrs,
662 static const struct attribute_group *tb_service_attr_groups[] = {
663 &tb_service_attr_group,
667 static int tb_service_uevent(struct device *dev, struct kobj_uevent_env *env)
669 struct tb_service *svc = container_of(dev, struct tb_service, dev);
672 get_modalias(svc, modalias, sizeof(modalias));
673 return add_uevent_var(env, "MODALIAS=%s", modalias);
676 static void tb_service_release(struct device *dev)
678 struct tb_service *svc = container_of(dev, struct tb_service, dev);
679 struct tb_xdomain *xd = tb_service_parent(svc);
681 ida_simple_remove(&xd->service_ids, svc->id);
686 struct device_type tb_service_type = {
687 .name = "thunderbolt_service",
688 .groups = tb_service_attr_groups,
689 .uevent = tb_service_uevent,
690 .release = tb_service_release,
692 EXPORT_SYMBOL_GPL(tb_service_type);
694 static int remove_missing_service(struct device *dev, void *data)
696 struct tb_xdomain *xd = data;
697 struct tb_service *svc;
699 svc = tb_to_service(dev);
703 if (!tb_property_find(xd->properties, svc->key,
704 TB_PROPERTY_TYPE_DIRECTORY))
705 device_unregister(dev);
710 static int find_service(struct device *dev, void *data)
712 const struct tb_property *p = data;
713 struct tb_service *svc;
715 svc = tb_to_service(dev);
719 return !strcmp(svc->key, p->key);
722 static int populate_service(struct tb_service *svc,
723 struct tb_property *property)
725 struct tb_property_dir *dir = property->value.dir;
726 struct tb_property *p;
728 /* Fill in standard properties */
729 p = tb_property_find(dir, "prtcid", TB_PROPERTY_TYPE_VALUE);
731 svc->prtcid = p->value.immediate;
732 p = tb_property_find(dir, "prtcvers", TB_PROPERTY_TYPE_VALUE);
734 svc->prtcvers = p->value.immediate;
735 p = tb_property_find(dir, "prtcrevs", TB_PROPERTY_TYPE_VALUE);
737 svc->prtcrevs = p->value.immediate;
738 p = tb_property_find(dir, "prtcstns", TB_PROPERTY_TYPE_VALUE);
740 svc->prtcstns = p->value.immediate;
742 svc->key = kstrdup(property->key, GFP_KERNEL);
749 static void enumerate_services(struct tb_xdomain *xd)
751 struct tb_service *svc;
752 struct tb_property *p;
756 * First remove all services that are not available anymore in
757 * the updated property block.
759 device_for_each_child_reverse(&xd->dev, xd, remove_missing_service);
761 /* Then re-enumerate properties creating new services as we go */
762 tb_property_for_each(xd->properties, p) {
763 if (p->type != TB_PROPERTY_TYPE_DIRECTORY)
766 /* If the service exists already we are fine */
767 dev = device_find_child(&xd->dev, p, find_service);
773 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
777 if (populate_service(svc, p)) {
782 svc->id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
783 svc->dev.bus = &tb_bus_type;
784 svc->dev.type = &tb_service_type;
785 svc->dev.parent = &xd->dev;
786 dev_set_name(&svc->dev, "%s.%d", dev_name(&xd->dev), svc->id);
788 if (device_register(&svc->dev)) {
789 put_device(&svc->dev);
795 static int populate_properties(struct tb_xdomain *xd,
796 struct tb_property_dir *dir)
798 const struct tb_property *p;
800 /* Required properties */
801 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_VALUE);
804 xd->device = p->value.immediate;
806 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_VALUE);
809 xd->vendor = p->value.immediate;
811 kfree(xd->device_name);
812 xd->device_name = NULL;
813 kfree(xd->vendor_name);
814 xd->vendor_name = NULL;
816 /* Optional properties */
817 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_TEXT);
819 xd->device_name = kstrdup(p->value.text, GFP_KERNEL);
820 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_TEXT);
822 xd->vendor_name = kstrdup(p->value.text, GFP_KERNEL);
827 /* Called with @xd->lock held */
828 static void tb_xdomain_restore_paths(struct tb_xdomain *xd)
834 if (xd->transmit_path) {
835 dev_dbg(&xd->dev, "re-establishing DMA path\n");
836 tb_domain_approve_xdomain_paths(xd->tb, xd);
840 static void tb_xdomain_get_properties(struct work_struct *work)
842 struct tb_xdomain *xd = container_of(work, typeof(*xd),
843 get_properties_work.work);
844 struct tb_property_dir *dir;
845 struct tb *tb = xd->tb;
851 ret = tb_xdp_properties_request(tb->ctl, xd->route, xd->local_uuid,
852 xd->remote_uuid, xd->properties_retries,
855 if (xd->properties_retries-- > 0) {
856 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
857 msecs_to_jiffies(1000));
861 "failed read XDomain properties from %pUb\n",
867 xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
869 mutex_lock(&xd->lock);
871 /* Only accept newer generation properties */
872 if (xd->properties && gen <= xd->property_block_gen) {
874 * On resume it is likely that the properties block is
875 * not changed (unless the other end added or removed
876 * services). However, we need to make sure the existing
877 * DMA paths are restored properly.
879 tb_xdomain_restore_paths(xd);
883 dir = tb_property_parse_dir(block, ret);
885 dev_err(&xd->dev, "failed to parse XDomain properties\n");
889 ret = populate_properties(xd, dir);
891 dev_err(&xd->dev, "missing XDomain properties in response\n");
895 /* Release the existing one */
896 if (xd->properties) {
897 tb_property_free_dir(xd->properties);
901 xd->properties = dir;
902 xd->property_block_gen = gen;
904 tb_xdomain_restore_paths(xd);
906 mutex_unlock(&xd->lock);
911 * Now the device should be ready enough so we can add it to the
912 * bus and let userspace know about it. If the device is already
913 * registered, we notify the userspace that it has changed.
916 if (device_add(&xd->dev)) {
917 dev_err(&xd->dev, "failed to add XDomain device\n");
921 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
924 enumerate_services(xd);
928 tb_property_free_dir(dir);
931 mutex_unlock(&xd->lock);
934 static void tb_xdomain_properties_changed(struct work_struct *work)
936 struct tb_xdomain *xd = container_of(work, typeof(*xd),
937 properties_changed_work.work);
940 ret = tb_xdp_properties_changed_request(xd->tb->ctl, xd->route,
941 xd->properties_changed_retries, xd->local_uuid);
943 if (xd->properties_changed_retries-- > 0)
944 queue_delayed_work(xd->tb->wq,
945 &xd->properties_changed_work,
946 msecs_to_jiffies(1000));
950 xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
953 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
956 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
958 return sprintf(buf, "%#x\n", xd->device);
960 static DEVICE_ATTR_RO(device);
963 device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
965 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
968 if (mutex_lock_interruptible(&xd->lock))
970 ret = sprintf(buf, "%s\n", xd->device_name ? xd->device_name : "");
971 mutex_unlock(&xd->lock);
975 static DEVICE_ATTR_RO(device_name);
977 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
980 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
982 return sprintf(buf, "%#x\n", xd->vendor);
984 static DEVICE_ATTR_RO(vendor);
987 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
989 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
992 if (mutex_lock_interruptible(&xd->lock))
994 ret = sprintf(buf, "%s\n", xd->vendor_name ? xd->vendor_name : "");
995 mutex_unlock(&xd->lock);
999 static DEVICE_ATTR_RO(vendor_name);
1001 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1004 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1006 return sprintf(buf, "%pUb\n", xd->remote_uuid);
1008 static DEVICE_ATTR_RO(unique_id);
1010 static struct attribute *xdomain_attrs[] = {
1011 &dev_attr_device.attr,
1012 &dev_attr_device_name.attr,
1013 &dev_attr_unique_id.attr,
1014 &dev_attr_vendor.attr,
1015 &dev_attr_vendor_name.attr,
1019 static struct attribute_group xdomain_attr_group = {
1020 .attrs = xdomain_attrs,
1023 static const struct attribute_group *xdomain_attr_groups[] = {
1024 &xdomain_attr_group,
1028 static void tb_xdomain_release(struct device *dev)
1030 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1032 put_device(xd->dev.parent);
1034 tb_property_free_dir(xd->properties);
1035 ida_destroy(&xd->service_ids);
1037 kfree(xd->local_uuid);
1038 kfree(xd->remote_uuid);
1039 kfree(xd->device_name);
1040 kfree(xd->vendor_name);
1044 static void start_handshake(struct tb_xdomain *xd)
1046 xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
1047 xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
1049 /* Start exchanging properties with the other host */
1050 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1051 msecs_to_jiffies(100));
1052 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
1053 msecs_to_jiffies(1000));
1056 static void stop_handshake(struct tb_xdomain *xd)
1058 xd->properties_retries = 0;
1059 xd->properties_changed_retries = 0;
1061 cancel_delayed_work_sync(&xd->get_properties_work);
1062 cancel_delayed_work_sync(&xd->properties_changed_work);
1065 static int __maybe_unused tb_xdomain_suspend(struct device *dev)
1067 stop_handshake(tb_to_xdomain(dev));
1071 static int __maybe_unused tb_xdomain_resume(struct device *dev)
1073 struct tb_xdomain *xd = tb_to_xdomain(dev);
1076 * Ask tb_xdomain_get_properties() restore any existing DMA
1077 * paths after properties are re-read.
1080 start_handshake(xd);
1085 static const struct dev_pm_ops tb_xdomain_pm_ops = {
1086 SET_SYSTEM_SLEEP_PM_OPS(tb_xdomain_suspend, tb_xdomain_resume)
1089 struct device_type tb_xdomain_type = {
1090 .name = "thunderbolt_xdomain",
1091 .release = tb_xdomain_release,
1092 .pm = &tb_xdomain_pm_ops,
1094 EXPORT_SYMBOL_GPL(tb_xdomain_type);
1097 * tb_xdomain_alloc() - Allocate new XDomain object
1098 * @tb: Domain where the XDomain belongs
1099 * @parent: Parent device (the switch through the connection to the
1100 * other domain is reached).
1101 * @route: Route string used to reach the other domain
1102 * @local_uuid: Our local domain UUID
1103 * @remote_uuid: UUID of the other domain
1105 * Allocates new XDomain structure and returns pointer to that. The
1106 * object must be released by calling tb_xdomain_put().
1108 struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
1109 u64 route, const uuid_t *local_uuid,
1110 const uuid_t *remote_uuid)
1112 struct tb_xdomain *xd;
1114 xd = kzalloc(sizeof(*xd), GFP_KERNEL);
1120 ida_init(&xd->service_ids);
1121 mutex_init(&xd->lock);
1122 INIT_DELAYED_WORK(&xd->get_properties_work, tb_xdomain_get_properties);
1123 INIT_DELAYED_WORK(&xd->properties_changed_work,
1124 tb_xdomain_properties_changed);
1126 xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL);
1127 if (!xd->local_uuid)
1130 xd->remote_uuid = kmemdup(remote_uuid, sizeof(uuid_t), GFP_KERNEL);
1131 if (!xd->remote_uuid)
1132 goto err_free_local_uuid;
1134 device_initialize(&xd->dev);
1135 xd->dev.parent = get_device(parent);
1136 xd->dev.bus = &tb_bus_type;
1137 xd->dev.type = &tb_xdomain_type;
1138 xd->dev.groups = xdomain_attr_groups;
1139 dev_set_name(&xd->dev, "%u-%llx", tb->index, route);
1143 err_free_local_uuid:
1144 kfree(xd->local_uuid);
1152 * tb_xdomain_add() - Add XDomain to the bus
1153 * @xd: XDomain to add
1155 * This function starts XDomain discovery protocol handshake and
1156 * eventually adds the XDomain to the bus. After calling this function
1157 * the caller needs to call tb_xdomain_remove() in order to remove and
1158 * release the object regardless whether the handshake succeeded or not.
1160 void tb_xdomain_add(struct tb_xdomain *xd)
1162 /* Start exchanging properties with the other host */
1163 start_handshake(xd);
1166 static int unregister_service(struct device *dev, void *data)
1168 device_unregister(dev);
1173 * tb_xdomain_remove() - Remove XDomain from the bus
1174 * @xd: XDomain to remove
1176 * This will stop all ongoing configuration work and remove the XDomain
1177 * along with any services from the bus. When the last reference to @xd
1178 * is released the object will be released as well.
1180 void tb_xdomain_remove(struct tb_xdomain *xd)
1184 device_for_each_child_reverse(&xd->dev, xd, unregister_service);
1186 if (!device_is_registered(&xd->dev))
1187 put_device(&xd->dev);
1189 device_unregister(&xd->dev);
1193 * tb_xdomain_enable_paths() - Enable DMA paths for XDomain connection
1194 * @xd: XDomain connection
1195 * @transmit_path: HopID of the transmit path the other end is using to
1197 * @transmit_ring: DMA ring used to receive packets from the other end
1198 * @receive_path: HopID of the receive path the other end is using to
1200 * @receive_ring: DMA ring used to send packets to the other end
1202 * The function enables DMA paths accordingly so that after successful
1203 * return the caller can send and receive packets using high-speed DMA
1206 * Return: %0 in case of success and negative errno in case of error
1208 int tb_xdomain_enable_paths(struct tb_xdomain *xd, u16 transmit_path,
1209 u16 transmit_ring, u16 receive_path,
1214 mutex_lock(&xd->lock);
1216 if (xd->transmit_path) {
1217 ret = xd->transmit_path == transmit_path ? 0 : -EBUSY;
1221 xd->transmit_path = transmit_path;
1222 xd->transmit_ring = transmit_ring;
1223 xd->receive_path = receive_path;
1224 xd->receive_ring = receive_ring;
1226 ret = tb_domain_approve_xdomain_paths(xd->tb, xd);
1229 mutex_unlock(&xd->lock);
1233 EXPORT_SYMBOL_GPL(tb_xdomain_enable_paths);
1236 * tb_xdomain_disable_paths() - Disable DMA paths for XDomain connection
1237 * @xd: XDomain connection
1239 * This does the opposite of tb_xdomain_enable_paths(). After call to
1240 * this the caller is not expected to use the rings anymore.
1242 * Return: %0 in case of success and negative errno in case of error
1244 int tb_xdomain_disable_paths(struct tb_xdomain *xd)
1248 mutex_lock(&xd->lock);
1249 if (xd->transmit_path) {
1250 xd->transmit_path = 0;
1251 xd->transmit_ring = 0;
1252 xd->receive_path = 0;
1253 xd->receive_ring = 0;
1255 ret = tb_domain_disconnect_xdomain_paths(xd->tb, xd);
1257 mutex_unlock(&xd->lock);
1261 EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths);
1263 struct tb_xdomain_lookup {
1269 static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw,
1270 const struct tb_xdomain_lookup *lookup)
1274 for (i = 1; i <= sw->config.max_port_number; i++) {
1275 struct tb_port *port = &sw->ports[i];
1276 struct tb_xdomain *xd;
1278 if (tb_is_upstream_port(port))
1281 if (port->xdomain) {
1285 if (uuid_equal(xd->remote_uuid, lookup->uuid))
1287 } else if (lookup->link == xd->link &&
1288 lookup->depth == xd->depth) {
1291 } else if (port->remote) {
1292 xd = switch_find_xdomain(port->remote->sw, lookup);
1302 * tb_xdomain_find_by_uuid() - Find an XDomain by UUID
1303 * @tb: Domain where the XDomain belongs to
1304 * @uuid: UUID to look for
1306 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1307 * The returned XDomain will have its reference count increased so the
1308 * caller needs to call tb_xdomain_put() when it is done with the
1311 * This will find all XDomains including the ones that are not yet added
1312 * to the bus (handshake is still in progress).
1314 * The caller needs to hold @tb->lock.
1316 struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid)
1318 struct tb_xdomain_lookup lookup;
1319 struct tb_xdomain *xd;
1321 memset(&lookup, 0, sizeof(lookup));
1324 xd = switch_find_xdomain(tb->root_switch, &lookup);
1326 get_device(&xd->dev);
1332 EXPORT_SYMBOL_GPL(tb_xdomain_find_by_uuid);
1335 * tb_xdomain_find_by_link_depth() - Find an XDomain by link and depth
1336 * @tb: Domain where the XDomain belongs to
1337 * @link: Root switch link number
1338 * @depth: Depth in the link
1340 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1341 * The returned XDomain will have its reference count increased so the
1342 * caller needs to call tb_xdomain_put() when it is done with the
1345 * This will find all XDomains including the ones that are not yet added
1346 * to the bus (handshake is still in progress).
1348 * The caller needs to hold @tb->lock.
1350 struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
1353 struct tb_xdomain_lookup lookup;
1354 struct tb_xdomain *xd;
1356 memset(&lookup, 0, sizeof(lookup));
1358 lookup.depth = depth;
1360 xd = switch_find_xdomain(tb->root_switch, &lookup);
1362 get_device(&xd->dev);
1369 bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
1370 const void *buf, size_t size)
1372 const struct tb_protocol_handler *handler, *tmp;
1373 const struct tb_xdp_header *hdr = buf;
1374 unsigned int length;
1377 /* We expect the packet is at least size of the header */
1378 length = hdr->xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
1379 if (length != size / 4 - sizeof(hdr->xd_hdr) / 4)
1381 if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4)
1385 * Handle XDomain discovery protocol packets directly here. For
1386 * other protocols (based on their UUID) we call registered
1389 if (uuid_equal(&hdr->uuid, &tb_xdp_uuid)) {
1390 if (type == TB_CFG_PKG_XDOMAIN_REQ) {
1391 tb_xdp_schedule_request(tb, hdr, size);
1397 mutex_lock(&xdomain_lock);
1398 list_for_each_entry_safe(handler, tmp, &protocol_handlers, list) {
1399 if (!uuid_equal(&hdr->uuid, handler->uuid))
1402 mutex_unlock(&xdomain_lock);
1403 ret = handler->callback(buf, size, handler->data);
1404 mutex_lock(&xdomain_lock);
1409 mutex_unlock(&xdomain_lock);
1414 static int rebuild_property_block(void)
1419 ret = tb_property_format_dir(xdomain_property_dir, NULL, 0);
1425 block = kcalloc(len, sizeof(u32), GFP_KERNEL);
1429 ret = tb_property_format_dir(xdomain_property_dir, block, len);
1435 kfree(xdomain_property_block);
1436 xdomain_property_block = block;
1437 xdomain_property_block_len = len;
1438 xdomain_property_block_gen++;
1443 static int update_xdomain(struct device *dev, void *data)
1445 struct tb_xdomain *xd;
1447 xd = tb_to_xdomain(dev);
1449 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1450 msecs_to_jiffies(50));
1456 static void update_all_xdomains(void)
1458 bus_for_each_dev(&tb_bus_type, NULL, NULL, update_xdomain);
1461 static bool remove_directory(const char *key, const struct tb_property_dir *dir)
1463 struct tb_property *p;
1465 p = tb_property_find(xdomain_property_dir, key,
1466 TB_PROPERTY_TYPE_DIRECTORY);
1467 if (p && p->value.dir == dir) {
1468 tb_property_remove(p);
1475 * tb_register_property_dir() - Register property directory to the host
1476 * @key: Key (name) of the directory to add
1477 * @dir: Directory to add
1479 * Service drivers can use this function to add new property directory
1480 * to the host available properties. The other connected hosts are
1481 * notified so they can re-read properties of this host if they are
1484 * Return: %0 on success and negative errno on failure
1486 int tb_register_property_dir(const char *key, struct tb_property_dir *dir)
1490 if (WARN_ON(!xdomain_property_dir))
1493 if (!key || strlen(key) > 8)
1496 mutex_lock(&xdomain_lock);
1497 if (tb_property_find(xdomain_property_dir, key,
1498 TB_PROPERTY_TYPE_DIRECTORY)) {
1503 ret = tb_property_add_dir(xdomain_property_dir, key, dir);
1507 ret = rebuild_property_block();
1509 remove_directory(key, dir);
1513 mutex_unlock(&xdomain_lock);
1514 update_all_xdomains();
1518 mutex_unlock(&xdomain_lock);
1521 EXPORT_SYMBOL_GPL(tb_register_property_dir);
1524 * tb_unregister_property_dir() - Removes property directory from host
1525 * @key: Key (name) of the directory
1526 * @dir: Directory to remove
1528 * This will remove the existing directory from this host and notify the
1529 * connected hosts about the change.
1531 void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir)
1535 mutex_lock(&xdomain_lock);
1536 if (remove_directory(key, dir))
1537 ret = rebuild_property_block();
1538 mutex_unlock(&xdomain_lock);
1541 update_all_xdomains();
1543 EXPORT_SYMBOL_GPL(tb_unregister_property_dir);
1545 int tb_xdomain_init(void)
1549 xdomain_property_dir = tb_property_create_dir(NULL);
1550 if (!xdomain_property_dir)
1554 * Initialize standard set of properties without any service
1555 * directories. Those will be added by service drivers
1556 * themselves when they are loaded.
1558 tb_property_add_immediate(xdomain_property_dir, "vendorid",
1559 PCI_VENDOR_ID_INTEL);
1560 tb_property_add_text(xdomain_property_dir, "vendorid", "Intel Corp.");
1561 tb_property_add_immediate(xdomain_property_dir, "deviceid", 0x1);
1562 tb_property_add_text(xdomain_property_dir, "deviceid",
1563 utsname()->nodename);
1564 tb_property_add_immediate(xdomain_property_dir, "devicerv", 0x80000100);
1566 ret = rebuild_property_block();
1568 tb_property_free_dir(xdomain_property_dir);
1569 xdomain_property_dir = NULL;
1575 void tb_xdomain_exit(void)
1577 kfree(xdomain_property_block);
1578 tb_property_free_dir(xdomain_property_dir);