linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] thunderbolt: thunderbolt: add vendor's NVM formats
@ 2022-08-15  4:11 Szuying Chen
  2022-08-15  6:38 ` Greg KH
                   ` (4 more replies)
  0 siblings, 5 replies; 16+ messages in thread
From: Szuying Chen @ 2022-08-15  4:11 UTC (permalink / raw)
  To: gregkh, mika.westerberg, andreas.noever, michael.jamet,
	YehezkelShB, linux-usb, linux-kernel
  Cc: Yd_Tseng, Chloe_Chen, Richard_Hsu

From: Szuying Chen <Chloe_Chen@asmedia.com.tw>

The patch add tb_nvm_validate() contain an array that has functions
pointers to asmedia_nvm_validate().
And asmedia_nvm_validate() that recognize supported vendor works in one
of the following cases:
Case nvm_upgrade: enable nvm's attribute by setting no_nvm_upgrade
flag to create nvm_authenticate file node.
Case nvm_add:add active/non-active NVM devices.
Case nvm_write:update firmware to non-ative NVM device.

Our patches were through checkpatch.pl. But the file(switch.c.)
have existed 13 warning before we patch it.

Signed-off-by: Szuying Chen <Chloe_Chen@asmedia.com.tw>
---
 drivers/thunderbolt/nvm.c    | 147 +++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/switch.c |  17 ++++
 drivers/thunderbolt/tb.h     |  18 +++++
 3 files changed, 182 insertions(+)

diff --git a/drivers/thunderbolt/nvm.c b/drivers/thunderbolt/nvm.c
index b3f310389378..6db2034ec8e5 100644
--- a/drivers/thunderbolt/nvm.c
+++ b/drivers/thunderbolt/nvm.c
@@ -9,11 +9,158 @@
 #include <linux/idr.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
+#include <linux/pm_runtime.h>

 #include "tb.h"

 static DEFINE_IDA(nvm_ida);

