xhci-mem.c 74 KB

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