diff mbox series

[v1,1/4] usb: xhci: Add missing endian conversions (cpu_to_leXX / leXX_to_cpu)

Message ID 20200702084733.2032531-2-sr@denx.de
State Superseded
Delegated to: Marek Vasut
Headers show
Series [v1,1/4] usb: xhci: Add missing endian conversions (cpu_to_leXX / leXX_to_cpu) | expand

Commit Message

Stefan Roese July 2, 2020, 8:47 a.m. UTC
While trying to use the U-Boot xHCI driver on the MIPS Octeon platform,
which is big endian, I noticed that the driver is missing a few endian
conversion calls. This patch adds these missing endian conversion
calls.

Signed-off-by: Stefan Roese <sr@denx.de>
Cc: Bin Meng <bmeng.cn@gmail.com>
Cc: Marek Vasut <marex@denx.de>
---

 drivers/usb/host/xhci-mem.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Stefan Roese July 16, 2020, 9:24 a.m. UTC | #1
Hi Bin,

On 02.07.20 10:47, Stefan Roese wrote:
> While trying to use the U-Boot xHCI driver on the MIPS Octeon platform,
> which is big endian, I noticed that the driver is missing a few endian
> conversion calls. This patch adds these missing endian conversion
> calls.

Did you find the time to review this patchset with endianess corrections
and support for physical vs virtual addresses? Its in preparation for
the Octeon (MIPS big endian) xHCI support.

I would like to have this common xhci parts integrated in mainline early
in this release (if there are no issues of course), so that these
changes can get a broad testing on multiple platforms.

Thanks,
Stefan

