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

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'thunderbolt-for-v6.14-rc1' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git/westeri/thunderbolt into usb-next

Pull Thunderbolt updates from Mika:

thunderbolt: Changes for v6.14 merge window

This includes following USB4/Thunderbolt changes for the v6.14 merge
window:

- Add debugfs write capability to path config spaces.
- Expose router DROM through debugfs.
- Increase DPRX capabilities read timeout to support runtime
suspending of graphics driver.
- Handle DisplayPort tunnel activation asynchronously.
- Small cleanups.

All these have been in linux-next with no reported issues.

* tag 'thunderbolt-for-v6.14-rc1' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git/westeri/thunderbolt:
thunderbolt: Expose router DROM through debugfs
thunderbolt: Handle DisplayPort tunnel activation asynchronously
thunderbolt: Rework tb_tunnel_consumed_bandwidth()
thunderbolt: Move forward declarations in one place
thunderbolt: Pass reason to tb_dp_resource_unavailable()
thunderbolt: Drop tb_tunnel_restart()
thunderbolt: Rework how tunnel->[init|deinit] hooks are called
thunderbolt: Show path name in debug log when path is deactivated
thunderbolt: Make tb_tunnel_one_dp() return void
thunderbolt: Increase DPRX capabilities read timeout
thunderbolt: Debug log an invalid config space reply just once
thunderbolt: Log config space when invalid config space reply is received
thunderbolt: Drop doubled empty line from ctl.h
thunderbolt: debugfs: Add write capability to path config space

