xhci-mem.c 75 KB

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