xhci-mem.c 76 KB

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