Skip to contentSkip to conversation

i2c: amd756: Fix endianness handling for word data

PATCH In review Atharva Tiwari 4 participants 11 messages Jan 1, 2025 – Jan 9, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: amd756: Fix endianness handling for word data

Ensure correct handling of "endianness"
for word-sized data in amd756_access

- Convert word data into little-endian using cpu_to_le16
- Convert word data from little-endian
to cpu native format using le16_to_cpu

This fixes poteential issues on big-endian systems and ensure proper byte ordering for SMBus word transacitions

and you would be thinking why did i resend the patch it is because kernel test robot
noticed a few warning so i fixed them

---
 drivers/i2c/busses/i2c-amd756.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-amd756.c +3 -2
index fa0d5a2c3732..e551d63e96b1 100644
@@ -31,6 +31,7 @@
 #include <linux/i2c.h>
 #include <linux/acpi.h>
 #include <linux/io.h>
+#include <linux/byteorder/generic.h>
 
 /* AMD756 SMBus address offsets */
 #define SMB_ADDR_OFFSET		0xE0
@@ -211,7 +212,7 @@ static s32 amd756_access(struct i2c_adapter * adap, u16 addr,
 		       SMB_HOST_ADDRESS);
 		outb_p(command, SMB_HOST_COMMAND);
 		if (read_write == I2C_SMBUS_WRITE)
-			outw_p(data->word, SMB_HOST_DATA);	/* TODO: endian???? */
+			outw_p(cpu_to_le16((u16)data->word), SMB_HOST_DATA);
 		size = AMD756_WORD_DATA;
 		break;
 	case I2C_SMBUS_BLOCK_DATA:
@@ -256,7 +257,7 @@ static s32 amd756_access(struct i2c_adapter * adap, u16 addr,
 		data->byte = inw_p(SMB_HOST_DATA);
 		break;
 	case AMD756_WORD_DATA:
-		data->word = inw_p(SMB_HOST_DATA);	/* TODO: endian???? */
+		data->word = (u16)le16_to_cpu(inw_p(SMB_HOST_DATA));
 		break;
 	case AMD756_BLOCK_DATA:
 		data->block[0] = inw_p(SMB_HOST_DATA) & 0x3f;
Signature · 1 line
2.39.5
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Atharva,

kernel test robot noticed the following build warnings:

[auto build test WARNING on v6.13-rc5]
[also build test WARNING on linus/master next-20241220] [cannot apply to andi-shyti/i2c/i2c-host]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Atharva-Tiwari/i2c-amd756-Fix-endianness-handling-for-word-data/20250101-183620
base:   v6.13-rc5
patch link:    https://lore.kernel.org/r/20250101103422.30523-1-evepolonium%40gmail.com
patch subject: [PATCH] i2c: amd756: Fix endianness handling for word data
config: powerpc-randconfig-r111-20250103 (https://download.01.org/0day-ci/archive/20250103/[email protected]/config)
compiler: powerpc-linux-gcc (GCC) 14.2.0
reproduce: (https://download.01.org/0day-ci/archive/20250103/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> drivers/i2c/busses/i2c-amd756.c:215:25: sparse: sparse: incorrect type in argument 1 (different base types) @@     expected unsigned short [usertype] val @@     got restricted __le16 [usertype] @@

drivers/i2c/busses/i2c-amd756.c:215:25: sparse: expected unsigned short [usertype] val
drivers/i2c/busses/i2c-amd756.c:215:25: sparse: got restricted __le16 [usertype]
drivers/i2c/busses/i2c-amd756.c:260:35: sparse: sparse: cast to restricted __le16

vim +215 drivers/i2c/busses/i2c-amd756.c

180
181 /* Return negative errno on error. */
182 static s32 amd756_access(struct i2c_adapter * adap, u16 addr,
183 unsigned short flags, char read_write,
184 u8 command, int size, union i2c_smbus_data * data)
185 {
186 int i, len;
187 int status;
188
189 switch (size) {
190 case I2C_SMBUS_QUICK:
191 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
192 SMB_HOST_ADDRESS);
193 size = AMD756_QUICK;
194 break;
195 case I2C_SMBUS_BYTE:
196 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
197 SMB_HOST_ADDRESS);
198 if (read_write == I2C_SMBUS_WRITE)
199 outb_p(command, SMB_HOST_DATA);
200 size = AMD756_BYTE;
201 break;
202 case I2C_SMBUS_BYTE_DATA:
203 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
204 SMB_HOST_ADDRESS);
205 outb_p(command, SMB_HOST_COMMAND);
206 if (read_write == I2C_SMBUS_WRITE)
207 outw_p(data->byte, SMB_HOST_DATA);
208 size = AMD756_BYTE_DATA;
209 break;
210 case I2C_SMBUS_WORD_DATA:
211 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
212 SMB_HOST_ADDRESS);
213 outb_p(command, SMB_HOST_COMMAND);
214 if (read_write == I2C_SMBUS_WRITE)

 > 215				outw_p(cpu_to_le16((u16)data->word), SMB_HOST_DATA);

216 size = AMD756_WORD_DATA;
217 break;
218 case I2C_SMBUS_BLOCK_DATA:
219 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
220 SMB_HOST_ADDRESS);
221 outb_p(command, SMB_HOST_COMMAND);
222 if (read_write == I2C_SMBUS_WRITE) {
223 len = data->block[0];
224 if (len < 0)
225 len = 0;
226 if (len > 32)
227 len = 32;
228 outw_p(len, SMB_HOST_DATA);
229 /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
230 for (i = 1; i <= len; i++)
231 outb_p(data->block[i],
232 SMB_HOST_BLOCK_DATA);
233 }
234 size = AMD756_BLOCK_DATA;
235 break;
236 default:
237 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
238 return -EOPNOTSUPP;
239 }
240
241 /* How about enabling interrupts... */
242 outw_p(size & GE_CYC_TYPE_MASK, SMB_GLOBAL_ENABLE);
243
244 status = amd756_transaction(adap);
245 if (status)
246 return status;
247
248 if ((read_write == I2C_SMBUS_WRITE) || (size == AMD756_QUICK))
249 return 0;
250
251
252 switch (size) {
253 case AMD756_BYTE:
254 data->byte = inw_p(SMB_HOST_DATA);
255 break;
256 case AMD756_BYTE_DATA:
257 data->byte = inw_p(SMB_HOST_DATA);
258 break;
259 case AMD756_WORD_DATA:
260 data->word = (u16)le16_to_cpu(inw_p(SMB_HOST_DATA));
261 break;
262 case AMD756_BLOCK_DATA:
263 data->block[0] = inw_p(SMB_HOST_DATA) & 0x3f;
264 if(data->block[0] > 32)
265 data->block[0] = 32;
266 /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
267 for (i = 1; i <= data->block[0]; i++)
268 data->block[i] = inb_p(SMB_HOST_BLOCK_DATA);
269 break;
270 }
271
272 return 0;
273 }
274

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Atharva Tiwari
kernel test robot noticed the following build warnings:

Hi Atharva,

kernel test robot noticed the following build warnings:

[auto build test WARNING on v6.13-rc5]
[also build test WARNING on linus/master next-20241220] [cannot apply to andi-shyti/i2c/i2c-host]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Atharva-Tiwari/i2c-amd756-Fix-endianness-handling-for-word-data/20250101-183620
base:   v6.13-rc5
patch link:    https://lore.kernel.org/r/20250101103422.30523-1-evepolonium%40gmail.com
patch subject: [PATCH] i2c: amd756: Fix endianness handling for word data
config: sparc64-randconfig-r132-20250103 (https://download.01.org/0day-ci/archive/20250103/[email protected]/config)
compiler: sparc64-linux-gcc (GCC) 14.2.0
reproduce: (https://download.01.org/0day-ci/archive/20250103/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> drivers/i2c/busses/i2c-amd756.c:215:25: sparse: sparse: incorrect type in argument 1 (different base types) @@     expected unsigned short [usertype] w @@     got restricted __le16 [usertype] @@

drivers/i2c/busses/i2c-amd756.c:215:25: sparse: expected unsigned short [usertype] w
drivers/i2c/busses/i2c-amd756.c:215:25: sparse: got restricted __le16 [usertype]
drivers/i2c/busses/i2c-amd756.c:260:35: sparse: sparse: cast to restricted __le16 drivers/i2c/busses/i2c-amd756.c:260:35: sparse: sparse: cast to restricted __le16 drivers/i2c/busses/i2c-amd756.c:260:35: sparse: sparse: cast to restricted __le16 drivers/i2c/busses/i2c-amd756.c:260:35: sparse: sparse: cast to restricted __le16

vim +215 drivers/i2c/busses/i2c-amd756.c

180
181 /* Return negative errno on error. */
182 static s32 amd756_access(struct i2c_adapter * adap, u16 addr,
183 unsigned short flags, char read_write,
184 u8 command, int size, union i2c_smbus_data * data)
185 {
186 int i, len;
187 int status;
188
189 switch (size) {
190 case I2C_SMBUS_QUICK:
191 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
192 SMB_HOST_ADDRESS);
193 size = AMD756_QUICK;
194 break;
195 case I2C_SMBUS_BYTE:
196 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
197 SMB_HOST_ADDRESS);
198 if (read_write == I2C_SMBUS_WRITE)
199 outb_p(command, SMB_HOST_DATA);
200 size = AMD756_BYTE;
201 break;
202 case I2C_SMBUS_BYTE_DATA:
203 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
204 SMB_HOST_ADDRESS);
205 outb_p(command, SMB_HOST_COMMAND);
206 if (read_write == I2C_SMBUS_WRITE)
207 outw_p(data->byte, SMB_HOST_DATA);
208 size = AMD756_BYTE_DATA;
209 break;
210 case I2C_SMBUS_WORD_DATA:
211 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
212 SMB_HOST_ADDRESS);
213 outb_p(command, SMB_HOST_COMMAND);
214 if (read_write == I2C_SMBUS_WRITE)

 > 215				outw_p(cpu_to_le16((u16)data->word), SMB_HOST_DATA);

216 size = AMD756_WORD_DATA;
217 break;
218 case I2C_SMBUS_BLOCK_DATA:
219 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
220 SMB_HOST_ADDRESS);
221 outb_p(command, SMB_HOST_COMMAND);
222 if (read_write == I2C_SMBUS_WRITE) {
223 len = data->block[0];
224 if (len < 0)
225 len = 0;
226 if (len > 32)
227 len = 32;
228 outw_p(len, SMB_HOST_DATA);
229 /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
230 for (i = 1; i <= len; i++)
231 outb_p(data->block[i],
232 SMB_HOST_BLOCK_DATA);
233 }
234 size = AMD756_BLOCK_DATA;
235 break;
236 default:
237 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
238 return -EOPNOTSUPP;
239 }
240
241 /* How about enabling interrupts... */
242 outw_p(size & GE_CYC_TYPE_MASK, SMB_GLOBAL_ENABLE);
243
244 status = amd756_transaction(adap);
245 if (status)
246 return status;
247
248 if ((read_write == I2C_SMBUS_WRITE) || (size == AMD756_QUICK))
249 return 0;
250
251
252 switch (size) {
253 case AMD756_BYTE:
254 data->byte = inw_p(SMB_HOST_DATA);
255 break;
256 case AMD756_BYTE_DATA:
257 data->byte = inw_p(SMB_HOST_DATA);
258 break;
259 case AMD756_WORD_DATA:
260 data->word = (u16)le16_to_cpu(inw_p(SMB_HOST_DATA));
261 break;
262 case AMD756_BLOCK_DATA:
263 data->block[0] = inw_p(SMB_HOST_DATA) & 0x3f;
264 if(data->block[0] > 32)
265 data->block[0] = 32;
266 /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
267 for (i = 1; i <= data->block[0]; i++)
268 data->block[i] = inb_p(SMB_HOST_BLOCK_DATA);
269 break;
270 }
271
272 return 0;
273 }
274

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#3 lore raw
In reply to Atharva Tiwari
Quoted 7 lines · Atharva Tiwari

