Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

thunderbolt: Factor common ICM add and update operations out

The newer ICM will not use link and depth to address devices. Instead it
uses route strings. In order to take advantage of the existing code
factor out common operations so that we can use the same functions with
the new ICM as well.

No functional changes intended.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>

+87 -49
+87 -49
drivers/thunderbolt/icm.c
··· 374 374 return 0; 375 375 } 376 376 377 + static void add_switch(struct tb_switch *parent_sw, u64 route, 378 + const uuid_t *uuid, u8 connection_id, u8 connection_key, 379 + u8 link, u8 depth, enum tb_security_level security_level, 380 + bool authorized) 381 + { 382 + struct tb_switch *sw; 383 + 384 + sw = tb_switch_alloc(parent_sw->tb, &parent_sw->dev, route); 385 + if (!sw) 386 + return; 387 + 388 + sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL); 389 + sw->connection_id = connection_id; 390 + sw->connection_key = connection_key; 391 + sw->link = link; 392 + sw->depth = depth; 393 + sw->authorized = authorized; 394 + sw->security_level = security_level; 395 + 396 + /* Link the two switches now */ 397 + tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw); 398 + tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw); 399 + 400 + if (tb_switch_add(sw)) { 401 + tb_port_at(tb_route(sw), parent_sw)->remote = NULL; 402 + tb_switch_put(sw); 403 + return; 404 + } 405 + } 406 + 407 + static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw, 408 + u64 route, u8 connection_id, u8 connection_key, 409 + u8 link, u8 depth) 410 + { 411 + /* Disconnect from parent */ 412 + tb_port_at(tb_route(sw), parent_sw)->remote = NULL; 413 + /* Re-connect via updated port*/ 414 + tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw); 415 + 416 + /* Update with the new addressing information */ 417 + sw->config.route_hi = upper_32_bits(route); 418 + sw->config.route_lo = lower_32_bits(route); 419 + sw->connection_id = connection_id; 420 + sw->connection_key = connection_key; 421 + sw->link = link; 422 + sw->depth = depth; 423 + 424 + /* This switch still exists */ 425 + sw->is_unplugged = false; 426 + } 427 + 377 428 static void remove_switch(struct tb_switch *sw) 378 429 { 379 430 struct tb_switch *parent_sw; ··· 432 381 parent_sw = tb_to_switch(sw->dev.parent); 433 382 tb_port_at(tb_route(sw), parent_sw)->remote = NULL; 434 383 tb_switch_remove(sw); 384 + } 385 + 386 + static void add_xdomain(struct tb_switch *sw, u64 route, 387 + const uuid_t *local_uuid, const uuid_t *remote_uuid, 388 + u8 link, u8 depth) 389 + { 390 + struct tb_xdomain *xd; 391 + 392 + xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid); 393 + if (!xd) 394 + return; 395 + 396 + xd->link = link; 397 + xd->depth = depth; 398 + 399 + tb_port_at(route, sw)->xdomain = xd; 400 + 401 + tb_xdomain_add(xd); 402 + } 403 + 404 + static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link) 405 + { 406 + xd->link = link; 407 + xd->route = route; 408 + xd->is_unplugged = false; 435 409 } 436 410 437 411 static void remove_xdomain(struct tb_xdomain *xd) ··· 473 397 { 474 398 const struct icm_fr_event_device_connected *pkg = 475 399 (const struct icm_fr_event_device_connected *)hdr; 400 + enum tb_security_level security_level; 476 401 struct tb_switch *sw, *parent_sw; 477 402 struct icm *icm = tb_priv(tb); 478 403 bool authorized = false; ··· 486 409 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >> 487 410 ICM_LINK_INFO_DEPTH_SHIFT; 488 411 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED; 412 + security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >> 413 + ICM_FLAGS_SLEVEL_SHIFT; 489 414 490 415 if (pkg->link_info & ICM_LINK_INFO_REJECTED) { 491 416 tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n", ··· 520 441 */ 521 442 if (sw->depth == depth && sw_phy_port == phy_port && 522 443 !!sw->authorized == authorized) { 523 - tb_port_at(tb_route(sw), parent_sw)->remote = NULL; 524 - tb_port_at(route, parent_sw)->remote = 525 - tb_upstream_port(sw); 526 - sw->config.route_hi = upper_32_bits(route); 527 - sw->config.route_lo = lower_32_bits(route); 528 - sw->connection_id = pkg->connection_id; 529 - sw->connection_key = pkg->connection_key; 530 - sw->link = link; 531 - sw->depth = depth; 532 - sw->is_unplugged = false; 444 + update_switch(parent_sw, sw, route, pkg->connection_id, 445 + pkg->connection_key, link, depth); 533 446 tb_switch_put(sw); 534 447 return; 535 448 } ··· 568 497 return; 569 498 } 570 499 571 - sw = tb_switch_alloc(tb, &parent_sw->dev, route); 572 - if (!sw) { 573 - tb_switch_put(parent_sw); 574 - return; 575 - } 500 + add_switch(parent_sw, route, &pkg->ep_uuid, pkg->connection_id, 501 + pkg->connection_key, link, depth, security_level, 502 + authorized); 576 503 577 - sw->uuid = kmemdup(&pkg->ep_uuid, sizeof(pkg->ep_uuid), GFP_KERNEL); 578 - sw->connection_id = pkg->connection_id; 579 - sw->connection_key = pkg->connection_key; 580 - sw->link = link; 581 - sw->depth = depth; 582 - sw->authorized = authorized; 583 - sw->security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >> 584 - ICM_FLAGS_SLEVEL_SHIFT; 585 - 586 - /* Link the two switches now */ 587 - tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw); 588 - tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw); 589 - 590 - ret = tb_switch_add(sw); 591 - if (ret) { 592 - tb_port_at(tb_route(sw), parent_sw)->remote = NULL; 593 - tb_switch_put(sw); 594 - } 595 504 tb_switch_put(parent_sw); 596 505 } 597 506 ··· 642 591 phy_port = phy_port_from_route(route, depth); 643 592 644 593 if (xd->depth == depth && xd_phy_port == phy_port) { 645 - xd->link = link; 646 - xd->route = route; 647 - xd->is_unplugged = false; 594 + update_xdomain(xd, route, link); 648 595 tb_xdomain_put(xd); 649 596 return; 650 597 } ··· 692 643 return; 693 644 } 694 645 695 - xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, 696 - &pkg->local_uuid, &pkg->remote_uuid); 697 - if (!xd) { 698 - tb_switch_put(sw); 699 - return; 700 - } 701 - 702 - xd->link = link; 703 - xd->depth = depth; 704 - 705 - tb_port_at(route, sw)->xdomain = xd; 706 - 707 - tb_xdomain_add(xd); 646 + add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link, 647 + depth); 708 648 tb_switch_put(sw); 709 649 } 710 650