linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] NTB: ntb_perf: fix cast to restricted __le32
@ 2018-01-19 17:30 Serge Semin
  2018-01-19 20:42 ` Arnd Bergmann
  2018-01-24  7:48 ` [PATCH v2] " Serge Semin
  0 siblings, 2 replies; 10+ messages in thread
From: Serge Semin @ 2018-01-19 17:30 UTC (permalink / raw)
  To: jdmason, dave.jiang, Allen.Hubbe
  Cc: gary.hook, arnd, Sergey.Semin, linux-ntb, linux-kernel, Serge Semin

Sparse is whining about the u32 and __le32 mixed usage in the
driver.

drivers/ntb/test/ntb_perf.c:288:21: warning: cast to restricted __le32
drivers/ntb/test/ntb_perf.c:295:37: warning: incorrect type in argument 4 (different base types)
drivers/ntb/test/ntb_perf.c:295:37:    expected unsigned int [unsigned] [usertype] val
drivers/ntb/test/ntb_perf.c:295:37:    got restricted __le32 [usertype] <noident>
...

The NTB API can't be changed so ntb_spad_*() methods
would return either pure __le32 or __be32, since the scratchpad
data can have arbitrary endianness in general. In this case we
need to forcibly cast all the u32 to be __le32 and vise-versa
where it's supposed to be in accordance with the driver logic.

Fixes: b83003b3fdc1 ("NTB: ntb_perf: Add full multi-port NTB API support")
Signed-off-by: Serge Semin <fancer.lancer@gmail.com>
---
 drivers/ntb/test/ntb_perf.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/drivers/ntb/test/ntb_perf.c b/drivers/ntb/test/ntb_perf.c
index eaa252b71f82..5590aaf8e8a5 100644
--- a/drivers/ntb/test/ntb_perf.c
+++ b/drivers/ntb/test/ntb_perf.c
@@ -283,21 +283,21 @@ static int perf_spad_cmd_send(struct perf_peer *peer, enum perf_cmd cmd,
 
 		sts = ntb_peer_spad_read(perf->ntb, peer->pidx,
 					 PERF_SPAD_CMD(perf->gidx));
-		if (le32_to_cpu(sts) != PERF_CMD_INVAL) {
+		if (le32_to_cpu((__force __le32)sts) != PERF_CMD_INVAL) {
 			usleep_range(MSG_UDELAY_LOW, MSG_UDELAY_HIGH);
 			continue;
 		}
 
 		ntb_peer_spad_write(perf->ntb, peer->pidx,
-				    PERF_SPAD_LDATA(perf->gidx),
-				    cpu_to_le32(lower_32_bits(data)));
+				PERF_SPAD_LDATA(perf->gidx),
+				(__force u32)cpu_to_le32(lower_32_bits(data)));
 		ntb_peer_spad_write(perf->ntb, peer->pidx,
-				    PERF_SPAD_HDATA(perf->gidx),
-				    cpu_to_le32(upper_32_bits(data)));
+				PERF_SPAD_HDATA(perf->gidx),
+				(__force u32)cpu_to_le32(upper_32_bits(data)));
 		mmiowb();
 		ntb_peer_spad_write(perf->ntb, peer->pidx,
 				    PERF_SPAD_CMD(perf->gidx),
-				    cpu_to_le32(cmd));
+				    (__force u32)cpu_to_le32(cmd));
 		mmiowb();
 		ntb_peer_db_set(perf->ntb, PERF_SPAD_NOTIFY(peer->gidx));
 
@@ -331,21 +331,21 @@ static int perf_spad_cmd_recv(struct perf_ctx *perf, int *pidx,
 			continue;
 
 		val = ntb_spad_read(perf->ntb, PERF_SPAD_CMD(peer->gidx));
-		val = le32_to_cpu(val);
+		val = le32_to_cpu((__force __le32)val);
 		if (val == PERF_CMD_INVAL)
 			continue;
 
 		*cmd = val;
 
 		val = ntb_spad_read(perf->ntb, PERF_SPAD_LDATA(peer->gidx));
-		*data = le32_to_cpu(val);
+		*data = le32_to_cpu((__force __le32)val);
 
 		val = ntb_spad_read(perf->ntb, PERF_SPAD_HDATA(peer->gidx));
-		*data |= (u64)le32_to_cpu(val) << 32;
+		*data |= (u64)le32_to_cpu((__force __le32)val) << 32;
 
 		/* Next command can be retrieved from now */
 		ntb_spad_write(perf->ntb, PERF_SPAD_CMD(peer->gidx),
-			       cpu_to_le32(PERF_CMD_INVAL));
+			       (__force u32)cpu_to_le32(PERF_CMD_INVAL));
 
 		dev_dbg(&perf->ntb->dev, "CMD recv: %d 0x%llx\n", *cmd, *data);
 
