2021-09-14 11:49:50

by Jonas Dreßler

[permalink] [raw]
Subject: [PATCH v2 0/2] mwifiex: Work around firmware bugs on 88W8897 chip

This is the second revision of the patch, the first one is here:
https://lore.kernel.org/linux-wireless/[email protected]/

Changes between v1 and v2:
- Only read-back the register write to the TX ring write pointer, not all writes
- Mention firmware version in commit message+code comment for future reference
- Use -EIO return value in second patch
- Use definitions for waiting intervals in second patch

Jonas Dreßler (2):
mwifiex: Use non-posted PCI write when setting TX ring write pointer
mwifiex: Try waking the firmware until we get an interrupt

drivers/net/wireless/marvell/mwifiex/pcie.c | 59 +++++++++++++++++----
1 file changed, 50 insertions(+), 9 deletions(-)

--
2.31.1


2021-09-14 11:49:50

by Jonas Dreßler

[permalink] [raw]
Subject: [PATCH v2 1/2] mwifiex: Use non-posted PCI write when setting TX ring write pointer

On the 88W8897 card it's very important the TX ring write pointer is
updated correctly to its new value before setting the TX ready
interrupt, otherwise the firmware appears to crash (probably because
it's trying to DMA-read from the wrong place). The issue is present in
the latest firmware version 15.68.19.p21 of the pcie+usb card.

Since PCI uses "posted writes" when writing to a register, it's not
guaranteed that a write will happen immediately. That means the pointer
might be outdated when setting the TX ready interrupt, leading to
firmware crashes especially when ASPM L1 and L1 substates are enabled
(because of the higher link latency, the write will probably take
longer).

So fix those firmware crashes by always using a non-posted write for
this specific register write. We do that by simply reading back the
register after writing it, just as a few other PCI drivers do.

This fixes a bug where during rx/tx traffic and with ASPM L1 substates
enabled (the enabled substates are platform dependent), the firmware
crashes and eventually a command timeout appears in the logs.

Cc: [email protected]
Signed-off-by: Jonas Dreßler <[email protected]>
---
drivers/net/wireless/marvell/mwifiex/pcie.c | 26 ++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/drivers/net/wireless/marvell/mwifiex/pcie.c b/drivers/net/wireless/marvell/mwifiex/pcie.c
index c6ccce426b49..0eff717ac5fa 100644
--- a/drivers/net/wireless/marvell/mwifiex/pcie.c
+++ b/drivers/net/wireless/marvell/mwifiex/pcie.c
@@ -240,6 +240,20 @@ static int mwifiex_write_reg(struct mwifiex_adapter *adapter, int reg, u32 data)
return 0;
}

+/*
+ * This function does a non-posted write into a PCIE card register, ensuring
+ * it's completion before returning.
+ */
+static int mwifiex_write_reg_np(struct mwifiex_adapter *adapter, int reg, u32 data)
+{
+ struct pcie_service_card *card = adapter->card;
+
+ iowrite32(data, card->pci_mmap1 + reg);
+ ioread32(card->pci_mmap1 + reg);
+
+ return 0;
+}
+
/* This function reads data from PCIE card register.
*/
static int mwifiex_read_reg(struct mwifiex_adapter *adapter, int reg, u32 *data)
@@ -1482,9 +1496,15 @@ mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb,
reg->tx_rollover_ind);

