xhci-mem.c 74 KB

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