xhci-mem.c 75 KB

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