xhci-mem.c 73 KB

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