xref: /linux/drivers/thunderbolt/tunnel.c (revision d5a821f66047c2effd06147b84775f9b2ce0f4aa)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Thunderbolt driver - Tunneling support
4  *
5  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6  * Copyright (C) 2019, Intel Corporation
7  */
8 
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/list.h>
12 #include <linux/ktime.h>
13 #include <linux/string_helpers.h>
14 
15 #include "tunnel.h"
16 #include "tb.h"
17 
18 /* PCIe adapters use always HopID of 8 for both directions */
19 #define TB_PCI_HOPID			8
20 
21 #define TB_PCI_PATH_DOWN		0
22 #define TB_PCI_PATH_UP			1
23 
24 #define TB_PCI_PRIORITY			3
25 #define TB_PCI_WEIGHT			1
26 
27 /* USB3 adapters use always HopID of 8 for both directions */
28 #define TB_USB3_HOPID			8
29 
30 #define TB_USB3_PATH_DOWN		0
31 #define TB_USB3_PATH_UP			1
32 
33 #define TB_USB3_PRIORITY		3
34 #define TB_USB3_WEIGHT			2
35 
36 /* DP adapters use HopID 8 for AUX and 9 for Video */
37 #define TB_DP_AUX_TX_HOPID		8
38 #define TB_DP_AUX_RX_HOPID		8
39 #define TB_DP_VIDEO_HOPID		9
40 
41 #define TB_DP_VIDEO_PATH_OUT		0
42 #define TB_DP_AUX_PATH_OUT		1
43 #define TB_DP_AUX_PATH_IN		2
44 
45 #define TB_DP_VIDEO_PRIORITY		1
46 #define TB_DP_VIDEO_WEIGHT		1
47 
48 #define TB_DP_AUX_PRIORITY		2
49 #define TB_DP_AUX_WEIGHT		1
50 
51 /* struct tb_regs_hop::initial_credits is 7 bits wide */
52 #define TB_MAX_CREDITS			127
53 
54 /* Minimum number of credits needed for PCIe path */
55 #define TB_MIN_PCIE_CREDITS		6U
56 /*
57  * Number of credits we try to allocate for each DMA path if not limited
58  * by the host router baMaxHI.
59  */
60 #define TB_DMA_CREDITS			14
61 /* Minimum number of credits for DMA path */
62 #define TB_MIN_DMA_CREDITS		1
63 
64 #define TB_DMA_PRIORITY			5
65 #define TB_DMA_WEIGHT			1
66 
67 /*
68  * Reserve additional bandwidth for USB 3.x and PCIe bulk traffic
69  * according to USB4 v2 Connection Manager guide. This ends up reserving
70  * 1500 Mb/s for PCIe and 3000 Mb/s for USB 3.x taking weights into
71  * account.
72  */
73 #define USB4_V2_PCI_MIN_BANDWIDTH	(1500 * TB_PCI_WEIGHT)
74 #define USB4_V2_USB3_MIN_BANDWIDTH	(1500 * TB_USB3_WEIGHT)
75 
76 /*
77  * According to VESA spec, the DPRX negotiation shall compete in 5
78  * seconds after tunnel is established. Since at least i915 can runtime
79  * suspend if there is nothing connected, and that it polls any new
80  * connections every 10 seconds, we use 12 seconds here.
81  *
82  * These are in ms.
83  */
84 #define TB_DPRX_TIMEOUT			12000
85 #define TB_DPRX_WAIT_TIMEOUT		25
86 #define TB_DPRX_POLL_DELAY		50
87 
88 static int dprx_timeout = TB_DPRX_TIMEOUT;
89 module_param(dprx_timeout, int, 0444);
90 MODULE_PARM_DESC(dprx_timeout,
91 		 "DPRX capability read timeout in ms, -1 waits forever (default: "
92 		 __MODULE_STRING(TB_DPRX_TIMEOUT) ")");
93 
94 static unsigned int dma_credits = TB_DMA_CREDITS;
95 module_param(dma_credits, uint, 0444);
96 MODULE_PARM_DESC(dma_credits, "specify custom credits for DMA tunnels (default: "
97                 __MODULE_STRING(TB_DMA_CREDITS) ")");
98 
99 static bool bw_alloc_mode = true;
100 module_param(bw_alloc_mode, bool, 0444);
101 MODULE_PARM_DESC(bw_alloc_mode,
102 		 "enable bandwidth allocation mode if supported (default: true)");
103 
104 static const char * const tb_tunnel_names[] = { "PCI", "DP", "DMA", "USB3" };
105 
106 static const char * const tb_event_names[] = {
107 	[TB_TUNNEL_ACTIVATED] = "activated",
108 	[TB_TUNNEL_CHANGED] = "changed",
109 	[TB_TUNNEL_DEACTIVATED] = "deactivated",
110 	[TB_TUNNEL_LOW_BANDWIDTH] = "low bandwidth",
111 	[TB_TUNNEL_NO_BANDWIDTH] = "insufficient bandwidth",
112 };
113 
114 /* Synchronizes kref_get()/put() of struct tb_tunnel */
115 static DEFINE_MUTEX(tb_tunnel_lock);
116 
tb_usable_credits(const struct tb_port * port)117 static inline unsigned int tb_usable_credits(const struct tb_port *port)
118 {
119 	return port->total_credits - port->ctl_credits;
120 }
121 
122 /**
123  * tb_available_credits() - Available credits for PCIe and DMA
124  * @port: Lane adapter to check
125  * @max_dp_streams: If non-%NULL stores maximum number of simultaneous DP
126  *		    streams possible through this lane adapter
127  *
128  * Return: Number of available credits.
129  */
tb_available_credits(const struct tb_port * port,size_t * max_dp_streams)130 static unsigned int tb_available_credits(const struct tb_port *port,
131 					 size_t *max_dp_streams)
132 {
133 	const struct tb_switch *sw = port->sw;
134 	int credits, usb3, pcie, spare;
135 	size_t ndp;
136 
137 	usb3 = tb_acpi_may_tunnel_usb3() ? sw->max_usb3_credits : 0;
138 	pcie = tb_acpi_may_tunnel_pcie() ? sw->max_pcie_credits : 0;
139 
140 	if (tb_acpi_is_xdomain_allowed()) {
141 		spare = min_not_zero(sw->max_dma_credits, dma_credits);
142 		/* Add some credits for potential second DMA tunnel */
143 		spare += TB_MIN_DMA_CREDITS;
144 	} else {
145 		spare = 0;
146 	}
147 
148 	credits = tb_usable_credits(port);
149 	if (tb_acpi_may_tunnel_dp()) {
150 		/*
151 		 * Maximum number of DP streams possible through the
152 		 * lane adapter.
153 		 */
154 		if (sw->min_dp_aux_credits + sw->min_dp_main_credits)
155 			ndp = (credits - (usb3 + pcie + spare)) /
156 			      (sw->min_dp_aux_credits + sw->min_dp_main_credits);
157 		else
158 			ndp = 0;
159 	} else {
160 		ndp = 0;
161 	}
162 	credits -= ndp * (sw->min_dp_aux_credits + sw->min_dp_main_credits);
163 	credits -= usb3;
164 
165 	if (max_dp_streams)
166 		*max_dp_streams = ndp;
167 
168 	return credits > 0 ? credits : 0;
169 }
170 
tb_init_pm_support(struct tb_path_hop * hop)171 static void tb_init_pm_support(struct tb_path_hop *hop)
172 {
173 	struct tb_port *out_port = hop->out_port;
174 	struct tb_port *in_port = hop->in_port;
175 
176 	if (tb_port_is_null(in_port) && tb_port_is_null(out_port) &&
177 	    usb4_switch_version(in_port->sw) >= 2)
178 		hop->pm_support = true;
179 }
180 
tb_tunnel_alloc(struct tb * tb,size_t npaths,enum tb_tunnel_type type)181 static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths,
182 					 enum tb_tunnel_type type)
183 {
184 	struct tb_tunnel *tunnel;
185 
186 	tunnel = kzalloc_flex(*tunnel, paths, npaths);
187 	if (!tunnel)
188 		return NULL;
189 
190 	tunnel->npaths = npaths;
191 
192 	INIT_LIST_HEAD(&tunnel->list);
193 	tunnel->tb = tb;
194 	tunnel->type = type;
195 	kref_init(&tunnel->kref);
196 
197 	return tunnel;
198 }
199 
tb_tunnel_get(struct tb_tunnel * tunnel)200 static void tb_tunnel_get(struct tb_tunnel *tunnel)
201 {
202 	mutex_lock(&tb_tunnel_lock);
203 	kref_get(&tunnel->kref);
204 	mutex_unlock(&tb_tunnel_lock);
205 }
206 
tb_tunnel_destroy(struct kref * kref)207 static void tb_tunnel_destroy(struct kref *kref)
208 {
209 	struct tb_tunnel *tunnel = container_of(kref, typeof(*tunnel), kref);
210 	int i;
211 
212 	if (tunnel->destroy)
213 		tunnel->destroy(tunnel);
214 
215 	for (i = 0; i < tunnel->npaths; i++) {
216 		if (tunnel->paths[i])
217 			tb_path_free(tunnel->paths[i]);
218 	}
219 
220 	kfree(tunnel);
221 }
222 
tb_tunnel_put(struct tb_tunnel * tunnel)223 void tb_tunnel_put(struct tb_tunnel *tunnel)
224 {
225 	mutex_lock(&tb_tunnel_lock);
226 	kref_put(&tunnel->kref, tb_tunnel_destroy);
227 	mutex_unlock(&tb_tunnel_lock);
228 }
229 
230 /**
231  * tb_tunnel_event() - Notify userspace about tunneling event
232  * @tb: Domain where the event occurred
233  * @event: Event that happened
234  * @type: Type of the tunnel in question
235  * @src_port: Tunnel source port (can be %NULL)
236  * @dst_port: Tunnel destination port (can be %NULL)
237  *
238  * Notifies userspace about tunneling @event in the domain. The tunnel
239  * does not need to exist (e.g the tunnel was not activated because
240  * there is not enough bandwidth). If the @src_port and @dst_port are
241  * given fill in full %TUNNEL_DETAILS environment variable. Otherwise
242  * uses the shorter one (just the tunnel type).
243  */
tb_tunnel_event(struct tb * tb,enum tb_tunnel_event event,enum tb_tunnel_type type,const struct tb_port * src_port,const struct tb_port * dst_port)244 void tb_tunnel_event(struct tb *tb, enum tb_tunnel_event event,
245 		     enum tb_tunnel_type type,
246 		     const struct tb_port *src_port,
247 		     const struct tb_port *dst_port)
248 {
249 	char *envp[3] = { NULL };
250 
251 	if (WARN_ON_ONCE(event >= ARRAY_SIZE(tb_event_names)))
252 		return;
253 	if (WARN_ON_ONCE(type >= ARRAY_SIZE(tb_tunnel_names)))
254 		return;
255 
256 	envp[0] = kasprintf(GFP_KERNEL, "TUNNEL_EVENT=%s", tb_event_names[event]);
257 	if (!envp[0])
258 		return;
259 
260 	if (src_port != NULL && dst_port != NULL) {
261 		envp[1] = kasprintf(GFP_KERNEL, "TUNNEL_DETAILS=%llx:%u <-> %llx:%u (%s)",
262 				    tb_route(src_port->sw), src_port->port,
263 				    tb_route(dst_port->sw), dst_port->port,
264 				    tb_tunnel_names[type]);
265 	} else {
266 		envp[1] = kasprintf(GFP_KERNEL, "TUNNEL_DETAILS=(%s)",
267 				    tb_tunnel_names[type]);
268 	}
269 
270 	if (envp[1])
271 		tb_domain_event(tb, envp);
272 
273 	kfree(envp[1]);
274 	kfree(envp[0]);
275 }
276 
tb_tunnel_set_active(struct tb_tunnel * tunnel,bool active)277 static inline void tb_tunnel_set_active(struct tb_tunnel *tunnel, bool active)
278 {
279 	if (active) {
280 		tunnel->state = TB_TUNNEL_ACTIVE;
281 		tb_tunnel_event(tunnel->tb, TB_TUNNEL_ACTIVATED, tunnel->type,
282 				tunnel->src_port, tunnel->dst_port);
283 	} else {
284 		tunnel->state = TB_TUNNEL_INACTIVE;
285 		tb_tunnel_event(tunnel->tb, TB_TUNNEL_DEACTIVATED, tunnel->type,
286 				tunnel->src_port, tunnel->dst_port);
287 	}
288 }
289 
tb_tunnel_changed(struct tb_tunnel * tunnel)290 static inline void tb_tunnel_changed(struct tb_tunnel *tunnel)
291 {
292 	tb_tunnel_event(tunnel->tb, TB_TUNNEL_CHANGED, tunnel->type,
293 			tunnel->src_port, tunnel->dst_port);
294 }
295 
tb_pci_port_ltssm_state_detect(struct tb_port * port)296 static int tb_pci_port_ltssm_state_detect(struct tb_port *port)
297 {
298 	ktime_t timeout = ktime_add_ms(ktime_get(), 500);
299 
300 	do {
301 		int ret;
302 
303 		ret = usb4_pci_port_ltssm_state(port);
304 		if (ret < 0)
305 			return ret;
306 		if (ret == USB4_PCIE_LTSSM_DETECT)
307 			return 0;
308 
309 		fsleep(50);
310 	} while (ktime_before(ktime_get(), timeout));
311 
312 	return -ETIMEDOUT;
313 }
314 
tb_pci_pre_activate(struct tb_tunnel * tunnel)315 static int tb_pci_pre_activate(struct tb_tunnel *tunnel)
316 {
317 	struct tb_port *down = tunnel->src_port;
318 	struct tb_port *up = tunnel->dst_port;
319 	int ret;
320 
321 	ret = tb_switch_is_usb4(down->sw) ?
322 		tb_pci_port_ltssm_state_detect(down) : 0;
323 	if (ret)
324 		return ret;
325 
326 	return tb_switch_is_usb4(up->sw) ?
327 		tb_pci_port_ltssm_state_detect(up) : 0;
328 }
329 
tb_pci_set_ext_encapsulation(struct tb_tunnel * tunnel,bool enable)330 static int tb_pci_set_ext_encapsulation(struct tb_tunnel *tunnel, bool enable)
331 {
332 	struct tb_port *port = tb_upstream_port(tunnel->dst_port->sw);
333 	int ret;
334 
335 	/* Only supported if both routers are at least USB4 v2 */
336 	if ((usb4_switch_version(tunnel->src_port->sw) < 2) ||
337 	   (usb4_switch_version(tunnel->dst_port->sw) < 2))
338 		return 0;
339 
340 	if (enable && tb_port_get_link_generation(port) < 4)
341 		return 0;
342 
343 	ret = usb4_pci_port_set_ext_encapsulation(tunnel->src_port, enable);
344 	if (ret)
345 		return ret;
346 
347 	/*
348 	 * Downstream router could be unplugged so disable of encapsulation
349 	 * in upstream router is still possible.
350 	 */
351 	ret = usb4_pci_port_set_ext_encapsulation(tunnel->dst_port, enable);
352 	if (ret) {
353 		if (enable)
354 			return ret;
355 		if (ret != -ENODEV)
356 			return ret;
357 	}
358 
359 	tb_tunnel_dbg(tunnel, "extended encapsulation %s\n",
360 		      str_enabled_disabled(enable));
361 	return 0;
362 }
363 
tb_pci_activate(struct tb_tunnel * tunnel,bool activate)364 static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate)
365 {
366 	int res;
367 
368 	if (activate) {
369 		res = tb_pci_set_ext_encapsulation(tunnel, activate);
370 		if (res)
371 			return res;
372 	}
373 
374 	if (activate)
375 		res = tb_pci_port_enable(tunnel->dst_port, activate);
376 	else
377 		res = tb_pci_port_enable(tunnel->src_port, activate);
378 	if (res)
379 		return res;
380 
381 
382 	if (activate) {
383 		res = tb_pci_port_enable(tunnel->src_port, activate);
384 		if (res)
385 			return res;
386 	} else {
387 		/* Downstream router could be unplugged */
388 		tb_pci_port_enable(tunnel->dst_port, activate);
389 	}
390 
391 	return activate ? 0 : tb_pci_set_ext_encapsulation(tunnel, activate);
392 }
393 
tb_pci_init_credits(struct tb_path_hop * hop)394 static int tb_pci_init_credits(struct tb_path_hop *hop)
395 {
396 	struct tb_port *port = hop->in_port;
397 	struct tb_switch *sw = port->sw;
398 	unsigned int credits;
399 
400 	if (tb_port_use_credit_allocation(port)) {
401 		unsigned int available;
402 
403 		available = tb_available_credits(port, NULL);
404 		credits = min(sw->max_pcie_credits, available);
405 
406 		if (credits < TB_MIN_PCIE_CREDITS)
407 			return -ENOSPC;
408 
409 		credits = max(TB_MIN_PCIE_CREDITS, credits);
410 	} else {
411 		if (tb_port_is_null(port))
412 			credits = port->bonded ? 32 : 16;
413 		else
414 			credits = 7;
415 	}
416 
417 	hop->initial_credits = credits;
418 	return 0;
419 }
420 
tb_pci_init_path(struct tb_path * path)421 static int tb_pci_init_path(struct tb_path *path)
422 {
423 	struct tb_path_hop *hop;
424 
425 	path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
426 	path->egress_shared_buffer = TB_PATH_NONE;
427 	path->ingress_fc_enable = TB_PATH_ALL;
428 	path->ingress_shared_buffer = TB_PATH_NONE;
429 	path->priority = TB_PCI_PRIORITY;
430 	path->weight = TB_PCI_WEIGHT;
431 	path->drop_packages = 0;
432 
433 	tb_path_for_each_hop(path, hop) {
434 		int ret;
435 
436 		ret = tb_pci_init_credits(hop);
437 		if (ret)
438 			return ret;
439 	}
440 
441 	return 0;
442 }
443 
444 /**
445  * tb_tunnel_discover_pci() - Discover existing PCIe tunnels
446  * @tb: Pointer to the domain structure
447  * @down: PCIe downstream adapter
448  * @alloc_hopid: Allocate HopIDs from visited ports
449  *
450  * If @down adapter is active, follows the tunnel to the PCIe upstream
451  * adapter and back.
452  *
453  * Return: Pointer to &struct tb_tunnel or %NULL if there was no tunnel.
454  */
tb_tunnel_discover_pci(struct tb * tb,struct tb_port * down,bool alloc_hopid)455 struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,
456 					 bool alloc_hopid)
457 {
458 	struct tb_tunnel *tunnel;
459 	struct tb_path *path;
460 
461 	if (!tb_pci_port_is_enabled(down))
462 		return NULL;
463 
464 	tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
465 	if (!tunnel)
466 		return NULL;
467 
468 	tunnel->activate = tb_pci_activate;
469 	tunnel->src_port = down;
470 
471 	/*
472 	 * Discover both paths even if they are not complete. We will
473 	 * clean them up by calling tb_tunnel_deactivate() below in that
474 	 * case.
475 	 */
476 	path = tb_path_discover(down, TB_PCI_HOPID, NULL, -1,
477 				&tunnel->dst_port, "PCIe Up", alloc_hopid);
478 	if (!path) {
479 		/* Just disable the downstream port */
480 		tb_pci_port_enable(down, false);
481 		goto err_free;
482 	}
483 	tunnel->paths[TB_PCI_PATH_UP] = path;
484 	if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_UP]))
485 		goto err_free;
486 
487 	path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL,
488 				"PCIe Down", alloc_hopid);
489 	if (!path)
490 		goto err_deactivate;
491 	tunnel->paths[TB_PCI_PATH_DOWN] = path;
492 	if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_DOWN]))
493 		goto err_deactivate;
494 
495 	/* Validate that the tunnel is complete */
496 	if (!tb_port_is_pcie_up(tunnel->dst_port)) {
497 		tb_port_warn(tunnel->dst_port,
498 			     "path does not end on a PCIe adapter, cleaning up\n");
499 		goto err_deactivate;
500 	}
501 
502 	if (down != tunnel->src_port) {
503 		tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
504 		goto err_deactivate;
505 	}
506 
507 	if (!tb_pci_port_is_enabled(tunnel->dst_port)) {
508 		tb_tunnel_warn(tunnel,
509 			       "tunnel is not fully activated, cleaning up\n");
510 		goto err_deactivate;
511 	}
512 
513 	tb_tunnel_set_active(tunnel, true);
514 	tb_tunnel_dbg(tunnel, "discovered\n");
515 	return tunnel;
516 
517 err_deactivate:
518 	tb_tunnel_deactivate(tunnel);
519 err_free:
520 	tb_tunnel_put(tunnel);
521 
522 	return NULL;
523 }
524 
525 /**
526  * tb_tunnel_alloc_pci() - allocate a pci tunnel
527  * @tb: Pointer to the domain structure
528  * @up: PCIe upstream adapter port
529  * @down: PCIe downstream adapter port
530  *
531  * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
532  * TB_TYPE_PCIE_DOWN.
533  *
534  * Return: Pointer to @struct tb_tunnel or %NULL on failure.
535  */
tb_tunnel_alloc_pci(struct tb * tb,struct tb_port * up,struct tb_port * down)536 struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up,
537 				      struct tb_port *down)
538 {
539 	struct tb_tunnel *tunnel;
540 	struct tb_path *path;
541 
542 	tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
543 	if (!tunnel)
544 		return NULL;
545 
546 	tunnel->pre_activate = tb_pci_pre_activate;
547 	tunnel->activate = tb_pci_activate;
548 	tunnel->src_port = down;
549 	tunnel->dst_port = up;
550 
551 	path = tb_path_alloc(tb, down, TB_PCI_HOPID, up, TB_PCI_HOPID, 0,
552 			     "PCIe Down");
553 	if (!path)
554 		goto err_free;
555 	tunnel->paths[TB_PCI_PATH_DOWN] = path;
556 	if (tb_pci_init_path(path))
557 		goto err_free;
558 
559 	path = tb_path_alloc(tb, up, TB_PCI_HOPID, down, TB_PCI_HOPID, 0,
560 			     "PCIe Up");
561 	if (!path)
562 		goto err_free;
563 	tunnel->paths[TB_PCI_PATH_UP] = path;
564 	if (tb_pci_init_path(path))
565 		goto err_free;
566 
567 	return tunnel;
568 
569 err_free:
570 	tb_tunnel_put(tunnel);
571 	return NULL;
572 }
573 
574 /**
575  * tb_tunnel_reserved_pci() - Amount of bandwidth to reserve for PCIe
576  * @port: Lane 0 adapter
577  * @reserved_up: Upstream bandwidth in Mb/s to reserve
578  * @reserved_down: Downstream bandwidth in Mb/s to reserve
579  *
580  * Can be called to any connected lane 0 adapter to find out how much
581  * bandwidth needs to be left in reserve for possible PCIe bulk traffic.
582  *
583  * Return:
584  * * %true - If there is something to be reserved. Writes the amount to
585  *   @reserved_down/@reserved_up.
586  * * %false - Nothing to be reserved. Leaves @reserved_down/@reserved_up
587  *   unmodified.
588  */
tb_tunnel_reserved_pci(struct tb_port * port,int * reserved_up,int * reserved_down)589 bool tb_tunnel_reserved_pci(struct tb_port *port, int *reserved_up,
590 			    int *reserved_down)
591 {
592 	if (WARN_ON_ONCE(!port->remote))
593 		return false;
594 
595 	if (!tb_acpi_may_tunnel_pcie())
596 		return false;
597 
598 	if (tb_port_get_link_generation(port) < 4)
599 		return false;
600 
601 	/* Must have PCIe adapters */
602 	if (tb_is_upstream_port(port)) {
603 		if (!tb_switch_find_port(port->sw, TB_TYPE_PCIE_UP))
604 			return false;
605 		if (!tb_switch_find_port(port->remote->sw, TB_TYPE_PCIE_DOWN))
606 			return false;
607 	} else {
608 		if (!tb_switch_find_port(port->sw, TB_TYPE_PCIE_DOWN))
609 			return false;
610 		if (!tb_switch_find_port(port->remote->sw, TB_TYPE_PCIE_UP))
611 			return false;
612 	}
613 
614 	*reserved_up = USB4_V2_PCI_MIN_BANDWIDTH;
615 	*reserved_down = USB4_V2_PCI_MIN_BANDWIDTH;
616 
617 	tb_port_dbg(port, "reserving %u/%u Mb/s for PCIe\n", *reserved_up,
618 		    *reserved_down);
619 	return true;
620 }
621 
tb_dp_is_usb4(const struct tb_switch * sw)622 static bool tb_dp_is_usb4(const struct tb_switch *sw)
623 {
624 	/* Titan Ridge DP adapters need the same treatment as USB4 */
625 	return tb_switch_is_usb4(sw) || tb_switch_is_titan_ridge(sw);
626 }
627 
tb_dp_cm_handshake(struct tb_port * in,struct tb_port * out,int timeout_msec)628 static int tb_dp_cm_handshake(struct tb_port *in, struct tb_port *out,
629 			      int timeout_msec)
630 {
631 	ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
632 	u32 val;
633 	int ret;
634 
635 	/* Both ends need to support this */
636 	if (!tb_dp_is_usb4(in->sw) || !tb_dp_is_usb4(out->sw))
637 		return 0;
638 
639 	ret = tb_port_read(out, &val, TB_CFG_PORT,
640 			   out->cap_adap + DP_STATUS_CTRL, 1);
641 	if (ret)
642 		return ret;
643 
644 	val |= DP_STATUS_CTRL_UF | DP_STATUS_CTRL_CMHS;
645 
646 	ret = tb_port_write(out, &val, TB_CFG_PORT,
647 			    out->cap_adap + DP_STATUS_CTRL, 1);
648 	if (ret)
649 		return ret;
650 
651 	do {
652 		ret = tb_port_read(out, &val, TB_CFG_PORT,
653 				   out->cap_adap + DP_STATUS_CTRL, 1);
654 		if (ret)
655 			return ret;
656 		if (!(val & DP_STATUS_CTRL_CMHS))
657 			return 0;
658 		usleep_range(100, 150);
659 	} while (ktime_before(ktime_get(), timeout));
660 
661 	return -ETIMEDOUT;
662 }
663 
664 /*
665  * Returns maximum possible rate from capability supporting only DP 2.0
666  * and below. Used when DP BW allocation mode is not enabled.
667  */
tb_dp_cap_get_rate(u32 val)668 static inline u32 tb_dp_cap_get_rate(u32 val)
669 {
670 	u32 rate = (val & DP_COMMON_CAP_RATE_MASK) >> DP_COMMON_CAP_RATE_SHIFT;
671 
672 	switch (rate) {
673 	case DP_COMMON_CAP_RATE_RBR:
674 		return 1620;
675 	case DP_COMMON_CAP_RATE_HBR:
676 		return 2700;
677 	case DP_COMMON_CAP_RATE_HBR2:
678 		return 5400;
679 	case DP_COMMON_CAP_RATE_HBR3:
680 		return 8100;
681 	default:
682 		return 0;
683 	}
684 }
685 
686 /*
687  * Returns maximum possible rate from capability supporting DP 2.1
688  * UHBR20, 13.5 and 10 rates as well. Use only when DP BW allocation
689  * mode is enabled.
690  */
tb_dp_cap_get_rate_ext(u32 val)691 static inline u32 tb_dp_cap_get_rate_ext(u32 val)
692 {
693 	if (val & DP_COMMON_CAP_UHBR20)
694 		return 20000;
695 	else if (val & DP_COMMON_CAP_UHBR13_5)
696 		return 13500;
697 	else if (val & DP_COMMON_CAP_UHBR10)
698 		return 10000;
699 
700 	return tb_dp_cap_get_rate(val);
701 }
702 
tb_dp_is_uhbr_rate(unsigned int rate)703 static inline bool tb_dp_is_uhbr_rate(unsigned int rate)
704 {
705 	return rate >= 10000;
706 }
707 
tb_dp_cap_set_rate(u32 val,u32 rate)708 static inline u32 tb_dp_cap_set_rate(u32 val, u32 rate)
709 {
710 	val &= ~DP_COMMON_CAP_RATE_MASK;
711 	switch (rate) {
712 	default:
713 		WARN(1, "invalid rate %u passed, defaulting to 1620 MB/s\n", rate);
714 		fallthrough;
715 	case 1620:
716 		val |= DP_COMMON_CAP_RATE_RBR << DP_COMMON_CAP_RATE_SHIFT;
717 		break;
718 	case 2700:
719 		val |= DP_COMMON_CAP_RATE_HBR << DP_COMMON_CAP_RATE_SHIFT;
720 		break;
721 	case 5400:
722 		val |= DP_COMMON_CAP_RATE_HBR2 << DP_COMMON_CAP_RATE_SHIFT;
723 		break;
724 	case 8100:
725 		val |= DP_COMMON_CAP_RATE_HBR3 << DP_COMMON_CAP_RATE_SHIFT;
726 		break;
727 	}
728 	return val;
729 }
730 
tb_dp_cap_get_lanes(u32 val)731 static inline u32 tb_dp_cap_get_lanes(u32 val)
732 {
733 	u32 lanes = (val & DP_COMMON_CAP_LANES_MASK) >> DP_COMMON_CAP_LANES_SHIFT;
734 
735 	switch (lanes) {
736 	case DP_COMMON_CAP_1_LANE:
737 		return 1;
738 	case DP_COMMON_CAP_2_LANES:
739 		return 2;
740 	case DP_COMMON_CAP_4_LANES:
741 		return 4;
742 	default:
743 		return 0;
744 	}
745 }
746 
tb_dp_cap_set_lanes(u32 val,u32 lanes)747 static inline u32 tb_dp_cap_set_lanes(u32 val, u32 lanes)
748 {
749 	val &= ~DP_COMMON_CAP_LANES_MASK;
750 	switch (lanes) {
751 	default:
752 		WARN(1, "invalid number of lanes %u passed, defaulting to 1\n",
753 		     lanes);
754 		fallthrough;
755 	case 1:
756 		val |= DP_COMMON_CAP_1_LANE << DP_COMMON_CAP_LANES_SHIFT;
757 		break;
758 	case 2:
759 		val |= DP_COMMON_CAP_2_LANES << DP_COMMON_CAP_LANES_SHIFT;
760 		break;
761 	case 4:
762 		val |= DP_COMMON_CAP_4_LANES << DP_COMMON_CAP_LANES_SHIFT;
763 		break;
764 	}
765 	return val;
766 }
767 
tb_dp_bandwidth(unsigned int rate,unsigned int lanes)768 static unsigned int tb_dp_bandwidth(unsigned int rate, unsigned int lanes)
769 {
770 	/* Tunneling removes the DP 8b/10b 128/132b encoding */
771 	if (tb_dp_is_uhbr_rate(rate))
772 		return rate * lanes * 128 / 132;
773 	return rate * lanes * 8 / 10;
774 }
775 
tb_dp_reduce_bandwidth(int max_bw,u32 in_rate,u32 in_lanes,u32 out_rate,u32 out_lanes,u32 * new_rate,u32 * new_lanes)776 static int tb_dp_reduce_bandwidth(int max_bw, u32 in_rate, u32 in_lanes,
777 				  u32 out_rate, u32 out_lanes, u32 *new_rate,
778 				  u32 *new_lanes)
779 {
780 	static const u32 dp_bw[][2] = {
781 		/* Mb/s, lanes */
782 		{ 8100, 4 }, /* 25920 Mb/s */
783 		{ 5400, 4 }, /* 17280 Mb/s */
784 		{ 8100, 2 }, /* 12960 Mb/s */
785 		{ 2700, 4 }, /* 8640 Mb/s */
786 		{ 5400, 2 }, /* 8640 Mb/s */
787 		{ 8100, 1 }, /* 6480 Mb/s */
788 		{ 1620, 4 }, /* 5184 Mb/s */
789 		{ 5400, 1 }, /* 4320 Mb/s */
790 		{ 2700, 2 }, /* 4320 Mb/s */
791 		{ 1620, 2 }, /* 2592 Mb/s */
792 		{ 2700, 1 }, /* 2160 Mb/s */
793 		{ 1620, 1 }, /* 1296 Mb/s */
794 	};
795 	unsigned int i;
796 
797 	/*
798 	 * Find a combination that can fit into max_bw and does not
799 	 * exceed the maximum rate and lanes supported by the DP OUT and
800 	 * DP IN adapters.
801 	 */
802 	for (i = 0; i < ARRAY_SIZE(dp_bw); i++) {
803 		if (dp_bw[i][0] > out_rate || dp_bw[i][1] > out_lanes)
804 			continue;
805 
806 		if (dp_bw[i][0] > in_rate || dp_bw[i][1] > in_lanes)
807 			continue;
808 
809 		if (tb_dp_bandwidth(dp_bw[i][0], dp_bw[i][1]) <= max_bw) {
810 			*new_rate = dp_bw[i][0];
811 			*new_lanes = dp_bw[i][1];
812 			return 0;
813 		}
814 	}
815 
816 	return -ENOSR;
817 }
818 
tb_dp_xchg_caps(struct tb_tunnel * tunnel)819 static int tb_dp_xchg_caps(struct tb_tunnel *tunnel)
820 {
821 	u32 out_dp_cap, out_rate, out_lanes, in_dp_cap, in_rate, in_lanes, bw;
822 	struct tb_port *out = tunnel->dst_port;
823 	struct tb_port *in = tunnel->src_port;
824 	int ret, max_bw;
825 
826 	/*
827 	 * Copy DP_LOCAL_CAP register to DP_REMOTE_CAP register for
828 	 * newer generation hardware.
829 	 */
830 	if (in->sw->generation < 2 || out->sw->generation < 2)
831 		return 0;
832 
833 	/*
834 	 * Perform connection manager handshake between IN and OUT ports
835 	 * before capabilities exchange can take place.
836 	 */
837 	ret = tb_dp_cm_handshake(in, out, 3000);
838 	if (ret)
839 		return ret;
840 
841 	/* Read both DP_LOCAL_CAP registers */
842 	ret = tb_port_read(in, &in_dp_cap, TB_CFG_PORT,
843 			   in->cap_adap + DP_LOCAL_CAP, 1);
844 	if (ret)
845 		return ret;
846 
847 	ret = tb_port_read(out, &out_dp_cap, TB_CFG_PORT,
848 			   out->cap_adap + DP_LOCAL_CAP, 1);
849 	if (ret)
850 		return ret;
851 
852 	/* Write IN local caps to OUT remote caps */
853 	ret = tb_port_write(out, &in_dp_cap, TB_CFG_PORT,
854 			    out->cap_adap + DP_REMOTE_CAP, 1);
855 	if (ret)
856 		return ret;
857 
858 	in_rate = tb_dp_cap_get_rate(in_dp_cap);
859 	in_lanes = tb_dp_cap_get_lanes(in_dp_cap);
860 	tb_tunnel_dbg(tunnel,
861 		      "DP IN maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
862 		      in_rate, in_lanes, tb_dp_bandwidth(in_rate, in_lanes));
863 
864 	/*
865 	 * If the tunnel bandwidth is limited (max_bw is set) then see
866 	 * if we need to reduce bandwidth to fit there.
867 	 */
868 	out_rate = tb_dp_cap_get_rate(out_dp_cap);
869 	out_lanes = tb_dp_cap_get_lanes(out_dp_cap);
870 	bw = tb_dp_bandwidth(out_rate, out_lanes);
871 	tb_tunnel_dbg(tunnel,
872 		      "DP OUT maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
873 		      out_rate, out_lanes, bw);
874 
875 	if (tb_tunnel_direction_downstream(tunnel))
876 		max_bw = tunnel->max_down;
877 	else
878 		max_bw = tunnel->max_up;
879 
880 	if (max_bw && bw > max_bw) {
881 		u32 new_rate, new_lanes, new_bw;
882 
883 		ret = tb_dp_reduce_bandwidth(max_bw, in_rate, in_lanes,
884 					     out_rate, out_lanes, &new_rate,
885 					     &new_lanes);
886 		if (ret) {
887 			tb_tunnel_info(tunnel, "not enough bandwidth\n");
888 			return ret;
889 		}
890 
891 		new_bw = tb_dp_bandwidth(new_rate, new_lanes);
892 		tb_tunnel_dbg(tunnel,
893 			      "bandwidth reduced to %u Mb/s x%u = %u Mb/s\n",
894 			      new_rate, new_lanes, new_bw);
895 
896 		/*
897 		 * Set new rate and number of lanes before writing it to
898 		 * the IN port remote caps.
899 		 */
900 		out_dp_cap = tb_dp_cap_set_rate(out_dp_cap, new_rate);
901 		out_dp_cap = tb_dp_cap_set_lanes(out_dp_cap, new_lanes);
902 	}
903 
904 	/*
905 	 * Titan Ridge does not disable AUX timers when it gets
906 	 * SET_CONFIG with SET_LTTPR_MODE set. This causes problems with
907 	 * DP tunneling.
908 	 */
909 	if (tb_route(out->sw) && tb_switch_is_titan_ridge(out->sw)) {
910 		out_dp_cap |= DP_COMMON_CAP_LTTPR_NS;
911 		tb_tunnel_dbg(tunnel, "disabling LTTPR\n");
912 	}
913 
914 	return tb_port_write(in, &out_dp_cap, TB_CFG_PORT,
915 			     in->cap_adap + DP_REMOTE_CAP, 1);
916 }
917 
tb_dp_bandwidth_alloc_mode_enable(struct tb_tunnel * tunnel)918 static int tb_dp_bandwidth_alloc_mode_enable(struct tb_tunnel *tunnel)
919 {
920 	int ret, estimated_bw, granularity, tmp;
921 	struct tb_port *out = tunnel->dst_port;
922 	struct tb_port *in = tunnel->src_port;
923 	u32 out_dp_cap, out_rate, out_lanes;
924 	u32 in_dp_cap, in_rate, in_lanes;
925 	u32 rate, lanes;
926 
927 	if (!bw_alloc_mode)
928 		return 0;
929 
930 	ret = usb4_dp_port_set_cm_bandwidth_mode_supported(in, true);
931 	if (ret)
932 		return ret;
933 
934 	ret = usb4_dp_port_set_group_id(in, in->group->index);
935 	if (ret)
936 		return ret;
937 
938 	/*
939 	 * Get the non-reduced rate and lanes based on the lowest
940 	 * capability of both adapters.
941 	 */
942 	ret = tb_port_read(in, &in_dp_cap, TB_CFG_PORT,
943 			   in->cap_adap + DP_LOCAL_CAP, 1);
944 	if (ret)
945 		return ret;
946 
947 	ret = tb_port_read(out, &out_dp_cap, TB_CFG_PORT,
948 			   out->cap_adap + DP_LOCAL_CAP, 1);
949 	if (ret)
950 		return ret;
951 
952 	in_rate = tb_dp_cap_get_rate(in_dp_cap);
953 	in_lanes = tb_dp_cap_get_lanes(in_dp_cap);
954 	out_rate = tb_dp_cap_get_rate(out_dp_cap);
955 	out_lanes = tb_dp_cap_get_lanes(out_dp_cap);
956 
957 	rate = min(in_rate, out_rate);
958 	lanes = min(in_lanes, out_lanes);
959 	tmp = tb_dp_bandwidth(rate, lanes);
960 
961 	tb_tunnel_dbg(tunnel, "non-reduced bandwidth %u Mb/s x%u = %u Mb/s\n",
962 		      rate, lanes, tmp);
963 
964 	ret = usb4_dp_port_set_nrd(in, rate, lanes);
965 	if (ret)
966 		return ret;
967 
968 	/*
969 	 * Pick up granularity that supports maximum possible bandwidth.
970 	 * For that we use the UHBR rates too.
971 	 */
972 	in_rate = tb_dp_cap_get_rate_ext(in_dp_cap);
973 	out_rate = tb_dp_cap_get_rate_ext(out_dp_cap);
974 	rate = min(in_rate, out_rate);
975 	tmp = tb_dp_bandwidth(rate, lanes);
976 
977 	tb_tunnel_dbg(tunnel,
978 		      "maximum bandwidth through allocation mode %u Mb/s x%u = %u Mb/s\n",
979 		      rate, lanes, tmp);
980 
981 	for (granularity = 250; tmp / granularity > 255 && granularity <= 1000;
982 	     granularity *= 2)
983 		;
984 
985 	tb_tunnel_dbg(tunnel, "granularity %d Mb/s\n", granularity);
986 
987 	/*
988 	 * Returns -EINVAL if granularity above is outside of the
989 	 * accepted ranges.
990 	 */
991 	ret = usb4_dp_port_set_granularity(in, granularity);
992 	if (ret)
993 		return ret;
994 
995 	/*
996 	 * Bandwidth estimation is pretty much what we have in
997 	 * max_up/down fields. For discovery we just read what the
998 	 * estimation was set to.
999 	 */
1000 	if (tb_tunnel_direction_downstream(tunnel))
1001 		estimated_bw = tunnel->max_down;
1002 	else
1003 		estimated_bw = tunnel->max_up;
1004 
1005 	tb_tunnel_dbg(tunnel, "estimated bandwidth %d Mb/s\n", estimated_bw);
1006 
1007 	ret = usb4_dp_port_set_estimated_bandwidth(in, estimated_bw);
1008 	if (ret)
1009 		return ret;
1010 
1011 	/* Initial allocation should be 0 according the spec */
1012 	ret = usb4_dp_port_allocate_bandwidth(in, 0);
1013 	if (ret)
1014 		return ret;
1015 
1016 	tb_tunnel_dbg(tunnel, "bandwidth allocation mode enabled\n");
1017 	return 0;
1018 }
1019 
tb_dp_pre_activate(struct tb_tunnel * tunnel)1020 static int tb_dp_pre_activate(struct tb_tunnel *tunnel)
1021 {
1022 	struct tb_port *in = tunnel->src_port;
1023 	struct tb_switch *sw = in->sw;
1024 	struct tb *tb = in->sw->tb;
1025 	int ret;
1026 
1027 	ret = tb_dp_xchg_caps(tunnel);
1028 	if (ret)
1029 		return ret;
1030 
1031 	if (!tb_switch_is_usb4(sw))
1032 		return 0;
1033 
1034 	if (!usb4_dp_port_bandwidth_mode_supported(in))
1035 		return 0;
1036 
1037 	tb_tunnel_dbg(tunnel, "bandwidth allocation mode supported\n");
1038 
1039 	ret = usb4_dp_port_set_cm_id(in, tb->index);
1040 	if (ret)
1041 		return ret;
1042 
1043 	return tb_dp_bandwidth_alloc_mode_enable(tunnel);
1044 }
1045 
tb_dp_post_deactivate(struct tb_tunnel * tunnel)1046 static void tb_dp_post_deactivate(struct tb_tunnel *tunnel)
1047 {
1048 	struct tb_port *in = tunnel->src_port;
1049 
1050 	if (!usb4_dp_port_bandwidth_mode_supported(in))
1051 		return;
1052 	if (usb4_dp_port_bandwidth_mode_enabled(in)) {
1053 		usb4_dp_port_set_cm_bandwidth_mode_supported(in, false);
1054 		tb_tunnel_dbg(tunnel, "bandwidth allocation mode disabled\n");
1055 	}
1056 }
1057 
dprx_timeout_to_ktime(int timeout_msec)1058 static ktime_t dprx_timeout_to_ktime(int timeout_msec)
1059 {
1060 	return timeout_msec >= 0 ?
1061 		ktime_add_ms(ktime_get(), timeout_msec) : KTIME_MAX;
1062 }
1063 
tb_dp_wait_dprx(struct tb_tunnel * tunnel,int timeout_msec)1064 static int tb_dp_wait_dprx(struct tb_tunnel *tunnel, int timeout_msec)
1065 {
1066 	ktime_t timeout = dprx_timeout_to_ktime(timeout_msec);
1067 	struct tb_port *in = tunnel->src_port;
1068 
1069 	/*
1070 	 * Wait for DPRX done. Normally it should be already set for
1071 	 * active tunnel.
1072 	 */
1073 	do {
1074 		u32 val;
1075 		int ret;
1076 
1077 		ret = tb_port_read(in, &val, TB_CFG_PORT,
1078 				   in->cap_adap + DP_COMMON_CAP, 1);
1079 		if (ret)
1080 			return ret;
1081 
1082 		if (val & DP_COMMON_CAP_DPRX_DONE)
1083 			return 0;
1084 
1085 		usleep_range(100, 150);
1086 	} while (ktime_before(ktime_get(), timeout));
1087 
1088 	tb_tunnel_dbg(tunnel, "DPRX read timeout\n");
1089 	return -ETIMEDOUT;
1090 }
1091 
tb_dp_dprx_work(struct work_struct * work)1092 static void tb_dp_dprx_work(struct work_struct *work)
1093 {
1094 	struct tb_tunnel *tunnel = container_of(work, typeof(*tunnel), dprx_work.work);
1095 	struct tb *tb = tunnel->tb;
1096 
1097 	/*
1098 	 * The DPRX read can be canceled while this work is waiting for
1099 	 * tb->lock. Check the flag only once it is held: while the lock is
1100 	 * held the tunnel cannot be torn down under us and the adapters are
1101 	 * safe to access.
1102 	 */
1103 	mutex_lock(&tb->lock);
1104 	if (!tunnel->dprx_canceled) {
1105 		if (tb_dp_is_usb4(tunnel->src_port->sw) &&
1106 		    tb_dp_wait_dprx(tunnel, TB_DPRX_WAIT_TIMEOUT)) {
1107 			if (ktime_before(ktime_get(), tunnel->dprx_timeout)) {
1108 				queue_delayed_work(tb->wq, &tunnel->dprx_work,
1109 						   msecs_to_jiffies(TB_DPRX_POLL_DELAY));
1110 				mutex_unlock(&tb->lock);
1111 				return;
1112 			}
1113 		} else {
1114 			tb_tunnel_set_active(tunnel, true);
1115 		}
1116 	}
1117 	mutex_unlock(&tb->lock);
1118 
1119 	tunnel->callback(tunnel, tunnel->callback_data);
1120 	tb_tunnel_put(tunnel);
1121 	tb_domain_put(tb);
1122 }
1123 
tb_dp_dprx_start(struct tb_tunnel * tunnel)1124 static int tb_dp_dprx_start(struct tb_tunnel *tunnel)
1125 {
1126 	/*
1127 	 * Bump up the references to keep the tunnel and the domain around
1128 	 * until the work has run or has been canceled.
1129 	 */
1130 	tb_tunnel_get(tunnel);
1131 	tb_domain_get(tunnel->tb);
1132 
1133 	tunnel->dprx_started = true;
1134 	tunnel->dprx_canceled = false;
1135 	tunnel->dprx_timeout = dprx_timeout_to_ktime(dprx_timeout);
1136 	queue_delayed_work(tunnel->tb->wq, &tunnel->dprx_work, 0);
1137 
1138 	return -EINPROGRESS;
1139 }
1140 
tb_dp_dprx_stop(struct tb_tunnel * tunnel)1141 static void tb_dp_dprx_stop(struct tb_tunnel *tunnel)
1142 {
1143 	struct tb *tb = tunnel->tb;
1144 
1145 	if (tunnel->dprx_started) {
1146 		tunnel->dprx_started = false;
1147 		tunnel->dprx_canceled = true;
1148 		if (cancel_delayed_work(&tunnel->dprx_work)) {
1149 			tb_tunnel_put(tunnel);
1150 			tb_domain_put(tb);
1151 		}
1152 	}
1153 }
1154 
tb_dp_activate(struct tb_tunnel * tunnel,bool active)1155 static int tb_dp_activate(struct tb_tunnel *tunnel, bool active)
1156 {
1157 	int ret;
1158 
1159 	if (active) {
1160 		struct tb_path **paths;
1161 		int last;
1162 
1163 		paths = tunnel->paths;
1164 		last = paths[TB_DP_VIDEO_PATH_OUT]->path_length - 1;
1165 
1166 		tb_dp_port_set_hops(tunnel->src_port,
1167 			paths[TB_DP_VIDEO_PATH_OUT]->hops[0].in_hop_index,
1168 			paths[TB_DP_AUX_PATH_OUT]->hops[0].in_hop_index,
1169 			paths[TB_DP_AUX_PATH_IN]->hops[last].next_hop_index);
1170 
1171 		tb_dp_port_set_hops(tunnel->dst_port,
1172 			paths[TB_DP_VIDEO_PATH_OUT]->hops[last].next_hop_index,
1173 			paths[TB_DP_AUX_PATH_IN]->hops[0].in_hop_index,
1174 			paths[TB_DP_AUX_PATH_OUT]->hops[last].next_hop_index);
1175 	} else {
1176 		tb_dp_dprx_stop(tunnel);
1177 		tb_dp_port_hpd_clear(tunnel->src_port);
1178 		tb_dp_port_set_hops(tunnel->src_port, 0, 0, 0);
1179 		if (tb_port_is_dpout(tunnel->dst_port))
1180 			tb_dp_port_set_hops(tunnel->dst_port, 0, 0, 0);
1181 	}
1182 
1183 	ret = tb_dp_port_enable(tunnel->src_port, active);
1184 	if (ret)
1185 		return ret;
1186 
1187 	if (tb_port_is_dpout(tunnel->dst_port)) {
1188 		ret = tb_dp_port_enable(tunnel->dst_port, active);
1189 		if (ret)
1190 			return ret;
1191 	}
1192 
1193 	return active ? tb_dp_dprx_start(tunnel) : 0;
1194 }
1195 
1196 /**
1197  * tb_dp_bandwidth_mode_maximum_bandwidth() - Maximum possible bandwidth
1198  * @tunnel: DP tunnel to check
1199  * @max_bw_rounded: Maximum bandwidth in Mb/s rounded up to the next granularity
1200  *
1201  * Return: Maximum possible bandwidth for this tunnel in Mb/s, negative errno
1202  * in case of failure.
1203  */
tb_dp_bandwidth_mode_maximum_bandwidth(struct tb_tunnel * tunnel,int * max_bw_rounded)1204 static int tb_dp_bandwidth_mode_maximum_bandwidth(struct tb_tunnel *tunnel,
1205 						  int *max_bw_rounded)
1206 {
1207 	struct tb_port *in = tunnel->src_port;
1208 	int ret, rate, lanes, max_bw;
1209 	u32 cap;
1210 
1211 	/*
1212 	 * DP IN adapter DP_LOCAL_CAP gets updated to the lowest AUX
1213 	 * read parameter values so we can use this to determine the
1214 	 * maximum possible bandwidth over this link.
1215 	 *
1216 	 * See USB4 v2 spec 1.0 10.4.4.5.
1217 	 */
1218 	ret = tb_port_read(in, &cap, TB_CFG_PORT,
1219 			   in->cap_adap + DP_LOCAL_CAP, 1);
1220 	if (ret)
1221 		return ret;
1222 
1223 	rate = tb_dp_cap_get_rate_ext(cap);
1224 	lanes = tb_dp_cap_get_lanes(cap);
1225 
1226 	max_bw = tb_dp_bandwidth(rate, lanes);
1227 
1228 	if (max_bw_rounded) {
1229 		ret = usb4_dp_port_granularity(in);
1230 		if (ret < 0)
1231 			return ret;
1232 		*max_bw_rounded = roundup(max_bw, ret);
1233 	}
1234 
1235 	return max_bw;
1236 }
1237 
tb_dp_bandwidth_mode_consumed_bandwidth(struct tb_tunnel * tunnel,int * consumed_up,int * consumed_down)1238 static int tb_dp_bandwidth_mode_consumed_bandwidth(struct tb_tunnel *tunnel,
1239 						   int *consumed_up,
1240 						   int *consumed_down)
1241 {
1242 	struct tb_port *in = tunnel->src_port;
1243 	int ret, allocated_bw, max_bw_rounded;
1244 
1245 	if (!usb4_dp_port_bandwidth_mode_enabled(in))
1246 		return -EOPNOTSUPP;
1247 
1248 	if (!tunnel->bw_mode)
1249 		return -EOPNOTSUPP;
1250 
1251 	/* Read what was allocated previously if any */
1252 	ret = usb4_dp_port_allocated_bandwidth(in);
1253 	if (ret < 0)
1254 		return ret;
1255 	allocated_bw = ret;
1256 
1257 	ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw_rounded);
1258 	if (ret < 0)
1259 		return ret;
1260 	if (allocated_bw == max_bw_rounded)
1261 		allocated_bw = ret;
1262 
1263 	if (tb_tunnel_direction_downstream(tunnel)) {
1264 		*consumed_up = 0;
1265 		*consumed_down = allocated_bw;
1266 	} else {
1267 		*consumed_up = allocated_bw;
1268 		*consumed_down = 0;
1269 	}
1270 
1271 	return 0;
1272 }
1273 
tb_dp_allocated_bandwidth(struct tb_tunnel * tunnel,int * allocated_up,int * allocated_down)1274 static int tb_dp_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up,
1275 				     int *allocated_down)
1276 {
1277 	struct tb_port *in = tunnel->src_port;
1278 
1279 	/*
1280 	 * If we have already set the allocated bandwidth then use that.
1281 	 * Otherwise we read it from the DPRX.
1282 	 */
1283 	if (usb4_dp_port_bandwidth_mode_enabled(in) && tunnel->bw_mode) {
1284 		int ret, allocated_bw, max_bw_rounded;
1285 
1286 		ret = usb4_dp_port_allocated_bandwidth(in);
1287 		if (ret < 0)
1288 			return ret;
1289 		allocated_bw = ret;
1290 
1291 		ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel,
1292 							     &max_bw_rounded);
1293 		if (ret < 0)
1294 			return ret;
1295 		if (allocated_bw == max_bw_rounded)
1296 			allocated_bw = ret;
1297 
1298 		if (tb_tunnel_direction_downstream(tunnel)) {
1299 			*allocated_up = 0;
1300 			*allocated_down = allocated_bw;
1301 		} else {
1302 			*allocated_up = allocated_bw;
1303 			*allocated_down = 0;
1304 		}
1305 		return 0;
1306 	}
1307 
1308 	return tunnel->consumed_bandwidth(tunnel, allocated_up,
1309 					  allocated_down);
1310 }
1311 
tb_dp_alloc_bandwidth(struct tb_tunnel * tunnel,int * alloc_up,int * alloc_down)1312 static int tb_dp_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up,
1313 				 int *alloc_down)
1314 {
1315 	struct tb_port *in = tunnel->src_port;
1316 	int max_bw_rounded, ret, tmp;
1317 
1318 	if (!usb4_dp_port_bandwidth_mode_enabled(in))
1319 		return -EOPNOTSUPP;
1320 
1321 	ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw_rounded);
1322 	if (ret < 0)
1323 		return ret;
1324 
1325 	if (tb_tunnel_direction_downstream(tunnel)) {
1326 		tmp = min(*alloc_down, max_bw_rounded);
1327 		ret = usb4_dp_port_allocate_bandwidth(in, tmp);
1328 		if (ret)
1329 			return ret;
1330 		*alloc_down = tmp;
1331 		*alloc_up = 0;
1332 	} else {
1333 		tmp = min(*alloc_up, max_bw_rounded);
1334 		ret = usb4_dp_port_allocate_bandwidth(in, tmp);
1335 		if (ret)
1336 			return ret;
1337 		*alloc_down = 0;
1338 		*alloc_up = tmp;
1339 	}
1340 
1341 	/* Now we can use BW mode registers to figure out the bandwidth */
1342 	/* TODO: need to handle discovery too */
1343 	tunnel->bw_mode = true;
1344 	return 0;
1345 }
1346 
1347 /* Read cap from tunnel DP IN */
tb_dp_read_cap(struct tb_tunnel * tunnel,unsigned int cap,u32 * rate,u32 * lanes)1348 static int tb_dp_read_cap(struct tb_tunnel *tunnel, unsigned int cap, u32 *rate,
1349 			  u32 *lanes)
1350 {
1351 	struct tb_port *in = tunnel->src_port;
1352 	u32 val;
1353 	int ret;
1354 
1355 	switch (cap) {
1356 	case DP_LOCAL_CAP:
1357 	case DP_REMOTE_CAP:
1358 	case DP_COMMON_CAP:
1359 		break;
1360 
1361 	default:
1362 		tb_tunnel_WARN(tunnel, "invalid capability index %#x\n", cap);
1363 		return -EINVAL;
1364 	}
1365 
1366 	/*
1367 	 * Read from the copied remote cap so that we take into account
1368 	 * if capabilities were reduced during exchange.
1369 	 */
1370 	ret = tb_port_read(in, &val, TB_CFG_PORT, in->cap_adap + cap, 1);
1371 	if (ret)
1372 		return ret;
1373 
1374 	*rate = tb_dp_cap_get_rate(val);
1375 	*lanes = tb_dp_cap_get_lanes(val);
1376 	return 0;
1377 }
1378 
tb_dp_maximum_bandwidth(struct tb_tunnel * tunnel,int * max_up,int * max_down)1379 static int tb_dp_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up,
1380 				   int *max_down)
1381 {
1382 	int ret;
1383 
1384 	if (!usb4_dp_port_bandwidth_mode_enabled(tunnel->src_port))
1385 		return -EOPNOTSUPP;
1386 
1387 	ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, NULL);
1388 	if (ret < 0)
1389 		return ret;
1390 
1391 	if (tb_tunnel_direction_downstream(tunnel)) {
1392 		*max_up = 0;
1393 		*max_down = ret;
1394 	} else {
1395 		*max_up = ret;
1396 		*max_down = 0;
1397 	}
1398 
1399 	return 0;
1400 }
1401 
tb_dp_consumed_bandwidth(struct tb_tunnel * tunnel,int * consumed_up,int * consumed_down)1402 static int tb_dp_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
1403 				    int *consumed_down)
1404 {
1405 	const struct tb_switch *sw = tunnel->src_port->sw;
1406 	u32 rate = 0, lanes = 0;
1407 	int ret;
1408 
1409 	if (tb_dp_is_usb4(sw)) {
1410 		ret = tb_dp_wait_dprx(tunnel, 0);
1411 		if (ret) {
1412 			if (ret == -ETIMEDOUT) {
1413 				/*
1414 				 * While we wait for DPRX complete the
1415 				 * tunnel consumes as much as it had
1416 				 * been reserved initially.
1417 				 */
1418 				ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP,
1419 						     &rate, &lanes);
1420 				if (ret)
1421 					return ret;
1422 			} else {
1423 				return ret;
1424 			}
1425 		} else {
1426 			/*
1427 			 * On USB4 routers check if the bandwidth allocation
1428 			 * mode is enabled first and then read the bandwidth
1429 			 * through those registers.
1430 			 */
1431 			ret = tb_dp_bandwidth_mode_consumed_bandwidth(tunnel, consumed_up,
1432 								      consumed_down);
1433 			if (ret < 0) {
1434 				if (ret != -EOPNOTSUPP)
1435 					return ret;
1436 			} else if (!ret) {
1437 				return 0;
1438 			}
1439 			ret = tb_dp_read_cap(tunnel, DP_COMMON_CAP, &rate, &lanes);
1440 			if (ret)
1441 				return ret;
1442 		}
1443 	} else if (sw->generation >= 2) {
1444 		ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP, &rate, &lanes);
1445 		if (ret)
1446 			return ret;
1447 	} else {
1448 		/* No bandwidth management for legacy devices  */
1449 		*consumed_up = 0;
1450 		*consumed_down = 0;
1451 		return 0;
1452 	}
1453 
1454 	if (tb_tunnel_direction_downstream(tunnel)) {
1455 		*consumed_up = 0;
1456 		*consumed_down = tb_dp_bandwidth(rate, lanes);
1457 	} else {
1458 		*consumed_up = tb_dp_bandwidth(rate, lanes);
1459 		*consumed_down = 0;
1460 	}
1461 
1462 	return 0;
1463 }
1464 
tb_dp_init_aux_credits(struct tb_path_hop * hop)1465 static void tb_dp_init_aux_credits(struct tb_path_hop *hop)
1466 {
1467 	struct tb_port *port = hop->in_port;
1468 	struct tb_switch *sw = port->sw;
1469 
1470 	if (tb_port_use_credit_allocation(port))
1471 		hop->initial_credits = sw->min_dp_aux_credits;
1472 	else
1473 		hop->initial_credits = 1;
1474 }
1475 
tb_dp_init_aux_path(struct tb_path * path,bool pm_support)1476 static void tb_dp_init_aux_path(struct tb_path *path, bool pm_support)
1477 {
1478 	struct tb_path_hop *hop;
1479 
1480 	path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
1481 	path->egress_shared_buffer = TB_PATH_NONE;
1482 	path->ingress_fc_enable = TB_PATH_ALL;
1483 	path->ingress_shared_buffer = TB_PATH_NONE;
1484 	path->priority = TB_DP_AUX_PRIORITY;
1485 	path->weight = TB_DP_AUX_WEIGHT;
1486 
1487 	tb_path_for_each_hop(path, hop) {
1488 		tb_dp_init_aux_credits(hop);
1489 		if (pm_support)
1490 			tb_init_pm_support(hop);
1491 	}
1492 }
1493 
tb_dp_init_video_credits(struct tb_path_hop * hop)1494 static int tb_dp_init_video_credits(struct tb_path_hop *hop)
1495 {
1496 	struct tb_port *port = hop->in_port;
1497 	struct tb_switch *sw = port->sw;
1498 
1499 	if (tb_port_use_credit_allocation(port)) {
1500 		unsigned int nfc_credits;
1501 		size_t max_dp_streams;
1502 
1503 		tb_available_credits(port, &max_dp_streams);
1504 		/*
1505 		 * Read the number of currently allocated NFC credits
1506 		 * from the lane adapter. Since we only use them for DP
1507 		 * tunneling we can use that to figure out how many DP
1508 		 * tunnels already go through the lane adapter.
1509 		 */
1510 		nfc_credits = port->config.nfc_credits &
1511 				ADP_CS_4_NFC_BUFFERS_MASK;
1512 		if (nfc_credits / sw->min_dp_main_credits > max_dp_streams)
1513 			return -ENOSPC;
1514 
1515 		hop->nfc_credits = sw->min_dp_main_credits;
1516 	} else {
1517 		hop->nfc_credits = min(port->total_credits - 2, 12U);
1518 	}
1519 
1520 	return 0;
1521 }
1522 
tb_dp_init_video_path(struct tb_path * path,bool pm_support)1523 static int tb_dp_init_video_path(struct tb_path *path, bool pm_support)
1524 {
1525 	struct tb_path_hop *hop;
1526 
1527 	path->egress_fc_enable = TB_PATH_NONE;
1528 	path->egress_shared_buffer = TB_PATH_NONE;
1529 	path->ingress_fc_enable = TB_PATH_NONE;
1530 	path->ingress_shared_buffer = TB_PATH_NONE;
1531 	path->priority = TB_DP_VIDEO_PRIORITY;
1532 	path->weight = TB_DP_VIDEO_WEIGHT;
1533 
1534 	tb_path_for_each_hop(path, hop) {
1535 		int ret;
1536 
1537 		ret = tb_dp_init_video_credits(hop);
1538 		if (ret)
1539 			return ret;
1540 		if (pm_support)
1541 			tb_init_pm_support(hop);
1542 	}
1543 
1544 	return 0;
1545 }
1546 
tb_dp_dump(struct tb_tunnel * tunnel)1547 static void tb_dp_dump(struct tb_tunnel *tunnel)
1548 {
1549 	struct tb_port *in, *out;
1550 	u32 dp_cap, rate, lanes;
1551 
1552 	in = tunnel->src_port;
1553 	out = tunnel->dst_port;
1554 
1555 	if (tb_port_read(in, &dp_cap, TB_CFG_PORT,
1556 			 in->cap_adap + DP_LOCAL_CAP, 1))
1557 		return;
1558 
1559 	rate = tb_dp_cap_get_rate(dp_cap);
1560 	lanes = tb_dp_cap_get_lanes(dp_cap);
1561 
1562 	tb_tunnel_dbg(tunnel,
1563 		      "DP IN maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
1564 		      rate, lanes, tb_dp_bandwidth(rate, lanes));
1565 
1566 	if (tb_port_read(out, &dp_cap, TB_CFG_PORT,
1567 			 out->cap_adap + DP_LOCAL_CAP, 1))
1568 		return;
1569 
1570 	rate = tb_dp_cap_get_rate(dp_cap);
1571 	lanes = tb_dp_cap_get_lanes(dp_cap);
1572 
1573 	tb_tunnel_dbg(tunnel,
1574 		      "DP OUT maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
1575 		      rate, lanes, tb_dp_bandwidth(rate, lanes));
1576 
1577 	if (tb_port_read(in, &dp_cap, TB_CFG_PORT,
1578 			 in->cap_adap + DP_REMOTE_CAP, 1))
1579 		return;
1580 
1581 	rate = tb_dp_cap_get_rate(dp_cap);
1582 	lanes = tb_dp_cap_get_lanes(dp_cap);
1583 
1584 	tb_tunnel_dbg(tunnel, "reduced bandwidth %u Mb/s x%u = %u Mb/s\n",
1585 		      rate, lanes, tb_dp_bandwidth(rate, lanes));
1586 }
1587 
1588 /**
1589  * tb_tunnel_discover_dp() - Discover existing Display Port tunnels
1590  * @tb: Pointer to the domain structure
1591  * @in: DP in adapter
1592  * @alloc_hopid: Allocate HopIDs from visited ports
1593  * @callback: Callback that is called when the DP tunnel is fully
1594  *	      activated (or there is an error)
1595  * @callback_data: Data for @callback
1596  *
1597  * If @in adapter is active, follows the tunnel to the DP out adapter
1598  * and back. Returns the discovered tunnel or %NULL if there was no
1599  * tunnel. See tb_tunnel_alloc_dp() for @callback.
1600  *
1601  * Return: Pointer to &struct tb_tunnel or %NULL if no tunnel found.
1602  */
tb_tunnel_discover_dp(struct tb * tb,struct tb_port * in,bool alloc_hopid,void (* callback)(struct tb_tunnel *,void *),void * callback_data)1603 struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
1604 					bool alloc_hopid,
1605 					void (*callback)(struct tb_tunnel *, void *),
1606 					void *callback_data)
1607 {
1608 	struct tb_tunnel *tunnel;
1609 	struct tb_port *port;
1610 	struct tb_path *path;
1611 
1612 	if (WARN_ON(!callback))
1613 		return NULL;
1614 
1615 	if (!tb_dp_port_is_enabled(in))
1616 		return NULL;
1617 
1618 	tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
1619 	if (!tunnel)
1620 		return NULL;
1621 
1622 	tunnel->pre_activate = tb_dp_pre_activate;
1623 	tunnel->activate = tb_dp_activate;
1624 	tunnel->post_deactivate = tb_dp_post_deactivate;
1625 	tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth;
1626 	tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth;
1627 	tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth;
1628 	tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
1629 	tunnel->src_port = in;
1630 	tunnel->callback = callback;
1631 	tunnel->callback_data = callback_data;
1632 	INIT_DELAYED_WORK(&tunnel->dprx_work, tb_dp_dprx_work);
1633 
1634 	path = tb_path_discover(in, TB_DP_VIDEO_HOPID, NULL, -1,
1635 				&tunnel->dst_port, "Video", alloc_hopid);
1636 	if (!path) {
1637 		/* Just disable the DP IN port */
1638 		tb_dp_port_enable(in, false);
1639 		goto err_free;
1640 	}
1641 	tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path;
1642 	if (tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT], false))
1643 		goto err_free;
1644 
1645 	path = tb_path_discover(in, TB_DP_AUX_TX_HOPID, NULL, -1, NULL, "AUX TX",
1646 				alloc_hopid);
1647 	if (!path)
1648 		goto err_deactivate;
1649 	tunnel->paths[TB_DP_AUX_PATH_OUT] = path;
1650 	tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT], false);
1651 
1652 	path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID,
1653 				&port, "AUX RX", alloc_hopid);
1654 	if (!path)
1655 		goto err_deactivate;
1656 	tunnel->paths[TB_DP_AUX_PATH_IN] = path;
1657 	tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN], false);
1658 
1659 	/* Validate that the tunnel is complete */
1660 	if (!tb_port_is_dpout(tunnel->dst_port)) {
1661 		tb_port_warn(in, "path does not end on a DP adapter, cleaning up\n");
1662 		goto err_deactivate;
1663 	}
1664 
1665 	if (!tb_dp_port_is_enabled(tunnel->dst_port))
1666 		goto err_deactivate;
1667 
1668 	if (!tb_dp_port_hpd_is_active(tunnel->dst_port))
1669 		goto err_deactivate;
1670 
1671 	if (port != tunnel->src_port) {
1672 		tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
1673 		goto err_deactivate;
1674 	}
1675 
1676 	tb_dp_dump(tunnel);
1677 
1678 	tb_tunnel_set_active(tunnel, true);
1679 	tb_tunnel_dbg(tunnel, "discovered\n");
1680 	return tunnel;
1681 
1682 err_deactivate:
1683 	tb_tunnel_deactivate(tunnel);
1684 err_free:
1685 	tb_tunnel_put(tunnel);
1686 
1687 	return NULL;
1688 }
1689 
1690 /**
1691  * tb_tunnel_alloc_dp() - allocate a Display Port tunnel
1692  * @tb: Pointer to the domain structure
1693  * @in: DP in adapter port
1694  * @out: DP out adapter port
1695  * @link_nr: Preferred lane adapter when the link is not bonded
1696  * @max_up: Maximum available upstream bandwidth for the DP tunnel.
1697  *	    %0 if no available bandwidth.
1698  * @max_down: Maximum available downstream bandwidth for the DP tunnel.
1699  *	      %0 if no available bandwidth.
1700  * @callback: Callback that is called when the DP tunnel is fully
1701  *	      activated (or there is an error)
1702  * @callback_data: Data for @callback
1703  *
1704  * Allocates a tunnel between @in and @out that is capable of tunneling
1705  * Display Port traffic. The @callback is called after tb_tunnel_activate()
1706  * once the tunnel has been fully activated. It can call
1707  * tb_tunnel_is_active() to check if activation was successful (or if it
1708  * returns %false there was some sort of issue). The @callback is called
1709  * without @tb->lock held.
1710  *
1711  * Return: Pointer to @struct tb_tunnel or %NULL in case of failure.
1712  */
tb_tunnel_alloc_dp(struct tb * tb,struct tb_port * in,struct tb_port * out,int link_nr,int max_up,int max_down,void (* callback)(struct tb_tunnel *,void *),void * callback_data)1713 struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
1714 				     struct tb_port *out, int link_nr,
1715 				     int max_up, int max_down,
1716 				     void (*callback)(struct tb_tunnel *, void *),
1717 				     void *callback_data)
1718 {
1719 	struct tb_tunnel *tunnel;
1720 	struct tb_path **paths;
1721 	struct tb_path *path;
1722 	bool pm_support;
1723 
1724 	if (WARN_ON(!in->cap_adap || !out->cap_adap || !callback))
1725 		return NULL;
1726 
1727 	tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
1728 	if (!tunnel)
1729 		return NULL;
1730 
1731 	tunnel->pre_activate = tb_dp_pre_activate;
1732 	tunnel->activate = tb_dp_activate;
1733 	tunnel->post_deactivate = tb_dp_post_deactivate;
1734 	tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth;
1735 	tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth;
1736 	tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth;
1737 	tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
1738 	tunnel->src_port = in;
1739 	tunnel->dst_port = out;
1740 	tunnel->max_up = max_up;
1741 	tunnel->max_down = max_down;
1742 	tunnel->callback = callback;
1743 	tunnel->callback_data = callback_data;
1744 	INIT_DELAYED_WORK(&tunnel->dprx_work, tb_dp_dprx_work);
1745 
1746 	paths = tunnel->paths;
1747 	pm_support = usb4_switch_version(in->sw) >= 2;
1748 
1749 	path = tb_path_alloc(tb, in, TB_DP_VIDEO_HOPID, out, TB_DP_VIDEO_HOPID,
1750 			     link_nr, "Video");
1751 	if (!path)
1752 		goto err_free;
1753 	tb_dp_init_video_path(path, pm_support);
1754 	paths[TB_DP_VIDEO_PATH_OUT] = path;
1755 
1756 	path = tb_path_alloc(tb, in, TB_DP_AUX_TX_HOPID, out,
1757 			     TB_DP_AUX_TX_HOPID, link_nr, "AUX TX");
1758 	if (!path)
1759 		goto err_free;
1760 	tb_dp_init_aux_path(path, pm_support);
1761 	paths[TB_DP_AUX_PATH_OUT] = path;
1762 
1763 	path = tb_path_alloc(tb, out, TB_DP_AUX_RX_HOPID, in,
1764 			     TB_DP_AUX_RX_HOPID, link_nr, "AUX RX");
1765 	if (!path)
1766 		goto err_free;
1767 	tb_dp_init_aux_path(path, pm_support);
1768 	paths[TB_DP_AUX_PATH_IN] = path;
1769 
1770 	return tunnel;
1771 
1772 err_free:
1773 	tb_tunnel_put(tunnel);
1774 	return NULL;
1775 }
1776 
tb_dma_available_credits(const struct tb_port * port)1777 static unsigned int tb_dma_available_credits(const struct tb_port *port)
1778 {
1779 	const struct tb_switch *sw = port->sw;
1780 	int credits;
1781 
1782 	credits = tb_available_credits(port, NULL);
1783 	if (tb_acpi_may_tunnel_pcie())
1784 		credits -= sw->max_pcie_credits;
1785 	credits -= port->dma_credits;
1786 
1787 	return credits > 0 ? credits : 0;
1788 }
1789 
tb_dma_reserve_credits(struct tb_path_hop * hop,unsigned int credits)1790 static int tb_dma_reserve_credits(struct tb_path_hop *hop, unsigned int credits)
1791 {
1792 	struct tb_port *port = hop->in_port;
1793 
1794 	if (tb_port_use_credit_allocation(port)) {
1795 		unsigned int available = tb_dma_available_credits(port);
1796 
1797 		/*
1798 		 * Need to have at least TB_MIN_DMA_CREDITS, otherwise
1799 		 * DMA path cannot be established.
1800 		 */
1801 		if (available < TB_MIN_DMA_CREDITS)
1802 			return -ENOSPC;
1803 
1804 		credits = min(credits, available);
1805 
1806 		tb_port_dbg(port, "reserving %u credits for DMA path\n",
1807 			    credits);
1808 
1809 		port->dma_credits += credits;
1810 	} else {
1811 		if (tb_port_is_null(port))
1812 			credits = port->bonded ? 14 : 6;
1813 		else
1814 			credits = min(port->total_credits, credits);
1815 	}
1816 
1817 	hop->initial_credits = credits;
1818 	return 0;
1819 }
1820 
1821 /* Path from lane adapter to NHI */
tb_dma_init_rx_path(struct tb_path * path,unsigned int credits)1822 static int tb_dma_init_rx_path(struct tb_path *path, unsigned int credits)
1823 {
1824 	struct tb_path_hop *hop;
1825 	unsigned int i, tmp;
1826 
1827 	path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
1828 	path->ingress_fc_enable = TB_PATH_ALL;
1829 	path->egress_shared_buffer = TB_PATH_NONE;
1830 	path->ingress_shared_buffer = TB_PATH_NONE;
1831 	path->priority = TB_DMA_PRIORITY;
1832 	path->weight = TB_DMA_WEIGHT;
1833 	path->clear_fc = true;
1834 
1835 	/*
1836 	 * First lane adapter is the one connected to the remote host.
1837 	 * We don't tunnel other traffic over this link so we can use
1838 	 * all the credits (except the ones reserved for control traffic).
1839 	 */
1840 	hop = &path->hops[0];
1841 	tmp = min(tb_usable_credits(hop->in_port), credits);
1842 	hop->initial_credits = tmp;
1843 	hop->in_port->dma_credits += tmp;
1844 
1845 	for (i = 1; i < path->path_length; i++) {
1846 		int ret;
1847 
1848 		ret = tb_dma_reserve_credits(&path->hops[i], credits);
1849 		if (ret)
1850 			return ret;
1851 	}
1852 
1853 	return 0;
1854 }
1855 
1856 /* Path from NHI to lane adapter */
tb_dma_init_tx_path(struct tb_path * path,unsigned int credits)1857 static int tb_dma_init_tx_path(struct tb_path *path, unsigned int credits)
1858 {
1859 	struct tb_path_hop *hop;
1860 
1861 	path->egress_fc_enable = TB_PATH_ALL;
1862 	path->ingress_fc_enable = TB_PATH_ALL;
1863 	path->egress_shared_buffer = TB_PATH_NONE;
1864 	path->ingress_shared_buffer = TB_PATH_NONE;
1865 	path->priority = TB_DMA_PRIORITY;
1866 	path->weight = TB_DMA_WEIGHT;
1867 	path->clear_fc = true;
1868 
1869 	tb_path_for_each_hop(path, hop) {
1870 		int ret;
1871 
1872 		ret = tb_dma_reserve_credits(hop, credits);
1873 		if (ret)
1874 			return ret;
1875 	}
1876 
1877 	return 0;
1878 }
1879 
tb_dma_release_credits(struct tb_path_hop * hop)1880 static void tb_dma_release_credits(struct tb_path_hop *hop)
1881 {
1882 	struct tb_port *port = hop->in_port;
1883 
1884 	if (tb_port_use_credit_allocation(port)) {
1885 		port->dma_credits -= hop->initial_credits;
1886 
1887 		tb_port_dbg(port, "released %u DMA path credits\n",
1888 			    hop->initial_credits);
1889 	}
1890 }
1891 
tb_dma_destroy_path(struct tb_path * path)1892 static void tb_dma_destroy_path(struct tb_path *path)
1893 {
1894 	struct tb_path_hop *hop;
1895 
1896 	tb_path_for_each_hop(path, hop)
1897 		tb_dma_release_credits(hop);
1898 }
1899 
tb_dma_destroy(struct tb_tunnel * tunnel)1900 static void tb_dma_destroy(struct tb_tunnel *tunnel)
1901 {
1902 	int i;
1903 
1904 	for (i = 0; i < tunnel->npaths; i++) {
1905 		if (!tunnel->paths[i])
1906 			continue;
1907 		tb_dma_destroy_path(tunnel->paths[i]);
1908 	}
1909 }
1910 
1911 /**
1912  * tb_tunnel_alloc_dma() - allocate a DMA tunnel
1913  * @tb: Pointer to the domain structure
1914  * @nhi: Host controller port
1915  * @dst: Destination null port which the other domain is connected to
1916  * @transmit_path: HopID used for transmitting packets
1917  * @transmit_ring: NHI ring number used to send packets towards the
1918  *		   other domain. Set to %-1 if TX path is not needed.
1919  * @receive_path: HopID used for receiving packets
1920  * @receive_ring: NHI ring number used to receive packets from the
1921  *		  other domain. Set to %-1 if RX path is not needed.
1922  *
1923  * Return: Pointer to @struct tb_tunnel or %NULL in case of failure.
1924  */
tb_tunnel_alloc_dma(struct tb * tb,struct tb_port * nhi,struct tb_port * dst,int transmit_path,int transmit_ring,int receive_path,int receive_ring)1925 struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
1926 				      struct tb_port *dst, int transmit_path,
1927 				      int transmit_ring, int receive_path,
1928 				      int receive_ring)
1929 {
1930 	struct tb_tunnel *tunnel;
1931 	size_t npaths = 0, i = 0;
1932 	struct tb_path *path;
1933 	unsigned int credits;
1934 
1935 	/* Ring 0 is reserved for control channel */
1936 	if (WARN_ON(!receive_ring || !transmit_ring))
1937 		return NULL;
1938 
1939 	if (receive_ring > 0)
1940 		npaths++;
1941 	if (transmit_ring > 0)
1942 		npaths++;
1943 
1944 	if (WARN_ON(!npaths))
1945 		return NULL;
1946 
1947 	tunnel = tb_tunnel_alloc(tb, npaths, TB_TUNNEL_DMA);
1948 	if (!tunnel)
1949 		return NULL;
1950 
1951 	tunnel->src_port = nhi;
1952 	tunnel->dst_port = dst;
1953 	tunnel->destroy = tb_dma_destroy;
1954 
1955 	credits = min_not_zero(dma_credits, nhi->sw->max_dma_credits);
1956 	if (credits > TB_MAX_CREDITS) {
1957 		tb_tunnel_dbg(tunnel, "%u credits do not fit a hop, using %u\n",
1958 			      credits, TB_MAX_CREDITS);
1959 		credits = TB_MAX_CREDITS;
1960 	}
1961 
1962 	if (receive_ring > 0) {
1963 		path = tb_path_alloc(tb, dst, receive_path, nhi, receive_ring, 0,
1964 				     "DMA RX");
1965 		if (!path)
1966 			goto err_free;
1967 		tunnel->paths[i++] = path;
1968 		if (tb_dma_init_rx_path(path, credits)) {
1969 			tb_tunnel_dbg(tunnel, "not enough buffers for RX path\n");
1970 			goto err_free;
1971 		}
1972 	}
1973 
1974 	if (transmit_ring > 0) {
1975 		path = tb_path_alloc(tb, nhi, transmit_ring, dst, transmit_path, 0,
1976 				     "DMA TX");
1977 		if (!path)
1978 			goto err_free;
1979 		tunnel->paths[i++] = path;
1980 		if (tb_dma_init_tx_path(path, credits)) {
1981 			tb_tunnel_dbg(tunnel, "not enough buffers for TX path\n");
1982 			goto err_free;
1983 		}
1984 	}
1985 
1986 	return tunnel;
1987 
1988 err_free:
1989 	tb_tunnel_put(tunnel);
1990 	return NULL;
1991 }
1992 
1993 /**
1994  * tb_tunnel_match_dma() - Match DMA tunnel
1995  * @tunnel: Tunnel to match
1996  * @transmit_path: HopID used for transmitting packets. Pass %-1 to ignore.
1997  * @transmit_ring: NHI ring number used to send packets towards the
1998  *		   other domain. Pass %-1 to ignore.
1999  * @receive_path: HopID used for receiving packets. Pass %-1 to ignore.
2000  * @receive_ring: NHI ring number used to receive packets from the
2001  *		  other domain. Pass %-1 to ignore.
2002  *
2003  * This function can be used to match specific DMA tunnel, if there are
2004  * multiple DMA tunnels going through the same XDomain connection.
2005  *
2006  * Return: %true if there is a match, %false otherwise.
2007  */
tb_tunnel_match_dma(const struct tb_tunnel * tunnel,int transmit_path,int transmit_ring,int receive_path,int receive_ring)2008 bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path,
2009 			 int transmit_ring, int receive_path, int receive_ring)
2010 {
2011 	const struct tb_path *tx_path = NULL, *rx_path = NULL;
2012 	int i;
2013 
2014 	if (!receive_ring || !transmit_ring)
2015 		return false;
2016 
2017 	for (i = 0; i < tunnel->npaths; i++) {
2018 		const struct tb_path *path = tunnel->paths[i];
2019 
2020 		if (!path)
2021 			continue;
2022 
2023 		if (tb_port_is_nhi(path->hops[0].in_port))
2024 			tx_path = path;
2025 		else if (tb_port_is_nhi(path->hops[path->path_length - 1].out_port))
2026 			rx_path = path;
2027 	}
2028 
2029 	if (transmit_ring > 0 || transmit_path > 0) {
2030 		if (!tx_path)
2031 			return false;
2032 		if (transmit_ring > 0 &&
2033 		    (tx_path->hops[0].in_hop_index != transmit_ring))
2034 			return false;
2035 		if (transmit_path > 0 &&
2036 		    (tx_path->hops[tx_path->path_length - 1].next_hop_index != transmit_path))
2037 			return false;
2038 	}
2039 
2040 	if (receive_ring > 0 || receive_path > 0) {
2041 		if (!rx_path)
2042 			return false;
2043 		if (receive_path > 0 &&
2044 		    (rx_path->hops[0].in_hop_index != receive_path))
2045 			return false;
2046 		if (receive_ring > 0 &&
2047 		    (rx_path->hops[rx_path->path_length - 1].next_hop_index != receive_ring))
2048 			return false;
2049 	}
2050 
2051 	return true;
2052 }
2053 
tb_usb3_max_link_rate(struct tb_port * up,struct tb_port * down)2054 static int tb_usb3_max_link_rate(struct tb_port *up, struct tb_port *down)
2055 {
2056 	int ret, up_max_rate, down_max_rate;
2057 
2058 	ret = usb4_usb3_port_max_link_rate(up);
2059 	if (ret < 0)
2060 		return ret;
2061 	up_max_rate = ret;
2062 
2063 	ret = usb4_usb3_port_max_link_rate(down);
2064 	if (ret < 0)
2065 		return ret;
2066 	down_max_rate = ret;
2067 
2068 	return min(up_max_rate, down_max_rate);
2069 }
2070 
tb_usb3_pre_activate(struct tb_tunnel * tunnel)2071 static int tb_usb3_pre_activate(struct tb_tunnel *tunnel)
2072 {
2073 	tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n",
2074 		      tunnel->allocated_up, tunnel->allocated_down);
2075 
2076 	return usb4_usb3_port_allocate_bandwidth(tunnel->src_port,
2077 						 &tunnel->allocated_up,
2078 						 &tunnel->allocated_down);
2079 }
2080 
tb_usb3_activate(struct tb_tunnel * tunnel,bool activate)2081 static int tb_usb3_activate(struct tb_tunnel *tunnel, bool activate)
2082 {
2083 	int res;
2084 
2085 	res = tb_usb3_port_enable(tunnel->src_port, activate);
2086 	if (res)
2087 		return res;
2088 
2089 	if (tb_port_is_usb3_up(tunnel->dst_port))
2090 		return tb_usb3_port_enable(tunnel->dst_port, activate);
2091 
2092 	return 0;
2093 }
2094 
tb_usb3_consumed_bandwidth(struct tb_tunnel * tunnel,int * consumed_up,int * consumed_down)2095 static int tb_usb3_consumed_bandwidth(struct tb_tunnel *tunnel,
2096 		int *consumed_up, int *consumed_down)
2097 {
2098 	struct tb_port *port = tb_upstream_port(tunnel->dst_port->sw);
2099 	int pcie_weight = tb_acpi_may_tunnel_pcie() ? TB_PCI_WEIGHT : 0;
2100 
2101 	/*
2102 	 * PCIe tunneling, if enabled, affects the USB3 bandwidth so
2103 	 * take that into account here.
2104 	 */
2105 	*consumed_up = tunnel->allocated_up *
2106 		(TB_USB3_WEIGHT + pcie_weight) / TB_USB3_WEIGHT;
2107 	*consumed_down = tunnel->allocated_down *
2108 		(TB_USB3_WEIGHT + pcie_weight) / TB_USB3_WEIGHT;
2109 
2110 	if (tb_port_get_link_generation(port) >= 4) {
2111 		*consumed_up = max(*consumed_up, USB4_V2_USB3_MIN_BANDWIDTH);
2112 		*consumed_down = max(*consumed_down, USB4_V2_USB3_MIN_BANDWIDTH);
2113 	}
2114 
2115 	return 0;
2116 }
2117 
tb_usb3_release_unused_bandwidth(struct tb_tunnel * tunnel)2118 static int tb_usb3_release_unused_bandwidth(struct tb_tunnel *tunnel)
2119 {
2120 	int ret;
2121 
2122 	ret = usb4_usb3_port_release_bandwidth(tunnel->src_port,
2123 					       &tunnel->allocated_up,
2124 					       &tunnel->allocated_down);
2125 	if (ret)
2126 		return ret;
2127 
2128 	tb_tunnel_dbg(tunnel, "decreased bandwidth allocation to %d/%d Mb/s\n",
2129 		      tunnel->allocated_up, tunnel->allocated_down);
2130 	return 0;
2131 }
2132 
tb_usb3_reclaim_available_bandwidth(struct tb_tunnel * tunnel,int * available_up,int * available_down)2133 static void tb_usb3_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
2134 						int *available_up,
2135 						int *available_down)
2136 {
2137 	int ret, max_rate, allocate_up, allocate_down;
2138 
2139 	ret = tb_usb3_max_link_rate(tunnel->dst_port, tunnel->src_port);
2140 	if (ret < 0) {
2141 		tb_tunnel_warn(tunnel, "failed to read maximum link rate\n");
2142 		return;
2143 	}
2144 
2145 	/*
2146 	 * 90% of the max rate can be allocated for isochronous
2147 	 * transfers.
2148 	 */
2149 	max_rate = ret * 90 / 100;
2150 
2151 	/* No need to reclaim if already at maximum */
2152 	if (tunnel->allocated_up >= max_rate &&
2153 	    tunnel->allocated_down >= max_rate)
2154 		return;
2155 
2156 	/* Don't go lower than what is already allocated */
2157 	allocate_up = min(max_rate, *available_up);
2158 	if (allocate_up < tunnel->allocated_up)
2159 		allocate_up = tunnel->allocated_up;
2160 
2161 	allocate_down = min(max_rate, *available_down);
2162 	if (allocate_down < tunnel->allocated_down)
2163 		allocate_down = tunnel->allocated_down;
2164 
2165 	/* If no changes no need to do more */
2166 	if (allocate_up == tunnel->allocated_up &&
2167 	    allocate_down == tunnel->allocated_down)
2168 		return;
2169 
2170 	ret = usb4_usb3_port_allocate_bandwidth(tunnel->src_port, &allocate_up,
2171 						&allocate_down);
2172 	if (ret) {
2173 		tb_tunnel_info(tunnel, "failed to allocate bandwidth\n");
2174 		return;
2175 	}
2176 
2177 	tunnel->allocated_up = allocate_up;
2178 	*available_up -= tunnel->allocated_up;
2179 
2180 	tunnel->allocated_down = allocate_down;
2181 	*available_down -= tunnel->allocated_down;
2182 
2183 	tb_tunnel_dbg(tunnel, "increased bandwidth allocation to %d/%d Mb/s\n",
2184 		      tunnel->allocated_up, tunnel->allocated_down);
2185 }
2186 
tb_usb3_init_credits(struct tb_path_hop * hop)2187 static void tb_usb3_init_credits(struct tb_path_hop *hop)
2188 {
2189 	struct tb_port *port = hop->in_port;
2190 	struct tb_switch *sw = port->sw;
2191 	unsigned int credits;
2192 
2193 	if (tb_port_use_credit_allocation(port)) {
2194 		credits = sw->max_usb3_credits;
2195 	} else {
2196 		if (tb_port_is_null(port))
2197 			credits = port->bonded ? 32 : 16;
2198 		else
2199 			credits = 7;
2200 	}
2201 
2202 	hop->initial_credits = credits;
2203 }
2204 
tb_usb3_init_path(struct tb_path * path)2205 static void tb_usb3_init_path(struct tb_path *path)
2206 {
2207 	struct tb_path_hop *hop;
2208 
2209 	path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
2210 	path->egress_shared_buffer = TB_PATH_NONE;
2211 	path->ingress_fc_enable = TB_PATH_ALL;
2212 	path->ingress_shared_buffer = TB_PATH_NONE;
2213 	path->priority = TB_USB3_PRIORITY;
2214 	path->weight = TB_USB3_WEIGHT;
2215 	path->drop_packages = 0;
2216 
2217 	tb_path_for_each_hop(path, hop)
2218 		tb_usb3_init_credits(hop);
2219 }
2220 
2221 /**
2222  * tb_tunnel_discover_usb3() - Discover existing USB3 tunnels
2223  * @tb: Pointer to the domain structure
2224  * @down: USB3 downstream adapter
2225  * @alloc_hopid: Allocate HopIDs from visited ports
2226  *
2227  * If @down adapter is active, follows the tunnel to the USB3 upstream
2228  * adapter and back.
2229  *
2230  * Return: Pointer to &struct tb_tunnel or %NULL if there was no tunnel.
2231  */
tb_tunnel_discover_usb3(struct tb * tb,struct tb_port * down,bool alloc_hopid)2232 struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down,
2233 					  bool alloc_hopid)
2234 {
2235 	struct tb_tunnel *tunnel;
2236 	struct tb_path *path;
2237 
2238 	if (!tb_usb3_port_is_enabled(down))
2239 		return NULL;
2240 
2241 	tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
2242 	if (!tunnel)
2243 		return NULL;
2244 
2245 	tunnel->activate = tb_usb3_activate;
2246 	tunnel->src_port = down;
2247 
2248 	/*
2249 	 * Discover both paths even if they are not complete. We will
2250 	 * clean them up by calling tb_tunnel_deactivate() below in that
2251 	 * case.
2252 	 */
2253 	path = tb_path_discover(down, TB_USB3_HOPID, NULL, -1,
2254 				&tunnel->dst_port, "USB3 Down", alloc_hopid);
2255 	if (!path) {
2256 		/* Just disable the downstream port */
2257 		tb_usb3_port_enable(down, false);
2258 		goto err_free;
2259 	}
2260 	tunnel->paths[TB_USB3_PATH_DOWN] = path;
2261 	tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]);
2262 
2263 	path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL,
2264 				"USB3 Up", alloc_hopid);
2265 	if (!path)
2266 		goto err_deactivate;
2267 	tunnel->paths[TB_USB3_PATH_UP] = path;
2268 	tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]);
2269 
2270 	/* Validate that the tunnel is complete */
2271 	if (!tb_port_is_usb3_up(tunnel->dst_port)) {
2272 		tb_port_warn(tunnel->dst_port,
2273 			     "path does not end on an USB3 adapter, cleaning up\n");
2274 		goto err_deactivate;
2275 	}
2276 
2277 	if (down != tunnel->src_port) {
2278 		tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
2279 		goto err_deactivate;
2280 	}
2281 
2282 	if (!tb_usb3_port_is_enabled(tunnel->dst_port)) {
2283 		tb_tunnel_warn(tunnel,
2284 			       "tunnel is not fully activated, cleaning up\n");
2285 		goto err_deactivate;
2286 	}
2287 
2288 	if (!tb_route(down->sw)) {
2289 		int ret;
2290 
2291 		/*
2292 		 * Read the initial bandwidth allocation for the first
2293 		 * hop tunnel.
2294 		 */
2295 		ret = usb4_usb3_port_allocated_bandwidth(down,
2296 			&tunnel->allocated_up, &tunnel->allocated_down);
2297 		if (ret)
2298 			goto err_deactivate;
2299 
2300 		tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n",
2301 			      tunnel->allocated_up, tunnel->allocated_down);
2302 
2303 		tunnel->pre_activate = tb_usb3_pre_activate;
2304 		tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
2305 		tunnel->release_unused_bandwidth =
2306 			tb_usb3_release_unused_bandwidth;
2307 		tunnel->reclaim_available_bandwidth =
2308 			tb_usb3_reclaim_available_bandwidth;
2309 	}
2310 
2311 	tb_tunnel_set_active(tunnel, true);
2312 	tb_tunnel_dbg(tunnel, "discovered\n");
2313 	return tunnel;
2314 
2315 err_deactivate:
2316 	tb_tunnel_deactivate(tunnel);
2317 err_free:
2318 	tb_tunnel_put(tunnel);
2319 
2320 	return NULL;
2321 }
2322 
2323 /**
2324  * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel
2325  * @tb: Pointer to the domain structure
2326  * @up: USB3 upstream adapter port
2327  * @down: USB3 downstream adapter port
2328  * @max_up: Maximum available upstream bandwidth for the USB3 tunnel.
2329  *	    %0 if no available bandwidth.
2330  * @max_down: Maximum available downstream bandwidth for the USB3 tunnel.
2331  *	      %0 if no available bandwidth.
2332  *
2333  * Allocate an USB3 tunnel. The ports must be of type @TB_TYPE_USB3_UP and
2334  * @TB_TYPE_USB3_DOWN.
2335  *
2336  * Return: Pointer to @struct tb_tunnel or %NULL in case of failure.
2337  */
tb_tunnel_alloc_usb3(struct tb * tb,struct tb_port * up,struct tb_port * down,int max_up,int max_down)2338 struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
2339 				       struct tb_port *down, int max_up,
2340 				       int max_down)
2341 {
2342 	struct tb_tunnel *tunnel;
2343 	struct tb_path *path;
2344 	int max_rate = 0;
2345 
2346 	if (!tb_route(down->sw) && (max_up > 0 || max_down > 0)) {
2347 		/*
2348 		 * For USB3 isochronous transfers, we allow bandwidth which is
2349 		 * not higher than 90% of maximum supported bandwidth by USB3
2350 		 * adapters.
2351 		 */
2352 		max_rate = tb_usb3_max_link_rate(down, up);
2353 		if (max_rate < 0)
2354 			return NULL;
2355 
2356 		max_rate = max_rate * 90 / 100;
2357 		tb_port_dbg(up, "maximum required bandwidth for USB3 tunnel %d Mb/s\n",
2358 			    max_rate);
2359 	}
2360 
2361 	tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
2362 	if (!tunnel)
2363 		return NULL;
2364 
2365 	tunnel->activate = tb_usb3_activate;
2366 	tunnel->src_port = down;
2367 	tunnel->dst_port = up;
2368 	tunnel->max_up = max_up;
2369 	tunnel->max_down = max_down;
2370 
2371 	path = tb_path_alloc(tb, down, TB_USB3_HOPID, up, TB_USB3_HOPID, 0,
2372 			     "USB3 Down");
2373 	if (!path)
2374 		goto err_free;
2375 	tb_usb3_init_path(path);
2376 	tunnel->paths[TB_USB3_PATH_DOWN] = path;
2377 
2378 	path = tb_path_alloc(tb, up, TB_USB3_HOPID, down, TB_USB3_HOPID, 0,
2379 			     "USB3 Up");
2380 	if (!path)
2381 		goto err_free;
2382 	tb_usb3_init_path(path);
2383 	tunnel->paths[TB_USB3_PATH_UP] = path;
2384 
2385 	if (!tb_route(down->sw)) {
2386 		tunnel->allocated_up = min(max_rate, max_up);
2387 		tunnel->allocated_down = min(max_rate, max_down);
2388 
2389 		tunnel->pre_activate = tb_usb3_pre_activate;
2390 		tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
2391 		tunnel->release_unused_bandwidth =
2392 			tb_usb3_release_unused_bandwidth;
2393 		tunnel->reclaim_available_bandwidth =
2394 			tb_usb3_reclaim_available_bandwidth;
2395 	}
2396 
2397 	return tunnel;
2398 
2399 err_free:
2400 	tb_tunnel_put(tunnel);
2401 	return NULL;
2402 }
2403 
2404 /**
2405  * tb_tunnel_is_invalid - check whether an activated path is still valid
2406  * @tunnel: Tunnel to check
2407  *
2408  * Return: %true if path is valid, %false otherwise.
2409  */
tb_tunnel_is_invalid(struct tb_tunnel * tunnel)2410 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel)
2411 {
2412 	int i;
2413 
2414 	for (i = 0; i < tunnel->npaths; i++) {
2415 		WARN_ON(!tunnel->paths[i]->activated);
2416 		if (tb_path_is_invalid(tunnel->paths[i]))
2417 			return true;
2418 	}
2419 
2420 	return false;
2421 }
2422 
2423 /**
2424  * tb_tunnel_activate() - activate a tunnel
2425  * @tunnel: Tunnel to activate
2426  *
2427  * Return:
2428  * * %0 - On success.
2429  * * %-EINPROGRESS - If the tunnel activation is still in progress (that's
2430  *   for DP tunnels to complete DPRX capabilities read).
2431  * * Negative errno - Another error occurred.
2432  */
tb_tunnel_activate(struct tb_tunnel * tunnel)2433 int tb_tunnel_activate(struct tb_tunnel *tunnel)
2434 {
2435 	int res, i;
2436 
2437 	tb_tunnel_dbg(tunnel, "activating\n");
2438 
2439 	/*
2440 	 * Make sure all paths are properly disabled before enabling
2441 	 * them again.
2442 	 */
2443 	for (i = 0; i < tunnel->npaths; i++) {
2444 		if (tunnel->paths[i]->activated) {
2445 			tb_path_deactivate(tunnel->paths[i]);
2446 			tunnel->paths[i]->activated = false;
2447 		}
2448 	}
2449 
2450 	tunnel->state = TB_TUNNEL_ACTIVATING;
2451 
2452 	if (tunnel->pre_activate) {
2453 		res = tunnel->pre_activate(tunnel);
2454 		if (res)
2455 			return res;
2456 	}
2457 
2458 	for (i = 0; i < tunnel->npaths; i++) {
2459 		res = tb_path_activate(tunnel->paths[i]);
2460 		if (res)
2461 			goto err;
2462 	}
2463 
2464 	if (tunnel->activate) {
2465 		res = tunnel->activate(tunnel, true);
2466 		if (res) {
2467 			if (res == -EINPROGRESS)
2468 				return res;
2469 			goto err;
2470 		}
2471 	}
2472 
2473 	tb_tunnel_set_active(tunnel, true);
2474 	return 0;
2475 
2476 err:
2477 	tb_tunnel_warn(tunnel, "activation failed\n");
2478 	tb_tunnel_deactivate(tunnel);
2479 	return res;
2480 }
2481 
2482 /**
2483  * tb_tunnel_deactivate() - deactivate a tunnel
2484  * @tunnel: Tunnel to deactivate
2485  */
tb_tunnel_deactivate(struct tb_tunnel * tunnel)2486 void tb_tunnel_deactivate(struct tb_tunnel *tunnel)
2487 {
2488 	int i;
2489 
2490 	tb_tunnel_dbg(tunnel, "deactivating\n");
2491 
2492 	if (tunnel->activate)
2493 		tunnel->activate(tunnel, false);
2494 
2495 	for (i = 0; i < tunnel->npaths; i++) {
2496 		if (tunnel->paths[i] && tunnel->paths[i]->activated)
2497 			tb_path_deactivate(tunnel->paths[i]);
2498 	}
2499 
2500 	if (tunnel->post_deactivate)
2501 		tunnel->post_deactivate(tunnel);
2502 
2503 	tb_tunnel_set_active(tunnel, false);
2504 }
2505 
2506 /**
2507  * tb_tunnel_port_on_path() - Does the tunnel go through port
2508  * @tunnel: Tunnel to check
2509  * @port: Port to check
2510  *
2511  * Return: %true if @tunnel goes through @port (direction does not matter),
2512  * %false otherwise.
2513  */
tb_tunnel_port_on_path(const struct tb_tunnel * tunnel,const struct tb_port * port)2514 bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel,
2515 			    const struct tb_port *port)
2516 {
2517 	int i;
2518 
2519 	for (i = 0; i < tunnel->npaths; i++) {
2520 		if (!tunnel->paths[i])
2521 			continue;
2522 
2523 		if (tb_path_port_on_path(tunnel->paths[i], port))
2524 			return true;
2525 	}
2526 
2527 	return false;
2528 }
2529 
2530 // Is tb_tunnel_activate() called for the tunnel
tb_tunnel_is_activated(const struct tb_tunnel * tunnel)2531 static bool tb_tunnel_is_activated(const struct tb_tunnel *tunnel)
2532 {
2533 	return tunnel->state == TB_TUNNEL_ACTIVATING || tb_tunnel_is_active(tunnel);
2534 }
2535 
2536 /**
2537  * tb_tunnel_maximum_bandwidth() - Return maximum possible bandwidth
2538  * @tunnel: Tunnel to check
2539  * @max_up: Maximum upstream bandwidth in Mb/s
2540  * @max_down: Maximum downstream bandwidth in Mb/s
2541  *
2542  * Return:
2543  * * Maximum possible bandwidth this tunnel can support if not
2544  *   limited by other bandwidth clients.
2545  * * %-EOPNOTSUPP - If the tunnel does not support this function.
2546  * * %-ENOTCONN - If the tunnel is not active.
2547  */
tb_tunnel_maximum_bandwidth(struct tb_tunnel * tunnel,int * max_up,int * max_down)2548 int tb_tunnel_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up,
2549 				int *max_down)
2550 {
2551 	if (!tb_tunnel_is_active(tunnel))
2552 		return -ENOTCONN;
2553 
2554 	if (tunnel->maximum_bandwidth)
2555 		return tunnel->maximum_bandwidth(tunnel, max_up, max_down);
2556 	return -EOPNOTSUPP;
2557 }
2558 
2559 /**
2560  * tb_tunnel_allocated_bandwidth() - Return bandwidth allocated for the tunnel
2561  * @tunnel: Tunnel to check
2562  * @allocated_up: Currently allocated upstream bandwidth in Mb/s is stored here
2563  * @allocated_down: Currently allocated downstream bandwidth in Mb/s is
2564  *		    stored here
2565  *
2566  * Return:
2567  * * Bandwidth allocated for the tunnel. This may be higher than what the
2568  *   tunnel actually consumes.
2569  * * %-EOPNOTSUPP - If the tunnel does not support this function.
2570  * * %-ENOTCONN - If the tunnel is not active.
2571  * * Negative errno - Another error occurred.
2572  */
tb_tunnel_allocated_bandwidth(struct tb_tunnel * tunnel,int * allocated_up,int * allocated_down)2573 int tb_tunnel_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up,
2574 				  int *allocated_down)
2575 {
2576 	if (!tb_tunnel_is_active(tunnel))
2577 		return -ENOTCONN;
2578 
2579 	if (tunnel->allocated_bandwidth)
2580 		return tunnel->allocated_bandwidth(tunnel, allocated_up,
2581 						   allocated_down);
2582 	return -EOPNOTSUPP;
2583 }
2584 
2585 /**
2586  * tb_tunnel_alloc_bandwidth() - Change tunnel bandwidth allocation
2587  * @tunnel: Tunnel whose bandwidth allocation to change
2588  * @alloc_up: New upstream bandwidth in Mb/s
2589  * @alloc_down: New downstream bandwidth in Mb/s
2590  *
2591  * Tries to change tunnel bandwidth allocation.
2592  *
2593  * Return:
2594  * * %0 - On success. Updates @alloc_up and @alloc_down to values that were
2595  *   actually allocated (it may not be the same as passed originally).
2596  * * Negative errno - In case of failure.
2597  */
tb_tunnel_alloc_bandwidth(struct tb_tunnel * tunnel,int * alloc_up,int * alloc_down)2598 int tb_tunnel_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up,
2599 			      int *alloc_down)
2600 {
2601 	if (!tb_tunnel_is_active(tunnel))
2602 		return -ENOTCONN;
2603 
2604 	if (tunnel->alloc_bandwidth) {
2605 		int ret;
2606 
2607 		ret = tunnel->alloc_bandwidth(tunnel, alloc_up, alloc_down);
2608 		if (ret)
2609 			return ret;
2610 
2611 		tb_tunnel_changed(tunnel);
2612 		return 0;
2613 	}
2614 
2615 	return -EOPNOTSUPP;
2616 }
2617 
2618 /**
2619  * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel
2620  * @tunnel: Tunnel to check
2621  * @consumed_up: Consumed bandwidth in Mb/s from @dst_port to @src_port.
2622  *		 Can be %NULL.
2623  * @consumed_down: Consumed bandwidth in Mb/s from @src_port to @dst_port.
2624  *		   Can be %NULL.
2625  *
2626  * Stores the amount of isochronous bandwidth @tunnel consumes in
2627  * @consumed_up and @consumed_down.
2628  *
2629  * Return: %0 on success, negative errno otherwise.
2630  */
tb_tunnel_consumed_bandwidth(struct tb_tunnel * tunnel,int * consumed_up,int * consumed_down)2631 int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
2632 				 int *consumed_down)
2633 {
2634 	int up_bw = 0, down_bw = 0;
2635 
2636 	/*
2637 	 * Here we need to distinguish between not active tunnel from
2638 	 * tunnels that are either fully active or activation started.
2639 	 * The latter is true for DP tunnels where we must report the
2640 	 * consumed to be the maximum we gave it until DPRX capabilities
2641 	 * read is done by the graphics driver.
2642 	 */
2643 	if (tb_tunnel_is_activated(tunnel) && tunnel->consumed_bandwidth) {
2644 		int ret;
2645 
2646 		ret = tunnel->consumed_bandwidth(tunnel, &up_bw, &down_bw);
2647 		if (ret)
2648 			return ret;
2649 	}
2650 
2651 	if (consumed_up)
2652 		*consumed_up = up_bw;
2653 	if (consumed_down)
2654 		*consumed_down = down_bw;
2655 
2656 	tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw, down_bw);
2657 	return 0;
2658 }
2659 
2660 /**
2661  * tb_tunnel_release_unused_bandwidth() - Release unused bandwidth
2662  * @tunnel: Tunnel whose unused bandwidth to release
2663  *
2664  * If tunnel supports dynamic bandwidth management (USB3 tunnels at the
2665  * moment) this function makes it release all the unused bandwidth.
2666  *
2667  * Return: %0 on success, negative errno otherwise.
2668  */
tb_tunnel_release_unused_bandwidth(struct tb_tunnel * tunnel)2669 int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel)
2670 {
2671 	if (!tb_tunnel_is_active(tunnel))
2672 		return -ENOTCONN;
2673 
2674 	if (tunnel->release_unused_bandwidth) {
2675 		int ret;
2676 
2677 		ret = tunnel->release_unused_bandwidth(tunnel);
2678 		if (ret)
2679 			return ret;
2680 	}
2681 
2682 	return 0;
2683 }
2684 
2685 /**
2686  * tb_tunnel_reclaim_available_bandwidth() - Reclaim available bandwidth
2687  * @tunnel: Tunnel reclaiming available bandwidth
2688  * @available_up: Available upstream bandwidth (in Mb/s)
2689  * @available_down: Available downstream bandwidth (in Mb/s)
2690  *
2691  * Reclaims bandwidth from @available_up and @available_down and updates
2692  * the variables accordingly (e.g decreases both according to what was
2693  * reclaimed by the tunnel). If nothing was reclaimed the values are
2694  * kept as is.
2695  */
tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel * tunnel,int * available_up,int * available_down)2696 void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
2697 					   int *available_up,
2698 					   int *available_down)
2699 {
2700 	if (!tb_tunnel_is_active(tunnel))
2701 		return;
2702 
2703 	if (tunnel->reclaim_available_bandwidth)
2704 		tunnel->reclaim_available_bandwidth(tunnel, available_up,
2705 						    available_down);
2706 }
2707 
tb_tunnel_type_name(const struct tb_tunnel * tunnel)2708 const char *tb_tunnel_type_name(const struct tb_tunnel *tunnel)
2709 {
2710 	return tb_tunnel_names[tunnel->type];
2711 }
2712