1*1b737461Sskrll /*	$NetBSD: dwc2_hcdqueue.c,v 1.16 2021/12/21 09:51:22 skrll Exp $	*/
2e30d28aaSskrll 
3de63295cSskrll /*
4de63295cSskrll  * hcd_queue.c - DesignWare HS OTG Controller host queuing routines
5de63295cSskrll  *
6de63295cSskrll  * Copyright (C) 2004-2013 Synopsys, Inc.
7de63295cSskrll  *
8de63295cSskrll  * Redistribution and use in source and binary forms, with or without
9de63295cSskrll  * modification, are permitted provided that the following conditions
10de63295cSskrll  * are met:
11de63295cSskrll  * 1. Redistributions of source code must retain the above copyright
12de63295cSskrll  *    notice, this list of conditions, and the following disclaimer,
13de63295cSskrll  *    without modification.
14de63295cSskrll  * 2. Redistributions in binary form must reproduce the above copyright
15de63295cSskrll  *    notice, this list of conditions and the following disclaimer in the
16de63295cSskrll  *    documentation and/or other materials provided with the distribution.
17de63295cSskrll  * 3. The names of the above-listed copyright holders may not be used
18de63295cSskrll  *    to endorse or promote products derived from this software without
19de63295cSskrll  *    specific prior written permission.
20de63295cSskrll  *
21de63295cSskrll  * ALTERNATIVELY, this software may be distributed under the terms of the
22de63295cSskrll  * GNU General Public License ("GPL") as published by the Free Software
23de63295cSskrll  * Foundation; either version 2 of the License, or (at your option) any
24de63295cSskrll  * later version.
25de63295cSskrll  *
26de63295cSskrll  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27de63295cSskrll  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28de63295cSskrll  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29de63295cSskrll  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30de63295cSskrll  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31de63295cSskrll  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32de63295cSskrll  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33de63295cSskrll  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34de63295cSskrll  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35de63295cSskrll  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36de63295cSskrll  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37de63295cSskrll  */
38de63295cSskrll 
39de63295cSskrll /*
40de63295cSskrll  * This file contains the functions to manage Queue Heads and Queue
41de63295cSskrll  * Transfer Descriptors for Host mode
42de63295cSskrll  */
435f137d9bSskrll 
445f137d9bSskrll #include <sys/cdefs.h>
45*1b737461Sskrll __KERNEL_RCSID(0, "$NetBSD: dwc2_hcdqueue.c,v 1.16 2021/12/21 09:51:22 skrll Exp $");
465f137d9bSskrll 
475f137d9bSskrll #include <sys/types.h>
485f137d9bSskrll #include <sys/kmem.h>
495f137d9bSskrll #include <sys/pool.h>
505f137d9bSskrll 
515f137d9bSskrll #include <dev/usb/usb.h>
525f137d9bSskrll #include <dev/usb/usbdi.h>
535f137d9bSskrll #include <dev/usb/usbdivar.h>
545f137d9bSskrll #include <dev/usb/usb_mem.h>
555f137d9bSskrll 
565f137d9bSskrll #include <machine/param.h>
575f137d9bSskrll 
58de63295cSskrll #include <linux/kernel.h>
59de63295cSskrll 
605f137d9bSskrll #include <dwc2/dwc2.h>
615f137d9bSskrll #include <dwc2/dwc2var.h>
62de63295cSskrll 
635f137d9bSskrll #include "dwc2_core.h"
645f137d9bSskrll #include "dwc2_hcd.h"
655f137d9bSskrll 
665f137d9bSskrll static u32 dwc2_calc_bus_time(struct dwc2_hsotg *, int, int, int, int);
6743e795b1Ssimonb static void dwc2_wait_timer_fn(void *);
6843e795b1Ssimonb 
6943e795b1Ssimonb /* If we get a NAK, wait this long before retrying */
7043e795b1Ssimonb #define DWC2_RETRY_WAIT_DELAY 1	/* msec */
71de63295cSskrll 
72de63295cSskrll /**
73de63295cSskrll  * dwc2_qh_init() - Initializes a QH structure
74de63295cSskrll  *
75de63295cSskrll  * @hsotg: The HCD state structure for the DWC OTG controller
76de63295cSskrll  * @qh:    The QH to init
77de63295cSskrll  * @urb:   Holds the information about the device/endpoint needed to initialize
78de63295cSskrll  *         the QH
79de63295cSskrll  */
80de63295cSskrll #define SCHEDULE_SLOP 10
dwc2_qh_init(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,struct dwc2_hcd_urb * urb)81de63295cSskrll static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
82de63295cSskrll 			 struct dwc2_hcd_urb *urb)
83de63295cSskrll {
84de63295cSskrll 	int dev_speed, hub_addr, hub_port;
85de63295cSskrll 
86de63295cSskrll 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
87de63295cSskrll 
88de63295cSskrll 	/* Initialize QH */
8943e795b1Ssimonb 	qh->hsotg = hsotg;
9043e795b1Ssimonb 	/* XXX timer_setup(&qh->wait_timer, dwc2_wait_timer_fn, 0); */
9143e795b1Ssimonb 	callout_init(&qh->wait_timer, 0);
9243e795b1Ssimonb 	callout_setfunc(&qh->wait_timer, dwc2_wait_timer_fn, qh);
93de63295cSskrll 	qh->ep_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
94de63295cSskrll 	qh->ep_is_in = dwc2_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
95de63295cSskrll 
96de63295cSskrll 	qh->data_toggle = DWC2_HC_PID_DATA0;
97de63295cSskrll 	qh->maxp = dwc2_hcd_get_mps(&urb->pipe_info);
98de63295cSskrll 	INIT_LIST_HEAD(&qh->qtd_list);
99de63295cSskrll 	INIT_LIST_HEAD(&qh->qh_list_entry);
100de63295cSskrll 
101de63295cSskrll 	/* FS/LS Endpoint on HS Hub, NOT virtual root hub */
102de63295cSskrll 	dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
103de63295cSskrll 
104de63295cSskrll 	dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
105f99b2d93Sskrll 	qh->nak_frame = 0xffff;
106de63295cSskrll 
107de63295cSskrll 	if ((dev_speed == USB_SPEED_LOW || dev_speed == USB_SPEED_FULL) &&
108de63295cSskrll 	    hub_addr != 0 && hub_addr != 1) {
109de63295cSskrll 		dev_vdbg(hsotg->dev,
110de63295cSskrll 			 "QH init: EP %d: TT found at hub addr %d, for port %d\n",
111de63295cSskrll 			 dwc2_hcd_get_ep_num(&urb->pipe_info), hub_addr,
112de63295cSskrll 			 hub_port);
113de63295cSskrll 		qh->do_split = 1;
114de63295cSskrll 	}
115de63295cSskrll 
116de63295cSskrll 	if (qh->ep_type == USB_ENDPOINT_XFER_INT ||
117de63295cSskrll 	    qh->ep_type == USB_ENDPOINT_XFER_ISOC) {
118de63295cSskrll 		/* Compute scheduling parameters once and save them */
119de63295cSskrll 		u32 hprt, prtspd;
120de63295cSskrll 
121de63295cSskrll 		/* Todo: Account for split transfers in the bus time */
122de63295cSskrll 		int bytecount =
123de63295cSskrll 			dwc2_hb_mult(qh->maxp) * dwc2_max_packet(qh->maxp);
124de63295cSskrll 
1255f137d9bSskrll 		qh->usecs = dwc2_calc_bus_time(hsotg, qh->do_split ?
126de63295cSskrll 				USB_SPEED_HIGH : dev_speed, qh->ep_is_in,
127de63295cSskrll 				qh->ep_type == USB_ENDPOINT_XFER_ISOC,
1285f137d9bSskrll 				bytecount);
1295edb980dSskrll 
1305edb980dSskrll 		/* Ensure frame_number corresponds to the reality */
1315edb980dSskrll 		hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
132de63295cSskrll 		/* Start in a slightly future (micro)frame */
133de63295cSskrll 		qh->sched_frame = dwc2_frame_num_inc(hsotg->frame_number,
134de63295cSskrll 						     SCHEDULE_SLOP);
135de63295cSskrll 		qh->interval = urb->interval;
136de63295cSskrll #if 0
137de63295cSskrll 		/* Increase interrupt polling rate for debugging */
138de63295cSskrll 		if (qh->ep_type == USB_ENDPOINT_XFER_INT)
139de63295cSskrll 			qh->interval = 8;
140de63295cSskrll #endif
1415f137d9bSskrll 		hprt = DWC2_READ_4(hsotg, HPRT0);
14201999631Sskrll 		prtspd = (hprt & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
143de63295cSskrll 		if (prtspd == HPRT0_SPD_HIGH_SPEED &&
144de63295cSskrll 		    (dev_speed == USB_SPEED_LOW ||
145de63295cSskrll 		     dev_speed == USB_SPEED_FULL)) {
146de63295cSskrll 			qh->interval *= 8;
147de63295cSskrll 			qh->sched_frame |= 0x7;
148de63295cSskrll 			qh->start_split_frame = qh->sched_frame;
149de63295cSskrll 		}
150de63295cSskrll 		dev_dbg(hsotg->dev, "interval=%d\n", qh->interval);
151de63295cSskrll 	}
152de63295cSskrll 
153de63295cSskrll 	dev_vdbg(hsotg->dev, "DWC OTG HCD QH Initialized\n");
154de63295cSskrll 	dev_vdbg(hsotg->dev, "DWC OTG HCD QH - qh = %p\n", qh);
155de63295cSskrll 	dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Device Address = %d\n",
156de63295cSskrll 		 dwc2_hcd_get_dev_addr(&urb->pipe_info));
157de63295cSskrll 	dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Endpoint %d, %s\n",
158de63295cSskrll 		 dwc2_hcd_get_ep_num(&urb->pipe_info),
159de63295cSskrll 		 dwc2_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
160de63295cSskrll 
161de63295cSskrll 	qh->dev_speed = dev_speed;
162de63295cSskrll 
163bb751261Sskrll #ifdef DWC2_DEBUG
164bb751261Sskrll 	const char *speed, *type;
165de63295cSskrll 	switch (dev_speed) {
166de63295cSskrll 	case USB_SPEED_LOW:
167de63295cSskrll 		speed = "low";
168de63295cSskrll 		break;
169de63295cSskrll 	case USB_SPEED_FULL:
170de63295cSskrll 		speed = "full";
171de63295cSskrll 		break;
172de63295cSskrll 	case USB_SPEED_HIGH:
173de63295cSskrll 		speed = "high";
174de63295cSskrll 		break;
175de63295cSskrll 	default:
176de63295cSskrll 		speed = "?";
177de63295cSskrll 		break;
178de63295cSskrll 	}
179de63295cSskrll 	dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Speed = %s\n", speed);
180de63295cSskrll 
181de63295cSskrll 	switch (qh->ep_type) {
182de63295cSskrll 	case USB_ENDPOINT_XFER_ISOC:
183de63295cSskrll 		type = "isochronous";
184de63295cSskrll 		break;
185de63295cSskrll 	case USB_ENDPOINT_XFER_INT:
186de63295cSskrll 		type = "interrupt";
187de63295cSskrll 		break;
188de63295cSskrll 	case USB_ENDPOINT_XFER_CONTROL:
189de63295cSskrll 		type = "control";
190de63295cSskrll 		break;
191de63295cSskrll 	case USB_ENDPOINT_XFER_BULK:
192de63295cSskrll 		type = "bulk";
193de63295cSskrll 		break;
194de63295cSskrll 	default:
195de63295cSskrll 		type = "?";
196de63295cSskrll 		break;
197de63295cSskrll 	}
198de63295cSskrll 
199de63295cSskrll 	dev_vdbg(hsotg->dev, "DWC OTG HCD QH - Type = %s\n", type);
200bb751261Sskrll #endif
201de63295cSskrll 
202de63295cSskrll 	if (qh->ep_type == USB_ENDPOINT_XFER_INT) {
203de63295cSskrll 		dev_vdbg(hsotg->dev, "DWC OTG HCD QH - usecs = %d\n",
204de63295cSskrll 			 qh->usecs);
205de63295cSskrll 		dev_vdbg(hsotg->dev, "DWC OTG HCD QH - interval = %d\n",
206de63295cSskrll 			 qh->interval);
207de63295cSskrll 	}
208de63295cSskrll }
209de63295cSskrll 
210de63295cSskrll /**
211de63295cSskrll  * dwc2_hcd_qh_create() - Allocates and initializes a QH
212de63295cSskrll  *
213de63295cSskrll  * @hsotg:     The HCD state structure for the DWC OTG controller
214de63295cSskrll  * @urb:       Holds the information about the device/endpoint needed
215de63295cSskrll  *             to initialize the QH
21601999631Sskrll  * @mem_flags: Flag to do atomic allocation if needed
217de63295cSskrll  *
218de63295cSskrll  * Return: Pointer to the newly allocated QH, or NULL on error
219de63295cSskrll  */
dwc2_hcd_qh_create(struct dwc2_hsotg * hsotg,struct dwc2_hcd_urb * urb,gfp_t mem_flags)220b4858ac2Sskrll struct dwc2_qh *dwc2_hcd_qh_create(struct dwc2_hsotg *hsotg,
221de63295cSskrll 					  struct dwc2_hcd_urb *urb,
222de63295cSskrll 					  gfp_t mem_flags)
223de63295cSskrll {
2245f137d9bSskrll 	struct dwc2_softc *sc = hsotg->hsotg_sc;
225de63295cSskrll 	struct dwc2_qh *qh;
226de63295cSskrll 
227de63295cSskrll 	if (!urb->priv)
228de63295cSskrll 		return NULL;
229de63295cSskrll 
230de63295cSskrll 	/* Allocate memory */
2315f137d9bSskrll 	qh = pool_cache_get(sc->sc_qhpool, PR_NOWAIT);
232de63295cSskrll 	if (!qh)
233de63295cSskrll 		return NULL;
234de63295cSskrll 
2355f137d9bSskrll 	memset(qh, 0, sizeof(*qh));
236de63295cSskrll 	dwc2_qh_init(hsotg, qh, urb);
237de63295cSskrll 
238de63295cSskrll 	if (hsotg->core_params->dma_desc_enable > 0 &&
239de63295cSskrll 	    dwc2_hcd_qh_init_ddma(hsotg, qh, mem_flags) < 0) {
240de63295cSskrll 		dwc2_hcd_qh_free(hsotg, qh);
241de63295cSskrll 		return NULL;
242de63295cSskrll 	}
243de63295cSskrll 
244de63295cSskrll 	return qh;
245de63295cSskrll }
246de63295cSskrll 
247de63295cSskrll /**
248de63295cSskrll  * dwc2_hcd_qh_free() - Frees the QH
249de63295cSskrll  *
250de63295cSskrll  * @hsotg: HCD instance
251de63295cSskrll  * @qh:    The QH to free
252de63295cSskrll  *
253de63295cSskrll  * QH should already be removed from the list. QTD list should already be empty
254de63295cSskrll  * if called from URB Dequeue.
255de63295cSskrll  *
256de63295cSskrll  * Must NOT be called with interrupt disabled or spinlock held
257de63295cSskrll  */
dwc2_hcd_qh_free(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)258de63295cSskrll void dwc2_hcd_qh_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
259de63295cSskrll {
2605f137d9bSskrll 	struct dwc2_softc *sc = hsotg->hsotg_sc;
26143e795b1Ssimonb 
26243e795b1Ssimonb 	/*
26343e795b1Ssimonb 	 * We don't have the lock so we can safely wait until the wait timer
26443e795b1Ssimonb 	 * finishes.  Of course, at this point in time we'd better have set
26543e795b1Ssimonb 	 * wait_timer_active to false so if this timer was still pending it
26643e795b1Ssimonb 	 * won't do anything anyway, but we want it to finish before we free
26743e795b1Ssimonb 	 * memory.
26843e795b1Ssimonb 	 */
26943e795b1Ssimonb 	/* XXX del_timer_sync(&qh->wait_timer); */
27043e795b1Ssimonb 	callout_destroy(&qh->wait_timer);	/* XXX need to callout_halt() first? */
27143e795b1Ssimonb 
2725edb980dSskrll 	if (qh->desc_list) {
273de63295cSskrll 		dwc2_hcd_qh_free_ddma(hsotg, qh);
274de63295cSskrll 	} else if (qh->dw_align_buf) {
275*1b737461Sskrll 		usb_freemem(&qh->dw_align_buf_usbdma);
276b4858ac2Sskrll  		qh->dw_align_buf_dma = (dma_addr_t)0;
277de63295cSskrll 	}
278de63295cSskrll 
2795f137d9bSskrll 	pool_cache_put(sc->sc_qhpool, qh);
280de63295cSskrll }
281de63295cSskrll 
282de63295cSskrll /**
283de63295cSskrll  * dwc2_periodic_channel_available() - Checks that a channel is available for a
284de63295cSskrll  * periodic transfer
285de63295cSskrll  *
286de63295cSskrll  * @hsotg: The HCD state structure for the DWC OTG controller
287de63295cSskrll  *
28801999631Sskrll  * Return: 0 if successful, negative error code otherwise
289de63295cSskrll  */
dwc2_periodic_channel_available(struct dwc2_hsotg * hsotg)290de63295cSskrll static int dwc2_periodic_channel_available(struct dwc2_hsotg *hsotg)
291de63295cSskrll {
292de63295cSskrll 	/*
29301999631Sskrll 	 * Currently assuming that there is a dedicated host channel for
294de63295cSskrll 	 * each periodic transaction plus at least one host channel for
295de63295cSskrll 	 * non-periodic transactions
296de63295cSskrll 	 */
297de63295cSskrll 	int status;
298de63295cSskrll 	int num_channels;
299de63295cSskrll 
300de63295cSskrll 	num_channels = hsotg->core_params->host_channels;
301de63295cSskrll 	if (hsotg->periodic_channels + hsotg->non_periodic_channels <
302de63295cSskrll 								num_channels
303de63295cSskrll 	    && hsotg->periodic_channels < num_channels - 1) {
304de63295cSskrll 		status = 0;
305de63295cSskrll 	} else {
306de63295cSskrll 		dev_dbg(hsotg->dev,
307de63295cSskrll 			"%s: Total channels: %d, Periodic: %d, "
308de63295cSskrll 			"Non-periodic: %d\n", __func__, num_channels,
309de63295cSskrll 			hsotg->periodic_channels, hsotg->non_periodic_channels);
310de63295cSskrll 		status = -ENOSPC;
311de63295cSskrll 	}
312de63295cSskrll 
313de63295cSskrll 	return status;
314de63295cSskrll }
315de63295cSskrll 
316de63295cSskrll /**
317de63295cSskrll  * dwc2_check_periodic_bandwidth() - Checks that there is sufficient bandwidth
318de63295cSskrll  * for the specified QH in the periodic schedule
319de63295cSskrll  *
320de63295cSskrll  * @hsotg: The HCD state structure for the DWC OTG controller
321de63295cSskrll  * @qh:    QH containing periodic bandwidth required
322de63295cSskrll  *
323de63295cSskrll  * Return: 0 if successful, negative error code otherwise
324de63295cSskrll  *
325de63295cSskrll  * For simplicity, this calculation assumes that all the transfers in the
326de63295cSskrll  * periodic schedule may occur in the same (micro)frame
327de63295cSskrll  */
dwc2_check_periodic_bandwidth(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)328de63295cSskrll static int dwc2_check_periodic_bandwidth(struct dwc2_hsotg *hsotg,
329de63295cSskrll 					 struct dwc2_qh *qh)
330de63295cSskrll {
331de63295cSskrll 	int status;
332de63295cSskrll 	s16 max_claimed_usecs;
333de63295cSskrll 
334de63295cSskrll 	status = 0;
335de63295cSskrll 
336de63295cSskrll 	if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
337de63295cSskrll 		/*
338de63295cSskrll 		 * High speed mode
339de63295cSskrll 		 * Max periodic usecs is 80% x 125 usec = 100 usec
340de63295cSskrll 		 */
341de63295cSskrll 		max_claimed_usecs = 100 - qh->usecs;
342de63295cSskrll 	} else {
343de63295cSskrll 		/*
344de63295cSskrll 		 * Full speed mode
345de63295cSskrll 		 * Max periodic usecs is 90% x 1000 usec = 900 usec
346de63295cSskrll 		 */
347de63295cSskrll 		max_claimed_usecs = 900 - qh->usecs;
348de63295cSskrll 	}
349de63295cSskrll 
350de63295cSskrll 	if (hsotg->periodic_usecs > max_claimed_usecs) {
351de63295cSskrll 		dev_err(hsotg->dev,
352de63295cSskrll 			"%s: already claimed usecs %d, required usecs %d\n",
353de63295cSskrll 			__func__, hsotg->periodic_usecs, qh->usecs);
354de63295cSskrll 		status = -ENOSPC;
355de63295cSskrll 	}
356de63295cSskrll 
357de63295cSskrll 	return status;
358de63295cSskrll }
359de63295cSskrll 
360de63295cSskrll /**
361de63295cSskrll  * Microframe scheduler
362de63295cSskrll  * track the total use in hsotg->frame_usecs
363de63295cSskrll  * keep each qh use in qh->frame_usecs
364de63295cSskrll  * when surrendering the qh then donate the time back
365de63295cSskrll  */
366de63295cSskrll static const unsigned short max_uframe_usecs[] = {
367de63295cSskrll 	100, 100, 100, 100, 100, 100, 30, 0
368de63295cSskrll };
369de63295cSskrll 
dwc2_hcd_init_usecs(struct dwc2_hsotg * hsotg)370de63295cSskrll void dwc2_hcd_init_usecs(struct dwc2_hsotg *hsotg)
371de63295cSskrll {
372de63295cSskrll 	int i;
373de63295cSskrll 
374de63295cSskrll 	for (i = 0; i < 8; i++)
375de63295cSskrll 		hsotg->frame_usecs[i] = max_uframe_usecs[i];
376de63295cSskrll }
377de63295cSskrll 
dwc2_find_single_uframe(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)378de63295cSskrll static int dwc2_find_single_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
379de63295cSskrll {
380de63295cSskrll 	unsigned short utime = qh->usecs;
3810223a09fSskrll 	int i;
382de63295cSskrll 
3830223a09fSskrll 	for (i = 0; i < 8; i++) {
384de63295cSskrll 		/* At the start hsotg->frame_usecs[i] = max_uframe_usecs[i] */
385de63295cSskrll 		if (utime <= hsotg->frame_usecs[i]) {
386de63295cSskrll 			hsotg->frame_usecs[i] -= utime;
387de63295cSskrll 			qh->frame_usecs[i] += utime;
3880223a09fSskrll 			return i;
389de63295cSskrll 		}
390de63295cSskrll 	}
39114ad0a56Sskrll 	return -ENOSPC;
392de63295cSskrll }
393de63295cSskrll 
394de63295cSskrll /*
395de63295cSskrll  * use this for FS apps that can span multiple uframes
396de63295cSskrll  */
dwc2_find_multi_uframe(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)397de63295cSskrll static int dwc2_find_multi_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
398de63295cSskrll {
399de63295cSskrll 	unsigned short utime = qh->usecs;
400de63295cSskrll 	unsigned short xtime;
4010223a09fSskrll 	int t_left;
4020223a09fSskrll 	int i;
403de63295cSskrll 	int j;
4040223a09fSskrll 	int k;
405de63295cSskrll 
4060223a09fSskrll 	for (i = 0; i < 8; i++) {
4070223a09fSskrll 		if (hsotg->frame_usecs[i] <= 0)
408de63295cSskrll 			continue;
409de63295cSskrll 
410de63295cSskrll 		/*
411de63295cSskrll 		 * we need n consecutive slots so use j as a start slot
412de63295cSskrll 		 * j plus j+1 must be enough time (for now)
413de63295cSskrll 		 */
414de63295cSskrll 		xtime = hsotg->frame_usecs[i];
415de63295cSskrll 		for (j = i + 1; j < 8; j++) {
416de63295cSskrll 			/*
417de63295cSskrll 			 * if we add this frame remaining time to xtime we may
418de63295cSskrll 			 * be OK, if not we need to test j for a complete frame
419de63295cSskrll 			 */
420de63295cSskrll 			if (xtime + hsotg->frame_usecs[j] < utime) {
421de63295cSskrll 				if (hsotg->frame_usecs[j] <
4220223a09fSskrll 							max_uframe_usecs[j])
4230223a09fSskrll 					continue;
424de63295cSskrll 			}
425de63295cSskrll 			if (xtime >= utime) {
4260223a09fSskrll 				t_left = utime;
4270223a09fSskrll 				for (k = i; k < 8; k++) {
4280223a09fSskrll 					t_left -= hsotg->frame_usecs[k];
4290223a09fSskrll 					if (t_left <= 0) {
4300223a09fSskrll 						qh->frame_usecs[k] +=
4310223a09fSskrll 							hsotg->frame_usecs[k]
4320223a09fSskrll 								+ t_left;
4330223a09fSskrll 						hsotg->frame_usecs[k] = -t_left;
4340223a09fSskrll 						return i;
4350223a09fSskrll 					} else {
4360223a09fSskrll 						qh->frame_usecs[k] +=
4370223a09fSskrll 							hsotg->frame_usecs[k];
4380223a09fSskrll 						hsotg->frame_usecs[k] = 0;
4390223a09fSskrll 					}
4400223a09fSskrll 				}
441de63295cSskrll 			}
442de63295cSskrll 			/* add the frame time to x time */
443de63295cSskrll 			xtime += hsotg->frame_usecs[j];
444de63295cSskrll 			/* we must have a fully available next frame or break */
445de63295cSskrll 			if (xtime < utime &&
4460223a09fSskrll 			   hsotg->frame_usecs[j] == max_uframe_usecs[j])
4470223a09fSskrll 				continue;
448de63295cSskrll 		}
449de63295cSskrll 	}
45014ad0a56Sskrll 	return -ENOSPC;
451de63295cSskrll }
452de63295cSskrll 
dwc2_find_uframe(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)453de63295cSskrll static int dwc2_find_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
454de63295cSskrll {
455de63295cSskrll 	int ret;
456de63295cSskrll 
457de63295cSskrll 	if (qh->dev_speed == USB_SPEED_HIGH) {
458de63295cSskrll 		/* if this is a hs transaction we need a full frame */
459de63295cSskrll 		ret = dwc2_find_single_uframe(hsotg, qh);
460de63295cSskrll 	} else {
461de63295cSskrll 		/*
462de63295cSskrll 		 * if this is a fs transaction we may need a sequence
463de63295cSskrll 		 * of frames
464de63295cSskrll 		 */
465de63295cSskrll 		ret = dwc2_find_multi_uframe(hsotg, qh);
466de63295cSskrll 	}
467de63295cSskrll 	return ret;
468de63295cSskrll }
469de63295cSskrll 
470de63295cSskrll /**
471de63295cSskrll  * dwc2_check_max_xfer_size() - Checks that the max transfer size allowed in a
472de63295cSskrll  * host channel is large enough to handle the maximum data transfer in a single
473de63295cSskrll  * (micro)frame for a periodic transfer
474de63295cSskrll  *
475de63295cSskrll  * @hsotg: The HCD state structure for the DWC OTG controller
476de63295cSskrll  * @qh:    QH for a periodic endpoint
477de63295cSskrll  *
478de63295cSskrll  * Return: 0 if successful, negative error code otherwise
479de63295cSskrll  */
dwc2_check_max_xfer_size(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)480de63295cSskrll static int dwc2_check_max_xfer_size(struct dwc2_hsotg *hsotg,
481de63295cSskrll 				    struct dwc2_qh *qh)
482de63295cSskrll {
483de63295cSskrll 	u32 max_xfer_size;
484de63295cSskrll 	u32 max_channel_xfer_size;
485de63295cSskrll 	int status = 0;
486de63295cSskrll 
487de63295cSskrll 	max_xfer_size = dwc2_max_packet(qh->maxp) * dwc2_hb_mult(qh->maxp);
488de63295cSskrll 	max_channel_xfer_size = hsotg->core_params->max_transfer_size;
489de63295cSskrll 
490de63295cSskrll 	if (max_xfer_size > max_channel_xfer_size) {
491de63295cSskrll 		dev_err(hsotg->dev,
492de63295cSskrll 			"%s: Periodic xfer length %d > max xfer length for channel %d\n",
493de63295cSskrll 			__func__, max_xfer_size, max_channel_xfer_size);
494de63295cSskrll 		status = -ENOSPC;
495de63295cSskrll 	}
496de63295cSskrll 
497de63295cSskrll 	return status;
498de63295cSskrll }
499de63295cSskrll 
500de63295cSskrll /**
501de63295cSskrll  * dwc2_schedule_periodic() - Schedules an interrupt or isochronous transfer in
502de63295cSskrll  * the periodic schedule
503de63295cSskrll  *
504de63295cSskrll  * @hsotg: The HCD state structure for the DWC OTG controller
505de63295cSskrll  * @qh:    QH for the periodic transfer. The QH should already contain the
506de63295cSskrll  *         scheduling information.
507de63295cSskrll  *
508de63295cSskrll  * Return: 0 if successful, negative error code otherwise
509de63295cSskrll  */
dwc2_schedule_periodic(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)510de63295cSskrll static int dwc2_schedule_periodic(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
511de63295cSskrll {
512de63295cSskrll 	int status;
513de63295cSskrll 
514de63295cSskrll 	if (hsotg->core_params->uframe_sched > 0) {
515de63295cSskrll 		int frame = -1;
516de63295cSskrll 
517de63295cSskrll 		status = dwc2_find_uframe(hsotg, qh);
518de63295cSskrll 		if (status == 0)
519de63295cSskrll 			frame = 7;
520de63295cSskrll 		else if (status > 0)
521de63295cSskrll 			frame = status - 1;
522de63295cSskrll 
523de63295cSskrll 		/* Set the new frame up */
52414ad0a56Sskrll 		if (frame >= 0) {
525de63295cSskrll 			qh->sched_frame &= ~0x7;
526de63295cSskrll 			qh->sched_frame |= (frame & 7);
527de63295cSskrll 		}
528de63295cSskrll 
52914ad0a56Sskrll 		if (status > 0)
530de63295cSskrll 			status = 0;
531de63295cSskrll 	} else {
532de63295cSskrll 		status = dwc2_periodic_channel_available(hsotg);
533de63295cSskrll 		if (status) {
534de63295cSskrll 			dev_info(hsotg->dev,
535de63295cSskrll 				 "%s: No host channel available for periodic transfer\n",
536de63295cSskrll 				 __func__);
537de63295cSskrll 			return status;
538de63295cSskrll 		}
539de63295cSskrll 
540de63295cSskrll 		status = dwc2_check_periodic_bandwidth(hsotg, qh);
541de63295cSskrll 	}
542de63295cSskrll 
543de63295cSskrll 	if (status) {
544de63295cSskrll 		dev_dbg(hsotg->dev,
545de63295cSskrll 			"%s: Insufficient periodic bandwidth for periodic transfer\n",
546de63295cSskrll 			__func__);
547de63295cSskrll 		return status;
548de63295cSskrll 	}
549de63295cSskrll 
550de63295cSskrll 	status = dwc2_check_max_xfer_size(hsotg, qh);
551de63295cSskrll 	if (status) {
552de63295cSskrll 		dev_dbg(hsotg->dev,
553de63295cSskrll 			"%s: Channel max transfer size too small for periodic transfer\n",
554de63295cSskrll 			__func__);
555de63295cSskrll 		return status;
556de63295cSskrll 	}
557de63295cSskrll 
55801999631Sskrll 	if (hsotg->core_params->dma_desc_enable > 0)
559de63295cSskrll 		/* Don't rely on SOF and start in ready schedule */
560de63295cSskrll 		list_add_tail(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
56101999631Sskrll 	else
562de63295cSskrll 		/* Always start in inactive schedule */
563de63295cSskrll 		list_add_tail(&qh->qh_list_entry,
564de63295cSskrll 			      &hsotg->periodic_sched_inactive);
565de63295cSskrll 
566de63295cSskrll 	if (hsotg->core_params->uframe_sched <= 0)
567de63295cSskrll 		/* Reserve periodic channel */
568de63295cSskrll 		hsotg->periodic_channels++;
569de63295cSskrll 
570de63295cSskrll 	/* Update claimed usecs per (micro)frame */
571de63295cSskrll 	hsotg->periodic_usecs += qh->usecs;
572de63295cSskrll 
573de63295cSskrll 	return status;
574de63295cSskrll }
575de63295cSskrll 
576de63295cSskrll /**
577de63295cSskrll  * dwc2_deschedule_periodic() - Removes an interrupt or isochronous transfer
578de63295cSskrll  * from the periodic schedule
579de63295cSskrll  *
580de63295cSskrll  * @hsotg: The HCD state structure for the DWC OTG controller
581de63295cSskrll  * @qh:	   QH for the periodic transfer
582de63295cSskrll  */
dwc2_deschedule_periodic(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)583de63295cSskrll static void dwc2_deschedule_periodic(struct dwc2_hsotg *hsotg,
584de63295cSskrll 				     struct dwc2_qh *qh)
585de63295cSskrll {
586de63295cSskrll 	int i;
587de63295cSskrll 
588de63295cSskrll 	list_del_init(&qh->qh_list_entry);
589de63295cSskrll 
590de63295cSskrll 	/* Update claimed usecs per (micro)frame */
591de63295cSskrll 	hsotg->periodic_usecs -= qh->usecs;
592de63295cSskrll 
593de63295cSskrll 	if (hsotg->core_params->uframe_sched > 0) {
594de63295cSskrll 		for (i = 0; i < 8; i++) {
595de63295cSskrll 			hsotg->frame_usecs[i] += qh->frame_usecs[i];
596de63295cSskrll 			qh->frame_usecs[i] = 0;
597de63295cSskrll 		}
598de63295cSskrll 	} else {
599de63295cSskrll 		/* Release periodic channel reservation */
600de63295cSskrll 		hsotg->periodic_channels--;
601de63295cSskrll 	}
602de63295cSskrll }
603de63295cSskrll 
604de63295cSskrll /**
60543e795b1Ssimonb  * dwc2_wait_timer_fn() - Timer function to re-queue after waiting
60643e795b1Ssimonb  *
60743e795b1Ssimonb  * As per the spec, a NAK indicates that "a function is temporarily unable to
60843e795b1Ssimonb  * transmit or receive data, but will eventually be able to do so without need
60943e795b1Ssimonb  * of host intervention".
61043e795b1Ssimonb  *
61143e795b1Ssimonb  * That means that when we encounter a NAK we're supposed to retry.
61243e795b1Ssimonb  *
61343e795b1Ssimonb  * ...but if we retry right away (from the interrupt handler that saw the NAK)
61443e795b1Ssimonb  * then we can end up with an interrupt storm (if the other side keeps NAKing
61543e795b1Ssimonb  * us) because on slow enough CPUs it could take us longer to get out of the
61643e795b1Ssimonb  * interrupt routine than it takes for the device to send another NAK.  That
61743e795b1Ssimonb  * leads to a constant stream of NAK interrupts and the CPU locks.
61843e795b1Ssimonb  *
61943e795b1Ssimonb  * ...so instead of retrying right away in the case of a NAK we'll set a timer
62043e795b1Ssimonb  * to retry some time later.  This function handles that timer and moves the
62143e795b1Ssimonb  * qh back to the "inactive" list, then queues transactions.
62243e795b1Ssimonb  *
62343e795b1Ssimonb  * @t: Pointer to wait_timer in a qh.
62443e795b1Ssimonb  */
dwc2_wait_timer_fn(void * arg)62543e795b1Ssimonb static void dwc2_wait_timer_fn(void *arg)
62643e795b1Ssimonb {
62743e795b1Ssimonb 	struct dwc2_qh *qh = arg;
62843e795b1Ssimonb 	struct dwc2_hsotg *hsotg = qh->hsotg;
62943e795b1Ssimonb 	unsigned long flags;
63043e795b1Ssimonb 
63143e795b1Ssimonb 	spin_lock_irqsave(&hsotg->lock, flags);
63243e795b1Ssimonb 
63343e795b1Ssimonb 	/*
63443e795b1Ssimonb 	 * We'll set wait_timer_cancel to true if we want to cancel this
63543e795b1Ssimonb 	 * operation in dwc2_hcd_qh_unlink().
63643e795b1Ssimonb 	 */
63743e795b1Ssimonb 	if (!qh->wait_timer_cancel) {
63843e795b1Ssimonb 		enum dwc2_transaction_type tr_type;
63943e795b1Ssimonb 
64043e795b1Ssimonb 		qh->want_wait = false;
64143e795b1Ssimonb 
64243e795b1Ssimonb 		list_move(&qh->qh_list_entry,
64343e795b1Ssimonb 			  &hsotg->non_periodic_sched_inactive);
64443e795b1Ssimonb 
64543e795b1Ssimonb 		tr_type = dwc2_hcd_select_transactions(hsotg);
64643e795b1Ssimonb 		if (tr_type != DWC2_TRANSACTION_NONE)
64743e795b1Ssimonb 			dwc2_hcd_queue_transactions(hsotg, tr_type);
64843e795b1Ssimonb 	}
64943e795b1Ssimonb 
65043e795b1Ssimonb 	spin_unlock_irqrestore(&hsotg->lock, flags);
65143e795b1Ssimonb }
65243e795b1Ssimonb 
65343e795b1Ssimonb /**
654de63295cSskrll  * dwc2_hcd_qh_add() - Adds a QH to either the non periodic or periodic
655de63295cSskrll  * schedule if it is not already in the schedule. If the QH is already in
656de63295cSskrll  * the schedule, no action is taken.
657de63295cSskrll  *
658de63295cSskrll  * @hsotg: The HCD state structure for the DWC OTG controller
659de63295cSskrll  * @qh:    The QH to add
660de63295cSskrll  *
661de63295cSskrll  * Return: 0 if successful, negative error code otherwise
662de63295cSskrll  */
dwc2_hcd_qh_add(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)663de63295cSskrll int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
664de63295cSskrll {
66514ad0a56Sskrll 	int status;
666de63295cSskrll 	u32 intr_mask;
667de63295cSskrll 
668de63295cSskrll 	if (dbg_qh(qh))
669de63295cSskrll 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
670de63295cSskrll 
671de63295cSskrll 	if (!list_empty(&qh->qh_list_entry))
672de63295cSskrll 		/* QH already in a schedule */
67314ad0a56Sskrll 		return 0;
674de63295cSskrll 
6755edb980dSskrll 	if (!dwc2_frame_num_le(qh->sched_frame, hsotg->frame_number) &&
6765edb980dSskrll 			!hsotg->frame_number) {
6775edb980dSskrll 		dev_dbg(hsotg->dev,
6785edb980dSskrll 				"reset frame number counter\n");
6795edb980dSskrll 		qh->sched_frame = dwc2_frame_num_inc(hsotg->frame_number,
6805edb980dSskrll 				SCHEDULE_SLOP);
6815edb980dSskrll 	}
6825edb980dSskrll 
683de63295cSskrll 	/* Add the new QH to the appropriate schedule */
684de63295cSskrll 	if (dwc2_qh_is_non_per(qh)) {
68543e795b1Ssimonb 		if (qh->want_wait) {
68643e795b1Ssimonb 			list_add_tail(&qh->qh_list_entry,
68743e795b1Ssimonb 				      &hsotg->non_periodic_sched_waiting);
68843e795b1Ssimonb 			qh->wait_timer_cancel = false;
68943e795b1Ssimonb 			/* XXX mod_timer(&qh->wait_timer,
69043e795b1Ssimonb 				  jiffies + DWC2_RETRY_WAIT_DELAY + 1); */
69143e795b1Ssimonb 			callout_schedule(&qh->wait_timer,
69243e795b1Ssimonb 			    mstohz(DWC2_RETRY_WAIT_DELAY));
69343e795b1Ssimonb 		} else {
694de63295cSskrll 			list_add_tail(&qh->qh_list_entry,
695de63295cSskrll 				      &hsotg->non_periodic_sched_inactive);
69643e795b1Ssimonb 		}
69714ad0a56Sskrll 		return 0;
69814ad0a56Sskrll 	}
699b4858ac2Sskrll 
700de63295cSskrll 	status = dwc2_schedule_periodic(hsotg, qh);
70114ad0a56Sskrll 	if (status)
70214ad0a56Sskrll 		return status;
703de63295cSskrll 	if (!hsotg->periodic_qh_count) {
7045f137d9bSskrll 		intr_mask = DWC2_READ_4(hsotg, GINTMSK);
705de63295cSskrll 		intr_mask |= GINTSTS_SOF;
7065f137d9bSskrll 		DWC2_WRITE_4(hsotg, GINTMSK, intr_mask);
707de63295cSskrll 	}
708de63295cSskrll 	hsotg->periodic_qh_count++;
709de63295cSskrll 
71014ad0a56Sskrll 	return 0;
711de63295cSskrll }
712de63295cSskrll 
713de63295cSskrll /**
714de63295cSskrll  * dwc2_hcd_qh_unlink() - Removes a QH from either the non-periodic or periodic
715de63295cSskrll  * schedule. Memory is not freed.
716de63295cSskrll  *
717de63295cSskrll  * @hsotg: The HCD state structure
718de63295cSskrll  * @qh:    QH to remove from schedule
719de63295cSskrll  */
dwc2_hcd_qh_unlink(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)720de63295cSskrll void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
721de63295cSskrll {
722de63295cSskrll 	u32 intr_mask;
723de63295cSskrll 
724de63295cSskrll 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
725de63295cSskrll 
72643e795b1Ssimonb 	/* If the wait_timer is pending, this will stop it from acting */
72743e795b1Ssimonb 	qh->wait_timer_cancel = true;
72843e795b1Ssimonb 
729de63295cSskrll 	if (list_empty(&qh->qh_list_entry))
730de63295cSskrll 		/* QH is not in a schedule */
731de63295cSskrll 		return;
732de63295cSskrll 
733de63295cSskrll 	if (dwc2_qh_is_non_per(qh)) {
734de63295cSskrll 		if (hsotg->non_periodic_qh_ptr == &qh->qh_list_entry)
735de63295cSskrll 			hsotg->non_periodic_qh_ptr =
736de63295cSskrll 					hsotg->non_periodic_qh_ptr->next;
737de63295cSskrll 		list_del_init(&qh->qh_list_entry);
73814ad0a56Sskrll 		return;
73914ad0a56Sskrll 	}
740b4858ac2Sskrll 
741de63295cSskrll 	dwc2_deschedule_periodic(hsotg, qh);
742de63295cSskrll 	hsotg->periodic_qh_count--;
743de63295cSskrll 	if (!hsotg->periodic_qh_count) {
7445f137d9bSskrll 		intr_mask = DWC2_READ_4(hsotg, GINTMSK);
745de63295cSskrll 		intr_mask &= ~GINTSTS_SOF;
7465f137d9bSskrll 		DWC2_WRITE_4(hsotg, GINTMSK, intr_mask);
747de63295cSskrll 	}
748de63295cSskrll }
749de63295cSskrll 
750de63295cSskrll /*
751de63295cSskrll  * Schedule the next continuing periodic split transfer
752de63295cSskrll  */
dwc2_sched_periodic_split(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,u16 frame_number,int sched_next_periodic_split)753de63295cSskrll static void dwc2_sched_periodic_split(struct dwc2_hsotg *hsotg,
754de63295cSskrll 				      struct dwc2_qh *qh, u16 frame_number,
755de63295cSskrll 				      int sched_next_periodic_split)
756de63295cSskrll {
757de63295cSskrll 	u16 incr;
758de63295cSskrll 
759de63295cSskrll 	if (sched_next_periodic_split) {
760de63295cSskrll 		qh->sched_frame = frame_number;
761de63295cSskrll 		incr = dwc2_frame_num_inc(qh->start_split_frame, 1);
762de63295cSskrll 		if (dwc2_frame_num_le(frame_number, incr)) {
763de63295cSskrll 			/*
764de63295cSskrll 			 * Allow one frame to elapse after start split
765de63295cSskrll 			 * microframe before scheduling complete split, but
766de63295cSskrll 			 * DON'T if we are doing the next start split in the
767de63295cSskrll 			 * same frame for an ISOC out
768de63295cSskrll 			 */
769de63295cSskrll 			if (qh->ep_type != USB_ENDPOINT_XFER_ISOC ||
770de63295cSskrll 			    qh->ep_is_in != 0) {
771de63295cSskrll 				qh->sched_frame =
772de63295cSskrll 					dwc2_frame_num_inc(qh->sched_frame, 1);
773de63295cSskrll 			}
774de63295cSskrll 		}
775de63295cSskrll 	} else {
776de63295cSskrll 		qh->sched_frame = dwc2_frame_num_inc(qh->start_split_frame,
777de63295cSskrll 						     qh->interval);
778de63295cSskrll 		if (dwc2_frame_num_le(qh->sched_frame, frame_number))
779de63295cSskrll 			qh->sched_frame = frame_number;
780de63295cSskrll 		qh->sched_frame |= 0x7;
781de63295cSskrll 		qh->start_split_frame = qh->sched_frame;
782de63295cSskrll 	}
783de63295cSskrll }
784de63295cSskrll 
785de63295cSskrll /*
786de63295cSskrll  * Deactivates a QH. For non-periodic QHs, removes the QH from the active
787de63295cSskrll  * non-periodic schedule. The QH is added to the inactive non-periodic
788de63295cSskrll  * schedule if any QTDs are still attached to the QH.
789de63295cSskrll  *
790de63295cSskrll  * For periodic QHs, the QH is removed from the periodic queued schedule. If
791de63295cSskrll  * there are any QTDs still attached to the QH, the QH is added to either the
792de63295cSskrll  * periodic inactive schedule or the periodic ready schedule and its next
793de63295cSskrll  * scheduled frame is calculated. The QH is placed in the ready schedule if
794de63295cSskrll  * the scheduled frame has been reached already. Otherwise it's placed in the
795de63295cSskrll  * inactive schedule. If there are no QTDs attached to the QH, the QH is
796de63295cSskrll  * completely removed from the periodic schedule.
797de63295cSskrll  */
dwc2_hcd_qh_deactivate(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,int sched_next_periodic_split)798de63295cSskrll void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
799de63295cSskrll 			    int sched_next_periodic_split)
800de63295cSskrll {
80114ad0a56Sskrll 	u16 frame_number;
80214ad0a56Sskrll 
803de63295cSskrll 	if (dbg_qh(qh))
804de63295cSskrll 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
805de63295cSskrll 
806de63295cSskrll 	if (dwc2_qh_is_non_per(qh)) {
807de63295cSskrll 		dwc2_hcd_qh_unlink(hsotg, qh);
808de63295cSskrll 		if (!list_empty(&qh->qtd_list))
80943e795b1Ssimonb 			/* Add back to inactive/waiting non-periodic schedule */
810de63295cSskrll 			dwc2_hcd_qh_add(hsotg, qh);
81114ad0a56Sskrll 		return;
81214ad0a56Sskrll 	}
81314ad0a56Sskrll 
81414ad0a56Sskrll 	frame_number = dwc2_hcd_get_frame_number(hsotg);
815de63295cSskrll 
816de63295cSskrll 	if (qh->do_split) {
817de63295cSskrll 		dwc2_sched_periodic_split(hsotg, qh, frame_number,
818de63295cSskrll 					  sched_next_periodic_split);
819de63295cSskrll 	} else {
820de63295cSskrll 		qh->sched_frame = dwc2_frame_num_inc(qh->sched_frame,
821de63295cSskrll 						     qh->interval);
822de63295cSskrll 		if (dwc2_frame_num_le(qh->sched_frame, frame_number))
823de63295cSskrll 			qh->sched_frame = frame_number;
824de63295cSskrll 	}
825de63295cSskrll 
826de63295cSskrll 	if (list_empty(&qh->qtd_list)) {
827de63295cSskrll 		dwc2_hcd_qh_unlink(hsotg, qh);
82814ad0a56Sskrll 		return;
82914ad0a56Sskrll 	}
830de63295cSskrll 	/*
831de63295cSskrll 	 * Remove from periodic_sched_queued and move to
832de63295cSskrll 	 * appropriate queue
833de63295cSskrll 	 */
834de63295cSskrll 	if ((hsotg->core_params->uframe_sched > 0 &&
83514ad0a56Sskrll 	     dwc2_frame_num_le(qh->sched_frame, frame_number)) ||
83614ad0a56Sskrll 	    (hsotg->core_params->uframe_sched <= 0 &&
83701999631Sskrll 	     qh->sched_frame == frame_number))
83814ad0a56Sskrll 		list_move(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
83901999631Sskrll 	else
84014ad0a56Sskrll 		list_move(&qh->qh_list_entry, &hsotg->periodic_sched_inactive);
841de63295cSskrll }
842de63295cSskrll 
843de63295cSskrll /**
844de63295cSskrll  * dwc2_hcd_qtd_init() - Initializes a QTD structure
845de63295cSskrll  *
846de63295cSskrll  * @qtd: The QTD to initialize
847de63295cSskrll  * @urb: The associated URB
848de63295cSskrll  */
dwc2_hcd_qtd_init(struct dwc2_qtd * qtd,struct dwc2_hcd_urb * urb)849de63295cSskrll void dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
850de63295cSskrll {
851de63295cSskrll 	qtd->urb = urb;
852de63295cSskrll 	if (dwc2_hcd_get_pipe_type(&urb->pipe_info) ==
853de63295cSskrll 			USB_ENDPOINT_XFER_CONTROL) {
854de63295cSskrll 		/*
855de63295cSskrll 		 * The only time the QTD data toggle is used is on the data
856de63295cSskrll 		 * phase of control transfers. This phase always starts with
857de63295cSskrll 		 * DATA1.
858de63295cSskrll 		 */
859de63295cSskrll 		qtd->data_toggle = DWC2_HC_PID_DATA1;
860de63295cSskrll 		qtd->control_phase = DWC2_CONTROL_SETUP;
861de63295cSskrll 	}
862de63295cSskrll 
863de63295cSskrll 	/* Start split */
864de63295cSskrll 	qtd->complete_split = 0;
865de63295cSskrll 	qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
866de63295cSskrll 	qtd->isoc_split_offset = 0;
867de63295cSskrll 	qtd->in_process = 0;
868de63295cSskrll 
869de63295cSskrll 	/* Store the qtd ptr in the urb to reference the QTD */
870de63295cSskrll 	urb->qtd = qtd;
871de63295cSskrll }
872de63295cSskrll 
873de63295cSskrll /**
874de63295cSskrll  * dwc2_hcd_qtd_add() - Adds a QTD to the QTD-list of a QH
875b4858ac2Sskrll  *			Caller must hold driver lock.
876de63295cSskrll  *
877de63295cSskrll  * @hsotg:        The DWC HCD structure
878de63295cSskrll  * @qtd:          The QTD to add
879b4858ac2Sskrll  * @qh:           Queue head to add qtd to
880de63295cSskrll  *
881de63295cSskrll  * Return: 0 if successful, negative error code otherwise
882de63295cSskrll  *
883b4858ac2Sskrll  * If the QH to which the QTD is added is not currently scheduled, it is placed
884b4858ac2Sskrll  * into the proper schedule based on its EP type.
885de63295cSskrll  */
dwc2_hcd_qtd_add(struct dwc2_hsotg * hsotg,struct dwc2_qtd * qtd,struct dwc2_qh * qh)886de63295cSskrll int dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
887b4858ac2Sskrll 		     struct dwc2_qh *qh)
888de63295cSskrll {
889b4858ac2Sskrll 
89082edbb90Suebayasi 	KASSERT(mutex_owned(&hsotg->lock));
891de63295cSskrll 	int retval;
892de63295cSskrll 
893b4858ac2Sskrll 	if (unlikely(!qh)) {
894b4858ac2Sskrll 		dev_err(hsotg->dev, "%s: Invalid QH\n", __func__);
895b4858ac2Sskrll 		retval = -EINVAL;
896b4858ac2Sskrll 		goto fail;
897de63295cSskrll 	}
898de63295cSskrll 
899b4858ac2Sskrll 	retval = dwc2_hcd_qh_add(hsotg, qh);
900de63295cSskrll 	if (retval)
901de63295cSskrll 		goto fail;
902de63295cSskrll 
903b4858ac2Sskrll 	qtd->qh = qh;
904b4858ac2Sskrll 	list_add_tail(&qtd->qtd_list_entry, &qh->qtd_list);
905de63295cSskrll 
906de63295cSskrll 	return 0;
907de63295cSskrll fail:
908de63295cSskrll 	return retval;
909de63295cSskrll }
9105f137d9bSskrll 
dwc2_hcd_qtd_unlink_and_free(struct dwc2_hsotg * hsotg,struct dwc2_qtd * qtd,struct dwc2_qh * qh)9115f137d9bSskrll void dwc2_hcd_qtd_unlink_and_free(struct dwc2_hsotg *hsotg,
9125f137d9bSskrll 				  struct dwc2_qtd *qtd,
9135f137d9bSskrll 				  struct dwc2_qh *qh)
9145f137d9bSskrll {
9155f137d9bSskrll 	struct dwc2_softc *sc = hsotg->hsotg_sc;
9165f137d9bSskrll 
9175f137d9bSskrll 	list_del_init(&qtd->qtd_list_entry);
9185f137d9bSskrll  	pool_cache_put(sc->sc_qtdpool, qtd);
9195f137d9bSskrll }
9205f137d9bSskrll 
9215f137d9bSskrll #define BITSTUFFTIME(bytecount)	((8 * 7 * (bytecount)) / 6)
9225f137d9bSskrll #define HS_HOST_DELAY		5	/* nanoseconds */
9235f137d9bSskrll #define FS_LS_HOST_DELAY	1000	/* nanoseconds */
9245f137d9bSskrll #define HUB_LS_SETUP		333	/* nanoseconds */
9255f137d9bSskrll 
dwc2_calc_bus_time(struct dwc2_hsotg * hsotg,int speed,int is_in,int is_isoc,int bytecount)9265f137d9bSskrll static u32 dwc2_calc_bus_time(struct dwc2_hsotg *hsotg, int speed, int is_in,
9275f137d9bSskrll 			      int is_isoc, int bytecount)
9285f137d9bSskrll {
9295f137d9bSskrll 	unsigned long retval;
9305f137d9bSskrll 
9315f137d9bSskrll 	switch (speed) {
9325f137d9bSskrll 	case USB_SPEED_HIGH:
9335f137d9bSskrll 		if (is_isoc)
9345f137d9bSskrll 			retval =
9355f137d9bSskrll 			    ((38 * 8 * 2083) +
9365f137d9bSskrll 			     (2083 * (3 + BITSTUFFTIME(bytecount)))) / 1000 +
9375f137d9bSskrll 			    HS_HOST_DELAY;
9385f137d9bSskrll 		else
9395f137d9bSskrll 			retval =
9405f137d9bSskrll 			    ((55 * 8 * 2083) +
9415f137d9bSskrll 			     (2083 * (3 + BITSTUFFTIME(bytecount)))) / 1000 +
9425f137d9bSskrll 			    HS_HOST_DELAY;
9435f137d9bSskrll 		break;
9445f137d9bSskrll 	case USB_SPEED_FULL:
9455f137d9bSskrll 		if (is_isoc) {
9465f137d9bSskrll 			retval =
9475f137d9bSskrll 			    (8354 * (31 + 10 * BITSTUFFTIME(bytecount))) / 1000;
9485f137d9bSskrll 			if (is_in)
9495f137d9bSskrll 				retval = 7268 + FS_LS_HOST_DELAY + retval;
9505f137d9bSskrll 			else
9515f137d9bSskrll 				retval = 6265 + FS_LS_HOST_DELAY + retval;
9525f137d9bSskrll 		} else {
9535f137d9bSskrll 			retval =
9545f137d9bSskrll 			    (8354 * (31 + 10 * BITSTUFFTIME(bytecount))) / 1000;
9555f137d9bSskrll 			retval = 9107 + FS_LS_HOST_DELAY + retval;
9565f137d9bSskrll 		}
9575f137d9bSskrll 		break;
9585f137d9bSskrll 	case USB_SPEED_LOW:
9595f137d9bSskrll 		if (is_in) {
9605f137d9bSskrll 			retval =
9615f137d9bSskrll 			    (67667 * (31 + 10 * BITSTUFFTIME(bytecount))) /
9625f137d9bSskrll 			    1000;
9635f137d9bSskrll 			retval =
9645f137d9bSskrll 			    64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
9655f137d9bSskrll 			    retval;
9665f137d9bSskrll 		} else {
9675f137d9bSskrll 			retval =
9685f137d9bSskrll 			    (66700 * (31 + 10 * BITSTUFFTIME(bytecount))) /
9695f137d9bSskrll 			    1000;
9705f137d9bSskrll 			retval =
9715f137d9bSskrll 			    64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
9725f137d9bSskrll 			    retval;
9735f137d9bSskrll 		}
9745f137d9bSskrll 		break;
9755f137d9bSskrll 	default:
9765f137d9bSskrll 		dev_warn(hsotg->dev, "Unknown device speed\n");
9775f137d9bSskrll 		retval = -1;
9785f137d9bSskrll 	}
9795f137d9bSskrll 
9805f137d9bSskrll 	return NS_TO_US(retval);
9815f137d9bSskrll }
982