xhci-mem.c 74 KB

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