2021-03-12 19:07:52

by Michael Walle

[permalink] [raw]
Subject: [RFC PATCH 1/3] mtd: spi-nor: sfdp: remember sfdp_size

Save the sftp_size in the spi_nor struct so we can use it to dump the
SFDP table without parsing the headers again.

Signed-off-by: Michael Walle <[email protected]>
---
drivers/mtd/spi-nor/sfdp.c | 12 ++++++++++++
include/linux/mtd/spi-nor.h | 1 +
2 files changed, 13 insertions(+)

diff --git a/drivers/mtd/spi-nor/sfdp.c b/drivers/mtd/spi-nor/sfdp.c
index 25142ec4737b..b1814afefc33 100644
--- a/drivers/mtd/spi-nor/sfdp.c
+++ b/drivers/mtd/spi-nor/sfdp.c
@@ -16,6 +16,7 @@
(((p)->parameter_table_pointer[2] << 16) | \
((p)->parameter_table_pointer[1] << 8) | \
((p)->parameter_table_pointer[0] << 0))
+#define SFDP_PARAM_HEADER_PARAM_LEN(p) ((p)->length * 4)

#define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
#define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
@@ -1263,6 +1264,7 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
struct sfdp_parameter_header *param_headers = NULL;
struct sfdp_header header;
struct device *dev = nor->dev;
+ size_t param_max_offset;
size_t psize;
int i, err;

@@ -1285,6 +1287,9 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
bfpt_header->major != SFDP_JESD216_MAJOR)
return -EINVAL;

