aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/host/xhci.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/host/xhci.c')
-rw-r--r--drivers/usb/host/xhci.c483
1 files changed, 479 insertions, 4 deletions
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 36641a7f2371..afdc73ee84a6 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -152,7 +152,7 @@ int xhci_reset(struct xhci_hcd *xhci)
152{ 152{
153 u32 command; 153 u32 command;
154 u32 state; 154 u32 state;
155 int ret; 155 int ret, i;
156 156
157 state = xhci_readl(xhci, &xhci->op_regs->status); 157 state = xhci_readl(xhci, &xhci->op_regs->status);
158 if ((state & STS_HALT) == 0) { 158 if ((state & STS_HALT) == 0) {
@@ -175,7 +175,15 @@ int xhci_reset(struct xhci_hcd *xhci)
175 * xHCI cannot write to any doorbells or operational registers other 175 * xHCI cannot write to any doorbells or operational registers other
176 * than status until the "Controller Not Ready" flag is cleared. 176 * than status until the "Controller Not Ready" flag is cleared.
177 */ 177 */
178 return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000); 178 ret = handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
179
180 for (i = 0; i < 2; ++i) {
181 xhci->bus_state[i].port_c_suspend = 0;
182 xhci->bus_state[i].suspended_ports = 0;
183 xhci->bus_state[i].resuming_ports = 0;
184 }
185
186 return ret;
179} 187}
180 188
181#ifdef CONFIG_PCI 189#ifdef CONFIG_PCI
@@ -2438,7 +2446,7 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
2438 udev->slot_id, must_succeed); 2446 udev->slot_id, must_succeed);
2439 else 2447 else
2440 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma, 2448 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
2441 udev->slot_id); 2449 udev->slot_id, must_succeed);
2442 if (ret < 0) { 2450 if (ret < 0) {
2443 if (command) 2451 if (command)
2444 list_del(&command->cmd_list); 2452 list_del(&command->cmd_list);
@@ -3863,6 +3871,474 @@ int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
3863 3871
3864#endif /* CONFIG_USB_SUSPEND */ 3872#endif /* CONFIG_USB_SUSPEND */
3865 3873
3874/*---------------------- USB 3.0 Link PM functions ------------------------*/
3875
3876#ifdef CONFIG_PM
3877/* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
3878static unsigned long long xhci_service_interval_to_ns(
3879 struct usb_endpoint_descriptor *desc)
3880{
3881 return (1 << (desc->bInterval - 1)) * 125 * 1000;
3882}
3883
3884static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev,
3885 enum usb3_link_state state)
3886{
3887 unsigned long long sel;
3888 unsigned long long pel;
3889 unsigned int max_sel_pel;
3890 char *state_name;
3891
3892 switch (state) {
3893 case USB3_LPM_U1:
3894 /* Convert SEL and PEL stored in nanoseconds to microseconds */
3895 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3896 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3897 max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL;
3898 state_name = "U1";
3899 break;
3900 case USB3_LPM_U2:
3901 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3902 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3903 max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL;
3904 state_name = "U2";
3905 break;
3906 default:
3907 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n",
3908 __func__);
3909 return -EINVAL;
3910 }
3911
3912 if (sel <= max_sel_pel && pel <= max_sel_pel)
3913 return USB3_LPM_DEVICE_INITIATED;
3914
3915 if (sel > max_sel_pel)
3916 dev_dbg(&udev->dev, "Device-initiated %s disabled "
3917 "due to long SEL %llu ms\n",
3918 state_name, sel);
3919 else
3920 dev_dbg(&udev->dev, "Device-initiated %s disabled "
3921 "due to long PEL %llu\n ms",
3922 state_name, pel);
3923 return USB3_LPM_DISABLED;
3924}
3925
3926/* Returns the hub-encoded U1 timeout value.
3927 * The U1 timeout should be the maximum of the following values:
3928 * - For control endpoints, U1 system exit latency (SEL) * 3
3929 * - For bulk endpoints, U1 SEL * 5
3930 * - For interrupt endpoints:
3931 * - Notification EPs, U1 SEL * 3
3932 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
3933 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
3934 */
3935static u16 xhci_calculate_intel_u1_timeout(struct usb_device *udev,
3936 struct usb_endpoint_descriptor *desc)
3937{
3938 unsigned long long timeout_ns;
3939 int ep_type;
3940 int intr_type;
3941
3942 ep_type = usb_endpoint_type(desc);
3943 switch (ep_type) {
3944 case USB_ENDPOINT_XFER_CONTROL:
3945 timeout_ns = udev->u1_params.sel * 3;
3946 break;
3947 case USB_ENDPOINT_XFER_BULK:
3948 timeout_ns = udev->u1_params.sel * 5;
3949 break;
3950 case USB_ENDPOINT_XFER_INT:
3951 intr_type = usb_endpoint_interrupt_type(desc);
3952 if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) {
3953 timeout_ns = udev->u1_params.sel * 3;
3954 break;
3955 }
3956 /* Otherwise the calculation is the same as isoc eps */
3957 case USB_ENDPOINT_XFER_ISOC:
3958 timeout_ns = xhci_service_interval_to_ns(desc);
3959 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100);
3960 if (timeout_ns < udev->u1_params.sel * 2)
3961 timeout_ns = udev->u1_params.sel * 2;
3962 break;
3963 default:
3964 return 0;
3965 }
3966
3967 /* The U1 timeout is encoded in 1us intervals. */
3968 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000);
3969 /* Don't return a timeout of zero, because that's USB3_LPM_DISABLED. */
3970 if (timeout_ns == USB3_LPM_DISABLED)
3971 timeout_ns++;
3972
3973 /* If the necessary timeout value is bigger than what we can set in the
3974 * USB 3.0 hub, we have to disable hub-initiated U1.
3975 */
3976 if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT)
3977 return timeout_ns;
3978 dev_dbg(&udev->dev, "Hub-initiated U1 disabled "
3979 "due to long timeout %llu ms\n", timeout_ns);
3980 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1);
3981}
3982
3983/* Returns the hub-encoded U2 timeout value.
3984 * The U2 timeout should be the maximum of:
3985 * - 10 ms (to avoid the bandwidth impact on the scheduler)
3986 * - largest bInterval of any active periodic endpoint (to avoid going
3987 * into lower power link states between intervals).
3988 * - the U2 Exit Latency of the device
3989 */
3990static u16 xhci_calculate_intel_u2_timeout(struct usb_device *udev,
3991 struct usb_endpoint_descriptor *desc)
3992{
3993 unsigned long long timeout_ns;
3994 unsigned long long u2_del_ns;
3995
3996 timeout_ns = 10 * 1000 * 1000;
3997
3998 if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) &&
3999 (xhci_service_interval_to_ns(desc) > timeout_ns))
4000 timeout_ns = xhci_service_interval_to_ns(desc);
4001
4002 u2_del_ns = udev->bos->ss_cap->bU2DevExitLat * 1000;
4003 if (u2_del_ns > timeout_ns)
4004 timeout_ns = u2_del_ns;
4005
4006 /* The U2 timeout is encoded in 256us intervals */
4007 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000);
4008 /* If the necessary timeout value is bigger than what we can set in the
4009 * USB 3.0 hub, we have to disable hub-initiated U2.
4010 */
4011 if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT)
4012 return timeout_ns;
4013 dev_dbg(&udev->dev, "Hub-initiated U2 disabled "
4014 "due to long timeout %llu ms\n", timeout_ns);
4015 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2);
4016}
4017
4018static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4019 struct usb_device *udev,
4020 struct usb_endpoint_descriptor *desc,
4021 enum usb3_link_state state,
4022 u16 *timeout)
4023{
4024 if (state == USB3_LPM_U1) {
4025 if (xhci->quirks & XHCI_INTEL_HOST)
4026 return xhci_calculate_intel_u1_timeout(udev, desc);
4027 } else {
4028 if (xhci->quirks & XHCI_INTEL_HOST)
4029 return xhci_calculate_intel_u2_timeout(udev, desc);
4030 }
4031
4032 return USB3_LPM_DISABLED;
4033}
4034
4035static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4036 struct usb_device *udev,
4037 struct usb_endpoint_descriptor *desc,
4038 enum usb3_link_state state,
4039 u16 *timeout)
4040{
4041 u16 alt_timeout;
4042
4043 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev,
4044 desc, state, timeout);
4045
4046 /* If we found we can't enable hub-initiated LPM, or
4047 * the U1 or U2 exit latency was too high to allow
4048 * device-initiated LPM as well, just stop searching.
4049 */
4050 if (alt_timeout == USB3_LPM_DISABLED ||
4051 alt_timeout == USB3_LPM_DEVICE_INITIATED) {
4052 *timeout = alt_timeout;
4053 return -E2BIG;
4054 }
4055 if (alt_timeout > *timeout)
4056 *timeout = alt_timeout;
4057 return 0;
4058}
4059
4060static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci,
4061 struct usb_device *udev,
4062 struct usb_host_interface *alt,
4063 enum usb3_link_state state,
4064 u16 *timeout)
4065{
4066 int j;
4067
4068 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
4069 if (xhci_update_timeout_for_endpoint(xhci, udev,
4070 &alt->endpoint[j].desc, state, timeout))
4071 return -E2BIG;
4072 continue;
4073 }
4074 return 0;
4075}
4076
4077static int xhci_check_intel_tier_policy(struct usb_device *udev,
4078 enum usb3_link_state state)
4079{
4080 struct usb_device *parent;
4081 unsigned int num_hubs;
4082
4083 if (state == USB3_LPM_U2)
4084 return 0;
4085
4086 /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
4087 for (parent = udev->parent, num_hubs = 0; parent->parent;
4088 parent = parent->parent)
4089 num_hubs++;
4090
4091 if (num_hubs < 2)
4092 return 0;
4093
4094 dev_dbg(&udev->dev, "Disabling U1 link state for device"
4095 " below second-tier hub.\n");
4096 dev_dbg(&udev->dev, "Plug device into first-tier hub "
4097 "to decrease power consumption.\n");
4098 return -E2BIG;
4099}
4100
4101static int xhci_check_tier_policy(struct xhci_hcd *xhci,
4102 struct usb_device *udev,
4103 enum usb3_link_state state)
4104{
4105 if (xhci->quirks & XHCI_INTEL_HOST)
4106 return xhci_check_intel_tier_policy(udev, state);
4107 return -EINVAL;
4108}
4109
4110/* Returns the U1 or U2 timeout that should be enabled.
4111 * If the tier check or timeout setting functions return with a non-zero exit
4112 * code, that means the timeout value has been finalized and we shouldn't look
4113 * at any more endpoints.
4114 */
4115static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd,
4116 struct usb_device *udev, enum usb3_link_state state)
4117{
4118 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4119 struct usb_host_config *config;
4120 char *state_name;
4121 int i;
4122 u16 timeout = USB3_LPM_DISABLED;
4123
4124 if (state == USB3_LPM_U1)
4125 state_name = "U1";
4126 else if (state == USB3_LPM_U2)
4127 state_name = "U2";
4128 else {
4129 dev_warn(&udev->dev, "Can't enable unknown link state %i\n",
4130 state);
4131 return timeout;
4132 }
4133
4134 if (xhci_check_tier_policy(xhci, udev, state) < 0)
4135 return timeout;
4136
4137 /* Gather some information about the currently installed configuration
4138 * and alternate interface settings.
4139 */
4140 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc,
4141 state, &timeout))
4142 return timeout;
4143
4144 config = udev->actconfig;
4145 if (!config)
4146 return timeout;
4147
4148 for (i = 0; i < USB_MAXINTERFACES; i++) {
4149 struct usb_driver *driver;
4150 struct usb_interface *intf = config->interface[i];
4151
4152 if (!intf)
4153 continue;
4154
4155 /* Check if any currently bound drivers want hub-initiated LPM
4156 * disabled.
4157 */
4158 if (intf->dev.driver) {
4159 driver = to_usb_driver(intf->dev.driver);
4160 if (driver && driver->disable_hub_initiated_lpm) {
4161 dev_dbg(&udev->dev, "Hub-initiated %s disabled "
4162 "at request of driver %s\n",
4163 state_name, driver->name);
4164 return xhci_get_timeout_no_hub_lpm(udev, state);
4165 }
4166 }
4167
4168 /* Not sure how this could happen... */
4169 if (!intf->cur_altsetting)
4170 continue;
4171
4172 if (xhci_update_timeout_for_interface(xhci, udev,
4173 intf->cur_altsetting,
4174 state, &timeout))
4175 return timeout;
4176 }
4177 return timeout;
4178}
4179
4180/*
4181 * Issue an Evaluate Context command to change the Maximum Exit Latency in the
4182 * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
4183 */
4184static int xhci_change_max_exit_latency(struct xhci_hcd *xhci,
4185 struct usb_device *udev, u16 max_exit_latency)
4186{
4187 struct xhci_virt_device *virt_dev;
4188 struct xhci_command *command;
4189 struct xhci_input_control_ctx *ctrl_ctx;
4190 struct xhci_slot_ctx *slot_ctx;
4191 unsigned long flags;
4192 int ret;
4193
4194 spin_lock_irqsave(&xhci->lock, flags);
4195 if (max_exit_latency == xhci->devs[udev->slot_id]->current_mel) {
4196 spin_unlock_irqrestore(&xhci->lock, flags);
4197 return 0;
4198 }
4199
4200 /* Attempt to issue an Evaluate Context command to change the MEL. */
4201 virt_dev = xhci->devs[udev->slot_id];
4202 command = xhci->lpm_command;
4203 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
4204 spin_unlock_irqrestore(&xhci->lock, flags);
4205
4206 ctrl_ctx = xhci_get_input_control_ctx(xhci, command->in_ctx);
4207 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
4208 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
4209 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT));
4210 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency);
4211
4212 xhci_dbg(xhci, "Set up evaluate context for LPM MEL change.\n");
4213 xhci_dbg(xhci, "Slot %u Input Context:\n", udev->slot_id);
4214 xhci_dbg_ctx(xhci, command->in_ctx, 0);
4215
4216 /* Issue and wait for the evaluate context command. */
4217 ret = xhci_configure_endpoint(xhci, udev, command,
4218 true, true);
4219 xhci_dbg(xhci, "Slot %u Output Context:\n", udev->slot_id);
4220 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 0);
4221
4222 if (!ret) {
4223 spin_lock_irqsave(&xhci->lock, flags);
4224 virt_dev->current_mel = max_exit_latency;
4225 spin_unlock_irqrestore(&xhci->lock, flags);
4226 }
4227 return ret;
4228}
4229
4230static int calculate_max_exit_latency(struct usb_device *udev,
4231 enum usb3_link_state state_changed,
4232 u16 hub_encoded_timeout)
4233{
4234 unsigned long long u1_mel_us = 0;
4235 unsigned long long u2_mel_us = 0;
4236 unsigned long long mel_us = 0;
4237 bool disabling_u1;
4238 bool disabling_u2;
4239 bool enabling_u1;
4240 bool enabling_u2;
4241
4242 disabling_u1 = (state_changed == USB3_LPM_U1 &&
4243 hub_encoded_timeout == USB3_LPM_DISABLED);
4244 disabling_u2 = (state_changed == USB3_LPM_U2 &&
4245 hub_encoded_timeout == USB3_LPM_DISABLED);
4246
4247 enabling_u1 = (state_changed == USB3_LPM_U1 &&
4248 hub_encoded_timeout != USB3_LPM_DISABLED);
4249 enabling_u2 = (state_changed == USB3_LPM_U2 &&
4250 hub_encoded_timeout != USB3_LPM_DISABLED);
4251
4252 /* If U1 was already enabled and we're not disabling it,
4253 * or we're going to enable U1, account for the U1 max exit latency.
4254 */
4255 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) ||
4256 enabling_u1)
4257 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000);
4258 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) ||
4259 enabling_u2)
4260 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000);
4261
4262 if (u1_mel_us > u2_mel_us)
4263 mel_us = u1_mel_us;
4264 else
4265 mel_us = u2_mel_us;
4266 /* xHCI host controller max exit latency field is only 16 bits wide. */
4267 if (mel_us > MAX_EXIT) {
4268 dev_warn(&udev->dev, "Link PM max exit latency of %lluus "
4269 "is too big.\n", mel_us);
4270 return -E2BIG;
4271 }
4272 return mel_us;
4273}
4274
4275/* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
4276int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4277 struct usb_device *udev, enum usb3_link_state state)
4278{
4279 struct xhci_hcd *xhci;
4280 u16 hub_encoded_timeout;
4281 int mel;
4282 int ret;
4283
4284 xhci = hcd_to_xhci(hcd);
4285 /* The LPM timeout values are pretty host-controller specific, so don't
4286 * enable hub-initiated timeouts unless the vendor has provided
4287 * information about their timeout algorithm.
4288 */
4289 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4290 !xhci->devs[udev->slot_id])
4291 return USB3_LPM_DISABLED;
4292
4293 hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state);
4294 mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout);
4295 if (mel < 0) {
4296 /* Max Exit Latency is too big, disable LPM. */
4297 hub_encoded_timeout = USB3_LPM_DISABLED;
4298 mel = 0;
4299 }
4300
4301 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4302 if (ret)
4303 return ret;
4304 return hub_encoded_timeout;
4305}
4306
4307int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4308 struct usb_device *udev, enum usb3_link_state state)
4309{
4310 struct xhci_hcd *xhci;
4311 u16 mel;
4312 int ret;
4313
4314 xhci = hcd_to_xhci(hcd);
4315 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4316 !xhci->devs[udev->slot_id])
4317 return 0;
4318
4319 mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED);
4320 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4321 if (ret)
4322 return ret;
4323 return 0;
4324}
4325#else /* CONFIG_PM */
4326
4327int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4328 struct usb_device *udev, enum usb3_link_state state)
4329{
4330 return USB3_LPM_DISABLED;
4331}
4332
4333int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4334 struct usb_device *udev, enum usb3_link_state state)
4335{
4336 return 0;
4337}
4338#endif /* CONFIG_PM */
4339
4340/*-------------------------------------------------------------------------*/
4341
3866/* Once a hub descriptor is fetched for a device, we need to update the xHC's 4342/* Once a hub descriptor is fetched for a device, we need to update the xHC's
3867 * internal data structures for the device. 4343 * internal data structures for the device.
3868 */ 4344 */
@@ -4090,7 +4566,6 @@ static int __init xhci_hcd_init(void)
4090 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8); 4566 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
4091 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */ 4567 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
4092 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8); 4568 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
4093 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
4094 return 0; 4569 return 0;
4095unreg_pci: 4570unreg_pci:
4096 xhci_unregister_pci(); 4571 xhci_unregister_pci();