Skip to contentSkip to conversation

thunderbolt: thunderbolt: add vendor's NVM formats

PATCH Superseded Szuying Chen 5 participants 6 messages Aug 16, 2022 – Aug 17, 2022

Subsystems (2)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 thunderbolt: thunderbolt: add vendor's NVM formats

From: Szuying Chen <[email protected]>

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.

---Add enum nvm_validate_ops and modify ASMedia NVM Version format. ---Repair file(switch.c) has existed warning, but have 7 warn not fixed.

Note: The three previous submissions accidentally used the same subject prefix. This changelog is relative to the most recent submission at
https://github.com/intel-lab-lkp/linux/commits/Szuying-Chen/thunderbolt-thunderbolt-add-vendor-s-NVM-formats/20220815-121330

 drivers/thunderbolt/nvm.c    | 148 +++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/switch.c |  28 ++++++-
 drivers/thunderbolt/tb.h     |  23 ++++++
 3 files changed, 196 insertions(+), 3 deletions(-)
drivers/thunderbolt/nvm.c +148 -0
index b3f310389378..be8cbcfafb80 100644
@@ -9,11 +9,159 @@
 #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;
+
+	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, ASMEDIA_NVM_VERSION, &val, sizeof(val));
+		if (ret)
+			break;
+
+		nvm->nvm_asm.major = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (u8)(val >> 0x10));
+		ret = usb4_switch_nvm_read(sw, ASMEDIA_NVM_DATE, &val, sizeof(val));
+		if (ret)
+			break;
+
+		nvm->nvm_asm.minor = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (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:
+		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 ((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 mode);
+};
+
+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 i;
+	int res = 0;
+
+	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
drivers/thunderbolt/switch.c +25 -3
index 244f8cd38b25..de380fb5a166 100644
@@ -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.
@@ -391,10 +399,14 @@ static int tb_switch_nvm_add(struct tb_switch *sw)
 		return 0;
 
 	/*
-	 * The NVM format of non-Intel hardware is not known so
-	 * currently restrict NVM upgrade for Intel hardware. We may
-	 * relax this in the future when we learn other NVM formats.
+	 * The NVM format of Intel and Asmedia hardware are known so
+	 * currently restrict NVM upgrade for Intel and Asmedia hardware.
+	 * We may relax this in the future when we learn other NVM formats.
 	 */
+	ret = tb_nvm_validate(sw, NVM_ADD);
+	if (ret)
+		return ret;
+
 	if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL &&
 	    sw->config.vendor_id != 0x8087) {
 		dev_info(&sw->dev,
@@ -527,6 +539,7 @@ int tb_port_state(struct tb_port *port)
 {
 	struct tb_cap_phy phy;
 	int res;
+
 	if (port->cap_phy == 0) {
 		tb_port_WARN(port, "does not have a PHY\n");
 		return -EINVAL;
@@ -556,6 +569,7 @@ int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
 {
 	int retries = 10;
 	int state;
+
 	if (!port->cap_phy) {
 		tb_port_WARN(port, "does not have PHY\n");
 		return -EINVAL;
@@ -653,6 +667,7 @@ int tb_port_add_nfc_credits(struct tb_port *port, int credits)
 int tb_port_clear_counter(struct tb_port *port, int counter)
 {
 	u32 zero[3] = { 0, 0, 0 };
+
 	tb_port_dbg(port, "clearing counter %d\n", counter);
 	return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
 }
@@ -875,6 +890,7 @@ static inline bool tb_switch_is_reachable(const struct tb_switch *parent,
 					  const struct tb_switch *sw)
 {
 	u64 mask = (1ULL << parent->config.depth * 8) - 1;
+
 	return (tb_route(parent) & mask) == (tb_route(sw) & mask);
 }
 
@@ -1345,6 +1361,7 @@ bool tb_pci_port_is_enabled(struct tb_port *port)
 int tb_pci_port_enable(struct tb_port *port, bool enable)
 {
 	u32 word = enable ? ADP_PCIE_CS_0_PE : 0x0;
+
 	if (!port->cap_adap)
 		return -ENXIO;
 	return tb_port_write(port, &word, TB_CFG_PORT,
@@ -1918,6 +1935,7 @@ static ssize_t nvm_authenticate_store(struct device *dev,
 	struct device_attribute *attr, const char *buf, size_t count)
 {
 	int ret = nvm_authenticate_sysfs(dev, buf, false);
+
 	if (ret)
 		return ret;
 	return count;
@@ -1953,6 +1971,9 @@ static ssize_t nvm_version_show(struct device *dev,
 		ret = -ENODATA;
 	else if (!sw->nvm)
 		ret = -EAGAIN;
+	/*ASMedia NVM version show format xxxxxx_xxxxxx */
+	else if (sw->config.vendor_id == 0x174C)
+		ret = sprintf(buf, "%06x.%06x\n", sw->nvm->nvm_asm.major, sw->nvm->nvm_asm.minor);
 	else
 		ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
 
@@ -2860,6 +2881,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) {
drivers/thunderbolt/tb.h +23 -0
index 5db76de40cc1..7f5c8ae731a0 100644
@@ -28,6 +28,15 @@
 #define NVM_VERSION		0x08
 #define NVM_FLASH_SIZE		0x45
 
+/* ASMedia specific NVM offsets */
+#define ASMEDIA_NVM_VERSION	0x28
+#define ASMEDIA_NVM_DATE	0x1c
+
+struct nvm_asmedia {
+	u32 major;
+	u32 minor;
+};
+
 /**
  * struct tb_nvm - Structure holding NVM information
  * @dev: Owner of the NVM
@@ -57,6 +66,7 @@ struct tb_nvm {
 	size_t buf_data_size;
 	bool authenticating;
 	bool flushed;
+	struct nvm_asmedia nvm_asm;
 };
 
 enum tb_nvm_write_ops {
@@ -65,6 +75,18 @@ enum tb_nvm_write_ops {
 	AUTHENTICATE_ONLY = 3,
 };
 
+/*
+ * enum nvm_validate_ops - Nvm upgrade for each vendor
+ * @NVM_UPGRADE: Not prevent NVM upgrade.
+ * @NVM_ADD: Vendor's NVM device add.
+ * @NVM_WRITE: Vendor's NVM write.
+ */
+enum nvm_validate_ops {
+	NVM_UPGRADE = 0,
+	NVM_ADD = 1,
+	NVM_WRITE = 2,
+};
+
 #define TB_SWITCH_KEY_SIZE		32
 #define TB_SWITCH_MAX_DEPTH		6
 #define USB4_SWITCH_MAX_DEPTH		5
@@ -736,6 +758,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

maintainer
#1 lore raw
Quoted 11 lines · Szuying Chen

From: Szuying Chen <[email protected]>

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.

Quoted 7 lines

---Add enum nvm_validate_ops and modify ASMedia NVM Version format. ---Repair file(switch.c) has existed warning, but have 7 warn not fixed.

Note: The three previous submissions accidentally used the same subject prefix. This changelog is relative to the most recent submission at
https://github.com/intel-lab-lkp/linux/commits/Szuying-Chen/thunderbolt-thunderbolt-add-vendor-s-NVM-formats/20220815-121330

This needs to be below a line that _only_ has "---" on it.

See the documentation for examples, or one of the many commits on this mailing list.

Please fix up and resend a v5.

Also be sure to fix up all the issues the kernel test robot found in your previous submissions.

thanks,

greg k-h

maintainer
#2 lore raw
In reply to Szuying Chen
Quoted 25 lines · Szuying Chen
drivers/thunderbolt/switch.c +3 -0
@@ -527,6 +539,7 @@ int tb_port_state(struct tb_port *port)
 {
 	struct tb_cap_phy phy;
 	int res;
+
 	if (port->cap_phy == 0) {
 		tb_port_WARN(port, "does not have a PHY\n");
 		return -EINVAL;
@@ -556,6 +569,7 @@ int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
 {
 	int retries = 10;
 	int state;
+
 	if (!port->cap_phy) {
 		tb_port_WARN(port, "does not have PHY\n");
 		return -EINVAL;
@@ -653,6 +667,7 @@ int tb_port_add_nfc_credits(struct tb_port *port, int credits)
 int tb_port_clear_counter(struct tb_port *port, int counter)
 {
 	u32 zero[3] = { 0, 0, 0 };
+
 	tb_port_dbg(port, "clearing counter %d\n", counter);
 	return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
 }

drivers/thunderbolt/switch.c static inline bool tb_switch_is_reachable(const struct tb_switch *parent, · line 896

Quoted 7 lines
drivers/thunderbolt/switch.c +1 -0
@@ -875,6 +890,7 @@ static inline bool tb_switch_is_reachable(const struct tb_switch *parent,
 					  const struct tb_switch *sw)
 {
 	u64 mask = (1ULL << parent->config.depth * 8) - 1;
+
 	return (tb_route(parent) & mask) == (tb_route(sw) & mask);
 }
 

These changes have nothing to do with your patch submission, and should be a different patch entirely.

Remember, only do one-logical-thing per patch.

thanks,

greg k-h

#3 lore raw
In reply to Szuying Chen

Hi,

Quoted 89 lines · Szuying Chen

From: Szuying Chen <[email protected]>

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.

---Add enum nvm_validate_ops and modify ASMedia NVM Version format. ---Repair file(switch.c) has existed warning, but have 7 warn not fixed.

Note: The three previous submissions accidentally used the same subject prefix. This changelog is relative to the most recent submission at
https://github.com/intel-lab-lkp/linux/commits/Szuying-Chen/thunderbolt-thunderbolt-add-vendor-s-NVM-formats/20220815-121330

 drivers/thunderbolt/nvm.c    | 148 +++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/switch.c |  28 ++++++-
 drivers/thunderbolt/tb.h     |  23 ++++++
 3 files changed, 196 insertions(+), 3 deletions(-)
drivers/thunderbolt/nvm.c +52 -0
index b3f310389378..be8cbcfafb80 100644
@@ -9,11 +9,159 @@
 #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;

drivers/thunderbolt/nvm.c line 78

Quoted 10 lines
drivers/thunderbolt/nvm.c +10 -0
+
+	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);

This function does not only "validate" but it also creates the NVMem devices and whatnot.

Do you have some public description of the ASMedia format that I could take a look? Perhaps we can find some simpler way of validating the thing that works accross different vendors.

Quoted 175 lines
drivers/thunderbolt/nvm.c +86 -0
+		if (IS_ERR(nvm)) {
+			ret = PTR_ERR(nvm);
+			break;
+		}
+
+		ret = usb4_switch_nvm_read(sw, ASMEDIA_NVM_VERSION, &val, sizeof(val));
+		if (ret)
+			break;
+
+		nvm->nvm_asm.major = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (u8)(val >> 0x10));
+		ret = usb4_switch_nvm_read(sw, ASMEDIA_NVM_DATE, &val, sizeof(val));
+		if (ret)
+			break;
+
+		nvm->nvm_asm.minor = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (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:
+		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 ((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 mode);
+};
+
+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 i;
+	int res = 0;
+
+	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
drivers/thunderbolt/switch.c +21 -3
index 244f8cd38b25..de380fb5a166 100644
@@ -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.
@@ -391,10 +399,14 @@ static int tb_switch_nvm_add(struct tb_switch *sw)
 		return 0;
 
 	/*
-	 * The NVM format of non-Intel hardware is not known so
-	 * currently restrict NVM upgrade for Intel hardware. We may
-	 * relax this in the future when we learn other NVM formats.
+	 * The NVM format of Intel and Asmedia hardware are known so
+	 * currently restrict NVM upgrade for Intel and Asmedia hardware.
+	 * We may relax this in the future when we learn other NVM formats.
 	 */
+	ret = tb_nvm_validate(sw, NVM_ADD);
+	if (ret)
+		return ret;
+
 	if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL &&
 	    sw->config.vendor_id != 0x8087) {
 		dev_info(&sw->dev,
@@ -527,6 +539,7 @@ int tb_port_state(struct tb_port *port)
 {
 	struct tb_cap_phy phy;
 	int res;
+
 	if (port->cap_phy == 0) {
 		tb_port_WARN(port, "does not have a PHY\n");
 		return -EINVAL;
@@ -556,6 +569,7 @@ int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
 {
 	int retries = 10;
 	int state;
+
 	if (!port->cap_phy) {
 		tb_port_WARN(port, "does not have PHY\n");
 		return -EINVAL;
@@ -653,6 +667,7 @@ int tb_port_add_nfc_credits(struct tb_port *port, int credits)
 int tb_port_clear_counter(struct tb_port *port, int counter)
 {
 	u32 zero[3] = { 0, 0, 0 };
+
 	tb_port_dbg(port, "clearing counter %d\n", counter);
 	return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
 }
@@ -875,6 +890,7 @@ static inline bool tb_switch_is_reachable(const struct tb_switch *parent,
 					  const struct tb_switch *sw)
 {
 	u64 mask = (1ULL << parent->config.depth * 8) - 1;
+
 	return (tb_route(parent) & mask) == (tb_route(sw) & mask);
 }
 
@@ -1345,6 +1361,7 @@ bool tb_pci_port_is_enabled(struct tb_port *port)
 int tb_pci_port_enable(struct tb_port *port, bool enable)
 {
 	u32 word = enable ? ADP_PCIE_CS_0_PE : 0x0;
+
 	if (!port->cap_adap)
 		return -ENXIO;
 	return tb_port_write(port, &word, TB_CFG_PORT,
@@ -1918,6 +1935,7 @@ static ssize_t nvm_authenticate_store(struct device *dev,
 	struct device_attribute *attr, const char *buf, size_t count)
 {
 	int ret = nvm_authenticate_sysfs(dev, buf, false);
+
 	if (ret)
 		return ret;
 	return count;

drivers/thunderbolt/switch.c static ssize_t nvm_version_show(struct device *dev, · line 1976

Quoted 6 lines
drivers/thunderbolt/switch.c +3 -0
@@ -1953,6 +1971,9 @@ static ssize_t nvm_version_show(struct device *dev,
 		ret = -ENODATA;
 	else if (!sw->nvm)
 		ret = -EAGAIN;
+	/*ASMedia NVM version show format xxxxxx_xxxxxx */
+	else if (sw->config.vendor_id == 0x174C)
+		ret = sprintf(buf, "%06x.%06x\n", sw->nvm->nvm_asm.major, sw->nvm->nvm_asm.minor);

And yes, we can make the nvm->major/minor to be 32-bit integers too for both Intel and ASMedia and continue to use the %x.%x formatting.

Quoted 16 lines
drivers/thunderbolt/switch.c +1 -0
 	else
 		ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
 
@@ -2860,6 +2881,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) {
drivers/thunderbolt/tb.h +0 -0
index 5db76de40cc1..7f5c8ae731a0 100644

drivers/thunderbolt/tb.h line 33

Quoted 6 lines
drivers/thunderbolt/tb.h +3 -0
@@ -28,6 +28,15 @@
 #define NVM_VERSION		0x08
 #define NVM_FLASH_SIZE		0x45
 
+/* ASMedia specific NVM offsets */
+#define ASMEDIA_NVM_VERSION	0x28
+#define ASMEDIA_NVM_DATE	0x1c

Didn't I already commented about these? Are my emails somehow lost or they just get ignored?

Quoted 45 lines
drivers/thunderbolt/tb.h +19 -1
+struct nvm_asmedia {
+	u32 major;
+	u32 minor;
+};
+
 /**
  * struct tb_nvm - Structure holding NVM information
  * @dev: Owner of the NVM
@@ -57,6 +66,7 @@ struct tb_nvm {
 	size_t buf_data_size;
 	bool authenticating;
 	bool flushed;
+	struct nvm_asmedia nvm_asm;
 };
 
 enum tb_nvm_write_ops {
@@ -65,6 +75,18 @@ enum tb_nvm_write_ops {
 	AUTHENTICATE_ONLY = 3,
 };
 
+/*
+ * enum nvm_validate_ops - Nvm upgrade for each vendor
+ * @NVM_UPGRADE: Not prevent NVM upgrade.
+ * @NVM_ADD: Vendor's NVM device add.
+ * @NVM_WRITE: Vendor's NVM write.
+ */
+enum nvm_validate_ops {
+	NVM_UPGRADE = 0,
+	NVM_ADD = 1,
+	NVM_WRITE = 2,
+};
+
 #define TB_SWITCH_KEY_SIZE		32
 #define TB_SWITCH_MAX_DEPTH		6
 #define USB4_SWITCH_MAX_DEPTH		5
@@ -736,6 +758,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

#4 lore raw
In reply to Szuying Chen
Quoted 11 lines · Szuying Chen

From: Szuying Chen <[email protected]>

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.

Quoted 3 lines

---Add enum nvm_validate_ops and modify ASMedia NVM Version format. ---Repair file(switch.c) has existed warning, but have 7 warn not fixed.

Greg and Mika mentioned some of this already, but when you make a patch - it should fix one thing. Some more detail to help you though:

IOW one patch should fixup any whitespace mistakes, then another completely separate patch should include your changes. When you make the patch series you can use with a number argument for how many patches to make in your series. For example if you have 2 fixups followed by your new patch it should be:
'git format-patch -3 --subjet-prefix="PATCH v5"'

In addition to checkpatch another good habit is to visually inspect the patches beore sending them to ensure they only contain the intended changes. If you made a mistake, you can just use git rebase -i and fixup individual patches and then run 'git format-patch' again.

Quoted 260 lines

Note: The three previous submissions accidentally used the same subject prefix. This changelog is relative to the most recent submission at
https://github.com/intel-lab-lkp/linux/commits/Szuying-Chen/thunderbolt-thunderbolt-add-vendor-s-NVM-formats/20220815-121330

  drivers/thunderbolt/nvm.c    | 148 +++++++++++++++++++++++++++++++++++
  drivers/thunderbolt/switch.c |  28 ++++++-
  drivers/thunderbolt/tb.h     |  23 ++++++
  3 files changed, 196 insertions(+), 3 deletions(-)
drivers/thunderbolt/nvm.c +148 -0
index b3f310389378..be8cbcfafb80 100644
@@ -9,11 +9,159 @@
  #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;
+
+	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, ASMEDIA_NVM_VERSION, &val, sizeof(val));
+		if (ret)
+			break;
+
+		nvm->nvm_asm.major = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (u8)(val >> 0x10));
+		ret = usb4_switch_nvm_read(sw, ASMEDIA_NVM_DATE, &val, sizeof(val));
+		if (ret)
+			break;
+
+		nvm->nvm_asm.minor = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (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:
+		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 ((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 mode);
+};
+
+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 i;
+	int res = 0;
+
+	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
drivers/thunderbolt/switch.c +21 -3
index 244f8cd38b25..de380fb5a166 100644
@@ -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.
@@ -391,10 +399,14 @@ static int tb_switch_nvm_add(struct tb_switch *sw)
  		return 0;
 
  	/*
-	 * The NVM format of non-Intel hardware is not known so
-	 * currently restrict NVM upgrade for Intel hardware. We may
-	 * relax this in the future when we learn other NVM formats.
+	 * The NVM format of Intel and Asmedia hardware are known so
+	 * currently restrict NVM upgrade for Intel and Asmedia hardware.
+	 * We may relax this in the future when we learn other NVM formats.
  	 */
+	ret = tb_nvm_validate(sw, NVM_ADD);
+	if (ret)
+		return ret;
+
  	if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL &&
  	    sw->config.vendor_id != 0x8087) {
  		dev_info(&sw->dev,
@@ -527,6 +539,7 @@ int tb_port_state(struct tb_port *port)
  {
  	struct tb_cap_phy phy;
  	int res;
+
  	if (port->cap_phy == 0) {
  		tb_port_WARN(port, "does not have a PHY\n");
  		return -EINVAL;
@@ -556,6 +569,7 @@ int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
  {
  	int retries = 10;
  	int state;
+
  	if (!port->cap_phy) {
  		tb_port_WARN(port, "does not have PHY\n");
  		return -EINVAL;
@@ -653,6 +667,7 @@ int tb_port_add_nfc_credits(struct tb_port *port, int credits)
  int tb_port_clear_counter(struct tb_port *port, int counter)
  {
  	u32 zero[3] = { 0, 0, 0 };
+
  	tb_port_dbg(port, "clearing counter %d\n", counter);
  	return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
  }
@@ -875,6 +890,7 @@ static inline bool tb_switch_is_reachable(const struct tb_switch *parent,
  					  const struct tb_switch *sw)
  {
  	u64 mask = (1ULL << parent->config.depth * 8) - 1;
+
  	return (tb_route(parent) & mask) == (tb_route(sw) & mask);
  }
 
@@ -1345,6 +1361,7 @@ bool tb_pci_port_is_enabled(struct tb_port *port)
  int tb_pci_port_enable(struct tb_port *port, bool enable)
  {
  	u32 word = enable ? ADP_PCIE_CS_0_PE : 0x0;
+
  	if (!port->cap_adap)
  		return -ENXIO;
  	return tb_port_write(port, &word, TB_CFG_PORT,
@@ -1918,6 +1935,7 @@ static ssize_t nvm_authenticate_store(struct device *dev,
  	struct device_attribute *attr, const char *buf, size_t count)
  {
  	int ret = nvm_authenticate_sysfs(dev, buf, false);
+
  	if (ret)
  		return ret;
  	return count;

drivers/thunderbolt/switch.c static ssize_t nvm_version_show(struct device *dev, · line 1976

Quoted 6 lines
drivers/thunderbolt/switch.c +3 -0
@@ -1953,6 +1971,9 @@ static ssize_t nvm_version_show(struct device *dev,
  		ret = -ENODATA;
  	else if (!sw->nvm)
  		ret = -EAGAIN;
+	/*ASMedia NVM version show format xxxxxx_xxxxxx */
+	else if (sw->config.vendor_id == 0x174C)
+		ret = sprintf(buf, "%06x.%06x\n", sw->nvm->nvm_asm.major, sw->nvm->nvm_asm.minor);

Any reason to need the zero padding? If not, the existing

ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);

Should work for you too once if/when you expand the members to 32 bit. (see comment below)

Quoted 32 lines
drivers/thunderbolt/switch.c +1 -0
  	else
  		ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
 
@@ -2860,6 +2881,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) {
drivers/thunderbolt/tb.h +9 -0
index 5db76de40cc1..7f5c8ae731a0 100644
@@ -28,6 +28,15 @@
  #define NVM_VERSION		0x08
  #define NVM_FLASH_SIZE		0x45
 
+/* ASMedia specific NVM offsets */
+#define ASMEDIA_NVM_VERSION	0x28
+#define ASMEDIA_NVM_DATE	0x1c
+
+struct nvm_asmedia {
+	u32 major;
+	u32 minor;
+};
+
  /**
   * struct tb_nvm - Structure holding NVM information
   * @dev: Owner of the NVM

drivers/thunderbolt/tb.h struct tb_nvm { · line 69

Quoted 4 lines
drivers/thunderbolt/tb.h +1 -0
@@ -57,6 +66,7 @@ struct tb_nvm {
  	size_t buf_data_size;
  	bool authenticating;
  	bool flushed;
+	struct nvm_asmedia nvm_asm;

You shouldn't need to include this in the tb_nvm structure.
It already has "minor" and "major" members, so you can just use those ones.

I think it's better to expand those to be 32 bit instead of 8 bit if you require the extra 3 bytes instead of make new ones. It won't harm the Intel case to store a byte there.

Quoted 33 lines
drivers/thunderbolt/tb.h +13 -1
  };
 
  enum tb_nvm_write_ops {
@@ -65,6 +75,18 @@ enum tb_nvm_write_ops {
  	AUTHENTICATE_ONLY = 3,
  };
 
+/*
+ * enum nvm_validate_ops - Nvm upgrade for each vendor
+ * @NVM_UPGRADE: Not prevent NVM upgrade.
+ * @NVM_ADD: Vendor's NVM device add.
+ * @NVM_WRITE: Vendor's NVM write.
+ */
+enum nvm_validate_ops {
+	NVM_UPGRADE = 0,
+	NVM_ADD = 1,
+	NVM_WRITE = 2,
+};
+
  #define TB_SWITCH_KEY_SIZE		32
  #define TB_SWITCH_MAX_DEPTH		6
  #define USB4_SWITCH_MAX_DEPTH		5
@@ -736,6 +758,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

#5 lore raw
In reply to Szuying Chen
Thank you for the patch! Yet something to improve:

Hi Szuying,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.0-rc1 next-20220816]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Szuying-Chen/thunderbolt-thunderbolt-add-vendor-s-NVM-formats/20220816-193757
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 568035b01cfb107af8d2e4bd2fb9aea22cf5b868
config: s390-buildonly-randconfig-r004-20220815 (https://download.01.org/0day-ci/archive/20220817/[email protected]/config)
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project aed5e3bea138ce581d682158eb61c27b3cfdd6ec)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install s390 cross compiling tool for clang build
# apt-get install binutils-s390x-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/16b15f1135451c01b616a6a2bacf2f54a1082a08
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Szuying-Chen/thunderbolt-thunderbolt-add-vendor-s-NVM-formats/20220816-193757 git checkout 16b15f1135451c01b616a6a2bacf2f54a1082a08
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=s390 SHELL=/bin/bash drivers/thunderbolt/

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

In file included from drivers/thunderbolt/nvm.c:14:
In file included from drivers/thunderbolt/tb.h:13:
In file included from include/linux/pci.h:39:
In file included from include/linux/io.h:13:
In file included from arch/s390/include/asm/io.h:75:
include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]

           val = __raw_readb(PCI_IOBASE + addr);
                             ~~~~~~~~~~ ^
   include/asm-generic/io.h:560:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/big_endian.h:37:59: note: expanded from macro '__le16_to_cpu'
   #define __le16_to_cpu(x) __swab16((__force __u16)(__le16)(x))
                                                             ^
   include/uapi/linux/swab.h:102:54: note: expanded from macro '__swab16'
   #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
                                                        ^
   In file included from drivers/thunderbolt/nvm.c:14:
   In file included from drivers/thunderbolt/tb.h:13:
   In file included from include/linux/pci.h:39:
   In file included from include/linux/io.h:13:
   In file included from arch/s390/include/asm/io.h:75:
   include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/big_endian.h:35:59: note: expanded from macro '__le32_to_cpu'
   #define __le32_to_cpu(x) __swab32((__force __u32)(__le32)(x))
                                                             ^
   include/uapi/linux/swab.h:115:54: note: expanded from macro '__swab32'
   #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
                                                        ^
   In file included from drivers/thunderbolt/nvm.c:14:
   In file included from drivers/thunderbolt/tb.h:13:
   In file included from include/linux/pci.h:39:
   In file included from include/linux/io.h:13:
   In file included from arch/s390/include/asm/io.h:75:
   include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writeb(value, PCI_IOBASE + addr);
                               ~~~~~~~~~~ ^
   include/asm-generic/io.h:594:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
   include/asm-generic/io.h:604:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
   include/asm-generic/io.h:692:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           readsb(PCI_IOBASE + addr, buffer, count);
                  ~~~~~~~~~~ ^
   include/asm-generic/io.h:700:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           readsw(PCI_IOBASE + addr, buffer, count);
                  ~~~~~~~~~~ ^
   include/asm-generic/io.h:708:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           readsl(PCI_IOBASE + addr, buffer, count);
                  ~~~~~~~~~~ ^
   include/asm-generic/io.h:717:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           writesb(PCI_IOBASE + addr, buffer, count);
                   ~~~~~~~~~~ ^
   include/asm-generic/io.h:726:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           writesw(PCI_IOBASE + addr, buffer, count);
                   ~~~~~~~~~~ ^
   include/asm-generic/io.h:735:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           writesl(PCI_IOBASE + addr, buffer, count);
                   ~~~~~~~~~~ ^
   drivers/thunderbolt/nvm.c:107:3: error: expected expression
                   const u8 *buf = sw->nvm->buf;
                   ^
>> drivers/thunderbolt/nvm.c:109:8: error: use of undeclared identifier 'buf'
                   if (!buf) {
                        ^
   drivers/thunderbolt/nvm.c:118:38: error: use of undeclared identifier 'buf'
                   ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
                                                      ^

12 warnings and 3 errors generated.

vim +/buf +109 drivers/thunderbolt/nvm.c

    61	
    62	static int asmedia_nvm_validate(struct tb_switch *sw, unsigned int mode)
    63	{
    64		struct tb_nvm *nvm;
    65		u32 val;
    66		u32 nvm_size;
    67		int ret = 0;
    68		unsigned int image_size;
    69	
    70		switch (mode) {
    71		case NVM_UPGRADE:
    72			if (sw->no_nvm_upgrade)
    73				sw->no_nvm_upgrade = false;
    74	
    75			break;
    76	
    77		case NVM_ADD:
    78			nvm = tb_nvm_alloc(&sw->dev);
    79			if (IS_ERR(nvm)) {
    80				ret = PTR_ERR(nvm);
    81				break;
    82			}
    83	
    84			ret = usb4_switch_nvm_read(sw, ASMEDIA_NVM_VERSION, &val, sizeof(val));
    85			if (ret)
    86				break;
    87	
    88			nvm->nvm_asm.major = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (u8)(val >> 0x10));
    89			ret = usb4_switch_nvm_read(sw, ASMEDIA_NVM_DATE, &val, sizeof(val));
    90			if (ret)
    91				break;
    92	
    93			nvm->nvm_asm.minor = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (u8)(val >> 0x10));
    94			nvm_size = SZ_512K;
    95			ret = tb_nvm_add_active(nvm, nvm_size, tb_switch_nvm_read);
    96			if (ret)
    97				break;
    98	
    99			ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE, tb_switch_nvm_write);
   100			if (ret)
   101				break;
   102	
   103			sw->nvm = nvm;
   104			break;
   105	
   106		case NVM_WRITE:
   107			const u8 *buf = sw->nvm->buf;
   108	
 > 109			if (!buf) {

110 ret = -EINVAL;
111 break;
112 }
113 image_size = sw->nvm->buf_data_size;
114 if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE) {
115 ret = -EINVAL;
116 break;
117 }
118 ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
119 if (!ret)
120 sw->nvm->flushed = true;
121
122 break;
123
124 default:
125 break;
126 }
127
128 if ((mode == NVM_ADD) && (ret != 0))
129 tb_nvm_free(sw->nvm);
130
131 return ret;
132 }
133

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp