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	return 0;
98
99err_nvm:
100	dev_dbg(&rt->dev, "NVM upgrade disabled\n");
101	if (!IS_ERR(nvm))
102		tb_nvm_free(nvm);
103
104	return ret;
105}
106
107static int tb_retimer_nvm_validate_and_write(struct tb_retimer *rt)
108{
109	unsigned int image_size;
110	const u8 *buf;
111	int ret;
112
113	ret = tb_nvm_validate(rt->nvm);
114	if (ret)
115		return ret;
116
117	buf = rt->nvm->buf_data_start;
118	image_size = rt->nvm->buf_data_size;
119
120	ret = usb4_port_retimer_nvm_write(rt->port, rt->index, 0, buf,
121					 image_size);
122	if (ret)
123		return ret;
124
125	rt->nvm->flushed = true;
126	return 0;
127}
128
129static int tb_retimer_nvm_authenticate(struct tb_retimer *rt, bool auth_only)
130{
131	u32 status;
132	int ret;
133
134	if (auth_only) {
135		ret = usb4_port_retimer_nvm_set_offset(rt->port, rt->index, 0);
136		if (ret)
137			return ret;
138	}
139
140	ret = usb4_port_retimer_nvm_authenticate(rt->port, rt->index);
141	if (ret)
142		return ret;
143
144	usleep_range(100, 150);
145
146	/*
147	 * Check the status now if we still can access the retimer. It
148	 * is expected that the below fails.
149	 */
150	ret = usb4_port_retimer_nvm_authenticate_status(rt->port, rt->index,
151							&status);
152	if (!ret) {
153		rt->auth_status = status;
154		return status ? -EINVAL : 0;
155	}
156
157	return 0;
158}
159
160static ssize_t device_show(struct device *dev, struct device_attribute *attr,
161			   char *buf)
162{
163	struct tb_retimer *rt = tb_to_retimer(dev);
164
165	return sysfs_emit(buf, "%#x\n", rt->device);
166}
167static DEVICE_ATTR_RO(device);
168
169static ssize_t nvm_authenticate_show(struct device *dev,
170	struct device_attribute *attr, char *buf)
171{
172	struct tb_retimer *rt = tb_to_retimer(dev);
173	int ret;
174
175	if (!mutex_trylock(&rt->tb->lock))
176		return restart_syscall();
177
178	if (!rt->nvm)
179		ret = -EAGAIN;
180	else if (rt->no_nvm_upgrade)
181		ret = -EOPNOTSUPP;
182	else
183		ret = sysfs_emit(buf, "%#x\n", rt->auth_status);
184
185	mutex_unlock(&rt->tb->lock);
186
187	return ret;
188}
189
190static void tb_retimer_nvm_authenticate_status(struct tb_port *port, u32 *status)
191{
192	int i;
193
194	tb_port_dbg(port, "reading NVM authentication status of retimers\n");
195
196	/*
197	 * Before doing anything else, read the authentication status.
198	 * If the retimer has it set, store it for the new retimer
199	 * device instance.
200	 */
201	for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
202		usb4_port_retimer_nvm_authenticate_status(port, i, &status[i]);
203}
204
205static void tb_retimer_set_inbound_sbtx(struct tb_port *port)
206{
207	int i;
208
209	/*
210	 * When USB4 port is online sideband communications are
211	 * already up.
212	 */
213	if (!usb4_port_device_is_offline(port->usb4))
214		return;
215
216	tb_port_dbg(port, "enabling sideband transactions\n");
217
218	for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
219		usb4_port_retimer_set_inbound_sbtx(port, i);
220}
221
222static void tb_retimer_unset_inbound_sbtx(struct tb_port *port)
223{
224	int i;
225
226	/*
227	 * When USB4 port is offline we need to keep the sideband
228	 * communications up to make it possible to communicate with
229	 * the connected retimers.
230	 */
231	if (usb4_port_device_is_offline(port->usb4))
232		return;
233
234	tb_port_dbg(port, "disabling sideband transactions\n");
235
236	for (i = TB_MAX_RETIMER_INDEX; i >= 1; i--)
237		usb4_port_retimer_unset_inbound_sbtx(port, i);
238}
239
240static ssize_t nvm_authenticate_store(struct device *dev,
241	struct device_attribute *attr, const char *buf, size_t count)
242{
243	struct tb_retimer *rt = tb_to_retimer(dev);
244	int val, ret;
245
246	pm_runtime_get_sync(&rt->dev);
247
248	if (!mutex_trylock(&rt->tb->lock)) {
249		ret = restart_syscall();
250		goto exit_rpm;
251	}
252
253	if (!rt->nvm) {
254		ret = -EAGAIN;
255		goto exit_unlock;
256	}
257
258	ret = kstrtoint(buf, 10, &val);
259	if (ret)
260		goto exit_unlock;
261
262	/* Always clear status */
263	rt->auth_status = 0;
264
265	if (val) {
266		/*
267		 * When NVM authentication starts the retimer is not
268		 * accessible so calling tb_retimer_unset_inbound_sbtx()
269		 * will fail and therefore we do not call it. Exception
270		 * is when the validation fails or we only write the new
271		 * NVM image without authentication.
272		 */
273		tb_retimer_set_inbound_sbtx(rt->port);
274		if (val == AUTHENTICATE_ONLY) {
275			ret = tb_retimer_nvm_authenticate(rt, true);
276		} else {
277			if (!rt->nvm->flushed) {
278				if (!rt->nvm->buf) {
279					ret = -EINVAL;
280					goto exit_unlock;
281				}
282
283				ret = tb_retimer_nvm_validate_and_write(rt);
284				if (ret || val == WRITE_ONLY)
285					goto exit_unlock;
286			}
287			if (val == WRITE_AND_AUTHENTICATE)
288				ret = tb_retimer_nvm_authenticate(rt, false);
289		}
290	}
291
292exit_unlock:
293	if (ret || val == WRITE_ONLY)
294		tb_retimer_unset_inbound_sbtx(rt->port);
295	mutex_unlock(&rt->tb->lock);
296exit_rpm:
297	pm_runtime_mark_last_busy(&rt->dev);
298	pm_runtime_put_autosuspend(&rt->dev);
299
300	if (ret)
301		return ret;
302	return count;
303}
304static DEVICE_ATTR_RW(nvm_authenticate);
305
306static ssize_t nvm_version_show(struct device *dev,
307				struct device_attribute *attr, char *buf)
308{
309	struct tb_retimer *rt = tb_to_retimer(dev);
310	int ret;
311
312	if (!mutex_trylock(&rt->tb->lock))
313		return restart_syscall();
314
315	if (!rt->nvm)
316		ret = -EAGAIN;
317	else
318		ret = sysfs_emit(buf, "%x.%x\n", rt->nvm->major, rt->nvm->minor);
319
320	mutex_unlock(&rt->tb->lock);
321	return ret;
322}
323static DEVICE_ATTR_RO(nvm_version);
324
325static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
326			   char *buf)
327{
328	struct tb_retimer *rt = tb_to_retimer(dev);
329
330	return sysfs_emit(buf, "%#x\n", rt->vendor);
331}
332static DEVICE_ATTR_RO(vendor);
333
334static struct attribute *retimer_attrs[] = {
335	&dev_attr_device.attr,
336	&dev_attr_nvm_authenticate.attr,
337	&dev_attr_nvm_version.attr,
338	&dev_attr_vendor.attr,
339	NULL
340};
341
342static const struct attribute_group retimer_group = {
343	.attrs = retimer_attrs,
344};
345
346static const struct attribute_group *retimer_groups[] = {
347	&retimer_group,
348	NULL
349};
350
351static void tb_retimer_release(struct device *dev)
352{
353	struct tb_retimer *rt = tb_to_retimer(dev);
354
355	kfree(rt);
356}
357
358struct device_type tb_retimer_type = {
359	.name = "thunderbolt_retimer",
360	.groups = retimer_groups,
361	.release = tb_retimer_release,
362};
363
364static int tb_retimer_add(struct tb_port *port, u8 index, u32 auth_status)
365{
366	struct tb_retimer *rt;
367	u32 vendor, device;
368	int ret;
369
370	ret = usb4_port_retimer_read(port, index, USB4_SB_VENDOR_ID, &vendor,
371				     sizeof(vendor));
372	if (ret) {
373		if (ret != -ENODEV)
374			tb_port_warn(port, "failed read retimer VendorId: %d\n", ret);
375		return ret;
376	}
377
378	ret = usb4_port_retimer_read(port, index, USB4_SB_PRODUCT_ID, &device,
379				     sizeof(device));
380	if (ret) {
381		if (ret != -ENODEV)
382			tb_port_warn(port, "failed read retimer ProductId: %d\n", ret);
383		return ret;
384	}
385
386	/*
387	 * Check that it supports NVM operations. If not then don't add
388	 * the device at all.
389	 */
390	ret = usb4_port_retimer_nvm_sector_size(port, index);
391	if (ret < 0)
392		return ret;
393
394	rt = kzalloc(sizeof(*rt), GFP_KERNEL);
395	if (!rt)
396		return -ENOMEM;
397
398	rt->index = index;
399	rt->vendor = vendor;
400	rt->device = device;
401	rt->auth_status = auth_status;
402	rt->port = port;
403	rt->tb = port->sw->tb;
404
405	rt->dev.parent = &port->usb4->dev;
406	rt->dev.bus = &tb_bus_type;
407	rt->dev.type = &tb_retimer_type;
408	dev_set_name(&rt->dev, "%s:%u.%u", dev_name(&port->sw->dev),
409		     port->port, index);
410
411	ret = device_register(&rt->dev);
412	if (ret) {
413		dev_err(&rt->dev, "failed to register retimer: %d\n", ret);
414		put_device(&rt->dev);
415		return ret;
416	}
417
418	ret = tb_retimer_nvm_add(rt);
419	if (ret) {
420		dev_err(&rt->dev, "failed to add NVM devices: %d\n", ret);
421		device_unregister(&rt->dev);
422		return ret;
423	}
424
425	dev_info(&rt->dev, "new retimer found, vendor=%#x device=%#x\n",
426		 rt->vendor, rt->device);
427
428	pm_runtime_no_callbacks(&rt->dev);
429	pm_runtime_set_active(&rt->dev);
430	pm_runtime_enable(&rt->dev);
431	pm_runtime_set_autosuspend_delay(&rt->dev, TB_AUTOSUSPEND_DELAY);
432	pm_runtime_mark_last_busy(&rt->dev);
433	pm_runtime_use_autosuspend(&rt->dev);
434
435	return 0;
436}
437
438static void tb_retimer_remove(struct tb_retimer *rt)
439{
440	dev_info(&rt->dev, "retimer disconnected\n");
441	tb_nvm_free(rt->nvm);
442	device_unregister(&rt->dev);
443}
444
445struct tb_retimer_lookup {
446	const struct tb_port *port;
447	u8 index;
448};
449
450static int retimer_match(struct device *dev, void *data)
451{
452	const struct tb_retimer_lookup *lookup = data;
453	struct tb_retimer *rt = tb_to_retimer(dev);
454
455	return rt && rt->port == lookup->port && rt->index == lookup->index;
456}
457
458static struct tb_retimer *tb_port_find_retimer(struct tb_port *port, u8 index)
459{
460	struct tb_retimer_lookup lookup = { .port = port, .index = index };
461	struct device *dev;
462
463	dev = device_find_child(&port->usb4->dev, &lookup, retimer_match);
464	if (dev)
465		return tb_to_retimer(dev);
466
467	return NULL;
468}
469
470/**
471 * tb_retimer_scan() - Scan for on-board retimers under port
472 * @port: USB4 port to scan
473 * @add: If true also registers found retimers
474 *
475 * Brings the sideband into a state where retimers can be accessed.
476 * Then Tries to enumerate on-board retimers connected to @port. Found
477 * retimers are registered as children of @port if @add is set.  Does
478 * not scan for cable retimers for now.
479 */
480int tb_retimer_scan(struct tb_port *port, bool add)
481{
482	u32 status[TB_MAX_RETIMER_INDEX + 1] = {};
483	int ret, i, last_idx = 0;
484
485	/*
486	 * Send broadcast RT to make sure retimer indices facing this
487	 * port are set.
488	 */
489	ret = usb4_port_enumerate_retimers(port);
490	if (ret)
491		return ret;
492
493	/*
494	 * Immediately after sending enumerate retimers read the
495	 * authentication status of each retimer.
496	 */
497	tb_retimer_nvm_authenticate_status(port, status);
498
499	/*
500	 * Enable sideband channel for each retimer. We can do this
501	 * regardless whether there is device connected or not.
502	 */
503	tb_retimer_set_inbound_sbtx(port);
504
505	for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) {
506		/*
507		 * Last retimer is true only for the last on-board
508		 * retimer (the one connected directly to the Type-C
509		 * port).
510		 */
511		ret = usb4_port_retimer_is_last(port, i);
512		if (ret > 0)
513			last_idx = i;
514		else if (ret < 0)
515			break;
516	}
517
518	tb_retimer_unset_inbound_sbtx(port);
519
520	if (!last_idx)
521		return 0;
522
523	/* Add on-board retimers if they do not exist already */
524	ret = 0;
525	for (i = 1; i <= last_idx; i++) {
526		struct tb_retimer *rt;
527
528		rt = tb_port_find_retimer(port, i);
529		if (rt) {
530			put_device(&rt->dev);
531		} else if (add) {
532			ret = tb_retimer_add(port, i, status[i]);
533			if (ret && ret != -EOPNOTSUPP)
534				break;
535		}
536	}
537
538	return ret;
539}
540
541static int remove_retimer(struct device *dev, void *data)
542{
543	struct tb_retimer *rt = tb_to_retimer(dev);
544	struct tb_port *port = data;
545
546	if (rt && rt->port == port)
547		tb_retimer_remove(rt);
548	return 0;
549}
550
551/**
552 * tb_retimer_remove_all() - Remove all retimers under port
553 * @port: USB4 port whose retimers to remove
554 *
555 * This removes all previously added retimers under @port.
556 */
557void tb_retimer_remove_all(struct tb_port *port)
558{
559	struct usb4_port *usb4;
560
561	usb4 = port->usb4;
562	if (usb4)
563		device_for_each_child_reverse(&usb4->dev, port,
564					      remove_retimer);
565}
566