Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * USB4 specific functionality |
| 4 | * |
| 5 | * Copyright (C) 2019, Intel Corporation |
| 6 | * Authors: Mika Westerberg <mika.westerberg@linux.intel.com> |
| 7 | * Rajmohan Mani <rajmohan.mani@intel.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/ktime.h> |
| 12 | |
| 13 | #include "tb.h" |
| 14 | |
| 15 | #define USB4_DATA_DWORDS 16 |
| 16 | #define USB4_DATA_RETRIES 3 |
| 17 | |
| 18 | enum usb4_switch_op { |
| 19 | USB4_SWITCH_OP_QUERY_DP_RESOURCE = 0x10, |
| 20 | USB4_SWITCH_OP_ALLOC_DP_RESOURCE = 0x11, |
| 21 | USB4_SWITCH_OP_DEALLOC_DP_RESOURCE = 0x12, |
| 22 | USB4_SWITCH_OP_NVM_WRITE = 0x20, |
| 23 | USB4_SWITCH_OP_NVM_AUTH = 0x21, |
| 24 | USB4_SWITCH_OP_NVM_READ = 0x22, |
| 25 | USB4_SWITCH_OP_NVM_SET_OFFSET = 0x23, |
| 26 | USB4_SWITCH_OP_DROM_READ = 0x24, |
| 27 | USB4_SWITCH_OP_NVM_SECTOR_SIZE = 0x25, |
| 28 | }; |
| 29 | |
| 30 | #define USB4_NVM_READ_OFFSET_MASK GENMASK(23, 2) |
| 31 | #define USB4_NVM_READ_OFFSET_SHIFT 2 |
| 32 | #define USB4_NVM_READ_LENGTH_MASK GENMASK(27, 24) |
| 33 | #define USB4_NVM_READ_LENGTH_SHIFT 24 |
| 34 | |
| 35 | #define USB4_NVM_SET_OFFSET_MASK USB4_NVM_READ_OFFSET_MASK |
| 36 | #define USB4_NVM_SET_OFFSET_SHIFT USB4_NVM_READ_OFFSET_SHIFT |
| 37 | |
| 38 | #define USB4_DROM_ADDRESS_MASK GENMASK(14, 2) |
| 39 | #define USB4_DROM_ADDRESS_SHIFT 2 |
| 40 | #define USB4_DROM_SIZE_MASK GENMASK(19, 15) |
| 41 | #define USB4_DROM_SIZE_SHIFT 15 |
| 42 | |
| 43 | #define USB4_NVM_SECTOR_SIZE_MASK GENMASK(23, 0) |
| 44 | |
| 45 | typedef int (*read_block_fn)(struct tb_switch *, unsigned int, void *, size_t); |
| 46 | typedef int (*write_block_fn)(struct tb_switch *, const void *, size_t); |
| 47 | |
| 48 | static int usb4_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit, |
| 49 | u32 value, int timeout_msec) |
| 50 | { |
| 51 | ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec); |
| 52 | |
| 53 | do { |
| 54 | u32 val; |
| 55 | int ret; |
| 56 | |
| 57 | ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1); |
| 58 | if (ret) |
| 59 | return ret; |
| 60 | |
| 61 | if ((val & bit) == value) |
| 62 | return 0; |
| 63 | |
| 64 | usleep_range(50, 100); |
| 65 | } while (ktime_before(ktime_get(), timeout)); |
| 66 | |
| 67 | return -ETIMEDOUT; |
| 68 | } |
| 69 | |
| 70 | static int usb4_switch_op_read_data(struct tb_switch *sw, void *data, |
| 71 | size_t dwords) |
| 72 | { |
| 73 | if (dwords > USB4_DATA_DWORDS) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | return tb_sw_read(sw, data, TB_CFG_SWITCH, ROUTER_CS_9, dwords); |
| 77 | } |
| 78 | |
| 79 | static int usb4_switch_op_write_data(struct tb_switch *sw, const void *data, |
| 80 | size_t dwords) |
| 81 | { |
| 82 | if (dwords > USB4_DATA_DWORDS) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | return tb_sw_write(sw, data, TB_CFG_SWITCH, ROUTER_CS_9, dwords); |
| 86 | } |
| 87 | |
| 88 | static int usb4_switch_op_read_metadata(struct tb_switch *sw, u32 *metadata) |
| 89 | { |
| 90 | return tb_sw_read(sw, metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1); |
| 91 | } |
| 92 | |
| 93 | static int usb4_switch_op_write_metadata(struct tb_switch *sw, u32 metadata) |
| 94 | { |
| 95 | return tb_sw_write(sw, &metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1); |
| 96 | } |
| 97 | |
| 98 | static int usb4_switch_do_read_data(struct tb_switch *sw, u16 address, |
| 99 | void *buf, size_t size, read_block_fn read_block) |
| 100 | { |
| 101 | unsigned int retries = USB4_DATA_RETRIES; |
| 102 | unsigned int offset; |
| 103 | |
| 104 | offset = address & 3; |
| 105 | address = address & ~3; |
| 106 | |
| 107 | do { |
| 108 | size_t nbytes = min_t(size_t, size, USB4_DATA_DWORDS * 4); |
| 109 | unsigned int dwaddress, dwords; |
| 110 | u8 data[USB4_DATA_DWORDS * 4]; |
| 111 | int ret; |
| 112 | |
| 113 | dwaddress = address / 4; |
| 114 | dwords = ALIGN(nbytes, 4) / 4; |
| 115 | |
| 116 | ret = read_block(sw, dwaddress, data, dwords); |
| 117 | if (ret) { |
| 118 | if (ret == -ETIMEDOUT) { |
| 119 | if (retries--) |
| 120 | continue; |
| 121 | ret = -EIO; |
| 122 | } |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | memcpy(buf, data + offset, nbytes); |
| 127 | |
| 128 | size -= nbytes; |
| 129 | address += nbytes; |
| 130 | buf += nbytes; |
| 131 | } while (size > 0); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int usb4_switch_do_write_data(struct tb_switch *sw, u16 address, |
| 137 | const void *buf, size_t size, write_block_fn write_next_block) |
| 138 | { |
| 139 | unsigned int retries = USB4_DATA_RETRIES; |
| 140 | unsigned int offset; |
| 141 | |
| 142 | offset = address & 3; |
| 143 | address = address & ~3; |
| 144 | |
| 145 | do { |
| 146 | u32 nbytes = min_t(u32, size, USB4_DATA_DWORDS * 4); |
| 147 | u8 data[USB4_DATA_DWORDS * 4]; |
| 148 | int ret; |
| 149 | |
| 150 | memcpy(data + offset, buf, nbytes); |
| 151 | |
| 152 | ret = write_next_block(sw, data, nbytes / 4); |
| 153 | if (ret) { |
| 154 | if (ret == -ETIMEDOUT) { |
| 155 | if (retries--) |
| 156 | continue; |
| 157 | ret = -EIO; |
| 158 | } |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | size -= nbytes; |
| 163 | address += nbytes; |
| 164 | buf += nbytes; |
| 165 | } while (size > 0); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int usb4_switch_op(struct tb_switch *sw, u16 opcode, u8 *status) |
| 171 | { |
| 172 | u32 val; |
| 173 | int ret; |
| 174 | |
| 175 | val = opcode | ROUTER_CS_26_OV; |
| 176 | ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1); |
| 177 | if (ret) |
| 178 | return ret; |
| 179 | |
| 180 | ret = usb4_switch_wait_for_bit(sw, ROUTER_CS_26, ROUTER_CS_26_OV, 0, 500); |
| 181 | if (ret) |
| 182 | return ret; |
| 183 | |
| 184 | ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1); |
Mika Westerberg | c3bf993 | 2020-04-09 10:18:10 +0300 | [diff] [blame] | 185 | if (ret) |
| 186 | return ret; |
| 187 | |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 188 | if (val & ROUTER_CS_26_ONS) |
| 189 | return -EOPNOTSUPP; |
| 190 | |
| 191 | *status = (val & ROUTER_CS_26_STATUS_MASK) >> ROUTER_CS_26_STATUS_SHIFT; |
| 192 | return 0; |
| 193 | } |
| 194 | |
Mika Westerberg | bbcf40b | 2020-03-04 17:09:14 +0200 | [diff] [blame] | 195 | static bool link_is_usb4(struct tb_port *port) |
| 196 | { |
| 197 | u32 val; |
| 198 | |
| 199 | if (!port->cap_usb4) |
| 200 | return false; |
| 201 | |
| 202 | if (tb_port_read(port, &val, TB_CFG_PORT, |
| 203 | port->cap_usb4 + PORT_CS_18, 1)) |
| 204 | return false; |
| 205 | |
| 206 | return !(val & PORT_CS_18_TCM); |
| 207 | } |
| 208 | |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 209 | /** |
| 210 | * usb4_switch_setup() - Additional setup for USB4 device |
| 211 | * @sw: USB4 router to setup |
| 212 | * |
| 213 | * USB4 routers need additional settings in order to enable all the |
| 214 | * tunneling. This function enables USB and PCIe tunneling if it can be |
| 215 | * enabled (e.g the parent switch also supports them). If USB tunneling |
| 216 | * is not available for some reason (like that there is Thunderbolt 3 |
| 217 | * switch upstream) then the internal xHCI controller is enabled |
| 218 | * instead. |
| 219 | */ |
| 220 | int usb4_switch_setup(struct tb_switch *sw) |
| 221 | { |
Mika Westerberg | bbcf40b | 2020-03-04 17:09:14 +0200 | [diff] [blame] | 222 | struct tb_port *downstream_port; |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 223 | struct tb_switch *parent; |
| 224 | bool tbt3, xhci; |
| 225 | u32 val = 0; |
| 226 | int ret; |
| 227 | |
| 228 | if (!tb_route(sw)) |
| 229 | return 0; |
| 230 | |
| 231 | ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1); |
| 232 | if (ret) |
| 233 | return ret; |
| 234 | |
Mika Westerberg | bbcf40b | 2020-03-04 17:09:14 +0200 | [diff] [blame] | 235 | parent = tb_switch_parent(sw); |
| 236 | downstream_port = tb_port_at(tb_route(sw), parent); |
| 237 | sw->link_usb4 = link_is_usb4(downstream_port); |
| 238 | tb_sw_dbg(sw, "link: %s\n", sw->link_usb4 ? "USB4" : "TBT3"); |
| 239 | |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 240 | xhci = val & ROUTER_CS_6_HCI; |
| 241 | tbt3 = !(val & ROUTER_CS_6_TNS); |
| 242 | |
| 243 | tb_sw_dbg(sw, "TBT3 support: %s, xHCI: %s\n", |
| 244 | tbt3 ? "yes" : "no", xhci ? "yes" : "no"); |
| 245 | |
| 246 | ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1); |
| 247 | if (ret) |
| 248 | return ret; |
| 249 | |
Mika Westerberg | bbcf40b | 2020-03-04 17:09:14 +0200 | [diff] [blame] | 250 | if (sw->link_usb4 && tb_switch_find_port(parent, TB_TYPE_USB3_DOWN)) { |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 251 | val |= ROUTER_CS_5_UTO; |
| 252 | xhci = false; |
| 253 | } |
| 254 | |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 255 | /* Only enable PCIe tunneling if the parent router supports it */ |
| 256 | if (tb_switch_find_port(parent, TB_TYPE_PCIE_DOWN)) { |
| 257 | val |= ROUTER_CS_5_PTO; |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 258 | /* |
| 259 | * xHCI can be enabled if PCIe tunneling is supported |
| 260 | * and the parent does not have any USB3 dowstream |
| 261 | * adapters (so we cannot do USB 3.x tunneling). |
| 262 | */ |
Mika Westerberg | c7a7ac8 | 2020-01-08 15:53:16 +0300 | [diff] [blame] | 263 | if (xhci) |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 264 | val |= ROUTER_CS_5_HCO; |
| 265 | } |
| 266 | |
| 267 | /* TBT3 supported by the CM */ |
| 268 | val |= ROUTER_CS_5_C3S; |
| 269 | /* Tunneling configuration is ready now */ |
| 270 | val |= ROUTER_CS_5_CV; |
| 271 | |
| 272 | ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1); |
| 273 | if (ret) |
| 274 | return ret; |
| 275 | |
| 276 | return usb4_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_CR, |
| 277 | ROUTER_CS_6_CR, 50); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * usb4_switch_read_uid() - Read UID from USB4 router |
| 282 | * @sw: USB4 router |
Mika Westerberg | 21d78d8 | 2020-02-14 15:16:38 +0300 | [diff] [blame] | 283 | * @uid: UID is stored here |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 284 | * |
| 285 | * Reads 64-bit UID from USB4 router config space. |
| 286 | */ |
| 287 | int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid) |
| 288 | { |
| 289 | return tb_sw_read(sw, uid, TB_CFG_SWITCH, ROUTER_CS_7, 2); |
| 290 | } |
| 291 | |
| 292 | static int usb4_switch_drom_read_block(struct tb_switch *sw, |
| 293 | unsigned int dwaddress, void *buf, |
| 294 | size_t dwords) |
| 295 | { |
| 296 | u8 status = 0; |
| 297 | u32 metadata; |
| 298 | int ret; |
| 299 | |
| 300 | metadata = (dwords << USB4_DROM_SIZE_SHIFT) & USB4_DROM_SIZE_MASK; |
| 301 | metadata |= (dwaddress << USB4_DROM_ADDRESS_SHIFT) & |
| 302 | USB4_DROM_ADDRESS_MASK; |
| 303 | |
| 304 | ret = usb4_switch_op_write_metadata(sw, metadata); |
| 305 | if (ret) |
| 306 | return ret; |
| 307 | |
| 308 | ret = usb4_switch_op(sw, USB4_SWITCH_OP_DROM_READ, &status); |
| 309 | if (ret) |
| 310 | return ret; |
| 311 | |
| 312 | if (status) |
| 313 | return -EIO; |
| 314 | |
| 315 | return usb4_switch_op_read_data(sw, buf, dwords); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * usb4_switch_drom_read() - Read arbitrary bytes from USB4 router DROM |
| 320 | * @sw: USB4 router |
Mika Westerberg | 21d78d8 | 2020-02-14 15:16:38 +0300 | [diff] [blame] | 321 | * @address: Byte address inside DROM to start reading |
| 322 | * @buf: Buffer where the DROM content is stored |
| 323 | * @size: Number of bytes to read from DROM |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 324 | * |
| 325 | * Uses USB4 router operations to read router DROM. For devices this |
| 326 | * should always work but for hosts it may return %-EOPNOTSUPP in which |
| 327 | * case the host router does not have DROM. |
| 328 | */ |
| 329 | int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf, |
| 330 | size_t size) |
| 331 | { |
| 332 | return usb4_switch_do_read_data(sw, address, buf, size, |
| 333 | usb4_switch_drom_read_block); |
| 334 | } |
| 335 | |
| 336 | static int usb4_set_port_configured(struct tb_port *port, bool configured) |
| 337 | { |
| 338 | int ret; |
| 339 | u32 val; |
| 340 | |
| 341 | ret = tb_port_read(port, &val, TB_CFG_PORT, |
| 342 | port->cap_usb4 + PORT_CS_19, 1); |
| 343 | if (ret) |
| 344 | return ret; |
| 345 | |
| 346 | if (configured) |
| 347 | val |= PORT_CS_19_PC; |
| 348 | else |
| 349 | val &= ~PORT_CS_19_PC; |
| 350 | |
| 351 | return tb_port_write(port, &val, TB_CFG_PORT, |
| 352 | port->cap_usb4 + PORT_CS_19, 1); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * usb4_switch_configure_link() - Set upstream USB4 link configured |
| 357 | * @sw: USB4 router |
| 358 | * |
| 359 | * Sets the upstream USB4 link to be configured for power management |
| 360 | * purposes. |
| 361 | */ |
| 362 | int usb4_switch_configure_link(struct tb_switch *sw) |
| 363 | { |
| 364 | struct tb_port *up; |
| 365 | |
| 366 | if (!tb_route(sw)) |
| 367 | return 0; |
| 368 | |
| 369 | up = tb_upstream_port(sw); |
| 370 | return usb4_set_port_configured(up, true); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * usb4_switch_unconfigure_link() - Un-set upstream USB4 link configuration |
| 375 | * @sw: USB4 router |
| 376 | * |
| 377 | * Reverse of usb4_switch_configure_link(). |
| 378 | */ |
| 379 | void usb4_switch_unconfigure_link(struct tb_switch *sw) |
| 380 | { |
| 381 | struct tb_port *up; |
| 382 | |
| 383 | if (sw->is_unplugged || !tb_route(sw)) |
| 384 | return; |
| 385 | |
| 386 | up = tb_upstream_port(sw); |
| 387 | usb4_set_port_configured(up, false); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * usb4_switch_lane_bonding_possible() - Are conditions met for lane bonding |
| 392 | * @sw: USB4 router |
| 393 | * |
| 394 | * Checks whether conditions are met so that lane bonding can be |
| 395 | * established with the upstream router. Call only for device routers. |
| 396 | */ |
| 397 | bool usb4_switch_lane_bonding_possible(struct tb_switch *sw) |
| 398 | { |
| 399 | struct tb_port *up; |
| 400 | int ret; |
| 401 | u32 val; |
| 402 | |
| 403 | up = tb_upstream_port(sw); |
| 404 | ret = tb_port_read(up, &val, TB_CFG_PORT, up->cap_usb4 + PORT_CS_18, 1); |
| 405 | if (ret) |
| 406 | return false; |
| 407 | |
| 408 | return !!(val & PORT_CS_18_BE); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * usb4_switch_set_sleep() - Prepare the router to enter sleep |
| 413 | * @sw: USB4 router |
| 414 | * |
| 415 | * Enables wakes and sets sleep bit for the router. Returns when the |
| 416 | * router sleep ready bit has been asserted. |
| 417 | */ |
| 418 | int usb4_switch_set_sleep(struct tb_switch *sw) |
| 419 | { |
| 420 | int ret; |
| 421 | u32 val; |
| 422 | |
| 423 | /* Set sleep bit and wait for sleep ready to be asserted */ |
| 424 | ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1); |
| 425 | if (ret) |
| 426 | return ret; |
| 427 | |
| 428 | val |= ROUTER_CS_5_SLP; |
| 429 | |
| 430 | ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1); |
| 431 | if (ret) |
| 432 | return ret; |
| 433 | |
| 434 | return usb4_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_SLPR, |
| 435 | ROUTER_CS_6_SLPR, 500); |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * usb4_switch_nvm_sector_size() - Return router NVM sector size |
| 440 | * @sw: USB4 router |
| 441 | * |
| 442 | * If the router supports NVM operations this function returns the NVM |
| 443 | * sector size in bytes. If NVM operations are not supported returns |
| 444 | * %-EOPNOTSUPP. |
| 445 | */ |
| 446 | int usb4_switch_nvm_sector_size(struct tb_switch *sw) |
| 447 | { |
| 448 | u32 metadata; |
| 449 | u8 status; |
| 450 | int ret; |
| 451 | |
| 452 | ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SECTOR_SIZE, &status); |
| 453 | if (ret) |
| 454 | return ret; |
| 455 | |
| 456 | if (status) |
| 457 | return status == 0x2 ? -EOPNOTSUPP : -EIO; |
| 458 | |
| 459 | ret = usb4_switch_op_read_metadata(sw, &metadata); |
| 460 | if (ret) |
| 461 | return ret; |
| 462 | |
| 463 | return metadata & USB4_NVM_SECTOR_SIZE_MASK; |
| 464 | } |
| 465 | |
| 466 | static int usb4_switch_nvm_read_block(struct tb_switch *sw, |
| 467 | unsigned int dwaddress, void *buf, size_t dwords) |
| 468 | { |
| 469 | u8 status = 0; |
| 470 | u32 metadata; |
| 471 | int ret; |
| 472 | |
| 473 | metadata = (dwords << USB4_NVM_READ_LENGTH_SHIFT) & |
| 474 | USB4_NVM_READ_LENGTH_MASK; |
| 475 | metadata |= (dwaddress << USB4_NVM_READ_OFFSET_SHIFT) & |
| 476 | USB4_NVM_READ_OFFSET_MASK; |
| 477 | |
| 478 | ret = usb4_switch_op_write_metadata(sw, metadata); |
| 479 | if (ret) |
| 480 | return ret; |
| 481 | |
| 482 | ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_READ, &status); |
| 483 | if (ret) |
| 484 | return ret; |
| 485 | |
| 486 | if (status) |
| 487 | return -EIO; |
| 488 | |
| 489 | return usb4_switch_op_read_data(sw, buf, dwords); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * usb4_switch_nvm_read() - Read arbitrary bytes from router NVM |
| 494 | * @sw: USB4 router |
| 495 | * @address: Starting address in bytes |
| 496 | * @buf: Read data is placed here |
| 497 | * @size: How many bytes to read |
| 498 | * |
| 499 | * Reads NVM contents of the router. If NVM is not supported returns |
| 500 | * %-EOPNOTSUPP. |
| 501 | */ |
| 502 | int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf, |
| 503 | size_t size) |
| 504 | { |
| 505 | return usb4_switch_do_read_data(sw, address, buf, size, |
| 506 | usb4_switch_nvm_read_block); |
| 507 | } |
| 508 | |
| 509 | static int usb4_switch_nvm_set_offset(struct tb_switch *sw, |
| 510 | unsigned int address) |
| 511 | { |
| 512 | u32 metadata, dwaddress; |
| 513 | u8 status = 0; |
| 514 | int ret; |
| 515 | |
| 516 | dwaddress = address / 4; |
| 517 | metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) & |
| 518 | USB4_NVM_SET_OFFSET_MASK; |
| 519 | |
| 520 | ret = usb4_switch_op_write_metadata(sw, metadata); |
| 521 | if (ret) |
| 522 | return ret; |
| 523 | |
| 524 | ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SET_OFFSET, &status); |
| 525 | if (ret) |
| 526 | return ret; |
| 527 | |
| 528 | return status ? -EIO : 0; |
| 529 | } |
| 530 | |
| 531 | static int usb4_switch_nvm_write_next_block(struct tb_switch *sw, |
| 532 | const void *buf, size_t dwords) |
| 533 | { |
| 534 | u8 status; |
| 535 | int ret; |
| 536 | |
| 537 | ret = usb4_switch_op_write_data(sw, buf, dwords); |
| 538 | if (ret) |
| 539 | return ret; |
| 540 | |
| 541 | ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_WRITE, &status); |
| 542 | if (ret) |
| 543 | return ret; |
| 544 | |
| 545 | return status ? -EIO : 0; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * usb4_switch_nvm_write() - Write to the router NVM |
| 550 | * @sw: USB4 router |
| 551 | * @address: Start address where to write in bytes |
| 552 | * @buf: Pointer to the data to write |
| 553 | * @size: Size of @buf in bytes |
| 554 | * |
| 555 | * Writes @buf to the router NVM using USB4 router operations. If NVM |
| 556 | * write is not supported returns %-EOPNOTSUPP. |
| 557 | */ |
| 558 | int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address, |
| 559 | const void *buf, size_t size) |
| 560 | { |
| 561 | int ret; |
| 562 | |
| 563 | ret = usb4_switch_nvm_set_offset(sw, address); |
| 564 | if (ret) |
| 565 | return ret; |
| 566 | |
| 567 | return usb4_switch_do_write_data(sw, address, buf, size, |
| 568 | usb4_switch_nvm_write_next_block); |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * usb4_switch_nvm_authenticate() - Authenticate new NVM |
| 573 | * @sw: USB4 router |
| 574 | * |
| 575 | * After the new NVM has been written via usb4_switch_nvm_write(), this |
| 576 | * function triggers NVM authentication process. If the authentication |
| 577 | * is successful the router is power cycled and the new NVM starts |
| 578 | * running. In case of failure returns negative errno. |
| 579 | */ |
| 580 | int usb4_switch_nvm_authenticate(struct tb_switch *sw) |
| 581 | { |
| 582 | u8 status = 0; |
| 583 | int ret; |
| 584 | |
| 585 | ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_AUTH, &status); |
| 586 | if (ret) |
| 587 | return ret; |
| 588 | |
| 589 | switch (status) { |
| 590 | case 0x0: |
| 591 | tb_sw_dbg(sw, "NVM authentication successful\n"); |
| 592 | return 0; |
| 593 | case 0x1: |
| 594 | return -EINVAL; |
| 595 | case 0x2: |
| 596 | return -EAGAIN; |
| 597 | case 0x3: |
| 598 | return -EOPNOTSUPP; |
| 599 | default: |
| 600 | return -EIO; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * usb4_switch_query_dp_resource() - Query availability of DP IN resource |
| 606 | * @sw: USB4 router |
| 607 | * @in: DP IN adapter |
| 608 | * |
| 609 | * For DP tunneling this function can be used to query availability of |
| 610 | * DP IN resource. Returns true if the resource is available for DP |
| 611 | * tunneling, false otherwise. |
| 612 | */ |
| 613 | bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in) |
| 614 | { |
| 615 | u8 status; |
| 616 | int ret; |
| 617 | |
| 618 | ret = usb4_switch_op_write_metadata(sw, in->port); |
| 619 | if (ret) |
| 620 | return false; |
| 621 | |
| 622 | ret = usb4_switch_op(sw, USB4_SWITCH_OP_QUERY_DP_RESOURCE, &status); |
| 623 | /* |
| 624 | * If DP resource allocation is not supported assume it is |
| 625 | * always available. |
| 626 | */ |
| 627 | if (ret == -EOPNOTSUPP) |
| 628 | return true; |
| 629 | else if (ret) |
| 630 | return false; |
| 631 | |
| 632 | return !status; |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * usb4_switch_alloc_dp_resource() - Allocate DP IN resource |
| 637 | * @sw: USB4 router |
| 638 | * @in: DP IN adapter |
| 639 | * |
| 640 | * Allocates DP IN resource for DP tunneling using USB4 router |
| 641 | * operations. If the resource was allocated returns %0. Otherwise |
| 642 | * returns negative errno, in particular %-EBUSY if the resource is |
| 643 | * already allocated. |
| 644 | */ |
| 645 | int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in) |
| 646 | { |
| 647 | u8 status; |
| 648 | int ret; |
| 649 | |
| 650 | ret = usb4_switch_op_write_metadata(sw, in->port); |
| 651 | if (ret) |
| 652 | return ret; |
| 653 | |
| 654 | ret = usb4_switch_op(sw, USB4_SWITCH_OP_ALLOC_DP_RESOURCE, &status); |
| 655 | if (ret == -EOPNOTSUPP) |
| 656 | return 0; |
| 657 | else if (ret) |
| 658 | return ret; |
| 659 | |
| 660 | return status ? -EBUSY : 0; |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * usb4_switch_dealloc_dp_resource() - Releases allocated DP IN resource |
| 665 | * @sw: USB4 router |
| 666 | * @in: DP IN adapter |
| 667 | * |
| 668 | * Releases the previously allocated DP IN resource. |
| 669 | */ |
| 670 | int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in) |
| 671 | { |
| 672 | u8 status; |
| 673 | int ret; |
| 674 | |
| 675 | ret = usb4_switch_op_write_metadata(sw, in->port); |
| 676 | if (ret) |
| 677 | return ret; |
| 678 | |
| 679 | ret = usb4_switch_op(sw, USB4_SWITCH_OP_DEALLOC_DP_RESOURCE, &status); |
| 680 | if (ret == -EOPNOTSUPP) |
| 681 | return 0; |
| 682 | else if (ret) |
| 683 | return ret; |
| 684 | |
| 685 | return status ? -EIO : 0; |
| 686 | } |
| 687 | |
| 688 | static int usb4_port_idx(const struct tb_switch *sw, const struct tb_port *port) |
| 689 | { |
| 690 | struct tb_port *p; |
| 691 | int usb4_idx = 0; |
| 692 | |
| 693 | /* Assume port is primary */ |
| 694 | tb_switch_for_each_port(sw, p) { |
| 695 | if (!tb_port_is_null(p)) |
| 696 | continue; |
| 697 | if (tb_is_upstream_port(p)) |
| 698 | continue; |
| 699 | if (!p->link_nr) { |
| 700 | if (p == port) |
| 701 | break; |
| 702 | usb4_idx++; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return usb4_idx; |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * usb4_switch_map_pcie_down() - Map USB4 port to a PCIe downstream adapter |
| 711 | * @sw: USB4 router |
| 712 | * @port: USB4 port |
| 713 | * |
| 714 | * USB4 routers have direct mapping between USB4 ports and PCIe |
| 715 | * downstream adapters where the PCIe topology is extended. This |
| 716 | * function returns the corresponding downstream PCIe adapter or %NULL |
| 717 | * if no such mapping was possible. |
| 718 | */ |
| 719 | struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw, |
| 720 | const struct tb_port *port) |
| 721 | { |
| 722 | int usb4_idx = usb4_port_idx(sw, port); |
| 723 | struct tb_port *p; |
| 724 | int pcie_idx = 0; |
| 725 | |
| 726 | /* Find PCIe down port matching usb4_port */ |
| 727 | tb_switch_for_each_port(sw, p) { |
| 728 | if (!tb_port_is_pcie_down(p)) |
| 729 | continue; |
| 730 | |
Mika Westerberg | 9cac51a | 2020-03-11 16:12:50 +0300 | [diff] [blame] | 731 | if (pcie_idx == usb4_idx) |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 732 | return p; |
| 733 | |
| 734 | pcie_idx++; |
| 735 | } |
| 736 | |
| 737 | return NULL; |
| 738 | } |
| 739 | |
| 740 | /** |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 741 | * usb4_switch_map_usb3_down() - Map USB4 port to a USB3 downstream adapter |
| 742 | * @sw: USB4 router |
| 743 | * @port: USB4 port |
| 744 | * |
| 745 | * USB4 routers have direct mapping between USB4 ports and USB 3.x |
| 746 | * downstream adapters where the USB 3.x topology is extended. This |
| 747 | * function returns the corresponding downstream USB 3.x adapter or |
| 748 | * %NULL if no such mapping was possible. |
| 749 | */ |
| 750 | struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw, |
| 751 | const struct tb_port *port) |
| 752 | { |
| 753 | int usb4_idx = usb4_port_idx(sw, port); |
| 754 | struct tb_port *p; |
| 755 | int usb_idx = 0; |
| 756 | |
| 757 | /* Find USB3 down port matching usb4_port */ |
| 758 | tb_switch_for_each_port(sw, p) { |
| 759 | if (!tb_port_is_usb3_down(p)) |
| 760 | continue; |
| 761 | |
Mika Westerberg | 77cfa40 | 2020-03-11 16:00:46 +0300 | [diff] [blame] | 762 | if (usb_idx == usb4_idx) |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 763 | return p; |
| 764 | |
| 765 | usb_idx++; |
| 766 | } |
| 767 | |
| 768 | return NULL; |
| 769 | } |
| 770 | |
| 771 | /** |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 772 | * usb4_port_unlock() - Unlock USB4 downstream port |
| 773 | * @port: USB4 port to unlock |
| 774 | * |
| 775 | * Unlocks USB4 downstream port so that the connection manager can |
| 776 | * access the router below this port. |
| 777 | */ |
| 778 | int usb4_port_unlock(struct tb_port *port) |
| 779 | { |
| 780 | int ret; |
| 781 | u32 val; |
| 782 | |
| 783 | ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_4, 1); |
| 784 | if (ret) |
| 785 | return ret; |
| 786 | |
| 787 | val &= ~ADP_CS_4_LCK; |
| 788 | return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_4, 1); |
| 789 | } |
Mika Westerberg | 3b1d8d5 | 2020-02-21 23:14:41 +0200 | [diff] [blame^] | 790 | |
| 791 | static int usb4_port_wait_for_bit(struct tb_port *port, u32 offset, u32 bit, |
| 792 | u32 value, int timeout_msec) |
| 793 | { |
| 794 | ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec); |
| 795 | |
| 796 | do { |
| 797 | u32 val; |
| 798 | int ret; |
| 799 | |
| 800 | ret = tb_port_read(port, &val, TB_CFG_PORT, offset, 1); |
| 801 | if (ret) |
| 802 | return ret; |
| 803 | |
| 804 | if ((val & bit) == value) |
| 805 | return 0; |
| 806 | |
| 807 | usleep_range(50, 100); |
| 808 | } while (ktime_before(ktime_get(), timeout)); |
| 809 | |
| 810 | return -ETIMEDOUT; |
| 811 | } |
| 812 | |
| 813 | /** |
| 814 | * usb4_usb3_port_max_link_rate() - Maximum support USB3 link rate |
| 815 | * @port: USB3 adapter port |
| 816 | * |
| 817 | * Return maximum supported link rate of a USB3 adapter in Mb/s. |
| 818 | * Negative errno in case of error. |
| 819 | */ |
| 820 | int usb4_usb3_port_max_link_rate(struct tb_port *port) |
| 821 | { |
| 822 | int ret, lr; |
| 823 | u32 val; |
| 824 | |
| 825 | if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port)) |
| 826 | return -EINVAL; |
| 827 | |
| 828 | ret = tb_port_read(port, &val, TB_CFG_PORT, |
| 829 | port->cap_adap + ADP_USB3_CS_4, 1); |
| 830 | if (ret) |
| 831 | return ret; |
| 832 | |
| 833 | lr = (val & ADP_USB3_CS_4_MSLR_MASK) >> ADP_USB3_CS_4_MSLR_SHIFT; |
| 834 | return lr == ADP_USB3_CS_4_MSLR_20G ? 20000 : 10000; |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * usb4_usb3_port_actual_link_rate() - Established USB3 link rate |
| 839 | * @port: USB3 adapter port |
| 840 | * |
| 841 | * Return actual established link rate of a USB3 adapter in Mb/s. If the |
| 842 | * link is not up returns %0 and negative errno in case of failure. |
| 843 | */ |
| 844 | int usb4_usb3_port_actual_link_rate(struct tb_port *port) |
| 845 | { |
| 846 | int ret, lr; |
| 847 | u32 val; |
| 848 | |
| 849 | if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port)) |
| 850 | return -EINVAL; |
| 851 | |
| 852 | ret = tb_port_read(port, &val, TB_CFG_PORT, |
| 853 | port->cap_adap + ADP_USB3_CS_4, 1); |
| 854 | if (ret) |
| 855 | return ret; |
| 856 | |
| 857 | if (!(val & ADP_USB3_CS_4_ULV)) |
| 858 | return 0; |
| 859 | |
| 860 | lr = val & ADP_USB3_CS_4_ALR_MASK; |
| 861 | return lr == ADP_USB3_CS_4_ALR_20G ? 20000 : 10000; |
| 862 | } |
| 863 | |
| 864 | static int usb4_usb3_port_cm_request(struct tb_port *port, bool request) |
| 865 | { |
| 866 | int ret; |
| 867 | u32 val; |
| 868 | |
| 869 | if (!tb_port_is_usb3_down(port)) |
| 870 | return -EINVAL; |
| 871 | if (tb_route(port->sw)) |
| 872 | return -EINVAL; |
| 873 | |
| 874 | ret = tb_port_read(port, &val, TB_CFG_PORT, |
| 875 | port->cap_adap + ADP_USB3_CS_2, 1); |
| 876 | if (ret) |
| 877 | return ret; |
| 878 | |
| 879 | if (request) |
| 880 | val |= ADP_USB3_CS_2_CMR; |
| 881 | else |
| 882 | val &= ~ADP_USB3_CS_2_CMR; |
| 883 | |
| 884 | ret = tb_port_write(port, &val, TB_CFG_PORT, |
| 885 | port->cap_adap + ADP_USB3_CS_2, 1); |
| 886 | if (ret) |
| 887 | return ret; |
| 888 | |
| 889 | /* |
| 890 | * We can use val here directly as the CMR bit is in the same place |
| 891 | * as HCA. Just mask out others. |
| 892 | */ |
| 893 | val &= ADP_USB3_CS_2_CMR; |
| 894 | return usb4_port_wait_for_bit(port, port->cap_adap + ADP_USB3_CS_1, |
| 895 | ADP_USB3_CS_1_HCA, val, 1500); |
| 896 | } |
| 897 | |
| 898 | static inline int usb4_usb3_port_set_cm_request(struct tb_port *port) |
| 899 | { |
| 900 | return usb4_usb3_port_cm_request(port, true); |
| 901 | } |
| 902 | |
| 903 | static inline int usb4_usb3_port_clear_cm_request(struct tb_port *port) |
| 904 | { |
| 905 | return usb4_usb3_port_cm_request(port, false); |
| 906 | } |
| 907 | |
| 908 | static unsigned int usb3_bw_to_mbps(u32 bw, u8 scale) |
| 909 | { |
| 910 | unsigned long uframes; |
| 911 | |
| 912 | uframes = bw * 512 << scale; |
| 913 | return DIV_ROUND_CLOSEST(uframes * 8000, 1000 * 1000); |
| 914 | } |
| 915 | |
| 916 | static u32 mbps_to_usb3_bw(unsigned int mbps, u8 scale) |
| 917 | { |
| 918 | unsigned long uframes; |
| 919 | |
| 920 | /* 1 uframe is 1/8 ms (125 us) -> 1 / 8000 s */ |
| 921 | uframes = ((unsigned long)mbps * 1000 * 1000) / 8000; |
| 922 | return DIV_ROUND_UP(uframes, 512 << scale); |
| 923 | } |
| 924 | |
| 925 | static int usb4_usb3_port_read_allocated_bandwidth(struct tb_port *port, |
| 926 | int *upstream_bw, |
| 927 | int *downstream_bw) |
| 928 | { |
| 929 | u32 val, bw, scale; |
| 930 | int ret; |
| 931 | |
| 932 | ret = tb_port_read(port, &val, TB_CFG_PORT, |
| 933 | port->cap_adap + ADP_USB3_CS_2, 1); |
| 934 | if (ret) |
| 935 | return ret; |
| 936 | |
| 937 | ret = tb_port_read(port, &scale, TB_CFG_PORT, |
| 938 | port->cap_adap + ADP_USB3_CS_3, 1); |
| 939 | if (ret) |
| 940 | return ret; |
| 941 | |
| 942 | scale &= ADP_USB3_CS_3_SCALE_MASK; |
| 943 | |
| 944 | bw = val & ADP_USB3_CS_2_AUBW_MASK; |
| 945 | *upstream_bw = usb3_bw_to_mbps(bw, scale); |
| 946 | |
| 947 | bw = (val & ADP_USB3_CS_2_ADBW_MASK) >> ADP_USB3_CS_2_ADBW_SHIFT; |
| 948 | *downstream_bw = usb3_bw_to_mbps(bw, scale); |
| 949 | |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | /** |
| 954 | * usb4_usb3_port_allocated_bandwidth() - Bandwidth allocated for USB3 |
| 955 | * @port: USB3 adapter port |
| 956 | * @upstream_bw: Allocated upstream bandwidth is stored here |
| 957 | * @downstream_bw: Allocated downstream bandwidth is stored here |
| 958 | * |
| 959 | * Stores currently allocated USB3 bandwidth into @upstream_bw and |
| 960 | * @downstream_bw in Mb/s. Returns %0 in case of success and negative |
| 961 | * errno in failure. |
| 962 | */ |
| 963 | int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw, |
| 964 | int *downstream_bw) |
| 965 | { |
| 966 | int ret; |
| 967 | |
| 968 | ret = usb4_usb3_port_set_cm_request(port); |
| 969 | if (ret) |
| 970 | return ret; |
| 971 | |
| 972 | ret = usb4_usb3_port_read_allocated_bandwidth(port, upstream_bw, |
| 973 | downstream_bw); |
| 974 | usb4_usb3_port_clear_cm_request(port); |
| 975 | |
| 976 | return ret; |
| 977 | } |
| 978 | |
| 979 | static int usb4_usb3_port_read_consumed_bandwidth(struct tb_port *port, |
| 980 | int *upstream_bw, |
| 981 | int *downstream_bw) |
| 982 | { |
| 983 | u32 val, bw, scale; |
| 984 | int ret; |
| 985 | |
| 986 | ret = tb_port_read(port, &val, TB_CFG_PORT, |
| 987 | port->cap_adap + ADP_USB3_CS_1, 1); |
| 988 | if (ret) |
| 989 | return ret; |
| 990 | |
| 991 | ret = tb_port_read(port, &scale, TB_CFG_PORT, |
| 992 | port->cap_adap + ADP_USB3_CS_3, 1); |
| 993 | if (ret) |
| 994 | return ret; |
| 995 | |
| 996 | scale &= ADP_USB3_CS_3_SCALE_MASK; |
| 997 | |
| 998 | bw = val & ADP_USB3_CS_1_CUBW_MASK; |
| 999 | *upstream_bw = usb3_bw_to_mbps(bw, scale); |
| 1000 | |
| 1001 | bw = (val & ADP_USB3_CS_1_CDBW_MASK) >> ADP_USB3_CS_1_CDBW_SHIFT; |
| 1002 | *downstream_bw = usb3_bw_to_mbps(bw, scale); |
| 1003 | |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | static int usb4_usb3_port_write_allocated_bandwidth(struct tb_port *port, |
| 1008 | int upstream_bw, |
| 1009 | int downstream_bw) |
| 1010 | { |
| 1011 | u32 val, ubw, dbw, scale; |
| 1012 | int ret; |
| 1013 | |
| 1014 | /* Read the used scale, hardware default is 0 */ |
| 1015 | ret = tb_port_read(port, &scale, TB_CFG_PORT, |
| 1016 | port->cap_adap + ADP_USB3_CS_3, 1); |
| 1017 | if (ret) |
| 1018 | return ret; |
| 1019 | |
| 1020 | scale &= ADP_USB3_CS_3_SCALE_MASK; |
| 1021 | ubw = mbps_to_usb3_bw(upstream_bw, scale); |
| 1022 | dbw = mbps_to_usb3_bw(downstream_bw, scale); |
| 1023 | |
| 1024 | ret = tb_port_read(port, &val, TB_CFG_PORT, |
| 1025 | port->cap_adap + ADP_USB3_CS_2, 1); |
| 1026 | if (ret) |
| 1027 | return ret; |
| 1028 | |
| 1029 | val &= ~(ADP_USB3_CS_2_AUBW_MASK | ADP_USB3_CS_2_ADBW_MASK); |
| 1030 | val |= dbw << ADP_USB3_CS_2_ADBW_SHIFT; |
| 1031 | val |= ubw; |
| 1032 | |
| 1033 | return tb_port_write(port, &val, TB_CFG_PORT, |
| 1034 | port->cap_adap + ADP_USB3_CS_2, 1); |
| 1035 | } |
| 1036 | |
| 1037 | /** |
| 1038 | * usb4_usb3_port_allocate_bandwidth() - Allocate bandwidth for USB3 |
| 1039 | * @port: USB3 adapter port |
| 1040 | * @upstream_bw: New upstream bandwidth |
| 1041 | * @downstream_bw: New downstream bandwidth |
| 1042 | * |
| 1043 | * This can be used to set how much bandwidth is allocated for the USB3 |
| 1044 | * tunneled isochronous traffic. @upstream_bw and @downstream_bw are the |
| 1045 | * new values programmed to the USB3 adapter allocation registers. If |
| 1046 | * the values are lower than what is currently consumed the allocation |
| 1047 | * is set to what is currently consumed instead (consumed bandwidth |
| 1048 | * cannot be taken away by CM). The actual new values are returned in |
| 1049 | * @upstream_bw and @downstream_bw. |
| 1050 | * |
| 1051 | * Returns %0 in case of success and negative errno if there was a |
| 1052 | * failure. |
| 1053 | */ |
| 1054 | int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw, |
| 1055 | int *downstream_bw) |
| 1056 | { |
| 1057 | int ret, consumed_up, consumed_down, allocate_up, allocate_down; |
| 1058 | |
| 1059 | ret = usb4_usb3_port_set_cm_request(port); |
| 1060 | if (ret) |
| 1061 | return ret; |
| 1062 | |
| 1063 | ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up, |
| 1064 | &consumed_down); |
| 1065 | if (ret) |
| 1066 | goto err_request; |
| 1067 | |
| 1068 | /* Don't allow it go lower than what is consumed */ |
| 1069 | allocate_up = max(*upstream_bw, consumed_up); |
| 1070 | allocate_down = max(*downstream_bw, consumed_down); |
| 1071 | |
| 1072 | ret = usb4_usb3_port_write_allocated_bandwidth(port, allocate_up, |
| 1073 | allocate_down); |
| 1074 | if (ret) |
| 1075 | goto err_request; |
| 1076 | |
| 1077 | *upstream_bw = allocate_up; |
| 1078 | *downstream_bw = allocate_down; |
| 1079 | |
| 1080 | err_request: |
| 1081 | usb4_usb3_port_clear_cm_request(port); |
| 1082 | return ret; |
| 1083 | } |
| 1084 | |
| 1085 | /** |
| 1086 | * usb4_usb3_port_release_bandwidth() - Release allocated USB3 bandwidth |
| 1087 | * @port: USB3 adapter port |
| 1088 | * @upstream_bw: New allocated upstream bandwidth |
| 1089 | * @downstream_bw: New allocated downstream bandwidth |
| 1090 | * |
| 1091 | * Releases USB3 allocated bandwidth down to what is actually consumed. |
| 1092 | * The new bandwidth is returned in @upstream_bw and @downstream_bw. |
| 1093 | * |
| 1094 | * Returns 0% in success and negative errno in case of failure. |
| 1095 | */ |
| 1096 | int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw, |
| 1097 | int *downstream_bw) |
| 1098 | { |
| 1099 | int ret, consumed_up, consumed_down; |
| 1100 | |
| 1101 | ret = usb4_usb3_port_set_cm_request(port); |
| 1102 | if (ret) |
| 1103 | return ret; |
| 1104 | |
| 1105 | ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up, |
| 1106 | &consumed_down); |
| 1107 | if (ret) |
| 1108 | goto err_request; |
| 1109 | |
| 1110 | /* |
| 1111 | * Always keep 1000 Mb/s to make sure xHCI has at least some |
| 1112 | * bandwidth available for isochronous traffic. |
| 1113 | */ |
| 1114 | if (consumed_up < 1000) |
| 1115 | consumed_up = 1000; |
| 1116 | if (consumed_down < 1000) |
| 1117 | consumed_down = 1000; |
| 1118 | |
| 1119 | ret = usb4_usb3_port_write_allocated_bandwidth(port, consumed_up, |
| 1120 | consumed_down); |
| 1121 | if (ret) |
| 1122 | goto err_request; |
| 1123 | |
| 1124 | *upstream_bw = consumed_up; |
| 1125 | *downstream_bw = consumed_down; |
| 1126 | |
| 1127 | err_request: |
| 1128 | usb4_usb3_port_clear_cm_request(port); |
| 1129 | return ret; |
| 1130 | } |