rx_val = card->rxbd_rdptr & reg->rx_wrap_mask;
- /* Write the TX ring write pointer in to reg->tx_wrptr */
- if (mwifiex_write_reg(adapter, reg->tx_wrptr,
- card->txbd_wrptr | rx_val)) {
+ /* Write the TX ring write pointer in to reg->tx_wrptr.
+ * The firmware (latest version 15.68.19.p21) of the 88W8897
+ * pcie+usb card seems to crash when getting the TX ready
+ * interrupt but the TX ring write pointer points to an outdated
+ * address, so it's important we do a non-posted write here to
+ * force the completion of the write.
+ */
+ if (mwifiex_write_reg_np(adapter, reg->tx_wrptr,
+ card->txbd_wrptr | rx_val)) {
mwifiex_dbg(adapter, ERROR,
"SEND DATA: failed to write reg->tx_wrptr\n");
ret = -1;
--
2.31.1

2021-09-14 11:51:20

by Jonas Dreßler

[permalink] [raw]
Subject: [PATCH v2 2/2] mwifiex: Try waking the firmware until we get an interrupt

It seems that the firmware of the 88W8897 card sometimes ignores or
misses when we try to wake it up by writing to the firmware status
register. This leads to the firmware wakeup timeout expiring and the
driver resetting the card because we assume the firmware has hung up or
crashed (unfortunately that's not unlikely with this card).

Turns out that most of the time the firmware actually didn't hang up,
but simply "missed" our wakeup request and didn't send us an AWAKE
event.

Trying again to read the firmware status register after a short timeout
usually makes the firmware wake up as expected, so add a small retry
loop to mwifiex_pm_wakeup_card() that looks at the interrupt status to
check whether the card woke up.

The number of tries and timeout lengths for this were determined
experimentally: The firmware usually takes about 500 us to wake up
after we attempt to read the status register. In some cases where the
firmware is very busy (for example while doing a bluetooth scan) it
might even miss our requests for multiple milliseconds, which is why
after 15 tries the waiting time gets increased to 10 ms. The maximum
number of tries it took to wake the firmware when testing this was
around 20, so a maximum number of 50 tries should give us plenty of
safety margin.

A good reproducer for this issue is letting the firmware sleep and wake
up in very short intervals, for example by pinging a device on the
network every 0.1 seconds.

Cc: [email protected]
Signed-off-by: Jonas Dreßler <[email protected]>
---
drivers/net/wireless/marvell/mwifiex/pcie.c | 33 +++++++++++++++++----
1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/drivers/net/wireless/marvell/mwifiex/pcie.c b/drivers/net/wireless/marvell/mwifiex/pcie.c
index 0eff717ac5fa..7fea319e013c 100644
--- a/drivers/net/wireless/marvell/mwifiex/pcie.c
+++ b/drivers/net/wireless/marvell/mwifiex/pcie.c
@@ -661,11 +661,15 @@ static void mwifiex_delay_for_sleep_cookie(struct mwifiex_adapter *adapter,
"max count reached while accessing sleep cookie\n");
}

+#define N_WAKEUP_TRIES_SHORT_INTERVAL 15
+#define N_WAKEUP_TRIES_LONG_INTERVAL 35
+
/* This function wakes up the card by reading fw_status register. */
static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
{
struct pcie_service_card *card = adapter->card;
const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
+ int n_tries = 0;

mwifiex_dbg(adapter, EVENT,
"event: Wakeup device...\n");
@@ -673,12 +677,29 @@ static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
if (reg->sleep_cookie)
mwifiex_pcie_dev_wakeup_delay(adapter);

- /* Accessing fw_status register will wakeup device */
- if (mwifiex_write_reg(adapter, reg->fw_status, FIRMWARE_READY_PCIE)) {
- mwifiex_dbg(adapter, ERROR,
- "Writing fw_status register failed\n");
- return -1;
- }
+ /* Access the fw_status register to wake up the device.
+ * Since the 88W8897 firmware sometimes appears to ignore or miss
+ * that wakeup request, we continue trying until we receive an
+ * interrupt from the card.
+ */
+ do {
+ if (mwifiex_write_reg(adapter, reg->fw_status, FIRMWARE_READY_PCIE)) {
+ mwifiex_dbg(adapter, ERROR,
+ "Writing fw_status register failed\n");
+ return -EIO;
+ }
+
+ n_tries++;
+
+ if (n_tries <= N_WAKEUP_TRIES_SHORT_INTERVAL)
+ usleep_range(400, 700);
+ else
+ msleep(10);
+ } while (n_tries <= N_WAKEUP_TRIES_SHORT_INTERVAL + N_WAKEUP_TRIES_LONG_INTERVAL &&
+ READ_ONCE(adapter->int_status) == 0);
+
+ mwifiex_dbg(adapter, EVENT,
+ "event: Tried %d times until firmware woke up\n", n_tries);

if (reg->sleep_cookie) {
mwifiex_pcie_dev_wakeup_delay(adapter);
--
2.31.1

2021-09-22 13:23:45

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] mwifiex: Use non-posted PCI write when setting TX ring write pointer

On Wed, Sep 22, 2021 at 02:08:39PM +0200, Jonas Dre?ler wrote:
> On 9/22/21 1:17 PM, Andy Shevchenko wrote:
> > On Tue, Sep 14, 2021 at 01:48:12PM +0200, Jonas Dre?ler wrote:

...

> > Should it have a Fixes tag?
> >
>
> Don't think so, there's the infamous
> (https://bugzilla.kernel.org/show_bug.cgi?id=109681) Bugzilla bug it fixes
> though, I'll mention that in v3.

Good idea, use BugLink tag for that!

...

> Interesting, I haven't noticed that mwifiex_write_reg() always returns 0. So
> are you suggesting to remove that return value and get rid of all the "if
> (mwifiex_write_reg()) {}" checks in a separate commit?

Something like this, yes.

> As for why the dummy read/write functions exist, I have no idea. Looking at
> git history it seems they were always there (only change is that
> mwifiex_read_reg() started to handle read errors with commit
> af05148392f50490c662dccee6c502d9fcba33e2). My bet would be that they were
> created to be consistent with sdio.c which is the oldest supported bus type
> in mwifiex.

It has a check against all ones. Also your another patch mentioned wake up.
Perhaps the purpose is to wake up and return if device was/is in power off mode
(D3hot).

--
With Best Regards,
Andy Shevchenko


2021-09-22 15:55:50

by David Laight

[permalink] [raw]
Subject: RE: [PATCH v2 1/2] mwifiex: Use non-posted PCI write when setting TX ring write pointer


From: Pali Rohár
> Sent: 22 September 2021 15:27
>
> On Wednesday 22 September 2021 14:03:25 David Laight wrote:
> > From: Jonas Dreßler
> > > Sent: 14 September 2021 12:48
> > >
> > > On the 88W8897 card it's very important the TX ring write pointer is
> > > updated correctly to its new value before setting the TX ready
> > > interrupt, otherwise the firmware appears to crash (probably because
> > > it's trying to DMA-read from the wrong place). The issue is present in
> > > the latest firmware version 15.68.19.p21 of the pcie+usb card.
> > >
> > > Since PCI uses "posted writes" when writing to a register, it's not
> > > guaranteed that a write will happen immediately. That means the pointer
> > > might be outdated when setting the TX ready interrupt, leading to
> > > firmware crashes especially when ASPM L1 and L1 substates are enabled
> > > (because of the higher link latency, the write will probably take
> > > longer).
> > >
> > > So fix those firmware crashes by always using a non-posted write for
> > > this specific register write. We do that by simply reading back the
> > > register after writing it, just as a few other PCI drivers do.
> > >
> > > This fixes a bug where during rx/tx traffic and with ASPM L1 substates
> > > enabled (the enabled substates are platform dependent), the firmware
> > > crashes and eventually a command timeout appears in the logs.
> >
> > I think you need to change your terminology.
> > PCIe does have some non-posted write transactions - but I can't
> > remember when they are used.
>
> In PCIe are all memory write requests as posted.
>
> Non-posted writes in PCIe are used only for IO and config requests. But
> this is not case for proposed patch change as it access only card's
> memory space.
>
> Technically this patch does not use non-posted memory write (as PCIe
> does not support / provide it), just adds something like a barrier and
> I'm not sure if it is really correct (you already wrote more details
> about it, so I will let it be).
>
> I'm not sure what is the correct terminology, I do not know how this
> kind of write-followed-by-read "trick" is correctly called.

I think it is probably best to say:
"flush the posted write when setting the TX ring write pointer".

The write can get posted in any/all of the following places:
1) The cpu store buffer.
2) The PCIe host bridge.
3) Any other PCIe bridges.
4) The PCIe slave logic in the target.
There could be separate buffers for each BAR,
5) The actual target logic for that address block.
The target (probably) will look a bit like an old fashioned cpu
motherboard with the PCIe slave logic as the main bus master.

The readback forces all the posted write buffers be flushed.

In this case I suspect it is either flushing (5) or the extra
delay of the read TLP processing that 'fixes' the problem.

Note that depending on the exact code and host cpu the second
write may not need to wait for the response to the read TLP.
So the write, readback, write TLP may be back to back on the
actual PCIe link.

Although I don't have access to an actual PCIe monitor we
do have the ability to trace 'data' TLP into fpga memory
on one of our systems.
This is near real-time but they are slightly munged.
Watching the TLP can be illuminating!

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

2021-09-27 20:31:20

by Brian Norris

[permalink] [raw]
Subject: Re: [PATCH v2 0/2] mwifiex: Work around firmware bugs on 88W8897 chip

On Tue, Sep 14, 2021 at 4:48 AM Jonas Dreßler <[email protected]> wrote:
>
> This is the second revision of the patch, the first one is here:
> https://lore.kernel.org/linux-wireless/[email protected]/
>
> Changes between v1 and v2:
> - Only read-back the register write to the TX ring write pointer, not all writes
> - Mention firmware version in commit message+code comment for future reference
> - Use -EIO return value in second patch
> - Use definitions for waiting intervals in second patch

I tested this version, and it doesn't have the same issues v1 had
(regarding long-blocking reads, causing audio dropouts, etc.), so:

Tested-by: Brian Norris <[email protected]>

As suggested elsewhere, this polling loop approach is a little slower
than just waiting for an interrupt instead (and that proves out; the
wakeup latency seems to increase by ~1 "short" polling interval; so
about half a millisecond). It seems like that could be optimized if
needed, because you *are* still waiting for an interrupt anyway. But I
haven't tried benchmarking anything that would really matter for this;
when we're already waiting 6+ ms, another 0.5ms isn't the end of the
world.

This doesn't really count as Reviewed-by. There are probably better
improvements to the poling loop (e.g., Andy's existing suggestions);
and frankly, I'd rather see if the dropped writes can be fixed
somehow. But I'm not holding my breath for the latter, and don't have
any good suggestions. So if this is the best we can do, so be it.

Regards,
Brian

2021-09-30 15:01:28

by Jonas Dreßler

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] mwifiex: Use non-posted PCI write when setting TX ring write pointer

On 9/22/21 5:54 PM, David Laight wrote:
>
> From: Pali Rohár
>> Sent: 22 September 2021 15:27
>>
>> On Wednesday 22 September 2021 14:03:25 David Laight wrote:
>>> From: Jonas Dreßler
>>>> Sent: 14 September 2021 12:48
>>>>
>>>> On the 88W8897 card it's very important the TX ring write pointer is
>>>> updated correctly to its new value before setting the TX ready
>>>> interrupt, otherwise the firmware appears to crash (probably because
>>>> it's trying to DMA-read from the wrong place). The issue is present in
>>>> the latest firmware version 15.68.19.p21 of the pcie+usb card.
>>>>
>>>> Since PCI uses "posted writes" when writing to a register, it's not
>>>> guaranteed that a write will happen immediately. That means the pointer
>>>> might be outdated when setting the TX ready interrupt, leading to
>>>> firmware crashes especially when ASPM L1 and L1 substates are enabled
>>>> (because of the higher link latency, the write will probably take
>>>> longer).
>>>>
>>>> So fix those firmware crashes by always using a non-posted write for
>>>> this specific register write. We do that by simply reading back the
>>>> register after writing it, just as a few other PCI drivers do.
>>>>
>>>> This fixes a bug where during rx/tx traffic and with ASPM L1 substates
>>>> enabled (the enabled substates are platform dependent), the firmware
>>>> crashes and eventually a command timeout appears in the logs.
>>>
>>> I think you need to change your terminology.
>>> PCIe does have some non-posted write transactions - but I can't
>>> remember when they are used.
>>
>> In PCIe are all memory write requests as posted.
>>
>> Non-posted writes in PCIe are used only for IO and config requests. But
>> this is not case for proposed patch change as it access only card's
>> memory space.
>>
>> Technically this patch does not use non-posted memory write (as PCIe
>> does not support / provide it), just adds something like a barrier and
>> I'm not sure if it is really correct (you already wrote more details
>> about it, so I will let it be).
>>
>> I'm not sure what is the correct terminology, I do not know how this
>> kind of write-followed-by-read "trick" is correctly called.
>
> I think it is probably best to say:
> "flush the posted write when setting the TX ring write pointer".
>
> The write can get posted in any/all of the following places:
> 1) The cpu store buffer.
> 2) The PCIe host bridge.
> 3) Any other PCIe bridges.
> 4) The PCIe slave logic in the target.
> There could be separate buffers for each BAR,
> 5) The actual target logic for that address block.
> The target (probably) will look a bit like an old fashioned cpu
> motherboard with the PCIe slave logic as the main bus master.
>
> The readback forces all the posted write buffers be flushed.
>
> In this case I suspect it is either flushing (5) or the extra
> delay of the read TLP processing that 'fixes' the problem.
>
> Note that depending on the exact code and host cpu the second
> write may not need to wait for the response to the read TLP.
> So the write, readback, write TLP may be back to back on the
> actual PCIe link.
>
> Although I don't have access to an actual PCIe monitor we
> do have the ability to trace 'data' TLP into fpga memory
> on one of our systems.
> This is near real-time but they are slightly munged.
> Watching the TLP can be illuminating!
>
> David
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)
>

