xhci-mtk-sch.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include "xhci.h"
  12. #include "xhci-mtk.h"
  13. #define SSP_BW_BOUNDARY 130000
  14. #define SS_BW_BOUNDARY 51000
  15. /* table 5-5. High-speed Isoc Transaction Limits in usb_20 spec */
  16. #define HS_BW_BOUNDARY 6144
  17. /* usb2 spec section11.18.1: at most 188 FS bytes per microframe */
  18. #define FS_PAYLOAD_MAX 188
  19. /*
  20. * max number of microframes for split transfer,
  21. * for fs isoc in : 1 ss + 1 idle + 7 cs
  22. */
  23. #define TT_MICROFRAMES_MAX 9
  24. /* mtk scheduler bitmasks */
  25. #define EP_BPKTS(p) ((p) & 0x7f)
  26. #define EP_BCSCOUNT(p) (((p) & 0x7) << 8)
  27. #define EP_BBM(p) ((p) << 11)
  28. #define EP_BOFFSET(p) ((p) & 0x3fff)
  29. #define EP_BREPEAT(p) (((p) & 0x7fff) << 16)
  30. static int is_fs_or_ls(enum usb_device_speed speed)
  31. {
  32. return speed == USB_SPEED_FULL || speed == USB_SPEED_LOW;
  33. }
  34. /*
  35. * get the index of bandwidth domains array which @ep belongs to.
  36. *
  37. * the bandwidth domain array is saved to @sch_array of struct xhci_hcd_mtk,
  38. * each HS root port is treated as a single bandwidth domain,
  39. * but each SS root port is treated as two bandwidth domains, one for IN eps,
  40. * one for OUT eps.
  41. * @real_port value is defined as follow according to xHCI spec:
  42. * 1 for SSport0, ..., N+1 for SSportN, N+2 for HSport0, N+3 for HSport1, etc
  43. * so the bandwidth domain array is organized as follow for simplification:
  44. * SSport0-OUT, SSport0-IN, ..., SSportX-OUT, SSportX-IN, HSport0, ..., HSportY
  45. */
  46. static int get_bw_index(struct xhci_hcd *xhci, struct usb_device *udev,
  47. struct usb_host_endpoint *ep)
  48. {
  49. struct xhci_virt_device *virt_dev;
  50. int bw_index;
  51. virt_dev = xhci->devs[udev->slot_id];
  52. if (udev->speed >= USB_SPEED_SUPER) {
  53. if (usb_endpoint_dir_out(&ep->desc))
  54. bw_index = (virt_dev->real_port - 1) * 2;
  55. else
  56. bw_index = (virt_dev->real_port - 1) * 2 + 1;
  57. } else {
  58. /* add one more for each SS port */
  59. bw_index = virt_dev->real_port + xhci->usb3_rhub.num_ports - 1;
  60. }
  61. return bw_index;
  62. }
  63. static u32 get_esit(struct xhci_ep_ctx *ep_ctx)
  64. {
  65. u32 esit;
  66. esit = 1 << CTX_TO_EP_INTERVAL(le32_to_cpu(ep_ctx->ep_info));
  67. if (esit > XHCI_MTK_MAX_ESIT)
  68. esit = XHCI_MTK_MAX_ESIT;
  69. return esit;
  70. }
  71. static struct mu3h_sch_tt *find_tt(struct usb_device *udev)
  72. {
  73. struct usb_tt *utt = udev->tt;
  74. struct mu3h_sch_tt *tt, **tt_index, **ptt;
  75. unsigned int port;
  76. bool allocated_index = false;
  77. if (!utt)
  78. return NULL; /* Not below a TT */
  79. /*
  80. * Find/create our data structure.
  81. * For hubs with a single TT, we get it directly.
  82. * For hubs with multiple TTs, there's an extra level of pointers.
  83. */
  84. tt_index = NULL;
  85. if (utt->multi) {
  86. tt_index = utt->hcpriv;
  87. if (!tt_index) { /* Create the index array */
  88. tt_index = kcalloc(utt->hub->maxchild,
  89. sizeof(*tt_index), GFP_KERNEL);
  90. if (!tt_index)
  91. return ERR_PTR(-ENOMEM);
  92. utt->hcpriv = tt_index;
  93. allocated_index = true;
  94. }
  95. port = udev->ttport - 1;
  96. ptt = &tt_index[port];
  97. } else {
  98. port = 0;
  99. ptt = (struct mu3h_sch_tt **) &utt->hcpriv;
  100. }
  101. tt = *ptt;
  102. if (!tt) { /* Create the mu3h_sch_tt */
  103. tt = kzalloc(sizeof(*tt), GFP_KERNEL);
  104. if (!tt) {
  105. if (allocated_index) {
  106. utt->hcpriv = NULL;
  107. kfree(tt_index);
  108. }
  109. return ERR_PTR(-ENOMEM);
  110. }
  111. INIT_LIST_HEAD(&tt->ep_list);
  112. tt->usb_tt = utt;
  113. tt->tt_port = port;
  114. *ptt = tt;
  115. }
  116. return tt;
  117. }
  118. /* Release the TT above udev, if it's not in use */
  119. static void drop_tt(struct usb_device *udev)
  120. {
  121. struct usb_tt *utt = udev->tt;
  122. struct mu3h_sch_tt *tt, **tt_index, **ptt;
  123. int i, cnt;
  124. if (!utt || !utt->hcpriv)
  125. return; /* Not below a TT, or never allocated */
  126. cnt = 0;
  127. if (utt->multi) {
  128. tt_index = utt->hcpriv;
  129. ptt = &tt_index[udev->ttport - 1];
  130. /* How many entries are left in tt_index? */
  131. for (i = 0; i < utt->hub->maxchild; ++i)
  132. cnt += !!tt_index[i];
  133. } else {
  134. tt_index = NULL;
  135. ptt = (struct mu3h_sch_tt **)&utt->hcpriv;
  136. }
  137. tt = *ptt;
  138. if (!tt || !list_empty(&tt->ep_list))
  139. return; /* never allocated , or still in use*/
  140. *ptt = NULL;
  141. kfree(tt);
  142. if (cnt == 1) {
  143. utt->hcpriv = NULL;
  144. kfree(tt_index);
  145. }
  146. }
  147. static struct mu3h_sch_ep_info *create_sch_ep(struct usb_device *udev,
  148. struct usb_host_endpoint *ep, struct xhci_ep_ctx *ep_ctx)
  149. {
  150. struct mu3h_sch_ep_info *sch_ep;
  151. struct mu3h_sch_tt *tt = NULL;
  152. u32 len_bw_budget_table;
  153. size_t mem_size;
  154. if (is_fs_or_ls(udev->speed))
  155. len_bw_budget_table = TT_MICROFRAMES_MAX;
  156. else if ((udev->speed >= USB_SPEED_SUPER)
  157. && usb_endpoint_xfer_isoc(&ep->desc))
  158. len_bw_budget_table = get_esit(ep_ctx);
  159. else
  160. len_bw_budget_table = 1;
  161. mem_size = sizeof(struct mu3h_sch_ep_info) +
  162. len_bw_budget_table * sizeof(u32);
  163. sch_ep = kzalloc(mem_size, GFP_KERNEL);
  164. if (!sch_ep)
  165. return ERR_PTR(-ENOMEM);
  166. if (is_fs_or_ls(udev->speed)) {
  167. tt = find_tt(udev);
  168. if (IS_ERR(tt)) {
  169. kfree(sch_ep);
  170. return ERR_PTR(-ENOMEM);
  171. }
  172. }
  173. sch_ep->sch_tt = tt;
  174. sch_ep->ep = ep;
  175. return sch_ep;
  176. }
  177. static void setup_sch_info(struct usb_device *udev,
  178. struct xhci_ep_ctx *ep_ctx, struct mu3h_sch_ep_info *sch_ep)
  179. {
  180. u32 ep_type;
  181. u32 maxpkt;
  182. u32 max_burst;
  183. u32 mult;
  184. u32 esit_pkts;
  185. u32 max_esit_payload;
  186. u32 *bwb_table = sch_ep->bw_budget_table;
  187. int i;
  188. ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
  189. maxpkt = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
  190. max_burst = CTX_TO_MAX_BURST(le32_to_cpu(ep_ctx->ep_info2));
  191. mult = CTX_TO_EP_MULT(le32_to_cpu(ep_ctx->ep_info));
  192. max_esit_payload =
  193. (CTX_TO_MAX_ESIT_PAYLOAD_HI(
  194. le32_to_cpu(ep_ctx->ep_info)) << 16) |
  195. CTX_TO_MAX_ESIT_PAYLOAD(le32_to_cpu(ep_ctx->tx_info));
  196. sch_ep->esit = get_esit(ep_ctx);
  197. sch_ep->ep_type = ep_type;
  198. sch_ep->maxpkt = maxpkt;
  199. sch_ep->offset = 0;
  200. sch_ep->burst_mode = 0;
  201. sch_ep->repeat = 0;
  202. if (udev->speed == USB_SPEED_HIGH) {
  203. sch_ep->cs_count = 0;
  204. /*
  205. * usb_20 spec section5.9
  206. * a single microframe is enough for HS synchromous endpoints
  207. * in a interval
  208. */
  209. sch_ep->num_budget_microframes = 1;
  210. /*
  211. * xHCI spec section6.2.3.4
  212. * @max_burst is the number of additional transactions
  213. * opportunities per microframe
  214. */
  215. sch_ep->pkts = max_burst + 1;
  216. sch_ep->bw_cost_per_microframe = maxpkt * sch_ep->pkts;
  217. bwb_table[0] = sch_ep->bw_cost_per_microframe;
  218. } else if (udev->speed >= USB_SPEED_SUPER) {
  219. /* usb3_r1 spec section4.4.7 & 4.4.8 */
  220. sch_ep->cs_count = 0;
  221. sch_ep->burst_mode = 1;
  222. /*
  223. * some device's (d)wBytesPerInterval is set as 0,
  224. * then max_esit_payload is 0, so evaluate esit_pkts from
  225. * mult and burst
  226. */
  227. esit_pkts = DIV_ROUND_UP(max_esit_payload, maxpkt);
  228. if (esit_pkts == 0)
  229. esit_pkts = (mult + 1) * (max_burst + 1);
  230. if (ep_type == INT_IN_EP || ep_type == INT_OUT_EP) {
  231. sch_ep->pkts = esit_pkts;
  232. sch_ep->num_budget_microframes = 1;
  233. bwb_table[0] = maxpkt * sch_ep->pkts;
  234. }
  235. if (ep_type == ISOC_IN_EP || ep_type == ISOC_OUT_EP) {
  236. u32 remainder;
  237. if (sch_ep->esit == 1)
  238. sch_ep->pkts = esit_pkts;
  239. else if (esit_pkts <= sch_ep->esit)
  240. sch_ep->pkts = 1;
  241. else
  242. sch_ep->pkts = roundup_pow_of_two(esit_pkts)
  243. / sch_ep->esit;
  244. sch_ep->num_budget_microframes =
  245. DIV_ROUND_UP(esit_pkts, sch_ep->pkts);
  246. sch_ep->repeat = !!(sch_ep->num_budget_microframes > 1);
  247. sch_ep->bw_cost_per_microframe = maxpkt * sch_ep->pkts;
  248. remainder = sch_ep->bw_cost_per_microframe;
  249. remainder *= sch_ep->num_budget_microframes;
  250. remainder -= (maxpkt * esit_pkts);
  251. for (i = 0; i < sch_ep->num_budget_microframes - 1; i++)
  252. bwb_table[i] = sch_ep->bw_cost_per_microframe;
  253. /* last one <= bw_cost_per_microframe */
  254. bwb_table[i] = remainder;
  255. }
  256. } else if (is_fs_or_ls(udev->speed)) {
  257. sch_ep->pkts = 1; /* at most one packet for each microframe */
  258. /*
  259. * num_budget_microframes and cs_count will be updated when
  260. * check TT for INT_OUT_EP, ISOC/INT_IN_EP type
  261. */
  262. sch_ep->cs_count = DIV_ROUND_UP(maxpkt, FS_PAYLOAD_MAX);
  263. sch_ep->num_budget_microframes = sch_ep->cs_count;
  264. sch_ep->bw_cost_per_microframe =
  265. (maxpkt < FS_PAYLOAD_MAX) ? maxpkt : FS_PAYLOAD_MAX;
  266. /* init budget table */
  267. if (ep_type == ISOC_OUT_EP) {
  268. for (i = 0; i < sch_ep->num_budget_microframes; i++)
  269. bwb_table[i] = sch_ep->bw_cost_per_microframe;
  270. } else if (ep_type == INT_OUT_EP) {
  271. /* only first one consumes bandwidth, others as zero */
  272. bwb_table[0] = sch_ep->bw_cost_per_microframe;
  273. } else { /* INT_IN_EP or ISOC_IN_EP */
  274. bwb_table[0] = 0; /* start split */
  275. bwb_table[1] = 0; /* idle */
  276. /*
  277. * due to cs_count will be updated according to cs
  278. * position, assign all remainder budget array
  279. * elements as @bw_cost_per_microframe, but only first
  280. * @num_budget_microframes elements will be used later
  281. */
  282. for (i = 2; i < TT_MICROFRAMES_MAX; i++)
  283. bwb_table[i] = sch_ep->bw_cost_per_microframe;
  284. }
  285. }
  286. }
  287. /* Get maximum bandwidth when we schedule at offset slot. */
  288. static u32 get_max_bw(struct mu3h_sch_bw_info *sch_bw,
  289. struct mu3h_sch_ep_info *sch_ep, u32 offset)
  290. {
  291. u32 num_esit;
  292. u32 max_bw = 0;
  293. u32 bw;
  294. int i;
  295. int j;
  296. num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
  297. for (i = 0; i < num_esit; i++) {
  298. u32 base = offset + i * sch_ep->esit;
  299. for (j = 0; j < sch_ep->num_budget_microframes; j++) {
  300. bw = sch_bw->bus_bw[base + j] +
  301. sch_ep->bw_budget_table[j];
  302. if (bw > max_bw)
  303. max_bw = bw;
  304. }
  305. }
  306. return max_bw;
  307. }
  308. static void update_bus_bw(struct mu3h_sch_bw_info *sch_bw,
  309. struct mu3h_sch_ep_info *sch_ep, bool used)
  310. {
  311. u32 num_esit;
  312. u32 base;
  313. int i;
  314. int j;
  315. num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
  316. for (i = 0; i < num_esit; i++) {
  317. base = sch_ep->offset + i * sch_ep->esit;
  318. for (j = 0; j < sch_ep->num_budget_microframes; j++) {
  319. if (used)
  320. sch_bw->bus_bw[base + j] +=
  321. sch_ep->bw_budget_table[j];
  322. else
  323. sch_bw->bus_bw[base + j] -=
  324. sch_ep->bw_budget_table[j];
  325. }
  326. }
  327. }
  328. static int check_sch_tt(struct usb_device *udev,
  329. struct mu3h_sch_ep_info *sch_ep, u32 offset)
  330. {
  331. struct mu3h_sch_tt *tt = sch_ep->sch_tt;
  332. u32 extra_cs_count;
  333. u32 fs_budget_start;
  334. u32 start_ss, last_ss;
  335. u32 start_cs, last_cs;
  336. int i;
  337. start_ss = offset % 8;
  338. fs_budget_start = (start_ss + 1) % 8;
  339. if (sch_ep->ep_type == ISOC_OUT_EP) {
  340. last_ss = start_ss + sch_ep->cs_count - 1;
  341. /*
  342. * usb_20 spec section11.18:
  343. * must never schedule Start-Split in Y6
  344. */
  345. if (!(start_ss == 7 || last_ss < 6))
  346. return -ERANGE;
  347. for (i = 0; i < sch_ep->cs_count; i++)
  348. if (test_bit(offset + i, tt->split_bit_map))
  349. return -ERANGE;
  350. } else {
  351. u32 cs_count = DIV_ROUND_UP(sch_ep->maxpkt, FS_PAYLOAD_MAX);
  352. /*
  353. * usb_20 spec section11.18:
  354. * must never schedule Start-Split in Y6
  355. */
  356. if (start_ss == 6)
  357. return -ERANGE;
  358. /* one uframe for ss + one uframe for idle */
  359. start_cs = (start_ss + 2) % 8;
  360. last_cs = start_cs + cs_count - 1;
  361. if (last_cs > 7)
  362. return -ERANGE;
  363. if (sch_ep->ep_type == ISOC_IN_EP)
  364. extra_cs_count = (last_cs == 7) ? 1 : 2;
  365. else /* ep_type : INTR IN / INTR OUT */
  366. extra_cs_count = (fs_budget_start == 6) ? 1 : 2;
  367. cs_count += extra_cs_count;
  368. if (cs_count > 7)
  369. cs_count = 7; /* HW limit */
  370. for (i = 0; i < cs_count + 2; i++) {
  371. if (test_bit(offset + i, tt->split_bit_map))
  372. return -ERANGE;
  373. }
  374. sch_ep->cs_count = cs_count;
  375. /* one for ss, the other for idle */
  376. sch_ep->num_budget_microframes = cs_count + 2;
  377. /*
  378. * if interval=1, maxp >752, num_budge_micoframe is larger
  379. * than sch_ep->esit, will overstep boundary
  380. */
  381. if (sch_ep->num_budget_microframes > sch_ep->esit)
  382. sch_ep->num_budget_microframes = sch_ep->esit;
  383. }
  384. return 0;
  385. }
  386. static void update_sch_tt(struct usb_device *udev,
  387. struct mu3h_sch_ep_info *sch_ep)
  388. {
  389. struct mu3h_sch_tt *tt = sch_ep->sch_tt;
  390. u32 base, num_esit;
  391. int i, j;
  392. num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
  393. for (i = 0; i < num_esit; i++) {
  394. base = sch_ep->offset + i * sch_ep->esit;
  395. for (j = 0; j < sch_ep->num_budget_microframes; j++)
  396. set_bit(base + j, tt->split_bit_map);
  397. }
  398. list_add_tail(&sch_ep->tt_endpoint, &tt->ep_list);
  399. }
  400. static int check_sch_bw(struct usb_device *udev,
  401. struct mu3h_sch_bw_info *sch_bw, struct mu3h_sch_ep_info *sch_ep)
  402. {
  403. u32 offset;
  404. u32 esit;
  405. u32 min_bw;
  406. u32 min_index;
  407. u32 worst_bw;
  408. u32 bw_boundary;
  409. u32 min_num_budget;
  410. u32 min_cs_count;
  411. bool tt_offset_ok = false;
  412. int ret;
  413. esit = sch_ep->esit;
  414. /*
  415. * Search through all possible schedule microframes.
  416. * and find a microframe where its worst bandwidth is minimum.
  417. */
  418. min_bw = ~0;
  419. min_index = 0;
  420. min_cs_count = sch_ep->cs_count;
  421. min_num_budget = sch_ep->num_budget_microframes;
  422. for (offset = 0; offset < esit; offset++) {
  423. if (is_fs_or_ls(udev->speed)) {
  424. ret = check_sch_tt(udev, sch_ep, offset);
  425. if (ret)
  426. continue;
  427. else
  428. tt_offset_ok = true;
  429. }
  430. if ((offset + sch_ep->num_budget_microframes) > sch_ep->esit)
  431. break;
  432. worst_bw = get_max_bw(sch_bw, sch_ep, offset);
  433. if (min_bw > worst_bw) {
  434. min_bw = worst_bw;
  435. min_index = offset;
  436. min_cs_count = sch_ep->cs_count;
  437. min_num_budget = sch_ep->num_budget_microframes;
  438. }
  439. if (min_bw == 0)
  440. break;
  441. }
  442. if (udev->speed == USB_SPEED_SUPER_PLUS)
  443. bw_boundary = SSP_BW_BOUNDARY;
  444. else if (udev->speed == USB_SPEED_SUPER)
  445. bw_boundary = SS_BW_BOUNDARY;
  446. else
  447. bw_boundary = HS_BW_BOUNDARY;
  448. /* check bandwidth */
  449. if (min_bw > bw_boundary)
  450. return -ERANGE;
  451. sch_ep->offset = min_index;
  452. sch_ep->cs_count = min_cs_count;
  453. sch_ep->num_budget_microframes = min_num_budget;
  454. if (is_fs_or_ls(udev->speed)) {
  455. /* all offset for tt is not ok*/
  456. if (!tt_offset_ok)
  457. return -ERANGE;
  458. update_sch_tt(udev, sch_ep);
  459. }
  460. /* update bus bandwidth info */
  461. update_bus_bw(sch_bw, sch_ep, 1);
  462. return 0;
  463. }
  464. static bool need_bw_sch(struct usb_host_endpoint *ep,
  465. enum usb_device_speed speed, int has_tt)
  466. {
  467. /* only for periodic endpoints */
  468. if (usb_endpoint_xfer_control(&ep->desc)
  469. || usb_endpoint_xfer_bulk(&ep->desc))
  470. return false;
  471. /*
  472. * for LS & FS periodic endpoints which its device is not behind
  473. * a TT are also ignored, root-hub will schedule them directly,
  474. * but need set @bpkts field of endpoint context to 1.
  475. */
  476. if (is_fs_or_ls(speed) && !has_tt)
  477. return false;
  478. return true;
  479. }
  480. int xhci_mtk_sch_init(struct xhci_hcd_mtk *mtk)
  481. {
  482. struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
  483. struct mu3h_sch_bw_info *sch_array;
  484. int num_usb_bus;
  485. int i;
  486. /* ss IN and OUT are separated */
  487. num_usb_bus = xhci->usb3_rhub.num_ports * 2 + xhci->usb2_rhub.num_ports;
  488. sch_array = kcalloc(num_usb_bus, sizeof(*sch_array), GFP_KERNEL);
  489. if (sch_array == NULL)
  490. return -ENOMEM;
  491. for (i = 0; i < num_usb_bus; i++)
  492. INIT_LIST_HEAD(&sch_array[i].bw_ep_list);
  493. mtk->sch_array = sch_array;
  494. return 0;
  495. }
  496. EXPORT_SYMBOL_GPL(xhci_mtk_sch_init);
  497. void xhci_mtk_sch_exit(struct xhci_hcd_mtk *mtk)
  498. {
  499. kfree(mtk->sch_array);
  500. }
  501. EXPORT_SYMBOL_GPL(xhci_mtk_sch_exit);
  502. int xhci_mtk_add_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
  503. struct usb_host_endpoint *ep)
  504. {
  505. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  506. struct xhci_hcd *xhci;
  507. struct xhci_ep_ctx *ep_ctx;
  508. struct xhci_slot_ctx *slot_ctx;
  509. struct xhci_virt_device *virt_dev;
  510. struct mu3h_sch_bw_info *sch_bw;
  511. struct mu3h_sch_ep_info *sch_ep;
  512. struct mu3h_sch_bw_info *sch_array;
  513. unsigned int ep_index;
  514. int bw_index;
  515. int ret = 0;
  516. xhci = hcd_to_xhci(hcd);
  517. virt_dev = xhci->devs[udev->slot_id];
  518. ep_index = xhci_get_endpoint_index(&ep->desc);
  519. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  520. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  521. sch_array = mtk->sch_array;
  522. xhci_dbg(xhci, "%s() type:%d, speed:%d, mpkt:%d, dir:%d, ep:%p\n",
  523. __func__, usb_endpoint_type(&ep->desc), udev->speed,
  524. usb_endpoint_maxp(&ep->desc),
  525. usb_endpoint_dir_in(&ep->desc), ep);
  526. if (!need_bw_sch(ep, udev->speed, slot_ctx->tt_info & TT_SLOT)) {
  527. /*
  528. * set @bpkts to 1 if it is LS or FS periodic endpoint, and its
  529. * device does not connected through an external HS hub
  530. */
  531. if (usb_endpoint_xfer_int(&ep->desc)
  532. || usb_endpoint_xfer_isoc(&ep->desc))
  533. ep_ctx->reserved[0] |= cpu_to_le32(EP_BPKTS(1));
  534. return 0;
  535. }
  536. bw_index = get_bw_index(xhci, udev, ep);
  537. sch_bw = &sch_array[bw_index];
  538. sch_ep = create_sch_ep(udev, ep, ep_ctx);
  539. if (IS_ERR_OR_NULL(sch_ep))
  540. return -ENOMEM;
  541. setup_sch_info(udev, ep_ctx, sch_ep);
  542. ret = check_sch_bw(udev, sch_bw, sch_ep);
  543. if (ret) {
  544. xhci_err(xhci, "Not enough bandwidth!\n");
  545. if (is_fs_or_ls(udev->speed))
  546. drop_tt(udev);
  547. kfree(sch_ep);
  548. return -ENOSPC;
  549. }
  550. list_add_tail(&sch_ep->endpoint, &sch_bw->bw_ep_list);
  551. ep_ctx->reserved[0] |= cpu_to_le32(EP_BPKTS(sch_ep->pkts)
  552. | EP_BCSCOUNT(sch_ep->cs_count) | EP_BBM(sch_ep->burst_mode));
  553. ep_ctx->reserved[1] |= cpu_to_le32(EP_BOFFSET(sch_ep->offset)
  554. | EP_BREPEAT(sch_ep->repeat));
  555. xhci_dbg(xhci, " PKTS:%x, CSCOUNT:%x, BM:%x, OFFSET:%x, REPEAT:%x\n",
  556. sch_ep->pkts, sch_ep->cs_count, sch_ep->burst_mode,
  557. sch_ep->offset, sch_ep->repeat);
  558. return 0;
  559. }
  560. EXPORT_SYMBOL_GPL(xhci_mtk_add_ep_quirk);
  561. void xhci_mtk_drop_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
  562. struct usb_host_endpoint *ep)
  563. {
  564. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  565. struct xhci_hcd *xhci;
  566. struct xhci_slot_ctx *slot_ctx;
  567. struct xhci_virt_device *virt_dev;
  568. struct mu3h_sch_bw_info *sch_array;
  569. struct mu3h_sch_bw_info *sch_bw;
  570. struct mu3h_sch_ep_info *sch_ep;
  571. int bw_index;
  572. xhci = hcd_to_xhci(hcd);
  573. virt_dev = xhci->devs[udev->slot_id];
  574. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  575. sch_array = mtk->sch_array;
  576. xhci_dbg(xhci, "%s() type:%d, speed:%d, mpks:%d, dir:%d, ep:%p\n",
  577. __func__, usb_endpoint_type(&ep->desc), udev->speed,
  578. usb_endpoint_maxp(&ep->desc),
  579. usb_endpoint_dir_in(&ep->desc), ep);
  580. if (!need_bw_sch(ep, udev->speed, slot_ctx->tt_info & TT_SLOT))
  581. return;
  582. bw_index = get_bw_index(xhci, udev, ep);
  583. sch_bw = &sch_array[bw_index];
  584. list_for_each_entry(sch_ep, &sch_bw->bw_ep_list, endpoint) {
  585. if (sch_ep->ep == ep) {
  586. update_bus_bw(sch_bw, sch_ep, 0);
  587. list_del(&sch_ep->endpoint);
  588. if (is_fs_or_ls(udev->speed)) {
  589. list_del(&sch_ep->tt_endpoint);
  590. drop_tt(udev);
  591. }
  592. kfree(sch_ep);
  593. break;
  594. }
  595. }
  596. }
  597. EXPORT_SYMBOL_GPL(xhci_mtk_drop_ep_quirk);