xhci-mem.c 64 KB

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