Commit e954af13 authored by Eddie James's avatar Eddie James Committed by Mark Brown

spi: fsi: Fix contention in the FSI2SPI engine

There was nothing to protect multiple SPI controllers on the same FSI2SPI
device from being accessed through the FSI2SPI device at the same time.
For example, multiple writes to the command and data registers might occur
for different SPI controllers, resulting in complete chaos in the SPI
engine. To prevent this, add a FSI2SPI device level mutex and lock it in
the SPI register read and write functions.

Fixes: bbb6b2f9 ("spi: Add FSI-attached SPI controller driver")
Signed-off-by: default avatarEddie James <eajames@linux.ibm.com>
Link: https://lore.kernel.org/r/20211026193327.52420-1-eajames@linux.ibm.comSigned-off-by: default avatarMark Brown <broonie@kernel.org>
parent 0b0a281e
...@@ -67,9 +67,14 @@ ...@@ -67,9 +67,14 @@
SPI_FSI_STATUS_RDR_OVERRUN) SPI_FSI_STATUS_RDR_OVERRUN)
#define SPI_FSI_PORT_CTRL 0x9 #define SPI_FSI_PORT_CTRL 0x9
struct fsi2spi {
struct fsi_device *fsi; /* FSI2SPI CFAM engine device */
struct mutex lock; /* lock access to the device */
};
struct fsi_spi { struct fsi_spi {
struct device *dev; /* SPI controller device */ struct device *dev; /* SPI controller device */
struct fsi_device *fsi; /* FSI2SPI CFAM engine device */ struct fsi2spi *bridge; /* FSI2SPI device */
u32 base; u32 base;
}; };
...@@ -104,7 +109,7 @@ static int fsi_spi_check_status(struct fsi_spi *ctx) ...@@ -104,7 +109,7 @@ static int fsi_spi_check_status(struct fsi_spi *ctx)
u32 sts; u32 sts;
__be32 sts_be; __be32 sts_be;
rc = fsi_device_read(ctx->fsi, FSI2SPI_STATUS, &sts_be, rc = fsi_device_read(ctx->bridge->fsi, FSI2SPI_STATUS, &sts_be,
sizeof(sts_be)); sizeof(sts_be));
if (rc) if (rc)
return rc; return rc;
...@@ -120,73 +125,91 @@ static int fsi_spi_check_status(struct fsi_spi *ctx) ...@@ -120,73 +125,91 @@ static int fsi_spi_check_status(struct fsi_spi *ctx)
static int fsi_spi_read_reg(struct fsi_spi *ctx, u32 offset, u64 *value) static int fsi_spi_read_reg(struct fsi_spi *ctx, u32 offset, u64 *value)
{ {
int rc; int rc = 0;
__be32 cmd_be; __be32 cmd_be;
__be32 data_be; __be32 data_be;
u32 cmd = offset + ctx->base; u32 cmd = offset + ctx->base;
struct fsi2spi *bridge = ctx->bridge;
*value = 0ULL; *value = 0ULL;
if (cmd & FSI2SPI_CMD_WRITE) if (cmd & FSI2SPI_CMD_WRITE)
return -EINVAL; return -EINVAL;
cmd_be = cpu_to_be32(cmd); rc = mutex_lock_interruptible(&bridge->lock);
rc = fsi_device_write(ctx->fsi, FSI2SPI_CMD, &cmd_be, sizeof(cmd_be));
if (rc) if (rc)
return rc; return rc;
cmd_be = cpu_to_be32(cmd);
rc = fsi_device_write(bridge->fsi, FSI2SPI_CMD, &cmd_be,
sizeof(cmd_be));
if (rc)
goto unlock;
rc = fsi_spi_check_status(ctx); rc = fsi_spi_check_status(ctx);
if (rc) if (rc)
return rc; goto unlock;
rc = fsi_device_read(ctx->fsi, FSI2SPI_DATA0, &data_be, rc = fsi_device_read(bridge->fsi, FSI2SPI_DATA0, &data_be,
sizeof(data_be)); sizeof(data_be));
if (rc) if (rc)
return rc; goto unlock;
*value |= (u64)be32_to_cpu(data_be) << 32; *value |= (u64)be32_to_cpu(data_be) << 32;
rc = fsi_device_read(ctx->fsi, FSI2SPI_DATA1, &data_be, rc = fsi_device_read(bridge->fsi, FSI2SPI_DATA1, &data_be,
sizeof(data_be)); sizeof(data_be));
if (rc) if (rc)
return rc; goto unlock;
*value |= (u64)be32_to_cpu(data_be); *value |= (u64)be32_to_cpu(data_be);
dev_dbg(ctx->dev, "Read %02x[%016llx].\n", offset, *value); dev_dbg(ctx->dev, "Read %02x[%016llx].\n", offset, *value);
return 0; unlock:
mutex_unlock(&bridge->lock);
return rc;
} }
static int fsi_spi_write_reg(struct fsi_spi *ctx, u32 offset, u64 value) static int fsi_spi_write_reg(struct fsi_spi *ctx, u32 offset, u64 value)
{ {
int rc; int rc = 0;
__be32 cmd_be; __be32 cmd_be;
__be32 data_be; __be32 data_be;
u32 cmd = offset + ctx->base; u32 cmd = offset + ctx->base;
struct fsi2spi *bridge = ctx->bridge;
if (cmd & FSI2SPI_CMD_WRITE) if (cmd & FSI2SPI_CMD_WRITE)
return -EINVAL; return -EINVAL;
rc = mutex_lock_interruptible(&bridge->lock);
if (rc)
return rc;
dev_dbg(ctx->dev, "Write %02x[%016llx].\n", offset, value); dev_dbg(ctx->dev, "Write %02x[%016llx].\n", offset, value);
data_be = cpu_to_be32(upper_32_bits(value)); data_be = cpu_to_be32(upper_32_bits(value));
rc = fsi_device_write(ctx->fsi, FSI2SPI_DATA0, &data_be, rc = fsi_device_write(bridge->fsi, FSI2SPI_DATA0, &data_be,
sizeof(data_be)); sizeof(data_be));
if (rc) if (rc)
return rc; goto unlock;
data_be = cpu_to_be32(lower_32_bits(value)); data_be = cpu_to_be32(lower_32_bits(value));
rc = fsi_device_write(ctx->fsi, FSI2SPI_DATA1, &data_be, rc = fsi_device_write(bridge->fsi, FSI2SPI_DATA1, &data_be,
sizeof(data_be)); sizeof(data_be));
if (rc) if (rc)
return rc; goto unlock;
cmd_be = cpu_to_be32(cmd | FSI2SPI_CMD_WRITE); cmd_be = cpu_to_be32(cmd | FSI2SPI_CMD_WRITE);
rc = fsi_device_write(ctx->fsi, FSI2SPI_CMD, &cmd_be, sizeof(cmd_be)); rc = fsi_device_write(bridge->fsi, FSI2SPI_CMD, &cmd_be,
sizeof(cmd_be));
if (rc) if (rc)
return rc; goto unlock;
rc = fsi_spi_check_status(ctx);
return fsi_spi_check_status(ctx); unlock:
mutex_unlock(&bridge->lock);
return rc;
} }
static int fsi_spi_data_in(u64 in, u8 *rx, int len) static int fsi_spi_data_in(u64 in, u8 *rx, int len)
...@@ -242,7 +265,7 @@ static int fsi_spi_status(struct fsi_spi *ctx, u64 *status, const char *dir) ...@@ -242,7 +265,7 @@ static int fsi_spi_status(struct fsi_spi *ctx, u64 *status, const char *dir)
return rc; return rc;
if (*status & SPI_FSI_STATUS_ANY_ERROR) { if (*status & SPI_FSI_STATUS_ANY_ERROR) {
dev_err(ctx->dev, "%s error: %08llx\n", dir, *status); dev_err(ctx->dev, "%s error: %016llx\n", dir, *status);
rc = fsi_spi_reset(ctx); rc = fsi_spi_reset(ctx);
if (rc) if (rc)
...@@ -394,7 +417,7 @@ static int fsi_spi_transfer_one_message(struct spi_controller *ctlr, ...@@ -394,7 +417,7 @@ static int fsi_spi_transfer_one_message(struct spi_controller *ctlr,
struct spi_transfer *transfer; struct spi_transfer *transfer;
struct fsi_spi *ctx = spi_controller_get_devdata(ctlr); struct fsi_spi *ctx = spi_controller_get_devdata(ctlr);
rc = fsi_spi_check_mux(ctx->fsi, ctx->dev); rc = fsi_spi_check_mux(ctx->bridge->fsi, ctx->dev);
if (rc) if (rc)
goto error; goto error;
...@@ -484,12 +507,20 @@ static int fsi_spi_probe(struct device *dev) ...@@ -484,12 +507,20 @@ static int fsi_spi_probe(struct device *dev)
int rc; int rc;
struct device_node *np; struct device_node *np;
int num_controllers_registered = 0; int num_controllers_registered = 0;
struct fsi2spi *bridge;
struct fsi_device *fsi = to_fsi_dev(dev); struct fsi_device *fsi = to_fsi_dev(dev);
rc = fsi_spi_check_mux(fsi, dev); rc = fsi_spi_check_mux(fsi, dev);
if (rc) if (rc)
return -ENODEV; return -ENODEV;
bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
if (!bridge)
return -ENOMEM;
bridge->fsi = fsi;
mutex_init(&bridge->lock);
for_each_available_child_of_node(dev->of_node, np) { for_each_available_child_of_node(dev->of_node, np) {
u32 base; u32 base;
struct fsi_spi *ctx; struct fsi_spi *ctx;
...@@ -512,7 +543,7 @@ static int fsi_spi_probe(struct device *dev) ...@@ -512,7 +543,7 @@ static int fsi_spi_probe(struct device *dev)
ctx = spi_controller_get_devdata(ctlr); ctx = spi_controller_get_devdata(ctlr);
ctx->dev = &ctlr->dev; ctx->dev = &ctlr->dev;
ctx->fsi = fsi; ctx->bridge = bridge;
ctx->base = base + SPI_FSI_BASE; ctx->base = base + SPI_FSI_BASE;
rc = devm_spi_register_controller(dev, ctlr); rc = devm_spi_register_controller(dev, ctlr);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment