xhci-hub.c 49 KB

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