thunderbolt: thunderbolt: Add vendor's specific operations of NVM
Subsystems (2)
PATCH 1/2 thunderbolt: thunderbolt: Add vendor's specific operations of NVM
From: Szuying Chen <[email protected]>
The patch add tb_switch_nvm_alloc() contain an array that has functions pointers to vendor_ops that vendor to define.
And moved vendor:intel part of the code to make all the vendors (includes Intel) support it in nvm.c.
--- drivers/thunderbolt/nvm.c | 162 +++++++++++++++++++++++++++++++++++ drivers/thunderbolt/switch.c | 97 ++++----------------- drivers/thunderbolt/tb.h | 17 +++- 3 files changed, 195 insertions(+), 81 deletions(-)
drivers/thunderbolt/nvm.c +162 -0
@@ -12,8 +12,170 @@#include "tb.h" +/* Switch NVM support */ +#define NVM_CSS 0x10 +static DEFINE_IDA (nvm_ida); +static inline int nvm_read (struct tb_switch *sw,unsigned int address, +void *buf,size_t size) +{ +if (tb_switch_is_usb4 (sw)) +return usb4_switch_nvm_read (sw, address, buf, size); +return dma_port_flash_read (sw->dma_port, address, buf, size); +} + +static int intel_nvm_version (struct tb_switch *sw) +{ +struct tb_nvm *nvm = sw->nvm; +u32 val; +int ret; + +/* +* If the switch is in safe-mode the only accessible portion of +* the NVM is the non-active one where userspace is expected to +* write new functional NVM. +*/ +if (!sw->safe_mode) { +u32 nvm_size, hdr_size; + + ret =nvm_read (sw,NVM_FLASH_SIZE , &val,sizeof (val)); +if (ret) +return ret; + + hdr_size = sw->generation <3 ?SZ_8K :SZ_16K ; + nvm_size = (SZ_1M << (val &7 )) /8 ; + nvm_size = (nvm_size - hdr_size) /2 ; + + ret =nvm_read (sw,NVM_VERSION , &val,sizeof (val)); +if (ret) +return ret; + + nvm->major = val >>16 ; + nvm->minor = val >>8 ; + nvm->nvm_size = nvm_size; + } +return 0 ; +} + +static int intel_nvm_validate (struct tb_switch *sw) +{ +unsigned int image_size, hdr_size; +const u8 *buf = sw->nvm->buf; +u16 ds_size; +int ret; + + image_size = sw->nvm->buf_data_size; +if (image_size <NVM_MIN_SIZE || image_size >NVM_MAX_SIZE ) +return -EINVAL ; + +/* +* FARB pointer must point inside the image and must at least +* contain parts of the digital section we will be reading here. +*/ + hdr_size = (*(u32 *)buf) &0xffffff ; +if (hdr_size +NVM_DEVID +2 >= image_size) +return -EINVAL ; + +/* Digital section start should be aligned to 4k page */ +if (!IS_ALIGNED (hdr_size,SZ_4K )) +return -EINVAL ; + +/* +* Read digital section size and check that it also fits inside +* the image. +*/ + ds_size = *(u16 *)(buf + hdr_size); +if (ds_size >= image_size) +return -EINVAL ; + +if (!sw->safe_mode) { +u16 device_id; + +/* +* Make sure the device ID in the image matches the one +* we read from the switch config space. +*/ + device_id = *(u16 *)(buf + hdr_size +NVM_DEVID ); +if (device_id != sw->config.device_id) +return -EINVAL ; + +if (sw->generation <3 ) { +/* Write CSS headers first */ + ret =dma_port_flash_write (sw->dma_port, +DMA_PORT_CSS_ADDRESS , buf +NVM_CSS , +DMA_PORT_CSS_MAX_SIZE ); +if (ret) +return ret; + } + +/* Skip headers in the image */ + buf += hdr_size; + image_size -= hdr_size; + } +return image_size; +} + +struct tb_nvm_vendor_ops intel_switch_nvm_ops = { + .version = intel_nvm_version, + .validate = intel_nvm_validate, +}; + +struct switch_nvm_vendor { +u16 vendor; +const struct tb_nvm_vendor_ops *vops; +}; + +static const struct switch_nvm_vendor switch_nvm_vendors[] = { + {0x8086 , &intel_switch_nvm_ops }, + {0x8087 , &intel_switch_nvm_ops }, +}; + +/** +* tb_switch_nvm_alloc() - alloc nvm and set nvm->vops to point +* the vendor specific operations. +* @sw: thunderbolt switch +*/ +struct tb_nvm *tb_switch_nvm_alloc (struct tb_switch *sw) +{ +const struct tb_nvm_vendor_ops *vops =NULL ; +struct tb_nvm *nvm; +int i; +int ret; + +/* +* If the vendor matches on the array then set nvm->vops to +* point the vendor specific operations. +*/ +for (i =0 ; i <ARRAY_SIZE (switch_nvm_vendors); i++) { +const struct switch_nvm_vendor *v = &switch_nvm_vendors[i]; + +if (v->vendor == sw->config.vendor_id) { + vops = v->vops; +break ; + } + } + +if (!vops) +return ERR_PTR (-EOPNOTSUPP ); + + nvm =tb_nvm_alloc (&sw->dev); +if (IS_ERR (nvm)) +return nvm; + + nvm->vops = vops; + sw->nvm = nvm; + ret = vops->version (sw); +if (ret) + goto err_nvm; + +return nvm; + +err_nvm: +tb_nvm_free (nvm); +return ERR_PTR (ret); +} +/** * tb_nvm_alloc() - Allocate new NVM structure * @dev: Device owning the NVM
drivers/thunderbolt/switch.c +18 -79
129 lines not on this page · raw message
drivers/thunderbolt/tb.h +15 -2
@@ -48,8 +48,8 @@*/ struct tb_nvm {struct device *dev; -u8 major; -u8 minor; +u32 major; +u32 minor;int id;struct nvmem_device *active;struct nvmem_device *non_active;
@@ -57,6 +57,8 @@ struct tb_nvm {size_t buf_data_size;bool authenticating;bool flushed; +u32 nvm_size; +const struct tb_nvm_vendor_ops *vops; };enum tb_nvm_write_ops {
@@ -65,6 +67,16 @@ enum tb_nvm_write_ops { AUTHENTICATE_ONLY = 3, }; +/** +* struct tb_nvm_vendor_ops - vendor NVM specific operations +* @version: Used NVM read get Firmware version. +* @validate: Vendors have their validate method before NVM write. +*/ +struct tb_nvm_vendor_ops { +int (*version)(struct tb_switch *sw); +int (*validate)(struct tb_switch *sw); +}; +#define TB_SWITCH_KEY_SIZE 32#define TB_SWITCH_MAX_DEPTH 6#define USB4_SWITCH_MAX_DEPTH 5
@@ -736,6 +748,7 @@ static inline void tb_domain_put(struct tb *tb)put_device (&tb->dev); } +struct tb_nvm *tb_switch_nvm_alloc (struct tb_switch *sw);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
Hi,
Quoted 6 lines · Szuying Chen
From: Szuying Chen <[email protected]>
The patch add
tb_switch_nvm_alloc()contain an array that has functions pointers tovendor_opsthat vendor to define.
And moved vendor:intel part of the code to make all the vendors (includes Intel) support it innvm.c.
You still did not fix the $subject as I commented previously and please check the threading since I got two separate emails that are not related to each other. If you use git send-email it handles this but send the series first just to yourself and check that everything is fine before you go outside.
Also don't send new versions like every single day. Let people time to review them.
And last, you should add cover letter too (--cover-letter option to git format-patch) that also has the version history.
Quoted 48 lines
Signed-off-by Szuying Chen <[email protected]>--- drivers/thunderbolt/nvm.c | 162 +++++++++++++++++++++++++++++++++++ drivers/thunderbolt/switch.c | 97 ++++----------------- drivers/thunderbolt/tb.h | 17 +++- 3 files changed, 195 insertions(+), 81 deletions(-)drivers/thunderbolt/nvm.c +31
-0@@ -12,8 +12,170 @@#include "tb.h" +/* Switch NVM support */ +#define NVM_CSS 0x10 +static DEFINE_IDA (nvm_ida); +static inline int nvm_read (struct tb_switch *sw,unsigned int address, +void *buf,size_t size) +{ +if (tb_switch_is_usb4 (sw)) +return usb4_switch_nvm_read (sw, address, buf, size); +return dma_port_flash_read (sw->dma_port, address, buf, size); +} + +static int intel_nvm_version (struct tb_switch *sw) +{ +struct tb_nvm *nvm = sw->nvm; +u32 val; +int ret; + +/* +* If the switch is in safe-mode the only accessible portion of +* the NVM is the non-active one where userspace is expected to +* write new functional NVM. +*/ +if (!sw->safe_mode) { +u32 nvm_size, hdr_size; + + ret =nvm_read (sw,NVM_FLASH_SIZE , &val,sizeof (val)); +if (ret) +return ret; + + hdr_size = sw->generation <3 ?SZ_8K :SZ_16K ; + nvm_size = (SZ_1M << (val &7 )) /8 ;
drivers/thunderbolt/nvm.c line 57
Quoted 10 lines
drivers/thunderbolt/nvm.c +10
-0+ nvm_size = (nvm_size - hdr_size) /2 ; + + ret =nvm_read (sw,NVM_VERSION , &val,sizeof (val)); +if (ret) +return ret; + + nvm->major = val >>16 ; + nvm->minor = val >>8 ; + nvm->nvm_size = nvm_size; + }
Blank line here.
Quoted 48 lines
drivers/thunderbolt/nvm.c +48
-0+return 0 ; +} + +static int intel_nvm_validate (struct tb_switch *sw) +{ +unsigned int image_size, hdr_size; +const u8 *buf = sw->nvm->buf; +u16 ds_size; +int ret; + + image_size = sw->nvm->buf_data_size; +if (image_size <NVM_MIN_SIZE || image_size >NVM_MAX_SIZE ) +return -EINVAL ; + +/* +* FARB pointer must point inside the image and must at least +* contain parts of the digital section we will be reading here. +*/ + hdr_size = (*(u32 *)buf) &0xffffff ; +if (hdr_size +NVM_DEVID +2 >= image_size) +return -EINVAL ; + +/* Digital section start should be aligned to 4k page */ +if (!IS_ALIGNED (hdr_size,SZ_4K )) +return -EINVAL ; + +/* +* Read digital section size and check that it also fits inside +* the image. +*/ + ds_size = *(u16 *)(buf + hdr_size); +if (ds_size >= image_size) +return -EINVAL ; + +if (!sw->safe_mode) { +u16 device_id; + +/* +* Make sure the device ID in the image matches the one +* we read from the switch config space. +*/ + device_id = *(u16 *)(buf + hdr_size +NVM_DEVID ); +if (device_id != sw->config.device_id) +return -EINVAL ; + +if (sw->generation <3 ) { +/* Write CSS headers first */ + ret = dma_port_flash_write(sw->dma_port,
drivers/thunderbolt/nvm.c line 115
Quoted 10 lines
drivers/thunderbolt/nvm.c +10
-0+DMA_PORT_CSS_ADDRESS , buf +NVM_CSS , + DMA_PORT_CSS_MAX_SIZE); +if (ret) +return ret; + } + +/* Skip headers in the image */ + buf += hdr_size; + image_size -= hdr_size; + }
Blank line here.
drivers/thunderbolt/nvm.c line 119
Quoted 4 lines
drivers/thunderbolt/nvm.c +4
-0+return image_size; +} + +struct tb_nvm_vendor_ops intel_switch_nvm_ops = {
const
Quoted 7 lines
drivers/thunderbolt/nvm.c +7
-0+ .version = intel_nvm_version, + .validate = intel_nvm_validate, +}; + +struct switch_nvm_vendor { +u16 vendor; +const struct tb_nvm_vendor_ops *vops;
drivers/thunderbolt/nvm.c line 136
Quoted 10 lines
drivers/thunderbolt/nvm.c +10
-0+}; + +static const struct switch_nvm_vendor switch_nvm_vendors[] = { + {0x8086 , &intel_switch_nvm_ops }, + {0x8087 , &intel_switch_nvm_ops }, +}; + +/** +* tb_switch_nvm_alloc() - alloc nvm and set nvm->vops to point +* the vendor specific operations.
This is not correct kernel-doc format.
drivers/thunderbolt/nvm.c line 137
Quoted 1 line
drivers/thunderbolt/nvm.c +1
-0+* @sw: thunderbolt switch
What it returns?
Quoted 21 lines
drivers/thunderbolt/nvm.c +21
-0+*/ +struct tb_nvm *tb_switch_nvm_alloc (struct tb_switch *sw) +{ +const struct tb_nvm_vendor_ops *vops =NULL ; +struct tb_nvm *nvm; +int i; +int ret; + +/* +* If the vendor matches on the array then set nvm->vops to +* point the vendor specific operations. +*/ +for (i =0 ; i <ARRAY_SIZE (switch_nvm_vendors); i++) { +const struct switch_nvm_vendor *v = &switch_nvm_vendors[i]; + +if (v->vendor == sw->config.vendor_id) { + vops = v->vops; +break ; + } + } +
drivers/thunderbolt/nvm.c line 168
Quoted 10 lines
drivers/thunderbolt/nvm.c +10
-0+if (!vops) +return ERR_PTR (-EOPNOTSUPP ); + + nvm =tb_nvm_alloc (&sw->dev); +if (IS_ERR (nvm)) +return nvm; + + nvm->vops = vops; + sw->nvm = nvm; + ret = vops->version (sw);
Probably ->read_version() is better.
Quoted 18 lines
drivers/thunderbolt/nvm.c +10
-0+if (ret) + goto err_nvm; + +return nvm; + +err_nvm: +tb_nvm_free (nvm); +return ERR_PTR (ret); +} +/** * tb_nvm_alloc() - Allocate new NVM structure * @dev: Device owning the NVM drivers/thunderbolt/switch.c +0
-0
drivers/thunderbolt/switch.c static void nvm_clear_auth_status(const struct tb_switch *sw) · line 108
Quoted 9 lines
drivers/thunderbolt/switch.c +2
-2@@ -102,10 +102,10 @@ static void nvm_clear_auth_status(const struct tb_switch *sw)static int nvm_validate_and_write (struct tb_switch *sw) { -unsigned int image_size, hdr_size; +unsigned int image_size;const u8 *buf = sw->nvm->buf; -u16 ds_size;int ret; +const struct tb_nvm_vendor_ops *vops = sw->nvm->vops;
Reverse "christmas-tree" format here for the declarations and everywhere else too, so
const struct tb_nvm_vendor_ops *vops = sw->nvm->vops; const u8 *buf = sw->nvm->buf;
unsigned int image_size;
int ret;
Quoted 42 lines
drivers/thunderbolt/switch.c +0
-35if (!buf)return -EINVAL ;@@ -114,49 +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 ; -/* -* FARB pointer must point inside the image and must at least -* contain parts of the digital section we will be reading here. -*/ - hdr_size = (*(u32 *)buf) &0xffffff ; -if (hdr_size +NVM_DEVID +2 >= image_size) -return -EINVAL ; - -/* Digital section start should be aligned to 4k page */ -if (!IS_ALIGNED (hdr_size,SZ_4K )) -return -EINVAL ; - -/* -* Read digital section size and check that it also fits inside -* the image. -*/ - ds_size = *(u16 *)(buf + hdr_size); -if (ds_size >= image_size) -return -EINVAL ; - -if (!sw->safe_mode) { -u16 device_id; - -/* -* Make sure the device ID in the image matches the one -* we read from the switch config space. -*/ - device_id = *(u16 *)(buf + hdr_size +NVM_DEVID ); -if (device_id != sw->config.device_id) -return -EINVAL ; - -if (sw->generation <3 ) { -/* Write CSS headers first */ - ret = dma_port_flash_write(sw->dma_port, - DMA_PORT_CSS_ADDRESS, buf + NVM_CSS,
drivers/thunderbolt/switch.c static int nvm_validate_and_write(struct tb_switch *sw) · line 118
Quoted 10 lines
drivers/thunderbolt/switch.c +2
-8- DMA_PORT_CSS_MAX_SIZE); -if (ret) -return ret; - } - -/* Skip headers in the image */ - buf += hdr_size; - image_size -= hdr_size; +/* Vendors to validate before write to router NVM */ +if (vops->validate) {
Should you check for !vops too? By default we don't assign them.
Quoted 76 lines
drivers/thunderbolt/switch.c +14
-34+ ret = vops->validate (sw); +if (ret <0 ) +return ret; + image_size = ret; }if (tb_switch_is_usb4 (sw))@@ -384,28 +347,21 @@ static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,static int tb_switch_nvm_add (struct tb_switch *sw) {struct tb_nvm *nvm; -u32 val;int ret;if (!nvm_readable (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. -*/ -if (sw->config.vendor_id !=PCI_VENDOR_ID_INTEL && - sw->config.vendor_id !=0x8087 ) { -dev_info (&sw->dev, -"NVM format of vendor %#x is not known, disabling NVM upgrade\n" , - sw->config.vendor_id); -return 0 ; - } - - nvm =tb_nvm_alloc (&sw->dev); -if (IS_ERR (nvm)) + nvm =tb_switch_nvm_alloc (sw); +if (IS_ERR (nvm)) { +if (PTR_ERR (nvm) == -EOPNOTSUPP ) { +dev_info (&sw->dev, +"NVM format of vendor %#x is not known, disabling NVM upgrade\n" , + sw->config.vendor_id); +return 0 ; + }return PTR_ERR (nvm); + }/* * If the switch is in safe-mode the only accessible portion of @@ -413,24 +369,7 @@ static int tb_switch_nvm_add(struct tb_switch *sw)* write new functional NVM. */ if (!sw->safe_mode) { -u32 nvm_size, hdr_size; - - ret =nvm_read (sw,NVM_FLASH_SIZE , &val,sizeof (val)); -if (ret) - goto err_nvm; - - hdr_size = sw->generation <3 ?SZ_8K :SZ_16K ; - nvm_size = (SZ_1M << (val &7 )) /8 ; - nvm_size = (nvm_size - hdr_size) /2 ; - - ret =nvm_read (sw,NVM_VERSION , &val,sizeof (val)); -if (ret) - goto err_nvm; - - nvm->major = val >>16 ; - nvm->minor = val >>8 ; - - ret =tb_nvm_add_active (nvm, nvm_size, tb_switch_nvm_read); + ret =tb_nvm_add_active (nvm, nvm->nvm_size, tb_switch_nvm_read);if (ret) goto err_nvm; }drivers/thunderbolt/tb.h +0
-0
drivers/thunderbolt/tb.h line 52
Quoted 7 lines
drivers/thunderbolt/tb.h +2
-2@@ -48,8 +48,8 @@*/ struct tb_nvm {struct device *dev; -u8 major; -u8 minor; +u32 major; +u32 minor;
This should be a separate patch, I think.
drivers/thunderbolt/tb.h struct tb_nvm { · line 60
Quoted 8 lines
drivers/thunderbolt/tb.h +1
-0int id;struct nvmem_device *active;struct nvmem_device *non_active;@@ -57,6 +57,8 @@ struct tb_nvm {size_t buf_data_size;bool authenticating;bool flushed; +u32 nvm_size;
Kernel-doc
drivers/thunderbolt/tb.h struct tb_nvm { · line 61
Quoted 1 line
drivers/thunderbolt/tb.h +1
-0+const struct tb_nvm_vendor_ops *vops;
Ditto.
Quoted 6 lines
drivers/thunderbolt/tb.h +0
-0};enum tb_nvm_write_ops {@@ -65,6 +67,16 @@ enum tb_nvm_write_ops { AUTHENTICATE_ONLY = 3, };
drivers/thunderbolt/tb.h enum tb_nvm_write_ops { · line 78
Quoted 10 lines
drivers/thunderbolt/tb.h +9
-0+/** +* struct tb_nvm_vendor_ops - vendor NVM specific operations +* @version: Used NVM read get Firmware version. +* @validate: Vendors have their validate method before NVM write. +*/ +struct tb_nvm_vendor_ops { +int (*version)(struct tb_switch *sw); +int (*validate)(struct tb_switch *sw); +};
This can be private in nvm.c.
drivers/thunderbolt/tb.h static inline void tb_domain_put(struct tb *tb) · line 751
Quoted 9 lines
drivers/thunderbolt/tb.h +2
-0+#define TB_SWITCH_KEY_SIZE 32#define TB_SWITCH_MAX_DEPTH 6#define USB4_SWITCH_MAX_DEPTH 5@@ -736,6 +748,7 @@ static inline void tb_domain_put(struct tb *tb)put_device (&tb->dev); } +struct tb_nvm *tb_switch_nvm_alloc (struct tb_switch *sw);
And this should be moved close to the tb_switch_ routines.
Quoted 5 lines
drivers/thunderbolt/tb.h +0
-1struct 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