blob: 6d2eea93298fbbcf31afb03cba64b262b228e313 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define MY_NAME "pci_hotplug"
46
Harvey Harrison66bef8c2008-03-03 19:09:46 -080047#define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __func__ , ## arg); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
49#define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
50#define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
51
52
53/* local variables */
54static int debug;
55
56#define DRIVER_VERSION "0.5"
57#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
58#define DRIVER_DESC "PCI Hot Plug PCI Core"
59
60
61//////////////////////////////////////////////////////////////////
62
63static LIST_HEAD(pci_hotplug_slot_list);
Kenji Kaneshige95cb9092008-10-20 17:40:57 -060064static DEFINE_MUTEX(pci_hp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#ifdef CONFIG_HOTPLUG_PCI_CPCI
67extern int cpci_hotplug_init(int debug);
68extern void cpci_hotplug_exit(void);
69#else
70static inline int cpci_hotplug_init(int debug) { return 0; }
71static inline void cpci_hotplug_exit(void) { }
72#endif
73
74/* Weee, fun with macros... */
75#define GET_STATUS(name,type) \
76static int get_##name (struct hotplug_slot *slot, type *value) \
77{ \
78 struct hotplug_slot_ops *ops = slot->ops; \
79 int retval = 0; \
Kenji Kaneshigebd1d9852008-09-29 17:37:05 +090080 if (!try_module_get(ops->owner)) \
Zhao, Yuc8761fe2008-09-22 14:26:05 +080081 return -ENODEV; \
82 if (ops->get_##name) \
83 retval = ops->get_##name(slot, value); \
84 else \
85 *value = slot->info->name; \
86 module_put(ops->owner); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return retval; \
88}
89
90GET_STATUS(power_status, u8)
91GET_STATUS(attention_status, u8)
92GET_STATUS(latch_status, u8)
93GET_STATUS(adapter_status, u8)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Alex Chiangf46753c2008-06-10 15:28:50 -060095static ssize_t power_read_file(struct pci_slot *slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 int retval;
98 u8 value;
99
Alex Chiangf46753c2008-06-10 15:28:50 -0600100 retval = get_power_status(slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (retval)
102 goto exit;
103 retval = sprintf (buf, "%d\n", value);
104exit:
105 return retval;
106}
107
Alex Chiangf46753c2008-06-10 15:28:50 -0600108static ssize_t power_write_file(struct pci_slot *pci_slot, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 size_t count)
110{
Alex Chiangf46753c2008-06-10 15:28:50 -0600111 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 unsigned long lpower;
113 u8 power;
114 int retval = 0;
115
116 lpower = simple_strtoul (buf, NULL, 10);
117 power = (u8)(lpower & 0xff);
118 dbg ("power = %d\n", power);
119
120 if (!try_module_get(slot->ops->owner)) {
121 retval = -ENODEV;
122 goto exit;
123 }
124 switch (power) {
125 case 0:
126 if (slot->ops->disable_slot)
127 retval = slot->ops->disable_slot(slot);
128 break;
129
130 case 1:
131 if (slot->ops->enable_slot)
132 retval = slot->ops->enable_slot(slot);
133 break;
134
135 default:
136 err ("Illegal value specified for power\n");
137 retval = -EINVAL;
138 }
139 module_put(slot->ops->owner);
140
141exit:
142 if (retval)
143 return retval;
144 return count;
145}
146
Alex Chiangf46753c2008-06-10 15:28:50 -0600147static struct pci_slot_attribute hotplug_slot_attr_power = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
149 .show = power_read_file,
150 .store = power_write_file
151};
152
Alex Chiangf46753c2008-06-10 15:28:50 -0600153static ssize_t attention_read_file(struct pci_slot *slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 int retval;
156 u8 value;
157
Alex Chiangf46753c2008-06-10 15:28:50 -0600158 retval = get_attention_status(slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (retval)
160 goto exit;
Alex Chiangf46753c2008-06-10 15:28:50 -0600161 retval = sprintf(buf, "%d\n", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163exit:
164 return retval;
165}
166
Alex Chiangf46753c2008-06-10 15:28:50 -0600167static ssize_t attention_write_file(struct pci_slot *slot, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 size_t count)
169{
Alex Chiangf46753c2008-06-10 15:28:50 -0600170 struct hotplug_slot_ops *ops = slot->hotplug->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 unsigned long lattention;
172 u8 attention;
173 int retval = 0;
174
175 lattention = simple_strtoul (buf, NULL, 10);
176 attention = (u8)(lattention & 0xff);
177 dbg (" - attention = %d\n", attention);
178
Alex Chiangf46753c2008-06-10 15:28:50 -0600179 if (!try_module_get(ops->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 retval = -ENODEV;
181 goto exit;
182 }
Alex Chiangf46753c2008-06-10 15:28:50 -0600183 if (ops->set_attention_status)
184 retval = ops->set_attention_status(slot->hotplug, attention);
185 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187exit:
188 if (retval)
189 return retval;
190 return count;
191}
192
Alex Chiangf46753c2008-06-10 15:28:50 -0600193static struct pci_slot_attribute hotplug_slot_attr_attention = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
195 .show = attention_read_file,
196 .store = attention_write_file
197};
198
Alex Chiangf46753c2008-06-10 15:28:50 -0600199static ssize_t latch_read_file(struct pci_slot *slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 int retval;
202 u8 value;
203
Alex Chiangf46753c2008-06-10 15:28:50 -0600204 retval = get_latch_status(slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (retval)
206 goto exit;
207 retval = sprintf (buf, "%d\n", value);
208
209exit:
210 return retval;
211}
212
Alex Chiangf46753c2008-06-10 15:28:50 -0600213static struct pci_slot_attribute hotplug_slot_attr_latch = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
215 .show = latch_read_file,
216};
217
Alex Chiangf46753c2008-06-10 15:28:50 -0600218static ssize_t presence_read_file(struct pci_slot *slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 int retval;
221 u8 value;
222
Alex Chiangf46753c2008-06-10 15:28:50 -0600223 retval = get_adapter_status(slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (retval)
225 goto exit;
226 retval = sprintf (buf, "%d\n", value);
227
228exit:
229 return retval;
230}
231
Alex Chiangf46753c2008-06-10 15:28:50 -0600232static struct pci_slot_attribute hotplug_slot_attr_presence = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
234 .show = presence_read_file,
235};
236
Alex Chiangf46753c2008-06-10 15:28:50 -0600237static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 size_t count)
239{
Alex Chiangf46753c2008-06-10 15:28:50 -0600240 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned long ltest;
242 u32 test;
243 int retval = 0;
244
245 ltest = simple_strtoul (buf, NULL, 10);
246 test = (u32)(ltest & 0xffffffff);
247 dbg ("test = %d\n", test);
248
249 if (!try_module_get(slot->ops->owner)) {
250 retval = -ENODEV;
251 goto exit;
252 }
253 if (slot->ops->hardware_test)
254 retval = slot->ops->hardware_test(slot, test);
255 module_put(slot->ops->owner);
256
257exit:
258 if (retval)
259 return retval;
260 return count;
261}
262
Alex Chiangf46753c2008-06-10 15:28:50 -0600263static struct pci_slot_attribute hotplug_slot_attr_test = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
265 .store = test_write_file
266};
267
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900268static bool has_power_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Alex Chiangf46753c2008-06-10 15:28:50 -0600270 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900272 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if ((slot->ops->enable_slot) ||
274 (slot->ops->disable_slot) ||
275 (slot->ops->get_power_status))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900276 return true;
277 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900280static bool has_attention_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Alex Chiangf46753c2008-06-10 15:28:50 -0600282 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900284 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if ((slot->ops->set_attention_status) ||
286 (slot->ops->get_attention_status))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900287 return true;
288 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900291static bool has_latch_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Alex Chiangf46753c2008-06-10 15:28:50 -0600293 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900295 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (slot->ops->get_latch_status)
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900297 return true;
298 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900301static bool has_adapter_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Alex Chiangf46753c2008-06-10 15:28:50 -0600303 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900305 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (slot->ops->get_adapter_status)
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900307 return true;
308 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900311static bool has_test_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Alex Chiangf46753c2008-06-10 15:28:50 -0600313 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900315 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (slot->ops->hardware_test)
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900317 return true;
318 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Alex Chiangf46753c2008-06-10 15:28:50 -0600321static int fs_add_slot(struct pci_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700323 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900325 /* Create symbolic link to the hotplug driver module */
326 pci_hp_create_module_link(slot);
327
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900328 if (has_power_file(slot)) {
329 retval = sysfs_create_file(&slot->kobj,
330 &hotplug_slot_attr_power.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700331 if (retval)
332 goto exit_power;
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900335 if (has_attention_file(slot)) {
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700336 retval = sysfs_create_file(&slot->kobj,
337 &hotplug_slot_attr_attention.attr);
338 if (retval)
339 goto exit_attention;
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900342 if (has_latch_file(slot)) {
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700343 retval = sysfs_create_file(&slot->kobj,
344 &hotplug_slot_attr_latch.attr);
345 if (retval)
346 goto exit_latch;
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900349 if (has_adapter_file(slot)) {
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700350 retval = sysfs_create_file(&slot->kobj,
351 &hotplug_slot_attr_presence.attr);
352 if (retval)
353 goto exit_adapter;
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900356 if (has_test_file(slot)) {
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700357 retval = sysfs_create_file(&slot->kobj,
358 &hotplug_slot_attr_test.attr);
359 if (retval)
360 goto exit_test;
361 }
362
363 goto exit;
364
365exit_test:
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900366 if (has_adapter_file(slot))
367 sysfs_remove_file(&slot->kobj,
368 &hotplug_slot_attr_presence.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700369exit_adapter:
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900370 if (has_latch_file(slot))
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700371 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700372exit_latch:
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900373 if (has_attention_file(slot))
374 sysfs_remove_file(&slot->kobj,
375 &hotplug_slot_attr_attention.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700376exit_attention:
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900377 if (has_power_file(slot))
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700378 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
379exit_power:
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900380 pci_hp_remove_module_link(slot);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700381exit:
382 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Alex Chiangf46753c2008-06-10 15:28:50 -0600385static void fs_remove_slot(struct pci_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900387 if (has_power_file(slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
389
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900390 if (has_attention_file(slot))
391 sysfs_remove_file(&slot->kobj,
392 &hotplug_slot_attr_attention.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900394 if (has_latch_file(slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
396
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900397 if (has_adapter_file(slot))
398 sysfs_remove_file(&slot->kobj,
399 &hotplug_slot_attr_presence.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900401 if (has_test_file(slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_test.attr);
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900403
404 pci_hp_remove_module_link(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407static struct hotplug_slot *get_slot_from_name (const char *name)
408{
409 struct hotplug_slot *slot;
410 struct list_head *tmp;
411
412 list_for_each (tmp, &pci_hotplug_slot_list) {
413 slot = list_entry (tmp, struct hotplug_slot, slot_list);
Alex Chiang58319b82008-10-20 17:41:58 -0600414 if (strcmp(hotplug_slot_name(slot), name) == 0)
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600415 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600417 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420/**
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900421 * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
Jesse Barnes65b943f2008-06-25 15:27:34 -0700422 * @bus: bus this slot is on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * @slot: pointer to the &struct hotplug_slot to register
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900424 * @devnr: device number
Alex Chiang1359f272008-10-20 17:40:42 -0600425 * @name: name registered with kobject core
Randy Dunlap503998c2009-06-24 09:18:14 -0700426 * @owner: caller module owner
427 * @mod_name: caller module name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 *
429 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
430 * userspace interaction to the slot.
431 *
432 * Returns 0 if successful, anything else for an error.
433 */
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900434int __pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus,
435 int devnr, const char *name,
436 struct module *owner, const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 int result;
Alex Chiangf46753c2008-06-10 15:28:50 -0600439 struct pci_slot *pci_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (slot == NULL)
442 return -ENODEV;
443 if ((slot->info == NULL) || (slot->ops == NULL))
444 return -EINVAL;
445 if (slot->release == NULL) {
Joe Perchesa6f29a92007-11-19 17:48:29 -0800446 dbg("Why are you trying to register a hotplug slot "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 "without a proper release function?\n");
448 return -EINVAL;
449 }
450
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900451 slot->ops->owner = owner;
452 slot->ops->mod_name = mod_name;
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600453
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900454 mutex_lock(&pci_hp_mutex);
Alex Chiang8344b562008-06-10 15:30:42 -0600455 /*
456 * No problems if we call this interface from both ACPI_PCI_SLOT
457 * driver and call it here again. If we've already created the
458 * pci_slot, the interface will simply bump the refcount.
459 */
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900460 pci_slot = pci_create_slot(bus, devnr, name, slot);
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600461 if (IS_ERR(pci_slot)) {
462 result = PTR_ERR(pci_slot);
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600463 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Greg Kroah-Hartman64dbcac2007-12-17 15:54:39 -0400465
Alex Chiangf46753c2008-06-10 15:28:50 -0600466 slot->pci_slot = pci_slot;
467 pci_slot->hotplug = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Alex Chiangf46753c2008-06-10 15:28:50 -0600469 list_add(&slot->slot_list, &pci_hotplug_slot_list);
Alex Chiangf46753c2008-06-10 15:28:50 -0600470
471 result = fs_add_slot(pci_slot);
472 kobject_uevent(&pci_slot->kobj, KOBJ_ADD);
Alex Chiang1359f272008-10-20 17:40:42 -0600473 dbg("Added slot %s to the list\n", name);
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600474out:
475 mutex_unlock(&pci_hp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return result;
477}
478
479/**
480 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
Jesse Barnes65b943f2008-06-25 15:27:34 -0700481 * @hotplug: pointer to the &struct hotplug_slot to deregister
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 *
483 * The @slot must have been registered with the pci hotplug subsystem
484 * previously with a call to pci_hp_register().
485 *
486 * Returns 0 if successful, anything else for an error.
487 */
Alex Chiangf46753c2008-06-10 15:28:50 -0600488int pci_hp_deregister(struct hotplug_slot *hotplug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct hotplug_slot *temp;
Alex Chiangf46753c2008-06-10 15:28:50 -0600491 struct pci_slot *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Alex Chiangf46753c2008-06-10 15:28:50 -0600493 if (!hotplug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return -ENODEV;
495
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600496 mutex_lock(&pci_hp_mutex);
Alex Chiang58319b82008-10-20 17:41:58 -0600497 temp = get_slot_from_name(hotplug_slot_name(hotplug));
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600498 if (temp != hotplug) {
499 mutex_unlock(&pci_hp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return -ENODEV;
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Alex Chiangf46753c2008-06-10 15:28:50 -0600503 list_del(&hotplug->slot_list);
Alex Chiangf46753c2008-06-10 15:28:50 -0600504
505 slot = hotplug->pci_slot;
506 fs_remove_slot(slot);
Alex Chiang58319b82008-10-20 17:41:58 -0600507 dbg("Removed slot %s from the list\n", hotplug_slot_name(hotplug));
Alex Chiangf46753c2008-06-10 15:28:50 -0600508
509 hotplug->release(hotplug);
510 slot->hotplug = NULL;
511 pci_destroy_slot(slot);
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600512 mutex_unlock(&pci_hp_mutex);
Alex Chiangf46753c2008-06-10 15:28:50 -0600513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 0;
515}
516
517/**
518 * pci_hp_change_slot_info - changes the slot's information structure in the core
Jesse Barnes65b943f2008-06-25 15:27:34 -0700519 * @hotplug: pointer to the slot whose info has changed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * @info: pointer to the info copy into the slot's info structure
521 *
522 * @slot must have been registered with the pci
523 * hotplug subsystem previously with a call to pci_hp_register().
524 *
525 * Returns 0 if successful, anything else for an error.
526 */
Alex Chiangf46753c2008-06-10 15:28:50 -0600527int __must_check pci_hp_change_slot_info(struct hotplug_slot *hotplug,
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700528 struct hotplug_slot_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Alex Chiangf46753c2008-06-10 15:28:50 -0600530 struct pci_slot *slot;
531 if (!hotplug || !info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return -ENODEV;
Alex Chiangf46753c2008-06-10 15:28:50 -0600533 slot = hotplug->pci_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Alex Chiangf46753c2008-06-10 15:28:50 -0600535 memcpy(hotplug->info, info, sizeof(struct hotplug_slot_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 return 0;
538}
539
540static int __init pci_hotplug_init (void)
541{
542 int result;
Greg Kroah-Hartman0fed80f2007-11-01 19:41:16 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 result = cpci_hotplug_init(debug);
545 if (result) {
546 err ("cpci_hotplug_init with error %d\n", result);
Alex Chiangf46753c2008-06-10 15:28:50 -0600547 goto err_cpci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
550 info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
Greg Kroah-Hartman81ace5c2007-10-29 23:22:26 -0500551
Alex Chiangf46753c2008-06-10 15:28:50 -0600552err_cpci:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return result;
554}
555
556static void __exit pci_hotplug_exit (void)
557{
558 cpci_hotplug_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561module_init(pci_hotplug_init);
562module_exit(pci_hotplug_exit);
563
564MODULE_AUTHOR(DRIVER_AUTHOR);
565MODULE_DESCRIPTION(DRIVER_DESC);
566MODULE_LICENSE("GPL");
567module_param(debug, bool, 0644);
568MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
569
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900570EXPORT_SYMBOL_GPL(__pci_hp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571EXPORT_SYMBOL_GPL(pci_hp_deregister);
572EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);