xhci-mem.c 64 KB

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