• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * USB4 specific functionality
4  *
5  * Copyright (C) 2019, Intel Corporation
6  * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
7  *	    Rajmohan Mani <rajmohan.mani@intel.com>
8  */
9 
10 #include <linux/delay.h>
11 #include <linux/ktime.h>
12 
13 #include "sb_regs.h"
14 #include "tb.h"
15 
16 #define USB4_DATA_DWORDS		16
17 #define USB4_DATA_RETRIES		3
18 
19 enum usb4_switch_op {
20 	USB4_SWITCH_OP_QUERY_DP_RESOURCE = 0x10,
21 	USB4_SWITCH_OP_ALLOC_DP_RESOURCE = 0x11,
22 	USB4_SWITCH_OP_DEALLOC_DP_RESOURCE = 0x12,
23 	USB4_SWITCH_OP_NVM_WRITE = 0x20,
24 	USB4_SWITCH_OP_NVM_AUTH = 0x21,
25 	USB4_SWITCH_OP_NVM_READ = 0x22,
26 	USB4_SWITCH_OP_NVM_SET_OFFSET = 0x23,
27 	USB4_SWITCH_OP_DROM_READ = 0x24,
28 	USB4_SWITCH_OP_NVM_SECTOR_SIZE = 0x25,
29 };
30 
31 enum usb4_sb_target {
32 	USB4_SB_TARGET_ROUTER,
33 	USB4_SB_TARGET_PARTNER,
34 	USB4_SB_TARGET_RETIMER,
35 };
36 
37 #define USB4_NVM_READ_OFFSET_MASK	GENMASK(23, 2)
38 #define USB4_NVM_READ_OFFSET_SHIFT	2
39 #define USB4_NVM_READ_LENGTH_MASK	GENMASK(27, 24)
40 #define USB4_NVM_READ_LENGTH_SHIFT	24
41 
42 #define USB4_NVM_SET_OFFSET_MASK	USB4_NVM_READ_OFFSET_MASK
43 #define USB4_NVM_SET_OFFSET_SHIFT	USB4_NVM_READ_OFFSET_SHIFT
44 
45 #define USB4_DROM_ADDRESS_MASK		GENMASK(14, 2)
46 #define USB4_DROM_ADDRESS_SHIFT		2
47 #define USB4_DROM_SIZE_MASK		GENMASK(19, 15)
48 #define USB4_DROM_SIZE_SHIFT		15
49 
50 #define USB4_NVM_SECTOR_SIZE_MASK	GENMASK(23, 0)
51 
52 typedef int (*read_block_fn)(void *, unsigned int, void *, size_t);
53 typedef int (*write_block_fn)(void *, const void *, size_t);
54 
usb4_switch_wait_for_bit(struct tb_switch * sw,u32 offset,u32 bit,u32 value,int timeout_msec)55 static int usb4_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit,
56 				    u32 value, int timeout_msec)
57 {
58 	ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
59 
60 	do {
61 		u32 val;
62 		int ret;
63 
64 		ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1);
65 		if (ret)
66 			return ret;
67 
68 		if ((val & bit) == value)
69 			return 0;
70 
71 		usleep_range(50, 100);
72 	} while (ktime_before(ktime_get(), timeout));
73 
74 	return -ETIMEDOUT;
75 }
76 
usb4_switch_op_read_data(struct tb_switch * sw,void * data,size_t dwords)77 static int usb4_switch_op_read_data(struct tb_switch *sw, void *data,
78 				    size_t dwords)
79 {
80 	if (dwords > USB4_DATA_DWORDS)
81 		return -EINVAL;
82 
83 	return tb_sw_read(sw, data, TB_CFG_SWITCH, ROUTER_CS_9, dwords);
84 }
85 
usb4_switch_op_write_data(struct tb_switch * sw,const void * data,size_t dwords)86 static int usb4_switch_op_write_data(struct tb_switch *sw, const void *data,
87 				     size_t dwords)
88 {
89 	if (dwords > USB4_DATA_DWORDS)
90 		return -EINVAL;
91 
92 	return tb_sw_write(sw, data, TB_CFG_SWITCH, ROUTER_CS_9, dwords);
93 }
94 
usb4_switch_op_read_metadata(struct tb_switch * sw,u32 * metadata)95 static int usb4_switch_op_read_metadata(struct tb_switch *sw, u32 *metadata)
96 {
97 	return tb_sw_read(sw, metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
98 }
99 
usb4_switch_op_write_metadata(struct tb_switch * sw,u32 metadata)100 static int usb4_switch_op_write_metadata(struct tb_switch *sw, u32 metadata)
101 {
102 	return tb_sw_write(sw, &metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
103 }
104 
usb4_do_read_data(u16 address,void * buf,size_t size,read_block_fn read_block,void * read_block_data)105 static int usb4_do_read_data(u16 address, void *buf, size_t size,
106 			     read_block_fn read_block, void *read_block_data)
107 {
108 	unsigned int retries = USB4_DATA_RETRIES;
109 	unsigned int offset;
110 
111 	do {
112 		unsigned int dwaddress, dwords;
113 		u8 data[USB4_DATA_DWORDS * 4];
114 		size_t nbytes;
115 		int ret;
116 
117 		offset = address & 3;
118 		nbytes = min_t(size_t, size + offset, USB4_DATA_DWORDS * 4);
119 
120 		dwaddress = address / 4;
121 		dwords = ALIGN(nbytes, 4) / 4;
122 
123 		ret = read_block(read_block_data, dwaddress, data, dwords);
124 		if (ret) {
125 			if (ret != -ENODEV && retries--)
126 				continue;
127 			return ret;
128 		}
129 
130 		nbytes -= offset;
131 		memcpy(buf, data + offset, nbytes);
132 
133 		size -= nbytes;
134 		address += nbytes;
135 		buf += nbytes;
136 	} while (size > 0);
137 
138 	return 0;
139 }
140 
usb4_do_write_data(unsigned int address,const void * buf,size_t size,write_block_fn write_next_block,void * write_block_data)141 static int usb4_do_write_data(unsigned int address, const void *buf, size_t size,
142 	write_block_fn write_next_block, void *write_block_data)
143 {
144 	unsigned int retries = USB4_DATA_RETRIES;
145 	unsigned int offset;
146 
147 	offset = address & 3;
148 	address = address & ~3;
149 
150 	do {
151 		u32 nbytes = min_t(u32, size, USB4_DATA_DWORDS * 4);
152 		u8 data[USB4_DATA_DWORDS * 4];
153 		int ret;
154 
155 		memcpy(data + offset, buf, nbytes);
156 
157 		ret = write_next_block(write_block_data, data, nbytes / 4);
158 		if (ret) {
159 			if (ret == -ETIMEDOUT) {
160 				if (retries--)
161 					continue;
162 				ret = -EIO;
163 			}
164 			return ret;
165 		}
166 
167 		size -= nbytes;
168 		address += nbytes;
169 		buf += nbytes;
170 	} while (size > 0);
171 
172 	return 0;
173 }
174 
usb4_switch_op(struct tb_switch * sw,u16 opcode,u8 * status)175 static int usb4_switch_op(struct tb_switch *sw, u16 opcode, u8 *status)
176 {
177 	u32 val;
178 	int ret;
179 
180 	val = opcode | ROUTER_CS_26_OV;
181 	ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
182 	if (ret)
183 		return ret;
184 
185 	ret = usb4_switch_wait_for_bit(sw, ROUTER_CS_26, ROUTER_CS_26_OV, 0, 500);
186 	if (ret)
187 		return ret;
188 
189 	ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
190 	if (ret)
191 		return ret;
192 
193 	if (val & ROUTER_CS_26_ONS)
194 		return -EOPNOTSUPP;
195 
196 	*status = (val & ROUTER_CS_26_STATUS_MASK) >> ROUTER_CS_26_STATUS_SHIFT;
197 	return 0;
198 }
199 
usb4_switch_check_wakes(struct tb_switch * sw)200 static void usb4_switch_check_wakes(struct tb_switch *sw)
201 {
202 	struct tb_port *port;
203 	bool wakeup = false;
204 	u32 val;
205 
206 	if (!device_may_wakeup(&sw->dev))
207 		return;
208 
209 	if (tb_route(sw)) {
210 		if (tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1))
211 			return;
212 
213 		tb_sw_dbg(sw, "PCIe wake: %s, USB3 wake: %s\n",
214 			  (val & ROUTER_CS_6_WOPS) ? "yes" : "no",
215 			  (val & ROUTER_CS_6_WOUS) ? "yes" : "no");
216 
217 		wakeup = val & (ROUTER_CS_6_WOPS | ROUTER_CS_6_WOUS);
218 	}
219 
220 	/* Check for any connected downstream ports for USB4 wake */
221 	tb_switch_for_each_port(sw, port) {
222 		if (!tb_port_has_remote(port))
223 			continue;
224 
225 		if (tb_port_read(port, &val, TB_CFG_PORT,
226 				 port->cap_usb4 + PORT_CS_18, 1))
227 			break;
228 
229 		tb_port_dbg(port, "USB4 wake: %s\n",
230 			    (val & PORT_CS_18_WOU4S) ? "yes" : "no");
231 
232 		if (val & PORT_CS_18_WOU4S)
233 			wakeup = true;
234 	}
235 
236 	if (wakeup)
237 		pm_wakeup_event(&sw->dev, 0);
238 }
239 
link_is_usb4(struct tb_port * port)240 static bool link_is_usb4(struct tb_port *port)
241 {
242 	u32 val;
243 
244 	if (!port->cap_usb4)
245 		return false;
246 
247 	if (tb_port_read(port, &val, TB_CFG_PORT,
248 			 port->cap_usb4 + PORT_CS_18, 1))
249 		return false;
250 
251 	return !(val & PORT_CS_18_TCM);
252 }
253 
254 /**
255  * usb4_switch_setup() - Additional setup for USB4 device
256  * @sw: USB4 router to setup
257  *
258  * USB4 routers need additional settings in order to enable all the
259  * tunneling. This function enables USB and PCIe tunneling if it can be
260  * enabled (e.g the parent switch also supports them). If USB tunneling
261  * is not available for some reason (like that there is Thunderbolt 3
262  * switch upstream) then the internal xHCI controller is enabled
263  * instead.
264  */
usb4_switch_setup(struct tb_switch * sw)265 int usb4_switch_setup(struct tb_switch *sw)
266 {
267 	struct tb_port *downstream_port;
268 	struct tb_switch *parent;
269 	bool tbt3, xhci;
270 	u32 val = 0;
271 	int ret;
272 
273 	usb4_switch_check_wakes(sw);
274 
275 	if (!tb_route(sw))
276 		return 0;
277 
278 	ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1);
279 	if (ret)
280 		return ret;
281 
282 	parent = tb_switch_parent(sw);
283 	downstream_port = tb_port_at(tb_route(sw), parent);
284 	sw->link_usb4 = link_is_usb4(downstream_port);
285 	tb_sw_dbg(sw, "link: %s\n", sw->link_usb4 ? "USB4" : "TBT3");
286 
287 	xhci = val & ROUTER_CS_6_HCI;
288 	tbt3 = !(val & ROUTER_CS_6_TNS);
289 
290 	tb_sw_dbg(sw, "TBT3 support: %s, xHCI: %s\n",
291 		  tbt3 ? "yes" : "no", xhci ? "yes" : "no");
292 
293 	ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
294 	if (ret)
295 		return ret;
296 
297 	if (sw->link_usb4 && tb_switch_find_port(parent, TB_TYPE_USB3_DOWN)) {
298 		val |= ROUTER_CS_5_UTO;
299 		xhci = false;
300 	}
301 
302 	/* Only enable PCIe tunneling if the parent router supports it */
303 	if (tb_switch_find_port(parent, TB_TYPE_PCIE_DOWN)) {
304 		val |= ROUTER_CS_5_PTO;
305 		/*
306 		 * xHCI can be enabled if PCIe tunneling is supported
307 		 * and the parent does not have any USB3 dowstream
308 		 * adapters (so we cannot do USB 3.x tunneling).
309 		 */
310 		if (xhci)
311 			val |= ROUTER_CS_5_HCO;
312 	}
313 
314 	/* TBT3 supported by the CM */
315 	val |= ROUTER_CS_5_C3S;
316 	/* Tunneling configuration is ready now */
317 	val |= ROUTER_CS_5_CV;
318 
319 	ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
320 	if (ret)
321 		return ret;
322 
323 	return usb4_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_CR,
324 					ROUTER_CS_6_CR, 50);
325 }
326 
327 /**
328  * usb4_switch_read_uid() - Read UID from USB4 router
329  * @sw: USB4 router
330  * @uid: UID is stored here
331  *
332  * Reads 64-bit UID from USB4 router config space.
333  */
usb4_switch_read_uid(struct tb_switch * sw,u64 * uid)334 int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid)
335 {
336 	return tb_sw_read(sw, uid, TB_CFG_SWITCH, ROUTER_CS_7, 2);
337 }
338 
usb4_switch_drom_read_block(void * data,unsigned int dwaddress,void * buf,size_t dwords)339 static int usb4_switch_drom_read_block(void *data,
340 				       unsigned int dwaddress, void *buf,
341 				       size_t dwords)
342 {
343 	struct tb_switch *sw = data;
344 	u8 status = 0;
345 	u32 metadata;
346 	int ret;
347 
348 	metadata = (dwords << USB4_DROM_SIZE_SHIFT) & USB4_DROM_SIZE_MASK;
349 	metadata |= (dwaddress << USB4_DROM_ADDRESS_SHIFT) &
350 		USB4_DROM_ADDRESS_MASK;
351 
352 	ret = usb4_switch_op_write_metadata(sw, metadata);
353 	if (ret)
354 		return ret;
355 
356 	ret = usb4_switch_op(sw, USB4_SWITCH_OP_DROM_READ, &status);
357 	if (ret)
358 		return ret;
359 
360 	if (status)
361 		return -EIO;
362 
363 	return usb4_switch_op_read_data(sw, buf, dwords);
364 }
365 
366 /**
367  * usb4_switch_drom_read() - Read arbitrary bytes from USB4 router DROM
368  * @sw: USB4 router
369  * @address: Byte address inside DROM to start reading
370  * @buf: Buffer where the DROM content is stored
371  * @size: Number of bytes to read from DROM
372  *
373  * Uses USB4 router operations to read router DROM. For devices this
374  * should always work but for hosts it may return %-EOPNOTSUPP in which
375  * case the host router does not have DROM.
376  */
usb4_switch_drom_read(struct tb_switch * sw,unsigned int address,void * buf,size_t size)377 int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
378 			  size_t size)
379 {
380 	return usb4_do_read_data(address, buf, size,
381 				 usb4_switch_drom_read_block, sw);
382 }
383 
384 /**
385  * usb4_switch_lane_bonding_possible() - Are conditions met for lane bonding
386  * @sw: USB4 router
387  *
388  * Checks whether conditions are met so that lane bonding can be
389  * established with the upstream router. Call only for device routers.
390  */
usb4_switch_lane_bonding_possible(struct tb_switch * sw)391 bool usb4_switch_lane_bonding_possible(struct tb_switch *sw)
392 {
393 	struct tb_port *up;
394 	int ret;
395 	u32 val;
396 
397 	up = tb_upstream_port(sw);
398 	ret = tb_port_read(up, &val, TB_CFG_PORT, up->cap_usb4 + PORT_CS_18, 1);
399 	if (ret)
400 		return false;
401 
402 	return !!(val & PORT_CS_18_BE);
403 }
404 
405 /**
406  * usb4_switch_set_wake() - Enabled/disable wake
407  * @sw: USB4 router
408  * @flags: Wakeup flags (%0 to disable)
409  *
410  * Enables/disables router to wake up from sleep.
411  */
usb4_switch_set_wake(struct tb_switch * sw,unsigned int flags)412 int usb4_switch_set_wake(struct tb_switch *sw, unsigned int flags)
413 {
414 	struct tb_port *port;
415 	u64 route = tb_route(sw);
416 	u32 val;
417 	int ret;
418 
419 	/*
420 	 * Enable wakes coming from all USB4 downstream ports (from
421 	 * child routers). For device routers do this also for the
422 	 * upstream USB4 port.
423 	 */
424 	tb_switch_for_each_port(sw, port) {
425 		if (!tb_port_is_null(port))
426 			continue;
427 		if (!route && tb_is_upstream_port(port))
428 			continue;
429 		if (!port->cap_usb4)
430 			continue;
431 
432 		ret = tb_port_read(port, &val, TB_CFG_PORT,
433 				   port->cap_usb4 + PORT_CS_19, 1);
434 		if (ret)
435 			return ret;
436 
437 		val &= ~(PORT_CS_19_WOC | PORT_CS_19_WOD | PORT_CS_19_WOU4);
438 
439 		if (flags & TB_WAKE_ON_CONNECT)
440 			val |= PORT_CS_19_WOC;
441 		if (flags & TB_WAKE_ON_DISCONNECT)
442 			val |= PORT_CS_19_WOD;
443 		if (flags & TB_WAKE_ON_USB4)
444 			val |= PORT_CS_19_WOU4;
445 
446 		ret = tb_port_write(port, &val, TB_CFG_PORT,
447 				    port->cap_usb4 + PORT_CS_19, 1);
448 		if (ret)
449 			return ret;
450 	}
451 
452 	/*
453 	 * Enable wakes from PCIe and USB 3.x on this router. Only
454 	 * needed for device routers.
455 	 */
456 	if (route) {
457 		ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
458 		if (ret)
459 			return ret;
460 
461 		val &= ~(ROUTER_CS_5_WOP | ROUTER_CS_5_WOU);
462 		if (flags & TB_WAKE_ON_USB3)
463 			val |= ROUTER_CS_5_WOU;
464 		if (flags & TB_WAKE_ON_PCIE)
465 			val |= ROUTER_CS_5_WOP;
466 
467 		ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
468 		if (ret)
469 			return ret;
470 	}
471 
472 	return 0;
473 }
474 
475 /**
476  * usb4_switch_set_sleep() - Prepare the router to enter sleep
477  * @sw: USB4 router
478  *
479  * Sets sleep bit for the router. Returns when the router sleep ready
480  * bit has been asserted.
481  */
usb4_switch_set_sleep(struct tb_switch * sw)482 int usb4_switch_set_sleep(struct tb_switch *sw)
483 {
484 	int ret;
485 	u32 val;
486 
487 	/* Set sleep bit and wait for sleep ready to be asserted */
488 	ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
489 	if (ret)
490 		return ret;
491 
492 	val |= ROUTER_CS_5_SLP;
493 
494 	ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
495 	if (ret)
496 		return ret;
497 
498 	return usb4_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_SLPR,
499 					ROUTER_CS_6_SLPR, 500);
500 }
501 
502 /**
503  * usb4_switch_nvm_sector_size() - Return router NVM sector size
504  * @sw: USB4 router
505  *
506  * If the router supports NVM operations this function returns the NVM
507  * sector size in bytes. If NVM operations are not supported returns
508  * %-EOPNOTSUPP.
509  */
usb4_switch_nvm_sector_size(struct tb_switch * sw)510 int usb4_switch_nvm_sector_size(struct tb_switch *sw)
511 {
512 	u32 metadata;
513 	u8 status;
514 	int ret;
515 
516 	ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SECTOR_SIZE, &status);
517 	if (ret)
518 		return ret;
519 
520 	if (status)
521 		return status == 0x2 ? -EOPNOTSUPP : -EIO;
522 
523 	ret = usb4_switch_op_read_metadata(sw, &metadata);
524 	if (ret)
525 		return ret;
526 
527 	return metadata & USB4_NVM_SECTOR_SIZE_MASK;
528 }
529 
usb4_switch_nvm_read_block(void * data,unsigned int dwaddress,void * buf,size_t dwords)530 static int usb4_switch_nvm_read_block(void *data,
531 	unsigned int dwaddress, void *buf, size_t dwords)
532 {
533 	struct tb_switch *sw = data;
534 	u8 status = 0;
535 	u32 metadata;
536 	int ret;
537 
538 	metadata = (dwords << USB4_NVM_READ_LENGTH_SHIFT) &
539 		   USB4_NVM_READ_LENGTH_MASK;
540 	metadata |= (dwaddress << USB4_NVM_READ_OFFSET_SHIFT) &
541 		   USB4_NVM_READ_OFFSET_MASK;
542 
543 	ret = usb4_switch_op_write_metadata(sw, metadata);
544 	if (ret)
545 		return ret;
546 
547 	ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_READ, &status);
548 	if (ret)
549 		return ret;
550 
551 	if (status)
552 		return -EIO;
553 
554 	return usb4_switch_op_read_data(sw, buf, dwords);
555 }
556 
557 /**
558  * usb4_switch_nvm_read() - Read arbitrary bytes from router NVM
559  * @sw: USB4 router
560  * @address: Starting address in bytes
561  * @buf: Read data is placed here
562  * @size: How many bytes to read
563  *
564  * Reads NVM contents of the router. If NVM is not supported returns
565  * %-EOPNOTSUPP.
566  */
usb4_switch_nvm_read(struct tb_switch * sw,unsigned int address,void * buf,size_t size)567 int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
568 			 size_t size)
569 {
570 	return usb4_do_read_data(address, buf, size,
571 				 usb4_switch_nvm_read_block, sw);
572 }
573 
usb4_switch_nvm_set_offset(struct tb_switch * sw,unsigned int address)574 static int usb4_switch_nvm_set_offset(struct tb_switch *sw,
575 				      unsigned int address)
576 {
577 	u32 metadata, dwaddress;
578 	u8 status = 0;
579 	int ret;
580 
581 	dwaddress = address / 4;
582 	metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
583 		   USB4_NVM_SET_OFFSET_MASK;
584 
585 	ret = usb4_switch_op_write_metadata(sw, metadata);
586 	if (ret)
587 		return ret;
588 
589 	ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SET_OFFSET, &status);
590 	if (ret)
591 		return ret;
592 
593 	return status ? -EIO : 0;
594 }
595 
usb4_switch_nvm_write_next_block(void * data,const void * buf,size_t dwords)596 static int usb4_switch_nvm_write_next_block(void *data, const void *buf,
597 					    size_t dwords)
598 {
599 	struct tb_switch *sw = data;
600 	u8 status;
601 	int ret;
602 
603 	ret = usb4_switch_op_write_data(sw, buf, dwords);
604 	if (ret)
605 		return ret;
606 
607 	ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_WRITE, &status);
608 	if (ret)
609 		return ret;
610 
611 	return status ? -EIO : 0;
612 }
613 
614 /**
615  * usb4_switch_nvm_write() - Write to the router NVM
616  * @sw: USB4 router
617  * @address: Start address where to write in bytes
618  * @buf: Pointer to the data to write
619  * @size: Size of @buf in bytes
620  *
621  * Writes @buf to the router NVM using USB4 router operations. If NVM
622  * write is not supported returns %-EOPNOTSUPP.
623  */
usb4_switch_nvm_write(struct tb_switch * sw,unsigned int address,const void * buf,size_t size)624 int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
625 			  const void *buf, size_t size)
626 {
627 	int ret;
628 
629 	ret = usb4_switch_nvm_set_offset(sw, address);
630 	if (ret)
631 		return ret;
632 
633 	return usb4_do_write_data(address, buf, size,
634 				  usb4_switch_nvm_write_next_block, sw);
635 }
636 
637 /**
638  * usb4_switch_nvm_authenticate() - Authenticate new NVM
639  * @sw: USB4 router
640  *
641  * After the new NVM has been written via usb4_switch_nvm_write(), this
642  * function triggers NVM authentication process. If the authentication
643  * is successful the router is power cycled and the new NVM starts
644  * running. In case of failure returns negative errno.
645  */
usb4_switch_nvm_authenticate(struct tb_switch * sw)646 int usb4_switch_nvm_authenticate(struct tb_switch *sw)
647 {
648 	u8 status = 0;
649 	int ret;
650 
651 	ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_AUTH, &status);
652 	if (ret)
653 		return ret;
654 
655 	switch (status) {
656 	case 0x0:
657 		tb_sw_dbg(sw, "NVM authentication successful\n");
658 		return 0;
659 	case 0x1:
660 		return -EINVAL;
661 	case 0x2:
662 		return -EAGAIN;
663 	case 0x3:
664 		return -EOPNOTSUPP;
665 	default:
666 		return -EIO;
667 	}
668 }
669 
670 /**
671  * usb4_switch_query_dp_resource() - Query availability of DP IN resource
672  * @sw: USB4 router
673  * @in: DP IN adapter
674  *
675  * For DP tunneling this function can be used to query availability of
676  * DP IN resource. Returns true if the resource is available for DP
677  * tunneling, false otherwise.
678  */
usb4_switch_query_dp_resource(struct tb_switch * sw,struct tb_port * in)679 bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
680 {
681 	u8 status;
682 	int ret;
683 
684 	ret = usb4_switch_op_write_metadata(sw, in->port);
685 	if (ret)
686 		return false;
687 
688 	ret = usb4_switch_op(sw, USB4_SWITCH_OP_QUERY_DP_RESOURCE, &status);
689 	/*
690 	 * If DP resource allocation is not supported assume it is
691 	 * always available.
692 	 */
693 	if (ret == -EOPNOTSUPP)
694 		return true;
695 	else if (ret)
696 		return false;
697 
698 	return !status;
699 }
700 
701 /**
702  * usb4_switch_alloc_dp_resource() - Allocate DP IN resource
703  * @sw: USB4 router
704  * @in: DP IN adapter
705  *
706  * Allocates DP IN resource for DP tunneling using USB4 router
707  * operations. If the resource was allocated returns %0. Otherwise
708  * returns negative errno, in particular %-EBUSY if the resource is
709  * already allocated.
710  */
usb4_switch_alloc_dp_resource(struct tb_switch * sw,struct tb_port * in)711 int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
712 {
713 	u8 status;
714 	int ret;
715 
716 	ret = usb4_switch_op_write_metadata(sw, in->port);
717 	if (ret)
718 		return ret;
719 
720 	ret = usb4_switch_op(sw, USB4_SWITCH_OP_ALLOC_DP_RESOURCE, &status);
721 	if (ret == -EOPNOTSUPP)
722 		return 0;
723 	else if (ret)
724 		return ret;
725 
726 	return status ? -EBUSY : 0;
727 }
728 
729 /**
730  * usb4_switch_dealloc_dp_resource() - Releases allocated DP IN resource
731  * @sw: USB4 router
732  * @in: DP IN adapter
733  *
734  * Releases the previously allocated DP IN resource.
735  */
usb4_switch_dealloc_dp_resource(struct tb_switch * sw,struct tb_port * in)736 int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
737 {
738 	u8 status;
739 	int ret;
740 
741 	ret = usb4_switch_op_write_metadata(sw, in->port);
742 	if (ret)
743 		return ret;
744 
745 	ret = usb4_switch_op(sw, USB4_SWITCH_OP_DEALLOC_DP_RESOURCE, &status);
746 	if (ret == -EOPNOTSUPP)
747 		return 0;
748 	else if (ret)
749 		return ret;
750 
751 	return status ? -EIO : 0;
752 }
753 
usb4_port_idx(const struct tb_switch * sw,const struct tb_port * port)754 static int usb4_port_idx(const struct tb_switch *sw, const struct tb_port *port)
755 {
756 	struct tb_port *p;
757 	int usb4_idx = 0;
758 
759 	/* Assume port is primary */
760 	tb_switch_for_each_port(sw, p) {
761 		if (!tb_port_is_null(p))
762 			continue;
763 		if (tb_is_upstream_port(p))
764 			continue;
765 		if (!p->link_nr) {
766 			if (p == port)
767 				break;
768 			usb4_idx++;
769 		}
770 	}
771 
772 	return usb4_idx;
773 }
774 
775 /**
776  * usb4_switch_map_pcie_down() - Map USB4 port to a PCIe downstream adapter
777  * @sw: USB4 router
778  * @port: USB4 port
779  *
780  * USB4 routers have direct mapping between USB4 ports and PCIe
781  * downstream adapters where the PCIe topology is extended. This
782  * function returns the corresponding downstream PCIe adapter or %NULL
783  * if no such mapping was possible.
784  */
usb4_switch_map_pcie_down(struct tb_switch * sw,const struct tb_port * port)785 struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
786 					  const struct tb_port *port)
787 {
788 	int usb4_idx = usb4_port_idx(sw, port);
789 	struct tb_port *p;
790 	int pcie_idx = 0;
791 
792 	/* Find PCIe down port matching usb4_port */
793 	tb_switch_for_each_port(sw, p) {
794 		if (!tb_port_is_pcie_down(p))
795 			continue;
796 
797 		if (pcie_idx == usb4_idx)
798 			return p;
799 
800 		pcie_idx++;
801 	}
802 
803 	return NULL;
804 }
805 
806 /**
807  * usb4_switch_map_usb3_down() - Map USB4 port to a USB3 downstream adapter
808  * @sw: USB4 router
809  * @port: USB4 port
810  *
811  * USB4 routers have direct mapping between USB4 ports and USB 3.x
812  * downstream adapters where the USB 3.x topology is extended. This
813  * function returns the corresponding downstream USB 3.x adapter or
814  * %NULL if no such mapping was possible.
815  */
usb4_switch_map_usb3_down(struct tb_switch * sw,const struct tb_port * port)816 struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
817 					  const struct tb_port *port)
818 {
819 	int usb4_idx = usb4_port_idx(sw, port);
820 	struct tb_port *p;
821 	int usb_idx = 0;
822 
823 	/* Find USB3 down port matching usb4_port */
824 	tb_switch_for_each_port(sw, p) {
825 		if (!tb_port_is_usb3_down(p))
826 			continue;
827 
828 		if (usb_idx == usb4_idx)
829 			return p;
830 
831 		usb_idx++;
832 	}
833 
834 	return NULL;
835 }
836 
837 /**
838  * usb4_port_unlock() - Unlock USB4 downstream port
839  * @port: USB4 port to unlock
840  *
841  * Unlocks USB4 downstream port so that the connection manager can
842  * access the router below this port.
843  */
usb4_port_unlock(struct tb_port * port)844 int usb4_port_unlock(struct tb_port *port)
845 {
846 	int ret;
847 	u32 val;
848 
849 	ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
850 	if (ret)
851 		return ret;
852 
853 	val &= ~ADP_CS_4_LCK;
854 	return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
855 }
856 
usb4_port_set_configured(struct tb_port * port,bool configured)857 static int usb4_port_set_configured(struct tb_port *port, bool configured)
858 {
859 	int ret;
860 	u32 val;
861 
862 	if (!port->cap_usb4)
863 		return -EINVAL;
864 
865 	ret = tb_port_read(port, &val, TB_CFG_PORT,
866 			   port->cap_usb4 + PORT_CS_19, 1);
867 	if (ret)
868 		return ret;
869 
870 	if (configured)
871 		val |= PORT_CS_19_PC;
872 	else
873 		val &= ~PORT_CS_19_PC;
874 
875 	return tb_port_write(port, &val, TB_CFG_PORT,
876 			     port->cap_usb4 + PORT_CS_19, 1);
877 }
878 
879 /**
880  * usb4_port_configure() - Set USB4 port configured
881  * @port: USB4 router
882  *
883  * Sets the USB4 link to be configured for power management purposes.
884  */
usb4_port_configure(struct tb_port * port)885 int usb4_port_configure(struct tb_port *port)
886 {
887 	return usb4_port_set_configured(port, true);
888 }
889 
890 /**
891  * usb4_port_unconfigure() - Set USB4 port unconfigured
892  * @port: USB4 router
893  *
894  * Sets the USB4 link to be unconfigured for power management purposes.
895  */
usb4_port_unconfigure(struct tb_port * port)896 void usb4_port_unconfigure(struct tb_port *port)
897 {
898 	usb4_port_set_configured(port, false);
899 }
900 
usb4_set_xdomain_configured(struct tb_port * port,bool configured)901 static int usb4_set_xdomain_configured(struct tb_port *port, bool configured)
902 {
903 	int ret;
904 	u32 val;
905 
906 	if (!port->cap_usb4)
907 		return -EINVAL;
908 
909 	ret = tb_port_read(port, &val, TB_CFG_PORT,
910 			   port->cap_usb4 + PORT_CS_19, 1);
911 	if (ret)
912 		return ret;
913 
914 	if (configured)
915 		val |= PORT_CS_19_PID;
916 	else
917 		val &= ~PORT_CS_19_PID;
918 
919 	return tb_port_write(port, &val, TB_CFG_PORT,
920 			     port->cap_usb4 + PORT_CS_19, 1);
921 }
922 
923 /**
924  * usb4_port_configure_xdomain() - Configure port for XDomain
925  * @port: USB4 port connected to another host
926  *
927  * Marks the USB4 port as being connected to another host. Returns %0 in
928  * success and negative errno in failure.
929  */
usb4_port_configure_xdomain(struct tb_port * port)930 int usb4_port_configure_xdomain(struct tb_port *port)
931 {
932 	return usb4_set_xdomain_configured(port, true);
933 }
934 
935 /**
936  * usb4_port_unconfigure_xdomain() - Unconfigure port for XDomain
937  * @port: USB4 port that was connected to another host
938  *
939  * Clears USB4 port from being marked as XDomain.
940  */
usb4_port_unconfigure_xdomain(struct tb_port * port)941 void usb4_port_unconfigure_xdomain(struct tb_port *port)
942 {
943 	usb4_set_xdomain_configured(port, false);
944 }
945 
usb4_port_wait_for_bit(struct tb_port * port,u32 offset,u32 bit,u32 value,int timeout_msec)946 static int usb4_port_wait_for_bit(struct tb_port *port, u32 offset, u32 bit,
947 				  u32 value, int timeout_msec)
948 {
949 	ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
950 
951 	do {
952 		u32 val;
953 		int ret;
954 
955 		ret = tb_port_read(port, &val, TB_CFG_PORT, offset, 1);
956 		if (ret)
957 			return ret;
958 
959 		if ((val & bit) == value)
960 			return 0;
961 
962 		usleep_range(50, 100);
963 	} while (ktime_before(ktime_get(), timeout));
964 
965 	return -ETIMEDOUT;
966 }
967 
usb4_port_read_data(struct tb_port * port,void * data,size_t dwords)968 static int usb4_port_read_data(struct tb_port *port, void *data, size_t dwords)
969 {
970 	if (dwords > USB4_DATA_DWORDS)
971 		return -EINVAL;
972 
973 	return tb_port_read(port, data, TB_CFG_PORT, port->cap_usb4 + PORT_CS_2,
974 			    dwords);
975 }
976 
usb4_port_write_data(struct tb_port * port,const void * data,size_t dwords)977 static int usb4_port_write_data(struct tb_port *port, const void *data,
978 				size_t dwords)
979 {
980 	if (dwords > USB4_DATA_DWORDS)
981 		return -EINVAL;
982 
983 	return tb_port_write(port, data, TB_CFG_PORT, port->cap_usb4 + PORT_CS_2,
984 			     dwords);
985 }
986 
usb4_port_sb_read(struct tb_port * port,enum usb4_sb_target target,u8 index,u8 reg,void * buf,u8 size)987 static int usb4_port_sb_read(struct tb_port *port, enum usb4_sb_target target,
988 			     u8 index, u8 reg, void *buf, u8 size)
989 {
990 	size_t dwords = DIV_ROUND_UP(size, 4);
991 	int ret;
992 	u32 val;
993 
994 	if (!port->cap_usb4)
995 		return -EINVAL;
996 
997 	val = reg;
998 	val |= size << PORT_CS_1_LENGTH_SHIFT;
999 	val |= (target << PORT_CS_1_TARGET_SHIFT) & PORT_CS_1_TARGET_MASK;
1000 	if (target == USB4_SB_TARGET_RETIMER)
1001 		val |= (index << PORT_CS_1_RETIMER_INDEX_SHIFT);
1002 	val |= PORT_CS_1_PND;
1003 
1004 	ret = tb_port_write(port, &val, TB_CFG_PORT,
1005 			    port->cap_usb4 + PORT_CS_1, 1);
1006 	if (ret)
1007 		return ret;
1008 
1009 	ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_1,
1010 				     PORT_CS_1_PND, 0, 500);
1011 	if (ret)
1012 		return ret;
1013 
1014 	ret = tb_port_read(port, &val, TB_CFG_PORT,
1015 			    port->cap_usb4 + PORT_CS_1, 1);
1016 	if (ret)
1017 		return ret;
1018 
1019 	if (val & PORT_CS_1_NR)
1020 		return -ENODEV;
1021 	if (val & PORT_CS_1_RC)
1022 		return -EIO;
1023 
1024 	return buf ? usb4_port_read_data(port, buf, dwords) : 0;
1025 }
1026 
usb4_port_sb_write(struct tb_port * port,enum usb4_sb_target target,u8 index,u8 reg,const void * buf,u8 size)1027 static int usb4_port_sb_write(struct tb_port *port, enum usb4_sb_target target,
1028 			      u8 index, u8 reg, const void *buf, u8 size)
1029 {
1030 	size_t dwords = DIV_ROUND_UP(size, 4);
1031 	int ret;
1032 	u32 val;
1033 
1034 	if (!port->cap_usb4)
1035 		return -EINVAL;
1036 
1037 	if (buf) {
1038 		ret = usb4_port_write_data(port, buf, dwords);
1039 		if (ret)
1040 			return ret;
1041 	}
1042 
1043 	val = reg;
1044 	val |= size << PORT_CS_1_LENGTH_SHIFT;
1045 	val |= PORT_CS_1_WNR_WRITE;
1046 	val |= (target << PORT_CS_1_TARGET_SHIFT) & PORT_CS_1_TARGET_MASK;
1047 	if (target == USB4_SB_TARGET_RETIMER)
1048 		val |= (index << PORT_CS_1_RETIMER_INDEX_SHIFT);
1049 	val |= PORT_CS_1_PND;
1050 
1051 	ret = tb_port_write(port, &val, TB_CFG_PORT,
1052 			    port->cap_usb4 + PORT_CS_1, 1);
1053 	if (ret)
1054 		return ret;
1055 
1056 	ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_1,
1057 				     PORT_CS_1_PND, 0, 500);
1058 	if (ret)
1059 		return ret;
1060 
1061 	ret = tb_port_read(port, &val, TB_CFG_PORT,
1062 			    port->cap_usb4 + PORT_CS_1, 1);
1063 	if (ret)
1064 		return ret;
1065 
1066 	if (val & PORT_CS_1_NR)
1067 		return -ENODEV;
1068 	if (val & PORT_CS_1_RC)
1069 		return -EIO;
1070 
1071 	return 0;
1072 }
1073 
usb4_port_sb_op(struct tb_port * port,enum usb4_sb_target target,u8 index,enum usb4_sb_opcode opcode,int timeout_msec)1074 static int usb4_port_sb_op(struct tb_port *port, enum usb4_sb_target target,
1075 			   u8 index, enum usb4_sb_opcode opcode, int timeout_msec)
1076 {
1077 	ktime_t timeout;
1078 	u32 val;
1079 	int ret;
1080 
1081 	val = opcode;
1082 	ret = usb4_port_sb_write(port, target, index, USB4_SB_OPCODE, &val,
1083 				 sizeof(val));
1084 	if (ret)
1085 		return ret;
1086 
1087 	timeout = ktime_add_ms(ktime_get(), timeout_msec);
1088 
1089 	do {
1090 		/* Check results */
1091 		ret = usb4_port_sb_read(port, target, index, USB4_SB_OPCODE,
1092 					&val, sizeof(val));
1093 		if (ret)
1094 			return ret;
1095 
1096 		switch (val) {
1097 		case 0:
1098 			return 0;
1099 
1100 		case USB4_SB_OPCODE_ERR:
1101 			return -EAGAIN;
1102 
1103 		case USB4_SB_OPCODE_ONS:
1104 			return -EOPNOTSUPP;
1105 
1106 		default:
1107 			if (val != opcode)
1108 				return -EIO;
1109 			break;
1110 		}
1111 	} while (ktime_before(ktime_get(), timeout));
1112 
1113 	return -ETIMEDOUT;
1114 }
1115 
1116 /**
1117  * usb4_port_enumerate_retimers() - Send RT broadcast transaction
1118  * @port: USB4 port
1119  *
1120  * This forces the USB4 port to send broadcast RT transaction which
1121  * makes the retimers on the link to assign index to themselves. Returns
1122  * %0 in case of success and negative errno if there was an error.
1123  */
usb4_port_enumerate_retimers(struct tb_port * port)1124 int usb4_port_enumerate_retimers(struct tb_port *port)
1125 {
1126 	u32 val;
1127 
1128 	val = USB4_SB_OPCODE_ENUMERATE_RETIMERS;
1129 	return usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1130 				  USB4_SB_OPCODE, &val, sizeof(val));
1131 }
1132 
usb4_port_retimer_op(struct tb_port * port,u8 index,enum usb4_sb_opcode opcode,int timeout_msec)1133 static inline int usb4_port_retimer_op(struct tb_port *port, u8 index,
1134 				       enum usb4_sb_opcode opcode,
1135 				       int timeout_msec)
1136 {
1137 	return usb4_port_sb_op(port, USB4_SB_TARGET_RETIMER, index, opcode,
1138 			       timeout_msec);
1139 }
1140 
1141 /**
1142  * usb4_port_retimer_read() - Read from retimer sideband registers
1143  * @port: USB4 port
1144  * @index: Retimer index
1145  * @reg: Sideband register to read
1146  * @buf: Data from @reg is stored here
1147  * @size: Number of bytes to read
1148  *
1149  * Function reads retimer sideband registers starting from @reg. The
1150  * retimer is connected to @port at @index. Returns %0 in case of
1151  * success, and read data is copied to @buf. If there is no retimer
1152  * present at given @index returns %-ENODEV. In any other failure
1153  * returns negative errno.
1154  */
usb4_port_retimer_read(struct tb_port * port,u8 index,u8 reg,void * buf,u8 size)1155 int usb4_port_retimer_read(struct tb_port *port, u8 index, u8 reg, void *buf,
1156 			   u8 size)
1157 {
1158 	return usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index, reg, buf,
1159 				 size);
1160 }
1161 
1162 /**
1163  * usb4_port_retimer_write() - Write to retimer sideband registers
1164  * @port: USB4 port
1165  * @index: Retimer index
1166  * @reg: Sideband register to write
1167  * @buf: Data that is written starting from @reg
1168  * @size: Number of bytes to write
1169  *
1170  * Writes retimer sideband registers starting from @reg. The retimer is
1171  * connected to @port at @index. Returns %0 in case of success. If there
1172  * is no retimer present at given @index returns %-ENODEV. In any other
1173  * failure returns negative errno.
1174  */
usb4_port_retimer_write(struct tb_port * port,u8 index,u8 reg,const void * buf,u8 size)1175 int usb4_port_retimer_write(struct tb_port *port, u8 index, u8 reg,
1176 			    const void *buf, u8 size)
1177 {
1178 	return usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index, reg, buf,
1179 				  size);
1180 }
1181 
1182 /**
1183  * usb4_port_retimer_is_last() - Is the retimer last on-board retimer
1184  * @port: USB4 port
1185  * @index: Retimer index
1186  *
1187  * If the retimer at @index is last one (connected directly to the
1188  * Type-C port) this function returns %1. If it is not returns %0. If
1189  * the retimer is not present returns %-ENODEV. Otherwise returns
1190  * negative errno.
1191  */
usb4_port_retimer_is_last(struct tb_port * port,u8 index)1192 int usb4_port_retimer_is_last(struct tb_port *port, u8 index)
1193 {
1194 	u32 metadata;
1195 	int ret;
1196 
1197 	ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_QUERY_LAST_RETIMER,
1198 				   500);
1199 	if (ret)
1200 		return ret;
1201 
1202 	ret = usb4_port_retimer_read(port, index, USB4_SB_METADATA, &metadata,
1203 				     sizeof(metadata));
1204 	return ret ? ret : metadata & 1;
1205 }
1206 
1207 /**
1208  * usb4_port_retimer_nvm_sector_size() - Read retimer NVM sector size
1209  * @port: USB4 port
1210  * @index: Retimer index
1211  *
1212  * Reads NVM sector size (in bytes) of a retimer at @index. This
1213  * operation can be used to determine whether the retimer supports NVM
1214  * upgrade for example. Returns sector size in bytes or negative errno
1215  * in case of error. Specifically returns %-ENODEV if there is no
1216  * retimer at @index.
1217  */
usb4_port_retimer_nvm_sector_size(struct tb_port * port,u8 index)1218 int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index)
1219 {
1220 	u32 metadata;
1221 	int ret;
1222 
1223 	ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_GET_NVM_SECTOR_SIZE,
1224 				   500);
1225 	if (ret)
1226 		return ret;
1227 
1228 	ret = usb4_port_retimer_read(port, index, USB4_SB_METADATA, &metadata,
1229 				     sizeof(metadata));
1230 	return ret ? ret : metadata & USB4_NVM_SECTOR_SIZE_MASK;
1231 }
1232 
usb4_port_retimer_nvm_set_offset(struct tb_port * port,u8 index,unsigned int address)1233 static int usb4_port_retimer_nvm_set_offset(struct tb_port *port, u8 index,
1234 					    unsigned int address)
1235 {
1236 	u32 metadata, dwaddress;
1237 	int ret;
1238 
1239 	dwaddress = address / 4;
1240 	metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
1241 		  USB4_NVM_SET_OFFSET_MASK;
1242 
1243 	ret = usb4_port_retimer_write(port, index, USB4_SB_METADATA, &metadata,
1244 				      sizeof(metadata));
1245 	if (ret)
1246 		return ret;
1247 
1248 	return usb4_port_retimer_op(port, index, USB4_SB_OPCODE_NVM_SET_OFFSET,
1249 				    500);
1250 }
1251 
1252 struct retimer_info {
1253 	struct tb_port *port;
1254 	u8 index;
1255 };
1256 
usb4_port_retimer_nvm_write_next_block(void * data,const void * buf,size_t dwords)1257 static int usb4_port_retimer_nvm_write_next_block(void *data, const void *buf,
1258 						  size_t dwords)
1259 
1260 {
1261 	const struct retimer_info *info = data;
1262 	struct tb_port *port = info->port;
1263 	u8 index = info->index;
1264 	int ret;
1265 
1266 	ret = usb4_port_retimer_write(port, index, USB4_SB_DATA,
1267 				      buf, dwords * 4);
1268 	if (ret)
1269 		return ret;
1270 
1271 	return usb4_port_retimer_op(port, index,
1272 			USB4_SB_OPCODE_NVM_BLOCK_WRITE, 1000);
1273 }
1274 
1275 /**
1276  * usb4_port_retimer_nvm_write() - Write to retimer NVM
1277  * @port: USB4 port
1278  * @index: Retimer index
1279  * @address: Byte address where to start the write
1280  * @buf: Data to write
1281  * @size: Size in bytes how much to write
1282  *
1283  * Writes @size bytes from @buf to the retimer NVM. Used for NVM
1284  * upgrade. Returns %0 if the data was written successfully and negative
1285  * errno in case of failure. Specifically returns %-ENODEV if there is
1286  * no retimer at @index.
1287  */
usb4_port_retimer_nvm_write(struct tb_port * port,u8 index,unsigned int address,const void * buf,size_t size)1288 int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index, unsigned int address,
1289 				const void *buf, size_t size)
1290 {
1291 	struct retimer_info info = { .port = port, .index = index };
1292 	int ret;
1293 
1294 	ret = usb4_port_retimer_nvm_set_offset(port, index, address);
1295 	if (ret)
1296 		return ret;
1297 
1298 	return usb4_do_write_data(address, buf, size,
1299 			usb4_port_retimer_nvm_write_next_block, &info);
1300 }
1301 
1302 /**
1303  * usb4_port_retimer_nvm_authenticate() - Start retimer NVM upgrade
1304  * @port: USB4 port
1305  * @index: Retimer index
1306  *
1307  * After the new NVM image has been written via usb4_port_retimer_nvm_write()
1308  * this function can be used to trigger the NVM upgrade process. If
1309  * successful the retimer restarts with the new NVM and may not have the
1310  * index set so one needs to call usb4_port_enumerate_retimers() to
1311  * force index to be assigned.
1312  */
usb4_port_retimer_nvm_authenticate(struct tb_port * port,u8 index)1313 int usb4_port_retimer_nvm_authenticate(struct tb_port *port, u8 index)
1314 {
1315 	u32 val;
1316 
1317 	/*
1318 	 * We need to use the raw operation here because once the
1319 	 * authentication completes the retimer index is not set anymore
1320 	 * so we do not get back the status now.
1321 	 */
1322 	val = USB4_SB_OPCODE_NVM_AUTH_WRITE;
1323 	return usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index,
1324 				  USB4_SB_OPCODE, &val, sizeof(val));
1325 }
1326 
1327 /**
1328  * usb4_port_retimer_nvm_authenticate_status() - Read status of NVM upgrade
1329  * @port: USB4 port
1330  * @index: Retimer index
1331  * @status: Raw status code read from metadata
1332  *
1333  * This can be called after usb4_port_retimer_nvm_authenticate() and
1334  * usb4_port_enumerate_retimers() to fetch status of the NVM upgrade.
1335  *
1336  * Returns %0 if the authentication status was successfully read. The
1337  * completion metadata (the result) is then stored into @status. If
1338  * reading the status fails, returns negative errno.
1339  */
usb4_port_retimer_nvm_authenticate_status(struct tb_port * port,u8 index,u32 * status)1340 int usb4_port_retimer_nvm_authenticate_status(struct tb_port *port, u8 index,
1341 					      u32 *status)
1342 {
1343 	u32 metadata, val;
1344 	int ret;
1345 
1346 	ret = usb4_port_retimer_read(port, index, USB4_SB_OPCODE, &val,
1347 				     sizeof(val));
1348 	if (ret)
1349 		return ret;
1350 
1351 	switch (val) {
1352 	case 0:
1353 		*status = 0;
1354 		return 0;
1355 
1356 	case USB4_SB_OPCODE_ERR:
1357 		ret = usb4_port_retimer_read(port, index, USB4_SB_METADATA,
1358 					     &metadata, sizeof(metadata));
1359 		if (ret)
1360 			return ret;
1361 
1362 		*status = metadata & USB4_SB_METADATA_NVM_AUTH_WRITE_MASK;
1363 		return 0;
1364 
1365 	case USB4_SB_OPCODE_ONS:
1366 		return -EOPNOTSUPP;
1367 
1368 	default:
1369 		return -EIO;
1370 	}
1371 }
1372 
usb4_port_retimer_nvm_read_block(void * data,unsigned int dwaddress,void * buf,size_t dwords)1373 static int usb4_port_retimer_nvm_read_block(void *data, unsigned int dwaddress,
1374 					    void *buf, size_t dwords)
1375 {
1376 	const struct retimer_info *info = data;
1377 	struct tb_port *port = info->port;
1378 	u8 index = info->index;
1379 	u32 metadata;
1380 	int ret;
1381 
1382 	metadata = dwaddress << USB4_NVM_READ_OFFSET_SHIFT;
1383 	if (dwords < USB4_DATA_DWORDS)
1384 		metadata |= dwords << USB4_NVM_READ_LENGTH_SHIFT;
1385 
1386 	ret = usb4_port_retimer_write(port, index, USB4_SB_METADATA, &metadata,
1387 				      sizeof(metadata));
1388 	if (ret)
1389 		return ret;
1390 
1391 	ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_NVM_READ, 500);
1392 	if (ret)
1393 		return ret;
1394 
1395 	return usb4_port_retimer_read(port, index, USB4_SB_DATA, buf,
1396 				      dwords * 4);
1397 }
1398 
1399 /**
1400  * usb4_port_retimer_nvm_read() - Read contents of retimer NVM
1401  * @port: USB4 port
1402  * @index: Retimer index
1403  * @address: NVM address (in bytes) to start reading
1404  * @buf: Data read from NVM is stored here
1405  * @size: Number of bytes to read
1406  *
1407  * Reads retimer NVM and copies the contents to @buf. Returns %0 if the
1408  * read was successful and negative errno in case of failure.
1409  * Specifically returns %-ENODEV if there is no retimer at @index.
1410  */
usb4_port_retimer_nvm_read(struct tb_port * port,u8 index,unsigned int address,void * buf,size_t size)1411 int usb4_port_retimer_nvm_read(struct tb_port *port, u8 index,
1412 			       unsigned int address, void *buf, size_t size)
1413 {
1414 	struct retimer_info info = { .port = port, .index = index };
1415 
1416 	return usb4_do_read_data(address, buf, size,
1417 			usb4_port_retimer_nvm_read_block, &info);
1418 }
1419 
1420 /**
1421  * usb4_usb3_port_max_link_rate() - Maximum support USB3 link rate
1422  * @port: USB3 adapter port
1423  *
1424  * Return maximum supported link rate of a USB3 adapter in Mb/s.
1425  * Negative errno in case of error.
1426  */
usb4_usb3_port_max_link_rate(struct tb_port * port)1427 int usb4_usb3_port_max_link_rate(struct tb_port *port)
1428 {
1429 	int ret, lr;
1430 	u32 val;
1431 
1432 	if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port))
1433 		return -EINVAL;
1434 
1435 	ret = tb_port_read(port, &val, TB_CFG_PORT,
1436 			   port->cap_adap + ADP_USB3_CS_4, 1);
1437 	if (ret)
1438 		return ret;
1439 
1440 	lr = (val & ADP_USB3_CS_4_MSLR_MASK) >> ADP_USB3_CS_4_MSLR_SHIFT;
1441 	return lr == ADP_USB3_CS_4_MSLR_20G ? 20000 : 10000;
1442 }
1443 
1444 /**
1445  * usb4_usb3_port_actual_link_rate() - Established USB3 link rate
1446  * @port: USB3 adapter port
1447  *
1448  * Return actual established link rate of a USB3 adapter in Mb/s. If the
1449  * link is not up returns %0 and negative errno in case of failure.
1450  */
usb4_usb3_port_actual_link_rate(struct tb_port * port)1451 int usb4_usb3_port_actual_link_rate(struct tb_port *port)
1452 {
1453 	int ret, lr;
1454 	u32 val;
1455 
1456 	if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port))
1457 		return -EINVAL;
1458 
1459 	ret = tb_port_read(port, &val, TB_CFG_PORT,
1460 			   port->cap_adap + ADP_USB3_CS_4, 1);
1461 	if (ret)
1462 		return ret;
1463 
1464 	if (!(val & ADP_USB3_CS_4_ULV))
1465 		return 0;
1466 
1467 	lr = val & ADP_USB3_CS_4_ALR_MASK;
1468 	return lr == ADP_USB3_CS_4_ALR_20G ? 20000 : 10000;
1469 }
1470 
usb4_usb3_port_cm_request(struct tb_port * port,bool request)1471 static int usb4_usb3_port_cm_request(struct tb_port *port, bool request)
1472 {
1473 	int ret;
1474 	u32 val;
1475 
1476 	if (!tb_port_is_usb3_down(port))
1477 		return -EINVAL;
1478 	if (tb_route(port->sw))
1479 		return -EINVAL;
1480 
1481 	ret = tb_port_read(port, &val, TB_CFG_PORT,
1482 			   port->cap_adap + ADP_USB3_CS_2, 1);
1483 	if (ret)
1484 		return ret;
1485 
1486 	if (request)
1487 		val |= ADP_USB3_CS_2_CMR;
1488 	else
1489 		val &= ~ADP_USB3_CS_2_CMR;
1490 
1491 	ret = tb_port_write(port, &val, TB_CFG_PORT,
1492 			    port->cap_adap + ADP_USB3_CS_2, 1);
1493 	if (ret)
1494 		return ret;
1495 
1496 	/*
1497 	 * We can use val here directly as the CMR bit is in the same place
1498 	 * as HCA. Just mask out others.
1499 	 */
1500 	val &= ADP_USB3_CS_2_CMR;
1501 	return usb4_port_wait_for_bit(port, port->cap_adap + ADP_USB3_CS_1,
1502 				      ADP_USB3_CS_1_HCA, val, 1500);
1503 }
1504 
usb4_usb3_port_set_cm_request(struct tb_port * port)1505 static inline int usb4_usb3_port_set_cm_request(struct tb_port *port)
1506 {
1507 	return usb4_usb3_port_cm_request(port, true);
1508 }
1509 
usb4_usb3_port_clear_cm_request(struct tb_port * port)1510 static inline int usb4_usb3_port_clear_cm_request(struct tb_port *port)
1511 {
1512 	return usb4_usb3_port_cm_request(port, false);
1513 }
1514 
usb3_bw_to_mbps(u32 bw,u8 scale)1515 static unsigned int usb3_bw_to_mbps(u32 bw, u8 scale)
1516 {
1517 	unsigned long uframes;
1518 
1519 	uframes = bw * 512UL << scale;
1520 	return DIV_ROUND_CLOSEST(uframes * 8000, 1000 * 1000);
1521 }
1522 
mbps_to_usb3_bw(unsigned int mbps,u8 scale)1523 static u32 mbps_to_usb3_bw(unsigned int mbps, u8 scale)
1524 {
1525 	unsigned long uframes;
1526 
1527 	/* 1 uframe is 1/8 ms (125 us) -> 1 / 8000 s */
1528 	uframes = ((unsigned long)mbps * 1000 *  1000) / 8000;
1529 	return DIV_ROUND_UP(uframes, 512UL << scale);
1530 }
1531 
usb4_usb3_port_read_allocated_bandwidth(struct tb_port * port,int * upstream_bw,int * downstream_bw)1532 static int usb4_usb3_port_read_allocated_bandwidth(struct tb_port *port,
1533 						   int *upstream_bw,
1534 						   int *downstream_bw)
1535 {
1536 	u32 val, bw, scale;
1537 	int ret;
1538 
1539 	ret = tb_port_read(port, &val, TB_CFG_PORT,
1540 			   port->cap_adap + ADP_USB3_CS_2, 1);
1541 	if (ret)
1542 		return ret;
1543 
1544 	ret = tb_port_read(port, &scale, TB_CFG_PORT,
1545 			   port->cap_adap + ADP_USB3_CS_3, 1);
1546 	if (ret)
1547 		return ret;
1548 
1549 	scale &= ADP_USB3_CS_3_SCALE_MASK;
1550 
1551 	bw = val & ADP_USB3_CS_2_AUBW_MASK;
1552 	*upstream_bw = usb3_bw_to_mbps(bw, scale);
1553 
1554 	bw = (val & ADP_USB3_CS_2_ADBW_MASK) >> ADP_USB3_CS_2_ADBW_SHIFT;
1555 	*downstream_bw = usb3_bw_to_mbps(bw, scale);
1556 
1557 	return 0;
1558 }
1559 
1560 /**
1561  * usb4_usb3_port_allocated_bandwidth() - Bandwidth allocated for USB3
1562  * @port: USB3 adapter port
1563  * @upstream_bw: Allocated upstream bandwidth is stored here
1564  * @downstream_bw: Allocated downstream bandwidth is stored here
1565  *
1566  * Stores currently allocated USB3 bandwidth into @upstream_bw and
1567  * @downstream_bw in Mb/s. Returns %0 in case of success and negative
1568  * errno in failure.
1569  */
usb4_usb3_port_allocated_bandwidth(struct tb_port * port,int * upstream_bw,int * downstream_bw)1570 int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
1571 				       int *downstream_bw)
1572 {
1573 	int ret;
1574 
1575 	ret = usb4_usb3_port_set_cm_request(port);
1576 	if (ret)
1577 		return ret;
1578 
1579 	ret = usb4_usb3_port_read_allocated_bandwidth(port, upstream_bw,
1580 						      downstream_bw);
1581 	usb4_usb3_port_clear_cm_request(port);
1582 
1583 	return ret;
1584 }
1585 
usb4_usb3_port_read_consumed_bandwidth(struct tb_port * port,int * upstream_bw,int * downstream_bw)1586 static int usb4_usb3_port_read_consumed_bandwidth(struct tb_port *port,
1587 						  int *upstream_bw,
1588 						  int *downstream_bw)
1589 {
1590 	u32 val, bw, scale;
1591 	int ret;
1592 
1593 	ret = tb_port_read(port, &val, TB_CFG_PORT,
1594 			   port->cap_adap + ADP_USB3_CS_1, 1);
1595 	if (ret)
1596 		return ret;
1597 
1598 	ret = tb_port_read(port, &scale, TB_CFG_PORT,
1599 			   port->cap_adap + ADP_USB3_CS_3, 1);
1600 	if (ret)
1601 		return ret;
1602 
1603 	scale &= ADP_USB3_CS_3_SCALE_MASK;
1604 
1605 	bw = val & ADP_USB3_CS_1_CUBW_MASK;
1606 	*upstream_bw = usb3_bw_to_mbps(bw, scale);
1607 
1608 	bw = (val & ADP_USB3_CS_1_CDBW_MASK) >> ADP_USB3_CS_1_CDBW_SHIFT;
1609 	*downstream_bw = usb3_bw_to_mbps(bw, scale);
1610 
1611 	return 0;
1612 }
1613 
usb4_usb3_port_write_allocated_bandwidth(struct tb_port * port,int upstream_bw,int downstream_bw)1614 static int usb4_usb3_port_write_allocated_bandwidth(struct tb_port *port,
1615 						    int upstream_bw,
1616 						    int downstream_bw)
1617 {
1618 	u32 val, ubw, dbw, scale;
1619 	int ret;
1620 
1621 	/* Read the used scale, hardware default is 0 */
1622 	ret = tb_port_read(port, &scale, TB_CFG_PORT,
1623 			   port->cap_adap + ADP_USB3_CS_3, 1);
1624 	if (ret)
1625 		return ret;
1626 
1627 	scale &= ADP_USB3_CS_3_SCALE_MASK;
1628 	ubw = mbps_to_usb3_bw(upstream_bw, scale);
1629 	dbw = mbps_to_usb3_bw(downstream_bw, scale);
1630 
1631 	ret = tb_port_read(port, &val, TB_CFG_PORT,
1632 			   port->cap_adap + ADP_USB3_CS_2, 1);
1633 	if (ret)
1634 		return ret;
1635 
1636 	val &= ~(ADP_USB3_CS_2_AUBW_MASK | ADP_USB3_CS_2_ADBW_MASK);
1637 	val |= dbw << ADP_USB3_CS_2_ADBW_SHIFT;
1638 	val |= ubw;
1639 
1640 	return tb_port_write(port, &val, TB_CFG_PORT,
1641 			     port->cap_adap + ADP_USB3_CS_2, 1);
1642 }
1643 
1644 /**
1645  * usb4_usb3_port_allocate_bandwidth() - Allocate bandwidth for USB3
1646  * @port: USB3 adapter port
1647  * @upstream_bw: New upstream bandwidth
1648  * @downstream_bw: New downstream bandwidth
1649  *
1650  * This can be used to set how much bandwidth is allocated for the USB3
1651  * tunneled isochronous traffic. @upstream_bw and @downstream_bw are the
1652  * new values programmed to the USB3 adapter allocation registers. If
1653  * the values are lower than what is currently consumed the allocation
1654  * is set to what is currently consumed instead (consumed bandwidth
1655  * cannot be taken away by CM). The actual new values are returned in
1656  * @upstream_bw and @downstream_bw.
1657  *
1658  * Returns %0 in case of success and negative errno if there was a
1659  * failure.
1660  */
usb4_usb3_port_allocate_bandwidth(struct tb_port * port,int * upstream_bw,int * downstream_bw)1661 int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
1662 				      int *downstream_bw)
1663 {
1664 	int ret, consumed_up, consumed_down, allocate_up, allocate_down;
1665 
1666 	ret = usb4_usb3_port_set_cm_request(port);
1667 	if (ret)
1668 		return ret;
1669 
1670 	ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
1671 						     &consumed_down);
1672 	if (ret)
1673 		goto err_request;
1674 
1675 	/* Don't allow it go lower than what is consumed */
1676 	allocate_up = max(*upstream_bw, consumed_up);
1677 	allocate_down = max(*downstream_bw, consumed_down);
1678 
1679 	ret = usb4_usb3_port_write_allocated_bandwidth(port, allocate_up,
1680 						       allocate_down);
1681 	if (ret)
1682 		goto err_request;
1683 
1684 	*upstream_bw = allocate_up;
1685 	*downstream_bw = allocate_down;
1686 
1687 err_request:
1688 	usb4_usb3_port_clear_cm_request(port);
1689 	return ret;
1690 }
1691 
1692 /**
1693  * usb4_usb3_port_release_bandwidth() - Release allocated USB3 bandwidth
1694  * @port: USB3 adapter port
1695  * @upstream_bw: New allocated upstream bandwidth
1696  * @downstream_bw: New allocated downstream bandwidth
1697  *
1698  * Releases USB3 allocated bandwidth down to what is actually consumed.
1699  * The new bandwidth is returned in @upstream_bw and @downstream_bw.
1700  *
1701  * Returns 0% in success and negative errno in case of failure.
1702  */
usb4_usb3_port_release_bandwidth(struct tb_port * port,int * upstream_bw,int * downstream_bw)1703 int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
1704 				     int *downstream_bw)
1705 {
1706 	int ret, consumed_up, consumed_down;
1707 
1708 	ret = usb4_usb3_port_set_cm_request(port);
1709 	if (ret)
1710 		return ret;
1711 
1712 	ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
1713 						     &consumed_down);
1714 	if (ret)
1715 		goto err_request;
1716 
1717 	/*
1718 	 * Always keep 1000 Mb/s to make sure xHCI has at least some
1719 	 * bandwidth available for isochronous traffic.
1720 	 */
1721 	if (consumed_up < 1000)
1722 		consumed_up = 1000;
1723 	if (consumed_down < 1000)
1724 		consumed_down = 1000;
1725 
1726 	ret = usb4_usb3_port_write_allocated_bandwidth(port, consumed_up,
1727 						       consumed_down);
1728 	if (ret)
1729 		goto err_request;
1730 
1731 	*upstream_bw = consumed_up;
1732 	*downstream_bw = consumed_down;
1733 
1734 err_request:
1735 	usb4_usb3_port_clear_cm_request(port);
1736 	return ret;
1737 }
1738