@@ -381,7 +381,7 @@ static int perf_msg_cmd_send(struct perf_peer *peer, enum perf_cmd cmd,
 			return ret;
 
 		ntb_peer_msg_write(perf->ntb, peer->pidx, PERF_MSG_LDATA,
-			      cpu_to_le32(lower_32_bits(data)));
+				(__force u32)cpu_to_le32(lower_32_bits(data)));
 
 		if (ntb_msg_read_sts(perf->ntb) & outbits) {
 			usleep_range(MSG_UDELAY_LOW, MSG_UDELAY_HIGH);
@@ -389,12 +389,12 @@ static int perf_msg_cmd_send(struct perf_peer *peer, enum perf_cmd cmd,
 		}
 
 		ntb_peer_msg_write(perf->ntb, peer->pidx, PERF_MSG_HDATA,
-			      cpu_to_le32(upper_32_bits(data)));
+				(__force u32)cpu_to_le32(upper_32_bits(data)));
 		mmiowb();
 
 		/* This call shall trigger peer message event */
 		ntb_peer_msg_write(perf->ntb, peer->pidx, PERF_MSG_CMD,
-			      cpu_to_le32(cmd));
+				   (__force u32)cpu_to_le32(cmd));
 
 		break;
 	}
@@ -414,13 +414,13 @@ static int perf_msg_cmd_recv(struct perf_ctx *perf, int *pidx,
 		return -ENODATA;
 
 	val = ntb_msg_read(perf->ntb, pidx, PERF_MSG_CMD);
-	*cmd = le32_to_cpu(val);
+	*cmd = le32_to_cpu((__force __le32)val);
 
 	val = ntb_msg_read(perf->ntb, pidx, PERF_MSG_LDATA);
-	*data = le32_to_cpu(val);
+	*data = le32_to_cpu((__force __le32)val);
 
 	val = ntb_msg_read(perf->ntb, pidx, PERF_MSG_HDATA);
-	*data |= (u64)le32_to_cpu(val) << 32;
+	*data |= (u64)le32_to_cpu((__force __le32)val) << 32;
 
 	/* Next command can be retrieved from now */
 	ntb_msg_clear_sts(perf->ntb, inbits);
-- 
2.12.0

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] NTB: ntb_perf: fix cast to restricted __le32
  2018-01-19 17:30 [PATCH] NTB: ntb_perf: fix cast to restricted __le32 Serge Semin
@ 2018-01-19 20:42 ` Arnd Bergmann
  2018-01-19 21:03   ` Serge Semin
  2018-01-24  7:48 ` [PATCH v2] " Serge Semin
  1 sibling, 1 reply; 10+ messages in thread
From: Arnd Bergmann @ 2018-01-19 20:42 UTC (permalink / raw)
  To: Serge Semin
  Cc: jdmason, Dave Jiang, Allen.Hubbe, Hook, Gary, Sergey.Semin,
	linux-ntb, Linux Kernel Mailing List

On Fri, Jan 19, 2018 at 6:30 PM, Serge Semin <fancer.lancer@gmail.com> wrote:
> Sparse is whining about the u32 and __le32 mixed usage in the
> driver.
>
> drivers/ntb/test/ntb_perf.c:288:21: warning: cast to restricted __le32
> drivers/ntb/test/ntb_perf.c:295:37: warning: incorrect type in argument 4 (different base types)
> drivers/ntb/test/ntb_perf.c:295:37:    expected unsigned int [unsigned] [usertype] val
> drivers/ntb/test/ntb_perf.c:295:37:    got restricted __le32 [usertype] <noident>
> ...
>
> The NTB API can't be changed so ntb_spad_*() methods
> would return either pure __le32 or __be32, since the scratchpad
> data can have arbitrary endianness in general. In this case we
> need to forcibly cast all the u32 to be __le32 and vise-versa
> where it's supposed to be in accordance with the driver logic.
>

There's got to be a better way to do this than sprinkling lots of __force
typecasts throughout the code.

It looks like all those casts are about
ntb_peer_spad_read()/ntb_peer_msg_write() calls, so why not change
those function prototypes to work on __le32 types?

There should also be some form of documentation regarding why you
need to swap the data twice, since all the ntb drivers later end up
doing another cpu_to_le32() on the little-endian data.

       Arnd

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] NTB: ntb_perf: fix cast to restricted __le32
  2018-01-19 20:42 ` Arnd Bergmann
