xhci-mem.c 78 KB

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