xhci-mem.c 75 KB

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