xhci-mem.c 74 KB

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