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 drv->driver.bus = &tb_bus_type; 972 return driver_register(&drv->driver); 973 } 974 EXPORT_SYMBOL_GPL(tb_register_service_driver); 975 976 /** 977 * tb_unregister_service_driver() - Unregister XDomain service driver 978 * @drv: Driver to unregister 979 * 980 * Unregisters XDomain service driver from the bus. 981 */ 982 void tb_unregister_service_driver(struct tb_service_driver *drv) 983 { 984 driver_unregister(&drv->driver); 985 } 986 EXPORT_SYMBOL_GPL(tb_unregister_service_driver); 987 988 static int update_xdomain(struct device *dev, void *data) 989 { 990 struct tb_xdomain *xd; 991 992 xd = tb_to_xdomain(dev); 993 if (xd) { 994 mutex_lock(&xd->lock); 995 if (!xd->removing) 996 queue_delayed_work(xd->tb->wq, 997 &xd->properties_changed_work, 998 msecs_to_jiffies(50)); 999 mutex_unlock(&xd->lock); 1000 } 1001 1002 return 0; 1003 } 1004 1005 /** 1006 * tb_service_properties_changed() - Notify the other host about changes 1007 * @svc: Service whose properties changed 1008 * 1009 * Notifies the other host that service properties may have been 1010 * changed. This should be called whenever @svc->local_properties is 1011 * updated. 1012 */ 1013 void tb_service_properties_changed(struct tb_service *svc) 1014 { 1015 struct tb_xdomain *xd = tb_service_parent(svc); 1016 1017 if (xd->is_unplugged) 1018 return; 1019 1020 scoped_guard(mutex, &xdomain_lock) 1021 xdomain_property_block_gen++; 1022 update_xdomain(&xd->dev, NULL); 1023 } 1024 EXPORT_SYMBOL_GPL(tb_service_properties_changed); 1025 1026 static ssize_t key_show(struct device *dev, struct device_attribute *attr, 1027 char *buf) 1028 { 1029 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1030 1031 /* 1032 * It should be null terminated but anything else is pretty much 1033 * allowed. 1034 */ 1035 return sysfs_emit(buf, "%*pE\n", (int)strlen(svc->key), svc->key); 1036 } 1037 static DEVICE_ATTR_RO(key); 1038 1039 static int get_modalias(const struct tb_service *svc, char *buf, size_t size) 1040 { 1041 return snprintf(buf, size, "tbsvc:k%sp%08Xv%08Xr%08X", svc->key, 1042 svc->prtcid, svc->prtcvers, svc->prtcrevs); 1043 } 1044 1045 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 1046 char *buf) 1047 { 1048 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1049 1050 /* Full buffer size except new line and null termination */ 1051 get_modalias(svc, buf, PAGE_SIZE - 2); 1052 return strlen(strcat(buf, "\n")); 1053 } 1054 static DEVICE_ATTR_RO(modalias); 1055 1056 static ssize_t prtcid_show(struct device *dev, struct device_attribute *attr, 1057 char *buf) 1058 { 1059 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1060 1061 return sysfs_emit(buf, "%u\n", svc->prtcid); 1062 } 1063 static DEVICE_ATTR_RO(prtcid); 1064 1065 static ssize_t prtcvers_show(struct device *dev, struct device_attribute *attr, 1066 char *buf) 1067 { 1068 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1069 1070 return sysfs_emit(buf, "%u\n", svc->prtcvers); 1071 } 1072 static DEVICE_ATTR_RO(prtcvers); 1073 1074 static ssize_t prtcrevs_show(struct device *dev, struct device_attribute *attr, 1075 char *buf) 1076 { 1077 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1078 1079 return sysfs_emit(buf, "%u\n", svc->prtcrevs); 1080 } 1081 static DEVICE_ATTR_RO(prtcrevs); 1082 1083 static ssize_t prtcstns_show(struct device *dev, struct device_attribute *attr, 1084 char *buf) 1085 { 1086 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1087 1088 return sysfs_emit(buf, "0x%08x\n", svc->prtcstns); 1089 } 1090 static DEVICE_ATTR_RO(prtcstns); 1091 1092 static struct attribute *tb_service_attrs[] = { 1093 &dev_attr_key.attr, 1094 &dev_attr_modalias.attr, 1095 &dev_attr_prtcid.attr, 1096 &dev_attr_prtcvers.attr, 1097 &dev_attr_prtcrevs.attr, 1098 &dev_attr_prtcstns.attr, 1099 NULL, 1100 }; 1101 1102 static const struct attribute_group tb_service_attr_group = { 1103 .attrs = tb_service_attrs, 1104 }; 1105 1106 static const struct attribute_group *tb_service_attr_groups[] = { 1107 &tb_service_attr_group, 1108 NULL, 1109 }; 1110 1111 static int tb_service_uevent(const struct device *dev, struct kobj_uevent_env *env) 1112 { 1113 const struct tb_service *svc = container_of_const(dev, struct tb_service, dev); 1114 char modalias[64]; 1115 1116 get_modalias(svc, modalias, sizeof(modalias)); 1117 return add_uevent_var(env, "MODALIAS=%s", modalias); 1118 } 1119 1120 static void tb_service_release(struct device *dev) 1121 { 1122 struct tb_service *svc = container_of(dev, struct tb_service, dev); 1123 struct tb_xdomain *xd = tb_service_parent(svc); 1124 1125 tb_property_free_dir(svc->remote_properties); 1126 ida_free(&xd->service_ids, svc->id); 1127 kfree(svc->key); 1128 kfree(svc); 1129 tb_xdomain_put(xd); 1130 } 1131 1132 const struct device_type tb_service_type = { 1133 .name = "thunderbolt_service", 1134 .groups = tb_service_attr_groups, 1135 .uevent = tb_service_uevent, 1136 .release = tb_service_release, 1137 }; 1138 EXPORT_SYMBOL_GPL(tb_service_type); 1139 1140 static void update_service(struct tb_service *svc, struct tb_property *property) 1141 { 1142 struct tb_property_dir *dir = property->value.dir; 1143 1144 guard(mutex)(&svc->lock); 1145 tb_property_free_dir(svc->remote_properties); 1146 svc->remote_properties = tb_property_copy_dir(dir); 1147 kobject_uevent(&svc->dev.kobj, KOBJ_CHANGE); 1148 } 1149 1150 static void __unregister_service(struct device *dev) 1151 { 1152 struct tb_service *svc = tb_to_service(dev); 1153 1154 tb_service_debugfs_remove(svc); 1155 device_unregister(&svc->dev); 1156 } 1157 1158 static int remove_missing_service(struct device *dev, void *data) 1159 { 1160 struct tb_xdomain *xd = data; 1161 struct tb_service *svc; 1162 1163 svc = tb_to_service(dev); 1164 if (!svc) 1165 return 0; 1166 1167 if (!tb_property_find(xd->remote_properties, svc->key, 1168 TB_PROPERTY_TYPE_DIRECTORY)) 1169 __unregister_service(dev); 1170 1171 return 0; 1172 } 1173 1174 static int find_service(struct device *dev, const void *data) 1175 { 1176 const struct tb_property *p = data; 1177 struct tb_service *svc; 1178 1179 svc = tb_to_service(dev); 1180 if (!svc) 1181 return 0; 1182 1183 return !strcmp(svc->key, p->key); 1184 } 1185 1186 static int populate_service(struct tb_service *svc, 1187 struct tb_property *property) 1188 { 1189 struct tb_property_dir *dir = property->value.dir; 1190 struct tb_property *p; 1191 1192 /* Fill in standard properties */ 1193 p = tb_property_find(dir, "prtcid", TB_PROPERTY_TYPE_VALUE); 1194 if (p) 1195 svc->prtcid = p->value.immediate; 1196 p = tb_property_find(dir, "prtcvers", TB_PROPERTY_TYPE_VALUE); 1197 if (p) 1198 svc->prtcvers = p->value.immediate; 1199 p = tb_property_find(dir, "prtcrevs", TB_PROPERTY_TYPE_VALUE); 1200 if (p) 1201 svc->prtcrevs = p->value.immediate; 1202 p = tb_property_find(dir, "prtcstns", TB_PROPERTY_TYPE_VALUE); 1203 if (p) 1204 svc->prtcstns = p->value.immediate; 1205 1206 svc->key = kstrdup(property->key, GFP_KERNEL); 1207 if (!svc->key) 1208 return -ENOMEM; 1209 1210 svc->remote_properties = tb_property_copy_dir(dir); 1211 if (!svc->remote_properties) { 1212 kfree(svc->key); 1213 return -ENOMEM; 1214 } 1215 1216 return 0; 1217 } 1218 1219 static void enumerate_services(struct tb_xdomain *xd) 1220 { 1221 struct tb_service *svc; 1222 struct tb_property *p; 1223 struct device *dev; 1224 int id; 1225 1226 /* 1227 * First remove all services that are not available anymore in 1228 * the updated property block. 1229 */ 1230 device_for_each_child_reverse(&xd->dev, xd, remove_missing_service); 1231 1232 /* Then re-enumerate properties creating new services as we go */ 1233 tb_property_for_each(xd->remote_properties, p) { 1234 if (p->type != TB_PROPERTY_TYPE_DIRECTORY) 1235 continue; 1236 1237 /* If the service exists already we are fine */ 1238 dev = device_find_child(&xd->dev, p, find_service); 1239 if (dev) { 1240 update_service(tb_to_service(dev), p); 1241 put_device(dev); 1242 continue; 1243 } 1244 1245 svc = kzalloc_obj(*svc); 1246 if (!svc) 1247 break; 1248 1249 if (populate_service(svc, p)) { 1250 kfree(svc); 1251 break; 1252 } 1253 1254 id = ida_alloc(&xd->service_ids, GFP_KERNEL); 1255 if (id < 0) { 1256 kfree(svc->key); 1257 kfree(svc); 1258 break; 1259 } 1260 svc->id = id; 1261 svc->dev.bus = &tb_bus_type; 1262 svc->dev.type = &tb_service_type; 1263 svc->dev.parent = get_device(&xd->dev); 1264 mutex_init(&svc->lock); 1265 dev_set_name(&svc->dev, "%s.%d", dev_name(&xd->dev), svc->id); 1266 1267 tb_service_debugfs_init(svc); 1268 1269 if (device_register(&svc->dev)) { 1270 tb_service_debugfs_remove(svc); 1271 put_device(&svc->dev); 1272 break; 1273 } 1274 } 1275 } 1276 1277 static int populate_properties(struct tb_xdomain *xd, 1278 struct tb_property_dir *dir) 1279 { 1280 const struct tb_property *p; 1281 1282 /* Required properties */ 1283 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_VALUE); 1284 if (!p) 1285 return -EINVAL; 1286 xd->device = p->value.immediate; 1287 1288 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_VALUE); 1289 if (!p) 1290 return -EINVAL; 1291 xd->vendor = p->value.immediate; 1292 1293 p = tb_property_find(dir, "maxhopid", TB_PROPERTY_TYPE_VALUE); 1294 /* 1295 * USB4 inter-domain spec suggests using 15 as HopID if the 1296 * other end does not announce it in a property. This is for 1297 * TBT3 compatibility. 1298 */ 1299 xd->remote_max_hopid = p ? p->value.immediate : XDOMAIN_DEFAULT_MAX_HOPID; 1300 1301 kfree(xd->device_name); 1302 xd->device_name = NULL; 1303 kfree(xd->vendor_name); 1304 xd->vendor_name = NULL; 1305 1306 /* Optional properties */ 1307 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_TEXT); 1308 if (p) 1309 xd->device_name = kstrdup(p->value.text, GFP_KERNEL); 1310 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_TEXT); 1311 if (p) 1312 xd->vendor_name = kstrdup(p->value.text, GFP_KERNEL); 1313 1314 return 0; 1315 } 1316 1317 static int tb_xdomain_update_link_attributes(struct tb_xdomain *xd) 1318 { 1319 bool change = false; 1320 struct tb_port *port; 1321 int ret; 1322 1323 port = tb_xdomain_downstream_port(xd); 1324 1325 ret = tb_port_get_link_speed(port); 1326 if (ret < 0) 1327 return ret; 1328 1329 if (xd->link_speed != ret) 1330 change = true; 1331 1332 xd->link_speed = ret; 1333 1334 ret = tb_port_get_link_width(port); 1335 if (ret < 0) 1336 return ret; 1337 1338 if (xd->link_width != ret) 1339 change = true; 1340 1341 xd->link_width = ret; 1342 1343 if (change) 1344 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE); 1345 1346 return 0; 1347 } 1348 1349 static int tb_xdomain_get_uuid(struct tb_xdomain *xd) 1350 { 1351 struct tb *tb = xd->tb; 1352 uuid_t uuid; 1353 u64 route; 1354 int ret; 1355 1356 dev_dbg(&xd->dev, "requesting remote UUID\n"); 1357 1358 ret = tb_xdp_uuid_request(tb->ctl, xd->route, xd->state_retries, &uuid, 1359 &route); 1360 if (ret < 0) { 1361 if (xd->state_retries-- > 0) { 1362 dev_dbg(&xd->dev, "failed to request UUID, retrying\n"); 1363 return -EAGAIN; 1364 } 1365 dev_dbg(&xd->dev, "failed to read remote UUID\n"); 1366 return ret; 1367 } 1368 1369 dev_dbg(&xd->dev, "got remote UUID %pUb\n", &uuid); 1370 1371 if (uuid_equal(&uuid, xd->local_uuid)) { 1372 if (route == xd->route) 1373 dev_dbg(&xd->dev, "loop back detected\n"); 1374 else 1375 dev_dbg(&xd->dev, "intra-domain loop detected\n"); 1376 1377 /* Don't bond lanes automatically for loops */ 1378 xd->bonding_possible = false; 1379 } 1380 1381 /* 1382 * If the UUID is different, there is another domain connected 1383 * so mark this one unplugged and wait for the connection 1384 * manager to replace it. 1385 */ 1386 if (xd->remote_uuid && !uuid_equal(&uuid, xd->remote_uuid)) { 1387 dev_dbg(&xd->dev, "remote UUID is different, unplugging\n"); 1388 xd->is_unplugged = true; 1389 return -ENODEV; 1390 } 1391 1392 /* First time fill in the missing UUID */ 1393 if (!xd->remote_uuid) { 1394 xd->remote_uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL); 1395 if (!xd->remote_uuid) 1396 return -ENOMEM; 1397 } 1398 1399 return 0; 1400 } 1401 1402 static int tb_xdomain_get_link_status(struct tb_xdomain *xd) 1403 { 1404 struct tb *tb = xd->tb; 1405 u8 slw, tlw, sls, tls; 1406 int ret; 1407 1408 dev_dbg(&xd->dev, "sending link state status request to %pUb\n", 1409 xd->remote_uuid); 1410 1411 ret = tb_xdp_link_state_status_request(tb->ctl, xd->route, 1412 xd->state_retries, &slw, &tlw, &sls, 1413 &tls); 1414 if (ret) { 1415 if (ret != -EOPNOTSUPP && xd->state_retries-- > 0) { 1416 dev_dbg(&xd->dev, 1417 "failed to request remote link status, retrying\n"); 1418 return -EAGAIN; 1419 } 1420 dev_dbg(&xd->dev, "failed to receive remote link status\n"); 1421 return ret; 1422 } 1423 1424 dev_dbg(&xd->dev, "remote link supports width %#x speed %#x\n", slw, sls); 1425 1426 if (slw < LANE_ADP_CS_0_SUPPORTED_WIDTH_DUAL) { 1427 dev_dbg(&xd->dev, "remote adapter is single lane only\n"); 1428 return -EOPNOTSUPP; 1429 } 1430 1431 return 0; 1432 } 1433 1434 static int tb_xdomain_link_state_change(struct tb_xdomain *xd, 1435 unsigned int width) 1436 { 1437 struct tb_port *port = tb_xdomain_downstream_port(xd); 1438 struct tb *tb = xd->tb; 1439 u8 tlw, tls; 1440 u32 val; 1441 int ret; 1442 1443 if (width == 2) 1444 tlw = LANE_ADP_CS_1_TARGET_WIDTH_DUAL; 1445 else if (width == 1) 1446 tlw = LANE_ADP_CS_1_TARGET_WIDTH_SINGLE; 1447 else 1448 return -EINVAL; 1449 1450 /* Use the current target speed */ 1451 ret = tb_port_read(port, &val, TB_CFG_PORT, port->cap_phy + LANE_ADP_CS_1, 1); 1452 if (ret) 1453 return ret; 1454 tls = val & LANE_ADP_CS_1_TARGET_SPEED_MASK; 1455 1456 dev_dbg(&xd->dev, "sending link state change request with width %#x speed %#x\n", 1457 tlw, tls); 1458 1459 ret = tb_xdp_link_state_change_request(tb->ctl, xd->route, 1460 xd->state_retries, tlw, tls); 1461 if (ret) { 1462 if (ret != -EOPNOTSUPP && xd->state_retries-- > 0) { 1463 dev_dbg(&xd->dev, 1464 "failed to change remote link state, retrying\n"); 1465 return -EAGAIN; 1466 } 1467 dev_err(&xd->dev, "failed request link state change, aborting\n"); 1468 return ret; 1469 } 1470 1471 dev_dbg(&xd->dev, "received link state change response\n"); 1472 return 0; 1473 } 1474 1475 static int tb_xdomain_bond_lanes_uuid_high(struct tb_xdomain *xd) 1476 { 1477 unsigned int width, width_mask; 1478 struct tb_port *port; 1479 int ret; 1480 1481 if (xd->target_link_width == LANE_ADP_CS_1_TARGET_WIDTH_SINGLE) { 1482 width = TB_LINK_WIDTH_SINGLE; 1483 width_mask = width; 1484 } else if (xd->target_link_width == LANE_ADP_CS_1_TARGET_WIDTH_DUAL) { 1485 width = TB_LINK_WIDTH_DUAL; 1486 width_mask = width | TB_LINK_WIDTH_ASYM_TX | TB_LINK_WIDTH_ASYM_RX; 1487 } else { 1488 if (xd->state_retries-- > 0) { 1489 dev_dbg(&xd->dev, 1490 "link state change request not received yet, retrying\n"); 1491 return -EAGAIN; 1492 } 1493 dev_dbg(&xd->dev, "timeout waiting for link change request\n"); 1494 return -ETIMEDOUT; 1495 } 1496 1497 port = tb_xdomain_downstream_port(xd); 1498 1499 /* 1500 * We can't use tb_xdomain_lane_bonding_enable() here because it 1501 * is the other side that initiates lane bonding. So here we 1502 * just set the width to both lane adapters and wait for the 1503 * link to transition bonded. 1504 */ 1505 ret = tb_port_set_link_width(port->dual_link_port, width); 1506 if (ret) { 1507 tb_port_warn(port->dual_link_port, 1508 "failed to set link width to %d\n", width); 1509 return ret; 1510 } 1511 1512 ret = tb_port_set_link_width(port, width); 1513 if (ret) { 1514 tb_port_warn(port, "failed to set link width to %d\n", width); 1515 return ret; 1516 } 1517 1518 ret = tb_port_wait_for_link_width(port, width_mask, 1519 XDOMAIN_BONDING_TIMEOUT); 1520 if (ret) { 1521 dev_warn(&xd->dev, "error waiting for link width to become %d\n", 1522 width_mask); 1523 return ret; 1524 } 1525 1526 port->bonded = width > TB_LINK_WIDTH_SINGLE; 1527 port->dual_link_port->bonded = width > TB_LINK_WIDTH_SINGLE; 1528 1529 tb_port_update_credits(port); 1530 tb_xdomain_update_link_attributes(xd); 1531 1532 dev_dbg(&xd->dev, "lane bonding %s\n", str_enabled_disabled(width == 2)); 1533 return 0; 1534 } 1535 1536 static int tb_xdomain_get_properties(struct tb_xdomain *xd) 1537 { 1538 struct tb_property_dir *dir; 1539 struct tb *tb = xd->tb; 1540 bool update = false; 1541 u32 *block = NULL; 1542 u32 gen = 0; 1543 int ret; 1544 1545 dev_dbg(&xd->dev, "requesting remote properties\n"); 1546 1547 ret = tb_xdp_properties_request(tb->ctl, xd->route, xd->local_uuid, 1548 xd->remote_uuid, xd->state_retries, 1549 &block, &gen); 1550 if (ret < 0) { 1551 if (xd->state_retries-- > 0) { 1552 dev_dbg(&xd->dev, 1553 "failed to request remote properties, retrying\n"); 1554 return -EAGAIN; 1555 } 1556 /* Give up now */ 1557 dev_err(&xd->dev, "failed read XDomain properties from %pUb\n", 1558 xd->remote_uuid); 1559 1560 return ret; 1561 } 1562 1563 mutex_lock(&xd->lock); 1564 1565 /* Only accept newer generation properties */ 1566 if (xd->remote_properties && gen <= xd->remote_property_block_gen) { 1567 ret = 0; 1568 goto err_free_block; 1569 } 1570 1571 dir = tb_property_parse_dir(block, ret); 1572 if (!dir) { 1573 dev_err(&xd->dev, "failed to parse XDomain properties\n"); 1574 ret = -ENOMEM; 1575 goto err_free_block; 1576 } 1577 1578 ret = populate_properties(xd, dir); 1579 if (ret) { 1580 dev_err(&xd->dev, "missing XDomain properties in response\n"); 1581 goto err_free_dir; 1582 } 1583 1584 /* Release the existing one */ 1585 if (xd->remote_properties) { 1586 tb_property_free_dir(xd->remote_properties); 1587 update = true; 1588 } 1589 1590 xd->remote_properties = dir; 1591 xd->remote_property_block_gen = gen; 1592 1593 tb_xdomain_update_link_attributes(xd); 1594 1595 mutex_unlock(&xd->lock); 1596 1597 kfree(block); 1598 1599 /* 1600 * Now the device should be ready enough so we can add it to the 1601 * bus and let userspace know about it. If the device is already 1602 * registered, we notify the userspace that it has changed. 1603 */ 1604 if (!update) { 1605 /* 1606 * Now disable lane 1 if bonding was not enabled. Do 1607 * this only if bonding was possible at the beginning 1608 * (that is we are the connection manager and there are 1609 * two lanes). 1610 */ 1611 if (xd->bonding_possible) { 1612 struct tb_port *port; 1613 1614 port = tb_xdomain_downstream_port(xd); 1615 if (!port->bonded) 1616 tb_port_disable(port->dual_link_port); 1617 } 1618 1619 dev_dbg(&xd->dev, "current link speed %u.0 Gb/s\n", 1620 xd->link_speed); 1621 dev_dbg(&xd->dev, "current link width %s\n", 1622 tb_width_name(xd->link_width)); 1623 1624 if (device_add(&xd->dev)) { 1625 dev_err(&xd->dev, "failed to add XDomain device\n"); 1626 return -ENODEV; 1627 } 1628 dev_info(&xd->dev, "new host found, vendor=%#x device=%#x\n", 1629 xd->vendor, xd->device); 1630 if (xd->vendor_name && xd->device_name) 1631 dev_info(&xd->dev, "%s %s\n", xd->vendor_name, 1632 xd->device_name); 1633 1634 tb_xdomain_debugfs_init(xd); 1635 } else { 1636 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE); 1637 } 1638 1639 enumerate_services(xd); 1640 return 0; 1641 1642 err_free_dir: 1643 tb_property_free_dir(dir); 1644 err_free_block: 1645 kfree(block); 1646 mutex_unlock(&xd->lock); 1647 1648 return ret; 1649 } 1650 1651 static void tb_xdomain_queue_uuid(struct tb_xdomain *xd) 1652 { 1653 xd->state = XDOMAIN_STATE_UUID; 1654 xd->state_retries = XDOMAIN_RETRIES; 1655 queue_delayed_work(xd->tb->wq, &xd->state_work, 1656 msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT)); 1657 } 1658 1659 static void tb_xdomain_queue_link_status(struct tb_xdomain *xd) 1660 { 1661 xd->state = XDOMAIN_STATE_LINK_STATUS; 1662 xd->state_retries = XDOMAIN_RETRIES; 1663 queue_delayed_work(xd->tb->wq, &xd->state_work, 1664 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1665 } 1666 1667 static void tb_xdomain_queue_link_status2(struct tb_xdomain *xd) 1668 { 1669 xd->state = XDOMAIN_STATE_LINK_STATUS2; 1670 xd->state_retries = XDOMAIN_RETRIES; 1671 queue_delayed_work(xd->tb->wq, &xd->state_work, 1672 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1673 } 1674 1675 static void tb_xdomain_queue_bonding(struct tb_xdomain *xd) 1676 { 1677 if (memcmp(xd->local_uuid, xd->remote_uuid, UUID_SIZE) > 0) { 1678 dev_dbg(&xd->dev, "we have higher UUID, other side bonds the lanes\n"); 1679 xd->state = XDOMAIN_STATE_BONDING_UUID_HIGH; 1680 } else { 1681 dev_dbg(&xd->dev, "we have lower UUID, bonding lanes\n"); 1682 xd->state = XDOMAIN_STATE_LINK_STATE_CHANGE; 1683 } 1684 1685 xd->state_retries = XDOMAIN_RETRIES; 1686 queue_delayed_work(xd->tb->wq, &xd->state_work, 1687 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1688 } 1689 1690 static void tb_xdomain_queue_bonding_uuid_low(struct tb_xdomain *xd) 1691 { 1692 xd->state = XDOMAIN_STATE_BONDING_UUID_LOW; 1693 xd->state_retries = XDOMAIN_RETRIES; 1694 queue_delayed_work(xd->tb->wq, &xd->state_work, 1695 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1696 } 1697 1698 static void tb_xdomain_queue_properties(struct tb_xdomain *xd) 1699 { 1700 xd->state = XDOMAIN_STATE_PROPERTIES; 1701 xd->state_retries = XDOMAIN_RETRIES; 1702 queue_delayed_work(xd->tb->wq, &xd->state_work, 1703 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1704 } 1705 1706 static void tb_xdomain_queue_properties_changed(struct tb_xdomain *xd) 1707 { 1708 xd->properties_changed_retries = XDOMAIN_RETRIES; 1709 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work, 1710 msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT)); 1711 } 1712 1713 static void tb_xdomain_failed(struct tb_xdomain *xd) 1714 { 1715 xd->state = XDOMAIN_STATE_ERROR; 1716 queue_delayed_work(xd->tb->wq, &xd->state_work, 1717 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1718 } 1719 1720 static void tb_xdomain_state_work(struct work_struct *work) 1721 { 1722 struct tb_xdomain *xd = container_of(work, typeof(*xd), state_work.work); 1723 int ret, state = xd->state; 1724 1725 if (WARN_ON_ONCE(state < XDOMAIN_STATE_INIT || 1726 state > XDOMAIN_STATE_ERROR)) 1727 return; 1728 1729 dev_dbg(&xd->dev, "running state %s\n", state_names[state]); 1730 1731 switch (state) { 1732 case XDOMAIN_STATE_INIT: 1733 if (xd->needs_uuid) { 1734 tb_xdomain_queue_uuid(xd); 1735 } else { 1736 tb_xdomain_queue_properties_changed(xd); 1737 tb_xdomain_queue_properties(xd); 1738 } 1739 break; 1740 1741 case XDOMAIN_STATE_UUID: 1742 ret = tb_xdomain_get_uuid(xd); 1743 if (ret) { 1744 if (ret == -EAGAIN) 1745 goto retry_state; 1746 tb_xdomain_failed(xd); 1747 } else { 1748 tb_xdomain_queue_properties_changed(xd); 1749 if (xd->bonding_possible) 1750 tb_xdomain_queue_link_status(xd); 1751 else 1752 tb_xdomain_queue_properties(xd); 1753 } 1754 break; 1755 1756 case XDOMAIN_STATE_LINK_STATUS: 1757 ret = tb_xdomain_get_link_status(xd); 1758 if (ret) { 1759 if (ret == -EAGAIN) 1760 goto retry_state; 1761 1762 /* 1763 * If any of the lane bonding states fail we skip 1764 * bonding completely and try to continue from 1765 * reading properties. 1766 */ 1767 tb_xdomain_queue_properties(xd); 1768 } else { 1769 tb_xdomain_queue_bonding(xd); 1770 } 1771 break; 1772 1773 case XDOMAIN_STATE_LINK_STATE_CHANGE: 1774 ret = tb_xdomain_link_state_change(xd, 2); 1775 if (ret) { 1776 if (ret == -EAGAIN) 1777 goto retry_state; 1778 tb_xdomain_queue_properties(xd); 1779 } else { 1780 tb_xdomain_queue_link_status2(xd); 1781 } 1782 break; 1783 1784 case XDOMAIN_STATE_LINK_STATUS2: 1785 ret = tb_xdomain_get_link_status(xd); 1786 if (ret) { 1787 if (ret == -EAGAIN) 1788 goto retry_state; 1789 tb_xdomain_queue_properties(xd); 1790 } else { 1791 tb_xdomain_queue_bonding_uuid_low(xd); 1792 } 1793 break; 1794 1795 case XDOMAIN_STATE_BONDING_UUID_LOW: 1796 tb_xdomain_lane_bonding_enable(xd); 1797 tb_xdomain_queue_properties(xd); 1798 break; 1799 1800 case XDOMAIN_STATE_BONDING_UUID_HIGH: 1801 if (tb_xdomain_bond_lanes_uuid_high(xd) == -EAGAIN) 1802 goto retry_state; 1803 tb_xdomain_queue_properties(xd); 1804 break; 1805 1806 case XDOMAIN_STATE_PROPERTIES: 1807 ret = tb_xdomain_get_properties(xd); 1808 if (ret) { 1809 if (ret == -EAGAIN) 1810 goto retry_state; 1811 tb_xdomain_failed(xd); 1812 } else { 1813 xd->state = XDOMAIN_STATE_ENUMERATED; 1814 } 1815 break; 1816 1817 case XDOMAIN_STATE_ENUMERATED: 1818 tb_xdomain_queue_properties(xd); 1819 break; 1820 1821 case XDOMAIN_STATE_ERROR: 1822 dev_dbg(&xd->dev, "discovery failed, stopping handshake\n"); 1823 __stop_handshake(xd); 1824 break; 1825 1826 default: 1827 dev_warn(&xd->dev, "unexpected state %d\n", state); 1828 break; 1829 } 1830 1831 return; 1832 1833 retry_state: 1834 queue_delayed_work(xd->tb->wq, &xd->state_work, 1835 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1836 } 1837 1838 static void tb_xdomain_properties_changed(struct work_struct *work) 1839 { 1840 struct tb_xdomain *xd = container_of(work, typeof(*xd), 1841 properties_changed_work.work); 1842 int ret; 1843 1844 dev_dbg(&xd->dev, "sending properties changed notification\n"); 1845 1846 ret = tb_xdp_properties_changed_request(xd->tb->ctl, xd->route, 1847 xd->properties_changed_retries, xd->local_uuid); 1848 if (ret) { 1849 if (xd->properties_changed_retries-- > 0) { 1850 dev_dbg(&xd->dev, 1851 "failed to send properties changed notification, retrying\n"); 1852 queue_delayed_work(xd->tb->wq, 1853 &xd->properties_changed_work, 1854 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT)); 1855 } 1856 dev_err(&xd->dev, "failed to send properties changed notification\n"); 1857 return; 1858 } 1859 1860 xd->properties_changed_retries = XDOMAIN_RETRIES; 1861 } 1862 1863 static ssize_t device_show(struct device *dev, struct device_attribute *attr, 1864 char *buf) 1865 { 1866 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1867 1868 return sysfs_emit(buf, "%#x\n", xd->device); 1869 } 1870 static DEVICE_ATTR_RO(device); 1871 1872 static ssize_t 1873 device_name_show(struct device *dev, struct device_attribute *attr, char *buf) 1874 { 1875 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1876 int ret; 1877 1878 if (mutex_lock_interruptible(&xd->lock)) 1879 return -ERESTARTSYS; 1880 ret = sysfs_emit(buf, "%s\n", xd->device_name ?: ""); 1881 mutex_unlock(&xd->lock); 1882 1883 return ret; 1884 } 1885 static DEVICE_ATTR_RO(device_name); 1886 1887 static ssize_t maxhopid_show(struct device *dev, struct device_attribute *attr, 1888 char *buf) 1889 { 1890 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1891 1892 return sysfs_emit(buf, "%d\n", xd->remote_max_hopid); 1893 } 1894 static DEVICE_ATTR_RO(maxhopid); 1895 1896 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr, 1897 char *buf) 1898 { 1899 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1900 1901 return sysfs_emit(buf, "%#x\n", xd->vendor); 1902 } 1903 static DEVICE_ATTR_RO(vendor); 1904 1905 static ssize_t 1906 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf) 1907 { 1908 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1909 int ret; 1910 1911 if (mutex_lock_interruptible(&xd->lock)) 1912 return -ERESTARTSYS; 1913 ret = sysfs_emit(buf, "%s\n", xd->vendor_name ?: ""); 1914 mutex_unlock(&xd->lock); 1915 1916 return ret; 1917 } 1918 static DEVICE_ATTR_RO(vendor_name); 1919 1920 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr, 1921 char *buf) 1922 { 1923 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1924 1925 return sysfs_emit(buf, "%pUb\n", xd->remote_uuid); 1926 } 1927 static DEVICE_ATTR_RO(unique_id); 1928 1929 static ssize_t speed_show(struct device *dev, struct device_attribute *attr, 1930 char *buf) 1931 { 1932 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1933 1934 return sysfs_emit(buf, "%u.0 Gb/s\n", xd->link_speed); 1935 } 1936 1937 static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL); 1938 static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL); 1939 1940 static ssize_t rx_lanes_show(struct device *dev, struct device_attribute *attr, 1941 char *buf) 1942 { 1943 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1944 unsigned int width; 1945 1946 switch (xd->link_width) { 1947 case TB_LINK_WIDTH_SINGLE: 1948 case TB_LINK_WIDTH_ASYM_TX: 1949 width = 1; 1950 break; 1951 case TB_LINK_WIDTH_DUAL: 1952 width = 2; 1953 break; 1954 case TB_LINK_WIDTH_ASYM_RX: 1955 width = 3; 1956 break; 1957 default: 1958 WARN_ON_ONCE(1); 1959 return -EINVAL; 1960 } 1961 1962 return sysfs_emit(buf, "%u\n", width); 1963 } 1964 static DEVICE_ATTR(rx_lanes, 0444, rx_lanes_show, NULL); 1965 1966 static ssize_t tx_lanes_show(struct device *dev, struct device_attribute *attr, 1967 char *buf) 1968 { 1969 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 1970 unsigned int width; 1971 1972 switch (xd->link_width) { 1973 case TB_LINK_WIDTH_SINGLE: 1974 case TB_LINK_WIDTH_ASYM_RX: 1975 width = 1; 1976 break; 1977 case TB_LINK_WIDTH_DUAL: 1978 width = 2; 1979 break; 1980 case TB_LINK_WIDTH_ASYM_TX: 1981 width = 3; 1982 break; 1983 default: 1984 WARN_ON_ONCE(1); 1985 return -EINVAL; 1986 } 1987 1988 return sysfs_emit(buf, "%u\n", width); 1989 } 1990 static DEVICE_ATTR(tx_lanes, 0444, tx_lanes_show, NULL); 1991 1992 static struct attribute *xdomain_attrs[] = { 1993 &dev_attr_device.attr, 1994 &dev_attr_device_name.attr, 1995 &dev_attr_maxhopid.attr, 1996 &dev_attr_rx_lanes.attr, 1997 &dev_attr_rx_speed.attr, 1998 &dev_attr_tx_lanes.attr, 1999 &dev_attr_tx_speed.attr, 2000 &dev_attr_unique_id.attr, 2001 &dev_attr_vendor.attr, 2002 &dev_attr_vendor_name.attr, 2003 NULL, 2004 }; 2005 2006 static const struct attribute_group xdomain_attr_group = { 2007 .attrs = xdomain_attrs, 2008 }; 2009 2010 static const struct attribute_group *xdomain_attr_groups[] = { 2011 &xdomain_attr_group, 2012 NULL, 2013 }; 2014 2015 static void tb_xdomain_release(struct device *dev) 2016 { 2017 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev); 2018 2019 put_device(xd->dev.parent); 2020 2021 kfree(xd->local_property_block); 2022 tb_property_free_dir(xd->remote_properties); 2023 ida_destroy(&xd->out_hopids); 2024 ida_destroy(&xd->in_hopids); 2025 ida_destroy(&xd->service_ids); 2026 2027 kfree(xd->local_uuid); 2028 kfree(xd->remote_uuid); 2029 kfree(xd->device_name); 2030 kfree(xd->vendor_name); 2031 kfree(xd); 2032 } 2033 2034 static int __maybe_unused tb_xdomain_suspend(struct device *dev) 2035 { 2036 stop_handshake(tb_to_xdomain(dev)); 2037 return 0; 2038 } 2039 2040 static int __maybe_unused tb_xdomain_resume(struct device *dev) 2041 { 2042 start_handshake(tb_to_xdomain(dev)); 2043 return 0; 2044 } 2045 2046 static const struct dev_pm_ops tb_xdomain_pm_ops = { 2047 SET_SYSTEM_SLEEP_PM_OPS(tb_xdomain_suspend, tb_xdomain_resume) 2048 }; 2049 2050 const struct device_type tb_xdomain_type = { 2051 .name = "thunderbolt_xdomain", 2052 .release = tb_xdomain_release, 2053 .pm = &tb_xdomain_pm_ops, 2054 }; 2055 EXPORT_SYMBOL_GPL(tb_xdomain_type); 2056 2057 static void tb_xdomain_link_init(struct tb_xdomain *xd, struct tb_port *down) 2058 { 2059 if (!down->dual_link_port) 2060 return; 2061 2062 /* 2063 * Gen 4 links come up already as bonded so only update the port 2064 * structures here. 2065 */ 2066 if (tb_port_get_link_generation(down) >= 4) { 2067 down->bonded = true; 2068 down->dual_link_port->bonded = true; 2069 } else { 2070 xd->bonding_possible = true; 2071 } 2072 } 2073 2074 static void tb_xdomain_link_exit(struct tb_xdomain *xd) 2075 { 2076 struct tb_port *down = tb_xdomain_downstream_port(xd); 2077 2078 if (!down->dual_link_port) 2079 return; 2080 2081 if (tb_port_get_link_generation(down) >= 4) { 2082 down->bonded = false; 2083 down->dual_link_port->bonded = false; 2084 return; 2085 } 2086 2087 if (!xd->bonding_possible) 2088 return; 2089 2090 if (xd->link_width > TB_LINK_WIDTH_SINGLE) { 2091 /* 2092 * Just return port structures back to way they were and 2093 * update credits. No need to update userspace because 2094 * the XDomain is removed soon anyway. 2095 */ 2096 tb_port_lane_bonding_disable(down); 2097 tb_port_update_credits(down); 2098 } else if (down->dual_link_port) { 2099 /* 2100 * Re-enable the lane 1 adapter we disabled at the end 2101 * of tb_xdomain_get_properties(). 2102 */ 2103 tb_port_enable(down->dual_link_port); 2104 } 2105 } 2106 2107 /** 2108 * tb_xdomain_alloc() - Allocate new XDomain object 2109 * @tb: Domain where the XDomain belongs 2110 * @parent: Parent device (the switch through which the other domain 2111 * is reached). 2112 * @route: Route string used to reach the other domain 2113 * @local_uuid: Our local domain UUID 2114 * @remote_uuid: UUID of the other domain (optional) 2115 * 2116 * Allocates new XDomain structure and returns pointer to that. The 2117 * object must be released by calling tb_xdomain_put(). 2118 * 2119 * Return: Pointer to &struct tb_xdomain, %NULL in case of failure. 2120 */ 2121 struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent, 2122 u64 route, const uuid_t *local_uuid, 2123 const uuid_t *remote_uuid) 2124 { 2125 struct tb_switch *parent_sw = tb_to_switch(parent); 2126 struct tb_xdomain *xd; 2127 struct tb_port *down; 2128 2129 /* Make sure the downstream domain is accessible */ 2130 down = tb_port_at(route, parent_sw); 2131 tb_port_unlock(down); 2132 2133 xd = kzalloc_obj(*xd); 2134 if (!xd) 2135 return NULL; 2136 2137 xd->tb = tb; 2138 xd->route = route; 2139 xd->local_max_hopid = down->config.max_in_hop_id; 2140 ida_init(&xd->service_ids); 2141 ida_init(&xd->in_hopids); 2142 ida_init(&xd->out_hopids); 2143 mutex_init(&xd->lock); 2144 INIT_DELAYED_WORK(&xd->state_work, tb_xdomain_state_work); 2145 INIT_DELAYED_WORK(&xd->properties_changed_work, 2146 tb_xdomain_properties_changed); 2147 atomic_set(&xd->ntunnels, 0); 2148 2149 xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL); 2150 if (!xd->local_uuid) 2151 goto err_free; 2152 2153 if (remote_uuid) { 2154 xd->remote_uuid = kmemdup(remote_uuid, sizeof(uuid_t), 2155 GFP_KERNEL); 2156 if (!xd->remote_uuid) 2157 goto err_free_local_uuid; 2158 } else { 2159 xd->needs_uuid = true; 2160 2161 tb_xdomain_link_init(xd, down); 2162 } 2163 2164 device_initialize(&xd->dev); 2165 xd->dev.parent = get_device(parent); 2166 xd->dev.bus = &tb_bus_type; 2167 xd->dev.type = &tb_xdomain_type; 2168 xd->dev.groups = xdomain_attr_groups; 2169 dev_set_name(&xd->dev, "%u-%llx", tb->index, route); 2170 2171 dev_dbg(&xd->dev, "local UUID %pUb\n", local_uuid); 2172 if (remote_uuid) 2173 dev_dbg(&xd->dev, "remote UUID %pUb\n", remote_uuid); 2174 2175 /* 2176 * This keeps the DMA powered on as long as we have active 2177 * connection to another host. 2178 */ 2179 pm_runtime_set_active(&xd->dev); 2180 pm_runtime_get_noresume(&xd->dev); 2181 pm_runtime_enable(&xd->dev); 2182 2183 return xd; 2184 2185 err_free_local_uuid: 2186 kfree(xd->local_uuid); 2187 err_free: 2188 kfree(xd); 2189 2190 return NULL; 2191 } 2192 2193 /** 2194 * tb_xdomain_add() - Add XDomain to the bus 2195 * @xd: XDomain to add 2196 * 2197 * This function starts XDomain discovery protocol handshake and 2198 * eventually adds the XDomain to the bus. After calling this function 2199 * the caller needs to call tb_xdomain_remove() in order to remove and 2200 * release the object regardless whether the handshake succeeded or not. 2201 */ 2202 void tb_xdomain_add(struct tb_xdomain *xd) 2203 { 2204 /* Start exchanging properties with the other host */ 2205 start_handshake(xd); 2206 } 2207 2208 static int unregister_service(struct device *dev, void *data) 2209 { 2210 __unregister_service(dev); 2211 return 0; 2212 } 2213 2214 /** 2215 * tb_xdomain_remove() - Remove XDomain 2216 * @xd: XDomain to remove 2217 * 2218 * This will stop all ongoing configuration work. XDomain is not removed 2219 * from the bus if it was added. That needs to be done separately by 2220 * calling tb_xdomain_unregister(). 2221 * 2222 * Called with @tb->lock held. 2223 */ 2224 void tb_xdomain_remove(struct tb_xdomain *xd) 2225 { 2226 tb_xdomain_debugfs_remove(xd); 2227 2228 mutex_lock(&xd->lock); 2229 xd->removing = true; 2230 mutex_unlock(&xd->lock); 2231 2232 stop_handshake(xd); 2233 tb_xdomain_link_exit(xd); 2234 2235 if (!device_is_registered(&xd->dev)) { 2236 /* 2237 * Undo runtime PM here explicitly because it is 2238 * possible that the XDomain was never added to the bus 2239 * and thus device_del() is not called for it 2240 * (device_del() would handle this otherwise). 2241 */ 2242 pm_runtime_disable(&xd->dev); 2243 pm_runtime_put_noidle(&xd->dev); 2244 pm_runtime_set_suspended(&xd->dev); 2245 put_device(&xd->dev); 2246 } 2247 } 2248 2249 /** 2250 * tb_xdomain_unregister() - Unregister XDomain 2251 * @xd: XDomain to unregister 2252 * 2253 * This will unregister the XDomain along with any services from the 2254 * bus. When the last reference to @xd is released the object will be 2255 * released as well. 2256 */ 2257 void tb_xdomain_unregister(struct tb_xdomain *xd) 2258 { 2259 lockdep_assert_not_held(&xd->tb->lock); 2260 2261 device_for_each_child_reverse(&xd->dev, xd, unregister_service); 2262 2263 dev_info(&xd->dev, "host disconnected\n"); 2264 device_unregister(&xd->dev); 2265 } 2266 2267 /** 2268 * tb_xdomain_lane_bonding_enable() - Enable lane bonding on XDomain 2269 * @xd: XDomain connection 2270 * 2271 * Lane bonding is disabled by default for XDomains. This function tries 2272 * to enable bonding by first enabling the port and waiting for the CL0 2273 * state. 2274 * 2275 * Return: %0 on success, negative errno otherwise. 2276 */ 2277 int tb_xdomain_lane_bonding_enable(struct tb_xdomain *xd) 2278 { 2279 unsigned int width_mask; 2280 struct tb_port *port; 2281 int ret; 2282 2283 port = tb_xdomain_downstream_port(xd); 2284 if (!port->dual_link_port) 2285 return -ENODEV; 2286 2287 ret = tb_port_enable(port->dual_link_port); 2288 if (ret) 2289 return ret; 2290 2291 ret = tb_wait_for_port(port->dual_link_port, true); 2292 if (ret < 0) 2293 return ret; 2294 if (!ret) 2295 return -ENOTCONN; 2296 2297 ret = tb_port_lane_bonding_enable(port); 2298 if (ret) { 2299 tb_port_warn(port, "failed to enable lane bonding\n"); 2300 return ret; 2301 } 2302 2303 /* Any of the widths are all bonded */ 2304 width_mask = TB_LINK_WIDTH_DUAL | TB_LINK_WIDTH_ASYM_TX | 2305 TB_LINK_WIDTH_ASYM_RX; 2306 2307 ret = tb_port_wait_for_link_width(port, width_mask, 2308 XDOMAIN_BONDING_TIMEOUT); 2309 if (ret) { 2310 tb_port_warn(port, "failed to enable lane bonding\n"); 2311 return ret; 2312 } 2313 2314 tb_port_update_credits(port); 2315 tb_xdomain_update_link_attributes(xd); 2316 2317 dev_dbg(&xd->dev, "lane bonding enabled\n"); 2318 return 0; 2319 } 2320 EXPORT_SYMBOL_GPL(tb_xdomain_lane_bonding_enable); 2321 2322 /** 2323 * tb_xdomain_lane_bonding_disable() - Disable lane bonding 2324 * @xd: XDomain connection 2325 * 2326 * Lane bonding is disabled by default for XDomains. If bonding has been 2327 * enabled, this function can be used to disable it. 2328 */ 2329 void tb_xdomain_lane_bonding_disable(struct tb_xdomain *xd) 2330 { 2331 struct tb_port *port; 2332 2333 port = tb_xdomain_downstream_port(xd); 2334 if (port->dual_link_port) { 2335 int ret; 2336 2337 tb_port_lane_bonding_disable(port); 2338 ret = tb_port_wait_for_link_width(port, TB_LINK_WIDTH_SINGLE, 100); 2339 if (ret == -ETIMEDOUT) 2340 tb_port_warn(port, "timeout disabling lane bonding\n"); 2341 tb_port_disable(port->dual_link_port); 2342 tb_port_update_credits(port); 2343 tb_xdomain_update_link_attributes(xd); 2344 2345 dev_dbg(&xd->dev, "lane bonding disabled\n"); 2346 } 2347 } 2348 EXPORT_SYMBOL_GPL(tb_xdomain_lane_bonding_disable); 2349 2350 /** 2351 * tb_xdomain_alloc_in_hopid() - Allocate input HopID for tunneling 2352 * @xd: XDomain connection 2353 * @hopid: Preferred HopID or %-1 for next available 2354 * 2355 * Returned HopID is guaranteed to be within range supported by the input 2356 * lane adapter. 2357 * Call tb_xdomain_release_in_hopid() to release the allocated HopID. 2358 * 2359 * Return: 2360 * * Allocated HopID - On success. 2361 * * %-ENOSPC - If there are no more available HopIDs. 2362 * * Negative errno - Another error occurred. 2363 */ 2364 int tb_xdomain_alloc_in_hopid(struct tb_xdomain *xd, int hopid) 2365 { 2366 if (hopid < 0) 2367 hopid = TB_PATH_MIN_HOPID; 2368 if (hopid < TB_PATH_MIN_HOPID || hopid > xd->local_max_hopid) 2369 return -EINVAL; 2370 2371 return ida_alloc_range(&xd->in_hopids, hopid, xd->local_max_hopid, 2372 GFP_KERNEL); 2373 } 2374 EXPORT_SYMBOL_GPL(tb_xdomain_alloc_in_hopid); 2375 2376 /** 2377 * tb_xdomain_alloc_out_hopid() - Allocate output HopID for tunneling 2378 * @xd: XDomain connection 2379 * @hopid: Preferred HopID or %-1 for next available 2380 * 2381 * Returned HopID is guaranteed to be within range supported by the 2382 * output lane adapter. 2383 * Call tb_xdomain_release_out_hopid() to release the allocated HopID. 2384 * 2385 * Return: 2386 * * Allocated HopID - On success. 2387 * * %-ENOSPC - If there are no more available HopIDs. 2388 * * Negative errno - Another error occurred. 2389 */ 2390 int tb_xdomain_alloc_out_hopid(struct tb_xdomain *xd, int hopid) 2391 { 2392 if (hopid < 0) 2393 hopid = TB_PATH_MIN_HOPID; 2394 if (hopid < TB_PATH_MIN_HOPID || hopid > xd->remote_max_hopid) 2395 return -EINVAL; 2396 2397 return ida_alloc_range(&xd->out_hopids, hopid, xd->remote_max_hopid, 2398 GFP_KERNEL); 2399 } 2400 EXPORT_SYMBOL_GPL(tb_xdomain_alloc_out_hopid); 2401 2402 /** 2403 * tb_xdomain_release_in_hopid() - Release input HopID 2404 * @xd: XDomain connection 2405 * @hopid: HopID to release 2406 */ 2407 void tb_xdomain_release_in_hopid(struct tb_xdomain *xd, int hopid) 2408 { 2409 ida_free(&xd->in_hopids, hopid); 2410 } 2411 EXPORT_SYMBOL_GPL(tb_xdomain_release_in_hopid); 2412 2413 /** 2414 * tb_xdomain_release_out_hopid() - Release output HopID 2415 * @xd: XDomain connection 2416 * @hopid: HopID to release 2417 */ 2418 void tb_xdomain_release_out_hopid(struct tb_xdomain *xd, int hopid) 2419 { 2420 ida_free(&xd->out_hopids, hopid); 2421 } 2422 EXPORT_SYMBOL_GPL(tb_xdomain_release_out_hopid); 2423 2424 /** 2425 * tb_xdomain_enable_paths() - Enable DMA paths for XDomain connection 2426 * @xd: XDomain connection 2427 * @transmit_path: HopID we are using to send out packets 2428 * @transmit_ring: DMA ring used to send out packets 2429 * @receive_path: HopID the other end is using to send packets to us 2430 * @receive_ring: DMA ring used to receive packets from @receive_path 2431 * 2432 * The function enables DMA paths accordingly so that after successful 2433 * return the caller can send and receive packets using high-speed DMA 2434 * path. If a transmit or receive path is not needed, pass %-1 for those 2435 * parameters. 2436 * 2437 * Return: %0 on success, negative errno otherwise. 2438 */ 2439 int tb_xdomain_enable_paths(struct tb_xdomain *xd, int transmit_path, 2440 int transmit_ring, int receive_path, 2441 int receive_ring) 2442 { 2443 int ret; 2444 2445 ret = tb_domain_approve_xdomain_paths(xd->tb, xd, transmit_path, 2446 transmit_ring, receive_path, 2447 receive_ring); 2448 if (ret) 2449 return ret; 2450 atomic_inc(&xd->ntunnels); 2451 return 0; 2452 } 2453 EXPORT_SYMBOL_GPL(tb_xdomain_enable_paths); 2454 2455 /** 2456 * tb_xdomain_disable_paths() - Disable DMA paths for XDomain connection 2457 * @xd: XDomain connection 2458 * @transmit_path: HopID we are using to send out packets 2459 * @transmit_ring: DMA ring used to send out packets 2460 * @receive_path: HopID the other end is using to send packets to us 2461 * @receive_ring: DMA ring used to receive packets from @receive_path 2462 * 2463 * This does the opposite of tb_xdomain_enable_paths(). After call to 2464 * this the caller is not expected to use the rings anymore. Passing %-1 2465 * as path/ring parameter means don't care. Normally the callers should 2466 * pass the same values here as they do when paths are enabled. 2467 * 2468 * Return: %0 on success, negative errno otherwise. 2469 */ 2470 int tb_xdomain_disable_paths(struct tb_xdomain *xd, int transmit_path, 2471 int transmit_ring, int receive_path, 2472 int receive_ring) 2473 { 2474 int ret; 2475 2476 ret = tb_domain_disconnect_xdomain_paths(xd->tb, xd, transmit_path, 2477 transmit_ring, receive_path, 2478 receive_ring); 2479 if (ret) 2480 return ret; 2481 atomic_dec(&xd->ntunnels); 2482 return 0; 2483 } 2484 EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths); 2485 2486 struct tb_xdomain_lookup { 2487 const uuid_t *uuid; 2488 u8 link; 2489 u8 depth; 2490 u64 route; 2491 }; 2492 2493 static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw, 2494 const struct tb_xdomain_lookup *lookup) 2495 { 2496 struct tb_port *port; 2497 2498 if (!sw) 2499 return NULL; 2500 2501 tb_switch_for_each_port(sw, port) { 2502 struct tb_xdomain *xd; 2503 2504 if (port->xdomain) { 2505 xd = port->xdomain; 2506 2507 if (lookup->uuid) { 2508 if (xd->remote_uuid && 2509 uuid_equal(xd->remote_uuid, lookup->uuid)) 2510 return xd; 2511 } else { 2512 if (lookup->link && lookup->link == xd->link && 2513 lookup->depth == xd->depth) 2514 return xd; 2515 if (lookup->route && lookup->route == xd->route) 2516 return xd; 2517 } 2518 } else if (tb_port_has_remote(port)) { 2519 xd = switch_find_xdomain(port->remote->sw, lookup); 2520 if (xd) 2521 return xd; 2522 } 2523 } 2524 2525 return NULL; 2526 } 2527 2528 /** 2529 * tb_xdomain_find_by_uuid() - Find an XDomain by UUID 2530 * @tb: Domain where the XDomain belongs to 2531 * @uuid: UUID to look for 2532 * 2533 * Finds XDomain by walking through the Thunderbolt topology below @tb. 2534 * The returned XDomain will have its reference count increased so the 2535 * caller needs to call tb_xdomain_put() when it is done with the 2536 * object. 2537 * 2538 * This will find all XDomains including the ones that are not yet added 2539 * to the bus (handshake is still in progress). 2540 * 2541 * The caller needs to hold @tb->lock. 2542 * 2543 * Return: Pointer to &struct tb_xdomain or %NULL if not found. 2544 */ 2545 struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid) 2546 { 2547 struct tb_xdomain_lookup lookup; 2548 struct tb_xdomain *xd; 2549 2550 memset(&lookup, 0, sizeof(lookup)); 2551 lookup.uuid = uuid; 2552 2553 xd = switch_find_xdomain(tb->root_switch, &lookup); 2554 return tb_xdomain_get(xd); 2555 } 2556 EXPORT_SYMBOL_GPL(tb_xdomain_find_by_uuid); 2557 2558 /** 2559 * tb_xdomain_find_by_link_depth() - Find an XDomain by link and depth 2560 * @tb: Domain where the XDomain belongs to 2561 * @link: Root switch link number 2562 * @depth: Depth in the link 2563 * 2564 * Finds XDomain by walking through the Thunderbolt topology below @tb. 2565 * The returned XDomain will have its reference count increased so the 2566 * caller needs to call tb_xdomain_put() when it is done with the 2567 * object. 2568 * 2569 * This will find all XDomains including the ones that are not yet added 2570 * to the bus (handshake is still in progress). 2571 * 2572 * The caller needs to hold @tb->lock. 2573 * 2574 * Return: Pointer to &struct tb_xdomain or %NULL if not found. 2575 */ 2576 struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link, 2577 u8 depth) 2578 { 2579 struct tb_xdomain_lookup lookup; 2580 struct tb_xdomain *xd; 2581 2582 memset(&lookup, 0, sizeof(lookup)); 2583 lookup.link = link; 2584 lookup.depth = depth; 2585 2586 xd = switch_find_xdomain(tb->root_switch, &lookup); 2587 return tb_xdomain_get(xd); 2588 } 2589 2590 /** 2591 * tb_xdomain_find_by_route() - Find an XDomain by route string 2592 * @tb: Domain where the XDomain belongs to 2593 * @route: XDomain route string 2594 * 2595 * Finds XDomain by walking through the Thunderbolt topology below @tb. 2596 * The returned XDomain will have its reference count increased so the 2597 * caller needs to call tb_xdomain_put() when it is done with the 2598 * object. 2599 * 2600 * This will find all XDomains including the ones that are not yet added 2601 * to the bus (handshake is still in progress). 2602 * 2603 * The caller needs to hold @tb->lock. 2604 * 2605 * Return: Pointer to &struct tb_xdomain or %NULL if not found. 2606 */ 2607 struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route) 2608 { 2609 struct tb_xdomain_lookup lookup; 2610 struct tb_xdomain *xd; 2611 2612 memset(&lookup, 0, sizeof(lookup)); 2613 lookup.route = route; 2614 2615 xd = switch_find_xdomain(tb->root_switch, &lookup); 2616 return tb_xdomain_get(xd); 2617 } 2618 EXPORT_SYMBOL_GPL(tb_xdomain_find_by_route); 2619 2620 bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type, 2621 const void *buf, size_t size) 2622 { 2623 const struct tb_protocol_handler *handler, *tmp; 2624 const struct tb_xdp_header *hdr = buf; 2625 unsigned int length; 2626 int ret = 0; 2627 2628 /* We expect the packet is at least size of the header */ 2629 length = hdr->xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK; 2630 if (length != size / 4 - sizeof(hdr->xd_hdr) / 4) 2631 return true; 2632 if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4) 2633 return true; 2634 2635 /* 2636 * Handle XDomain discovery protocol packets directly here. For 2637 * other protocols (based on their UUID) we call registered 2638 * handlers in turn. 2639 */ 2640 if (uuid_equal(&hdr->uuid, &tb_xdp_uuid)) { 2641 if (type == TB_CFG_PKG_XDOMAIN_REQ) 2642 return tb_xdp_schedule_request(tb, hdr, size); 2643 return false; 2644 } 2645 2646 mutex_lock(&xdomain_lock); 2647 list_for_each_entry_safe(handler, tmp, &protocol_handlers, list) { 2648 if (!uuid_equal(&hdr->uuid, handler->uuid)) 2649 continue; 2650 2651 mutex_unlock(&xdomain_lock); 2652 ret = handler->callback(buf, size, handler->data); 2653 mutex_lock(&xdomain_lock); 2654 2655 if (ret) 2656 break; 2657 } 2658 mutex_unlock(&xdomain_lock); 2659 2660 return ret > 0; 2661 } 2662 2663 static void update_all_xdomains(void) 2664 { 2665 bus_for_each_dev(&tb_bus_type, NULL, NULL, update_xdomain); 2666 } 2667 2668 static bool remove_directory(const char *key, const struct tb_property_dir *dir) 2669 { 2670 struct tb_property *p; 2671 2672 p = tb_property_find(xdomain_property_dir, key, 2673 TB_PROPERTY_TYPE_DIRECTORY); 2674 if (p && p->value.dir == dir) { 2675 tb_property_remove(p); 2676 return true; 2677 } 2678 return false; 2679 } 2680 2681 /** 2682 * tb_register_property_dir() - Register property directory to the host 2683 * @key: Key (name) of the directory to add 2684 * @dir: Directory to add 2685 * 2686 * Service drivers can use this function to add new property directory 2687 * to the host available properties. The other connected hosts are 2688 * notified so they can re-read properties of this host if they are 2689 * interested. 2690 * 2691 * Return: %0 on success, negative errno otherwise. 2692 */ 2693 int tb_register_property_dir(const char *key, struct tb_property_dir *dir) 2694 { 2695 int ret; 2696 2697 if (WARN_ON(!xdomain_property_dir)) 2698 return -EAGAIN; 2699 2700 if (!key || strlen(key) > 8) 2701 return -EINVAL; 2702 2703 mutex_lock(&xdomain_lock); 2704 if (tb_property_find(xdomain_property_dir, key, 2705 TB_PROPERTY_TYPE_DIRECTORY)) { 2706 ret = -EEXIST; 2707 goto err_unlock; 2708 } 2709 2710 ret = tb_property_add_dir(xdomain_property_dir, key, dir); 2711 if (ret) 2712 goto err_unlock; 2713 2714 xdomain_property_block_gen++; 2715 2716 mutex_unlock(&xdomain_lock); 2717 update_all_xdomains(); 2718 return 0; 2719 2720 err_unlock: 2721 mutex_unlock(&xdomain_lock); 2722 return ret; 2723 } 2724 EXPORT_SYMBOL_GPL(tb_register_property_dir); 2725 2726 /** 2727 * tb_unregister_property_dir() - Removes property directory from host 2728 * @key: Key (name) of the directory 2729 * @dir: Directory to remove 2730 * 2731 * This will remove the existing directory from this host and notify the 2732 * connected hosts about the change. 2733 */ 2734 void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir) 2735 { 2736 int ret = 0; 2737 2738 mutex_lock(&xdomain_lock); 2739 if (remove_directory(key, dir)) 2740 xdomain_property_block_gen++; 2741 mutex_unlock(&xdomain_lock); 2742 2743 if (!ret) 2744 update_all_xdomains(); 2745 } 2746 EXPORT_SYMBOL_GPL(tb_unregister_property_dir); 2747 2748 int tb_xdomain_init(void) 2749 { 2750 xdomain_property_dir = tb_property_create_dir(NULL); 2751 if (!xdomain_property_dir) 2752 return -ENOMEM; 2753 2754 /* 2755 * Initialize standard set of properties without any service 2756 * directories. Those will be added by service drivers 2757 * themselves when they are loaded. 2758 * 2759 * Rest of the properties are filled dynamically based on these 2760 * when the P2P connection is made. 2761 */ 2762 tb_property_add_immediate(xdomain_property_dir, "vendorid", 0x1d6b); 2763 tb_property_add_text(xdomain_property_dir, "vendorid", "Linux"); 2764 tb_property_add_immediate(xdomain_property_dir, "deviceid", 0x0004); 2765 tb_property_add_immediate(xdomain_property_dir, "devicerv", 0x80000100); 2766 2767 xdomain_property_block_gen = get_random_u32(); 2768 return 0; 2769 } 2770 2771 void tb_xdomain_exit(void) 2772 { 2773 tb_property_free_dir(xdomain_property_dir); 2774 } 2775