@ 2018-01-19 21:03   ` Serge Semin
  2018-01-19 21:25     ` Serge Semin
  2018-01-19 21:26     ` Arnd Bergmann
  0 siblings, 2 replies; 10+ messages in thread
From: Serge Semin @ 2018-01-19 21:03 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: jdmason, Dave Jiang, Allen.Hubbe, Hook, Gary, Sergey.Semin,
	linux-ntb, Linux Kernel Mailing List

On Fri, Jan 19, 2018 at 09:42:17PM +0100, Arnd Bergmann <arnd@arndb.de> wrote:
> On Fri, Jan 19, 2018 at 6:30 PM, Serge Semin <fancer.lancer@gmail.com> wrote:
> > Sparse is whining about the u32 and __le32 mixed usage in the
> > driver.
> >
> > drivers/ntb/test/ntb_perf.c:288:21: warning: cast to restricted __le32
> > drivers/ntb/test/ntb_perf.c:295:37: warning: incorrect type in argument 4 (different base types)
> > drivers/ntb/test/ntb_perf.c:295:37:    expected unsigned int [unsigned] [usertype] val
> > drivers/ntb/test/ntb_perf.c:295:37:    got restricted __le32 [usertype] <noident>
> > ...
> >
> > The NTB API can't be changed so ntb_spad_*() methods
> > would return either pure __le32 or __be32, since the scratchpad
> > data can have arbitrary endianness in general. In this case we
> > need to forcibly cast all the u32 to be __le32 and vise-versa
> > where it's supposed to be in accordance with the driver logic.
> >
> 
> There's got to be a better way to do this than sprinkling lots of __force
> typecasts throughout the code.
> 
> It looks like all those casts are about
> ntb_peer_spad_read()/ntb_peer_msg_write() calls, so why not change
> those function prototypes to work on __le32 types?
> 
> There should also be some form of documentation regarding why you
> need to swap the data twice, since all the ntb drivers later end up
> doing another cpu_to_le32() on the little-endian data.
> 
>        Arnd

Actually the provided patch is the best solution I could come up with.
The thing is, that the methods can't be changed. Those functions are
the part of the NTB API methods used by many drivers. So basically they
are like pci_{read,write}_config_{byte,word,dword}() methods. We can't
change their prototypes only because it's suit some driver. The methods
give an access to the NTB device dummy u32-sized registers, nothing
else. So endianness is the transmitted data settings in this case.

NTB is the technology to interconnect some two systems with possibly
different endianness (unlike PCI, which interconnect CPU with LE devices).
In this case I'd need to set some agreement up between two systems about
the endianness of the exchanged data like host and network types in
Linux networking. I've chosen the network data to be little-endian,
that's why I needed first to convert them from CPU to le32, then on
remote side convert them back from le32 to CPU.

If you have any better suggestion how the warning can be fixed, I'd
be glad to stick to it.

-Sergey

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] NTB: ntb_perf: fix cast to restricted __le32
  2018-01-19 21:03   ` Serge Semin
@ 2018-01-19 21:25     ` Serge Semin
  2018-01-19 21:26     ` Arnd Bergmann
  1 sibling, 0 replies; 10+ messages in thread
From: Serge Semin @ 2018-01-19 21:25 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: jdmason, Dave Jiang, allenbh, Hook, Gary, Sergey.Semin,
	linux-ntb, Linux Kernel Mailing List

