xhci-mem.c 78 KB

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