Skip to contentSkip to conversation

serial: port: Fix UPIO_PORT iotype handling

PATCH In review Andy Shevchenko 2 participants 8 messages Jan 24, 2025 – Feb 4, 2025 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

cover serial: port: Fix UPIO_PORT iotype handling

It appears that the conversion to use uart_read_and_validate_port_properties() broke 8250_of driver for the cases when UPIO_PORT is required.

Looking at the code there is an opportunity to clean up it a bit as well, hence this mini-series.

Patches 1 and 2 (and 1 is most important) are the fixes to the helper function followed by patch 3 that makes code robust against possible future changes in the same area.

The top three patches are post-fix cleanups.

This is done in a series, but can be split to fix-series + cleanup-series for the routing to the current and next Linux kernel releases. It's also possible to route all of them as fixes as they are toughly linked to each other and have not much code changes overall.

Andy Shevchenko (6):
serial: port: Assign ->iotype correctly when ->iobase is set
serial: port: Always update ->iotype in __uart_read_properties()
serial: port: Make ->iotype validation global in
__uart_read_properties()
serial: 8250_of: Remove unneeded ->iotype assignment
serial: 8250_platform: Remove unneeded ->iotype assignment
serial: 8250_pnp: Remove unneeded ->iotype assignment

 drivers/tty/serial/8250/8250_of.c       |  1 -
 drivers/tty/serial/8250/8250_platform.c |  9 ---------
 drivers/tty/serial/8250/8250_pnp.c      | 10 ----------
 drivers/tty/serial/serial_port.c        | 12 +++++++-----
 4 files changed, 7 insertions(+), 25 deletions(-)
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#1 lore raw

PATCH 1/6 serial: port: Assign ->iotype correctly when ->iobase is set

Currently the ->iotype is always assigned to the UPIO_MEM when the respective property is not found. However, this will not support the cases when user wants to have UPIO_PORT to be set or preserved. Support this scenario by checking ->iobase value and default the ->iotype respectively.

Fixes 1117a6fdc7c1 ("serial: 8250_of: Switch to use uart_read_port_properties()")
Fixes e894b6005dce ("serial: port: Introduce a common helper to read properties")
---
 drivers/tty/serial/serial_port.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/tty/serial/serial_port.c +2 -1
index d35f1d24156c..f28d0633fe6b 100644
@@ -173,6 +173,7 @@ EXPORT_SYMBOL(uart_remove_one_port);
  * The caller is responsible to initialize the following fields of the @port
  *   ->dev (must be valid)
  *   ->flags
+ *   ->iobase
  *   ->mapbase
  *   ->mapsize
  *   ->regshift (if @use_defaults is false)
@@ -214,7 +215,7 @@ static int __uart_read_properties(struct uart_port *port, bool use_defaults)
 	/* Read the registers I/O access type (default: MMIO 8-bit) */
 	ret = device_property_read_u32(dev, "reg-io-width", &value);
 	if (ret) {
-		port->iotype = UPIO_MEM;
+		port->iotype = port->iobase ? UPIO_PORT : UPIO_MEM;
 	} else {
 		switch (value) {
 		case 1:
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#2 lore raw

Re: [PATCH v1 1/6] serial: port: Assign ->iotype correctly when ->iobase is set

Quoted 8 lines · Andy Shevchenko

Currently the ->iotype is always assigned to the UPIO_MEM when the respective property is not found. However, this will not support the cases when user wants to have UPIO_PORT to be set or preserved. Support this scenario by checking ->iobase value and default the ->iotype respectively.

Fixes 1117a6fdc7c1 ("serial: 8250_of: Switch to use uart_read_port_properties()")
Fixes e894b6005dce ("serial: port: Introduce a common helper to read properties")

You forgot the cc: stable lines. I'll go add them in, but be more careful next time please.

thanks,

greg k-h

#3 lore raw

PATCH 2/6 serial: port: Always update ->iotype in __uart_read_properties()

In reply to Andy Shevchenko

The documentation of the __uart_read_properties() states that ->iotype member is always altered after the function call, but the code doesn't do that in the case when use_defaults == false and the value of reg-io-width is unsupported. Make sure the code follows the documentation.

Note, the current users of the uart_read_and_validate_port_properties() will fail and the change doesn't affect their behaviour, neither users of uart_read_port_properties() will be affected since the alteration happens there even in the current code flow.

Fixes e894b6005dce ("serial: port: Introduce a common helper to read properties")
---
 drivers/tty/serial/serial_port.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/serial_port.c +1 -1
index f28d0633fe6b..85285c56fabf 100644
@@ -228,11 +228,11 @@ static int __uart_read_properties(struct uart_port *port, bool use_defaults)
 			port->iotype = device_is_big_endian(dev) ? UPIO_MEM32BE : UPIO_MEM32;
 			break;
 		default:
+			port->iotype = UPIO_UNKNOWN;
 			if (!use_defaults) {
 				dev_err(dev, "Unsupported reg-io-width (%u)\n", value);
 				return -EINVAL;
 			}
-			port->iotype = UPIO_UNKNOWN;
 			break;
 		}
 	}
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#4 lore raw

PATCH 3/6 serial: port: Make ->iotype validation global in __uart_read_properties()

In reply to Andy Shevchenko

In order to make code robust against potential changes in the future move ->iotype validation outside of switch in __uart_read_properties(). If any code will be added in between that might leave the ->iotype value unknown the validation catches this up.

---
 drivers/tty/serial/serial_port.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/tty/serial/serial_port.c +5 -4
