xref: /openbmc/linux/drivers/usb/dwc2/hcd_ddma.c (revision c845428b7a9157523103100806bc8130d64769c8)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2197ba5f4SPaul Zimmerman /*
3197ba5f4SPaul Zimmerman  * hcd_ddma.c - DesignWare HS OTG Controller descriptor DMA routines
4197ba5f4SPaul Zimmerman  *
5197ba5f4SPaul Zimmerman  * Copyright (C) 2004-2013 Synopsys, Inc.
6197ba5f4SPaul Zimmerman  */
7197ba5f4SPaul Zimmerman 
8197ba5f4SPaul Zimmerman /*
9197ba5f4SPaul Zimmerman  * This file contains the Descriptor DMA implementation for Host mode
10197ba5f4SPaul Zimmerman  */
11197ba5f4SPaul Zimmerman #include <linux/kernel.h>
12197ba5f4SPaul Zimmerman #include <linux/module.h>
13197ba5f4SPaul Zimmerman #include <linux/spinlock.h>
14197ba5f4SPaul Zimmerman #include <linux/interrupt.h>
15197ba5f4SPaul Zimmerman #include <linux/dma-mapping.h>
16197ba5f4SPaul Zimmerman #include <linux/io.h>
17197ba5f4SPaul Zimmerman #include <linux/slab.h>
18197ba5f4SPaul Zimmerman #include <linux/usb.h>
19197ba5f4SPaul Zimmerman 
20197ba5f4SPaul Zimmerman #include <linux/usb/hcd.h>
21197ba5f4SPaul Zimmerman #include <linux/usb/ch11.h>
22197ba5f4SPaul Zimmerman 
23197ba5f4SPaul Zimmerman #include "core.h"
24197ba5f4SPaul Zimmerman #include "hcd.h"
25197ba5f4SPaul Zimmerman 
dwc2_frame_list_idx(u16 frame)26197ba5f4SPaul Zimmerman static u16 dwc2_frame_list_idx(u16 frame)
27197ba5f4SPaul Zimmerman {
28197ba5f4SPaul Zimmerman 	return frame & (FRLISTEN_64_SIZE - 1);
29197ba5f4SPaul Zimmerman }
30197ba5f4SPaul Zimmerman 
dwc2_desclist_idx_inc(u16 idx,u16 inc,u8 speed)31197ba5f4SPaul Zimmerman static u16 dwc2_desclist_idx_inc(u16 idx, u16 inc, u8 speed)
32197ba5f4SPaul Zimmerman {
33197ba5f4SPaul Zimmerman 	return (idx + inc) &
34197ba5f4SPaul Zimmerman 		((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
35197ba5f4SPaul Zimmerman 		  MAX_DMA_DESC_NUM_GENERIC) - 1);
36197ba5f4SPaul Zimmerman }
37197ba5f4SPaul Zimmerman 
dwc2_desclist_idx_dec(u16 idx,u16 inc,u8 speed)38197ba5f4SPaul Zimmerman static u16 dwc2_desclist_idx_dec(u16 idx, u16 inc, u8 speed)
39197ba5f4SPaul Zimmerman {
40197ba5f4SPaul Zimmerman 	return (idx - inc) &
41197ba5f4SPaul Zimmerman 		((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
42197ba5f4SPaul Zimmerman 		  MAX_DMA_DESC_NUM_GENERIC) - 1);
43197ba5f4SPaul Zimmerman }
44197ba5f4SPaul Zimmerman 
dwc2_max_desc_num(struct dwc2_qh * qh)45197ba5f4SPaul Zimmerman static u16 dwc2_max_desc_num(struct dwc2_qh *qh)
46197ba5f4SPaul Zimmerman {
47197ba5f4SPaul Zimmerman 	return (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
48197ba5f4SPaul Zimmerman 		qh->dev_speed == USB_SPEED_HIGH) ?
49197ba5f4SPaul Zimmerman 		MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC;
50197ba5f4SPaul Zimmerman }
51197ba5f4SPaul Zimmerman 
dwc2_frame_incr_val(struct dwc2_qh * qh)52197ba5f4SPaul Zimmerman static u16 dwc2_frame_incr_val(struct dwc2_qh *qh)
53197ba5f4SPaul Zimmerman {
54197ba5f4SPaul Zimmerman 	return qh->dev_speed == USB_SPEED_HIGH ?
55ced9eee1SDouglas Anderson 	       (qh->host_interval + 8 - 1) / 8 : qh->host_interval;
56197ba5f4SPaul Zimmerman }
57197ba5f4SPaul Zimmerman 
dwc2_desc_list_alloc(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,gfp_t flags)58197ba5f4SPaul Zimmerman static int dwc2_desc_list_alloc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
59197ba5f4SPaul Zimmerman 				gfp_t flags)
60197ba5f4SPaul Zimmerman {
613b5fcc9aSGregory Herrero 	struct kmem_cache *desc_cache;
623b5fcc9aSGregory Herrero 
63ab283202SJohn Youn 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
64ab283202SJohn Youn 	    qh->dev_speed == USB_SPEED_HIGH)
653b5fcc9aSGregory Herrero 		desc_cache = hsotg->desc_hsisoc_cache;
663b5fcc9aSGregory Herrero 	else
673b5fcc9aSGregory Herrero 		desc_cache = hsotg->desc_gen_cache;
683b5fcc9aSGregory Herrero 
69ec703251SVahram Aharonyan 	qh->desc_list_sz = sizeof(struct dwc2_dma_desc) *
7095105a99SGregory Herrero 						dwc2_max_desc_num(qh);
71197ba5f4SPaul Zimmerman 
723b5fcc9aSGregory Herrero 	qh->desc_list = kmem_cache_zalloc(desc_cache, flags | GFP_DMA);
73197ba5f4SPaul Zimmerman 	if (!qh->desc_list)
74197ba5f4SPaul Zimmerman 		return -ENOMEM;
75197ba5f4SPaul Zimmerman 
7695105a99SGregory Herrero 	qh->desc_list_dma = dma_map_single(hsotg->dev, qh->desc_list,
7795105a99SGregory Herrero 					   qh->desc_list_sz,
7895105a99SGregory Herrero 					   DMA_TO_DEVICE);
79197ba5f4SPaul Zimmerman 
809da51974SJohn Youn 	qh->n_bytes = kcalloc(dwc2_max_desc_num(qh), sizeof(u32), flags);
81197ba5f4SPaul Zimmerman 	if (!qh->n_bytes) {
8295105a99SGregory Herrero 		dma_unmap_single(hsotg->dev, qh->desc_list_dma,
8395105a99SGregory Herrero 				 qh->desc_list_sz,
8495105a99SGregory Herrero 				 DMA_FROM_DEVICE);
859bbe91a1SAmitoj Kaur Chawla 		kmem_cache_free(desc_cache, qh->desc_list);
86197ba5f4SPaul Zimmerman 		qh->desc_list = NULL;
87197ba5f4SPaul Zimmerman 		return -ENOMEM;
88197ba5f4SPaul Zimmerman 	}
89197ba5f4SPaul Zimmerman 
90197ba5f4SPaul Zimmerman 	return 0;
91197ba5f4SPaul Zimmerman }
92197ba5f4SPaul Zimmerman 
dwc2_desc_list_free(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)93197ba5f4SPaul Zimmerman static void dwc2_desc_list_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
94197ba5f4SPaul Zimmerman {
953b5fcc9aSGregory Herrero 	struct kmem_cache *desc_cache;
963b5fcc9aSGregory Herrero 
97ab283202SJohn Youn 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
98ab283202SJohn Youn 	    qh->dev_speed == USB_SPEED_HIGH)
993b5fcc9aSGregory Herrero 		desc_cache = hsotg->desc_hsisoc_cache;
1003b5fcc9aSGregory Herrero 	else
1013b5fcc9aSGregory Herrero 		desc_cache = hsotg->desc_gen_cache;
1023b5fcc9aSGregory Herrero 
103197ba5f4SPaul Zimmerman 	if (qh->desc_list) {
10495105a99SGregory Herrero 		dma_unmap_single(hsotg->dev, qh->desc_list_dma,
10595105a99SGregory Herrero 				 qh->desc_list_sz, DMA_FROM_DEVICE);
1063b5fcc9aSGregory Herrero 		kmem_cache_free(desc_cache, qh->desc_list);
107197ba5f4SPaul Zimmerman 		qh->desc_list = NULL;
108197ba5f4SPaul Zimmerman 	}
109197ba5f4SPaul Zimmerman 
110197ba5f4SPaul Zimmerman 	kfree(qh->n_bytes);
111197ba5f4SPaul Zimmerman 	qh->n_bytes = NULL;
112197ba5f4SPaul Zimmerman }
113197ba5f4SPaul Zimmerman 
dwc2_frame_list_alloc(struct dwc2_hsotg * hsotg,gfp_t mem_flags)114197ba5f4SPaul Zimmerman static int dwc2_frame_list_alloc(struct dwc2_hsotg *hsotg, gfp_t mem_flags)
115197ba5f4SPaul Zimmerman {
116197ba5f4SPaul Zimmerman 	if (hsotg->frame_list)
117197ba5f4SPaul Zimmerman 		return 0;
118197ba5f4SPaul Zimmerman 
11995105a99SGregory Herrero 	hsotg->frame_list_sz = 4 * FRLISTEN_64_SIZE;
12095105a99SGregory Herrero 	hsotg->frame_list = kzalloc(hsotg->frame_list_sz, GFP_ATOMIC | GFP_DMA);
121197ba5f4SPaul Zimmerman 	if (!hsotg->frame_list)
122197ba5f4SPaul Zimmerman 		return -ENOMEM;
123197ba5f4SPaul Zimmerman 
12495105a99SGregory Herrero 	hsotg->frame_list_dma = dma_map_single(hsotg->dev, hsotg->frame_list,
12595105a99SGregory Herrero 					       hsotg->frame_list_sz,
12695105a99SGregory Herrero 					       DMA_TO_DEVICE);
12795105a99SGregory Herrero 
128197ba5f4SPaul Zimmerman 	return 0;
129197ba5f4SPaul Zimmerman }
130197ba5f4SPaul Zimmerman 
dwc2_frame_list_free(struct dwc2_hsotg * hsotg)131197ba5f4SPaul Zimmerman static void dwc2_frame_list_free(struct dwc2_hsotg *hsotg)
132197ba5f4SPaul Zimmerman {
133197ba5f4SPaul Zimmerman 	unsigned long flags;
134197ba5f4SPaul Zimmerman 
135197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
136197ba5f4SPaul Zimmerman 
137197ba5f4SPaul Zimmerman 	if (!hsotg->frame_list) {
138197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
139197ba5f4SPaul Zimmerman 		return;
140197ba5f4SPaul Zimmerman 	}
141197ba5f4SPaul Zimmerman 
14295105a99SGregory Herrero 	dma_unmap_single(hsotg->dev, hsotg->frame_list_dma,
14395105a99SGregory Herrero 			 hsotg->frame_list_sz, DMA_FROM_DEVICE);
14495105a99SGregory Herrero 
14595105a99SGregory Herrero 	kfree(hsotg->frame_list);
146197ba5f4SPaul Zimmerman 	hsotg->frame_list = NULL;
147197ba5f4SPaul Zimmerman 
148197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
149197ba5f4SPaul Zimmerman }
150197ba5f4SPaul Zimmerman 
dwc2_per_sched_enable(struct dwc2_hsotg * hsotg,u32 fr_list_en)151197ba5f4SPaul Zimmerman static void dwc2_per_sched_enable(struct dwc2_hsotg *hsotg, u32 fr_list_en)
152197ba5f4SPaul Zimmerman {
153197ba5f4SPaul Zimmerman 	u32 hcfg;
154197ba5f4SPaul Zimmerman 	unsigned long flags;
155197ba5f4SPaul Zimmerman 
156197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
157197ba5f4SPaul Zimmerman 
158f25c42b8SGevorg Sahakyan 	hcfg = dwc2_readl(hsotg, HCFG);
159197ba5f4SPaul Zimmerman 	if (hcfg & HCFG_PERSCHEDENA) {
160197ba5f4SPaul Zimmerman 		/* already enabled */
161197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
162197ba5f4SPaul Zimmerman 		return;
163197ba5f4SPaul Zimmerman 	}
164197ba5f4SPaul Zimmerman 
165f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hsotg->frame_list_dma, HFLBADDR);
166197ba5f4SPaul Zimmerman 
167197ba5f4SPaul Zimmerman 	hcfg &= ~HCFG_FRLISTEN_MASK;
168197ba5f4SPaul Zimmerman 	hcfg |= fr_list_en | HCFG_PERSCHEDENA;
169197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Enabling Periodic schedule\n");
170f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcfg, HCFG);
171197ba5f4SPaul Zimmerman 
172197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
173197ba5f4SPaul Zimmerman }
174197ba5f4SPaul Zimmerman 
dwc2_per_sched_disable(struct dwc2_hsotg * hsotg)175197ba5f4SPaul Zimmerman static void dwc2_per_sched_disable(struct dwc2_hsotg *hsotg)
176197ba5f4SPaul Zimmerman {
177197ba5f4SPaul Zimmerman 	u32 hcfg;
178197ba5f4SPaul Zimmerman 	unsigned long flags;
179197ba5f4SPaul Zimmerman 
180197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
181197ba5f4SPaul Zimmerman 
182f25c42b8SGevorg Sahakyan 	hcfg = dwc2_readl(hsotg, HCFG);
183197ba5f4SPaul Zimmerman 	if (!(hcfg & HCFG_PERSCHEDENA)) {
184197ba5f4SPaul Zimmerman 		/* already disabled */
185197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
186197ba5f4SPaul Zimmerman 		return;
187197ba5f4SPaul Zimmerman 	}
188197ba5f4SPaul Zimmerman 
189197ba5f4SPaul Zimmerman 	hcfg &= ~HCFG_PERSCHEDENA;
190197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Disabling Periodic schedule\n");
191f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcfg, HCFG);
192197ba5f4SPaul Zimmerman 
193197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
194197ba5f4SPaul Zimmerman }
195197ba5f4SPaul Zimmerman 
196197ba5f4SPaul Zimmerman /*
197197ba5f4SPaul Zimmerman  * Activates/Deactivates FrameList entries for the channel based on endpoint
198197ba5f4SPaul Zimmerman  * servicing period
199197ba5f4SPaul Zimmerman  */
dwc2_update_frame_list(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,int enable)200197ba5f4SPaul Zimmerman static void dwc2_update_frame_list(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
201197ba5f4SPaul Zimmerman 				   int enable)
202197ba5f4SPaul Zimmerman {
203197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
204197ba5f4SPaul Zimmerman 	u16 i, j, inc;
205197ba5f4SPaul Zimmerman 
206197ba5f4SPaul Zimmerman 	if (!hsotg) {
207197ba5f4SPaul Zimmerman 		pr_err("hsotg = %p\n", hsotg);
208197ba5f4SPaul Zimmerman 		return;
209197ba5f4SPaul Zimmerman 	}
210197ba5f4SPaul Zimmerman 
211197ba5f4SPaul Zimmerman 	if (!qh->channel) {
212197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "qh->channel = %p\n", qh->channel);
213197ba5f4SPaul Zimmerman 		return;
214197ba5f4SPaul Zimmerman 	}
215197ba5f4SPaul Zimmerman 
216197ba5f4SPaul Zimmerman 	if (!hsotg->frame_list) {
217197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "hsotg->frame_list = %p\n",
218197ba5f4SPaul Zimmerman 			hsotg->frame_list);
219197ba5f4SPaul Zimmerman 		return;
220197ba5f4SPaul Zimmerman 	}
221197ba5f4SPaul Zimmerman 
222197ba5f4SPaul Zimmerman 	chan = qh->channel;
223197ba5f4SPaul Zimmerman 	inc = dwc2_frame_incr_val(qh);
224197ba5f4SPaul Zimmerman 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC)
225ced9eee1SDouglas Anderson 		i = dwc2_frame_list_idx(qh->next_active_frame);
226197ba5f4SPaul Zimmerman 	else
227197ba5f4SPaul Zimmerman 		i = 0;
228197ba5f4SPaul Zimmerman 
229197ba5f4SPaul Zimmerman 	j = i;
230197ba5f4SPaul Zimmerman 	do {
231197ba5f4SPaul Zimmerman 		if (enable)
232197ba5f4SPaul Zimmerman 			hsotg->frame_list[j] |= 1 << chan->hc_num;
233197ba5f4SPaul Zimmerman 		else
234197ba5f4SPaul Zimmerman 			hsotg->frame_list[j] &= ~(1 << chan->hc_num);
235197ba5f4SPaul Zimmerman 		j = (j + inc) & (FRLISTEN_64_SIZE - 1);
236197ba5f4SPaul Zimmerman 	} while (j != i);
237197ba5f4SPaul Zimmerman 
23895105a99SGregory Herrero 	/*
23995105a99SGregory Herrero 	 * Sync frame list since controller will access it if periodic
24095105a99SGregory Herrero 	 * channel is currently enabled.
24195105a99SGregory Herrero 	 */
24295105a99SGregory Herrero 	dma_sync_single_for_device(hsotg->dev,
24395105a99SGregory Herrero 				   hsotg->frame_list_dma,
24495105a99SGregory Herrero 				   hsotg->frame_list_sz,
24595105a99SGregory Herrero 				   DMA_TO_DEVICE);
24695105a99SGregory Herrero 
247197ba5f4SPaul Zimmerman 	if (!enable)
248197ba5f4SPaul Zimmerman 		return;
249197ba5f4SPaul Zimmerman 
250197ba5f4SPaul Zimmerman 	chan->schinfo = 0;
251ced9eee1SDouglas Anderson 	if (chan->speed == USB_SPEED_HIGH && qh->host_interval) {
252197ba5f4SPaul Zimmerman 		j = 1;
253197ba5f4SPaul Zimmerman 		/* TODO - check this */
254ced9eee1SDouglas Anderson 		inc = (8 + qh->host_interval - 1) / qh->host_interval;
255197ba5f4SPaul Zimmerman 		for (i = 0; i < inc; i++) {
256197ba5f4SPaul Zimmerman 			chan->schinfo |= j;
257ced9eee1SDouglas Anderson 			j = j << qh->host_interval;
258197ba5f4SPaul Zimmerman 		}
259197ba5f4SPaul Zimmerman 	} else {
260197ba5f4SPaul Zimmerman 		chan->schinfo = 0xff;
261197ba5f4SPaul Zimmerman 	}
262197ba5f4SPaul Zimmerman }
263197ba5f4SPaul Zimmerman 
dwc2_release_channel_ddma(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)264197ba5f4SPaul Zimmerman static void dwc2_release_channel_ddma(struct dwc2_hsotg *hsotg,
265197ba5f4SPaul Zimmerman 				      struct dwc2_qh *qh)
266197ba5f4SPaul Zimmerman {
267197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan = qh->channel;
268197ba5f4SPaul Zimmerman 
269197ba5f4SPaul Zimmerman 	if (dwc2_qh_is_non_per(qh)) {
27095832c00SJohn Youn 		if (hsotg->params.uframe_sched)
271197ba5f4SPaul Zimmerman 			hsotg->available_host_channels++;
272197ba5f4SPaul Zimmerman 		else
273197ba5f4SPaul Zimmerman 			hsotg->non_periodic_channels--;
274197ba5f4SPaul Zimmerman 	} else {
275197ba5f4SPaul Zimmerman 		dwc2_update_frame_list(hsotg, qh, 0);
2763f808bdaSGregory Herrero 		hsotg->available_host_channels++;
277197ba5f4SPaul Zimmerman 	}
278197ba5f4SPaul Zimmerman 
279197ba5f4SPaul Zimmerman 	/*
280197ba5f4SPaul Zimmerman 	 * The condition is added to prevent double cleanup try in case of
281197ba5f4SPaul Zimmerman 	 * device disconnect. See channel cleanup in dwc2_hcd_disconnect().
282197ba5f4SPaul Zimmerman 	 */
283197ba5f4SPaul Zimmerman 	if (chan->qh) {
284197ba5f4SPaul Zimmerman 		if (!list_empty(&chan->hc_list_entry))
285197ba5f4SPaul Zimmerman 			list_del(&chan->hc_list_entry);
286197ba5f4SPaul Zimmerman 		dwc2_hc_cleanup(hsotg, chan);
287197ba5f4SPaul Zimmerman 		list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
288197ba5f4SPaul Zimmerman 		chan->qh = NULL;
289197ba5f4SPaul Zimmerman 	}
290197ba5f4SPaul Zimmerman 
291197ba5f4SPaul Zimmerman 	qh->channel = NULL;
292197ba5f4SPaul Zimmerman 	qh->ntd = 0;
293197ba5f4SPaul Zimmerman 
294197ba5f4SPaul Zimmerman 	if (qh->desc_list)
295ec703251SVahram Aharonyan 		memset(qh->desc_list, 0, sizeof(struct dwc2_dma_desc) *
296197ba5f4SPaul Zimmerman 		       dwc2_max_desc_num(qh));
297197ba5f4SPaul Zimmerman }
298197ba5f4SPaul Zimmerman 
299197ba5f4SPaul Zimmerman /**
300197ba5f4SPaul Zimmerman  * dwc2_hcd_qh_init_ddma() - Initializes a QH structure's Descriptor DMA
301197ba5f4SPaul Zimmerman  * related members
302197ba5f4SPaul Zimmerman  *
303197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure for the DWC OTG controller
304197ba5f4SPaul Zimmerman  * @qh:    The QH to init
3056fb914d7SGrigor Tovmasyan  * @mem_flags: Indicates the type of memory allocation
306197ba5f4SPaul Zimmerman  *
307197ba5f4SPaul Zimmerman  * Return: 0 if successful, negative error code otherwise
308197ba5f4SPaul Zimmerman  *
309197ba5f4SPaul Zimmerman  * Allocates memory for the descriptor list. For the first periodic QH,
310197ba5f4SPaul Zimmerman  * allocates memory for the FrameList and enables periodic scheduling.
311197ba5f4SPaul Zimmerman  */
dwc2_hcd_qh_init_ddma(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,gfp_t mem_flags)312197ba5f4SPaul Zimmerman int dwc2_hcd_qh_init_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
313197ba5f4SPaul Zimmerman 			  gfp_t mem_flags)
314197ba5f4SPaul Zimmerman {
315197ba5f4SPaul Zimmerman 	int retval;
316197ba5f4SPaul Zimmerman 
317197ba5f4SPaul Zimmerman 	if (qh->do_split) {
318197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev,
319197ba5f4SPaul Zimmerman 			"SPLIT Transfers are not supported in Descriptor DMA mode.\n");
320197ba5f4SPaul Zimmerman 		retval = -EINVAL;
321197ba5f4SPaul Zimmerman 		goto err0;
322197ba5f4SPaul Zimmerman 	}
323197ba5f4SPaul Zimmerman 
324197ba5f4SPaul Zimmerman 	retval = dwc2_desc_list_alloc(hsotg, qh, mem_flags);
325197ba5f4SPaul Zimmerman 	if (retval)
326197ba5f4SPaul Zimmerman 		goto err0;
327197ba5f4SPaul Zimmerman 
328197ba5f4SPaul Zimmerman 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
329197ba5f4SPaul Zimmerman 	    qh->ep_type == USB_ENDPOINT_XFER_INT) {
330197ba5f4SPaul Zimmerman 		if (!hsotg->frame_list) {
331197ba5f4SPaul Zimmerman 			retval = dwc2_frame_list_alloc(hsotg, mem_flags);
332197ba5f4SPaul Zimmerman 			if (retval)
333197ba5f4SPaul Zimmerman 				goto err1;
334197ba5f4SPaul Zimmerman 			/* Enable periodic schedule on first periodic QH */
335197ba5f4SPaul Zimmerman 			dwc2_per_sched_enable(hsotg, HCFG_FRLISTEN_64);
336197ba5f4SPaul Zimmerman 		}
337197ba5f4SPaul Zimmerman 	}
338197ba5f4SPaul Zimmerman 
339197ba5f4SPaul Zimmerman 	qh->ntd = 0;
340197ba5f4SPaul Zimmerman 	return 0;
341197ba5f4SPaul Zimmerman 
342197ba5f4SPaul Zimmerman err1:
343197ba5f4SPaul Zimmerman 	dwc2_desc_list_free(hsotg, qh);
344197ba5f4SPaul Zimmerman err0:
345197ba5f4SPaul Zimmerman 	return retval;
346197ba5f4SPaul Zimmerman }
347197ba5f4SPaul Zimmerman 
348197ba5f4SPaul Zimmerman /**
349197ba5f4SPaul Zimmerman  * dwc2_hcd_qh_free_ddma() - Frees a QH structure's Descriptor DMA related
350197ba5f4SPaul Zimmerman  * members
351197ba5f4SPaul Zimmerman  *
352197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure for the DWC OTG controller
353197ba5f4SPaul Zimmerman  * @qh:    The QH to free
354197ba5f4SPaul Zimmerman  *
355197ba5f4SPaul Zimmerman  * Frees descriptor list memory associated with the QH. If QH is periodic and
356197ba5f4SPaul Zimmerman  * the last, frees FrameList memory and disables periodic scheduling.
357197ba5f4SPaul Zimmerman  */
dwc2_hcd_qh_free_ddma(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)358197ba5f4SPaul Zimmerman void dwc2_hcd_qh_free_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
359197ba5f4SPaul Zimmerman {
3602b046bc5SGregory Herrero 	unsigned long flags;
3612b046bc5SGregory Herrero 
362197ba5f4SPaul Zimmerman 	dwc2_desc_list_free(hsotg, qh);
363197ba5f4SPaul Zimmerman 
364197ba5f4SPaul Zimmerman 	/*
365197ba5f4SPaul Zimmerman 	 * Channel still assigned due to some reasons.
366197ba5f4SPaul Zimmerman 	 * Seen on Isoc URB dequeue. Channel halted but no subsequent
367197ba5f4SPaul Zimmerman 	 * ChHalted interrupt to release the channel. Afterwards
368197ba5f4SPaul Zimmerman 	 * when it comes here from endpoint disable routine
369197ba5f4SPaul Zimmerman 	 * channel remains assigned.
370197ba5f4SPaul Zimmerman 	 */
3712b046bc5SGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
372197ba5f4SPaul Zimmerman 	if (qh->channel)
373197ba5f4SPaul Zimmerman 		dwc2_release_channel_ddma(hsotg, qh);
3742b046bc5SGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
375197ba5f4SPaul Zimmerman 
376197ba5f4SPaul Zimmerman 	if ((qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
377197ba5f4SPaul Zimmerman 	     qh->ep_type == USB_ENDPOINT_XFER_INT) &&
37895832c00SJohn Youn 	    (hsotg->params.uframe_sched ||
379197ba5f4SPaul Zimmerman 	     !hsotg->periodic_channels) && hsotg->frame_list) {
380197ba5f4SPaul Zimmerman 		dwc2_per_sched_disable(hsotg);
381197ba5f4SPaul Zimmerman 		dwc2_frame_list_free(hsotg);
382197ba5f4SPaul Zimmerman 	}
383197ba5f4SPaul Zimmerman }
384197ba5f4SPaul Zimmerman 
dwc2_frame_to_desc_idx(struct dwc2_qh * qh,u16 frame_idx)385197ba5f4SPaul Zimmerman static u8 dwc2_frame_to_desc_idx(struct dwc2_qh *qh, u16 frame_idx)
386197ba5f4SPaul Zimmerman {
387197ba5f4SPaul Zimmerman 	if (qh->dev_speed == USB_SPEED_HIGH)
388197ba5f4SPaul Zimmerman 		/* Descriptor set (8 descriptors) index which is 8-aligned */
389197ba5f4SPaul Zimmerman 		return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
390197ba5f4SPaul Zimmerman 	else
391197ba5f4SPaul Zimmerman 		return frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1);
392197ba5f4SPaul Zimmerman }
393197ba5f4SPaul Zimmerman 
394197ba5f4SPaul Zimmerman /*
395197ba5f4SPaul Zimmerman  * Determine starting frame for Isochronous transfer.
396197ba5f4SPaul Zimmerman  * Few frames skipped to prevent race condition with HC.
397197ba5f4SPaul Zimmerman  */
dwc2_calc_starting_frame(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,u16 * skip_frames)398197ba5f4SPaul Zimmerman static u16 dwc2_calc_starting_frame(struct dwc2_hsotg *hsotg,
399197ba5f4SPaul Zimmerman 				    struct dwc2_qh *qh, u16 *skip_frames)
400197ba5f4SPaul Zimmerman {
401197ba5f4SPaul Zimmerman 	u16 frame;
402197ba5f4SPaul Zimmerman 
403197ba5f4SPaul Zimmerman 	hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
404197ba5f4SPaul Zimmerman 
405ced9eee1SDouglas Anderson 	/*
406ced9eee1SDouglas Anderson 	 * next_active_frame is always frame number (not uFrame) both in FS
407ced9eee1SDouglas Anderson 	 * and HS!
408ced9eee1SDouglas Anderson 	 */
409197ba5f4SPaul Zimmerman 
410197ba5f4SPaul Zimmerman 	/*
411197ba5f4SPaul Zimmerman 	 * skip_frames is used to limit activated descriptors number
412197ba5f4SPaul Zimmerman 	 * to avoid the situation when HC services the last activated
413197ba5f4SPaul Zimmerman 	 * descriptor firstly.
414197ba5f4SPaul Zimmerman 	 * Example for FS:
415197ba5f4SPaul Zimmerman 	 * Current frame is 1, scheduled frame is 3. Since HC always fetches
416197ba5f4SPaul Zimmerman 	 * the descriptor corresponding to curr_frame+1, the descriptor
417197ba5f4SPaul Zimmerman 	 * corresponding to frame 2 will be fetched. If the number of
418197ba5f4SPaul Zimmerman 	 * descriptors is max=64 (or greather) the list will be fully programmed
419197ba5f4SPaul Zimmerman 	 * with Active descriptors and it is possible case (rare) that the
420197ba5f4SPaul Zimmerman 	 * latest descriptor(considering rollback) corresponding to frame 2 will
421197ba5f4SPaul Zimmerman 	 * be serviced first. HS case is more probable because, in fact, up to
422197ba5f4SPaul Zimmerman 	 * 11 uframes (16 in the code) may be skipped.
423197ba5f4SPaul Zimmerman 	 */
424197ba5f4SPaul Zimmerman 	if (qh->dev_speed == USB_SPEED_HIGH) {
425197ba5f4SPaul Zimmerman 		/*
426197ba5f4SPaul Zimmerman 		 * Consider uframe counter also, to start xfer asap. If half of
427197ba5f4SPaul Zimmerman 		 * the frame elapsed skip 2 frames otherwise just 1 frame.
428197ba5f4SPaul Zimmerman 		 * Starting descriptor index must be 8-aligned, so if the
429197ba5f4SPaul Zimmerman 		 * current frame is near to complete the next one is skipped as
430197ba5f4SPaul Zimmerman 		 * well.
431197ba5f4SPaul Zimmerman 		 */
432197ba5f4SPaul Zimmerman 		if (dwc2_micro_frame_num(hsotg->frame_number) >= 5) {
433197ba5f4SPaul Zimmerman 			*skip_frames = 2 * 8;
434197ba5f4SPaul Zimmerman 			frame = dwc2_frame_num_inc(hsotg->frame_number,
435197ba5f4SPaul Zimmerman 						   *skip_frames);
436197ba5f4SPaul Zimmerman 		} else {
437197ba5f4SPaul Zimmerman 			*skip_frames = 1 * 8;
438197ba5f4SPaul Zimmerman 			frame = dwc2_frame_num_inc(hsotg->frame_number,
439197ba5f4SPaul Zimmerman 						   *skip_frames);
440197ba5f4SPaul Zimmerman 		}
441197ba5f4SPaul Zimmerman 
442197ba5f4SPaul Zimmerman 		frame = dwc2_full_frame_num(frame);
443197ba5f4SPaul Zimmerman 	} else {
444197ba5f4SPaul Zimmerman 		/*
445197ba5f4SPaul Zimmerman 		 * Two frames are skipped for FS - the current and the next.
446197ba5f4SPaul Zimmerman 		 * But for descriptor programming, 1 frame (descriptor) is
447197ba5f4SPaul Zimmerman 		 * enough, see example above.
448197ba5f4SPaul Zimmerman 		 */
449197ba5f4SPaul Zimmerman 		*skip_frames = 1;
450197ba5f4SPaul Zimmerman 		frame = dwc2_frame_num_inc(hsotg->frame_number, 2);
451197ba5f4SPaul Zimmerman 	}
452197ba5f4SPaul Zimmerman 
453197ba5f4SPaul Zimmerman 	return frame;
454197ba5f4SPaul Zimmerman }
455197ba5f4SPaul Zimmerman 
456197ba5f4SPaul Zimmerman /*
457197ba5f4SPaul Zimmerman  * Calculate initial descriptor index for isochronous transfer based on
458197ba5f4SPaul Zimmerman  * scheduled frame
459197ba5f4SPaul Zimmerman  */
dwc2_recalc_initial_desc_idx(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)460197ba5f4SPaul Zimmerman static u16 dwc2_recalc_initial_desc_idx(struct dwc2_hsotg *hsotg,
461197ba5f4SPaul Zimmerman 					struct dwc2_qh *qh)
462197ba5f4SPaul Zimmerman {
463197ba5f4SPaul Zimmerman 	u16 frame, fr_idx, fr_idx_tmp, skip_frames;
464197ba5f4SPaul Zimmerman 
465197ba5f4SPaul Zimmerman 	/*
466197ba5f4SPaul Zimmerman 	 * With current ISOC processing algorithm the channel is being released
467197ba5f4SPaul Zimmerman 	 * when no more QTDs in the list (qh->ntd == 0). Thus this function is
468197ba5f4SPaul Zimmerman 	 * called only when qh->ntd == 0 and qh->channel == 0.
469197ba5f4SPaul Zimmerman 	 *
470197ba5f4SPaul Zimmerman 	 * So qh->channel != NULL branch is not used and just not removed from
471197ba5f4SPaul Zimmerman 	 * the source file. It is required for another possible approach which
472197ba5f4SPaul Zimmerman 	 * is, do not disable and release the channel when ISOC session
473197ba5f4SPaul Zimmerman 	 * completed, just move QH to inactive schedule until new QTD arrives.
474197ba5f4SPaul Zimmerman 	 * On new QTD, the QH moved back to 'ready' schedule, starting frame and
475197ba5f4SPaul Zimmerman 	 * therefore starting desc_index are recalculated. In this case channel
476197ba5f4SPaul Zimmerman 	 * is released only on ep_disable.
477197ba5f4SPaul Zimmerman 	 */
478197ba5f4SPaul Zimmerman 
479197ba5f4SPaul Zimmerman 	/*
480197ba5f4SPaul Zimmerman 	 * Calculate starting descriptor index. For INTERRUPT endpoint it is
481197ba5f4SPaul Zimmerman 	 * always 0.
482197ba5f4SPaul Zimmerman 	 */
483197ba5f4SPaul Zimmerman 	if (qh->channel) {
484197ba5f4SPaul Zimmerman 		frame = dwc2_calc_starting_frame(hsotg, qh, &skip_frames);
485197ba5f4SPaul Zimmerman 		/*
486197ba5f4SPaul Zimmerman 		 * Calculate initial descriptor index based on FrameList current
487197ba5f4SPaul Zimmerman 		 * bitmap and servicing period
488197ba5f4SPaul Zimmerman 		 */
489197ba5f4SPaul Zimmerman 		fr_idx_tmp = dwc2_frame_list_idx(frame);
490197ba5f4SPaul Zimmerman 		fr_idx = (FRLISTEN_64_SIZE +
491ced9eee1SDouglas Anderson 			  dwc2_frame_list_idx(qh->next_active_frame) -
492ced9eee1SDouglas Anderson 			  fr_idx_tmp) % dwc2_frame_incr_val(qh);
493197ba5f4SPaul Zimmerman 		fr_idx = (fr_idx + fr_idx_tmp) % FRLISTEN_64_SIZE;
494197ba5f4SPaul Zimmerman 	} else {
495ced9eee1SDouglas Anderson 		qh->next_active_frame = dwc2_calc_starting_frame(hsotg, qh,
496197ba5f4SPaul Zimmerman 							   &skip_frames);
497ced9eee1SDouglas Anderson 		fr_idx = dwc2_frame_list_idx(qh->next_active_frame);
498197ba5f4SPaul Zimmerman 	}
499197ba5f4SPaul Zimmerman 
500197ba5f4SPaul Zimmerman 	qh->td_first = qh->td_last = dwc2_frame_to_desc_idx(qh, fr_idx);
501197ba5f4SPaul Zimmerman 
502197ba5f4SPaul Zimmerman 	return skip_frames;
503197ba5f4SPaul Zimmerman }
504197ba5f4SPaul Zimmerman 
505197ba5f4SPaul Zimmerman #define ISOC_URB_GIVEBACK_ASAP
506197ba5f4SPaul Zimmerman 
507197ba5f4SPaul Zimmerman #define MAX_ISOC_XFER_SIZE_FS	1023
508197ba5f4SPaul Zimmerman #define MAX_ISOC_XFER_SIZE_HS	3072
509197ba5f4SPaul Zimmerman #define DESCNUM_THRESHOLD	4
510197ba5f4SPaul Zimmerman 
dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg * hsotg,struct dwc2_qtd * qtd,struct dwc2_qh * qh,u32 max_xfer_size,u16 idx)511197ba5f4SPaul Zimmerman static void dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
512197ba5f4SPaul Zimmerman 					 struct dwc2_qtd *qtd,
513197ba5f4SPaul Zimmerman 					 struct dwc2_qh *qh, u32 max_xfer_size,
514197ba5f4SPaul Zimmerman 					 u16 idx)
515197ba5f4SPaul Zimmerman {
516ec703251SVahram Aharonyan 	struct dwc2_dma_desc *dma_desc = &qh->desc_list[idx];
517197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
518197ba5f4SPaul Zimmerman 
519197ba5f4SPaul Zimmerman 	memset(dma_desc, 0, sizeof(*dma_desc));
520197ba5f4SPaul Zimmerman 	frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
521197ba5f4SPaul Zimmerman 
522197ba5f4SPaul Zimmerman 	if (frame_desc->length > max_xfer_size)
523197ba5f4SPaul Zimmerman 		qh->n_bytes[idx] = max_xfer_size;
524197ba5f4SPaul Zimmerman 	else
525197ba5f4SPaul Zimmerman 		qh->n_bytes[idx] = frame_desc->length;
526197ba5f4SPaul Zimmerman 
527197ba5f4SPaul Zimmerman 	dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
528197ba5f4SPaul Zimmerman 	dma_desc->status = qh->n_bytes[idx] << HOST_DMA_ISOC_NBYTES_SHIFT &
529197ba5f4SPaul Zimmerman 			   HOST_DMA_ISOC_NBYTES_MASK;
530197ba5f4SPaul Zimmerman 
531dde4c1bfSGregory Herrero 	/* Set active bit */
532dde4c1bfSGregory Herrero 	dma_desc->status |= HOST_DMA_A;
533dde4c1bfSGregory Herrero 
5343ac38d26SGregory Herrero 	qh->ntd++;
5353ac38d26SGregory Herrero 	qtd->isoc_frame_index_last++;
5363ac38d26SGregory Herrero 
537197ba5f4SPaul Zimmerman #ifdef ISOC_URB_GIVEBACK_ASAP
538197ba5f4SPaul Zimmerman 	/* Set IOC for each descriptor corresponding to last frame of URB */
539197ba5f4SPaul Zimmerman 	if (qtd->isoc_frame_index_last == qtd->urb->packet_count)
540197ba5f4SPaul Zimmerman 		dma_desc->status |= HOST_DMA_IOC;
541197ba5f4SPaul Zimmerman #endif
542197ba5f4SPaul Zimmerman 
54395105a99SGregory Herrero 	dma_sync_single_for_device(hsotg->dev,
54495105a99SGregory Herrero 				   qh->desc_list_dma +
545ec703251SVahram Aharonyan 			(idx * sizeof(struct dwc2_dma_desc)),
546ec703251SVahram Aharonyan 			sizeof(struct dwc2_dma_desc),
54795105a99SGregory Herrero 			DMA_TO_DEVICE);
548197ba5f4SPaul Zimmerman }
549197ba5f4SPaul Zimmerman 
dwc2_init_isoc_dma_desc(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,u16 skip_frames)550197ba5f4SPaul Zimmerman static void dwc2_init_isoc_dma_desc(struct dwc2_hsotg *hsotg,
551197ba5f4SPaul Zimmerman 				    struct dwc2_qh *qh, u16 skip_frames)
552197ba5f4SPaul Zimmerman {
553197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
554197ba5f4SPaul Zimmerman 	u32 max_xfer_size;
555c17b337cSGregory Herrero 	u16 idx, inc, n_desc = 0, ntd_max = 0;
556c17b337cSGregory Herrero 	u16 cur_idx;
557c17b337cSGregory Herrero 	u16 next_idx;
558197ba5f4SPaul Zimmerman 
559197ba5f4SPaul Zimmerman 	idx = qh->td_last;
560ced9eee1SDouglas Anderson 	inc = qh->host_interval;
561c17b337cSGregory Herrero 	hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
5628d310e5dSMinas Harutyunyan 	cur_idx = idx;
563c17b337cSGregory Herrero 	next_idx = dwc2_desclist_idx_inc(qh->td_last, inc, qh->dev_speed);
564c17b337cSGregory Herrero 
565c17b337cSGregory Herrero 	/*
566c17b337cSGregory Herrero 	 * Ensure current frame number didn't overstep last scheduled
567c17b337cSGregory Herrero 	 * descriptor. If it happens, the only way to recover is to move
568c17b337cSGregory Herrero 	 * qh->td_last to current frame number + 1.
569c17b337cSGregory Herrero 	 * So that next isoc descriptor will be scheduled on frame number + 1
570c17b337cSGregory Herrero 	 * and not on a past frame.
571c17b337cSGregory Herrero 	 */
572c17b337cSGregory Herrero 	if (dwc2_frame_idx_num_gt(cur_idx, next_idx) || (cur_idx == next_idx)) {
573c17b337cSGregory Herrero 		if (inc < 32) {
574c17b337cSGregory Herrero 			dev_vdbg(hsotg->dev,
575c17b337cSGregory Herrero 				 "current frame number overstep last descriptor\n");
576c17b337cSGregory Herrero 			qh->td_last = dwc2_desclist_idx_inc(cur_idx, inc,
577c17b337cSGregory Herrero 							    qh->dev_speed);
578c17b337cSGregory Herrero 			idx = qh->td_last;
579c17b337cSGregory Herrero 		}
580c17b337cSGregory Herrero 	}
581197ba5f4SPaul Zimmerman 
582ced9eee1SDouglas Anderson 	if (qh->host_interval) {
583ced9eee1SDouglas Anderson 		ntd_max = (dwc2_max_desc_num(qh) + qh->host_interval - 1) /
584ced9eee1SDouglas Anderson 				qh->host_interval;
585197ba5f4SPaul Zimmerman 		if (skip_frames && !qh->channel)
586ced9eee1SDouglas Anderson 			ntd_max -= skip_frames / qh->host_interval;
587197ba5f4SPaul Zimmerman 	}
588197ba5f4SPaul Zimmerman 
589197ba5f4SPaul Zimmerman 	max_xfer_size = qh->dev_speed == USB_SPEED_HIGH ?
590197ba5f4SPaul Zimmerman 			MAX_ISOC_XFER_SIZE_HS : MAX_ISOC_XFER_SIZE_FS;
591197ba5f4SPaul Zimmerman 
592197ba5f4SPaul Zimmerman 	list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
593c17b337cSGregory Herrero 		if (qtd->in_process &&
594c17b337cSGregory Herrero 		    qtd->isoc_frame_index_last ==
595c17b337cSGregory Herrero 		    qtd->urb->packet_count)
596c17b337cSGregory Herrero 			continue;
597c17b337cSGregory Herrero 
598c17b337cSGregory Herrero 		qtd->isoc_td_first = idx;
599197ba5f4SPaul Zimmerman 		while (qh->ntd < ntd_max && qtd->isoc_frame_index_last <
600197ba5f4SPaul Zimmerman 						qtd->urb->packet_count) {
601197ba5f4SPaul Zimmerman 			dwc2_fill_host_isoc_dma_desc(hsotg, qtd, qh,
602197ba5f4SPaul Zimmerman 						     max_xfer_size, idx);
603197ba5f4SPaul Zimmerman 			idx = dwc2_desclist_idx_inc(idx, inc, qh->dev_speed);
604197ba5f4SPaul Zimmerman 			n_desc++;
605197ba5f4SPaul Zimmerman 		}
606c17b337cSGregory Herrero 		qtd->isoc_td_last = idx;
607197ba5f4SPaul Zimmerman 		qtd->in_process = 1;
608197ba5f4SPaul Zimmerman 	}
609197ba5f4SPaul Zimmerman 
610197ba5f4SPaul Zimmerman 	qh->td_last = idx;
611197ba5f4SPaul Zimmerman 
612197ba5f4SPaul Zimmerman #ifdef ISOC_URB_GIVEBACK_ASAP
613197ba5f4SPaul Zimmerman 	/* Set IOC for last descriptor if descriptor list is full */
614197ba5f4SPaul Zimmerman 	if (qh->ntd == ntd_max) {
615197ba5f4SPaul Zimmerman 		idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
616197ba5f4SPaul Zimmerman 		qh->desc_list[idx].status |= HOST_DMA_IOC;
61795105a99SGregory Herrero 		dma_sync_single_for_device(hsotg->dev,
61895105a99SGregory Herrero 					   qh->desc_list_dma + (idx *
619ec703251SVahram Aharonyan 					   sizeof(struct dwc2_dma_desc)),
620ec703251SVahram Aharonyan 					   sizeof(struct dwc2_dma_desc),
62195105a99SGregory Herrero 					   DMA_TO_DEVICE);
622197ba5f4SPaul Zimmerman 	}
623197ba5f4SPaul Zimmerman #else
624197ba5f4SPaul Zimmerman 	/*
625197ba5f4SPaul Zimmerman 	 * Set IOC bit only for one descriptor. Always try to be ahead of HW
626197ba5f4SPaul Zimmerman 	 * processing, i.e. on IOC generation driver activates next descriptor
627197ba5f4SPaul Zimmerman 	 * but core continues to process descriptors following the one with IOC
628197ba5f4SPaul Zimmerman 	 * set.
629197ba5f4SPaul Zimmerman 	 */
630197ba5f4SPaul Zimmerman 
631197ba5f4SPaul Zimmerman 	if (n_desc > DESCNUM_THRESHOLD)
632197ba5f4SPaul Zimmerman 		/*
633197ba5f4SPaul Zimmerman 		 * Move IOC "up". Required even if there is only one QTD
634197ba5f4SPaul Zimmerman 		 * in the list, because QTDs might continue to be queued,
635197ba5f4SPaul Zimmerman 		 * but during the activation it was only one queued.
636197ba5f4SPaul Zimmerman 		 * Actually more than one QTD might be in the list if this
637197ba5f4SPaul Zimmerman 		 * function called from XferCompletion - QTDs was queued during
638197ba5f4SPaul Zimmerman 		 * HW processing of the previous descriptor chunk.
639197ba5f4SPaul Zimmerman 		 */
640197ba5f4SPaul Zimmerman 		idx = dwc2_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2),
641197ba5f4SPaul Zimmerman 					    qh->dev_speed);
642197ba5f4SPaul Zimmerman 	else
643197ba5f4SPaul Zimmerman 		/*
644197ba5f4SPaul Zimmerman 		 * Set the IOC for the latest descriptor if either number of
645197ba5f4SPaul Zimmerman 		 * descriptors is not greater than threshold or no more new
646197ba5f4SPaul Zimmerman 		 * descriptors activated
647197ba5f4SPaul Zimmerman 		 */
648197ba5f4SPaul Zimmerman 		idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
649197ba5f4SPaul Zimmerman 
650197ba5f4SPaul Zimmerman 	qh->desc_list[idx].status |= HOST_DMA_IOC;
65195105a99SGregory Herrero 	dma_sync_single_for_device(hsotg->dev,
65295105a99SGregory Herrero 				   qh->desc_list_dma +
653ec703251SVahram Aharonyan 				   (idx * sizeof(struct dwc2_dma_desc)),
654ec703251SVahram Aharonyan 				   sizeof(struct dwc2_dma_desc),
65595105a99SGregory Herrero 				   DMA_TO_DEVICE);
656197ba5f4SPaul Zimmerman #endif
657197ba5f4SPaul Zimmerman }
658197ba5f4SPaul Zimmerman 
dwc2_fill_host_dma_desc(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,struct dwc2_qtd * qtd,struct dwc2_qh * qh,int n_desc)659197ba5f4SPaul Zimmerman static void dwc2_fill_host_dma_desc(struct dwc2_hsotg *hsotg,
660197ba5f4SPaul Zimmerman 				    struct dwc2_host_chan *chan,
661197ba5f4SPaul Zimmerman 				    struct dwc2_qtd *qtd, struct dwc2_qh *qh,
662197ba5f4SPaul Zimmerman 				    int n_desc)
663197ba5f4SPaul Zimmerman {
664ec703251SVahram Aharonyan 	struct dwc2_dma_desc *dma_desc = &qh->desc_list[n_desc];
665197ba5f4SPaul Zimmerman 	int len = chan->xfer_len;
666197ba5f4SPaul Zimmerman 
6673a1ec351SVahram Aharonyan 	if (len > HOST_DMA_NBYTES_LIMIT - (chan->max_packet - 1))
6683a1ec351SVahram Aharonyan 		len = HOST_DMA_NBYTES_LIMIT - (chan->max_packet - 1);
669197ba5f4SPaul Zimmerman 
670197ba5f4SPaul Zimmerman 	if (chan->ep_is_in) {
671197ba5f4SPaul Zimmerman 		int num_packets;
672197ba5f4SPaul Zimmerman 
673197ba5f4SPaul Zimmerman 		if (len > 0 && chan->max_packet)
674197ba5f4SPaul Zimmerman 			num_packets = (len + chan->max_packet - 1)
675197ba5f4SPaul Zimmerman 					/ chan->max_packet;
676197ba5f4SPaul Zimmerman 		else
677197ba5f4SPaul Zimmerman 			/* Need 1 packet for transfer length of 0 */
678197ba5f4SPaul Zimmerman 			num_packets = 1;
679197ba5f4SPaul Zimmerman 
680197ba5f4SPaul Zimmerman 		/* Always program an integral # of packets for IN transfers */
681197ba5f4SPaul Zimmerman 		len = num_packets * chan->max_packet;
682197ba5f4SPaul Zimmerman 	}
683197ba5f4SPaul Zimmerman 
684197ba5f4SPaul Zimmerman 	dma_desc->status = len << HOST_DMA_NBYTES_SHIFT & HOST_DMA_NBYTES_MASK;
685197ba5f4SPaul Zimmerman 	qh->n_bytes[n_desc] = len;
686197ba5f4SPaul Zimmerman 
687197ba5f4SPaul Zimmerman 	if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL &&
688197ba5f4SPaul Zimmerman 	    qtd->control_phase == DWC2_CONTROL_SETUP)
689197ba5f4SPaul Zimmerman 		dma_desc->status |= HOST_DMA_SUP;
690197ba5f4SPaul Zimmerman 
691197ba5f4SPaul Zimmerman 	dma_desc->buf = (u32)chan->xfer_dma;
692197ba5f4SPaul Zimmerman 
69395105a99SGregory Herrero 	dma_sync_single_for_device(hsotg->dev,
69495105a99SGregory Herrero 				   qh->desc_list_dma +
695ec703251SVahram Aharonyan 				   (n_desc * sizeof(struct dwc2_dma_desc)),
696ec703251SVahram Aharonyan 				   sizeof(struct dwc2_dma_desc),
69795105a99SGregory Herrero 				   DMA_TO_DEVICE);
69895105a99SGregory Herrero 
699197ba5f4SPaul Zimmerman 	/*
700197ba5f4SPaul Zimmerman 	 * Last (or only) descriptor of IN transfer with actual size less
701197ba5f4SPaul Zimmerman 	 * than MaxPacket
702197ba5f4SPaul Zimmerman 	 */
703197ba5f4SPaul Zimmerman 	if (len > chan->xfer_len) {
704197ba5f4SPaul Zimmerman 		chan->xfer_len = 0;
705197ba5f4SPaul Zimmerman 	} else {
706197ba5f4SPaul Zimmerman 		chan->xfer_dma += len;
707197ba5f4SPaul Zimmerman 		chan->xfer_len -= len;
708197ba5f4SPaul Zimmerman 	}
709197ba5f4SPaul Zimmerman }
710197ba5f4SPaul Zimmerman 
dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)711197ba5f4SPaul Zimmerman static void dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg *hsotg,
712197ba5f4SPaul Zimmerman 					struct dwc2_qh *qh)
713197ba5f4SPaul Zimmerman {
714197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
715197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan = qh->channel;
716197ba5f4SPaul Zimmerman 	int n_desc = 0;
717197ba5f4SPaul Zimmerman 
718197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s(): qh=%p dma=%08lx len=%d\n", __func__, qh,
719197ba5f4SPaul Zimmerman 		 (unsigned long)chan->xfer_dma, chan->xfer_len);
720197ba5f4SPaul Zimmerman 
721197ba5f4SPaul Zimmerman 	/*
722197ba5f4SPaul Zimmerman 	 * Start with chan->xfer_dma initialized in assign_and_init_hc(), then
723197ba5f4SPaul Zimmerman 	 * if SG transfer consists of multiple URBs, this pointer is re-assigned
724197ba5f4SPaul Zimmerman 	 * to the buffer of the currently processed QTD. For non-SG request
725197ba5f4SPaul Zimmerman 	 * there is always one QTD active.
726197ba5f4SPaul Zimmerman 	 */
727197ba5f4SPaul Zimmerman 
728197ba5f4SPaul Zimmerman 	list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
729197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "qtd=%p\n", qtd);
730197ba5f4SPaul Zimmerman 
731197ba5f4SPaul Zimmerman 		if (n_desc) {
732197ba5f4SPaul Zimmerman 			/* SG request - more than 1 QTD */
733197ba5f4SPaul Zimmerman 			chan->xfer_dma = qtd->urb->dma +
734197ba5f4SPaul Zimmerman 					qtd->urb->actual_length;
735197ba5f4SPaul Zimmerman 			chan->xfer_len = qtd->urb->length -
736197ba5f4SPaul Zimmerman 					qtd->urb->actual_length;
737197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "buf=%08lx len=%d\n",
738197ba5f4SPaul Zimmerman 				 (unsigned long)chan->xfer_dma, chan->xfer_len);
739197ba5f4SPaul Zimmerman 		}
740197ba5f4SPaul Zimmerman 
741197ba5f4SPaul Zimmerman 		qtd->n_desc = 0;
742197ba5f4SPaul Zimmerman 		do {
743197ba5f4SPaul Zimmerman 			if (n_desc > 1) {
744197ba5f4SPaul Zimmerman 				qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
745197ba5f4SPaul Zimmerman 				dev_vdbg(hsotg->dev,
746197ba5f4SPaul Zimmerman 					 "set A bit in desc %d (%p)\n",
747197ba5f4SPaul Zimmerman 					 n_desc - 1,
748197ba5f4SPaul Zimmerman 					 &qh->desc_list[n_desc - 1]);
74995105a99SGregory Herrero 				dma_sync_single_for_device(hsotg->dev,
75095105a99SGregory Herrero 							   qh->desc_list_dma +
75195105a99SGregory Herrero 					((n_desc - 1) *
752ec703251SVahram Aharonyan 					sizeof(struct dwc2_dma_desc)),
753ec703251SVahram Aharonyan 					sizeof(struct dwc2_dma_desc),
75495105a99SGregory Herrero 					DMA_TO_DEVICE);
755197ba5f4SPaul Zimmerman 			}
756197ba5f4SPaul Zimmerman 			dwc2_fill_host_dma_desc(hsotg, chan, qtd, qh, n_desc);
757197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev,
758197ba5f4SPaul Zimmerman 				 "desc %d (%p) buf=%08x status=%08x\n",
759197ba5f4SPaul Zimmerman 				 n_desc, &qh->desc_list[n_desc],
760197ba5f4SPaul Zimmerman 				 qh->desc_list[n_desc].buf,
761197ba5f4SPaul Zimmerman 				 qh->desc_list[n_desc].status);
762197ba5f4SPaul Zimmerman 			qtd->n_desc++;
763197ba5f4SPaul Zimmerman 			n_desc++;
764197ba5f4SPaul Zimmerman 		} while (chan->xfer_len > 0 &&
765197ba5f4SPaul Zimmerman 			 n_desc != MAX_DMA_DESC_NUM_GENERIC);
766197ba5f4SPaul Zimmerman 
767197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "n_desc=%d\n", n_desc);
768197ba5f4SPaul Zimmerman 		qtd->in_process = 1;
769197ba5f4SPaul Zimmerman 		if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL)
770197ba5f4SPaul Zimmerman 			break;
771197ba5f4SPaul Zimmerman 		if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
772197ba5f4SPaul Zimmerman 			break;
773197ba5f4SPaul Zimmerman 	}
774197ba5f4SPaul Zimmerman 
775197ba5f4SPaul Zimmerman 	if (n_desc) {
776197ba5f4SPaul Zimmerman 		qh->desc_list[n_desc - 1].status |=
777197ba5f4SPaul Zimmerman 				HOST_DMA_IOC | HOST_DMA_EOL | HOST_DMA_A;
778197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "set IOC/EOL/A bits in desc %d (%p)\n",
779197ba5f4SPaul Zimmerman 			 n_desc - 1, &qh->desc_list[n_desc - 1]);
78095105a99SGregory Herrero 		dma_sync_single_for_device(hsotg->dev,
78195105a99SGregory Herrero 					   qh->desc_list_dma + (n_desc - 1) *
782ec703251SVahram Aharonyan 					   sizeof(struct dwc2_dma_desc),
783ec703251SVahram Aharonyan 					   sizeof(struct dwc2_dma_desc),
78495105a99SGregory Herrero 					   DMA_TO_DEVICE);
785197ba5f4SPaul Zimmerman 		if (n_desc > 1) {
786197ba5f4SPaul Zimmerman 			qh->desc_list[0].status |= HOST_DMA_A;
787197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "set A bit in desc 0 (%p)\n",
788197ba5f4SPaul Zimmerman 				 &qh->desc_list[0]);
78995105a99SGregory Herrero 			dma_sync_single_for_device(hsotg->dev,
79095105a99SGregory Herrero 						   qh->desc_list_dma,
791ec703251SVahram Aharonyan 					sizeof(struct dwc2_dma_desc),
79295105a99SGregory Herrero 					DMA_TO_DEVICE);
793197ba5f4SPaul Zimmerman 		}
794197ba5f4SPaul Zimmerman 		chan->ntd = n_desc;
795197ba5f4SPaul Zimmerman 	}
796197ba5f4SPaul Zimmerman }
797197ba5f4SPaul Zimmerman 
798197ba5f4SPaul Zimmerman /**
799197ba5f4SPaul Zimmerman  * dwc2_hcd_start_xfer_ddma() - Starts a transfer in Descriptor DMA mode
800197ba5f4SPaul Zimmerman  *
801197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure for the DWC OTG controller
802197ba5f4SPaul Zimmerman  * @qh:    The QH to init
803197ba5f4SPaul Zimmerman  *
804197ba5f4SPaul Zimmerman  * Return: 0 if successful, negative error code otherwise
805197ba5f4SPaul Zimmerman  *
806197ba5f4SPaul Zimmerman  * For Control and Bulk endpoints, initializes descriptor list and starts the
807197ba5f4SPaul Zimmerman  * transfer. For Interrupt and Isochronous endpoints, initializes descriptor
808197ba5f4SPaul Zimmerman  * list then updates FrameList, marking appropriate entries as active.
809197ba5f4SPaul Zimmerman  *
810197ba5f4SPaul Zimmerman  * For Isochronous endpoints the starting descriptor index is calculated based
811197ba5f4SPaul Zimmerman  * on the scheduled frame, but only on the first transfer descriptor within a
812197ba5f4SPaul Zimmerman  * session. Then the transfer is started via enabling the channel.
813197ba5f4SPaul Zimmerman  *
814197ba5f4SPaul Zimmerman  * For Isochronous endpoints the channel is not halted on XferComplete
815197ba5f4SPaul Zimmerman  * interrupt so remains assigned to the endpoint(QH) until session is done.
816197ba5f4SPaul Zimmerman  */
dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)817197ba5f4SPaul Zimmerman void dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
818197ba5f4SPaul Zimmerman {
819197ba5f4SPaul Zimmerman 	/* Channel is already assigned */
820197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan = qh->channel;
821197ba5f4SPaul Zimmerman 	u16 skip_frames = 0;
822197ba5f4SPaul Zimmerman 
823197ba5f4SPaul Zimmerman 	switch (chan->ep_type) {
824197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_CONTROL:
825197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_BULK:
826197ba5f4SPaul Zimmerman 		dwc2_init_non_isoc_dma_desc(hsotg, qh);
827197ba5f4SPaul Zimmerman 		dwc2_hc_start_transfer_ddma(hsotg, chan);
828197ba5f4SPaul Zimmerman 		break;
829197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_INT:
830197ba5f4SPaul Zimmerman 		dwc2_init_non_isoc_dma_desc(hsotg, qh);
831197ba5f4SPaul Zimmerman 		dwc2_update_frame_list(hsotg, qh, 1);
832197ba5f4SPaul Zimmerman 		dwc2_hc_start_transfer_ddma(hsotg, chan);
833197ba5f4SPaul Zimmerman 		break;
834197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_ISOC:
835197ba5f4SPaul Zimmerman 		if (!qh->ntd)
836197ba5f4SPaul Zimmerman 			skip_frames = dwc2_recalc_initial_desc_idx(hsotg, qh);
837197ba5f4SPaul Zimmerman 		dwc2_init_isoc_dma_desc(hsotg, qh, skip_frames);
838197ba5f4SPaul Zimmerman 
839197ba5f4SPaul Zimmerman 		if (!chan->xfer_started) {
840197ba5f4SPaul Zimmerman 			dwc2_update_frame_list(hsotg, qh, 1);
841197ba5f4SPaul Zimmerman 
842197ba5f4SPaul Zimmerman 			/*
843197ba5f4SPaul Zimmerman 			 * Always set to max, instead of actual size. Otherwise
844197ba5f4SPaul Zimmerman 			 * ntd will be changed with channel being enabled. Not
845197ba5f4SPaul Zimmerman 			 * recommended.
846197ba5f4SPaul Zimmerman 			 */
847197ba5f4SPaul Zimmerman 			chan->ntd = dwc2_max_desc_num(qh);
848197ba5f4SPaul Zimmerman 
849197ba5f4SPaul Zimmerman 			/* Enable channel only once for ISOC */
850197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer_ddma(hsotg, chan);
851197ba5f4SPaul Zimmerman 		}
852197ba5f4SPaul Zimmerman 
853197ba5f4SPaul Zimmerman 		break;
854197ba5f4SPaul Zimmerman 	default:
855197ba5f4SPaul Zimmerman 		break;
856197ba5f4SPaul Zimmerman 	}
857197ba5f4SPaul Zimmerman }
858197ba5f4SPaul Zimmerman 
859197ba5f4SPaul Zimmerman #define DWC2_CMPL_DONE		1
860197ba5f4SPaul Zimmerman #define DWC2_CMPL_STOP		2
861197ba5f4SPaul Zimmerman 
dwc2_cmpl_host_isoc_dma_desc(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,struct dwc2_qtd * qtd,struct dwc2_qh * qh,u16 idx)862197ba5f4SPaul Zimmerman static int dwc2_cmpl_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
863197ba5f4SPaul Zimmerman 					struct dwc2_host_chan *chan,
864197ba5f4SPaul Zimmerman 					struct dwc2_qtd *qtd,
865197ba5f4SPaul Zimmerman 					struct dwc2_qh *qh, u16 idx)
866197ba5f4SPaul Zimmerman {
867ec703251SVahram Aharonyan 	struct dwc2_dma_desc *dma_desc;
868197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
8698d310e5dSMinas Harutyunyan 	u16 frame_desc_idx;
870*8a139fa4SMinas Harutyunyan 	struct urb *usb_urb;
871197ba5f4SPaul Zimmerman 	u16 remain = 0;
872197ba5f4SPaul Zimmerman 	int rc = 0;
873197ba5f4SPaul Zimmerman 
874197ba5f4SPaul Zimmerman 	if (!qtd->urb)
875197ba5f4SPaul Zimmerman 		return -EINVAL;
876197ba5f4SPaul Zimmerman 
877*8a139fa4SMinas Harutyunyan 	usb_urb = qtd->urb->priv;
878*8a139fa4SMinas Harutyunyan 
87995105a99SGregory Herrero 	dma_sync_single_for_cpu(hsotg->dev, qh->desc_list_dma + (idx *
880ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc)),
881ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc),
88295105a99SGregory Herrero 				DMA_FROM_DEVICE);
88395105a99SGregory Herrero 
88495105a99SGregory Herrero 	dma_desc = &qh->desc_list[idx];
8858d310e5dSMinas Harutyunyan 	frame_desc_idx = (idx - qtd->isoc_td_first) & (usb_urb->number_of_packets - 1);
88695105a99SGregory Herrero 
8878d310e5dSMinas Harutyunyan 	frame_desc = &qtd->urb->iso_descs[frame_desc_idx];
8888d310e5dSMinas Harutyunyan 	if (idx == qtd->isoc_td_first)
8898d310e5dSMinas Harutyunyan 		usb_urb->start_frame = dwc2_hcd_get_frame_number(hsotg);
890197ba5f4SPaul Zimmerman 	dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
891197ba5f4SPaul Zimmerman 	if (chan->ep_is_in)
892197ba5f4SPaul Zimmerman 		remain = (dma_desc->status & HOST_DMA_ISOC_NBYTES_MASK) >>
893197ba5f4SPaul Zimmerman 			 HOST_DMA_ISOC_NBYTES_SHIFT;
894197ba5f4SPaul Zimmerman 
895197ba5f4SPaul Zimmerman 	if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
896197ba5f4SPaul Zimmerman 		/*
897197ba5f4SPaul Zimmerman 		 * XactError, or unable to complete all the transactions
898197ba5f4SPaul Zimmerman 		 * in the scheduled micro-frame/frame, both indicated by
899197ba5f4SPaul Zimmerman 		 * HOST_DMA_STS_PKTERR
900197ba5f4SPaul Zimmerman 		 */
901197ba5f4SPaul Zimmerman 		qtd->urb->error_count++;
902197ba5f4SPaul Zimmerman 		frame_desc->actual_length = qh->n_bytes[idx] - remain;
903197ba5f4SPaul Zimmerman 		frame_desc->status = -EPROTO;
904197ba5f4SPaul Zimmerman 	} else {
905197ba5f4SPaul Zimmerman 		/* Success */
906197ba5f4SPaul Zimmerman 		frame_desc->actual_length = qh->n_bytes[idx] - remain;
907197ba5f4SPaul Zimmerman 		frame_desc->status = 0;
908197ba5f4SPaul Zimmerman 	}
909197ba5f4SPaul Zimmerman 
9108d310e5dSMinas Harutyunyan 	if (++qtd->isoc_frame_index == usb_urb->number_of_packets) {
911197ba5f4SPaul Zimmerman 		/*
912197ba5f4SPaul Zimmerman 		 * urb->status is not used for isoc transfers here. The
913197ba5f4SPaul Zimmerman 		 * individual frame_desc status are used instead.
914197ba5f4SPaul Zimmerman 		 */
915197ba5f4SPaul Zimmerman 		dwc2_host_complete(hsotg, qtd, 0);
916197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
917197ba5f4SPaul Zimmerman 
918197ba5f4SPaul Zimmerman 		/*
919197ba5f4SPaul Zimmerman 		 * This check is necessary because urb_dequeue can be called
920197ba5f4SPaul Zimmerman 		 * from urb complete callback (sound driver for example). All
921197ba5f4SPaul Zimmerman 		 * pending URBs are dequeued there, so no need for further
922197ba5f4SPaul Zimmerman 		 * processing.
923197ba5f4SPaul Zimmerman 		 */
924197ba5f4SPaul Zimmerman 		if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE)
925197ba5f4SPaul Zimmerman 			return -1;
926197ba5f4SPaul Zimmerman 		rc = DWC2_CMPL_DONE;
927197ba5f4SPaul Zimmerman 	}
928197ba5f4SPaul Zimmerman 
929197ba5f4SPaul Zimmerman 	qh->ntd--;
930197ba5f4SPaul Zimmerman 
931197ba5f4SPaul Zimmerman 	/* Stop if IOC requested descriptor reached */
932197ba5f4SPaul Zimmerman 	if (dma_desc->status & HOST_DMA_IOC)
933197ba5f4SPaul Zimmerman 		rc = DWC2_CMPL_STOP;
934197ba5f4SPaul Zimmerman 
935197ba5f4SPaul Zimmerman 	return rc;
936197ba5f4SPaul Zimmerman }
937197ba5f4SPaul Zimmerman 
dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,enum dwc2_halt_status halt_status)938197ba5f4SPaul Zimmerman static void dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
939197ba5f4SPaul Zimmerman 					 struct dwc2_host_chan *chan,
940197ba5f4SPaul Zimmerman 					 enum dwc2_halt_status halt_status)
941197ba5f4SPaul Zimmerman {
942197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
943197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
944197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
945197ba5f4SPaul Zimmerman 	u16 idx;
946197ba5f4SPaul Zimmerman 	int rc;
947197ba5f4SPaul Zimmerman 
948197ba5f4SPaul Zimmerman 	qh = chan->qh;
949197ba5f4SPaul Zimmerman 	idx = qh->td_first;
950197ba5f4SPaul Zimmerman 
951197ba5f4SPaul Zimmerman 	if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
952197ba5f4SPaul Zimmerman 		list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
953197ba5f4SPaul Zimmerman 			qtd->in_process = 0;
954197ba5f4SPaul Zimmerman 		return;
955197ba5f4SPaul Zimmerman 	}
956197ba5f4SPaul Zimmerman 
957197ba5f4SPaul Zimmerman 	if (halt_status == DWC2_HC_XFER_AHB_ERR ||
958197ba5f4SPaul Zimmerman 	    halt_status == DWC2_HC_XFER_BABBLE_ERR) {
959197ba5f4SPaul Zimmerman 		/*
960197ba5f4SPaul Zimmerman 		 * Channel is halted in these error cases, considered as serious
961197ba5f4SPaul Zimmerman 		 * issues.
962197ba5f4SPaul Zimmerman 		 * Complete all URBs marking all frames as failed, irrespective
963197ba5f4SPaul Zimmerman 		 * whether some of the descriptors (frames) succeeded or not.
964197ba5f4SPaul Zimmerman 		 * Pass error code to completion routine as well, to update
965197ba5f4SPaul Zimmerman 		 * urb->status, some of class drivers might use it to stop
966197ba5f4SPaul Zimmerman 		 * queing transfer requests.
967197ba5f4SPaul Zimmerman 		 */
968197ba5f4SPaul Zimmerman 		int err = halt_status == DWC2_HC_XFER_AHB_ERR ?
969197ba5f4SPaul Zimmerman 			  -EIO : -EOVERFLOW;
970197ba5f4SPaul Zimmerman 
971197ba5f4SPaul Zimmerman 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
972197ba5f4SPaul Zimmerman 					 qtd_list_entry) {
973197ba5f4SPaul Zimmerman 			if (qtd->urb) {
974197ba5f4SPaul Zimmerman 				for (idx = 0; idx < qtd->urb->packet_count;
975197ba5f4SPaul Zimmerman 				     idx++) {
976197ba5f4SPaul Zimmerman 					frame_desc = &qtd->urb->iso_descs[idx];
977197ba5f4SPaul Zimmerman 					frame_desc->status = err;
978197ba5f4SPaul Zimmerman 				}
979197ba5f4SPaul Zimmerman 
980197ba5f4SPaul Zimmerman 				dwc2_host_complete(hsotg, qtd, err);
981197ba5f4SPaul Zimmerman 			}
982197ba5f4SPaul Zimmerman 
983197ba5f4SPaul Zimmerman 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
984197ba5f4SPaul Zimmerman 		}
985197ba5f4SPaul Zimmerman 
986197ba5f4SPaul Zimmerman 		return;
987197ba5f4SPaul Zimmerman 	}
988197ba5f4SPaul Zimmerman 
989197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
990197ba5f4SPaul Zimmerman 		if (!qtd->in_process)
991197ba5f4SPaul Zimmerman 			break;
992762d3a1aSGregory Herrero 
993762d3a1aSGregory Herrero 		/*
994762d3a1aSGregory Herrero 		 * Ensure idx corresponds to descriptor where first urb of this
995762d3a1aSGregory Herrero 		 * qtd was added. In fact, during isoc desc init, dwc2 may skip
996762d3a1aSGregory Herrero 		 * an index if current frame number is already over this index.
997762d3a1aSGregory Herrero 		 */
998762d3a1aSGregory Herrero 		if (idx != qtd->isoc_td_first) {
999762d3a1aSGregory Herrero 			dev_vdbg(hsotg->dev,
1000762d3a1aSGregory Herrero 				 "try to complete %d instead of %d\n",
1001762d3a1aSGregory Herrero 				 idx, qtd->isoc_td_first);
1002762d3a1aSGregory Herrero 			idx = qtd->isoc_td_first;
1003762d3a1aSGregory Herrero 		}
1004762d3a1aSGregory Herrero 
1005197ba5f4SPaul Zimmerman 		do {
1006762d3a1aSGregory Herrero 			struct dwc2_qtd *qtd_next;
1007762d3a1aSGregory Herrero 			u16 cur_idx;
1008762d3a1aSGregory Herrero 
1009197ba5f4SPaul Zimmerman 			rc = dwc2_cmpl_host_isoc_dma_desc(hsotg, chan, qtd, qh,
1010197ba5f4SPaul Zimmerman 							  idx);
1011197ba5f4SPaul Zimmerman 			if (rc < 0)
1012197ba5f4SPaul Zimmerman 				return;
1013ced9eee1SDouglas Anderson 			idx = dwc2_desclist_idx_inc(idx, qh->host_interval,
1014197ba5f4SPaul Zimmerman 						    chan->speed);
10158d310e5dSMinas Harutyunyan 			if (rc == 0)
1016762d3a1aSGregory Herrero 				continue;
1017762d3a1aSGregory Herrero 
10188d310e5dSMinas Harutyunyan 			if (rc == DWC2_CMPL_DONE || rc == DWC2_CMPL_STOP)
10198d310e5dSMinas Harutyunyan 				goto stop_scan;
1020762d3a1aSGregory Herrero 
1021762d3a1aSGregory Herrero 			/* rc == DWC2_CMPL_STOP */
1022762d3a1aSGregory Herrero 
1023ced9eee1SDouglas Anderson 			if (qh->host_interval >= 32)
1024762d3a1aSGregory Herrero 				goto stop_scan;
1025762d3a1aSGregory Herrero 
1026762d3a1aSGregory Herrero 			qh->td_first = idx;
1027762d3a1aSGregory Herrero 			cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
1028762d3a1aSGregory Herrero 			qtd_next = list_first_entry(&qh->qtd_list,
1029762d3a1aSGregory Herrero 						    struct dwc2_qtd,
1030762d3a1aSGregory Herrero 						    qtd_list_entry);
1031762d3a1aSGregory Herrero 			if (dwc2_frame_idx_num_gt(cur_idx,
1032762d3a1aSGregory Herrero 						  qtd_next->isoc_td_last))
1033762d3a1aSGregory Herrero 				break;
1034762d3a1aSGregory Herrero 
1035762d3a1aSGregory Herrero 			goto stop_scan;
1036762d3a1aSGregory Herrero 
1037197ba5f4SPaul Zimmerman 		} while (idx != qh->td_first);
1038197ba5f4SPaul Zimmerman 	}
1039197ba5f4SPaul Zimmerman 
1040197ba5f4SPaul Zimmerman stop_scan:
1041197ba5f4SPaul Zimmerman 	qh->td_first = idx;
1042197ba5f4SPaul Zimmerman }
1043197ba5f4SPaul Zimmerman 
dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,struct dwc2_qtd * qtd,struct dwc2_dma_desc * dma_desc,enum dwc2_halt_status halt_status,u32 n_bytes,int * xfer_done)1044197ba5f4SPaul Zimmerman static int dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg *hsotg,
1045197ba5f4SPaul Zimmerman 					       struct dwc2_host_chan *chan,
1046197ba5f4SPaul Zimmerman 					struct dwc2_qtd *qtd,
1047ec703251SVahram Aharonyan 					struct dwc2_dma_desc *dma_desc,
1048197ba5f4SPaul Zimmerman 					enum dwc2_halt_status halt_status,
1049197ba5f4SPaul Zimmerman 					u32 n_bytes, int *xfer_done)
1050197ba5f4SPaul Zimmerman {
1051197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb = qtd->urb;
1052197ba5f4SPaul Zimmerman 	u16 remain = 0;
1053197ba5f4SPaul Zimmerman 
1054197ba5f4SPaul Zimmerman 	if (chan->ep_is_in)
1055197ba5f4SPaul Zimmerman 		remain = (dma_desc->status & HOST_DMA_NBYTES_MASK) >>
1056197ba5f4SPaul Zimmerman 			 HOST_DMA_NBYTES_SHIFT;
1057197ba5f4SPaul Zimmerman 
1058197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "remain=%d dwc2_urb=%p\n", remain, urb);
1059197ba5f4SPaul Zimmerman 
1060197ba5f4SPaul Zimmerman 	if (halt_status == DWC2_HC_XFER_AHB_ERR) {
1061197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "EIO\n");
1062197ba5f4SPaul Zimmerman 		urb->status = -EIO;
1063197ba5f4SPaul Zimmerman 		return 1;
1064197ba5f4SPaul Zimmerman 	}
1065197ba5f4SPaul Zimmerman 
1066197ba5f4SPaul Zimmerman 	if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
1067197ba5f4SPaul Zimmerman 		switch (halt_status) {
1068197ba5f4SPaul Zimmerman 		case DWC2_HC_XFER_STALL:
1069197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "Stall\n");
1070197ba5f4SPaul Zimmerman 			urb->status = -EPIPE;
1071197ba5f4SPaul Zimmerman 			break;
1072197ba5f4SPaul Zimmerman 		case DWC2_HC_XFER_BABBLE_ERR:
1073197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "Babble\n");
1074197ba5f4SPaul Zimmerman 			urb->status = -EOVERFLOW;
1075197ba5f4SPaul Zimmerman 			break;
1076197ba5f4SPaul Zimmerman 		case DWC2_HC_XFER_XACT_ERR:
1077197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "XactErr\n");
1078197ba5f4SPaul Zimmerman 			urb->status = -EPROTO;
1079197ba5f4SPaul Zimmerman 			break;
1080197ba5f4SPaul Zimmerman 		default:
1081197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
1082197ba5f4SPaul Zimmerman 				"%s: Unhandled descriptor error status (%d)\n",
1083197ba5f4SPaul Zimmerman 				__func__, halt_status);
1084197ba5f4SPaul Zimmerman 			break;
1085197ba5f4SPaul Zimmerman 		}
1086197ba5f4SPaul Zimmerman 		return 1;
1087197ba5f4SPaul Zimmerman 	}
1088197ba5f4SPaul Zimmerman 
1089197ba5f4SPaul Zimmerman 	if (dma_desc->status & HOST_DMA_A) {
1090197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
1091197ba5f4SPaul Zimmerman 			 "Active descriptor encountered on channel %d\n",
1092197ba5f4SPaul Zimmerman 			 chan->hc_num);
1093197ba5f4SPaul Zimmerman 		return 0;
1094197ba5f4SPaul Zimmerman 	}
1095197ba5f4SPaul Zimmerman 
1096197ba5f4SPaul Zimmerman 	if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1097197ba5f4SPaul Zimmerman 		if (qtd->control_phase == DWC2_CONTROL_DATA) {
1098197ba5f4SPaul Zimmerman 			urb->actual_length += n_bytes - remain;
1099197ba5f4SPaul Zimmerman 			if (remain || urb->actual_length >= urb->length) {
1100197ba5f4SPaul Zimmerman 				/*
1101197ba5f4SPaul Zimmerman 				 * For Control Data stage do not set urb->status
1102197ba5f4SPaul Zimmerman 				 * to 0, to prevent URB callback. Set it when
1103197ba5f4SPaul Zimmerman 				 * Status phase is done. See below.
1104197ba5f4SPaul Zimmerman 				 */
1105197ba5f4SPaul Zimmerman 				*xfer_done = 1;
1106197ba5f4SPaul Zimmerman 			}
1107197ba5f4SPaul Zimmerman 		} else if (qtd->control_phase == DWC2_CONTROL_STATUS) {
1108197ba5f4SPaul Zimmerman 			urb->status = 0;
1109197ba5f4SPaul Zimmerman 			*xfer_done = 1;
1110197ba5f4SPaul Zimmerman 		}
1111197ba5f4SPaul Zimmerman 		/* No handling for SETUP stage */
1112197ba5f4SPaul Zimmerman 	} else {
1113197ba5f4SPaul Zimmerman 		/* BULK and INTR */
1114197ba5f4SPaul Zimmerman 		urb->actual_length += n_bytes - remain;
1115197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "length=%d actual=%d\n", urb->length,
1116197ba5f4SPaul Zimmerman 			 urb->actual_length);
1117197ba5f4SPaul Zimmerman 		if (remain || urb->actual_length >= urb->length) {
1118197ba5f4SPaul Zimmerman 			urb->status = 0;
1119197ba5f4SPaul Zimmerman 			*xfer_done = 1;
1120197ba5f4SPaul Zimmerman 		}
1121197ba5f4SPaul Zimmerman 	}
1122197ba5f4SPaul Zimmerman 
1123197ba5f4SPaul Zimmerman 	return 0;
1124197ba5f4SPaul Zimmerman }
1125197ba5f4SPaul Zimmerman 
dwc2_process_non_isoc_desc(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,int chnum,struct dwc2_qtd * qtd,int desc_num,enum dwc2_halt_status halt_status,int * xfer_done)1126197ba5f4SPaul Zimmerman static int dwc2_process_non_isoc_desc(struct dwc2_hsotg *hsotg,
1127197ba5f4SPaul Zimmerman 				      struct dwc2_host_chan *chan,
1128197ba5f4SPaul Zimmerman 				      int chnum, struct dwc2_qtd *qtd,
1129197ba5f4SPaul Zimmerman 				      int desc_num,
1130197ba5f4SPaul Zimmerman 				      enum dwc2_halt_status halt_status,
1131197ba5f4SPaul Zimmerman 				      int *xfer_done)
1132197ba5f4SPaul Zimmerman {
1133197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = chan->qh;
1134197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb = qtd->urb;
1135ec703251SVahram Aharonyan 	struct dwc2_dma_desc *dma_desc;
1136197ba5f4SPaul Zimmerman 	u32 n_bytes;
1137197ba5f4SPaul Zimmerman 	int failed;
1138197ba5f4SPaul Zimmerman 
1139197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
1140197ba5f4SPaul Zimmerman 
1141197ba5f4SPaul Zimmerman 	if (!urb)
1142197ba5f4SPaul Zimmerman 		return -EINVAL;
1143197ba5f4SPaul Zimmerman 
114495105a99SGregory Herrero 	dma_sync_single_for_cpu(hsotg->dev,
114595105a99SGregory Herrero 				qh->desc_list_dma + (desc_num *
1146ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc)),
1147ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc),
114895105a99SGregory Herrero 				DMA_FROM_DEVICE);
114995105a99SGregory Herrero 
1150197ba5f4SPaul Zimmerman 	dma_desc = &qh->desc_list[desc_num];
1151197ba5f4SPaul Zimmerman 	n_bytes = qh->n_bytes[desc_num];
1152197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev,
1153197ba5f4SPaul Zimmerman 		 "qtd=%p dwc2_urb=%p desc_num=%d desc=%p n_bytes=%d\n",
1154197ba5f4SPaul Zimmerman 		 qtd, urb, desc_num, dma_desc, n_bytes);
1155197ba5f4SPaul Zimmerman 	failed = dwc2_update_non_isoc_urb_state_ddma(hsotg, chan, qtd, dma_desc,
1156197ba5f4SPaul Zimmerman 						     halt_status, n_bytes,
1157197ba5f4SPaul Zimmerman 						     xfer_done);
11583142a16bSVardan Mikayelyan 	if (failed || (*xfer_done && urb->status != -EINPROGRESS)) {
1159197ba5f4SPaul Zimmerman 		dwc2_host_complete(hsotg, qtd, urb->status);
1160197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
11613142a16bSVardan Mikayelyan 		dev_vdbg(hsotg->dev, "failed=%1x xfer_done=%1x\n",
11623142a16bSVardan Mikayelyan 			 failed, *xfer_done);
1163197ba5f4SPaul Zimmerman 		return failed;
1164197ba5f4SPaul Zimmerman 	}
1165197ba5f4SPaul Zimmerman 
1166197ba5f4SPaul Zimmerman 	if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1167197ba5f4SPaul Zimmerman 		switch (qtd->control_phase) {
1168197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_SETUP:
1169197ba5f4SPaul Zimmerman 			if (urb->length > 0)
1170197ba5f4SPaul Zimmerman 				qtd->control_phase = DWC2_CONTROL_DATA;
1171197ba5f4SPaul Zimmerman 			else
1172197ba5f4SPaul Zimmerman 				qtd->control_phase = DWC2_CONTROL_STATUS;
1173197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev,
1174197ba5f4SPaul Zimmerman 				 "  Control setup transaction done\n");
1175197ba5f4SPaul Zimmerman 			break;
1176197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_DATA:
1177197ba5f4SPaul Zimmerman 			if (*xfer_done) {
1178197ba5f4SPaul Zimmerman 				qtd->control_phase = DWC2_CONTROL_STATUS;
1179197ba5f4SPaul Zimmerman 				dev_vdbg(hsotg->dev,
1180197ba5f4SPaul Zimmerman 					 "  Control data transfer done\n");
1181197ba5f4SPaul Zimmerman 			} else if (desc_num + 1 == qtd->n_desc) {
1182197ba5f4SPaul Zimmerman 				/*
1183197ba5f4SPaul Zimmerman 				 * Last descriptor for Control data stage which
1184197ba5f4SPaul Zimmerman 				 * is not completed yet
1185197ba5f4SPaul Zimmerman 				 */
1186197ba5f4SPaul Zimmerman 				dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
1187197ba5f4SPaul Zimmerman 							  qtd);
1188197ba5f4SPaul Zimmerman 			}
1189197ba5f4SPaul Zimmerman 			break;
1190197ba5f4SPaul Zimmerman 		default:
1191197ba5f4SPaul Zimmerman 			break;
1192197ba5f4SPaul Zimmerman 		}
1193197ba5f4SPaul Zimmerman 	}
1194197ba5f4SPaul Zimmerman 
1195197ba5f4SPaul Zimmerman 	return 0;
1196197ba5f4SPaul Zimmerman }
1197197ba5f4SPaul Zimmerman 
dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,int chnum,enum dwc2_halt_status halt_status)1198197ba5f4SPaul Zimmerman static void dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
1199197ba5f4SPaul Zimmerman 					     struct dwc2_host_chan *chan,
1200197ba5f4SPaul Zimmerman 					     int chnum,
1201197ba5f4SPaul Zimmerman 					     enum dwc2_halt_status halt_status)
1202197ba5f4SPaul Zimmerman {
1203197ba5f4SPaul Zimmerman 	struct list_head *qtd_item, *qtd_tmp;
1204197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = chan->qh;
1205197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd = NULL;
1206197ba5f4SPaul Zimmerman 	int xfer_done;
1207197ba5f4SPaul Zimmerman 	int desc_num = 0;
1208197ba5f4SPaul Zimmerman 
1209197ba5f4SPaul Zimmerman 	if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
1210197ba5f4SPaul Zimmerman 		list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
1211197ba5f4SPaul Zimmerman 			qtd->in_process = 0;
1212197ba5f4SPaul Zimmerman 		return;
1213197ba5f4SPaul Zimmerman 	}
1214197ba5f4SPaul Zimmerman 
1215197ba5f4SPaul Zimmerman 	list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
1216197ba5f4SPaul Zimmerman 		int i;
12173142a16bSVardan Mikayelyan 		int qtd_desc_count;
1218197ba5f4SPaul Zimmerman 
1219197ba5f4SPaul Zimmerman 		qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
1220197ba5f4SPaul Zimmerman 		xfer_done = 0;
12213142a16bSVardan Mikayelyan 		qtd_desc_count = qtd->n_desc;
1222197ba5f4SPaul Zimmerman 
12233142a16bSVardan Mikayelyan 		for (i = 0; i < qtd_desc_count; i++) {
1224197ba5f4SPaul Zimmerman 			if (dwc2_process_non_isoc_desc(hsotg, chan, chnum, qtd,
1225197ba5f4SPaul Zimmerman 						       desc_num, halt_status,
12261fc65989SJohn Youn 						       &xfer_done)) {
12271fc65989SJohn Youn 				qtd = NULL;
12283142a16bSVardan Mikayelyan 				goto stop_scan;
12291fc65989SJohn Youn 			}
12303142a16bSVardan Mikayelyan 
1231197ba5f4SPaul Zimmerman 			desc_num++;
1232197ba5f4SPaul Zimmerman 		}
1233197ba5f4SPaul Zimmerman 	}
1234197ba5f4SPaul Zimmerman 
12353142a16bSVardan Mikayelyan stop_scan:
1236197ba5f4SPaul Zimmerman 	if (qh->ep_type != USB_ENDPOINT_XFER_CONTROL) {
1237197ba5f4SPaul Zimmerman 		/*
1238197ba5f4SPaul Zimmerman 		 * Resetting the data toggle for bulk and interrupt endpoints
1239197ba5f4SPaul Zimmerman 		 * in case of stall. See handle_hc_stall_intr().
1240197ba5f4SPaul Zimmerman 		 */
1241197ba5f4SPaul Zimmerman 		if (halt_status == DWC2_HC_XFER_STALL)
1242197ba5f4SPaul Zimmerman 			qh->data_toggle = DWC2_HC_PID_DATA0;
124362943b7dSTang, Jianqiang 		else
12441fc65989SJohn Youn 			dwc2_hcd_save_data_toggle(hsotg, chan, chnum, NULL);
1245197ba5f4SPaul Zimmerman 	}
1246197ba5f4SPaul Zimmerman 
1247197ba5f4SPaul Zimmerman 	if (halt_status == DWC2_HC_XFER_COMPLETE) {
1248197ba5f4SPaul Zimmerman 		if (chan->hcint & HCINTMSK_NYET) {
1249197ba5f4SPaul Zimmerman 			/*
1250197ba5f4SPaul Zimmerman 			 * Got a NYET on the last transaction of the transfer.
1251197ba5f4SPaul Zimmerman 			 * It means that the endpoint should be in the PING
1252197ba5f4SPaul Zimmerman 			 * state at the beginning of the next transfer.
1253197ba5f4SPaul Zimmerman 			 */
1254197ba5f4SPaul Zimmerman 			qh->ping_state = 1;
1255197ba5f4SPaul Zimmerman 		}
1256197ba5f4SPaul Zimmerman 	}
1257197ba5f4SPaul Zimmerman }
1258197ba5f4SPaul Zimmerman 
1259197ba5f4SPaul Zimmerman /**
1260197ba5f4SPaul Zimmerman  * dwc2_hcd_complete_xfer_ddma() - Scans the descriptor list, updates URB's
1261197ba5f4SPaul Zimmerman  * status and calls completion routine for the URB if it's done. Called from
1262197ba5f4SPaul Zimmerman  * interrupt handlers.
1263197ba5f4SPaul Zimmerman  *
1264197ba5f4SPaul Zimmerman  * @hsotg:       The HCD state structure for the DWC OTG controller
1265197ba5f4SPaul Zimmerman  * @chan:        Host channel the transfer is completed on
1266197ba5f4SPaul Zimmerman  * @chnum:       Index of Host channel registers
1267197ba5f4SPaul Zimmerman  * @halt_status: Reason the channel is being halted or just XferComplete
1268197ba5f4SPaul Zimmerman  *               for isochronous transfers
1269197ba5f4SPaul Zimmerman  *
1270197ba5f4SPaul Zimmerman  * Releases the channel to be used by other transfers.
1271197ba5f4SPaul Zimmerman  * In case of Isochronous endpoint the channel is not halted until the end of
1272197ba5f4SPaul Zimmerman  * the session, i.e. QTD list is empty.
1273197ba5f4SPaul Zimmerman  * If periodic channel released the FrameList is updated accordingly.
1274197ba5f4SPaul Zimmerman  * Calls transaction selection routines to activate pending transfers.
1275197ba5f4SPaul Zimmerman  */
dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,int chnum,enum dwc2_halt_status halt_status)1276197ba5f4SPaul Zimmerman void dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg *hsotg,
1277197ba5f4SPaul Zimmerman 				 struct dwc2_host_chan *chan, int chnum,
1278197ba5f4SPaul Zimmerman 				 enum dwc2_halt_status halt_status)
1279197ba5f4SPaul Zimmerman {
1280197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = chan->qh;
1281197ba5f4SPaul Zimmerman 	int continue_isoc_xfer = 0;
1282197ba5f4SPaul Zimmerman 	enum dwc2_transaction_type tr_type;
1283197ba5f4SPaul Zimmerman 
1284197ba5f4SPaul Zimmerman 	if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1285197ba5f4SPaul Zimmerman 		dwc2_complete_isoc_xfer_ddma(hsotg, chan, halt_status);
1286197ba5f4SPaul Zimmerman 
1287197ba5f4SPaul Zimmerman 		/* Release the channel if halted or session completed */
1288197ba5f4SPaul Zimmerman 		if (halt_status != DWC2_HC_XFER_COMPLETE ||
1289197ba5f4SPaul Zimmerman 		    list_empty(&qh->qtd_list)) {
1290c503b381SGregory Herrero 			struct dwc2_qtd *qtd, *qtd_tmp;
1291c503b381SGregory Herrero 
1292c503b381SGregory Herrero 			/*
1293c503b381SGregory Herrero 			 * Kill all remainings QTDs since channel has been
1294c503b381SGregory Herrero 			 * halted.
1295c503b381SGregory Herrero 			 */
1296c503b381SGregory Herrero 			list_for_each_entry_safe(qtd, qtd_tmp,
1297c503b381SGregory Herrero 						 &qh->qtd_list,
1298c503b381SGregory Herrero 						 qtd_list_entry) {
1299c503b381SGregory Herrero 				dwc2_host_complete(hsotg, qtd,
1300c503b381SGregory Herrero 						   -ECONNRESET);
1301c503b381SGregory Herrero 				dwc2_hcd_qtd_unlink_and_free(hsotg,
1302c503b381SGregory Herrero 							     qtd, qh);
1303c503b381SGregory Herrero 			}
1304c503b381SGregory Herrero 
1305197ba5f4SPaul Zimmerman 			/* Halt the channel if session completed */
1306197ba5f4SPaul Zimmerman 			if (halt_status == DWC2_HC_XFER_COMPLETE)
1307197ba5f4SPaul Zimmerman 				dwc2_hc_halt(hsotg, chan, halt_status);
1308197ba5f4SPaul Zimmerman 			dwc2_release_channel_ddma(hsotg, qh);
1309197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_unlink(hsotg, qh);
1310197ba5f4SPaul Zimmerman 		} else {
1311197ba5f4SPaul Zimmerman 			/* Keep in assigned schedule to continue transfer */
131294ef7aeeSDouglas Anderson 			list_move_tail(&qh->qh_list_entry,
1313197ba5f4SPaul Zimmerman 				       &hsotg->periodic_sched_assigned);
1314c503b381SGregory Herrero 			/*
1315c503b381SGregory Herrero 			 * If channel has been halted during giveback of urb
1316c503b381SGregory Herrero 			 * then prevent any new scheduling.
1317c503b381SGregory Herrero 			 */
1318c503b381SGregory Herrero 			if (!chan->halt_status)
1319197ba5f4SPaul Zimmerman 				continue_isoc_xfer = 1;
1320197ba5f4SPaul Zimmerman 		}
1321197ba5f4SPaul Zimmerman 		/*
1322197ba5f4SPaul Zimmerman 		 * Todo: Consider the case when period exceeds FrameList size.
1323197ba5f4SPaul Zimmerman 		 * Frame Rollover interrupt should be used.
1324197ba5f4SPaul Zimmerman 		 */
1325197ba5f4SPaul Zimmerman 	} else {
1326197ba5f4SPaul Zimmerman 		/*
1327197ba5f4SPaul Zimmerman 		 * Scan descriptor list to complete the URB(s), then release
1328197ba5f4SPaul Zimmerman 		 * the channel
1329197ba5f4SPaul Zimmerman 		 */
1330197ba5f4SPaul Zimmerman 		dwc2_complete_non_isoc_xfer_ddma(hsotg, chan, chnum,
1331197ba5f4SPaul Zimmerman 						 halt_status);
1332197ba5f4SPaul Zimmerman 		dwc2_release_channel_ddma(hsotg, qh);
1333197ba5f4SPaul Zimmerman 		dwc2_hcd_qh_unlink(hsotg, qh);
1334197ba5f4SPaul Zimmerman 
1335197ba5f4SPaul Zimmerman 		if (!list_empty(&qh->qtd_list)) {
1336197ba5f4SPaul Zimmerman 			/*
1337197ba5f4SPaul Zimmerman 			 * Add back to inactive non-periodic schedule on normal
1338197ba5f4SPaul Zimmerman 			 * completion
1339197ba5f4SPaul Zimmerman 			 */
1340197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_add(hsotg, qh);
1341197ba5f4SPaul Zimmerman 		}
1342197ba5f4SPaul Zimmerman 	}
1343197ba5f4SPaul Zimmerman 
1344197ba5f4SPaul Zimmerman 	tr_type = dwc2_hcd_select_transactions(hsotg);
1345197ba5f4SPaul Zimmerman 	if (tr_type != DWC2_TRANSACTION_NONE || continue_isoc_xfer) {
1346197ba5f4SPaul Zimmerman 		if (continue_isoc_xfer) {
1347197ba5f4SPaul Zimmerman 			if (tr_type == DWC2_TRANSACTION_NONE)
1348197ba5f4SPaul Zimmerman 				tr_type = DWC2_TRANSACTION_PERIODIC;
1349197ba5f4SPaul Zimmerman 			else if (tr_type == DWC2_TRANSACTION_NON_PERIODIC)
1350197ba5f4SPaul Zimmerman 				tr_type = DWC2_TRANSACTION_ALL;
1351197ba5f4SPaul Zimmerman 		}
1352197ba5f4SPaul Zimmerman 		dwc2_hcd_queue_transactions(hsotg, tr_type);
1353197ba5f4SPaul Zimmerman 	}
1354197ba5f4SPaul Zimmerman }
1355