Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions drivers/net/wireless/apple/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ config APPLE_CENTAURI_T6050_WIFI
help
Add scanning and station connections for the T6050 protocol profile.

config APPLE_CENTAURI_T6050_RECOVERY_KUNIT
bool "KUnit: Centauri T6050 recovery helpers (built-in, COMPILE_TEST)"
depends on KUNIT && COMPILE_TEST
depends on !APPLE_CENTAURI_T6050
help
Build only centauri_recovery.c into vmlinux with its KUnit suite so
kunit.py can run centauri_t6050_recovery without the full T6050 driver.

endif
1 change: 1 addition & 0 deletions drivers/net/wireless/apple/centauri/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
obj-$(CONFIG_APPLE_CENTAURI) += apple_centauri.o
apple_centauri-y := centauri_pci.o centauri_port.o centauri_ipc.o centauri_wifi.o
obj-$(CONFIG_APPLE_CENTAURI_T6050) += t6050/
obj-$(CONFIG_APPLE_CENTAURI_T6050_RECOVERY_KUNIT) += t6050/
9 changes: 9 additions & 0 deletions drivers/net/wireless/apple/centauri/t6050/.kunitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CONFIG_COMPILE_TEST=y
CONFIG_KUNIT=y
CONFIG_NET=y
CONFIG_INET=y
CONFIG_NETDEVICES=y
CONFIG_WIRELESS=y
CONFIG_WLAN=y
CONFIG_WLAN_VENDOR_APPLE=y
CONFIG_APPLE_CENTAURI_T6050_RECOVERY_KUNIT=y
5 changes: 5 additions & 0 deletions drivers/net/wireless/apple/centauri/t6050/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-$(CONFIG_APPLE_CENTAURI_T6050_RECOVERY_KUNIT) += centauri_recovery.o

obj-$(CONFIG_APPLE_CENTAURI_T6050) += apple_centauri_t6050.o
apple_centauri_t6050-y := centauri_pci.o centauri_ipc.o
apple_centauri_t6050-$(CONFIG_APPLE_CENTAURI_T6050_WIFI) += centauri_wifi.o
ifneq ($(CONFIG_APPLE_CENTAURI_T6050_RECOVERY_KUNIT),y)
apple_centauri_t6050-$(CONFIG_APPLE_CENTAURI_T6050_WIFI) += centauri_recovery.o
endif
35 changes: 31 additions & 4 deletions drivers/net/wireless/apple/centauri/t6050/centauri_pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <linux/unaligned.h>

#include "centauri_t6050.h"
#include "centauri_recovery.h"

/* Registry owns one reference, registrations and queued work own additional ones. */
struct centauri_t6050_rewake {
Expand All @@ -31,7 +32,7 @@ struct centauri_t6050_rewake {
struct work_struct work;
struct pci_dev *bridge;
unsigned int slot;
bool active, rerun;
bool active, rerun, force_reset;
unsigned int generation, work_generation;
int error;
struct pci_dev *control;
Expand Down Expand Up @@ -158,7 +159,7 @@ void apple_centauri_sleep_unregister(struct pci_dev *fn)
}
EXPORT_SYMBOL_GPL(apple_centauri_sleep_unregister);

void apple_centauri_sleep_recovery_needed(struct pci_dev *fn, int error)
void centauri_t6050_request_rewake(struct pci_dev *fn, int error, bool force_reset)
{
struct centauri_t6050_rewake *rw;

Expand All @@ -170,6 +171,7 @@ void apple_centauri_sleep_recovery_needed(struct pci_dev *fn, int error)
continue;
if (error && !rw->error)
rw->error = error;
rw->force_reset |= force_reset;
/* One owner and one work reference even while the work is running. */
if (rw->active && rw->generation != rw->work_generation)
rw->rerun = true;
Expand All @@ -182,6 +184,11 @@ void apple_centauri_sleep_recovery_needed(struct pci_dev *fn, int error)
return;
}
}

void apple_centauri_sleep_recovery_needed(struct pci_dev *fn, int error)
{
centauri_t6050_request_rewake(fn, error, false);
}
EXPORT_SYMBOL_GPL(apple_centauri_sleep_recovery_needed);

static struct pci_driver centauri_t6050_driver;
Expand Down Expand Up @@ -472,8 +479,10 @@ static void centauri_t6050_boot_worker(struct work_struct *work)
break;
case CEN_BOOT_IPC:
ret = centauri_t6050_start_ipc(c);
if (!ret)
if (!ret) {
c->boot_phase = CEN_BOOT_READY;
c->transport_recover_count = 0;
}
break;
default:
goto out;
Expand Down Expand Up @@ -519,6 +528,8 @@ static int centauri_t6050_probe(struct pci_dev *pdev, const struct pci_device_id
c->link_ready = true;
mutex_init(&c->boot_lock);
INIT_DELAYED_WORK(&c->boot_work, centauri_t6050_boot_worker);
if (IS_ENABLED(CONFIG_APPLE_CENTAURI_T6050_WIFI))
centauri_t6050_transport_recover_init(c);
c->boot_phase = CEN_BOOT_ROM;
ret = device_property_read_string(&pdev->dev, "firmware-name", &c->firmware_name);
if (ret)
Expand Down Expand Up @@ -775,6 +786,8 @@ static int centauri_t6050_quiesce(struct centauri_t6050 *c)
WRITE_ONCE(c->removing, true);
/* Pairs with control_ready: no new consumer can attach. */
smp_store_release(&c->ready, false);
if (IS_ENABLED(CONFIG_APPLE_CENTAURI_T6050_WIFI))
centauri_t6050_transport_recover_cancel(c);
cancel_delayed_work_sync(&c->boot_work);
/* Never hold boot_lock over an HCI close or Bluetooth work cancellation. */
ret = centauri_t6050_beta_call(c->recovery, false);
Expand Down Expand Up @@ -866,6 +879,19 @@ static int centauri_t6050_rewake_teardown(struct centauri_t6050_rewake *rw)
return ret;
}

