blob: 0e009c3ba5fd9b322d490e5836f9d717f9ab9472 [file] [log] [blame]
Alex Chiangf46753c2008-06-10 15:28:50 -06001/*
2 * drivers/pci/slot.c
3 * Copyright (C) 2006 Matthew Wilcox <matthew@wil.cx>
4 * Copyright (C) 2006-2008 Hewlett-Packard Development Company, L.P.
5 * Alex Chiang <achiang@hp.com>
6 */
7
8#include <linux/kobject.h>
9#include <linux/pci.h>
10#include <linux/err.h>
11#include "pci.h"
12
13struct kset *pci_slots_kset;
14EXPORT_SYMBOL_GPL(pci_slots_kset);
15
16static ssize_t pci_slot_attr_show(struct kobject *kobj,
17 struct attribute *attr, char *buf)
18{
19 struct pci_slot *slot = to_pci_slot(kobj);
20 struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
21 return attribute->show ? attribute->show(slot, buf) : -EIO;
22}
23
24static ssize_t pci_slot_attr_store(struct kobject *kobj,
25 struct attribute *attr, const char *buf, size_t len)
26{
27 struct pci_slot *slot = to_pci_slot(kobj);
28 struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
29 return attribute->store ? attribute->store(slot, buf, len) : -EIO;
30}
31
32static struct sysfs_ops pci_slot_sysfs_ops = {
33 .show = pci_slot_attr_show,
34 .store = pci_slot_attr_store,
35};
36
37static ssize_t address_read_file(struct pci_slot *slot, char *buf)
38{
39 if (slot->number == 0xff)
40 return sprintf(buf, "%04x:%02x\n",
41 pci_domain_nr(slot->bus),
42 slot->bus->number);
43 else
44 return sprintf(buf, "%04x:%02x:%02x\n",
45 pci_domain_nr(slot->bus),
46 slot->bus->number,
47 slot->number);
48}
49
50static void pci_slot_release(struct kobject *kobj)
51{
Alex Chiangcef354d2008-09-02 09:40:51 -060052 struct pci_dev *dev;
Alex Chiangf46753c2008-06-10 15:28:50 -060053 struct pci_slot *slot = to_pci_slot(kobj);
54
55 pr_debug("%s: releasing pci_slot on %x:%d\n", __func__,
56 slot->bus->number, slot->number);
57
Alex Chiangcef354d2008-09-02 09:40:51 -060058 list_for_each_entry(dev, &slot->bus->devices, bus_list)
59 if (PCI_SLOT(dev->devfn) == slot->number)
60 dev->slot = NULL;
61
Alex Chiangf46753c2008-06-10 15:28:50 -060062 list_del(&slot->list);
63
64 kfree(slot);
65}
66
67static struct pci_slot_attribute pci_slot_attr_address =
68 __ATTR(address, (S_IFREG | S_IRUGO), address_read_file, NULL);
69
70static struct attribute *pci_slot_default_attrs[] = {
71 &pci_slot_attr_address.attr,
72 NULL,
73};
74
75static struct kobj_type pci_slot_ktype = {
76 .sysfs_ops = &pci_slot_sysfs_ops,
77 .release = &pci_slot_release,
78 .default_attrs = pci_slot_default_attrs,
79};
80
81/**
82 * pci_create_slot - create or increment refcount for physical PCI slot
83 * @parent: struct pci_bus of parent bridge
84 * @slot_nr: PCI_SLOT(pci_dev->devfn) or -1 for placeholder
85 * @name: user visible string presented in /sys/bus/pci/slots/<name>
Alex Chiang828f3762008-10-20 17:40:52 -060086 * @hotplug: set if caller is hotplug driver, NULL otherwise
Alex Chiangf46753c2008-06-10 15:28:50 -060087 *
88 * PCI slots have first class attributes such as address, speed, width,
89 * and a &struct pci_slot is used to manage them. This interface will
90 * either return a new &struct pci_slot to the caller, or if the pci_slot
91 * already exists, its refcount will be incremented.
92 *
93 * Slots are uniquely identified by a @pci_bus, @slot_nr, @name tuple.
94 *
95 * Placeholder slots:
96 * In most cases, @pci_bus, @slot_nr will be sufficient to uniquely identify
97 * a slot. There is one notable exception - pSeries (rpaphp), where the
98 * @slot_nr cannot be determined until a device is actually inserted into
99 * the slot. In this scenario, the caller may pass -1 for @slot_nr.
100 *
101 * The following semantics are imposed when the caller passes @slot_nr ==
102 * -1. First, the check for existing %struct pci_slot is skipped, as the
103 * caller may know about several unpopulated slots on a given %struct
104 * pci_bus, and each slot would have a @slot_nr of -1. Uniqueness for
105 * these slots is then determined by the @name parameter. We expect
106 * kobject_init_and_add() to warn us if the caller attempts to create
107 * multiple slots with the same name. The other change in semantics is
108 * user-visible, which is the 'address' parameter presented in sysfs will
109 * consist solely of a dddd:bb tuple, where dddd is the PCI domain of the
110 * %struct pci_bus and bb is the bus number. In other words, the devfn of
111 * the 'placeholder' slot will not be displayed.
112 */
113
114struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
Alex Chiang828f3762008-10-20 17:40:52 -0600115 const char *name,
116 struct hotplug_slot *hotplug)
Alex Chiangf46753c2008-06-10 15:28:50 -0600117{
Alex Chiangcef354d2008-09-02 09:40:51 -0600118 struct pci_dev *dev;
Alex Chiangf46753c2008-06-10 15:28:50 -0600119 struct pci_slot *slot;
120 int err;
121
122 down_write(&pci_bus_sem);
123
124 if (slot_nr == -1)
125 goto placeholder;
126
127 /* If we've already created this slot, bump refcount and return. */
128 list_for_each_entry(slot, &parent->slots, list) {
129 if (slot->number == slot_nr) {
130 kobject_get(&slot->kobj);
131 pr_debug("%s: inc refcount to %d on %04x:%02x:%02x\n",
132 __func__,
133 atomic_read(&slot->kobj.kref.refcount),
134 pci_domain_nr(parent), parent->number,
135 slot_nr);
136 goto out;
137 }
138 }
139
140placeholder:
141 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
142 if (!slot) {
143 slot = ERR_PTR(-ENOMEM);
144 goto out;
145 }
146
147 slot->bus = parent;
148 slot->number = slot_nr;
149
150 slot->kobj.kset = pci_slots_kset;
151 err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
152 "%s", name);
153 if (err) {
154 printk(KERN_ERR "Unable to register kobject %s\n", name);
155 goto err;
156 }
157
158 INIT_LIST_HEAD(&slot->list);
159 list_add(&slot->list, &parent->slots);
160
Alex Chiangcef354d2008-09-02 09:40:51 -0600161 list_for_each_entry(dev, &parent->devices, bus_list)
162 if (PCI_SLOT(dev->devfn) == slot_nr)
163 dev->slot = slot;
164
Alex Chiangf46753c2008-06-10 15:28:50 -0600165 /* Don't care if debug printk has a -1 for slot_nr */
166 pr_debug("%s: created pci_slot on %04x:%02x:%02x\n",
167 __func__, pci_domain_nr(parent), parent->number, slot_nr);
168
169 out:
170 up_write(&pci_bus_sem);
171 return slot;
172 err:
173 kfree(slot);
174 slot = ERR_PTR(err);
175 goto out;
176}
177EXPORT_SYMBOL_GPL(pci_create_slot);
178
179/**
Alex Chiangd25b7c82008-10-20 17:40:47 -0600180 * pci_renumber_slot - update %struct pci_slot -> number
Alex Chiangf46753c2008-06-10 15:28:50 -0600181 * @slot - %struct pci_slot to update
182 * @slot_nr - new number for slot
183 *
184 * The primary purpose of this interface is to allow callers who earlier
185 * created a placeholder slot in pci_create_slot() by passing a -1 as
186 * slot_nr, to update their %struct pci_slot with the correct @slot_nr.
187 */
Alex Chiangd25b7c82008-10-20 17:40:47 -0600188void pci_renumber_slot(struct pci_slot *slot, int slot_nr)
Alex Chiangf46753c2008-06-10 15:28:50 -0600189{
Alex Chiangf46753c2008-06-10 15:28:50 -0600190 struct pci_slot *tmp;
191
192 down_write(&pci_bus_sem);
193
194 list_for_each_entry(tmp, &slot->bus->slots, list) {
195 WARN_ON(tmp->number == slot_nr);
Alex Chiangd25b7c82008-10-20 17:40:47 -0600196 goto out;
Alex Chiangf46753c2008-06-10 15:28:50 -0600197 }
198
Alex Chiangf46753c2008-06-10 15:28:50 -0600199 slot->number = slot_nr;
Alex Chiangd25b7c82008-10-20 17:40:47 -0600200out:
Alex Chiangf46753c2008-06-10 15:28:50 -0600201 up_write(&pci_bus_sem);
202}
Alex Chiangd25b7c82008-10-20 17:40:47 -0600203EXPORT_SYMBOL_GPL(pci_renumber_slot);
Alex Chiangf46753c2008-06-10 15:28:50 -0600204
205/**
206 * pci_destroy_slot - decrement refcount for physical PCI slot
207 * @slot: struct pci_slot to decrement
208 *
209 * %struct pci_slot is refcounted, so destroying them is really easy; we
210 * just call kobject_put on its kobj and let our release methods do the
211 * rest.
212 */
213
214void pci_destroy_slot(struct pci_slot *slot)
215{
216 pr_debug("%s: dec refcount to %d on %04x:%02x:%02x\n", __func__,
217 atomic_read(&slot->kobj.kref.refcount) - 1,
218 pci_domain_nr(slot->bus), slot->bus->number, slot->number);
219
220 down_write(&pci_bus_sem);
221 kobject_put(&slot->kobj);
222 up_write(&pci_bus_sem);
223}
224EXPORT_SYMBOL_GPL(pci_destroy_slot);
225
226static int pci_slot_init(void)
227{
228 struct kset *pci_bus_kset;
229
230 pci_bus_kset = bus_get_kset(&pci_bus_type);
231 pci_slots_kset = kset_create_and_add("slots", NULL,
232 &pci_bus_kset->kobj);
233 if (!pci_slots_kset) {
234 printk(KERN_ERR "PCI: Slot initialization failure\n");
235 return -ENOMEM;
236 }
237 return 0;
238}
239
240subsys_initcall(pci_slot_init);