xhci-mem.c 74 KB

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