static bool centauri_t6050_rewake_already_reset(struct centauri_t6050_rewake *rw)
{
bool force_reset;

mutex_lock(&cen_recovery_lock);
force_reset = rw->force_reset;
rw->force_reset = false;
mutex_unlock(&cen_recovery_lock);

/* A prior wake's reset does not recover a subsequent runtime wedge. */
return !force_reset && apple_pcie_t6050_warm_reset_done(rw->bridge);
}

/*
* The functions are removed while their link is still up, then the warm reset, then the link
* (if it went down, until it is back), then a new enumeration, all under the rescan lock: no
Expand Down Expand Up @@ -908,7 +934,7 @@ static void centauri_t6050_rewake_worker(struct work_struct *work)
* here, on a trained link, would take the link down for good: after a suspend that kept the
* link the host takes the port down and up around the reset instead.
*/
if (apple_pcie_t6050_warm_reset_done(bridge)) {
if (centauri_t6050_rewake_already_reset(rw)) {
pci_dbg(bridge, "N1 after the sleep: reset by the host before its port came up\n");
ret = 0;
} else {
Expand Down Expand Up @@ -987,6 +1013,7 @@ static void centauri_t6050_rewake_worker(struct work_struct *work)
}
rw->active = false;
rw->error = 0;
rw->force_reset = false;
mutex_unlock(&cen_recovery_lock);
kref_put(&rw->ref, centauri_t6050_owner_free);
}
Expand Down
229 changes: 229 additions & 0 deletions drivers/net/wireless/apple/centauri/t6050/centauri_recovery.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
// SPDX-License-Identifier: GPL-2.0-only

#include <linux/export.h>
#include <linux/io.h>
#include <linux/ktime.h>
#include <linux/mutex.h>
#include <linux/pci.h>
#include <linux/workqueue.h>

#include "centauri_recovery.h"

bool centauri_control_idle_ready(bool event_valid, u32 stage_first,
u32 stage_second, bool ring_drained)
{
if (!event_valid)
return true;
/* Settled: the control stage reads running twice in a row and the
* command ring has drained, so no slot is in flight from a pre-event
* command. */
return stage_first == 2 && stage_first == stage_second && ring_drained;
}
EXPORT_SYMBOL_GPL(centauri_control_idle_ready);

int centauri_t6050_boot_phase_for_control_stage(u32 control_stage,
enum centauri_t6050_boot_phase *phase)
{
if (!phase)
return -EINVAL;

switch (control_stage) {
case 0:
*phase = CEN_BOOT_ROM;
return 0;
case 1:
*phase = CEN_BOOT_IPC;
return 0;
case 4:
*phase = CEN_BOOT_CYCLE;
return 0;
case 2:
case 3:
*phase = CEN_BOOT_REWAKE;
return 0;
default:
return -EINVAL;
}
}
EXPORT_SYMBOL_GPL(centauri_t6050_boot_phase_for_control_stage);

#if IS_ENABLED(CONFIG_APPLE_CENTAURI_T6050)

#define CEN_TRANSPORT_RECOVER_MAX 3

static void centauri_t6050_transport_recover_worker(struct work_struct *work)
{
struct centauri_t6050 *c =
container_of(work, struct centauri_t6050, transport_recover_work);

c->transport_recover_count++;
centauri_t6050_schedule_transport_recover(c);
}

void centauri_t6050_transport_recover_init(struct centauri_t6050 *c)
{
INIT_WORK(&c->transport_recover_work, centauri_t6050_transport_recover_worker);
c->transport_recover_count = 0;
}

void centauri_t6050_transport_recover_cancel(struct centauri_t6050 *c)
{
cancel_work_sync(&c->transport_recover_work);
}

void centauri_t6050_request_transport_recover(struct centauri_t6050 *c)
{
if (!c || READ_ONCE(c->removing))
return;
if (c->transport_recover_count >= CEN_TRANSPORT_RECOVER_MAX) {
dev_err(&c->pdev->dev,
"Wi-Fi transport recovery limit (%u) reached\n",
CEN_TRANSPORT_RECOVER_MAX);
return;
}
schedule_work(&c->transport_recover_work);
}
EXPORT_SYMBOL_GPL(centauri_t6050_request_transport_recover);

void centauri_t6050_schedule_transport_recover(struct centauri_t6050 *c)
{
enum centauri_t6050_boot_phase phase;
u32 control;
int ret;

if (!c || READ_ONCE(c->removing))
return;

mutex_lock(&c->boot_lock);
if (READ_ONCE(c->removing) || c->boot_phase == CEN_BOOT_STOPPED ||
c->boot_phase == CEN_BOOT_FAILED)
goto out_unlock;

control = readl(c->bar + 0x8000);
ret = centauri_t6050_boot_phase_for_control_stage(control, &phase);
if (ret) {
dev_err(&c->pdev->dev,
"Wi-Fi transport recovery unsupported (control stage=%#x): %d\n",
control, ret);
c->boot_phase = CEN_BOOT_FAILED;
goto out_unlock;
}
if (phase == CEN_BOOT_REWAKE) {
mutex_unlock(&c->boot_lock);
centauri_t6050_request_rewake(c->pdev, -EIO, true);
return;
}

WRITE_ONCE(c->ready, false);
centauri_t6050_free_ipc(c);
if (phase == CEN_BOOT_ROM)
c->boot_attempted = false;
c->boot_phase = phase;
mutex_unlock(&c->boot_lock);
queue_delayed_work(system_long_wq, &c->boot_work, 0);
return;

out_unlock:
mutex_unlock(&c->boot_lock);
}
EXPORT_SYMBOL_GPL(centauri_t6050_schedule_transport_recover);

#endif /* CONFIG_APPLE_CENTAURI_T6050 */

#if IS_ENABLED(CONFIG_APPLE_CENTAURI_T6050_RECOVERY_KUNIT)

#include <kunit/test.h>

static void recover_phase_rom(struct kunit *test)
{
enum centauri_t6050_boot_phase phase = CEN_BOOT_FAILED;

KUNIT_EXPECT_EQ(test, centauri_t6050_boot_phase_for_control_stage(0, &phase), 0);
KUNIT_EXPECT_EQ(test, phase, CEN_BOOT_ROM);
}

static void recover_phase_ipc(struct kunit *test)
{
enum centauri_t6050_boot_phase phase = CEN_BOOT_FAILED;

KUNIT_EXPECT_EQ(test, centauri_t6050_boot_phase_for_control_stage(1, &phase), 0);
KUNIT_EXPECT_EQ(test, phase, CEN_BOOT_IPC);
}

static void recover_phase_cycle(struct kunit *test)
{
enum centauri_t6050_boot_phase phase = CEN_BOOT_FAILED;

KUNIT_EXPECT_EQ(test, centauri_t6050_boot_phase_for_control_stage(4, &phase), 0);
KUNIT_EXPECT_EQ(test, phase, CEN_BOOT_CYCLE);
}

static void recover_phase_rewake23(struct kunit *test)
{
enum centauri_t6050_boot_phase phase = CEN_BOOT_READY;

KUNIT_ASSERT_EQ(test, centauri_t6050_boot_phase_for_control_stage(2, &phase), 0);
KUNIT_EXPECT_EQ(test, phase, CEN_BOOT_REWAKE);
phase = CEN_BOOT_READY;
KUNIT_ASSERT_EQ(test, centauri_t6050_boot_phase_for_control_stage(3, &phase), 0);
KUNIT_EXPECT_EQ(test, phase, CEN_BOOT_REWAKE);
}

static void recover_phase_null(struct kunit *test)
{
KUNIT_EXPECT_EQ(test,
centauri_t6050_boot_phase_for_control_stage(1, NULL),
-EINVAL);
}

static void control_idle_no_event(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test,
centauri_control_idle_ready(false, 0, 0, false));
}

static void control_idle_settled(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test,
centauri_control_idle_ready(true, 2, 2, true));
}

static void control_idle_unstable_stage(struct kunit *test)
{
KUNIT_EXPECT_FALSE(test,
centauri_control_idle_ready(true, 2, 3, true));
}

static void control_idle_transitional_stage(struct kunit *test)
{
KUNIT_EXPECT_FALSE(test,
centauri_control_idle_ready(true, 3, 3, true));
}

static void control_idle_slot_in_flight(struct kunit *test)
{
KUNIT_EXPECT_FALSE(test,
centauri_control_idle_ready(true, 2, 2, false));
}

static struct kunit_case centauri_recovery_cases[] = {
KUNIT_CASE(recover_phase_rom),
KUNIT_CASE(recover_phase_ipc),
KUNIT_CASE(recover_phase_cycle),
KUNIT_CASE(recover_phase_rewake23),
KUNIT_CASE(recover_phase_null),
KUNIT_CASE(control_idle_no_event),
KUNIT_CASE(control_idle_settled),
KUNIT_CASE(control_idle_unstable_stage),
KUNIT_CASE(control_idle_transitional_stage),
KUNIT_CASE(control_idle_slot_in_flight),
{}
};

static struct kunit_suite centauri_recovery_suite = {
.name = "centauri_t6050_recovery",
.test_cases = centauri_recovery_cases,
};
kunit_test_suite(centauri_recovery_suite);

#endif /* CONFIG_KUNIT */
Loading
Loading