162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Thunderbolt driver - quirks
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2020 Mario Limonciello <mario.limonciello@dell.com>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include "tb.h"
962306a36Sopenharmony_ci
1062306a36Sopenharmony_cistatic void quirk_force_power_link(struct tb_switch *sw)
1162306a36Sopenharmony_ci{
1262306a36Sopenharmony_ci	sw->quirks |= QUIRK_FORCE_POWER_LINK_CONTROLLER;
1362306a36Sopenharmony_ci	tb_sw_dbg(sw, "forcing power to link controller\n");
1462306a36Sopenharmony_ci}
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cistatic void quirk_dp_credit_allocation(struct tb_switch *sw)
1762306a36Sopenharmony_ci{
1862306a36Sopenharmony_ci	if (sw->credit_allocation && sw->min_dp_main_credits == 56) {
1962306a36Sopenharmony_ci		sw->min_dp_main_credits = 18;
2062306a36Sopenharmony_ci		tb_sw_dbg(sw, "quirked DP main: %u\n", sw->min_dp_main_credits);
2162306a36Sopenharmony_ci	}
2262306a36Sopenharmony_ci}
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistatic void quirk_clx_disable(struct tb_switch *sw)
2562306a36Sopenharmony_ci{
2662306a36Sopenharmony_ci	sw->quirks |= QUIRK_NO_CLX;
2762306a36Sopenharmony_ci	tb_sw_dbg(sw, "disabling CL states\n");
2862306a36Sopenharmony_ci}
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_cistatic void quirk_usb3_maximum_bandwidth(struct tb_switch *sw)
3162306a36Sopenharmony_ci{
3262306a36Sopenharmony_ci	struct tb_port *port;
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci	if (tb_switch_is_icm(sw))
3562306a36Sopenharmony_ci		return;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	tb_switch_for_each_port(sw, port) {
3862306a36Sopenharmony_ci		if (!tb_port_is_usb3_down(port))
3962306a36Sopenharmony_ci			continue;
4062306a36Sopenharmony_ci		port->max_bw = 16376;
4162306a36Sopenharmony_ci		tb_port_dbg(port, "USB3 maximum bandwidth limited to %u Mb/s\n",
4262306a36Sopenharmony_ci			    port->max_bw);
4362306a36Sopenharmony_ci	}
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistruct tb_quirk {
4762306a36Sopenharmony_ci	u16 hw_vendor_id;
4862306a36Sopenharmony_ci	u16 hw_device_id;
4962306a36Sopenharmony_ci	u16 vendor;
5062306a36Sopenharmony_ci	u16 device;
5162306a36Sopenharmony_ci	void (*hook)(struct tb_switch *sw);
5262306a36Sopenharmony_ci};
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cistatic const struct tb_quirk tb_quirks[] = {
5562306a36Sopenharmony_ci	/* Dell WD19TB supports self-authentication on unplug */
5662306a36Sopenharmony_ci	{ 0x0000, 0x0000, 0x00d4, 0xb070, quirk_force_power_link },
5762306a36Sopenharmony_ci	{ 0x0000, 0x0000, 0x00d4, 0xb071, quirk_force_power_link },
5862306a36Sopenharmony_ci	/*
5962306a36Sopenharmony_ci	 * Intel Goshen Ridge NVM 27 and before report wrong number of
6062306a36Sopenharmony_ci	 * DP buffers.
6162306a36Sopenharmony_ci	 */
6262306a36Sopenharmony_ci	{ 0x8087, 0x0b26, 0x0000, 0x0000, quirk_dp_credit_allocation },
6362306a36Sopenharmony_ci	/*
6462306a36Sopenharmony_ci	 * Limit the maximum USB3 bandwidth for the following Intel USB4
6562306a36Sopenharmony_ci	 * host routers due to a hardware issue.
6662306a36Sopenharmony_ci	 */
6762306a36Sopenharmony_ci	{ 0x8087, PCI_DEVICE_ID_INTEL_ADL_NHI0, 0x0000, 0x0000,
6862306a36Sopenharmony_ci		  quirk_usb3_maximum_bandwidth },
6962306a36Sopenharmony_ci	{ 0x8087, PCI_DEVICE_ID_INTEL_ADL_NHI1, 0x0000, 0x0000,
7062306a36Sopenharmony_ci		  quirk_usb3_maximum_bandwidth },
7162306a36Sopenharmony_ci	{ 0x8087, PCI_DEVICE_ID_INTEL_RPL_NHI0, 0x0000, 0x0000,
7262306a36Sopenharmony_ci		  quirk_usb3_maximum_bandwidth },
7362306a36Sopenharmony_ci	{ 0x8087, PCI_DEVICE_ID_INTEL_RPL_NHI1, 0x0000, 0x0000,
7462306a36Sopenharmony_ci		  quirk_usb3_maximum_bandwidth },
7562306a36Sopenharmony_ci	{ 0x8087, PCI_DEVICE_ID_INTEL_MTL_M_NHI0, 0x0000, 0x0000,
7662306a36Sopenharmony_ci		  quirk_usb3_maximum_bandwidth },
7762306a36Sopenharmony_ci	{ 0x8087, PCI_DEVICE_ID_INTEL_MTL_P_NHI0, 0x0000, 0x0000,
7862306a36Sopenharmony_ci		  quirk_usb3_maximum_bandwidth },
7962306a36Sopenharmony_ci	{ 0x8087, PCI_DEVICE_ID_INTEL_MTL_P_NHI1, 0x0000, 0x0000,
8062306a36Sopenharmony_ci		  quirk_usb3_maximum_bandwidth },
8162306a36Sopenharmony_ci	{ 0x8087, PCI_DEVICE_ID_INTEL_BARLOW_RIDGE_HOST_80G_NHI, 0x0000, 0x0000,
8262306a36Sopenharmony_ci		  quirk_usb3_maximum_bandwidth },
8362306a36Sopenharmony_ci	{ 0x8087, PCI_DEVICE_ID_INTEL_BARLOW_RIDGE_HOST_40G_NHI, 0x0000, 0x0000,
8462306a36Sopenharmony_ci		  quirk_usb3_maximum_bandwidth },
8562306a36Sopenharmony_ci	{ 0x8087, PCI_DEVICE_ID_INTEL_BARLOW_RIDGE_HUB_80G_BRIDGE, 0x0000, 0x0000,
8662306a36Sopenharmony_ci		  quirk_usb3_maximum_bandwidth },
8762306a36Sopenharmony_ci	{ 0x8087, PCI_DEVICE_ID_INTEL_BARLOW_RIDGE_HUB_40G_BRIDGE, 0x0000, 0x0000,
8862306a36Sopenharmony_ci		  quirk_usb3_maximum_bandwidth },
8962306a36Sopenharmony_ci	/*
9062306a36Sopenharmony_ci	 * CLx is not supported on AMD USB4 Yellow Carp and Pink Sardine platforms.
9162306a36Sopenharmony_ci	 */
9262306a36Sopenharmony_ci	{ 0x0438, 0x0208, 0x0000, 0x0000, quirk_clx_disable },
9362306a36Sopenharmony_ci	{ 0x0438, 0x0209, 0x0000, 0x0000, quirk_clx_disable },
9462306a36Sopenharmony_ci	{ 0x0438, 0x020a, 0x0000, 0x0000, quirk_clx_disable },
9562306a36Sopenharmony_ci	{ 0x0438, 0x020b, 0x0000, 0x0000, quirk_clx_disable },
9662306a36Sopenharmony_ci};
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci/**
9962306a36Sopenharmony_ci * tb_check_quirks() - Check for quirks to apply
10062306a36Sopenharmony_ci * @sw: Thunderbolt switch
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci * Apply any quirks for the Thunderbolt controller.
10362306a36Sopenharmony_ci */
10462306a36Sopenharmony_civoid tb_check_quirks(struct tb_switch *sw)
10562306a36Sopenharmony_ci{
10662306a36Sopenharmony_ci	int i;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(tb_quirks); i++) {
10962306a36Sopenharmony_ci		const struct tb_quirk *q = &tb_quirks[i];
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci		if (q->hw_vendor_id && q->hw_vendor_id != sw->config.vendor_id)
11262306a36Sopenharmony_ci			continue;
11362306a36Sopenharmony_ci		if (q->hw_device_id && q->hw_device_id != sw->config.device_id)
11462306a36Sopenharmony_ci			continue;
11562306a36Sopenharmony_ci		if (q->vendor && q->vendor != sw->vendor)
11662306a36Sopenharmony_ci			continue;
11762306a36Sopenharmony_ci		if (q->device && q->device != sw->device)
11862306a36Sopenharmony_ci			continue;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci		tb_sw_dbg(sw, "running %ps\n", q->hook);
12162306a36Sopenharmony_ci		q->hook(sw);
12262306a36Sopenharmony_ci	}
12362306a36Sopenharmony_ci}
124