Skip to content

Commit 52d3967

Browse files
pawellcdnsPeter Chen
authored andcommitted
usb: cdns3: Fix on-chip memory overflow issue
Patch fixes issue caused setting On-chip memory overflow bit in usb_sts register. The issue occurred because EP_CFG register was set twice before USB_STS.CFGSTS was set. Every write operation on EP_CFG.BUFFERING causes that controller increases internal counter holding the number of reserved on-chip buffers. First time this register was updated in function cdns3_ep_config before delegating SET_CONFIGURATION request to class driver and again it was updated when class wanted to enable endpoint. This patch fixes this issue by configuring endpoints enabled by class driver in cdns3_gadget_ep_enable and others just before status stage. Cc: stable@vger.kernel.org#v5.8+ Fixes: 7733f6c ("usb: cdns3: Add Cadence USB3 DRD Driver") Reported-and-tested-by: Peter Chen <peter.chen@nxp.com> Signed-off-by: Pawel Laszczak <pawell@cadence.com> Signed-off-by: Peter Chen <peter.chen@nxp.com>
1 parent 5fca3f0 commit 52d3967

3 files changed

Lines changed: 94 additions & 76 deletions

File tree

drivers/usb/cdns3/ep0.c

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -137,48 +137,36 @@ static int cdns3_req_ep0_set_configuration(struct cdns3_device *priv_dev,
137137
struct usb_ctrlrequest *ctrl_req)
138138
{
139139
enum usb_device_state device_state = priv_dev->gadget.state;
140-
struct cdns3_endpoint *priv_ep;
141140
u32 config = le16_to_cpu(ctrl_req->wValue);
142141
int result = 0;
143-
int i;
144142

145143
switch (device_state) {
146144
case USB_STATE_ADDRESS:
147-
/* Configure non-control EPs */
148-
for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
149-
priv_ep = priv_dev->eps[i];
150-
if (!priv_ep)
151-
continue;
152-
153-
if (priv_ep->flags & EP_CLAIMED)
154-
cdns3_ep_config(priv_ep);
155-
}
156-
157145
result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
158146

159-
if (result)
160-
return result;
161-
162-
if (!config) {
163-
cdns3_hw_reset_eps_config(priv_dev);
164-
usb_gadget_set_state(&priv_dev->gadget,
165-
USB_STATE_ADDRESS);
166-
}
147+
if (result || !config)
148+
goto reset_config;
167149

168150
break;
169151
case USB_STATE_CONFIGURED:
170152
result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
153+
if (!config && !result)
154+
goto reset_config;
171155

172-
if (!config && !result) {
173-
cdns3_hw_reset_eps_config(priv_dev);
174-
usb_gadget_set_state(&priv_dev->gadget,
175-
USB_STATE_ADDRESS);
176-
}
177156
break;
178157
default:
179-
result = -EINVAL;
158+
return -EINVAL;
180159
}
181160

161+
return 0;
162+
163+
reset_config:
164+
if (result != USB_GADGET_DELAYED_STATUS)
165+
cdns3_hw_reset_eps_config(priv_dev);
166+
167+
usb_gadget_set_state(&priv_dev->gadget,
168+
USB_STATE_ADDRESS);
169+
182170
return result;
183171
}
184172

@@ -705,6 +693,7 @@ static int cdns3_gadget_ep0_queue(struct usb_ep *ep,
705693
unsigned long flags;
706694
int ret = 0;
707695
u8 zlp = 0;
696+
int i;
708697

709698
spin_lock_irqsave(&priv_dev->lock, flags);
710699
trace_cdns3_ep0_queue(priv_dev, request);
@@ -720,6 +709,17 @@ static int cdns3_gadget_ep0_queue(struct usb_ep *ep,
720709
u32 val;
721710

722711
cdns3_select_ep(priv_dev, 0x00);
712+
713+
/*
714+
* Configure all non-control EPs which are not enabled by class driver
715+
*/
716+
for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
717+
priv_ep = priv_dev->eps[i];
718+
if (priv_ep && priv_ep->flags & EP_CLAIMED &&
719+
!(priv_ep->flags & EP_ENABLED))
720+
cdns3_ep_config(priv_ep, 0);
721+
}
722+
723723
cdns3_set_hw_configuration(priv_dev);
724724
cdns3_ep0_complete_setup(priv_dev, 0, 1);
725725
/* wait until configuration set */
@@ -811,6 +811,7 @@ void cdns3_ep0_config(struct cdns3_device *priv_dev)
811811
struct cdns3_usb_regs __iomem *regs;
812812
struct cdns3_endpoint *priv_ep;
813813
u32 max_packet_size = 64;
814+
u32 ep_cfg;
814815

815816
regs = priv_dev->regs;
816817

@@ -842,17 +843,21 @@ void cdns3_ep0_config(struct cdns3_device *priv_dev)
842843
BIT(0) | BIT(16));
843844
}
844845

845-
writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
846-
&regs->ep_cfg);
846+
ep_cfg = EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size);
847+
848+
if (!(priv_ep->flags & EP_CONFIGURED))
849+
writel(ep_cfg, &regs->ep_cfg);
847850

848851
writel(EP_STS_EN_SETUPEN | EP_STS_EN_DESCMISEN | EP_STS_EN_TRBERREN,
849852
&regs->ep_sts_en);
850853

851854
/* init ep in */
852855
cdns3_select_ep(priv_dev, USB_DIR_IN);
853856

854-
writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
855-
&regs->ep_cfg);
857+
if (!(priv_ep->flags & EP_CONFIGURED))
858+
writel(ep_cfg, &regs->ep_cfg);
859+
860+
priv_ep->flags |= EP_CONFIGURED;
856861

857862
writel(EP_STS_EN_SETUPEN | EP_STS_EN_TRBERREN, &regs->ep_sts_en);
858863

drivers/usb/cdns3/gadget.c

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ static void cdns3_ep_stall_flush(struct cdns3_endpoint *priv_ep)
296296
*/
297297
void cdns3_hw_reset_eps_config(struct cdns3_device *priv_dev)
298298
{
299+
int i;
300+
299301
writel(USB_CONF_CFGRST, &priv_dev->regs->usb_conf);
300302

301303
cdns3_allow_enable_l1(priv_dev, 0);
@@ -304,6 +306,10 @@ void cdns3_hw_reset_eps_config(struct cdns3_device *priv_dev)
304306
priv_dev->out_mem_is_allocated = 0;
305307
priv_dev->wait_for_setup = 0;
306308
priv_dev->using_streams = 0;
309+
310+
for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++)
311+
if (priv_dev->eps[i])
312+
priv_dev->eps[i]->flags &= ~EP_CONFIGURED;
307313
}
308314

