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