blob: 5c5043f239cf560fb33fad276f47972f7ffd65f4 [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>
36#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
38#include <linux/mount.h>
39#include <linux/namei.h>
Kenji Kaneshige95cb9092008-10-20 17:40:57 -060040#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070042#include <linux/pci_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/uaccess.h>
Alex Chiangf46753c2008-06-10 15:28:50 -060044#include "../pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define MY_NAME "pci_hotplug"
47
Harvey Harrison66bef8c2008-03-03 19:09:46 -080048#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 -070049#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)
52
53
54/* local variables */
55static int debug;
56
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
62//////////////////////////////////////////////////////////////////
63
64static LIST_HEAD(pci_hotplug_slot_list);
Kenji Kaneshige95cb9092008-10-20 17:40:57 -060065static DEFINE_MUTEX(pci_hp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* these strings match up with the values in pci_bus_speed */
68static char *pci_bus_speed_strings[] = {
69 "33 MHz PCI", /* 0x00 */
70 "66 MHz PCI", /* 0x01 */
71 "66 MHz PCIX", /* 0x02 */
72 "100 MHz PCIX", /* 0x03 */
73 "133 MHz PCIX", /* 0x04 */
74 NULL, /* 0x05 */
75 NULL, /* 0x06 */
76 NULL, /* 0x07 */
77 NULL, /* 0x08 */
78 "66 MHz PCIX 266", /* 0x09 */
79 "100 MHz PCIX 266", /* 0x0a */
80 "133 MHz PCIX 266", /* 0x0b */
81 NULL, /* 0x0c */
82 NULL, /* 0x0d */
83 NULL, /* 0x0e */
84 NULL, /* 0x0f */
85 NULL, /* 0x10 */
86 "66 MHz PCIX 533", /* 0x11 */
87 "100 MHz PCIX 533", /* 0x12 */
88 "133 MHz PCIX 533", /* 0x13 */
89 "25 GBps PCI-E", /* 0x14 */
90};
91
92#ifdef CONFIG_HOTPLUG_PCI_CPCI
93extern int cpci_hotplug_init(int debug);
94extern void cpci_hotplug_exit(void);
95#else
96static inline int cpci_hotplug_init(int debug) { return 0; }
97static inline void cpci_hotplug_exit(void) { }
98#endif
99
100/* Weee, fun with macros... */
101#define GET_STATUS(name,type) \
102static int get_##name (struct hotplug_slot *slot, type *value) \
103{ \
104 struct hotplug_slot_ops *ops = slot->ops; \
105 int retval = 0; \
Kenji Kaneshigebd1d9852008-09-29 17:37:05 +0900106 if (!try_module_get(ops->owner)) \
Zhao, Yuc8761fe2008-09-22 14:26:05 +0800107 return -ENODEV; \
108 if (ops->get_##name) \
109 retval = ops->get_##name(slot, value); \
110 else \
111 *value = slot->info->name; \
112 module_put(ops->owner); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return retval; \
114}
115
116GET_STATUS(power_status, u8)
117GET_STATUS(attention_status, u8)
118GET_STATUS(latch_status, u8)
119GET_STATUS(adapter_status, u8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120GET_STATUS(max_bus_speed, enum pci_bus_speed)
121GET_STATUS(cur_bus_speed, enum pci_bus_speed)
122
Alex Chiangf46753c2008-06-10 15:28:50 -0600123static ssize_t power_read_file(struct pci_slot *slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 int retval;
126 u8 value;
127
Alex Chiangf46753c2008-06-10 15:28:50 -0600128 retval = get_power_status(slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (retval)
130 goto exit;
131 retval = sprintf (buf, "%d\n", value);
132exit:
133 return retval;
134}
135
Alex Chiangf46753c2008-06-10 15:28:50 -0600136static ssize_t power_write_file(struct pci_slot *pci_slot, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 size_t count)
138{
Alex Chiangf46753c2008-06-10 15:28:50 -0600139 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 unsigned long lpower;
141 u8 power;
142 int retval = 0;
143
144 lpower = simple_strtoul (buf, NULL, 10);
145 power = (u8)(lpower & 0xff);
146 dbg ("power = %d\n", power);
147
148 if (!try_module_get(slot->ops->owner)) {
149 retval = -ENODEV;
150 goto exit;
151 }
152 switch (power) {
153 case 0:
154 if (slot->ops->disable_slot)
155 retval = slot->ops->disable_slot(slot);
156 break;
157
158 case 1:
159 if (slot->ops->enable_slot)
160 retval = slot->ops->enable_slot(slot);
161 break;
162
163 default:
164 err ("Illegal value specified for power\n");
165 retval = -EINVAL;
166 }
167 module_put(slot->ops->owner);
168
169exit:
170 if (retval)
171 return retval;
172 return count;
173}
174
Alex Chiangf46753c2008-06-10 15:28:50 -0600175static struct pci_slot_attribute hotplug_slot_attr_power = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
177 .show = power_read_file,
178 .store = power_write_file
179};
180
Alex Chiangf46753c2008-06-10 15:28:50 -0600181static ssize_t attention_read_file(struct pci_slot *slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 int retval;
184 u8 value;
185
Alex Chiangf46753c2008-06-10 15:28:50 -0600186 retval = get_attention_status(slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (retval)
188 goto exit;
Alex Chiangf46753c2008-06-10 15:28:50 -0600189 retval = sprintf(buf, "%d\n", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191exit:
192 return retval;
193}
194
Alex Chiangf46753c2008-06-10 15:28:50 -0600195static ssize_t attention_write_file(struct pci_slot *slot, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 size_t count)
197{
Alex Chiangf46753c2008-06-10 15:28:50 -0600198 struct hotplug_slot_ops *ops = slot->hotplug->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 unsigned long lattention;
200 u8 attention;
201 int retval = 0;
202
203 lattention = simple_strtoul (buf, NULL, 10);
204 attention = (u8)(lattention & 0xff);
205 dbg (" - attention = %d\n", attention);
206
Alex Chiangf46753c2008-06-10 15:28:50 -0600207 if (!try_module_get(ops->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 retval = -ENODEV;
209 goto exit;
210 }
Alex Chiangf46753c2008-06-10 15:28:50 -0600211 if (ops->set_attention_status)
212 retval = ops->set_attention_status(slot->hotplug, attention);
213 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215exit:
216 if (retval)
217 return retval;
218 return count;
219}
220
Alex Chiangf46753c2008-06-10 15:28:50 -0600221static struct pci_slot_attribute hotplug_slot_attr_attention = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
223 .show = attention_read_file,
224 .store = attention_write_file
225};
226
Alex Chiangf46753c2008-06-10 15:28:50 -0600227static ssize_t latch_read_file(struct pci_slot *slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 int retval;
230 u8 value;
231
Alex Chiangf46753c2008-06-10 15:28:50 -0600232 retval = get_latch_status(slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (retval)
234 goto exit;
235 retval = sprintf (buf, "%d\n", value);
236
237exit:
238 return retval;
239}
240
Alex Chiangf46753c2008-06-10 15:28:50 -0600241static struct pci_slot_attribute hotplug_slot_attr_latch = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
243 .show = latch_read_file,
244};
245
Alex Chiangf46753c2008-06-10 15:28:50 -0600246static ssize_t presence_read_file(struct pci_slot *slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 int retval;
249 u8 value;
250
Alex Chiangf46753c2008-06-10 15:28:50 -0600251 retval = get_adapter_status(slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (retval)
253 goto exit;
254 retval = sprintf (buf, "%d\n", value);
255
256exit:
257 return retval;
258}
259
Alex Chiangf46753c2008-06-10 15:28:50 -0600260static struct pci_slot_attribute hotplug_slot_attr_presence = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
262 .show = presence_read_file,
263};
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static char *unknown_speed = "Unknown bus speed";
266
Alex Chiangf46753c2008-06-10 15:28:50 -0600267static ssize_t max_bus_speed_read_file(struct pci_slot *slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 char *speed_string;
270 int retval;
271 enum pci_bus_speed value;
272
Alex Chiangf46753c2008-06-10 15:28:50 -0600273 retval = get_max_bus_speed(slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (retval)
275 goto exit;
276
277 if (value == PCI_SPEED_UNKNOWN)
278 speed_string = unknown_speed;
279 else
280 speed_string = pci_bus_speed_strings[value];
281
282 retval = sprintf (buf, "%s\n", speed_string);
283
284exit:
285 return retval;
286}
287
Alex Chiangf46753c2008-06-10 15:28:50 -0600288static struct pci_slot_attribute hotplug_slot_attr_max_bus_speed = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 .attr = {.name = "max_bus_speed", .mode = S_IFREG | S_IRUGO},
290 .show = max_bus_speed_read_file,
291};
292
Alex Chiangf46753c2008-06-10 15:28:50 -0600293static ssize_t cur_bus_speed_read_file(struct pci_slot *slot, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 char *speed_string;
296 int retval;
297 enum pci_bus_speed value;
298
Alex Chiangf46753c2008-06-10 15:28:50 -0600299 retval = get_cur_bus_speed(slot->hotplug, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (retval)
301 goto exit;
302
303 if (value == PCI_SPEED_UNKNOWN)
304 speed_string = unknown_speed;
305 else
306 speed_string = pci_bus_speed_strings[value];
307
308 retval = sprintf (buf, "%s\n", speed_string);
309
310exit:
311 return retval;
312}
313
Alex Chiangf46753c2008-06-10 15:28:50 -0600314static struct pci_slot_attribute hotplug_slot_attr_cur_bus_speed = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 .attr = {.name = "cur_bus_speed", .mode = S_IFREG | S_IRUGO},
316 .show = cur_bus_speed_read_file,
317};
318
Alex Chiangf46753c2008-06-10 15:28:50 -0600319static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 size_t count)
321{
Alex Chiangf46753c2008-06-10 15:28:50 -0600322 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unsigned long ltest;
324 u32 test;
325 int retval = 0;
326
327 ltest = simple_strtoul (buf, NULL, 10);
328 test = (u32)(ltest & 0xffffffff);
329 dbg ("test = %d\n", test);
330
331 if (!try_module_get(slot->ops->owner)) {
332 retval = -ENODEV;
333 goto exit;
334 }
335 if (slot->ops->hardware_test)
336 retval = slot->ops->hardware_test(slot, test);
337 module_put(slot->ops->owner);
338
339exit:
340 if (retval)
341 return retval;
342 return count;
343}
344
Alex Chiangf46753c2008-06-10 15:28:50 -0600345static struct pci_slot_attribute hotplug_slot_attr_test = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
347 .store = test_write_file
348};
349
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900350static bool has_power_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Alex Chiangf46753c2008-06-10 15:28:50 -0600352 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900354 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if ((slot->ops->enable_slot) ||
356 (slot->ops->disable_slot) ||
357 (slot->ops->get_power_status))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900358 return true;
359 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900362static bool has_attention_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Alex Chiangf46753c2008-06-10 15:28:50 -0600364 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900366 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if ((slot->ops->set_attention_status) ||
368 (slot->ops->get_attention_status))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900369 return true;
370 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900373static bool has_latch_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Alex Chiangf46753c2008-06-10 15:28:50 -0600375 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900377 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (slot->ops->get_latch_status)
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900379 return true;
380 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900383static bool has_adapter_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Alex Chiangf46753c2008-06-10 15:28:50 -0600385 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900387 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (slot->ops->get_adapter_status)
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900389 return true;
390 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900393static bool has_max_bus_speed_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Alex Chiangf46753c2008-06-10 15:28:50 -0600395 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900397 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (slot->ops->get_max_bus_speed)
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900399 return true;
400 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900403static bool has_cur_bus_speed_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Alex Chiangf46753c2008-06-10 15:28:50 -0600405 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900407 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (slot->ops->get_cur_bus_speed)
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900409 return true;
410 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900413static bool has_test_file(struct pci_slot *pci_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Alex Chiangf46753c2008-06-10 15:28:50 -0600415 struct hotplug_slot *slot = pci_slot->hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if ((!slot) || (!slot->ops))
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900417 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (slot->ops->hardware_test)
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900419 return true;
420 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Alex Chiangf46753c2008-06-10 15:28:50 -0600423static int fs_add_slot(struct pci_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700425 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900427 /* Create symbolic link to the hotplug driver module */
428 pci_hp_create_module_link(slot);
429
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900430 if (has_power_file(slot)) {
431 retval = sysfs_create_file(&slot->kobj,
432 &hotplug_slot_attr_power.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700433 if (retval)
434 goto exit_power;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900437 if (has_attention_file(slot)) {
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700438 retval = sysfs_create_file(&slot->kobj,
439 &hotplug_slot_attr_attention.attr);
440 if (retval)
441 goto exit_attention;
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900444 if (has_latch_file(slot)) {
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700445 retval = sysfs_create_file(&slot->kobj,
446 &hotplug_slot_attr_latch.attr);
447 if (retval)
448 goto exit_latch;
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900451 if (has_adapter_file(slot)) {
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700452 retval = sysfs_create_file(&slot->kobj,
453 &hotplug_slot_attr_presence.attr);
454 if (retval)
455 goto exit_adapter;
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900458 if (has_max_bus_speed_file(slot)) {
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700459 retval = sysfs_create_file(&slot->kobj,
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900460 &hotplug_slot_attr_max_bus_speed.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700461 if (retval)
462 goto exit_max_speed;
463 }
464
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900465 if (has_cur_bus_speed_file(slot)) {
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700466 retval = sysfs_create_file(&slot->kobj,
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900467 &hotplug_slot_attr_cur_bus_speed.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700468 if (retval)
469 goto exit_cur_speed;
470 }
471
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900472 if (has_test_file(slot)) {
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700473 retval = sysfs_create_file(&slot->kobj,
474 &hotplug_slot_attr_test.attr);
475 if (retval)
476 goto exit_test;
477 }
478
479 goto exit;
480
481exit_test:
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900482 if (has_cur_bus_speed_file(slot))
483 sysfs_remove_file(&slot->kobj,
484 &hotplug_slot_attr_cur_bus_speed.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700485exit_cur_speed:
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900486 if (has_max_bus_speed_file(slot))
487 sysfs_remove_file(&slot->kobj,
488 &hotplug_slot_attr_max_bus_speed.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700489exit_max_speed:
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900490 if (has_adapter_file(slot))
491 sysfs_remove_file(&slot->kobj,
492 &hotplug_slot_attr_presence.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700493exit_adapter:
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900494 if (has_latch_file(slot))
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700495 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700496exit_latch:
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900497 if (has_attention_file(slot))
498 sysfs_remove_file(&slot->kobj,
499 &hotplug_slot_attr_attention.attr);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700500exit_attention:
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900501 if (has_power_file(slot))
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700502 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
503exit_power:
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900504 pci_hp_remove_module_link(slot);
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700505exit:
506 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Alex Chiangf46753c2008-06-10 15:28:50 -0600509static void fs_remove_slot(struct pci_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900511 if (has_power_file(slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
513
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900514 if (has_attention_file(slot))
515 sysfs_remove_file(&slot->kobj,
516 &hotplug_slot_attr_attention.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900518 if (has_latch_file(slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
520
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900521 if (has_adapter_file(slot))
522 sysfs_remove_file(&slot->kobj,
523 &hotplug_slot_attr_presence.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900525 if (has_max_bus_speed_file(slot))
526 sysfs_remove_file(&slot->kobj,
527 &hotplug_slot_attr_max_bus_speed.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900529 if (has_cur_bus_speed_file(slot))
530 sysfs_remove_file(&slot->kobj,
531 &hotplug_slot_attr_cur_bus_speed.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Kenji Kaneshige498a8fa2009-06-16 11:00:47 +0900533 if (has_test_file(slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_test.attr);
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900535
536 pci_hp_remove_module_link(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539static struct hotplug_slot *get_slot_from_name (const char *name)
540{
541 struct hotplug_slot *slot;
542 struct list_head *tmp;
543
544 list_for_each (tmp, &pci_hotplug_slot_list) {
545 slot = list_entry (tmp, struct hotplug_slot, slot_list);
Alex Chiang58319b82008-10-20 17:41:58 -0600546 if (strcmp(hotplug_slot_name(slot), name) == 0)
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600547 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600549 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552/**
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900553 * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
Jesse Barnes65b943f2008-06-25 15:27:34 -0700554 * @bus: bus this slot is on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * @slot: pointer to the &struct hotplug_slot to register
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900556 * @devnr: device number
Alex Chiang1359f272008-10-20 17:40:42 -0600557 * @name: name registered with kobject core
Randy Dunlap503998c2009-06-24 09:18:14 -0700558 * @owner: caller module owner
559 * @mod_name: caller module name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 *
561 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
562 * userspace interaction to the slot.
563 *
564 * Returns 0 if successful, anything else for an error.
565 */
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900566int __pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus,
567 int devnr, const char *name,
568 struct module *owner, const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 int result;
Alex Chiangf46753c2008-06-10 15:28:50 -0600571 struct pci_slot *pci_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (slot == NULL)
574 return -ENODEV;
575 if ((slot->info == NULL) || (slot->ops == NULL))
576 return -EINVAL;
577 if (slot->release == NULL) {
Joe Perchesa6f29a92007-11-19 17:48:29 -0800578 dbg("Why are you trying to register a hotplug slot "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 "without a proper release function?\n");
580 return -EINVAL;
581 }
582
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900583 slot->ops->owner = owner;
584 slot->ops->mod_name = mod_name;
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600585
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900586 mutex_lock(&pci_hp_mutex);
Alex Chiang8344b562008-06-10 15:30:42 -0600587 /*
588 * No problems if we call this interface from both ACPI_PCI_SLOT
589 * driver and call it here again. If we've already created the
590 * pci_slot, the interface will simply bump the refcount.
591 */
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900592 pci_slot = pci_create_slot(bus, devnr, name, slot);
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600593 if (IS_ERR(pci_slot)) {
594 result = PTR_ERR(pci_slot);
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600595 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Greg Kroah-Hartman64dbcac2007-12-17 15:54:39 -0400597
Alex Chiangf46753c2008-06-10 15:28:50 -0600598 slot->pci_slot = pci_slot;
599 pci_slot->hotplug = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Alex Chiangf46753c2008-06-10 15:28:50 -0600601 list_add(&slot->slot_list, &pci_hotplug_slot_list);
Alex Chiangf46753c2008-06-10 15:28:50 -0600602
603 result = fs_add_slot(pci_slot);
604 kobject_uevent(&pci_slot->kobj, KOBJ_ADD);
Alex Chiang1359f272008-10-20 17:40:42 -0600605 dbg("Added slot %s to the list\n", name);
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600606out:
607 mutex_unlock(&pci_hp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return result;
609}
610
611/**
612 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
Jesse Barnes65b943f2008-06-25 15:27:34 -0700613 * @hotplug: pointer to the &struct hotplug_slot to deregister
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 *
615 * The @slot must have been registered with the pci hotplug subsystem
616 * previously with a call to pci_hp_register().
617 *
618 * Returns 0 if successful, anything else for an error.
619 */
Alex Chiangf46753c2008-06-10 15:28:50 -0600620int pci_hp_deregister(struct hotplug_slot *hotplug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 struct hotplug_slot *temp;
Alex Chiangf46753c2008-06-10 15:28:50 -0600623 struct pci_slot *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Alex Chiangf46753c2008-06-10 15:28:50 -0600625 if (!hotplug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return -ENODEV;
627
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600628 mutex_lock(&pci_hp_mutex);
Alex Chiang58319b82008-10-20 17:41:58 -0600629 temp = get_slot_from_name(hotplug_slot_name(hotplug));
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600630 if (temp != hotplug) {
631 mutex_unlock(&pci_hp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -ENODEV;
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Alex Chiangf46753c2008-06-10 15:28:50 -0600635 list_del(&hotplug->slot_list);
Alex Chiangf46753c2008-06-10 15:28:50 -0600636
637 slot = hotplug->pci_slot;
638 fs_remove_slot(slot);
Alex Chiang58319b82008-10-20 17:41:58 -0600639 dbg("Removed slot %s from the list\n", hotplug_slot_name(hotplug));
Alex Chiangf46753c2008-06-10 15:28:50 -0600640
641 hotplug->release(hotplug);
642 slot->hotplug = NULL;
643 pci_destroy_slot(slot);
Kenji Kaneshige95cb9092008-10-20 17:40:57 -0600644 mutex_unlock(&pci_hp_mutex);
Alex Chiangf46753c2008-06-10 15:28:50 -0600645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return 0;
647}
648
649/**
650 * pci_hp_change_slot_info - changes the slot's information structure in the core
Jesse Barnes65b943f2008-06-25 15:27:34 -0700651 * @hotplug: pointer to the slot whose info has changed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 * @info: pointer to the info copy into the slot's info structure
653 *
654 * @slot must have been registered with the pci
655 * hotplug subsystem previously with a call to pci_hp_register().
656 *
657 * Returns 0 if successful, anything else for an error.
658 */
Alex Chiangf46753c2008-06-10 15:28:50 -0600659int __must_check pci_hp_change_slot_info(struct hotplug_slot *hotplug,
Greg Kroah-Hartman660a0e82006-08-28 11:43:25 -0700660 struct hotplug_slot_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Alex Chiangf46753c2008-06-10 15:28:50 -0600662 struct pci_slot *slot;
663 if (!hotplug || !info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return -ENODEV;
Alex Chiangf46753c2008-06-10 15:28:50 -0600665 slot = hotplug->pci_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Alex Chiangf46753c2008-06-10 15:28:50 -0600667 memcpy(hotplug->info, info, sizeof(struct hotplug_slot_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 return 0;
670}
671
672static int __init pci_hotplug_init (void)
673{
674 int result;
Greg Kroah-Hartman0fed80f2007-11-01 19:41:16 -0700675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 result = cpci_hotplug_init(debug);
677 if (result) {
678 err ("cpci_hotplug_init with error %d\n", result);
Alex Chiangf46753c2008-06-10 15:28:50 -0600679 goto err_cpci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
682 info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
Greg Kroah-Hartman81ace5c2007-10-29 23:22:26 -0500683
Alex Chiangf46753c2008-06-10 15:28:50 -0600684err_cpci:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return result;
686}
687
688static void __exit pci_hotplug_exit (void)
689{
690 cpci_hotplug_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693module_init(pci_hotplug_init);
694module_exit(pci_hotplug_exit);
695
696MODULE_AUTHOR(DRIVER_AUTHOR);
697MODULE_DESCRIPTION(DRIVER_DESC);
698MODULE_LICENSE("GPL");
699module_param(debug, bool, 0644);
700MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
701
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900702EXPORT_SYMBOL_GPL(__pci_hp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703EXPORT_SYMBOL_GPL(pci_hp_deregister);
704EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);