309315
/**
@@ -1977,27 +1983,6 @@ static int cdns3_ep_onchip_buffer_reserve(struct cdns3_device *priv_dev,
19771983
return 0;
19781984
}
19791985

1980-
static void cdns3_stream_ep_reconfig(struct cdns3_device *priv_dev,
1981-
struct cdns3_endpoint *priv_ep)
1982-
{
1983-
if (!priv_ep->use_streams || priv_dev->gadget.speed < USB_SPEED_SUPER)
1984-
return;
1985-
1986-
if (priv_dev->dev_ver >= DEV_VER_V3) {
1987-
u32 mask = BIT(priv_ep->num + (priv_ep->dir ? 16 : 0));
1988-
1989-
/*
1990-
* Stream capable endpoints are handled by using ep_tdl
1991-
* register. Other endpoints use TDL from TRB feature.
1992-
*/
1993-
cdns3_clear_register_bit(&priv_dev->regs->tdl_from_trb, mask);
1994-
}
1995-
1996-
/* Enable Stream Bit TDL chk and SID chk */
1997-
cdns3_set_register_bit(&priv_dev->regs->ep_cfg, EP_CFG_STREAM_EN |
1998-
EP_CFG_TDL_CHK | EP_CFG_SID_CHK);
1999-
}
2000-
20011986
static void cdns3_configure_dmult(struct cdns3_device *priv_dev,
20021987
struct cdns3_endpoint *priv_ep)
20031988
{
@@ -2035,8 +2020,9 @@ static void cdns3_configure_dmult(struct cdns3_device *priv_dev,
20352020
/**
20362021
* cdns3_ep_config Configure hardware endpoint
20372022
* @priv_ep: extended endpoint object
2023+
* @enable: set EP_CFG_ENABLE bit in ep_cfg register.
20382024
*/
2039-
void cdns3_ep_config(struct cdns3_endpoint *priv_ep)
2025+
int cdns3_ep_config(struct cdns3_endpoint *priv_ep, bool enable)
20402026
{
20412027
bool is_iso_ep = (priv_ep->type == USB_ENDPOINT_XFER_ISOC);
20422028
struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
@@ -2097,7 +2083,7 @@ void cdns3_ep_config(struct cdns3_endpoint *priv_ep)
20972083
break;
20982084
default:
20992085
/* all other speed are not supported */
2100-
return;
2086+
return -EINVAL;
21012087
}
21022088

21032089
if (max_packet_size == 1024)
@@ -2107,11 +2093,33 @@ void cdns3_ep_config(struct cdns3_endpoint *priv_ep)
21072093
else
21082094
priv_ep->trb_burst_size = 16;
21092095

2110-
ret = cdns3_ep_onchip_buffer_reserve(priv_dev, buffering + 1,
2111-
!!priv_ep->dir);
2112-
if (ret) {
2113-
dev_err(priv_dev->dev, "onchip mem is full, ep is invalid\n");
2114-
return;
2096+
/* onchip buffer is only allocated before configuration */
2097+
if (!priv_dev->hw_configured_flag) {
2098+
ret = cdns3_ep_onchip_buffer_reserve(priv_dev, buffering + 1,
2099+
!!priv_ep->dir);
2100+
if (ret) {
2101+
dev_err(priv_dev->dev, "onchip mem is full, ep is invalid\n");
2102+
return ret;
2103+
}
2104+
}
2105+
2106+
if (enable)
2107+
ep_cfg |= EP_CFG_ENABLE;
2108+
2109+
if (priv_ep->use_streams && priv_dev->gadget.speed >= USB_SPEED_SUPER) {
2110+
if (priv_dev->dev_ver >= DEV_VER_V3) {
2111+
u32 mask = BIT(priv_ep->num + (priv_ep->dir ? 16 : 0));
2112+
2113+
/*
2114+
* Stream capable endpoints are handled by using ep_tdl
2115+
* register. Other endpoints use TDL from TRB feature.
2116+
*/
2117+
cdns3_clear_register_bit(&priv_dev->regs->tdl_from_trb,
2118+
mask);
2119+
}
2120+
2121+
/* Enable Stream Bit TDL chk and SID chk */
2122+
ep_cfg |= EP_CFG_STREAM_EN | EP_CFG_TDL_CHK | EP_CFG_SID_CHK;
21152123
}
21162124

21172125
ep_cfg |= EP_CFG_MAXPKTSIZE(max_packet_size) |
@@ -2121,9 +2129,12 @@ void cdns3_ep_config(struct cdns3_endpoint *priv_ep)
21212129

21222130
cdns3_select_ep(priv_dev, bEndpointAddress);
21232131
writel(ep_cfg, &priv_dev->regs->ep_cfg);
2132+
priv_ep->flags |= EP_CONFIGURED;
21242133

21252134
dev_dbg(priv_dev->dev, "Configure %s: with val %08x\n",
21262135
priv_ep->name, ep_cfg);
2136+
2137+
return 0;
21272138
}
21282139

21292140
/* Find correct direction for HW endpoint according to description */
@@ -2264,7 +2275,7 @@ static int cdns3_gadget_ep_enable(struct usb_ep *ep,
22642275
u32 bEndpointAddress;
22652276
unsigned long flags;
22662277
int enable = 1;
2267-
int ret;
2278+
int ret = 0;
22682279
int val;
22692280

22702281
priv_ep = ep_to_cdns3_ep(ep);
@@ -2303,6 +2314,17 @@ static int cdns3_gadget_ep_enable(struct usb_ep *ep,
23032314
bEndpointAddress = priv_ep->num | priv_ep->dir;
23042315
cdns3_select_ep(priv_dev, bEndpointAddress);
23052316

2317+
/*
2318+
* For some versions of controller at some point during ISO OUT traffic
2319+
* DMA reads Transfer Ring for the EP which has never got doorbell.
2320+
* This issue was detected only on simulation, but to avoid this issue
2321+
* driver add protection against it. To fix it driver enable ISO OUT
2322+
* endpoint before setting DRBL. This special treatment of ISO OUT
2323+
* endpoints are recommended by controller specification.
2324+
*/
2325+
if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
2326+
enable = 0;
2327+
23062328
if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
23072329
/*
23082330
* Enable stream support (SS mode) related interrupts
@@ -2313,13 +2335,17 @@ static int cdns3_gadget_ep_enable(struct usb_ep *ep,
23132335
EP_STS_EN_SIDERREN | EP_STS_EN_MD_EXITEN |
23142336
EP_STS_EN_STREAMREN;
23152337
priv_ep->use_streams = true;
2316-
cdns3_stream_ep_reconfig(priv_dev, priv_ep);
2338+
ret = cdns3_ep_config(priv_ep, enable);
23172339
priv_dev->using_streams |= true;
23182340
}
2341+
} else {
2342+
ret = cdns3_ep_config(priv_ep, enable);
23192343
}
23202344

2321-
ret = cdns3_allocate_trb_pool(priv_ep);
2345+
if (ret)
2346+
goto exit;
23222347

2348+
ret = cdns3_allocate_trb_pool(priv_ep);
23232349
if (ret)
23242350
goto exit;
23252351

@@ -2349,20 +2375,6 @@ static int cdns3_gadget_ep_enable(struct usb_ep *ep,
23492375

23502376
writel(reg, &priv_dev->regs->ep_sts_en);
23512377

2352-
/*
2353-
* For some versions of controller at some point during ISO OUT traffic
2354-
* DMA reads Transfer Ring for the EP which has never got doorbell.
2355-
* This issue was detected only on simulation, but to avoid this issue
2356-
* driver add protection against it. To fix it driver enable ISO OUT
2357-
* endpoint before setting DRBL. This special treatment of ISO OUT
2358-
* endpoints are recommended by controller specification.
2359-
*/
2360-
if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
2361-
enable = 0;
2362-
2363-
if (enable)
2364-
cdns3_set_register_bit(&priv_dev->regs->ep_cfg, EP_CFG_ENABLE);
2365-
23662378
ep->desc = desc;
23672379
priv_ep->flags &= ~(EP_PENDING_REQUEST | EP_STALLED | EP_STALL_PENDING |
23682380
EP_QUIRK_ISO_OUT_EN | EP_QUIRK_EXTRA_BUF_EN);

drivers/usb/cdns3/gadget.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ struct cdns3_endpoint {
11591159
#define EP_QUIRK_EXTRA_BUF_DET BIT(12)
11601160
#define EP_QUIRK_EXTRA_BUF_EN BIT(13)
11611161
#define EP_TDLCHK_EN BIT(15)
1162+
#define EP_CONFIGURED BIT(16)
11621163
u32 flags;
11631164

11641165
struct cdns3_request *descmis_req;
@@ -1360,7 +1361,7 @@ void cdns3_gadget_giveback(struct cdns3_endpoint *priv_ep,
13601361
int cdns3_init_ep0(struct cdns3_device *priv_dev,
13611362
struct cdns3_endpoint *priv_ep);
13621363
void cdns3_ep0_config(struct cdns3_device *priv_dev);
1363-
void cdns3_ep_config(struct cdns3_endpoint *priv_ep);
1364+
int cdns3_ep_config(struct cdns3_endpoint *priv_ep, bool enable);
13641365
void cdns3_check_ep0_interrupt_proceed(struct cdns3_device *priv_dev, int dir);
13651366
int __cdns3_gadget_wakeup(struct cdns3_device *priv_dev);
13661367

0 commit comments

Comments
 (0)