+ nor->sfdp_size = SFDP_PARAM_HEADER_PTP(bfpt_header) +
+ SFDP_PARAM_HEADER_PARAM_LEN(bfpt_header);
+
/*
* Allocate memory then read all parameter headers with a single
* Read SFDP command. These parameter headers will actually be parsed
@@ -1311,6 +1316,13 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
}
}

+ for (i = 0; i < header.nph; i++) {
+ param_header = &param_headers[i];
+ param_max_offset = SFDP_PARAM_HEADER_PTP(param_header) +
+ SFDP_PARAM_HEADER_PARAM_LEN(param_header);
+ nor->sfdp_size = max(nor->sfdp_size, param_max_offset);
+ }
+
/*
* Check other parameter headers to get the latest revision of
* the basic flash parameter table.
diff --git a/include/linux/mtd/spi-nor.h b/include/linux/mtd/spi-nor.h
index a0d572855444..a58118b8b002 100644
--- a/include/linux/mtd/spi-nor.h
+++ b/include/linux/mtd/spi-nor.h
@@ -404,6 +404,7 @@ struct spi_nor {
bool sst_write_second;
u32 flags;
enum spi_nor_cmd_ext cmd_ext_type;
+ size_t sfdp_size;

const struct spi_nor_controller_ops *controller_ops;

--
2.20.1


2021-03-16 15:29:06

by Michael Walle

[permalink] [raw]
Subject: Re: [RFC PATCH 1/3] mtd: spi-nor: sfdp: remember sfdp_size

Hi Pratyush,

Am 2021-03-16 11:42, schrieb Pratyush Yadav:
> On 12/03/21 08:05PM, Michael Walle wrote:
>> Save the sftp_size in the spi_nor struct so we can use it to dump the
>> SFDP table without parsing the headers again.
>>
>> Signed-off-by: Michael Walle <[email protected]>
>> ---
>> drivers/mtd/spi-nor/sfdp.c | 12 ++++++++++++
>> include/linux/mtd/spi-nor.h | 1 +
>> 2 files changed, 13 insertions(+)
>>
>> diff --git a/drivers/mtd/spi-nor/sfdp.c b/drivers/mtd/spi-nor/sfdp.c
>> index 25142ec4737b..b1814afefc33 100644
>> --- a/drivers/mtd/spi-nor/sfdp.c
>> +++ b/drivers/mtd/spi-nor/sfdp.c
>> @@ -16,6 +16,7 @@
>> (((p)->parameter_table_pointer[2] << 16) | \
>> ((p)->parameter_table_pointer[1] << 8) | \
>> ((p)->parameter_table_pointer[0] << 0))
>> +#define SFDP_PARAM_HEADER_PARAM_LEN(p) ((p)->length * 4)
>>
>> #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
>> #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
>> @@ -1263,6 +1264,7 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
>> struct sfdp_parameter_header *param_headers = NULL;
>> struct sfdp_header header;
>> struct device *dev = nor->dev;
>> + size_t param_max_offset;
>> size_t psize;
>> int i, err;
>>
>> @@ -1285,6 +1287,9 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
>> bfpt_header->major != SFDP_JESD216_MAJOR)
>> return -EINVAL;
>>
>> + nor->sfdp_size = SFDP_PARAM_HEADER_PTP(bfpt_header) +
>> + SFDP_PARAM_HEADER_PARAM_LEN(bfpt_header);
>> +
>> /*
>> * Allocate memory then read all parameter headers with a single
>> * Read SFDP command. These parameter headers will actually be
>> parsed
>> @@ -1311,6 +1316,13 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
>> }
>> }
>>
>> + for (i = 0; i < header.nph; i++) {
>> + param_header = &param_headers[i];
>> + param_max_offset = SFDP_PARAM_HEADER_PTP(param_header) +
>> + SFDP_PARAM_HEADER_PARAM_LEN(param_header);
>> + nor->sfdp_size = max(nor->sfdp_size, param_max_offset);
>> + }
>> +
>
> I don't see any mention in the SFDP spec (JESD216D-01) that parameter
> tables have to be contiguous.

ch. 6.1, fig.10 looks like all the headers come after the initial SFDP
header. If that wasn't the case, how would you find the headers then?

Also ch. 6.3
"""All subsequent parameter headers need to be contiguous and may be
specified..."""

> In fact, it says that "Parameter tables
> may be located anywhere in the SFDP space. They do not need to
> immediately follow the parameter headers".

The tables itself, yes, but not the headers for the tables.

> But I guess we can just say
> the sysfs entry exports the entire SFDP space instead of just the
> tables
> so that is OK.
>
> This patch looks good to me other than the small nitpick that we can
> merge this loop and the one below that tries to find the latest BFPT
> version.

btw. I've also added an export for the jedec id and the flash name to
this patch, which will be included in the next version.

>> /*
>> * Check other parameter headers to get the latest revision of
>> * the basic flash parameter table.
>> diff --git a/include/linux/mtd/spi-nor.h b/include/linux/mtd/spi-nor.h
>> index a0d572855444..a58118b8b002 100644
>> --- a/include/linux/mtd/spi-nor.h
>> +++ b/include/linux/mtd/spi-nor.h
>> @@ -404,6 +404,7 @@ struct spi_nor {
>> bool sst_write_second;
>> u32 flags;
>> enum spi_nor_cmd_ext cmd_ext_type;
>> + size_t sfdp_size;
>
> Documentation for this variable missing.
>
>>
>> const struct spi_nor_controller_ops *controller_ops;
>>
>> --
>> 2.20.1
>>

--
-michael

2021-03-16 15:34:37

by Michael Walle

[permalink] [raw]
Subject: Re: [RFC PATCH 1/3] mtd: spi-nor: sfdp: remember sfdp_size

Am 2021-03-16 12:06, schrieb Pratyush Yadav:
> On 16/03/21 12:01PM, Michael Walle wrote:
>> Hi Pratyush,
>>
>> Am 2021-03-16 11:42, schrieb Pratyush Yadav:
>> > On 12/03/21 08:05PM, Michael Walle wrote:
>> > > Save the sftp_size in the spi_nor struct so we can use it to dump the
>> > > SFDP table without parsing the headers again.
>> > >
>> > > Signed-off-by: Michael Walle <[email protected]>
>> > > ---
>> > > drivers/mtd/spi-nor/sfdp.c | 12 ++++++++++++
>> > > include/linux/mtd/spi-nor.h | 1 +
>> > > 2 files changed, 13 insertions(+)
>> > >
>> > > diff --git a/drivers/mtd/spi-nor/sfdp.c b/drivers/mtd/spi-nor/sfdp.c
>> > > index 25142ec4737b..b1814afefc33 100644
>> > > --- a/drivers/mtd/spi-nor/sfdp.c
>> > > +++ b/drivers/mtd/spi-nor/sfdp.c
>> > > @@ -16,6 +16,7 @@
>> > > (((p)->parameter_table_pointer[2] << 16) | \
>> > > ((p)->parameter_table_pointer[1] << 8) | \
>> > > ((p)->parameter_table_pointer[0] << 0))
>> > > +#define SFDP_PARAM_HEADER_PARAM_LEN(p) ((p)->length * 4)
>> > >
>> > > #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
>> > > #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
>> > > @@ -1263,6 +1264,7 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
>> > > struct sfdp_parameter_header *param_headers = NULL;
>> > > struct sfdp_header header;
>> > > struct device *dev = nor->dev;
>> > > + size_t param_max_offset;
>> > > size_t psize;
>> > > int i, err;
>> > >
>> > > @@ -1285,6 +1287,9 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
>> > > bfpt_header->major != SFDP_JESD216_MAJOR)
>> > > return -EINVAL;
>> > >
>> > > + nor->sfdp_size = SFDP_PARAM_HEADER_PTP(bfpt_header) +
>> > > + SFDP_PARAM_HEADER_PARAM_LEN(bfpt_header);
>> > > +
>> > > /*
>> > > * Allocate memory then read all parameter headers with a single
>> > > * Read SFDP command. These parameter headers will actually be
>> > > parsed
>> > > @@ -1311,6 +1316,13 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
>> > > }
>> > > }
>> > >
>> > > + for (i = 0; i < header.nph; i++) {
>> > > + param_header = &param_headers[i];
>> > > + param_max_offset = SFDP_PARAM_HEADER_PTP(param_header) +
>> > > + SFDP_PARAM_HEADER_PARAM_LEN(param_header);
>> > > + nor->sfdp_size = max(nor->sfdp_size, param_max_offset);
>> > > + }
>> > > +
>> >
>> > I don't see any mention in the SFDP spec (JESD216D-01) that parameter
>> > tables have to be contiguous.
>>
>> ch. 6.1, fig.10 looks like all the headers come after the initial SFDP
>> header. If that wasn't the case, how would you find the headers then?
>>
>> Also ch. 6.3
>> """All subsequent parameter headers need to be contiguous and may be
>> specified..."""
>>
>> > In fact, it says that "Parameter tables
>> > may be located anywhere in the SFDP space. They do not need to
>> > immediately follow the parameter headers".
>>
>> The tables itself, yes, but not the headers for the tables.
>
> Yes, I was talking about the tables and not the headers. There might be
> holes in the SFDP space but I don't think it would be a problem.

Ah, ok. Well basically I'm assuming that the holes will returning some
sane values. Or you could dump the space together with a mask. But that
is overkill ;)

-michael

2021-03-16 18:21:44

by Pratyush Yadav

[permalink] [raw]
Subject: Re: [RFC PATCH 1/3] mtd: spi-nor: sfdp: remember sfdp_size

On 12/03/21 08:05PM, Michael Walle wrote:
> Save the sftp_size in the spi_nor struct so we can use it to dump the
> SFDP table without parsing the headers again.
>
> Signed-off-by: Michael Walle <[email protected]>
> ---
> drivers/mtd/spi-nor/sfdp.c | 12 ++++++++++++
> include/linux/mtd/spi-nor.h | 1 +
> 2 files changed, 13 insertions(+)
>
> diff --git a/drivers/mtd/spi-nor/sfdp.c b/drivers/mtd/spi-nor/sfdp.c
> index 25142ec4737b..b1814afefc33 100644
> --- a/drivers/mtd/spi-nor/sfdp.c
> +++ b/drivers/mtd/spi-nor/sfdp.c
> @@ -16,6 +16,7 @@
> (((p)->parameter_table_pointer[2] << 16) | \
> ((p)->parameter_table_pointer[1] << 8) | \
> ((p)->parameter_table_pointer[0] << 0))
> +#define SFDP_PARAM_HEADER_PARAM_LEN(p) ((p)->length * 4)
>
> #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
> #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
> @@ -1263,6 +1264,7 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
> struct sfdp_parameter_header *param_headers = NULL;
> struct sfdp_header header;
> struct device *dev = nor->dev;
> + size_t param_max_offset;
> size_t psize;
> int i, err;
>
> @@ -1285,6 +1287,9 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
> bfpt_header->major != SFDP_JESD216_MAJOR)
> return -EINVAL;
>
> + nor->sfdp_size = SFDP_PARAM_HEADER_PTP(bfpt_header) +
> + SFDP_PARAM_HEADER_PARAM_LEN(bfpt_header);
> +
> /*
> * Allocate memory then read all parameter headers with a single
> * Read SFDP command. These parameter headers will actually be parsed
> @@ -1311,6 +1316,13 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
> }
> }
>
> + for (i = 0; i < header.nph; i++) {
> + param_header = &param_headers[i];
> + param_max_offset = SFDP_PARAM_HEADER_PTP(param_header) +
> + SFDP_PARAM_HEADER_PARAM_LEN(param_header);
> + nor->sfdp_size = max(nor->sfdp_size, param_max_offset);
> + }
> +

I don't see any mention in the SFDP spec (JESD216D-01) that parameter
tables have to be contiguous. In fact, it says that "Parameter tables
may be located anywhere in the SFDP space. They do not need to
immediately follow the parameter headers". But I guess we can just say
the sysfs entry exports the entire SFDP space instead of just the tables
so that is OK.

This patch looks good to me other than the small nitpick that we can
merge this loop and the one below that tries to find the latest BFPT
version.

> /*
> * Check other parameter headers to get the latest revision of
> * the basic flash parameter table.
> diff --git a/include/linux/mtd/spi-nor.h b/include/linux/mtd/spi-nor.h
> index a0d572855444..a58118b8b002 100644
> --- a/include/linux/mtd/spi-nor.h
> +++ b/include/linux/mtd/spi-nor.h
> @@ -404,6 +404,7 @@ struct spi_nor {
> bool sst_write_second;
> u32 flags;
> enum spi_nor_cmd_ext cmd_ext_type;
> + size_t sfdp_size;

Documentation for this variable missing.

>
> const struct spi_nor_controller_ops *controller_ops;
>
> --
> 2.20.1
>

--
Regards,
Pratyush Yadav
Texas Instruments Inc.

2021-03-16 18:30:40

by Pratyush Yadav

[permalink] [raw]
Subject: Re: [RFC PATCH 1/3] mtd: spi-nor: sfdp: remember sfdp_size

On 16/03/21 12:01PM, Michael Walle wrote:
> Hi Pratyush,
>
> Am 2021-03-16 11:42, schrieb Pratyush Yadav:
> > On 12/03/21 08:05PM, Michael Walle wrote:
> > > Save the sftp_size in the spi_nor struct so we can use it to dump the
> > > SFDP table without parsing the headers again.
> > >
> > > Signed-off-by: Michael Walle <[email protected]>
> > > ---
> > > drivers/mtd/spi-nor/sfdp.c | 12 ++++++++++++
> > > include/linux/mtd/spi-nor.h | 1 +
> > > 2 files changed, 13 insertions(+)
> > >
> > > diff --git a/drivers/mtd/spi-nor/sfdp.c b/drivers/mtd/spi-nor/sfdp.c
> > > index 25142ec4737b..b1814afefc33 100644
> > > --- a/drivers/mtd/spi-nor/sfdp.c
> > > +++ b/drivers/mtd/spi-nor/sfdp.c
> > > @@ -16,6 +16,7 @@
> > > (((p)->parameter_table_pointer[2] << 16) | \
> > > ((p)->parameter_table_pointer[1] << 8) | \
> > > ((p)->parameter_table_pointer[0] << 0))
> > > +#define SFDP_PARAM_HEADER_PARAM_LEN(p) ((p)->length * 4)
> > >
> > > #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
> > > #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
> > > @@ -1263,6 +1264,7 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
> > > struct sfdp_parameter_header *param_headers = NULL;
> > > struct sfdp_header header;
> > > struct device *dev = nor->dev;
> > > + size_t param_max_offset;
> > > size_t psize;
> > > int i, err;
> > >
> > > @@ -1285,6 +1287,9 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
> > > bfpt_header->major != SFDP_JESD216_MAJOR)
> > > return -EINVAL;
> > >
> > > + nor->sfdp_size = SFDP_PARAM_HEADER_PTP(bfpt_header) +
> > > + SFDP_PARAM_HEADER_PARAM_LEN(bfpt_header);
> > > +
> > > /*
> > > * Allocate memory then read all parameter headers with a single
> > > * Read SFDP command. These parameter headers will actually be
> > > parsed
> > > @@ -1311,6 +1316,13 @@ int spi_nor_parse_sfdp(struct spi_nor *nor,
> > > }
> > > }
> > >
> > > + for (i = 0; i < header.nph; i++) {
> > > + param_header = &param_headers[i];
> > > + param_max_offset = SFDP_PARAM_HEADER_PTP(param_header) +
> > > + SFDP_PARAM_HEADER_PARAM_LEN(param_header);
> > > + nor->sfdp_size = max(nor->sfdp_size, param_max_offset);
> > > + }
> > > +
> >
> > I don't see any mention in the SFDP spec (JESD216D-01) that parameter
> > tables have to be contiguous.
>
> ch. 6.1, fig.10 looks like all the headers come after the initial SFDP
> header. If that wasn't the case, how would you find the headers then?
>
> Also ch. 6.3
> """All subsequent parameter headers need to be contiguous and may be
> specified..."""
>
> > In fact, it says that "Parameter tables
> > may be located anywhere in the SFDP space. They do not need to
> > immediately follow the parameter headers".
>
> The tables itself, yes, but not the headers for the tables.

Yes, I was talking about the tables and not the headers. There might be
holes in the SFDP space but I don't think it would be a problem.

--
Regards,
Pratyush Yadav
Texas Instruments Inc.