xhci-mem.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * xHCI host controller driver
  4. *
  5. * Copyright (C) 2008 Intel Corp.
  6. *
  7. * Author: Sarah Sharp
  8. * Some code borrowed from the Linux EHCI driver.
  9. */
  10. #include <linux/usb.h>
  11. #include <linux/pci.h>
  12. #include <linux/slab.h>
  13. #include <linux/dmapool.h>
  14. #include <linux/dma-mapping.h>
  15. #include "xhci.h"
  16. #include "xhci-trace.h"
  17. #include "xhci-debugfs.h"
  18. /*
  19. * Allocates a generic ring segment from the ring pool, sets the dma address,
  20. * initializes the segment to zero, and sets the private next pointer to NULL.
  21. *
  22. * Section 4.11.1.1:
  23. * "All components of all Command and Transfer TRBs shall be initialized to '0'"
  24. */
  25. static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci,
  26. unsigned int cycle_state,
  27. unsigned int max_packet,
  28. gfp_t flags)
  29. {
  30. struct xhci_segment *seg;
  31. dma_addr_t dma;
  32. int i;
  33. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  34. seg = kzalloc_node(sizeof(*seg), flags, dev_to_node(dev));
  35. if (!seg)
  36. return NULL;
  37. seg->trbs = dma_pool_zalloc(xhci->segment_pool, flags, &dma);
  38. if (!seg->trbs) {
  39. kfree(seg);
  40. return NULL;
  41. }
  42. if (max_packet) {
  43. seg->bounce_buf = kzalloc_node(max_packet, flags,
  44. dev_to_node(dev));
  45. if (!seg->bounce_buf) {
  46. dma_pool_free(xhci->segment_pool, seg->trbs, dma);
  47. kfree(seg);
  48. return NULL;
  49. }
  50. }
  51. /* If the cycle state is 0, set the cycle bit to 1 for all the TRBs */
  52. if (cycle_state == 0) {
  53. for (i = 0; i < TRBS_PER_SEGMENT; i++)
  54. seg->trbs[i].link.control |= cpu_to_le32(TRB_CYCLE);
  55. }
  56. seg->dma = dma;
  57. seg->next = NULL;
  58. return seg;
  59. }
  60. static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
  61. {
  62. if (seg->trbs) {
  63. dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma);
  64. seg->trbs = NULL;
  65. }
  66. kfree(seg->bounce_buf);
  67. kfree(seg);
  68. }
  69. static void xhci_free_segments_for_ring(struct xhci_hcd *xhci,
  70. struct xhci_segment *first)
  71. {
  72. struct xhci_segment *seg;
  73. seg = first->next;
  74. while (seg != first) {
  75. struct xhci_segment *next = seg->next;
  76. xhci_segment_free(xhci, seg);
  77. seg = next;
  78. }
  79. xhci_segment_free(xhci, first);
  80. }
  81. /*
  82. * Make the prev segment point to the next segment.
  83. *
  84. * Change the last TRB in the prev segment to be a Link TRB which points to the
  85. * DMA address of the next segment. The caller needs to set any Link TRB
  86. * related flags, such as End TRB, Toggle Cycle, and no snoop.
  87. */
  88. static void xhci_link_segments(struct xhci_hcd *xhci, struct xhci_segment *prev,
  89. struct xhci_segment *next, enum xhci_ring_type type)
  90. {
  91. u32 val;
  92. if (!prev || !next)
  93. return;
  94. prev->next = next;
  95. if (type != TYPE_EVENT) {
  96. prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr =
  97. cpu_to_le64(next->dma);
  98. /* Set the last TRB in the segment to have a TRB type ID of Link TRB */
  99. val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control);
  100. val &= ~TRB_TYPE_BITMASK;
  101. val |= TRB_TYPE(TRB_LINK);
  102. /* Always set the chain bit with 0.95 hardware */
  103. /* Set chain bit for isoc rings on AMD 0.96 host */
  104. if (xhci_link_trb_quirk(xhci) ||
  105. (type == TYPE_ISOC &&
  106. (xhci->quirks & XHCI_AMD_0x96_HOST)))
  107. val |= TRB_CHAIN;
  108. prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
  109. }
  110. }
  111. /*
  112. * Link the ring to the new segments.
  113. * Set Toggle Cycle for the new ring if needed.
  114. */
  115. static void xhci_link_rings(struct xhci_hcd *xhci, struct xhci_ring *ring,
  116. struct xhci_segment *first, struct xhci_segment *last,
  117. unsigned int num_segs)
  118. {
  119. struct xhci_segment *next;
  120. if (!ring || !first || !last)
  121. return;
  122. next = ring->enq_seg->next;
  123. xhci_link_segments(xhci, ring->enq_seg, first, ring->type);
  124. xhci_link_segments(xhci, last, next, ring->type);
  125. ring->num_segs += num_segs;
  126. ring->num_trbs_free += (TRBS_PER_SEGMENT - 1) * num_segs;
  127. if (ring->type != TYPE_EVENT && ring->enq_seg == ring->last_seg) {
  128. ring->last_seg->trbs[TRBS_PER_SEGMENT-1].link.control
  129. &= ~cpu_to_le32(LINK_TOGGLE);
  130. last->trbs[TRBS_PER_SEGMENT-1].link.control
  131. |= cpu_to_le32(LINK_TOGGLE);
  132. ring->last_seg = last;
  133. }
  134. }
  135. /*
  136. * We need a radix tree for mapping physical addresses of TRBs to which stream
  137. * ID they belong to. We need to do this because the host controller won't tell
  138. * us which stream ring the TRB came from. We could store the stream ID in an
  139. * event data TRB, but that doesn't help us for the cancellation case, since the
  140. * endpoint may stop before it reaches that event data TRB.
  141. *
  142. * The radix tree maps the upper portion of the TRB DMA address to a ring
  143. * segment that has the same upper portion of DMA addresses. For example, say I
  144. * have segments of size 1KB, that are always 1KB aligned. A segment may
  145. * start at 0x10c91000 and end at 0x10c913f0. If I use the upper 10 bits, the
  146. * key to the stream ID is 0x43244. I can use the DMA address of the TRB to
  147. * pass the radix tree a key to get the right stream ID:
  148. *
  149. * 0x10c90fff >> 10 = 0x43243
  150. * 0x10c912c0 >> 10 = 0x43244
  151. * 0x10c91400 >> 10 = 0x43245
  152. *
  153. * Obviously, only those TRBs with DMA addresses that are within the segment
  154. * will make the radix tree return the stream ID for that ring.
  155. *
  156. * Caveats for the radix tree:
  157. *
  158. * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an
  159. * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be
  160. * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the
  161. * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit
  162. * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit
  163. * extended systems (where the DMA address can be bigger than 32-bits),
  164. * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that.
  165. */
  166. static int xhci_insert_segment_mapping(struct radix_tree_root *trb_address_map,
  167. struct xhci_ring *ring,
  168. struct xhci_segment *seg,
  169. gfp_t mem_flags)
  170. {
  171. unsigned long key;
  172. int ret;
  173. key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
  174. /* Skip any segments that were already added. */
  175. if (radix_tree_lookup(trb_address_map, key))
  176. return 0;
  177. ret = radix_tree_maybe_preload(mem_flags);
  178. if (ret)
  179. return ret;
  180. ret = radix_tree_insert(trb_address_map,
  181. key, ring);
  182. radix_tree_preload_end();
  183. return ret;
  184. }
  185. static void xhci_remove_segment_mapping(struct radix_tree_root *trb_address_map,
  186. struct xhci_segment *seg)
  187. {
  188. unsigned long key;
  189. key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
  190. if (radix_tree_lookup(trb_address_map, key))
  191. radix_tree_delete(trb_address_map, key);
  192. }
  193. static int xhci_update_stream_segment_mapping(
  194. struct radix_tree_root *trb_address_map,
  195. struct xhci_ring *ring,
  196. struct xhci_segment *first_seg,
  197. struct xhci_segment *last_seg,
  198. gfp_t mem_flags)
  199. {
  200. struct xhci_segment *seg;
  201. struct xhci_segment *failed_seg;
  202. int ret;
  203. if (WARN_ON_ONCE(trb_address_map == NULL))
  204. return 0;
  205. seg = first_seg;
  206. do {
  207. ret = xhci_insert_segment_mapping(trb_address_map,
  208. ring, seg, mem_flags);
  209. if (ret)
  210. goto remove_streams;
  211. if (seg == last_seg)
  212. return 0;
  213. seg = seg->next;
  214. } while (seg != first_seg);
  215. return 0;
  216. remove_streams:
  217. failed_seg = seg;
  218. seg = first_seg;
  219. do {
  220. xhci_remove_segment_mapping(trb_address_map, seg);
  221. if (seg == failed_seg)
  222. return ret;
  223. seg = seg->next;
  224. } while (seg != first_seg);
  225. return ret;
  226. }
  227. static void xhci_remove_stream_mapping(struct xhci_ring *ring)
  228. {
  229. struct xhci_segment *seg;
  230. if (WARN_ON_ONCE(ring->trb_address_map == NULL))
  231. return;
  232. seg = ring->first_seg;
  233. do {
  234. xhci_remove_segment_mapping(ring->trb_address_map, seg);
  235. seg = seg->next;
  236. } while (seg != ring->first_seg);
  237. }
  238. static int xhci_update_stream_mapping(struct xhci_ring *ring, gfp_t mem_flags)
  239. {
  240. return xhci_update_stream_segment_mapping(ring->trb_address_map, ring,
  241. ring->first_seg, ring->last_seg, mem_flags);
  242. }
  243. /* XXX: Do we need the hcd structure in all these functions? */
  244. void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
  245. {
  246. if (!ring)
  247. return;
  248. trace_xhci_ring_free(ring);
  249. if (ring->first_seg) {
  250. if (ring->type == TYPE_STREAM)
  251. xhci_remove_stream_mapping(ring);
  252. xhci_free_segments_for_ring(xhci, ring->first_seg);
  253. }
  254. kfree(ring);
  255. }
  256. static void xhci_initialize_ring_info(struct xhci_ring *ring,
  257. unsigned int cycle_state)
  258. {
  259. /* The ring is empty, so the enqueue pointer == dequeue pointer */
  260. ring->enqueue = ring->first_seg->trbs;
  261. ring->enq_seg = ring->first_seg;
  262. ring->dequeue = ring->enqueue;
  263. ring->deq_seg = ring->first_seg;
  264. /* The ring is initialized to 0. The producer must write 1 to the cycle
  265. * bit to handover ownership of the TRB, so PCS = 1. The consumer must
  266. * compare CCS to the cycle bit to check ownership, so CCS = 1.
  267. *
  268. * New rings are initialized with cycle state equal to 1; if we are
  269. * handling ring expansion, set the cycle state equal to the old ring.
  270. */
  271. ring->cycle_state = cycle_state;
  272. /*
  273. * Each segment has a link TRB, and leave an extra TRB for SW
  274. * accounting purpose
  275. */
  276. ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
  277. }
  278. /* Allocate segments and link them for a ring */
  279. static int xhci_alloc_segments_for_ring(struct xhci_hcd *xhci,
  280. struct xhci_segment **first, struct xhci_segment **last,
  281. unsigned int num_segs, unsigned int cycle_state,
  282. enum xhci_ring_type type, unsigned int max_packet, gfp_t flags)
  283. {
  284. struct xhci_segment *prev;
  285. prev = xhci_segment_alloc(xhci, cycle_state, max_packet, flags);
  286. if (!prev)
  287. return -ENOMEM;
  288. num_segs--;
  289. *first = prev;
  290. while (num_segs > 0) {
  291. struct xhci_segment *next;
  292. next = xhci_segment_alloc(xhci, cycle_state, max_packet, flags);
  293. if (!next) {
  294. prev = *first;
  295. while (prev) {
  296. next = prev->next;
  297. xhci_segment_free(xhci, prev);
  298. prev = next;
  299. }
  300. return -ENOMEM;
  301. }
  302. xhci_link_segments(xhci, prev, next, type);
  303. prev = next;
  304. num_segs--;
  305. }
  306. xhci_link_segments(xhci, prev, *first, type);
  307. *last = prev;
  308. return 0;
  309. }
  310. /**
  311. * Create a new ring with zero or more segments.
  312. *
  313. * Link each segment together into a ring.
  314. * Set the end flag and the cycle toggle bit on the last segment.
  315. * See section 4.9.1 and figures 15 and 16.
  316. */
  317. struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
  318. unsigned int num_segs, unsigned int cycle_state,
  319. enum xhci_ring_type type, unsigned int max_packet, gfp_t flags)
  320. {
  321. struct xhci_ring *ring;
  322. int ret;
  323. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  324. ring = kzalloc_node(sizeof(*ring), flags, dev_to_node(dev));
  325. if (!ring)
  326. return NULL;
  327. ring->num_segs = num_segs;
  328. ring->bounce_buf_len = max_packet;
  329. INIT_LIST_HEAD(&ring->td_list);
  330. ring->type = type;
  331. if (num_segs == 0)
  332. return ring;
  333. ret = xhci_alloc_segments_for_ring(xhci, &ring->first_seg,
  334. &ring->last_seg, num_segs, cycle_state, type,
  335. max_packet, flags);
  336. if (ret)
  337. goto fail;
  338. /* Only event ring does not use link TRB */
  339. if (type != TYPE_EVENT) {
  340. /* See section 4.9.2.1 and 6.4.4.1 */
  341. ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control |=
  342. cpu_to_le32(LINK_TOGGLE);
  343. }
  344. xhci_initialize_ring_info(ring, cycle_state);
  345. trace_xhci_ring_alloc(ring);
  346. return ring;
  347. fail:
  348. kfree(ring);
  349. return NULL;
  350. }
  351. void xhci_free_endpoint_ring(struct xhci_hcd *xhci,
  352. struct xhci_virt_device *virt_dev,
  353. unsigned int ep_index)
  354. {
  355. xhci_ring_free(xhci, virt_dev->eps[ep_index].ring);
  356. virt_dev->eps[ep_index].ring = NULL;
  357. }
  358. /*
  359. * Expand an existing ring.
  360. * Allocate a new ring which has same segment numbers and link the two rings.
  361. */
  362. int xhci_ring_expansion(struct xhci_hcd *xhci, struct xhci_ring *ring,
  363. unsigned int num_trbs, gfp_t flags)
  364. {
  365. struct xhci_segment *first;
  366. struct xhci_segment *last;
  367. unsigned int num_segs;
  368. unsigned int num_segs_needed;
  369. int ret;
  370. num_segs_needed = (num_trbs + (TRBS_PER_SEGMENT - 1) - 1) /
  371. (TRBS_PER_SEGMENT - 1);
  372. /* Allocate number of segments we needed, or double the ring size */
  373. num_segs = ring->num_segs > num_segs_needed ?
  374. ring->num_segs : num_segs_needed;
  375. ret = xhci_alloc_segments_for_ring(xhci, &first, &last,
  376. num_segs, ring->cycle_state, ring->type,
  377. ring->bounce_buf_len, flags);
  378. if (ret)
  379. return -ENOMEM;
  380. if (ring->type == TYPE_STREAM)
  381. ret = xhci_update_stream_segment_mapping(ring->trb_address_map,
  382. ring, first, last, flags);
  383. if (ret) {
  384. struct xhci_segment *next;
  385. do {
  386. next = first->next;
  387. xhci_segment_free(xhci, first);
  388. if (first == last)
  389. break;
  390. first = next;
  391. } while (true);
  392. return ret;
  393. }
  394. xhci_link_rings(xhci, ring, first, last, num_segs);
  395. trace_xhci_ring_expansion(ring);
  396. xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion,
  397. "ring expansion succeed, now has %d segments",
  398. ring->num_segs);
  399. return 0;
  400. }
  401. struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci,
  402. int type, gfp_t flags)
  403. {
  404. struct xhci_container_ctx *ctx;
  405. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  406. if ((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT))
  407. return NULL;
  408. ctx = kzalloc_node(sizeof(*ctx), flags, dev_to_node(dev));
  409. if (!ctx)
  410. return NULL;
  411. ctx->type = type;
  412. ctx->size = HCC_64BYTE_CONTEXT(xhci->hcc_params) ? 2048 : 1024;
  413. if (type == XHCI_CTX_TYPE_INPUT)
  414. ctx->size += CTX_SIZE(xhci->hcc_params);
  415. ctx->bytes = dma_pool_zalloc(xhci->device_pool, flags, &ctx->dma);
  416. if (!ctx->bytes) {
  417. kfree(ctx);
  418. return NULL;
  419. }
  420. return ctx;
  421. }
  422. void xhci_free_container_ctx(struct xhci_hcd *xhci,
  423. struct xhci_container_ctx *ctx)
  424. {
  425. if (!ctx)
  426. return;
  427. dma_pool_free(xhci->device_pool, ctx->bytes, ctx->dma);
  428. kfree(ctx);
  429. }
  430. struct xhci_input_control_ctx *xhci_get_input_control_ctx(
  431. struct xhci_container_ctx *ctx)
  432. {
  433. if (ctx->type != XHCI_CTX_TYPE_INPUT)
  434. return NULL;
  435. return (struct xhci_input_control_ctx *)ctx->bytes;
  436. }
  437. struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_hcd *xhci,
  438. struct xhci_container_ctx *ctx)
  439. {
  440. if (ctx->type == XHCI_CTX_TYPE_DEVICE)
  441. return (struct xhci_slot_ctx *)ctx->bytes;
  442. return (struct xhci_slot_ctx *)
  443. (ctx->bytes + CTX_SIZE(xhci->hcc_params));
  444. }
  445. struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci,
  446. struct xhci_container_ctx *ctx,
  447. unsigned int ep_index)
  448. {
  449. /* increment ep index by offset of start of ep ctx array */
  450. ep_index++;
  451. if (ctx->type == XHCI_CTX_TYPE_INPUT)
  452. ep_index++;
  453. return (struct xhci_ep_ctx *)
  454. (ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params)));
  455. }
  456. /***************** Streams structures manipulation *************************/
  457. static void xhci_free_stream_ctx(struct xhci_hcd *xhci,
  458. unsigned int num_stream_ctxs,
  459. struct xhci_stream_ctx *stream_ctx, dma_addr_t dma)
  460. {
  461. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  462. size_t size = sizeof(struct xhci_stream_ctx) * num_stream_ctxs;
  463. if (size > MEDIUM_STREAM_ARRAY_SIZE)
  464. dma_free_coherent(dev, size,
  465. stream_ctx, dma);
  466. else if (size <= SMALL_STREAM_ARRAY_SIZE)
  467. return dma_pool_free(xhci->small_streams_pool,
  468. stream_ctx, dma);
  469. else
  470. return dma_pool_free(xhci->medium_streams_pool,
  471. stream_ctx, dma);
  472. }
  473. /*
  474. * The stream context array for each endpoint with bulk streams enabled can
  475. * vary in size, based on:
  476. * - how many streams the endpoint supports,
  477. * - the maximum primary stream array size the host controller supports,
  478. * - and how many streams the device driver asks for.
  479. *
  480. * The stream context array must be a power of 2, and can be as small as
  481. * 64 bytes or as large as 1MB.
  482. */
  483. static struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci,
  484. unsigned int num_stream_ctxs, dma_addr_t *dma,
  485. gfp_t mem_flags)
  486. {
  487. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  488. size_t size = sizeof(struct xhci_stream_ctx) * num_stream_ctxs;
  489. if (size > MEDIUM_STREAM_ARRAY_SIZE)
  490. return dma_alloc_coherent(dev, size,
  491. dma, mem_flags);
  492. else if (size <= SMALL_STREAM_ARRAY_SIZE)
  493. return dma_pool_alloc(xhci->small_streams_pool,
  494. mem_flags, dma);
  495. else
  496. return dma_pool_alloc(xhci->medium_streams_pool,
  497. mem_flags, dma);
  498. }
  499. struct xhci_ring *xhci_dma_to_transfer_ring(
  500. struct xhci_virt_ep *ep,
  501. u64 address)
  502. {
  503. if (ep->ep_state & EP_HAS_STREAMS)
  504. return radix_tree_lookup(&ep->stream_info->trb_address_map,
  505. address >> TRB_SEGMENT_SHIFT);
  506. return ep->ring;
  507. }
  508. struct xhci_ring *xhci_stream_id_to_ring(
  509. struct xhci_virt_device *dev,
  510. unsigned int ep_index,
  511. unsigned int stream_id)
  512. {
  513. struct xhci_virt_ep *ep = &dev->eps[ep_index];
  514. if (stream_id == 0)
  515. return ep->ring;
  516. if (!ep->stream_info)
  517. return NULL;
  518. if (stream_id >= ep->stream_info->num_streams)
  519. return NULL;
  520. return ep->stream_info->stream_rings[stream_id];
  521. }
  522. /*
  523. * Change an endpoint's internal structure so it supports stream IDs. The
  524. * number of requested streams includes stream 0, which cannot be used by device
  525. * drivers.
  526. *
  527. * The number of stream contexts in the stream context array may be bigger than
  528. * the number of streams the driver wants to use. This is because the number of
  529. * stream context array entries must be a power of two.
  530. */
  531. struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
  532. unsigned int num_stream_ctxs,
  533. unsigned int num_streams,
  534. unsigned int max_packet, gfp_t mem_flags)
  535. {
  536. struct xhci_stream_info *stream_info;
  537. u32 cur_stream;
  538. struct xhci_ring *cur_ring;
  539. u64 addr;
  540. int ret;
  541. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  542. xhci_dbg(xhci, "Allocating %u streams and %u "
  543. "stream context array entries.\n",
  544. num_streams, num_stream_ctxs);
  545. if (xhci->cmd_ring_reserved_trbs == MAX_RSVD_CMD_TRBS) {
  546. xhci_dbg(xhci, "Command ring has no reserved TRBs available\n");
  547. return NULL;
  548. }
  549. xhci->cmd_ring_reserved_trbs++;
  550. stream_info = kzalloc_node(sizeof(*stream_info), mem_flags,
  551. dev_to_node(dev));
  552. if (!stream_info)
  553. goto cleanup_trbs;
  554. stream_info->num_streams = num_streams;
  555. stream_info->num_stream_ctxs = num_stream_ctxs;
  556. /* Initialize the array of virtual pointers to stream rings. */
  557. stream_info->stream_rings = kcalloc_node(
  558. num_streams, sizeof(struct xhci_ring *), mem_flags,
  559. dev_to_node(dev));
  560. if (!stream_info->stream_rings)
  561. goto cleanup_info;
  562. /* Initialize the array of DMA addresses for stream rings for the HW. */
  563. stream_info->stream_ctx_array = xhci_alloc_stream_ctx(xhci,
  564. num_stream_ctxs, &stream_info->ctx_array_dma,
  565. mem_flags);
  566. if (!stream_info->stream_ctx_array)
  567. goto cleanup_ctx;
  568. memset(stream_info->stream_ctx_array, 0,
  569. sizeof(struct xhci_stream_ctx)*num_stream_ctxs);
  570. /* Allocate everything needed to free the stream rings later */
  571. stream_info->free_streams_command =
  572. xhci_alloc_command_with_ctx(xhci, true, mem_flags);
  573. if (!stream_info->free_streams_command)
  574. goto cleanup_ctx;
  575. INIT_RADIX_TREE(&stream_info->trb_address_map, GFP_ATOMIC);
  576. /* Allocate rings for all the streams that the driver will use,
  577. * and add their segment DMA addresses to the radix tree.
  578. * Stream 0 is reserved.
  579. */
  580. for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
  581. stream_info->stream_rings[cur_stream] =
  582. xhci_ring_alloc(xhci, 2, 1, TYPE_STREAM, max_packet,
  583. mem_flags);
  584. cur_ring = stream_info->stream_rings[cur_stream];
  585. if (!cur_ring)
  586. goto cleanup_rings;
  587. cur_ring->stream_id = cur_stream;
  588. cur_ring->trb_address_map = &stream_info->trb_address_map;
  589. /* Set deq ptr, cycle bit, and stream context type */
  590. addr = cur_ring->first_seg->dma |
  591. SCT_FOR_CTX(SCT_PRI_TR) |
  592. cur_ring->cycle_state;
  593. stream_info->stream_ctx_array[cur_stream].stream_ring =
  594. cpu_to_le64(addr);
  595. xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n",
  596. cur_stream, (unsigned long long) addr);
  597. ret = xhci_update_stream_mapping(cur_ring, mem_flags);
  598. if (ret) {
  599. xhci_ring_free(xhci, cur_ring);
  600. stream_info->stream_rings[cur_stream] = NULL;
  601. goto cleanup_rings;
  602. }
  603. }
  604. /* Leave the other unused stream ring pointers in the stream context
  605. * array initialized to zero. This will cause the xHC to give us an
  606. * error if the device asks for a stream ID we don't have setup (if it
  607. * was any other way, the host controller would assume the ring is
  608. * "empty" and wait forever for data to be queued to that stream ID).
  609. */
  610. return stream_info;
  611. cleanup_rings:
  612. for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
  613. cur_ring = stream_info->stream_rings[cur_stream];
  614. if (cur_ring) {
  615. xhci_ring_free(xhci, cur_ring);
  616. stream_info->stream_rings[cur_stream] = NULL;
  617. }
  618. }
  619. xhci_free_command(xhci, stream_info->free_streams_command);
  620. cleanup_ctx:
  621. kfree(stream_info->stream_rings);
  622. cleanup_info:
  623. kfree(stream_info);
  624. cleanup_trbs:
  625. xhci->cmd_ring_reserved_trbs--;
  626. return NULL;
  627. }
  628. /*
  629. * Sets the MaxPStreams field and the Linear Stream Array field.
  630. * Sets the dequeue pointer to the stream context array.
  631. */
  632. void xhci_setup_streams_ep_input_ctx(struct xhci_hcd *xhci,
  633. struct xhci_ep_ctx *ep_ctx,
  634. struct xhci_stream_info *stream_info)
  635. {
  636. u32 max_primary_streams;
  637. /* MaxPStreams is the number of stream context array entries, not the
  638. * number we're actually using. Must be in 2^(MaxPstreams + 1) format.
  639. * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc.
  640. */
  641. max_primary_streams = fls(stream_info->num_stream_ctxs) - 2;
  642. xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
  643. "Setting number of stream ctx array entries to %u",
  644. 1 << (max_primary_streams + 1));
  645. ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK);
  646. ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams)
  647. | EP_HAS_LSA);
  648. ep_ctx->deq = cpu_to_le64(stream_info->ctx_array_dma);
  649. }
  650. /*
  651. * Sets the MaxPStreams field and the Linear Stream Array field to 0.
  652. * Reinstalls the "normal" endpoint ring (at its previous dequeue mark,
  653. * not at the beginning of the ring).
  654. */
  655. void xhci_setup_no_streams_ep_input_ctx(struct xhci_ep_ctx *ep_ctx,
  656. struct xhci_virt_ep *ep)
  657. {
  658. dma_addr_t addr;
  659. ep_ctx->ep_info &= cpu_to_le32(~(EP_MAXPSTREAMS_MASK | EP_HAS_LSA));
  660. addr = xhci_trb_virt_to_dma(ep->ring->deq_seg, ep->ring->dequeue);
  661. ep_ctx->deq = cpu_to_le64(addr | ep->ring->cycle_state);
  662. }
  663. /* Frees all stream contexts associated with the endpoint,
  664. *
  665. * Caller should fix the endpoint context streams fields.
  666. */
  667. void xhci_free_stream_info(struct xhci_hcd *xhci,
  668. struct xhci_stream_info *stream_info)
  669. {
  670. int cur_stream;
  671. struct xhci_ring *cur_ring;
  672. if (!stream_info)
  673. return;
  674. for (cur_stream = 1; cur_stream < stream_info->num_streams;
  675. cur_stream++) {
  676. cur_ring = stream_info->stream_rings[cur_stream];
  677. if (cur_ring) {
  678. xhci_ring_free(xhci, cur_ring);
  679. stream_info->stream_rings[cur_stream] = NULL;
  680. }
  681. }
  682. xhci_free_command(xhci, stream_info->free_streams_command);
  683. xhci->cmd_ring_reserved_trbs--;
  684. if (stream_info->stream_ctx_array)
  685. xhci_free_stream_ctx(xhci,
  686. stream_info->num_stream_ctxs,
  687. stream_info->stream_ctx_array,
  688. stream_info->ctx_array_dma);
  689. kfree(stream_info->stream_rings);
  690. kfree(stream_info);
  691. }
  692. /***************** Device context manipulation *************************/
  693. static void xhci_init_endpoint_timer(struct xhci_hcd *xhci,
  694. struct xhci_virt_ep *ep)
  695. {
  696. timer_setup(&ep->stop_cmd_timer, xhci_stop_endpoint_command_watchdog,
  697. 0);
  698. ep->xhci = xhci;
  699. }
  700. static void xhci_free_tt_info(struct xhci_hcd *xhci,
  701. struct xhci_virt_device *virt_dev,
  702. int slot_id)
  703. {
  704. struct list_head *tt_list_head;
  705. struct xhci_tt_bw_info *tt_info, *next;
  706. bool slot_found = false;
  707. /* If the device never made it past the Set Address stage,
  708. * it may not have the real_port set correctly.
  709. */
  710. if (virt_dev->real_port == 0 ||
  711. virt_dev->real_port > HCS_MAX_PORTS(xhci->hcs_params1)) {
  712. xhci_dbg(xhci, "Bad real port.\n");
  713. return;
  714. }
  715. tt_list_head = &(xhci->rh_bw[virt_dev->real_port - 1].tts);
  716. list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
  717. /* Multi-TT hubs will have more than one entry */
  718. if (tt_info->slot_id == slot_id) {
  719. slot_found = true;
  720. list_del(&tt_info->tt_list);
  721. kfree(tt_info);
  722. } else if (slot_found) {
  723. break;
  724. }
  725. }
  726. }
  727. int xhci_alloc_tt_info(struct xhci_hcd *xhci,
  728. struct xhci_virt_device *virt_dev,
  729. struct usb_device *hdev,
  730. struct usb_tt *tt, gfp_t mem_flags)
  731. {
  732. struct xhci_tt_bw_info *tt_info;
  733. unsigned int num_ports;
  734. int i, j;
  735. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  736. if (!tt->multi)
  737. num_ports = 1;
  738. else
  739. num_ports = hdev->maxchild;
  740. for (i = 0; i < num_ports; i++, tt_info++) {
  741. struct xhci_interval_bw_table *bw_table;
  742. tt_info = kzalloc_node(sizeof(*tt_info), mem_flags,
  743. dev_to_node(dev));
  744. if (!tt_info)
  745. goto free_tts;
  746. INIT_LIST_HEAD(&tt_info->tt_list);
  747. list_add(&tt_info->tt_list,
  748. &xhci->rh_bw[virt_dev->real_port - 1].tts);
  749. tt_info->slot_id = virt_dev->udev->slot_id;
  750. if (tt->multi)
  751. tt_info->ttport = i+1;
  752. bw_table = &tt_info->bw_table;
  753. for (j = 0; j < XHCI_MAX_INTERVAL; j++)
  754. INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints);
  755. }
  756. return 0;
  757. free_tts:
  758. xhci_free_tt_info(xhci, virt_dev, virt_dev->udev->slot_id);
  759. return -ENOMEM;
  760. }
  761. /* All the xhci_tds in the ring's TD list should be freed at this point.
  762. * Should be called with xhci->lock held if there is any chance the TT lists
  763. * will be manipulated by the configure endpoint, allocate device, or update
  764. * hub functions while this function is removing the TT entries from the list.
  765. */
  766. void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
  767. {
  768. struct xhci_virt_device *dev;
  769. int i;
  770. int old_active_eps = 0;
  771. /* Slot ID 0 is reserved */
  772. if (slot_id == 0 || !xhci->devs[slot_id])
  773. return;
  774. dev = xhci->devs[slot_id];
  775. xhci->dcbaa->dev_context_ptrs[slot_id] = 0;
  776. if (!dev)
  777. return;
  778. trace_xhci_free_virt_device(dev);
  779. if (dev->tt_info)
  780. old_active_eps = dev->tt_info->active_eps;
  781. for (i = 0; i < 31; i++) {
  782. if (dev->eps[i].ring)
  783. xhci_ring_free(xhci, dev->eps[i].ring);
  784. if (dev->eps[i].stream_info)
  785. xhci_free_stream_info(xhci,
  786. dev->eps[i].stream_info);
  787. /* Endpoints on the TT/root port lists should have been removed
  788. * when usb_disable_device() was called for the device.
  789. * We can't drop them anyway, because the udev might have gone
  790. * away by this point, and we can't tell what speed it was.
  791. */
  792. if (!list_empty(&dev->eps[i].bw_endpoint_list))
  793. xhci_warn(xhci, "Slot %u endpoint %u "
  794. "not removed from BW list!\n",
  795. slot_id, i);
  796. }
  797. /* If this is a hub, free the TT(s) from the TT list */
  798. xhci_free_tt_info(xhci, dev, slot_id);
  799. /* If necessary, update the number of active TTs on this root port */
  800. xhci_update_tt_active_eps(xhci, dev, old_active_eps);
  801. if (dev->in_ctx)
  802. xhci_free_container_ctx(xhci, dev->in_ctx);
  803. if (dev->out_ctx)
  804. xhci_free_container_ctx(xhci, dev->out_ctx);
  805. if (dev->udev && dev->udev->slot_id)
  806. dev->udev->slot_id = 0;
  807. kfree(xhci->devs[slot_id]);
  808. xhci->devs[slot_id] = NULL;
  809. }
  810. /*
  811. * Free a virt_device structure.
  812. * If the virt_device added a tt_info (a hub) and has children pointing to
  813. * that tt_info, then free the child first. Recursive.
  814. * We can't rely on udev at this point to find child-parent relationships.
  815. */
  816. void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
  817. {
  818. struct xhci_virt_device *vdev;
  819. struct list_head *tt_list_head;
  820. struct xhci_tt_bw_info *tt_info, *next;
  821. int i;
  822. vdev = xhci->devs[slot_id];
  823. if (!vdev)
  824. return;
  825. if (vdev->real_port == 0 ||
  826. vdev->real_port > HCS_MAX_PORTS(xhci->hcs_params1)) {
  827. xhci_dbg(xhci, "Bad vdev->real_port.\n");
  828. goto out;
  829. }
  830. tt_list_head = &(xhci->rh_bw[vdev->real_port - 1].tts);
  831. list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
  832. /* is this a hub device that added a tt_info to the tts list */
  833. if (tt_info->slot_id == slot_id) {
  834. /* are any devices using this tt_info? */
  835. for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
  836. vdev = xhci->devs[i];
  837. if (vdev && (vdev->tt_info == tt_info))
  838. xhci_free_virt_devices_depth_first(
  839. xhci, i);
  840. }
  841. }
  842. }
  843. out:
  844. /* we are now at a leaf device */
  845. xhci_debugfs_remove_slot(xhci, slot_id);
  846. xhci_free_virt_device(xhci, slot_id);
  847. }
  848. int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
  849. struct usb_device *udev, gfp_t flags)
  850. {
  851. struct xhci_virt_device *dev;
  852. int i;
  853. /* Slot ID 0 is reserved */
  854. if (slot_id == 0 || xhci->devs[slot_id]) {
  855. xhci_warn(xhci, "Bad Slot ID %d\n", slot_id);
  856. return 0;
  857. }
  858. dev = kzalloc(sizeof(*dev), flags);
  859. if (!dev)
  860. return 0;
  861. /* Allocate the (output) device context that will be used in the HC. */
  862. dev->out_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_DEVICE, flags);
  863. if (!dev->out_ctx)
  864. goto fail;
  865. xhci_dbg(xhci, "Slot %d output ctx = 0x%llx (dma)\n", slot_id,
  866. (unsigned long long)dev->out_ctx->dma);
  867. /* Allocate the (input) device context for address device command */
  868. dev->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, flags);
  869. if (!dev->in_ctx)
  870. goto fail;
  871. xhci_dbg(xhci, "Slot %d input ctx = 0x%llx (dma)\n", slot_id,
  872. (unsigned long long)dev->in_ctx->dma);
  873. /* Initialize the cancellation list and watchdog timers for each ep */
  874. for (i = 0; i < 31; i++) {
  875. xhci_init_endpoint_timer(xhci, &dev->eps[i]);
  876. INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list);
  877. INIT_LIST_HEAD(&dev->eps[i].bw_endpoint_list);
  878. }
  879. /* Allocate endpoint 0 ring */
  880. dev->eps[0].ring = xhci_ring_alloc(xhci, 2, 1, TYPE_CTRL, 0, flags);
  881. if (!dev->eps[0].ring)
  882. goto fail;
  883. dev->udev = udev;
  884. /* Point to output device context in dcbaa. */
  885. xhci->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(dev->out_ctx->dma);
  886. xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n",
  887. slot_id,
  888. &xhci->dcbaa->dev_context_ptrs[slot_id],
  889. le64_to_cpu(xhci->dcbaa->dev_context_ptrs[slot_id]));
  890. trace_xhci_alloc_virt_device(dev);
  891. xhci->devs[slot_id] = dev;
  892. return 1;
  893. fail:
  894. if (dev->in_ctx)
  895. xhci_free_container_ctx(xhci, dev->in_ctx);
  896. if (dev->out_ctx)
  897. xhci_free_container_ctx(xhci, dev->out_ctx);
  898. kfree(dev);
  899. return 0;
  900. }
  901. void xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd *xhci,
  902. struct usb_device *udev)
  903. {
  904. struct xhci_virt_device *virt_dev;
  905. struct xhci_ep_ctx *ep0_ctx;
  906. struct xhci_ring *ep_ring;
  907. virt_dev = xhci->devs[udev->slot_id];
  908. ep0_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, 0);
  909. ep_ring = virt_dev->eps[0].ring;
  910. /*
  911. * FIXME we don't keep track of the dequeue pointer very well after a
  912. * Set TR dequeue pointer, so we're setting the dequeue pointer of the
  913. * host to our enqueue pointer. This should only be called after a
  914. * configured device has reset, so all control transfers should have
  915. * been completed or cancelled before the reset.
  916. */
  917. ep0_ctx->deq = cpu_to_le64(xhci_trb_virt_to_dma(ep_ring->enq_seg,
  918. ep_ring->enqueue)
  919. | ep_ring->cycle_state);
  920. }
  921. /*
  922. * The xHCI roothub may have ports of differing speeds in any order in the port
  923. * status registers.
  924. *
  925. * The xHCI hardware wants to know the roothub port number that the USB device
  926. * is attached to (or the roothub port its ancestor hub is attached to). All we
  927. * know is the index of that port under either the USB 2.0 or the USB 3.0
  928. * roothub, but that doesn't give us the real index into the HW port status
  929. * registers. Call xhci_find_raw_port_number() to get real index.
  930. */
  931. static u32 xhci_find_real_port_number(struct xhci_hcd *xhci,
  932. struct usb_device *udev)
  933. {
  934. struct usb_device *top_dev;
  935. struct usb_hcd *hcd;
  936. if (udev->speed >= USB_SPEED_SUPER)
  937. hcd = xhci->shared_hcd;
  938. else
  939. hcd = xhci->main_hcd;
  940. for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
  941. top_dev = top_dev->parent)
  942. /* Found device below root hub */;
  943. return xhci_find_raw_port_number(hcd, top_dev->portnum);
  944. }
  945. /* Setup an xHCI virtual device for a Set Address command */
  946. int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev)
  947. {
  948. struct xhci_virt_device *dev;
  949. struct xhci_ep_ctx *ep0_ctx;
  950. struct xhci_slot_ctx *slot_ctx;
  951. u32 port_num;
  952. u32 max_packets;
  953. struct usb_device *top_dev;
  954. dev = xhci->devs[udev->slot_id];
  955. /* Slot ID 0 is reserved */
  956. if (udev->slot_id == 0 || !dev) {
  957. xhci_warn(xhci, "Slot ID %d is not assigned to this device\n",
  958. udev->slot_id);
  959. return -EINVAL;
  960. }
  961. ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0);
  962. slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx);
  963. /* 3) Only the control endpoint is valid - one endpoint context */
  964. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1) | udev->route);
  965. switch (udev->speed) {
  966. case USB_SPEED_SUPER_PLUS:
  967. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SSP);
  968. max_packets = MAX_PACKET(512);
  969. break;
  970. case USB_SPEED_SUPER:
  971. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
  972. max_packets = MAX_PACKET(512);
  973. break;
  974. case USB_SPEED_HIGH:
  975. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
  976. max_packets = MAX_PACKET(64);
  977. break;
  978. /* USB core guesses at a 64-byte max packet first for FS devices */
  979. case USB_SPEED_FULL:
  980. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
  981. max_packets = MAX_PACKET(64);
  982. break;
  983. case USB_SPEED_LOW:
  984. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS);
  985. max_packets = MAX_PACKET(8);
  986. break;
  987. case USB_SPEED_WIRELESS:
  988. xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
  989. return -EINVAL;
  990. break;
  991. default:
  992. /* Speed was set earlier, this shouldn't happen. */
  993. return -EINVAL;
  994. }
  995. /* Find the root hub port this device is under */
  996. port_num = xhci_find_real_port_number(xhci, udev);
  997. if (!port_num)
  998. return -EINVAL;
  999. slot_ctx->dev_info2 |= cpu_to_le32(ROOT_HUB_PORT(port_num));
  1000. /* Set the port number in the virtual_device to the faked port number */
  1001. for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
  1002. top_dev = top_dev->parent)
  1003. /* Found device below root hub */;
  1004. dev->fake_port = top_dev->portnum;
  1005. dev->real_port = port_num;
  1006. xhci_dbg(xhci, "Set root hub portnum to %d\n", port_num);
  1007. xhci_dbg(xhci, "Set fake root hub portnum to %d\n", dev->fake_port);
  1008. /* Find the right bandwidth table that this device will be a part of.
  1009. * If this is a full speed device attached directly to a root port (or a
  1010. * decendent of one), it counts as a primary bandwidth domain, not a
  1011. * secondary bandwidth domain under a TT. An xhci_tt_info structure
  1012. * will never be created for the HS root hub.
  1013. */
  1014. if (!udev->tt || !udev->tt->hub->parent) {
  1015. dev->bw_table = &xhci->rh_bw[port_num - 1].bw_table;
  1016. } else {
  1017. struct xhci_root_port_bw_info *rh_bw;
  1018. struct xhci_tt_bw_info *tt_bw;
  1019. rh_bw = &xhci->rh_bw[port_num - 1];
  1020. /* Find the right TT. */
  1021. list_for_each_entry(tt_bw, &rh_bw->tts, tt_list) {
  1022. if (tt_bw->slot_id != udev->tt->hub->slot_id)
  1023. continue;
  1024. if (!dev->udev->tt->multi ||
  1025. (udev->tt->multi &&
  1026. tt_bw->ttport == dev->udev->ttport)) {
  1027. dev->bw_table = &tt_bw->bw_table;
  1028. dev->tt_info = tt_bw;
  1029. break;
  1030. }
  1031. }
  1032. if (!dev->tt_info)
  1033. xhci_warn(xhci, "WARN: Didn't find a matching TT\n");
  1034. }
  1035. /* Is this a LS/FS device under an external HS hub? */
  1036. if (udev->tt && udev->tt->hub->parent) {
  1037. slot_ctx->tt_info = cpu_to_le32(udev->tt->hub->slot_id |
  1038. (udev->ttport << 8));
  1039. if (udev->tt->multi)
  1040. slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
  1041. }
  1042. xhci_dbg(xhci, "udev->tt = %p\n", udev->tt);
  1043. xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport);
  1044. /* Step 4 - ring already allocated */
  1045. /* Step 5 */
  1046. ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP));
  1047. /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
  1048. ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3) |
  1049. max_packets);
  1050. ep0_ctx->deq = cpu_to_le64(dev->eps[0].ring->first_seg->dma |
  1051. dev->eps[0].ring->cycle_state);
  1052. trace_xhci_setup_addressable_virt_device(dev);
  1053. /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
  1054. return 0;
  1055. }
  1056. /*
  1057. * Convert interval expressed as 2^(bInterval - 1) == interval into
  1058. * straight exponent value 2^n == interval.
  1059. *
  1060. */
  1061. static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
  1062. struct usb_host_endpoint *ep)
  1063. {
  1064. unsigned int interval;
  1065. interval = clamp_val(ep->desc.bInterval, 1, 16) - 1;
  1066. if (interval != ep->desc.bInterval - 1)
  1067. dev_warn(&udev->dev,
  1068. "ep %#x - rounding interval to %d %sframes\n",
  1069. ep->desc.bEndpointAddress,
  1070. 1 << interval,
  1071. udev->speed == USB_SPEED_FULL ? "" : "micro");
  1072. if (udev->speed == USB_SPEED_FULL) {
  1073. /*
  1074. * Full speed isoc endpoints specify interval in frames,
  1075. * not microframes. We are using microframes everywhere,
  1076. * so adjust accordingly.
  1077. */
  1078. interval += 3; /* 1 frame = 2^3 uframes */
  1079. }
  1080. return interval;
  1081. }
  1082. /*
  1083. * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
  1084. * microframes, rounded down to nearest power of 2.
  1085. */
  1086. static unsigned int xhci_microframes_to_exponent(struct usb_device *udev,
  1087. struct usb_host_endpoint *ep, unsigned int desc_interval,
  1088. unsigned int min_exponent, unsigned int max_exponent)
  1089. {
  1090. unsigned int interval;
  1091. interval = fls(desc_interval) - 1;
  1092. interval = clamp_val(interval, min_exponent, max_exponent);
  1093. if ((1 << interval) != desc_interval)
  1094. dev_dbg(&udev->dev,
  1095. "ep %#x - rounding interval to %d microframes, ep desc says %d microframes\n",
  1096. ep->desc.bEndpointAddress,
  1097. 1 << interval,
  1098. desc_interval);
  1099. return interval;
  1100. }
  1101. static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
  1102. struct usb_host_endpoint *ep)
  1103. {
  1104. if (ep->desc.bInterval == 0)
  1105. return 0;
  1106. return xhci_microframes_to_exponent(udev, ep,
  1107. ep->desc.bInterval, 0, 15);
  1108. }
  1109. static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
  1110. struct usb_host_endpoint *ep)
  1111. {
  1112. return xhci_microframes_to_exponent(udev, ep,
  1113. ep->desc.bInterval * 8, 3, 10);
  1114. }
  1115. /* Return the polling or NAK interval.
  1116. *
  1117. * The polling interval is expressed in "microframes". If xHCI's Interval field
  1118. * is set to N, it will service the endpoint every 2^(Interval)*125us.
  1119. *
  1120. * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
  1121. * is set to 0.
  1122. */
  1123. static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
  1124. struct usb_host_endpoint *ep)
  1125. {
  1126. unsigned int interval = 0;
  1127. switch (udev->speed) {
  1128. case USB_SPEED_HIGH:
  1129. /* Max NAK rate */
  1130. if (usb_endpoint_xfer_control(&ep->desc) ||
  1131. usb_endpoint_xfer_bulk(&ep->desc)) {
  1132. interval = xhci_parse_microframe_interval(udev, ep);
  1133. break;
  1134. }
  1135. /* Fall through - SS and HS isoc/int have same decoding */
  1136. case USB_SPEED_SUPER_PLUS:
  1137. case USB_SPEED_SUPER:
  1138. if (usb_endpoint_xfer_int(&ep->desc) ||
  1139. usb_endpoint_xfer_isoc(&ep->desc)) {
  1140. interval = xhci_parse_exponent_interval(udev, ep);
  1141. }
  1142. break;
  1143. case USB_SPEED_FULL:
  1144. if (usb_endpoint_xfer_isoc(&ep->desc)) {
  1145. interval = xhci_parse_exponent_interval(udev, ep);
  1146. break;
  1147. }
  1148. /*
  1149. * Fall through for interrupt endpoint interval decoding
  1150. * since it uses the same rules as low speed interrupt
  1151. * endpoints.
  1152. */
  1153. /* fall through */
  1154. case USB_SPEED_LOW:
  1155. if (usb_endpoint_xfer_int(&ep->desc) ||
  1156. usb_endpoint_xfer_isoc(&ep->desc)) {
  1157. interval = xhci_parse_frame_interval(udev, ep);
  1158. }
  1159. break;
  1160. default:
  1161. BUG();
  1162. }
  1163. return interval;
  1164. }
  1165. /* The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
  1166. * High speed endpoint descriptors can define "the number of additional
  1167. * transaction opportunities per microframe", but that goes in the Max Burst
  1168. * endpoint context field.
  1169. */
  1170. static u32 xhci_get_endpoint_mult(struct usb_device *udev,
  1171. struct usb_host_endpoint *ep)
  1172. {
  1173. if (udev->speed < USB_SPEED_SUPER ||
  1174. !usb_endpoint_xfer_isoc(&ep->desc))
  1175. return 0;
  1176. return ep->ss_ep_comp.bmAttributes;
  1177. }
  1178. static u32 xhci_get_endpoint_max_burst(struct usb_device *udev,
  1179. struct usb_host_endpoint *ep)
  1180. {
  1181. /* Super speed and Plus have max burst in ep companion desc */
  1182. if (udev->speed >= USB_SPEED_SUPER)
  1183. return ep->ss_ep_comp.bMaxBurst;
  1184. if (udev->speed == USB_SPEED_HIGH &&
  1185. (usb_endpoint_xfer_isoc(&ep->desc) ||
  1186. usb_endpoint_xfer_int(&ep->desc)))
  1187. return usb_endpoint_maxp_mult(&ep->desc) - 1;
  1188. return 0;
  1189. }
  1190. static u32 xhci_get_endpoint_type(struct usb_host_endpoint *ep)
  1191. {
  1192. int in;
  1193. in = usb_endpoint_dir_in(&ep->desc);
  1194. switch (usb_endpoint_type(&ep->desc)) {
  1195. case USB_ENDPOINT_XFER_CONTROL:
  1196. return CTRL_EP;
  1197. case USB_ENDPOINT_XFER_BULK:
  1198. return in ? BULK_IN_EP : BULK_OUT_EP;
  1199. case USB_ENDPOINT_XFER_ISOC:
  1200. return in ? ISOC_IN_EP : ISOC_OUT_EP;
  1201. case USB_ENDPOINT_XFER_INT:
  1202. return in ? INT_IN_EP : INT_OUT_EP;
  1203. }
  1204. return 0;
  1205. }
  1206. /* Return the maximum endpoint service interval time (ESIT) payload.
  1207. * Basically, this is the maxpacket size, multiplied by the burst size
  1208. * and mult size.
  1209. */
  1210. static u32 xhci_get_max_esit_payload(struct usb_device *udev,
  1211. struct usb_host_endpoint *ep)
  1212. {
  1213. int max_burst;
  1214. int max_packet;
  1215. /* Only applies for interrupt or isochronous endpoints */
  1216. if (usb_endpoint_xfer_control(&ep->desc) ||
  1217. usb_endpoint_xfer_bulk(&ep->desc))
  1218. return 0;
  1219. /* SuperSpeedPlus Isoc ep sending over 48k per esit */
  1220. if ((udev->speed >= USB_SPEED_SUPER_PLUS) &&
  1221. USB_SS_SSP_ISOC_COMP(ep->ss_ep_comp.bmAttributes))
  1222. return le32_to_cpu(ep->ssp_isoc_ep_comp.dwBytesPerInterval);
  1223. /* SuperSpeed or SuperSpeedPlus Isoc ep with less than 48k per esit */
  1224. else if (udev->speed >= USB_SPEED_SUPER)
  1225. return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval);
  1226. max_packet = usb_endpoint_maxp(&ep->desc);
  1227. max_burst = usb_endpoint_maxp_mult(&ep->desc);
  1228. /* A 0 in max burst means 1 transfer per ESIT */
  1229. return max_packet * max_burst;
  1230. }
  1231. /* Set up an endpoint with one ring segment. Do not allocate stream rings.
  1232. * Drivers will have to call usb_alloc_streams() to do that.
  1233. */
  1234. int xhci_endpoint_init(struct xhci_hcd *xhci,
  1235. struct xhci_virt_device *virt_dev,
  1236. struct usb_device *udev,
  1237. struct usb_host_endpoint *ep,
  1238. gfp_t mem_flags)
  1239. {
  1240. unsigned int ep_index;
  1241. struct xhci_ep_ctx *ep_ctx;
  1242. struct xhci_ring *ep_ring;
  1243. unsigned int max_packet;
  1244. enum xhci_ring_type ring_type;
  1245. u32 max_esit_payload;
  1246. u32 endpoint_type;
  1247. unsigned int max_burst;
  1248. unsigned int interval;
  1249. unsigned int mult;
  1250. unsigned int avg_trb_len;
  1251. unsigned int err_count = 0;
  1252. ep_index = xhci_get_endpoint_index(&ep->desc);
  1253. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  1254. endpoint_type = xhci_get_endpoint_type(ep);
  1255. if (!endpoint_type)
  1256. return -EINVAL;
  1257. ring_type = usb_endpoint_type(&ep->desc);
  1258. /*
  1259. * Get values to fill the endpoint context, mostly from ep descriptor.
  1260. * The average TRB buffer lengt for bulk endpoints is unclear as we
  1261. * have no clue on scatter gather list entry size. For Isoc and Int,
  1262. * set it to max available. See xHCI 1.1 spec 4.14.1.1 for details.
  1263. */
  1264. max_esit_payload = xhci_get_max_esit_payload(udev, ep);
  1265. interval = xhci_get_endpoint_interval(udev, ep);
  1266. /* Periodic endpoint bInterval limit quirk */
  1267. if (usb_endpoint_xfer_int(&ep->desc) ||
  1268. usb_endpoint_xfer_isoc(&ep->desc)) {
  1269. if ((xhci->quirks & XHCI_LIMIT_ENDPOINT_INTERVAL_7) &&
  1270. udev->speed >= USB_SPEED_HIGH &&
  1271. interval >= 7) {
  1272. interval = 6;
  1273. }
  1274. }
  1275. mult = xhci_get_endpoint_mult(udev, ep);
  1276. max_packet = usb_endpoint_maxp(&ep->desc);
  1277. max_burst = xhci_get_endpoint_max_burst(udev, ep);
  1278. avg_trb_len = max_esit_payload;
  1279. /* FIXME dig Mult and streams info out of ep companion desc */
  1280. /* Allow 3 retries for everything but isoc, set CErr = 3 */
  1281. if (!usb_endpoint_xfer_isoc(&ep->desc))
  1282. err_count = 3;
  1283. /* HS bulk max packet should be 512, FS bulk supports 8, 16, 32 or 64 */
  1284. if (usb_endpoint_xfer_bulk(&ep->desc)) {
  1285. if (udev->speed == USB_SPEED_HIGH)
  1286. max_packet = 512;
  1287. if (udev->speed == USB_SPEED_FULL) {
  1288. max_packet = rounddown_pow_of_two(max_packet);
  1289. max_packet = clamp_val(max_packet, 8, 64);
  1290. }
  1291. }
  1292. /* xHCI 1.0 and 1.1 indicates that ctrl ep avg TRB Length should be 8 */
  1293. if (usb_endpoint_xfer_control(&ep->desc) && xhci->hci_version >= 0x100)
  1294. avg_trb_len = 8;
  1295. /* xhci 1.1 with LEC support doesn't use mult field, use RsvdZ */
  1296. if ((xhci->hci_version > 0x100) && HCC2_LEC(xhci->hcc_params2))
  1297. mult = 0;
  1298. /* Set up the endpoint ring */
  1299. virt_dev->eps[ep_index].new_ring =
  1300. xhci_ring_alloc(xhci, 2, 1, ring_type, max_packet, mem_flags);
  1301. if (!virt_dev->eps[ep_index].new_ring)
  1302. return -ENOMEM;
  1303. virt_dev->eps[ep_index].skip = false;
  1304. ep_ring = virt_dev->eps[ep_index].new_ring;
  1305. /* Fill the endpoint context */
  1306. ep_ctx->ep_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
  1307. EP_INTERVAL(interval) |
  1308. EP_MULT(mult));
  1309. ep_ctx->ep_info2 = cpu_to_le32(EP_TYPE(endpoint_type) |
  1310. MAX_PACKET(max_packet) |
  1311. MAX_BURST(max_burst) |
  1312. ERROR_COUNT(err_count));
  1313. ep_ctx->deq = cpu_to_le64(ep_ring->first_seg->dma |
  1314. ep_ring->cycle_state);
  1315. ep_ctx->tx_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
  1316. EP_AVG_TRB_LENGTH(avg_trb_len));
  1317. return 0;
  1318. }
  1319. void xhci_endpoint_zero(struct xhci_hcd *xhci,
  1320. struct xhci_virt_device *virt_dev,
  1321. struct usb_host_endpoint *ep)
  1322. {
  1323. unsigned int ep_index;
  1324. struct xhci_ep_ctx *ep_ctx;
  1325. ep_index = xhci_get_endpoint_index(&ep->desc);
  1326. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  1327. ep_ctx->ep_info = 0;
  1328. ep_ctx->ep_info2 = 0;
  1329. ep_ctx->deq = 0;
  1330. ep_ctx->tx_info = 0;
  1331. /* Don't free the endpoint ring until the set interface or configuration
  1332. * request succeeds.
  1333. */
  1334. }
  1335. void xhci_clear_endpoint_bw_info(struct xhci_bw_info *bw_info)
  1336. {
  1337. bw_info->ep_interval = 0;
  1338. bw_info->mult = 0;
  1339. bw_info->num_packets = 0;
  1340. bw_info->max_packet_size = 0;
  1341. bw_info->type = 0;
  1342. bw_info->max_esit_payload = 0;
  1343. }
  1344. void xhci_update_bw_info(struct xhci_hcd *xhci,
  1345. struct xhci_container_ctx *in_ctx,
  1346. struct xhci_input_control_ctx *ctrl_ctx,
  1347. struct xhci_virt_device *virt_dev)
  1348. {
  1349. struct xhci_bw_info *bw_info;
  1350. struct xhci_ep_ctx *ep_ctx;
  1351. unsigned int ep_type;
  1352. int i;
  1353. for (i = 1; i < 31; i++) {
  1354. bw_info = &virt_dev->eps[i].bw_info;
  1355. /* We can't tell what endpoint type is being dropped, but
  1356. * unconditionally clearing the bandwidth info for non-periodic
  1357. * endpoints should be harmless because the info will never be
  1358. * set in the first place.
  1359. */
  1360. if (!EP_IS_ADDED(ctrl_ctx, i) && EP_IS_DROPPED(ctrl_ctx, i)) {
  1361. /* Dropped endpoint */
  1362. xhci_clear_endpoint_bw_info(bw_info);
  1363. continue;
  1364. }
  1365. if (EP_IS_ADDED(ctrl_ctx, i)) {
  1366. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, i);
  1367. ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
  1368. /* Ignore non-periodic endpoints */
  1369. if (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
  1370. ep_type != ISOC_IN_EP &&
  1371. ep_type != INT_IN_EP)
  1372. continue;
  1373. /* Added or changed endpoint */
  1374. bw_info->ep_interval = CTX_TO_EP_INTERVAL(
  1375. le32_to_cpu(ep_ctx->ep_info));
  1376. /* Number of packets and mult are zero-based in the
  1377. * input context, but we want one-based for the
  1378. * interval table.
  1379. */
  1380. bw_info->mult = CTX_TO_EP_MULT(
  1381. le32_to_cpu(ep_ctx->ep_info)) + 1;
  1382. bw_info->num_packets = CTX_TO_MAX_BURST(
  1383. le32_to_cpu(ep_ctx->ep_info2)) + 1;
  1384. bw_info->max_packet_size = MAX_PACKET_DECODED(
  1385. le32_to_cpu(ep_ctx->ep_info2));
  1386. bw_info->type = ep_type;
  1387. bw_info->max_esit_payload = CTX_TO_MAX_ESIT_PAYLOAD(
  1388. le32_to_cpu(ep_ctx->tx_info));
  1389. }
  1390. }
  1391. }
  1392. /* Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
  1393. * Useful when you want to change one particular aspect of the endpoint and then
  1394. * issue a configure endpoint command.
  1395. */
  1396. void xhci_endpoint_copy(struct xhci_hcd *xhci,
  1397. struct xhci_container_ctx *in_ctx,
  1398. struct xhci_container_ctx *out_ctx,
  1399. unsigned int ep_index)
  1400. {
  1401. struct xhci_ep_ctx *out_ep_ctx;
  1402. struct xhci_ep_ctx *in_ep_ctx;
  1403. out_ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1404. in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  1405. in_ep_ctx->ep_info = out_ep_ctx->ep_info;
  1406. in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
  1407. in_ep_ctx->deq = out_ep_ctx->deq;
  1408. in_ep_ctx->tx_info = out_ep_ctx->tx_info;
  1409. if (xhci->quirks & XHCI_MTK_HOST) {
  1410. in_ep_ctx->reserved[0] = out_ep_ctx->reserved[0];
  1411. in_ep_ctx->reserved[1] = out_ep_ctx->reserved[1];
  1412. }
  1413. }
  1414. /* Copy output xhci_slot_ctx to the input xhci_slot_ctx.
  1415. * Useful when you want to change one particular aspect of the endpoint and then
  1416. * issue a configure endpoint command. Only the context entries field matters,
  1417. * but we'll copy the whole thing anyway.
  1418. */
  1419. void xhci_slot_copy(struct xhci_hcd *xhci,
  1420. struct xhci_container_ctx *in_ctx,
  1421. struct xhci_container_ctx *out_ctx)
  1422. {
  1423. struct xhci_slot_ctx *in_slot_ctx;
  1424. struct xhci_slot_ctx *out_slot_ctx;
  1425. in_slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1426. out_slot_ctx = xhci_get_slot_ctx(xhci, out_ctx);
  1427. in_slot_ctx->dev_info = out_slot_ctx->dev_info;
  1428. in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
  1429. in_slot_ctx->tt_info = out_slot_ctx->tt_info;
  1430. in_slot_ctx->dev_state = out_slot_ctx->dev_state;
  1431. }
  1432. /* Set up the scratchpad buffer array and scratchpad buffers, if needed. */
  1433. static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
  1434. {
  1435. int i;
  1436. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1437. int num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
  1438. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  1439. "Allocating %d scratchpad buffers", num_sp);
  1440. if (!num_sp)
  1441. return 0;
  1442. xhci->scratchpad = kzalloc_node(sizeof(*xhci->scratchpad), flags,
  1443. dev_to_node(dev));
  1444. if (!xhci->scratchpad)
  1445. goto fail_sp;
  1446. xhci->scratchpad->sp_array = dma_alloc_coherent(dev,
  1447. num_sp * sizeof(u64),
  1448. &xhci->scratchpad->sp_dma, flags);
  1449. if (!xhci->scratchpad->sp_array)
  1450. goto fail_sp2;
  1451. xhci->scratchpad->sp_buffers = kcalloc_node(num_sp, sizeof(void *),
  1452. flags, dev_to_node(dev));
  1453. if (!xhci->scratchpad->sp_buffers)
  1454. goto fail_sp3;
  1455. xhci->dcbaa->dev_context_ptrs[0] = cpu_to_le64(xhci->scratchpad->sp_dma);
  1456. for (i = 0; i < num_sp; i++) {
  1457. dma_addr_t dma;
  1458. void *buf = dma_zalloc_coherent(dev, xhci->page_size, &dma,
  1459. flags);
  1460. if (!buf)
  1461. goto fail_sp4;
  1462. xhci->scratchpad->sp_array[i] = dma;
  1463. xhci->scratchpad->sp_buffers[i] = buf;
  1464. }
  1465. return 0;
  1466. fail_sp4:
  1467. for (i = i - 1; i >= 0; i--) {
  1468. dma_free_coherent(dev, xhci->page_size,
  1469. xhci->scratchpad->sp_buffers[i],
  1470. xhci->scratchpad->sp_array[i]);
  1471. }
  1472. kfree(xhci->scratchpad->sp_buffers);
  1473. fail_sp3:
  1474. dma_free_coherent(dev, num_sp * sizeof(u64),
  1475. xhci->scratchpad->sp_array,
  1476. xhci->scratchpad->sp_dma);
  1477. fail_sp2:
  1478. kfree(xhci->scratchpad);
  1479. xhci->scratchpad = NULL;
  1480. fail_sp:
  1481. return -ENOMEM;
  1482. }
  1483. static void scratchpad_free(struct xhci_hcd *xhci)
  1484. {
  1485. int num_sp;
  1486. int i;
  1487. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1488. if (!xhci->scratchpad)
  1489. return;
  1490. num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
  1491. for (i = 0; i < num_sp; i++) {
  1492. dma_free_coherent(dev, xhci->page_size,
  1493. xhci->scratchpad->sp_buffers[i],
  1494. xhci->scratchpad->sp_array[i]);
  1495. }
  1496. kfree(xhci->scratchpad->sp_buffers);
  1497. dma_free_coherent(dev, num_sp * sizeof(u64),
  1498. xhci->scratchpad->sp_array,
  1499. xhci->scratchpad->sp_dma);
  1500. kfree(xhci->scratchpad);
  1501. xhci->scratchpad = NULL;
  1502. }
  1503. struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
  1504. bool allocate_completion, gfp_t mem_flags)
  1505. {
  1506. struct xhci_command *command;
  1507. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1508. command = kzalloc_node(sizeof(*command), mem_flags, dev_to_node(dev));
  1509. if (!command)
  1510. return NULL;
  1511. if (allocate_completion) {
  1512. command->completion =
  1513. kzalloc_node(sizeof(struct completion), mem_flags,
  1514. dev_to_node(dev));
  1515. if (!command->completion) {
  1516. kfree(command);
  1517. return NULL;
  1518. }
  1519. init_completion(command->completion);
  1520. }
  1521. command->status = 0;
  1522. INIT_LIST_HEAD(&command->cmd_list);
  1523. return command;
  1524. }
  1525. struct xhci_command *xhci_alloc_command_with_ctx(struct xhci_hcd *xhci,
  1526. bool allocate_completion, gfp_t mem_flags)
  1527. {
  1528. struct xhci_command *command;
  1529. command = xhci_alloc_command(xhci, allocate_completion, mem_flags);
  1530. if (!command)
  1531. return NULL;
  1532. command->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT,
  1533. mem_flags);
  1534. if (!command->in_ctx) {
  1535. kfree(command->completion);
  1536. kfree(command);
  1537. return NULL;
  1538. }
  1539. return command;
  1540. }
  1541. void xhci_urb_free_priv(struct urb_priv *urb_priv)
  1542. {
  1543. kfree(urb_priv);
  1544. }
  1545. void xhci_free_command(struct xhci_hcd *xhci,
  1546. struct xhci_command *command)
  1547. {
  1548. xhci_free_container_ctx(xhci,
  1549. command->in_ctx);
  1550. kfree(command->completion);
  1551. kfree(command);
  1552. }
  1553. int xhci_alloc_erst(struct xhci_hcd *xhci,
  1554. struct xhci_ring *evt_ring,
  1555. struct xhci_erst *erst,
  1556. gfp_t flags)
  1557. {
  1558. size_t size;
  1559. unsigned int val;
  1560. struct xhci_segment *seg;
  1561. struct xhci_erst_entry *entry;
  1562. size = sizeof(struct xhci_erst_entry) * evt_ring->num_segs;
  1563. erst->entries = dma_zalloc_coherent(xhci_to_hcd(xhci)->self.sysdev,
  1564. size, &erst->erst_dma_addr, flags);
  1565. if (!erst->entries)
  1566. return -ENOMEM;
  1567. erst->num_entries = evt_ring->num_segs;
  1568. seg = evt_ring->first_seg;
  1569. for (val = 0; val < evt_ring->num_segs; val++) {
  1570. entry = &erst->entries[val];
  1571. entry->seg_addr = cpu_to_le64(seg->dma);
  1572. entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
  1573. entry->rsvd = 0;
  1574. seg = seg->next;
  1575. }
  1576. return 0;
  1577. }
  1578. void xhci_free_erst(struct xhci_hcd *xhci, struct xhci_erst *erst)
  1579. {
  1580. size_t size;
  1581. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1582. size = sizeof(struct xhci_erst_entry) * (erst->num_entries);
  1583. if (erst->entries)
  1584. dma_free_coherent(dev, size,
  1585. erst->entries,
  1586. erst->erst_dma_addr);
  1587. erst->entries = NULL;
  1588. }
  1589. void xhci_mem_cleanup(struct xhci_hcd *xhci)
  1590. {
  1591. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1592. int i, j, num_ports;
  1593. cancel_delayed_work_sync(&xhci->cmd_timer);
  1594. xhci_free_erst(xhci, &xhci->erst);
  1595. if (xhci->event_ring)
  1596. xhci_ring_free(xhci, xhci->event_ring);
  1597. xhci->event_ring = NULL;
  1598. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed event ring");
  1599. if (xhci->lpm_command)
  1600. xhci_free_command(xhci, xhci->lpm_command);
  1601. xhci->lpm_command = NULL;
  1602. if (xhci->cmd_ring)
  1603. xhci_ring_free(xhci, xhci->cmd_ring);
  1604. xhci->cmd_ring = NULL;
  1605. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed command ring");
  1606. xhci_cleanup_command_queue(xhci);
  1607. num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
  1608. for (i = 0; i < num_ports && xhci->rh_bw; i++) {
  1609. struct xhci_interval_bw_table *bwt = &xhci->rh_bw[i].bw_table;
  1610. for (j = 0; j < XHCI_MAX_INTERVAL; j++) {
  1611. struct list_head *ep = &bwt->interval_bw[j].endpoints;
  1612. while (!list_empty(ep))
  1613. list_del_init(ep->next);
  1614. }
  1615. }
  1616. for (i = HCS_MAX_SLOTS(xhci->hcs_params1); i > 0; i--)
  1617. xhci_free_virt_devices_depth_first(xhci, i);
  1618. dma_pool_destroy(xhci->segment_pool);
  1619. xhci->segment_pool = NULL;
  1620. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed segment pool");
  1621. dma_pool_destroy(xhci->device_pool);
  1622. xhci->device_pool = NULL;
  1623. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed device context pool");
  1624. dma_pool_destroy(xhci->small_streams_pool);
  1625. xhci->small_streams_pool = NULL;
  1626. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  1627. "Freed small stream array pool");
  1628. dma_pool_destroy(xhci->medium_streams_pool);
  1629. xhci->medium_streams_pool = NULL;
  1630. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  1631. "Freed medium stream array pool");
  1632. if (xhci->dcbaa)
  1633. dma_free_coherent(dev, sizeof(*xhci->dcbaa),
  1634. xhci->dcbaa, xhci->dcbaa->dma);
  1635. xhci->dcbaa = NULL;
  1636. scratchpad_free(xhci);
  1637. if (!xhci->rh_bw)
  1638. goto no_bw;
  1639. for (i = 0; i < num_ports; i++) {
  1640. struct xhci_tt_bw_info *tt, *n;
  1641. list_for_each_entry_safe(tt, n, &xhci->rh_bw[i].tts, tt_list) {
  1642. list_del(&tt->tt_list);
  1643. kfree(tt);
  1644. }
  1645. }
  1646. no_bw:
  1647. xhci->cmd_ring_reserved_trbs = 0;
  1648. xhci->usb2_rhub.num_ports = 0;
  1649. xhci->usb3_rhub.num_ports = 0;
  1650. xhci->num_active_eps = 0;
  1651. kfree(xhci->usb2_rhub.ports);
  1652. kfree(xhci->usb3_rhub.ports);
  1653. kfree(xhci->hw_ports);
  1654. kfree(xhci->rh_bw);
  1655. kfree(xhci->ext_caps);
  1656. for (i = 0; i < xhci->num_port_caps; i++)
  1657. kfree(xhci->port_caps[i].psi);
  1658. kfree(xhci->port_caps);
  1659. xhci->num_port_caps = 0;
  1660. xhci->usb2_rhub.ports = NULL;
  1661. xhci->usb3_rhub.ports = NULL;
  1662. xhci->hw_ports = NULL;
  1663. xhci->rh_bw = NULL;
  1664. xhci->ext_caps = NULL;
  1665. xhci->page_size = 0;
  1666. xhci->page_shift = 0;
  1667. xhci->bus_state[0].bus_suspended = 0;
  1668. xhci->bus_state[1].bus_suspended = 0;
  1669. }
  1670. static int xhci_test_trb_in_td(struct xhci_hcd *xhci,
  1671. struct xhci_segment *input_seg,
  1672. union xhci_trb *start_trb,
  1673. union xhci_trb *end_trb,
  1674. dma_addr_t input_dma,
  1675. struct xhci_segment *result_seg,
  1676. char *test_name, int test_number)
  1677. {
  1678. unsigned long long start_dma;
  1679. unsigned long long end_dma;
  1680. struct xhci_segment *seg;
  1681. start_dma = xhci_trb_virt_to_dma(input_seg, start_trb);
  1682. end_dma = xhci_trb_virt_to_dma(input_seg, end_trb);
  1683. seg = trb_in_td(xhci, input_seg, start_trb, end_trb, input_dma, false);
  1684. if (seg != result_seg) {
  1685. xhci_warn(xhci, "WARN: %s TRB math test %d failed!\n",
  1686. test_name, test_number);
  1687. xhci_warn(xhci, "Tested TRB math w/ seg %p and "
  1688. "input DMA 0x%llx\n",
  1689. input_seg,
  1690. (unsigned long long) input_dma);
  1691. xhci_warn(xhci, "starting TRB %p (0x%llx DMA), "
  1692. "ending TRB %p (0x%llx DMA)\n",
  1693. start_trb, start_dma,
  1694. end_trb, end_dma);
  1695. xhci_warn(xhci, "Expected seg %p, got seg %p\n",
  1696. result_seg, seg);
  1697. trb_in_td(xhci, input_seg, start_trb, end_trb, input_dma,
  1698. true);
  1699. return -1;
  1700. }
  1701. return 0;
  1702. }
  1703. /* TRB math checks for xhci_trb_in_td(), using the command and event rings. */
  1704. static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci)
  1705. {
  1706. struct {
  1707. dma_addr_t input_dma;
  1708. struct xhci_segment *result_seg;
  1709. } simple_test_vector [] = {
  1710. /* A zeroed DMA field should fail */
  1711. { 0, NULL },
  1712. /* One TRB before the ring start should fail */
  1713. { xhci->event_ring->first_seg->dma - 16, NULL },
  1714. /* One byte before the ring start should fail */
  1715. { xhci->event_ring->first_seg->dma - 1, NULL },
  1716. /* Starting TRB should succeed */
  1717. { xhci->event_ring->first_seg->dma, xhci->event_ring->first_seg },
  1718. /* Ending TRB should succeed */
  1719. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16,
  1720. xhci->event_ring->first_seg },
  1721. /* One byte after the ring end should fail */
  1722. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16 + 1, NULL },
  1723. /* One TRB after the ring end should fail */
  1724. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT)*16, NULL },
  1725. /* An address of all ones should fail */
  1726. { (dma_addr_t) (~0), NULL },
  1727. };
  1728. struct {
  1729. struct xhci_segment *input_seg;
  1730. union xhci_trb *start_trb;
  1731. union xhci_trb *end_trb;
  1732. dma_addr_t input_dma;
  1733. struct xhci_segment *result_seg;
  1734. } complex_test_vector [] = {
  1735. /* Test feeding a valid DMA address from a different ring */
  1736. { .input_seg = xhci->event_ring->first_seg,
  1737. .start_trb = xhci->event_ring->first_seg->trbs,
  1738. .end_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1739. .input_dma = xhci->cmd_ring->first_seg->dma,
  1740. .result_seg = NULL,
  1741. },
  1742. /* Test feeding a valid end TRB from a different ring */
  1743. { .input_seg = xhci->event_ring->first_seg,
  1744. .start_trb = xhci->event_ring->first_seg->trbs,
  1745. .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1746. .input_dma = xhci->cmd_ring->first_seg->dma,
  1747. .result_seg = NULL,
  1748. },
  1749. /* Test feeding a valid start and end TRB from a different ring */
  1750. { .input_seg = xhci->event_ring->first_seg,
  1751. .start_trb = xhci->cmd_ring->first_seg->trbs,
  1752. .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1753. .input_dma = xhci->cmd_ring->first_seg->dma,
  1754. .result_seg = NULL,
  1755. },
  1756. /* TRB in this ring, but after this TD */
  1757. { .input_seg = xhci->event_ring->first_seg,
  1758. .start_trb = &xhci->event_ring->first_seg->trbs[0],
  1759. .end_trb = &xhci->event_ring->first_seg->trbs[3],
  1760. .input_dma = xhci->event_ring->first_seg->dma + 4*16,
  1761. .result_seg = NULL,
  1762. },
  1763. /* TRB in this ring, but before this TD */
  1764. { .input_seg = xhci->event_ring->first_seg,
  1765. .start_trb = &xhci->event_ring->first_seg->trbs[3],
  1766. .end_trb = &xhci->event_ring->first_seg->trbs[6],
  1767. .input_dma = xhci->event_ring->first_seg->dma + 2*16,
  1768. .result_seg = NULL,
  1769. },
  1770. /* TRB in this ring, but after this wrapped TD */
  1771. { .input_seg = xhci->event_ring->first_seg,
  1772. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  1773. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  1774. .input_dma = xhci->event_ring->first_seg->dma + 2*16,
  1775. .result_seg = NULL,
  1776. },
  1777. /* TRB in this ring, but before this wrapped TD */
  1778. { .input_seg = xhci->event_ring->first_seg,
  1779. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  1780. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  1781. .input_dma = xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 4)*16,
  1782. .result_seg = NULL,
  1783. },
  1784. /* TRB not in this ring, and we have a wrapped TD */
  1785. { .input_seg = xhci->event_ring->first_seg,
  1786. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  1787. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  1788. .input_dma = xhci->cmd_ring->first_seg->dma + 2*16,
  1789. .result_seg = NULL,
  1790. },
  1791. };
  1792. unsigned int num_tests;
  1793. int i, ret;
  1794. num_tests = ARRAY_SIZE(simple_test_vector);
  1795. for (i = 0; i < num_tests; i++) {
  1796. ret = xhci_test_trb_in_td(xhci,
  1797. xhci->event_ring->first_seg,
  1798. xhci->event_ring->first_seg->trbs,
  1799. &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1800. simple_test_vector[i].input_dma,
  1801. simple_test_vector[i].result_seg,
  1802. "Simple", i);
  1803. if (ret < 0)
  1804. return ret;
  1805. }
  1806. num_tests = ARRAY_SIZE(complex_test_vector);
  1807. for (i = 0; i < num_tests; i++) {
  1808. ret = xhci_test_trb_in_td(xhci,
  1809. complex_test_vector[i].input_seg,
  1810. complex_test_vector[i].start_trb,
  1811. complex_test_vector[i].end_trb,
  1812. complex_test_vector[i].input_dma,
  1813. complex_test_vector[i].result_seg,
  1814. "Complex", i);
  1815. if (ret < 0)
  1816. return ret;
  1817. }
  1818. xhci_dbg(xhci, "TRB math tests passed.\n");
  1819. return 0;
  1820. }
  1821. static void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
  1822. {
  1823. u64 temp;
  1824. dma_addr_t deq;
  1825. deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
  1826. xhci->event_ring->dequeue);
  1827. if (deq == 0 && !in_interrupt())
  1828. xhci_warn(xhci, "WARN something wrong with SW event ring "
  1829. "dequeue ptr.\n");
  1830. /* Update HC event ring dequeue pointer */
  1831. temp = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  1832. temp &= ERST_PTR_MASK;
  1833. /* Don't clear the EHB bit (which is RW1C) because
  1834. * there might be more events to service.
  1835. */
  1836. temp &= ~ERST_EHB;
  1837. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  1838. "// Write event ring dequeue pointer, "
  1839. "preserving EHB bit");
  1840. xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp,
  1841. &xhci->ir_set->erst_dequeue);
  1842. }
  1843. static void xhci_add_in_port(struct xhci_hcd *xhci, unsigned int num_ports,
  1844. __le32 __iomem *addr, int max_caps)
  1845. {
  1846. u32 temp, port_offset, port_count;
  1847. int i;
  1848. u8 major_revision, minor_revision;
  1849. struct xhci_hub *rhub;
  1850. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1851. struct xhci_port_cap *port_cap;
  1852. temp = readl(addr);
  1853. major_revision = XHCI_EXT_PORT_MAJOR(temp);
  1854. minor_revision = XHCI_EXT_PORT_MINOR(temp);
  1855. if (major_revision == 0x03) {
  1856. rhub = &xhci->usb3_rhub;
  1857. } else if (major_revision <= 0x02) {
  1858. rhub = &xhci->usb2_rhub;
  1859. } else {
  1860. xhci_warn(xhci, "Ignoring unknown port speed, "
  1861. "Ext Cap %p, revision = 0x%x\n",
  1862. addr, major_revision);
  1863. /* Ignoring port protocol we can't understand. FIXME */
  1864. return;
  1865. }
  1866. rhub->maj_rev = XHCI_EXT_PORT_MAJOR(temp);
  1867. if (rhub->min_rev < minor_revision)
  1868. rhub->min_rev = minor_revision;
  1869. /* Port offset and count in the third dword, see section 7.2 */
  1870. temp = readl(addr + 2);
  1871. port_offset = XHCI_EXT_PORT_OFF(temp);
  1872. port_count = XHCI_EXT_PORT_COUNT(temp);
  1873. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  1874. "Ext Cap %p, port offset = %u, "
  1875. "count = %u, revision = 0x%x",
  1876. addr, port_offset, port_count, major_revision);
  1877. /* Port count includes the current port offset */
  1878. if (port_offset == 0 || (port_offset + port_count - 1) > num_ports)
  1879. /* WTF? "Valid values are ‘1’ to MaxPorts" */
  1880. return;
  1881. port_cap = &xhci->port_caps[xhci->num_port_caps++];
  1882. if (xhci->num_port_caps > max_caps)
  1883. return;
  1884. port_cap->maj_rev = major_revision;
  1885. port_cap->min_rev = minor_revision;
  1886. port_cap->psi_count = XHCI_EXT_PORT_PSIC(temp);
  1887. if (port_cap->psi_count) {
  1888. port_cap->psi = kcalloc_node(port_cap->psi_count,
  1889. sizeof(*port_cap->psi),
  1890. GFP_KERNEL, dev_to_node(dev));
  1891. if (!port_cap->psi)
  1892. port_cap->psi_count = 0;
  1893. port_cap->psi_uid_count++;
  1894. for (i = 0; i < port_cap->psi_count; i++) {
  1895. port_cap->psi[i] = readl(addr + 4 + i);
  1896. /* count unique ID values, two consecutive entries can
  1897. * have the same ID if link is assymetric
  1898. */
  1899. if (i && (XHCI_EXT_PORT_PSIV(port_cap->psi[i]) !=
  1900. XHCI_EXT_PORT_PSIV(port_cap->psi[i - 1])))
  1901. port_cap->psi_uid_count++;
  1902. xhci_dbg(xhci, "PSIV:%d PSIE:%d PLT:%d PFD:%d LP:%d PSIM:%d\n",
  1903. XHCI_EXT_PORT_PSIV(port_cap->psi[i]),
  1904. XHCI_EXT_PORT_PSIE(port_cap->psi[i]),
  1905. XHCI_EXT_PORT_PLT(port_cap->psi[i]),
  1906. XHCI_EXT_PORT_PFD(port_cap->psi[i]),
  1907. XHCI_EXT_PORT_LP(port_cap->psi[i]),
  1908. XHCI_EXT_PORT_PSIM(port_cap->psi[i]));
  1909. }
  1910. }
  1911. /* cache usb2 port capabilities */
  1912. if (major_revision < 0x03 && xhci->num_ext_caps < max_caps)
  1913. xhci->ext_caps[xhci->num_ext_caps++] = temp;
  1914. /* Check the host's USB2 LPM capability */
  1915. if ((xhci->hci_version == 0x96) && (major_revision != 0x03) &&
  1916. (temp & XHCI_L1C)) {
  1917. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  1918. "xHCI 0.96: support USB2 software lpm");
  1919. xhci->sw_lpm_support = 1;
  1920. }
  1921. if ((xhci->hci_version >= 0x100) && (major_revision != 0x03)) {
  1922. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  1923. "xHCI 1.0: support USB2 software lpm");
  1924. xhci->sw_lpm_support = 1;
  1925. if (temp & XHCI_HLC) {
  1926. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  1927. "xHCI 1.0: support USB2 hardware lpm");
  1928. xhci->hw_lpm_support = 1;
  1929. }
  1930. }
  1931. port_offset--;
  1932. for (i = port_offset; i < (port_offset + port_count); i++) {
  1933. struct xhci_port *hw_port = &xhci->hw_ports[i];
  1934. /* Duplicate entry. Ignore the port if the revisions differ. */
  1935. if (hw_port->rhub) {
  1936. xhci_warn(xhci, "Duplicate port entry, Ext Cap %p,"
  1937. " port %u\n", addr, i);
  1938. xhci_warn(xhci, "Port was marked as USB %u, "
  1939. "duplicated as USB %u\n",
  1940. hw_port->rhub->maj_rev, major_revision);
  1941. /* Only adjust the roothub port counts if we haven't
  1942. * found a similar duplicate.
  1943. */
  1944. if (hw_port->rhub != rhub &&
  1945. hw_port->hcd_portnum != DUPLICATE_ENTRY) {
  1946. hw_port->rhub->num_ports--;
  1947. hw_port->hcd_portnum = DUPLICATE_ENTRY;
  1948. }
  1949. continue;
  1950. }
  1951. hw_port->rhub = rhub;
  1952. hw_port->port_cap = port_cap;
  1953. rhub->num_ports++;
  1954. }
  1955. /* FIXME: Should we disable ports not in the Extended Capabilities? */
  1956. }
  1957. static void xhci_create_rhub_port_array(struct xhci_hcd *xhci,
  1958. struct xhci_hub *rhub, gfp_t flags)
  1959. {
  1960. int port_index = 0;
  1961. int i;
  1962. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1963. if (!rhub->num_ports)
  1964. return;
  1965. rhub->ports = kcalloc_node(rhub->num_ports, sizeof(rhub->ports), flags,
  1966. dev_to_node(dev));
  1967. for (i = 0; i < HCS_MAX_PORTS(xhci->hcs_params1); i++) {
  1968. if (xhci->hw_ports[i].rhub != rhub ||
  1969. xhci->hw_ports[i].hcd_portnum == DUPLICATE_ENTRY)
  1970. continue;
  1971. xhci->hw_ports[i].hcd_portnum = port_index;
  1972. rhub->ports[port_index] = &xhci->hw_ports[i];
  1973. port_index++;
  1974. if (port_index == rhub->num_ports)
  1975. break;
  1976. }
  1977. }
  1978. /*
  1979. * Scan the Extended Capabilities for the "Supported Protocol Capabilities" that
  1980. * specify what speeds each port is supposed to be. We can't count on the port
  1981. * speed bits in the PORTSC register being correct until a device is connected,
  1982. * but we need to set up the two fake roothubs with the correct number of USB
  1983. * 3.0 and USB 2.0 ports at host controller initialization time.
  1984. */
  1985. static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags)
  1986. {
  1987. void __iomem *base;
  1988. u32 offset;
  1989. unsigned int num_ports;
  1990. int i, j;
  1991. int cap_count = 0;
  1992. u32 cap_start;
  1993. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1994. num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
  1995. xhci->hw_ports = kcalloc_node(num_ports, sizeof(*xhci->hw_ports),
  1996. flags, dev_to_node(dev));
  1997. if (!xhci->hw_ports)
  1998. return -ENOMEM;
  1999. for (i = 0; i < num_ports; i++) {
  2000. xhci->hw_ports[i].addr = &xhci->op_regs->port_status_base +
  2001. NUM_PORT_REGS * i;
  2002. xhci->hw_ports[i].hw_portnum = i;
  2003. }
  2004. xhci->rh_bw = kcalloc_node(num_ports, sizeof(*xhci->rh_bw), flags,
  2005. dev_to_node(dev));
  2006. if (!xhci->rh_bw)
  2007. return -ENOMEM;
  2008. for (i = 0; i < num_ports; i++) {
  2009. struct xhci_interval_bw_table *bw_table;
  2010. INIT_LIST_HEAD(&xhci->rh_bw[i].tts);
  2011. bw_table = &xhci->rh_bw[i].bw_table;
  2012. for (j = 0; j < XHCI_MAX_INTERVAL; j++)
  2013. INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints);
  2014. }
  2015. base = &xhci->cap_regs->hc_capbase;
  2016. cap_start = xhci_find_next_ext_cap(base, 0, XHCI_EXT_CAPS_PROTOCOL);
  2017. if (!cap_start) {
  2018. xhci_err(xhci, "No Extended Capability registers, unable to set up roothub\n");
  2019. return -ENODEV;
  2020. }
  2021. offset = cap_start;
  2022. /* count extended protocol capability entries for later caching */
  2023. while (offset) {
  2024. cap_count++;
  2025. offset = xhci_find_next_ext_cap(base, offset,
  2026. XHCI_EXT_CAPS_PROTOCOL);
  2027. }
  2028. xhci->ext_caps = kcalloc_node(cap_count, sizeof(*xhci->ext_caps),
  2029. flags, dev_to_node(dev));
  2030. if (!xhci->ext_caps)
  2031. return -ENOMEM;
  2032. xhci->port_caps = kcalloc_node(cap_count, sizeof(*xhci->port_caps),
  2033. flags, dev_to_node(dev));
  2034. if (!xhci->port_caps)
  2035. return -ENOMEM;
  2036. offset = cap_start;
  2037. while (offset) {
  2038. xhci_add_in_port(xhci, num_ports, base + offset, cap_count);
  2039. if (xhci->usb2_rhub.num_ports + xhci->usb3_rhub.num_ports ==
  2040. num_ports)
  2041. break;
  2042. offset = xhci_find_next_ext_cap(base, offset,
  2043. XHCI_EXT_CAPS_PROTOCOL);
  2044. }
  2045. if (xhci->usb2_rhub.num_ports == 0 && xhci->usb3_rhub.num_ports == 0) {
  2046. xhci_warn(xhci, "No ports on the roothubs?\n");
  2047. return -ENODEV;
  2048. }
  2049. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2050. "Found %u USB 2.0 ports and %u USB 3.0 ports.",
  2051. xhci->usb2_rhub.num_ports, xhci->usb3_rhub.num_ports);
  2052. /* Place limits on the number of roothub ports so that the hub
  2053. * descriptors aren't longer than the USB core will allocate.
  2054. */
  2055. if (xhci->usb3_rhub.num_ports > USB_SS_MAXPORTS) {
  2056. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2057. "Limiting USB 3.0 roothub ports to %u.",
  2058. USB_SS_MAXPORTS);
  2059. xhci->usb3_rhub.num_ports = USB_SS_MAXPORTS;
  2060. }
  2061. if (xhci->usb2_rhub.num_ports > USB_MAXCHILDREN) {
  2062. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2063. "Limiting USB 2.0 roothub ports to %u.",
  2064. USB_MAXCHILDREN);
  2065. xhci->usb2_rhub.num_ports = USB_MAXCHILDREN;
  2066. }
  2067. /*
  2068. * Note we could have all USB 3.0 ports, or all USB 2.0 ports.
  2069. * Not sure how the USB core will handle a hub with no ports...
  2070. */
  2071. xhci_create_rhub_port_array(xhci, &xhci->usb2_rhub, flags);
  2072. xhci_create_rhub_port_array(xhci, &xhci->usb3_rhub, flags);
  2073. return 0;
  2074. }
  2075. int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
  2076. {
  2077. dma_addr_t dma;
  2078. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  2079. unsigned int val, val2;
  2080. u64 val_64;
  2081. u32 page_size, temp;
  2082. int i, ret;
  2083. INIT_LIST_HEAD(&xhci->cmd_list);
  2084. /* init command timeout work */
  2085. INIT_DELAYED_WORK(&xhci->cmd_timer, xhci_handle_command_timeout);
  2086. init_completion(&xhci->cmd_ring_stop_completion);
  2087. page_size = readl(&xhci->op_regs->page_size);
  2088. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2089. "Supported page size register = 0x%x", page_size);
  2090. for (i = 0; i < 16; i++) {
  2091. if ((0x1 & page_size) != 0)
  2092. break;
  2093. page_size = page_size >> 1;
  2094. }
  2095. if (i < 16)
  2096. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2097. "Supported page size of %iK", (1 << (i+12)) / 1024);
  2098. else
  2099. xhci_warn(xhci, "WARN: no supported page size\n");
  2100. /* Use 4K pages, since that's common and the minimum the HC supports */
  2101. xhci->page_shift = 12;
  2102. xhci->page_size = 1 << xhci->page_shift;
  2103. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2104. "HCD page size set to %iK", xhci->page_size / 1024);
  2105. /*
  2106. * Program the Number of Device Slots Enabled field in the CONFIG
  2107. * register with the max value of slots the HC can handle.
  2108. */
  2109. val = HCS_MAX_SLOTS(readl(&xhci->cap_regs->hcs_params1));
  2110. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2111. "// xHC can handle at most %d device slots.", val);
  2112. val2 = readl(&xhci->op_regs->config_reg);
  2113. val |= (val2 & ~HCS_SLOTS_MASK);
  2114. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2115. "// Setting Max device slots reg = 0x%x.", val);
  2116. writel(val, &xhci->op_regs->config_reg);
  2117. /*
  2118. * xHCI section 5.4.6 - doorbell array must be
  2119. * "physically contiguous and 64-byte (cache line) aligned".
  2120. */
  2121. xhci->dcbaa = dma_alloc_coherent(dev, sizeof(*xhci->dcbaa), &dma,
  2122. flags);
  2123. if (!xhci->dcbaa)
  2124. goto fail;
  2125. memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
  2126. xhci->dcbaa->dma = dma;
  2127. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2128. "// Device context base array address = 0x%llx (DMA), %p (virt)",
  2129. (unsigned long long)xhci->dcbaa->dma, xhci->dcbaa);
  2130. xhci_write_64(xhci, dma, &xhci->op_regs->dcbaa_ptr);
  2131. /*
  2132. * Initialize the ring segment pool. The ring must be a contiguous
  2133. * structure comprised of TRBs. The TRBs must be 16 byte aligned,
  2134. * however, the command ring segment needs 64-byte aligned segments
  2135. * and our use of dma addresses in the trb_address_map radix tree needs
  2136. * TRB_SEGMENT_SIZE alignment, so we pick the greater alignment need.
  2137. */
  2138. xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
  2139. TRB_SEGMENT_SIZE, TRB_SEGMENT_SIZE, xhci->page_size);
  2140. /* See Table 46 and Note on Figure 55 */
  2141. xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
  2142. 2112, 64, xhci->page_size);
  2143. if (!xhci->segment_pool || !xhci->device_pool)
  2144. goto fail;
  2145. /* Linear stream context arrays don't have any boundary restrictions,
  2146. * and only need to be 16-byte aligned.
  2147. */
  2148. xhci->small_streams_pool =
  2149. dma_pool_create("xHCI 256 byte stream ctx arrays",
  2150. dev, SMALL_STREAM_ARRAY_SIZE, 16, 0);
  2151. xhci->medium_streams_pool =
  2152. dma_pool_create("xHCI 1KB stream ctx arrays",
  2153. dev, MEDIUM_STREAM_ARRAY_SIZE, 16, 0);
  2154. /* Any stream context array bigger than MEDIUM_STREAM_ARRAY_SIZE
  2155. * will be allocated with dma_alloc_coherent()
  2156. */
  2157. if (!xhci->small_streams_pool || !xhci->medium_streams_pool)
  2158. goto fail;
  2159. /* Set up the command ring to have one segments for now. */
  2160. xhci->cmd_ring = xhci_ring_alloc(xhci, 1, 1, TYPE_COMMAND, 0, flags);
  2161. if (!xhci->cmd_ring)
  2162. goto fail;
  2163. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2164. "Allocated command ring at %p", xhci->cmd_ring);
  2165. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "First segment DMA is 0x%llx",
  2166. (unsigned long long)xhci->cmd_ring->first_seg->dma);
  2167. /* Set the address in the Command Ring Control register */
  2168. val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  2169. val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
  2170. (xhci->cmd_ring->first_seg->dma & (u64) ~CMD_RING_RSVD_BITS) |
  2171. xhci->cmd_ring->cycle_state;
  2172. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2173. "// Setting command ring address to 0x%016llx", val_64);
  2174. xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
  2175. xhci->lpm_command = xhci_alloc_command_with_ctx(xhci, true, flags);
  2176. if (!xhci->lpm_command)
  2177. goto fail;
  2178. /* Reserve one command ring TRB for disabling LPM.
  2179. * Since the USB core grabs the shared usb_bus bandwidth mutex before
  2180. * disabling LPM, we only need to reserve one TRB for all devices.
  2181. */
  2182. xhci->cmd_ring_reserved_trbs++;
  2183. val = readl(&xhci->cap_regs->db_off);
  2184. val &= DBOFF_MASK;
  2185. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2186. "// Doorbell array is located at offset 0x%x"
  2187. " from cap regs base addr", val);
  2188. xhci->dba = (void __iomem *) xhci->cap_regs + val;
  2189. /* Set ir_set to interrupt register set 0 */
  2190. xhci->ir_set = &xhci->run_regs->ir_set[0];
  2191. /*
  2192. * Event ring setup: Allocate a normal ring, but also setup
  2193. * the event ring segment table (ERST). Section 4.9.3.
  2194. */
  2195. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Allocating event ring");
  2196. xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, 1, TYPE_EVENT,
  2197. 0, flags);
  2198. if (!xhci->event_ring)
  2199. goto fail;
  2200. if (xhci_check_trb_in_td_math(xhci) < 0)
  2201. goto fail;
  2202. ret = xhci_alloc_erst(xhci, xhci->event_ring, &xhci->erst, flags);
  2203. if (ret)
  2204. goto fail;
  2205. /* set ERST count with the number of entries in the segment table */
  2206. val = readl(&xhci->ir_set->erst_size);
  2207. val &= ERST_SIZE_MASK;
  2208. val |= ERST_NUM_SEGS;
  2209. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2210. "// Write ERST size = %i to ir_set 0 (some bits preserved)",
  2211. val);
  2212. writel(val, &xhci->ir_set->erst_size);
  2213. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2214. "// Set ERST entries to point to event ring.");
  2215. /* set the segment table base address */
  2216. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2217. "// Set ERST base address for ir_set 0 = 0x%llx",
  2218. (unsigned long long)xhci->erst.erst_dma_addr);
  2219. val_64 = xhci_read_64(xhci, &xhci->ir_set->erst_base);
  2220. val_64 &= ERST_PTR_MASK;
  2221. val_64 |= (xhci->erst.erst_dma_addr & (u64) ~ERST_PTR_MASK);
  2222. xhci_write_64(xhci, val_64, &xhci->ir_set->erst_base);
  2223. /* Set the event ring dequeue address */
  2224. xhci_set_hc_event_deq(xhci);
  2225. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2226. "Wrote ERST address to ir_set 0.");
  2227. /*
  2228. * XXX: Might need to set the Interrupter Moderation Register to
  2229. * something other than the default (~1ms minimum between interrupts).
  2230. * See section 5.5.1.2.
  2231. */
  2232. for (i = 0; i < MAX_HC_SLOTS; i++)
  2233. xhci->devs[i] = NULL;
  2234. for (i = 0; i < USB_MAXCHILDREN; i++) {
  2235. xhci->bus_state[0].resume_done[i] = 0;
  2236. xhci->bus_state[1].resume_done[i] = 0;
  2237. /* Only the USB 2.0 completions will ever be used. */
  2238. init_completion(&xhci->bus_state[1].rexit_done[i]);
  2239. }
  2240. if (scratchpad_alloc(xhci, flags))
  2241. goto fail;
  2242. if (xhci_setup_port_arrays(xhci, flags))
  2243. goto fail;
  2244. /* Enable USB 3.0 device notifications for function remote wake, which
  2245. * is necessary for allowing USB 3.0 devices to do remote wakeup from
  2246. * U3 (device suspend).
  2247. */
  2248. temp = readl(&xhci->op_regs->dev_notification);
  2249. temp &= ~DEV_NOTE_MASK;
  2250. temp |= DEV_NOTE_FWAKE;
  2251. writel(temp, &xhci->op_regs->dev_notification);
  2252. return 0;
  2253. fail:
  2254. xhci_halt(xhci);
  2255. xhci_reset(xhci);
  2256. xhci_mem_cleanup(xhci);
  2257. return -ENOMEM;
  2258. }