xhci-mem.c 72 KB

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