xhci-mem.c 68 KB

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