On Sat, Jan 20, 2018 at 12:03:10AM +0300, Serge Semin <fancer.lancer@gmail.com> wrote:
> On Fri, Jan 19, 2018 at 09:42:17PM +0100, Arnd Bergmann <arnd@arndb.de> wrote:
> > On Fri, Jan 19, 2018 at 6:30 PM, Serge Semin <fancer.lancer@gmail.com> wrote:
> > > Sparse is whining about the u32 and __le32 mixed usage in the
> > > driver.
> > >
> > > drivers/ntb/test/ntb_perf.c:288:21: warning: cast to restricted __le32
> > > drivers/ntb/test/ntb_perf.c:295:37: warning: incorrect type in argument 4 (different base types)
> > > drivers/ntb/test/ntb_perf.c:295:37:    expected unsigned int [unsigned] [usertype] val
> > > drivers/ntb/test/ntb_perf.c:295:37:    got restricted __le32 [usertype] <noident>
> > > ...
> > >
> > > The NTB API can't be changed so ntb_spad_*() methods
> > > would return either pure __le32 or __be32, since the scratchpad
> > > data can have arbitrary endianness in general. In this case we
> > > need to forcibly cast all the u32 to be __le32 and vise-versa
> > > where it's supposed to be in accordance with the driver logic.
> > >
> > 
> > There's got to be a better way to do this than sprinkling lots of __force
> > typecasts throughout the code.
> > 
> > It looks like all those casts are about
> > ntb_peer_spad_read()/ntb_peer_msg_write() calls, so why not change
> > those function prototypes to work on __le32 types?
> > 
> > There should also be some form of documentation regarding why you
> > need to swap the data twice, since all the ntb drivers later end up
> > doing another cpu_to_le32() on the little-endian data.
> > 
> >        Arnd
> 
> Actually the provided patch is the best solution I could come up with.
> The thing is, that the methods can't be changed. Those functions are
> the part of the NTB API methods used by many drivers. So basically they
> are like pci_{read,write}_config_{byte,word,dword}() methods. We can't
> change their prototypes only because it's suit some driver. The methods
> give an access to the NTB device dummy u32-sized registers, nothing
> else. So endianness is the transmitted data settings in this case.
> 
> NTB is the technology to interconnect some two systems with possibly
> different endianness (unlike PCI, which interconnect CPU with LE devices).
> In this case I'd need to set some agreement up between two systems about
> the endianness of the exchanged data like host and network types in
> Linux networking. I've chosen the network data to be little-endian,
> that's why I needed first to convert them from CPU to le32, then on
> remote side convert them back from le32 to CPU.
> 
> If you have any better suggestion how the warning can be fixed, I'd
> be glad to stick to it.
> 
> -Sergey
> 

I meant, everything depends on the NTB hardware driver hidden behind the
API. If it does back and forth conversions writing/reading data to/from
scratchpad registers (using IO methods like write32/read32), then
I don't need to worry about data endianness at all and should have
discarded le32_to_cpu()/cpu_to_l32() usage. But if it doesn't do it,
then ntb_perf driver will be in trouble. So I sticked with the safest
solution. Although the final decision is after the subsystem maintainer.

-Sergey

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] NTB: ntb_perf: fix cast to restricted __le32
  2018-01-19 21:03   ` Serge Semin
  2018-01-19 21:25     ` Serge Semin
@ 2018-01-19 21:26     ` Arnd Bergmann
  2018-01-24  4:18       ` Jon Mason
  1 sibling, 1 reply; 10+ messages in thread
From: Arnd Bergmann @ 2018-01-19 21:26 UTC (permalink / raw)
  To: Serge Semin
  Cc: jdmason, Dave Jiang, Allen.Hubbe, Hook, Gary, Sergey.Semin,
	linux-ntb, Linux Kernel Mailing List

On Fri, Jan 19, 2018 at 10:03 PM, Serge Semin <fancer.lancer@gmail.com> wrote:
>
> Actually the provided patch is the best solution I could come up with.
> The thing is, that the methods can't be changed. Those functions are
> the part of the NTB API methods used by many drivers. So basically they
> are like pci_{read,write}_config_{byte,word,dword}() methods. We can't
> change their prototypes only because it's suit some driver. The methods
> give an access to the NTB device dummy u32-sized registers, nothing
> else. So endianness is the transmitted data settings in this case.
>
> NTB is the technology to interconnect some two systems with possibly
> different endianness (unlike PCI, which interconnect CPU with LE devices).
> In this case I'd need to set some agreement up between two systems about
> the endianness of the exchanged data like host and network types in
> Linux networking. I've chosen the network data to be little-endian,
> that's why I needed first to convert them from CPU to le32, then on
> remote side convert them back from le32 to CPU.
>
> If you have any better suggestion how the warning can be fixed, I'd
> be glad to stick to it.

I don't think your description matches what you actually do: The
underlying ntb hardware drivers (amd, idt, intel, mscc) all treat the
incoming data as CPU-endian and convert it to little-endian on
the register side, so the framework already assumes that whatever
you do here uses a little-endian wire-level protocol.

On a little-endian kernel/CPU, nothing is ever swapped here, neither
in the ntb_perf front-end nor in the back-ends. On a big-endian
kernel/CPU, they both swap, so you end up with CPU-endian
data on the wire, so it should be impossible for a big-endian
system to talk to a little-endian one. Have you actually tried that
combination with the current code?

If my interpretation is correct, then the best solution would be to
completely remove the cpu_to_le32/le32_to_cpu conversions
from ntb_perf, and just define that it works like any other PCI
device, exchanging little-endian data.

There are two interesting cases to consider though:

- if someone wants to implement an NTB based protocol
  using big-endian data on the wire, you probably want to add
  a ntb_peer_spad_read_be()/ntb_peer_msg_write_be()
  set of interfaces, to go along with ioread32_be()/iowrite32_be()
  the same way that ntb_peer_spad_read()/ntb_peer_msg_write()
  ends up doing ioread32()/iowrite32() with the implied little-endian
  behavior.

- memcpy_toio()/memcpy_fromio() and ioread32_rep()/iowrite32_rep
  importantly do not do any byteswap, they are meant to
  transfer byte streams.

        Arnd

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] NTB: ntb_perf: fix cast to restricted __le32
  2018-01-19 21:26     ` Arnd Bergmann
@ 2018-01-24  4:18       ` Jon Mason
  2018-01-24  7:31         ` Serge Semin
  0 siblings, 1 reply; 10+ messages in thread
From: Jon Mason @ 2018-01-24  4:18 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Serge Semin, Dave Jiang, Allen.Hubbe, Hook, Gary, Sergey.Semin,
	linux-ntb, Linux Kernel Mailing List

On Fri, Jan 19, 2018 at 10:26:37PM +0100, Arnd Bergmann wrote:
> On Fri, Jan 19, 2018 at 10:03 PM, Serge Semin <fancer.lancer@gmail.com> wrote:
> >
> > Actually the provided patch is the best solution I could come up with.
> > The thing is, that the methods can't be changed. Those functions are
> > the part of the NTB API methods used by many drivers. So basically they
> > are like pci_{read,write}_config_{byte,word,dword}() methods. We can't
> > change their prototypes only because it's suit some driver. The methods
> > give an access to the NTB device dummy u32-sized registers, nothing
> > else. So endianness is the transmitted data settings in this case.
> >
> > NTB is the technology to interconnect some two systems with possibly
> > different endianness (unlike PCI, which interconnect CPU with LE devices).
> > In this case I'd need to set some agreement up between two systems about
> > the endianness of the exchanged data like host and network types in
> > Linux networking. I've chosen the network data to be little-endian,
> > that's why I needed first to convert them from CPU to le32, then on
> > remote side convert them back from le32 to CPU.
> >
> > If you have any better suggestion how the warning can be fixed, I'd
> > be glad to stick to it.
> 
> I don't think your description matches what you actually do: The
> underlying ntb hardware drivers (amd, idt, intel, mscc) all treat the
> incoming data as CPU-endian and convert it to little-endian on
> the register side, so the framework already assumes that whatever
> you do here uses a little-endian wire-level protocol.
> 
> On a little-endian kernel/CPU, nothing is ever swapped here, neither
> in the ntb_perf front-end nor in the back-ends. On a big-endian
> kernel/CPU, they both swap, so you end up with CPU-endian
> data on the wire, so it should be impossible for a big-endian
> system to talk to a little-endian one. Have you actually tried that
> combination with the current code?

I do not believe anyone has every tried NTB on a big endien system,
let alone tried it with one side LE and the other BE.  To my
knowledge, this has only ever been used on x86 to x86.

> If my interpretation is correct, then the best solution would be to
> completely remove the cpu_to_le32/le32_to_cpu conversions
> from ntb_perf, and just define that it works like any other PCI
> device, exchanging little-endian data.

Yes, this would be the best solution.  Thank you for the insight.

Serge, when you get a chance, please make this change and resumbit.

Thanks,
Jon


> 
> There are two interesting cases to consider though:
> 
> - if someone wants to implement an NTB based protocol
>   using big-endian data on the wire, you probably want to add
>   a ntb_peer_spad_read_be()/ntb_peer_msg_write_be()
>   set of interfaces, to go along with ioread32_be()/iowrite32_be()
>   the same way that ntb_peer_spad_read()/ntb_peer_msg_write()
>   ends up doing ioread32()/iowrite32() with the implied little-endian
>   behavior.
> 
> - memcpy_toio()/memcpy_fromio() and ioread32_rep()/iowrite32_rep
>   importantly do not do any byteswap, they are meant to
>   transfer byte streams.
> 
>         Arnd

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] NTB: ntb_perf: fix cast to restricted __le32
  2018-01-24  4:18       ` Jon Mason
