diff mbox series

thunderbolt: thunderbolt: add vendor's NVM formats

Message ID 20220805092224.2536-1-Chloe_Chen@asmedia.com.tw (mailing list archive)
State Superseded
Headers show
Series thunderbolt: thunderbolt: add vendor's NVM formats | expand

Commit Message

Szuying Chen Aug. 5, 2022, 9:22 a.m. UTC
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(+)

--
2.34.1

Comments

Greg KH Aug. 5, 2022, 9:31 a.m. UTC | #1
On Fri, Aug 05, 2022 at 05:22:24PM +0800, Szuying Chen wrote:
> 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.

Please wrap your changelog text at 72 columns.

> 
> Signed-off-by: Szuying Chen <Chloe_Chen@asmedia.com.tw>

This does not match with your From: line in your email.  Please put the
correct From: line in the changelog text as the documentation suggests
when you can't use your corporate email system for patches.

> ---
>  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)

Please put the blank line back after the DEFINE_IDA line.


> +{
> +	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();

Why not just wait for the lock?  What does restarting help with?

> +		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)

What is a handle here?  Why didn't you document it in the comments for
this function?

> +{
> +	int res, i;
> +	bool NoVendor = true;

Please always use checkpatch for your changes before you send them out.


> +
> +	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;

There should be a thunderbolt match device function somewhere already,
right?  If not, I suggest making one here.

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

No need, if there are no quirks, that's fine, don't return an error.

> +	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);

What is the "2" for?

> +	if (ret != -ENODEV)
> +		return ret;

Again, no need for -ENODEV checking here.

