Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp34935ybj; Fri, 8 May 2020 06:01:56 -0700 (PDT) X-Google-Smtp-Source: APiQypKvlABkW2x5vdOUVPrKDeEZjFLglxs3qmIfCEdummYa2vQabpzYYY6uCpUMSHCqJVtoikJH X-Received: by 2002:a05:6402:3129:: with SMTP id dd9mr2073955edb.121.1588942916305; Fri, 08 May 2020 06:01:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588942916; cv=none; d=google.com; s=arc-20160816; b=nRmZ0TqleVEDj1LMLs9nX0gHhfqVaPyKvFBi/n0ZiW3mRLgCN1/dn8nvFYUDwA5kzp yxHIOCUe3M/HjCPbBt4QDDKeqM9k5GFoJ9hvjqMUE1GFjt+msOavBkxRHpcnzT5JnxTM H8HPoBOvWKQl0wvkRCrHEiIqtUMbzpTbO3XVY1j8me3ITBYdZr1aIYXQsqPHu5gA6yHp m8M2wEDt6ww0hq7i+DCJt5iO43Uvi7FOdvcasMWTW11OPOHPZG3Qy1rh7R/mJXMpDMQd xb8vK+e+CKvS+ulv442FFsT388oVRtOtc3MTunut/0hHvrhOjTGqFj+rJu/8lSiyJf7m FQZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wcgSo9+GSWbtyAy+kiK0ES+F6s8shseduCR65niP4SY=; b=DWJAf8nFdKMPhAjLpsWR6M8+lNQhpuqfq45Z7orNouKy6C8KORQxcuNhmLSH62zBDw Gcfh8WO2hSWCB80QeHL+2ZiJZ9UoRjya0C46Ptq2zgqCQw5ZqBZbV8bLTMNJBomCgG+k 99c+6G7Bc4utVi6vEh5OG9ZxRBFhrCTo0ggFRc6Mqm4PUxKahUibhfaldrGj/fS11wQo sqqVDUip7PNJZEHqaNj8SRFWucUUIA7maHzlHkArI+fIyPi3BS9KurPCxrox3m3D3faI r+HlfYgievRxPv/Y2aOjTYkBHjwJk1cZrp8hmzAz6nqo+KHn4WKIvmyk/xkNm57WUeoe wHaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=iZMAvLLx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s27si866342eja.411.2020.05.08.06.01.30; Fri, 08 May 2020 06:01:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=iZMAvLLx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729826AbgEHNAN (ORCPT + 99 others); Fri, 8 May 2020 09:00:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:33768 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728709AbgEHMwd (ORCPT ); Fri, 8 May 2020 08:52:33 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 912F224953; Fri, 8 May 2020 12:52:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588942352; bh=56wc2T2Ds3OP+AwV4IRMX/evqxsa+vTXshBIIEREP3E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iZMAvLLxd8d0VzP2PvIusGN9T82CEPnSxo9HRL2UaCDD3zla11per6aMJGj7iMHXZ fmmkMKFFGmjFGZB56DLnoJEPfjZwjiGdbVQ7vgs7eXPxPnAEA1kWKx2Wist5Z1OnMx l/oWY9xEikZn4YSf5XtL8qqm/fLrFvU3CN/pL7WE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Thinh Nguyen , Felipe Balbi , Sasha Levin Subject: [PATCH 5.4 11/50] usb: dwc3: gadget: Properly set maxpacket limit Date: Fri, 8 May 2020 14:35:17 +0200 Message-Id: <20200508123044.998764402@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200508123043.085296641@linuxfoundation.org> References: <20200508123043.085296641@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thinh Nguyen [ Upstream commit d94ea5319813658ad5861d161ae16a194c2abf88 ] Currently the calculation of max packet size limit for IN endpoints is too restrictive. This prevents a matching of a capable hardware endpoint during configuration. Below is the minimum recommended HW configuration to support a particular endpoint setup from the databook: For OUT endpoints, the databook recommended the minimum RxFIFO size to be at least 3x MaxPacketSize + 3x setup packets size (8 bytes each) + clock crossing margin (16 bytes). For IN endpoints, the databook recommended the minimum TxFIFO size to be at least 3x MaxPacketSize for endpoints that support burst. If the endpoint doesn't support burst or when the device is operating in USB 2.0 mode, a minimum TxFIFO size of 2x MaxPacketSize is recommended. Base on these recommendations, we can calculate the MaxPacketSize limit of each endpoint. This patch revises the IN endpoint MaxPacketSize limit and also sets the MaxPacketSize limit for OUT endpoints. Reference: Databook 3.30a section 3.2.2 and 3.2.3 Signed-off-by: Thinh Nguyen Signed-off-by: Felipe Balbi Signed-off-by: Sasha Levin --- drivers/usb/dwc3/core.h | 4 +++ drivers/usb/dwc3/gadget.c | 52 ++++++++++++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index 3ecc69c5b150f..ce4acbf7fef90 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -310,6 +310,10 @@ #define DWC3_GTXFIFOSIZ_TXFDEF(n) ((n) & 0xffff) #define DWC3_GTXFIFOSIZ_TXFSTADDR(n) ((n) & 0xffff0000) +/* Global RX Fifo Size Register */ +#define DWC31_GRXFIFOSIZ_RXFDEP(n) ((n) & 0x7fff) /* DWC_usb31 only */ +#define DWC3_GRXFIFOSIZ_RXFDEP(n) ((n) & 0xffff) + /* Global Event Size Registers */ #define DWC3_GEVNTSIZ_INTMASK BIT(31) #define DWC3_GEVNTSIZ_SIZE(n) ((n) & 0xffff) diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index 379f978db13d5..3d30dec42c81a 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -2220,7 +2220,6 @@ static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep) { struct dwc3 *dwc = dep->dwc; int mdwidth; - int kbytes; int size; mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0); @@ -2236,17 +2235,17 @@ static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep) /* FIFO Depth is in MDWDITH bytes. Multiply */ size *= mdwidth; - kbytes = size / 1024; - if (kbytes == 0) - kbytes = 1; - /* - * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for - * internal overhead. We don't really know how these are used, - * but documentation say it exists. + * To meet performance requirement, a minimum TxFIFO size of 3x + * MaxPacketSize is recommended for endpoints that support burst and a + * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't + * support burst. Use those numbers and we can calculate the max packet + * limit as below. */ - size -= mdwidth * (kbytes + 1); - size /= kbytes; + if (dwc->maximum_speed >= USB_SPEED_SUPER) + size /= 3; + else + size /= 2; usb_ep_set_maxpacket_limit(&dep->endpoint, size); @@ -2264,8 +2263,39 @@ static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep) static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep) { struct dwc3 *dwc = dep->dwc; + int mdwidth; + int size; + + mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0); + + /* MDWIDTH is represented in bits, convert to bytes */ + mdwidth /= 8; - usb_ep_set_maxpacket_limit(&dep->endpoint, 1024); + /* All OUT endpoints share a single RxFIFO space */ + size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0)); + if (dwc3_is_usb31(dwc)) + size = DWC31_GRXFIFOSIZ_RXFDEP(size); + else + size = DWC3_GRXFIFOSIZ_RXFDEP(size); + + /* FIFO depth is in MDWDITH bytes */ + size *= mdwidth; + + /* + * To meet performance requirement, a minimum recommended RxFIFO size + * is defined as follow: + * RxFIFO size >= (3 x MaxPacketSize) + + * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin) + * + * Then calculate the max packet limit as below. + */ + size -= (3 * 8) + 16; + if (size < 0) + size = 0; + else + size /= 3; + + usb_ep_set_maxpacket_limit(&dep->endpoint, size); dep->endpoint.max_streams = 15; dep->endpoint.ops = &dwc3_gadget_ep_ops; list_add_tail(&dep->endpoint.ep_list, -- 2.20.1