+601 -320
+7 -4
drivers/thunderbolt/ctl.c
··· 70 70 #define tb_ctl_dbg(ctl, format, arg...) \ 71 71 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg) 72 72 73 + #define tb_ctl_dbg_once(ctl, format, arg...) \ 74 + dev_dbg_once(&(ctl)->nhi->pdev->dev, format, ## arg) 75 + 73 76 static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue); 74 77 /* Serializes access to request kref_get/put */ 75 78 static DEFINE_MUTEX(tb_cfg_request_lock); ··· 268 265 return res; 269 266 } 270 267 271 - static void tb_cfg_print_error(struct tb_ctl *ctl, 268 + static void tb_cfg_print_error(struct tb_ctl *ctl, enum tb_cfg_space space, 272 269 const struct tb_cfg_result *res) 273 270 { 274 271 WARN_ON(res->err != 1); ··· 282 279 * Invalid cfg_space/offset/length combination in 283 280 * cfg_read/cfg_write. 284 281 */ 285 - tb_ctl_dbg(ctl, "%llx:%x: invalid config space or offset\n", 286 - res->response_route, res->response_port); 282 + tb_ctl_dbg_once(ctl, "%llx:%x: invalid config space (%u) or offset\n", 283 + res->response_route, res->response_port, space); 287 284 return; 288 285 case TB_CFG_ERROR_NO_SUCH_PORT: 289 286 /* ··· 1075 1072 res->tb_error == TB_CFG_ERROR_INVALID_CONFIG_SPACE) 1076 1073 return -ENODEV; 1077 1074 1078 - tb_cfg_print_error(ctl, res); 1075 + tb_cfg_print_error(ctl, space, res); 1079 1076 1080 1077 if (res->tb_error == TB_CFG_ERROR_LOCK) 1081 1078 return -EACCES;
-1
drivers/thunderbolt/ctl.h
··· 140 140 enum tb_cfg_space space, u32 offset, u32 length); 141 141 int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route); 142 142 143 - 144 143 #endif
+58 -11
drivers/thunderbolt/debugfs.c
··· 168 168 * offset relative_offset cap_id vs_cap_id value\n 169 169 * v[0] v[1] v[2] v[3] v[4] 170 170 * 171 + * For Path configuration space: 172 + * Short format is: offset value\n 173 + * v[0] v[1] 174 + * Long format as produced from the read side: 175 + * offset relative_offset in_hop_id value\n 176 + * v[0] v[1] v[2] v[3] 177 + * 171 178 * For Counter configuration space: 172 179 * Short format is: offset\n 173 180 * v[0] ··· 198 191 } 199 192 200 193 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE) 201 - static ssize_t regs_write(struct tb_switch *sw, struct tb_port *port, 202 - const char __user *user_buf, size_t count, 203 - loff_t *ppos) 194 + /* 195 + * Path registers need to be written in double word pairs and they both must be 196 + * read before written. This writes one double word in patch config space 197 + * following the spec flow. 198 + */ 199 + static int path_write_one(struct tb_port *port, u32 val, u32 offset) 204 200 { 201 + u32 index = offset % PATH_LEN; 202 + u32 offs = offset - index; 203 + u32 data[PATH_LEN]; 204 + int ret; 205 + 206 + ret = tb_port_read(port, data, TB_CFG_HOPS, offs, PATH_LEN); 207 + if (ret) 208 + return ret; 209 + data[index] = val; 210 + return tb_port_write(port, data, TB_CFG_HOPS, offs, PATH_LEN); 211 + } 212 + 213 + static ssize_t regs_write(struct tb_switch *sw, struct tb_port *port, 214 + enum tb_cfg_space space, const char __user *user_buf, 215 + size_t count, loff_t *ppos) 216 + { 217 + int long_fmt_len, ret = 0; 205 218 struct tb *tb = sw->tb; 206 219 char *line, *buf; 207 220 u32 val, offset; 208 - int ret = 0; 209 221 210 222 buf = validate_and_copy_from_user(user_buf, &count); 211 223 if (IS_ERR(buf)) ··· 240 214 /* User did hardware changes behind the driver's back */ 241 215 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 242 216 217 + if (space == TB_CFG_HOPS) 218 + long_fmt_len = 4; 219 + else 220 + long_fmt_len = 5; 221 + 243 222 line = buf; 244 - while (parse_line(&line, &offset, &val, 2, 5)) { 245 - if (port) 246 - ret = tb_port_write(port, &val, TB_CFG_PORT, offset, 1); 247 - else 223 + while (parse_line(&line, &offset, &val, 2, long_fmt_len)) { 224 + if (port) { 225 + if (space == TB_CFG_HOPS) 226 + ret = path_write_one(port, val, offset); 227 + else 228 + ret = tb_port_write(port, &val, space, offset, 1); 229 + } else { 248 230 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1); 231 + } 249 232 if (ret) 250 233 break; 251 234 } ··· 275 240 struct seq_file *s = file->private_data; 276 241 struct tb_port *port = s->private; 277 242 278 - return regs_write(port->sw, port, user_buf, count, ppos); 243 + return regs_write(port->sw, port, TB_CFG_PORT, user_buf, count, ppos); 244 + } 245 + 246 + static ssize_t path_write(struct file *file, const char __user *user_buf, 247 + size_t count, loff_t *ppos) 248 + { 249 + struct seq_file *s = file->private_data; 250 + struct tb_port *port = s->private; 251 + 252 + return regs_write(port->sw, port, TB_CFG_HOPS, user_buf, count, ppos); 279 253 } 280 254 281 255 static ssize_t switch_regs_write(struct file *file, const char __user *user_buf, ··· 293 249 struct seq_file *s = file->private_data; 294 250 struct tb_switch *sw = s->private; 295 251 296 - return regs_write(sw, NULL, user_buf, count, ppos); 252 + return regs_write(sw, NULL, TB_CFG_SWITCH, user_buf, count, ppos); 297 253 } 298 254 299 255 static bool parse_sb_line(char **line, u8 *reg, u8 *data, size_t data_size, ··· 445 401 #define DEBUGFS_MODE 0600 446 402 #else 447 403 #define port_regs_write NULL 404 + #define path_write NULL 448 405 #define switch_regs_write NULL 449 406 #define port_sb_regs_write NULL 450 407 #define retimer_sb_regs_write NULL ··· 2288 2243 2289 2244 return ret; 2290 2245 } 2291 - DEBUGFS_ATTR_RO(path); 2246 + DEBUGFS_ATTR_RW(path); 2292 2247 2293 2248 static int counter_set_regs_show(struct tb_port *port, struct seq_file *s, 2294 2249 int counter) ··· 2413 2368 sw->debugfs_dir = debugfs_dir; 2414 2369 debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, sw, 2415 2370 &switch_regs_fops); 2371 + if (sw->drom) 2372 + debugfs_create_blob("drom", 0400, debugfs_dir, &sw->drom_blob); 2416 2373 2417 2374 tb_switch_for_each_port(sw, port) { 2418 2375 struct dentry *debugfs_dir;
+44 -34
drivers/thunderbolt/eeprom.c
··· 435 435 return 0; 436 436 } 437 437 438 + static int tb_switch_drom_alloc(struct tb_switch *sw, size_t size) 439 + { 440 + sw->drom = kzalloc(size, GFP_KERNEL); 441 + if (!sw->drom) 442 + return -ENOMEM; 443 + 444 + #ifdef CONFIG_DEBUG_FS 445 + sw->drom_blob.data = sw->drom; 446 + sw->drom_blob.size = size; 447 + #endif 448 + return 0; 449 + } 450 + 451 + static void tb_switch_drom_free(struct tb_switch *sw) 452 + { 453 + #ifdef CONFIG_DEBUG_FS 454 + sw->drom_blob.data = NULL; 455 + sw->drom_blob.size = 0; 456 + #endif 457 + kfree(sw->drom); 458 + sw->drom = NULL; 459 + } 460 + 438 461 /* 439 462 * tb_drom_copy_efi - copy drom supplied by EFI to sw->drom if present 440 463 */ ··· 470 447 if (len < 0 || len < sizeof(struct tb_drom_header)) 471 448 return -EINVAL; 472 449 473 - sw->drom = kmalloc(len, GFP_KERNEL); 474 - if (!sw->drom) 475 - return -ENOMEM; 450 + res = tb_switch_drom_alloc(sw, len); 451 + if (res) 452 + return res; 476 453 477 454 res = device_property_read_u8_array(dev, "ThunderboltDROM", sw->drom, 478 455 len); ··· 487 464 return 0; 488 465 489 466 err: 490 - kfree(sw->drom); 491 - sw->drom = NULL; 467 + tb_switch_drom_free(sw); 492 468 return -EINVAL; 493 469 } 494 470 ··· 513 491 514 492 /* Size includes CRC8 + UID + CRC32 */ 515 493 *size += 1 + 8 + 4; 516 - sw->drom = kzalloc(*size, GFP_KERNEL); 517 - if (!sw->drom) 518 - return -ENOMEM; 494 + ret = tb_switch_drom_alloc(sw, *size); 495 + if (ret) 496 + return ret; 519 497 520 498 ret = dma_port_flash_read(sw->dma_port, drom_offset, sw->drom, *size); 521 - if (ret) 522 - goto err_free; 499 + if (ret) { 500 + tb_switch_drom_free(sw); 501 + return ret; 502 + } 523 503 524 504 /* 525 505 * Read UID from the minimal DROM because the one in NVM is just ··· 529 505 */ 530 506 tb_drom_read_uid_only(sw, &sw->uid); 531 507 return 0; 532 - 533 - err_free: 534 - kfree(sw->drom); 535 - sw->drom = NULL; 536 - return ret; 537 508 } 538 509 539 510 static int usb4_copy_drom(struct tb_switch *sw, u16 *size) ··· 541 522 542 523 /* Size includes CRC8 + UID + CRC32 */ 543 524 *size += 1 + 8 + 4; 544 - sw->drom = kzalloc(*size, GFP_KERNEL); 545 - if (!sw->drom) 546 - return -ENOMEM; 525 + ret = tb_switch_drom_alloc(sw, *size); 526 + if (ret) 527 + return ret; 547 528 548 529 ret = usb4_switch_drom_read(sw, 0, sw->drom, *size); 549 - if (ret) { 550 - kfree(sw->drom); 551 - sw->drom = NULL; 552 - } 530 + if (ret) 531 + tb_switch_drom_free(sw); 553 532 554 533 return ret; 555 534 } ··· 569 552 return -EIO; 570 553 } 571 554 572 - sw->drom = kzalloc(*size, GFP_KERNEL); 573 - if (!sw->drom) 574 - return -ENOMEM; 555 + ret = tb_switch_drom_alloc(sw, *size); 556 + if (ret) 557 + return ret; 575 558 576 559 ret = tb_eeprom_read_n(sw, 0, sw->drom, *size); 577 560 if (ret) 578 - goto err; 561 + tb_switch_drom_free(sw); 579 562 580 - return 0; 581 - 582 - err: 583 - kfree(sw->drom); 584 - sw->drom = NULL; 585 563 return ret; 586 564 } 587 565 ··· 658 646 return 0; 659 647 660 648 err: 661 - kfree(sw->drom); 662 - sw->drom = NULL; 663 - 649 + tb_switch_drom_free(sw); 664 650 return ret; 665 651 } 666 652
+2 -2
drivers/thunderbolt/path.c
··· 581 581 } 582 582 } 583 583 path->activated = true; 584 - tb_dbg(path->tb, "path activation complete\n"); 584 + tb_dbg(path->tb, "%s path activation complete\n", path->name); 585 585 return 0; 586 586 err: 587 - tb_WARN(path->tb, "path activation failed\n"); 587 + tb_WARN(path->tb, "%s path activation failed\n", path->name); 588 588 return res; 589 589 } 590 590
+137 -59
drivers/thunderbolt/tb.c
··· 20 20 #define TB_RELEASE_BW_TIMEOUT 10000 /* ms */ 21 21 22 22 /* 23 + * How many time bandwidth allocation request from graphics driver is 24 + * retried if the DP tunnel is still activating. 25 + */ 26 + #define TB_BW_ALLOC_RETRIES 3 27 + 28 + /* 23 29 * Minimum bandwidth (in Mb/s) that is needed in the single transmitter/receiver 24 30 * direction. This is 40G - 10% guard band bandwidth. 25 31 */ ··· 75 69 } 76 70 77 71 struct tb_hotplug_event { 78 - struct work_struct work; 72 + struct delayed_work work; 79 73 struct tb *tb; 80 74 u64 route; 81 75 u8 port; 82 76 bool unplug; 77 + int retry; 83 78 }; 84 79 80 + static void tb_scan_port(struct tb_port *port); 85 81 static void tb_handle_hotplug(struct work_struct *work); 82 + static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port, 83 + const char *reason); 84 + static void tb_queue_dp_bandwidth_request(struct tb *tb, u64 route, u8 port, 85 + int retry, unsigned long delay); 86 86 87 87 static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug) 88 88 { ··· 102 90 ev->route = route; 103 91 ev->port = port; 104 92 ev->unplug = unplug; 105 - INIT_WORK(&ev->work, tb_handle_hotplug); 106 - queue_work(tb->wq, &ev->work); 93 + INIT_DELAYED_WORK(&ev->work, tb_handle_hotplug); 94 + queue_delayed_work(tb->wq, &ev->work, 0); 107 95 } 108 96 109 97 /* enumeration & hot plug handling */ ··· 973 961 return 0; 974 962 975 963 err_free: 976 - tb_tunnel_free(tunnel); 964 + tb_tunnel_put(tunnel); 977 965 err_reclaim: 978 966 if (tb_route(parent)) 979 967 tb_reclaim_usb3_bandwidth(tb, down, up); ··· 1249 1237 /* Set the link configured */ 1250 1238 tb_switch_configure_link(sw); 1251 1239 } 1252 - 1253 - static void tb_scan_port(struct tb_port *port); 1254 1240 1255 1241 /* 1256 1242 * tb_scan_switch() - scan for and initialize downstream switches ··· 1737 1727 break; 1738 1728 } 1739 1729 1740 - tb_tunnel_free(tunnel); 1730 + tb_tunnel_put(tunnel); 1741 1731 } 1742 1732 1743 1733 /* ··· 1874 1864 return NULL; 1875 1865 } 1876 1866 1877 - static bool tb_tunnel_one_dp(struct tb *tb, struct tb_port *in, 1867 + static void tb_dp_tunnel_active(struct tb_tunnel *tunnel, void *data) 1868 + { 1869 + struct tb_port *in = tunnel->src_port; 1870 + struct tb_port *out = tunnel->dst_port; 1871 + struct tb *tb = data; 1872 + 1873 + mutex_lock(&tb->lock); 1874 + if (tb_tunnel_is_active(tunnel)) { 1875 + int consumed_up, consumed_down, ret; 1876 + 1877 + tb_tunnel_dbg(tunnel, "DPRX capabilities read completed\n"); 1878 + 1879 + /* If fail reading tunnel's consumed bandwidth, tear it down */ 1880 + ret = tb_tunnel_consumed_bandwidth(tunnel, &consumed_up, 1881 + &consumed_down); 1882 + if (ret) { 1883 + tb_tunnel_warn(tunnel, 1884 + "failed to read consumed bandwidth, tearing down\n"); 1885 + tb_deactivate_and_free_tunnel(tunnel); 1886 + } else { 1887 + tb_reclaim_usb3_bandwidth(tb, in, out); 1888 + /* 1889 + * Transition the links to asymmetric if the 1890 + * consumption exceeds the threshold. 1891 + */ 1892 + tb_configure_asym(tb, in, out, consumed_up, 1893 + consumed_down); 1894 + /* 1895 + * Update the domain with the new bandwidth 1896 + * estimation. 1897 + */ 1898 + tb_recalc_estimated_bandwidth(tb); 1899 + /* 1900 + * In case of DP tunnel exists, change host 1901 + * router's 1st children TMU mode to HiFi for 1902 + * CL0s to work. 1903 + */ 1904 + tb_increase_tmu_accuracy(tunnel); 1905 + } 1906 + } else { 1907 + struct tb_port *in = tunnel->src_port; 1908 + 1909 + /* 1910 + * This tunnel failed to establish. This means DPRX 1911 + * negotiation most likely did not complete which 1912 + * happens either because there is no graphics driver 1913 + * loaded or not all DP cables where connected to the 1914 + * discrete router. 1915 + * 1916 + * In both cases we remove the DP IN adapter from the 1917 + * available resources as it is not usable. This will 1918 + * also tear down the tunnel and try to re-use the 1919 + * released DP OUT. 1920 + * 1921 + * It will be added back only if there is hotplug for 1922 + * the DP IN again. 1923 + */ 1924 + tb_tunnel_warn(tunnel, "not active, tearing down\n"); 1925 + tb_dp_resource_unavailable(tb, in, "DPRX negotiation failed"); 1926 + } 1927 + mutex_unlock(&tb->lock); 1928 + 1929 + tb_domain_put(tb); 1930 + } 1931 + 1932 + static void tb_tunnel_one_dp(struct tb *tb, struct tb_port *in, 1878 1933 struct tb_port *out) 1879 1934 { 1880 1935 int available_up, available_down, ret, link_nr; 1881 1936 struct tb_cm *tcm = tb_priv(tb); 1882 - int consumed_up, consumed_down; 1883 1937 struct tb_tunnel *tunnel; 1884 1938 1885 1939 /* ··· 1995 1921 available_up, available_down); 1996 1922 1997 1923 tunnel = tb_tunnel_alloc_dp(tb, in, out, link_nr, available_up, 1998 - available_down); 1924 + available_down, tb_dp_tunnel_active, 1925 + tb_domain_get(tb)); 1999 1926 if (!tunnel) { 2000 1927 tb_port_dbg(out, "could not allocate DP tunnel\n"); 2001 1928 goto err_reclaim_usb; 2002 1929 } 2003 1930 2004 - if (tb_tunnel_activate(tunnel)) { 1931 + list_add_tail(&tunnel->list, &tcm->tunnel_list); 1932 + 1933 + ret = tb_tunnel_activate(tunnel); 1934 + if (ret && ret != -EINPROGRESS) { 2005 1935 tb_port_info(out, "DP tunnel activation failed, aborting\n"); 1936 + list_del(&tunnel->list); 2006 1937 goto err_free; 2007 1938 } 2008 1939 2009 - /* If fail reading tunnel's consumed bandwidth, tear it down */ 2010 - ret = tb_tunnel_consumed_bandwidth(tunnel, &consumed_up, &consumed_down); 2011 - if (ret) 2012 - goto err_deactivate; 1940 + return; 2013 1941 2014 - list_add_tail(&tunnel->list, &tcm->tunnel_list); 2015 - 2016 - tb_reclaim_usb3_bandwidth(tb, in, out); 2017 - /* 2018 - * Transition the links to asymmetric if the consumption exceeds 2019 - * the threshold. 2020 - */ 2021 - tb_configure_asym(tb, in, out, consumed_up, consumed_down); 2022 - 2023 - /* Update the domain with the new bandwidth estimation */ 2024 - tb_recalc_estimated_bandwidth(tb); 2025 - 2026 - /* 2027 - * In case of DP tunnel exists, change host router's 1st children 2028 - * TMU mode to HiFi for CL0s to work. 2029 - */ 2030 - tb_increase_tmu_accuracy(tunnel); 2031 - return true; 2032 - 2033 - err_deactivate: 2034 - tb_tunnel_deactivate(tunnel); 2035 1942 err_free: 2036 - tb_tunnel_free(tunnel); 1943 + tb_tunnel_put(tunnel); 2037 1944 err_reclaim_usb: 2038 1945 tb_reclaim_usb3_bandwidth(tb, in, out); 1946 + tb_domain_put(tb); 2039 1947 err_detach_group: 2040 1948 tb_detach_bandwidth_group(in); 2041 1949 err_dealloc_dp: ··· 2027 1971 pm_runtime_put_autosuspend(&out->sw->dev); 2028 1972 pm_runtime_mark_last_busy(&in->sw->dev); 2029 1973 pm_runtime_put_autosuspend(&in->sw->dev); 2030 - 2031 - return false; 2032 1974 } 2033 1975 2034 1976 static void tb_tunnel_dp(struct tb *tb) ··· 2144 2090 } 2145 2091 } 2146 2092 2147 - static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port) 2093 + static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port, 2094 + const char *reason) 2148 2095 { 2149 2096 struct tb_port *in, *out; 2150 2097 struct tb_tunnel *tunnel; 2151 2098 2152 2099 if (tb_port_is_dpin(port)) { 2153 - tb_port_dbg(port, "DP IN resource unavailable\n"); 2100 + tb_port_dbg(port, "DP IN resource unavailable: %s\n", reason); 2154 2101 in = port; 2155 2102 out = NULL; 2156 2103 } else { 2157 - tb_port_dbg(port, "DP OUT resource unavailable\n"); 2104 + tb_port_dbg(port, "DP OUT resource unavailable: %s\n", reason); 2158 2105 in = NULL; 2159 2106 out = port; 2160 2107 } ··· 2237 2182 2238 2183 tb_tunnel_deactivate(tunnel); 2239 2184 list_del(&tunnel->list); 2240 - tb_tunnel_free(tunnel); 2185 + tb_tunnel_put(tunnel); 2241 2186 return 0; 2242 2187 } 2243 2188 ··· 2267 2212 if (tb_tunnel_activate(tunnel)) { 2268 2213 tb_port_info(up, 2269 2214 "PCIe tunnel activation failed, aborting\n"); 2270 - tb_tunnel_free(tunnel); 2215 + tb_tunnel_put(tunnel); 2271 2216 return -EIO; 2272 2217 } 2273 2218 ··· 2326 2271 return 0; 2327 2272 2328 2273 err_free: 2329 - tb_tunnel_free(tunnel); 2274 + tb_tunnel_put(tunnel); 2330 2275 err_clx: 2331 2276 tb_enable_clx(sw); 2332 2277 mutex_unlock(&tb->lock); ··· 2389 2334 */ 2390 2335 static void tb_handle_hotplug(struct work_struct *work) 2391 2336 { 2392 - struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work); 2337 + struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work.work); 2393 2338 struct tb *tb = ev->tb; 2394 2339 struct tb_cm *tcm = tb_priv(tb); 2395 2340 struct tb_switch *sw; ··· 2461 2406 tb_xdomain_put(xd); 2462 2407 tb_port_unconfigure_xdomain(port); 2463 2408 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) { 2464 - tb_dp_resource_unavailable(tb, port); 2409 + tb_dp_resource_unavailable(tb, port, "adapter unplug"); 2465 2410 } else if (!port->port) { 2466 2411 tb_sw_dbg(sw, "xHCI disconnect request\n"); 2467 2412 tb_switch_xhci_disconnect(sw); ··· 2694 2639 2695 2640 static void tb_handle_dp_bandwidth_request(struct work_struct *work) 2696 2641 { 2697 - struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work); 2642 + struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work.work); 2698 2643 int requested_bw, requested_up, requested_down, ret; 2699 2644 struct tb_tunnel *tunnel; 2700 2645 struct tb *tb = ev->tb; ··· 2721 2666 goto put_sw; 2722 2667 } 2723 2668 2724 - tb_port_dbg(in, "handling bandwidth allocation request\n"); 2669 + tb_port_dbg(in, "handling bandwidth allocation request, retry %d\n", ev->retry); 2725 2670 2726 2671 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, NULL); 2727 2672 if (!tunnel) { ··· 2774 2719 2775 2720 ret = tb_alloc_dp_bandwidth(tunnel, &requested_up, &requested_down); 2776 2721 if (ret) { 2777 - if (ret == -ENOBUFS) 2722 + if (ret == -ENOBUFS) { 2778 2723 tb_tunnel_warn(tunnel, 2779 2724 "not enough bandwidth available\n"); 2780 - else 2725 + } else if (ret == -ENOTCONN) { 2726 + tb_tunnel_dbg(tunnel, "not active yet\n"); 2727 + /* 2728 + * We got bandwidth allocation request but the 2729 + * tunnel is not yet active. This means that 2730 + * tb_dp_tunnel_active() is not yet called for 2731 + * this tunnel. Allow it some time and retry 2732 + * this request a couple of times. 2733 + */ 2734 + if (ev->retry < TB_BW_ALLOC_RETRIES) { 2735 + tb_tunnel_dbg(tunnel, 2736 + "retrying bandwidth allocation request\n"); 2737 + tb_queue_dp_bandwidth_request(tb, ev->route, 2738 + ev->port, 2739 + ev->retry + 1, 2740 + msecs_to_jiffies(50)); 2741 + } else { 2742 + tb_tunnel_dbg(tunnel, 2743 + "run out of retries, failing the request"); 2744 + } 2745 + } else { 2781 2746 tb_tunnel_warn(tunnel, 2782 2747 "failed to change bandwidth allocation\n"); 2748 + } 2783 2749 } else { 2784 2750 tb_tunnel_dbg(tunnel, 2785 2751 "bandwidth allocation changed to %d/%d Mb/s\n", ··· 2821 2745 kfree(ev); 2822 2746 } 2823 2747 2824 - static void tb_queue_dp_bandwidth_request(struct tb *tb, u64 route, u8 port) 2748 + static void tb_queue_dp_bandwidth_request(struct tb *tb, u64 route, u8 port, 2749 + int retry, unsigned long delay) 2825 2750 { 2826 2751 struct tb_hotplug_event *ev; 2827 2752 ··· 2833 2756 ev->tb = tb; 2834 2757 ev->route = route; 2835 2758 ev->port = port; 2836 - INIT_WORK(&ev->work, tb_handle_dp_bandwidth_request); 2837 - queue_work(tb->wq, &ev->work); 2759 + ev->retry = retry; 2760 + INIT_DELAYED_WORK(&ev->work, tb_handle_dp_bandwidth_request); 2761 + queue_delayed_work(tb->wq, &ev->work, delay); 2838 2762 } 2839 2763 2840 2764 static void tb_handle_notification(struct tb *tb, u64 route, ··· 2855 2777 if (tb_cfg_ack_notification(tb->ctl, route, error)) 2856 2778 tb_warn(tb, "could not ack notification on %llx\n", 2857 2779 route); 2858 - tb_queue_dp_bandwidth_request(tb, route, error->port); 2780 + tb_queue_dp_bandwidth_request(tb, route, error->port, 0, 0); 2859 2781 break; 2860 2782 2861 2783 default: ··· 2910 2832 */ 2911 2833 if (tb_tunnel_is_dma(tunnel)) 2912 2834 tb_tunnel_deactivate(tunnel); 2913 - tb_tunnel_free(tunnel); 2835 + tb_tunnel_put(tunnel); 2914 2836 } 2915 2837 tb_switch_remove(tb->root_switch); 2916 2838 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ ··· 3106 3028 if (tb_tunnel_is_usb3(tunnel)) 3107 3029 usb3_delay = 500; 3108 3030 tb_tunnel_deactivate(tunnel); 3109 - tb_tunnel_free(tunnel); 3031 + tb_tunnel_put(tunnel); 3110 3032 } 3111 3033 3112 3034 /* Re-create our tunnels now */ ··· 3117 3039 /* Only need to do it once */ 3118 3040 usb3_delay = 0; 3119 3041 } 3120 - tb_tunnel_restart(tunnel); 3042 + tb_tunnel_activate(tunnel); 3121 3043 } 3122 3044 if (!list_empty(&tcm->tunnel_list)) { 3123 3045 /* ··· 3227 3149 tb_free_invalid_tunnels(tb); 3228 3150 tb_restore_children(tb->root_switch); 3229 3151 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) 3230 - tb_tunnel_restart(tunnel); 3152 + tb_tunnel_activate(tunnel); 3231 3153 tb_switch_enter_redrive(tb->root_switch); 3232 3154 tcm->hotplug_active = true; 3233 3155 mutex_unlock(&tb->lock);
+5
drivers/thunderbolt/tb.h
··· 9 9 #ifndef TB_H_ 10 10 #define TB_H_ 11 11 12 + #include <linux/debugfs.h> 12 13 #include <linux/nvmem-provider.h> 13 14 #include <linux/pci.h> 14 15 #include <linux/thunderbolt.h> ··· 161 160 * @max_pcie_credits: Router preferred number of buffers for PCIe 162 161 * @max_dma_credits: Router preferred number of buffers for DMA/P2P 163 162 * @clx: CLx states on the upstream link of the router 163 + * @drom_blob: DROM debugfs blob wrapper 164 164 * 165 165 * When the switch is being added or removed to the domain (other 166 166 * switches) you need to have domain lock held. ··· 214 212 unsigned int max_pcie_credits; 215 213 unsigned int max_dma_credits; 216 214 unsigned int clx; 215 + #ifdef CONFIG_DEBUG_FS 216 + struct debugfs_blob_wrapper drom_blob; 217 + #endif 217 218 }; 218 219 219 220 /**
+45 -45
drivers/thunderbolt/test.c
··· 1382 1382 KUNIT_EXPECT_PTR_EQ(test, tunnel2->paths[1]->hops[0].in_port, up); 1383 1383 KUNIT_EXPECT_PTR_EQ(test, tunnel2->paths[1]->hops[1].out_port, down); 1384 1384 1385 - tb_tunnel_free(tunnel2); 1386 - tb_tunnel_free(tunnel1); 1385 + tb_tunnel_put(tunnel2); 1386 + tb_tunnel_put(tunnel1); 1387 1387 } 1388 1388 1389 1389 static void tb_test_tunnel_dp(struct kunit *test) ··· 1406 1406 in = &host->ports[5]; 1407 1407 out = &dev->ports[13]; 1408 1408 1409 - tunnel = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0); 1409 + tunnel = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0, NULL, NULL); 1410 1410 KUNIT_ASSERT_NOT_NULL(test, tunnel); 1411 1411 KUNIT_EXPECT_EQ(test, tunnel->type, TB_TUNNEL_DP); 1412 1412 KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, in); ··· 1421 1421 KUNIT_ASSERT_EQ(test, tunnel->paths[2]->path_length, 2); 1422 1422 KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[0].in_port, out); 1423 1423 KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[1].out_port, in); 1424 - tb_tunnel_free(tunnel); 1424 + tb_tunnel_put(tunnel); 1425 1425 } 1426 1426 1427 1427 static void tb_test_tunnel_dp_chain(struct kunit *test) ··· 1452 1452 in = &host->ports[5]; 1453 1453 out = &dev4->ports[14]; 1454 1454 1455 - tunnel = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0); 1455 + tunnel = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0, NULL, NULL); 1456 1456 KUNIT_ASSERT_NOT_NULL(test, tunnel); 1457 1457 KUNIT_EXPECT_EQ(test, tunnel->type, TB_TUNNEL_DP); 1458 1458 KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, in); ··· 1467 1467 KUNIT_ASSERT_EQ(test, tunnel->paths[2]->path_length, 3); 1468 1468 KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[0].in_port, out); 1469 1469 KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[2].out_port, in); 1470 - tb_tunnel_free(tunnel); 1470 + tb_tunnel_put(tunnel); 1471 1471 } 1472 1472 1473 1473 static void tb_test_tunnel_dp_tree(struct kunit *test) ··· 1502 1502 in = &dev2->ports[13]; 1503 1503 out = &dev5->ports[13]; 1504 1504 1505 - tunnel = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0); 1505 + tunnel = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0, NULL, NULL); 1506 1506 KUNIT_ASSERT_NOT_NULL(test, tunnel); 1507 1507 KUNIT_EXPECT_EQ(test, tunnel->type, TB_TUNNEL_DP); 1508 1508 KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, in); ··· 1517 1517 KUNIT_ASSERT_EQ(test, tunnel->paths[2]->path_length, 4); 1518 1518 KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[0].in_port, out); 1519 1519 KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[3].out_port, in); 1520 - tb_tunnel_free(tunnel); 1520 + tb_tunnel_put(tunnel); 1521 1521 } 1522 1522 1523 1523 static void tb_test_tunnel_dp_max_length(struct kunit *test) ··· 1567 1567 in = &dev6->ports[13]; 1568 1568 out = &dev12->ports[13]; 1569 1569 1570 - tunnel = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0); 1570 + tunnel = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0, NULL, NULL); 1571 1571 KUNIT_ASSERT_NOT_NULL(test, tunnel); 1572 1572 KUNIT_EXPECT_EQ(test, tunnel->type, TB_TUNNEL_DP); 1573 1573 KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, in); ··· 1597 1597 KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[6].out_port, 1598 1598 &host->ports[1]); 1599 1599 KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[12].out_port, in); 1600 - tb_tunnel_free(tunnel); 1600 + tb_tunnel_put(tunnel); 1601 1601 } 1602 1602 1603 1603 static void tb_test_tunnel_3dp(struct kunit *test) ··· 1637 1637 out2 = &dev5->ports[13]; 1638 1638 out3 = &dev4->ports[14]; 1639 1639 1640 - tunnel1 = tb_tunnel_alloc_dp(NULL, in1, out1, 1, 0, 0); 1640 + tunnel1 = tb_tunnel_alloc_dp(NULL, in1, out1, 1, 0, 0, NULL, NULL); 1641 1641 KUNIT_ASSERT_TRUE(test, tunnel1 != NULL); 1642 1642 KUNIT_EXPECT_EQ(test, tunnel1->type, TB_TUNNEL_DP); 1643 1643 KUNIT_EXPECT_PTR_EQ(test, tunnel1->src_port, in1); ··· 1645 1645 KUNIT_ASSERT_EQ(test, tunnel1->npaths, 3); 1646 1646 KUNIT_ASSERT_EQ(test, tunnel1->paths[0]->path_length, 3); 1647 1647 1648 - tunnel2 = tb_tunnel_alloc_dp(NULL, in2, out2, 1, 0, 0); 1648 + tunnel2 = tb_tunnel_alloc_dp(NULL, in2, out2, 1, 0, 0, NULL, NULL); 1649 1649 KUNIT_ASSERT_TRUE(test, tunnel2 != NULL); 1650 1650 KUNIT_EXPECT_EQ(test, tunnel2->type, TB_TUNNEL_DP); 1651 1651 KUNIT_EXPECT_PTR_EQ(test, tunnel2->src_port, in2); ··· 1653 1653 KUNIT_ASSERT_EQ(test, tunnel2->npaths, 3); 1654 1654 KUNIT_ASSERT_EQ(test, tunnel2->paths[0]->path_length, 4); 1655 1655 1656 - tunnel3 = tb_tunnel_alloc_dp(NULL, in3, out3, 1, 0, 0); 1656 + tunnel3 = tb_tunnel_alloc_dp(NULL, in3, out3, 1, 0, 0, NULL, NULL); 1657 1657 KUNIT_ASSERT_TRUE(test, tunnel3 != NULL); 1658 1658 KUNIT_EXPECT_EQ(test, tunnel3->type, TB_TUNNEL_DP); 1659 1659 KUNIT_EXPECT_PTR_EQ(test, tunnel3->src_port, in3); ··· 1661 1661 KUNIT_ASSERT_EQ(test, tunnel3->npaths, 3); 1662 1662 KUNIT_ASSERT_EQ(test, tunnel3->paths[0]->path_length, 3); 1663 1663 1664 - tb_tunnel_free(tunnel2); 1665 - tb_tunnel_free(tunnel1); 1664 + tb_tunnel_put(tunnel2); 1665 + tb_tunnel_put(tunnel1); 1666 1666 } 1667 1667 1668 1668 static void tb_test_tunnel_usb3(struct kunit *test) ··· 1716 1716 KUNIT_EXPECT_PTR_EQ(test, tunnel2->paths[1]->hops[0].in_port, up); 1717 1717 KUNIT_EXPECT_PTR_EQ(test, tunnel2->paths[1]->hops[1].out_port, down); 1718 1718 1719 - tb_tunnel_free(tunnel2); 1720 - tb_tunnel_free(tunnel1); 1719 + tb_tunnel_put(tunnel2); 1720 + tb_tunnel_put(tunnel1); 1721 1721 } 1722 1722 1723 1723 static void tb_test_tunnel_port_on_path(struct kunit *test) ··· 1750 1750 in = &dev2->ports[13]; 1751 1751 out = &dev5->ports[13]; 1752 1752 1753 - dp_tunnel = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0); 1753 + dp_tunnel = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0, NULL, NULL); 1754 1754 KUNIT_ASSERT_NOT_NULL(test, dp_tunnel); 1755 1755 1756 1756 KUNIT_EXPECT_TRUE(test, tb_tunnel_port_on_path(dp_tunnel, in)); ··· 1783 1783 port = &dev4->ports[1]; 1784 1784 KUNIT_EXPECT_FALSE(test, tb_tunnel_port_on_path(dp_tunnel, port)); 1785 1785 1786 - tb_tunnel_free(dp_tunnel); 1786 + tb_tunnel_put(dp_tunnel); 1787 1787 } 1788 1788 1789 1789 static void tb_test_tunnel_dma(struct kunit *test) ··· 1826 1826 KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[0].out_port, port); 1827 1827 KUNIT_EXPECT_EQ(test, tunnel->paths[1]->hops[0].next_hop_index, 8); 1828 1828 1829 - tb_tunnel_free(tunnel); 1829 + tb_tunnel_put(tunnel); 1830 1830 } 1831 1831 1832 1832 static void tb_test_tunnel_dma_rx(struct kunit *test) ··· 1863 1863 KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].out_port, nhi); 1864 1864 KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[0].next_hop_index, 2); 1865 1865 1866 - tb_tunnel_free(tunnel); 1866 + tb_tunnel_put(tunnel); 1867 1867 } 1868 1868 1869 1869 static void tb_test_tunnel_dma_tx(struct kunit *test) ··· 1900 1900 KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].out_port, port); 1901 1901 KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[0].next_hop_index, 15); 1902 1902 1903 - tb_tunnel_free(tunnel); 1903 + tb_tunnel_put(tunnel); 1904 1904 } 1905 1905 1906 1906 static void tb_test_tunnel_dma_chain(struct kunit *test) ··· 1966 1966 KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[2].out_port, port); 1967 1967 KUNIT_EXPECT_EQ(test, tunnel->paths[1]->hops[2].next_hop_index, 8); 1968 1968 1969 - tb_tunnel_free(tunnel); 1969 + tb_tunnel_put(tunnel); 1970 1970 } 1971 1971 1972 1972 static void tb_test_tunnel_dma_match(struct kunit *test) ··· 1993 1993 KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, -1, -1)); 1994 1994 KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, 8, -1, 8, -1)); 1995 1995 1996 - tb_tunnel_free(tunnel); 1996 + tb_tunnel_put(tunnel); 1997 1997 1998 1998 tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 15, 1, -1, -1); 1999 1999 KUNIT_ASSERT_NOT_NULL(test, tunnel); ··· 2005 2005 KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, -1, -1, 15, 1)); 2006 2006 KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, 15, 11, -1, -1)); 2007 2007 2008 - tb_tunnel_free(tunnel); 2008 + tb_tunnel_put(tunnel); 2009 2009 2010 2010 tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, -1, -1, 15, 11); 2011 2011 KUNIT_ASSERT_NOT_NULL(test, tunnel); ··· 2017 2017 KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, -1, -1, 10, 11)); 2018 2018 KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, 15, 11, -1, -1)); 2019 2019 2020 - tb_tunnel_free(tunnel); 2020 + tb_tunnel_put(tunnel); 2021 2021 } 2022 2022 2023 2023 static void tb_test_credit_alloc_legacy_not_bonded(struct kunit *test) ··· 2050 2050 KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U); 2051 2051 KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 16U); 2052 2052 2053 - tb_tunnel_free(tunnel); 2053 + tb_tunnel_put(tunnel); 2054 2054 } 2055 2055 2056 2056 static void tb_test_credit_alloc_legacy_bonded(struct kunit *test) ··· 2083 2083 KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U); 2084 2084 KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 32U); 2085 2085 2086 - tb_tunnel_free(tunnel); 2086 + tb_tunnel_put(tunnel); 2087 2087 } 2088 2088 2089 2089 static void tb_test_credit_alloc_pcie(struct kunit *test) ··· 2116 2116 KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U); 2117 2117 KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 64U); 2118 2118 2119 - tb_tunnel_free(tunnel); 2119 + tb_tunnel_put(tunnel); 2120 2120 } 2121 2121 2122 2122 static void tb_test_credit_alloc_without_dp(struct kunit *test) ··· 2166 2166 KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U); 2167 2167 KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 64U); 2168 2168 2169 - tb_tunnel_free(tunnel); 2169 + tb_tunnel_put(tunnel); 2170 2170 } 2171 2171 2172 2172 static void tb_test_credit_alloc_dp(struct kunit *test) ··· 2182 2182 in = &host->ports[5]; 2183 2183 out = &dev->ports[14]; 2184 2184 2185 - tunnel = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0); 2185 + tunnel = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0, NULL, NULL); 2186 2186 KUNIT_ASSERT_NOT_NULL(test, tunnel); 2187 2187 KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)3); 2188 2188 ··· 2210 2210 KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U); 2211 2211 KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 1U); 2212 2212 2213 - tb_tunnel_free(tunnel); 2213 + tb_tunnel_put(tunnel); 2214 2214 } 2215 2215 2216 2216 static void tb_test_credit_alloc_usb3(struct kunit *test) ··· 2243 2243 KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U); 2244 2244 KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 32U); 2245 2245 2246 - tb_tunnel_free(tunnel); 2246 + tb_tunnel_put(tunnel); 2247 2247 } 2248 2248 2249 2249 static void tb_test_credit_alloc_dma(struct kunit *test) ··· 2279 2279 KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U); 2280 2280 KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U); 2281 2281 2282 - tb_tunnel_free(tunnel); 2282 + tb_tunnel_put(tunnel); 2283 2283 } 2284 2284 2285 2285 static void tb_test_credit_alloc_dma_multiple(struct kunit *test) ··· 2356 2356 * Release the first DMA tunnel. That should make 14 buffers 2357 2357 * available for the next tunnel. 2358 2358 */ 2359 - tb_tunnel_free(tunnel1); 2359 + tb_tunnel_put(tunnel1); 2360 2360 2361 2361 tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3); 2362 2362 KUNIT_ASSERT_NOT_NULL(test, tunnel3); ··· 2375 2375 KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U); 2376 2376 KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U); 2377 2377 2378 - tb_tunnel_free(tunnel3); 2379 - tb_tunnel_free(tunnel2); 2378 + tb_tunnel_put(tunnel3); 2379 + tb_tunnel_put(tunnel2); 2380 2380 } 2381 2381 2382 2382 static struct tb_tunnel *TB_TEST_PCIE_TUNNEL(struct kunit *test, ··· 2418 2418 2419 2419 in = &host->ports[5]; 2420 2420 out = &dev->ports[13]; 2421 - dp_tunnel1 = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0); 2421 + dp_tunnel1 = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0, NULL, NULL); 2422 2422 KUNIT_ASSERT_NOT_NULL(test, dp_tunnel1); 2423 2423 KUNIT_ASSERT_EQ(test, dp_tunnel1->npaths, (size_t)3); 2424 2424 ··· 2455 2455 2456 2456 in = &host->ports[6]; 2457 2457 out = &dev->ports[14]; 2458 - dp_tunnel2 = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0); 2458 + dp_tunnel2 = tb_tunnel_alloc_dp(NULL, in, out, 1, 0, 0, NULL, NULL); 2459 2459 KUNIT_ASSERT_NOT_NULL(test, dp_tunnel2); 2460 2460 KUNIT_ASSERT_EQ(test, dp_tunnel2->npaths, (size_t)3); 2461 2461 ··· 2595 2595 dma_tunnel1 = TB_TEST_DMA_TUNNEL1(test, host, dev); 2596 2596 dma_tunnel2 = TB_TEST_DMA_TUNNEL2(test, host, dev); 2597 2597 2598 - tb_tunnel_free(dma_tunnel2); 2599 - tb_tunnel_free(dma_tunnel1); 2600 - tb_tunnel_free(usb3_tunnel); 2601 - tb_tunnel_free(dp_tunnel2); 2602 - tb_tunnel_free(dp_tunnel1); 2603 - tb_tunnel_free(pcie_tunnel); 2598 + tb_tunnel_put(dma_tunnel2); 2599 + tb_tunnel_put(dma_tunnel1); 2600 + tb_tunnel_put(usb3_tunnel); 2601 + tb_tunnel_put(dp_tunnel2); 2602 + tb_tunnel_put(dp_tunnel1); 2603 + tb_tunnel_put(pcie_tunnel); 2604 2604 } 2605 2605 2606 2606 static const u32 root_directory[] = {
+249 -157
drivers/thunderbolt/tunnel.c
··· 70 70 #define USB4_V2_PCI_MIN_BANDWIDTH (1500 * TB_PCI_WEIGHT) 71 71 #define USB4_V2_USB3_MIN_BANDWIDTH (1500 * TB_USB3_WEIGHT) 72 72 73 + /* 74 + * According to VESA spec, the DPRX negotiation shall compete in 5 75 + * seconds after tunnel is established. Since at least i915 can runtime 76 + * suspend if there is nothing connected, and that it polls any new 77 + * connections every 10 seconds, we use 12 seconds here. 78 + * 79 + * These are in ms. 80 + */ 81 + #define TB_DPRX_TIMEOUT 12000 82 + #define TB_DPRX_WAIT_TIMEOUT 25 83 + #define TB_DPRX_POLL_DELAY 50 84 + 85 + static int dprx_timeout = TB_DPRX_TIMEOUT; 86 + module_param(dprx_timeout, int, 0444); 87 + MODULE_PARM_DESC(dprx_timeout, 88 + "DPRX capability read timeout in ms, -1 waits forever (default: " 89 + __MODULE_STRING(TB_DPRX_TIMEOUT) ")"); 90 + 73 91 static unsigned int dma_credits = TB_DMA_CREDITS; 74 92 module_param(dma_credits, uint, 0444); 75 93 MODULE_PARM_DESC(dma_credits, "specify custom credits for DMA tunnels (default: " ··· 99 81 "enable bandwidth allocation mode if supported (default: true)"); 100 82 101 83 static const char * const tb_tunnel_names[] = { "PCI", "DP", "DMA", "USB3" }; 84 + 85 + /* Synchronizes kref_get()/put() of struct tb_tunnel */ 86 + static DEFINE_MUTEX(tb_tunnel_lock); 102 87 103 88 static inline unsigned int tb_usable_credits(const struct tb_port *port) 104 89 { ··· 176 155 177 156 tunnel->paths = kcalloc(npaths, sizeof(tunnel->paths[0]), GFP_KERNEL); 178 157 if (!tunnel->paths) { 179 - tb_tunnel_free(tunnel); 158 + kfree(tunnel); 180 159 return NULL; 181 160 } 182 161 ··· 184 163 tunnel->tb = tb; 185 164 tunnel->npaths = npaths; 186 165 tunnel->type = type; 166 + kref_init(&tunnel->kref); 187 167 188 168 return tunnel; 169 + } 170 + 171 + static void tb_tunnel_get(struct tb_tunnel *tunnel) 172 + { 173 + mutex_lock(&tb_tunnel_lock); 174 + kref_get(&tunnel->kref); 175 + mutex_unlock(&tb_tunnel_lock); 176 + } 177 + 178 + static void tb_tunnel_destroy(struct kref *kref) 179 + { 180 + struct tb_tunnel *tunnel = container_of(kref, typeof(*tunnel), kref); 181 + int i; 182 + 183 + if (tunnel->destroy) 184 + tunnel->destroy(tunnel); 185 + 186 + for (i = 0; i < tunnel->npaths; i++) { 187 + if (tunnel->paths[i]) 188 + tb_path_free(tunnel->paths[i]); 189 + } 190 + 191 + kfree(tunnel->paths); 192 + kfree(tunnel); 193 + } 194 + 195 + void tb_tunnel_put(struct tb_tunnel *tunnel) 196 + { 197 + mutex_lock(&tb_tunnel_lock); 198 + kref_put(&tunnel->kref, tb_tunnel_destroy); 199 + mutex_unlock(&tb_tunnel_lock); 189 200 } 190 201 191 202 static int tb_pci_set_ext_encapsulation(struct tb_tunnel *tunnel, bool enable) ··· 408 355 err_deactivate: 409 356 tb_tunnel_deactivate(tunnel); 410 357 err_free: 411 - tb_tunnel_free(tunnel); 358 + tb_tunnel_put(tunnel); 412 359 413 360 return NULL; 414 361 } ··· 457 404 return tunnel; 458 405 459 406 err_free: 460 - tb_tunnel_free(tunnel); 407 + tb_tunnel_put(tunnel); 461 408 return NULL; 462 409 } 463 410 ··· 904 851 return 0; 905 852 } 906 853 907 - static int tb_dp_init(struct tb_tunnel *tunnel) 854 + static int tb_dp_pre_activate(struct tb_tunnel *tunnel) 908 855 { 909 856 struct tb_port *in = tunnel->src_port; 910 857 struct tb_switch *sw = in->sw; ··· 930 877 return tb_dp_bandwidth_alloc_mode_enable(tunnel); 931 878 } 932 879 933 - static void tb_dp_deinit(struct tb_tunnel *tunnel) 880 + static void tb_dp_post_deactivate(struct tb_tunnel *tunnel) 934 881 { 935 882 struct tb_port *in = tunnel->src_port; 936 883 ··· 940 887 usb4_dp_port_set_cm_bandwidth_mode_supported(in, false); 941 888 tb_tunnel_dbg(tunnel, "bandwidth allocation mode disabled\n"); 942 889 } 890 + } 891 + 892 + static ktime_t dprx_timeout_to_ktime(int timeout_msec) 893 + { 894 + return timeout_msec >= 0 ? 895 + ktime_add_ms(ktime_get(), timeout_msec) : KTIME_MAX; 896 + } 897 + 898 + static int tb_dp_wait_dprx(struct tb_tunnel *tunnel, int timeout_msec) 899 + { 900 + ktime_t timeout = dprx_timeout_to_ktime(timeout_msec); 901 + struct tb_port *in = tunnel->src_port; 902 + 903 + /* 904 + * Wait for DPRX done. Normally it should be already set for 905 + * active tunnel. 906 + */ 907 + do { 908 + u32 val; 909 + int ret; 910 + 911 + ret = tb_port_read(in, &val, TB_CFG_PORT, 912 + in->cap_adap + DP_COMMON_CAP, 1); 913 + if (ret) 914 + return ret; 915 + 916 + if (val & DP_COMMON_CAP_DPRX_DONE) 917 + return 0; 918 + 919 + usleep_range(100, 150); 920 + } while (ktime_before(ktime_get(), timeout)); 921 + 922 + tb_tunnel_dbg(tunnel, "DPRX read timeout\n"); 923 + return -ETIMEDOUT; 924 + } 925 + 926 + static void tb_dp_dprx_work(struct work_struct *work) 927 + { 928 + struct tb_tunnel *tunnel = container_of(work, typeof(*tunnel), dprx_work.work); 929 + struct tb *tb = tunnel->tb; 930 + 931 + if (!tunnel->dprx_canceled) { 932 + mutex_lock(&tb->lock); 933 + if (tb_dp_is_usb4(tunnel->src_port->sw) && 934 + tb_dp_wait_dprx(tunnel, TB_DPRX_WAIT_TIMEOUT)) { 935 + if (ktime_before(ktime_get(), tunnel->dprx_timeout)) { 936 + queue_delayed_work(tb->wq, &tunnel->dprx_work, 937 + msecs_to_jiffies(TB_DPRX_POLL_DELAY)); 938 + mutex_unlock(&tb->lock); 939 + return; 940 + } 941 + } else { 942 + tunnel->state = TB_TUNNEL_ACTIVE; 943 + } 944 + mutex_unlock(&tb->lock); 945 + } 946 + 947 + if (tunnel->callback) 948 + tunnel->callback(tunnel, tunnel->callback_data); 949 + } 950 + 951 + static int tb_dp_dprx_start(struct tb_tunnel *tunnel) 952 + { 953 + /* 954 + * Bump up the reference to keep the tunnel around. It will be 955 + * dropped in tb_dp_dprx_stop() once the tunnel is deactivated. 956 + */ 957 + tb_tunnel_get(tunnel); 958 + 959 + if (tunnel->callback) { 960 + tunnel->dprx_timeout = dprx_timeout_to_ktime(dprx_timeout); 961 + queue_delayed_work(tunnel->tb->wq, &tunnel->dprx_work, 0); 962 + return -EINPROGRESS; 963 + } 964 + 965 + return tb_dp_is_usb4(tunnel->src_port->sw) ? 966 + tb_dp_wait_dprx(tunnel, dprx_timeout) : 0; 967 + } 968 + 969 + static void tb_dp_dprx_stop(struct tb_tunnel *tunnel) 970 + { 971 + tunnel->dprx_canceled = true; 972 + cancel_delayed_work(&tunnel->dprx_work); 973 + tb_tunnel_put(tunnel); 943 974 } 944 975 945 976 static int tb_dp_activate(struct tb_tunnel *tunnel, bool active) ··· 1047 910 paths[TB_DP_AUX_PATH_IN]->hops[0].in_hop_index, 1048 911 paths[TB_DP_AUX_PATH_OUT]->hops[last].next_hop_index); 1049 912 } else { 913 + tb_dp_dprx_stop(tunnel); 1050 914 tb_dp_port_hpd_clear(tunnel->src_port); 1051 915 tb_dp_port_set_hops(tunnel->src_port, 0, 0, 0); 1052 916 if (tb_port_is_dpout(tunnel->dst_port)) ··· 1058 920 if (ret) 1059 921 return ret; 1060 922 1061 - if (tb_port_is_dpout(tunnel->dst_port)) 1062 - return tb_dp_port_enable(tunnel->dst_port, active); 923 + if (tb_port_is_dpout(tunnel->dst_port)) { 924 + ret = tb_dp_port_enable(tunnel->dst_port, active); 925 + if (ret) 926 + return ret; 927 + } 1063 928 1064 - return 0; 929 + return active ? tb_dp_dprx_start(tunnel) : 0; 1065 930 } 1066 931 1067 932 /** ··· 1217 1076 return 0; 1218 1077 } 1219 1078 1220 - static int tb_dp_wait_dprx(struct tb_tunnel *tunnel, int timeout_msec) 1221 - { 1222 - ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec); 1223 - struct tb_port *in = tunnel->src_port; 1224 - 1225 - /* 1226 - * Wait for DPRX done. Normally it should be already set for 1227 - * active tunnel. 1228 - */ 1229 - do { 1230 - u32 val; 1231 - int ret; 1232 - 1233 - ret = tb_port_read(in, &val, TB_CFG_PORT, 1234 - in->cap_adap + DP_COMMON_CAP, 1); 1235 - if (ret) 1236 - return ret; 1237 - 1238 - if (val & DP_COMMON_CAP_DPRX_DONE) { 1239 - tb_tunnel_dbg(tunnel, "DPRX read done\n"); 1240 - return 0; 1241 - } 1242 - usleep_range(100, 150); 1243 - } while (ktime_before(ktime_get(), timeout)); 1244 - 1245 - tb_tunnel_dbg(tunnel, "DPRX read timeout\n"); 1246 - return -ETIMEDOUT; 1247 - } 1248 - 1249 1079 /* Read cap from tunnel DP IN */ 1250 1080 static int tb_dp_read_cap(struct tb_tunnel *tunnel, unsigned int cap, u32 *rate, 1251 1081 u32 *lanes) ··· 1280 1168 int ret; 1281 1169 1282 1170 if (tb_dp_is_usb4(sw)) { 1283 - /* 1284 - * On USB4 routers check if the bandwidth allocation 1285 - * mode is enabled first and then read the bandwidth 1286 - * through those registers. 1287 - */ 1288 - ret = tb_dp_bandwidth_mode_consumed_bandwidth(tunnel, consumed_up, 1289 - consumed_down); 1290 - if (ret < 0) { 1291 - if (ret != -EOPNOTSUPP) 1171 + ret = tb_dp_wait_dprx(tunnel, 0); 1172 + if (ret) { 1173 + if (ret == -ETIMEDOUT) { 1174 + /* 1175 + * While we wait for DPRX complete the 1176 + * tunnel consumes as much as it had 1177 + * been reserved initially. 1178 + */ 1179 + ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP, 1180 + &rate, &lanes); 1181 + if (ret) 1182 + return ret; 1183 + } else { 1292 1184 return ret; 1293 - } else if (!ret) { 1294 - return 0; 1185 + } 1186 + } else { 1187 + /* 1188 + * On USB4 routers check if the bandwidth allocation 1189 + * mode is enabled first and then read the bandwidth 1190 + * through those registers. 1191 + */ 1192 + ret = tb_dp_bandwidth_mode_consumed_bandwidth(tunnel, consumed_up, 1193 + consumed_down); 1194 + if (ret < 0) { 1195 + if (ret != -EOPNOTSUPP) 1196 + return ret; 1197 + } else if (!ret) { 1198 + return 0; 1199 + } 1200 + ret = tb_dp_read_cap(tunnel, DP_COMMON_CAP, &rate, &lanes); 1201 + if (ret) 1202 + return ret; 1295 1203 } 1296 - /* 1297 - * Then see if the DPRX negotiation is ready and if yes 1298 - * return that bandwidth (it may be smaller than the 1299 - * reduced one). According to VESA spec, the DPRX 1300 - * negotiation shall compete in 5 seconds after tunnel 1301 - * established. We give it 100ms extra just in case. 1302 - */ 1303 - ret = tb_dp_wait_dprx(tunnel, 5100); 1304 - if (ret) 1305 - return ret; 1306 - ret = tb_dp_read_cap(tunnel, DP_COMMON_CAP, &rate, &lanes); 1307 - if (ret) 1308 - return ret; 1309 1204 } else if (sw->generation >= 2) { 1310 1205 ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP, &rate, &lanes); 1311 1206 if (ret) ··· 1484 1365 if (!tunnel) 1485 1366 return NULL; 1486 1367 1487 - tunnel->init = tb_dp_init; 1488 - tunnel->deinit = tb_dp_deinit; 1368 + tunnel->pre_activate = tb_dp_pre_activate; 1489 1369 tunnel->activate = tb_dp_activate; 1370 + tunnel->post_deactivate = tb_dp_post_deactivate; 1490 1371 tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth; 1491 1372 tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth; 1492 1373 tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth; ··· 1543 1424 err_deactivate: 1544 1425 tb_tunnel_deactivate(tunnel); 1545 1426 err_free: 1546 - tb_tunnel_free(tunnel); 1427 + tb_tunnel_put(tunnel); 1547 1428 1548 1429 return NULL; 1549 1430 } ··· 1558 1439 * %0 if no available bandwidth. 1559 1440 * @max_down: Maximum available downstream bandwidth for the DP tunnel. 1560 1441 * %0 if no available bandwidth. 1442 + * @callback: Optional callback that is called when the DP tunnel is 1443 + * fully activated (or there is an error) 1444 + * @callback_data: Optional data for @callback 1561 1445 * 1562 1446 * Allocates a tunnel between @in and @out that is capable of tunneling 1563 - * Display Port traffic. 1447 + * Display Port traffic. If @callback is not %NULL it will be called 1448 + * after tb_tunnel_activate() once the tunnel has been fully activated. 1449 + * It can call tb_tunnel_is_active() to check if activation was 1450 + * successful (or if it returns %false there was some sort of issue). 1451 + * The @callback is called without @tb->lock held. 1564 1452 * 1565 - * Return: Returns a tb_tunnel on success or NULL on failure. 1453 + * Return: Returns a tb_tunnel on success or &NULL on failure. 1566 1454 */ 1567 1455 struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in, 1568 1456 struct tb_port *out, int link_nr, 1569 - int max_up, int max_down) 1457 + int max_up, int max_down, 1458 + void (*callback)(struct tb_tunnel *, void *), 1459 + void *callback_data) 1570 1460 { 1571 1461 struct tb_tunnel *tunnel; 1572 1462 struct tb_path **paths; ··· 1589 1461 if (!tunnel) 1590 1462 return NULL; 1591 1463 1592 - tunnel->init = tb_dp_init; 1593 - tunnel->deinit = tb_dp_deinit; 1464 + tunnel->pre_activate = tb_dp_pre_activate; 1594 1465 tunnel->activate = tb_dp_activate; 1466 + tunnel->post_deactivate = tb_dp_post_deactivate; 1595 1467 tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth; 1596 1468 tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth; 1597 1469 tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth; ··· 1600 1472 tunnel->dst_port = out; 1601 1473 tunnel->max_up = max_up; 1602 1474 tunnel->max_down = max_down; 1475 + tunnel->callback = callback; 1476 + tunnel->callback_data = callback_data; 1477 + INIT_DELAYED_WORK(&tunnel->dprx_work, tb_dp_dprx_work); 1603 1478 1604 1479 paths = tunnel->paths; 1605 1480 pm_support = usb4_switch_version(in->sw) >= 2; ··· 1631 1500 return tunnel; 1632 1501 1633 1502 err_free: 1634 - tb_tunnel_free(tunnel); 1503 + tb_tunnel_put(tunnel); 1635 1504 return NULL; 1636 1505 } 1637 1506 ··· 1751 1620 } 1752 1621 } 1753 1622 1754 - static void tb_dma_deinit_path(struct tb_path *path) 1623 + static void tb_dma_destroy_path(struct tb_path *path) 1755 1624 { 1756 1625 struct tb_path_hop *hop; 1757 1626 ··· 1759 1628 tb_dma_release_credits(hop); 1760 1629 } 1761 1630 1762 - static void tb_dma_deinit(struct tb_tunnel *tunnel) 1631 + static void tb_dma_destroy(struct tb_tunnel *tunnel) 1763 1632 { 1764 1633 int i; 1765 1634 1766 1635 for (i = 0; i < tunnel->npaths; i++) { 1767 1636 if (!tunnel->paths[i]) 1768 1637 continue; 1769 - tb_dma_deinit_path(tunnel->paths[i]); 1638 + tb_dma_destroy_path(tunnel->paths[i]); 1770 1639 } 1771 1640 } 1772 1641 ··· 1812 1681 1813 1682 tunnel->src_port = nhi; 1814 1683 tunnel->dst_port = dst; 1815 - tunnel->deinit = tb_dma_deinit; 1684 + tunnel->destroy = tb_dma_destroy; 1816 1685 1817 1686 credits = min_not_zero(dma_credits, nhi->sw->max_dma_credits); 1818 1687 ··· 1843 1712 return tunnel; 1844 1713 1845 1714 err_free: 1846 - tb_tunnel_free(tunnel); 1715 + tb_tunnel_put(tunnel); 1847 1716 return NULL; 1848 1717 } 1849 1718 ··· 1924 1793 return min(up_max_rate, down_max_rate); 1925 1794 } 1926 1795 1927 - static int tb_usb3_init(struct tb_tunnel *tunnel) 1796 + static int tb_usb3_pre_activate(struct tb_tunnel *tunnel) 1928 1797 { 1929 1798 tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n", 1930 1799 tunnel->allocated_up, tunnel->allocated_down); ··· 2155 2024 tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n", 2156 2025 tunnel->allocated_up, tunnel->allocated_down); 2157 2026 2158 - tunnel->init = tb_usb3_init; 2027 + tunnel->pre_activate = tb_usb3_pre_activate; 2159 2028 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth; 2160 2029 tunnel->release_unused_bandwidth = 2161 2030 tb_usb3_release_unused_bandwidth; ··· 2169 2038 err_deactivate: 2170 2039 tb_tunnel_deactivate(tunnel); 2171 2040 err_free: 2172 - tb_tunnel_free(tunnel); 2041 + tb_tunnel_put(tunnel); 2173 2042 2174 2043 return NULL; 2175 2044 } ··· 2225 2094 path = tb_path_alloc(tb, down, TB_USB3_HOPID, up, TB_USB3_HOPID, 0, 2226 2095 "USB3 Down"); 2227 2096 if (!path) { 2228 - tb_tunnel_free(tunnel); 2097 + tb_tunnel_put(tunnel); 2229 2098 return NULL; 2230 2099 } 2231 2100 tb_usb3_init_path(path); ··· 2234 2103 path = tb_path_alloc(tb, up, TB_USB3_HOPID, down, TB_USB3_HOPID, 0, 2235 2104 "USB3 Up"); 2236 2105 if (!path) { 2237 - tb_tunnel_free(tunnel); 2106 + tb_tunnel_put(tunnel); 2238 2107 return NULL; 2239 2108 } 2240 2109 tb_usb3_init_path(path); ··· 2244 2113 tunnel->allocated_up = min(max_rate, max_up); 2245 2114 tunnel->allocated_down = min(max_rate, max_down); 2246 2115 2247 - tunnel->init = tb_usb3_init; 2116 + tunnel->pre_activate = tb_usb3_pre_activate; 2248 2117 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth; 2249 2118 tunnel->release_unused_bandwidth = 2250 2119 tb_usb3_release_unused_bandwidth; ··· 2253 2122 } 2254 2123 2255 2124 return tunnel; 2256 - } 2257 - 2258 - /** 2259 - * tb_tunnel_free() - free a tunnel 2260 - * @tunnel: Tunnel to be freed 2261 - * 2262 - * Frees a tunnel. The tunnel does not need to be deactivated. 2263 - */ 2264 - void tb_tunnel_free(struct tb_tunnel *tunnel) 2265 - { 2266 - int i; 2267 - 2268 - if (!tunnel) 2269 - return; 2270 - 2271 - if (tunnel->deinit) 2272 - tunnel->deinit(tunnel); 2273 - 2274 - for (i = 0; i < tunnel->npaths; i++) { 2275 - if (tunnel->paths[i]) 2276 - tb_path_free(tunnel->paths[i]); 2277 - } 2278 - 2279 - kfree(tunnel->paths); 2280 - kfree(tunnel); 2281 2125 } 2282 2126 2283 2127 /** ··· 2273 2167 } 2274 2168 2275 2169 /** 2276 - * tb_tunnel_restart() - activate a tunnel after a hardware reset 2277 - * @tunnel: Tunnel to restart 2170 + * tb_tunnel_activate() - activate a tunnel 2171 + * @tunnel: Tunnel to activate 2278 2172 * 2279 - * Return: 0 on success and negative errno in case if failure 2173 + * Return: 0 on success and negative errno in case if failure. 2174 + * Specifically returns %-EINPROGRESS if the tunnel activation is still 2175 + * in progress (that's for DP tunnels to complete DPRX capabilities 2176 + * read). 2280 2177 */ 2281 - int tb_tunnel_restart(struct tb_tunnel *tunnel) 2178 + int tb_tunnel_activate(struct tb_tunnel *tunnel) 2282 2179 { 2283 2180 int res, i; 2284 2181 ··· 2298 2189 } 2299 2190 } 2300 2191 2301 - if (tunnel->init) { 2302 - res = tunnel->init(tunnel); 2192 + tunnel->state = TB_TUNNEL_ACTIVATING; 2193 + 2194 + if (tunnel->pre_activate) { 2195 + res = tunnel->pre_activate(tunnel); 2303 2196 if (res) 2304 2197 return res; 2305 2198 } ··· 2314 2203 2315 2204 if (tunnel->activate) { 2316 2205 res = tunnel->activate(tunnel, true); 2317 - if (res) 2206 + if (res) { 2207 + if (res == -EINPROGRESS) 2208 + return res; 2318 2209 goto err; 2210 + } 2319 2211 } 2320 2212 2213 + tunnel->state = TB_TUNNEL_ACTIVE; 2321 2214 return 0; 2322 2215 2323 2216 err: 2324 2217 tb_tunnel_warn(tunnel, "activation failed\n"); 2325 2218 tb_tunnel_deactivate(tunnel); 2326 2219 return res; 2327 - } 2328 - 2329 - /** 2330 - * tb_tunnel_activate() - activate a tunnel 2331 - * @tunnel: Tunnel to activate 2332 - * 2333 - * Return: Returns 0 on success or an error code on failure. 2334 - */ 2335 - int tb_tunnel_activate(struct tb_tunnel *tunnel) 2336 - { 2337 - int i; 2338 - 2339 - for (i = 0; i < tunnel->npaths; i++) { 2340 - if (tunnel->paths[i]->activated) { 2341 - tb_tunnel_WARN(tunnel, 2342 - "trying to activate an already activated tunnel\n"); 2343 - return -EINVAL; 2344 - } 2345 - } 2346 - 2347 - return tb_tunnel_restart(tunnel); 2348 2220 } 2349 2221 2350 2222 /** ··· 2347 2253 if (tunnel->paths[i] && tunnel->paths[i]->activated) 2348 2254 tb_path_deactivate(tunnel->paths[i]); 2349 2255 } 2256 + 2257 + if (tunnel->post_deactivate) 2258 + tunnel->post_deactivate(tunnel); 2259 + 2260 + tunnel->state = TB_TUNNEL_INACTIVE; 2350 2261 } 2351 2262 2352 2263 /** ··· 2378 2279 return false; 2379 2280 } 2380 2281 2381 - static bool tb_tunnel_is_active(const struct tb_tunnel *tunnel) 2282 + // Is tb_tunnel_activate() called for the tunnel 2283 + static bool tb_tunnel_is_activated(const struct tb_tunnel *tunnel) 2382 2284 { 2383 - int i; 2384 - 2385 - for (i = 0; i < tunnel->npaths; i++) { 2386 - if (!tunnel->paths[i]) 2387 - return false; 2388 - if (!tunnel->paths[i]->activated) 2389 - return false; 2390 - } 2391 - 2392 - return true; 2285 + return tunnel->state == TB_TUNNEL_ACTIVATING || tb_tunnel_is_active(tunnel); 2393 2286 } 2394 2287 2395 2288 /** ··· 2398 2307 int *max_down) 2399 2308 { 2400 2309 if (!tb_tunnel_is_active(tunnel)) 2401 - return -EINVAL; 2310 + return -ENOTCONN; 2402 2311 2403 2312 if (tunnel->maximum_bandwidth) 2404 2313 return tunnel->maximum_bandwidth(tunnel, max_up, max_down); ··· 2419 2328 int *allocated_down) 2420 2329 { 2421 2330 if (!tb_tunnel_is_active(tunnel)) 2422 - return -EINVAL; 2331 + return -ENOTCONN; 2423 2332 2424 2333 if (tunnel->allocated_bandwidth) 2425 2334 return tunnel->allocated_bandwidth(tunnel, allocated_up, ··· 2442 2351 int *alloc_down) 2443 2352 { 2444 2353 if (!tb_tunnel_is_active(tunnel)) 2445 - return -EINVAL; 2354 + return -ENOTCONN; 2446 2355 2447 2356 if (tunnel->alloc_bandwidth) 2448 2357 return tunnel->alloc_bandwidth(tunnel, alloc_up, alloc_down); ··· 2467 2376 { 2468 2377 int up_bw = 0, down_bw = 0; 2469 2378 2470 - if (!tb_tunnel_is_active(tunnel)) 2471 - goto out; 2472 - 2473 - if (tunnel->consumed_bandwidth) { 2379 + /* 2380 + * Here we need to distinguish between not active tunnel from 2381 + * tunnels that are either fully active or activation started. 2382 + * The latter is true for DP tunnels where we must report the 2383 + * consumed to be the maximum we gave it until DPRX capabilities 2384 + * read is done by the graphics driver. 2385 + */ 2386 + if (tb_tunnel_is_activated(tunnel) && tunnel->consumed_bandwidth) { 2474 2387 int ret; 2475 2388 2476 2389 ret = tunnel->consumed_bandwidth(tunnel, &up_bw, &down_bw); 2477 2390 if (ret) 2478 2391 return ret; 2479 - 2480 - tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw, 2481 - down_bw); 2482 2392 } 2483 2393 2484 - out: 2485 2394 if (consumed_up) 2486 2395 *consumed_up = up_bw; 2487 2396 if (consumed_down) 2488 2397 *consumed_down = down_bw; 2489 2398 2399 + tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw, down_bw); 2490 2400 return 0; 2491 2401 } 2492 2402 ··· 2503 2411 int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel) 2504 2412 { 2505 2413 if (!tb_tunnel_is_active(tunnel)) 2506 - return 0; 2414 + return -ENOTCONN; 2507 2415 2508 2416 if (tunnel->release_unused_bandwidth) { 2509 2417 int ret;
+54 -7
drivers/thunderbolt/tunnel.h
··· 19 19 }; 20 20 21 21 /** 22 + * enum tb_tunnel_state - State of a tunnel 23 + * @TB_TUNNEL_INACTIVE: tb_tunnel_activate() is not called for the tunnel 24 + * @TB_TUNNEL_ACTIVATING: tb_tunnel_activate() returned successfully for the tunnel 25 + * @TB_TUNNEL_ACTIVE: The tunnel is fully active 26 + */ 27 + enum tb_tunnel_state { 28 + TB_TUNNEL_INACTIVE, 29 + TB_TUNNEL_ACTIVATING, 30 + TB_TUNNEL_ACTIVE, 31 + }; 32 + 33 + /** 22 34 * struct tb_tunnel - Tunnel between two ports 35 + * @kref: Reference count 23 36 * @tb: Pointer to the domain 24 37 * @src_port: Source port of the tunnel 25 38 * @dst_port: Destination port of the tunnel. For discovered incomplete 26 39 * tunnels may be %NULL or null adapter port instead. 27 40 * @paths: All paths required by the tunnel 28 41 * @npaths: Number of paths in @paths 29 - * @init: Optional tunnel specific initialization 30 - * @deinit: Optional tunnel specific de-initialization 42 + * @pre_activate: Optional tunnel specific initialization called before 43 + * activation. Can touch hardware. 31 44 * @activate: Optional tunnel specific activation/deactivation 45 + * @post_deactivate: Optional tunnel specific de-initialization called 46 + * after deactivation. Can touch hardware. 47 + * @destroy: Optional tunnel specific callback called when the tunnel 48 + * memory is being released. Should not touch hardware. 32 49 * @maximum_bandwidth: Returns maximum possible bandwidth for this tunnel 33 50 * @allocated_bandwidth: Return how much bandwidth is allocated for the tunnel 34 51 * @alloc_bandwidth: Change tunnel bandwidth allocation ··· 54 37 * @reclaim_available_bandwidth: Reclaim back available bandwidth 55 38 * @list: Tunnels are linked using this field 56 39 * @type: Type of the tunnel 40 + * @state: Current state of the tunnel 57 41 * @max_up: Maximum upstream bandwidth (Mb/s) available for the tunnel. 58 42 * Only set if the bandwidth needs to be limited. 59 43 * @max_down: Maximum downstream bandwidth (Mb/s) available for the tunnel. ··· 63 45 * @allocated_down: Allocated downstream bandwidth (only for USB3) 64 46 * @bw_mode: DP bandwidth allocation mode registers can be used to 65 47 * determine consumed and allocated bandwidth 48 + * @dprx_canceled: Was DPRX capabilities read poll canceled 49 + * @dprx_timeout: If set DPRX capabilities read poll work will timeout after this passes 50 + * @dprx_work: Worker that is scheduled to poll completion of DPRX capabilities read 51 + * @callback: Optional callback called when DP tunnel is fully activated 52 + * @callback_data: Optional data for @callback 66 53 */ 67 54 struct tb_tunnel { 55 + struct kref kref; 68 56 struct tb *tb; 69 57 struct tb_port *src_port; 70 58 struct tb_port *dst_port; 71 59 struct tb_path **paths; 72 60 size_t npaths; 73 - int (*init)(struct tb_tunnel *tunnel); 74 - void (*deinit)(struct tb_tunnel *tunnel); 61 + int (*pre_activate)(struct tb_tunnel *tunnel); 75 62 int (*activate)(struct tb_tunnel *tunnel, bool activate); 63 + void (*post_deactivate)(struct tb_tunnel *tunnel); 64 + void (*destroy)(struct tb_tunnel *tunnel); 76 65 int (*maximum_bandwidth)(struct tb_tunnel *tunnel, int *max_up, 77 66 int *max_down); 78 67 int (*allocated_bandwidth)(struct tb_tunnel *tunnel, int *allocated_up, ··· 94 69 int *available_down); 95 70 struct list_head list; 96 71 enum tb_tunnel_type type; 72 + enum tb_tunnel_state state; 97 73 int max_up; 98 74 int max_down; 99 75 int allocated_up; 100 76 int allocated_down; 101 77 bool bw_mode; 78 + bool dprx_canceled; 79 + ktime_t dprx_timeout; 80 + struct delayed_work dprx_work; 81 + void (*callback)(struct tb_tunnel *tunnel, void *data); 82 + void *callback_data; 102 83 }; 103 84 104 85 struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down, ··· 117 86 bool alloc_hopid); 118 87 struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in, 119 88 struct tb_port *out, int link_nr, 120 - int max_up, int max_down); 89 + int max_up, int max_down, 90 + void (*callback)(struct tb_tunnel *, void *), 91 + void *callback_data); 121 92 struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi, 122 93 struct tb_port *dst, int transmit_path, 123 94 int transmit_ring, int receive_path, ··· 132 99 struct tb_port *down, int max_up, 133 100 int max_down); 134 101 135 - void tb_tunnel_free(struct tb_tunnel *tunnel); 102 + void tb_tunnel_put(struct tb_tunnel *tunnel); 136 103 int tb_tunnel_activate(struct tb_tunnel *tunnel); 137 - int tb_tunnel_restart(struct tb_tunnel *tunnel); 138 104 void tb_tunnel_deactivate(struct tb_tunnel *tunnel); 105 + 106 + /** 107 + * tb_tunnel_is_active() - Is tunnel fully activated 108 + * @tunnel: Tunnel to check 109 + * 110 + * Returns %true if @tunnel is fully activated. For other than DP 111 + * tunnels this is pretty much once tb_tunnel_activate() returns 112 + * successfully. However, for DP tunnels this returns %true only once the 113 + * DPRX capabilities read has been issued successfully. 114 + */ 115 + static inline bool tb_tunnel_is_active(const struct tb_tunnel *tunnel) 116 + { 117 + return tunnel->state == TB_TUNNEL_ACTIVE; 118 + } 119 + 139 120 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel); 140 121 bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel, 141 122 const struct tb_port *port);