xhci-hub.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  1. /*
  2. * xHCI host controller driver
  3. *
  4. * Copyright (C) 2008 Intel Corp.
  5. *
  6. * Author: Sarah Sharp
  7. * Some code borrowed from the Linux EHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/slab.h>
  23. #include <asm/unaligned.h>
  24. #include "xhci.h"
  25. #include "xhci-trace.h"
  26. #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
  27. #define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \
  28. PORT_RC | PORT_PLC | PORT_PE)
  29. /* USB 3 BOS descriptor and a capability descriptors, combined.
  30. * Fields will be adjusted and added later in xhci_create_usb3_bos_desc()
  31. */
  32. static u8 usb_bos_descriptor [] = {
  33. USB_DT_BOS_SIZE, /* __u8 bLength, 5 bytes */
  34. USB_DT_BOS, /* __u8 bDescriptorType */
  35. 0x0F, 0x00, /* __le16 wTotalLength, 15 bytes */
  36. 0x1, /* __u8 bNumDeviceCaps */
  37. /* First device capability, SuperSpeed */
  38. USB_DT_USB_SS_CAP_SIZE, /* __u8 bLength, 10 bytes */
  39. USB_DT_DEVICE_CAPABILITY, /* Device Capability */
  40. USB_SS_CAP_TYPE, /* bDevCapabilityType, SUPERSPEED_USB */
  41. 0x00, /* bmAttributes, LTM off by default */
  42. USB_5GBPS_OPERATION, 0x00, /* wSpeedsSupported, 5Gbps only */
  43. 0x03, /* bFunctionalitySupport,
  44. USB 3.0 speed only */
  45. 0x00, /* bU1DevExitLat, set later. */
  46. 0x00, 0x00, /* __le16 bU2DevExitLat, set later. */
  47. /* Second device capability, SuperSpeedPlus */
  48. 0x1c, /* bLength 28, will be adjusted later */
  49. USB_DT_DEVICE_CAPABILITY, /* Device Capability */
  50. USB_SSP_CAP_TYPE, /* bDevCapabilityType SUPERSPEED_PLUS */
  51. 0x00, /* bReserved 0 */
  52. 0x23, 0x00, 0x00, 0x00, /* bmAttributes, SSAC=3 SSIC=1 */
  53. 0x01, 0x00, /* wFunctionalitySupport */
  54. 0x00, 0x00, /* wReserved 0 */
  55. /* Default Sublink Speed Attributes, overwrite if custom PSI exists */
  56. 0x34, 0x00, 0x05, 0x00, /* 5Gbps, symmetric, rx, ID = 4 */
  57. 0xb4, 0x00, 0x05, 0x00, /* 5Gbps, symmetric, tx, ID = 4 */
  58. 0x35, 0x40, 0x0a, 0x00, /* 10Gbps, SSP, symmetric, rx, ID = 5 */
  59. 0xb5, 0x40, 0x0a, 0x00, /* 10Gbps, SSP, symmetric, tx, ID = 5 */
  60. };
  61. static int xhci_create_usb3_bos_desc(struct xhci_hcd *xhci, char *buf,
  62. u16 wLength)
  63. {
  64. int i, ssa_count;
  65. u32 temp;
  66. u16 desc_size, ssp_cap_size, ssa_size = 0;
  67. bool usb3_1 = false;
  68. desc_size = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  69. ssp_cap_size = sizeof(usb_bos_descriptor) - desc_size;
  70. /* does xhci support USB 3.1 Enhanced SuperSpeed */
  71. if (xhci->usb3_rhub.min_rev >= 0x01) {
  72. /* does xhci provide a PSI table for SSA speed attributes? */
  73. if (xhci->usb3_rhub.psi_count) {
  74. /* two SSA entries for each unique PSI ID, RX and TX */
  75. ssa_count = xhci->usb3_rhub.psi_uid_count * 2;
  76. ssa_size = ssa_count * sizeof(u32);
  77. ssp_cap_size -= 16; /* skip copying the default SSA */
  78. }
  79. desc_size += ssp_cap_size;
  80. usb3_1 = true;
  81. }
  82. memcpy(buf, &usb_bos_descriptor, min(desc_size, wLength));
  83. if (usb3_1) {
  84. /* modify bos descriptor bNumDeviceCaps and wTotalLength */
  85. buf[4] += 1;
  86. put_unaligned_le16(desc_size + ssa_size, &buf[2]);
  87. }
  88. if (wLength < USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE)
  89. return wLength;
  90. /* Indicate whether the host has LTM support. */
  91. temp = readl(&xhci->cap_regs->hcc_params);
  92. if (HCC_LTC(temp))
  93. buf[8] |= USB_LTM_SUPPORT;
  94. /* Set the U1 and U2 exit latencies. */
  95. if ((xhci->quirks & XHCI_LPM_SUPPORT)) {
  96. temp = readl(&xhci->cap_regs->hcs_params3);
  97. buf[12] = HCS_U1_LATENCY(temp);
  98. put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
  99. }
  100. /* If PSI table exists, add the custom speed attributes from it */
  101. if (usb3_1 && xhci->usb3_rhub.psi_count) {
  102. u32 ssp_cap_base, bm_attrib, psi, psi_mant, psi_exp;
  103. int offset;
  104. ssp_cap_base = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  105. if (wLength < desc_size)
  106. return wLength;
  107. buf[ssp_cap_base] = ssp_cap_size + ssa_size;
  108. /* attribute count SSAC bits 4:0 and ID count SSIC bits 8:5 */
  109. bm_attrib = (ssa_count - 1) & 0x1f;
  110. bm_attrib |= (xhci->usb3_rhub.psi_uid_count - 1) << 5;
  111. put_unaligned_le32(bm_attrib, &buf[ssp_cap_base + 4]);
  112. if (wLength < desc_size + ssa_size)
  113. return wLength;
  114. /*
  115. * Create the Sublink Speed Attributes (SSA) array.
  116. * The xhci PSI field and USB 3.1 SSA fields are very similar,
  117. * but link type bits 7:6 differ for values 01b and 10b.
  118. * xhci has also only one PSI entry for a symmetric link when
  119. * USB 3.1 requires two SSA entries (RX and TX) for every link
  120. */
  121. offset = desc_size;
  122. for (i = 0; i < xhci->usb3_rhub.psi_count; i++) {
  123. psi = xhci->usb3_rhub.psi[i];
  124. psi &= ~USB_SSP_SUBLINK_SPEED_RSVD;
  125. psi_exp = XHCI_EXT_PORT_PSIE(psi);
  126. psi_mant = XHCI_EXT_PORT_PSIM(psi);
  127. /* Shift to Gbps and set SSP Link BIT(14) if 10Gpbs */
  128. for (; psi_exp < 3; psi_exp++)
  129. psi_mant /= 1000;
  130. if (psi_mant >= 10)
  131. psi |= BIT(14);
  132. if ((psi & PLT_MASK) == PLT_SYM) {
  133. /* Symmetric, create SSA RX and TX from one PSI entry */
  134. put_unaligned_le32(psi, &buf[offset]);
  135. psi |= 1 << 7; /* turn entry to TX */
  136. offset += 4;
  137. if (offset >= desc_size + ssa_size)
  138. return desc_size + ssa_size;
  139. } else if ((psi & PLT_MASK) == PLT_ASYM_RX) {
  140. /* Asymetric RX, flip bits 7:6 for SSA */
  141. psi ^= PLT_MASK;
  142. }
  143. put_unaligned_le32(psi, &buf[offset]);
  144. offset += 4;
  145. if (offset >= desc_size + ssa_size)
  146. return desc_size + ssa_size;
  147. }
  148. }
  149. /* ssa_size is 0 for other than usb 3.1 hosts */
  150. return desc_size + ssa_size;
  151. }
  152. static void xhci_common_hub_descriptor(struct xhci_hcd *xhci,
  153. struct usb_hub_descriptor *desc, int ports)
  154. {
  155. u16 temp;
  156. desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */
  157. desc->bHubContrCurrent = 0;
  158. desc->bNbrPorts = ports;
  159. temp = 0;
  160. /* Bits 1:0 - support per-port power switching, or power always on */
  161. if (HCC_PPC(xhci->hcc_params))
  162. temp |= HUB_CHAR_INDV_PORT_LPSM;
  163. else
  164. temp |= HUB_CHAR_NO_LPSM;
  165. /* Bit 2 - root hubs are not part of a compound device */
  166. /* Bits 4:3 - individual port over current protection */
  167. temp |= HUB_CHAR_INDV_PORT_OCPM;
  168. /* Bits 6:5 - no TTs in root ports */
  169. /* Bit 7 - no port indicators */
  170. desc->wHubCharacteristics = cpu_to_le16(temp);
  171. }
  172. /* Fill in the USB 2.0 roothub descriptor */
  173. static void xhci_usb2_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  174. struct usb_hub_descriptor *desc)
  175. {
  176. int ports;
  177. u16 temp;
  178. __u8 port_removable[(USB_MAXCHILDREN + 1 + 7) / 8];
  179. u32 portsc;
  180. unsigned int i;
  181. ports = xhci->num_usb2_ports;
  182. xhci_common_hub_descriptor(xhci, desc, ports);
  183. desc->bDescriptorType = USB_DT_HUB;
  184. temp = 1 + (ports / 8);
  185. desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp;
  186. /* The Device Removable bits are reported on a byte granularity.
  187. * If the port doesn't exist within that byte, the bit is set to 0.
  188. */
  189. memset(port_removable, 0, sizeof(port_removable));
  190. for (i = 0; i < ports; i++) {
  191. portsc = readl(xhci->usb2_ports[i]);
  192. /* If a device is removable, PORTSC reports a 0, same as in the
  193. * hub descriptor DeviceRemovable bits.
  194. */
  195. if (portsc & PORT_DEV_REMOVE)
  196. /* This math is hairy because bit 0 of DeviceRemovable
  197. * is reserved, and bit 1 is for port 1, etc.
  198. */
  199. port_removable[(i + 1) / 8] |= 1 << ((i + 1) % 8);
  200. }
  201. /* ch11.h defines a hub descriptor that has room for USB_MAXCHILDREN
  202. * ports on it. The USB 2.0 specification says that there are two
  203. * variable length fields at the end of the hub descriptor:
  204. * DeviceRemovable and PortPwrCtrlMask. But since we can have less than
  205. * USB_MAXCHILDREN ports, we may need to use the DeviceRemovable array
  206. * to set PortPwrCtrlMask bits. PortPwrCtrlMask must always be set to
  207. * 0xFF, so we initialize the both arrays (DeviceRemovable and
  208. * PortPwrCtrlMask) to 0xFF. Then we set the DeviceRemovable for each
  209. * set of ports that actually exist.
  210. */
  211. memset(desc->u.hs.DeviceRemovable, 0xff,
  212. sizeof(desc->u.hs.DeviceRemovable));
  213. memset(desc->u.hs.PortPwrCtrlMask, 0xff,
  214. sizeof(desc->u.hs.PortPwrCtrlMask));
  215. for (i = 0; i < (ports + 1 + 7) / 8; i++)
  216. memset(&desc->u.hs.DeviceRemovable[i], port_removable[i],
  217. sizeof(__u8));
  218. }
  219. /* Fill in the USB 3.0 roothub descriptor */
  220. static void xhci_usb3_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  221. struct usb_hub_descriptor *desc)
  222. {
  223. int ports;
  224. u16 port_removable;
  225. u32 portsc;
  226. unsigned int i;
  227. ports = xhci->num_usb3_ports;
  228. xhci_common_hub_descriptor(xhci, desc, ports);
  229. desc->bDescriptorType = USB_DT_SS_HUB;
  230. desc->bDescLength = USB_DT_SS_HUB_SIZE;
  231. /* header decode latency should be zero for roothubs,
  232. * see section 4.23.5.2.
  233. */
  234. desc->u.ss.bHubHdrDecLat = 0;
  235. desc->u.ss.wHubDelay = 0;
  236. port_removable = 0;
  237. /* bit 0 is reserved, bit 1 is for port 1, etc. */
  238. for (i = 0; i < ports; i++) {
  239. portsc = readl(xhci->usb3_ports[i]);
  240. if (portsc & PORT_DEV_REMOVE)
  241. port_removable |= 1 << (i + 1);
  242. }
  243. desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
  244. }
  245. static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  246. struct usb_hub_descriptor *desc)
  247. {
  248. if (hcd->speed >= HCD_USB3)
  249. xhci_usb3_hub_descriptor(hcd, xhci, desc);
  250. else
  251. xhci_usb2_hub_descriptor(hcd, xhci, desc);
  252. }
  253. static unsigned int xhci_port_speed(unsigned int port_status)
  254. {
  255. if (DEV_LOWSPEED(port_status))
  256. return USB_PORT_STAT_LOW_SPEED;
  257. if (DEV_HIGHSPEED(port_status))
  258. return USB_PORT_STAT_HIGH_SPEED;
  259. /*
  260. * FIXME: Yes, we should check for full speed, but the core uses that as
  261. * a default in portspeed() in usb/core/hub.c (which is the only place
  262. * USB_PORT_STAT_*_SPEED is used).
  263. */
  264. return 0;
  265. }
  266. /*
  267. * These bits are Read Only (RO) and should be saved and written to the
  268. * registers: 0, 3, 10:13, 30
  269. * connect status, over-current status, port speed, and device removable.
  270. * connect status and port speed are also sticky - meaning they're in
  271. * the AUX well and they aren't changed by a hot, warm, or cold reset.
  272. */
  273. #define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
  274. /*
  275. * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
  276. * bits 5:8, 9, 14:15, 25:27
  277. * link state, port power, port indicator state, "wake on" enable state
  278. */
  279. #define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
  280. /*
  281. * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
  282. * bit 4 (port reset)
  283. */
  284. #define XHCI_PORT_RW1S ((1<<4))
  285. /*
  286. * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
  287. * bits 1, 17, 18, 19, 20, 21, 22, 23
  288. * port enable/disable, and
  289. * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
  290. * over-current, reset, link state, and L1 change
  291. */
  292. #define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
  293. /*
  294. * Bit 16 is RW, and writing a '1' to it causes the link state control to be
  295. * latched in
  296. */
  297. #define XHCI_PORT_RW ((1<<16))
  298. /*
  299. * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
  300. * bits 2, 24, 28:31
  301. */
  302. #define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
  303. /*
  304. * Given a port state, this function returns a value that would result in the
  305. * port being in the same state, if the value was written to the port status
  306. * control register.
  307. * Save Read Only (RO) bits and save read/write bits where
  308. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  309. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  310. */
  311. u32 xhci_port_state_to_neutral(u32 state)
  312. {
  313. /* Save read-only status and port state */
  314. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  315. }
  316. /*
  317. * find slot id based on port number.
  318. * @port: The one-based port number from one of the two split roothubs.
  319. */
  320. int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  321. u16 port)
  322. {
  323. int slot_id;
  324. int i;
  325. enum usb_device_speed speed;
  326. slot_id = 0;
  327. for (i = 0; i < MAX_HC_SLOTS; i++) {
  328. if (!xhci->devs[i])
  329. continue;
  330. speed = xhci->devs[i]->udev->speed;
  331. if (((speed >= USB_SPEED_SUPER) == (hcd->speed >= HCD_USB3))
  332. && xhci->devs[i]->fake_port == port) {
  333. slot_id = i;
  334. break;
  335. }
  336. }
  337. return slot_id;
  338. }
  339. /*
  340. * Stop device
  341. * It issues stop endpoint command for EP 0 to 30. And wait the last command
  342. * to complete.
  343. * suspend will set to 1, if suspend bit need to set in command.
  344. */
  345. static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
  346. {
  347. struct xhci_virt_device *virt_dev;
  348. struct xhci_command *cmd;
  349. unsigned long flags;
  350. int ret;
  351. int i;
  352. ret = 0;
  353. virt_dev = xhci->devs[slot_id];
  354. if (!virt_dev)
  355. return -ENODEV;
  356. cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  357. if (!cmd) {
  358. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  359. return -ENOMEM;
  360. }
  361. spin_lock_irqsave(&xhci->lock, flags);
  362. for (i = LAST_EP_INDEX; i > 0; i--) {
  363. if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) {
  364. struct xhci_command *command;
  365. command = xhci_alloc_command(xhci, false, false,
  366. GFP_NOWAIT);
  367. if (!command) {
  368. spin_unlock_irqrestore(&xhci->lock, flags);
  369. xhci_free_command(xhci, cmd);
  370. return -ENOMEM;
  371. }
  372. xhci_queue_stop_endpoint(xhci, command, slot_id, i,
  373. suspend);
  374. }
  375. }
  376. xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
  377. xhci_ring_cmd_db(xhci);
  378. spin_unlock_irqrestore(&xhci->lock, flags);
  379. /* Wait for last stop endpoint command to finish */
  380. wait_for_completion(cmd->completion);
  381. if (cmd->status == COMP_CMD_ABORT || cmd->status == COMP_CMD_STOP) {
  382. xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
  383. ret = -ETIME;
  384. }
  385. xhci_free_command(xhci, cmd);
  386. return ret;
  387. }
  388. /*
  389. * Ring device, it rings the all doorbells unconditionally.
  390. */
  391. void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
  392. {
  393. int i, s;
  394. struct xhci_virt_ep *ep;
  395. for (i = 0; i < LAST_EP_INDEX + 1; i++) {
  396. ep = &xhci->devs[slot_id]->eps[i];
  397. if (ep->ep_state & EP_HAS_STREAMS) {
  398. for (s = 1; s < ep->stream_info->num_streams; s++)
  399. xhci_ring_ep_doorbell(xhci, slot_id, i, s);
  400. } else if (ep->ring && ep->ring->dequeue) {
  401. xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
  402. }
  403. }
  404. return;
  405. }
  406. static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  407. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  408. {
  409. /* Don't allow the USB core to disable SuperSpeed ports. */
  410. if (hcd->speed >= HCD_USB3) {
  411. xhci_dbg(xhci, "Ignoring request to disable "
  412. "SuperSpeed port.\n");
  413. return;
  414. }
  415. if (xhci->quirks & XHCI_BROKEN_PORT_PED) {
  416. xhci_dbg(xhci,
  417. "Broken Port Enabled/Disabled, ignoring port disable request.\n");
  418. return;
  419. }
  420. /* Write 1 to disable the port */
  421. writel(port_status | PORT_PE, addr);
  422. port_status = readl(addr);
  423. xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
  424. wIndex, port_status);
  425. }
  426. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  427. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  428. {
  429. char *port_change_bit;
  430. u32 status;
  431. switch (wValue) {
  432. case USB_PORT_FEAT_C_RESET:
  433. status = PORT_RC;
  434. port_change_bit = "reset";
  435. break;
  436. case USB_PORT_FEAT_C_BH_PORT_RESET:
  437. status = PORT_WRC;
  438. port_change_bit = "warm(BH) reset";
  439. break;
  440. case USB_PORT_FEAT_C_CONNECTION:
  441. status = PORT_CSC;
  442. port_change_bit = "connect";
  443. break;
  444. case USB_PORT_FEAT_C_OVER_CURRENT:
  445. status = PORT_OCC;
  446. port_change_bit = "over-current";
  447. break;
  448. case USB_PORT_FEAT_C_ENABLE:
  449. status = PORT_PEC;
  450. port_change_bit = "enable/disable";
  451. break;
  452. case USB_PORT_FEAT_C_SUSPEND:
  453. status = PORT_PLC;
  454. port_change_bit = "suspend/resume";
  455. break;
  456. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  457. status = PORT_PLC;
  458. port_change_bit = "link state";
  459. break;
  460. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  461. status = PORT_CEC;
  462. port_change_bit = "config error";
  463. break;
  464. default:
  465. /* Should never happen */
  466. return;
  467. }
  468. /* Change bits are all write 1 to clear */
  469. writel(port_status | status, addr);
  470. port_status = readl(addr);
  471. xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
  472. port_change_bit, wIndex, port_status);
  473. }
  474. static int xhci_get_ports(struct usb_hcd *hcd, __le32 __iomem ***port_array)
  475. {
  476. int max_ports;
  477. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  478. if (hcd->speed >= HCD_USB3) {
  479. max_ports = xhci->num_usb3_ports;
  480. *port_array = xhci->usb3_ports;
  481. } else {
  482. max_ports = xhci->num_usb2_ports;
  483. *port_array = xhci->usb2_ports;
  484. }
  485. return max_ports;
  486. }
  487. void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  488. int port_id, u32 link_state)
  489. {
  490. u32 temp;
  491. temp = readl(port_array[port_id]);
  492. temp = xhci_port_state_to_neutral(temp);
  493. temp &= ~PORT_PLS_MASK;
  494. temp |= PORT_LINK_STROBE | link_state;
  495. writel(temp, port_array[port_id]);
  496. }
  497. static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
  498. __le32 __iomem **port_array, int port_id, u16 wake_mask)
  499. {
  500. u32 temp;
  501. temp = readl(port_array[port_id]);
  502. temp = xhci_port_state_to_neutral(temp);
  503. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
  504. temp |= PORT_WKCONN_E;
  505. else
  506. temp &= ~PORT_WKCONN_E;
  507. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
  508. temp |= PORT_WKDISC_E;
  509. else
  510. temp &= ~PORT_WKDISC_E;
  511. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
  512. temp |= PORT_WKOC_E;
  513. else
  514. temp &= ~PORT_WKOC_E;
  515. writel(temp, port_array[port_id]);
  516. }
  517. /* Test and clear port RWC bit */
  518. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  519. int port_id, u32 port_bit)
  520. {
  521. u32 temp;
  522. temp = readl(port_array[port_id]);
  523. if (temp & port_bit) {
  524. temp = xhci_port_state_to_neutral(temp);
  525. temp |= port_bit;
  526. writel(temp, port_array[port_id]);
  527. }
  528. }
  529. /* Updates Link Status for USB 2.1 port */
  530. static void xhci_hub_report_usb2_link_state(u32 *status, u32 status_reg)
  531. {
  532. if ((status_reg & PORT_PLS_MASK) == XDEV_U2)
  533. *status |= USB_PORT_STAT_L1;
  534. }
  535. /* Updates Link Status for super Speed port */
  536. static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
  537. u32 *status, u32 status_reg)
  538. {
  539. u32 pls = status_reg & PORT_PLS_MASK;
  540. /* resume state is a xHCI internal state.
  541. * Do not report it to usb core, instead, pretend to be U3,
  542. * thus usb core knows it's not ready for transfer
  543. */
  544. if (pls == XDEV_RESUME) {
  545. *status |= USB_SS_PORT_LS_U3;
  546. return;
  547. }
  548. /* When the CAS bit is set then warm reset
  549. * should be performed on port
  550. */
  551. if (status_reg & PORT_CAS) {
  552. /* The CAS bit can be set while the port is
  553. * in any link state.
  554. * Only roothubs have CAS bit, so we
  555. * pretend to be in compliance mode
  556. * unless we're already in compliance
  557. * or the inactive state.
  558. */
  559. if (pls != USB_SS_PORT_LS_COMP_MOD &&
  560. pls != USB_SS_PORT_LS_SS_INACTIVE) {
  561. pls = USB_SS_PORT_LS_COMP_MOD;
  562. }
  563. /* Return also connection bit -
  564. * hub state machine resets port
  565. * when this bit is set.
  566. */
  567. pls |= USB_PORT_STAT_CONNECTION;
  568. } else {
  569. /*
  570. * If CAS bit isn't set but the Port is already at
  571. * Compliance Mode, fake a connection so the USB core
  572. * notices the Compliance state and resets the port.
  573. * This resolves an issue generated by the SN65LVPE502CP
  574. * in which sometimes the port enters compliance mode
  575. * caused by a delay on the host-device negotiation.
  576. */
  577. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  578. (pls == USB_SS_PORT_LS_COMP_MOD))
  579. pls |= USB_PORT_STAT_CONNECTION;
  580. }
  581. /* update status field */
  582. *status |= pls;
  583. }
  584. /*
  585. * Function for Compliance Mode Quirk.
  586. *
  587. * This Function verifies if all xhc USB3 ports have entered U0, if so,
  588. * the compliance mode timer is deleted. A port won't enter
  589. * compliance mode if it has previously entered U0.
  590. */
  591. static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,
  592. u16 wIndex)
  593. {
  594. u32 all_ports_seen_u0 = ((1 << xhci->num_usb3_ports)-1);
  595. bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
  596. if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
  597. return;
  598. if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
  599. xhci->port_status_u0 |= 1 << wIndex;
  600. if (xhci->port_status_u0 == all_ports_seen_u0) {
  601. del_timer_sync(&xhci->comp_mode_recovery_timer);
  602. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  603. "All USB3 ports have entered U0 already!");
  604. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  605. "Compliance Mode Recovery Timer Deleted.");
  606. }
  607. }
  608. }
  609. static u32 xhci_get_ext_port_status(u32 raw_port_status, u32 port_li)
  610. {
  611. u32 ext_stat = 0;
  612. int speed_id;
  613. /* only support rx and tx lane counts of 1 in usb3.1 spec */
  614. speed_id = DEV_PORT_SPEED(raw_port_status);
  615. ext_stat |= speed_id; /* bits 3:0, RX speed id */
  616. ext_stat |= speed_id << 4; /* bits 7:4, TX speed id */
  617. ext_stat |= PORT_RX_LANES(port_li) << 8; /* bits 11:8 Rx lane count */
  618. ext_stat |= PORT_TX_LANES(port_li) << 12; /* bits 15:12 Tx lane count */
  619. return ext_stat;
  620. }
  621. /*
  622. * Converts a raw xHCI port status into the format that external USB 2.0 or USB
  623. * 3.0 hubs use.
  624. *
  625. * Possible side effects:
  626. * - Mark a port as being done with device resume,
  627. * and ring the endpoint doorbells.
  628. * - Stop the Synopsys redriver Compliance Mode polling.
  629. * - Drop and reacquire the xHCI lock, in order to wait for port resume.
  630. */
  631. static u32 xhci_get_port_status(struct usb_hcd *hcd,
  632. struct xhci_bus_state *bus_state,
  633. __le32 __iomem **port_array,
  634. u16 wIndex, u32 raw_port_status,
  635. unsigned long flags)
  636. __releases(&xhci->lock)
  637. __acquires(&xhci->lock)
  638. {
  639. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  640. u32 status = 0;
  641. int slot_id;
  642. /* wPortChange bits */
  643. if (raw_port_status & PORT_CSC)
  644. status |= USB_PORT_STAT_C_CONNECTION << 16;
  645. if (raw_port_status & PORT_PEC)
  646. status |= USB_PORT_STAT_C_ENABLE << 16;
  647. if ((raw_port_status & PORT_OCC))
  648. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  649. if ((raw_port_status & PORT_RC))
  650. status |= USB_PORT_STAT_C_RESET << 16;
  651. /* USB3.0 only */
  652. if (hcd->speed >= HCD_USB3) {
  653. /* Port link change with port in resume state should not be
  654. * reported to usbcore, as this is an internal state to be
  655. * handled by xhci driver. Reporting PLC to usbcore may
  656. * cause usbcore clearing PLC first and port change event
  657. * irq won't be generated.
  658. */
  659. if ((raw_port_status & PORT_PLC) &&
  660. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME)
  661. status |= USB_PORT_STAT_C_LINK_STATE << 16;
  662. if ((raw_port_status & PORT_WRC))
  663. status |= USB_PORT_STAT_C_BH_RESET << 16;
  664. if ((raw_port_status & PORT_CEC))
  665. status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
  666. }
  667. if (hcd->speed < HCD_USB3) {
  668. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U3
  669. && (raw_port_status & PORT_POWER))
  670. status |= USB_PORT_STAT_SUSPEND;
  671. }
  672. if ((raw_port_status & PORT_PLS_MASK) == XDEV_RESUME &&
  673. !DEV_SUPERSPEED_ANY(raw_port_status)) {
  674. if ((raw_port_status & PORT_RESET) ||
  675. !(raw_port_status & PORT_PE))
  676. return 0xffffffff;
  677. /* did port event handler already start resume timing? */
  678. if (!bus_state->resume_done[wIndex]) {
  679. /* If not, maybe we are in a host initated resume? */
  680. if (test_bit(wIndex, &bus_state->resuming_ports)) {
  681. /* Host initated resume doesn't time the resume
  682. * signalling using resume_done[].
  683. * It manually sets RESUME state, sleeps 20ms
  684. * and sets U0 state. This should probably be
  685. * changed, but not right now.
  686. */
  687. } else {
  688. /* port resume was discovered now and here,
  689. * start resume timing
  690. */
  691. unsigned long timeout = jiffies +
  692. msecs_to_jiffies(USB_RESUME_TIMEOUT);
  693. set_bit(wIndex, &bus_state->resuming_ports);
  694. bus_state->resume_done[wIndex] = timeout;
  695. mod_timer(&hcd->rh_timer, timeout);
  696. }
  697. /* Has resume been signalled for USB_RESUME_TIME yet? */
  698. } else if (time_after_eq(jiffies,
  699. bus_state->resume_done[wIndex])) {
  700. int time_left;
  701. xhci_dbg(xhci, "Resume USB2 port %d\n",
  702. wIndex + 1);
  703. bus_state->resume_done[wIndex] = 0;
  704. clear_bit(wIndex, &bus_state->resuming_ports);
  705. set_bit(wIndex, &bus_state->rexit_ports);
  706. xhci_test_and_clear_bit(xhci, port_array, wIndex,
  707. PORT_PLC);
  708. xhci_set_link_state(xhci, port_array, wIndex,
  709. XDEV_U0);
  710. spin_unlock_irqrestore(&xhci->lock, flags);
  711. time_left = wait_for_completion_timeout(
  712. &bus_state->rexit_done[wIndex],
  713. msecs_to_jiffies(
  714. XHCI_MAX_REXIT_TIMEOUT));
  715. spin_lock_irqsave(&xhci->lock, flags);
  716. if (time_left) {
  717. slot_id = xhci_find_slot_id_by_port(hcd,
  718. xhci, wIndex + 1);
  719. if (!slot_id) {
  720. xhci_dbg(xhci, "slot_id is zero\n");
  721. return 0xffffffff;
  722. }
  723. xhci_ring_device(xhci, slot_id);
  724. } else {
  725. int port_status = readl(port_array[wIndex]);
  726. xhci_warn(xhci, "Port resume took longer than %i msec, port status = 0x%x\n",
  727. XHCI_MAX_REXIT_TIMEOUT,
  728. port_status);
  729. status |= USB_PORT_STAT_SUSPEND;
  730. clear_bit(wIndex, &bus_state->rexit_ports);
  731. }
  732. bus_state->port_c_suspend |= 1 << wIndex;
  733. bus_state->suspended_ports &= ~(1 << wIndex);
  734. } else {
  735. /*
  736. * The resume has been signaling for less than
  737. * USB_RESUME_TIME. Report the port status as SUSPEND,
  738. * let the usbcore check port status again and clear
  739. * resume signaling later.
  740. */
  741. status |= USB_PORT_STAT_SUSPEND;
  742. }
  743. }
  744. /*
  745. * Clear stale usb2 resume signalling variables in case port changed
  746. * state during resume signalling. For example on error
  747. */
  748. if ((bus_state->resume_done[wIndex] ||
  749. test_bit(wIndex, &bus_state->resuming_ports)) &&
  750. (raw_port_status & PORT_PLS_MASK) != XDEV_U3 &&
  751. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME) {
  752. bus_state->resume_done[wIndex] = 0;
  753. clear_bit(wIndex, &bus_state->resuming_ports);
  754. }
  755. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U0 &&
  756. (raw_port_status & PORT_POWER)) {
  757. if (bus_state->suspended_ports & (1 << wIndex)) {
  758. bus_state->suspended_ports &= ~(1 << wIndex);
  759. if (hcd->speed < HCD_USB3)
  760. bus_state->port_c_suspend |= 1 << wIndex;
  761. }
  762. bus_state->resume_done[wIndex] = 0;
  763. clear_bit(wIndex, &bus_state->resuming_ports);
  764. }
  765. if (raw_port_status & PORT_CONNECT) {
  766. status |= USB_PORT_STAT_CONNECTION;
  767. status |= xhci_port_speed(raw_port_status);
  768. }
  769. if (raw_port_status & PORT_PE)
  770. status |= USB_PORT_STAT_ENABLE;
  771. if (raw_port_status & PORT_OC)
  772. status |= USB_PORT_STAT_OVERCURRENT;
  773. if (raw_port_status & PORT_RESET)
  774. status |= USB_PORT_STAT_RESET;
  775. if (raw_port_status & PORT_POWER) {
  776. if (hcd->speed >= HCD_USB3)
  777. status |= USB_SS_PORT_STAT_POWER;
  778. else
  779. status |= USB_PORT_STAT_POWER;
  780. }
  781. /* Update Port Link State */
  782. if (hcd->speed >= HCD_USB3) {
  783. xhci_hub_report_usb3_link_state(xhci, &status, raw_port_status);
  784. /*
  785. * Verify if all USB3 Ports Have entered U0 already.
  786. * Delete Compliance Mode Timer if so.
  787. */
  788. xhci_del_comp_mod_timer(xhci, raw_port_status, wIndex);
  789. } else {
  790. xhci_hub_report_usb2_link_state(&status, raw_port_status);
  791. }
  792. if (bus_state->port_c_suspend & (1 << wIndex))
  793. status |= USB_PORT_STAT_C_SUSPEND << 16;
  794. return status;
  795. }
  796. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  797. u16 wIndex, char *buf, u16 wLength)
  798. {
  799. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  800. int max_ports;
  801. unsigned long flags;
  802. u32 temp, status;
  803. int retval = 0;
  804. __le32 __iomem **port_array;
  805. int slot_id;
  806. struct xhci_bus_state *bus_state;
  807. u16 link_state = 0;
  808. u16 wake_mask = 0;
  809. u16 timeout = 0;
  810. max_ports = xhci_get_ports(hcd, &port_array);
  811. bus_state = &xhci->bus_state[hcd_index(hcd)];
  812. spin_lock_irqsave(&xhci->lock, flags);
  813. switch (typeReq) {
  814. case GetHubStatus:
  815. /* No power source, over-current reported per port */
  816. memset(buf, 0, 4);
  817. break;
  818. case GetHubDescriptor:
  819. /* Check to make sure userspace is asking for the USB 3.0 hub
  820. * descriptor for the USB 3.0 roothub. If not, we stall the
  821. * endpoint, like external hubs do.
  822. */
  823. if (hcd->speed >= HCD_USB3 &&
  824. (wLength < USB_DT_SS_HUB_SIZE ||
  825. wValue != (USB_DT_SS_HUB << 8))) {
  826. xhci_dbg(xhci, "Wrong hub descriptor type for "
  827. "USB 3.0 roothub.\n");
  828. goto error;
  829. }
  830. xhci_hub_descriptor(hcd, xhci,
  831. (struct usb_hub_descriptor *) buf);
  832. break;
  833. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  834. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  835. goto error;
  836. if (hcd->speed < HCD_USB3)
  837. goto error;
  838. retval = xhci_create_usb3_bos_desc(xhci, buf, wLength);
  839. spin_unlock_irqrestore(&xhci->lock, flags);
  840. return retval;
  841. case GetPortStatus:
  842. if (!wIndex || wIndex > max_ports)
  843. goto error;
  844. wIndex--;
  845. temp = readl(port_array[wIndex]);
  846. if (temp == 0xffffffff) {
  847. retval = -ENODEV;
  848. break;
  849. }
  850. status = xhci_get_port_status(hcd, bus_state, port_array,
  851. wIndex, temp, flags);
  852. if (status == 0xffffffff)
  853. goto error;
  854. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n",
  855. wIndex, temp);
  856. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  857. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  858. /* if USB 3.1 extended port status return additional 4 bytes */
  859. if (wValue == 0x02) {
  860. u32 port_li;
  861. if (hcd->speed < HCD_USB31 || wLength != 8) {
  862. xhci_err(xhci, "get ext port status invalid parameter\n");
  863. retval = -EINVAL;
  864. break;
  865. }
  866. port_li = readl(port_array[wIndex] + PORTLI);
  867. status = xhci_get_ext_port_status(temp, port_li);
  868. put_unaligned_le32(cpu_to_le32(status), &buf[4]);
  869. }
  870. break;
  871. case SetPortFeature:
  872. if (wValue == USB_PORT_FEAT_LINK_STATE)
  873. link_state = (wIndex & 0xff00) >> 3;
  874. if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
  875. wake_mask = wIndex & 0xff00;
  876. /* The MSB of wIndex is the U1/U2 timeout */
  877. timeout = (wIndex & 0xff00) >> 8;
  878. wIndex &= 0xff;
  879. if (!wIndex || wIndex > max_ports)
  880. goto error;
  881. wIndex--;
  882. temp = readl(port_array[wIndex]);
  883. if (temp == 0xffffffff) {
  884. retval = -ENODEV;
  885. break;
  886. }
  887. temp = xhci_port_state_to_neutral(temp);
  888. /* FIXME: What new port features do we need to support? */
  889. switch (wValue) {
  890. case USB_PORT_FEAT_SUSPEND:
  891. temp = readl(port_array[wIndex]);
  892. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  893. /* Resume the port to U0 first */
  894. xhci_set_link_state(xhci, port_array, wIndex,
  895. XDEV_U0);
  896. spin_unlock_irqrestore(&xhci->lock, flags);
  897. msleep(10);
  898. spin_lock_irqsave(&xhci->lock, flags);
  899. }
  900. /* In spec software should not attempt to suspend
  901. * a port unless the port reports that it is in the
  902. * enabled (PED = ‘1’,PLS < ‘3’) state.
  903. */
  904. temp = readl(port_array[wIndex]);
  905. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  906. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  907. xhci_warn(xhci, "USB core suspending device "
  908. "not in U0/U1/U2.\n");
  909. goto error;
  910. }
  911. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  912. wIndex + 1);
  913. if (!slot_id) {
  914. xhci_warn(xhci, "slot_id is zero\n");
  915. goto error;
  916. }
  917. /* unlock to execute stop endpoint commands */
  918. spin_unlock_irqrestore(&xhci->lock, flags);
  919. xhci_stop_device(xhci, slot_id, 1);
  920. spin_lock_irqsave(&xhci->lock, flags);
  921. xhci_set_link_state(xhci, port_array, wIndex, XDEV_U3);
  922. spin_unlock_irqrestore(&xhci->lock, flags);
  923. msleep(10); /* wait device to enter */
  924. spin_lock_irqsave(&xhci->lock, flags);
  925. temp = readl(port_array[wIndex]);
  926. bus_state->suspended_ports |= 1 << wIndex;
  927. break;
  928. case USB_PORT_FEAT_LINK_STATE:
  929. temp = readl(port_array[wIndex]);
  930. /* Disable port */
  931. if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
  932. xhci_dbg(xhci, "Disable port %d\n", wIndex);
  933. temp = xhci_port_state_to_neutral(temp);
  934. /*
  935. * Clear all change bits, so that we get a new
  936. * connection event.
  937. */
  938. temp |= PORT_CSC | PORT_PEC | PORT_WRC |
  939. PORT_OCC | PORT_RC | PORT_PLC |
  940. PORT_CEC;
  941. writel(temp | PORT_PE, port_array[wIndex]);
  942. temp = readl(port_array[wIndex]);
  943. break;
  944. }
  945. /* Put link in RxDetect (enable port) */
  946. if (link_state == USB_SS_PORT_LS_RX_DETECT) {
  947. xhci_dbg(xhci, "Enable port %d\n", wIndex);
  948. xhci_set_link_state(xhci, port_array, wIndex,
  949. link_state);
  950. temp = readl(port_array[wIndex]);
  951. break;
  952. }
  953. /* Software should not attempt to set
  954. * port link state above '3' (U3) and the port
  955. * must be enabled.
  956. */
  957. if ((temp & PORT_PE) == 0 ||
  958. (link_state > USB_SS_PORT_LS_U3)) {
  959. xhci_warn(xhci, "Cannot set link state.\n");
  960. goto error;
  961. }
  962. if (link_state == USB_SS_PORT_LS_U3) {
  963. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  964. wIndex + 1);
  965. if (slot_id) {
  966. /* unlock to execute stop endpoint
  967. * commands */
  968. spin_unlock_irqrestore(&xhci->lock,
  969. flags);
  970. xhci_stop_device(xhci, slot_id, 1);
  971. spin_lock_irqsave(&xhci->lock, flags);
  972. }
  973. }
  974. xhci_set_link_state(xhci, port_array, wIndex,
  975. link_state);
  976. spin_unlock_irqrestore(&xhci->lock, flags);
  977. msleep(20); /* wait device to enter */
  978. spin_lock_irqsave(&xhci->lock, flags);
  979. temp = readl(port_array[wIndex]);
  980. if (link_state == USB_SS_PORT_LS_U3)
  981. bus_state->suspended_ports |= 1 << wIndex;
  982. break;
  983. case USB_PORT_FEAT_POWER:
  984. /*
  985. * Turn on ports, even if there isn't per-port switching.
  986. * HC will report connect events even before this is set.
  987. * However, hub_wq will ignore the roothub events until
  988. * the roothub is registered.
  989. */
  990. writel(temp | PORT_POWER, port_array[wIndex]);
  991. temp = readl(port_array[wIndex]);
  992. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
  993. spin_unlock_irqrestore(&xhci->lock, flags);
  994. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  995. wIndex);
  996. if (temp)
  997. usb_acpi_set_power_state(hcd->self.root_hub,
  998. wIndex, true);
  999. spin_lock_irqsave(&xhci->lock, flags);
  1000. break;
  1001. case USB_PORT_FEAT_RESET:
  1002. temp = (temp | PORT_RESET);
  1003. writel(temp, port_array[wIndex]);
  1004. temp = readl(port_array[wIndex]);
  1005. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  1006. break;
  1007. case USB_PORT_FEAT_REMOTE_WAKE_MASK:
  1008. xhci_set_remote_wake_mask(xhci, port_array,
  1009. wIndex, wake_mask);
  1010. temp = readl(port_array[wIndex]);
  1011. xhci_dbg(xhci, "set port remote wake mask, "
  1012. "actual port %d status = 0x%x\n",
  1013. wIndex, temp);
  1014. break;
  1015. case USB_PORT_FEAT_BH_PORT_RESET:
  1016. temp |= PORT_WR;
  1017. writel(temp, port_array[wIndex]);
  1018. temp = readl(port_array[wIndex]);
  1019. break;
  1020. case USB_PORT_FEAT_U1_TIMEOUT:
  1021. if (hcd->speed < HCD_USB3)
  1022. goto error;
  1023. temp = readl(port_array[wIndex] + PORTPMSC);
  1024. temp &= ~PORT_U1_TIMEOUT_MASK;
  1025. temp |= PORT_U1_TIMEOUT(timeout);
  1026. writel(temp, port_array[wIndex] + PORTPMSC);
  1027. break;
  1028. case USB_PORT_FEAT_U2_TIMEOUT:
  1029. if (hcd->speed < HCD_USB3)
  1030. goto error;
  1031. temp = readl(port_array[wIndex] + PORTPMSC);
  1032. temp &= ~PORT_U2_TIMEOUT_MASK;
  1033. temp |= PORT_U2_TIMEOUT(timeout);
  1034. writel(temp, port_array[wIndex] + PORTPMSC);
  1035. break;
  1036. default:
  1037. goto error;
  1038. }
  1039. /* unblock any posted writes */
  1040. temp = readl(port_array[wIndex]);
  1041. break;
  1042. case ClearPortFeature:
  1043. if (!wIndex || wIndex > max_ports)
  1044. goto error;
  1045. wIndex--;
  1046. temp = readl(port_array[wIndex]);
  1047. if (temp == 0xffffffff) {
  1048. retval = -ENODEV;
  1049. break;
  1050. }
  1051. /* FIXME: What new port features do we need to support? */
  1052. temp = xhci_port_state_to_neutral(temp);
  1053. switch (wValue) {
  1054. case USB_PORT_FEAT_SUSPEND:
  1055. temp = readl(port_array[wIndex]);
  1056. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  1057. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  1058. if (temp & PORT_RESET)
  1059. goto error;
  1060. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  1061. if ((temp & PORT_PE) == 0)
  1062. goto error;
  1063. set_bit(wIndex, &bus_state->resuming_ports);
  1064. xhci_set_link_state(xhci, port_array, wIndex,
  1065. XDEV_RESUME);
  1066. spin_unlock_irqrestore(&xhci->lock, flags);
  1067. msleep(USB_RESUME_TIMEOUT);
  1068. spin_lock_irqsave(&xhci->lock, flags);
  1069. xhci_set_link_state(xhci, port_array, wIndex,
  1070. XDEV_U0);
  1071. clear_bit(wIndex, &bus_state->resuming_ports);
  1072. }
  1073. bus_state->port_c_suspend |= 1 << wIndex;
  1074. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1075. wIndex + 1);
  1076. if (!slot_id) {
  1077. xhci_dbg(xhci, "slot_id is zero\n");
  1078. goto error;
  1079. }
  1080. xhci_ring_device(xhci, slot_id);
  1081. break;
  1082. case USB_PORT_FEAT_C_SUSPEND:
  1083. bus_state->port_c_suspend &= ~(1 << wIndex);
  1084. case USB_PORT_FEAT_C_RESET:
  1085. case USB_PORT_FEAT_C_BH_PORT_RESET:
  1086. case USB_PORT_FEAT_C_CONNECTION:
  1087. case USB_PORT_FEAT_C_OVER_CURRENT:
  1088. case USB_PORT_FEAT_C_ENABLE:
  1089. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  1090. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  1091. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  1092. port_array[wIndex], temp);
  1093. break;
  1094. case USB_PORT_FEAT_ENABLE:
  1095. xhci_disable_port(hcd, xhci, wIndex,
  1096. port_array[wIndex], temp);
  1097. break;
  1098. case USB_PORT_FEAT_POWER:
  1099. writel(temp & ~PORT_POWER, port_array[wIndex]);
  1100. spin_unlock_irqrestore(&xhci->lock, flags);
  1101. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  1102. wIndex);
  1103. if (temp)
  1104. usb_acpi_set_power_state(hcd->self.root_hub,
  1105. wIndex, false);
  1106. spin_lock_irqsave(&xhci->lock, flags);
  1107. break;
  1108. default:
  1109. goto error;
  1110. }
  1111. break;
  1112. default:
  1113. error:
  1114. /* "stall" on error */
  1115. retval = -EPIPE;
  1116. }
  1117. spin_unlock_irqrestore(&xhci->lock, flags);
  1118. return retval;
  1119. }
  1120. /*
  1121. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  1122. * Ports are 0-indexed from the HCD point of view,
  1123. * and 1-indexed from the USB core pointer of view.
  1124. *
  1125. * Note that the status change bits will be cleared as soon as a port status
  1126. * change event is generated, so we use the saved status from that event.
  1127. */
  1128. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  1129. {
  1130. unsigned long flags;
  1131. u32 temp, status;
  1132. u32 mask;
  1133. int i, retval;
  1134. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1135. int max_ports;
  1136. __le32 __iomem **port_array;
  1137. struct xhci_bus_state *bus_state;
  1138. bool reset_change = false;
  1139. max_ports = xhci_get_ports(hcd, &port_array);
  1140. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1141. /* Initial status is no changes */
  1142. retval = (max_ports + 8) / 8;
  1143. memset(buf, 0, retval);
  1144. /*
  1145. * Inform the usbcore about resume-in-progress by returning
  1146. * a non-zero value even if there are no status changes.
  1147. */
  1148. status = bus_state->resuming_ports;
  1149. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
  1150. spin_lock_irqsave(&xhci->lock, flags);
  1151. /* For each port, did anything change? If so, set that bit in buf. */
  1152. for (i = 0; i < max_ports; i++) {
  1153. temp = readl(port_array[i]);
  1154. if (temp == 0xffffffff) {
  1155. retval = -ENODEV;
  1156. break;
  1157. }
  1158. if ((temp & mask) != 0 ||
  1159. (bus_state->port_c_suspend & 1 << i) ||
  1160. (bus_state->resume_done[i] && time_after_eq(
  1161. jiffies, bus_state->resume_done[i]))) {
  1162. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  1163. status = 1;
  1164. }
  1165. if ((temp & PORT_RC))
  1166. reset_change = true;
  1167. }
  1168. if (!status && !reset_change) {
  1169. xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
  1170. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  1171. }
  1172. spin_unlock_irqrestore(&xhci->lock, flags);
  1173. return status ? retval : 0;
  1174. }
  1175. #ifdef CONFIG_PM
  1176. int xhci_bus_suspend(struct usb_hcd *hcd)
  1177. {
  1178. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1179. int max_ports, port_index;
  1180. __le32 __iomem **port_array;
  1181. struct xhci_bus_state *bus_state;
  1182. unsigned long flags;
  1183. max_ports = xhci_get_ports(hcd, &port_array);
  1184. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1185. spin_lock_irqsave(&xhci->lock, flags);
  1186. if (hcd->self.root_hub->do_remote_wakeup) {
  1187. if (bus_state->resuming_ports || /* USB2 */
  1188. bus_state->port_remote_wakeup) { /* USB3 */
  1189. spin_unlock_irqrestore(&xhci->lock, flags);
  1190. xhci_dbg(xhci, "suspend failed because a port is resuming\n");
  1191. return -EBUSY;
  1192. }
  1193. }
  1194. port_index = max_ports;
  1195. bus_state->bus_suspended = 0;
  1196. while (port_index--) {
  1197. /* suspend the port if the port is not suspended */
  1198. u32 t1, t2;
  1199. int slot_id;
  1200. t1 = readl(port_array[port_index]);
  1201. t2 = xhci_port_state_to_neutral(t1);
  1202. if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
  1203. xhci_dbg(xhci, "port %d not suspended\n", port_index);
  1204. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1205. port_index + 1);
  1206. if (slot_id) {
  1207. spin_unlock_irqrestore(&xhci->lock, flags);
  1208. xhci_stop_device(xhci, slot_id, 1);
  1209. spin_lock_irqsave(&xhci->lock, flags);
  1210. }
  1211. t2 &= ~PORT_PLS_MASK;
  1212. t2 |= PORT_LINK_STROBE | XDEV_U3;
  1213. set_bit(port_index, &bus_state->bus_suspended);
  1214. }
  1215. /* USB core sets remote wake mask for USB 3.0 hubs,
  1216. * including the USB 3.0 roothub, but only if CONFIG_PM
  1217. * is enabled, so also enable remote wake here.
  1218. */
  1219. if (hcd->self.root_hub->do_remote_wakeup) {
  1220. if (t1 & PORT_CONNECT) {
  1221. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  1222. t2 &= ~PORT_WKCONN_E;
  1223. } else {
  1224. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  1225. t2 &= ~PORT_WKDISC_E;
  1226. }
  1227. } else
  1228. t2 &= ~PORT_WAKE_BITS;
  1229. t1 = xhci_port_state_to_neutral(t1);
  1230. if (t1 != t2)
  1231. writel(t2, port_array[port_index]);
  1232. }
  1233. hcd->state = HC_STATE_SUSPENDED;
  1234. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  1235. spin_unlock_irqrestore(&xhci->lock, flags);
  1236. return 0;
  1237. }
  1238. /*
  1239. * Workaround for missing Cold Attach Status (CAS) if device re-plugged in S3.
  1240. * warm reset a USB3 device stuck in polling or compliance mode after resume.
  1241. * See Intel 100/c230 series PCH specification update Doc #332692-006 Errata #8
  1242. */
  1243. static bool xhci_port_missing_cas_quirk(int port_index,
  1244. __le32 __iomem **port_array)
  1245. {
  1246. u32 portsc;
  1247. portsc = readl(port_array[port_index]);
  1248. /* if any of these are set we are not stuck */
  1249. if (portsc & (PORT_CONNECT | PORT_CAS))
  1250. return false;
  1251. if (((portsc & PORT_PLS_MASK) != XDEV_POLLING) &&
  1252. ((portsc & PORT_PLS_MASK) != XDEV_COMP_MODE))
  1253. return false;
  1254. /* clear wakeup/change bits, and do a warm port reset */
  1255. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1256. portsc |= PORT_WR;
  1257. writel(portsc, port_array[port_index]);
  1258. /* flush write */
  1259. readl(port_array[port_index]);
  1260. return true;
  1261. }
  1262. int xhci_bus_resume(struct usb_hcd *hcd)
  1263. {
  1264. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1265. int max_ports, port_index;
  1266. __le32 __iomem **port_array;
  1267. struct xhci_bus_state *bus_state;
  1268. u32 temp;
  1269. unsigned long flags;
  1270. unsigned long port_was_suspended = 0;
  1271. bool need_usb2_u3_exit = false;
  1272. int slot_id;
  1273. int sret;
  1274. max_ports = xhci_get_ports(hcd, &port_array);
  1275. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1276. if (time_before(jiffies, bus_state->next_statechange))
  1277. msleep(5);
  1278. spin_lock_irqsave(&xhci->lock, flags);
  1279. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1280. spin_unlock_irqrestore(&xhci->lock, flags);
  1281. return -ESHUTDOWN;
  1282. }
  1283. /* delay the irqs */
  1284. temp = readl(&xhci->op_regs->command);
  1285. temp &= ~CMD_EIE;
  1286. writel(temp, &xhci->op_regs->command);
  1287. port_index = max_ports;
  1288. while (port_index--) {
  1289. /* Check whether need resume ports. If needed
  1290. resume port and disable remote wakeup */
  1291. u32 temp;
  1292. temp = readl(port_array[port_index]);
  1293. /* warm reset CAS limited ports stuck in polling/compliance */
  1294. if ((xhci->quirks & XHCI_MISSING_CAS) &&
  1295. (hcd->speed >= HCD_USB3) &&
  1296. xhci_port_missing_cas_quirk(port_index, port_array)) {
  1297. xhci_dbg(xhci, "reset stuck port %d\n", port_index);
  1298. continue;
  1299. }
  1300. if (DEV_SUPERSPEED_ANY(temp))
  1301. temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1302. else
  1303. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  1304. if (test_bit(port_index, &bus_state->bus_suspended) &&
  1305. (temp & PORT_PLS_MASK)) {
  1306. set_bit(port_index, &port_was_suspended);
  1307. if (!DEV_SUPERSPEED_ANY(temp)) {
  1308. xhci_set_link_state(xhci, port_array,
  1309. port_index, XDEV_RESUME);
  1310. need_usb2_u3_exit = true;
  1311. }
  1312. } else
  1313. writel(temp, port_array[port_index]);
  1314. }
  1315. if (need_usb2_u3_exit) {
  1316. spin_unlock_irqrestore(&xhci->lock, flags);
  1317. msleep(USB_RESUME_TIMEOUT);
  1318. spin_lock_irqsave(&xhci->lock, flags);
  1319. }
  1320. port_index = max_ports;
  1321. while (port_index--) {
  1322. if (!(port_was_suspended & BIT(port_index)))
  1323. continue;
  1324. /* Clear PLC to poll it later after XDEV_U0 */
  1325. xhci_test_and_clear_bit(xhci, port_array, port_index, PORT_PLC);
  1326. xhci_set_link_state(xhci, port_array, port_index, XDEV_U0);
  1327. }
  1328. port_index = max_ports;
  1329. while (port_index--) {
  1330. if (!(port_was_suspended & BIT(port_index)))
  1331. continue;
  1332. /* Poll and Clear PLC */
  1333. sret = xhci_handshake(port_array[port_index], PORT_PLC,
  1334. PORT_PLC, 10 * 1000);
  1335. if (sret)
  1336. xhci_warn(xhci, "port %d resume PLC timeout\n",
  1337. port_index);
  1338. xhci_test_and_clear_bit(xhci, port_array, port_index, PORT_PLC);
  1339. slot_id = xhci_find_slot_id_by_port(hcd, xhci, port_index + 1);
  1340. if (slot_id)
  1341. xhci_ring_device(xhci, slot_id);
  1342. }
  1343. (void) readl(&xhci->op_regs->command);
  1344. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  1345. /* re-enable irqs */
  1346. temp = readl(&xhci->op_regs->command);
  1347. temp |= CMD_EIE;
  1348. writel(temp, &xhci->op_regs->command);
  1349. temp = readl(&xhci->op_regs->command);
  1350. spin_unlock_irqrestore(&xhci->lock, flags);
  1351. return 0;
  1352. }
  1353. #endif /* CONFIG_PM */