Ensure correct handling of "endianness"
for word-sized data in amd756_access

- Convert word data into little-endian using cpu_to_le16
- Convert word data from little-endian
to cpu native format using le16_to_cpu

Quoted 6 lines

This fixes poteential issues on big-endian systems and ensure proper byte ordering for SMBus word transacitions

and you would be thinking why did i resend the patch it is because kernel test robot
noticed a few warning so i fixed them

first of all, thanks for your patch. I was aware that this issue had been reported by the LKP, but I initially decided to keep it as it was and queue it in i2c/i2c-host since I didn't consider it a serious issue.

Quoted 2 lines

Now, you're treating it as a fix, which is perfectly fine. For me, it's a 50-50 case.

I'll reward your second version by moving this patch to the fixes branch. However, for the next time, please:

- Be transparent about your intentions: you knew I had already merged it, and you should have discussed this with me before sending a new version.
- Number your patches as [PATCH v2] using 'git format-patch -v 2...'.
- Avoid leaving blank spaces in the tag section: for example, there is a blank line between 'Closes:' and 'Signed-off-by:'.
- Add a changelog to highlight what has changed.
- Include the 'Fixes:' tag[1] (wow, this dates back to the origins of git!).
- Cc the stable mailing list[2] to ensure proper visibility.

In any case, don't worry—I’ll take care of this, and there's no need for you to resend it.

Thanks,
Andi

[1] Fixes: 1da177e4c3f4 ('Linux-2.6.12-rc2')
[2] Cc: <[email protected]> # v2.6.12+

#4 lore raw
Quoted 3 lines

Now, you're treating it as a fix, which is perfectly fine. For me, it's a 50-50 case.

For me, this is a clear no-fix case as long as nobody really reported problems (which I am not aware of). Also, looking at the Kconfig text, it looks unlikely to me that this controller has been used with big endian systems. Or?

maintainer
#5 lore raw
Quoted 7 lines · Wolfram Sang
Quoted 2 lines

Now, you're treating it as a fix, which is perfectly fine. For me, it's a 50-50 case.

For me, this is a clear no-fix case as long as nobody really reported problems (which I am not aware of). Also, looking at the Kconfig text, it looks unlikely to me that this controller has been used with big endian systems. Or?

Yeah! That was my first thought, but because this was reported by LKP (which I respect more than other code analyzers) as a potential issue, I was on the 50-50.

I still agree that the patch is correct because there's a hanging comment. So, either remove the comment or go with Athrva's approach.

Urgh, I'm just leaving things as they are :-)

Andi

#6 lore raw
Quoted 4 lines

Yeah! That was my first thought, but because this was reported by LKP (which I respect more than other code analyzers) as a potential issue, I was on the 50-50.

A potential issue on outdated hardware (or better: non-existing versions of outdated harware) which has not showed up since the git era is not really a for-current bugfix in my book. That being said, it does not harm in for-current.

maintainer
#7 lore raw
Quoted 8 lines · Wolfram Sang
Quoted 3 lines

Yeah! That was my first thought, but because this was reported by LKP (which I respect more than other code analyzers) as a potential issue, I was on the 50-50.

A potential issue on outdated hardware (or better: non-existing versions of outdated harware) which has not showed up since the git era is not really a for-current bugfix in my book. That being said, it does not harm in for-current.

Honestly, I'm not entirely happy with this patch. It seems to add unnecessary churn without addressing an actual need. Since we're operating in the same endianness domain, I don't think there is requirement for an endianness conversion here.