+static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val,
+			      size_t bytes)
+{
+	struct tb_nvm *nvm = priv;
+	struct tb_switch *sw = tb_to_switch(nvm->dev);
+	int ret;
+
+	pm_runtime_get_sync(&sw->dev);
+	if (!mutex_trylock(&sw->tb->lock)) {
+		ret = restart_syscall();
+		goto out;
+	}
+	ret = usb4_switch_nvm_read(sw, offset, val, bytes);
+	mutex_unlock(&sw->tb->lock);
+
+out:
+	pm_runtime_mark_last_busy(&sw->dev);
+	pm_runtime_put_autosuspend(&sw->dev);
+
+	return ret;
+}
+
+static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
+			       size_t bytes)
+{
+	struct tb_nvm *nvm = priv;
+	struct tb_switch *sw = tb_to_switch(nvm->dev);
+	int ret;
+
+	if (!mutex_trylock(&sw->tb->lock))
+		return restart_syscall();
+
+	/*
+	 * Since writing the NVM image might require some special steps,
+	 * for example when CSS headers are written, we cache the image
+	 * locally here and handle the special cases when the user asks
+	 * us to authenticate the image.
+	 */
+	ret = tb_nvm_write_buf(nvm, offset, val, bytes);
+	mutex_unlock(&sw->tb->lock);
+
+	return ret;
+}
+
+static int asmedia_nvm_validate(struct tb_switch *sw, unsigned int mode)
+{
+	struct tb_nvm *nvm;
+	u32 val;
+	u32 nvm_size;
+	int ret = 0;
+	unsigned int image_size;
+	const u8 *buf = sw->nvm->buf;
+
+	switch (mode) {
+	case nvm_upgrade:
+		if (sw->no_nvm_upgrade)
+			sw->no_nvm_upgrade = false;
+
+		break;
+
+	case nvm_add:
+		nvm = tb_nvm_alloc(&sw->dev);
+		if (IS_ERR(nvm)) {
+			ret = PTR_ERR(nvm);
+			break;
+		}
+
+		ret = usb4_switch_nvm_read(sw, NVM_Date, &val, sizeof(val));
+		if (ret)
+			break;
+
+		nvm->nvm_asm.date = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (u8)(val >> 0x10));
+		ret = usb4_switch_nvm_read(sw, NVM_CUSTOMER_ID, &val, sizeof(val));
+		if (ret)
+			break;
+
+		nvm->nvm_asm.customerID = (((u8)val) << 0x8 | ((u8)(val >> 0x8)));
+		nvm->nvm_asm.version = (u8)(val >> 0x10);
+		nvm_size = SZ_512K;
+		ret = tb_nvm_add_active(nvm, nvm_size, tb_switch_nvm_read);
+		if (ret)
+			break;
+
+		ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE, tb_switch_nvm_write);
+		if (ret)
+			break;
+
+		sw->nvm = nvm;
+		break;
+
+	case nvm_write:
+		if (!buf) {
+			ret = -EINVAL;
+			break;
+		}
+		image_size = sw->nvm->buf_data_size;
+		if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE) {
+			ret = -EINVAL;
+			break;
+		}
+		ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
+		if (!ret)
+			sw->nvm->flushed = true;
+
+		break;
+
+	default:
+		break;
+	}
+
+	if ((mode == nvm_add) && (ret != 0))
+		tb_nvm_free(sw->nvm);
+
+	return ret;
+}
+
+struct tb_nvm_id {
+	u16 hw_vendor_id;
+	int (*validate)(struct tb_switch *sw, unsigned int handle);
+};
+
+static const struct tb_nvm_id tb_nvm_vendors[] = {
+	/* ASMedia software CM firmware upgrade */
+	{ 0x174c, asmedia_nvm_validate },
+};
+
+/**
+ * tb_nvm_vendor_handle() - support vendor's NVM format
+ * @sw: Thunderbolt switch
+ * @handle: 0:NvmUpgradeSuppport, 1:NvmAdd, 2:NvmWrite
+ */
+int tb_nvm_validate(struct tb_switch *sw, unsigned int mode)
+{
+	int res, i;
+
+	for (i = 0; i < ARRAY_SIZE(tb_nvm_vendors); i++) {
+		const struct tb_nvm_id *id = &tb_nvm_vendors[i];
+
+		if (id->hw_vendor_id && id->hw_vendor_id != sw->config.vendor_id)
+			continue;
+
+		 res = id->validate(sw, mode);
+	}
+	return res;
+}
+
 /**
  * tb_nvm_alloc() - Allocate new NVM structure
  * @dev: Device owning the NVM
diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index 244f8cd38b25..352e64f3dc92 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -114,6 +114,14 @@ static int nvm_validate_and_write(struct tb_switch *sw)
 	if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE)
 		return -EINVAL;

+	/*
+	 * Vendor's nvm write. If the image has been flushed to the
+	 * storage are, nvm write is complete.
+	 */
+	ret = tb_nvm_validate(sw, nvm_write);
+	if (sw->nvm->flushed)
+		return ret;
+
 	/*
 	 * FARB pointer must point inside the image and must at least
 	 * contain parts of the digital section we will be reading here.
@@ -390,6 +398,11 @@ static int tb_switch_nvm_add(struct tb_switch *sw)
 	if (!nvm_readable(sw))
 		return 0;

+	/* Vendor's NVM formats add */
+	ret = tb_nvm_validate(sw, nvm_add);
+	if (ret)
+		return ret;
+
 	/*
 	 * The NVM format of non-Intel hardware is not known so
 	 * currently restrict NVM upgrade for Intel hardware. We may
@@ -1953,6 +1966,9 @@ static ssize_t nvm_version_show(struct device *dev,
 		ret = -ENODATA;
 	else if (!sw->nvm)
 		ret = -EAGAIN;
+	/*ASMedia NVM version show format xxxxxx_xxxx_xx */
+	else if (sw->config.vendor_id == 0x174C)
+		ret = sprintf(buf, "%06x_%04x_%02x\n", sw->nvm->nvm_asm.date, sw->nvm->nvm_asm.customerID, sw->nvm->nvm_asm.version);
 	else
 		ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);

@@ -2860,6 +2876,7 @@ int tb_switch_add(struct tb_switch *sw)
 		tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);

 		tb_check_quirks(sw);
