Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Thunderbolt/USB4 retimer support.
  4 *
  5 * Copyright (C) 2020, Intel Corporation
  6 * Authors: Kranthi Kuntala <kranthi.kuntala@intel.com>
  7 *	    Mika Westerberg <mika.westerberg@linux.intel.com>
  8 */
  9
 10#include <linux/delay.h>
 11#include <linux/pm_runtime.h>
 12#include <linux/sched/signal.h>
 13
 14#include "sb_regs.h"
 15#include "tb.h"
 16
 
 17#define TB_MAX_RETIMER_INDEX	6
 
 
 
 18
 19/**
 20 * tb_retimer_nvm_read() - Read contents of retimer NVM
 21 * @rt: Retimer device
 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
 25 *
 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.
 28 */
 29int tb_retimer_nvm_read(struct tb_retimer *rt, unsigned int address, void *buf,
 30			size_t size)
 31{
 32	return usb4_port_retimer_nvm_read(rt->port, rt->index, address, buf, size);
 33}
 34
 35static int nvm_read(void *priv, unsigned int offset, void *val, size_t bytes)
 36{
 37	struct tb_nvm *nvm = priv;
 38	struct tb_retimer *rt = tb_to_retimer(nvm->dev);
 39	int ret;
 40
 41	pm_runtime_get_sync(&rt->dev);
 42
 43	if (!mutex_trylock(&rt->tb->lock)) {
 44		ret = restart_syscall();
 45		goto out;
 46	}
 47
 48	ret = tb_retimer_nvm_read(rt, offset, val, bytes);
 49	mutex_unlock(&rt->tb->lock);
 50
 51out:
 52	pm_runtime_mark_last_busy(&rt->dev);
 53	pm_runtime_put_autosuspend(&rt->dev);
 54
 55	return ret;
 56}
 57
 58static int nvm_write(void *priv, unsigned int offset, void *val, size_t bytes)
 59{
 60	struct tb_nvm *nvm = priv;
 61	struct tb_retimer *rt = tb_to_retimer(nvm->dev);
 62	int ret = 0;
 63
 64	if (!mutex_trylock(&rt->tb->lock))
 65		return restart_syscall();
 66
 67	ret = tb_nvm_write_buf(nvm, offset, val, bytes);
 68	mutex_unlock(&rt->tb->lock);
 69
 70	return ret;
 71}
 72
 73static int tb_retimer_nvm_add(struct tb_retimer *rt)
 74{
 75	struct tb_nvm *nvm;
 76	int ret;
 77
 78	nvm = tb_nvm_alloc(&rt->dev);
 79	if (IS_ERR(nvm)) {
 80		ret = PTR_ERR(nvm) == -EOPNOTSUPP ? 0 : PTR_ERR(nvm);
 81		goto err_nvm;
 82	}
 83
 84	ret = tb_nvm_read_version(nvm);
 85	if (ret)
 86		goto err_nvm;
 87
 88	ret = tb_nvm_add_active(nvm, nvm_read);
 89	if (ret)
 90		goto err_nvm;
 91
 92	ret = tb_nvm_add_non_active(nvm, nvm_write);
 93	if (ret)
 94		goto err_nvm;
 95
 96	rt->nvm = nvm;
 97	dev_dbg(&rt->dev, "NVM version %x.%x\n", nvm->major, nvm->minor);
 98	return 0;
 99
100err_nvm:
101	dev_dbg(&rt->dev, "NVM upgrade disabled\n");
 
102	if (!IS_ERR(nvm))
103		tb_nvm_free(nvm);
104
105	return ret;
106}
107
108static int tb_retimer_nvm_validate_and_write(struct tb_retimer *rt)
109{
110	unsigned int image_size;
111	const u8 *buf;
112	int ret;
113
114	ret = tb_nvm_validate(rt->nvm);
115	if (ret)
116		return ret;
117
118	buf = rt->nvm->buf_data_start;
119	image_size = rt->nvm->buf_data_size;
120
121	ret = usb4_port_retimer_nvm_write(rt->port, rt->index, 0, buf,
122					 image_size);
123	if (ret)
124		return ret;
125
126	rt->nvm->flushed = true;
127	return 0;
128}
129
130static int tb_retimer_nvm_authenticate(struct tb_retimer *rt, bool auth_only)
131{
132	u32 status;
133	int ret;
134
135	if (auth_only) {
136		ret = usb4_port_retimer_nvm_set_offset(rt->port, rt->index, 0);
137		if (ret)
138			return ret;
139	}
140
141	ret = usb4_port_retimer_nvm_authenticate(rt->port, rt->index);
142	if (ret)
143		return ret;
144
145	usleep_range(100, 150);
146
147	/*
148	 * Check the status now if we still can access the retimer. It
149	 * is expected that the below fails.
150	 */
151	ret = usb4_port_retimer_nvm_authenticate_status(rt->port, rt->index,
152							&status);
153	if (!ret) {
154		rt->auth_status = status;
155		return status ? -EINVAL : 0;
156	}
157
158	return 0;
159}
160
161static ssize_t device_show(struct device *dev, struct device_attribute *attr,
162			   char *buf)
163{
164	struct tb_retimer *rt = tb_to_retimer(dev);
165
166	return sysfs_emit(buf, "%#x\n", rt->device);
167}
168static DEVICE_ATTR_RO(device);
169
170static ssize_t nvm_authenticate_show(struct device *dev,
171	struct device_attribute *attr, char *buf)
172{
173	struct tb_retimer *rt = tb_to_retimer(dev);
174	int ret;
175
176	if (!mutex_trylock(&rt->tb->lock))
177		return restart_syscall();
178
179	if (!rt->nvm)
180		ret = -EAGAIN;
181	else if (rt->no_nvm_upgrade)
182		ret = -EOPNOTSUPP;
183	else
184		ret = sysfs_emit(buf, "%#x\n", rt->auth_status);
185
186	mutex_unlock(&rt->tb->lock);
187
188	return ret;
189}
190
191static void tb_retimer_nvm_authenticate_status(struct tb_port *port, u32 *status)
192{
193	int i;
194
195	tb_port_dbg(port, "reading NVM authentication status of retimers\n");
196
197	/*
198	 * Before doing anything else, read the authentication status.
199	 * If the retimer has it set, store it for the new retimer
200	 * device instance.
201	 */
202	for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
203		usb4_port_retimer_nvm_authenticate_status(port, i, &status[i]);
 
 
204}
205
206static void tb_retimer_set_inbound_sbtx(struct tb_port *port)
207{
208	int i;
209
210	/*
211	 * When USB4 port is online sideband communications are
212	 * already up.
213	 */
214	if (!usb4_port_device_is_offline(port->usb4))
215		return;
216
217	tb_port_dbg(port, "enabling sideband transactions\n");
218
219	for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
220		usb4_port_retimer_set_inbound_sbtx(port, i);
221}
222
223static void tb_retimer_unset_inbound_sbtx(struct tb_port *port)
224{
225	int i;
226
227	/*
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.
231	 */
232	if (usb4_port_device_is_offline(port->usb4))
233		return;
234
235	tb_port_dbg(port, "disabling sideband transactions\n");
236
237	for (i = TB_MAX_RETIMER_INDEX; i >= 1; i--)
238		usb4_port_retimer_unset_inbound_sbtx(port, i);
 
 
239}
240
241static ssize_t nvm_authenticate_store(struct device *dev,
242	struct device_attribute *attr, const char *buf, size_t count)
243{
244	struct tb_retimer *rt = tb_to_retimer(dev);
245	int val, ret;
246
247	pm_runtime_get_sync(&rt->dev);
248
249	if (!mutex_trylock(&rt->tb->lock)) {
250		ret = restart_syscall();
251		goto exit_rpm;
252	}
253
254	if (!rt->nvm) {
255		ret = -EAGAIN;
256		goto exit_unlock;
257	}
258
259	ret = kstrtoint(buf, 10, &val);
260	if (ret)
261		goto exit_unlock;
262
263	/* Always clear status */
264	rt->auth_status = 0;
265
266	if (val) {
267		/*
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.
273		 */
274		tb_retimer_set_inbound_sbtx(rt->port);
275		if (val == AUTHENTICATE_ONLY) {
276			ret = tb_retimer_nvm_authenticate(rt, true);
277		} else {
278			if (!rt->nvm->flushed) {
279				if (!rt->nvm->buf) {
280					ret = -EINVAL;
281					goto exit_unlock;
282				}
283
284				ret = tb_retimer_nvm_validate_and_write(rt);
285				if (ret || val == WRITE_ONLY)
286					goto exit_unlock;
287			}
288			if (val == WRITE_AND_AUTHENTICATE)
289				ret = tb_retimer_nvm_authenticate(rt, false);
290		}
291	}
292
293exit_unlock:
294	if (ret || val == WRITE_ONLY)
295		tb_retimer_unset_inbound_sbtx(rt->port);
296	mutex_unlock(&rt->tb->lock);
297exit_rpm:
298	pm_runtime_mark_last_busy(&rt->dev);
299	pm_runtime_put_autosuspend(&rt->dev);
300
301	if (ret)
302		return ret;
303	return count;
304}
305static DEVICE_ATTR_RW(nvm_authenticate);
306
307static ssize_t nvm_version_show(struct device *dev,
308				struct device_attribute *attr, char *buf)
309{
310	struct tb_retimer *rt = tb_to_retimer(dev);
311	int ret;
312
313	if (!mutex_trylock(&rt->tb->lock))
314		return restart_syscall();
315
316	if (!rt->nvm)
317		ret = -EAGAIN;
318	else
319		ret = sysfs_emit(buf, "%x.%x\n", rt->nvm->major, rt->nvm->minor);
320
321	mutex_unlock(&rt->tb->lock);
322	return ret;
323}
324static DEVICE_ATTR_RO(nvm_version);
325
326static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
327			   char *buf)
328{
329	struct tb_retimer *rt = tb_to_retimer(dev);
330
331	return sysfs_emit(buf, "%#x\n", rt->vendor);
332}
333static DEVICE_ATTR_RO(vendor);
334
 
 
 
 
 
 
 
 
 
 
 
 
 