index 85285c56fabf..2fc48cd63f6c 100644
@@ -229,14 +229,15 @@ static int __uart_read_properties(struct uart_port *port, bool use_defaults)
 			break;
 		default:
 			port->iotype = UPIO_UNKNOWN;
-			if (!use_defaults) {
-				dev_err(dev, "Unsupported reg-io-width (%u)\n", value);
-				return -EINVAL;
-			}
 			break;
 		}
 	}
 
+	if (!use_defaults && port->iotype == UPIO_UNKNOWN) {
+		dev_err(dev, "Unsupported reg-io-width (%u)\n", value);
+		return -EINVAL;
+	}
+
 	/* Read the address mapping base offset (default: no offset) */
 	ret = device_property_read_u32(dev, "reg-offset", &value);
 	if (ret)
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#5 lore raw

PATCH 4/6 serial: 8250_of: Remove unneeded ->iotype assignment

In reply to Andy Shevchenko

If ->iobase is set the default will be UPIO_PORT for ->iotype after the uart_read_and_validate_port_properties() call. Hence no need to assign that explicitly.

---
 drivers/tty/serial/8250/8250_of.c | 1 -
 1 file changed, 1 deletion(-)
drivers/tty/serial/8250/8250_of.c +0 -1
index 64aed7efc569..11c860ea80f6 100644
@@ -110,7 +110,6 @@ static int of_platform_serial_setup(struct platform_device *ofdev,
 	spin_lock_init(&port->lock);
 
 	if (resource_type(&resource) == IORESOURCE_IO) {
-		port->iotype = UPIO_PORT;
 		port->iobase = resource.start;
 	} else {
 		port->mapbase = resource.start;
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#6 lore raw

PATCH 5/6 serial: 8250_platform: Remove unneeded ->iotype assignment

In reply to Andy Shevchenko

If ->iobase is set the default will be UPIO_PORT for ->iotype after the uart_read_and_validate_port_properties() call. Hence no need to assign that explicitly. Otherwise it will be UPIO_MEM.

---
 drivers/tty/serial/8250/8250_platform.c | 9 ---------
 1 file changed, 9 deletions(-)
drivers/tty/serial/8250/8250_platform.c +0 -9
index 348b7fa2e94d..d8cbd8d87b0e 100644
@@ -112,7 +112,6 @@ static int serial8250_probe_acpi(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	struct uart_8250_port uart = { };
 	struct resource *regs;
-	unsigned char iotype;
 	int ret, line;
 
 	regs = platform_get_mem_or_io(pdev, 0);
@@ -122,13 +121,11 @@ static int serial8250_probe_acpi(struct platform_device *pdev)
 	switch (resource_type(regs)) {
 	case IORESOURCE_IO:
 		uart.port.iobase = regs->start;
-		iotype = UPIO_PORT;
 		break;
 	case IORESOURCE_MEM:
 		uart.port.mapbase = regs->start;
 		uart.port.mapsize = resource_size(regs);
 		uart.port.flags = UPF_IOREMAP;
-		iotype = UPIO_MEM;
 		break;
 	default:
 		return -EINVAL;
@@ -147,12 +144,6 @@ static int serial8250_probe_acpi(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	/*
-	 * The previous call may not set iotype correctly when reg-io-width
-	 * property is absent and it doesn't support IO port resource.
-	 */
-	uart.port.iotype = iotype;
-
 	line = serial8250_register_8250_port(&uart);
 	if (line < 0)
 		return line;
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#7 lore raw

PATCH 6/6 serial: 8250_pnp: Remove unneeded ->iotype assignment

In reply to Andy Shevchenko

If ->iobase is set the default will be UPIO_PORT for ->iotype after the uart_read_and_validate_port_properties() call. Hence no need to assign that explicitly. Otherwise it will be UPIO_MEM.

---
 drivers/tty/serial/8250/8250_pnp.c | 10 ----------
 1 file changed, 10 deletions(-)
drivers/tty/serial/8250/8250_pnp.c +0 -10
index 7c06ae79d8e2..7a837fdf9df1 100644
@@ -436,7 +436,6 @@ serial_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
 {
 	struct uart_8250_port uart, *port;
 	int ret, flags = dev_id->driver_data;
-	unsigned char iotype;
 	long line;
 
 	if (flags & UNKNOWN_DEV) {
@@ -448,14 +447,11 @@ serial_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
 	memset(&uart, 0, sizeof(uart));
 	if ((flags & CIR_PORT) && pnp_port_valid(dev, 2)) {
 		uart.port.iobase = pnp_port_start(dev, 2);
-		iotype = UPIO_PORT;
 	} else if (pnp_port_valid(dev, 0)) {
 		uart.port.iobase = pnp_port_start(dev, 0);
-		iotype = UPIO_PORT;
 	} else if (pnp_mem_valid(dev, 0)) {
 		uart.port.mapbase = pnp_mem_start(dev, 0);
 		uart.port.mapsize = pnp_mem_len(dev, 0);
-		iotype = UPIO_MEM;
 		uart.port.flags = UPF_IOREMAP;
 	} else
 		return -ENODEV;
@@ -471,12 +467,6 @@ serial_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
 	if (ret)
 		return ret;
 
-	/*
-	 * The previous call may not set iotype correctly when reg-io-width
-	 * property is absent and it doesn't support IO port resource.
-	 */
-	uart.port.iotype = iotype;
-
 	if (flags & CIR_PORT) {
 		uart.port.flags |= UPF_FIXED_PORT | UPF_FIXED_TYPE;
 		uart.port.type = PORT_8250_CIR;
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac