xref: /linux/drivers/usb/dwc2/hcd_intr.c (revision 52c9780404963fea7300a7517ef1290439a1e08b)
1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /*
3  * hcd_intr.c - DesignWare HS OTG Controller host-mode interrupt handling
4  *
5  * Copyright (C) 2004-2013 Synopsys, Inc.
6  */
7 
8 /*
9  * This file contains the interrupt handlers for Host mode
10  */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/spinlock.h>
14 #include <linux/interrupt.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/io.h>
17 #include <linux/slab.h>
18 #include <linux/usb.h>
19 
20 #include <linux/usb/hcd.h>
21 #include <linux/usb/ch11.h>
22 
23 #include "core.h"
24 #include "hcd.h"
25 
26 /*
27  * If we get this many NAKs on a split transaction we'll slow down
28  * retransmission.  A 1 here means delay after the first NAK.
29  */
30 #define DWC2_NAKS_BEFORE_DELAY		3
31 
32 /* This function is for debug only */
33 static void dwc2_track_missed_sofs(struct dwc2_hsotg *hsotg)
34 {
35 	u16 curr_frame_number = hsotg->frame_number;
36 	u16 expected = dwc2_frame_num_inc(hsotg->last_frame_num, 1);
37 
38 	if (expected != curr_frame_number)
39 		dwc2_sch_vdbg(hsotg, "MISSED SOF %04x != %04x\n",
40 			      expected, curr_frame_number);
41 
42 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
43 	if (hsotg->frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
44 		if (expected != curr_frame_number) {
45 			hsotg->frame_num_array[hsotg->frame_num_idx] =
46 					curr_frame_number;
47 			hsotg->last_frame_num_array[hsotg->frame_num_idx] =
48 					hsotg->last_frame_num;
49 			hsotg->frame_num_idx++;
50 		}
51 	} else if (!hsotg->dumped_frame_num_array) {
52 		int i;
53 
54 		dev_info(hsotg->dev, "Frame     Last Frame\n");
55 		dev_info(hsotg->dev, "-----     ----------\n");
56 		for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
57 			dev_info(hsotg->dev, "0x%04x    0x%04x\n",
58 				 hsotg->frame_num_array[i],
59 				 hsotg->last_frame_num_array[i]);
60 		}
61 		hsotg->dumped_frame_num_array = 1;
62 	}
63 #endif
64 	hsotg->last_frame_num = curr_frame_number;
65 }
66 
67 static void dwc2_hc_handle_tt_clear(struct dwc2_hsotg *hsotg,
68 				    struct dwc2_host_chan *chan,
69 				    struct dwc2_qtd *qtd)
70 {
71 	struct usb_device *root_hub = dwc2_hsotg_to_hcd(hsotg)->self.root_hub;
72 	struct urb *usb_urb;
73 
74 	if (!chan->qh)
75 		return;
76 
77 	if (chan->qh->dev_speed == USB_SPEED_HIGH)
78 		return;
79 
80 	if (!qtd->urb)
81 		return;
82 
83 	usb_urb = qtd->urb->priv;
84 	if (!usb_urb || !usb_urb->dev || !usb_urb->dev->tt)
85 		return;
86 
87 	/*
88 	 * The root hub doesn't really have a TT, but Linux thinks it
89 	 * does because how could you have a "high speed hub" that
90 	 * directly talks directly to low speed devices without a TT?
91 	 * It's all lies.  Lies, I tell you.
92 	 */
93 	if (usb_urb->dev->tt->hub == root_hub)
94 		return;
95 
96 	if (qtd->urb->status != -EPIPE && qtd->urb->status != -EREMOTEIO) {
97 		chan->qh->tt_buffer_dirty = 1;
98 		if (usb_hub_clear_tt_buffer(usb_urb))
99 			/* Clear failed; let's hope things work anyway */
100 			chan->qh->tt_buffer_dirty = 0;
101 	}
102 }
103 
104 /*
105  * Handles the start-of-frame interrupt in host mode. Non-periodic
106  * transactions may be queued to the DWC_otg controller for the current
107  * (micro)frame. Periodic transactions may be queued to the controller
108  * for the next (micro)frame.
109  */
110 static void dwc2_sof_intr(struct dwc2_hsotg *hsotg)
111 {
112 	struct list_head *qh_entry;
113 	struct dwc2_qh *qh;
114 	enum dwc2_transaction_type tr_type;
115 
116 	/* Clear interrupt */
117 	dwc2_writel(hsotg, GINTSTS_SOF, GINTSTS);
118 
119 #ifdef DEBUG_SOF
120 	dev_vdbg(hsotg->dev, "--Start of Frame Interrupt--\n");
121 #endif
122 
123 	hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
124 
125 	dwc2_track_missed_sofs(hsotg);
126 
127 	/* Determine whether any periodic QHs should be executed */
128 	qh_entry = hsotg->periodic_sched_inactive.next;
129 	while (qh_entry != &hsotg->periodic_sched_inactive) {
130 		qh = list_entry(qh_entry, struct dwc2_qh, qh_list_entry);
131 		qh_entry = qh_entry->next;
132 		if (dwc2_frame_num_le(qh->next_active_frame,
133 				      hsotg->frame_number)) {
134 			dwc2_sch_vdbg(hsotg, "QH=%p ready fn=%04x, nxt=%04x\n",
135 				      qh, hsotg->frame_number,
136 				      qh->next_active_frame);
137 
138 			/*
139 			 * Move QH to the ready list to be executed next
140 			 * (micro)frame
141 			 */
142 			list_move_tail(&qh->qh_list_entry,
143 				       &hsotg->periodic_sched_ready);
144 		}
145 	}
146 	tr_type = dwc2_hcd_select_transactions(hsotg);
147 	if (tr_type != DWC2_TRANSACTION_NONE)
148 		dwc2_hcd_queue_transactions(hsotg, tr_type);
149 }
150 
151 /*
152  * Handles the Rx FIFO Level Interrupt, which indicates that there is
153  * at least one packet in the Rx FIFO. The packets are moved from the FIFO to
154  * memory if the DWC_otg controller is operating in Slave mode.
155  */
156 static void dwc2_rx_fifo_level_intr(struct dwc2_hsotg *hsotg)
157 {
158 	u32 grxsts, chnum, bcnt, dpid, pktsts;
159 	struct dwc2_host_chan *chan;
160 
161 	if (dbg_perio())
162 		dev_vdbg(hsotg->dev, "--RxFIFO Level Interrupt--\n");
163 
164 	grxsts = dwc2_readl(hsotg, GRXSTSP);
165 	chnum = (grxsts & GRXSTS_HCHNUM_MASK) >> GRXSTS_HCHNUM_SHIFT;
166 	chan = hsotg->hc_ptr_array[chnum];
167 	if (!chan) {
168 		dev_err(hsotg->dev, "Unable to get corresponding channel\n");
169 		return;
170 	}
171 
172 	bcnt = (grxsts & GRXSTS_BYTECNT_MASK) >> GRXSTS_BYTECNT_SHIFT;
173 	dpid = (grxsts & GRXSTS_DPID_MASK) >> GRXSTS_DPID_SHIFT;
174 	pktsts = (grxsts & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT;
175 
176 	/* Packet Status */
177 	if (dbg_perio()) {
178 		dev_vdbg(hsotg->dev, "    Ch num = %d\n", chnum);
179 		dev_vdbg(hsotg->dev, "    Count = %d\n", bcnt);
180 		dev_vdbg(hsotg->dev, "    DPID = %d, chan.dpid = %d\n", dpid,
181 			 chan->data_pid_start);
182 		dev_vdbg(hsotg->dev, "    PStatus = %d\n", pktsts);
183 	}
184 
185 	switch (pktsts) {
186 	case GRXSTS_PKTSTS_HCHIN:
187 		/* Read the data into the host buffer */
188 		if (bcnt > 0) {
189 			dwc2_read_packet(hsotg, chan->xfer_buf, bcnt);
190 
191 			/* Update the HC fields for the next packet received */
192 			chan->xfer_count += bcnt;
193 			chan->xfer_buf += bcnt;
194 		}
195 		break;
196 	case GRXSTS_PKTSTS_HCHIN_XFER_COMP:
197 	case GRXSTS_PKTSTS_DATATOGGLEERR:
198 	case GRXSTS_PKTSTS_HCHHALTED:
199 		/* Handled in interrupt, just ignore data */
200 		break;
201 	default:
202 		dev_err(hsotg->dev,
203 			"RxFIFO Level Interrupt: Unknown status %d\n", pktsts);
204 		break;
205 	}
206 }
207 
208 /*
209  * This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
210  * data packets may be written to the FIFO for OUT transfers. More requests
211  * may be written to the non-periodic request queue for IN transfers. This
212  * interrupt is enabled only in Slave mode.
213  */
214 static void dwc2_np_tx_fifo_empty_intr(struct dwc2_hsotg *hsotg)
215 {
216 	dev_vdbg(hsotg->dev, "--Non-Periodic TxFIFO Empty Interrupt--\n");
217 	dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_NON_PERIODIC);
218 }
219 
220 /*
221  * This interrupt occurs when the periodic Tx FIFO is half-empty. More data
222  * packets may be written to the FIFO for OUT transfers. More requests may be
223  * written to the periodic request queue for IN transfers. This interrupt is
224  * enabled only in Slave mode.
225  */
226 static void dwc2_perio_tx_fifo_empty_intr(struct dwc2_hsotg *hsotg)
227 {
228 	if (dbg_perio())
229 		dev_vdbg(hsotg->dev, "--Periodic TxFIFO Empty Interrupt--\n");
230 	dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_PERIODIC);
231 }
232 
233 static void dwc2_hprt0_enable(struct dwc2_hsotg *hsotg, u32 hprt0,
234 			      u32 *hprt0_modify)
235 {
236 	struct dwc2_core_params *params = &hsotg->params;
237 	int do_reset = 0;
238 	u32 usbcfg;
239 	u32 prtspd;
240 	u32 hcfg;
241 	u32 fslspclksel;
242 	u32 hfir;
243 
244 	dev_vdbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
245 
246 	/* Every time when port enables calculate HFIR.FrInterval */
247 	hfir = dwc2_readl(hsotg, HFIR);
248 	hfir &= ~HFIR_FRINT_MASK;
249 	hfir |= dwc2_calc_frame_interval(hsotg) << HFIR_FRINT_SHIFT &
250 		HFIR_FRINT_MASK;
251 	dwc2_writel(hsotg, hfir, HFIR);
252 
253 	/* Check if we need to adjust the PHY clock speed for low power */
254 	if (!params->host_support_fs_ls_low_power) {
255 		/* Port has been enabled, set the reset change flag */
256 		hsotg->flags.b.port_reset_change = 1;
257 		return;
258 	}
259 
260 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
261 	prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
262 
263 	if (prtspd == HPRT0_SPD_LOW_SPEED || prtspd == HPRT0_SPD_FULL_SPEED) {
264 		/* Low power */
265 		if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL)) {
266 			/* Set PHY low power clock select for FS/LS devices */
267 			usbcfg |= GUSBCFG_PHY_LP_CLK_SEL;
268 			dwc2_writel(hsotg, usbcfg, GUSBCFG);
269 			do_reset = 1;
270 		}
271 
272 		hcfg = dwc2_readl(hsotg, HCFG);
273 		fslspclksel = (hcfg & HCFG_FSLSPCLKSEL_MASK) >>
274 			      HCFG_FSLSPCLKSEL_SHIFT;
275 
276 		if (prtspd == HPRT0_SPD_LOW_SPEED &&
277 		    params->host_ls_low_power_phy_clk) {
278 			/* 6 MHZ */
279 			dev_vdbg(hsotg->dev,
280 				 "FS_PHY programming HCFG to 6 MHz\n");
281 			if (fslspclksel != HCFG_FSLSPCLKSEL_6_MHZ) {
282 				fslspclksel = HCFG_FSLSPCLKSEL_6_MHZ;
283 				hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
284 				hcfg |= fslspclksel << HCFG_FSLSPCLKSEL_SHIFT;
285 				dwc2_writel(hsotg, hcfg, HCFG);
286 				do_reset = 1;
287 			}
288 		} else {
289 			/* 48 MHZ */
290 			dev_vdbg(hsotg->dev,
291 				 "FS_PHY programming HCFG to 48 MHz\n");
292 			if (fslspclksel != HCFG_FSLSPCLKSEL_48_MHZ) {
293 				fslspclksel = HCFG_FSLSPCLKSEL_48_MHZ;
294 				hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
295 				hcfg |= fslspclksel << HCFG_FSLSPCLKSEL_SHIFT;
296 				dwc2_writel(hsotg, hcfg, HCFG);
297 				do_reset = 1;
298 			}
299 		}
300 	} else {
301 		/* Not low power */
302 		if (usbcfg & GUSBCFG_PHY_LP_CLK_SEL) {
303 			usbcfg &= ~GUSBCFG_PHY_LP_CLK_SEL;
304 			dwc2_writel(hsotg, usbcfg, GUSBCFG);
305 			do_reset = 1;
306 		}
307 	}
308 
309 	if (do_reset) {
310 		*hprt0_modify |= HPRT0_RST;
311 		dwc2_writel(hsotg, *hprt0_modify, HPRT0);
312 		queue_delayed_work(hsotg->wq_otg, &hsotg->reset_work,
313 				   msecs_to_jiffies(60));
314 	} else {
315 		/* Port has been enabled, set the reset change flag */
316 		hsotg->flags.b.port_reset_change = 1;
317 	}
318 }
319 
320 /*
321  * There are multiple conditions that can cause a port interrupt. This function
322  * determines which interrupt conditions have occurred and handles them
323  * appropriately.
324  */
325 static void dwc2_port_intr(struct dwc2_hsotg *hsotg)
326 {
327 	u32 hprt0;
328 	u32 hprt0_modify;
329 
330 	dev_vdbg(hsotg->dev, "--Port Interrupt--\n");
331 
332 	hprt0 = dwc2_readl(hsotg, HPRT0);
333 	hprt0_modify = hprt0;
334 
335 	/*
336 	 * Clear appropriate bits in HPRT0 to clear the interrupt bit in
337 	 * GINTSTS
338 	 */
339 	hprt0_modify &= ~(HPRT0_ENA | HPRT0_CONNDET | HPRT0_ENACHG |
340 			  HPRT0_OVRCURRCHG);
341 
342 	/*
343 	 * Port Connect Detected
344 	 * Set flag and clear if detected
345 	 */
346 	if (hprt0 & HPRT0_CONNDET) {
347 		dwc2_writel(hsotg, hprt0_modify | HPRT0_CONNDET, HPRT0);
348 
349 		dev_vdbg(hsotg->dev,
350 			 "--Port Interrupt HPRT0=0x%08x Port Connect Detected--\n",
351 			 hprt0);
352 		dwc2_hcd_connect(hsotg);
353 
354 		/*
355 		 * The Hub driver asserts a reset when it sees port connect
356 		 * status change flag
357 		 */
358 	}
359 
360 	/*
361 	 * Port Enable Changed
362 	 * Clear if detected - Set internal flag if disabled
363 	 */
364 	if (hprt0 & HPRT0_ENACHG) {
365 		dwc2_writel(hsotg, hprt0_modify | HPRT0_ENACHG, HPRT0);
366 		dev_vdbg(hsotg->dev,
367 			 "  --Port Interrupt HPRT0=0x%08x Port Enable Changed (now %d)--\n",
368 			 hprt0, !!(hprt0 & HPRT0_ENA));
369 		if (hprt0 & HPRT0_ENA) {
370 			hsotg->new_connection = true;
371 			dwc2_hprt0_enable(hsotg, hprt0, &hprt0_modify);
372 		} else {
373 			hsotg->flags.b.port_enable_change = 1;
374 			if (hsotg->params.dma_desc_fs_enable) {
375 				u32 hcfg;
376 
377 				hsotg->params.dma_desc_enable = false;
378 				hsotg->new_connection = false;
379 				hcfg = dwc2_readl(hsotg, HCFG);
380 				hcfg &= ~HCFG_DESCDMA;
381 				dwc2_writel(hsotg, hcfg, HCFG);
382 			}
383 		}
384 	}
385 
386 	/* Overcurrent Change Interrupt */
387 	if (hprt0 & HPRT0_OVRCURRCHG) {
388 		dwc2_writel(hsotg, hprt0_modify | HPRT0_OVRCURRCHG,
389 			    HPRT0);
390 		dev_vdbg(hsotg->dev,
391 			 "  --Port Interrupt HPRT0=0x%08x Port Overcurrent Changed--\n",
392 			 hprt0);
393 		hsotg->flags.b.port_over_current_change = 1;
394 	}
395 }
396 
397 /*
398  * Gets the actual length of a transfer after the transfer halts. halt_status
399  * holds the reason for the halt.
400  *
401  * For IN transfers where halt_status is DWC2_HC_XFER_COMPLETE, *short_read
402  * is set to 1 upon return if less than the requested number of bytes were
403  * transferred. short_read may also be NULL on entry, in which case it remains
404  * unchanged.
405  */
406 static u32 dwc2_get_actual_xfer_length(struct dwc2_hsotg *hsotg,
407 				       struct dwc2_host_chan *chan, int chnum,
408 				       struct dwc2_qtd *qtd,
409 				       enum dwc2_halt_status halt_status,
410 				       int *short_read)
411 {
412 	u32 hctsiz, count, length;
413 
414 	hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
415 
416 	if (halt_status == DWC2_HC_XFER_COMPLETE) {
417 		if (chan->ep_is_in) {
418 			count = (hctsiz & TSIZ_XFERSIZE_MASK) >>
419 				TSIZ_XFERSIZE_SHIFT;
420 			length = chan->xfer_len - count;
421 			if (short_read)
422 				*short_read = (count != 0);
423 		} else if (chan->qh->do_split) {
424 			length = qtd->ssplit_out_xfer_count;
425 		} else {
426 			length = chan->xfer_len;
427 		}
428 	} else {
429 		/*
430 		 * Must use the hctsiz.pktcnt field to determine how much data
431 		 * has been transferred. This field reflects the number of
432 		 * packets that have been transferred via the USB. This is
433 		 * always an integral number of packets if the transfer was
434 		 * halted before its normal completion. (Can't use the
435 		 * hctsiz.xfersize field because that reflects the number of
436 		 * bytes transferred via the AHB, not the USB).
437 		 */
438 		count = (hctsiz & TSIZ_PKTCNT_MASK) >> TSIZ_PKTCNT_SHIFT;
439 		length = (chan->start_pkt_count - count) * chan->max_packet;
440 	}
441 
442 	return length;
443 }
444 
445 /**
446  * dwc2_update_urb_state() - Updates the state of the URB after a Transfer
447  * Complete interrupt on the host channel. Updates the actual_length field
448  * of the URB based on the number of bytes transferred via the host channel.
449  * Sets the URB status if the data transfer is finished.
450  *
451  * @hsotg: Programming view of the DWC_otg controller
452  * @chan: Programming view of host channel
453  * @chnum: Channel number
454  * @urb: Processing URB
455  * @qtd: Queue transfer descriptor
456  *
457  * Return: 1 if the data transfer specified by the URB is completely finished,
458  * 0 otherwise
459  */
460 static int dwc2_update_urb_state(struct dwc2_hsotg *hsotg,
461 				 struct dwc2_host_chan *chan, int chnum,
462 				 struct dwc2_hcd_urb *urb,
463 				 struct dwc2_qtd *qtd)
464 {
465 	u32 hctsiz;
466 	int xfer_done = 0;
467 	int short_read = 0;
468 	int xfer_length = dwc2_get_actual_xfer_length(hsotg, chan, chnum, qtd,
469 						      DWC2_HC_XFER_COMPLETE,
470 						      &short_read);
471 
472 	if (urb->actual_length + xfer_length > urb->length) {
473 		dev_dbg(hsotg->dev, "%s(): trimming xfer length\n", __func__);
474 		xfer_length = urb->length - urb->actual_length;
475 	}
476 
477 	dev_vdbg(hsotg->dev, "urb->actual_length=%d xfer_length=%d\n",
478 		 urb->actual_length, xfer_length);
479 	urb->actual_length += xfer_length;
480 
481 	if (xfer_length && chan->ep_type == USB_ENDPOINT_XFER_BULK &&
482 	    (urb->flags & URB_SEND_ZERO_PACKET) &&
483 	    urb->actual_length >= urb->length &&
484 	    !(urb->length % chan->max_packet)) {
485 		xfer_done = 0;
486 	} else if (short_read || urb->actual_length >= urb->length) {
487 		xfer_done = 1;
488 		urb->status = 0;
489 	}
490 
491 	hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
492 	dev_vdbg(hsotg->dev, "DWC_otg: %s: %s, channel %d\n",
493 		 __func__, (chan->ep_is_in ? "IN" : "OUT"), chnum);
494 	dev_vdbg(hsotg->dev, "  chan->xfer_len %d\n", chan->xfer_len);
495 	dev_vdbg(hsotg->dev, "  hctsiz.xfersize %d\n",
496 		 (hctsiz & TSIZ_XFERSIZE_MASK) >> TSIZ_XFERSIZE_SHIFT);
497 	dev_vdbg(hsotg->dev, "  urb->transfer_buffer_length %d\n", urb->length);
498 	dev_vdbg(hsotg->dev, "  urb->actual_length %d\n", urb->actual_length);
499 	dev_vdbg(hsotg->dev, "  short_read %d, xfer_done %d\n", short_read,
500 		 xfer_done);
501 
502 	return xfer_done;
503 }
504 
505 /*
506  * Save the starting data toggle for the next transfer. The data toggle is
507  * saved in the QH for non-control transfers and it's saved in the QTD for
508  * control transfers.
509  */
510 void dwc2_hcd_save_data_toggle(struct dwc2_hsotg *hsotg,
511 			       struct dwc2_host_chan *chan, int chnum,
512 			       struct dwc2_qtd *qtd)
513 {
514 	u32 hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
515 	u32 pid = (hctsiz & TSIZ_SC_MC_PID_MASK) >> TSIZ_SC_MC_PID_SHIFT;
516 
517 	if (chan->ep_type != USB_ENDPOINT_XFER_CONTROL) {
518 		if (!chan->qh) {
519 			dev_err(hsotg->dev, "chan->qh must be specified for non-control eps\n");
520 			return;
521 		}
522 
523 		if (pid == TSIZ_SC_MC_PID_DATA0)
524 			chan->qh->data_toggle = DWC2_HC_PID_DATA0;
525 		else
526 			chan->qh->data_toggle = DWC2_HC_PID_DATA1;
527 	} else {
528 		if (!qtd) {
529 			dev_err(hsotg->dev, "qtd must be specified for control eps\n");
530 			return;
531 		}
532 
533 		if (pid == TSIZ_SC_MC_PID_DATA0)
534 			qtd->data_toggle = DWC2_HC_PID_DATA0;
535 		else
536 			qtd->data_toggle = DWC2_HC_PID_DATA1;
537 	}
538 }
539 
540 /**
541  * dwc2_update_isoc_urb_state() - Updates the state of an Isochronous URB when
542  * the transfer is stopped for any reason. The fields of the current entry in
543  * the frame descriptor array are set based on the transfer state and the input
544  * halt_status. Completes the Isochronous URB if all the URB frames have been
545  * completed.
546  *
547  * @hsotg: Programming view of the DWC_otg controller
548  * @chan: Programming view of host channel
549  * @chnum: Channel number
550  * @halt_status: Reason for halting a host channel
551  * @qtd: Queue transfer descriptor
552  *
553  * Return: DWC2_HC_XFER_COMPLETE if there are more frames remaining to be
554  * transferred in the URB. Otherwise return DWC2_HC_XFER_URB_COMPLETE.
555  */
556 static enum dwc2_halt_status dwc2_update_isoc_urb_state(
557 		struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
558 		int chnum, struct dwc2_qtd *qtd,
559 		enum dwc2_halt_status halt_status)
560 {
561 	struct dwc2_hcd_iso_packet_desc *frame_desc;
562 	struct dwc2_hcd_urb *urb = qtd->urb;
563 
564 	if (!urb)
565 		return DWC2_HC_XFER_NO_HALT_STATUS;
566 
567 	frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
568 
569 	switch (halt_status) {
570 	case DWC2_HC_XFER_COMPLETE:
571 		frame_desc->status = 0;
572 		frame_desc->actual_length = dwc2_get_actual_xfer_length(hsotg,
573 					chan, chnum, qtd, halt_status, NULL);
574 		break;
575 	case DWC2_HC_XFER_FRAME_OVERRUN:
576 		urb->error_count++;
577 		if (chan->ep_is_in)
578 			frame_desc->status = -ENOSR;
579 		else
580 			frame_desc->status = -ECOMM;
581 		frame_desc->actual_length = 0;
582 		break;
583 	case DWC2_HC_XFER_BABBLE_ERR:
584 		urb->error_count++;
585 		frame_desc->status = -EOVERFLOW;
586 		/* Don't need to update actual_length in this case */
587 		break;
588 	case DWC2_HC_XFER_XACT_ERR:
589 		urb->error_count++;
590 		frame_desc->status = -EPROTO;
591 		frame_desc->actual_length = dwc2_get_actual_xfer_length(hsotg,
592 					chan, chnum, qtd, halt_status, NULL);
593 
594 		/* Skip whole frame */
595 		if (chan->qh->do_split &&
596 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC && chan->ep_is_in &&
597 		    hsotg->params.host_dma) {
598 			qtd->complete_split = 0;
599 			qtd->isoc_split_offset = 0;
600 		}
601 
602 		break;
603 	default:
604 		dev_err(hsotg->dev, "Unhandled halt_status (%d)\n",
605 			halt_status);
606 		break;
607 	}
608 
609 	if (++qtd->isoc_frame_index == urb->packet_count) {
610 		/*
611 		 * urb->status is not used for isoc transfers. The individual
612 		 * frame_desc statuses are used instead.
613 		 */
614 		dwc2_host_complete(hsotg, qtd, 0);
615 		halt_status = DWC2_HC_XFER_URB_COMPLETE;
616 	} else {
617 		halt_status = DWC2_HC_XFER_COMPLETE;
618 	}
619 
620 	return halt_status;
621 }
622 
623 /*
624  * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
625  * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
626  * still linked to the QH, the QH is added to the end of the inactive
627  * non-periodic schedule. For periodic QHs, removes the QH from the periodic
628  * schedule if no more QTDs are linked to the QH.
629  */
630 static void dwc2_deactivate_qh(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
631 			       int free_qtd)
632 {
633 	int continue_split = 0;
634 	struct dwc2_qtd *qtd;
635 
636 	if (dbg_qh(qh))
637 		dev_vdbg(hsotg->dev, "  %s(%p,%p,%d)\n", __func__,
638 			 hsotg, qh, free_qtd);
639 
640 	if (list_empty(&qh->qtd_list)) {
641 		dev_dbg(hsotg->dev, "## QTD list empty ##\n");
642 		goto no_qtd;
643 	}
644 
645 	qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
646 
647 	if (qtd->complete_split)
648 		continue_split = 1;
649 	else if (qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_MID ||
650 		 qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_END)
651 		continue_split = 1;
652 
653 	if (free_qtd) {
654 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
655 		continue_split = 0;
656 	}
657 
658 no_qtd:
659 	qh->channel = NULL;
660 	dwc2_hcd_qh_deactivate(hsotg, qh, continue_split);
661 }
662 
663 /**
664  * dwc2_release_channel() - Releases a host channel for use by other transfers
665  *
666  * @hsotg:       The HCD state structure
667  * @chan:        The host channel to release
668  * @qtd:         The QTD associated with the host channel. This QTD may be
669  *               freed if the transfer is complete or an error has occurred.
670  * @halt_status: Reason the channel is being released. This status
671  *               determines the actions taken by this function.
672  *
673  * Also attempts to select and queue more transactions since at least one host
674  * channel is available.
675  */
676 static void dwc2_release_channel(struct dwc2_hsotg *hsotg,
677 				 struct dwc2_host_chan *chan,
678 				 struct dwc2_qtd *qtd,
679 				 enum dwc2_halt_status halt_status)
680 {
681 	enum dwc2_transaction_type tr_type;
682 	u32 haintmsk;
683 	int free_qtd = 0;
684 
685 	if (dbg_hc(chan))
686 		dev_vdbg(hsotg->dev, "  %s: channel %d, halt_status %d\n",
687 			 __func__, chan->hc_num, halt_status);
688 
689 	switch (halt_status) {
690 	case DWC2_HC_XFER_URB_COMPLETE:
691 		free_qtd = 1;
692 		break;
693 	case DWC2_HC_XFER_AHB_ERR:
694 	case DWC2_HC_XFER_STALL:
695 	case DWC2_HC_XFER_BABBLE_ERR:
696 		free_qtd = 1;
697 		break;
698 	case DWC2_HC_XFER_XACT_ERR:
699 		if (qtd && qtd->error_count >= 3) {
700 			dev_vdbg(hsotg->dev,
701 				 "  Complete URB with transaction error\n");
702 			free_qtd = 1;
703 			dwc2_host_complete(hsotg, qtd, -EPROTO);
704 		}
705 		break;
706 	case DWC2_HC_XFER_URB_DEQUEUE:
707 		/*
708 		 * The QTD has already been removed and the QH has been
709 		 * deactivated. Don't want to do anything except release the
710 		 * host channel and try to queue more transfers.
711 		 */
712 		goto cleanup;
713 	case DWC2_HC_XFER_PERIODIC_INCOMPLETE:
714 		dev_vdbg(hsotg->dev, "  Complete URB with I/O error\n");
715 		free_qtd = 1;
716 		dwc2_host_complete(hsotg, qtd, -EIO);
717 		break;
718 	case DWC2_HC_XFER_NO_HALT_STATUS:
719 	default:
720 		break;
721 	}
722 
723 	dwc2_deactivate_qh(hsotg, chan->qh, free_qtd);
724 
725 cleanup:
726 	/*
727 	 * Release the host channel for use by other transfers. The cleanup
728 	 * function clears the channel interrupt enables and conditions, so
729 	 * there's no need to clear the Channel Halted interrupt separately.
730 	 */
731 	if (!list_empty(&chan->hc_list_entry))
732 		list_del(&chan->hc_list_entry);
733 	dwc2_hc_cleanup(hsotg, chan);
734 	list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
735 
736 	if (hsotg->params.uframe_sched) {
737 		hsotg->available_host_channels++;
738 	} else {
739 		switch (chan->ep_type) {
740 		case USB_ENDPOINT_XFER_CONTROL:
741 		case USB_ENDPOINT_XFER_BULK:
742 			hsotg->non_periodic_channels--;
743 			break;
744 		default:
745 			/*
746 			 * Don't release reservations for periodic channels
747 			 * here. That's done when a periodic transfer is
748 			 * descheduled (i.e. when the QH is removed from the
749 			 * periodic schedule).
750 			 */
751 			break;
752 		}
753 	}
754 
755 	haintmsk = dwc2_readl(hsotg, HAINTMSK);
756 	haintmsk &= ~(1 << chan->hc_num);
757 	dwc2_writel(hsotg, haintmsk, HAINTMSK);
758 
759 	/* Try to queue more transfers now that there's a free channel */
760 	tr_type = dwc2_hcd_select_transactions(hsotg);
761 	if (tr_type != DWC2_TRANSACTION_NONE)
762 		dwc2_hcd_queue_transactions(hsotg, tr_type);
763 }
764 
765 /*
766  * Halts a host channel. If the channel cannot be halted immediately because
767  * the request queue is full, this function ensures that the FIFO empty
768  * interrupt for the appropriate queue is enabled so that the halt request can
769  * be queued when there is space in the request queue.
770  *
771  * This function may also be called in DMA mode. In that case, the channel is
772  * simply released since the core always halts the channel automatically in
773  * DMA mode.
774  */
775 static void dwc2_halt_channel(struct dwc2_hsotg *hsotg,
776 			      struct dwc2_host_chan *chan, struct dwc2_qtd *qtd,
777 			      enum dwc2_halt_status halt_status)
778 {
779 	if (dbg_hc(chan))
780 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
781 
782 	if (hsotg->params.host_dma) {
783 		if (dbg_hc(chan))
784 			dev_vdbg(hsotg->dev, "DMA enabled\n");
785 		dwc2_release_channel(hsotg, chan, qtd, halt_status);
786 		return;
787 	}
788 
789 	/* Slave mode processing */
790 	dwc2_hc_halt(hsotg, chan, halt_status);
791 
792 	if (chan->halt_on_queue) {
793 		u32 gintmsk;
794 
795 		dev_vdbg(hsotg->dev, "Halt on queue\n");
796 		if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
797 		    chan->ep_type == USB_ENDPOINT_XFER_BULK) {
798 			dev_vdbg(hsotg->dev, "control/bulk\n");
799 			/*
800 			 * Make sure the Non-periodic Tx FIFO empty interrupt
801 			 * is enabled so that the non-periodic schedule will
802 			 * be processed
803 			 */
804 			gintmsk = dwc2_readl(hsotg, GINTMSK);
805 			gintmsk |= GINTSTS_NPTXFEMP;
806 			dwc2_writel(hsotg, gintmsk, GINTMSK);
807 		} else {
808 			dev_vdbg(hsotg->dev, "isoc/intr\n");
809 			/*
810 			 * Move the QH from the periodic queued schedule to
811 			 * the periodic assigned schedule. This allows the
812 			 * halt to be queued when the periodic schedule is
813 			 * processed.
814 			 */
815 			list_move_tail(&chan->qh->qh_list_entry,
816 				       &hsotg->periodic_sched_assigned);
817 
818 			/*
819 			 * Make sure the Periodic Tx FIFO Empty interrupt is
820 			 * enabled so that the periodic schedule will be
821 			 * processed
822 			 */
823 			gintmsk = dwc2_readl(hsotg, GINTMSK);
824 			gintmsk |= GINTSTS_PTXFEMP;
825 			dwc2_writel(hsotg, gintmsk, GINTMSK);
826 		}
827 	}
828 }
829 
830 /*
831  * Performs common cleanup for non-periodic transfers after a Transfer
832  * Complete interrupt. This function should be called after any endpoint type
833  * specific handling is finished to release the host channel.
834  */
835 static void dwc2_complete_non_periodic_xfer(struct dwc2_hsotg *hsotg,
836 					    struct dwc2_host_chan *chan,
837 					    int chnum, struct dwc2_qtd *qtd,
838 					    enum dwc2_halt_status halt_status)
839 {
840 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
841 
842 	qtd->error_count = 0;
843 
844 	if (chan->hcint & HCINTMSK_NYET) {
845 		/*
846 		 * Got a NYET on the last transaction of the transfer. This
847 		 * means that the endpoint should be in the PING state at the
848 		 * beginning of the next transfer.
849 		 */
850 		dev_vdbg(hsotg->dev, "got NYET\n");
851 		chan->qh->ping_state = 1;
852 	}
853 
854 	/*
855 	 * Always halt and release the host channel to make it available for
856 	 * more transfers. There may still be more phases for a control
857 	 * transfer or more data packets for a bulk transfer at this point,
858 	 * but the host channel is still halted. A channel will be reassigned
859 	 * to the transfer when the non-periodic schedule is processed after
860 	 * the channel is released. This allows transactions to be queued
861 	 * properly via dwc2_hcd_queue_transactions, which also enables the
862 	 * Tx FIFO Empty interrupt if necessary.
863 	 */
864 	if (chan->ep_is_in) {
865 		/*
866 		 * IN transfers in Slave mode require an explicit disable to
867 		 * halt the channel. (In DMA mode, this call simply releases
868 		 * the channel.)
869 		 */
870 		dwc2_halt_channel(hsotg, chan, qtd, halt_status);
871 	} else {
872 		/*
873 		 * The channel is automatically disabled by the core for OUT
874 		 * transfers in Slave mode
875 		 */
876 		dwc2_release_channel(hsotg, chan, qtd, halt_status);
877 	}
878 }
879 
880 /*
881  * Performs common cleanup for periodic transfers after a Transfer Complete
882  * interrupt. This function should be called after any endpoint type specific
883  * handling is finished to release the host channel.
884  */
885 static void dwc2_complete_periodic_xfer(struct dwc2_hsotg *hsotg,
886 					struct dwc2_host_chan *chan, int chnum,
887 					struct dwc2_qtd *qtd,
888 					enum dwc2_halt_status halt_status)
889 {
890 	u32 hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
891 
892 	qtd->error_count = 0;
893 
894 	if (!chan->ep_is_in || (hctsiz & TSIZ_PKTCNT_MASK) == 0)
895 		/* Core halts channel in these cases */
896 		dwc2_release_channel(hsotg, chan, qtd, halt_status);
897 	else
898 		/* Flush any outstanding requests from the Tx queue */
899 		dwc2_halt_channel(hsotg, chan, qtd, halt_status);
900 }
901 
902 static int dwc2_xfercomp_isoc_split_in(struct dwc2_hsotg *hsotg,
903 				       struct dwc2_host_chan *chan, int chnum,
904 				       struct dwc2_qtd *qtd)
905 {
906 	struct dwc2_hcd_iso_packet_desc *frame_desc;
907 	u32 len;
908 	u32 hctsiz;
909 	u32 pid;
910 
911 	if (!qtd->urb)
912 		return 0;
913 
914 	frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
915 	len = dwc2_get_actual_xfer_length(hsotg, chan, chnum, qtd,
916 					  DWC2_HC_XFER_COMPLETE, NULL);
917 	if (!len && !qtd->isoc_split_offset) {
918 		qtd->complete_split = 0;
919 		return 0;
920 	}
921 
922 	frame_desc->actual_length += len;
923 
924 	if (chan->align_buf) {
925 		dev_vdbg(hsotg->dev, "non-aligned buffer\n");
926 		dma_unmap_single(hsotg->dev, chan->qh->dw_align_buf_dma,
927 				 DWC2_KMEM_UNALIGNED_BUF_SIZE, DMA_FROM_DEVICE);
928 		memcpy(qtd->urb->buf + (chan->xfer_dma - qtd->urb->dma),
929 		       chan->qh->dw_align_buf, len);
930 	}
931 
932 	qtd->isoc_split_offset += len;
933 
934 	hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
935 	pid = (hctsiz & TSIZ_SC_MC_PID_MASK) >> TSIZ_SC_MC_PID_SHIFT;
936 
937 	if (frame_desc->actual_length >= frame_desc->length || pid == 0) {
938 		frame_desc->status = 0;
939 		qtd->isoc_frame_index++;
940 		qtd->complete_split = 0;
941 		qtd->isoc_split_offset = 0;
942 	}
943 
944 	if (qtd->isoc_frame_index == qtd->urb->packet_count) {
945 		dwc2_host_complete(hsotg, qtd, 0);
946 		dwc2_release_channel(hsotg, chan, qtd,
947 				     DWC2_HC_XFER_URB_COMPLETE);
948 	} else {
949 		dwc2_release_channel(hsotg, chan, qtd,
950 				     DWC2_HC_XFER_NO_HALT_STATUS);
951 	}
952 
953 	return 1;	/* Indicates that channel released */
954 }
955 
956 /*
957  * Handles a host channel Transfer Complete interrupt. This handler may be
958  * called in either DMA mode or Slave mode.
959  */
960 static void dwc2_hc_xfercomp_intr(struct dwc2_hsotg *hsotg,
961 				  struct dwc2_host_chan *chan, int chnum,
962 				  struct dwc2_qtd *qtd)
963 {
964 	struct dwc2_hcd_urb *urb = qtd->urb;
965 	enum dwc2_halt_status halt_status = DWC2_HC_XFER_COMPLETE;
966 	int pipe_type;
967 	int urb_xfer_done;
968 
969 	if (dbg_hc(chan))
970 		dev_vdbg(hsotg->dev,
971 			 "--Host Channel %d Interrupt: Transfer Complete--\n",
972 			 chnum);
973 
974 	if (!urb)
975 		goto handle_xfercomp_done;
976 
977 	pipe_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
978 
979 	if (hsotg->params.dma_desc_enable) {
980 		dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum, halt_status);
981 		if (pipe_type == USB_ENDPOINT_XFER_ISOC)
982 			/* Do not disable the interrupt, just clear it */
983 			return;
984 		goto handle_xfercomp_done;
985 	}
986 
987 	/* Handle xfer complete on CSPLIT */
988 	if (chan->qh->do_split) {
989 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC && chan->ep_is_in &&
990 		    hsotg->params.host_dma) {
991 			if (qtd->complete_split &&
992 			    dwc2_xfercomp_isoc_split_in(hsotg, chan, chnum,
993 							qtd))
994 				goto handle_xfercomp_done;
995 		} else {
996 			qtd->complete_split = 0;
997 		}
998 	}
999 
1000 	/* Update the QTD and URB states */
1001 	switch (pipe_type) {
1002 	case USB_ENDPOINT_XFER_CONTROL:
1003 		switch (qtd->control_phase) {
1004 		case DWC2_CONTROL_SETUP:
1005 			if (urb->length > 0)
1006 				qtd->control_phase = DWC2_CONTROL_DATA;
1007 			else
1008 				qtd->control_phase = DWC2_CONTROL_STATUS;
1009 			dev_vdbg(hsotg->dev,
1010 				 "  Control setup transaction done\n");
1011 			halt_status = DWC2_HC_XFER_COMPLETE;
1012 			break;
1013 		case DWC2_CONTROL_DATA:
1014 			urb_xfer_done = dwc2_update_urb_state(hsotg, chan,
1015 							      chnum, urb, qtd);
1016 			if (urb_xfer_done) {
1017 				qtd->control_phase = DWC2_CONTROL_STATUS;
1018 				dev_vdbg(hsotg->dev,
1019 					 "  Control data transfer done\n");
1020 			} else {
1021 				dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
1022 							  qtd);
1023 			}
1024 			halt_status = DWC2_HC_XFER_COMPLETE;
1025 			break;
1026 		case DWC2_CONTROL_STATUS:
1027 			dev_vdbg(hsotg->dev, "  Control transfer complete\n");
1028 			if (urb->status == -EINPROGRESS)
1029 				urb->status = 0;
1030 			dwc2_host_complete(hsotg, qtd, urb->status);
1031 			halt_status = DWC2_HC_XFER_URB_COMPLETE;
1032 			break;
1033 		}
1034 
1035 		dwc2_complete_non_periodic_xfer(hsotg, chan, chnum, qtd,
1036 						halt_status);
1037 		break;
1038 	case USB_ENDPOINT_XFER_BULK:
1039 		dev_vdbg(hsotg->dev, "  Bulk transfer complete\n");
1040 		urb_xfer_done = dwc2_update_urb_state(hsotg, chan, chnum, urb,
1041 						      qtd);
1042 		if (urb_xfer_done) {
1043 			dwc2_host_complete(hsotg, qtd, urb->status);
1044 			halt_status = DWC2_HC_XFER_URB_COMPLETE;
1045 		} else {
1046 			halt_status = DWC2_HC_XFER_COMPLETE;
1047 		}
1048 
1049 		dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1050 		dwc2_complete_non_periodic_xfer(hsotg, chan, chnum, qtd,
1051 						halt_status);
1052 		break;
1053 	case USB_ENDPOINT_XFER_INT:
1054 		dev_vdbg(hsotg->dev, "  Interrupt transfer complete\n");
1055 		urb_xfer_done = dwc2_update_urb_state(hsotg, chan, chnum, urb,
1056 						      qtd);
1057 
1058 		/*
1059 		 * Interrupt URB is done on the first transfer complete
1060 		 * interrupt
1061 		 */
1062 		if (urb_xfer_done) {
1063 			dwc2_host_complete(hsotg, qtd, urb->status);
1064 			halt_status = DWC2_HC_XFER_URB_COMPLETE;
1065 		} else {
1066 			halt_status = DWC2_HC_XFER_COMPLETE;
1067 		}
1068 
1069 		dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1070 		dwc2_complete_periodic_xfer(hsotg, chan, chnum, qtd,
1071 					    halt_status);
1072 		break;
1073 	case USB_ENDPOINT_XFER_ISOC:
1074 		if (dbg_perio())
1075 			dev_vdbg(hsotg->dev, "  Isochronous transfer complete\n");
1076 		if (qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_ALL)
1077 			halt_status = dwc2_update_isoc_urb_state(hsotg, chan,
1078 							chnum, qtd,
1079 							DWC2_HC_XFER_COMPLETE);
1080 		dwc2_complete_periodic_xfer(hsotg, chan, chnum, qtd,
1081 					    halt_status);
1082 		break;
1083 	}
1084 
1085 handle_xfercomp_done:
1086 	disable_hc_int(hsotg, chnum, HCINTMSK_XFERCOMPL);
1087 }
1088 
1089 /*
1090  * Handles a host channel STALL interrupt. This handler may be called in
1091  * either DMA mode or Slave mode.
1092  */
1093 static void dwc2_hc_stall_intr(struct dwc2_hsotg *hsotg,
1094 			       struct dwc2_host_chan *chan, int chnum,
1095 			       struct dwc2_qtd *qtd)
1096 {
1097 	struct dwc2_hcd_urb *urb = qtd->urb;
1098 	int pipe_type;
1099 
1100 	dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: STALL Received--\n",
1101 		chnum);
1102 
1103 	if (hsotg->params.dma_desc_enable) {
1104 		dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1105 					    DWC2_HC_XFER_STALL);
1106 		goto handle_stall_done;
1107 	}
1108 
1109 	if (!urb)
1110 		goto handle_stall_halt;
1111 
1112 	pipe_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
1113 
1114 	if (pipe_type == USB_ENDPOINT_XFER_CONTROL)
1115 		dwc2_host_complete(hsotg, qtd, -EPIPE);
1116 
1117 	if (pipe_type == USB_ENDPOINT_XFER_BULK ||
1118 	    pipe_type == USB_ENDPOINT_XFER_INT) {
1119 		dwc2_host_complete(hsotg, qtd, -EPIPE);
1120 		/*
1121 		 * USB protocol requires resetting the data toggle for bulk
1122 		 * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
1123 		 * setup command is issued to the endpoint. Anticipate the
1124 		 * CLEAR_FEATURE command since a STALL has occurred and reset
1125 		 * the data toggle now.
1126 		 */
1127 		chan->qh->data_toggle = 0;
1128 	}
1129 
1130 handle_stall_halt:
1131 	dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_STALL);
1132 
1133 handle_stall_done:
1134 	disable_hc_int(hsotg, chnum, HCINTMSK_STALL);
1135 }
1136 
1137 /*
1138  * Updates the state of the URB when a transfer has been stopped due to an
1139  * abnormal condition before the transfer completes. Modifies the
1140  * actual_length field of the URB to reflect the number of bytes that have
1141  * actually been transferred via the host channel.
1142  */
1143 static void dwc2_update_urb_state_abn(struct dwc2_hsotg *hsotg,
1144 				      struct dwc2_host_chan *chan, int chnum,
1145 				      struct dwc2_hcd_urb *urb,
1146 				      struct dwc2_qtd *qtd,
1147 				      enum dwc2_halt_status halt_status)
1148 {
1149 	u32 xfer_length = dwc2_get_actual_xfer_length(hsotg, chan, chnum,
1150 						      qtd, halt_status, NULL);
1151 	u32 hctsiz;
1152 
1153 	if (urb->actual_length + xfer_length > urb->length) {
1154 		dev_warn(hsotg->dev, "%s(): trimming xfer length\n", __func__);
1155 		xfer_length = urb->length - urb->actual_length;
1156 	}
1157 
1158 	urb->actual_length += xfer_length;
1159 
1160 	hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
1161 	dev_vdbg(hsotg->dev, "DWC_otg: %s: %s, channel %d\n",
1162 		 __func__, (chan->ep_is_in ? "IN" : "OUT"), chnum);
1163 	dev_vdbg(hsotg->dev, "  chan->start_pkt_count %d\n",
1164 		 chan->start_pkt_count);
1165 	dev_vdbg(hsotg->dev, "  hctsiz.pktcnt %d\n",
1166 		 (hctsiz & TSIZ_PKTCNT_MASK) >> TSIZ_PKTCNT_SHIFT);
1167 	dev_vdbg(hsotg->dev, "  chan->max_packet %d\n", chan->max_packet);
1168 	dev_vdbg(hsotg->dev, "  bytes_transferred %d\n",
1169 		 xfer_length);
1170 	dev_vdbg(hsotg->dev, "  urb->actual_length %d\n",
1171 		 urb->actual_length);
1172 	dev_vdbg(hsotg->dev, "  urb->transfer_buffer_length %d\n",
1173 		 urb->length);
1174 }
1175 
1176 /*
1177  * Handles a host channel NAK interrupt. This handler may be called in either
1178  * DMA mode or Slave mode.
1179  */
1180 static void dwc2_hc_nak_intr(struct dwc2_hsotg *hsotg,
1181 			     struct dwc2_host_chan *chan, int chnum,
1182 			     struct dwc2_qtd *qtd)
1183 {
1184 	if (!qtd) {
1185 		dev_dbg(hsotg->dev, "%s: qtd is NULL\n", __func__);
1186 		return;
1187 	}
1188 
1189 	if (!qtd->urb) {
1190 		dev_dbg(hsotg->dev, "%s: qtd->urb is NULL\n", __func__);
1191 		return;
1192 	}
1193 
1194 	if (dbg_hc(chan))
1195 		dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: NAK Received--\n",
1196 			 chnum);
1197 
1198 	/*
1199 	 * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
1200 	 * interrupt. Re-start the SSPLIT transfer.
1201 	 *
1202 	 * Normally for non-periodic transfers we'll retry right away, but to
1203 	 * avoid interrupt storms we'll wait before retrying if we've got
1204 	 * several NAKs. If we didn't do this we'd retry directly from the
1205 	 * interrupt handler and could end up quickly getting another
1206 	 * interrupt (another NAK), which we'd retry. Note that we do not
1207 	 * delay retries for IN parts of control requests, as those are expected
1208 	 * to complete fairly quickly, and if we delay them we risk confusing
1209 	 * the device and cause it issue STALL.
1210 	 *
1211 	 * Note that in DMA mode software only gets involved to re-send NAKed
1212 	 * transfers for split transactions, so we only need to apply this
1213 	 * delaying logic when handling splits. In non-DMA mode presumably we
1214 	 * might want a similar delay if someone can demonstrate this problem
1215 	 * affects that code path too.
1216 	 */
1217 	if (chan->do_split) {
1218 		if (chan->complete_split)
1219 			qtd->error_count = 0;
1220 		qtd->complete_split = 0;
1221 		qtd->num_naks++;
1222 		qtd->qh->want_wait = qtd->num_naks >= DWC2_NAKS_BEFORE_DELAY &&
1223 				!(chan->ep_type == USB_ENDPOINT_XFER_CONTROL &&
1224 				  chan->ep_is_in);
1225 		dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
1226 		goto handle_nak_done;
1227 	}
1228 
1229 	switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
1230 	case USB_ENDPOINT_XFER_CONTROL:
1231 	case USB_ENDPOINT_XFER_BULK:
1232 		if (hsotg->params.host_dma && chan->ep_is_in) {
1233 			/*
1234 			 * NAK interrupts are enabled on bulk/control IN
1235 			 * transfers in DMA mode for the sole purpose of
1236 			 * resetting the error count after a transaction error
1237 			 * occurs. The core will continue transferring data.
1238 			 */
1239 			qtd->error_count = 0;
1240 			break;
1241 		}
1242 
1243 		/*
1244 		 * NAK interrupts normally occur during OUT transfers in DMA
1245 		 * or Slave mode. For IN transfers, more requests will be
1246 		 * queued as request queue space is available.
1247 		 */
1248 		qtd->error_count = 0;
1249 
1250 		if (!chan->qh->ping_state) {
1251 			dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
1252 						  qtd, DWC2_HC_XFER_NAK);
1253 			dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1254 
1255 			if (chan->speed == USB_SPEED_HIGH)
1256 				chan->qh->ping_state = 1;
1257 		}
1258 
1259 		/*
1260 		 * Halt the channel so the transfer can be re-started from
1261 		 * the appropriate point or the PING protocol will
1262 		 * start/continue
1263 		 */
1264 		dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
1265 		break;
1266 	case USB_ENDPOINT_XFER_INT:
1267 		qtd->error_count = 0;
1268 		dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
1269 		break;
1270 	case USB_ENDPOINT_XFER_ISOC:
1271 		/* Should never get called for isochronous transfers */
1272 		dev_err(hsotg->dev, "NACK interrupt for ISOC transfer\n");
1273 		break;
1274 	}
1275 
1276 handle_nak_done:
1277 	disable_hc_int(hsotg, chnum, HCINTMSK_NAK);
1278 }
1279 
1280 /*
1281  * Handles a host channel ACK interrupt. This interrupt is enabled when
1282  * performing the PING protocol in Slave mode, when errors occur during
1283  * either Slave mode or DMA mode, and during Start Split transactions.
1284  */
1285 static void dwc2_hc_ack_intr(struct dwc2_hsotg *hsotg,
1286 			     struct dwc2_host_chan *chan, int chnum,
1287 			     struct dwc2_qtd *qtd)
1288 {
1289 	struct dwc2_hcd_iso_packet_desc *frame_desc;
1290 
1291 	if (dbg_hc(chan))
1292 		dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: ACK Received--\n",
1293 			 chnum);
1294 
1295 	if (chan->do_split) {
1296 		/* Handle ACK on SSPLIT. ACK should not occur in CSPLIT. */
1297 		if (!chan->ep_is_in &&
1298 		    chan->data_pid_start != DWC2_HC_PID_SETUP)
1299 			qtd->ssplit_out_xfer_count = chan->xfer_len;
1300 
1301 		if (chan->ep_type != USB_ENDPOINT_XFER_ISOC || chan->ep_is_in) {
1302 			qtd->complete_split = 1;
1303 			dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_ACK);
1304 		} else {
1305 			/* ISOC OUT */
1306 			switch (chan->xact_pos) {
1307 			case DWC2_HCSPLT_XACTPOS_ALL:
1308 				break;
1309 			case DWC2_HCSPLT_XACTPOS_END:
1310 				qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
1311 				qtd->isoc_split_offset = 0;
1312 				break;
1313 			case DWC2_HCSPLT_XACTPOS_BEGIN:
1314 			case DWC2_HCSPLT_XACTPOS_MID:
1315 				/*
1316 				 * For BEGIN or MID, calculate the length for
1317 				 * the next microframe to determine the correct
1318 				 * SSPLIT token, either MID or END
1319 				 */
1320 				frame_desc = &qtd->urb->iso_descs[
1321 						qtd->isoc_frame_index];
1322 				qtd->isoc_split_offset += 188;
1323 
1324 				if (frame_desc->length - qtd->isoc_split_offset
1325 							<= 188)
1326 					qtd->isoc_split_pos =
1327 							DWC2_HCSPLT_XACTPOS_END;
1328 				else
1329 					qtd->isoc_split_pos =
1330 							DWC2_HCSPLT_XACTPOS_MID;
1331 				break;
1332 			}
1333 		}
1334 	} else {
1335 		qtd->error_count = 0;
1336 
1337 		if (chan->qh->ping_state) {
1338 			chan->qh->ping_state = 0;
1339 			/*
1340 			 * Halt the channel so the transfer can be re-started
1341 			 * from the appropriate point. This only happens in
1342 			 * Slave mode. In DMA mode, the ping_state is cleared
1343 			 * when the transfer is started because the core
1344 			 * automatically executes the PING, then the transfer.
1345 			 */
1346 			dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_ACK);
1347 		}
1348 	}
1349 
1350 	/*
1351 	 * If the ACK occurred when _not_ in the PING state, let the channel
1352 	 * continue transferring data after clearing the error count
1353 	 */
1354 	disable_hc_int(hsotg, chnum, HCINTMSK_ACK);
1355 }
1356 
1357 /*
1358  * Handles a host channel NYET interrupt. This interrupt should only occur on
1359  * Bulk and Control OUT endpoints and for complete split transactions. If a
1360  * NYET occurs at the same time as a Transfer Complete interrupt, it is
1361  * handled in the xfercomp interrupt handler, not here. This handler may be
1362  * called in either DMA mode or Slave mode.
1363  */
1364 static void dwc2_hc_nyet_intr(struct dwc2_hsotg *hsotg,
1365 			      struct dwc2_host_chan *chan, int chnum,
1366 			      struct dwc2_qtd *qtd)
1367 {
1368 	if (dbg_hc(chan))
1369 		dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: NYET Received--\n",
1370 			 chnum);
1371 
1372 	/*
1373 	 * NYET on CSPLIT
1374 	 * re-do the CSPLIT immediately on non-periodic
1375 	 */
1376 	if (chan->do_split && chan->complete_split) {
1377 		if (chan->ep_is_in && chan->ep_type == USB_ENDPOINT_XFER_ISOC &&
1378 		    hsotg->params.host_dma) {
1379 			qtd->complete_split = 0;
1380 			qtd->isoc_split_offset = 0;
1381 			qtd->isoc_frame_index++;
1382 			if (qtd->urb &&
1383 			    qtd->isoc_frame_index == qtd->urb->packet_count) {
1384 				dwc2_host_complete(hsotg, qtd, 0);
1385 				dwc2_release_channel(hsotg, chan, qtd,
1386 						     DWC2_HC_XFER_URB_COMPLETE);
1387 			} else {
1388 				dwc2_release_channel(hsotg, chan, qtd,
1389 						DWC2_HC_XFER_NO_HALT_STATUS);
1390 			}
1391 			goto handle_nyet_done;
1392 		}
1393 
1394 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1395 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1396 			struct dwc2_qh *qh = chan->qh;
1397 			bool past_end;
1398 
1399 			if (!hsotg->params.uframe_sched) {
1400 				int frnum = dwc2_hcd_get_frame_number(hsotg);
1401 
1402 				/* Don't have num_hs_transfers; simple logic */
1403 				past_end = dwc2_full_frame_num(frnum) !=
1404 				     dwc2_full_frame_num(qh->next_active_frame);
1405 			} else {
1406 				int end_frnum;
1407 
1408 				/*
1409 				 * Figure out the end frame based on
1410 				 * schedule.
1411 				 *
1412 				 * We don't want to go on trying again
1413 				 * and again forever. Let's stop when
1414 				 * we've done all the transfers that
1415 				 * were scheduled.
1416 				 *
1417 				 * We're going to be comparing
1418 				 * start_active_frame and
1419 				 * next_active_frame, both of which
1420 				 * are 1 before the time the packet
1421 				 * goes on the wire, so that cancels
1422 				 * out. Basically if had 1 transfer
1423 				 * and we saw 1 NYET then we're done.
1424 				 * We're getting a NYET here so if
1425 				 * next >= (start + num_transfers)
1426 				 * we're done. The complexity is that
1427 				 * for all but ISOC_OUT we skip one
1428 				 * slot.
1429 				 */
1430 				end_frnum = dwc2_frame_num_inc(
1431 					qh->start_active_frame,
1432 					qh->num_hs_transfers);
1433 
1434 				if (qh->ep_type != USB_ENDPOINT_XFER_ISOC ||
1435 				    qh->ep_is_in)
1436 					end_frnum =
1437 					       dwc2_frame_num_inc(end_frnum, 1);
1438 
1439 				past_end = dwc2_frame_num_le(
1440 					end_frnum, qh->next_active_frame);
1441 			}
1442 
1443 			if (past_end) {
1444 				/* Treat this as a transaction error. */
1445 #if 0
1446 				/*
1447 				 * Todo: Fix system performance so this can
1448 				 * be treated as an error. Right now complete
1449 				 * splits cannot be scheduled precisely enough
1450 				 * due to other system activity, so this error
1451 				 * occurs regularly in Slave mode.
1452 				 */
1453 				qtd->error_count++;
1454 #endif
1455 				qtd->complete_split = 0;
1456 				dwc2_halt_channel(hsotg, chan, qtd,
1457 						  DWC2_HC_XFER_XACT_ERR);
1458 				/* Todo: add support for isoc release */
1459 				goto handle_nyet_done;
1460 			}
1461 		}
1462 
1463 		dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NYET);
1464 		goto handle_nyet_done;
1465 	}
1466 
1467 	chan->qh->ping_state = 1;
1468 	qtd->error_count = 0;
1469 
1470 	dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb, qtd,
1471 				  DWC2_HC_XFER_NYET);
1472 	dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1473 
1474 	/*
1475 	 * Halt the channel and re-start the transfer so the PING protocol
1476 	 * will start
1477 	 */
1478 	dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NYET);
1479 
1480 handle_nyet_done:
1481 	disable_hc_int(hsotg, chnum, HCINTMSK_NYET);
1482 }
1483 
1484 /*
1485  * Handles a host channel babble interrupt. This handler may be called in
1486  * either DMA mode or Slave mode.
1487  */
1488 static void dwc2_hc_babble_intr(struct dwc2_hsotg *hsotg,
1489 				struct dwc2_host_chan *chan, int chnum,
1490 				struct dwc2_qtd *qtd)
1491 {
1492 	dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: Babble Error--\n",
1493 		chnum);
1494 
1495 	dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1496 
1497 	if (hsotg->params.dma_desc_enable) {
1498 		dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1499 					    DWC2_HC_XFER_BABBLE_ERR);
1500 		goto disable_int;
1501 	}
1502 
1503 	if (chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
1504 		dwc2_host_complete(hsotg, qtd, -EOVERFLOW);
1505 		dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_BABBLE_ERR);
1506 	} else {
1507 		enum dwc2_halt_status halt_status;
1508 
1509 		halt_status = dwc2_update_isoc_urb_state(hsotg, chan, chnum,
1510 						qtd, DWC2_HC_XFER_BABBLE_ERR);
1511 		dwc2_halt_channel(hsotg, chan, qtd, halt_status);
1512 	}
1513 
1514 disable_int:
1515 	disable_hc_int(hsotg, chnum, HCINTMSK_BBLERR);
1516 }
1517 
1518 /*
1519  * Handles a host channel AHB error interrupt. This handler is only called in
1520  * DMA mode.
1521  */
1522 static void dwc2_hc_ahberr_intr(struct dwc2_hsotg *hsotg,
1523 				struct dwc2_host_chan *chan, int chnum,
1524 				struct dwc2_qtd *qtd)
1525 {
1526 	struct dwc2_hcd_urb *urb = qtd->urb;
1527 	char *pipetype, *speed;
1528 	u32 hcchar;
1529 	u32 hcsplt;
1530 	u32 hctsiz;
1531 	u32 hc_dma;
1532 
1533 	dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: AHB Error--\n",
1534 		chnum);
1535 
1536 	if (!urb)
1537 		goto handle_ahberr_halt;
1538 
1539 	dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1540 
1541 	hcchar = dwc2_readl(hsotg, HCCHAR(chnum));
1542 	hcsplt = dwc2_readl(hsotg, HCSPLT(chnum));
1543 	hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
1544 	hc_dma = dwc2_readl(hsotg, HCDMA(chnum));
1545 
1546 	dev_err(hsotg->dev, "AHB ERROR, Channel %d\n", chnum);
1547 	dev_err(hsotg->dev, "  hcchar 0x%08x, hcsplt 0x%08x\n", hcchar, hcsplt);
1548 	dev_err(hsotg->dev, "  hctsiz 0x%08x, hc_dma 0x%08x\n", hctsiz, hc_dma);
1549 	dev_err(hsotg->dev, "  Device address: %d\n",
1550 		dwc2_hcd_get_dev_addr(&urb->pipe_info));
1551 	dev_err(hsotg->dev, "  Endpoint: %d, %s\n",
1552 		dwc2_hcd_get_ep_num(&urb->pipe_info),
1553 		dwc2_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
1554 
1555 	switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
1556 	case USB_ENDPOINT_XFER_CONTROL:
1557 		pipetype = "CONTROL";
1558 		break;
1559 	case USB_ENDPOINT_XFER_BULK:
1560 		pipetype = "BULK";
1561 		break;
1562 	case USB_ENDPOINT_XFER_INT:
1563 		pipetype = "INTERRUPT";
1564 		break;
1565 	case USB_ENDPOINT_XFER_ISOC:
1566 		pipetype = "ISOCHRONOUS";
1567 		break;
1568 	default:
1569 		pipetype = "UNKNOWN";
1570 		break;
1571 	}
1572 
1573 	dev_err(hsotg->dev, "  Endpoint type: %s\n", pipetype);
1574 
1575 	switch (chan->speed) {
1576 	case USB_SPEED_HIGH:
1577 		speed = "HIGH";
1578 		break;
1579 	case USB_SPEED_FULL:
1580 		speed = "FULL";
1581 		break;
1582 	case USB_SPEED_LOW:
1583 		speed = "LOW";
1584 		break;
1585 	default:
1586 		speed = "UNKNOWN";
1587 		break;
1588 	}
1589 
1590 	dev_err(hsotg->dev, "  Speed: %s\n", speed);
1591 
1592 	dev_err(hsotg->dev, "  Max packet size: %d (mult %d)\n",
1593 		dwc2_hcd_get_maxp(&urb->pipe_info),
1594 		dwc2_hcd_get_maxp_mult(&urb->pipe_info));
1595 	dev_err(hsotg->dev, "  Data buffer length: %d\n", urb->length);
1596 	dev_err(hsotg->dev, "  Transfer buffer: %p, Transfer DMA: %08lx\n",
1597 		urb->buf, (unsigned long)urb->dma);
1598 	dev_err(hsotg->dev, "  Setup buffer: %p, Setup DMA: %08lx\n",
1599 		urb->setup_packet, (unsigned long)urb->setup_dma);
1600 	dev_err(hsotg->dev, "  Interval: %d\n", urb->interval);
1601 
1602 	/* Core halts the channel for Descriptor DMA mode */
1603 	if (hsotg->params.dma_desc_enable) {
1604 		dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1605 					    DWC2_HC_XFER_AHB_ERR);
1606 		goto handle_ahberr_done;
1607 	}
1608 
1609 	dwc2_host_complete(hsotg, qtd, -EIO);
1610 
1611 handle_ahberr_halt:
1612 	/*
1613 	 * Force a channel halt. Don't call dwc2_halt_channel because that won't
1614 	 * write to the HCCHARn register in DMA mode to force the halt.
1615 	 */
1616 	dwc2_hc_halt(hsotg, chan, DWC2_HC_XFER_AHB_ERR);
1617 
1618 handle_ahberr_done:
1619 	disable_hc_int(hsotg, chnum, HCINTMSK_AHBERR);
1620 }
1621 
1622 /*
1623  * Handles a host channel transaction error interrupt. This handler may be
1624  * called in either DMA mode or Slave mode.
1625  */
1626 static void dwc2_hc_xacterr_intr(struct dwc2_hsotg *hsotg,
1627 				 struct dwc2_host_chan *chan, int chnum,
1628 				 struct dwc2_qtd *qtd)
1629 {
1630 	dev_dbg(hsotg->dev,
1631 		"--Host Channel %d Interrupt: Transaction Error--\n", chnum);
1632 
1633 	dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1634 
1635 	if (hsotg->params.dma_desc_enable) {
1636 		dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1637 					    DWC2_HC_XFER_XACT_ERR);
1638 		goto handle_xacterr_done;
1639 	}
1640 
1641 	switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
1642 	case USB_ENDPOINT_XFER_CONTROL:
1643 	case USB_ENDPOINT_XFER_BULK:
1644 		qtd->error_count++;
1645 		if (!chan->qh->ping_state) {
1646 			dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
1647 						  qtd, DWC2_HC_XFER_XACT_ERR);
1648 			dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1649 			if (!chan->ep_is_in && chan->speed == USB_SPEED_HIGH)
1650 				chan->qh->ping_state = 1;
1651 		}
1652 
1653 		/*
1654 		 * Halt the channel so the transfer can be re-started from
1655 		 * the appropriate point or the PING protocol will start
1656 		 */
1657 		dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
1658 		break;
1659 	case USB_ENDPOINT_XFER_INT:
1660 		qtd->error_count++;
1661 		if (chan->do_split && chan->complete_split)
1662 			qtd->complete_split = 0;
1663 		dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
1664 		break;
1665 	case USB_ENDPOINT_XFER_ISOC:
1666 		{
1667 			enum dwc2_halt_status halt_status;
1668 
1669 			halt_status = dwc2_update_isoc_urb_state(hsotg, chan,
1670 					 chnum, qtd, DWC2_HC_XFER_XACT_ERR);
1671 			dwc2_halt_channel(hsotg, chan, qtd, halt_status);
1672 		}
1673 		break;
1674 	}
1675 
1676 handle_xacterr_done:
1677 	disable_hc_int(hsotg, chnum, HCINTMSK_XACTERR);
1678 }
1679 
1680 /*
1681  * Handles a host channel frame overrun interrupt. This handler may be called
1682  * in either DMA mode or Slave mode.
1683  */
1684 static void dwc2_hc_frmovrun_intr(struct dwc2_hsotg *hsotg,
1685 				  struct dwc2_host_chan *chan, int chnum,
1686 				  struct dwc2_qtd *qtd)
1687 {
1688 	enum dwc2_halt_status halt_status;
1689 
1690 	if (dbg_hc(chan))
1691 		dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: Frame Overrun--\n",
1692 			chnum);
1693 
1694 	dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1695 
1696 	switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
1697 	case USB_ENDPOINT_XFER_CONTROL:
1698 	case USB_ENDPOINT_XFER_BULK:
1699 		break;
1700 	case USB_ENDPOINT_XFER_INT:
1701 		dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_FRAME_OVERRUN);
1702 		break;
1703 	case USB_ENDPOINT_XFER_ISOC:
1704 		halt_status = dwc2_update_isoc_urb_state(hsotg, chan, chnum,
1705 					qtd, DWC2_HC_XFER_FRAME_OVERRUN);
1706 		dwc2_halt_channel(hsotg, chan, qtd, halt_status);
1707 		break;
1708 	}
1709 
1710 	disable_hc_int(hsotg, chnum, HCINTMSK_FRMOVRUN);
1711 }
1712 
1713 /*
1714  * Handles a host channel data toggle error interrupt. This handler may be
1715  * called in either DMA mode or Slave mode.
1716  */
1717 static void dwc2_hc_datatglerr_intr(struct dwc2_hsotg *hsotg,
1718 				    struct dwc2_host_chan *chan, int chnum,
1719 				    struct dwc2_qtd *qtd)
1720 {
1721 	dev_dbg(hsotg->dev,
1722 		"--Host Channel %d Interrupt: Data Toggle Error--\n", chnum);
1723 
1724 	if (chan->ep_is_in)
1725 		qtd->error_count = 0;
1726 	else
1727 		dev_err(hsotg->dev,
1728 			"Data Toggle Error on OUT transfer, channel %d\n",
1729 			chnum);
1730 
1731 	dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1732 	disable_hc_int(hsotg, chnum, HCINTMSK_DATATGLERR);
1733 }
1734 
1735 /*
1736  * For debug only. It checks that a valid halt status is set and that
1737  * HCCHARn.chdis is clear. If there's a problem, corrective action is
1738  * taken and a warning is issued.
1739  *
1740  * Return: true if halt status is ok, false otherwise
1741  */
1742 static bool dwc2_halt_status_ok(struct dwc2_hsotg *hsotg,
1743 				struct dwc2_host_chan *chan, int chnum,
1744 				struct dwc2_qtd *qtd)
1745 {
1746 #ifdef DEBUG
1747 	u32 hcchar;
1748 	u32 hctsiz;
1749 	u32 hcintmsk;
1750 	u32 hcsplt;
1751 
1752 	if (chan->halt_status == DWC2_HC_XFER_NO_HALT_STATUS) {
1753 		/*
1754 		 * This code is here only as a check. This condition should
1755 		 * never happen. Ignore the halt if it does occur.
1756 		 */
1757 		hcchar = dwc2_readl(hsotg, HCCHAR(chnum));
1758 		hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
1759 		hcintmsk = dwc2_readl(hsotg, HCINTMSK(chnum));
1760 		hcsplt = dwc2_readl(hsotg, HCSPLT(chnum));
1761 		dev_dbg(hsotg->dev,
1762 			"%s: chan->halt_status DWC2_HC_XFER_NO_HALT_STATUS,\n",
1763 			 __func__);
1764 		dev_dbg(hsotg->dev,
1765 			"channel %d, hcchar 0x%08x, hctsiz 0x%08x,\n",
1766 			chnum, hcchar, hctsiz);
1767 		dev_dbg(hsotg->dev,
1768 			"hcint 0x%08x, hcintmsk 0x%08x, hcsplt 0x%08x,\n",
1769 			chan->hcint, hcintmsk, hcsplt);
1770 		if (qtd)
1771 			dev_dbg(hsotg->dev, "qtd->complete_split %d\n",
1772 				qtd->complete_split);
1773 		dev_warn(hsotg->dev,
1774 			 "%s: no halt status, channel %d, ignoring interrupt\n",
1775 			 __func__, chnum);
1776 		return false;
1777 	}
1778 
1779 	/*
1780 	 * This code is here only as a check. hcchar.chdis should never be set
1781 	 * when the halt interrupt occurs. Halt the channel again if it does
1782 	 * occur.
1783 	 */
1784 	hcchar = dwc2_readl(hsotg, HCCHAR(chnum));
1785 	if (hcchar & HCCHAR_CHDIS) {
1786 		dev_warn(hsotg->dev,
1787 			 "%s: hcchar.chdis set unexpectedly, hcchar 0x%08x, trying to halt again\n",
1788 			 __func__, hcchar);
1789 		chan->halt_pending = 0;
1790 		dwc2_halt_channel(hsotg, chan, qtd, chan->halt_status);
1791 		return false;
1792 	}
1793 #endif
1794 
1795 	return true;
1796 }
1797 
1798 /*
1799  * Handles a host Channel Halted interrupt in DMA mode. This handler
1800  * determines the reason the channel halted and proceeds accordingly.
1801  */
1802 static void dwc2_hc_chhltd_intr_dma(struct dwc2_hsotg *hsotg,
1803 				    struct dwc2_host_chan *chan, int chnum,
1804 				    struct dwc2_qtd *qtd)
1805 {
1806 	u32 hcintmsk;
1807 	int out_nak_enh = 0;
1808 
1809 	if (dbg_hc(chan))
1810 		dev_vdbg(hsotg->dev,
1811 			 "--Host Channel %d Interrupt: DMA Channel Halted--\n",
1812 			 chnum);
1813 
1814 	/*
1815 	 * For core with OUT NAK enhancement, the flow for high-speed
1816 	 * CONTROL/BULK OUT is handled a little differently
1817 	 */
1818 	if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_71a) {
1819 		if (chan->speed == USB_SPEED_HIGH && !chan->ep_is_in &&
1820 		    (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1821 		     chan->ep_type == USB_ENDPOINT_XFER_BULK)) {
1822 			out_nak_enh = 1;
1823 		}
1824 	}
1825 
1826 	if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1827 	    (chan->halt_status == DWC2_HC_XFER_AHB_ERR &&
1828 	     !hsotg->params.dma_desc_enable)) {
1829 		if (hsotg->params.dma_desc_enable)
1830 			dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1831 						    chan->halt_status);
1832 		else
1833 			/*
1834 			 * Just release the channel. A dequeue can happen on a
1835 			 * transfer timeout. In the case of an AHB Error, the
1836 			 * channel was forced to halt because there's no way to
1837 			 * gracefully recover.
1838 			 */
1839 			dwc2_release_channel(hsotg, chan, qtd,
1840 					     chan->halt_status);
1841 		return;
1842 	}
1843 
1844 	hcintmsk = dwc2_readl(hsotg, HCINTMSK(chnum));
1845 
1846 	if (chan->hcint & HCINTMSK_XFERCOMPL) {
1847 		/*
1848 		 * Todo: This is here because of a possible hardware bug. Spec
1849 		 * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
1850 		 * interrupt w/ACK bit set should occur, but I only see the
1851 		 * XFERCOMP bit, even with it masked out. This is a workaround
1852 		 * for that behavior. Should fix this when hardware is fixed.
1853 		 */
1854 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC && !chan->ep_is_in)
1855 			dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
1856 		dwc2_hc_xfercomp_intr(hsotg, chan, chnum, qtd);
1857 	} else if (chan->hcint & HCINTMSK_STALL) {
1858 		dwc2_hc_stall_intr(hsotg, chan, chnum, qtd);
1859 	} else if ((chan->hcint & HCINTMSK_XACTERR) &&
1860 		   !hsotg->params.dma_desc_enable) {
1861 		if (out_nak_enh) {
1862 			if (chan->hcint &
1863 			    (HCINTMSK_NYET | HCINTMSK_NAK | HCINTMSK_ACK)) {
1864 				dev_vdbg(hsotg->dev,
1865 					 "XactErr with NYET/NAK/ACK\n");
1866 				qtd->error_count = 0;
1867 			} else {
1868 				dev_vdbg(hsotg->dev,
1869 					 "XactErr without NYET/NAK/ACK\n");
1870 			}
1871 		}
1872 
1873 		/*
1874 		 * Must handle xacterr before nak or ack. Could get a xacterr
1875 		 * at the same time as either of these on a BULK/CONTROL OUT
1876 		 * that started with a PING. The xacterr takes precedence.
1877 		 */
1878 		dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
1879 	} else if ((chan->hcint & HCINTMSK_XCS_XACT) &&
1880 		   hsotg->params.dma_desc_enable) {
1881 		dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
1882 	} else if ((chan->hcint & HCINTMSK_AHBERR) &&
1883 		   hsotg->params.dma_desc_enable) {
1884 		dwc2_hc_ahberr_intr(hsotg, chan, chnum, qtd);
1885 	} else if (chan->hcint & HCINTMSK_BBLERR) {
1886 		dwc2_hc_babble_intr(hsotg, chan, chnum, qtd);
1887 	} else if (chan->hcint & HCINTMSK_FRMOVRUN) {
1888 		dwc2_hc_frmovrun_intr(hsotg, chan, chnum, qtd);
1889 	} else if (!out_nak_enh) {
1890 		if (chan->hcint & HCINTMSK_NYET) {
1891 			/*
1892 			 * Must handle nyet before nak or ack. Could get a nyet
1893 			 * at the same time as either of those on a BULK/CONTROL
1894 			 * OUT that started with a PING. The nyet takes
1895 			 * precedence.
1896 			 */
1897 			dwc2_hc_nyet_intr(hsotg, chan, chnum, qtd);
1898 		} else if ((chan->hcint & HCINTMSK_NAK) &&
1899 			   !(hcintmsk & HCINTMSK_NAK)) {
1900 			/*
1901 			 * If nak is not masked, it's because a non-split IN
1902 			 * transfer is in an error state. In that case, the nak
1903 			 * is handled by the nak interrupt handler, not here.
1904 			 * Handle nak here for BULK/CONTROL OUT transfers, which
1905 			 * halt on a NAK to allow rewinding the buffer pointer.
1906 			 */
1907 			dwc2_hc_nak_intr(hsotg, chan, chnum, qtd);
1908 		} else if ((chan->hcint & HCINTMSK_ACK) &&
1909 			   !(hcintmsk & HCINTMSK_ACK)) {
1910 			/*
1911 			 * If ack is not masked, it's because a non-split IN
1912 			 * transfer is in an error state. In that case, the ack
1913 			 * is handled by the ack interrupt handler, not here.
1914 			 * Handle ack here for split transfers. Start splits
1915 			 * halt on ACK.
1916 			 */
1917 			dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
1918 		} else {
1919 			if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1920 			    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1921 				/*
1922 				 * A periodic transfer halted with no other
1923 				 * channel interrupts set. Assume it was halted
1924 				 * by the core because it could not be completed
1925 				 * in its scheduled (micro)frame.
1926 				 */
1927 				dev_dbg(hsotg->dev,
1928 					"%s: Halt channel %d (assume incomplete periodic transfer)\n",
1929 					__func__, chnum);
1930 				dwc2_halt_channel(hsotg, chan, qtd,
1931 					DWC2_HC_XFER_PERIODIC_INCOMPLETE);
1932 			} else {
1933 				dev_err(hsotg->dev,
1934 					"%s: Channel %d - ChHltd set, but reason is unknown\n",
1935 					__func__, chnum);
1936 				dev_err(hsotg->dev,
1937 					"hcint 0x%08x, intsts 0x%08x\n",
1938 					chan->hcint,
1939 					dwc2_readl(hsotg, GINTSTS));
1940 				goto error;
1941 			}
1942 		}
1943 	} else {
1944 		dev_info(hsotg->dev,
1945 			 "NYET/NAK/ACK/other in non-error case, 0x%08x\n",
1946 			 chan->hcint);
1947 error:
1948 		/* Failthrough: use 3-strikes rule */
1949 		qtd->error_count++;
1950 		dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
1951 					  qtd, DWC2_HC_XFER_XACT_ERR);
1952 		/*
1953 		 * We can get here after a completed transaction
1954 		 * (urb->actual_length >= urb->length) which was not reported
1955 		 * as completed. If that is the case, and we do not abort
1956 		 * the transfer, a transfer of size 0 will be enqueued
1957 		 * subsequently. If urb->actual_length is not DMA-aligned,
1958 		 * the buffer will then point to an unaligned address, and
1959 		 * the resulting behavior is undefined. Bail out in that
1960 		 * situation.
1961 		 */
1962 		if (qtd->urb->actual_length >= qtd->urb->length)
1963 			qtd->error_count = 3;
1964 		dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1965 		dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
1966 	}
1967 }
1968 
1969 /*
1970  * Handles a host channel Channel Halted interrupt
1971  *
1972  * In slave mode, this handler is called only when the driver specifically
1973  * requests a halt. This occurs during handling other host channel interrupts
1974  * (e.g. nak, xacterr, stall, nyet, etc.).
1975  *
1976  * In DMA mode, this is the interrupt that occurs when the core has finished
1977  * processing a transfer on a channel. Other host channel interrupts (except
1978  * ahberr) are disabled in DMA mode.
1979  */
1980 static void dwc2_hc_chhltd_intr(struct dwc2_hsotg *hsotg,
1981 				struct dwc2_host_chan *chan, int chnum,
1982 				struct dwc2_qtd *qtd)
1983 {
1984 	if (dbg_hc(chan))
1985 		dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: Channel Halted--\n",
1986 			 chnum);
1987 
1988 	if (hsotg->params.host_dma) {
1989 		dwc2_hc_chhltd_intr_dma(hsotg, chan, chnum, qtd);
1990 	} else {
1991 		if (!dwc2_halt_status_ok(hsotg, chan, chnum, qtd))
1992 			return;
1993 		dwc2_release_channel(hsotg, chan, qtd, chan->halt_status);
1994 	}
1995 }
1996 
1997 /*
1998  * Check if the given qtd is still the top of the list (and thus valid).
1999  *
2000  * If dwc2_hcd_qtd_unlink_and_free() has been called since we grabbed
2001  * the qtd from the top of the list, this will return false (otherwise true).
2002  */
2003 static bool dwc2_check_qtd_still_ok(struct dwc2_qtd *qtd, struct dwc2_qh *qh)
2004 {
2005 	struct dwc2_qtd *cur_head;
2006 
2007 	if (!qh)
2008 		return false;
2009 
2010 	cur_head = list_first_entry(&qh->qtd_list, struct dwc2_qtd,
2011 				    qtd_list_entry);
2012 	return (cur_head == qtd);
2013 }
2014 
2015 /* Handles interrupt for a specific Host Channel */
2016 static void dwc2_hc_n_intr(struct dwc2_hsotg *hsotg, int chnum)
2017 {
2018 	struct dwc2_qtd *qtd;
2019 	struct dwc2_host_chan *chan;
2020 	u32 hcint, hcintraw, hcintmsk;
2021 
2022 	chan = hsotg->hc_ptr_array[chnum];
2023 
2024 	hcintraw = dwc2_readl(hsotg, HCINT(chnum));
2025 	hcintmsk = dwc2_readl(hsotg, HCINTMSK(chnum));
2026 	hcint = hcintraw & hcintmsk;
2027 	dwc2_writel(hsotg, hcint, HCINT(chnum));
2028 
2029 	if (!chan) {
2030 		dev_err(hsotg->dev, "## hc_ptr_array for channel is NULL ##\n");
2031 		return;
2032 	}
2033 
2034 	if (dbg_hc(chan)) {
2035 		dev_vdbg(hsotg->dev, "--Host Channel Interrupt--, Channel %d\n",
2036 			 chnum);
2037 		dev_vdbg(hsotg->dev,
2038 			 "  hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
2039 			 hcintraw, hcintmsk, hcint);
2040 	}
2041 
2042 	/*
2043 	 * If we got an interrupt after someone called
2044 	 * dwc2_hcd_endpoint_disable() we don't want to crash below
2045 	 */
2046 	if (!chan->qh) {
2047 		dev_warn(hsotg->dev, "Interrupt on disabled channel\n");
2048 		return;
2049 	}
2050 
2051 	chan->hcint = hcintraw;
2052 
2053 	/*
2054 	 * If the channel was halted due to a dequeue, the qtd list might
2055 	 * be empty or at least the first entry will not be the active qtd.
2056 	 * In this case, take a shortcut and just release the channel.
2057 	 */
2058 	if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
2059 		/*
2060 		 * If the channel was halted, this should be the only
2061 		 * interrupt unmasked
2062 		 */
2063 		WARN_ON(hcint != HCINTMSK_CHHLTD);
2064 		if (hsotg->params.dma_desc_enable)
2065 			dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
2066 						    chan->halt_status);
2067 		else
2068 			dwc2_release_channel(hsotg, chan, NULL,
2069 					     chan->halt_status);
2070 		return;
2071 	}
2072 
2073 	if (list_empty(&chan->qh->qtd_list)) {
2074 		/*
2075 		 * TODO: Will this ever happen with the
2076 		 * DWC2_HC_XFER_URB_DEQUEUE handling above?
2077 		 */
2078 		dev_dbg(hsotg->dev, "## no QTD queued for channel %d ##\n",
2079 			chnum);
2080 		dev_dbg(hsotg->dev,
2081 			"  hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
2082 			chan->hcint, hcintmsk, hcint);
2083 		chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2084 		disable_hc_int(hsotg, chnum, HCINTMSK_CHHLTD);
2085 		chan->hcint = 0;
2086 		return;
2087 	}
2088 
2089 	qtd = list_first_entry(&chan->qh->qtd_list, struct dwc2_qtd,
2090 			       qtd_list_entry);
2091 
2092 	if (!hsotg->params.host_dma) {
2093 		if ((hcint & HCINTMSK_CHHLTD) && hcint != HCINTMSK_CHHLTD)
2094 			hcint &= ~HCINTMSK_CHHLTD;
2095 	}
2096 
2097 	if (hcint & HCINTMSK_XFERCOMPL) {
2098 		dwc2_hc_xfercomp_intr(hsotg, chan, chnum, qtd);
2099 		/*
2100 		 * If NYET occurred at same time as Xfer Complete, the NYET is
2101 		 * handled by the Xfer Complete interrupt handler. Don't want
2102 		 * to call the NYET interrupt handler in this case.
2103 		 */
2104 		hcint &= ~HCINTMSK_NYET;
2105 	}
2106 
2107 	if (hcint & HCINTMSK_CHHLTD) {
2108 		dwc2_hc_chhltd_intr(hsotg, chan, chnum, qtd);
2109 		if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2110 			goto exit;
2111 	}
2112 	if (hcint & HCINTMSK_AHBERR) {
2113 		dwc2_hc_ahberr_intr(hsotg, chan, chnum, qtd);
2114 		if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2115 			goto exit;
2116 	}
2117 	if (hcint & HCINTMSK_STALL) {
2118 		dwc2_hc_stall_intr(hsotg, chan, chnum, qtd);
2119 		if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2120 			goto exit;
2121 	}
2122 	if (hcint & HCINTMSK_NAK) {
2123 		dwc2_hc_nak_intr(hsotg, chan, chnum, qtd);
2124 		if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2125 			goto exit;
2126 	}
2127 	if (hcint & HCINTMSK_ACK) {
2128 		dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
2129 		if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2130 			goto exit;
2131 	}
2132 	if (hcint & HCINTMSK_NYET) {
2133 		dwc2_hc_nyet_intr(hsotg, chan, chnum, qtd);
2134 		if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2135 			goto exit;
2136 	}
2137 	if (hcint & HCINTMSK_XACTERR) {
2138 		dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
2139 		if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2140 			goto exit;
2141 	}
2142 	if (hcint & HCINTMSK_BBLERR) {
2143 		dwc2_hc_babble_intr(hsotg, chan, chnum, qtd);
2144 		if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2145 			goto exit;
2146 	}
2147 	if (hcint & HCINTMSK_FRMOVRUN) {
2148 		dwc2_hc_frmovrun_intr(hsotg, chan, chnum, qtd);
2149 		if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2150 			goto exit;
2151 	}
2152 	if (hcint & HCINTMSK_DATATGLERR) {
2153 		dwc2_hc_datatglerr_intr(hsotg, chan, chnum, qtd);
2154 		if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2155 			goto exit;
2156 	}
2157 
2158 exit:
2159 	chan->hcint = 0;
2160 }
2161 
2162 /*
2163  * This interrupt indicates that one or more host channels has a pending
2164  * interrupt. There are multiple conditions that can cause each host channel
2165  * interrupt. This function determines which conditions have occurred for each
2166  * host channel interrupt and handles them appropriately.
2167  */
2168 static void dwc2_hc_intr(struct dwc2_hsotg *hsotg)
2169 {
2170 	u32 haint;
2171 	int i;
2172 	struct dwc2_host_chan *chan, *chan_tmp;
2173 
2174 	haint = dwc2_readl(hsotg, HAINT);
2175 	if (dbg_perio()) {
2176 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
2177 
2178 		dev_vdbg(hsotg->dev, "HAINT=%08x\n", haint);
2179 	}
2180 
2181 	/*
2182 	 * According to USB 2.0 spec section 11.18.8, a host must
2183 	 * issue complete-split transactions in a microframe for a
2184 	 * set of full-/low-speed endpoints in the same relative
2185 	 * order as the start-splits were issued in a microframe for.
2186 	 */
2187 	list_for_each_entry_safe(chan, chan_tmp, &hsotg->split_order,
2188 				 split_order_list_entry) {
2189 		int hc_num = chan->hc_num;
2190 
2191 		if (haint & (1 << hc_num)) {
2192 			dwc2_hc_n_intr(hsotg, hc_num);
2193 			haint &= ~(1 << hc_num);
2194 		}
2195 	}
2196 
2197 	for (i = 0; i < hsotg->params.host_channels; i++) {
2198 		if (haint & (1 << i))
2199 			dwc2_hc_n_intr(hsotg, i);
2200 	}
2201 }
2202 
2203 /* This function handles interrupts for the HCD */
2204 irqreturn_t dwc2_handle_hcd_intr(struct dwc2_hsotg *hsotg)
2205 {
2206 	u32 gintsts, dbg_gintsts;
2207 	irqreturn_t retval = IRQ_HANDLED;
2208 
2209 	if (!dwc2_is_controller_alive(hsotg)) {
2210 		dev_warn(hsotg->dev, "Controller is dead\n");
2211 		return retval;
2212 	} else {
2213 		retval = IRQ_NONE;
2214 	}
2215 
2216 	spin_lock(&hsotg->lock);
2217 
2218 	/* Check if HOST Mode */
2219 	if (dwc2_is_host_mode(hsotg)) {
2220 		gintsts = dwc2_read_core_intr(hsotg);
2221 		if (!gintsts) {
2222 			spin_unlock(&hsotg->lock);
2223 			return retval;
2224 		}
2225 
2226 		retval = IRQ_HANDLED;
2227 
2228 		dbg_gintsts = gintsts;
2229 #ifndef DEBUG_SOF
2230 		dbg_gintsts &= ~GINTSTS_SOF;
2231 #endif
2232 		if (!dbg_perio())
2233 			dbg_gintsts &= ~(GINTSTS_HCHINT | GINTSTS_RXFLVL |
2234 					 GINTSTS_PTXFEMP);
2235 
2236 		/* Only print if there are any non-suppressed interrupts left */
2237 		if (dbg_gintsts)
2238 			dev_vdbg(hsotg->dev,
2239 				 "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x\n",
2240 				 gintsts);
2241 
2242 		if (gintsts & GINTSTS_SOF)
2243 			dwc2_sof_intr(hsotg);
2244 		if (gintsts & GINTSTS_RXFLVL)
2245 			dwc2_rx_fifo_level_intr(hsotg);
2246 		if (gintsts & GINTSTS_NPTXFEMP)
2247 			dwc2_np_tx_fifo_empty_intr(hsotg);
2248 		if (gintsts & GINTSTS_PRTINT)
2249 			dwc2_port_intr(hsotg);
2250 		if (gintsts & GINTSTS_HCHINT)
2251 			dwc2_hc_intr(hsotg);
2252 		if (gintsts & GINTSTS_PTXFEMP)
2253 			dwc2_perio_tx_fifo_empty_intr(hsotg);
2254 
2255 		if (dbg_gintsts) {
2256 			dev_vdbg(hsotg->dev,
2257 				 "DWC OTG HCD Finished Servicing Interrupts\n");
2258 			dev_vdbg(hsotg->dev,
2259 				 "DWC OTG HCD gintsts=0x%08x gintmsk=0x%08x\n",
2260 				 dwc2_readl(hsotg, GINTSTS),
2261 				 dwc2_readl(hsotg, GINTMSK));
2262 		}
2263 	}
2264 
2265 	spin_unlock(&hsotg->lock);
2266 
2267 	return retval;
2268 }
2269