xhci-mem.c 71 KB

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