@ 2018-01-24  7:31         ` Serge Semin
  0 siblings, 0 replies; 10+ messages in thread
From: Serge Semin @ 2018-01-24  7:31 UTC (permalink / raw)
  To: Jon Mason
  Cc: Arnd Bergmann, Dave Jiang, Allen.Hubbe, Hook, Gary, Sergey.Semin,
	linux-ntb, Linux Kernel Mailing List

On Tue, Jan 23, 2018 at 11:18:06PM -0500, Jon Mason <jdmason@kudzu.us> wrote:
> On Fri, Jan 19, 2018 at 10:26:37PM +0100, Arnd Bergmann wrote:
> > On Fri, Jan 19, 2018 at 10:03 PM, Serge Semin <fancer.lancer@gmail.com> wrote:
> > >
> > > Actually the provided patch is the best solution I could come up with.
> > > The thing is, that the methods can't be changed. Those functions are
> > > the part of the NTB API methods used by many drivers. So basically they
> > > are like pci_{read,write}_config_{byte,word,dword}() methods. We can't
> > > change their prototypes only because it's suit some driver. The methods
> > > give an access to the NTB device dummy u32-sized registers, nothing
> > > else. So endianness is the transmitted data settings in this case.
> > >
> > > NTB is the technology to interconnect some two systems with possibly
> > > different endianness (unlike PCI, which interconnect CPU with LE devices).
> > > In this case I'd need to set some agreement up between two systems about
> > > the endianness of the exchanged data like host and network types in
> > > Linux networking. I've chosen the network data to be little-endian,
> > > that's why I needed first to convert them from CPU to le32, then on
> > > remote side convert them back from le32 to CPU.
> > >
> > > If you have any better suggestion how the warning can be fixed, I'd
> > > be glad to stick to it.
> > 
> > I don't think your description matches what you actually do: The
> > underlying ntb hardware drivers (amd, idt, intel, mscc) all treat the
> > incoming data as CPU-endian and convert it to little-endian on
> > the register side, so the framework already assumes that whatever
> > you do here uses a little-endian wire-level protocol.
> > 
> > On a little-endian kernel/CPU, nothing is ever swapped here, neither
> > in the ntb_perf front-end nor in the back-ends. On a big-endian
> > kernel/CPU, they both swap, so you end up with CPU-endian
> > data on the wire, so it should be impossible for a big-endian
> > system to talk to a little-endian one. Have you actually tried that
> > combination with the current code?
> 
> I do not believe anyone has every tried NTB on a big endien system,
> let alone tried it with one side LE and the other BE.  To my
> knowledge, this has only ever been used on x86 to x86.
> 
> > If my interpretation is correct, then the best solution would be to
> > completely remove the cpu_to_le32/le32_to_cpu conversions
> > from ntb_perf, and just define that it works like any other PCI
> > device, exchanging little-endian data.
> 
> Yes, this would be the best solution.  Thank you for the insight.
> 
> Serge, when you get a chance, please make this change and resumbit.
> 

Ok. I'll do it in an hour.

Regards,
-Sergey

> Thanks,
> Jon
> 
> 
> > 
> > There are two interesting cases to consider though:
> > 
> > - if someone wants to implement an NTB based protocol
> >   using big-endian data on the wire, you probably want to add
> >   a ntb_peer_spad_read_be()/ntb_peer_msg_write_be()
> >   set of interfaces, to go along with ioread32_be()/iowrite32_be()
> >   the same way that ntb_peer_spad_read()/ntb_peer_msg_write()
> >   ends up doing ioread32()/iowrite32() with the implied little-endian
> >   behavior.
> > 
> > - memcpy_toio()/memcpy_fromio() and ioread32_rep()/iowrite32_rep
> >   importantly do not do any byteswap, they are meant to
> >   transfer byte streams.
> > 
> >         Arnd

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v2] NTB: ntb_perf: fix cast to restricted __le32
  2018-01-19 17:30 [PATCH] NTB: ntb_perf: fix cast to restricted __le32 Serge Semin
  2018-01-19 20:42 ` Arnd Bergmann
@ 2018-01-24  7:48 ` Serge Semin
  2018-01-24  8:07   ` Arnd Bergmann
  1 sibling, 1 reply; 10+ messages in thread
From: Serge Semin @ 2018-01-24  7:48 UTC (permalink / raw)
  To: jdmason, dave.jiang, allenbh, arnd
  Cc: gary.hook, Sergey.Semin, linux-ntb, linux-kernel, Serge Semin

Sparse is whining about the u32 and __le32 mixed usage in the driver

drivers/ntb/test/ntb_perf.c:288:21: warning: cast to restricted __le32
drivers/ntb/test/ntb_perf.c:295:37: warning: incorrect type in argument 4 (different base types)
drivers/ntb/test/ntb_perf.c:295:37:    expected unsigned int [unsigned] [usertype] val
drivers/ntb/test/ntb_perf.c:295:37:    got restricted __le32 [usertype] <noident>
...

NTB hardware drivers shall accept CPU-endian data and translate it to
the portable formate by internal means, so the explicit conversions
are not necessary before Scratchpad/Messages API usage anymore.