+		tb_nvm_validate(sw, nvm_upgrade);

 		ret = tb_switch_set_uuid(sw);
 		if (ret) {
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index 5db76de40cc1..7f20f10352d9 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -28,6 +28,22 @@
 #define NVM_VERSION		0x08
 #define NVM_FLASH_SIZE		0x45

+/* ASMedia specific NVM offsets */
+#define NVM_Date	0x1c
+#define NVM_CUSTOMER_ID	0x28
+
+/* ASMedia specific validation mode */
+#define nvm_upgrade 0
+#define nvm_add 1
+#define nvm_write 2
+
+struct nvm_asmedia {
+	u32 date;
+	u32 customerID:16;
+	u32 version:8;
+	u32 reserved:8;
+};
+
 /**
  * struct tb_nvm - Structure holding NVM information
  * @dev: Owner of the NVM
@@ -57,6 +73,7 @@ struct tb_nvm {
 	size_t buf_data_size;
 	bool authenticating;
 	bool flushed;
+	struct nvm_asmedia nvm_asm;
 };

 enum tb_nvm_write_ops {
@@ -736,6 +753,7 @@ static inline void tb_domain_put(struct tb *tb)
 	put_device(&tb->dev);
 }

+int tb_nvm_validate(struct tb_switch *sw, unsigned int mode);
 struct tb_nvm *tb_nvm_alloc(struct device *dev);
 int tb_nvm_add_active(struct tb_nvm *nvm, size_t size, nvmem_reg_read_t reg_read);
 int tb_nvm_write_buf(struct tb_nvm *nvm, unsigned int offset, void *val,
--
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread
* [PATCH] thunderbolt: thunderbolt: add vendor's NVM formats
@ 2022-08-10 10:17 Szuying Chen
  2022-08-10 10:41 ` Greg KH
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Szuying Chen @ 2022-08-10 10:17 UTC (permalink / raw)
  To: gregkh, mika.westerberg, andreas.noever, michael.jamet,
	YehezkelShB, linux-usb, linux-kernel
  Cc: Yd_Tseng, Chloe_Chen, Richard_Hsu, Szuying Chen

The patch add tb_nvm_quirks() contain an array that has functions
pointers to tb_nvm_vendor_apply().
And tb_nvm_vendor_apply() that recognize supported vendor works in one
of the following cases:
Case NvmUpgradeSuppport: enable nvm's attribute by setting
no_nvm_upgrade flag to create nvm_authenticate file node.
Case NvmAdd:add active/non-active NVM devices.
Case NvmWrite:update firmware to non-ative NVM device.

Signed-off-by: Szuying Chen <chensiying21@gmail.com>
---
 drivers/thunderbolt/nvm.c    | 59 ++++++++++++++----------------------
 drivers/thunderbolt/switch.c | 19 +++++++-----
 drivers/thunderbolt/tb.h     | 18 +++++++----
 3 files changed, 46 insertions(+), 50 deletions(-)

diff --git a/drivers/thunderbolt/nvm.c b/drivers/thunderbolt/nvm.c
index d5f283889da8..2aae8b3fa5dc 100644
--- a/drivers/thunderbolt/nvm.c
+++ b/drivers/thunderbolt/nvm.c
@@ -14,6 +14,7 @@
 #include "tb.h"

 static DEFINE_IDA(nvm_ida);
+
 static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val,
 			      size_t bytes)
 {
@@ -22,7 +23,6 @@ static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val,
 	int ret;

 	pm_runtime_get_sync(&sw->dev);
-
 	if (!mutex_trylock(&sw->tb->lock)) {
 		ret = restart_syscall();
 		goto out;
@@ -60,22 +60,23 @@ static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
 	return ret;
 }

 Refer to thunderbolt source: switch.c.

-static int tb_nvm_vendor_apply(struct tb_switch *sw, unsigned int handle)
+static int asm_nvm_apply(struct tb_switch *sw, unsigned int handle)
 {
+	struct tb_nvm *nvm;
+	u32 val;
+	u32 nvm_size;
 	int ret = 0;
+	unsigned int image_size;
+	const u8 *buf = sw->nvm->buf;

 	switch (handle) {
-	case 0:
+	case NvmUpgradeSuppport:
 		if (sw->no_nvm_upgrade)
 			sw->no_nvm_upgrade = false;

 		break;

-	case 1:
-		struct tb_nvm *nvm;
-		u32 val;
-		u32 nvm_size;
-
+	case NvmAdd:
 		nvm = tb_nvm_alloc(&sw->dev);
 		if (IS_ERR(nvm)) {
 			ret = PTR_ERR(nvm);
@@ -86,13 +87,13 @@ static int tb_nvm_vendor_apply(struct tb_switch *sw, unsigned int handle)
 		if (ret)
 			break;

-		nvm->vendor.date = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (u8)(val >> 0x10));
+		nvm->nvm_asm.date = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (u8)(val >> 0x10));
 		ret = usb4_switch_nvm_read(sw, NVM_CUSTOMER_ID, &val, sizeof(val));
 		if (ret)
 			break;

-		nvm->vendor.customerID = (((u8)val) << 0x8 | ((u8)(val >> 0x8)));
-		nvm->vendor.version = (u8)(val >> 0x10);
+		nvm->nvm_asm.customerID = (((u8)val) << 0x8 | ((u8)(val >> 0x8)));
+		nvm->nvm_asm.version = (u8)(val >> 0x10);
 		nvm_size = SZ_512K;
 		ret = tb_nvm_add_active(nvm, nvm_size, tb_switch_nvm_read);
 		if (ret)
@@ -105,10 +106,7 @@ static int tb_nvm_vendor_apply(struct tb_switch *sw, unsigned int handle)
 		sw->nvm = nvm;
 		break;

-	case 2:
-		unsigned int image_size;
-		const u8 *buf = sw->nvm->buf;
-
+	case NvmWrite:
 		if (!buf) {
 			ret = -EINVAL;
 			break;
@@ -128,7 +126,7 @@ static int tb_nvm_vendor_apply(struct tb_switch *sw, unsigned int handle)
 		break;
 	}

-	if ((handle == 1) && (ret != 0))
+	if ((handle == NvmAdd) && (ret != 0))
 		tb_nvm_free(sw->nvm);

 	return ret;
@@ -136,45 +134,32 @@ static int tb_nvm_vendor_apply(struct tb_switch *sw, unsigned int handle)

 struct tb_nvm_id {
 	u16 hw_vendor_id;
-	u16 hw_device_id;
-	u16 vendor;
-	u16 device;
 	int (*hook)(struct tb_switch *sw, unsigned int handle);
 };

 static const struct tb_nvm_id tb_nvm_vendors[] = {
 	/* ASMedia software CM firmware upgrade */
-	{ 0x174c, 0x2428, 0x174c, 0x0009, tb_nvm_vendor_apply },
+	{ 0x174c, asm_nvm_apply },
 };

 /**
- * tb_nvm_quirks() - support vendor's NVM format
+ * tb_nvm_vendor_handle() - support vendor's NVM format
  * @sw: Thunderbolt switch
+ * @handle: 0:NvmUpgradeSuppport, 1:NvmAdd, 2:NvmWrite
  */
