Skip to contentSkip to conversation

Bluetooth: hci_bcm4377: resume after ACPI S3 and ASPM

PATCH In review Andre Eikmeyer 3 participants 8 messages Nov 27 – Nov 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover Bluetooth: hci_bcm4377: resume after ACPI S3 and ASPM

After ACPI S3 the Bluetooth function of the BCM4377 comes back without its firmware. Patch 2 probes it again after such a resume.

When the previous controller is still being released, the re-probe registers the new one under another index, which runs into the probe return check fixed in patch 1. Patch 1 is a fix on its own and goes first.

Because of a hardware erratum the driver keeps ASPM disabled on the whole link to the Wi-Fi and Bluetooth chip for as long as it is bound, which holds the CPU package at PC3. Patch 3 disables ASPM only while the firmware boots and restores it once the firmware runs, also after the re-probe from patch 2.

The Wi-Fi function of the same chip needs the same after S3 and goes to linux-wireless in "wifi: brcmfmac: pcie: S3 suspend and resume on the T2 Mac chips".

Tested on MacBookAir9,1 and MacBookPro15,4.

Changes in v3:
- Rebase on bluetooth-next
- Patch 1: shorter subject, add Sven's Reviewed-by from v1
- Patch 3: add Sven's Acked-by

Changes in v2:
- Add patch 3, keep ASPM off only while the firmware boots
- Patches 1 and 2 are unchanged

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Andre Eikmeyer (3):
Bluetooth: hci_bcm4377: check hci_register_dev() for a negative return
Bluetooth: hci_bcm4377: probe again after a resume through firmware
Bluetooth: hci_bcm4377: keep ASPM off only while the firmware boots

 drivers/bluetooth/hci_bcm4377.c | 93 +++++++++++++++++++++++++++++----
 1 file changed, 83 insertions(+), 10 deletions(-)

base-commit: f57f78086ab54f3b3122fbaeba2c842ba7bb6d00

#1 lore raw

PATCH v3 1/3 Bluetooth: hci_bcm4377: check hci_register_dev() for a negative return

If another controller already holds hci0, a USB Bluetooth dongle for example, the BCM4377 is registered as hci1 and probe returns 1. The PCI core takes that as success, but the devm action that unregisters the controller is never added, so it stays registered after unbind while its driver data is freed.

hci_register_dev() returns the index of the new controller on success. Check for a negative return, as the other drivers do.

Fixes 8a06127602de ("Bluetooth: hci_bcm4377: Add new driver for BCM4377 PCIe boards")
---
 drivers/bluetooth/hci_bcm4377.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/bluetooth/hci_bcm4377.c +1 -1
index 72a9f269e8b..8c9833d625e 100644
@@ -2410,7 +2410,7 @@ static int bcm4377_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 
 	ret = hci_register_dev(hdev);
-	if (ret)
+	if (ret < 0)
 		return ret;
 	return devm_add_action_or_reset(&pdev->dev, bcm4377_hci_unregister_dev,
 					hdev);
#2 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1224492/

---Test result---

Test Summary:

CheckPatch                    PASS      2.25 seconds
VerifyFixes                   PASS      0.12 seconds
VerifySignedoff               PASS      0.12 seconds
GitLint                       PASS      1.00 seconds
SubjectPrefix                 PASS      0.35 seconds
BuildKernel                   PASS      31.99 seconds
CheckAllWarning               PASS      35.87 seconds
CheckSparse                   PASS      38.70 seconds
BuildKernel32                 PASS      32.11 seconds
CheckKernelLLVM               PASS      35.27 seconds
TestRunnerSetup               PASS      774.93 seconds
IncrementalBuild              PASS      33.21 seconds

https://github.com/bluez/bluetooth-next/pull/1245

---
Regards,
Linux Bluetooth

Tested-by on v3 1/3
#3 lore raw

Re: [PATCH v3 1/3] Bluetooth: hci_bcm4377: check hci_register_dev() for a negative return

In reply to Andre Eikmeyer

This is my first time replying on the kernel lists, so please do tell me if I'm doing anything wrong.

Tested on a MacBookAir9,1 (BCM4377) with an out-of-tree re-probe on resume (linux-t2-patches PR 87 [1]). Without this patch, both resumes I tried with A2DP audio streaming at suspend hit this bug: the re-probe registered the controller as hci1, the PCI core logged "Driver probe function unexpectedly returned 1", and BR/EDR stayed dead (page timeouts) until reboot. Reloading the driver then oopsed in bcm4377_enqueue() from hci1's hci_cmd_work, which looks like the stale registration your commit message describes.

With this patch on top of PR 87, 11 of 11 such resumes worked, with two different speakers; with the whole v3 series, 3 of 3 did.

