pciehp_core.c 7.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11
/*
 * PCI Express Hot Plug Controller Driver
 *
 * Copyright (C) 1995,2001 Compaq Computer Corporation
 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
 * Copyright (C) 2001 IBM Corp.
 * Copyright (C) 2003-2004 Intel Corporation
 *
 * All rights reserved.
 *
12
 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
L
Linus Torvalds 已提交
13
 *
14 15 16 17
 * Authors:
 *   Dan Zink <dan.zink@compaq.com>
 *   Greg Kroah-Hartman <greg@kroah.com>
 *   Dely Sy <dely.l.sy@intel.com>"
L
Linus Torvalds 已提交
18 19 20 21
 */

#include <linux/moduleparam.h>
#include <linux/kernel.h>
22
#include <linux/slab.h>
L
Linus Torvalds 已提交
23 24 25 26
#include <linux/types.h>
#include <linux/pci.h>
#include "pciehp.h"

27 28
#include "../pci.h"

L
Linus Torvalds 已提交
29
/* Global variables */
30 31
bool pciehp_debug;
bool pciehp_poll_mode;
L
Linus Torvalds 已提交
32 33
int pciehp_poll_time;

34 35 36 37
/*
 * not really modular, but the easiest way to keep compat with existing
 * bootargs behaviour is to continue using module_param here.
 */
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46
module_param(pciehp_debug, bool, 0644);
module_param(pciehp_poll_mode, bool, 0644);
module_param(pciehp_poll_time, int, 0644);
MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");

#define PCIE_MODULE_NAME "pciehp"

B
Bogicevic Sasa 已提交
47 48 49 50
static int set_attention_status(struct hotplug_slot *slot, u8 value);
static int get_power_status(struct hotplug_slot *slot, u8 *value);
static int get_latch_status(struct hotplug_slot *slot, u8 *value);
static int get_adapter_status(struct hotplug_slot *slot, u8 *value);
L
Linus Torvalds 已提交
51

52
static int init_slot(struct controller *ctrl)
L
Linus Torvalds 已提交
53
{
L
Lukas Wunner 已提交
54
	struct hotplug_slot_ops *ops;
A
Alex Chiang 已提交
55
	char name[SLOT_NAME_SIZE];
L
Lukas Wunner 已提交
56
	int retval;
57

58 59 60
	/* Setup hotplug slot ops */
	ops = kzalloc(sizeof(*ops), GFP_KERNEL);
	if (!ops)
L
Lukas Wunner 已提交
61
		return -ENOMEM;
B
Bjorn Helgaas 已提交
62

63 64
	ops->enable_slot = pciehp_sysfs_enable_slot;
	ops->disable_slot = pciehp_sysfs_disable_slot;
65 66
	ops->get_power_status = get_power_status;
	ops->get_adapter_status = get_adapter_status;
67
	ops->reset_slot = pciehp_reset_slot;
68 69 70
	if (MRL_SENS(ctrl))
		ops->get_latch_status = get_latch_status;
	if (ATTN_LED(ctrl)) {
71
		ops->get_attention_status = pciehp_get_attention_status;
72
		ops->set_attention_status = set_attention_status;
73 74 75
	} else if (ctrl->pcie->port->hotplug_user_indicators) {
		ops->get_attention_status = pciehp_get_raw_indicator_status;
		ops->set_attention_status = pciehp_set_raw_indicator_status;
76 77
	}

78
	/* register this slot with the hotplug pci core */
L
Lukas Wunner 已提交
79
	ctrl->hotplug_slot.ops = ops;
80
	snprintf(name, SLOT_NAME_SIZE, "%u", PSN(ctrl));
81

L
Lukas Wunner 已提交
82
	retval = pci_hp_initialize(&ctrl->hotplug_slot,
83
				   ctrl->pcie->port->subordinate, 0, name);
84
	if (retval) {
L
Lukas Wunner 已提交
85
		ctrl_err(ctrl, "pci_hp_initialize failed: error %d\n", retval);
86
		kfree(ops);
L
Linus Torvalds 已提交
87
	}
K
Kenji Kaneshige 已提交
88
	return retval;
L
Linus Torvalds 已提交
89 90
}

91
static void cleanup_slot(struct controller *ctrl)
L
Linus Torvalds 已提交
92
{
L
Lukas Wunner 已提交
93
	struct hotplug_slot *hotplug_slot = &ctrl->hotplug_slot;
94

95
	pci_hp_destroy(hotplug_slot);
96
	kfree(hotplug_slot->ops);
L
Linus Torvalds 已提交
97 98 99 100 101 102 103
}

