xhci-hub.c 51 KB

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