[PATCH] hw/sd/ssi-sd: Handle immediate write start token after R1

Manuele Conti posted 1 patch 2 weeks, 4 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261104111619.24479-1-conti.manuele@gmail.com
Maintainers: "Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>, Bin Meng <bmeng.cn@gmail.com>
There is a newer version of this series
hw/sd/ssi-sd.c | 6 ++++++
1 file changed, 6 insertions(+)
[PATCH] hw/sd/ssi-sd: Handle immediate write start token after R1
Posted by Manuele Conti 2 weeks, 4 days ago
When a write start token immediately follows the R1 response,
the SPI SD controller may still be in SSI_SD_RESPONSE state
and discard the token.

Recognize single-block and multi-block write start tokens
when the SD card is ready to receive data, allowing the
write operation to proceed without an additional dummy byte.

Signed-off-by: Manuele Conti <conti.manuele@gmail.com>
---
 hw/sd/ssi-sd.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/hw/sd/ssi-sd.c b/hw/sd/ssi-sd.c
index a3a9f0c7efd..13c87c49cfb 100644
--- a/hw/sd/ssi-sd.c
+++ b/hw/sd/ssi-sd.c
@@ -185,6 +185,12 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)
             s->mode = SSI_SD_CMD;
             return SSI_DUMMY;
         }
+        /* The next byte after R1 may already be the write start token. */
+        if (sdbus_receive_ready(&s->sdbus) &&
+            (val == SSI_TOKEN_SINGLE || val == SSI_TOKEN_MULTI_WRITE)) {
+            s->mode = SSI_SD_DATA_WRITE;
+            return SSI_DUMMY;
+        }
         if (sdbus_data_ready(&s->sdbus)) {
             DPRINTF("Data read\n");
             s->mode = SSI_SD_DATA_START;
-- 
2.41.0
Re: [PATCH] hw/sd/ssi-sd: Handle immediate write start token after R1
Posted by Philippe Mathieu-Daudé 2 weeks, 4 days ago
Hi Manuele,

On 4/11/26 12:16, Manuele Conti wrote:
> When a write start token immediately follows the R1 response,
> the SPI SD controller may still be in SSI_SD_RESPONSE state
> and discard the token.
> 
> Recognize single-block and multi-block write start tokens
> when the SD card is ready to receive data, allowing the
> write operation to proceed without an additional dummy byte.

What guest is being run? Can you share the reproducer?

Unfortunately I only have access to the Simplified Spec and this
seems to be covered by the Full one.

> 
> Signed-off-by: Manuele Conti <conti.manuele@gmail.com>
> ---
>   hw/sd/ssi-sd.c | 6 ++++++
>   1 file changed, 6 insertions(+)
> 
> diff --git a/hw/sd/ssi-sd.c b/hw/sd/ssi-sd.c
> index a3a9f0c7efd..13c87c49cfb 100644
> --- a/hw/sd/ssi-sd.c
> +++ b/hw/sd/ssi-sd.c
> @@ -185,6 +185,12 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)
>               s->mode = SSI_SD_CMD;
>               return SSI_DUMMY;
>           }
> +        /* The next byte after R1 may already be the write start token. */
> +        if (sdbus_receive_ready(&s->sdbus) &&
> +            (val == SSI_TOKEN_SINGLE || val == SSI_TOKEN_MULTI_WRITE)) {
> +            s->mode = SSI_SD_DATA_WRITE;
> +            return SSI_DUMMY;
> +        }
>           if (sdbus_data_ready(&s->sdbus)) {
>               DPRINTF("Data read\n");
>               s->mode = SSI_SD_DATA_START;
[PATCH v2] hw/sd/ssi-sd: Handle immediate write start token after R1
Posted by Manuele Conti 2 weeks, 4 days ago
When a write start token immediately follows the R1 response,
the SPI SD controller may still be in SSI_SD_RESPONSE state
and discard the token.

Recognize single-block and multi-block write start tokens
when the SD card is ready to receive data, allowing the
write operation to proceed without an additional dummy byte.

Signed-off-by: Manuele Conti <conti.manuele@gmail.com>
---
Hi Philippe,

Thanks for your feedback.

I encountered this issue while running a bare-metal STM32F4 firmware under QEMU, using SPI mode to communicate with an SD card.

The firmware performs a block write (CMD24) and sends the data start token (0xFE) immediately after receiving the R1 response, without an additional dummy byte.

In this case, the SSI SD controller is still in SSI_SD_RESPONSE state and discards the token, causing the subsequent write operation to fail.

I've now added a regression test to reproduce this behavior without requiring the original firmware.

The test exercises the immediate write-token sequence and verifies that the controller correctly transitions to the data-write state.

I'll submit an updated patch including the regression test so the issue can be reproduced and verified independently.

Best regards,
Manuele

 hw/sd/ssi-sd.c                        |   6 +
 tests/qtest/ssi-sd-token-local-test.c | 165 ++++++++++++++++++++++++++
 2 files changed, 171 insertions(+)
 create mode 100644 tests/qtest/ssi-sd-token-local-test.c

diff --git a/hw/sd/ssi-sd.c b/hw/sd/ssi-sd.c
index a3a9f0c7efd..13c87c49cfb 100644
--- a/hw/sd/ssi-sd.c
+++ b/hw/sd/ssi-sd.c
@@ -185,6 +185,12 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)
             s->mode = SSI_SD_CMD;
             return SSI_DUMMY;
         }
