xhci-hub.c 49 KB

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