xhci-mem.c 75 KB

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