diff --git a/Documentation/devicetree/bindings/net/wireless/apple,centauri.yaml b/Documentation/devicetree/bindings/net/wireless/apple,centauri.yaml index 8d226eec13091..7f064886f055f 100644 --- a/Documentation/devicetree/bindings/net/wireless/apple,centauri.yaml +++ b/Documentation/devicetree/bindings/net/wireless/apple,centauri.yaml @@ -24,6 +24,11 @@ description: | native driver selects personalized firmware and transport data from its root-board match table rather than the T6050 wire-protocol properties. + On T8142 the root port above function zero may describe the N1's warm + reset with apple,function-warm-reset and apple,warm-reset-writes, as in + pci/apple,pcie.yaml. With both, the native driver restarts a firmware + that stopped; without them it never resets the chip. + properties: compatible: oneOf: diff --git a/arch/arm64/boot/dts/apple/t8142-j813.dts b/arch/arm64/boot/dts/apple/t8142-j813.dts index 5260a5eed2ab7..b4cc280fc9ef7 100644 --- a/arch/arm64/boot/dts/apple/t8142-j813.dts +++ b/arch/arm64/boot/dts/apple/t8142-j813.dts @@ -170,6 +170,13 @@ #address-cells = <3>; #size-cells = <2>; ranges; + /* + * The N1's warm reset: the ADT's pci-bridge0 function-warm_reset (the SMC, key + * 'bpli', the same key and argument as on the T6050 boards) and the T6050 writes. + * The N1 driver restarts a stopped firmware with it. + */ + apple,function-warm-reset = <&smc 0x62706c69 0x1000000>; + apple,warm-reset-writes = <0x00010001 17000>, <0x00010001 20000>; n1_control: control@0,0 { compatible = "apple,t8142-centauri-control", "pci106b,1900"; diff --git a/arch/arm64/boot/dts/apple/t8142-j815.dts b/arch/arm64/boot/dts/apple/t8142-j815.dts index 82e73d6dafc2b..ac142ba30a641 100644 --- a/arch/arm64/boot/dts/apple/t8142-j815.dts +++ b/arch/arm64/boot/dts/apple/t8142-j815.dts @@ -268,6 +268,12 @@ #address-cells = <3>; #size-cells = <2>; ranges; + /* + * The N1's warm reset, as on J813: the J815 ADT's pci-bridge0 + * function-warm_reset names the same SMC key 'bpli' and argument. + */ + apple,function-warm-reset = <&smc 0x62706c69 0x1000000>; + apple,warm-reset-writes = <0x00010001 17000>, <0x00010001 20000>; n1_control: control@0,0 { compatible = "apple,t8142-centauri-control", "pci106b,1900"; diff --git a/drivers/bluetooth/btcentauri.c b/drivers/bluetooth/btcentauri.c index 7ae66124aa7ce..f51f787753c82 100644 --- a/drivers/bluetooth/btcentauri.c +++ b/drivers/bluetooth/btcentauri.c @@ -1040,10 +1040,15 @@ static int cen_probe(struct pci_dev *pdev, const struct pci_device_id *id) if (ret) return ret; bt->bar = pcim_iomap_table(pdev)[0]; - if (readl(bt->bar + 0x18) != 2 || readl(bt->bar) != 0) - return dev_err_probe(&pdev->dev, -EBUSY, - "Beta requires running firmware and unowned IPC (exec=%u ipc=%u)\n", - readl(bt->bar + 0x18), readl(bt->bar)); + /* + * The function appears with the N1's IPC start, before Beta's firmware runs. + * apple_centauri attaches it again once that firmware is up. + */ + if (readl(bt->bar + 0x18) != 2 || readl(bt->bar) != 0) { + dev_info(&pdev->dev, "waiting for Beta's firmware (exec=%u ipc=%u)\n", + readl(bt->bar + 0x18), readl(bt->bar)); + return -ENXIO; + } ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40)); if (ret) return ret; diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 35f86f9fe0027..cf1bbdab1889e 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -913,6 +913,70 @@ int apple_smc_write_noirq(struct apple_smc *smc, smc_key key, const void *buf, s } EXPORT_SYMBOL_GPL(apple_smc_write_noirq); +int apple_smc_warm_reset_parse(struct device_node *np, struct apple_smc_warm_reset *wr) +{ + struct of_phandle_args fn; + int count, i; + + memset(wr, 0, sizeof(*wr)); + if (!np || of_parse_phandle_with_fixed_args(np, "apple,function-warm-reset", 2, 0, &fn)) + return -ENODEV; + wr->smc = of_find_device_by_node(fn.np); + of_node_put(fn.np); + if (!wr->smc) + return -ENODEV; + count = of_property_count_u32_elems(np, "apple,warm-reset-writes"); + if (count < 2 || count % 2 || count > ARRAY_SIZE(wr->writes) || + of_property_read_u32_array(np, "apple,warm-reset-writes", wr->writes, count)) + goto bad; + for (i = 1; i < count; i += 2) + if (wr->writes[i] > 100 * USEC_PER_MSEC) + goto bad; + wr->key = fn.args[0]; + wr->count = count / 2; + return 0; +bad: + apple_smc_warm_reset_put(wr); + return -EINVAL; +} +EXPORT_SYMBOL_GPL(apple_smc_warm_reset_parse); + +void apple_smc_warm_reset_put(struct apple_smc_warm_reset *wr) +{ + if (wr->smc) + put_device(&wr->smc->dev); + memset(wr, 0, sizeof(*wr)); +} +EXPORT_SYMBOL_GPL(apple_smc_warm_reset_put); + +int apple_smc_warm_reset(const struct apple_smc_warm_reset *wr, bool noirq) +{ + struct apple_smc *smc = wr->smc ? platform_get_drvdata(wr->smc) : NULL; + unsigned int i; + int ret = 0; + + if (!smc || !wr->count) + return -ENODEV; + for (i = 0; i < wr->count && !ret; i++) { + __be32 value = cpu_to_be32(wr->writes[2 * i]); + u32 wait = wr->writes[2 * i + 1]; + + if (noirq) { + ret = apple_smc_write_noirq(smc, wr->key, &value, sizeof(value)); + if (!ret) { + mdelay(wait / USEC_PER_MSEC); + udelay(wait % USEC_PER_MSEC); + } + } else { + ret = apple_smc_write(smc, wr->key, &value, sizeof(value)); + if (!ret) + fsleep(wait); + } + } + return ret; +} +EXPORT_SYMBOL_GPL(apple_smc_warm_reset); + static void apple_smc_write_mbse(struct apple_smc *smc, smc_key value) { int ret; diff --git a/drivers/net/wireless/apple/Kconfig b/drivers/net/wireless/apple/Kconfig index d9b13a9785de2..e7d24b6e7a52e 100644 --- a/drivers/net/wireless/apple/Kconfig +++ b/drivers/net/wireless/apple/Kconfig @@ -10,6 +10,7 @@ config APPLE_CENTAURI tristate "Apple N1 Centauri experimental J813 Wi-Fi" depends on ARCH_APPLE || COMPILE_TEST depends on PCI && CFG80211 && DEBUG_FS + depends on MFD_MACSMC select FW_LOADER help Experimental Apple N1 PCIe and ACIPC Wi-Fi support for the J813 diff --git a/drivers/net/wireless/apple/centauri/centauri.h b/drivers/net/wireless/apple/centauri/centauri.h index fa937c1c4e2e3..17abbfb8352f2 100644 --- a/drivers/net/wireless/apple/centauri/centauri.h +++ b/drivers/net/wireless/apple/centauri/centauri.h @@ -6,6 +6,7 @@ #include #include #include +#include #define CEN_ALPHA_SCRATCH_OFFSET SZ_8M #define CEN_ALPHA_SCRATCH_SIZE (5 * SZ_1M) @@ -23,7 +24,7 @@ struct centauri { void __iomem *bar; resource_size_t size; u32 stage_offset; - struct dentry *debug; + struct dentry *debug, *crash_file; struct mutex boot_lock; void *firmware, *descriptor, *msi_page; dma_addr_t firmware_dma, descriptor_dma, msi_dma; @@ -35,6 +36,14 @@ struct centauri { bool link_ready; struct centauri_ipc *ipc; struct centauri_wifi *wifi; + /* Restart of a stopped firmware: requested by the Wi-Fi code, confirmed and run here */ + struct delayed_work recover_work; + unsigned long recover_at[3]; + bool removing; + bool recover_gave_up; /* restart limit reached: requests are ignored */ + bool restart_failed; /* the last restart failed with the link up: try again */ + bool warm_reset; /* the root port describes the N1's warm reset: restart on a stop */ + bool inject_stall; /* debugfs test: the next check finds the firmware stopped */ }; /* One row per machine with an N1: the FTAB file name, the word written to BAR+0x807c, @@ -92,8 +101,10 @@ static inline const struct centauri_board *centauri_board(void) return NULL; } -int centauri_port_cycle(struct pci_dev *pdev); +int centauri_port_cycle(struct pci_dev *pdev, int *reset_ret); +bool centauri_warm_reset_described(struct pci_dev *pdev); int centauri_start_ipc(struct centauri *c); +bool centauri_beta_running(struct centauri *c); void centauri_free_ipc(struct centauri *c); int centauri_alpha_resources(struct centauri *c, struct cen_alpha_resources *resources); int centauri_alpha_log_buffer(struct centauri *c, void **cpu, dma_addr_t *dma, size_t *size); @@ -102,4 +113,6 @@ void centauri_wifi_unregister(struct centauri *c); int centauri_wifi_suspend(struct centauri *c); int centauri_wifi_resume(struct centauri *c); void centauri_wifi_kick(struct centauri *c); +bool centauri_wifi_stalled(struct centauri *c); +void centauri_recovery_request(struct centauri *c, const char *why); #endif diff --git a/drivers/net/wireless/apple/centauri/centauri_ipc.c b/drivers/net/wireless/apple/centauri/centauri_ipc.c index 8d117989cd760..d657e6a4b5374 100644 --- a/drivers/net/wireless/apple/centauri/centauri_ipc.c +++ b/drivers/net/wireless/apple/centauri/centauri_ipc.c @@ -169,7 +169,13 @@ static int secondary_init(struct centauri *c) pci_write_config_dword(c->pdev, 0xf8c, upper_32_bits(c->ipc->secondary->dma)); pci_write_config_dword(c->pdev, 0xf90, required); dev_info(&c->pdev->dev, "secondary memory: seed=%u required=%u\n", length, required); - debugfs_create_file("firmware_crash", 0400, c->debug, c, &firmware_crash_fops); + /* + * Created once and kept across restarts: its reader looks up c->ipc under boot_lock, + * and removing it here, under boot_lock, would wait for a reader blocked on that lock. + */ + if (!c->crash_file) + c->crash_file = debugfs_create_file("firmware_crash", 0400, c->debug, c, + &firmware_crash_fops); return 0; } @@ -546,6 +552,17 @@ int centauri_start_ipc(struct centauri *c) return ret; } +/* + * Whether Beta's (Bluetooth's) firmware runs with its IPC still unowned: the state + * btcentauri needs to take function 2. Called with the boot lock held. + */ +bool centauri_beta_running(struct centauri *c) +{ + void __iomem *bar = c->ipc ? c->ipc->bar[2] : NULL; + + return bar && readl(bar + 0x18) == 2 && !readl(bar); +} + void centauri_free_ipc(struct centauri *c) { struct centauri_ipc *ipc = c->ipc; diff --git a/drivers/net/wireless/apple/centauri/centauri_pci.c b/drivers/net/wireless/apple/centauri/centauri_pci.c index 598671387565a..67d3a7884759f 100644 --- a/drivers/net/wireless/apple/centauri/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/centauri_pci.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "centauri.h" @@ -28,6 +29,8 @@ static int centauri_status_show(struct seq_file *s, void *unused) { struct centauri *c = s->private; + /* A restart cycles the link: no BAR read while it runs */ + mutex_lock(&c->boot_lock); seq_printf(s, "pci=%s vendor=%04x device=%04x\n", pci_name(c->pdev), c->pdev->vendor, c->pdev->device); if (c->link_ready) @@ -38,6 +41,7 @@ static int centauri_status_show(struct seq_file *s, void *unused) seq_printf(s, "transport=hardware-pci firmware_boot=%s image_response=%#x wifi=%s\n", c->boot_attempted ? "attempted" : "not-started", c->image_response, READ_ONCE(c->wifi) ? "registered" : "not-owned-by-this-function"); + mutex_unlock(&c->boot_lock); return 0; } DEFINE_SHOW_ATTRIBUTE(centauri_status); @@ -156,6 +160,188 @@ static int centauri_rom_boot(struct centauri *c) return ret; } +/* + * Restart the N1 from a running or stopped firmware, without the bootloader: release + * everything the firmware could write to, then a port cycle with the SMC warm reset + * (PERST# alone leaves the firmware running), which leaves the chip in its ROM (stage 0), + * then the boot steps 1-3 as at probe. btcentauri is released first: it drives function 2 + * over the IPC that is freed here. + * + * Not safe against function 0 being removed under the PCI rescan lock (sysfs remove, host + * unbind, hot unplug) while a restart runs: remove waits for the restart, which waits for the + * rescan lock. The debugfs boot steps have the same limit. + */ +static int centauri_recover(struct centauri *c) +{ + struct device *dev = &c->pdev->dev; + u32 stage = readl(c->bar + 0x8000); + struct pci_dev *beta; + int ret, reset_ret, i; + + if (!c->warm_reset) + return -EOPNOTSUPP; + if (!c->link_ready || PCI_FUNC(c->pdev->devfn)) + return -EBUSY; + dev_info(dev, "recovery: stage %#x, releasing the firmware's buffers\n", stage); + beta = pci_get_slot(c->pdev->bus, PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 2)); + if (beta) + device_release_driver(&beta->dev); + pci_dev_put(beta); + centauri_free_boot(c); + /* + * The warm reset puts the chip back in its single-function ROM personality and wipes + * functions 1 and 2. Remove them now; the IPC start's rescan adds them again, as at + * probe. A stale function's BAR read after the reset hangs the fabric. + */ + pci_lock_rescan_remove(); + for (i = 2; i >= 1; i--) { + struct pci_dev *fn = pci_get_slot(c->pdev->bus, + PCI_DEVFN(PCI_SLOT(c->pdev->devfn), i)); + + if (fn) + pci_stop_and_remove_bus_device(fn); + pci_dev_put(fn); + } + pci_unlock_rescan_remove(); + c->boot_attempted = false; + c->image_response = 0; + c->link_ready = false; + ret = centauri_port_cycle(c->pdev, &reset_ret); + if (ret) { + dev_err(dev, "recovery: reset port cycle failed: %d\n", ret); + return ret; + } + c->link_ready = true; + if (reset_ret) { + dev_err(dev, "recovery: warm reset failed: %d\n", reset_ret); + return reset_ret; + } + stage = readl(c->bar + 0x8000); + dev_info(dev, "recovery: stage %#x after the warm reset\n", stage); + if (stage != 0) + return -EIO; + /* Returns once the ROM reports the image loaded (stage 4), as the boot step does. */ + ret = centauri_rom_boot(c); + if (ret) { + dev_err(dev, "recovery: ROM boot failed: %d\n", ret); + return ret; + } + c->link_ready = false; + ret = centauri_port_cycle(c->pdev, NULL); + if (ret) { + dev_err(dev, "recovery: boot port cycle failed: %d\n", ret); + return ret; + } + c->link_ready = true; + /* The IPC start needs the image's first stage (1) after the port cycle. */ + ret = readl_poll_timeout(c->bar + 0x8000, stage, stage == 1, 20 * USEC_PER_MSEC, + 5 * USEC_PER_SEC); + dev_info(dev, "recovery: post-reset execution stage=%#x\n", stage); + if (ret) + return ret; + ret = centauri_start_ipc(c); + dev_info(dev, "recovery: IPC start %d\n", ret); + return ret; +} + +/* Ordered and freezable: a restart never runs during a system sleep or beside another one. */ +static struct workqueue_struct *centauri_wq; + +/* + * The Wi-Fi code saw a sign of a stopped firmware: an Alpha execution stage other than 2, + * a command ring the firmware no longer drains, or a command timeout. The worker looks + * again 2 s later and restarts the chip only if the firmware is still stopped. + */ +void centauri_recovery_request(struct centauri *c, const char *why) +{ + if (!c->warm_reset || READ_ONCE(c->removing) || READ_ONCE(c->recover_gave_up)) + return; + if (queue_delayed_work(centauri_wq, &c->recover_work, 2 * HZ)) + dev_warn(&c->pdev->dev, "firmware fault (%s); checking again in 2 s\n", why); +} + +static bool centauri_beta_up(struct centauri *c) +{ + bool up; + + mutex_lock(&c->boot_lock); + up = READ_ONCE(c->removing) || centauri_beta_running(c); + mutex_unlock(&c->boot_lock); + return up; +} + +/* + * The IPC start's rescan adds the Beta (Bluetooth) function before Beta's firmware runs, + * so btcentauri cannot take it then. Wait for that firmware and attach the function. + * Called without the boot lock and outside the PCI rescan lock. Returns 1 when + * function 2 has a driver, 0 when none took it, or a negative error. + */ +static int centauri_attach_beta(struct centauri *c) +{ + struct pci_dev *beta; + bool up; + int ret; + + beta = pci_get_slot(c->pdev->bus, PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 2)); + if (!beta) + return -ENODEV; + ret = 1; + if (!READ_ONCE(beta->dev.driver)) { + ret = read_poll_timeout(centauri_beta_up, up, up, 20 * USEC_PER_MSEC, + 10 * USEC_PER_SEC, false, c); + if (!ret) + ret = READ_ONCE(c->removing) ? -ENODEV : device_attach(&beta->dev); + } + pci_dev_put(beta); + return ret; +} + +static void centauri_recover_worker(struct work_struct *work) +{ + struct centauri *c = container_of(to_delayed_work(work), struct centauri, recover_work); + struct device *dev = &c->pdev->dev; + unsigned long now = jiffies; + int ret; + + mutex_lock(&c->boot_lock); + if (READ_ONCE(c->removing) || (!c->ipc && !c->restart_failed)) { + mutex_unlock(&c->boot_lock); + return; + } + if (!xchg(&c->inject_stall, false) && !c->restart_failed && !centauri_wifi_stalled(c)) { + mutex_unlock(&c->boot_lock); + dev_info(dev, "firmware fault cleared; no restart\n"); + return; + } + /* Three restarts in 30 minutes: the chip is left down rather than reset in a loop. */ + if (c->recover_at[0] && time_before(now, c->recover_at[0] + 30 * 60 * HZ)) { + WRITE_ONCE(c->recover_gave_up, true); + mutex_unlock(&c->boot_lock); + dev_err(dev, "firmware stopped again after 3 restarts in 30 min; leaving Wi-Fi down until the driver is reloaded\n"); + return; + } + memmove(c->recover_at, c->recover_at + 1, sizeof(c->recover_at) - sizeof(c->recover_at[0])); + c->recover_at[ARRAY_SIZE(c->recover_at) - 1] = now; + dev_warn(dev, "firmware stopped; restarting the N1\n"); + ret = centauri_recover(c); + /* With the link up the chip can be reset again; without it nothing more can be done. */ + c->restart_failed = ret && c->link_ready; + mutex_unlock(&c->boot_lock); + if (ret && !c->restart_failed) { + dev_err(dev, "restart failed: %d, and the link is down; Wi-Fi stays down until a reboot\n", + ret); + return; + } + if (ret) { + /* Counts against the same limit: at most three attempts in 30 minutes. */ + dev_err(dev, "restart failed: %d; trying again in 10 s\n", ret); + if (!READ_ONCE(c->removing)) + queue_delayed_work(centauri_wq, &c->recover_work, 10 * HZ); + return; + } + dev_info(dev, "restarted; Bluetooth function attach: %d\n", centauri_attach_beta(c)); +} + static ssize_t centauri_boot_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -163,18 +349,25 @@ static ssize_t centauri_boot_write(struct file *file, const char __user *buffer, u32 value; int ret = kstrtou32_from_user(buffer, count, 0, &value); - if (ret || value < 1 || value > 3) + if (ret || value < 1 || value > 6) return ret ?: -EINVAL; mutex_lock(&c->boot_lock); if (value == 1) { ret = centauri_rom_boot(c); } else if (value == 3) { ret = centauri_start_ipc(c); + } else if (value == 4) { + ret = centauri_recover(c); + } else if (value == 5 || value == 6) { + /* Test the fault path: 5 as a stopped firmware, 6 as a fault that clears. */ + WRITE_ONCE(c->inject_stall, value == 5); + centauri_recovery_request(c, value == 5 ? "debugfs: stopped" : "debugfs: spurious"); + ret = 0; } else if (!c->firmware || !c->link_ready || readl(c->bar + 0x8000) != 4) { ret = -EBUSY; } else { c->link_ready = false; - ret = centauri_port_cycle(c->pdev); + ret = centauri_port_cycle(c->pdev, NULL); if (!ret) { c->link_ready = true; dev_info(&c->pdev->dev, "post-reset execution stage=%#x\n", @@ -182,6 +375,14 @@ static ssize_t centauri_boot_write(struct file *file, const char __user *buffer, } } mutex_unlock(&c->boot_lock); + if (value == 3 && !ret) { + int attached = centauri_attach_beta(c); + + if (attached == 1) + dev_info(&c->pdev->dev, "Bluetooth function attached\n"); + else + dev_warn(&c->pdev->dev, "Bluetooth function not attached: %d\n", attached); + } return ret ?: count; } @@ -210,6 +411,8 @@ static int centauri_probe(struct pci_dev *pdev, const struct pci_device_id *id) c->pdev = pdev; c->link_ready = true; mutex_init(&c->boot_lock); + INIT_DELAYED_WORK(&c->recover_work, centauri_recover_worker); + c->warm_reset = centauri_warm_reset_described(pdev); c->stage_offset = id->driver_data; ret = pcim_enable_device(pdev); if (ret) @@ -246,6 +449,9 @@ static void centauri_remove(struct pci_dev *pdev) { struct centauri *c = pci_get_drvdata(pdev); + /* Disabled, not only cancelled: a request racing this remove can no longer queue it */ + WRITE_ONCE(c->removing, true); + disable_delayed_work_sync(&c->recover_work); centauri_wifi_unregister(c); debugfs_remove_recursive(c->debug); centauri_free_boot(c); @@ -306,10 +512,15 @@ static int __init centauri_init(void) { int ret; + centauri_wq = alloc_ordered_workqueue("apple_centauri", WQ_FREEZABLE); + if (!centauri_wq) + return -ENOMEM; centauri_debug = debugfs_create_dir("apple_centauri", NULL); ret = pci_register_driver(¢auri_driver); - if (ret) + if (ret) { debugfs_remove_recursive(centauri_debug); + destroy_workqueue(centauri_wq); + } return ret; } @@ -317,6 +528,7 @@ static void __exit centauri_exit(void) { pci_unregister_driver(¢auri_driver); debugfs_remove_recursive(centauri_debug); + destroy_workqueue(centauri_wq); } module_init(centauri_init); module_exit(centauri_exit); diff --git a/drivers/net/wireless/apple/centauri/centauri_port.c b/drivers/net/wireless/apple/centauri/centauri_port.c index 59eadc3f8521c..55964946895e7 100644 --- a/drivers/net/wireless/apple/centauri/centauri_port.c +++ b/drivers/net/wireless/apple/centauri/centauri_port.c @@ -11,7 +11,10 @@ #include #include #include +#include +#include #include +#include #include "centauri.h" static void update(void __iomem *base, u32 offset, u32 clear, u32 set) @@ -26,7 +29,64 @@ static bool wait_bits(void __iomem *address, u32 mask, u32 wanted, u32 us) return !readl_poll_timeout(address, value, (value & mask) == wanted, 100, us); } -int centauri_port_cycle(struct pci_dev *pdev) +/* + * The N1's warm reset, as the root port describes it (bindings/pci/apple,pcie.yaml, as on the + * T6050 boards): "apple,function-warm-reset" names the SMC and the key (the ADT's pci-bridge0 + * function-warm_reset), "apple,warm-reset-writes" the writes. + */ +static int warm_reset_parse(struct pci_dev *pdev, struct apple_smc_warm_reset *wr) +{ + struct pci_dev *bridge = pdev->bus->self; + + if (!IS_REACHABLE(CONFIG_MFD_MACSMC) || !bridge) + return -ENODEV; + return apple_smc_warm_reset_parse(pci_device_to_OF_node(bridge), wr); +} + +/* + * The root port describes a warm reset, and centauri_port_cycle() can cycle this port: only + * then does the driver restart a stopped N1, so a restart never tears the functions down on a + * board whose port cycle would refuse. + */ +bool centauri_warm_reset_described(struct pci_dev *pdev) +{ + const struct centauri_board *board = centauri_board(); + struct pci_dev *bridge = pdev->bus->self; + struct apple_smc_warm_reset wr; + + if (!board || !board->port || !bridge || bridge->vendor != PCI_VENDOR_ID_APPLE || + (board->bridge_id && bridge->device != board->bridge_id)) + return false; + if (warm_reset_parse(pdev, &wr)) + return false; + apple_smc_warm_reset_put(&wr); + return true; +} + +/* + * Given while the port is down and not set up: the link then trains a chip that was just + * reset. PERST# alone leaves the firmware running. + */ +static int warm_reset(struct pci_dev *pdev) +{ + struct apple_smc_warm_reset wr; + int ret; + + ret = warm_reset_parse(pdev, &wr); + if (!ret) { + ret = apple_smc_warm_reset(&wr, false); + apple_smc_warm_reset_put(&wr); + } + dev_info(&pdev->dev, "warm reset through the SMC: %d\n", ret); + return ret; +} + +/* + * Take the N1's port down and train it again. With @reset_ret, the N1 is warm reset while + * the port is down and the reset's result is stored there; the link comes up either way. + * Returns the link's result. + */ +int centauri_port_cycle(struct pci_dev *pdev, int *reset_ret) { static const u32 reset[][2] = { {0x88, 0x110}, {0x100, ~0U}, {0x148, ~0U}, {0x210, ~0U}, @@ -141,6 +201,8 @@ int centauri_port_cycle(struct pci_dev *pdev) writel(0, port + 0x3800 + 4 * i); for (i = 0; i < ARRAY_SIZE(tune); i++) writel(tune[i][1], port + tune[i][0]); + if (reset_ret) + *reset_ret = warm_reset(pdev); update(port, 0x800, 0, 1); udelay(10); update(phy, 0, 0, 1); diff --git a/drivers/net/wireless/apple/centauri/centauri_wifi.c b/drivers/net/wireless/apple/centauri/centauri_wifi.c index 7f953bf1d27cc..fe8088a84be27 100644 --- a/drivers/net/wireless/apple/centauri/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/centauri_wifi.c @@ -745,8 +745,10 @@ static int command_control(struct centauri_wifi *wifi, u32 id, const void *paylo return -EBUSY; if (size + 24 > BUFFER_SIZE) return -EMSGSIZE; - if ((slot + 1) % ring_size[1] == index_read(wifi, 1, 1)) + if ((slot + 1) % ring_size[1] == index_read(wifi, 1, 1)) { + centauri_recovery_request(wifi->control, "command ring full"); return -ENOSPC; + } packet = wifi->a.window + TX_BUFFER + slot * BUFFER_SIZE; memset(packet, 0, 24 + size); put_unaligned_le16(24 + size, packet + 0x10); @@ -766,8 +768,10 @@ static int command_control(struct centauri_wifi *wifi, u32 id, const void *paylo writel(1, wifi->a.bar + 0x1004); do { drain_control(wifi, pm_command); - if (readl(wifi->a.bar + 8) != 2) + if (readl(wifi->a.bar + 8) != 2) { + centauri_recovery_request(wifi->control, "command: Alpha stage"); return -EIO; + } /* Bit 30 is more-data-pending, not part of the command identity. * Scan/connect replies set it before their later completion event. */ @@ -782,9 +786,26 @@ static int command_control(struct centauri_wifi *wifi, u32 id, const void *paylo usleep_range(500, 1000); } while (time_before(jiffies, deadline)); dev_err(&wifi->control->pdev->dev, "Wi-Fi command %#x timed out (rx=%u)\n", id, wifi->rx_seen[2]); + centauri_recovery_request(wifi->control, "command timeout"); return -ETIMEDOUT; } +/* + * The firmware has stopped: Alpha left execution stage 2, or the command ring stays full + * (a running firmware drains each command before the next is queued). Not while the + * transport is paused for a system sleep. + */ +bool centauri_wifi_stalled(struct centauri *c) +{ + struct centauri_wifi *wifi = c->wifi; + + if (!wifi || READ_ONCE(wifi->transport_paused) || READ_ONCE(wifi->stopping)) + return false; + if (readl(wifi->a.bar + 8) != 2) + return true; + return (READ_ONCE(wifi->command_head) + 1) % ring_size[1] == index_read(wifi, 1, 1); +} + static int command(struct centauri_wifi *wifi, u32 id, const void *payload, size_t size) { return command_control(wifi, id, payload, size, false); @@ -1005,6 +1026,9 @@ static void scan_worker(struct work_struct *work) progress = jiffies; do { if (scan_transport_stopped(wifi)) { + if (!READ_ONCE(wifi->stopping) && !READ_ONCE(wifi->transport_paused) && + readl(wifi->a.bar + 8) != 2) + centauri_recovery_request(wifi->control, "scan: Alpha stage"); ret = -EIO; break; } diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 824b94d04caa7..7d139eab8c452 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -565,7 +565,6 @@ struct apple_pcie_pd { struct apple_radio_sleep; -#define APPLE_PCIE_T6050_WARM_WRITES 4 struct apple_pcie { struct mutex lock; @@ -588,10 +587,7 @@ struct apple_pcie { /* Those ports back, and their functions looked for, once the resume is over */ struct work_struct t6050_unpark_work; /* The endpoint's warm reset, given by this host while the port is down */ - struct platform_device *t6050_smc; - u32 t6050_warm_key; - u32 t6050_warm_writes[2 * APPLE_PCIE_T6050_WARM_WRITES]; - unsigned int t6050_warm_count; + struct apple_smc_warm_reset t6050_warm; bool t6050_warm_done; /* When this wake's bring-up of that port had the link up and its 100 ms over; 0 before */ ktime_t t6050_link_up; @@ -5546,35 +5542,14 @@ static int apple_pcie_t6050_pm_notify(struct notifier_block *nb, unsigned long a static void apple_pcie_t6050_warm_reset_probe(struct platform_device *pdev, struct apple_pcie *pcie, struct apple_pcie_port *port) { - struct of_phandle_args fn; - int count, i; - if (!IS_REACHABLE(CONFIG_MFD_MACSMC) || - of_parse_phandle_with_fixed_args(port->np, "apple,function-warm-reset", 2, 0, &fn)) - return; - pcie->t6050_smc = of_find_device_by_node(fn.np); - of_node_put(fn.np); - if (!pcie->t6050_smc) + apple_smc_warm_reset_parse(port->np, &pcie->t6050_warm)) return; - count = of_property_count_u32_elems(port->np, "apple,warm-reset-writes"); - if (count < 2 || count % 2 || count > ARRAY_SIZE(pcie->t6050_warm_writes) || - of_property_read_u32_array(port->np, "apple,warm-reset-writes", - pcie->t6050_warm_writes, count)) - goto drop; - for (i = 1; i < count; i += 2) - if (pcie->t6050_warm_writes[i] > 100 * USEC_PER_MSEC) - goto drop; /* The SMC's noirq resume comes before this host's: the reset is written from there */ - if (!device_link_add(&pdev->dev, &pcie->t6050_smc->dev, DL_FLAG_STATELESS)) { + if (!device_link_add(&pdev->dev, &pcie->t6050_warm.smc->dev, DL_FLAG_STATELESS)) { dev_warn(&pdev->dev, "no warm reset after a sleep: no link to the SMC\n"); - goto drop; + apple_smc_warm_reset_put(&pcie->t6050_warm); } - pcie->t6050_warm_key = fn.args[0]; - pcie->t6050_warm_count = count / 2; - return; -drop: - put_device(&pcie->t6050_smc->dev); - pcie->t6050_smc = NULL; } /* @@ -5585,34 +5560,15 @@ static void apple_pcie_t6050_warm_reset_probe(struct platform_device *pdev, stru */ static int apple_pcie_t6050_warm_reset(struct apple_pcie *pcie, bool noirq) { - struct apple_smc *smc; - unsigned int i; - int ret = 0; + int ret; pcie->t6050_warm_done = false; - if (!IS_REACHABLE(CONFIG_MFD_MACSMC) || !pcie->t6050_warm_count) + if (!IS_REACHABLE(CONFIG_MFD_MACSMC) || !pcie->t6050_warm.count) return -ENODEV; - smc = platform_get_drvdata(pcie->t6050_smc); - if (!smc) { + ret = apple_smc_warm_reset(&pcie->t6050_warm, noirq); + if (ret == -ENODEV) { dev_warn(pcie->dev, "no warm reset: the SMC isn't bound\n"); - return -ENODEV; - } - for (i = 0; i < pcie->t6050_warm_count && !ret; i++) { - __be32 value = cpu_to_be32(pcie->t6050_warm_writes[2 * i]); - u32 wait = pcie->t6050_warm_writes[2 * i + 1]; - smc_key key = pcie->t6050_warm_key; - - if (noirq) { - ret = apple_smc_write_noirq(smc, key, &value, sizeof(value)); - if (!ret) { - mdelay(wait / USEC_PER_MSEC); - udelay(wait % USEC_PER_MSEC); - } - } else { - ret = apple_smc_write(smc, key, &value, sizeof(value)); - if (!ret) - fsleep(wait); - } + return ret; } pcie->t6050_warm_done = !ret; if (ret) @@ -5811,7 +5767,7 @@ void apple_pcie_t6050_sleep_reset(struct pci_dev *bridge) { struct apple_pcie *pcie = apple_pcie_t6050_from_bridge(bridge); - if (pcie && pcie->t6050_warm_count) { + if (pcie && pcie->t6050_warm.count) { pcie->t6050_keep_alive = false; pcie->t6050_reset_before_sleep = true; } @@ -5876,7 +5832,7 @@ int apple_pcie_t6050_port_recycle(struct pci_dev *bridge) if (!pcie) return -ENODEV; - if (!pcie->t6050_warm_count || pcie->t6050_lost || READ_ONCE(pcie->t6050_down)) + if (!pcie->t6050_warm.count || pcie->t6050_lost || READ_ONCE(pcie->t6050_down)) return -EBUSY; pci_save_state(bridge); ret = apple_pcie_t6050_sleep_down(pcie); @@ -7645,7 +7601,7 @@ static int apple_pcie_resume_noirq(struct device *dev) /* Up, and reset unless kept alive: nothing takes it down again */ pcie->t6050_woke = !READ_ONCE(pcie->t6050_down) && !pcie->t6050_lost && (!warm_reset || pcie->t6050_warm_done || - !pcie->t6050_warm_count); + !pcie->t6050_warm.count); list_for_each_entry(port, &pcie->ports, entry) if (pcie->t6050_parked & BIT(port->idx)) dev_dbg(dev, "port %d at the wake: parked since before the sleep, power off, PERST# low, no functions, root port %s, DART %s; %s\n", diff --git a/include/linux/mfd/macsmc.h b/include/linux/mfd/macsmc.h index 5afcf707a7c31..47d4bc7c9b35d 100644 --- a/include/linux/mfd/macsmc.h +++ b/include/linux/mfd/macsmc.h @@ -223,6 +223,55 @@ int apple_smc_write(struct apple_smc *smc, smc_key key, const void *buf, size_t */ int apple_smc_write_noirq(struct apple_smc *smc, smc_key key, const void *buf, size_t size); +struct device_node; +struct platform_device; + +#define APPLE_SMC_WARM_RESET_WRITES 4 + +/** + * struct apple_smc_warm_reset - a PCIe endpoint's reset through an SMC key + * @smc: the SMC's device (a reference, dropped by apple_smc_warm_reset_put()) + * @key: the key to write + * @count: how many writes + * @writes: per write, the key's value (its bytes from the cell's most significant down) + * and the wait after it in microseconds + * + * Read from a PCIe root port's "apple,function-warm-reset" and "apple,warm-reset-writes" + * (bindings/pci/apple,pcie.yaml). + */ +struct apple_smc_warm_reset { + struct platform_device *smc; + smc_key key; + unsigned int count; + u32 writes[2 * APPLE_SMC_WARM_RESET_WRITES]; +}; + +/** + * apple_smc_warm_reset_parse - read a root port's warm reset + * @np: the root port's node + * @wr: filled in on success + * + * Return: Zero on success, -ENODEV when the port describes none or its SMC has no device, + * -EINVAL when the writes are malformed. + */ +int apple_smc_warm_reset_parse(struct device_node *np, struct apple_smc_warm_reset *wr); + +/** + * apple_smc_warm_reset_put - drop the reference apple_smc_warm_reset_parse() took + * @wr: the parsed reset; cleared + */ +void apple_smc_warm_reset_put(struct apple_smc_warm_reset *wr); + +/** + * apple_smc_warm_reset - give a parsed warm reset + * @wr: the parsed reset + * @noirq: from another device's noirq callbacks: the replies are polled for and the waits + * busy-wait, as for apple_smc_write_noirq() + * + * Return: Zero on success, -ENODEV when the SMC isn't bound, or the first write's error. + */ +int apple_smc_warm_reset(const struct apple_smc_warm_reset *wr, bool noirq); + /** * apple_smc_enter_atomic - Enter atomic mode to be able to use apple_smc_write_atomic * @smc: Pointer to apple_smc struct diff --git a/tools/asahi/centauri-scan-tests.py b/tools/asahi/centauri-scan-tests.py index cb41035cf500b..a095cb79c2b05 100755 --- a/tools/asahi/centauri-scan-tests.py +++ b/tools/asahi/centauri-scan-tests.py @@ -394,6 +394,11 @@ def extract_fn(source, name): static void mutex_lock(int *l) { assert(!*l); *l = 1; } static void mutex_unlock(int *l) { assert(*l); *l = 0; } static u32 readl(void *addr) { (void)addr; return bar_state; } +static unsigned int recovery_requests; +static void centauri_recovery_request(struct centauri *c, const char *why) +{ + (void)c; (void)why; recovery_requests++; +} static size_t build_scan(struct centauri_wifi *w, unsigned int c, u8 *b) { (void)w; (void)c; (void)b;