]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/pci/hotplug/pci_hotplug_core.c
Merge branch 'fixes' of git://git.armlinux.org.uk/~rmk/linux-arm
[mirror_ubuntu-bionic-kernel.git] / drivers / pci / hotplug / pci_hotplug_core.c
CommitLineData
1da177e4
LT
1/*
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 *
fb5f4d7a 24 * Send feedback to <kristen.c.accardi@intel.com>
1da177e4 25 *
d9b47d54
BH
26 * Authors:
27 * Greg Kroah-Hartman <greg@kroah.com>
28 * Scott Murray <scottm@somanetworks.com>
1da177e4
LT
29 */
30
57b51b9a 31#include <linux/module.h> /* try_module_get & module_put */
1da177e4
LT
32#include <linux/moduleparam.h>
33#include <linux/kernel.h>
34#include <linux/types.h>
35#include <linux/list.h>
7a54f25c
GKH
36#include <linux/kobject.h>
37#include <linux/sysfs.h>
1da177e4 38#include <linux/pagemap.h>
1da177e4
LT
39#include <linux/init.h>
40#include <linux/mount.h>
41#include <linux/namei.h>
95cb9093 42#include <linux/mutex.h>
1da177e4 43#include <linux/pci.h>
7a54f25c 44#include <linux/pci_hotplug.h>
7c0f6ba6 45#include <linux/uaccess.h>
f46753c5 46#include "../pci.h"
c3139ba2 47#include "cpci_hotplug.h"
1da177e4
LT
48
49#define MY_NAME "pci_hotplug"
50
ff3ce480
BS
51#define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt, MY_NAME, __func__, ## arg); } while (0)
52#define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME, ## arg)
53#define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME, ## arg)
54#define warn(format, arg...) printk(KERN_WARNING "%s: " format, MY_NAME, ## arg)
1da177e4 55
1da177e4 56/* local variables */
90ab5ee9 57static bool debug;
1da177e4 58
1da177e4 59static LIST_HEAD(pci_hotplug_slot_list);
95cb9093 60static DEFINE_MUTEX(pci_hp_mutex);
1da177e4 61
1da177e4 62/* Weee, fun with macros... */
3c78bc61
RD
63#define GET_STATUS(name, type) \
64static int get_##name(struct hotplug_slot *slot, type *value) \
1da177e4
LT
65{ \
66 struct hotplug_slot_ops *ops = slot->ops; \
67 int retval = 0; \
bd1d9855 68 if (!try_module_get(ops->owner)) \
c8761fe8
ZY
69 return -ENODEV; \
70 if (ops->get_##name) \
71 retval = ops->get_##name(slot, value); \
72 else \
73 *value = slot->info->name; \
74 module_put(ops->owner); \
1da177e4
LT
75 return retval; \
76}
77
78GET_STATUS(power_status, u8)
79GET_STATUS(attention_status, u8)
80GET_STATUS(latch_status, u8)
81GET_STATUS(adapter_status, u8)
1da177e4 82
a6ed1f4e 83static ssize_t power_read_file(struct pci_slot *pci_slot, char *buf)
1da177e4
LT
84{
85 int retval;
86 u8 value;
87
a6ed1f4e 88 retval = get_power_status(pci_slot->hotplug, &value);
1da177e4 89 if (retval)
3c78bc61
RD
90 return retval;
91
92 return sprintf(buf, "%d\n", value);
1da177e4
LT
93}
94
f46753c5 95static ssize_t power_write_file(struct pci_slot *pci_slot, const char *buf,
3c78bc61 96 size_t count)
1da177e4 97{
f46753c5 98 struct hotplug_slot *slot = pci_slot->hotplug;
1da177e4
LT
99 unsigned long lpower;
100 u8 power;
101 int retval = 0;
102
3c78bc61 103 lpower = simple_strtoul(buf, NULL, 10);
1da177e4 104 power = (u8)(lpower & 0xff);
3c78bc61 105 dbg("power = %d\n", power);
1da177e4
LT
106
107 if (!try_module_get(slot->ops->owner)) {
108 retval = -ENODEV;
109 goto exit;
110 }
111 switch (power) {
3c78bc61
RD
112 case 0:
113 if (slot->ops->disable_slot)
114 retval = slot->ops->disable_slot(slot);
115 break;
116
117 case 1:
118 if (slot->ops->enable_slot)
119 retval = slot->ops->enable_slot(slot);
120 break;
121
122 default:
123 err("Illegal value specified for power\n");
124 retval = -EINVAL;
1da177e4
LT
125 }
126 module_put(slot->ops->owner);
127
f7625980 128exit:
1da177e4
LT
129 if (retval)
130 return retval;
131 return count;
132}
133
f46753c5 134static struct pci_slot_attribute hotplug_slot_attr_power = {
1da177e4
LT
135 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
136 .show = power_read_file,
137 .store = power_write_file
138};
139
a6ed1f4e 140static ssize_t attention_read_file(struct pci_slot *pci_slot, char *buf)
1da177e4
LT
141{
142 int retval;
143 u8 value;
144
a6ed1f4e 145 retval = get_attention_status(pci_slot->hotplug, &value);
1da177e4 146 if (retval)
3c78bc61 147 return retval;
1da177e4 148
3c78bc61 149 return sprintf(buf, "%d\n", value);
1da177e4
LT
150}
151
a6ed1f4e 152static ssize_t attention_write_file(struct pci_slot *pci_slot, const char *buf,
3c78bc61 153 size_t count)
1da177e4 154{
a6ed1f4e 155 struct hotplug_slot_ops *ops = pci_slot->hotplug->ops;
1da177e4
LT
156 unsigned long lattention;
157 u8 attention;
158 int retval = 0;
159
3c78bc61 160 lattention = simple_strtoul(buf, NULL, 10);
1da177e4 161 attention = (u8)(lattention & 0xff);
3c78bc61 162 dbg(" - attention = %d\n", attention);
1da177e4 163
f46753c5 164 if (!try_module_get(ops->owner)) {
1da177e4
LT
165 retval = -ENODEV;
166 goto exit;
167 }
f46753c5 168 if (ops->set_attention_status)
a6ed1f4e 169 retval = ops->set_attention_status(pci_slot->hotplug, attention);
f46753c5 170 module_put(ops->owner);
1da177e4 171
f7625980 172exit:
1da177e4
LT
173 if (retval)
174 return retval;
175 return count;
176}
177
f46753c5 178static struct pci_slot_attribute hotplug_slot_attr_attention = {
1da177e4
LT
179 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
180 .show = attention_read_file,
181 .store = attention_write_file
182};
183
a6ed1f4e 184static ssize_t latch_read_file(struct pci_slot *pci_slot, char *buf)
1da177e4
LT
185{
186 int retval;
187 u8 value;
188
a6ed1f4e 189 retval = get_latch_status(pci_slot->hotplug, &value);
1da177e4 190 if (retval)
3c78bc61 191 return retval;
1da177e4 192
3c78bc61 193 return sprintf(buf, "%d\n", value);
1da177e4
LT
194}
195
f46753c5 196static struct pci_slot_attribute hotplug_slot_attr_latch = {
1da177e4
LT
197 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
198 .show = latch_read_file,
199};
200
a6ed1f4e 201static ssize_t presence_read_file(struct pci_slot *pci_slot, char *buf)
1da177e4
LT
202{
203 int retval;
204 u8 value;
205
a6ed1f4e 206 retval = get_adapter_status(pci_slot->hotplug, &value);
1da177e4 207 if (retval)
3c78bc61 208 return retval;
1da177e4 209
3c78bc61 210 return sprintf(buf, "%d\n", value);
1da177e4
LT
211}
212
f46753c5 213static struct pci_slot_attribute hotplug_slot_attr_presence = {
1da177e4
LT
214 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
215 .show = presence_read_file,
216};
217
f46753c5 218static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
3c78bc61 219 size_t count)
1da177e4 220{
f46753c5 221 struct hotplug_slot *slot = pci_slot->hotplug;
1da177e4
LT
222 unsigned long ltest;
223 u32 test;
224 int retval = 0;
225
ff3ce480 226 ltest = simple_strtoul(buf, NULL, 10);
1da177e4 227 test = (u32)(ltest & 0xffffffff);
3c78bc61 228 dbg("test = %d\n", test);
1da177e4
LT
229
230 if (!try_module_get(slot->ops->owner)) {
231 retval = -ENODEV;
232 goto exit;
233 }
234 if (slot->ops->hardware_test)
235 retval = slot->ops->hardware_test(slot, test);
236 module_put(slot->ops->owner);
237
f7625980 238exit:
1da177e4
LT
239 if (retval)
240 return retval;
241 return count;
242}
243
f46753c5 244static struct pci_slot_attribute hotplug_slot_attr_test = {
1da177e4
LT
245 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
246 .store = test_write_file
247};
248
498a8faf 249static bool has_power_file(struct pci_slot *pci_slot)
1da177e4 250{
f46753c5 251 struct hotplug_slot *slot = pci_slot->hotplug;
3c78bc61 252
1da177e4 253 if ((!slot) || (!slot->ops))
498a8faf 254 return false;
1da177e4
LT
255 if ((slot->ops->enable_slot) ||
256 (slot->ops->disable_slot) ||
257 (slot->ops->get_power_status))
498a8faf
KK
258 return true;
259 return false;
1da177e4
LT
260}
261
498a8faf 262static bool has_attention_file(struct pci_slot *pci_slot)
1da177e4 263{
f46753c5 264 struct hotplug_slot *slot = pci_slot->hotplug;
3c78bc61 265
1da177e4 266 if ((!slot) || (!slot->ops))
498a8faf 267 return false;
1da177e4
LT
268 if ((slot->ops->set_attention_status) ||
269 (slot->ops->get_attention_status))
498a8faf
KK
270 return true;
271 return false;
1da177e4
LT
272}
273
498a8faf 274static bool has_latch_file(struct pci_slot *pci_slot)
1da177e4 275{
f46753c5 276 struct hotplug_slot *slot = pci_slot->hotplug;
3c78bc61 277
1da177e4 278 if ((!slot) || (!slot->ops))
498a8faf 279 return false;
1da177e4 280 if (slot->ops->get_latch_status)
498a8faf
KK
281 return true;
282 return false;
1da177e4
LT
283}
284
498a8faf 285static bool has_adapter_file(struct pci_slot *pci_slot)
1da177e4 286{
f46753c5 287 struct hotplug_slot *slot = pci_slot->hotplug;
3c78bc61 288
1da177e4 289 if ((!slot) || (!slot->ops))
498a8faf 290 return false;
1da177e4 291 if (slot->ops->get_adapter_status)
498a8faf
KK
292 return true;
293 return false;
1da177e4
LT
294}
295
498a8faf 296static bool has_test_file(struct pci_slot *pci_slot)
1da177e4 297{
f46753c5 298 struct hotplug_slot *slot = pci_slot->hotplug;
3c78bc61 299
1da177e4 300 if ((!slot) || (!slot->ops))
498a8faf 301 return false;
1da177e4 302 if (slot->ops->hardware_test)
498a8faf
KK
303 return true;
304 return false;
1da177e4
LT
305}
306
a6ed1f4e 307static int fs_add_slot(struct pci_slot *pci_slot)
1da177e4 308{
660a0e8f 309 int retval = 0;
1da177e4 310
c825bc94 311 /* Create symbolic link to the hotplug driver module */
a6ed1f4e 312 pci_hp_create_module_link(pci_slot);
c825bc94 313
a6ed1f4e
YW
314 if (has_power_file(pci_slot)) {
315 retval = sysfs_create_file(&pci_slot->kobj,
498a8faf 316 &hotplug_slot_attr_power.attr);
660a0e8f
GKH
317 if (retval)
318 goto exit_power;
319 }
1da177e4 320
a6ed1f4e
YW
321 if (has_attention_file(pci_slot)) {
322 retval = sysfs_create_file(&pci_slot->kobj,
660a0e8f
GKH
323 &hotplug_slot_attr_attention.attr);
324 if (retval)
325 goto exit_attention;
326 }
1da177e4 327
a6ed1f4e
YW
328 if (has_latch_file(pci_slot)) {
329 retval = sysfs_create_file(&pci_slot->kobj,
660a0e8f
GKH
330 &hotplug_slot_attr_latch.attr);
331 if (retval)
332 goto exit_latch;
333 }
1da177e4 334
a6ed1f4e
YW
335 if (has_adapter_file(pci_slot)) {
336 retval = sysfs_create_file(&pci_slot->kobj,
660a0e8f
GKH
337 &hotplug_slot_attr_presence.attr);
338 if (retval)
339 goto exit_adapter;
340 }
1da177e4 341
a6ed1f4e
YW
342 if (has_test_file(pci_slot)) {
343 retval = sysfs_create_file(&pci_slot->kobj,
660a0e8f
GKH
344 &hotplug_slot_attr_test.attr);
345 if (retval)
346 goto exit_test;
347 }
348
349 goto exit;
350
351exit_test:
a6ed1f4e
YW
352 if (has_adapter_file(pci_slot))
353 sysfs_remove_file(&pci_slot->kobj,
498a8faf 354 &hotplug_slot_attr_presence.attr);
660a0e8f 355exit_adapter:
a6ed1f4e
YW
356 if (has_latch_file(pci_slot))
357 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
660a0e8f 358exit_latch:
a6ed1f4e
YW
359 if (has_attention_file(pci_slot))
360 sysfs_remove_file(&pci_slot->kobj,
498a8faf 361 &hotplug_slot_attr_attention.attr);
660a0e8f 362exit_attention:
a6ed1f4e
YW
363 if (has_power_file(pci_slot))
364 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
660a0e8f 365exit_power:
a6ed1f4e 366 pci_hp_remove_module_link(pci_slot);
660a0e8f
GKH
367exit:
368 return retval;
1da177e4
LT
369}
370
a6ed1f4e 371static void fs_remove_slot(struct pci_slot *pci_slot)
1da177e4 372{
a6ed1f4e
YW
373 if (has_power_file(pci_slot))
374 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
1da177e4 375
a6ed1f4e
YW
376 if (has_attention_file(pci_slot))
377 sysfs_remove_file(&pci_slot->kobj,
498a8faf 378 &hotplug_slot_attr_attention.attr);
1da177e4 379
a6ed1f4e
YW
380 if (has_latch_file(pci_slot))
381 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
1da177e4 382
a6ed1f4e
YW
383 if (has_adapter_file(pci_slot))
384 sysfs_remove_file(&pci_slot->kobj,
498a8faf 385 &hotplug_slot_attr_presence.attr);
1da177e4 386
a6ed1f4e
YW
387 if (has_test_file(pci_slot))
388 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_test.attr);
c825bc94 389
a6ed1f4e 390 pci_hp_remove_module_link(pci_slot);
1da177e4
LT
391}
392
3c78bc61 393static struct hotplug_slot *get_slot_from_name(const char *name)
1da177e4
LT
394{
395 struct hotplug_slot *slot;
1da177e4 396
2ac83ccc 397 list_for_each_entry(slot, &pci_hotplug_slot_list, slot_list) {
58319b80 398 if (strcmp(hotplug_slot_name(slot), name) == 0)
95cb9093 399 return slot;
1da177e4 400 }
95cb9093 401 return NULL;
1da177e4
LT
402}
403
404/**
c825bc94 405 * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
65b943f6 406 * @bus: bus this slot is on
1da177e4 407 * @slot: pointer to the &struct hotplug_slot to register
c825bc94 408 * @devnr: device number
1359f270 409 * @name: name registered with kobject core
503998ca
RD
410 * @owner: caller module owner
411 * @mod_name: caller module name
1da177e4
LT
412 *
413 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
414 * userspace interaction to the slot.
415 *
416 * Returns 0 if successful, anything else for an error.
417 */
c825bc94
KK
418int __pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus,
419 int devnr, const char *name,
420 struct module *owner, const char *mod_name)
1da177e4
LT
421{
422 int result;
f46753c5 423 struct pci_slot *pci_slot;
1da177e4
LT
424
425 if (slot == NULL)
426 return -ENODEV;
427 if ((slot->info == NULL) || (slot->ops == NULL))
428 return -EINVAL;
429 if (slot->release == NULL) {
227f0647 430 dbg("Why are you trying to register a hotplug slot without a proper release function?\n");
1da177e4
LT
431 return -EINVAL;
432 }
433
c825bc94
KK
434 slot->ops->owner = owner;
435 slot->ops->mod_name = mod_name;
95cb9093 436
c825bc94 437 mutex_lock(&pci_hp_mutex);
8344b568
AC
438 /*
439 * No problems if we call this interface from both ACPI_PCI_SLOT
440 * driver and call it here again. If we've already created the
441 * pci_slot, the interface will simply bump the refcount.
442 */
c825bc94 443 pci_slot = pci_create_slot(bus, devnr, name, slot);
95cb9093
KK
444 if (IS_ERR(pci_slot)) {
445 result = PTR_ERR(pci_slot);
5fe6cc60 446 goto out;
1da177e4 447 }
64dbcac3 448
f46753c5
AC
449 slot->pci_slot = pci_slot;
450 pci_slot->hotplug = slot;
451
f46753c5 452 list_add(&slot->slot_list, &pci_hotplug_slot_list);
f46753c5
AC
453
454 result = fs_add_slot(pci_slot);
455 kobject_uevent(&pci_slot->kobj, KOBJ_ADD);
1359f270 456 dbg("Added slot %s to the list\n", name);
95cb9093
KK
457out:
458 mutex_unlock(&pci_hp_mutex);
1da177e4
LT
459 return result;
460}
b7fe9434 461EXPORT_SYMBOL_GPL(__pci_hp_register);
1da177e4
LT
462
463/**
464 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
a6ed1f4e 465 * @slot: pointer to the &struct hotplug_slot to deregister
1da177e4
LT
466 *
467 * The @slot must have been registered with the pci hotplug subsystem
468 * previously with a call to pci_hp_register().
469 *
470 * Returns 0 if successful, anything else for an error.
471 */
a6ed1f4e 472int pci_hp_deregister(struct hotplug_slot *slot)
1da177e4
LT
473{
474 struct hotplug_slot *temp;
a6ed1f4e 475 struct pci_slot *pci_slot;
1da177e4 476
a6ed1f4e 477 if (!slot)
1da177e4
LT
478 return -ENODEV;
479
95cb9093 480 mutex_lock(&pci_hp_mutex);
a6ed1f4e
YW
481 temp = get_slot_from_name(hotplug_slot_name(slot));
482 if (temp != slot) {
95cb9093 483 mutex_unlock(&pci_hp_mutex);
1da177e4 484 return -ENODEV;
95cb9093 485 }
1da177e4 486
a6ed1f4e 487 list_del(&slot->slot_list);
f46753c5 488
a6ed1f4e
YW
489 pci_slot = slot->pci_slot;
490 fs_remove_slot(pci_slot);
491 dbg("Removed slot %s from the list\n", hotplug_slot_name(slot));
f46753c5 492
a6ed1f4e
YW
493 slot->release(slot);
494 pci_slot->hotplug = NULL;
495 pci_destroy_slot(pci_slot);
95cb9093 496 mutex_unlock(&pci_hp_mutex);
f46753c5 497
1da177e4
LT
498 return 0;
499}
b7fe9434 500EXPORT_SYMBOL_GPL(pci_hp_deregister);
1da177e4
LT
501
502/**
503 * pci_hp_change_slot_info - changes the slot's information structure in the core
a6ed1f4e 504 * @slot: pointer to the slot whose info has changed
1da177e4
LT
505 * @info: pointer to the info copy into the slot's info structure
506 *
f7625980 507 * @slot must have been registered with the pci
1da177e4
LT
508 * hotplug subsystem previously with a call to pci_hp_register().
509 *
510 * Returns 0 if successful, anything else for an error.
511 */
a6ed1f4e 512int pci_hp_change_slot_info(struct hotplug_slot *slot,
d67aed63 513 struct hotplug_slot_info *info)
1da177e4 514{
a6ed1f4e 515 if (!slot || !info)
1da177e4
LT
516 return -ENODEV;
517
a6ed1f4e 518 memcpy(slot->info, info, sizeof(struct hotplug_slot_info));
1da177e4
LT
519
520 return 0;
521}
b7fe9434 522EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);
1da177e4 523
3c78bc61 524static int __init pci_hotplug_init(void)
1da177e4
LT
525{
526 int result;
0fed80f7 527
1da177e4
LT
528 result = cpci_hotplug_init(debug);
529 if (result) {
3c78bc61
RD
530 err("cpci_hotplug_init with error %d\n", result);
531 return result;
1da177e4
LT
532 }
533
1da177e4
LT
534 return result;
535}
57b51b9a 536device_initcall(pci_hotplug_init);
1da177e4 537
57b51b9a
PG
538/*
539 * not really modular, but the easiest way to keep compat with existing
540 * bootargs behaviour is to continue using module_param here.
541 */
1da177e4
LT
542module_param(debug, bool, 0644);
543MODULE_PARM_DESC(debug, "Debugging mode enabled or not");