License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 17:07:57 +03:00
// SPDX-License-Identifier: GPL-2.0
2014-06-04 00:04:08 +04:00
/*
2017-02-19 14:48:29 +03:00
* Thunderbolt driver - Tunneling support
2014-06-04 00:04:08 +04:00
*
* Copyright ( c ) 2014 Andreas Noever < andreas . noever @ gmail . com >
2017-02-19 14:48:29 +03:00
* Copyright ( C ) 2019 , Intel Corporation
2014-06-04 00:04:08 +04:00
*/
2019-02-15 19:18:47 +03:00
# include <linux/delay.h>
2014-06-04 00:04:08 +04:00
# include <linux/slab.h>
# include <linux/list.h>
2022-03-23 17:45:39 +03:00
# include <linux/ktime.h>
2022-09-29 12:49:48 +03:00
# include <linux/string_helpers.h>
2014-06-04 00:04:08 +04:00
2017-02-19 11:58:35 +03:00
# include "tunnel.h"
2014-06-04 00:04:08 +04:00
# include "tb.h"
2017-02-19 23:11:41 +03:00
/* PCIe adapters use always HopID of 8 for both directions */
# define TB_PCI_HOPID 8
2017-02-19 14:48:29 +03:00
# define TB_PCI_PATH_DOWN 0
# define TB_PCI_PATH_UP 1
2023-07-31 05:25:37 +03:00
# define TB_PCI_PRIORITY 3
# define TB_PCI_WEIGHT 1
2019-12-17 15:33:44 +03:00
/* USB3 adapters use always HopID of 8 for both directions */
# define TB_USB3_HOPID 8
# define TB_USB3_PATH_DOWN 0
# define TB_USB3_PATH_UP 1
2023-07-31 05:25:37 +03:00
# define TB_USB3_PRIORITY 3
2023-07-31 05:25:40 +03:00
# define TB_USB3_WEIGHT 2
2023-07-31 05:25:37 +03:00
2018-09-17 16:30:49 +03:00
/* DP adapters use HopID 8 for AUX and 9 for Video */
# define TB_DP_AUX_TX_HOPID 8
# define TB_DP_AUX_RX_HOPID 8
# define TB_DP_VIDEO_HOPID 9
# define TB_DP_VIDEO_PATH_OUT 0
# define TB_DP_AUX_PATH_OUT 1
# define TB_DP_AUX_PATH_IN 2
2023-07-31 05:25:37 +03:00
# define TB_DP_VIDEO_PRIORITY 1
# define TB_DP_VIDEO_WEIGHT 1
# define TB_DP_AUX_PRIORITY 2
# define TB_DP_AUX_WEIGHT 1
2021-03-22 19:09:35 +03:00
/* Minimum number of credits needed for PCIe path */
# define TB_MIN_PCIE_CREDITS 6U
/*
* Number of credits we try to allocate for each DMA path if not limited
* by the host router baMaxHI .
*/
2022-09-08 13:17:26 +03:00
# define TB_DMA_CREDITS 14
2021-03-22 19:09:35 +03:00
/* Minimum number of credits for DMA path */
2022-09-08 13:17:26 +03:00
# define TB_MIN_DMA_CREDITS 1
2023-07-31 05:25:37 +03:00
# define TB_DMA_PRIORITY 5
# define TB_DMA_WEIGHT 1
2023-07-31 05:25:40 +03:00
/*
* Reserve additional bandwidth for USB 3. x and PCIe bulk traffic
* according to USB4 v2 Connection Manager guide . This ends up reserving
* 1500 Mb / s for PCIe and 3000 Mb / s for USB 3. x taking weights into
* account .
*/
# define USB4_V2_PCI_MIN_BANDWIDTH (1500 * TB_PCI_WEIGHT)
# define USB4_V2_USB3_MIN_BANDWIDTH (1500 * TB_USB3_WEIGHT)
2022-09-08 13:17:26 +03:00
static unsigned int dma_credits = TB_DMA_CREDITS ;
module_param ( dma_credits , uint , 0444 ) ;
MODULE_PARM_DESC ( dma_credits , " specify custom credits for DMA tunnels (default: "
__MODULE_STRING ( TB_DMA_CREDITS ) " ) " ) ;
2021-03-22 19:09:35 +03:00
2022-03-23 17:45:39 +03:00
static bool bw_alloc_mode = true ;
module_param ( bw_alloc_mode , bool , 0444 ) ;
MODULE_PARM_DESC ( bw_alloc_mode ,
" enable bandwidth allocation mode if supported (default: true) " ) ;
2019-12-17 15:33:44 +03:00
static const char * const tb_tunnel_names [ ] = { " PCI " , " DP " , " DMA " , " USB3 " } ;
2018-09-17 16:30:49 +03:00
2021-03-22 19:09:35 +03:00
static inline unsigned int tb_usable_credits ( const struct tb_port * port )
{
return port - > total_credits - port - > ctl_credits ;
}
/**
* tb_available_credits ( ) - Available credits for PCIe and DMA
* @ port : Lane adapter to check
* @ max_dp_streams : If non - % NULL stores maximum number of simultaneous DP
* streams possible through this lane adapter
*/
static unsigned int tb_available_credits ( const struct tb_port * port ,
size_t * max_dp_streams )
{
const struct tb_switch * sw = port - > sw ;
int credits , usb3 , pcie , spare ;
size_t ndp ;
usb3 = tb_acpi_may_tunnel_usb3 ( ) ? sw - > max_usb3_credits : 0 ;
pcie = tb_acpi_may_tunnel_pcie ( ) ? sw - > max_pcie_credits : 0 ;
if ( tb_acpi_is_xdomain_allowed ( ) ) {
2022-09-08 13:17:26 +03:00
spare = min_not_zero ( sw - > max_dma_credits , dma_credits ) ;
2021-03-22 19:09:35 +03:00
/* Add some credits for potential second DMA tunnel */
spare + = TB_MIN_DMA_CREDITS ;
} else {
spare = 0 ;
}
credits = tb_usable_credits ( port ) ;
if ( tb_acpi_may_tunnel_dp ( ) ) {
/*
* Maximum number of DP streams possible through the
* lane adapter .
*/
2022-05-09 23:49:03 +03:00
if ( sw - > min_dp_aux_credits + sw - > min_dp_main_credits )
ndp = ( credits - ( usb3 + pcie + spare ) ) /
( sw - > min_dp_aux_credits + sw - > min_dp_main_credits ) ;
else
ndp = 0 ;
2021-03-22 19:09:35 +03:00
} else {
ndp = 0 ;
}
credits - = ndp * ( sw - > min_dp_aux_credits + sw - > min_dp_main_credits ) ;
credits - = usb3 ;
if ( max_dp_streams )
* max_dp_streams = ndp ;
return credits > 0 ? credits : 0 ;
}
2023-09-08 12:07:47 +03:00
static void tb_init_pm_support ( struct tb_path_hop * hop )
{
struct tb_port * out_port = hop - > out_port ;
struct tb_port * in_port = hop - > in_port ;
if ( tb_port_is_null ( in_port ) & & tb_port_is_null ( out_port ) & &
usb4_switch_version ( in_port - > sw ) > = 2 )
hop - > pm_support = true ;
}
2018-09-17 16:30:49 +03:00
static struct tb_tunnel * tb_tunnel_alloc ( struct tb * tb , size_t npaths ,
enum tb_tunnel_type type )
2017-02-19 14:48:29 +03:00
{
struct tb_tunnel * tunnel ;
tunnel = kzalloc ( sizeof ( * tunnel ) , GFP_KERNEL ) ;
if ( ! tunnel )
return NULL ;
tunnel - > paths = kcalloc ( npaths , sizeof ( tunnel - > paths [ 0 ] ) , GFP_KERNEL ) ;
if ( ! tunnel - > paths ) {
tb_tunnel_free ( tunnel ) ;
return NULL ;
}
INIT_LIST_HEAD ( & tunnel - > list ) ;
tunnel - > tb = tb ;
tunnel - > npaths = npaths ;
2018-09-17 16:30:49 +03:00
tunnel - > type = type ;
2017-02-19 14:48:29 +03:00
return tunnel ;
}
2022-09-29 12:49:48 +03:00
static int tb_pci_set_ext_encapsulation ( struct tb_tunnel * tunnel , bool enable )
{
2023-07-31 05:25:40 +03:00
struct tb_port * port = tb_upstream_port ( tunnel - > dst_port - > sw ) ;
2022-09-29 12:49:48 +03:00
int ret ;
/* Only supported of both routers are at least USB4 v2 */
2023-11-15 13:09:57 +03:00
if ( ( usb4_switch_version ( tunnel - > src_port - > sw ) < 2 ) | |
( usb4_switch_version ( tunnel - > dst_port - > sw ) < 2 ) )
return 0 ;
if ( enable & & tb_port_get_link_generation ( port ) < 4 )
2022-09-29 12:49:48 +03:00
return 0 ;
ret = usb4_pci_port_set_ext_encapsulation ( tunnel - > src_port , enable ) ;
if ( ret )
return ret ;
2023-11-15 13:09:57 +03:00
/*
* Downstream router could be unplugged so disable of encapsulation
* in upstream router is still possible .
*/
2022-09-29 12:49:48 +03:00
ret = usb4_pci_port_set_ext_encapsulation ( tunnel - > dst_port , enable ) ;
2023-11-15 13:09:57 +03:00
if ( ret ) {
if ( enable )
return ret ;
if ( ret ! = - ENODEV )
return ret ;
}
2022-09-29 12:49:48 +03:00
tb_tunnel_dbg ( tunnel , " extended encapsulation %s \n " ,
str_enabled_disabled ( enable ) ) ;
return 0 ;
}
2017-02-19 14:48:29 +03:00
static int tb_pci_activate ( struct tb_tunnel * tunnel , bool activate )
{
int res ;
2022-09-29 12:49:48 +03:00
if ( activate ) {
res = tb_pci_set_ext_encapsulation ( tunnel , activate ) ;
if ( res )
return res ;
}
2023-11-15 13:09:56 +03:00
if ( activate )
res = tb_pci_port_enable ( tunnel - > dst_port , activate ) ;
else
res = tb_pci_port_enable ( tunnel - > src_port , activate ) ;
2017-02-19 14:48:29 +03:00
if ( res )
return res ;
2023-11-15 13:09:56 +03:00
if ( activate ) {
res = tb_pci_port_enable ( tunnel - > src_port , activate ) ;
2022-09-29 12:49:48 +03:00
if ( res )
return res ;
2023-11-15 13:09:56 +03:00
} else {
/* Downstream router could be unplugged */
tb_pci_port_enable ( tunnel - > dst_port , activate ) ;
2022-09-29 12:49:48 +03:00
}
2017-02-20 00:43:26 +03:00
2022-09-29 12:49:48 +03:00
return activate ? 0 : tb_pci_set_ext_encapsulation ( tunnel , activate ) ;
2017-02-19 14:48:29 +03:00
}
2021-03-22 19:09:35 +03:00
static int tb_pci_init_credits ( struct tb_path_hop * hop )
2019-03-21 20:03:00 +03:00
{
2021-03-22 19:09:35 +03:00
struct tb_port * port = hop - > in_port ;
struct tb_switch * sw = port - > sw ;
unsigned int credits ;
if ( tb_port_use_credit_allocation ( port ) ) {
unsigned int available ;
available = tb_available_credits ( port , NULL ) ;
credits = min ( sw - > max_pcie_credits , available ) ;
if ( credits < TB_MIN_PCIE_CREDITS )
return - ENOSPC ;
credits = max ( TB_MIN_PCIE_CREDITS , credits ) ;
} else {
if ( tb_port_is_null ( port ) )
credits = port - > bonded ? 32 : 16 ;
else
credits = 7 ;
2019-03-21 20:03:00 +03:00
}
2021-03-22 19:09:35 +03:00
hop - > initial_credits = credits ;
return 0 ;
2019-03-21 20:03:00 +03:00
}
2021-03-22 19:09:35 +03:00
static int tb_pci_init_path ( struct tb_path * path )
2014-06-04 00:04:08 +04:00
{
2021-03-22 19:09:35 +03:00
struct tb_path_hop * hop ;
2014-06-04 00:04:08 +04:00
path - > egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL ;
path - > egress_shared_buffer = TB_PATH_NONE ;
path - > ingress_fc_enable = TB_PATH_ALL ;
path - > ingress_shared_buffer = TB_PATH_NONE ;
2023-07-31 05:25:37 +03:00
path - > priority = TB_PCI_PRIORITY ;
path - > weight = TB_PCI_WEIGHT ;
2014-06-04 00:04:08 +04:00
path - > drop_packages = 0 ;
2021-03-22 19:09:35 +03:00
tb_path_for_each_hop ( path , hop ) {
int ret ;
ret = tb_pci_init_credits ( hop ) ;
if ( ret )
return ret ;
}
return 0 ;
2017-02-20 00:43:26 +03:00
}
/**
* tb_tunnel_discover_pci ( ) - Discover existing PCIe tunnels
* @ tb : Pointer to the domain structure
* @ down : PCIe downstream adapter
2021-11-14 18:20:59 +03:00
* @ alloc_hopid : Allocate HopIDs from visited ports
2017-02-20 00:43:26 +03:00
*
* If @ down adapter is active , follows the tunnel to the PCIe upstream
* adapter and back . Returns the discovered tunnel or % NULL if there was
* no tunnel .
*/
2021-11-14 18:20:59 +03:00
struct tb_tunnel * tb_tunnel_discover_pci ( struct tb * tb , struct tb_port * down ,
bool alloc_hopid )
2017-02-20 00:43:26 +03:00
{
struct tb_tunnel * tunnel ;
struct tb_path * path ;
if ( ! tb_pci_port_is_enabled ( down ) )
return NULL ;
2018-09-17 16:30:49 +03:00
tunnel = tb_tunnel_alloc ( tb , 2 , TB_TUNNEL_PCI ) ;
2017-02-20 00:43:26 +03:00
if ( ! tunnel )
return NULL ;
tunnel - > activate = tb_pci_activate ;
tunnel - > src_port = down ;
/*
* Discover both paths even if they are not complete . We will
* clean them up by calling tb_tunnel_deactivate ( ) below in that
* case .
*/
path = tb_path_discover ( down , TB_PCI_HOPID , NULL , - 1 ,
2021-11-14 18:20:59 +03:00
& tunnel - > dst_port , " PCIe Up " , alloc_hopid ) ;
2017-02-20 00:43:26 +03:00
if ( ! path ) {
/* Just disable the downstream port */
tb_pci_port_enable ( down , false ) ;
goto err_free ;
}
tunnel - > paths [ TB_PCI_PATH_UP ] = path ;
2021-03-22 19:09:35 +03:00
if ( tb_pci_init_path ( tunnel - > paths [ TB_PCI_PATH_UP ] ) )
goto err_free ;
2017-02-20 00:43:26 +03:00
path = tb_path_discover ( tunnel - > dst_port , - 1 , down , TB_PCI_HOPID , NULL ,
2021-11-14 18:20:59 +03:00
" PCIe Down " , alloc_hopid ) ;
2017-02-20 00:43:26 +03:00
if ( ! path )
goto err_deactivate ;
tunnel - > paths [ TB_PCI_PATH_DOWN ] = path ;
2021-03-22 19:09:35 +03:00
if ( tb_pci_init_path ( tunnel - > paths [ TB_PCI_PATH_DOWN ] ) )
goto err_deactivate ;
2017-02-20 00:43:26 +03:00
/* Validate that the tunnel is complete */
if ( ! tb_port_is_pcie_up ( tunnel - > dst_port ) ) {
tb_port_warn ( tunnel - > dst_port ,
" path does not end on a PCIe adapter, cleaning up \n " ) ;
goto err_deactivate ;
}
if ( down ! = tunnel - > src_port ) {
tb_tunnel_warn ( tunnel , " path is not complete, cleaning up \n " ) ;
goto err_deactivate ;
}
if ( ! tb_pci_port_is_enabled ( tunnel - > dst_port ) ) {
tb_tunnel_warn ( tunnel ,
" tunnel is not fully activated, cleaning up \n " ) ;
goto err_deactivate ;
}
tb_tunnel_dbg ( tunnel , " discovered \n " ) ;
return tunnel ;
err_deactivate :
tb_tunnel_deactivate ( tunnel ) ;
err_free :
tb_tunnel_free ( tunnel ) ;
return NULL ;
2014-06-04 00:04:08 +04:00
}
/**
2017-02-19 14:48:29 +03:00
* tb_tunnel_alloc_pci ( ) - allocate a pci tunnel
* @ tb : Pointer to the domain structure
* @ up : PCIe upstream adapter port
* @ down : PCIe downstream adapter port
2014-06-04 00:04:08 +04:00
*
* Allocate a PCI tunnel . The ports must be of type TB_TYPE_PCIE_UP and
* TB_TYPE_PCIE_DOWN .
*
2017-02-19 14:48:29 +03:00
* Return : Returns a tb_tunnel on success or NULL on failure .
2014-06-04 00:04:08 +04:00
*/
2017-02-19 14:48:29 +03:00
struct tb_tunnel * tb_tunnel_alloc_pci ( struct tb * tb , struct tb_port * up ,
struct tb_port * down )
2014-06-04 00:04:08 +04:00
{
2017-02-19 14:48:29 +03:00
struct tb_tunnel * tunnel ;
2017-02-19 23:11:41 +03:00
struct tb_path * path ;
2017-02-19 14:48:29 +03:00
2018-09-17 16:30:49 +03:00
tunnel = tb_tunnel_alloc ( tb , 2 , TB_TUNNEL_PCI ) ;
2014-06-04 00:04:08 +04:00
if ( ! tunnel )
2017-02-19 14:48:29 +03:00
return NULL ;
2014-06-04 00:04:08 +04:00
2017-02-19 14:48:29 +03:00
tunnel - > activate = tb_pci_activate ;
tunnel - > src_port = down ;
tunnel - > dst_port = up ;
2017-02-19 23:11:41 +03:00
path = tb_path_alloc ( tb , down , TB_PCI_HOPID , up , TB_PCI_HOPID , 0 ,
" PCIe Down " ) ;
2021-03-22 19:09:35 +03:00
if ( ! path )
goto err_free ;
2019-06-11 19:31:26 +03:00
tunnel - > paths [ TB_PCI_PATH_DOWN ] = path ;
2021-03-22 19:09:35 +03:00
if ( tb_pci_init_path ( path ) )
goto err_free ;
2017-02-19 14:48:29 +03:00
2017-02-19 23:11:41 +03:00
path = tb_path_alloc ( tb , up , TB_PCI_HOPID , down , TB_PCI_HOPID , 0 ,
" PCIe Up " ) ;
2021-03-22 19:09:35 +03:00
if ( ! path )
goto err_free ;
2019-06-11 19:31:26 +03:00
tunnel - > paths [ TB_PCI_PATH_UP ] = path ;
2021-03-22 19:09:35 +03:00
if ( tb_pci_init_path ( path ) )
goto err_free ;
2017-02-19 14:48:29 +03:00
return tunnel ;
2021-03-22 19:09:35 +03:00
err_free :
tb_tunnel_free ( tunnel ) ;
return NULL ;
2014-06-04 00:04:08 +04:00
}
2023-07-31 05:25:40 +03:00
/**
* tb_tunnel_reserved_pci ( ) - Amount of bandwidth to reserve for PCIe
* @ port : Lane 0 adapter
* @ reserved_up : Upstream bandwidth in Mb / s to reserve
* @ reserved_down : Downstream bandwidth in Mb / s to reserve
*
* Can be called to any connected lane 0 adapter to find out how much
* bandwidth needs to be left in reserve for possible PCIe bulk traffic .
* Returns true if there is something to be reserved and writes the
* amount to @ reserved_down / @ reserved_up . Otherwise returns false and
* does not touch the parameters .
*/
bool tb_tunnel_reserved_pci ( struct tb_port * port , int * reserved_up ,
int * reserved_down )
{
if ( WARN_ON_ONCE ( ! port - > remote ) )
return false ;
if ( ! tb_acpi_may_tunnel_pcie ( ) )
return false ;
if ( tb_port_get_link_generation ( port ) < 4 )
return false ;
/* Must have PCIe adapters */
if ( tb_is_upstream_port ( port ) ) {
if ( ! tb_switch_find_port ( port - > sw , TB_TYPE_PCIE_UP ) )
return false ;
if ( ! tb_switch_find_port ( port - > remote - > sw , TB_TYPE_PCIE_DOWN ) )
return false ;
} else {
if ( ! tb_switch_find_port ( port - > sw , TB_TYPE_PCIE_DOWN ) )
return false ;
if ( ! tb_switch_find_port ( port - > remote - > sw , TB_TYPE_PCIE_UP ) )
return false ;
}
* reserved_up = USB4_V2_PCI_MIN_BANDWIDTH ;
* reserved_down = USB4_V2_PCI_MIN_BANDWIDTH ;
tb_port_dbg ( port , " reserving %u/%u Mb/s for PCIe \n " , * reserved_up ,
* reserved_down ) ;
return true ;
}
2019-12-17 15:33:40 +03:00
static bool tb_dp_is_usb4 ( const struct tb_switch * sw )
{
/* Titan Ridge DP adapters need the same treatment as USB4 */
return tb_switch_is_usb4 ( sw ) | | tb_switch_is_titan_ridge ( sw ) ;
}
2022-06-19 18:02:48 +03:00
static int tb_dp_cm_handshake ( struct tb_port * in , struct tb_port * out ,
int timeout_msec )
2019-02-15 19:18:47 +03:00
{
2022-06-19 18:02:48 +03:00
ktime_t timeout = ktime_add_ms ( ktime_get ( ) , timeout_msec ) ;
2019-02-15 19:18:47 +03:00
u32 val ;
int ret ;
/* Both ends need to support this */
2019-12-17 15:33:40 +03:00
if ( ! tb_dp_is_usb4 ( in - > sw ) | | ! tb_dp_is_usb4 ( out - > sw ) )
2019-02-15 19:18:47 +03:00
return 0 ;
ret = tb_port_read ( out , & val , TB_CFG_PORT ,
out - > cap_adap + DP_STATUS_CTRL , 1 ) ;
if ( ret )
return ret ;
val | = DP_STATUS_CTRL_UF | DP_STATUS_CTRL_CMHS ;
ret = tb_port_write ( out , & val , TB_CFG_PORT ,
out - > cap_adap + DP_STATUS_CTRL , 1 ) ;
if ( ret )
return ret ;
do {
ret = tb_port_read ( out , & val , TB_CFG_PORT ,
out - > cap_adap + DP_STATUS_CTRL , 1 ) ;
if ( ret )
return ret ;
if ( ! ( val & DP_STATUS_CTRL_CMHS ) )
return 0 ;
2022-06-19 18:02:48 +03:00
usleep_range ( 100 , 150 ) ;
} while ( ktime_before ( ktime_get ( ) , timeout ) ) ;
2019-02-15 19:18:47 +03:00
return - ETIMEDOUT ;
}
2023-01-23 11:42:58 +03:00
/*
* Returns maximum possible rate from capability supporting only DP 2.0
* and below . Used when DP BW allocation mode is not enabled .
*/
2019-03-26 16:03:48 +03:00
static inline u32 tb_dp_cap_get_rate ( u32 val )
{
u32 rate = ( val & DP_COMMON_CAP_RATE_MASK ) > > DP_COMMON_CAP_RATE_SHIFT ;
switch ( rate ) {
case DP_COMMON_CAP_RATE_RBR :
return 1620 ;
case DP_COMMON_CAP_RATE_HBR :
return 2700 ;
case DP_COMMON_CAP_RATE_HBR2 :
return 5400 ;
case DP_COMMON_CAP_RATE_HBR3 :
return 8100 ;
default :
return 0 ;
}
}
2023-01-23 11:42:58 +03:00
/*
* Returns maximum possible rate from capability supporting DP 2.1
* UHBR20 , 13.5 and 10 rates as well . Use only when DP BW allocation
* mode is enabled .
*/
static inline u32 tb_dp_cap_get_rate_ext ( u32 val )
{
if ( val & DP_COMMON_CAP_UHBR20 )
return 20000 ;
else if ( val & DP_COMMON_CAP_UHBR13_5 )
return 13500 ;
else if ( val & DP_COMMON_CAP_UHBR10 )
return 10000 ;
return tb_dp_cap_get_rate ( val ) ;
}
static inline bool tb_dp_is_uhbr_rate ( unsigned int rate )
{
return rate > = 10000 ;
}
2019-03-26 16:03:48 +03:00
static inline u32 tb_dp_cap_set_rate ( u32 val , u32 rate )
{
val & = ~ DP_COMMON_CAP_RATE_MASK ;
switch ( rate ) {
default :
WARN ( 1 , " invalid rate %u passed, defaulting to 1620 MB/s \n " , rate ) ;
2020-08-24 01:36:59 +03:00
fallthrough ;
2019-03-26 16:03:48 +03:00
case 1620 :
val | = DP_COMMON_CAP_RATE_RBR < < DP_COMMON_CAP_RATE_SHIFT ;
break ;
case 2700 :
val | = DP_COMMON_CAP_RATE_HBR < < DP_COMMON_CAP_RATE_SHIFT ;
break ;
case 5400 :
val | = DP_COMMON_CAP_RATE_HBR2 < < DP_COMMON_CAP_RATE_SHIFT ;
break ;
case 8100 :
val | = DP_COMMON_CAP_RATE_HBR3 < < DP_COMMON_CAP_RATE_SHIFT ;
break ;
}
return val ;
}
static inline u32 tb_dp_cap_get_lanes ( u32 val )
{
u32 lanes = ( val & DP_COMMON_CAP_LANES_MASK ) > > DP_COMMON_CAP_LANES_SHIFT ;
switch ( lanes ) {
case DP_COMMON_CAP_1_LANE :
return 1 ;
case DP_COMMON_CAP_2_LANES :
return 2 ;
case DP_COMMON_CAP_4_LANES :
return 4 ;
default :
return 0 ;
}
}
static inline u32 tb_dp_cap_set_lanes ( u32 val , u32 lanes )
{
val & = ~ DP_COMMON_CAP_LANES_MASK ;
switch ( lanes ) {
default :
WARN ( 1 , " invalid number of lanes %u passed, defaulting to 1 \n " ,
lanes ) ;
2020-08-24 01:36:59 +03:00
fallthrough ;
2019-03-26 16:03:48 +03:00
case 1 :
val | = DP_COMMON_CAP_1_LANE < < DP_COMMON_CAP_LANES_SHIFT ;
break ;
case 2 :
val | = DP_COMMON_CAP_2_LANES < < DP_COMMON_CAP_LANES_SHIFT ;
break ;
case 4 :
val | = DP_COMMON_CAP_4_LANES < < DP_COMMON_CAP_LANES_SHIFT ;
break ;
}
return val ;
}
static unsigned int tb_dp_bandwidth ( unsigned int rate , unsigned int lanes )
{
2023-01-23 11:42:58 +03:00
/* Tunneling removes the DP 8b/10b 128/132b encoding */
if ( tb_dp_is_uhbr_rate ( rate ) )
return rate * lanes * 128 / 132 ;
2019-03-26 16:03:48 +03:00
return rate * lanes * 8 / 10 ;
}
static int 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 )
{
static const u32 dp_bw [ ] [ 2 ] = {
/* Mb/s, lanes */
{ 8100 , 4 } , /* 25920 Mb/s */
{ 5400 , 4 } , /* 17280 Mb/s */
{ 8100 , 2 } , /* 12960 Mb/s */
{ 2700 , 4 } , /* 8640 Mb/s */
{ 5400 , 2 } , /* 8640 Mb/s */
{ 8100 , 1 } , /* 6480 Mb/s */
{ 1620 , 4 } , /* 5184 Mb/s */
{ 5400 , 1 } , /* 4320 Mb/s */
{ 2700 , 2 } , /* 4320 Mb/s */
{ 1620 , 2 } , /* 2592 Mb/s */
{ 2700 , 1 } , /* 2160 Mb/s */
{ 1620 , 1 } , /* 1296 Mb/s */
} ;
unsigned int i ;
/*
* Find a combination that can fit into max_bw and does not
* exceed the maximum rate and lanes supported by the DP OUT and
* DP IN adapters .
*/
for ( i = 0 ; i < ARRAY_SIZE ( dp_bw ) ; i + + ) {
if ( dp_bw [ i ] [ 0 ] > out_rate | | dp_bw [ i ] [ 1 ] > out_lanes )
continue ;
if ( dp_bw [ i ] [ 0 ] > in_rate | | dp_bw [ i ] [ 1 ] > in_lanes )
continue ;
if ( tb_dp_bandwidth ( dp_bw [ i ] [ 0 ] , dp_bw [ i ] [ 1 ] ) < = max_bw ) {
* new_rate = dp_bw [ i ] [ 0 ] ;
* new_lanes = dp_bw [ i ] [ 1 ] ;
return 0 ;
}
}
return - ENOSR ;
}
2018-09-17 16:30:49 +03:00
static int tb_dp_xchg_caps ( struct tb_tunnel * tunnel )
{
2019-03-26 16:03:48 +03:00
u32 out_dp_cap , out_rate , out_lanes , in_dp_cap , in_rate , in_lanes , bw ;
2018-09-17 16:30:49 +03:00
struct tb_port * out = tunnel - > dst_port ;
struct tb_port * in = tunnel - > src_port ;
2020-03-24 15:44:13 +03:00
int ret , max_bw ;
2018-09-17 16:30:49 +03:00
/*
* Copy DP_LOCAL_CAP register to DP_REMOTE_CAP register for
* newer generation hardware .
*/
if ( in - > sw - > generation < 2 | | out - > sw - > generation < 2 )
return 0 ;
2019-02-15 19:18:47 +03:00
/*
* Perform connection manager handshake between IN and OUT ports
* before capabilities exchange can take place .
*/
2023-05-24 10:41:57 +03:00
ret = tb_dp_cm_handshake ( in , out , 3000 ) ;
2019-02-15 19:18:47 +03:00
if ( ret )
return ret ;
2018-09-17 16:30:49 +03:00
/* Read both DP_LOCAL_CAP registers */
ret = tb_port_read ( in , & in_dp_cap , TB_CFG_PORT ,
2019-09-06 11:32:15 +03:00
in - > cap_adap + DP_LOCAL_CAP , 1 ) ;
2018-09-17 16:30:49 +03:00
if ( ret )
return ret ;
ret = tb_port_read ( out , & out_dp_cap , TB_CFG_PORT ,
2019-09-06 11:32:15 +03:00
out - > cap_adap + DP_LOCAL_CAP , 1 ) ;
2018-09-17 16:30:49 +03:00
if ( ret )
return ret ;
/* Write IN local caps to OUT remote caps */
ret = tb_port_write ( out , & in_dp_cap , TB_CFG_PORT ,
2019-09-06 11:32:15 +03:00
out - > cap_adap + DP_REMOTE_CAP , 1 ) ;
2018-09-17 16:30:49 +03:00
if ( ret )
return ret ;
2019-03-26 16:03:48 +03:00
in_rate = tb_dp_cap_get_rate ( in_dp_cap ) ;
in_lanes = tb_dp_cap_get_lanes ( in_dp_cap ) ;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel ,
" DP IN maximum supported bandwidth %u Mb/s x%u = %u Mb/s \n " ,
in_rate , in_lanes , tb_dp_bandwidth ( in_rate , in_lanes ) ) ;
2019-03-26 16:03:48 +03:00
/*
* If the tunnel bandwidth is limited ( max_bw is set ) then see
* if we need to reduce bandwidth to fit there .
*/
out_rate = tb_dp_cap_get_rate ( out_dp_cap ) ;
out_lanes = tb_dp_cap_get_lanes ( out_dp_cap ) ;
bw = tb_dp_bandwidth ( out_rate , out_lanes ) ;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel ,
" DP OUT maximum supported bandwidth %u Mb/s x%u = %u Mb/s \n " ,
out_rate , out_lanes , bw ) ;
2019-03-26 16:03:48 +03:00
2023-08-10 22:37:14 +03:00
if ( tb_port_path_direction_downstream ( in , out ) )
2020-03-24 15:44:13 +03:00
max_bw = tunnel - > max_down ;
else
max_bw = tunnel - > max_up ;
if ( max_bw & & bw > max_bw ) {
2019-03-26 16:03:48 +03:00
u32 new_rate , new_lanes , new_bw ;
2020-03-24 15:44:13 +03:00
ret = tb_dp_reduce_bandwidth ( max_bw , in_rate , in_lanes ,
2019-03-26 16:03:48 +03:00
out_rate , out_lanes , & new_rate ,
& new_lanes ) ;
if ( ret ) {
2023-09-19 15:03:58 +03:00
tb_tunnel_info ( tunnel , " not enough bandwidth \n " ) ;
2019-03-26 16:03:48 +03:00
return ret ;
}
new_bw = tb_dp_bandwidth ( new_rate , new_lanes ) ;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel ,
" bandwidth reduced to %u Mb/s x%u = %u Mb/s \n " ,
new_rate , new_lanes , new_bw ) ;
2019-03-26 16:03:48 +03:00
/*
* Set new rate and number of lanes before writing it to
* the IN port remote caps .
*/
out_dp_cap = tb_dp_cap_set_rate ( out_dp_cap , new_rate ) ;
out_dp_cap = tb_dp_cap_set_lanes ( out_dp_cap , new_lanes ) ;
}
2021-10-27 13:29:09 +03:00
/*
* Titan Ridge does not disable AUX timers when it gets
* SET_CONFIG with SET_LTTPR_MODE set . This causes problems with
* DP tunneling .
*/
if ( tb_route ( out - > sw ) & & tb_switch_is_titan_ridge ( out - > sw ) ) {
out_dp_cap | = DP_COMMON_CAP_LTTPR_NS ;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel , " disabling LTTPR \n " ) ;
2021-10-27 13:29:09 +03:00
}
2018-09-17 16:30:49 +03:00
return tb_port_write ( in , & out_dp_cap , TB_CFG_PORT ,
2019-09-06 11:32:15 +03:00
in - > cap_adap + DP_REMOTE_CAP , 1 ) ;
2018-09-17 16:30:49 +03:00
}
2023-02-01 14:21:37 +03:00
static int tb_dp_bandwidth_alloc_mode_enable ( struct tb_tunnel * tunnel )
2022-03-23 17:45:39 +03:00
{
int ret , estimated_bw , granularity , tmp ;
struct tb_port * out = tunnel - > dst_port ;
struct tb_port * in = tunnel - > src_port ;
u32 out_dp_cap , out_rate , out_lanes ;
u32 in_dp_cap , in_rate , in_lanes ;
u32 rate , lanes ;
if ( ! bw_alloc_mode )
return 0 ;
2023-02-01 14:21:37 +03:00
ret = usb4_dp_port_set_cm_bandwidth_mode_supported ( in , true ) ;
2022-03-23 17:45:39 +03:00
if ( ret )
return ret ;
ret = usb4_dp_port_set_group_id ( in , in - > group - > index ) ;
if ( ret )
return ret ;
/*
* Get the non - reduced rate and lanes based on the lowest
* capability of both adapters .
*/
ret = tb_port_read ( in , & in_dp_cap , TB_CFG_PORT ,
in - > cap_adap + DP_LOCAL_CAP , 1 ) ;
if ( ret )
return ret ;
ret = tb_port_read ( out , & out_dp_cap , TB_CFG_PORT ,
out - > cap_adap + DP_LOCAL_CAP , 1 ) ;
if ( ret )
return ret ;
in_rate = tb_dp_cap_get_rate ( in_dp_cap ) ;
in_lanes = tb_dp_cap_get_lanes ( in_dp_cap ) ;
out_rate = tb_dp_cap_get_rate ( out_dp_cap ) ;
out_lanes = tb_dp_cap_get_lanes ( out_dp_cap ) ;
rate = min ( in_rate , out_rate ) ;
lanes = min ( in_lanes , out_lanes ) ;
tmp = tb_dp_bandwidth ( rate , lanes ) ;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel , " non-reduced bandwidth %u Mb/s x%u = %u Mb/s \n " ,
rate , lanes , tmp ) ;
2022-03-23 17:45:39 +03:00
ret = usb4_dp_port_set_nrd ( in , rate , lanes ) ;
if ( ret )
return ret ;
2023-01-23 11:42:58 +03:00
/*
* Pick up granularity that supports maximum possible bandwidth .
* For that we use the UHBR rates too .
*/
in_rate = tb_dp_cap_get_rate_ext ( in_dp_cap ) ;
out_rate = tb_dp_cap_get_rate_ext ( out_dp_cap ) ;
rate = min ( in_rate , out_rate ) ;
tmp = tb_dp_bandwidth ( rate , lanes ) ;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel ,
" maximum bandwidth through allocation mode %u Mb/s x%u = %u Mb/s \n " ,
rate , lanes , tmp ) ;
2023-01-23 11:42:58 +03:00
2022-03-23 17:45:39 +03:00
for ( granularity = 250 ; tmp / granularity > 255 & & granularity < = 1000 ;
granularity * = 2 )
;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel , " granularity %d Mb/s \n " , granularity ) ;
2022-03-23 17:45:39 +03:00
/*
* Returns - EINVAL if granularity above is outside of the
* accepted ranges .
*/
ret = usb4_dp_port_set_granularity ( in , granularity ) ;
if ( ret )
return ret ;
/*
* Bandwidth estimation is pretty much what we have in
* max_up / down fields . For discovery we just read what the
* estimation was set to .
*/
2023-08-10 22:37:14 +03:00
if ( tb_port_path_direction_downstream ( in , out ) )
2022-03-23 17:45:39 +03:00
estimated_bw = tunnel - > max_down ;
else
estimated_bw = tunnel - > max_up ;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel , " estimated bandwidth %d Mb/s \n " , estimated_bw ) ;
2022-03-23 17:45:39 +03:00
2023-02-01 14:21:37 +03:00
ret = usb4_dp_port_set_estimated_bandwidth ( in , estimated_bw ) ;
2022-03-23 17:45:39 +03:00
if ( ret )
return ret ;
/* Initial allocation should be 0 according the spec */
2023-02-01 14:21:37 +03:00
ret = usb4_dp_port_allocate_bandwidth ( in , 0 ) ;
2022-03-23 17:45:39 +03:00
if ( ret )
return ret ;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel , " bandwidth allocation mode enabled \n " ) ;
2022-03-23 17:45:39 +03:00
return 0 ;
}
static int tb_dp_init ( struct tb_tunnel * tunnel )
{
struct tb_port * in = tunnel - > src_port ;
struct tb_switch * sw = in - > sw ;
struct tb * tb = in - > sw - > tb ;
int ret ;
ret = tb_dp_xchg_caps ( tunnel ) ;
if ( ret )
return ret ;
if ( ! tb_switch_is_usb4 ( sw ) )
return 0 ;
2023-02-01 14:21:37 +03:00
if ( ! usb4_dp_port_bandwidth_mode_supported ( in ) )
2022-03-23 17:45:39 +03:00
return 0 ;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel , " bandwidth allocation mode supported \n " ) ;
2022-03-23 17:45:39 +03:00
ret = usb4_dp_port_set_cm_id ( in , tb - > index ) ;
if ( ret )
return ret ;
2023-02-01 14:21:37 +03:00
return tb_dp_bandwidth_alloc_mode_enable ( tunnel ) ;
2022-03-23 17:45:39 +03:00
}
static void tb_dp_deinit ( struct tb_tunnel * tunnel )
{
struct tb_port * in = tunnel - > src_port ;
2023-02-01 14:21:37 +03:00
if ( ! usb4_dp_port_bandwidth_mode_supported ( in ) )
2022-03-23 17:45:39 +03:00
return ;
2023-02-01 14:21:37 +03:00
if ( usb4_dp_port_bandwidth_mode_enabled ( in ) ) {
usb4_dp_port_set_cm_bandwidth_mode_supported ( in , false ) ;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel , " bandwidth allocation mode disabled \n " ) ;
2022-03-23 17:45:39 +03:00
}
}
2018-09-17 16:30:49 +03:00
static int tb_dp_activate ( struct tb_tunnel * tunnel , bool active )
{
int ret ;
if ( active ) {
struct tb_path * * paths ;
int last ;
paths = tunnel - > paths ;
last = paths [ TB_DP_VIDEO_PATH_OUT ] - > path_length - 1 ;
tb_dp_port_set_hops ( tunnel - > src_port ,
paths [ TB_DP_VIDEO_PATH_OUT ] - > hops [ 0 ] . in_hop_index ,
paths [ TB_DP_AUX_PATH_OUT ] - > hops [ 0 ] . in_hop_index ,
paths [ TB_DP_AUX_PATH_IN ] - > hops [ last ] . next_hop_index ) ;
tb_dp_port_set_hops ( tunnel - > dst_port ,
paths [ TB_DP_VIDEO_PATH_OUT ] - > hops [ last ] . next_hop_index ,
paths [ TB_DP_AUX_PATH_IN ] - > hops [ 0 ] . in_hop_index ,
paths [ TB_DP_AUX_PATH_OUT ] - > hops [ last ] . next_hop_index ) ;
} else {
tb_dp_port_hpd_clear ( tunnel - > src_port ) ;
tb_dp_port_set_hops ( tunnel - > src_port , 0 , 0 , 0 ) ;
if ( tb_port_is_dpout ( tunnel - > dst_port ) )
tb_dp_port_set_hops ( tunnel - > dst_port , 0 , 0 , 0 ) ;
}
ret = tb_dp_port_enable ( tunnel - > src_port , active ) ;
if ( ret )
return ret ;
if ( tb_port_is_dpout ( tunnel - > dst_port ) )
return tb_dp_port_enable ( tunnel - > dst_port , active ) ;
return 0 ;
}
2023-12-11 14:50:22 +03:00
/**
* tb_dp_bandwidth_mode_maximum_bandwidth ( ) - Maximum possible bandwidth
* @ tunnel : DP tunnel to check
* @ max_bw_rounded : Maximum bandwidth in Mb / s rounded up to the next granularity
*
* Returns maximum possible bandwidth for this tunnel in Mb / s .
*/
2023-01-23 11:42:58 +03:00
static int tb_dp_bandwidth_mode_maximum_bandwidth ( struct tb_tunnel * tunnel ,
2023-12-11 14:50:22 +03:00
int * max_bw_rounded )
2022-03-23 17:45:39 +03:00
{
struct tb_port * in = tunnel - > src_port ;
2023-12-11 14:50:22 +03:00
int ret , rate , lanes , max_bw ;
2023-01-23 11:42:58 +03:00
u32 cap ;
2022-03-23 17:45:39 +03:00
2023-01-23 11:42:58 +03:00
/*
* DP IN adapter DP_LOCAL_CAP gets updated to the lowest AUX
* read parameter values so this so we can use this to determine
* the maximum possible bandwidth over this link .
*
* See USB4 v2 spec 1.0 10.4 .4 .5 .
*/
ret = tb_port_read ( in , & cap , TB_CFG_PORT ,
in - > cap_adap + DP_LOCAL_CAP , 1 ) ;
2022-03-23 17:45:39 +03:00
if ( ret )
return ret ;
2023-01-23 11:42:58 +03:00
rate = tb_dp_cap_get_rate_ext ( cap ) ;
2023-12-11 14:50:22 +03:00
lanes = tb_dp_cap_get_lanes ( cap ) ;
2023-01-23 11:42:58 +03:00
2023-12-11 14:50:22 +03:00
max_bw = tb_dp_bandwidth ( rate , lanes ) ;
2022-03-23 17:45:39 +03:00
2023-12-11 14:50:22 +03:00
if ( max_bw_rounded ) {
2022-03-23 17:45:39 +03:00
ret = usb4_dp_port_granularity ( in ) ;
if ( ret < 0 )
return ret ;
2023-12-11 14:50:22 +03:00
* max_bw_rounded = roundup ( max_bw , ret ) ;
2022-03-23 17:45:39 +03:00
}
2023-12-11 14:50:22 +03:00
return max_bw ;
2022-03-23 17:45:39 +03:00
}
2023-02-01 14:21:37 +03:00
static int tb_dp_bandwidth_mode_consumed_bandwidth ( struct tb_tunnel * tunnel ,
int * consumed_up ,
int * consumed_down )
2022-03-23 17:45:39 +03:00
{
struct tb_port * out = tunnel - > dst_port ;
struct tb_port * in = tunnel - > src_port ;
2023-12-11 14:50:22 +03:00
int ret , allocated_bw , max_bw_rounded ;
2022-03-23 17:45:39 +03:00
2023-02-01 14:21:37 +03:00
if ( ! usb4_dp_port_bandwidth_mode_enabled ( in ) )
2022-03-23 17:45:39 +03:00
return - EOPNOTSUPP ;
if ( ! tunnel - > bw_mode )
return - EOPNOTSUPP ;
/* Read what was allocated previously if any */
2023-02-01 14:21:37 +03:00
ret = usb4_dp_port_allocated_bandwidth ( in ) ;
2022-03-23 17:45:39 +03:00
if ( ret < 0 )
return ret ;
allocated_bw = ret ;
2023-12-11 14:50:22 +03:00
ret = tb_dp_bandwidth_mode_maximum_bandwidth ( tunnel , & max_bw_rounded ) ;
2022-03-23 17:45:39 +03:00
if ( ret < 0 )
return ret ;
2023-12-11 14:50:22 +03:00
if ( allocated_bw = = max_bw_rounded )
2022-03-23 17:45:39 +03:00
allocated_bw = ret ;
2023-08-10 22:37:14 +03:00
if ( tb_port_path_direction_downstream ( in , out ) ) {
2022-03-23 17:45:39 +03:00
* consumed_up = 0 ;
* consumed_down = allocated_bw ;
} else {
* consumed_up = allocated_bw ;
* consumed_down = 0 ;
}
return 0 ;
}
static int tb_dp_allocated_bandwidth ( struct tb_tunnel * tunnel , int * allocated_up ,
int * allocated_down )
{
struct tb_port * out = tunnel - > dst_port ;
struct tb_port * in = tunnel - > src_port ;
/*
* If we have already set the allocated bandwidth then use that .
* Otherwise we read it from the DPRX .
*/
2023-02-01 14:21:37 +03:00
if ( usb4_dp_port_bandwidth_mode_enabled ( in ) & & tunnel - > bw_mode ) {
2023-12-11 14:50:22 +03:00
int ret , allocated_bw , max_bw_rounded ;
2022-03-23 17:45:39 +03:00
2023-02-01 14:21:37 +03:00
ret = usb4_dp_port_allocated_bandwidth ( in ) ;
2022-03-23 17:45:39 +03:00
if ( ret < 0 )
return ret ;
allocated_bw = ret ;
2023-12-11 14:50:22 +03:00
ret = tb_dp_bandwidth_mode_maximum_bandwidth ( tunnel ,
& max_bw_rounded ) ;
2022-03-23 17:45:39 +03:00
if ( ret < 0 )
return ret ;
2023-12-11 14:50:22 +03:00
if ( allocated_bw = = max_bw_rounded )
2022-03-23 17:45:39 +03:00
allocated_bw = ret ;
2023-08-10 22:37:14 +03:00
if ( tb_port_path_direction_downstream ( in , out ) ) {
2022-03-23 17:45:39 +03:00
* allocated_up = 0 ;
* allocated_down = allocated_bw ;
} else {
* allocated_up = allocated_bw ;
* allocated_down = 0 ;
}
return 0 ;
}
return tunnel - > consumed_bandwidth ( tunnel , allocated_up ,
allocated_down ) ;
}
static int tb_dp_alloc_bandwidth ( struct tb_tunnel * tunnel , int * alloc_up ,
int * alloc_down )
{
struct tb_port * out = tunnel - > dst_port ;
struct tb_port * in = tunnel - > src_port ;
2023-12-11 14:50:22 +03:00
int max_bw_rounded , ret , tmp ;
2022-03-23 17:45:39 +03:00
2023-02-01 14:21:37 +03:00
if ( ! usb4_dp_port_bandwidth_mode_enabled ( in ) )
2022-03-23 17:45:39 +03:00
return - EOPNOTSUPP ;
2023-12-11 14:50:22 +03:00
ret = tb_dp_bandwidth_mode_maximum_bandwidth ( tunnel , & max_bw_rounded ) ;
2022-03-23 17:45:39 +03:00
if ( ret < 0 )
return ret ;
2023-08-10 22:37:14 +03:00
if ( tb_port_path_direction_downstream ( in , out ) ) {
2023-12-11 14:50:22 +03:00
tmp = min ( * alloc_down , max_bw_rounded ) ;
2023-02-01 14:21:37 +03:00
ret = usb4_dp_port_allocate_bandwidth ( in , tmp ) ;
2022-03-23 17:45:39 +03:00
if ( ret )
return ret ;
* alloc_down = tmp ;
* alloc_up = 0 ;
} else {
2023-12-11 14:50:22 +03:00
tmp = min ( * alloc_up , max_bw_rounded ) ;
2023-02-01 14:21:37 +03:00
ret = usb4_dp_port_allocate_bandwidth ( in , tmp ) ;
2022-03-23 17:45:39 +03:00
if ( ret )
return ret ;
* alloc_down = 0 ;
* alloc_up = tmp ;
}
/* Now we can use BW mode registers to figure out the bandwidth */
/* TODO: need to handle discovery too */
tunnel - > bw_mode = true ;
return 0 ;
}
2023-11-23 14:39:21 +03:00
static int tb_dp_wait_dprx ( struct tb_tunnel * tunnel , int timeout_msec )
2022-03-23 17:45:39 +03:00
{
ktime_t timeout = ktime_add_ms ( ktime_get ( ) , timeout_msec ) ;
struct tb_port * in = tunnel - > src_port ;
/*
* Wait for DPRX done . Normally it should be already set for
* active tunnel .
*/
do {
u32 val ;
int ret ;
ret = tb_port_read ( in , & val , TB_CFG_PORT ,
in - > cap_adap + DP_COMMON_CAP , 1 ) ;
if ( ret )
return ret ;
if ( val & DP_COMMON_CAP_DPRX_DONE ) {
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel , " DPRX read done \n " ) ;
2022-03-23 17:45:39 +03:00
return 0 ;
}
usleep_range ( 100 , 150 ) ;
} while ( ktime_before ( ktime_get ( ) , timeout ) ) ;
2023-11-23 14:39:21 +03:00
tb_tunnel_dbg ( tunnel , " DPRX read timeout \n " ) ;
2022-03-23 17:45:39 +03:00
return - ETIMEDOUT ;
}
/* Read cap from tunnel DP IN */
static int tb_dp_read_cap ( struct tb_tunnel * tunnel , unsigned int cap , u32 * rate ,
u32 * lanes )
{
struct tb_port * in = tunnel - > src_port ;
u32 val ;
int ret ;
switch ( cap ) {
case DP_LOCAL_CAP :
case DP_REMOTE_CAP :
2023-11-23 14:39:21 +03:00
case DP_COMMON_CAP :
2022-03-23 17:45:39 +03:00
break ;
default :
tb_tunnel_WARN ( tunnel , " invalid capability index %#x \n " , cap ) ;
return - EINVAL ;
}
/*
* Read from the copied remote cap so that we take into account
* if capabilities were reduced during exchange .
*/
ret = tb_port_read ( in , & val , TB_CFG_PORT , in - > cap_adap + cap , 1 ) ;
if ( ret )
return ret ;
* rate = tb_dp_cap_get_rate ( val ) ;
* lanes = tb_dp_cap_get_lanes ( val ) ;
return 0 ;
}
static int tb_dp_maximum_bandwidth ( struct tb_tunnel * tunnel , int * max_up ,
int * max_down )
{
struct tb_port * in = tunnel - > src_port ;
int ret ;
2023-01-23 11:42:58 +03:00
if ( ! usb4_dp_port_bandwidth_mode_enabled ( in ) )
return - EOPNOTSUPP ;
ret = tb_dp_bandwidth_mode_maximum_bandwidth ( tunnel , NULL ) ;
if ( ret < 0 )
2022-03-23 17:45:39 +03:00
return ret ;
2023-08-10 22:37:14 +03:00
if ( tb_port_path_direction_downstream ( in , tunnel - > dst_port ) ) {
2022-03-23 17:45:39 +03:00
* max_up = 0 ;
2023-01-23 11:42:58 +03:00
* max_down = ret ;
2022-03-23 17:45:39 +03:00
} else {
2023-01-23 11:42:58 +03:00
* max_up = ret ;
2022-03-23 17:45:39 +03:00
* max_down = 0 ;
}
return 0 ;
}
2020-03-28 13:52:31 +03:00
static int tb_dp_consumed_bandwidth ( struct tb_tunnel * tunnel , int * consumed_up ,
int * consumed_down )
2019-03-26 16:03:48 +03:00
{
struct tb_port * in = tunnel - > src_port ;
const struct tb_switch * sw = in - > sw ;
2022-03-23 17:45:39 +03:00
u32 rate = 0 , lanes = 0 ;
2019-03-26 16:03:48 +03:00
int ret ;
2019-12-17 15:33:40 +03:00
if ( tb_dp_is_usb4 ( sw ) ) {
2019-03-26 16:03:48 +03:00
/*
2022-03-23 17:45:39 +03:00
* On USB4 routers check if the bandwidth allocation
* mode is enabled first and then read the bandwidth
* through those registers .
2019-03-26 16:03:48 +03:00
*/
2023-02-01 14:21:37 +03:00
ret = tb_dp_bandwidth_mode_consumed_bandwidth ( tunnel , consumed_up ,
consumed_down ) ;
2022-03-23 17:45:39 +03:00
if ( ret < 0 ) {
if ( ret ! = - EOPNOTSUPP )
2019-03-26 16:03:48 +03:00
return ret ;
2022-03-23 17:45:39 +03:00
} else if ( ! ret ) {
return 0 ;
}
2019-03-26 16:03:48 +03:00
/*
2022-03-23 17:45:39 +03:00
* Then see if the DPRX negotiation is ready and if yes
* return that bandwidth ( it may be smaller than the
* reduced one ) . Otherwise return the remote ( possibly
* reduced ) caps .
2019-03-26 16:03:48 +03:00
*/
2023-11-23 14:39:21 +03:00
ret = tb_dp_wait_dprx ( tunnel , 150 ) ;
2022-03-23 17:45:39 +03:00
if ( ret ) {
if ( ret = = - ETIMEDOUT )
ret = tb_dp_read_cap ( tunnel , DP_REMOTE_CAP ,
& rate , & lanes ) ;
if ( ret )
return ret ;
}
2023-11-23 14:39:21 +03:00
ret = tb_dp_read_cap ( tunnel , DP_COMMON_CAP , & rate , & lanes ) ;
if ( ret )
return ret ;
2022-03-23 17:45:39 +03:00
} else if ( sw - > generation > = 2 ) {
ret = tb_dp_read_cap ( tunnel , DP_REMOTE_CAP , & rate , & lanes ) ;
2019-03-26 16:03:48 +03:00
if ( ret )
return ret ;
} else {
/* No bandwidth management for legacy devices */
2020-03-28 13:52:31 +03:00
* consumed_up = 0 ;
* consumed_down = 0 ;
2019-03-26 16:03:48 +03:00
return 0 ;
}
2023-08-10 22:37:14 +03:00
if ( tb_port_path_direction_downstream ( in , tunnel - > dst_port ) ) {
2020-03-28 13:52:31 +03:00
* consumed_up = 0 ;
* consumed_down = tb_dp_bandwidth ( rate , lanes ) ;
} else {
* consumed_up = tb_dp_bandwidth ( rate , lanes ) ;
* consumed_down = 0 ;
}
return 0 ;
2019-03-26 16:03:48 +03:00
}
2021-03-22 19:09:35 +03:00
static void tb_dp_init_aux_credits ( struct tb_path_hop * hop )
{
struct tb_port * port = hop - > in_port ;
struct tb_switch * sw = port - > sw ;
if ( tb_port_use_credit_allocation ( port ) )
hop - > initial_credits = sw - > min_dp_aux_credits ;
else
hop - > initial_credits = 1 ;
}
2023-09-08 12:07:47 +03:00
static void tb_dp_init_aux_path ( struct tb_path * path , bool pm_support )
2018-09-17 16:30:49 +03:00
{
2021-03-22 19:09:35 +03:00
struct tb_path_hop * hop ;
2018-09-17 16:30:49 +03:00
path - > egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL ;
path - > egress_shared_buffer = TB_PATH_NONE ;
path - > ingress_fc_enable = TB_PATH_ALL ;
path - > ingress_shared_buffer = TB_PATH_NONE ;
2023-07-31 05:25:37 +03:00
path - > priority = TB_DP_AUX_PRIORITY ;
path - > weight = TB_DP_AUX_WEIGHT ;
2018-09-17 16:30:49 +03:00
2023-09-08 12:07:47 +03:00
tb_path_for_each_hop ( path , hop ) {
2021-03-22 19:09:35 +03:00
tb_dp_init_aux_credits ( hop ) ;
2023-09-08 12:07:47 +03:00
if ( pm_support )
tb_init_pm_support ( hop ) ;
}
2018-09-17 16:30:49 +03:00
}
2021-03-22 19:09:35 +03:00
static int tb_dp_init_video_credits ( struct tb_path_hop * hop )
2018-09-17 16:30:49 +03:00
{
2021-03-22 19:09:35 +03:00
struct tb_port * port = hop - > in_port ;
struct tb_switch * sw = port - > sw ;
if ( tb_port_use_credit_allocation ( port ) ) {
unsigned int nfc_credits ;
size_t max_dp_streams ;
tb_available_credits ( port , & max_dp_streams ) ;
/*
* Read the number of currently allocated NFC credits
* from the lane adapter . Since we only use them for DP
* tunneling we can use that to figure out how many DP
* tunnels already go through the lane adapter .
*/
nfc_credits = port - > config . nfc_credits &
ADP_CS_4_NFC_BUFFERS_MASK ;
if ( nfc_credits / sw - > min_dp_main_credits > max_dp_streams )
return - ENOSPC ;
hop - > nfc_credits = sw - > min_dp_main_credits ;
} else {
hop - > nfc_credits = min ( port - > total_credits - 2 , 12U ) ;
}
return 0 ;
}
2023-09-08 12:07:47 +03:00
static int tb_dp_init_video_path ( struct tb_path * path , bool pm_support )
2021-03-22 19:09:35 +03:00
{
struct tb_path_hop * hop ;
2018-09-17 16:30:49 +03:00
path - > egress_fc_enable = TB_PATH_NONE ;
path - > egress_shared_buffer = TB_PATH_NONE ;
path - > ingress_fc_enable = TB_PATH_NONE ;
path - > ingress_shared_buffer = TB_PATH_NONE ;
2023-07-31 05:25:37 +03:00
path - > priority = TB_DP_VIDEO_PRIORITY ;
path - > weight = TB_DP_VIDEO_WEIGHT ;
2018-09-17 16:30:49 +03:00
2021-03-22 19:09:35 +03:00
tb_path_for_each_hop ( path , hop ) {
int ret ;
2020-12-10 17:07:59 +03:00
2021-03-22 19:09:35 +03:00
ret = tb_dp_init_video_credits ( hop ) ;
if ( ret )
return ret ;
2023-09-08 12:07:47 +03:00
if ( pm_support )
tb_init_pm_support ( hop ) ;
2018-09-17 16:30:49 +03:00
}
2021-03-22 19:09:35 +03:00
return 0 ;
2018-09-17 16:30:49 +03:00
}
2023-05-24 14:03:55 +03:00
static void tb_dp_dump ( struct tb_tunnel * tunnel )
{
struct tb_port * in , * out ;
u32 dp_cap , rate , lanes ;
in = tunnel - > src_port ;
out = tunnel - > dst_port ;
if ( tb_port_read ( in , & dp_cap , TB_CFG_PORT ,
in - > cap_adap + DP_LOCAL_CAP , 1 ) )
return ;
rate = tb_dp_cap_get_rate ( dp_cap ) ;
lanes = tb_dp_cap_get_lanes ( dp_cap ) ;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel ,
" DP IN maximum supported bandwidth %u Mb/s x%u = %u Mb/s \n " ,
rate , lanes , tb_dp_bandwidth ( rate , lanes ) ) ;
2023-05-24 14:03:55 +03:00
if ( tb_port_read ( out , & dp_cap , TB_CFG_PORT ,
out - > cap_adap + DP_LOCAL_CAP , 1 ) )
return ;
rate = tb_dp_cap_get_rate ( dp_cap ) ;
lanes = tb_dp_cap_get_lanes ( dp_cap ) ;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel ,
" DP OUT maximum supported bandwidth %u Mb/s x%u = %u Mb/s \n " ,
rate , lanes , tb_dp_bandwidth ( rate , lanes ) ) ;
2023-05-24 14:03:55 +03:00
if ( tb_port_read ( in , & dp_cap , TB_CFG_PORT ,
in - > cap_adap + DP_REMOTE_CAP , 1 ) )
return ;
rate = tb_dp_cap_get_rate ( dp_cap ) ;
lanes = tb_dp_cap_get_lanes ( dp_cap ) ;
2023-09-19 15:03:58 +03:00
tb_tunnel_dbg ( tunnel , " reduced bandwidth %u Mb/s x%u = %u Mb/s \n " ,
rate , lanes , tb_dp_bandwidth ( rate , lanes ) ) ;
2023-05-24 14:03:55 +03:00
}
2018-09-17 16:30:49 +03:00
/**
* tb_tunnel_discover_dp ( ) - Discover existing Display Port tunnels
* @ tb : Pointer to the domain structure
* @ in : DP in adapter
2021-11-14 18:20:59 +03:00
* @ alloc_hopid : Allocate HopIDs from visited ports
2018-09-17 16:30:49 +03:00
*
* If @ in adapter is active , follows the tunnel to the DP out adapter
* and back . Returns the discovered tunnel or % NULL if there was no
* tunnel .
*
* Return : DP tunnel or % NULL if no tunnel found .
*/
2021-11-14 18:20:59 +03:00
struct tb_tunnel * tb_tunnel_discover_dp ( struct tb * tb , struct tb_port * in ,
bool alloc_hopid )
2018-09-17 16:30:49 +03:00
{
struct tb_tunnel * tunnel ;
struct tb_port * port ;
struct tb_path * path ;
if ( ! tb_dp_port_is_enabled ( in ) )
return NULL ;
tunnel = tb_tunnel_alloc ( tb , 3 , TB_TUNNEL_DP ) ;
if ( ! tunnel )
return NULL ;
2022-03-23 17:45:39 +03:00
tunnel - > init = tb_dp_init ;
tunnel - > deinit = tb_dp_deinit ;
2018-09-17 16:30:49 +03:00
tunnel - > activate = tb_dp_activate ;
2022-03-23 17:45:39 +03:00
tunnel - > maximum_bandwidth = tb_dp_maximum_bandwidth ;
tunnel - > allocated_bandwidth = tb_dp_allocated_bandwidth ;
tunnel - > alloc_bandwidth = tb_dp_alloc_bandwidth ;
2019-03-26 16:03:48 +03:00
tunnel - > consumed_bandwidth = tb_dp_consumed_bandwidth ;
2018-09-17 16:30:49 +03:00
tunnel - > src_port = in ;
path = tb_path_discover ( in , TB_DP_VIDEO_HOPID , NULL , - 1 ,
2021-11-14 18:20:59 +03:00
& tunnel - > dst_port , " Video " , alloc_hopid ) ;
2018-09-17 16:30:49 +03:00
if ( ! path ) {
/* Just disable the DP IN port */
tb_dp_port_enable ( in , false ) ;
goto err_free ;
}
tunnel - > paths [ TB_DP_VIDEO_PATH_OUT ] = path ;
2023-09-08 12:07:47 +03:00
if ( tb_dp_init_video_path ( tunnel - > paths [ TB_DP_VIDEO_PATH_OUT ] , false ) )
2021-03-22 19:09:35 +03:00
goto err_free ;
2018-09-17 16:30:49 +03:00
2021-11-14 18:20:59 +03:00
path = tb_path_discover ( in , TB_DP_AUX_TX_HOPID , NULL , - 1 , NULL , " AUX TX " ,
alloc_hopid ) ;
2018-09-17 16:30:49 +03:00
if ( ! path )
goto err_deactivate ;
tunnel - > paths [ TB_DP_AUX_PATH_OUT ] = path ;
2023-09-08 12:07:47 +03:00
tb_dp_init_aux_path ( tunnel - > paths [ TB_DP_AUX_PATH_OUT ] , false ) ;
2018-09-17 16:30:49 +03:00
path = tb_path_discover ( tunnel - > dst_port , - 1 , in , TB_DP_AUX_RX_HOPID ,
2021-11-14 18:20:59 +03:00
& port , " AUX RX " , alloc_hopid ) ;
2018-09-17 16:30:49 +03:00
if ( ! path )
goto err_deactivate ;
tunnel - > paths [ TB_DP_AUX_PATH_IN ] = path ;
2023-09-08 12:07:47 +03:00
tb_dp_init_aux_path ( tunnel - > paths [ TB_DP_AUX_PATH_IN ] , false ) ;
2018-09-17 16:30:49 +03:00
/* Validate that the tunnel is complete */
if ( ! tb_port_is_dpout ( tunnel - > dst_port ) ) {
tb_port_warn ( in , " path does not end on a DP adapter, cleaning up \n " ) ;
goto err_deactivate ;
}
if ( ! tb_dp_port_is_enabled ( tunnel - > dst_port ) )
goto err_deactivate ;
if ( ! tb_dp_port_hpd_is_active ( tunnel - > dst_port ) )
goto err_deactivate ;
if ( port ! = tunnel - > src_port ) {
tb_tunnel_warn ( tunnel , " path is not complete, cleaning up \n " ) ;
goto err_deactivate ;
}
2023-05-24 14:03:55 +03:00
tb_dp_dump ( tunnel ) ;
2018-09-17 16:30:49 +03:00
tb_tunnel_dbg ( tunnel , " discovered \n " ) ;
return tunnel ;
err_deactivate :
tb_tunnel_deactivate ( tunnel ) ;
err_free :
tb_tunnel_free ( tunnel ) ;
return NULL ;
}
/**
* tb_tunnel_alloc_dp ( ) - allocate a Display Port tunnel
* @ tb : Pointer to the domain structure
* @ in : DP in adapter port
* @ out : DP out adapter port
2022-04-01 17:24:28 +03:00
* @ link_nr : Preferred lane adapter when the link is not bonded
2020-03-24 15:44:13 +03:00
* @ max_up : Maximum available upstream bandwidth for the DP tunnel ( % 0
* if not limited )
* @ max_down : Maximum available downstream bandwidth for the DP tunnel
* ( % 0 if not limited )
2018-09-17 16:30:49 +03:00
*
* Allocates a tunnel between @ in and @ out that is capable of tunneling
* Display Port traffic .
*
* Return : Returns a tb_tunnel on success or NULL on failure .
*/
struct tb_tunnel * tb_tunnel_alloc_dp ( struct tb * tb , struct tb_port * in ,
2022-04-01 17:24:28 +03:00
struct tb_port * out , int link_nr ,
int max_up , int max_down )
2018-09-17 16:30:49 +03:00
{
struct tb_tunnel * tunnel ;
struct tb_path * * paths ;
struct tb_path * path ;
2023-09-08 12:07:47 +03:00
bool pm_support ;
2018-09-17 16:30:49 +03:00
if ( WARN_ON ( ! in - > cap_adap | | ! out - > cap_adap ) )
return NULL ;
tunnel = tb_tunnel_alloc ( tb , 3 , TB_TUNNEL_DP ) ;
if ( ! tunnel )
return NULL ;
2022-03-23 17:45:39 +03:00
tunnel - > init = tb_dp_init ;
tunnel - > deinit = tb_dp_deinit ;
2018-09-17 16:30:49 +03:00
tunnel - > activate = tb_dp_activate ;
2022-03-23 17:45:39 +03:00
tunnel - > maximum_bandwidth = tb_dp_maximum_bandwidth ;
tunnel - > allocated_bandwidth = tb_dp_allocated_bandwidth ;
tunnel - > alloc_bandwidth = tb_dp_alloc_bandwidth ;
2019-03-26 16:03:48 +03:00
tunnel - > consumed_bandwidth = tb_dp_consumed_bandwidth ;
2018-09-17 16:30:49 +03:00
tunnel - > src_port = in ;
tunnel - > dst_port = out ;
2020-03-24 15:44:13 +03:00
tunnel - > max_up = max_up ;
tunnel - > max_down = max_down ;
2018-09-17 16:30:49 +03:00
paths = tunnel - > paths ;
2023-09-08 12:07:47 +03:00
pm_support = usb4_switch_version ( in - > sw ) > = 2 ;
2018-09-17 16:30:49 +03:00
path = tb_path_alloc ( tb , in , TB_DP_VIDEO_HOPID , out , TB_DP_VIDEO_HOPID ,
2022-04-01 17:24:28 +03:00
link_nr , " Video " ) ;
2018-09-17 16:30:49 +03:00
if ( ! path )
goto err_free ;
2023-09-08 12:07:47 +03:00
tb_dp_init_video_path ( path , pm_support ) ;
2018-09-17 16:30:49 +03:00
paths [ TB_DP_VIDEO_PATH_OUT ] = path ;
path = tb_path_alloc ( tb , in , TB_DP_AUX_TX_HOPID , out ,
2022-04-01 17:24:28 +03:00
TB_DP_AUX_TX_HOPID , link_nr , " AUX TX " ) ;
2018-09-17 16:30:49 +03:00
if ( ! path )
goto err_free ;
2023-09-08 12:07:47 +03:00
tb_dp_init_aux_path ( path , pm_support ) ;
2018-09-17 16:30:49 +03:00
paths [ TB_DP_AUX_PATH_OUT ] = path ;
path = tb_path_alloc ( tb , out , TB_DP_AUX_RX_HOPID , in ,
2022-04-01 17:24:28 +03:00
TB_DP_AUX_RX_HOPID , link_nr , " AUX RX " ) ;
2018-09-17 16:30:49 +03:00
if ( ! path )
goto err_free ;
2023-09-08 12:07:47 +03:00
tb_dp_init_aux_path ( path , pm_support ) ;
2018-09-17 16:30:49 +03:00
paths [ TB_DP_AUX_PATH_IN ] = path ;
return tunnel ;
err_free :
tb_tunnel_free ( tunnel ) ;
return NULL ;
}
2021-03-22 19:09:35 +03:00
static unsigned int tb_dma_available_credits ( const struct tb_port * port )
2018-09-28 16:35:32 +03:00
{
2021-03-22 19:09:35 +03:00
const struct tb_switch * sw = port - > sw ;
int credits ;
2018-09-28 16:35:32 +03:00
2021-03-22 19:09:35 +03:00
credits = tb_available_credits ( port , NULL ) ;
if ( tb_acpi_may_tunnel_pcie ( ) )
credits - = sw - > max_pcie_credits ;
credits - = port - > dma_credits ;
return credits > 0 ? credits : 0 ;
2018-09-28 16:35:32 +03:00
}
2021-03-22 19:09:35 +03:00
static int tb_dma_reserve_credits ( struct tb_path_hop * hop , unsigned int credits )
2018-09-28 16:35:32 +03:00
{
2021-03-22 19:09:35 +03:00
struct tb_port * port = hop - > in_port ;
if ( tb_port_use_credit_allocation ( port ) ) {
unsigned int available = tb_dma_available_credits ( port ) ;
/*
* Need to have at least TB_MIN_DMA_CREDITS , otherwise
* DMA path cannot be established .
*/
if ( available < TB_MIN_DMA_CREDITS )
return - ENOSPC ;
while ( credits > available )
credits - - ;
tb_port_dbg ( port , " reserving %u credits for DMA path \n " ,
credits ) ;
port - > dma_credits + = credits ;
} else {
if ( tb_port_is_null ( port ) )
credits = port - > bonded ? 14 : 6 ;
else
credits = min ( port - > total_credits , credits ) ;
}
2018-09-28 16:35:32 +03:00
2021-03-22 19:09:35 +03:00
hop - > initial_credits = credits ;
return 0 ;
}
/* Path from lane adapter to NHI */
static int tb_dma_init_rx_path ( struct tb_path * path , unsigned int credits )
{
struct tb_path_hop * hop ;
unsigned int i , tmp ;
path - > egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL ;
2018-09-28 16:35:32 +03:00
path - > ingress_fc_enable = TB_PATH_ALL ;
path - > egress_shared_buffer = TB_PATH_NONE ;
2021-01-14 20:27:58 +03:00
path - > ingress_shared_buffer = TB_PATH_NONE ;
2023-07-31 05:25:37 +03:00
path - > priority = TB_DMA_PRIORITY ;
path - > weight = TB_DMA_WEIGHT ;
2018-09-28 16:35:32 +03:00
path - > clear_fc = true ;
2021-03-22 19:09:35 +03:00
/*
* First lane adapter is the one connected to the remote host .
* We don ' t tunnel other traffic over this link so can use all
* the credits ( except the ones reserved for control traffic ) .
*/
hop = & path - > hops [ 0 ] ;
tmp = min ( tb_usable_credits ( hop - > in_port ) , credits ) ;
hop - > initial_credits = tmp ;
hop - > in_port - > dma_credits + = tmp ;
for ( i = 1 ; i < path - > path_length ; i + + ) {
int ret ;
ret = tb_dma_reserve_credits ( & path - > hops [ i ] , credits ) ;
if ( ret )
return ret ;
}
return 0 ;
}
/* Path from NHI to lane adapter */
static int tb_dma_init_tx_path ( struct tb_path * path , unsigned int credits )
{
struct tb_path_hop * hop ;
path - > egress_fc_enable = TB_PATH_ALL ;
path - > ingress_fc_enable = TB_PATH_ALL ;
path - > egress_shared_buffer = TB_PATH_NONE ;
path - > ingress_shared_buffer = TB_PATH_NONE ;
2023-07-31 05:25:37 +03:00
path - > priority = TB_DMA_PRIORITY ;
path - > weight = TB_DMA_WEIGHT ;
2021-03-22 19:09:35 +03:00
path - > clear_fc = true ;
tb_path_for_each_hop ( path , hop ) {
int ret ;
ret = tb_dma_reserve_credits ( hop , credits ) ;
if ( ret )
return ret ;
}
return 0 ;
}
static void tb_dma_release_credits ( struct tb_path_hop * hop )
{
struct tb_port * port = hop - > in_port ;
if ( tb_port_use_credit_allocation ( port ) ) {
port - > dma_credits - = hop - > initial_credits ;
tb_port_dbg ( port , " released %u DMA path credits \n " ,
hop - > initial_credits ) ;
}
}
static void tb_dma_deinit_path ( struct tb_path * path )
{
struct tb_path_hop * hop ;
tb_path_for_each_hop ( path , hop )
tb_dma_release_credits ( hop ) ;
}
static void tb_dma_deinit ( struct tb_tunnel * tunnel )
{
int i ;
for ( i = 0 ; i < tunnel - > npaths ; i + + ) {
if ( ! tunnel - > paths [ i ] )
continue ;
tb_dma_deinit_path ( tunnel - > paths [ i ] ) ;
}
2018-09-28 16:35:32 +03:00
}
/**
* tb_tunnel_alloc_dma ( ) - allocate a DMA tunnel
* @ tb : Pointer to the domain structure
* @ nhi : Host controller port
* @ dst : Destination null port which the other domain is connected to
* @ transmit_path : HopID used for transmitting packets
2021-01-08 17:25:39 +03:00
* @ transmit_ring : NHI ring number used to send packets towards the
* other domain . Set to % - 1 if TX path is not needed .
2021-01-27 14:25:53 +03:00
* @ receive_path : HopID used for receiving packets
2021-01-08 17:25:39 +03:00
* @ receive_ring : NHI ring number used to receive packets from the
* other domain . Set to % - 1 if RX path is not needed .
2018-09-28 16:35:32 +03:00
*
* Return : Returns a tb_tunnel on success or NULL on failure .
*/
struct tb_tunnel * tb_tunnel_alloc_dma ( struct tb * tb , struct tb_port * nhi ,
2021-01-08 17:25:39 +03:00
struct tb_port * dst , int transmit_path ,
int transmit_ring , int receive_path ,
int receive_ring )
2018-09-28 16:35:32 +03:00
{
struct tb_tunnel * tunnel ;
2020-10-07 18:17:12 +03:00
size_t npaths = 0 , i = 0 ;
2018-09-28 16:35:32 +03:00
struct tb_path * path ;
2021-03-22 19:09:35 +03:00
int credits ;
2018-09-28 16:35:32 +03:00
2023-03-31 13:01:32 +03:00
/* Ring 0 is reserved for control channel */
if ( WARN_ON ( ! receive_ring | | ! transmit_ring ) )
return NULL ;
2021-01-08 17:25:39 +03:00
if ( receive_ring > 0 )
2020-10-07 18:17:12 +03:00
npaths + + ;
2021-01-08 17:25:39 +03:00
if ( transmit_ring > 0 )
2020-10-07 18:17:12 +03:00
npaths + + ;
if ( WARN_ON ( ! npaths ) )
return NULL ;
tunnel = tb_tunnel_alloc ( tb , npaths , TB_TUNNEL_DMA ) ;
2018-09-28 16:35:32 +03:00
if ( ! tunnel )
return NULL ;
tunnel - > src_port = nhi ;
tunnel - > dst_port = dst ;
2021-03-22 19:09:35 +03:00
tunnel - > deinit = tb_dma_deinit ;
2018-09-28 16:35:32 +03:00
2022-09-08 13:17:26 +03:00
credits = min_not_zero ( dma_credits , nhi - > sw - > max_dma_credits ) ;
2018-09-28 16:35:32 +03:00
2021-01-08 17:25:39 +03:00
if ( receive_ring > 0 ) {
2020-10-07 18:17:12 +03:00
path = tb_path_alloc ( tb , dst , receive_path , nhi , receive_ring , 0 ,
" DMA RX " ) ;
2021-03-22 19:09:35 +03:00
if ( ! path )
goto err_free ;
2020-10-07 18:17:12 +03:00
tunnel - > paths [ i + + ] = path ;
2021-03-22 19:09:35 +03:00
if ( tb_dma_init_rx_path ( path , credits ) ) {
tb_tunnel_dbg ( tunnel , " not enough buffers for RX path \n " ) ;
goto err_free ;
}
2018-09-28 16:35:32 +03:00
}
2021-01-08 17:25:39 +03:00
if ( transmit_ring > 0 ) {
2020-10-07 18:17:12 +03:00
path = tb_path_alloc ( tb , nhi , transmit_ring , dst , transmit_path , 0 ,
" DMA TX " ) ;
2021-03-22 19:09:35 +03:00
if ( ! path )
goto err_free ;
2020-10-07 18:17:12 +03:00
tunnel - > paths [ i + + ] = path ;
2021-03-22 19:09:35 +03:00
if ( tb_dma_init_tx_path ( path , credits ) ) {
tb_tunnel_dbg ( tunnel , " not enough buffers for TX path \n " ) ;
goto err_free ;
}
2018-09-28 16:35:32 +03:00
}
return tunnel ;
2021-03-22 19:09:35 +03:00
err_free :
tb_tunnel_free ( tunnel ) ;
return NULL ;
2018-09-28 16:35:32 +03:00
}
2021-01-08 17:25:39 +03:00
/**
* tb_tunnel_match_dma ( ) - Match DMA tunnel
* @ tunnel : Tunnel to match
* @ transmit_path : HopID used for transmitting packets . Pass % - 1 to ignore .
* @ transmit_ring : NHI ring number used to send packets towards the
* other domain . Pass % - 1 to ignore .
* @ receive_path : HopID used for receiving packets . Pass % - 1 to ignore .
* @ receive_ring : NHI ring number used to receive packets from the
* other domain . Pass % - 1 to ignore .
*
* This function can be used to match specific DMA tunnel , if there are
* multiple DMA tunnels going through the same XDomain connection .
* Returns true if there is match and false otherwise .
*/
bool tb_tunnel_match_dma ( const struct tb_tunnel * tunnel , int transmit_path ,
int transmit_ring , int receive_path , int receive_ring )
{
const struct tb_path * tx_path = NULL , * rx_path = NULL ;
int i ;
if ( ! receive_ring | | ! transmit_ring )
return false ;
for ( i = 0 ; i < tunnel - > npaths ; i + + ) {
const struct tb_path * path = tunnel - > paths [ i ] ;
if ( ! path )
continue ;
if ( tb_port_is_nhi ( path - > hops [ 0 ] . in_port ) )
tx_path = path ;
else if ( tb_port_is_nhi ( path - > hops [ path - > path_length - 1 ] . out_port ) )
rx_path = path ;
}
if ( transmit_ring > 0 | | transmit_path > 0 ) {
if ( ! tx_path )
return false ;
if ( transmit_ring > 0 & &
( tx_path - > hops [ 0 ] . in_hop_index ! = transmit_ring ) )
return false ;
if ( transmit_path > 0 & &
( tx_path - > hops [ tx_path - > path_length - 1 ] . next_hop_index ! = transmit_path ) )
return false ;
}
if ( receive_ring > 0 | | receive_path > 0 ) {
if ( ! rx_path )
return false ;
if ( receive_path > 0 & &
( rx_path - > hops [ 0 ] . in_hop_index ! = receive_path ) )
return false ;
if ( receive_ring > 0 & &
( rx_path - > hops [ rx_path - > path_length - 1 ] . next_hop_index ! = receive_ring ) )
return false ;
}
return true ;
}
2020-03-24 15:44:13 +03:00
static int tb_usb3_max_link_rate ( struct tb_port * up , struct tb_port * down )
{
int ret , up_max_rate , down_max_rate ;
ret = usb4_usb3_port_max_link_rate ( up ) ;
if ( ret < 0 )
return ret ;
up_max_rate = ret ;
ret = usb4_usb3_port_max_link_rate ( down ) ;
if ( ret < 0 )
return ret ;
down_max_rate = ret ;
return min ( up_max_rate , down_max_rate ) ;
}
static int tb_usb3_init ( struct tb_tunnel * tunnel )
{
tb_tunnel_dbg ( tunnel , " allocating initial bandwidth %d/%d Mb/s \n " ,
tunnel - > allocated_up , tunnel - > allocated_down ) ;
return usb4_usb3_port_allocate_bandwidth ( tunnel - > src_port ,
& tunnel - > allocated_up ,
& tunnel - > allocated_down ) ;
}
2019-12-17 15:33:44 +03:00
static int tb_usb3_activate ( struct tb_tunnel * tunnel , bool activate )
{
int res ;
res = tb_usb3_port_enable ( tunnel - > src_port , activate ) ;
if ( res )
return res ;
if ( tb_port_is_usb3_up ( tunnel - > dst_port ) )
return tb_usb3_port_enable ( tunnel - > dst_port , activate ) ;
return 0 ;
}
2020-03-24 15:44:13 +03:00
static int tb_usb3_consumed_bandwidth ( struct tb_tunnel * tunnel ,
int * consumed_up , int * consumed_down )
{
2023-07-31 05:25:40 +03:00
struct tb_port * port = tb_upstream_port ( tunnel - > dst_port - > sw ) ;
2023-07-31 05:25:38 +03:00
int pcie_weight = tb_acpi_may_tunnel_pcie ( ) ? TB_PCI_WEIGHT : 0 ;
2020-02-18 17:14:42 +03:00
2020-03-24 15:44:13 +03:00
/*
2020-02-18 17:14:42 +03:00
* PCIe tunneling , if enabled , affects the USB3 bandwidth so
* take that it into account here .
2020-03-24 15:44:13 +03:00
*/
2023-07-31 05:25:38 +03:00
* consumed_up = tunnel - > allocated_up *
( TB_USB3_WEIGHT + pcie_weight ) / TB_USB3_WEIGHT ;
* consumed_down = tunnel - > allocated_down *
( TB_USB3_WEIGHT + pcie_weight ) / TB_USB3_WEIGHT ;
2023-07-31 05:25:40 +03:00
if ( tb_port_get_link_generation ( port ) > = 4 ) {
* consumed_up = max ( * consumed_up , USB4_V2_USB3_MIN_BANDWIDTH ) ;
* consumed_down = max ( * consumed_down , USB4_V2_USB3_MIN_BANDWIDTH ) ;
}
2020-03-24 15:44:13 +03:00
return 0 ;
}
static int tb_usb3_release_unused_bandwidth ( struct tb_tunnel * tunnel )
{
int ret ;
ret = usb4_usb3_port_release_bandwidth ( tunnel - > src_port ,
& tunnel - > allocated_up ,
& tunnel - > allocated_down ) ;
if ( ret )
return ret ;
tb_tunnel_dbg ( tunnel , " decreased bandwidth allocation to %d/%d Mb/s \n " ,
tunnel - > allocated_up , tunnel - > allocated_down ) ;
return 0 ;
}
static void tb_usb3_reclaim_available_bandwidth ( struct tb_tunnel * tunnel ,
int * available_up ,
int * available_down )
{
int ret , max_rate , allocate_up , allocate_down ;
2023-08-31 14:15:26 +03:00
ret = tb_usb3_max_link_rate ( tunnel - > dst_port , tunnel - > src_port ) ;
2020-06-24 21:45:03 +03:00
if ( ret < 0 ) {
2023-08-31 14:15:26 +03:00
tb_tunnel_warn ( tunnel , " failed to read maximum link rate \n " ) ;
2020-03-24 15:44:13 +03:00
return ;
}
2020-06-24 21:45:03 +03:00
2020-03-24 15:44:13 +03:00
/*
* 90 % of the max rate can be allocated for isochronous
* transfers .
*/
max_rate = ret * 90 / 100 ;
/* No need to reclaim if already at maximum */
if ( tunnel - > allocated_up > = max_rate & &
tunnel - > allocated_down > = max_rate )
return ;
/* Don't go lower than what is already allocated */
allocate_up = min ( max_rate , * available_up ) ;
if ( allocate_up < tunnel - > allocated_up )
allocate_up = tunnel - > allocated_up ;
allocate_down = min ( max_rate , * available_down ) ;
if ( allocate_down < tunnel - > allocated_down )
allocate_down = tunnel - > allocated_down ;
/* If no changes no need to do more */
if ( allocate_up = = tunnel - > allocated_up & &
allocate_down = = tunnel - > allocated_down )
return ;
ret = usb4_usb3_port_allocate_bandwidth ( tunnel - > src_port , & allocate_up ,
& allocate_down ) ;
if ( ret ) {
tb_tunnel_info ( tunnel , " failed to allocate bandwidth \n " ) ;
return ;
}
tunnel - > allocated_up = allocate_up ;
* available_up - = tunnel - > allocated_up ;
tunnel - > allocated_down = allocate_down ;
* available_down - = tunnel - > allocated_down ;
tb_tunnel_dbg ( tunnel , " increased bandwidth allocation to %d/%d Mb/s \n " ,
tunnel - > allocated_up , tunnel - > allocated_down ) ;
}
2021-03-22 19:09:35 +03:00
static void tb_usb3_init_credits ( struct tb_path_hop * hop )
{
struct tb_port * port = hop - > in_port ;
struct tb_switch * sw = port - > sw ;
unsigned int credits ;
if ( tb_port_use_credit_allocation ( port ) ) {
credits = sw - > max_usb3_credits ;
} else {
if ( tb_port_is_null ( port ) )
credits = port - > bonded ? 32 : 16 ;
else
credits = 7 ;
}
hop - > initial_credits = credits ;
}
2019-12-17 15:33:44 +03:00
static void tb_usb3_init_path ( struct tb_path * path )
{
2021-03-22 19:09:35 +03:00
struct tb_path_hop * hop ;
2019-12-17 15:33:44 +03:00
path - > egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL ;
path - > egress_shared_buffer = TB_PATH_NONE ;
path - > ingress_fc_enable = TB_PATH_ALL ;
path - > ingress_shared_buffer = TB_PATH_NONE ;
2023-07-31 05:25:37 +03:00
path - > priority = TB_USB3_PRIORITY ;
path - > weight = TB_USB3_WEIGHT ;
2019-12-17 15:33:44 +03:00
path - > drop_packages = 0 ;
2021-03-22 19:09:35 +03:00
tb_path_for_each_hop ( path , hop )
tb_usb3_init_credits ( hop ) ;
2019-12-17 15:33:44 +03:00
}
/**
* tb_tunnel_discover_usb3 ( ) - Discover existing USB3 tunnels
* @ tb : Pointer to the domain structure
* @ down : USB3 downstream adapter
2021-11-14 18:20:59 +03:00
* @ alloc_hopid : Allocate HopIDs from visited ports
2019-12-17 15:33:44 +03:00
*
* If @ down adapter is active , follows the tunnel to the USB3 upstream
* adapter and back . Returns the discovered tunnel or % NULL if there was
* no tunnel .
*/
2021-11-14 18:20:59 +03:00
struct tb_tunnel * tb_tunnel_discover_usb3 ( struct tb * tb , struct tb_port * down ,
bool alloc_hopid )
2019-12-17 15:33:44 +03:00
{
struct tb_tunnel * tunnel ;
struct tb_path * path ;
if ( ! tb_usb3_port_is_enabled ( down ) )
return NULL ;
tunnel = tb_tunnel_alloc ( tb , 2 , TB_TUNNEL_USB3 ) ;
if ( ! tunnel )
return NULL ;
tunnel - > activate = tb_usb3_activate ;
tunnel - > src_port = down ;
/*
* Discover both paths even if they are not complete . We will
* clean them up by calling tb_tunnel_deactivate ( ) below in that
* case .
*/
path = tb_path_discover ( down , TB_USB3_HOPID , NULL , - 1 ,
2021-11-14 18:20:59 +03:00
& tunnel - > dst_port , " USB3 Down " , alloc_hopid ) ;
2019-12-17 15:33:44 +03:00
if ( ! path ) {
/* Just disable the downstream port */
tb_usb3_port_enable ( down , false ) ;
goto err_free ;
}
2020-04-02 12:45:34 +03:00
tunnel - > paths [ TB_USB3_PATH_DOWN ] = path ;
tb_usb3_init_path ( tunnel - > paths [ TB_USB3_PATH_DOWN ] ) ;
2019-12-17 15:33:44 +03:00
path = tb_path_discover ( tunnel - > dst_port , - 1 , down , TB_USB3_HOPID , NULL ,
2021-11-14 18:20:59 +03:00
" USB3 Up " , alloc_hopid ) ;
2019-12-17 15:33:44 +03:00
if ( ! path )
goto err_deactivate ;
2020-04-02 12:45:34 +03:00
tunnel - > paths [ TB_USB3_PATH_UP ] = path ;
tb_usb3_init_path ( tunnel - > paths [ TB_USB3_PATH_UP ] ) ;
2019-12-17 15:33:44 +03:00
/* Validate that the tunnel is complete */
if ( ! tb_port_is_usb3_up ( tunnel - > dst_port ) ) {
tb_port_warn ( tunnel - > dst_port ,
" path does not end on an USB3 adapter, cleaning up \n " ) ;
goto err_deactivate ;
}
if ( down ! = tunnel - > src_port ) {
tb_tunnel_warn ( tunnel , " path is not complete, cleaning up \n " ) ;
goto err_deactivate ;
}
if ( ! tb_usb3_port_is_enabled ( tunnel - > dst_port ) ) {
tb_tunnel_warn ( tunnel ,
" tunnel is not fully activated, cleaning up \n " ) ;
goto err_deactivate ;
}
2020-03-24 15:44:13 +03:00
if ( ! tb_route ( down - > sw ) ) {
int ret ;
/*
* Read the initial bandwidth allocation for the first
* hop tunnel .
*/
ret = usb4_usb3_port_allocated_bandwidth ( down ,
& tunnel - > allocated_up , & tunnel - > allocated_down ) ;
if ( ret )
goto err_deactivate ;
tb_tunnel_dbg ( tunnel , " currently allocated bandwidth %d/%d Mb/s \n " ,
tunnel - > allocated_up , tunnel - > allocated_down ) ;
tunnel - > init = tb_usb3_init ;
tunnel - > consumed_bandwidth = tb_usb3_consumed_bandwidth ;
tunnel - > release_unused_bandwidth =
tb_usb3_release_unused_bandwidth ;
tunnel - > reclaim_available_bandwidth =
tb_usb3_reclaim_available_bandwidth ;
}
2019-12-17 15:33:44 +03:00
tb_tunnel_dbg ( tunnel , " discovered \n " ) ;
return tunnel ;
err_deactivate :
tb_tunnel_deactivate ( tunnel ) ;
err_free :
tb_tunnel_free ( tunnel ) ;
return NULL ;
}
/**
* tb_tunnel_alloc_usb3 ( ) - allocate a USB3 tunnel
* @ tb : Pointer to the domain structure
* @ up : USB3 upstream adapter port
* @ down : USB3 downstream adapter port
2020-03-24 15:44:13 +03:00
* @ max_up : Maximum available upstream bandwidth for the USB3 tunnel ( % 0
* if not limited ) .
* @ max_down : Maximum available downstream bandwidth for the USB3 tunnel
* ( % 0 if not limited ) .
2019-12-17 15:33:44 +03:00
*
* Allocate an USB3 tunnel . The ports must be of type @ TB_TYPE_USB3_UP and
* @ TB_TYPE_USB3_DOWN .
*
* Return : Returns a tb_tunnel on success or % NULL on failure .
*/
struct tb_tunnel * tb_tunnel_alloc_usb3 ( struct tb * tb , struct tb_port * up ,
2020-03-24 15:44:13 +03:00
struct tb_port * down , int max_up ,
int max_down )
2019-12-17 15:33:44 +03:00
{
struct tb_tunnel * tunnel ;
struct tb_path * path ;
2020-03-24 15:44:13 +03:00
int max_rate = 0 ;
/*
* Check that we have enough bandwidth available for the new
* USB3 tunnel .
*/
if ( max_up > 0 | | max_down > 0 ) {
max_rate = tb_usb3_max_link_rate ( down , up ) ;
if ( max_rate < 0 )
return NULL ;
/* Only 90% can be allocated for USB3 isochronous transfers */
max_rate = max_rate * 90 / 100 ;
tb_port_dbg ( up , " required bandwidth for USB3 tunnel %d Mb/s \n " ,
max_rate ) ;
if ( max_rate > max_up | | max_rate > max_down ) {
tb_port_warn ( up , " not enough bandwidth for USB3 tunnel \n " ) ;
return NULL ;
}
}
2019-12-17 15:33:44 +03:00
tunnel = tb_tunnel_alloc ( tb , 2 , TB_TUNNEL_USB3 ) ;
if ( ! tunnel )
return NULL ;
tunnel - > activate = tb_usb3_activate ;
tunnel - > src_port = down ;
tunnel - > dst_port = up ;
2020-03-24 15:44:13 +03:00
tunnel - > max_up = max_up ;
tunnel - > max_down = max_down ;
2019-12-17 15:33:44 +03:00
path = tb_path_alloc ( tb , down , TB_USB3_HOPID , up , TB_USB3_HOPID , 0 ,
" USB3 Down " ) ;
if ( ! path ) {
tb_tunnel_free ( tunnel ) ;
return NULL ;
}
tb_usb3_init_path ( path ) ;
tunnel - > paths [ TB_USB3_PATH_DOWN ] = path ;
path = tb_path_alloc ( tb , up , TB_USB3_HOPID , down , TB_USB3_HOPID , 0 ,
" USB3 Up " ) ;
if ( ! path ) {
tb_tunnel_free ( tunnel ) ;
return NULL ;
}
tb_usb3_init_path ( path ) ;
tunnel - > paths [ TB_USB3_PATH_UP ] = path ;
2020-03-24 15:44:13 +03:00
if ( ! tb_route ( down - > sw ) ) {
tunnel - > allocated_up = max_rate ;
tunnel - > allocated_down = max_rate ;
tunnel - > init = tb_usb3_init ;
tunnel - > consumed_bandwidth = tb_usb3_consumed_bandwidth ;
tunnel - > release_unused_bandwidth =
tb_usb3_release_unused_bandwidth ;
tunnel - > reclaim_available_bandwidth =
tb_usb3_reclaim_available_bandwidth ;
}
2019-12-17 15:33:44 +03:00
return tunnel ;
}
2014-06-04 00:04:08 +04:00
/**
2017-02-19 14:48:29 +03:00
* tb_tunnel_free ( ) - free a tunnel
* @ tunnel : Tunnel to be freed
2014-06-04 00:04:08 +04:00
*
2019-03-06 19:21:08 +03:00
* Frees a tunnel . The tunnel does not need to be deactivated .
2014-06-04 00:04:08 +04:00
*/
2017-02-19 14:48:29 +03:00
void tb_tunnel_free ( struct tb_tunnel * tunnel )
2014-06-04 00:04:08 +04:00
{
2017-02-19 14:48:29 +03:00
int i ;
if ( ! tunnel )
2014-06-04 00:04:08 +04:00
return ;
2017-02-19 14:48:29 +03:00
2021-03-22 19:09:35 +03:00
if ( tunnel - > deinit )
tunnel - > deinit ( tunnel ) ;
2017-02-19 14:48:29 +03:00
for ( i = 0 ; i < tunnel - > npaths ; i + + ) {
if ( tunnel - > paths [ i ] )
tb_path_free ( tunnel - > paths [ i ] ) ;
}
kfree ( tunnel - > paths ) ;
2014-06-04 00:04:08 +04:00
kfree ( tunnel ) ;
}
/**
2017-02-19 14:48:29 +03:00
* tb_tunnel_is_invalid - check whether an activated path is still valid
* @ tunnel : Tunnel to check
2014-06-04 00:04:08 +04:00
*/
2017-02-19 14:48:29 +03:00
bool tb_tunnel_is_invalid ( struct tb_tunnel * tunnel )
2014-06-04 00:04:08 +04:00
{
2017-02-19 14:48:29 +03:00
int i ;
2014-06-04 00:04:08 +04:00
2017-02-19 14:48:29 +03:00
for ( i = 0 ; i < tunnel - > npaths ; i + + ) {
WARN_ON ( ! tunnel - > paths [ i ] - > activated ) ;
if ( tb_path_is_invalid ( tunnel - > paths [ i ] ) )
return true ;
}
2014-06-04 00:04:08 +04:00
2017-02-19 14:48:29 +03:00
return false ;
2014-06-04 00:04:08 +04:00
}
/**
2017-02-19 14:48:29 +03:00
* tb_tunnel_restart ( ) - activate a tunnel after a hardware reset
* @ tunnel : Tunnel to restart
*
* Return : 0 on success and negative errno in case if failure
2014-06-04 00:04:08 +04:00
*/
2017-02-19 14:48:29 +03:00
int tb_tunnel_restart ( struct tb_tunnel * tunnel )
2014-06-04 00:04:08 +04:00
{
2017-02-19 14:48:29 +03:00
int res , i ;
2014-06-04 00:04:08 +04:00
2018-09-17 16:32:13 +03:00
tb_tunnel_dbg ( tunnel , " activating \n " ) ;
2014-06-04 00:04:08 +04:00
2017-02-20 00:37:35 +03:00
/*
* Make sure all paths are properly disabled before enabling
* them again .
*/
for ( i = 0 ; i < tunnel - > npaths ; i + + ) {
if ( tunnel - > paths [ i ] - > activated ) {
tb_path_deactivate ( tunnel - > paths [ i ] ) ;
tunnel - > paths [ i ] - > activated = false ;
}
}
2018-09-17 16:30:49 +03:00
if ( tunnel - > init ) {
res = tunnel - > init ( tunnel ) ;
if ( res )
return res ;
}
2017-02-19 14:48:29 +03:00
for ( i = 0 ; i < tunnel - > npaths ; i + + ) {
res = tb_path_activate ( tunnel - > paths [ i ] ) ;
if ( res )
goto err ;
}
2014-06-04 00:04:08 +04:00
2017-02-19 14:48:29 +03:00
if ( tunnel - > activate ) {
res = tunnel - > activate ( tunnel , true ) ;
if ( res )
goto err ;
}
2014-06-04 00:04:08 +04:00
return 0 ;
2017-02-19 14:48:29 +03:00
2014-06-04 00:04:08 +04:00
err :
tb_tunnel_warn ( tunnel , " activation failed \n " ) ;
2017-02-19 14:48:29 +03:00
tb_tunnel_deactivate ( tunnel ) ;
2014-06-04 00:04:08 +04:00
return res ;
}
/**
2017-02-19 14:48:29 +03:00
* tb_tunnel_activate ( ) - activate a tunnel
* @ tunnel : Tunnel to activate
2014-06-04 00:04:08 +04:00
*
* Return : Returns 0 on success or an error code on failure .
*/
2017-02-19 14:48:29 +03:00
int tb_tunnel_activate ( struct tb_tunnel * tunnel )
2014-06-04 00:04:08 +04:00
{
2017-02-19 14:48:29 +03:00
int i ;
2014-06-04 00:04:08 +04:00
2017-02-19 14:48:29 +03:00
for ( i = 0 ; i < tunnel - > npaths ; i + + ) {
if ( tunnel - > paths [ i ] - > activated ) {
tb_tunnel_WARN ( tunnel ,
" trying to activate an already activated tunnel \n " ) ;
return - EINVAL ;
}
}
2014-06-04 00:04:08 +04:00
2017-02-19 14:48:29 +03:00
return tb_tunnel_restart ( tunnel ) ;
}
2014-06-04 00:04:08 +04:00
/**
2017-02-19 14:48:29 +03:00
* tb_tunnel_deactivate ( ) - deactivate a tunnel
* @ tunnel : Tunnel to deactivate
2014-06-04 00:04:08 +04:00
*/
2017-02-19 14:48:29 +03:00
void tb_tunnel_deactivate ( struct tb_tunnel * tunnel )
2014-06-04 00:04:08 +04:00
{
2017-02-19 14:48:29 +03:00
int i ;
2018-09-17 16:32:13 +03:00
tb_tunnel_dbg ( tunnel , " deactivating \n " ) ;
2014-06-04 00:04:08 +04:00
2017-02-19 14:48:29 +03:00
if ( tunnel - > activate )
tunnel - > activate ( tunnel , false ) ;
for ( i = 0 ; i < tunnel - > npaths ; i + + ) {
2017-02-20 00:43:26 +03:00
if ( tunnel - > paths [ i ] & & tunnel - > paths [ i ] - > activated )
2017-02-19 14:48:29 +03:00
tb_path_deactivate ( tunnel - > paths [ i ] ) ;
}
}
2019-03-26 16:03:48 +03:00
/**
2020-03-24 15:44:13 +03:00
* tb_tunnel_port_on_path ( ) - Does the tunnel go through port
2019-03-26 16:03:48 +03:00
* @ tunnel : Tunnel to check
2020-03-24 15:44:13 +03:00
* @ port : Port to check
2019-03-26 16:03:48 +03:00
*
2020-03-24 15:44:13 +03:00
* Returns true if @ tunnel goes through @ port ( direction does not matter ) ,
2019-03-26 16:03:48 +03:00
* false otherwise .
*/
2020-03-24 15:44:13 +03:00
bool tb_tunnel_port_on_path ( const struct tb_tunnel * tunnel ,
const struct tb_port * port )
2019-03-26 16:03:48 +03:00
{
int i ;
for ( i = 0 ; i < tunnel - > npaths ; i + + ) {
if ( ! tunnel - > paths [ i ] )
continue ;
2020-03-24 15:44:13 +03:00
if ( tb_path_port_on_path ( tunnel - > paths [ i ] , port ) )
2019-03-26 16:03:48 +03:00
return true ;
}
return false ;
}
static bool tb_tunnel_is_active ( const struct tb_tunnel * tunnel )
{
int i ;
for ( i = 0 ; i < tunnel - > npaths ; i + + ) {
if ( ! tunnel - > paths [ i ] )
return false ;
if ( ! tunnel - > paths [ i ] - > activated )
return false ;
}
return true ;
}
2023-01-18 15:44:10 +03:00
/**
* tb_tunnel_maximum_bandwidth ( ) - Return maximum possible bandwidth
* @ tunnel : Tunnel to check
* @ max_up : Maximum upstream bandwidth in Mb / s
* @ max_down : Maximum downstream bandwidth in Mb / s
*
* Returns maximum possible bandwidth this tunnel can go if not limited
* by other bandwidth clients . If the tunnel does not support this
* returns % - EOPNOTSUPP .
*/
2022-03-23 17:45:39 +03:00
int tb_tunnel_maximum_bandwidth ( struct tb_tunnel * tunnel , int * max_up ,
int * max_down )
{
if ( ! tb_tunnel_is_active ( tunnel ) )
return - EINVAL ;
if ( tunnel - > maximum_bandwidth )
return tunnel - > maximum_bandwidth ( tunnel , max_up , max_down ) ;
return - EOPNOTSUPP ;
}
/**
* tb_tunnel_allocated_bandwidth ( ) - Return bandwidth allocated for the tunnel
* @ tunnel : Tunnel to check
* @ allocated_up : Currently allocated upstream bandwidth in Mb / s is stored here
* @ allocated_down : Currently allocated downstream bandwidth in Mb / s is
* stored here
*
* Returns the bandwidth allocated for the tunnel . This may be higher
* than what the tunnel actually consumes .
*/
int tb_tunnel_allocated_bandwidth ( struct tb_tunnel * tunnel , int * allocated_up ,
int * allocated_down )
{
if ( ! tb_tunnel_is_active ( tunnel ) )
return - EINVAL ;
if ( tunnel - > allocated_bandwidth )
return tunnel - > allocated_bandwidth ( tunnel , allocated_up ,
allocated_down ) ;
return - EOPNOTSUPP ;
}
/**
* tb_tunnel_alloc_bandwidth ( ) - Change tunnel bandwidth allocation
* @ tunnel : Tunnel whose bandwidth allocation to change
* @ alloc_up : New upstream bandwidth in Mb / s
* @ alloc_down : New downstream bandwidth in Mb / s
*
* Tries to change tunnel bandwidth allocation . If succeeds returns % 0
* and updates @ alloc_up and @ alloc_down to that was actually allocated
* ( it may not be the same as passed originally ) . Returns negative errno
* in case of failure .
*/
int tb_tunnel_alloc_bandwidth ( struct tb_tunnel * tunnel , int * alloc_up ,
int * alloc_down )
{
if ( ! tb_tunnel_is_active ( tunnel ) )
return - EINVAL ;
if ( tunnel - > alloc_bandwidth )
return tunnel - > alloc_bandwidth ( tunnel , alloc_up , alloc_down ) ;
return - EOPNOTSUPP ;
}
2019-03-26 16:03:48 +03:00
/**
* tb_tunnel_consumed_bandwidth ( ) - Return bandwidth consumed by the tunnel
* @ tunnel : Tunnel to check
2020-03-28 13:52:31 +03:00
* @ consumed_up : Consumed bandwidth in Mb / s from @ dst_port to @ src_port .
* Can be % NULL .
* @ consumed_down : Consumed bandwidth in Mb / s from @ src_port to @ dst_port .
* Can be % NULL .
2019-03-26 16:03:48 +03:00
*
2020-03-28 13:52:31 +03:00
* Stores the amount of isochronous bandwidth @ tunnel consumes in
* @ consumed_up and @ consumed_down . In case of success returns % 0 ,
* negative errno otherwise .
2019-03-26 16:03:48 +03:00
*/
2020-03-28 13:52:31 +03:00
int tb_tunnel_consumed_bandwidth ( struct tb_tunnel * tunnel , int * consumed_up ,
int * consumed_down )
2019-03-26 16:03:48 +03:00
{
2020-03-28 13:52:31 +03:00
int up_bw = 0 , down_bw = 0 ;
2019-03-26 16:03:48 +03:00
if ( ! tb_tunnel_is_active ( tunnel ) )
2020-03-28 13:52:31 +03:00
goto out ;
2019-03-26 16:03:48 +03:00
if ( tunnel - > consumed_bandwidth ) {
2020-03-28 13:52:31 +03:00
int ret ;
2019-03-26 16:03:48 +03:00
2020-03-28 13:52:31 +03:00
ret = tunnel - > consumed_bandwidth ( tunnel , & up_bw , & down_bw ) ;
if ( ret )
return ret ;
tb_tunnel_dbg ( tunnel , " consumed bandwidth %d/%d Mb/s \n " , up_bw ,
down_bw ) ;
2019-03-26 16:03:48 +03:00
}
2020-03-28 13:52:31 +03:00
out :
if ( consumed_up )
* consumed_up = up_bw ;
if ( consumed_down )
* consumed_down = down_bw ;
2019-03-26 16:03:48 +03:00
return 0 ;
}
2020-03-24 15:44:13 +03:00
/**
* tb_tunnel_release_unused_bandwidth ( ) - Release unused bandwidth
* @ tunnel : Tunnel whose unused bandwidth to release
*
* If tunnel supports dynamic bandwidth management ( USB3 tunnels at the
* moment ) this function makes it to release all the unused bandwidth .
*
* Returns % 0 in case of success and negative errno otherwise .
*/
int tb_tunnel_release_unused_bandwidth ( struct tb_tunnel * tunnel )
{
if ( ! tb_tunnel_is_active ( tunnel ) )
return 0 ;
if ( tunnel - > release_unused_bandwidth ) {
int ret ;
ret = tunnel - > release_unused_bandwidth ( tunnel ) ;
if ( ret )
return ret ;
}
return 0 ;
}
/**
* tb_tunnel_reclaim_available_bandwidth ( ) - Reclaim available bandwidth
* @ tunnel : Tunnel reclaiming available bandwidth
* @ available_up : Available upstream bandwidth ( in Mb / s )
* @ available_down : Available downstream bandwidth ( in Mb / s )
*
* Reclaims bandwidth from @ available_up and @ available_down and updates
* the variables accordingly ( e . g decreases both according to what was
* reclaimed by the tunnel ) . If nothing was reclaimed the values are
* kept as is .
*/
void tb_tunnel_reclaim_available_bandwidth ( struct tb_tunnel * tunnel ,
int * available_up ,
int * available_down )
{
if ( ! tb_tunnel_is_active ( tunnel ) )
return ;
if ( tunnel - > reclaim_available_bandwidth )
tunnel - > reclaim_available_bandwidth ( tunnel , available_up ,
available_down ) ;
}
2023-09-19 15:00:44 +03:00
const char * tb_tunnel_type_name ( const struct tb_tunnel * tunnel )
{
return tb_tunnel_names [ tunnel - > type ] ;
}