xref: /openbmc/linux/drivers/usb/dwc2/hcd_ddma.c (revision 94ef7aee11c26e79441276ca43f0c25a04bd1303)
1197ba5f4SPaul Zimmerman /*
2197ba5f4SPaul Zimmerman  * hcd_ddma.c - DesignWare HS OTG Controller descriptor DMA routines
3197ba5f4SPaul Zimmerman  *
4197ba5f4SPaul Zimmerman  * Copyright (C) 2004-2013 Synopsys, Inc.
5197ba5f4SPaul Zimmerman  *
6197ba5f4SPaul Zimmerman  * Redistribution and use in source and binary forms, with or without
7197ba5f4SPaul Zimmerman  * modification, are permitted provided that the following conditions
8197ba5f4SPaul Zimmerman  * are met:
9197ba5f4SPaul Zimmerman  * 1. Redistributions of source code must retain the above copyright
10197ba5f4SPaul Zimmerman  *    notice, this list of conditions, and the following disclaimer,
11197ba5f4SPaul Zimmerman  *    without modification.
12197ba5f4SPaul Zimmerman  * 2. Redistributions in binary form must reproduce the above copyright
13197ba5f4SPaul Zimmerman  *    notice, this list of conditions and the following disclaimer in the
14197ba5f4SPaul Zimmerman  *    documentation and/or other materials provided with the distribution.
15197ba5f4SPaul Zimmerman  * 3. The names of the above-listed copyright holders may not be used
16197ba5f4SPaul Zimmerman  *    to endorse or promote products derived from this software without
17197ba5f4SPaul Zimmerman  *    specific prior written permission.
18197ba5f4SPaul Zimmerman  *
19197ba5f4SPaul Zimmerman  * ALTERNATIVELY, this software may be distributed under the terms of the
20197ba5f4SPaul Zimmerman  * GNU General Public License ("GPL") as published by the Free Software
21197ba5f4SPaul Zimmerman  * Foundation; either version 2 of the License, or (at your option) any
22197ba5f4SPaul Zimmerman  * later version.
23197ba5f4SPaul Zimmerman  *
24197ba5f4SPaul Zimmerman  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25197ba5f4SPaul Zimmerman  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26197ba5f4SPaul Zimmerman  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27197ba5f4SPaul Zimmerman  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28197ba5f4SPaul Zimmerman  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29197ba5f4SPaul Zimmerman  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30197ba5f4SPaul Zimmerman  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31197ba5f4SPaul Zimmerman  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32197ba5f4SPaul Zimmerman  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33197ba5f4SPaul Zimmerman  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34197ba5f4SPaul Zimmerman  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35197ba5f4SPaul Zimmerman  */
36197ba5f4SPaul Zimmerman 
37197ba5f4SPaul Zimmerman /*
38197ba5f4SPaul Zimmerman  * This file contains the Descriptor DMA implementation for Host mode
39197ba5f4SPaul Zimmerman  */
40197ba5f4SPaul Zimmerman #include <linux/kernel.h>
41197ba5f4SPaul Zimmerman #include <linux/module.h>
42197ba5f4SPaul Zimmerman #include <linux/spinlock.h>
43197ba5f4SPaul Zimmerman #include <linux/interrupt.h>
44197ba5f4SPaul Zimmerman #include <linux/dma-mapping.h>
45197ba5f4SPaul Zimmerman #include <linux/io.h>
46197ba5f4SPaul Zimmerman #include <linux/slab.h>
47197ba5f4SPaul Zimmerman #include <linux/usb.h>
48197ba5f4SPaul Zimmerman 
49197ba5f4SPaul Zimmerman #include <linux/usb/hcd.h>
50197ba5f4SPaul Zimmerman #include <linux/usb/ch11.h>
51197ba5f4SPaul Zimmerman 
52197ba5f4SPaul Zimmerman #include "core.h"
53197ba5f4SPaul Zimmerman #include "hcd.h"
54197ba5f4SPaul Zimmerman 
55197ba5f4SPaul Zimmerman static u16 dwc2_frame_list_idx(u16 frame)
56197ba5f4SPaul Zimmerman {
57197ba5f4SPaul Zimmerman 	return frame & (FRLISTEN_64_SIZE - 1);
58197ba5f4SPaul Zimmerman }
59197ba5f4SPaul Zimmerman 
60197ba5f4SPaul Zimmerman static u16 dwc2_desclist_idx_inc(u16 idx, u16 inc, u8 speed)
61197ba5f4SPaul Zimmerman {
62197ba5f4SPaul Zimmerman 	return (idx + inc) &
63197ba5f4SPaul Zimmerman 		((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
64197ba5f4SPaul Zimmerman 		  MAX_DMA_DESC_NUM_GENERIC) - 1);
65197ba5f4SPaul Zimmerman }
66197ba5f4SPaul Zimmerman 
67197ba5f4SPaul Zimmerman static u16 dwc2_desclist_idx_dec(u16 idx, u16 inc, u8 speed)
68197ba5f4SPaul Zimmerman {
69197ba5f4SPaul Zimmerman 	return (idx - inc) &
70197ba5f4SPaul Zimmerman 		((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
71197ba5f4SPaul Zimmerman 		  MAX_DMA_DESC_NUM_GENERIC) - 1);
72197ba5f4SPaul Zimmerman }
73197ba5f4SPaul Zimmerman 
74197ba5f4SPaul Zimmerman static u16 dwc2_max_desc_num(struct dwc2_qh *qh)
75197ba5f4SPaul Zimmerman {
76197ba5f4SPaul Zimmerman 	return (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
77197ba5f4SPaul Zimmerman 		qh->dev_speed == USB_SPEED_HIGH) ?
78197ba5f4SPaul Zimmerman 		MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC;
79197ba5f4SPaul Zimmerman }
80197ba5f4SPaul Zimmerman 
81197ba5f4SPaul Zimmerman static u16 dwc2_frame_incr_val(struct dwc2_qh *qh)
82197ba5f4SPaul Zimmerman {
83197ba5f4SPaul Zimmerman 	return qh->dev_speed == USB_SPEED_HIGH ?
84197ba5f4SPaul Zimmerman 	       (qh->interval + 8 - 1) / 8 : qh->interval;
85197ba5f4SPaul Zimmerman }
86197ba5f4SPaul Zimmerman 
87197ba5f4SPaul Zimmerman static int dwc2_desc_list_alloc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
88197ba5f4SPaul Zimmerman 				gfp_t flags)
89197ba5f4SPaul Zimmerman {
903b5fcc9aSGregory Herrero 	struct kmem_cache *desc_cache;
913b5fcc9aSGregory Herrero 
923b5fcc9aSGregory Herrero 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC
933b5fcc9aSGregory Herrero 	    && qh->dev_speed == USB_SPEED_HIGH)
943b5fcc9aSGregory Herrero 		desc_cache = hsotg->desc_hsisoc_cache;
953b5fcc9aSGregory Herrero 	else
963b5fcc9aSGregory Herrero 		desc_cache = hsotg->desc_gen_cache;
973b5fcc9aSGregory Herrero 
9895105a99SGregory Herrero 	qh->desc_list_sz = sizeof(struct dwc2_hcd_dma_desc) *
9995105a99SGregory Herrero 						dwc2_max_desc_num(qh);
100197ba5f4SPaul Zimmerman 
1013b5fcc9aSGregory Herrero 	qh->desc_list = kmem_cache_zalloc(desc_cache, flags | GFP_DMA);
102197ba5f4SPaul Zimmerman 	if (!qh->desc_list)
103197ba5f4SPaul Zimmerman 		return -ENOMEM;
104197ba5f4SPaul Zimmerman 
10595105a99SGregory Herrero 	qh->desc_list_dma = dma_map_single(hsotg->dev, qh->desc_list,
10695105a99SGregory Herrero 					   qh->desc_list_sz,
10795105a99SGregory Herrero 					   DMA_TO_DEVICE);
108197ba5f4SPaul Zimmerman 
109197ba5f4SPaul Zimmerman 	qh->n_bytes = kzalloc(sizeof(u32) * dwc2_max_desc_num(qh), flags);
110197ba5f4SPaul Zimmerman 	if (!qh->n_bytes) {
11195105a99SGregory Herrero 		dma_unmap_single(hsotg->dev, qh->desc_list_dma,
11295105a99SGregory Herrero 				 qh->desc_list_sz,
11395105a99SGregory Herrero 				 DMA_FROM_DEVICE);
11495105a99SGregory Herrero 		kfree(qh->desc_list);
115197ba5f4SPaul Zimmerman 		qh->desc_list = NULL;
116197ba5f4SPaul Zimmerman 		return -ENOMEM;
117197ba5f4SPaul Zimmerman 	}
118197ba5f4SPaul Zimmerman 
119197ba5f4SPaul Zimmerman 	return 0;
120197ba5f4SPaul Zimmerman }
121197ba5f4SPaul Zimmerman 
122197ba5f4SPaul Zimmerman static void dwc2_desc_list_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
123197ba5f4SPaul Zimmerman {
1243b5fcc9aSGregory Herrero 	struct kmem_cache *desc_cache;
1253b5fcc9aSGregory Herrero 
1263b5fcc9aSGregory Herrero 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC
1273b5fcc9aSGregory Herrero 	    && qh->dev_speed == USB_SPEED_HIGH)
1283b5fcc9aSGregory Herrero 		desc_cache = hsotg->desc_hsisoc_cache;
1293b5fcc9aSGregory Herrero 	else
1303b5fcc9aSGregory Herrero 		desc_cache = hsotg->desc_gen_cache;
1313b5fcc9aSGregory Herrero 
132197ba5f4SPaul Zimmerman 	if (qh->desc_list) {
13395105a99SGregory Herrero 		dma_unmap_single(hsotg->dev, qh->desc_list_dma,
13495105a99SGregory Herrero 				 qh->desc_list_sz, DMA_FROM_DEVICE);
1353b5fcc9aSGregory Herrero 		kmem_cache_free(desc_cache, qh->desc_list);
136197ba5f4SPaul Zimmerman 		qh->desc_list = NULL;
137197ba5f4SPaul Zimmerman 	}
138197ba5f4SPaul Zimmerman 
139197ba5f4SPaul Zimmerman 	kfree(qh->n_bytes);
140197ba5f4SPaul Zimmerman 	qh->n_bytes = NULL;
141197ba5f4SPaul Zimmerman }
142197ba5f4SPaul Zimmerman 
143197ba5f4SPaul Zimmerman static int dwc2_frame_list_alloc(struct dwc2_hsotg *hsotg, gfp_t mem_flags)
144197ba5f4SPaul Zimmerman {
145197ba5f4SPaul Zimmerman 	if (hsotg->frame_list)
146197ba5f4SPaul Zimmerman 		return 0;
147197ba5f4SPaul Zimmerman 
14895105a99SGregory Herrero 	hsotg->frame_list_sz = 4 * FRLISTEN_64_SIZE;
14995105a99SGregory Herrero 	hsotg->frame_list = kzalloc(hsotg->frame_list_sz, GFP_ATOMIC | GFP_DMA);
150197ba5f4SPaul Zimmerman 	if (!hsotg->frame_list)
151197ba5f4SPaul Zimmerman 		return -ENOMEM;
152197ba5f4SPaul Zimmerman 
15395105a99SGregory Herrero 	hsotg->frame_list_dma = dma_map_single(hsotg->dev, hsotg->frame_list,
15495105a99SGregory Herrero 					       hsotg->frame_list_sz,
15595105a99SGregory Herrero 					       DMA_TO_DEVICE);
15695105a99SGregory Herrero 
157197ba5f4SPaul Zimmerman 	return 0;
158197ba5f4SPaul Zimmerman }
159197ba5f4SPaul Zimmerman 
160197ba5f4SPaul Zimmerman static void dwc2_frame_list_free(struct dwc2_hsotg *hsotg)
161197ba5f4SPaul Zimmerman {
162197ba5f4SPaul Zimmerman 	unsigned long flags;
163197ba5f4SPaul Zimmerman 
164197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
165197ba5f4SPaul Zimmerman 
166197ba5f4SPaul Zimmerman 	if (!hsotg->frame_list) {
167197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
168197ba5f4SPaul Zimmerman 		return;
169197ba5f4SPaul Zimmerman 	}
170197ba5f4SPaul Zimmerman 
17195105a99SGregory Herrero 	dma_unmap_single(hsotg->dev, hsotg->frame_list_dma,
17295105a99SGregory Herrero 			 hsotg->frame_list_sz, DMA_FROM_DEVICE);
17395105a99SGregory Herrero 
17495105a99SGregory Herrero 	kfree(hsotg->frame_list);
175197ba5f4SPaul Zimmerman 	hsotg->frame_list = NULL;
176197ba5f4SPaul Zimmerman 
177197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
178197ba5f4SPaul Zimmerman 
179197ba5f4SPaul Zimmerman }
180197ba5f4SPaul Zimmerman 
181197ba5f4SPaul Zimmerman static void dwc2_per_sched_enable(struct dwc2_hsotg *hsotg, u32 fr_list_en)
182197ba5f4SPaul Zimmerman {
183197ba5f4SPaul Zimmerman 	u32 hcfg;
184197ba5f4SPaul Zimmerman 	unsigned long flags;
185197ba5f4SPaul Zimmerman 
186197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
187197ba5f4SPaul Zimmerman 
18895c8bc36SAntti Seppälä 	hcfg = dwc2_readl(hsotg->regs + HCFG);
189197ba5f4SPaul Zimmerman 	if (hcfg & HCFG_PERSCHEDENA) {
190197ba5f4SPaul Zimmerman 		/* already enabled */
191197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
192197ba5f4SPaul Zimmerman 		return;
193197ba5f4SPaul Zimmerman 	}
194197ba5f4SPaul Zimmerman 
19595c8bc36SAntti Seppälä 	dwc2_writel(hsotg->frame_list_dma, hsotg->regs + HFLBADDR);
196197ba5f4SPaul Zimmerman 
197197ba5f4SPaul Zimmerman 	hcfg &= ~HCFG_FRLISTEN_MASK;
198197ba5f4SPaul Zimmerman 	hcfg |= fr_list_en | HCFG_PERSCHEDENA;
199197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Enabling Periodic schedule\n");
20095c8bc36SAntti Seppälä 	dwc2_writel(hcfg, hsotg->regs + HCFG);
201197ba5f4SPaul Zimmerman 
202197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
203197ba5f4SPaul Zimmerman }
204197ba5f4SPaul Zimmerman 
205197ba5f4SPaul Zimmerman static void dwc2_per_sched_disable(struct dwc2_hsotg *hsotg)
206197ba5f4SPaul Zimmerman {
207197ba5f4SPaul Zimmerman 	u32 hcfg;
208197ba5f4SPaul Zimmerman 	unsigned long flags;
209197ba5f4SPaul Zimmerman 
210197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
211197ba5f4SPaul Zimmerman 
21295c8bc36SAntti Seppälä 	hcfg = dwc2_readl(hsotg->regs + HCFG);
213197ba5f4SPaul Zimmerman 	if (!(hcfg & HCFG_PERSCHEDENA)) {
214197ba5f4SPaul Zimmerman 		/* already disabled */
215197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
216197ba5f4SPaul Zimmerman 		return;
217197ba5f4SPaul Zimmerman 	}
218197ba5f4SPaul Zimmerman 
219197ba5f4SPaul Zimmerman 	hcfg &= ~HCFG_PERSCHEDENA;
220197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Disabling Periodic schedule\n");
22195c8bc36SAntti Seppälä 	dwc2_writel(hcfg, hsotg->regs + HCFG);
222197ba5f4SPaul Zimmerman 
223197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
224197ba5f4SPaul Zimmerman }
225197ba5f4SPaul Zimmerman 
226197ba5f4SPaul Zimmerman /*
227197ba5f4SPaul Zimmerman  * Activates/Deactivates FrameList entries for the channel based on endpoint
228197ba5f4SPaul Zimmerman  * servicing period
229197ba5f4SPaul Zimmerman  */
230197ba5f4SPaul Zimmerman static void dwc2_update_frame_list(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
231197ba5f4SPaul Zimmerman 				   int enable)
232197ba5f4SPaul Zimmerman {
233197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
234197ba5f4SPaul Zimmerman 	u16 i, j, inc;
235197ba5f4SPaul Zimmerman 
236197ba5f4SPaul Zimmerman 	if (!hsotg) {
237197ba5f4SPaul Zimmerman 		pr_err("hsotg = %p\n", hsotg);
238197ba5f4SPaul Zimmerman 		return;
239197ba5f4SPaul Zimmerman 	}
240197ba5f4SPaul Zimmerman 
241197ba5f4SPaul Zimmerman 	if (!qh->channel) {
242197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "qh->channel = %p\n", qh->channel);
243197ba5f4SPaul Zimmerman 		return;
244197ba5f4SPaul Zimmerman 	}
245197ba5f4SPaul Zimmerman 
246197ba5f4SPaul Zimmerman 	if (!hsotg->frame_list) {
247197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "hsotg->frame_list = %p\n",
248197ba5f4SPaul Zimmerman 			hsotg->frame_list);
249197ba5f4SPaul Zimmerman 		return;
250197ba5f4SPaul Zimmerman 	}
251197ba5f4SPaul Zimmerman 
252197ba5f4SPaul Zimmerman 	chan = qh->channel;
253197ba5f4SPaul Zimmerman 	inc = dwc2_frame_incr_val(qh);
254197ba5f4SPaul Zimmerman 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC)
255197ba5f4SPaul Zimmerman 		i = dwc2_frame_list_idx(qh->sched_frame);
256197ba5f4SPaul Zimmerman 	else
257197ba5f4SPaul Zimmerman 		i = 0;
258197ba5f4SPaul Zimmerman 
259197ba5f4SPaul Zimmerman 	j = i;
260197ba5f4SPaul Zimmerman 	do {
261197ba5f4SPaul Zimmerman 		if (enable)
262197ba5f4SPaul Zimmerman 			hsotg->frame_list[j] |= 1 << chan->hc_num;
263197ba5f4SPaul Zimmerman 		else
264197ba5f4SPaul Zimmerman 			hsotg->frame_list[j] &= ~(1 << chan->hc_num);
265197ba5f4SPaul Zimmerman 		j = (j + inc) & (FRLISTEN_64_SIZE - 1);
266197ba5f4SPaul Zimmerman 	} while (j != i);
267197ba5f4SPaul Zimmerman 
26895105a99SGregory Herrero 	/*
26995105a99SGregory Herrero 	 * Sync frame list since controller will access it if periodic
27095105a99SGregory Herrero 	 * channel is currently enabled.
27195105a99SGregory Herrero 	 */
27295105a99SGregory Herrero 	dma_sync_single_for_device(hsotg->dev,
27395105a99SGregory Herrero 				   hsotg->frame_list_dma,
27495105a99SGregory Herrero 				   hsotg->frame_list_sz,
27595105a99SGregory Herrero 				   DMA_TO_DEVICE);
27695105a99SGregory Herrero 
277197ba5f4SPaul Zimmerman 	if (!enable)
278197ba5f4SPaul Zimmerman 		return;
279197ba5f4SPaul Zimmerman 
280197ba5f4SPaul Zimmerman 	chan->schinfo = 0;
281197ba5f4SPaul Zimmerman 	if (chan->speed == USB_SPEED_HIGH && qh->interval) {
282197ba5f4SPaul Zimmerman 		j = 1;
283197ba5f4SPaul Zimmerman 		/* TODO - check this */
284197ba5f4SPaul Zimmerman 		inc = (8 + qh->interval - 1) / qh->interval;
285197ba5f4SPaul Zimmerman 		for (i = 0; i < inc; i++) {
286197ba5f4SPaul Zimmerman 			chan->schinfo |= j;
287197ba5f4SPaul Zimmerman 			j = j << qh->interval;
288197ba5f4SPaul Zimmerman 		}
289197ba5f4SPaul Zimmerman 	} else {
290197ba5f4SPaul Zimmerman 		chan->schinfo = 0xff;
291197ba5f4SPaul Zimmerman 	}
292197ba5f4SPaul Zimmerman }
293197ba5f4SPaul Zimmerman 
294197ba5f4SPaul Zimmerman static void dwc2_release_channel_ddma(struct dwc2_hsotg *hsotg,
295197ba5f4SPaul Zimmerman 				      struct dwc2_qh *qh)
296197ba5f4SPaul Zimmerman {
297197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan = qh->channel;
298197ba5f4SPaul Zimmerman 
299197ba5f4SPaul Zimmerman 	if (dwc2_qh_is_non_per(qh)) {
300197ba5f4SPaul Zimmerman 		if (hsotg->core_params->uframe_sched > 0)
301197ba5f4SPaul Zimmerman 			hsotg->available_host_channels++;
302197ba5f4SPaul Zimmerman 		else
303197ba5f4SPaul Zimmerman 			hsotg->non_periodic_channels--;
304197ba5f4SPaul Zimmerman 	} else {
305197ba5f4SPaul Zimmerman 		dwc2_update_frame_list(hsotg, qh, 0);
3063f808bdaSGregory Herrero 		hsotg->available_host_channels++;
307197ba5f4SPaul Zimmerman 	}
308197ba5f4SPaul Zimmerman 
309197ba5f4SPaul Zimmerman 	/*
310197ba5f4SPaul Zimmerman 	 * The condition is added to prevent double cleanup try in case of
311197ba5f4SPaul Zimmerman 	 * device disconnect. See channel cleanup in dwc2_hcd_disconnect().
312197ba5f4SPaul Zimmerman 	 */
313197ba5f4SPaul Zimmerman 	if (chan->qh) {
314197ba5f4SPaul Zimmerman 		if (!list_empty(&chan->hc_list_entry))
315197ba5f4SPaul Zimmerman 			list_del(&chan->hc_list_entry);
316197ba5f4SPaul Zimmerman 		dwc2_hc_cleanup(hsotg, chan);
317197ba5f4SPaul Zimmerman 		list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
318197ba5f4SPaul Zimmerman 		chan->qh = NULL;
319197ba5f4SPaul Zimmerman 	}
320197ba5f4SPaul Zimmerman 
321197ba5f4SPaul Zimmerman 	qh->channel = NULL;
322197ba5f4SPaul Zimmerman 	qh->ntd = 0;
323197ba5f4SPaul Zimmerman 
324197ba5f4SPaul Zimmerman 	if (qh->desc_list)
325197ba5f4SPaul Zimmerman 		memset(qh->desc_list, 0, sizeof(struct dwc2_hcd_dma_desc) *
326197ba5f4SPaul Zimmerman 		       dwc2_max_desc_num(qh));
327197ba5f4SPaul Zimmerman }
328197ba5f4SPaul Zimmerman 
329197ba5f4SPaul Zimmerman /**
330197ba5f4SPaul Zimmerman  * dwc2_hcd_qh_init_ddma() - Initializes a QH structure's Descriptor DMA
331197ba5f4SPaul Zimmerman  * related members
332197ba5f4SPaul Zimmerman  *
333197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure for the DWC OTG controller
334197ba5f4SPaul Zimmerman  * @qh:    The QH to init
335197ba5f4SPaul Zimmerman  *
336197ba5f4SPaul Zimmerman  * Return: 0 if successful, negative error code otherwise
337197ba5f4SPaul Zimmerman  *
338197ba5f4SPaul Zimmerman  * Allocates memory for the descriptor list. For the first periodic QH,
339197ba5f4SPaul Zimmerman  * allocates memory for the FrameList and enables periodic scheduling.
340197ba5f4SPaul Zimmerman  */
341197ba5f4SPaul Zimmerman int dwc2_hcd_qh_init_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
342197ba5f4SPaul Zimmerman 			  gfp_t mem_flags)
343197ba5f4SPaul Zimmerman {
344197ba5f4SPaul Zimmerman 	int retval;
345197ba5f4SPaul Zimmerman 
346197ba5f4SPaul Zimmerman 	if (qh->do_split) {
347197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev,
348197ba5f4SPaul Zimmerman 			"SPLIT Transfers are not supported in Descriptor DMA mode.\n");
349197ba5f4SPaul Zimmerman 		retval = -EINVAL;
350197ba5f4SPaul Zimmerman 		goto err0;
351197ba5f4SPaul Zimmerman 	}
352197ba5f4SPaul Zimmerman 
353197ba5f4SPaul Zimmerman 	retval = dwc2_desc_list_alloc(hsotg, qh, mem_flags);
354197ba5f4SPaul Zimmerman 	if (retval)
355197ba5f4SPaul Zimmerman 		goto err0;
356197ba5f4SPaul Zimmerman 
357197ba5f4SPaul Zimmerman 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
358197ba5f4SPaul Zimmerman 	    qh->ep_type == USB_ENDPOINT_XFER_INT) {
359197ba5f4SPaul Zimmerman 		if (!hsotg->frame_list) {
360197ba5f4SPaul Zimmerman 			retval = dwc2_frame_list_alloc(hsotg, mem_flags);
361197ba5f4SPaul Zimmerman 			if (retval)
362197ba5f4SPaul Zimmerman 				goto err1;
363197ba5f4SPaul Zimmerman 			/* Enable periodic schedule on first periodic QH */
364197ba5f4SPaul Zimmerman 			dwc2_per_sched_enable(hsotg, HCFG_FRLISTEN_64);
365197ba5f4SPaul Zimmerman 		}
366197ba5f4SPaul Zimmerman 	}
367197ba5f4SPaul Zimmerman 
368197ba5f4SPaul Zimmerman 	qh->ntd = 0;
369197ba5f4SPaul Zimmerman 	return 0;
370197ba5f4SPaul Zimmerman 
371197ba5f4SPaul Zimmerman err1:
372197ba5f4SPaul Zimmerman 	dwc2_desc_list_free(hsotg, qh);
373197ba5f4SPaul Zimmerman err0:
374197ba5f4SPaul Zimmerman 	return retval;
375197ba5f4SPaul Zimmerman }
376197ba5f4SPaul Zimmerman 
377197ba5f4SPaul Zimmerman /**
378197ba5f4SPaul Zimmerman  * dwc2_hcd_qh_free_ddma() - Frees a QH structure's Descriptor DMA related
379197ba5f4SPaul Zimmerman  * members
380197ba5f4SPaul Zimmerman  *
381197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure for the DWC OTG controller
382197ba5f4SPaul Zimmerman  * @qh:    The QH to free
383197ba5f4SPaul Zimmerman  *
384197ba5f4SPaul Zimmerman  * Frees descriptor list memory associated with the QH. If QH is periodic and
385197ba5f4SPaul Zimmerman  * the last, frees FrameList memory and disables periodic scheduling.
386197ba5f4SPaul Zimmerman  */
387197ba5f4SPaul Zimmerman void dwc2_hcd_qh_free_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
388197ba5f4SPaul Zimmerman {
3892b046bc5SGregory Herrero 	unsigned long flags;
3902b046bc5SGregory Herrero 
391197ba5f4SPaul Zimmerman 	dwc2_desc_list_free(hsotg, qh);
392197ba5f4SPaul Zimmerman 
393197ba5f4SPaul Zimmerman 	/*
394197ba5f4SPaul Zimmerman 	 * Channel still assigned due to some reasons.
395197ba5f4SPaul Zimmerman 	 * Seen on Isoc URB dequeue. Channel halted but no subsequent
396197ba5f4SPaul Zimmerman 	 * ChHalted interrupt to release the channel. Afterwards
397197ba5f4SPaul Zimmerman 	 * when it comes here from endpoint disable routine
398197ba5f4SPaul Zimmerman 	 * channel remains assigned.
399197ba5f4SPaul Zimmerman 	 */
4002b046bc5SGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
401197ba5f4SPaul Zimmerman 	if (qh->channel)
402197ba5f4SPaul Zimmerman 		dwc2_release_channel_ddma(hsotg, qh);
4032b046bc5SGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
404197ba5f4SPaul Zimmerman 
405197ba5f4SPaul Zimmerman 	if ((qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
406197ba5f4SPaul Zimmerman 	     qh->ep_type == USB_ENDPOINT_XFER_INT) &&
407197ba5f4SPaul Zimmerman 	    (hsotg->core_params->uframe_sched > 0 ||
408197ba5f4SPaul Zimmerman 	     !hsotg->periodic_channels) && hsotg->frame_list) {
409197ba5f4SPaul Zimmerman 		dwc2_per_sched_disable(hsotg);
410197ba5f4SPaul Zimmerman 		dwc2_frame_list_free(hsotg);
411197ba5f4SPaul Zimmerman 	}
412197ba5f4SPaul Zimmerman }
413197ba5f4SPaul Zimmerman 
414197ba5f4SPaul Zimmerman static u8 dwc2_frame_to_desc_idx(struct dwc2_qh *qh, u16 frame_idx)
415197ba5f4SPaul Zimmerman {
416197ba5f4SPaul Zimmerman 	if (qh->dev_speed == USB_SPEED_HIGH)
417197ba5f4SPaul Zimmerman 		/* Descriptor set (8 descriptors) index which is 8-aligned */
418197ba5f4SPaul Zimmerman 		return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
419197ba5f4SPaul Zimmerman 	else
420197ba5f4SPaul Zimmerman 		return frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1);
421197ba5f4SPaul Zimmerman }
422197ba5f4SPaul Zimmerman 
423197ba5f4SPaul Zimmerman /*
424197ba5f4SPaul Zimmerman  * Determine starting frame for Isochronous transfer.
425197ba5f4SPaul Zimmerman  * Few frames skipped to prevent race condition with HC.
426197ba5f4SPaul Zimmerman  */
427197ba5f4SPaul Zimmerman static u16 dwc2_calc_starting_frame(struct dwc2_hsotg *hsotg,
428197ba5f4SPaul Zimmerman 				    struct dwc2_qh *qh, u16 *skip_frames)
429197ba5f4SPaul Zimmerman {
430197ba5f4SPaul Zimmerman 	u16 frame;
431197ba5f4SPaul Zimmerman 
432197ba5f4SPaul Zimmerman 	hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
433197ba5f4SPaul Zimmerman 
434197ba5f4SPaul Zimmerman 	/* sched_frame is always frame number (not uFrame) both in FS and HS! */
435197ba5f4SPaul Zimmerman 
436197ba5f4SPaul Zimmerman 	/*
437197ba5f4SPaul Zimmerman 	 * skip_frames is used to limit activated descriptors number
438197ba5f4SPaul Zimmerman 	 * to avoid the situation when HC services the last activated
439197ba5f4SPaul Zimmerman 	 * descriptor firstly.
440197ba5f4SPaul Zimmerman 	 * Example for FS:
441197ba5f4SPaul Zimmerman 	 * Current frame is 1, scheduled frame is 3. Since HC always fetches
442197ba5f4SPaul Zimmerman 	 * the descriptor corresponding to curr_frame+1, the descriptor
443197ba5f4SPaul Zimmerman 	 * corresponding to frame 2 will be fetched. If the number of
444197ba5f4SPaul Zimmerman 	 * descriptors is max=64 (or greather) the list will be fully programmed
445197ba5f4SPaul Zimmerman 	 * with Active descriptors and it is possible case (rare) that the
446197ba5f4SPaul Zimmerman 	 * latest descriptor(considering rollback) corresponding to frame 2 will
447197ba5f4SPaul Zimmerman 	 * be serviced first. HS case is more probable because, in fact, up to
448197ba5f4SPaul Zimmerman 	 * 11 uframes (16 in the code) may be skipped.
449197ba5f4SPaul Zimmerman 	 */
450197ba5f4SPaul Zimmerman 	if (qh->dev_speed == USB_SPEED_HIGH) {
451197ba5f4SPaul Zimmerman 		/*
452197ba5f4SPaul Zimmerman 		 * Consider uframe counter also, to start xfer asap. If half of
453197ba5f4SPaul Zimmerman 		 * the frame elapsed skip 2 frames otherwise just 1 frame.
454197ba5f4SPaul Zimmerman 		 * Starting descriptor index must be 8-aligned, so if the
455197ba5f4SPaul Zimmerman 		 * current frame is near to complete the next one is skipped as
456197ba5f4SPaul Zimmerman 		 * well.
457197ba5f4SPaul Zimmerman 		 */
458197ba5f4SPaul Zimmerman 		if (dwc2_micro_frame_num(hsotg->frame_number) >= 5) {
459197ba5f4SPaul Zimmerman 			*skip_frames = 2 * 8;
460197ba5f4SPaul Zimmerman 			frame = dwc2_frame_num_inc(hsotg->frame_number,
461197ba5f4SPaul Zimmerman 						   *skip_frames);
462197ba5f4SPaul Zimmerman 		} else {
463197ba5f4SPaul Zimmerman 			*skip_frames = 1 * 8;
464197ba5f4SPaul Zimmerman 			frame = dwc2_frame_num_inc(hsotg->frame_number,
465197ba5f4SPaul Zimmerman 						   *skip_frames);
466197ba5f4SPaul Zimmerman 		}
467197ba5f4SPaul Zimmerman 
468197ba5f4SPaul Zimmerman 		frame = dwc2_full_frame_num(frame);
469197ba5f4SPaul Zimmerman 	} else {
470197ba5f4SPaul Zimmerman 		/*
471197ba5f4SPaul Zimmerman 		 * Two frames are skipped for FS - the current and the next.
472197ba5f4SPaul Zimmerman 		 * But for descriptor programming, 1 frame (descriptor) is
473197ba5f4SPaul Zimmerman 		 * enough, see example above.
474197ba5f4SPaul Zimmerman 		 */
475197ba5f4SPaul Zimmerman 		*skip_frames = 1;
476197ba5f4SPaul Zimmerman 		frame = dwc2_frame_num_inc(hsotg->frame_number, 2);
477197ba5f4SPaul Zimmerman 	}
478197ba5f4SPaul Zimmerman 
479197ba5f4SPaul Zimmerman 	return frame;
480197ba5f4SPaul Zimmerman }
481197ba5f4SPaul Zimmerman 
482197ba5f4SPaul Zimmerman /*
483197ba5f4SPaul Zimmerman  * Calculate initial descriptor index for isochronous transfer based on
484197ba5f4SPaul Zimmerman  * scheduled frame
485197ba5f4SPaul Zimmerman  */
486197ba5f4SPaul Zimmerman static u16 dwc2_recalc_initial_desc_idx(struct dwc2_hsotg *hsotg,
487197ba5f4SPaul Zimmerman 					struct dwc2_qh *qh)
488197ba5f4SPaul Zimmerman {
489197ba5f4SPaul Zimmerman 	u16 frame, fr_idx, fr_idx_tmp, skip_frames;
490197ba5f4SPaul Zimmerman 
491197ba5f4SPaul Zimmerman 	/*
492197ba5f4SPaul Zimmerman 	 * With current ISOC processing algorithm the channel is being released
493197ba5f4SPaul Zimmerman 	 * when no more QTDs in the list (qh->ntd == 0). Thus this function is
494197ba5f4SPaul Zimmerman 	 * called only when qh->ntd == 0 and qh->channel == 0.
495197ba5f4SPaul Zimmerman 	 *
496197ba5f4SPaul Zimmerman 	 * So qh->channel != NULL branch is not used and just not removed from
497197ba5f4SPaul Zimmerman 	 * the source file. It is required for another possible approach which
498197ba5f4SPaul Zimmerman 	 * is, do not disable and release the channel when ISOC session
499197ba5f4SPaul Zimmerman 	 * completed, just move QH to inactive schedule until new QTD arrives.
500197ba5f4SPaul Zimmerman 	 * On new QTD, the QH moved back to 'ready' schedule, starting frame and
501197ba5f4SPaul Zimmerman 	 * therefore starting desc_index are recalculated. In this case channel
502197ba5f4SPaul Zimmerman 	 * is released only on ep_disable.
503197ba5f4SPaul Zimmerman 	 */
504197ba5f4SPaul Zimmerman 
505197ba5f4SPaul Zimmerman 	/*
506197ba5f4SPaul Zimmerman 	 * Calculate starting descriptor index. For INTERRUPT endpoint it is
507197ba5f4SPaul Zimmerman 	 * always 0.
508197ba5f4SPaul Zimmerman 	 */
509197ba5f4SPaul Zimmerman 	if (qh->channel) {
510197ba5f4SPaul Zimmerman 		frame = dwc2_calc_starting_frame(hsotg, qh, &skip_frames);
511197ba5f4SPaul Zimmerman 		/*
512197ba5f4SPaul Zimmerman 		 * Calculate initial descriptor index based on FrameList current
513197ba5f4SPaul Zimmerman 		 * bitmap and servicing period
514197ba5f4SPaul Zimmerman 		 */
515197ba5f4SPaul Zimmerman 		fr_idx_tmp = dwc2_frame_list_idx(frame);
516197ba5f4SPaul Zimmerman 		fr_idx = (FRLISTEN_64_SIZE +
517197ba5f4SPaul Zimmerman 			  dwc2_frame_list_idx(qh->sched_frame) - fr_idx_tmp)
518197ba5f4SPaul Zimmerman 			 % dwc2_frame_incr_val(qh);
519197ba5f4SPaul Zimmerman 		fr_idx = (fr_idx + fr_idx_tmp) % FRLISTEN_64_SIZE;
520197ba5f4SPaul Zimmerman 	} else {
521197ba5f4SPaul Zimmerman 		qh->sched_frame = dwc2_calc_starting_frame(hsotg, qh,
522197ba5f4SPaul Zimmerman 							   &skip_frames);
523197ba5f4SPaul Zimmerman 		fr_idx = dwc2_frame_list_idx(qh->sched_frame);
524197ba5f4SPaul Zimmerman 	}
525197ba5f4SPaul Zimmerman 
526197ba5f4SPaul Zimmerman 	qh->td_first = qh->td_last = dwc2_frame_to_desc_idx(qh, fr_idx);
527197ba5f4SPaul Zimmerman 
528197ba5f4SPaul Zimmerman 	return skip_frames;
529197ba5f4SPaul Zimmerman }
530197ba5f4SPaul Zimmerman 
531197ba5f4SPaul Zimmerman #define ISOC_URB_GIVEBACK_ASAP
532197ba5f4SPaul Zimmerman 
533197ba5f4SPaul Zimmerman #define MAX_ISOC_XFER_SIZE_FS	1023
534197ba5f4SPaul Zimmerman #define MAX_ISOC_XFER_SIZE_HS	3072
535197ba5f4SPaul Zimmerman #define DESCNUM_THRESHOLD	4
536197ba5f4SPaul Zimmerman 
537197ba5f4SPaul Zimmerman static void dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
538197ba5f4SPaul Zimmerman 					 struct dwc2_qtd *qtd,
539197ba5f4SPaul Zimmerman 					 struct dwc2_qh *qh, u32 max_xfer_size,
540197ba5f4SPaul Zimmerman 					 u16 idx)
541197ba5f4SPaul Zimmerman {
542197ba5f4SPaul Zimmerman 	struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[idx];
543197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
544197ba5f4SPaul Zimmerman 
545197ba5f4SPaul Zimmerman 	memset(dma_desc, 0, sizeof(*dma_desc));
546197ba5f4SPaul Zimmerman 	frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
547197ba5f4SPaul Zimmerman 
548197ba5f4SPaul Zimmerman 	if (frame_desc->length > max_xfer_size)
549197ba5f4SPaul Zimmerman 		qh->n_bytes[idx] = max_xfer_size;
550197ba5f4SPaul Zimmerman 	else
551197ba5f4SPaul Zimmerman 		qh->n_bytes[idx] = frame_desc->length;
552197ba5f4SPaul Zimmerman 
553197ba5f4SPaul Zimmerman 	dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
554197ba5f4SPaul Zimmerman 	dma_desc->status = qh->n_bytes[idx] << HOST_DMA_ISOC_NBYTES_SHIFT &
555197ba5f4SPaul Zimmerman 			   HOST_DMA_ISOC_NBYTES_MASK;
556197ba5f4SPaul Zimmerman 
557dde4c1bfSGregory Herrero 	/* Set active bit */
558dde4c1bfSGregory Herrero 	dma_desc->status |= HOST_DMA_A;
559dde4c1bfSGregory Herrero 
5603ac38d26SGregory Herrero 	qh->ntd++;
5613ac38d26SGregory Herrero 	qtd->isoc_frame_index_last++;
5623ac38d26SGregory Herrero 
563197ba5f4SPaul Zimmerman #ifdef ISOC_URB_GIVEBACK_ASAP
564197ba5f4SPaul Zimmerman 	/* Set IOC for each descriptor corresponding to last frame of URB */
565197ba5f4SPaul Zimmerman 	if (qtd->isoc_frame_index_last == qtd->urb->packet_count)
566197ba5f4SPaul Zimmerman 		dma_desc->status |= HOST_DMA_IOC;
567197ba5f4SPaul Zimmerman #endif
568197ba5f4SPaul Zimmerman 
56995105a99SGregory Herrero 	dma_sync_single_for_device(hsotg->dev,
57095105a99SGregory Herrero 			qh->desc_list_dma +
57195105a99SGregory Herrero 			(idx * sizeof(struct dwc2_hcd_dma_desc)),
57295105a99SGregory Herrero 			sizeof(struct dwc2_hcd_dma_desc),
57395105a99SGregory Herrero 			DMA_TO_DEVICE);
574197ba5f4SPaul Zimmerman }
575197ba5f4SPaul Zimmerman 
576197ba5f4SPaul Zimmerman static void dwc2_init_isoc_dma_desc(struct dwc2_hsotg *hsotg,
577197ba5f4SPaul Zimmerman 				    struct dwc2_qh *qh, u16 skip_frames)
578197ba5f4SPaul Zimmerman {
579197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
580197ba5f4SPaul Zimmerman 	u32 max_xfer_size;
581c17b337cSGregory Herrero 	u16 idx, inc, n_desc = 0, ntd_max = 0;
582c17b337cSGregory Herrero 	u16 cur_idx;
583c17b337cSGregory Herrero 	u16 next_idx;
584197ba5f4SPaul Zimmerman 
585197ba5f4SPaul Zimmerman 	idx = qh->td_last;
586197ba5f4SPaul Zimmerman 	inc = qh->interval;
587c17b337cSGregory Herrero 	hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
588c17b337cSGregory Herrero 	cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
589c17b337cSGregory Herrero 	next_idx = dwc2_desclist_idx_inc(qh->td_last, inc, qh->dev_speed);
590c17b337cSGregory Herrero 
591c17b337cSGregory Herrero 	/*
592c17b337cSGregory Herrero 	 * Ensure current frame number didn't overstep last scheduled
593c17b337cSGregory Herrero 	 * descriptor. If it happens, the only way to recover is to move
594c17b337cSGregory Herrero 	 * qh->td_last to current frame number + 1.
595c17b337cSGregory Herrero 	 * So that next isoc descriptor will be scheduled on frame number + 1
596c17b337cSGregory Herrero 	 * and not on a past frame.
597c17b337cSGregory Herrero 	 */
598c17b337cSGregory Herrero 	if (dwc2_frame_idx_num_gt(cur_idx, next_idx) || (cur_idx == next_idx)) {
599c17b337cSGregory Herrero 		if (inc < 32) {
600c17b337cSGregory Herrero 			dev_vdbg(hsotg->dev,
601c17b337cSGregory Herrero 				 "current frame number overstep last descriptor\n");
602c17b337cSGregory Herrero 			qh->td_last = dwc2_desclist_idx_inc(cur_idx, inc,
603c17b337cSGregory Herrero 							    qh->dev_speed);
604c17b337cSGregory Herrero 			idx = qh->td_last;
605c17b337cSGregory Herrero 		}
606c17b337cSGregory Herrero 	}
607197ba5f4SPaul Zimmerman 
608197ba5f4SPaul Zimmerman 	if (qh->interval) {
609197ba5f4SPaul Zimmerman 		ntd_max = (dwc2_max_desc_num(qh) + qh->interval - 1) /
610197ba5f4SPaul Zimmerman 				qh->interval;
611197ba5f4SPaul Zimmerman 		if (skip_frames && !qh->channel)
612197ba5f4SPaul Zimmerman 			ntd_max -= skip_frames / qh->interval;
613197ba5f4SPaul Zimmerman 	}
614197ba5f4SPaul Zimmerman 
615197ba5f4SPaul Zimmerman 	max_xfer_size = qh->dev_speed == USB_SPEED_HIGH ?
616197ba5f4SPaul Zimmerman 			MAX_ISOC_XFER_SIZE_HS : MAX_ISOC_XFER_SIZE_FS;
617197ba5f4SPaul Zimmerman 
618197ba5f4SPaul Zimmerman 	list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
619c17b337cSGregory Herrero 		if (qtd->in_process &&
620c17b337cSGregory Herrero 		    qtd->isoc_frame_index_last ==
621c17b337cSGregory Herrero 		    qtd->urb->packet_count)
622c17b337cSGregory Herrero 			continue;
623c17b337cSGregory Herrero 
624c17b337cSGregory Herrero 		qtd->isoc_td_first = idx;
625197ba5f4SPaul Zimmerman 		while (qh->ntd < ntd_max && qtd->isoc_frame_index_last <
626197ba5f4SPaul Zimmerman 						qtd->urb->packet_count) {
627197ba5f4SPaul Zimmerman 			dwc2_fill_host_isoc_dma_desc(hsotg, qtd, qh,
628197ba5f4SPaul Zimmerman 						     max_xfer_size, idx);
629197ba5f4SPaul Zimmerman 			idx = dwc2_desclist_idx_inc(idx, inc, qh->dev_speed);
630197ba5f4SPaul Zimmerman 			n_desc++;
631197ba5f4SPaul Zimmerman 		}
632c17b337cSGregory Herrero 		qtd->isoc_td_last = idx;
633197ba5f4SPaul Zimmerman 		qtd->in_process = 1;
634197ba5f4SPaul Zimmerman 	}
635197ba5f4SPaul Zimmerman 
636197ba5f4SPaul Zimmerman 	qh->td_last = idx;
637197ba5f4SPaul Zimmerman 
638197ba5f4SPaul Zimmerman #ifdef ISOC_URB_GIVEBACK_ASAP
639197ba5f4SPaul Zimmerman 	/* Set IOC for last descriptor if descriptor list is full */
640197ba5f4SPaul Zimmerman 	if (qh->ntd == ntd_max) {
641197ba5f4SPaul Zimmerman 		idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
642197ba5f4SPaul Zimmerman 		qh->desc_list[idx].status |= HOST_DMA_IOC;
64395105a99SGregory Herrero 		dma_sync_single_for_device(hsotg->dev,
64495105a99SGregory Herrero 					   qh->desc_list_dma + (idx *
64595105a99SGregory Herrero 					   sizeof(struct dwc2_hcd_dma_desc)),
64695105a99SGregory Herrero 					   sizeof(struct dwc2_hcd_dma_desc),
64795105a99SGregory Herrero 					   DMA_TO_DEVICE);
648197ba5f4SPaul Zimmerman 	}
649197ba5f4SPaul Zimmerman #else
650197ba5f4SPaul Zimmerman 	/*
651197ba5f4SPaul Zimmerman 	 * Set IOC bit only for one descriptor. Always try to be ahead of HW
652197ba5f4SPaul Zimmerman 	 * processing, i.e. on IOC generation driver activates next descriptor
653197ba5f4SPaul Zimmerman 	 * but core continues to process descriptors following the one with IOC
654197ba5f4SPaul Zimmerman 	 * set.
655197ba5f4SPaul Zimmerman 	 */
656197ba5f4SPaul Zimmerman 
657197ba5f4SPaul Zimmerman 	if (n_desc > DESCNUM_THRESHOLD)
658197ba5f4SPaul Zimmerman 		/*
659197ba5f4SPaul Zimmerman 		 * Move IOC "up". Required even if there is only one QTD
660197ba5f4SPaul Zimmerman 		 * in the list, because QTDs might continue to be queued,
661197ba5f4SPaul Zimmerman 		 * but during the activation it was only one queued.
662197ba5f4SPaul Zimmerman 		 * Actually more than one QTD might be in the list if this
663197ba5f4SPaul Zimmerman 		 * function called from XferCompletion - QTDs was queued during
664197ba5f4SPaul Zimmerman 		 * HW processing of the previous descriptor chunk.
665197ba5f4SPaul Zimmerman 		 */
666197ba5f4SPaul Zimmerman 		idx = dwc2_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2),
667197ba5f4SPaul Zimmerman 					    qh->dev_speed);
668197ba5f4SPaul Zimmerman 	else
669197ba5f4SPaul Zimmerman 		/*
670197ba5f4SPaul Zimmerman 		 * Set the IOC for the latest descriptor if either number of
671197ba5f4SPaul Zimmerman 		 * descriptors is not greater than threshold or no more new
672197ba5f4SPaul Zimmerman 		 * descriptors activated
673197ba5f4SPaul Zimmerman 		 */
674197ba5f4SPaul Zimmerman 		idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
675197ba5f4SPaul Zimmerman 
676197ba5f4SPaul Zimmerman 	qh->desc_list[idx].status |= HOST_DMA_IOC;
67795105a99SGregory Herrero 	dma_sync_single_for_device(hsotg->dev,
67895105a99SGregory Herrero 				   qh->desc_list_dma +
67995105a99SGregory Herrero 				   (idx * sizeof(struct dwc2_hcd_dma_desc)),
68095105a99SGregory Herrero 				   sizeof(struct dwc2_hcd_dma_desc),
68195105a99SGregory Herrero 				   DMA_TO_DEVICE);
682197ba5f4SPaul Zimmerman #endif
683197ba5f4SPaul Zimmerman }
684197ba5f4SPaul Zimmerman 
685197ba5f4SPaul Zimmerman static void dwc2_fill_host_dma_desc(struct dwc2_hsotg *hsotg,
686197ba5f4SPaul Zimmerman 				    struct dwc2_host_chan *chan,
687197ba5f4SPaul Zimmerman 				    struct dwc2_qtd *qtd, struct dwc2_qh *qh,
688197ba5f4SPaul Zimmerman 				    int n_desc)
689197ba5f4SPaul Zimmerman {
690197ba5f4SPaul Zimmerman 	struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[n_desc];
691197ba5f4SPaul Zimmerman 	int len = chan->xfer_len;
692197ba5f4SPaul Zimmerman 
693197ba5f4SPaul Zimmerman 	if (len > MAX_DMA_DESC_SIZE - (chan->max_packet - 1))
694197ba5f4SPaul Zimmerman 		len = MAX_DMA_DESC_SIZE - (chan->max_packet - 1);
695197ba5f4SPaul Zimmerman 
696197ba5f4SPaul Zimmerman 	if (chan->ep_is_in) {
697197ba5f4SPaul Zimmerman 		int num_packets;
698197ba5f4SPaul Zimmerman 
699197ba5f4SPaul Zimmerman 		if (len > 0 && chan->max_packet)
700197ba5f4SPaul Zimmerman 			num_packets = (len + chan->max_packet - 1)
701197ba5f4SPaul Zimmerman 					/ chan->max_packet;
702197ba5f4SPaul Zimmerman 		else
703197ba5f4SPaul Zimmerman 			/* Need 1 packet for transfer length of 0 */
704197ba5f4SPaul Zimmerman 			num_packets = 1;
705197ba5f4SPaul Zimmerman 
706197ba5f4SPaul Zimmerman 		/* Always program an integral # of packets for IN transfers */
707197ba5f4SPaul Zimmerman 		len = num_packets * chan->max_packet;
708197ba5f4SPaul Zimmerman 	}
709197ba5f4SPaul Zimmerman 
710197ba5f4SPaul Zimmerman 	dma_desc->status = len << HOST_DMA_NBYTES_SHIFT & HOST_DMA_NBYTES_MASK;
711197ba5f4SPaul Zimmerman 	qh->n_bytes[n_desc] = len;
712197ba5f4SPaul Zimmerman 
713197ba5f4SPaul Zimmerman 	if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL &&
714197ba5f4SPaul Zimmerman 	    qtd->control_phase == DWC2_CONTROL_SETUP)
715197ba5f4SPaul Zimmerman 		dma_desc->status |= HOST_DMA_SUP;
716197ba5f4SPaul Zimmerman 
717197ba5f4SPaul Zimmerman 	dma_desc->buf = (u32)chan->xfer_dma;
718197ba5f4SPaul Zimmerman 
71995105a99SGregory Herrero 	dma_sync_single_for_device(hsotg->dev,
72095105a99SGregory Herrero 				   qh->desc_list_dma +
72195105a99SGregory Herrero 				   (n_desc * sizeof(struct dwc2_hcd_dma_desc)),
72295105a99SGregory Herrero 				   sizeof(struct dwc2_hcd_dma_desc),
72395105a99SGregory Herrero 				   DMA_TO_DEVICE);
72495105a99SGregory Herrero 
725197ba5f4SPaul Zimmerman 	/*
726197ba5f4SPaul Zimmerman 	 * Last (or only) descriptor of IN transfer with actual size less
727197ba5f4SPaul Zimmerman 	 * than MaxPacket
728197ba5f4SPaul Zimmerman 	 */
729197ba5f4SPaul Zimmerman 	if (len > chan->xfer_len) {
730197ba5f4SPaul Zimmerman 		chan->xfer_len = 0;
731197ba5f4SPaul Zimmerman 	} else {
732197ba5f4SPaul Zimmerman 		chan->xfer_dma += len;
733197ba5f4SPaul Zimmerman 		chan->xfer_len -= len;
734197ba5f4SPaul Zimmerman 	}
735197ba5f4SPaul Zimmerman }
736197ba5f4SPaul Zimmerman 
737197ba5f4SPaul Zimmerman static void dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg *hsotg,
738197ba5f4SPaul Zimmerman 					struct dwc2_qh *qh)
739197ba5f4SPaul Zimmerman {
740197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
741197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan = qh->channel;
742197ba5f4SPaul Zimmerman 	int n_desc = 0;
743197ba5f4SPaul Zimmerman 
744197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s(): qh=%p dma=%08lx len=%d\n", __func__, qh,
745197ba5f4SPaul Zimmerman 		 (unsigned long)chan->xfer_dma, chan->xfer_len);
746197ba5f4SPaul Zimmerman 
747197ba5f4SPaul Zimmerman 	/*
748197ba5f4SPaul Zimmerman 	 * Start with chan->xfer_dma initialized in assign_and_init_hc(), then
749197ba5f4SPaul Zimmerman 	 * if SG transfer consists of multiple URBs, this pointer is re-assigned
750197ba5f4SPaul Zimmerman 	 * to the buffer of the currently processed QTD. For non-SG request
751197ba5f4SPaul Zimmerman 	 * there is always one QTD active.
752197ba5f4SPaul Zimmerman 	 */
753197ba5f4SPaul Zimmerman 
754197ba5f4SPaul Zimmerman 	list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
755197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "qtd=%p\n", qtd);
756197ba5f4SPaul Zimmerman 
757197ba5f4SPaul Zimmerman 		if (n_desc) {
758197ba5f4SPaul Zimmerman 			/* SG request - more than 1 QTD */
759197ba5f4SPaul Zimmerman 			chan->xfer_dma = qtd->urb->dma +
760197ba5f4SPaul Zimmerman 					qtd->urb->actual_length;
761197ba5f4SPaul Zimmerman 			chan->xfer_len = qtd->urb->length -
762197ba5f4SPaul Zimmerman 					qtd->urb->actual_length;
763197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "buf=%08lx len=%d\n",
764197ba5f4SPaul Zimmerman 				 (unsigned long)chan->xfer_dma, chan->xfer_len);
765197ba5f4SPaul Zimmerman 		}
766197ba5f4SPaul Zimmerman 
767197ba5f4SPaul Zimmerman 		qtd->n_desc = 0;
768197ba5f4SPaul Zimmerman 		do {
769197ba5f4SPaul Zimmerman 			if (n_desc > 1) {
770197ba5f4SPaul Zimmerman 				qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
771197ba5f4SPaul Zimmerman 				dev_vdbg(hsotg->dev,
772197ba5f4SPaul Zimmerman 					 "set A bit in desc %d (%p)\n",
773197ba5f4SPaul Zimmerman 					 n_desc - 1,
774197ba5f4SPaul Zimmerman 					 &qh->desc_list[n_desc - 1]);
77595105a99SGregory Herrero 				dma_sync_single_for_device(hsotg->dev,
77695105a99SGregory Herrero 					qh->desc_list_dma +
77795105a99SGregory Herrero 					((n_desc - 1) *
77895105a99SGregory Herrero 					sizeof(struct dwc2_hcd_dma_desc)),
77995105a99SGregory Herrero 					sizeof(struct dwc2_hcd_dma_desc),
78095105a99SGregory Herrero 					DMA_TO_DEVICE);
781197ba5f4SPaul Zimmerman 			}
782197ba5f4SPaul Zimmerman 			dwc2_fill_host_dma_desc(hsotg, chan, qtd, qh, n_desc);
783197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev,
784197ba5f4SPaul Zimmerman 				 "desc %d (%p) buf=%08x status=%08x\n",
785197ba5f4SPaul Zimmerman 				 n_desc, &qh->desc_list[n_desc],
786197ba5f4SPaul Zimmerman 				 qh->desc_list[n_desc].buf,
787197ba5f4SPaul Zimmerman 				 qh->desc_list[n_desc].status);
788197ba5f4SPaul Zimmerman 			qtd->n_desc++;
789197ba5f4SPaul Zimmerman 			n_desc++;
790197ba5f4SPaul Zimmerman 		} while (chan->xfer_len > 0 &&
791197ba5f4SPaul Zimmerman 			 n_desc != MAX_DMA_DESC_NUM_GENERIC);
792197ba5f4SPaul Zimmerman 
793197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "n_desc=%d\n", n_desc);
794197ba5f4SPaul Zimmerman 		qtd->in_process = 1;
795197ba5f4SPaul Zimmerman 		if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL)
796197ba5f4SPaul Zimmerman 			break;
797197ba5f4SPaul Zimmerman 		if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
798197ba5f4SPaul Zimmerman 			break;
799197ba5f4SPaul Zimmerman 	}
800197ba5f4SPaul Zimmerman 
801197ba5f4SPaul Zimmerman 	if (n_desc) {
802197ba5f4SPaul Zimmerman 		qh->desc_list[n_desc - 1].status |=
803197ba5f4SPaul Zimmerman 				HOST_DMA_IOC | HOST_DMA_EOL | HOST_DMA_A;
804197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "set IOC/EOL/A bits in desc %d (%p)\n",
805197ba5f4SPaul Zimmerman 			 n_desc - 1, &qh->desc_list[n_desc - 1]);
80695105a99SGregory Herrero 		dma_sync_single_for_device(hsotg->dev,
80795105a99SGregory Herrero 					   qh->desc_list_dma + (n_desc - 1) *
80895105a99SGregory Herrero 					   sizeof(struct dwc2_hcd_dma_desc),
80995105a99SGregory Herrero 					   sizeof(struct dwc2_hcd_dma_desc),
81095105a99SGregory Herrero 					   DMA_TO_DEVICE);
811197ba5f4SPaul Zimmerman 		if (n_desc > 1) {
812197ba5f4SPaul Zimmerman 			qh->desc_list[0].status |= HOST_DMA_A;
813197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "set A bit in desc 0 (%p)\n",
814197ba5f4SPaul Zimmerman 				 &qh->desc_list[0]);
81595105a99SGregory Herrero 			dma_sync_single_for_device(hsotg->dev,
81695105a99SGregory Herrero 					qh->desc_list_dma,
81795105a99SGregory Herrero 					sizeof(struct dwc2_hcd_dma_desc),
81895105a99SGregory Herrero 					DMA_TO_DEVICE);
819197ba5f4SPaul Zimmerman 		}
820197ba5f4SPaul Zimmerman 		chan->ntd = n_desc;
821197ba5f4SPaul Zimmerman 	}
822197ba5f4SPaul Zimmerman }
823197ba5f4SPaul Zimmerman 
824197ba5f4SPaul Zimmerman /**
825197ba5f4SPaul Zimmerman  * dwc2_hcd_start_xfer_ddma() - Starts a transfer in Descriptor DMA mode
826197ba5f4SPaul Zimmerman  *
827197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure for the DWC OTG controller
828197ba5f4SPaul Zimmerman  * @qh:    The QH to init
829197ba5f4SPaul Zimmerman  *
830197ba5f4SPaul Zimmerman  * Return: 0 if successful, negative error code otherwise
831197ba5f4SPaul Zimmerman  *
832197ba5f4SPaul Zimmerman  * For Control and Bulk endpoints, initializes descriptor list and starts the
833197ba5f4SPaul Zimmerman  * transfer. For Interrupt and Isochronous endpoints, initializes descriptor
834197ba5f4SPaul Zimmerman  * list then updates FrameList, marking appropriate entries as active.
835197ba5f4SPaul Zimmerman  *
836197ba5f4SPaul Zimmerman  * For Isochronous endpoints the starting descriptor index is calculated based
837197ba5f4SPaul Zimmerman  * on the scheduled frame, but only on the first transfer descriptor within a
838197ba5f4SPaul Zimmerman  * session. Then the transfer is started via enabling the channel.
839197ba5f4SPaul Zimmerman  *
840197ba5f4SPaul Zimmerman  * For Isochronous endpoints the channel is not halted on XferComplete
841197ba5f4SPaul Zimmerman  * interrupt so remains assigned to the endpoint(QH) until session is done.
842197ba5f4SPaul Zimmerman  */
843197ba5f4SPaul Zimmerman void dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
844197ba5f4SPaul Zimmerman {
845197ba5f4SPaul Zimmerman 	/* Channel is already assigned */
846197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan = qh->channel;
847197ba5f4SPaul Zimmerman 	u16 skip_frames = 0;
848197ba5f4SPaul Zimmerman 
849197ba5f4SPaul Zimmerman 	switch (chan->ep_type) {
850197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_CONTROL:
851197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_BULK:
852197ba5f4SPaul Zimmerman 		dwc2_init_non_isoc_dma_desc(hsotg, qh);
853197ba5f4SPaul Zimmerman 		dwc2_hc_start_transfer_ddma(hsotg, chan);
854197ba5f4SPaul Zimmerman 		break;
855197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_INT:
856197ba5f4SPaul Zimmerman 		dwc2_init_non_isoc_dma_desc(hsotg, qh);
857197ba5f4SPaul Zimmerman 		dwc2_update_frame_list(hsotg, qh, 1);
858197ba5f4SPaul Zimmerman 		dwc2_hc_start_transfer_ddma(hsotg, chan);
859197ba5f4SPaul Zimmerman 		break;
860197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_ISOC:
861197ba5f4SPaul Zimmerman 		if (!qh->ntd)
862197ba5f4SPaul Zimmerman 			skip_frames = dwc2_recalc_initial_desc_idx(hsotg, qh);
863197ba5f4SPaul Zimmerman 		dwc2_init_isoc_dma_desc(hsotg, qh, skip_frames);
864197ba5f4SPaul Zimmerman 
865197ba5f4SPaul Zimmerman 		if (!chan->xfer_started) {
866197ba5f4SPaul Zimmerman 			dwc2_update_frame_list(hsotg, qh, 1);
867197ba5f4SPaul Zimmerman 
868197ba5f4SPaul Zimmerman 			/*
869197ba5f4SPaul Zimmerman 			 * Always set to max, instead of actual size. Otherwise
870197ba5f4SPaul Zimmerman 			 * ntd will be changed with channel being enabled. Not
871197ba5f4SPaul Zimmerman 			 * recommended.
872197ba5f4SPaul Zimmerman 			 */
873197ba5f4SPaul Zimmerman 			chan->ntd = dwc2_max_desc_num(qh);
874197ba5f4SPaul Zimmerman 
875197ba5f4SPaul Zimmerman 			/* Enable channel only once for ISOC */
876197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer_ddma(hsotg, chan);
877197ba5f4SPaul Zimmerman 		}
878197ba5f4SPaul Zimmerman 
879197ba5f4SPaul Zimmerman 		break;
880197ba5f4SPaul Zimmerman 	default:
881197ba5f4SPaul Zimmerman 		break;
882197ba5f4SPaul Zimmerman 	}
883197ba5f4SPaul Zimmerman }
884197ba5f4SPaul Zimmerman 
885197ba5f4SPaul Zimmerman #define DWC2_CMPL_DONE		1
886197ba5f4SPaul Zimmerman #define DWC2_CMPL_STOP		2
887197ba5f4SPaul Zimmerman 
888197ba5f4SPaul Zimmerman static int dwc2_cmpl_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
889197ba5f4SPaul Zimmerman 					struct dwc2_host_chan *chan,
890197ba5f4SPaul Zimmerman 					struct dwc2_qtd *qtd,
891197ba5f4SPaul Zimmerman 					struct dwc2_qh *qh, u16 idx)
892197ba5f4SPaul Zimmerman {
89395105a99SGregory Herrero 	struct dwc2_hcd_dma_desc *dma_desc;
894197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
895197ba5f4SPaul Zimmerman 	u16 remain = 0;
896197ba5f4SPaul Zimmerman 	int rc = 0;
897197ba5f4SPaul Zimmerman 
898197ba5f4SPaul Zimmerman 	if (!qtd->urb)
899197ba5f4SPaul Zimmerman 		return -EINVAL;
900197ba5f4SPaul Zimmerman 
90195105a99SGregory Herrero 	dma_sync_single_for_cpu(hsotg->dev, qh->desc_list_dma + (idx *
90295105a99SGregory Herrero 				sizeof(struct dwc2_hcd_dma_desc)),
90395105a99SGregory Herrero 				sizeof(struct dwc2_hcd_dma_desc),
90495105a99SGregory Herrero 				DMA_FROM_DEVICE);
90595105a99SGregory Herrero 
90695105a99SGregory Herrero 	dma_desc = &qh->desc_list[idx];
90795105a99SGregory Herrero 
908197ba5f4SPaul Zimmerman 	frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
909197ba5f4SPaul Zimmerman 	dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
910197ba5f4SPaul Zimmerman 	if (chan->ep_is_in)
911197ba5f4SPaul Zimmerman 		remain = (dma_desc->status & HOST_DMA_ISOC_NBYTES_MASK) >>
912197ba5f4SPaul Zimmerman 			 HOST_DMA_ISOC_NBYTES_SHIFT;
913197ba5f4SPaul Zimmerman 
914197ba5f4SPaul Zimmerman 	if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
915197ba5f4SPaul Zimmerman 		/*
916197ba5f4SPaul Zimmerman 		 * XactError, or unable to complete all the transactions
917197ba5f4SPaul Zimmerman 		 * in the scheduled micro-frame/frame, both indicated by
918197ba5f4SPaul Zimmerman 		 * HOST_DMA_STS_PKTERR
919197ba5f4SPaul Zimmerman 		 */
920197ba5f4SPaul Zimmerman 		qtd->urb->error_count++;
921197ba5f4SPaul Zimmerman 		frame_desc->actual_length = qh->n_bytes[idx] - remain;
922197ba5f4SPaul Zimmerman 		frame_desc->status = -EPROTO;
923197ba5f4SPaul Zimmerman 	} else {
924197ba5f4SPaul Zimmerman 		/* Success */
925197ba5f4SPaul Zimmerman 		frame_desc->actual_length = qh->n_bytes[idx] - remain;
926197ba5f4SPaul Zimmerman 		frame_desc->status = 0;
927197ba5f4SPaul Zimmerman 	}
928197ba5f4SPaul Zimmerman 
929197ba5f4SPaul Zimmerman 	if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
930197ba5f4SPaul Zimmerman 		/*
931197ba5f4SPaul Zimmerman 		 * urb->status is not used for isoc transfers here. The
932197ba5f4SPaul Zimmerman 		 * individual frame_desc status are used instead.
933197ba5f4SPaul Zimmerman 		 */
934197ba5f4SPaul Zimmerman 		dwc2_host_complete(hsotg, qtd, 0);
935197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
936197ba5f4SPaul Zimmerman 
937197ba5f4SPaul Zimmerman 		/*
938197ba5f4SPaul Zimmerman 		 * This check is necessary because urb_dequeue can be called
939197ba5f4SPaul Zimmerman 		 * from urb complete callback (sound driver for example). All
940197ba5f4SPaul Zimmerman 		 * pending URBs are dequeued there, so no need for further
941197ba5f4SPaul Zimmerman 		 * processing.
942197ba5f4SPaul Zimmerman 		 */
943197ba5f4SPaul Zimmerman 		if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE)
944197ba5f4SPaul Zimmerman 			return -1;
945197ba5f4SPaul Zimmerman 		rc = DWC2_CMPL_DONE;
946197ba5f4SPaul Zimmerman 	}
947197ba5f4SPaul Zimmerman 
948197ba5f4SPaul Zimmerman 	qh->ntd--;
949197ba5f4SPaul Zimmerman 
950197ba5f4SPaul Zimmerman 	/* Stop if IOC requested descriptor reached */
951197ba5f4SPaul Zimmerman 	if (dma_desc->status & HOST_DMA_IOC)
952197ba5f4SPaul Zimmerman 		rc = DWC2_CMPL_STOP;
953197ba5f4SPaul Zimmerman 
954197ba5f4SPaul Zimmerman 	return rc;
955197ba5f4SPaul Zimmerman }
956197ba5f4SPaul Zimmerman 
957197ba5f4SPaul Zimmerman static void dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
958197ba5f4SPaul Zimmerman 					 struct dwc2_host_chan *chan,
959197ba5f4SPaul Zimmerman 					 enum dwc2_halt_status halt_status)
960197ba5f4SPaul Zimmerman {
961197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
962197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
963197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
964197ba5f4SPaul Zimmerman 	u16 idx;
965197ba5f4SPaul Zimmerman 	int rc;
966197ba5f4SPaul Zimmerman 
967197ba5f4SPaul Zimmerman 	qh = chan->qh;
968197ba5f4SPaul Zimmerman 	idx = qh->td_first;
969197ba5f4SPaul Zimmerman 
970197ba5f4SPaul Zimmerman 	if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
971197ba5f4SPaul Zimmerman 		list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
972197ba5f4SPaul Zimmerman 			qtd->in_process = 0;
973197ba5f4SPaul Zimmerman 		return;
974197ba5f4SPaul Zimmerman 	}
975197ba5f4SPaul Zimmerman 
976197ba5f4SPaul Zimmerman 	if (halt_status == DWC2_HC_XFER_AHB_ERR ||
977197ba5f4SPaul Zimmerman 	    halt_status == DWC2_HC_XFER_BABBLE_ERR) {
978197ba5f4SPaul Zimmerman 		/*
979197ba5f4SPaul Zimmerman 		 * Channel is halted in these error cases, considered as serious
980197ba5f4SPaul Zimmerman 		 * issues.
981197ba5f4SPaul Zimmerman 		 * Complete all URBs marking all frames as failed, irrespective
982197ba5f4SPaul Zimmerman 		 * whether some of the descriptors (frames) succeeded or not.
983197ba5f4SPaul Zimmerman 		 * Pass error code to completion routine as well, to update
984197ba5f4SPaul Zimmerman 		 * urb->status, some of class drivers might use it to stop
985197ba5f4SPaul Zimmerman 		 * queing transfer requests.
986197ba5f4SPaul Zimmerman 		 */
987197ba5f4SPaul Zimmerman 		int err = halt_status == DWC2_HC_XFER_AHB_ERR ?
988197ba5f4SPaul Zimmerman 			  -EIO : -EOVERFLOW;
989197ba5f4SPaul Zimmerman 
990197ba5f4SPaul Zimmerman 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
991197ba5f4SPaul Zimmerman 					 qtd_list_entry) {
992197ba5f4SPaul Zimmerman 			if (qtd->urb) {
993197ba5f4SPaul Zimmerman 				for (idx = 0; idx < qtd->urb->packet_count;
994197ba5f4SPaul Zimmerman 				     idx++) {
995197ba5f4SPaul Zimmerman 					frame_desc = &qtd->urb->iso_descs[idx];
996197ba5f4SPaul Zimmerman 					frame_desc->status = err;
997197ba5f4SPaul Zimmerman 				}
998197ba5f4SPaul Zimmerman 
999197ba5f4SPaul Zimmerman 				dwc2_host_complete(hsotg, qtd, err);
1000197ba5f4SPaul Zimmerman 			}
1001197ba5f4SPaul Zimmerman 
1002197ba5f4SPaul Zimmerman 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1003197ba5f4SPaul Zimmerman 		}
1004197ba5f4SPaul Zimmerman 
1005197ba5f4SPaul Zimmerman 		return;
1006197ba5f4SPaul Zimmerman 	}
1007197ba5f4SPaul Zimmerman 
1008197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
1009197ba5f4SPaul Zimmerman 		if (!qtd->in_process)
1010197ba5f4SPaul Zimmerman 			break;
1011762d3a1aSGregory Herrero 
1012762d3a1aSGregory Herrero 		/*
1013762d3a1aSGregory Herrero 		 * Ensure idx corresponds to descriptor where first urb of this
1014762d3a1aSGregory Herrero 		 * qtd was added. In fact, during isoc desc init, dwc2 may skip
1015762d3a1aSGregory Herrero 		 * an index if current frame number is already over this index.
1016762d3a1aSGregory Herrero 		 */
1017762d3a1aSGregory Herrero 		if (idx != qtd->isoc_td_first) {
1018762d3a1aSGregory Herrero 			dev_vdbg(hsotg->dev,
1019762d3a1aSGregory Herrero 				 "try to complete %d instead of %d\n",
1020762d3a1aSGregory Herrero 				 idx, qtd->isoc_td_first);
1021762d3a1aSGregory Herrero 			idx = qtd->isoc_td_first;
1022762d3a1aSGregory Herrero 		}
1023762d3a1aSGregory Herrero 
1024197ba5f4SPaul Zimmerman 		do {
1025762d3a1aSGregory Herrero 			struct dwc2_qtd *qtd_next;
1026762d3a1aSGregory Herrero 			u16 cur_idx;
1027762d3a1aSGregory Herrero 
1028197ba5f4SPaul Zimmerman 			rc = dwc2_cmpl_host_isoc_dma_desc(hsotg, chan, qtd, qh,
1029197ba5f4SPaul Zimmerman 							  idx);
1030197ba5f4SPaul Zimmerman 			if (rc < 0)
1031197ba5f4SPaul Zimmerman 				return;
1032197ba5f4SPaul Zimmerman 			idx = dwc2_desclist_idx_inc(idx, qh->interval,
1033197ba5f4SPaul Zimmerman 						    chan->speed);
1034762d3a1aSGregory Herrero 			if (!rc)
1035762d3a1aSGregory Herrero 				continue;
1036762d3a1aSGregory Herrero 
1037197ba5f4SPaul Zimmerman 			if (rc == DWC2_CMPL_DONE)
1038197ba5f4SPaul Zimmerman 				break;
1039762d3a1aSGregory Herrero 
1040762d3a1aSGregory Herrero 			/* rc == DWC2_CMPL_STOP */
1041762d3a1aSGregory Herrero 
1042762d3a1aSGregory Herrero 			if (qh->interval >= 32)
1043762d3a1aSGregory Herrero 				goto stop_scan;
1044762d3a1aSGregory Herrero 
1045762d3a1aSGregory Herrero 			qh->td_first = idx;
1046762d3a1aSGregory Herrero 			cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
1047762d3a1aSGregory Herrero 			qtd_next = list_first_entry(&qh->qtd_list,
1048762d3a1aSGregory Herrero 						    struct dwc2_qtd,
1049762d3a1aSGregory Herrero 						    qtd_list_entry);
1050762d3a1aSGregory Herrero 			if (dwc2_frame_idx_num_gt(cur_idx,
1051762d3a1aSGregory Herrero 						  qtd_next->isoc_td_last))
1052762d3a1aSGregory Herrero 				break;
1053762d3a1aSGregory Herrero 
1054762d3a1aSGregory Herrero 			goto stop_scan;
1055762d3a1aSGregory Herrero 
1056197ba5f4SPaul Zimmerman 		} while (idx != qh->td_first);
1057197ba5f4SPaul Zimmerman 	}
1058197ba5f4SPaul Zimmerman 
1059197ba5f4SPaul Zimmerman stop_scan:
1060197ba5f4SPaul Zimmerman 	qh->td_first = idx;
1061197ba5f4SPaul Zimmerman }
1062197ba5f4SPaul Zimmerman 
1063197ba5f4SPaul Zimmerman static int dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg *hsotg,
1064197ba5f4SPaul Zimmerman 					struct dwc2_host_chan *chan,
1065197ba5f4SPaul Zimmerman 					struct dwc2_qtd *qtd,
1066197ba5f4SPaul Zimmerman 					struct dwc2_hcd_dma_desc *dma_desc,
1067197ba5f4SPaul Zimmerman 					enum dwc2_halt_status halt_status,
1068197ba5f4SPaul Zimmerman 					u32 n_bytes, int *xfer_done)
1069197ba5f4SPaul Zimmerman {
1070197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb = qtd->urb;
1071197ba5f4SPaul Zimmerman 	u16 remain = 0;
1072197ba5f4SPaul Zimmerman 
1073197ba5f4SPaul Zimmerman 	if (chan->ep_is_in)
1074197ba5f4SPaul Zimmerman 		remain = (dma_desc->status & HOST_DMA_NBYTES_MASK) >>
1075197ba5f4SPaul Zimmerman 			 HOST_DMA_NBYTES_SHIFT;
1076197ba5f4SPaul Zimmerman 
1077197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "remain=%d dwc2_urb=%p\n", remain, urb);
1078197ba5f4SPaul Zimmerman 
1079197ba5f4SPaul Zimmerman 	if (halt_status == DWC2_HC_XFER_AHB_ERR) {
1080197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "EIO\n");
1081197ba5f4SPaul Zimmerman 		urb->status = -EIO;
1082197ba5f4SPaul Zimmerman 		return 1;
1083197ba5f4SPaul Zimmerman 	}
1084197ba5f4SPaul Zimmerman 
1085197ba5f4SPaul Zimmerman 	if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
1086197ba5f4SPaul Zimmerman 		switch (halt_status) {
1087197ba5f4SPaul Zimmerman 		case DWC2_HC_XFER_STALL:
1088197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "Stall\n");
1089197ba5f4SPaul Zimmerman 			urb->status = -EPIPE;
1090197ba5f4SPaul Zimmerman 			break;
1091197ba5f4SPaul Zimmerman 		case DWC2_HC_XFER_BABBLE_ERR:
1092197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "Babble\n");
1093197ba5f4SPaul Zimmerman 			urb->status = -EOVERFLOW;
1094197ba5f4SPaul Zimmerman 			break;
1095197ba5f4SPaul Zimmerman 		case DWC2_HC_XFER_XACT_ERR:
1096197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "XactErr\n");
1097197ba5f4SPaul Zimmerman 			urb->status = -EPROTO;
1098197ba5f4SPaul Zimmerman 			break;
1099197ba5f4SPaul Zimmerman 		default:
1100197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
1101197ba5f4SPaul Zimmerman 				"%s: Unhandled descriptor error status (%d)\n",
1102197ba5f4SPaul Zimmerman 				__func__, halt_status);
1103197ba5f4SPaul Zimmerman 			break;
1104197ba5f4SPaul Zimmerman 		}
1105197ba5f4SPaul Zimmerman 		return 1;
1106197ba5f4SPaul Zimmerman 	}
1107197ba5f4SPaul Zimmerman 
1108197ba5f4SPaul Zimmerman 	if (dma_desc->status & HOST_DMA_A) {
1109197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
1110197ba5f4SPaul Zimmerman 			 "Active descriptor encountered on channel %d\n",
1111197ba5f4SPaul Zimmerman 			 chan->hc_num);
1112197ba5f4SPaul Zimmerman 		return 0;
1113197ba5f4SPaul Zimmerman 	}
1114197ba5f4SPaul Zimmerman 
1115197ba5f4SPaul Zimmerman 	if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1116197ba5f4SPaul Zimmerman 		if (qtd->control_phase == DWC2_CONTROL_DATA) {
1117197ba5f4SPaul Zimmerman 			urb->actual_length += n_bytes - remain;
1118197ba5f4SPaul Zimmerman 			if (remain || urb->actual_length >= urb->length) {
1119197ba5f4SPaul Zimmerman 				/*
1120197ba5f4SPaul Zimmerman 				 * For Control Data stage do not set urb->status
1121197ba5f4SPaul Zimmerman 				 * to 0, to prevent URB callback. Set it when
1122197ba5f4SPaul Zimmerman 				 * Status phase is done. See below.
1123197ba5f4SPaul Zimmerman 				 */
1124197ba5f4SPaul Zimmerman 				*xfer_done = 1;
1125197ba5f4SPaul Zimmerman 			}
1126197ba5f4SPaul Zimmerman 		} else if (qtd->control_phase == DWC2_CONTROL_STATUS) {
1127197ba5f4SPaul Zimmerman 			urb->status = 0;
1128197ba5f4SPaul Zimmerman 			*xfer_done = 1;
1129197ba5f4SPaul Zimmerman 		}
1130197ba5f4SPaul Zimmerman 		/* No handling for SETUP stage */
1131197ba5f4SPaul Zimmerman 	} else {
1132197ba5f4SPaul Zimmerman 		/* BULK and INTR */
1133197ba5f4SPaul Zimmerman 		urb->actual_length += n_bytes - remain;
1134197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "length=%d actual=%d\n", urb->length,
1135197ba5f4SPaul Zimmerman 			 urb->actual_length);
1136197ba5f4SPaul Zimmerman 		if (remain || urb->actual_length >= urb->length) {
1137197ba5f4SPaul Zimmerman 			urb->status = 0;
1138197ba5f4SPaul Zimmerman 			*xfer_done = 1;
1139197ba5f4SPaul Zimmerman 		}
1140197ba5f4SPaul Zimmerman 	}
1141197ba5f4SPaul Zimmerman 
1142197ba5f4SPaul Zimmerman 	return 0;
1143197ba5f4SPaul Zimmerman }
1144197ba5f4SPaul Zimmerman 
1145197ba5f4SPaul Zimmerman static int dwc2_process_non_isoc_desc(struct dwc2_hsotg *hsotg,
1146197ba5f4SPaul Zimmerman 				      struct dwc2_host_chan *chan,
1147197ba5f4SPaul Zimmerman 				      int chnum, struct dwc2_qtd *qtd,
1148197ba5f4SPaul Zimmerman 				      int desc_num,
1149197ba5f4SPaul Zimmerman 				      enum dwc2_halt_status halt_status,
1150197ba5f4SPaul Zimmerman 				      int *xfer_done)
1151197ba5f4SPaul Zimmerman {
1152197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = chan->qh;
1153197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb = qtd->urb;
1154197ba5f4SPaul Zimmerman 	struct dwc2_hcd_dma_desc *dma_desc;
1155197ba5f4SPaul Zimmerman 	u32 n_bytes;
1156197ba5f4SPaul Zimmerman 	int failed;
1157197ba5f4SPaul Zimmerman 
1158197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
1159197ba5f4SPaul Zimmerman 
1160197ba5f4SPaul Zimmerman 	if (!urb)
1161197ba5f4SPaul Zimmerman 		return -EINVAL;
1162197ba5f4SPaul Zimmerman 
116395105a99SGregory Herrero 	dma_sync_single_for_cpu(hsotg->dev,
116495105a99SGregory Herrero 				qh->desc_list_dma + (desc_num *
116595105a99SGregory Herrero 				sizeof(struct dwc2_hcd_dma_desc)),
116695105a99SGregory Herrero 				sizeof(struct dwc2_hcd_dma_desc),
116795105a99SGregory Herrero 				DMA_FROM_DEVICE);
116895105a99SGregory Herrero 
1169197ba5f4SPaul Zimmerman 	dma_desc = &qh->desc_list[desc_num];
1170197ba5f4SPaul Zimmerman 	n_bytes = qh->n_bytes[desc_num];
1171197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev,
1172197ba5f4SPaul Zimmerman 		 "qtd=%p dwc2_urb=%p desc_num=%d desc=%p n_bytes=%d\n",
1173197ba5f4SPaul Zimmerman 		 qtd, urb, desc_num, dma_desc, n_bytes);
1174197ba5f4SPaul Zimmerman 	failed = dwc2_update_non_isoc_urb_state_ddma(hsotg, chan, qtd, dma_desc,
1175197ba5f4SPaul Zimmerman 						     halt_status, n_bytes,
1176197ba5f4SPaul Zimmerman 						     xfer_done);
11773142a16bSVardan Mikayelyan 	if (failed || (*xfer_done && urb->status != -EINPROGRESS)) {
1178197ba5f4SPaul Zimmerman 		dwc2_host_complete(hsotg, qtd, urb->status);
1179197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
11803142a16bSVardan Mikayelyan 		dev_vdbg(hsotg->dev, "failed=%1x xfer_done=%1x\n",
11813142a16bSVardan Mikayelyan 			 failed, *xfer_done);
1182197ba5f4SPaul Zimmerman 		return failed;
1183197ba5f4SPaul Zimmerman 	}
1184197ba5f4SPaul Zimmerman 
1185197ba5f4SPaul Zimmerman 	if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1186197ba5f4SPaul Zimmerman 		switch (qtd->control_phase) {
1187197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_SETUP:
1188197ba5f4SPaul Zimmerman 			if (urb->length > 0)
1189197ba5f4SPaul Zimmerman 				qtd->control_phase = DWC2_CONTROL_DATA;
1190197ba5f4SPaul Zimmerman 			else
1191197ba5f4SPaul Zimmerman 				qtd->control_phase = DWC2_CONTROL_STATUS;
1192197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev,
1193197ba5f4SPaul Zimmerman 				 "  Control setup transaction done\n");
1194197ba5f4SPaul Zimmerman 			break;
1195197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_DATA:
1196197ba5f4SPaul Zimmerman 			if (*xfer_done) {
1197197ba5f4SPaul Zimmerman 				qtd->control_phase = DWC2_CONTROL_STATUS;
1198197ba5f4SPaul Zimmerman 				dev_vdbg(hsotg->dev,
1199197ba5f4SPaul Zimmerman 					 "  Control data transfer done\n");
1200197ba5f4SPaul Zimmerman 			} else if (desc_num + 1 == qtd->n_desc) {
1201197ba5f4SPaul Zimmerman 				/*
1202197ba5f4SPaul Zimmerman 				 * Last descriptor for Control data stage which
1203197ba5f4SPaul Zimmerman 				 * is not completed yet
1204197ba5f4SPaul Zimmerman 				 */
1205197ba5f4SPaul Zimmerman 				dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
1206197ba5f4SPaul Zimmerman 							  qtd);
1207197ba5f4SPaul Zimmerman 			}
1208197ba5f4SPaul Zimmerman 			break;
1209197ba5f4SPaul Zimmerman 		default:
1210197ba5f4SPaul Zimmerman 			break;
1211197ba5f4SPaul Zimmerman 		}
1212197ba5f4SPaul Zimmerman 	}
1213197ba5f4SPaul Zimmerman 
1214197ba5f4SPaul Zimmerman 	return 0;
1215197ba5f4SPaul Zimmerman }
1216197ba5f4SPaul Zimmerman 
1217197ba5f4SPaul Zimmerman static void dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
1218197ba5f4SPaul Zimmerman 					     struct dwc2_host_chan *chan,
1219197ba5f4SPaul Zimmerman 					     int chnum,
1220197ba5f4SPaul Zimmerman 					     enum dwc2_halt_status halt_status)
1221197ba5f4SPaul Zimmerman {
1222197ba5f4SPaul Zimmerman 	struct list_head *qtd_item, *qtd_tmp;
1223197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = chan->qh;
1224197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd = NULL;
1225197ba5f4SPaul Zimmerman 	int xfer_done;
1226197ba5f4SPaul Zimmerman 	int desc_num = 0;
1227197ba5f4SPaul Zimmerman 
1228197ba5f4SPaul Zimmerman 	if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
1229197ba5f4SPaul Zimmerman 		list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
1230197ba5f4SPaul Zimmerman 			qtd->in_process = 0;
1231197ba5f4SPaul Zimmerman 		return;
1232197ba5f4SPaul Zimmerman 	}
1233197ba5f4SPaul Zimmerman 
1234197ba5f4SPaul Zimmerman 	list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
1235197ba5f4SPaul Zimmerman 		int i;
12363142a16bSVardan Mikayelyan 		int qtd_desc_count;
1237197ba5f4SPaul Zimmerman 
1238197ba5f4SPaul Zimmerman 		qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
1239197ba5f4SPaul Zimmerman 		xfer_done = 0;
12403142a16bSVardan Mikayelyan 		qtd_desc_count = qtd->n_desc;
1241197ba5f4SPaul Zimmerman 
12423142a16bSVardan Mikayelyan 		for (i = 0; i < qtd_desc_count; i++) {
1243197ba5f4SPaul Zimmerman 			if (dwc2_process_non_isoc_desc(hsotg, chan, chnum, qtd,
1244197ba5f4SPaul Zimmerman 						       desc_num, halt_status,
12453142a16bSVardan Mikayelyan 						       &xfer_done))
12463142a16bSVardan Mikayelyan 				goto stop_scan;
12473142a16bSVardan Mikayelyan 
1248197ba5f4SPaul Zimmerman 			desc_num++;
1249197ba5f4SPaul Zimmerman 		}
1250197ba5f4SPaul Zimmerman 	}
1251197ba5f4SPaul Zimmerman 
12523142a16bSVardan Mikayelyan stop_scan:
1253197ba5f4SPaul Zimmerman 	if (qh->ep_type != USB_ENDPOINT_XFER_CONTROL) {
1254197ba5f4SPaul Zimmerman 		/*
1255197ba5f4SPaul Zimmerman 		 * Resetting the data toggle for bulk and interrupt endpoints
1256197ba5f4SPaul Zimmerman 		 * in case of stall. See handle_hc_stall_intr().
1257197ba5f4SPaul Zimmerman 		 */
1258197ba5f4SPaul Zimmerman 		if (halt_status == DWC2_HC_XFER_STALL)
1259197ba5f4SPaul Zimmerman 			qh->data_toggle = DWC2_HC_PID_DATA0;
126062943b7dSTang, Jianqiang 		else
1261197ba5f4SPaul Zimmerman 			dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1262197ba5f4SPaul Zimmerman 	}
1263197ba5f4SPaul Zimmerman 
1264197ba5f4SPaul Zimmerman 	if (halt_status == DWC2_HC_XFER_COMPLETE) {
1265197ba5f4SPaul Zimmerman 		if (chan->hcint & HCINTMSK_NYET) {
1266197ba5f4SPaul Zimmerman 			/*
1267197ba5f4SPaul Zimmerman 			 * Got a NYET on the last transaction of the transfer.
1268197ba5f4SPaul Zimmerman 			 * It means that the endpoint should be in the PING
1269197ba5f4SPaul Zimmerman 			 * state at the beginning of the next transfer.
1270197ba5f4SPaul Zimmerman 			 */
1271197ba5f4SPaul Zimmerman 			qh->ping_state = 1;
1272197ba5f4SPaul Zimmerman 		}
1273197ba5f4SPaul Zimmerman 	}
1274197ba5f4SPaul Zimmerman }
1275197ba5f4SPaul Zimmerman 
1276197ba5f4SPaul Zimmerman /**
1277197ba5f4SPaul Zimmerman  * dwc2_hcd_complete_xfer_ddma() - Scans the descriptor list, updates URB's
1278197ba5f4SPaul Zimmerman  * status and calls completion routine for the URB if it's done. Called from
1279197ba5f4SPaul Zimmerman  * interrupt handlers.
1280197ba5f4SPaul Zimmerman  *
1281197ba5f4SPaul Zimmerman  * @hsotg:       The HCD state structure for the DWC OTG controller
1282197ba5f4SPaul Zimmerman  * @chan:        Host channel the transfer is completed on
1283197ba5f4SPaul Zimmerman  * @chnum:       Index of Host channel registers
1284197ba5f4SPaul Zimmerman  * @halt_status: Reason the channel is being halted or just XferComplete
1285197ba5f4SPaul Zimmerman  *               for isochronous transfers
1286197ba5f4SPaul Zimmerman  *
1287197ba5f4SPaul Zimmerman  * Releases the channel to be used by other transfers.
1288197ba5f4SPaul Zimmerman  * In case of Isochronous endpoint the channel is not halted until the end of
1289197ba5f4SPaul Zimmerman  * the session, i.e. QTD list is empty.
1290197ba5f4SPaul Zimmerman  * If periodic channel released the FrameList is updated accordingly.
1291197ba5f4SPaul Zimmerman  * Calls transaction selection routines to activate pending transfers.
1292197ba5f4SPaul Zimmerman  */
1293197ba5f4SPaul Zimmerman void dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg *hsotg,
1294197ba5f4SPaul Zimmerman 				 struct dwc2_host_chan *chan, int chnum,
1295197ba5f4SPaul Zimmerman 				 enum dwc2_halt_status halt_status)
1296197ba5f4SPaul Zimmerman {
1297197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = chan->qh;
1298197ba5f4SPaul Zimmerman 	int continue_isoc_xfer = 0;
1299197ba5f4SPaul Zimmerman 	enum dwc2_transaction_type tr_type;
1300197ba5f4SPaul Zimmerman 
1301197ba5f4SPaul Zimmerman 	if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1302197ba5f4SPaul Zimmerman 		dwc2_complete_isoc_xfer_ddma(hsotg, chan, halt_status);
1303197ba5f4SPaul Zimmerman 
1304197ba5f4SPaul Zimmerman 		/* Release the channel if halted or session completed */
1305197ba5f4SPaul Zimmerman 		if (halt_status != DWC2_HC_XFER_COMPLETE ||
1306197ba5f4SPaul Zimmerman 		    list_empty(&qh->qtd_list)) {
1307c503b381SGregory Herrero 			struct dwc2_qtd *qtd, *qtd_tmp;
1308c503b381SGregory Herrero 
1309c503b381SGregory Herrero 			/*
1310c503b381SGregory Herrero 			 * Kill all remainings QTDs since channel has been
1311c503b381SGregory Herrero 			 * halted.
1312c503b381SGregory Herrero 			 */
1313c503b381SGregory Herrero 			list_for_each_entry_safe(qtd, qtd_tmp,
1314c503b381SGregory Herrero 						 &qh->qtd_list,
1315c503b381SGregory Herrero 						 qtd_list_entry) {
1316c503b381SGregory Herrero 				dwc2_host_complete(hsotg, qtd,
1317c503b381SGregory Herrero 						   -ECONNRESET);
1318c503b381SGregory Herrero 				dwc2_hcd_qtd_unlink_and_free(hsotg,
1319c503b381SGregory Herrero 							     qtd, qh);
1320c503b381SGregory Herrero 			}
1321c503b381SGregory Herrero 
1322197ba5f4SPaul Zimmerman 			/* Halt the channel if session completed */
1323197ba5f4SPaul Zimmerman 			if (halt_status == DWC2_HC_XFER_COMPLETE)
1324197ba5f4SPaul Zimmerman 				dwc2_hc_halt(hsotg, chan, halt_status);
1325197ba5f4SPaul Zimmerman 			dwc2_release_channel_ddma(hsotg, qh);
1326197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_unlink(hsotg, qh);
1327197ba5f4SPaul Zimmerman 		} else {
1328197ba5f4SPaul Zimmerman 			/* Keep in assigned schedule to continue transfer */
1329*94ef7aeeSDouglas Anderson 			list_move_tail(&qh->qh_list_entry,
1330197ba5f4SPaul Zimmerman 				       &hsotg->periodic_sched_assigned);
1331c503b381SGregory Herrero 			/*
1332c503b381SGregory Herrero 			 * If channel has been halted during giveback of urb
1333c503b381SGregory Herrero 			 * then prevent any new scheduling.
1334c503b381SGregory Herrero 			 */
1335c503b381SGregory Herrero 			if (!chan->halt_status)
1336197ba5f4SPaul Zimmerman 				continue_isoc_xfer = 1;
1337197ba5f4SPaul Zimmerman 		}
1338197ba5f4SPaul Zimmerman 		/*
1339197ba5f4SPaul Zimmerman 		 * Todo: Consider the case when period exceeds FrameList size.
1340197ba5f4SPaul Zimmerman 		 * Frame Rollover interrupt should be used.
1341197ba5f4SPaul Zimmerman 		 */
1342197ba5f4SPaul Zimmerman 	} else {
1343197ba5f4SPaul Zimmerman 		/*
1344197ba5f4SPaul Zimmerman 		 * Scan descriptor list to complete the URB(s), then release
1345197ba5f4SPaul Zimmerman 		 * the channel
1346197ba5f4SPaul Zimmerman 		 */
1347197ba5f4SPaul Zimmerman 		dwc2_complete_non_isoc_xfer_ddma(hsotg, chan, chnum,
1348197ba5f4SPaul Zimmerman 						 halt_status);
1349197ba5f4SPaul Zimmerman 		dwc2_release_channel_ddma(hsotg, qh);
1350197ba5f4SPaul Zimmerman 		dwc2_hcd_qh_unlink(hsotg, qh);
1351197ba5f4SPaul Zimmerman 
1352197ba5f4SPaul Zimmerman 		if (!list_empty(&qh->qtd_list)) {
1353197ba5f4SPaul Zimmerman 			/*
1354197ba5f4SPaul Zimmerman 			 * Add back to inactive non-periodic schedule on normal
1355197ba5f4SPaul Zimmerman 			 * completion
1356197ba5f4SPaul Zimmerman 			 */
1357197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_add(hsotg, qh);
1358197ba5f4SPaul Zimmerman 		}
1359197ba5f4SPaul Zimmerman 	}
1360197ba5f4SPaul Zimmerman 
1361197ba5f4SPaul Zimmerman 	tr_type = dwc2_hcd_select_transactions(hsotg);
1362197ba5f4SPaul Zimmerman 	if (tr_type != DWC2_TRANSACTION_NONE || continue_isoc_xfer) {
1363197ba5f4SPaul Zimmerman 		if (continue_isoc_xfer) {
1364197ba5f4SPaul Zimmerman 			if (tr_type == DWC2_TRANSACTION_NONE)
1365197ba5f4SPaul Zimmerman 				tr_type = DWC2_TRANSACTION_PERIODIC;
1366197ba5f4SPaul Zimmerman 			else if (tr_type == DWC2_TRANSACTION_NON_PERIODIC)
1367197ba5f4SPaul Zimmerman 				tr_type = DWC2_TRANSACTION_ALL;
1368197ba5f4SPaul Zimmerman 		}
1369197ba5f4SPaul Zimmerman 		dwc2_hcd_queue_transactions(hsotg, tr_type);
1370197ba5f4SPaul Zimmerman 	}
1371197ba5f4SPaul Zimmerman }
1372