Commit graph

417 commits

Author SHA1 Message Date
Lee Jones 4366979fdd thunderbolt: cap: Fix kernel-doc formatting issue
Fixes the following W=1 kernel build warning(s):

 drivers/thunderbolt/cap.c:189: warning: Function parameter or member 'sw' not described in 'tb_switch_find_cap'

Cc: Andreas Noever <andreas.noever@gmail.com>
Cc: Michael Jamet <michael.jamet@intel.com>
Cc: Yehezkel Bernat <YehezkelShB@gmail.com>
Cc: linux-usb@vger.kernel.org
Signed-off-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2021-01-28 12:33:18 +03:00
Lee Jones f679a41faf thunderbolt: dma_port: Check 'dma_port_flash_write_block()'s return value
... and take the error path if it fails.

Fixes the following W=1 kernel build warning(s):

 drivers/thunderbolt/dma_port.c: In function ‘dma_port_flash_write_block’:
 drivers/thunderbolt/dma_port.c:331:6: warning: variable ‘ret’ set but not used [-Wunused-but-set-variable]

Cc: Andreas Noever <andreas.noever@gmail.com>
Cc: Michael Jamet <michael.jamet@intel.com>
Cc: Yehezkel Bernat <YehezkelShB@gmail.com>
Cc: linux-usb@vger.kernel.org
Signed-off-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2021-01-28 12:33:06 +03:00
Mika Westerberg 3da88be249 thunderbolt: Add support for de-authorizing devices
In some cases it is useful to be able de-authorize devices. For example
if user logs out the userspace can have a policy that disconnects PCIe
devices until logged in again. This is only possible for software based
connection manager as it directly controls the tunnels.

For this reason make the authorized attribute accept writing 0 which
makes the software connection manager to tear down the corresponding
PCIe tunnel. Userspace can check if this is supported by reading a new
domain attribute deauthorization, that holds 1 in that case.

While there correct tb_domain_approve_switch() kernel-doc and
description of authorized attribute to mention that it is only about
PCIe tunnels.

Cc: Christian Kellner <christian@kellner.me>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Yehezkel Bernat <YehezkelShB@gmail.com>
2021-01-15 10:53:06 +03:00
Mika Westerberg fdb0887c5a thunderbolt: Start lane initialization after sleep
USB4 spec says that for TBT3 compatible device routers the connection
manager needs to set SLI (Start Lane Initialization) to get the lanes
that were not connected back to functional state after sleep. Same needs
to be done if the link was XDomain.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Yehezkel Bernat <YehezkelShB@gmail.com>
2021-01-11 17:15:42 +03:00
Rikard Falkeborn 6889e00f0e thunderbolt: Constify static attribute_group structs
The only usage of these is to put their addresses in arrays of pointers
to const attribute_groups. Make them const to allow the compiler to put
them in read-only memory.

Signed-off-by: Rikard Falkeborn <rikard.falkeborn@gmail.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2021-01-11 17:13:48 +03:00
Mika Westerberg c14556fc0c thunderbolt: Drop duplicated 0x prefix from format string
The tb_dbg() call is using %#x that already adds the 0x prefix so don't
duplicate it.

Fixes: 9039387e16 ("thunderbolt: Add USB4 router operation proxy for firmware connection manager")
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Yehezkel Bernat <YehezkelShB@gmail.com>
2021-01-07 15:14:44 +03:00
Tian Tao 82096ecf58 thunderbolt: Use kmemdup instead of kzalloc and memcpy
Fixes coccicheck warning:
drivers/thunderbolt/dma_test.c:302:13-20: WARNING opportunity for kmemdup.

Signed-off-by: Tian Tao <tiantao6@hisilicon.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-12-28 12:41:53 +03:00
Greg Kroah-Hartman 3d41137801 thunderbolt: Changes for v5.11 merge window
This includes following Thunderbolt/USB4 changes for v5.11 merge window:
 
   * DMA traffic test driver
 
   * USB4 router NVM upgrade improvements
 
   * USB4 router operations proxy implementation available in the recent
     Intel Connection Manager firmwares
 
   * Support for Intel Maple Ridge discrete Thunderbolt 4 controller
 
   * A couple of cleanups and minor improvements.
 -----BEGIN PGP SIGNATURE-----
 
 iQJUBAABCgA+FiEEVTdhRGBbNzLrSUBaAP2fSd+ZWKAFAl/PagggHG1pa2Eud2Vz
 dGVyYmVyZ0BsaW51eC5pbnRlbC5jb20ACgkQAP2fSd+ZWKD5DQ/+NFsABFQaf1P+
 sU4HVOo9cwfvQCFCapfOsOjBsrkuLsjZPQdUqVdTzhrDzRM6uVQXHqWkcInNYxEs
 D0o9f8yheYSPuZolHIIydkNZ7VjhXwhVp7FuF+6M3bIhtD9siuBUisCu7QtOjzpF
 EAzBZcGHvKXkPmVAQKZS/P4WsgcZDv0/pODjeQawosgJAPOo5begZVBEYcpbOeCT
 qvl1vEs+Fr5fcoFcY/58sJX932CcbbO5bZMSc01IIF94FQMsQJg3ATLdxkgc++2M
 FnzcHEVQi7h8zwCmMT4deGwLJqvbyVy5SNo6GY4/Adhsf0HQzrvtWdESegoIooJK
 dNWhSCuAFbXrFKGH4iBEUldigNXiCGiTwalmJ1+IIDccJQwkKC4GGU+9KEWBtYCn
 OIvKkHUWPeeqNBzSeiMbFDXiK6QFe2VpNBg/iRUZwZwxibqgjgJE1rHbY098sPrL
 yHRcrz6vih3wgpqZJTGdanIMk6F0MzaoHtj2egXbXaqyGf8dIdvnZJZN9gb5WDyu
 ZT/ffh3XiNfBvFtsu9gosnn3m76TQ4jIb4lUesTOVZjHX2yNz3MabYet312lP4PO
 qhxb1l2HGWuxnLLSxas6gzEv5arpx88ldSj6PaA86pBL/eezy59Bvn5hYrmCQ269
 lVZQ19nC8y13VyCgbqcyTSpGxS+NXV0=
 =+RXF
 -----END PGP SIGNATURE-----

Merge tag 'thunderbolt-for-v5.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/westeri/thunderbolt into usb-next

Mika writes:

thunderbolt: Changes for v5.11 merge window

This includes following Thunderbolt/USB4 changes for v5.11 merge window:

  * DMA traffic test driver

  * USB4 router NVM upgrade improvements

  * USB4 router operations proxy implementation available in the recent
    Intel Connection Manager firmwares

  * Support for Intel Maple Ridge discrete Thunderbolt 4 controller

  * A couple of cleanups and minor improvements.

* tag 'thunderbolt-for-v5.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/westeri/thunderbolt: (22 commits)
  thunderbolt: Add support for Intel Maple Ridge
  thunderbolt: Add USB4 router operation proxy for firmware connection manager
  thunderbolt: Move constants for USB4 router operations to tb_regs.h
  thunderbolt: Add connection manager specific hooks for USB4 router operations
  thunderbolt: Pass TX and RX data directly to usb4_switch_op()
  thunderbolt: Pass metadata directly to usb4_switch_op()
  thunderbolt: Perform USB4 router NVM upgrade in two phases
  thunderbolt: Return -ENOTCONN when ERR_CONN is received
  thunderbolt: Keep the parent runtime resumed for a while on device disconnect
  thunderbolt: Log adapter numbers in decimal in path activation/deactivation
  thunderbolt: Log which connection manager implementation is used
  thunderbolt: Move max_boot_acl field to correct place in struct icm
  MAINTAINERS: Add Isaac as maintainer of Thunderbolt DMA traffic test driver
  thunderbolt: Add DMA traffic test driver
  thunderbolt: Add support for end-to-end flow control
  thunderbolt: Make it possible to allocate one directional DMA tunnel
  thunderbolt: Create debugfs directory automatically for services
  thunderbolt: Add functions for enabling and disabling lane bonding on XDomain
  thunderbolt: Add link_speed and link_width to XDomain
  thunderbolt: Create XDomain devices for loops back to the host
  ...
2020-12-08 13:41:45 +01:00
Mika Westerberg db0746e339 thunderbolt: Add support for Intel Maple Ridge
Maple Ridge is first discrete USB4 host controller from Intel. It comes
with firmware based connection manager and the flows are similar as used
in Intel Titan Ridge.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-30 14:39:24 +03:00
Mika Westerberg 9039387e16 thunderbolt: Add USB4 router operation proxy for firmware connection manager
Intel Maple Ridge and Tiger Lake connection manager firmware implements
a USB4 router operation proxy that should be used instead of direct
register access to avoid races with the firmware. This is supported in
all firmwares where the protocol version field returned in the driver
ready response is 3 (or higher).

This adds the USB4 router proxy operations support to the driver so that
we first check the protocol version and if it is 3 (or higher) the USB4
router operation is run through the firmware provided proxy. Otherwise
the native version is used.

Most USB4 router proxy operations are pretty straightforward except
NVM_AUTH where the firmware only responds once the router is restarted
but before it sends device connected notification. To support this we
split the operation so that the reply is received asynchronously and
stored to struct icm. This last reply is then returned in
icm_usb4_switch_nvm_authenticate_status() if available.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-30 14:39:24 +03:00
Mika Westerberg 579f14217c thunderbolt: Move constants for USB4 router operations to tb_regs.h
We are going to use these in subsequent patch so make them available
outside of usb4.c.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-30 14:39:24 +03:00
Mika Westerberg 9490f71167 thunderbolt: Add connection manager specific hooks for USB4 router operations
Intel USB4 host routers that run the firmware based connection manager
(ICM) may implement a proxy for USB4 router operations. This is to avoid
the firmware to race with the OS driver, as both may need to run these
operations.

This adds two new connection manager specific callbacks which, if
provided, get called instead of the native USB4 router operation.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-30 14:39:24 +03:00
Mika Westerberg 83bab44ada thunderbolt: Pass TX and RX data directly to usb4_switch_op()
We are going to make usb4_switch_op() to match better the corresponding
firmware (ICM) USB4 router operation proxy interface, so that we can use
either based on the connection manager implementation.

For this reason rename usb4_switch_op() to __usb4_switch_op() that
provides the most complete interface. Then make usb4_switch_op() and
usb4_switch_op_data() call it with correct set of parameters and update
the callers accordingly.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-30 14:39:24 +03:00
Mika Westerberg fe265a0631 thunderbolt: Pass metadata directly to usb4_switch_op()
We are going to make usb4_switch_op() to match better the corresponding
firmware (ICM) USB4 router operation proxy interface, so that we can use
either based on the connection manager implementation. For this reason
pass metadata directly to usb4_switch_op().

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-30 14:39:24 +03:00
Mika Westerberg 661b19473b thunderbolt: Perform USB4 router NVM upgrade in two phases
The currect code expects that the router returns back the status of the
NVM authentication immediately. When tested against a real USB4 device
what happens is that the router is reset and only after that the result
is updated in the ROUTER_CS_26 register status field. This also seems to
align better what the spec suggests.

For this reason do the same what we already do with the Thunderbolt 3
devices and perform the NVM upgrade in two phases. First start the
NVM_AUTH router operation and once the router is added back after the
reset read the status in ROUTER_CS_26 and expose it to the userspace
accordingly.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-30 14:39:24 +03:00
Mika Westerberg 463e48fa54 thunderbolt: Return -ENOTCONN when ERR_CONN is received
This allows the calling code to distinguish if the error was due to
ERR_CONN (adapter is disconneced or disabled) or something else. Will be
needed in USB4 router NVM update in the following patch.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-30 14:39:24 +03:00
Mika Westerberg b658eb9d90 thunderbolt: Keep the parent runtime resumed for a while on device disconnect
When doing device firmware upgrade the device will disconnect for a
while and then reconnect back. Keep the parent device (and the whole
domain) powered for a while so we don't need to runtime resume
immediately when the device is connected back after the device upgrade
completes.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-30 14:39:23 +03:00
Mika Westerberg a359525897 thunderbolt: Log adapter numbers in decimal in path activation/deactivation
This makes it consistent with other debug logs that already are using
decimal number for adapters (ports).

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-30 14:39:23 +03:00
Mika Westerberg e0258805d7 thunderbolt: Log which connection manager implementation is used
This makes it easier to figure out whether the driver is using firmware
or software based connection manager implementation.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-30 14:39:23 +03:00
Mika Westerberg 45ef561abc thunderbolt: Move max_boot_acl field to correct place in struct icm
This makes the kernel-doc to match the ordering and also this is better
place for it, not between upstream_port and vnd_cap that are used
together.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-30 14:39:23 +03:00
Mika Westerberg 600c0849cf thunderbolt: Fix use-after-free in remove_unplugged_switch()
Paulian reported a crash that happens when a dock is unplugged during
hibernation:

[78436.228217] thunderbolt 0-1: device disconnected
[78436.228365] BUG: kernel NULL pointer dereference, address: 00000000000001e0
...
[78436.228397] RIP: 0010:icm_free_unplugged_children+0x109/0x1a0
...
[78436.228432] Call Trace:
[78436.228439]  icm_rescan_work+0x24/0x30
[78436.228444]  process_one_work+0x1a3/0x3a0
[78436.228449]  worker_thread+0x30/0x370
[78436.228454]  ? process_one_work+0x3a0/0x3a0
[78436.228457]  kthread+0x13d/0x160
[78436.228461]  ? kthread_park+0x90/0x90
[78436.228465]  ret_from_fork+0x1f/0x30

This happens because remove_unplugged_switch() calls tb_switch_remove()
that releases the memory pointed by sw so the following lines reference
to a memory that might be released already.

Fix this by saving pointer to the parent device before calling
tb_switch_remove().

Reported-by: Paulian Bogdan Marinca <paulian@marinca.net>
Fixes: 4f7c2e0d87 ("thunderbolt: Make sure device runtime resume completes before taking domain lock")
Cc: stable@vger.kernel.org
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-11-19 17:44:10 +03:00
Isaac Hazan edc0f494ed thunderbolt: Add DMA traffic test driver
This driver allows sending DMA traffic over XDomain connection.
Specifically over a loopback connection using either a Thunderbolt/USB4
cable that is connected back to the host router port, or a special
loopback dongle that has RX and TX lines crossed. This can be useful at
manufacturing floor to check whether Thunderbolt/USB4 ports are
functional.

The driver exposes debugfs directory under the XDomain service that can
be used to configure the driver, start the test and check the results.

If a loopback dongle is used the steps to send and receive 1000 packets
can be done like:

  # modprobe thunderbolt_dma_test
  # echo 1000 > /sys/kernel/debug/thunderbolt/<service_id>/dma_test/packets_to_receive
  # echo 1000 > /sys/kernel/debug/thunderbolt/<service_id>/dma_test/packets_to_send
  # echo 1 > /sys/kernel/debug/thunderbolt/<service_id>/dma_test/test
  # cat /sys/kernel/debug/thunderbolt/<service_id>/dma_test/status

When a cable is connected back to host then there are two Thunderbolt
services, one is configured for receiving (does not matter which one):

  # modprobe thunderbolt_dma_test
  # echo 1000 > /sys/kernel/debug/thunderbolt/<service_a>/dma_test/packets_to_receive
  # echo 1 > /sys/kernel/debug/thunderbolt/<service_a>/dma_test/test

The other one for sending:

  # echo 1000 > /sys/kernel/debug/thunderbolt/<service_b>/dma_test/packets_to_send
  # echo 1 > /sys/kernel/debug/thunderbolt/<service_b>/dma_test/test

Results can be read from both services status attributes.

Signed-off-by: Isaac Hazan <isaac.hazan@intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Yehezkel Bernat <YehezkelShB@gmail.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-11-11 10:20:16 +03:00
Mika Westerberg afe704a2d0 thunderbolt: Add support for end-to-end flow control
USB4 spec defines end-to-end (E2E) flow control that can be used between
hosts to prevent overflow of a RX ring. We previously had this partially
implemented but that code was removed with commit 53f13319d1
("thunderbolt: Get rid of E2E workaround") with the idea that we add it
back properly if there ever is need. Now that we are going to add DMA
traffic test driver (in subsequent patches) this can be useful.

For this reason we modify tb_ring_alloc_rx/tx() so that they accept
RING_FLAG_E2E and configure the hardware ring accordingly. The RX side
also requires passing TX HopID (e2e_tx_hop) used in the credit grant
packets.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Yehezkel Bernat <YehezkelShB@gmail.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-11-11 10:20:16 +03:00
Mika Westerberg 5bf722df5d thunderbolt: Make it possible to allocate one directional DMA tunnel
With DMA tunnels it is possible that the service using it does not
require bi-directional paths so make RX and TX optional (but of course
one of them needs to be set).

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Yehezkel Bernat <YehezkelShB@gmail.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-11-11 10:20:16 +03:00
Mika Westerberg 407ac931ae thunderbolt: Create debugfs directory automatically for services
This allows service drivers to use it as parent directory if they need
to add their own debugfs entries.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Yehezkel Bernat <YehezkelShB@gmail.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-11-11 10:20:16 +03:00
Isaac Hazan 5cc0df9ce1 thunderbolt: Add functions for enabling and disabling lane bonding on XDomain
These can be used by service drivers to enable and disable lane bonding
as needed.

Signed-off-by: Isaac Hazan <isaac.hazan@intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Yehezkel Bernat <YehezkelShB@gmail.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-11-11 10:20:16 +03:00
Isaac Hazan 4210d50f0b thunderbolt: Add link_speed and link_width to XDomain
Link speed and link width are needed for checking expected values in
case of using a loopback service.

Signed-off-by: Isaac Hazan <isaac.hazan@intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Yehezkel Bernat <YehezkelShB@gmail.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-11-11 10:20:16 +03:00
Mika Westerberg 47844ecb8c thunderbolt: Create XDomain devices for loops back to the host
It is perfectly possible to have loops back from the routers to the
host, or even from one host port to another. Instead of ignoring these,
we create XDomain devices for each. This allows creating services such
as DMA traffic test that is used in manufacturing for example.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Yehezkel Bernat <YehezkelShB@gmail.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-11-11 10:20:16 +03:00
Mika Westerberg d67274bacb thunderbolt: Find XDomain by route instead of UUID
We are going to represent loops back to the host also as XDomains and
they all have the same (host) UUID, so finding them needs to use route
string instead. This also requires that we check if the XDomain device
is added to the bus before its properties can be updated. Otherwise the
remote UUID might not be populated yet.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Yehezkel Bernat <YehezkelShB@gmail.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-11-11 10:20:16 +03:00
Mika Westerberg 81816f5048 thunderbolt: Do not clear USB4 router protocol adapter IFC and ISE bits
These fields are marked as vendor defined in the USB4 spec and should
not be modified by the software, so only clear them when we are dealing
with pre-USB4 hardware.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Yehezkel Bernat <YehezkelShB@gmail.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-11-11 10:20:15 +03:00
Mika Westerberg f6439c531d thunderbolt: Add support for Intel Tiger Lake-H
Intel Tiger Lake-H has the same Thunderbolt/USB4 controller as Tiger
Lake-LP. Add the Tiger Lake-H PCI IDs to the driver list of supported
devices.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-06 16:39:11 +03:00
Mika Westerberg f8fa2c2e63 thunderbolt: Only configure USB4 wake for lane 0 adapters
Only USB4 lane 0 adapter has the USB4 port capability for wakes so only
program wakes on such adapters.

Fixes: b2911a593a ("thunderbolt: Enable wakes from system suspend")
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-05 13:32:53 +03:00
Casey Bowman 77455129fb thunderbolt: Add uaccess dependency to debugfs interface
Some calls in the debugfs interface are made to the linux/uaccess.h header,
but the header is not referenced. So, for x86_64 architectures, this
dependency seems to be pulled in elsewhere, which leads to a successful
compilation. However, on arm/arm64 architectures, it was found to error out
on implicit declarations.

This change fixes the implicit declaration error by adding the
linux/uaccess.h header.

Fixes: 54e418106c ("thunderbolt: Add debugfs interface")
Signed-off-by: Casey Bowman <casey.g.bowman@intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-05 13:32:46 +03:00
Mika Westerberg a663e0df4a thunderbolt: Fix memory leak if ida_simple_get() fails in enumerate_services()
The svc->key field is not released as it should be if ida_simple_get()
fails so fix that.

Fixes: 9aabb68568 ("thunderbolt: Fix to check return value of ida_simple_get")
Cc: stable@vger.kernel.org
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-11-05 13:32:40 +03:00
Jing Xiangfeng 7342ca34d9 thunderbolt: Add the missed ida_simple_remove() in ring_request_msix()
ring_request_msix() misses to call ida_simple_remove() in an error path.
Add a label 'err_ida_remove' and jump to it.

Fixes: 046bee1f9a ("thunderbolt: Add MSI-X support")
Cc: stable@vger.kernel.org
Signed-off-by: Jing Xiangfeng <jingxiangfeng@huawei.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-10-26 18:28:14 +03:00
Greg Kroah-Hartman 59ee364baf thunderbolt: Changes for v5.10 merge window
This includes following Thunderbolt/USB4 changes for v5.10 merge window:
 
   * A couple of optimizations around Tiger Lake force power logic and
     NHI (Native Host Interface) LC (Link Controller) mailbox command
     processing
 
   * Power management improvements for Software Connection Manager
 
   * Debugfs support
 
   * Allow KUnit tests to be enabled also when Thunderbolt driver is
     configured as module.
 
   * Few minor cleanups and fixes
 
 All these have been in linux-next with no reported issues.
 -----BEGIN PGP SIGNATURE-----
 
 iQJUBAABCgA+FiEEVTdhRGBbNzLrSUBaAP2fSd+ZWKAFAl90HsMgHG1pa2Eud2Vz
 dGVyYmVyZ0BsaW51eC5pbnRlbC5jb20ACgkQAP2fSd+ZWKAhOhAAnR/SpkKkTPkw
 vFgL4jtQ89KY/mhJnfmbqFqcb2zLWaso9kFdWfLs6ITqOU0H3Pu1PR/shyx0Xka4
 i/kIT8iuorqO7Y1ILFtWgybXkE77AEJgtp3Q+Li/Y7ZPR0kipbjUH/fmCAWPtmHd
 Qkjd1nV72TQEkU3P7X6ob70Xkil2qn6i4fF1Kp5Mjg/8fGkqkrO2TZZPi7PXnsaG
 PySIffJY2zeEiohjl9Q/gOqBogMCPC8DgZmcS4QJ69DS8zOF9yKLUFqGQtd31BFG
 i9HgLYR8xOV7mdTVmTLZp0dzDT3rqjubnA3TfgUG7HAjQyeTZnnSGbeLlYtpoKX0
 rvL4gkEmQBkn51KiqKCvCcktzdqQ6hUjyqWjVJar63qks0AIcbDHphj37DHFW4Fc
 qO5lpON0T8JxBBQ0xDoQk+1aU6xq8QhaB+iOzyy5ZSU5vL3pcsAvqoOsxN+k0nud
 F5hfXsHwi8M/ZWcFrxNzpID0B9IVU+Fe2FXXVTMKdmpoPy/vU1xgFHj88o7vNAAb
 412u4NEzlxDOitONY6L1M6uGl9wSCVudMozfZci4dAAnAxj1/oeDurZJn5h4BtsR
 q0iP2JToSqrovd4V5uQ53MEF0JqkkYVwbAaV+y4/2afwJho3SoIDa9xv3ffpei68
 If1wkLHkBJIlWCci1fsiOuvJ+IH+rK0=
 =KvWM
 -----END PGP SIGNATURE-----

Merge tag 'thunderbolt-for-v5.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/westeri/thunderbolt into usb-next

Mika writes:

thunderbolt: Changes for v5.10 merge window

This includes following Thunderbolt/USB4 changes for v5.10 merge window:

  * A couple of optimizations around Tiger Lake force power logic and
    NHI (Native Host Interface) LC (Link Controller) mailbox command
    processing

  * Power management improvements for Software Connection Manager

  * Debugfs support

  * Allow KUnit tests to be enabled also when Thunderbolt driver is
    configured as module.

  * Few minor cleanups and fixes

All these have been in linux-next with no reported issues.

* tag 'thunderbolt-for-v5.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/westeri/thunderbolt: (37 commits)
  thunderbolt: Capitalize comment on top of QUIRK_FORCE_POWER_LINK_CONTROLLER
  thunderbolt: Correct tb_check_quirks() kernel-doc
  thunderbolt: Log correct zeroX entries in decode_error()
  thunderbolt: Handle ERR_LOCK notification
  thunderbolt: Use "if USB4" instead of "depends on" in Kconfig
  thunderbolt: Allow KUnit tests to be built also when CONFIG_USB4=m
  thunderbolt: Only stop control channel when entering freeze
  thunderbolt: debugfs: Fix uninitialized return in counters_write()
  thunderbolt: Add debugfs interface
  thunderbolt: No need to warn in TB_CFG_ERROR_INVALID_CONFIG_SPACE
  thunderbolt: Introduce tb_switch_is_tiger_lake()
  thunderbolt: Introduce tb_switch_is_ice_lake()
  thunderbolt: Check for Intel vendor ID when identifying controller
  thunderbolt: Introduce tb_port_is_nhi()
  thunderbolt: Introduce tb_switch_next_cap()
  thunderbolt: Introduce tb_port_next_cap()
  thunderbolt: Move struct tb_cap_any to tb_regs.h
  thunderbolt: Add runtime PM for Software CM
  thunderbolt: Create device links from ACPI description
  ACPI: Export acpi_get_first_physical_node() to modules
  ...
2020-09-30 09:42:36 +02:00
Mika Westerberg 810278da90 thunderbolt: Capitalize comment on top of QUIRK_FORCE_POWER_LINK_CONTROLLER
To keep it consistent with the other single line comments in the driver.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-16 14:57:46 +03:00
Mika Westerberg 9c8cac6adf thunderbolt: Correct tb_check_quirks() kernel-doc
Remove extra white space and make the sentence end with a period.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-16 14:57:46 +03:00
Mika Westerberg 22255bec2b thunderbolt: Log correct zeroX entries in decode_error()
There was copy & paste error so it always printed value of pkg->zero1.
Also use tb_ctl_warn() here, no need to print backtrace.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-16 14:57:46 +03:00
Mika Westerberg 80e7c5dd1e thunderbolt: Handle ERR_LOCK notification
If the USB4 router downstream port is locked, sending configuration
packet to a router below it causes ERR_LOCK to be sent. Instead of warn
splat about unknown error we log the error (just warning level) and
return -EACCESS instead. The idea is that we may want to do something
when such error code is received, like perform unlock.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-16 14:57:46 +03:00
Mika Westerberg 8eabfca523 thunderbolt: Use "if USB4" instead of "depends on" in Kconfig
This groups the USB4 options more nicely, and also does not require that
every config option lists explicit depends on USB4.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-16 14:57:46 +03:00
Mika Westerberg 2c6ea4e2ce thunderbolt: Allow KUnit tests to be built also when CONFIG_USB4=m
This adds a bit more build coverage for the tests even though these are
not expected to be enabled by normal users and distros. In order to make
this working we need to open-code kunit_test_suite() and call the
relevant functions directly in the driver init/exit hook.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-16 14:57:46 +03:00
Mika Westerberg 884e4d576f thunderbolt: Only stop control channel when entering freeze
According to the kernel power management documentation freeze phase
should only quiesce the device, no need to configure wakes or put it to
low power state. For this reason we simply stop the control channel and
in case of Software Connection Manager also mark the hotplug disabled.
This should align the driver better with the PM framework expectations.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-16 14:57:46 +03:00
Greg Kroah-Hartman bf638d736e thunderbolt: Fix for v5.9-rc6
One more fix that makes ASUS PA27AC Thunderbolt 3 monitor work more
 reliably.
 
 This has been in linux-next with no reported issues.
 -----BEGIN PGP SIGNATURE-----
 
 iQJUBAABCgA+FiEEVTdhRGBbNzLrSUBaAP2fSd+ZWKAFAl9giR0gHG1pa2Eud2Vz
 dGVyYmVyZ0BsaW51eC5pbnRlbC5jb20ACgkQAP2fSd+ZWKBhFA/+KVuBU+0vneG1
 77j9No3wVVYp7spGa/HORYUKuBj72IBCIxARNzyjNY3h5422aKn44F5ReuTj4TGx
 414cQ1iGblZSiF8Geen0e7Oyg+KSKnenkKdiIE9oa4Hy/UPT39i7uJiDBriMc0dD
 PpriEEa8AMXfdjxY3WK8iynf9qoky1S2EHBo8Rz9nCQkQ+yOEKfZq8KC+Egb9XFV
 ebRsRj7rM4V9VNhETnfTm54M2zPw8n7HDKkQB1z8qddyv9i6biHTFXXXRD8A/Q/7
 ZrC7fW/OqUVra34bV70tGD3goSAi7kfHyMF4C4+dUhCP8lF36ozLqh/3cUt+5TOE
 ghpuHNsAb35PZu70h3U4hBTn9j/KtWgo6JH++xv6TkuffPEFWo1FSuPmupKg1WZK
 XMJ0CtwBeUM2JF2sXKlaUrjFsql7mxoRbnBjkp/giP9PiW7QXZzkl2Y8DgCvmDmF
 mBJj4rKu/oBBjs8NyD41roY2e+02DYIos5UzkYZl2/68Vv6USa601nG580gA6dBb
 Y/8RoaCFIxl3WK26+Ri08190BNlwMlVcX8ToaM9rroCI+9eDqsa5G8eb9zLmcWwC
 +tTNJMMFsho2Js+DxOcJXY0LoQeNrNazFKylz6UnsgL7zsw0kI0dM3l3qpRysqha
 Ua06NbrPEpqhQ62xKXtj7qAiWdIIZAo=
 =sgTO
 -----END PGP SIGNATURE-----

Merge tag 'thunderbolt-for-v5.9-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/westeri/thunderbolt into usb-linus

Mika writes:

thunderbolt: Fix for v5.9-rc6

One more fix that makes ASUS PA27AC Thunderbolt 3 monitor work more
reliably.

This has been in linux-next with no reported issues.

* tag 'thunderbolt-for-v5.9-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/westeri/thunderbolt:
  thunderbolt: Retry DROM read once if parsing fails
2020-09-15 13:52:14 +02:00
Dan Carpenter 77e4907fa6 thunderbolt: debugfs: Fix uninitialized return in counters_write()
If the first line is in an invalid format then the "ret" value is
uninitialized.  We should return -EINVAL instead.

Fixes: 54e418106c ("thunderbolt: Add debugfs interface")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-10 19:39:56 +03:00
Mika Westerberg f022ff7bf3 thunderbolt: Retry DROM read once if parsing fails
Kai-Heng reported that sometimes DROM parsing of ASUS PA27AC Thunderbolt 3
monitor fails. This makes the driver to fail to add the device so only
DisplayPort tunneling is functional.

It is not clear what exactly happens but waiting for 100 ms and retrying
the read seems to work this around so we do that here.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=206493
Reported-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
Tested-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
Cc: stable@vger.kernel.org
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-09 14:00:56 +03:00
Gil Fine 54e418106c thunderbolt: Add debugfs interface
This adds debugfs interface that can be used for debugging possible
issues in hardware/software. It exposes router and adapter config spaces
through files like this:

  /sys/kernel/debug/thunderbolt/<DEVICE>/regs
  /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT1>/regs
  /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT1>/path
  /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT1>/counters
  /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT2>/regs
  /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT2>/path
  /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT2>/counters
  ...

The "regs" is either the router or port configuration space register
dump. The "path" is the port path configuration space and "counters" is
the optional counters configuration space.

These files contains one register per line so it should be easy to use
normal filtering tools to find the registers of interest if needed.

The router and adapter regs file becomes writable when
CONFIG_USB4_DEBUGFS_WRITE is enabled (which is not supposed to be done
in production systems) and in this case the developer can write "offset
value" lines there to modify the hardware directly. For convenience this
also supports the long format the read side produces (but ignores the
additional fields). The counters file can be written even when
CONFIG_USB4_DEBUGFS_WRITE is not enabled and it is only used to clear
the counter values.

Signed-off-by: Gil Fine <gil.fine@intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-09-03 12:21:08 +03:00
Mika Westerberg fa1653d99c thunderbolt: No need to warn in TB_CFG_ERROR_INVALID_CONFIG_SPACE
This may be returned for example when accessing some of the vendor
specific capabilities. It is not fatal by any means so instead of WARN()
just log it as debug level. The caller gets error back anyway and is
expected to handle it accordingly.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-09-03 12:21:08 +03:00
Gil Fine 0637e3df17 thunderbolt: Introduce tb_switch_is_tiger_lake()
This is needed to differentiate Tiger Lake from other controllers.

Signed-off-by: Gil Fine <gil.fine@intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-09-03 12:21:08 +03:00
Mika Westerberg 8c3b15a600 thunderbolt: Introduce tb_switch_is_ice_lake()
This is needed to differentiate Ice Lake from other controllers.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-09-03 12:21:08 +03:00
Mika Westerberg 35ee69e94d thunderbolt: Check for Intel vendor ID when identifying controller
With USB4 there will be other vendors so make sure the current checks
for different Intel controllers will not accidentally match those.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-09-03 12:21:08 +03:00
Mika Westerberg a3cfebdc1b thunderbolt: Introduce tb_port_is_nhi()
This is useful if one needs to check if adapter (port) is the host
interface (NHI). Make tb_port_alloc_hopid() take advantage of this.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-09-03 12:21:08 +03:00
Mika Westerberg 6de057ef91 thunderbolt: Introduce tb_switch_next_cap()
This is similar to tb_port_next_cap() but instead allows walking
capability list of a switch (router). Convert tb_switch_find_cap() and
tb_switch_find_vse_cap() to use this as well.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-09-03 12:21:07 +03:00
Mika Westerberg 3c8b228d43 thunderbolt: Introduce tb_port_next_cap()
This function is useful for walking port config space (adapter)
capability lists. Convert the tb_port_find_cap() to use this as well.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-09-03 12:21:07 +03:00
Mika Westerberg 8f8310115e thunderbolt: Move struct tb_cap_any to tb_regs.h
This structure will be needed by the debugfs implementation so make it
available outside of cap.c.

While there add kernel-doc comments to the structure.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-09-03 12:21:07 +03:00
Mika Westerberg 6ac6faee5d thunderbolt: Add runtime PM for Software CM
This adds runtime PM support for the Software Connection Manager parts
of the driver. This allows to save power when either there is no device
attached at all or there is a device attached and all following
conditions are true:

  - Tunneled PCIe root/downstream ports are runtime suspended
  - Tunneled USB3 ports are runtime suspended
  - No active DisplayPort stream
  - No active XDomain connection

For the first two we take advantage of device links that were added in
previous patch. Difference for the system sleep case is that we also
enable wakes when something is geting plugged in/out of the Thunderbolt
ports.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:42 +03:00
Mika Westerberg b2be2b05cf thunderbolt: Create device links from ACPI description
The new way to describe relationship between tunneled ports and USB4 NHI
(Native Host Interface) is with ACPI _DSD looking like below for a PCIe
downstream port:

    Scope (\_SB.PCI0)
    {
        Device (NHI0) { } // Thunderbolt NHI

        Device (DSB0) // Hotplug downstream port
        {
            Name (_DSD, Package () {
                ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
                Package () {
                    Package () {"usb4-host-interface", \_SB.PCI0.NHI0},
                    ...
                }
            })
        }
    }

This is "documented" in these [1] USB-IF slides and being used on
systems that ship with Windows.

The _DSD can be added to tunneled USB3 and PCIe ports, and is needed to
make sure the USB4 NHI is resumed before any of the tunneled ports so
the protocol tunnels get established properly before the actual port
itself is resumed. Othwerwise the USB/PCI core find the link may not be
established and starts tearing down the device stack.

