Skip to contentSkip to conversation

i3c: Remove the const qualifier from i2c_msg pointer in i2c_xfers API

PATCH Applied Billy Tsai 7 participants 12 messages Feb 4, 2025 – Feb 20, 2025

Subsystems (4)
lore
Share & tools
lore

Billy Tsai

author
#0 lore raw

PATCH v2 1/2 i3c: Remove the const qualifier from i2c_msg pointer in i2c_xfers API

The change is necessary to enable the use of the i2c_get_dma_safe_msg_buf() API, which requires a non-const struct i2c_msg * to operate. The i2c_get_dma_safe_msg_buf() function ensures safe handling of I2C messages when using DMA, making it essential for scenarios where DMA transfers are involved. By removing the const qualifier, this patch allows drivers to prepare and manage DMA-safe buffers directly.

---
 drivers/i3c/master/dw-i3c-master.c     | 2 +-
 drivers/i3c/master/i3c-master-cdns.c   | 2 +-
 drivers/i3c/master/mipi-i3c-hci/core.c | 2 +-
 drivers/i3c/master/svc-i3c-master.c    | 2 +-
 include/linux/i3c/master.h             | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)
drivers/i3c/master/dw-i3c-master.c +1 -1
index d09ea5b6467c..54c7d86997d5 100644
@@ -1079,7 +1079,7 @@ static void dw_i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev)
 }
 
 static int dw_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				   const struct i2c_msg *i2c_xfers,
+				   struct i2c_msg *i2c_xfers,
 				   int i2c_nxfers)
 {
 	struct dw_i3c_i2c_dev_data *data = i2c_dev_get_master_data(dev);
drivers/i3c/master/i3c-master-cdns.c +1 -1
index 8d69a34986d9..c9e4c129454c 100644
@@ -813,7 +813,7 @@ static int cdns_i3c_master_priv_xfers(struct i3c_dev_desc *dev,
 }
 
 static int cdns_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				     const struct i2c_msg *xfers, int nxfers)
+				     struct i2c_msg *xfers, int nxfers)
 {
 	struct i3c_master_controller *m = i2c_dev_get_master(dev);
 	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
drivers/i3c/master/mipi-i3c-hci/core.c +1 -1
index e6e482a259b4..a408feac3e9e 100644
@@ -367,7 +367,7 @@ static int i3c_hci_priv_xfers(struct i3c_dev_desc *dev,
 }
 
 static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
-			     const struct i2c_msg *i2c_xfers, int nxfers)
+			     struct i2c_msg *i2c_xfers, int nxfers)
 {
 	struct i3c_master_controller *m = i2c_dev_get_master(dev);
 	struct i3c_hci *hci = to_i3c_hci(m);
drivers/i3c/master/svc-i3c-master.c +1 -1
index c1ee3828e7ee..24bd701b5de0 100644
@@ -1584,7 +1584,7 @@ static int svc_i3c_master_priv_xfers(struct i3c_dev_desc *dev,
 }
 
 static int svc_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				    const struct i2c_msg *xfers,
+				    struct i2c_msg *xfers,
 				    int nxfers)
 {
 	struct i3c_master_controller *m = i2c_dev_get_master(dev);
include/linux/i3c/master.h +1 -1
index 12d532b012c5..c67922ece617 100644
@@ -475,7 +475,7 @@ struct i3c_master_controller_ops {
 	int (*attach_i2c_dev)(struct i2c_dev_desc *dev);
 	void (*detach_i2c_dev)(struct i2c_dev_desc *dev);
 	int (*i2c_xfers)(struct i2c_dev_desc *dev,
-			 const struct i2c_msg *xfers, int nxfers);
+			 struct i2c_msg *xfers, int nxfers);
 	int (*request_ibi)(struct i3c_dev_desc *dev,
 			   const struct i3c_ibi_setup *req);
 	void (*free_ibi)(struct i3c_dev_desc *dev);
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 2/2 i3c: mipi-i3c-hci: Use I2C DMA-safe api

Use the i2c_get/put_dma_safe_msg_buf for I2C transfers instead of using the I3C-specific API.

---
 drivers/i3c/master/mipi-i3c-hci/core.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/i3c/master/mipi-i3c-hci/core.c +3 -5
index a408feac3e9e..4f3738beb0f2 100644
@@ -382,14 +382,11 @@ static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
 		return -ENOMEM;
 
 	for (i = 0; i < nxfers; i++) {
-		xfer[i].data = i2c_xfers[i].buf;
+		xfer[i].data = i2c_get_dma_safe_msg_buf(&i2c_xfers[i], 1);
 		xfer[i].data_len = i2c_xfers[i].len;
 		xfer[i].rnw = i2c_xfers[i].flags & I2C_M_RD;
 		hci->cmd->prep_i2c_xfer(hci, dev, &xfer[i]);
 		xfer[i].cmd_desc[0] |= CMD_0_ROC;
-		ret = i3c_hci_alloc_safe_xfer_buf(hci, &xfer[i]);
-		if (ret)
-			goto out;
 	}
 	last = i - 1;
 	xfer[last].cmd_desc[0] |= CMD_0_TOC;
@@ -412,7 +409,8 @@ static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
 
 out:
 	for (i = 0; i < nxfers; i++)
-		i3c_hci_free_safe_xfer_buf(hci, &xfer[i]);
+		i2c_put_dma_safe_msg_buf(xfer[i].data, &i2c_xfers[i],
+					 ret ? false : true);
 
 	hci_free_xfer(xfer, nxfers);
 	return ret;
Signature · 1 line
2.25.1
Acked-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] i3c: mipi-i3c-hci: Use I2C DMA-safe api