/*
 * set_attention_status - Turns the Amber LED for a slot on, off or blink
 */
static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
{
L
Lukas Wunner 已提交
104
	struct controller *ctrl = to_ctrl(hotplug_slot);
105
	struct pci_dev *pdev = ctrl->pcie->port;
L
Linus Torvalds 已提交
106

107
	pci_config_pm_runtime_get(pdev);
108
	pciehp_set_attention_status(ctrl, status);
109
	pci_config_pm_runtime_put(pdev);
110
	return 0;
L
Linus Torvalds 已提交
111 112 113 114
}

static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
{
L
Lukas Wunner 已提交
115
	struct controller *ctrl = to_ctrl(hotplug_slot);
116
	struct pci_dev *pdev = ctrl->pcie->port;
L
Linus Torvalds 已提交
117

118
	pci_config_pm_runtime_get(pdev);
119
	pciehp_get_power_status(ctrl, value);
120
	pci_config_pm_runtime_put(pdev);
121
	return 0;
L
Linus Torvalds 已提交
122 123 124 125
}

static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
{
L
Lukas Wunner 已提交
126
	struct controller *ctrl = to_ctrl(hotplug_slot);
127
	struct pci_dev *pdev = ctrl->pcie->port;
L
Linus Torvalds 已提交
128

129
	pci_config_pm_runtime_get(pdev);
130
	pciehp_get_latch_status(ctrl, value);
131
	pci_config_pm_runtime_put(pdev);
132
	return 0;
L
Linus Torvalds 已提交
133 134 135 136
}

static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
{
L
Lukas Wunner 已提交
137
	struct controller *ctrl = to_ctrl(hotplug_slot);
138
	struct pci_dev *pdev = ctrl->pcie->port;
L
Linus Torvalds 已提交
139

140
	pci_config_pm_runtime_get(pdev);
141
	*value = pciehp_card_present_or_link_active(ctrl);
142
	pci_config_pm_runtime_put(pdev);
143
	return 0;
L
Linus Torvalds 已提交
144 145
}

146 147 148 149 150 151 152 153 154 155 156 157
/**
 * pciehp_check_presence() - synthesize event if presence has changed
 *
 * On probe and resume, an explicit presence check is necessary to bring up an
 * occupied slot or bring down an unoccupied slot.  This can't be triggered by
 * events in the Slot Status register, they may be stale and are therefore
 * cleared.  Secondly, sending an interrupt for "events that occur while
 * interrupt generation is disabled [when] interrupt generation is subsequently
 * enabled" is optional per PCIe r4.0, sec 6.7.3.4.
 */
static void pciehp_check_presence(struct controller *ctrl)
{
158
	bool occupied;
159 160

	down_read(&ctrl->reset_lock);
161
	mutex_lock(&ctrl->state_lock);
162

163
	occupied = pciehp_card_present_or_link_active(ctrl);
164 165 166 167
	if ((occupied && (ctrl->state == OFF_STATE ||
			  ctrl->state == BLINKINGON_STATE)) ||
	    (!occupied && (ctrl->state == ON_STATE ||
			   ctrl->state == BLINKINGOFF_STATE)))
168 169
		pciehp_request(ctrl, PCI_EXP_SLTSTA_PDC);

170
	mutex_unlock(&ctrl->state_lock);
171 172 173
	up_read(&ctrl->reset_lock);
}