On one hand, I want to give credit to Atharva for his effort; on the other hand, I think the real solution would be to just remove the comments altogether.

I'm going to hold off for now and wait for additional feedback. If no further comments come in, I'll likely drop this patch.

Andi

#8 lore raw
Quoted 4 lines

On one hand, I want to give credit to Atharva for his effort; on the other hand, I think the real solution would be to just remove the comments altogether.

Yes. Atharva can still send a patch doing this.

maintainer
#9 lore raw
In reply to Atharva Tiwari

Hi again,

Quoted 10 lines
drivers/i2c/busses/i2c-amd756.c +1 -1
@@ -211,7 +212,7 @@ static s32 amd756_access(struct i2c_adapter * adap, u16 addr,
 		       SMB_HOST_ADDRESS);
 		outb_p(command, SMB_HOST_COMMAND);
 		if (read_write == I2C_SMBUS_WRITE)
-			outw_p(data->word, SMB_HOST_DATA);	/* TODO: endian???? */
+			outw_p(cpu_to_le16((u16)data->word), SMB_HOST_DATA);
 		size = AMD756_WORD_DATA;
 		break;
 	case I2C_SMBUS_BLOCK_DATA:

drivers/i2c/busses/i2c-amd756.c static s32 amd756_access(struct i2c_adapter * adap, u16 addr, · line 260

Quoted 5 lines
drivers/i2c/busses/i2c-amd756.c +1 -1
@@ -256,7 +257,7 @@ static s32 amd756_access(struct i2c_adapter * adap, u16 addr,
 		data->byte = inw_p(SMB_HOST_DATA);
 		break;
 	case AMD756_WORD_DATA:
-		data->word = inw_p(SMB_HOST_DATA);	/* TODO: endian???? */
+		data->word = (u16)le16_to_cpu(inw_p(SMB_HOST_DATA));

sorry, please do send a new version, the cast should not be needed here.

If you have any questions, feel free to ask, after having read Documentation/process/coding-style.rst.

Thanks,
Andi

Quoted 6 lines
drivers/i2c/busses/i2c-amd756.c +0 -0
 		break;
 	case AMD756_BLOCK_DATA:
 		data->block[0] = inw_p(SMB_HOST_DATA) & 0x3f;
Signature · 1 line
2.39.5
maintainer
#10 lore raw
Quoted 21 lines · Andi Shyti

Hi again,

Quoted 10 lines
drivers/i2c/busses/i2c-amd756.c +1 -1
@@ -211,7 +212,7 @@ static s32 amd756_access(struct i2c_adapter * adap, u16 addr,
 		       SMB_HOST_ADDRESS);
 		outb_p(command, SMB_HOST_COMMAND);
 		if (read_write == I2C_SMBUS_WRITE)
-			outw_p(data->word, SMB_HOST_DATA);	/* TODO: endian???? */
+			outw_p(cpu_to_le16((u16)data->word), SMB_HOST_DATA);
 		size = AMD756_WORD_DATA;
 		break;
 	case I2C_SMBUS_BLOCK_DATA:

drivers/i2c/busses/i2c-amd756.c static s32 amd756_access(struct i2c_adapter * adap, u16 addr, · line 260

Quoted 5 lines
drivers/i2c/busses/i2c-amd756.c +1 -1
@@ -256,7 +257,7 @@ static s32 amd756_access(struct i2c_adapter * adap, u16 addr,
 		data->byte = inw_p(SMB_HOST_DATA);
 		break;
 	case AMD756_WORD_DATA:
-		data->word = inw_p(SMB_HOST_DATA);	/* TODO: endian???? */
+		data->word = (u16)le16_to_cpu(inw_p(SMB_HOST_DATA));

sorry, please do send a new version, the cast should not be needed here.

Quoted 2 lines

If you have any questions, feel free to ask, after having read Documentation/process/coding-style.rst.

Sorry, I managed to make myself misunderstood in my previous emails. Please don't send anything, I've already pushed the version with the fix reported by LKP.

Andi