Fixes: b83003b3fdc1 ("NTB: ntb_perf: Add full multi-port NTB API support")
Signed-off-by: Serge Semin <fancer.lancer@gmail.com>
---
 drivers/ntb/test/ntb_perf.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)

diff --git a/drivers/ntb/test/ntb_perf.c b/drivers/ntb/test/ntb_perf.c
index 1829a17dd461..3fded6aeda08 100644
--- a/drivers/ntb/test/ntb_perf.c
+++ b/drivers/ntb/test/ntb_perf.c
@@ -273,21 +273,21 @@ static int perf_spad_cmd_send(struct perf_peer *peer, enum perf_cmd cmd,
 
 		sts = ntb_peer_spad_read(perf->ntb, peer->pidx,
 					 PERF_SPAD_CMD(perf->gidx));
-		if (le32_to_cpu(sts) != PERF_CMD_INVAL) {
+		if (sts != PERF_CMD_INVAL) {
 			usleep_range(MSG_UDELAY_LOW, MSG_UDELAY_HIGH);
 			continue;
 		}
 
 		ntb_peer_spad_write(perf->ntb, peer->pidx,
 				    PERF_SPAD_LDATA(perf->gidx),
-				    cpu_to_le32(lower_32_bits(data)));
+				    lower_32_bits(data));
 		ntb_peer_spad_write(perf->ntb, peer->pidx,
 				    PERF_SPAD_HDATA(perf->gidx),
-				    cpu_to_le32(upper_32_bits(data)));
+				    upper_32_bits(data));
 		mmiowb();
 		ntb_peer_spad_write(perf->ntb, peer->pidx,
 				    PERF_SPAD_CMD(perf->gidx),
-				    cpu_to_le32(cmd));
+				    cmd);
 		mmiowb();
 		ntb_peer_db_set(perf->ntb, PERF_SPAD_NOTIFY(peer->gidx));
 
@@ -321,21 +321,20 @@ static int perf_spad_cmd_recv(struct perf_ctx *perf, int *pidx,
 			continue;
 
 		val = ntb_spad_read(perf->ntb, PERF_SPAD_CMD(peer->gidx));
-		val = le32_to_cpu(val);
 		if (val == PERF_CMD_INVAL)
 			continue;
 
 		*cmd = val;
 
 		val = ntb_spad_read(perf->ntb, PERF_SPAD_LDATA(peer->gidx));
-		*data = le32_to_cpu(val);
+		*data = val;
 
 		val = ntb_spad_read(perf->ntb, PERF_SPAD_HDATA(peer->gidx));
-		*data |= (u64)le32_to_cpu(val) << 32;
+		*data |= (u64)val << 32;
 
 		/* Next command can be retrieved from now */
 		ntb_spad_write(perf->ntb, PERF_SPAD_CMD(peer->gidx),
-			       cpu_to_le32(PERF_CMD_INVAL));
+			       PERF_CMD_INVAL);
 
 		dev_dbg(&perf->ntb->dev, "CMD recv: %d 0x%llx\n", *cmd, *data);
 
@@ -371,7 +370,7 @@ static int perf_msg_cmd_send(struct perf_peer *peer, enum perf_cmd cmd,
 			return ret;
 
 		ntb_peer_msg_write(perf->ntb, peer->pidx, PERF_MSG_LDATA,
-			      cpu_to_le32(lower_32_bits(data)));
+				   lower_32_bits(data));
 
 		if (ntb_msg_read_sts(perf->ntb) & outbits) {
 			usleep_range(MSG_UDELAY_LOW, MSG_UDELAY_HIGH);
@@ -379,12 +378,11 @@ static int perf_msg_cmd_send(struct perf_peer *peer, enum perf_cmd cmd,
 		}
 
 		ntb_peer_msg_write(perf->ntb, peer->pidx, PERF_MSG_HDATA,
-			      cpu_to_le32(upper_32_bits(data)));
+				   upper_32_bits(data));
 		mmiowb();
 
 		/* This call shall trigger peer message event */
-		ntb_peer_msg_write(perf->ntb, peer->pidx, PERF_MSG_CMD,
-			      cpu_to_le32(cmd));
+		ntb_peer_msg_write(perf->ntb, peer->pidx, PERF_MSG_CMD, cmd);
 
 		break;
 	}