+        /* The next byte after R1 may already be the write start token. */
+        if (sdbus_receive_ready(&s->sdbus) &&
+            (val == SSI_TOKEN_SINGLE || val == SSI_TOKEN_MULTI_WRITE)) {
+            s->mode = SSI_SD_DATA_WRITE;
+            return SSI_DUMMY;
+        }
         if (sdbus_data_ready(&s->sdbus)) {
             DPRINTF("Data read\n");
             s->mode = SSI_SD_DATA_START;
diff --git a/tests/qtest/ssi-sd-token-local-test.c b/tests/qtest/ssi-sd-token-local-test.c
new file mode 100644
index 00000000000..55ac5fec643
--- /dev/null
+++ b/tests/qtest/ssi-sd-token-local-test.c
@@ -0,0 +1,165 @@
+/*
+ * SD/SPI transfert tests
+ *
+ * Copyright (c) 2026 Manuele Conti <manuele.conti@archimede-energia.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "libqtest-single.h"
+
+#define SSP_BASE 0x40008000
+#define GPIO_D_DATA0 0x40007004
+#define GPIO_D_DIR 0x40007400
+#define BLOCK_SIZE 512
+
+typedef struct WriteCase {
+    const char *name;
+    bool multi;
+    unsigned dummy_bytes;
+} WriteCase;
+
+static uint8_t transfer(uint8_t value)
+{
+    writel(SSP_BASE + 8, value);
+    return readl(SSP_BASE + 8) & 0xff;
+}
+
+static uint8_t command(unsigned cmd, uint32_t argument)
+{
+    transfer(0xff);
+    transfer(0x40 | cmd);
+    transfer(argument >> 24);
+    transfer(argument >> 16);
+    transfer(argument >> 8);
+    transfer(argument);
+    transfer(cmd == 0 ? 0x95 : cmd == 8 ? 0x87 : 0xff);
+    for (unsigned i = 0; i < 32; i++) {
+        uint8_t response = transfer(0xff);
+        if (!(response & 0x80)) {
+            return response;
+        }
+    }
+    g_assert_not_reached();
+}
+
+static void initialize_card(void)
+{
+    bool ready = false;
+    bool token = false;
+
+    /* Drive D0 high and then low to select SD and deselect the OLED. */
+    writel(GPIO_D_DIR, 1);
+    writel(GPIO_D_DATA0, 1);
+    writel(GPIO_D_DATA0, 0);
+    writel(SSP_BASE, 7);       /* Eight-bit SPI frames. */
+    writel(SSP_BASE + 4, 2);   /* Enable PL022. */
+    for (unsigned i = 0; i < 10; i++) {
+        transfer(0xff);
+    }
+    g_assert_cmphex(command(0, 0), ==, 1);
+    g_assert_cmphex(command(8, 0x1aa), ==, 1);
+    for (unsigned i = 0; i < 4; i++) {
+        transfer(0xff);
+    }
+    for (unsigned i = 0; i < 100; i++) {
+        g_assert_cmphex(command(55, 0), ==, 1);
+        if (command(41, 0x40000000) == 0) {
+            ready = true;
+            break;
+        }
+        clock_step(1000000);
+    }
+    g_assert_true(ready);
+
+    /* Upstream's SPI CMD9 handler switches the card to transfer state. */
+    g_assert_cmphex(command(9, 0), ==, 0);
+    for (unsigned i = 0; i < 32; i++) {
+        if (transfer(0xff) == 0xfe) {
+            token = true;
+            break;
+        }
+    }
+    g_assert_true(token);
+    for (unsigned i = 0; i < 18; i++) {
+        transfer(0xff);       /* Sixteen CSD bytes and two CRC bytes. */
+    }
+    g_assert_cmphex(command(16, BLOCK_SIZE), ==, 0);
+}
+
+static void test_write(const void *opaque)
+{
+    const WriteCase *tc = opaque;
+    unsigned blocks = tc->multi ? 2 : 1;
+    uint8_t expected[2 * BLOCK_SIZE], actual[2 * BLOCK_SIZE];
+    uint8_t response[2] = { 0xff, 0xff };
+    bool accepted = true;
+    g_autofree char *path = NULL;
+    g_autofree char *args = NULL;
+    int fd = g_file_open_tmp("ssi-sd-token-local-XXXXXX", &path, NULL);
+    ssize_t bytes;
+
+    g_assert_cmpint(fd, >=, 0);
+    g_assert_cmpint(ftruncate(fd, 64 * 1024 * 1024), ==, 0);
+    args = g_strdup_printf("-M lm3s6965evb -serial none "
+                           "-drive file=%s,if=sd,format=raw", path);
+    qtest_start(args);
+    initialize_card();
+    g_assert_cmphex(command(tc->multi ? 25 : 24, 0), ==, 0);
+    for (unsigned i = 0; i < tc->dummy_bytes; i++) {
+        transfer(0xff);
+    }
+    for (unsigned block = 0; block < blocks; block++) {
+        /* Constant data avoids accidentally supplying a second start token. */
+        memset(expected + block * BLOCK_SIZE, 0xa5 + block, BLOCK_SIZE);
+        transfer(tc->multi ? 0xfc : 0xfe);
+        for (unsigned i = 0; i < BLOCK_SIZE; i++) {
+            transfer(expected[block * BLOCK_SIZE + i]);
+        }
+        transfer(0xff);
+        transfer(0xff);
+        response[block] = transfer(0xff);
+        if ((response[block] & 0x1f) != 0x05) {
+            accepted = false;
+            break;
+        }
+        /* This experiment isolates the first token after command R1. */
+        if (tc->multi && block + 1 < blocks) {
+            transfer(0xff);
+        }
+    }
+    if (tc->multi && accepted) {
+        transfer(0xff);
+        transfer(0xfd);
+        transfer(0xff);
+    }
+    qtest_end();
+    bytes = pread(fd, actual, blocks * BLOCK_SIZE, 0);
+    close(fd);
+    unlink(path);
+
+    g_test_message("CMD%u dummy=%u response[0]=0x%02x response[1]=0x%02x",
+                   tc->multi ? 25 : 24, tc->dummy_bytes,
+                   response[0], response[1]);
+    g_assert_true(accepted);
+    g_assert_cmpint(bytes, ==, blocks * BLOCK_SIZE);
+    g_assert_cmpmem(actual, blocks * BLOCK_SIZE,
+                   expected, blocks * BLOCK_SIZE);
+}
+
+int main(int argc, char **argv)
+{
+    static const WriteCase cases[] = {
+        { "/ssi-sd-local/single/immediate", false, 0 },
+        { "/ssi-sd-local/single/dummy", false, 1 },
+        { "/ssi-sd-local/multi/immediate", true, 0 },
+        { "/ssi-sd-local/multi/dummy", true, 1 },
+    };
+
+    g_test_init(&argc, &argv, NULL);
+    for (unsigned i = 0; i < G_N_ELEMENTS(cases); i++) {
+        qtest_add_data_func(cases[i].name, &cases[i], test_write);
+    }
+    return g_test_run();
+}
-- 
2.41.0
Re: [PATCH v2] hw/sd/ssi-sd: Handle immediate write start token after R1
Posted by Philippe Mathieu-Daudé 2 weeks, 4 days ago
Please keep Bin in Cc (as maintainer)

$ ./scripts/get_maintainer.pl -f hw/sd/ssi-sd.c
"Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com> (odd fixer:SD (Secure 
Card), gitlab:@philmd)
Bin Meng <bmeng.cn@gmail.com> (odd fixer:SD (Secure Card), gitlab:@lbmeng)

On 4/11/26 13:31, Manuele Conti wrote:
> When a write start token immediately follows the R1 response,
> the SPI SD controller may still be in SSI_SD_RESPONSE state
> and discard the token.
> 
> Recognize single-block and multi-block write start tokens
> when the SD card is ready to receive data, allowing the
> write operation to proceed without an additional dummy byte.
> 
> Signed-off-by: Manuele Conti <conti.manuele@gmail.com>
> ---
> Hi Philippe,
> 
> Thanks for your feedback.
> 
> I encountered this issue while running a bare-metal STM32F4 firmware under QEMU, using SPI mode to communicate with an SD card.
> 
> The firmware performs a block write (CMD24) and sends the data start token (0xFE) immediately after receiving the R1 response, without an additional dummy byte.
> 
> In this case, the SSI SD controller is still in SSI_SD_RESPONSE state and discards the token, causing the subsequent write operation to fail.
> 
> I've now added a regression test to reproduce this behavior without requiring the original firmware.

Wow, that was fast! I suppose you already had that qtest written
locally, if so next time better share along with a fix.

Looking at older spec v1.10 I found section 7.5.4 "Timing Values" and
Figure 56 "Write operation - bus timing". The table gives NWR (between
the write command response and the data block) a minimum of 1, in
units of 8 clock cycles (so at least one byte). If that reading is
right, a host sending 0xFE in the very next byte after R1 is outside
the card timing, and the model dropping it is not obviously a bug.

You described the model but not the guest, what firmware / drivers
it uses. Does your firmware work on real HW -- and if so what is the
SD card being used --? Does the FW intentionally send the token with
no gap, or does it poll/read a byte after the command response that
our SPI controller model hides or reorders?

Thanks,

Phil.

> The test exercises the immediate write-token sequence and verifies that the controller correctly transitions to the data-write state.
> 
> I'll submit an updated patch including the regression test so the issue can be reproduced and verified independently.
> 
> Best regards,
> Manuele
> 
>   hw/sd/ssi-sd.c                        |   6 +
>   tests/qtest/ssi-sd-token-local-test.c | 165 ++++++++++++++++++++++++++
>   2 files changed, 171 insertions(+)
>   create mode 100644 tests/qtest/ssi-sd-token-local-test.c


Re: [PATCH v2] hw/sd/ssi-sd: Handle immediate write start token after R1
Posted by Manuele Conti 2 weeks, 4 days ago
Hi Philippe, Bin,

Thank you for the detailed review and for pointing out the NWR timing
requirement.

I checked section 7.5.4 and Figure 56 of the SD Physical Layer
Specification v1.10. I agree that NWR has a minimum of one byte (8 clock
cycles), which means my zero-gap test case does not establish a
protocol-compliant host sequence.

The other observation about matching the write tokens to CMD24/CMD25 and
handling the CMD25 data response is also valid.

Before sending another revision, I will investigate the guest-side SPI
sequence and check whether the issue can be reproduced with the required
NWR interval.

I will also check whether the firmware is accounting for this delay or
whether the problem is caused by the interaction with QEMU's response
handling.

I won't submit a v3 until I can demonstrate a protocol-compliant reproducer.

Thanks again for the careful review.

Best regards,
Manuele

Il giorno mer 4 nov 2026 alle ore 15:25 Philippe Mathieu-Daudé <
philmd@oss.qualcomm.com> ha scritto:

