xhci-mem.c 71 KB

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