From patchwork Mon Apr 18 11:39:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Roger Quadros X-Patchwork-Id: 66018 Delivered-To: patch@linaro.org Received: by 10.140.93.198 with SMTP id d64csp1232356qge; Mon, 18 Apr 2016 04:40:12 -0700 (PDT) X-Received: by 10.28.5.85 with SMTP id 82mr18667088wmf.26.1460979612786; Mon, 18 Apr 2016 04:40:12 -0700 (PDT) Return-Path: Received: from theia.denx.de (theia.denx.de. [85.214.87.163]) by mx.google.com with ESMTP id k65si26221234wmb.11.2016.04.18.04.40.12; Mon, 18 Apr 2016 04:40:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 85.214.87.163 as permitted sender) client-ip=85.214.87.163; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 85.214.87.163 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 0DAFFA760E; Mon, 18 Apr 2016 13:40:11 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 0-dxCgy1Wwwa; Mon, 18 Apr 2016 13:40:10 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 64529A75DE; Mon, 18 Apr 2016 13:40:10 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 10D07A75DE for ; Mon, 18 Apr 2016 13:40:06 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id qgppKvUDNQrN for ; Mon, 18 Apr 2016 13:40:05 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from comal.ext.ti.com (comal.ext.ti.com [198.47.26.152]) by theia.denx.de (Postfix) with ESMTPS id 4298DA7498 for ; Mon, 18 Apr 2016 13:40:02 +0200 (CEST) Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by comal.ext.ti.com (8.13.7/8.13.7) with ESMTP id u3IBduMU016176; Mon, 18 Apr 2016 06:39:56 -0500 Received: from DFLE73.ent.ti.com (dfle73.ent.ti.com [128.247.5.110]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id u3IBdtjb023616; Mon, 18 Apr 2016 06:39:55 -0500 Received: from dflp33.itg.ti.com (10.64.6.16) by DFLE73.ent.ti.com (128.247.5.110) with Microsoft SMTP Server id 14.3.224.2; Mon, 18 Apr 2016 06:39:55 -0500 Received: from [192.168.2.6] (ileax41-snat.itg.ti.com [10.172.224.153]) by dflp33.itg.ti.com (8.14.3/8.13.8) with ESMTP id u3IBdqCq018525; Mon, 18 Apr 2016 06:39:53 -0500 To: Steve Rae , Marek Vasut , Steve Rae , Lukasz Majewski References: <1460548862-27625-1-git-send-email-semen.protsenko@linaro.org> <570E3C67.6080200@ti.com> <570F6E61.10206@ti.com> <57149337.30601@ti.com> From: Roger Quadros Message-ID: <5714C788.5020301@ti.com> Date: Mon, 18 Apr 2016 14:39:52 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: <57149337.30601@ti.com> Cc: Praneeth Bajjuri , Tom Rini , U-Boot Mailing List Subject: Re: [U-Boot] [PATCH] fastboot: Fix OUT transaction length alignment X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" +Lukazs On 18/04/16 10:56, Roger Quadros wrote: > On 15/04/16 22:44, Steve Rae wrote: >> >> >> On Thu, Apr 14, 2016 at 3:18 AM, Roger Quadros > wrote: >> >> Hi, >> >> On 13/04/16 19:56, Sam Protsenko wrote: >> > On Wed, Apr 13, 2016 at 3:32 PM, Roger Quadros > wrote: >> >> Hi, >> >> >> >> On 13/04/16 15:01, Semen Protsenko wrote: >> >>> From: Sam Protsenko > >> >>> >> >>> Some UDC controllers may require buffer size to be aligned to >> >>> wMaxPacketSize. It's indicated by gadget->quirk_ep_out_aligned_size >> >>> field being set to "true" (in UDC driver code). In that case >> >>> rx_bytes_expected must be aligned to wMaxPacket size, otherwise stuck on >> >>> transaction will happen. For example, it's required by DWC3 controller >> >>> data manual: >> >>> >> >>> section 8.2.3.3 Buffer Size Rules and Zero-Length Packets: >> >>> >> >>> For OUT endpoints, the following rules apply: >> >>> - The BUFSIZ field must be ≥ 1 byte. >> >>> - The total size of a Buffer Descriptor must be a multiple of >> >>> MaxPacketSize >> >>> - A received zero-length packet still requires a MaxPacketSize buffer. >> >>> Therefore, if the expected amount of data to be received is a multiple >> >>> of MaxPacketSize, software should add MaxPacketSize bytes to the buffer >> >>> to sink a possible zero-length packet at the end of the transfer. >> >>> >> >>> But other UDC controllers don't need such alignment, so mentioned field >> >>> is set to "false". If buffer size is aligned to wMaxPacketSize, those >> >>> controllers may stuck on transaction. The example is DWC2. >> >>> >> >>> This patch checks gadget->quirk_ep_out_aligned_size field and aligns >> >>> rx_bytes_expected to wMaxPacketSize only when it's needed. >> >>> >> >>> Signed-off-by: Sam Protsenko > >> >>> --- >> >>> drivers/usb/gadget/f_fastboot.c | 9 +++++++++ >> >>> 1 file changed, 9 insertions(+) >> >>> >> >>> diff --git a/drivers/usb/gadget/f_fastboot.c b/drivers/usb/gadget/f_fastboot.c >> >>> index 2e87fee..54dcce0 100644 >> >>> --- a/drivers/usb/gadget/f_fastboot.c >> >>> +++ b/drivers/usb/gadget/f_fastboot.c >> >>> @@ -58,6 +58,7 @@ static unsigned int fastboot_flash_session_id; >> >>> static unsigned int download_size; >> >>> static unsigned int download_bytes; >> >>> static bool is_high_speed; >> >>> +static bool quirk_ep_out_aligned_size; >> >>> >> >>> static struct usb_endpoint_descriptor fs_ep_in = { >> >>> .bLength = USB_DT_ENDPOINT_SIZE, >> >>> @@ -240,6 +241,8 @@ static int fastboot_set_alt(struct usb_function *f, >> >>> debug("%s: func: %s intf: %d alt: %d\n", >> >>> __func__, f->name, interface, alt); >> >>> >> >>> + quirk_ep_out_aligned_size = gadget->quirk_ep_out_aligned_size; >> >>> + >> >>> /* make sure we don't enable the ep twice */ >> >>> if (gadget->speed == USB_SPEED_HIGH) { >> >>> ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out); >> >>> @@ -435,12 +438,18 @@ static unsigned int rx_bytes_expected(unsigned int maxpacket) >> >>> return 0; >> >>> if (rx_remain > EP_BUFFER_SIZE) >> >>> return EP_BUFFER_SIZE; >> >>> + >> >>> + if (!quirk_ep_out_aligned_size) >> >>> + goto out; >> >>> + >> >>> if (rx_remain < maxpacket) { >> >>> rx_remain = maxpacket; >> >>> } else if (rx_remain % maxpacket != 0) { >> >>> rem = rx_remain % maxpacket; >> >>> rx_remain = rx_remain + (maxpacket - rem); >> >>> } >> >>> + >> >>> +out: >> >>> return rx_remain; >> >>> } >> >>> >> >>> >> >> >> >> Why do we need a special flag for this driver if other drivers e.g. mass storage >> >> are doing perfectly fine without it? >> >> >> > >> > I don't know how it works in other gadgets, but please see this patch >> > in kernel: [1]. That patch is doing just the same as I did (and also >> > in gadget code), using usb_ep_align_maybe() function for alignment. >> >> NOTE: there haven't been such quirks in the kernel drivers except for that one >> driver that has a user mode interface and needs more moral policing for user >> provided buffers. So that example is not something we should be using as reference. >> Our buffers are alreay aligned to maxpacket size. The only thing we need to worry >> about is the length of the last transaction that is not integral multiple of >> maxpacket size. >> >> If my understanding is right all USB controllers should work fine with >> bulk OUT requests that are integral multiple of maxpacket size. >> So we shouldn't be needing any quirk flags. >> >> > >> >> This patch is just covering up the real problem, by bypassing the faulty code >> >> with a flag. >> >> >> >> The buffer size is EP_BUFFER_SIZE and is already aligned to wMaxPacketSize so >> >> the problem shouldn't have happened in the first place. But it is happening >> >> indicating something else is wrong. >> >> >> > >> > There is what I'm observing on platform with DWC3 controller: >> > - when doing "fastboot flash xloader MLO": >> > - the whole size of data to send is 70964 bytes >> > - the size of all packets (except of last packet) is 4096 bytes >> > - so those are being sent just fine (in req->complete, which is >> > rx_handler_dl_image() function) >> > - but last packet has size of 1332 bytes (because 70964 % 4096 = 1332) >> > - when its req->length is aligned to wMaxPacketSize (so it's 1536 >> > bytes): after we send it using usb_ep_queue(), the req->complete >> > callback is called one last time and we see that transmission is >> > finished (download_bytes >= download_size) >> > - but when its req->length is not aligned to wMaxPacketSize (so it's >> > 1332 bytes): req->complete callback is not called last time, so >> > transaction is not finished and we are stuck in "fastboot flash" >> > >> > So I guess the issue is real and related to DWC3 quirk. If you have >> > any thoughts regarding other possible causes of this problem -- please >> > share. I can't predict all possible causes as I'm not USB expert. >> >> I've tried to clean up the bulk out handling code in the below patch. >> Note you will need to apply this on top of the 2 patches I sent earlier. >> https://patchwork.ozlabs.org/patch/609417/ >> https://patchwork.ozlabs.org/patch/609896/ >> >> Steve, do let me know if it works for you. If it doesn't then we need to figure out why. >> Can you please share details about the USB controller on your board? >> Does it not like OUT requests that are aligned to maxpacket size? >> What does ep->maxpacket show for your board? >> >> >> This (still) does not work.... >> - using the standard U-Boot DWC2 driver, >> - do not know if it doesn't like "OUT requests that are aligned to maxpacket size" -- perhaps @Lukasz could answer this.... >> - ep->maxpacket is 512 >> >> with logging in "drivers/usb/gadget/dwc2_udc_otg.c" enabled, output is (for the last transactions...): >> >> *** dwc2_udc_irq : GINTSTS=0x14088028(on state WAIT_FOR_SETUP), GINTMSK : 0x800c3800,DAINT : 0x40000, DAINTMSK : 0x50003 >> *** process_ep_out_intr: EP OUT interrupt : DAINT = 0x40000 >> EP2-OUT : DOEPINT = 0x2011 >> complete_rx: RX DMA done : ep = 2, rx bytes = 4096/4096, is_short = 0, DOEPTSIZ = 0x0, remained bytes = 4096 >> complete_rx: Next Rx request start... >> setdma_rx: EP2 RX DMA start : DOEPDMA = 0xffb84f80,DOEPTSIZ = 0x401000, DOEPCTL = 0x80098200 >> buf = 0xffb84f80, pktcnt = 8, xfersize = 4096 > > OK so we asked for 4096 bytes and looks like we received 3968 bytes, > which is probably the end of transfer. > >> >> *** dwc2_udc_irq : GINTSTS=0x14088028(on state WAIT_FOR_SETUP), GINTMSK : 0x800c3800,DAINT : 0x40000, DAINTMSK : 0x50003 >> *** process_ep_out_intr: EP OUT interrupt : DAINT = 0x40000 >> EP2-OUT : DOEPINT = 0x2011 >> complete_rx: RX DMA done : ep = 2, rx bytes = 3968/4096, is_short = 0, DOEPTSIZ = 0x80, remained bytes = 3968 >> setdma_rx: EP2 RX DMA start : DOEPDMA = 0xffb85f00,DOEPTSIZ = 0x80080, DOEPCTL = 0x80098200 >> buf = 0xffb85f00, pktcnt = 1, xfersize = 128 >> >>>>>> and it hangs here!!! > > The dwc2 driver should have returned then and not queued another 128 bytes. > IMO there is a bug in the dwc2 driver. > > 3968 = 7 x 512 + 384 > This means the last packet (384 bytes) was a short packet and it signals end of transfer > so the dwc2 driver shouldn't be queuing another transfer. It should end the usb ep_queue > request. > > So, question to dwc2 mantainers. > Can we modify dwc2 driver to not automatically queue a pending transfer if the transfer > ended in a short packet? BTW, this is mentioned in the USB Specification. Ref: USB2.0 Secification: Section. 5.3.2 Pipes "An IRP (I/O request packet) may require multiple data payloads to move the client data over the bus. The data payloads for such a multiple data payload IRP are expected to be of the maximum packet size until the last data payload that contains the remainder of the overall IRP. See the description of each transfer type for more details. For such an IRP, short packets (i.e., less than maximum-sized data payloads) on input that do not completely fill an IRP data buffer can have one of two possible meanings, depending upon the expectations of a client: • A client can expect a variable-sized amount of data in an IRP. In this case, a short packet that does not fill an IRP data buffer can be used simply as an in-band delimiter to indicate “end of unit of data.” The IRP should be retired without error and the Host Controller should advance to the next IRP. • A client can expect a specific-sized amount of data. In this case, a short packet that does not fill an IRP data buffer is an indication of an error. The IRP should be retired, the pipe should be stalled, and any pending IRPs associated with the pipe should also be retired." I think we want the controller to behave as the first case since we haven't set the short_not_ok flag in the USB request. So the below patch to the dwc2 driver should fix it. --- cheers, -roger diff --git a/drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c b/drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c index bce9c30..a31d875 100644 --- a/drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c +++ b/drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c @@ -229,7 +229,7 @@ static void complete_rx(struct dwc2_udc *dev, u8 ep_num) ROUND(xfer_size, CONFIG_SYS_CACHELINE_SIZE)); req->req.actual += min(xfer_size, req->req.length - req->req.actual); - is_short = (xfer_size < ep->ep.maxpacket); + is_short = xfer_size % ep->ep.maxpacket; debug_cond(DEBUG_OUT_EP != 0, "%s: RX DMA done : ep = %d, rx bytes = %d/%d, "