> Please keep Bin in Cc (as maintainer)
>
> $ ./scripts/get_maintainer.pl -f hw/sd/ssi-sd.c
> "Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com> (odd fixer:SD (Secure
> Card), gitlab:@philmd)
> Bin Meng <bmeng.cn@gmail.com> (odd fixer:SD (Secure Card), gitlab:@lbmeng)
>
> On 4/11/26 13:31, Manuele Conti wrote:
> > When a write start token immediately follows the R1 response,
> > the SPI SD controller may still be in SSI_SD_RESPONSE state
> > and discard the token.
> >
> > Recognize single-block and multi-block write start tokens
> > when the SD card is ready to receive data, allowing the
> > write operation to proceed without an additional dummy byte.
> >
> > Signed-off-by: Manuele Conti <conti.manuele@gmail.com>
> > ---
> > Hi Philippe,
> >
> > Thanks for your feedback.
> >
> > I encountered this issue while running a bare-metal STM32F4 firmware
> under QEMU, using SPI mode to communicate with an SD card.
> >
> > The firmware performs a block write (CMD24) and sends the data start
> token (0xFE) immediately after receiving the R1 response, without an
> additional dummy byte.
> >
> > In this case, the SSI SD controller is still in SSI_SD_RESPONSE state
> and discards the token, causing the subsequent write operation to fail.
> >
> > I've now added a regression test to reproduce this behavior without
> requiring the original firmware.
>
> Wow, that was fast! I suppose you already had that qtest written
> locally, if so next time better share along with a fix.
>
> Looking at older spec v1.10 I found section 7.5.4 "Timing Values" and
> Figure 56 "Write operation - bus timing". The table gives NWR (between
> the write command response and the data block) a minimum of 1, in
> units of 8 clock cycles (so at least one byte). If that reading is
> right, a host sending 0xFE in the very next byte after R1 is outside
> the card timing, and the model dropping it is not obviously a bug.
>
> You described the model but not the guest, what firmware / drivers
> it uses. Does your firmware work on real HW -- and if so what is the
> SD card being used --? Does the FW intentionally send the token with
> no gap, or does it poll/read a byte after the command response that
> our SPI controller model hides or reorders?
>
> Thanks,
>
> Phil.
>
> > The test exercises the immediate write-token sequence and verifies that
> the controller correctly transitions to the data-write state.
> >
> > I'll submit an updated patch including the regression test so the issue
> can be reproduced and verified independently.
> >
> > Best regards,
> > Manuele
> >
> >   hw/sd/ssi-sd.c                        |   6 +
> >   tests/qtest/ssi-sd-token-local-test.c | 165 ++++++++++++++++++++++++++
> >   2 files changed, 171 insertions(+)
> >   create mode 100644 tests/qtest/ssi-sd-token-local-test.c
>
>
Re: [PATCH v2] hw/sd/ssi-sd: Handle immediate write start token after R1
Posted by Manuele Conti 2 weeks, 4 days ago
Hi Philippe, Bin,

I've completed the investigation using the unchanged STM32F4 firmware ELF.

You were right about the NWR timing. The SPI trace confirms that the
firmware sends 0xFE immediately after R1, without the required clock
interval.

Therefore, the proposed patch is a workaround for the guest behavior rather
than a QEMU bug fix. I won't pursue it upstream.

However, the investigation also revealed a separate QEMU regression in SPI
mode.

After ACMD41, the card remains in sd_ready_state, but CMD16 requires
sd_transfer_state. This causes CMD16 to fail and the subsequent CMD9 to
return R1 = 0x04 (ILLEGAL_COMMAND).

I traced this behavior back to changes introduced in QEMU 10.1.0. Restoring
the SPI transition to sd_transfer_state after ACMD41 fixes the
initialization failure with the unchanged firmware.

I'll address the CMD16/ACMD41 issue separately.

Thanks again for your helpful feedback.

Best regards,
Manuele

Il giorno mer 4 nov 2026 alle ore 21:21 Manuele Conti <
conti.manuele@gmail.com> ha scritto:

> Hi Philippe, Bin,
>
> Thank you for the detailed review and for pointing out the NWR timing
> requirement.
>
> I checked section 7.5.4 and Figure 56 of the SD Physical Layer
> Specification v1.10. I agree that NWR has a minimum of one byte (8 clock
> cycles), which means my zero-gap test case does not establish a
> protocol-compliant host sequence.
>
> The other observation about matching the write tokens to CMD24/CMD25 and
> handling the CMD25 data response is also valid.
>
> Before sending another revision, I will investigate the guest-side SPI
> sequence and check whether the issue can be reproduced with the required
> NWR interval.
>
> I will also check whether the firmware is accounting for this delay or
> whether the problem is caused by the interaction with QEMU's response
> handling.
>
> I won't submit a v3 until I can demonstrate a protocol-compliant
> reproducer.
>
> Thanks again for the careful review.
>
> Best regards,
> Manuele
>
> Il giorno mer 4 nov 2026 alle ore 15:25 Philippe Mathieu-Daudé <
> philmd@oss.qualcomm.com> ha scritto:
>
>> Please keep Bin in Cc (as maintainer)
>>
>> $ ./scripts/get_maintainer.pl -f hw/sd/ssi-sd.c
>> "Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com> (odd fixer:SD (Secure
>> Card), gitlab:@philmd)
>> Bin Meng <bmeng.cn@gmail.com> (odd fixer:SD (Secure Card), gitlab:@lbmeng
>> )
>>
>> On 4/11/26 13:31, Manuele Conti wrote:
>> > When a write start token immediately follows the R1 response,
>> > the SPI SD controller may still be in SSI_SD_RESPONSE state
>> > and discard the token.
>> >
>> > Recognize single-block and multi-block write start tokens
>> > when the SD card is ready to receive data, allowing the
>> > write operation to proceed without an additional dummy byte.
>> >
>> > Signed-off-by: Manuele Conti <conti.manuele@gmail.com>
>> > ---
>> > Hi Philippe,
>> >
>> > Thanks for your feedback.
>> >
>> > I encountered this issue while running a bare-metal STM32F4 firmware
>> under QEMU, using SPI mode to communicate with an SD card.
>> >
>> > The firmware performs a block write (CMD24) and sends the data start
>> token (0xFE) immediately after receiving the R1 response, without an
>> additional dummy byte.
>> >
>> > In this case, the SSI SD controller is still in SSI_SD_RESPONSE state
>> and discards the token, causing the subsequent write operation to fail.
>> >
>> > I've now added a regression test to reproduce this behavior without
>> requiring the original firmware.
>>
>> Wow, that was fast! I suppose you already had that qtest written
>> locally, if so next time better share along with a fix.
>>
>> Looking at older spec v1.10 I found section 7.5.4 "Timing Values" and
>> Figure 56 "Write operation - bus timing". The table gives NWR (between
>> the write command response and the data block) a minimum of 1, in
>> units of 8 clock cycles (so at least one byte). If that reading is
>> right, a host sending 0xFE in the very next byte after R1 is outside
>> the card timing, and the model dropping it is not obviously a bug.
>>
>> You described the model but not the guest, what firmware / drivers
>> it uses. Does your firmware work on real HW -- and if so what is the
>> SD card being used --? Does the FW intentionally send the token with
>> no gap, or does it poll/read a byte after the command response that
>> our SPI controller model hides or reorders?
>>
>> Thanks,
>>
>> Phil.
>>
>> > The test exercises the immediate write-token sequence and verifies that
>> the controller correctly transitions to the data-write state.
>> >
>> > I'll submit an updated patch including the regression test so the issue
>> can be reproduced and verified independently.
>> >
>> > Best regards,
>> > Manuele
>> >
>> >   hw/sd/ssi-sd.c                        |   6 +
>> >   tests/qtest/ssi-sd-token-local-test.c | 165 ++++++++++++++++++++++++++
>> >   2 files changed, 171 insertions(+)
>> >   create mode 100644 tests/qtest/ssi-sd-token-local-test.c
>>
>>
Re: [PATCH v2] hw/sd/ssi-sd: Handle immediate write start token after R1
Posted by Philippe Mathieu-Daudé 2 weeks, 4 days ago
On 4/11/26 13:31, Manuele Conti wrote:
> When a write start token immediately follows the R1 response,
> the SPI SD controller may still be in SSI_SD_RESPONSE state
> and discard the token.
> 
> Recognize single-block and multi-block write start tokens
> when the SD card is ready to receive data, allowing the
> write operation to proceed without an additional dummy byte.
> 
> Signed-off-by: Manuele Conti <conti.manuele@gmail.com>

>   hw/sd/ssi-sd.c                        |   6 +
>   tests/qtest/ssi-sd-token-local-test.c | 165 ++++++++++++++++++++++++++
>   2 files changed, 171 insertions(+)
>   create mode 100644 tests/qtest/ssi-sd-token-local-test.c
> 
> diff --git a/hw/sd/ssi-sd.c b/hw/sd/ssi-sd.c
> index a3a9f0c7efd..13c87c49cfb 100644
> --- a/hw/sd/ssi-sd.c
> +++ b/hw/sd/ssi-sd.c
> @@ -185,6 +185,12 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)
>               s->mode = SSI_SD_CMD;
>               return SSI_DUMMY;
>           }
> +        /* The next byte after R1 may already be the write start token. */
> +        if (sdbus_receive_ready(&s->sdbus) &&
> +            (val == SSI_TOKEN_SINGLE || val == SSI_TOKEN_MULTI_WRITE)) {
> +            s->mode = SSI_SD_DATA_WRITE;
> +            return SSI_DUMMY;

IIUC this does not check that the token type matches the command
(0xFC for CMD25, 0xFE for CMD24), and accepts a zero-gap 0xFC
right after a CMD25 data response. Is that deliberate? (I don't
think, but if so this should be mentioned in commit description).

> +        }
>           if (sdbus_data_ready(&s->sdbus)) {
>               DPRINTF("Data read\n");
>               s->mode = SSI_SD_DATA_START;