Re: [PATCH v2 3/4] serial: sc16is7xx: split into core and I2C/SPI parts (sc16is7xx_lines)

From: Hugo Villeneuve
Date: Wed Mar 13 2024 - 10:38:14 EST


On Thu, 7 Mar 2024 11:18:27 -0500
Hugo Villeneuve <hugo@xxxxxxxxxxx> wrote:

> From: Hugo Villeneuve <hvilleneuve@xxxxxxxxxxxx>
>
> Before, sc16is7xx_lines was checked for a free (zero) bit first, and then
> later it was set only if UART port registration succeeded.
>
> Now that sc16is7xx_lines is shared for the I2C and SPI drivers, make sure
> it is reserved and modified atomically, and use a new variable to hold the
> status of UART port regisration.
>
> Remove need to check for previous port registration in sc16is7xx_remove(),
> because if sc16is7xx_probe() succeeded, we are guaranteed to have
> successfully registered both ports.
>
> Signed-off-by: Hugo Villeneuve <hvilleneuve@xxxxxxxxxxxx>
> ---
> drivers/tty/serial/sc16is7xx.c | 22 +++++++++++++++++-----
> 1 file changed, 17 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/tty/serial/sc16is7xx.c b/drivers/tty/serial/sc16is7xx.c
> index 5b53c88b7133..d81aad1b201d 100644
> --- a/drivers/tty/serial/sc16is7xx.c
> +++ b/drivers/tty/serial/sc16is7xx.c
> @@ -343,6 +343,8 @@ struct sc16is7xx_port {
>
> static DECLARE_BITMAP(sc16is7xx_lines, SC16IS7XX_MAX_DEVS);
>
> +static DEFINE_MUTEX(sc16is7xx_lines_lock); /* For probe atomic line reservation. */
> +
> static struct uart_driver sc16is7xx_uart = {
> .owner = THIS_MODULE,
> .driver_name = SC16IS7XX_NAME,
> @@ -1468,6 +1470,7 @@ int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
> u32 uartclk = 0;
> int i, ret;
> struct sc16is7xx_port *s;
> + bool port_registered[SC16IS7XX_MAX_PORTS];
>
> for (i = 0; i < devtype->nr_uart; i++)
> if (IS_ERR(regmaps[i]))
> @@ -1532,14 +1535,21 @@ int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
> regmap_write(regmaps[0], SC16IS7XX_IOCONTROL_REG,
> SC16IS7XX_IOCONTROL_SRESET_BIT);
>
> + memset(port_registered, 0, sizeof(port_registered));
> +
> for (i = 0; i < devtype->nr_uart; ++i) {
> + mutex_lock(&sc16is7xx_lines_lock);
> s->p[i].port.line = find_first_zero_bit(sc16is7xx_lines,
> SC16IS7XX_MAX_DEVS);
> if (s->p[i].port.line >= SC16IS7XX_MAX_DEVS) {
> ret = -ERANGE;
> + mutex_unlock(&sc16is7xx_lines_lock);
> goto out_ports;
> }
>
> + set_bit(s->p[i].port.line, sc16is7xx_lines);
> + mutex_unlock(&sc16is7xx_lines_lock);
> +
> /* Initialize port data */
> s->p[i].port.dev = dev;
> s->p[i].port.irq = irq;
> @@ -1584,7 +1594,7 @@ int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
> if (ret)
> goto out_ports;
>
> - set_bit(s->p[i].port.line, sc16is7xx_lines);
> + port_registered[i] = true;
>
> /* Enable EFR */
> sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_LCR_REG,
> @@ -1642,9 +1652,11 @@ int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
> #endif
>
> out_ports:
> - for (i = 0; i < devtype->nr_uart; i++)
> - if (test_and_clear_bit(s->p[i].port.line, sc16is7xx_lines))
> + for (i = 0; i < devtype->nr_uart; i++) {
> + clear_bit(s->p[i].port.line, sc16is7xx_lines);
> + if (port_registered[i])

Hi Andy,
I just realised that since we no longer need to search if a
bit is set, it is possible to simplify sc16is7xx_lines allocation
by using the IDA framework, as you suggested a few months ago.

I will send a V3 with this change.

Hugo Villeneuve


> uart_remove_one_port(&sc16is7xx_uart, &s->p[i].port);
> + }
>
> kthread_stop(s->kworker_task);
>
> @@ -1667,8 +1679,8 @@ void sc16is7xx_remove(struct device *dev)
>
> for (i = 0; i < s->devtype->nr_uart; i++) {
> kthread_cancel_delayed_work_sync(&s->p[i].ms_work);
> - if (test_and_clear_bit(s->p[i].port.line, sc16is7xx_lines))
> - uart_remove_one_port(&sc16is7xx_uart, &s->p[i].port);
> + clear_bit(s->p[i].port.line, sc16is7xx_lines);
> + uart_remove_one_port(&sc16is7xx_uart, &s->p[i].port);
> sc16is7xx_power(&s->p[i].port, 0);
> }
>
> --
> 2.39.2
>
>