blob: 9acd1997c6fef6ec037fa3cf046ba4075ac1557d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PCI HotPlug Controller Core
3 *
4 * Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2001-2002 IBM Corp.
6 *
7 * All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
17 * NON INFRINGEMENT. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
Kristen Carlson Accardifb5f4d72006-09-29 10:30:27 -070024 * Send feedback to <kristen.c.accardi@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 *
26 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/kernel.h>
31#include <linux/types.h>
32#include <linux/list.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070033#include <linux/kobject.h>
34#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/init.h>
37#include <linux/mount.h>
38#include <linux/namei.h>
Kenji Kaneshige95cb9092008-10-20 17:40:57 -060039#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070041#include <linux/pci_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
Alex Chiangf46753c2008-06-10 15:28:50 -060043#include "../pci.h"
Bjorn Helgaasc3139ba2013-04-15 10:44:18 -060044#include "cpci_hotplug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define MY_NAME "pci_hotplug"
47
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080048#define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt, MY_NAME, __func__, ## arg); } while (0)
49#define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME, ## arg)
50#define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME, ## arg)
51#define warn(format, arg...) printk(KERN_WARNING "%s: " format, MY_NAME, ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53
54/* local variables */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103055static bool debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define DRIVER_VERSION "0.5"
58#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
59#define DRIVER_DESC "PCI Hot Plug PCI Core"
60
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static LIST_HEAD(pci_hotplug_slot_list);
Kenji Kaneshige95cb9092008-10-20 17:40:57 -060063static DEFINE_MUTEX(pci_hp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* Weee, fun with macros... */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040066#define GET_STATUS(name, type) \
67static int get_##name(struct hotplug_slot *slot, type *value) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{ \
69 struct hotplug_slot_ops *ops = slot->ops; \
70 int retval = 0; \
Kenji Kaneshigebd1d9852008-09-29 17:37:05 +090071 if (!try_module_get(ops->owner)) \
Zhao, Yuc8761fe2008-09-22 14:26:05 +080072 return -ENODEV; \
73 if (ops->get_##name) \
74 retval = ops->get_##name(slot, value); \
75 else \
76 *value = slot->info->name; \
77 module_put(ops->owner); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return retval; \
79}
80
81GET_STATUS(power_status, u8)
82GET_STATUS(attention_status, u8)
83GET_STATUS(latch_status, u8)
84GET_STATUS(adapter_status, u8)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Yijing Wanga6ed1f42015-06-19 15:57:44 +080086static ssize_t power_read_file(struct pci_slot *pci_slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 int retval;
89 u8 value;
90
Yijing Wanga6ed1f42015-06-19 15:57:44 +080091 retval = get_power_status(pci_slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (retval)
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040093 return retval;
94
95 return sprintf(buf, "%d\n", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Alex Chiangf46753c2008-06-10 15:28:50 -060098static ssize_t power_write_file(struct pci_slot *pci_slot, const char *buf,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040099 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Alex Chiangf46753c2008-06-10 15:28:50 -0600101 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 unsigned long lpower;
103 u8 power;
104 int retval = 0;
105
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400106 lpower = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 power = (u8)(lpower & 0xff);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400108 dbg("power = %d\n", power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 if (!try_module_get(slot->ops->owner)) {
111 retval = -ENODEV;
112 goto exit;
113 }
114 switch (power) {
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400115 case 0:
116 if (slot->ops->disable_slot)
117 retval = slot->ops->disable_slot(slot);
118 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400120 case 1:
121 if (slot->ops->enable_slot)
122 retval = slot->ops->enable_slot(slot);
123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400125 default:
126 err("Illegal value specified for power\n");
127 retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129 module_put(slot->ops->owner);
130
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700131exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (retval)
133 return retval;
134 return count;
135}
136
Alex Chiangf46753c2008-06-10 15:28:50 -0600137static struct pci_slot_attribute hotplug_slot_attr_power = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
139 .show = power_read_file,
140 .store = power_write_file
141};
142
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800143static ssize_t attention_read_file(struct pci_slot *pci_slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int retval;
146 u8 value;
147
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800148 retval = get_attention_status(pci_slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (retval)
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400150 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400152 return sprintf(buf, "%d\n", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800155static ssize_t attention_write_file(struct pci_slot *pci_slot, const char *buf,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400156 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800158 struct hotplug_slot_ops *ops = pci_slot->hotplug->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 unsigned long lattention;
160 u8 attention;
161 int retval = 0;
162
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400163 lattention = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 attention = (u8)(lattention & 0xff);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400165 dbg(" - attention = %d\n", attention);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Alex Chiangf46753c2008-06-10 15:28:50 -0600167 if (!try_module_get(ops->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 retval = -ENODEV;
169 goto exit;
170 }
Alex Chiangf46753c2008-06-10 15:28:50 -0600171 if (ops->set_attention_status)
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800172 retval = ops->set_attention_status(pci_slot->hotplug, attention);
Alex Chiangf46753c2008-06-10 15:28:50 -0600173 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700175exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (retval)
177 return retval;
178 return count;
179}
180
Alex Chiangf46753c2008-06-10 15:28:50 -0600181static struct pci_slot_attribute hotplug_slot_attr_attention = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
183 .show = attention_read_file,
184 .store = attention_write_file
185};
186
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800187static ssize_t latch_read_file(struct pci_slot *pci_slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 int retval;
190 u8 value;
191
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800192 retval = get_latch_status(pci_slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (retval)
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400194 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400196 return sprintf(buf, "%d\n", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Alex Chiangf46753c2008-06-10 15:28:50 -0600199static struct pci_slot_attribute hotplug_slot_attr_latch = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
201 .show = latch_read_file,
202};
203
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800204static ssize_t presence_read_file(struct pci_slot *pci_slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 int retval;
207 u8 value;
208
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800209 retval = get_adapter_status(pci_slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (retval)
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400211 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400213 return sprintf(buf, "%d\n", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Alex Chiangf46753c2008-06-10 15:28:50 -0600216static struct pci_slot_attribute hotplug_slot_attr_presence = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
218 .show = presence_read_file,
219};
220
Alex Chiangf46753c2008-06-10 15:28:50 -0600221static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400222 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Alex Chiangf46753c2008-06-10 15:28:50 -0600224 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 unsigned long ltest;
226 u32 test;
227 int retval = 0;
228
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800229 ltest = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 test = (u32)(ltest & 0xffffffff);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400231 dbg("test = %d\n", test);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (!try_module_get(slot->ops->owner)) {
234 retval = -ENODEV;
235 goto exit;
236 }
237 if (slot->ops->hardware_test)
238 retval = slot->ops->hardware_test(slot, test);
239 module_put(slot->ops->owner);
240
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700241exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (retval)
243 return retval;
244 return count;
245}
246
Alex Chiangf46753c2008-06-10 15:28:50 -0600247static struct pci_slot_attribute hotplug_slot_attr_test = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
249 .store = test_write_file
250};
251
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900252static bool has_power_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Alex Chiangf46753c2008-06-10 15:28:50 -0600254 struct hotplug_slot *slot = pci_slot->hotplug;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900257 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if ((slot->ops->enable_slot) ||
259 (slot->ops->disable_slot) ||
260 (slot->ops->get_power_status))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900261 return true;
262 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900265static bool has_attention_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Alex Chiangf46753c2008-06-10 15:28:50 -0600267 struct hotplug_slot *slot = pci_slot->hotplug;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900270 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if ((slot->ops->set_attention_status) ||
272 (slot->ops->get_attention_status))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900273 return true;
274 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900277static bool has_latch_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Alex Chiangf46753c2008-06-10 15:28:50 -0600279 struct hotplug_slot *slot = pci_slot->hotplug;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900282 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (slot->ops->get_latch_status)
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900284 return true;
285 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900288static bool has_adapter_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Alex Chiangf46753c2008-06-10 15:28:50 -0600290 struct hotplug_slot *slot = pci_slot->hotplug;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900293 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (slot->ops->get_adapter_status)
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900295 return true;
296 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900299static bool has_test_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Alex Chiangf46753c2008-06-10 15:28:50 -0600301 struct hotplug_slot *slot = pci_slot->hotplug;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900304 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (slot->ops->hardware_test)
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900306 return true;
307 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800310static int fs_add_slot(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700312 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900314 /* Create symbolic link to the hotplug driver module */
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800315 pci_hp_create_module_link(pci_slot);
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900316
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800317 if (has_power_file(pci_slot)) {
318 retval = sysfs_create_file(&pci_slot->kobj,
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900319 &hotplug_slot_attr_power.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700320 if (retval)
321 goto exit_power;
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800324 if (has_attention_file(pci_slot)) {
325 retval = sysfs_create_file(&pci_slot->kobj,
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700326 &hotplug_slot_attr_attention.attr);
327 if (retval)
328 goto exit_attention;
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800331 if (has_latch_file(pci_slot)) {
332 retval = sysfs_create_file(&pci_slot->kobj,
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700333 &hotplug_slot_attr_latch.attr);
334 if (retval)
335 goto exit_latch;
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800338 if (has_adapter_file(pci_slot)) {
339 retval = sysfs_create_file(&pci_slot->kobj,
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700340 &hotplug_slot_attr_presence.attr);
341 if (retval)
342 goto exit_adapter;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800345 if (has_test_file(pci_slot)) {
346 retval = sysfs_create_file(&pci_slot->kobj,
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700347 &hotplug_slot_attr_test.attr);
348 if (retval)
349 goto exit_test;
350 }
351
352 goto exit;
353
354exit_test:
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800355 if (has_adapter_file(pci_slot))
356 sysfs_remove_file(&pci_slot->kobj,
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900357 &hotplug_slot_attr_presence.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700358exit_adapter:
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800359 if (has_latch_file(pci_slot))
360 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700361exit_latch:
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800362 if (has_attention_file(pci_slot))
363 sysfs_remove_file(&pci_slot->kobj,
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900364 &hotplug_slot_attr_attention.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700365exit_attention:
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800366 if (has_power_file(pci_slot))
367 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700368exit_power:
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800369 pci_hp_remove_module_link(pci_slot);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700370exit:
371 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800374static void fs_remove_slot(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800376 if (has_power_file(pci_slot))
377 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800379 if (has_attention_file(pci_slot))
380 sysfs_remove_file(&pci_slot->kobj,
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900381 &hotplug_slot_attr_attention.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800383 if (has_latch_file(pci_slot))
384 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800386 if (has_adapter_file(pci_slot))
387 sysfs_remove_file(&pci_slot->kobj,
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900388 &hotplug_slot_attr_presence.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800390 if (has_test_file(pci_slot))
391 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_test.attr);
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900392
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800393 pci_hp_remove_module_link(pci_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400396static struct hotplug_slot *get_slot_from_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct hotplug_slot *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Geliang Tang2ac83cc2015-12-12 21:36:57 +0800400 list_for_each_entry(slot, &pci_hotplug_slot_list, slot_list) {
Alex Chiang58319b82008-10-20 17:41:58 -0600401 if (strcmp(hotplug_slot_name(slot), name) == 0)
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600402 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600404 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407/**
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900408 * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
Jesse Barnes65b943f2008-06-25 15:27:34 -0700409 * @bus: bus this slot is on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * @slot: pointer to the &struct hotplug_slot to register
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900411 * @devnr: device number
Alex Chiang1359f272008-10-20 17:40:42 -0600412 * @name: name registered with kobject core
Randy Dunlap503998c2009-06-24 09:18:14 -0700413 * @owner: caller module owner
414 * @mod_name: caller module name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 *
416 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
417 * userspace interaction to the slot.
418 *
419 * Returns 0 if successful, anything else for an error.
420 */
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900421int __pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus,
422 int devnr, const char *name,
423 struct module *owner, const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 int result;
Alex Chiangf46753c2008-06-10 15:28:50 -0600426 struct pci_slot *pci_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if (slot == NULL)
429 return -ENODEV;
430 if ((slot->info == NULL) || (slot->ops == NULL))
431 return -EINVAL;
432 if (slot->release == NULL) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400433 dbg("Why are you trying to register a hotplug slot without a proper release function?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return -EINVAL;
435 }
436
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900437 slot->ops->owner = owner;
438 slot->ops->mod_name = mod_name;
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600439
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900440 mutex_lock(&pci_hp_mutex);
Alex Chiang8344b562008-06-10 15:30:42 -0600441 /*
442 * No problems if we call this interface from both ACPI_PCI_SLOT
443 * driver and call it here again. If we've already created the
444 * pci_slot, the interface will simply bump the refcount.
445 */
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900446 pci_slot = pci_create_slot(bus, devnr, name, slot);
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600447 if (IS_ERR(pci_slot)) {
448 result = PTR_ERR(pci_slot);
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600449 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
Greg Kroah-Hartman64dbcac2007-12-17 15:54:39 -0400451
Alex Chiangf46753c2008-06-10 15:28:50 -0600452 slot->pci_slot = pci_slot;
453 pci_slot->hotplug = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Alex Chiangf46753c2008-06-10 15:28:50 -0600455 list_add(&slot->slot_list, &pci_hotplug_slot_list);
Alex Chiangf46753c2008-06-10 15:28:50 -0600456
457 result = fs_add_slot(pci_slot);
458 kobject_uevent(&pci_slot->kobj, KOBJ_ADD);
Alex Chiang1359f272008-10-20 17:40:42 -0600459 dbg("Added slot %s to the list\n", name);
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600460out:
461 mutex_unlock(&pci_hp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return result;
463}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600464EXPORT_SYMBOL_GPL(__pci_hp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466/**
467 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800468 * @slot: pointer to the &struct hotplug_slot to deregister
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
470 * The @slot must have been registered with the pci hotplug subsystem
471 * previously with a call to pci_hp_register().
472 *
473 * Returns 0 if successful, anything else for an error.
474 */
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800475int pci_hp_deregister(struct hotplug_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct hotplug_slot *temp;
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800478 struct pci_slot *pci_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800480 if (!slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return -ENODEV;
482
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600483 mutex_lock(&pci_hp_mutex);
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800484 temp = get_slot_from_name(hotplug_slot_name(slot));
485 if (temp != slot) {
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600486 mutex_unlock(&pci_hp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return -ENODEV;
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800490 list_del(&slot->slot_list);
Alex Chiangf46753c2008-06-10 15:28:50 -0600491
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800492 pci_slot = slot->pci_slot;
493 fs_remove_slot(pci_slot);
494 dbg("Removed slot %s from the list\n", hotplug_slot_name(slot));
Alex Chiangf46753c2008-06-10 15:28:50 -0600495
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800496 slot->release(slot);
497 pci_slot->hotplug = NULL;
498 pci_destroy_slot(pci_slot);
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600499 mutex_unlock(&pci_hp_mutex);
Alex Chiangf46753c2008-06-10 15:28:50 -0600500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return 0;
502}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600503EXPORT_SYMBOL_GPL(pci_hp_deregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505/**
506 * pci_hp_change_slot_info - changes the slot's information structure in the core
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800507 * @slot: pointer to the slot whose info has changed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * @info: pointer to the info copy into the slot's info structure
509 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700510 * @slot must have been registered with the pci
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * hotplug subsystem previously with a call to pci_hp_register().
512 *
513 * Returns 0 if successful, anything else for an error.
514 */
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800515int pci_hp_change_slot_info(struct hotplug_slot *slot,
Bjorn Helgaasd67aed62013-04-12 11:18:07 -0600516 struct hotplug_slot_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800518 if (!slot || !info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return -ENODEV;
520
Yijing Wanga6ed1f42015-06-19 15:57:44 +0800521 memcpy(slot->info, info, sizeof(struct hotplug_slot_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 return 0;
524}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600525EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400527static int __init pci_hotplug_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 int result;
Greg Kroah-Hartman0fed80f2007-11-01 19:41:16 -0700530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 result = cpci_hotplug_init(debug);
532 if (result) {
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400533 err("cpci_hotplug_init with error %d\n", result);
534 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400537 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return result;
539}
540
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400541static void __exit pci_hotplug_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 cpci_hotplug_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546module_init(pci_hotplug_init);
547module_exit(pci_hotplug_exit);
548
549MODULE_AUTHOR(DRIVER_AUTHOR);
550MODULE_DESCRIPTION(DRIVER_DESC);
551MODULE_LICENSE("GPL");
552module_param(debug, bool, 0644);
553MODULE_PARM_DESC(debug, "Debugging mode enabled or not");