xref: /OK3568_Linux_fs/kernel/drivers/usb/dwc2/hcd_ddma.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * hcd_ddma.c - DesignWare HS OTG Controller descriptor DMA routines
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2004-2013 Synopsys, Inc.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
8*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
9*4882a593Smuzhiyun  * are met:
10*4882a593Smuzhiyun  * 1. Redistributions of source code must retain the above copyright
11*4882a593Smuzhiyun  *    notice, this list of conditions, and the following disclaimer,
12*4882a593Smuzhiyun  *    without modification.
13*4882a593Smuzhiyun  * 2. Redistributions in binary form must reproduce the above copyright
14*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in the
15*4882a593Smuzhiyun  *    documentation and/or other materials provided with the distribution.
16*4882a593Smuzhiyun  * 3. The names of the above-listed copyright holders may not be used
17*4882a593Smuzhiyun  *    to endorse or promote products derived from this software without
18*4882a593Smuzhiyun  *    specific prior written permission.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * ALTERNATIVELY, this software may be distributed under the terms of the
21*4882a593Smuzhiyun  * GNU General Public License ("GPL") as published by the Free Software
22*4882a593Smuzhiyun  * Foundation; either version 2 of the License, or (at your option) any
23*4882a593Smuzhiyun  * later version.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26*4882a593Smuzhiyun  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27*4882a593Smuzhiyun  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28*4882a593Smuzhiyun  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29*4882a593Smuzhiyun  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30*4882a593Smuzhiyun  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31*4882a593Smuzhiyun  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32*4882a593Smuzhiyun  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33*4882a593Smuzhiyun  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34*4882a593Smuzhiyun  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35*4882a593Smuzhiyun  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36*4882a593Smuzhiyun  */
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun  * This file contains the Descriptor DMA implementation for Host mode
40*4882a593Smuzhiyun  */
41*4882a593Smuzhiyun #include <linux/kernel.h>
42*4882a593Smuzhiyun #include <linux/module.h>
43*4882a593Smuzhiyun #include <linux/spinlock.h>
44*4882a593Smuzhiyun #include <linux/interrupt.h>
45*4882a593Smuzhiyun #include <linux/dma-mapping.h>
46*4882a593Smuzhiyun #include <linux/io.h>
47*4882a593Smuzhiyun #include <linux/slab.h>
48*4882a593Smuzhiyun #include <linux/usb.h>
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun #include <linux/usb/hcd.h>
51*4882a593Smuzhiyun #include <linux/usb/ch11.h>
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #include "core.h"
54*4882a593Smuzhiyun #include "hcd.h"
55*4882a593Smuzhiyun 
dwc2_frame_list_idx(u16 frame)56*4882a593Smuzhiyun static u16 dwc2_frame_list_idx(u16 frame)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	return frame & (FRLISTEN_64_SIZE - 1);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
dwc2_desclist_idx_inc(u16 idx,u16 inc,u8 speed)61*4882a593Smuzhiyun static u16 dwc2_desclist_idx_inc(u16 idx, u16 inc, u8 speed)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	return (idx + inc) &
64*4882a593Smuzhiyun 		((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
65*4882a593Smuzhiyun 		  MAX_DMA_DESC_NUM_GENERIC) - 1);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
dwc2_desclist_idx_dec(u16 idx,u16 inc,u8 speed)68*4882a593Smuzhiyun static u16 dwc2_desclist_idx_dec(u16 idx, u16 inc, u8 speed)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	return (idx - inc) &
71*4882a593Smuzhiyun 		((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
72*4882a593Smuzhiyun 		  MAX_DMA_DESC_NUM_GENERIC) - 1);
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
dwc2_max_desc_num(struct dwc2_qh * qh)75*4882a593Smuzhiyun static u16 dwc2_max_desc_num(struct dwc2_qh *qh)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	return (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
78*4882a593Smuzhiyun 		qh->dev_speed == USB_SPEED_HIGH) ?
79*4882a593Smuzhiyun 		MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
dwc2_frame_incr_val(struct dwc2_qh * qh)82*4882a593Smuzhiyun static u16 dwc2_frame_incr_val(struct dwc2_qh *qh)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	return qh->dev_speed == USB_SPEED_HIGH ?
85*4882a593Smuzhiyun 	       (qh->host_interval + 8 - 1) / 8 : qh->host_interval;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
dwc2_desc_list_alloc(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,gfp_t flags)88*4882a593Smuzhiyun static int dwc2_desc_list_alloc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
89*4882a593Smuzhiyun 				gfp_t flags)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	struct kmem_cache *desc_cache;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
94*4882a593Smuzhiyun 	    qh->dev_speed == USB_SPEED_HIGH)
95*4882a593Smuzhiyun 		desc_cache = hsotg->desc_hsisoc_cache;
96*4882a593Smuzhiyun 	else
97*4882a593Smuzhiyun 		desc_cache = hsotg->desc_gen_cache;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	qh->desc_list_sz = sizeof(struct dwc2_dma_desc) *
100*4882a593Smuzhiyun 						dwc2_max_desc_num(qh);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	qh->desc_list = kmem_cache_zalloc(desc_cache, flags | GFP_DMA);
103*4882a593Smuzhiyun 	if (!qh->desc_list)
104*4882a593Smuzhiyun 		return -ENOMEM;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	qh->desc_list_dma = dma_map_single(hsotg->dev, qh->desc_list,
107*4882a593Smuzhiyun 					   qh->desc_list_sz,
108*4882a593Smuzhiyun 					   DMA_TO_DEVICE);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	qh->n_bytes = kcalloc(dwc2_max_desc_num(qh), sizeof(u32), flags);
111*4882a593Smuzhiyun 	if (!qh->n_bytes) {
112*4882a593Smuzhiyun 		dma_unmap_single(hsotg->dev, qh->desc_list_dma,
113*4882a593Smuzhiyun 				 qh->desc_list_sz,
114*4882a593Smuzhiyun 				 DMA_FROM_DEVICE);
115*4882a593Smuzhiyun 		kmem_cache_free(desc_cache, qh->desc_list);
116*4882a593Smuzhiyun 		qh->desc_list = NULL;
117*4882a593Smuzhiyun 		return -ENOMEM;
118*4882a593Smuzhiyun 	}
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	return 0;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
dwc2_desc_list_free(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)123*4882a593Smuzhiyun static void dwc2_desc_list_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	struct kmem_cache *desc_cache;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
128*4882a593Smuzhiyun 	    qh->dev_speed == USB_SPEED_HIGH)
129*4882a593Smuzhiyun 		desc_cache = hsotg->desc_hsisoc_cache;
130*4882a593Smuzhiyun 	else
131*4882a593Smuzhiyun 		desc_cache = hsotg->desc_gen_cache;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	if (qh->desc_list) {
134*4882a593Smuzhiyun 		dma_unmap_single(hsotg->dev, qh->desc_list_dma,
135*4882a593Smuzhiyun 				 qh->desc_list_sz, DMA_FROM_DEVICE);
136*4882a593Smuzhiyun 		kmem_cache_free(desc_cache, qh->desc_list);
137*4882a593Smuzhiyun 		qh->desc_list = NULL;
138*4882a593Smuzhiyun 	}
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	kfree(qh->n_bytes);
141*4882a593Smuzhiyun 	qh->n_bytes = NULL;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
dwc2_frame_list_alloc(struct dwc2_hsotg * hsotg,gfp_t mem_flags)144*4882a593Smuzhiyun static int dwc2_frame_list_alloc(struct dwc2_hsotg *hsotg, gfp_t mem_flags)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	if (hsotg->frame_list)
147*4882a593Smuzhiyun 		return 0;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	hsotg->frame_list_sz = 4 * FRLISTEN_64_SIZE;
150*4882a593Smuzhiyun 	hsotg->frame_list = kzalloc(hsotg->frame_list_sz, GFP_ATOMIC | GFP_DMA);
151*4882a593Smuzhiyun 	if (!hsotg->frame_list)
152*4882a593Smuzhiyun 		return -ENOMEM;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	hsotg->frame_list_dma = dma_map_single(hsotg->dev, hsotg->frame_list,
155*4882a593Smuzhiyun 					       hsotg->frame_list_sz,
156*4882a593Smuzhiyun 					       DMA_TO_DEVICE);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	return 0;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
dwc2_frame_list_free(struct dwc2_hsotg * hsotg)161*4882a593Smuzhiyun static void dwc2_frame_list_free(struct dwc2_hsotg *hsotg)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	unsigned long flags;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	spin_lock_irqsave(&hsotg->lock, flags);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	if (!hsotg->frame_list) {
168*4882a593Smuzhiyun 		spin_unlock_irqrestore(&hsotg->lock, flags);
169*4882a593Smuzhiyun 		return;
170*4882a593Smuzhiyun 	}
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	dma_unmap_single(hsotg->dev, hsotg->frame_list_dma,
173*4882a593Smuzhiyun 			 hsotg->frame_list_sz, DMA_FROM_DEVICE);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	kfree(hsotg->frame_list);
176*4882a593Smuzhiyun 	hsotg->frame_list = NULL;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	spin_unlock_irqrestore(&hsotg->lock, flags);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
dwc2_per_sched_enable(struct dwc2_hsotg * hsotg,u32 fr_list_en)181*4882a593Smuzhiyun static void dwc2_per_sched_enable(struct dwc2_hsotg *hsotg, u32 fr_list_en)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	u32 hcfg;
184*4882a593Smuzhiyun 	unsigned long flags;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	spin_lock_irqsave(&hsotg->lock, flags);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	hcfg = dwc2_readl(hsotg, HCFG);
189*4882a593Smuzhiyun 	if (hcfg & HCFG_PERSCHEDENA) {
190*4882a593Smuzhiyun 		/* already enabled */
191*4882a593Smuzhiyun 		spin_unlock_irqrestore(&hsotg->lock, flags);
192*4882a593Smuzhiyun 		return;
193*4882a593Smuzhiyun 	}
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	dwc2_writel(hsotg, hsotg->frame_list_dma, HFLBADDR);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	hcfg &= ~HCFG_FRLISTEN_MASK;
198*4882a593Smuzhiyun 	hcfg |= fr_list_en | HCFG_PERSCHEDENA;
199*4882a593Smuzhiyun 	dev_vdbg(hsotg->dev, "Enabling Periodic schedule\n");
200*4882a593Smuzhiyun 	dwc2_writel(hsotg, hcfg, HCFG);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	spin_unlock_irqrestore(&hsotg->lock, flags);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun 
dwc2_per_sched_disable(struct dwc2_hsotg * hsotg)205*4882a593Smuzhiyun static void dwc2_per_sched_disable(struct dwc2_hsotg *hsotg)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun 	u32 hcfg;
208*4882a593Smuzhiyun 	unsigned long flags;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	spin_lock_irqsave(&hsotg->lock, flags);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	hcfg = dwc2_readl(hsotg, HCFG);
213*4882a593Smuzhiyun 	if (!(hcfg & HCFG_PERSCHEDENA)) {
214*4882a593Smuzhiyun 		/* already disabled */
215*4882a593Smuzhiyun 		spin_unlock_irqrestore(&hsotg->lock, flags);
216*4882a593Smuzhiyun 		return;
217*4882a593Smuzhiyun 	}
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	hcfg &= ~HCFG_PERSCHEDENA;
220*4882a593Smuzhiyun 	dev_vdbg(hsotg->dev, "Disabling Periodic schedule\n");
221*4882a593Smuzhiyun 	dwc2_writel(hsotg, hcfg, HCFG);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	spin_unlock_irqrestore(&hsotg->lock, flags);
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun  * Activates/Deactivates FrameList entries for the channel based on endpoint
228*4882a593Smuzhiyun  * servicing period
229*4882a593Smuzhiyun  */
dwc2_update_frame_list(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,int enable)230*4882a593Smuzhiyun static void dwc2_update_frame_list(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
231*4882a593Smuzhiyun 				   int enable)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun 	struct dwc2_host_chan *chan;
234*4882a593Smuzhiyun 	u16 i, j, inc;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	if (!hsotg) {
237*4882a593Smuzhiyun 		pr_err("hsotg = %p\n", hsotg);
238*4882a593Smuzhiyun 		return;
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	if (!qh->channel) {
242*4882a593Smuzhiyun 		dev_err(hsotg->dev, "qh->channel = %p\n", qh->channel);
243*4882a593Smuzhiyun 		return;
244*4882a593Smuzhiyun 	}
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	if (!hsotg->frame_list) {
247*4882a593Smuzhiyun 		dev_err(hsotg->dev, "hsotg->frame_list = %p\n",
248*4882a593Smuzhiyun 			hsotg->frame_list);
249*4882a593Smuzhiyun 		return;
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	chan = qh->channel;
253*4882a593Smuzhiyun 	inc = dwc2_frame_incr_val(qh);
254*4882a593Smuzhiyun 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC)
255*4882a593Smuzhiyun 		i = dwc2_frame_list_idx(qh->next_active_frame);
256*4882a593Smuzhiyun 	else
257*4882a593Smuzhiyun 		i = 0;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	j = i;
260*4882a593Smuzhiyun 	do {
261*4882a593Smuzhiyun 		if (enable)
262*4882a593Smuzhiyun 			hsotg->frame_list[j] |= 1 << chan->hc_num;
263*4882a593Smuzhiyun 		else
264*4882a593Smuzhiyun 			hsotg->frame_list[j] &= ~(1 << chan->hc_num);
265*4882a593Smuzhiyun 		j = (j + inc) & (FRLISTEN_64_SIZE - 1);
266*4882a593Smuzhiyun 	} while (j != i);
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	/*
269*4882a593Smuzhiyun 	 * Sync frame list since controller will access it if periodic
270*4882a593Smuzhiyun 	 * channel is currently enabled.
271*4882a593Smuzhiyun 	 */
272*4882a593Smuzhiyun 	dma_sync_single_for_device(hsotg->dev,
273*4882a593Smuzhiyun 				   hsotg->frame_list_dma,
274*4882a593Smuzhiyun 				   hsotg->frame_list_sz,
275*4882a593Smuzhiyun 				   DMA_TO_DEVICE);
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	if (!enable)
278*4882a593Smuzhiyun 		return;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	chan->schinfo = 0;
281*4882a593Smuzhiyun 	if (chan->speed == USB_SPEED_HIGH && qh->host_interval) {
282*4882a593Smuzhiyun 		j = 1;
283*4882a593Smuzhiyun 		/* TODO - check this */
284*4882a593Smuzhiyun 		inc = (8 + qh->host_interval - 1) / qh->host_interval;
285*4882a593Smuzhiyun 		for (i = 0; i < inc; i++) {
286*4882a593Smuzhiyun 			chan->schinfo |= j;
287*4882a593Smuzhiyun 			j = j << qh->host_interval;
288*4882a593Smuzhiyun 		}
289*4882a593Smuzhiyun 	} else {
290*4882a593Smuzhiyun 		chan->schinfo = 0xff;
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun 
dwc2_release_channel_ddma(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)294*4882a593Smuzhiyun static void dwc2_release_channel_ddma(struct dwc2_hsotg *hsotg,
295*4882a593Smuzhiyun 				      struct dwc2_qh *qh)
296*4882a593Smuzhiyun {
297*4882a593Smuzhiyun 	struct dwc2_host_chan *chan = qh->channel;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	if (dwc2_qh_is_non_per(qh)) {
300*4882a593Smuzhiyun 		if (hsotg->params.uframe_sched)
301*4882a593Smuzhiyun 			hsotg->available_host_channels++;
302*4882a593Smuzhiyun 		else
303*4882a593Smuzhiyun 			hsotg->non_periodic_channels--;
304*4882a593Smuzhiyun 	} else {
305*4882a593Smuzhiyun 		dwc2_update_frame_list(hsotg, qh, 0);
306*4882a593Smuzhiyun 		hsotg->available_host_channels++;
307*4882a593Smuzhiyun 	}
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	/*
310*4882a593Smuzhiyun 	 * The condition is added to prevent double cleanup try in case of
311*4882a593Smuzhiyun 	 * device disconnect. See channel cleanup in dwc2_hcd_disconnect().
312*4882a593Smuzhiyun 	 */
313*4882a593Smuzhiyun 	if (chan->qh) {
314*4882a593Smuzhiyun 		if (!list_empty(&chan->hc_list_entry))
315*4882a593Smuzhiyun 			list_del(&chan->hc_list_entry);
316*4882a593Smuzhiyun 		dwc2_hc_cleanup(hsotg, chan);
317*4882a593Smuzhiyun 		list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
318*4882a593Smuzhiyun 		chan->qh = NULL;
319*4882a593Smuzhiyun 	}
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	qh->channel = NULL;
322*4882a593Smuzhiyun 	qh->ntd = 0;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	if (qh->desc_list)
325*4882a593Smuzhiyun 		memset(qh->desc_list, 0, sizeof(struct dwc2_dma_desc) *
326*4882a593Smuzhiyun 		       dwc2_max_desc_num(qh));
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun /**
330*4882a593Smuzhiyun  * dwc2_hcd_qh_init_ddma() - Initializes a QH structure's Descriptor DMA
331*4882a593Smuzhiyun  * related members
332*4882a593Smuzhiyun  *
333*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller
334*4882a593Smuzhiyun  * @qh:    The QH to init
335*4882a593Smuzhiyun  * @mem_flags: Indicates the type of memory allocation
336*4882a593Smuzhiyun  *
337*4882a593Smuzhiyun  * Return: 0 if successful, negative error code otherwise
338*4882a593Smuzhiyun  *
339*4882a593Smuzhiyun  * Allocates memory for the descriptor list. For the first periodic QH,
340*4882a593Smuzhiyun  * allocates memory for the FrameList and enables periodic scheduling.
341*4882a593Smuzhiyun  */
dwc2_hcd_qh_init_ddma(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,gfp_t mem_flags)342*4882a593Smuzhiyun int dwc2_hcd_qh_init_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
343*4882a593Smuzhiyun 			  gfp_t mem_flags)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	int retval;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	if (qh->do_split) {
348*4882a593Smuzhiyun 		dev_err(hsotg->dev,
349*4882a593Smuzhiyun 			"SPLIT Transfers are not supported in Descriptor DMA mode.\n");
350*4882a593Smuzhiyun 		retval = -EINVAL;
351*4882a593Smuzhiyun 		goto err0;
352*4882a593Smuzhiyun 	}
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	retval = dwc2_desc_list_alloc(hsotg, qh, mem_flags);
355*4882a593Smuzhiyun 	if (retval)
356*4882a593Smuzhiyun 		goto err0;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
359*4882a593Smuzhiyun 	    qh->ep_type == USB_ENDPOINT_XFER_INT) {
360*4882a593Smuzhiyun 		if (!hsotg->frame_list) {
361*4882a593Smuzhiyun 			retval = dwc2_frame_list_alloc(hsotg, mem_flags);
362*4882a593Smuzhiyun 			if (retval)
363*4882a593Smuzhiyun 				goto err1;
364*4882a593Smuzhiyun 			/* Enable periodic schedule on first periodic QH */
365*4882a593Smuzhiyun 			dwc2_per_sched_enable(hsotg, HCFG_FRLISTEN_64);
366*4882a593Smuzhiyun 		}
367*4882a593Smuzhiyun 	}
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	qh->ntd = 0;
370*4882a593Smuzhiyun 	return 0;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun err1:
373*4882a593Smuzhiyun 	dwc2_desc_list_free(hsotg, qh);
374*4882a593Smuzhiyun err0:
375*4882a593Smuzhiyun 	return retval;
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun /**
379*4882a593Smuzhiyun  * dwc2_hcd_qh_free_ddma() - Frees a QH structure's Descriptor DMA related
380*4882a593Smuzhiyun  * members
381*4882a593Smuzhiyun  *
382*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller
383*4882a593Smuzhiyun  * @qh:    The QH to free
384*4882a593Smuzhiyun  *
385*4882a593Smuzhiyun  * Frees descriptor list memory associated with the QH. If QH is periodic and
386*4882a593Smuzhiyun  * the last, frees FrameList memory and disables periodic scheduling.
387*4882a593Smuzhiyun  */
dwc2_hcd_qh_free_ddma(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)388*4882a593Smuzhiyun void dwc2_hcd_qh_free_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun 	unsigned long flags;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	dwc2_desc_list_free(hsotg, qh);
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	/*
395*4882a593Smuzhiyun 	 * Channel still assigned due to some reasons.
396*4882a593Smuzhiyun 	 * Seen on Isoc URB dequeue. Channel halted but no subsequent
397*4882a593Smuzhiyun 	 * ChHalted interrupt to release the channel. Afterwards
398*4882a593Smuzhiyun 	 * when it comes here from endpoint disable routine
399*4882a593Smuzhiyun 	 * channel remains assigned.
400*4882a593Smuzhiyun 	 */
401*4882a593Smuzhiyun 	spin_lock_irqsave(&hsotg->lock, flags);
402*4882a593Smuzhiyun 	if (qh->channel)
403*4882a593Smuzhiyun 		dwc2_release_channel_ddma(hsotg, qh);
404*4882a593Smuzhiyun 	spin_unlock_irqrestore(&hsotg->lock, flags);
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	if ((qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
407*4882a593Smuzhiyun 	     qh->ep_type == USB_ENDPOINT_XFER_INT) &&
408*4882a593Smuzhiyun 	    (hsotg->params.uframe_sched ||
409*4882a593Smuzhiyun 	     !hsotg->periodic_channels) && hsotg->frame_list) {
410*4882a593Smuzhiyun 		dwc2_per_sched_disable(hsotg);
411*4882a593Smuzhiyun 		dwc2_frame_list_free(hsotg);
412*4882a593Smuzhiyun 	}
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
dwc2_frame_to_desc_idx(struct dwc2_qh * qh,u16 frame_idx)415*4882a593Smuzhiyun static u8 dwc2_frame_to_desc_idx(struct dwc2_qh *qh, u16 frame_idx)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	if (qh->dev_speed == USB_SPEED_HIGH)
418*4882a593Smuzhiyun 		/* Descriptor set (8 descriptors) index which is 8-aligned */
419*4882a593Smuzhiyun 		return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
420*4882a593Smuzhiyun 	else
421*4882a593Smuzhiyun 		return frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1);
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun /*
425*4882a593Smuzhiyun  * Determine starting frame for Isochronous transfer.
426*4882a593Smuzhiyun  * Few frames skipped to prevent race condition with HC.
427*4882a593Smuzhiyun  */
dwc2_calc_starting_frame(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,u16 * skip_frames)428*4882a593Smuzhiyun static u16 dwc2_calc_starting_frame(struct dwc2_hsotg *hsotg,
429*4882a593Smuzhiyun 				    struct dwc2_qh *qh, u16 *skip_frames)
430*4882a593Smuzhiyun {
431*4882a593Smuzhiyun 	u16 frame;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	/*
436*4882a593Smuzhiyun 	 * next_active_frame is always frame number (not uFrame) both in FS
437*4882a593Smuzhiyun 	 * and HS!
438*4882a593Smuzhiyun 	 */
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	/*
441*4882a593Smuzhiyun 	 * skip_frames is used to limit activated descriptors number
442*4882a593Smuzhiyun 	 * to avoid the situation when HC services the last activated
443*4882a593Smuzhiyun 	 * descriptor firstly.
444*4882a593Smuzhiyun 	 * Example for FS:
445*4882a593Smuzhiyun 	 * Current frame is 1, scheduled frame is 3. Since HC always fetches
446*4882a593Smuzhiyun 	 * the descriptor corresponding to curr_frame+1, the descriptor
447*4882a593Smuzhiyun 	 * corresponding to frame 2 will be fetched. If the number of
448*4882a593Smuzhiyun 	 * descriptors is max=64 (or greather) the list will be fully programmed
449*4882a593Smuzhiyun 	 * with Active descriptors and it is possible case (rare) that the
450*4882a593Smuzhiyun 	 * latest descriptor(considering rollback) corresponding to frame 2 will
451*4882a593Smuzhiyun 	 * be serviced first. HS case is more probable because, in fact, up to
452*4882a593Smuzhiyun 	 * 11 uframes (16 in the code) may be skipped.
453*4882a593Smuzhiyun 	 */
454*4882a593Smuzhiyun 	if (qh->dev_speed == USB_SPEED_HIGH) {
455*4882a593Smuzhiyun 		/*
456*4882a593Smuzhiyun 		 * Consider uframe counter also, to start xfer asap. If half of
457*4882a593Smuzhiyun 		 * the frame elapsed skip 2 frames otherwise just 1 frame.
458*4882a593Smuzhiyun 		 * Starting descriptor index must be 8-aligned, so if the
459*4882a593Smuzhiyun 		 * current frame is near to complete the next one is skipped as
460*4882a593Smuzhiyun 		 * well.
461*4882a593Smuzhiyun 		 */
462*4882a593Smuzhiyun 		if (dwc2_micro_frame_num(hsotg->frame_number) >= 5) {
463*4882a593Smuzhiyun 			*skip_frames = 2 * 8;
464*4882a593Smuzhiyun 			frame = dwc2_frame_num_inc(hsotg->frame_number,
465*4882a593Smuzhiyun 						   *skip_frames);
466*4882a593Smuzhiyun 		} else {
467*4882a593Smuzhiyun 			*skip_frames = 1 * 8;
468*4882a593Smuzhiyun 			frame = dwc2_frame_num_inc(hsotg->frame_number,
469*4882a593Smuzhiyun 						   *skip_frames);
470*4882a593Smuzhiyun 		}
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 		frame = dwc2_full_frame_num(frame);
473*4882a593Smuzhiyun 	} else {
474*4882a593Smuzhiyun 		/*
475*4882a593Smuzhiyun 		 * Two frames are skipped for FS - the current and the next.
476*4882a593Smuzhiyun 		 * But for descriptor programming, 1 frame (descriptor) is
477*4882a593Smuzhiyun 		 * enough, see example above.
478*4882a593Smuzhiyun 		 */
479*4882a593Smuzhiyun 		*skip_frames = 1;
480*4882a593Smuzhiyun 		frame = dwc2_frame_num_inc(hsotg->frame_number, 2);
481*4882a593Smuzhiyun 	}
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	return frame;
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun /*
487*4882a593Smuzhiyun  * Calculate initial descriptor index for isochronous transfer based on
488*4882a593Smuzhiyun  * scheduled frame
489*4882a593Smuzhiyun  */
dwc2_recalc_initial_desc_idx(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)490*4882a593Smuzhiyun static u16 dwc2_recalc_initial_desc_idx(struct dwc2_hsotg *hsotg,
491*4882a593Smuzhiyun 					struct dwc2_qh *qh)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun 	u16 frame, fr_idx, fr_idx_tmp, skip_frames;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	/*
496*4882a593Smuzhiyun 	 * With current ISOC processing algorithm the channel is being released
497*4882a593Smuzhiyun 	 * when no more QTDs in the list (qh->ntd == 0). Thus this function is
498*4882a593Smuzhiyun 	 * called only when qh->ntd == 0 and qh->channel == 0.
499*4882a593Smuzhiyun 	 *
500*4882a593Smuzhiyun 	 * So qh->channel != NULL branch is not used and just not removed from
501*4882a593Smuzhiyun 	 * the source file. It is required for another possible approach which
502*4882a593Smuzhiyun 	 * is, do not disable and release the channel when ISOC session
503*4882a593Smuzhiyun 	 * completed, just move QH to inactive schedule until new QTD arrives.
504*4882a593Smuzhiyun 	 * On new QTD, the QH moved back to 'ready' schedule, starting frame and
505*4882a593Smuzhiyun 	 * therefore starting desc_index are recalculated. In this case channel
506*4882a593Smuzhiyun 	 * is released only on ep_disable.
507*4882a593Smuzhiyun 	 */
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	/*
510*4882a593Smuzhiyun 	 * Calculate starting descriptor index. For INTERRUPT endpoint it is
511*4882a593Smuzhiyun 	 * always 0.
512*4882a593Smuzhiyun 	 */
513*4882a593Smuzhiyun 	if (qh->channel) {
514*4882a593Smuzhiyun 		frame = dwc2_calc_starting_frame(hsotg, qh, &skip_frames);
515*4882a593Smuzhiyun 		/*
516*4882a593Smuzhiyun 		 * Calculate initial descriptor index based on FrameList current
517*4882a593Smuzhiyun 		 * bitmap and servicing period
518*4882a593Smuzhiyun 		 */
519*4882a593Smuzhiyun 		fr_idx_tmp = dwc2_frame_list_idx(frame);
520*4882a593Smuzhiyun 		fr_idx = (FRLISTEN_64_SIZE +
521*4882a593Smuzhiyun 			  dwc2_frame_list_idx(qh->next_active_frame) -
522*4882a593Smuzhiyun 			  fr_idx_tmp) % dwc2_frame_incr_val(qh);
523*4882a593Smuzhiyun 		fr_idx = (fr_idx + fr_idx_tmp) % FRLISTEN_64_SIZE;
524*4882a593Smuzhiyun 	} else {
525*4882a593Smuzhiyun 		qh->next_active_frame = dwc2_calc_starting_frame(hsotg, qh,
526*4882a593Smuzhiyun 							   &skip_frames);
527*4882a593Smuzhiyun 		fr_idx = dwc2_frame_list_idx(qh->next_active_frame);
528*4882a593Smuzhiyun 	}
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	qh->td_first = qh->td_last = dwc2_frame_to_desc_idx(qh, fr_idx);
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	return skip_frames;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun #define ISOC_URB_GIVEBACK_ASAP
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun #define MAX_ISOC_XFER_SIZE_FS	1023
538*4882a593Smuzhiyun #define MAX_ISOC_XFER_SIZE_HS	3072
539*4882a593Smuzhiyun #define DESCNUM_THRESHOLD	4
540*4882a593Smuzhiyun 
dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg * hsotg,struct dwc2_qtd * qtd,struct dwc2_qh * qh,u32 max_xfer_size,u16 idx)541*4882a593Smuzhiyun static void dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
542*4882a593Smuzhiyun 					 struct dwc2_qtd *qtd,
543*4882a593Smuzhiyun 					 struct dwc2_qh *qh, u32 max_xfer_size,
544*4882a593Smuzhiyun 					 u16 idx)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun 	struct dwc2_dma_desc *dma_desc = &qh->desc_list[idx];
547*4882a593Smuzhiyun 	struct dwc2_hcd_iso_packet_desc *frame_desc;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	memset(dma_desc, 0, sizeof(*dma_desc));
550*4882a593Smuzhiyun 	frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	if (frame_desc->length > max_xfer_size)
553*4882a593Smuzhiyun 		qh->n_bytes[idx] = max_xfer_size;
554*4882a593Smuzhiyun 	else
555*4882a593Smuzhiyun 		qh->n_bytes[idx] = frame_desc->length;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
558*4882a593Smuzhiyun 	dma_desc->status = qh->n_bytes[idx] << HOST_DMA_ISOC_NBYTES_SHIFT &
559*4882a593Smuzhiyun 			   HOST_DMA_ISOC_NBYTES_MASK;
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	/* Set active bit */
562*4882a593Smuzhiyun 	dma_desc->status |= HOST_DMA_A;
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	qh->ntd++;
565*4882a593Smuzhiyun 	qtd->isoc_frame_index_last++;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun #ifdef ISOC_URB_GIVEBACK_ASAP
568*4882a593Smuzhiyun 	/* Set IOC for each descriptor corresponding to last frame of URB */
569*4882a593Smuzhiyun 	if (qtd->isoc_frame_index_last == qtd->urb->packet_count)
570*4882a593Smuzhiyun 		dma_desc->status |= HOST_DMA_IOC;
571*4882a593Smuzhiyun #endif
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	dma_sync_single_for_device(hsotg->dev,
574*4882a593Smuzhiyun 				   qh->desc_list_dma +
575*4882a593Smuzhiyun 			(idx * sizeof(struct dwc2_dma_desc)),
576*4882a593Smuzhiyun 			sizeof(struct dwc2_dma_desc),
577*4882a593Smuzhiyun 			DMA_TO_DEVICE);
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun 
dwc2_init_isoc_dma_desc(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,u16 skip_frames)580*4882a593Smuzhiyun static void dwc2_init_isoc_dma_desc(struct dwc2_hsotg *hsotg,
581*4882a593Smuzhiyun 				    struct dwc2_qh *qh, u16 skip_frames)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun 	struct dwc2_qtd *qtd;
584*4882a593Smuzhiyun 	u32 max_xfer_size;
585*4882a593Smuzhiyun 	u16 idx, inc, n_desc = 0, ntd_max = 0;
586*4882a593Smuzhiyun 	u16 cur_idx;
587*4882a593Smuzhiyun 	u16 next_idx;
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	idx = qh->td_last;
590*4882a593Smuzhiyun 	inc = qh->host_interval;
591*4882a593Smuzhiyun 	hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
592*4882a593Smuzhiyun 	cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
593*4882a593Smuzhiyun 	next_idx = dwc2_desclist_idx_inc(qh->td_last, inc, qh->dev_speed);
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	/*
596*4882a593Smuzhiyun 	 * Ensure current frame number didn't overstep last scheduled
597*4882a593Smuzhiyun 	 * descriptor. If it happens, the only way to recover is to move
598*4882a593Smuzhiyun 	 * qh->td_last to current frame number + 1.
599*4882a593Smuzhiyun 	 * So that next isoc descriptor will be scheduled on frame number + 1
600*4882a593Smuzhiyun 	 * and not on a past frame.
601*4882a593Smuzhiyun 	 */
602*4882a593Smuzhiyun 	if (dwc2_frame_idx_num_gt(cur_idx, next_idx) || (cur_idx == next_idx)) {
603*4882a593Smuzhiyun 		if (inc < 32) {
604*4882a593Smuzhiyun 			dev_vdbg(hsotg->dev,
605*4882a593Smuzhiyun 				 "current frame number overstep last descriptor\n");
606*4882a593Smuzhiyun 			qh->td_last = dwc2_desclist_idx_inc(cur_idx, inc,
607*4882a593Smuzhiyun 							    qh->dev_speed);
608*4882a593Smuzhiyun 			idx = qh->td_last;
609*4882a593Smuzhiyun 		}
610*4882a593Smuzhiyun 	}
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	if (qh->host_interval) {
613*4882a593Smuzhiyun 		ntd_max = (dwc2_max_desc_num(qh) + qh->host_interval - 1) /
614*4882a593Smuzhiyun 				qh->host_interval;
615*4882a593Smuzhiyun 		if (skip_frames && !qh->channel)
616*4882a593Smuzhiyun 			ntd_max -= skip_frames / qh->host_interval;
617*4882a593Smuzhiyun 	}
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	max_xfer_size = qh->dev_speed == USB_SPEED_HIGH ?
620*4882a593Smuzhiyun 			MAX_ISOC_XFER_SIZE_HS : MAX_ISOC_XFER_SIZE_FS;
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
623*4882a593Smuzhiyun 		if (qtd->in_process &&
624*4882a593Smuzhiyun 		    qtd->isoc_frame_index_last ==
625*4882a593Smuzhiyun 		    qtd->urb->packet_count)
626*4882a593Smuzhiyun 			continue;
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 		qtd->isoc_td_first = idx;
629*4882a593Smuzhiyun 		while (qh->ntd < ntd_max && qtd->isoc_frame_index_last <
630*4882a593Smuzhiyun 						qtd->urb->packet_count) {
631*4882a593Smuzhiyun 			dwc2_fill_host_isoc_dma_desc(hsotg, qtd, qh,
632*4882a593Smuzhiyun 						     max_xfer_size, idx);
633*4882a593Smuzhiyun 			idx = dwc2_desclist_idx_inc(idx, inc, qh->dev_speed);
634*4882a593Smuzhiyun 			n_desc++;
635*4882a593Smuzhiyun 		}
636*4882a593Smuzhiyun 		qtd->isoc_td_last = idx;
637*4882a593Smuzhiyun 		qtd->in_process = 1;
638*4882a593Smuzhiyun 	}
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	qh->td_last = idx;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun #ifdef ISOC_URB_GIVEBACK_ASAP
643*4882a593Smuzhiyun 	/* Set IOC for last descriptor if descriptor list is full */
644*4882a593Smuzhiyun 	if (qh->ntd == ntd_max) {
645*4882a593Smuzhiyun 		idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
646*4882a593Smuzhiyun 		qh->desc_list[idx].status |= HOST_DMA_IOC;
647*4882a593Smuzhiyun 		dma_sync_single_for_device(hsotg->dev,
648*4882a593Smuzhiyun 					   qh->desc_list_dma + (idx *
649*4882a593Smuzhiyun 					   sizeof(struct dwc2_dma_desc)),
650*4882a593Smuzhiyun 					   sizeof(struct dwc2_dma_desc),
651*4882a593Smuzhiyun 					   DMA_TO_DEVICE);
652*4882a593Smuzhiyun 	}
653*4882a593Smuzhiyun #else
654*4882a593Smuzhiyun 	/*
655*4882a593Smuzhiyun 	 * Set IOC bit only for one descriptor. Always try to be ahead of HW
656*4882a593Smuzhiyun 	 * processing, i.e. on IOC generation driver activates next descriptor
657*4882a593Smuzhiyun 	 * but core continues to process descriptors following the one with IOC
658*4882a593Smuzhiyun 	 * set.
659*4882a593Smuzhiyun 	 */
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	if (n_desc > DESCNUM_THRESHOLD)
662*4882a593Smuzhiyun 		/*
663*4882a593Smuzhiyun 		 * Move IOC "up". Required even if there is only one QTD
664*4882a593Smuzhiyun 		 * in the list, because QTDs might continue to be queued,
665*4882a593Smuzhiyun 		 * but during the activation it was only one queued.
666*4882a593Smuzhiyun 		 * Actually more than one QTD might be in the list if this
667*4882a593Smuzhiyun 		 * function called from XferCompletion - QTDs was queued during
668*4882a593Smuzhiyun 		 * HW processing of the previous descriptor chunk.
669*4882a593Smuzhiyun 		 */
670*4882a593Smuzhiyun 		idx = dwc2_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2),
671*4882a593Smuzhiyun 					    qh->dev_speed);
672*4882a593Smuzhiyun 	else
673*4882a593Smuzhiyun 		/*
674*4882a593Smuzhiyun 		 * Set the IOC for the latest descriptor if either number of
675*4882a593Smuzhiyun 		 * descriptors is not greater than threshold or no more new
676*4882a593Smuzhiyun 		 * descriptors activated
677*4882a593Smuzhiyun 		 */
678*4882a593Smuzhiyun 		idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	qh->desc_list[idx].status |= HOST_DMA_IOC;
681*4882a593Smuzhiyun 	dma_sync_single_for_device(hsotg->dev,
682*4882a593Smuzhiyun 				   qh->desc_list_dma +
683*4882a593Smuzhiyun 				   (idx * sizeof(struct dwc2_dma_desc)),
684*4882a593Smuzhiyun 				   sizeof(struct dwc2_dma_desc),
685*4882a593Smuzhiyun 				   DMA_TO_DEVICE);
686*4882a593Smuzhiyun #endif
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun 
dwc2_fill_host_dma_desc(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,struct dwc2_qtd * qtd,struct dwc2_qh * qh,int n_desc)689*4882a593Smuzhiyun static void dwc2_fill_host_dma_desc(struct dwc2_hsotg *hsotg,
690*4882a593Smuzhiyun 				    struct dwc2_host_chan *chan,
691*4882a593Smuzhiyun 				    struct dwc2_qtd *qtd, struct dwc2_qh *qh,
692*4882a593Smuzhiyun 				    int n_desc)
693*4882a593Smuzhiyun {
694*4882a593Smuzhiyun 	struct dwc2_dma_desc *dma_desc = &qh->desc_list[n_desc];
695*4882a593Smuzhiyun 	int len = chan->xfer_len;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	if (len > HOST_DMA_NBYTES_LIMIT - (chan->max_packet - 1))
698*4882a593Smuzhiyun 		len = HOST_DMA_NBYTES_LIMIT - (chan->max_packet - 1);
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	if (chan->ep_is_in) {
701*4882a593Smuzhiyun 		int num_packets;
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 		if (len > 0 && chan->max_packet)
704*4882a593Smuzhiyun 			num_packets = (len + chan->max_packet - 1)
705*4882a593Smuzhiyun 					/ chan->max_packet;
706*4882a593Smuzhiyun 		else
707*4882a593Smuzhiyun 			/* Need 1 packet for transfer length of 0 */
708*4882a593Smuzhiyun 			num_packets = 1;
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 		/* Always program an integral # of packets for IN transfers */
711*4882a593Smuzhiyun 		len = num_packets * chan->max_packet;
712*4882a593Smuzhiyun 	}
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	dma_desc->status = len << HOST_DMA_NBYTES_SHIFT & HOST_DMA_NBYTES_MASK;
715*4882a593Smuzhiyun 	qh->n_bytes[n_desc] = len;
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL &&
718*4882a593Smuzhiyun 	    qtd->control_phase == DWC2_CONTROL_SETUP)
719*4882a593Smuzhiyun 		dma_desc->status |= HOST_DMA_SUP;
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	dma_desc->buf = (u32)chan->xfer_dma;
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	dma_sync_single_for_device(hsotg->dev,
724*4882a593Smuzhiyun 				   qh->desc_list_dma +
725*4882a593Smuzhiyun 				   (n_desc * sizeof(struct dwc2_dma_desc)),
726*4882a593Smuzhiyun 				   sizeof(struct dwc2_dma_desc),
727*4882a593Smuzhiyun 				   DMA_TO_DEVICE);
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	/*
730*4882a593Smuzhiyun 	 * Last (or only) descriptor of IN transfer with actual size less
731*4882a593Smuzhiyun 	 * than MaxPacket
732*4882a593Smuzhiyun 	 */
733*4882a593Smuzhiyun 	if (len > chan->xfer_len) {
734*4882a593Smuzhiyun 		chan->xfer_len = 0;
735*4882a593Smuzhiyun 	} else {
736*4882a593Smuzhiyun 		chan->xfer_dma += len;
737*4882a593Smuzhiyun 		chan->xfer_len -= len;
738*4882a593Smuzhiyun 	}
739*4882a593Smuzhiyun }
740*4882a593Smuzhiyun 
dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)741*4882a593Smuzhiyun static void dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg *hsotg,
742*4882a593Smuzhiyun 					struct dwc2_qh *qh)
743*4882a593Smuzhiyun {
744*4882a593Smuzhiyun 	struct dwc2_qtd *qtd;
745*4882a593Smuzhiyun 	struct dwc2_host_chan *chan = qh->channel;
746*4882a593Smuzhiyun 	int n_desc = 0;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	dev_vdbg(hsotg->dev, "%s(): qh=%p dma=%08lx len=%d\n", __func__, qh,
749*4882a593Smuzhiyun 		 (unsigned long)chan->xfer_dma, chan->xfer_len);
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	/*
752*4882a593Smuzhiyun 	 * Start with chan->xfer_dma initialized in assign_and_init_hc(), then
753*4882a593Smuzhiyun 	 * if SG transfer consists of multiple URBs, this pointer is re-assigned
754*4882a593Smuzhiyun 	 * to the buffer of the currently processed QTD. For non-SG request
755*4882a593Smuzhiyun 	 * there is always one QTD active.
756*4882a593Smuzhiyun 	 */
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
759*4882a593Smuzhiyun 		dev_vdbg(hsotg->dev, "qtd=%p\n", qtd);
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 		if (n_desc) {
762*4882a593Smuzhiyun 			/* SG request - more than 1 QTD */
763*4882a593Smuzhiyun 			chan->xfer_dma = qtd->urb->dma +
764*4882a593Smuzhiyun 					qtd->urb->actual_length;
765*4882a593Smuzhiyun 			chan->xfer_len = qtd->urb->length -
766*4882a593Smuzhiyun 					qtd->urb->actual_length;
767*4882a593Smuzhiyun 			dev_vdbg(hsotg->dev, "buf=%08lx len=%d\n",
768*4882a593Smuzhiyun 				 (unsigned long)chan->xfer_dma, chan->xfer_len);
769*4882a593Smuzhiyun 		}
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 		qtd->n_desc = 0;
772*4882a593Smuzhiyun 		do {
773*4882a593Smuzhiyun 			if (n_desc > 1) {
774*4882a593Smuzhiyun 				qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
775*4882a593Smuzhiyun 				dev_vdbg(hsotg->dev,
776*4882a593Smuzhiyun 					 "set A bit in desc %d (%p)\n",
777*4882a593Smuzhiyun 					 n_desc - 1,
778*4882a593Smuzhiyun 					 &qh->desc_list[n_desc - 1]);
779*4882a593Smuzhiyun 				dma_sync_single_for_device(hsotg->dev,
780*4882a593Smuzhiyun 							   qh->desc_list_dma +
781*4882a593Smuzhiyun 					((n_desc - 1) *
782*4882a593Smuzhiyun 					sizeof(struct dwc2_dma_desc)),
783*4882a593Smuzhiyun 					sizeof(struct dwc2_dma_desc),
784*4882a593Smuzhiyun 					DMA_TO_DEVICE);
785*4882a593Smuzhiyun 			}
786*4882a593Smuzhiyun 			dwc2_fill_host_dma_desc(hsotg, chan, qtd, qh, n_desc);
787*4882a593Smuzhiyun 			dev_vdbg(hsotg->dev,
788*4882a593Smuzhiyun 				 "desc %d (%p) buf=%08x status=%08x\n",
789*4882a593Smuzhiyun 				 n_desc, &qh->desc_list[n_desc],
790*4882a593Smuzhiyun 				 qh->desc_list[n_desc].buf,
791*4882a593Smuzhiyun 				 qh->desc_list[n_desc].status);
792*4882a593Smuzhiyun 			qtd->n_desc++;
793*4882a593Smuzhiyun 			n_desc++;
794*4882a593Smuzhiyun 		} while (chan->xfer_len > 0 &&
795*4882a593Smuzhiyun 			 n_desc != MAX_DMA_DESC_NUM_GENERIC);
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 		dev_vdbg(hsotg->dev, "n_desc=%d\n", n_desc);
798*4882a593Smuzhiyun 		qtd->in_process = 1;
799*4882a593Smuzhiyun 		if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL)
800*4882a593Smuzhiyun 			break;
801*4882a593Smuzhiyun 		if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
802*4882a593Smuzhiyun 			break;
803*4882a593Smuzhiyun 	}
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	if (n_desc) {
806*4882a593Smuzhiyun 		qh->desc_list[n_desc - 1].status |=
807*4882a593Smuzhiyun 				HOST_DMA_IOC | HOST_DMA_EOL | HOST_DMA_A;
808*4882a593Smuzhiyun 		dev_vdbg(hsotg->dev, "set IOC/EOL/A bits in desc %d (%p)\n",
809*4882a593Smuzhiyun 			 n_desc - 1, &qh->desc_list[n_desc - 1]);
810*4882a593Smuzhiyun 		dma_sync_single_for_device(hsotg->dev,
811*4882a593Smuzhiyun 					   qh->desc_list_dma + (n_desc - 1) *
812*4882a593Smuzhiyun 					   sizeof(struct dwc2_dma_desc),
813*4882a593Smuzhiyun 					   sizeof(struct dwc2_dma_desc),
814*4882a593Smuzhiyun 					   DMA_TO_DEVICE);
815*4882a593Smuzhiyun 		if (n_desc > 1) {
816*4882a593Smuzhiyun 			qh->desc_list[0].status |= HOST_DMA_A;
817*4882a593Smuzhiyun 			dev_vdbg(hsotg->dev, "set A bit in desc 0 (%p)\n",
818*4882a593Smuzhiyun 				 &qh->desc_list[0]);
819*4882a593Smuzhiyun 			dma_sync_single_for_device(hsotg->dev,
820*4882a593Smuzhiyun 						   qh->desc_list_dma,
821*4882a593Smuzhiyun 					sizeof(struct dwc2_dma_desc),
822*4882a593Smuzhiyun 					DMA_TO_DEVICE);
823*4882a593Smuzhiyun 		}
824*4882a593Smuzhiyun 		chan->ntd = n_desc;
825*4882a593Smuzhiyun 	}
826*4882a593Smuzhiyun }
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun /**
829*4882a593Smuzhiyun  * dwc2_hcd_start_xfer_ddma() - Starts a transfer in Descriptor DMA mode
830*4882a593Smuzhiyun  *
831*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller
832*4882a593Smuzhiyun  * @qh:    The QH to init
833*4882a593Smuzhiyun  *
834*4882a593Smuzhiyun  * Return: 0 if successful, negative error code otherwise
835*4882a593Smuzhiyun  *
836*4882a593Smuzhiyun  * For Control and Bulk endpoints, initializes descriptor list and starts the
837*4882a593Smuzhiyun  * transfer. For Interrupt and Isochronous endpoints, initializes descriptor
838*4882a593Smuzhiyun  * list then updates FrameList, marking appropriate entries as active.
839*4882a593Smuzhiyun  *
840*4882a593Smuzhiyun  * For Isochronous endpoints the starting descriptor index is calculated based
841*4882a593Smuzhiyun  * on the scheduled frame, but only on the first transfer descriptor within a
842*4882a593Smuzhiyun  * session. Then the transfer is started via enabling the channel.
843*4882a593Smuzhiyun  *
844*4882a593Smuzhiyun  * For Isochronous endpoints the channel is not halted on XferComplete
845*4882a593Smuzhiyun  * interrupt so remains assigned to the endpoint(QH) until session is done.
846*4882a593Smuzhiyun  */
dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)847*4882a593Smuzhiyun void dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
848*4882a593Smuzhiyun {
849*4882a593Smuzhiyun 	/* Channel is already assigned */
850*4882a593Smuzhiyun 	struct dwc2_host_chan *chan = qh->channel;
851*4882a593Smuzhiyun 	u16 skip_frames = 0;
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 	switch (chan->ep_type) {
854*4882a593Smuzhiyun 	case USB_ENDPOINT_XFER_CONTROL:
855*4882a593Smuzhiyun 	case USB_ENDPOINT_XFER_BULK:
856*4882a593Smuzhiyun 		dwc2_init_non_isoc_dma_desc(hsotg, qh);
857*4882a593Smuzhiyun 		dwc2_hc_start_transfer_ddma(hsotg, chan);
858*4882a593Smuzhiyun 		break;
859*4882a593Smuzhiyun 	case USB_ENDPOINT_XFER_INT:
860*4882a593Smuzhiyun 		dwc2_init_non_isoc_dma_desc(hsotg, qh);
861*4882a593Smuzhiyun 		dwc2_update_frame_list(hsotg, qh, 1);
862*4882a593Smuzhiyun 		dwc2_hc_start_transfer_ddma(hsotg, chan);
863*4882a593Smuzhiyun 		break;
864*4882a593Smuzhiyun 	case USB_ENDPOINT_XFER_ISOC:
865*4882a593Smuzhiyun 		if (!qh->ntd)
866*4882a593Smuzhiyun 			skip_frames = dwc2_recalc_initial_desc_idx(hsotg, qh);
867*4882a593Smuzhiyun 		dwc2_init_isoc_dma_desc(hsotg, qh, skip_frames);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 		if (!chan->xfer_started) {
870*4882a593Smuzhiyun 			dwc2_update_frame_list(hsotg, qh, 1);
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 			/*
873*4882a593Smuzhiyun 			 * Always set to max, instead of actual size. Otherwise
874*4882a593Smuzhiyun 			 * ntd will be changed with channel being enabled. Not
875*4882a593Smuzhiyun 			 * recommended.
876*4882a593Smuzhiyun 			 */
877*4882a593Smuzhiyun 			chan->ntd = dwc2_max_desc_num(qh);
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 			/* Enable channel only once for ISOC */
880*4882a593Smuzhiyun 			dwc2_hc_start_transfer_ddma(hsotg, chan);
881*4882a593Smuzhiyun 		}
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 		break;
884*4882a593Smuzhiyun 	default:
885*4882a593Smuzhiyun 		break;
886*4882a593Smuzhiyun 	}
887*4882a593Smuzhiyun }
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun #define DWC2_CMPL_DONE		1
890*4882a593Smuzhiyun #define DWC2_CMPL_STOP		2
891*4882a593Smuzhiyun 
dwc2_cmpl_host_isoc_dma_desc(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,struct dwc2_qtd * qtd,struct dwc2_qh * qh,u16 idx)892*4882a593Smuzhiyun static int dwc2_cmpl_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
893*4882a593Smuzhiyun 					struct dwc2_host_chan *chan,
894*4882a593Smuzhiyun 					struct dwc2_qtd *qtd,
895*4882a593Smuzhiyun 					struct dwc2_qh *qh, u16 idx)
896*4882a593Smuzhiyun {
897*4882a593Smuzhiyun 	struct dwc2_dma_desc *dma_desc;
898*4882a593Smuzhiyun 	struct dwc2_hcd_iso_packet_desc *frame_desc;
899*4882a593Smuzhiyun 	u16 remain = 0;
900*4882a593Smuzhiyun 	int rc = 0;
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	if (!qtd->urb)
903*4882a593Smuzhiyun 		return -EINVAL;
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	dma_sync_single_for_cpu(hsotg->dev, qh->desc_list_dma + (idx *
906*4882a593Smuzhiyun 				sizeof(struct dwc2_dma_desc)),
907*4882a593Smuzhiyun 				sizeof(struct dwc2_dma_desc),
908*4882a593Smuzhiyun 				DMA_FROM_DEVICE);
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	dma_desc = &qh->desc_list[idx];
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
913*4882a593Smuzhiyun 	dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
914*4882a593Smuzhiyun 	if (chan->ep_is_in)
915*4882a593Smuzhiyun 		remain = (dma_desc->status & HOST_DMA_ISOC_NBYTES_MASK) >>
916*4882a593Smuzhiyun 			 HOST_DMA_ISOC_NBYTES_SHIFT;
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
919*4882a593Smuzhiyun 		/*
920*4882a593Smuzhiyun 		 * XactError, or unable to complete all the transactions
921*4882a593Smuzhiyun 		 * in the scheduled micro-frame/frame, both indicated by
922*4882a593Smuzhiyun 		 * HOST_DMA_STS_PKTERR
923*4882a593Smuzhiyun 		 */
924*4882a593Smuzhiyun 		qtd->urb->error_count++;
925*4882a593Smuzhiyun 		frame_desc->actual_length = qh->n_bytes[idx] - remain;
926*4882a593Smuzhiyun 		frame_desc->status = -EPROTO;
927*4882a593Smuzhiyun 	} else {
928*4882a593Smuzhiyun 		/* Success */
929*4882a593Smuzhiyun 		frame_desc->actual_length = qh->n_bytes[idx] - remain;
930*4882a593Smuzhiyun 		frame_desc->status = 0;
931*4882a593Smuzhiyun 	}
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
934*4882a593Smuzhiyun 		/*
935*4882a593Smuzhiyun 		 * urb->status is not used for isoc transfers here. The
936*4882a593Smuzhiyun 		 * individual frame_desc status are used instead.
937*4882a593Smuzhiyun 		 */
938*4882a593Smuzhiyun 		dwc2_host_complete(hsotg, qtd, 0);
939*4882a593Smuzhiyun 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 		/*
942*4882a593Smuzhiyun 		 * This check is necessary because urb_dequeue can be called
943*4882a593Smuzhiyun 		 * from urb complete callback (sound driver for example). All
944*4882a593Smuzhiyun 		 * pending URBs are dequeued there, so no need for further
945*4882a593Smuzhiyun 		 * processing.
946*4882a593Smuzhiyun 		 */
947*4882a593Smuzhiyun 		if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE)
948*4882a593Smuzhiyun 			return -1;
949*4882a593Smuzhiyun 		rc = DWC2_CMPL_DONE;
950*4882a593Smuzhiyun 	}
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	qh->ntd--;
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 	/* Stop if IOC requested descriptor reached */
955*4882a593Smuzhiyun 	if (dma_desc->status & HOST_DMA_IOC)
956*4882a593Smuzhiyun 		rc = DWC2_CMPL_STOP;
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	return rc;
959*4882a593Smuzhiyun }
960*4882a593Smuzhiyun 
dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,enum dwc2_halt_status halt_status)961*4882a593Smuzhiyun static void dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
962*4882a593Smuzhiyun 					 struct dwc2_host_chan *chan,
963*4882a593Smuzhiyun 					 enum dwc2_halt_status halt_status)
964*4882a593Smuzhiyun {
965*4882a593Smuzhiyun 	struct dwc2_hcd_iso_packet_desc *frame_desc;
966*4882a593Smuzhiyun 	struct dwc2_qtd *qtd, *qtd_tmp;
967*4882a593Smuzhiyun 	struct dwc2_qh *qh;
968*4882a593Smuzhiyun 	u16 idx;
969*4882a593Smuzhiyun 	int rc;
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	qh = chan->qh;
972*4882a593Smuzhiyun 	idx = qh->td_first;
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
975*4882a593Smuzhiyun 		list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
976*4882a593Smuzhiyun 			qtd->in_process = 0;
977*4882a593Smuzhiyun 		return;
978*4882a593Smuzhiyun 	}
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	if (halt_status == DWC2_HC_XFER_AHB_ERR ||
981*4882a593Smuzhiyun 	    halt_status == DWC2_HC_XFER_BABBLE_ERR) {
982*4882a593Smuzhiyun 		/*
983*4882a593Smuzhiyun 		 * Channel is halted in these error cases, considered as serious
984*4882a593Smuzhiyun 		 * issues.
985*4882a593Smuzhiyun 		 * Complete all URBs marking all frames as failed, irrespective
986*4882a593Smuzhiyun 		 * whether some of the descriptors (frames) succeeded or not.
987*4882a593Smuzhiyun 		 * Pass error code to completion routine as well, to update
988*4882a593Smuzhiyun 		 * urb->status, some of class drivers might use it to stop
989*4882a593Smuzhiyun 		 * queing transfer requests.
990*4882a593Smuzhiyun 		 */
991*4882a593Smuzhiyun 		int err = halt_status == DWC2_HC_XFER_AHB_ERR ?
992*4882a593Smuzhiyun 			  -EIO : -EOVERFLOW;
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
995*4882a593Smuzhiyun 					 qtd_list_entry) {
996*4882a593Smuzhiyun 			if (qtd->urb) {
997*4882a593Smuzhiyun 				for (idx = 0; idx < qtd->urb->packet_count;
998*4882a593Smuzhiyun 				     idx++) {
999*4882a593Smuzhiyun 					frame_desc = &qtd->urb->iso_descs[idx];
1000*4882a593Smuzhiyun 					frame_desc->status = err;
1001*4882a593Smuzhiyun 				}
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 				dwc2_host_complete(hsotg, qtd, err);
1004*4882a593Smuzhiyun 			}
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1007*4882a593Smuzhiyun 		}
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 		return;
1010*4882a593Smuzhiyun 	}
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun 	list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
1013*4882a593Smuzhiyun 		if (!qtd->in_process)
1014*4882a593Smuzhiyun 			break;
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 		/*
1017*4882a593Smuzhiyun 		 * Ensure idx corresponds to descriptor where first urb of this
1018*4882a593Smuzhiyun 		 * qtd was added. In fact, during isoc desc init, dwc2 may skip
1019*4882a593Smuzhiyun 		 * an index if current frame number is already over this index.
1020*4882a593Smuzhiyun 		 */
1021*4882a593Smuzhiyun 		if (idx != qtd->isoc_td_first) {
1022*4882a593Smuzhiyun 			dev_vdbg(hsotg->dev,
1023*4882a593Smuzhiyun 				 "try to complete %d instead of %d\n",
1024*4882a593Smuzhiyun 				 idx, qtd->isoc_td_first);
1025*4882a593Smuzhiyun 			idx = qtd->isoc_td_first;
1026*4882a593Smuzhiyun 		}
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun 		do {
1029*4882a593Smuzhiyun 			struct dwc2_qtd *qtd_next;
1030*4882a593Smuzhiyun 			u16 cur_idx;
1031*4882a593Smuzhiyun 
1032*4882a593Smuzhiyun 			rc = dwc2_cmpl_host_isoc_dma_desc(hsotg, chan, qtd, qh,
1033*4882a593Smuzhiyun 							  idx);
1034*4882a593Smuzhiyun 			if (rc < 0)
1035*4882a593Smuzhiyun 				return;
1036*4882a593Smuzhiyun 			idx = dwc2_desclist_idx_inc(idx, qh->host_interval,
1037*4882a593Smuzhiyun 						    chan->speed);
1038*4882a593Smuzhiyun 			if (!rc)
1039*4882a593Smuzhiyun 				continue;
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun 			if (rc == DWC2_CMPL_DONE)
1042*4882a593Smuzhiyun 				break;
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 			/* rc == DWC2_CMPL_STOP */
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 			if (qh->host_interval >= 32)
1047*4882a593Smuzhiyun 				goto stop_scan;
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 			qh->td_first = idx;
1050*4882a593Smuzhiyun 			cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
1051*4882a593Smuzhiyun 			qtd_next = list_first_entry(&qh->qtd_list,
1052*4882a593Smuzhiyun 						    struct dwc2_qtd,
1053*4882a593Smuzhiyun 						    qtd_list_entry);
1054*4882a593Smuzhiyun 			if (dwc2_frame_idx_num_gt(cur_idx,
1055*4882a593Smuzhiyun 						  qtd_next->isoc_td_last))
1056*4882a593Smuzhiyun 				break;
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 			goto stop_scan;
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 		} while (idx != qh->td_first);
1061*4882a593Smuzhiyun 	}
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun stop_scan:
1064*4882a593Smuzhiyun 	qh->td_first = idx;
1065*4882a593Smuzhiyun }
1066*4882a593Smuzhiyun 
dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,struct dwc2_qtd * qtd,struct dwc2_dma_desc * dma_desc,enum dwc2_halt_status halt_status,u32 n_bytes,int * xfer_done)1067*4882a593Smuzhiyun static int dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg *hsotg,
1068*4882a593Smuzhiyun 					       struct dwc2_host_chan *chan,
1069*4882a593Smuzhiyun 					struct dwc2_qtd *qtd,
1070*4882a593Smuzhiyun 					struct dwc2_dma_desc *dma_desc,
1071*4882a593Smuzhiyun 					enum dwc2_halt_status halt_status,
1072*4882a593Smuzhiyun 					u32 n_bytes, int *xfer_done)
1073*4882a593Smuzhiyun {
1074*4882a593Smuzhiyun 	struct dwc2_hcd_urb *urb = qtd->urb;
1075*4882a593Smuzhiyun 	u16 remain = 0;
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 	if (chan->ep_is_in)
1078*4882a593Smuzhiyun 		remain = (dma_desc->status & HOST_DMA_NBYTES_MASK) >>
1079*4882a593Smuzhiyun 			 HOST_DMA_NBYTES_SHIFT;
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	dev_vdbg(hsotg->dev, "remain=%d dwc2_urb=%p\n", remain, urb);
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	if (halt_status == DWC2_HC_XFER_AHB_ERR) {
1084*4882a593Smuzhiyun 		dev_err(hsotg->dev, "EIO\n");
1085*4882a593Smuzhiyun 		urb->status = -EIO;
1086*4882a593Smuzhiyun 		return 1;
1087*4882a593Smuzhiyun 	}
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 	if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
1090*4882a593Smuzhiyun 		switch (halt_status) {
1091*4882a593Smuzhiyun 		case DWC2_HC_XFER_STALL:
1092*4882a593Smuzhiyun 			dev_vdbg(hsotg->dev, "Stall\n");
1093*4882a593Smuzhiyun 			urb->status = -EPIPE;
1094*4882a593Smuzhiyun 			break;
1095*4882a593Smuzhiyun 		case DWC2_HC_XFER_BABBLE_ERR:
1096*4882a593Smuzhiyun 			dev_err(hsotg->dev, "Babble\n");
1097*4882a593Smuzhiyun 			urb->status = -EOVERFLOW;
1098*4882a593Smuzhiyun 			break;
1099*4882a593Smuzhiyun 		case DWC2_HC_XFER_XACT_ERR:
1100*4882a593Smuzhiyun 			dev_err(hsotg->dev, "XactErr\n");
1101*4882a593Smuzhiyun 			urb->status = -EPROTO;
1102*4882a593Smuzhiyun 			break;
1103*4882a593Smuzhiyun 		default:
1104*4882a593Smuzhiyun 			dev_err(hsotg->dev,
1105*4882a593Smuzhiyun 				"%s: Unhandled descriptor error status (%d)\n",
1106*4882a593Smuzhiyun 				__func__, halt_status);
1107*4882a593Smuzhiyun 			break;
1108*4882a593Smuzhiyun 		}
1109*4882a593Smuzhiyun 		return 1;
1110*4882a593Smuzhiyun 	}
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun 	if (dma_desc->status & HOST_DMA_A) {
1113*4882a593Smuzhiyun 		dev_vdbg(hsotg->dev,
1114*4882a593Smuzhiyun 			 "Active descriptor encountered on channel %d\n",
1115*4882a593Smuzhiyun 			 chan->hc_num);
1116*4882a593Smuzhiyun 		return 0;
1117*4882a593Smuzhiyun 	}
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun 	if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1120*4882a593Smuzhiyun 		if (qtd->control_phase == DWC2_CONTROL_DATA) {
1121*4882a593Smuzhiyun 			urb->actual_length += n_bytes - remain;
1122*4882a593Smuzhiyun 			if (remain || urb->actual_length >= urb->length) {
1123*4882a593Smuzhiyun 				/*
1124*4882a593Smuzhiyun 				 * For Control Data stage do not set urb->status
1125*4882a593Smuzhiyun 				 * to 0, to prevent URB callback. Set it when
1126*4882a593Smuzhiyun 				 * Status phase is done. See below.
1127*4882a593Smuzhiyun 				 */
1128*4882a593Smuzhiyun 				*xfer_done = 1;
1129*4882a593Smuzhiyun 			}
1130*4882a593Smuzhiyun 		} else if (qtd->control_phase == DWC2_CONTROL_STATUS) {
1131*4882a593Smuzhiyun 			urb->status = 0;
1132*4882a593Smuzhiyun 			*xfer_done = 1;
1133*4882a593Smuzhiyun 		}
1134*4882a593Smuzhiyun 		/* No handling for SETUP stage */
1135*4882a593Smuzhiyun 	} else {
1136*4882a593Smuzhiyun 		/* BULK and INTR */
1137*4882a593Smuzhiyun 		urb->actual_length += n_bytes - remain;
1138*4882a593Smuzhiyun 		dev_vdbg(hsotg->dev, "length=%d actual=%d\n", urb->length,
1139*4882a593Smuzhiyun 			 urb->actual_length);
1140*4882a593Smuzhiyun 		if (remain || urb->actual_length >= urb->length) {
1141*4882a593Smuzhiyun 			urb->status = 0;
1142*4882a593Smuzhiyun 			*xfer_done = 1;
1143*4882a593Smuzhiyun 		}
1144*4882a593Smuzhiyun 	}
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	return 0;
1147*4882a593Smuzhiyun }
1148*4882a593Smuzhiyun 
dwc2_process_non_isoc_desc(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,int chnum,struct dwc2_qtd * qtd,int desc_num,enum dwc2_halt_status halt_status,int * xfer_done)1149*4882a593Smuzhiyun static int dwc2_process_non_isoc_desc(struct dwc2_hsotg *hsotg,
1150*4882a593Smuzhiyun 				      struct dwc2_host_chan *chan,
1151*4882a593Smuzhiyun 				      int chnum, struct dwc2_qtd *qtd,
1152*4882a593Smuzhiyun 				      int desc_num,
1153*4882a593Smuzhiyun 				      enum dwc2_halt_status halt_status,
1154*4882a593Smuzhiyun 				      int *xfer_done)
1155*4882a593Smuzhiyun {
1156*4882a593Smuzhiyun 	struct dwc2_qh *qh = chan->qh;
1157*4882a593Smuzhiyun 	struct dwc2_hcd_urb *urb = qtd->urb;
1158*4882a593Smuzhiyun 	struct dwc2_dma_desc *dma_desc;
1159*4882a593Smuzhiyun 	u32 n_bytes;
1160*4882a593Smuzhiyun 	int failed;
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	if (!urb)
1165*4882a593Smuzhiyun 		return -EINVAL;
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun 	dma_sync_single_for_cpu(hsotg->dev,
1168*4882a593Smuzhiyun 				qh->desc_list_dma + (desc_num *
1169*4882a593Smuzhiyun 				sizeof(struct dwc2_dma_desc)),
1170*4882a593Smuzhiyun 				sizeof(struct dwc2_dma_desc),
1171*4882a593Smuzhiyun 				DMA_FROM_DEVICE);
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 	dma_desc = &qh->desc_list[desc_num];
1174*4882a593Smuzhiyun 	n_bytes = qh->n_bytes[desc_num];
1175*4882a593Smuzhiyun 	dev_vdbg(hsotg->dev,
1176*4882a593Smuzhiyun 		 "qtd=%p dwc2_urb=%p desc_num=%d desc=%p n_bytes=%d\n",
1177*4882a593Smuzhiyun 		 qtd, urb, desc_num, dma_desc, n_bytes);
1178*4882a593Smuzhiyun 	failed = dwc2_update_non_isoc_urb_state_ddma(hsotg, chan, qtd, dma_desc,
1179*4882a593Smuzhiyun 						     halt_status, n_bytes,
1180*4882a593Smuzhiyun 						     xfer_done);
1181*4882a593Smuzhiyun 	if (failed || (*xfer_done && urb->status != -EINPROGRESS)) {
1182*4882a593Smuzhiyun 		dwc2_host_complete(hsotg, qtd, urb->status);
1183*4882a593Smuzhiyun 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1184*4882a593Smuzhiyun 		dev_vdbg(hsotg->dev, "failed=%1x xfer_done=%1x\n",
1185*4882a593Smuzhiyun 			 failed, *xfer_done);
1186*4882a593Smuzhiyun 		return failed;
1187*4882a593Smuzhiyun 	}
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 	if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1190*4882a593Smuzhiyun 		switch (qtd->control_phase) {
1191*4882a593Smuzhiyun 		case DWC2_CONTROL_SETUP:
1192*4882a593Smuzhiyun 			if (urb->length > 0)
1193*4882a593Smuzhiyun 				qtd->control_phase = DWC2_CONTROL_DATA;
1194*4882a593Smuzhiyun 			else
1195*4882a593Smuzhiyun 				qtd->control_phase = DWC2_CONTROL_STATUS;
1196*4882a593Smuzhiyun 			dev_vdbg(hsotg->dev,
1197*4882a593Smuzhiyun 				 "  Control setup transaction done\n");
1198*4882a593Smuzhiyun 			break;
1199*4882a593Smuzhiyun 		case DWC2_CONTROL_DATA:
1200*4882a593Smuzhiyun 			if (*xfer_done) {
1201*4882a593Smuzhiyun 				qtd->control_phase = DWC2_CONTROL_STATUS;
1202*4882a593Smuzhiyun 				dev_vdbg(hsotg->dev,
1203*4882a593Smuzhiyun 					 "  Control data transfer done\n");
1204*4882a593Smuzhiyun 			} else if (desc_num + 1 == qtd->n_desc) {
1205*4882a593Smuzhiyun 				/*
1206*4882a593Smuzhiyun 				 * Last descriptor for Control data stage which
1207*4882a593Smuzhiyun 				 * is not completed yet
1208*4882a593Smuzhiyun 				 */
1209*4882a593Smuzhiyun 				dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
1210*4882a593Smuzhiyun 							  qtd);
1211*4882a593Smuzhiyun 			}
1212*4882a593Smuzhiyun 			break;
1213*4882a593Smuzhiyun 		default:
1214*4882a593Smuzhiyun 			break;
1215*4882a593Smuzhiyun 		}
1216*4882a593Smuzhiyun 	}
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	return 0;
1219*4882a593Smuzhiyun }
1220*4882a593Smuzhiyun 
dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,int chnum,enum dwc2_halt_status halt_status)1221*4882a593Smuzhiyun static void dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
1222*4882a593Smuzhiyun 					     struct dwc2_host_chan *chan,
1223*4882a593Smuzhiyun 					     int chnum,
1224*4882a593Smuzhiyun 					     enum dwc2_halt_status halt_status)
1225*4882a593Smuzhiyun {
1226*4882a593Smuzhiyun 	struct list_head *qtd_item, *qtd_tmp;
1227*4882a593Smuzhiyun 	struct dwc2_qh *qh = chan->qh;
1228*4882a593Smuzhiyun 	struct dwc2_qtd *qtd = NULL;
1229*4882a593Smuzhiyun 	int xfer_done;
1230*4882a593Smuzhiyun 	int desc_num = 0;
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 	if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
1233*4882a593Smuzhiyun 		list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
1234*4882a593Smuzhiyun 			qtd->in_process = 0;
1235*4882a593Smuzhiyun 		return;
1236*4882a593Smuzhiyun 	}
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun 	list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
1239*4882a593Smuzhiyun 		int i;
1240*4882a593Smuzhiyun 		int qtd_desc_count;
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun 		qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
1243*4882a593Smuzhiyun 		xfer_done = 0;
1244*4882a593Smuzhiyun 		qtd_desc_count = qtd->n_desc;
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 		for (i = 0; i < qtd_desc_count; i++) {
1247*4882a593Smuzhiyun 			if (dwc2_process_non_isoc_desc(hsotg, chan, chnum, qtd,
1248*4882a593Smuzhiyun 						       desc_num, halt_status,
1249*4882a593Smuzhiyun 						       &xfer_done)) {
1250*4882a593Smuzhiyun 				qtd = NULL;
1251*4882a593Smuzhiyun 				goto stop_scan;
1252*4882a593Smuzhiyun 			}
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun 			desc_num++;
1255*4882a593Smuzhiyun 		}
1256*4882a593Smuzhiyun 	}
1257*4882a593Smuzhiyun 
1258*4882a593Smuzhiyun stop_scan:
1259*4882a593Smuzhiyun 	if (qh->ep_type != USB_ENDPOINT_XFER_CONTROL) {
1260*4882a593Smuzhiyun 		/*
1261*4882a593Smuzhiyun 		 * Resetting the data toggle for bulk and interrupt endpoints
1262*4882a593Smuzhiyun 		 * in case of stall. See handle_hc_stall_intr().
1263*4882a593Smuzhiyun 		 */
1264*4882a593Smuzhiyun 		if (halt_status == DWC2_HC_XFER_STALL)
1265*4882a593Smuzhiyun 			qh->data_toggle = DWC2_HC_PID_DATA0;
1266*4882a593Smuzhiyun 		else
1267*4882a593Smuzhiyun 			dwc2_hcd_save_data_toggle(hsotg, chan, chnum, NULL);
1268*4882a593Smuzhiyun 	}
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	if (halt_status == DWC2_HC_XFER_COMPLETE) {
1271*4882a593Smuzhiyun 		if (chan->hcint & HCINTMSK_NYET) {
1272*4882a593Smuzhiyun 			/*
1273*4882a593Smuzhiyun 			 * Got a NYET on the last transaction of the transfer.
1274*4882a593Smuzhiyun 			 * It means that the endpoint should be in the PING
1275*4882a593Smuzhiyun 			 * state at the beginning of the next transfer.
1276*4882a593Smuzhiyun 			 */
1277*4882a593Smuzhiyun 			qh->ping_state = 1;
1278*4882a593Smuzhiyun 		}
1279*4882a593Smuzhiyun 	}
1280*4882a593Smuzhiyun }
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun /**
1283*4882a593Smuzhiyun  * dwc2_hcd_complete_xfer_ddma() - Scans the descriptor list, updates URB's
1284*4882a593Smuzhiyun  * status and calls completion routine for the URB if it's done. Called from
1285*4882a593Smuzhiyun  * interrupt handlers.
1286*4882a593Smuzhiyun  *
1287*4882a593Smuzhiyun  * @hsotg:       The HCD state structure for the DWC OTG controller
1288*4882a593Smuzhiyun  * @chan:        Host channel the transfer is completed on
1289*4882a593Smuzhiyun  * @chnum:       Index of Host channel registers
1290*4882a593Smuzhiyun  * @halt_status: Reason the channel is being halted or just XferComplete
1291*4882a593Smuzhiyun  *               for isochronous transfers
1292*4882a593Smuzhiyun  *
1293*4882a593Smuzhiyun  * Releases the channel to be used by other transfers.
1294*4882a593Smuzhiyun  * In case of Isochronous endpoint the channel is not halted until the end of
1295*4882a593Smuzhiyun  * the session, i.e. QTD list is empty.
1296*4882a593Smuzhiyun  * If periodic channel released the FrameList is updated accordingly.
1297*4882a593Smuzhiyun  * Calls transaction selection routines to activate pending transfers.
1298*4882a593Smuzhiyun  */
dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,int chnum,enum dwc2_halt_status halt_status)1299*4882a593Smuzhiyun void dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg *hsotg,
1300*4882a593Smuzhiyun 				 struct dwc2_host_chan *chan, int chnum,
1301*4882a593Smuzhiyun 				 enum dwc2_halt_status halt_status)
1302*4882a593Smuzhiyun {
1303*4882a593Smuzhiyun 	struct dwc2_qh *qh = chan->qh;
1304*4882a593Smuzhiyun 	int continue_isoc_xfer = 0;
1305*4882a593Smuzhiyun 	enum dwc2_transaction_type tr_type;
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun 	if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1308*4882a593Smuzhiyun 		dwc2_complete_isoc_xfer_ddma(hsotg, chan, halt_status);
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 		/* Release the channel if halted or session completed */
1311*4882a593Smuzhiyun 		if (halt_status != DWC2_HC_XFER_COMPLETE ||
1312*4882a593Smuzhiyun 		    list_empty(&qh->qtd_list)) {
1313*4882a593Smuzhiyun 			struct dwc2_qtd *qtd, *qtd_tmp;
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 			/*
1316*4882a593Smuzhiyun 			 * Kill all remainings QTDs since channel has been
1317*4882a593Smuzhiyun 			 * halted.
1318*4882a593Smuzhiyun 			 */
1319*4882a593Smuzhiyun 			list_for_each_entry_safe(qtd, qtd_tmp,
1320*4882a593Smuzhiyun 						 &qh->qtd_list,
1321*4882a593Smuzhiyun 						 qtd_list_entry) {
1322*4882a593Smuzhiyun 				dwc2_host_complete(hsotg, qtd,
1323*4882a593Smuzhiyun 						   -ECONNRESET);
1324*4882a593Smuzhiyun 				dwc2_hcd_qtd_unlink_and_free(hsotg,
1325*4882a593Smuzhiyun 							     qtd, qh);
1326*4882a593Smuzhiyun 			}
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun 			/* Halt the channel if session completed */
1329*4882a593Smuzhiyun 			if (halt_status == DWC2_HC_XFER_COMPLETE)
1330*4882a593Smuzhiyun 				dwc2_hc_halt(hsotg, chan, halt_status);
1331*4882a593Smuzhiyun 			dwc2_release_channel_ddma(hsotg, qh);
1332*4882a593Smuzhiyun 			dwc2_hcd_qh_unlink(hsotg, qh);
1333*4882a593Smuzhiyun 		} else {
1334*4882a593Smuzhiyun 			/* Keep in assigned schedule to continue transfer */
1335*4882a593Smuzhiyun 			list_move_tail(&qh->qh_list_entry,
1336*4882a593Smuzhiyun 				       &hsotg->periodic_sched_assigned);
1337*4882a593Smuzhiyun 			/*
1338*4882a593Smuzhiyun 			 * If channel has been halted during giveback of urb
1339*4882a593Smuzhiyun 			 * then prevent any new scheduling.
1340*4882a593Smuzhiyun 			 */
1341*4882a593Smuzhiyun 			if (!chan->halt_status)
1342*4882a593Smuzhiyun 				continue_isoc_xfer = 1;
1343*4882a593Smuzhiyun 		}
1344*4882a593Smuzhiyun 		/*
1345*4882a593Smuzhiyun 		 * Todo: Consider the case when period exceeds FrameList size.
1346*4882a593Smuzhiyun 		 * Frame Rollover interrupt should be used.
1347*4882a593Smuzhiyun 		 */
1348*4882a593Smuzhiyun 	} else {
1349*4882a593Smuzhiyun 		/*
1350*4882a593Smuzhiyun 		 * Scan descriptor list to complete the URB(s), then release
1351*4882a593Smuzhiyun 		 * the channel
1352*4882a593Smuzhiyun 		 */
1353*4882a593Smuzhiyun 		dwc2_complete_non_isoc_xfer_ddma(hsotg, chan, chnum,
1354*4882a593Smuzhiyun 						 halt_status);
1355*4882a593Smuzhiyun 		dwc2_release_channel_ddma(hsotg, qh);
1356*4882a593Smuzhiyun 		dwc2_hcd_qh_unlink(hsotg, qh);
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 		if (!list_empty(&qh->qtd_list)) {
1359*4882a593Smuzhiyun 			/*
1360*4882a593Smuzhiyun 			 * Add back to inactive non-periodic schedule on normal
1361*4882a593Smuzhiyun 			 * completion
1362*4882a593Smuzhiyun 			 */
1363*4882a593Smuzhiyun 			dwc2_hcd_qh_add(hsotg, qh);
1364*4882a593Smuzhiyun 		}
1365*4882a593Smuzhiyun 	}
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	tr_type = dwc2_hcd_select_transactions(hsotg);
1368*4882a593Smuzhiyun 	if (tr_type != DWC2_TRANSACTION_NONE || continue_isoc_xfer) {
1369*4882a593Smuzhiyun 		if (continue_isoc_xfer) {
1370*4882a593Smuzhiyun 			if (tr_type == DWC2_TRANSACTION_NONE)
1371*4882a593Smuzhiyun 				tr_type = DWC2_TRANSACTION_PERIODIC;
1372*4882a593Smuzhiyun 			else if (tr_type == DWC2_TRANSACTION_NON_PERIODIC)
1373*4882a593Smuzhiyun 				tr_type = DWC2_TRANSACTION_ALL;
1374*4882a593Smuzhiyun 		}
1375*4882a593Smuzhiyun 		dwc2_hcd_queue_transactions(hsotg, tr_type);
1376*4882a593Smuzhiyun 	}
1377*4882a593Smuzhiyun }
1378