xhci-mem.c 75 KB

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