xhci-hub.c 50 KB

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