Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2006 Matthew Wilcox <matthew@wil.cx>
0004  * Copyright (C) 2006-2009 Hewlett-Packard Development Company, L.P.
0005  *  Alex Chiang <achiang@hp.com>
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      * Make sure we hit the realloc case the first time through the
0118      * loop.  'len' will be strlen(name) + 3 at that point which is
0119      * enough space for "name-X" and the trailing NUL.
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     /* We already hold pci_slot_mutex */
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  * pci_create_slot - create or increment refcount for physical PCI slot
0190  * @parent: struct pci_bus of parent bridge
0191  * @slot_nr: PCI_SLOT(pci_dev->devfn) or -1 for placeholder
0192  * @name: user visible string presented in /sys/bus/pci/slots/<name>
0193  * @hotplug: set if caller is hotplug driver, NULL otherwise
0194  *
0195  * PCI slots have first class attributes such as address, speed, width,
0196  * and a &struct pci_slot is used to manage them. This interface will
0197  * either return a new &struct pci_slot to the caller, or if the pci_slot
0198  * already exists, its refcount will be incremented.
0199  *
0200  * Slots are uniquely identified by a @pci_bus, @slot_nr tuple.
0201  *
0202  * There are known platforms with broken firmware that assign the same
0203  * name to multiple slots. Workaround these broken platforms by renaming
0204  * the slots on behalf of the caller. If firmware assigns name N to
0205  * multiple slots:
0206  *
0207  * The first slot is assigned N
0208  * The second slot is assigned N-1
0209  * The third slot is assigned N-2
0210  * etc.
0211  *
0212  * Placeholder slots:
0213  * In most cases, @pci_bus, @slot_nr will be sufficient to uniquely identify
0214  * a slot. There is one notable exception - pSeries (rpaphp), where the
0215  * @slot_nr cannot be determined until a device is actually inserted into
0216  * the slot. In this scenario, the caller may pass -1 for @slot_nr.
0217  *
0218  * The following semantics are imposed when the caller passes @slot_nr ==
0219  * -1. First, we no longer check for an existing %struct pci_slot, as there
0220  * may be many slots with @slot_nr of -1.  The other change in semantics is
0221  * user-visible, which is the 'address' parameter presented in sysfs will
0222  * consist solely of a dddd:bb tuple, where dddd is the PCI domain of the
0223  * %struct pci_bus and bb is the bus number. In other words, the devfn of
0224  * the 'placeholder' slot will not be displayed.
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      * Hotplug drivers are allowed to rename an existing slot,
0242      * but only if not already claimed.
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  * pci_destroy_slot - decrement refcount for physical PCI slot
0307  * @slot: struct pci_slot to decrement
0308  *
0309  * %struct pci_slot is refcounted, so destroying them is really easy; we
0310  * just call kobject_put on its kobj and let our release methods do the
0311  * rest.
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  * pci_hp_create_module_link - create symbolic link to hotplug driver module
0328  * @pci_slot: struct pci_slot
0329  *
0330  * Helper function for pci_hotplug_core.c to create symbolic link to
0331  * the hotplug driver module.
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  * pci_hp_remove_module_link - remove symbolic link to the hotplug driver
0354  *  module.
0355  * @pci_slot: struct pci_slot
0356  *
0357  * Helper function for pci_hotplug_core.c to remove symbolic link to
0358  * the hotplug driver module.
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);