0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/kobject.h>
0009 #include <linux/slab.h>
0010 #include <linux/module.h>
0011 #include <linux/pci.h>
0012 #include <linux/err.h>
0013 #include "pci.h"
0014
0015 struct kset *pci_slots_kset;
0016 EXPORT_SYMBOL_GPL(pci_slots_kset);
0017
0018 static ssize_t pci_slot_attr_show(struct kobject *kobj,
0019 struct attribute *attr, char *buf)
0020 {
0021 struct pci_slot *slot = to_pci_slot(kobj);
0022 struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
0023 return attribute->show ? attribute->show(slot, buf) : -EIO;
0024 }
0025
0026 static ssize_t pci_slot_attr_store(struct kobject *kobj,
0027 struct attribute *attr, const char *buf, size_t len)
0028 {
0029 struct pci_slot *slot = to_pci_slot(kobj);
0030 struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
0031 return attribute->store ? attribute->store(slot, buf, len) : -EIO;
0032 }
0033
0034 static const struct sysfs_ops pci_slot_sysfs_ops = {
0035 .show = pci_slot_attr_show,
0036 .store = pci_slot_attr_store,
0037 };
0038
0039 static ssize_t address_read_file(struct pci_slot *slot, char *buf)
0040 {
0041 if (slot->number == 0xff)
0042 return sysfs_emit(buf, "%04x:%02x\n",
0043 pci_domain_nr(slot->bus),
0044 slot->bus->number);
0045
0046 return sysfs_emit(buf, "%04x:%02x:%02x\n",
0047 pci_domain_nr(slot->bus),
0048 slot->bus->number,
0049 slot->number);
0050 }
0051
0052 static ssize_t bus_speed_read(enum pci_bus_speed speed, char *buf)
0053 {
0054 return sysfs_emit(buf, "%s\n", pci_speed_string(speed));
0055 }
0056
0057 static ssize_t max_speed_read_file(struct pci_slot *slot, char *buf)
0058 {
0059 return bus_speed_read(slot->bus->max_bus_speed, buf);
0060 }
0061
0062 static ssize_t cur_speed_read_file(struct pci_slot *slot, char *buf)
0063 {
0064 return bus_speed_read(slot->bus->cur_bus_speed, buf);
0065 }
0066
0067 static void pci_slot_release(struct kobject *kobj)
0068 {
0069 struct pci_dev *dev;
0070 struct pci_slot *slot = to_pci_slot(kobj);
0071
0072 dev_dbg(&slot->bus->dev, "dev %02x, released physical slot %s\n",
0073 slot->number, pci_slot_name(slot));
0074
0075 down_read(&pci_bus_sem);
0076 list_for_each_entry(dev, &slot->bus->devices, bus_list)
0077 if (PCI_SLOT(dev->devfn) == slot->number)
0078 dev->slot = NULL;
0079 up_read(&pci_bus_sem);
0080
0081 list_del(&slot->list);
0082
0083 kfree(slot);
0084 }
0085
0086 static struct pci_slot_attribute pci_slot_attr_address =
0087 __ATTR(address, S_IRUGO, address_read_file, NULL);
0088 static struct pci_slot_attribute pci_slot_attr_max_speed =
0089 __ATTR(max_bus_speed, S_IRUGO, max_speed_read_file, NULL);
0090 static struct pci_slot_attribute pci_slot_attr_cur_speed =
0091 __ATTR(cur_bus_speed, S_IRUGO, cur_speed_read_file, NULL);
0092
0093 static struct attribute *pci_slot_default_attrs[] = {
0094 &pci_slot_attr_address.attr,
0095 &pci_slot_attr_max_speed.attr,
0096 &pci_slot_attr_cur_speed.attr,
0097 NULL,
0098 };
0099 ATTRIBUTE_GROUPS(pci_slot_default);
0100
0101 static struct kobj_type pci_slot_ktype = {
0102 .sysfs_ops = &pci_slot_sysfs_ops,
0103 .release = &pci_slot_release,
0104 .default_groups = pci_slot_default_groups,
0105 };
0106
0107 static char *make_slot_name(const char *name)
0108 {
0109 char *new_name;
0110 int len, max, dup;
0111
0112 new_name = kstrdup(name, GFP_KERNEL);
0113 if (!new_name)
0114 return NULL;
0115
0116
0117
0118
0119
0120
0121 len = strlen(name) + 2;
0122 max = 1;
0123 dup = 1;
0124
0125 for (;;) {
0126 struct kobject *dup_slot;
0127 dup_slot = kset_find_obj(pci_slots_kset, new_name);
0128 if (!dup_slot)
0129 break;
0130 kobject_put(dup_slot);
0131 if (dup == max) {
0132 len++;
0133 max *= 10;
0134 kfree(new_name);
0135 new_name = kmalloc(len, GFP_KERNEL);
0136 if (!new_name)
0137 break;
0138 }
0139 sprintf(new_name, "%s-%d", name, dup++);
0140 }
0141
0142 return new_name;
0143 }
0144
0145 static int rename_slot(struct pci_slot *slot, const char *name)
0146 {
0147 int result = 0;
0148 char *slot_name;
0149
0150 if (strcmp(pci_slot_name(slot), name) == 0)
0151 return result;
0152
0153 slot_name = make_slot_name(name);
0154 if (!slot_name)
0155 return -ENOMEM;
0156
0157 result = kobject_rename(&slot->kobj, slot_name);
0158 kfree(slot_name);
0159
0160 return result;
0161 }
0162
0163 void pci_dev_assign_slot(struct pci_dev *dev)
0164 {
0165 struct pci_slot *slot;
0166
0167 mutex_lock(&pci_slot_mutex);
0168 list_for_each_entry(slot, &dev->bus->slots, list)
0169 if (PCI_SLOT(dev->devfn) == slot->number)
0170 dev->slot = slot;
0171 mutex_unlock(&pci_slot_mutex);
0172 }
0173
0174 static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
0175 {
0176 struct pci_slot *slot;
0177
0178
0179 list_for_each_entry(slot, &parent->slots, list)
0180 if (slot->number == slot_nr) {
0181 kobject_get(&slot->kobj);
0182 return slot;
0183 }
0184
0185 return NULL;
0186 }
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226 struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
0227 const char *name,
0228 struct hotplug_slot *hotplug)
0229 {
0230 struct pci_dev *dev;
0231 struct pci_slot *slot;
0232 int err = 0;
0233 char *slot_name = NULL;
0234
0235 mutex_lock(&pci_slot_mutex);
0236
0237 if (slot_nr == -1)
0238 goto placeholder;
0239
0240
0241
0242
0243
0244 slot = get_slot(parent, slot_nr);
0245 if (slot) {
0246 if (hotplug) {
0247 if ((err = slot->hotplug ? -EBUSY : 0)
0248 || (err = rename_slot(slot, name))) {
0249 kobject_put(&slot->kobj);
0250 slot = NULL;
0251 goto err;
0252 }
0253 }
0254 goto out;
0255 }
0256
0257 placeholder:
0258 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
0259 if (!slot) {
0260 err = -ENOMEM;
0261 goto err;
0262 }
0263
0264 slot->bus = parent;
0265 slot->number = slot_nr;
0266
0267 slot->kobj.kset = pci_slots_kset;
0268
0269 slot_name = make_slot_name(name);
0270 if (!slot_name) {
0271 err = -ENOMEM;
0272 kfree(slot);
0273 goto err;
0274 }
0275
0276 INIT_LIST_HEAD(&slot->list);
0277 list_add(&slot->list, &parent->slots);
0278
0279 err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
0280 "%s", slot_name);
0281 if (err) {
0282 kobject_put(&slot->kobj);
0283 goto err;
0284 }
0285
0286 down_read(&pci_bus_sem);
0287 list_for_each_entry(dev, &parent->devices, bus_list)
0288 if (PCI_SLOT(dev->devfn) == slot_nr)
0289 dev->slot = slot;
0290 up_read(&pci_bus_sem);
0291
0292 dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
0293 slot_nr, pci_slot_name(slot));
0294
0295 out:
0296 kfree(slot_name);
0297 mutex_unlock(&pci_slot_mutex);
0298 return slot;
0299 err:
0300 slot = ERR_PTR(err);
0301 goto out;
0302 }
0303 EXPORT_SYMBOL_GPL(pci_create_slot);
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313 void pci_destroy_slot(struct pci_slot *slot)
0314 {
0315 dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
0316 slot->number, kref_read(&slot->kobj.kref) - 1);
0317
0318 mutex_lock(&pci_slot_mutex);
0319 kobject_put(&slot->kobj);
0320 mutex_unlock(&pci_slot_mutex);
0321 }
0322 EXPORT_SYMBOL_GPL(pci_destroy_slot);
0323
0324 #if defined(CONFIG_HOTPLUG_PCI) || defined(CONFIG_HOTPLUG_PCI_MODULE)
0325 #include <linux/pci_hotplug.h>
0326
0327
0328
0329
0330
0331
0332
0333 void pci_hp_create_module_link(struct pci_slot *pci_slot)
0334 {
0335 struct hotplug_slot *slot = pci_slot->hotplug;
0336 struct kobject *kobj = NULL;
0337 int ret;
0338
0339 if (!slot || !slot->ops)
0340 return;
0341 kobj = kset_find_obj(module_kset, slot->mod_name);
0342 if (!kobj)
0343 return;
0344 ret = sysfs_create_link(&pci_slot->kobj, kobj, "module");
0345 if (ret)
0346 dev_err(&pci_slot->bus->dev, "Error creating sysfs link (%d)\n",
0347 ret);
0348 kobject_put(kobj);
0349 }
0350 EXPORT_SYMBOL_GPL(pci_hp_create_module_link);
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360 void pci_hp_remove_module_link(struct pci_slot *pci_slot)
0361 {
0362 sysfs_remove_link(&pci_slot->kobj, "module");
0363 }
0364 EXPORT_SYMBOL_GPL(pci_hp_remove_module_link);
0365 #endif
0366
0367 static int pci_slot_init(void)
0368 {
0369 struct kset *pci_bus_kset;
0370
0371 pci_bus_kset = bus_get_kset(&pci_bus_type);
0372 pci_slots_kset = kset_create_and_add("slots", NULL,
0373 &pci_bus_kset->kobj);
0374 if (!pci_slots_kset) {
0375 pr_err("PCI: Slot initialization failure\n");
0376 return -ENOMEM;
0377 }
0378 return 0;
0379 }
0380
0381 subsys_initcall(pci_slot_init);