xhci-hub.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  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 %d-%d, portsc: 0x%x\n",
  438. hcd->self.busnum, wIndex + 1, 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%d %s change, portsc: 0x%x\n",
  486. wIndex + 1, port_change_bit, 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. xhci_dbg(xhci, "set port power %d-%d %s, portsc: 0x%x\n",
  510. hcd->self.busnum, index + 1, on ? "ON" : "OFF", temp);
  511. temp = xhci_port_state_to_neutral(temp);
  512. if (on) {
  513. /* Power on */
  514. writel(temp | PORT_POWER, port->addr);
  515. readl(port->addr);
  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. u32 portsc;
  602. portsc = readl(port->addr);
  603. temp = xhci_port_state_to_neutral(portsc);
  604. temp &= ~PORT_PLS_MASK;
  605. temp |= PORT_LINK_STROBE | link_state;
  606. writel(temp, port->addr);
  607. xhci_dbg(xhci, "Set port %d-%d link state, portsc: 0x%x, write 0x%x",
  608. port->rhub->hcd->self.busnum, port->hcd_portnum + 1,
  609. portsc, temp);
  610. }
  611. static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
  612. struct xhci_port *port, u16 wake_mask)
  613. {
  614. u32 temp;
  615. temp = readl(port->addr);
  616. temp = xhci_port_state_to_neutral(temp);
  617. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
  618. temp |= PORT_WKCONN_E;
  619. else
  620. temp &= ~PORT_WKCONN_E;
  621. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
  622. temp |= PORT_WKDISC_E;
  623. else
  624. temp &= ~PORT_WKDISC_E;
  625. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
  626. temp |= PORT_WKOC_E;
  627. else
  628. temp &= ~PORT_WKOC_E;
  629. writel(temp, port->addr);
  630. }
  631. /* Test and clear port RWC bit */
  632. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, struct xhci_port *port,
  633. u32 port_bit)
  634. {
  635. u32 temp;
  636. temp = readl(port->addr);
  637. if (temp & port_bit) {
  638. temp = xhci_port_state_to_neutral(temp);
  639. temp |= port_bit;
  640. writel(temp, port->addr);
  641. }
  642. }
  643. /* Updates Link Status for super Speed port */
  644. static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
  645. u32 *status, u32 status_reg)
  646. {
  647. u32 pls = status_reg & PORT_PLS_MASK;
  648. /* When the CAS bit is set then warm reset
  649. * should be performed on port
  650. */
  651. if (status_reg & PORT_CAS) {
  652. /* The CAS bit can be set while the port is
  653. * in any link state.
  654. * Only roothubs have CAS bit, so we
  655. * pretend to be in compliance mode
  656. * unless we're already in compliance
  657. * or the inactive state.
  658. */
  659. if (pls != USB_SS_PORT_LS_COMP_MOD &&
  660. pls != USB_SS_PORT_LS_SS_INACTIVE) {
  661. pls = USB_SS_PORT_LS_COMP_MOD;
  662. }
  663. /* Return also connection bit -
  664. * hub state machine resets port
  665. * when this bit is set.
  666. */
  667. pls |= USB_PORT_STAT_CONNECTION;
  668. } else {
  669. /*
  670. * Resume state is an xHCI internal state. Do not report it to
  671. * usb core, instead, pretend to be U3, thus usb core knows
  672. * it's not ready for transfer.
  673. */
  674. if (pls == XDEV_RESUME) {
  675. *status |= USB_SS_PORT_LS_U3;
  676. return;
  677. }
  678. /*
  679. * If CAS bit isn't set but the Port is already at
  680. * Compliance Mode, fake a connection so the USB core
  681. * notices the Compliance state and resets the port.
  682. * This resolves an issue generated by the SN65LVPE502CP
  683. * in which sometimes the port enters compliance mode
  684. * caused by a delay on the host-device negotiation.
  685. */
  686. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  687. (pls == USB_SS_PORT_LS_COMP_MOD))
  688. pls |= USB_PORT_STAT_CONNECTION;
  689. }
  690. /* update status field */
  691. *status |= pls;
  692. }
  693. /*
  694. * Function for Compliance Mode Quirk.
  695. *
  696. * This Function verifies if all xhc USB3 ports have entered U0, if so,
  697. * the compliance mode timer is deleted. A port won't enter
  698. * compliance mode if it has previously entered U0.
  699. */
  700. static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,
  701. u16 wIndex)
  702. {
  703. u32 all_ports_seen_u0 = ((1 << xhci->usb3_rhub.num_ports) - 1);
  704. bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
  705. if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
  706. return;
  707. if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
  708. xhci->port_status_u0 |= 1 << wIndex;
  709. if (xhci->port_status_u0 == all_ports_seen_u0) {
  710. del_timer_sync(&xhci->comp_mode_recovery_timer);
  711. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  712. "All USB3 ports have entered U0 already!");
  713. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  714. "Compliance Mode Recovery Timer Deleted.");
  715. }
  716. }
  717. }
  718. static int xhci_handle_usb2_port_link_resume(struct xhci_port *port,
  719. u32 *status, u32 portsc,
  720. unsigned long *flags)
  721. {
  722. struct xhci_bus_state *bus_state;
  723. struct xhci_hcd *xhci;
  724. struct usb_hcd *hcd;
  725. int slot_id;
  726. u32 wIndex;
  727. hcd = port->rhub->hcd;
  728. bus_state = &port->rhub->bus_state;
  729. xhci = hcd_to_xhci(hcd);
  730. wIndex = port->hcd_portnum;
  731. if ((portsc & PORT_RESET) || !(portsc & PORT_PE)) {
  732. *status = 0xffffffff;
  733. return -EINVAL;
  734. }
  735. /* did port event handler already start resume timing? */
  736. if (!bus_state->resume_done[wIndex]) {
  737. /* If not, maybe we are in a host initated resume? */
  738. if (test_bit(wIndex, &bus_state->resuming_ports)) {
  739. /* Host initated resume doesn't time the resume
  740. * signalling using resume_done[].
  741. * It manually sets RESUME state, sleeps 20ms
  742. * and sets U0 state. This should probably be
  743. * changed, but not right now.
  744. */
  745. } else {
  746. /* port resume was discovered now and here,
  747. * start resume timing
  748. */
  749. unsigned long timeout = jiffies +
  750. msecs_to_jiffies(USB_RESUME_TIMEOUT);
  751. set_bit(wIndex, &bus_state->resuming_ports);
  752. bus_state->resume_done[wIndex] = timeout;
  753. mod_timer(&hcd->rh_timer, timeout);
  754. usb_hcd_start_port_resume(&hcd->self, wIndex);
  755. }
  756. /* Has resume been signalled for USB_RESUME_TIME yet? */
  757. } else if (time_after_eq(jiffies, bus_state->resume_done[wIndex])) {
  758. int time_left;
  759. xhci_dbg(xhci, "resume USB2 port %d-%d\n",
  760. hcd->self.busnum, wIndex + 1);
  761. bus_state->resume_done[wIndex] = 0;
  762. clear_bit(wIndex, &bus_state->resuming_ports);
  763. set_bit(wIndex, &bus_state->rexit_ports);
  764. xhci_test_and_clear_bit(xhci, port, PORT_PLC);
  765. xhci_set_link_state(xhci, port, XDEV_U0);
  766. spin_unlock_irqrestore(&xhci->lock, *flags);
  767. time_left = wait_for_completion_timeout(
  768. &bus_state->rexit_done[wIndex],
  769. msecs_to_jiffies(XHCI_MAX_REXIT_TIMEOUT_MS));
  770. spin_lock_irqsave(&xhci->lock, *flags);
  771. if (time_left) {
  772. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  773. wIndex + 1);
  774. if (!slot_id) {
  775. xhci_dbg(xhci, "slot_id is zero\n");
  776. *status = 0xffffffff;
  777. return -ENODEV;
  778. }
  779. xhci_ring_device(xhci, slot_id);
  780. } else {
  781. int port_status = readl(port->addr);
  782. xhci_warn(xhci, "Port resume timed out, port %d-%d: 0x%x\n",
  783. hcd->self.busnum, wIndex + 1, port_status);
  784. *status |= USB_PORT_STAT_SUSPEND;
  785. clear_bit(wIndex, &bus_state->rexit_ports);
  786. }
  787. usb_hcd_end_port_resume(&hcd->self, wIndex);
  788. bus_state->port_c_suspend |= 1 << wIndex;
  789. bus_state->suspended_ports &= ~(1 << wIndex);
  790. } else {
  791. /*
  792. * The resume has been signaling for less than
  793. * USB_RESUME_TIME. Report the port status as SUSPEND,
  794. * let the usbcore check port status again and clear
  795. * resume signaling later.
  796. */
  797. *status |= USB_PORT_STAT_SUSPEND;
  798. }
  799. return 0;
  800. }
  801. static u32 xhci_get_ext_port_status(u32 raw_port_status, u32 port_li)
  802. {
  803. u32 ext_stat = 0;
  804. int speed_id;
  805. /* only support rx and tx lane counts of 1 in usb3.1 spec */
  806. speed_id = DEV_PORT_SPEED(raw_port_status);
  807. ext_stat |= speed_id; /* bits 3:0, RX speed id */
  808. ext_stat |= speed_id << 4; /* bits 7:4, TX speed id */
  809. ext_stat |= PORT_RX_LANES(port_li) << 8; /* bits 11:8 Rx lane count */
  810. ext_stat |= PORT_TX_LANES(port_li) << 12; /* bits 15:12 Tx lane count */
  811. return ext_stat;
  812. }
  813. static void xhci_get_usb3_port_status(struct xhci_port *port, u32 *status,
  814. u32 portsc)
  815. {
  816. struct xhci_bus_state *bus_state;
  817. struct xhci_hcd *xhci;
  818. struct usb_hcd *hcd;
  819. u32 link_state;
  820. u32 portnum;
  821. bus_state = &port->rhub->bus_state;
  822. xhci = hcd_to_xhci(port->rhub->hcd);
  823. hcd = port->rhub->hcd;
  824. link_state = portsc & PORT_PLS_MASK;
  825. portnum = port->hcd_portnum;
  826. /* USB3 specific wPortChange bits
  827. *
  828. * Port link change with port in resume state should not be
  829. * reported to usbcore, as this is an internal state to be
  830. * handled by xhci driver. Reporting PLC to usbcore may
  831. * cause usbcore clearing PLC first and port change event
  832. * irq won't be generated.
  833. */
  834. if (portsc & PORT_PLC && (link_state != XDEV_RESUME))
  835. *status |= USB_PORT_STAT_C_LINK_STATE << 16;
  836. if (portsc & PORT_WRC)
  837. *status |= USB_PORT_STAT_C_BH_RESET << 16;
  838. if (portsc & PORT_CEC)
  839. *status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
  840. /* USB3 specific wPortStatus bits */
  841. if (portsc & PORT_POWER) {
  842. *status |= USB_SS_PORT_STAT_POWER;
  843. /* link state handling */
  844. if (link_state == XDEV_U0)
  845. bus_state->suspended_ports &= ~(1 << portnum);
  846. }
  847. /* remote wake resume signaling complete */
  848. if (bus_state->port_remote_wakeup & (1 << portnum) &&
  849. link_state != XDEV_RESUME &&
  850. link_state != XDEV_RECOVERY) {
  851. bus_state->port_remote_wakeup &= ~(1 << portnum);
  852. usb_hcd_end_port_resume(&hcd->self, portnum);
  853. }
  854. xhci_hub_report_usb3_link_state(xhci, status, portsc);
  855. xhci_del_comp_mod_timer(xhci, portsc, portnum);
  856. }
  857. static void xhci_get_usb2_port_status(struct xhci_port *port, u32 *status,
  858. u32 portsc, unsigned long *flags)
  859. {
  860. struct xhci_bus_state *bus_state;
  861. u32 link_state;
  862. u32 portnum;
  863. int ret;
  864. bus_state = &port->rhub->bus_state;
  865. link_state = portsc & PORT_PLS_MASK;
  866. portnum = port->hcd_portnum;
  867. /* USB2 wPortStatus bits */
  868. if (portsc & PORT_POWER) {
  869. *status |= USB_PORT_STAT_POWER;
  870. /* link state is only valid if port is powered */
  871. if (link_state == XDEV_U3)
  872. *status |= USB_PORT_STAT_SUSPEND;
  873. if (link_state == XDEV_U2)
  874. *status |= USB_PORT_STAT_L1;
  875. if (link_state == XDEV_U0) {
  876. bus_state->resume_done[portnum] = 0;
  877. clear_bit(portnum, &bus_state->resuming_ports);
  878. if (bus_state->suspended_ports & (1 << portnum)) {
  879. bus_state->suspended_ports &= ~(1 << portnum);
  880. bus_state->port_c_suspend |= 1 << portnum;
  881. }
  882. }
  883. if (link_state == XDEV_RESUME) {
  884. ret = xhci_handle_usb2_port_link_resume(port, status,
  885. portsc, flags);
  886. if (ret)
  887. return;
  888. }
  889. }
  890. }
  891. /*
  892. * Converts a raw xHCI port status into the format that external USB 2.0 or USB
  893. * 3.0 hubs use.
  894. *
  895. * Possible side effects:
  896. * - Mark a port as being done with device resume,
  897. * and ring the endpoint doorbells.
  898. * - Stop the Synopsys redriver Compliance Mode polling.
  899. * - Drop and reacquire the xHCI lock, in order to wait for port resume.
  900. */
  901. static u32 xhci_get_port_status(struct usb_hcd *hcd,
  902. struct xhci_bus_state *bus_state,
  903. u16 wIndex, u32 raw_port_status,
  904. unsigned long *flags)
  905. __releases(&xhci->lock)
  906. __acquires(&xhci->lock)
  907. {
  908. u32 status = 0;
  909. struct xhci_hub *rhub;
  910. struct xhci_port *port;
  911. rhub = xhci_get_rhub(hcd);
  912. port = rhub->ports[wIndex];
  913. /* common wPortChange bits */
  914. if (raw_port_status & PORT_CSC)
  915. status |= USB_PORT_STAT_C_CONNECTION << 16;
  916. if (raw_port_status & PORT_PEC)
  917. status |= USB_PORT_STAT_C_ENABLE << 16;
  918. if ((raw_port_status & PORT_OCC))
  919. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  920. if ((raw_port_status & PORT_RC))
  921. status |= USB_PORT_STAT_C_RESET << 16;
  922. /* common wPortStatus bits */
  923. if (raw_port_status & PORT_CONNECT) {
  924. status |= USB_PORT_STAT_CONNECTION;
  925. status |= xhci_port_speed(raw_port_status);
  926. }
  927. if (raw_port_status & PORT_PE)
  928. status |= USB_PORT_STAT_ENABLE;
  929. if (raw_port_status & PORT_OC)
  930. status |= USB_PORT_STAT_OVERCURRENT;
  931. if (raw_port_status & PORT_RESET)
  932. status |= USB_PORT_STAT_RESET;
  933. /* USB2 and USB3 specific bits, including Port Link State */
  934. if (hcd->speed >= HCD_USB3)
  935. xhci_get_usb3_port_status(port, &status, raw_port_status);
  936. else
  937. xhci_get_usb2_port_status(port, &status, raw_port_status,
  938. flags);
  939. /*
  940. * Clear stale usb2 resume signalling variables in case port changed
  941. * state during resume signalling. For example on error
  942. */
  943. if ((bus_state->resume_done[wIndex] ||
  944. test_bit(wIndex, &bus_state->resuming_ports)) &&
  945. (raw_port_status & PORT_PLS_MASK) != XDEV_U3 &&
  946. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME) {
  947. bus_state->resume_done[wIndex] = 0;
  948. clear_bit(wIndex, &bus_state->resuming_ports);
  949. usb_hcd_end_port_resume(&hcd->self, wIndex);
  950. }
  951. if (bus_state->port_c_suspend & (1 << wIndex))
  952. status |= USB_PORT_STAT_C_SUSPEND << 16;
  953. return status;
  954. }
  955. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  956. u16 wIndex, char *buf, u16 wLength)
  957. {
  958. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  959. int max_ports;
  960. unsigned long flags;
  961. u32 temp, status;
  962. int retval = 0;
  963. int slot_id;
  964. struct xhci_bus_state *bus_state;
  965. u16 link_state = 0;
  966. u16 wake_mask = 0;
  967. u16 timeout = 0;
  968. u16 test_mode = 0;
  969. struct xhci_hub *rhub;
  970. struct xhci_port **ports;
  971. rhub = xhci_get_rhub(hcd);
  972. ports = rhub->ports;
  973. max_ports = rhub->num_ports;
  974. bus_state = &rhub->bus_state;
  975. spin_lock_irqsave(&xhci->lock, flags);
  976. switch (typeReq) {
  977. case GetHubStatus:
  978. /* No power source, over-current reported per port */
  979. memset(buf, 0, 4);
  980. break;
  981. case GetHubDescriptor:
  982. /* Check to make sure userspace is asking for the USB 3.0 hub
  983. * descriptor for the USB 3.0 roothub. If not, we stall the
  984. * endpoint, like external hubs do.
  985. */
  986. if (hcd->speed >= HCD_USB3 &&
  987. (wLength < USB_DT_SS_HUB_SIZE ||
  988. wValue != (USB_DT_SS_HUB << 8))) {
  989. xhci_dbg(xhci, "Wrong hub descriptor type for "
  990. "USB 3.0 roothub.\n");
  991. goto error;
  992. }
  993. xhci_hub_descriptor(hcd, xhci,
  994. (struct usb_hub_descriptor *) buf);
  995. break;
  996. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  997. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  998. goto error;
  999. if (hcd->speed < HCD_USB3)
  1000. goto error;
  1001. retval = xhci_create_usb3_bos_desc(xhci, buf, wLength);
  1002. spin_unlock_irqrestore(&xhci->lock, flags);
  1003. return retval;
  1004. case GetPortStatus:
  1005. if (!wIndex || wIndex > max_ports)
  1006. goto error;
  1007. wIndex--;
  1008. temp = readl(ports[wIndex]->addr);
  1009. if (temp == ~(u32)0) {
  1010. xhci_hc_died(xhci);
  1011. retval = -ENODEV;
  1012. break;
  1013. }
  1014. trace_xhci_get_port_status(wIndex, temp);
  1015. status = xhci_get_port_status(hcd, bus_state, wIndex, temp,
  1016. &flags);
  1017. if (status == 0xffffffff)
  1018. goto error;
  1019. xhci_dbg(xhci, "Get port status %d-%d read: 0x%x, return 0x%x",
  1020. hcd->self.busnum, wIndex + 1, temp, status);
  1021. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  1022. /* if USB 3.1 extended port status return additional 4 bytes */
  1023. if (wValue == 0x02) {
  1024. u32 port_li;
  1025. if (hcd->speed < HCD_USB31 || wLength != 8) {
  1026. xhci_err(xhci, "get ext port status invalid parameter\n");
  1027. retval = -EINVAL;
  1028. break;
  1029. }
  1030. port_li = readl(ports[wIndex]->addr + PORTLI);
  1031. status = xhci_get_ext_port_status(temp, port_li);
  1032. put_unaligned_le32(status, &buf[4]);
  1033. }
  1034. break;
  1035. case SetPortFeature:
  1036. if (wValue == USB_PORT_FEAT_LINK_STATE)
  1037. link_state = (wIndex & 0xff00) >> 3;
  1038. if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
  1039. wake_mask = wIndex & 0xff00;
  1040. if (wValue == USB_PORT_FEAT_TEST)
  1041. test_mode = (wIndex & 0xff00) >> 8;
  1042. /* The MSB of wIndex is the U1/U2 timeout */
  1043. timeout = (wIndex & 0xff00) >> 8;
  1044. wIndex &= 0xff;
  1045. if (!wIndex || wIndex > max_ports)
  1046. goto error;
  1047. wIndex--;
  1048. temp = readl(ports[wIndex]->addr);
  1049. if (temp == ~(u32)0) {
  1050. xhci_hc_died(xhci);
  1051. retval = -ENODEV;
  1052. break;
  1053. }
  1054. temp = xhci_port_state_to_neutral(temp);
  1055. /* FIXME: What new port features do we need to support? */
  1056. switch (wValue) {
  1057. case USB_PORT_FEAT_SUSPEND:
  1058. temp = readl(ports[wIndex]->addr);
  1059. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  1060. /* Resume the port to U0 first */
  1061. xhci_set_link_state(xhci, ports[wIndex],
  1062. XDEV_U0);
  1063. spin_unlock_irqrestore(&xhci->lock, flags);
  1064. msleep(10);
  1065. spin_lock_irqsave(&xhci->lock, flags);
  1066. }
  1067. /* In spec software should not attempt to suspend
  1068. * a port unless the port reports that it is in the
  1069. * enabled (PED = ‘1’,PLS < ‘3’) state.
  1070. */
  1071. temp = readl(ports[wIndex]->addr);
  1072. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  1073. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  1074. xhci_warn(xhci, "USB core suspending port %d-%d not in U0/U1/U2\n",
  1075. hcd->self.busnum, wIndex + 1);
  1076. goto error;
  1077. }
  1078. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1079. wIndex + 1);
  1080. if (!slot_id) {
  1081. xhci_warn(xhci, "slot_id is zero\n");
  1082. goto error;
  1083. }
  1084. /* unlock to execute stop endpoint commands */
  1085. spin_unlock_irqrestore(&xhci->lock, flags);
  1086. xhci_stop_device(xhci, slot_id, 1);
  1087. spin_lock_irqsave(&xhci->lock, flags);
  1088. xhci_set_link_state(xhci, ports[wIndex], XDEV_U3);
  1089. spin_unlock_irqrestore(&xhci->lock, flags);
  1090. msleep(10); /* wait device to enter */
  1091. spin_lock_irqsave(&xhci->lock, flags);
  1092. temp = readl(ports[wIndex]->addr);
  1093. bus_state->suspended_ports |= 1 << wIndex;
  1094. break;
  1095. case USB_PORT_FEAT_LINK_STATE:
  1096. temp = readl(ports[wIndex]->addr);
  1097. /* Disable port */
  1098. if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
  1099. xhci_dbg(xhci, "Disable port %d\n", wIndex);
  1100. temp = xhci_port_state_to_neutral(temp);
  1101. /*
  1102. * Clear all change bits, so that we get a new
  1103. * connection event.
  1104. */
  1105. temp |= PORT_CSC | PORT_PEC | PORT_WRC |
  1106. PORT_OCC | PORT_RC | PORT_PLC |
  1107. PORT_CEC;
  1108. writel(temp | PORT_PE, ports[wIndex]->addr);
  1109. temp = readl(ports[wIndex]->addr);
  1110. break;
  1111. }
  1112. /* Put link in RxDetect (enable port) */
  1113. if (link_state == USB_SS_PORT_LS_RX_DETECT) {
  1114. xhci_dbg(xhci, "Enable port %d\n", wIndex);
  1115. xhci_set_link_state(xhci, ports[wIndex],
  1116. link_state);
  1117. temp = readl(ports[wIndex]->addr);
  1118. break;
  1119. }
  1120. /*
  1121. * For xHCI 1.1 according to section 4.19.1.2.4.1 a
  1122. * root hub port's transition to compliance mode upon
  1123. * detecting LFPS timeout may be controlled by an
  1124. * Compliance Transition Enabled (CTE) flag (not
  1125. * software visible). This flag is set by writing 0xA
  1126. * to PORTSC PLS field which will allow transition to
  1127. * compliance mode the next time LFPS timeout is
  1128. * encountered. A warm reset will clear it.
  1129. *
  1130. * The CTE flag is only supported if the HCCPARAMS2 CTC
  1131. * flag is set, otherwise, the compliance substate is
  1132. * automatically entered as on 1.0 and prior.
  1133. */
  1134. if (link_state == USB_SS_PORT_LS_COMP_MOD) {
  1135. if (!HCC2_CTC(xhci->hcc_params2)) {
  1136. xhci_dbg(xhci, "CTC flag is 0, port already supports entering compliance mode\n");
  1137. break;
  1138. }
  1139. if ((temp & PORT_CONNECT)) {
  1140. xhci_warn(xhci, "Can't set compliance mode when port is connected\n");
  1141. goto error;
  1142. }
  1143. xhci_dbg(xhci, "Enable compliance mode transition for port %d\n",
  1144. wIndex);
  1145. xhci_set_link_state(xhci, ports[wIndex],
  1146. link_state);
  1147. temp = readl(ports[wIndex]->addr);
  1148. break;
  1149. }
  1150. /* Port must be enabled */
  1151. if (!(temp & PORT_PE)) {
  1152. retval = -ENODEV;
  1153. break;
  1154. }
  1155. /* Can't set port link state above '3' (U3) */
  1156. if (link_state > USB_SS_PORT_LS_U3) {
  1157. xhci_warn(xhci, "Cannot set port %d link state %d\n",
  1158. wIndex, link_state);
  1159. goto error;
  1160. }
  1161. /*
  1162. * set link to U0, steps depend on current link state.
  1163. * U3: set link to U0 and wait for u3exit completion.
  1164. * U1/U2: no PLC complete event, only set link to U0.
  1165. * Resume/Recovery: device initiated U0, only wait for
  1166. * completion
  1167. */
  1168. if (link_state == USB_SS_PORT_LS_U0) {
  1169. u32 pls = temp & PORT_PLS_MASK;
  1170. bool wait_u0 = false;
  1171. /* already in U0 */
  1172. if (pls == XDEV_U0)
  1173. break;
  1174. if (pls == XDEV_U3 ||
  1175. pls == XDEV_RESUME ||
  1176. pls == XDEV_RECOVERY) {
  1177. wait_u0 = true;
  1178. reinit_completion(&bus_state->u3exit_done[wIndex]);
  1179. }
  1180. if (pls <= XDEV_U3) /* U1, U2, U3 */
  1181. xhci_set_link_state(xhci, ports[wIndex],
  1182. USB_SS_PORT_LS_U0);
  1183. if (!wait_u0) {
  1184. if (pls > XDEV_U3)
  1185. goto error;
  1186. break;
  1187. }
  1188. spin_unlock_irqrestore(&xhci->lock, flags);
  1189. if (!wait_for_completion_timeout(&bus_state->u3exit_done[wIndex],
  1190. msecs_to_jiffies(100)))
  1191. xhci_dbg(xhci, "missing U0 port change event for port %d\n",
  1192. wIndex);
  1193. spin_lock_irqsave(&xhci->lock, flags);
  1194. temp = readl(ports[wIndex]->addr);
  1195. break;
  1196. }
  1197. if (link_state == USB_SS_PORT_LS_U3) {
  1198. int retries = 16;
  1199. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1200. wIndex + 1);
  1201. if (slot_id) {
  1202. /* unlock to execute stop endpoint
  1203. * commands */
  1204. spin_unlock_irqrestore(&xhci->lock,
  1205. flags);
  1206. xhci_stop_device(xhci, slot_id, 1);
  1207. spin_lock_irqsave(&xhci->lock, flags);
  1208. }
  1209. xhci_set_link_state(xhci, ports[wIndex], USB_SS_PORT_LS_U3);
  1210. spin_unlock_irqrestore(&xhci->lock, flags);
  1211. while (retries--) {
  1212. usleep_range(4000, 8000);
  1213. temp = readl(ports[wIndex]->addr);
  1214. if ((temp & PORT_PLS_MASK) == XDEV_U3)
  1215. break;
  1216. }
  1217. spin_lock_irqsave(&xhci->lock, flags);
  1218. temp = readl(ports[wIndex]->addr);
  1219. bus_state->suspended_ports |= 1 << wIndex;
  1220. }
  1221. break;
  1222. case USB_PORT_FEAT_POWER:
  1223. /*
  1224. * Turn on ports, even if there isn't per-port switching.
  1225. * HC will report connect events even before this is set.
  1226. * However, hub_wq will ignore the roothub events until
  1227. * the roothub is registered.
  1228. */
  1229. xhci_set_port_power(xhci, hcd, wIndex, true, &flags);
  1230. break;
  1231. case USB_PORT_FEAT_RESET:
  1232. temp = (temp | PORT_RESET);
  1233. writel(temp, ports[wIndex]->addr);
  1234. temp = readl(ports[wIndex]->addr);
  1235. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  1236. break;
  1237. case USB_PORT_FEAT_REMOTE_WAKE_MASK:
  1238. xhci_set_remote_wake_mask(xhci, ports[wIndex],
  1239. wake_mask);
  1240. temp = readl(ports[wIndex]->addr);
  1241. xhci_dbg(xhci, "set port remote wake mask, "
  1242. "actual port %d status = 0x%x\n",
  1243. wIndex, temp);
  1244. break;
  1245. case USB_PORT_FEAT_BH_PORT_RESET:
  1246. temp |= PORT_WR;
  1247. writel(temp, ports[wIndex]->addr);
  1248. temp = readl(ports[wIndex]->addr);
  1249. break;
  1250. case USB_PORT_FEAT_U1_TIMEOUT:
  1251. if (hcd->speed < HCD_USB3)
  1252. goto error;
  1253. temp = readl(ports[wIndex]->addr + PORTPMSC);
  1254. temp &= ~PORT_U1_TIMEOUT_MASK;
  1255. temp |= PORT_U1_TIMEOUT(timeout);
  1256. writel(temp, ports[wIndex]->addr + PORTPMSC);
  1257. break;
  1258. case USB_PORT_FEAT_U2_TIMEOUT:
  1259. if (hcd->speed < HCD_USB3)
  1260. goto error;
  1261. temp = readl(ports[wIndex]->addr + PORTPMSC);
  1262. temp &= ~PORT_U2_TIMEOUT_MASK;
  1263. temp |= PORT_U2_TIMEOUT(timeout);
  1264. writel(temp, ports[wIndex]->addr + PORTPMSC);
  1265. break;
  1266. case USB_PORT_FEAT_TEST:
  1267. /* 4.19.6 Port Test Modes (USB2 Test Mode) */
  1268. if (hcd->speed != HCD_USB2)
  1269. goto error;
  1270. if (test_mode > TEST_FORCE_EN || test_mode < TEST_J)
  1271. goto error;
  1272. retval = xhci_enter_test_mode(xhci, test_mode, wIndex,
  1273. &flags);
  1274. break;
  1275. default:
  1276. goto error;
  1277. }
  1278. /* unblock any posted writes */
  1279. temp = readl(ports[wIndex]->addr);
  1280. break;
  1281. case ClearPortFeature:
  1282. if (!wIndex || wIndex > max_ports)
  1283. goto error;
  1284. wIndex--;
  1285. temp = readl(ports[wIndex]->addr);
  1286. if (temp == ~(u32)0) {
  1287. xhci_hc_died(xhci);
  1288. retval = -ENODEV;
  1289. break;
  1290. }
  1291. /* FIXME: What new port features do we need to support? */
  1292. temp = xhci_port_state_to_neutral(temp);
  1293. switch (wValue) {
  1294. case USB_PORT_FEAT_SUSPEND:
  1295. temp = readl(ports[wIndex]->addr);
  1296. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  1297. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  1298. if (temp & PORT_RESET)
  1299. goto error;
  1300. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  1301. if ((temp & PORT_PE) == 0)
  1302. goto error;
  1303. set_bit(wIndex, &bus_state->resuming_ports);
  1304. usb_hcd_start_port_resume(&hcd->self, wIndex);
  1305. xhci_set_link_state(xhci, ports[wIndex],
  1306. XDEV_RESUME);
  1307. spin_unlock_irqrestore(&xhci->lock, flags);
  1308. msleep(USB_RESUME_TIMEOUT);
  1309. spin_lock_irqsave(&xhci->lock, flags);
  1310. xhci_set_link_state(xhci, ports[wIndex],
  1311. XDEV_U0);
  1312. clear_bit(wIndex, &bus_state->resuming_ports);
  1313. usb_hcd_end_port_resume(&hcd->self, wIndex);
  1314. }
  1315. bus_state->port_c_suspend |= 1 << wIndex;
  1316. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1317. wIndex + 1);
  1318. if (!slot_id) {
  1319. xhci_dbg(xhci, "slot_id is zero\n");
  1320. goto error;
  1321. }
  1322. xhci_ring_device(xhci, slot_id);
  1323. break;
  1324. case USB_PORT_FEAT_C_SUSPEND:
  1325. bus_state->port_c_suspend &= ~(1 << wIndex);
  1326. /* fall through */
  1327. case USB_PORT_FEAT_C_RESET:
  1328. case USB_PORT_FEAT_C_BH_PORT_RESET:
  1329. case USB_PORT_FEAT_C_CONNECTION:
  1330. case USB_PORT_FEAT_C_OVER_CURRENT:
  1331. case USB_PORT_FEAT_C_ENABLE:
  1332. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  1333. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  1334. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  1335. ports[wIndex]->addr, temp);
  1336. break;
  1337. case USB_PORT_FEAT_ENABLE:
  1338. xhci_disable_port(hcd, xhci, wIndex,
  1339. ports[wIndex]->addr, temp);
  1340. break;
  1341. case USB_PORT_FEAT_POWER:
  1342. xhci_set_port_power(xhci, hcd, wIndex, false, &flags);
  1343. break;
  1344. case USB_PORT_FEAT_TEST:
  1345. retval = xhci_exit_test_mode(xhci);
  1346. break;
  1347. default:
  1348. goto error;
  1349. }
  1350. break;
  1351. default:
  1352. error:
  1353. /* "stall" on error */
  1354. retval = -EPIPE;
  1355. }
  1356. spin_unlock_irqrestore(&xhci->lock, flags);
  1357. return retval;
  1358. }
  1359. /*
  1360. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  1361. * Ports are 0-indexed from the HCD point of view,
  1362. * and 1-indexed from the USB core pointer of view.
  1363. *
  1364. * Note that the status change bits will be cleared as soon as a port status
  1365. * change event is generated, so we use the saved status from that event.
  1366. */
  1367. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  1368. {
  1369. unsigned long flags;
  1370. u32 temp, status;
  1371. u32 mask;
  1372. int i, retval;
  1373. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1374. int max_ports;
  1375. struct xhci_bus_state *bus_state;
  1376. bool reset_change = false;
  1377. struct xhci_hub *rhub;
  1378. struct xhci_port **ports;
  1379. rhub = xhci_get_rhub(hcd);
  1380. ports = rhub->ports;
  1381. max_ports = rhub->num_ports;
  1382. bus_state = &rhub->bus_state;
  1383. /* Initial status is no changes */
  1384. retval = (max_ports + 8) / 8;
  1385. memset(buf, 0, retval);
  1386. /*
  1387. * Inform the usbcore about resume-in-progress by returning
  1388. * a non-zero value even if there are no status changes.
  1389. */
  1390. spin_lock_irqsave(&xhci->lock, flags);
  1391. status = bus_state->resuming_ports;
  1392. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
  1393. /* For each port, did anything change? If so, set that bit in buf. */
  1394. for (i = 0; i < max_ports; i++) {
  1395. temp = readl(ports[i]->addr);
  1396. if (temp == ~(u32)0) {
  1397. xhci_hc_died(xhci);
  1398. retval = -ENODEV;
  1399. break;
  1400. }
  1401. trace_xhci_hub_status_data(i, temp);
  1402. if ((temp & mask) != 0 ||
  1403. (bus_state->port_c_suspend & 1 << i) ||
  1404. (bus_state->resume_done[i] && time_after_eq(
  1405. jiffies, bus_state->resume_done[i]))) {
  1406. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  1407. status = 1;
  1408. }
  1409. if ((temp & PORT_RC))
  1410. reset_change = true;
  1411. if (temp & PORT_OC)
  1412. status = 1;
  1413. }
  1414. if (!status && !reset_change) {
  1415. xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
  1416. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  1417. }
  1418. spin_unlock_irqrestore(&xhci->lock, flags);
  1419. return status ? retval : 0;
  1420. }
  1421. #ifdef CONFIG_PM
  1422. int xhci_bus_suspend(struct usb_hcd *hcd)
  1423. {
  1424. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1425. int max_ports, port_index;
  1426. struct xhci_bus_state *bus_state;
  1427. unsigned long flags;
  1428. struct xhci_hub *rhub;
  1429. struct xhci_port **ports;
  1430. u32 portsc_buf[USB_MAXCHILDREN];
  1431. bool wake_enabled;
  1432. rhub = xhci_get_rhub(hcd);
  1433. ports = rhub->ports;
  1434. max_ports = rhub->num_ports;
  1435. bus_state = &rhub->bus_state;
  1436. wake_enabled = hcd->self.root_hub->do_remote_wakeup;
  1437. spin_lock_irqsave(&xhci->lock, flags);
  1438. if (wake_enabled) {
  1439. if (bus_state->resuming_ports || /* USB2 */
  1440. bus_state->port_remote_wakeup) { /* USB3 */
  1441. spin_unlock_irqrestore(&xhci->lock, flags);
  1442. xhci_dbg(xhci, "suspend failed because a port is resuming\n");
  1443. return -EBUSY;
  1444. }
  1445. }
  1446. /*
  1447. * Prepare ports for suspend, but don't write anything before all ports
  1448. * are checked and we know bus suspend can proceed
  1449. */
  1450. bus_state->bus_suspended = 0;
  1451. port_index = max_ports;
  1452. while (port_index--) {
  1453. u32 t1, t2;
  1454. int retries = 10;
  1455. retry:
  1456. t1 = readl(ports[port_index]->addr);
  1457. t2 = xhci_port_state_to_neutral(t1);
  1458. portsc_buf[port_index] = 0;
  1459. /*
  1460. * Give a USB3 port in link training time to finish, but don't
  1461. * prevent suspend as port might be stuck
  1462. */
  1463. if ((hcd->speed >= HCD_USB3) && retries-- &&
  1464. (t1 & PORT_PLS_MASK) == XDEV_POLLING) {
  1465. spin_unlock_irqrestore(&xhci->lock, flags);
  1466. msleep(XHCI_PORT_POLLING_LFPS_TIME);
  1467. spin_lock_irqsave(&xhci->lock, flags);
  1468. xhci_dbg(xhci, "port %d polling in bus suspend, waiting\n",
  1469. port_index);
  1470. goto retry;
  1471. }
  1472. /* bail out if port detected a over-current condition */
  1473. if (t1 & PORT_OC) {
  1474. bus_state->bus_suspended = 0;
  1475. spin_unlock_irqrestore(&xhci->lock, flags);
  1476. xhci_dbg(xhci, "Bus suspend bailout, port over-current detected\n");
  1477. return -EBUSY;
  1478. }
  1479. /* suspend ports in U0, or bail out for new connect changes */
  1480. if ((t1 & PORT_PE) && (t1 & PORT_PLS_MASK) == XDEV_U0) {
  1481. if ((t1 & PORT_CSC) && wake_enabled) {
  1482. bus_state->bus_suspended = 0;
  1483. spin_unlock_irqrestore(&xhci->lock, flags);
  1484. xhci_dbg(xhci, "Bus suspend bailout, port connect change\n");
  1485. return -EBUSY;
  1486. }
  1487. xhci_dbg(xhci, "port %d not suspended\n", port_index);
  1488. t2 &= ~PORT_PLS_MASK;
  1489. t2 |= PORT_LINK_STROBE | XDEV_U3;
  1490. set_bit(port_index, &bus_state->bus_suspended);
  1491. }
  1492. /* USB core sets remote wake mask for USB 3.0 hubs,
  1493. * including the USB 3.0 roothub, but only if CONFIG_PM
  1494. * is enabled, so also enable remote wake here.
  1495. */
  1496. if (wake_enabled) {
  1497. if (t1 & PORT_CONNECT) {
  1498. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  1499. t2 &= ~PORT_WKCONN_E;
  1500. } else {
  1501. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  1502. t2 &= ~PORT_WKDISC_E;
  1503. }
  1504. if ((xhci->quirks & XHCI_U2_DISABLE_WAKE) &&
  1505. (hcd->speed < HCD_USB3)) {
  1506. if (usb_amd_pt_check_port(hcd->self.controller,
  1507. port_index))
  1508. t2 &= ~PORT_WAKE_BITS;
  1509. }
  1510. } else
  1511. t2 &= ~PORT_WAKE_BITS;
  1512. t1 = xhci_port_state_to_neutral(t1);
  1513. if (t1 != t2)
  1514. portsc_buf[port_index] = t2;
  1515. }
  1516. /* write port settings, stopping and suspending ports if needed */
  1517. port_index = max_ports;
  1518. while (port_index--) {
  1519. if (!portsc_buf[port_index])
  1520. continue;
  1521. if (test_bit(port_index, &bus_state->bus_suspended)) {
  1522. int slot_id;
  1523. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1524. port_index + 1);
  1525. if (slot_id) {
  1526. spin_unlock_irqrestore(&xhci->lock, flags);
  1527. xhci_stop_device(xhci, slot_id, 1);
  1528. spin_lock_irqsave(&xhci->lock, flags);
  1529. }
  1530. }
  1531. writel(portsc_buf[port_index], ports[port_index]->addr);
  1532. }
  1533. hcd->state = HC_STATE_SUSPENDED;
  1534. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  1535. spin_unlock_irqrestore(&xhci->lock, flags);
  1536. if (bus_state->bus_suspended)
  1537. usleep_range(5000, 10000);
  1538. return 0;
  1539. }
  1540. /*
  1541. * Workaround for missing Cold Attach Status (CAS) if device re-plugged in S3.
  1542. * warm reset a USB3 device stuck in polling or compliance mode after resume.
  1543. * See Intel 100/c230 series PCH specification update Doc #332692-006 Errata #8
  1544. */
  1545. static bool xhci_port_missing_cas_quirk(struct xhci_port *port)
  1546. {
  1547. u32 portsc;
  1548. portsc = readl(port->addr);
  1549. /* if any of these are set we are not stuck */
  1550. if (portsc & (PORT_CONNECT | PORT_CAS))
  1551. return false;
  1552. if (((portsc & PORT_PLS_MASK) != XDEV_POLLING) &&
  1553. ((portsc & PORT_PLS_MASK) != XDEV_COMP_MODE))
  1554. return false;
  1555. /* clear wakeup/change bits, and do a warm port reset */
  1556. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1557. portsc |= PORT_WR;
  1558. writel(portsc, port->addr);
  1559. /* flush write */
  1560. readl(port->addr);
  1561. return true;
  1562. }
  1563. int xhci_bus_resume(struct usb_hcd *hcd)
  1564. {
  1565. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1566. struct xhci_bus_state *bus_state;
  1567. unsigned long flags;
  1568. int max_ports, port_index;
  1569. int slot_id;
  1570. int sret;
  1571. u32 next_state;
  1572. u32 temp, portsc;
  1573. struct xhci_hub *rhub;
  1574. struct xhci_port **ports;
  1575. rhub = xhci_get_rhub(hcd);
  1576. ports = rhub->ports;
  1577. max_ports = rhub->num_ports;
  1578. bus_state = &rhub->bus_state;
  1579. if (time_before(jiffies, bus_state->next_statechange))
  1580. msleep(5);
  1581. spin_lock_irqsave(&xhci->lock, flags);
  1582. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1583. spin_unlock_irqrestore(&xhci->lock, flags);
  1584. return -ESHUTDOWN;
  1585. }
  1586. /* delay the irqs */
  1587. temp = readl(&xhci->op_regs->command);
  1588. temp &= ~CMD_EIE;
  1589. writel(temp, &xhci->op_regs->command);
  1590. /* bus specific resume for ports we suspended at bus_suspend */
  1591. if (hcd->speed >= HCD_USB3)
  1592. next_state = XDEV_U0;
  1593. else
  1594. next_state = XDEV_RESUME;
  1595. port_index = max_ports;
  1596. while (port_index--) {
  1597. portsc = readl(ports[port_index]->addr);
  1598. /* warm reset CAS limited ports stuck in polling/compliance */
  1599. if ((xhci->quirks & XHCI_MISSING_CAS) &&
  1600. (hcd->speed >= HCD_USB3) &&
  1601. xhci_port_missing_cas_quirk(ports[port_index])) {
  1602. xhci_dbg(xhci, "reset stuck port %d\n", port_index);
  1603. clear_bit(port_index, &bus_state->bus_suspended);
  1604. continue;
  1605. }
  1606. /* resume if we suspended the link, and it is still suspended */
  1607. if (test_bit(port_index, &bus_state->bus_suspended))
  1608. switch (portsc & PORT_PLS_MASK) {
  1609. case XDEV_U3:
  1610. portsc = xhci_port_state_to_neutral(portsc);
  1611. portsc &= ~PORT_PLS_MASK;
  1612. portsc |= PORT_LINK_STROBE | next_state;
  1613. break;
  1614. case XDEV_RESUME:
  1615. /* resume already initiated */
  1616. break;
  1617. default:
  1618. /* not in a resumeable state, ignore it */
  1619. clear_bit(port_index,
  1620. &bus_state->bus_suspended);
  1621. break;
  1622. }
  1623. /* disable wake for all ports, write new link state if needed */
  1624. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1625. writel(portsc, ports[port_index]->addr);
  1626. }
  1627. /* USB2 specific resume signaling delay and U0 link state transition */
  1628. if (hcd->speed < HCD_USB3) {
  1629. if (bus_state->bus_suspended) {
  1630. spin_unlock_irqrestore(&xhci->lock, flags);
  1631. msleep(USB_RESUME_TIMEOUT);
  1632. spin_lock_irqsave(&xhci->lock, flags);
  1633. }
  1634. for_each_set_bit(port_index, &bus_state->bus_suspended,
  1635. BITS_PER_LONG) {
  1636. /* Clear PLC to poll it later for U0 transition */
  1637. xhci_test_and_clear_bit(xhci, ports[port_index],
  1638. PORT_PLC);
  1639. xhci_set_link_state(xhci, ports[port_index], XDEV_U0);
  1640. }
  1641. }
  1642. /* poll for U0 link state complete, both USB2 and USB3 */
  1643. for_each_set_bit(port_index, &bus_state->bus_suspended, BITS_PER_LONG) {
  1644. sret = xhci_handshake(ports[port_index]->addr, PORT_PLC,
  1645. PORT_PLC, 10 * 1000);
  1646. if (sret) {
  1647. xhci_warn(xhci, "port %d resume PLC timeout\n",
  1648. port_index);
  1649. continue;
  1650. }
  1651. xhci_test_and_clear_bit(xhci, ports[port_index], PORT_PLC);
  1652. slot_id = xhci_find_slot_id_by_port(hcd, xhci, port_index + 1);
  1653. if (slot_id)
  1654. xhci_ring_device(xhci, slot_id);
  1655. }
  1656. (void) readl(&xhci->op_regs->command);
  1657. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  1658. /* re-enable irqs */
  1659. temp = readl(&xhci->op_regs->command);
  1660. temp |= CMD_EIE;
  1661. writel(temp, &xhci->op_regs->command);
  1662. temp = readl(&xhci->op_regs->command);
  1663. spin_unlock_irqrestore(&xhci->lock, flags);
  1664. return 0;
  1665. }
  1666. unsigned long xhci_get_resuming_ports(struct usb_hcd *hcd)
  1667. {
  1668. struct xhci_hub *rhub = xhci_get_rhub(hcd);
  1669. /* USB3 port wakeups are reported via usb_wakeup_notification() */
  1670. return rhub->bus_state.resuming_ports; /* USB2 ports only */
  1671. }
  1672. #endif /* CONFIG_PM */