Thanks for the detailed explanations, it looks like indeed the read-back
is not the real fix here, a simple udelay(50) before sending the "TX
ready" interrupt also does the trick.

} else {
+ udelay(50);
+
/* Send the TX ready interrupt */
if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
CPU_INTR_DNLD_RDY)) {

I've tested that for a week now and haven't seen any firmware crashes.
Interestingly enough it looks like the delay can also be added after
setting the "TX ready" interrupt, just not before updating the TX ring
write pointer.

I have no idea if 50 usecs is a good duration to wait here, from trying
different values I found that 10 to 20 usecs is not enough, but who
knows, maybe that's platform dependent?

2021-09-30 19:05:50

by Jonas Dreßler

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] mwifiex: Try waking the firmware until we get an interrupt

On 9/22/21 1:19 PM, Andy Shevchenko wrote:
> On Tue, Sep 14, 2021 at 01:48:13PM +0200, Jonas Dreßler wrote:
>> It seems that the firmware of the 88W8897 card sometimes ignores or
>> misses when we try to wake it up by writing to the firmware status
>> register. This leads to the firmware wakeup timeout expiring and the
>> driver resetting the card because we assume the firmware has hung up or
>> crashed (unfortunately that's not unlikely with this card).
>>
>> Turns out that most of the time the firmware actually didn't hang up,
>> but simply "missed" our wakeup request and didn't send us an AWAKE
>> event.
>>
>> Trying again to read the firmware status register after a short timeout
>> usually makes the firmware wake up as expected, so add a small retry
>> loop to mwifiex_pm_wakeup_card() that looks at the interrupt status to
>> check whether the card woke up.
>>
>> The number of tries and timeout lengths for this were determined
>> experimentally: The firmware usually takes about 500 us to wake up
>> after we attempt to read the status register. In some cases where the
>> firmware is very busy (for example while doing a bluetooth scan) it
>> might even miss our requests for multiple milliseconds, which is why
>> after 15 tries the waiting time gets increased to 10 ms. The maximum
>> number of tries it took to wake the firmware when testing this was
>> around 20, so a maximum number of 50 tries should give us plenty of
>> safety margin.
>>
>> A good reproducer for this issue is letting the firmware sleep and wake
>> up in very short intervals, for example by pinging a device on the
>> network every 0.1 seconds.
>
> ...
>
>> + do {
>> + if (mwifiex_write_reg(adapter, reg->fw_status, FIRMWARE_READY_PCIE)) {
>> + mwifiex_dbg(adapter, ERROR,
>> + "Writing fw_status register failed\n");
>> + return -EIO;
>> + }
>> +
>> + n_tries++;
>> +
>> + if (n_tries <= N_WAKEUP_TRIES_SHORT_INTERVAL)
>> + usleep_range(400, 700);
>> + else
>> + msleep(10);
>> + } while (n_tries <= N_WAKEUP_TRIES_SHORT_INTERVAL + N_WAKEUP_TRIES_LONG_INTERVAL &&
>> + READ_ONCE(adapter->int_status) == 0);
>
> Can't you use read_poll_timeout() twice instead of this custom approach?
>

I've tried this now, but read_poll_timeout() is not ideal for our
use-case. What we'd need would be read->sleep->poll->repeat instead of
read->poll->sleep->repeat. With read_poll_timeout() we always end up
doing one more (unnecessary) write.

>> + mwifiex_dbg(adapter, EVENT,
>> + "event: Tried %d times until firmware woke up\n", n_tries);
>

2021-09-30 21:36:24

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] mwifiex: Try waking the firmware until we get an interrupt

On Thu, Sep 30, 2021 at 08:04:00PM +0200, Jonas Dre?ler wrote:
> On 9/22/21 1:19 PM, Andy Shevchenko wrote:
> > On Tue, Sep 14, 2021 at 01:48:13PM +0200, Jonas Dre?ler wrote:

...

> > > + do {
> > > + if (mwifiex_write_reg(adapter, reg->fw_status, FIRMWARE_READY_PCIE)) {
> > > + mwifiex_dbg(adapter, ERROR,
> > > + "Writing fw_status register failed\n");
> > > + return -EIO;
> > > + }
> > > +
> > > + n_tries++;
> > > +
> > > + if (n_tries <= N_WAKEUP_TRIES_SHORT_INTERVAL)
> > > + usleep_range(400, 700);
> > > + else
> > > + msleep(10);
> > > + } while (n_tries <= N_WAKEUP_TRIES_SHORT_INTERVAL + N_WAKEUP_TRIES_LONG_INTERVAL &&
> > > + READ_ONCE(adapter->int_status) == 0);
> >
> > Can't you use read_poll_timeout() twice instead of this custom approach?
>
> I've tried this now, but read_poll_timeout() is not ideal for our use-case.
> What we'd need would be read->sleep->poll->repeat instead of
> read->poll->sleep->repeat. With read_poll_timeout() we always end up doing
> one more (unnecessary) write.

First of all, there is a parameter to get sleep beforehand.
Second, what is the problem with having one write more or less?
Your current code doesn't guarantee this either. It only decreases
probability of such scenario. Am I wrong?


--
With Best Regards,
Andy Shevchenko


2021-09-30 21:37:43

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] mwifiex: Try waking the firmware until we get an interrupt

On Thu, Sep 30, 2021 at 11:07:09PM +0200, Jonas Dre?ler wrote:
> On 9/30/21 10:58 PM, Andy Shevchenko wrote:
> > On Thu, Sep 30, 2021 at 08:04:00PM +0200, Jonas Dre?ler wrote:

...

> > Second, what is the problem with having one write more or less?
> > Your current code doesn't guarantee this either. It only decreases
> > probability of such scenario. Am I wrong?
>
> Indeed my approach just decreases the probability and we sometimes end up
> writing twice to wakeup the card, but it would kinda bug me if we'd always
> do one write too much.
>
> Anyway, if you still prefer the read_poll_timeout() solution I'd be alright
> with that of course.

Yes, it will make code cleaner.

--
With Best Regards,
Andy Shevchenko


2021-10-03 09:20:44

by Jonas Dreßler

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] mwifiex: Try waking the firmware until we get an interrupt

