xref: /linux/drivers/usb/host/xhci-mtk-sch.c (revision 0a94608f0f7de9b1135ffea3546afe68eafef57f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2015 MediaTek Inc.
4  * Author:
5  *  Zhigang.Wei <zhigang.wei@mediatek.com>
6  *  Chunfeng.Yun <chunfeng.yun@mediatek.com>
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 
13 #include "xhci.h"
14 #include "xhci-mtk.h"
15 
16 #define SSP_BW_BOUNDARY	130000
17 #define SS_BW_BOUNDARY	51000
18 /* table 5-5. High-speed Isoc Transaction Limits in usb_20 spec */
19 #define HS_BW_BOUNDARY	6144
20 /* usb2 spec section11.18.1: at most 188 FS bytes per microframe */
21 #define FS_PAYLOAD_MAX 188
22 /*
23  * max number of microframes for split transfer,
24  * for fs isoc in : 1 ss + 1 idle + 7 cs
25  */
26 #define TT_MICROFRAMES_MAX 9
27 
28 #define DBG_BUF_EN	64
29 
30 /* schedule error type */
31 #define ESCH_SS_Y6		1001
32 #define ESCH_SS_OVERLAP		1002
33 #define ESCH_CS_OVERFLOW	1003
34 #define ESCH_BW_OVERFLOW	1004
35 #define ESCH_FIXME		1005
36 
37 /* mtk scheduler bitmasks */
38 #define EP_BPKTS(p)	((p) & 0x7f)
39 #define EP_BCSCOUNT(p)	(((p) & 0x7) << 8)
40 #define EP_BBM(p)	((p) << 11)
41 #define EP_BOFFSET(p)	((p) & 0x3fff)
42 #define EP_BREPEAT(p)	(((p) & 0x7fff) << 16)
43 
44 static char *sch_error_string(int err_num)
45 {
46 	switch (err_num) {
47 	case ESCH_SS_Y6:
48 		return "Can't schedule Start-Split in Y6";
49 	case ESCH_SS_OVERLAP:
50 		return "Can't find a suitable Start-Split location";
51 	case ESCH_CS_OVERFLOW:
52 		return "The last Complete-Split is greater than 7";
53 	case ESCH_BW_OVERFLOW:
54 		return "Bandwidth exceeds the maximum limit";
55 	case ESCH_FIXME:
56 		return "FIXME, to be resolved";
57 	default:
58 		return "Unknown";
59 	}
60 }
61 
62 static int is_fs_or_ls(enum usb_device_speed speed)
63 {
64 	return speed == USB_SPEED_FULL || speed == USB_SPEED_LOW;
65 }
66 
67 static const char *
68 decode_ep(struct usb_host_endpoint *ep, enum usb_device_speed speed)
69 {
70 	static char buf[DBG_BUF_EN];
71 	struct usb_endpoint_descriptor *epd = &ep->desc;
72 	unsigned int interval;
73 	const char *unit;
74 
75 	interval = usb_decode_interval(epd, speed);
76 	if (interval % 1000) {
77 		unit = "us";
78 	} else {
79 		unit = "ms";
80 		interval /= 1000;
81 	}
82 
83 	snprintf(buf, DBG_BUF_EN, "%s ep%d%s %s, mpkt:%d, interval:%d/%d%s",
84 		 usb_speed_string(speed), usb_endpoint_num(epd),
85 		 usb_endpoint_dir_in(epd) ? "in" : "out",
86 		 usb_ep_type_string(usb_endpoint_type(epd)),
87 		 usb_endpoint_maxp(epd), epd->bInterval, interval, unit);
88 
89 	return buf;
90 }
91 
92 static u32 get_bw_boundary(enum usb_device_speed speed)
93 {
94 	u32 boundary;
95 
96 	switch (speed) {
97 	case USB_SPEED_SUPER_PLUS:
98 		boundary = SSP_BW_BOUNDARY;
99 		break;
100 	case USB_SPEED_SUPER:
101 		boundary = SS_BW_BOUNDARY;
102 		break;
103 	default:
104 		boundary = HS_BW_BOUNDARY;
105 		break;
106 	}
107 
108 	return boundary;
109 }
110 
111 /*
112 * get the bandwidth domain which @ep belongs to.
113 *
114 * the bandwidth domain array is saved to @sch_array of struct xhci_hcd_mtk,
115 * each HS root port is treated as a single bandwidth domain,
116 * but each SS root port is treated as two bandwidth domains, one for IN eps,
117 * one for OUT eps.
118 * @real_port value is defined as follow according to xHCI spec:
119 * 1 for SSport0, ..., N+1 for SSportN, N+2 for HSport0, N+3 for HSport1, etc
120 * so the bandwidth domain array is organized as follow for simplification:
121 * SSport0-OUT, SSport0-IN, ..., SSportX-OUT, SSportX-IN, HSport0, ..., HSportY
122 */
123 static struct mu3h_sch_bw_info *
124 get_bw_info(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
125 	    struct usb_host_endpoint *ep)
126 {
127 	struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
128 	struct xhci_virt_device *virt_dev;
129 	int bw_index;
130 
131 	virt_dev = xhci->devs[udev->slot_id];
132 	if (!virt_dev->real_port) {
133 		WARN_ONCE(1, "%s invalid real_port\n", dev_name(&udev->dev));
134 		return NULL;
135 	}
136 
137 	if (udev->speed >= USB_SPEED_SUPER) {
138 		if (usb_endpoint_dir_out(&ep->desc))
139 			bw_index = (virt_dev->real_port - 1) * 2;
140 		else
141 			bw_index = (virt_dev->real_port - 1) * 2 + 1;
142 	} else {
143 		/* add one more for each SS port */
144 		bw_index = virt_dev->real_port + xhci->usb3_rhub.num_ports - 1;
145 	}
146 
147 	return &mtk->sch_array[bw_index];
148 }
149 
150 static u32 get_esit(struct xhci_ep_ctx *ep_ctx)
151 {
152 	u32 esit;
153 
154 	esit = 1 << CTX_TO_EP_INTERVAL(le32_to_cpu(ep_ctx->ep_info));
155 	if (esit > XHCI_MTK_MAX_ESIT)
156 		esit = XHCI_MTK_MAX_ESIT;
157 
158 	return esit;
159 }
160 
161 static struct mu3h_sch_tt *find_tt(struct usb_device *udev)
162 {
163 	struct usb_tt *utt = udev->tt;
164 	struct mu3h_sch_tt *tt, **tt_index, **ptt;
165 	bool allocated_index = false;
166 
167 	if (!utt)
168 		return NULL;	/* Not below a TT */
169 
170 	/*
171 	 * Find/create our data structure.
172 	 * For hubs with a single TT, we get it directly.
173 	 * For hubs with multiple TTs, there's an extra level of pointers.
174 	 */
175 	tt_index = NULL;
176 	if (utt->multi) {
177 		tt_index = utt->hcpriv;
178 		if (!tt_index) {	/* Create the index array */
179 			tt_index = kcalloc(utt->hub->maxchild,
180 					sizeof(*tt_index), GFP_KERNEL);
181 			if (!tt_index)
182 				return ERR_PTR(-ENOMEM);
183 			utt->hcpriv = tt_index;
184 			allocated_index = true;
185 		}
186 		ptt = &tt_index[udev->ttport - 1];
187 	} else {
188 		ptt = (struct mu3h_sch_tt **) &utt->hcpriv;
189 	}
190 
191 	tt = *ptt;
192 	if (!tt) {	/* Create the mu3h_sch_tt */
193 		tt = kzalloc(sizeof(*tt), GFP_KERNEL);
194 		if (!tt) {
195 			if (allocated_index) {
196 				utt->hcpriv = NULL;
197 				kfree(tt_index);
198 			}
199 			return ERR_PTR(-ENOMEM);
200 		}
201 		INIT_LIST_HEAD(&tt->ep_list);
202 		*ptt = tt;
203 	}
204 
205 	return tt;
206 }
207 
208 /* Release the TT above udev, if it's not in use */
209 static void drop_tt(struct usb_device *udev)
210 {
211 	struct usb_tt *utt = udev->tt;
212 	struct mu3h_sch_tt *tt, **tt_index, **ptt;
213 	int i, cnt;
214 
215 	if (!utt || !utt->hcpriv)
216 		return;		/* Not below a TT, or never allocated */
217 
218 	cnt = 0;
219 	if (utt->multi) {
220 		tt_index = utt->hcpriv;
221 		ptt = &tt_index[udev->ttport - 1];
222 		/*  How many entries are left in tt_index? */
223 		for (i = 0; i < utt->hub->maxchild; ++i)
224 			cnt += !!tt_index[i];
225 	} else {
226 		tt_index = NULL;
227 		ptt = (struct mu3h_sch_tt **)&utt->hcpriv;
228 	}
229 
230 	tt = *ptt;
231 	if (!tt || !list_empty(&tt->ep_list))
232 		return;		/* never allocated , or still in use*/
233 
234 	*ptt = NULL;
235 	kfree(tt);
236 
237 	if (cnt == 1) {
238 		utt->hcpriv = NULL;
239 		kfree(tt_index);
240 	}
241 }
242 
243 static struct mu3h_sch_ep_info *
244 create_sch_ep(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
245 	      struct usb_host_endpoint *ep, struct xhci_ep_ctx *ep_ctx)
246 {
247 	struct mu3h_sch_ep_info *sch_ep;
248 	struct mu3h_sch_bw_info *bw_info;
249 	struct mu3h_sch_tt *tt = NULL;
250 	u32 len_bw_budget_table;
251 
252 	bw_info = get_bw_info(mtk, udev, ep);
253 	if (!bw_info)
254 		return ERR_PTR(-ENODEV);
255 
256 	if (is_fs_or_ls(udev->speed))
257 		len_bw_budget_table = TT_MICROFRAMES_MAX;
258 	else if ((udev->speed >= USB_SPEED_SUPER)
259 			&& usb_endpoint_xfer_isoc(&ep->desc))
260 		len_bw_budget_table = get_esit(ep_ctx);
261 	else
262 		len_bw_budget_table = 1;
263 
264 	sch_ep = kzalloc(struct_size(sch_ep, bw_budget_table,
265 				     len_bw_budget_table),
266 			 GFP_KERNEL);
267 	if (!sch_ep)
268 		return ERR_PTR(-ENOMEM);
269 
270 	if (is_fs_or_ls(udev->speed)) {
271 		tt = find_tt(udev);
272 		if (IS_ERR(tt)) {
273 			kfree(sch_ep);
274 			return ERR_PTR(-ENOMEM);
275 		}
276 	}
277 
278 	sch_ep->bw_info = bw_info;
279 	sch_ep->sch_tt = tt;
280 	sch_ep->ep = ep;
281 	sch_ep->speed = udev->speed;
282 	INIT_LIST_HEAD(&sch_ep->endpoint);
283 	INIT_LIST_HEAD(&sch_ep->tt_endpoint);
284 	INIT_HLIST_NODE(&sch_ep->hentry);
285 
286 	return sch_ep;
287 }
288 
289 static void setup_sch_info(struct xhci_ep_ctx *ep_ctx,
290 			   struct mu3h_sch_ep_info *sch_ep)
291 {
292 	u32 ep_type;
293 	u32 maxpkt;
294 	u32 max_burst;
295 	u32 mult;
296 	u32 esit_pkts;
297 	u32 max_esit_payload;
298 	u32 *bwb_table = sch_ep->bw_budget_table;
299 	int i;
300 
301 	ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
302 	maxpkt = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
303 	max_burst = CTX_TO_MAX_BURST(le32_to_cpu(ep_ctx->ep_info2));
304 	mult = CTX_TO_EP_MULT(le32_to_cpu(ep_ctx->ep_info));
305 	max_esit_payload =
306 		(CTX_TO_MAX_ESIT_PAYLOAD_HI(
307 			le32_to_cpu(ep_ctx->ep_info)) << 16) |
308 		 CTX_TO_MAX_ESIT_PAYLOAD(le32_to_cpu(ep_ctx->tx_info));
309 
310 	sch_ep->esit = get_esit(ep_ctx);
311 	sch_ep->num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
312 	sch_ep->ep_type = ep_type;
313 	sch_ep->maxpkt = maxpkt;
314 	sch_ep->offset = 0;
315 	sch_ep->burst_mode = 0;
316 	sch_ep->repeat = 0;
317 
318 	if (sch_ep->speed == USB_SPEED_HIGH) {
319 		sch_ep->cs_count = 0;
320 
321 		/*
322 		 * usb_20 spec section5.9
323 		 * a single microframe is enough for HS synchromous endpoints
324 		 * in a interval
325 		 */
326 		sch_ep->num_budget_microframes = 1;
327 
328 		/*
329 		 * xHCI spec section6.2.3.4
330 		 * @max_burst is the number of additional transactions
331 		 * opportunities per microframe
332 		 */
333 		sch_ep->pkts = max_burst + 1;
334 		sch_ep->bw_cost_per_microframe = maxpkt * sch_ep->pkts;
335 		bwb_table[0] = sch_ep->bw_cost_per_microframe;
336 	} else if (sch_ep->speed >= USB_SPEED_SUPER) {
337 		/* usb3_r1 spec section4.4.7 & 4.4.8 */
338 		sch_ep->cs_count = 0;
339 		sch_ep->burst_mode = 1;
340 		/*
341 		 * some device's (d)wBytesPerInterval is set as 0,
342 		 * then max_esit_payload is 0, so evaluate esit_pkts from
343 		 * mult and burst
344 		 */
345 		esit_pkts = DIV_ROUND_UP(max_esit_payload, maxpkt);
346 		if (esit_pkts == 0)
347 			esit_pkts = (mult + 1) * (max_burst + 1);
348 
349 		if (ep_type == INT_IN_EP || ep_type == INT_OUT_EP) {
350 			sch_ep->pkts = esit_pkts;
351 			sch_ep->num_budget_microframes = 1;
352 			bwb_table[0] = maxpkt * sch_ep->pkts;
353 		}
354 
355 		if (ep_type == ISOC_IN_EP || ep_type == ISOC_OUT_EP) {
356 
357 			if (sch_ep->esit == 1)
358 				sch_ep->pkts = esit_pkts;
359 			else if (esit_pkts <= sch_ep->esit)
360 				sch_ep->pkts = 1;
361 			else
362 				sch_ep->pkts = roundup_pow_of_two(esit_pkts)
363 					/ sch_ep->esit;
364 
365 			sch_ep->num_budget_microframes =
366 				DIV_ROUND_UP(esit_pkts, sch_ep->pkts);
367 
368 			sch_ep->repeat = !!(sch_ep->num_budget_microframes > 1);
369 			sch_ep->bw_cost_per_microframe = maxpkt * sch_ep->pkts;
370 
371 			for (i = 0; i < sch_ep->num_budget_microframes - 1; i++)
372 				bwb_table[i] = sch_ep->bw_cost_per_microframe;
373 
374 			/* last one <= bw_cost_per_microframe */
375 			bwb_table[i] = maxpkt * esit_pkts
376 				       - i * sch_ep->bw_cost_per_microframe;
377 		}
378 	} else if (is_fs_or_ls(sch_ep->speed)) {
379 		sch_ep->pkts = 1; /* at most one packet for each microframe */
380 
381 		/*
382 		 * num_budget_microframes and cs_count will be updated when
383 		 * check TT for INT_OUT_EP, ISOC/INT_IN_EP type
384 		 */
385 		sch_ep->cs_count = DIV_ROUND_UP(maxpkt, FS_PAYLOAD_MAX);
386 		sch_ep->num_budget_microframes = sch_ep->cs_count;
387 		sch_ep->bw_cost_per_microframe =
388 			(maxpkt < FS_PAYLOAD_MAX) ? maxpkt : FS_PAYLOAD_MAX;
389 
390 		/* init budget table */
391 		if (ep_type == ISOC_OUT_EP) {
392 			for (i = 0; i < sch_ep->num_budget_microframes; i++)
393 				bwb_table[i] =	sch_ep->bw_cost_per_microframe;
394 		} else if (ep_type == INT_OUT_EP) {
395 			/* only first one consumes bandwidth, others as zero */
396 			bwb_table[0] = sch_ep->bw_cost_per_microframe;
397 		} else { /* INT_IN_EP or ISOC_IN_EP */
398 			bwb_table[0] = 0; /* start split */
399 			bwb_table[1] = 0; /* idle */
400 			/*
401 			 * due to cs_count will be updated according to cs
402 			 * position, assign all remainder budget array
403 			 * elements as @bw_cost_per_microframe, but only first
404 			 * @num_budget_microframes elements will be used later
405 			 */
406 			for (i = 2; i < TT_MICROFRAMES_MAX; i++)
407 				bwb_table[i] =	sch_ep->bw_cost_per_microframe;
408 		}
409 	}
410 }
411 
412 /* Get maximum bandwidth when we schedule at offset slot. */
413 static u32 get_max_bw(struct mu3h_sch_bw_info *sch_bw,
414 	struct mu3h_sch_ep_info *sch_ep, u32 offset)
415 {
416 	u32 max_bw = 0;
417 	u32 bw;
418 	int i, j, k;
419 
420 	for (i = 0; i < sch_ep->num_esit; i++) {
421 		u32 base = offset + i * sch_ep->esit;
422 
423 		for (j = 0; j < sch_ep->num_budget_microframes; j++) {
424 			k = XHCI_MTK_BW_INDEX(base + j);
425 			bw = sch_bw->bus_bw[k] + sch_ep->bw_budget_table[j];
426 			if (bw > max_bw)
427 				max_bw = bw;
428 		}
429 	}
430 	return max_bw;
431 }
432 
433 static void update_bus_bw(struct mu3h_sch_bw_info *sch_bw,
434 	struct mu3h_sch_ep_info *sch_ep, bool used)
435 {
436 	u32 base;
437 	int i, j, k;
438 
439 	for (i = 0; i < sch_ep->num_esit; i++) {
440 		base = sch_ep->offset + i * sch_ep->esit;
441 		for (j = 0; j < sch_ep->num_budget_microframes; j++) {
442 			k = XHCI_MTK_BW_INDEX(base + j);
443 			if (used)
444 				sch_bw->bus_bw[k] += sch_ep->bw_budget_table[j];
445 			else
446 				sch_bw->bus_bw[k] -= sch_ep->bw_budget_table[j];
447 		}
448 	}
449 }
450 
451 static int check_fs_bus_bw(struct mu3h_sch_ep_info *sch_ep, int offset)
452 {
453 	struct mu3h_sch_tt *tt = sch_ep->sch_tt;
454 	u32 tmp;
455 	int base;
456 	int i, j, k;
457 
458 	for (i = 0; i < sch_ep->num_esit; i++) {
459 		base = offset + i * sch_ep->esit;
460 
461 		/*
462 		 * Compared with hs bus, no matter what ep type,
463 		 * the hub will always delay one uframe to send data
464 		 */
465 		for (j = 0; j < sch_ep->num_budget_microframes; j++) {
466 			k = XHCI_MTK_BW_INDEX(base + j);
467 			tmp = tt->fs_bus_bw[k] + sch_ep->bw_budget_table[j];
468 			if (tmp > FS_PAYLOAD_MAX)
469 				return -ESCH_BW_OVERFLOW;
470 		}
471 	}
472 
473 	return 0;
474 }
475 
476 static int check_sch_tt(struct mu3h_sch_ep_info *sch_ep, u32 offset)
477 {
478 	u32 extra_cs_count;
479 	u32 start_ss, last_ss;
480 	u32 start_cs, last_cs;
481 
482 	if (!sch_ep->sch_tt)
483 		return 0;
484 
485 	start_ss = offset % 8;
486 
487 	if (sch_ep->ep_type == ISOC_OUT_EP) {
488 		last_ss = start_ss + sch_ep->cs_count - 1;
489 
490 		/*
491 		 * usb_20 spec section11.18:
492 		 * must never schedule Start-Split in Y6
493 		 */
494 		if (!(start_ss == 7 || last_ss < 6))
495 			return -ESCH_SS_Y6;
496 
497 	} else {
498 		u32 cs_count = DIV_ROUND_UP(sch_ep->maxpkt, FS_PAYLOAD_MAX);
499 
500 		/*
501 		 * usb_20 spec section11.18:
502 		 * must never schedule Start-Split in Y6
503 		 */
504 		if (start_ss == 6)
505 			return -ESCH_SS_Y6;
506 
507 		/* one uframe for ss + one uframe for idle */
508 		start_cs = (start_ss + 2) % 8;
509 		last_cs = start_cs + cs_count - 1;
510 
511 		if (last_cs > 7)
512 			return -ESCH_CS_OVERFLOW;
513 
514 		if (sch_ep->ep_type == ISOC_IN_EP)
515 			extra_cs_count = (last_cs == 7) ? 1 : 2;
516 		else /*  ep_type : INTR IN / INTR OUT */
517 			extra_cs_count = 1;
518 
519 		cs_count += extra_cs_count;
520 		if (cs_count > 7)
521 			cs_count = 7; /* HW limit */
522 
523 		sch_ep->cs_count = cs_count;
524 		/* one for ss, the other for idle */
525 		sch_ep->num_budget_microframes = cs_count + 2;
526 
527 		/*
528 		 * if interval=1, maxp >752, num_budge_micoframe is larger
529 		 * than sch_ep->esit, will overstep boundary
530 		 */
531 		if (sch_ep->num_budget_microframes > sch_ep->esit)
532 			sch_ep->num_budget_microframes = sch_ep->esit;
533 	}
534 
535 	return check_fs_bus_bw(sch_ep, offset);
536 }
537 
538 static void update_sch_tt(struct mu3h_sch_ep_info *sch_ep, bool used)
539 {
540 	struct mu3h_sch_tt *tt = sch_ep->sch_tt;
541 	u32 base;
542 	int i, j, k;
543 
544 	for (i = 0; i < sch_ep->num_esit; i++) {
545 		base = sch_ep->offset + i * sch_ep->esit;
546 
547 		for (j = 0; j < sch_ep->num_budget_microframes; j++) {
548 			k = XHCI_MTK_BW_INDEX(base + j);
549 			if (used)
550 				tt->fs_bus_bw[k] += sch_ep->bw_budget_table[j];
551 			else
552 				tt->fs_bus_bw[k] -= sch_ep->bw_budget_table[j];
553 		}
554 	}
555 
556 	if (used)
557 		list_add_tail(&sch_ep->tt_endpoint, &tt->ep_list);
558 	else
559 		list_del(&sch_ep->tt_endpoint);
560 }
561 
562 static int load_ep_bw(struct mu3h_sch_bw_info *sch_bw,
563 		      struct mu3h_sch_ep_info *sch_ep, bool loaded)
564 {
565 	if (sch_ep->sch_tt)
566 		update_sch_tt(sch_ep, loaded);
567 
568 	/* update bus bandwidth info */
569 	update_bus_bw(sch_bw, sch_ep, loaded);
570 	sch_ep->allocated = loaded;
571 
572 	return 0;
573 }
574 
575 static int check_sch_bw(struct mu3h_sch_ep_info *sch_ep)
576 {
577 	struct mu3h_sch_bw_info *sch_bw = sch_ep->bw_info;
578 	const u32 bw_boundary = get_bw_boundary(sch_ep->speed);
579 	u32 offset;
580 	u32 worst_bw;
581 	u32 min_bw = ~0;
582 	int min_index = -1;
583 	int ret = 0;
584 
585 	/*
586 	 * Search through all possible schedule microframes.
587 	 * and find a microframe where its worst bandwidth is minimum.
588 	 */
589 	for (offset = 0; offset < sch_ep->esit; offset++) {
590 		ret = check_sch_tt(sch_ep, offset);
591 		if (ret)
592 			continue;
593 
594 		worst_bw = get_max_bw(sch_bw, sch_ep, offset);
595 		if (worst_bw > bw_boundary)
596 			continue;
597 
598 		if (min_bw > worst_bw) {
599 			min_bw = worst_bw;
600 			min_index = offset;
601 		}
602 
603 		/* use first-fit for LS/FS */
604 		if (sch_ep->sch_tt && min_index >= 0)
605 			break;
606 
607 		if (min_bw == 0)
608 			break;
609 	}
610 
611 	if (min_index < 0)
612 		return ret ? ret : -ESCH_BW_OVERFLOW;
613 
614 	sch_ep->offset = min_index;
615 
616 	return load_ep_bw(sch_bw, sch_ep, true);
617 }
618 
619 static void destroy_sch_ep(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
620 			   struct mu3h_sch_ep_info *sch_ep)
621 {
622 	/* only release ep bw check passed by check_sch_bw() */
623 	if (sch_ep->allocated)
624 		load_ep_bw(sch_ep->bw_info, sch_ep, false);
625 
626 	if (sch_ep->sch_tt)
627 		drop_tt(udev);
628 
629 	list_del(&sch_ep->endpoint);
630 	hlist_del(&sch_ep->hentry);
631 	kfree(sch_ep);
632 }
633 
634 static bool need_bw_sch(struct usb_device *udev,
635 			struct usb_host_endpoint *ep)
636 {
637 	bool has_tt = udev->tt && udev->tt->hub->parent;
638 
639 	/* only for periodic endpoints */
640 	if (usb_endpoint_xfer_control(&ep->desc)
641 		|| usb_endpoint_xfer_bulk(&ep->desc))
642 		return false;
643 
644 	/*
645 	 * for LS & FS periodic endpoints which its device is not behind
646 	 * a TT are also ignored, root-hub will schedule them directly,
647 	 * but need set @bpkts field of endpoint context to 1.
648 	 */
649 	if (is_fs_or_ls(udev->speed) && !has_tt)
650 		return false;
651 
652 	/* skip endpoint with zero maxpkt */
653 	if (usb_endpoint_maxp(&ep->desc) == 0)
654 		return false;
655 
656 	return true;
657 }
658 
659 int xhci_mtk_sch_init(struct xhci_hcd_mtk *mtk)
660 {
661 	struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
662 	struct mu3h_sch_bw_info *sch_array;
663 	int num_usb_bus;
664 
665 	/* ss IN and OUT are separated */
666 	num_usb_bus = xhci->usb3_rhub.num_ports * 2 + xhci->usb2_rhub.num_ports;
667 
668 	sch_array = kcalloc(num_usb_bus, sizeof(*sch_array), GFP_KERNEL);
669 	if (sch_array == NULL)
670 		return -ENOMEM;
671 
672 	mtk->sch_array = sch_array;
673 
674 	INIT_LIST_HEAD(&mtk->bw_ep_chk_list);
675 	hash_init(mtk->sch_ep_hash);
676 
677 	return 0;
678 }
679 
680 void xhci_mtk_sch_exit(struct xhci_hcd_mtk *mtk)
681 {
682 	kfree(mtk->sch_array);
683 }
684 
685 static int add_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
686 			struct usb_host_endpoint *ep)
687 {
688 	struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
689 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
690 	struct xhci_ep_ctx *ep_ctx;
691 	struct xhci_virt_device *virt_dev;
692 	struct mu3h_sch_ep_info *sch_ep;
693 	unsigned int ep_index;
694 
695 	virt_dev = xhci->devs[udev->slot_id];
696 	ep_index = xhci_get_endpoint_index(&ep->desc);
697 	ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
698 
699 	if (!need_bw_sch(udev, ep)) {
700 		/*
701 		 * set @bpkts to 1 if it is LS or FS periodic endpoint, and its
702 		 * device does not connected through an external HS hub
703 		 */
704 		if (usb_endpoint_xfer_int(&ep->desc)
705 			|| usb_endpoint_xfer_isoc(&ep->desc))
706 			ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(1));
707 
708 		return 0;
709 	}
710 
711 	xhci_dbg(xhci, "%s %s\n", __func__, decode_ep(ep, udev->speed));
712 
713 	sch_ep = create_sch_ep(mtk, udev, ep, ep_ctx);
714 	if (IS_ERR_OR_NULL(sch_ep))
715 		return -ENOMEM;
716 
717 	setup_sch_info(ep_ctx, sch_ep);
718 
719 	list_add_tail(&sch_ep->endpoint, &mtk->bw_ep_chk_list);
720 	hash_add(mtk->sch_ep_hash, &sch_ep->hentry, (unsigned long)ep);
721 
722 	return 0;
723 }
724 
725 static void drop_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
726 			  struct usb_host_endpoint *ep)
727 {
728 	struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
729 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
730 	struct mu3h_sch_ep_info *sch_ep;
731 	struct hlist_node *hn;
732 
733 	if (!need_bw_sch(udev, ep))
734 		return;
735 
736 	xhci_dbg(xhci, "%s %s\n", __func__, decode_ep(ep, udev->speed));
737 
738 	hash_for_each_possible_safe(mtk->sch_ep_hash, sch_ep,
739 				    hn, hentry, (unsigned long)ep) {
740 		if (sch_ep->ep == ep) {
741 			destroy_sch_ep(mtk, udev, sch_ep);
742 			break;
743 		}
744 	}
745 }
746 
747 int xhci_mtk_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
748 {
749 	struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
750 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
751 	struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
752 	struct mu3h_sch_ep_info *sch_ep;
753 	int ret;
754 
755 	xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
756 
757 	list_for_each_entry(sch_ep, &mtk->bw_ep_chk_list, endpoint) {
758 		struct xhci_ep_ctx *ep_ctx;
759 		struct usb_host_endpoint *ep = sch_ep->ep;
760 		unsigned int ep_index = xhci_get_endpoint_index(&ep->desc);
761 
762 		ret = check_sch_bw(sch_ep);
763 		if (ret) {
764 			xhci_err(xhci, "Not enough bandwidth! (%s)\n",
765 				 sch_error_string(-ret));
766 			return -ENOSPC;
767 		}
768 
769 		ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
770 		ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(sch_ep->pkts)
771 			| EP_BCSCOUNT(sch_ep->cs_count)
772 			| EP_BBM(sch_ep->burst_mode));
773 		ep_ctx->reserved[1] = cpu_to_le32(EP_BOFFSET(sch_ep->offset)
774 			| EP_BREPEAT(sch_ep->repeat));
775 
776 		xhci_dbg(xhci, " PKTS:%x, CSCOUNT:%x, BM:%x, OFFSET:%x, REPEAT:%x\n",
777 			sch_ep->pkts, sch_ep->cs_count, sch_ep->burst_mode,
778 			sch_ep->offset, sch_ep->repeat);
779 	}
780 
781 	ret = xhci_check_bandwidth(hcd, udev);
782 	if (!ret)
783 		list_del_init(&mtk->bw_ep_chk_list);
784 
785 	return ret;
786 }
787 
788 void xhci_mtk_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
789 {
790 	struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
791 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
792 	struct mu3h_sch_ep_info *sch_ep, *tmp;
793 
794 	xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
795 
796 	list_for_each_entry_safe(sch_ep, tmp, &mtk->bw_ep_chk_list, endpoint)
797 		destroy_sch_ep(mtk, udev, sch_ep);
798 
799 	xhci_reset_bandwidth(hcd, udev);
800 }
801 
802 int xhci_mtk_add_ep(struct usb_hcd *hcd, struct usb_device *udev,
803 		    struct usb_host_endpoint *ep)
804 {
805 	int ret;
806 
807 	ret = xhci_add_endpoint(hcd, udev, ep);
808 	if (ret)
809 		return ret;
810 
811 	if (ep->hcpriv)
812 		ret = add_ep_quirk(hcd, udev, ep);
813 
814 	return ret;
815 }
816 
817 int xhci_mtk_drop_ep(struct usb_hcd *hcd, struct usb_device *udev,
818 		     struct usb_host_endpoint *ep)
819 {
820 	int ret;
821 
822 	ret = xhci_drop_endpoint(hcd, udev, ep);
823 	if (ret)
824 		return ret;
825 
826 	if (ep->hcpriv)
827 		drop_ep_quirk(hcd, udev, ep);
828 
829 	return 0;
830 }
831