-int tb_nvm_quirks(struct tb_switch *sw, unsigned int handle)
+int tb_nvm_vendor_handle(struct tb_switch *sw, unsigned int handle)
 {
 	int res, i;
-	bool NoVendor = true;

 	for (i = 0; i < ARRAY_SIZE(tb_nvm_vendors); i++) {
-		const struct tb_nvm_id *q = &tb_nvm_vendors[i];
+		const struct tb_nvm_id *id = &tb_nvm_vendors[i];

-		if (q->hw_vendor_id && q->hw_vendor_id != sw->config.vendor_id)
-			continue;
-		if (q->hw_device_id && q->hw_device_id != sw->config.device_id)
-			continue;
-		if (q->vendor && q->vendor != sw->vendor)
-			continue;
-		if (q->device && q->device != sw->device)
+		if (id->hw_vendor_id && id->hw_vendor_id != sw->config.vendor_id)
 			continue;

-		 res = q->hook(sw, handle);
-		 NoVendor = false;
+		 res = id->hook(sw, handle);
 	}
-	if (NoVendor)
-		return -ENODEV;
-	else
-		return res;
+	return res;
 }

 /**
diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index a654b6951bef..8fa9efa8a900 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -114,8 +114,12 @@ static int nvm_validate_and_write(struct tb_switch *sw)
 	if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE)
 		return -EINVAL;

-	ret = tb_nvm_quirks(sw, 2);
-	if (ret != -ENODEV)
+	/*
+	 * Vendor's nvm write. If the image has been flushed to the
+	 * storage are, nvm write is complete.
+	 */
+	ret = tb_nvm_vendor_handle(sw, NvmWrite);
+	if (sw->nvm->flushed)
 		return ret;

 	/*
@@ -394,9 +398,9 @@ static int tb_switch_nvm_add(struct tb_switch *sw)
 	if (!nvm_readable(sw))
 		return 0;

-	/* Vendor's NVM formats definition */
-	ret = tb_nvm_quirks(sw, 1);
-	if (ret != -ENODEV)
+	/* Vendor's NVM formats add */
+	ret = tb_nvm_vendor_handle(sw, NvmAdd);
+	if (ret)
 		return ret;

 	/*
@@ -1962,8 +1966,9 @@ static ssize_t nvm_version_show(struct device *dev,
 		ret = -ENODATA;
 	else if (!sw->nvm)
 		ret = -EAGAIN;
+	/*ASMedia NVM version show format xxxxxx_xxxx_xx */
 	else if (sw->config.vendor_id == 0x174C)
