Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: dsa: microchip: properly support platform_data probing

The ksz driver has bits and pieces of platform_data probing support, but
it doesn't work.

The conventional thing to do is to have an encapsulating structure for
struct dsa_chip_data that gets put into dev->platform_data. This driver
expects a struct ksz_platform_data, but that doesn't contain a struct
dsa_chip_data as first element, which will obviously not work with
dsa_switch_probe() -> dsa_switch_parse().

Pointing dev->platform_data to a struct dsa_chip_data directly is in
principle possible, but that doesn't work either. The driver has
ksz_switch_detect() to read the device ID from hardware, followed by
ksz_check_device_id() to compare it against a predetermined expected
value. This protects against early errors in the SPI/I2C communication.
With platform_data, the mechanism in ksz_check_device_id() doesn't work
and even leads to NULL pointer dereferences, since of_device_get_match_data()
doesn't work in that probe path.

So obviously, the platform_data support is actually missing, and the
existing handling of struct ksz_platform_data is bogus. Complete the
support by adding a struct dsa_chip_data as first element, and fixing up
ksz_check_device_id() to pick up the platform_data instead of the
unavailable of_device_get_match_data().

The early dev->chip_id assignment from ksz_switch_register() is also
bogus, because ksz_switch_detect() sets it to an initial value. So
remove it.

Also, ksz_platform_data :: enabled_ports isn't used anywhere, delete it.

Link: https://lore.kernel.org/netdev/20231204154315.3906267-1-dd@embedd.com/
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Signed-off-by: Daniel Danzberger <dd@embedd.com>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Vladimir Oltean and committed by
David S. Miller
3bc05faf d5449d59

+16 -9
+13 -8
drivers/net/dsa/microchip/ksz_common.c
··· 1673 1673 1674 1674 static int ksz_check_device_id(struct ksz_device *dev) 1675 1675 { 1676 - const struct ksz_chip_data *dt_chip_data; 1676 + const struct ksz_chip_data *expected_chip_data; 1677 + u32 expected_chip_id; 1677 1678 1678 - dt_chip_data = of_device_get_match_data(dev->dev); 1679 + if (dev->pdata) { 1680 + expected_chip_id = dev->pdata->chip_id; 1681 + expected_chip_data = ksz_lookup_info(expected_chip_id); 1682 + if (WARN_ON(!expected_chip_data)) 1683 + return -ENODEV; 1684 + } else { 1685 + expected_chip_data = of_device_get_match_data(dev->dev); 1686 + expected_chip_id = expected_chip_data->chip_id; 1687 + } 1679 1688 1680 - /* Check for Device Tree and Chip ID */ 1681 - if (dt_chip_data->chip_id != dev->chip_id) { 1689 + if (expected_chip_id != dev->chip_id) { 1682 1690 dev_err(dev->dev, 1683 1691 "Device tree specifies chip %s but found %s, please fix it!\n", 1684 - dt_chip_data->dev_name, dev->info->dev_name); 1692 + expected_chip_data->dev_name, dev->info->dev_name); 1685 1693 return -ENODEV; 1686 1694 } 1687 1695 ··· 4163 4155 unsigned int port_num; 4164 4156 int ret; 4165 4157 int i; 4166 - 4167 - if (dev->pdata) 4168 - dev->chip_id = dev->pdata->chip_id; 4169 4158 4170 4159 dev->reset_gpio = devm_gpiod_get_optional(dev->dev, "reset", 4171 4160 GPIOD_OUT_LOW);
+3 -1
include/linux/platform_data/microchip-ksz.h
··· 20 20 #define __MICROCHIP_KSZ_H 21 21 22 22 #include <linux/types.h> 23 + #include <linux/platform_data/dsa.h> 23 24 24 25 struct ksz_platform_data { 26 + /* Must be first such that dsa_register_switch() can access it */ 27 + struct dsa_chip_data cd; 25 28 u32 chip_id; 26 - u16 enabled_ports; 27 29 }; 28 30 29 31 #endif