1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Thunderbolt/USB4 retimer support. 4 * 5 * Copyright (C) 2020, Intel Corporation 6 * Authors: Kranthi Kuntala <[email protected]> 7 * Mika Westerberg <[email protected]> 8 */ 9 10 #include <linux/delay.h> 11 #include <linux/pm_runtime.h> 12 #include <linux/sched/signal.h> 13 14 #include "sb_regs.h" 15 #include "tb.h" 16 17 #define TB_MAX_RETIMER_INDEX 6 18 19 /** 20 * tb_retimer_nvm_read() - Read contents of retimer NVM 21 * @rt: Retimer device 22 * @address: NVM address (in bytes) to start reading 23 * @buf: Data read from NVM is stored here 24 * @size: Number of bytes to read 25 * 26 * Reads retimer NVM and copies the contents to @buf. Returns %0 if the 27 * read was successful and negative errno in case of failure. 28 */ 29 int tb_retimer_nvm_read(struct tb_retimer *rt, unsigned int address, void *buf, 30 size_t size) 31 { 32 return usb4_port_retimer_nvm_read(rt->port, rt->index, address, buf, size); 33 } 34 35 static int nvm_read(void *priv, unsigned int offset, void *val, size_t bytes) 36 { 37 struct tb_nvm *nvm = priv; 38 struct tb_retimer *rt = tb_to_retimer(nvm->dev); 39 int ret; 40 41 pm_runtime_get_sync(&rt->dev); 42 43 if (!mutex_trylock(&rt->tb->lock)) { 44 ret = restart_syscall(); 45 goto out; 46 } 47 48 ret = tb_retimer_nvm_read(rt, offset, val, bytes); 49 mutex_unlock(&rt->tb->lock); 50 51 out: 52 pm_runtime_mark_last_busy(&rt->dev); 53 pm_runtime_put_autosuspend(&rt->dev); 54 55 return ret; 56 } 57 58 static int nvm_write(void *priv, unsigned int offset, void *val, size_t bytes) 59 { 60 struct tb_nvm *nvm = priv; 61 struct tb_retimer *rt = tb_to_retimer(nvm->dev); 62 int ret = 0; 63 64 if (!mutex_trylock(&rt->tb->lock)) 65 return restart_syscall(); 66 67 ret = tb_nvm_write_buf(nvm, offset, val, bytes); 68 mutex_unlock(&rt->tb->lock); 69 70 return ret; 71 } 72 73 static int tb_retimer_nvm_add(struct tb_retimer *rt) 74 { 75 struct tb_nvm *nvm; 76 int ret; 77 78 nvm = tb_nvm_alloc(&rt->dev); 79 if (IS_ERR(nvm)) { 80 ret = PTR_ERR(nvm) == -EOPNOTSUPP ? 0 : PTR_ERR(nvm); 81 goto err_nvm; 82 } 83 84 ret = tb_nvm_read_version(nvm); 85 if (ret) 86 goto err_nvm; 87 88 ret = tb_nvm_add_active(nvm, nvm_read); 89 if (ret) 90 goto err_nvm; 91 92 ret = tb_nvm_add_non_active(nvm, nvm_write); 93 if (ret) 94 goto err_nvm; 95 96 rt->nvm = nvm; 97 dev_dbg(&rt->dev, "NVM version %x.%x\n", nvm->major, nvm->minor); 98 return 0; 99 100 err_nvm: 101 dev_dbg(&rt->dev, "NVM upgrade disabled\n"); 102 if (!IS_ERR(nvm)) 103 tb_nvm_free(nvm); 104 105 return ret; 106 } 107 108 static int tb_retimer_nvm_validate_and_write(struct tb_retimer *rt) 109 { 110 unsigned int image_size; 111 const u8 *buf; 112 int ret; 113 114 ret = tb_nvm_validate(rt->nvm); 115 if (ret) 116 return ret; 117 118 buf = rt->nvm->buf_data_start; 119 image_size = rt->nvm->buf_data_size; 120 121 ret = usb4_port_retimer_nvm_write(rt->port, rt->index, 0, buf, 122 image_size); 123 if (ret) 124 return ret; 125 126 rt->nvm->flushed = true; 127 return 0; 128 } 129 130 static int tb_retimer_nvm_authenticate(struct tb_retimer *rt, bool auth_only) 131 { 132 u32 status; 133 int ret; 134 135 if (auth_only) { 136 ret = usb4_port_retimer_nvm_set_offset(rt->port, rt->index, 0); 137 if (ret) 138 return ret; 139 } 140 141 ret = usb4_port_retimer_nvm_authenticate(rt->port, rt->index); 142 if (ret) 143 return ret; 144 145 usleep_range(100, 150); 146 147 /* 148 * Check the status now if we still can access the retimer. It 149 * is expected that the below fails. 150 */ 151 ret = usb4_port_retimer_nvm_authenticate_status(rt->port, rt->index, 152 &status); 153 if (!ret) { 154 rt->auth_status = status; 155 return status ? -EINVAL : 0; 156 } 157 158 return 0; 159 } 160 161 static ssize_t device_show(struct device *dev, struct device_attribute *attr, 162 char *buf) 163 { 164 struct tb_retimer *rt = tb_to_retimer(dev); 165 166 return sysfs_emit(buf, "%#x\n", rt->device); 167 } 168 static DEVICE_ATTR_RO(device); 169 170 static ssize_t nvm_authenticate_show(struct device *dev, 171 struct device_attribute *attr, char *buf) 172 { 173 struct tb_retimer *rt = tb_to_retimer(dev); 174 int ret; 175 176 if (!mutex_trylock(&rt->tb->lock)) 177 return restart_syscall(); 178 179 if (!rt->nvm) 180 ret = -EAGAIN; 181 else if (rt->no_nvm_upgrade) 182 ret = -EOPNOTSUPP; 183 else 184 ret = sysfs_emit(buf, "%#x\n", rt->auth_status); 185 186 mutex_unlock(&rt->tb->lock); 187 188 return ret; 189 } 190 191 static void tb_retimer_nvm_authenticate_status(struct tb_port *port, u32 *status) 192 { 193 int i; 194 195 tb_port_dbg(port, "reading NVM authentication status of retimers\n"); 196 197 /* 198 * Before doing anything else, read the authentication status. 199 * If the retimer has it set, store it for the new retimer 200 * device instance. 201 */ 202 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) { 203 if (usb4_port_retimer_nvm_authenticate_status(port, i, &status[i])) 204 break; 205 } 206 } 207 208 static void tb_retimer_set_inbound_sbtx(struct tb_port *port) 209 { 210 int i; 211 212 /* 213 * When USB4 port is online sideband communications are 214 * already up. 215 */ 216 if (!usb4_port_device_is_offline(port->usb4)) 217 return; 218 219 tb_port_dbg(port, "enabling sideband transactions\n"); 220 221 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) 222 usb4_port_retimer_set_inbound_sbtx(port, i); 223 } 224 225 static void tb_retimer_unset_inbound_sbtx(struct tb_port *port) 226 { 227 int i; 228 229 /* 230 * When USB4 port is offline we need to keep the sideband 231 * communications up to make it possible to communicate with 232 * the connected retimers. 233 */ 234 if (usb4_port_device_is_offline(port->usb4)) 235 return; 236 237 tb_port_dbg(port, "disabling sideband transactions\n"); 238 239 for (i = TB_MAX_RETIMER_INDEX; i >= 1; i--) { 240 if (usb4_port_retimer_unset_inbound_sbtx(port, i)) 241 break; 242 } 243 } 244 245 static ssize_t nvm_authenticate_store(struct device *dev, 246 struct device_attribute *attr, const char *buf, size_t count) 247 { 248 struct tb_retimer *rt = tb_to_retimer(dev); 249 int val, ret; 250 251 pm_runtime_get_sync(&rt->dev); 252 253 if (!mutex_trylock(&rt->tb->lock)) { 254 ret = restart_syscall(); 255 goto exit_rpm; 256 } 257 258 if (!rt->nvm) { 259 ret = -EAGAIN; 260 goto exit_unlock; 261 } 262 263 ret = kstrtoint(buf, 10, &val); 264 if (ret) 265 goto exit_unlock; 266 267 /* Always clear status */ 268 rt->auth_status = 0; 269 270 if (val) { 271 /* 272 * When NVM authentication starts the retimer is not 273 * accessible so calling tb_retimer_unset_inbound_sbtx() 274 * will fail and therefore we do not call it. Exception 275 * is when the validation fails or we only write the new 276 * NVM image without authentication. 277 */ 278 tb_retimer_set_inbound_sbtx(rt->port); 279 if (val == AUTHENTICATE_ONLY) { 280 ret = tb_retimer_nvm_authenticate(rt, true); 281 } else { 282 if (!rt->nvm->flushed) { 283 if (!rt->nvm->buf) { 284 ret = -EINVAL; 285 goto exit_unlock; 286 } 287 288 ret = tb_retimer_nvm_validate_and_write(rt); 289 if (ret || val == WRITE_ONLY) 290 goto exit_unlock; 291 } 292 if (val == WRITE_AND_AUTHENTICATE) 293 ret = tb_retimer_nvm_authenticate(rt, false); 294 } 295 } 296 297 exit_unlock: 298 if (ret || val == WRITE_ONLY) 299 tb_retimer_unset_inbound_sbtx(rt->port); 300 mutex_unlock(&rt->tb->lock); 301 exit_rpm: 302 pm_runtime_mark_last_busy(&rt->dev); 303 pm_runtime_put_autosuspend(&rt->dev); 304 305 if (ret) 306 return ret; 307 return count; 308 } 309 static DEVICE_ATTR_RW(nvm_authenticate); 310 311 static ssize_t nvm_version_show(struct device *dev, 312 struct device_attribute *attr, char *buf) 313 { 314 struct tb_retimer *rt = tb_to_retimer(dev); 315 int ret; 316 317 if (!mutex_trylock(&rt->tb->lock)) 318 return restart_syscall(); 319 320 if (!rt->nvm) 321 ret = -EAGAIN; 322 else 323 ret = sysfs_emit(buf, "%x.%x\n", rt->nvm->major, rt->nvm->minor); 324 325 mutex_unlock(&rt->tb->lock); 326 return ret; 327 } 328 static DEVICE_ATTR_RO(nvm_version); 329 330 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr, 331 char *buf) 332 { 333 struct tb_retimer *rt = tb_to_retimer(dev); 334 335 return sysfs_emit(buf, "%#x\n", rt->vendor); 336 } 337 static DEVICE_ATTR_RO(vendor); 338 339 static struct attribute *retimer_attrs[] = { 340 &dev_attr_device.attr, 341 &dev_attr_nvm_authenticate.attr, 342 &dev_attr_nvm_version.attr, 343 &dev_attr_vendor.attr, 344 NULL 345 }; 346 347 static const struct attribute_group retimer_group = { 348 .attrs = retimer_attrs, 349 }; 350 351 static const struct attribute_group *retimer_groups[] = { 352 &retimer_group, 353 NULL 354 }; 355 356 static void tb_retimer_release(struct device *dev) 357 { 358 struct tb_retimer *rt = tb_to_retimer(dev); 359 360 kfree(rt); 361 } 362 363 const struct device_type tb_retimer_type = { 364 .name = "thunderbolt_retimer", 365 .groups = retimer_groups, 366 .release = tb_retimer_release, 367 }; 368 369 static int tb_retimer_add(struct tb_port *port, u8 index, u32 auth_status) 370 { 371 struct tb_retimer *rt; 372 u32 vendor, device; 373 int ret; 374 375 ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index, 376 USB4_SB_VENDOR_ID, &vendor, sizeof(vendor)); 377 if (ret) { 378 if (ret != -ENODEV) 379 tb_port_warn(port, "failed read retimer VendorId: %d\n", ret); 380 return ret; 381 } 382 383 ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index, 384 USB4_SB_PRODUCT_ID, &device, sizeof(device)); 385 if (ret) { 386 if (ret != -ENODEV) 387 tb_port_warn(port, "failed read retimer ProductId: %d\n", ret); 388 return ret; 389 } 390 391 /* 392 * Check that it supports NVM operations. If not then don't add 393 * the device at all. 394 */ 395 ret = usb4_port_retimer_nvm_sector_size(port, index); 396 if (ret < 0) 397 return ret; 398 399 rt = kzalloc(sizeof(*rt), GFP_KERNEL); 400 if (!rt) 401 return -ENOMEM; 402 403 rt->index = index; 404 rt->vendor = vendor; 405 rt->device = device; 406 rt->auth_status = auth_status; 407 rt->port = port; 408 rt->tb = port->sw->tb; 409 410 rt->dev.parent = &port->usb4->dev; 411 rt->dev.bus = &tb_bus_type; 412 rt->dev.type = &tb_retimer_type; 413 dev_set_name(&rt->dev, "%s:%u.%u", dev_name(&port->sw->dev), 414 port->port, index); 415 416 ret = device_register(&rt->dev); 417 if (ret) { 418 dev_err(&rt->dev, "failed to register retimer: %d\n", ret); 419 put_device(&rt->dev); 420 return ret; 421 } 422 423 ret = tb_retimer_nvm_add(rt); 424 if (ret) { 425 dev_err(&rt->dev, "failed to add NVM devices: %d\n", ret); 426 device_unregister(&rt->dev); 427 return ret; 428 } 429 430 dev_info(&rt->dev, "new retimer found, vendor=%#x device=%#x\n", 431 rt->vendor, rt->device); 432 433 pm_runtime_no_callbacks(&rt->dev); 434 pm_runtime_set_active(&rt->dev); 435 pm_runtime_enable(&rt->dev); 436 pm_runtime_set_autosuspend_delay(&rt->dev, TB_AUTOSUSPEND_DELAY); 437 pm_runtime_mark_last_busy(&rt->dev); 438 pm_runtime_use_autosuspend(&rt->dev); 439 440 tb_retimer_debugfs_init(rt); 441 return 0; 442 } 443 444 static void tb_retimer_remove(struct tb_retimer *rt) 445 { 446 dev_info(&rt->dev, "retimer disconnected\n"); 447 tb_retimer_debugfs_remove(rt); 448 tb_nvm_free(rt->nvm); 449 device_unregister(&rt->dev); 450 } 451 452 struct tb_retimer_lookup { 453 const struct tb_port *port; 454 u8 index; 455 }; 456 457 static int retimer_match(struct device *dev, void *data) 458 { 459 const struct tb_retimer_lookup *lookup = data; 460 struct tb_retimer *rt = tb_to_retimer(dev); 461 462 return rt && rt->port == lookup->port && rt->index == lookup->index; 463 } 464 465 static struct tb_retimer *tb_port_find_retimer(struct tb_port *port, u8 index) 466 { 467 struct tb_retimer_lookup lookup = { .port = port, .index = index }; 468 struct device *dev; 469 470 dev = device_find_child(&port->usb4->dev, &lookup, retimer_match); 471 if (dev) 472 return tb_to_retimer(dev); 473 474 return NULL; 475 } 476 477 /** 478 * tb_retimer_scan() - Scan for on-board retimers under port 479 * @port: USB4 port to scan 480 * @add: If true also registers found retimers 481 * 482 * Brings the sideband into a state where retimers can be accessed. 483 * Then Tries to enumerate on-board retimers connected to @port. Found 484 * retimers are registered as children of @port if @add is set. Does 485 * not scan for cable retimers for now. 486 */ 487 int tb_retimer_scan(struct tb_port *port, bool add) 488 { 489 u32 status[TB_MAX_RETIMER_INDEX + 1] = {}; 490 int ret, i, last_idx = 0; 491 492 /* 493 * Send broadcast RT to make sure retimer indices facing this 494 * port are set. 495 */ 496 ret = usb4_port_enumerate_retimers(port); 497 if (ret) 498 return ret; 499 500 /* 501 * Immediately after sending enumerate retimers read the 502 * authentication status of each retimer. 503 */ 504 tb_retimer_nvm_authenticate_status(port, status); 505 506 /* 507 * Enable sideband channel for each retimer. We can do this 508 * regardless whether there is device connected or not. 509 */ 510 tb_retimer_set_inbound_sbtx(port); 511 512 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) { 513 /* 514 * Last retimer is true only for the last on-board 515 * retimer (the one connected directly to the Type-C 516 * port). 517 */ 518 ret = usb4_port_retimer_is_last(port, i); 519 if (ret > 0) 520 last_idx = i; 521 else if (ret < 0) 522 break; 523 } 524 525 tb_retimer_unset_inbound_sbtx(port); 526 527 if (!last_idx) 528 return 0; 529 530 /* Add on-board retimers if they do not exist already */ 531 ret = 0; 532 for (i = 1; i <= last_idx; i++) { 533 struct tb_retimer *rt; 534 535 rt = tb_port_find_retimer(port, i); 536 if (rt) { 537 put_device(&rt->dev); 538 } else if (add) { 539 ret = tb_retimer_add(port, i, status[i]); 540 if (ret && ret != -EOPNOTSUPP) 541 break; 542 } 543 } 544 545 return ret; 546 } 547 548 static int remove_retimer(struct device *dev, void *data) 549 { 550 struct tb_retimer *rt = tb_to_retimer(dev); 551 struct tb_port *port = data; 552 553 if (rt && rt->port == port) 554 tb_retimer_remove(rt); 555 return 0; 556 } 557 558 /** 559 * tb_retimer_remove_all() - Remove all retimers under port 560 * @port: USB4 port whose retimers to remove 561 * 562 * This removes all previously added retimers under @port. 563 */ 564 void tb_retimer_remove_all(struct tb_port *port) 565 { 566 struct usb4_port *usb4; 567 568 usb4 = port->usb4; 569 if (usb4) 570 device_for_each_child_reverse(&usb4->dev, port, 571 remove_retimer); 572 } 573