[1] https://github.com/t2linux/linux-t2-patches/pull/87

I used Claude Opus 5.5 to help run these tests and go through the logs.

#4 lore raw

PATCH v3 2/3 Bluetooth: hci_bcm4377: probe again after a resume through firmware

In reply to Andre Eikmeyer

The BCM4377 is powered off during ACPI S3 and loses its firmware. On resume the driver only unquiesces it, so every command times out.

When the platform firmware handled the resume, probe the controller again from a work item and skip the ring teardown messages to the lost firmware. Suspend to idle keeps the quiesce path.

---
 drivers/bluetooth/hci_bcm4377.c | 59 +++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
drivers/bluetooth/hci_bcm4377.c +59 -0
index 8c9833d625e..f1beb09c9f5 100644
@@ -16,6 +16,9 @@
 #include <linux/of.h>
 #include <linux/pci.h>
 #include <linux/printk.h>
+#include <linux/slab.h>
+#include <linux/suspend.h>
+#include <linux/workqueue.h>
 
 #include <linux/unaligned.h>
 
@@ -566,6 +569,9 @@ struct bcm4377_data {
 	struct pci_dev *pdev;
 	struct hci_dev *hdev;
 
+	/* set when the controller lost its firmware during system sleep */
+	bool fw_lost;
+
 	void __iomem *bar0;
 	void __iomem *bar2;
 
@@ -1017,6 +1023,11 @@ static int bcm4377_destroy_completion_ring(struct bcm4377_data *bcm4377,
 	struct bcm4377_destroy_completion_ring_msg msg;
 	int ret;
 
+	if (bcm4377->fw_lost) {
+		ring->enabled = false;
+		return 0;
+	}
+
 	memset(&msg, 0, sizeof(msg));
 	msg.msg_type = BCM4377_CONTROL_MSG_DESTROY_COMPLETION_RING;
 	msg.ring_id = cpu_to_le16(ring->ring_id);
@@ -1107,6 +1118,11 @@ static int bcm4377_destroy_transfer_ring(struct bcm4377_data *bcm4377,
 	struct bcm4377_destroy_transfer_ring_msg msg;
 	int ret;
 
+	if (bcm4377->fw_lost) {
+		ring->enabled = false;
+		return 0;
+	}
+
 	memset(&msg, 0, sizeof(msg));
 	msg.msg_type = BCM4377_CONTROL_MSG_DESTROY_XFER_RING;
 	msg.ring_id = cpu_to_le16(ring->ring_id);
@@ -2432,11 +2448,54 @@ static int bcm4377_suspend(struct device *dev)
 	return 0;
 }
 
+struct bcm4377_reprobe {
+	struct work_struct work;
+	struct pci_dev *pdev;
+};
+
+static void bcm4377_reprobe_work(struct work_struct *work)
+{
+	struct bcm4377_reprobe *reprobe =
+		container_of(work, struct bcm4377_reprobe, work);
+	int ret;
+
+	/*
+	 * Probing stays blocked until the system resume completes, the driver
+	 * core then retries the deferred probe.
+	 */
+	ret = device_reprobe(&reprobe->pdev->dev);
+	if (ret && ret != -EPROBE_DEFER)
+		dev_err(&reprobe->pdev->dev, "re-probe after resume failed (%d)\n",
+			ret);
+
+	pci_dev_put(reprobe->pdev);
+	kfree(reprobe);
+}
+
 static int bcm4377_resume(struct device *dev)
 {
 	struct pci_dev *pdev = to_pci_dev(dev);
 	struct bcm4377_data *bcm4377 = pci_get_drvdata(pdev);
 
+	/*
+	 * The controller is powered off when the platform firmware handles
+	 * the system sleep, as with ACPI S3 on x86 Macs, and comes back
+	 * without its firmware. Probe it again.
+	 */
+	if (pm_resume_via_firmware()) {
+		struct bcm4377_reprobe *reprobe;
+
+		reprobe = kzalloc_obj(*reprobe);
+		if (!reprobe)
+			return -ENOMEM;
+
+		bcm4377->fw_lost = true;
+		reprobe->pdev = pci_dev_get(pdev);
+		INIT_WORK(&reprobe->work, bcm4377_reprobe_work);
+		schedule_work(&reprobe->work);
+		return 0;
+	}
+
 	iowrite32(BCM4377_BAR0_SLEEP_CONTROL_UNQUIESCE,
 		  bcm4377->bar0 + BCM4377_BAR0_SLEEP_CONTROL);
 
Tested-by on v3 2/3
#5 lore raw

Re: [PATCH v3 2/3] Bluetooth: hci_bcm4377: probe again after a resume through firmware

Tested on a MacBookAir9,1 on 7.2.15, with your "wifi: brcmfmac: reload the firmware after a missing D3 ACK" (as carried in linux-t2-patches PR 80 [1]) for Wi-Fi, not your brcmfmac S3 series. Bluetooth came back after S3: 4 of 4 resumes on AC, 8 of 8 on battery (45 s to 15 min) and 3 of 3 with audio streaming, plus the 8 short ones in my 3/3 reply. Most of these runs had 3/3 applied too; 3 of the battery resumes had only 1/3 and 2/3. One other resume failed, but not because of this patch; see below.

One observation: the re-probe's function reset lands at nearly the same moment as brcmfmac's firmware reload on the Wi-Fi function. In 59 of 65 logged resumes the two were within 50 ms (mostly with PR 87's re-probe; with this patch, 8 of 8 at 13-27 ms). Once, with PR 87, the Wi-Fi firmware crashed 2 s after the overlapping reset ("Firmware has halted or crashed") and Wi-Fi stayed down until reboot. I now unbind Bluetooth before suspend and bind it again once Wi-Fi is back, from userspace, so the re-probe path isn't used; that has given 0 failures in over 140 resumes. I can't prove the overlap caused the firmware crash, but it may matter for the brcmfmac series too.

The failed resume, with all three of your patches, was the only one I started with a bare "rtcwake -m mem" instead of systemctl suspend. While suspending, before entering S3, the root port logged a DPC containment event (status 0x1f01) and both functions "AER: can't recover". On resume brcmfmac's reload read 0xffffffff and failed (-19), then the re-probe failed reading the OTP (-2). The machine hung on the next suspend. I've seen the same DPC/AER pair once before (August, 7.1.8, in-tree drivers), so I don't think the re-probe caused it.

[1] https://github.com/t2linux/linux-t2-patches/pull/80

I used Claude Opus 5.5 to help run these tests and go through the logs.

#6 lore raw

PATCH v3 3/3 Bluetooth: hci_bcm4377: keep ASPM off only while the firmware boots

In reply to Andre Eikmeyer

The BCM4377 needs ASPM disabled because of a hardware erratum, and the driver disables it on the whole link for as long as the device is bound. On the MacBookAir9,1 this keeps the link to the Wi-Fi and Bluetooth chip in L0, and the package does not go deeper than PC3.

Disable ASPM only in this function's Link Control register before the function level reset and the firmware boot, and restore the saved value once the firmware runs. A non-ARI Multi-Function Device enables only the ASPM states enabled in all of its functions, so the link stays in L0 until then.

Tested on a MacBookAir9,1: with ASPM L1 enabled by the PCI core the link returns to L1 after probe and after the re-probe that follows ACPI S3, Bluetooth and Wi-Fi keep working, and the package reaches PC8 instead of PC3 at idle.

---
 drivers/bluetooth/hci_bcm4377.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)
drivers/bluetooth/hci_bcm4377.c +23 -9
index f1beb09c9f5..fc1164a470c 100644
@@ -497,7 +497,8 @@ struct bcm4377_data;
  * clear_pciecfg_subsystem_ctrl_bit19: Set to true if bit 19 in the
  *                                     vendor-specific subsystem control
  *                                     register has to be cleared
- * disable_aspm: Set to true if ASPM must be disabled due to hardware errata
+ * disable_aspm: Set to true if ASPM must be disabled while the firmware is
+ *               loaded and booted due to hardware errata
  * broken_ext_scan: Set to true if the chip erroneously claims to support
  *                  extended scanning
  * broken_mws_transport_config: Set to true if the chip erroneously claims to
@@ -572,6 +573,9 @@ struct bcm4377_data {
 	/* set when the controller lost its firmware during system sleep */
 	bool fw_lost;
 
+	/* Link Control value saved while ASPM is disabled for the boot */
+	u16 link_ctl;
+
 	void __iomem *bar0;
 	void __iomem *bar2;
 
@@ -2260,20 +2264,27 @@ static int bcm4377_probe_of(struct bcm4377_data *bcm4377)
 	return 0;
 }
 
+/*
+ * ASPM is disabled in this function's Link Control register only. A
+ * non-ARI Multi-Function Device enables only the ASPM states enabled in
+ * all of its functions, so the link stays in L0 until it is restored.
+ */
 static void bcm4377_disable_aspm(struct bcm4377_data *bcm4377)
 {
-	pci_disable_link_state(bcm4377->pdev,
-			       PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
-
-	/*
-	 * pci_disable_link_state can fail if either CONFIG_PCIEASPM is disabled
-	 * or if the BIOS hasn't handed over control to us. We must *always*
-	 * disable ASPM for this device due to hardware errata though.
-	 */
+	pcie_capability_read_word(bcm4377->pdev, PCI_EXP_LNKCTL,
+				  &bcm4377->link_ctl);
 	pcie_capability_clear_word(bcm4377->pdev, PCI_EXP_LNKCTL,
 				   PCI_EXP_LNKCTL_ASPMC);
 }
 
+static void bcm4377_restore_aspm(struct bcm4377_data *bcm4377)
+{
+	pcie_capability_clear_and_set_word(bcm4377->pdev, PCI_EXP_LNKCTL,
+					   PCI_EXP_LNKCTL_ASPMC,
+					   bcm4377->link_ctl &
+					   PCI_EXP_LNKCTL_ASPMC);
+}
+
 static void bcm4377_pci_free_irq_vectors(void *data)
 {
 	pci_free_irq_vectors(data);
@@ -2425,6 +2436,9 @@ static int bcm4377_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	if (ret)
 		return ret;
 
+	if (bcm4377->hw->disable_aspm)
+		bcm4377_restore_aspm(bcm4377);
+
 	ret = hci_register_dev(hdev);
 	if (ret < 0)
 		return ret;
Tested-by on v3 3/3
#7 lore raw

Re: [PATCH v3 3/3] Bluetooth: hci_bcm4377: keep ASPM off only while the firmware boots

Quoted 2 lines · Andre Eikmeyer

static void bcm4377_disable_aspm(struct bcm4377_data *bcm4377) {

[...]

Quoted 5 lines
drivers/bluetooth/hci_bcm4377.c +2 -0
+	pcie_capability_read_word(bcm4377->pdev, PCI_EXP_LNKCTL,
+				  &bcm4377->link_ctl);
 	pcie_capability_clear_word(bcm4377->pdev, PCI_EXP_LNKCTL,
 				   PCI_EXP_LNKCTL_ASPMC);
 }

[...]

drivers/bluetooth/hci_bcm4377.c static int bcm4377_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 2441

Quoted 7 lines
drivers/bluetooth/hci_bcm4377.c +3 -0
@@ -2425,6 +2436,9 @@ static int bcm4377_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	if (ret)
 		return ret;
 
+	if (bcm4377->hw->disable_aspm)
+		bcm4377_restore_aspm(bcm4377);
+

I think ASPM can end up off for good when a probe fails between these two. Any error return between bcm4377_disable_aspm() and bcm4377_restore_aspm() (enabling the device, BAR mapping, OTP read, IRQ setup, firmware boot, RTI setup) leaves ASPM cleared in LNKCTL. The next probe, after a rebind or a reload, saves that cleared value and later "restores" it, so ASPM stays off until reboot. Restoring the saved value on the error paths, for example with a devm action registered right after bcm4377_disable_aspm(), would avoid that. I haven't written or tested such a change.

Failed probes do happen on this MacBookAir9,1. With all three patches, Bluetooth hung during setup on most boots, and on the rebinds that followed, probes often failed with -110 (firmware boot timeout) before one succeeded. On those boots lspci showed ASPM disabled on the Bluetooth function afterwards; after a clean probe it shows L1 enabled. The timeouts themselves came from outside this series:
brcmfmac's probe reset the whole chip while hci_bcm4377 was still starting, because my distribution loads hci_bcm4377 first. Loading it after Wi-Fi is up fixed that (50 of 50 clean boots, 10 of them with all three patches).

ASPM on the Bluetooth function is what moves the package C-state here. Idle, with its ASPM bits cleared by hand (setpci) and then restored to what 3/3 leaves, each pair measured back to back in one boot:

  AC, screen on:       PC3 28%, 2.49 W  ->  PC6 22%, 1.97 W  (package)
  AC, screen off:      PC3 55%, 1.51 W  ->  PC6 54%, 0.90 W  (package)
  battery, screen off: PC3 54%, 1.45 W  ->  PC6 53%, 0.93 W  (package)
                       3.35 W  ->  2.67 W  (whole system, discharge)

Your commit message says this model reaches PC8; here it stops at PC6 (PC7 to PC10 stayed at 0 where I logged them). Did your PC8 run include other changes?

With ASPM actually on (after a clean probe; in my earlier runs failed probes had left it off), resumes with all three patches were fine apart from the rtcwake one in my 2/3 reply: 7 of 7 with Bluetooth unbound around suspend from userspace (ASPM on again after each), and 8 of 8 that went through the re-probe from 2/3, alternating ASPM on and off before suspend. That's too few to rule ASPM out, but it doesn't seem to trigger that failure by itself.

I used Claude Opus 5.5 to help run these tests. It spotted this issue while going through the code.