174
static int pciehp_probe(struct pcie_device *dev)
L
Linus Torvalds 已提交
175 176 177
{
	int rc;
	struct controller *ctrl;
178

179 180 181
	/* If this is not a "hotplug" service, we have no business here. */
	if (dev->service != PCIE_PORT_SERVICE_HP)
		return -ENODEV;
182

183 184 185 186
	if (!dev->port->subordinate) {
		/* Can happen if we run out of bus numbers during probe */
		dev_err(&dev->device,
			"Hotplug bridge without secondary bus, ignoring\n");
187
		return -ENODEV;
188 189
	}

190
	ctrl = pcie_init(dev);
L
Linus Torvalds 已提交
191
	if (!ctrl) {
192
		dev_err(&dev->device, "Controller initialization failed\n");
193
		return -ENODEV;
L
Linus Torvalds 已提交
194
	}
K
Kenji Kaneshige 已提交
195
	set_service_data(dev, ctrl);
L
Linus Torvalds 已提交
196 197

	/* Setup the slot information structures */
198
	rc = init_slot(ctrl);
L
Linus Torvalds 已提交
199
	if (rc) {
A
Alex Chiang 已提交
200
		if (rc == -EBUSY)
201
			ctrl_warn(ctrl, "Slot already registered by another hotplug driver\n");
A
Alex Chiang 已提交
202
		else
203
			ctrl_err(ctrl, "Slot initialization failed (%d)\n", rc);
204
		goto err_out_release_ctlr;
L
Linus Torvalds 已提交
205 206
	}

207 208 209
	/* Enable events after we have setup the data structures */
	rc = pcie_init_notification(ctrl);
	if (rc) {
210
		ctrl_err(ctrl, "Notification initialization failed (%d)\n", rc);
211
		goto err_out_free_ctrl_slot;
212 213
	}

214
	/* Publish to user space */
L
Lukas Wunner 已提交
215
	rc = pci_hp_add(&ctrl->hotplug_slot);
216 217 218 219 220
	if (rc) {
		ctrl_err(ctrl, "Publication to user space failed (%d)\n", rc);
		goto err_out_shutdown_notification;
	}

221
	pciehp_check_presence(ctrl);
L
Linus Torvalds 已提交
222 223 224

	return 0;

225 226
err_out_shutdown_notification:
	pcie_shutdown_notification(ctrl);
L
Linus Torvalds 已提交
227
err_out_free_ctrl_slot:
228
	cleanup_slot(ctrl);
229
err_out_release_ctlr:
K
Kenji Kaneshige 已提交
230
	pciehp_release_ctrl(ctrl);
L
Linus Torvalds 已提交
231 232 233
	return -ENODEV;
}

K
Kenji Kaneshige 已提交
234
static void pciehp_remove(struct pcie_device *dev)
L
Linus Torvalds 已提交
235
{
K
Kenji Kaneshige 已提交
236
	struct controller *ctrl = get_service_data(dev);
L
Linus Torvalds 已提交
237

L
Lukas Wunner 已提交
238
	pci_hp_del(&ctrl->hotplug_slot);
239
	pcie_shutdown_notification(ctrl);
240
	cleanup_slot(ctrl);
K
Kenji Kaneshige 已提交
241
	pciehp_release_ctrl(ctrl);
L
Linus Torvalds 已提交
242 243 244
}

#ifdef CONFIG_PM
R
Ryan Desfosses 已提交
245
static int pciehp_suspend(struct pcie_device *dev)
L
Linus Torvalds 已提交
246 247 248 249
{
	return 0;
}

250 251 252 253
static int pciehp_resume_noirq(struct pcie_device *dev)
{
	struct controller *ctrl = get_service_data(dev);

254 255 256 257
	/* pci_restore_state() just wrote to the Slot Control register */
	ctrl->cmd_started = jiffies;
	ctrl->cmd_busy = true;

258
	/* clear spurious events from rediscovery of inserted card */
259
	if (ctrl->state == ON_STATE || ctrl->state == BLINKINGOFF_STATE)
260 261 262 263 264
		pcie_clear_hotplug_events(ctrl);

	return 0;
}

R
Ryan Desfosses 已提交
265
static int pciehp_resume(struct pcie_device *dev)
L
Linus Torvalds 已提交
266
{
267
	struct controller *ctrl = get_service_data(dev);
268

269
	pciehp_check_presence(ctrl);
270

L
Linus Torvalds 已提交
271 272
	return 0;
}
273
#endif /* PM */
L
Linus Torvalds 已提交
274 275

static struct pcie_port_service_driver hpdriver_portdrv = {
276
	.name		= PCIE_MODULE_NAME,
277 278
	.port_type	= PCIE_ANY_PORT,
	.service	= PCIE_PORT_SERVICE_HP,
L
Linus Torvalds 已提交
279 280 281 282 283 284

	.probe		= pciehp_probe,
	.remove		= pciehp_remove,

#ifdef	CONFIG_PM
	.suspend	= pciehp_suspend,
285
	.resume_noirq	= pciehp_resume_noirq,
L
Linus Torvalds 已提交
286 287 288 289
	.resume		= pciehp_resume,
#endif	/* PM */
};

290
int __init pcie_hp_init(void)
L
Linus Torvalds 已提交
291 292 293
{
	int retval = 0;

294
	retval = pcie_port_service_register(&hpdriver_portdrv);
295
	dbg("pcie_port_service_register = %d\n", retval);
296
	if (retval)
297
		dbg("Failure to register service\n");
298

L
Linus Torvalds 已提交
299 300
	return retval;
}