Received: by 10.223.185.116 with SMTP id b49csp2605504wrg; Mon, 5 Mar 2018 05:56:26 -0800 (PST) X-Google-Smtp-Source: AG47ELscvKsCRA4vZrdGemRL1/SxUomD2CkMhMbyOPRdeSL4PkRKzWCamE1B3IDVujgizG+VI4Wq X-Received: by 10.98.86.151 with SMTP id h23mr15330178pfj.79.1520258186697; Mon, 05 Mar 2018 05:56:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520258186; cv=none; d=google.com; s=arc-20160816; b=ZrZbIck2utLtOdrWZyABNChkKSsofnFM226racgCBevQUpGFpQprOBJwPnCwyuYWpt SmH30JDZ0GuSeLEMdIlpSZvuKZJ9pl0HxfwljnCYlDcShKZSJSurJyq0f2HYRWGjW2M6 u4vbO+f1jBM1sEUBYEpw0NQyDXNsqBb0pB+QeMfvso6RGRQifeb0yEYrJEEH+nuGPLRM hlsBskU63x63KW42iKpdsBYoE6USwChqIduqkGU8o2IPSvYThB2bO7XsHKv//XniwiWa 1tGm56Z/Uw15ZhzxDEXBGk0xpR5LwoExv7Wbw9JHAxSQeo/uJuXJALX6itVjb+NXzEbX ZpwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=49FSfdgsiJ/WRGVGiaCRWzVwtnta2m79nRnk+Dh4Mpo=; b=NYR95yaUYutSR3kKIwhZDAslcPi4RBetIyLBu2qa4QU08PJGBosmYBhCnG31ydNO0I 0mn2OX7KXQjeiiEkDB7+AZfSvCgXaYxyA8mavbpmrzXF4yWOKDM/l73V0dJ3uSzQjSNs BvEbFJWW3NV+FTZEiMMmF1TyDmyf/ZI0TUhqK7s0Ocnbq7HEeuKa9HMHw2qfBRHR4AzC +OUOkXCRXiOIhzGwrO/mEytuTEJZ7FPfMLFmP6ZYY+akrHOuI57D1/i1P7qeYyEyYZQc HEXQaylS8ZDxga+PnlYmPuo5xp6+YqoZqfVcjFN55JCJQJn1x5+5FIsXVdZSYNgk4LQp VzHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Q/9QOwAl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z17si3977289pfh.136.2018.03.05.05.56.12; Mon, 05 Mar 2018 05:56:26 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Q/9QOwAl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934386AbeCELZS (ORCPT + 99 others); Mon, 5 Mar 2018 06:25:18 -0500 Received: from mail-ot0-f196.google.com ([74.125.82.196]:40703 "EHLO mail-ot0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933983AbeCELZO (ORCPT ); Mon, 5 Mar 2018 06:25:14 -0500 Received: by mail-ot0-f196.google.com with SMTP id l12so14564513otj.7 for ; Mon, 05 Mar 2018 03:25:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=49FSfdgsiJ/WRGVGiaCRWzVwtnta2m79nRnk+Dh4Mpo=; b=Q/9QOwAlM6JsfBrdRUk5aO0M2U4xTGPORAQb86PBJOF1B+VD8lYekfCiNEM259WGnx KBJKVn5CMzh1zk1/T6UNXs8EROYNDHY8Ev59HGRboeSIeuZ9vqMrFgDRMGyK04SAn9xN RXITj5JzqLbaKsrvzgKK8pu1fJ4DhOHXnjZYw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=49FSfdgsiJ/WRGVGiaCRWzVwtnta2m79nRnk+Dh4Mpo=; b=KU/HXZXnpM4gyZFPd0oqE704RJ+QPsR8IKY8RDrP8YcWyRaeUjkSZ4oenB/bWkybz0 siBT4KDhIoMdtHc7p9kasEzBiY8LeQ+VlfqGhEn/rOubk5MpPok7Pj94RN/9yVCeTWp7 MR08qy+6ds3kXG7CYfi6UvIB6LdrIVzTR+vv4rFrT2o6h3w39Vai7tRiDH37UiYnP5/y 4FEXPW5xEDaBPgv6oR3CNk6/hnBUmiJqynF11f3miGxmo8Trnm7PcSMkrX+ftxLLwPnM CjHwzITGlFl7oe4Gl5DQeZzA17HiYTyf5DJ+Z4uflr8lpoZgN1QHEcsKIhvfySeCBytA u+6g== X-Gm-Message-State: AElRT7EEwjwQ1yyKEy+h7IWk2uU5G4Ql45eaZ4R4nzJomDMUV0tlP3/v Ji718RXSNJK3IHngwCsPSnCMJNw/1U0DQc/wpQZYgPhJCB0= X-Received: by 10.157.60.112 with SMTP id j45mr10466069ote.141.1520249114041; Mon, 05 Mar 2018 03:25:14 -0800 (PST) MIME-Version: 1.0 Received: by 10.157.96.5 with HTTP; Mon, 5 Mar 2018 03:25:13 -0800 (PST) In-Reply-To: <8ec0485e-89af-568b-e34a-b0cd490817d0@ti.com> References: <1519730526-22274-1-git-send-email-rogerq@ti.com> <87sh9l5z4l.fsf@linux.intel.com> <94cd6377-1327-2309-8d69-6ab0de2bdfd4@ti.com> <87po4i3o1v.fsf@linux.intel.com> <87k1uq3ho6.fsf@linux.intel.com> <8ec0485e-89af-568b-e34a-b0cd490817d0@ti.com> From: Baolin Wang Date: Mon, 5 Mar 2018 19:25:13 +0800 Message-ID: Subject: Re: [PATCH] usb: dwc3: Prevent indefinite sleep in _dwc3_set_mode during suspend/resume To: Roger Quadros Cc: Felipe Balbi , USB , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 5 March 2018 at 19:14, Roger Quadros wrote: > On 05/03/18 13:06, Felipe Balbi wrote: >> >> Hi, >> >> Baolin Wang writes: >>>>> Roger Quadros writes: >>>>>>> Roger Quadros writes: >>>>>>>> In the following test we get stuck by sleeping forever in _dwc3_set_mode() >>>>>>>> after which dual-role switching doesn't work. >>>>>>>> >>>>>>>> On dra7-evm's dual-role port, >>>>>>>> - Load g_zero gadget driver and enumerate to host >>>>>>>> - suspend to mem >>>>>>>> - disconnect USB cable to host and connect otg cable with Pen drive in it. >>>>>>>> - resume system >>>>>>>> - we sleep indefinitely in _dwc3_set_mode due to. >>>>>>>> dwc3_gadget_exit()->usb_del_gadget_udc()->udc_stop()-> >>>>>>>> dwc3_gadget_stop()->wait_event_lock_irq() >>>>>>>> >>>>>>>> Let's clear the DWC3_EP_END_TRANSFER_PENDING flag on all endpoints >>>>>>>> so we don't wait in dwc3_gadget_stop(). >>>>>>>> >>>>>>>> Signed-off-by: Roger Quadros >>>>>>>> --- >>>>>>>> drivers/usb/dwc3/gadget.c | 14 ++++++++++++++ >>>>>>>> 1 file changed, 14 insertions(+) >>>>>>>> >>>>>>>> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c >>>>>>>> index 2bda4eb..0a360da 100644 >>>>>>>> --- a/drivers/usb/dwc3/gadget.c >>>>>>>> +++ b/drivers/usb/dwc3/gadget.c >>>>>>>> @@ -3273,6 +3273,20 @@ int dwc3_gadget_init(struct dwc3 *dwc) >>>>>>>> >>>>>>>> void dwc3_gadget_exit(struct dwc3 *dwc) >>>>>>>> { >>>>>>>> + int epnum; >>>>>>>> + unsigned long flags; >>>>>>>> + >>>>>>>> + spin_lock_irqsave(&dwc->lock, flags); >>>>>>>> + for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) { >>>>>>>> + struct dwc3_ep *dep = dwc->eps[epnum]; >>>>>>>> + >>>>>>>> + if (!dep) >>>>>>>> + continue; >>>>>>>> + >>>>>>>> + dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; >>>>>>>> + } >>>>>>>> + spin_unlock_irqrestore(&dwc->lock, flags); >>>>>>>> + >>>>>>>> usb_del_gadget_udc(&dwc->gadget); >>>>>>>> dwc3_gadget_free_endpoints(dwc); >>>>>>> >>>>>>> free endpoints is a better place for this. It's already going to free >>>>>>> the memory anyway. Might as well clear all flags to 0 there. >>>>>>> >>>>>> >>>>>> But it won't solve the deadlock issue. Since dwc3_gadget_free_endpoints() >>>>>> is called after usb_del_gadget_udc() and the deadlock happens when >>>>>> >>>>>> usb_del_gadget_udc()->udc_stop()->dwc3_gadget_stop()->wait_event_lock_irq() >>>>>> >>>>>> and DWC3_EP_END_TRANSFER_PENDING flag is set. >>>>> >>>>> indeed. Iterating twice over the entire endpoint list seems >>>>> wasteful. Perhaps we just shouldn't wait when removing the UDC since >>>>> that's essentially what this patch will do, right? If you clear the flag >>>>> before calling ->udc_stop(), this means the loop in dwc3_gadget_stop() >>>>> will do nothing. Might as well remove it. >>>>> >>>> >>>> This means that we will never wait for DWC3_EP_END_TRANSFER_PENDING to clear >>>> in dwc3_gadget_stop() like we used to. This is perfectly fine, right? >>>> >>>> It makes sense to me as dwc3_gadget_stop() calls __dwc3_gadget_stop() which >>>> masks all interrupts and nobody will ever clear that flag if it was set. >>> >>> I don't think so. It can not mask the endpoint events, please check >>> the events which will be masked in DEVTEN register. The reason why we >>> should wait for DWC3_EP_END_TRANSFER_PENDING to clear is that, >>> sometimes the DWC3_DEPEVT_EPCMDCMPLT event will be triggered later >>> than 100us, but now we may have freed the gadget irq which will cause >>> crash. >> >> We could mask command complete events as soon as ->udc_stop() is called, >> right? Hmm, actually, __dwc3_gadget_stop() already clears DEVTEN >> completely. > > But which bit in DEVTEN says Endpoint events are disabled? When we set up the DWC3_DEPCMD_ENDTRANSFER command in dwc3_stop_active_transfer(), we can do not set DWC3_DEPCMD_CMDIOC, then there will no endpoint command complete interrupts I think. cmd |= DWC3_DEPCMD_CMDIOC; > >> >> /me goes check databook >> >> At least on revision 2.60a of the databook, bit 10 is reserved. I wonder >> if that's the start of all the problems. Anybody has access to older and >> newer databook revisions so we can cross-check? >> > > I can access v2.40 and v3.10 books. > > bit 10 is reserved on both > > Differences in v2.4 vs v3.10 are: > > bit 8 reserved vs L1SUSPEN > bit 13 reserved vs StopOnDisconnectEn > bit 14 reserved vs L1WKUPEVTEN > > -- > cheers, > -roger > > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki -- Baolin.wang Best Regards