xref: /openbsd-src/sys/dev/usb/dwc2/dwc2_hcdddma.c (revision a9beb1ed4245313a4b68e10ec33a67c806523ec4)
1*a9beb1edSmglocker /*	$OpenBSD: dwc2_hcdddma.c,v 1.21 2022/09/04 08:42:40 mglocker Exp $	*/
225e434cbSuebayasi /*	$NetBSD: dwc2_hcdddma.c,v 1.6 2014/04/03 06:34:58 skrll Exp $	*/
3578f812dSuebayasi 
4578f812dSuebayasi /*
5578f812dSuebayasi  * hcd_ddma.c - DesignWare HS OTG Controller descriptor DMA routines
6578f812dSuebayasi  *
7578f812dSuebayasi  * Copyright (C) 2004-2013 Synopsys, Inc.
8578f812dSuebayasi  *
9578f812dSuebayasi  * Redistribution and use in source and binary forms, with or without
10578f812dSuebayasi  * modification, are permitted provided that the following conditions
11578f812dSuebayasi  * are met:
12578f812dSuebayasi  * 1. Redistributions of source code must retain the above copyright
13578f812dSuebayasi  *    notice, this list of conditions, and the following disclaimer,
14578f812dSuebayasi  *    without modification.
15578f812dSuebayasi  * 2. Redistributions in binary form must reproduce the above copyright
16578f812dSuebayasi  *    notice, this list of conditions and the following disclaimer in the
17578f812dSuebayasi  *    documentation and/or other materials provided with the distribution.
18578f812dSuebayasi  * 3. The names of the above-listed copyright holders may not be used
19578f812dSuebayasi  *    to endorse or promote products derived from this software without
20578f812dSuebayasi  *    specific prior written permission.
21578f812dSuebayasi  *
22578f812dSuebayasi  * ALTERNATIVELY, this software may be distributed under the terms of the
23578f812dSuebayasi  * GNU General Public License ("GPL") as published by the Free Software
24578f812dSuebayasi  * Foundation; either version 2 of the License, or (at your option) any
25578f812dSuebayasi  * later version.
26578f812dSuebayasi  *
27578f812dSuebayasi  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28578f812dSuebayasi  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29578f812dSuebayasi  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30578f812dSuebayasi  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31578f812dSuebayasi  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32578f812dSuebayasi  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33578f812dSuebayasi  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34578f812dSuebayasi  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35578f812dSuebayasi  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36578f812dSuebayasi  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37578f812dSuebayasi  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38578f812dSuebayasi  */
39578f812dSuebayasi 
40578f812dSuebayasi /*
41578f812dSuebayasi  * This file contains the Descriptor DMA implementation for Host mode
42578f812dSuebayasi  */
43578f812dSuebayasi #include <sys/param.h>
44c95c98b7Sjmatthew #include <sys/systm.h>
45578f812dSuebayasi #include <sys/kernel.h>
46309465dbSuebayasi #include <sys/malloc.h>
47309465dbSuebayasi 
48309465dbSuebayasi #include <machine/bus.h>
49578f812dSuebayasi 
50578f812dSuebayasi #include <dev/usb/usb.h>
51578f812dSuebayasi #include <dev/usb/usbdi.h>
52578f812dSuebayasi #include <dev/usb/usbdivar.h>
53578f812dSuebayasi #include <dev/usb/usb_mem.h>
54578f812dSuebayasi 
55309465dbSuebayasi #include <dev/usb/dwc2/dwc2.h>
56309465dbSuebayasi #include <dev/usb/dwc2/dwc2var.h>
57578f812dSuebayasi 
58309465dbSuebayasi #include <dev/usb/dwc2/dwc2_core.h>
59309465dbSuebayasi #include <dev/usb/dwc2/dwc2_hcd.h>
60578f812dSuebayasi 
dwc2_frame_list_idx(u16 frame)6105c50565Suebayasi STATIC u16 dwc2_frame_list_idx(u16 frame)
62578f812dSuebayasi {
63578f812dSuebayasi 	return frame & (FRLISTEN_64_SIZE - 1);
64578f812dSuebayasi }
65578f812dSuebayasi 
dwc2_desclist_idx_inc(u16 idx,u16 inc,u8 speed)6605c50565Suebayasi STATIC u16 dwc2_desclist_idx_inc(u16 idx, u16 inc, u8 speed)
67578f812dSuebayasi {
68578f812dSuebayasi 	return (idx + inc) &
69578f812dSuebayasi 		((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
70578f812dSuebayasi 		  MAX_DMA_DESC_NUM_GENERIC) - 1);
71578f812dSuebayasi }
72578f812dSuebayasi 
dwc2_desclist_idx_dec(u16 idx,u16 inc,u8 speed)7305c50565Suebayasi STATIC u16 dwc2_desclist_idx_dec(u16 idx, u16 inc, u8 speed)
74578f812dSuebayasi {
75578f812dSuebayasi 	return (idx - inc) &
76578f812dSuebayasi 		((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
77578f812dSuebayasi 		  MAX_DMA_DESC_NUM_GENERIC) - 1);
78578f812dSuebayasi }
79578f812dSuebayasi 
dwc2_max_desc_num(struct dwc2_qh * qh)8005c50565Suebayasi STATIC u16 dwc2_max_desc_num(struct dwc2_qh *qh)
81578f812dSuebayasi {
82578f812dSuebayasi 	return (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
83578f812dSuebayasi 		qh->dev_speed == USB_SPEED_HIGH) ?
84578f812dSuebayasi 		MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC;
85578f812dSuebayasi }
86578f812dSuebayasi 
dwc2_frame_incr_val(struct dwc2_qh * qh)8705c50565Suebayasi STATIC u16 dwc2_frame_incr_val(struct dwc2_qh *qh)
88578f812dSuebayasi {
89578f812dSuebayasi 	return qh->dev_speed == USB_SPEED_HIGH ?
90*a9beb1edSmglocker 	       (qh->host_interval + 8 - 1) / 8 : qh->host_interval;
91578f812dSuebayasi }
92578f812dSuebayasi 
dwc2_desc_list_alloc(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,gfp_t flags)9305c50565Suebayasi STATIC int dwc2_desc_list_alloc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
94578f812dSuebayasi 				gfp_t flags)
95578f812dSuebayasi {
96578f812dSuebayasi 	int err;
97*a9beb1edSmglocker #if 0
98*a9beb1edSmglocker 	struct kmem_cache *desc_cache;
99578f812dSuebayasi 
100*a9beb1edSmglocker 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
101*a9beb1edSmglocker 	    qh->dev_speed == USB_SPEED_HIGH)
102*a9beb1edSmglocker 		desc_cache = hsotg->desc_hsisoc_cache;
103*a9beb1edSmglocker 	else
104*a9beb1edSmglocker 		desc_cache = hsotg->desc_gen_cache;
105*a9beb1edSmglocker #endif
106*a9beb1edSmglocker 
107*a9beb1edSmglocker 	qh->desc_list_sz = sizeof(struct dwc2_dma_desc) *
108d05ae140Smglocker 						dwc2_max_desc_num(qh);
109d05ae140Smglocker 
110d05ae140Smglocker 	err = usb_allocmem(&hsotg->hsotg_sc->sc_bus, qh->desc_list_sz, 0,
111c28d999fSpatrick 	    USB_DMA_COHERENT, &qh->desc_list_usbdma);
112d05ae140Smglocker 	if (err)
113578f812dSuebayasi 		return -ENOMEM;
114578f812dSuebayasi 
115d05ae140Smglocker 	qh->desc_list = KERNADDR(&qh->desc_list_usbdma, 0);
116d05ae140Smglocker 	qh->desc_list_dma = DMAADDR(&qh->desc_list_usbdma, 0);
117578f812dSuebayasi 
1186303db42Smglocker 	qh->n_bytes = malloc(sizeof(u32) * dwc2_max_desc_num(qh), M_USBHC,
119aaade355Suebayasi 	    M_ZERO | M_WAITOK);
120d05ae140Smglocker 	if (!qh->n_bytes) {
121d05ae140Smglocker 		usb_freemem(&hsotg->hsotg_sc->sc_bus, &qh->desc_list_usbdma);
122d05ae140Smglocker 		qh->desc_list = NULL;
123d05ae140Smglocker 		return -ENOMEM;
124d05ae140Smglocker 	}
125d05ae140Smglocker 
126578f812dSuebayasi 	return 0;
127578f812dSuebayasi }
128578f812dSuebayasi 
dwc2_desc_list_free(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)12905c50565Suebayasi STATIC void dwc2_desc_list_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
130578f812dSuebayasi {
131*a9beb1edSmglocker #if 0
132*a9beb1edSmglocker 	struct kmem_cache *desc_cache;
133*a9beb1edSmglocker 
134*a9beb1edSmglocker 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
135*a9beb1edSmglocker 	    qh->dev_speed == USB_SPEED_HIGH)
136*a9beb1edSmglocker 		desc_cache = hsotg->desc_hsisoc_cache;
137*a9beb1edSmglocker 	else
138*a9beb1edSmglocker 		desc_cache = hsotg->desc_gen_cache;
139*a9beb1edSmglocker #endif
140d05ae140Smglocker 
141578f812dSuebayasi 	if (qh->desc_list) {
142578f812dSuebayasi 		usb_freemem(&hsotg->hsotg_sc->sc_bus, &qh->desc_list_usbdma);
143578f812dSuebayasi 		qh->desc_list = NULL;
144578f812dSuebayasi 	}
145578f812dSuebayasi 
1466303db42Smglocker 	free(qh->n_bytes, M_USBHC, sizeof(u32) * dwc2_max_desc_num(qh));
147578f812dSuebayasi 	qh->n_bytes = NULL;
148578f812dSuebayasi }
149578f812dSuebayasi 
dwc2_frame_list_alloc(struct dwc2_hsotg * hsotg,gfp_t mem_flags)15005c50565Suebayasi STATIC int dwc2_frame_list_alloc(struct dwc2_hsotg *hsotg, gfp_t mem_flags)
151578f812dSuebayasi {
152578f812dSuebayasi 	int err;
153578f812dSuebayasi 
154578f812dSuebayasi 	if (hsotg->frame_list)
155578f812dSuebayasi 		return 0;
156578f812dSuebayasi 
157d05ae140Smglocker 	/* XXXNH - pool_cache_t */
158d05ae140Smglocker 	hsotg->frame_list_sz = 4 * FRLISTEN_64_SIZE;
159578f812dSuebayasi 	hsotg->frame_list = NULL;
160d05ae140Smglocker 	err = usb_allocmem(&hsotg->hsotg_sc->sc_bus, hsotg->frame_list_sz,
161c28d999fSpatrick 	    0, USB_DMA_COHERENT, &hsotg->frame_list_usbdma);
162578f812dSuebayasi 	if (!err) {
163578f812dSuebayasi 		hsotg->frame_list = KERNADDR(&hsotg->frame_list_usbdma, 0);
164578f812dSuebayasi 		hsotg->frame_list_dma = DMAADDR(&hsotg->frame_list_usbdma, 0);
165578f812dSuebayasi 	}
166578f812dSuebayasi 
167578f812dSuebayasi 	if (!hsotg->frame_list)
168578f812dSuebayasi 		return -ENOMEM;
169578f812dSuebayasi 
170578f812dSuebayasi 	return 0;
171578f812dSuebayasi }
172578f812dSuebayasi 
dwc2_frame_list_free(struct dwc2_hsotg * hsotg)17305c50565Suebayasi STATIC void dwc2_frame_list_free(struct dwc2_hsotg *hsotg)
174578f812dSuebayasi {
1753215ab50Suebayasi 	struct usb_dma frame_list_usbdma;
176578f812dSuebayasi 	unsigned long flags;
177578f812dSuebayasi 
178578f812dSuebayasi 	spin_lock_irqsave(&hsotg->lock, flags);
179578f812dSuebayasi 
180578f812dSuebayasi 	if (!hsotg->frame_list) {
181578f812dSuebayasi 		spin_unlock_irqrestore(&hsotg->lock, flags);
182578f812dSuebayasi 		return;
183578f812dSuebayasi 	}
184578f812dSuebayasi 
185578f812dSuebayasi 	frame_list_usbdma = hsotg->frame_list_usbdma;
186578f812dSuebayasi 	hsotg->frame_list = NULL;
187578f812dSuebayasi 
188578f812dSuebayasi 	spin_unlock_irqrestore(&hsotg->lock, flags);
189578f812dSuebayasi 
190578f812dSuebayasi 	usb_freemem(&hsotg->hsotg_sc->sc_bus, &frame_list_usbdma);
191578f812dSuebayasi }
192578f812dSuebayasi 
dwc2_per_sched_enable(struct dwc2_hsotg * hsotg,u32 fr_list_en)19305c50565Suebayasi STATIC void dwc2_per_sched_enable(struct dwc2_hsotg *hsotg, u32 fr_list_en)
194578f812dSuebayasi {
195578f812dSuebayasi 	u32 hcfg;
196578f812dSuebayasi 	unsigned long flags;
197578f812dSuebayasi 
198578f812dSuebayasi 	spin_lock_irqsave(&hsotg->lock, flags);
199578f812dSuebayasi 
200*a9beb1edSmglocker 	hcfg = dwc2_readl(hsotg, HCFG);
201578f812dSuebayasi 	if (hcfg & HCFG_PERSCHEDENA) {
202578f812dSuebayasi 		/* already enabled */
203578f812dSuebayasi 		spin_unlock_irqrestore(&hsotg->lock, flags);
204578f812dSuebayasi 		return;
205578f812dSuebayasi 	}
206578f812dSuebayasi 
207*a9beb1edSmglocker 	dwc2_writel(hsotg, hsotg->frame_list_dma, HFLBADDR);
208578f812dSuebayasi 
209578f812dSuebayasi 	hcfg &= ~HCFG_FRLISTEN_MASK;
210578f812dSuebayasi 	hcfg |= fr_list_en | HCFG_PERSCHEDENA;
211578f812dSuebayasi 	dev_vdbg(hsotg->dev, "Enabling Periodic schedule\n");
212*a9beb1edSmglocker 	dwc2_writel(hsotg, hcfg, HCFG);
213578f812dSuebayasi 
214578f812dSuebayasi 	spin_unlock_irqrestore(&hsotg->lock, flags);
215578f812dSuebayasi }
216578f812dSuebayasi 
dwc2_per_sched_disable(struct dwc2_hsotg * hsotg)21705c50565Suebayasi STATIC void dwc2_per_sched_disable(struct dwc2_hsotg *hsotg)
218578f812dSuebayasi {
219578f812dSuebayasi 	u32 hcfg;
220578f812dSuebayasi 	unsigned long flags;
221578f812dSuebayasi 
222578f812dSuebayasi 	spin_lock_irqsave(&hsotg->lock, flags);
223578f812dSuebayasi 
224*a9beb1edSmglocker 	hcfg = dwc2_readl(hsotg, HCFG);
225578f812dSuebayasi 	if (!(hcfg & HCFG_PERSCHEDENA)) {
226578f812dSuebayasi 		/* already disabled */
227578f812dSuebayasi 		spin_unlock_irqrestore(&hsotg->lock, flags);
228578f812dSuebayasi 		return;
229578f812dSuebayasi 	}
230578f812dSuebayasi 
231578f812dSuebayasi 	hcfg &= ~HCFG_PERSCHEDENA;
232578f812dSuebayasi 	dev_vdbg(hsotg->dev, "Disabling Periodic schedule\n");
233*a9beb1edSmglocker 	dwc2_writel(hsotg, hcfg, HCFG);
234578f812dSuebayasi 
235578f812dSuebayasi 	spin_unlock_irqrestore(&hsotg->lock, flags);
236578f812dSuebayasi }
237578f812dSuebayasi 
238578f812dSuebayasi /*
239578f812dSuebayasi  * Activates/Deactivates FrameList entries for the channel based on endpoint
240578f812dSuebayasi  * servicing period
241578f812dSuebayasi  */
dwc2_update_frame_list(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,int enable)24205c50565Suebayasi STATIC void dwc2_update_frame_list(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
243578f812dSuebayasi 				   int enable)
244578f812dSuebayasi {
245578f812dSuebayasi 	struct dwc2_host_chan *chan;
246578f812dSuebayasi 	u16 i, j, inc;
247578f812dSuebayasi 
248578f812dSuebayasi 	if (!hsotg) {
249578f812dSuebayasi 		printf("hsotg = %p\n", hsotg);
250578f812dSuebayasi 		return;
251578f812dSuebayasi 	}
252578f812dSuebayasi 
253578f812dSuebayasi 	if (!qh->channel) {
254578f812dSuebayasi 		dev_err(hsotg->dev, "qh->channel = %p\n", qh->channel);
255578f812dSuebayasi 		return;
256578f812dSuebayasi 	}
257578f812dSuebayasi 
258578f812dSuebayasi 	if (!hsotg->frame_list) {
259578f812dSuebayasi 		dev_err(hsotg->dev, "hsotg->frame_list = %p\n",
260578f812dSuebayasi 			hsotg->frame_list);
261578f812dSuebayasi 		return;
262578f812dSuebayasi 	}
263578f812dSuebayasi 
264578f812dSuebayasi 	chan = qh->channel;
265578f812dSuebayasi 	inc = dwc2_frame_incr_val(qh);
266578f812dSuebayasi 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC)
267*a9beb1edSmglocker 		i = dwc2_frame_list_idx(qh->next_active_frame);
268578f812dSuebayasi 	else
269578f812dSuebayasi 		i = 0;
270578f812dSuebayasi 
271578f812dSuebayasi 	j = i;
272578f812dSuebayasi 	do {
273578f812dSuebayasi 		if (enable)
274578f812dSuebayasi 			hsotg->frame_list[j] |= 1 << chan->hc_num;
275578f812dSuebayasi 		else
276578f812dSuebayasi 			hsotg->frame_list[j] &= ~(1 << chan->hc_num);
277578f812dSuebayasi 		j = (j + inc) & (FRLISTEN_64_SIZE - 1);
278578f812dSuebayasi 	} while (j != i);
279578f812dSuebayasi 
280d05ae140Smglocker 	/*
281d05ae140Smglocker 	 * Sync frame list since controller will access it if periodic
282d05ae140Smglocker 	 * channel is currently enabled.
283d05ae140Smglocker 	 */
284d05ae140Smglocker 	usb_syncmem(&hsotg->frame_list_usbdma, 0, hsotg->frame_list_sz,
285d05ae140Smglocker 	    BUS_DMASYNC_PREWRITE);
286d05ae140Smglocker 
287578f812dSuebayasi 	if (!enable)
288578f812dSuebayasi 		return;
289578f812dSuebayasi 
290578f812dSuebayasi 	chan->schinfo = 0;
291*a9beb1edSmglocker 	if (chan->speed == USB_SPEED_HIGH && qh->host_interval) {
292578f812dSuebayasi 		j = 1;
293578f812dSuebayasi 		/* TODO - check this */
294*a9beb1edSmglocker 		inc = (8 + qh->host_interval - 1) / qh->host_interval;
295578f812dSuebayasi 		for (i = 0; i < inc; i++) {
296578f812dSuebayasi 			chan->schinfo |= j;
297*a9beb1edSmglocker 			j = j << qh->host_interval;
298578f812dSuebayasi 		}
299578f812dSuebayasi 	} else {
300578f812dSuebayasi 		chan->schinfo = 0xff;
301578f812dSuebayasi 	}
302578f812dSuebayasi }
303578f812dSuebayasi 
dwc2_release_channel_ddma(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)30405c50565Suebayasi STATIC void dwc2_release_channel_ddma(struct dwc2_hsotg *hsotg,
305578f812dSuebayasi 				      struct dwc2_qh *qh)
306578f812dSuebayasi {
307578f812dSuebayasi 	struct dwc2_host_chan *chan = qh->channel;
308578f812dSuebayasi 
309578f812dSuebayasi 	if (dwc2_qh_is_non_per(qh)) {
310*a9beb1edSmglocker 		if (hsotg->params.uframe_sched)
311578f812dSuebayasi 			hsotg->available_host_channels++;
312578f812dSuebayasi 		else
313578f812dSuebayasi 			hsotg->non_periodic_channels--;
314578f812dSuebayasi 	} else {
315578f812dSuebayasi 		dwc2_update_frame_list(hsotg, qh, 0);
316d05ae140Smglocker 		hsotg->available_host_channels++;
317578f812dSuebayasi 	}
318578f812dSuebayasi 
319578f812dSuebayasi 	/*
320578f812dSuebayasi 	 * The condition is added to prevent double cleanup try in case of
321578f812dSuebayasi 	 * device disconnect. See channel cleanup in dwc2_hcd_disconnect().
322578f812dSuebayasi 	 */
323578f812dSuebayasi 	if (chan->qh) {
324d05ae140Smglocker 		if (!list_empty(&chan->hc_list_entry))
325d05ae140Smglocker 			list_del(&chan->hc_list_entry);
326578f812dSuebayasi 		dwc2_hc_cleanup(hsotg, chan);
327d05ae140Smglocker 		list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
328578f812dSuebayasi 		chan->qh = NULL;
329578f812dSuebayasi 	}
330578f812dSuebayasi 
331578f812dSuebayasi 	qh->channel = NULL;
332578f812dSuebayasi 	qh->ntd = 0;
333578f812dSuebayasi 
334578f812dSuebayasi 	if (qh->desc_list)
335*a9beb1edSmglocker 		memset(qh->desc_list, 0, sizeof(struct dwc2_dma_desc) *
336578f812dSuebayasi 		       dwc2_max_desc_num(qh));
337578f812dSuebayasi }
338578f812dSuebayasi 
339578f812dSuebayasi /**
340578f812dSuebayasi  * dwc2_hcd_qh_init_ddma() - Initializes a QH structure's Descriptor DMA
341578f812dSuebayasi  * related members
342578f812dSuebayasi  *
343578f812dSuebayasi  * @hsotg: The HCD state structure for the DWC OTG controller
344578f812dSuebayasi  * @qh:    The QH to init
345*a9beb1edSmglocker  * @mem_flags: Indicates the type of memory allocation
346578f812dSuebayasi  *
347578f812dSuebayasi  * Return: 0 if successful, negative error code otherwise
348578f812dSuebayasi  *
349578f812dSuebayasi  * Allocates memory for the descriptor list. For the first periodic QH,
350578f812dSuebayasi  * allocates memory for the FrameList and enables periodic scheduling.
351578f812dSuebayasi  */
dwc2_hcd_qh_init_ddma(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,gfp_t mem_flags)352578f812dSuebayasi int dwc2_hcd_qh_init_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
353578f812dSuebayasi 			  gfp_t mem_flags)
354578f812dSuebayasi {
355578f812dSuebayasi 	int retval;
356578f812dSuebayasi 
357578f812dSuebayasi 	if (qh->do_split) {
358578f812dSuebayasi 		dev_err(hsotg->dev,
359578f812dSuebayasi 			"SPLIT Transfers are not supported in Descriptor DMA mode.\n");
360578f812dSuebayasi 		retval = -EINVAL;
361578f812dSuebayasi 		goto err0;
362578f812dSuebayasi 	}
363578f812dSuebayasi 
364578f812dSuebayasi 	retval = dwc2_desc_list_alloc(hsotg, qh, mem_flags);
365578f812dSuebayasi 	if (retval)
366578f812dSuebayasi 		goto err0;
367578f812dSuebayasi 
368578f812dSuebayasi 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
369578f812dSuebayasi 	    qh->ep_type == USB_ENDPOINT_XFER_INT) {
370578f812dSuebayasi 		if (!hsotg->frame_list) {
371578f812dSuebayasi 			retval = dwc2_frame_list_alloc(hsotg, mem_flags);
372578f812dSuebayasi 			if (retval)
373578f812dSuebayasi 				goto err1;
374578f812dSuebayasi 			/* Enable periodic schedule on first periodic QH */
375578f812dSuebayasi 			dwc2_per_sched_enable(hsotg, HCFG_FRLISTEN_64);
376578f812dSuebayasi 		}
377578f812dSuebayasi 	}
378578f812dSuebayasi 
379578f812dSuebayasi 	qh->ntd = 0;
380578f812dSuebayasi 	return 0;
381578f812dSuebayasi 
382578f812dSuebayasi err1:
383578f812dSuebayasi 	dwc2_desc_list_free(hsotg, qh);
384578f812dSuebayasi err0:
385578f812dSuebayasi 	return retval;
386578f812dSuebayasi }
387578f812dSuebayasi 
388578f812dSuebayasi /**
389578f812dSuebayasi  * dwc2_hcd_qh_free_ddma() - Frees a QH structure's Descriptor DMA related
390578f812dSuebayasi  * members
391578f812dSuebayasi  *
392578f812dSuebayasi  * @hsotg: The HCD state structure for the DWC OTG controller
393578f812dSuebayasi  * @qh:    The QH to free
394578f812dSuebayasi  *
395578f812dSuebayasi  * Frees descriptor list memory associated with the QH. If QH is periodic and
396578f812dSuebayasi  * the last, frees FrameList memory and disables periodic scheduling.
397578f812dSuebayasi  */
dwc2_hcd_qh_free_ddma(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)398578f812dSuebayasi void dwc2_hcd_qh_free_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
399578f812dSuebayasi {
400d05ae140Smglocker 	unsigned long flags;
401d05ae140Smglocker 
402578f812dSuebayasi 	dwc2_desc_list_free(hsotg, qh);
403578f812dSuebayasi 
404578f812dSuebayasi 	/*
405578f812dSuebayasi 	 * Channel still assigned due to some reasons.
406578f812dSuebayasi 	 * Seen on Isoc URB dequeue. Channel halted but no subsequent
407578f812dSuebayasi 	 * ChHalted interrupt to release the channel. Afterwards
408578f812dSuebayasi 	 * when it comes here from endpoint disable routine
409578f812dSuebayasi 	 * channel remains assigned.
410578f812dSuebayasi 	 */
411d05ae140Smglocker 	spin_lock_irqsave(&hsotg->lock, flags);
412578f812dSuebayasi 	if (qh->channel)
413578f812dSuebayasi 		dwc2_release_channel_ddma(hsotg, qh);
414d05ae140Smglocker 	spin_unlock_irqrestore(&hsotg->lock, flags);
415578f812dSuebayasi 
416578f812dSuebayasi 	if ((qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
417578f812dSuebayasi 	     qh->ep_type == USB_ENDPOINT_XFER_INT) &&
418*a9beb1edSmglocker 	    (hsotg->params.uframe_sched ||
419578f812dSuebayasi 	     !hsotg->periodic_channels) && hsotg->frame_list) {
420578f812dSuebayasi 		dwc2_per_sched_disable(hsotg);
421578f812dSuebayasi 		dwc2_frame_list_free(hsotg);
422578f812dSuebayasi 	}
423578f812dSuebayasi }
424578f812dSuebayasi 
dwc2_frame_to_desc_idx(struct dwc2_qh * qh,u16 frame_idx)42505c50565Suebayasi STATIC u8 dwc2_frame_to_desc_idx(struct dwc2_qh *qh, u16 frame_idx)
426578f812dSuebayasi {
427578f812dSuebayasi 	if (qh->dev_speed == USB_SPEED_HIGH)
428578f812dSuebayasi 		/* Descriptor set (8 descriptors) index which is 8-aligned */
429578f812dSuebayasi 		return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
430578f812dSuebayasi 	else
431578f812dSuebayasi 		return frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1);
432578f812dSuebayasi }
433578f812dSuebayasi 
434578f812dSuebayasi /*
435578f812dSuebayasi  * Determine starting frame for Isochronous transfer.
436578f812dSuebayasi  * Few frames skipped to prevent race condition with HC.
437578f812dSuebayasi  */
dwc2_calc_starting_frame(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,u16 * skip_frames)43805c50565Suebayasi STATIC u16 dwc2_calc_starting_frame(struct dwc2_hsotg *hsotg,
439578f812dSuebayasi 				    struct dwc2_qh *qh, u16 *skip_frames)
440578f812dSuebayasi {
441578f812dSuebayasi 	u16 frame;
442578f812dSuebayasi 
443578f812dSuebayasi 	hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
444578f812dSuebayasi 
445*a9beb1edSmglocker 	/*
446*a9beb1edSmglocker 	 * next_active_frame is always frame number (not uFrame) both in FS
447*a9beb1edSmglocker 	 * and HS!
448*a9beb1edSmglocker 	 */
449578f812dSuebayasi 
450578f812dSuebayasi 	/*
451578f812dSuebayasi 	 * skip_frames is used to limit activated descriptors number
452578f812dSuebayasi 	 * to avoid the situation when HC services the last activated
453578f812dSuebayasi 	 * descriptor firstly.
454578f812dSuebayasi 	 * Example for FS:
455578f812dSuebayasi 	 * Current frame is 1, scheduled frame is 3. Since HC always fetches
456578f812dSuebayasi 	 * the descriptor corresponding to curr_frame+1, the descriptor
457578f812dSuebayasi 	 * corresponding to frame 2 will be fetched. If the number of
458578f812dSuebayasi 	 * descriptors is max=64 (or greather) the list will be fully programmed
459578f812dSuebayasi 	 * with Active descriptors and it is possible case (rare) that the
460578f812dSuebayasi 	 * latest descriptor(considering rollback) corresponding to frame 2 will
461578f812dSuebayasi 	 * be serviced first. HS case is more probable because, in fact, up to
462578f812dSuebayasi 	 * 11 uframes (16 in the code) may be skipped.
463578f812dSuebayasi 	 */
464578f812dSuebayasi 	if (qh->dev_speed == USB_SPEED_HIGH) {
465578f812dSuebayasi 		/*
466578f812dSuebayasi 		 * Consider uframe counter also, to start xfer asap. If half of
467578f812dSuebayasi 		 * the frame elapsed skip 2 frames otherwise just 1 frame.
468578f812dSuebayasi 		 * Starting descriptor index must be 8-aligned, so if the
469578f812dSuebayasi 		 * current frame is near to complete the next one is skipped as
470578f812dSuebayasi 		 * well.
471578f812dSuebayasi 		 */
472578f812dSuebayasi 		if (dwc2_micro_frame_num(hsotg->frame_number) >= 5) {
473578f812dSuebayasi 			*skip_frames = 2 * 8;
474578f812dSuebayasi 			frame = dwc2_frame_num_inc(hsotg->frame_number,
475578f812dSuebayasi 						   *skip_frames);
476578f812dSuebayasi 		} else {
477578f812dSuebayasi 			*skip_frames = 1 * 8;
478578f812dSuebayasi 			frame = dwc2_frame_num_inc(hsotg->frame_number,
479578f812dSuebayasi 						   *skip_frames);
480578f812dSuebayasi 		}
481578f812dSuebayasi 
482578f812dSuebayasi 		frame = dwc2_full_frame_num(frame);
483578f812dSuebayasi 	} else {
484578f812dSuebayasi 		/*
485578f812dSuebayasi 		 * Two frames are skipped for FS - the current and the next.
486578f812dSuebayasi 		 * But for descriptor programming, 1 frame (descriptor) is
487578f812dSuebayasi 		 * enough, see example above.
488578f812dSuebayasi 		 */
489578f812dSuebayasi 		*skip_frames = 1;
490578f812dSuebayasi 		frame = dwc2_frame_num_inc(hsotg->frame_number, 2);
491578f812dSuebayasi 	}
492578f812dSuebayasi 
493578f812dSuebayasi 	return frame;
494578f812dSuebayasi }
495578f812dSuebayasi 
496578f812dSuebayasi /*
497578f812dSuebayasi  * Calculate initial descriptor index for isochronous transfer based on
498578f812dSuebayasi  * scheduled frame
499578f812dSuebayasi  */
dwc2_recalc_initial_desc_idx(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)50005c50565Suebayasi STATIC u16 dwc2_recalc_initial_desc_idx(struct dwc2_hsotg *hsotg,
501578f812dSuebayasi 					struct dwc2_qh *qh)
502578f812dSuebayasi {
503578f812dSuebayasi 	u16 frame, fr_idx, fr_idx_tmp, skip_frames;
504578f812dSuebayasi 
505578f812dSuebayasi 	/*
506578f812dSuebayasi 	 * With current ISOC processing algorithm the channel is being released
507578f812dSuebayasi 	 * when no more QTDs in the list (qh->ntd == 0). Thus this function is
508578f812dSuebayasi 	 * called only when qh->ntd == 0 and qh->channel == 0.
509578f812dSuebayasi 	 *
510578f812dSuebayasi 	 * So qh->channel != NULL branch is not used and just not removed from
511578f812dSuebayasi 	 * the source file. It is required for another possible approach which
512578f812dSuebayasi 	 * is, do not disable and release the channel when ISOC session
513578f812dSuebayasi 	 * completed, just move QH to inactive schedule until new QTD arrives.
514578f812dSuebayasi 	 * On new QTD, the QH moved back to 'ready' schedule, starting frame and
515578f812dSuebayasi 	 * therefore starting desc_index are recalculated. In this case channel
516578f812dSuebayasi 	 * is released only on ep_disable.
517578f812dSuebayasi 	 */
518578f812dSuebayasi 
519578f812dSuebayasi 	/*
520578f812dSuebayasi 	 * Calculate starting descriptor index. For INTERRUPT endpoint it is
521578f812dSuebayasi 	 * always 0.
522578f812dSuebayasi 	 */
523578f812dSuebayasi 	if (qh->channel) {
524578f812dSuebayasi 		frame = dwc2_calc_starting_frame(hsotg, qh, &skip_frames);
525578f812dSuebayasi 		/*
526578f812dSuebayasi 		 * Calculate initial descriptor index based on FrameList current
527578f812dSuebayasi 		 * bitmap and servicing period
528578f812dSuebayasi 		 */
529578f812dSuebayasi 		fr_idx_tmp = dwc2_frame_list_idx(frame);
530578f812dSuebayasi 		fr_idx = (FRLISTEN_64_SIZE +
531*a9beb1edSmglocker 			  dwc2_frame_list_idx(qh->next_active_frame) -
532*a9beb1edSmglocker 			  fr_idx_tmp) % dwc2_frame_incr_val(qh);
533578f812dSuebayasi 		fr_idx = (fr_idx + fr_idx_tmp) % FRLISTEN_64_SIZE;
534578f812dSuebayasi 	} else {
535*a9beb1edSmglocker 		qh->next_active_frame = dwc2_calc_starting_frame(hsotg, qh,
536578f812dSuebayasi 							   &skip_frames);
537*a9beb1edSmglocker 		fr_idx = dwc2_frame_list_idx(qh->next_active_frame);
538578f812dSuebayasi 	}
539578f812dSuebayasi 
540578f812dSuebayasi 	qh->td_first = qh->td_last = dwc2_frame_to_desc_idx(qh, fr_idx);
541578f812dSuebayasi 
542578f812dSuebayasi 	return skip_frames;
543578f812dSuebayasi }
544578f812dSuebayasi 
545578f812dSuebayasi #define ISOC_URB_GIVEBACK_ASAP
546578f812dSuebayasi 
547578f812dSuebayasi #define MAX_ISOC_XFER_SIZE_FS	1023
548578f812dSuebayasi #define MAX_ISOC_XFER_SIZE_HS	3072
549578f812dSuebayasi #define DESCNUM_THRESHOLD	4
550578f812dSuebayasi 
dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg * hsotg,struct dwc2_qtd * qtd,struct dwc2_qh * qh,u32 max_xfer_size,u16 idx)55105c50565Suebayasi STATIC void dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
552578f812dSuebayasi 					 struct dwc2_qtd *qtd,
553578f812dSuebayasi 					 struct dwc2_qh *qh, u32 max_xfer_size,
554578f812dSuebayasi 					 u16 idx)
555578f812dSuebayasi {
556*a9beb1edSmglocker 	struct dwc2_dma_desc *dma_desc = &qh->desc_list[idx];
557578f812dSuebayasi 	struct dwc2_hcd_iso_packet_desc *frame_desc;
558578f812dSuebayasi 
559578f812dSuebayasi 	memset(dma_desc, 0, sizeof(*dma_desc));
560578f812dSuebayasi 	frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
561578f812dSuebayasi 
562578f812dSuebayasi 	if (frame_desc->length > max_xfer_size)
563578f812dSuebayasi 		qh->n_bytes[idx] = max_xfer_size;
564578f812dSuebayasi 	else
565578f812dSuebayasi 		qh->n_bytes[idx] = frame_desc->length;
566578f812dSuebayasi 
567578f812dSuebayasi 	dma_desc->buf = (u32)(DMAADDR(qtd->urb->usbdma, frame_desc->offset));
568578f812dSuebayasi 	dma_desc->status = qh->n_bytes[idx] << HOST_DMA_ISOC_NBYTES_SHIFT &
569578f812dSuebayasi 			   HOST_DMA_ISOC_NBYTES_MASK;
570578f812dSuebayasi 
571d05ae140Smglocker 	/* Set active bit */
572d05ae140Smglocker 	dma_desc->status |= HOST_DMA_A;
573d05ae140Smglocker 
574d05ae140Smglocker 	qh->ntd++;
575d05ae140Smglocker 	qtd->isoc_frame_index_last++;
576d05ae140Smglocker 
577578f812dSuebayasi #ifdef ISOC_URB_GIVEBACK_ASAP
578578f812dSuebayasi 	/* Set IOC for each descriptor corresponding to last frame of URB */
579578f812dSuebayasi 	if (qtd->isoc_frame_index_last == qtd->urb->packet_count)
580578f812dSuebayasi 		dma_desc->status |= HOST_DMA_IOC;
581578f812dSuebayasi #endif
582578f812dSuebayasi 
583d05ae140Smglocker 	usb_syncmem(&qh->desc_list_usbdma,
584*a9beb1edSmglocker 	    (idx * sizeof(struct dwc2_dma_desc)),
585*a9beb1edSmglocker 	    sizeof(struct dwc2_dma_desc),
586d05ae140Smglocker 	    BUS_DMASYNC_PREWRITE);
587578f812dSuebayasi }
588578f812dSuebayasi 
dwc2_init_isoc_dma_desc(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,u16 skip_frames)58905c50565Suebayasi STATIC void dwc2_init_isoc_dma_desc(struct dwc2_hsotg *hsotg,
590578f812dSuebayasi 				    struct dwc2_qh *qh, u16 skip_frames)
591578f812dSuebayasi {
592578f812dSuebayasi 	struct dwc2_qtd *qtd;
593578f812dSuebayasi 	u32 max_xfer_size;
594d05ae140Smglocker 	u16 idx, inc, n_desc = 0, ntd_max = 0;
595d05ae140Smglocker 	u16 cur_idx;
596d05ae140Smglocker 	u16 next_idx;
597578f812dSuebayasi 
598578f812dSuebayasi 	idx = qh->td_last;
599*a9beb1edSmglocker 	inc = qh->host_interval;
600d05ae140Smglocker 	hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
601d05ae140Smglocker 	cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
602d05ae140Smglocker 	next_idx = dwc2_desclist_idx_inc(qh->td_last, inc, qh->dev_speed);
603d05ae140Smglocker 
604d05ae140Smglocker 	/*
605d05ae140Smglocker 	 * Ensure current frame number didn't overstep last scheduled
606d05ae140Smglocker 	 * descriptor. If it happens, the only way to recover is to move
607d05ae140Smglocker 	 * qh->td_last to current frame number + 1.
608d05ae140Smglocker 	 * So that next isoc descriptor will be scheduled on frame number + 1
609d05ae140Smglocker 	 * and not on a past frame.
610d05ae140Smglocker 	 */
611d05ae140Smglocker 	if (dwc2_frame_idx_num_gt(cur_idx, next_idx) || (cur_idx == next_idx)) {
612d05ae140Smglocker 		if (inc < 32) {
613d05ae140Smglocker 			dev_vdbg(hsotg->dev,
614d05ae140Smglocker 				 "current frame number overstep last descriptor\n");
615d05ae140Smglocker 			qh->td_last = dwc2_desclist_idx_inc(cur_idx, inc,
616d05ae140Smglocker 							    qh->dev_speed);
617d05ae140Smglocker 			idx = qh->td_last;
618d05ae140Smglocker 		}
619d05ae140Smglocker 	}
620578f812dSuebayasi 
621*a9beb1edSmglocker 	if (qh->host_interval) {
622*a9beb1edSmglocker 		ntd_max = (dwc2_max_desc_num(qh) + qh->host_interval - 1) /
623*a9beb1edSmglocker 				qh->host_interval;
624578f812dSuebayasi 		if (skip_frames && !qh->channel)
625*a9beb1edSmglocker 			ntd_max -= skip_frames / qh->host_interval;
626578f812dSuebayasi 	}
627578f812dSuebayasi 
628578f812dSuebayasi 	max_xfer_size = qh->dev_speed == USB_SPEED_HIGH ?
629578f812dSuebayasi 			MAX_ISOC_XFER_SIZE_HS : MAX_ISOC_XFER_SIZE_FS;
630578f812dSuebayasi 
631d05ae140Smglocker 	list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
632d05ae140Smglocker 		if (qtd->in_process &&
633d05ae140Smglocker 		    qtd->isoc_frame_index_last ==
634d05ae140Smglocker 		    qtd->urb->packet_count)
635d05ae140Smglocker 			continue;
636d05ae140Smglocker 
637d05ae140Smglocker 		qtd->isoc_td_first = idx;
638578f812dSuebayasi 		while (qh->ntd < ntd_max && qtd->isoc_frame_index_last <
639578f812dSuebayasi 						qtd->urb->packet_count) {
640578f812dSuebayasi 			dwc2_fill_host_isoc_dma_desc(hsotg, qtd, qh,
641578f812dSuebayasi 						     max_xfer_size, idx);
642578f812dSuebayasi 			idx = dwc2_desclist_idx_inc(idx, inc, qh->dev_speed);
643578f812dSuebayasi 			n_desc++;
644578f812dSuebayasi 		}
645d05ae140Smglocker 		qtd->isoc_td_last = idx;
646578f812dSuebayasi 		qtd->in_process = 1;
647578f812dSuebayasi 	}
648578f812dSuebayasi 
649578f812dSuebayasi 	qh->td_last = idx;
650578f812dSuebayasi 
651578f812dSuebayasi #ifdef ISOC_URB_GIVEBACK_ASAP
652578f812dSuebayasi 	/* Set IOC for last descriptor if descriptor list is full */
653578f812dSuebayasi 	if (qh->ntd == ntd_max) {
654578f812dSuebayasi 		idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
655578f812dSuebayasi 		qh->desc_list[idx].status |= HOST_DMA_IOC;
656d05ae140Smglocker 		usb_syncmem(&qh->desc_list_usbdma,
657*a9beb1edSmglocker 		    (idx * sizeof(struct dwc2_dma_desc)),
658*a9beb1edSmglocker 		    sizeof(struct dwc2_dma_desc),
659d05ae140Smglocker 		    BUS_DMASYNC_PREWRITE);
660578f812dSuebayasi 	}
661578f812dSuebayasi #else
662578f812dSuebayasi 	/*
663578f812dSuebayasi 	 * Set IOC bit only for one descriptor. Always try to be ahead of HW
664578f812dSuebayasi 	 * processing, i.e. on IOC generation driver activates next descriptor
665578f812dSuebayasi 	 * but core continues to process descriptors following the one with IOC
666578f812dSuebayasi 	 * set.
667578f812dSuebayasi 	 */
668578f812dSuebayasi 
669578f812dSuebayasi 	if (n_desc > DESCNUM_THRESHOLD)
670578f812dSuebayasi 		/*
671578f812dSuebayasi 		 * Move IOC "up". Required even if there is only one QTD
672578f812dSuebayasi 		 * in the list, because QTDs might continue to be queued,
673578f812dSuebayasi 		 * but during the activation it was only one queued.
674578f812dSuebayasi 		 * Actually more than one QTD might be in the list if this
675578f812dSuebayasi 		 * function called from XferCompletion - QTDs was queued during
676578f812dSuebayasi 		 * HW processing of the previous descriptor chunk.
677578f812dSuebayasi 		 */
678578f812dSuebayasi 		idx = dwc2_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2),
679578f812dSuebayasi 					    qh->dev_speed);
680578f812dSuebayasi 	else
681578f812dSuebayasi 		/*
682578f812dSuebayasi 		 * Set the IOC for the latest descriptor if either number of
683578f812dSuebayasi 		 * descriptors is not greater than threshold or no more new
684578f812dSuebayasi 		 * descriptors activated
685578f812dSuebayasi 		 */
686578f812dSuebayasi 		idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
687578f812dSuebayasi 
688578f812dSuebayasi 	qh->desc_list[idx].status |= HOST_DMA_IOC;
689d05ae140Smglocker 	usb_syncmem(&qh->desc_list_usbdma,
690*a9beb1edSmglocker 	    (idx * sizeof(struct dwc2_dma_desc)),
691*a9beb1edSmglocker 	    sizeof(struct dwc2_dma_desc),
692d05ae140Smglocker 	    BUS_DMASYNC_PREWRITE);
693578f812dSuebayasi #endif
694578f812dSuebayasi }
695578f812dSuebayasi 
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)69605c50565Suebayasi STATIC void dwc2_fill_host_dma_desc(struct dwc2_hsotg *hsotg,
697578f812dSuebayasi 				    struct dwc2_host_chan *chan,
698578f812dSuebayasi 				    struct dwc2_qtd *qtd, struct dwc2_qh *qh,
699578f812dSuebayasi 				    int n_desc)
700578f812dSuebayasi {
701*a9beb1edSmglocker 	struct dwc2_dma_desc *dma_desc = &qh->desc_list[n_desc];
702578f812dSuebayasi 	int len = chan->xfer_len;
703578f812dSuebayasi 
704*a9beb1edSmglocker 	if (len > HOST_DMA_NBYTES_LIMIT - (chan->max_packet - 1))
705*a9beb1edSmglocker 		len = HOST_DMA_NBYTES_LIMIT - (chan->max_packet - 1);
706578f812dSuebayasi 
707578f812dSuebayasi 	if (chan->ep_is_in) {
708578f812dSuebayasi 		int num_packets;
709578f812dSuebayasi 
710578f812dSuebayasi 		if (len > 0 && chan->max_packet)
711578f812dSuebayasi 			num_packets = (len + chan->max_packet - 1)
712578f812dSuebayasi 					/ chan->max_packet;
713578f812dSuebayasi 		else
714578f812dSuebayasi 			/* Need 1 packet for transfer length of 0 */
715578f812dSuebayasi 			num_packets = 1;
716578f812dSuebayasi 
717578f812dSuebayasi 		/* Always program an integral # of packets for IN transfers */
718578f812dSuebayasi 		len = num_packets * chan->max_packet;
719578f812dSuebayasi 	}
720578f812dSuebayasi 
721578f812dSuebayasi 	dma_desc->status = len << HOST_DMA_NBYTES_SHIFT & HOST_DMA_NBYTES_MASK;
722578f812dSuebayasi 	qh->n_bytes[n_desc] = len;
723578f812dSuebayasi 
724578f812dSuebayasi 	if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL &&
725578f812dSuebayasi 	    qtd->control_phase == DWC2_CONTROL_SETUP)
726578f812dSuebayasi 		dma_desc->status |= HOST_DMA_SUP;
727578f812dSuebayasi 
728578f812dSuebayasi 	dma_desc->buf = (u32)chan->xfer_dma;
729578f812dSuebayasi 
730d05ae140Smglocker 	usb_syncmem(&qh->desc_list_usbdma,
731*a9beb1edSmglocker 	    (n_desc * sizeof(struct dwc2_dma_desc)),
732*a9beb1edSmglocker 	    sizeof(struct dwc2_dma_desc),
733d05ae140Smglocker 	    BUS_DMASYNC_PREWRITE);
734d05ae140Smglocker 
735578f812dSuebayasi 	/*
736578f812dSuebayasi 	 * Last (or only) descriptor of IN transfer with actual size less
737578f812dSuebayasi 	 * than MaxPacket
738578f812dSuebayasi 	 */
739578f812dSuebayasi 	if (len > chan->xfer_len) {
740578f812dSuebayasi 		chan->xfer_len = 0;
741578f812dSuebayasi 	} else {
742*a9beb1edSmglocker 		chan->xfer_dma += len;
743578f812dSuebayasi 		chan->xfer_len -= len;
744578f812dSuebayasi 	}
745578f812dSuebayasi }
746578f812dSuebayasi 
dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)74705c50565Suebayasi STATIC void dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg *hsotg,
748578f812dSuebayasi 					struct dwc2_qh *qh)
749578f812dSuebayasi {
750578f812dSuebayasi 	struct dwc2_qtd *qtd;
751578f812dSuebayasi 	struct dwc2_host_chan *chan = qh->channel;
752578f812dSuebayasi 	int n_desc = 0;
753578f812dSuebayasi 
754578f812dSuebayasi 	dev_vdbg(hsotg->dev, "%s(): qh=%p dma=%08lx len=%d\n", __func__, qh,
755578f812dSuebayasi 		 (unsigned long)chan->xfer_dma, chan->xfer_len);
756578f812dSuebayasi 
757578f812dSuebayasi 	/*
758578f812dSuebayasi 	 * Start with chan->xfer_dma initialized in assign_and_init_hc(), then
759578f812dSuebayasi 	 * if SG transfer consists of multiple URBs, this pointer is re-assigned
760578f812dSuebayasi 	 * to the buffer of the currently processed QTD. For non-SG request
761578f812dSuebayasi 	 * there is always one QTD active.
762578f812dSuebayasi 	 */
763578f812dSuebayasi 
764d05ae140Smglocker 	list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
765578f812dSuebayasi 		dev_vdbg(hsotg->dev, "qtd=%p\n", qtd);
766578f812dSuebayasi 
767578f812dSuebayasi 		if (n_desc) {
768578f812dSuebayasi 			/* SG request - more than 1 QTD */
769578f812dSuebayasi 			chan->xfer_dma = DMAADDR(qtd->urb->usbdma,
770578f812dSuebayasi 					qtd->urb->actual_length);
771578f812dSuebayasi 			chan->xfer_len = qtd->urb->length -
772578f812dSuebayasi 					qtd->urb->actual_length;
773578f812dSuebayasi 			dev_vdbg(hsotg->dev, "buf=%08lx len=%d\n",
774578f812dSuebayasi 				 (unsigned long)chan->xfer_dma, chan->xfer_len);
775578f812dSuebayasi 		}
776578f812dSuebayasi 
777578f812dSuebayasi 		qtd->n_desc = 0;
778578f812dSuebayasi 		do {
779578f812dSuebayasi 			if (n_desc > 1) {
780578f812dSuebayasi 				qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
781578f812dSuebayasi 				dev_vdbg(hsotg->dev,
782578f812dSuebayasi 					 "set A bit in desc %d (%p)\n",
783578f812dSuebayasi 					 n_desc - 1,
784578f812dSuebayasi 					 &qh->desc_list[n_desc - 1]);
785d05ae140Smglocker 				usb_syncmem(&qh->desc_list_usbdma,
786d05ae140Smglocker 				    ((n_desc - 1) *
787*a9beb1edSmglocker 				    sizeof(struct dwc2_dma_desc)),
788*a9beb1edSmglocker 				    sizeof(struct dwc2_dma_desc),
789d05ae140Smglocker 				    BUS_DMASYNC_PREWRITE);
790578f812dSuebayasi 			}
791578f812dSuebayasi 			dwc2_fill_host_dma_desc(hsotg, chan, qtd, qh, n_desc);
792578f812dSuebayasi 			dev_vdbg(hsotg->dev,
793578f812dSuebayasi 				 "desc %d (%p) buf=%08x status=%08x\n",
794578f812dSuebayasi 				 n_desc, &qh->desc_list[n_desc],
795578f812dSuebayasi 				 qh->desc_list[n_desc].buf,
796578f812dSuebayasi 				 qh->desc_list[n_desc].status);
797578f812dSuebayasi 			qtd->n_desc++;
798578f812dSuebayasi 			n_desc++;
799578f812dSuebayasi 		} while (chan->xfer_len > 0 &&
800578f812dSuebayasi 			 n_desc != MAX_DMA_DESC_NUM_GENERIC);
801578f812dSuebayasi 
802578f812dSuebayasi 		dev_vdbg(hsotg->dev, "n_desc=%d\n", n_desc);
803578f812dSuebayasi 		qtd->in_process = 1;
804578f812dSuebayasi 		if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL)
805578f812dSuebayasi 			break;
806578f812dSuebayasi 		if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
807578f812dSuebayasi 			break;
808578f812dSuebayasi 	}
809578f812dSuebayasi 
810578f812dSuebayasi 	if (n_desc) {
811578f812dSuebayasi 		qh->desc_list[n_desc - 1].status |=
812578f812dSuebayasi 				HOST_DMA_IOC | HOST_DMA_EOL | HOST_DMA_A;
813578f812dSuebayasi 		dev_vdbg(hsotg->dev, "set IOC/EOL/A bits in desc %d (%p)\n",
814578f812dSuebayasi 			 n_desc - 1, &qh->desc_list[n_desc - 1]);
815d05ae140Smglocker 		usb_syncmem(&qh->desc_list_usbdma,
816*a9beb1edSmglocker 		    ((n_desc - 1) * sizeof(struct dwc2_dma_desc)),
817*a9beb1edSmglocker 		    sizeof(struct dwc2_dma_desc),
818d05ae140Smglocker 		    BUS_DMASYNC_PREWRITE);
819578f812dSuebayasi 		if (n_desc > 1) {
820578f812dSuebayasi 			qh->desc_list[0].status |= HOST_DMA_A;
821578f812dSuebayasi 			dev_vdbg(hsotg->dev, "set A bit in desc 0 (%p)\n",
822578f812dSuebayasi 				 &qh->desc_list[0]);
823d05ae140Smglocker 			usb_syncmem(&qh->desc_list_usbdma, 0,
824*a9beb1edSmglocker 			    sizeof(struct dwc2_dma_desc),
825d05ae140Smglocker 			    BUS_DMASYNC_PREWRITE);
826578f812dSuebayasi 		}
827578f812dSuebayasi 		chan->ntd = n_desc;
828578f812dSuebayasi 	}
829578f812dSuebayasi }
830578f812dSuebayasi 
831578f812dSuebayasi /**
832578f812dSuebayasi  * dwc2_hcd_start_xfer_ddma() - Starts a transfer in Descriptor DMA mode
833578f812dSuebayasi  *
834578f812dSuebayasi  * @hsotg: The HCD state structure for the DWC OTG controller
835578f812dSuebayasi  * @qh:    The QH to init
836578f812dSuebayasi  *
837578f812dSuebayasi  * Return: 0 if successful, negative error code otherwise
838578f812dSuebayasi  *
839578f812dSuebayasi  * For Control and Bulk endpoints, initializes descriptor list and starts the
840578f812dSuebayasi  * transfer. For Interrupt and Isochronous endpoints, initializes descriptor
841578f812dSuebayasi  * list then updates FrameList, marking appropriate entries as active.
842578f812dSuebayasi  *
843578f812dSuebayasi  * For Isochronous endpoints the starting descriptor index is calculated based
844578f812dSuebayasi  * on the scheduled frame, but only on the first transfer descriptor within a
845578f812dSuebayasi  * session. Then the transfer is started via enabling the channel.
846578f812dSuebayasi  *
847578f812dSuebayasi  * For Isochronous endpoints the channel is not halted on XferComplete
848578f812dSuebayasi  * interrupt so remains assigned to the endpoint(QH) until session is done.
849578f812dSuebayasi  */
dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)850578f812dSuebayasi void dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
851578f812dSuebayasi {
852578f812dSuebayasi 	/* Channel is already assigned */
853578f812dSuebayasi 	struct dwc2_host_chan *chan = qh->channel;
854578f812dSuebayasi 	u16 skip_frames = 0;
855578f812dSuebayasi 
856578f812dSuebayasi 	switch (chan->ep_type) {
857578f812dSuebayasi 	case USB_ENDPOINT_XFER_CONTROL:
858578f812dSuebayasi 	case USB_ENDPOINT_XFER_BULK:
859578f812dSuebayasi 		dwc2_init_non_isoc_dma_desc(hsotg, qh);
860578f812dSuebayasi 		dwc2_hc_start_transfer_ddma(hsotg, chan);
861578f812dSuebayasi 		break;
862578f812dSuebayasi 	case USB_ENDPOINT_XFER_INT:
863578f812dSuebayasi 		dwc2_init_non_isoc_dma_desc(hsotg, qh);
864578f812dSuebayasi 		dwc2_update_frame_list(hsotg, qh, 1);
865578f812dSuebayasi 		dwc2_hc_start_transfer_ddma(hsotg, chan);
866578f812dSuebayasi 		break;
867578f812dSuebayasi 	case USB_ENDPOINT_XFER_ISOC:
868578f812dSuebayasi 		if (!qh->ntd)
869578f812dSuebayasi 			skip_frames = dwc2_recalc_initial_desc_idx(hsotg, qh);
870578f812dSuebayasi 		dwc2_init_isoc_dma_desc(hsotg, qh, skip_frames);
871578f812dSuebayasi 
872578f812dSuebayasi 		if (!chan->xfer_started) {
873578f812dSuebayasi 			dwc2_update_frame_list(hsotg, qh, 1);
874578f812dSuebayasi 
875578f812dSuebayasi 			/*
876578f812dSuebayasi 			 * Always set to max, instead of actual size. Otherwise
877578f812dSuebayasi 			 * ntd will be changed with channel being enabled. Not
878578f812dSuebayasi 			 * recommended.
879578f812dSuebayasi 			 */
880578f812dSuebayasi 			chan->ntd = dwc2_max_desc_num(qh);
881578f812dSuebayasi 
882578f812dSuebayasi 			/* Enable channel only once for ISOC */
883578f812dSuebayasi 			dwc2_hc_start_transfer_ddma(hsotg, chan);
884578f812dSuebayasi 		}
885578f812dSuebayasi 
886578f812dSuebayasi 		break;
887578f812dSuebayasi 	default:
888578f812dSuebayasi 		break;
889578f812dSuebayasi 	}
890578f812dSuebayasi }
891578f812dSuebayasi 
892578f812dSuebayasi #define DWC2_CMPL_DONE		1
893578f812dSuebayasi #define DWC2_CMPL_STOP		2
894578f812dSuebayasi 
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)89505c50565Suebayasi STATIC int dwc2_cmpl_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
896578f812dSuebayasi 					struct dwc2_host_chan *chan,
897578f812dSuebayasi 					struct dwc2_qtd *qtd,
898578f812dSuebayasi 					struct dwc2_qh *qh, u16 idx)
899578f812dSuebayasi {
900*a9beb1edSmglocker 	struct dwc2_dma_desc *dma_desc;
901578f812dSuebayasi 	struct dwc2_hcd_iso_packet_desc *frame_desc;
902578f812dSuebayasi 	u16 remain = 0;
903578f812dSuebayasi 	int rc = 0;
904578f812dSuebayasi 
905578f812dSuebayasi 	if (!qtd->urb)
906578f812dSuebayasi 		return -EINVAL;
907578f812dSuebayasi 
908d05ae140Smglocker 	usb_syncmem(&qh->desc_list_usbdma,
909*a9beb1edSmglocker 	    (idx * sizeof(struct dwc2_dma_desc)),
910*a9beb1edSmglocker 	    sizeof(struct dwc2_dma_desc),
911d05ae140Smglocker 	    BUS_DMASYNC_POSTREAD);
912d05ae140Smglocker 
913d05ae140Smglocker 	dma_desc = &qh->desc_list[idx];
914d05ae140Smglocker 
915578f812dSuebayasi 	frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
916578f812dSuebayasi 	dma_desc->buf = (u32)(DMAADDR(qtd->urb->usbdma, frame_desc->offset));
917578f812dSuebayasi 	if (chan->ep_is_in)
918578f812dSuebayasi 		remain = (dma_desc->status & HOST_DMA_ISOC_NBYTES_MASK) >>
919578f812dSuebayasi 			 HOST_DMA_ISOC_NBYTES_SHIFT;
920578f812dSuebayasi 
921578f812dSuebayasi 	if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
922578f812dSuebayasi 		/*
923578f812dSuebayasi 		 * XactError, or unable to complete all the transactions
924578f812dSuebayasi 		 * in the scheduled micro-frame/frame, both indicated by
925578f812dSuebayasi 		 * HOST_DMA_STS_PKTERR
926578f812dSuebayasi 		 */
927578f812dSuebayasi 		qtd->urb->error_count++;
928578f812dSuebayasi 		frame_desc->actual_length = qh->n_bytes[idx] - remain;
929578f812dSuebayasi 		frame_desc->status = -EPROTO;
930578f812dSuebayasi 	} else {
931578f812dSuebayasi 		/* Success */
932578f812dSuebayasi 		frame_desc->actual_length = qh->n_bytes[idx] - remain;
933578f812dSuebayasi 		frame_desc->status = 0;
934578f812dSuebayasi 	}
935578f812dSuebayasi 
936578f812dSuebayasi 	if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
937578f812dSuebayasi 		/*
938578f812dSuebayasi 		 * urb->status is not used for isoc transfers here. The
939578f812dSuebayasi 		 * individual frame_desc status are used instead.
940578f812dSuebayasi 		 */
941578f812dSuebayasi 		dwc2_host_complete(hsotg, qtd, 0);
942578f812dSuebayasi 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
943578f812dSuebayasi 
944578f812dSuebayasi 		/*
945578f812dSuebayasi 		 * This check is necessary because urb_dequeue can be called
946578f812dSuebayasi 		 * from urb complete callback (sound driver for example). All
947578f812dSuebayasi 		 * pending URBs are dequeued there, so no need for further
948578f812dSuebayasi 		 * processing.
949578f812dSuebayasi 		 */
950578f812dSuebayasi 		if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE)
951578f812dSuebayasi 			return -1;
952578f812dSuebayasi 		rc = DWC2_CMPL_DONE;
953578f812dSuebayasi 	}
954578f812dSuebayasi 
955578f812dSuebayasi 	qh->ntd--;
956578f812dSuebayasi 
957578f812dSuebayasi 	/* Stop if IOC requested descriptor reached */
958578f812dSuebayasi 	if (dma_desc->status & HOST_DMA_IOC)
959578f812dSuebayasi 		rc = DWC2_CMPL_STOP;
960578f812dSuebayasi 
961578f812dSuebayasi 	return rc;
962578f812dSuebayasi }
963578f812dSuebayasi 
dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,enum dwc2_halt_status halt_status)96405c50565Suebayasi STATIC void dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
965578f812dSuebayasi 					 struct dwc2_host_chan *chan,
966578f812dSuebayasi 					 enum dwc2_halt_status halt_status)
967578f812dSuebayasi {
968578f812dSuebayasi 	struct dwc2_hcd_iso_packet_desc *frame_desc;
969578f812dSuebayasi 	struct dwc2_qtd *qtd, *qtd_tmp;
970578f812dSuebayasi 	struct dwc2_qh *qh;
971578f812dSuebayasi 	u16 idx;
972578f812dSuebayasi 	int rc;
973578f812dSuebayasi 
974578f812dSuebayasi 	qh = chan->qh;
975578f812dSuebayasi 	idx = qh->td_first;
976578f812dSuebayasi 
977578f812dSuebayasi 	if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
978d05ae140Smglocker 		list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
979578f812dSuebayasi 			qtd->in_process = 0;
980578f812dSuebayasi 		return;
981578f812dSuebayasi 	}
982578f812dSuebayasi 
983578f812dSuebayasi 	if (halt_status == DWC2_HC_XFER_AHB_ERR ||
984578f812dSuebayasi 	    halt_status == DWC2_HC_XFER_BABBLE_ERR) {
985578f812dSuebayasi 		/*
986578f812dSuebayasi 		 * Channel is halted in these error cases, considered as serious
987578f812dSuebayasi 		 * issues.
988578f812dSuebayasi 		 * Complete all URBs marking all frames as failed, irrespective
989578f812dSuebayasi 		 * whether some of the descriptors (frames) succeeded or not.
990578f812dSuebayasi 		 * Pass error code to completion routine as well, to update
991578f812dSuebayasi 		 * urb->status, some of class drivers might use it to stop
992578f812dSuebayasi 		 * queing transfer requests.
993578f812dSuebayasi 		 */
994578f812dSuebayasi 		int err = halt_status == DWC2_HC_XFER_AHB_ERR ?
995578f812dSuebayasi 			  -EIO : -EOVERFLOW;
996578f812dSuebayasi 
997d05ae140Smglocker 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
998d05ae140Smglocker 					 qtd_list_entry) {
999578f812dSuebayasi 			if (qtd->urb) {
1000578f812dSuebayasi 				for (idx = 0; idx < qtd->urb->packet_count;
1001578f812dSuebayasi 				     idx++) {
1002578f812dSuebayasi 					frame_desc = &qtd->urb->iso_descs[idx];
1003578f812dSuebayasi 					frame_desc->status = err;
1004578f812dSuebayasi 				}
1005578f812dSuebayasi 
1006578f812dSuebayasi 				dwc2_host_complete(hsotg, qtd, err);
1007578f812dSuebayasi 			}
1008578f812dSuebayasi 
1009578f812dSuebayasi 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1010578f812dSuebayasi 		}
1011578f812dSuebayasi 
1012578f812dSuebayasi 		return;
1013578f812dSuebayasi 	}
1014578f812dSuebayasi 
1015d05ae140Smglocker 	list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
1016578f812dSuebayasi 		if (!qtd->in_process)
1017578f812dSuebayasi 			break;
1018d05ae140Smglocker 
1019d05ae140Smglocker 		/*
1020d05ae140Smglocker 		 * Ensure idx corresponds to descriptor where first urb of this
1021d05ae140Smglocker 		 * qtd was added. In fact, during isoc desc init, dwc2 may skip
1022d05ae140Smglocker 		 * an index if current frame number is already over this index.
1023d05ae140Smglocker 		 */
1024d05ae140Smglocker 		if (idx != qtd->isoc_td_first) {
1025d05ae140Smglocker 			dev_vdbg(hsotg->dev,
1026d05ae140Smglocker 				 "try to complete %d instead of %d\n",
1027d05ae140Smglocker 				 idx, qtd->isoc_td_first);
1028d05ae140Smglocker 			idx = qtd->isoc_td_first;
1029d05ae140Smglocker 		}
1030d05ae140Smglocker 
1031578f812dSuebayasi 		do {
1032d05ae140Smglocker 			struct dwc2_qtd *qtd_next;
1033d05ae140Smglocker 			u16 cur_idx;
1034d05ae140Smglocker 
1035578f812dSuebayasi 			rc = dwc2_cmpl_host_isoc_dma_desc(hsotg, chan, qtd, qh,
1036578f812dSuebayasi 							  idx);
1037578f812dSuebayasi 			if (rc < 0)
1038578f812dSuebayasi 				return;
1039*a9beb1edSmglocker 			idx = dwc2_desclist_idx_inc(idx, qh->host_interval,
1040578f812dSuebayasi 						    chan->speed);
1041d05ae140Smglocker 			if (!rc)
1042d05ae140Smglocker 				continue;
1043d05ae140Smglocker 
1044578f812dSuebayasi 			if (rc == DWC2_CMPL_DONE)
1045578f812dSuebayasi 				break;
1046d05ae140Smglocker 
1047d05ae140Smglocker 			/* rc == DWC2_CMPL_STOP */
1048d05ae140Smglocker 
1049*a9beb1edSmglocker 			if (qh->host_interval >= 32)
1050d05ae140Smglocker 				goto stop_scan;
1051d05ae140Smglocker 
1052d05ae140Smglocker 			qh->td_first = idx;
1053d05ae140Smglocker 			cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
1054d05ae140Smglocker 			qtd_next = list_first_entry(&qh->qtd_list,
1055d05ae140Smglocker 						    struct dwc2_qtd,
1056d05ae140Smglocker 						    qtd_list_entry);
1057d05ae140Smglocker 			if (dwc2_frame_idx_num_gt(cur_idx,
1058d05ae140Smglocker 						  qtd_next->isoc_td_last))
1059d05ae140Smglocker 				break;
1060d05ae140Smglocker 
1061d05ae140Smglocker 			goto stop_scan;
1062d05ae140Smglocker 
1063578f812dSuebayasi 		} while (idx != qh->td_first);
1064578f812dSuebayasi 	}
1065578f812dSuebayasi 
1066578f812dSuebayasi stop_scan:
1067578f812dSuebayasi 	qh->td_first = idx;
1068578f812dSuebayasi }
1069578f812dSuebayasi 
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)107005c50565Suebayasi STATIC int dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg *hsotg,
1071578f812dSuebayasi 					       struct dwc2_host_chan *chan,
1072578f812dSuebayasi 					struct dwc2_qtd *qtd,
1073*a9beb1edSmglocker 					struct dwc2_dma_desc *dma_desc,
1074578f812dSuebayasi 					enum dwc2_halt_status halt_status,
1075578f812dSuebayasi 					u32 n_bytes, int *xfer_done)
1076578f812dSuebayasi {
1077578f812dSuebayasi 	struct dwc2_hcd_urb *urb = qtd->urb;
1078578f812dSuebayasi 	u16 remain = 0;
1079578f812dSuebayasi 
1080578f812dSuebayasi 	if (chan->ep_is_in)
1081578f812dSuebayasi 		remain = (dma_desc->status & HOST_DMA_NBYTES_MASK) >>
1082578f812dSuebayasi 			 HOST_DMA_NBYTES_SHIFT;
1083578f812dSuebayasi 
1084578f812dSuebayasi 	dev_vdbg(hsotg->dev, "remain=%d dwc2_urb=%p\n", remain, urb);
1085578f812dSuebayasi 
1086578f812dSuebayasi 	if (halt_status == DWC2_HC_XFER_AHB_ERR) {
1087578f812dSuebayasi 		dev_err(hsotg->dev, "EIO\n");
1088578f812dSuebayasi 		urb->status = -EIO;
1089578f812dSuebayasi 		return 1;
1090578f812dSuebayasi 	}
1091578f812dSuebayasi 
1092578f812dSuebayasi 	if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
1093578f812dSuebayasi 		switch (halt_status) {
1094578f812dSuebayasi 		case DWC2_HC_XFER_STALL:
1095578f812dSuebayasi 			dev_vdbg(hsotg->dev, "Stall\n");
1096578f812dSuebayasi 			urb->status = -EPIPE;
1097578f812dSuebayasi 			break;
1098578f812dSuebayasi 		case DWC2_HC_XFER_BABBLE_ERR:
1099578f812dSuebayasi 			dev_err(hsotg->dev, "Babble\n");
1100578f812dSuebayasi 			urb->status = -EOVERFLOW;
1101578f812dSuebayasi 			break;
1102578f812dSuebayasi 		case DWC2_HC_XFER_XACT_ERR:
1103578f812dSuebayasi 			dev_err(hsotg->dev, "XactErr\n");
1104578f812dSuebayasi 			urb->status = -EPROTO;
1105578f812dSuebayasi 			break;
1106578f812dSuebayasi 		default:
1107578f812dSuebayasi 			dev_err(hsotg->dev,
1108578f812dSuebayasi 				"%s: Unhandled descriptor error status (%d)\n",
1109578f812dSuebayasi 				__func__, halt_status);
1110578f812dSuebayasi 			break;
1111578f812dSuebayasi 		}
1112578f812dSuebayasi 		return 1;
1113578f812dSuebayasi 	}
1114578f812dSuebayasi 
1115578f812dSuebayasi 	if (dma_desc->status & HOST_DMA_A) {
1116578f812dSuebayasi 		dev_vdbg(hsotg->dev,
1117578f812dSuebayasi 			 "Active descriptor encountered on channel %d\n",
1118578f812dSuebayasi 			 chan->hc_num);
1119578f812dSuebayasi 		return 0;
1120578f812dSuebayasi 	}
1121578f812dSuebayasi 
1122578f812dSuebayasi 	if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1123578f812dSuebayasi 		if (qtd->control_phase == DWC2_CONTROL_DATA) {
1124578f812dSuebayasi 			urb->actual_length += n_bytes - remain;
1125578f812dSuebayasi 			if (remain || urb->actual_length >= urb->length) {
1126578f812dSuebayasi 				/*
1127578f812dSuebayasi 				 * For Control Data stage do not set urb->status
1128578f812dSuebayasi 				 * to 0, to prevent URB callback. Set it when
1129578f812dSuebayasi 				 * Status phase is done. See below.
1130578f812dSuebayasi 				 */
1131578f812dSuebayasi 				*xfer_done = 1;
1132578f812dSuebayasi 			}
1133578f812dSuebayasi 		} else if (qtd->control_phase == DWC2_CONTROL_STATUS) {
1134578f812dSuebayasi 			urb->status = 0;
1135578f812dSuebayasi 			*xfer_done = 1;
1136578f812dSuebayasi 		}
1137578f812dSuebayasi 		/* No handling for SETUP stage */
1138578f812dSuebayasi 	} else {
1139578f812dSuebayasi 		/* BULK and INTR */
1140578f812dSuebayasi 		urb->actual_length += n_bytes - remain;
1141578f812dSuebayasi 		dev_vdbg(hsotg->dev, "length=%d actual=%d\n", urb->length,
1142578f812dSuebayasi 			 urb->actual_length);
1143578f812dSuebayasi 		if (remain || urb->actual_length >= urb->length) {
1144578f812dSuebayasi 			urb->status = 0;
1145578f812dSuebayasi 			*xfer_done = 1;
1146578f812dSuebayasi 		}
1147578f812dSuebayasi 	}
1148578f812dSuebayasi 
1149578f812dSuebayasi 	return 0;
1150578f812dSuebayasi }
1151578f812dSuebayasi 
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)115205c50565Suebayasi STATIC int dwc2_process_non_isoc_desc(struct dwc2_hsotg *hsotg,
1153578f812dSuebayasi 				      struct dwc2_host_chan *chan,
1154578f812dSuebayasi 				      int chnum, struct dwc2_qtd *qtd,
1155578f812dSuebayasi 				      int desc_num,
1156578f812dSuebayasi 				      enum dwc2_halt_status halt_status,
1157578f812dSuebayasi 				      int *xfer_done)
1158578f812dSuebayasi {
1159578f812dSuebayasi 	struct dwc2_qh *qh = chan->qh;
1160578f812dSuebayasi 	struct dwc2_hcd_urb *urb = qtd->urb;
1161*a9beb1edSmglocker 	struct dwc2_dma_desc *dma_desc;
1162578f812dSuebayasi 	u32 n_bytes;
1163578f812dSuebayasi 	int failed;
1164578f812dSuebayasi 
1165578f812dSuebayasi 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
1166578f812dSuebayasi 
1167578f812dSuebayasi 	if (!urb)
1168578f812dSuebayasi 		return -EINVAL;
1169578f812dSuebayasi 
1170d05ae140Smglocker 	usb_syncmem(&qh->desc_list_usbdma,
1171*a9beb1edSmglocker 	    (desc_num * sizeof(struct dwc2_dma_desc)),
1172*a9beb1edSmglocker 	    sizeof(struct dwc2_dma_desc),
1173d05ae140Smglocker 	    BUS_DMASYNC_POSTREAD);
1174d05ae140Smglocker 
1175578f812dSuebayasi 	dma_desc = &qh->desc_list[desc_num];
1176578f812dSuebayasi 	n_bytes = qh->n_bytes[desc_num];
1177578f812dSuebayasi 	dev_vdbg(hsotg->dev,
1178578f812dSuebayasi 		 "qtd=%p dwc2_urb=%p desc_num=%d desc=%p n_bytes=%d\n",
1179578f812dSuebayasi 		 qtd, urb, desc_num, dma_desc, n_bytes);
1180578f812dSuebayasi 	failed = dwc2_update_non_isoc_urb_state_ddma(hsotg, chan, qtd, dma_desc,
1181578f812dSuebayasi 						     halt_status, n_bytes,
1182578f812dSuebayasi 						     xfer_done);
1183*a9beb1edSmglocker 	if (failed || (*xfer_done && urb->status != -EINPROGRESS)) {
1184578f812dSuebayasi 		dwc2_host_complete(hsotg, qtd, urb->status);
1185578f812dSuebayasi 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1186*a9beb1edSmglocker 		dev_vdbg(hsotg->dev, "failed=%1x xfer_done=%1x\n",
1187*a9beb1edSmglocker 			 failed, *xfer_done);
1188578f812dSuebayasi 		return failed;
1189578f812dSuebayasi 	}
1190578f812dSuebayasi 
1191578f812dSuebayasi 	if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1192578f812dSuebayasi 		switch (qtd->control_phase) {
1193578f812dSuebayasi 		case DWC2_CONTROL_SETUP:
1194578f812dSuebayasi 			if (urb->length > 0)
1195578f812dSuebayasi 				qtd->control_phase = DWC2_CONTROL_DATA;
1196578f812dSuebayasi 			else
1197578f812dSuebayasi 				qtd->control_phase = DWC2_CONTROL_STATUS;
1198578f812dSuebayasi 			dev_vdbg(hsotg->dev,
1199578f812dSuebayasi 				 "  Control setup transaction done\n");
1200578f812dSuebayasi 			break;
1201578f812dSuebayasi 		case DWC2_CONTROL_DATA:
1202578f812dSuebayasi 			if (*xfer_done) {
1203578f812dSuebayasi 				qtd->control_phase = DWC2_CONTROL_STATUS;
1204578f812dSuebayasi 				dev_vdbg(hsotg->dev,
1205578f812dSuebayasi 					 "  Control data transfer done\n");
1206578f812dSuebayasi 			} else if (desc_num + 1 == qtd->n_desc) {
1207578f812dSuebayasi 				/*
1208578f812dSuebayasi 				 * Last descriptor for Control data stage which
1209578f812dSuebayasi 				 * is not completed yet
1210578f812dSuebayasi 				 */
1211578f812dSuebayasi 				dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
1212578f812dSuebayasi 							  qtd);
1213578f812dSuebayasi 			}
1214578f812dSuebayasi 			break;
1215578f812dSuebayasi 		default:
1216578f812dSuebayasi 			break;
1217578f812dSuebayasi 		}
1218578f812dSuebayasi 	}
1219578f812dSuebayasi 
1220578f812dSuebayasi 	return 0;
1221578f812dSuebayasi }
1222578f812dSuebayasi 
dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,int chnum,enum dwc2_halt_status halt_status)122305c50565Suebayasi STATIC void dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
1224578f812dSuebayasi 					     struct dwc2_host_chan *chan,
1225578f812dSuebayasi 					     int chnum,
1226578f812dSuebayasi 					     enum dwc2_halt_status halt_status)
1227578f812dSuebayasi {
1228d05ae140Smglocker 	struct list_head *qtd_item, *qtd_tmp;
1229578f812dSuebayasi 	struct dwc2_qh *qh = chan->qh;
1230d05ae140Smglocker 	struct dwc2_qtd *qtd = NULL;
1231578f812dSuebayasi 	int xfer_done;
1232578f812dSuebayasi 	int desc_num = 0;
1233578f812dSuebayasi 
1234578f812dSuebayasi 	if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
1235d05ae140Smglocker 		list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
1236578f812dSuebayasi 			qtd->in_process = 0;
1237578f812dSuebayasi 		return;
1238578f812dSuebayasi 	}
1239578f812dSuebayasi 
1240d05ae140Smglocker 	list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
1241578f812dSuebayasi 		int i;
1242*a9beb1edSmglocker 		int qtd_desc_count;
1243578f812dSuebayasi 
1244d05ae140Smglocker 		qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
1245578f812dSuebayasi 		xfer_done = 0;
1246*a9beb1edSmglocker 		qtd_desc_count = qtd->n_desc;
1247578f812dSuebayasi 
1248*a9beb1edSmglocker 		for (i = 0; i < qtd_desc_count; i++) {
1249578f812dSuebayasi 			if (dwc2_process_non_isoc_desc(hsotg, chan, chnum, qtd,
1250578f812dSuebayasi 						       desc_num, halt_status,
1251578f812dSuebayasi 						       &xfer_done)) {
1252578f812dSuebayasi 				qtd = NULL;
1253*a9beb1edSmglocker 				goto stop_scan;
1254578f812dSuebayasi 			}
1255*a9beb1edSmglocker 
1256578f812dSuebayasi 			desc_num++;
1257578f812dSuebayasi 		}
1258578f812dSuebayasi 	}
1259578f812dSuebayasi 
1260*a9beb1edSmglocker stop_scan:
1261578f812dSuebayasi 	if (qh->ep_type != USB_ENDPOINT_XFER_CONTROL) {
1262578f812dSuebayasi 		/*
1263578f812dSuebayasi 		 * Resetting the data toggle for bulk and interrupt endpoints
1264578f812dSuebayasi 		 * in case of stall. See handle_hc_stall_intr().
1265578f812dSuebayasi 		 */
1266578f812dSuebayasi 		if (halt_status == DWC2_HC_XFER_STALL)
1267578f812dSuebayasi 			qh->data_toggle = DWC2_HC_PID_DATA0;
1268*a9beb1edSmglocker 		else
1269*a9beb1edSmglocker 			dwc2_hcd_save_data_toggle(hsotg, chan, chnum, NULL);
1270578f812dSuebayasi 	}
1271578f812dSuebayasi 
1272578f812dSuebayasi 	if (halt_status == DWC2_HC_XFER_COMPLETE) {
1273578f812dSuebayasi 		if (chan->hcint & HCINTMSK_NYET) {
1274578f812dSuebayasi 			/*
1275578f812dSuebayasi 			 * Got a NYET on the last transaction of the transfer.
1276578f812dSuebayasi 			 * It means that the endpoint should be in the PING
1277578f812dSuebayasi 			 * state at the beginning of the next transfer.
1278578f812dSuebayasi 			 */
1279578f812dSuebayasi 			qh->ping_state = 1;
1280578f812dSuebayasi 		}
1281578f812dSuebayasi 	}
1282578f812dSuebayasi }
1283578f812dSuebayasi 
1284578f812dSuebayasi /**
1285578f812dSuebayasi  * dwc2_hcd_complete_xfer_ddma() - Scans the descriptor list, updates URB's
1286578f812dSuebayasi  * status and calls completion routine for the URB if it's done. Called from
1287578f812dSuebayasi  * interrupt handlers.
1288578f812dSuebayasi  *
1289578f812dSuebayasi  * @hsotg:       The HCD state structure for the DWC OTG controller
1290578f812dSuebayasi  * @chan:        Host channel the transfer is completed on
1291578f812dSuebayasi  * @chnum:       Index of Host channel registers
1292578f812dSuebayasi  * @halt_status: Reason the channel is being halted or just XferComplete
1293578f812dSuebayasi  *               for isochronous transfers
1294578f812dSuebayasi  *
1295578f812dSuebayasi  * Releases the channel to be used by other transfers.
1296578f812dSuebayasi  * In case of Isochronous endpoint the channel is not halted until the end of
1297578f812dSuebayasi  * the session, i.e. QTD list is empty.
1298578f812dSuebayasi  * If periodic channel released the FrameList is updated accordingly.
1299578f812dSuebayasi  * Calls transaction selection routines to activate pending transfers.
1300578f812dSuebayasi  */
dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg * hsotg,struct dwc2_host_chan * chan,int chnum,enum dwc2_halt_status halt_status)1301578f812dSuebayasi void dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg *hsotg,
1302578f812dSuebayasi 				 struct dwc2_host_chan *chan, int chnum,
1303578f812dSuebayasi 				 enum dwc2_halt_status halt_status)
1304578f812dSuebayasi {
1305578f812dSuebayasi 	struct dwc2_qh *qh = chan->qh;
1306578f812dSuebayasi 	int continue_isoc_xfer = 0;
1307578f812dSuebayasi 	enum dwc2_transaction_type tr_type;
1308578f812dSuebayasi 
1309578f812dSuebayasi 	if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1310578f812dSuebayasi 		dwc2_complete_isoc_xfer_ddma(hsotg, chan, halt_status);
1311578f812dSuebayasi 
1312578f812dSuebayasi 		/* Release the channel if halted or session completed */
1313578f812dSuebayasi 		if (halt_status != DWC2_HC_XFER_COMPLETE ||
1314d05ae140Smglocker 		    list_empty(&qh->qtd_list)) {
1315d05ae140Smglocker 			struct dwc2_qtd *qtd, *qtd_tmp;
1316d05ae140Smglocker 
1317d05ae140Smglocker 			/*
1318d05ae140Smglocker 			 * Kill all remainings QTDs since channel has been
1319d05ae140Smglocker 			 * halted.
1320d05ae140Smglocker 			 */
1321d05ae140Smglocker 			list_for_each_entry_safe(qtd, qtd_tmp,
1322d05ae140Smglocker 						 &qh->qtd_list,
1323d05ae140Smglocker 						 qtd_list_entry) {
1324d05ae140Smglocker 				dwc2_host_complete(hsotg, qtd,
1325d05ae140Smglocker 						   -ECONNRESET);
1326d05ae140Smglocker 				dwc2_hcd_qtd_unlink_and_free(hsotg,
1327d05ae140Smglocker 							     qtd, qh);
1328d05ae140Smglocker 			}
1329d05ae140Smglocker 
1330578f812dSuebayasi 			/* Halt the channel if session completed */
1331578f812dSuebayasi 			if (halt_status == DWC2_HC_XFER_COMPLETE)
1332578f812dSuebayasi 				dwc2_hc_halt(hsotg, chan, halt_status);
1333578f812dSuebayasi 			dwc2_release_channel_ddma(hsotg, qh);
1334578f812dSuebayasi 			dwc2_hcd_qh_unlink(hsotg, qh);
1335578f812dSuebayasi 		} else {
1336578f812dSuebayasi 			/* Keep in assigned schedule to continue transfer */
1337*a9beb1edSmglocker 			list_move_tail(&qh->qh_list_entry,
1338d05ae140Smglocker 				       &hsotg->periodic_sched_assigned);
1339d05ae140Smglocker 			/*
1340d05ae140Smglocker 			 * If channel has been halted during giveback of urb
1341d05ae140Smglocker 			 * then prevent any new scheduling.
1342d05ae140Smglocker 			 */
1343d05ae140Smglocker 			if (!chan->halt_status)
1344578f812dSuebayasi 				continue_isoc_xfer = 1;
1345578f812dSuebayasi 		}
1346578f812dSuebayasi 		/*
1347578f812dSuebayasi 		 * Todo: Consider the case when period exceeds FrameList size.
1348578f812dSuebayasi 		 * Frame Rollover interrupt should be used.
1349578f812dSuebayasi 		 */
1350578f812dSuebayasi 	} else {
1351578f812dSuebayasi 		/*
1352578f812dSuebayasi 		 * Scan descriptor list to complete the URB(s), then release
1353578f812dSuebayasi 		 * the channel
1354578f812dSuebayasi 		 */
1355578f812dSuebayasi 		dwc2_complete_non_isoc_xfer_ddma(hsotg, chan, chnum,
1356578f812dSuebayasi 						 halt_status);
1357578f812dSuebayasi 		dwc2_release_channel_ddma(hsotg, qh);
1358578f812dSuebayasi 		dwc2_hcd_qh_unlink(hsotg, qh);
1359578f812dSuebayasi 
1360d05ae140Smglocker 		if (!list_empty(&qh->qtd_list)) {
1361578f812dSuebayasi 			/*
1362578f812dSuebayasi 			 * Add back to inactive non-periodic schedule on normal
1363578f812dSuebayasi 			 * completion
1364578f812dSuebayasi 			 */
1365578f812dSuebayasi 			dwc2_hcd_qh_add(hsotg, qh);
1366578f812dSuebayasi 		}
1367578f812dSuebayasi 	}
1368578f812dSuebayasi 
1369578f812dSuebayasi 	tr_type = dwc2_hcd_select_transactions(hsotg);
1370578f812dSuebayasi 	if (tr_type != DWC2_TRANSACTION_NONE || continue_isoc_xfer) {
1371578f812dSuebayasi 		if (continue_isoc_xfer) {
1372578f812dSuebayasi 			if (tr_type == DWC2_TRANSACTION_NONE)
1373578f812dSuebayasi 				tr_type = DWC2_TRANSACTION_PERIODIC;
1374578f812dSuebayasi 			else if (tr_type == DWC2_TRANSACTION_NON_PERIODIC)
1375578f812dSuebayasi 				tr_type = DWC2_TRANSACTION_ALL;
1376578f812dSuebayasi 		}
1377578f812dSuebayasi 		dwc2_hcd_queue_transactions(hsotg, tr_type);
1378578f812dSuebayasi 	}
1379578f812dSuebayasi }
1380