usb: xhci-mtk: Do not use xhci's virt_dev in drop_endpoint
[platform/kernel/linux-rpi.git] / drivers / usb / host / xhci-mtk-sch.c
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 (WARN_ONCE(!virt_dev, "xhci-mtk: usb %s has no virt_dev\n",
133                                 dev_name(&udev->dev)))
134                 return NULL;
135         if (WARN_ONCE(!virt_dev->real_port, "xhci-mtk: usb %s has invalid port number\n",
136                         dev_name(&udev->dev)))
137                 return NULL;
138
139         if (udev->speed >= USB_SPEED_SUPER) {
140                 if (usb_endpoint_dir_out(&ep->desc))
141                         bw_index = (virt_dev->real_port - 1) * 2;
142                 else
143                         bw_index = (virt_dev->real_port - 1) * 2 + 1;
144         } else {
145                 /* add one more for each SS port */
146                 bw_index = virt_dev->real_port + xhci->usb3_rhub.num_ports - 1;
147         }
148
149         return &mtk->sch_array[bw_index];
150 }
151
152 static u32 get_esit(struct xhci_ep_ctx *ep_ctx)
153 {
154         u32 esit;
155
156         esit = 1 << CTX_TO_EP_INTERVAL(le32_to_cpu(ep_ctx->ep_info));
157         if (esit > XHCI_MTK_MAX_ESIT)
158                 esit = XHCI_MTK_MAX_ESIT;
159
160         return esit;
161 }
162
163 static struct mu3h_sch_tt *find_tt(struct usb_device *udev)
164 {
165         struct usb_tt *utt = udev->tt;
166         struct mu3h_sch_tt *tt, **tt_index, **ptt;
167         bool allocated_index = false;
168
169         if (!utt)
170                 return NULL;    /* Not below a TT */
171
172         /*
173          * Find/create our data structure.
174          * For hubs with a single TT, we get it directly.
175          * For hubs with multiple TTs, there's an extra level of pointers.
176          */
177         tt_index = NULL;
178         if (utt->multi) {
179                 tt_index = utt->hcpriv;
180                 if (!tt_index) {        /* Create the index array */
181                         tt_index = kcalloc(utt->hub->maxchild,
182                                         sizeof(*tt_index), GFP_KERNEL);
183                         if (!tt_index)
184                                 return ERR_PTR(-ENOMEM);
185                         utt->hcpriv = tt_index;
186                         allocated_index = true;
187                 }
188                 ptt = &tt_index[udev->ttport - 1];
189         } else {
190                 ptt = (struct mu3h_sch_tt **) &utt->hcpriv;
191         }
192
193         tt = *ptt;
194         if (!tt) {      /* Create the mu3h_sch_tt */
195                 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
196                 if (!tt) {
197                         if (allocated_index) {
198                                 utt->hcpriv = NULL;
199                                 kfree(tt_index);
200                         }
201                         return ERR_PTR(-ENOMEM);
202                 }
203                 *ptt = tt;
204         }
205
206         return tt;
207 }
208
209 /* Release the TT above udev, if it's not in use */
210 static void drop_tt(struct usb_device *udev)
211 {
212         struct usb_tt *utt = udev->tt;
213         struct mu3h_sch_tt *tt, **tt_index, **ptt;
214         int i, cnt;
215
216         if (!utt || !utt->hcpriv)
217                 return;         /* Not below a TT, or never allocated */
218
219         cnt = 0;
220         if (utt->multi) {
221                 tt_index = utt->hcpriv;
222                 ptt = &tt_index[udev->ttport - 1];
223                 /*  How many entries are left in tt_index? */
224                 for (i = 0; i < utt->hub->maxchild; ++i)
225                         cnt += !!tt_index[i];
226         } else {
227                 tt_index = NULL;
228                 ptt = (struct mu3h_sch_tt **)&utt->hcpriv;
229         }
230
231         tt = *ptt;
232         if (!tt || tt->nr_eps > 0)
233                 return;         /* never allocated , or still in use*/
234
235         *ptt = NULL;
236         kfree(tt);
237
238         if (cnt == 1) {
239                 utt->hcpriv = NULL;
240                 kfree(tt_index);
241         }
242 }
243
244 static struct mu3h_sch_ep_info *create_sch_ep(struct xhci_hcd_mtk *mtk,
245                 struct usb_device *udev, struct usb_host_endpoint *ep,
246                 struct xhci_ep_ctx *ep_ctx)
247 {
248         struct mu3h_sch_ep_info *sch_ep;
249         struct mu3h_sch_tt *tt = NULL;
250         u32 len_bw_budget_table;
251         size_t mem_size;
252         struct mu3h_sch_bw_info *bw_info;
253
254         bw_info = get_bw_info(mtk, udev, ep);
255         if (!bw_info)
256                 return ERR_PTR(-ENODEV);
257
258         if (is_fs_or_ls(udev->speed))
259                 len_bw_budget_table = TT_MICROFRAMES_MAX;
260         else if ((udev->speed >= USB_SPEED_SUPER)
261                         && usb_endpoint_xfer_isoc(&ep->desc))
262                 len_bw_budget_table = get_esit(ep_ctx);
263         else
264                 len_bw_budget_table = 1;
265
266         mem_size = sizeof(struct mu3h_sch_ep_info) +
267                         len_bw_budget_table * sizeof(u32);
268         sch_ep = kzalloc(mem_size, GFP_KERNEL);
269         if (!sch_ep)
270                 return ERR_PTR(-ENOMEM);
271
272         if (is_fs_or_ls(udev->speed)) {
273                 tt = find_tt(udev);
274                 if (IS_ERR(tt)) {
275                         kfree(sch_ep);
276                         return ERR_PTR(-ENOMEM);
277                 }
278         }
279
280         sch_ep->bw_info = bw_info;
281         sch_ep->sch_tt = tt;
282         sch_ep->ep = ep;
283         sch_ep->speed = udev->speed;
284
285         return sch_ep;
286 }
287
288 static void setup_sch_info(struct xhci_ep_ctx *ep_ctx,
289                            struct mu3h_sch_ep_info *sch_ep)
290 {
291         u32 ep_type;
292         u32 maxpkt;
293         u32 max_burst;
294         u32 mult;
295         u32 esit_pkts;
296         u32 max_esit_payload;
297         u32 *bwb_table = sch_ep->bw_budget_table;
298         int i;
299
300         ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
301         maxpkt = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
302         max_burst = CTX_TO_MAX_BURST(le32_to_cpu(ep_ctx->ep_info2));
303         mult = CTX_TO_EP_MULT(le32_to_cpu(ep_ctx->ep_info));
304         max_esit_payload =
305                 (CTX_TO_MAX_ESIT_PAYLOAD_HI(
306                         le32_to_cpu(ep_ctx->ep_info)) << 16) |
307                  CTX_TO_MAX_ESIT_PAYLOAD(le32_to_cpu(ep_ctx->tx_info));
308
309         sch_ep->esit = get_esit(ep_ctx);
310         sch_ep->ep_type = ep_type;
311         sch_ep->maxpkt = maxpkt;
312         sch_ep->offset = 0;
313         sch_ep->burst_mode = 0;
314         sch_ep->repeat = 0;
315
316         if (sch_ep->speed == USB_SPEED_HIGH) {
317                 sch_ep->cs_count = 0;
318
319                 /*
320                  * usb_20 spec section5.9
321                  * a single microframe is enough for HS synchromous endpoints
322                  * in a interval
323                  */
324                 sch_ep->num_budget_microframes = 1;
325
326                 /*
327                  * xHCI spec section6.2.3.4
328                  * @max_burst is the number of additional transactions
329                  * opportunities per microframe
330                  */
331                 sch_ep->pkts = max_burst + 1;
332                 sch_ep->bw_cost_per_microframe = maxpkt * sch_ep->pkts;
333                 bwb_table[0] = sch_ep->bw_cost_per_microframe;
334         } else if (sch_ep->speed >= USB_SPEED_SUPER) {
335                 /* usb3_r1 spec section4.4.7 & 4.4.8 */
336                 sch_ep->cs_count = 0;
337                 sch_ep->burst_mode = 1;
338                 /*
339                  * some device's (d)wBytesPerInterval is set as 0,
340                  * then max_esit_payload is 0, so evaluate esit_pkts from
341                  * mult and burst
342                  */
343                 esit_pkts = DIV_ROUND_UP(max_esit_payload, maxpkt);
344                 if (esit_pkts == 0)
345                         esit_pkts = (mult + 1) * (max_burst + 1);
346
347                 if (ep_type == INT_IN_EP || ep_type == INT_OUT_EP) {
348                         sch_ep->pkts = esit_pkts;
349                         sch_ep->num_budget_microframes = 1;
350                         bwb_table[0] = maxpkt * sch_ep->pkts;
351                 }
352
353                 if (ep_type == ISOC_IN_EP || ep_type == ISOC_OUT_EP) {
354
355                         if (sch_ep->esit == 1)
356                                 sch_ep->pkts = esit_pkts;
357                         else if (esit_pkts <= sch_ep->esit)
358                                 sch_ep->pkts = 1;
359                         else
360                                 sch_ep->pkts = roundup_pow_of_two(esit_pkts)
361                                         / sch_ep->esit;
362
363                         sch_ep->num_budget_microframes =
364                                 DIV_ROUND_UP(esit_pkts, sch_ep->pkts);
365
366                         sch_ep->repeat = !!(sch_ep->num_budget_microframes > 1);
367                         sch_ep->bw_cost_per_microframe = maxpkt * sch_ep->pkts;
368
369                         for (i = 0; i < sch_ep->num_budget_microframes - 1; i++)
370                                 bwb_table[i] = sch_ep->bw_cost_per_microframe;
371
372                         /* last one <= bw_cost_per_microframe */
373                         bwb_table[i] = maxpkt * esit_pkts
374                                        - i * sch_ep->bw_cost_per_microframe;
375                 }
376         } else if (is_fs_or_ls(sch_ep->speed)) {
377                 sch_ep->pkts = 1; /* at most one packet for each microframe */
378
379                 /*
380                  * num_budget_microframes and cs_count will be updated when
381                  * check TT for INT_OUT_EP, ISOC/INT_IN_EP type
382                  */
383                 sch_ep->cs_count = DIV_ROUND_UP(maxpkt, FS_PAYLOAD_MAX);
384                 sch_ep->num_budget_microframes = sch_ep->cs_count;
385                 sch_ep->bw_cost_per_microframe =
386                         (maxpkt < FS_PAYLOAD_MAX) ? maxpkt : FS_PAYLOAD_MAX;
387
388                 /* init budget table */
389                 if (ep_type == ISOC_OUT_EP) {
390                         for (i = 0; i < sch_ep->num_budget_microframes; i++)
391                                 bwb_table[i] =  sch_ep->bw_cost_per_microframe;
392                 } else if (ep_type == INT_OUT_EP) {
393                         /* only first one consumes bandwidth, others as zero */
394                         bwb_table[0] = sch_ep->bw_cost_per_microframe;
395                 } else { /* INT_IN_EP or ISOC_IN_EP */
396                         bwb_table[0] = 0; /* start split */
397                         bwb_table[1] = 0; /* idle */
398                         /*
399                          * due to cs_count will be updated according to cs
400                          * position, assign all remainder budget array
401                          * elements as @bw_cost_per_microframe, but only first
402                          * @num_budget_microframes elements will be used later
403                          */
404                         for (i = 2; i < TT_MICROFRAMES_MAX; i++)
405                                 bwb_table[i] =  sch_ep->bw_cost_per_microframe;
406                 }
407         }
408 }
409
410 /* Get maximum bandwidth when we schedule at offset slot. */
411 static u32 get_max_bw(struct mu3h_sch_bw_info *sch_bw,
412         struct mu3h_sch_ep_info *sch_ep, u32 offset)
413 {
414         u32 num_esit;
415         u32 max_bw = 0;
416         u32 bw;
417         int i;
418         int j;
419
420         num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
421         for (i = 0; i < num_esit; i++) {
422                 u32 base = offset + i * sch_ep->esit;
423
424                 for (j = 0; j < sch_ep->num_budget_microframes; j++) {
425                         bw = sch_bw->bus_bw[base + j] +
426                                         sch_ep->bw_budget_table[j];
427                         if (bw > max_bw)
428                                 max_bw = bw;
429                 }
430         }
431         return max_bw;
432 }
433
434 static void update_bus_bw(struct mu3h_sch_bw_info *sch_bw,
435         struct mu3h_sch_ep_info *sch_ep, bool used)
436 {
437         u32 num_esit;
438         u32 base;
439         int i;
440         int j;
441
442         num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
443         for (i = 0; i < num_esit; i++) {
444                 base = sch_ep->offset + i * sch_ep->esit;
445                 for (j = 0; j < sch_ep->num_budget_microframes; j++) {
446                         if (used)
447                                 sch_bw->bus_bw[base + j] +=
448                                         sch_ep->bw_budget_table[j];
449                         else
450                                 sch_bw->bus_bw[base + j] -=
451                                         sch_ep->bw_budget_table[j];
452                 }
453         }
454 }
455
456 static int check_fs_bus_bw(struct mu3h_sch_ep_info *sch_ep, int offset)
457 {
458         struct mu3h_sch_tt *tt = sch_ep->sch_tt;
459         u32 num_esit, tmp;
460         int base;
461         int i, j;
462
463         num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
464         for (i = 0; i < num_esit; i++) {
465                 base = offset + i * sch_ep->esit;
466
467                 /*
468                  * Compared with hs bus, no matter what ep type,
469                  * the hub will always delay one uframe to send data
470                  */
471                 for (j = 0; j < sch_ep->cs_count; j++) {
472                         tmp = tt->fs_bus_bw[base + j] + sch_ep->bw_cost_per_microframe;
473                         if (tmp > FS_PAYLOAD_MAX)
474                                 return -ESCH_BW_OVERFLOW;
475                 }
476         }
477
478         return 0;
479 }
480
481 static int check_sch_tt(struct mu3h_sch_ep_info *sch_ep, u32 offset)
482 {
483         u32 extra_cs_count;
484         u32 start_ss, last_ss;
485         u32 start_cs, last_cs;
486
487         if (!sch_ep->sch_tt)
488                 return 0;
489
490         start_ss = offset % 8;
491
492         if (sch_ep->ep_type == ISOC_OUT_EP) {
493                 last_ss = start_ss + sch_ep->cs_count - 1;
494
495                 /*
496                  * usb_20 spec section11.18:
497                  * must never schedule Start-Split in Y6
498                  */
499                 if (!(start_ss == 7 || last_ss < 6))
500                         return -ESCH_SS_Y6;
501
502         } else {
503                 u32 cs_count = DIV_ROUND_UP(sch_ep->maxpkt, FS_PAYLOAD_MAX);
504
505                 /*
506                  * usb_20 spec section11.18:
507                  * must never schedule Start-Split in Y6
508                  */
509                 if (start_ss == 6)
510                         return -ESCH_SS_Y6;
511
512                 /* one uframe for ss + one uframe for idle */
513                 start_cs = (start_ss + 2) % 8;
514                 last_cs = start_cs + cs_count - 1;
515
516                 if (last_cs > 7)
517                         return -ESCH_CS_OVERFLOW;
518
519                 if (sch_ep->ep_type == ISOC_IN_EP)
520                         extra_cs_count = (last_cs == 7) ? 1 : 2;
521                 else /*  ep_type : INTR IN / INTR OUT */
522                         extra_cs_count = 1;
523
524                 cs_count += extra_cs_count;
525                 if (cs_count > 7)
526                         cs_count = 7; /* HW limit */
527
528                 sch_ep->cs_count = cs_count;
529                 /* one for ss, the other for idle */
530                 sch_ep->num_budget_microframes = cs_count + 2;
531
532                 /*
533                  * if interval=1, maxp >752, num_budge_micoframe is larger
534                  * than sch_ep->esit, will overstep boundary
535                  */
536                 if (sch_ep->num_budget_microframes > sch_ep->esit)
537                         sch_ep->num_budget_microframes = sch_ep->esit;
538         }
539
540         return check_fs_bus_bw(sch_ep, offset);
541 }
542
543 static void update_sch_tt(struct mu3h_sch_ep_info *sch_ep, bool used)
544 {
545         struct mu3h_sch_tt *tt = sch_ep->sch_tt;
546         u32 base, num_esit;
547         int bw_updated;
548         int i, j;
549
550         num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
551
552         if (used)
553                 bw_updated = sch_ep->bw_cost_per_microframe;
554         else
555                 bw_updated = -sch_ep->bw_cost_per_microframe;
556
557         for (i = 0; i < num_esit; i++) {
558                 base = sch_ep->offset + i * sch_ep->esit;
559
560                 for (j = 0; j < sch_ep->cs_count; j++)
561                         tt->fs_bus_bw[base + j] += bw_updated;
562         }
563
564         if (used)
565                 tt->nr_eps++;
566         else if (!WARN_ONCE(tt->nr_eps < 1, "unbalanced sch_tt's ep count"))
567                 tt->nr_eps--;
568 }
569
570 static int load_ep_bw(struct mu3h_sch_bw_info *sch_bw,
571                       struct mu3h_sch_ep_info *sch_ep, bool loaded)
572 {
573         if (sch_ep->sch_tt)
574                 update_sch_tt(sch_ep, loaded);
575
576         /* update bus bandwidth info */
577         update_bus_bw(sch_bw, sch_ep, loaded);
578         sch_ep->allocated = loaded;
579
580         return 0;
581 }
582
583 static u32 get_esit_boundary(struct mu3h_sch_ep_info *sch_ep)
584 {
585         u32 boundary = sch_ep->esit;
586
587         if (sch_ep->sch_tt) { /* LS/FS with TT */
588                 /* tune for CS */
589                 if (sch_ep->ep_type != ISOC_OUT_EP)
590                         boundary++;
591                 else if (boundary > 1) /* normally esit >= 8 for FS/LS */
592                         boundary--;
593         }
594
595         return boundary;
596 }
597
598 static int check_sch_bw(struct mu3h_sch_ep_info *sch_ep)
599 {
600         struct mu3h_sch_bw_info *sch_bw = sch_ep->bw_info;
601         const u32 esit_boundary = get_esit_boundary(sch_ep);
602         const u32 bw_boundary = get_bw_boundary(sch_ep->speed);
603         u32 offset;
604         u32 worst_bw;
605         u32 min_bw = ~0;
606         int min_index = -1;
607         int ret = 0;
608
609         /*
610          * Search through all possible schedule microframes.
611          * and find a microframe where its worst bandwidth is minimum.
612          */
613         for (offset = 0; offset < sch_ep->esit; offset++) {
614                 ret = check_sch_tt(sch_ep, offset);
615                 if (ret)
616                         continue;
617
618                 if ((offset + sch_ep->num_budget_microframes) > esit_boundary)
619                         break;
620
621                 worst_bw = get_max_bw(sch_bw, sch_ep, offset);
622                 if (worst_bw > bw_boundary)
623                         continue;
624
625                 if (min_bw > worst_bw) {
626                         min_bw = worst_bw;
627                         min_index = offset;
628                 }
629
630                 /* use first-fit for LS/FS */
631                 if (sch_ep->sch_tt && min_index >= 0)
632                         break;
633
634                 if (min_bw == 0)
635                         break;
636         }
637
638         if (min_index < 0)
639                 return ret ? ret : -ESCH_BW_OVERFLOW;
640
641         sch_ep->offset = min_index;
642
643         return load_ep_bw(sch_bw, sch_ep, true);
644 }
645
646 static const struct rhashtable_params sch_ep_table_param = {
647         .key_len        = sizeof(struct usb_host_endpoint *),
648         .key_offset     = offsetof(struct mu3h_sch_ep_info, ep),
649         .head_offset    = offsetof(struct mu3h_sch_ep_info, ep_link),
650 };
651
652 static void destroy_sch_ep(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
653                            struct mu3h_sch_ep_info *sch_ep)
654 {
655         /* only release ep bw check passed by check_sch_bw() */
656         if (sch_ep->allocated)
657                 load_ep_bw(sch_ep->bw_info, sch_ep, false);
658
659         if (sch_ep->sch_tt)
660                 drop_tt(udev);
661
662         list_del(&sch_ep->endpoint);
663         rhashtable_remove_fast(&mtk->sch_ep_table, &sch_ep->ep_link,
664                                 sch_ep_table_param);
665         kfree(sch_ep);
666 }
667
668 static bool need_bw_sch(struct usb_device *udev,
669                         struct usb_host_endpoint *ep)
670 {
671         bool has_tt = udev->tt && udev->tt->hub->parent;
672
673         /* only for periodic endpoints */
674         if (usb_endpoint_xfer_control(&ep->desc)
675                 || usb_endpoint_xfer_bulk(&ep->desc))
676                 return false;
677
678         /*
679          * for LS & FS periodic endpoints which its device is not behind
680          * a TT are also ignored, root-hub will schedule them directly,
681          * but need set @bpkts field of endpoint context to 1.
682          */
683         if (is_fs_or_ls(udev->speed) && !has_tt)
684                 return false;
685
686         /* skip endpoint with zero maxpkt */
687         if (usb_endpoint_maxp(&ep->desc) == 0)
688                 return false;
689
690         return true;
691 }
692
693 int xhci_mtk_sch_init(struct xhci_hcd_mtk *mtk)
694 {
695         struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
696         struct mu3h_sch_bw_info *sch_array;
697         int num_usb_bus;
698         int ret;
699
700         /* mu3h_sch_ep_info table, 'usb_host_endpoint*' as a key */
701         ret = rhashtable_init(&mtk->sch_ep_table, &sch_ep_table_param);
702         if (ret)
703                 return ret;
704
705         /* ss IN and OUT are separated */
706         num_usb_bus = xhci->usb3_rhub.num_ports * 2 + xhci->usb2_rhub.num_ports;
707
708         sch_array = kcalloc(num_usb_bus, sizeof(*sch_array), GFP_KERNEL);
709         if (sch_array == NULL)
710                 return -ENOMEM;
711
712         mtk->sch_array = sch_array;
713
714         INIT_LIST_HEAD(&mtk->bw_ep_chk_list);
715
716         return 0;
717 }
718
719 void xhci_mtk_sch_exit(struct xhci_hcd_mtk *mtk)
720 {
721         rhashtable_destroy(&mtk->sch_ep_table);
722         kfree(mtk->sch_array);
723 }
724
725 static int add_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 xhci_ep_ctx *ep_ctx;
731         struct xhci_virt_device *virt_dev;
732         struct mu3h_sch_ep_info *sch_ep;
733         unsigned int ep_index;
734
735         virt_dev = xhci->devs[udev->slot_id];
736         ep_index = xhci_get_endpoint_index(&ep->desc);
737         ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
738
739         if (!need_bw_sch(udev, ep)) {
740                 /*
741                  * set @bpkts to 1 if it is LS or FS periodic endpoint, and its
742                  * device does not connected through an external HS hub
743                  */
744                 if (usb_endpoint_xfer_int(&ep->desc)
745                         || usb_endpoint_xfer_isoc(&ep->desc))
746                         ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(1));
747
748                 return 0;
749         }
750
751         xhci_dbg(xhci, "%s %s\n", __func__, decode_ep(ep, udev->speed));
752
753         sch_ep = create_sch_ep(mtk, udev, ep, ep_ctx);
754         if (IS_ERR_OR_NULL(sch_ep))
755                 return -ENOMEM;
756
757         if (rhashtable_insert_fast(&mtk->sch_ep_table, &sch_ep->ep_link,
758                                    sch_ep_table_param))
759                 return -EEXIST;
760
761         setup_sch_info(ep_ctx, sch_ep);
762
763         list_add_tail(&sch_ep->endpoint, &mtk->bw_ep_chk_list);
764
765         xhci_dbg(xhci, "added sch_ep %p : %p\n", sch_ep, ep);
766
767         return 0;
768 }
769
770 static void drop_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
771                           struct usb_host_endpoint *ep)
772 {
773         struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
774         struct xhci_hcd *xhci = hcd_to_xhci(hcd);
775         struct mu3h_sch_ep_info *sch_ep;
776         void *key = ep;
777
778         if (!need_bw_sch(udev, ep))
779                 return;
780
781         xhci_dbg(xhci, "%s %s\n", __func__, decode_ep(ep, udev->speed));
782
783         sch_ep = rhashtable_lookup_fast(&mtk->sch_ep_table, &key,
784                                         sch_ep_table_param);
785         if (sch_ep)
786                 destroy_sch_ep(mtk, udev, sch_ep);
787         else
788                 xhci_dbg(xhci, "ep %p is not on the bw table\n", ep);
789 }
790
791 int xhci_mtk_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
792 {
793         struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
794         struct xhci_hcd *xhci = hcd_to_xhci(hcd);
795         struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
796         struct mu3h_sch_ep_info *sch_ep;
797         int ret;
798
799         xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
800
801         list_for_each_entry(sch_ep, &mtk->bw_ep_chk_list, endpoint) {
802                 struct xhci_ep_ctx *ep_ctx;
803                 struct usb_host_endpoint *ep = sch_ep->ep;
804                 unsigned int ep_index = xhci_get_endpoint_index(&ep->desc);
805
806                 ret = check_sch_bw(sch_ep);
807                 if (ret) {
808                         xhci_err(xhci, "Not enough bandwidth! (%s)\n",
809                                  sch_error_string(-ret));
810                         return -ENOSPC;
811                 }
812
813                 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
814                 ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(sch_ep->pkts)
815                         | EP_BCSCOUNT(sch_ep->cs_count)
816                         | EP_BBM(sch_ep->burst_mode));
817                 ep_ctx->reserved[1] = cpu_to_le32(EP_BOFFSET(sch_ep->offset)
818                         | EP_BREPEAT(sch_ep->repeat));
819
820                 xhci_dbg(xhci, " PKTS:%x, CSCOUNT:%x, BM:%x, OFFSET:%x, REPEAT:%x\n",
821                         sch_ep->pkts, sch_ep->cs_count, sch_ep->burst_mode,
822                         sch_ep->offset, sch_ep->repeat);
823         }
824
825         ret = xhci_check_bandwidth(hcd, udev);
826         if (!ret)
827                 INIT_LIST_HEAD(&mtk->bw_ep_chk_list);
828
829         return ret;
830 }
831
832 void xhci_mtk_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
833 {
834         struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
835         struct xhci_hcd *xhci = hcd_to_xhci(hcd);
836         struct mu3h_sch_ep_info *sch_ep, *tmp;
837
838         xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
839
840         list_for_each_entry_safe(sch_ep, tmp, &mtk->bw_ep_chk_list, endpoint)
841                 destroy_sch_ep(mtk, udev, sch_ep);
842
843         xhci_reset_bandwidth(hcd, udev);
844 }
845
846 int xhci_mtk_add_ep(struct usb_hcd *hcd, struct usb_device *udev,
847                     struct usb_host_endpoint *ep)
848 {
849         int ret;
850
851         ret = xhci_add_endpoint(hcd, udev, ep);
852         if (ret)
853                 return ret;
854
855         if (ep->hcpriv)
856                 ret = add_ep_quirk(hcd, udev, ep);
857
858         return ret;
859 }
860
861 int xhci_mtk_drop_ep(struct usb_hcd *hcd, struct usb_device *udev,
862                      struct usb_host_endpoint *ep)
863 {
864         int ret;
865
866         ret = xhci_drop_endpoint(hcd, udev, ep);
867         if (ret)
868                 return ret;
869
870         drop_ep_quirk(hcd, udev, ep);
871
872         return 0;
873 }