xhci-mem.c 54 KB

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