1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt/USB4 retimer support.
5 * Copyright (C) 2020, Intel Corporation
6 * Authors: Kranthi Kuntala <kranthi.kuntala@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
10 #include <linux/delay.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/sched/signal.h>
17 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING)
18 #define TB_MAX_RETIMER_INDEX 6
20 #define TB_MAX_RETIMER_INDEX 2
24 * tb_retimer_nvm_read() - Read contents of retimer NVM
26 * @address: NVM address (in bytes) to start reading
27 * @buf: Data read from NVM is stored here
28 * @size: Number of bytes to read
30 * Reads retimer NVM and copies the contents to @buf. Returns %0 if the
31 * read was successful and negative errno in case of failure.
33 int tb_retimer_nvm_read(struct tb_retimer
*rt
, unsigned int address
, void *buf
,
36 return usb4_port_retimer_nvm_read(rt
->port
, rt
->index
, address
, buf
, size
);
39 static int nvm_read(void *priv
, unsigned int offset
, void *val
, size_t bytes
)
41 struct tb_nvm
*nvm
= priv
;
42 struct tb_retimer
*rt
= tb_to_retimer(nvm
->dev
);
45 pm_runtime_get_sync(&rt
->dev
);
47 if (!mutex_trylock(&rt
->tb
->lock
)) {
48 ret
= restart_syscall();
52 ret
= tb_retimer_nvm_read(rt
, offset
, val
, bytes
);
53 mutex_unlock(&rt
->tb
->lock
);
56 pm_runtime_mark_last_busy(&rt
->dev
);
57 pm_runtime_put_autosuspend(&rt
->dev
);
62 static int nvm_write(void *priv
, unsigned int offset
, void *val
, size_t bytes
)
64 struct tb_nvm
*nvm
= priv
;
65 struct tb_retimer
*rt
= tb_to_retimer(nvm
->dev
);
68 if (!mutex_trylock(&rt
->tb
->lock
))
69 return restart_syscall();
71 ret
= tb_nvm_write_buf(nvm
, offset
, val
, bytes
);
72 mutex_unlock(&rt
->tb
->lock
);
77 static int tb_retimer_nvm_add(struct tb_retimer
*rt
)
82 nvm
= tb_nvm_alloc(&rt
->dev
);
84 ret
= PTR_ERR(nvm
) == -EOPNOTSUPP
? 0 : PTR_ERR(nvm
);
88 ret
= tb_nvm_read_version(nvm
);
92 ret
= tb_nvm_add_active(nvm
, nvm_read
);
96 ret
= tb_nvm_add_non_active(nvm
, nvm_write
);
101 dev_dbg(&rt
->dev
, "NVM version %x.%x\n", nvm
->major
, nvm
->minor
);
105 dev_dbg(&rt
->dev
, "NVM upgrade disabled\n");
106 rt
->no_nvm_upgrade
= true;
113 static int tb_retimer_nvm_validate_and_write(struct tb_retimer
*rt
)
115 unsigned int image_size
;
119 ret
= tb_nvm_validate(rt
->nvm
);
123 buf
= rt
->nvm
->buf_data_start
;
124 image_size
= rt
->nvm
->buf_data_size
;
126 ret
= usb4_port_retimer_nvm_write(rt
->port
, rt
->index
, 0, buf
,
131 rt
->nvm
->flushed
= true;
135 static int tb_retimer_nvm_authenticate(struct tb_retimer
*rt
, bool auth_only
)
141 ret
= usb4_port_retimer_nvm_set_offset(rt
->port
, rt
->index
, 0);
146 ret
= usb4_port_retimer_nvm_authenticate(rt
->port
, rt
->index
);
150 usleep_range(100, 150);
153 * Check the status now if we still can access the retimer. It
154 * is expected that the below fails.
156 ret
= usb4_port_retimer_nvm_authenticate_status(rt
->port
, rt
->index
,
159 rt
->auth_status
= status
;
160 return status
? -EINVAL
: 0;
166 static ssize_t
device_show(struct device
*dev
, struct device_attribute
*attr
,
169 struct tb_retimer
*rt
= tb_to_retimer(dev
);
171 return sysfs_emit(buf
, "%#x\n", rt
->device
);
173 static DEVICE_ATTR_RO(device
);
175 static ssize_t
nvm_authenticate_show(struct device
*dev
,
176 struct device_attribute
*attr
, char *buf
)
178 struct tb_retimer
*rt
= tb_to_retimer(dev
);
181 if (!mutex_trylock(&rt
->tb
->lock
))
182 return restart_syscall();
187 ret
= sysfs_emit(buf
, "%#x\n", rt
->auth_status
);
189 mutex_unlock(&rt
->tb
->lock
);
194 static void tb_retimer_nvm_authenticate_status(struct tb_port
*port
, u32
*status
)
198 tb_port_dbg(port
, "reading NVM authentication status of retimers\n");
201 * Before doing anything else, read the authentication status.
202 * If the retimer has it set, store it for the new retimer
205 for (i
= 1; i
<= TB_MAX_RETIMER_INDEX
; i
++) {
206 if (usb4_port_retimer_nvm_authenticate_status(port
, i
, &status
[i
]))
211 static void tb_retimer_set_inbound_sbtx(struct tb_port
*port
)
216 * When USB4 port is online sideband communications are
219 if (!usb4_port_device_is_offline(port
->usb4
))
222 tb_port_dbg(port
, "enabling sideband transactions\n");
224 for (i
= 1; i
<= TB_MAX_RETIMER_INDEX
; i
++)
225 usb4_port_retimer_set_inbound_sbtx(port
, i
);
228 static void tb_retimer_unset_inbound_sbtx(struct tb_port
*port
)
233 * When USB4 port is offline we need to keep the sideband
234 * communications up to make it possible to communicate with
235 * the connected retimers.
237 if (usb4_port_device_is_offline(port
->usb4
))
240 tb_port_dbg(port
, "disabling sideband transactions\n");
242 for (i
= TB_MAX_RETIMER_INDEX
; i
>= 1; i
--) {
243 if (usb4_port_retimer_unset_inbound_sbtx(port
, i
))
248 static ssize_t
nvm_authenticate_store(struct device
*dev
,
249 struct device_attribute
*attr
, const char *buf
, size_t count
)
251 struct tb_retimer
*rt
= tb_to_retimer(dev
);
254 pm_runtime_get_sync(&rt
->dev
);
256 if (!mutex_trylock(&rt
->tb
->lock
)) {
257 ret
= restart_syscall();
266 ret
= kstrtoint(buf
, 10, &val
);
270 /* Always clear status */
275 * When NVM authentication starts the retimer is not
276 * accessible so calling tb_retimer_unset_inbound_sbtx()
277 * will fail and therefore we do not call it. Exception
278 * is when the validation fails or we only write the new
279 * NVM image without authentication.
281 tb_retimer_set_inbound_sbtx(rt
->port
);
282 if (val
== AUTHENTICATE_ONLY
) {
283 ret
= tb_retimer_nvm_authenticate(rt
, true);
285 if (!rt
->nvm
->flushed
) {
291 ret
= tb_retimer_nvm_validate_and_write(rt
);
292 if (ret
|| val
== WRITE_ONLY
)
295 if (val
== WRITE_AND_AUTHENTICATE
)
296 ret
= tb_retimer_nvm_authenticate(rt
, false);
301 if (ret
|| val
== WRITE_ONLY
)
302 tb_retimer_unset_inbound_sbtx(rt
->port
);
303 mutex_unlock(&rt
->tb
->lock
);
305 pm_runtime_mark_last_busy(&rt
->dev
);
306 pm_runtime_put_autosuspend(&rt
->dev
);
312 static DEVICE_ATTR_RW(nvm_authenticate
);
314 static ssize_t
nvm_version_show(struct device
*dev
,
315 struct device_attribute
*attr
, char *buf
)
317 struct tb_retimer
*rt
= tb_to_retimer(dev
);
320 if (!mutex_trylock(&rt
->tb
->lock
))
321 return restart_syscall();
326 ret
= sysfs_emit(buf
, "%x.%x\n", rt
->nvm
->major
, rt
->nvm
->minor
);
328 mutex_unlock(&rt
->tb
->lock
);
331 static DEVICE_ATTR_RO(nvm_version
);
333 static ssize_t
vendor_show(struct device
*dev
, struct device_attribute
*attr
,
336 struct tb_retimer
*rt
= tb_to_retimer(dev
);
338 return sysfs_emit(buf
, "%#x\n", rt
->vendor
);
340 static DEVICE_ATTR_RO(vendor
);
342 static umode_t
retimer_is_visible(struct kobject
*kobj
, struct attribute
*attr
,
345 struct device
*dev
= kobj_to_dev(kobj
);
346 struct tb_retimer
*rt
= tb_to_retimer(dev
);
348 if (attr
== &dev_attr_nvm_authenticate
.attr
||
349 attr
== &dev_attr_nvm_version
.attr
)
350 return rt
->no_nvm_upgrade
? 0 : attr
->mode
;
355 static struct attribute
*retimer_attrs
[] = {
356 &dev_attr_device
.attr
,
357 &dev_attr_nvm_authenticate
.attr
,
358 &dev_attr_nvm_version
.attr
,
359 &dev_attr_vendor
.attr
,
363 static const struct attribute_group retimer_group
= {
364 .is_visible
= retimer_is_visible
,
365 .attrs
= retimer_attrs
,
368 static const struct attribute_group
*retimer_groups
[] = {
373 static void tb_retimer_release(struct device
*dev
)
375 struct tb_retimer
*rt
= tb_to_retimer(dev
);
380 const struct device_type tb_retimer_type
= {
381 .name
= "thunderbolt_retimer",
382 .groups
= retimer_groups
,
383 .release
= tb_retimer_release
,
386 static int tb_retimer_add(struct tb_port
*port
, u8 index
, u32 auth_status
,
389 struct tb_retimer
*rt
;
393 ret
= usb4_port_sb_read(port
, USB4_SB_TARGET_RETIMER
, index
,
394 USB4_SB_VENDOR_ID
, &vendor
, sizeof(vendor
));
397 tb_port_warn(port
, "failed read retimer VendorId: %d\n", ret
);
401 ret
= usb4_port_sb_read(port
, USB4_SB_TARGET_RETIMER
, index
,
402 USB4_SB_PRODUCT_ID
, &device
, sizeof(device
));
405 tb_port_warn(port
, "failed read retimer ProductId: %d\n", ret
);
410 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
417 rt
->auth_status
= auth_status
;
419 rt
->tb
= port
->sw
->tb
;
422 * Only support NVM upgrade for on-board retimers. The retimers
423 * on the other side of the connection.
425 if (!on_board
|| usb4_port_retimer_nvm_sector_size(port
, index
) <= 0)
426 rt
->no_nvm_upgrade
= true;
428 rt
->dev
.parent
= &port
->usb4
->dev
;
429 rt
->dev
.bus
= &tb_bus_type
;
430 rt
->dev
.type
= &tb_retimer_type
;
431 dev_set_name(&rt
->dev
, "%s:%u.%u", dev_name(&port
->sw
->dev
),
434 ret
= device_register(&rt
->dev
);
436 dev_err(&rt
->dev
, "failed to register retimer: %d\n", ret
);
437 put_device(&rt
->dev
);
441 ret
= tb_retimer_nvm_add(rt
);
443 dev_err(&rt
->dev
, "failed to add NVM devices: %d\n", ret
);
444 device_unregister(&rt
->dev
);
448 dev_info(&rt
->dev
, "new retimer found, vendor=%#x device=%#x\n",
449 rt
->vendor
, rt
->device
);
451 pm_runtime_no_callbacks(&rt
->dev
);
452 pm_runtime_set_active(&rt
->dev
);
453 pm_runtime_enable(&rt
->dev
);
454 pm_runtime_set_autosuspend_delay(&rt
->dev
, TB_AUTOSUSPEND_DELAY
);
455 pm_runtime_mark_last_busy(&rt
->dev
);
456 pm_runtime_use_autosuspend(&rt
->dev
);
458 tb_retimer_debugfs_init(rt
);
462 static void tb_retimer_remove(struct tb_retimer
*rt
)
464 dev_info(&rt
->dev
, "retimer disconnected\n");
465 tb_retimer_debugfs_remove(rt
);
466 tb_nvm_free(rt
->nvm
);
467 device_unregister(&rt
->dev
);
470 struct tb_retimer_lookup
{
471 const struct tb_port
*port
;
475 static int retimer_match(struct device
*dev
, void *data
)
477 const struct tb_retimer_lookup
*lookup
= data
;
478 struct tb_retimer
*rt
= tb_to_retimer(dev
);
480 return rt
&& rt
->port
== lookup
->port
&& rt
->index
== lookup
->index
;
483 static struct tb_retimer
*tb_port_find_retimer(struct tb_port
*port
, u8 index
)
485 struct tb_retimer_lookup lookup
= { .port
= port
, .index
= index
};
488 dev
= device_find_child(&port
->usb4
->dev
, &lookup
, retimer_match
);
490 return tb_to_retimer(dev
);
496 * tb_retimer_scan() - Scan for on-board retimers under port
497 * @port: USB4 port to scan
498 * @add: If true also registers found retimers
500 * Brings the sideband into a state where retimers can be accessed.
501 * Then Tries to enumerate on-board retimers connected to @port. Found
502 * retimers are registered as children of @port if @add is set. Does
503 * not scan for cable retimers for now.
505 int tb_retimer_scan(struct tb_port
*port
, bool add
)
507 u32 status
[TB_MAX_RETIMER_INDEX
+ 1] = {};
508 int ret
, i
, max
, last_idx
= 0;
511 * Send broadcast RT to make sure retimer indices facing this
514 ret
= usb4_port_enumerate_retimers(port
);
519 * Immediately after sending enumerate retimers read the
520 * authentication status of each retimer.
522 tb_retimer_nvm_authenticate_status(port
, status
);
525 * Enable sideband channel for each retimer. We can do this
526 * regardless whether there is device connected or not.
528 tb_retimer_set_inbound_sbtx(port
);
530 for (max
= 1, i
= 1; i
<= TB_MAX_RETIMER_INDEX
; i
++) {
532 * Last retimer is true only for the last on-board
533 * retimer (the one connected directly to the Type-C
536 ret
= usb4_port_retimer_is_last(port
, i
);
546 if (!IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING
))
547 max
= min(last_idx
, max
);
549 /* Add retimers if they do not exist already */
550 for (i
= 1; i
<= max
; i
++) {
551 struct tb_retimer
*rt
;
553 /* Skip cable retimers */
554 if (usb4_port_retimer_is_cable(port
, i
))
557 rt
= tb_port_find_retimer(port
, i
);
559 put_device(&rt
->dev
);
561 ret
= tb_retimer_add(port
, i
, status
[i
], i
<= last_idx
);
562 if (ret
&& ret
!= -EOPNOTSUPP
)
567 tb_retimer_unset_inbound_sbtx(port
);
571 static int remove_retimer(struct device
*dev
, void *data
)
573 struct tb_retimer
*rt
= tb_to_retimer(dev
);
574 struct tb_port
*port
= data
;
576 if (rt
&& rt
->port
== port
)
577 tb_retimer_remove(rt
);
582 * tb_retimer_remove_all() - Remove all retimers under port
583 * @port: USB4 port whose retimers to remove
585 * This removes all previously added retimers under @port.
587 void tb_retimer_remove_all(struct tb_port
*port
)
589 struct usb4_port
*usb4
;
593 device_for_each_child_reverse(&usb4
->dev
, port
,