Re: [QUERY] Cold plugged USB device to Inateck PCIE USB card is not detected

From: Mathias Nyman
Date: Thu Aug 19 2021 - 09:15:49 EST


On 19.8.2021 10.54, Kishon Vijay Abraham I wrote:
> Hi All,
>
> I was trying to test PCIe USB card (Inateck) connected to AM64 EVM and
> J7200 EVM. Inateck uses Renesas uPD720201 USB3 host controller.
>
> So if I connect USB pendrive and then boot the board (cold plug), I
> don't see the pendrive getting detected. But if I remove and plug it
> again, it gets detected.
>
> For the cold plug case, I see this message
> "usb usb1-port3: couldn't allocate usb_device"
>
> It actually fails in
> xhci_alloc_dev()->xhci_queue_slot_control()->queue_command()->XHCI_STATE_HALTED
>
> I'm not familiar with xhci but it looks like port event is invoked
> before the controller is fully initialized (?).

Maybe this controller is capable of generating interrupts before it's running?

>
> I tried the following hack which kind of changes the sequence where
> xhci_start() and xhci_run_finished() is invoked before port_event() and
> with that I could see the pendrive enumerate successfully in cold plug case.
>
> diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
> index 228e3d4e1a9f..d19f27c46c6f 100644
> --- a/drivers/usb/core/hub.c
> +++ b/drivers/usb/core/hub.c
> @@ -1077,7 +1077,7 @@ static void hub_activate(struct usb_hub *hub, enum
> hub_activation_type type)
> INIT_DELAYED_WORK(&hub->init_work, hub_init_func2);
> queue_delayed_work(system_power_efficient_wq,
> &hub->init_work,
> - msecs_to_jiffies(delay));
> + msecs_to_jiffies(150));
>
> /* Suppress autosuspend until init is done */
> usb_autopm_get_interface_no_resume(
>
> Irrespective of the delay the port status looks correct and the modified
> delay only helps to change the flow.
>
> Adding other prints and delays also change the sequence and seems to
> detect the connected pendrive.
>
> Can someone provide hints on how to fix this properly?

Either keep xHC interrupts disabled until second (usb3) hcd is added, and
host is running. (haven't thought about side effects yet)
Or make sure we don't start polling the usb2 roothub until host is running.

Below code should return before port event handler starts roothub polling if
any xhci->xhc_state flag is set.
Untested, does it work for you?

diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index e676749f543b..9f4cc5c87b27 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -2024,6 +2024,8 @@ static void handle_port_status(struct xhci_hcd *xhci,
if (bogus_port_status)
return;

+ if (xhci->xhc_state != 0)
+ return;
/*
* xHCI port-status-change events occur when the "or" of all the
* status-change bits in the portsc register changes from 0 to 1.
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index f3dabd02382c..b171558956dd 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -646,9 +646,15 @@ int xhci_run(struct usb_hcd *hcd)
*/

hcd->uses_new_polling = 1;
- if (!usb_hcd_is_primary_hcd(hcd))
- return xhci_run_finished(xhci);
-
+ if (!usb_hcd_is_primary_hcd(hcd)) {
+ ret = xhci_run_finished(xhci);
+ if (ret)
+ return ret;
+ /* start polling usb2 roothub */
+ set_bit(HCD_FLAG_POLL_RH, &xhci->main_hcd->flags);
+ usb_hcd_poll_rh_status(xhci->main_hcd);
+ return ret;
+ }
xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run");

ret = xhci_try_enable_msi(hcd);

Thanks
-Mathias