xhci.h 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  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. #ifndef __LINUX_XHCI_HCD_H
  23. #define __LINUX_XHCI_HCD_H
  24. #include <linux/usb.h>
  25. #include <linux/timer.h>
  26. #include <linux/kernel.h>
  27. #include <linux/usb/hcd.h>
  28. /* Code sharing between pci-quirks and xhci hcd */
  29. #include "xhci-ext-caps.h"
  30. #include "pci-quirks.h"
  31. /* xHCI PCI Configuration Registers */
  32. #define XHCI_SBRN_OFFSET (0x60)
  33. /* Max number of USB devices for any host controller - limit in section 6.1 */
  34. #define MAX_HC_SLOTS 256
  35. /* Section 5.3.3 - MaxPorts */
  36. #define MAX_HC_PORTS 127
  37. /*
  38. * xHCI register interface.
  39. * This corresponds to the eXtensible Host Controller Interface (xHCI)
  40. * Revision 0.95 specification
  41. */
  42. /**
  43. * struct xhci_cap_regs - xHCI Host Controller Capability Registers.
  44. * @hc_capbase: length of the capabilities register and HC version number
  45. * @hcs_params1: HCSPARAMS1 - Structural Parameters 1
  46. * @hcs_params2: HCSPARAMS2 - Structural Parameters 2
  47. * @hcs_params3: HCSPARAMS3 - Structural Parameters 3
  48. * @hcc_params: HCCPARAMS - Capability Parameters
  49. * @db_off: DBOFF - Doorbell array offset
  50. * @run_regs_off: RTSOFF - Runtime register space offset
  51. */
  52. struct xhci_cap_regs {
  53. __le32 hc_capbase;
  54. __le32 hcs_params1;
  55. __le32 hcs_params2;
  56. __le32 hcs_params3;
  57. __le32 hcc_params;
  58. __le32 db_off;
  59. __le32 run_regs_off;
  60. /* Reserved up to (CAPLENGTH - 0x1C) */
  61. };
  62. /* hc_capbase bitmasks */
  63. /* bits 7:0 - how long is the Capabilities register */
  64. #define HC_LENGTH(p) XHCI_HC_LENGTH(p)
  65. /* bits 31:16 */
  66. #define HC_VERSION(p) (((p) >> 16) & 0xffff)
  67. /* HCSPARAMS1 - hcs_params1 - bitmasks */
  68. /* bits 0:7, Max Device Slots */
  69. #define HCS_MAX_SLOTS(p) (((p) >> 0) & 0xff)
  70. #define HCS_SLOTS_MASK 0xff
  71. /* bits 8:18, Max Interrupters */
  72. #define HCS_MAX_INTRS(p) (((p) >> 8) & 0x7ff)
  73. /* bits 24:31, Max Ports - max value is 0x7F = 127 ports */
  74. #define HCS_MAX_PORTS(p) (((p) >> 24) & 0x7f)
  75. /* HCSPARAMS2 - hcs_params2 - bitmasks */
  76. /* bits 0:3, frames or uframes that SW needs to queue transactions
  77. * ahead of the HW to meet periodic deadlines */
  78. #define HCS_IST(p) (((p) >> 0) & 0xf)
  79. /* bits 4:7, max number of Event Ring segments */
  80. #define HCS_ERST_MAX(p) (((p) >> 4) & 0xf)
  81. /* bits 21:25 Hi 5 bits of Scratchpad buffers SW must allocate for the HW */
  82. /* bit 26 Scratchpad restore - for save/restore HW state - not used yet */
  83. /* bits 27:31 Lo 5 bits of Scratchpad buffers SW must allocate for the HW */
  84. #define HCS_MAX_SCRATCHPAD(p) ((((p) >> 16) & 0x3e0) | (((p) >> 27) & 0x1f))
  85. /* HCSPARAMS3 - hcs_params3 - bitmasks */
  86. /* bits 0:7, Max U1 to U0 latency for the roothub ports */
  87. #define HCS_U1_LATENCY(p) (((p) >> 0) & 0xff)
  88. /* bits 16:31, Max U2 to U0 latency for the roothub ports */
  89. #define HCS_U2_LATENCY(p) (((p) >> 16) & 0xffff)
  90. /* HCCPARAMS - hcc_params - bitmasks */
  91. /* true: HC can use 64-bit address pointers */
  92. #define HCC_64BIT_ADDR(p) ((p) & (1 << 0))
  93. /* true: HC can do bandwidth negotiation */
  94. #define HCC_BANDWIDTH_NEG(p) ((p) & (1 << 1))
  95. /* true: HC uses 64-byte Device Context structures
  96. * FIXME 64-byte context structures aren't supported yet.
  97. */
  98. #define HCC_64BYTE_CONTEXT(p) ((p) & (1 << 2))
  99. /* true: HC has port power switches */
  100. #define HCC_PPC(p) ((p) & (1 << 3))
  101. /* true: HC has port indicators */
  102. #define HCS_INDICATOR(p) ((p) & (1 << 4))
  103. /* true: HC has Light HC Reset Capability */
  104. #define HCC_LIGHT_RESET(p) ((p) & (1 << 5))
  105. /* true: HC supports latency tolerance messaging */
  106. #define HCC_LTC(p) ((p) & (1 << 6))
  107. /* true: no secondary Stream ID Support */
  108. #define HCC_NSS(p) ((p) & (1 << 7))
  109. /* Max size for Primary Stream Arrays - 2^(n+1), where n is bits 12:15 */
  110. #define HCC_MAX_PSA(p) (1 << ((((p) >> 12) & 0xf) + 1))
  111. /* Extended Capabilities pointer from PCI base - section 5.3.6 */
  112. #define HCC_EXT_CAPS(p) XHCI_HCC_EXT_CAPS(p)
  113. /* db_off bitmask - bits 0:1 reserved */
  114. #define DBOFF_MASK (~0x3)
  115. /* run_regs_off bitmask - bits 0:4 reserved */
  116. #define RTSOFF_MASK (~0x1f)
  117. /* Number of registers per port */
  118. #define NUM_PORT_REGS 4
  119. #define PORTSC 0
  120. #define PORTPMSC 1
  121. #define PORTLI 2
  122. #define PORTHLPMC 3
  123. /**
  124. * struct xhci_op_regs - xHCI Host Controller Operational Registers.
  125. * @command: USBCMD - xHC command register
  126. * @status: USBSTS - xHC status register
  127. * @page_size: This indicates the page size that the host controller
  128. * supports. If bit n is set, the HC supports a page size
  129. * of 2^(n+12), up to a 128MB page size.
  130. * 4K is the minimum page size.
  131. * @cmd_ring: CRP - 64-bit Command Ring Pointer
  132. * @dcbaa_ptr: DCBAAP - 64-bit Device Context Base Address Array Pointer
  133. * @config_reg: CONFIG - Configure Register
  134. * @port_status_base: PORTSCn - base address for Port Status and Control
  135. * Each port has a Port Status and Control register,
  136. * followed by a Port Power Management Status and Control
  137. * register, a Port Link Info register, and a reserved
  138. * register.
  139. * @port_power_base: PORTPMSCn - base address for
  140. * Port Power Management Status and Control
  141. * @port_link_base: PORTLIn - base address for Port Link Info (current
  142. * Link PM state and control) for USB 2.1 and USB 3.0
  143. * devices.
  144. */
  145. struct xhci_op_regs {
  146. __le32 command;
  147. __le32 status;
  148. __le32 page_size;
  149. __le32 reserved1;
  150. __le32 reserved2;
  151. __le32 dev_notification;
  152. __le64 cmd_ring;
  153. /* rsvd: offset 0x20-2F */
  154. __le32 reserved3[4];
  155. __le64 dcbaa_ptr;
  156. __le32 config_reg;
  157. /* rsvd: offset 0x3C-3FF */
  158. __le32 reserved4[241];
  159. /* port 1 registers, which serve as a base address for other ports */
  160. __le32 port_status_base;
  161. __le32 port_power_base;
  162. __le32 port_link_base;
  163. __le32 reserved5;
  164. /* registers for ports 2-255 */
  165. __le32 reserved6[NUM_PORT_REGS*254];
  166. };
  167. /* USBCMD - USB command - command bitmasks */
  168. /* start/stop HC execution - do not write unless HC is halted*/
  169. #define CMD_RUN XHCI_CMD_RUN
  170. /* Reset HC - resets internal HC state machine and all registers (except
  171. * PCI config regs). HC does NOT drive a USB reset on the downstream ports.
  172. * The xHCI driver must reinitialize the xHC after setting this bit.
  173. */
  174. #define CMD_RESET (1 << 1)
  175. /* Event Interrupt Enable - a '1' allows interrupts from the host controller */
  176. #define CMD_EIE XHCI_CMD_EIE
  177. /* Host System Error Interrupt Enable - get out-of-band signal for HC errors */
  178. #define CMD_HSEIE XHCI_CMD_HSEIE
  179. /* bits 4:6 are reserved (and should be preserved on writes). */
  180. /* light reset (port status stays unchanged) - reset completed when this is 0 */
  181. #define CMD_LRESET (1 << 7)
  182. /* host controller save/restore state. */
  183. #define CMD_CSS (1 << 8)
  184. #define CMD_CRS (1 << 9)
  185. /* Enable Wrap Event - '1' means xHC generates an event when MFINDEX wraps. */
  186. #define CMD_EWE XHCI_CMD_EWE
  187. /* MFINDEX power management - '1' means xHC can stop MFINDEX counter if all root
  188. * hubs are in U3 (selective suspend), disconnect, disabled, or powered-off.
  189. * '0' means the xHC can power it off if all ports are in the disconnect,
  190. * disabled, or powered-off state.
  191. */
  192. #define CMD_PM_INDEX (1 << 11)
  193. /* bits 12:31 are reserved (and should be preserved on writes). */
  194. /* IMAN - Interrupt Management Register */
  195. #define IMAN_IE (1 << 1)
  196. #define IMAN_IP (1 << 0)
  197. /* USBSTS - USB status - status bitmasks */
  198. /* HC not running - set to 1 when run/stop bit is cleared. */
  199. #define STS_HALT XHCI_STS_HALT
  200. /* serious error, e.g. PCI parity error. The HC will clear the run/stop bit. */
  201. #define STS_FATAL (1 << 2)
  202. /* event interrupt - clear this prior to clearing any IP flags in IR set*/
  203. #define STS_EINT (1 << 3)
  204. /* port change detect */
  205. #define STS_PORT (1 << 4)
  206. /* bits 5:7 reserved and zeroed */
  207. /* save state status - '1' means xHC is saving state */
  208. #define STS_SAVE (1 << 8)
  209. /* restore state status - '1' means xHC is restoring state */
  210. #define STS_RESTORE (1 << 9)
  211. /* true: save or restore error */
  212. #define STS_SRE (1 << 10)
  213. /* true: Controller Not Ready to accept doorbell or op reg writes after reset */
  214. #define STS_CNR XHCI_STS_CNR
  215. /* true: internal Host Controller Error - SW needs to reset and reinitialize */
  216. #define STS_HCE (1 << 12)
  217. /* bits 13:31 reserved and should be preserved */
  218. /*
  219. * DNCTRL - Device Notification Control Register - dev_notification bitmasks
  220. * Generate a device notification event when the HC sees a transaction with a
  221. * notification type that matches a bit set in this bit field.
  222. */
  223. #define DEV_NOTE_MASK (0xffff)
  224. #define ENABLE_DEV_NOTE(x) (1 << (x))
  225. /* Most of the device notification types should only be used for debug.
  226. * SW does need to pay attention to function wake notifications.
  227. */
  228. #define DEV_NOTE_FWAKE ENABLE_DEV_NOTE(1)
  229. /* CRCR - Command Ring Control Register - cmd_ring bitmasks */
  230. /* bit 0 is the command ring cycle state */
  231. /* stop ring operation after completion of the currently executing command */
  232. #define CMD_RING_PAUSE (1 << 1)
  233. /* stop ring immediately - abort the currently executing command */
  234. #define CMD_RING_ABORT (1 << 2)
  235. /* true: command ring is running */
  236. #define CMD_RING_RUNNING (1 << 3)
  237. /* bits 4:5 reserved and should be preserved */
  238. /* Command Ring pointer - bit mask for the lower 32 bits. */
  239. #define CMD_RING_RSVD_BITS (0x3f)
  240. /* CONFIG - Configure Register - config_reg bitmasks */
  241. /* bits 0:7 - maximum number of device slots enabled (NumSlotsEn) */
  242. #define MAX_DEVS(p) ((p) & 0xff)
  243. /* bits 8:31 - reserved and should be preserved */
  244. /* PORTSC - Port Status and Control Register - port_status_base bitmasks */
  245. /* true: device connected */
  246. #define PORT_CONNECT (1 << 0)
  247. /* true: port enabled */
  248. #define PORT_PE (1 << 1)
  249. /* bit 2 reserved and zeroed */
  250. /* true: port has an over-current condition */
  251. #define PORT_OC (1 << 3)
  252. /* true: port reset signaling asserted */
  253. #define PORT_RESET (1 << 4)
  254. /* Port Link State - bits 5:8
  255. * A read gives the current link PM state of the port,
  256. * a write with Link State Write Strobe set sets the link state.
  257. */
  258. #define PORT_PLS_MASK (0xf << 5)
  259. #define XDEV_U0 (0x0 << 5)
  260. #define XDEV_U2 (0x2 << 5)
  261. #define XDEV_U3 (0x3 << 5)
  262. #define XDEV_RESUME (0xf << 5)
  263. /* true: port has power (see HCC_PPC) */
  264. #define PORT_POWER (1 << 9)
  265. /* bits 10:13 indicate device speed:
  266. * 0 - undefined speed - port hasn't be initialized by a reset yet
  267. * 1 - full speed
  268. * 2 - low speed
  269. * 3 - high speed
  270. * 4 - super speed
  271. * 5-15 reserved
  272. */
  273. #define DEV_SPEED_MASK (0xf << 10)
  274. #define XDEV_FS (0x1 << 10)
  275. #define XDEV_LS (0x2 << 10)
  276. #define XDEV_HS (0x3 << 10)
  277. #define XDEV_SS (0x4 << 10)
  278. #define DEV_UNDEFSPEED(p) (((p) & DEV_SPEED_MASK) == (0x0<<10))
  279. #define DEV_FULLSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_FS)
  280. #define DEV_LOWSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_LS)
  281. #define DEV_HIGHSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_HS)
  282. #define DEV_SUPERSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_SS)
  283. /* Bits 20:23 in the Slot Context are the speed for the device */
  284. #define SLOT_SPEED_FS (XDEV_FS << 10)
  285. #define SLOT_SPEED_LS (XDEV_LS << 10)
  286. #define SLOT_SPEED_HS (XDEV_HS << 10)
  287. #define SLOT_SPEED_SS (XDEV_SS << 10)
  288. /* Port Indicator Control */
  289. #define PORT_LED_OFF (0 << 14)
  290. #define PORT_LED_AMBER (1 << 14)
  291. #define PORT_LED_GREEN (2 << 14)
  292. #define PORT_LED_MASK (3 << 14)
  293. /* Port Link State Write Strobe - set this when changing link state */
  294. #define PORT_LINK_STROBE (1 << 16)
  295. /* true: connect status change */
  296. #define PORT_CSC (1 << 17)
  297. /* true: port enable change */
  298. #define PORT_PEC (1 << 18)
  299. /* true: warm reset for a USB 3.0 device is done. A "hot" reset puts the port
  300. * into an enabled state, and the device into the default state. A "warm" reset
  301. * also resets the link, forcing the device through the link training sequence.
  302. * SW can also look at the Port Reset register to see when warm reset is done.
  303. */
  304. #define PORT_WRC (1 << 19)
  305. /* true: over-current change */
  306. #define PORT_OCC (1 << 20)
  307. /* true: reset change - 1 to 0 transition of PORT_RESET */
  308. #define PORT_RC (1 << 21)
  309. /* port link status change - set on some port link state transitions:
  310. * Transition Reason
  311. * ------------------------------------------------------------------------------
  312. * - U3 to Resume Wakeup signaling from a device
  313. * - Resume to Recovery to U0 USB 3.0 device resume
  314. * - Resume to U0 USB 2.0 device resume
  315. * - U3 to Recovery to U0 Software resume of USB 3.0 device complete
  316. * - U3 to U0 Software resume of USB 2.0 device complete
  317. * - U2 to U0 L1 resume of USB 2.1 device complete
  318. * - U0 to U0 (???) L1 entry rejection by USB 2.1 device
  319. * - U0 to disabled L1 entry error with USB 2.1 device
  320. * - Any state to inactive Error on USB 3.0 port
  321. */
  322. #define PORT_PLC (1 << 22)
  323. /* port configure error change - port failed to configure its link partner */
  324. #define PORT_CEC (1 << 23)
  325. /* Cold Attach Status - xHC can set this bit to report device attached during
  326. * Sx state. Warm port reset should be perfomed to clear this bit and move port
  327. * to connected state.
  328. */
  329. #define PORT_CAS (1 << 24)
  330. /* wake on connect (enable) */
  331. #define PORT_WKCONN_E (1 << 25)
  332. /* wake on disconnect (enable) */
  333. #define PORT_WKDISC_E (1 << 26)
  334. /* wake on over-current (enable) */
  335. #define PORT_WKOC_E (1 << 27)
  336. /* bits 28:29 reserved */
  337. /* true: device is non-removable - for USB 3.0 roothub emulation */
  338. #define PORT_DEV_REMOVE (1 << 30)
  339. /* Initiate a warm port reset - complete when PORT_WRC is '1' */
  340. #define PORT_WR (1 << 31)
  341. /* We mark duplicate entries with -1 */
  342. #define DUPLICATE_ENTRY ((u8)(-1))
  343. /* Port Power Management Status and Control - port_power_base bitmasks */
  344. /* Inactivity timer value for transitions into U1, in microseconds.
  345. * Timeout can be up to 127us. 0xFF means an infinite timeout.
  346. */
  347. #define PORT_U1_TIMEOUT(p) ((p) & 0xff)
  348. #define PORT_U1_TIMEOUT_MASK 0xff
  349. /* Inactivity timer value for transitions into U2 */
  350. #define PORT_U2_TIMEOUT(p) (((p) & 0xff) << 8)
  351. #define PORT_U2_TIMEOUT_MASK (0xff << 8)
  352. /* Bits 24:31 for port testing */
  353. /* USB2 Protocol PORTSPMSC */
  354. #define PORT_L1S_MASK 7
  355. #define PORT_L1S_SUCCESS 1
  356. #define PORT_RWE (1 << 3)
  357. #define PORT_HIRD(p) (((p) & 0xf) << 4)
  358. #define PORT_HIRD_MASK (0xf << 4)
  359. #define PORT_L1DS_MASK (0xff << 8)
  360. #define PORT_L1DS(p) (((p) & 0xff) << 8)
  361. #define PORT_HLE (1 << 16)
  362. /* USB2 Protocol PORTHLPMC */
  363. #define PORT_HIRDM(p)((p) & 3)
  364. #define PORT_L1_TIMEOUT(p)(((p) & 0xff) << 2)
  365. #define PORT_BESLD(p)(((p) & 0xf) << 10)
  366. /* use 512 microseconds as USB2 LPM L1 default timeout. */
  367. #define XHCI_L1_TIMEOUT 512
  368. /* Set default HIRD/BESL value to 4 (350/400us) for USB2 L1 LPM resume latency.
  369. * Safe to use with mixed HIRD and BESL systems (host and device) and is used
  370. * by other operating systems.
  371. *
  372. * XHCI 1.0 errata 8/14/12 Table 13 notes:
  373. * "Software should choose xHC BESL/BESLD field values that do not violate a
  374. * device's resume latency requirements,
  375. * e.g. not program values > '4' if BLC = '1' and a HIRD device is attached,
  376. * or not program values < '4' if BLC = '0' and a BESL device is attached.
  377. */
  378. #define XHCI_DEFAULT_BESL 4
  379. /**
  380. * struct xhci_intr_reg - Interrupt Register Set
  381. * @irq_pending: IMAN - Interrupt Management Register. Used to enable
  382. * interrupts and check for pending interrupts.
  383. * @irq_control: IMOD - Interrupt Moderation Register.
  384. * Used to throttle interrupts.
  385. * @erst_size: Number of segments in the Event Ring Segment Table (ERST).
  386. * @erst_base: ERST base address.
  387. * @erst_dequeue: Event ring dequeue pointer.
  388. *
  389. * Each interrupter (defined by a MSI-X vector) has an event ring and an Event
  390. * Ring Segment Table (ERST) associated with it. The event ring is comprised of
  391. * multiple segments of the same size. The HC places events on the ring and
  392. * "updates the Cycle bit in the TRBs to indicate to software the current
  393. * position of the Enqueue Pointer." The HCD (Linux) processes those events and
  394. * updates the dequeue pointer.
  395. */
  396. struct xhci_intr_reg {
  397. __le32 irq_pending;
  398. __le32 irq_control;
  399. __le32 erst_size;
  400. __le32 rsvd;
  401. __le64 erst_base;
  402. __le64 erst_dequeue;
  403. };
  404. /* irq_pending bitmasks */
  405. #define ER_IRQ_PENDING(p) ((p) & 0x1)
  406. /* bits 2:31 need to be preserved */
  407. /* THIS IS BUGGY - FIXME - IP IS WRITE 1 TO CLEAR */
  408. #define ER_IRQ_CLEAR(p) ((p) & 0xfffffffe)
  409. #define ER_IRQ_ENABLE(p) ((ER_IRQ_CLEAR(p)) | 0x2)
  410. #define ER_IRQ_DISABLE(p) ((ER_IRQ_CLEAR(p)) & ~(0x2))
  411. /* irq_control bitmasks */
  412. /* Minimum interval between interrupts (in 250ns intervals). The interval
  413. * between interrupts will be longer if there are no events on the event ring.
  414. * Default is 4000 (1 ms).
  415. */
  416. #define ER_IRQ_INTERVAL_MASK (0xffff)
  417. /* Counter used to count down the time to the next interrupt - HW use only */
  418. #define ER_IRQ_COUNTER_MASK (0xffff << 16)
  419. /* erst_size bitmasks */
  420. /* Preserve bits 16:31 of erst_size */
  421. #define ERST_SIZE_MASK (0xffff << 16)
  422. /* erst_dequeue bitmasks */
  423. /* Dequeue ERST Segment Index (DESI) - Segment number (or alias)
  424. * where the current dequeue pointer lies. This is an optional HW hint.
  425. */
  426. #define ERST_DESI_MASK (0x7)
  427. /* Event Handler Busy (EHB) - is the event ring scheduled to be serviced by
  428. * a work queue (or delayed service routine)?
  429. */
  430. #define ERST_EHB (1 << 3)
  431. #define ERST_PTR_MASK (0xf)
  432. /**
  433. * struct xhci_run_regs
  434. * @microframe_index:
  435. * MFINDEX - current microframe number
  436. *
  437. * Section 5.5 Host Controller Runtime Registers:
  438. * "Software should read and write these registers using only Dword (32 bit)
  439. * or larger accesses"
  440. */
  441. struct xhci_run_regs {
  442. __le32 microframe_index;
  443. __le32 rsvd[7];
  444. struct xhci_intr_reg ir_set[128];
  445. };
  446. /**
  447. * struct doorbell_array
  448. *
  449. * Bits 0 - 7: Endpoint target
  450. * Bits 8 - 15: RsvdZ
  451. * Bits 16 - 31: Stream ID
  452. *
  453. * Section 5.6
  454. */
  455. struct xhci_doorbell_array {
  456. __le32 doorbell[256];
  457. };
  458. #define DB_VALUE(ep, stream) ((((ep) + 1) & 0xff) | ((stream) << 16))
  459. #define DB_VALUE_HOST 0x00000000
  460. /**
  461. * struct xhci_protocol_caps
  462. * @revision: major revision, minor revision, capability ID,
  463. * and next capability pointer.
  464. * @name_string: Four ASCII characters to say which spec this xHC
  465. * follows, typically "USB ".
  466. * @port_info: Port offset, count, and protocol-defined information.
  467. */
  468. struct xhci_protocol_caps {
  469. u32 revision;
  470. u32 name_string;
  471. u32 port_info;
  472. };
  473. #define XHCI_EXT_PORT_MAJOR(x) (((x) >> 24) & 0xff)
  474. #define XHCI_EXT_PORT_OFF(x) ((x) & 0xff)
  475. #define XHCI_EXT_PORT_COUNT(x) (((x) >> 8) & 0xff)
  476. /**
  477. * struct xhci_container_ctx
  478. * @type: Type of context. Used to calculated offsets to contained contexts.
  479. * @size: Size of the context data
  480. * @bytes: The raw context data given to HW
  481. * @dma: dma address of the bytes
  482. *
  483. * Represents either a Device or Input context. Holds a pointer to the raw
  484. * memory used for the context (bytes) and dma address of it (dma).
  485. */
  486. struct xhci_container_ctx {
  487. unsigned type;
  488. #define XHCI_CTX_TYPE_DEVICE 0x1
  489. #define XHCI_CTX_TYPE_INPUT 0x2
  490. int size;
  491. u8 *bytes;
  492. dma_addr_t dma;
  493. };
  494. /**
  495. * struct xhci_slot_ctx
  496. * @dev_info: Route string, device speed, hub info, and last valid endpoint
  497. * @dev_info2: Max exit latency for device number, root hub port number
  498. * @tt_info: tt_info is used to construct split transaction tokens
  499. * @dev_state: slot state and device address
  500. *
  501. * Slot Context - section 6.2.1.1. This assumes the HC uses 32-byte context
  502. * structures. If the HC uses 64-byte contexts, there is an additional 32 bytes
  503. * reserved at the end of the slot context for HC internal use.
  504. */
  505. struct xhci_slot_ctx {
  506. __le32 dev_info;
  507. __le32 dev_info2;
  508. __le32 tt_info;
  509. __le32 dev_state;
  510. /* offset 0x10 to 0x1f reserved for HC internal use */
  511. __le32 reserved[4];
  512. };
  513. /* dev_info bitmasks */
  514. /* Route String - 0:19 */
  515. #define ROUTE_STRING_MASK (0xfffff)
  516. /* Device speed - values defined by PORTSC Device Speed field - 20:23 */
  517. #define DEV_SPEED (0xf << 20)
  518. /* bit 24 reserved */
  519. /* Is this LS/FS device connected through a HS hub? - bit 25 */
  520. #define DEV_MTT (0x1 << 25)
  521. /* Set if the device is a hub - bit 26 */
  522. #define DEV_HUB (0x1 << 26)
  523. /* Index of the last valid endpoint context in this device context - 27:31 */
  524. #define LAST_CTX_MASK (0x1f << 27)
  525. #define LAST_CTX(p) ((p) << 27)
  526. #define LAST_CTX_TO_EP_NUM(p) (((p) >> 27) - 1)
  527. #define SLOT_FLAG (1 << 0)
  528. #define EP0_FLAG (1 << 1)
  529. /* dev_info2 bitmasks */
  530. /* Max Exit Latency (ms) - worst case time to wake up all links in dev path */
  531. #define MAX_EXIT (0xffff)
  532. /* Root hub port number that is needed to access the USB device */
  533. #define ROOT_HUB_PORT(p) (((p) & 0xff) << 16)
  534. #define DEVINFO_TO_ROOT_HUB_PORT(p) (((p) >> 16) & 0xff)
  535. /* Maximum number of ports under a hub device */
  536. #define XHCI_MAX_PORTS(p) (((p) & 0xff) << 24)
  537. /* tt_info bitmasks */
  538. /*
  539. * TT Hub Slot ID - for low or full speed devices attached to a high-speed hub
  540. * The Slot ID of the hub that isolates the high speed signaling from
  541. * this low or full-speed device. '0' if attached to root hub port.
  542. */
  543. #define TT_SLOT (0xff)
  544. /*
  545. * The number of the downstream facing port of the high-speed hub
  546. * '0' if the device is not low or full speed.
  547. */
  548. #define TT_PORT (0xff << 8)
  549. #define TT_THINK_TIME(p) (((p) & 0x3) << 16)
  550. /* dev_state bitmasks */
  551. /* USB device address - assigned by the HC */
  552. #define DEV_ADDR_MASK (0xff)
  553. /* bits 8:26 reserved */
  554. /* Slot state */
  555. #define SLOT_STATE (0x1f << 27)
  556. #define GET_SLOT_STATE(p) (((p) & (0x1f << 27)) >> 27)
  557. #define SLOT_STATE_DISABLED 0
  558. #define SLOT_STATE_ENABLED SLOT_STATE_DISABLED
  559. #define SLOT_STATE_DEFAULT 1
  560. #define SLOT_STATE_ADDRESSED 2
  561. #define SLOT_STATE_CONFIGURED 3
  562. /**
  563. * struct xhci_ep_ctx
  564. * @ep_info: endpoint state, streams, mult, and interval information.
  565. * @ep_info2: information on endpoint type, max packet size, max burst size,
  566. * error count, and whether the HC will force an event for all
  567. * transactions.
  568. * @deq: 64-bit ring dequeue pointer address. If the endpoint only
  569. * defines one stream, this points to the endpoint transfer ring.
  570. * Otherwise, it points to a stream context array, which has a
  571. * ring pointer for each flow.
  572. * @tx_info:
  573. * Average TRB lengths for the endpoint ring and
  574. * max payload within an Endpoint Service Interval Time (ESIT).
  575. *
  576. * Endpoint Context - section 6.2.1.2. This assumes the HC uses 32-byte context
  577. * structures. If the HC uses 64-byte contexts, there is an additional 32 bytes
  578. * reserved at the end of the endpoint context for HC internal use.
  579. */
  580. struct xhci_ep_ctx {
  581. __le32 ep_info;
  582. __le32 ep_info2;
  583. __le64 deq;
  584. __le32 tx_info;
  585. /* offset 0x14 - 0x1f reserved for HC internal use */
  586. __le32 reserved[3];
  587. };
  588. /* ep_info bitmasks */
  589. /*
  590. * Endpoint State - bits 0:2
  591. * 0 - disabled
  592. * 1 - running
  593. * 2 - halted due to halt condition - ok to manipulate endpoint ring
  594. * 3 - stopped
  595. * 4 - TRB error
  596. * 5-7 - reserved
  597. */
  598. #define EP_STATE_MASK (0xf)
  599. #define EP_STATE_DISABLED 0
  600. #define EP_STATE_RUNNING 1
  601. #define EP_STATE_HALTED 2
  602. #define EP_STATE_STOPPED 3
  603. #define EP_STATE_ERROR 4
  604. /* Mult - Max number of burtst within an interval, in EP companion desc. */
  605. #define EP_MULT(p) (((p) & 0x3) << 8)
  606. #define CTX_TO_EP_MULT(p) (((p) >> 8) & 0x3)
  607. /* bits 10:14 are Max Primary Streams */
  608. /* bit 15 is Linear Stream Array */
  609. /* Interval - period between requests to an endpoint - 125u increments. */
  610. #define EP_INTERVAL(p) (((p) & 0xff) << 16)
  611. #define EP_INTERVAL_TO_UFRAMES(p) (1 << (((p) >> 16) & 0xff))
  612. #define CTX_TO_EP_INTERVAL(p) (((p) >> 16) & 0xff)
  613. #define EP_MAXPSTREAMS_MASK (0x1f << 10)
  614. #define EP_MAXPSTREAMS(p) (((p) << 10) & EP_MAXPSTREAMS_MASK)
  615. /* Endpoint is set up with a Linear Stream Array (vs. Secondary Stream Array) */
  616. #define EP_HAS_LSA (1 << 15)
  617. /* ep_info2 bitmasks */
  618. /*
  619. * Force Event - generate transfer events for all TRBs for this endpoint
  620. * This will tell the HC to ignore the IOC and ISP flags (for debugging only).
  621. */
  622. #define FORCE_EVENT (0x1)
  623. #define ERROR_COUNT(p) (((p) & 0x3) << 1)
  624. #define CTX_TO_EP_TYPE(p) (((p) >> 3) & 0x7)
  625. #define EP_TYPE(p) ((p) << 3)
  626. #define ISOC_OUT_EP 1
  627. #define BULK_OUT_EP 2
  628. #define INT_OUT_EP 3
  629. #define CTRL_EP 4
  630. #define ISOC_IN_EP 5
  631. #define BULK_IN_EP 6
  632. #define INT_IN_EP 7
  633. /* bit 6 reserved */
  634. /* bit 7 is Host Initiate Disable - for disabling stream selection */
  635. #define MAX_BURST(p) (((p)&0xff) << 8)
  636. #define CTX_TO_MAX_BURST(p) (((p) >> 8) & 0xff)
  637. #define MAX_PACKET(p) (((p)&0xffff) << 16)
  638. #define MAX_PACKET_MASK (0xffff << 16)
  639. #define MAX_PACKET_DECODED(p) (((p) >> 16) & 0xffff)
  640. /* Get max packet size from ep desc. Bit 10..0 specify the max packet size.
  641. * USB2.0 spec 9.6.6.
  642. */
  643. #define GET_MAX_PACKET(p) ((p) & 0x7ff)
  644. /* tx_info bitmasks */
  645. #define AVG_TRB_LENGTH_FOR_EP(p) ((p) & 0xffff)
  646. #define MAX_ESIT_PAYLOAD_FOR_EP(p) (((p) & 0xffff) << 16)
  647. #define CTX_TO_MAX_ESIT_PAYLOAD(p) (((p) >> 16) & 0xffff)
  648. /* deq bitmasks */
  649. #define EP_CTX_CYCLE_MASK (1 << 0)
  650. #define SCTX_DEQ_MASK (~0xfL)
  651. /**
  652. * struct xhci_input_control_context
  653. * Input control context; see section 6.2.5.
  654. *
  655. * @drop_context: set the bit of the endpoint context you want to disable
  656. * @add_context: set the bit of the endpoint context you want to enable
  657. */
  658. struct xhci_input_control_ctx {
  659. __le32 drop_flags;
  660. __le32 add_flags;
  661. __le32 rsvd2[6];
  662. };
  663. #define EP_IS_ADDED(ctrl_ctx, i) \
  664. (le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))
  665. #define EP_IS_DROPPED(ctrl_ctx, i) \
  666. (le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1)))
  667. /* Represents everything that is needed to issue a command on the command ring.
  668. * It's useful to pre-allocate these for commands that cannot fail due to
  669. * out-of-memory errors, like freeing streams.
  670. */
  671. struct xhci_command {
  672. /* Input context for changing device state */
  673. struct xhci_container_ctx *in_ctx;
  674. u32 status;
  675. /* If completion is null, no one is waiting on this command
  676. * and the structure can be freed after the command completes.
  677. */
  678. struct completion *completion;
  679. union xhci_trb *command_trb;
  680. struct list_head cmd_list;
  681. };
  682. /* drop context bitmasks */
  683. #define DROP_EP(x) (0x1 << x)
  684. /* add context bitmasks */
  685. #define ADD_EP(x) (0x1 << x)
  686. struct xhci_stream_ctx {
  687. /* 64-bit stream ring address, cycle state, and stream type */
  688. __le64 stream_ring;
  689. /* offset 0x14 - 0x1f reserved for HC internal use */
  690. __le32 reserved[2];
  691. };
  692. /* Stream Context Types (section 6.4.1) - bits 3:1 of stream ctx deq ptr */
  693. #define SCT_FOR_CTX(p) (((p) & 0x7) << 1)
  694. /* Secondary stream array type, dequeue pointer is to a transfer ring */
  695. #define SCT_SEC_TR 0
  696. /* Primary stream array type, dequeue pointer is to a transfer ring */
  697. #define SCT_PRI_TR 1
  698. /* Dequeue pointer is for a secondary stream array (SSA) with 8 entries */
  699. #define SCT_SSA_8 2
  700. #define SCT_SSA_16 3
  701. #define SCT_SSA_32 4
  702. #define SCT_SSA_64 5
  703. #define SCT_SSA_128 6
  704. #define SCT_SSA_256 7
  705. /* Assume no secondary streams for now */
  706. struct xhci_stream_info {
  707. struct xhci_ring **stream_rings;
  708. /* Number of streams, including stream 0 (which drivers can't use) */
  709. unsigned int num_streams;
  710. /* The stream context array may be bigger than
  711. * the number of streams the driver asked for
  712. */
  713. struct xhci_stream_ctx *stream_ctx_array;
  714. unsigned int num_stream_ctxs;
  715. dma_addr_t ctx_array_dma;
  716. /* For mapping physical TRB addresses to segments in stream rings */
  717. struct radix_tree_root trb_address_map;
  718. struct xhci_command *free_streams_command;
  719. };
  720. #define SMALL_STREAM_ARRAY_SIZE 256
  721. #define MEDIUM_STREAM_ARRAY_SIZE 1024
  722. /* Some Intel xHCI host controllers need software to keep track of the bus
  723. * bandwidth. Keep track of endpoint info here. Each root port is allocated
  724. * the full bus bandwidth. We must also treat TTs (including each port under a
  725. * multi-TT hub) as a separate bandwidth domain. The direct memory interface
  726. * (DMI) also limits the total bandwidth (across all domains) that can be used.
  727. */
  728. struct xhci_bw_info {
  729. /* ep_interval is zero-based */
  730. unsigned int ep_interval;
  731. /* mult and num_packets are one-based */
  732. unsigned int mult;
  733. unsigned int num_packets;
  734. unsigned int max_packet_size;
  735. unsigned int max_esit_payload;
  736. unsigned int type;
  737. };
  738. /* "Block" sizes in bytes the hardware uses for different device speeds.
  739. * The logic in this part of the hardware limits the number of bits the hardware
  740. * can use, so must represent bandwidth in a less precise manner to mimic what
  741. * the scheduler hardware computes.
  742. */
  743. #define FS_BLOCK 1
  744. #define HS_BLOCK 4
  745. #define SS_BLOCK 16
  746. #define DMI_BLOCK 32
  747. /* Each device speed has a protocol overhead (CRC, bit stuffing, etc) associated
  748. * with each byte transferred. SuperSpeed devices have an initial overhead to
  749. * set up bursts. These are in blocks, see above. LS overhead has already been
  750. * translated into FS blocks.
  751. */
  752. #define DMI_OVERHEAD 8
  753. #define DMI_OVERHEAD_BURST 4
  754. #define SS_OVERHEAD 8
  755. #define SS_OVERHEAD_BURST 32
  756. #define HS_OVERHEAD 26
  757. #define FS_OVERHEAD 20
  758. #define LS_OVERHEAD 128
  759. /* The TTs need to claim roughly twice as much bandwidth (94 bytes per
  760. * microframe ~= 24Mbps) of the HS bus as the devices can actually use because
  761. * of overhead associated with split transfers crossing microframe boundaries.
  762. * 31 blocks is pure protocol overhead.
  763. */
  764. #define TT_HS_OVERHEAD (31 + 94)
  765. #define TT_DMI_OVERHEAD (25 + 12)
  766. /* Bandwidth limits in blocks */
  767. #define FS_BW_LIMIT 1285
  768. #define TT_BW_LIMIT 1320
  769. #define HS_BW_LIMIT 1607
  770. #define SS_BW_LIMIT_IN 3906
  771. #define DMI_BW_LIMIT_IN 3906
  772. #define SS_BW_LIMIT_OUT 3906
  773. #define DMI_BW_LIMIT_OUT 3906
  774. /* Percentage of bus bandwidth reserved for non-periodic transfers */
  775. #define FS_BW_RESERVED 10
  776. #define HS_BW_RESERVED 20
  777. #define SS_BW_RESERVED 10
  778. struct xhci_virt_ep {
  779. struct xhci_ring *ring;
  780. /* Related to endpoints that are configured to use stream IDs only */
  781. struct xhci_stream_info *stream_info;
  782. /* Temporary storage in case the configure endpoint command fails and we
  783. * have to restore the device state to the previous state
  784. */
  785. struct xhci_ring *new_ring;
  786. unsigned int ep_state;
  787. #define SET_DEQ_PENDING (1 << 0)
  788. #define EP_HALTED (1 << 1) /* For stall handling */
  789. #define EP_HALT_PENDING (1 << 2) /* For URB cancellation */
  790. /* Transitioning the endpoint to using streams, don't enqueue URBs */
  791. #define EP_GETTING_STREAMS (1 << 3)
  792. #define EP_HAS_STREAMS (1 << 4)
  793. /* Transitioning the endpoint to not using streams, don't enqueue URBs */
  794. #define EP_GETTING_NO_STREAMS (1 << 5)
  795. /* ---- Related to URB cancellation ---- */
  796. struct list_head cancelled_td_list;
  797. struct xhci_td *stopped_td;
  798. unsigned int stopped_stream;
  799. /* Watchdog timer for stop endpoint command to cancel URBs */
  800. struct timer_list stop_cmd_timer;
  801. int stop_cmds_pending;
  802. struct xhci_hcd *xhci;
  803. /* Dequeue pointer and dequeue segment for a submitted Set TR Dequeue
  804. * command. We'll need to update the ring's dequeue segment and dequeue
  805. * pointer after the command completes.
  806. */
  807. struct xhci_segment *queued_deq_seg;
  808. union xhci_trb *queued_deq_ptr;
  809. /*
  810. * Sometimes the xHC can not process isochronous endpoint ring quickly
  811. * enough, and it will miss some isoc tds on the ring and generate
  812. * a Missed Service Error Event.
  813. * Set skip flag when receive a Missed Service Error Event and
  814. * process the missed tds on the endpoint ring.
  815. */
  816. bool skip;
  817. /* Bandwidth checking storage */
  818. struct xhci_bw_info bw_info;
  819. struct list_head bw_endpoint_list;
  820. };
  821. enum xhci_overhead_type {
  822. LS_OVERHEAD_TYPE = 0,
  823. FS_OVERHEAD_TYPE,
  824. HS_OVERHEAD_TYPE,
  825. };
  826. struct xhci_interval_bw {
  827. unsigned int num_packets;
  828. /* Sorted by max packet size.
  829. * Head of the list is the greatest max packet size.
  830. */
  831. struct list_head endpoints;
  832. /* How many endpoints of each speed are present. */
  833. unsigned int overhead[3];
  834. };
  835. #define XHCI_MAX_INTERVAL 16
  836. struct xhci_interval_bw_table {
  837. unsigned int interval0_esit_payload;
  838. struct xhci_interval_bw interval_bw[XHCI_MAX_INTERVAL];
  839. /* Includes reserved bandwidth for async endpoints */
  840. unsigned int bw_used;
  841. unsigned int ss_bw_in;
  842. unsigned int ss_bw_out;
  843. };
  844. struct xhci_virt_device {
  845. struct usb_device *udev;
  846. /*
  847. * Commands to the hardware are passed an "input context" that
  848. * tells the hardware what to change in its data structures.
  849. * The hardware will return changes in an "output context" that
  850. * software must allocate for the hardware. We need to keep
  851. * track of input and output contexts separately because
  852. * these commands might fail and we don't trust the hardware.
  853. */
  854. struct xhci_container_ctx *out_ctx;
  855. /* Used for addressing devices and configuration changes */
  856. struct xhci_container_ctx *in_ctx;
  857. /* Rings saved to ensure old alt settings can be re-instated */
  858. struct xhci_ring **ring_cache;
  859. int num_rings_cached;
  860. #define XHCI_MAX_RINGS_CACHED 31
  861. struct xhci_virt_ep eps[31];
  862. struct completion cmd_completion;
  863. u8 fake_port;
  864. u8 real_port;
  865. struct xhci_interval_bw_table *bw_table;
  866. struct xhci_tt_bw_info *tt_info;
  867. /* The current max exit latency for the enabled USB3 link states. */
  868. u16 current_mel;
  869. };
  870. /*
  871. * For each roothub, keep track of the bandwidth information for each periodic
  872. * interval.
  873. *
  874. * If a high speed hub is attached to the roothub, each TT associated with that
  875. * hub is a separate bandwidth domain. The interval information for the
  876. * endpoints on the devices under that TT will appear in the TT structure.
  877. */
  878. struct xhci_root_port_bw_info {
  879. struct list_head tts;
  880. unsigned int num_active_tts;
  881. struct xhci_interval_bw_table bw_table;
  882. };
  883. struct xhci_tt_bw_info {
  884. struct list_head tt_list;
  885. int slot_id;
  886. int ttport;
  887. struct xhci_interval_bw_table bw_table;
  888. int active_eps;
  889. };
  890. /**
  891. * struct xhci_device_context_array
  892. * @dev_context_ptr array of 64-bit DMA addresses for device contexts
  893. */
  894. struct xhci_device_context_array {
  895. /* 64-bit device addresses; we only write 32-bit addresses */
  896. __le64 dev_context_ptrs[MAX_HC_SLOTS];
  897. /* private xHCD pointers */
  898. dma_addr_t dma;
  899. };
  900. /* TODO: write function to set the 64-bit device DMA address */
  901. /*
  902. * TODO: change this to be dynamically sized at HC mem init time since the HC
  903. * might not be able to handle the maximum number of devices possible.
  904. */
  905. struct xhci_transfer_event {
  906. /* 64-bit buffer address, or immediate data */
  907. __le64 buffer;
  908. __le32 transfer_len;
  909. /* This field is interpreted differently based on the type of TRB */
  910. __le32 flags;
  911. };
  912. /* Transfer event TRB length bit mask */
  913. /* bits 0:23 */
  914. #define EVENT_TRB_LEN(p) ((p) & 0xffffff)
  915. /** Transfer Event bit fields **/
  916. #define TRB_TO_EP_ID(p) (((p) >> 16) & 0x1f)
  917. /* Completion Code - only applicable for some types of TRBs */
  918. #define COMP_CODE_MASK (0xff << 24)
  919. #define GET_COMP_CODE(p) (((p) & COMP_CODE_MASK) >> 24)
  920. #define COMP_SUCCESS 1
  921. /* Data Buffer Error */
  922. #define COMP_DB_ERR 2
  923. /* Babble Detected Error */
  924. #define COMP_BABBLE 3
  925. /* USB Transaction Error */
  926. #define COMP_TX_ERR 4
  927. /* TRB Error - some TRB field is invalid */
  928. #define COMP_TRB_ERR 5
  929. /* Stall Error - USB device is stalled */
  930. #define COMP_STALL 6
  931. /* Resource Error - HC doesn't have memory for that device configuration */
  932. #define COMP_ENOMEM 7
  933. /* Bandwidth Error - not enough room in schedule for this dev config */
  934. #define COMP_BW_ERR 8
  935. /* No Slots Available Error - HC ran out of device slots */
  936. #define COMP_ENOSLOTS 9
  937. /* Invalid Stream Type Error */
  938. #define COMP_STREAM_ERR 10
  939. /* Slot Not Enabled Error - doorbell rung for disabled device slot */
  940. #define COMP_EBADSLT 11
  941. /* Endpoint Not Enabled Error */
  942. #define COMP_EBADEP 12
  943. /* Short Packet */
  944. #define COMP_SHORT_TX 13
  945. /* Ring Underrun - doorbell rung for an empty isoc OUT ep ring */
  946. #define COMP_UNDERRUN 14
  947. /* Ring Overrun - isoc IN ep ring is empty when ep is scheduled to RX */
  948. #define COMP_OVERRUN 15
  949. /* Virtual Function Event Ring Full Error */
  950. #define COMP_VF_FULL 16
  951. /* Parameter Error - Context parameter is invalid */
  952. #define COMP_EINVAL 17
  953. /* Bandwidth Overrun Error - isoc ep exceeded its allocated bandwidth */
  954. #define COMP_BW_OVER 18
  955. /* Context State Error - illegal context state transition requested */
  956. #define COMP_CTX_STATE 19
  957. /* No Ping Response Error - HC didn't get PING_RESPONSE in time to TX */
  958. #define COMP_PING_ERR 20
  959. /* Event Ring is full */
  960. #define COMP_ER_FULL 21
  961. /* Incompatible Device Error */
  962. #define COMP_DEV_ERR 22
  963. /* Missed Service Error - HC couldn't service an isoc ep within interval */
  964. #define COMP_MISSED_INT 23
  965. /* Successfully stopped command ring */
  966. #define COMP_CMD_STOP 24
  967. /* Successfully aborted current command and stopped command ring */
  968. #define COMP_CMD_ABORT 25
  969. /* Stopped - transfer was terminated by a stop endpoint command */
  970. #define COMP_STOP 26
  971. /* Same as COMP_EP_STOPPED, but the transferred length in the event is invalid */
  972. #define COMP_STOP_INVAL 27
  973. /* Control Abort Error - Debug Capability - control pipe aborted */
  974. #define COMP_DBG_ABORT 28
  975. /* Max Exit Latency Too Large Error */
  976. #define COMP_MEL_ERR 29
  977. /* TRB type 30 reserved */
  978. /* Isoc Buffer Overrun - an isoc IN ep sent more data than could fit in TD */
  979. #define COMP_BUFF_OVER 31
  980. /* Event Lost Error - xHC has an "internal event overrun condition" */
  981. #define COMP_ISSUES 32
  982. /* Undefined Error - reported when other error codes don't apply */
  983. #define COMP_UNKNOWN 33
  984. /* Invalid Stream ID Error */
  985. #define COMP_STRID_ERR 34
  986. /* Secondary Bandwidth Error - may be returned by a Configure Endpoint cmd */
  987. #define COMP_2ND_BW_ERR 35
  988. /* Split Transaction Error */
  989. #define COMP_SPLIT_ERR 36
  990. struct xhci_link_trb {
  991. /* 64-bit segment pointer*/
  992. __le64 segment_ptr;
  993. __le32 intr_target;
  994. __le32 control;
  995. };
  996. /* control bitfields */
  997. #define LINK_TOGGLE (0x1<<1)
  998. /* Command completion event TRB */
  999. struct xhci_event_cmd {
  1000. /* Pointer to command TRB, or the value passed by the event data trb */
  1001. __le64 cmd_trb;
  1002. __le32 status;
  1003. __le32 flags;
  1004. };
  1005. /* flags bitmasks */
  1006. /* Address device - disable SetAddress */
  1007. #define TRB_BSR (1<<9)
  1008. enum xhci_setup_dev {
  1009. SETUP_CONTEXT_ONLY,
  1010. SETUP_CONTEXT_ADDRESS,
  1011. };
  1012. /* bits 16:23 are the virtual function ID */
  1013. /* bits 24:31 are the slot ID */
  1014. #define TRB_TO_SLOT_ID(p) (((p) & (0xff<<24)) >> 24)
  1015. #define SLOT_ID_FOR_TRB(p) (((p) & 0xff) << 24)
  1016. /* Stop Endpoint TRB - ep_index to endpoint ID for this TRB */
  1017. #define TRB_TO_EP_INDEX(p) ((((p) & (0x1f << 16)) >> 16) - 1)
  1018. #define EP_ID_FOR_TRB(p) ((((p) + 1) & 0x1f) << 16)
  1019. #define SUSPEND_PORT_FOR_TRB(p) (((p) & 1) << 23)
  1020. #define TRB_TO_SUSPEND_PORT(p) (((p) & (1 << 23)) >> 23)
  1021. #define LAST_EP_INDEX 30
  1022. /* Set TR Dequeue Pointer command TRB fields, 6.4.3.9 */
  1023. #define TRB_TO_STREAM_ID(p) ((((p) & (0xffff << 16)) >> 16))
  1024. #define STREAM_ID_FOR_TRB(p) ((((p)) & 0xffff) << 16)
  1025. #define SCT_FOR_TRB(p) (((p) << 1) & 0x7)
  1026. /* Port Status Change Event TRB fields */
  1027. /* Port ID - bits 31:24 */
  1028. #define GET_PORT_ID(p) (((p) & (0xff << 24)) >> 24)
  1029. /* Normal TRB fields */
  1030. /* transfer_len bitmasks - bits 0:16 */
  1031. #define TRB_LEN(p) ((p) & 0x1ffff)
  1032. /* Interrupter Target - which MSI-X vector to target the completion event at */
  1033. #define TRB_INTR_TARGET(p) (((p) & 0x3ff) << 22)
  1034. #define GET_INTR_TARGET(p) (((p) >> 22) & 0x3ff)
  1035. #define TRB_TBC(p) (((p) & 0x3) << 7)
  1036. #define TRB_TLBPC(p) (((p) & 0xf) << 16)
  1037. /* Cycle bit - indicates TRB ownership by HC or HCD */
  1038. #define TRB_CYCLE (1<<0)
  1039. /*
  1040. * Force next event data TRB to be evaluated before task switch.
  1041. * Used to pass OS data back after a TD completes.
  1042. */
  1043. #define TRB_ENT (1<<1)
  1044. /* Interrupt on short packet */
  1045. #define TRB_ISP (1<<2)
  1046. /* Set PCIe no snoop attribute */
  1047. #define TRB_NO_SNOOP (1<<3)
  1048. /* Chain multiple TRBs into a TD */
  1049. #define TRB_CHAIN (1<<4)
  1050. /* Interrupt on completion */
  1051. #define TRB_IOC (1<<5)
  1052. /* The buffer pointer contains immediate data */
  1053. #define TRB_IDT (1<<6)
  1054. /* Block Event Interrupt */
  1055. #define TRB_BEI (1<<9)
  1056. /* Control transfer TRB specific fields */
  1057. #define TRB_DIR_IN (1<<16)
  1058. #define TRB_TX_TYPE(p) ((p) << 16)
  1059. #define TRB_DATA_OUT 2
  1060. #define TRB_DATA_IN 3
  1061. /* Isochronous TRB specific fields */
  1062. #define TRB_SIA (1<<31)
  1063. struct xhci_generic_trb {
  1064. __le32 field[4];
  1065. };
  1066. union xhci_trb {
  1067. struct xhci_link_trb link;
  1068. struct xhci_transfer_event trans_event;
  1069. struct xhci_event_cmd event_cmd;
  1070. struct xhci_generic_trb generic;
  1071. };
  1072. /* TRB bit mask */
  1073. #define TRB_TYPE_BITMASK (0xfc00)
  1074. #define TRB_TYPE(p) ((p) << 10)
  1075. #define TRB_FIELD_TO_TYPE(p) (((p) & TRB_TYPE_BITMASK) >> 10)
  1076. /* TRB type IDs */
  1077. /* bulk, interrupt, isoc scatter/gather, and control data stage */
  1078. #define TRB_NORMAL 1
  1079. /* setup stage for control transfers */
  1080. #define TRB_SETUP 2
  1081. /* data stage for control transfers */
  1082. #define TRB_DATA 3
  1083. /* status stage for control transfers */
  1084. #define TRB_STATUS 4
  1085. /* isoc transfers */
  1086. #define TRB_ISOC 5
  1087. /* TRB for linking ring segments */
  1088. #define TRB_LINK 6
  1089. #define TRB_EVENT_DATA 7
  1090. /* Transfer Ring No-op (not for the command ring) */
  1091. #define TRB_TR_NOOP 8
  1092. /* Command TRBs */
  1093. /* Enable Slot Command */
  1094. #define TRB_ENABLE_SLOT 9
  1095. /* Disable Slot Command */
  1096. #define TRB_DISABLE_SLOT 10
  1097. /* Address Device Command */
  1098. #define TRB_ADDR_DEV 11
  1099. /* Configure Endpoint Command */
  1100. #define TRB_CONFIG_EP 12
  1101. /* Evaluate Context Command */
  1102. #define TRB_EVAL_CONTEXT 13
  1103. /* Reset Endpoint Command */
  1104. #define TRB_RESET_EP 14
  1105. /* Stop Transfer Ring Command */
  1106. #define TRB_STOP_RING 15
  1107. /* Set Transfer Ring Dequeue Pointer Command */
  1108. #define TRB_SET_DEQ 16
  1109. /* Reset Device Command */
  1110. #define TRB_RESET_DEV 17
  1111. /* Force Event Command (opt) */
  1112. #define TRB_FORCE_EVENT 18
  1113. /* Negotiate Bandwidth Command (opt) */
  1114. #define TRB_NEG_BANDWIDTH 19
  1115. /* Set Latency Tolerance Value Command (opt) */
  1116. #define TRB_SET_LT 20
  1117. /* Get port bandwidth Command */
  1118. #define TRB_GET_BW 21
  1119. /* Force Header Command - generate a transaction or link management packet */
  1120. #define TRB_FORCE_HEADER 22
  1121. /* No-op Command - not for transfer rings */
  1122. #define TRB_CMD_NOOP 23
  1123. /* TRB IDs 24-31 reserved */
  1124. /* Event TRBS */
  1125. /* Transfer Event */
  1126. #define TRB_TRANSFER 32
  1127. /* Command Completion Event */
  1128. #define TRB_COMPLETION 33
  1129. /* Port Status Change Event */
  1130. #define TRB_PORT_STATUS 34
  1131. /* Bandwidth Request Event (opt) */
  1132. #define TRB_BANDWIDTH_EVENT 35
  1133. /* Doorbell Event (opt) */
  1134. #define TRB_DOORBELL 36
  1135. /* Host Controller Event */
  1136. #define TRB_HC_EVENT 37
  1137. /* Device Notification Event - device sent function wake notification */
  1138. #define TRB_DEV_NOTE 38
  1139. /* MFINDEX Wrap Event - microframe counter wrapped */
  1140. #define TRB_MFINDEX_WRAP 39
  1141. /* TRB IDs 40-47 reserved, 48-63 is vendor-defined */
  1142. /* Nec vendor-specific command completion event. */
  1143. #define TRB_NEC_CMD_COMP 48
  1144. /* Get NEC firmware revision. */
  1145. #define TRB_NEC_GET_FW 49
  1146. #define TRB_TYPE_LINK(x) (((x) & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK))
  1147. /* Above, but for __le32 types -- can avoid work by swapping constants: */
  1148. #define TRB_TYPE_LINK_LE32(x) (((x) & cpu_to_le32(TRB_TYPE_BITMASK)) == \
  1149. cpu_to_le32(TRB_TYPE(TRB_LINK)))
  1150. #define TRB_TYPE_NOOP_LE32(x) (((x) & cpu_to_le32(TRB_TYPE_BITMASK)) == \
  1151. cpu_to_le32(TRB_TYPE(TRB_TR_NOOP)))
  1152. #define NEC_FW_MINOR(p) (((p) >> 0) & 0xff)
  1153. #define NEC_FW_MAJOR(p) (((p) >> 8) & 0xff)
  1154. /*
  1155. * TRBS_PER_SEGMENT must be a multiple of 4,
  1156. * since the command ring is 64-byte aligned.
  1157. * It must also be greater than 16.
  1158. */
  1159. #define TRBS_PER_SEGMENT 64
  1160. /* Allow two commands + a link TRB, along with any reserved command TRBs */
  1161. #define MAX_RSVD_CMD_TRBS (TRBS_PER_SEGMENT - 3)
  1162. #define TRB_SEGMENT_SIZE (TRBS_PER_SEGMENT*16)
  1163. #define TRB_SEGMENT_SHIFT (ilog2(TRB_SEGMENT_SIZE))
  1164. /* TRB buffer pointers can't cross 64KB boundaries */
  1165. #define TRB_MAX_BUFF_SHIFT 16
  1166. #define TRB_MAX_BUFF_SIZE (1 << TRB_MAX_BUFF_SHIFT)
  1167. struct xhci_segment {
  1168. union xhci_trb *trbs;
  1169. /* private to HCD */
  1170. struct xhci_segment *next;
  1171. dma_addr_t dma;
  1172. };
  1173. struct xhci_td {
  1174. struct list_head td_list;
  1175. struct list_head cancelled_td_list;
  1176. struct urb *urb;
  1177. struct xhci_segment *start_seg;
  1178. union xhci_trb *first_trb;
  1179. union xhci_trb *last_trb;
  1180. /* actual_length of the URB has already been set */
  1181. bool urb_length_set;
  1182. };
  1183. /* xHCI command default timeout value */
  1184. #define XHCI_CMD_DEFAULT_TIMEOUT (5 * HZ)
  1185. /* command descriptor */
  1186. struct xhci_cd {
  1187. struct xhci_command *command;
  1188. union xhci_trb *cmd_trb;
  1189. };
  1190. struct xhci_dequeue_state {
  1191. struct xhci_segment *new_deq_seg;
  1192. union xhci_trb *new_deq_ptr;
  1193. int new_cycle_state;
  1194. };
  1195. enum xhci_ring_type {
  1196. TYPE_CTRL = 0,
  1197. TYPE_ISOC,
  1198. TYPE_BULK,
  1199. TYPE_INTR,
  1200. TYPE_STREAM,
  1201. TYPE_COMMAND,
  1202. TYPE_EVENT,
  1203. };
  1204. struct xhci_ring {
  1205. struct xhci_segment *first_seg;
  1206. struct xhci_segment *last_seg;
  1207. union xhci_trb *enqueue;
  1208. struct xhci_segment *enq_seg;
  1209. unsigned int enq_updates;
  1210. union xhci_trb *dequeue;
  1211. struct xhci_segment *deq_seg;
  1212. unsigned int deq_updates;
  1213. struct list_head td_list;
  1214. /*
  1215. * Write the cycle state into the TRB cycle field to give ownership of
  1216. * the TRB to the host controller (if we are the producer), or to check
  1217. * if we own the TRB (if we are the consumer). See section 4.9.1.
  1218. */
  1219. u32 cycle_state;
  1220. unsigned int stream_id;
  1221. unsigned int num_segs;
  1222. unsigned int num_trbs_free;
  1223. unsigned int num_trbs_free_temp;
  1224. enum xhci_ring_type type;
  1225. bool last_td_was_short;
  1226. struct radix_tree_root *trb_address_map;
  1227. };
  1228. struct xhci_erst_entry {
  1229. /* 64-bit event ring segment address */
  1230. __le64 seg_addr;
  1231. __le32 seg_size;
  1232. /* Set to zero */
  1233. __le32 rsvd;
  1234. };
  1235. struct xhci_erst {
  1236. struct xhci_erst_entry *entries;
  1237. unsigned int num_entries;
  1238. /* xhci->event_ring keeps track of segment dma addresses */
  1239. dma_addr_t erst_dma_addr;
  1240. /* Num entries the ERST can contain */
  1241. unsigned int erst_size;
  1242. };
  1243. struct xhci_scratchpad {
  1244. u64 *sp_array;
  1245. dma_addr_t sp_dma;
  1246. void **sp_buffers;
  1247. dma_addr_t *sp_dma_buffers;
  1248. };
  1249. struct urb_priv {
  1250. int length;
  1251. int td_cnt;
  1252. struct xhci_td *td[0];
  1253. };
  1254. /*
  1255. * Each segment table entry is 4*32bits long. 1K seems like an ok size:
  1256. * (1K bytes * 8bytes/bit) / (4*32 bits) = 64 segment entries in the table,
  1257. * meaning 64 ring segments.
  1258. * Initial allocated size of the ERST, in number of entries */
  1259. #define ERST_NUM_SEGS 1
  1260. /* Initial allocated size of the ERST, in number of entries */
  1261. #define ERST_SIZE 64
  1262. /* Initial number of event segment rings allocated */
  1263. #define ERST_ENTRIES 1
  1264. /* Poll every 60 seconds */
  1265. #define POLL_TIMEOUT 60
  1266. /* Stop endpoint command timeout (secs) for URB cancellation watchdog timer */
  1267. #define XHCI_STOP_EP_CMD_TIMEOUT 5
  1268. /* XXX: Make these module parameters */
  1269. struct s3_save {
  1270. u32 command;
  1271. u32 dev_nt;
  1272. u64 dcbaa_ptr;
  1273. u32 config_reg;
  1274. u32 irq_pending;
  1275. u32 irq_control;
  1276. u32 erst_size;
  1277. u64 erst_base;
  1278. u64 erst_dequeue;
  1279. };
  1280. /* Use for lpm */
  1281. struct dev_info {
  1282. u32 dev_id;
  1283. struct list_head list;
  1284. };
  1285. struct xhci_bus_state {
  1286. unsigned long bus_suspended;
  1287. unsigned long next_statechange;
  1288. /* Port suspend arrays are indexed by the portnum of the fake roothub */
  1289. /* ports suspend status arrays - max 31 ports for USB2, 15 for USB3 */
  1290. u32 port_c_suspend;
  1291. u32 suspended_ports;
  1292. u32 port_remote_wakeup;
  1293. unsigned long resume_done[USB_MAXCHILDREN];
  1294. /* which ports have started to resume */
  1295. unsigned long resuming_ports;
  1296. /* Which ports are waiting on RExit to U0 transition. */
  1297. unsigned long rexit_ports;
  1298. struct completion rexit_done[USB_MAXCHILDREN];
  1299. };
  1300. /*
  1301. * It can take up to 20 ms to transition from RExit to U0 on the
  1302. * Intel Lynx Point LP xHCI host.
  1303. */
  1304. #define XHCI_MAX_REXIT_TIMEOUT (20 * 1000)
  1305. static inline unsigned int hcd_index(struct usb_hcd *hcd)
  1306. {
  1307. if (hcd->speed == HCD_USB3)
  1308. return 0;
  1309. else
  1310. return 1;
  1311. }
  1312. /* There is one xhci_hcd structure per controller */
  1313. struct xhci_hcd {
  1314. struct usb_hcd *main_hcd;
  1315. struct usb_hcd *shared_hcd;
  1316. /* glue to PCI and HCD framework */
  1317. struct xhci_cap_regs __iomem *cap_regs;
  1318. struct xhci_op_regs __iomem *op_regs;
  1319. struct xhci_run_regs __iomem *run_regs;
  1320. struct xhci_doorbell_array __iomem *dba;
  1321. /* Our HCD's current interrupter register set */
  1322. struct xhci_intr_reg __iomem *ir_set;
  1323. /* Cached register copies of read-only HC data */
  1324. __u32 hcs_params1;
  1325. __u32 hcs_params2;
  1326. __u32 hcs_params3;
  1327. __u32 hcc_params;
  1328. spinlock_t lock;
  1329. /* packed release number */
  1330. u8 sbrn;
  1331. u16 hci_version;
  1332. u8 max_slots;
  1333. u8 max_interrupters;
  1334. u8 max_ports;
  1335. u8 isoc_threshold;
  1336. int event_ring_max;
  1337. int addr_64;
  1338. /* 4KB min, 128MB max */
  1339. int page_size;
  1340. /* Valid values are 12 to 20, inclusive */
  1341. int page_shift;
  1342. /* msi-x vectors */
  1343. int msix_count;
  1344. struct msix_entry *msix_entries;
  1345. /* optional clock */
  1346. struct clk *clk;
  1347. /* data structures */
  1348. struct xhci_device_context_array *dcbaa;
  1349. struct xhci_ring *cmd_ring;
  1350. unsigned int cmd_ring_state;
  1351. #define CMD_RING_STATE_RUNNING (1 << 0)
  1352. #define CMD_RING_STATE_ABORTED (1 << 1)
  1353. #define CMD_RING_STATE_STOPPED (1 << 2)
  1354. struct list_head cmd_list;
  1355. unsigned int cmd_ring_reserved_trbs;
  1356. struct timer_list cmd_timer;
  1357. struct xhci_command *current_cmd;
  1358. struct xhci_ring *event_ring;
  1359. struct xhci_erst erst;
  1360. /* Scratchpad */
  1361. struct xhci_scratchpad *scratchpad;
  1362. /* Store LPM test failed devices' information */
  1363. struct list_head lpm_failed_devs;
  1364. /* slot enabling and address device helpers */
  1365. struct completion addr_dev;
  1366. int slot_id;
  1367. /* For USB 3.0 LPM enable/disable. */
  1368. struct xhci_command *lpm_command;
  1369. /* Internal mirror of the HW's dcbaa */
  1370. struct xhci_virt_device *devs[MAX_HC_SLOTS];
  1371. /* For keeping track of bandwidth domains per roothub. */
  1372. struct xhci_root_port_bw_info *rh_bw;
  1373. /* DMA pools */
  1374. struct dma_pool *device_pool;
  1375. struct dma_pool *segment_pool;
  1376. struct dma_pool *small_streams_pool;
  1377. struct dma_pool *medium_streams_pool;
  1378. /* Host controller watchdog timer structures */
  1379. unsigned int xhc_state;
  1380. u32 command;
  1381. struct s3_save s3;
  1382. /* Host controller is dying - not responding to commands. "I'm not dead yet!"
  1383. *
  1384. * xHC interrupts have been disabled and a watchdog timer will (or has already)
  1385. * halt the xHCI host, and complete all URBs with an -ESHUTDOWN code. Any code
  1386. * that sees this status (other than the timer that set it) should stop touching
  1387. * hardware immediately. Interrupt handlers should return immediately when
  1388. * they see this status (any time they drop and re-acquire xhci->lock).
  1389. * xhci_urb_dequeue() should call usb_hcd_check_unlink_urb() and return without
  1390. * putting the TD on the canceled list, etc.
  1391. *
  1392. * There are no reports of xHCI host controllers that display this issue.
  1393. */
  1394. #define XHCI_STATE_DYING (1 << 0)
  1395. #define XHCI_STATE_HALTED (1 << 1)
  1396. /* Statistics */
  1397. int error_bitmask;
  1398. unsigned int quirks;
  1399. #define XHCI_LINK_TRB_QUIRK (1 << 0)
  1400. #define XHCI_RESET_EP_QUIRK (1 << 1)
  1401. #define XHCI_NEC_HOST (1 << 2)
  1402. #define XHCI_AMD_PLL_FIX (1 << 3)
  1403. #define XHCI_SPURIOUS_SUCCESS (1 << 4)
  1404. /*
  1405. * Certain Intel host controllers have a limit to the number of endpoint
  1406. * contexts they can handle. Ideally, they would signal that they can't handle
  1407. * anymore endpoint contexts by returning a Resource Error for the Configure
  1408. * Endpoint command, but they don't. Instead they expect software to keep track
  1409. * of the number of active endpoints for them, across configure endpoint
  1410. * commands, reset device commands, disable slot commands, and address device
  1411. * commands.
  1412. */
  1413. #define XHCI_EP_LIMIT_QUIRK (1 << 5)
  1414. #define XHCI_BROKEN_MSI (1 << 6)
  1415. #define XHCI_RESET_ON_RESUME (1 << 7)
  1416. #define XHCI_SW_BW_CHECKING (1 << 8)
  1417. #define XHCI_AMD_0x96_HOST (1 << 9)
  1418. #define XHCI_TRUST_TX_LENGTH (1 << 10)
  1419. #define XHCI_LPM_SUPPORT (1 << 11)
  1420. #define XHCI_INTEL_HOST (1 << 12)
  1421. #define XHCI_SPURIOUS_REBOOT (1 << 13)
  1422. #define XHCI_COMP_MODE_QUIRK (1 << 14)
  1423. #define XHCI_AVOID_BEI (1 << 15)
  1424. #define XHCI_PLAT (1 << 16)
  1425. #define XHCI_SLOW_SUSPEND (1 << 17)
  1426. #define XHCI_SPURIOUS_WAKEUP (1 << 18)
  1427. /* For controllers with a broken beyond repair streams implementation */
  1428. #define XHCI_BROKEN_STREAMS (1 << 19)
  1429. #define XHCI_PME_STUCK_QUIRK (1 << 20)
  1430. unsigned int num_active_eps;
  1431. unsigned int limit_active_eps;
  1432. /* There are two roothubs to keep track of bus suspend info for */
  1433. struct xhci_bus_state bus_state[2];
  1434. /* Is each xHCI roothub port a USB 3.0, USB 2.0, or USB 1.1 port? */
  1435. u8 *port_array;
  1436. /* Array of pointers to USB 3.0 PORTSC registers */
  1437. __le32 __iomem **usb3_ports;
  1438. unsigned int num_usb3_ports;
  1439. /* Array of pointers to USB 2.0 PORTSC registers */
  1440. __le32 __iomem **usb2_ports;
  1441. unsigned int num_usb2_ports;
  1442. /* support xHCI 0.96 spec USB2 software LPM */
  1443. unsigned sw_lpm_support:1;
  1444. /* support xHCI 1.0 spec USB2 hardware LPM */
  1445. unsigned hw_lpm_support:1;
  1446. /* cached usb2 extened protocol capabilites */
  1447. u32 *ext_caps;
  1448. unsigned int num_ext_caps;
  1449. /* Compliance Mode Recovery Data */
  1450. struct timer_list comp_mode_recovery_timer;
  1451. u32 port_status_u0;
  1452. /* Compliance Mode Timer Triggered every 2 seconds */
  1453. #define COMP_MODE_RCVRY_MSECS 2000
  1454. };
  1455. /* convert between an HCD pointer and the corresponding EHCI_HCD */
  1456. static inline struct xhci_hcd *hcd_to_xhci(struct usb_hcd *hcd)
  1457. {
  1458. return *((struct xhci_hcd **) (hcd->hcd_priv));
  1459. }
  1460. static inline struct usb_hcd *xhci_to_hcd(struct xhci_hcd *xhci)
  1461. {
  1462. return xhci->main_hcd;
  1463. }
  1464. #define xhci_dbg(xhci, fmt, args...) \
  1465. dev_dbg(xhci_to_hcd(xhci)->self.controller , fmt , ## args)
  1466. #define xhci_err(xhci, fmt, args...) \
  1467. dev_err(xhci_to_hcd(xhci)->self.controller , fmt , ## args)
  1468. #define xhci_warn(xhci, fmt, args...) \
  1469. dev_warn(xhci_to_hcd(xhci)->self.controller , fmt , ## args)
  1470. #define xhci_warn_ratelimited(xhci, fmt, args...) \
  1471. dev_warn_ratelimited(xhci_to_hcd(xhci)->self.controller , fmt , ## args)
  1472. #define xhci_info(xhci, fmt, args...) \
  1473. dev_info(xhci_to_hcd(xhci)->self.controller , fmt , ## args)
  1474. /*
  1475. * Registers should always be accessed with double word or quad word accesses.
  1476. *
  1477. * Some xHCI implementations may support 64-bit address pointers. Registers
  1478. * with 64-bit address pointers should be written to with dword accesses by
  1479. * writing the low dword first (ptr[0]), then the high dword (ptr[1]) second.
  1480. * xHCI implementations that do not support 64-bit address pointers will ignore
  1481. * the high dword, and write order is irrelevant.
  1482. */
  1483. static inline u64 xhci_read_64(const struct xhci_hcd *xhci,
  1484. __le64 __iomem *regs)
  1485. {
  1486. __u32 __iomem *ptr = (__u32 __iomem *) regs;
  1487. u64 val_lo = readl(ptr);
  1488. u64 val_hi = readl(ptr + 1);
  1489. return val_lo + (val_hi << 32);
  1490. }
  1491. static inline void xhci_write_64(struct xhci_hcd *xhci,
  1492. const u64 val, __le64 __iomem *regs)
  1493. {
  1494. __u32 __iomem *ptr = (__u32 __iomem *) regs;
  1495. u32 val_lo = lower_32_bits(val);
  1496. u32 val_hi = upper_32_bits(val);
  1497. writel(val_lo, ptr);
  1498. writel(val_hi, ptr + 1);
  1499. }
  1500. static inline int xhci_link_trb_quirk(struct xhci_hcd *xhci)
  1501. {
  1502. return xhci->quirks & XHCI_LINK_TRB_QUIRK;
  1503. }
  1504. /* xHCI debugging */
  1505. void xhci_print_ir_set(struct xhci_hcd *xhci, int set_num);
  1506. void xhci_print_registers(struct xhci_hcd *xhci);
  1507. void xhci_dbg_regs(struct xhci_hcd *xhci);
  1508. void xhci_print_run_regs(struct xhci_hcd *xhci);
  1509. void xhci_print_trb_offsets(struct xhci_hcd *xhci, union xhci_trb *trb);
  1510. void xhci_debug_trb(struct xhci_hcd *xhci, union xhci_trb *trb);
  1511. void xhci_debug_segment(struct xhci_hcd *xhci, struct xhci_segment *seg);
  1512. void xhci_debug_ring(struct xhci_hcd *xhci, struct xhci_ring *ring);
  1513. void xhci_dbg_erst(struct xhci_hcd *xhci, struct xhci_erst *erst);
  1514. void xhci_dbg_cmd_ptrs(struct xhci_hcd *xhci);
  1515. void xhci_dbg_ring_ptrs(struct xhci_hcd *xhci, struct xhci_ring *ring);
  1516. void xhci_dbg_ctx(struct xhci_hcd *xhci, struct xhci_container_ctx *ctx, unsigned int last_ep);
  1517. char *xhci_get_slot_state(struct xhci_hcd *xhci,
  1518. struct xhci_container_ctx *ctx);
  1519. void xhci_dbg_ep_rings(struct xhci_hcd *xhci,
  1520. unsigned int slot_id, unsigned int ep_index,
  1521. struct xhci_virt_ep *ep);
  1522. void xhci_dbg_trace(struct xhci_hcd *xhci, void (*trace)(struct va_format *),
  1523. const char *fmt, ...);
  1524. /* xHCI memory management */
  1525. void xhci_mem_cleanup(struct xhci_hcd *xhci);
  1526. int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags);
  1527. void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id);
  1528. int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id, struct usb_device *udev, gfp_t flags);
  1529. int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev);
  1530. void xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd *xhci,
  1531. struct usb_device *udev);
  1532. unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc);
  1533. unsigned int xhci_get_endpoint_address(unsigned int ep_index);
  1534. unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc);
  1535. unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index);
  1536. unsigned int xhci_last_valid_endpoint(u32 added_ctxs);
  1537. void xhci_endpoint_zero(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev, struct usb_host_endpoint *ep);
  1538. void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
  1539. struct xhci_bw_info *ep_bw,
  1540. struct xhci_interval_bw_table *bw_table,
  1541. struct usb_device *udev,
  1542. struct xhci_virt_ep *virt_ep,
  1543. struct xhci_tt_bw_info *tt_info);
  1544. void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
  1545. struct xhci_virt_device *virt_dev,
  1546. int old_active_eps);
  1547. void xhci_clear_endpoint_bw_info(struct xhci_bw_info *bw_info);
  1548. void xhci_update_bw_info(struct xhci_hcd *xhci,
  1549. struct xhci_container_ctx *in_ctx,
  1550. struct xhci_input_control_ctx *ctrl_ctx,
  1551. struct xhci_virt_device *virt_dev);
  1552. void xhci_endpoint_copy(struct xhci_hcd *xhci,
  1553. struct xhci_container_ctx *in_ctx,
  1554. struct xhci_container_ctx *out_ctx,
  1555. unsigned int ep_index);
  1556. void xhci_slot_copy(struct xhci_hcd *xhci,
  1557. struct xhci_container_ctx *in_ctx,
  1558. struct xhci_container_ctx *out_ctx);
  1559. int xhci_endpoint_init(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev,
  1560. struct usb_device *udev, struct usb_host_endpoint *ep,
  1561. gfp_t mem_flags);
  1562. void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring);
  1563. int xhci_ring_expansion(struct xhci_hcd *xhci, struct xhci_ring *ring,
  1564. unsigned int num_trbs, gfp_t flags);
  1565. void xhci_free_or_cache_endpoint_ring(struct xhci_hcd *xhci,
  1566. struct xhci_virt_device *virt_dev,
  1567. unsigned int ep_index);
  1568. struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
  1569. unsigned int num_stream_ctxs,
  1570. unsigned int num_streams, gfp_t flags);
  1571. void xhci_free_stream_info(struct xhci_hcd *xhci,
  1572. struct xhci_stream_info *stream_info);
  1573. void xhci_setup_streams_ep_input_ctx(struct xhci_hcd *xhci,
  1574. struct xhci_ep_ctx *ep_ctx,
  1575. struct xhci_stream_info *stream_info);
  1576. void xhci_setup_no_streams_ep_input_ctx(struct xhci_ep_ctx *ep_ctx,
  1577. struct xhci_virt_ep *ep);
  1578. void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
  1579. struct xhci_virt_device *virt_dev, bool drop_control_ep);
  1580. struct xhci_ring *xhci_dma_to_transfer_ring(
  1581. struct xhci_virt_ep *ep,
  1582. u64 address);
  1583. struct xhci_ring *xhci_stream_id_to_ring(
  1584. struct xhci_virt_device *dev,
  1585. unsigned int ep_index,
  1586. unsigned int stream_id);
  1587. struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
  1588. bool allocate_in_ctx, bool allocate_completion,
  1589. gfp_t mem_flags);
  1590. void xhci_urb_free_priv(struct urb_priv *urb_priv);
  1591. void xhci_free_command(struct xhci_hcd *xhci,
  1592. struct xhci_command *command);
  1593. /* xHCI host controller glue */
  1594. typedef void (*xhci_get_quirks_t)(struct device *, struct xhci_hcd *);
  1595. int xhci_handshake(void __iomem *ptr, u32 mask, u32 done, int usec);
  1596. void xhci_quiesce(struct xhci_hcd *xhci);
  1597. int xhci_halt(struct xhci_hcd *xhci);
  1598. int xhci_reset(struct xhci_hcd *xhci);
  1599. int xhci_init(struct usb_hcd *hcd);
  1600. int xhci_run(struct usb_hcd *hcd);
  1601. void xhci_stop(struct usb_hcd *hcd);
  1602. void xhci_shutdown(struct usb_hcd *hcd);
  1603. int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks);
  1604. void xhci_init_driver(struct hc_driver *drv, int (*setup_fn)(struct usb_hcd *));
  1605. #ifdef CONFIG_PM
  1606. int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup);
  1607. int xhci_resume(struct xhci_hcd *xhci, bool hibernated);
  1608. #else
  1609. #define xhci_suspend NULL
  1610. #define xhci_resume NULL
  1611. #endif
  1612. int xhci_get_frame(struct usb_hcd *hcd);
  1613. irqreturn_t xhci_irq(struct usb_hcd *hcd);
  1614. irqreturn_t xhci_msi_irq(int irq, void *hcd);
  1615. int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev);
  1616. void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev);
  1617. int xhci_alloc_tt_info(struct xhci_hcd *xhci,
  1618. struct xhci_virt_device *virt_dev,
  1619. struct usb_device *hdev,
  1620. struct usb_tt *tt, gfp_t mem_flags);
  1621. int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  1622. struct usb_host_endpoint **eps, unsigned int num_eps,
  1623. unsigned int num_streams, gfp_t mem_flags);
  1624. int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  1625. struct usb_host_endpoint **eps, unsigned int num_eps,
  1626. gfp_t mem_flags);
  1627. int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev);
  1628. int xhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev);
  1629. int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev);
  1630. int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
  1631. struct usb_device *udev, int enable);
  1632. int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
  1633. struct usb_tt *tt, gfp_t mem_flags);
  1634. int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags);
  1635. int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
  1636. int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev, struct usb_host_endpoint *ep);
  1637. int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev, struct usb_host_endpoint *ep);
  1638. void xhci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  1639. int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev);
  1640. int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev);
  1641. void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev);
  1642. /* xHCI ring, segment, TRB, and TD functions */
  1643. dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg, union xhci_trb *trb);
  1644. struct xhci_segment *trb_in_td(struct xhci_hcd *xhci,
  1645. struct xhci_segment *start_seg, union xhci_trb *start_trb,
  1646. union xhci_trb *end_trb, dma_addr_t suspect_dma, bool debug);
  1647. int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code);
  1648. void xhci_ring_cmd_db(struct xhci_hcd *xhci);
  1649. int xhci_queue_slot_control(struct xhci_hcd *xhci, struct xhci_command *cmd,
  1650. u32 trb_type, u32 slot_id);
  1651. int xhci_queue_address_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
  1652. dma_addr_t in_ctx_ptr, u32 slot_id, enum xhci_setup_dev);
  1653. int xhci_queue_vendor_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
  1654. u32 field1, u32 field2, u32 field3, u32 field4);
  1655. int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, struct xhci_command *cmd,
  1656. int slot_id, unsigned int ep_index, int suspend);
  1657. int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb,
  1658. int slot_id, unsigned int ep_index);
  1659. int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb,
  1660. int slot_id, unsigned int ep_index);
  1661. int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb,
  1662. int slot_id, unsigned int ep_index);
  1663. int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
  1664. struct urb *urb, int slot_id, unsigned int ep_index);
  1665. int xhci_queue_configure_endpoint(struct xhci_hcd *xhci,
  1666. struct xhci_command *cmd, dma_addr_t in_ctx_ptr, u32 slot_id,
  1667. bool command_must_succeed);
  1668. int xhci_queue_evaluate_context(struct xhci_hcd *xhci, struct xhci_command *cmd,
  1669. dma_addr_t in_ctx_ptr, u32 slot_id, bool command_must_succeed);
  1670. int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd,
  1671. int slot_id, unsigned int ep_index);
  1672. int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
  1673. u32 slot_id);
  1674. void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
  1675. unsigned int slot_id, unsigned int ep_index,
  1676. unsigned int stream_id, struct xhci_td *cur_td,
  1677. struct xhci_dequeue_state *state);
  1678. void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
  1679. unsigned int slot_id, unsigned int ep_index,
  1680. unsigned int stream_id,
  1681. struct xhci_dequeue_state *deq_state);
  1682. void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
  1683. unsigned int ep_index, struct xhci_td *td);
  1684. void xhci_queue_config_ep_quirk(struct xhci_hcd *xhci,
  1685. unsigned int slot_id, unsigned int ep_index,
  1686. struct xhci_dequeue_state *deq_state);
  1687. void xhci_stop_endpoint_command_watchdog(unsigned long arg);
  1688. void xhci_handle_command_timeout(unsigned long data);
  1689. void xhci_ring_ep_doorbell(struct xhci_hcd *xhci, unsigned int slot_id,
  1690. unsigned int ep_index, unsigned int stream_id);
  1691. void xhci_cleanup_command_queue(struct xhci_hcd *xhci);
  1692. /* xHCI roothub code */
  1693. void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  1694. int port_id, u32 link_state);
  1695. int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
  1696. struct usb_device *udev, enum usb3_link_state state);
  1697. int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
  1698. struct usb_device *udev, enum usb3_link_state state);
  1699. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  1700. int port_id, u32 port_bit);
  1701. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex,
  1702. char *buf, u16 wLength);
  1703. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf);
  1704. int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1);
  1705. #ifdef CONFIG_PM
  1706. int xhci_bus_suspend(struct usb_hcd *hcd);
  1707. int xhci_bus_resume(struct usb_hcd *hcd);
  1708. #else
  1709. #define xhci_bus_suspend NULL
  1710. #define xhci_bus_resume NULL
  1711. #endif /* CONFIG_PM */
  1712. u32 xhci_port_state_to_neutral(u32 state);
  1713. int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  1714. u16 port);
  1715. void xhci_ring_device(struct xhci_hcd *xhci, int slot_id);
  1716. /* xHCI contexts */
  1717. struct xhci_input_control_ctx *xhci_get_input_control_ctx(struct xhci_container_ctx *ctx);
  1718. struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_hcd *xhci, struct xhci_container_ctx *ctx);
  1719. struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci, struct xhci_container_ctx *ctx, unsigned int ep_index);
  1720. #endif /* __LINUX_XHCI_HCD_H */