xhci-mem.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  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. void xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd *xhci,
  742. struct usb_device *udev)
  743. {
  744. struct xhci_virt_device *virt_dev;
  745. struct xhci_ep_ctx *ep0_ctx;
  746. struct xhci_ring *ep_ring;
  747. virt_dev = xhci->devs[udev->slot_id];
  748. ep0_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, 0);
  749. ep_ring = virt_dev->eps[0].ring;
  750. /*
  751. * FIXME we don't keep track of the dequeue pointer very well after a
  752. * Set TR dequeue pointer, so we're setting the dequeue pointer of the
  753. * host to our enqueue pointer. This should only be called after a
  754. * configured device has reset, so all control transfers should have
  755. * been completed or cancelled before the reset.
  756. */
  757. ep0_ctx->deq = xhci_trb_virt_to_dma(ep_ring->enq_seg, ep_ring->enqueue);
  758. ep0_ctx->deq |= ep_ring->cycle_state;
  759. }
  760. /* Setup an xHCI virtual device for a Set Address command */
  761. int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev)
  762. {
  763. struct xhci_virt_device *dev;
  764. struct xhci_ep_ctx *ep0_ctx;
  765. struct usb_device *top_dev;
  766. struct xhci_slot_ctx *slot_ctx;
  767. struct xhci_input_control_ctx *ctrl_ctx;
  768. dev = xhci->devs[udev->slot_id];
  769. /* Slot ID 0 is reserved */
  770. if (udev->slot_id == 0 || !dev) {
  771. xhci_warn(xhci, "Slot ID %d is not assigned to this device\n",
  772. udev->slot_id);
  773. return -EINVAL;
  774. }
  775. ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0);
  776. ctrl_ctx = xhci_get_input_control_ctx(xhci, dev->in_ctx);
  777. slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx);
  778. /* 2) New slot context and endpoint 0 context are valid*/
  779. ctrl_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
  780. /* 3) Only the control endpoint is valid - one endpoint context */
  781. slot_ctx->dev_info |= LAST_CTX(1);
  782. slot_ctx->dev_info |= (u32) udev->route;
  783. switch (udev->speed) {
  784. case USB_SPEED_SUPER:
  785. slot_ctx->dev_info |= (u32) SLOT_SPEED_SS;
  786. break;
  787. case USB_SPEED_HIGH:
  788. slot_ctx->dev_info |= (u32) SLOT_SPEED_HS;
  789. break;
  790. case USB_SPEED_FULL:
  791. slot_ctx->dev_info |= (u32) SLOT_SPEED_FS;
  792. break;
  793. case USB_SPEED_LOW:
  794. slot_ctx->dev_info |= (u32) SLOT_SPEED_LS;
  795. break;
  796. case USB_SPEED_WIRELESS:
  797. xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
  798. return -EINVAL;
  799. break;
  800. default:
  801. /* Speed was set earlier, this shouldn't happen. */
  802. BUG();
  803. }
  804. /* Find the root hub port this device is under */
  805. for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
  806. top_dev = top_dev->parent)
  807. /* Found device below root hub */;
  808. slot_ctx->dev_info2 |= (u32) ROOT_HUB_PORT(top_dev->portnum);
  809. xhci_dbg(xhci, "Set root hub portnum to %d\n", top_dev->portnum);
  810. /* Is this a LS/FS device under a HS hub? */
  811. if ((udev->speed == USB_SPEED_LOW || udev->speed == USB_SPEED_FULL) &&
  812. udev->tt) {
  813. slot_ctx->tt_info = udev->tt->hub->slot_id;
  814. slot_ctx->tt_info |= udev->ttport << 8;
  815. if (udev->tt->multi)
  816. slot_ctx->dev_info |= DEV_MTT;
  817. }
  818. xhci_dbg(xhci, "udev->tt = %p\n", udev->tt);
  819. xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport);
  820. /* Step 4 - ring already allocated */
  821. /* Step 5 */
  822. ep0_ctx->ep_info2 = EP_TYPE(CTRL_EP);
  823. /*
  824. * XXX: Not sure about wireless USB devices.
  825. */
  826. switch (udev->speed) {
  827. case USB_SPEED_SUPER:
  828. ep0_ctx->ep_info2 |= MAX_PACKET(512);
  829. break;
  830. case USB_SPEED_HIGH:
  831. /* USB core guesses at a 64-byte max packet first for FS devices */
  832. case USB_SPEED_FULL:
  833. ep0_ctx->ep_info2 |= MAX_PACKET(64);
  834. break;
  835. case USB_SPEED_LOW:
  836. ep0_ctx->ep_info2 |= MAX_PACKET(8);
  837. break;
  838. case USB_SPEED_WIRELESS:
  839. xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
  840. return -EINVAL;
  841. break;
  842. default:
  843. /* New speed? */
  844. BUG();
  845. }
  846. /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
  847. ep0_ctx->ep_info2 |= MAX_BURST(0);
  848. ep0_ctx->ep_info2 |= ERROR_COUNT(3);
  849. ep0_ctx->deq =
  850. dev->eps[0].ring->first_seg->dma;
  851. ep0_ctx->deq |= dev->eps[0].ring->cycle_state;
  852. /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
  853. return 0;
  854. }
  855. /* Return the polling or NAK interval.
  856. *
  857. * The polling interval is expressed in "microframes". If xHCI's Interval field
  858. * is set to N, it will service the endpoint every 2^(Interval)*125us.
  859. *
  860. * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
  861. * is set to 0.
  862. */
  863. static inline unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
  864. struct usb_host_endpoint *ep)
  865. {
  866. unsigned int interval = 0;
  867. switch (udev->speed) {
  868. case USB_SPEED_HIGH:
  869. /* Max NAK rate */
  870. if (usb_endpoint_xfer_control(&ep->desc) ||
  871. usb_endpoint_xfer_bulk(&ep->desc))
  872. interval = ep->desc.bInterval;
  873. /* Fall through - SS and HS isoc/int have same decoding */
  874. case USB_SPEED_SUPER:
  875. if (usb_endpoint_xfer_int(&ep->desc) ||
  876. usb_endpoint_xfer_isoc(&ep->desc)) {
  877. if (ep->desc.bInterval == 0)
  878. interval = 0;
  879. else
  880. interval = ep->desc.bInterval - 1;
  881. if (interval > 15)
  882. interval = 15;
  883. if (interval != ep->desc.bInterval + 1)
  884. dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
  885. ep->desc.bEndpointAddress, 1 << interval);
  886. }
  887. break;
  888. /* Convert bInterval (in 1-255 frames) to microframes and round down to
  889. * nearest power of 2.
  890. */
  891. case USB_SPEED_FULL:
  892. case USB_SPEED_LOW:
  893. if (usb_endpoint_xfer_int(&ep->desc) ||
  894. usb_endpoint_xfer_isoc(&ep->desc)) {
  895. interval = fls(8*ep->desc.bInterval) - 1;
  896. if (interval > 10)
  897. interval = 10;
  898. if (interval < 3)
  899. interval = 3;
  900. if ((1 << interval) != 8*ep->desc.bInterval)
  901. dev_warn(&udev->dev,
  902. "ep %#x - rounding interval"
  903. " to %d microframes, "
  904. "ep desc says %d microframes\n",
  905. ep->desc.bEndpointAddress,
  906. 1 << interval,
  907. 8*ep->desc.bInterval);
  908. }
  909. break;
  910. default:
  911. BUG();
  912. }
  913. return EP_INTERVAL(interval);
  914. }
  915. /* The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
  916. * High speed endpoint descriptors can define "the number of additional
  917. * transaction opportunities per microframe", but that goes in the Max Burst
  918. * endpoint context field.
  919. */
  920. static inline u32 xhci_get_endpoint_mult(struct usb_device *udev,
  921. struct usb_host_endpoint *ep)
  922. {
  923. if (udev->speed != USB_SPEED_SUPER ||
  924. !usb_endpoint_xfer_isoc(&ep->desc))
  925. return 0;
  926. return ep->ss_ep_comp.bmAttributes;
  927. }
  928. static inline u32 xhci_get_endpoint_type(struct usb_device *udev,
  929. struct usb_host_endpoint *ep)
  930. {
  931. int in;
  932. u32 type;
  933. in = usb_endpoint_dir_in(&ep->desc);
  934. if (usb_endpoint_xfer_control(&ep->desc)) {
  935. type = EP_TYPE(CTRL_EP);
  936. } else if (usb_endpoint_xfer_bulk(&ep->desc)) {
  937. if (in)
  938. type = EP_TYPE(BULK_IN_EP);
  939. else
  940. type = EP_TYPE(BULK_OUT_EP);
  941. } else if (usb_endpoint_xfer_isoc(&ep->desc)) {
  942. if (in)
  943. type = EP_TYPE(ISOC_IN_EP);
  944. else
  945. type = EP_TYPE(ISOC_OUT_EP);
  946. } else if (usb_endpoint_xfer_int(&ep->desc)) {
  947. if (in)
  948. type = EP_TYPE(INT_IN_EP);
  949. else
  950. type = EP_TYPE(INT_OUT_EP);
  951. } else {
  952. BUG();
  953. }
  954. return type;
  955. }
  956. /* Return the maximum endpoint service interval time (ESIT) payload.
  957. * Basically, this is the maxpacket size, multiplied by the burst size
  958. * and mult size.
  959. */
  960. static inline u32 xhci_get_max_esit_payload(struct xhci_hcd *xhci,
  961. struct usb_device *udev,
  962. struct usb_host_endpoint *ep)
  963. {
  964. int max_burst;
  965. int max_packet;
  966. /* Only applies for interrupt or isochronous endpoints */
  967. if (usb_endpoint_xfer_control(&ep->desc) ||
  968. usb_endpoint_xfer_bulk(&ep->desc))
  969. return 0;
  970. if (udev->speed == USB_SPEED_SUPER)
  971. return ep->ss_ep_comp.wBytesPerInterval;
  972. max_packet = ep->desc.wMaxPacketSize & 0x3ff;
  973. max_burst = (ep->desc.wMaxPacketSize & 0x1800) >> 11;
  974. /* A 0 in max burst means 1 transfer per ESIT */
  975. return max_packet * (max_burst + 1);
  976. }
  977. /* Set up an endpoint with one ring segment. Do not allocate stream rings.
  978. * Drivers will have to call usb_alloc_streams() to do that.
  979. */
  980. int xhci_endpoint_init(struct xhci_hcd *xhci,
  981. struct xhci_virt_device *virt_dev,
  982. struct usb_device *udev,
  983. struct usb_host_endpoint *ep,
  984. gfp_t mem_flags)
  985. {
  986. unsigned int ep_index;
  987. struct xhci_ep_ctx *ep_ctx;
  988. struct xhci_ring *ep_ring;
  989. unsigned int max_packet;
  990. unsigned int max_burst;
  991. u32 max_esit_payload;
  992. ep_index = xhci_get_endpoint_index(&ep->desc);
  993. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  994. /* Set up the endpoint ring */
  995. virt_dev->eps[ep_index].new_ring =
  996. xhci_ring_alloc(xhci, 1, true, mem_flags);
  997. if (!virt_dev->eps[ep_index].new_ring) {
  998. /* Attempt to use the ring cache */
  999. if (virt_dev->num_rings_cached == 0)
  1000. return -ENOMEM;
  1001. virt_dev->eps[ep_index].new_ring =
  1002. virt_dev->ring_cache[virt_dev->num_rings_cached];
  1003. virt_dev->ring_cache[virt_dev->num_rings_cached] = NULL;
  1004. virt_dev->num_rings_cached--;
  1005. xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring);
  1006. }
  1007. ep_ring = virt_dev->eps[ep_index].new_ring;
  1008. ep_ctx->deq = ep_ring->first_seg->dma | ep_ring->cycle_state;
  1009. ep_ctx->ep_info = xhci_get_endpoint_interval(udev, ep);
  1010. ep_ctx->ep_info |= EP_MULT(xhci_get_endpoint_mult(udev, ep));
  1011. /* FIXME dig Mult and streams info out of ep companion desc */
  1012. /* Allow 3 retries for everything but isoc;
  1013. * error count = 0 means infinite retries.
  1014. */
  1015. if (!usb_endpoint_xfer_isoc(&ep->desc))
  1016. ep_ctx->ep_info2 = ERROR_COUNT(3);
  1017. else
  1018. ep_ctx->ep_info2 = ERROR_COUNT(1);
  1019. ep_ctx->ep_info2 |= xhci_get_endpoint_type(udev, ep);
  1020. /* Set the max packet size and max burst */
  1021. switch (udev->speed) {
  1022. case USB_SPEED_SUPER:
  1023. max_packet = ep->desc.wMaxPacketSize;
  1024. ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
  1025. /* dig out max burst from ep companion desc */
  1026. max_packet = ep->ss_ep_comp.bMaxBurst;
  1027. if (!max_packet)
  1028. xhci_warn(xhci, "WARN no SS endpoint bMaxBurst\n");
  1029. ep_ctx->ep_info2 |= MAX_BURST(max_packet);
  1030. break;
  1031. case USB_SPEED_HIGH:
  1032. /* bits 11:12 specify the number of additional transaction
  1033. * opportunities per microframe (USB 2.0, section 9.6.6)
  1034. */
  1035. if (usb_endpoint_xfer_isoc(&ep->desc) ||
  1036. usb_endpoint_xfer_int(&ep->desc)) {
  1037. max_burst = (ep->desc.wMaxPacketSize & 0x1800) >> 11;
  1038. ep_ctx->ep_info2 |= MAX_BURST(max_burst);
  1039. }
  1040. /* Fall through */
  1041. case USB_SPEED_FULL:
  1042. case USB_SPEED_LOW:
  1043. max_packet = ep->desc.wMaxPacketSize & 0x3ff;
  1044. ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
  1045. break;
  1046. default:
  1047. BUG();
  1048. }
  1049. max_esit_payload = xhci_get_max_esit_payload(xhci, udev, ep);
  1050. ep_ctx->tx_info = MAX_ESIT_PAYLOAD_FOR_EP(max_esit_payload);
  1051. /*
  1052. * XXX no idea how to calculate the average TRB buffer length for bulk
  1053. * endpoints, as the driver gives us no clue how big each scatter gather
  1054. * list entry (or buffer) is going to be.
  1055. *
  1056. * For isochronous and interrupt endpoints, we set it to the max
  1057. * available, until we have new API in the USB core to allow drivers to
  1058. * declare how much bandwidth they actually need.
  1059. *
  1060. * Normally, it would be calculated by taking the total of the buffer
  1061. * lengths in the TD and then dividing by the number of TRBs in a TD,
  1062. * including link TRBs, No-op TRBs, and Event data TRBs. Since we don't
  1063. * use Event Data TRBs, and we don't chain in a link TRB on short
  1064. * transfers, we're basically dividing by 1.
  1065. */
  1066. ep_ctx->tx_info |= AVG_TRB_LENGTH_FOR_EP(max_esit_payload);
  1067. /* FIXME Debug endpoint context */
  1068. return 0;
  1069. }
  1070. void xhci_endpoint_zero(struct xhci_hcd *xhci,
  1071. struct xhci_virt_device *virt_dev,
  1072. struct usb_host_endpoint *ep)
  1073. {
  1074. unsigned int ep_index;
  1075. struct xhci_ep_ctx *ep_ctx;
  1076. ep_index = xhci_get_endpoint_index(&ep->desc);
  1077. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  1078. ep_ctx->ep_info = 0;
  1079. ep_ctx->ep_info2 = 0;
  1080. ep_ctx->deq = 0;
  1081. ep_ctx->tx_info = 0;
  1082. /* Don't free the endpoint ring until the set interface or configuration
  1083. * request succeeds.
  1084. */
  1085. }
  1086. /* Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
  1087. * Useful when you want to change one particular aspect of the endpoint and then
  1088. * issue a configure endpoint command.
  1089. */
  1090. void xhci_endpoint_copy(struct xhci_hcd *xhci,
  1091. struct xhci_container_ctx *in_ctx,
  1092. struct xhci_container_ctx *out_ctx,
  1093. unsigned int ep_index)
  1094. {
  1095. struct xhci_ep_ctx *out_ep_ctx;
  1096. struct xhci_ep_ctx *in_ep_ctx;
  1097. out_ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1098. in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  1099. in_ep_ctx->ep_info = out_ep_ctx->ep_info;
  1100. in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
  1101. in_ep_ctx->deq = out_ep_ctx->deq;
  1102. in_ep_ctx->tx_info = out_ep_ctx->tx_info;
  1103. }
  1104. /* Copy output xhci_slot_ctx to the input xhci_slot_ctx.
  1105. * Useful when you want to change one particular aspect of the endpoint and then
  1106. * issue a configure endpoint command. Only the context entries field matters,
  1107. * but we'll copy the whole thing anyway.
  1108. */
  1109. void xhci_slot_copy(struct xhci_hcd *xhci,
  1110. struct xhci_container_ctx *in_ctx,
  1111. struct xhci_container_ctx *out_ctx)
  1112. {
  1113. struct xhci_slot_ctx *in_slot_ctx;
  1114. struct xhci_slot_ctx *out_slot_ctx;
  1115. in_slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1116. out_slot_ctx = xhci_get_slot_ctx(xhci, out_ctx);
  1117. in_slot_ctx->dev_info = out_slot_ctx->dev_info;
  1118. in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
  1119. in_slot_ctx->tt_info = out_slot_ctx->tt_info;
  1120. in_slot_ctx->dev_state = out_slot_ctx->dev_state;
  1121. }
  1122. /* Set up the scratchpad buffer array and scratchpad buffers, if needed. */
  1123. static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
  1124. {
  1125. int i;
  1126. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  1127. int num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
  1128. xhci_dbg(xhci, "Allocating %d scratchpad buffers\n", num_sp);
  1129. if (!num_sp)
  1130. return 0;
  1131. xhci->scratchpad = kzalloc(sizeof(*xhci->scratchpad), flags);
  1132. if (!xhci->scratchpad)
  1133. goto fail_sp;
  1134. xhci->scratchpad->sp_array =
  1135. pci_alloc_consistent(to_pci_dev(dev),
  1136. num_sp * sizeof(u64),
  1137. &xhci->scratchpad->sp_dma);
  1138. if (!xhci->scratchpad->sp_array)
  1139. goto fail_sp2;
  1140. xhci->scratchpad->sp_buffers = kzalloc(sizeof(void *) * num_sp, flags);
  1141. if (!xhci->scratchpad->sp_buffers)
  1142. goto fail_sp3;
  1143. xhci->scratchpad->sp_dma_buffers =
  1144. kzalloc(sizeof(dma_addr_t) * num_sp, flags);
  1145. if (!xhci->scratchpad->sp_dma_buffers)
  1146. goto fail_sp4;
  1147. xhci->dcbaa->dev_context_ptrs[0] = xhci->scratchpad->sp_dma;
  1148. for (i = 0; i < num_sp; i++) {
  1149. dma_addr_t dma;
  1150. void *buf = pci_alloc_consistent(to_pci_dev(dev),
  1151. xhci->page_size, &dma);
  1152. if (!buf)
  1153. goto fail_sp5;
  1154. xhci->scratchpad->sp_array[i] = dma;
  1155. xhci->scratchpad->sp_buffers[i] = buf;
  1156. xhci->scratchpad->sp_dma_buffers[i] = dma;
  1157. }
  1158. return 0;
  1159. fail_sp5:
  1160. for (i = i - 1; i >= 0; i--) {
  1161. pci_free_consistent(to_pci_dev(dev), xhci->page_size,
  1162. xhci->scratchpad->sp_buffers[i],
  1163. xhci->scratchpad->sp_dma_buffers[i]);
  1164. }
  1165. kfree(xhci->scratchpad->sp_dma_buffers);
  1166. fail_sp4:
  1167. kfree(xhci->scratchpad->sp_buffers);
  1168. fail_sp3:
  1169. pci_free_consistent(to_pci_dev(dev), num_sp * sizeof(u64),
  1170. xhci->scratchpad->sp_array,
  1171. xhci->scratchpad->sp_dma);
  1172. fail_sp2:
  1173. kfree(xhci->scratchpad);
  1174. xhci->scratchpad = NULL;
  1175. fail_sp:
  1176. return -ENOMEM;
  1177. }
  1178. static void scratchpad_free(struct xhci_hcd *xhci)
  1179. {
  1180. int num_sp;
  1181. int i;
  1182. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  1183. if (!xhci->scratchpad)
  1184. return;
  1185. num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
  1186. for (i = 0; i < num_sp; i++) {
  1187. pci_free_consistent(pdev, xhci->page_size,
  1188. xhci->scratchpad->sp_buffers[i],
  1189. xhci->scratchpad->sp_dma_buffers[i]);
  1190. }
  1191. kfree(xhci->scratchpad->sp_dma_buffers);
  1192. kfree(xhci->scratchpad->sp_buffers);
  1193. pci_free_consistent(pdev, num_sp * sizeof(u64),
  1194. xhci->scratchpad->sp_array,
  1195. xhci->scratchpad->sp_dma);
  1196. kfree(xhci->scratchpad);
  1197. xhci->scratchpad = NULL;
  1198. }
  1199. struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
  1200. bool allocate_in_ctx, bool allocate_completion,
  1201. gfp_t mem_flags)
  1202. {
  1203. struct xhci_command *command;
  1204. command = kzalloc(sizeof(*command), mem_flags);
  1205. if (!command)
  1206. return NULL;
  1207. if (allocate_in_ctx) {
  1208. command->in_ctx =
  1209. xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT,
  1210. mem_flags);
  1211. if (!command->in_ctx) {
  1212. kfree(command);
  1213. return NULL;
  1214. }
  1215. }
  1216. if (allocate_completion) {
  1217. command->completion =
  1218. kzalloc(sizeof(struct completion), mem_flags);
  1219. if (!command->completion) {
  1220. xhci_free_container_ctx(xhci, command->in_ctx);
  1221. kfree(command);
  1222. return NULL;
  1223. }
  1224. init_completion(command->completion);
  1225. }
  1226. command->status = 0;
  1227. INIT_LIST_HEAD(&command->cmd_list);
  1228. return command;
  1229. }
  1230. void xhci_free_command(struct xhci_hcd *xhci,
  1231. struct xhci_command *command)
  1232. {
  1233. xhci_free_container_ctx(xhci,
  1234. command->in_ctx);
  1235. kfree(command->completion);
  1236. kfree(command);
  1237. }
  1238. void xhci_mem_cleanup(struct xhci_hcd *xhci)
  1239. {
  1240. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  1241. int size;
  1242. int i;
  1243. /* Free the Event Ring Segment Table and the actual Event Ring */
  1244. if (xhci->ir_set) {
  1245. xhci_writel(xhci, 0, &xhci->ir_set->erst_size);
  1246. xhci_write_64(xhci, 0, &xhci->ir_set->erst_base);
  1247. xhci_write_64(xhci, 0, &xhci->ir_set->erst_dequeue);
  1248. }
  1249. size = sizeof(struct xhci_erst_entry)*(xhci->erst.num_entries);
  1250. if (xhci->erst.entries)
  1251. pci_free_consistent(pdev, size,
  1252. xhci->erst.entries, xhci->erst.erst_dma_addr);
  1253. xhci->erst.entries = NULL;
  1254. xhci_dbg(xhci, "Freed ERST\n");
  1255. if (xhci->event_ring)
  1256. xhci_ring_free(xhci, xhci->event_ring);
  1257. xhci->event_ring = NULL;
  1258. xhci_dbg(xhci, "Freed event ring\n");
  1259. xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring);
  1260. if (xhci->cmd_ring)
  1261. xhci_ring_free(xhci, xhci->cmd_ring);
  1262. xhci->cmd_ring = NULL;
  1263. xhci_dbg(xhci, "Freed command ring\n");
  1264. for (i = 1; i < MAX_HC_SLOTS; ++i)
  1265. xhci_free_virt_device(xhci, i);
  1266. if (xhci->segment_pool)
  1267. dma_pool_destroy(xhci->segment_pool);
  1268. xhci->segment_pool = NULL;
  1269. xhci_dbg(xhci, "Freed segment pool\n");
  1270. if (xhci->device_pool)
  1271. dma_pool_destroy(xhci->device_pool);
  1272. xhci->device_pool = NULL;
  1273. xhci_dbg(xhci, "Freed device context pool\n");
  1274. if (xhci->small_streams_pool)
  1275. dma_pool_destroy(xhci->small_streams_pool);
  1276. xhci->small_streams_pool = NULL;
  1277. xhci_dbg(xhci, "Freed small stream array pool\n");
  1278. if (xhci->medium_streams_pool)
  1279. dma_pool_destroy(xhci->medium_streams_pool);
  1280. xhci->medium_streams_pool = NULL;
  1281. xhci_dbg(xhci, "Freed medium stream array pool\n");
  1282. xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
  1283. if (xhci->dcbaa)
  1284. pci_free_consistent(pdev, sizeof(*xhci->dcbaa),
  1285. xhci->dcbaa, xhci->dcbaa->dma);
  1286. xhci->dcbaa = NULL;
  1287. scratchpad_free(xhci);
  1288. xhci->page_size = 0;
  1289. xhci->page_shift = 0;
  1290. }
  1291. static int xhci_test_trb_in_td(struct xhci_hcd *xhci,
  1292. struct xhci_segment *input_seg,
  1293. union xhci_trb *start_trb,
  1294. union xhci_trb *end_trb,
  1295. dma_addr_t input_dma,
  1296. struct xhci_segment *result_seg,
  1297. char *test_name, int test_number)
  1298. {
  1299. unsigned long long start_dma;
  1300. unsigned long long end_dma;
  1301. struct xhci_segment *seg;
  1302. start_dma = xhci_trb_virt_to_dma(input_seg, start_trb);
  1303. end_dma = xhci_trb_virt_to_dma(input_seg, end_trb);
  1304. seg = trb_in_td(input_seg, start_trb, end_trb, input_dma);
  1305. if (seg != result_seg) {
  1306. xhci_warn(xhci, "WARN: %s TRB math test %d failed!\n",
  1307. test_name, test_number);
  1308. xhci_warn(xhci, "Tested TRB math w/ seg %p and "
  1309. "input DMA 0x%llx\n",
  1310. input_seg,
  1311. (unsigned long long) input_dma);
  1312. xhci_warn(xhci, "starting TRB %p (0x%llx DMA), "
  1313. "ending TRB %p (0x%llx DMA)\n",
  1314. start_trb, start_dma,
  1315. end_trb, end_dma);
  1316. xhci_warn(xhci, "Expected seg %p, got seg %p\n",
  1317. result_seg, seg);
  1318. return -1;
  1319. }
  1320. return 0;
  1321. }
  1322. /* TRB math checks for xhci_trb_in_td(), using the command and event rings. */
  1323. static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci, gfp_t mem_flags)
  1324. {
  1325. struct {
  1326. dma_addr_t input_dma;
  1327. struct xhci_segment *result_seg;
  1328. } simple_test_vector [] = {
  1329. /* A zeroed DMA field should fail */
  1330. { 0, NULL },
  1331. /* One TRB before the ring start should fail */
  1332. { xhci->event_ring->first_seg->dma - 16, NULL },
  1333. /* One byte before the ring start should fail */
  1334. { xhci->event_ring->first_seg->dma - 1, NULL },
  1335. /* Starting TRB should succeed */
  1336. { xhci->event_ring->first_seg->dma, xhci->event_ring->first_seg },
  1337. /* Ending TRB should succeed */
  1338. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16,
  1339. xhci->event_ring->first_seg },
  1340. /* One byte after the ring end should fail */
  1341. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16 + 1, NULL },
  1342. /* One TRB after the ring end should fail */
  1343. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT)*16, NULL },
  1344. /* An address of all ones should fail */
  1345. { (dma_addr_t) (~0), NULL },
  1346. };
  1347. struct {
  1348. struct xhci_segment *input_seg;
  1349. union xhci_trb *start_trb;
  1350. union xhci_trb *end_trb;
  1351. dma_addr_t input_dma;
  1352. struct xhci_segment *result_seg;
  1353. } complex_test_vector [] = {
  1354. /* Test feeding a valid DMA address from a different ring */
  1355. { .input_seg = xhci->event_ring->first_seg,
  1356. .start_trb = xhci->event_ring->first_seg->trbs,
  1357. .end_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1358. .input_dma = xhci->cmd_ring->first_seg->dma,
  1359. .result_seg = NULL,
  1360. },
  1361. /* Test feeding a valid end TRB from a different ring */
  1362. { .input_seg = xhci->event_ring->first_seg,
  1363. .start_trb = xhci->event_ring->first_seg->trbs,
  1364. .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1365. .input_dma = xhci->cmd_ring->first_seg->dma,
  1366. .result_seg = NULL,
  1367. },
  1368. /* Test feeding a valid start and end TRB from a different ring */
  1369. { .input_seg = xhci->event_ring->first_seg,
  1370. .start_trb = xhci->cmd_ring->first_seg->trbs,
  1371. .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1372. .input_dma = xhci->cmd_ring->first_seg->dma,
  1373. .result_seg = NULL,
  1374. },
  1375. /* TRB in this ring, but after this TD */
  1376. { .input_seg = xhci->event_ring->first_seg,
  1377. .start_trb = &xhci->event_ring->first_seg->trbs[0],
  1378. .end_trb = &xhci->event_ring->first_seg->trbs[3],
  1379. .input_dma = xhci->event_ring->first_seg->dma + 4*16,
  1380. .result_seg = NULL,
  1381. },
  1382. /* TRB in this ring, but before this TD */
  1383. { .input_seg = xhci->event_ring->first_seg,
  1384. .start_trb = &xhci->event_ring->first_seg->trbs[3],
  1385. .end_trb = &xhci->event_ring->first_seg->trbs[6],
  1386. .input_dma = xhci->event_ring->first_seg->dma + 2*16,
  1387. .result_seg = NULL,
  1388. },
  1389. /* TRB in this ring, but after this wrapped TD */
  1390. { .input_seg = xhci->event_ring->first_seg,
  1391. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  1392. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  1393. .input_dma = xhci->event_ring->first_seg->dma + 2*16,
  1394. .result_seg = NULL,
  1395. },
  1396. /* TRB in this ring, but before this wrapped TD */
  1397. { .input_seg = xhci->event_ring->first_seg,
  1398. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  1399. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  1400. .input_dma = xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 4)*16,
  1401. .result_seg = NULL,
  1402. },
  1403. /* TRB not in this ring, and we have a wrapped TD */
  1404. { .input_seg = xhci->event_ring->first_seg,
  1405. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  1406. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  1407. .input_dma = xhci->cmd_ring->first_seg->dma + 2*16,
  1408. .result_seg = NULL,
  1409. },
  1410. };
  1411. unsigned int num_tests;
  1412. int i, ret;
  1413. num_tests = sizeof(simple_test_vector) / sizeof(simple_test_vector[0]);
  1414. for (i = 0; i < num_tests; i++) {
  1415. ret = xhci_test_trb_in_td(xhci,
  1416. xhci->event_ring->first_seg,
  1417. xhci->event_ring->first_seg->trbs,
  1418. &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1419. simple_test_vector[i].input_dma,
  1420. simple_test_vector[i].result_seg,
  1421. "Simple", i);
  1422. if (ret < 0)
  1423. return ret;
  1424. }
  1425. num_tests = sizeof(complex_test_vector) / sizeof(complex_test_vector[0]);
  1426. for (i = 0; i < num_tests; i++) {
  1427. ret = xhci_test_trb_in_td(xhci,
  1428. complex_test_vector[i].input_seg,
  1429. complex_test_vector[i].start_trb,
  1430. complex_test_vector[i].end_trb,
  1431. complex_test_vector[i].input_dma,
  1432. complex_test_vector[i].result_seg,
  1433. "Complex", i);
  1434. if (ret < 0)
  1435. return ret;
  1436. }
  1437. xhci_dbg(xhci, "TRB math tests passed.\n");
  1438. return 0;
  1439. }
  1440. int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
  1441. {
  1442. dma_addr_t dma;
  1443. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  1444. unsigned int val, val2;
  1445. u64 val_64;
  1446. struct xhci_segment *seg;
  1447. u32 page_size;
  1448. int i;
  1449. page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
  1450. xhci_dbg(xhci, "Supported page size register = 0x%x\n", page_size);
  1451. for (i = 0; i < 16; i++) {
  1452. if ((0x1 & page_size) != 0)
  1453. break;
  1454. page_size = page_size >> 1;
  1455. }
  1456. if (i < 16)
  1457. xhci_dbg(xhci, "Supported page size of %iK\n", (1 << (i+12)) / 1024);
  1458. else
  1459. xhci_warn(xhci, "WARN: no supported page size\n");
  1460. /* Use 4K pages, since that's common and the minimum the HC supports */
  1461. xhci->page_shift = 12;
  1462. xhci->page_size = 1 << xhci->page_shift;
  1463. xhci_dbg(xhci, "HCD page size set to %iK\n", xhci->page_size / 1024);
  1464. /*
  1465. * Program the Number of Device Slots Enabled field in the CONFIG
  1466. * register with the max value of slots the HC can handle.
  1467. */
  1468. val = HCS_MAX_SLOTS(xhci_readl(xhci, &xhci->cap_regs->hcs_params1));
  1469. xhci_dbg(xhci, "// xHC can handle at most %d device slots.\n",
  1470. (unsigned int) val);
  1471. val2 = xhci_readl(xhci, &xhci->op_regs->config_reg);
  1472. val |= (val2 & ~HCS_SLOTS_MASK);
  1473. xhci_dbg(xhci, "// Setting Max device slots reg = 0x%x.\n",
  1474. (unsigned int) val);
  1475. xhci_writel(xhci, val, &xhci->op_regs->config_reg);
  1476. /*
  1477. * Section 5.4.8 - doorbell array must be
  1478. * "physically contiguous and 64-byte (cache line) aligned".
  1479. */
  1480. xhci->dcbaa = pci_alloc_consistent(to_pci_dev(dev),
  1481. sizeof(*xhci->dcbaa), &dma);
  1482. if (!xhci->dcbaa)
  1483. goto fail;
  1484. memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
  1485. xhci->dcbaa->dma = dma;
  1486. xhci_dbg(xhci, "// Device context base array address = 0x%llx (DMA), %p (virt)\n",
  1487. (unsigned long long)xhci->dcbaa->dma, xhci->dcbaa);
  1488. xhci_write_64(xhci, dma, &xhci->op_regs->dcbaa_ptr);
  1489. /*
  1490. * Initialize the ring segment pool. The ring must be a contiguous
  1491. * structure comprised of TRBs. The TRBs must be 16 byte aligned,
  1492. * however, the command ring segment needs 64-byte aligned segments,
  1493. * so we pick the greater alignment need.
  1494. */
  1495. xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
  1496. SEGMENT_SIZE, 64, xhci->page_size);
  1497. /* See Table 46 and Note on Figure 55 */
  1498. xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
  1499. 2112, 64, xhci->page_size);
  1500. if (!xhci->segment_pool || !xhci->device_pool)
  1501. goto fail;
  1502. /* Linear stream context arrays don't have any boundary restrictions,
  1503. * and only need to be 16-byte aligned.
  1504. */
  1505. xhci->small_streams_pool =
  1506. dma_pool_create("xHCI 256 byte stream ctx arrays",
  1507. dev, SMALL_STREAM_ARRAY_SIZE, 16, 0);
  1508. xhci->medium_streams_pool =
  1509. dma_pool_create("xHCI 1KB stream ctx arrays",
  1510. dev, MEDIUM_STREAM_ARRAY_SIZE, 16, 0);
  1511. /* Any stream context array bigger than MEDIUM_STREAM_ARRAY_SIZE
  1512. * will be allocated with pci_alloc_consistent()
  1513. */
  1514. if (!xhci->small_streams_pool || !xhci->medium_streams_pool)
  1515. goto fail;
  1516. /* Set up the command ring to have one segments for now. */
  1517. xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
  1518. if (!xhci->cmd_ring)
  1519. goto fail;
  1520. xhci_dbg(xhci, "Allocated command ring at %p\n", xhci->cmd_ring);
  1521. xhci_dbg(xhci, "First segment DMA is 0x%llx\n",
  1522. (unsigned long long)xhci->cmd_ring->first_seg->dma);
  1523. /* Set the address in the Command Ring Control register */
  1524. val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  1525. val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
  1526. (xhci->cmd_ring->first_seg->dma & (u64) ~CMD_RING_RSVD_BITS) |
  1527. xhci->cmd_ring->cycle_state;
  1528. xhci_dbg(xhci, "// Setting command ring address to 0x%x\n", val);
  1529. xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
  1530. xhci_dbg_cmd_ptrs(xhci);
  1531. val = xhci_readl(xhci, &xhci->cap_regs->db_off);
  1532. val &= DBOFF_MASK;
  1533. xhci_dbg(xhci, "// Doorbell array is located at offset 0x%x"
  1534. " from cap regs base addr\n", val);
  1535. xhci->dba = (void *) xhci->cap_regs + val;
  1536. xhci_dbg_regs(xhci);
  1537. xhci_print_run_regs(xhci);
  1538. /* Set ir_set to interrupt register set 0 */
  1539. xhci->ir_set = (void *) xhci->run_regs->ir_set;
  1540. /*
  1541. * Event ring setup: Allocate a normal ring, but also setup
  1542. * the event ring segment table (ERST). Section 4.9.3.
  1543. */
  1544. xhci_dbg(xhci, "// Allocating event ring\n");
  1545. xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
  1546. if (!xhci->event_ring)
  1547. goto fail;
  1548. if (xhci_check_trb_in_td_math(xhci, flags) < 0)
  1549. goto fail;
  1550. xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev),
  1551. sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma);
  1552. if (!xhci->erst.entries)
  1553. goto fail;
  1554. xhci_dbg(xhci, "// Allocated event ring segment table at 0x%llx\n",
  1555. (unsigned long long)dma);
  1556. memset(xhci->erst.entries, 0, sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS);
  1557. xhci->erst.num_entries = ERST_NUM_SEGS;
  1558. xhci->erst.erst_dma_addr = dma;
  1559. xhci_dbg(xhci, "Set ERST to 0; private num segs = %i, virt addr = %p, dma addr = 0x%llx\n",
  1560. xhci->erst.num_entries,
  1561. xhci->erst.entries,
  1562. (unsigned long long)xhci->erst.erst_dma_addr);
  1563. /* set ring base address and size for each segment table entry */
  1564. for (val = 0, seg = xhci->event_ring->first_seg; val < ERST_NUM_SEGS; val++) {
  1565. struct xhci_erst_entry *entry = &xhci->erst.entries[val];
  1566. entry->seg_addr = seg->dma;
  1567. entry->seg_size = TRBS_PER_SEGMENT;
  1568. entry->rsvd = 0;
  1569. seg = seg->next;
  1570. }
  1571. /* set ERST count with the number of entries in the segment table */
  1572. val = xhci_readl(xhci, &xhci->ir_set->erst_size);
  1573. val &= ERST_SIZE_MASK;
  1574. val |= ERST_NUM_SEGS;
  1575. xhci_dbg(xhci, "// Write ERST size = %i to ir_set 0 (some bits preserved)\n",
  1576. val);
  1577. xhci_writel(xhci, val, &xhci->ir_set->erst_size);
  1578. xhci_dbg(xhci, "// Set ERST entries to point to event ring.\n");
  1579. /* set the segment table base address */
  1580. xhci_dbg(xhci, "// Set ERST base address for ir_set 0 = 0x%llx\n",
  1581. (unsigned long long)xhci->erst.erst_dma_addr);
  1582. val_64 = xhci_read_64(xhci, &xhci->ir_set->erst_base);
  1583. val_64 &= ERST_PTR_MASK;
  1584. val_64 |= (xhci->erst.erst_dma_addr & (u64) ~ERST_PTR_MASK);
  1585. xhci_write_64(xhci, val_64, &xhci->ir_set->erst_base);
  1586. /* Set the event ring dequeue address */
  1587. xhci_set_hc_event_deq(xhci);
  1588. xhci_dbg(xhci, "Wrote ERST address to ir_set 0.\n");
  1589. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  1590. /*
  1591. * XXX: Might need to set the Interrupter Moderation Register to
  1592. * something other than the default (~1ms minimum between interrupts).
  1593. * See section 5.5.1.2.
  1594. */
  1595. init_completion(&xhci->addr_dev);
  1596. for (i = 0; i < MAX_HC_SLOTS; ++i)
  1597. xhci->devs[i] = NULL;
  1598. if (scratchpad_alloc(xhci, flags))
  1599. goto fail;
  1600. return 0;
  1601. fail:
  1602. xhci_warn(xhci, "Couldn't initialize memory\n");
  1603. xhci_mem_cleanup(xhci);
  1604. return -ENOMEM;
  1605. }