2023-05-22 07:46:35

by shaopeijie

[permalink] [raw]
Subject: [PATCH v2] tpm_tis_spi: fix:release chip select when flow control fails

From: Peijie Shao <[email protected]>

The failure paths in tpm_tis_spi_transfer() do not deactivate
chip select. Send an empty message (cs_select == 0) to overcome
this.

Signed-off-by: Peijie Shao <[email protected]>
---
Changes since v1:
1. Deactive cs all of the failure path, not only flow control.
2. change and update comments.
---
drivers/char/tpm/tpm_tis_spi_main.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/drivers/char/tpm/tpm_tis_spi_main.c b/drivers/char/tpm/tpm_tis_spi_main.c
index 1f5207974..9bfaba092 100644
--- a/drivers/char/tpm/tpm_tis_spi_main.c
+++ b/drivers/char/tpm/tpm_tis_spi_main.c
@@ -136,6 +136,14 @@ int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
}

exit:
+ if (ret < 0) {
+ /* Deactivate chip select */
+ memset(&spi_xfer, 0, sizeof(spi_xfer));
+ spi_message_init(&m);
+ spi_message_add_tail(&spi_xfer, &m);
+ spi_sync_locked(phy->spi_device, &m);
+ }
+
spi_bus_unlock(phy->spi_device->master);
return ret;
}
--
2.39.1





2023-05-22 08:33:31

by Paul Menzel

[permalink] [raw]
Subject: Re: [PATCH v2] tpm_tis_spi: fix:release chip select when flow control fails

Dear Peijie,


Thank you for your patch.

The fix: tag in your commit message summary is uncommon. I suggest:

> tpm_tis_spi: Release chip select when flow control fails


Am 22.05.23 um 09:01 schrieb [email protected]:
> From: Peijie Shao <[email protected]>
>
> The failure paths in tpm_tis_spi_transfer() do not deactivate
> chip select. Send an empty message (cs_select == 0) to overcome
> this.

Does the standard require to deactivate it?

A note on your test setup would be nice to have in the commit message.

> Signed-off-by: Peijie Shao <[email protected]>
> ---
> Changes since v1:
> 1. Deactive cs all of the failure path, not only flow control.

Deactivate


Kind regards,

Paul


> 2. change and update comments.
> ---
> drivers/char/tpm/tpm_tis_spi_main.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/drivers/char/tpm/tpm_tis_spi_main.c b/drivers/char/tpm/tpm_tis_spi_main.c
> index 1f5207974..9bfaba092 100644
> --- a/drivers/char/tpm/tpm_tis_spi_main.c
> +++ b/drivers/char/tpm/tpm_tis_spi_main.c
> @@ -136,6 +136,14 @@ int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
> }
>
> exit:
> + if (ret < 0) {
> + /* Deactivate chip select */
> + memset(&spi_xfer, 0, sizeof(spi_xfer));
> + spi_message_init(&m);
> + spi_message_add_tail(&spi_xfer, &m);
> + spi_sync_locked(phy->spi_device, &m);
> + }
> +
> spi_bus_unlock(phy->spi_device->master);
> return ret;
> }

2023-05-23 02:57:07

by shaopeijie

[permalink] [raw]
Subject: [PATCH v3] tpm_tis_spi: Release chip select when flow control fails

From: Peijie Shao <[email protected]>

The failure paths in tpm_tis_spi_transfer() do not deactivate
chip select. Send an empty message (cs_select == 0) to overcome
this.

The patch is tested by two ways.
One way needs to touch hardware:
1. force pull MISO pin down to GND, it emulates a forever
'WAIT' timing.
2. probe cs pin by an oscilloscope.
3. load tpm_tis_spi.ko.
After loading, dmesg prints:
"probe of spi0.0 failed with error -110"
and oscilloscope shows cs pin goes high(deactivated) after
the failure. Before the patch, cs pin keeps low.

Second way is by writing a fake spi controller.
1. implement .transfer_one method, fill all rx buf with 0.
2. implement .set_cs method, print the state of cs pin.
we can see cs goes high after the failure.

Signed-off-by: Peijie Shao <[email protected]>
---
Changes since v1:
Thanks to BR, Jarkko's suggestions.
1. Deactivate cs all of the failure path, not only flow control.
2. change and update comments.
---
Changes since v2:
Thanks to Paul?Menzel's suggestions.
1. common commit tag and correct spelling mistakes.
2. add test steps.
---
drivers/char/tpm/tpm_tis_spi_main.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/drivers/char/tpm/tpm_tis_spi_main.c b/drivers/char/tpm/tpm_tis_spi_main.c
index 1f5207974..9bfaba092 100644
--- a/drivers/char/tpm/tpm_tis_spi_main.c
+++ b/drivers/char/tpm/tpm_tis_spi_main.c
@@ -136,6 +136,14 @@ int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
}

