blob: 57e3f2c5475af396b314caaaba71eeb00193a10b [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002/*
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.
Sarah Sharp0f2a7932009-04-27 19:57:12 -07009 */
10
Mathias Nymanddba5cd2014-05-08 19:26:00 +030011
12#include <linux/slab.h>
Sarah Sharp0f2a7932009-04-27 19:57:12 -070013#include <asm/unaligned.h>
14
15#include "xhci.h"
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +030016#include "xhci-trace.h"
Sarah Sharp0f2a7932009-04-27 19:57:12 -070017
Andiry Xu9777e3c2010-10-14 07:23:03 -070018#define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
19#define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \
20 PORT_RC | PORT_PLC | PORT_PE)
21
Mathias Nyman5693e0b2015-10-01 18:40:35 +030022/* USB 3 BOS descriptor and a capability descriptors, combined.
23 * Fields will be adjusted and added later in xhci_create_usb3_bos_desc()
24 */
Sarah Sharp48e82362011-10-06 11:54:23 -070025static u8 usb_bos_descriptor [] = {
26 USB_DT_BOS_SIZE, /* __u8 bLength, 5 bytes */
27 USB_DT_BOS, /* __u8 bDescriptorType */
28 0x0F, 0x00, /* __le16 wTotalLength, 15 bytes */
29 0x1, /* __u8 bNumDeviceCaps */
Mathias Nyman5693e0b2015-10-01 18:40:35 +030030 /* First device capability, SuperSpeed */
Sarah Sharp48e82362011-10-06 11:54:23 -070031 USB_DT_USB_SS_CAP_SIZE, /* __u8 bLength, 10 bytes */
32 USB_DT_DEVICE_CAPABILITY, /* Device Capability */
33 USB_SS_CAP_TYPE, /* bDevCapabilityType, SUPERSPEED_USB */
34 0x00, /* bmAttributes, LTM off by default */
35 USB_5GBPS_OPERATION, 0x00, /* wSpeedsSupported, 5Gbps only */
36 0x03, /* bFunctionalitySupport,
37 USB 3.0 speed only */
38 0x00, /* bU1DevExitLat, set later. */
Mathias Nyman5693e0b2015-10-01 18:40:35 +030039 0x00, 0x00, /* __le16 bU2DevExitLat, set later. */
40 /* Second device capability, SuperSpeedPlus */
Mathias Nyman5da665f2016-01-25 15:30:46 +020041 0x1c, /* bLength 28, will be adjusted later */
Mathias Nyman5693e0b2015-10-01 18:40:35 +030042 USB_DT_DEVICE_CAPABILITY, /* Device Capability */
43 USB_SSP_CAP_TYPE, /* bDevCapabilityType SUPERSPEED_PLUS */
44 0x00, /* bReserved 0 */
Mathias Nyman5da665f2016-01-25 15:30:46 +020045 0x23, 0x00, 0x00, 0x00, /* bmAttributes, SSAC=3 SSIC=1 */
46 0x01, 0x00, /* wFunctionalitySupport */
Mathias Nyman5693e0b2015-10-01 18:40:35 +030047 0x00, 0x00, /* wReserved 0 */
Mathias Nyman5da665f2016-01-25 15:30:46 +020048 /* Default Sublink Speed Attributes, overwrite if custom PSI exists */
49 0x34, 0x00, 0x05, 0x00, /* 5Gbps, symmetric, rx, ID = 4 */
50 0xb4, 0x00, 0x05, 0x00, /* 5Gbps, symmetric, tx, ID = 4 */
51 0x35, 0x40, 0x0a, 0x00, /* 10Gbps, SSP, symmetric, rx, ID = 5 */
52 0xb5, 0x40, 0x0a, 0x00, /* 10Gbps, SSP, symmetric, tx, ID = 5 */
Sarah Sharp48e82362011-10-06 11:54:23 -070053};
54
Mathias Nyman5693e0b2015-10-01 18:40:35 +030055static int xhci_create_usb3_bos_desc(struct xhci_hcd *xhci, char *buf,
56 u16 wLength)
57{
58 int i, ssa_count;
59 u32 temp;
60 u16 desc_size, ssp_cap_size, ssa_size = 0;
61 bool usb3_1 = false;
62
63 desc_size = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
64 ssp_cap_size = sizeof(usb_bos_descriptor) - desc_size;
65
66 /* does xhci support USB 3.1 Enhanced SuperSpeed */
Mathias Nyman5da665f2016-01-25 15:30:46 +020067 if (xhci->usb3_rhub.min_rev >= 0x01) {
68 /* does xhci provide a PSI table for SSA speed attributes? */
69 if (xhci->usb3_rhub.psi_count) {
70 /* two SSA entries for each unique PSI ID, RX and TX */
71 ssa_count = xhci->usb3_rhub.psi_uid_count * 2;
72 ssa_size = ssa_count * sizeof(u32);
73 ssp_cap_size -= 16; /* skip copying the default SSA */
74 }
Mathias Nyman5693e0b2015-10-01 18:40:35 +030075 desc_size += ssp_cap_size;
76 usb3_1 = true;
77 }
78 memcpy(buf, &usb_bos_descriptor, min(desc_size, wLength));
79
80 if (usb3_1) {
81 /* modify bos descriptor bNumDeviceCaps and wTotalLength */
82 buf[4] += 1;
83 put_unaligned_le16(desc_size + ssa_size, &buf[2]);
84 }
85
86 if (wLength < USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE)
87 return wLength;
88
89 /* Indicate whether the host has LTM support. */
90 temp = readl(&xhci->cap_regs->hcc_params);
91 if (HCC_LTC(temp))
92 buf[8] |= USB_LTM_SUPPORT;
93
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
Mathias Nyman5da665f2016-01-25 15:30:46 +0200101 /* If PSI table exists, add the custom speed attributes from it */
102 if (usb3_1 && xhci->usb3_rhub.psi_count) {
Mathias Nyman7bea22b2017-09-18 17:39:18 +0300103 u32 ssp_cap_base, bm_attrib, psi, psi_mant, psi_exp;
Mathias Nyman5693e0b2015-10-01 18:40:35 +0300104 int offset;
105
106 ssp_cap_base = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
107
108 if (wLength < desc_size)
109 return wLength;
110 buf[ssp_cap_base] = ssp_cap_size + ssa_size;
111
112 /* attribute count SSAC bits 4:0 and ID count SSIC bits 8:5 */
113 bm_attrib = (ssa_count - 1) & 0x1f;
114 bm_attrib |= (xhci->usb3_rhub.psi_uid_count - 1) << 5;
115 put_unaligned_le32(bm_attrib, &buf[ssp_cap_base + 4]);
116
117 if (wLength < desc_size + ssa_size)
118 return wLength;
119 /*
120 * Create the Sublink Speed Attributes (SSA) array.
121 * The xhci PSI field and USB 3.1 SSA fields are very similar,
122 * but link type bits 7:6 differ for values 01b and 10b.
123 * xhci has also only one PSI entry for a symmetric link when
124 * USB 3.1 requires two SSA entries (RX and TX) for every link
125 */
126 offset = desc_size;
127 for (i = 0; i < xhci->usb3_rhub.psi_count; i++) {
128 psi = xhci->usb3_rhub.psi[i];
129 psi &= ~USB_SSP_SUBLINK_SPEED_RSVD;
Mathias Nyman7bea22b2017-09-18 17:39:18 +0300130 psi_exp = XHCI_EXT_PORT_PSIE(psi);
131 psi_mant = XHCI_EXT_PORT_PSIM(psi);
132
133 /* Shift to Gbps and set SSP Link BIT(14) if 10Gpbs */
134 for (; psi_exp < 3; psi_exp++)
135 psi_mant /= 1000;
136 if (psi_mant >= 10)
137 psi |= BIT(14);
138
Mathias Nyman5693e0b2015-10-01 18:40:35 +0300139 if ((psi & PLT_MASK) == PLT_SYM) {
140 /* Symmetric, create SSA RX and TX from one PSI entry */
141 put_unaligned_le32(psi, &buf[offset]);
142 psi |= 1 << 7; /* turn entry to TX */
143 offset += 4;
144 if (offset >= desc_size + ssa_size)
145 return desc_size + ssa_size;
146 } else if ((psi & PLT_MASK) == PLT_ASYM_RX) {
147 /* Asymetric RX, flip bits 7:6 for SSA */
148 psi ^= PLT_MASK;
149 }
150 put_unaligned_le32(psi, &buf[offset]);
151 offset += 4;
152 if (offset >= desc_size + ssa_size)
153 return desc_size + ssa_size;
154 }
155 }
156 /* ssa_size is 0 for other than usb 3.1 hosts */
157 return desc_size + ssa_size;
158}
Sarah Sharp48e82362011-10-06 11:54:23 -0700159
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -0800160static void xhci_common_hub_descriptor(struct xhci_hcd *xhci,
161 struct usb_hub_descriptor *desc, int ports)
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700162{
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700163 u16 temp;
164
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700165 desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */
166 desc->bHubContrCurrent = 0;
167
168 desc->bNbrPorts = ports;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700169 temp = 0;
Aman Deepc8421142011-11-22 19:33:36 +0530170 /* Bits 1:0 - support per-port power switching, or power always on */
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700171 if (HCC_PPC(xhci->hcc_params))
Aman Deepc8421142011-11-22 19:33:36 +0530172 temp |= HUB_CHAR_INDV_PORT_LPSM;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700173 else
Aman Deepc8421142011-11-22 19:33:36 +0530174 temp |= HUB_CHAR_NO_LPSM;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700175 /* Bit 2 - root hubs are not part of a compound device */
176 /* Bits 4:3 - individual port over current protection */
Aman Deepc8421142011-11-22 19:33:36 +0530177 temp |= HUB_CHAR_INDV_PORT_OCPM;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700178 /* Bits 6:5 - no TTs in root ports */
179 /* Bit 7 - no port indicators */
Matt Evans28ccd292011-03-29 13:40:46 +1100180 desc->wHubCharacteristics = cpu_to_le16(temp);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700181}
182
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -0800183/* Fill in the USB 2.0 roothub descriptor */
184static void xhci_usb2_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
185 struct usb_hub_descriptor *desc)
186{
187 int ports;
188 u16 temp;
189 __u8 port_removable[(USB_MAXCHILDREN + 1 + 7) / 8];
190 u32 portsc;
191 unsigned int i;
Mathias Nymane740b012018-05-21 16:39:55 +0300192 struct xhci_hub *rhub;
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -0800193
Mathias Nymane740b012018-05-21 16:39:55 +0300194 rhub = &xhci->usb2_rhub;
195 ports = rhub->num_ports;
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -0800196 xhci_common_hub_descriptor(xhci, desc, ports);
Aman Deepc8421142011-11-22 19:33:36 +0530197 desc->bDescriptorType = USB_DT_HUB;
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -0800198 temp = 1 + (ports / 8);
Aman Deepc8421142011-11-22 19:33:36 +0530199 desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp;
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -0800200
201 /* The Device Removable bits are reported on a byte granularity.
202 * If the port doesn't exist within that byte, the bit is set to 0.
203 */
204 memset(port_removable, 0, sizeof(port_removable));
205 for (i = 0; i < ports; i++) {
Mathias Nymane740b012018-05-21 16:39:55 +0300206 portsc = readl(rhub->ports[i]->addr);
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -0800207 /* If a device is removable, PORTSC reports a 0, same as in the
208 * hub descriptor DeviceRemovable bits.
209 */
210 if (portsc & PORT_DEV_REMOVE)
211 /* This math is hairy because bit 0 of DeviceRemovable
212 * is reserved, and bit 1 is for port 1, etc.
213 */
214 port_removable[(i + 1) / 8] |= 1 << ((i + 1) % 8);
215 }
216
217 /* ch11.h defines a hub descriptor that has room for USB_MAXCHILDREN
218 * ports on it. The USB 2.0 specification says that there are two
219 * variable length fields at the end of the hub descriptor:
220 * DeviceRemovable and PortPwrCtrlMask. But since we can have less than
221 * USB_MAXCHILDREN ports, we may need to use the DeviceRemovable array
222 * to set PortPwrCtrlMask bits. PortPwrCtrlMask must always be set to
223 * 0xFF, so we initialize the both arrays (DeviceRemovable and
224 * PortPwrCtrlMask) to 0xFF. Then we set the DeviceRemovable for each
225 * set of ports that actually exist.
226 */
227 memset(desc->u.hs.DeviceRemovable, 0xff,
228 sizeof(desc->u.hs.DeviceRemovable));
229 memset(desc->u.hs.PortPwrCtrlMask, 0xff,
230 sizeof(desc->u.hs.PortPwrCtrlMask));
231
232 for (i = 0; i < (ports + 1 + 7) / 8; i++)
233 memset(&desc->u.hs.DeviceRemovable[i], port_removable[i],
234 sizeof(__u8));
235}
236
237/* Fill in the USB 3.0 roothub descriptor */
238static void xhci_usb3_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
239 struct usb_hub_descriptor *desc)
240{
241 int ports;
242 u16 port_removable;
243 u32 portsc;
244 unsigned int i;
Mathias Nymane740b012018-05-21 16:39:55 +0300245 struct xhci_hub *rhub;
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -0800246
Mathias Nymane740b012018-05-21 16:39:55 +0300247 rhub = &xhci->usb3_rhub;
248 ports = rhub->num_ports;
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -0800249 xhci_common_hub_descriptor(xhci, desc, ports);
Aman Deepc8421142011-11-22 19:33:36 +0530250 desc->bDescriptorType = USB_DT_SS_HUB;
251 desc->bDescLength = USB_DT_SS_HUB_SIZE;
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -0800252
253 /* header decode latency should be zero for roothubs,
254 * see section 4.23.5.2.
255 */
256 desc->u.ss.bHubHdrDecLat = 0;
257 desc->u.ss.wHubDelay = 0;
258
259 port_removable = 0;
260 /* bit 0 is reserved, bit 1 is for port 1, etc. */
261 for (i = 0; i < ports; i++) {
Mathias Nymane740b012018-05-21 16:39:55 +0300262 portsc = readl(rhub->ports[i]->addr);
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -0800263 if (portsc & PORT_DEV_REMOVE)
264 port_removable |= 1 << (i + 1);
265 }
Lan Tianyu27c411c2012-10-15 15:38:35 +0800266
267 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -0800268}
269
270static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
271 struct usb_hub_descriptor *desc)
272{
273
Mathias Nymanb50107b2015-10-01 18:40:38 +0300274 if (hcd->speed >= HCD_USB3)
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -0800275 xhci_usb3_hub_descriptor(hcd, xhci, desc);
276 else
277 xhci_usb2_hub_descriptor(hcd, xhci, desc);
278
279}
280
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700281static unsigned int xhci_port_speed(unsigned int port_status)
282{
283 if (DEV_LOWSPEED(port_status))
Alan Stern288ead42010-03-04 11:32:30 -0500284 return USB_PORT_STAT_LOW_SPEED;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700285 if (DEV_HIGHSPEED(port_status))
Alan Stern288ead42010-03-04 11:32:30 -0500286 return USB_PORT_STAT_HIGH_SPEED;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700287 /*
288 * FIXME: Yes, we should check for full speed, but the core uses that as
289 * a default in portspeed() in usb/core/hub.c (which is the only place
Alan Stern288ead42010-03-04 11:32:30 -0500290 * USB_PORT_STAT_*_SPEED is used).
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700291 */
292 return 0;
293}
294
295/*
296 * These bits are Read Only (RO) and should be saved and written to the
297 * registers: 0, 3, 10:13, 30
298 * connect status, over-current status, port speed, and device removable.
299 * connect status and port speed are also sticky - meaning they're in
300 * the AUX well and they aren't changed by a hot, warm, or cold reset.
301 */
302#define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
303/*
304 * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
305 * bits 5:8, 9, 14:15, 25:27
306 * link state, port power, port indicator state, "wake on" enable state
307 */
308#define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
309/*
310 * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
311 * bit 4 (port reset)
312 */
313#define XHCI_PORT_RW1S ((1<<4))
314/*
315 * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
316 * bits 1, 17, 18, 19, 20, 21, 22, 23
317 * port enable/disable, and
318 * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
319 * over-current, reset, link state, and L1 change
320 */
321#define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
322/*
323 * Bit 16 is RW, and writing a '1' to it causes the link state control to be
324 * latched in
325 */
326#define XHCI_PORT_RW ((1<<16))
327/*
328 * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
329 * bits 2, 24, 28:31
330 */
331#define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
332
333/*
334 * Given a port state, this function returns a value that would result in the
335 * port being in the same state, if the value was written to the port status
336 * control register.
337 * Save Read Only (RO) bits and save read/write bits where
338 * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
339 * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
340 */
Andiry Xu56192532010-10-14 07:23:00 -0700341u32 xhci_port_state_to_neutral(u32 state)
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700342{
343 /* Save read-only status and port state */
344 return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
345}
346
Andiry Xube88fe42010-10-14 07:22:57 -0700347/*
348 * find slot id based on port number.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800349 * @port: The one-based port number from one of the two split roothubs.
Andiry Xube88fe42010-10-14 07:22:57 -0700350 */
Sarah Sharp52336302010-12-16 10:49:09 -0800351int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
352 u16 port)
Andiry Xube88fe42010-10-14 07:22:57 -0700353{
354 int slot_id;
355 int i;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800356 enum usb_device_speed speed;
Andiry Xube88fe42010-10-14 07:22:57 -0700357
358 slot_id = 0;
359 for (i = 0; i < MAX_HC_SLOTS; i++) {
Mathias Nyman22784462018-05-14 11:57:23 +0300360 if (!xhci->devs[i] || !xhci->devs[i]->udev)
Andiry Xube88fe42010-10-14 07:22:57 -0700361 continue;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800362 speed = xhci->devs[i]->udev->speed;
Mathias Nymanb50107b2015-10-01 18:40:38 +0300363 if (((speed >= USB_SPEED_SUPER) == (hcd->speed >= HCD_USB3))
Sarah Sharpfe301822011-09-02 11:05:41 -0700364 && xhci->devs[i]->fake_port == port) {
Andiry Xube88fe42010-10-14 07:22:57 -0700365 slot_id = i;
366 break;
367 }
368 }
369
370 return slot_id;
371}
372
373/*
374 * Stop device
375 * It issues stop endpoint command for EP 0 to 30. And wait the last command
376 * to complete.
377 * suspend will set to 1, if suspend bit need to set in command.
378 */
379static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
380{
381 struct xhci_virt_device *virt_dev;
382 struct xhci_command *cmd;
383 unsigned long flags;
Andiry Xube88fe42010-10-14 07:22:57 -0700384 int ret;
385 int i;
386
387 ret = 0;
388 virt_dev = xhci->devs[slot_id];
Jim Lin88716a92016-08-16 10:18:05 +0300389 if (!virt_dev)
390 return -ENODEV;
391
Felipe Balbia711ede2017-01-23 14:20:23 +0200392 trace_xhci_stop_device(virt_dev);
393
Mathias Nyman103afda2017-12-08 17:59:08 +0200394 cmd = xhci_alloc_command(xhci, true, GFP_NOIO);
Lu Baolu74e0b562017-04-07 17:57:05 +0300395 if (!cmd)
Andiry Xube88fe42010-10-14 07:22:57 -0700396 return -ENOMEM;
Andiry Xube88fe42010-10-14 07:22:57 -0700397
398 spin_lock_irqsave(&xhci->lock, flags);
399 for (i = LAST_EP_INDEX; i > 0; i--) {
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300400 if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) {
Shyam Sundar S K28a23692017-07-20 14:48:28 +0300401 struct xhci_ep_ctx *ep_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300402 struct xhci_command *command;
Shyam Sundar S K28a23692017-07-20 14:48:28 +0300403
404 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->out_ctx, i);
405
406 /* Check ep is running, required by AMD SNPS 3.1 xHC */
407 if (GET_EP_CTX_STATE(ep_ctx) != EP_STATE_RUNNING)
408 continue;
409
Mathias Nyman103afda2017-12-08 17:59:08 +0200410 command = xhci_alloc_command(xhci, false, GFP_NOWAIT);
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300411 if (!command) {
412 spin_unlock_irqrestore(&xhci->lock, flags);
Mayank Ranab3207c62017-10-06 17:45:30 +0300413 ret = -ENOMEM;
414 goto cmd_cleanup;
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300415 }
Mayank Ranab3207c62017-10-06 17:45:30 +0300416
417 ret = xhci_queue_stop_endpoint(xhci, command, slot_id,
418 i, suspend);
419 if (ret) {
420 spin_unlock_irqrestore(&xhci->lock, flags);
421 xhci_free_command(xhci, command);
422 goto cmd_cleanup;
423 }
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300424 }
Andiry Xube88fe42010-10-14 07:22:57 -0700425 }
Mayank Ranab3207c62017-10-06 17:45:30 +0300426 ret = xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
427 if (ret) {
428 spin_unlock_irqrestore(&xhci->lock, flags);
429 goto cmd_cleanup;
430 }
431
Andiry Xube88fe42010-10-14 07:22:57 -0700432 xhci_ring_cmd_db(xhci);
433 spin_unlock_irqrestore(&xhci->lock, flags);
434
435 /* Wait for last stop endpoint command to finish */
Mathias Nymanc311e392014-05-08 19:26:03 +0300436 wait_for_completion(cmd->completion);
437
Felipe Balbi0b7c1052017-01-23 14:20:06 +0200438 if (cmd->status == COMP_COMMAND_ABORTED ||
Mathias Nyman604d02a2017-05-17 18:32:05 +0300439 cmd->status == COMP_COMMAND_RING_STOPPED) {
Mathias Nymanc311e392014-05-08 19:26:03 +0300440 xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
Andiry Xube88fe42010-10-14 07:22:57 -0700441 ret = -ETIME;
Andiry Xube88fe42010-10-14 07:22:57 -0700442 }
Mayank Ranab3207c62017-10-06 17:45:30 +0300443
444cmd_cleanup:
Andiry Xube88fe42010-10-14 07:22:57 -0700445 xhci_free_command(xhci, cmd);
446 return ret;
447}
448
449/*
450 * Ring device, it rings the all doorbells unconditionally.
451 */
Andiry Xu56192532010-10-14 07:23:00 -0700452void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
Andiry Xube88fe42010-10-14 07:22:57 -0700453{
Hans de Goedeb7f96962014-08-20 16:41:56 +0300454 int i, s;
455 struct xhci_virt_ep *ep;
Andiry Xube88fe42010-10-14 07:22:57 -0700456
Hans de Goedeb7f96962014-08-20 16:41:56 +0300457 for (i = 0; i < LAST_EP_INDEX + 1; i++) {
458 ep = &xhci->devs[slot_id]->eps[i];
459
460 if (ep->ep_state & EP_HAS_STREAMS) {
461 for (s = 1; s < ep->stream_info->num_streams; s++)
462 xhci_ring_ep_doorbell(xhci, slot_id, i, s);
463 } else if (ep->ring && ep->ring->dequeue) {
Andiry Xube88fe42010-10-14 07:22:57 -0700464 xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
Hans de Goedeb7f96962014-08-20 16:41:56 +0300465 }
466 }
Andiry Xube88fe42010-10-14 07:22:57 -0700467
468 return;
469}
470
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800471static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
Matt Evans28ccd292011-03-29 13:40:46 +1100472 u16 wIndex, __le32 __iomem *addr, u32 port_status)
Sarah Sharp6219c0472009-12-09 15:59:11 -0800473{
Sarah Sharp6dd0a3a72010-11-16 15:58:52 -0800474 /* Don't allow the USB core to disable SuperSpeed ports. */
Mathias Nymanb50107b2015-10-01 18:40:38 +0300475 if (hcd->speed >= HCD_USB3) {
Sarah Sharp6dd0a3a72010-11-16 15:58:52 -0800476 xhci_dbg(xhci, "Ignoring request to disable "
477 "SuperSpeed port.\n");
478 return;
479 }
480
Felipe Balbi41135de2017-01-23 14:19:58 +0200481 if (xhci->quirks & XHCI_BROKEN_PORT_PED) {
482 xhci_dbg(xhci,
483 "Broken Port Enabled/Disabled, ignoring port disable request.\n");
484 return;
485 }
486
Sarah Sharp6219c0472009-12-09 15:59:11 -0800487 /* Write 1 to disable the port */
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200488 writel(port_status | PORT_PE, addr);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200489 port_status = readl(addr);
Mathias Nymand70d5a82019-04-26 16:23:30 +0300490 xhci_dbg(xhci, "disable port %d-%d, portsc: 0x%x\n",
491 hcd->self.busnum, wIndex + 1, port_status);
Sarah Sharp6219c0472009-12-09 15:59:11 -0800492}
493
Sarah Sharp34fb5622009-12-09 15:59:08 -0800494static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
Matt Evans28ccd292011-03-29 13:40:46 +1100495 u16 wIndex, __le32 __iomem *addr, u32 port_status)
Sarah Sharp34fb5622009-12-09 15:59:08 -0800496{
497 char *port_change_bit;
498 u32 status;
499
500 switch (wValue) {
501 case USB_PORT_FEAT_C_RESET:
502 status = PORT_RC;
503 port_change_bit = "reset";
504 break;
Andiry Xua11496e2011-04-27 18:07:29 +0800505 case USB_PORT_FEAT_C_BH_PORT_RESET:
506 status = PORT_WRC;
507 port_change_bit = "warm(BH) reset";
508 break;
Sarah Sharp34fb5622009-12-09 15:59:08 -0800509 case USB_PORT_FEAT_C_CONNECTION:
510 status = PORT_CSC;
511 port_change_bit = "connect";
512 break;
513 case USB_PORT_FEAT_C_OVER_CURRENT:
514 status = PORT_OCC;
515 port_change_bit = "over-current";
516 break;
Sarah Sharp6219c0472009-12-09 15:59:11 -0800517 case USB_PORT_FEAT_C_ENABLE:
518 status = PORT_PEC;
519 port_change_bit = "enable/disable";
520 break;
Andiry Xube88fe42010-10-14 07:22:57 -0700521 case USB_PORT_FEAT_C_SUSPEND:
522 status = PORT_PLC;
523 port_change_bit = "suspend/resume";
524 break;
Andiry Xu85387c02011-04-27 18:07:35 +0800525 case USB_PORT_FEAT_C_PORT_LINK_STATE:
526 status = PORT_PLC;
527 port_change_bit = "link state";
528 break;
Lu Baolu94251832015-03-23 18:27:41 +0200529 case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
530 status = PORT_CEC;
531 port_change_bit = "config error";
532 break;
Sarah Sharp34fb5622009-12-09 15:59:08 -0800533 default:
534 /* Should never happen */
535 return;
536 }
537 /* Change bits are all write 1 to clear */
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200538 writel(port_status | status, addr);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200539 port_status = readl(addr);
Mathias Nymand70d5a82019-04-26 16:23:30 +0300540
541 xhci_dbg(xhci, "clear port%d %s change, portsc: 0x%x\n",
542 wIndex + 1, port_change_bit, port_status);
Sarah Sharp34fb5622009-12-09 15:59:08 -0800543}
544
Mathias Nymanffd4b4f2018-05-21 16:39:54 +0300545struct xhci_hub *xhci_get_rhub(struct usb_hcd *hcd)
546{
547 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
548
549 if (hcd->speed >= HCD_USB3)
550 return &xhci->usb3_rhub;
551 return &xhci->usb2_rhub;
552}
553
Guoqing Zhanga6ff6cb2017-04-07 17:56:51 +0300554/*
555 * xhci_set_port_power() must be called with xhci->lock held.
556 * It will release and re-aquire the lock while calling ACPI
557 * method.
558 */
559static void xhci_set_port_power(struct xhci_hcd *xhci, struct usb_hcd *hcd,
Mathias Nymanec1dafe2017-04-13 14:01:04 +0300560 u16 index, bool on, unsigned long *flags)
Jules Irengedce174e02020-02-14 20:47:33 +0000561 __must_hold(&xhci->lock)
Guoqing Zhanga6ff6cb2017-04-07 17:56:51 +0300562{
Mathias Nymane740b012018-05-21 16:39:55 +0300563 struct xhci_hub *rhub;
564 struct xhci_port *port;
Guoqing Zhanga6ff6cb2017-04-07 17:56:51 +0300565 u32 temp;
Guoqing Zhanga6ff6cb2017-04-07 17:56:51 +0300566
Mathias Nymane740b012018-05-21 16:39:55 +0300567 rhub = xhci_get_rhub(hcd);
568 port = rhub->ports[index];
569 temp = readl(port->addr);
Mathias Nymand70d5a82019-04-26 16:23:30 +0300570
571 xhci_dbg(xhci, "set port power %d-%d %s, portsc: 0x%x\n",
572 hcd->self.busnum, index + 1, on ? "ON" : "OFF", temp);
573
Guoqing Zhanga6ff6cb2017-04-07 17:56:51 +0300574 temp = xhci_port_state_to_neutral(temp);
Mathias Nymand70d5a82019-04-26 16:23:30 +0300575
Guoqing Zhanga6ff6cb2017-04-07 17:56:51 +0300576 if (on) {
577 /* Power on */
Mathias Nymane740b012018-05-21 16:39:55 +0300578 writel(temp | PORT_POWER, port->addr);
Mathias Nymand70d5a82019-04-26 16:23:30 +0300579 readl(port->addr);
Guoqing Zhanga6ff6cb2017-04-07 17:56:51 +0300580 } else {
581 /* Power off */
Mathias Nymane740b012018-05-21 16:39:55 +0300582 writel(temp & ~PORT_POWER, port->addr);
Guoqing Zhanga6ff6cb2017-04-07 17:56:51 +0300583 }
584
Mathias Nymanec1dafe2017-04-13 14:01:04 +0300585 spin_unlock_irqrestore(&xhci->lock, *flags);
Guoqing Zhanga6ff6cb2017-04-07 17:56:51 +0300586 temp = usb_acpi_power_manageable(hcd->self.root_hub,
587 index);
588 if (temp)
589 usb_acpi_set_power_state(hcd->self.root_hub,
590 index, on);
Mathias Nymanec1dafe2017-04-13 14:01:04 +0300591 spin_lock_irqsave(&xhci->lock, *flags);
Guoqing Zhanga6ff6cb2017-04-07 17:56:51 +0300592}
593
Guoqing Zhang0f1d8322017-04-07 17:56:54 +0300594static void xhci_port_set_test_mode(struct xhci_hcd *xhci,
595 u16 test_mode, u16 wIndex)
596{
597 u32 temp;
Mathias Nymane740b012018-05-21 16:39:55 +0300598 struct xhci_port *port;
Guoqing Zhang0f1d8322017-04-07 17:56:54 +0300599
Mathias Nymane740b012018-05-21 16:39:55 +0300600 /* xhci only supports test mode for usb2 ports */
601 port = xhci->usb2_rhub.ports[wIndex];
602 temp = readl(port->addr + PORTPMSC);
Guoqing Zhang0f1d8322017-04-07 17:56:54 +0300603 temp |= test_mode << PORT_TEST_MODE_SHIFT;
Mathias Nymane740b012018-05-21 16:39:55 +0300604 writel(temp, port->addr + PORTPMSC);
Guoqing Zhang0f1d8322017-04-07 17:56:54 +0300605 xhci->test_mode = test_mode;
606 if (test_mode == TEST_FORCE_EN)
607 xhci_start(xhci);
608}
609
610static int xhci_enter_test_mode(struct xhci_hcd *xhci,
Mathias Nymanec1dafe2017-04-13 14:01:04 +0300611 u16 test_mode, u16 wIndex, unsigned long *flags)
Guoqing Zhang0f1d8322017-04-07 17:56:54 +0300612{
613 int i, retval;
614
615 /* Disable all Device Slots */
616 xhci_dbg(xhci, "Disable all slots\n");
Peter Chen576d5542017-07-20 14:48:30 +0300617 spin_unlock_irqrestore(&xhci->lock, *flags);
Guoqing Zhang0f1d8322017-04-07 17:56:54 +0300618 for (i = 1; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
Lu Baolub64149c2017-10-05 11:21:40 +0300619 if (!xhci->devs[i])
620 continue;
621
Lu Baolucd3f1792017-10-05 11:21:41 +0300622 retval = xhci_disable_slot(xhci, i);
Guoqing Zhang0f1d8322017-04-07 17:56:54 +0300623 if (retval)
624 xhci_err(xhci, "Failed to disable slot %d, %d. Enter test mode anyway\n",
625 i, retval);
626 }
Peter Chen576d5542017-07-20 14:48:30 +0300627 spin_lock_irqsave(&xhci->lock, *flags);
Guoqing Zhang0f1d8322017-04-07 17:56:54 +0300628 /* Put all ports to the Disable state by clear PP */
629 xhci_dbg(xhci, "Disable all port (PP = 0)\n");
630 /* Power off USB3 ports*/
Mathias Nymane740b012018-05-21 16:39:55 +0300631 for (i = 0; i < xhci->usb3_rhub.num_ports; i++)
Mathias Nymanec1dafe2017-04-13 14:01:04 +0300632 xhci_set_port_power(xhci, xhci->shared_hcd, i, false, flags);
Guoqing Zhang0f1d8322017-04-07 17:56:54 +0300633 /* Power off USB2 ports*/
Mathias Nymane740b012018-05-21 16:39:55 +0300634 for (i = 0; i < xhci->usb2_rhub.num_ports; i++)
Mathias Nymanec1dafe2017-04-13 14:01:04 +0300635 xhci_set_port_power(xhci, xhci->main_hcd, i, false, flags);
Guoqing Zhang0f1d8322017-04-07 17:56:54 +0300636 /* Stop the controller */
637 xhci_dbg(xhci, "Stop controller\n");
638 retval = xhci_halt(xhci);
639 if (retval)
640 return retval;
641 /* Disable runtime PM for test mode */
642 pm_runtime_forbid(xhci_to_hcd(xhci)->self.controller);
643 /* Set PORTPMSC.PTC field to enter selected test mode */
644 /* Port is selected by wIndex. port_id = wIndex + 1 */
645 xhci_dbg(xhci, "Enter Test Mode: %d, Port_id=%d\n",
646 test_mode, wIndex + 1);
647 xhci_port_set_test_mode(xhci, test_mode, wIndex);
648 return retval;
649}
650
651static int xhci_exit_test_mode(struct xhci_hcd *xhci)
652{
653 int retval;
654
655 if (!xhci->test_mode) {
656 xhci_err(xhci, "Not in test mode, do nothing.\n");
657 return 0;
658 }
659 if (xhci->test_mode == TEST_FORCE_EN &&
660 !(xhci->xhc_state & XHCI_STATE_HALTED)) {
661 retval = xhci_halt(xhci);
662 if (retval)
663 return retval;
664 }
665 pm_runtime_allow(xhci_to_hcd(xhci)->self.controller);
666 xhci->test_mode = 0;
667 return xhci_reset(xhci);
668}
669
Mathias Nyman6b7f40f2018-05-21 16:39:59 +0300670void xhci_set_link_state(struct xhci_hcd *xhci, struct xhci_port *port,
671 u32 link_state)
Andiry Xuc9682df2011-09-23 14:19:48 -0700672{
673 u32 temp;
Mathias Nymand70d5a82019-04-26 16:23:30 +0300674 u32 portsc;
Andiry Xuc9682df2011-09-23 14:19:48 -0700675
Mathias Nymand70d5a82019-04-26 16:23:30 +0300676 portsc = readl(port->addr);
677 temp = xhci_port_state_to_neutral(portsc);
Andiry Xuc9682df2011-09-23 14:19:48 -0700678 temp &= ~PORT_PLS_MASK;
679 temp |= PORT_LINK_STROBE | link_state;
Mathias Nyman6b7f40f2018-05-21 16:39:59 +0300680 writel(temp, port->addr);
Mathias Nymand70d5a82019-04-26 16:23:30 +0300681
682 xhci_dbg(xhci, "Set port %d-%d link state, portsc: 0x%x, write 0x%x",
683 port->rhub->hcd->self.busnum, port->hcd_portnum + 1,
684 portsc, temp);
Andiry Xuc9682df2011-09-23 14:19:48 -0700685}
686
Felipe Balbied384bd2012-08-07 14:10:03 +0300687static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
Mathias Nymanfdcf74ff2018-05-21 16:39:56 +0300688 struct xhci_port *port, u16 wake_mask)
Sarah Sharp4296c70a2012-01-06 10:34:31 -0800689{
690 u32 temp;
691
Mathias Nymanfdcf74ff2018-05-21 16:39:56 +0300692 temp = readl(port->addr);
Sarah Sharp4296c70a2012-01-06 10:34:31 -0800693 temp = xhci_port_state_to_neutral(temp);
694
695 if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
696 temp |= PORT_WKCONN_E;
697 else
698 temp &= ~PORT_WKCONN_E;
699
700 if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
701 temp |= PORT_WKDISC_E;
702 else
703 temp &= ~PORT_WKDISC_E;
704
705 if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
706 temp |= PORT_WKOC_E;
707 else
708 temp &= ~PORT_WKOC_E;
709
Mathias Nymanfdcf74ff2018-05-21 16:39:56 +0300710 writel(temp, port->addr);
Sarah Sharp4296c70a2012-01-06 10:34:31 -0800711}
712
Andiry Xud2f52c92011-09-23 14:19:49 -0700713/* Test and clear port RWC bit */
Mathias Nymaneaefcf22018-05-21 16:40:00 +0300714void xhci_test_and_clear_bit(struct xhci_hcd *xhci, struct xhci_port *port,
715 u32 port_bit)
Andiry Xud2f52c92011-09-23 14:19:49 -0700716{
717 u32 temp;
718
Mathias Nymaneaefcf22018-05-21 16:40:00 +0300719 temp = readl(port->addr);
Andiry Xud2f52c92011-09-23 14:19:49 -0700720 if (temp & port_bit) {
721 temp = xhci_port_state_to_neutral(temp);
722 temp |= port_bit;
Mathias Nymaneaefcf22018-05-21 16:40:00 +0300723 writel(temp, port->addr);
Andiry Xud2f52c92011-09-23 14:19:49 -0700724 }
725}
726
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +0200727/* Updates Link Status for super Speed port */
Felipe Balbi96908582014-08-27 16:38:04 -0500728static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
729 u32 *status, u32 status_reg)
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +0200730{
731 u32 pls = status_reg & PORT_PLS_MASK;
732
733 /* resume state is a xHCI internal state.
Zhuang Jin Can243292a2015-07-21 17:20:29 +0300734 * Do not report it to usb core, instead, pretend to be U3,
735 * thus usb core knows it's not ready for transfer
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +0200736 */
Zhuang Jin Can243292a2015-07-21 17:20:29 +0300737 if (pls == XDEV_RESUME) {
738 *status |= USB_SS_PORT_LS_U3;
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +0200739 return;
Zhuang Jin Can243292a2015-07-21 17:20:29 +0300740 }
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +0200741
742 /* When the CAS bit is set then warm reset
743 * should be performed on port
744 */
745 if (status_reg & PORT_CAS) {
746 /* The CAS bit can be set while the port is
747 * in any link state.
748 * Only roothubs have CAS bit, so we
749 * pretend to be in compliance mode
750 * unless we're already in compliance
751 * or the inactive state.
752 */
753 if (pls != USB_SS_PORT_LS_COMP_MOD &&
754 pls != USB_SS_PORT_LS_SS_INACTIVE) {
755 pls = USB_SS_PORT_LS_COMP_MOD;
756 }
757 /* Return also connection bit -
758 * hub state machine resets port
759 * when this bit is set.
760 */
761 pls |= USB_PORT_STAT_CONNECTION;
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500762 } else {
763 /*
764 * If CAS bit isn't set but the Port is already at
765 * Compliance Mode, fake a connection so the USB core
766 * notices the Compliance state and resets the port.
767 * This resolves an issue generated by the SN65LVPE502CP
768 * in which sometimes the port enters compliance mode
769 * caused by a delay on the host-device negotiation.
770 */
Felipe Balbi96908582014-08-27 16:38:04 -0500771 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
772 (pls == USB_SS_PORT_LS_COMP_MOD))
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500773 pls |= USB_PORT_STAT_CONNECTION;
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +0200774 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500775
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +0200776 /* update status field */
777 *status |= pls;
778}
779
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500780/*
781 * Function for Compliance Mode Quirk.
782 *
783 * This Function verifies if all xhc USB3 ports have entered U0, if so,
784 * the compliance mode timer is deleted. A port won't enter
785 * compliance mode if it has previously entered U0.
786 */
Sachin Kamat5f20cf12013-09-16 12:01:34 +0530787static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,
788 u16 wIndex)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500789{
Mathias Nymane740b012018-05-21 16:39:55 +0300790 u32 all_ports_seen_u0 = ((1 << xhci->usb3_rhub.num_ports) - 1);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500791 bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
792
793 if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
794 return;
795
796 if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
797 xhci->port_status_u0 |= 1 << wIndex;
798 if (xhci->port_status_u0 == all_ports_seen_u0) {
799 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300800 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
801 "All USB3 ports have entered U0 already!");
802 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
803 "Compliance Mode Recovery Timer Deleted.");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500804 }
805 }
806}
807
Mathias Nymane67ebf12018-12-07 16:19:36 +0200808static int xhci_handle_usb2_port_link_resume(struct xhci_port *port,
809 u32 *status, u32 portsc,
Mathias Nymanbd828732019-12-11 16:20:07 +0200810 unsigned long *flags)
Mathias Nymane67ebf12018-12-07 16:19:36 +0200811{
812 struct xhci_bus_state *bus_state;
813 struct xhci_hcd *xhci;
814 struct usb_hcd *hcd;
815 int slot_id;
816 u32 wIndex;
817
818 hcd = port->rhub->hcd;
819 bus_state = &port->rhub->bus_state;
820 xhci = hcd_to_xhci(hcd);
821 wIndex = port->hcd_portnum;
822
823 if ((portsc & PORT_RESET) || !(portsc & PORT_PE)) {
824 *status = 0xffffffff;
825 return -EINVAL;
826 }
827 /* did port event handler already start resume timing? */
828 if (!bus_state->resume_done[wIndex]) {
829 /* If not, maybe we are in a host initated resume? */
830 if (test_bit(wIndex, &bus_state->resuming_ports)) {
831 /* Host initated resume doesn't time the resume
832 * signalling using resume_done[].
833 * It manually sets RESUME state, sleeps 20ms
834 * and sets U0 state. This should probably be
835 * changed, but not right now.
836 */
837 } else {
838 /* port resume was discovered now and here,
839 * start resume timing
840 */
841 unsigned long timeout = jiffies +
842 msecs_to_jiffies(USB_RESUME_TIMEOUT);
843
844 set_bit(wIndex, &bus_state->resuming_ports);
845 bus_state->resume_done[wIndex] = timeout;
846 mod_timer(&hcd->rh_timer, timeout);
847 usb_hcd_start_port_resume(&hcd->self, wIndex);
848 }
849 /* Has resume been signalled for USB_RESUME_TIME yet? */
850 } else if (time_after_eq(jiffies, bus_state->resume_done[wIndex])) {
851 int time_left;
852
Mathias Nymand70d5a82019-04-26 16:23:30 +0300853 xhci_dbg(xhci, "resume USB2 port %d-%d\n",
854 hcd->self.busnum, wIndex + 1);
855
Mathias Nymane67ebf12018-12-07 16:19:36 +0200856 bus_state->resume_done[wIndex] = 0;
857 clear_bit(wIndex, &bus_state->resuming_ports);
858
859 set_bit(wIndex, &bus_state->rexit_ports);
860
861 xhci_test_and_clear_bit(xhci, port, PORT_PLC);
862 xhci_set_link_state(xhci, port, XDEV_U0);
863
Mathias Nymanbd828732019-12-11 16:20:07 +0200864 spin_unlock_irqrestore(&xhci->lock, *flags);
Mathias Nymane67ebf12018-12-07 16:19:36 +0200865 time_left = wait_for_completion_timeout(
866 &bus_state->rexit_done[wIndex],
867 msecs_to_jiffies(XHCI_MAX_REXIT_TIMEOUT_MS));
Mathias Nymanbd828732019-12-11 16:20:07 +0200868 spin_lock_irqsave(&xhci->lock, *flags);
Mathias Nymane67ebf12018-12-07 16:19:36 +0200869
870 if (time_left) {
871 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
872 wIndex + 1);
873 if (!slot_id) {
874 xhci_dbg(xhci, "slot_id is zero\n");
875 *status = 0xffffffff;
876 return -ENODEV;
877 }
878 xhci_ring_device(xhci, slot_id);
879 } else {
880 int port_status = readl(port->addr);
881
Mathias Nymand70d5a82019-04-26 16:23:30 +0300882 xhci_warn(xhci, "Port resume timed out, port %d-%d: 0x%x\n",
883 hcd->self.busnum, wIndex + 1, port_status);
Mathias Nymane67ebf12018-12-07 16:19:36 +0200884 *status |= USB_PORT_STAT_SUSPEND;
885 clear_bit(wIndex, &bus_state->rexit_ports);
886 }
887
888 usb_hcd_end_port_resume(&hcd->self, wIndex);
889 bus_state->port_c_suspend |= 1 << wIndex;
890 bus_state->suspended_ports &= ~(1 << wIndex);
891 } else {
892 /*
893 * The resume has been signaling for less than
894 * USB_RESUME_TIME. Report the port status as SUSPEND,
895 * let the usbcore check port status again and clear
896 * resume signaling later.
897 */
898 *status |= USB_PORT_STAT_SUSPEND;
899 }
900 return 0;
901}
902
Mathias Nyman395f5402015-10-01 18:40:39 +0300903static u32 xhci_get_ext_port_status(u32 raw_port_status, u32 port_li)
904{
905 u32 ext_stat = 0;
906 int speed_id;
907
908 /* only support rx and tx lane counts of 1 in usb3.1 spec */
909 speed_id = DEV_PORT_SPEED(raw_port_status);
910 ext_stat |= speed_id; /* bits 3:0, RX speed id */
911 ext_stat |= speed_id << 4; /* bits 7:4, TX speed id */
912
913 ext_stat |= PORT_RX_LANES(port_li) << 8; /* bits 11:8 Rx lane count */
914 ext_stat |= PORT_TX_LANES(port_li) << 12; /* bits 15:12 Tx lane count */
915
916 return ext_stat;
917}
918
Mathias Nyman5f78a542018-12-07 16:19:32 +0200919static void xhci_get_usb3_port_status(struct xhci_port *port, u32 *status,
920 u32 portsc)
921{
Mathias Nymana231ec42018-12-07 16:19:35 +0200922 struct xhci_bus_state *bus_state;
Mathias Nyman5f78a542018-12-07 16:19:32 +0200923 struct xhci_hcd *xhci;
Mathias Nyman057d4762019-12-11 16:20:03 +0200924 struct usb_hcd *hcd;
Mathias Nyman5f78a542018-12-07 16:19:32 +0200925 u32 link_state;
926 u32 portnum;
927
Mathias Nymana231ec42018-12-07 16:19:35 +0200928 bus_state = &port->rhub->bus_state;
Mathias Nyman5f78a542018-12-07 16:19:32 +0200929 xhci = hcd_to_xhci(port->rhub->hcd);
Mathias Nyman057d4762019-12-11 16:20:03 +0200930 hcd = port->rhub->hcd;
Mathias Nyman5f78a542018-12-07 16:19:32 +0200931 link_state = portsc & PORT_PLS_MASK;
932 portnum = port->hcd_portnum;
933
934 /* USB3 specific wPortChange bits
935 *
936 * Port link change with port in resume state should not be
937 * reported to usbcore, as this is an internal state to be
938 * handled by xhci driver. Reporting PLC to usbcore may
939 * cause usbcore clearing PLC first and port change event
940 * irq won't be generated.
941 */
942
943 if (portsc & PORT_PLC && (link_state != XDEV_RESUME))
944 *status |= USB_PORT_STAT_C_LINK_STATE << 16;
945 if (portsc & PORT_WRC)
946 *status |= USB_PORT_STAT_C_BH_RESET << 16;
947 if (portsc & PORT_CEC)
948 *status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
949
950 /* USB3 specific wPortStatus bits */
Mathias Nymana231ec42018-12-07 16:19:35 +0200951 if (portsc & PORT_POWER) {
Mathias Nyman5f78a542018-12-07 16:19:32 +0200952 *status |= USB_SS_PORT_STAT_POWER;
Mathias Nymana231ec42018-12-07 16:19:35 +0200953 /* link state handling */
954 if (link_state == XDEV_U0)
955 bus_state->suspended_ports &= ~(1 << portnum);
956 }
Mathias Nyman5f78a542018-12-07 16:19:32 +0200957
Mathias Nyman057d4762019-12-11 16:20:03 +0200958 /* remote wake resume signaling complete */
959 if (bus_state->port_remote_wakeup & (1 << portnum) &&
960 link_state != XDEV_RESUME &&
961 link_state != XDEV_RECOVERY) {
962 bus_state->port_remote_wakeup &= ~(1 << portnum);
963 usb_hcd_end_port_resume(&hcd->self, portnum);
964 }
965
Mathias Nyman5f78a542018-12-07 16:19:32 +0200966 xhci_hub_report_usb3_link_state(xhci, status, portsc);
967 xhci_del_comp_mod_timer(xhci, portsc, portnum);
968}
969
Mathias Nyman70e9b532018-12-07 16:19:33 +0200970static void xhci_get_usb2_port_status(struct xhci_port *port, u32 *status,
Mathias Nymanbd828732019-12-11 16:20:07 +0200971 u32 portsc, unsigned long *flags)
Mathias Nyman70e9b532018-12-07 16:19:33 +0200972{
Mathias Nymana231ec42018-12-07 16:19:35 +0200973 struct xhci_bus_state *bus_state;
Mathias Nyman70e9b532018-12-07 16:19:33 +0200974 u32 link_state;
Mathias Nymana231ec42018-12-07 16:19:35 +0200975 u32 portnum;
Mathias Nymane67ebf12018-12-07 16:19:36 +0200976 int ret;
Mathias Nyman70e9b532018-12-07 16:19:33 +0200977
Mathias Nymana231ec42018-12-07 16:19:35 +0200978 bus_state = &port->rhub->bus_state;
Mathias Nyman70e9b532018-12-07 16:19:33 +0200979 link_state = portsc & PORT_PLS_MASK;
Mathias Nymana231ec42018-12-07 16:19:35 +0200980 portnum = port->hcd_portnum;
Mathias Nyman70e9b532018-12-07 16:19:33 +0200981
982 /* USB2 wPortStatus bits */
983 if (portsc & PORT_POWER) {
984 *status |= USB_PORT_STAT_POWER;
985
986 /* link state is only valid if port is powered */
987 if (link_state == XDEV_U3)
988 *status |= USB_PORT_STAT_SUSPEND;
989 if (link_state == XDEV_U2)
990 *status |= USB_PORT_STAT_L1;
Mathias Nymana231ec42018-12-07 16:19:35 +0200991 if (link_state == XDEV_U0) {
992 bus_state->resume_done[portnum] = 0;
993 clear_bit(portnum, &bus_state->resuming_ports);
994 if (bus_state->suspended_ports & (1 << portnum)) {
995 bus_state->suspended_ports &= ~(1 << portnum);
996 bus_state->port_c_suspend |= 1 << portnum;
997 }
998 }
Mathias Nymane67ebf12018-12-07 16:19:36 +0200999 if (link_state == XDEV_RESUME) {
1000 ret = xhci_handle_usb2_port_link_resume(port, status,
1001 portsc, flags);
1002 if (ret)
1003 return;
1004 }
Mathias Nyman70e9b532018-12-07 16:19:33 +02001005 }
1006}
1007
Sarah Sharpeae5b172013-04-02 08:42:20 -07001008/*
1009 * Converts a raw xHCI port status into the format that external USB 2.0 or USB
1010 * 3.0 hubs use.
1011 *
1012 * Possible side effects:
1013 * - Mark a port as being done with device resume,
1014 * and ring the endpoint doorbells.
1015 * - Stop the Synopsys redriver Compliance Mode polling.
Sarah Sharp8b3d4572013-08-20 08:12:12 -07001016 * - Drop and reacquire the xHCI lock, in order to wait for port resume.
Sarah Sharpeae5b172013-04-02 08:42:20 -07001017 */
1018static u32 xhci_get_port_status(struct usb_hcd *hcd,
1019 struct xhci_bus_state *bus_state,
Mathias Nymaneaefcf22018-05-21 16:40:00 +03001020 u16 wIndex, u32 raw_port_status,
Mathias Nymanbd828732019-12-11 16:20:07 +02001021 unsigned long *flags)
Sarah Sharp8b3d4572013-08-20 08:12:12 -07001022 __releases(&xhci->lock)
1023 __acquires(&xhci->lock)
Sarah Sharpeae5b172013-04-02 08:42:20 -07001024{
Sarah Sharpeae5b172013-04-02 08:42:20 -07001025 u32 status = 0;
Mathias Nymane740b012018-05-21 16:39:55 +03001026 struct xhci_hub *rhub;
1027 struct xhci_port *port;
1028
1029 rhub = xhci_get_rhub(hcd);
1030 port = rhub->ports[wIndex];
Sarah Sharpeae5b172013-04-02 08:42:20 -07001031
Mathias Nyman3c2ddb42018-12-07 16:19:34 +02001032 /* common wPortChange bits */
Sarah Sharpeae5b172013-04-02 08:42:20 -07001033 if (raw_port_status & PORT_CSC)
1034 status |= USB_PORT_STAT_C_CONNECTION << 16;
1035 if (raw_port_status & PORT_PEC)
1036 status |= USB_PORT_STAT_C_ENABLE << 16;
1037 if ((raw_port_status & PORT_OCC))
1038 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1039 if ((raw_port_status & PORT_RC))
1040 status |= USB_PORT_STAT_C_RESET << 16;
Mathias Nyman70e9b532018-12-07 16:19:33 +02001041
Mathias Nyman3c2ddb42018-12-07 16:19:34 +02001042 /* common wPortStatus bits */
1043 if (raw_port_status & PORT_CONNECT) {
1044 status |= USB_PORT_STAT_CONNECTION;
1045 status |= xhci_port_speed(raw_port_status);
1046 }
1047 if (raw_port_status & PORT_PE)
1048 status |= USB_PORT_STAT_ENABLE;
1049 if (raw_port_status & PORT_OC)
1050 status |= USB_PORT_STAT_OVERCURRENT;
1051 if (raw_port_status & PORT_RESET)
1052 status |= USB_PORT_STAT_RESET;
1053
1054 /* USB2 and USB3 specific bits, including Port Link State */
Mathias Nyman5f78a542018-12-07 16:19:32 +02001055 if (hcd->speed >= HCD_USB3)
1056 xhci_get_usb3_port_status(port, &status, raw_port_status);
Mathias Nyman70e9b532018-12-07 16:19:33 +02001057 else
Mathias Nymane67ebf12018-12-07 16:19:36 +02001058 xhci_get_usb2_port_status(port, &status, raw_port_status,
1059 flags);
Mathias Nymanf69115f2015-12-11 14:38:06 +02001060 /*
1061 * Clear stale usb2 resume signalling variables in case port changed
1062 * state during resume signalling. For example on error
1063 */
1064 if ((bus_state->resume_done[wIndex] ||
1065 test_bit(wIndex, &bus_state->resuming_ports)) &&
1066 (raw_port_status & PORT_PLS_MASK) != XDEV_U3 &&
1067 (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME) {
1068 bus_state->resume_done[wIndex] = 0;
1069 clear_bit(wIndex, &bus_state->resuming_ports);
Anshuman Gupta330e2d62018-09-20 19:13:40 +03001070 usb_hcd_end_port_resume(&hcd->self, wIndex);
Mathias Nymanf69115f2015-12-11 14:38:06 +02001071 }
1072
Sarah Sharpeae5b172013-04-02 08:42:20 -07001073 if (bus_state->port_c_suspend & (1 << wIndex))
Mathias Nyman5e6389f2015-11-24 13:09:46 +02001074 status |= USB_PORT_STAT_C_SUSPEND << 16;
Sarah Sharpeae5b172013-04-02 08:42:20 -07001075
1076 return status;
1077}
1078
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001079int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1080 u16 wIndex, char *buf, u16 wLength)
1081{
1082 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
huajun lia0885922011-05-03 21:11:00 +08001083 int max_ports;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001084 unsigned long flags;
Andiry Xuc9682df2011-09-23 14:19:48 -07001085 u32 temp, status;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001086 int retval = 0;
Andiry Xube88fe42010-10-14 07:22:57 -07001087 int slot_id;
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001088 struct xhci_bus_state *bus_state;
Andiry Xu2c441782011-04-27 18:07:39 +08001089 u16 link_state = 0;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08001090 u16 wake_mask = 0;
Sarah Sharp797b0ca2011-11-10 16:02:13 -08001091 u16 timeout = 0;
Guoqing Zhang0f1d8322017-04-07 17:56:54 +03001092 u16 test_mode = 0;
Mathias Nymane740b012018-05-21 16:39:55 +03001093 struct xhci_hub *rhub;
1094 struct xhci_port **ports;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001095
Mathias Nymane740b012018-05-21 16:39:55 +03001096 rhub = xhci_get_rhub(hcd);
1097 ports = rhub->ports;
Mathias Nyman925f3492018-05-21 16:40:02 +03001098 max_ports = rhub->num_ports;
Mathias Nymanf6187f42018-12-07 16:19:30 +02001099 bus_state = &rhub->bus_state;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001100
1101 spin_lock_irqsave(&xhci->lock, flags);
1102 switch (typeReq) {
1103 case GetHubStatus:
1104 /* No power source, over-current reported per port */
1105 memset(buf, 0, 4);
1106 break;
1107 case GetHubDescriptor:
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -08001108 /* Check to make sure userspace is asking for the USB 3.0 hub
1109 * descriptor for the USB 3.0 roothub. If not, we stall the
1110 * endpoint, like external hubs do.
1111 */
Mathias Nymanb50107b2015-10-01 18:40:38 +03001112 if (hcd->speed >= HCD_USB3 &&
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -08001113 (wLength < USB_DT_SS_HUB_SIZE ||
1114 wValue != (USB_DT_SS_HUB << 8))) {
1115 xhci_dbg(xhci, "Wrong hub descriptor type for "
1116 "USB 3.0 roothub.\n");
1117 goto error;
1118 }
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001119 xhci_hub_descriptor(hcd, xhci,
1120 (struct usb_hub_descriptor *) buf);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001121 break;
Sarah Sharp48e82362011-10-06 11:54:23 -07001122 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
1123 if ((wValue & 0xff00) != (USB_DT_BOS << 8))
1124 goto error;
1125
Mathias Nyman5693e0b2015-10-01 18:40:35 +03001126 if (hcd->speed < HCD_USB3)
Sarah Sharp48e82362011-10-06 11:54:23 -07001127 goto error;
1128
Mathias Nyman5693e0b2015-10-01 18:40:35 +03001129 retval = xhci_create_usb3_bos_desc(xhci, buf, wLength);
Sarah Sharp48e82362011-10-06 11:54:23 -07001130 spin_unlock_irqrestore(&xhci->lock, flags);
Mathias Nyman5693e0b2015-10-01 18:40:35 +03001131 return retval;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001132 case GetPortStatus:
huajun lia0885922011-05-03 21:11:00 +08001133 if (!wIndex || wIndex > max_ports)
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001134 goto error;
1135 wIndex--;
Mathias Nymane740b012018-05-21 16:39:55 +03001136 temp = readl(ports[wIndex]->addr);
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001137 if (temp == ~(u32)0) {
1138 xhci_hc_died(xhci);
Sarah Sharpf9de8152010-10-29 14:37:23 -07001139 retval = -ENODEV;
1140 break;
1141 }
Mathias Nyman28c06e52017-12-08 17:59:14 +02001142 trace_xhci_get_port_status(wIndex, temp);
Mathias Nymaneaefcf22018-05-21 16:40:00 +03001143 status = xhci_get_port_status(hcd, bus_state, wIndex, temp,
Mathias Nymanbd828732019-12-11 16:20:07 +02001144 &flags);
Sarah Sharpeae5b172013-04-02 08:42:20 -07001145 if (status == 0xffffffff)
1146 goto error;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001147
Mathias Nymand70d5a82019-04-26 16:23:30 +03001148 xhci_dbg(xhci, "Get port status %d-%d read: 0x%x, return 0x%x",
1149 hcd->self.busnum, wIndex + 1, temp, status);
Sarah Sharpeae5b172013-04-02 08:42:20 -07001150
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001151 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
Mathias Nyman395f5402015-10-01 18:40:39 +03001152 /* if USB 3.1 extended port status return additional 4 bytes */
1153 if (wValue == 0x02) {
1154 u32 port_li;
1155
1156 if (hcd->speed < HCD_USB31 || wLength != 8) {
1157 xhci_err(xhci, "get ext port status invalid parameter\n");
1158 retval = -EINVAL;
1159 break;
1160 }
Mathias Nymane740b012018-05-21 16:39:55 +03001161 port_li = readl(ports[wIndex]->addr + PORTLI);
Mathias Nyman395f5402015-10-01 18:40:39 +03001162 status = xhci_get_ext_port_status(temp, port_li);
Ruslan Bilovol6269e4c2019-07-07 15:17:19 +03001163 put_unaligned_le32(status, &buf[4]);
Mathias Nyman395f5402015-10-01 18:40:39 +03001164 }
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001165 break;
1166 case SetPortFeature:
Andiry Xu2c441782011-04-27 18:07:39 +08001167 if (wValue == USB_PORT_FEAT_LINK_STATE)
1168 link_state = (wIndex & 0xff00) >> 3;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08001169 if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
1170 wake_mask = wIndex & 0xff00;
Guoqing Zhang0f1d8322017-04-07 17:56:54 +03001171 if (wValue == USB_PORT_FEAT_TEST)
1172 test_mode = (wIndex & 0xff00) >> 8;
Sarah Sharp797b0ca2011-11-10 16:02:13 -08001173 /* The MSB of wIndex is the U1/U2 timeout */
1174 timeout = (wIndex & 0xff00) >> 8;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001175 wIndex &= 0xff;
huajun lia0885922011-05-03 21:11:00 +08001176 if (!wIndex || wIndex > max_ports)
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001177 goto error;
1178 wIndex--;
Mathias Nymane740b012018-05-21 16:39:55 +03001179 temp = readl(ports[wIndex]->addr);
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001180 if (temp == ~(u32)0) {
1181 xhci_hc_died(xhci);
Sarah Sharpf9de8152010-10-29 14:37:23 -07001182 retval = -ENODEV;
1183 break;
1184 }
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001185 temp = xhci_port_state_to_neutral(temp);
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -08001186 /* FIXME: What new port features do we need to support? */
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001187 switch (wValue) {
Andiry Xube88fe42010-10-14 07:22:57 -07001188 case USB_PORT_FEAT_SUSPEND:
Mathias Nymane740b012018-05-21 16:39:55 +03001189 temp = readl(ports[wIndex]->addr);
Andiry Xu65580b432011-09-23 14:19:52 -07001190 if ((temp & PORT_PLS_MASK) != XDEV_U0) {
1191 /* Resume the port to U0 first */
Mathias Nyman6b7f40f2018-05-21 16:39:59 +03001192 xhci_set_link_state(xhci, ports[wIndex],
Andiry Xu65580b432011-09-23 14:19:52 -07001193 XDEV_U0);
1194 spin_unlock_irqrestore(&xhci->lock, flags);
1195 msleep(10);
1196 spin_lock_irqsave(&xhci->lock, flags);
1197 }
Andiry Xube88fe42010-10-14 07:22:57 -07001198 /* In spec software should not attempt to suspend
1199 * a port unless the port reports that it is in the
1200 * enabled (PED = ‘1’,PLS < ‘3’) state.
1201 */
Mathias Nymane740b012018-05-21 16:39:55 +03001202 temp = readl(ports[wIndex]->addr);
Andiry Xube88fe42010-10-14 07:22:57 -07001203 if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
1204 || (temp & PORT_PLS_MASK) >= XDEV_U3) {
Mathias Nymand70d5a82019-04-26 16:23:30 +03001205 xhci_warn(xhci, "USB core suspending port %d-%d not in U0/U1/U2\n",
1206 hcd->self.busnum, wIndex + 1);
Andiry Xube88fe42010-10-14 07:22:57 -07001207 goto error;
1208 }
1209
Sarah Sharp52336302010-12-16 10:49:09 -08001210 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1211 wIndex + 1);
Andiry Xube88fe42010-10-14 07:22:57 -07001212 if (!slot_id) {
1213 xhci_warn(xhci, "slot_id is zero\n");
1214 goto error;
1215 }
1216 /* unlock to execute stop endpoint commands */
1217 spin_unlock_irqrestore(&xhci->lock, flags);
1218 xhci_stop_device(xhci, slot_id, 1);
1219 spin_lock_irqsave(&xhci->lock, flags);
1220
Mathias Nyman6b7f40f2018-05-21 16:39:59 +03001221 xhci_set_link_state(xhci, ports[wIndex], XDEV_U3);
Andiry Xube88fe42010-10-14 07:22:57 -07001222
1223 spin_unlock_irqrestore(&xhci->lock, flags);
1224 msleep(10); /* wait device to enter */
1225 spin_lock_irqsave(&xhci->lock, flags);
1226
Mathias Nymane740b012018-05-21 16:39:55 +03001227 temp = readl(ports[wIndex]->addr);
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001228 bus_state->suspended_ports |= 1 << wIndex;
Andiry Xube88fe42010-10-14 07:22:57 -07001229 break;
Andiry Xu2c441782011-04-27 18:07:39 +08001230 case USB_PORT_FEAT_LINK_STATE:
Mathias Nymane740b012018-05-21 16:39:55 +03001231 temp = readl(ports[wIndex]->addr);
Sarah Sharp41e7e052012-11-14 16:42:32 -08001232 /* Disable port */
1233 if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
1234 xhci_dbg(xhci, "Disable port %d\n", wIndex);
1235 temp = xhci_port_state_to_neutral(temp);
1236 /*
1237 * Clear all change bits, so that we get a new
1238 * connection event.
1239 */
1240 temp |= PORT_CSC | PORT_PEC | PORT_WRC |
1241 PORT_OCC | PORT_RC | PORT_PLC |
1242 PORT_CEC;
Mathias Nymane740b012018-05-21 16:39:55 +03001243 writel(temp | PORT_PE, ports[wIndex]->addr);
1244 temp = readl(ports[wIndex]->addr);
Sarah Sharp41e7e052012-11-14 16:42:32 -08001245 break;
1246 }
1247
1248 /* Put link in RxDetect (enable port) */
1249 if (link_state == USB_SS_PORT_LS_RX_DETECT) {
1250 xhci_dbg(xhci, "Enable port %d\n", wIndex);
Mathias Nyman6b7f40f2018-05-21 16:39:59 +03001251 xhci_set_link_state(xhci, ports[wIndex],
1252 link_state);
Mathias Nymane740b012018-05-21 16:39:55 +03001253 temp = readl(ports[wIndex]->addr);
Sarah Sharp41e7e052012-11-14 16:42:32 -08001254 break;
1255 }
1256
Jack Pham4b562bd2017-08-23 00:35:29 -07001257 /*
1258 * For xHCI 1.1 according to section 4.19.1.2.4.1 a
1259 * root hub port's transition to compliance mode upon
1260 * detecting LFPS timeout may be controlled by an
1261 * Compliance Transition Enabled (CTE) flag (not
1262 * software visible). This flag is set by writing 0xA
1263 * to PORTSC PLS field which will allow transition to
1264 * compliance mode the next time LFPS timeout is
1265 * encountered. A warm reset will clear it.
1266 *
1267 * The CTE flag is only supported if the HCCPARAMS2 CTC
1268 * flag is set, otherwise, the compliance substate is
1269 * automatically entered as on 1.0 and prior.
1270 */
1271 if (link_state == USB_SS_PORT_LS_COMP_MOD) {
1272 if (!HCC2_CTC(xhci->hcc_params2)) {
1273 xhci_dbg(xhci, "CTC flag is 0, port already supports entering compliance mode\n");
1274 break;
1275 }
1276
1277 if ((temp & PORT_CONNECT)) {
1278 xhci_warn(xhci, "Can't set compliance mode when port is connected\n");
1279 goto error;
1280 }
1281
1282 xhci_dbg(xhci, "Enable compliance mode transition for port %d\n",
1283 wIndex);
Mathias Nyman6b7f40f2018-05-21 16:39:59 +03001284 xhci_set_link_state(xhci, ports[wIndex],
Jack Pham4b562bd2017-08-23 00:35:29 -07001285 link_state);
Mathias Nyman6b7f40f2018-05-21 16:39:59 +03001286
Mathias Nymane740b012018-05-21 16:39:55 +03001287 temp = readl(ports[wIndex]->addr);
Jack Pham4b562bd2017-08-23 00:35:29 -07001288 break;
1289 }
Mathias Nyman1208d8a2018-02-12 14:24:47 +02001290 /* Port must be enabled */
1291 if (!(temp & PORT_PE)) {
1292 retval = -ENODEV;
1293 break;
1294 }
1295 /* Can't set port link state above '3' (U3) */
1296 if (link_state > USB_SS_PORT_LS_U3) {
1297 xhci_warn(xhci, "Cannot set port %d link state %d\n",
1298 wIndex, link_state);
Andiry Xu2c441782011-04-27 18:07:39 +08001299 goto error;
1300 }
Andiry Xu2c441782011-04-27 18:07:39 +08001301 if (link_state == USB_SS_PORT_LS_U3) {
1302 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1303 wIndex + 1);
1304 if (slot_id) {
1305 /* unlock to execute stop endpoint
1306 * commands */
1307 spin_unlock_irqrestore(&xhci->lock,
1308 flags);
1309 xhci_stop_device(xhci, slot_id, 1);
1310 spin_lock_irqsave(&xhci->lock, flags);
1311 }
1312 }
1313
Mathias Nyman6b7f40f2018-05-21 16:39:59 +03001314 xhci_set_link_state(xhci, ports[wIndex], link_state);
Andiry Xu2c441782011-04-27 18:07:39 +08001315
1316 spin_unlock_irqrestore(&xhci->lock, flags);
1317 msleep(20); /* wait device to enter */
1318 spin_lock_irqsave(&xhci->lock, flags);
1319
Mathias Nymane740b012018-05-21 16:39:55 +03001320 temp = readl(ports[wIndex]->addr);
Andiry Xu2c441782011-04-27 18:07:39 +08001321 if (link_state == USB_SS_PORT_LS_U3)
1322 bus_state->suspended_ports |= 1 << wIndex;
1323 break;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001324 case USB_PORT_FEAT_POWER:
1325 /*
1326 * Turn on ports, even if there isn't per-port switching.
1327 * HC will report connect events even before this is set.
Petr Mladek37ebb542014-09-19 17:32:23 +02001328 * However, hub_wq will ignore the roothub events until
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001329 * the roothub is registered.
1330 */
Mathias Nymanec1dafe2017-04-13 14:01:04 +03001331 xhci_set_port_power(xhci, hcd, wIndex, true, &flags);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001332 break;
1333 case USB_PORT_FEAT_RESET:
1334 temp = (temp | PORT_RESET);
Mathias Nymane740b012018-05-21 16:39:55 +03001335 writel(temp, ports[wIndex]->addr);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001336
Mathias Nymane740b012018-05-21 16:39:55 +03001337 temp = readl(ports[wIndex]->addr);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001338 xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
1339 break;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08001340 case USB_PORT_FEAT_REMOTE_WAKE_MASK:
Mathias Nymanfdcf74ff2018-05-21 16:39:56 +03001341 xhci_set_remote_wake_mask(xhci, ports[wIndex],
1342 wake_mask);
Mathias Nymane740b012018-05-21 16:39:55 +03001343 temp = readl(ports[wIndex]->addr);
Sarah Sharp4296c70a2012-01-06 10:34:31 -08001344 xhci_dbg(xhci, "set port remote wake mask, "
1345 "actual port %d status = 0x%x\n",
1346 wIndex, temp);
1347 break;
Andiry Xua11496e2011-04-27 18:07:29 +08001348 case USB_PORT_FEAT_BH_PORT_RESET:
1349 temp |= PORT_WR;
Mathias Nymane740b012018-05-21 16:39:55 +03001350 writel(temp, ports[wIndex]->addr);
1351 temp = readl(ports[wIndex]->addr);
Andiry Xua11496e2011-04-27 18:07:29 +08001352 break;
Sarah Sharp797b0ca2011-11-10 16:02:13 -08001353 case USB_PORT_FEAT_U1_TIMEOUT:
Mathias Nymanb50107b2015-10-01 18:40:38 +03001354 if (hcd->speed < HCD_USB3)
Sarah Sharp797b0ca2011-11-10 16:02:13 -08001355 goto error;
Mathias Nymane740b012018-05-21 16:39:55 +03001356 temp = readl(ports[wIndex]->addr + PORTPMSC);
Sarah Sharp797b0ca2011-11-10 16:02:13 -08001357 temp &= ~PORT_U1_TIMEOUT_MASK;
1358 temp |= PORT_U1_TIMEOUT(timeout);
Mathias Nymane740b012018-05-21 16:39:55 +03001359 writel(temp, ports[wIndex]->addr + PORTPMSC);
Sarah Sharp797b0ca2011-11-10 16:02:13 -08001360 break;
1361 case USB_PORT_FEAT_U2_TIMEOUT:
Mathias Nymanb50107b2015-10-01 18:40:38 +03001362 if (hcd->speed < HCD_USB3)
Sarah Sharp797b0ca2011-11-10 16:02:13 -08001363 goto error;
Mathias Nymane740b012018-05-21 16:39:55 +03001364 temp = readl(ports[wIndex]->addr + PORTPMSC);
Sarah Sharp797b0ca2011-11-10 16:02:13 -08001365 temp &= ~PORT_U2_TIMEOUT_MASK;
1366 temp |= PORT_U2_TIMEOUT(timeout);
Mathias Nymane740b012018-05-21 16:39:55 +03001367 writel(temp, ports[wIndex]->addr + PORTPMSC);
Sarah Sharp797b0ca2011-11-10 16:02:13 -08001368 break;
Guoqing Zhang0f1d8322017-04-07 17:56:54 +03001369 case USB_PORT_FEAT_TEST:
1370 /* 4.19.6 Port Test Modes (USB2 Test Mode) */
1371 if (hcd->speed != HCD_USB2)
1372 goto error;
1373 if (test_mode > TEST_FORCE_EN || test_mode < TEST_J)
1374 goto error;
Mathias Nymanec1dafe2017-04-13 14:01:04 +03001375 retval = xhci_enter_test_mode(xhci, test_mode, wIndex,
1376 &flags);
Guoqing Zhang0f1d8322017-04-07 17:56:54 +03001377 break;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001378 default:
1379 goto error;
1380 }
Sarah Sharp5308a912010-12-01 11:34:59 -08001381 /* unblock any posted writes */
Mathias Nymane740b012018-05-21 16:39:55 +03001382 temp = readl(ports[wIndex]->addr);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001383 break;
1384 case ClearPortFeature:
huajun lia0885922011-05-03 21:11:00 +08001385 if (!wIndex || wIndex > max_ports)
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001386 goto error;
1387 wIndex--;
Mathias Nymane740b012018-05-21 16:39:55 +03001388 temp = readl(ports[wIndex]->addr);
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001389 if (temp == ~(u32)0) {
1390 xhci_hc_died(xhci);
Sarah Sharpf9de8152010-10-29 14:37:23 -07001391 retval = -ENODEV;
1392 break;
1393 }
Sarah Sharp4bbb0ac2010-11-29 16:14:37 -08001394 /* FIXME: What new port features do we need to support? */
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001395 temp = xhci_port_state_to_neutral(temp);
1396 switch (wValue) {
Andiry Xube88fe42010-10-14 07:22:57 -07001397 case USB_PORT_FEAT_SUSPEND:
Mathias Nymane740b012018-05-21 16:39:55 +03001398 temp = readl(ports[wIndex]->addr);
Andiry Xube88fe42010-10-14 07:22:57 -07001399 xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
1400 xhci_dbg(xhci, "PORTSC %04x\n", temp);
1401 if (temp & PORT_RESET)
1402 goto error;
Andiry Xu5ac04bf2011-08-03 16:46:48 +08001403 if ((temp & PORT_PLS_MASK) == XDEV_U3) {
Andiry Xube88fe42010-10-14 07:22:57 -07001404 if ((temp & PORT_PE) == 0)
1405 goto error;
Andiry Xube88fe42010-10-14 07:22:57 -07001406
Mathias Nymanf69115f2015-12-11 14:38:06 +02001407 set_bit(wIndex, &bus_state->resuming_ports);
Anshuman Gupta330e2d62018-09-20 19:13:40 +03001408 usb_hcd_start_port_resume(&hcd->self, wIndex);
Mathias Nyman6b7f40f2018-05-21 16:39:59 +03001409 xhci_set_link_state(xhci, ports[wIndex],
1410 XDEV_RESUME);
Andiry Xuc9682df2011-09-23 14:19:48 -07001411 spin_unlock_irqrestore(&xhci->lock, flags);
Mathias Nyman7d3b0162016-10-20 18:09:20 +03001412 msleep(USB_RESUME_TIMEOUT);
Andiry Xua7114232011-04-27 18:07:50 +08001413 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nyman6b7f40f2018-05-21 16:39:59 +03001414 xhci_set_link_state(xhci, ports[wIndex],
Andiry Xuc9682df2011-09-23 14:19:48 -07001415 XDEV_U0);
Mathias Nymanf69115f2015-12-11 14:38:06 +02001416 clear_bit(wIndex, &bus_state->resuming_ports);
Anshuman Gupta330e2d62018-09-20 19:13:40 +03001417 usb_hcd_end_port_resume(&hcd->self, wIndex);
Andiry Xube88fe42010-10-14 07:22:57 -07001418 }
Andiry Xua7114232011-04-27 18:07:50 +08001419 bus_state->port_c_suspend |= 1 << wIndex;
Andiry Xube88fe42010-10-14 07:22:57 -07001420
Sarah Sharp52336302010-12-16 10:49:09 -08001421 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1422 wIndex + 1);
Andiry Xube88fe42010-10-14 07:22:57 -07001423 if (!slot_id) {
1424 xhci_dbg(xhci, "slot_id is zero\n");
1425 goto error;
1426 }
1427 xhci_ring_device(xhci, slot_id);
1428 break;
1429 case USB_PORT_FEAT_C_SUSPEND:
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001430 bus_state->port_c_suspend &= ~(1 << wIndex);
Gustavo A. R. Silvaff504f52017-10-25 13:49:38 -05001431 /* fall through */
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001432 case USB_PORT_FEAT_C_RESET:
Andiry Xua11496e2011-04-27 18:07:29 +08001433 case USB_PORT_FEAT_C_BH_PORT_RESET:
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001434 case USB_PORT_FEAT_C_CONNECTION:
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001435 case USB_PORT_FEAT_C_OVER_CURRENT:
Sarah Sharp6219c0472009-12-09 15:59:11 -08001436 case USB_PORT_FEAT_C_ENABLE:
Andiry Xu85387c02011-04-27 18:07:35 +08001437 case USB_PORT_FEAT_C_PORT_LINK_STATE:
Lu Baolu94251832015-03-23 18:27:41 +02001438 case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
Sarah Sharp34fb5622009-12-09 15:59:08 -08001439 xhci_clear_port_change_bit(xhci, wValue, wIndex,
Mathias Nymane740b012018-05-21 16:39:55 +03001440 ports[wIndex]->addr, temp);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001441 break;
Sarah Sharp6219c0472009-12-09 15:59:11 -08001442 case USB_PORT_FEAT_ENABLE:
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001443 xhci_disable_port(hcd, xhci, wIndex,
Mathias Nymane740b012018-05-21 16:39:55 +03001444 ports[wIndex]->addr, temp);
Sarah Sharp6219c0472009-12-09 15:59:11 -08001445 break;
Lan Tianyu693d8eb2012-09-05 13:44:35 +08001446 case USB_PORT_FEAT_POWER:
Mathias Nymanec1dafe2017-04-13 14:01:04 +03001447 xhci_set_port_power(xhci, hcd, wIndex, false, &flags);
Lan Tianyu693d8eb2012-09-05 13:44:35 +08001448 break;
Guoqing Zhang0f1d8322017-04-07 17:56:54 +03001449 case USB_PORT_FEAT_TEST:
1450 retval = xhci_exit_test_mode(xhci);
1451 break;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001452 default:
1453 goto error;
1454 }
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001455 break;
1456 default:
1457error:
1458 /* "stall" on error */
1459 retval = -EPIPE;
1460 }
1461 spin_unlock_irqrestore(&xhci->lock, flags);
1462 return retval;
1463}
1464
1465/*
1466 * Returns 0 if the status hasn't changed, or the number of bytes in buf.
1467 * Ports are 0-indexed from the HCD point of view,
1468 * and 1-indexed from the USB core pointer of view.
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001469 *
1470 * Note that the status change bits will be cleared as soon as a port status
1471 * change event is generated, so we use the saved status from that event.
1472 */
1473int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
1474{
1475 unsigned long flags;
1476 u32 temp, status;
Andiry Xu56192532010-10-14 07:23:00 -07001477 u32 mask;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001478 int i, retval;
1479 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
huajun lia0885922011-05-03 21:11:00 +08001480 int max_ports;
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001481 struct xhci_bus_state *bus_state;
Sarah Sharpc52804a2012-11-27 12:30:23 -08001482 bool reset_change = false;
Mathias Nymane740b012018-05-21 16:39:55 +03001483 struct xhci_hub *rhub;
1484 struct xhci_port **ports;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001485
Mathias Nymane740b012018-05-21 16:39:55 +03001486 rhub = xhci_get_rhub(hcd);
1487 ports = rhub->ports;
Mathias Nyman925f3492018-05-21 16:40:02 +03001488 max_ports = rhub->num_ports;
Mathias Nymanf6187f42018-12-07 16:19:30 +02001489 bus_state = &rhub->bus_state;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001490
1491 /* Initial status is no changes */
huajun lia0885922011-05-03 21:11:00 +08001492 retval = (max_ports + 8) / 8;
William Gulland419a8e812010-05-12 10:20:34 -07001493 memset(buf, 0, retval);
Andiry Xuf370b992012-04-14 02:54:30 +08001494
1495 /*
1496 * Inform the usbcore about resume-in-progress by returning
1497 * a non-zero value even if there are no status changes.
1498 */
1499 status = bus_state->resuming_ports;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001500
Lu Baolu94251832015-03-23 18:27:41 +02001501 mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
Andiry Xu56192532010-10-14 07:23:00 -07001502
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001503 spin_lock_irqsave(&xhci->lock, flags);
1504 /* For each port, did anything change? If so, set that bit in buf. */
huajun lia0885922011-05-03 21:11:00 +08001505 for (i = 0; i < max_ports; i++) {
Mathias Nymane740b012018-05-21 16:39:55 +03001506 temp = readl(ports[i]->addr);
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001507 if (temp == ~(u32)0) {
1508 xhci_hc_died(xhci);
Sarah Sharpf9de8152010-10-29 14:37:23 -07001509 retval = -ENODEV;
1510 break;
1511 }
Mathias Nyman3f8499a2017-12-08 17:59:15 +02001512 trace_xhci_hub_status_data(i, temp);
1513
Andiry Xu56192532010-10-14 07:23:00 -07001514 if ((temp & mask) != 0 ||
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001515 (bus_state->port_c_suspend & 1 << i) ||
1516 (bus_state->resume_done[i] && time_after_eq(
1517 jiffies, bus_state->resume_done[i]))) {
William Gulland419a8e812010-05-12 10:20:34 -07001518 buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001519 status = 1;
1520 }
Sarah Sharpc52804a2012-11-27 12:30:23 -08001521 if ((temp & PORT_RC))
1522 reset_change = true;
1523 }
1524 if (!status && !reset_change) {
1525 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
1526 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001527 }
1528 spin_unlock_irqrestore(&xhci->lock, flags);
1529 return status ? retval : 0;
1530}
Andiry Xu9777e3c2010-10-14 07:23:03 -07001531
1532#ifdef CONFIG_PM
1533
1534int xhci_bus_suspend(struct usb_hcd *hcd)
1535{
1536 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp518e8482010-12-15 11:56:29 -08001537 int max_ports, port_index;
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001538 struct xhci_bus_state *bus_state;
Andiry Xu9777e3c2010-10-14 07:23:03 -07001539 unsigned long flags;
Mathias Nymane740b012018-05-21 16:39:55 +03001540 struct xhci_hub *rhub;
1541 struct xhci_port **ports;
Mathias Nyman2f31a672018-11-15 11:38:41 +02001542 u32 portsc_buf[USB_MAXCHILDREN];
1543 bool wake_enabled;
Andiry Xu9777e3c2010-10-14 07:23:03 -07001544
Mathias Nymane740b012018-05-21 16:39:55 +03001545 rhub = xhci_get_rhub(hcd);
1546 ports = rhub->ports;
Mathias Nyman925f3492018-05-21 16:40:02 +03001547 max_ports = rhub->num_ports;
Mathias Nymanf6187f42018-12-07 16:19:30 +02001548 bus_state = &rhub->bus_state;
Mathias Nyman2f31a672018-11-15 11:38:41 +02001549 wake_enabled = hcd->self.root_hub->do_remote_wakeup;
Andiry Xu9777e3c2010-10-14 07:23:03 -07001550
1551 spin_lock_irqsave(&xhci->lock, flags);
1552
Mathias Nyman2f31a672018-11-15 11:38:41 +02001553 if (wake_enabled) {
Zhuang Jin Canfac42712015-07-21 17:20:30 +03001554 if (bus_state->resuming_ports || /* USB2 */
1555 bus_state->port_remote_wakeup) { /* USB3 */
Andiry Xuf370b992012-04-14 02:54:30 +08001556 spin_unlock_irqrestore(&xhci->lock, flags);
Zhuang Jin Canfac42712015-07-21 17:20:30 +03001557 xhci_dbg(xhci, "suspend failed because a port is resuming\n");
Andiry Xuf370b992012-04-14 02:54:30 +08001558 return -EBUSY;
Andiry Xu9777e3c2010-10-14 07:23:03 -07001559 }
1560 }
Mathias Nyman2f31a672018-11-15 11:38:41 +02001561 /*
1562 * Prepare ports for suspend, but don't write anything before all ports
1563 * are checked and we know bus suspend can proceed
1564 */
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001565 bus_state->bus_suspended = 0;
Mathias Nyman2f31a672018-11-15 11:38:41 +02001566 port_index = max_ports;
Sarah Sharp518e8482010-12-15 11:56:29 -08001567 while (port_index--) {
Andiry Xu9777e3c2010-10-14 07:23:03 -07001568 u32 t1, t2;
Mathias Nymand92f2c52019-03-22 17:50:17 +02001569 int retries = 10;
1570retry:
Mathias Nymane740b012018-05-21 16:39:55 +03001571 t1 = readl(ports[port_index]->addr);
Andiry Xu9777e3c2010-10-14 07:23:03 -07001572 t2 = xhci_port_state_to_neutral(t1);
Mathias Nyman2f31a672018-11-15 11:38:41 +02001573 portsc_buf[port_index] = 0;
Andiry Xu9777e3c2010-10-14 07:23:03 -07001574
Mathias Nymand92f2c52019-03-22 17:50:17 +02001575 /*
1576 * Give a USB3 port in link training time to finish, but don't
1577 * prevent suspend as port might be stuck
1578 */
1579 if ((hcd->speed >= HCD_USB3) && retries-- &&
Mathias Nyman45f750c2018-12-14 10:54:43 +02001580 (t1 & PORT_PLS_MASK) == XDEV_POLLING) {
Mathias Nyman2f31a672018-11-15 11:38:41 +02001581 spin_unlock_irqrestore(&xhci->lock, flags);
Mathias Nymand92f2c52019-03-22 17:50:17 +02001582 msleep(XHCI_PORT_POLLING_LFPS_TIME);
1583 spin_lock_irqsave(&xhci->lock, flags);
1584 xhci_dbg(xhci, "port %d polling in bus suspend, waiting\n",
1585 port_index);
1586 goto retry;
Mathias Nyman2f31a672018-11-15 11:38:41 +02001587 }
Mathias Nyman2f31a672018-11-15 11:38:41 +02001588 /* suspend ports in U0, or bail out for new connect changes */
1589 if ((t1 & PORT_PE) && (t1 & PORT_PLS_MASK) == XDEV_U0) {
1590 if ((t1 & PORT_CSC) && wake_enabled) {
1591 bus_state->bus_suspended = 0;
Andiry Xu9777e3c2010-10-14 07:23:03 -07001592 spin_unlock_irqrestore(&xhci->lock, flags);
Mathias Nyman2f31a672018-11-15 11:38:41 +02001593 xhci_dbg(xhci, "Bus suspend bailout, port connect change\n");
1594 return -EBUSY;
Andiry Xu9777e3c2010-10-14 07:23:03 -07001595 }
Mathias Nyman2f31a672018-11-15 11:38:41 +02001596 xhci_dbg(xhci, "port %d not suspended\n", port_index);
Andiry Xu9777e3c2010-10-14 07:23:03 -07001597 t2 &= ~PORT_PLS_MASK;
1598 t2 |= PORT_LINK_STROBE | XDEV_U3;
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001599 set_bit(port_index, &bus_state->bus_suspended);
Andiry Xu9777e3c2010-10-14 07:23:03 -07001600 }
Sarah Sharp4296c70a2012-01-06 10:34:31 -08001601 /* USB core sets remote wake mask for USB 3.0 hubs,
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01001602 * including the USB 3.0 roothub, but only if CONFIG_PM
Sarah Sharp4296c70a2012-01-06 10:34:31 -08001603 * is enabled, so also enable remote wake here.
1604 */
Mathias Nyman2f31a672018-11-15 11:38:41 +02001605 if (wake_enabled) {
Andiry Xu9777e3c2010-10-14 07:23:03 -07001606 if (t1 & PORT_CONNECT) {
1607 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
1608 t2 &= ~PORT_WKCONN_E;
1609 } else {
1610 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
1611 t2 &= ~PORT_WKDISC_E;
1612 }
Joe Leebde07162018-02-12 14:24:46 +02001613
1614 if ((xhci->quirks & XHCI_U2_DISABLE_WAKE) &&
1615 (hcd->speed < HCD_USB3)) {
1616 if (usb_amd_pt_check_port(hcd->self.controller,
1617 port_index))
1618 t2 &= ~PORT_WAKE_BITS;
1619 }
Andiry Xu9777e3c2010-10-14 07:23:03 -07001620 } else
1621 t2 &= ~PORT_WAKE_BITS;
1622
1623 t1 = xhci_port_state_to_neutral(t1);
1624 if (t1 != t2)
Mathias Nyman2f31a672018-11-15 11:38:41 +02001625 portsc_buf[port_index] = t2;
1626 }
1627
1628 /* write port settings, stopping and suspending ports if needed */
1629 port_index = max_ports;
1630 while (port_index--) {
1631 if (!portsc_buf[port_index])
1632 continue;
1633 if (test_bit(port_index, &bus_state->bus_suspended)) {
1634 int slot_id;
1635
1636 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1637 port_index + 1);
1638 if (slot_id) {
1639 spin_unlock_irqrestore(&xhci->lock, flags);
1640 xhci_stop_device(xhci, slot_id, 1);
1641 spin_lock_irqsave(&xhci->lock, flags);
1642 }
1643 }
1644 writel(portsc_buf[port_index], ports[port_index]->addr);
Andiry Xu9777e3c2010-10-14 07:23:03 -07001645 }
1646 hcd->state = HC_STATE_SUSPENDED;
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001647 bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
Andiry Xu9777e3c2010-10-14 07:23:03 -07001648 spin_unlock_irqrestore(&xhci->lock, flags);
1649 return 0;
1650}
1651
Mathias Nyman346e99732016-10-20 18:09:19 +03001652/*
1653 * Workaround for missing Cold Attach Status (CAS) if device re-plugged in S3.
1654 * warm reset a USB3 device stuck in polling or compliance mode after resume.
1655 * See Intel 100/c230 series PCH specification update Doc #332692-006 Errata #8
1656 */
Mathias Nymanfdcf74ff2018-05-21 16:39:56 +03001657static bool xhci_port_missing_cas_quirk(struct xhci_port *port)
Mathias Nyman346e99732016-10-20 18:09:19 +03001658{
1659 u32 portsc;
1660
Mathias Nymanfdcf74ff2018-05-21 16:39:56 +03001661 portsc = readl(port->addr);
Mathias Nyman346e99732016-10-20 18:09:19 +03001662
1663 /* if any of these are set we are not stuck */
1664 if (portsc & (PORT_CONNECT | PORT_CAS))
1665 return false;
1666
1667 if (((portsc & PORT_PLS_MASK) != XDEV_POLLING) &&
1668 ((portsc & PORT_PLS_MASK) != XDEV_COMP_MODE))
1669 return false;
1670
1671 /* clear wakeup/change bits, and do a warm port reset */
1672 portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
1673 portsc |= PORT_WR;
Mathias Nymanfdcf74ff2018-05-21 16:39:56 +03001674 writel(portsc, port->addr);
Mathias Nyman346e99732016-10-20 18:09:19 +03001675 /* flush write */
Mathias Nymanfdcf74ff2018-05-21 16:39:56 +03001676 readl(port->addr);
Mathias Nyman346e99732016-10-20 18:09:19 +03001677 return true;
1678}
1679
Andiry Xu9777e3c2010-10-14 07:23:03 -07001680int xhci_bus_resume(struct usb_hcd *hcd)
1681{
1682 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001683 struct xhci_bus_state *bus_state;
Andiry Xu9777e3c2010-10-14 07:23:03 -07001684 unsigned long flags;
Mathias Nymana85c0f82017-08-16 14:23:26 +03001685 int max_ports, port_index;
Mathias Nyman41485a92015-05-29 17:01:51 +03001686 int slot_id;
1687 int sret;
Mathias Nymana85c0f82017-08-16 14:23:26 +03001688 u32 next_state;
1689 u32 temp, portsc;
Mathias Nymane740b012018-05-21 16:39:55 +03001690 struct xhci_hub *rhub;
1691 struct xhci_port **ports;
Andiry Xu9777e3c2010-10-14 07:23:03 -07001692
Mathias Nymane740b012018-05-21 16:39:55 +03001693 rhub = xhci_get_rhub(hcd);
1694 ports = rhub->ports;
Mathias Nyman925f3492018-05-21 16:40:02 +03001695 max_ports = rhub->num_ports;
Mathias Nymanf6187f42018-12-07 16:19:30 +02001696 bus_state = &rhub->bus_state;
Andiry Xu9777e3c2010-10-14 07:23:03 -07001697
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001698 if (time_before(jiffies, bus_state->next_statechange))
Andiry Xu9777e3c2010-10-14 07:23:03 -07001699 msleep(5);
1700
1701 spin_lock_irqsave(&xhci->lock, flags);
1702 if (!HCD_HW_ACCESSIBLE(hcd)) {
1703 spin_unlock_irqrestore(&xhci->lock, flags);
1704 return -ESHUTDOWN;
1705 }
1706
1707 /* delay the irqs */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001708 temp = readl(&xhci->op_regs->command);
Andiry Xu9777e3c2010-10-14 07:23:03 -07001709 temp &= ~CMD_EIE;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001710 writel(temp, &xhci->op_regs->command);
Andiry Xu9777e3c2010-10-14 07:23:03 -07001711
Mathias Nymana85c0f82017-08-16 14:23:26 +03001712 /* bus specific resume for ports we suspended at bus_suspend */
1713 if (hcd->speed >= HCD_USB3)
1714 next_state = XDEV_U0;
1715 else
1716 next_state = XDEV_RESUME;
1717
Sarah Sharp518e8482010-12-15 11:56:29 -08001718 port_index = max_ports;
1719 while (port_index--) {
Mathias Nymane740b012018-05-21 16:39:55 +03001720 portsc = readl(ports[port_index]->addr);
Mathias Nyman346e99732016-10-20 18:09:19 +03001721
1722 /* warm reset CAS limited ports stuck in polling/compliance */
1723 if ((xhci->quirks & XHCI_MISSING_CAS) &&
1724 (hcd->speed >= HCD_USB3) &&
Mathias Nymanfdcf74ff2018-05-21 16:39:56 +03001725 xhci_port_missing_cas_quirk(ports[port_index])) {
Mathias Nyman346e99732016-10-20 18:09:19 +03001726 xhci_dbg(xhci, "reset stuck port %d\n", port_index);
Mathias Nymana85c0f82017-08-16 14:23:26 +03001727 clear_bit(port_index, &bus_state->bus_suspended);
Mathias Nyman346e99732016-10-20 18:09:19 +03001728 continue;
1729 }
Mathias Nymana85c0f82017-08-16 14:23:26 +03001730 /* resume if we suspended the link, and it is still suspended */
1731 if (test_bit(port_index, &bus_state->bus_suspended))
1732 switch (portsc & PORT_PLS_MASK) {
1733 case XDEV_U3:
1734 portsc = xhci_port_state_to_neutral(portsc);
1735 portsc &= ~PORT_PLS_MASK;
1736 portsc |= PORT_LINK_STROBE | next_state;
1737 break;
1738 case XDEV_RESUME:
1739 /* resume already initiated */
1740 break;
1741 default:
1742 /* not in a resumeable state, ignore it */
1743 clear_bit(port_index,
1744 &bus_state->bus_suspended);
1745 break;
Andiry Xu9777e3c2010-10-14 07:23:03 -07001746 }
Mathias Nymana85c0f82017-08-16 14:23:26 +03001747 /* disable wake for all ports, write new link state if needed */
1748 portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
Mathias Nymane740b012018-05-21 16:39:55 +03001749 writel(portsc, ports[port_index]->addr);
Andiry Xu9777e3c2010-10-14 07:23:03 -07001750 }
1751
Mathias Nymana85c0f82017-08-16 14:23:26 +03001752 /* USB2 specific resume signaling delay and U0 link state transition */
1753 if (hcd->speed < HCD_USB3) {
1754 if (bus_state->bus_suspended) {
1755 spin_unlock_irqrestore(&xhci->lock, flags);
1756 msleep(USB_RESUME_TIMEOUT);
1757 spin_lock_irqsave(&xhci->lock, flags);
1758 }
1759 for_each_set_bit(port_index, &bus_state->bus_suspended,
1760 BITS_PER_LONG) {
1761 /* Clear PLC to poll it later for U0 transition */
Mathias Nymaneaefcf22018-05-21 16:40:00 +03001762 xhci_test_and_clear_bit(xhci, ports[port_index],
Mathias Nymana85c0f82017-08-16 14:23:26 +03001763 PORT_PLC);
Mathias Nyman6b7f40f2018-05-21 16:39:59 +03001764 xhci_set_link_state(xhci, ports[port_index], XDEV_U0);
Mathias Nymana85c0f82017-08-16 14:23:26 +03001765 }
Mathias Nyman41485a92015-05-29 17:01:51 +03001766 }
1767
Mathias Nymana85c0f82017-08-16 14:23:26 +03001768 /* poll for U0 link state complete, both USB2 and USB3 */
1769 for_each_set_bit(port_index, &bus_state->bus_suspended, BITS_PER_LONG) {
Mathias Nymane740b012018-05-21 16:39:55 +03001770 sret = xhci_handshake(ports[port_index]->addr, PORT_PLC,
Mathias Nyman41485a92015-05-29 17:01:51 +03001771 PORT_PLC, 10 * 1000);
Mathias Nymana85c0f82017-08-16 14:23:26 +03001772 if (sret) {
Mathias Nyman41485a92015-05-29 17:01:51 +03001773 xhci_warn(xhci, "port %d resume PLC timeout\n",
1774 port_index);
Mathias Nymana85c0f82017-08-16 14:23:26 +03001775 continue;
1776 }
Mathias Nymaneaefcf22018-05-21 16:40:00 +03001777 xhci_test_and_clear_bit(xhci, ports[port_index], PORT_PLC);
Mathias Nyman41485a92015-05-29 17:01:51 +03001778 slot_id = xhci_find_slot_id_by_port(hcd, xhci, port_index + 1);
1779 if (slot_id)
1780 xhci_ring_device(xhci, slot_id);
1781 }
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001782 (void) readl(&xhci->op_regs->command);
Andiry Xu9777e3c2010-10-14 07:23:03 -07001783
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001784 bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
Andiry Xu9777e3c2010-10-14 07:23:03 -07001785 /* re-enable irqs */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001786 temp = readl(&xhci->op_regs->command);
Andiry Xu9777e3c2010-10-14 07:23:03 -07001787 temp |= CMD_EIE;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001788 writel(temp, &xhci->op_regs->command);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001789 temp = readl(&xhci->op_regs->command);
Andiry Xu9777e3c2010-10-14 07:23:03 -07001790
1791 spin_unlock_irqrestore(&xhci->lock, flags);
1792 return 0;
1793}
1794
Alan Stern8f9cc83c2018-06-08 16:59:57 -04001795unsigned long xhci_get_resuming_ports(struct usb_hcd *hcd)
1796{
Mathias Nymanf6187f42018-12-07 16:19:30 +02001797 struct xhci_hub *rhub = xhci_get_rhub(hcd);
Alan Stern8f9cc83c2018-06-08 16:59:57 -04001798
1799 /* USB3 port wakeups are reported via usb_wakeup_notification() */
Mathias Nymanf6187f42018-12-07 16:19:30 +02001800 return rhub->bus_state.resuming_ports; /* USB2 ports only */
Alan Stern8f9cc83c2018-06-08 16:59:57 -04001801}
1802
Sarah Sharp436a3892010-10-15 14:59:15 -07001803#endif /* CONFIG_PM */