Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / thunderbolt / retimer.c
blob89d2919d0193e8f5c68e669d054f3efc7abf78c8
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
8 */
10 #include <linux/delay.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/sched/signal.h>
14 #include "sb_regs.h"
15 #include "tb.h"
17 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING)
18 #define TB_MAX_RETIMER_INDEX 6
19 #else
20 #define TB_MAX_RETIMER_INDEX 2
21 #endif
23 /**
24 * tb_retimer_nvm_read() - Read contents of retimer NVM
25 * @rt: Retimer device
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,
34 size_t size)
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);
43 int ret;
45 pm_runtime_get_sync(&rt->dev);
47 if (!mutex_trylock(&rt->tb->lock)) {
48 ret = restart_syscall();
49 goto out;
52 ret = tb_retimer_nvm_read(rt, offset, val, bytes);
53 mutex_unlock(&rt->tb->lock);
55 out:
56 pm_runtime_mark_last_busy(&rt->dev);
57 pm_runtime_put_autosuspend(&rt->dev);
59 return ret;
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);
66 int ret = 0;
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);
74 return ret;
77 static int tb_retimer_nvm_add(struct tb_retimer *rt)
79 struct tb_nvm *nvm;
80 int ret;
82 nvm = tb_nvm_alloc(&rt->dev);
83 if (IS_ERR(nvm)) {
84 ret = PTR_ERR(nvm) == -EOPNOTSUPP ? 0 : PTR_ERR(nvm);
85 goto err_nvm;
88 ret = tb_nvm_read_version(nvm);
89 if (ret)
90 goto err_nvm;
92 ret = tb_nvm_add_active(nvm, nvm_read);
93 if (ret)
94 goto err_nvm;
96 ret = tb_nvm_add_non_active(nvm, nvm_write);
97 if (ret)
98 goto err_nvm;
100 rt->nvm = nvm;
101 dev_dbg(&rt->dev, "NVM version %x.%x\n", nvm->major, nvm->minor);
102 return 0;
104 err_nvm:
105 dev_dbg(&rt->dev, "NVM upgrade disabled\n");
106 if (!IS_ERR(nvm))
107 tb_nvm_free(nvm);
109 return ret;
112 static int tb_retimer_nvm_validate_and_write(struct tb_retimer *rt)
114 unsigned int image_size;
115 const u8 *buf;
116 int ret;
118 ret = tb_nvm_validate(rt->nvm);
119 if (ret)
120 return ret;
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,
126 image_size);
127 if (ret)
128 return ret;
130 rt->nvm->flushed = true;
131 return 0;
134 static int tb_retimer_nvm_authenticate(struct tb_retimer *rt, bool auth_only)
136 u32 status;
137 int ret;
139 if (auth_only) {
140 ret = usb4_port_retimer_nvm_set_offset(rt->port, rt->index, 0);
141 if (ret)
142 return ret;
145 ret = usb4_port_retimer_nvm_authenticate(rt->port, rt->index);
146 if (ret)
147 return ret;
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,
156 &status);
157 if (!ret) {
158 rt->auth_status = status;
159 return status ? -EINVAL : 0;
162 return 0;
165 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
166 char *buf)
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);
178 int ret;
180 if (!mutex_trylock(&rt->tb->lock))
181 return restart_syscall();
183 if (!rt->nvm)
184 ret = -EAGAIN;
185 else if (rt->no_nvm_upgrade)
186 ret = -EOPNOTSUPP;
187 else
188 ret = sysfs_emit(buf, "%#x\n", rt->auth_status);
190 mutex_unlock(&rt->tb->lock);
192 return ret;
195 static void tb_retimer_nvm_authenticate_status(struct tb_port *port, u32 *status)
197 int i;
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
204 * device instance.
206 for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) {
207 if (usb4_port_retimer_nvm_authenticate_status(port, i, &status[i]))
208 break;
212 static void tb_retimer_set_inbound_sbtx(struct tb_port *port)
214 int i;
217 * When USB4 port is online sideband communications are
218 * already up.
220 if (!usb4_port_device_is_offline(port->usb4))
221 return;
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)
231 int i;
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))
239 return;
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))
245 break;
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);
253 int val, ret;
255 pm_runtime_get_sync(&rt->dev);
257 if (!mutex_trylock(&rt->tb->lock)) {
258 ret = restart_syscall();
259 goto exit_rpm;
262 if (!rt->nvm) {
263 ret = -EAGAIN;
264 goto exit_unlock;
267 ret = kstrtoint(buf, 10, &val);
268 if (ret)
269 goto exit_unlock;
271 /* Always clear status */
272 rt->auth_status = 0;
274 if (val) {
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);
285 } else {
286 if (!rt->nvm->flushed) {
287 if (!rt->nvm->buf) {
288 ret = -EINVAL;
289 goto exit_unlock;
292 ret = tb_retimer_nvm_validate_and_write(rt);
293 if (ret || val == WRITE_ONLY)
294 goto exit_unlock;
296 if (val == WRITE_AND_AUTHENTICATE)
297 ret = tb_retimer_nvm_authenticate(rt, false);
301 exit_unlock:
302 if (ret || val == WRITE_ONLY)
303 tb_retimer_unset_inbound_sbtx(rt->port);
304 mutex_unlock(&rt->tb->lock);
305 exit_rpm:
306 pm_runtime_mark_last_busy(&rt->dev);
307 pm_runtime_put_autosuspend(&rt->dev);
309 if (ret)
310 return ret;
311 return count;
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);
319 int ret;
321 if (!mutex_trylock(&rt->tb->lock))
322 return restart_syscall();
324 if (!rt->nvm)
325 ret = -EAGAIN;
326 else if (rt->no_nvm_upgrade)
327 ret = -EOPNOTSUPP;
328 else
329 ret = sysfs_emit(buf, "%x.%x\n", rt->nvm->major, rt->nvm->minor);
331 mutex_unlock(&rt->tb->lock);
332 return ret;
334 static DEVICE_ATTR_RO(nvm_version);
336 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
337 char *buf)
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,
350 NULL
353 static const struct attribute_group retimer_group = {
354 .attrs = retimer_attrs,
357 static const struct attribute_group *retimer_groups[] = {
358 &retimer_group,
359 NULL
362 static void tb_retimer_release(struct device *dev)
364 struct tb_retimer *rt = tb_to_retimer(dev);
366 kfree(rt);
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,
376 bool on_board)
378 struct tb_retimer *rt;
379 u32 vendor, device;
380 int ret;
382 ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
383 USB4_SB_VENDOR_ID, &vendor, sizeof(vendor));
384 if (ret) {
385 if (ret != -ENODEV)
386 tb_port_warn(port, "failed read retimer VendorId: %d\n", ret);
387 return ret;
390 ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
391 USB4_SB_PRODUCT_ID, &device, sizeof(device));
392 if (ret) {
393 if (ret != -ENODEV)
394 tb_port_warn(port, "failed read retimer ProductId: %d\n", ret);
395 return ret;
399 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
400 if (!rt)
401 return -ENOMEM;
403 rt->index = index;
404 rt->vendor = vendor;
405 rt->device = device;
406 rt->auth_status = auth_status;
407 rt->port = port;
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),
421 port->port, index);
423 ret = device_register(&rt->dev);
424 if (ret) {
425 dev_err(&rt->dev, "failed to register retimer: %d\n", ret);
426 put_device(&rt->dev);
427 return ret;
430 ret = tb_retimer_nvm_add(rt);
431 if (ret) {
432 dev_err(&rt->dev, "failed to add NVM devices: %d\n", ret);
433 device_unregister(&rt->dev);
434 return ret;
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);
448 return 0;
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;
461 u8 index;
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 };
475 struct device *dev;
477 dev = device_find_child(&port->usb4->dev, &lookup, retimer_match);
478 if (dev)
479 return tb_to_retimer(dev);
481 return NULL;
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
501 * port are set.
503 ret = usb4_port_enumerate_retimers(port);
504 if (ret)
505 return ret;
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 (max = 1, 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
523 * port).
525 ret = usb4_port_retimer_is_last(port, i);
526 if (ret > 0)
527 last_idx = i;
528 else if (ret < 0)
529 break;
531 max = i;
534 ret = 0;
535 if (!IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING))
536 max = min(last_idx, max);
538 /* Add retimers if they do not exist already */
539 for (i = 1; i <= max; i++) {
540 struct tb_retimer *rt;
542 /* Skip cable retimers */
543 if (usb4_port_retimer_is_cable(port, i))
544 continue;
546 rt = tb_port_find_retimer(port, i);
547 if (rt) {
548 put_device(&rt->dev);
549 } else if (add) {
550 ret = tb_retimer_add(port, i, status[i], i <= last_idx);
551 if (ret && ret != -EOPNOTSUPP)
552 break;
556 tb_retimer_unset_inbound_sbtx(port);
557 return ret;
560 static int remove_retimer(struct device *dev, void *data)
562 struct tb_retimer *rt = tb_to_retimer(dev);
563 struct tb_port *port = data;
565 if (rt && rt->port == port)
566 tb_retimer_remove(rt);
567 return 0;
571 * tb_retimer_remove_all() - Remove all retimers under port
572 * @port: USB4 port whose retimers to remove
574 * This removes all previously added retimers under @port.
576 void tb_retimer_remove_all(struct tb_port *port)
578 struct usb4_port *usb4;
580 usb4 = port->usb4;
581 if (usb4)
582 device_for_each_child_reverse(&usb4->dev, port,
583 remove_retimer);