diff --git a/Documentation/devicetree/bindings/usb/apple,sn201202x.yaml b/Documentation/devicetree/bindings/usb/apple,sn201202x.yaml index ea3d497d46748..16e43804a9f29 100644 --- a/Documentation/devicetree/bindings/usb/apple,sn201202x.yaml +++ b/Documentation/devicetree/bindings/usb/apple,sn201202x.yaml @@ -95,7 +95,10 @@ properties: the J714s) that the controller's LOCK command takes to unlock the commands that need it. These are its debug mode, for rearming the debug link after a sleep ended it, and a disconnect and reattach of - its partner (DISC). + its partner (DISC). It marks a port that may carry a debug link, and + whose partner the OS may replug on its own after a sleep. A port + without it needs no key here for a replug the user asks for, the key + is then the board number of the machine's compatible, reversed. apple,event-irq-in-sleep: type: boolean diff --git a/arch/arm64/configs/asahi.config b/arch/arm64/configs/asahi.config index da707b138c4a0..763871d3308de 100644 --- a/arch/arm64/configs/asahi.config +++ b/arch/arm64/configs/asahi.config @@ -131,6 +131,7 @@ CONFIG_USB4=m CONFIG_USB4_APPLE_SOC=m CONFIG_USB_XHCI_PCI_ASMEDIA=y CONFIG_TYPEC_SN201202X=m +CONFIG_TYPEC_TPS6598X_DEBUG_COMMANDS=y CONFIG_RTC_DRV_MACSMC=y CONFIG_RTC_DRV_APPLE_PMU=y CONFIG_APPLE_ADMAC=m diff --git a/drivers/usb/typec/tipd/Kconfig b/drivers/usb/typec/tipd/Kconfig index 9f68c60d88caf..fe32aa980cd97 100644 --- a/drivers/usb/typec/tipd/Kconfig +++ b/drivers/usb/typec/tipd/Kconfig @@ -31,6 +31,7 @@ config TYPEC_SN201202X config TYPEC_TPS6598X_DEBUG_COMMANDS bool "Debugfs command access to TPS6598x/CD321x controllers" depends on TYPEC_TPS6598X_CORE && DEBUG_FS + default y if ARCH_APPLE help Add a root-only debugfs file, usb/tipd//command, that sends a few controller commands (LOCK, DBMa, VDMs, DVEn and DISC) to the @@ -42,7 +43,13 @@ config TYPEC_TPS6598X_DEBUG_COMMANDS device. The read-only register file, usb/tipd//regs, is available with DEBUG_FS whether or not this is set. - Say N unless you use the controller's debug mode for development. + With kernel lockdown, in integrity or confidentiality mode, opening + the command file (and usb/tipd//replug) fails with EPERM. + The regs file can still be read. + + Say Y on Apple silicon, so that a hung Mac on a USB-C cable can be + rebooted from this one. Say N elsewhere, unless you use the + controller's debug mode for development. config TYPEC_TIPD_KUNIT_TEST tristate "KUnit tests for CD321x" if !KUNIT_ALL_TESTS @@ -67,7 +74,8 @@ config TYPEC_TIPD_KUNIT_TEST nothing, and that other providers and errors keep the bounded retries. They also cover which DisplayPort hotplug event a cable update sends - to the display connector. + to the display connector, and the LOCK key derived from the board's + number. These tests run without accessing a physical Type-C controller. config TYPEC_SN201202X_KUNIT_TEST diff --git a/drivers/usb/typec/tipd/Makefile b/drivers/usb/typec/tipd/Makefile index d8b3b0f4ec5ec..ef81e4ce43d7f 100644 --- a/drivers/usb/typec/tipd/Makefile +++ b/drivers/usb/typec/tipd/Makefile @@ -13,5 +13,6 @@ obj-$(CONFIG_TYPEC_SN201202X) += sn201202x.o sn201202x-y := spmi.o obj-$(CONFIG_TYPEC_TIPD_KUNIT_TEST) += cd321x-pm-test.o cd321x-tbt-test.o \ - cd321x-irq-test.o cd321x-mux-test.o cd321x-dp-test.o + cd321x-irq-test.o cd321x-mux-test.o cd321x-dp-test.o \ + tipd-key-test.o obj-$(CONFIG_TYPEC_SN201202X_KUNIT_TEST) += sn201202x-pm-test.o diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index 01c7d2fa4d048..4452518b4c9de 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -3781,40 +3781,50 @@ static int cd321x_remove(struct tps6598x *tps) } /* - * An SN201202x takes "DISC" unlocked with the key its DT gives it (apple,debug-lock-key, the board - * number reversed). + * The LOCK key of the board with the machine compatible @compatible: the letter and the three + * digits of its board number, reversed. "apple,j414c" gives "414J", "apple,k114s" "411K". */ -static int tipd_replug_key(struct tps6598x *tps, u8 key[4]) +VISIBLE_IF_KUNIT int tipd_board_lock_key(const char *compatible, u8 key[4]) +{ + size_t len = str_has_prefix(compatible, "apple,"); + const char *board = compatible + len; + + /* Short-circuits at the end of the string. A digit after the three is a SoC's number. */ + if (!len || !isalpha(board[0]) || !isdigit(board[1]) || !isdigit(board[2]) || + !isdigit(board[3]) || isdigit(board[4])) + return -EOPNOTSUPP; + key[0] = board[3]; + key[1] = board[2]; + key[2] = board[1]; + key[3] = toupper(board[0]); + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(tipd_board_lock_key); + +/* The LOCK key a controller's DT gives it, apple,debug-lock-key. */ +static int tipd_dt_lock_key(struct tps6598x *tps, u8 key[4]) { const char *s; - if (!device_is_compatible(tps->dev, "apple,sn201202x") || - device_property_read_string(tps->dev, "apple,debug-lock-key", &s) || strlen(s) != 4) + if (device_property_read_string(tps->dev, "apple,debug-lock-key", &s) || strlen(s) != 4) return -EOPNOTSUPP; memcpy(key, s, 4); return 0; } -/* - * A CD321x takes the board number reversed as its LOCK key, as the J716c's does ("617J"). - * It comes from the machine's compatible: "apple,j414c" gives "414J". - */ -static int cd321x_board_key(u8 key[4]) +/* The LOCK key of an Apple controller: the one its DT gives it, or the board's. */ +static int tipd_lock_key(struct tps6598x *tps, u8 key[4]) { - struct device_node *root = of_find_node_by_path("/"); - const char *s; - int ret = -EOPNOTSUPP; + const char *machine; - if (root && !of_property_read_string(root, "compatible", &s) && - !strncmp(s, "apple,j", 7) && isdigit(s[7]) && isdigit(s[8]) && isdigit(s[9])) { - key[0] = s[9]; - key[1] = s[8]; - key[2] = s[7]; - key[3] = 'J'; - ret = 0; - } - of_node_put(root); - return ret; + if (!device_is_compatible(tps->dev, "apple,sn201202x") && + !device_is_compatible(tps->dev, "apple,cd321x")) + return -EOPNOTSUPP; + if (!tipd_dt_lock_key(tps, key)) + return 0; + if (of_machine_read_compatible(&machine, 0)) + return -EOPNOTSUPP; + return tipd_board_lock_key(machine, key); } /* @@ -3826,7 +3836,7 @@ static int cd321x_board_key(u8 key[4]) * The renegotiation's DISC takes the controller unlocked with the board's LOCK key, the board * number reversed. A port whose DT gives it (apple,debug-lock-key) is left as it was if the * renegotiation fails. A port whose DT does not give it (the J716c's) takes the key from the - * board number (cd321x_board_key()), and a failure there fails the probe. + * board number (tipd_lock_key()), and a failure there fails the probe. */ static int __cd321x_reset_inherited_connection(struct tps6598x *tps, const u8 key[4], @@ -3887,7 +3897,7 @@ static int __cd321x_reset_inherited_connection(struct tps6598x *tps, const u8 ke static int cd321x_reset_inherited_connection(struct tps6598x *tps, u32 *status) { - bool best_effort = true; + bool best_effort; u32 fresh; u8 key[4]; int ret; @@ -3897,13 +3907,11 @@ static int cd321x_reset_inherited_connection(struct tps6598x *tps, u32 *status) tps->dbma_at_probe || !(*status & TPS_STATUS_PLUG_PRESENT) || !TPS_STATUS_TO_TYPEC_DATAROLE(*status)) return 0; - if (tipd_replug_key(tps, key)) { - if (cd321x_board_key(key)) { - dev_warn(tps->dev, - "inherited connection not reinitialized: no apple,debug-lock-key\n"); - return 0; - } - best_effort = false; + best_effort = !tipd_dt_lock_key(tps, key); + if (!best_effort && tipd_lock_key(tps, key)) { + dev_warn(tps->dev, + "inherited connection not reinitialized: no apple,debug-lock-key\n"); + return 0; } ret = __cd321x_reset_inherited_connection(tps, key, status); @@ -3945,7 +3953,7 @@ static int tipd_read_plug_state(struct tps6598x *tps) return 0; } -/* The ports tipd_port_replug() looks up by the USB role switch their data role drives. */ +/* The ports that can be replugged; tipd_port_replug() looks one up by its USB role switch. */ static LIST_HEAD(tipd_ports); static DEFINE_MUTEX(tipd_ports_lock); @@ -3955,8 +3963,6 @@ static void tipd_port_list_add(struct tps6598x *tps) { INIT_LIST_HEAD(&tps->port_node); INIT_WORK(&tps->replug_work, tipd_replug_work); - if (!tps->role_sw) - return; mutex_lock(&tipd_ports_lock); list_add_tail(&tps->port_node, &tipd_ports); mutex_unlock(&tipd_ports_lock); @@ -3972,11 +3978,14 @@ static void tipd_port_list_del(struct tps6598x *tps) cancel_work_sync(&tps->replug_work); } +/* A port with a connector (MagSafe's controller has none) and a known LOCK key. */ bool tipd_replug_supported(struct tps6598x *tps) { + struct fwnode_handle *con = device_get_named_child_node(tps->dev, "connector"); u8 key[4]; - return !tipd_replug_key(tps, key); + fwnode_handle_put(con); + return con && !tipd_lock_key(tps, key); } /* @@ -3999,12 +4008,7 @@ static void tipd_replug_work(struct work_struct *work) u8 key[4]; mutex_lock(&tps->lock); - if (tps->kept_attach_replug) { - memcpy(key, tps->kept_attach_key, sizeof(key)); - ret = 0; - } else { - ret = tipd_replug_key(tps, key); - } + ret = tipd_lock_key(tps, key); if (!ret) ret = tps6598x_exec_cmd_gap(tps, "LOCK", sizeof(key), key, sizeof(result), &result, 1000, 0, CD321X_SSPS_READ_GAP_US); @@ -4040,7 +4044,6 @@ static void tipd_replug_work(struct work_struct *work) msleep(500); } - tps->kept_attach_replug = false; if (ret || lock_ret) dev_warn(tps->dev, "port replug for its partner failed: %d (unlock result %u), locked again: %d (try %u)\n", ret, result, lock_ret, tries); @@ -4055,8 +4058,6 @@ static int tipd_replug_queue(struct tps6598x *tps, u8 seconds) int ret; lockdep_assert_held(&tipd_ports_lock); - if (!tipd_replug_supported(tps)) - return -EOPNOTSUPP; if (work_pending(&tps->replug_work)) return -EBUSY; tps->replug_seconds = seconds; @@ -4077,19 +4078,26 @@ static int tipd_replug_queue(struct tps6598x *tps, u8 seconds) * DP alt mode hub whose SuperSpeed side does not come back after some wakes (dwc3-apple). A * display on the port goes dark meanwhile. The replug runs on the port's own work, after this * returns: 0 when it is queued, -EBUSY if one already is, -EOPNOTSUPP on a controller that is - * not known to take it (an SN201202x whose DT gives apple,debug-lock-key), -ENODEV if no port - * drives @sw. + * not known to take it unasked (an SN201202x whose DT gives apple,debug-lock-key), -ENODEV if no + * port drives @sw. */ int tipd_port_replug(struct usb_role_switch *sw, u8 seconds) { struct tps6598x *tps; int ret = -ENODEV; + u8 key[4]; + if (!sw) + return -ENODEV; mutex_lock(&tipd_ports_lock); list_for_each_entry(tps, &tipd_ports, port_node) { if (tps->role_sw != sw) continue; - ret = tipd_replug_queue(tps, seconds); + if (device_is_compatible(tps->dev, "apple,sn201202x") && + !tipd_dt_lock_key(tps, key)) + ret = tipd_replug_queue(tps, seconds); + else + ret = -EOPNOTSUPP; break; } mutex_unlock(&tipd_ports_lock); @@ -4123,7 +4131,8 @@ static void cd321x_replug_kept_attach(struct tps6598x *tps) u32 status; int ret; - if (!device_is_compatible(tps->dev, "apple,cd321x")) + if (!device_is_compatible(tps->dev, "apple,cd321x") || !tps->role_sw || + !tipd_replug_supported(tps)) return; scoped_guard(mutex, &tps->lock) { if (!tps6598x_read_status(tps, &status) || (status & TPS_STATUS_PLUG_PRESENT)) @@ -4132,31 +4141,29 @@ static void cd321x_replug_kept_attach(struct tps6598x *tps) (state[CD321X_ATTACH_STATE_BYTE] & kept) != kept) return; } - if (cd321x_board_key(tps->kept_attach_key)) - return; guard(mutex)(&tipd_ports_lock); if (!tps->port_node.next || list_empty(&tps->port_node) || work_pending(&tps->replug_work)) return; - tps->kept_attach_replug = true; tps->replug_seconds = CD321X_KEPT_ATTACH_REPLUG_S; ret = queue_work(system_freezable_wq, &tps->replug_work) ? 0 : -EBUSY; - if (ret) - tps->kept_attach_replug = false; dev_info(tps->dev, "no partner, but the controller kept an earlier attach (%#04x): replugging the port: %d\n", state[CD321X_ATTACH_STATE_BYTE], ret); } /* - * The same replug for this port, asked for through debugfs ("replug"). -ENODEV while the port is - * not on tipd_ports: without a role switch, and from tipd_port_list_del() on, which cancels the - * work after taking the port off, so nothing is queued after it. + * The same replug for this port, asked for through debugfs ("replug"). It needs neither a USB role + * switch (the J700's ports have none, its host is fixed) nor a key in the DT. -ENODEV while the + * port is not on tipd_ports: from tipd_port_list_del() on, which cancels the work after taking the + * port off, so nothing is queued after it. */ int tipd_replug_request(struct tps6598x *tps, u8 seconds) { guard(mutex)(&tipd_ports_lock); if (!tps->port_node.next || list_empty(&tps->port_node)) return -ENODEV; + if (!tipd_replug_supported(tps)) + return -EOPNOTSUPP; return tipd_replug_queue(tps, seconds); } diff --git a/drivers/usb/typec/tipd/debugfs.c b/drivers/usb/typec/tipd/debugfs.c index 2214cf305a673..b6fe8ee9141d1 100644 --- a/drivers/usb/typec/tipd/debugfs.c +++ b/drivers/usb/typec/tipd/debugfs.c @@ -15,8 +15,9 @@ * read-only register access: pread() with the register as offset * (0x00..0x7f) and the length (1..64 bytes) as count. * /sys/kernel/debug/usb/tipd//replug - * Only on a port tipd can replug its partner on (an SN201202x whose DT - * gives apple,debug-lock-key, with a USB role switch). + * Only on a port tipd can replug its partner on (an SN201202x or CD321x + * with a connector, whose LOCK key is known from apple,debug-lock-key + * or from the board number in the machine's compatible). * write: the seconds (1..10) the partner is disconnected for. One replug, * queued as the driver queues its own after a sleep (see * tipd_port_replug()); -EBUSY while one is queued and not yet @@ -316,7 +317,7 @@ void tipd_debugfs_register(struct tps6598x *tps) &tipd_debugfs_command_fops); debugfs_create_file("regs", 0400, dbg->dentry, tps, &tipd_debugfs_regs_fops); - if (tipd_replug_supported(tps) && tps->role_sw) + if (tipd_replug_supported(tps)) debugfs_create_file("replug", 0200, dbg->dentry, tps, &tipd_debugfs_replug_fops); } diff --git a/drivers/usb/typec/tipd/tipd-key-test.c b/drivers/usb/typec/tipd/tipd-key-test.c new file mode 100644 index 0000000000000..e927b29dbf5f1 --- /dev/null +++ b/drivers/usb/typec/tipd/tipd-key-test.c @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * KUnit tests for the LOCK key that tipd derives from the machine compatible + */ + +#include +#include + +#include "tps6598x.h" + +static void tipd_board_lock_key_test(struct kunit *test) +{ + static const struct { + const char *compatible; + const char *key; + } boards[] = { + /* As the DTs of these boards give it */ + { "apple,j714s", "417J" }, + { "apple,j575c", "575J" }, + { "apple,j700", "007J" }, + /* With and without a variant, of one letter or more */ + { "apple,j414c", "414J" }, + { "apple,j516s", "615J" }, + { "apple,j613", "316J" }, + { "apple,j833ct", "338J" }, + { "apple,k114s", "411K" }, + }; + unsigned int i; + u8 key[4]; + + for (i = 0; i < ARRAY_SIZE(boards); i++) { + KUNIT_EXPECT_EQ_MSG(test, tipd_board_lock_key(boards[i].compatible, key), 0, + "%s", boards[i].compatible); + KUNIT_EXPECT_MEMEQ_MSG(test, key, boards[i].key, sizeof(key), + "%s", boards[i].compatible); + } +} + +static void tipd_board_lock_key_not_a_board_test(struct kunit *test) +{ + static const char * const others[] = { + "", "apple,", "apple,j", "apple,j71", "apple,j71b", + /* A SoC, the platform, a device of another maker */ + "apple,t8132", "apple,arm-platform", "apple,s8001", "linux,dummy-virt", + "pine64,j714s", + }; + unsigned int i; + u8 key[4] = { 'n', 'o', 'n', 'e' }; + + for (i = 0; i < ARRAY_SIZE(others); i++) + KUNIT_EXPECT_EQ_MSG(test, tipd_board_lock_key(others[i], key), -EOPNOTSUPP, + "%s", others[i]); + KUNIT_EXPECT_MEMEQ(test, key, "none", sizeof(key)); +} + +static struct kunit_case tipd_key_cases[] = { + KUNIT_CASE(tipd_board_lock_key_test), + KUNIT_CASE(tipd_board_lock_key_not_a_board_test), + {} +}; + +static struct kunit_suite tipd_key_suite = { + .name = "tipd-key", + .test_cases = tipd_key_cases, +}; + +kunit_test_suite(tipd_key_suite); +MODULE_DESCRIPTION("KUnit tests for the TIPD LOCK key"); +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"); diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h index 3881276bfb3fb..8ecfa8e9d0d62 100644 --- a/drivers/usb/typec/tipd/tps6598x.h +++ b/drivers/usb/typec/tipd/tps6598x.h @@ -416,9 +416,6 @@ struct tps6598x { struct list_head port_node; struct work_struct replug_work; u8 replug_seconds; - /* The unlock key of a replug queued for a kept attach (cd321x_replug_kept_attach()). */ - u8 kept_attach_key[4]; - bool kept_attach_replug; struct device *dev; struct regmap *regmap; struct mutex lock; /* device lock */ @@ -686,6 +683,7 @@ static inline void tipd_debugfs_exit(void) { } #if IS_ENABLED(CONFIG_KUNIT) struct device_node; +int tipd_board_lock_key(const char *compatible, u8 key[4]); int sn201202x_select_irq_get(struct device_node *np, bool *select_polled); void cd321x_mux_failed(struct cd321x *cd321x, const char *what, int ret, bool held); void cd321x_mux_gave_up(struct cd321x *cd321x, unsigned int retries, unsigned int held_ms);