xhci-hub.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  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. ret = -ENOMEM;
  374. goto cmd_cleanup;
  375. }
  376. ret = xhci_queue_stop_endpoint(xhci, command, slot_id,
  377. i, suspend);
  378. if (ret) {
  379. spin_unlock_irqrestore(&xhci->lock, flags);
  380. xhci_free_command(xhci, command);
  381. goto cmd_cleanup;
  382. }
  383. }
  384. }
  385. ret = xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
  386. if (ret) {
  387. spin_unlock_irqrestore(&xhci->lock, flags);
  388. goto cmd_cleanup;
  389. }
  390. xhci_ring_cmd_db(xhci);
  391. spin_unlock_irqrestore(&xhci->lock, flags);
  392. /* Wait for last stop endpoint command to finish */
  393. wait_for_completion(cmd->completion);
  394. if (cmd->status == COMP_COMMAND_ABORTED ||
  395. cmd->status == COMP_COMMAND_RING_STOPPED) {
  396. xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
  397. ret = -ETIME;
  398. }
  399. cmd_cleanup:
  400. xhci_free_command(xhci, cmd);
  401. return ret;
  402. }
  403. /*
  404. * Ring device, it rings the all doorbells unconditionally.
  405. */
  406. void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
  407. {
  408. int i, s;
  409. struct xhci_virt_ep *ep;
  410. for (i = 0; i < LAST_EP_INDEX + 1; i++) {
  411. ep = &xhci->devs[slot_id]->eps[i];
  412. if (ep->ep_state & EP_HAS_STREAMS) {
  413. for (s = 1; s < ep->stream_info->num_streams; s++)
  414. xhci_ring_ep_doorbell(xhci, slot_id, i, s);
  415. } else if (ep->ring && ep->ring->dequeue) {
  416. xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
  417. }
  418. }
  419. return;
  420. }
  421. static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  422. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  423. {
  424. /* Don't allow the USB core to disable SuperSpeed ports. */
  425. if (hcd->speed >= HCD_USB3) {
  426. xhci_dbg(xhci, "Ignoring request to disable "
  427. "SuperSpeed port.\n");
  428. return;
  429. }
  430. if (xhci->quirks & XHCI_BROKEN_PORT_PED) {
  431. xhci_dbg(xhci,
  432. "Broken Port Enabled/Disabled, ignoring port disable request.\n");
  433. return;
  434. }
  435. /* Write 1 to disable the port */
  436. writel(port_status | PORT_PE, addr);
  437. port_status = readl(addr);
  438. xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
  439. wIndex, port_status);
  440. }
  441. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  442. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  443. {
  444. char *port_change_bit;
  445. u32 status;
  446. switch (wValue) {
  447. case USB_PORT_FEAT_C_RESET:
  448. status = PORT_RC;
  449. port_change_bit = "reset";
  450. break;
  451. case USB_PORT_FEAT_C_BH_PORT_RESET:
  452. status = PORT_WRC;
  453. port_change_bit = "warm(BH) reset";
  454. break;
  455. case USB_PORT_FEAT_C_CONNECTION:
  456. status = PORT_CSC;
  457. port_change_bit = "connect";
  458. break;
  459. case USB_PORT_FEAT_C_OVER_CURRENT:
  460. status = PORT_OCC;
  461. port_change_bit = "over-current";
  462. break;
  463. case USB_PORT_FEAT_C_ENABLE:
  464. status = PORT_PEC;
  465. port_change_bit = "enable/disable";
  466. break;
  467. case USB_PORT_FEAT_C_SUSPEND:
  468. status = PORT_PLC;
  469. port_change_bit = "suspend/resume";
  470. break;
  471. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  472. status = PORT_PLC;
  473. port_change_bit = "link state";
  474. break;
  475. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  476. status = PORT_CEC;
  477. port_change_bit = "config error";
  478. break;
  479. default:
  480. /* Should never happen */
  481. return;
  482. }
  483. /* Change bits are all write 1 to clear */
  484. writel(port_status | status, addr);
  485. port_status = readl(addr);
  486. xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
  487. port_change_bit, wIndex, port_status);
  488. }
  489. static int xhci_get_ports(struct usb_hcd *hcd, __le32 __iomem ***port_array)
  490. {
  491. int max_ports;
  492. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  493. if (hcd->speed >= HCD_USB3) {
  494. max_ports = xhci->num_usb3_ports;
  495. *port_array = xhci->usb3_ports;
  496. } else {
  497. max_ports = xhci->num_usb2_ports;
  498. *port_array = xhci->usb2_ports;
  499. }
  500. return max_ports;
  501. }
  502. static __le32 __iomem *xhci_get_port_io_addr(struct usb_hcd *hcd, int index)
  503. {
  504. __le32 __iomem **port_array;
  505. xhci_get_ports(hcd, &port_array);
  506. return port_array[index];
  507. }
  508. /*
  509. * xhci_set_port_power() must be called with xhci->lock held.
  510. * It will release and re-aquire the lock while calling ACPI
  511. * method.
  512. */
  513. static void xhci_set_port_power(struct xhci_hcd *xhci, struct usb_hcd *hcd,
  514. u16 index, bool on, unsigned long *flags)
  515. {
  516. __le32 __iomem *addr;
  517. u32 temp;
  518. addr = xhci_get_port_io_addr(hcd, index);
  519. temp = readl(addr);
  520. temp = xhci_port_state_to_neutral(temp);
  521. if (on) {
  522. /* Power on */
  523. writel(temp | PORT_POWER, addr);
  524. temp = readl(addr);
  525. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n",
  526. index, temp);
  527. } else {
  528. /* Power off */
  529. writel(temp & ~PORT_POWER, addr);
  530. }
  531. spin_unlock_irqrestore(&xhci->lock, *flags);
  532. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  533. index);
  534. if (temp)
  535. usb_acpi_set_power_state(hcd->self.root_hub,
  536. index, on);
  537. spin_lock_irqsave(&xhci->lock, *flags);
  538. }
  539. static void xhci_port_set_test_mode(struct xhci_hcd *xhci,
  540. u16 test_mode, u16 wIndex)
  541. {
  542. u32 temp;
  543. __le32 __iomem *addr;
  544. /* xhci only supports test mode for usb2 ports, i.e. xhci->main_hcd */
  545. addr = xhci_get_port_io_addr(xhci->main_hcd, wIndex);
  546. temp = readl(addr + PORTPMSC);
  547. temp |= test_mode << PORT_TEST_MODE_SHIFT;
  548. writel(temp, addr + PORTPMSC);
  549. xhci->test_mode = test_mode;
  550. if (test_mode == TEST_FORCE_EN)
  551. xhci_start(xhci);
  552. }
  553. static int xhci_enter_test_mode(struct xhci_hcd *xhci,
  554. u16 test_mode, u16 wIndex, unsigned long *flags)
  555. {
  556. int i, retval;
  557. /* Disable all Device Slots */
  558. xhci_dbg(xhci, "Disable all slots\n");
  559. spin_unlock_irqrestore(&xhci->lock, *flags);
  560. for (i = 1; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
  561. retval = xhci_disable_slot(xhci, NULL, i);
  562. if (retval)
  563. xhci_err(xhci, "Failed to disable slot %d, %d. Enter test mode anyway\n",
  564. i, retval);
  565. }
  566. spin_lock_irqsave(&xhci->lock, *flags);
  567. /* Put all ports to the Disable state by clear PP */
  568. xhci_dbg(xhci, "Disable all port (PP = 0)\n");
  569. /* Power off USB3 ports*/
  570. for (i = 0; i < xhci->num_usb3_ports; i++)
  571. xhci_set_port_power(xhci, xhci->shared_hcd, i, false, flags);
  572. /* Power off USB2 ports*/
  573. for (i = 0; i < xhci->num_usb2_ports; i++)
  574. xhci_set_port_power(xhci, xhci->main_hcd, i, false, flags);
  575. /* Stop the controller */
  576. xhci_dbg(xhci, "Stop controller\n");
  577. retval = xhci_halt(xhci);
  578. if (retval)
  579. return retval;
  580. /* Disable runtime PM for test mode */
  581. pm_runtime_forbid(xhci_to_hcd(xhci)->self.controller);
  582. /* Set PORTPMSC.PTC field to enter selected test mode */
  583. /* Port is selected by wIndex. port_id = wIndex + 1 */
  584. xhci_dbg(xhci, "Enter Test Mode: %d, Port_id=%d\n",
  585. test_mode, wIndex + 1);
  586. xhci_port_set_test_mode(xhci, test_mode, wIndex);
  587. return retval;
  588. }
  589. static int xhci_exit_test_mode(struct xhci_hcd *xhci)
  590. {
  591. int retval;
  592. if (!xhci->test_mode) {
  593. xhci_err(xhci, "Not in test mode, do nothing.\n");
  594. return 0;
  595. }
  596. if (xhci->test_mode == TEST_FORCE_EN &&
  597. !(xhci->xhc_state & XHCI_STATE_HALTED)) {
  598. retval = xhci_halt(xhci);
  599. if (retval)
  600. return retval;
  601. }
  602. pm_runtime_allow(xhci_to_hcd(xhci)->self.controller);
  603. xhci->test_mode = 0;
  604. return xhci_reset(xhci);
  605. }
  606. void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  607. int port_id, u32 link_state)
  608. {
  609. u32 temp;
  610. temp = readl(port_array[port_id]);
  611. temp = xhci_port_state_to_neutral(temp);
  612. temp &= ~PORT_PLS_MASK;
  613. temp |= PORT_LINK_STROBE | link_state;
  614. writel(temp, port_array[port_id]);
  615. }
  616. static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
  617. __le32 __iomem **port_array, int port_id, u16 wake_mask)
  618. {
  619. u32 temp;
  620. temp = readl(port_array[port_id]);
  621. temp = xhci_port_state_to_neutral(temp);
  622. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
  623. temp |= PORT_WKCONN_E;
  624. else
  625. temp &= ~PORT_WKCONN_E;
  626. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
  627. temp |= PORT_WKDISC_E;
  628. else
  629. temp &= ~PORT_WKDISC_E;
  630. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
  631. temp |= PORT_WKOC_E;
  632. else
  633. temp &= ~PORT_WKOC_E;
  634. writel(temp, port_array[port_id]);
  635. }
  636. /* Test and clear port RWC bit */
  637. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  638. int port_id, u32 port_bit)
  639. {
  640. u32 temp;
  641. temp = readl(port_array[port_id]);
  642. if (temp & port_bit) {
  643. temp = xhci_port_state_to_neutral(temp);
  644. temp |= port_bit;
  645. writel(temp, port_array[port_id]);
  646. }
  647. }
  648. /* Updates Link Status for USB 2.1 port */
  649. static void xhci_hub_report_usb2_link_state(u32 *status, u32 status_reg)
  650. {
  651. if ((status_reg & PORT_PLS_MASK) == XDEV_U2)
  652. *status |= USB_PORT_STAT_L1;
  653. }
  654. /* Updates Link Status for super Speed port */
  655. static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
  656. u32 *status, u32 status_reg)
  657. {
  658. u32 pls = status_reg & PORT_PLS_MASK;
  659. /* resume state is a xHCI internal state.
  660. * Do not report it to usb core, instead, pretend to be U3,
  661. * thus usb core knows it's not ready for transfer
  662. */
  663. if (pls == XDEV_RESUME) {
  664. *status |= USB_SS_PORT_LS_U3;
  665. return;
  666. }
  667. /* When the CAS bit is set then warm reset
  668. * should be performed on port
  669. */
  670. if (status_reg & PORT_CAS) {
  671. /* The CAS bit can be set while the port is
  672. * in any link state.
  673. * Only roothubs have CAS bit, so we
  674. * pretend to be in compliance mode
  675. * unless we're already in compliance
  676. * or the inactive state.
  677. */
  678. if (pls != USB_SS_PORT_LS_COMP_MOD &&
  679. pls != USB_SS_PORT_LS_SS_INACTIVE) {
  680. pls = USB_SS_PORT_LS_COMP_MOD;
  681. }
  682. /* Return also connection bit -
  683. * hub state machine resets port
  684. * when this bit is set.
  685. */
  686. pls |= USB_PORT_STAT_CONNECTION;
  687. } else {
  688. /*
  689. * If CAS bit isn't set but the Port is already at
  690. * Compliance Mode, fake a connection so the USB core
  691. * notices the Compliance state and resets the port.
  692. * This resolves an issue generated by the SN65LVPE502CP
  693. * in which sometimes the port enters compliance mode
  694. * caused by a delay on the host-device negotiation.
  695. */
  696. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  697. (pls == USB_SS_PORT_LS_COMP_MOD))
  698. pls |= USB_PORT_STAT_CONNECTION;
  699. }
  700. /* update status field */
  701. *status |= pls;
  702. }
  703. /*
  704. * Function for Compliance Mode Quirk.
  705. *
  706. * This Function verifies if all xhc USB3 ports have entered U0, if so,
  707. * the compliance mode timer is deleted. A port won't enter
  708. * compliance mode if it has previously entered U0.
  709. */
  710. static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,
  711. u16 wIndex)
  712. {
  713. u32 all_ports_seen_u0 = ((1 << xhci->num_usb3_ports)-1);
  714. bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
  715. if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
  716. return;
  717. if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
  718. xhci->port_status_u0 |= 1 << wIndex;
  719. if (xhci->port_status_u0 == all_ports_seen_u0) {
  720. del_timer_sync(&xhci->comp_mode_recovery_timer);
  721. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  722. "All USB3 ports have entered U0 already!");
  723. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  724. "Compliance Mode Recovery Timer Deleted.");
  725. }
  726. }
  727. }
  728. static u32 xhci_get_ext_port_status(u32 raw_port_status, u32 port_li)
  729. {
  730. u32 ext_stat = 0;
  731. int speed_id;
  732. /* only support rx and tx lane counts of 1 in usb3.1 spec */
  733. speed_id = DEV_PORT_SPEED(raw_port_status);
  734. ext_stat |= speed_id; /* bits 3:0, RX speed id */
  735. ext_stat |= speed_id << 4; /* bits 7:4, TX speed id */
  736. ext_stat |= PORT_RX_LANES(port_li) << 8; /* bits 11:8 Rx lane count */
  737. ext_stat |= PORT_TX_LANES(port_li) << 12; /* bits 15:12 Tx lane count */
  738. return ext_stat;
  739. }
  740. /*
  741. * Converts a raw xHCI port status into the format that external USB 2.0 or USB
  742. * 3.0 hubs use.
  743. *
  744. * Possible side effects:
  745. * - Mark a port as being done with device resume,
  746. * and ring the endpoint doorbells.
  747. * - Stop the Synopsys redriver Compliance Mode polling.
  748. * - Drop and reacquire the xHCI lock, in order to wait for port resume.
  749. */
  750. static u32 xhci_get_port_status(struct usb_hcd *hcd,
  751. struct xhci_bus_state *bus_state,
  752. __le32 __iomem **port_array,
  753. u16 wIndex, u32 raw_port_status,
  754. unsigned long flags)
  755. __releases(&xhci->lock)
  756. __acquires(&xhci->lock)
  757. {
  758. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  759. u32 status = 0;
  760. int slot_id;
  761. /* wPortChange bits */
  762. if (raw_port_status & PORT_CSC)
  763. status |= USB_PORT_STAT_C_CONNECTION << 16;
  764. if (raw_port_status & PORT_PEC)
  765. status |= USB_PORT_STAT_C_ENABLE << 16;
  766. if ((raw_port_status & PORT_OCC))
  767. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  768. if ((raw_port_status & PORT_RC))
  769. status |= USB_PORT_STAT_C_RESET << 16;
  770. /* USB3.0 only */
  771. if (hcd->speed >= HCD_USB3) {
  772. /* Port link change with port in resume state should not be
  773. * reported to usbcore, as this is an internal state to be
  774. * handled by xhci driver. Reporting PLC to usbcore may
  775. * cause usbcore clearing PLC first and port change event
  776. * irq won't be generated.
  777. */
  778. if ((raw_port_status & PORT_PLC) &&
  779. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME)
  780. status |= USB_PORT_STAT_C_LINK_STATE << 16;
  781. if ((raw_port_status & PORT_WRC))
  782. status |= USB_PORT_STAT_C_BH_RESET << 16;
  783. if ((raw_port_status & PORT_CEC))
  784. status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
  785. }
  786. if (hcd->speed < HCD_USB3) {
  787. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U3
  788. && (raw_port_status & PORT_POWER))
  789. status |= USB_PORT_STAT_SUSPEND;
  790. }
  791. if ((raw_port_status & PORT_PLS_MASK) == XDEV_RESUME &&
  792. !DEV_SUPERSPEED_ANY(raw_port_status)) {
  793. if ((raw_port_status & PORT_RESET) ||
  794. !(raw_port_status & PORT_PE))
  795. return 0xffffffff;
  796. /* did port event handler already start resume timing? */
  797. if (!bus_state->resume_done[wIndex]) {
  798. /* If not, maybe we are in a host initated resume? */
  799. if (test_bit(wIndex, &bus_state->resuming_ports)) {
  800. /* Host initated resume doesn't time the resume
  801. * signalling using resume_done[].
  802. * It manually sets RESUME state, sleeps 20ms
  803. * and sets U0 state. This should probably be
  804. * changed, but not right now.
  805. */
  806. } else {
  807. /* port resume was discovered now and here,
  808. * start resume timing
  809. */
  810. unsigned long timeout = jiffies +
  811. msecs_to_jiffies(USB_RESUME_TIMEOUT);
  812. set_bit(wIndex, &bus_state->resuming_ports);
  813. bus_state->resume_done[wIndex] = timeout;
  814. mod_timer(&hcd->rh_timer, timeout);
  815. }
  816. /* Has resume been signalled for USB_RESUME_TIME yet? */
  817. } else if (time_after_eq(jiffies,
  818. bus_state->resume_done[wIndex])) {
  819. int time_left;
  820. xhci_dbg(xhci, "Resume USB2 port %d\n",
  821. wIndex + 1);
  822. bus_state->resume_done[wIndex] = 0;
  823. clear_bit(wIndex, &bus_state->resuming_ports);
  824. set_bit(wIndex, &bus_state->rexit_ports);
  825. xhci_test_and_clear_bit(xhci, port_array, wIndex,
  826. PORT_PLC);
  827. xhci_set_link_state(xhci, port_array, wIndex,
  828. XDEV_U0);
  829. spin_unlock_irqrestore(&xhci->lock, flags);
  830. time_left = wait_for_completion_timeout(
  831. &bus_state->rexit_done[wIndex],
  832. msecs_to_jiffies(
  833. XHCI_MAX_REXIT_TIMEOUT));
  834. spin_lock_irqsave(&xhci->lock, flags);
  835. if (time_left) {
  836. slot_id = xhci_find_slot_id_by_port(hcd,
  837. xhci, wIndex + 1);
  838. if (!slot_id) {
  839. xhci_dbg(xhci, "slot_id is zero\n");
  840. return 0xffffffff;
  841. }
  842. xhci_ring_device(xhci, slot_id);
  843. } else {
  844. int port_status = readl(port_array[wIndex]);
  845. xhci_warn(xhci, "Port resume took longer than %i msec, port status = 0x%x\n",
  846. XHCI_MAX_REXIT_TIMEOUT,
  847. port_status);
  848. status |= USB_PORT_STAT_SUSPEND;
  849. clear_bit(wIndex, &bus_state->rexit_ports);
  850. }
  851. bus_state->port_c_suspend |= 1 << wIndex;
  852. bus_state->suspended_ports &= ~(1 << wIndex);
  853. } else {
  854. /*
  855. * The resume has been signaling for less than
  856. * USB_RESUME_TIME. Report the port status as SUSPEND,
  857. * let the usbcore check port status again and clear
  858. * resume signaling later.
  859. */
  860. status |= USB_PORT_STAT_SUSPEND;
  861. }
  862. }
  863. /*
  864. * Clear stale usb2 resume signalling variables in case port changed
  865. * state during resume signalling. For example on error
  866. */
  867. if ((bus_state->resume_done[wIndex] ||
  868. test_bit(wIndex, &bus_state->resuming_ports)) &&
  869. (raw_port_status & PORT_PLS_MASK) != XDEV_U3 &&
  870. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME) {
  871. bus_state->resume_done[wIndex] = 0;
  872. clear_bit(wIndex, &bus_state->resuming_ports);
  873. }
  874. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U0 &&
  875. (raw_port_status & PORT_POWER)) {
  876. if (bus_state->suspended_ports & (1 << wIndex)) {
  877. bus_state->suspended_ports &= ~(1 << wIndex);
  878. if (hcd->speed < HCD_USB3)
  879. bus_state->port_c_suspend |= 1 << wIndex;
  880. }
  881. bus_state->resume_done[wIndex] = 0;
  882. clear_bit(wIndex, &bus_state->resuming_ports);
  883. }
  884. if (raw_port_status & PORT_CONNECT) {
  885. status |= USB_PORT_STAT_CONNECTION;
  886. status |= xhci_port_speed(raw_port_status);
  887. }
  888. if (raw_port_status & PORT_PE)
  889. status |= USB_PORT_STAT_ENABLE;
  890. if (raw_port_status & PORT_OC)
  891. status |= USB_PORT_STAT_OVERCURRENT;
  892. if (raw_port_status & PORT_RESET)
  893. status |= USB_PORT_STAT_RESET;
  894. if (raw_port_status & PORT_POWER) {
  895. if (hcd->speed >= HCD_USB3)
  896. status |= USB_SS_PORT_STAT_POWER;
  897. else
  898. status |= USB_PORT_STAT_POWER;
  899. }
  900. /* Update Port Link State */
  901. if (hcd->speed >= HCD_USB3) {
  902. xhci_hub_report_usb3_link_state(xhci, &status, raw_port_status);
  903. /*
  904. * Verify if all USB3 Ports Have entered U0 already.
  905. * Delete Compliance Mode Timer if so.
  906. */
  907. xhci_del_comp_mod_timer(xhci, raw_port_status, wIndex);
  908. } else {
  909. xhci_hub_report_usb2_link_state(&status, raw_port_status);
  910. }
  911. if (bus_state->port_c_suspend & (1 << wIndex))
  912. status |= USB_PORT_STAT_C_SUSPEND << 16;
  913. return status;
  914. }
  915. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  916. u16 wIndex, char *buf, u16 wLength)
  917. {
  918. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  919. int max_ports;
  920. unsigned long flags;
  921. u32 temp, status;
  922. int retval = 0;
  923. __le32 __iomem **port_array;
  924. int slot_id;
  925. struct xhci_bus_state *bus_state;
  926. u16 link_state = 0;
  927. u16 wake_mask = 0;
  928. u16 timeout = 0;
  929. u16 test_mode = 0;
  930. max_ports = xhci_get_ports(hcd, &port_array);
  931. bus_state = &xhci->bus_state[hcd_index(hcd)];
  932. spin_lock_irqsave(&xhci->lock, flags);
  933. switch (typeReq) {
  934. case GetHubStatus:
  935. /* No power source, over-current reported per port */
  936. memset(buf, 0, 4);
  937. break;
  938. case GetHubDescriptor:
  939. /* Check to make sure userspace is asking for the USB 3.0 hub
  940. * descriptor for the USB 3.0 roothub. If not, we stall the
  941. * endpoint, like external hubs do.
  942. */
  943. if (hcd->speed >= HCD_USB3 &&
  944. (wLength < USB_DT_SS_HUB_SIZE ||
  945. wValue != (USB_DT_SS_HUB << 8))) {
  946. xhci_dbg(xhci, "Wrong hub descriptor type for "
  947. "USB 3.0 roothub.\n");
  948. goto error;
  949. }
  950. xhci_hub_descriptor(hcd, xhci,
  951. (struct usb_hub_descriptor *) buf);
  952. break;
  953. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  954. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  955. goto error;
  956. if (hcd->speed < HCD_USB3)
  957. goto error;
  958. retval = xhci_create_usb3_bos_desc(xhci, buf, wLength);
  959. spin_unlock_irqrestore(&xhci->lock, flags);
  960. return retval;
  961. case GetPortStatus:
  962. if (!wIndex || wIndex > max_ports)
  963. goto error;
  964. wIndex--;
  965. temp = readl(port_array[wIndex]);
  966. if (temp == ~(u32)0) {
  967. xhci_hc_died(xhci);
  968. retval = -ENODEV;
  969. break;
  970. }
  971. status = xhci_get_port_status(hcd, bus_state, port_array,
  972. wIndex, temp, flags);
  973. if (status == 0xffffffff)
  974. goto error;
  975. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n",
  976. wIndex, temp);
  977. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  978. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  979. /* if USB 3.1 extended port status return additional 4 bytes */
  980. if (wValue == 0x02) {
  981. u32 port_li;
  982. if (hcd->speed < HCD_USB31 || wLength != 8) {
  983. xhci_err(xhci, "get ext port status invalid parameter\n");
  984. retval = -EINVAL;
  985. break;
  986. }
  987. port_li = readl(port_array[wIndex] + PORTLI);
  988. status = xhci_get_ext_port_status(temp, port_li);
  989. put_unaligned_le32(cpu_to_le32(status), &buf[4]);
  990. }
  991. break;
  992. case SetPortFeature:
  993. if (wValue == USB_PORT_FEAT_LINK_STATE)
  994. link_state = (wIndex & 0xff00) >> 3;
  995. if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
  996. wake_mask = wIndex & 0xff00;
  997. if (wValue == USB_PORT_FEAT_TEST)
  998. test_mode = (wIndex & 0xff00) >> 8;
  999. /* The MSB of wIndex is the U1/U2 timeout */
  1000. timeout = (wIndex & 0xff00) >> 8;
  1001. wIndex &= 0xff;
  1002. if (!wIndex || wIndex > max_ports)
  1003. goto error;
  1004. wIndex--;
  1005. temp = readl(port_array[wIndex]);
  1006. if (temp == ~(u32)0) {
  1007. xhci_hc_died(xhci);
  1008. retval = -ENODEV;
  1009. break;
  1010. }
  1011. temp = xhci_port_state_to_neutral(temp);
  1012. /* FIXME: What new port features do we need to support? */
  1013. switch (wValue) {
  1014. case USB_PORT_FEAT_SUSPEND:
  1015. temp = readl(port_array[wIndex]);
  1016. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  1017. /* Resume the port to U0 first */
  1018. xhci_set_link_state(xhci, port_array, wIndex,
  1019. XDEV_U0);
  1020. spin_unlock_irqrestore(&xhci->lock, flags);
  1021. msleep(10);
  1022. spin_lock_irqsave(&xhci->lock, flags);
  1023. }
  1024. /* In spec software should not attempt to suspend
  1025. * a port unless the port reports that it is in the
  1026. * enabled (PED = ‘1’,PLS < ‘3’) state.
  1027. */
  1028. temp = readl(port_array[wIndex]);
  1029. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  1030. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  1031. xhci_warn(xhci, "USB core suspending device not in U0/U1/U2.\n");
  1032. goto error;
  1033. }
  1034. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1035. wIndex + 1);
  1036. if (!slot_id) {
  1037. xhci_warn(xhci, "slot_id is zero\n");
  1038. goto error;
  1039. }
  1040. /* unlock to execute stop endpoint commands */
  1041. spin_unlock_irqrestore(&xhci->lock, flags);
  1042. xhci_stop_device(xhci, slot_id, 1);
  1043. spin_lock_irqsave(&xhci->lock, flags);
  1044. xhci_set_link_state(xhci, port_array, wIndex, XDEV_U3);
  1045. spin_unlock_irqrestore(&xhci->lock, flags);
  1046. msleep(10); /* wait device to enter */
  1047. spin_lock_irqsave(&xhci->lock, flags);
  1048. temp = readl(port_array[wIndex]);
  1049. bus_state->suspended_ports |= 1 << wIndex;
  1050. break;
  1051. case USB_PORT_FEAT_LINK_STATE:
  1052. temp = readl(port_array[wIndex]);
  1053. /* Disable port */
  1054. if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
  1055. xhci_dbg(xhci, "Disable port %d\n", wIndex);
  1056. temp = xhci_port_state_to_neutral(temp);
  1057. /*
  1058. * Clear all change bits, so that we get a new
  1059. * connection event.
  1060. */
  1061. temp |= PORT_CSC | PORT_PEC | PORT_WRC |
  1062. PORT_OCC | PORT_RC | PORT_PLC |
  1063. PORT_CEC;
  1064. writel(temp | PORT_PE, port_array[wIndex]);
  1065. temp = readl(port_array[wIndex]);
  1066. break;
  1067. }
  1068. /* Put link in RxDetect (enable port) */
  1069. if (link_state == USB_SS_PORT_LS_RX_DETECT) {
  1070. xhci_dbg(xhci, "Enable port %d\n", wIndex);
  1071. xhci_set_link_state(xhci, port_array, wIndex,
  1072. link_state);
  1073. temp = readl(port_array[wIndex]);
  1074. break;
  1075. }
  1076. /*
  1077. * For xHCI 1.1 according to section 4.19.1.2.4.1 a
  1078. * root hub port's transition to compliance mode upon
  1079. * detecting LFPS timeout may be controlled by an
  1080. * Compliance Transition Enabled (CTE) flag (not
  1081. * software visible). This flag is set by writing 0xA
  1082. * to PORTSC PLS field which will allow transition to
  1083. * compliance mode the next time LFPS timeout is
  1084. * encountered. A warm reset will clear it.
  1085. *
  1086. * The CTE flag is only supported if the HCCPARAMS2 CTC
  1087. * flag is set, otherwise, the compliance substate is
  1088. * automatically entered as on 1.0 and prior.
  1089. */
  1090. if (link_state == USB_SS_PORT_LS_COMP_MOD) {
  1091. if (!HCC2_CTC(xhci->hcc_params2)) {
  1092. xhci_dbg(xhci, "CTC flag is 0, port already supports entering compliance mode\n");
  1093. break;
  1094. }
  1095. if ((temp & PORT_CONNECT)) {
  1096. xhci_warn(xhci, "Can't set compliance mode when port is connected\n");
  1097. goto error;
  1098. }
  1099. xhci_dbg(xhci, "Enable compliance mode transition for port %d\n",
  1100. wIndex);
  1101. xhci_set_link_state(xhci, port_array, wIndex,
  1102. link_state);
  1103. temp = readl(port_array[wIndex]);
  1104. break;
  1105. }
  1106. /* Software should not attempt to set
  1107. * port link state above '3' (U3) and the port
  1108. * must be enabled.
  1109. */
  1110. if ((temp & PORT_PE) == 0 ||
  1111. (link_state > USB_SS_PORT_LS_U3)) {
  1112. xhci_warn(xhci, "Cannot set link state.\n");
  1113. goto error;
  1114. }
  1115. if (link_state == USB_SS_PORT_LS_U3) {
  1116. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1117. wIndex + 1);
  1118. if (slot_id) {
  1119. /* unlock to execute stop endpoint
  1120. * commands */
  1121. spin_unlock_irqrestore(&xhci->lock,
  1122. flags);
  1123. xhci_stop_device(xhci, slot_id, 1);
  1124. spin_lock_irqsave(&xhci->lock, flags);
  1125. }
  1126. }
  1127. xhci_set_link_state(xhci, port_array, wIndex,
  1128. link_state);
  1129. spin_unlock_irqrestore(&xhci->lock, flags);
  1130. msleep(20); /* wait device to enter */
  1131. spin_lock_irqsave(&xhci->lock, flags);
  1132. temp = readl(port_array[wIndex]);
  1133. if (link_state == USB_SS_PORT_LS_U3)
  1134. bus_state->suspended_ports |= 1 << wIndex;
  1135. break;
  1136. case USB_PORT_FEAT_POWER:
  1137. /*
  1138. * Turn on ports, even if there isn't per-port switching.
  1139. * HC will report connect events even before this is set.
  1140. * However, hub_wq will ignore the roothub events until
  1141. * the roothub is registered.
  1142. */
  1143. xhci_set_port_power(xhci, hcd, wIndex, true, &flags);
  1144. break;
  1145. case USB_PORT_FEAT_RESET:
  1146. temp = (temp | PORT_RESET);
  1147. writel(temp, port_array[wIndex]);
  1148. temp = readl(port_array[wIndex]);
  1149. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  1150. break;
  1151. case USB_PORT_FEAT_REMOTE_WAKE_MASK:
  1152. xhci_set_remote_wake_mask(xhci, port_array,
  1153. wIndex, wake_mask);
  1154. temp = readl(port_array[wIndex]);
  1155. xhci_dbg(xhci, "set port remote wake mask, "
  1156. "actual port %d status = 0x%x\n",
  1157. wIndex, temp);
  1158. break;
  1159. case USB_PORT_FEAT_BH_PORT_RESET:
  1160. temp |= PORT_WR;
  1161. writel(temp, port_array[wIndex]);
  1162. temp = readl(port_array[wIndex]);
  1163. break;
  1164. case USB_PORT_FEAT_U1_TIMEOUT:
  1165. if (hcd->speed < HCD_USB3)
  1166. goto error;
  1167. temp = readl(port_array[wIndex] + PORTPMSC);
  1168. temp &= ~PORT_U1_TIMEOUT_MASK;
  1169. temp |= PORT_U1_TIMEOUT(timeout);
  1170. writel(temp, port_array[wIndex] + PORTPMSC);
  1171. break;
  1172. case USB_PORT_FEAT_U2_TIMEOUT:
  1173. if (hcd->speed < HCD_USB3)
  1174. goto error;
  1175. temp = readl(port_array[wIndex] + PORTPMSC);
  1176. temp &= ~PORT_U2_TIMEOUT_MASK;
  1177. temp |= PORT_U2_TIMEOUT(timeout);
  1178. writel(temp, port_array[wIndex] + PORTPMSC);
  1179. break;
  1180. case USB_PORT_FEAT_TEST:
  1181. /* 4.19.6 Port Test Modes (USB2 Test Mode) */
  1182. if (hcd->speed != HCD_USB2)
  1183. goto error;
  1184. if (test_mode > TEST_FORCE_EN || test_mode < TEST_J)
  1185. goto error;
  1186. retval = xhci_enter_test_mode(xhci, test_mode, wIndex,
  1187. &flags);
  1188. break;
  1189. default:
  1190. goto error;
  1191. }
  1192. /* unblock any posted writes */
  1193. temp = readl(port_array[wIndex]);
  1194. break;
  1195. case ClearPortFeature:
  1196. if (!wIndex || wIndex > max_ports)
  1197. goto error;
  1198. wIndex--;
  1199. temp = readl(port_array[wIndex]);
  1200. if (temp == ~(u32)0) {
  1201. xhci_hc_died(xhci);
  1202. retval = -ENODEV;
  1203. break;
  1204. }
  1205. /* FIXME: What new port features do we need to support? */
  1206. temp = xhci_port_state_to_neutral(temp);
  1207. switch (wValue) {
  1208. case USB_PORT_FEAT_SUSPEND:
  1209. temp = readl(port_array[wIndex]);
  1210. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  1211. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  1212. if (temp & PORT_RESET)
  1213. goto error;
  1214. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  1215. if ((temp & PORT_PE) == 0)
  1216. goto error;
  1217. set_bit(wIndex, &bus_state->resuming_ports);
  1218. xhci_set_link_state(xhci, port_array, wIndex,
  1219. XDEV_RESUME);
  1220. spin_unlock_irqrestore(&xhci->lock, flags);
  1221. msleep(USB_RESUME_TIMEOUT);
  1222. spin_lock_irqsave(&xhci->lock, flags);
  1223. xhci_set_link_state(xhci, port_array, wIndex,
  1224. XDEV_U0);
  1225. clear_bit(wIndex, &bus_state->resuming_ports);
  1226. }
  1227. bus_state->port_c_suspend |= 1 << wIndex;
  1228. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1229. wIndex + 1);
  1230. if (!slot_id) {
  1231. xhci_dbg(xhci, "slot_id is zero\n");
  1232. goto error;
  1233. }
  1234. xhci_ring_device(xhci, slot_id);
  1235. break;
  1236. case USB_PORT_FEAT_C_SUSPEND:
  1237. bus_state->port_c_suspend &= ~(1 << wIndex);
  1238. case USB_PORT_FEAT_C_RESET:
  1239. case USB_PORT_FEAT_C_BH_PORT_RESET:
  1240. case USB_PORT_FEAT_C_CONNECTION:
  1241. case USB_PORT_FEAT_C_OVER_CURRENT:
  1242. case USB_PORT_FEAT_C_ENABLE:
  1243. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  1244. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  1245. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  1246. port_array[wIndex], temp);
  1247. break;
  1248. case USB_PORT_FEAT_ENABLE:
  1249. xhci_disable_port(hcd, xhci, wIndex,
  1250. port_array[wIndex], temp);
  1251. break;
  1252. case USB_PORT_FEAT_POWER:
  1253. xhci_set_port_power(xhci, hcd, wIndex, false, &flags);
  1254. break;
  1255. case USB_PORT_FEAT_TEST:
  1256. retval = xhci_exit_test_mode(xhci);
  1257. break;
  1258. default:
  1259. goto error;
  1260. }
  1261. break;
  1262. default:
  1263. error:
  1264. /* "stall" on error */
  1265. retval = -EPIPE;
  1266. }
  1267. spin_unlock_irqrestore(&xhci->lock, flags);
  1268. return retval;
  1269. }
  1270. /*
  1271. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  1272. * Ports are 0-indexed from the HCD point of view,
  1273. * and 1-indexed from the USB core pointer of view.
  1274. *
  1275. * Note that the status change bits will be cleared as soon as a port status
  1276. * change event is generated, so we use the saved status from that event.
  1277. */
  1278. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  1279. {
  1280. unsigned long flags;
  1281. u32 temp, status;
  1282. u32 mask;
  1283. int i, retval;
  1284. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1285. int max_ports;
  1286. __le32 __iomem **port_array;
  1287. struct xhci_bus_state *bus_state;
  1288. bool reset_change = false;
  1289. max_ports = xhci_get_ports(hcd, &port_array);
  1290. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1291. /* Initial status is no changes */
  1292. retval = (max_ports + 8) / 8;
  1293. memset(buf, 0, retval);
  1294. /*
  1295. * Inform the usbcore about resume-in-progress by returning
  1296. * a non-zero value even if there are no status changes.
  1297. */
  1298. status = bus_state->resuming_ports;
  1299. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
  1300. spin_lock_irqsave(&xhci->lock, flags);
  1301. /* For each port, did anything change? If so, set that bit in buf. */
  1302. for (i = 0; i < max_ports; i++) {
  1303. temp = readl(port_array[i]);
  1304. if (temp == ~(u32)0) {
  1305. xhci_hc_died(xhci);
  1306. retval = -ENODEV;
  1307. break;
  1308. }
  1309. if ((temp & mask) != 0 ||
  1310. (bus_state->port_c_suspend & 1 << i) ||
  1311. (bus_state->resume_done[i] && time_after_eq(
  1312. jiffies, bus_state->resume_done[i]))) {
  1313. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  1314. status = 1;
  1315. }
  1316. if ((temp & PORT_RC))
  1317. reset_change = true;
  1318. }
  1319. if (!status && !reset_change) {
  1320. xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
  1321. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  1322. }
  1323. spin_unlock_irqrestore(&xhci->lock, flags);
  1324. return status ? retval : 0;
  1325. }
  1326. #ifdef CONFIG_PM
  1327. int xhci_bus_suspend(struct usb_hcd *hcd)
  1328. {
  1329. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1330. int max_ports, port_index;
  1331. __le32 __iomem **port_array;
  1332. struct xhci_bus_state *bus_state;
  1333. unsigned long flags;
  1334. max_ports = xhci_get_ports(hcd, &port_array);
  1335. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1336. spin_lock_irqsave(&xhci->lock, flags);
  1337. if (hcd->self.root_hub->do_remote_wakeup) {
  1338. if (bus_state->resuming_ports || /* USB2 */
  1339. bus_state->port_remote_wakeup) { /* USB3 */
  1340. spin_unlock_irqrestore(&xhci->lock, flags);
  1341. xhci_dbg(xhci, "suspend failed because a port is resuming\n");
  1342. return -EBUSY;
  1343. }
  1344. }
  1345. port_index = max_ports;
  1346. bus_state->bus_suspended = 0;
  1347. while (port_index--) {
  1348. /* suspend the port if the port is not suspended */
  1349. u32 t1, t2;
  1350. int slot_id;
  1351. t1 = readl(port_array[port_index]);
  1352. t2 = xhci_port_state_to_neutral(t1);
  1353. if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
  1354. xhci_dbg(xhci, "port %d not suspended\n", port_index);
  1355. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1356. port_index + 1);
  1357. if (slot_id) {
  1358. spin_unlock_irqrestore(&xhci->lock, flags);
  1359. xhci_stop_device(xhci, slot_id, 1);
  1360. spin_lock_irqsave(&xhci->lock, flags);
  1361. }
  1362. t2 &= ~PORT_PLS_MASK;
  1363. t2 |= PORT_LINK_STROBE | XDEV_U3;
  1364. set_bit(port_index, &bus_state->bus_suspended);
  1365. }
  1366. /* USB core sets remote wake mask for USB 3.0 hubs,
  1367. * including the USB 3.0 roothub, but only if CONFIG_PM
  1368. * is enabled, so also enable remote wake here.
  1369. */
  1370. if (hcd->self.root_hub->do_remote_wakeup) {
  1371. if (t1 & PORT_CONNECT) {
  1372. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  1373. t2 &= ~PORT_WKCONN_E;
  1374. } else {
  1375. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  1376. t2 &= ~PORT_WKDISC_E;
  1377. }
  1378. } else
  1379. t2 &= ~PORT_WAKE_BITS;
  1380. t1 = xhci_port_state_to_neutral(t1);
  1381. if (t1 != t2)
  1382. writel(t2, port_array[port_index]);
  1383. }
  1384. hcd->state = HC_STATE_SUSPENDED;
  1385. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  1386. spin_unlock_irqrestore(&xhci->lock, flags);
  1387. return 0;
  1388. }
  1389. /*
  1390. * Workaround for missing Cold Attach Status (CAS) if device re-plugged in S3.
  1391. * warm reset a USB3 device stuck in polling or compliance mode after resume.
  1392. * See Intel 100/c230 series PCH specification update Doc #332692-006 Errata #8
  1393. */
  1394. static bool xhci_port_missing_cas_quirk(int port_index,
  1395. __le32 __iomem **port_array)
  1396. {
  1397. u32 portsc;
  1398. portsc = readl(port_array[port_index]);
  1399. /* if any of these are set we are not stuck */
  1400. if (portsc & (PORT_CONNECT | PORT_CAS))
  1401. return false;
  1402. if (((portsc & PORT_PLS_MASK) != XDEV_POLLING) &&
  1403. ((portsc & PORT_PLS_MASK) != XDEV_COMP_MODE))
  1404. return false;
  1405. /* clear wakeup/change bits, and do a warm port reset */
  1406. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1407. portsc |= PORT_WR;
  1408. writel(portsc, port_array[port_index]);
  1409. /* flush write */
  1410. readl(port_array[port_index]);
  1411. return true;
  1412. }
  1413. int xhci_bus_resume(struct usb_hcd *hcd)
  1414. {
  1415. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1416. struct xhci_bus_state *bus_state;
  1417. __le32 __iomem **port_array;
  1418. unsigned long flags;
  1419. int max_ports, port_index;
  1420. int slot_id;
  1421. int sret;
  1422. u32 next_state;
  1423. u32 temp, portsc;
  1424. max_ports = xhci_get_ports(hcd, &port_array);
  1425. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1426. if (time_before(jiffies, bus_state->next_statechange))
  1427. msleep(5);
  1428. spin_lock_irqsave(&xhci->lock, flags);
  1429. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1430. spin_unlock_irqrestore(&xhci->lock, flags);
  1431. return -ESHUTDOWN;
  1432. }
  1433. /* delay the irqs */
  1434. temp = readl(&xhci->op_regs->command);
  1435. temp &= ~CMD_EIE;
  1436. writel(temp, &xhci->op_regs->command);
  1437. /* bus specific resume for ports we suspended at bus_suspend */
  1438. if (hcd->speed >= HCD_USB3)
  1439. next_state = XDEV_U0;
  1440. else
  1441. next_state = XDEV_RESUME;
  1442. port_index = max_ports;
  1443. while (port_index--) {
  1444. portsc = readl(port_array[port_index]);
  1445. /* warm reset CAS limited ports stuck in polling/compliance */
  1446. if ((xhci->quirks & XHCI_MISSING_CAS) &&
  1447. (hcd->speed >= HCD_USB3) &&
  1448. xhci_port_missing_cas_quirk(port_index, port_array)) {
  1449. xhci_dbg(xhci, "reset stuck port %d\n", port_index);
  1450. clear_bit(port_index, &bus_state->bus_suspended);
  1451. continue;
  1452. }
  1453. /* resume if we suspended the link, and it is still suspended */
  1454. if (test_bit(port_index, &bus_state->bus_suspended))
  1455. switch (portsc & PORT_PLS_MASK) {
  1456. case XDEV_U3:
  1457. portsc = xhci_port_state_to_neutral(portsc);
  1458. portsc &= ~PORT_PLS_MASK;
  1459. portsc |= PORT_LINK_STROBE | next_state;
  1460. break;
  1461. case XDEV_RESUME:
  1462. /* resume already initiated */
  1463. break;
  1464. default:
  1465. /* not in a resumeable state, ignore it */
  1466. clear_bit(port_index,
  1467. &bus_state->bus_suspended);
  1468. break;
  1469. }
  1470. /* disable wake for all ports, write new link state if needed */
  1471. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1472. writel(portsc, port_array[port_index]);
  1473. }
  1474. /* USB2 specific resume signaling delay and U0 link state transition */
  1475. if (hcd->speed < HCD_USB3) {
  1476. if (bus_state->bus_suspended) {
  1477. spin_unlock_irqrestore(&xhci->lock, flags);
  1478. msleep(USB_RESUME_TIMEOUT);
  1479. spin_lock_irqsave(&xhci->lock, flags);
  1480. }
  1481. for_each_set_bit(port_index, &bus_state->bus_suspended,
  1482. BITS_PER_LONG) {
  1483. /* Clear PLC to poll it later for U0 transition */
  1484. xhci_test_and_clear_bit(xhci, port_array, port_index,
  1485. PORT_PLC);
  1486. xhci_set_link_state(xhci, port_array, port_index,
  1487. XDEV_U0);
  1488. }
  1489. }
  1490. /* poll for U0 link state complete, both USB2 and USB3 */
  1491. for_each_set_bit(port_index, &bus_state->bus_suspended, BITS_PER_LONG) {
  1492. sret = xhci_handshake(port_array[port_index], PORT_PLC,
  1493. PORT_PLC, 10 * 1000);
  1494. if (sret) {
  1495. xhci_warn(xhci, "port %d resume PLC timeout\n",
  1496. port_index);
  1497. continue;
  1498. }
  1499. xhci_test_and_clear_bit(xhci, port_array, port_index, PORT_PLC);
  1500. slot_id = xhci_find_slot_id_by_port(hcd, xhci, port_index + 1);
  1501. if (slot_id)
  1502. xhci_ring_device(xhci, slot_id);
  1503. }
  1504. (void) readl(&xhci->op_regs->command);
  1505. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  1506. /* re-enable irqs */
  1507. temp = readl(&xhci->op_regs->command);
  1508. temp |= CMD_EIE;
  1509. writel(temp, &xhci->op_regs->command);
  1510. temp = readl(&xhci->op_regs->command);
  1511. spin_unlock_irqrestore(&xhci->lock, flags);
  1512. return 0;
  1513. }
  1514. #endif /* CONFIG_PM */