335static 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,
340	NULL
341};
342
343static const struct attribute_group retimer_group = {
 
344	.attrs = retimer_attrs,
345};
346
347static const struct attribute_group *retimer_groups[] = {
348	&retimer_group,
349	NULL
350};
351
352static void tb_retimer_release(struct device *dev)
353{
354	struct tb_retimer *rt = tb_to_retimer(dev);
355
356	kfree(rt);
357}
358
359struct device_type tb_retimer_type = {
360	.name = "thunderbolt_retimer",
361	.groups = retimer_groups,
362	.release = tb_retimer_release,
363};
364
365static int tb_retimer_add(struct tb_port *port, u8 index, u32 auth_status)
 
366{
367	struct tb_retimer *rt;
368	u32 vendor, device;
369	int ret;
370
371	ret = usb4_port_retimer_read(port, index, USB4_SB_VENDOR_ID, &vendor,
372				     sizeof(vendor));
373	if (ret) {
374		if (ret != -ENODEV)
375			tb_port_warn(port, "failed read retimer VendorId: %d\n", ret);
376		return ret;
377	}
378
379	ret = usb4_port_retimer_read(port, index, USB4_SB_PRODUCT_ID, &device,
380				     sizeof(device));
381	if (ret) {
382		if (ret != -ENODEV)
383			tb_port_warn(port, "failed read retimer ProductId: %d\n", ret);
384		return ret;
385	}
386
387	/*
388	 * Check that it supports NVM operations. If not then don't add
389	 * the device at all.
390	 */
391	ret = usb4_port_retimer_nvm_sector_size(port, index);
392	if (ret < 0)
393		return ret;
394
395	rt = kzalloc(sizeof(*rt), GFP_KERNEL);
396	if (!rt)
397		return -ENOMEM;
398
399	rt->index = index;
400	rt->vendor = vendor;
401	rt->device = device;
402	rt->auth_status = auth_status;
403	rt->port = port;
404	rt->tb = port->sw->tb;
405
 
 
 
 
 
 
 
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),
410		     port->port, index);
411
412	ret = device_register(&rt->dev);
413	if (ret) {
414		dev_err(&rt->dev, "failed to register retimer: %d\n", ret);
415		put_device(&rt->dev);
416		return ret;
417	}
418
419	ret = tb_retimer_nvm_add(rt);
420	if (ret) {
421		dev_err(&rt->dev, "failed to add NVM devices: %d\n", ret);
422		device_unregister(&rt->dev);
423		return ret;
424	}
425
426	dev_info(&rt->dev, "new retimer found, vendor=%#x device=%#x\n",
427		 rt->vendor, rt->device);
428
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);
435
 