> +
>  	/*
>  	 * 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);

What is the 1 for?

> +	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)

Don't you have a PCI id somewhere for this value?

> +		ret = sprintf(buf, "%06x_%04x_%02x\n", sw->nvm->vendor.date, sw->nvm->vendor.customerID, sw->nvm->vendor.version);

Why is this vendor special for a different string?

>  	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);

What is 0 here?

> 
>  		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 {

If this is ASMedia specific, please say that in the structure name.

> +	int	date;
> +	u16	customerID;

__le16?  Or is this really native-cpu-endian?

> +	u8	version;
> +};

thanks,

greg k-h
Mika Westerberg Aug. 5, 2022, 10:20 a.m. UTC | #2
Hi,

On Fri, Aug 05, 2022 at 05:22:24PM +0800, Szuying Chen wrote:
> 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:

In addition to what Greg already commented. These are not quirks so
don't call them that. They are vendor specific means to identify and
verify the NVM firmware.

> 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)

Things like these should not be needed once we have the table for
supported vendor NVM formats.

> +		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

These should not be in tb.h at all. They should be in nvm.c.

> +
> +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
kernel test robot Aug. 7, 2022, 9:40 a.m. UTC | #3
Hi Szuying,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.19 next-20220805]
[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/20220805-172732
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git b2a88c212e652e94f1e4b635910972ac57ba4e97
config: i386-randconfig-a015 (https://download.01.org/0day-ci/archive/20220807/202208071758.wZC5VHIZ-lkp@intel.com/config)
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 26dd42705c2af0b8f6e5d6cdb32c9bd5ed9524eb)
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
        # https://github.com/intel-lab-lkp/linux/commit/cc64350d27db3b30e5ceef2b5d6a3c48b9a4d989
        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/20220805-172732
        git checkout cc64350d27db3b30e5ceef2b5d6a3c48b9a4d989
        # 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=i386 SHELL=/bin/bash drivers/thunderbolt/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   drivers/thunderbolt/nvm.c:75:3: error: expected expression
                   struct tb_nvm *nvm;
                   ^
   drivers/thunderbolt/nvm.c:79:3: error: use of undeclared identifier 'nvm'
                   nvm = tb_nvm_alloc(&sw->dev);
                   ^
   drivers/thunderbolt/nvm.c:80:14: error: use of undeclared identifier 'nvm'
                   if (IS_ERR(nvm)) {
                              ^
   drivers/thunderbolt/nvm.c:81:18: error: use of undeclared identifier 'nvm'
                           ret = PTR_ERR(nvm);
                                         ^
   drivers/thunderbolt/nvm.c:89:3: error: use of undeclared identifier 'nvm'
                   nvm->vendor.date = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (u8)(val >> 0x10));
                   ^
   drivers/thunderbolt/nvm.c:94:3: error: use of undeclared identifier 'nvm'
                   nvm->vendor.customerID = (((u8)val) << 0x8 | ((u8)(val >> 0x8)));
                   ^
   drivers/thunderbolt/nvm.c:95:3: error: use of undeclared identifier 'nvm'
                   nvm->vendor.version = (u8)(val >> 0x10);
                   ^
   drivers/thunderbolt/nvm.c:97:27: error: use of undeclared identifier 'nvm'
                   ret = tb_nvm_add_active(nvm, nvm_size, tb_switch_nvm_read);
                                           ^
   drivers/thunderbolt/nvm.c:101:31: error: use of undeclared identifier 'nvm'
                   ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE, tb_switch_nvm_write);
                                               ^
   drivers/thunderbolt/nvm.c:105:13: error: use of undeclared identifier 'nvm'
                   sw->nvm = nvm;
                             ^
   drivers/thunderbolt/nvm.c:109:3: error: expected expression
                   unsigned int image_size;
                   ^
   drivers/thunderbolt/nvm.c:116:3: error: use of undeclared identifier 'image_size'
                   image_size = sw->nvm->buf_data_size;
                   ^
   drivers/thunderbolt/nvm.c:117:7: error: use of undeclared identifier 'image_size'; did you mean 'page_size'?
                   if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE) {
                       ^~~~~~~~~~
                       page_size
   include/linux/mm.h:927:29: note: 'page_size' declared here
   static inline unsigned long page_size(struct page *page)
                               ^
   drivers/thunderbolt/nvm.c:117:36: error: use of undeclared identifier 'image_size'; did you mean 'page_size'?
                   if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE) {
                                                    ^~~~~~~~~~
                                                    page_size
   include/linux/mm.h:927:29: note: 'page_size' declared here
   static inline unsigned long page_size(struct page *page)
                               ^
   drivers/thunderbolt/nvm.c:121:43: error: use of undeclared identifier 'image_size'; did you mean 'page_size'?
                   ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
                                                           ^~~~~~~~~~
                                                           page_size
   include/linux/mm.h:927:29: note: 'page_size' declared here
   static inline unsigned long page_size(struct page *page)
                               ^
>> drivers/thunderbolt/nvm.c:76:7: warning: mixing declarations and code is incompatible with standards before C99 [-Wdeclaration-after-statement]
                   u32 val;
                       ^
   1 warning and 15 errors generated.


vim +76 drivers/thunderbolt/nvm.c

    62	
    63	static int tb_nvm_vendor_apply(struct tb_switch *sw, unsigned int handle)
    64	{
    65		int ret = 0;
    66	
    67		switch (handle) {
    68		case 0:
    69			if (sw->no_nvm_upgrade)
    70				sw->no_nvm_upgrade = false;
    71	
    72			break;
    73	
    74		case 1:
    75			struct tb_nvm *nvm;
  > 76			u32 val;
    77			u32 nvm_size;
    78	
    79			nvm = tb_nvm_alloc(&sw->dev);
    80			if (IS_ERR(nvm)) {
    81				ret = PTR_ERR(nvm);
    82				break;
    83			}
    84	
    85			ret = usb4_switch_nvm_read(sw, NVM_Date, &val, sizeof(val));
    86			if (ret)
    87				break;
    88	
    89			nvm->vendor.date = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (u8)(val >> 0x10));
    90			ret = usb4_switch_nvm_read(sw, NVM_CUSTOMER_ID, &val, sizeof(val));
    91			if (ret)
    92				break;
    93	
    94			nvm->vendor.customerID = (((u8)val) << 0x8 | ((u8)(val >> 0x8)));
    95			nvm->vendor.version = (u8)(val >> 0x10);
    96			nvm_size = SZ_512K;
    97			ret = tb_nvm_add_active(nvm, nvm_size, tb_switch_nvm_read);
    98			if (ret)
    99				break;
   100	
   101			ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE, tb_switch_nvm_write);
   102			if (ret)
   103				break;
   104	
   105			sw->nvm = nvm;
   106			break;
   107	
   108		case 2:
   109			unsigned int image_size;
   110			const u8 *buf = sw->nvm->buf;
   111	
   112			if (!buf) {
   113				ret = -EINVAL;
   114				break;
   115			}
   116			image_size = sw->nvm->buf_data_size;
   117			if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE) {
   118				ret = -EINVAL;
   119				break;
   120			}
   121			ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
   122			if (!ret)
   123				sw->nvm->flushed = true;
   124	
   125			break;
   126	
   127		default:
   128			break;
   129		}
   130	
   131		if ((handle == 1) && (ret != 0))
   132			tb_nvm_free(sw->nvm);
   133	
   134		return ret;
   135	}
   136
kernel test robot Aug. 7, 2022, 10:33 a.m. UTC | #4
Hi Szuying,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.19 next-20220805]
[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/20220805-172732
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git b2a88c212e652e94f1e4b635910972ac57ba4e97
config: x86_64-randconfig-a012 (https://download.01.org/0day-ci/archive/20220807/202208071845.cTQlPQto-lkp@intel.com/config)
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 26dd42705c2af0b8f6e5d6cdb32c9bd5ed9524eb)
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
        # https://github.com/intel-lab-lkp/linux/commit/cc64350d27db3b30e5ceef2b5d6a3c48b9a4d989
        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/20220805-172732
        git checkout cc64350d27db3b30e5ceef2b5d6a3c48b9a4d989
        # 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=x86_64 SHELL=/bin/bash drivers/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

>> drivers/thunderbolt/nvm.c:75:3: error: expected expression
                   struct tb_nvm *nvm;
                   ^
>> drivers/thunderbolt/nvm.c:79:3: error: use of undeclared identifier 'nvm'
                   nvm = tb_nvm_alloc(&sw->dev);
                   ^
   drivers/thunderbolt/nvm.c:80:14: error: use of undeclared identifier 'nvm'
                   if (IS_ERR(nvm)) {
                              ^
   drivers/thunderbolt/nvm.c:81:18: error: use of undeclared identifier 'nvm'
                           ret = PTR_ERR(nvm);
                                         ^
   drivers/thunderbolt/nvm.c:89:3: error: use of undeclared identifier 'nvm'
                   nvm->vendor.date = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (u8)(val >> 0x10));
                   ^
   drivers/thunderbolt/nvm.c:94:3: error: use of undeclared identifier 'nvm'
                   nvm->vendor.customerID = (((u8)val) << 0x8 | ((u8)(val >> 0x8)));
                   ^
   drivers/thunderbolt/nvm.c:95:3: error: use of undeclared identifier 'nvm'
                   nvm->vendor.version = (u8)(val >> 0x10);
                   ^
   drivers/thunderbolt/nvm.c:97:27: error: use of undeclared identifier 'nvm'
                   ret = tb_nvm_add_active(nvm, nvm_size, tb_switch_nvm_read);
                                           ^
   drivers/thunderbolt/nvm.c:101:31: error: use of undeclared identifier 'nvm'
                   ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE, tb_switch_nvm_write);
                                               ^
   drivers/thunderbolt/nvm.c:105:13: error: use of undeclared identifier 'nvm'
                   sw->nvm = nvm;
                             ^
   drivers/thunderbolt/nvm.c:109:3: error: expected expression
                   unsigned int image_size;
                   ^
>> drivers/thunderbolt/nvm.c:116:3: error: use of undeclared identifier 'image_size'
                   image_size = sw->nvm->buf_data_size;
                   ^
   drivers/thunderbolt/nvm.c:117:7: error: use of undeclared identifier 'image_size'; did you mean 'page_size'?
                   if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE) {
                       ^~~~~~~~~~
                       page_size
   include/linux/mm.h:927:29: note: 'page_size' declared here
   static inline unsigned long page_size(struct page *page)
                               ^
   drivers/thunderbolt/nvm.c:117:36: error: use of undeclared identifier 'image_size'; did you mean 'page_size'?
                   if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE) {
                                                    ^~~~~~~~~~
                                                    page_size
   include/linux/mm.h:927:29: note: 'page_size' declared here
   static inline unsigned long page_size(struct page *page)
                               ^
   drivers/thunderbolt/nvm.c:121:43: error: use of undeclared identifier 'image_size'; did you mean 'page_size'?
                   ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
                                                           ^~~~~~~~~~
                                                           page_size
   include/linux/mm.h:927:29: note: 'page_size' declared here
   static inline unsigned long page_size(struct page *page)
                               ^
   drivers/thunderbolt/nvm.c:76:7: warning: mixing declarations and code is incompatible with standards before C99 [-Wdeclaration-after-statement]
                   u32 val;
                       ^
   1 warning and 15 errors generated.


vim +75 drivers/thunderbolt/nvm.c

    62	
    63	static int tb_nvm_vendor_apply(struct tb_switch *sw, unsigned int handle)
    64	{
    65		int ret = 0;
    66	
    67		switch (handle) {
    68		case 0:
    69			if (sw->no_nvm_upgrade)
    70				sw->no_nvm_upgrade = false;
    71	
    72			break;
    73	
    74		case 1:
  > 75			struct tb_nvm *nvm;
    76			u32 val;
    77			u32 nvm_size;
    78	
  > 79			nvm = tb_nvm_alloc(&sw->dev);
    80			if (IS_ERR(nvm)) {
    81				ret = PTR_ERR(nvm);
    82				break;
    83			}
    84	
    85			ret = usb4_switch_nvm_read(sw, NVM_Date, &val, sizeof(val));
    86			if (ret)
    87				break;
    88	
    89			nvm->vendor.date = (((u8)val) << 0x10 | ((u8)(val >> 0x8)) << 0x8 | (u8)(val >> 0x10));
    90			ret = usb4_switch_nvm_read(sw, NVM_CUSTOMER_ID, &val, sizeof(val));
    91			if (ret)
    92				break;
    93	
    94			nvm->vendor.customerID = (((u8)val) << 0x8 | ((u8)(val >> 0x8)));
    95			nvm->vendor.version = (u8)(val >> 0x10);
    96			nvm_size = SZ_512K;
    97			ret = tb_nvm_add_active(nvm, nvm_size, tb_switch_nvm_read);
    98			if (ret)
    99				break;
   100	
   101			ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE, tb_switch_nvm_write);
   102			if (ret)
   103				break;
   104	
   105			sw->nvm = nvm;
   106			break;
   107	
   108		case 2:
   109			unsigned int image_size;
   110			const u8 *buf = sw->nvm->buf;
   111	
   112			if (!buf) {
   113				ret = -EINVAL;
   114				break;
   115			}
 > 116			image_size = sw->nvm->buf_data_size;
   117			if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE) {
   118				ret = -EINVAL;
   119				break;
   120			}
   121			ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
   122			if (!ret)
   123				sw->nvm->flushed = true;
   124	
   125			break;
   126	
   127		default:
   128			break;
   129		}
   130	
   131		if ((handle == 1) && (ret != 0))
   132			tb_nvm_free(sw->nvm);
   133	
   134		return ret;
   135	}
   136
diff mbox series

Patch

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,