]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
usb: dwc3: gadget: Wait for ep0 xfers to complete during dequeue
authorThinh Nguyen <Thinh.Nguyen@synopsys.com>
Wed, 9 Mar 2022 20:54:02 +0000 (12:54 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 23 Feb 2024 07:55:11 +0000 (08:55 +0100)
[ Upstream commit e4cf6580ac740f766dae26203bd6311d353dcd42 ]

If a Setup packet is received but yet to DMA out, the controller will
not process the End Transfer command of any endpoint. Polling of its
DEPCMD.CmdAct may block setting up TRB for Setup packet, causing a
command timeout.

This may occur if the driver doesn’t service the completion interrupt of
the control status stage yet due to system latency, then it won’t
prepare TRB and start the transfer for the next Setup Stage. To the host
side, the control transfer had completed, and the host can send a new
Setup packet at this point.

In the meanwhile, if the driver receives an async call to dequeue a
request (triggering End Transfer) to any endpoint, then the driver will
service that End transfer first, blocking the control status stage
completion handler. Since no TRB is available for the Setup stage, the
Setup packet can’t be DMA’ed out and the End Transfer gets hung.

The driver must not block setting up of the Setup stage. So track and
only issue the End Transfer command only when there’s Setup TRB prepared
so that the controller can DMA out the Setup packet. Delay the End
transfer command if there's no Setup TRB available. This is applicable to
all DWC_usb3x IPs.

Co-developed-by: Wesley Cheng <quic_wcheng@quicinc.com>
Signed-off-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
Link: https://lore.kernel.org/r/20220309205402.4467-1-quic_wcheng@quicinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Stable-dep-of: 730e12fbec53 ("usb: dwc3: gadget: Handle EP0 request dequeuing properly")
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/usb/dwc3/core.h
drivers/usb/dwc3/ep0.c
drivers/usb/dwc3/gadget.c
drivers/usb/dwc3/gadget.h

index 3dcb5b744f7cec408cf33c75d5ecac443e78e59f..d64f7edc70c1372b2672eb3204671850e6d0a0fb 100644 (file)
@@ -722,6 +722,7 @@ struct dwc3_ep {
 #define DWC3_EP_FIRST_STREAM_PRIMED    BIT(10)
 #define DWC3_EP_PENDING_CLEAR_STALL    BIT(11)
 #define DWC3_EP_TXFIFO_RESIZED         BIT(12)
+#define DWC3_EP_DELAY_STOP             BIT(13)
 
        /* This last one is specific to EP0 */
 #define DWC3_EP0_DIR_IN                BIT(31)
index 52f2bfae46bce48115b6db71cd8fe308d7d2585a..34cb8662e1298d563c7d809d3e51d396ad939efe 100644 (file)
@@ -274,6 +274,7 @@ void dwc3_ep0_out_start(struct dwc3 *dwc)
 {
        struct dwc3_ep                  *dep;
        int                             ret;
+       int                             i;
 
        complete(&dwc->ep0_in_setup);
 
@@ -282,6 +283,19 @@ void dwc3_ep0_out_start(struct dwc3 *dwc)
                        DWC3_TRBCTL_CONTROL_SETUP, false);
        ret = dwc3_ep0_start_trans(dep);
        WARN_ON(ret < 0);
+       for (i = 2; i < DWC3_ENDPOINTS_NUM; i++) {
+               struct dwc3_ep *dwc3_ep;
+
+               dwc3_ep = dwc->eps[i];
+               if (!dwc3_ep)
+                       continue;
+
+               if (!(dwc3_ep->flags & DWC3_EP_DELAY_STOP))
+                       continue;
+
+               dwc3_ep->flags &= ~DWC3_EP_DELAY_STOP;
+               dwc3_stop_active_transfer(dwc3_ep, true, true);
+       }
 }
 
 static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
index 6188193e5ef4ce0883dab2a4eda36724d11a9956..3a663d71d79146eea2347349b46eff7d1f3c3730 100644 (file)
@@ -641,9 +641,6 @@ static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
        return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
 }
 
-static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
-               bool interrupt);
-
 /**
  * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value
  * @dwc: pointer to the DWC3 context
@@ -1891,6 +1888,7 @@ static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
         */
        if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
            (dep->flags & DWC3_EP_WEDGE) ||
+           (dep->flags & DWC3_EP_DELAY_STOP) ||
            (dep->flags & DWC3_EP_STALL)) {
                dep->flags |= DWC3_EP_DELAY_START;
                return 0;
@@ -2031,6 +2029,16 @@ static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
                if (r == req) {
                        struct dwc3_request *t;
 
+                       /*
+                        * If a Setup packet is received but yet to DMA out, the controller will
+                        * not process the End Transfer command of any endpoint. Polling of its
+                        * DEPCMD.CmdAct may block setting up TRB for Setup packet, causing a
+                        * timeout. Delay issuing the End Transfer command until the Setup TRB is
+                        * prepared.
+                        */
+                       if (dwc->ep0state != EP0_SETUP_PHASE && !dwc->delayed_status)
+                               dep->flags |= DWC3_EP_DELAY_STOP;
+
                        /* wait until it is processed */
                        dwc3_stop_active_transfer(dep, true, true);
 
@@ -2114,7 +2122,8 @@ int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
                list_for_each_entry_safe(req, tmp, &dep->started_list, list)
                        dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_STALLED);
 
-               if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
+               if (dep->flags & DWC3_EP_END_TRANSFER_PENDING ||
+                   (dep->flags & DWC3_EP_DELAY_STOP)) {
                        dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
                        return 0;
                }
@@ -3630,10 +3639,11 @@ static void dwc3_reset_gadget(struct dwc3 *dwc)
        }
 }
 
-static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
+void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
        bool interrupt)
 {
        if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
+           (dep->flags & DWC3_EP_DELAY_STOP) ||
            (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
                return;
 
index 77df4b6d6c13d68b5ccbeadc633608836b46cfb0..f763380e672edbbb605148185288bc4b8591a739 100644 (file)
@@ -116,6 +116,7 @@ int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
                gfp_t gfp_flags);
 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol);
 void dwc3_ep0_send_delayed_status(struct dwc3 *dwc);
+void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt);
 
 /**
  * dwc3_gadget_ep_get_transfer_index - Gets transfer index from HW