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 #define TB_MAX_RETIMER_INDEX 6
20 * tb_retimer_nvm_read() - Read contents of retimer NVM
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
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.
29 int tb_retimer_nvm_read(struct tb_retimer *rt, unsigned int address, void *buf,
32 return usb4_port_retimer_nvm_read(rt->port, rt->index, address, buf, size);
35 static int nvm_read(void *priv, unsigned int offset, void *val, size_t bytes)
37 struct tb_nvm *nvm = priv;
38 struct tb_retimer *rt = tb_to_retimer(nvm->dev);
41 pm_runtime_get_sync(&rt->dev);
43 if (!mutex_trylock(&rt->tb->lock)) {
44 ret = restart_syscall();
48 ret = tb_retimer_nvm_read(rt, offset, val, bytes);
49 mutex_unlock(&rt->tb->lock);
52 pm_runtime_mark_last_busy(&rt->dev);
53 pm_runtime_put_autosuspend(&rt->dev);
58 static int nvm_write(void *priv, unsigned int offset, void *val, size_t bytes)
60 struct tb_nvm *nvm = priv;
61 struct tb_retimer *rt = tb_to_retimer(nvm->dev);
64 if (!mutex_trylock(&rt->tb->lock))
65 return restart_syscall();
67 ret = tb_nvm_write_buf(nvm, offset, val, bytes);
68 mutex_unlock(&rt->tb->lock);
73 static int tb_retimer_nvm_add(struct tb_retimer *rt)
78 nvm = tb_nvm_alloc(&rt->dev);
80 ret = PTR_ERR(nvm) == -EOPNOTSUPP ? 0 : PTR_ERR(nvm);
84 ret = tb_nvm_read_version(nvm);
88 ret = tb_nvm_add_active(nvm, nvm_read);
92 ret = tb_nvm_add_non_active(nvm, nvm_write);
97 dev_dbg(&rt->dev, "NVM version %x.%x\n", nvm->major, nvm->minor);
101 dev_dbg(&rt->dev, "NVM upgrade disabled\n");
108 static int tb_retimer_nvm_validate_and_write(struct tb_retimer *rt)
110 unsigned int image_size;
114 ret = tb_nvm_validate(rt->nvm);
118 buf = rt->nvm->buf_data_start;
119 image_size = rt->nvm->buf_data_size;
121 ret = usb4_port_retimer_nvm_write(rt->port, rt->index, 0, buf,
126 rt->nvm->flushed = true;
130 static int tb_retimer_nvm_authenticate(struct tb_retimer *rt, bool auth_only)
136 ret = usb4_port_retimer_nvm_set_offset(rt->port, rt->index, 0);
141 ret = usb4_port_retimer_nvm_authenticate(rt->port, rt->index);
145 usleep_range(100, 150);
148 * Check the status now if we still can access the retimer. It
149 * is expected that the below fails.
151 ret = usb4_port_retimer_nvm_authenticate_status(rt->port, rt->index,
154 rt->auth_status = status;
155 return status ? -EINVAL : 0;
161 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
164 struct tb_retimer *rt = tb_to_retimer(dev);
166 return sysfs_emit(buf, "%#x\n", rt->device);
168 static DEVICE_ATTR_RO(device);
170 static ssize_t nvm_authenticate_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
173 struct tb_retimer *rt = tb_to_retimer(dev);
176 if (!mutex_trylock(&rt->tb->lock))
177 return restart_syscall();
181 else if (rt->no_nvm_upgrade)
184 ret = sysfs_emit(buf, "%#x\n", rt->auth_status);
186 mutex_unlock(&rt->tb->lock);
191 static void tb_retimer_nvm_authenticate_status(struct tb_port *port, u32 *status)
195 tb_port_dbg(port, "reading NVM authentication status of retimers\n");
198 * Before doing anything else, read the authentication status.
199 * If the retimer has it set, store it for the new retimer
202 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
203 usb4_port_retimer_nvm_authenticate_status(port, i, &status[i]);
206 static void tb_retimer_set_inbound_sbtx(struct tb_port *port)
211 * When USB4 port is online sideband communications are
214 if (!usb4_port_device_is_offline(port->usb4))
217 tb_port_dbg(port, "enabling sideband transactions\n");
219 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
220 usb4_port_retimer_set_inbound_sbtx(port, i);
223 static void tb_retimer_unset_inbound_sbtx(struct tb_port *port)
228 * When USB4 port is offline we need to keep the sideband
229 * communications up to make it possible to communicate with
230 * the connected retimers.
232 if (usb4_port_device_is_offline(port->usb4))
235 tb_port_dbg(port, "disabling sideband transactions\n");
237 for (i = TB_MAX_RETIMER_INDEX; i >= 1; i--)
238 usb4_port_retimer_unset_inbound_sbtx(port, i);
241 static ssize_t nvm_authenticate_store(struct device *dev,
242 struct device_attribute *attr, const char *buf, size_t count)
244 struct tb_retimer *rt = tb_to_retimer(dev);
247 pm_runtime_get_sync(&rt->dev);
249 if (!mutex_trylock(&rt->tb->lock)) {
250 ret = restart_syscall();
259 ret = kstrtoint(buf, 10, &val);
263 /* Always clear status */
268 * When NVM authentication starts the retimer is not
269 * accessible so calling tb_retimer_unset_inbound_sbtx()
270 * will fail and therefore we do not call it. Exception
271 * is when the validation fails or we only write the new
272 * NVM image without authentication.
274 tb_retimer_set_inbound_sbtx(rt->port);
275 if (val == AUTHENTICATE_ONLY) {
276 ret = tb_retimer_nvm_authenticate(rt, true);
278 if (!rt->nvm->flushed) {
284 ret = tb_retimer_nvm_validate_and_write(rt);
285 if (ret || val == WRITE_ONLY)
288 if (val == WRITE_AND_AUTHENTICATE)
289 ret = tb_retimer_nvm_authenticate(rt, false);
294 if (ret || val == WRITE_ONLY)
295 tb_retimer_unset_inbound_sbtx(rt->port);
296 mutex_unlock(&rt->tb->lock);
298 pm_runtime_mark_last_busy(&rt->dev);
299 pm_runtime_put_autosuspend(&rt->dev);
305 static DEVICE_ATTR_RW(nvm_authenticate);
307 static ssize_t nvm_version_show(struct device *dev,
308 struct device_attribute *attr, char *buf)
310 struct tb_retimer *rt = tb_to_retimer(dev);
313 if (!mutex_trylock(&rt->tb->lock))
314 return restart_syscall();
319 ret = sysfs_emit(buf, "%x.%x\n", rt->nvm->major, rt->nvm->minor);
321 mutex_unlock(&rt->tb->lock);
324 static DEVICE_ATTR_RO(nvm_version);
326 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
329 struct tb_retimer *rt = tb_to_retimer(dev);
331 return sysfs_emit(buf, "%#x\n", rt->vendor);
333 static DEVICE_ATTR_RO(vendor);
335 static struct attribute *retimer_attrs[] = {
336 &dev_attr_device.attr,
337 &dev_attr_nvm_authenticate.attr,
338 &dev_attr_nvm_version.attr,
339 &dev_attr_vendor.attr,
343 static const struct attribute_group retimer_group = {
344 .attrs = retimer_attrs,
347 static const struct attribute_group *retimer_groups[] = {
352 static void tb_retimer_release(struct device *dev)
354 struct tb_retimer *rt = tb_to_retimer(dev);
359 struct device_type tb_retimer_type = {
360 .name = "thunderbolt_retimer",
361 .groups = retimer_groups,
362 .release = tb_retimer_release,
365 static int tb_retimer_add(struct tb_port *port, u8 index, u32 auth_status)
367 struct tb_retimer *rt;
371 ret = usb4_port_retimer_read(port, index, USB4_SB_VENDOR_ID, &vendor,
375 tb_port_warn(port, "failed read retimer VendorId: %d\n", ret);
379 ret = usb4_port_retimer_read(port, index, USB4_SB_PRODUCT_ID, &device,
383 tb_port_warn(port, "failed read retimer ProductId: %d\n", ret);
388 * Check that it supports NVM operations. If not then don't add
391 ret = usb4_port_retimer_nvm_sector_size(port, index);
395 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
402 rt->auth_status = auth_status;
404 rt->tb = port->sw->tb;
406 rt->dev.parent = &port->usb4->dev;
407 rt->dev.bus = &tb_bus_type;
408 rt->dev.type = &tb_retimer_type;
409 dev_set_name(&rt->dev, "%s:%u.%u", dev_name(&port->sw->dev),
412 ret = device_register(&rt->dev);
414 dev_err(&rt->dev, "failed to register retimer: %d\n", ret);
415 put_device(&rt->dev);
419 ret = tb_retimer_nvm_add(rt);
421 dev_err(&rt->dev, "failed to add NVM devices: %d\n", ret);
422 device_unregister(&rt->dev);
426 dev_info(&rt->dev, "new retimer found, vendor=%#x device=%#x\n",
427 rt->vendor, rt->device);
429 pm_runtime_no_callbacks(&rt->dev);
430 pm_runtime_set_active(&rt->dev);
431 pm_runtime_enable(&rt->dev);
432 pm_runtime_set_autosuspend_delay(&rt->dev, TB_AUTOSUSPEND_DELAY);
433 pm_runtime_mark_last_busy(&rt->dev);
434 pm_runtime_use_autosuspend(&rt->dev);
439 static void tb_retimer_remove(struct tb_retimer *rt)
441 dev_info(&rt->dev, "retimer disconnected\n");
442 tb_nvm_free(rt->nvm);
443 device_unregister(&rt->dev);
446 struct tb_retimer_lookup {
447 const struct tb_port *port;
451 static int retimer_match(struct device *dev, void *data)
453 const struct tb_retimer_lookup *lookup = data;
454 struct tb_retimer *rt = tb_to_retimer(dev);
456 return rt && rt->port == lookup->port && rt->index == lookup->index;
459 static struct tb_retimer *tb_port_find_retimer(struct tb_port *port, u8 index)
461 struct tb_retimer_lookup lookup = { .port = port, .index = index };
464 dev = device_find_child(&port->usb4->dev, &lookup, retimer_match);
466 return tb_to_retimer(dev);
472 * tb_retimer_scan() - Scan for on-board retimers under port
473 * @port: USB4 port to scan
474 * @add: If true also registers found retimers
476 * Brings the sideband into a state where retimers can be accessed.
477 * Then Tries to enumerate on-board retimers connected to @port. Found
478 * retimers are registered as children of @port if @add is set. Does
479 * not scan for cable retimers for now.
481 int tb_retimer_scan(struct tb_port *port, bool add)
483 u32 status[TB_MAX_RETIMER_INDEX + 1] = {};
484 int ret, i, last_idx = 0;
487 * Send broadcast RT to make sure retimer indices facing this
490 ret = usb4_port_enumerate_retimers(port);
495 * Immediately after sending enumerate retimers read the
496 * authentication status of each retimer.
498 tb_retimer_nvm_authenticate_status(port, status);
501 * Enable sideband channel for each retimer. We can do this
502 * regardless whether there is device connected or not.
504 tb_retimer_set_inbound_sbtx(port);
506 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) {
508 * Last retimer is true only for the last on-board
509 * retimer (the one connected directly to the Type-C
512 ret = usb4_port_retimer_is_last(port, i);
519 tb_retimer_unset_inbound_sbtx(port);
524 /* Add on-board retimers if they do not exist already */
526 for (i = 1; i <= last_idx; i++) {
527 struct tb_retimer *rt;
529 rt = tb_port_find_retimer(port, i);
531 put_device(&rt->dev);
533 ret = tb_retimer_add(port, i, status[i]);
534 if (ret && ret != -EOPNOTSUPP)
542 static int remove_retimer(struct device *dev, void *data)
544 struct tb_retimer *rt = tb_to_retimer(dev);
545 struct tb_port *port = data;
547 if (rt && rt->port == port)
548 tb_retimer_remove(rt);
553 * tb_retimer_remove_all() - Remove all retimers under port
554 * @port: USB4 port whose retimers to remove
556 * This removes all previously added retimers under @port.
558 void tb_retimer_remove_all(struct tb_port *port)
560 struct usb4_port *usb4;
564 device_for_each_child_reverse(&usb4->dev, port,