Linux Integrity Measurement development
 help / color / mirror / Atom feed
* [PATCH v2] tpm_tis_spi: fix:release chip select when flow control fails
@ 2023-05-22  7:01 shaopeijie
  2023-05-22  8:17 ` Paul Menzel
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: shaopeijie @ 2023-05-22  7:01 UTC (permalink / raw)
  To: jarkko; +Cc: peterhuewe, jgg, linux-integrity, linux-kernel, Peijie Shao

From: Peijie Shao <shaopeijie@cestc.cn>

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 <shaopeijie@cestc.cn>
---
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




^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] tpm_tis_spi: fix:release chip select when flow control fails
  2023-05-22  7:01 [PATCH v2] tpm_tis_spi: fix:release chip select when flow control fails shaopeijie
@ 2023-05-22  8:17 ` Paul Menzel
  2023-05-24  2:52   ` Jarkko Sakkinen
  2023-05-23  2:45 ` shaopeijie
  2023-05-23  2:45 ` [PATCH v3] tpm_tis_spi: Release " shaopeijie
  2 siblings, 1 reply; 7+ messages in thread
From: Paul Menzel @ 2023-05-22  8:17 UTC (permalink / raw)
  To: Peijie Shao; +Cc: jarkko, peterhuewe, jgg, linux-integrity, linux-kernel

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 shaopeijie@cestc.cn:
> From: Peijie Shao <shaopeijie@cestc.cn>
> 
> 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 <shaopeijie@cestc.cn>
> ---
> 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;
>   }

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Re: [PATCH v2] tpm_tis_spi: fix:release chip select when flow control fails
  2023-05-22  7:01 [PATCH v2] tpm_tis_spi: fix:release chip select when flow control fails shaopeijie
  2023-05-22  8:17 ` Paul Menzel
@ 2023-05-23  2:45 ` shaopeijie
  2023-05-23  2:45 ` [PATCH v3] tpm_tis_spi: Release " shaopeijie
  2 siblings, 0 replies; 7+ messages in thread
From: shaopeijie @ 2023-05-23  2:45 UTC (permalink / raw)
  To: pmenzel; +Cc: jarkko, peterhuewe, jgg, linux-integrity, linux-kernel

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 shaopeijie@cestc.cn:
>> From: Peijie Shao <shaopeijie@cestc.cn>
>> 
>> 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 <shaopeijie@cestc.cn>
>> ---
>> 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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3] tpm_tis_spi: Release chip select when flow control fails
  2023-05-22  7:01 [PATCH v2] tpm_tis_spi: fix:release chip select when flow control fails shaopeijie
  2023-05-22  8:17 ` Paul Menzel
  2023-05-23  2:45 ` shaopeijie
@ 2023-05-23  2:45 ` shaopeijie
  2023-05-24  2:06   ` Jarkko Sakkinen
  2023-05-24  3:14   ` Jarkko Sakkinen
  2 siblings, 2 replies; 7+ messages in thread
From: shaopeijie @ 2023-05-23  2:45 UTC (permalink / raw)
  To: pmenzel; +Cc: jarkko, peterhuewe, jgg, linux-integrity, linux-kernel,
	Peijie Shao

From: Peijie Shao <shaopeijie@cestc.cn>

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 <shaopeijie@cestc.cn>
---
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




^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] tpm_tis_spi: Release chip select when flow control fails
  2023-05-23  2:45 ` [PATCH v3] tpm_tis_spi: Release " shaopeijie
@ 2023-05-24  2:06   ` Jarkko Sakkinen
  2023-05-24  3:14   ` Jarkko Sakkinen
  1 sibling, 0 replies; 7+ messages in thread
From: Jarkko Sakkinen @ 2023-05-24  2:06 UTC (permalink / raw)
  To: shaopeijie, pmenzel; +Cc: peterhuewe, jgg, linux-integrity, linux-kernel

On Tue May 23, 2023 at 5:45 AM EEST,  wrote:
> From: Peijie Shao <shaopeijie@cestc.cn>
>
> 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 <shaopeijie@cestc.cn>

Looks good to me + great explanation, thank you.

Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>

BR, Jarkko

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] tpm_tis_spi: fix:release chip select when flow control fails
  2023-05-22  8:17 ` Paul Menzel
@ 2023-05-24  2:52   ` Jarkko Sakkinen
  0 siblings, 0 replies; 7+ messages in thread
From: Jarkko Sakkinen @ 2023-05-24  2:52 UTC (permalink / raw)
  To: Paul Menzel, Peijie Shao; +Cc: peterhuewe, jgg, linux-integrity, linux-kernel

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 shaopeijie@cestc.cn:
> > From: Peijie Shao <shaopeijie@cestc.cn>
> > 
> > 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] tpm_tis_spi: Release chip select when flow control fails
  2023-05-23  2:45 ` [PATCH v3] tpm_tis_spi: Release " shaopeijie
  2023-05-24  2:06   ` Jarkko Sakkinen
@ 2023-05-24  3:14   ` Jarkko Sakkinen
  1 sibling, 0 replies; 7+ messages in thread
From: Jarkko Sakkinen @ 2023-05-24  3:14 UTC (permalink / raw)
  To: shaopeijie, pmenzel; +Cc: peterhuewe, jgg, linux-integrity, linux-kernel

On Tue May 23, 2023 at 5:45 AM EEST,  wrote:
> From: Peijie Shao <shaopeijie@cestc.cn>
>
> 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 <shaopeijie@cestc.cn>

Thanks I substitute the patch that I applied with this.

Again:

Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>

BR, Jarkko

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-05-24  3:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-22  7:01 [PATCH v2] tpm_tis_spi: fix:release chip select when flow control fails shaopeijie
2023-05-22  8:17 ` Paul Menzel
2023-05-24  2:52   ` Jarkko Sakkinen
2023-05-23  2:45 ` shaopeijie
2023-05-23  2:45 ` [PATCH v3] tpm_tis_spi: Release " shaopeijie
2023-05-24  2:06   ` Jarkko Sakkinen
2023-05-24  3:14   ` Jarkko Sakkinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox