xhci-mem.c 64 KB

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