-		ret = sprintf(buf, "%06x_%04x_%02x\n", sw->nvm->vendor.date, sw->nvm->vendor.customerID, sw->nvm->vendor.version);
+		ret = sprintf(buf, "%06x_%04x_%02x\n", sw->nvm->nvm_asm.date, sw->nvm->nvm_asm.customerID, sw->nvm->nvm_asm.version);
 	else
 		ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);

@@ -2871,7 +2876,7 @@ int tb_switch_add(struct tb_switch *sw)
 		tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);

 		tb_check_quirks(sw);
-		tb_nvm_quirks(sw, 0);
+		tb_nvm_vendor_handle(sw, NvmUpgradeSuppport);

 		ret = tb_switch_set_uuid(sw);
 		if (ret) {
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index 9dd1ba7b07b6..a8dd0ae04741 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -31,10 +31,16 @@
 #define NVM_Date	0x1C
 #define NVM_CUSTOMER_ID	0x28

-struct tb_nvm_vendor {
-	int	date;
-	u16	customerID;
-	u8	version;
+/* ASMedia specific NVM handle */
+#define NvmUpgradeSuppport 0
+#define NvmAdd 1
+#define NvmWrite 2
+
+struct nvm_asmedia {
+	u32 date;
+	u32 customerID:16;
+	u32 version:8;
+	u32 reserved:8;
 };

 /**
@@ -66,7 +72,7 @@ struct tb_nvm {
 	size_t buf_data_size;
 	bool authenticating;
 	bool flushed;
-	struct tb_nvm_vendor vendor;
+	struct nvm_asmedia nvm_asm;
 };

 enum tb_nvm_write_ops {
@@ -746,7 +752,7 @@ static inline void tb_domain_put(struct tb *tb)
 	put_device(&tb->dev);
 }

-int tb_nvm_quirks(struct tb_switch *sw, unsigned int handle);
+int tb_nvm_vendor_handle(struct tb_switch *sw, unsigned int handle);
 struct tb_nvm *tb_nvm_alloc(struct device *dev);
 int tb_nvm_add_active(struct tb_nvm *nvm, size_t size, nvmem_reg_read_t reg_read);
 int tb_nvm_write_buf(struct tb_nvm *nvm, unsigned int offset, void *val,
--
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread
* [PATCH] thunderbolt: thunderbolt: add vendor's NVM formats
@ 2022-08-05  9:22 Szuying Chen
  2022-08-05  9:31 ` Greg KH
                   ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: Szuying Chen @ 2022-08-05  9:22 UTC (permalink / raw)
  To: andreas.noever, michael.jamet, mika.westerberg, YehezkelShB,
	linux-usb, linux-kernel
  Cc: Yd_Tseng, Richard_Hsu, Chloe_Chen

The patch add tb_nvm_quirks() contain an array that has functions pointers to tb_nvm_vendor_apply().
And tb_nvm_vendor_apply() that recognize supported vendor works in one of the following cases:
Case 0:enable nvm's attribute by setting no_nvm_upgrade flag to create nvm_authenticate file node.
Case 1:add active/non-active NVM devices.
Case 2:update firmware to non-ative NVM device.

Signed-off-by: Szuying Chen <Chloe_Chen@asmedia.com.tw>
---
 drivers/thunderbolt/nvm.c    | 163 +++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/switch.c |  12 +++
 drivers/thunderbolt/tb.h     |  12 +++
 3 files changed, 187 insertions(+)

diff --git a/drivers/thunderbolt/nvm.c b/drivers/thunderbolt/nvm.c
index b3f310389378..d5f283889da8 100644
--- a/drivers/thunderbolt/nvm.c
+++ b/drivers/thunderbolt/nvm.c
@@ -9,10 +9,173 @@
 #include <linux/idr.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
+#include <linux/pm_runtime.h>

 #include "tb.h"

 static DEFINE_IDA(nvm_ida);
+static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val,
+			      size_t bytes)
+{
+	struct tb_nvm *nvm = priv;
+	struct tb_switch *sw = tb_to_switch(nvm->dev);
+	int ret;
+
+	pm_runtime_get_sync(&sw->dev);
+
+	if (!mutex_trylock(&sw->tb->lock)) {
+		ret = restart_syscall();
+		goto out;
+	}
+
+	ret = usb4_switch_nvm_read(sw, offset, val, bytes);
+	mutex_unlock(&sw->tb->lock);
+
+out:
+	pm_runtime_mark_last_busy(&sw->dev);
+	pm_runtime_put_autosuspend(&sw->dev);
+
+	return ret;
+}
+
+static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
+			       size_t bytes)
+{
+	struct tb_nvm *nvm = priv;
+	struct tb_switch *sw = tb_to_switch(nvm->dev);
+	int ret;
+
+	if (!mutex_trylock(&sw->tb->lock))
+		return restart_syscall();
+
+	/*
+	 * Since writing the NVM image might require some special steps,
+	 * for example when CSS headers are written, we cache the image
+	 * locally here and handle the special cases when the user asks
+	 * us to authenticate the image.
+	 */
+	ret = tb_nvm_write_buf(nvm, offset, val, bytes);
+	mutex_unlock(&sw->tb->lock);
+
+	return ret;
+}
+
+static int tb_nvm_vendor_apply(struct tb_switch *sw, unsigned int handle)
+{
+	int ret = 0;
+
+	switch (handle) {
+	case 0:
+		if (sw->no_nvm_upgrade)
+			sw->no_nvm_upgrade = false;
+
+		break;
+
+	case 1:
+		struct tb_nvm *nvm;
+		u32 val;
+		u32 nvm_size;
+
+		nvm = tb_nvm_alloc(&sw->dev);
+		if (IS_ERR(nvm)) {
+			ret = PTR_ERR(nvm);
+			break;
+		}
+
+		ret = usb4_switch_nvm_read(sw, NVM_Date, &val, sizeof(val));
+		if (ret)
+			break;
+
+		nvm->vendor.date = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (u8)(val >> 0x10));
+		ret = usb4_switch_nvm_read(sw, NVM_CUSTOMER_ID, &val, sizeof(val));
+		if (ret)
+			break;
+
+		nvm->vendor.customerID = (((u8)val) << 0x8 | ((u8)(val >> 0x8)));
+		nvm->vendor.version = (u8)(val >> 0x10);
+		nvm_size = SZ_512K;
+		ret = tb_nvm_add_active(nvm, nvm_size, tb_switch_nvm_read);
+		if (ret)
+			break;
+
+		ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE, tb_switch_nvm_write);
+		if (ret)
+			break;
+
+		sw->nvm = nvm;
+		break;
+
+	case 2:
+		unsigned int image_size;
+		const u8 *buf = sw->nvm->buf;
+
+		if (!buf) {
+			ret = -EINVAL;
+			break;
+		}
+		image_size = sw->nvm->buf_data_size;
+		if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE) {
+			ret = -EINVAL;
+			break;
+		}
+		ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
+		if (!ret)
+			sw->nvm->flushed = true;
+
+		break;
+
+	default:
+		break;
+	}
+
+	if ((handle == 1) && (ret != 0))
+		tb_nvm_free(sw->nvm);
+
+	return ret;
+}
+
+struct tb_nvm_id {
+	u16 hw_vendor_id;
+	u16 hw_device_id;
+	u16 vendor;
+	u16 device;
+	int (*hook)(struct tb_switch *sw, unsigned int handle);
+};
+
+static const struct tb_nvm_id tb_nvm_vendors[] = {
+	/* ASMedia software CM firmware upgrade */
+	{ 0x174c, 0x2428, 0x174c, 0x0009, tb_nvm_vendor_apply },
+};
+
+/**
+ * tb_nvm_quirks() - support vendor's NVM format
+ * @sw: Thunderbolt switch
+ */
+int tb_nvm_quirks(struct tb_switch *sw, unsigned int handle)
+{
+	int res, i;
+	bool NoVendor = true;
+
+	for (i = 0; i < ARRAY_SIZE(tb_nvm_vendors); i++) {
+		const struct tb_nvm_id *q = &tb_nvm_vendors[i];
+
+		if (q->hw_vendor_id && q->hw_vendor_id != sw->config.vendor_id)
+			continue;
+		if (q->hw_device_id && q->hw_device_id != sw->config.device_id)
+			continue;
+		if (q->vendor && q->vendor != sw->vendor)
+			continue;
+		if (q->device && q->device != sw->device)
+			continue;
+
+		 res = q->hook(sw, handle);
+		 NoVendor = false;
+	}
+	if (NoVendor)
+		return -ENODEV;
+	else
+		return res;
+}

 /**
  * tb_nvm_alloc() - Allocate new NVM structure
diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index 561e1d77240e..a654b6951bef 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -114,6 +114,10 @@ static int nvm_validate_and_write(struct tb_switch *sw)
 	if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE)
 		return -EINVAL;

+	ret = tb_nvm_quirks(sw, 2);
+	if (ret != -ENODEV)
+		return ret;
+
 	/*
 	 * FARB pointer must point inside the image and must at least
 	 * contain parts of the digital section we will be reading here.
@@ -390,6 +394,11 @@ static int tb_switch_nvm_add(struct tb_switch *sw)
 	if (!nvm_readable(sw))
 		return 0;

+	/* Vendor's NVM formats definition */
+	ret = tb_nvm_quirks(sw, 1);
+	if (ret != -ENODEV)
+		return ret;
+
 	/*
 	 * The NVM format of non-Intel hardware is not known so
 	 * currently restrict NVM upgrade for Intel hardware. We may
@@ -1953,6 +1962,8 @@ static ssize_t nvm_version_show(struct device *dev,
 		ret = -ENODATA;
 	else if (!sw->nvm)
 		ret = -EAGAIN;
+	else if (sw->config.vendor_id == 0x174C)
+		ret = sprintf(buf, "%06x_%04x_%02x\n", sw->nvm->vendor.date, sw->nvm->vendor.customerID, sw->nvm->vendor.version);
 	else
 		ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);

@@ -2860,6 +2871,7 @@ int tb_switch_add(struct tb_switch *sw)
 		tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);

 		tb_check_quirks(sw);
+		tb_nvm_quirks(sw, 0);

 		ret = tb_switch_set_uuid(sw);
 		if (ret) {
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index a831faa50f65..9dd1ba7b07b6 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -27,6 +27,16 @@
 #define NVM_VERSION		0x08
 #define NVM_FLASH_SIZE		0x45

+/* ASMedia specific NVM offsets */
+#define NVM_Date	0x1C
+#define NVM_CUSTOMER_ID	0x28
+
+struct tb_nvm_vendor {
+	int	date;
+	u16	customerID;
+	u8	version;
+};
+
 /**
  * struct tb_nvm - Structure holding NVM information
  * @dev: Owner of the NVM
@@ -56,6 +66,7 @@ struct tb_nvm {
 	size_t buf_data_size;
 	bool authenticating;
 	bool flushed;
+	struct tb_nvm_vendor vendor;
 };

 enum tb_nvm_write_ops {
@@ -735,6 +746,7 @@ static inline void tb_domain_put(struct tb *tb)
 	put_device(&tb->dev);
 }

+int tb_nvm_quirks(struct tb_switch *sw, unsigned int handle);
 struct tb_nvm *tb_nvm_alloc(struct device *dev);
 int tb_nvm_add_active(struct tb_nvm *nvm, size_t size, nvmem_reg_read_t reg_read);
 int tb_nvm_write_buf(struct tb_nvm *nvm, unsigned int offset, void *val,
--
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2022-08-16  7:18 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-15  4:11 [PATCH] thunderbolt: thunderbolt: add vendor's NVM formats Szuying Chen
2022-08-15  6:38 ` Greg KH
2022-08-15  6:40 ` Greg KH
2022-08-15 13:12 ` kernel test robot
2022-08-15 17:28 ` Limonciello, Mario
2022-08-15 19:20   ` Limonciello, Mario
2022-08-16  4:50 ` kernel test robot
  -- strict thread matches above, loose matches on Subject: below --
2022-08-10 10:17 Szuying Chen
2022-08-10 10:41 ` Greg KH
2022-08-10 10:41 ` Greg KH
2022-08-10 10:56 ` Mika Westerberg
2022-08-05  9:22 Szuying Chen
2022-08-05  9:31 ` Greg KH
2022-08-05 10:20 ` Mika Westerberg
2022-08-07  9:40 ` kernel test robot
2022-08-07 10:33 ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).