From patchwork Wed Jan 22 22:26:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 182924 Delivered-To: patches@linaro.org Received: by 2002:a92:1f12:0:0:0:0:0 with SMTP id i18csp31762ile; Wed, 22 Jan 2020 14:26:52 -0800 (PST) X-Received: by 2002:a65:56c9:: with SMTP id w9mr500192pgs.296.1579732012091; Wed, 22 Jan 2020 14:26:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579732012; cv=none; d=google.com; s=arc-20160816; b=idJLjS9CbWPGnL6DPOugua3MjM/hDvcXTRJEK1hab301CkVk4Hgh/Uei8wzW72Be06 vCpsoP8Vkxb5Tpk3xjK/5p+oQgg0+RdDouflXtdtfOVebTSz7h2KUaaN4p67tETAbJ0A 840hoojAKcLzbEZv5UU0LgVWMWR6XNx+6JuRkowgN8nmt98G1T8DWmk1tHcWqH4yZJVn tK3GyluL4wv+G17x8AkRxZKxqvjz8GrfUgcVg5mZSZDSkqQ2Ywzt/DCGwy/fFqHW1WBO Ch0Bn+5h0PXQIudVnM7ALIvUE2dQPJyae/fm0fC+IyvcuGt0xXQzYLdOnLfPELeBgnZx 9nYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JESjh6inBv92WxZqfb4czYKZ8FKdIT47e0CUpHsL9zw=; b=K1LgrFeU2ikrJXIntbdF9BMD/EqKAmn+uKNrRHleHgcw5VNfXXIAEOrEhMR0VX2MgU HQSBVcjZLDTpET6X8xyjKpVfsd56BJ5D+qRhxpqgRx8B3hSmTSLOwcUVvt6QJMo9J1+1 lgBUssf3bTBAUrZWIE5hRKePKr1YHXmf4lGAa9s0nhIGOTM3TpGYLZ6gKKky50HRMcW7 TwYAJXGmTmQcJ2sBcOLDQV93iUTdVCN2U8aoFMo3S9oEXXXVpHrsl3z41mWDbEVsMlzF xvOYLWl7uOInWKk7SC2Yg+lRdiFEZLQW1ITc/8A2Qm38N4LrXzCEYh8s4afBkFVJTMpT SbPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RsGmOvIt; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id v62sor47233622pgd.24.2020.01.22.14.26.51 for (Google Transport Security); Wed, 22 Jan 2020 14:26:52 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RsGmOvIt; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=JESjh6inBv92WxZqfb4czYKZ8FKdIT47e0CUpHsL9zw=; b=RsGmOvIt+Bzr6i4aIVzintUFXr+6XX+aQJNI6juKMss4wBAunzxItSZlGbXw7ha+3c V5pNjplf1oyvhJV3/UY1eBclT7Bav7RGheW0wzEZ34ThAk5YhiMCUCN0jmmS1fz8i5Rq 3WBNsJe0+drVR3Fi8glb78/uzKQBNkHkind1gitZeqjlkXBg0eabjb5ILlDjiP473ux2 LZ5J1BPDiBf1L0IJfDdw5BDmpkkp2vVEuWl426uWWk4c+8E+pLHAJj5YK1M0M+OeLBsV MxdvsQOkXmBtkojh4QldDc4RcnkQiSuotRNJcvmGpFH5ESJmrVx/O4yKvoYr4dhdqkxW 1p0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=JESjh6inBv92WxZqfb4czYKZ8FKdIT47e0CUpHsL9zw=; b=N0ehFIuPY4/9r4AKwGnV/WDxbleg98CH/rksWQpV0npFQnQArY6t/ag2XOsuBup25k v9uZEhhV/kZv3KJ+uG20fufV1e9IyTl7+ZPGvq8u0NN0fTaPqFilwhKoY4Sy1BuQ2uBy YTx43AWmkBdvTgS6BkOgNJA6BHF0zIjyE7JrDXXmydN0AhIQDTAi+Nq11W3bnxPQQPHw vCfTZ0Sx/bL7hxe9SK1x6yh/6xX5EoqEYUXdoNUX4MqyejI16E/UHHFdidFUyts3uYE5 iXUtyzJDpQNk1sicR99BroXOFJI9yn8+wW2WD8s5LCFU68d/yjrA8t2xtN4MxvbzXFb6 XChQ== X-Gm-Message-State: APjAAAWWSfsL41lMLZzdTTqfU9ydkA0PaP9cEuFfeTWH0HDZsvEPQLE0 3IWolYoKRV3/tf6dJZruxm8AhCTj X-Google-Smtp-Source: APXvYqyHFnzdcPwFmtLdIrSYOuAiwsCTGRIQwQ9Jlfx8tJ4RGPs5SBRmOuXWib6jnxyijCN3yYVuiA== X-Received: by 2002:a63:111e:: with SMTP id g30mr541062pgl.251.1579732011742; Wed, 22 Jan 2020 14:26:51 -0800 (PST) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id a28sm47793509pfh.119.2020.01.22.14.26.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jan 2020 14:26:51 -0800 (PST) From: John Stultz To: lkml Cc: Anurag Kumar Vulisha , Felipe Balbi , Yang Fei , Thinh Nguyen , Tejas Joglekar , Andrzej Pietrasiewicz , Jack Pham , Todd Kjos , Greg KH , Linux USB List , stable , John Stultz Subject: [RFC][PATCH 1/2] usb: dwc3: gadget: Check for IOC/LST bit in both event->status and TRB->ctrl fields Date: Wed, 22 Jan 2020 22:26:44 +0000 Message-Id: <20200122222645.38805-2-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200122222645.38805-1-john.stultz@linaro.org> References: <20200122222645.38805-1-john.stultz@linaro.org> From: Anurag Kumar Vulisha The present code in dwc3_gadget_ep_reclaim_completed_trb() will check for IOC/LST bit in the event->status and returns if IOC/LST bit is set. This logic doesn't work if multiple TRBs are queued per request and the IOC/LST bit is set on the last TRB of that request. Consider an example where a queued request has multiple queued TRBs and IOC/LST bit is set only for the last TRB. In this case, the Core generates XferComplete/XferInProgress events only for the last TRB (since IOC/LST are set only for the last TRB). As per the logic in dwc3_gadget_ep_reclaim_completed_trb() event->status is checked for IOC/LST bit and returns on the first TRB. This makes the remaining TRBs left unhandled. To aviod this, changed the code to check for IOC/LST bits in both event->status & TRB->ctrl. This patch does the same. At a practical level, this patch resolves USB transfer stalls seen with adb on dwc3 based HiKey960 after functionfs gadget added scatter-gather support around v4.20. Cc: Felipe Balbi Cc: Yang Fei Cc: Thinh Nguyen Cc: Tejas Joglekar Cc: Andrzej Pietrasiewicz Cc: Jack Pham Cc: Todd Kjos Cc: Greg KH Cc: Linux USB List Cc: stable Tested-by: Tejas Joglekar Reviewed-by: Thinh Nguyen Signed-off-by: Anurag Kumar Vulisha [jstultz: forward ported to mainline, added note to commit log] Signed-off-by: John Stultz --- drivers/usb/dwc3/gadget.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) -- 2.17.1 diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index 154f3f3e8cff..1edce3bbb55c 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -2420,7 +2420,12 @@ static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep, if (event->status & DEPEVT_STATUS_SHORT && !chain) return 1; - if (event->status & DEPEVT_STATUS_IOC) + if ((event->status & DEPEVT_STATUS_IOC) && + (trb->ctrl & DWC3_TRB_CTRL_IOC)) + return 1; + + if ((event->status & DEPEVT_STATUS_LST) && + (trb->ctrl & DWC3_TRB_CTRL_LST)) return 1; return 0; From patchwork Wed Jan 22 22:26:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 182925 Delivered-To: patches@linaro.org Received: by 2002:a92:1f12:0:0:0:0:0 with SMTP id i18csp31773ile; Wed, 22 Jan 2020 14:26:53 -0800 (PST) X-Received: by 2002:a63:646:: with SMTP id 67mr516786pgg.150.1579732013191; Wed, 22 Jan 2020 14:26:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579732013; cv=none; d=google.com; s=arc-20160816; b=b30UrE8tQbCKYF08Rr4n/IWxP8vSV/9+YkNa8/t92EjwvSbk0bhsocZDVyQiBSJZKU BYybsGcdRO9WfQ2thfw3nlqRnLacTlYQVbqAb3lydXBe4vVaKuYeaew8Ycfo1PxZ07r1 gwOP4oM0SoaiTt5MJUP8FBsBjX1qDexJJPpq3qdMBsqGsHiGpctTDz6Ko4ppOec5HbLZ Fy71i+55Sn5OspgtHW4aseoA4RRPIFr8pUR75zTFHOEkC9V6lLcKn74hC5ClljlwZgqA 9wLecwLGDcmgK7lsRzquC2aV5ftC/sVeqvFQ3HEYwDHfR1ivlfVJtLTthtuZdokUJ3Zn 9ROA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=swWbt3aJL3q8h4lz+/4twKGtWH3pxZbO2/WppH1VqRE=; b=RMkQ7TOQQ9xc3mUJhUoixW2BAnu3+wTzSrbXp0LHo9oiZRRLnrx2OOHo9zUdgz0oxh bZmZoZtRKCqlZ5LgpUvjNJVcCM3RLwUoLfgSdwSHdM8Kz4tNm5+a1AdYsza9xoTAPft8 nAfkarsGr9r1j3ElTSj8d7dD4z5UIijah/bOhZ96u83RfJZCbMSmw0gK8uERIPSLchBC DUup6RWHplTsOS8AyB7JxOufB8TxTwebNxwUnZvy71a0EiTXtB946YQJQPGrhiipOw3B KS7fTnFjvXy0kkDQpg0GAb6zSvCB2VKron3rtMyrFPi7vSv05PYxDXDyOn9qA6WLrIMl vZyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=K0ohNbHM; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id x7sor46653418pfb.40.2020.01.22.14.26.53 for (Google Transport Security); Wed, 22 Jan 2020 14:26:53 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=K0ohNbHM; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=swWbt3aJL3q8h4lz+/4twKGtWH3pxZbO2/WppH1VqRE=; b=K0ohNbHMOAsVb4M6Ck8cIf3BM8XchMabhMit1TMM4M9rA7DOOGd/sgBcVKqk4Dkfyl MzkiSn6mrkDn4Rzo4C7qixNHdL/CFF78glRDZyCApSy9ECeubN35sr+56Jowj5rwc1qX P0nCx7C1QsIOrrIqxRuZIJCWeIkHckFcMN5GGbk1NVw9Y/JbCuMS4i3CzwgBPRDfpjcq SBIyfImGz9ImHeT0LopgirqAnevkSzpfLi+UMd7UT0MkGdqNkR32HmrLKL2wP3KNBLbp aDB8LJZb1z0/CHBTLZbYRBbK4qL9hHXMe7uTtYP/yX1g1i8LefORegdQRnX/eR1YsnR8 UbNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=swWbt3aJL3q8h4lz+/4twKGtWH3pxZbO2/WppH1VqRE=; b=tB9X1ybzOJ65qR6yOLUXZubY2AX4Ri5zelE0tjRw6r6M/VGS7JzBPiOz/Ked6aX3P5 Dak1gJ8weTIO/U4KwhMbtWKJInGblYmsCRo8rduxM716omK2t6MlsOL+5qeACRB1Acpa g0cqVvYNNZX+I2XHo/7Mo5DHcoA6+6EXh8DP/6R1v7qUUaZQF7T9pWglUHSaV0tmSHt7 t132h+TNHRIESEeNa/2VhjyoexJXrOIOZQ3IturH7S8hlKSIBycakHXRggNmnb1k4BHF zJH6k3Tp2/41d45lOppWqiQP84ipDEf9Zmu9hRPSRLY171gxKniXJsdUE1fnQGre12VA wSwQ== X-Gm-Message-State: APjAAAXPy1hpxHRDfslj79Gp6Axx4NAXyJmoF/rPxEzhAdXV/uD9UgLB +HRAtqhZu02eIA/1WBdDiz6PBIct X-Google-Smtp-Source: APXvYqxjWJnBDQWDLfs492qyC8m1YgOMNGISqY5eu6Q4wsVKzbqlsokkc0qtDmoQD/wqOD4EOckAOw== X-Received: by 2002:a62:3892:: with SMTP id f140mr4552209pfa.190.1579732012776; Wed, 22 Jan 2020 14:26:52 -0800 (PST) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id a28sm47793509pfh.119.2020.01.22.14.26.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jan 2020 14:26:52 -0800 (PST) From: John Stultz To: lkml Cc: Anurag Kumar Vulisha , Felipe Balbi , Yang Fei , Thinh Nguyen , Tejas Joglekar , Andrzej Pietrasiewicz , Jack Pham , Todd Kjos , Greg KH , Linux USB List , stable , John Stultz Subject: [RFC][PATCH 2/2] usb: dwc3: gadget: Correct the logic for finding last SG entry Date: Wed, 22 Jan 2020 22:26:45 +0000 Message-Id: <20200122222645.38805-3-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200122222645.38805-1-john.stultz@linaro.org> References: <20200122222645.38805-1-john.stultz@linaro.org> From: Anurag Kumar Vulisha As a process of preparing TRBs usb_gadget_map_request_by_dev() is called from dwc3_prepare_trbs() for mapping the request. This will call dma_map_sg() if req->num_sgs are greater than 0. dma_map_sg() will map the sg entries in sglist and return the number of mapped SGs. As a part of mapping, some sg entries having contigous memory may be merged together into a single sg (when IOMMU used). So, the number of mapped sg entries may not be equal to the number of orginal sg entries in the request (req->num_sgs). As a part of preparing the TRBs, dwc3_prepare_one_trb_sg() iterates over the sg entries present in the sglist and calls sg_is_last() to identify whether the sg entry is last and set IOC bit for the last sg entry. The sg_is_last() determines last sg if SG_END is set in sg->page_link. When IOMMU used, dma_map_sg() merges 2 or more sgs into a single sg and it doesn't retain the page_link properties. Because of this reason the sg_is_last() may not find SG_END and thus resulting in IOC bit never getting set. For example: Consider a request having 8 sg entries with each entry having a length of 4096 bytes. Assume that sg1 & sg2, sg3 & sg4, sg5 & sg6, sg7 & sg8 are having contigous memory regions. Before calling dma_map_sg(): sg1-->sg2-->sg3-->sg4-->sg6-->sg7-->sg8 dma_length: 4K 4K 4K 4K 4K 4K 4K SG_END: False False False False False False True num_sgs = 8 num_mapped_sgs = 0 The dma_map_sg() merges sg1 & sg2 memory regions into sg1->dma_address. Similarly sg3 & sg4 into sg2->dma_address, sg5 & sg6 into the sg3->dma_address and sg6 & sg8 into sg4->dma_address. Here the memory regions are merged but the page_link properties like SG_END are not retained into the merged sgs. After calling dma_map_sg(); sg1-->sg2-->sg3-->sg4-->sg6-->sg7-->sg8 dma_length: 8K 8K 8K 8K 0K 0K 0K SG_END: False False False False False False True num_sgs = 8 num_mapped_sgs = 4 After calling dma_map_sg(), sg1,sg2,sg3,sg4 are having dma_length of 8096 bytes each and remaining sg4,sg5,sg6,sg7 are having 0 bytes of dma_length. After dma_map_sg() is performed dma_perpare_trb_sg() iterates on all sg entries and sets IOC bit only for the sg8 (since sg_is_last() returns true only for sg8). But after calling dma_map_sg() the valid data are present only till sg4 and the IOC bit should be set for sg4 TRB only (which is not happening in the present code) The above mentioned issue can be fixed by determining last sg based on the req->num_queued_sgs instead of sg_is_last(). If (req->num_queued_sgs + 1) is equal to req->num_mapped_sgs, then this sg is the last sg. In the above example, the dwc3 driver has already queued 3 sgs (upto sg3), so the num_queued_sgs = 3. On preparing the next sg (i.e sg4), check for last sg (num_queued_sgs + 1) == num_mapped_sgs becomes true. So, the driver sets IOC bit for sg4. This patch does the same. At a practical level, this patch resolves USB transfer stalls seen with adb on dwc3 based db845c, pixel3 and other qcom hardware after functionfs gadget added scatter-gather support around v4.20. Cc: Felipe Balbi Cc: Yang Fei Cc: Thinh Nguyen Cc: Tejas Joglekar Cc: Andrzej Pietrasiewicz Cc: Jack Pham Cc: Todd Kjos Cc: Greg KH Cc: Linux USB List Cc: stable Signed-off-by: Anurag Kumar Vulisha [jstultz: Add note to end of commit message on specific issue this resovles] Signed-off-by: John Stultz --- drivers/usb/dwc3/gadget.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- 2.17.1 diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index 1edce3bbb55c..30a80bc97cfe 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -1068,7 +1068,7 @@ static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep, unsigned int rem = length % maxp; unsigned chain = true; - if (sg_is_last(s)) + if ((req->num_queued_sgs + 1) == req->request.num_mapped_sgs) chain = false; if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {