xhci-mem.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * xHCI host controller driver
  4. *
  5. * Copyright (C) 2008 Intel Corp.
  6. *
  7. * Author: Sarah Sharp
  8. * Some code borrowed from the Linux EHCI driver.
  9. */
  10. #include <linux/usb.h>
  11. #include <linux/pci.h>
  12. #include <linux/slab.h>
  13. #include <linux/dmapool.h>
  14. #include <linux/dma-mapping.h>
  15. #include "xhci.h"
  16. #include "xhci-trace.h"
  17. #include "xhci-debugfs.h"
  18. /*
  19. * Allocates a generic ring segment from the ring pool, sets the dma address,
  20. * initializes the segment to zero, and sets the private next pointer to NULL.
  21. *
  22. * Section 4.11.1.1:
  23. * "All components of all Command and Transfer TRBs shall be initialized to '0'"
  24. */
  25. static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci,
  26. unsigned int cycle_state,
  27. unsigned int max_packet,
  28. gfp_t flags)
  29. {
  30. struct xhci_segment *seg;
  31. dma_addr_t dma;
  32. int i;
  33. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  34. seg = kzalloc_node(sizeof(*seg), flags, dev_to_node(dev));
  35. if (!seg)
  36. return NULL;
  37. seg->trbs = dma_pool_zalloc(xhci->segment_pool, flags, &dma);
  38. if (!seg->trbs) {
  39. kfree(seg);
  40. return NULL;
  41. }
  42. if (max_packet) {
  43. seg->bounce_buf = kzalloc_node(max_packet, flags,
  44. dev_to_node(dev));
  45. if (!seg->bounce_buf) {
  46. dma_pool_free(xhci->segment_pool, seg->trbs, dma);
  47. kfree(seg);
  48. return NULL;
  49. }
  50. }
  51. /* If the cycle state is 0, set the cycle bit to 1 for all the TRBs */
  52. if (cycle_state == 0) {
  53. for (i = 0; i < TRBS_PER_SEGMENT; i++)
  54. seg->trbs[i].link.control = cpu_to_le32(TRB_CYCLE);
  55. }
  56. seg->dma = dma;
  57. seg->next = NULL;
  58. return seg;
  59. }
  60. void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
  61. {
  62. if (seg->trbs) {
  63. dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma);
  64. seg->trbs = NULL;
  65. }
  66. kfree(seg->bounce_buf);
  67. kfree(seg);
  68. }
  69. EXPORT_SYMBOL_GPL(xhci_segment_free);
  70. void xhci_free_segments_for_ring(struct xhci_hcd *xhci,
  71. struct xhci_segment *first)
  72. {
  73. struct xhci_segment *seg;
  74. seg = first->next;
  75. while (seg != first) {
  76. struct xhci_segment *next = seg->next;
  77. xhci_segment_free(xhci, seg);
  78. seg = next;
  79. }
  80. xhci_segment_free(xhci, first);
  81. }
  82. /*
  83. * Make the prev segment point to the next segment.
  84. *
  85. * Change the last TRB in the prev segment to be a Link TRB which points to the
  86. * DMA address of the next segment. The caller needs to set any Link TRB
  87. * related flags, such as End TRB, Toggle Cycle, and no snoop.
  88. */
  89. void xhci_link_segments(struct xhci_segment *prev,
  90. struct xhci_segment *next,
  91. enum xhci_ring_type type, bool chain_links)
  92. {
  93. u32 val;
  94. if (!prev || !next)
  95. return;
  96. prev->next = next;
  97. if (type != TYPE_EVENT) {
  98. prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr =
  99. cpu_to_le64(next->dma);
  100. /* Set the last TRB in the segment to have a TRB type ID of Link TRB */
  101. val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control);
  102. val &= ~TRB_TYPE_BITMASK;
  103. val |= TRB_TYPE(TRB_LINK);
  104. if (chain_links)
  105. val |= TRB_CHAIN;
  106. prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
  107. }
  108. }
  109. EXPORT_SYMBOL_GPL(xhci_link_segments);
  110. /*
  111. * Link the ring to the new segments.
  112. * Set Toggle Cycle for the new ring if needed.
  113. */
  114. static void xhci_link_rings(struct xhci_hcd *xhci, struct xhci_ring *ring,
  115. struct xhci_segment *first, struct xhci_segment *last,
  116. unsigned int num_segs)
  117. {
  118. struct xhci_segment *next;
  119. bool chain_links;
  120. if (!ring || !first || !last)
  121. return;
  122. /* Set chain bit for 0.95 hosts, and for isoc rings on AMD 0.96 host */
  123. chain_links = !!(xhci_link_trb_quirk(xhci) ||
  124. (ring->type == TYPE_ISOC &&
  125. (xhci->quirks & XHCI_AMD_0x96_HOST)));
  126. next = ring->enq_seg->next;
  127. xhci_link_segments(ring->enq_seg, first, ring->type, chain_links);
  128. xhci_link_segments(last, next, ring->type, chain_links);
  129. ring->num_segs += num_segs;
  130. ring->num_trbs_free += (TRBS_PER_SEGMENT - 1) * num_segs;
  131. if (ring->type != TYPE_EVENT && ring->enq_seg == ring->last_seg) {
  132. ring->last_seg->trbs[TRBS_PER_SEGMENT-1].link.control
  133. &= ~cpu_to_le32(LINK_TOGGLE);
  134. last->trbs[TRBS_PER_SEGMENT-1].link.control
  135. |= cpu_to_le32(LINK_TOGGLE);
  136. ring->last_seg = last;
  137. }
  138. }
  139. /*
  140. * We need a radix tree for mapping physical addresses of TRBs to which stream
  141. * ID they belong to. We need to do this because the host controller won't tell
  142. * us which stream ring the TRB came from. We could store the stream ID in an
  143. * event data TRB, but that doesn't help us for the cancellation case, since the
  144. * endpoint may stop before it reaches that event data TRB.
  145. *
  146. * The radix tree maps the upper portion of the TRB DMA address to a ring
  147. * segment that has the same upper portion of DMA addresses. For example, say I
  148. * have segments of size 1KB, that are always 1KB aligned. A segment may
  149. * start at 0x10c91000 and end at 0x10c913f0. If I use the upper 10 bits, the
  150. * key to the stream ID is 0x43244. I can use the DMA address of the TRB to
  151. * pass the radix tree a key to get the right stream ID:
  152. *
  153. * 0x10c90fff >> 10 = 0x43243
  154. * 0x10c912c0 >> 10 = 0x43244
  155. * 0x10c91400 >> 10 = 0x43245
  156. *
  157. * Obviously, only those TRBs with DMA addresses that are within the segment
  158. * will make the radix tree return the stream ID for that ring.
  159. *
  160. * Caveats for the radix tree:
  161. *
  162. * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an
  163. * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be
  164. * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the
  165. * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit
  166. * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit
  167. * extended systems (where the DMA address can be bigger than 32-bits),
  168. * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that.
  169. */
  170. static int xhci_insert_segment_mapping(struct radix_tree_root *trb_address_map,
  171. struct xhci_ring *ring,
  172. struct xhci_segment *seg,
  173. gfp_t mem_flags)
  174. {
  175. unsigned long key;
  176. int ret;
  177. key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
  178. /* Skip any segments that were already added. */
  179. if (radix_tree_lookup(trb_address_map, key))
  180. return 0;
  181. ret = radix_tree_maybe_preload(mem_flags);
  182. if (ret)
  183. return ret;
  184. ret = radix_tree_insert(trb_address_map,
  185. key, ring);
  186. radix_tree_preload_end();
  187. return ret;
  188. }
  189. static void xhci_remove_segment_mapping(struct radix_tree_root *trb_address_map,
  190. struct xhci_segment *seg)
  191. {
  192. unsigned long key;
  193. key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
  194. if (radix_tree_lookup(trb_address_map, key))
  195. radix_tree_delete(trb_address_map, key);
  196. }
  197. static int xhci_update_stream_segment_mapping(
  198. struct radix_tree_root *trb_address_map,
  199. struct xhci_ring *ring,
  200. struct xhci_segment *first_seg,
  201. struct xhci_segment *last_seg,
  202. gfp_t mem_flags)
  203. {
  204. struct xhci_segment *seg;
  205. struct xhci_segment *failed_seg;
  206. int ret;
  207. if (WARN_ON_ONCE(trb_address_map == NULL))
  208. return 0;
  209. seg = first_seg;
  210. do {
  211. ret = xhci_insert_segment_mapping(trb_address_map,
  212. ring, seg, mem_flags);
  213. if (ret)
  214. goto remove_streams;
  215. if (seg == last_seg)
  216. return 0;
  217. seg = seg->next;
  218. } while (seg != first_seg);
  219. return 0;
  220. remove_streams:
  221. failed_seg = seg;
  222. seg = first_seg;
  223. do {
  224. xhci_remove_segment_mapping(trb_address_map, seg);
  225. if (seg == failed_seg)
  226. return ret;
  227. seg = seg->next;
  228. } while (seg != first_seg);
  229. return ret;
  230. }
  231. void xhci_remove_stream_mapping(struct xhci_ring *ring)
  232. {
  233. struct xhci_segment *seg;
  234. if (WARN_ON_ONCE(ring->trb_address_map == NULL))
  235. return;
  236. seg = ring->first_seg;
  237. do {
  238. xhci_remove_segment_mapping(ring->trb_address_map, seg);
  239. seg = seg->next;
  240. } while (seg != ring->first_seg);
  241. }
  242. EXPORT_SYMBOL_GPL(xhci_remove_stream_mapping);
  243. static int xhci_update_stream_mapping(struct xhci_ring *ring, gfp_t mem_flags)
  244. {
  245. return xhci_update_stream_segment_mapping(ring->trb_address_map, ring,
  246. ring->first_seg, ring->last_seg, mem_flags);
  247. }
  248. /* XXX: Do we need the hcd structure in all these functions? */
  249. void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
  250. {
  251. if (!ring)
  252. return;
  253. trace_xhci_ring_free(ring);
  254. if (ring->first_seg) {
  255. if (ring->type == TYPE_STREAM)
  256. xhci_remove_stream_mapping(ring);
  257. xhci_free_segments_for_ring(xhci, ring->first_seg);
  258. }
  259. kfree(ring);
  260. }
  261. EXPORT_SYMBOL_GPL(xhci_ring_free);
  262. void xhci_initialize_ring_info(struct xhci_ring *ring,
  263. unsigned int cycle_state)
  264. {
  265. /* The ring is empty, so the enqueue pointer == dequeue pointer */
  266. ring->enqueue = ring->first_seg->trbs;
  267. ring->enq_seg = ring->first_seg;
  268. ring->dequeue = ring->enqueue;
  269. ring->deq_seg = ring->first_seg;
  270. /* The ring is initialized to 0. The producer must write 1 to the cycle
  271. * bit to handover ownership of the TRB, so PCS = 1. The consumer must
  272. * compare CCS to the cycle bit to check ownership, so CCS = 1.
  273. *
  274. * New rings are initialized with cycle state equal to 1; if we are
  275. * handling ring expansion, set the cycle state equal to the old ring.
  276. */
  277. ring->cycle_state = cycle_state;
  278. /*
  279. * Each segment has a link TRB, and leave an extra TRB for SW
  280. * accounting purpose
  281. */
  282. ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
  283. }
  284. EXPORT_SYMBOL_GPL(xhci_initialize_ring_info);
  285. /* Allocate segments and link them for a ring */
  286. static int xhci_alloc_segments_for_ring(struct xhci_hcd *xhci,
  287. struct xhci_segment **first, struct xhci_segment **last,
  288. unsigned int num_segs, unsigned int cycle_state,
  289. enum xhci_ring_type type, unsigned int max_packet, gfp_t flags)
  290. {
  291. struct xhci_segment *prev;
  292. bool chain_links;
  293. /* Set chain bit for 0.95 hosts, and for isoc rings on AMD 0.96 host */
  294. chain_links = !!(xhci_link_trb_quirk(xhci) ||
  295. (type == TYPE_ISOC &&
  296. (xhci->quirks & XHCI_AMD_0x96_HOST)));
  297. prev = xhci_segment_alloc(xhci, cycle_state, max_packet, flags);
  298. if (!prev)
  299. return -ENOMEM;
  300. num_segs--;
  301. *first = prev;
  302. while (num_segs > 0) {
  303. struct xhci_segment *next;
  304. next = xhci_segment_alloc(xhci, cycle_state, max_packet, flags);
  305. if (!next) {
  306. prev = *first;
  307. while (prev) {
  308. next = prev->next;
  309. xhci_segment_free(xhci, prev);
  310. prev = next;
  311. }
  312. return -ENOMEM;
  313. }
  314. xhci_link_segments(prev, next, type, chain_links);
  315. prev = next;
  316. num_segs--;
  317. }
  318. xhci_link_segments(prev, *first, type, chain_links);
  319. *last = prev;
  320. return 0;
  321. }
  322. static void xhci_vendor_free_container_ctx(struct xhci_hcd *xhci, struct xhci_container_ctx *ctx)
  323. {
  324. struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
  325. if (ops && ops->free_container_ctx)
  326. ops->free_container_ctx(xhci, ctx);
  327. }
  328. static void xhci_vendor_alloc_container_ctx(struct xhci_hcd *xhci, struct xhci_container_ctx *ctx,
  329. int type, gfp_t flags)
  330. {
  331. struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
  332. if (ops && ops->alloc_container_ctx)
  333. ops->alloc_container_ctx(xhci, ctx, type, flags);
  334. }
  335. static struct xhci_ring *xhci_vendor_alloc_transfer_ring(struct xhci_hcd *xhci,
  336. u32 endpoint_type, enum xhci_ring_type ring_type,
  337. unsigned int max_packet, gfp_t mem_flags)
  338. {
  339. struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
  340. if (ops && ops->alloc_transfer_ring)
  341. return ops->alloc_transfer_ring(xhci, endpoint_type, ring_type,
  342. max_packet, mem_flags);
  343. return 0;
  344. }
  345. void xhci_vendor_free_transfer_ring(struct xhci_hcd *xhci,
  346. struct xhci_virt_device *virt_dev, unsigned int ep_index)
  347. {
  348. struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
  349. if (ops && ops->free_transfer_ring)
  350. ops->free_transfer_ring(xhci, virt_dev, ep_index);
  351. }
  352. bool xhci_vendor_is_usb_offload_enabled(struct xhci_hcd *xhci,
  353. struct xhci_virt_device *virt_dev, unsigned int ep_index)
  354. {
  355. struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
  356. if (ops && ops->is_usb_offload_enabled)
  357. return ops->is_usb_offload_enabled(xhci, virt_dev, ep_index);
  358. return false;
  359. }
  360. /*
  361. * Create a new ring with zero or more segments.
  362. *
  363. * Link each segment together into a ring.
  364. * Set the end flag and the cycle toggle bit on the last segment.
  365. * See section 4.9.1 and figures 15 and 16.
  366. */
  367. struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
  368. unsigned int num_segs, unsigned int cycle_state,
  369. enum xhci_ring_type type, unsigned int max_packet, gfp_t flags)
  370. {
  371. struct xhci_ring *ring;
  372. int ret;
  373. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  374. ring = kzalloc_node(sizeof(*ring), flags, dev_to_node(dev));
  375. if (!ring)
  376. return NULL;
  377. ring->num_segs = num_segs;
  378. ring->bounce_buf_len = max_packet;
  379. INIT_LIST_HEAD(&ring->td_list);
  380. ring->type = type;
  381. if (num_segs == 0)
  382. return ring;
  383. ret = xhci_alloc_segments_for_ring(xhci, &ring->first_seg,
  384. &ring->last_seg, num_segs, cycle_state, type,
  385. max_packet, flags);
  386. if (ret)
  387. goto fail;
  388. /* Only event ring does not use link TRB */
  389. if (type != TYPE_EVENT) {
  390. /* See section 4.9.2.1 and 6.4.4.1 */
  391. ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control |=
  392. cpu_to_le32(LINK_TOGGLE);
  393. }
  394. xhci_initialize_ring_info(ring, cycle_state);
  395. trace_xhci_ring_alloc(ring);
  396. return ring;
  397. fail:
  398. kfree(ring);
  399. return NULL;
  400. }
  401. EXPORT_SYMBOL_GPL(xhci_ring_alloc);
  402. void xhci_free_endpoint_ring(struct xhci_hcd *xhci,
  403. struct xhci_virt_device *virt_dev,
  404. unsigned int ep_index)
  405. {
  406. if (xhci_vendor_is_usb_offload_enabled(xhci, virt_dev, ep_index))
  407. xhci_vendor_free_transfer_ring(xhci, virt_dev, ep_index);
  408. else
  409. xhci_ring_free(xhci, virt_dev->eps[ep_index].ring);
  410. virt_dev->eps[ep_index].ring = NULL;
  411. }
  412. /*
  413. * Expand an existing ring.
  414. * Allocate a new ring which has same segment numbers and link the two rings.
  415. */
  416. int xhci_ring_expansion(struct xhci_hcd *xhci, struct xhci_ring *ring,
  417. unsigned int num_trbs, gfp_t flags)
  418. {
  419. struct xhci_segment *first;
  420. struct xhci_segment *last;
  421. unsigned int num_segs;
  422. unsigned int num_segs_needed;
  423. int ret;
  424. num_segs_needed = (num_trbs + (TRBS_PER_SEGMENT - 1) - 1) /
  425. (TRBS_PER_SEGMENT - 1);
  426. /* Allocate number of segments we needed, or double the ring size */
  427. num_segs = max(ring->num_segs, num_segs_needed);
  428. ret = xhci_alloc_segments_for_ring(xhci, &first, &last,
  429. num_segs, ring->cycle_state, ring->type,
  430. ring->bounce_buf_len, flags);
  431. if (ret)
  432. return -ENOMEM;
  433. if (ring->type == TYPE_STREAM)
  434. ret = xhci_update_stream_segment_mapping(ring->trb_address_map,
  435. ring, first, last, flags);
  436. if (ret) {
  437. struct xhci_segment *next;
  438. do {
  439. next = first->next;
  440. xhci_segment_free(xhci, first);
  441. if (first == last)
  442. break;
  443. first = next;
  444. } while (true);
  445. return ret;
  446. }
  447. xhci_link_rings(xhci, ring, first, last, num_segs);
  448. trace_xhci_ring_expansion(ring);
  449. xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion,
  450. "ring expansion succeed, now has %d segments",
  451. ring->num_segs);
  452. return 0;
  453. }
  454. struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci,
  455. int type, gfp_t flags)
  456. {
  457. struct xhci_container_ctx *ctx;
  458. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  459. struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
  460. if ((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT))
  461. return NULL;
  462. ctx = kzalloc_node(sizeof(*ctx), flags, dev_to_node(dev));
  463. if (!ctx)
  464. return NULL;
  465. ctx->type = type;
  466. ctx->size = HCC_64BYTE_CONTEXT(xhci->hcc_params) ? 2048 : 1024;
  467. if (type == XHCI_CTX_TYPE_INPUT)
  468. ctx->size += CTX_SIZE(xhci->hcc_params);
  469. if (xhci_vendor_is_usb_offload_enabled(xhci, NULL, 0) &&
  470. (ops && ops->alloc_container_ctx))
  471. xhci_vendor_alloc_container_ctx(xhci, ctx, type, flags);
  472. else
  473. ctx->bytes = dma_pool_zalloc(xhci->device_pool, flags, &ctx->dma);
  474. if (!ctx->bytes) {
  475. kfree(ctx);
  476. return NULL;
  477. }
  478. return ctx;
  479. }
  480. void xhci_free_container_ctx(struct xhci_hcd *xhci,
  481. struct xhci_container_ctx *ctx)
  482. {
  483. struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
  484. if (!ctx)
  485. return;
  486. if (xhci_vendor_is_usb_offload_enabled(xhci, NULL, 0) &&
  487. (ops && ops->free_container_ctx))
  488. xhci_vendor_free_container_ctx(xhci, ctx);
  489. else
  490. dma_pool_free(xhci->device_pool, ctx->bytes, ctx->dma);
  491. kfree(ctx);
  492. }
  493. struct xhci_input_control_ctx *xhci_get_input_control_ctx(
  494. struct xhci_container_ctx *ctx)
  495. {
  496. if (ctx->type != XHCI_CTX_TYPE_INPUT)
  497. return NULL;
  498. return (struct xhci_input_control_ctx *)ctx->bytes;
  499. }
  500. struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_hcd *xhci,
  501. struct xhci_container_ctx *ctx)
  502. {
  503. if (ctx->type == XHCI_CTX_TYPE_DEVICE)
  504. return (struct xhci_slot_ctx *)ctx->bytes;
  505. return (struct xhci_slot_ctx *)
  506. (ctx->bytes + CTX_SIZE(xhci->hcc_params));
  507. }
  508. EXPORT_SYMBOL_GPL(xhci_get_slot_ctx);
  509. struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci,
  510. struct xhci_container_ctx *ctx,
  511. unsigned int ep_index)
  512. {
  513. /* increment ep index by offset of start of ep ctx array */
  514. ep_index++;
  515. if (ctx->type == XHCI_CTX_TYPE_INPUT)
  516. ep_index++;
  517. return (struct xhci_ep_ctx *)
  518. (ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params)));
  519. }
  520. EXPORT_SYMBOL_GPL(xhci_get_ep_ctx);
  521. /***************** Streams structures manipulation *************************/
  522. static void xhci_free_stream_ctx(struct xhci_hcd *xhci,
  523. unsigned int num_stream_ctxs,
  524. struct xhci_stream_ctx *stream_ctx, dma_addr_t dma)
  525. {
  526. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  527. size_t size = sizeof(struct xhci_stream_ctx) * num_stream_ctxs;
  528. if (size > MEDIUM_STREAM_ARRAY_SIZE)
  529. dma_free_coherent(dev, size,
  530. stream_ctx, dma);
  531. else if (size <= SMALL_STREAM_ARRAY_SIZE)
  532. return dma_pool_free(xhci->small_streams_pool,
  533. stream_ctx, dma);
  534. else
  535. return dma_pool_free(xhci->medium_streams_pool,
  536. stream_ctx, dma);
  537. }
  538. /*
  539. * The stream context array for each endpoint with bulk streams enabled can
  540. * vary in size, based on:
  541. * - how many streams the endpoint supports,
  542. * - the maximum primary stream array size the host controller supports,
  543. * - and how many streams the device driver asks for.
  544. *
  545. * The stream context array must be a power of 2, and can be as small as
  546. * 64 bytes or as large as 1MB.
  547. */
  548. static struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci,
  549. unsigned int num_stream_ctxs, dma_addr_t *dma,
  550. gfp_t mem_flags)
  551. {
  552. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  553. size_t size = sizeof(struct xhci_stream_ctx) * num_stream_ctxs;
  554. if (size > MEDIUM_STREAM_ARRAY_SIZE)
  555. return dma_alloc_coherent(dev, size,
  556. dma, mem_flags);
  557. else if (size <= SMALL_STREAM_ARRAY_SIZE)
  558. return dma_pool_alloc(xhci->small_streams_pool,
  559. mem_flags, dma);
  560. else
  561. return dma_pool_alloc(xhci->medium_streams_pool,
  562. mem_flags, dma);
  563. }
  564. struct xhci_ring *xhci_dma_to_transfer_ring(
  565. struct xhci_virt_ep *ep,
  566. u64 address)
  567. {
  568. if (ep->ep_state & EP_HAS_STREAMS)
  569. return radix_tree_lookup(&ep->stream_info->trb_address_map,
  570. address >> TRB_SEGMENT_SHIFT);
  571. return ep->ring;
  572. }
  573. /*
  574. * Change an endpoint's internal structure so it supports stream IDs. The
  575. * number of requested streams includes stream 0, which cannot be used by device
  576. * drivers.
  577. *
  578. * The number of stream contexts in the stream context array may be bigger than
  579. * the number of streams the driver wants to use. This is because the number of
  580. * stream context array entries must be a power of two.
  581. */
  582. struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
  583. unsigned int num_stream_ctxs,
  584. unsigned int num_streams,
  585. unsigned int max_packet, gfp_t mem_flags)
  586. {
  587. struct xhci_stream_info *stream_info;
  588. u32 cur_stream;
  589. struct xhci_ring *cur_ring;
  590. u64 addr;
  591. int ret;
  592. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  593. xhci_dbg(xhci, "Allocating %u streams and %u "
  594. "stream context array entries.\n",
  595. num_streams, num_stream_ctxs);
  596. if (xhci->cmd_ring_reserved_trbs == MAX_RSVD_CMD_TRBS) {
  597. xhci_dbg(xhci, "Command ring has no reserved TRBs available\n");
  598. return NULL;
  599. }
  600. xhci->cmd_ring_reserved_trbs++;
  601. stream_info = kzalloc_node(sizeof(*stream_info), mem_flags,
  602. dev_to_node(dev));
  603. if (!stream_info)
  604. goto cleanup_trbs;
  605. stream_info->num_streams = num_streams;
  606. stream_info->num_stream_ctxs = num_stream_ctxs;
  607. /* Initialize the array of virtual pointers to stream rings. */
  608. stream_info->stream_rings = kcalloc_node(
  609. num_streams, sizeof(struct xhci_ring *), mem_flags,
  610. dev_to_node(dev));
  611. if (!stream_info->stream_rings)
  612. goto cleanup_info;
  613. /* Initialize the array of DMA addresses for stream rings for the HW. */
  614. stream_info->stream_ctx_array = xhci_alloc_stream_ctx(xhci,
  615. num_stream_ctxs, &stream_info->ctx_array_dma,
  616. mem_flags);
  617. if (!stream_info->stream_ctx_array)
  618. goto cleanup_ring_array;
  619. memset(stream_info->stream_ctx_array, 0,
  620. sizeof(struct xhci_stream_ctx)*num_stream_ctxs);
  621. /* Allocate everything needed to free the stream rings later */
  622. stream_info->free_streams_command =
  623. xhci_alloc_command_with_ctx(xhci, true, mem_flags);
  624. if (!stream_info->free_streams_command)
  625. goto cleanup_ctx;
  626. INIT_RADIX_TREE(&stream_info->trb_address_map, GFP_ATOMIC);
  627. /* Allocate rings for all the streams that the driver will use,
  628. * and add their segment DMA addresses to the radix tree.
  629. * Stream 0 is reserved.
  630. */
  631. for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
  632. stream_info->stream_rings[cur_stream] =
  633. xhci_ring_alloc(xhci, 2, 1, TYPE_STREAM, max_packet,
  634. mem_flags);
  635. cur_ring = stream_info->stream_rings[cur_stream];
  636. if (!cur_ring)
  637. goto cleanup_rings;
  638. cur_ring->stream_id = cur_stream;
  639. cur_ring->trb_address_map = &stream_info->trb_address_map;
  640. /* Set deq ptr, cycle bit, and stream context type */
  641. addr = cur_ring->first_seg->dma |
  642. SCT_FOR_CTX(SCT_PRI_TR) |
  643. cur_ring->cycle_state;
  644. stream_info->stream_ctx_array[cur_stream].stream_ring =
  645. cpu_to_le64(addr);
  646. xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n",
  647. cur_stream, (unsigned long long) addr);
  648. ret = xhci_update_stream_mapping(cur_ring, mem_flags);
  649. if (ret) {
  650. xhci_ring_free(xhci, cur_ring);
  651. stream_info->stream_rings[cur_stream] = NULL;
  652. goto cleanup_rings;
  653. }
  654. }
  655. /* Leave the other unused stream ring pointers in the stream context
  656. * array initialized to zero. This will cause the xHC to give us an
  657. * error if the device asks for a stream ID we don't have setup (if it
  658. * was any other way, the host controller would assume the ring is
  659. * "empty" and wait forever for data to be queued to that stream ID).
  660. */
  661. return stream_info;
  662. cleanup_rings:
  663. for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
  664. cur_ring = stream_info->stream_rings[cur_stream];
  665. if (cur_ring) {
  666. xhci_ring_free(xhci, cur_ring);
  667. stream_info->stream_rings[cur_stream] = NULL;
  668. }
  669. }
  670. xhci_free_command(xhci, stream_info->free_streams_command);
  671. cleanup_ctx:
  672. xhci_free_stream_ctx(xhci,
  673. stream_info->num_stream_ctxs,
  674. stream_info->stream_ctx_array,
  675. stream_info->ctx_array_dma);
  676. cleanup_ring_array:
  677. kfree(stream_info->stream_rings);
  678. cleanup_info:
  679. kfree(stream_info);
  680. cleanup_trbs:
  681. xhci->cmd_ring_reserved_trbs--;
  682. return NULL;
  683. }
  684. /*
  685. * Sets the MaxPStreams field and the Linear Stream Array field.
  686. * Sets the dequeue pointer to the stream context array.
  687. */
  688. void xhci_setup_streams_ep_input_ctx(struct xhci_hcd *xhci,
  689. struct xhci_ep_ctx *ep_ctx,
  690. struct xhci_stream_info *stream_info)
  691. {
  692. u32 max_primary_streams;
  693. /* MaxPStreams is the number of stream context array entries, not the
  694. * number we're actually using. Must be in 2^(MaxPstreams + 1) format.
  695. * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc.
  696. */
  697. max_primary_streams = fls(stream_info->num_stream_ctxs) - 2;
  698. xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
  699. "Setting number of stream ctx array entries to %u",
  700. 1 << (max_primary_streams + 1));
  701. ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK);
  702. ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams)
  703. | EP_HAS_LSA);
  704. ep_ctx->deq = cpu_to_le64(stream_info->ctx_array_dma);
  705. }
  706. /*
  707. * Sets the MaxPStreams field and the Linear Stream Array field to 0.
  708. * Reinstalls the "normal" endpoint ring (at its previous dequeue mark,
  709. * not at the beginning of the ring).
  710. */
  711. void xhci_setup_no_streams_ep_input_ctx(struct xhci_ep_ctx *ep_ctx,
  712. struct xhci_virt_ep *ep)
  713. {
  714. dma_addr_t addr;
  715. ep_ctx->ep_info &= cpu_to_le32(~(EP_MAXPSTREAMS_MASK | EP_HAS_LSA));
  716. addr = xhci_trb_virt_to_dma(ep->ring->deq_seg, ep->ring->dequeue);
  717. ep_ctx->deq = cpu_to_le64(addr | ep->ring->cycle_state);
  718. }
  719. /* Frees all stream contexts associated with the endpoint,
  720. *
  721. * Caller should fix the endpoint context streams fields.
  722. */
  723. void xhci_free_stream_info(struct xhci_hcd *xhci,
  724. struct xhci_stream_info *stream_info)
  725. {
  726. int cur_stream;
  727. struct xhci_ring *cur_ring;
  728. if (!stream_info)
  729. return;
  730. for (cur_stream = 1; cur_stream < stream_info->num_streams;
  731. cur_stream++) {
  732. cur_ring = stream_info->stream_rings[cur_stream];
  733. if (cur_ring) {
  734. xhci_ring_free(xhci, cur_ring);
  735. stream_info->stream_rings[cur_stream] = NULL;
  736. }
  737. }
  738. xhci_free_command(xhci, stream_info->free_streams_command);
  739. xhci->cmd_ring_reserved_trbs--;
  740. if (stream_info->stream_ctx_array)
  741. xhci_free_stream_ctx(xhci,
  742. stream_info->num_stream_ctxs,
  743. stream_info->stream_ctx_array,
  744. stream_info->ctx_array_dma);
  745. kfree(stream_info->stream_rings);
  746. kfree(stream_info);
  747. }
  748. /***************** Device context manipulation *************************/
  749. static void xhci_free_tt_info(struct xhci_hcd *xhci,
  750. struct xhci_virt_device *virt_dev,
  751. int slot_id)
  752. {
  753. struct list_head *tt_list_head;
  754. struct xhci_tt_bw_info *tt_info, *next;
  755. bool slot_found = false;
  756. /* If the device never made it past the Set Address stage,
  757. * it may not have the real_port set correctly.
  758. */
  759. if (virt_dev->real_port == 0 ||
  760. virt_dev->real_port > HCS_MAX_PORTS(xhci->hcs_params1)) {
  761. xhci_dbg(xhci, "Bad real port.\n");
  762. return;
  763. }
  764. tt_list_head = &(xhci->rh_bw[virt_dev->real_port - 1].tts);
  765. list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
  766. /* Multi-TT hubs will have more than one entry */
  767. if (tt_info->slot_id == slot_id) {
  768. slot_found = true;
  769. list_del(&tt_info->tt_list);
  770. kfree(tt_info);
  771. } else if (slot_found) {
  772. break;
  773. }
  774. }
  775. }
  776. int xhci_alloc_tt_info(struct xhci_hcd *xhci,
  777. struct xhci_virt_device *virt_dev,
  778. struct usb_device *hdev,
  779. struct usb_tt *tt, gfp_t mem_flags)
  780. {
  781. struct xhci_tt_bw_info *tt_info;
  782. unsigned int num_ports;
  783. int i, j;
  784. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  785. if (!tt->multi)
  786. num_ports = 1;
  787. else
  788. num_ports = hdev->maxchild;
  789. for (i = 0; i < num_ports; i++, tt_info++) {
  790. struct xhci_interval_bw_table *bw_table;
  791. tt_info = kzalloc_node(sizeof(*tt_info), mem_flags,
  792. dev_to_node(dev));
  793. if (!tt_info)
  794. goto free_tts;
  795. INIT_LIST_HEAD(&tt_info->tt_list);
  796. list_add(&tt_info->tt_list,
  797. &xhci->rh_bw[virt_dev->real_port - 1].tts);
  798. tt_info->slot_id = virt_dev->udev->slot_id;
  799. if (tt->multi)
  800. tt_info->ttport = i+1;
  801. bw_table = &tt_info->bw_table;
  802. for (j = 0; j < XHCI_MAX_INTERVAL; j++)
  803. INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints);
  804. }
  805. return 0;
  806. free_tts:
  807. xhci_free_tt_info(xhci, virt_dev, virt_dev->udev->slot_id);
  808. return -ENOMEM;
  809. }
  810. /* All the xhci_tds in the ring's TD list should be freed at this point.
  811. * Should be called with xhci->lock held if there is any chance the TT lists
  812. * will be manipulated by the configure endpoint, allocate device, or update
  813. * hub functions while this function is removing the TT entries from the list.
  814. */
  815. void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
  816. {
  817. struct xhci_virt_device *dev;
  818. int i;
  819. int old_active_eps = 0;
  820. /* Slot ID 0 is reserved */
  821. if (slot_id == 0 || !xhci->devs[slot_id])
  822. return;
  823. dev = xhci->devs[slot_id];
  824. xhci->dcbaa->dev_context_ptrs[slot_id] = 0;
  825. if (!dev)
  826. return;
  827. trace_xhci_free_virt_device(dev);
  828. if (dev->tt_info)
  829. old_active_eps = dev->tt_info->active_eps;
  830. for (i = 0; i < 31; i++) {
  831. if (dev->eps[i].ring)
  832. xhci_free_endpoint_ring(xhci, dev, i);
  833. if (dev->eps[i].stream_info)
  834. xhci_free_stream_info(xhci,
  835. dev->eps[i].stream_info);
  836. /*
  837. * Endpoints are normally deleted from the bandwidth list when
  838. * endpoints are dropped, before device is freed.
  839. * If host is dying or being removed then endpoints aren't
  840. * dropped cleanly, so delete the endpoint from list here.
  841. * Only applicable for hosts with software bandwidth checking.
  842. */
  843. if (!list_empty(&dev->eps[i].bw_endpoint_list)) {
  844. list_del_init(&dev->eps[i].bw_endpoint_list);
  845. xhci_dbg(xhci, "Slot %u endpoint %u not removed from BW list!\n",
  846. slot_id, i);
  847. }
  848. }
  849. /* If this is a hub, free the TT(s) from the TT list */
  850. xhci_free_tt_info(xhci, dev, slot_id);
  851. /* If necessary, update the number of active TTs on this root port */
  852. xhci_update_tt_active_eps(xhci, dev, old_active_eps);
  853. if (dev->in_ctx)
  854. xhci_free_container_ctx(xhci, dev->in_ctx);
  855. if (dev->out_ctx)
  856. xhci_free_container_ctx(xhci, dev->out_ctx);
  857. if (dev->udev && dev->udev->slot_id)
  858. dev->udev->slot_id = 0;
  859. kfree(xhci->devs[slot_id]);
  860. xhci->devs[slot_id] = NULL;
  861. }
  862. /*
  863. * Free a virt_device structure.
  864. * If the virt_device added a tt_info (a hub) and has children pointing to
  865. * that tt_info, then free the child first. Recursive.
  866. * We can't rely on udev at this point to find child-parent relationships.
  867. */
  868. static void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
  869. {
  870. struct xhci_virt_device *vdev;
  871. struct list_head *tt_list_head;
  872. struct xhci_tt_bw_info *tt_info, *next;
  873. int i;
  874. vdev = xhci->devs[slot_id];
  875. if (!vdev)
  876. return;
  877. if (vdev->real_port == 0 ||
  878. vdev->real_port > HCS_MAX_PORTS(xhci->hcs_params1)) {
  879. xhci_dbg(xhci, "Bad vdev->real_port.\n");
  880. goto out;
  881. }
  882. tt_list_head = &(xhci->rh_bw[vdev->real_port - 1].tts);
  883. list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
  884. /* is this a hub device that added a tt_info to the tts list */
  885. if (tt_info->slot_id == slot_id) {
  886. /* are any devices using this tt_info? */
  887. for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
  888. vdev = xhci->devs[i];
  889. if (vdev && (vdev->tt_info == tt_info))
  890. xhci_free_virt_devices_depth_first(
  891. xhci, i);
  892. }
  893. }
  894. }
  895. out:
  896. /* we are now at a leaf device */
  897. xhci_debugfs_remove_slot(xhci, slot_id);
  898. xhci_free_virt_device(xhci, slot_id);
  899. }
  900. int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
  901. struct usb_device *udev, gfp_t flags)
  902. {
  903. struct xhci_virt_device *dev;
  904. int i;
  905. /* Slot ID 0 is reserved */
  906. if (slot_id == 0 || xhci->devs[slot_id]) {
  907. xhci_warn(xhci, "Bad Slot ID %d\n", slot_id);
  908. return 0;
  909. }
  910. dev = kzalloc(sizeof(*dev), flags);
  911. if (!dev)
  912. return 0;
  913. dev->slot_id = slot_id;
  914. /* Allocate the (output) device context that will be used in the HC. */
  915. dev->out_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_DEVICE, flags);
  916. if (!dev->out_ctx)
  917. goto fail;
  918. xhci_dbg(xhci, "Slot %d output ctx = 0x%llx (dma)\n", slot_id,
  919. (unsigned long long)dev->out_ctx->dma);
  920. /* Allocate the (input) device context for address device command */
  921. dev->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, flags);
  922. if (!dev->in_ctx)
  923. goto fail;
  924. xhci_dbg(xhci, "Slot %d input ctx = 0x%llx (dma)\n", slot_id,
  925. (unsigned long long)dev->in_ctx->dma);
  926. /* Initialize the cancellation and bandwidth list for each ep */
  927. for (i = 0; i < 31; i++) {
  928. dev->eps[i].ep_index = i;
  929. dev->eps[i].vdev = dev;
  930. dev->eps[i].xhci = xhci;
  931. INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list);
  932. INIT_LIST_HEAD(&dev->eps[i].bw_endpoint_list);
  933. }
  934. /* Allocate endpoint 0 ring */
  935. dev->eps[0].ring = xhci_ring_alloc(xhci, 2, 1, TYPE_CTRL, 0, flags);
  936. if (!dev->eps[0].ring)
  937. goto fail;
  938. dev->udev = udev;
  939. /* Point to output device context in dcbaa. */
  940. xhci->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(dev->out_ctx->dma);
  941. xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n",
  942. slot_id,
  943. &xhci->dcbaa->dev_context_ptrs[slot_id],
  944. le64_to_cpu(xhci->dcbaa->dev_context_ptrs[slot_id]));
  945. trace_xhci_alloc_virt_device(dev);
  946. xhci->devs[slot_id] = dev;
  947. return 1;
  948. fail:
  949. if (dev->in_ctx)
  950. xhci_free_container_ctx(xhci, dev->in_ctx);
  951. if (dev->out_ctx)
  952. xhci_free_container_ctx(xhci, dev->out_ctx);
  953. kfree(dev);
  954. return 0;
  955. }
  956. void xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd *xhci,
  957. struct usb_device *udev)
  958. {
  959. struct xhci_virt_device *virt_dev;
  960. struct xhci_ep_ctx *ep0_ctx;
  961. struct xhci_ring *ep_ring;
  962. virt_dev = xhci->devs[udev->slot_id];
  963. ep0_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, 0);
  964. ep_ring = virt_dev->eps[0].ring;
  965. /*
  966. * FIXME we don't keep track of the dequeue pointer very well after a
  967. * Set TR dequeue pointer, so we're setting the dequeue pointer of the
  968. * host to our enqueue pointer. This should only be called after a
  969. * configured device has reset, so all control transfers should have
  970. * been completed or cancelled before the reset.
  971. */
  972. ep0_ctx->deq = cpu_to_le64(xhci_trb_virt_to_dma(ep_ring->enq_seg,
  973. ep_ring->enqueue)
  974. | ep_ring->cycle_state);
  975. }
  976. /*
  977. * The xHCI roothub may have ports of differing speeds in any order in the port
  978. * status registers.
  979. *
  980. * The xHCI hardware wants to know the roothub port number that the USB device
  981. * is attached to (or the roothub port its ancestor hub is attached to). All we
  982. * know is the index of that port under either the USB 2.0 or the USB 3.0
  983. * roothub, but that doesn't give us the real index into the HW port status
  984. * registers. Call xhci_find_raw_port_number() to get real index.
  985. */
  986. static u32 xhci_find_real_port_number(struct xhci_hcd *xhci,
  987. struct usb_device *udev)
  988. {
  989. struct usb_device *top_dev;
  990. struct usb_hcd *hcd;
  991. if (udev->speed >= USB_SPEED_SUPER)
  992. hcd = xhci_get_usb3_hcd(xhci);
  993. else
  994. hcd = xhci->main_hcd;
  995. for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
  996. top_dev = top_dev->parent)
  997. /* Found device below root hub */;
  998. return xhci_find_raw_port_number(hcd, top_dev->portnum);
  999. }
  1000. /* Setup an xHCI virtual device for a Set Address command */
  1001. int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev)
  1002. {
  1003. struct xhci_virt_device *dev;
  1004. struct xhci_ep_ctx *ep0_ctx;
  1005. struct xhci_slot_ctx *slot_ctx;
  1006. u32 port_num;
  1007. u32 max_packets;
  1008. struct usb_device *top_dev;
  1009. dev = xhci->devs[udev->slot_id];
  1010. /* Slot ID 0 is reserved */
  1011. if (udev->slot_id == 0 || !dev) {
  1012. xhci_warn(xhci, "Slot ID %d is not assigned to this device\n",
  1013. udev->slot_id);
  1014. return -EINVAL;
  1015. }
  1016. ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0);
  1017. slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx);
  1018. /* 3) Only the control endpoint is valid - one endpoint context */
  1019. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1) | udev->route);
  1020. switch (udev->speed) {
  1021. case USB_SPEED_SUPER_PLUS:
  1022. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SSP);
  1023. max_packets = MAX_PACKET(512);
  1024. break;
  1025. case USB_SPEED_SUPER:
  1026. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
  1027. max_packets = MAX_PACKET(512);
  1028. break;
  1029. case USB_SPEED_HIGH:
  1030. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
  1031. max_packets = MAX_PACKET(64);
  1032. break;
  1033. /* USB core guesses at a 64-byte max packet first for FS devices */
  1034. case USB_SPEED_FULL:
  1035. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
  1036. max_packets = MAX_PACKET(64);
  1037. break;
  1038. case USB_SPEED_LOW:
  1039. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS);
  1040. max_packets = MAX_PACKET(8);
  1041. break;
  1042. case USB_SPEED_WIRELESS:
  1043. xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
  1044. return -EINVAL;
  1045. default:
  1046. /* Speed was set earlier, this shouldn't happen. */
  1047. return -EINVAL;
  1048. }
  1049. /* Find the root hub port this device is under */
  1050. port_num = xhci_find_real_port_number(xhci, udev);
  1051. if (!port_num)
  1052. return -EINVAL;
  1053. slot_ctx->dev_info2 |= cpu_to_le32(ROOT_HUB_PORT(port_num));
  1054. /* Set the port number in the virtual_device to the faked port number */
  1055. for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
  1056. top_dev = top_dev->parent)
  1057. /* Found device below root hub */;
  1058. dev->fake_port = top_dev->portnum;
  1059. dev->real_port = port_num;
  1060. xhci_dbg(xhci, "Set root hub portnum to %d\n", port_num);
  1061. xhci_dbg(xhci, "Set fake root hub portnum to %d\n", dev->fake_port);
  1062. /* Find the right bandwidth table that this device will be a part of.
  1063. * If this is a full speed device attached directly to a root port (or a
  1064. * decendent of one), it counts as a primary bandwidth domain, not a
  1065. * secondary bandwidth domain under a TT. An xhci_tt_info structure
  1066. * will never be created for the HS root hub.
  1067. */
  1068. if (!udev->tt || !udev->tt->hub->parent) {
  1069. dev->bw_table = &xhci->rh_bw[port_num - 1].bw_table;
  1070. } else {
  1071. struct xhci_root_port_bw_info *rh_bw;
  1072. struct xhci_tt_bw_info *tt_bw;
  1073. rh_bw = &xhci->rh_bw[port_num - 1];
  1074. /* Find the right TT. */
  1075. list_for_each_entry(tt_bw, &rh_bw->tts, tt_list) {
  1076. if (tt_bw->slot_id != udev->tt->hub->slot_id)
  1077. continue;
  1078. if (!dev->udev->tt->multi ||
  1079. (udev->tt->multi &&
  1080. tt_bw->ttport == dev->udev->ttport)) {
  1081. dev->bw_table = &tt_bw->bw_table;
  1082. dev->tt_info = tt_bw;
  1083. break;
  1084. }
  1085. }
  1086. if (!dev->tt_info)
  1087. xhci_warn(xhci, "WARN: Didn't find a matching TT\n");
  1088. }
  1089. /* Is this a LS/FS device under an external HS hub? */
  1090. if (udev->tt && udev->tt->hub->parent) {
  1091. slot_ctx->tt_info = cpu_to_le32(udev->tt->hub->slot_id |
  1092. (udev->ttport << 8));
  1093. if (udev->tt->multi)
  1094. slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
  1095. }
  1096. xhci_dbg(xhci, "udev->tt = %p\n", udev->tt);
  1097. xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport);
  1098. /* Step 4 - ring already allocated */
  1099. /* Step 5 */
  1100. ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP));
  1101. /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
  1102. ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3) |
  1103. max_packets);
  1104. ep0_ctx->deq = cpu_to_le64(dev->eps[0].ring->first_seg->dma |
  1105. dev->eps[0].ring->cycle_state);
  1106. trace_xhci_setup_addressable_virt_device(dev);
  1107. /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
  1108. return 0;
  1109. }
  1110. /*
  1111. * Convert interval expressed as 2^(bInterval - 1) == interval into
  1112. * straight exponent value 2^n == interval.
  1113. *
  1114. */
  1115. static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
  1116. struct usb_host_endpoint *ep)
  1117. {
  1118. unsigned int interval;
  1119. interval = clamp_val(ep->desc.bInterval, 1, 16) - 1;
  1120. if (interval != ep->desc.bInterval - 1)
  1121. dev_warn(&udev->dev,
  1122. "ep %#x - rounding interval to %d %sframes\n",
  1123. ep->desc.bEndpointAddress,
  1124. 1 << interval,
  1125. udev->speed == USB_SPEED_FULL ? "" : "micro");
  1126. if (udev->speed == USB_SPEED_FULL) {
  1127. /*
  1128. * Full speed isoc endpoints specify interval in frames,
  1129. * not microframes. We are using microframes everywhere,
  1130. * so adjust accordingly.
  1131. */
  1132. interval += 3; /* 1 frame = 2^3 uframes */
  1133. }
  1134. return interval;
  1135. }
  1136. /*
  1137. * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
  1138. * microframes, rounded down to nearest power of 2.
  1139. */
  1140. static unsigned int xhci_microframes_to_exponent(struct usb_device *udev,
  1141. struct usb_host_endpoint *ep, unsigned int desc_interval,
  1142. unsigned int min_exponent, unsigned int max_exponent)
  1143. {
  1144. unsigned int interval;
  1145. interval = fls(desc_interval) - 1;
  1146. interval = clamp_val(interval, min_exponent, max_exponent);
  1147. if ((1 << interval) != desc_interval)
  1148. dev_dbg(&udev->dev,
  1149. "ep %#x - rounding interval to %d microframes, ep desc says %d microframes\n",
  1150. ep->desc.bEndpointAddress,
  1151. 1 << interval,
  1152. desc_interval);
  1153. return interval;
  1154. }
  1155. static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
  1156. struct usb_host_endpoint *ep)
  1157. {
  1158. if (ep->desc.bInterval == 0)
  1159. return 0;
  1160. return xhci_microframes_to_exponent(udev, ep,
  1161. ep->desc.bInterval, 0, 15);
  1162. }
  1163. static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
  1164. struct usb_host_endpoint *ep)
  1165. {
  1166. return xhci_microframes_to_exponent(udev, ep,
  1167. ep->desc.bInterval * 8, 3, 10);
  1168. }
  1169. /* Return the polling or NAK interval.
  1170. *
  1171. * The polling interval is expressed in "microframes". If xHCI's Interval field
  1172. * is set to N, it will service the endpoint every 2^(Interval)*125us.
  1173. *
  1174. * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
  1175. * is set to 0.
  1176. */
  1177. static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
  1178. struct usb_host_endpoint *ep)
  1179. {
  1180. unsigned int interval = 0;
  1181. switch (udev->speed) {
  1182. case USB_SPEED_HIGH:
  1183. /* Max NAK rate */
  1184. if (usb_endpoint_xfer_control(&ep->desc) ||
  1185. usb_endpoint_xfer_bulk(&ep->desc)) {
  1186. interval = xhci_parse_microframe_interval(udev, ep);
  1187. break;
  1188. }
  1189. fallthrough; /* SS and HS isoc/int have same decoding */
  1190. case USB_SPEED_SUPER_PLUS:
  1191. case USB_SPEED_SUPER:
  1192. if (usb_endpoint_xfer_int(&ep->desc) ||
  1193. usb_endpoint_xfer_isoc(&ep->desc)) {
  1194. interval = xhci_parse_exponent_interval(udev, ep);
  1195. }
  1196. break;
  1197. case USB_SPEED_FULL:
  1198. if (usb_endpoint_xfer_isoc(&ep->desc)) {
  1199. interval = xhci_parse_exponent_interval(udev, ep);
  1200. break;
  1201. }
  1202. /*
  1203. * Fall through for interrupt endpoint interval decoding
  1204. * since it uses the same rules as low speed interrupt
  1205. * endpoints.
  1206. */
  1207. fallthrough;
  1208. case USB_SPEED_LOW:
  1209. if (usb_endpoint_xfer_int(&ep->desc) ||
  1210. usb_endpoint_xfer_isoc(&ep->desc)) {
  1211. interval = xhci_parse_frame_interval(udev, ep);
  1212. }
  1213. break;
  1214. default:
  1215. BUG();
  1216. }
  1217. return interval;
  1218. }
  1219. /* The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
  1220. * High speed endpoint descriptors can define "the number of additional
  1221. * transaction opportunities per microframe", but that goes in the Max Burst
  1222. * endpoint context field.
  1223. */
  1224. static u32 xhci_get_endpoint_mult(struct usb_device *udev,
  1225. struct usb_host_endpoint *ep)
  1226. {
  1227. if (udev->speed < USB_SPEED_SUPER ||
  1228. !usb_endpoint_xfer_isoc(&ep->desc))
  1229. return 0;
  1230. return ep->ss_ep_comp.bmAttributes;
  1231. }
  1232. static u32 xhci_get_endpoint_max_burst(struct usb_device *udev,
  1233. struct usb_host_endpoint *ep)
  1234. {
  1235. /* Super speed and Plus have max burst in ep companion desc */
  1236. if (udev->speed >= USB_SPEED_SUPER)
  1237. return ep->ss_ep_comp.bMaxBurst;
  1238. if (udev->speed == USB_SPEED_HIGH &&
  1239. (usb_endpoint_xfer_isoc(&ep->desc) ||
  1240. usb_endpoint_xfer_int(&ep->desc)))
  1241. return usb_endpoint_maxp_mult(&ep->desc) - 1;
  1242. return 0;
  1243. }
  1244. static u32 xhci_get_endpoint_type(struct usb_host_endpoint *ep)
  1245. {
  1246. int in;
  1247. in = usb_endpoint_dir_in(&ep->desc);
  1248. switch (usb_endpoint_type(&ep->desc)) {
  1249. case USB_ENDPOINT_XFER_CONTROL:
  1250. return CTRL_EP;
  1251. case USB_ENDPOINT_XFER_BULK:
  1252. return in ? BULK_IN_EP : BULK_OUT_EP;
  1253. case USB_ENDPOINT_XFER_ISOC:
  1254. return in ? ISOC_IN_EP : ISOC_OUT_EP;
  1255. case USB_ENDPOINT_XFER_INT:
  1256. return in ? INT_IN_EP : INT_OUT_EP;
  1257. }
  1258. return 0;
  1259. }
  1260. /* Return the maximum endpoint service interval time (ESIT) payload.
  1261. * Basically, this is the maxpacket size, multiplied by the burst size
  1262. * and mult size.
  1263. */
  1264. static u32 xhci_get_max_esit_payload(struct usb_device *udev,
  1265. struct usb_host_endpoint *ep)
  1266. {
  1267. int max_burst;
  1268. int max_packet;
  1269. /* Only applies for interrupt or isochronous endpoints */
  1270. if (usb_endpoint_xfer_control(&ep->desc) ||
  1271. usb_endpoint_xfer_bulk(&ep->desc))
  1272. return 0;
  1273. /* SuperSpeedPlus Isoc ep sending over 48k per esit */
  1274. if ((udev->speed >= USB_SPEED_SUPER_PLUS) &&
  1275. USB_SS_SSP_ISOC_COMP(ep->ss_ep_comp.bmAttributes))
  1276. return le32_to_cpu(ep->ssp_isoc_ep_comp.dwBytesPerInterval);
  1277. /* SuperSpeed or SuperSpeedPlus Isoc ep with less than 48k per esit */
  1278. else if (udev->speed >= USB_SPEED_SUPER)
  1279. return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval);
  1280. max_packet = usb_endpoint_maxp(&ep->desc);
  1281. max_burst = usb_endpoint_maxp_mult(&ep->desc);
  1282. /* A 0 in max burst means 1 transfer per ESIT */
  1283. return max_packet * max_burst;
  1284. }
  1285. /* Set up an endpoint with one ring segment. Do not allocate stream rings.
  1286. * Drivers will have to call usb_alloc_streams() to do that.
  1287. */
  1288. int xhci_endpoint_init(struct xhci_hcd *xhci,
  1289. struct xhci_virt_device *virt_dev,
  1290. struct usb_device *udev,
  1291. struct usb_host_endpoint *ep,
  1292. gfp_t mem_flags)
  1293. {
  1294. unsigned int ep_index;
  1295. struct xhci_ep_ctx *ep_ctx;
  1296. struct xhci_ring *ep_ring;
  1297. unsigned int max_packet;
  1298. enum xhci_ring_type ring_type;
  1299. u32 max_esit_payload;
  1300. u32 endpoint_type;
  1301. unsigned int max_burst;
  1302. unsigned int interval;
  1303. unsigned int mult;
  1304. unsigned int avg_trb_len;
  1305. unsigned int err_count = 0;
  1306. ep_index = xhci_get_endpoint_index(&ep->desc);
  1307. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  1308. endpoint_type = xhci_get_endpoint_type(ep);
  1309. if (!endpoint_type)
  1310. return -EINVAL;
  1311. ring_type = usb_endpoint_type(&ep->desc);
  1312. /*
  1313. * Get values to fill the endpoint context, mostly from ep descriptor.
  1314. * The average TRB buffer lengt for bulk endpoints is unclear as we
  1315. * have no clue on scatter gather list entry size. For Isoc and Int,
  1316. * set it to max available. See xHCI 1.1 spec 4.14.1.1 for details.
  1317. */
  1318. max_esit_payload = xhci_get_max_esit_payload(udev, ep);
  1319. interval = xhci_get_endpoint_interval(udev, ep);
  1320. /* Periodic endpoint bInterval limit quirk */
  1321. if (usb_endpoint_xfer_int(&ep->desc) ||
  1322. usb_endpoint_xfer_isoc(&ep->desc)) {
  1323. if ((xhci->quirks & XHCI_LIMIT_ENDPOINT_INTERVAL_7) &&
  1324. udev->speed >= USB_SPEED_HIGH &&
  1325. interval >= 7) {
  1326. interval = 6;
  1327. }
  1328. }
  1329. mult = xhci_get_endpoint_mult(udev, ep);
  1330. max_packet = usb_endpoint_maxp(&ep->desc);
  1331. max_burst = xhci_get_endpoint_max_burst(udev, ep);
  1332. avg_trb_len = max_esit_payload;
  1333. /* FIXME dig Mult and streams info out of ep companion desc */
  1334. /* Allow 3 retries for everything but isoc, set CErr = 3 */
  1335. if (!usb_endpoint_xfer_isoc(&ep->desc))
  1336. err_count = 3;
  1337. /* HS bulk max packet should be 512, FS bulk supports 8, 16, 32 or 64 */
  1338. if (usb_endpoint_xfer_bulk(&ep->desc)) {
  1339. if (udev->speed == USB_SPEED_HIGH)
  1340. max_packet = 512;
  1341. if (udev->speed == USB_SPEED_FULL) {
  1342. max_packet = rounddown_pow_of_two(max_packet);
  1343. max_packet = clamp_val(max_packet, 8, 64);
  1344. }
  1345. }
  1346. /* xHCI 1.0 and 1.1 indicates that ctrl ep avg TRB Length should be 8 */
  1347. if (usb_endpoint_xfer_control(&ep->desc) && xhci->hci_version >= 0x100)
  1348. avg_trb_len = 8;
  1349. /* xhci 1.1 with LEC support doesn't use mult field, use RsvdZ */
  1350. if ((xhci->hci_version > 0x100) && HCC2_LEC(xhci->hcc_params2))
  1351. mult = 0;
  1352. /* Set up the endpoint ring */
  1353. if (xhci_vendor_is_usb_offload_enabled(xhci, virt_dev, ep_index) &&
  1354. usb_endpoint_xfer_isoc(&ep->desc)) {
  1355. virt_dev->eps[ep_index].new_ring =
  1356. xhci_vendor_alloc_transfer_ring(xhci, endpoint_type, ring_type,
  1357. max_packet, mem_flags);
  1358. } else {
  1359. virt_dev->eps[ep_index].new_ring =
  1360. xhci_ring_alloc(xhci, 2, 1, ring_type, max_packet, mem_flags);
  1361. }
  1362. if (!virt_dev->eps[ep_index].new_ring)
  1363. return -ENOMEM;
  1364. virt_dev->eps[ep_index].skip = false;
  1365. ep_ring = virt_dev->eps[ep_index].new_ring;
  1366. /* Fill the endpoint context */
  1367. ep_ctx->ep_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
  1368. EP_INTERVAL(interval) |
  1369. EP_MULT(mult));
  1370. ep_ctx->ep_info2 = cpu_to_le32(EP_TYPE(endpoint_type) |
  1371. MAX_PACKET(max_packet) |
  1372. MAX_BURST(max_burst) |
  1373. ERROR_COUNT(err_count));
  1374. ep_ctx->deq = cpu_to_le64(ep_ring->first_seg->dma |
  1375. ep_ring->cycle_state);
  1376. ep_ctx->tx_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
  1377. EP_AVG_TRB_LENGTH(avg_trb_len));
  1378. return 0;
  1379. }
  1380. void xhci_endpoint_zero(struct xhci_hcd *xhci,
  1381. struct xhci_virt_device *virt_dev,
  1382. struct usb_host_endpoint *ep)
  1383. {
  1384. unsigned int ep_index;
  1385. struct xhci_ep_ctx *ep_ctx;
  1386. ep_index = xhci_get_endpoint_index(&ep->desc);
  1387. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  1388. ep_ctx->ep_info = 0;
  1389. ep_ctx->ep_info2 = 0;
  1390. ep_ctx->deq = 0;
  1391. ep_ctx->tx_info = 0;
  1392. /* Don't free the endpoint ring until the set interface or configuration
  1393. * request succeeds.
  1394. */
  1395. }
  1396. void xhci_clear_endpoint_bw_info(struct xhci_bw_info *bw_info)
  1397. {
  1398. bw_info->ep_interval = 0;
  1399. bw_info->mult = 0;
  1400. bw_info->num_packets = 0;
  1401. bw_info->max_packet_size = 0;
  1402. bw_info->type = 0;
  1403. bw_info->max_esit_payload = 0;
  1404. }
  1405. void xhci_update_bw_info(struct xhci_hcd *xhci,
  1406. struct xhci_container_ctx *in_ctx,
  1407. struct xhci_input_control_ctx *ctrl_ctx,
  1408. struct xhci_virt_device *virt_dev)
  1409. {
  1410. struct xhci_bw_info *bw_info;
  1411. struct xhci_ep_ctx *ep_ctx;
  1412. unsigned int ep_type;
  1413. int i;
  1414. for (i = 1; i < 31; i++) {
  1415. bw_info = &virt_dev->eps[i].bw_info;
  1416. /* We can't tell what endpoint type is being dropped, but
  1417. * unconditionally clearing the bandwidth info for non-periodic
  1418. * endpoints should be harmless because the info will never be
  1419. * set in the first place.
  1420. */
  1421. if (!EP_IS_ADDED(ctrl_ctx, i) && EP_IS_DROPPED(ctrl_ctx, i)) {
  1422. /* Dropped endpoint */
  1423. xhci_clear_endpoint_bw_info(bw_info);
  1424. continue;
  1425. }
  1426. if (EP_IS_ADDED(ctrl_ctx, i)) {
  1427. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, i);
  1428. ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
  1429. /* Ignore non-periodic endpoints */
  1430. if (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
  1431. ep_type != ISOC_IN_EP &&
  1432. ep_type != INT_IN_EP)
  1433. continue;
  1434. /* Added or changed endpoint */
  1435. bw_info->ep_interval = CTX_TO_EP_INTERVAL(
  1436. le32_to_cpu(ep_ctx->ep_info));
  1437. /* Number of packets and mult are zero-based in the
  1438. * input context, but we want one-based for the
  1439. * interval table.
  1440. */
  1441. bw_info->mult = CTX_TO_EP_MULT(
  1442. le32_to_cpu(ep_ctx->ep_info)) + 1;
  1443. bw_info->num_packets = CTX_TO_MAX_BURST(
  1444. le32_to_cpu(ep_ctx->ep_info2)) + 1;
  1445. bw_info->max_packet_size = MAX_PACKET_DECODED(
  1446. le32_to_cpu(ep_ctx->ep_info2));
  1447. bw_info->type = ep_type;
  1448. bw_info->max_esit_payload = CTX_TO_MAX_ESIT_PAYLOAD(
  1449. le32_to_cpu(ep_ctx->tx_info));
  1450. }
  1451. }
  1452. }
  1453. /* Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
  1454. * Useful when you want to change one particular aspect of the endpoint and then
  1455. * issue a configure endpoint command.
  1456. */
  1457. void xhci_endpoint_copy(struct xhci_hcd *xhci,
  1458. struct xhci_container_ctx *in_ctx,
  1459. struct xhci_container_ctx *out_ctx,
  1460. unsigned int ep_index)
  1461. {
  1462. struct xhci_ep_ctx *out_ep_ctx;
  1463. struct xhci_ep_ctx *in_ep_ctx;
  1464. out_ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1465. in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  1466. in_ep_ctx->ep_info = out_ep_ctx->ep_info;
  1467. in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
  1468. in_ep_ctx->deq = out_ep_ctx->deq;
  1469. in_ep_ctx->tx_info = out_ep_ctx->tx_info;
  1470. if (xhci->quirks & XHCI_MTK_HOST) {
  1471. in_ep_ctx->reserved[0] = out_ep_ctx->reserved[0];
  1472. in_ep_ctx->reserved[1] = out_ep_ctx->reserved[1];
  1473. }
  1474. }
  1475. /* Copy output xhci_slot_ctx to the input xhci_slot_ctx.
  1476. * Useful when you want to change one particular aspect of the endpoint and then
  1477. * issue a configure endpoint command. Only the context entries field matters,
  1478. * but we'll copy the whole thing anyway.
  1479. */
  1480. void xhci_slot_copy(struct xhci_hcd *xhci,
  1481. struct xhci_container_ctx *in_ctx,
  1482. struct xhci_container_ctx *out_ctx)
  1483. {
  1484. struct xhci_slot_ctx *in_slot_ctx;
  1485. struct xhci_slot_ctx *out_slot_ctx;
  1486. in_slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1487. out_slot_ctx = xhci_get_slot_ctx(xhci, out_ctx);
  1488. in_slot_ctx->dev_info = out_slot_ctx->dev_info;
  1489. in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
  1490. in_slot_ctx->tt_info = out_slot_ctx->tt_info;
  1491. in_slot_ctx->dev_state = out_slot_ctx->dev_state;
  1492. }
  1493. /* Set up the scratchpad buffer array and scratchpad buffers, if needed. */
  1494. static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
  1495. {
  1496. int i;
  1497. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1498. int num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
  1499. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  1500. "Allocating %d scratchpad buffers", num_sp);
  1501. if (!num_sp)
  1502. return 0;
  1503. xhci->scratchpad = kzalloc_node(sizeof(*xhci->scratchpad), flags,
  1504. dev_to_node(dev));
  1505. if (!xhci->scratchpad)
  1506. goto fail_sp;
  1507. xhci->scratchpad->sp_array = dma_alloc_coherent(dev,
  1508. num_sp * sizeof(u64),
  1509. &xhci->scratchpad->sp_dma, flags);
  1510. if (!xhci->scratchpad->sp_array)
  1511. goto fail_sp2;
  1512. xhci->scratchpad->sp_buffers = kcalloc_node(num_sp, sizeof(void *),
  1513. flags, dev_to_node(dev));
  1514. if (!xhci->scratchpad->sp_buffers)
  1515. goto fail_sp3;
  1516. xhci->dcbaa->dev_context_ptrs[0] = cpu_to_le64(xhci->scratchpad->sp_dma);
  1517. for (i = 0; i < num_sp; i++) {
  1518. dma_addr_t dma;
  1519. void *buf = dma_alloc_coherent(dev, xhci->page_size, &dma,
  1520. flags);
  1521. if (!buf)
  1522. goto fail_sp4;
  1523. xhci->scratchpad->sp_array[i] = dma;
  1524. xhci->scratchpad->sp_buffers[i] = buf;
  1525. }
  1526. return 0;
  1527. fail_sp4:
  1528. for (i = i - 1; i >= 0; i--) {
  1529. dma_free_coherent(dev, xhci->page_size,
  1530. xhci->scratchpad->sp_buffers[i],
  1531. xhci->scratchpad->sp_array[i]);
  1532. }
  1533. kfree(xhci->scratchpad->sp_buffers);
  1534. fail_sp3:
  1535. dma_free_coherent(dev, num_sp * sizeof(u64),
  1536. xhci->scratchpad->sp_array,
  1537. xhci->scratchpad->sp_dma);
  1538. fail_sp2:
  1539. kfree(xhci->scratchpad);
  1540. xhci->scratchpad = NULL;
  1541. fail_sp:
  1542. return -ENOMEM;
  1543. }
  1544. static void scratchpad_free(struct xhci_hcd *xhci)
  1545. {
  1546. int num_sp;
  1547. int i;
  1548. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1549. if (!xhci->scratchpad)
  1550. return;
  1551. num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
  1552. for (i = 0; i < num_sp; i++) {
  1553. dma_free_coherent(dev, xhci->page_size,
  1554. xhci->scratchpad->sp_buffers[i],
  1555. xhci->scratchpad->sp_array[i]);
  1556. }
  1557. kfree(xhci->scratchpad->sp_buffers);
  1558. dma_free_coherent(dev, num_sp * sizeof(u64),
  1559. xhci->scratchpad->sp_array,
  1560. xhci->scratchpad->sp_dma);
  1561. kfree(xhci->scratchpad);
  1562. xhci->scratchpad = NULL;
  1563. }
  1564. struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
  1565. bool allocate_completion, gfp_t mem_flags)
  1566. {
  1567. struct xhci_command *command;
  1568. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1569. command = kzalloc_node(sizeof(*command), mem_flags, dev_to_node(dev));
  1570. if (!command)
  1571. return NULL;
  1572. if (allocate_completion) {
  1573. command->completion =
  1574. kzalloc_node(sizeof(struct completion), mem_flags,
  1575. dev_to_node(dev));
  1576. if (!command->completion) {
  1577. kfree(command);
  1578. return NULL;
  1579. }
  1580. init_completion(command->completion);
  1581. }
  1582. command->status = 0;
  1583. INIT_LIST_HEAD(&command->cmd_list);
  1584. return command;
  1585. }
  1586. EXPORT_SYMBOL_GPL(xhci_alloc_command);
  1587. struct xhci_command *xhci_alloc_command_with_ctx(struct xhci_hcd *xhci,
  1588. bool allocate_completion, gfp_t mem_flags)
  1589. {
  1590. struct xhci_command *command;
  1591. command = xhci_alloc_command(xhci, allocate_completion, mem_flags);
  1592. if (!command)
  1593. return NULL;
  1594. command->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT,
  1595. mem_flags);
  1596. if (!command->in_ctx) {
  1597. kfree(command->completion);
  1598. kfree(command);
  1599. return NULL;
  1600. }
  1601. return command;
  1602. }
  1603. void xhci_urb_free_priv(struct urb_priv *urb_priv)
  1604. {
  1605. kfree(urb_priv);
  1606. }
  1607. void xhci_free_command(struct xhci_hcd *xhci,
  1608. struct xhci_command *command)
  1609. {
  1610. xhci_free_container_ctx(xhci,
  1611. command->in_ctx);
  1612. kfree(command->completion);
  1613. kfree(command);
  1614. }
  1615. EXPORT_SYMBOL_GPL(xhci_free_command);
  1616. int xhci_alloc_erst(struct xhci_hcd *xhci,
  1617. struct xhci_ring *evt_ring,
  1618. struct xhci_erst *erst,
  1619. gfp_t flags)
  1620. {
  1621. size_t size;
  1622. unsigned int val;
  1623. struct xhci_segment *seg;
  1624. struct xhci_erst_entry *entry;
  1625. size = sizeof(struct xhci_erst_entry) * evt_ring->num_segs;
  1626. erst->entries = dma_alloc_coherent(xhci_to_hcd(xhci)->self.sysdev,
  1627. size, &erst->erst_dma_addr, flags);
  1628. if (!erst->entries)
  1629. return -ENOMEM;
  1630. erst->num_entries = evt_ring->num_segs;
  1631. seg = evt_ring->first_seg;
  1632. for (val = 0; val < evt_ring->num_segs; val++) {
  1633. entry = &erst->entries[val];
  1634. entry->seg_addr = cpu_to_le64(seg->dma);
  1635. entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
  1636. entry->rsvd = 0;
  1637. seg = seg->next;
  1638. }
  1639. return 0;
  1640. }
  1641. EXPORT_SYMBOL_GPL(xhci_alloc_erst);
  1642. void xhci_free_erst(struct xhci_hcd *xhci, struct xhci_erst *erst)
  1643. {
  1644. size_t size;
  1645. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1646. size = sizeof(struct xhci_erst_entry) * (erst->num_entries);
  1647. if (erst->entries)
  1648. dma_free_coherent(dev, size,
  1649. erst->entries,
  1650. erst->erst_dma_addr);
  1651. erst->entries = NULL;
  1652. }
  1653. EXPORT_SYMBOL_GPL(xhci_free_erst);
  1654. static struct xhci_device_context_array *xhci_vendor_alloc_dcbaa(
  1655. struct xhci_hcd *xhci, gfp_t flags)
  1656. {
  1657. struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
  1658. if (ops && ops->alloc_dcbaa)
  1659. return ops->alloc_dcbaa(xhci, flags);
  1660. return 0;
  1661. }
  1662. static void xhci_vendor_free_dcbaa(struct xhci_hcd *xhci)
  1663. {
  1664. struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
  1665. if (ops && ops->free_dcbaa)
  1666. ops->free_dcbaa(xhci);
  1667. }
  1668. void xhci_mem_cleanup(struct xhci_hcd *xhci)
  1669. {
  1670. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1671. int i, j, num_ports;
  1672. cancel_delayed_work_sync(&xhci->cmd_timer);
  1673. xhci_free_erst(xhci, &xhci->erst);
  1674. if (xhci->event_ring)
  1675. xhci_ring_free(xhci, xhci->event_ring);
  1676. xhci->event_ring = NULL;
  1677. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed event ring");
  1678. if (xhci->cmd_ring)
  1679. xhci_ring_free(xhci, xhci->cmd_ring);
  1680. xhci->cmd_ring = NULL;
  1681. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed command ring");
  1682. xhci_cleanup_command_queue(xhci);
  1683. num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
  1684. for (i = 0; i < num_ports && xhci->rh_bw; i++) {
  1685. struct xhci_interval_bw_table *bwt = &xhci->rh_bw[i].bw_table;
  1686. for (j = 0; j < XHCI_MAX_INTERVAL; j++) {
  1687. struct list_head *ep = &bwt->interval_bw[j].endpoints;
  1688. while (!list_empty(ep))
  1689. list_del_init(ep->next);
  1690. }
  1691. }
  1692. for (i = HCS_MAX_SLOTS(xhci->hcs_params1); i > 0; i--)
  1693. xhci_free_virt_devices_depth_first(xhci, i);
  1694. dma_pool_destroy(xhci->segment_pool);
  1695. xhci->segment_pool = NULL;
  1696. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed segment pool");
  1697. dma_pool_destroy(xhci->device_pool);
  1698. xhci->device_pool = NULL;
  1699. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed device context pool");
  1700. dma_pool_destroy(xhci->small_streams_pool);
  1701. xhci->small_streams_pool = NULL;
  1702. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  1703. "Freed small stream array pool");
  1704. dma_pool_destroy(xhci->medium_streams_pool);
  1705. xhci->medium_streams_pool = NULL;
  1706. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  1707. "Freed medium stream array pool");
  1708. if (xhci_vendor_is_usb_offload_enabled(xhci, NULL, 0)) {
  1709. xhci_vendor_free_dcbaa(xhci);
  1710. } else {
  1711. if (xhci->dcbaa)
  1712. dma_free_coherent(dev, sizeof(*xhci->dcbaa),
  1713. xhci->dcbaa, xhci->dcbaa->dma);
  1714. }
  1715. xhci->dcbaa = NULL;
  1716. scratchpad_free(xhci);
  1717. if (!xhci->rh_bw)
  1718. goto no_bw;
  1719. for (i = 0; i < num_ports; i++) {
  1720. struct xhci_tt_bw_info *tt, *n;
  1721. list_for_each_entry_safe(tt, n, &xhci->rh_bw[i].tts, tt_list) {
  1722. list_del(&tt->tt_list);
  1723. kfree(tt);
  1724. }
  1725. }
  1726. no_bw:
  1727. xhci->cmd_ring_reserved_trbs = 0;
  1728. xhci->usb2_rhub.num_ports = 0;
  1729. xhci->usb3_rhub.num_ports = 0;
  1730. xhci->num_active_eps = 0;
  1731. kfree(xhci->usb2_rhub.ports);
  1732. kfree(xhci->usb3_rhub.ports);
  1733. kfree(xhci->hw_ports);
  1734. kfree(xhci->rh_bw);
  1735. kfree(xhci->ext_caps);
  1736. for (i = 0; i < xhci->num_port_caps; i++)
  1737. kfree(xhci->port_caps[i].psi);
  1738. kfree(xhci->port_caps);
  1739. xhci->num_port_caps = 0;
  1740. xhci->usb2_rhub.ports = NULL;
  1741. xhci->usb3_rhub.ports = NULL;
  1742. xhci->hw_ports = NULL;
  1743. xhci->rh_bw = NULL;
  1744. xhci->ext_caps = NULL;
  1745. xhci->port_caps = NULL;
  1746. xhci->page_size = 0;
  1747. xhci->page_shift = 0;
  1748. xhci->usb2_rhub.bus_state.bus_suspended = 0;
  1749. xhci->usb3_rhub.bus_state.bus_suspended = 0;
  1750. }
  1751. static int xhci_test_trb_in_td(struct xhci_hcd *xhci,
  1752. struct xhci_segment *input_seg,
  1753. union xhci_trb *start_trb,
  1754. union xhci_trb *end_trb,
  1755. dma_addr_t input_dma,
  1756. struct xhci_segment *result_seg,
  1757. char *test_name, int test_number)
  1758. {
  1759. unsigned long long start_dma;
  1760. unsigned long long end_dma;
  1761. struct xhci_segment *seg;
  1762. start_dma = xhci_trb_virt_to_dma(input_seg, start_trb);
  1763. end_dma = xhci_trb_virt_to_dma(input_seg, end_trb);
  1764. seg = trb_in_td(xhci, input_seg, start_trb, end_trb, input_dma, false);
  1765. if (seg != result_seg) {
  1766. xhci_warn(xhci, "WARN: %s TRB math test %d failed!\n",
  1767. test_name, test_number);
  1768. xhci_warn(xhci, "Tested TRB math w/ seg %p and "
  1769. "input DMA 0x%llx\n",
  1770. input_seg,
  1771. (unsigned long long) input_dma);
  1772. xhci_warn(xhci, "starting TRB %p (0x%llx DMA), "
  1773. "ending TRB %p (0x%llx DMA)\n",
  1774. start_trb, start_dma,
  1775. end_trb, end_dma);
  1776. xhci_warn(xhci, "Expected seg %p, got seg %p\n",
  1777. result_seg, seg);
  1778. trb_in_td(xhci, input_seg, start_trb, end_trb, input_dma,
  1779. true);
  1780. return -1;
  1781. }
  1782. return 0;
  1783. }
  1784. /* TRB math checks for xhci_trb_in_td(), using the command and event rings. */
  1785. int xhci_check_trb_in_td_math(struct xhci_hcd *xhci)
  1786. {
  1787. struct {
  1788. dma_addr_t input_dma;
  1789. struct xhci_segment *result_seg;
  1790. } simple_test_vector [] = {
  1791. /* A zeroed DMA field should fail */
  1792. { 0, NULL },
  1793. /* One TRB before the ring start should fail */
  1794. { xhci->event_ring->first_seg->dma - 16, NULL },
  1795. /* One byte before the ring start should fail */
  1796. { xhci->event_ring->first_seg->dma - 1, NULL },
  1797. /* Starting TRB should succeed */
  1798. { xhci->event_ring->first_seg->dma, xhci->event_ring->first_seg },
  1799. /* Ending TRB should succeed */
  1800. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16,
  1801. xhci->event_ring->first_seg },
  1802. /* One byte after the ring end should fail */
  1803. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16 + 1, NULL },
  1804. /* One TRB after the ring end should fail */
  1805. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT)*16, NULL },
  1806. /* An address of all ones should fail */
  1807. { (dma_addr_t) (~0), NULL },
  1808. };
  1809. struct {
  1810. struct xhci_segment *input_seg;
  1811. union xhci_trb *start_trb;
  1812. union xhci_trb *end_trb;
  1813. dma_addr_t input_dma;
  1814. struct xhci_segment *result_seg;
  1815. } complex_test_vector [] = {
  1816. /* Test feeding a valid DMA address from a different ring */
  1817. { .input_seg = xhci->event_ring->first_seg,
  1818. .start_trb = xhci->event_ring->first_seg->trbs,
  1819. .end_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1820. .input_dma = xhci->cmd_ring->first_seg->dma,
  1821. .result_seg = NULL,
  1822. },
  1823. /* Test feeding a valid end TRB from a different ring */
  1824. { .input_seg = xhci->event_ring->first_seg,
  1825. .start_trb = xhci->event_ring->first_seg->trbs,
  1826. .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1827. .input_dma = xhci->cmd_ring->first_seg->dma,
  1828. .result_seg = NULL,
  1829. },
  1830. /* Test feeding a valid start and end TRB from a different ring */
  1831. { .input_seg = xhci->event_ring->first_seg,
  1832. .start_trb = xhci->cmd_ring->first_seg->trbs,
  1833. .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1834. .input_dma = xhci->cmd_ring->first_seg->dma,
  1835. .result_seg = NULL,
  1836. },
  1837. /* TRB in this ring, but after this TD */
  1838. { .input_seg = xhci->event_ring->first_seg,
  1839. .start_trb = &xhci->event_ring->first_seg->trbs[0],
  1840. .end_trb = &xhci->event_ring->first_seg->trbs[3],
  1841. .input_dma = xhci->event_ring->first_seg->dma + 4*16,
  1842. .result_seg = NULL,
  1843. },
  1844. /* TRB in this ring, but before this TD */
  1845. { .input_seg = xhci->event_ring->first_seg,
  1846. .start_trb = &xhci->event_ring->first_seg->trbs[3],
  1847. .end_trb = &xhci->event_ring->first_seg->trbs[6],
  1848. .input_dma = xhci->event_ring->first_seg->dma + 2*16,
  1849. .result_seg = NULL,
  1850. },
  1851. /* TRB in this ring, but after this wrapped TD */
  1852. { .input_seg = xhci->event_ring->first_seg,
  1853. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  1854. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  1855. .input_dma = xhci->event_ring->first_seg->dma + 2*16,
  1856. .result_seg = NULL,
  1857. },
  1858. /* TRB in this ring, but before this wrapped TD */
  1859. { .input_seg = xhci->event_ring->first_seg,
  1860. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  1861. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  1862. .input_dma = xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 4)*16,
  1863. .result_seg = NULL,
  1864. },
  1865. /* TRB not in this ring, and we have a wrapped TD */
  1866. { .input_seg = xhci->event_ring->first_seg,
  1867. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  1868. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  1869. .input_dma = xhci->cmd_ring->first_seg->dma + 2*16,
  1870. .result_seg = NULL,
  1871. },
  1872. };
  1873. unsigned int num_tests;
  1874. int i, ret;
  1875. num_tests = ARRAY_SIZE(simple_test_vector);
  1876. for (i = 0; i < num_tests; i++) {
  1877. ret = xhci_test_trb_in_td(xhci,
  1878. xhci->event_ring->first_seg,
  1879. xhci->event_ring->first_seg->trbs,
  1880. &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1881. simple_test_vector[i].input_dma,
  1882. simple_test_vector[i].result_seg,
  1883. "Simple", i);
  1884. if (ret < 0)
  1885. return ret;
  1886. }
  1887. num_tests = ARRAY_SIZE(complex_test_vector);
  1888. for (i = 0; i < num_tests; i++) {
  1889. ret = xhci_test_trb_in_td(xhci,
  1890. complex_test_vector[i].input_seg,
  1891. complex_test_vector[i].start_trb,
  1892. complex_test_vector[i].end_trb,
  1893. complex_test_vector[i].input_dma,
  1894. complex_test_vector[i].result_seg,
  1895. "Complex", i);
  1896. if (ret < 0)
  1897. return ret;
  1898. }
  1899. xhci_dbg(xhci, "TRB math tests passed.\n");
  1900. return 0;
  1901. }
  1902. EXPORT_SYMBOL_GPL(xhci_check_trb_in_td_math);
  1903. static void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
  1904. {
  1905. u64 temp;
  1906. dma_addr_t deq;
  1907. deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
  1908. xhci->event_ring->dequeue);
  1909. if (!deq)
  1910. xhci_warn(xhci, "WARN something wrong with SW event ring "
  1911. "dequeue ptr.\n");
  1912. /* Update HC event ring dequeue pointer */
  1913. temp = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  1914. temp &= ERST_PTR_MASK;
  1915. /* Don't clear the EHB bit (which is RW1C) because
  1916. * there might be more events to service.
  1917. */
  1918. temp &= ~ERST_EHB;
  1919. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  1920. "// Write event ring dequeue pointer, "
  1921. "preserving EHB bit");
  1922. xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp,
  1923. &xhci->ir_set->erst_dequeue);
  1924. }
  1925. static void xhci_add_in_port(struct xhci_hcd *xhci, unsigned int num_ports,
  1926. __le32 __iomem *addr, int max_caps)
  1927. {
  1928. u32 temp, port_offset, port_count;
  1929. int i;
  1930. u8 major_revision, minor_revision, tmp_minor_revision;
  1931. struct xhci_hub *rhub;
  1932. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  1933. struct xhci_port_cap *port_cap;
  1934. temp = readl(addr);
  1935. major_revision = XHCI_EXT_PORT_MAJOR(temp);
  1936. minor_revision = XHCI_EXT_PORT_MINOR(temp);
  1937. if (major_revision == 0x03) {
  1938. rhub = &xhci->usb3_rhub;
  1939. /*
  1940. * Some hosts incorrectly use sub-minor version for minor
  1941. * version (i.e. 0x02 instead of 0x20 for bcdUSB 0x320 and 0x01
  1942. * for bcdUSB 0x310). Since there is no USB release with sub
  1943. * minor version 0x301 to 0x309, we can assume that they are
  1944. * incorrect and fix it here.
  1945. */
  1946. if (minor_revision > 0x00 && minor_revision < 0x10)
  1947. minor_revision <<= 4;
  1948. /*
  1949. * Some zhaoxin's xHCI controller that follow usb3.1 spec
  1950. * but only support Gen1.
  1951. */
  1952. if (xhci->quirks & XHCI_ZHAOXIN_HOST) {
  1953. tmp_minor_revision = minor_revision;
  1954. minor_revision = 0;
  1955. }
  1956. } else if (major_revision <= 0x02) {
  1957. rhub = &xhci->usb2_rhub;
  1958. } else {
  1959. xhci_warn(xhci, "Ignoring unknown port speed, "
  1960. "Ext Cap %p, revision = 0x%x\n",
  1961. addr, major_revision);
  1962. /* Ignoring port protocol we can't understand. FIXME */
  1963. return;
  1964. }
  1965. /* Port offset and count in the third dword, see section 7.2 */
  1966. temp = readl(addr + 2);
  1967. port_offset = XHCI_EXT_PORT_OFF(temp);
  1968. port_count = XHCI_EXT_PORT_COUNT(temp);
  1969. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  1970. "Ext Cap %p, port offset = %u, "
  1971. "count = %u, revision = 0x%x",
  1972. addr, port_offset, port_count, major_revision);
  1973. /* Port count includes the current port offset */
  1974. if (port_offset == 0 || (port_offset + port_count - 1) > num_ports)
  1975. /* WTF? "Valid values are ‘1’ to MaxPorts" */
  1976. return;
  1977. port_cap = &xhci->port_caps[xhci->num_port_caps++];
  1978. if (xhci->num_port_caps > max_caps)
  1979. return;
  1980. port_cap->psi_count = XHCI_EXT_PORT_PSIC(temp);
  1981. if (port_cap->psi_count) {
  1982. port_cap->psi = kcalloc_node(port_cap->psi_count,
  1983. sizeof(*port_cap->psi),
  1984. GFP_KERNEL, dev_to_node(dev));
  1985. if (!port_cap->psi)
  1986. port_cap->psi_count = 0;
  1987. port_cap->psi_uid_count++;
  1988. for (i = 0; i < port_cap->psi_count; i++) {
  1989. port_cap->psi[i] = readl(addr + 4 + i);
  1990. /* count unique ID values, two consecutive entries can
  1991. * have the same ID if link is assymetric
  1992. */
  1993. if (i && (XHCI_EXT_PORT_PSIV(port_cap->psi[i]) !=
  1994. XHCI_EXT_PORT_PSIV(port_cap->psi[i - 1])))
  1995. port_cap->psi_uid_count++;
  1996. if (xhci->quirks & XHCI_ZHAOXIN_HOST &&
  1997. major_revision == 0x03 &&
  1998. XHCI_EXT_PORT_PSIV(port_cap->psi[i]) >= 5)
  1999. minor_revision = tmp_minor_revision;
  2000. xhci_dbg(xhci, "PSIV:%d PSIE:%d PLT:%d PFD:%d LP:%d PSIM:%d\n",
  2001. XHCI_EXT_PORT_PSIV(port_cap->psi[i]),
  2002. XHCI_EXT_PORT_PSIE(port_cap->psi[i]),
  2003. XHCI_EXT_PORT_PLT(port_cap->psi[i]),
  2004. XHCI_EXT_PORT_PFD(port_cap->psi[i]),
  2005. XHCI_EXT_PORT_LP(port_cap->psi[i]),
  2006. XHCI_EXT_PORT_PSIM(port_cap->psi[i]));
  2007. }
  2008. }
  2009. rhub->maj_rev = major_revision;
  2010. if (rhub->min_rev < minor_revision)
  2011. rhub->min_rev = minor_revision;
  2012. port_cap->maj_rev = major_revision;
  2013. port_cap->min_rev = minor_revision;
  2014. /* cache usb2 port capabilities */
  2015. if (major_revision < 0x03 && xhci->num_ext_caps < max_caps)
  2016. xhci->ext_caps[xhci->num_ext_caps++] = temp;
  2017. if ((xhci->hci_version >= 0x100) && (major_revision != 0x03) &&
  2018. (temp & XHCI_HLC)) {
  2019. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2020. "xHCI 1.0: support USB2 hardware lpm");
  2021. xhci->hw_lpm_support = 1;
  2022. }
  2023. port_offset--;
  2024. for (i = port_offset; i < (port_offset + port_count); i++) {
  2025. struct xhci_port *hw_port = &xhci->hw_ports[i];
  2026. /* Duplicate entry. Ignore the port if the revisions differ. */
  2027. if (hw_port->rhub) {
  2028. xhci_warn(xhci, "Duplicate port entry, Ext Cap %p,"
  2029. " port %u\n", addr, i);
  2030. xhci_warn(xhci, "Port was marked as USB %u, "
  2031. "duplicated as USB %u\n",
  2032. hw_port->rhub->maj_rev, major_revision);
  2033. /* Only adjust the roothub port counts if we haven't
  2034. * found a similar duplicate.
  2035. */
  2036. if (hw_port->rhub != rhub &&
  2037. hw_port->hcd_portnum != DUPLICATE_ENTRY) {
  2038. hw_port->rhub->num_ports--;
  2039. hw_port->hcd_portnum = DUPLICATE_ENTRY;
  2040. }
  2041. continue;
  2042. }
  2043. hw_port->rhub = rhub;
  2044. hw_port->port_cap = port_cap;
  2045. rhub->num_ports++;
  2046. }
  2047. /* FIXME: Should we disable ports not in the Extended Capabilities? */
  2048. }
  2049. static void xhci_create_rhub_port_array(struct xhci_hcd *xhci,
  2050. struct xhci_hub *rhub, gfp_t flags)
  2051. {
  2052. int port_index = 0;
  2053. int i;
  2054. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  2055. if (!rhub->num_ports)
  2056. return;
  2057. rhub->ports = kcalloc_node(rhub->num_ports, sizeof(*rhub->ports),
  2058. flags, dev_to_node(dev));
  2059. if (!rhub->ports)
  2060. return;
  2061. for (i = 0; i < HCS_MAX_PORTS(xhci->hcs_params1); i++) {
  2062. if (xhci->hw_ports[i].rhub != rhub ||
  2063. xhci->hw_ports[i].hcd_portnum == DUPLICATE_ENTRY)
  2064. continue;
  2065. xhci->hw_ports[i].hcd_portnum = port_index;
  2066. rhub->ports[port_index] = &xhci->hw_ports[i];
  2067. port_index++;
  2068. if (port_index == rhub->num_ports)
  2069. break;
  2070. }
  2071. }
  2072. /*
  2073. * Scan the Extended Capabilities for the "Supported Protocol Capabilities" that
  2074. * specify what speeds each port is supposed to be. We can't count on the port
  2075. * speed bits in the PORTSC register being correct until a device is connected,
  2076. * but we need to set up the two fake roothubs with the correct number of USB
  2077. * 3.0 and USB 2.0 ports at host controller initialization time.
  2078. */
  2079. static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags)
  2080. {
  2081. void __iomem *base;
  2082. u32 offset;
  2083. unsigned int num_ports;
  2084. int i, j;
  2085. int cap_count = 0;
  2086. u32 cap_start;
  2087. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  2088. num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
  2089. xhci->hw_ports = kcalloc_node(num_ports, sizeof(*xhci->hw_ports),
  2090. flags, dev_to_node(dev));
  2091. if (!xhci->hw_ports)
  2092. return -ENOMEM;
  2093. for (i = 0; i < num_ports; i++) {
  2094. xhci->hw_ports[i].addr = &xhci->op_regs->port_status_base +
  2095. NUM_PORT_REGS * i;
  2096. xhci->hw_ports[i].hw_portnum = i;
  2097. }
  2098. xhci->rh_bw = kcalloc_node(num_ports, sizeof(*xhci->rh_bw), flags,
  2099. dev_to_node(dev));
  2100. if (!xhci->rh_bw)
  2101. return -ENOMEM;
  2102. for (i = 0; i < num_ports; i++) {
  2103. struct xhci_interval_bw_table *bw_table;
  2104. INIT_LIST_HEAD(&xhci->rh_bw[i].tts);
  2105. bw_table = &xhci->rh_bw[i].bw_table;
  2106. for (j = 0; j < XHCI_MAX_INTERVAL; j++)
  2107. INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints);
  2108. }
  2109. base = &xhci->cap_regs->hc_capbase;
  2110. cap_start = xhci_find_next_ext_cap(base, 0, XHCI_EXT_CAPS_PROTOCOL);
  2111. if (!cap_start) {
  2112. xhci_err(xhci, "No Extended Capability registers, unable to set up roothub\n");
  2113. return -ENODEV;
  2114. }
  2115. offset = cap_start;
  2116. /* count extended protocol capability entries for later caching */
  2117. while (offset) {
  2118. cap_count++;
  2119. offset = xhci_find_next_ext_cap(base, offset,
  2120. XHCI_EXT_CAPS_PROTOCOL);
  2121. }
  2122. xhci->ext_caps = kcalloc_node(cap_count, sizeof(*xhci->ext_caps),
  2123. flags, dev_to_node(dev));
  2124. if (!xhci->ext_caps)
  2125. return -ENOMEM;
  2126. xhci->port_caps = kcalloc_node(cap_count, sizeof(*xhci->port_caps),
  2127. flags, dev_to_node(dev));
  2128. if (!xhci->port_caps)
  2129. return -ENOMEM;
  2130. offset = cap_start;
  2131. while (offset) {
  2132. xhci_add_in_port(xhci, num_ports, base + offset, cap_count);
  2133. if (xhci->usb2_rhub.num_ports + xhci->usb3_rhub.num_ports ==
  2134. num_ports)
  2135. break;
  2136. offset = xhci_find_next_ext_cap(base, offset,
  2137. XHCI_EXT_CAPS_PROTOCOL);
  2138. }
  2139. if (xhci->usb2_rhub.num_ports == 0 && xhci->usb3_rhub.num_ports == 0) {
  2140. xhci_warn(xhci, "No ports on the roothubs?\n");
  2141. return -ENODEV;
  2142. }
  2143. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2144. "Found %u USB 2.0 ports and %u USB 3.0 ports.",
  2145. xhci->usb2_rhub.num_ports, xhci->usb3_rhub.num_ports);
  2146. /* Place limits on the number of roothub ports so that the hub
  2147. * descriptors aren't longer than the USB core will allocate.
  2148. */
  2149. if (xhci->usb3_rhub.num_ports > USB_SS_MAXPORTS) {
  2150. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2151. "Limiting USB 3.0 roothub ports to %u.",
  2152. USB_SS_MAXPORTS);
  2153. xhci->usb3_rhub.num_ports = USB_SS_MAXPORTS;
  2154. }
  2155. if (xhci->usb2_rhub.num_ports > USB_MAXCHILDREN) {
  2156. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2157. "Limiting USB 2.0 roothub ports to %u.",
  2158. USB_MAXCHILDREN);
  2159. xhci->usb2_rhub.num_ports = USB_MAXCHILDREN;
  2160. }
  2161. if (!xhci->usb2_rhub.num_ports)
  2162. xhci_info(xhci, "USB2 root hub has no ports\n");
  2163. if (!xhci->usb3_rhub.num_ports)
  2164. xhci_info(xhci, "USB3 root hub has no ports\n");
  2165. xhci_create_rhub_port_array(xhci, &xhci->usb2_rhub, flags);
  2166. xhci_create_rhub_port_array(xhci, &xhci->usb3_rhub, flags);
  2167. return 0;
  2168. }
  2169. int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
  2170. {
  2171. dma_addr_t dma;
  2172. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  2173. unsigned int val, val2;
  2174. u64 val_64;
  2175. u32 page_size, temp;
  2176. int i, ret;
  2177. INIT_LIST_HEAD(&xhci->cmd_list);
  2178. /* init command timeout work */
  2179. INIT_DELAYED_WORK(&xhci->cmd_timer, xhci_handle_command_timeout);
  2180. init_completion(&xhci->cmd_ring_stop_completion);
  2181. page_size = readl(&xhci->op_regs->page_size);
  2182. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2183. "Supported page size register = 0x%x", page_size);
  2184. i = ffs(page_size);
  2185. if (i < 16)
  2186. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2187. "Supported page size of %iK", (1 << (i+12)) / 1024);
  2188. else
  2189. xhci_warn(xhci, "WARN: no supported page size\n");
  2190. /* Use 4K pages, since that's common and the minimum the HC supports */
  2191. xhci->page_shift = 12;
  2192. xhci->page_size = 1 << xhci->page_shift;
  2193. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2194. "HCD page size set to %iK", xhci->page_size / 1024);
  2195. /*
  2196. * Program the Number of Device Slots Enabled field in the CONFIG
  2197. * register with the max value of slots the HC can handle.
  2198. */
  2199. val = HCS_MAX_SLOTS(readl(&xhci->cap_regs->hcs_params1));
  2200. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2201. "// xHC can handle at most %d device slots.", val);
  2202. val2 = readl(&xhci->op_regs->config_reg);
  2203. val |= (val2 & ~HCS_SLOTS_MASK);
  2204. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2205. "// Setting Max device slots reg = 0x%x.", val);
  2206. writel(val, &xhci->op_regs->config_reg);
  2207. /*
  2208. * xHCI section 5.4.6 - Device Context array must be
  2209. * "physically contiguous and 64-byte (cache line) aligned".
  2210. */
  2211. if (xhci_vendor_is_usb_offload_enabled(xhci, NULL, 0)) {
  2212. xhci->dcbaa = xhci_vendor_alloc_dcbaa(xhci, flags);
  2213. if (!xhci->dcbaa)
  2214. goto fail;
  2215. } else {
  2216. xhci->dcbaa = dma_alloc_coherent(dev, sizeof(*xhci->dcbaa), &dma,
  2217. flags);
  2218. if (!xhci->dcbaa)
  2219. goto fail;
  2220. xhci->dcbaa->dma = dma;
  2221. }
  2222. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2223. "// Device context base array address = 0x%llx (DMA), %p (virt)",
  2224. (unsigned long long)xhci->dcbaa->dma, xhci->dcbaa);
  2225. xhci_write_64(xhci, xhci->dcbaa->dma, &xhci->op_regs->dcbaa_ptr);
  2226. /*
  2227. * Initialize the ring segment pool. The ring must be a contiguous
  2228. * structure comprised of TRBs. The TRBs must be 16 byte aligned,
  2229. * however, the command ring segment needs 64-byte aligned segments
  2230. * and our use of dma addresses in the trb_address_map radix tree needs
  2231. * TRB_SEGMENT_SIZE alignment, so we pick the greater alignment need.
  2232. */
  2233. if (xhci->quirks & XHCI_ZHAOXIN_TRB_FETCH)
  2234. xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
  2235. TRB_SEGMENT_SIZE * 2, TRB_SEGMENT_SIZE * 2, xhci->page_size * 2);
  2236. else
  2237. xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
  2238. TRB_SEGMENT_SIZE, TRB_SEGMENT_SIZE, xhci->page_size);
  2239. /* See Table 46 and Note on Figure 55 */
  2240. xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
  2241. 2112, 64, xhci->page_size);
  2242. if (!xhci->segment_pool || !xhci->device_pool)
  2243. goto fail;
  2244. /* Linear stream context arrays don't have any boundary restrictions,
  2245. * and only need to be 16-byte aligned.
  2246. */
  2247. xhci->small_streams_pool =
  2248. dma_pool_create("xHCI 256 byte stream ctx arrays",
  2249. dev, SMALL_STREAM_ARRAY_SIZE, 16, 0);
  2250. xhci->medium_streams_pool =
  2251. dma_pool_create("xHCI 1KB stream ctx arrays",
  2252. dev, MEDIUM_STREAM_ARRAY_SIZE, 16, 0);
  2253. /* Any stream context array bigger than MEDIUM_STREAM_ARRAY_SIZE
  2254. * will be allocated with dma_alloc_coherent()
  2255. */
  2256. if (!xhci->small_streams_pool || !xhci->medium_streams_pool)
  2257. goto fail;
  2258. /* Set up the command ring to have one segments for now. */
  2259. xhci->cmd_ring = xhci_ring_alloc(xhci, 1, 1, TYPE_COMMAND, 0, flags);
  2260. if (!xhci->cmd_ring)
  2261. goto fail;
  2262. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2263. "Allocated command ring at %p", xhci->cmd_ring);
  2264. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "First segment DMA is 0x%llx",
  2265. (unsigned long long)xhci->cmd_ring->first_seg->dma);
  2266. /* Set the address in the Command Ring Control register */
  2267. val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  2268. val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
  2269. (xhci->cmd_ring->first_seg->dma & (u64) ~CMD_RING_RSVD_BITS) |
  2270. xhci->cmd_ring->cycle_state;
  2271. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2272. "// Setting command ring address to 0x%016llx", val_64);
  2273. xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
  2274. /* Reserve one command ring TRB for disabling LPM.
  2275. * Since the USB core grabs the shared usb_bus bandwidth mutex before
  2276. * disabling LPM, we only need to reserve one TRB for all devices.
  2277. */
  2278. xhci->cmd_ring_reserved_trbs++;
  2279. val = readl(&xhci->cap_regs->db_off);
  2280. val &= DBOFF_MASK;
  2281. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2282. "// Doorbell array is located at offset 0x%x"
  2283. " from cap regs base addr", val);
  2284. xhci->dba = (void __iomem *) xhci->cap_regs + val;
  2285. /* Set ir_set to interrupt register set 0 */
  2286. xhci->ir_set = &xhci->run_regs->ir_set[0];
  2287. /*
  2288. * Event ring setup: Allocate a normal ring, but also setup
  2289. * the event ring segment table (ERST). Section 4.9.3.
  2290. */
  2291. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Allocating event ring");
  2292. xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, 1, TYPE_EVENT,
  2293. 0, flags);
  2294. if (!xhci->event_ring)
  2295. goto fail;
  2296. if (xhci_check_trb_in_td_math(xhci) < 0)
  2297. goto fail;
  2298. ret = xhci_alloc_erst(xhci, xhci->event_ring, &xhci->erst, flags);
  2299. if (ret)
  2300. goto fail;
  2301. /* set ERST count with the number of entries in the segment table */
  2302. val = readl(&xhci->ir_set->erst_size);
  2303. val &= ERST_SIZE_MASK;
  2304. val |= ERST_NUM_SEGS;
  2305. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2306. "// Write ERST size = %i to ir_set 0 (some bits preserved)",
  2307. val);
  2308. writel(val, &xhci->ir_set->erst_size);
  2309. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2310. "// Set ERST entries to point to event ring.");
  2311. /* set the segment table base address */
  2312. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2313. "// Set ERST base address for ir_set 0 = 0x%llx",
  2314. (unsigned long long)xhci->erst.erst_dma_addr);
  2315. val_64 = xhci_read_64(xhci, &xhci->ir_set->erst_base);
  2316. val_64 &= ERST_PTR_MASK;
  2317. val_64 |= (xhci->erst.erst_dma_addr & (u64) ~ERST_PTR_MASK);
  2318. xhci_write_64(xhci, val_64, &xhci->ir_set->erst_base);
  2319. /* Set the event ring dequeue address */
  2320. xhci_set_hc_event_deq(xhci);
  2321. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  2322. "Wrote ERST address to ir_set 0.");
  2323. xhci->isoc_bei_interval = AVOID_BEI_INTERVAL_MAX;
  2324. /*
  2325. * XXX: Might need to set the Interrupter Moderation Register to
  2326. * something other than the default (~1ms minimum between interrupts).
  2327. * See section 5.5.1.2.
  2328. */
  2329. for (i = 0; i < MAX_HC_SLOTS; i++)
  2330. xhci->devs[i] = NULL;
  2331. for (i = 0; i < USB_MAXCHILDREN; i++) {
  2332. xhci->usb2_rhub.bus_state.resume_done[i] = 0;
  2333. xhci->usb3_rhub.bus_state.resume_done[i] = 0;
  2334. /* Only the USB 2.0 completions will ever be used. */
  2335. init_completion(&xhci->usb2_rhub.bus_state.rexit_done[i]);
  2336. init_completion(&xhci->usb3_rhub.bus_state.u3exit_done[i]);
  2337. }
  2338. if (scratchpad_alloc(xhci, flags))
  2339. goto fail;
  2340. if (xhci_setup_port_arrays(xhci, flags))
  2341. goto fail;
  2342. /* Enable USB 3.0 device notifications for function remote wake, which
  2343. * is necessary for allowing USB 3.0 devices to do remote wakeup from
  2344. * U3 (device suspend).
  2345. */
  2346. temp = readl(&xhci->op_regs->dev_notification);
  2347. temp &= ~DEV_NOTE_MASK;
  2348. temp |= DEV_NOTE_FWAKE;
  2349. writel(temp, &xhci->op_regs->dev_notification);
  2350. return 0;
  2351. fail:
  2352. xhci_halt(xhci);
  2353. xhci_reset(xhci, XHCI_RESET_SHORT_USEC);
  2354. xhci_mem_cleanup(xhci);
  2355. return -ENOMEM;
  2356. }