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