1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt/USB4 retimer support.
5 * Copyright (C) 2020, Intel Corporation
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");
112 static int tb_retimer_nvm_validate_and_write(struct tb_retimer *rt)
114 unsigned int image_size;
118 ret = tb_nvm_validate(rt->nvm);
122 buf = rt->nvm->buf_data_start;
123 image_size = rt->nvm->buf_data_size;
125 ret = usb4_port_retimer_nvm_write(rt->port, rt->index, 0, buf,
130 rt->nvm->flushed = true;
134 static int tb_retimer_nvm_authenticate(struct tb_retimer *rt, bool auth_only)
140 ret = usb4_port_retimer_nvm_set_offset(rt->port, rt->index, 0);
145 ret = usb4_port_retimer_nvm_authenticate(rt->port, rt->index);
149 usleep_range(100, 150);
152 * Check the status now if we still can access the retimer. It
153 * is expected that the below fails.
155 ret = usb4_port_retimer_nvm_authenticate_status(rt->port, rt->index,
158 rt->auth_status = status;
159 return status ? -EINVAL : 0;
165 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
168 struct tb_retimer *rt = tb_to_retimer(dev);
170 return sysfs_emit(buf, "%#x\n", rt->device);
172 static DEVICE_ATTR_RO(device);
174 static ssize_t nvm_authenticate_show(struct device *dev,
175 struct device_attribute *attr, char *buf)
177 struct tb_retimer *rt = tb_to_retimer(dev);
180 if (!mutex_trylock(&rt->tb->lock))
181 return restart_syscall();
185 else if (rt->no_nvm_upgrade)
188 ret = sysfs_emit(buf, "%#x\n", rt->auth_status);
190 mutex_unlock(&rt->tb->lock);
195 static void tb_retimer_nvm_authenticate_status(struct tb_port *port, u32 *status)
199 tb_port_dbg(port, "reading NVM authentication status of retimers\n");
202 * Before doing anything else, read the authentication status.
203 * If the retimer has it set, store it for the new retimer
206 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) {
207 if (usb4_port_retimer_nvm_authenticate_status(port, i, &status[i]))
212 static void tb_retimer_set_inbound_sbtx(struct tb_port *port)
217 * When USB4 port is online sideband communications are
220 if (!usb4_port_device_is_offline(port->usb4))
223 tb_port_dbg(port, "enabling sideband transactions\n");
225 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
226 usb4_port_retimer_set_inbound_sbtx(port, i);
229 static void tb_retimer_unset_inbound_sbtx(struct tb_port *port)
234 * When USB4 port is offline we need to keep the sideband
235 * communications up to make it possible to communicate with
236 * the connected retimers.
238 if (usb4_port_device_is_offline(port->usb4))
241 tb_port_dbg(port, "disabling sideband transactions\n");
243 for (i = TB_MAX_RETIMER_INDEX; i >= 1; i--) {
244 if (usb4_port_retimer_unset_inbound_sbtx(port, i))
249 static ssize_t nvm_authenticate_store(struct device *dev,
250 struct device_attribute *attr, const char *buf, size_t count)
252 struct tb_retimer *rt = tb_to_retimer(dev);
255 pm_runtime_get_sync(&rt->dev);
257 if (!mutex_trylock(&rt->tb->lock)) {
258 ret = restart_syscall();
267 ret = kstrtoint(buf, 10, &val);
271 /* Always clear status */
276 * When NVM authentication starts the retimer is not
277 * accessible so calling tb_retimer_unset_inbound_sbtx()
278 * will fail and therefore we do not call it. Exception
279 * is when the validation fails or we only write the new
280 * NVM image without authentication.
282 tb_retimer_set_inbound_sbtx(rt->port);
283 if (val == AUTHENTICATE_ONLY) {
284 ret = tb_retimer_nvm_authenticate(rt, true);
286 if (!rt->nvm->flushed) {
292 ret = tb_retimer_nvm_validate_and_write(rt);
293 if (ret || val == WRITE_ONLY)
296 if (val == WRITE_AND_AUTHENTICATE)
297 ret = tb_retimer_nvm_authenticate(rt, false);
302 if (ret || val == WRITE_ONLY)
303 tb_retimer_unset_inbound_sbtx(rt->port);
304 mutex_unlock(&rt->tb->lock);
306 pm_runtime_mark_last_busy(&rt->dev);
307 pm_runtime_put_autosuspend(&rt->dev);
313 static DEVICE_ATTR_RW(nvm_authenticate);
315 static ssize_t nvm_version_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct tb_retimer *rt = tb_to_retimer(dev);
321 if (!mutex_trylock(&rt->tb->lock))
322 return restart_syscall();
326 else if (rt->no_nvm_upgrade)
329 ret = sysfs_emit(buf, "%x.%x\n", rt->nvm->major, rt->nvm->minor);
331 mutex_unlock(&rt->tb->lock);
334 static DEVICE_ATTR_RO(nvm_version);
336 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
339 struct tb_retimer *rt = tb_to_retimer(dev);
341 return sysfs_emit(buf, "%#x\n", rt->vendor);
343 static DEVICE_ATTR_RO(vendor);
345 static struct attribute *retimer_attrs[] = {
346 &dev_attr_device.attr,
347 &dev_attr_nvm_authenticate.attr,
348 &dev_attr_nvm_version.attr,
349 &dev_attr_vendor.attr,
353 static const struct attribute_group retimer_group = {
354 .attrs = retimer_attrs,
357 static const struct attribute_group *retimer_groups[] = {
362 static void tb_retimer_release(struct device *dev)
364 struct tb_retimer *rt = tb_to_retimer(dev);
369 const struct device_type tb_retimer_type = {
370 .name = "thunderbolt_retimer",
371 .groups = retimer_groups,
372 .release = tb_retimer_release,
375 static int tb_retimer_add(struct tb_port *port, u8 index, u32 auth_status,
378 struct tb_retimer *rt;
382 ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
383 USB4_SB_VENDOR_ID, &vendor, sizeof(vendor));
386 tb_port_warn(port, "failed read retimer VendorId: %d\n", ret);
390 ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
391 USB4_SB_PRODUCT_ID, &device, sizeof(device));
394 tb_port_warn(port, "failed read retimer ProductId: %d\n", ret);
399 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
406 rt->auth_status = auth_status;
408 rt->tb = port->sw->tb;
411 * Only support NVM upgrade for on-board retimers. The retimers
412 * on the other side of the connection.
414 if (!on_board || usb4_port_retimer_nvm_sector_size(port, index) <= 0)
415 rt->no_nvm_upgrade = true;
417 rt->dev.parent = &port->usb4->dev;
418 rt->dev.bus = &tb_bus_type;
419 rt->dev.type = &tb_retimer_type;
420 dev_set_name(&rt->dev, "%s:%u.%u", dev_name(&port->sw->dev),
423 ret = device_register(&rt->dev);
425 dev_err(&rt->dev, "failed to register retimer: %d\n", ret);
426 put_device(&rt->dev);
430 ret = tb_retimer_nvm_add(rt);
432 dev_err(&rt->dev, "failed to add NVM devices: %d\n", ret);
433 device_unregister(&rt->dev);
437 dev_info(&rt->dev, "new retimer found, vendor=%#x device=%#x\n",
438 rt->vendor, rt->device);
440 pm_runtime_no_callbacks(&rt->dev);
441 pm_runtime_set_active(&rt->dev);
442 pm_runtime_enable(&rt->dev);
443 pm_runtime_set_autosuspend_delay(&rt->dev, TB_AUTOSUSPEND_DELAY);
444 pm_runtime_mark_last_busy(&rt->dev);
445 pm_runtime_use_autosuspend(&rt->dev);
447 tb_retimer_debugfs_init(rt);
451 static void tb_retimer_remove(struct tb_retimer *rt)
453 dev_info(&rt->dev, "retimer disconnected\n");
454 tb_retimer_debugfs_remove(rt);
455 tb_nvm_free(rt->nvm);
456 device_unregister(&rt->dev);
459 struct tb_retimer_lookup {
460 const struct tb_port *port;
464 static int retimer_match(struct device *dev, void *data)
466 const struct tb_retimer_lookup *lookup = data;
467 struct tb_retimer *rt = tb_to_retimer(dev);
469 return rt && rt->port == lookup->port && rt->index == lookup->index;
472 static struct tb_retimer *tb_port_find_retimer(struct tb_port *port, u8 index)
474 struct tb_retimer_lookup lookup = { .port = port, .index = index };
477 dev = device_find_child(&port->usb4->dev, &lookup, retimer_match);
479 return tb_to_retimer(dev);
485 * tb_retimer_scan() - Scan for on-board retimers under port
486 * @port: USB4 port to scan
487 * @add: If true also registers found retimers
489 * Brings the sideband into a state where retimers can be accessed.
490 * Then Tries to enumerate on-board retimers connected to @port. Found
491 * retimers are registered as children of @port if @add is set. Does
492 * not scan for cable retimers for now.
494 int tb_retimer_scan(struct tb_port *port, bool add)
496 u32 status[TB_MAX_RETIMER_INDEX + 1] = {};
497 int ret, i, max, last_idx = 0;
500 * Send broadcast RT to make sure retimer indices facing this
503 ret = usb4_port_enumerate_retimers(port);
508 * Immediately after sending enumerate retimers read the
509 * authentication status of each retimer.
511 tb_retimer_nvm_authenticate_status(port, status);
514 * Enable sideband channel for each retimer. We can do this
515 * regardless whether there is device connected or not.
517 tb_retimer_set_inbound_sbtx(port);
519 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) {
521 * Last retimer is true only for the last on-board
522 * retimer (the one connected directly to the Type-C
525 ret = usb4_port_retimer_is_last(port, i);
535 /* Add retimers if they do not exist already */
536 for (i = 1; i <= max; i++) {
537 struct tb_retimer *rt;
539 /* Skip cable retimers */
540 if (usb4_port_retimer_is_cable(port, i))
543 rt = tb_port_find_retimer(port, i);
545 put_device(&rt->dev);
547 ret = tb_retimer_add(port, i, status[i], i <= last_idx);
548 if (ret && ret != -EOPNOTSUPP)
553 tb_retimer_unset_inbound_sbtx(port);
557 static int remove_retimer(struct device *dev, void *data)
559 struct tb_retimer *rt = tb_to_retimer(dev);
560 struct tb_port *port = data;
562 if (rt && rt->port == port)
563 tb_retimer_remove(rt);
568 * tb_retimer_remove_all() - Remove all retimers under port
569 * @port: USB4 port whose retimers to remove
571 * This removes all previously added retimers under @port.
573 void tb_retimer_remove_all(struct tb_port *port)
575 struct usb4_port *usb4;
579 device_for_each_child_reverse(&usb4->dev, port,