This parses the ACPI description each time NHI is probed and tries to
find devices that has the property and it references the NHI in
question. For each matching device a device link from that device to the
NHI is created.

Since USB3 ports themselves do not get runtime suspended with the parent
device (hub) we do not add the link from the USB3 port to USB4 NHI but
instead we add the link from the xHCI device. This makes the device link
usable for runtime PM as well.

[1] https://www.usb.org/sites/default/files/D1T2-2%20-%20USB4%20on%20Windows.pdf

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2020-09-03 12:06:42 +03:00
Mika Westerberg 2b9941e089 PCI / thunderbolt: Switch to use device links instead of PCI quirk
On older Apple systems there is currently a PCI quirk in place to block
resume of tunneled PCIe ports until NHI (Thunderbolt controller) is
resumed. This makes sure the PCIe tunnels are re-established before PCI
core notices it.

With device links the same thing can be done without quirks. The driver
core will make sure the supplier (NHI) is resumed before consumers (PCIe
downstream ports).

For this reason switch the Thunderbolt driver to use device links and
remove the PCI quirk.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
2020-09-03 12:06:42 +03:00
Mika Westerberg b2911a593a thunderbolt: Enable wakes from system suspend
In order for the router and the whole domain to wake up from system
suspend states we need to enable wakes for the connected routers. For
device routers we enable wakes from PCIe and USB 3.x. This allows
devices such as keyboards connected to USB 3.x hub that is tunneled to
wake the system up as expected. For all routers we enabled wake on USB4
for each connected ports. This is used to propagate the wake from router
to another.

Do the same for legacy routers through link controller vendor specific
registers as documented in USB4 spec chapter 13.

While there correct kernel-doc of usb4_switch_set_sleep() -- it does not
enable wakes instead there is a separate function (usb4_switch_set_wake())
that does.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:42 +03:00
Mika Westerberg 341d45188a thunderbolt: Disable lane 1 for XDomain connection
USB4 spec mandates that the lane 1 should be disabled if lanes are not
bonded. For host-to-host connections (XDomain) we don't support lane
bonding so in order to be compatible with the spec, disable lane 1 when
another host is connected.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:41 +03:00
Mika Westerberg 284652a4a4 thunderbolt: Configure port for XDomain
When the port is connected to another host it should be marked as such
in the USB4 port capability. This information is used by the router
during sleep and wakeup.

Also do the same for legacy switches via link controller vendor specific
registers.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:41 +03:00
Mika Westerberg e28178bf56 thunderbolt: Set port configured for both ends of the link
Both ends of the link needs to have this set. Otherwise the link is not
re-established properly after sleep. Now since it is possible to have
mixed USB4 and Thunderbolt 1, 2 and 3 devices we need to split the link
configuration functionality to happen per port so we can pick the
correct implementation.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:41 +03:00
Mika Westerberg de46203917 thunderbolt: Configure link after lane bonding is enabled
During testing it was noticed that the link is not properly restored
after the domain exits sleep if the link configured bits are set before
lane bonding is enabled. The USB4 spec does not say in which order these
need to be set but setting link configured afterwards makes the link
restoration work so we do that instead.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:41 +03:00
Mika Westerberg 5cb6ed31c5 thunderbolt: Do not change default USB4 router notification timeout
Some early stage USB4 devices do not like that any of the enumerating
router config space fields (ROUTER_CS_1 - ROUTER_CS_4) are written after
the initial enumeration for example when entering sleep states. The
default timeout by the USB4 spec is 10 ms which should be fine for the
driver to handle.

For this reason do not change the notification timeout from the default
10 ms for USB4 routers.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:41 +03:00
Mika Westerberg 8145c4350e thunderbolt: Initialize TMU again on resume
The TMU will be reset after router exits sleep so in order to
re-configure it upon resume make sure the structure is initialized again
based on the current hardware state.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:41 +03:00
Mika Westerberg 81a2e3e49f thunderbolt: Tear down DP tunnels when suspending
DP tunnels do not need the same kind of treatment as others because they
are created based on hot-plug events on DP adapter ports, and the
display stack does not need the tunnels to be enabled when resuming from
suspend. Also Tiger Lake Thunderbolt controller sends unplug event on D3
exit so this avoids that as well.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:41 +03:00
Mika Westerberg 356b6c4ef5 thunderbolt: Send reset only to first generation routers
First generation routers may need the reset command upon resume but it
is not supported by newer generations.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:41 +03:00
Mika Westerberg 2ca3263a80 thunderbolt: No need to log an error if tb_switch_lane_bonding_enable() fails
The function already logs an error if it fails so get rid of the
duplication.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:40 +03:00
Mika Westerberg edfbd68bb5 thunderbolt: Do not program NFC buffers for USB4 router protocol adapters
USB4 spec says that NFC buffers field is not used for protocol adapters,
only for lane adapters so make tb_port_add_nfc_credits() skip non-lane
adapters in order to follow the spec.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:40 +03:00
Mika Westerberg 49f2a7f4cd thunderbolt: Use bit 31 to check if Firmware CM is running in Tiger Lake
In Tiger Lake the Firmware CM is always enabled (so bit 0 is always set)
but it may be in "pass through" mode which means it requires Software CM
instead. This can be determined by checking bit 31 instead.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:40 +03:00
Mika Westerberg 59ed8dcad6 thunderbolt: Software CM only should set force power in Tiger Lake
When Software CM is running it should not send any NHI mailbox command
during PM flows. Only force power bit needs to be set and cleared so
change Tiger Lake (well and Ice Lake) nhi_ops to take this into account.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:40 +03:00
Rajmohan Mani 6651c91de0 thunderbolt: Optimize NHI LC mailbox command processing
Currently the Ice Lake and Tiger Lake NHI (host controller) LC (link
controller) mailbox command processing checks for the completion of
command every 100 msecs. These controllers are found to complete this in
the order of 1 ms or so. Since this delay is in suspend path, surplus
delay is effectively affecting runtime PM suspend flows.

Optimize this so that we do the wait for 1 ms after reading the mailbox
register. This should make Ice Lake and Tiger Lake runtime suspend take
less time to complete.

Reported-by: Dana Alkattan <dana.alkattan@intel.com>
Signed-off-by: Rajmohan Mani <rajmohan.mani@intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:40 +03:00
Rajmohan Mani dc4c4bf122 thunderbolt: Optimize Force Power logic
Currently the "Force Power" logic uses 10 retries, each with a delay of
250 ms. Thunderbolt controllers in Ice Lake and Tiger Lake platforms are
found to complete this in the order of 3 ms or so. Since this delay
is in resume path, surplus delay is effectively affecting runtime PM
resume flows.

Decrease the granularity of the delay to 3 ms and increase the number of
retries so we wait maximum of ~1 s which is the recommended timeout.
This should make runtime resume a bit faster.

Reported-by: Dana Alkattan <dana.alkattan@intel.com>
Signed-off-by: Rajmohan Mani <rajmohan.mani@intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-03 12:06:40 +03:00
Tian Tao fff15f23b8 thunderbolt: Use kobj_to_dev() instead of container_of()
Doesn't really matter for an individual driver, but it may
get coppied to lots more. I consider it's a little tidy up.

Signed-off-by: Tian Tao <tiantao6@hisilicon.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-09-01 12:06:08 +03:00
Greg Kroah-Hartman 6b6c71e897 thunderbolt: Fixes for v5.9-rc4
This includes two fixes, one that fixes a regression around reboot and
 other that uses a correct link rate when USB3 bandwidth is reclaimed
 when the link is not up.
 
 Both have been in linux-next with no reported issues.
 -----BEGIN PGP SIGNATURE-----
 
 iQJUBAABCgA+FiEEVTdhRGBbNzLrSUBaAP2fSd+ZWKAFAl9N9lMgHG1pa2Eud2Vz
 dGVyYmVyZ0BsaW51eC5pbnRlbC5jb20ACgkQAP2fSd+ZWKDqFg//Rm8xBbFp+DFF
 LysW8K6ZCEbwUVZa0CvrDv2fSG/E//yYKstMOIuIZBs49LENNEGtQeyPwRkZmeyk
 LA1n6lxOefyLDHw/7Q4pd6mrxFTUBQoLYrTofuTck5b7HVABv1CpyhWjBmEijTy0
 wKcSEE2LWDJ4e6ROMrk6X6hYSYAIgfjWyjiiswqETuPB7s62Nnpv14cLc6//2thc
 OzRaa5qKJ3ldwswJZzjFDWriDIyIBQiEHHP8YM+QfiMNs+Fb/pP1Uxyg0OJSk6g3
 1qMV8b2VmSXdnT0APF0oyxkkB3zELvJo33RhRKgW9OTAhFkPSeqdxbydAe//MdVo
 AWjJ8hvRcFCxnXF9cKzA3HagUX4CVP/IXSH3L8kWR9PprRJvsGXKNA4Xna9Cx4FI
 qfPatQ73ATIBM2iwsFiYweawuO0Ti5JcPpRBfm1D4rG0yv/r7rN+3fBatHsrdYi4
 FMT+kRZI/GUi5LQFXLcG6UE8BesGipJJvGJTIkuKu7GQJ3RlLrZRLMkmM/wTx6be
 aahVT25OXUeZJ77y+RhgVQuikSWDTLS4TjXBa7/t0y+JuEkBjtoPlDbWn3IFGaRr
 oPnCkNnpFvTA8pnv7KWt9X8J5pZW3WNd35OmPlTilXBz1gbrH6+U0sSE5KsC6ii1
 EYewgZI+e8qZXGcwDw0qBBXJ8RqFWPI=
 =vxwa
 -----END PGP SIGNATURE-----

Merge tag 'thunderbolt-for-v5.9-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/westeri/thunderbolt into usb-linus

Mika writes:

thunderbolt: Fixes for v5.9-rc4

This includes two fixes, one that fixes a regression around reboot and
other that uses a correct link rate when USB3 bandwidth is reclaimed
when the link is not up.

Both have been in linux-next with no reported issues.

* tag 'thunderbolt-for-v5.9-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/westeri/thunderbolt:
  thunderbolt: Use maximum USB3 link rate when reclaiming if link is not up
  thunderbolt: Disable ports that are not implemented
2020-09-01 09:48:28 +02:00
Mika Westerberg 813050e0a9 thunderbolt: Use maximum USB3 link rate when reclaiming if link is not up
If the USB3 link is not up the actual link rate is 0 so when reclaiming
bandwidth we should look at maximum supported link rate instead.

Cc: stable@vger.kernel.org
Fixes: 0bd680cd90 ("thunderbolt: Add USB3 bandwidth management")
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-08-25 11:29:01 +03:00
Nikunj A. Dadhania 8824d19b45 thunderbolt: Disable ports that are not implemented
Commit 4caf2511ec ("thunderbolt: Add trivial .shutdown") exposes a bug
in the Thunderbolt driver, that frees an unallocated id, resulting in the
following spinlock bad magic bug.

[ 20.633803] BUG: spinlock bad magic on CPU#4, halt/3313
[ 20.640030] lock: 0xffff92e6ad5c97e0, .magic: 00000000, .owner: <none>/-1, .owner_cpu: 0
[ 20.672139] Call Trace:
[ 20.675032] dump_stack+0x97/0xdb
[ 20.678950] ? spin_bug+0xa5/0xb0
[ 20.682865] do_raw_spin_lock+0x68/0x98
[ 20.687397] _raw_spin_lock_irqsave+0x3f/0x5d
[ 20.692535] ida_destroy+0x4f/0x124
[ 20.696657] tb_switch_release+0x6d/0xfd
[ 20.701295] device_release+0x2c/0x7d
[ 20.705622] kobject_put+0x8e/0xac
[ 20.709637] tb_stop+0x55/0x66
[ 20.713243] tb_domain_remove+0x36/0x62
[ 20.717774] nhi_remove+0x4d/0x58

Fix the issue by disabling ports that are enabled as per the EEPROM, but
not implemented. While at it, update the kernel doc for the disabled
field, to reflect this.

Cc: stable@vger.kernel.org
Fixes: 4caf2511ec ("thunderbolt: Add trivial .shutdown")
Reported-by: Srikanth Nandamuri <srikanth.nandamuri@intel.com>
Signed-off-by: Nikunj A. Dadhania <nikunj.dadhania@linux.intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-08-25 11:28:03 +03:00
Gustavo A. R. Silva df561f6688 treewide: Use fallthrough pseudo-keyword
Replace the existing /* fall through */ comments and its variants with
the new pseudo-keyword macro fallthrough[1]. Also, remove unnecessary
fall-through markings when it is the case.

[1] https://www.kernel.org/doc/html/v5.7/process/deprecated.html?highlight=fallthrough#implicit-switch-case-fall-through

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
2020-08-23 17:36:59 -05:00
Stephen Rothwell 71fa1a4489 thunderbolt: merge fix for kunix_resource changes
Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-08-09 11:06:10 -07:00
Wei Yongjun ef7e12078a thunderbolt: Fix old style declaration warning
Fix gcc build warning:

drivers/thunderbolt/quirks.c:21:1: warning:
 'static' is not at beginning of declaration [-Wold-style-declaration]
   21 | const static struct tb_quirk tb_quirks[] = {
      | ^~~~~

Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-07-02 14:50:11 +03:00
Mario Limonciello 1cb3629383 thunderbolt: Add support for authenticate on disconnect
Some external devices can support completing thunderbolt authentication
when they are unplugged. For this to work though, the link controller must
remain operational.

The only device known to support this right now is the Dell WD19TB, so add
a quirk for this.

Signed-off-by: Mario Limonciello <mario.limonciello@dell.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-07-01 13:51:49 +03:00
Mario Limonciello 4b794f8066 thunderbolt: Add support for separating the flush to SPI and authenticate
This allows userspace to have a shorter period of time that the device
is unusable and to call it at a more convenient time.

For example flushing the image may happen while the user is using the
machine and authenticating/rebooting may happen while logging out.

Signed-off-by: Mario Limonciello <mario.limonciello@dell.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-07-01 13:51:49 +03:00
Colin Ian King 4c767ce48c thunderbolt: Ensure left shift of 512 does not overflow a 32 bit int
The 32 bit int value 512 is being left shifted and then used in a context
that expects the expression to be a larger unsigned long. There may be
a potential integer overflow, so make 512 a UL before shift to avoid
any such issues.

Addresses-Coverity: ("Uninintentional integer overflow")
Fixes: 3b1d8d577c ("thunderbolt: Implement USB3 bandwidth negotiation routines")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-07-01 13:46:47 +03:00
Kranthi Kuntala dacb12877d thunderbolt: Add support for on-board retimers
USB4 spec specifies standard access to retimers (both on-board and
cable) through USB4 port sideband access. This makes it possible to
upgrade their firmware in the same way than we already do with the
routers.

This enumerates on-board retimers under each USB4 port when the link
comes up and adds them to the bus under the router the retimer belongs
to. Retimers are exposed in sysfs with name like <device>:<port>.<index>
where device is the router the retimer belongs to, port is the USB4 port
the retimer is connected to and index is the retimer index under that
port (starting from 1). This applies to the upstream USB4 port as well
so if there is on-board retimer between the port and the router it is
also added accordingly.

At this time we do not add cable retimers but there is no techincal
restriction to do so in the future if needed. It is not clear whether it
makes sense to upgrade their firmwares and at least Thunderbolt 3 cables
it has not been done outside of lab environments.

The sysfs interface is made to follow the router NVM upgrade to make it
easy to extend the existing userspace (fwupd) to handle these as well.

Signed-off-by: Kranthi Kuntala <kranthi.kuntala@intel.com>
Co-developed-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:32 +03:00
Rajmohan Mani 02d12855f5 thunderbolt: Implement USB4 port sideband operations for retimer access
USB4 spec specifies standard set of sideband operations that are send
over the low speed link to access either retimers on the link or the
link parter (the other router). The USB4 retimer spec extends these and
adds operations for retimer NVM upgrade.

This implements the retimer access and NVM upgrade USB4 port sideband
operations which we need for retimer support in the patch that follows.

Signed-off-by: Rajmohan Mani <rajmohan.mani@intel.com>
Co-developed-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:32 +03:00
Mika Westerberg 6bfe33473e thunderbolt: Retry USB4 block read operation
Especially when accessing retimers over USB4 sideband operations the
possibility to get read errors seems to be higher so make the
usb4_do_read_data() retry a couple of times if it sees any other error
than -ENODEV (device is gone). We can only do this for read side because
it carries the offset as part of metadata (as opposed to writes).

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:32 +03:00
Mika Westerberg 7e72846bb9 thunderbolt: Generalize usb4_switch_do_[read|write]_data()
Currently these functions operate on struct tb_switch but we are going
to need the same functionality with retimers as well so make the two
functions work with an arbitrary object that gets passed as parameter to
the callbacks.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:32 +03:00
Mika Westerberg 719a5fe87e thunderbolt: Split common NVM functionality into a separate file
We are going to reuse some of this functionality to implement retimer
NVM upgrade so move common NVM functionality into its own file. We also
rename the structure from tb_switch_nvm to tb_nvm to make it clear that
it is not just for switches.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:32 +03:00
Mika Westerberg 83d1703634 thunderbolt: Add Intel USB-IF ID to the NVM upgrade supported list
With USB4 Intel is also using its USB-IF ID (0x8087) with the new
devices. The NVM format is the same. Add this to the driver so NVM
upgrade is possible with these devices as well.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:31 +03:00
Mika Westerberg 40c14d9f4f thunderbolt: Add KUnit tests for tunneling
We can test some parts of tunneling, like path allocation without access
to test hardware so add KUnit tests for PCIe, DP and USB3 tunneling.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:20 +03:00
Mika Westerberg 0bd680cd90 thunderbolt: Add USB3 bandwidth management
USB3 supports both isochronous and non-isochronous traffic. The former
requires guaranteed bandwidth and can take up to 90% of the total
bandwidth. With USB4 USB3 is tunneled over USB4 fabric which means that
we need to make sure there is enough bandwidth allocated for the USB3
tunnels in addition to DisplayPort tunnels.

Whereas DisplayPort bandwidth management is static and done before the
DP tunnel is established, the USB3 bandwidth management is dynamic and
allows increasing and decreasing the allocated bandwidth according to
what is currently consumed. This is done through host router USB3
downstream adapter registers.

This adds USB3 bandwidth management to the software connection manager
so that we always try to allocate maximum bandwidth for DP tunnels and
what is left is allocated for USB3.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:20 +03:00
Mika Westerberg 5b7b8c0af1 thunderbolt: Make tb_port_get_link_speed() available to other files
We need to call this from tb.c when we improve the bandwidth management
to take USB3 into account.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:20 +03:00
Mika Westerberg 3b1d8d577c thunderbolt: Implement USB3 bandwidth negotiation routines
Each host router USB3 downstream adapter has a set of registers that are
used to negotiate bandwidth between the connection manager and the
internal xHCI controller. These registers allow dynamic bandwidth
management for USB3 isochronous traffic based on what is actually
consumed vs. allocated at any given time.

Implement these USB3 bandwidth negotiation routines to allow the
software connection manager take advantage of these.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:20 +03:00
Mika Westerberg acf815b867 thunderbolt: Increase DP DPRX wait timeout
Sometimes it takes longer for DPRX to be set so increase the timeout to
cope with this.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:20 +03:00
Mika Westerberg 7c0ee8fd3b thunderbolt: Report consumed bandwidth in both directions
Whereas DisplayPort bandwidth is consumed only in one direction (from DP
IN adapter to DP OUT adapter), USB3 adds separate bandwidth for both
upstream and downstream directions.

For this reason extend the tunnel consumed bandwidth routines to support
both directions and implement this for DP.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:20 +03:00
Mika Westerberg 9cac51a049 thunderbolt: Make usb4_switch_map_pcie_down() also return enabled ports
Just for symmetry with the usb4_switch_map_usb3_down() make this one
also return ports that are enabled.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:20 +03:00
Mika Westerberg 77cfa40fcd thunderbolt: Make usb4_switch_map_usb3_down() also return enabled ports
We need to call this on enabled ports in order to find the mapping from
host router USB4 port to a USB 3.x downstream adapter, so make the
function return enabled ports as well.

While there fix parameter alignment in tb_find_usb3_down().

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:20 +03:00
Mika Westerberg bbcf40b392 thunderbolt: Do not tunnel USB3 if link is not USB4
USB3 tunneling is possible only over USB4 link so don't create USB3
tunnels if that's not the case.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:20 +03:00
Mika Westerberg e876f34adc thunderbolt: Add DP IN resources for all routers
USB4 spec allows DP tunneling from any router that has DP IN adapter,
not just from host router. The driver currently only added the DP IN
resources for the host router because Thunderbolt 1, 2 and 3 devices do
not have DP IN adapters. However, USB4 allows device routers to have DP
IN adapter as well so update the driver to add DP IN resources for each
device that has one. One example would be an eGPU enclosure where the
eGPU output is forwarded to DP IN port and then tunneled over the USB4
fabric.

Only limitation we add now is that the DP IN and DP OUT that gets paired
for tunnel creation should both be under the same topology starting from
host router downstream port. In other words we do not create DP tunnels
across host router at this time even though that is possible as well but
it complicates the bandwidth management and there is no real use-case
for this anyway.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:20 +03:00
Mika Westerberg 54509f5005 thunderbolt: Add KUnit tests for path walking
This adds KUnit tests for path walking which is only dependent on
software structures, so no hardware is needed to run these.

We make these available only when both KUnit and the driver itself are
built into the kernel image. The reason for this is that KUnit adds its
own module_init() call in kunit_test_suite() which generates linker
error because the driver does the same in nhi.c. This should be fine for
now because these tests are only meant to run by developers anyway.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
2020-06-22 19:58:20 +03:00