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 u32 val, nvm_size; 77 int ret; 78 79 nvm = tb_nvm_alloc(&rt->dev); 80 if (IS_ERR(nvm)) 81 return PTR_ERR(nvm); 82 83 ret = usb4_port_retimer_nvm_read(rt->port, rt->index, NVM_VERSION, &val, 84 sizeof(val)); 85 if (ret) 86 goto err_nvm; 87 88 nvm->major = (val >> 16) & 0xff; 89 nvm->minor = (val >> 8) & 0xff; 90 91 ret = usb4_port_retimer_nvm_read(rt->port, rt->index, NVM_FLASH_SIZE, 92 &val, sizeof(val)); 93 if (ret) 94 goto err_nvm; 95 96 nvm_size = (SZ_1M << (val & 7)) / 8; 97 nvm_size = (nvm_size - SZ_16K) / 2; 98 99 ret = tb_nvm_add_active(nvm, nvm_size, nvm_read); 100 if (ret) 101 goto err_nvm; 102 103 ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE, nvm_write); 104 if (ret) 105 goto err_nvm; 106 107 rt->nvm = nvm; 108 return 0; 109 110 err_nvm: 111 tb_nvm_free(nvm); 112 return ret; 113 } 114 115 static int tb_retimer_nvm_validate_and_write(struct tb_retimer *rt) 116 { 117 unsigned int image_size, hdr_size; 118 const u8 *buf = rt->nvm->buf; 119 u16 ds_size, device; 120 int ret; 121 122 image_size = rt->nvm->buf_data_size; 123 if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE) 124 return -EINVAL; 125 126 /* 127 * FARB pointer must point inside the image and must at least 128 * contain parts of the digital section we will be reading here. 129 */ 130 hdr_size = (*(u32 *)buf) & 0xffffff; 131 if (hdr_size + NVM_DEVID + 2 >= image_size) 132 return -EINVAL; 133 134 /* Digital section start should be aligned to 4k page */ 135 if (!IS_ALIGNED(hdr_size, SZ_4K)) 136 return -EINVAL; 137 138 /* 139 * Read digital section size and check that it also fits inside 140 * the image. 141 */ 142 ds_size = *(u16 *)(buf + hdr_size); 143 if (ds_size >= image_size) 144 return -EINVAL; 145 146 /* 147 * Make sure the device ID in the image matches the retimer 148 * hardware. 149 */ 150 device = *(u16 *)(buf + hdr_size + NVM_DEVID); 151 if (device != rt->device) 152 return -EINVAL; 153 154 /* Skip headers in the image */ 155 buf += hdr_size; 156 image_size -= hdr_size; 157 158 ret = usb4_port_retimer_nvm_write(rt->port, rt->index, 0, buf, 159 image_size); 160 if (!ret) 161 rt->nvm->flushed = true; 162 163 return ret; 164 } 165 166 static int tb_retimer_nvm_authenticate(struct tb_retimer *rt, bool auth_only) 167 { 168 u32 status; 169 int ret; 170 171 if (auth_only) { 172 ret = usb4_port_retimer_nvm_set_offset(rt->port, rt->index, 0); 173 if (ret) 174 return ret; 175 } 176 177 ret = usb4_port_retimer_nvm_authenticate(rt->port, rt->index); 178 if (ret) 179 return ret; 180 181 usleep_range(100, 150); 182 183 /* 184 * Check the status now if we still can access the retimer. It 185 * is expected that the below fails. 186 */ 187 ret = usb4_port_retimer_nvm_authenticate_status(rt->port, rt->index, 188 &status); 189 if (!ret) { 190 rt->auth_status = status; 191 return status ? -EINVAL : 0; 192 } 193 194 return 0; 195 } 196 197 static ssize_t device_show(struct device *dev, struct device_attribute *attr, 198 char *buf) 199 { 200 struct tb_retimer *rt = tb_to_retimer(dev); 201 202 return sprintf(buf, "%#x\n", rt->device); 203 } 204 static DEVICE_ATTR_RO(device); 205 206 static ssize_t nvm_authenticate_show(struct device *dev, 207 struct device_attribute *attr, char *buf) 208 { 209 struct tb_retimer *rt = tb_to_retimer(dev); 210 int ret; 211 212 if (!mutex_trylock(&rt->tb->lock)) 213 return restart_syscall(); 214 215 if (!rt->nvm) 216 ret = -EAGAIN; 217 else 218 ret = sprintf(buf, "%#x\n", rt->auth_status); 219 220 mutex_unlock(&rt->tb->lock); 221 222 return ret; 223 } 224 225 static ssize_t nvm_authenticate_store(struct device *dev, 226 struct device_attribute *attr, const char *buf, size_t count) 227 { 228 struct tb_retimer *rt = tb_to_retimer(dev); 229 int val, ret; 230 231 pm_runtime_get_sync(&rt->dev); 232 233 if (!mutex_trylock(&rt->tb->lock)) { 234 ret = restart_syscall(); 235 goto exit_rpm; 236 } 237 238 if (!rt->nvm) { 239 ret = -EAGAIN; 240 goto exit_unlock; 241 } 242 243 ret = kstrtoint(buf, 10, &val); 244 if (ret) 245 goto exit_unlock; 246 247 /* Always clear status */ 248 rt->auth_status = 0; 249 250 if (val) { 251 if (val == AUTHENTICATE_ONLY) { 252 ret = tb_retimer_nvm_authenticate(rt, true); 253 } else { 254 if (!rt->nvm->flushed) { 255 if (!rt->nvm->buf) { 256 ret = -EINVAL; 257 goto exit_unlock; 258 } 259 260 ret = tb_retimer_nvm_validate_and_write(rt); 261 if (ret || val == WRITE_ONLY) 262 goto exit_unlock; 263 } 264 if (val == WRITE_AND_AUTHENTICATE) 265 ret = tb_retimer_nvm_authenticate(rt, false); 266 } 267 } 268 269 exit_unlock: 270 mutex_unlock(&rt->tb->lock); 271 exit_rpm: 272 pm_runtime_mark_last_busy(&rt->dev); 273 pm_runtime_put_autosuspend(&rt->dev); 274 275 if (ret) 276 return ret; 277 return count; 278 } 279 static DEVICE_ATTR_RW(nvm_authenticate); 280 281 static ssize_t nvm_version_show(struct device *dev, 282 struct device_attribute *attr, char *buf) 283 { 284 struct tb_retimer *rt = tb_to_retimer(dev); 285 int ret; 286 287 if (!mutex_trylock(&rt->tb->lock)) 288 return restart_syscall(); 289 290 if (!rt->nvm) 291 ret = -EAGAIN; 292 else 293 ret = sprintf(buf, "%x.%x\n", rt->nvm->major, rt->nvm->minor); 294 295 mutex_unlock(&rt->tb->lock); 296 return ret; 297 } 298 static DEVICE_ATTR_RO(nvm_version); 299 300 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr, 301 char *buf) 302 { 303 struct tb_retimer *rt = tb_to_retimer(dev); 304 305 return sprintf(buf, "%#x\n", rt->vendor); 306 } 307 static DEVICE_ATTR_RO(vendor); 308 309 static struct attribute *retimer_attrs[] = { 310 &dev_attr_device.attr, 311 &dev_attr_nvm_authenticate.attr, 312 &dev_attr_nvm_version.attr, 313 &dev_attr_vendor.attr, 314 NULL 315 }; 316 317 static const struct attribute_group retimer_group = { 318 .attrs = retimer_attrs, 319 }; 320 321 static const struct attribute_group *retimer_groups[] = { 322 &retimer_group, 323 NULL 324 }; 325 326 static void tb_retimer_release(struct device *dev) 327 { 328 struct tb_retimer *rt = tb_to_retimer(dev); 329 330 kfree(rt); 331 } 332 333 struct device_type tb_retimer_type = { 334 .name = "thunderbolt_retimer", 335 .groups = retimer_groups, 336 .release = tb_retimer_release, 337 }; 338 339 static int tb_retimer_add(struct tb_port *port, u8 index, u32 auth_status) 340 { 341 struct tb_retimer *rt; 342 u32 vendor, device; 343 int ret; 344 345 ret = usb4_port_retimer_read(port, index, USB4_SB_VENDOR_ID, &vendor, 346 sizeof(vendor)); 347 if (ret) { 348 if (ret != -ENODEV) 349 tb_port_warn(port, "failed read retimer VendorId: %d\n", ret); 350 return ret; 351 } 352 353 ret = usb4_port_retimer_read(port, index, USB4_SB_PRODUCT_ID, &device, 354 sizeof(device)); 355 if (ret) { 356 if (ret != -ENODEV) 357 tb_port_warn(port, "failed read retimer ProductId: %d\n", ret); 358 return ret; 359 } 360 361 if (vendor != PCI_VENDOR_ID_INTEL && vendor != 0x8087) { 362 tb_port_info(port, "retimer NVM format of vendor %#x is not supported\n", 363 vendor); 364 return -EOPNOTSUPP; 365 } 366 367 /* 368 * Check that it supports NVM operations. If not then don't add 369 * the device at all. 370 */ 371 ret = usb4_port_retimer_nvm_sector_size(port, index); 372 if (ret < 0) 373 return ret; 374 375 rt = kzalloc(sizeof(*rt), GFP_KERNEL); 376 if (!rt) 377 return -ENOMEM; 378 379 rt->index = index; 380 rt->vendor = vendor; 381 rt->device = device; 382 rt->auth_status = auth_status; 383 rt->port = port; 384 rt->tb = port->sw->tb; 385 386 rt->dev.parent = &port->usb4->dev; 387 rt->dev.bus = &tb_bus_type; 388 rt->dev.type = &tb_retimer_type; 389 dev_set_name(&rt->dev, "%s:%u.%u", dev_name(&port->sw->dev), 390 port->port, index); 391 392 ret = device_register(&rt->dev); 393 if (ret) { 394 dev_err(&rt->dev, "failed to register retimer: %d\n", ret); 395 put_device(&rt->dev); 396 return ret; 397 } 398 399 ret = tb_retimer_nvm_add(rt); 400 if (ret) { 401 dev_err(&rt->dev, "failed to add NVM devices: %d\n", ret); 402 device_unregister(&rt->dev); 403 return ret; 404 } 405 406 dev_info(&rt->dev, "new retimer found, vendor=%#x device=%#x\n", 407 rt->vendor, rt->device); 408 409 pm_runtime_no_callbacks(&rt->dev); 410 pm_runtime_set_active(&rt->dev); 411 pm_runtime_enable(&rt->dev); 412 pm_runtime_set_autosuspend_delay(&rt->dev, TB_AUTOSUSPEND_DELAY); 413 pm_runtime_mark_last_busy(&rt->dev); 414 pm_runtime_use_autosuspend(&rt->dev); 415 416 return 0; 417 } 418 419 static void tb_retimer_remove(struct tb_retimer *rt) 420 { 421 dev_info(&rt->dev, "retimer disconnected\n"); 422 tb_nvm_free(rt->nvm); 423 device_unregister(&rt->dev); 424 } 425 426 struct tb_retimer_lookup { 427 const struct tb_port *port; 428 u8 index; 429 }; 430 431 static int retimer_match(struct device *dev, void *data) 432 { 433 const struct tb_retimer_lookup *lookup = data; 434 struct tb_retimer *rt = tb_to_retimer(dev); 435 436 return rt && rt->port == lookup->port && rt->index == lookup->index; 437 } 438 439 static struct tb_retimer *tb_port_find_retimer(struct tb_port *port, u8 index) 440 { 441 struct tb_retimer_lookup lookup = { .port = port, .index = index }; 442 struct device *dev; 443 444 dev = device_find_child(&port->usb4->dev, &lookup, retimer_match); 445 if (dev) 446 return tb_to_retimer(dev); 447 448 return NULL; 449 } 450 451 /** 452 * tb_retimer_scan() - Scan for on-board retimers under port 453 * @port: USB4 port to scan 454 * @add: If true also registers found retimers 455 * 456 * Brings the sideband into a state where retimers can be accessed. 457 * Then Tries to enumerate on-board retimers connected to @port. Found 458 * retimers are registered as children of @port if @add is set. Does 459 * not scan for cable retimers for now. 460 */ 461 int tb_retimer_scan(struct tb_port *port, bool add) 462 { 463 u32 status[TB_MAX_RETIMER_INDEX + 1] = {}; 464 int ret, i, last_idx = 0; 465 struct usb4_port *usb4; 466 467 usb4 = port->usb4; 468 if (!usb4) 469 return 0; 470 471 pm_runtime_get_sync(&usb4->dev); 472 473 /* 474 * Send broadcast RT to make sure retimer indices facing this 475 * port are set. 476 */ 477 ret = usb4_port_enumerate_retimers(port); 478 if (ret) 479 goto out; 480 481 /* 482 * Enable sideband channel for each retimer. We can do this 483 * regardless whether there is device connected or not. 484 */ 485 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) 486 usb4_port_retimer_set_inbound_sbtx(port, i); 487 488 /* 489 * Before doing anything else, read the authentication status. 490 * If the retimer has it set, store it for the new retimer 491 * device instance. 492 */ 493 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) 494 usb4_port_retimer_nvm_authenticate_status(port, i, &status[i]); 495 496 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) { 497 /* 498 * Last retimer is true only for the last on-board 499 * retimer (the one connected directly to the Type-C 500 * port). 501 */ 502 ret = usb4_port_retimer_is_last(port, i); 503 if (ret > 0) 504 last_idx = i; 505 else if (ret < 0) 506 break; 507 } 508 509 if (!last_idx) { 510 ret = 0; 511 goto out; 512 } 513 514 /* Add on-board retimers if they do not exist already */ 515 for (i = 1; i <= last_idx; i++) { 516 struct tb_retimer *rt; 517 518 rt = tb_port_find_retimer(port, i); 519 if (rt) { 520 put_device(&rt->dev); 521 } else if (add) { 522 ret = tb_retimer_add(port, i, status[i]); 523 if (ret && ret != -EOPNOTSUPP) 524 break; 525 } 526 } 527 528 out: 529 pm_runtime_mark_last_busy(&usb4->dev); 530 pm_runtime_put_autosuspend(&usb4->dev); 531 532 return ret; 533 } 534 535 static int remove_retimer(struct device *dev, void *data) 536 { 537 struct tb_retimer *rt = tb_to_retimer(dev); 538 struct tb_port *port = data; 539 540 if (rt && rt->port == port) 541 tb_retimer_remove(rt); 542 return 0; 543 } 544 545 /** 546 * tb_retimer_remove_all() - Remove all retimers under port 547 * @port: USB4 port whose retimers to remove 548 * 549 * This removes all previously added retimers under @port. 550 */ 551 void tb_retimer_remove_all(struct tb_port *port) 552 { 553 struct usb4_port *usb4; 554 555 usb4 = port->usb4; 556 if (usb4) 557 device_for_each_child_reverse(&usb4->dev, port, 558 remove_retimer); 559 } 560