@@ -404,13 +402,13 @@ static int perf_msg_cmd_recv(struct perf_ctx *perf, int *pidx,
 		return -ENODATA;
 
 	val = ntb_msg_read(perf->ntb, pidx, PERF_MSG_CMD);
-	*cmd = le32_to_cpu(val);
+	*cmd = val;
 
 	val = ntb_msg_read(perf->ntb, pidx, PERF_MSG_LDATA);
-	*data = le32_to_cpu(val);
+	*data = val;
 
 	val = ntb_msg_read(perf->ntb, pidx, PERF_MSG_HDATA);
-	*data |= (u64)le32_to_cpu(val) << 32;
+	*data |= (u64)val << 32;
 
 	/* Next command can be retrieved from now */
 	ntb_msg_clear_sts(perf->ntb, inbits);
-- 
2.12.0

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] NTB: ntb_perf: fix cast to restricted __le32
  2018-01-24  7:48 ` [PATCH v2] " Serge Semin
@ 2018-01-24  8:07   ` Arnd Bergmann
  2018-01-25  4:13     ` Jon Mason
  0 siblings, 1 reply; 10+ messages in thread
From: Arnd Bergmann @ 2018-01-24  8:07 UTC (permalink / raw)
  To: Serge Semin
  Cc: jdmason, Dave Jiang, allenbh, Hook, Gary, Sergey.Semin,
	linux-ntb, Linux Kernel Mailing List

On Wed, Jan 24, 2018 at 8:48 AM, Serge Semin <fancer.lancer@gmail.com> wrote:
> Sparse is whining about the u32 and __le32 mixed usage in the driver
>
> drivers/ntb/test/ntb_perf.c:288:21: warning: cast to restricted __le32
> drivers/ntb/test/ntb_perf.c:295:37: warning: incorrect type in argument 4 (different base types)
> drivers/ntb/test/ntb_perf.c:295:37:    expected unsigned int [unsigned] [usertype] val
> drivers/ntb/test/ntb_perf.c:295:37:    got restricted __le32 [usertype] <noident>
> ...
>
> NTB hardware drivers shall accept CPU-endian data and translate it to
> the portable formate by internal means, so the explicit conversions
> are not necessary before Scratchpad/Messages API usage anymore.
>
> Fixes: b83003b3fdc1 ("NTB: ntb_perf: Add full multi-port NTB API support")
> Signed-off-by: Serge Semin <fancer.lancer@gmail.com>

Looks good to me,

Acked-by: Arnd Bergmann <arnd@arndb.de>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] NTB: ntb_perf: fix cast to restricted __le32
  2018-01-24  8:07   ` Arnd Bergmann
@ 2018-01-25  4:13     ` Jon Mason
  0 siblings, 0 replies; 10+ messages in thread
From: Jon Mason @ 2018-01-25  4:13 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Serge Semin, Dave Jiang, allenbh, Hook, Gary, Sergey.Semin,
	linux-ntb, Linux Kernel Mailing List

On Wed, Jan 24, 2018 at 09:07:26AM +0100, Arnd Bergmann wrote:
> On Wed, Jan 24, 2018 at 8:48 AM, Serge Semin <fancer.lancer@gmail.com> wrote:
> > Sparse is whining about the u32 and __le32 mixed usage in the driver
> >
> > drivers/ntb/test/ntb_perf.c:288:21: warning: cast to restricted __le32
> > drivers/ntb/test/ntb_perf.c:295:37: warning: incorrect type in argument 4 (different base types)
> > drivers/ntb/test/ntb_perf.c:295:37:    expected unsigned int [unsigned] [usertype] val
> > drivers/ntb/test/ntb_perf.c:295:37:    got restricted __le32 [usertype] <noident>
> > ...
> >
> > NTB hardware drivers shall accept CPU-endian data and translate it to
> > the portable formate by internal means, so the explicit conversions
> > are not necessary before Scratchpad/Messages API usage anymore.
> >
> > Fixes: b83003b3fdc1 ("NTB: ntb_perf: Add full multi-port NTB API support")
> > Signed-off-by: Serge Semin <fancer.lancer@gmail.com>
> 
> Looks good to me,
> 
> Acked-by: Arnd Bergmann <arnd@arndb.de>

Applied to my ntb-next branch.  Thanks for all of the help on this.

Thanks,
Jon

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2018-01-25  4:13 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-19 17:30 [PATCH] NTB: ntb_perf: fix cast to restricted __le32 Serge Semin
2018-01-19 20:42 ` Arnd Bergmann
2018-01-19 21:03   ` Serge Semin
2018-01-19 21:25     ` Serge Semin
2018-01-19 21:26     ` Arnd Bergmann
2018-01-24  4:18       ` Jon Mason
2018-01-24  7:31         ` Serge Semin
2018-01-24  7:48 ` [PATCH v2] " Serge Semin
2018-01-24  8:07   ` Arnd Bergmann
2018-01-25  4:13     ` Jon Mason

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).