diff --git a/drivers/thunderbolt/ctl.c b/drivers/thunderbolt/ctl.c index 9cb446d9df613..f14252ea4418f 100644 --- a/drivers/thunderbolt/ctl.c +++ b/drivers/thunderbolt/ctl.c @@ -965,6 +965,9 @@ struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl, DECLARE_COMPLETION_ONSTACK(done); int ret; + /* The KUnit tests answer requests without hardware */ + KUNIT_STATIC_STUB_REDIRECT(tb_cfg_request_sync, ctl, req, timeout_msec); + ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done); if (ret) { res.err = ret; diff --git a/drivers/thunderbolt/eeprom.c b/drivers/thunderbolt/eeprom.c index f2d56acb21a10..c7051ead2aba7 100644 --- a/drivers/thunderbolt/eeprom.c +++ b/drivers/thunderbolt/eeprom.c @@ -324,7 +324,7 @@ int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid) } static int tb_drom_parse_entry_generic(struct tb_switch *sw, - struct tb_drom_entry_header *header) + const struct tb_drom_entry_header *header) { const struct tb_drom_entry_generic *entry = (const struct tb_drom_entry_generic *)header; @@ -348,6 +348,13 @@ static int tb_drom_parse_entry_generic(struct tb_switch *sw, const struct tb_drom_entry_desc *desc = (const struct tb_drom_entry_desc *)entry; + /* Only the IDs are read; sizeof() would count the padding */ + if (header->len < offsetofend(struct tb_drom_entry_desc, idProduct)) { + tb_sw_warn(sw, "product descriptor entry too short (%u bytes), ignoring\n", + header->len); + break; + } + if (!sw->vendor && !sw->device) { sw->vendor = desc->idVendor; sw->device = desc->idProduct; @@ -360,7 +367,7 @@ static int tb_drom_parse_entry_generic(struct tb_switch *sw, } static int tb_drom_parse_entry_port(struct tb_switch *sw, - struct tb_drom_entry_header *header) + const struct tb_drom_entry_header *header) { struct tb_port *port; int res; @@ -392,11 +399,13 @@ static int tb_drom_parse_entry_port(struct tb_switch *sw, type &= 0xffffff; if (type == TB_TYPE_PORT) { - struct tb_drom_entry_port *entry = (void *) header; + const struct tb_drom_entry_port *entry = + (const struct tb_drom_entry_port *)header; + if (header->len != sizeof(*entry)) { tb_sw_warn(sw, "port entry has size %#x (expected %#zx)\n", - header->len, sizeof(struct tb_drom_entry_port)); + header->len, sizeof(*entry)); return -EIO; } port->link_nr = entry->link_nr; @@ -427,9 +436,16 @@ static int tb_drom_parse_entries(struct tb_switch *sw, size_t header_size) int res; while (pos < drom_size) { - struct tb_drom_entry_header *entry = (void *) (sw->drom + pos); - if (pos + 1 == drom_size || pos + entry->len > drom_size - || !entry->len) { + const struct tb_drom_entry_header *entry; + + if (drom_size - pos < sizeof(*entry)) { + tb_sw_warn(sw, "DROM buffer overrun\n"); + return -EIO; + } + + entry = (const struct tb_drom_entry_header *)(sw->drom + pos); + if (entry->len < sizeof(*entry) || + entry->len > drom_size - pos) { tb_sw_warn(sw, "DROM buffer overrun\n"); return -EIO; } @@ -659,6 +675,13 @@ static int tb_drom_parse(struct tb_switch *sw, u16 size) const struct tb_drom_header *header = (const void *)sw->drom; int ret; + /* The USB4 header is the shorter one, and holds the revision and length */ + if (size < USB4_DROM_HEADER_SIZE) { + tb_sw_warn(sw, "DROM too small (%u bytes)\n", size); + ret = -EIO; + goto err; + } + if (header->data_len + TB_DROM_DATA_START != size) { tb_sw_warn(sw, "DROM size mismatch\n"); ret = -EIO; @@ -676,6 +699,11 @@ static int tb_drom_parse(struct tb_switch *sw, u16 size) header->device_rom_revision); fallthrough; case 1: + if (size < TB_DROM_HEADER_SIZE) { + tb_sw_warn(sw, "DROM too small for its header (%u bytes)\n", size); + ret = -EIO; + break; + } ret = tb_drom_parse_v1(sw); break; } diff --git a/drivers/thunderbolt/path.c b/drivers/thunderbolt/path.c index 8d4e3d63d21c4..a41fd7b1b30d6 100644 --- a/drivers/thunderbolt/path.c +++ b/drivers/thunderbolt/path.c @@ -31,20 +31,36 @@ static void tb_dump_hop(const struct tb_path_hop *hop, const struct tb_regs_hop regs->unknown1, regs->unknown2, regs->unknown3); } +/* + * The adapter that @hop, read from @port, sends the path out of: the router + * reports it, so it is NULL if the router has no such adapter. + */ +static struct tb_port *tb_path_hop_out_port(struct tb_port *port, + const struct tb_regs_hop *hop) +{ + struct tb_switch *sw = port->sw; + + /* Adapter 0 is the control adapter: no tunnel path leaves through it */ + if (!hop->out_port || hop->out_port > sw->config.max_port_number) { + tb_port_warn_ratelimited(port, "path goes out of invalid adapter %u\n", + hop->out_port); + return NULL; + } + + return &sw->ports[hop->out_port]; +} + static struct tb_port *tb_path_find_dst_port(struct tb_port *src, int src_hopid, int dst_hopid) { struct tb_port *port, *out_port = NULL; struct tb_regs_hop hop; - struct tb_switch *sw; int i, ret, hopid; hopid = src_hopid; port = src; for (i = 0; port && i < TB_PATH_MAX_HOPS; i++) { - sw = port->sw; - ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hopid, 2); if (ret) { tb_port_warn(port, "failed to read path at %d\n", hopid); @@ -54,7 +70,9 @@ static struct tb_port *tb_path_find_dst_port(struct tb_port *src, int src_hopid, if (!hop.enable) return NULL; - out_port = &sw->ports[hop.out_port]; + out_port = tb_path_hop_out_port(port, &hop); + if (!out_port) + return NULL; hopid = hop.next_hop; port = out_port->remote; } @@ -106,7 +124,6 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid, struct tb_port *out_port; struct tb_regs_hop hop; struct tb_path *path; - struct tb_switch *sw; struct tb_port *p; size_t num_hops; int ret, i, h; @@ -129,8 +146,6 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid, num_hops = 0; for (i = 0; p && i < TB_PATH_MAX_HOPS; i++) { - sw = p->sw; - ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2); if (ret) { tb_port_warn(p, "failed to read path at %d\n", h); @@ -141,7 +156,9 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid, if (!hop.enable) break; - out_port = &sw->ports[hop.out_port]; + out_port = tb_path_hop_out_port(p, &hop); + if (!out_port) + return NULL; if (last) *last = out_port; @@ -170,7 +187,9 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid, for (i = 0; i < num_hops; i++) { int next_hop; - sw = p->sw; + /* Ended early: a router reported an adapter without a link this time */ + if (!p) + goto err; ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2); if (ret) { @@ -178,10 +197,18 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid, goto err; } + /* The hop may have been disabled since the first pass */ + if (!hop.enable) + goto err; + + /* Read again: the router may report another adapter this time */ + out_port = tb_path_hop_out_port(p, &hop); + if (!out_port) + goto err; + if (alloc_hopid && tb_port_alloc_in_hopid(p, h, h) < 0) goto err; - out_port = &sw->ports[hop.out_port]; next_hop = hop.next_hop; if (alloc_hopid && diff --git a/drivers/thunderbolt/property.c b/drivers/thunderbolt/property.c index df18a6d69d8b4..7fea31d06e47b 100644 --- a/drivers/thunderbolt/property.c +++ b/drivers/thunderbolt/property.c @@ -246,6 +246,8 @@ struct tb_property_dir *tb_property_parse_dir(const u32 *block, const struct tb_property_rootdir_entry *rootdir = (const struct tb_property_rootdir_entry *)block; + if (block_len < sizeof(*rootdir) / 4) + return NULL; if (rootdir->magic != TB_PROPERTY_ROOTDIR_MAGIC) return NULL; if (rootdir->length > block_len) diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c index 41d9bdc44e8df..ed04a9b3eefda 100644 --- a/drivers/thunderbolt/tb.c +++ b/drivers/thunderbolt/tb.c @@ -3661,6 +3661,12 @@ static void tb_handle_dp_bandwidth_request(struct work_struct *work) goto unlock; } + if (ev->port > sw->config.max_port_number) { + tb_warn(tb, "bandwidth request from non-existent adapter %llx:%u\n", + ev->route, ev->port); + goto put_sw; + } + in = &sw->ports[ev->port]; if (!tb_port_is_dpin(in)) { tb_port_warn(in, "bandwidth request to non-DP IN adapter\n"); @@ -4581,4 +4587,23 @@ void tb_test_cm_stop(struct tb *tb) if (cancel_work_sync(&tcm->dp_relink_run)) tb_domain_put(tb); } + +/* Handle a DP bandwidth notification from @port of the router at @route, with no retry. */ +void tb_test_cm_dp_bandwidth_request(struct tb *tb, u64 route, u8 port) +{ + struct tb_cm *tcm = tb_priv(tb); + struct tb_hotplug_event *ev; + + ev = kmalloc_obj(*ev); + if (!ev) + return; + + tcm->hotplug_active = true; + ev->tb = tb; + ev->route = route; + ev->port = port; + ev->retry = TB_BW_ALLOC_RETRIES; + INIT_DELAYED_WORK(&ev->work, tb_handle_dp_bandwidth_request); + tb_handle_dp_bandwidth_request(&ev->work.work); +} #endif diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h index 70caebcf2b38b..12ce300710f96 100644 --- a/drivers/thunderbolt/tb.h +++ b/drivers/thunderbolt/tb.h @@ -736,6 +736,8 @@ static inline int tb_port_write(struct tb_port *port, const void *buffer, #define tb_err(tb, fmt, arg...) dev_err((tb)->nhi->dev, fmt, ## arg) #define tb_WARN(tb, fmt, arg...) dev_WARN((tb)->nhi->dev, fmt, ## arg) #define tb_warn(tb, fmt, arg...) dev_warn((tb)->nhi->dev, fmt, ## arg) +#define tb_warn_ratelimited(tb, fmt, arg...) \ + dev_warn_ratelimited((tb)->nhi->dev, fmt, ## arg) #define tb_info(tb, fmt, arg...) dev_info((tb)->nhi->dev, fmt, ## arg) #define tb_dbg(tb, fmt, arg...) dev_dbg((tb)->nhi->dev, fmt, ## arg) @@ -760,6 +762,8 @@ static inline int tb_port_write(struct tb_port *port, const void *buffer, __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg) #define tb_port_warn(port, fmt, arg...) \ __TB_PORT_PRINT(tb_warn, port, fmt, ##arg) +#define tb_port_warn_ratelimited(port, fmt, arg...) \ + __TB_PORT_PRINT(tb_warn_ratelimited, port, fmt, ##arg) #define tb_port_info(port, fmt, arg...) \ __TB_PORT_PRINT(tb_info, port, fmt, ##arg) #define tb_port_dbg(port, fmt, arg...) \ @@ -777,6 +781,9 @@ bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type, const void *buf, size_t size); bool tb_domain_retain_noirq_control(struct tb *tb); bool tb_xdp_noirq_request(enum tb_xdp_type type); +int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route, + const uuid_t *src_uuid, const uuid_t *dst_uuid, + int retry, u32 **block, u32 *generation); #endif #if IS_ENABLED(CONFIG_USB4_KUNIT_TEST) size_t tb_test_cm_size(void); @@ -786,6 +793,7 @@ struct list_head *tb_test_cm_dp_resources(struct tb *tb); void tb_test_cm_set_relink_deferred(struct tb *tb, bool deferred); bool tb_test_cm_relink_armed(struct tb *tb); void tb_test_cm_stop(struct tb *tb); +void tb_test_cm_dp_bandwidth_request(struct tb *tb, u64 route, u8 port); #endif extern const struct device_type tb_domain_type; diff --git a/drivers/thunderbolt/test.c b/drivers/thunderbolt/test.c index fbb318f17812d..347fe4d04495a 100644 --- a/drivers/thunderbolt/test.c +++ b/drivers/thunderbolt/test.c @@ -6,11 +6,15 @@ * Author: Mika Westerberg */ +#include #include +#include #include #include #include +#include #include +#include #include "tb.h" #include "tunnel.h" @@ -3035,6 +3039,23 @@ static void tb_test_property_parse_zero_length(struct kunit *test) tb_property_free_dir(dir); } +/* A block shorter than the root directory header is not read past. */ +static void tb_test_property_parse_short_block(struct kunit *test) +{ + size_t len; + + for (len = 0; len < 2; len++) { + /* Exactly the block, so a read past it is caught */ + u32 *block = kunit_kzalloc(test, max_t(size_t, len, 1) * sizeof(u32), + GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, block); + block[0] = 0x55584401; /* rootdir magic */ + KUNIT_EXPECT_NULL_MSG(test, tb_property_parse_dir(block, len), + "%zu dwords", len); + } +} + static void tb_test_property_parse_rootdir_overflow(struct kunit *test) { u32 *block = kunit_kzalloc(test, 4 * sizeof(u32), GFP_KERNEL); @@ -4647,6 +4668,466 @@ static void tb_test_noirq_xdomain_filter(struct kunit *test) } } +/* + * A DROM as the device tree hands it to the host router: @size bytes, of which + * the DROM is the first @drom_size. Returns what tb_drom_read() returns. + */ +static int tb_test_drom_read(struct kunit *test, struct tb_switch *sw, + const u8 *drom, size_t drom_size, size_t size) +{ + struct property_entry props[2] = {}; + struct tb_nhi *nhi; + struct device *dev; + struct tb *tb; + u8 *buf; + int ret; + + buf = kunit_kzalloc(test, size, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, buf); + memcpy(buf, drom, drom_size); + /* The data length covers everything after the CRC32 */ + buf[14] = drom_size - 13; + buf[15] = (drom_size - 13) >> 8; + put_unaligned_le32(~crc32c(~0, buf + 13, drom_size - 13), buf + 9); + + dev = kunit_device_register(test, "tb-test-drom"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev); + props[0] = PROPERTY_ENTRY_U8_ARRAY_LEN("apple,thunderbolt-drom", buf, size); + KUNIT_ASSERT_EQ(test, device_create_managed_software_node(dev, props, NULL), 0); + + nhi = kunit_kzalloc(test, sizeof(*nhi), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, nhi); + tb = kunit_kzalloc(test, sizeof(*tb), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, tb); + nhi->dev = dev; + tb->nhi = nhi; + sw->tb = tb; + + ret = tb_drom_read(sw); + kfree(sw->drom); + sw->drom = NULL; + kunit_device_unregister(test, dev); + return ret; +} + +static void tb_test_drom_free_names(void *data) +{ + struct tb_switch *sw = data; + + kfree(sw->vendor_name); + kfree(sw->device_name); +} + +static struct tb_switch *tb_test_drom_switch(struct kunit *test) +{ + struct tb_switch *sw = alloc_host(test); + + KUNIT_ASSERT_NOT_NULL(test, sw); + KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, tb_test_drom_free_names, sw), 0); + return sw; +} + +/* USB4 DROM header: revision 3, the entries follow at byte 16 */ +#define TB_TEST_DROM_V3 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0 +/* Generic entry @index of @len bytes, header included */ +#define TB_TEST_DROM_GENERIC(len, index) (len), (index) +/* The vendor name "Aurora" */ +#define TB_TEST_DROM_VENDOR TB_TEST_DROM_GENERIC(9, 1), 'A', 'u', 'r', 'o', 'r', 'a', 0 + +/* An entry is at least its two-byte header, and fits in the DROM. */ +static void tb_test_drom_entry_lengths(struct kunit *test) +{ + static const u8 valid[] = { TB_TEST_DROM_V3, TB_TEST_DROM_VENDOR }; + /* A name entry of one byte, the last two bytes of the DROM */ + static const u8 one[] = { TB_TEST_DROM_V3, TB_TEST_DROM_VENDOR, + TB_TEST_DROM_GENERIC(1, 2) }; + static const u8 zero[] = { TB_TEST_DROM_V3, TB_TEST_DROM_GENERIC(0, 2), + TB_TEST_DROM_VENDOR }; + /* Only one byte left for the next entry's header */ + static const u8 tail[] = { TB_TEST_DROM_V3, TB_TEST_DROM_VENDOR, 5 }; + static const u8 header_only[] = { TB_TEST_DROM_V3, TB_TEST_DROM_GENERIC(2, 2), + TB_TEST_DROM_VENDOR }; + static const u8 overrun[] = { TB_TEST_DROM_V3, TB_TEST_DROM_VENDOR, + TB_TEST_DROM_GENERIC(4, 2), 'x' }; + struct tb_switch *sw; + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, valid, sizeof(valid), sizeof(valid)), 0); + KUNIT_EXPECT_STREQ(test, sw->vendor_name, "Aurora"); + KUNIT_EXPECT_NULL(test, sw->device_name); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, one, sizeof(one), sizeof(one)), -EIO); + KUNIT_EXPECT_NULL(test, sw->device_name); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, zero, sizeof(zero), sizeof(zero)), -EIO); + KUNIT_EXPECT_NULL(test, sw->vendor_name); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, tail, sizeof(tail), sizeof(tail)), -EIO); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, header_only, sizeof(header_only), + sizeof(header_only)), 0); + KUNIT_EXPECT_STREQ(test, sw->device_name, ""); + KUNIT_EXPECT_STREQ(test, sw->vendor_name, "Aurora"); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, overrun, sizeof(overrun), + sizeof(overrun)), -EIO); +} + +/* The product descriptor is used only if it holds both IDs. */ +static void tb_test_drom_product_descriptor(struct kunit *test) +{ + static const u8 full[] = { + TB_TEST_DROM_V3, TB_TEST_DROM_VENDOR, + TB_TEST_DROM_GENERIC(15, 9), 0x00, 0x04, 0xac, 0x05, 0x34, 0x12, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + }; + /* Ends inside the product ID, at the end of the DROM */ + static const u8 short_desc[] = { + TB_TEST_DROM_V3, TB_TEST_DROM_VENDOR, + TB_TEST_DROM_GENERIC(7, 9), 0x00, 0x04, 0xac, 0x05, 0x34, + }; + struct tb_switch *sw; + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, full, sizeof(full), sizeof(full)), 0); + KUNIT_EXPECT_EQ(test, sw->vendor, 0x05ac); + KUNIT_EXPECT_EQ(test, sw->device, 0x1234); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, short_desc, sizeof(short_desc), + sizeof(short_desc)), 0); + KUNIT_EXPECT_EQ(test, sw->vendor, 0); + KUNIT_EXPECT_EQ(test, sw->device, 0); + KUNIT_EXPECT_STREQ(test, sw->vendor_name, "Aurora"); +} + +static u8 tb_test_crc8(const u8 *data, int len) +{ + u8 val = 0xff; + int i, j; + + for (i = 0; i < len; i++) { + val ^= data[i]; + for (j = 0; j < 8; j++) + val = (val << 1) ^ ((val & 0x80) ? 7 : 0); + } + return val; +} + +/* The header is read only from a DROM that holds it. */ +static void tb_test_drom_header_size(struct kunit *test) +{ + /* A revision 1 DROM as long as a USB4 header: 6 bytes short of its own */ + u8 v1[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 1 }; + /* No room for the revision and data length */ + static const u8 tiny[13] = {}; + struct tb_switch *sw; + + v1[0] = tb_test_crc8(v1 + 1, 8); + + /* The device tree property is always at least the full header */ + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, v1, sizeof(v1), 22), -EIO); + KUNIT_EXPECT_EQ(test, sw->vendor, 0); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, tiny, sizeof(tiny), 22), -EIO); +} + +/* + * A DP bandwidth notification names an adapter of its router: one the router + * does not have, or that is no DP IN adapter, is refused without using it. + */ +static void tb_test_dp_bandwidth_request_port(struct kunit *test) +{ + struct tb_test_cm *cm = tb_test_cm_alloc(test); + struct tb_switch *host = cm->host; + static const u8 ports[] = { 14, 63, 0, 1, 7, 5 }; + unsigned int refs; + int i; + + cm->tb->root_switch = host; + refs = kref_read(&host->dev.kobj.kref); + for (i = 0; i < ARRAY_SIZE(ports); i++) { + tb_test_cm_dp_bandwidth_request(cm->tb, 0, ports[i]); + KUNIT_EXPECT_EQ_MSG(test, kref_read(&host->dev.kobj.kref), refs, + "request for adapter %u", ports[i]); + } + /* Nothing was tunneled or changed for any of them */ + KUNIT_EXPECT_TRUE(test, list_empty(tb_test_cm_tunnels(cm->tb))); + cm->tb->root_switch = NULL; +} + +/* A remote host answering XDomain properties requests from a fixed block. */ +struct tb_test_xdp_props { + unsigned int requests; + u16 data_length; + unsigned int words; +}; + +static struct tb_cfg_result tb_test_xdp_props_sync(struct tb_ctl *ctl, + struct tb_cfg_request *req, + int timeout_msec) +{ + struct tb_test_xdp_props *peer = kunit_get_current_test()->priv; + const struct tb_xdp_properties *request = req->request; + struct tb_xdp_properties_response *res = req->response; + struct tb_cfg_result result = {}; + unsigned int i; + + /* Give up after a few, as a remote host that stops answering */ + if (++peer->requests > 4) { + result.err = -ETIMEDOUT; + return result; + } + + memset(res, 0, req->response_size); + res->hdr.xd_hdr.length_sn = (sizeof(*res) - sizeof(res->hdr.xd_hdr)) / 4 + peer->words; + res->hdr.type = PROPERTIES_RESPONSE; + res->offset = request->offset; + res->data_length = peer->data_length; + res->generation = 7; + for (i = 0; i < peer->words; i++) + res->data[i] = request->offset + i + 1; + return result; +} + +static int tb_test_xdp_props_request(struct kunit *test, u32 **block, u32 *generation) +{ + kunit_activate_static_stub(test, tb_cfg_request_sync, tb_test_xdp_props_sync); + return tb_xdp_properties_request(NULL, 0x1, &tb_test_xdp_uuid, &tb_test_service_uuid, + 0, block, generation); +} + +/* Each response moves the request on, and an empty block ends it. */ +static void tb_test_xdp_properties_progress(struct kunit *test) +{ + struct tb_test_xdp_props *peer; + u32 *block = NULL; + u32 generation = 0; + int ret, i; + + peer = kunit_kzalloc(test, sizeof(*peer), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, peer); + test->priv = peer; + + /* An empty block: refused at the first response, not asked for again */ + peer->words = 3; + ret = tb_test_xdp_props_request(test, &block, &generation); + KUNIT_EXPECT_EQ(test, ret, -EINVAL); + KUNIT_EXPECT_EQ(test, peer->requests, 1U); + if (ret >= 0) + kfree(block); + + /* Five dwords in two responses, the second one clamped to the block */ + peer->requests = 0; + peer->data_length = 5; + block = NULL; + ret = tb_test_xdp_props_request(test, &block, &generation); + KUNIT_ASSERT_EQ(test, ret, 5); + KUNIT_EXPECT_EQ(test, peer->requests, 2U); + KUNIT_EXPECT_EQ(test, generation, 7U); + for (i = 0; i < 5; i++) + KUNIT_EXPECT_EQ(test, block[i], (u32)i + 1); + kfree(block); +} + +/* Routers whose hop entries go out of the adapters listed, one per read. */ +struct tb_test_hops { + unsigned int reads, count; + u8 out_port[4]; + bool disabled[4]; +}; + +static int tb_test_hops_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, + enum tb_cfg_space space, u32 offset, u32 length) +{ + struct tb_test_hops *hops = kunit_get_current_test()->priv; + struct tb_regs_hop hop = { .enable = true, .next_hop = 8 }; + unsigned int n = min_t(unsigned int, hops->reads++, hops->count - 1); + + if (space != TB_CFG_HOPS || length * 4 > sizeof(hop)) + return -EIO; + hop.out_port = hops->out_port[n]; + hop.enable = !hops->disabled[n]; + memcpy(buffer, &hop, length * 4); + return 0; +} + +static struct tb_test_hops *tb_test_hops_alloc(struct kunit *test, struct tb_switch **host) +{ + struct tb_test_hops *hops; + struct tb_nhi *nhi; + struct tb *tb; + + hops = kunit_kzalloc(test, sizeof(*hops), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, hops); + nhi = kunit_kzalloc(test, sizeof(*nhi), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, nhi); + tb = kunit_kzalloc(test, sizeof(*tb), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, tb); + *host = alloc_host(test); + KUNIT_ASSERT_NOT_NULL(test, *host); + tb->nhi = nhi; + (*host)->tb = tb; + + test->priv = hops; + kunit_activate_static_stub(test, tb_cfg_read, tb_test_hops_cfg_read); + return hops; +} + +static void tb_test_hops_set(struct tb_test_hops *hops, u8 first, u8 again) +{ + hops->reads = 0; + hops->count = 2; + hops->out_port[0] = first; + hops->out_port[1] = again; + memset(hops->disabled, 0, sizeof(hops->disabled)); +} + +/* + * Path discovery follows the adapters the routers report in their hop + * entries, and stops at one the router does not have. + */ +static void tb_test_path_discover_out_port(struct kunit *test) +{ + struct tb_switch *host; + struct tb_test_hops *hops = tb_test_hops_alloc(test, &host); + struct tb_port *in = &host->ports[5], *lane = &host->ports[1]; + u8 last_port = host->config.max_port_number; + struct tb_port *last = NULL; + struct tb_path *path; + + /* DP IN HopID 8 out of lane adapter 1, where the path ends */ + tb_test_hops_set(hops, 1, 1); + path = tb_path_discover(in, 8, NULL, -1, &last, "Video", true); + KUNIT_ASSERT_NOT_NULL(test, path); + KUNIT_EXPECT_EQ(test, path->path_length, 1); + KUNIT_EXPECT_PTR_EQ(test, path->hops[0].out_port, lane); + KUNIT_EXPECT_PTR_EQ(test, last, lane); + tb_path_free(path); + + /* An adapter past the router's last one, and the largest the entry holds */ + tb_test_hops_set(hops, last_port + 1, last_port + 1); + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + tb_test_hops_set(hops, 63, 63); + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + + /* Valid while the hops are counted, not when they are read again */ + tb_test_hops_set(hops, 1, last_port + 1); + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + KUNIT_EXPECT_EQ(test, hops->reads, 2U); + + /* Looking for the source HopID of a path that ends on the lane adapter */ + tb_test_hops_set(hops, last_port + 1, last_port + 1); + KUNIT_EXPECT_NULL(test, tb_path_discover(in, -1, lane, 8, NULL, "Video", false)); + tb_test_hops_set(hops, 1, 1); + path = tb_path_discover(in, -1, lane, 8, NULL, "Video", false); + KUNIT_ASSERT_NOT_NULL(test, path); + KUNIT_EXPECT_EQ(test, path->hops[0].in_hop_index, 8); + tb_path_free(path); + + /* No HopID is left allocated by the refused paths */ + KUNIT_EXPECT_EQ(test, tb_port_alloc_in_hopid(in, 8, 8), 8); + tb_port_release_in_hopid(in, 8); + KUNIT_EXPECT_EQ(test, tb_port_alloc_out_hopid(lane, 8, 8), 8); + tb_port_release_out_hopid(lane, 8); +} + +/* + * Adapter 0 is the control adapter: a hop entry naming it is refused, on + * either read, and leaves no HopID allocated. + */ +static void tb_test_path_discover_out_port_zero(struct kunit *test) +{ + struct tb_switch *host; + struct tb_test_hops *hops = tb_test_hops_alloc(test, &host); + struct tb_port *in = &host->ports[5], *lane = &host->ports[1]; + + tb_test_hops_set(hops, 0, 0); + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + KUNIT_EXPECT_EQ(test, hops->reads, 1U); + + tb_test_hops_set(hops, 1, 0); + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + KUNIT_EXPECT_EQ(test, hops->reads, 2U); + + KUNIT_EXPECT_EQ(test, tb_port_alloc_in_hopid(in, 8, 8), 8); + tb_port_release_in_hopid(in, 8); + KUNIT_EXPECT_EQ(test, tb_port_alloc_out_hopid(lane, 8, 8), 8); + tb_port_release_out_hopid(lane, 8); +} + +/* + * A hop enabled while the hops are counted but disabled when it is read + * again fails discovery instead of being used as a live hop. + */ +static void tb_test_path_discover_hop_disabled(struct kunit *test) +{ + struct tb_switch *host; + struct tb_test_hops *hops = tb_test_hops_alloc(test, &host); + struct tb_port *in = &host->ports[5], *lane = &host->ports[1]; + + tb_test_hops_set(hops, 1, 1); + hops->disabled[1] = true; + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + KUNIT_EXPECT_EQ(test, hops->reads, 2U); + + KUNIT_EXPECT_EQ(test, tb_port_alloc_in_hopid(in, 8, 8), 8); + tb_port_release_in_hopid(in, 8); + KUNIT_EXPECT_EQ(test, tb_port_alloc_out_hopid(lane, 8, 8), 8); + tb_port_release_out_hopid(lane, 8); +} + +/* + * Read again, a path can change shape: here it reaches a device router + * through lane adapter 1 and ends on the device's adapter 3, then goes out + * of the host's unlinked adapter 3 instead. Discovery fails instead of + * following a link that is not there. + */ +static void tb_test_path_discover_shorter(struct kunit *test) +{ + struct tb_switch *host, *dev; + struct tb_test_hops *hops = tb_test_hops_alloc(test, &host); + struct tb_port *in = &host->ports[5], *unlinked = &host->ports[3]; + struct tb_path *path; + + dev = alloc_dev_default(test, host, 0x1, true); + KUNIT_ASSERT_NOT_NULL(test, dev); + dev->tb = host->tb; + + /* Both reads the same: two hops, the second on the device router */ + hops->reads = 0; + hops->count = 4; + hops->out_port[0] = 1; + hops->out_port[1] = 3; + hops->out_port[2] = 1; + hops->out_port[3] = 3; + path = tb_path_discover(in, 8, NULL, -1, NULL, "Video", true); + KUNIT_ASSERT_NOT_NULL(test, path); + KUNIT_EXPECT_EQ(test, path->path_length, 2); + KUNIT_EXPECT_PTR_EQ(test, path->hops[1].in_port, &dev->ports[1]); + KUNIT_EXPECT_PTR_EQ(test, path->hops[1].out_port, &dev->ports[3]); + tb_path_free(path); + + /* The host's entry names its unlinked adapter 3 on the second read */ + hops->reads = 0; + hops->out_port[2] = 3; + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + KUNIT_EXPECT_EQ(test, hops->reads, 3U); + + /* The HopIDs taken for the first hop were given back */ + KUNIT_EXPECT_EQ(test, tb_port_alloc_in_hopid(in, 8, 8), 8); + tb_port_release_in_hopid(in, 8); + KUNIT_EXPECT_EQ(test, tb_port_alloc_out_hopid(unlinked, 8, 8), 8); + tb_port_release_out_hopid(unlinked, 8); +} + static struct kunit_case tb_test_cases[] = { KUNIT_CASE(tb_test_apple_right_dpin_wiring), KUNIT_CASE(tb_test_dp_reduce_bandwidth_shared), @@ -4720,7 +5201,17 @@ static struct kunit_case tb_test_cases[] = { KUNIT_CASE(tb_test_property_copy), KUNIT_CASE(tb_test_property_parse_zero_length), KUNIT_CASE(tb_test_property_parse_rootdir_overflow), + KUNIT_CASE(tb_test_property_parse_short_block), KUNIT_CASE(tb_test_property_merge), + KUNIT_CASE(tb_test_drom_entry_lengths), + KUNIT_CASE(tb_test_drom_product_descriptor), + KUNIT_CASE(tb_test_drom_header_size), + KUNIT_CASE(tb_test_dp_bandwidth_request_port), + KUNIT_CASE(tb_test_xdp_properties_progress), + KUNIT_CASE(tb_test_path_discover_out_port), + KUNIT_CASE(tb_test_path_discover_shorter), + KUNIT_CASE(tb_test_path_discover_out_port_zero), + KUNIT_CASE(tb_test_path_discover_hop_disabled), { } }; diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c index cd60c717ab4c5..3db6a93c4eabf 100644 --- a/drivers/thunderbolt/xdomain.c +++ b/drivers/thunderbolt/xdomain.c @@ -329,7 +329,7 @@ static int tb_xdp_error_response(struct tb_ctl *ctl, u64 route, u8 sequence, TB_CFG_PKG_XDOMAIN_RESP); } -static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route, +VISIBLE_IF_KUNIT int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route, const uuid_t *src_uuid, const uuid_t *dst_uuid, int retry, u32 **block, u32 *generation) { @@ -394,6 +394,11 @@ static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route, */ if (!data) { data_len = res->data_length; + /* Nothing would be copied, and the request repeated */ + if (!data_len) { + ret = -EINVAL; + goto err; + } if (data_len > TB_XDP_PROPERTIES_MAX_LENGTH) { ret = -E2BIG; goto err; @@ -410,7 +415,7 @@ static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route, len = data_len - req.offset; memcpy(data + req.offset, res->data, len * 4); req.offset += len; - } while (!data_len || req.offset < data_len); + } while (req.offset < data_len); *block = data; *generation = res->generation;