exit:
+ if (ret < 0) {
+ /* Deactivate chip select */
+ memset(&spi_xfer, 0, sizeof(spi_xfer));
+ spi_message_init(&m);
+ spi_message_add_tail(&spi_xfer, &m);
+ spi_sync_locked(phy->spi_device, &m);
+ }
+
spi_bus_unlock(phy->spi_device->master);
return ret;
}
--
2.39.1




2023-05-23 03:01:04

by shaopeijie

[permalink] [raw]
Subject: Re: Re: [PATCH v2] tpm_tis_spi: fix:release chip select when flow control fails

Thank you, Paul.

>Dear Peijie,
>
>
>Thank you for your patch.
>
>The fix: tag in your commit message summary is uncommon. I suggest:
>
>> tpm_tis_spi: Release chip select when flow control fails
>

I will change it in v3 version.

>
>Am 22.05.23 um 09:01 schrieb [email protected]:
>> From: Peijie Shao <[email protected]>
>>
>> The failure paths in tpm_tis_spi_transfer() do not deactivate
>> chip select. Send an empty message (cs_select == 0) to overcome
>> this.
>
>Does the standard require to deactivate it?

Yes.
<TCG_PCClientTPMInterfaceSpecification_TIS__1-3_27_03212013.pdf>, section
6.4.2 Electrical Specification, Figure 4 Timing Diagram, describes the timing.

Moreover, when multi devices are sharing with the same SPI bus, only one cs can be
activated simultaneously. If two or more cs are activated, corresponding devices
may response at same time, cause bus conflicts.

>
>A note on your test setup would be nice to have in the commit message.
>
>> Signed-off-by: Peijie Shao <[email protected]>
>> ---
>> Changes since v1:
>> 1. Deactive cs all of the failure path, not only flow control.
>
>Deactivate

Sorry, will be fixed in v3 version.

>
>
>Kind regards,
>
>Paul
>
>
>> 2. change and update comments.
>> ---
>> drivers/char/tpm/tpm_tis_spi_main.c | 8 ++++++++
>> 1 file changed, 8 insertions(+)
>>
>> diff --git a/drivers/char/tpm/tpm_tis_spi_main.c b/drivers/char/tpm/tpm_tis_spi_main.c
>> index 1f5207974..9bfaba092 100644
>> --- a/drivers/char/tpm/tpm_tis_spi_main.c
>> +++ b/drivers/char/tpm/tpm_tis_spi_main.c
>> @@ -136,6 +136,14 @@ int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
>> }
>>
>> exit:
>> + if (ret < 0) {
>> + /* Deactivate chip select */
>> + memset(&spi_xfer, 0, sizeof(spi_xfer));
>> + spi_message_init(&m);
>> + spi_message_add_tail(&spi_xfer, &m);
>> + spi_sync_locked(phy->spi_device, &m);
>> + }
>> +
>> spi_bus_unlock(phy->spi_device->master);
>> return ret;
>> }

Peijie, Shao



2023-05-23 03:06:29

by shaopeijie

[permalink] [raw]
Subject: Re: Re: [PATCH v2] tpm_tis_spi: fix:release chip select when flow control fails

Thank you, Paul.

>Dear Peijie,
>
>
>Thank you for your patch.
>
>The fix: tag in your commit message summary is uncommon. I suggest:
>
>> tpm_tis_spi: Release chip select when flow control fails
>

I will change it in v3 version.

>
>Am 22.05.23 um 09:01 schrieb [email protected]:
>> From: Peijie Shao <[email protected]>
>>
>> The failure paths in tpm_tis_spi_transfer() do not deactivate
>> chip select. Send an empty message (cs_select == 0) to overcome
>> this.
>
>Does the standard require to deactivate it?

Yes.
<TCG_PCClientTPMInterfaceSpecification_TIS__1-3_27_03212013.pdf>, section
6.4.2 Electrical Specification, Figure 4 Timing Diagram, describes the timing.

Moreover, when multi devices are sharing with the same SPI bus, only one cs can be
activated simultaneously. If two or more cs are activated, corresponding devices
may response at same time, cause bus conflicts.

>
>A note on your test setup would be nice to have in the commit message.
>
>> Signed-off-by: Peijie Shao <[email protected]>
>> ---
>> Changes since v1:
>> 1. Deactive cs all of the failure path, not only flow control.
>
>Deactivate

Sorry, will be fixed in v3 version.

