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
19 static int tb_retimer_nvm_read(void *priv, unsigned int offset, void *val,
22 struct tb_nvm *nvm = priv;
23 struct tb_retimer *rt = tb_to_retimer(nvm->dev);
26 pm_runtime_get_sync(&rt->dev);
28 if (!mutex_trylock(&rt->tb->lock)) {
29 ret = restart_syscall();
33 ret = usb4_port_retimer_nvm_read(rt->port, rt->index, offset, val, bytes);
34 mutex_unlock(&rt->tb->lock);
37 pm_runtime_mark_last_busy(&rt->dev);
38 pm_runtime_put_autosuspend(&rt->dev);
43 static int tb_retimer_nvm_write(void *priv, unsigned int offset, void *val,
46 struct tb_nvm *nvm = priv;
47 struct tb_retimer *rt = tb_to_retimer(nvm->dev);
50 if (!mutex_trylock(&rt->tb->lock))
51 return restart_syscall();
53 ret = tb_nvm_write_buf(nvm, offset, val, bytes);
54 mutex_unlock(&rt->tb->lock);
59 static int tb_retimer_nvm_add(struct tb_retimer *rt)
65 nvm = tb_nvm_alloc(&rt->dev);
69 ret = usb4_port_retimer_nvm_read(rt->port, rt->index, NVM_VERSION, &val,
74 nvm->major = val >> 16;
75 nvm->minor = val >> 8;
77 ret = usb4_port_retimer_nvm_read(rt->port, rt->index, NVM_FLASH_SIZE,
82 nvm_size = (SZ_1M << (val & 7)) / 8;
83 nvm_size = (nvm_size - SZ_16K) / 2;
85 ret = tb_nvm_add_active(nvm, nvm_size, tb_retimer_nvm_read);
89 ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE, tb_retimer_nvm_write);
101 static int tb_retimer_nvm_validate_and_write(struct tb_retimer *rt)
103 unsigned int image_size, hdr_size;
104 const u8 *buf = rt->nvm->buf;
108 image_size = rt->nvm->buf_data_size;
109 if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE)
113 * FARB pointer must point inside the image and must at least
114 * contain parts of the digital section we will be reading here.
116 hdr_size = (*(u32 *)buf) & 0xffffff;
117 if (hdr_size + NVM_DEVID + 2 >= image_size)
120 /* Digital section start should be aligned to 4k page */
121 if (!IS_ALIGNED(hdr_size, SZ_4K))
125 * Read digital section size and check that it also fits inside
128 ds_size = *(u16 *)(buf + hdr_size);
129 if (ds_size >= image_size)
133 * Make sure the device ID in the image matches the retimer
136 device = *(u16 *)(buf + hdr_size + NVM_DEVID);
137 if (device != rt->device)
140 /* Skip headers in the image */
142 image_size -= hdr_size;
144 ret = usb4_port_retimer_nvm_write(rt->port, rt->index, 0, buf,
147 rt->nvm->flushed = true;
152 static int tb_retimer_nvm_authenticate(struct tb_retimer *rt, bool auth_only)
157 ret = usb4_port_retimer_nvm_set_offset(rt->port, rt->index, 0);
162 return usb4_port_retimer_nvm_authenticate(rt->port, rt->index);
165 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
168 struct tb_retimer *rt = tb_to_retimer(dev);
170 return sprintf(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();
186 ret = sprintf(buf, "%#x\n", rt->auth_status);
188 mutex_unlock(&rt->tb->lock);
193 static ssize_t nvm_authenticate_store(struct device *dev,
194 struct device_attribute *attr, const char *buf, size_t count)
196 struct tb_retimer *rt = tb_to_retimer(dev);
199 pm_runtime_get_sync(&rt->dev);
201 if (!mutex_trylock(&rt->tb->lock)) {
202 ret = restart_syscall();
211 ret = kstrtoint(buf, 10, &val);
215 /* Always clear status */
219 if (val == AUTHENTICATE_ONLY) {
220 ret = tb_retimer_nvm_authenticate(rt, true);
222 if (!rt->nvm->flushed) {
228 ret = tb_retimer_nvm_validate_and_write(rt);
229 if (ret || val == WRITE_ONLY)
232 if (val == WRITE_AND_AUTHENTICATE)
233 ret = tb_retimer_nvm_authenticate(rt, false);
238 mutex_unlock(&rt->tb->lock);
240 pm_runtime_mark_last_busy(&rt->dev);
241 pm_runtime_put_autosuspend(&rt->dev);
247 static DEVICE_ATTR_RW(nvm_authenticate);
249 static ssize_t nvm_version_show(struct device *dev,
250 struct device_attribute *attr, char *buf)
252 struct tb_retimer *rt = tb_to_retimer(dev);
255 if (!mutex_trylock(&rt->tb->lock))
256 return restart_syscall();
261 ret = sprintf(buf, "%x.%x\n", rt->nvm->major, rt->nvm->minor);
263 mutex_unlock(&rt->tb->lock);
266 static DEVICE_ATTR_RO(nvm_version);
268 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
271 struct tb_retimer *rt = tb_to_retimer(dev);
273 return sprintf(buf, "%#x\n", rt->vendor);
275 static DEVICE_ATTR_RO(vendor);
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,
285 static const struct attribute_group retimer_group = {
286 .attrs = retimer_attrs,
289 static const struct attribute_group *retimer_groups[] = {
294 static void tb_retimer_release(struct device *dev)
296 struct tb_retimer *rt = tb_to_retimer(dev);
301 struct device_type tb_retimer_type = {
302 .name = "thunderbolt_retimer",
303 .groups = retimer_groups,
304 .release = tb_retimer_release,
307 static int tb_retimer_add(struct tb_port *port, u8 index, u32 auth_status)
309 struct usb4_port *usb4;
310 struct tb_retimer *rt;
318 ret = usb4_port_retimer_read(port, index, USB4_SB_VENDOR_ID, &vendor,
322 tb_port_warn(port, "failed read retimer VendorId: %d\n", ret);
326 ret = usb4_port_retimer_read(port, index, USB4_SB_PRODUCT_ID, &device,
330 tb_port_warn(port, "failed read retimer ProductId: %d\n", ret);
334 if (vendor != PCI_VENDOR_ID_INTEL && vendor != 0x8087) {
335 tb_port_info(port, "retimer NVM format of vendor %#x is not supported\n",
341 * Check that it supports NVM operations. If not then don't add
344 ret = usb4_port_retimer_nvm_sector_size(port, index);
348 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
355 rt->auth_status = auth_status;
357 rt->tb = port->sw->tb;
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),
365 ret = device_register(&rt->dev);
367 dev_err(&rt->dev, "failed to register retimer: %d\n", ret);
368 put_device(&rt->dev);
372 ret = tb_retimer_nvm_add(rt);
374 dev_err(&rt->dev, "failed to add NVM devices: %d\n", ret);
375 device_unregister(&rt->dev);
379 dev_info(&rt->dev, "new retimer found, vendor=%#x device=%#x\n",
380 rt->vendor, rt->device);
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);
392 static void tb_retimer_remove(struct tb_retimer *rt)
394 dev_info(&rt->dev, "retimer disconnected\n");
395 tb_nvm_free(rt->nvm);
396 device_unregister(&rt->dev);
399 struct tb_retimer_lookup {
400 const struct tb_port *port;
404 static int retimer_match(struct device *dev, void *data)
406 const struct tb_retimer_lookup *lookup = data;
407 struct tb_retimer *rt = tb_to_retimer(dev);
409 return rt && rt->port == lookup->port && rt->index == lookup->index;
412 static struct tb_retimer *tb_port_find_retimer(struct tb_port *port, u8 index)
414 struct tb_retimer_lookup lookup = { .port = port, .index = index };
417 dev = device_find_child(&port->usb4->dev, &lookup, retimer_match);
419 return tb_to_retimer(dev);
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
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.
434 int tb_retimer_scan(struct tb_port *port, bool add)
436 u32 status[TB_MAX_RETIMER_INDEX + 1] = {};
437 int ret, i, last_idx = 0;
440 * Send broadcast RT to make sure retimer indices facing this
443 ret = usb4_port_enumerate_retimers(port);
448 * Enable sideband channel for each retimer. We can do this
449 * regardless whether there is device connected or not.
451 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
452 usb4_port_retimer_set_inbound_sbtx(port, i);
455 * Before doing anything else, read the authentication status.
456 * If the retimer has it set, store it for the new retimer
459 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
460 usb4_port_retimer_nvm_authenticate_status(port, i, &status[i]);
462 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) {
464 * Last retimer is true only for the last on-board
465 * retimer (the one connected directly to the Type-C
468 ret = usb4_port_retimer_is_last(port, i);
478 /* Add on-board retimers if they do not exist already */
479 for (i = 1; i <= last_idx; i++) {
480 struct tb_retimer *rt;
482 rt = tb_port_find_retimer(port, i);
484 put_device(&rt->dev);
486 ret = tb_retimer_add(port, i, status[i]);
487 if (ret && ret != -EOPNOTSUPP)
495 static int remove_retimer(struct device *dev, void *data)
497 struct tb_retimer *rt = tb_to_retimer(dev);
498 struct tb_port *port = data;
500 if (rt && rt->port == port)
501 tb_retimer_remove(rt);
506 * tb_retimer_remove_all() - Remove all retimers under port
507 * @port: USB4 port whose retimers to remove
509 * This removes all previously added retimers under @port.
511 void tb_retimer_remove_all(struct tb_port *port)
513 struct usb4_port *usb4;
517 device_for_each_child_reverse(&usb4->dev, port,