436	return 0;
437}
438
439static void tb_retimer_remove(struct tb_retimer *rt)
440{
441	dev_info(&rt->dev, "retimer disconnected\n");
 
442	tb_nvm_free(rt->nvm);
443	device_unregister(&rt->dev);
444}
445
446struct tb_retimer_lookup {
447	const struct tb_port *port;
448	u8 index;
449};
450
451static int retimer_match(struct device *dev, void *data)
452{
453	const struct tb_retimer_lookup *lookup = data;
454	struct tb_retimer *rt = tb_to_retimer(dev);
455
456	return rt && rt->port == lookup->port && rt->index == lookup->index;
457}
458
459static struct tb_retimer *tb_port_find_retimer(struct tb_port *port, u8 index)
460{
461	struct tb_retimer_lookup lookup = { .port = port, .index = index };
462	struct device *dev;
463
464	dev = device_find_child(&port->usb4->dev, &lookup, retimer_match);
465	if (dev)
466		return tb_to_retimer(dev);
467
468	return NULL;
469}
470
471/**
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
475 *
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.
480 */
481int tb_retimer_scan(struct tb_port *port, bool add)
482{
483	u32 status[TB_MAX_RETIMER_INDEX + 1] = {};
484	int ret, i, last_idx = 0;
485
486	/*
487	 * Send broadcast RT to make sure retimer indices facing this
488	 * port are set.
489	 */
490	ret = usb4_port_enumerate_retimers(port);
491	if (ret)
492		return ret;
493
494	/*
495	 * Immediately after sending enumerate retimers read the
496	 * authentication status of each retimer.
497	 */
498	tb_retimer_nvm_authenticate_status(port, status);
499
500	/*
501	 * Enable sideband channel for each retimer. We can do this
502	 * regardless whether there is device connected or not.
503	 */
504	tb_retimer_set_inbound_sbtx(port);
505
506	for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) {
507		/*
508		 * Last retimer is true only for the last on-board
509		 * retimer (the one connected directly to the Type-C
510		 * port).
511		 */
512		ret = usb4_port_retimer_is_last(port, i);
513		if (ret > 0)
514			last_idx = i;
515		else if (ret < 0)
516			break;
517	}
518
519	tb_retimer_unset_inbound_sbtx(port);
520
521	if (!last_idx)
522		return 0;
523
524	/* Add on-board retimers if they do not exist already */
525	ret = 0;
526	for (i = 1; i <= last_idx; i++) {
 
 
 
 
527		struct tb_retimer *rt;
528
 
 
 
 
529		rt = tb_port_find_retimer(port, i);
530		if (rt) {
531			put_device(&rt->dev);
532		} else if (add) {
533			ret = tb_retimer_add(port, i, status[i]);
534			if (ret && ret != -EOPNOTSUPP)
535				break;
536		}
537	}
538
 
