blob: c38e392d63e4b2037b1c1cbb879b3d1239ec8a80 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PCI Express Hot Plug Controller Driver
3 *
4 * Copyright (C) 1995,2001 Compaq Computer Corporation
5 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2001 IBM Corp.
7 * Copyright (C) 2003-2004 Intel Corporation
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19 * NON INFRINGEMENT. See the GNU General Public License for more
20 * details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
Kristen Accardi8cf4c192005-08-16 15:16:10 -070026 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
Bjorn Helgaas5fbeef62016-11-15 07:57:30 -060028 * Authors:
29 * Dan Zink <dan.zink@compaq.com>
30 * Greg Kroah-Hartman <greg@kroah.com>
31 * Dely Sy <dely.l.sy@intel.com>"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/moduleparam.h>
35#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "pciehp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/interrupt.h>
Kristen Carlson Accardi34d03412007-01-09 13:02:36 -080041#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* Global variables */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103044bool pciehp_debug;
45bool pciehp_poll_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046int pciehp_poll_time;
Stephen Hemminger0b950f02014-01-10 17:14:48 -070047static bool pciehp_force;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Paul Gortmaker70626d82016-08-24 16:57:52 -040049/*
50 * not really modular, but the easiest way to keep compat with existing
51 * bootargs behaviour is to continue using module_param here.
52 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053module_param(pciehp_debug, bool, 0644);
54module_param(pciehp_poll_mode, bool, 0644);
55module_param(pciehp_poll_time, int, 0644);
rajesh.shah@intel.coma3a45ec2005-10-31 16:20:12 -080056module_param(pciehp_force, bool, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
58MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
59MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
Rafael J. Wysocki28eb5f22010-08-21 22:02:38 +020060MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if OSHP is missing");
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define PCIE_MODULE_NAME "pciehp"
63
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080064static int set_attention_status(struct hotplug_slot *slot, u8 value);
65static int enable_slot(struct hotplug_slot *slot);
66static int disable_slot(struct hotplug_slot *slot);
67static int get_power_status(struct hotplug_slot *slot, u8 *value);
68static int get_attention_status(struct hotplug_slot *slot, u8 *value);
69static int get_latch_status(struct hotplug_slot *slot, u8 *value);
70static int get_adapter_status(struct hotplug_slot *slot, u8 *value);
71static int reset_slot(struct hotplug_slot *slot, int probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Dely Syb3082402005-04-28 18:08:53 -070073/**
74 * release_slot - free up the memory used by a slot
75 * @hotplug_slot: slot to free
76 */
77static void release_slot(struct hotplug_slot *hotplug_slot)
78{
Lukas Wunner2f27dfa2018-07-19 17:27:32 -050079 struct slot *slot = hotplug_slot->private;
80
81 /* queued work needs hotplug_slot name */
82 cancel_delayed_work(&slot->work);
83 drain_workqueue(slot->wq);
84
Kenji Kaneshige586f1d62009-10-05 17:41:37 +090085 kfree(hotplug_slot->ops);
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +090086 kfree(hotplug_slot->info);
87 kfree(hotplug_slot);
Kenji Kaneshigea0b17252006-12-21 17:01:02 -080088}
89
Kenji Kaneshige8720d272009-09-15 17:24:46 +090090static int init_slot(struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Kenji Kaneshige8720d272009-09-15 17:24:46 +090092 struct slot *slot = ctrl->slot;
93 struct hotplug_slot *hotplug = NULL;
94 struct hotplug_slot_info *info = NULL;
Kenji Kaneshige586f1d62009-10-05 17:41:37 +090095 struct hotplug_slot_ops *ops = NULL;
Alex Chiange1acb242008-10-20 17:41:38 -060096 char name[SLOT_NAME_SIZE];
Kenji Kaneshigea0b17252006-12-21 17:01:02 -080097 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Kenji Kaneshige8720d272009-09-15 17:24:46 +090099 hotplug = kzalloc(sizeof(*hotplug), GFP_KERNEL);
100 if (!hotplug)
101 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900103 info = kzalloc(sizeof(*info), GFP_KERNEL);
104 if (!info)
105 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Kenji Kaneshige586f1d62009-10-05 17:41:37 +0900107 /* Setup hotplug slot ops */
108 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
109 if (!ops)
110 goto out;
Bjorn Helgaas9cad7f52014-02-11 15:26:29 -0700111
Kenji Kaneshige586f1d62009-10-05 17:41:37 +0900112 ops->enable_slot = enable_slot;
113 ops->disable_slot = disable_slot;
114 ops->get_power_status = get_power_status;
115 ops->get_adapter_status = get_adapter_status;
Alex Williamson2e35afa2013-08-08 14:09:37 -0600116 ops->reset_slot = reset_slot;
Kenji Kaneshige586f1d62009-10-05 17:41:37 +0900117 if (MRL_SENS(ctrl))
118 ops->get_latch_status = get_latch_status;
119 if (ATTN_LED(ctrl)) {
120 ops->get_attention_status = get_attention_status;
121 ops->set_attention_status = set_attention_status;
Keith Busch576243b2016-09-13 10:31:59 -0600122 } else if (ctrl->pcie->port->hotplug_user_indicators) {
123 ops->get_attention_status = pciehp_get_raw_indicator_status;
124 ops->set_attention_status = pciehp_set_raw_indicator_status;
Kenji Kaneshige586f1d62009-10-05 17:41:37 +0900125 }
126
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900127 /* register this slot with the hotplug pci core */
128 hotplug->info = info;
129 hotplug->private = slot;
130 hotplug->release = &release_slot;
Kenji Kaneshige586f1d62009-10-05 17:41:37 +0900131 hotplug->ops = ops;
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900132 slot->hotplug_slot = hotplug;
Kenji Kaneshige07a09692009-09-15 17:31:16 +0900133 snprintf(name, SLOT_NAME_SIZE, "%u", PSN(ctrl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900135 retval = pci_hp_register(hotplug,
Kenji Kaneshige385e2492009-09-15 17:30:14 +0900136 ctrl->pcie->port->subordinate, 0, name);
Kenji Kaneshige445f7982009-10-05 17:42:59 +0900137 if (retval)
Bjorn Helgaas3784e0c2015-06-15 16:28:29 -0500138 ctrl_err(ctrl, "pci_hp_register failed: error %d\n", retval);
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900139out:
140 if (retval) {
Kenji Kaneshige586f1d62009-10-05 17:41:37 +0900141 kfree(ops);
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900142 kfree(info);
143 kfree(hotplug);
144 }
Kenji Kaneshigea0b17252006-12-21 17:01:02 -0800145 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900148static void cleanup_slot(struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900150 pci_hp_deregister(ctrl->slot->hotplug_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * set_attention_status - Turns the Amber LED for a slot on, off or blink
155 */
156static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
157{
158 struct slot *slot = hotplug_slot->private;
159
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700160 pciehp_set_attention_status(slot, status);
161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164
165static int enable_slot(struct hotplug_slot *hotplug_slot)
166{
167 struct slot *slot = hotplug_slot->private;
168
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800169 return pciehp_sysfs_enable_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172
173static int disable_slot(struct hotplug_slot *hotplug_slot)
174{
175 struct slot *slot = hotplug_slot->private;
176
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800177 return pciehp_sysfs_disable_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
181{
182 struct slot *slot = hotplug_slot->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700184 pciehp_get_power_status(slot, value);
185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
189{
190 struct slot *slot = hotplug_slot->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700192 pciehp_get_attention_status(slot, value);
193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
197{
198 struct slot *slot = hotplug_slot->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700200 pciehp_get_latch_status(slot, value);
201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
205{
206 struct slot *slot = hotplug_slot->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700208 pciehp_get_adapter_status(slot, value);
209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Alex Williamson2e35afa2013-08-08 14:09:37 -0600212static int reset_slot(struct hotplug_slot *hotplug_slot, int probe)
213{
214 struct slot *slot = hotplug_slot->private;
215
Alex Williamson2e35afa2013-08-08 14:09:37 -0600216 return pciehp_reset_slot(slot, probe);
217}
218
Rafael J. Wysocki0516c8b2009-01-13 14:44:19 +0100219static int pciehp_probe(struct pcie_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 int rc;
222 struct controller *ctrl;
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900223 struct slot *slot;
Kenji Kaneshige8792e112009-10-05 17:46:43 +0900224 u8 occupied, poweron;
Kenji Kaneshige125c39f2008-05-28 14:57:30 +0900225
Rafael J. Wysockie705c292015-05-19 15:27:58 +0200226 /* If this is not a "hotplug" service, we have no business here. */
227 if (dev->service != PCIE_PORT_SERVICE_HP)
228 return -ENODEV;
Kenji Kaneshigea073a822007-08-09 16:09:35 -0700229
Andreas Noever62e44922014-06-09 23:03:32 +0200230 if (!dev->port->subordinate) {
231 /* Can happen if we run out of bus numbers during probe */
232 dev_err(&dev->device,
233 "Hotplug bridge without secondary bus, ignoring\n");
Rafael J. Wysocki2af31f42015-05-23 00:38:57 +0200234 return -ENODEV;
Andreas Noever62e44922014-06-09 23:03:32 +0200235 }
236
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900237 ctrl = pcie_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (!ctrl) {
Taku Izumi18b341b2008-10-23 11:47:32 +0900239 dev_err(&dev->device, "Controller initialization failed\n");
Rafael J. Wysocki2af31f42015-05-23 00:38:57 +0200240 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Kenji Kaneshigeb9708942008-06-26 20:06:24 +0900242 set_service_data(dev, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* Setup the slot information structures */
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900245 rc = init_slot(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (rc) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600247 if (rc == -EBUSY)
Ryan Desfosses227f0642014-04-18 20:13:50 -0400248 ctrl_warn(ctrl, "Slot already registered by another hotplug driver\n");
Alex Chiangf46753c2008-06-10 15:28:50 -0600249 else
Bjorn Helgaas3784e0c2015-06-15 16:28:29 -0500250 ctrl_err(ctrl, "Slot initialization failed (%d)\n", rc);
Kenji Kaneshigea8c2b632006-12-21 17:01:05 -0800251 goto err_out_release_ctlr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
Eric W. Biedermandbc7e1e2009-01-28 19:31:18 -0800254 /* Enable events after we have setup the data structures */
255 rc = pcie_init_notification(ctrl);
256 if (rc) {
Bjorn Helgaas3784e0c2015-06-15 16:28:29 -0500257 ctrl_err(ctrl, "Notification initialization failed (%d)\n", rc);
Kenji Kaneshige65b947b2009-10-05 17:43:29 +0900258 goto err_out_free_ctrl_slot;
Eric W. Biedermandbc7e1e2009-01-28 19:31:18 -0800259 }
260
Kenji Kaneshigedb9aaf02008-12-08 14:30:24 +0900261 /* Check if slot is occupied */
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900262 slot = ctrl->slot;
Kenji Kaneshige8792e112009-10-05 17:46:43 +0900263 pciehp_get_adapter_status(slot, &occupied);
264 pciehp_get_power_status(slot, &poweron);
Rajat Jain50b52fd2014-02-04 18:31:11 -0800265 if (occupied && pciehp_force) {
266 mutex_lock(&slot->hotplug_lock);
Kenji Kaneshige8792e112009-10-05 17:46:43 +0900267 pciehp_enable_slot(slot);
Rajat Jain50b52fd2014-02-04 18:31:11 -0800268 mutex_unlock(&slot->hotplug_lock);
269 }
Kenji Kaneshige8792e112009-10-05 17:46:43 +0900270 /* If empty slot's power status is on, turn power off */
271 if (!occupied && poweron && POWER_CTRL(ctrl))
272 pciehp_power_off_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return 0;
275
276err_out_free_ctrl_slot:
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900277 cleanup_slot(ctrl);
Kenji Kaneshigea8c2b632006-12-21 17:01:05 -0800278err_out_release_ctlr:
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900279 pciehp_release_ctrl(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return -ENODEV;
281}
282
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900283static void pciehp_remove(struct pcie_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Kenji Kaneshigeb9708942008-06-26 20:06:24 +0900285 struct controller *ctrl = get_service_data(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Lukas Wunner2f27dfa2018-07-19 17:27:32 -0500287 pcie_shutdown_notification(ctrl);
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900288 cleanup_slot(ctrl);
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900289 pciehp_release_ctrl(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292#ifdef CONFIG_PM
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400293static int pciehp_suspend(struct pcie_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0;
296}
297
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400298static int pciehp_resume(struct pcie_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Oliver Neukum87683e22012-09-07 23:28:30 +0200300 struct controller *ctrl;
301 struct slot *slot;
302 u8 status;
303
Oliver Neukum87683e22012-09-07 23:28:30 +0200304 ctrl = get_service_data(dev);
Mark Lordcd2fe832007-11-28 15:12:00 -0800305
Oliver Neukum87683e22012-09-07 23:28:30 +0200306 /* reinitialize the chipset's event detection logic */
Mika Westerberg226ffbf2018-05-23 17:14:39 -0500307 pcie_reenable_notification(ctrl);
Mark Lordcd2fe832007-11-28 15:12:00 -0800308
Oliver Neukum87683e22012-09-07 23:28:30 +0200309 slot = ctrl->slot;
Mark Lordcd2fe832007-11-28 15:12:00 -0800310
Oliver Neukum87683e22012-09-07 23:28:30 +0200311 /* Check if slot is occupied */
312 pciehp_get_adapter_status(slot, &status);
Rajat Jain50b52fd2014-02-04 18:31:11 -0800313 mutex_lock(&slot->hotplug_lock);
Oliver Neukum87683e22012-09-07 23:28:30 +0200314 if (status)
315 pciehp_enable_slot(slot);
316 else
317 pciehp_disable_slot(slot);
Rajat Jain50b52fd2014-02-04 18:31:11 -0800318 mutex_unlock(&slot->hotplug_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return 0;
320}
Rafael J. Wysocki3a3c2442009-02-15 22:32:48 +0100321#endif /* PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323static struct pcie_port_service_driver hpdriver_portdrv = {
Taku Izumi83e9ad52008-09-05 12:09:43 +0900324 .name = PCIE_MODULE_NAME,
Rafael J. Wysocki22106362009-01-13 14:46:46 +0100325 .port_type = PCIE_ANY_PORT,
326 .service = PCIE_PORT_SERVICE_HP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 .probe = pciehp_probe,
329 .remove = pciehp_remove,
330
331#ifdef CONFIG_PM
332 .suspend = pciehp_suspend,
333 .resume = pciehp_resume,
334#endif /* PM */
335};
336
337static int __init pcied_init(void)
338{
339 int retval = 0;
340
rajesh.shah@intel.coma8a2be92005-10-31 16:20:07 -0800341 retval = pcie_port_service_register(&hpdriver_portdrv);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700342 dbg("pcie_port_service_register = %d\n", retval);
Yijing Wangc2be6f92013-01-11 10:15:54 +0800343 if (retval)
Taku Izumi18b341b2008-10-23 11:47:32 +0900344 dbg("Failure to register service\n");
Yijing Wangc2be6f92013-01-11 10:15:54 +0800345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return retval;
347}
Paul Gortmaker70626d82016-08-24 16:57:52 -0400348device_initcall(pcied_init);