Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
3 changes: 3 additions & 0 deletions drivers/thunderbolt/ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 35 additions & 7 deletions drivers/thunderbolt/eeprom.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
42 changes: 32 additions & 10 deletions drivers/thunderbolt/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,35 @@ 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;

if (hop->out_port > sw->config.max_port_number) {
tb_port_warn_ratelimited(port, "path goes out of non-existent 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);
Expand All @@ -54,7 +69,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;
}
Expand Down Expand Up @@ -106,7 +123,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;
Expand All @@ -129,8 +145,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);
Expand All @@ -141,7 +155,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;

Expand Down Expand Up @@ -170,18 +186,24 @@ 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) {
tb_port_warn(p, "failed to read path at %d\n", h);
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 &&
Expand Down
2 changes: 2 additions & 0 deletions drivers/thunderbolt/property.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions drivers/thunderbolt/tb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions drivers/thunderbolt/tb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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...) \
Expand All @@ -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);
Expand All @@ -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;
Expand Down
Loading
Loading