Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp5763715imw; Wed, 20 Jul 2022 12:02:30 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vPz/RQuKz/3bh+N9s3JE+KagDsHGQfWfGBGTcTXwbKZRf3nNGTnUBHpGeMLM0W7tJ1JUbf X-Received: by 2002:a17:906:6a26:b0:72e:cee5:d1b0 with SMTP id qw38-20020a1709066a2600b0072ecee5d1b0mr34029738ejc.404.1658343750025; Wed, 20 Jul 2022 12:02:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658343750; cv=none; d=google.com; s=arc-20160816; b=kPbK7ZraUJdw4L2eRKLqYVe7S5Sj5+DUPe0QhGYazyg9PfD9xWgtysZJDL6pnmohHX 0rLLcJqWpBJ8zLAAm850s4Z2XsYF6S98HMo5PiTQdn13PbPfdBsPrLOgqDr4Y4SHNOxY VAG8ct9Mk1lGyzhpAmNOohDDJyXruBVmCe7xqK2/I3YoDK3Vp0rbhog7xEX8cWDFx8hC OPwQSbxS+FCSO2977MBZl3nMorGFyKhCfUCtPYs2bPouqmkBwp2Ocwv2tJsqTZOAZ8CG 8y3pcdBDb32eLJ/aJ8LDjjJajqfL93jD/4IHj/TQbNcAz1wvQVb4ZDcevP2Ui9CDPh1m M2yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=31aoo9bp1wIMFqWUajED9dirY3x3ItLRRWPOC75kWEc=; b=jiP09wibSgVm9zpxo0x+tC6/1JYYUGHdKTlLLgRDwI4BROmNfpdVqonT10hg55LEwY zY0oz6LTGpf4JXmal/Yk/HFtcFFg10WOEZNPNLP4aqlguncY4wSqiiMYAlWbbBGXF6MQ 9O+xROW65+fVFAEftESlgckB0A3HldQ06hC7jfVGk0Ob+owj6Ogu4TfDa0zLSB1as6Su rVjXMgG0SMXsnoViV+wrP8ALKig71+mfiVFzsCso3nIYDjecBoBUp50rOp8EC4OpLi+U iYEum4Ge7v0bxR57Mx8dytYKT9zW6+fVr3Pk6xHhV430VO6NkkNOJsmNDmSb9uFBtZcS xelA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcdkim header.b=XCLDFAz8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ne29-20020a1709077b9d00b0072b87c68bf1si26574507ejc.68.2022.07.20.12.01.56; Wed, 20 Jul 2022 12:02:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcdkim header.b=XCLDFAz8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230085AbiGTSvL (ORCPT + 99 others); Wed, 20 Jul 2022 14:51:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229622AbiGTSvI (ORCPT ); Wed, 20 Jul 2022 14:51:08 -0400 Received: from alexa-out-sd-02.qualcomm.com (alexa-out-sd-02.qualcomm.com [199.106.114.39]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1A3047BA0; Wed, 20 Jul 2022 11:51:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; i=@quicinc.com; q=dns/txt; s=qcdkim; t=1658343067; x=1689879067; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=31aoo9bp1wIMFqWUajED9dirY3x3ItLRRWPOC75kWEc=; b=XCLDFAz8WoqTmBKATRBTVV1gHWbGH2O3x/DaI55WHThdCbTumIIMDRkC pWtK4mOG9JgGPo//pLVShIHvvl0TXx+e7KbeNPcLunIm6iU/8dSYj7J/E HNe/qnAoxzsVQJPzSNLCrhpYwZkB0gyxipiGJBF1uhguzNYDvTg4UIZgm M=; Received: from unknown (HELO ironmsg02-sd.qualcomm.com) ([10.53.140.142]) by alexa-out-sd-02.qualcomm.com with ESMTP; 20 Jul 2022 11:51:06 -0700 X-QCInternal: smtphost Received: from nasanex01c.na.qualcomm.com ([10.47.97.222]) by ironmsg02-sd.qualcomm.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jul 2022 11:51:06 -0700 Received: from nalasex01b.na.qualcomm.com (10.47.209.197) by nasanex01c.na.qualcomm.com (10.47.97.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.22; Wed, 20 Jul 2022 11:51:06 -0700 Received: from [10.110.25.47] (10.80.80.8) by nalasex01b.na.qualcomm.com (10.47.209.197) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.22; Wed, 20 Jul 2022 11:51:05 -0700 Message-ID: <3e6867cc-489a-b626-ff9c-79615613b2dd@quicinc.com> Date: Wed, 20 Jul 2022 11:50:58 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: [PATCH v2 3/5] usb: dwc3: gadget: Adjust IRQ management during soft disconnect/connect Content-Language: en-US To: Thinh Nguyen , "balbi@kernel.org" , "gregkh@linuxfoundation.org" CC: "linux-kernel@vger.kernel.org" , "linux-usb@vger.kernel.org" , "quic_jackp@quicinc.com" References: <20220713003523.29309-1-quic_wcheng@quicinc.com> <20220713003523.29309-4-quic_wcheng@quicinc.com> From: Wesley Cheng In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) To nalasex01b.na.qualcomm.com (10.47.209.197) X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Thinh, On 7/14/2022 10:38 AM, Thinh Nguyen wrote: > On 7/12/2022, Wesley Cheng wrote: >> Local interrupts are currently being disabled as part of aquiring the >> spin lock before issuing the endxfer command. Leave interrupts enabled, so >> that EP0 events can continue to be processed. Also, ensure that there are >> no pending interrupts before attempting to handle any soft >> connect/disconnect. >> >> Fixes: 861c010a2ee1 ("usb: dwc3: gadget: Refactor pullup()") >> Signed-off-by: Wesley Cheng >> --- >> drivers/usb/dwc3/gadget.c | 21 ++++++++++++--------- >> 1 file changed, 12 insertions(+), 9 deletions(-) >> >> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c >> index a455f8d4631d..ee85b773e3fe 100644 >> --- a/drivers/usb/dwc3/gadget.c >> +++ b/drivers/usb/dwc3/gadget.c >> @@ -1674,6 +1674,7 @@ static int __dwc3_gadget_get_frame(struct dwc3 *dwc) >> static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt) >> { >> struct dwc3_gadget_ep_cmd_params params; >> + struct dwc3 *dwc = dep->dwc; >> u32 cmd; >> int ret; >> >> @@ -1682,7 +1683,9 @@ static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool int >> cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0; >> cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); >> memset(¶ms, 0, sizeof(params)); >> + spin_unlock(&dwc->lock); >> ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); >> + spin_lock(&dwc->lock); >> WARN_ON_ONCE(ret); >> dep->resource_index = 0; >> >> @@ -2029,12 +2032,11 @@ static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, >> struct dwc3_ep *dep = to_dwc3_ep(ep); >> struct dwc3 *dwc = dep->dwc; >> >> - unsigned long flags; >> int ret = 0; >> >> trace_dwc3_ep_dequeue(req); >> >> - spin_lock_irqsave(&dwc->lock, flags); >> + spin_lock(&dwc->lock); >> >> list_for_each_entry(r, &dep->cancelled_list, list) { >> if (r == req) >> @@ -2073,7 +2075,7 @@ static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, >> request, ep->name); >> ret = -EINVAL; >> out: >> - spin_unlock_irqrestore(&dwc->lock, flags); >> + spin_unlock(&dwc->lock); >> >> return ret; >> } >> @@ -2489,9 +2491,7 @@ static int __dwc3_gadget_start(struct dwc3 *dwc); >> >> static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc) >> { >> - unsigned long flags; >> - >> - spin_lock_irqsave(&dwc->lock, flags); >> + spin_lock(&dwc->lock); >> dwc->connected = false; >> >> /* >> @@ -2506,10 +2506,10 @@ static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc) >> >> reinit_completion(&dwc->ep0_in_setup); >> >> - spin_unlock_irqrestore(&dwc->lock, flags); >> + spin_unlock(&dwc->lock); >> ret = wait_for_completion_timeout(&dwc->ep0_in_setup, >> msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT)); >> - spin_lock_irqsave(&dwc->lock, flags); >> + spin_lock(&dwc->lock); >> if (ret == 0) >> dev_warn(dwc->dev, "timed out waiting for SETUP phase\n"); >> } >> @@ -2523,7 +2523,7 @@ static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc) >> */ >> dwc3_stop_active_transfers(dwc); >> __dwc3_gadget_stop(dwc); >> - spin_unlock_irqrestore(&dwc->lock, flags); >> + spin_unlock(&dwc->lock); >> >> /* >> * Note: if the GEVNTCOUNT indicates events in the event buffer, the >> @@ -2569,6 +2569,8 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) >> return 0; >> } >> >> + synchronize_irq(dwc->irq_gadget); >> + >> if (!is_on) { >> ret = dwc3_gadget_soft_disconnect(dwc); >> } else { >> @@ -3729,6 +3731,7 @@ void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, >> */ >> >> __dwc3_stop_active_transfer(dep, force, interrupt); >> + >> } >> >> static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) > > Hi Greg, > > Please don't pick up this patch yet. We're still in discussion with > this. I have some concern with unlocking/locking when sending End > Transfer command. For example, this patch may cause issues with > DWC3_EP_END_TRANSFER_PENDING checks. > > Hi Wesley, > > Did you try out my suggestion yet? > Just providing a quick update. So with your suggestion, I was able to consistently reproduce the controller halt issue after a day or so of testing. However, when I took a further look, I believe the problem is due to the DWC3 event handler: static void dwc3_endpoint_interrupt(struct dwc3 *dwc, const struct dwc3_event_depevt *event) { ... if (!(dep->flags & DWC3_EP_ENABLED)) { if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) return; /* Handle only EPCMDCMPLT when EP disabled */ if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) return; } The soft disconnect routine reached to the run/stop polling point, and I could see that DWC3_EP_DELAYED_STOP was set, and we got a xfercomplete event for the STATUS phase. However, since we exit early in the event handler (due to __dwc3_gadget_stop() being called and disabling EP0), the STATUS complete is never handled, and we do not issue the endxfer command. I don't think I saw this issue with my change, as we allowed the STATUS phase handling to happen BEFORE gadget stop was called (since I released the lock in the stop active transfers API). However, I think even with my approach, we'd eventually run into a possibility of this issue, as we aren't truly handling EP0 events while polling for the halted status due to the above. It was just reducing the chances. The scenario of this issue is coming because the host took a long time to complete the STATUS phase, so we ran into a "timed out waiting for SETUP phase," which allowed us to call the run/stop routine while we were not yet in the SETUP phase. I'm currently running a change to add a EP num check to this IF condition: if ((epnum > 1) && !(dep->flags & DWC3_EP_ENABLED)) { if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) return; /* Handle only EPCMDCMPLT when EP disabled */ if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) return; } Thanks Wesley Cheng