539	return ret;
540}
541
542static int remove_retimer(struct device *dev, void *data)
543{
544	struct tb_retimer *rt = tb_to_retimer(dev);
545	struct tb_port *port = data;
546
547	if (rt && rt->port == port)
548		tb_retimer_remove(rt);
549	return 0;
550}
551
552/**
553 * tb_retimer_remove_all() - Remove all retimers under port
554 * @port: USB4 port whose retimers to remove
555 *
556 * This removes all previously added retimers under @port.
557 */
558void tb_retimer_remove_all(struct tb_port *port)
559{
560	struct usb4_port *usb4;
561
562	usb4 = port->usb4;
563	if (usb4)
564		device_for_each_child_reverse(&usb4->dev, port,
565					      remove_retimer);
566}
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Thunderbolt/USB4 retimer support.
  4 *
  5 * Copyright (C) 2020, Intel Corporation
  6 * Authors: Kranthi Kuntala <kranthi.kuntala@intel.com>
  7 *	    Mika Westerberg <mika.westerberg@linux.intel.com>
  8 */
  9
 10#include <linux/delay.h>
 11#include <linux/pm_runtime.h>
 12#include <linux/sched/signal.h>
 13
 14#include "sb_regs.h"
 15#include "tb.h"
 16
 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
 22
 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
 29 *
 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.
 32 */
 33int tb_retimer_nvm_read(struct tb_retimer *rt, unsigned int address, void *buf,
 34			size_t size)
 35{
 36	return usb4_port_retimer_nvm_read(rt->port, rt->index, address, buf, size);
 37}
 38
 39static int nvm_read(void *priv, unsigned int offset, void *val, size_t bytes)
 40{
 41	struct tb_nvm *nvm = priv;
 42	struct tb_retimer *rt = tb_to_retimer(nvm->dev);
 43	int ret;
 44
 45	pm_runtime_get_sync(&rt->dev);
 46
 47	if (!mutex_trylock(&rt->tb->lock)) {
 48		ret = restart_syscall();
 49		goto out;
 50	}
 51
 52	ret = tb_retimer_nvm_read(rt, offset, val, bytes);
 53	mutex_unlock(&rt->tb->lock);
 54
 55out:
 56	pm_runtime_mark_last_busy(&rt->dev);
 57	pm_runtime_put_autosuspend(&rt->dev);
 58
 59	return ret;
 60}
 61
 62static int nvm_write(void *priv, unsigned int offset, void *val, size_t bytes)
 63{
 64	struct tb_nvm *nvm = priv;
 65	struct tb_retimer *rt = tb_to_retimer(nvm->dev);
 66	int ret = 0;
 67
 68	if (!mutex_trylock(&rt->tb->lock))
 69		return restart_syscall();
 70
 71	ret = tb_nvm_write_buf(nvm, offset, val, bytes);
 72	mutex_unlock(&rt->tb->lock);
 73
 74	return ret;
 75}
 76
 77static int tb_retimer_nvm_add(struct tb_retimer *rt)
 78{
 79	struct tb_nvm *nvm;
 80	int ret;
 81
 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;
 86	}
 87
 88	ret = tb_nvm_read_version(nvm);
 89	if (ret)
 90		goto err_nvm;
 91
 92	ret = tb_nvm_add_active(nvm, nvm_read);
 93	if (ret)
 94		goto err_nvm;
 95
 96	ret = tb_nvm_add_non_active(nvm, nvm_write);
 97	if (ret)
 98		goto err_nvm;
 99
100	rt->nvm = nvm;
101	dev_dbg(&rt->dev, "NVM version %x.%x\n", nvm->major, nvm->minor);
102	return 0;
103
104err_nvm:
105	dev_dbg(&rt->dev, "NVM upgrade disabled\n");
106	rt->no_nvm_upgrade = true;
107	if (!IS_ERR(nvm))
108		tb_nvm_free(nvm);
109
110	return ret;
111}
112
113static int tb_retimer_nvm_validate_and_write(struct tb_retimer *rt)
114{
115	unsigned int image_size;
116	const u8 *buf;
117	int ret;
118
119	ret = tb_nvm_validate(rt->nvm);
120	if (ret)
121		return ret;
122
123	buf = rt->nvm->buf_data_start;
124	image_size = rt->nvm->buf_data_size;
125
126	ret = usb4_port_retimer_nvm_write(rt->port, rt->index, 0, buf,
127					 image_size);
128	if (ret)
129		return ret;
130
131	rt->nvm->flushed = true;
132	return 0;
133}
134
135static int tb_retimer_nvm_authenticate(struct tb_retimer *rt, bool auth_only)
136{
137	u32 status;
138	int ret;
139
140	if (auth_only) {
141		ret = usb4_port_retimer_nvm_set_offset(rt->port, rt->index, 0);
142		if (ret)
143			return ret;
144	}
145
146	ret = usb4_port_retimer_nvm_authenticate(rt->port, rt->index);
147	if (ret)
148		return ret;
149
150	usleep_range(100, 150);
151
152	/*
153	 * Check the status now if we still can access the retimer. It
154	 * is expected that the below fails.
155	 */
156	ret = usb4_port_retimer_nvm_authenticate_status(rt->port, rt->index,
157							&status);
158	if (!ret) {
159		rt->auth_status = status;
160		return status ? -EINVAL : 0;
161	}
162
163	return 0;
164}
165
166static ssize_t device_show(struct device *dev, struct device_attribute *attr,
167			   char *buf)
168{
169	struct tb_retimer *rt = tb_to_retimer(dev);
170
171	return sysfs_emit(buf, "%#x\n", rt->device);
172}
173static DEVICE_ATTR_RO(device);
174
175static ssize_t nvm_authenticate_show(struct device *dev,
176	struct device_attribute *attr, char *buf)
177{
178	struct tb_retimer *rt = tb_to_retimer(dev);
179	int ret;
180
181	if (!mutex_trylock(&rt->tb->lock))
182		return restart_syscall();
183
184	if (!rt->nvm)
185		ret = -EAGAIN;
 
 
186	else
187		ret = sysfs_emit(buf, "%#x\n", rt->auth_status);
188
189	mutex_unlock(&rt->tb->lock);
190
191	return ret;
192}
193
194static void tb_retimer_nvm_authenticate_status(struct tb_port *port, u32 *status)
195{
196	int i;
197
198	tb_port_dbg(port, "reading NVM authentication status of retimers\n");
199
200	/*
201	 * Before doing anything else, read the authentication status.
202	 * If the retimer has it set, store it for the new retimer
203	 * device instance.
204	 */
205	for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) {
206		if (usb4_port_retimer_nvm_authenticate_status(port, i, &status[i]))
207			break;
208	}
209}
210
211static void tb_retimer_set_inbound_sbtx(struct tb_port *port)
212{
213	int i;
214
215	/*
216	 * When USB4 port is online sideband communications are
217	 * already up.
218	 */
219	if (!usb4_port_device_is_offline(port->usb4))
220		return;
221
222	tb_port_dbg(port, "enabling sideband transactions\n");
223
224	for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
225		usb4_port_retimer_set_inbound_sbtx(port, i);
226}
227
228static void tb_retimer_unset_inbound_sbtx(struct tb_port *port)
229{
230	int i;
231
232	/*
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.
236	 */
237	if (usb4_port_device_is_offline(port->usb4))
238		return;
239
240	tb_port_dbg(port, "disabling sideband transactions\n");
241
242	for (i = TB_MAX_RETIMER_INDEX; i >= 1; i--) {
243		if (usb4_port_retimer_unset_inbound_sbtx(port, i))
244			break;
245	}
246}
247
248static ssize_t nvm_authenticate_store(struct device *dev,
249	struct device_attribute *attr, const char *buf, size_t count)
250{
251	struct tb_retimer *rt = tb_to_retimer(dev);
252	int val, ret;
253
254	pm_runtime_get_sync(&rt->dev);
255
256	if (!mutex_trylock(&rt->tb->lock)) {
257		ret = restart_syscall();
258		goto exit_rpm;
259	}
260
261	if (!rt->nvm) {
262		ret = -EAGAIN;
263		goto exit_unlock;
264	}
265
266	ret = kstrtoint(buf, 10, &val);
267	if (ret)
268		goto exit_unlock;
269
270	/* Always clear status */
271	rt->auth_status = 0;
272
273	if (val) {
274		/*
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.
280		 */
281		tb_retimer_set_inbound_sbtx(rt->port);
282		if (val == AUTHENTICATE_ONLY) {
283			ret = tb_retimer_nvm_authenticate(rt, true);
284		} else {
285			if (!rt->nvm->flushed) {
286				if (!rt->nvm->buf) {
287					ret = -EINVAL;
288					goto exit_unlock;
289				}
290
291				ret = tb_retimer_nvm_validate_and_write(rt);
292				if (ret || val == WRITE_ONLY)
293					goto exit_unlock;
294			}
295			if (val == WRITE_AND_AUTHENTICATE)
296				ret = tb_retimer_nvm_authenticate(rt, false);
297		}
298	}
299
300exit_unlock:
301	if (ret || val == WRITE_ONLY)
302		tb_retimer_unset_inbound_sbtx(rt->port);
303	mutex_unlock(&rt->tb->lock);
304exit_rpm:
305	pm_runtime_mark_last_busy(&rt->dev);
306	pm_runtime_put_autosuspend(&rt->dev);
307
308	if (ret)
309		return ret;
310	return count;
311}
312static DEVICE_ATTR_RW(nvm_authenticate);
313
314static ssize_t nvm_version_show(struct device *dev,
315				struct device_attribute *attr, char *buf)
316{
317	struct tb_retimer *rt = tb_to_retimer(dev);
318	int ret;
319
320	if (!mutex_trylock(&rt->tb->lock))
321		return restart_syscall();
322
323	if (!rt->nvm)
324		ret = -EAGAIN;
325	else
326		ret = sysfs_emit(buf, "%x.%x\n", rt->nvm->major, rt->nvm->minor);
327
328	mutex_unlock(&rt->tb->lock);
329	return ret;
330}
331static DEVICE_ATTR_RO(nvm_version);
332
333static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
334			   char *buf)
335{
336	struct tb_retimer *rt = tb_to_retimer(dev);
337
338	return sysfs_emit(buf, "%#x\n", rt->vendor);
339}
340static DEVICE_ATTR_RO(vendor);
341
342static umode_t retimer_is_visible(struct kobject *kobj, struct attribute *attr,
343				  int n)
344{
345	struct device *dev = kobj_to_dev(kobj);
346	struct tb_retimer *rt = tb_to_retimer(dev);
347
348	if (attr == &dev_attr_nvm_authenticate.attr ||
349	    attr == &dev_attr_nvm_version.attr)
350		return rt->no_nvm_upgrade ? 0 : attr->mode;
351
352	return attr->mode;
353}
354
355static 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,
360	NULL
361};
362
363static const struct attribute_group retimer_group = {
364	.is_visible = retimer_is_visible,
365	.attrs = retimer_attrs,
366};
367
368static const struct attribute_group *retimer_groups[] = {
369	&retimer_group,
370	NULL
371};
372
373static void tb_retimer_release(struct device *dev)
374{
375	struct tb_retimer *rt = tb_to_retimer(dev);
376
377	kfree(rt);
378}
379
380const struct device_type tb_retimer_type = {
381	.name = "thunderbolt_retimer",
382	.groups = retimer_groups,
383	.release = tb_retimer_release,
384};
385
386static int tb_retimer_add(struct tb_port *port, u8 index, u32 auth_status,
387			  bool on_board)
388{
389	struct tb_retimer *rt;
390	u32 vendor, device;
391	int ret;
392
393	ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
394				USB4_SB_VENDOR_ID, &vendor, sizeof(vendor));
395	if (ret) {
396		if (ret != -ENODEV)
397			tb_port_warn(port, "failed read retimer VendorId: %d\n", ret);
398		return ret;
399	}
400
401	ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
402				USB4_SB_PRODUCT_ID, &device, sizeof(device));
403	if (ret) {
404		if (ret != -ENODEV)
405			tb_port_warn(port, "failed read retimer ProductId: %d\n", ret);
406		return ret;
407	}
408
 
 
 
 
 
 
 
