xhci-hub.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * xHCI host controller driver
  4. *
  5. * Copyright (C) 2008 Intel Corp.
  6. *
  7. * Author: Sarah Sharp
  8. * Some code borrowed from the Linux EHCI driver.
  9. */
  10. #include <linux/slab.h>
  11. #include <asm/unaligned.h>
  12. #include "xhci.h"
  13. #include "xhci-trace.h"
  14. #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
  15. #define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \
  16. PORT_RC | PORT_PLC | PORT_PE)
  17. /* USB 3 BOS descriptor and a capability descriptors, combined.
  18. * Fields will be adjusted and added later in xhci_create_usb3_bos_desc()
  19. */
  20. static u8 usb_bos_descriptor [] = {
  21. USB_DT_BOS_SIZE, /* __u8 bLength, 5 bytes */
  22. USB_DT_BOS, /* __u8 bDescriptorType */
  23. 0x0F, 0x00, /* __le16 wTotalLength, 15 bytes */
  24. 0x1, /* __u8 bNumDeviceCaps */
  25. /* First device capability, SuperSpeed */
  26. USB_DT_USB_SS_CAP_SIZE, /* __u8 bLength, 10 bytes */
  27. USB_DT_DEVICE_CAPABILITY, /* Device Capability */
  28. USB_SS_CAP_TYPE, /* bDevCapabilityType, SUPERSPEED_USB */
  29. 0x00, /* bmAttributes, LTM off by default */
  30. USB_5GBPS_OPERATION, 0x00, /* wSpeedsSupported, 5Gbps only */
  31. 0x03, /* bFunctionalitySupport,
  32. USB 3.0 speed only */
  33. 0x00, /* bU1DevExitLat, set later. */
  34. 0x00, 0x00, /* __le16 bU2DevExitLat, set later. */
  35. /* Second device capability, SuperSpeedPlus */
  36. 0x1c, /* bLength 28, will be adjusted later */
  37. USB_DT_DEVICE_CAPABILITY, /* Device Capability */
  38. USB_SSP_CAP_TYPE, /* bDevCapabilityType SUPERSPEED_PLUS */
  39. 0x00, /* bReserved 0 */
  40. 0x23, 0x00, 0x00, 0x00, /* bmAttributes, SSAC=3 SSIC=1 */
  41. 0x01, 0x00, /* wFunctionalitySupport */
  42. 0x00, 0x00, /* wReserved 0 */
  43. /* Default Sublink Speed Attributes, overwrite if custom PSI exists */
  44. 0x34, 0x00, 0x05, 0x00, /* 5Gbps, symmetric, rx, ID = 4 */
  45. 0xb4, 0x00, 0x05, 0x00, /* 5Gbps, symmetric, tx, ID = 4 */
  46. 0x35, 0x40, 0x0a, 0x00, /* 10Gbps, SSP, symmetric, rx, ID = 5 */
  47. 0xb5, 0x40, 0x0a, 0x00, /* 10Gbps, SSP, symmetric, tx, ID = 5 */
  48. };
  49. static int xhci_create_usb3_bos_desc(struct xhci_hcd *xhci, char *buf,
  50. u16 wLength)
  51. {
  52. int i, ssa_count;
  53. u32 temp;
  54. u16 desc_size, ssp_cap_size, ssa_size = 0;
  55. bool usb3_1 = false;
  56. desc_size = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  57. ssp_cap_size = sizeof(usb_bos_descriptor) - desc_size;
  58. /* does xhci support USB 3.1 Enhanced SuperSpeed */
  59. if (xhci->usb3_rhub.min_rev >= 0x01) {
  60. /* does xhci provide a PSI table for SSA speed attributes? */
  61. if (xhci->usb3_rhub.psi_count) {
  62. /* two SSA entries for each unique PSI ID, RX and TX */
  63. ssa_count = xhci->usb3_rhub.psi_uid_count * 2;
  64. ssa_size = ssa_count * sizeof(u32);
  65. ssp_cap_size -= 16; /* skip copying the default SSA */
  66. }
  67. desc_size += ssp_cap_size;
  68. usb3_1 = true;
  69. }
  70. memcpy(buf, &usb_bos_descriptor, min(desc_size, wLength));
  71. if (usb3_1) {
  72. /* modify bos descriptor bNumDeviceCaps and wTotalLength */
  73. buf[4] += 1;
  74. put_unaligned_le16(desc_size + ssa_size, &buf[2]);
  75. }
  76. if (wLength < USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE)
  77. return wLength;
  78. /* Indicate whether the host has LTM support. */
  79. temp = readl(&xhci->cap_regs->hcc_params);
  80. if (HCC_LTC(temp))
  81. buf[8] |= USB_LTM_SUPPORT;
  82. /* Set the U1 and U2 exit latencies. */
  83. if ((xhci->quirks & XHCI_LPM_SUPPORT)) {
  84. temp = readl(&xhci->cap_regs->hcs_params3);
  85. buf[12] = HCS_U1_LATENCY(temp);
  86. put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
  87. }
  88. /* If PSI table exists, add the custom speed attributes from it */
  89. if (usb3_1 && xhci->usb3_rhub.psi_count) {
  90. u32 ssp_cap_base, bm_attrib, psi, psi_mant, psi_exp;
  91. int offset;
  92. ssp_cap_base = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  93. if (wLength < desc_size)
  94. return wLength;
  95. buf[ssp_cap_base] = ssp_cap_size + ssa_size;
  96. /* attribute count SSAC bits 4:0 and ID count SSIC bits 8:5 */
  97. bm_attrib = (ssa_count - 1) & 0x1f;
  98. bm_attrib |= (xhci->usb3_rhub.psi_uid_count - 1) << 5;
  99. put_unaligned_le32(bm_attrib, &buf[ssp_cap_base + 4]);
  100. if (wLength < desc_size + ssa_size)
  101. return wLength;
  102. /*
  103. * Create the Sublink Speed Attributes (SSA) array.
  104. * The xhci PSI field and USB 3.1 SSA fields are very similar,
  105. * but link type bits 7:6 differ for values 01b and 10b.
  106. * xhci has also only one PSI entry for a symmetric link when
  107. * USB 3.1 requires two SSA entries (RX and TX) for every link
  108. */
  109. offset = desc_size;
  110. for (i = 0; i < xhci->usb3_rhub.psi_count; i++) {
  111. psi = xhci->usb3_rhub.psi[i];
  112. psi &= ~USB_SSP_SUBLINK_SPEED_RSVD;
  113. psi_exp = XHCI_EXT_PORT_PSIE(psi);
  114. psi_mant = XHCI_EXT_PORT_PSIM(psi);
  115. /* Shift to Gbps and set SSP Link BIT(14) if 10Gpbs */
  116. for (; psi_exp < 3; psi_exp++)
  117. psi_mant /= 1000;
  118. if (psi_mant >= 10)
  119. psi |= BIT(14);
  120. if ((psi & PLT_MASK) == PLT_SYM) {
  121. /* Symmetric, create SSA RX and TX from one PSI entry */
  122. put_unaligned_le32(psi, &buf[offset]);
  123. psi |= 1 << 7; /* turn entry to TX */
  124. offset += 4;
  125. if (offset >= desc_size + ssa_size)
  126. return desc_size + ssa_size;
  127. } else if ((psi & PLT_MASK) == PLT_ASYM_RX) {
  128. /* Asymetric RX, flip bits 7:6 for SSA */
  129. psi ^= PLT_MASK;
  130. }
  131. put_unaligned_le32(psi, &buf[offset]);
  132. offset += 4;
  133. if (offset >= desc_size + ssa_size)
  134. return desc_size + ssa_size;
  135. }
  136. }
  137. /* ssa_size is 0 for other than usb 3.1 hosts */
  138. return desc_size + ssa_size;
  139. }
  140. static void xhci_common_hub_descriptor(struct xhci_hcd *xhci,
  141. struct usb_hub_descriptor *desc, int ports)
  142. {
  143. u16 temp;
  144. desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */
  145. desc->bHubContrCurrent = 0;
  146. desc->bNbrPorts = ports;
  147. temp = 0;
  148. /* Bits 1:0 - support per-port power switching, or power always on */
  149. if (HCC_PPC(xhci->hcc_params))
  150. temp |= HUB_CHAR_INDV_PORT_LPSM;
  151. else
  152. temp |= HUB_CHAR_NO_LPSM;
  153. /* Bit 2 - root hubs are not part of a compound device */
  154. /* Bits 4:3 - individual port over current protection */
  155. temp |= HUB_CHAR_INDV_PORT_OCPM;
  156. /* Bits 6:5 - no TTs in root ports */
  157. /* Bit 7 - no port indicators */
  158. desc->wHubCharacteristics = cpu_to_le16(temp);
  159. }
  160. /* Fill in the USB 2.0 roothub descriptor */
  161. static void xhci_usb2_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  162. struct usb_hub_descriptor *desc)
  163. {
  164. int ports;
  165. u16 temp;
  166. __u8 port_removable[(USB_MAXCHILDREN + 1 + 7) / 8];
  167. u32 portsc;
  168. unsigned int i;
  169. ports = xhci->num_usb2_ports;
  170. xhci_common_hub_descriptor(xhci, desc, ports);
  171. desc->bDescriptorType = USB_DT_HUB;
  172. temp = 1 + (ports / 8);
  173. desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp;
  174. /* The Device Removable bits are reported on a byte granularity.
  175. * If the port doesn't exist within that byte, the bit is set to 0.
  176. */
  177. memset(port_removable, 0, sizeof(port_removable));
  178. for (i = 0; i < ports; i++) {
  179. portsc = readl(xhci->usb2_ports[i]);
  180. /* If a device is removable, PORTSC reports a 0, same as in the
  181. * hub descriptor DeviceRemovable bits.
  182. */
  183. if (portsc & PORT_DEV_REMOVE)
  184. /* This math is hairy because bit 0 of DeviceRemovable
  185. * is reserved, and bit 1 is for port 1, etc.
  186. */
  187. port_removable[(i + 1) / 8] |= 1 << ((i + 1) % 8);
  188. }
  189. /* ch11.h defines a hub descriptor that has room for USB_MAXCHILDREN
  190. * ports on it. The USB 2.0 specification says that there are two
  191. * variable length fields at the end of the hub descriptor:
  192. * DeviceRemovable and PortPwrCtrlMask. But since we can have less than
  193. * USB_MAXCHILDREN ports, we may need to use the DeviceRemovable array
  194. * to set PortPwrCtrlMask bits. PortPwrCtrlMask must always be set to
  195. * 0xFF, so we initialize the both arrays (DeviceRemovable and
  196. * PortPwrCtrlMask) to 0xFF. Then we set the DeviceRemovable for each
  197. * set of ports that actually exist.
  198. */
  199. memset(desc->u.hs.DeviceRemovable, 0xff,
  200. sizeof(desc->u.hs.DeviceRemovable));
  201. memset(desc->u.hs.PortPwrCtrlMask, 0xff,
  202. sizeof(desc->u.hs.PortPwrCtrlMask));
  203. for (i = 0; i < (ports + 1 + 7) / 8; i++)
  204. memset(&desc->u.hs.DeviceRemovable[i], port_removable[i],
  205. sizeof(__u8));
  206. }
  207. /* Fill in the USB 3.0 roothub descriptor */
  208. static void xhci_usb3_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  209. struct usb_hub_descriptor *desc)
  210. {
  211. int ports;
  212. u16 port_removable;
  213. u32 portsc;
  214. unsigned int i;
  215. ports = xhci->num_usb3_ports;
  216. xhci_common_hub_descriptor(xhci, desc, ports);
  217. desc->bDescriptorType = USB_DT_SS_HUB;
  218. desc->bDescLength = USB_DT_SS_HUB_SIZE;
  219. /* header decode latency should be zero for roothubs,
  220. * see section 4.23.5.2.
  221. */
  222. desc->u.ss.bHubHdrDecLat = 0;
  223. desc->u.ss.wHubDelay = 0;
  224. port_removable = 0;
  225. /* bit 0 is reserved, bit 1 is for port 1, etc. */
  226. for (i = 0; i < ports; i++) {
  227. portsc = readl(xhci->usb3_ports[i]);
  228. if (portsc & PORT_DEV_REMOVE)
  229. port_removable |= 1 << (i + 1);
  230. }
  231. desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
  232. }
  233. static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  234. struct usb_hub_descriptor *desc)
  235. {
  236. if (hcd->speed >= HCD_USB3)
  237. xhci_usb3_hub_descriptor(hcd, xhci, desc);
  238. else
  239. xhci_usb2_hub_descriptor(hcd, xhci, desc);
  240. }
  241. static unsigned int xhci_port_speed(unsigned int port_status)
  242. {
  243. if (DEV_LOWSPEED(port_status))
  244. return USB_PORT_STAT_LOW_SPEED;
  245. if (DEV_HIGHSPEED(port_status))
  246. return USB_PORT_STAT_HIGH_SPEED;
  247. /*
  248. * FIXME: Yes, we should check for full speed, but the core uses that as
  249. * a default in portspeed() in usb/core/hub.c (which is the only place
  250. * USB_PORT_STAT_*_SPEED is used).
  251. */
  252. return 0;
  253. }
  254. /*
  255. * These bits are Read Only (RO) and should be saved and written to the
  256. * registers: 0, 3, 10:13, 30
  257. * connect status, over-current status, port speed, and device removable.
  258. * connect status and port speed are also sticky - meaning they're in
  259. * the AUX well and they aren't changed by a hot, warm, or cold reset.
  260. */
  261. #define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
  262. /*
  263. * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
  264. * bits 5:8, 9, 14:15, 25:27
  265. * link state, port power, port indicator state, "wake on" enable state
  266. */
  267. #define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
  268. /*
  269. * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
  270. * bit 4 (port reset)
  271. */
  272. #define XHCI_PORT_RW1S ((1<<4))
  273. /*
  274. * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
  275. * bits 1, 17, 18, 19, 20, 21, 22, 23
  276. * port enable/disable, and
  277. * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
  278. * over-current, reset, link state, and L1 change
  279. */
  280. #define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
  281. /*
  282. * Bit 16 is RW, and writing a '1' to it causes the link state control to be
  283. * latched in
  284. */
  285. #define XHCI_PORT_RW ((1<<16))
  286. /*
  287. * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
  288. * bits 2, 24, 28:31
  289. */
  290. #define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
  291. /*
  292. * Given a port state, this function returns a value that would result in the
  293. * port being in the same state, if the value was written to the port status
  294. * control register.
  295. * Save Read Only (RO) bits and save read/write bits where
  296. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  297. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  298. */
  299. u32 xhci_port_state_to_neutral(u32 state)
  300. {
  301. /* Save read-only status and port state */
  302. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  303. }
  304. /*
  305. * find slot id based on port number.
  306. * @port: The one-based port number from one of the two split roothubs.
  307. */
  308. int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  309. u16 port)
  310. {
  311. int slot_id;
  312. int i;
  313. enum usb_device_speed speed;
  314. slot_id = 0;
  315. for (i = 0; i < MAX_HC_SLOTS; i++) {
  316. if (!xhci->devs[i])
  317. continue;
  318. speed = xhci->devs[i]->udev->speed;
  319. if (((speed >= USB_SPEED_SUPER) == (hcd->speed >= HCD_USB3))
  320. && xhci->devs[i]->fake_port == port) {
  321. slot_id = i;
  322. break;
  323. }
  324. }
  325. return slot_id;
  326. }
  327. /*
  328. * Stop device
  329. * It issues stop endpoint command for EP 0 to 30. And wait the last command
  330. * to complete.
  331. * suspend will set to 1, if suspend bit need to set in command.
  332. */
  333. static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
  334. {
  335. struct xhci_virt_device *virt_dev;
  336. struct xhci_command *cmd;
  337. unsigned long flags;
  338. int ret;
  339. int i;
  340. ret = 0;
  341. virt_dev = xhci->devs[slot_id];
  342. if (!virt_dev)
  343. return -ENODEV;
  344. trace_xhci_stop_device(virt_dev);
  345. cmd = xhci_alloc_command(xhci, true, GFP_NOIO);
  346. if (!cmd)
  347. return -ENOMEM;
  348. spin_lock_irqsave(&xhci->lock, flags);
  349. for (i = LAST_EP_INDEX; i > 0; i--) {
  350. if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) {
  351. struct xhci_ep_ctx *ep_ctx;
  352. struct xhci_command *command;
  353. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->out_ctx, i);
  354. /* Check ep is running, required by AMD SNPS 3.1 xHC */
  355. if (GET_EP_CTX_STATE(ep_ctx) != EP_STATE_RUNNING)
  356. continue;
  357. command = xhci_alloc_command(xhci, false, GFP_NOWAIT);
  358. if (!command) {
  359. spin_unlock_irqrestore(&xhci->lock, flags);
  360. ret = -ENOMEM;
  361. goto cmd_cleanup;
  362. }
  363. ret = xhci_queue_stop_endpoint(xhci, command, slot_id,
  364. i, suspend);
  365. if (ret) {
  366. spin_unlock_irqrestore(&xhci->lock, flags);
  367. xhci_free_command(xhci, command);
  368. goto cmd_cleanup;
  369. }
  370. }
  371. }
  372. ret = xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
  373. if (ret) {
  374. spin_unlock_irqrestore(&xhci->lock, flags);
  375. goto cmd_cleanup;
  376. }
  377. xhci_ring_cmd_db(xhci);
  378. spin_unlock_irqrestore(&xhci->lock, flags);
  379. /* Wait for last stop endpoint command to finish */
  380. wait_for_completion(cmd->completion);
  381. if (cmd->status == COMP_COMMAND_ABORTED ||
  382. cmd->status == COMP_COMMAND_RING_STOPPED) {
  383. xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
  384. ret = -ETIME;
  385. }
  386. cmd_cleanup:
  387. xhci_free_command(xhci, cmd);
  388. return ret;
  389. }
  390. /*
  391. * Ring device, it rings the all doorbells unconditionally.
  392. */
  393. void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
  394. {
  395. int i, s;
  396. struct xhci_virt_ep *ep;
  397. for (i = 0; i < LAST_EP_INDEX + 1; i++) {
  398. ep = &xhci->devs[slot_id]->eps[i];
  399. if (ep->ep_state & EP_HAS_STREAMS) {
  400. for (s = 1; s < ep->stream_info->num_streams; s++)
  401. xhci_ring_ep_doorbell(xhci, slot_id, i, s);
  402. } else if (ep->ring && ep->ring->dequeue) {
  403. xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
  404. }
  405. }
  406. return;
  407. }
  408. static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  409. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  410. {
  411. /* Don't allow the USB core to disable SuperSpeed ports. */
  412. if (hcd->speed >= HCD_USB3) {
  413. xhci_dbg(xhci, "Ignoring request to disable "
  414. "SuperSpeed port.\n");
  415. return;
  416. }
  417. if (xhci->quirks & XHCI_BROKEN_PORT_PED) {
  418. xhci_dbg(xhci,
  419. "Broken Port Enabled/Disabled, ignoring port disable request.\n");
  420. return;
  421. }
  422. /* Write 1 to disable the port */
  423. writel(port_status | PORT_PE, addr);
  424. port_status = readl(addr);
  425. xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
  426. wIndex, port_status);
  427. }
  428. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  429. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  430. {
  431. char *port_change_bit;
  432. u32 status;
  433. switch (wValue) {
  434. case USB_PORT_FEAT_C_RESET:
  435. status = PORT_RC;
  436. port_change_bit = "reset";
  437. break;
  438. case USB_PORT_FEAT_C_BH_PORT_RESET:
  439. status = PORT_WRC;
  440. port_change_bit = "warm(BH) reset";
  441. break;
  442. case USB_PORT_FEAT_C_CONNECTION:
  443. status = PORT_CSC;
  444. port_change_bit = "connect";
  445. break;
  446. case USB_PORT_FEAT_C_OVER_CURRENT:
  447. status = PORT_OCC;
  448. port_change_bit = "over-current";
  449. break;
  450. case USB_PORT_FEAT_C_ENABLE:
  451. status = PORT_PEC;
  452. port_change_bit = "enable/disable";
  453. break;
  454. case USB_PORT_FEAT_C_SUSPEND:
  455. status = PORT_PLC;
  456. port_change_bit = "suspend/resume";
  457. break;
  458. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  459. status = PORT_PLC;
  460. port_change_bit = "link state";
  461. break;
  462. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  463. status = PORT_CEC;
  464. port_change_bit = "config error";
  465. break;
  466. default:
  467. /* Should never happen */
  468. return;
  469. }
  470. /* Change bits are all write 1 to clear */
  471. writel(port_status | status, addr);
  472. port_status = readl(addr);
  473. xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
  474. port_change_bit, wIndex, port_status);
  475. }
  476. static int xhci_get_ports(struct usb_hcd *hcd, __le32 __iomem ***port_array)
  477. {
  478. int max_ports;
  479. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  480. if (hcd->speed >= HCD_USB3) {
  481. max_ports = xhci->num_usb3_ports;
  482. *port_array = xhci->usb3_ports;
  483. } else {
  484. max_ports = xhci->num_usb2_ports;
  485. *port_array = xhci->usb2_ports;
  486. }
  487. return max_ports;
  488. }
  489. static __le32 __iomem *xhci_get_port_io_addr(struct usb_hcd *hcd, int index)
  490. {
  491. __le32 __iomem **port_array;
  492. xhci_get_ports(hcd, &port_array);
  493. return port_array[index];
  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. __le32 __iomem *addr;
  504. u32 temp;
  505. addr = xhci_get_port_io_addr(hcd, index);
  506. temp = readl(addr);
  507. temp = xhci_port_state_to_neutral(temp);
  508. if (on) {
  509. /* Power on */
  510. writel(temp | PORT_POWER, addr);
  511. temp = readl(addr);
  512. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n",
  513. index, temp);
  514. } else {
  515. /* Power off */
  516. writel(temp & ~PORT_POWER, addr);
  517. }
  518. spin_unlock_irqrestore(&xhci->lock, *flags);
  519. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  520. index);
  521. if (temp)
  522. usb_acpi_set_power_state(hcd->self.root_hub,
  523. index, on);
  524. spin_lock_irqsave(&xhci->lock, *flags);
  525. }
  526. static void xhci_port_set_test_mode(struct xhci_hcd *xhci,
  527. u16 test_mode, u16 wIndex)
  528. {
  529. u32 temp;
  530. __le32 __iomem *addr;
  531. /* xhci only supports test mode for usb2 ports, i.e. xhci->main_hcd */
  532. addr = xhci_get_port_io_addr(xhci->main_hcd, wIndex);
  533. temp = readl(addr + PORTPMSC);
  534. temp |= test_mode << PORT_TEST_MODE_SHIFT;
  535. writel(temp, addr + PORTPMSC);
  536. xhci->test_mode = test_mode;
  537. if (test_mode == TEST_FORCE_EN)
  538. xhci_start(xhci);
  539. }
  540. static int xhci_enter_test_mode(struct xhci_hcd *xhci,
  541. u16 test_mode, u16 wIndex, unsigned long *flags)
  542. {
  543. int i, retval;
  544. /* Disable all Device Slots */
  545. xhci_dbg(xhci, "Disable all slots\n");
  546. spin_unlock_irqrestore(&xhci->lock, *flags);
  547. for (i = 1; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
  548. if (!xhci->devs[i])
  549. continue;
  550. retval = xhci_disable_slot(xhci, i);
  551. if (retval)
  552. xhci_err(xhci, "Failed to disable slot %d, %d. Enter test mode anyway\n",
  553. i, retval);
  554. }
  555. spin_lock_irqsave(&xhci->lock, *flags);
  556. /* Put all ports to the Disable state by clear PP */
  557. xhci_dbg(xhci, "Disable all port (PP = 0)\n");
  558. /* Power off USB3 ports*/
  559. for (i = 0; i < xhci->num_usb3_ports; i++)
  560. xhci_set_port_power(xhci, xhci->shared_hcd, i, false, flags);
  561. /* Power off USB2 ports*/
  562. for (i = 0; i < xhci->num_usb2_ports; i++)
  563. xhci_set_port_power(xhci, xhci->main_hcd, i, false, flags);
  564. /* Stop the controller */
  565. xhci_dbg(xhci, "Stop controller\n");
  566. retval = xhci_halt(xhci);
  567. if (retval)
  568. return retval;
  569. /* Disable runtime PM for test mode */
  570. pm_runtime_forbid(xhci_to_hcd(xhci)->self.controller);
  571. /* Set PORTPMSC.PTC field to enter selected test mode */
  572. /* Port is selected by wIndex. port_id = wIndex + 1 */
  573. xhci_dbg(xhci, "Enter Test Mode: %d, Port_id=%d\n",
  574. test_mode, wIndex + 1);
  575. xhci_port_set_test_mode(xhci, test_mode, wIndex);
  576. return retval;
  577. }
  578. static int xhci_exit_test_mode(struct xhci_hcd *xhci)
  579. {
  580. int retval;
  581. if (!xhci->test_mode) {
  582. xhci_err(xhci, "Not in test mode, do nothing.\n");
  583. return 0;
  584. }
  585. if (xhci->test_mode == TEST_FORCE_EN &&
  586. !(xhci->xhc_state & XHCI_STATE_HALTED)) {
  587. retval = xhci_halt(xhci);
  588. if (retval)
  589. return retval;
  590. }
  591. pm_runtime_allow(xhci_to_hcd(xhci)->self.controller);
  592. xhci->test_mode = 0;
  593. return xhci_reset(xhci);
  594. }
  595. void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  596. int port_id, u32 link_state)
  597. {
  598. u32 temp;
  599. temp = readl(port_array[port_id]);
  600. temp = xhci_port_state_to_neutral(temp);
  601. temp &= ~PORT_PLS_MASK;
  602. temp |= PORT_LINK_STROBE | link_state;
  603. writel(temp, port_array[port_id]);
  604. }
  605. static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
  606. __le32 __iomem **port_array, int port_id, u16 wake_mask)
  607. {
  608. u32 temp;
  609. temp = readl(port_array[port_id]);
  610. temp = xhci_port_state_to_neutral(temp);
  611. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
  612. temp |= PORT_WKCONN_E;
  613. else
  614. temp &= ~PORT_WKCONN_E;
  615. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
  616. temp |= PORT_WKDISC_E;
  617. else
  618. temp &= ~PORT_WKDISC_E;
  619. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
  620. temp |= PORT_WKOC_E;
  621. else
  622. temp &= ~PORT_WKOC_E;
  623. writel(temp, port_array[port_id]);
  624. }
  625. /* Test and clear port RWC bit */
  626. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  627. int port_id, u32 port_bit)
  628. {
  629. u32 temp;
  630. temp = readl(port_array[port_id]);
  631. if (temp & port_bit) {
  632. temp = xhci_port_state_to_neutral(temp);
  633. temp |= port_bit;
  634. writel(temp, port_array[port_id]);
  635. }
  636. }
  637. /* Updates Link Status for USB 2.1 port */
  638. static void xhci_hub_report_usb2_link_state(u32 *status, u32 status_reg)
  639. {
  640. if ((status_reg & PORT_PLS_MASK) == XDEV_U2)
  641. *status |= USB_PORT_STAT_L1;
  642. }
  643. /* Updates Link Status for super Speed port */
  644. static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
  645. u32 *status, u32 status_reg)
  646. {
  647. u32 pls = status_reg & PORT_PLS_MASK;
  648. /* resume state is a xHCI internal state.
  649. * Do not report it to usb core, instead, pretend to be U3,
  650. * thus usb core knows it's not ready for transfer
  651. */
  652. if (pls == XDEV_RESUME) {
  653. *status |= USB_SS_PORT_LS_U3;
  654. return;
  655. }
  656. /* When the CAS bit is set then warm reset
  657. * should be performed on port
  658. */
  659. if (status_reg & PORT_CAS) {
  660. /* The CAS bit can be set while the port is
  661. * in any link state.
  662. * Only roothubs have CAS bit, so we
  663. * pretend to be in compliance mode
  664. * unless we're already in compliance
  665. * or the inactive state.
  666. */
  667. if (pls != USB_SS_PORT_LS_COMP_MOD &&
  668. pls != USB_SS_PORT_LS_SS_INACTIVE) {
  669. pls = USB_SS_PORT_LS_COMP_MOD;
  670. }
  671. /* Return also connection bit -
  672. * hub state machine resets port
  673. * when this bit is set.
  674. */
  675. pls |= USB_PORT_STAT_CONNECTION;
  676. } else {
  677. /*
  678. * If CAS bit isn't set but the Port is already at
  679. * Compliance Mode, fake a connection so the USB core
  680. * notices the Compliance state and resets the port.
  681. * This resolves an issue generated by the SN65LVPE502CP
  682. * in which sometimes the port enters compliance mode
  683. * caused by a delay on the host-device negotiation.
  684. */
  685. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  686. (pls == USB_SS_PORT_LS_COMP_MOD))
  687. pls |= USB_PORT_STAT_CONNECTION;
  688. }
  689. /* update status field */
  690. *status |= pls;
  691. }
  692. /*
  693. * Function for Compliance Mode Quirk.
  694. *
  695. * This Function verifies if all xhc USB3 ports have entered U0, if so,
  696. * the compliance mode timer is deleted. A port won't enter
  697. * compliance mode if it has previously entered U0.
  698. */
  699. static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,
  700. u16 wIndex)
  701. {
  702. u32 all_ports_seen_u0 = ((1 << xhci->num_usb3_ports)-1);
  703. bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
  704. if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
  705. return;
  706. if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
  707. xhci->port_status_u0 |= 1 << wIndex;
  708. if (xhci->port_status_u0 == all_ports_seen_u0) {
  709. del_timer_sync(&xhci->comp_mode_recovery_timer);
  710. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  711. "All USB3 ports have entered U0 already!");
  712. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  713. "Compliance Mode Recovery Timer Deleted.");
  714. }
  715. }
  716. }
  717. static u32 xhci_get_ext_port_status(u32 raw_port_status, u32 port_li)
  718. {
  719. u32 ext_stat = 0;
  720. int speed_id;
  721. /* only support rx and tx lane counts of 1 in usb3.1 spec */
  722. speed_id = DEV_PORT_SPEED(raw_port_status);
  723. ext_stat |= speed_id; /* bits 3:0, RX speed id */
  724. ext_stat |= speed_id << 4; /* bits 7:4, TX speed id */
  725. ext_stat |= PORT_RX_LANES(port_li) << 8; /* bits 11:8 Rx lane count */
  726. ext_stat |= PORT_TX_LANES(port_li) << 12; /* bits 15:12 Tx lane count */
  727. return ext_stat;
  728. }
  729. /*
  730. * Converts a raw xHCI port status into the format that external USB 2.0 or USB
  731. * 3.0 hubs use.
  732. *
  733. * Possible side effects:
  734. * - Mark a port as being done with device resume,
  735. * and ring the endpoint doorbells.
  736. * - Stop the Synopsys redriver Compliance Mode polling.
  737. * - Drop and reacquire the xHCI lock, in order to wait for port resume.
  738. */
  739. static u32 xhci_get_port_status(struct usb_hcd *hcd,
  740. struct xhci_bus_state *bus_state,
  741. __le32 __iomem **port_array,
  742. u16 wIndex, u32 raw_port_status,
  743. unsigned long flags)
  744. __releases(&xhci->lock)
  745. __acquires(&xhci->lock)
  746. {
  747. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  748. u32 status = 0;
  749. int slot_id;
  750. /* wPortChange bits */
  751. if (raw_port_status & PORT_CSC)
  752. status |= USB_PORT_STAT_C_CONNECTION << 16;
  753. if (raw_port_status & PORT_PEC)
  754. status |= USB_PORT_STAT_C_ENABLE << 16;
  755. if ((raw_port_status & PORT_OCC))
  756. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  757. if ((raw_port_status & PORT_RC))
  758. status |= USB_PORT_STAT_C_RESET << 16;
  759. /* USB3.0 only */
  760. if (hcd->speed >= HCD_USB3) {
  761. /* Port link change with port in resume state should not be
  762. * reported to usbcore, as this is an internal state to be
  763. * handled by xhci driver. Reporting PLC to usbcore may
  764. * cause usbcore clearing PLC first and port change event
  765. * irq won't be generated.
  766. */
  767. if ((raw_port_status & PORT_PLC) &&
  768. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME)
  769. status |= USB_PORT_STAT_C_LINK_STATE << 16;
  770. if ((raw_port_status & PORT_WRC))
  771. status |= USB_PORT_STAT_C_BH_RESET << 16;
  772. if ((raw_port_status & PORT_CEC))
  773. status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
  774. }
  775. if (hcd->speed < HCD_USB3) {
  776. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U3
  777. && (raw_port_status & PORT_POWER))
  778. status |= USB_PORT_STAT_SUSPEND;
  779. }
  780. if ((raw_port_status & PORT_PLS_MASK) == XDEV_RESUME &&
  781. !DEV_SUPERSPEED_ANY(raw_port_status)) {
  782. if ((raw_port_status & PORT_RESET) ||
  783. !(raw_port_status & PORT_PE))
  784. return 0xffffffff;
  785. /* did port event handler already start resume timing? */
  786. if (!bus_state->resume_done[wIndex]) {
  787. /* If not, maybe we are in a host initated resume? */
  788. if (test_bit(wIndex, &bus_state->resuming_ports)) {
  789. /* Host initated resume doesn't time the resume
  790. * signalling using resume_done[].
  791. * It manually sets RESUME state, sleeps 20ms
  792. * and sets U0 state. This should probably be
  793. * changed, but not right now.
  794. */
  795. } else {
  796. /* port resume was discovered now and here,
  797. * start resume timing
  798. */
  799. unsigned long timeout = jiffies +
  800. msecs_to_jiffies(USB_RESUME_TIMEOUT);
  801. set_bit(wIndex, &bus_state->resuming_ports);
  802. bus_state->resume_done[wIndex] = timeout;
  803. mod_timer(&hcd->rh_timer, timeout);
  804. }
  805. /* Has resume been signalled for USB_RESUME_TIME yet? */
  806. } else if (time_after_eq(jiffies,
  807. bus_state->resume_done[wIndex])) {
  808. int time_left;
  809. xhci_dbg(xhci, "Resume USB2 port %d\n",
  810. wIndex + 1);
  811. bus_state->resume_done[wIndex] = 0;
  812. clear_bit(wIndex, &bus_state->resuming_ports);
  813. set_bit(wIndex, &bus_state->rexit_ports);
  814. xhci_test_and_clear_bit(xhci, port_array, wIndex,
  815. PORT_PLC);
  816. xhci_set_link_state(xhci, port_array, wIndex,
  817. XDEV_U0);
  818. spin_unlock_irqrestore(&xhci->lock, flags);
  819. time_left = wait_for_completion_timeout(
  820. &bus_state->rexit_done[wIndex],
  821. msecs_to_jiffies(
  822. XHCI_MAX_REXIT_TIMEOUT));
  823. spin_lock_irqsave(&xhci->lock, flags);
  824. if (time_left) {
  825. slot_id = xhci_find_slot_id_by_port(hcd,
  826. xhci, wIndex + 1);
  827. if (!slot_id) {
  828. xhci_dbg(xhci, "slot_id is zero\n");
  829. return 0xffffffff;
  830. }
  831. xhci_ring_device(xhci, slot_id);
  832. } else {
  833. int port_status = readl(port_array[wIndex]);
  834. xhci_warn(xhci, "Port resume took longer than %i msec, port status = 0x%x\n",
  835. XHCI_MAX_REXIT_TIMEOUT,
  836. port_status);
  837. status |= USB_PORT_STAT_SUSPEND;
  838. clear_bit(wIndex, &bus_state->rexit_ports);
  839. }
  840. bus_state->port_c_suspend |= 1 << wIndex;
  841. bus_state->suspended_ports &= ~(1 << wIndex);
  842. } else {
  843. /*
  844. * The resume has been signaling for less than
  845. * USB_RESUME_TIME. Report the port status as SUSPEND,
  846. * let the usbcore check port status again and clear
  847. * resume signaling later.
  848. */
  849. status |= USB_PORT_STAT_SUSPEND;
  850. }
  851. }
  852. /*
  853. * Clear stale usb2 resume signalling variables in case port changed
  854. * state during resume signalling. For example on error
  855. */
  856. if ((bus_state->resume_done[wIndex] ||
  857. test_bit(wIndex, &bus_state->resuming_ports)) &&
  858. (raw_port_status & PORT_PLS_MASK) != XDEV_U3 &&
  859. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME) {
  860. bus_state->resume_done[wIndex] = 0;
  861. clear_bit(wIndex, &bus_state->resuming_ports);
  862. }
  863. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U0 &&
  864. (raw_port_status & PORT_POWER)) {
  865. if (bus_state->suspended_ports & (1 << wIndex)) {
  866. bus_state->suspended_ports &= ~(1 << wIndex);
  867. if (hcd->speed < HCD_USB3)
  868. bus_state->port_c_suspend |= 1 << wIndex;
  869. }
  870. bus_state->resume_done[wIndex] = 0;
  871. clear_bit(wIndex, &bus_state->resuming_ports);
  872. }
  873. if (raw_port_status & PORT_CONNECT) {
  874. status |= USB_PORT_STAT_CONNECTION;
  875. status |= xhci_port_speed(raw_port_status);
  876. }
  877. if (raw_port_status & PORT_PE)
  878. status |= USB_PORT_STAT_ENABLE;
  879. if (raw_port_status & PORT_OC)
  880. status |= USB_PORT_STAT_OVERCURRENT;
  881. if (raw_port_status & PORT_RESET)
  882. status |= USB_PORT_STAT_RESET;
  883. if (raw_port_status & PORT_POWER) {
  884. if (hcd->speed >= HCD_USB3)
  885. status |= USB_SS_PORT_STAT_POWER;
  886. else
  887. status |= USB_PORT_STAT_POWER;
  888. }
  889. /* Update Port Link State */
  890. if (hcd->speed >= HCD_USB3) {
  891. xhci_hub_report_usb3_link_state(xhci, &status, raw_port_status);
  892. /*
  893. * Verify if all USB3 Ports Have entered U0 already.
  894. * Delete Compliance Mode Timer if so.
  895. */
  896. xhci_del_comp_mod_timer(xhci, raw_port_status, wIndex);
  897. } else {
  898. xhci_hub_report_usb2_link_state(&status, raw_port_status);
  899. }
  900. if (bus_state->port_c_suspend & (1 << wIndex))
  901. status |= USB_PORT_STAT_C_SUSPEND << 16;
  902. return status;
  903. }
  904. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  905. u16 wIndex, char *buf, u16 wLength)
  906. {
  907. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  908. int max_ports;
  909. unsigned long flags;
  910. u32 temp, status;
  911. int retval = 0;
  912. __le32 __iomem **port_array;
  913. int slot_id;
  914. struct xhci_bus_state *bus_state;
  915. u16 link_state = 0;
  916. u16 wake_mask = 0;
  917. u16 timeout = 0;
  918. u16 test_mode = 0;
  919. max_ports = xhci_get_ports(hcd, &port_array);
  920. bus_state = &xhci->bus_state[hcd_index(hcd)];
  921. spin_lock_irqsave(&xhci->lock, flags);
  922. switch (typeReq) {
  923. case GetHubStatus:
  924. /* No power source, over-current reported per port */
  925. memset(buf, 0, 4);
  926. break;
  927. case GetHubDescriptor:
  928. /* Check to make sure userspace is asking for the USB 3.0 hub
  929. * descriptor for the USB 3.0 roothub. If not, we stall the
  930. * endpoint, like external hubs do.
  931. */
  932. if (hcd->speed >= HCD_USB3 &&
  933. (wLength < USB_DT_SS_HUB_SIZE ||
  934. wValue != (USB_DT_SS_HUB << 8))) {
  935. xhci_dbg(xhci, "Wrong hub descriptor type for "
  936. "USB 3.0 roothub.\n");
  937. goto error;
  938. }
  939. xhci_hub_descriptor(hcd, xhci,
  940. (struct usb_hub_descriptor *) buf);
  941. break;
  942. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  943. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  944. goto error;
  945. if (hcd->speed < HCD_USB3)
  946. goto error;
  947. retval = xhci_create_usb3_bos_desc(xhci, buf, wLength);
  948. spin_unlock_irqrestore(&xhci->lock, flags);
  949. return retval;
  950. case GetPortStatus:
  951. if (!wIndex || wIndex > max_ports)
  952. goto error;
  953. wIndex--;
  954. temp = readl(port_array[wIndex]);
  955. if (temp == ~(u32)0) {
  956. xhci_hc_died(xhci);
  957. retval = -ENODEV;
  958. break;
  959. }
  960. trace_xhci_get_port_status(wIndex, temp);
  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. /* fall through */
  1229. case USB_PORT_FEAT_C_RESET:
  1230. case USB_PORT_FEAT_C_BH_PORT_RESET:
  1231. case USB_PORT_FEAT_C_CONNECTION:
  1232. case USB_PORT_FEAT_C_OVER_CURRENT:
  1233. case USB_PORT_FEAT_C_ENABLE:
  1234. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  1235. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  1236. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  1237. port_array[wIndex], temp);
  1238. break;
  1239. case USB_PORT_FEAT_ENABLE:
  1240. xhci_disable_port(hcd, xhci, wIndex,
  1241. port_array[wIndex], temp);
  1242. break;
  1243. case USB_PORT_FEAT_POWER:
  1244. xhci_set_port_power(xhci, hcd, wIndex, false, &flags);
  1245. break;
  1246. case USB_PORT_FEAT_TEST:
  1247. retval = xhci_exit_test_mode(xhci);
  1248. break;
  1249. default:
  1250. goto error;
  1251. }
  1252. break;
  1253. default:
  1254. error:
  1255. /* "stall" on error */
  1256. retval = -EPIPE;
  1257. }
  1258. spin_unlock_irqrestore(&xhci->lock, flags);
  1259. return retval;
  1260. }
  1261. /*
  1262. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  1263. * Ports are 0-indexed from the HCD point of view,
  1264. * and 1-indexed from the USB core pointer of view.
  1265. *
  1266. * Note that the status change bits will be cleared as soon as a port status
  1267. * change event is generated, so we use the saved status from that event.
  1268. */
  1269. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  1270. {
  1271. unsigned long flags;
  1272. u32 temp, status;
  1273. u32 mask;
  1274. int i, retval;
  1275. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1276. int max_ports;
  1277. __le32 __iomem **port_array;
  1278. struct xhci_bus_state *bus_state;
  1279. bool reset_change = false;
  1280. max_ports = xhci_get_ports(hcd, &port_array);
  1281. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1282. /* Initial status is no changes */
  1283. retval = (max_ports + 8) / 8;
  1284. memset(buf, 0, retval);
  1285. /*
  1286. * Inform the usbcore about resume-in-progress by returning
  1287. * a non-zero value even if there are no status changes.
  1288. */
  1289. status = bus_state->resuming_ports;
  1290. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
  1291. spin_lock_irqsave(&xhci->lock, flags);
  1292. /* For each port, did anything change? If so, set that bit in buf. */
  1293. for (i = 0; i < max_ports; i++) {
  1294. temp = readl(port_array[i]);
  1295. if (temp == ~(u32)0) {
  1296. xhci_hc_died(xhci);
  1297. retval = -ENODEV;
  1298. break;
  1299. }
  1300. trace_xhci_hub_status_data(i, temp);
  1301. if ((temp & mask) != 0 ||
  1302. (bus_state->port_c_suspend & 1 << i) ||
  1303. (bus_state->resume_done[i] && time_after_eq(
  1304. jiffies, bus_state->resume_done[i]))) {
  1305. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  1306. status = 1;
  1307. }
  1308. if ((temp & PORT_RC))
  1309. reset_change = true;
  1310. }
  1311. if (!status && !reset_change) {
  1312. xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
  1313. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  1314. }
  1315. spin_unlock_irqrestore(&xhci->lock, flags);
  1316. return status ? retval : 0;
  1317. }
  1318. #ifdef CONFIG_PM
  1319. int xhci_bus_suspend(struct usb_hcd *hcd)
  1320. {
  1321. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1322. int max_ports, port_index;
  1323. __le32 __iomem **port_array;
  1324. struct xhci_bus_state *bus_state;
  1325. unsigned long flags;
  1326. max_ports = xhci_get_ports(hcd, &port_array);
  1327. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1328. spin_lock_irqsave(&xhci->lock, flags);
  1329. if (hcd->self.root_hub->do_remote_wakeup) {
  1330. if (bus_state->resuming_ports || /* USB2 */
  1331. bus_state->port_remote_wakeup) { /* USB3 */
  1332. spin_unlock_irqrestore(&xhci->lock, flags);
  1333. xhci_dbg(xhci, "suspend failed because a port is resuming\n");
  1334. return -EBUSY;
  1335. }
  1336. }
  1337. port_index = max_ports;
  1338. bus_state->bus_suspended = 0;
  1339. while (port_index--) {
  1340. /* suspend the port if the port is not suspended */
  1341. u32 t1, t2;
  1342. int slot_id;
  1343. t1 = readl(port_array[port_index]);
  1344. t2 = xhci_port_state_to_neutral(t1);
  1345. if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
  1346. xhci_dbg(xhci, "port %d not suspended\n", port_index);
  1347. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1348. port_index + 1);
  1349. if (slot_id) {
  1350. spin_unlock_irqrestore(&xhci->lock, flags);
  1351. xhci_stop_device(xhci, slot_id, 1);
  1352. spin_lock_irqsave(&xhci->lock, flags);
  1353. }
  1354. t2 &= ~PORT_PLS_MASK;
  1355. t2 |= PORT_LINK_STROBE | XDEV_U3;
  1356. set_bit(port_index, &bus_state->bus_suspended);
  1357. }
  1358. /* USB core sets remote wake mask for USB 3.0 hubs,
  1359. * including the USB 3.0 roothub, but only if CONFIG_PM
  1360. * is enabled, so also enable remote wake here.
  1361. */
  1362. if (hcd->self.root_hub->do_remote_wakeup) {
  1363. if (t1 & PORT_CONNECT) {
  1364. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  1365. t2 &= ~PORT_WKCONN_E;
  1366. } else {
  1367. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  1368. t2 &= ~PORT_WKDISC_E;
  1369. }
  1370. } else
  1371. t2 &= ~PORT_WAKE_BITS;
  1372. t1 = xhci_port_state_to_neutral(t1);
  1373. if (t1 != t2)
  1374. writel(t2, port_array[port_index]);
  1375. }
  1376. hcd->state = HC_STATE_SUSPENDED;
  1377. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  1378. spin_unlock_irqrestore(&xhci->lock, flags);
  1379. return 0;
  1380. }
  1381. /*
  1382. * Workaround for missing Cold Attach Status (CAS) if device re-plugged in S3.
  1383. * warm reset a USB3 device stuck in polling or compliance mode after resume.
  1384. * See Intel 100/c230 series PCH specification update Doc #332692-006 Errata #8
  1385. */
  1386. static bool xhci_port_missing_cas_quirk(int port_index,
  1387. __le32 __iomem **port_array)
  1388. {
  1389. u32 portsc;
  1390. portsc = readl(port_array[port_index]);
  1391. /* if any of these are set we are not stuck */
  1392. if (portsc & (PORT_CONNECT | PORT_CAS))
  1393. return false;
  1394. if (((portsc & PORT_PLS_MASK) != XDEV_POLLING) &&
  1395. ((portsc & PORT_PLS_MASK) != XDEV_COMP_MODE))
  1396. return false;
  1397. /* clear wakeup/change bits, and do a warm port reset */
  1398. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1399. portsc |= PORT_WR;
  1400. writel(portsc, port_array[port_index]);
  1401. /* flush write */
  1402. readl(port_array[port_index]);
  1403. return true;
  1404. }
  1405. int xhci_bus_resume(struct usb_hcd *hcd)
  1406. {
  1407. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1408. struct xhci_bus_state *bus_state;
  1409. __le32 __iomem **port_array;
  1410. unsigned long flags;
  1411. int max_ports, port_index;
  1412. int slot_id;
  1413. int sret;
  1414. u32 next_state;
  1415. u32 temp, portsc;
  1416. max_ports = xhci_get_ports(hcd, &port_array);
  1417. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1418. if (time_before(jiffies, bus_state->next_statechange))
  1419. msleep(5);
  1420. spin_lock_irqsave(&xhci->lock, flags);
  1421. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1422. spin_unlock_irqrestore(&xhci->lock, flags);
  1423. return -ESHUTDOWN;
  1424. }
  1425. /* delay the irqs */
  1426. temp = readl(&xhci->op_regs->command);
  1427. temp &= ~CMD_EIE;
  1428. writel(temp, &xhci->op_regs->command);
  1429. /* bus specific resume for ports we suspended at bus_suspend */
  1430. if (hcd->speed >= HCD_USB3)
  1431. next_state = XDEV_U0;
  1432. else
  1433. next_state = XDEV_RESUME;
  1434. port_index = max_ports;
  1435. while (port_index--) {
  1436. portsc = readl(port_array[port_index]);
  1437. /* warm reset CAS limited ports stuck in polling/compliance */
  1438. if ((xhci->quirks & XHCI_MISSING_CAS) &&
  1439. (hcd->speed >= HCD_USB3) &&
  1440. xhci_port_missing_cas_quirk(port_index, port_array)) {
  1441. xhci_dbg(xhci, "reset stuck port %d\n", port_index);
  1442. clear_bit(port_index, &bus_state->bus_suspended);
  1443. continue;
  1444. }
  1445. /* resume if we suspended the link, and it is still suspended */
  1446. if (test_bit(port_index, &bus_state->bus_suspended))
  1447. switch (portsc & PORT_PLS_MASK) {
  1448. case XDEV_U3:
  1449. portsc = xhci_port_state_to_neutral(portsc);
  1450. portsc &= ~PORT_PLS_MASK;
  1451. portsc |= PORT_LINK_STROBE | next_state;
  1452. break;
  1453. case XDEV_RESUME:
  1454. /* resume already initiated */
  1455. break;
  1456. default:
  1457. /* not in a resumeable state, ignore it */
  1458. clear_bit(port_index,
  1459. &bus_state->bus_suspended);
  1460. break;
  1461. }
  1462. /* disable wake for all ports, write new link state if needed */
  1463. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1464. writel(portsc, port_array[port_index]);
  1465. }
  1466. /* USB2 specific resume signaling delay and U0 link state transition */
  1467. if (hcd->speed < HCD_USB3) {
  1468. if (bus_state->bus_suspended) {
  1469. spin_unlock_irqrestore(&xhci->lock, flags);
  1470. msleep(USB_RESUME_TIMEOUT);
  1471. spin_lock_irqsave(&xhci->lock, flags);
  1472. }
  1473. for_each_set_bit(port_index, &bus_state->bus_suspended,
  1474. BITS_PER_LONG) {
  1475. /* Clear PLC to poll it later for U0 transition */
  1476. xhci_test_and_clear_bit(xhci, port_array, port_index,
  1477. PORT_PLC);
  1478. xhci_set_link_state(xhci, port_array, port_index,
  1479. XDEV_U0);
  1480. }
  1481. }
  1482. /* poll for U0 link state complete, both USB2 and USB3 */
  1483. for_each_set_bit(port_index, &bus_state->bus_suspended, BITS_PER_LONG) {
  1484. sret = xhci_handshake(port_array[port_index], PORT_PLC,
  1485. PORT_PLC, 10 * 1000);
  1486. if (sret) {
  1487. xhci_warn(xhci, "port %d resume PLC timeout\n",
  1488. port_index);
  1489. continue;
  1490. }
  1491. xhci_test_and_clear_bit(xhci, port_array, port_index, PORT_PLC);
  1492. slot_id = xhci_find_slot_id_by_port(hcd, xhci, port_index + 1);
  1493. if (slot_id)
  1494. xhci_ring_device(xhci, slot_id);
  1495. }
  1496. (void) readl(&xhci->op_regs->command);
  1497. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  1498. /* re-enable irqs */
  1499. temp = readl(&xhci->op_regs->command);
  1500. temp |= CMD_EIE;
  1501. writel(temp, &xhci->op_regs->command);
  1502. temp = readl(&xhci->op_regs->command);
  1503. spin_unlock_irqrestore(&xhci->lock, flags);
  1504. return 0;
  1505. }
  1506. #endif /* CONFIG_PM */