>
>
>Kind regards,
>
>Paul
>
>
>> 2. change and update comments.
>> ---
>> drivers/char/tpm/tpm_tis_spi_main.c | 8 ++++++++
>> 1 file changed, 8 insertions(+)
>>
>> diff --git a/drivers/char/tpm/tpm_tis_spi_main.c b/drivers/char/tpm/tpm_tis_spi_main.c
>> index 1f5207974..9bfaba092 100644
>> --- a/drivers/char/tpm/tpm_tis_spi_main.c
>> +++ b/drivers/char/tpm/tpm_tis_spi_main.c
>> @@ -136,6 +136,14 @@ int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
>> }
>>
>> exit:
>> + if (ret < 0) {
>> + /* Deactivate chip select */
>> + memset(&spi_xfer, 0, sizeof(spi_xfer));
>> + spi_message_init(&m);
>> + spi_message_add_tail(&spi_xfer, &m);
>> + spi_sync_locked(phy->spi_device, &m);
>> + }
>> +
>> spi_bus_unlock(phy->spi_device->master);
>> return ret;
>> }

Peijie, Shao



2023-05-24 02:17:34

by Jarkko Sakkinen

[permalink] [raw]
Subject: Re: [PATCH v3] tpm_tis_spi: Release chip select when flow control fails

On Tue May 23, 2023 at 5:45 AM EEST, wrote:
> From: Peijie Shao <[email protected]>
>
> The failure paths in tpm_tis_spi_transfer() do not deactivate
> chip select. Send an empty message (cs_select == 0) to overcome
> this.
>
> The patch is tested by two ways.
> One way needs to touch hardware:
> 1. force pull MISO pin down to GND, it emulates a forever
> 'WAIT' timing.
> 2. probe cs pin by an oscilloscope.
> 3. load tpm_tis_spi.ko.
> After loading, dmesg prints:
> "probe of spi0.0 failed with error -110"
> and oscilloscope shows cs pin goes high(deactivated) after
> the failure. Before the patch, cs pin keeps low.
>
> Second way is by writing a fake spi controller.
> 1. implement .transfer_one method, fill all rx buf with 0.
> 2. implement .set_cs method, print the state of cs pin.
> we can see cs goes high after the failure.
>
> Signed-off-by: Peijie Shao <[email protected]>

Looks good to me + great explanation, thank you.

Reviewed-by: Jarkko Sakkinen <[email protected]>

BR, Jarkko

2023-05-24 03:14:29

by Jarkko Sakkinen

[permalink] [raw]
Subject: Re: [PATCH v2] tpm_tis_spi: fix:release chip select when flow control fails

On Mon May 22, 2023 at 11:17 AM EEST, Paul Menzel wrote:
> Dear Peijie,
>
>
> Thank you for your patch.
>
> The fix: tag in your commit message summary is uncommon. I suggest:
>
> > tpm_tis_spi: Release chip select when flow control fails
>
>
> Am 22.05.23 um 09:01 schrieb [email protected]:
> > From: Peijie Shao <[email protected]>
> >
> > The failure paths in tpm_tis_spi_transfer() do not deactivate
> > chip select. Send an empty message (cs_select == 0) to overcome
> > this.
>
> Does the standard require to deactivate it?
>
> A note on your test setup would be nice to have in the commit message.

I think it is "good enough", not to say it couldn't be improved.

I can accept as it is, or substitute with one with an updated commit
message. This is going earliest to v6.5.

BR, Jarkko

2023-05-24 03:47:27

by Jarkko Sakkinen

[permalink] [raw]
Subject: Re: [PATCH v3] tpm_tis_spi: Release chip select when flow control fails

On Tue May 23, 2023 at 5:45 AM EEST, wrote:
> From: Peijie Shao <[email protected]>
>
> The failure paths in tpm_tis_spi_transfer() do not deactivate
> chip select. Send an empty message (cs_select == 0) to overcome
> this.
>
> The patch is tested by two ways.
> One way needs to touch hardware:
> 1. force pull MISO pin down to GND, it emulates a forever
> 'WAIT' timing.
> 2. probe cs pin by an oscilloscope.
> 3. load tpm_tis_spi.ko.
> After loading, dmesg prints:
> "probe of spi0.0 failed with error -110"
> and oscilloscope shows cs pin goes high(deactivated) after
> the failure. Before the patch, cs pin keeps low.
>
> Second way is by writing a fake spi controller.
> 1. implement .transfer_one method, fill all rx buf with 0.
> 2. implement .set_cs method, print the state of cs pin.
> we can see cs goes high after the failure.
>
> Signed-off-by: Peijie Shao <[email protected]>

Thanks I substitute the patch that I applied with this.

Again:

Reviewed-by: Jarkko Sakkinen <[email protected]>

BR, Jarkko