409
410	rt = kzalloc(sizeof(*rt), GFP_KERNEL);
411	if (!rt)
412		return -ENOMEM;
413
414	rt->index = index;
415	rt->vendor = vendor;
416	rt->device = device;
417	rt->auth_status = auth_status;
418	rt->port = port;
419	rt->tb = port->sw->tb;
420
421	/*
422	 * Only support NVM upgrade for on-board retimers. The retimers
423	 * on the other side of the connection.
424	 */
425	if (!on_board || usb4_port_retimer_nvm_sector_size(port, index) <= 0)
426		rt->no_nvm_upgrade = true;
427
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),
432		     port->port, index);
433
434	ret = device_register(&rt->dev);
435	if (ret) {
436		dev_err(&rt->dev, "failed to register retimer: %d\n", ret);
437		put_device(&rt->dev);
438		return ret;
439	}
440
441	ret = tb_retimer_nvm_add(rt);
442	if (ret) {
443		dev_err(&rt->dev, "failed to add NVM devices: %d\n", ret);
444		device_unregister(&rt->dev);
445		return ret;
446	}
447
448	dev_info(&rt->dev, "new retimer found, vendor=%#x device=%#x\n",
449		 rt->vendor, rt->device);
450
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);
457
458	tb_retimer_debugfs_init(rt);
459	return 0;
460}
461
462static void tb_retimer_remove(struct tb_retimer *rt)
463{
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);
468}
469
470struct tb_retimer_lookup {
471	const struct tb_port *port;
472	u8 index;
473};
474
475static int retimer_match(struct device *dev, void *data)
476{
477	const struct tb_retimer_lookup *lookup = data;
478	struct tb_retimer *rt = tb_to_retimer(dev);
479
480	return rt && rt->port == lookup->port && rt->index == lookup->index;
481}
482
483static struct tb_retimer *tb_port_find_retimer(struct tb_port *port, u8 index)
484{
485	struct tb_retimer_lookup lookup = { .port = port, .index = index };
486	struct device *dev;
487
488	dev = device_find_child(&port->usb4->dev, &lookup, retimer_match);
489	if (dev)
490		return tb_to_retimer(dev);
491
492	return NULL;
493}
494
495/**
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
499 *
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.
504 */
505int tb_retimer_scan(struct tb_port *port, bool add)
506{
507	u32 status[TB_MAX_RETIMER_INDEX + 1] = {};
508	int ret, i, max, last_idx = 0;
509
510	/*
511	 * Send broadcast RT to make sure retimer indices facing this
512	 * port are set.
513	 */
514	ret = usb4_port_enumerate_retimers(port);
515	if (ret)
516		return ret;
517
518	/*
519	 * Immediately after sending enumerate retimers read the
520	 * authentication status of each retimer.
521	 */
522	tb_retimer_nvm_authenticate_status(port, status);
523
524	/*
525	 * Enable sideband channel for each retimer. We can do this
526	 * regardless whether there is device connected or not.
527	 */
528	tb_retimer_set_inbound_sbtx(port);
529
530	for (max = 1, i = 1; i <= TB_MAX_RETIMER_INDEX; i++) {
531		/*
532		 * Last retimer is true only for the last on-board
533		 * retimer (the one connected directly to the Type-C
534		 * port).
535		 */
536		ret = usb4_port_retimer_is_last(port, i);
537		if (ret > 0)
538			last_idx = i;
539		else if (ret < 0)
540			break;
 
541
542		max = i;
543	}
 
 
544
 
545	ret = 0;
546	if (!IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING))
547		max = min(last_idx, max);
548
549	/* Add retimers if they do not exist already */
550	for (i = 1; i <= max; i++) {
551		struct tb_retimer *rt;
552
553		/* Skip cable retimers */
554		if (usb4_port_retimer_is_cable(port, i))
555			continue;
556
557		rt = tb_port_find_retimer(port, i);
558		if (rt) {
559			put_device(&rt->dev);
560		} else if (add) {
561			ret = tb_retimer_add(port, i, status[i], i <= last_idx);
562			if (ret && ret != -EOPNOTSUPP)
563				break;
564		}
565	}
566
567	tb_retimer_unset_inbound_sbtx(port);
568	return ret;
569}
570
571static int remove_retimer(struct device *dev, void *data)
572{
573	struct tb_retimer *rt = tb_to_retimer(dev);
574	struct tb_port *port = data;
575
576	if (rt && rt->port == port)
577		tb_retimer_remove(rt);
578	return 0;
579}
580
581/**
582 * tb_retimer_remove_all() - Remove all retimers under port
583 * @port: USB4 port whose retimers to remove
584 *
585 * This removes all previously added retimers under @port.
586 */
587void tb_retimer_remove_all(struct tb_port *port)
588{
589	struct usb4_port *usb4;
590
591	usb4 = port->usb4;
592	if (usb4)
593		device_for_each_child_reverse(&usb4->dev, port,
594					      remove_retimer);
595}