xhci-hub.c 51 KB

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