aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pci/slot.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/pci/slot.c')
-rw-r--r--drivers/pci/slot.c160
1 files changed, 121 insertions, 39 deletions
diff --git a/drivers/pci/slot.c b/drivers/pci/slot.c
index 0c6db03698ea..4dd1c3e157ae 100644
--- a/drivers/pci/slot.c
+++ b/drivers/pci/slot.c
@@ -78,18 +78,100 @@ static struct kobj_type pci_slot_ktype = {
78 .default_attrs = pci_slot_default_attrs, 78 .default_attrs = pci_slot_default_attrs,
79}; 79};
80 80
81static char *make_slot_name(const char *name)
82{
83 char *new_name;
84 int len, max, dup;
85
86 new_name = kstrdup(name, GFP_KERNEL);
87 if (!new_name)
88 return NULL;
89
90 /*
91 * Make sure we hit the realloc case the first time through the
92 * loop. 'len' will be strlen(name) + 3 at that point which is
93 * enough space for "name-X" and the trailing NUL.
94 */
95 len = strlen(name) + 2;
96 max = 1;
97 dup = 1;
98
99 for (;;) {
100 struct kobject *dup_slot;
101 dup_slot = kset_find_obj(pci_slots_kset, new_name);
102 if (!dup_slot)
103 break;
104 kobject_put(dup_slot);
105 if (dup == max) {
106 len++;
107 max *= 10;
108 kfree(new_name);
109 new_name = kmalloc(len, GFP_KERNEL);
110 if (!new_name)
111 break;
112 }
113 sprintf(new_name, "%s-%d", name, dup++);
114 }
115
116 return new_name;
117}
118
119static int rename_slot(struct pci_slot *slot, const char *name)
120{
121 int result = 0;
122 char *slot_name;
123
124 if (strcmp(pci_slot_name(slot), name) == 0)
125 return result;
126
127 slot_name = make_slot_name(name);
128 if (!slot_name)
129 return -ENOMEM;
130
131 result = kobject_rename(&slot->kobj, slot_name);
132 kfree(slot_name);
133
134 return result;
135}
136
137static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
138{
139 struct pci_slot *slot;
140 /*
141 * We already hold pci_bus_sem so don't worry
142 */
143 list_for_each_entry(slot, &parent->slots, list)
144 if (slot->number == slot_nr) {
145 kobject_get(&slot->kobj);
146 return slot;
147 }
148
149 return NULL;
150}
151
81/** 152/**
82 * pci_create_slot - create or increment refcount for physical PCI slot 153 * pci_create_slot - create or increment refcount for physical PCI slot
83 * @parent: struct pci_bus of parent bridge 154 * @parent: struct pci_bus of parent bridge
84 * @slot_nr: PCI_SLOT(pci_dev->devfn) or -1 for placeholder 155 * @slot_nr: PCI_SLOT(pci_dev->devfn) or -1 for placeholder
85 * @name: user visible string presented in /sys/bus/pci/slots/<name> 156 * @name: user visible string presented in /sys/bus/pci/slots/<name>
157 * @hotplug: set if caller is hotplug driver, NULL otherwise
86 * 158 *
87 * PCI slots have first class attributes such as address, speed, width, 159 * PCI slots have first class attributes such as address, speed, width,
88 * and a &struct pci_slot is used to manage them. This interface will 160 * and a &struct pci_slot is used to manage them. This interface will
89 * either return a new &struct pci_slot to the caller, or if the pci_slot 161 * either return a new &struct pci_slot to the caller, or if the pci_slot
90 * already exists, its refcount will be incremented. 162 * already exists, its refcount will be incremented.
91 * 163 *
92 * Slots are uniquely identified by a @pci_bus, @slot_nr, @name tuple. 164 * Slots are uniquely identified by a @pci_bus, @slot_nr tuple.
165 *
166 * There are known platforms with broken firmware that assign the same
167 * name to multiple slots. Workaround these broken platforms by renaming
168 * the slots on behalf of the caller. If firmware assigns name N to
169 * multiple slots:
170 *
171 * The first slot is assigned N
172 * The second slot is assigned N-1
173 * The third slot is assigned N-2
174 * etc.
93 * 175 *
94 * Placeholder slots: 176 * Placeholder slots:
95 * In most cases, @pci_bus, @slot_nr will be sufficient to uniquely identify 177 * In most cases, @pci_bus, @slot_nr will be sufficient to uniquely identify
@@ -98,61 +180,67 @@ static struct kobj_type pci_slot_ktype = {
98 * the slot. In this scenario, the caller may pass -1 for @slot_nr. 180 * the slot. In this scenario, the caller may pass -1 for @slot_nr.
99 * 181 *
100 * The following semantics are imposed when the caller passes @slot_nr == 182 * The following semantics are imposed when the caller passes @slot_nr ==
101 * -1. First, the check for existing %struct pci_slot is skipped, as the 183 * -1. First, we no longer check for an existing %struct pci_slot, as there
102 * caller may know about several unpopulated slots on a given %struct 184 * may be many slots with @slot_nr of -1. The other change in semantics is
103 * pci_bus, and each slot would have a @slot_nr of -1. Uniqueness for
104 * these slots is then determined by the @name parameter. We expect
105 * kobject_init_and_add() to warn us if the caller attempts to create
106 * multiple slots with the same name. The other change in semantics is
107 * user-visible, which is the 'address' parameter presented in sysfs will 185 * user-visible, which is the 'address' parameter presented in sysfs will
108 * consist solely of a dddd:bb tuple, where dddd is the PCI domain of the 186 * consist solely of a dddd:bb tuple, where dddd is the PCI domain of the
109 * %struct pci_bus and bb is the bus number. In other words, the devfn of 187 * %struct pci_bus and bb is the bus number. In other words, the devfn of
110 * the 'placeholder' slot will not be displayed. 188 * the 'placeholder' slot will not be displayed.
111 */ 189 */
112
113struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr, 190struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
114 const char *name) 191 const char *name,
192 struct hotplug_slot *hotplug)
115{ 193{
116 struct pci_dev *dev; 194 struct pci_dev *dev;
117 struct pci_slot *slot; 195 struct pci_slot *slot;
118 int err; 196 int err = 0;
197 char *slot_name = NULL;
119 198
120 down_write(&pci_bus_sem); 199 down_write(&pci_bus_sem);
121 200
122 if (slot_nr == -1) 201 if (slot_nr == -1)
123 goto placeholder; 202 goto placeholder;
124 203
125 /* If we've already created this slot, bump refcount and return. */ 204 /*
126 list_for_each_entry(slot, &parent->slots, list) { 205 * Hotplug drivers are allowed to rename an existing slot,
127 if (slot->number == slot_nr) { 206 * but only if not already claimed.
128 kobject_get(&slot->kobj); 207 */
129 pr_debug("%s: inc refcount to %d on %04x:%02x:%02x\n", 208 slot = get_slot(parent, slot_nr);
130 __func__, 209 if (slot) {
131 atomic_read(&slot->kobj.kref.refcount), 210 if (hotplug) {
132 pci_domain_nr(parent), parent->number, 211 if ((err = slot->hotplug ? -EBUSY : 0)
133 slot_nr); 212 || (err = rename_slot(slot, name))) {
134 goto out; 213 kobject_put(&slot->kobj);
214 slot = NULL;
215 goto err;
216 }
135 } 217 }
218 goto out;
136 } 219 }
137 220
138placeholder: 221placeholder:
139 slot = kzalloc(sizeof(*slot), GFP_KERNEL); 222 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
140 if (!slot) { 223 if (!slot) {
141 slot = ERR_PTR(-ENOMEM); 224 err = -ENOMEM;
142 goto out; 225 goto err;
143 } 226 }
144 227
145 slot->bus = parent; 228 slot->bus = parent;
146 slot->number = slot_nr; 229 slot->number = slot_nr;
147 230
148 slot->kobj.kset = pci_slots_kset; 231 slot->kobj.kset = pci_slots_kset;
149 err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL, 232
150 "%s", name); 233 slot_name = make_slot_name(name);
151 if (err) { 234 if (!slot_name) {
152 printk(KERN_ERR "Unable to register kobject %s\n", name); 235 err = -ENOMEM;
153 goto err; 236 goto err;
154 } 237 }
155 238
239 err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
240 "%s", slot_name);
241 if (err)
242 goto err;
243
156 INIT_LIST_HEAD(&slot->list); 244 INIT_LIST_HEAD(&slot->list);
157 list_add(&slot->list, &parent->slots); 245 list_add(&slot->list, &parent->slots);
158 246
@@ -164,10 +252,10 @@ placeholder:
164 pr_debug("%s: created pci_slot on %04x:%02x:%02x\n", 252 pr_debug("%s: created pci_slot on %04x:%02x:%02x\n",
165 __func__, pci_domain_nr(parent), parent->number, slot_nr); 253 __func__, pci_domain_nr(parent), parent->number, slot_nr);
166 254
167 out: 255out:
168 up_write(&pci_bus_sem); 256 up_write(&pci_bus_sem);
169 return slot; 257 return slot;
170 err: 258err:
171 kfree(slot); 259 kfree(slot);
172 slot = ERR_PTR(err); 260 slot = ERR_PTR(err);
173 goto out; 261 goto out;
@@ -175,7 +263,7 @@ placeholder:
175EXPORT_SYMBOL_GPL(pci_create_slot); 263EXPORT_SYMBOL_GPL(pci_create_slot);
176 264
177/** 265/**
178 * pci_update_slot_number - update %struct pci_slot -> number 266 * pci_renumber_slot - update %struct pci_slot -> number
179 * @slot - %struct pci_slot to update 267 * @slot - %struct pci_slot to update
180 * @slot_nr - new number for slot 268 * @slot_nr - new number for slot
181 * 269 *
@@ -183,27 +271,22 @@ EXPORT_SYMBOL_GPL(pci_create_slot);
183 * created a placeholder slot in pci_create_slot() by passing a -1 as 271 * created a placeholder slot in pci_create_slot() by passing a -1 as
184 * slot_nr, to update their %struct pci_slot with the correct @slot_nr. 272 * slot_nr, to update their %struct pci_slot with the correct @slot_nr.
185 */ 273 */
186 274void pci_renumber_slot(struct pci_slot *slot, int slot_nr)
187void pci_update_slot_number(struct pci_slot *slot, int slot_nr)
188{ 275{
189 int name_count = 0;
190 struct pci_slot *tmp; 276 struct pci_slot *tmp;
191 277
192 down_write(&pci_bus_sem); 278 down_write(&pci_bus_sem);
193 279
194 list_for_each_entry(tmp, &slot->bus->slots, list) { 280 list_for_each_entry(tmp, &slot->bus->slots, list) {
195 WARN_ON(tmp->number == slot_nr); 281 WARN_ON(tmp->number == slot_nr);
196 if (!strcmp(kobject_name(&tmp->kobj), kobject_name(&slot->kobj))) 282 goto out;
197 name_count++;
198 } 283 }
199 284
200 if (name_count > 1)
201 printk(KERN_WARNING "pci_update_slot_number found %d slots with the same name: %s\n", name_count, kobject_name(&slot->kobj));
202
203 slot->number = slot_nr; 285 slot->number = slot_nr;
286out:
204 up_write(&pci_bus_sem); 287 up_write(&pci_bus_sem);
205} 288}
206EXPORT_SYMBOL_GPL(pci_update_slot_number); 289EXPORT_SYMBOL_GPL(pci_renumber_slot);
207 290
208/** 291/**
209 * pci_destroy_slot - decrement refcount for physical PCI slot 292 * pci_destroy_slot - decrement refcount for physical PCI slot
@@ -213,7 +296,6 @@ EXPORT_SYMBOL_GPL(pci_update_slot_number);
213 * just call kobject_put on its kobj and let our release methods do the 296 * just call kobject_put on its kobj and let our release methods do the
214 * rest. 297 * rest.
215 */ 298 */
216
217void pci_destroy_slot(struct pci_slot *slot) 299void pci_destroy_slot(struct pci_slot *slot)
218{ 300{
219 pr_debug("%s: dec refcount to %d on %04x:%02x:%02x\n", __func__, 301 pr_debug("%s: dec refcount to %d on %04x:%02x:%02x\n", __func__,