A target's MRL and MWL do not account for controller transfer limits. Add max_tx_bytes and max_rx_bytes to let controllers report these limits, and helpers returning the smaller nonzero target and controller limit. The limits apply to a single message; batched transfers may still exceed controller capacity. Return 0 when neither side reports a limit, leaving callers to apply their defaults. Signed-off-by: Shubham Patil --- Changes in v3: - Rename tx_fifo_bytes/rx_fifo_bytes to max_tx_bytes/max_rx_bytes. - Shorten the commit message; no functional changes. drivers/i3c/device.c | 49 ++++++++++++++++++++++++++++++++++++++ include/linux/i3c/device.h | 3 +++ include/linux/i3c/master.h | 13 +++++++--- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/drivers/i3c/device.c b/drivers/i3c/device.c index 4df4682af150..93adf4c74004 100644 --- a/drivers/i3c/device.c +++ b/drivers/i3c/device.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -106,6 +107,54 @@ void i3c_device_get_info(const struct i3c_device *dev, } EXPORT_SYMBOL_GPL(i3c_device_get_info); +/** + * i3c_device_get_max_read_xfer_bytes() - get the maximum readable message size + * + * @dev: device the transfers would be directed to + * + * Return: the smaller of @dev's MRL and the controller's receive limit for a + * single message, or 0 if neither reports a limit. + */ +u16 i3c_device_get_max_read_xfer_bytes(const struct i3c_device *dev) +{ + u16 mrl = 0, max_rx_bytes = 0; + + i3c_bus_normaluse_lock(dev->bus); + if (dev->desc) { + mrl = dev->desc->info.max_read_len; + max_rx_bytes = i3c_dev_get_master(dev->desc)->max_rx_bytes; + } + i3c_bus_normaluse_unlock(dev->bus); + + /* 0 means "no limit reported" on either side. */ + return min_not_zero(mrl, max_rx_bytes); +} +EXPORT_SYMBOL_GPL(i3c_device_get_max_read_xfer_bytes); + +/** + * i3c_device_get_max_write_xfer_bytes() - get the maximum writable message size + * + * @dev: device the transfers would be directed to + * + * Return: the smaller of @dev's MWL and the controller's transmit limit for a + * single message, or 0 if neither reports a limit. + */ +u16 i3c_device_get_max_write_xfer_bytes(const struct i3c_device *dev) +{ + u16 mwl = 0, max_tx_bytes = 0; + + i3c_bus_normaluse_lock(dev->bus); + if (dev->desc) { + mwl = dev->desc->info.max_write_len; + max_tx_bytes = i3c_dev_get_master(dev->desc)->max_tx_bytes; + } + i3c_bus_normaluse_unlock(dev->bus); + + /* 0 means "no limit reported" on either side. */ + return min_not_zero(mwl, max_tx_bytes); +} +EXPORT_SYMBOL_GPL(i3c_device_get_max_write_xfer_bytes); + /** * i3c_device_disable_ibi() - Disable IBIs coming from a specific device * @dev: device on which IBIs should be disabled diff --git a/include/linux/i3c/device.h b/include/linux/i3c/device.h index e30ea2cb13cb..cb3374e4f9fa 100644 --- a/include/linux/i3c/device.h +++ b/include/linux/i3c/device.h @@ -331,6 +331,9 @@ int i3c_device_do_setdasa(struct i3c_device *dev); void i3c_device_get_info(const struct i3c_device *dev, struct i3c_device_info *info); +u16 i3c_device_get_max_read_xfer_bytes(const struct i3c_device *dev); +u16 i3c_device_get_max_write_xfer_bytes(const struct i3c_device *dev); + struct i3c_ibi_payload { unsigned int len; const void *data; diff --git a/include/linux/i3c/master.h b/include/linux/i3c/master.h index d58070a72a49..08a26aa9c164 100644 --- a/include/linux/i3c/master.h +++ b/include/linux/i3c/master.h @@ -551,11 +551,16 @@ struct i3c_master_controller_ops { * context (notably the resume path), and is skipped if the * controller is shutting down * @dev_nack_retry_count: retry count when slave device nack + * @max_tx_bytes: maximum number of bytes this controller can transmit in a + * single message, or 0 if it has no such limit + * @max_rx_bytes: maximum number of bytes this controller can receive in a + * single message, or 0 if it has no such limit * * A &struct i3c_master_controller has to be registered to the I3C subsystem - * through i3c_master_register(). None of &struct i3c_master_controller fields - * should be set manually, just pass appropriate values to - * i3c_master_register(). + * through i3c_master_register(). Except for @max_tx_bytes and + * @max_rx_bytes, which a controller driver may set before registering, none + * of &struct i3c_master_controller fields should be set manually, just pass + * appropriate values to i3c_master_register(). */ struct i3c_master_controller { struct device dev; @@ -580,6 +585,8 @@ struct i3c_master_controller { struct work_struct hj_work; struct work_struct reg_work; unsigned int dev_nack_retry_count; + u16 max_tx_bytes; + u16 max_rx_bytes; }; /** -- 2.34.1 The driver currently uses the transmit buffer depth to validate both transmit and receive transfers. Since the transmit and receive buffers can have different capacities, receive transfers must instead be validated against the receive buffer depth. QUEUE_SIZE_CAPABILITY reports the configured size of each buffer. Take the receive depth from there, leave the transmit and command depths on the status level registers they already use, and split caps.datafifodepth into caps.txfifodepth and caps.rxfifodepth so that each direction is checked against its own buffer. That register was already defined at the right offset, but under a name that does not appear in the databook and with no user at all. Rename EXTENDED_CAPABILITY to QUEUE_SIZE_CAPABILITY and add the field accessor. The DesignWare I2C/I3C databook, section 11.1.58 ("QUEUE_SIZE_CAPABILITY"), defines RX_BUF_SIZE in bits [7:4]. The documented encodings 0 through 5 represent 2 through 64 DWORDs, respectively, corresponding to 2U << RX_BUF_SIZE. Publish both depths in bytes through base.max_tx_bytes and base.max_rx_bytes, so that device drivers can size a transfer this controller will accept. Signed-off-by: Shubham Patil --- Changes in v3: - Use FIELD_GET() and a u32 variable for RX_BUF_SIZE extraction. - Use sizeof(u32) for byte conversion and the renamed controller fields. - Add the RX_BUF_SIZE encoding and databook reference. drivers/i3c/master/dw-i3c-master.c | 24 +++++++++++++++++------- drivers/i3c/master/dw-i3c-master.h | 3 ++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/drivers/i3c/master/dw-i3c-master.c b/drivers/i3c/master/dw-i3c-master.c index d1119f203d1b..21bd94e5a6be 100644 --- a/drivers/i3c/master/dw-i3c-master.c +++ b/drivers/i3c/master/dw-i3c-master.c @@ -203,7 +203,10 @@ #define BUS_IDLE_TIMING 0xd8 #define I3C_VER_ID 0xe0 #define I3C_VER_TYPE 0xe4 -#define EXTENDED_CAPABILITY 0xe8 +#define QUEUE_SIZE_CAPABILITY 0xe8 +#define QUEUE_SIZE_CAPABILITY_RX_BUF(x) FIELD_GET(GENMASK(7, 4), (x)) +/* Sizes are encoded, 0 meaning 2 DWORDs and every further step doubling it. */ +#define QUEUE_SIZE_TO_DWORDS(x) (2U << (x)) #define SLAVE_CONFIG 0xec #define DYN_ADDR_LO_MASK GENMASK(4, 0) @@ -978,8 +981,8 @@ static int dw_i3c_master_i3c_xfers(struct i3c_dev_desc *dev, ntxwords += DIV_ROUND_UP(i3c_xfers[i].len, 4); } - if (ntxwords > master->caps.datafifodepth || - nrxwords > master->caps.datafifodepth) + if (ntxwords > master->caps.txfifodepth || + nrxwords > master->caps.rxfifodepth) return -EOPNOTSUPP; struct dw_i3c_xfer *xfer __free(kfree) = dw_i3c_master_alloc_xfer(master, i3c_nxfers); @@ -1136,8 +1139,8 @@ static int dw_i3c_master_i2c_xfers(struct i2c_dev_desc *dev, ntxwords += DIV_ROUND_UP(i2c_xfers[i].len, 4); } - if (ntxwords > master->caps.datafifodepth || - nrxwords > master->caps.datafifodepth) + if (ntxwords > master->caps.txfifodepth || + nrxwords > master->caps.rxfifodepth) return -EOPNOTSUPP; struct dw_i3c_xfer *xfer __free(kfree) = dw_i3c_master_alloc_xfer(master, i2c_nxfers); @@ -1666,7 +1669,7 @@ int dw_i3c_common_probe(struct dw_i3c_master *master, struct platform_device *pdev) { int ret, irq; - u32 thld_ctrl; + u32 thld_ctrl, val; const struct dw_i3c_drvdata *drvdata; unsigned long quirks = 0; @@ -1738,7 +1741,14 @@ int dw_i3c_common_probe(struct dw_i3c_master *master, master->caps.cmdfifodepth = QUEUE_STATUS_LEVEL_CMD(ret); ret = readl(master->regs + DATA_BUFFER_STATUS_LEVEL); - master->caps.datafifodepth = DATA_BUFFER_STATUS_LEVEL_TX(ret); + master->caps.txfifodepth = DATA_BUFFER_STATUS_LEVEL_TX(ret); + + val = readl(master->regs + QUEUE_SIZE_CAPABILITY); + master->caps.rxfifodepth = + QUEUE_SIZE_TO_DWORDS(QUEUE_SIZE_CAPABILITY_RX_BUF(val)); + + master->base.max_tx_bytes = master->caps.txfifodepth * sizeof(u32); + master->base.max_rx_bytes = master->caps.rxfifodepth * sizeof(u32); ret = readl(master->regs + DEVICE_ADDR_TABLE_POINTER); master->datstartaddr = ret; diff --git a/drivers/i3c/master/dw-i3c-master.h b/drivers/i3c/master/dw-i3c-master.h index f5b4491b247a..e23554bfc5e4 100644 --- a/drivers/i3c/master/dw-i3c-master.h +++ b/drivers/i3c/master/dw-i3c-master.h @@ -14,7 +14,8 @@ struct dw_i3c_master_caps { u8 cmdfifodepth; - u8 datafifodepth; + u8 txfifodepth; + u8 rxfifodepth; }; struct dw_i3c_dat_entry { -- 2.34.1 mctp_i3c_setup() takes the maximum read and write lengths straight from GETMRL and GETMWL. Those describe the endpoint alone, so on a controller with a smaller limit every read can be built too large and rejected. Transmit packets can similarly reach the controller even when they are larger than its transmit buffer. The receive path also allocates an skb for data the hardware can never deliver. Use i3c_device_get_max_read_xfer_bytes() and i3c_device_get_max_write_xfer_bytes() instead, which return the smaller of the endpoint and controller limits. Behaviour is unchanged when neither reports a limit, as the helpers return 0 exactly as the GETMRL and GETMWL fields do today. Signed-off-by: Shubham Patil --- Changes in v3: - Change the subject prefix to "net: mctp i3c:". - No code changes. drivers/net/mctp/mctp-i3c.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/net/mctp/mctp-i3c.c b/drivers/net/mctp/mctp-i3c.c index 88d9e36cd4a2..6d08898e1d21 100644 --- a/drivers/net/mctp/mctp-i3c.c +++ b/drivers/net/mctp/mctp-i3c.c @@ -81,9 +81,9 @@ struct mctp_i3c_device { bool have_mdb; /* I3C dynamic address */ u8 addr; - /* Maximum read length */ + /* Maximum read length, limited by the device MRL and the controller */ u16 mrl; - /* Maximum write length */ + /* Maximum write length, limited by the device MWL and the controller */ u16 mwl; /* Provisioned ID */ u64 pid; @@ -214,8 +214,8 @@ static int mctp_i3c_setup(struct mctp_i3c_device *mi) i3c_device_get_info(mi->i3c, &info); mi->have_mdb = info.bcr & BIT(2); mi->addr = info.dyn_addr; - mi->mwl = info.max_write_len; - mi->mrl = info.max_read_len; + mi->mwl = i3c_device_get_max_write_xfer_bytes(mi->i3c); + mi->mrl = i3c_device_get_max_read_xfer_bytes(mi->i3c); mi->pid = info.pid; rc = i3c_device_request_ibi(mi->i3c, &ibi); -- 2.34.1