Looks good to me !

Quoted 4 lines · Billy Tsai

Use the i2c_get/put_dma_safe_msg_buf for I2C transfers instead of using the I3C-specific API.

Quoted 34 lines
---
  drivers/i3c/master/mipi-i3c-hci/core.c | 8 +++-----
  1 file changed, 3 insertions(+), 5 deletions(-)
drivers/i3c/master/mipi-i3c-hci/core.c +3 -5
index a408feac3e9e..4f3738beb0f2 100644
@@ -382,14 +382,11 @@ static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
  		return -ENOMEM;
 
  	for (i = 0; i < nxfers; i++) {
-		xfer[i].data = i2c_xfers[i].buf;
+		xfer[i].data = i2c_get_dma_safe_msg_buf(&i2c_xfers[i], 1);
  		xfer[i].data_len = i2c_xfers[i].len;
  		xfer[i].rnw = i2c_xfers[i].flags & I2C_M_RD;
  		hci->cmd->prep_i2c_xfer(hci, dev, &xfer[i]);
  		xfer[i].cmd_desc[0] |= CMD_0_ROC;
-		ret = i3c_hci_alloc_safe_xfer_buf(hci, &xfer[i]);
-		if (ret)
-			goto out;
  	}
  	last = i - 1;
  	xfer[last].cmd_desc[0] |= CMD_0_TOC;
@@ -412,7 +409,8 @@ static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
 
  out:
  	for (i = 0; i < nxfers; i++)
-		i3c_hci_free_safe_xfer_buf(hci, &xfer[i]);
+		i2c_put_dma_safe_msg_buf(xfer[i].data, &i2c_xfers[i],
+					 ret ? false : true);
 
  	hci_free_xfer(xfer, nxfers);
  	return ret;
Signature · 3 lines
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
maintainer
#4 lore raw

Re: [PATCH v2 2/2] i3c: mipi-i3c-hci: Use I2C DMA-safe api

In reply to Billy Tsai
Quoted 13 lines · Billy Tsai

Use the i2c_get/put_dma_safe_msg_buf for I2C transfers instead of using the I3C-specific API.

---
 drivers/i3c/master/mipi-i3c-hci/core.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/i3c/master/mipi-i3c-hci/core.c +0 -0
index a408feac3e9e..4f3738beb0f2 100644

drivers/i3c/master/mipi-i3c-hci/core.c static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev, · line 385

Quoted 5 lines
drivers/i3c/master/mipi-i3c-hci/core.c +1 -1
@@ -382,14 +382,11 @@ static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
 		return -ENOMEM;
 
 	for (i = 0; i < nxfers; i++) {
-		xfer[i].data = i2c_xfers[i].buf;
+		xfer[i].data = i2c_get_dma_safe_msg_buf(&i2c_xfers[i], 1);

1 should true?

Frank

Quoted 28 lines
drivers/i3c/master/mipi-i3c-hci/core.c +2 -5
 		xfer[i].data_len = i2c_xfers[i].len;
 		xfer[i].rnw = i2c_xfers[i].flags & I2C_M_RD;
 		hci->cmd->prep_i2c_xfer(hci, dev, &xfer[i]);
 		xfer[i].cmd_desc[0] |= CMD_0_ROC;
-		ret = i3c_hci_alloc_safe_xfer_buf(hci, &xfer[i]);
-		if (ret)
-			goto out;
 	}
 	last = i - 1;
 	xfer[last].cmd_desc[0] |= CMD_0_TOC;
@@ -412,7 +409,8 @@ static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
 
 out:
 	for (i = 0; i < nxfers; i++)
-		i3c_hci_free_safe_xfer_buf(hci, &xfer[i]);
+		i2c_put_dma_safe_msg_buf(xfer[i].data, &i2c_xfers[i],
+					 ret ? false : true);
 
 	hci_free_xfer(xfer, nxfers);
 	return ret;
--

2.25.1

--
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c

#5 lore raw
In reply to Billy Tsai

Hi Billy,

Quoted 7 lines · Billy Tsai

The change is necessary to enable the use of the i2c_get_dma_safe_msg_buf() API, which requires a non-const struct i2c_msg * to operate. The i2c_get_dma_safe_msg_buf() function ensures safe handling of I2C messages when using DMA, making it essential for scenarios where DMA transfers are involved. By removing the const qualifier, this patch allows drivers to prepare and manage DMA-safe buffers directly.

This is missing a changelog to v1 of the series.

Also I asked before why it is not possible to change the signature of i2c_get_dma_safe_msg_buf() to accept 'const struct i2c_msg *' [0]. That looks like the nicer solution to me.

Quoted 8 lines
---
 drivers/i3c/master/dw-i3c-master.c     | 2 +-
 drivers/i3c/master/i3c-master-cdns.c   | 2 +-
 drivers/i3c/master/mipi-i3c-hci/core.c | 2 +-
 drivers/i3c/master/svc-i3c-master.c    | 2 +-
 include/linux/i3c/master.h             | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)

<snip>

[0] https://lore.kernel.org/lkml/[email protected]/

author
#6 lore raw
Quoted 9 lines · Billy Tsai
Quoted 7 lines

The change is necessary to enable the use of the i2c_get_dma_safe_msg_buf() API, which requires a non-const struct i2c_msg * to operate. The i2c_get_dma_safe_msg_buf() function ensures safe handling of I2C messages when using DMA, making it essential for scenarios where DMA transfers are involved. By removing the const qualifier, this patch allows drivers to prepare and manage DMA-safe buffers directly.

Quoted 5 lines

This is missing a changelog to v1 of the series.

Also I asked before why it is not possible to change the signature of i2c_get_dma_safe_msg_buf() to accept 'const struct i2c_msg *' [0]. That looks like the nicer solution to me.

Hi Thomas,

The i2c_get_dma_safe_msg_buf() function has existed for a long time, but I do not know the original reason why it declares struct i2c_msg * without const. However, I believe this is because the i2c_put_dma_safe_msg_buf() function modifies the buffer data, so for consistency, it is declared without const.

Best regards,
Billy Tsai.

#7 lore raw

Hi Billy,

Quoted 11 lines · Billy Tsai
Quoted 10 lines · Billy Tsai
Quoted 7 lines

The change is necessary to enable the use of the i2c_get_dma_safe_msg_buf() API, which requires a non-const struct i2c_msg * to operate. The i2c_get_dma_safe_msg_buf() function ensures safe handling of I2C messages when using DMA, making it essential for scenarios where DMA transfers are involved. By removing the const qualifier, this patch allows drivers to prepare and manage DMA-safe buffers directly.

This is missing a changelog to v1 of the series.

Quoted 7 lines
Quoted 3 lines

Also I asked before why it is not possible to change the signature of i2c_get_dma_safe_msg_buf() to accept 'const struct i2c_msg *' [0]. That looks like the nicer solution to me.

The i2c_get_dma_safe_msg_buf() function has existed for a long time, but I do not know the original reason why it declares struct i2c_msg * without const.

I would guess that nobody ever cared before.

Quoted 3 lines

However, I believe this is because the i2c_put_dma_safe_msg_buf() function modifies the buffer data, so for consistency, it is declared without const.

IMO that's a weak argument.
Maybe the maintainers have a preference?

Thomas

#8 lore raw
Quoted 22 lines · Thomas Weißschuh

Hi Billy,

Quoted 11 lines · Billy Tsai
Quoted 10 lines · Billy Tsai
Quoted 7 lines

The change is necessary to enable the use of the i2c_get_dma_safe_msg_buf() API, which requires a non-const struct i2c_msg * to operate. The i2c_get_dma_safe_msg_buf() function ensures safe handling of I2C messages when using DMA, making it essential for scenarios where DMA transfers are involved. By removing the const qualifier, this patch allows drivers to prepare and manage DMA-safe buffers directly.

This is missing a changelog to v1 of the series.

Quoted 7 lines
Quoted 3 lines

Also I asked before why it is not possible to change the signature of i2c_get_dma_safe_msg_buf() to accept 'const struct i2c_msg *' [0]. That looks like the nicer solution to me.

The i2c_get_dma_safe_msg_buf() function has existed for a long time, but I do not know the original reason why it declares struct i2c_msg * without const.

Quoted 9 lines

I would guess that nobody ever cared before.

Quoted 3 lines

However, I believe this is because the i2c_put_dma_safe_msg_buf() function modifies the buffer data, so for consistency, it is declared without const.

IMO that's a weak argument.
Maybe the maintainers have a preference?

I guess Wolfram will be the one have the preference, I'm fine using the existing API as-is.

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#9 lore raw
Quoted 3 lines

I guess Wolfram will be the one have the preference, I'm fine using the existing API as-is.

Happy to take (tested) patches.

maintainer Reviewed-by on v2 1/2
#10 lore raw
In reply to Billy Tsai
Quoted 10 lines · Billy Tsai

The change is necessary to enable the use of the i2c_get_dma_safe_msg_buf() API, which requires a non-const struct i2c_msg * to operate. The i2c_get_dma_safe_msg_buf() function ensures safe handling of I2C messages when using DMA, making it essential for scenarios where DMA transfers are involved. By removing the const qualifier, this patch allows drivers to prepare and manage DMA-safe buffers directly.

---

Quoted 80 lines
 drivers/i3c/master/dw-i3c-master.c     | 2 +-
 drivers/i3c/master/i3c-master-cdns.c   | 2 +-
 drivers/i3c/master/mipi-i3c-hci/core.c | 2 +-
 drivers/i3c/master/svc-i3c-master.c    | 2 +-
 include/linux/i3c/master.h             | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)
drivers/i3c/master/dw-i3c-master.c +1 -1
index d09ea5b6467c..54c7d86997d5 100644
@@ -1079,7 +1079,7 @@ static void dw_i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev)
 }
 
 static int dw_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				   const struct i2c_msg *i2c_xfers,
+				   struct i2c_msg *i2c_xfers,
 				   int i2c_nxfers)
 {
 	struct dw_i3c_i2c_dev_data *data = i2c_dev_get_master_data(dev);
drivers/i3c/master/i3c-master-cdns.c +1 -1
index 8d69a34986d9..c9e4c129454c 100644
@@ -813,7 +813,7 @@ static int cdns_i3c_master_priv_xfers(struct i3c_dev_desc *dev,
 }
 
 static int cdns_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				     const struct i2c_msg *xfers, int nxfers)
+				     struct i2c_msg *xfers, int nxfers)
 {
 	struct i3c_master_controller *m = i2c_dev_get_master(dev);
 	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
drivers/i3c/master/mipi-i3c-hci/core.c +1 -1
index e6e482a259b4..a408feac3e9e 100644
@@ -367,7 +367,7 @@ static int i3c_hci_priv_xfers(struct i3c_dev_desc *dev,
 }
 
 static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
-			     const struct i2c_msg *i2c_xfers, int nxfers)
+			     struct i2c_msg *i2c_xfers, int nxfers)
 {
 	struct i3c_master_controller *m = i2c_dev_get_master(dev);
 	struct i3c_hci *hci = to_i3c_hci(m);
drivers/i3c/master/svc-i3c-master.c +1 -1
index c1ee3828e7ee..24bd701b5de0 100644
@@ -1584,7 +1584,7 @@ static int svc_i3c_master_priv_xfers(struct i3c_dev_desc *dev,
 }
 
 static int svc_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				    const struct i2c_msg *xfers,
+				    struct i2c_msg *xfers,
 				    int nxfers)
 {
 	struct i3c_master_controller *m = i2c_dev_get_master(dev);
include/linux/i3c/master.h +1 -2
index 12d532b012c5..c67922ece617 100644
@@ -475,7 +475,7 @@ struct i3c_master_controller_ops {
 	int (*attach_i2c_dev)(struct i2c_dev_desc *dev);
 	void (*detach_i2c_dev)(struct i2c_dev_desc *dev);
 	int (*i2c_xfers)(struct i2c_dev_desc *dev,
-			 const struct i2c_msg *xfers, int nxfers);
+			 struct i2c_msg *xfers, int nxfers);
 	int (*request_ibi)(struct i3c_dev_desc *dev,
 			   const struct i3c_ibi_setup *req);
 	void (*free_ibi)(struct i3c_dev_desc *dev);
--

2.25.1

--
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c

maintainer Applied
#11 lore raw
In reply to Billy Tsai
Quoted 9 lines · Billy Tsai

The change is necessary to enable the use of the i2c_get_dma_safe_msg_buf() API, which requires a non-const struct i2c_msg * to operate. The i2c_get_dma_safe_msg_buf() function ensures safe handling of I2C messages when using DMA, making it essential for scenarios where DMA transfers are involved. By removing the const qualifier, this patch allows drivers to prepare and manage DMA-safe buffers directly.

[...]

Applied, thanks!

[1/2] i3c: Remove the const qualifier from i2c_msg pointer in i2c_xfers API

      https://git.kernel.org/abelloni/c/6866c91f8c23

[2/2] i3c: mipi-i3c-hci: Use I2C DMA-safe api
https://git.kernel.org/abelloni/c/effed5dac8f8

Best regards,

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com