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