xhci.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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 "../core/hcd.h"
  28. /* Code sharing between pci-quirks and xhci hcd */
  29. #include "xhci-ext-caps.h"
  30. /* xHCI PCI Configuration Registers */
  31. #define XHCI_SBRN_OFFSET (0x60)
  32. /* Max number of USB devices for any host controller - limit in section 6.1 */
  33. #define MAX_HC_SLOTS 256
  34. /* Section 5.3.3 - MaxPorts */
  35. #define MAX_HC_PORTS 127
  36. /*
  37. * xHCI register interface.
  38. * This corresponds to the eXtensible Host Controller Interface (xHCI)
  39. * Revision 0.95 specification
  40. */
  41. /**
  42. * struct xhci_cap_regs - xHCI Host Controller Capability Registers.
  43. * @hc_capbase: length of the capabilities register and HC version number
  44. * @hcs_params1: HCSPARAMS1 - Structural Parameters 1
  45. * @hcs_params2: HCSPARAMS2 - Structural Parameters 2
  46. * @hcs_params3: HCSPARAMS3 - Structural Parameters 3
  47. * @hcc_params: HCCPARAMS - Capability Parameters
  48. * @db_off: DBOFF - Doorbell array offset
  49. * @run_regs_off: RTSOFF - Runtime register space offset
  50. */
  51. struct xhci_cap_regs {
  52. u32 hc_capbase;
  53. u32 hcs_params1;
  54. u32 hcs_params2;
  55. u32 hcs_params3;
  56. u32 hcc_params;
  57. u32 db_off;
  58. u32 run_regs_off;
  59. /* Reserved up to (CAPLENGTH - 0x1C) */
  60. };
  61. /* hc_capbase bitmasks */
  62. /* bits 7:0 - how long is the Capabilities register */
  63. #define HC_LENGTH(p) XHCI_HC_LENGTH(p)
  64. /* bits 31:16 */
  65. #define HC_VERSION(p) (((p) >> 16) & 0xffff)
  66. /* HCSPARAMS1 - hcs_params1 - bitmasks */
  67. /* bits 0:7, Max Device Slots */
  68. #define HCS_MAX_SLOTS(p) (((p) >> 0) & 0xff)
  69. #define HCS_SLOTS_MASK 0xff
  70. /* bits 8:18, Max Interrupters */
  71. #define HCS_MAX_INTRS(p) (((p) >> 8) & 0x7ff)
  72. /* bits 24:31, Max Ports - max value is 0x7F = 127 ports */
  73. #define HCS_MAX_PORTS(p) (((p) >> 24) & 0x7f)
  74. /* HCSPARAMS2 - hcs_params2 - bitmasks */
  75. /* bits 0:3, frames or uframes that SW needs to queue transactions
  76. * ahead of the HW to meet periodic deadlines */
  77. #define HCS_IST(p) (((p) >> 0) & 0xf)
  78. /* bits 4:7, max number of Event Ring segments */
  79. #define HCS_ERST_MAX(p) (((p) >> 4) & 0xf)
  80. /* bit 26 Scratchpad restore - for save/restore HW state - not used yet */
  81. /* bits 27:31 number of Scratchpad buffers SW must allocate for the HW */
  82. /* HCSPARAMS3 - hcs_params3 - bitmasks */
  83. /* bits 0:7, Max U1 to U0 latency for the roothub ports */
  84. #define HCS_U1_LATENCY(p) (((p) >> 0) & 0xff)
  85. /* bits 16:31, Max U2 to U0 latency for the roothub ports */
  86. #define HCS_U2_LATENCY(p) (((p) >> 16) & 0xffff)
  87. /* HCCPARAMS - hcc_params - bitmasks */
  88. /* true: HC can use 64-bit address pointers */
  89. #define HCC_64BIT_ADDR(p) ((p) & (1 << 0))
  90. /* true: HC can do bandwidth negotiation */
  91. #define HCC_BANDWIDTH_NEG(p) ((p) & (1 << 1))
  92. /* true: HC uses 64-byte Device Context structures
  93. * FIXME 64-byte context structures aren't supported yet.
  94. */
  95. #define HCC_64BYTE_CONTEXT(p) ((p) & (1 << 2))
  96. /* true: HC has port power switches */
  97. #define HCC_PPC(p) ((p) & (1 << 3))
  98. /* true: HC has port indicators */
  99. #define HCS_INDICATOR(p) ((p) & (1 << 4))
  100. /* true: HC has Light HC Reset Capability */
  101. #define HCC_LIGHT_RESET(p) ((p) & (1 << 5))
  102. /* true: HC supports latency tolerance messaging */
  103. #define HCC_LTC(p) ((p) & (1 << 6))
  104. /* true: no secondary Stream ID Support */
  105. #define HCC_NSS(p) ((p) & (1 << 7))
  106. /* Max size for Primary Stream Arrays - 2^(n+1), where n is bits 12:15 */
  107. #define HCC_MAX_PSA (1 << ((((p) >> 12) & 0xf) + 1))
  108. /* Extended Capabilities pointer from PCI base - section 5.3.6 */
  109. #define HCC_EXT_CAPS(p) XHCI_HCC_EXT_CAPS(p)
  110. /* db_off bitmask - bits 0:1 reserved */
  111. #define DBOFF_MASK (~0x3)
  112. /* run_regs_off bitmask - bits 0:4 reserved */
  113. #define RTSOFF_MASK (~0x1f)
  114. /* Number of registers per port */
  115. #define NUM_PORT_REGS 4
  116. /**
  117. * struct xhci_op_regs - xHCI Host Controller Operational Registers.
  118. * @command: USBCMD - xHC command register
  119. * @status: USBSTS - xHC status register
  120. * @page_size: This indicates the page size that the host controller
  121. * supports. If bit n is set, the HC supports a page size
  122. * of 2^(n+12), up to a 128MB page size.
  123. * 4K is the minimum page size.
  124. * @cmd_ring: CRP - 64-bit Command Ring Pointer
  125. * @dcbaa_ptr: DCBAAP - 64-bit Device Context Base Address Array Pointer
  126. * @config_reg: CONFIG - Configure Register
  127. * @port_status_base: PORTSCn - base address for Port Status and Control
  128. * Each port has a Port Status and Control register,
  129. * followed by a Port Power Management Status and Control
  130. * register, a Port Link Info register, and a reserved
  131. * register.
  132. * @port_power_base: PORTPMSCn - base address for
  133. * Port Power Management Status and Control
  134. * @port_link_base: PORTLIn - base address for Port Link Info (current
  135. * Link PM state and control) for USB 2.1 and USB 3.0
  136. * devices.
  137. */
  138. struct xhci_op_regs {
  139. u32 command;
  140. u32 status;
  141. u32 page_size;
  142. u32 reserved1;
  143. u32 reserved2;
  144. u32 dev_notification;
  145. u64 cmd_ring;
  146. /* rsvd: offset 0x20-2F */
  147. u32 reserved3[4];
  148. u64 dcbaa_ptr;
  149. u32 config_reg;
  150. /* rsvd: offset 0x3C-3FF */
  151. u32 reserved4[241];
  152. /* port 1 registers, which serve as a base address for other ports */
  153. u32 port_status_base;
  154. u32 port_power_base;
  155. u32 port_link_base;
  156. u32 reserved5;
  157. /* registers for ports 2-255 */
  158. u32 reserved6[NUM_PORT_REGS*254];
  159. };
  160. /* USBCMD - USB command - command bitmasks */
  161. /* start/stop HC execution - do not write unless HC is halted*/
  162. #define CMD_RUN XHCI_CMD_RUN
  163. /* Reset HC - resets internal HC state machine and all registers (except
  164. * PCI config regs). HC does NOT drive a USB reset on the downstream ports.
  165. * The xHCI driver must reinitialize the xHC after setting this bit.
  166. */
  167. #define CMD_RESET (1 << 1)
  168. /* Event Interrupt Enable - a '1' allows interrupts from the host controller */
  169. #define CMD_EIE XHCI_CMD_EIE
  170. /* Host System Error Interrupt Enable - get out-of-band signal for HC errors */
  171. #define CMD_HSEIE XHCI_CMD_HSEIE
  172. /* bits 4:6 are reserved (and should be preserved on writes). */
  173. /* light reset (port status stays unchanged) - reset completed when this is 0 */
  174. #define CMD_LRESET (1 << 7)
  175. /* FIXME: ignoring host controller save/restore state for now. */
  176. #define CMD_CSS (1 << 8)
  177. #define CMD_CRS (1 << 9)
  178. /* Enable Wrap Event - '1' means xHC generates an event when MFINDEX wraps. */
  179. #define CMD_EWE XHCI_CMD_EWE
  180. /* MFINDEX power management - '1' means xHC can stop MFINDEX counter if all root
  181. * hubs are in U3 (selective suspend), disconnect, disabled, or powered-off.
  182. * '0' means the xHC can power it off if all ports are in the disconnect,
  183. * disabled, or powered-off state.
  184. */
  185. #define CMD_PM_INDEX (1 << 11)
  186. /* bits 12:31 are reserved (and should be preserved on writes). */
  187. /* USBSTS - USB status - status bitmasks */
  188. /* HC not running - set to 1 when run/stop bit is cleared. */
  189. #define STS_HALT XHCI_STS_HALT
  190. /* serious error, e.g. PCI parity error. The HC will clear the run/stop bit. */
  191. #define STS_FATAL (1 << 2)
  192. /* event interrupt - clear this prior to clearing any IP flags in IR set*/
  193. #define STS_EINT (1 << 3)
  194. /* port change detect */
  195. #define STS_PORT (1 << 4)
  196. /* bits 5:7 reserved and zeroed */
  197. /* save state status - '1' means xHC is saving state */
  198. #define STS_SAVE (1 << 8)
  199. /* restore state status - '1' means xHC is restoring state */
  200. #define STS_RESTORE (1 << 9)
  201. /* true: save or restore error */
  202. #define STS_SRE (1 << 10)
  203. /* true: Controller Not Ready to accept doorbell or op reg writes after reset */
  204. #define STS_CNR XHCI_STS_CNR
  205. /* true: internal Host Controller Error - SW needs to reset and reinitialize */
  206. #define STS_HCE (1 << 12)
  207. /* bits 13:31 reserved and should be preserved */
  208. /*
  209. * DNCTRL - Device Notification Control Register - dev_notification bitmasks
  210. * Generate a device notification event when the HC sees a transaction with a
  211. * notification type that matches a bit set in this bit field.
  212. */
  213. #define DEV_NOTE_MASK (0xffff)
  214. #define ENABLE_DEV_NOTE(x) (1 << x)
  215. /* Most of the device notification types should only be used for debug.
  216. * SW does need to pay attention to function wake notifications.
  217. */
  218. #define DEV_NOTE_FWAKE ENABLE_DEV_NOTE(1)
  219. /* CRCR - Command Ring Control Register - cmd_ring bitmasks */
  220. /* bit 0 is the command ring cycle state */
  221. /* stop ring operation after completion of the currently executing command */
  222. #define CMD_RING_PAUSE (1 << 1)
  223. /* stop ring immediately - abort the currently executing command */
  224. #define CMD_RING_ABORT (1 << 2)
  225. /* true: command ring is running */
  226. #define CMD_RING_RUNNING (1 << 3)
  227. /* bits 4:5 reserved and should be preserved */
  228. /* Command Ring pointer - bit mask for the lower 32 bits. */
  229. #define CMD_RING_RSVD_BITS (0x3f)
  230. /* CONFIG - Configure Register - config_reg bitmasks */
  231. /* bits 0:7 - maximum number of device slots enabled (NumSlotsEn) */
  232. #define MAX_DEVS(p) ((p) & 0xff)
  233. /* bits 8:31 - reserved and should be preserved */
  234. /* PORTSC - Port Status and Control Register - port_status_base bitmasks */
  235. /* true: device connected */
  236. #define PORT_CONNECT (1 << 0)
  237. /* true: port enabled */
  238. #define PORT_PE (1 << 1)
  239. /* bit 2 reserved and zeroed */
  240. /* true: port has an over-current condition */
  241. #define PORT_OC (1 << 3)
  242. /* true: port reset signaling asserted */
  243. #define PORT_RESET (1 << 4)
  244. /* Port Link State - bits 5:8
  245. * A read gives the current link PM state of the port,
  246. * a write with Link State Write Strobe set sets the link state.
  247. */
  248. /* true: port has power (see HCC_PPC) */
  249. #define PORT_POWER (1 << 9)
  250. /* bits 10:13 indicate device speed:
  251. * 0 - undefined speed - port hasn't be initialized by a reset yet
  252. * 1 - full speed
  253. * 2 - low speed
  254. * 3 - high speed
  255. * 4 - super speed
  256. * 5-15 reserved
  257. */
  258. #define DEV_SPEED_MASK (0xf << 10)
  259. #define XDEV_FS (0x1 << 10)
  260. #define XDEV_LS (0x2 << 10)
  261. #define XDEV_HS (0x3 << 10)
  262. #define XDEV_SS (0x4 << 10)
  263. #define DEV_UNDEFSPEED(p) (((p) & DEV_SPEED_MASK) == (0x0<<10))
  264. #define DEV_FULLSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_FS)
  265. #define DEV_LOWSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_LS)
  266. #define DEV_HIGHSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_HS)
  267. #define DEV_SUPERSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_SS)
  268. /* Bits 20:23 in the Slot Context are the speed for the device */
  269. #define SLOT_SPEED_FS (XDEV_FS << 10)
  270. #define SLOT_SPEED_LS (XDEV_LS << 10)
  271. #define SLOT_SPEED_HS (XDEV_HS << 10)
  272. #define SLOT_SPEED_SS (XDEV_SS << 10)
  273. /* Port Indicator Control */
  274. #define PORT_LED_OFF (0 << 14)
  275. #define PORT_LED_AMBER (1 << 14)
  276. #define PORT_LED_GREEN (2 << 14)
  277. #define PORT_LED_MASK (3 << 14)
  278. /* Port Link State Write Strobe - set this when changing link state */
  279. #define PORT_LINK_STROBE (1 << 16)
  280. /* true: connect status change */
  281. #define PORT_CSC (1 << 17)
  282. /* true: port enable change */
  283. #define PORT_PEC (1 << 18)
  284. /* true: warm reset for a USB 3.0 device is done. A "hot" reset puts the port
  285. * into an enabled state, and the device into the default state. A "warm" reset
  286. * also resets the link, forcing the device through the link training sequence.
  287. * SW can also look at the Port Reset register to see when warm reset is done.
  288. */
  289. #define PORT_WRC (1 << 19)
  290. /* true: over-current change */
  291. #define PORT_OCC (1 << 20)
  292. /* true: reset change - 1 to 0 transition of PORT_RESET */
  293. #define PORT_RC (1 << 21)
  294. /* port link status change - set on some port link state transitions:
  295. * Transition Reason
  296. * ------------------------------------------------------------------------------
  297. * - U3 to Resume Wakeup signaling from a device
  298. * - Resume to Recovery to U0 USB 3.0 device resume
  299. * - Resume to U0 USB 2.0 device resume
  300. * - U3 to Recovery to U0 Software resume of USB 3.0 device complete
  301. * - U3 to U0 Software resume of USB 2.0 device complete
  302. * - U2 to U0 L1 resume of USB 2.1 device complete
  303. * - U0 to U0 (???) L1 entry rejection by USB 2.1 device
  304. * - U0 to disabled L1 entry error with USB 2.1 device
  305. * - Any state to inactive Error on USB 3.0 port
  306. */
  307. #define PORT_PLC (1 << 22)
  308. /* port configure error change - port failed to configure its link partner */
  309. #define PORT_CEC (1 << 23)
  310. /* bit 24 reserved */
  311. /* wake on connect (enable) */
  312. #define PORT_WKCONN_E (1 << 25)
  313. /* wake on disconnect (enable) */
  314. #define PORT_WKDISC_E (1 << 26)
  315. /* wake on over-current (enable) */
  316. #define PORT_WKOC_E (1 << 27)
  317. /* bits 28:29 reserved */
  318. /* true: device is removable - for USB 3.0 roothub emulation */
  319. #define PORT_DEV_REMOVE (1 << 30)
  320. /* Initiate a warm port reset - complete when PORT_WRC is '1' */
  321. #define PORT_WR (1 << 31)
  322. /* Port Power Management Status and Control - port_power_base bitmasks */
  323. /* Inactivity timer value for transitions into U1, in microseconds.
  324. * Timeout can be up to 127us. 0xFF means an infinite timeout.
  325. */
  326. #define PORT_U1_TIMEOUT(p) ((p) & 0xff)
  327. /* Inactivity timer value for transitions into U2 */
  328. #define PORT_U2_TIMEOUT(p) (((p) & 0xff) << 8)
  329. /* Bits 24:31 for port testing */
  330. /**
  331. * struct xhci_intr_reg - Interrupt Register Set
  332. * @irq_pending: IMAN - Interrupt Management Register. Used to enable
  333. * interrupts and check for pending interrupts.
  334. * @irq_control: IMOD - Interrupt Moderation Register.
  335. * Used to throttle interrupts.
  336. * @erst_size: Number of segments in the Event Ring Segment Table (ERST).
  337. * @erst_base: ERST base address.
  338. * @erst_dequeue: Event ring dequeue pointer.
  339. *
  340. * Each interrupter (defined by a MSI-X vector) has an event ring and an Event
  341. * Ring Segment Table (ERST) associated with it. The event ring is comprised of
  342. * multiple segments of the same size. The HC places events on the ring and
  343. * "updates the Cycle bit in the TRBs to indicate to software the current
  344. * position of the Enqueue Pointer." The HCD (Linux) processes those events and
  345. * updates the dequeue pointer.
  346. */
  347. struct xhci_intr_reg {
  348. u32 irq_pending;
  349. u32 irq_control;
  350. u32 erst_size;
  351. u32 rsvd;
  352. u64 erst_base;
  353. u64 erst_dequeue;
  354. };
  355. /* irq_pending bitmasks */
  356. #define ER_IRQ_PENDING(p) ((p) & 0x1)
  357. /* bits 2:31 need to be preserved */
  358. /* THIS IS BUGGY - FIXME - IP IS WRITE 1 TO CLEAR */
  359. #define ER_IRQ_CLEAR(p) ((p) & 0xfffffffe)
  360. #define ER_IRQ_ENABLE(p) ((ER_IRQ_CLEAR(p)) | 0x2)
  361. #define ER_IRQ_DISABLE(p) ((ER_IRQ_CLEAR(p)) & ~(0x2))
  362. /* irq_control bitmasks */
  363. /* Minimum interval between interrupts (in 250ns intervals). The interval
  364. * between interrupts will be longer if there are no events on the event ring.
  365. * Default is 4000 (1 ms).
  366. */
  367. #define ER_IRQ_INTERVAL_MASK (0xffff)
  368. /* Counter used to count down the time to the next interrupt - HW use only */
  369. #define ER_IRQ_COUNTER_MASK (0xffff << 16)
  370. /* erst_size bitmasks */
  371. /* Preserve bits 16:31 of erst_size */
  372. #define ERST_SIZE_MASK (0xffff << 16)
  373. /* erst_dequeue bitmasks */
  374. /* Dequeue ERST Segment Index (DESI) - Segment number (or alias)
  375. * where the current dequeue pointer lies. This is an optional HW hint.
  376. */
  377. #define ERST_DESI_MASK (0x7)
  378. /* Event Handler Busy (EHB) - is the event ring scheduled to be serviced by
  379. * a work queue (or delayed service routine)?
  380. */
  381. #define ERST_EHB (1 << 3)
  382. #define ERST_PTR_MASK (0xf)
  383. /**
  384. * struct xhci_run_regs
  385. * @microframe_index:
  386. * MFINDEX - current microframe number
  387. *
  388. * Section 5.5 Host Controller Runtime Registers:
  389. * "Software should read and write these registers using only Dword (32 bit)
  390. * or larger accesses"
  391. */
  392. struct xhci_run_regs {
  393. u32 microframe_index;
  394. u32 rsvd[7];
  395. struct xhci_intr_reg ir_set[128];
  396. };
  397. /**
  398. * struct doorbell_array
  399. *
  400. * Section 5.6
  401. */
  402. struct xhci_doorbell_array {
  403. u32 doorbell[256];
  404. };
  405. #define DB_TARGET_MASK 0xFFFFFF00
  406. #define DB_STREAM_ID_MASK 0x0000FFFF
  407. #define DB_TARGET_HOST 0x0
  408. #define DB_STREAM_ID_HOST 0x0
  409. #define DB_MASK (0xff << 8)
  410. /* Endpoint Target - bits 0:7 */
  411. #define EPI_TO_DB(p) (((p) + 1) & 0xff)
  412. /**
  413. * struct xhci_slot_ctx
  414. * @dev_info: Route string, device speed, hub info, and last valid endpoint
  415. * @dev_info2: Max exit latency for device number, root hub port number
  416. * @tt_info: tt_info is used to construct split transaction tokens
  417. * @dev_state: slot state and device address
  418. *
  419. * Slot Context - section 6.2.1.1. This assumes the HC uses 32-byte context
  420. * structures. If the HC uses 64-byte contexts, there is an additional 32 bytes
  421. * reserved at the end of the slot context for HC internal use.
  422. */
  423. struct xhci_slot_ctx {
  424. u32 dev_info;
  425. u32 dev_info2;
  426. u32 tt_info;
  427. u32 dev_state;
  428. /* offset 0x10 to 0x1f reserved for HC internal use */
  429. u32 reserved[4];
  430. };
  431. /* dev_info bitmasks */
  432. /* Route String - 0:19 */
  433. #define ROUTE_STRING_MASK (0xfffff)
  434. /* Device speed - values defined by PORTSC Device Speed field - 20:23 */
  435. #define DEV_SPEED (0xf << 20)
  436. /* bit 24 reserved */
  437. /* Is this LS/FS device connected through a HS hub? - bit 25 */
  438. #define DEV_MTT (0x1 << 25)
  439. /* Set if the device is a hub - bit 26 */
  440. #define DEV_HUB (0x1 << 26)
  441. /* Index of the last valid endpoint context in this device context - 27:31 */
  442. #define LAST_CTX_MASK (0x1f << 27)
  443. #define LAST_CTX(p) ((p) << 27)
  444. #define LAST_CTX_TO_EP_NUM(p) (((p) >> 27) - 1)
  445. #define SLOT_FLAG (1 << 0)
  446. #define EP0_FLAG (1 << 1)
  447. /* dev_info2 bitmasks */
  448. /* Max Exit Latency (ms) - worst case time to wake up all links in dev path */
  449. #define MAX_EXIT (0xffff)
  450. /* Root hub port number that is needed to access the USB device */
  451. #define ROOT_HUB_PORT(p) (((p) & 0xff) << 16)
  452. /* tt_info bitmasks */
  453. /*
  454. * TT Hub Slot ID - for low or full speed devices attached to a high-speed hub
  455. * The Slot ID of the hub that isolates the high speed signaling from
  456. * this low or full-speed device. '0' if attached to root hub port.
  457. */
  458. #define TT_SLOT (0xff)
  459. /*
  460. * The number of the downstream facing port of the high-speed hub
  461. * '0' if the device is not low or full speed.
  462. */
  463. #define TT_PORT (0xff << 8)
  464. /* dev_state bitmasks */
  465. /* USB device address - assigned by the HC */
  466. #define DEV_ADDR_MASK (0xff)
  467. /* bits 8:26 reserved */
  468. /* Slot state */
  469. #define SLOT_STATE (0x1f << 27)
  470. #define GET_SLOT_STATE(p) (((p) & (0x1f << 27)) >> 27)
  471. /**
  472. * struct xhci_ep_ctx
  473. * @ep_info: endpoint state, streams, mult, and interval information.
  474. * @ep_info2: information on endpoint type, max packet size, max burst size,
  475. * error count, and whether the HC will force an event for all
  476. * transactions.
  477. * @deq: 64-bit ring dequeue pointer address. If the endpoint only
  478. * defines one stream, this points to the endpoint transfer ring.
  479. * Otherwise, it points to a stream context array, which has a
  480. * ring pointer for each flow.
  481. * @tx_info:
  482. * Average TRB lengths for the endpoint ring and
  483. * max payload within an Endpoint Service Interval Time (ESIT).
  484. *
  485. * Endpoint Context - section 6.2.1.2. This assumes the HC uses 32-byte context
  486. * structures. If the HC uses 64-byte contexts, there is an additional 32 bytes
  487. * reserved at the end of the endpoint context for HC internal use.
  488. */
  489. struct xhci_ep_ctx {
  490. u32 ep_info;
  491. u32 ep_info2;
  492. u64 deq;
  493. u32 tx_info;
  494. /* offset 0x14 - 0x1f reserved for HC internal use */
  495. u32 reserved[3];
  496. };
  497. /* ep_info bitmasks */
  498. /*
  499. * Endpoint State - bits 0:2
  500. * 0 - disabled
  501. * 1 - running
  502. * 2 - halted due to halt condition - ok to manipulate endpoint ring
  503. * 3 - stopped
  504. * 4 - TRB error
  505. * 5-7 - reserved
  506. */
  507. #define EP_STATE_MASK (0xf)
  508. #define EP_STATE_DISABLED 0
  509. #define EP_STATE_RUNNING 1
  510. #define EP_STATE_HALTED 2
  511. #define EP_STATE_STOPPED 3
  512. #define EP_STATE_ERROR 4
  513. /* Mult - Max number of burtst within an interval, in EP companion desc. */
  514. #define EP_MULT(p) ((p & 0x3) << 8)
  515. /* bits 10:14 are Max Primary Streams */
  516. /* bit 15 is Linear Stream Array */
  517. /* Interval - period between requests to an endpoint - 125u increments. */
  518. #define EP_INTERVAL(p) ((p & 0xff) << 16)
  519. /* ep_info2 bitmasks */
  520. /*
  521. * Force Event - generate transfer events for all TRBs for this endpoint
  522. * This will tell the HC to ignore the IOC and ISP flags (for debugging only).
  523. */
  524. #define FORCE_EVENT (0x1)
  525. #define ERROR_COUNT(p) (((p) & 0x3) << 1)
  526. #define EP_TYPE(p) ((p) << 3)
  527. #define ISOC_OUT_EP 1
  528. #define BULK_OUT_EP 2
  529. #define INT_OUT_EP 3
  530. #define CTRL_EP 4
  531. #define ISOC_IN_EP 5
  532. #define BULK_IN_EP 6
  533. #define INT_IN_EP 7
  534. /* bit 6 reserved */
  535. /* bit 7 is Host Initiate Disable - for disabling stream selection */
  536. #define MAX_BURST(p) (((p)&0xff) << 8)
  537. #define MAX_PACKET(p) (((p)&0xffff) << 16)
  538. /**
  539. * struct xhci_device_control
  540. * Input/Output context; see section 6.2.5.
  541. *
  542. * @drop_context: set the bit of the endpoint context you want to disable
  543. * @add_context: set the bit of the endpoint context you want to enable
  544. */
  545. struct xhci_device_control {
  546. u32 drop_flags;
  547. u32 add_flags;
  548. u32 rsvd[6];
  549. struct xhci_slot_ctx slot;
  550. struct xhci_ep_ctx ep[31];
  551. };
  552. /* drop context bitmasks */
  553. #define DROP_EP(x) (0x1 << x)
  554. /* add context bitmasks */
  555. #define ADD_EP(x) (0x1 << x)
  556. struct xhci_virt_device {
  557. /*
  558. * Commands to the hardware are passed an "input context" that
  559. * tells the hardware what to change in its data structures.
  560. * The hardware will return changes in an "output context" that
  561. * software must allocate for the hardware. We need to keep
  562. * track of input and output contexts separately because
  563. * these commands might fail and we don't trust the hardware.
  564. */
  565. struct xhci_device_control *out_ctx;
  566. dma_addr_t out_ctx_dma;
  567. /* Used for addressing devices and configuration changes */
  568. struct xhci_device_control *in_ctx;
  569. dma_addr_t in_ctx_dma;
  570. /* FIXME when stream support is added */
  571. struct xhci_ring *ep_rings[31];
  572. /* Temporary storage in case the configure endpoint command fails and we
  573. * have to restore the device state to the previous state
  574. */
  575. struct xhci_ring *new_ep_rings[31];
  576. struct completion cmd_completion;
  577. /* Status of the last command issued for this device */
  578. u32 cmd_status;
  579. };
  580. /**
  581. * struct xhci_device_context_array
  582. * @dev_context_ptr array of 64-bit DMA addresses for device contexts
  583. */
  584. struct xhci_device_context_array {
  585. /* 64-bit device addresses; we only write 32-bit addresses */
  586. u64 dev_context_ptrs[MAX_HC_SLOTS];
  587. /* private xHCD pointers */
  588. dma_addr_t dma;
  589. };
  590. /* TODO: write function to set the 64-bit device DMA address */
  591. /*
  592. * TODO: change this to be dynamically sized at HC mem init time since the HC
  593. * might not be able to handle the maximum number of devices possible.
  594. */
  595. struct xhci_stream_ctx {
  596. /* 64-bit stream ring address, cycle state, and stream type */
  597. u64 stream_ring;
  598. /* offset 0x14 - 0x1f reserved for HC internal use */
  599. u32 reserved[2];
  600. };
  601. struct xhci_transfer_event {
  602. /* 64-bit buffer address, or immediate data */
  603. u64 buffer;
  604. u32 transfer_len;
  605. /* This field is interpreted differently based on the type of TRB */
  606. u32 flags;
  607. };
  608. /** Transfer Event bit fields **/
  609. #define TRB_TO_EP_ID(p) (((p) >> 16) & 0x1f)
  610. /* Completion Code - only applicable for some types of TRBs */
  611. #define COMP_CODE_MASK (0xff << 24)
  612. #define GET_COMP_CODE(p) (((p) & COMP_CODE_MASK) >> 24)
  613. #define COMP_SUCCESS 1
  614. /* Data Buffer Error */
  615. #define COMP_DB_ERR 2
  616. /* Babble Detected Error */
  617. #define COMP_BABBLE 3
  618. /* USB Transaction Error */
  619. #define COMP_TX_ERR 4
  620. /* TRB Error - some TRB field is invalid */
  621. #define COMP_TRB_ERR 5
  622. /* Stall Error - USB device is stalled */
  623. #define COMP_STALL 6
  624. /* Resource Error - HC doesn't have memory for that device configuration */
  625. #define COMP_ENOMEM 7
  626. /* Bandwidth Error - not enough room in schedule for this dev config */
  627. #define COMP_BW_ERR 8
  628. /* No Slots Available Error - HC ran out of device slots */
  629. #define COMP_ENOSLOTS 9
  630. /* Invalid Stream Type Error */
  631. #define COMP_STREAM_ERR 10
  632. /* Slot Not Enabled Error - doorbell rung for disabled device slot */
  633. #define COMP_EBADSLT 11
  634. /* Endpoint Not Enabled Error */
  635. #define COMP_EBADEP 12
  636. /* Short Packet */
  637. #define COMP_SHORT_TX 13
  638. /* Ring Underrun - doorbell rung for an empty isoc OUT ep ring */
  639. #define COMP_UNDERRUN 14
  640. /* Ring Overrun - isoc IN ep ring is empty when ep is scheduled to RX */
  641. #define COMP_OVERRUN 15
  642. /* Virtual Function Event Ring Full Error */
  643. #define COMP_VF_FULL 16
  644. /* Parameter Error - Context parameter is invalid */
  645. #define COMP_EINVAL 17
  646. /* Bandwidth Overrun Error - isoc ep exceeded its allocated bandwidth */
  647. #define COMP_BW_OVER 18
  648. /* Context State Error - illegal context state transition requested */
  649. #define COMP_CTX_STATE 19
  650. /* No Ping Response Error - HC didn't get PING_RESPONSE in time to TX */
  651. #define COMP_PING_ERR 20
  652. /* Event Ring is full */
  653. #define COMP_ER_FULL 21
  654. /* Missed Service Error - HC couldn't service an isoc ep within interval */
  655. #define COMP_MISSED_INT 23
  656. /* Successfully stopped command ring */
  657. #define COMP_CMD_STOP 24
  658. /* Successfully aborted current command and stopped command ring */
  659. #define COMP_CMD_ABORT 25
  660. /* Stopped - transfer was terminated by a stop endpoint command */
  661. #define COMP_STOP 26
  662. /* Same as COMP_EP_STOPPED, but the transfered length in the event is invalid */
  663. #define COMP_STOP_INVAL 27
  664. /* Control Abort Error - Debug Capability - control pipe aborted */
  665. #define COMP_DBG_ABORT 28
  666. /* TRB type 29 and 30 reserved */
  667. /* Isoc Buffer Overrun - an isoc IN ep sent more data than could fit in TD */
  668. #define COMP_BUFF_OVER 31
  669. /* Event Lost Error - xHC has an "internal event overrun condition" */
  670. #define COMP_ISSUES 32
  671. /* Undefined Error - reported when other error codes don't apply */
  672. #define COMP_UNKNOWN 33
  673. /* Invalid Stream ID Error */
  674. #define COMP_STRID_ERR 34
  675. /* Secondary Bandwidth Error - may be returned by a Configure Endpoint cmd */
  676. /* FIXME - check for this */
  677. #define COMP_2ND_BW_ERR 35
  678. /* Split Transaction Error */
  679. #define COMP_SPLIT_ERR 36
  680. struct xhci_link_trb {
  681. /* 64-bit segment pointer*/
  682. u64 segment_ptr;
  683. u32 intr_target;
  684. u32 control;
  685. };
  686. /* control bitfields */
  687. #define LINK_TOGGLE (0x1<<1)
  688. /* Command completion event TRB */
  689. struct xhci_event_cmd {
  690. /* Pointer to command TRB, or the value passed by the event data trb */
  691. u64 cmd_trb;
  692. u32 status;
  693. u32 flags;
  694. };
  695. /* flags bitmasks */
  696. /* bits 16:23 are the virtual function ID */
  697. /* bits 24:31 are the slot ID */
  698. #define TRB_TO_SLOT_ID(p) (((p) & (0xff<<24)) >> 24)
  699. #define SLOT_ID_FOR_TRB(p) (((p) & 0xff) << 24)
  700. /* Stop Endpoint TRB - ep_index to endpoint ID for this TRB */
  701. #define TRB_TO_EP_INDEX(p) ((((p) & (0x1f << 16)) >> 16) - 1)
  702. #define EP_ID_FOR_TRB(p) ((((p) + 1) & 0x1f) << 16)
  703. /* Port Status Change Event TRB fields */
  704. /* Port ID - bits 31:24 */
  705. #define GET_PORT_ID(p) (((p) & (0xff << 24)) >> 24)
  706. /* Normal TRB fields */
  707. /* transfer_len bitmasks - bits 0:16 */
  708. #define TRB_LEN(p) ((p) & 0x1ffff)
  709. /* TD size - number of bytes remaining in the TD (including this TRB):
  710. * bits 17 - 21. Shift the number of bytes by 10. */
  711. #define TD_REMAINDER(p) ((((p) >> 10) & 0x1f) << 17)
  712. /* Interrupter Target - which MSI-X vector to target the completion event at */
  713. #define TRB_INTR_TARGET(p) (((p) & 0x3ff) << 22)
  714. #define GET_INTR_TARGET(p) (((p) >> 22) & 0x3ff)
  715. /* Cycle bit - indicates TRB ownership by HC or HCD */
  716. #define TRB_CYCLE (1<<0)
  717. /*
  718. * Force next event data TRB to be evaluated before task switch.
  719. * Used to pass OS data back after a TD completes.
  720. */
  721. #define TRB_ENT (1<<1)
  722. /* Interrupt on short packet */
  723. #define TRB_ISP (1<<2)
  724. /* Set PCIe no snoop attribute */
  725. #define TRB_NO_SNOOP (1<<3)
  726. /* Chain multiple TRBs into a TD */
  727. #define TRB_CHAIN (1<<4)
  728. /* Interrupt on completion */
  729. #define TRB_IOC (1<<5)
  730. /* The buffer pointer contains immediate data */
  731. #define TRB_IDT (1<<6)
  732. /* Control transfer TRB specific fields */
  733. #define TRB_DIR_IN (1<<16)
  734. struct xhci_generic_trb {
  735. u32 field[4];
  736. };
  737. union xhci_trb {
  738. struct xhci_link_trb link;
  739. struct xhci_transfer_event trans_event;
  740. struct xhci_event_cmd event_cmd;
  741. struct xhci_generic_trb generic;
  742. };
  743. /* TRB bit mask */
  744. #define TRB_TYPE_BITMASK (0xfc00)
  745. #define TRB_TYPE(p) ((p) << 10)
  746. /* TRB type IDs */
  747. /* bulk, interrupt, isoc scatter/gather, and control data stage */
  748. #define TRB_NORMAL 1
  749. /* setup stage for control transfers */
  750. #define TRB_SETUP 2
  751. /* data stage for control transfers */
  752. #define TRB_DATA 3
  753. /* status stage for control transfers */
  754. #define TRB_STATUS 4
  755. /* isoc transfers */
  756. #define TRB_ISOC 5
  757. /* TRB for linking ring segments */
  758. #define TRB_LINK 6
  759. #define TRB_EVENT_DATA 7
  760. /* Transfer Ring No-op (not for the command ring) */
  761. #define TRB_TR_NOOP 8
  762. /* Command TRBs */
  763. /* Enable Slot Command */
  764. #define TRB_ENABLE_SLOT 9
  765. /* Disable Slot Command */
  766. #define TRB_DISABLE_SLOT 10
  767. /* Address Device Command */
  768. #define TRB_ADDR_DEV 11
  769. /* Configure Endpoint Command */
  770. #define TRB_CONFIG_EP 12
  771. /* Evaluate Context Command */
  772. #define TRB_EVAL_CONTEXT 13
  773. /* Reset Endpoint Command */
  774. #define TRB_RESET_EP 14
  775. /* Stop Transfer Ring Command */
  776. #define TRB_STOP_RING 15
  777. /* Set Transfer Ring Dequeue Pointer Command */
  778. #define TRB_SET_DEQ 16
  779. /* Reset Device Command */
  780. #define TRB_RESET_DEV 17
  781. /* Force Event Command (opt) */
  782. #define TRB_FORCE_EVENT 18
  783. /* Negotiate Bandwidth Command (opt) */
  784. #define TRB_NEG_BANDWIDTH 19
  785. /* Set Latency Tolerance Value Command (opt) */
  786. #define TRB_SET_LT 20
  787. /* Get port bandwidth Command */
  788. #define TRB_GET_BW 21
  789. /* Force Header Command - generate a transaction or link management packet */
  790. #define TRB_FORCE_HEADER 22
  791. /* No-op Command - not for transfer rings */
  792. #define TRB_CMD_NOOP 23
  793. /* TRB IDs 24-31 reserved */
  794. /* Event TRBS */
  795. /* Transfer Event */
  796. #define TRB_TRANSFER 32
  797. /* Command Completion Event */
  798. #define TRB_COMPLETION 33
  799. /* Port Status Change Event */
  800. #define TRB_PORT_STATUS 34
  801. /* Bandwidth Request Event (opt) */
  802. #define TRB_BANDWIDTH_EVENT 35
  803. /* Doorbell Event (opt) */
  804. #define TRB_DOORBELL 36
  805. /* Host Controller Event */
  806. #define TRB_HC_EVENT 37
  807. /* Device Notification Event - device sent function wake notification */
  808. #define TRB_DEV_NOTE 38
  809. /* MFINDEX Wrap Event - microframe counter wrapped */
  810. #define TRB_MFINDEX_WRAP 39
  811. /* TRB IDs 40-47 reserved, 48-63 is vendor-defined */
  812. /*
  813. * TRBS_PER_SEGMENT must be a multiple of 4,
  814. * since the command ring is 64-byte aligned.
  815. * It must also be greater than 16.
  816. */
  817. #define TRBS_PER_SEGMENT 64
  818. #define SEGMENT_SIZE (TRBS_PER_SEGMENT*16)
  819. /* TRB buffer pointers can't cross 64KB boundaries */
  820. #define TRB_MAX_BUFF_SHIFT 16
  821. #define TRB_MAX_BUFF_SIZE (1 << TRB_MAX_BUFF_SHIFT)
  822. struct xhci_segment {
  823. union xhci_trb *trbs;
  824. /* private to HCD */
  825. struct xhci_segment *next;
  826. dma_addr_t dma;
  827. };
  828. struct xhci_td {
  829. struct list_head td_list;
  830. struct list_head cancelled_td_list;
  831. struct urb *urb;
  832. struct xhci_segment *start_seg;
  833. union xhci_trb *first_trb;
  834. union xhci_trb *last_trb;
  835. };
  836. struct xhci_ring {
  837. struct xhci_segment *first_seg;
  838. union xhci_trb *enqueue;
  839. struct xhci_segment *enq_seg;
  840. unsigned int enq_updates;
  841. union xhci_trb *dequeue;
  842. struct xhci_segment *deq_seg;
  843. unsigned int deq_updates;
  844. struct list_head td_list;
  845. /* ---- Related to URB cancellation ---- */
  846. struct list_head cancelled_td_list;
  847. unsigned int cancels_pending;
  848. unsigned int state;
  849. #define SET_DEQ_PENDING (1 << 0)
  850. #define EP_HALTED (1 << 1)
  851. /* The TRB that was last reported in a stopped endpoint ring */
  852. union xhci_trb *stopped_trb;
  853. struct xhci_td *stopped_td;
  854. /*
  855. * Write the cycle state into the TRB cycle field to give ownership of
  856. * the TRB to the host controller (if we are the producer), or to check
  857. * if we own the TRB (if we are the consumer). See section 4.9.1.
  858. */
  859. u32 cycle_state;
  860. };
  861. struct xhci_erst_entry {
  862. /* 64-bit event ring segment address */
  863. u64 seg_addr;
  864. u32 seg_size;
  865. /* Set to zero */
  866. u32 rsvd;
  867. };
  868. struct xhci_erst {
  869. struct xhci_erst_entry *entries;
  870. unsigned int num_entries;
  871. /* xhci->event_ring keeps track of segment dma addresses */
  872. dma_addr_t erst_dma_addr;
  873. /* Num entries the ERST can contain */
  874. unsigned int erst_size;
  875. };
  876. /*
  877. * Each segment table entry is 4*32bits long. 1K seems like an ok size:
  878. * (1K bytes * 8bytes/bit) / (4*32 bits) = 64 segment entries in the table,
  879. * meaning 64 ring segments.
  880. * Initial allocated size of the ERST, in number of entries */
  881. #define ERST_NUM_SEGS 1
  882. /* Initial allocated size of the ERST, in number of entries */
  883. #define ERST_SIZE 64
  884. /* Initial number of event segment rings allocated */
  885. #define ERST_ENTRIES 1
  886. /* Poll every 60 seconds */
  887. #define POLL_TIMEOUT 60
  888. /* XXX: Make these module parameters */
  889. /* There is one ehci_hci structure per controller */
  890. struct xhci_hcd {
  891. /* glue to PCI and HCD framework */
  892. struct xhci_cap_regs __iomem *cap_regs;
  893. struct xhci_op_regs __iomem *op_regs;
  894. struct xhci_run_regs __iomem *run_regs;
  895. struct xhci_doorbell_array __iomem *dba;
  896. /* Our HCD's current interrupter register set */
  897. struct xhci_intr_reg __iomem *ir_set;
  898. /* Cached register copies of read-only HC data */
  899. __u32 hcs_params1;
  900. __u32 hcs_params2;
  901. __u32 hcs_params3;
  902. __u32 hcc_params;
  903. spinlock_t lock;
  904. /* packed release number */
  905. u8 sbrn;
  906. u16 hci_version;
  907. u8 max_slots;
  908. u8 max_interrupters;
  909. u8 max_ports;
  910. u8 isoc_threshold;
  911. int event_ring_max;
  912. int addr_64;
  913. /* 4KB min, 128MB max */
  914. int page_size;
  915. /* Valid values are 12 to 20, inclusive */
  916. int page_shift;
  917. /* only one MSI vector for now, but might need more later */
  918. int msix_count;
  919. struct msix_entry *msix_entries;
  920. /* data structures */
  921. struct xhci_device_context_array *dcbaa;
  922. struct xhci_ring *cmd_ring;
  923. struct xhci_ring *event_ring;
  924. struct xhci_erst erst;
  925. /* slot enabling and address device helpers */
  926. struct completion addr_dev;
  927. int slot_id;
  928. /* Internal mirror of the HW's dcbaa */
  929. struct xhci_virt_device *devs[MAX_HC_SLOTS];
  930. /* DMA pools */
  931. struct dma_pool *device_pool;
  932. struct dma_pool *segment_pool;
  933. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  934. /* Poll the rings - for debugging */
  935. struct timer_list event_ring_timer;
  936. int zombie;
  937. #endif
  938. /* Statistics */
  939. int noops_submitted;
  940. int noops_handled;
  941. int error_bitmask;
  942. };
  943. /* For testing purposes */
  944. #define NUM_TEST_NOOPS 0
  945. /* convert between an HCD pointer and the corresponding EHCI_HCD */
  946. static inline struct xhci_hcd *hcd_to_xhci(struct usb_hcd *hcd)
  947. {
  948. return (struct xhci_hcd *) (hcd->hcd_priv);
  949. }
  950. static inline struct usb_hcd *xhci_to_hcd(struct xhci_hcd *xhci)
  951. {
  952. return container_of((void *) xhci, struct usb_hcd, hcd_priv);
  953. }
  954. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  955. #define XHCI_DEBUG 1
  956. #else
  957. #define XHCI_DEBUG 0
  958. #endif
  959. #define xhci_dbg(xhci, fmt, args...) \
  960. do { if (XHCI_DEBUG) dev_dbg(xhci_to_hcd(xhci)->self.controller , fmt , ## args); } while (0)
  961. #define xhci_info(xhci, fmt, args...) \
  962. do { if (XHCI_DEBUG) dev_info(xhci_to_hcd(xhci)->self.controller , fmt , ## args); } while (0)
  963. #define xhci_err(xhci, fmt, args...) \
  964. dev_err(xhci_to_hcd(xhci)->self.controller , fmt , ## args)
  965. #define xhci_warn(xhci, fmt, args...) \
  966. dev_warn(xhci_to_hcd(xhci)->self.controller , fmt , ## args)
  967. /* TODO: copied from ehci.h - can be refactored? */
  968. /* xHCI spec says all registers are little endian */
  969. static inline unsigned int xhci_readl(const struct xhci_hcd *xhci,
  970. __u32 __iomem *regs)
  971. {
  972. return readl(regs);
  973. }
  974. static inline void xhci_writel(struct xhci_hcd *xhci,
  975. const unsigned int val, __u32 __iomem *regs)
  976. {
  977. if (!in_interrupt())
  978. xhci_dbg(xhci,
  979. "`MEM_WRITE_DWORD(3'b000, 32'h%p, 32'h%0x, 4'hf);\n",
  980. regs, val);
  981. writel(val, regs);
  982. }
  983. /*
  984. * Registers should always be accessed with double word or quad word accesses.
  985. *
  986. * Some xHCI implementations may support 64-bit address pointers. Registers
  987. * with 64-bit address pointers should be written to with dword accesses by
  988. * writing the low dword first (ptr[0]), then the high dword (ptr[1]) second.
  989. * xHCI implementations that do not support 64-bit address pointers will ignore
  990. * the high dword, and write order is irrelevant.
  991. */
  992. static inline u64 xhci_read_64(const struct xhci_hcd *xhci,
  993. __u64 __iomem *regs)
  994. {
  995. __u32 __iomem *ptr = (__u32 __iomem *) regs;
  996. u64 val_lo = readl(ptr);
  997. u64 val_hi = readl(ptr + 1);
  998. return val_lo + (val_hi << 32);
  999. }
  1000. static inline void xhci_write_64(struct xhci_hcd *xhci,
  1001. const u64 val, __u64 __iomem *regs)
  1002. {
  1003. __u32 __iomem *ptr = (__u32 __iomem *) regs;
  1004. u32 val_lo = lower_32_bits(val);
  1005. u32 val_hi = upper_32_bits(val);
  1006. if (!in_interrupt())
  1007. xhci_dbg(xhci,
  1008. "`MEM_WRITE_DWORD(3'b000, 64'h%p, 64'h%0lx, 4'hf);\n",
  1009. regs, (long unsigned int) val);
  1010. writel(val_lo, ptr);
  1011. writel(val_hi, ptr + 1);
  1012. }
  1013. /* xHCI debugging */
  1014. void xhci_print_ir_set(struct xhci_hcd *xhci, struct xhci_intr_reg *ir_set, int set_num);
  1015. void xhci_print_registers(struct xhci_hcd *xhci);
  1016. void xhci_dbg_regs(struct xhci_hcd *xhci);
  1017. void xhci_print_run_regs(struct xhci_hcd *xhci);
  1018. void xhci_print_trb_offsets(struct xhci_hcd *xhci, union xhci_trb *trb);
  1019. void xhci_debug_trb(struct xhci_hcd *xhci, union xhci_trb *trb);
  1020. void xhci_debug_segment(struct xhci_hcd *xhci, struct xhci_segment *seg);
  1021. void xhci_debug_ring(struct xhci_hcd *xhci, struct xhci_ring *ring);
  1022. void xhci_dbg_erst(struct xhci_hcd *xhci, struct xhci_erst *erst);
  1023. void xhci_dbg_cmd_ptrs(struct xhci_hcd *xhci);
  1024. void xhci_dbg_ring_ptrs(struct xhci_hcd *xhci, struct xhci_ring *ring);
  1025. void xhci_dbg_ctx(struct xhci_hcd *xhci, struct xhci_device_control *ctx, dma_addr_t dma, unsigned int last_ep);
  1026. /* xHCI memory managment */
  1027. void xhci_mem_cleanup(struct xhci_hcd *xhci);
  1028. int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags);
  1029. void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id);
  1030. int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id, struct usb_device *udev, gfp_t flags);
  1031. int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev);
  1032. unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc);
  1033. unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc);
  1034. void xhci_endpoint_zero(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev, struct usb_host_endpoint *ep);
  1035. int xhci_endpoint_init(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev,
  1036. struct usb_device *udev, struct usb_host_endpoint *ep,
  1037. gfp_t mem_flags);
  1038. void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring);
  1039. #ifdef CONFIG_PCI
  1040. /* xHCI PCI glue */
  1041. int xhci_register_pci(void);
  1042. void xhci_unregister_pci(void);
  1043. #endif
  1044. /* xHCI host controller glue */
  1045. int xhci_halt(struct xhci_hcd *xhci);
  1046. int xhci_reset(struct xhci_hcd *xhci);
  1047. int xhci_init(struct usb_hcd *hcd);
  1048. int xhci_run(struct usb_hcd *hcd);
  1049. void xhci_stop(struct usb_hcd *hcd);
  1050. void xhci_shutdown(struct usb_hcd *hcd);
  1051. int xhci_get_frame(struct usb_hcd *hcd);
  1052. irqreturn_t xhci_irq(struct usb_hcd *hcd);
  1053. int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev);
  1054. void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev);
  1055. int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev);
  1056. int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags);
  1057. int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
  1058. int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev, struct usb_host_endpoint *ep);
  1059. int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev, struct usb_host_endpoint *ep);
  1060. void xhci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  1061. int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev);
  1062. void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev);
  1063. /* xHCI ring, segment, TRB, and TD functions */
  1064. dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg, union xhci_trb *trb);
  1065. void xhci_ring_cmd_db(struct xhci_hcd *xhci);
  1066. void *xhci_setup_one_noop(struct xhci_hcd *xhci);
  1067. void xhci_handle_event(struct xhci_hcd *xhci);
  1068. void xhci_set_hc_event_deq(struct xhci_hcd *xhci);
  1069. int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id);
  1070. int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1071. u32 slot_id);
  1072. int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
  1073. unsigned int ep_index);
  1074. int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb,
  1075. int slot_id, unsigned int ep_index);
  1076. int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb,
  1077. int slot_id, unsigned int ep_index);
  1078. int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1079. u32 slot_id);
  1080. int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
  1081. unsigned int ep_index);
  1082. /* xHCI roothub code */
  1083. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex,
  1084. char *buf, u16 wLength);
  1085. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf);
  1086. #endif /* __LINUX_XHCI_HCD_H */