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