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
9 changes: 8 additions & 1 deletion 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 Down Expand Up @@ -472,8 +473,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 +522,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 +780,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
245 changes: 245 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,245 @@
// 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"

s64 centauri_fw_event_scan_settle_remainder_ms(bool event_valid, ktime_t event_at,
ktime_t now, unsigned settle_ms)
{
s64 elapsed;

if (!event_valid || !settle_ms)
return 0;
elapsed = ktime_to_ms(ktime_sub(now, event_at));
if (elapsed < 0)
elapsed = 0;
if (elapsed >= settle_ms)
return 0;
return settle_ms - elapsed;
}
EXPORT_SYMBOL_GPL(centauri_fw_event_scan_settle_remainder_ms);

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:
return -EOPNOTSUPP;
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 (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;
}

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_stop23(struct kunit *test)
{
enum centauri_t6050_boot_phase phase = CEN_BOOT_READY;

KUNIT_EXPECT_EQ(test,
centauri_t6050_boot_phase_for_control_stage(2, &phase),
-EOPNOTSUPP);
KUNIT_EXPECT_EQ(test,
centauri_t6050_boot_phase_for_control_stage(3, &phase),
-EOPNOTSUPP);
}

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

static void scan_settle_remainder_inside(struct kunit *test)
{
ktime_t event = ktime_set(1000, 0);
ktime_t now = ktime_add_ms(event, 2);

KUNIT_EXPECT_EQ(test,
centauri_fw_event_scan_settle_remainder_ms(true, event,
now, 15),
13);
}

static void scan_settle_remainder_waits_out(struct kunit *test)
{
ktime_t event = ktime_set(0, 0);
ktime_t now = ktime_add_ms(event, 10);

KUNIT_EXPECT_EQ(test,
centauri_fw_event_scan_settle_remainder_ms(true, event,
now, 15),
5);
}

static void scan_settle_remainder_outside(struct kunit *test)
{
ktime_t event = ktime_set(0, 0);
ktime_t now = ktime_add_ms(event, 15);

KUNIT_EXPECT_EQ(test,
centauri_fw_event_scan_settle_remainder_ms(true, event,
now, 15),
0);
now = ktime_add_ms(event, 20);
KUNIT_EXPECT_EQ(test,
centauri_fw_event_scan_settle_remainder_ms(true, event,
now, 15),
0);
}

static void scan_settle_remainder_no_event(struct kunit *test)
{
ktime_t event = ktime_set(0, 0);
ktime_t now = ktime_add_ms(event, 2);

KUNIT_EXPECT_EQ(test,
centauri_fw_event_scan_settle_remainder_ms(false, event,
now, 15),
0);
}

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_stop23),
KUNIT_CASE(recover_phase_null),
KUNIT_CASE(scan_settle_remainder_inside),
KUNIT_CASE(scan_settle_remainder_waits_out),
KUNIT_CASE(scan_settle_remainder_outside),
KUNIT_CASE(scan_settle_remainder_no_event),
{}
};

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 */
25 changes: 25 additions & 0 deletions drivers/net/wireless/apple/centauri/t6050/centauri_recovery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef AURORA_CENTAURI_RECOVERY_H
#define AURORA_CENTAURI_RECOVERY_H

#include <linux/ktime.h>
#include "centauri_t6050.h"

/*
* M5 journal (channels/disconnect event -> scan cmd 0x1000f): failing boots
* 0.34, 2.25, 2.51 ms; good 39.51, 43.04, 46.65 ms; no good pair under 10.79 ms.
*/
#define CEN_FW_EVENT_SCAN_SETTLE_MS 15

int centauri_t6050_boot_phase_for_control_stage(u32 control_stage,
enum centauri_t6050_boot_phase *phase);

void centauri_t6050_transport_recover_init(struct centauri_t6050 *c);
void centauri_t6050_transport_recover_cancel(struct centauri_t6050 *c);
void centauri_t6050_request_transport_recover(struct centauri_t6050 *c);
void centauri_t6050_schedule_transport_recover(struct centauri_t6050 *c);

s64 centauri_fw_event_scan_settle_remainder_ms(bool event_valid, ktime_t event_at,
ktime_t now, unsigned settle_ms);

#endif
2 changes: 2 additions & 0 deletions drivers/net/wireless/apple/centauri/t6050/centauri_t6050.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ struct centauri_t6050 {
struct dentry *debug;
struct mutex boot_lock;
struct delayed_work boot_work;
struct work_struct transport_recover_work;
unsigned int transport_recover_count;
enum centauri_t6050_boot_phase boot_phase;
bool removing, ready;
void *firmware, *descriptor;
Expand Down
Loading
Loading