On 9/14/21 13:48, Jonas Dreßler wrote:
> It seems that the firmware of the 88W8897 card sometimes ignores or
> misses when we try to wake it up by writing to the firmware status
> register. This leads to the firmware wakeup timeout expiring and the
> driver resetting the card because we assume the firmware has hung up or
> crashed (unfortunately that's not unlikely with this card).
>
> Turns out that most of the time the firmware actually didn't hang up,
> but simply "missed" our wakeup request and didn't send us an AWAKE
> event.
>
> Trying again to read the firmware status register after a short timeout
> usually makes the firmware wake up as expected, so add a small retry
> loop to mwifiex_pm_wakeup_card() that looks at the interrupt status to
> check whether the card woke up.
>
> The number of tries and timeout lengths for this were determined
> experimentally: The firmware usually takes about 500 us to wake up
> after we attempt to read the status register. In some cases where the
> firmware is very busy (for example while doing a bluetooth scan) it
> might even miss our requests for multiple milliseconds, which is why
> after 15 tries the waiting time gets increased to 10 ms. The maximum
> number of tries it took to wake the firmware when testing this was
> around 20, so a maximum number of 50 tries should give us plenty of
> safety margin.
>
> A good reproducer for this issue is letting the firmware sleep and wake
> up in very short intervals, for example by pinging a device on the
> network every 0.1 seconds.
>
> Cc: [email protected]
> Signed-off-by: Jonas Dreßler <[email protected]>
> ---
> drivers/net/wireless/marvell/mwifiex/pcie.c | 33 +++++++++++++++++----
> 1 file changed, 27 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/net/wireless/marvell/mwifiex/pcie.c b/drivers/net/wireless/marvell/mwifiex/pcie.c
> index 0eff717ac5fa..7fea319e013c 100644
> --- a/drivers/net/wireless/marvell/mwifiex/pcie.c
> +++ b/drivers/net/wireless/marvell/mwifiex/pcie.c
> @@ -661,11 +661,15 @@ static void mwifiex_delay_for_sleep_cookie(struct mwifiex_adapter *adapter,
> "max count reached while accessing sleep cookie\n");
> }
>
> +#define N_WAKEUP_TRIES_SHORT_INTERVAL 15
> +#define N_WAKEUP_TRIES_LONG_INTERVAL 35
> +
> /* This function wakes up the card by reading fw_status register. */
> static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
> {
> struct pcie_service_card *card = adapter->card;
> const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
> + int n_tries = 0;
>
> mwifiex_dbg(adapter, EVENT,
> "event: Wakeup device...\n");
> @@ -673,12 +677,29 @@ static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
> if (reg->sleep_cookie)
> mwifiex_pcie_dev_wakeup_delay(adapter);
>
> - /* Accessing fw_status register will wakeup device */
> - if (mwifiex_write_reg(adapter, reg->fw_status, FIRMWARE_READY_PCIE)) {
> - mwifiex_dbg(adapter, ERROR,
> - "Writing fw_status register failed\n");
> - return -1;
> - }
> + /* Access the fw_status register to wake up the device.
> + * Since the 88W8897 firmware sometimes appears to ignore or miss
> + * that wakeup request, we continue trying until we receive an
> + * interrupt from the card.
> + */
> + do {
> + if (mwifiex_write_reg(adapter, reg->fw_status, FIRMWARE_READY_PCIE)) {
> + mwifiex_dbg(adapter, ERROR,
> + "Writing fw_status register failed\n");
> + return -EIO;
> + }
> +
> + n_tries++;
> +
> + if (n_tries <= N_WAKEUP_TRIES_SHORT_INTERVAL)
> + usleep_range(400, 700);
> + else
> + msleep(10);
> + } while (n_tries <= N_WAKEUP_TRIES_SHORT_INTERVAL + N_WAKEUP_TRIES_LONG_INTERVAL &&
> + READ_ONCE(adapter->int_status) == 0);
> +
> + mwifiex_dbg(adapter, EVENT,
> + "event: Tried %d times until firmware woke up\n", n_tries);
>
> if (reg->sleep_cookie) {
> mwifiex_pcie_dev_wakeup_delay(adapter);
>

So I think I have another solution that might be a lot more elegant, how
about this:

try_again:
n_tries++;

mwifiex_write_reg(adapter, reg->fw_status, FIRMWARE_READY_PCIE);

if (wait_event_interruptible_timeout(adapter->card_wakeup_wait_q,
READ_ONCE(adapter->int_status) != 0,
WAKEUP_TRY_AGAIN_TIMEOUT) == 0 &&
n_tries < MAX_N_WAKEUP_TRIES) {
goto try_again;
}


and then call wake_up_interruptible() in the mwifiex_interrupt_status()
interrupt handler.

This solution should make sure we always keep wakeup latency to a minimum
and can still retry the register write if things didn't work.

2021-10-04 23:17:11

by Brian Norris

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] mwifiex: Try waking the firmware until we get an interrupt

Hi,

On Sun, Oct 3, 2021 at 2:18 AM Jonas Dreßler <[email protected]> wrote:
> So I think I have another solution that might be a lot more elegant, how
> about this:
>
> try_again:
> n_tries++;
>
> mwifiex_write_reg(adapter, reg->fw_status, FIRMWARE_READY_PCIE);
>
> if (wait_event_interruptible_timeout(adapter->card_wakeup_wait_q,
> READ_ONCE(adapter->int_status) != 0,
> WAKEUP_TRY_AGAIN_TIMEOUT) == 0 &&
> n_tries < MAX_N_WAKEUP_TRIES) {
> goto try_again;
> }

Isn't wait_event_interruptible_timeout()'s timeout in jiffies, which
is not necessarily that predictable, and also a lot more
coarse-grained than we want? (As in, if HZ=100, we're looking at
precision on the order of 10ms, whereas the expected wakeup latency is
~6ms.) That would be OK for well-behaved PCI cases, where we never
miss a write, but it could ~double your latency for your bad systems
that will need more than one run of the loop.

Also, feels like a do/while could be cleaner, but that's a lesser detail.

> and then call wake_up_interruptible() in the mwifiex_interrupt_status()
> interrupt handler.
>
> This solution should make sure we always keep wakeup latency to a minimum
> and can still retry the register write if things didn't work.

Brian