1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Thunderbolt XDomain discovery protocol support 4 * 5 * Copyright (C) 2017, Intel Corporation 6 * Authors: Michael Jamet <michael.jamet@intel.com> 7 * Mika Westerberg <mika.westerberg@linux.intel.com> 8 */ 9 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/string_helpers.h> 17 #include <linux/utsname.h> 18 #include <linux/uuid.h> 19 #include <linux/workqueue.h> 20 21 #include "tb.h" 22 23 #define XDOMAIN_SHORT_TIMEOUT 100 /* ms */ 24 #define XDOMAIN_DEFAULT_TIMEOUT 1000 /* ms */ 25 #define XDOMAIN_BONDING_TIMEOUT 10000 /* ms */ 26 #define XDOMAIN_RETRIES 10 27 #define XDOMAIN_DEFAULT_MAX_HOPID 15 28 29 enum { 30 XDOMAIN_STATE_INIT, 31 XDOMAIN_STATE_UUID, 32 XDOMAIN_STATE_LINK_STATUS, 33 XDOMAIN_STATE_LINK_STATE_CHANGE, 34 XDOMAIN_STATE_LINK_STATUS2, 35 XDOMAIN_STATE_BONDING_UUID_LOW, 36 XDOMAIN_STATE_BONDING_UUID_HIGH, 37 XDOMAIN_STATE_PROPERTIES, 38 XDOMAIN_STATE_ENUMERATED, 39 XDOMAIN_STATE_ERROR, 40 }; 41 42 static const char * const state_names[] = { 43 [XDOMAIN_STATE_INIT] = "INIT", 44 [XDOMAIN_STATE_UUID] = "UUID", 45 [XDOMAIN_STATE_LINK_STATUS] = "LINK_STATUS", 46 [XDOMAIN_STATE_LINK_STATE_CHANGE] = "LINK_STATE_CHANGE", 47 [XDOMAIN_STATE_LINK_STATUS2] = "LINK_STATUS2", 48 [XDOMAIN_STATE_BONDING_UUID_LOW] = "BONDING_UUID_LOW", 49 [XDOMAIN_STATE_BONDING_UUID_HIGH] = "BONDING_UUID_HIGH", 50 [XDOMAIN_STATE_PROPERTIES] = "PROPERTIES", 51 [XDOMAIN_STATE_ENUMERATED] = "ENUMERATED", 52 [XDOMAIN_STATE_ERROR] = "ERROR", 53 }; 54 55 struct xdomain_request_work { 56 struct work_struct work; 57 struct tb_xdp_header *pkg; 58 size_t pkg_len; 59 struct tb *tb; 60 }; 61 62 static bool tb_xdomain_enabled = true; 63 module_param_named(xdomain, tb_xdomain_enabled, bool, 0444); 64 MODULE_PARM_DESC(xdomain, "allow XDomain protocol (default: true)"); 65 66 /* 67 * Serializes access to the properties and protocol handlers below. If 68 * you need to take both this lock and the struct tb_xdomain lock, take 69 * this one first. 70 */ 71 static DEFINE_MUTEX(xdomain_lock); 72 73 /* Properties exposed to the remote domains */ 74 static struct tb_property_dir *xdomain_property_dir; 75 static u32 xdomain_property_block_gen; 76 77 /* Additional protocol handlers */ 78 static LIST_HEAD(protocol_handlers); 79 80 /* UUID for XDomain discovery protocol: b638d70e-42ff-40bb-97c2-90e2c0b2ff07 */ 81 static const uuid_t tb_xdp_uuid = 82 UUID_INIT(0xb638d70e, 0x42ff, 0x40bb, 83 0x97, 0xc2, 0x90, 0xe2, 0xc0, 0xb2, 0xff, 0x07); 84 85 bool tb_is_xdomain_enabled(void) 86 { 87 return tb_xdomain_enabled && tb_acpi_is_xdomain_allowed(); 88 } 89 90 static bool tb_xdomain_match(const struct tb_cfg_request *req, 91 const struct ctl_pkg *pkg) 92 { 93 switch (pkg->frame.eof) { 94 case TB_CFG_PKG_ERROR: 95 return true; 96 97 case TB_CFG_PKG_XDOMAIN_RESP: { 98 const struct tb_xdp_header *res_hdr = pkg->buffer; 99 const struct tb_xdp_header *req_hdr = req->request; 100 101 if (pkg->frame.size < req->response_size / 4) 102 return false; 103 104 /* Make sure route matches */ 105 if ((res_hdr->xd_hdr.route_hi & ~BIT(31)) != 106 req_hdr->xd_hdr.route_hi) 107 return false; 108 if ((res_hdr->xd_hdr.route_lo) != req_hdr->xd_hdr.route_lo) 109 return false; 110 111 /* Check that the XDomain protocol matches */ 112 if (!uuid_equal(&res_hdr->uuid, &req_hdr->uuid)) 113 return false; 114 115 return true; 116 } 117 118 default: 119 return false; 120 } 121 } 122 123 static bool tb_xdomain_copy(struct tb_cfg_request *req, 124 const struct ctl_pkg *pkg) 125 { 126 size_t len = min_t(size_t, pkg->frame.size, req->response_size); 127 128 memcpy(req->response, pkg->buffer, len); 129 req->result.err = 0; 130 return true; 131 } 132 133 static void response_ready(void *data) 134 { 135 tb_cfg_request_put(data); 136 } 137 138 static int __tb_xdomain_response(struct tb_ctl *ctl, const void *response, 139 size_t size, enum tb_cfg_pkg_type type) 140 { 141 struct tb_cfg_request *req; 142 int ret; 143 144 req = tb_cfg_request_alloc(); 145 if (!req) 146 return -ENOMEM; 147 148 req->match = tb_xdomain_match; 149 req->copy = tb_xdomain_copy; 150 req->request = response; 151 req->request_size = size; 152 req->request_type = type; 153 154 ret = tb_cfg_request(ctl, req, response_ready, req); 155 if (ret) 156 tb_cfg_request_put(req); 157 158 return ret; 159 } 160 161 /** 162 * tb_xdomain_response() - Send a XDomain response message 163 * @xd: XDomain to send the message 164 * @response: Response to send 165 * @size: Size of the response 166 * @type: PDF type of the response 167 * 168 * This can be used to send a XDomain response message to the other 169 * domain. No response for the message is expected. 170 * 171 * Return: %0 on success, negative errno otherwise. 172 */ 173 int tb_xdomain_response(struct tb_xdomain *xd, const void *response, 174 size_t size, enum tb_cfg_pkg_type type) 175 { 176 return __tb_xdomain_response(xd->tb->ctl, response, size, type); 177 } 178 EXPORT_SYMBOL_GPL(tb_xdomain_response); 179 180 static int __tb_xdomain_request(struct tb_ctl *ctl, const void *request, 181 size_t request_size, enum tb_cfg_pkg_type request_type, void *response, 182 size_t response_size, enum tb_cfg_pkg_type response_type, 183 unsigned int timeout_msec) 184 { 185 struct tb_cfg_request *req; 186 struct tb_cfg_result res; 187 188 req = tb_cfg_request_alloc(); 189 if (!req) 190 return -ENOMEM; 191 192 req->match = tb_xdomain_match; 193 req->copy = tb_xdomain_copy; 194 req->request = request; 195 req->request_size = request_size; 196 req->request_type = request_type; 197 req->response = response; 198 req->response_size = response_size; 199 req->response_type = response_type; 200 201 res = tb_cfg_request_sync(ctl, req, timeout_msec); 202 203 tb_cfg_request_put(req); 204 205 return res.err == 1 ? -EIO : res.err; 206 } 207 208 /** 209 * tb_xdomain_request() - Send a XDomain request 210 * @xd: XDomain to send the request 211 * @request: Request to send 212 * @request_size: Size of the request in bytes 213 * @request_type: PDF type of the request 214 * @response: Response is copied here 215 * @response_size: Expected size of the response in bytes 216 * @response_type: Expected PDF type of the response 217 * @timeout_msec: Timeout in milliseconds to wait for the response 218 * 219 * This function can be used to send XDomain control channel messages to 220 * the other domain. The function waits until the response is received 221 * or when timeout triggers. Whichever comes first. 222 * 223 * Return: %0 on success, negative errno otherwise. 224 */ 225 int tb_xdomain_request(struct tb_xdomain *xd, const void *request, 226 size_t request_size, enum tb_cfg_pkg_type request_type, 227 void *response, size_t response_size, 228 enum tb_cfg_pkg_type response_type, unsigned int timeout_msec) 229 { 230 return __tb_xdomain_request(xd->tb->ctl, request, request_size, 231 request_type, response, response_size, 232 response_type, timeout_msec); 233 } 234 EXPORT_SYMBOL_GPL(tb_xdomain_request); 235 236 static inline void tb_xdp_fill_header(struct tb_xdp_header *hdr, u64 route, 237 u8 sequence, enum tb_xdp_type type, size_t size) 238 { 239 u32 length_sn; 240 241 length_sn = (size - sizeof(hdr->xd_hdr)) / 4; 242 length_sn |= (sequence << TB_XDOMAIN_SN_SHIFT) & TB_XDOMAIN_SN_MASK; 243 244 hdr->xd_hdr.route_hi = upper_32_bits(route); 245 hdr->xd_hdr.route_lo = lower_32_bits(route); 246 hdr->xd_hdr.length_sn = length_sn; 247 hdr->type = type; 248 memcpy(&hdr->uuid, &tb_xdp_uuid, sizeof(tb_xdp_uuid)); 249 } 250 251 static int tb_xdp_handle_error(const struct tb_xdp_error_response *res) 252 { 253 if (res->hdr.type != ERROR_RESPONSE) 254 return 0; 255 256 switch (res->error) { 257 case ERROR_UNKNOWN_PACKET: 258 case ERROR_UNKNOWN_DOMAIN: 259 return -EIO; 260 case ERROR_NOT_SUPPORTED: 261 return -EOPNOTSUPP; 262 case ERROR_NOT_READY: 263 return -EAGAIN; 264 default: 265 break; 266 } 267 268 return 0; 269 } 270 271 static int tb_xdp_uuid_request(struct tb_ctl *ctl, u64 route, int retry, 272 uuid_t *uuid, u64 *remote_route) 273 { 274 struct tb_xdp_uuid_response res; 275 struct tb_xdp_uuid req; 276 int ret; 277 278 memset(&req, 0, sizeof(req)); 279 tb_xdp_fill_header(&req.hdr, route, retry % 4, UUID_REQUEST, 280 sizeof(req)); 281 282 memset(&res, 0, sizeof(res)); 283 ret = __tb_xdomain_request(ctl, &req, sizeof(req), 284 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res), 285 TB_CFG_PKG_XDOMAIN_RESP, 286 XDOMAIN_DEFAULT_TIMEOUT); 287 if (ret) 288 return ret; 289 290 ret = tb_xdp_handle_error(&res.err); 291 if (ret) 292 return ret; 293 294 uuid_copy(uuid, &res.src_uuid); 295 *remote_route = (u64)res.src_route_hi << 32 | res.src_route_lo; 296 297 return 0; 298 } 299 300 static int tb_xdp_uuid_response(struct tb_ctl *ctl, u64 route, u8 sequence, 301 const uuid_t *uuid) 302 { 303 struct tb_xdp_uuid_response res; 304 305 memset(&res, 0, sizeof(res)); 306 tb_xdp_fill_header(&res.hdr, route, sequence, UUID_RESPONSE, 307 sizeof(res)); 308 309 uuid_copy(&res.src_uuid, uuid); 310 res.src_route_hi = upper_32_bits(route); 311 res.src_route_lo = lower_32_bits(route); 312 313 return __tb_xdomain_response(ctl, &res, sizeof(res), 314 TB_CFG_PKG_XDOMAIN_RESP); 315 } 316 317 static int tb_xdp_error_response(struct tb_ctl *ctl, u64 route, u8 sequence, 318 enum tb_xdp_error error) 319 { 320 struct tb_xdp_error_response res; 321 322 memset(&res, 0, sizeof(res)); 323 tb_xdp_fill_header(&res.hdr, route, sequence, ERROR_RESPONSE, 324 sizeof(res)); 325 res.error = error; 326 327 return __tb_xdomain_response(ctl, &res, sizeof(res), 328 TB_CFG_PKG_XDOMAIN_RESP); 329 } 330 331 static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route, 332 const uuid_t *src_uuid, const uuid_t *dst_uuid, int retry, 333 u32 **block, u32 *generation) 334 { 335 struct tb_xdp_properties_response *res; 336 struct tb_xdp_properties req; 337 u16 data_len, len; 338 size_t total_size; 339 u32 *data = NULL; 340 int ret; 341 342 total_size = sizeof(*res) + TB_XDP_PROPERTIES_MAX_DATA_LENGTH * 4; 343 res = kzalloc(total_size, GFP_KERNEL); 344 if (!res) 345 return -ENOMEM; 346 347 memset(&req, 0, sizeof(req)); 348 tb_xdp_fill_header(&req.hdr, route, retry % 4, PROPERTIES_REQUEST, 349 sizeof(req)); 350 memcpy(&req.src_uuid, src_uuid, sizeof(*src_uuid)); 351 memcpy(&req.dst_uuid, dst_uuid, sizeof(*dst_uuid)); 352 353 data_len = 0; 354 355 do { 356 ret = __tb_xdomain_request(ctl, &req, sizeof(req), 357 TB_CFG_PKG_XDOMAIN_REQ, res, 358 total_size, TB_CFG_PKG_XDOMAIN_RESP, 359 XDOMAIN_DEFAULT_TIMEOUT); 360 if (ret) 361 goto err; 362 363 ret = tb_xdp_handle_error(&res->err); 364 if (ret) 365 goto err; 366 367 /* 368 * Package length includes the whole payload without the 369 * XDomain header. Validate first that the package is at 370 * least size of the response structure. 371 */ 372 len = res->hdr.xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK; 373 if (len < sizeof(*res) / 4) { 374 ret = -EINVAL; 375 goto err; 376 } 377 378 len += sizeof(res->hdr.xd_hdr) / 4; 379 len -= sizeof(*res) / 4; 380 381 if (res->offset != req.offset) { 382 ret = -EINVAL; 383 goto err; 384 } 385 386 /* 387 * First time allocate block that has enough space for 388 * the whole properties block. 389 */ 390 if (!data) { 391 data_len = res->data_length; 392 if (data_len > TB_XDP_PROPERTIES_MAX_LENGTH) { 393 ret = -E2BIG; 394 goto err; 395 } 396 397 data = kcalloc(data_len, sizeof(u32), GFP_KERNEL); 398 if (!data) { 399 ret = -ENOMEM; 400 goto err; 401 } 402 } 403 404 if (req.offset + len > data_len) 405 len = data_len - req.offset; 406 memcpy(data + req.offset, res->data, len * 4); 407 req.offset += len; 408 } while (!data_len || req.offset < data_len); 409 410 *block = data; 411 *generation = res->generation; 412 413 kfree(res); 414 415 return data_len; 416 417 err: 418 kfree(data); 419 kfree(res); 420 421 return ret; 422 } 423 424 static int tb_xdp_properties_response(struct tb *tb, struct tb_ctl *ctl, 425 struct tb_xdomain *xd, u8 sequence, const struct tb_xdp_properties *req) 426 { 427 struct tb_xdp_properties_response *res; 428 size_t total_size; 429 u16 len; 430 int ret; 431 432 /* 433 * Currently we expect all requests to be directed to us. The 434 * protocol supports forwarding, though which we might add 435 * support later on. 436 */ 437 if (!uuid_equal(xd->local_uuid, &req->dst_uuid)) { 438 tb_xdp_error_response(ctl, xd->route, sequence, 439 ERROR_UNKNOWN_DOMAIN); 440 return 0; 441 } 442 443 mutex_lock(&xd->lock); 444 445 if (req->offset >= xd->local_property_block_len) { 446 mutex_unlock(&xd->lock); 447 return -EINVAL; 448 } 449 450 len = xd->local_property_block_len - req->offset; 451 len = min_t(u16, len, TB_XDP_PROPERTIES_MAX_DATA_LENGTH); 452 total_size = sizeof(*res) + len * 4; 453 454 res = kzalloc(total_size, GFP_KERNEL); 455 if (!res) { 456 mutex_unlock(&xd->lock); 457 return -ENOMEM; 458 } 459 460 tb_xdp_fill_header(&res->hdr, xd->route, sequence, PROPERTIES_RESPONSE, 461 total_size); 462 res->generation = xd->local_property_block_gen; 463 res->data_length = xd->local_property_block_len; 464 res->offset = req->offset; 465 uuid_copy(&res->src_uuid, xd->local_uuid); 466 uuid_copy(&res->dst_uuid, &req->src_uuid); 467 memcpy(res->data, &xd->local_property_block[req->offset], len * 4); 468 469 mutex_unlock(&xd->lock); 470 471 ret = __tb_xdomain_response(ctl, res, total_size, 472 TB_CFG_PKG_XDOMAIN_RESP); 473 474 kfree(res); 475 return ret; 476 } 477 478 static int tb_xdp_properties_changed_request(struct tb_ctl *ctl, u64 route, 479 int retry, const uuid_t *uuid) 480 { 481 struct tb_xdp_properties_changed_response res; 482 struct tb_xdp_properties_changed req; 483 int ret; 484 485 memset(&req, 0, sizeof(req)); 486 tb_xdp_fill_header(&req.hdr, route, retry % 4, 487 PROPERTIES_CHANGED_REQUEST, sizeof(req)); 488 uuid_copy(&req.src_uuid, uuid); 489 490 memset(&res, 0, sizeof(res)); 491 ret = __tb_xdomain_request(ctl, &req, sizeof(req), 492 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res), 493 TB_CFG_PKG_XDOMAIN_RESP, 494 XDOMAIN_DEFAULT_TIMEOUT); 495 if (ret) 496 return ret; 497 498 return tb_xdp_handle_error(&res.err); 499 } 500 501 static int 502 tb_xdp_properties_changed_response(struct tb_ctl *ctl, u64 route, u8 sequence) 503 { 504 struct tb_xdp_properties_changed_response res; 505 506 memset(&res, 0, sizeof(res)); 507 tb_xdp_fill_header(&res.hdr, route, sequence, 508 PROPERTIES_CHANGED_RESPONSE, sizeof(res)); 509 return __tb_xdomain_response(ctl, &res, sizeof(res), 510 TB_CFG_PKG_XDOMAIN_RESP); 511 } 512 513 static int tb_xdp_link_state_status_request(struct tb_ctl *ctl, u64 route, 514 u8 sequence, u8 *slw, u8 *tlw, 515 u8 *sls, u8 *tls) 516 { 517 struct tb_xdp_link_state_status_response res; 518 struct tb_xdp_link_state_status req; 519 int ret; 520 521 memset(&req, 0, sizeof(req)); 522 tb_xdp_fill_header(&req.hdr, route, sequence, LINK_STATE_STATUS_REQUEST, 523 sizeof(req)); 524 525 memset(&res, 0, sizeof(res)); 526 ret = __tb_xdomain_request(ctl, &req, sizeof(req), TB_CFG_PKG_XDOMAIN_REQ, 527 &res, sizeof(res), TB_CFG_PKG_XDOMAIN_RESP, 528 XDOMAIN_DEFAULT_TIMEOUT); 529 if (ret) 530 return ret; 531 532 ret = tb_xdp_handle_error(&res.err); 533 if (ret) 534 return ret; 535 536 if (res.status != 0) 537 return -EREMOTEIO; 538 539 *slw = res.slw; 540 *tlw = res.tlw; 541 *sls = res.sls; 542 *tls = res.tls; 543 544 return 0; 545 } 546 547 static int tb_xdp_link_state_status_response(struct tb *tb, struct tb_ctl *ctl, 548 struct tb_xdomain *xd, u8 sequence, 549 u8 slw, u8 sls, u8 tls, u8 tlw) 550 { 551 struct tb_xdp_link_state_status_response res; 552 553 memset(&res, 0, sizeof(res)); 554 tb_xdp_fill_header(&res.hdr, xd->route, sequence, 555 LINK_STATE_STATUS_RESPONSE, sizeof(res)); 556 557 res.slw = slw; 558 res.sls = sls; 559 res.tls = tls; 560 res.tlw = tlw; 561 562 return __tb_xdomain_response(ctl, &res, sizeof(res), 563 TB_CFG_PKG_XDOMAIN_RESP); 564 } 565 566 static int tb_xdp_link_state_change_request(struct tb_ctl *ctl, u64 route, 567 u8 sequence, u8 tlw, u8 tls) 568 { 569 struct tb_xdp_link_state_change_response res; 570 struct tb_xdp_link_state_change req; 571 int ret; 572 573 memset(&req, 0, sizeof(req)); 574 tb_xdp_fill_header(&req.hdr, route, sequence, LINK_STATE_CHANGE_REQUEST, 575 sizeof(req)); 576 req.tlw = tlw; 577 req.tls = tls; 578 579 memset(&res, 0, sizeof(res)); 580 ret = __tb_xdomain_request(ctl, &req, sizeof(req), TB_CFG_PKG_XDOMAIN_REQ, 581 &res, sizeof(res), TB_CFG_PKG_XDOMAIN_RESP, 582 XDOMAIN_DEFAULT_TIMEOUT); 583 if (ret) 584 return ret; 585 586 ret = tb_xdp_handle_error(&res.err); 587 if (ret) 588 return ret; 589 590 return res.status != 0 ? -EREMOTEIO : 0; 591 } 592 593 static int tb_xdp_link_state_change_response(struct tb_ctl *ctl, u64 route, 594 u8 sequence, u32 status) 595 { 596 struct tb_xdp_link_state_change_response res; 597 598 memset(&res, 0, sizeof(res)); 599 tb_xdp_fill_header(&res.hdr, route, sequence, LINK_STATE_CHANGE_RESPONSE, 600 sizeof(res)); 601 602 res.status = status; 603 604 return __tb_xdomain_response(ctl, &res, sizeof(res), 605 TB_CFG_PKG_XDOMAIN_RESP); 606 } 607 608 /** 609 * tb_register_protocol_handler() - Register protocol handler 610 * @handler: Handler to register 611 * 612 * This allows XDomain service drivers to hook into incoming XDomain 613 * messages. After this function is called the service driver needs to 614 * be able to handle calls to callback whenever a package with the 615 * registered protocol is received. 616 * 617 * Return: %0 on success, negative errno otherwise. 618 */ 619 int tb_register_protocol_handler(struct tb_protocol_handler *handler) 620 { 621 if (!handler->uuid || !handler->callback) 622 return -EINVAL; 623 if (uuid_equal(handler->uuid, &tb_xdp_uuid)) 624 return -EINVAL; 625 626 mutex_lock(&xdomain_lock); 627 list_add_tail(&handler->list, &protocol_handlers); 628 mutex_unlock(&xdomain_lock); 629 630 return 0; 631 } 632 EXPORT_SYMBOL_GPL(tb_register_protocol_handler); 633 634 /** 635 * tb_unregister_protocol_handler() - Unregister protocol handler 636 * @handler: Handler to unregister 637 * 638 * Removes the previously registered protocol handler. 639 */ 640 void tb_unregister_protocol_handler(struct tb_protocol_handler *handler) 641 { 642 mutex_lock(&xdomain_lock); 643 list_del_init(&handler->list); 644 mutex_unlock(&xdomain_lock); 645 } 646 EXPORT_SYMBOL_GPL(tb_unregister_protocol_handler); 647 648 static int update_service_properties(struct device *dev, void *data) 649 { 650 struct tb_property_dir *root = data; 651 struct tb_service *svc; 652 struct tb_property *p; 653 654 svc = tb_to_service(dev); 655 if (!svc) 656 return 0; 657 658 guard(mutex)(&svc->lock); 659 660 /* 661 * Replace the static service properties with the dynamic one. 662 * Typically this is the same but service drivers can add their 663 * own dynamic properties here too. 664 */ 665 p = tb_property_find(root, svc->key, TB_PROPERTY_TYPE_DIRECTORY); 666 if (!p) 667 return 0; 668 if (svc->local_properties) 669 return tb_property_merge_dir(p->value.dir, 670 svc->local_properties, false); 671 return 0; 672 } 673 674 static void update_property_block(struct tb_xdomain *xd) 675 { 676 mutex_lock(&xdomain_lock); 677 mutex_lock(&xd->lock); 678 /* 679 * If the local property block is not up-to-date, rebuild it now 680 * based on the global property template. 681 */ 682 if (!xd->local_property_block || 683 xd->local_property_block_gen < xdomain_property_block_gen) { 684 struct tb_property_dir *dir; 685 int ret, block_len; 686 u32 *block; 687 688 dir = tb_property_copy_dir(xdomain_property_dir); 689 if (!dir) { 690 dev_warn(&xd->dev, "failed to copy properties\n"); 691 goto out_unlock; 692 } 693 694 /* Fill in non-static properties now */ 695 tb_property_add_text(dir, "deviceid", utsname()->nodename); 696 tb_property_add_immediate(dir, "maxhopid", xd->local_max_hopid); 697 698 /* Add service specific dynamic properties */ 699 device_for_each_child(&xd->dev, dir, update_service_properties); 700 701 ret = tb_property_format_dir(dir, NULL, 0); 702 if (ret < 0) { 703 dev_warn(&xd->dev, "local property block creation failed\n"); 704 tb_property_free_dir(dir); 705 goto out_unlock; 706 } 707 708 block_len = ret; 709 block = kcalloc(block_len, sizeof(*block), GFP_KERNEL); 710 if (!block) { 711 tb_property_free_dir(dir); 712 goto out_unlock; 713 } 714 715 ret = tb_property_format_dir(dir, block, block_len); 716 if (ret) { 717 dev_warn(&xd->dev, "property block generation failed\n"); 718 tb_property_free_dir(dir); 719 kfree(block); 720 goto out_unlock; 721 } 722 723 tb_property_free_dir(dir); 724 /* Release the previous block */ 725 kfree(xd->local_property_block); 726 /* Assign new one */ 727 xd->local_property_block = block; 728 xd->local_property_block_len = block_len; 729 xd->local_property_block_gen = xdomain_property_block_gen; 730 } 731 732 out_unlock: 733 mutex_unlock(&xd->lock); 734 mutex_unlock(&xdomain_lock); 735 } 736 737 static void start_handshake(struct tb_xdomain *xd) 738 { 739 xd->state = XDOMAIN_STATE_INIT; 740 queue_delayed_work(xd->tb->wq, &xd->state_work, 741 msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT)); 742 } 743 744 /* Can be called from state_work */ 745 static void __stop_handshake(struct tb_xdomain *xd) 746 { 747 cancel_delayed_work_sync(&xd->properties_changed_work); 748 xd->properties_changed_retries = 0; 749 xd->state_retries = 0; 750 } 751 752 static void stop_handshake(struct tb_xdomain *xd) 753 { 754 cancel_delayed_work_sync(&xd->state_work); 755 __stop_handshake(xd); 756 } 757 758 static void tb_xdp_handle_request(struct work_struct *work) 759 { 760 struct xdomain_request_work *xw = container_of(work, typeof(*xw), work); 761 const struct tb_xdp_header *pkg = xw->pkg; 762 const struct tb_xdomain_header *xhdr = &pkg->xd_hdr; 763 size_t pkg_len = xw->pkg_len; 764 struct tb *tb = xw->tb; 765 struct tb_ctl *ctl = tb->ctl; 766 struct tb_xdomain *xd; 767 const uuid_t *uuid; 768 int ret = 0; 769 u32 sequence; 770 u64 route; 771 772 route = ((u64)xhdr->route_hi << 32 | xhdr->route_lo) & ~BIT_ULL(63); 773 sequence = xhdr->length_sn & TB_XDOMAIN_SN_MASK; 774 sequence >>= TB_XDOMAIN_SN_SHIFT; 775 776 mutex_lock(&tb->lock); 777 if (tb->root_switch) 778 uuid = kmemdup(tb->root_switch->uuid, sizeof(*uuid), GFP_KERNEL); 779 else 780 uuid = NULL; 781 mutex_unlock(&tb->lock); 782 783 if (!uuid) { 784 tb_xdp_error_response(ctl, route, sequence, ERROR_NOT_READY); 785 goto out; 786 } 787 788 xd = tb_xdomain_find_by_route_locked(tb, route); 789 if (xd) 790 update_property_block(xd); 791 792 switch (pkg->type) { 793 case PROPERTIES_REQUEST: 794 tb_dbg(tb, "%llx: received XDomain properties request\n", route); 795 if (xd && pkg_len >= sizeof(struct tb_xdp_properties)) { 796 ret = tb_xdp_properties_response(tb, ctl, xd, sequence, 797 (const struct tb_xdp_properties *)pkg); 798 } 799 break; 800 801 case PROPERTIES_CHANGED_REQUEST: 802 tb_dbg(tb, "%llx: received XDomain properties changed request\n", 803 route); 804 805 ret = tb_xdp_properties_changed_response(ctl, route, sequence); 806 807 /* 808 * Since the properties have been changed, let's update 809 * the xdomain related to this connection as well in 810 * case there is a change in services it offers. 811 */ 812 if (xd) { 813 mutex_lock(&xd->lock); 814 if (!xd->removing && device_is_registered(&xd->dev)) 815 queue_delayed_work(tb->wq, &xd->state_work, 816 msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT)); 817 mutex_unlock(&xd->lock); 818 } 819 break; 820 821 case UUID_REQUEST_OLD: 822 case UUID_REQUEST: 823 tb_dbg(tb, "%llx: received XDomain UUID request\n", route); 824 ret = tb_xdp_uuid_response(ctl, route, sequence, uuid); 825 /* 826 * If we've stopped the discovery with an error such as 827 * timing out, we will restart the handshake now that we 828 * received UUID request from the remote host. 829 */ 830 if (!ret && xd && xd->state == XDOMAIN_STATE_ERROR) { 831 mutex_lock(&xd->lock); 832 if (!xd->removing) { 833 dev_dbg(&xd->dev, "restarting handshake\n"); 834 start_handshake(xd); 835 } 836 mutex_unlock(&xd->lock); 837 } 838 break; 839 840 case LINK_STATE_STATUS_REQUEST: 841 tb_dbg(tb, "%llx: received XDomain link state status request\n", 842 route); 843 844 if (xd) { 845 struct tb_port *port = tb_xdomain_downstream_port(xd); 846 u8 slw, sls, tls, tlw; 847 u32 val[2]; 848 849 /* 850 * Read the adapter supported and target widths 851 * and speeds. 852 */ 853 ret = tb_port_read(port, val, TB_CFG_PORT, 854 port->cap_phy + LANE_ADP_CS_0, 855 ARRAY_SIZE(val)); 856 if (ret) 857 break; 858 859 slw = (val[0] & LANE_ADP_CS_0_SUPPORTED_WIDTH_MASK) >> 860 LANE_ADP_CS_0_SUPPORTED_WIDTH_SHIFT; 861 sls = (val[0] & LANE_ADP_CS_0_SUPPORTED_SPEED_MASK) >> 862 LANE_ADP_CS_0_SUPPORTED_SPEED_SHIFT; 863 tls = val[1] & LANE_ADP_CS_1_TARGET_SPEED_MASK; 864 tlw = (val[1] & LANE_ADP_CS_1_TARGET_WIDTH_MASK) >> 865 LANE_ADP_CS_1_TARGET_WIDTH_SHIFT; 866 867 /* 868 * When we have higher UUID, we are supposed to 869 * return ERROR_NOT_READY if the tlw is not yet 870 * set according to the Inter-Domain spec for 871 * USB4 v2. 872 */ 873 if (xd->state == XDOMAIN_STATE_BONDING_UUID_HIGH && 874 xd->target_link_width && 875 xd->target_link_width != tlw) { 876 tb_dbg(tb, "%llx: target link width not yet set %#x != %#x\n", 877 route, tlw, xd->target_link_width); 878 tb_xdp_error_response(ctl, route, sequence, 879 ERROR_NOT_READY); 880 } else { 881 tb_dbg(tb, "%llx: replying with target link width set to %#x\n", 882 route, tlw); 883 ret = tb_xdp_link_state_status_response(tb, ctl, 884 xd, sequence, slw, sls, tls, tlw); 885 } 886 } else { 887 tb_xdp_error_response(ctl, route, sequence, 888 ERROR_NOT_READY); 889 } 890 break; 891 892 case LINK_STATE_CHANGE_REQUEST: 893 tb_dbg(tb, "%llx: received XDomain link state change request\n", 894 route); 895 896 if (xd && xd->state == XDOMAIN_STATE_BONDING_UUID_HIGH && 897 pkg_len >= sizeof(struct tb_xdp_link_state_change)) { 898 const struct tb_xdp_link_state_change *lsc = 899 (const struct tb_xdp_link_state_change *)pkg; 900 901 ret = tb_xdp_link_state_change_response(ctl, route, 902 sequence, 0); 903 mutex_lock(&xd->lock); 904 if (!xd->removing) { 905 xd->target_link_width = lsc->tlw; 906 queue_delayed_work(tb->wq, &xd->state_work, 907 msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT)); 908 } 909 mutex_unlock(&xd->lock); 910 } else { 911 tb_xdp_error_response(ctl, route, sequence, 912 ERROR_NOT_READY); 913 } 914 break; 915 916 default: 917 tb_dbg(tb, "%llx: unknown XDomain request %#x\n", route, pkg->type); 918 tb_xdp_error_response(ctl, route, sequence, 919 ERROR_NOT_SUPPORTED); 920 break; 921 } 922 923 tb_xdomain_put(xd); 924 925 if (ret) { 926 tb_warn(tb, "failed to send XDomain response for %#x\n", 927 pkg->type); 928 } 929 930 out: 931 kfree(uuid); 932 kfree(xw->pkg); 933 kfree(xw); 934 935 tb_domain_put(tb); 936 } 937 938 static bool 939 tb_xdp_schedule_request(struct tb *tb, const struct tb_xdp_header *hdr, 940 size_t size) 941 { 942 struct xdomain_request_work *xw; 943 944 xw = kmalloc_obj(*xw); 945 if (!xw) 946 return false; 947 948 INIT_WORK(&xw->work, tb_xdp_handle_request); 949 xw->pkg = kmemdup(hdr, size, GFP_KERNEL); 950 if (!xw->pkg) { 951 kfree(xw); 952 return false; 953 } 954 xw->pkg_len = size; 955 xw->tb = tb_domain_get(tb); 956 957 schedule_work(&xw->work); 958 return true; 959 } 960 961 /** 962 * tb_register_service_driver() - Register XDomain service driver 963 * @drv: Driver to register 964 * 965 * Registers new service driver from @drv to the bus. 966 * 967 * Return: %0 on success, negative errno otherwise. 968 */ 969 int tb_register_service_driver(struct tb_service_driver *drv) 970 { 971 if (!drv->probe) 972 return -EINVAL; 973 974 drv->driver.bus = &tb_bus_type; 975 return driver_register(&drv->driver); 976 } 977 EXPORT_SYMBOL_GPL(tb_register_service_driver); 978 979 /** 980 * tb_unregister_service_driver() - Unregister XDomain service driver 981 * @drv: Driver to unregister 982 * 983 * Unregisters XDomain service driver from the bus. 984 */ 985 void tb_unregister_service_driver(struct tb_service_driver *drv) 986 { 987 driver_unregister(&drv->driver); 988 } 989 EXPORT_SYMBOL_GPL(tb_unregister_service_driver); 990 991 static int update_xdomain(struct device *dev, void *data) 992 { 993 struct tb_xdomain *xd; 994 995 xd = tb_to_xdomain(dev); 996 if (xd) { 997 mutex_lock(&xd->lock); 998 if (!xd->removing) 999 queue_delayed_work(xd->tb->wq, 1000 &xd->properties_changed_work, 1001 msecs_to_jiffies(50)); 1002 mutex_unlock(&xd->lock); 1003 } 1004 1005 return 0; 1006 } 1007 1008 /** 1009 * tb_service_properties_changed() - Notify the other host about changes 1010 * @svc: Service whose properties changed 1011 * 1012 * Notifies the other host that service properties may have been 1013 * changed. This should be called whenever @svc->local_properties is 1014 * updated. 1015 */ 1016 void tb_service_properties_changed(struct tb_service *svc) 1017 { 1018 struct tb_xdomain *xd = tb_service_parent(svc); 1019 1020 if (xd->is_unplugged) 1021 return; 1022 1023 scoped_guard(mutex, &xdomain_lock) 1024 xdomain_property_block_gen++; 1025 update_xdomain(&xd->dev, NULL); 1026 } 1027 EXPORT_SYMBOL_GPL(tb_service_properties_changed); 1028 1029 static ssize_t key_show(struct device *dev, struct device_attribute *attr, 1030 char *buf) 1031 { 1032 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1033 1034 /* 1035 * It should be null terminated but anything else is pretty much 1036 * allowed. 1037 */ 1038 return sysfs_emit(buf, "%*pE\n", (int)strlen(svc->key), svc->key); 1039 } 1040 static DEVICE_ATTR_RO(key); 1041 1042 static int get_modalias(const struct tb_service *svc, char *buf, size_t size) 1043 { 1044 return snprintf(buf, size, "tbsvc:k%sp%08Xv%08Xr%08X", svc->key, 1045 svc->prtcid, svc->prtcvers, svc->prtcrevs); 1046 } 1047 1048 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 1049 char *buf) 1050 { 1051 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1052 1053 /* Full buffer size except new line and null termination */ 1054 get_modalias(svc, buf, PAGE_SIZE - 2); 1055 return strlen(strcat(buf, "\n")); 1056 } 1057 static DEVICE_ATTR_RO(modalias); 1058 1059 static ssize_t prtcid_show(struct device *dev, struct device_attribute *attr, 1060 char *buf) 1061 { 1062 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1063 1064 return sysfs_emit(buf, "%u\n", svc->prtcid); 1065 } 1066 static DEVICE_ATTR_RO(prtcid); 1067 1068 static ssize_t prtcvers_show(struct device *dev, struct device_attribute *attr, 1069 char *buf) 1070 { 1071 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1072 1073 return sysfs_emit(buf, "%u\n", svc->prtcvers); 1074 } 1075 static DEVICE_ATTR_RO(prtcvers); 1076 1077 static ssize_t prtcrevs_show(struct device *dev, struct device_attribute *attr, 1078 char *buf) 1079 { 1080 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1081 1082 return sysfs_emit(buf, "%u\n", svc->prtcrevs); 1083 } 1084 static DEVICE_ATTR_RO(prtcrevs); 1085 1086 static ssize_t prtcstns_show(struct device *dev, struct device_attribute *attr, 1087 char *buf) 1088 { 1089 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1090 1091 return sysfs_emit(buf, "0x%08x\n", svc->prtcstns); 1092 } 1093 static DEVICE_ATTR_RO(prtcstns); 1094 1095 static struct attribute *tb_service_attrs[] = { 1096 &dev_attr_key.attr, 1097 &dev_attr_modalias.attr, 1098 &dev_attr_prtcid.attr, 1099 &dev_attr_prtcvers.attr, 1100 &dev_attr_prtcrevs.attr, 1101 &dev_attr_prtcstns.attr, 1102 NULL, 1103 }; 1104 1105 static const struct attribute_group tb_service_attr_group = { 1106 .attrs = tb_service_attrs, 1107 }; 1108 1109 static const struct attribute_group *tb_service_attr_groups[] = { 1110 &tb_service_attr_group, 1111 NULL, 1112 }; 1113 1114 static int tb_service_uevent(const struct device *dev, struct kobj_uevent_env *env) 1115 { 1116 const struct tb_service *svc = container_of_const(dev, struct tb_service, dev); 1117 char modalias[64]; 1118 1119 get_modalias(svc, modalias, sizeof(modalias)); 1120 return add_uevent_var(env, "MODALIAS=%s", modalias); 1121 } 1122 1123 static void tb_service_release(struct device *dev) 1124 { 1125 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1126 struct tb_xdomain *xd = tb_service_parent(svc); 1127 1128 tb_property_free_dir(svc->remote_properties); 1129 ida_free(&xd->service_ids, svc->id); 1130 kfree(svc->key); 1131 kfree(svc); 1132 tb_xdomain_put(xd); 1133 } 1134 1135 const struct device_type tb_service_type = { 1136 .name = "thunderbolt_service", 1137 .groups = tb_service_attr_groups, 1138 .uevent = tb_service_uevent, 1139 .release = tb_service_release, 1140 }; 1141 EXPORT_SYMBOL_GPL(tb_service_type); 1142 1143 static void update_service(struct tb_service *svc, struct tb_property *property) 1144 { 1145 struct tb_property_dir *dir = property->value.dir; 1146 1147 guard(mutex)(&svc->lock); 1148 tb_property_free_dir(svc->remote_properties); 1149 svc->remote_properties = tb_property_copy_dir(dir); 1150 kobject_uevent(&svc->dev.kobj, KOBJ_CHANGE); 1151 } 1152 1153 static void __unregister_service(struct device *dev) 1154 { 1155 struct tb_service *svc = tb_to_service(dev); 1156 1157 tb_service_debugfs_remove(svc); 1158 device_unregister(&svc->dev); 1159 } 1160 1161 static int remove_missing_service(struct device *dev, void *data) 1162 { 1163 struct tb_xdomain *xd = data; 1164 struct tb_service *svc; 1165 1166 svc = tb_to_service(dev); 1167 if (!svc) 1168 return 0; 1169 1170 if (!tb_property_find(xd->remote_properties, svc->key, 1171 TB_PROPERTY_TYPE_DIRECTORY)) 1172 __unregister_service(dev); 1173 1174 return 0; 1175 } 1176 1177 static int find_service(struct device *dev, const void *data) 1178 { 1179 const struct tb_property *p = data; 1180 struct tb_service *svc; 1181 1182 svc = tb_to_service(dev); 1183 if (!svc) 1184 return 0; 1185 1186 return !strcmp(svc->key, p->key); 1187 } 1188 1189 static int populate_service(struct tb_service *svc, 1190 struct tb_property *property) 1191 { 1192 struct tb_property_dir *dir = property->value.dir; 1193 struct tb_property *p; 1194 1195 /* Fill in standard properties */ 1196 p = tb_property_find(dir, "prtcid", TB_PROPERTY_TYPE_VALUE); 1197 if (p) 1198 svc->prtcid = p->value.immediate; 1199 p = tb_property_find(dir, "prtcvers", TB_PROPERTY_TYPE_VALUE); 1200 if (p) 1201 svc->prtcvers = p->value.immediate; 1202 p = tb_property_find(dir, "prtcrevs", TB_PROPERTY_TYPE_VALUE); 1203 if (p) 1204 svc->prtcrevs = p->value.immediate; 1205 p = tb_property_find(dir, "prtcstns", TB_PROPERTY_TYPE_VALUE); 1206 if (p) 1207 svc->prtcstns = p->value.immediate; 1208 1209 svc->key = kstrdup(property->key, GFP_KERNEL); 1210 if (!svc->key) 1211 return -ENOMEM; 1212 1213 svc->remote_properties = tb_property_copy_dir(dir); 1214 if (!svc->remote_properties) { 1215 kfree(svc->key); 1216 return -ENOMEM; 1217 } 1218 1219 return 0; 1220 } 1221 1222 static void enumerate_services(struct tb_xdomain *xd) 1223 { 1224 struct tb_service *svc; 1225 struct tb_property *p; 1226 struct device *dev; 1227 int id; 1228 1229 /* 1230 * First remove all services that are not available anymore in 1231 * the updated property block. 1232 */ 1233 device_for_each_child_reverse(&xd->dev, xd, remove_missing_service); 1234 1235 /* Then re-enumerate properties creating new services as we go */ 1236 tb_property_for_each(xd->remote_properties, p) { 1237 if (p->type != TB_PROPERTY_TYPE_DIRECTORY) 1238 continue; 1239 1240 /* If the service exists already we are fine */ 1241 dev = device_find_child(&xd->dev, p, find_service); 1242 if (dev) { 1243 update_service(tb_to_service(dev), p); 1244 put_device(dev); 1245 continue; 1246 } 1247 1248 svc = kzalloc_obj(*svc); 1249 if (!svc) 1250 break; 1251 1252 if (populate_service(svc, p)) { 1253 kfree(svc); 1254 break; 1255 } 1256 1257 id = ida_alloc(&xd->service_ids, GFP_KERNEL); 1258 if (id < 0) { 1259 kfree(svc->key); 1260 kfree(svc); 1261 break; 1262 } 1263 svc->id = id; 1264 svc->dev.bus = &tb_bus_type; 1265 svc->dev.type = &tb_service_type; 1266 svc->dev.parent = get_device(&xd->dev); 1267 mutex_init(&svc->lock); 1268 dev_set_name(&svc->dev, "%s.%d", dev_name(&xd->dev), svc->id); 1269 1270 tb_service_debugfs_init(svc); 1271 1272 if (device_register(&svc->dev)) { 1273 tb_service_debugfs_remove(svc); 1274 put_device(&svc->dev); 1275 break; 1276 } 1277 } 1278 } 1279 1280 static int populate_properties(struct tb_xdomain *xd, 1281 struct tb_property_dir *dir) 1282 { 1283 const struct tb_property *p; 1284 1285 /* Required properties */ 1286 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_VALUE); 1287 if (!p) 1288 return -EINVAL; 1289 xd->device = p->value.immediate; 1290 1291 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_VALUE); 1292 if (!p) 1293 return -EINVAL; 1294 xd->vendor = p->value.immediate; 1295 1296 p = tb_property_find(dir, "maxhopid", TB_PROPERTY_TYPE_VALUE); 1297 /* 1298 * USB4 inter-domain spec suggests using 15 as HopID if the 1299 * other end does not announce it in a property. This is for 1300 * TBT3 compatibility. 1301 */ 1302 xd->remote_max_hopid = p ? p->value.immediate : XDOMAIN_DEFAULT_MAX_HOPID; 1303 1304 kfree(xd->device_name); 1305 xd->device_name = NULL; 1306 kfree(xd->vendor_name); 1307 xd->vendor_name = NULL; 1308 1309 /* Optional properties */ 1310 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_TEXT); 1311 if (p) 1312 xd->device_name = kstrdup(p->value.text, GFP_KERNEL); 1313 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_TEXT); 1314 if (p) 1315 xd->vendor_name = kstrdup(p->value.text, GFP_KERNEL); 1316 1317 return 0; 1318 } 1319 1320 static int tb_xdomain_update_link_attributes(struct tb_xdomain *xd) 1321 { 1322 bool change = false; 1323 struct tb_port *port; 1324 int ret; 1325 1326 port = tb_xdomain_downstream_port(xd); 1327 1328 ret = tb_port_get_link_speed(port); 1329 if (ret < 0) 1330 return ret; 1331 1332 if (xd->link_speed != ret) 1333 change = true; 1334 1335 xd->link_speed = ret; 1336 1337 ret = tb_port_get_link_width(port); 1338 if (ret < 0) 1339 return ret; 1340 1341 if (xd->link_width != ret) 1342 change = true; 1343 1344 xd->link_width = ret; 1345 1346 if (change) 1347 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE); 1348 1349 return 0; 1350 } 1351 1352 static int tb_xdomain_get_uuid(struct tb_xdomain *xd) 1353 { 1354 struct tb *tb = xd->tb; 1355 uuid_t uuid; 1356 u64 route; 1357 int ret; 1358 1359 dev_dbg(&xd->dev, "requesting remote UUID\n"); 1360 1361 ret = tb_xdp_uuid_request(tb->ctl, xd->route, xd->state_retries, &uuid, 1362 &route); 1363 if (ret < 0) { 1364 if (xd->state_retries-- > 0) { 1365 dev_dbg(&xd->dev, "failed to request UUID, retrying\n"); 1366 return -EAGAIN; 1367 } 1368 dev_dbg(&xd->dev, "failed to read remote UUID\n"); 1369 return ret; 1370 } 1371 1372 dev_dbg(&xd->dev, "got remote UUID %pUb\n", &uuid); 1373 1374 if (uuid_equal(&uuid, xd->local_uuid)) { 1375 if (route == xd->route) 1376 dev_dbg(&xd->dev, "loop back detected\n"); 1377 else 1378 dev_dbg(&xd->dev, "intra-domain loop detected\n"); 1379 1380 /* Don't bond lanes automatically for loops */ 1381 xd->bonding_possible = false; 1382 } 1383 1384 /* 1385 * If the UUID is different, there is another domain connected 1386 * so mark this one unplugged and wait for the connection 1387 * manager to replace it. 1388 */ 1389 if (xd->remote_uuid && !uuid_equal(&uuid, xd->remote_uuid)) { 1390 dev_dbg(&xd->dev, "remote UUID is different, unplugging\n"); 1391 xd->is_unplugged = true; 1392 return -ENODEV; 1393 } 1394 1395 /* First time fill in the missing UUID */ 1396 if (!xd->remote_uuid) { 1397 xd->remote_uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL); 1398 if (!xd->remote_uuid) 1399 return -ENOMEM; 1400 } 1401 1402 return 0; 1403 } 1404 1405 static int tb_xdomain_get_link_status(struct tb_xdomain *xd) 1406 { 1407 struct tb *tb = xd->tb; 1408 u8 slw, tlw, sls, tls; 1409 int ret; 1410 1411 dev_dbg(&xd->dev, "sending link state status request to %pUb\n", 1412 xd->remote_uuid); 1413 1414 ret = tb_xdp_link_state_status_request(tb->ctl, xd->route, 1415 xd->state_retries, &slw, &tlw, &sls, 1416 &tls); 1417 if (ret) { 1418 if (ret != -EOPNOTSUPP && xd->state_retries-- > 0) { 1419 dev_dbg(&xd->dev, 1420 "failed to request remote link status, retrying\n"); 1421 return -EAGAIN; 1422 } 1423 dev_dbg(&xd->dev, "failed to receive remote link status\n"); 1424 return ret; 1425 } 1426 1427 dev_dbg(&xd->dev, "remote link supports width %#x speed %#x\n", slw, sls); 1428 1429 if (slw < LANE_ADP_CS_0_SUPPORTED_WIDTH_DUAL) { 1430 dev_dbg(&xd->dev, "remote adapter is single lane only\n"); 1431 return -EOPNOTSUPP; 1432 } 1433 1434 return 0; 1435 } 1436 1437 static int tb_xdomain_link_state_change(struct tb_xdomain *xd, 1438 unsigned int width) 1439 { 1440 struct tb_port *port = tb_xdomain_downstream_port(xd); 1441 struct tb *tb = xd->tb; 1442 u8 tlw, tls; 1443 u32 val; 1444 int ret; 1445 1446 if (width == 2) 1447 tlw = LANE_ADP_CS_1_TARGET_WIDTH_DUAL; 1448 else if (width == 1) 1449 tlw = LANE_ADP_CS_1_TARGET_WIDTH_SINGLE; 1450 else 1451 return -EINVAL; 1452 1453 /* Use the current target speed */ 1454 ret = tb_port_read(port, &val, TB_CFG_PORT, port->cap_phy + LANE_ADP_CS_1, 1); 1455 if (ret) 1456 return ret; 1457 tls = val & LANE_ADP_CS_1_TARGET_SPEED_MASK; 1458 1459 dev_dbg(&xd->dev, "sending link state change request with width %#x speed %#x\n", 1460 tlw, tls); 1461 1462 ret = tb_xdp_link_state_change_request(tb->ctl, xd->route, 1463 xd->state_retries, tlw, tls); 1464 if (ret) { 1465 if (ret != -EOPNOTSUPP && xd->state_retries-- > 0) { 1466 dev_dbg(&xd->dev, 1467 "failed to change remote link state, retrying\n"); 1468 return -EAGAIN; 1469 } 1470 dev_err(&xd->dev, "failed request link state change, aborting\n"); 1471 return ret; 1472 } 1473 1474 dev_dbg(&xd->dev, "received link state change response\n"); 1475 return 0; 1476 } 1477 1478 static int tb_xdomain_bond_lanes_uuid_high(struct tb_xdomain *xd) 1479 { 1480 unsigned int width, width_mask; 1481 struct tb_port *port; 1482 int ret; 1483 1484 if (xd->target_link_width == LANE_ADP_CS_1_TARGET_WIDTH_SINGLE) { 1485 width = TB_LINK_WIDTH_SINGLE; 1486 width_mask = width; 1487 } else if (xd->target_link_width == LANE_ADP_CS_1_TARGET_WIDTH_DUAL) { 1488 width = TB_LINK_WIDTH_DUAL; 1489 width_mask = width | TB_LINK_WIDTH_ASYM_TX | TB_LINK_WIDTH_ASYM_RX; 1490 } else { 1491 if (xd->state_retries-- > 0) { 1492 dev_dbg(&xd->dev, 1493 "link state change request not received yet, retrying\n"); 1494 return -EAGAIN; 1495 } 1496 dev_dbg(&xd->dev, "timeout waiting for link change request\n"); 1497 return -ETIMEDOUT; 1498 } 1499 1500 port = tb_xdomain_downstream_port(xd); 1501 1502 /* 1503 * We can't use tb_xdomain_lane_bonding_enable() here because it 1504 * is the other side that initiates lane bonding. So here we 1505 * just set the width to both lane adapters and wait for the 1506 * link to transition bonded. 1507 */ 1508 ret = tb_port_set_link_width(port->dual_link_port, width); 1509 if (ret) { 1510 tb_port_warn(port->dual_link_port, 1511 "failed to set link width to %d\n", width); 1512 return ret; 1513 } 1514 1515 ret = tb_port_set_link_width(port, width); 1516 if (ret) { 1517 tb_port_warn(port, "failed to set link width to %d\n", width); 1518 return ret; 1519 } 1520 1521 ret = tb_port_wait_for_link_width(port, width_mask, 1522 XDOMAIN_BONDING_TIMEOUT); 1523 if (ret) { 1524 dev_warn(&xd->dev, "error waiting for link width to become %d\n", 1525 width_mask); 1526 return ret; 1527 } 1528 1529 port->bonded = width > TB_LINK_WIDTH_SINGLE; 1530 port->dual_link_port->bonded = width > TB_LINK_WIDTH_SINGLE; 1531 1532 tb_port_update_credits(port); 1533 tb_xdomain_update_link_attributes(xd); 1534 1535 dev_dbg(&xd->dev, "lane bonding %s\n", str_enabled_disabled(width == 2)); 1536 return 0; 1537 } 1538 1539 static int tb_xdomain_get_properties(struct tb_xdomain *xd) 1540 { 1541 struct tb_property_dir *dir; 1542 struct tb *tb = xd->tb; 1543 bool update = false; 1544 u32 *block = NULL; 1545 u32 gen = 0; 1546 int ret; 1547 1548 dev_dbg(&xd->dev, "requesting remote properties\n"); 1549 1550 ret = tb_xdp_properties_request(tb->ctl, xd->route, xd->local_uuid, 1551 xd->remote_uuid, xd->state_retries, 1552 &block, &gen); 1553 if (ret < 0) { 1554 if (xd->state_retries-- > 0) { 1555 dev_dbg(&xd->dev, 1556 "failed to request remote properties, retrying\n"); 1557 return -EAGAIN; 1558 } 1559 /* Give up now */ 1560 dev_err(&xd->dev, "failed read XDomain properties from %pUb\n", 1561 xd->remote_uuid); 1562 1563 return ret; 1564 } 1565 1566 mutex_lock(&xd->lock); 1567 1568 /* Only accept newer generation properties */ 1569 if (xd->remote_properties && gen <= xd->remote_property_block_gen) { 1570 ret = 0; 1571 goto err_free_block; 1572 } 1573 1574 dir = tb_property_parse_dir(block, ret); 1575 if (!dir) { 1576 dev_err(&xd->dev, "failed to parse XDomain properties\n"); 1577 ret = -ENOMEM; 1578 goto err_free_block; 1579 } 1580 1581 ret = populate_properties(xd, dir); 1582 if (ret) { 1583 dev_err(&xd->dev, "missing XDomain properties in response\n"); 1584 goto err_free_dir; 1585 } 1586 1587 /* Release the existing one */ 1588 if (xd->remote_properties) { 1589 tb_property_free_dir(xd->remote_properties); 1590 update = true; 1591 } 1592 1593 xd->remote_properties = dir; 1594 xd->remote_property_block_gen = gen; 1595 1596 tb_xdomain_update_link_attributes(xd); 1597 1598 mutex_unlock(&xd->lock); 1599 1600 kfree(block); 1601 1602 /* 1603 * Now the device should be ready enough so we can add it to the 1604 * bus and let userspace know about it. If the device is already 1605 * registered, we notify the userspace that it has changed. 1606 */ 1607 if (!update) { 1608 /* 1609 * Now disable lane 1 if bonding was not enabled. Do 1610 * this only if bonding was possible at the beginning 1611 * (that is we are the connection manager and there are 1612 * two lanes). 1613 */ 1614 if (xd->bonding_possible) { 1615 struct tb_port *port; 1616 1617 port = tb_xdomain_downstream_port(xd); 1618 if (!port->bonded) 1619 tb_port_disable(port->dual_link_port); 1620 } 1621 1622 dev_dbg(&xd->dev, "current link speed %u.0 Gb/s\n", 1623 xd->link_speed); 1624 dev_dbg(&xd->dev, "current link width %s\n", 1625 tb_width_name(xd->link_width)); 1626 1627 if (device_add(&xd->dev)) { 1628 dev_err(&xd->dev, "failed to add XDomain device\n"); 1629 return -ENODEV; 1630 } 1631 dev_info(&xd->dev, "new host found, vendor=%#x device=%#x\n", 1632 xd->vendor, xd->device); 1633 if (xd->vendor_name && xd->device_name) 1634 dev_info(&xd->dev, "%s %s\n", xd->vendor_name, 1635 xd->device_name); 1636 1637 tb_xdomain_debugfs_init(xd); 1638 } else { 1639 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE); 1640 } 1641 1642 enumerate_services(xd); 1643 return 0; 1644 1645 err_free_dir: 1646 tb_property_free_dir(dir); 1647 err_free_block: 1648 kfree(block); 1649 mutex_unlock(&xd->lock); 1650 1651 return ret; 1652 } 1653 1654 static void tb_xdomain_queue_uuid(struct tb_xdomain *xd) 1655 { 1656 xd->state = XDOMAIN_STATE_UUID; 1657 xd->state_retries = XDOMAIN_RETRIES; 1658 queue_delayed_work(xd->tb->wq, &xd->state_work, 1659 msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT)); 1660 } 1661 1662 static void tb_xdomain_queue_link_status(struct tb_xdomain *xd) 1663 { 1664 xd->state = XDOMAIN_STATE_LINK_STATUS; 1665 xd->state_retries = XDOMAIN_RETRIES; 1666 queue_delayed_work(xd->tb->wq, &xd->state_work, 1667 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1668 } 1669 1670 static void tb_xdomain_queue_link_status2(struct tb_xdomain *xd) 1671 { 1672 xd->state = XDOMAIN_STATE_LINK_STATUS2; 1673 xd->state_retries = XDOMAIN_RETRIES; 1674 queue_delayed_work(xd->tb->wq, &xd->state_work, 1675 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1676 } 1677 1678 static void tb_xdomain_queue_bonding(struct tb_xdomain *xd) 1679 { 1680 if (memcmp(xd->local_uuid, xd->remote_uuid, UUID_SIZE) > 0) { 1681 dev_dbg(&xd->dev, "we have higher UUID, other side bonds the lanes\n"); 1682 xd->state = XDOMAIN_STATE_BONDING_UUID_HIGH; 1683 } else { 1684 dev_dbg(&xd->dev, "we have lower UUID, bonding lanes\n"); 1685 xd->state = XDOMAIN_STATE_LINK_STATE_CHANGE; 1686 } 1687 1688 xd->state_retries = XDOMAIN_RETRIES; 1689 queue_delayed_work(xd->tb->wq, &xd->state_work, 1690 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1691 } 1692 1693 static void tb_xdomain_queue_bonding_uuid_low(struct tb_xdomain *xd) 1694 { 1695 xd->state = XDOMAIN_STATE_BONDING_UUID_LOW; 1696 xd->state_retries = XDOMAIN_RETRIES; 1697 queue_delayed_work(xd->tb->wq, &xd->state_work, 1698 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1699 } 1700 1701 static void tb_xdomain_queue_properties(struct tb_xdomain *xd) 1702 { 1703 xd->state = XDOMAIN_STATE_PROPERTIES; 1704 xd->state_retries = XDOMAIN_RETRIES; 1705 queue_delayed_work(xd->tb->wq, &xd->state_work, 1706 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1707 } 1708 1709 static void tb_xdomain_queue_properties_changed(struct tb_xdomain *xd) 1710 { 1711 xd->properties_changed_retries = XDOMAIN_RETRIES; 1712 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work, 1713 msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT)); 1714 } 1715 1716 static void tb_xdomain_failed(struct tb_xdomain *xd) 1717 { 1718 xd->state = XDOMAIN_STATE_ERROR; 1719 queue_delayed_work(xd->tb->wq, &xd->state_work, 1720 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1721 } 1722 1723 static void tb_xdomain_state_work(struct work_struct *work) 1724 { 1725 struct tb_xdomain *xd = container_of(work, typeof(*xd), state_work.work); 1726 int ret, state = xd->state; 1727 1728 if (WARN_ON_ONCE(state < XDOMAIN_STATE_INIT || 1729 state > XDOMAIN_STATE_ERROR)) 1730 return; 1731 1732 dev_dbg(&xd->dev, "running state %s\n", state_names[state]); 1733 1734 switch (state) { 1735 case XDOMAIN_STATE_INIT: 1736 if (xd->needs_uuid) { 1737 tb_xdomain_queue_uuid(xd); 1738 } else { 1739 tb_xdomain_queue_properties_changed(xd); 1740 tb_xdomain_queue_properties(xd); 1741 } 1742 break; 1743 1744 case XDOMAIN_STATE_UUID: 1745 ret = tb_xdomain_get_uuid(xd); 1746 if (ret) { 1747 if (ret == -EAGAIN) 1748 goto retry_state; 1749 tb_xdomain_failed(xd); 1750 } else { 1751 tb_xdomain_queue_properties_changed(xd); 1752 if (xd->bonding_possible) 1753 tb_xdomain_queue_link_status(xd); 1754 else 1755 tb_xdomain_queue_properties(xd); 1756 } 1757 break; 1758 1759 case XDOMAIN_STATE_LINK_STATUS: 1760 ret = tb_xdomain_get_link_status(xd); 1761 if (ret) { 1762 if (ret == -EAGAIN) 1763 goto retry_state; 1764 1765 /* 1766 * If any of the lane bonding states fail we skip 1767 * bonding completely and try to continue from 1768 * reading properties. 1769 */ 1770 tb_xdomain_queue_properties(xd); 1771 } else { 1772 tb_xdomain_queue_bonding(xd); 1773 } 1774 break; 1775 1776 case XDOMAIN_STATE_LINK_STATE_CHANGE: 1777 ret = tb_xdomain_link_state_change(xd, 2); 1778 if (ret) { 1779 if (ret == -EAGAIN) 1780 goto retry_state; 1781 tb_xdomain_queue_properties(xd); 1782 } else { 1783 tb_xdomain_queue_link_status2(xd); 1784 } 1785 break; 1786 1787 case XDOMAIN_STATE_LINK_STATUS2: 1788 ret = tb_xdomain_get_link_status(xd); 1789 if (ret) { 1790 if (ret == -EAGAIN) 1791 goto retry_state; 1792 tb_xdomain_queue_properties(xd); 1793 } else { 1794 tb_xdomain_queue_bonding_uuid_low(xd); 1795 } 1796 break; 1797 1798 case XDOMAIN_STATE_BONDING_UUID_LOW: 1799 tb_xdomain_lane_bonding_enable(xd); 1800 tb_xdomain_queue_properties(xd); 1801 break; 1802 1803 case XDOMAIN_STATE_BONDING_UUID_HIGH: 1804 if (tb_xdomain_bond_lanes_uuid_high(xd) == -EAGAIN) 1805 goto retry_state; 1806 tb_xdomain_queue_properties(xd); 1807 break; 1808 1809 case XDOMAIN_STATE_PROPERTIES: 1810 ret = tb_xdomain_get_properties(xd); 1811 if (ret) { 1812 if (ret == -EAGAIN) 1813 goto retry_state; 1814 tb_xdomain_failed(xd); 1815 } else { 1816 xd->state = XDOMAIN_STATE_ENUMERATED; 1817 tb_xdomain_queue_properties_changed(xd); 1818 } 1819 break; 1820 1821 case XDOMAIN_STATE_ENUMERATED: 1822 tb_xdomain_queue_properties(xd); 1823 break; 1824 1825 case XDOMAIN_STATE_ERROR: 1826 dev_dbg(&xd->dev, "discovery failed, stopping handshake\n"); 1827 __stop_handshake(xd); 1828 break; 1829 1830 default: 1831 dev_warn(&xd->dev, "unexpected state %d\n", state); 1832 break; 1833 } 1834 1835 return; 1836 1837 retry_state: 1838 queue_delayed_work(xd->tb->wq, &xd->state_work, 1839 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1840 } 1841 1842 static void tb_xdomain_properties_changed(struct work_struct *work) 1843 { 1844 struct tb_xdomain *xd = container_of(work, typeof(*xd), 1845 properties_changed_work.work); 1846 int ret; 1847 1848 dev_dbg(&xd->dev, "sending properties changed notification\n"); 1849 1850 ret = tb_xdp_properties_changed_request(xd->tb->ctl, xd->route, 1851 xd->properties_changed_retries, xd->local_uuid); 1852 if (ret) { 1853 if (xd->properties_changed_retries-- > 0) { 1854 dev_dbg(&xd->dev, 1855 "failed to send properties changed notification, retrying\n"); 1856 queue_delayed_work(xd->tb->wq, 1857 &xd->properties_changed_work, 1858 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1859 } 1860 dev_err(&xd->dev, "failed to send properties changed notification\n"); 1861 return; 1862 } 1863 1864 xd->properties_changed_retries = XDOMAIN_RETRIES; 1865 } 1866 1867 static ssize_t device_show(struct device *dev, struct device_attribute *attr, 1868 char *buf) 1869 { 1870 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1871 1872 return sysfs_emit(buf, "%#x\n", xd->device); 1873 } 1874 static DEVICE_ATTR_RO(device); 1875 1876 static ssize_t 1877 device_name_show(struct device *dev, struct device_attribute *attr, char *buf) 1878 { 1879 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1880 int ret; 1881 1882 if (mutex_lock_interruptible(&xd->lock)) 1883 return -ERESTARTSYS; 1884 ret = sysfs_emit(buf, "%s\n", xd->device_name ?: ""); 1885 mutex_unlock(&xd->lock); 1886 1887 return ret; 1888 } 1889 static DEVICE_ATTR_RO(device_name); 1890 1891 static ssize_t maxhopid_show(struct device *dev, struct device_attribute *attr, 1892 char *buf) 1893 { 1894 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1895 1896 return sysfs_emit(buf, "%d\n", xd->remote_max_hopid); 1897 } 1898 static DEVICE_ATTR_RO(maxhopid); 1899 1900 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr, 1901 char *buf) 1902 { 1903 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1904 1905 return sysfs_emit(buf, "%#x\n", xd->vendor); 1906 } 1907 static DEVICE_ATTR_RO(vendor); 1908 1909 static ssize_t 1910 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf) 1911 { 1912 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1913 int ret; 1914 1915 if (mutex_lock_interruptible(&xd->lock)) 1916 return -ERESTARTSYS; 1917 ret = sysfs_emit(buf, "%s\n", xd->vendor_name ?: ""); 1918 mutex_unlock(&xd->lock); 1919 1920 return ret; 1921 } 1922 static DEVICE_ATTR_RO(vendor_name); 1923 1924 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr, 1925 char *buf) 1926 { 1927 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1928 1929 return sysfs_emit(buf, "%pUb\n", xd->remote_uuid); 1930 } 1931 static DEVICE_ATTR_RO(unique_id); 1932 1933 static ssize_t speed_show(struct device *dev, struct device_attribute *attr, 1934 char *buf) 1935 { 1936 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1937 1938 return sysfs_emit(buf, "%u.0 Gb/s\n", xd->link_speed); 1939 } 1940 1941 static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL); 1942 static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL); 1943 1944 static ssize_t rx_lanes_show(struct device *dev, struct device_attribute *attr, 1945 char *buf) 1946 { 1947 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1948 unsigned int width; 1949 1950 switch (xd->link_width) { 1951 case TB_LINK_WIDTH_SINGLE: 1952 case TB_LINK_WIDTH_ASYM_TX: 1953 width = 1; 1954 break; 1955 case TB_LINK_WIDTH_DUAL: 1956 width = 2; 1957 break; 1958 case TB_LINK_WIDTH_ASYM_RX: 1959 width = 3; 1960 break; 1961 default: 1962 WARN_ON_ONCE(1); 1963 return -EINVAL; 1964 } 1965 1966 return sysfs_emit(buf, "%u\n", width); 1967 } 1968 static DEVICE_ATTR(rx_lanes, 0444, rx_lanes_show, NULL); 1969 1970 static ssize_t tx_lanes_show(struct device *dev, struct device_attribute *attr, 1971 char *buf) 1972 { 1973 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1974 unsigned int width; 1975 1976 switch (xd->link_width) { 1977 case TB_LINK_WIDTH_SINGLE: 1978 case TB_LINK_WIDTH_ASYM_RX: 1979 width = 1; 1980 break; 1981 case TB_LINK_WIDTH_DUAL: 1982 width = 2; 1983 break; 1984 case TB_LINK_WIDTH_ASYM_TX: 1985 width = 3; 1986 break; 1987 default: 1988 WARN_ON_ONCE(1); 1989 return -EINVAL; 1990 } 1991 1992 return sysfs_emit(buf, "%u\n", width); 1993 } 1994 static DEVICE_ATTR(tx_lanes, 0444, tx_lanes_show, NULL); 1995 1996 static struct attribute *xdomain_attrs[] = { 1997 &dev_attr_device.attr, 1998 &dev_attr_device_name.attr, 1999 &dev_attr_maxhopid.attr, 2000 &dev_attr_rx_lanes.attr, 2001 &dev_attr_rx_speed.attr, 2002 &dev_attr_tx_lanes.attr, 2003 &dev_attr_tx_speed.attr, 2004 &dev_attr_unique_id.attr, 2005 &dev_attr_vendor.attr, 2006 &dev_attr_vendor_name.attr, 2007 NULL, 2008 }; 2009 2010 static const struct attribute_group xdomain_attr_group = { 2011 .attrs = xdomain_attrs, 2012 }; 2013 2014 static const struct attribute_group *xdomain_attr_groups[] = { 2015 &xdomain_attr_group, 2016 NULL, 2017 }; 2018 2019 static void tb_xdomain_release(struct device *dev) 2020 { 2021 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 2022 2023 put_device(xd->dev.parent); 2024 2025 kfree(xd->local_property_block); 2026 tb_property_free_dir(xd->remote_properties); 2027 ida_destroy(&xd->out_hopids); 2028 ida_destroy(&xd->in_hopids); 2029 ida_destroy(&xd->service_ids); 2030 2031 kfree(xd->local_uuid); 2032 kfree(xd->remote_uuid); 2033 kfree(xd->device_name); 2034 kfree(xd->vendor_name); 2035 kfree(xd); 2036 } 2037 2038 static int __maybe_unused tb_xdomain_suspend(struct device *dev) 2039 { 2040 stop_handshake(tb_to_xdomain(dev)); 2041 return 0; 2042 } 2043 2044 static int __maybe_unused tb_xdomain_resume(struct device *dev) 2045 { 2046 start_handshake(tb_to_xdomain(dev)); 2047 return 0; 2048 } 2049 2050 static const struct dev_pm_ops tb_xdomain_pm_ops = { 2051 SET_SYSTEM_SLEEP_PM_OPS(tb_xdomain_suspend, tb_xdomain_resume) 2052 }; 2053 2054 const struct device_type tb_xdomain_type = { 2055 .name = "thunderbolt_xdomain", 2056 .release = tb_xdomain_release, 2057 .pm = &tb_xdomain_pm_ops, 2058 }; 2059 EXPORT_SYMBOL_GPL(tb_xdomain_type); 2060 2061 static void tb_xdomain_link_init(struct tb_xdomain *xd, struct tb_port *down) 2062 { 2063 if (!down->dual_link_port) 2064 return; 2065 2066 /* 2067 * Gen 4 links come up already as bonded so only update the port 2068 * structures here. 2069 */ 2070 if (tb_port_get_link_generation(down) >= 4) { 2071 down->bonded = true; 2072 down->dual_link_port->bonded = true; 2073 } else { 2074 xd->bonding_possible = true; 2075 } 2076 } 2077 2078 static void tb_xdomain_link_exit(struct tb_xdomain *xd) 2079 { 2080 struct tb_port *down = tb_xdomain_downstream_port(xd); 2081 2082 if (!down->dual_link_port) 2083 return; 2084 2085 if (tb_port_get_link_generation(down) >= 4) { 2086 down->bonded = false; 2087 down->dual_link_port->bonded = false; 2088 return; 2089 } 2090 2091 if (!xd->bonding_possible) 2092 return; 2093 2094 if (xd->link_width > TB_LINK_WIDTH_SINGLE) { 2095 /* 2096 * Just return port structures back to way they were and 2097 * update credits. No need to update userspace because 2098 * the XDomain is removed soon anyway. 2099 */ 2100 tb_port_lane_bonding_disable(down); 2101 tb_port_update_credits(down); 2102 } else if (down->dual_link_port) { 2103 /* 2104 * Re-enable the lane 1 adapter we disabled at the end 2105 * of tb_xdomain_get_properties(). 2106 */ 2107 tb_port_enable(down->dual_link_port); 2108 } 2109 } 2110 2111 /** 2112 * tb_xdomain_alloc() - Allocate new XDomain object 2113 * @tb: Domain where the XDomain belongs 2114 * @parent: Parent device (the switch through which the other domain 2115 * is reached). 2116 * @route: Route string used to reach the other domain 2117 * @local_uuid: Our local domain UUID 2118 * @remote_uuid: UUID of the other domain (optional) 2119 * 2120 * Allocates new XDomain structure and returns pointer to that. The 2121 * object must be released by calling tb_xdomain_put(). 2122 * 2123 * Return: Pointer to &struct tb_xdomain, %NULL in case of failure. 2124 */ 2125 struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent, 2126 u64 route, const uuid_t *local_uuid, 2127 const uuid_t *remote_uuid) 2128 { 2129 struct tb_switch *parent_sw = tb_to_switch(parent); 2130 struct tb_xdomain *xd; 2131 struct tb_port *down; 2132 2133 /* Make sure the downstream domain is accessible */ 2134 down = tb_port_at(route, parent_sw); 2135 tb_port_unlock(down); 2136 2137 xd = kzalloc_obj(*xd); 2138 if (!xd) 2139 return NULL; 2140 2141 xd->tb = tb; 2142 xd->route = route; 2143 xd->local_max_hopid = down->config.max_in_hop_id; 2144 ida_init(&xd->service_ids); 2145 ida_init(&xd->in_hopids); 2146 ida_init(&xd->out_hopids); 2147 mutex_init(&xd->lock); 2148 INIT_DELAYED_WORK(&xd->state_work, tb_xdomain_state_work); 2149 INIT_DELAYED_WORK(&xd->properties_changed_work, 2150 tb_xdomain_properties_changed); 2151 atomic_set(&xd->ntunnels, 0); 2152 2153 xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL); 2154 if (!xd->local_uuid) 2155 goto err_free; 2156 2157 if (remote_uuid) { 2158 xd->remote_uuid = kmemdup(remote_uuid, sizeof(uuid_t), 2159 GFP_KERNEL); 2160 if (!xd->remote_uuid) 2161 goto err_free_local_uuid; 2162 } else { 2163 xd->needs_uuid = true; 2164 2165 tb_xdomain_link_init(xd, down); 2166 } 2167 2168 device_initialize(&xd->dev); 2169 xd->dev.parent = get_device(parent); 2170 xd->dev.bus = &tb_bus_type; 2171 xd->dev.type = &tb_xdomain_type; 2172 xd->dev.groups = xdomain_attr_groups; 2173 dev_set_name(&xd->dev, "%u-%llx", tb->index, route); 2174 2175 dev_dbg(&xd->dev, "local UUID %pUb\n", local_uuid); 2176 if (remote_uuid) 2177 dev_dbg(&xd->dev, "remote UUID %pUb\n", remote_uuid); 2178 2179 /* 2180 * This keeps the DMA powered on as long as we have active 2181 * connection to another host. 2182 */ 2183 pm_runtime_set_active(&xd->dev); 2184 pm_runtime_get_noresume(&xd->dev); 2185 pm_runtime_enable(&xd->dev); 2186 2187 return xd; 2188 2189 err_free_local_uuid: 2190 kfree(xd->local_uuid); 2191 err_free: 2192 kfree(xd); 2193 2194 return NULL; 2195 } 2196 2197 /** 2198 * tb_xdomain_add() - Add XDomain to the bus 2199 * @xd: XDomain to add 2200 * 2201 * This function starts XDomain discovery protocol handshake and 2202 * eventually adds the XDomain to the bus. After calling this function 2203 * the caller needs to call tb_xdomain_remove() in order to remove and 2204 * release the object regardless whether the handshake succeeded or not. 2205 */ 2206 void tb_xdomain_add(struct tb_xdomain *xd) 2207 { 2208 /* Start exchanging properties with the other host */ 2209 start_handshake(xd); 2210 } 2211 2212 static int unregister_service(struct device *dev, void *data) 2213 { 2214 __unregister_service(dev); 2215 return 0; 2216 } 2217 2218 /** 2219 * tb_xdomain_remove() - Remove XDomain 2220 * @xd: XDomain to remove 2221 * 2222 * This will stop all ongoing configuration work. XDomain is not removed 2223 * from the bus if it was added. That needs to be done separately by 2224 * calling tb_xdomain_unregister(). 2225 * 2226 * Called with @tb->lock held. 2227 */ 2228 void tb_xdomain_remove(struct tb_xdomain *xd) 2229 { 2230 tb_xdomain_debugfs_remove(xd); 2231 2232 mutex_lock(&xd->lock); 2233 xd->removing = true; 2234 mutex_unlock(&xd->lock); 2235 2236 stop_handshake(xd); 2237 tb_xdomain_link_exit(xd); 2238 2239 if (!device_is_registered(&xd->dev)) { 2240 /* 2241 * Undo runtime PM here explicitly because it is 2242 * possible that the XDomain was never added to the bus 2243 * and thus device_del() is not called for it 2244 * (device_del() would handle this otherwise). 2245 */ 2246 pm_runtime_disable(&xd->dev); 2247 pm_runtime_put_noidle(&xd->dev); 2248 pm_runtime_set_suspended(&xd->dev); 2249 put_device(&xd->dev); 2250 } 2251 } 2252 2253 /** 2254 * tb_xdomain_unregister() - Unregister XDomain 2255 * @xd: XDomain to unregister 2256 * 2257 * This will unregister the XDomain along with any services from the 2258 * bus. When the last reference to @xd is released the object will be 2259 * released as well. 2260 */ 2261 void tb_xdomain_unregister(struct tb_xdomain *xd) 2262 { 2263 lockdep_assert_not_held(&xd->tb->lock); 2264 2265 device_for_each_child_reverse(&xd->dev, xd, unregister_service); 2266 2267 dev_info(&xd->dev, "host disconnected\n"); 2268 device_unregister(&xd->dev); 2269 } 2270 2271 /** 2272 * tb_xdomain_lane_bonding_enable() - Enable lane bonding on XDomain 2273 * @xd: XDomain connection 2274 * 2275 * Lane bonding is disabled by default for XDomains. This function tries 2276 * to enable bonding by first enabling the port and waiting for the CL0 2277 * state. 2278 * 2279 * Return: %0 on success, negative errno otherwise. 2280 */ 2281 int tb_xdomain_lane_bonding_enable(struct tb_xdomain *xd) 2282 { 2283 unsigned int width_mask; 2284 struct tb_port *port; 2285 int ret; 2286 2287 port = tb_xdomain_downstream_port(xd); 2288 if (!port->dual_link_port) 2289 return -ENODEV; 2290 2291 ret = tb_port_enable(port->dual_link_port); 2292 if (ret) 2293 return ret; 2294 2295 ret = tb_wait_for_port(port->dual_link_port, true); 2296 if (ret < 0) 2297 return ret; 2298 if (!ret) 2299 return -ENOTCONN; 2300 2301 ret = tb_port_lane_bonding_enable(port); 2302 if (ret) { 2303 tb_port_warn(port, "failed to enable lane bonding\n"); 2304 return ret; 2305 } 2306 2307 /* Any of the widths are all bonded */ 2308 width_mask = TB_LINK_WIDTH_DUAL | TB_LINK_WIDTH_ASYM_TX | 2309 TB_LINK_WIDTH_ASYM_RX; 2310 2311 ret = tb_port_wait_for_link_width(port, width_mask, 2312 XDOMAIN_BONDING_TIMEOUT); 2313 if (ret) { 2314 tb_port_warn(port, "failed to enable lane bonding\n"); 2315 return ret; 2316 } 2317 2318 tb_port_update_credits(port); 2319 tb_xdomain_update_link_attributes(xd); 2320 2321 dev_dbg(&xd->dev, "lane bonding enabled\n"); 2322 return 0; 2323 } 2324 EXPORT_SYMBOL_GPL(tb_xdomain_lane_bonding_enable); 2325 2326 /** 2327 * tb_xdomain_lane_bonding_disable() - Disable lane bonding 2328 * @xd: XDomain connection 2329 * 2330 * Lane bonding is disabled by default for XDomains. If bonding has been 2331 * enabled, this function can be used to disable it. 2332 */ 2333 void tb_xdomain_lane_bonding_disable(struct tb_xdomain *xd) 2334 { 2335 struct tb_port *port; 2336 2337 port = tb_xdomain_downstream_port(xd); 2338 if (port->dual_link_port) { 2339 int ret; 2340 2341 tb_port_lane_bonding_disable(port); 2342 ret = tb_port_wait_for_link_width(port, TB_LINK_WIDTH_SINGLE, 100); 2343 if (ret == -ETIMEDOUT) 2344 tb_port_warn(port, "timeout disabling lane bonding\n"); 2345 tb_port_disable(port->dual_link_port); 2346 tb_port_update_credits(port); 2347 tb_xdomain_update_link_attributes(xd); 2348 2349 dev_dbg(&xd->dev, "lane bonding disabled\n"); 2350 } 2351 } 2352 EXPORT_SYMBOL_GPL(tb_xdomain_lane_bonding_disable); 2353 2354 /** 2355 * tb_xdomain_alloc_in_hopid() - Allocate input HopID for tunneling 2356 * @xd: XDomain connection 2357 * @hopid: Preferred HopID or %-1 for next available 2358 * 2359 * Returned HopID is guaranteed to be within range supported by the input 2360 * lane adapter. 2361 * Call tb_xdomain_release_in_hopid() to release the allocated HopID. 2362 * 2363 * Return: 2364 * * Allocated HopID - On success. 2365 * * %-ENOSPC - If there are no more available HopIDs. 2366 * * Negative errno - Another error occurred. 2367 */ 2368 int tb_xdomain_alloc_in_hopid(struct tb_xdomain *xd, int hopid) 2369 { 2370 if (hopid < 0) 2371 hopid = TB_PATH_MIN_HOPID; 2372 if (hopid < TB_PATH_MIN_HOPID || hopid > xd->local_max_hopid) 2373 return -EINVAL; 2374 2375 return ida_alloc_range(&xd->in_hopids, hopid, xd->local_max_hopid, 2376 GFP_KERNEL); 2377 } 2378 EXPORT_SYMBOL_GPL(tb_xdomain_alloc_in_hopid); 2379 2380 /** 2381 * tb_xdomain_alloc_out_hopid() - Allocate output HopID for tunneling 2382 * @xd: XDomain connection 2383 * @hopid: Preferred HopID or %-1 for next available 2384 * 2385 * Returned HopID is guaranteed to be within range supported by the 2386 * output lane adapter. 2387 * Call tb_xdomain_release_out_hopid() to release the allocated HopID. 2388 * 2389 * Return: 2390 * * Allocated HopID - On success. 2391 * * %-ENOSPC - If there are no more available HopIDs. 2392 * * Negative errno - Another error occurred. 2393 */ 2394 int tb_xdomain_alloc_out_hopid(struct tb_xdomain *xd, int hopid) 2395 { 2396 if (hopid < 0) 2397 hopid = TB_PATH_MIN_HOPID; 2398 if (hopid < TB_PATH_MIN_HOPID || hopid > xd->remote_max_hopid) 2399 return -EINVAL; 2400 2401 return ida_alloc_range(&xd->out_hopids, hopid, xd->remote_max_hopid, 2402 GFP_KERNEL); 2403 } 2404 EXPORT_SYMBOL_GPL(tb_xdomain_alloc_out_hopid); 2405 2406 /** 2407 * tb_xdomain_release_in_hopid() - Release input HopID 2408 * @xd: XDomain connection 2409 * @hopid: HopID to release 2410 */ 2411 void tb_xdomain_release_in_hopid(struct tb_xdomain *xd, int hopid) 2412 { 2413 ida_free(&xd->in_hopids, hopid); 2414 } 2415 EXPORT_SYMBOL_GPL(tb_xdomain_release_in_hopid); 2416 2417 /** 2418 * tb_xdomain_release_out_hopid() - Release output HopID 2419 * @xd: XDomain connection 2420 * @hopid: HopID to release 2421 */ 2422 void tb_xdomain_release_out_hopid(struct tb_xdomain *xd, int hopid) 2423 { 2424 ida_free(&xd->out_hopids, hopid); 2425 } 2426 EXPORT_SYMBOL_GPL(tb_xdomain_release_out_hopid); 2427 2428 /** 2429 * tb_xdomain_enable_paths() - Enable DMA paths for XDomain connection 2430 * @xd: XDomain connection 2431 * @transmit_path: HopID we are using to send out packets 2432 * @transmit_ring: DMA ring used to send out packets 2433 * @receive_path: HopID the other end is using to send packets to us 2434 * @receive_ring: DMA ring used to receive packets from @receive_path 2435 * 2436 * The function enables DMA paths accordingly so that after successful 2437 * return the caller can send and receive packets using high-speed DMA 2438 * path. If a transmit or receive path is not needed, pass %-1 for those 2439 * parameters. 2440 * 2441 * Return: %0 on success, negative errno otherwise. 2442 */ 2443 int tb_xdomain_enable_paths(struct tb_xdomain *xd, int transmit_path, 2444 int transmit_ring, int receive_path, 2445 int receive_ring) 2446 { 2447 int ret; 2448 2449 ret = tb_domain_approve_xdomain_paths(xd->tb, xd, transmit_path, 2450 transmit_ring, receive_path, 2451 receive_ring); 2452 if (ret) 2453 return ret; 2454 atomic_inc(&xd->ntunnels); 2455 return 0; 2456 } 2457 EXPORT_SYMBOL_GPL(tb_xdomain_enable_paths); 2458 2459 /** 2460 * tb_xdomain_disable_paths() - Disable DMA paths for XDomain connection 2461 * @xd: XDomain connection 2462 * @transmit_path: HopID we are using to send out packets 2463 * @transmit_ring: DMA ring used to send out packets 2464 * @receive_path: HopID the other end is using to send packets to us 2465 * @receive_ring: DMA ring used to receive packets from @receive_path 2466 * 2467 * This does the opposite of tb_xdomain_enable_paths(). After call to 2468 * this the caller is not expected to use the rings anymore. Passing %-1 2469 * as path/ring parameter means don't care. Normally the callers should 2470 * pass the same values here as they do when paths are enabled. 2471 * 2472 * Return: %0 on success, negative errno otherwise. 2473 */ 2474 int tb_xdomain_disable_paths(struct tb_xdomain *xd, int transmit_path, 2475 int transmit_ring, int receive_path, 2476 int receive_ring) 2477 { 2478 int ret; 2479 2480 ret = tb_domain_disconnect_xdomain_paths(xd->tb, xd, transmit_path, 2481 transmit_ring, receive_path, 2482 receive_ring); 2483 if (ret) 2484 return ret; 2485 atomic_dec(&xd->ntunnels); 2486 return 0; 2487 } 2488 EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths); 2489 2490 struct tb_xdomain_lookup { 2491 const uuid_t *uuid; 2492 u8 link; 2493 u8 depth; 2494 u64 route; 2495 }; 2496 2497 static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw, 2498 const struct tb_xdomain_lookup *lookup) 2499 { 2500 struct tb_port *port; 2501 2502 if (!sw) 2503 return NULL; 2504 2505 tb_switch_for_each_port(sw, port) { 2506 struct tb_xdomain *xd; 2507 2508 if (port->xdomain) { 2509 xd = port->xdomain; 2510 2511 if (lookup->uuid) { 2512 if (xd->remote_uuid && 2513 uuid_equal(xd->remote_uuid, lookup->uuid)) 2514 return xd; 2515 } else { 2516 if (lookup->link && lookup->link == xd->link && 2517 lookup->depth == xd->depth) 2518 return xd; 2519 if (lookup->route && lookup->route == xd->route) 2520 return xd; 2521 } 2522 } else if (tb_port_has_remote(port)) { 2523 xd = switch_find_xdomain(port->remote->sw, lookup); 2524 if (xd) 2525 return xd; 2526 } 2527 } 2528 2529 return NULL; 2530 } 2531 2532 /** 2533 * tb_xdomain_find_by_uuid() - Find an XDomain by UUID 2534 * @tb: Domain where the XDomain belongs to 2535 * @uuid: UUID to look for 2536 * 2537 * Finds XDomain by walking through the Thunderbolt topology below @tb. 2538 * The returned XDomain will have its reference count increased so the 2539 * caller needs to call tb_xdomain_put() when it is done with the 2540 * object. 2541 * 2542 * This will find all XDomains including the ones that are not yet added 2543 * to the bus (handshake is still in progress). 2544 * 2545 * The caller needs to hold @tb->lock. 2546 * 2547 * Return: Pointer to &struct tb_xdomain or %NULL if not found. 2548 */ 2549 struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid) 2550 { 2551 struct tb_xdomain_lookup lookup; 2552 struct tb_xdomain *xd; 2553 2554 memset(&lookup, 0, sizeof(lookup)); 2555 lookup.uuid = uuid; 2556 2557 xd = switch_find_xdomain(tb->root_switch, &lookup); 2558 return tb_xdomain_get(xd); 2559 } 2560 EXPORT_SYMBOL_GPL(tb_xdomain_find_by_uuid); 2561 2562 /** 2563 * tb_xdomain_find_by_link_depth() - Find an XDomain by link and depth 2564 * @tb: Domain where the XDomain belongs to 2565 * @link: Root switch link number 2566 * @depth: Depth in the link 2567 * 2568 * Finds XDomain by walking through the Thunderbolt topology below @tb. 2569 * The returned XDomain will have its reference count increased so the 2570 * caller needs to call tb_xdomain_put() when it is done with the 2571 * object. 2572 * 2573 * This will find all XDomains including the ones that are not yet added 2574 * to the bus (handshake is still in progress). 2575 * 2576 * The caller needs to hold @tb->lock. 2577 * 2578 * Return: Pointer to &struct tb_xdomain or %NULL if not found. 2579 */ 2580 struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link, 2581 u8 depth) 2582 { 2583 struct tb_xdomain_lookup lookup; 2584 struct tb_xdomain *xd; 2585 2586 memset(&lookup, 0, sizeof(lookup)); 2587 lookup.link = link; 2588 lookup.depth = depth; 2589 2590 xd = switch_find_xdomain(tb->root_switch, &lookup); 2591 return tb_xdomain_get(xd); 2592 } 2593 2594 /** 2595 * tb_xdomain_find_by_route() - Find an XDomain by route string 2596 * @tb: Domain where the XDomain belongs to 2597 * @route: XDomain route string 2598 * 2599 * Finds XDomain by walking through the Thunderbolt topology below @tb. 2600 * The returned XDomain will have its reference count increased so the 2601 * caller needs to call tb_xdomain_put() when it is done with the 2602 * object. 2603 * 2604 * This will find all XDomains including the ones that are not yet added 2605 * to the bus (handshake is still in progress). 2606 * 2607 * The caller needs to hold @tb->lock. 2608 * 2609 * Return: Pointer to &struct tb_xdomain or %NULL if not found. 2610 */ 2611 struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route) 2612 { 2613 struct tb_xdomain_lookup lookup; 2614 struct tb_xdomain *xd; 2615 2616 memset(&lookup, 0, sizeof(lookup)); 2617 lookup.route = route; 2618 2619 xd = switch_find_xdomain(tb->root_switch, &lookup); 2620 return tb_xdomain_get(xd); 2621 } 2622 EXPORT_SYMBOL_GPL(tb_xdomain_find_by_route); 2623 2624 bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type, 2625 const void *buf, size_t size) 2626 { 2627 const struct tb_protocol_handler *handler, *tmp; 2628 const struct tb_xdp_header *hdr = buf; 2629 unsigned int length; 2630 int ret = 0; 2631 2632 /* We expect the packet is at least size of the header */ 2633 length = hdr->xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK; 2634 if (length != size / 4 - sizeof(hdr->xd_hdr) / 4) 2635 return true; 2636 if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4) 2637 return true; 2638 2639 /* 2640 * Handle XDomain discovery protocol packets directly here. For 2641 * other protocols (based on their UUID) we call registered 2642 * handlers in turn. 2643 */ 2644 if (uuid_equal(&hdr->uuid, &tb_xdp_uuid)) { 2645 if (type == TB_CFG_PKG_XDOMAIN_REQ) 2646 return tb_xdp_schedule_request(tb, hdr, size); 2647 return false; 2648 } 2649 2650 mutex_lock(&xdomain_lock); 2651 list_for_each_entry_safe(handler, tmp, &protocol_handlers, list) { 2652 if (!uuid_equal(&hdr->uuid, handler->uuid)) 2653 continue; 2654 2655 mutex_unlock(&xdomain_lock); 2656 ret = handler->callback(buf, size, handler->data); 2657 mutex_lock(&xdomain_lock); 2658 2659 if (ret) 2660 break; 2661 } 2662 mutex_unlock(&xdomain_lock); 2663 2664 return ret > 0; 2665 } 2666 2667 static void update_all_xdomains(void) 2668 { 2669 bus_for_each_dev(&tb_bus_type, NULL, NULL, update_xdomain); 2670 } 2671 2672 static bool remove_directory(const char *key, const struct tb_property_dir *dir) 2673 { 2674 struct tb_property *p; 2675 2676 p = tb_property_find(xdomain_property_dir, key, 2677 TB_PROPERTY_TYPE_DIRECTORY); 2678 if (p && p->value.dir == dir) { 2679 tb_property_remove(p); 2680 return true; 2681 } 2682 return false; 2683 } 2684 2685 /** 2686 * tb_register_property_dir() - Register property directory to the host 2687 * @key: Key (name) of the directory to add 2688 * @dir: Directory to add 2689 * 2690 * Service drivers can use this function to add new property directory 2691 * to the host available properties. The other connected hosts are 2692 * notified so they can re-read properties of this host if they are 2693 * interested. 2694 * 2695 * Return: %0 on success, negative errno otherwise. 2696 */ 2697 int tb_register_property_dir(const char *key, struct tb_property_dir *dir) 2698 { 2699 int ret; 2700 2701 if (WARN_ON(!xdomain_property_dir)) 2702 return -EAGAIN; 2703 2704 if (!key || strlen(key) > 8) 2705 return -EINVAL; 2706 2707 mutex_lock(&xdomain_lock); 2708 if (tb_property_find(xdomain_property_dir, key, 2709 TB_PROPERTY_TYPE_DIRECTORY)) { 2710 ret = -EEXIST; 2711 goto err_unlock; 2712 } 2713 2714 ret = tb_property_add_dir(xdomain_property_dir, key, dir); 2715 if (ret) 2716 goto err_unlock; 2717 2718 xdomain_property_block_gen++; 2719 2720 mutex_unlock(&xdomain_lock); 2721 update_all_xdomains(); 2722 return 0; 2723 2724 err_unlock: 2725 mutex_unlock(&xdomain_lock); 2726 return ret; 2727 } 2728 EXPORT_SYMBOL_GPL(tb_register_property_dir); 2729 2730 /** 2731 * tb_unregister_property_dir() - Removes property directory from host 2732 * @key: Key (name) of the directory 2733 * @dir: Directory to remove 2734 * 2735 * This will remove the existing directory from this host and notify the 2736 * connected hosts about the change. 2737 */ 2738 void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir) 2739 { 2740 int ret = 0; 2741 2742 mutex_lock(&xdomain_lock); 2743 if (remove_directory(key, dir)) 2744 xdomain_property_block_gen++; 2745 mutex_unlock(&xdomain_lock); 2746 2747 if (!ret) 2748 update_all_xdomains(); 2749 } 2750 EXPORT_SYMBOL_GPL(tb_unregister_property_dir); 2751 2752 int tb_xdomain_init(void) 2753 { 2754 xdomain_property_dir = tb_property_create_dir(NULL); 2755 if (!xdomain_property_dir) 2756 return -ENOMEM; 2757 2758 /* 2759 * Initialize standard set of properties without any service 2760 * directories. Those will be added by service drivers 2761 * themselves when they are loaded. 2762 * 2763 * Rest of the properties are filled dynamically based on these 2764 * when the P2P connection is made. 2765 */ 2766 tb_property_add_immediate(xdomain_property_dir, "vendorid", 0x1d6b); 2767 tb_property_add_text(xdomain_property_dir, "vendorid", "Linux"); 2768 tb_property_add_immediate(xdomain_property_dir, "deviceid", 0x0004); 2769 tb_property_add_immediate(xdomain_property_dir, "devicerv", 0x80000100); 2770 2771 xdomain_property_block_gen = get_random_u32(); 2772 return 0; 2773 } 2774 2775 void tb_xdomain_exit(void) 2776 { 2777 tb_property_free_dir(xdomain_property_dir); 2778 } 2779