> Signed-off-by: Stefan Roese <sr@denx.de>
> Cc: Bin Meng <bmeng.cn@gmail.com>
> Cc: Marek Vasut <marex@denx.de>
> ---
> 
>   drivers/usb/host/xhci-mem.c | 9 +++++----
>   1 file changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
> index 2d968aafb0..bd959b4093 100644
> --- a/drivers/usb/host/xhci-mem.c
> +++ b/drivers/usb/host/xhci-mem.c
> @@ -110,7 +110,7 @@ static void xhci_scratchpad_free(struct xhci_ctrl *ctrl)
>   
>   	ctrl->dcbaa->dev_context_ptrs[0] = 0;
>   
> -	free((void *)(uintptr_t)ctrl->scratchpad->sp_array[0]);
> +	free((void *)le64_to_cpu(ctrl->scratchpad->sp_array[0]));
>   	free(ctrl->scratchpad->sp_array);
>   	free(ctrl->scratchpad);
>   	ctrl->scratchpad = NULL;
> @@ -225,7 +225,8 @@ static void xhci_link_segments(struct xhci_segment *prev,
>   	prev->next = next;
>   	if (link_trbs) {
>   		val_64 = (uintptr_t)next->trbs;
> -		prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = val_64;
> +		prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr =
> +			cpu_to_le64(val_64);
>   
>   		/*
>   		 * Set the last TRB in the segment to
> @@ -486,7 +487,7 @@ int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id)
>   	byte_64 = (uintptr_t)(virt_dev->out_ctx->bytes);
>   
>   	/* Point to output device context in dcbaa. */
> -	ctrl->dcbaa->dev_context_ptrs[slot_id] = byte_64;
> +	ctrl->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(byte_64);
>   
>   	xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
>   			 sizeof(__le64));
> @@ -768,7 +769,7 @@ void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,
>   
>   	debug("route string %x\n", route);
>   #endif
> -	slot_ctx->dev_info |= route;
> +	slot_ctx->dev_info |= cpu_to_le32(route);
>   
>   	switch (speed) {
>   	case USB_SPEED_SUPER:
> 


Viele Grüße,
Stefan
Bin Meng July 16, 2020, 9:39 a.m. UTC | #2
Hi Stefan,

On Thu, Jul 16, 2020 at 5:25 PM Stefan Roese <sr@denx.de> wrote:
>
> Hi Bin,
>
> On 02.07.20 10:47, Stefan Roese wrote:
> > While trying to use the U-Boot xHCI driver on the MIPS Octeon platform,
> > which is big endian, I noticed that the driver is missing a few endian
> > conversion calls. This patch adds these missing endian conversion
> > calls.
>
> Did you find the time to review this patchset with endianess corrections
> and support for physical vs virtual addresses? Its in preparation for
> the Octeon (MIPS big endian) xHCI support.

Sorry I missed this series. I will take a look.

>
> I would like to have this common xhci parts integrated in mainline early
> in this release (if there are no issues of course), so that these
> changes can get a broad testing on multiple platforms.

Regards,
Bin
Bin Meng July 17, 2020, 5:15 a.m. UTC | #3
On Thu, Jul 2, 2020 at 4:47 PM Stefan Roese <sr@denx.de> wrote:
>
> While trying to use the U-Boot xHCI driver on the MIPS Octeon platform,
> which is big endian, I noticed that the driver is missing a few endian
> conversion calls. This patch adds these missing endian conversion
> calls.
>
> Signed-off-by: Stefan Roese <sr@denx.de>
> Cc: Bin Meng <bmeng.cn@gmail.com>
> Cc: Marek Vasut <marex@denx.de>
> ---
>
>  drivers/usb/host/xhci-mem.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
>

Good catch! It's hard to detect these problems if we only validate
xHCI on ARM/x86 which are little-endian. Apparently there is no xHCI
on PPC, so MIPS becomes the first big endian platform using xHCI.

Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Stefan Roese July 17, 2020, 9:57 a.m. UTC | #4
On 17.07.20 07:15, Bin Meng wrote:
> On Thu, Jul 2, 2020 at 4:47 PM Stefan Roese <sr@denx.de> wrote:
>>
>> While trying to use the U-Boot xHCI driver on the MIPS Octeon platform,
>> which is big endian, I noticed that the driver is missing a few endian
>> conversion calls. This patch adds these missing endian conversion
>> calls.
>>
>> Signed-off-by: Stefan Roese <sr@denx.de>
>> Cc: Bin Meng <bmeng.cn@gmail.com>
>> Cc: Marek Vasut <marex@denx.de>
>> ---
>>
>>   drivers/usb/host/xhci-mem.c | 9 +++++----
>>   1 file changed, 5 insertions(+), 4 deletions(-)
>>
> 
> Good catch! It's hard to detect these problems if we only validate
> xHCI on ARM/x86 which are little-endian. Apparently there is no xHCI
> on PPC, so MIPS becomes the first big endian platform using xHCI.

Yes, I was also astonished that the xHCI driver has only been used by
little-endian platforms so far. I did expect that at least some PPC
platforms supported this code. Apparently this is not the case.

> Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

Thanks,
Stefan
Bin Meng July 17, 2020, 11:18 a.m. UTC | #5
Hi Stefan,

On Thu, Jul 2, 2020 at 4:47 PM Stefan Roese <sr@denx.de> wrote:
>
> While trying to use the U-Boot xHCI driver on the MIPS Octeon platform,
> which is big endian, I noticed that the driver is missing a few endian
> conversion calls. This patch adds these missing endian conversion
> calls.
>
> Signed-off-by: Stefan Roese <sr@denx.de>
> Cc: Bin Meng <bmeng.cn@gmail.com>
> Cc: Marek Vasut <marex@denx.de>
> ---
>
>  drivers/usb/host/xhci-mem.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
> index 2d968aafb0..bd959b4093 100644
> --- a/drivers/usb/host/xhci-mem.c
> +++ b/drivers/usb/host/xhci-mem.c
> @@ -110,7 +110,7 @@ static void xhci_scratchpad_free(struct xhci_ctrl *ctrl)
>
>         ctrl->dcbaa->dev_context_ptrs[0] = 0;
>
> -       free((void *)(uintptr_t)ctrl->scratchpad->sp_array[0]);
> +       free((void *)le64_to_cpu(ctrl->scratchpad->sp_array[0]));

There is a build warning for this:

drivers/usb/host/xhci-mem.c: In function 'xhci_scratchpad_free':
drivers/usb/host/xhci-mem.c:113:7: warning: cast to pointer from
integer of different size [-Wint-to-pointer-cast]
  free((void *)le64_to_cpu(ctrl->scratchpad->sp_array[0]));
       ^

>         free(ctrl->scratchpad->sp_array);
>         free(ctrl->scratchpad);
>         ctrl->scratchpad = NULL;
> @@ -225,7 +225,8 @@ static void xhci_link_segments(struct xhci_segment *prev,
>         prev->next = next;
>         if (link_trbs) {
>                 val_64 = (uintptr_t)next->trbs;
> -               prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = val_64;
> +               prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr =
> +                       cpu_to_le64(val_64);
>
>                 /*
>                  * Set the last TRB in the segment to
> @@ -486,7 +487,7 @@ int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id)
>         byte_64 = (uintptr_t)(virt_dev->out_ctx->bytes);
>
>         /* Point to output device context in dcbaa. */
> -       ctrl->dcbaa->dev_context_ptrs[slot_id] = byte_64;
> +       ctrl->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(byte_64);
>
>         xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
>                          sizeof(__le64));
> @@ -768,7 +769,7 @@ void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,
>
>         debug("route string %x\n", route);
>  #endif
> -       slot_ctx->dev_info |= route;
> +       slot_ctx->dev_info |= cpu_to_le32(route);
>
>         switch (speed) {
>         case USB_SPEED_SUPER:
> --

Test results on Minnowmax which has one USB 2.0 port and one 3.0 port:

USB 2.0 flash drive inserted to USB 2.0 port: recognized, read/write OK
USB 2.0 flash drive inserted to USB 3.0 port: recognized, read/write OK

USB 3.0 flash drive inserted to USB 2.0 port: recognized, read/write OK
USB 3.0 flash drive inserted to USB 3.0 port: recognized, read/write OK

USB 2.0 flash drive connected to a USB 3.0 HUB inserted to USB 2.0
port: recognized, read/write OK
USB 2.0 flash drive connected to a USB 3.0 HUB inserted to USB 3.0
port: recognized, read/write OK

USB 3.0 flash drive connected to a USB 3.0 HUB inserted to USB 2.0
port: recognized, read/write OK
USB 3.0 flash drive connected to a USB 3.0 HUB inserted to USB 3.0
port: recognized, read/write OK

Tested-by: Bin Meng <bmeng.cn@gmail.com>

Regards,
Bin
Stefan Roese July 17, 2020, 11:34 a.m. UTC | #6
Hi Bin,

On 17.07.20 13:18, Bin Meng wrote:
> Hi Stefan,
> 
> On Thu, Jul 2, 2020 at 4:47 PM Stefan Roese <sr@denx.de> wrote:
>>
>> While trying to use the U-Boot xHCI driver on the MIPS Octeon platform,
>> which is big endian, I noticed that the driver is missing a few endian
>> conversion calls. This patch adds these missing endian conversion
>> calls.
>>
>> Signed-off-by: Stefan Roese <sr@denx.de>
>> Cc: Bin Meng <bmeng.cn@gmail.com>
>> Cc: Marek Vasut <marex@denx.de>
>> ---
>>
>>   drivers/usb/host/xhci-mem.c | 9 +++++----
>>   1 file changed, 5 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
>> index 2d968aafb0..bd959b4093 100644
>> --- a/drivers/usb/host/xhci-mem.c
>> +++ b/drivers/usb/host/xhci-mem.c
>> @@ -110,7 +110,7 @@ static void xhci_scratchpad_free(struct xhci_ctrl *ctrl)
>>
>>          ctrl->dcbaa->dev_context_ptrs[0] = 0;
>>
>> -       free((void *)(uintptr_t)ctrl->scratchpad->sp_array[0]);
>> +       free((void *)le64_to_cpu(ctrl->scratchpad->sp_array[0]));
> 
> There is a build warning for this:
> 
> drivers/usb/host/xhci-mem.c: In function 'xhci_scratchpad_free':
> drivers/usb/host/xhci-mem.c:113:7: warning: cast to pointer from
> integer of different size [-Wint-to-pointer-cast]
>    free((void *)le64_to_cpu(ctrl->scratchpad->sp_array[0]));
>         ^

So we need the (uintptr_t) here as well?

   free((void *)(uintptr_t)le64_to_cpu(ctrl->scratchpad->sp_array[0]));

Should I send v2 for this patch?

>>          free(ctrl->scratchpad->sp_array);
>>          free(ctrl->scratchpad);
>>          ctrl->scratchpad = NULL;
>> @@ -225,7 +225,8 @@ static void xhci_link_segments(struct xhci_segment *prev,
>>          prev->next = next;
>>          if (link_trbs) {
>>                  val_64 = (uintptr_t)next->trbs;
>> -               prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = val_64;
>> +               prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr =
>> +                       cpu_to_le64(val_64);
>>
>>                  /*
>>                   * Set the last TRB in the segment to
>> @@ -486,7 +487,7 @@ int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id)
>>          byte_64 = (uintptr_t)(virt_dev->out_ctx->bytes);
>>
>>          /* Point to output device context in dcbaa. */
>> -       ctrl->dcbaa->dev_context_ptrs[slot_id] = byte_64;
>> +       ctrl->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(byte_64);
>>
>>          xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
>>                           sizeof(__le64));
>> @@ -768,7 +769,7 @@ void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,
>>
>>          debug("route string %x\n", route);
>>   #endif
>> -       slot_ctx->dev_info |= route;
>> +       slot_ctx->dev_info |= cpu_to_le32(route);
>>
>>          switch (speed) {
>>          case USB_SPEED_SUPER:
>> --
> 
> Test results on Minnowmax which has one USB 2.0 port and one 3.0 port:
> 
> USB 2.0 flash drive inserted to USB 2.0 port: recognized, read/write OK
> USB 2.0 flash drive inserted to USB 3.0 port: recognized, read/write OK
> 
> USB 3.0 flash drive inserted to USB 2.0 port: recognized, read/write OK
> USB 3.0 flash drive inserted to USB 3.0 port: recognized, read/write OK
> 
> USB 2.0 flash drive connected to a USB 3.0 HUB inserted to USB 2.0
> port: recognized, read/write OK
> USB 2.0 flash drive connected to a USB 3.0 HUB inserted to USB 3.0
> port: recognized, read/write OK
> 
> USB 3.0 flash drive connected to a USB 3.0 HUB inserted to USB 2.0
> port: recognized, read/write OK
> USB 3.0 flash drive connected to a USB 3.0 HUB inserted to USB 3.0
> port: recognized, read/write OK
> 
> Tested-by: Bin Meng <bmeng.cn@gmail.com>

Very good. :)

Thanks,
Stefan
Bin Meng July 17, 2020, 1:46 p.m. UTC | #7
Hi Stefan,

On Fri, Jul 17, 2020 at 7:34 PM Stefan Roese <sr@denx.de> wrote:
>
> Hi Bin,
>
> On 17.07.20 13:18, Bin Meng wrote:
> > Hi Stefan,
> >
> > On Thu, Jul 2, 2020 at 4:47 PM Stefan Roese <sr@denx.de> wrote:
> >>
> >> While trying to use the U-Boot xHCI driver on the MIPS Octeon platform,
> >> which is big endian, I noticed that the driver is missing a few endian
> >> conversion calls. This patch adds these missing endian conversion
> >> calls.
> >>
> >> Signed-off-by: Stefan Roese <sr@denx.de>
> >> Cc: Bin Meng <bmeng.cn@gmail.com>
> >> Cc: Marek Vasut <marex@denx.de>
> >> ---
> >>
> >>   drivers/usb/host/xhci-mem.c | 9 +++++----
> >>   1 file changed, 5 insertions(+), 4 deletions(-)
> >>
> >> diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
> >> index 2d968aafb0..bd959b4093 100644
> >> --- a/drivers/usb/host/xhci-mem.c
> >> +++ b/drivers/usb/host/xhci-mem.c
> >> @@ -110,7 +110,7 @@ static void xhci_scratchpad_free(struct xhci_ctrl *ctrl)
> >>
> >>          ctrl->dcbaa->dev_context_ptrs[0] = 0;
> >>
> >> -       free((void *)(uintptr_t)ctrl->scratchpad->sp_array[0]);
> >> +       free((void *)le64_to_cpu(ctrl->scratchpad->sp_array[0]));
> >
> > There is a build warning for this:
> >
> > drivers/usb/host/xhci-mem.c: In function 'xhci_scratchpad_free':
> > drivers/usb/host/xhci-mem.c:113:7: warning: cast to pointer from
> > integer of different size [-Wint-to-pointer-cast]
> >    free((void *)le64_to_cpu(ctrl->scratchpad->sp_array[0]));
> >         ^
>
> So we need the (uintptr_t) here as well?
>
>    free((void *)(uintptr_t)le64_to_cpu(ctrl->scratchpad->sp_array[0]));

I think so.

>
> Should I send v2 for this patch?

Yes, please.

>
> >>          free(ctrl->scratchpad->sp_array);
> >>          free(ctrl->scratchpad);
> >>          ctrl->scratchpad = NULL;
> >> @@ -225,7 +225,8 @@ static void xhci_link_segments(struct xhci_segment *prev,
> >>          prev->next = next;
> >>          if (link_trbs) {
> >>                  val_64 = (uintptr_t)next->trbs;
> >> -               prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = val_64;
> >> +               prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr =
> >> +                       cpu_to_le64(val_64);
> >>
> >>                  /*
> >>                   * Set the last TRB in the segment to
> >> @@ -486,7 +487,7 @@ int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id)
> >>          byte_64 = (uintptr_t)(virt_dev->out_ctx->bytes);
> >>
> >>          /* Point to output device context in dcbaa. */
> >> -       ctrl->dcbaa->dev_context_ptrs[slot_id] = byte_64;
> >> +       ctrl->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(byte_64);
> >>
> >>          xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
> >>                           sizeof(__le64));
> >> @@ -768,7 +769,7 @@ void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,
> >>
> >>          debug("route string %x\n", route);
> >>   #endif
> >> -       slot_ctx->dev_info |= route;
> >> +       slot_ctx->dev_info |= cpu_to_le32(route);
> >>
> >>          switch (speed) {
> >>          case USB_SPEED_SUPER:
> >> --
> >
> > Test results on Minnowmax which has one USB 2.0 port and one 3.0 port:
> >
> > USB 2.0 flash drive inserted to USB 2.0 port: recognized, read/write OK
> > USB 2.0 flash drive inserted to USB 3.0 port: recognized, read/write OK
> >
> > USB 3.0 flash drive inserted to USB 2.0 port: recognized, read/write OK
> > USB 3.0 flash drive inserted to USB 3.0 port: recognized, read/write OK
> >
> > USB 2.0 flash drive connected to a USB 3.0 HUB inserted to USB 2.0
> > port: recognized, read/write OK
> > USB 2.0 flash drive connected to a USB 3.0 HUB inserted to USB 3.0
> > port: recognized, read/write OK
> >
> > USB 3.0 flash drive connected to a USB 3.0 HUB inserted to USB 2.0
> > port: recognized, read/write OK
> > USB 3.0 flash drive connected to a USB 3.0 HUB inserted to USB 3.0
> > port: recognized, read/write OK
> >
> > Tested-by: Bin Meng <bmeng.cn@gmail.com>
>
> Very good. :)

Regards,
Bin
diff mbox series

Patch

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 2d968aafb0..bd959b4093 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -110,7 +110,7 @@  static void xhci_scratchpad_free(struct xhci_ctrl *ctrl)
 
 	ctrl->dcbaa->dev_context_ptrs[0] = 0;
 
-	free((void *)(uintptr_t)ctrl->scratchpad->sp_array[0]);
+	free((void *)le64_to_cpu(ctrl->scratchpad->sp_array[0]));
 	free(ctrl->scratchpad->sp_array);
 	free(ctrl->scratchpad);
 	ctrl->scratchpad = NULL;
@@ -225,7 +225,8 @@  static void xhci_link_segments(struct xhci_segment *prev,
 	prev->next = next;
 	if (link_trbs) {
 		val_64 = (uintptr_t)next->trbs;
-		prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = val_64;
+		prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr =
+			cpu_to_le64(val_64);
 
 		/*
 		 * Set the last TRB in the segment to
@@ -486,7 +487,7 @@  int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id)
 	byte_64 = (uintptr_t)(virt_dev->out_ctx->bytes);
 
 	/* Point to output device context in dcbaa. */
-	ctrl->dcbaa->dev_context_ptrs[slot_id] = byte_64;
+	ctrl->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(byte_64);
 
 	xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
 			 sizeof(__le64));
@@ -768,7 +769,7 @@  void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,
 
 	debug("route string %x\n", route);
 #endif
-	slot_ctx->dev_info |= route;
+	slot_ctx->dev_info |= cpu_to_le32(route);
 
 	switch (speed) {
 	case USB_SPEED_SUPER: