blob: c7260d4e339b565e3800c7217f2f474225f9b6e1 [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>
Alex Chiang62795042009-02-04 11:25:22 -07004 * Copyright (C) 2006-2009 Hewlett-Packard Development Company, L.P.
5 * Alex Chiang <achiang@hp.com>
Alex Chiangf46753c2008-06-10 15:28:50 -06006 */
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
Matthew Wilcox3749c512009-12-13 08:11:32 -050050/* these strings match up with the values in pci_bus_speed */
51static char *pci_bus_speed_strings[] = {
52 "33 MHz PCI", /* 0x00 */
53 "66 MHz PCI", /* 0x01 */
54 "66 MHz PCI-X", /* 0x02 */
55 "100 MHz PCI-X", /* 0x03 */
56 "133 MHz PCI-X", /* 0x04 */
57 NULL, /* 0x05 */
58 NULL, /* 0x06 */
59 NULL, /* 0x07 */
60 NULL, /* 0x08 */
61 "66 MHz PCI-X 266", /* 0x09 */
62 "100 MHz PCI-X 266", /* 0x0a */
63 "133 MHz PCI-X 266", /* 0x0b */
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -050064 "Unknown AGP", /* 0x0c */
65 "1x AGP", /* 0x0d */
66 "2x AGP", /* 0x0e */
67 "4x AGP", /* 0x0f */
68 "8x AGP", /* 0x10 */
Matthew Wilcox3749c512009-12-13 08:11:32 -050069 "66 MHz PCI-X 533", /* 0x11 */
70 "100 MHz PCI-X 533", /* 0x12 */
71 "133 MHz PCI-X 533", /* 0x13 */
72 "2.5 GT/s PCIe", /* 0x14 */
73 "5.0 GT/s PCIe", /* 0x15 */
74};
75
76static ssize_t bus_speed_read(enum pci_bus_speed speed, char *buf)
77{
78 const char *speed_string;
79
80 if (speed < ARRAY_SIZE(pci_bus_speed_strings))
81 speed_string = pci_bus_speed_strings[speed];
82 else
83 speed_string = "Unknown";
84
85 return sprintf(buf, "%s\n", speed_string);
86}
87
88static ssize_t max_speed_read_file(struct pci_slot *slot, char *buf)
89{
90 return bus_speed_read(slot->bus->max_bus_speed, buf);
91}
92
93static ssize_t cur_speed_read_file(struct pci_slot *slot, char *buf)
94{
95 return bus_speed_read(slot->bus->cur_bus_speed, buf);
96}
97
Alex Chiangf46753c2008-06-10 15:28:50 -060098static void pci_slot_release(struct kobject *kobj)
99{
Alex Chiangcef354d2008-09-02 09:40:51 -0600100 struct pci_dev *dev;
Alex Chiangf46753c2008-06-10 15:28:50 -0600101 struct pci_slot *slot = to_pci_slot(kobj);
102
Alex Chiang62795042009-02-04 11:25:22 -0700103 dev_dbg(&slot->bus->dev, "dev %02x, released physical slot %s\n",
104 slot->number, pci_slot_name(slot));
Alex Chiangf46753c2008-06-10 15:28:50 -0600105
Alex Chiangcef354d2008-09-02 09:40:51 -0600106 list_for_each_entry(dev, &slot->bus->devices, bus_list)
107 if (PCI_SLOT(dev->devfn) == slot->number)
108 dev->slot = NULL;
109
Alex Chiangf46753c2008-06-10 15:28:50 -0600110 list_del(&slot->list);
111
112 kfree(slot);
113}
114
115static struct pci_slot_attribute pci_slot_attr_address =
116 __ATTR(address, (S_IFREG | S_IRUGO), address_read_file, NULL);
Matthew Wilcox3749c512009-12-13 08:11:32 -0500117static struct pci_slot_attribute pci_slot_attr_max_speed =
118 __ATTR(max_bus_speed, (S_IFREG | S_IRUGO), max_speed_read_file, NULL);
119static struct pci_slot_attribute pci_slot_attr_cur_speed =
120 __ATTR(cur_bus_speed, (S_IFREG | S_IRUGO), cur_speed_read_file, NULL);
Alex Chiangf46753c2008-06-10 15:28:50 -0600121
122static struct attribute *pci_slot_default_attrs[] = {
123 &pci_slot_attr_address.attr,
Matthew Wilcox3749c512009-12-13 08:11:32 -0500124 &pci_slot_attr_max_speed.attr,
125 &pci_slot_attr_cur_speed.attr,
Alex Chiangf46753c2008-06-10 15:28:50 -0600126 NULL,
127};
128
129static struct kobj_type pci_slot_ktype = {
130 .sysfs_ops = &pci_slot_sysfs_ops,
131 .release = &pci_slot_release,
132 .default_attrs = pci_slot_default_attrs,
133};
134
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600135static char *make_slot_name(const char *name)
136{
137 char *new_name;
138 int len, max, dup;
139
140 new_name = kstrdup(name, GFP_KERNEL);
141 if (!new_name)
142 return NULL;
143
144 /*
145 * Make sure we hit the realloc case the first time through the
146 * loop. 'len' will be strlen(name) + 3 at that point which is
147 * enough space for "name-X" and the trailing NUL.
148 */
149 len = strlen(name) + 2;
150 max = 1;
151 dup = 1;
152
153 for (;;) {
154 struct kobject *dup_slot;
155 dup_slot = kset_find_obj(pci_slots_kset, new_name);
156 if (!dup_slot)
157 break;
158 kobject_put(dup_slot);
159 if (dup == max) {
160 len++;
161 max *= 10;
162 kfree(new_name);
163 new_name = kmalloc(len, GFP_KERNEL);
164 if (!new_name)
165 break;
166 }
167 sprintf(new_name, "%s-%d", name, dup++);
168 }
169
170 return new_name;
171}
172
173static int rename_slot(struct pci_slot *slot, const char *name)
174{
175 int result = 0;
176 char *slot_name;
177
Alex Chiang0ad772e2008-10-20 17:41:07 -0600178 if (strcmp(pci_slot_name(slot), name) == 0)
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600179 return result;
180
181 slot_name = make_slot_name(name);
182 if (!slot_name)
183 return -ENOMEM;
184
185 result = kobject_rename(&slot->kobj, slot_name);
186 kfree(slot_name);
187
188 return result;
189}
190
191static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
192{
193 struct pci_slot *slot;
194 /*
195 * We already hold pci_bus_sem so don't worry
196 */
197 list_for_each_entry(slot, &parent->slots, list)
198 if (slot->number == slot_nr) {
199 kobject_get(&slot->kobj);
200 return slot;
201 }
202
203 return NULL;
204}
205
Alex Chiangf46753c2008-06-10 15:28:50 -0600206/**
207 * pci_create_slot - create or increment refcount for physical PCI slot
208 * @parent: struct pci_bus of parent bridge
209 * @slot_nr: PCI_SLOT(pci_dev->devfn) or -1 for placeholder
210 * @name: user visible string presented in /sys/bus/pci/slots/<name>
Alex Chiang828f3762008-10-20 17:40:52 -0600211 * @hotplug: set if caller is hotplug driver, NULL otherwise
Alex Chiangf46753c2008-06-10 15:28:50 -0600212 *
213 * PCI slots have first class attributes such as address, speed, width,
214 * and a &struct pci_slot is used to manage them. This interface will
215 * either return a new &struct pci_slot to the caller, or if the pci_slot
216 * already exists, its refcount will be incremented.
217 *
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600218 * Slots are uniquely identified by a @pci_bus, @slot_nr tuple.
219 *
220 * There are known platforms with broken firmware that assign the same
221 * name to multiple slots. Workaround these broken platforms by renaming
222 * the slots on behalf of the caller. If firmware assigns name N to
223 * multiple slots:
224 *
225 * The first slot is assigned N
226 * The second slot is assigned N-1
227 * The third slot is assigned N-2
228 * etc.
Alex Chiangf46753c2008-06-10 15:28:50 -0600229 *
230 * Placeholder slots:
231 * In most cases, @pci_bus, @slot_nr will be sufficient to uniquely identify
232 * a slot. There is one notable exception - pSeries (rpaphp), where the
233 * @slot_nr cannot be determined until a device is actually inserted into
234 * the slot. In this scenario, the caller may pass -1 for @slot_nr.
235 *
236 * The following semantics are imposed when the caller passes @slot_nr ==
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600237 * -1. First, we no longer check for an existing %struct pci_slot, as there
238 * may be many slots with @slot_nr of -1. The other change in semantics is
Alex Chiangf46753c2008-06-10 15:28:50 -0600239 * user-visible, which is the 'address' parameter presented in sysfs will
240 * consist solely of a dddd:bb tuple, where dddd is the PCI domain of the
241 * %struct pci_bus and bb is the bus number. In other words, the devfn of
242 * the 'placeholder' slot will not be displayed.
243 */
Alex Chiangf46753c2008-06-10 15:28:50 -0600244struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
Alex Chiang828f3762008-10-20 17:40:52 -0600245 const char *name,
246 struct hotplug_slot *hotplug)
Alex Chiangf46753c2008-06-10 15:28:50 -0600247{
Alex Chiangcef354d2008-09-02 09:40:51 -0600248 struct pci_dev *dev;
Alex Chiangf46753c2008-06-10 15:28:50 -0600249 struct pci_slot *slot;
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600250 int err = 0;
251 char *slot_name = NULL;
Alex Chiangf46753c2008-06-10 15:28:50 -0600252
253 down_write(&pci_bus_sem);
254
255 if (slot_nr == -1)
256 goto placeholder;
257
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600258 /*
259 * Hotplug drivers are allowed to rename an existing slot,
260 * but only if not already claimed.
261 */
262 slot = get_slot(parent, slot_nr);
263 if (slot) {
264 if (hotplug) {
265 if ((err = slot->hotplug ? -EBUSY : 0)
266 || (err = rename_slot(slot, name))) {
267 kobject_put(&slot->kobj);
268 slot = NULL;
269 goto err;
270 }
Alex Chiangf46753c2008-06-10 15:28:50 -0600271 }
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600272 goto out;
Alex Chiangf46753c2008-06-10 15:28:50 -0600273 }
274
275placeholder:
276 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
277 if (!slot) {
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600278 err = -ENOMEM;
279 goto err;
Alex Chiangf46753c2008-06-10 15:28:50 -0600280 }
281
282 slot->bus = parent;
283 slot->number = slot_nr;
284
285 slot->kobj.kset = pci_slots_kset;
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600286
287 slot_name = make_slot_name(name);
288 if (!slot_name) {
289 err = -ENOMEM;
Alex Chiangf46753c2008-06-10 15:28:50 -0600290 goto err;
291 }
292
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600293 err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
294 "%s", slot_name);
295 if (err)
296 goto err;
297
Alex Chiangf46753c2008-06-10 15:28:50 -0600298 INIT_LIST_HEAD(&slot->list);
299 list_add(&slot->list, &parent->slots);
300
Alex Chiangcef354d2008-09-02 09:40:51 -0600301 list_for_each_entry(dev, &parent->devices, bus_list)
302 if (PCI_SLOT(dev->devfn) == slot_nr)
303 dev->slot = slot;
304
Alex Chiang62795042009-02-04 11:25:22 -0700305 dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
306 slot_nr, pci_slot_name(slot));
Alex Chiangf46753c2008-06-10 15:28:50 -0600307
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600308out:
Alex Chiang3b5dd452008-12-01 18:17:21 -0700309 kfree(slot_name);
Alex Chiangf46753c2008-06-10 15:28:50 -0600310 up_write(&pci_bus_sem);
311 return slot;
Alex Chiang5fe6cc62008-10-20 17:41:02 -0600312err:
Alex Chiangf46753c2008-06-10 15:28:50 -0600313 kfree(slot);
314 slot = ERR_PTR(err);
315 goto out;
316}
317EXPORT_SYMBOL_GPL(pci_create_slot);
318
319/**
Alex Chiangd25b7c82008-10-20 17:40:47 -0600320 * pci_renumber_slot - update %struct pci_slot -> number
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700321 * @slot: &struct pci_slot to update
322 * @slot_nr: new number for slot
Alex Chiangf46753c2008-06-10 15:28:50 -0600323 *
324 * The primary purpose of this interface is to allow callers who earlier
325 * created a placeholder slot in pci_create_slot() by passing a -1 as
326 * slot_nr, to update their %struct pci_slot with the correct @slot_nr.
327 */
Alex Chiangd25b7c82008-10-20 17:40:47 -0600328void pci_renumber_slot(struct pci_slot *slot, int slot_nr)
Alex Chiangf46753c2008-06-10 15:28:50 -0600329{
Alex Chiangf46753c2008-06-10 15:28:50 -0600330 struct pci_slot *tmp;
331
332 down_write(&pci_bus_sem);
333
334 list_for_each_entry(tmp, &slot->bus->slots, list) {
335 WARN_ON(tmp->number == slot_nr);
Alex Chiangd25b7c82008-10-20 17:40:47 -0600336 goto out;
Alex Chiangf46753c2008-06-10 15:28:50 -0600337 }
338
Alex Chiangf46753c2008-06-10 15:28:50 -0600339 slot->number = slot_nr;
Alex Chiangd25b7c82008-10-20 17:40:47 -0600340out:
Alex Chiangf46753c2008-06-10 15:28:50 -0600341 up_write(&pci_bus_sem);
342}
Alex Chiangd25b7c82008-10-20 17:40:47 -0600343EXPORT_SYMBOL_GPL(pci_renumber_slot);
Alex Chiangf46753c2008-06-10 15:28:50 -0600344
345/**
346 * pci_destroy_slot - decrement refcount for physical PCI slot
347 * @slot: struct pci_slot to decrement
348 *
349 * %struct pci_slot is refcounted, so destroying them is really easy; we
350 * just call kobject_put on its kobj and let our release methods do the
351 * rest.
352 */
Alex Chiangf46753c2008-06-10 15:28:50 -0600353void pci_destroy_slot(struct pci_slot *slot)
354{
Alex Chiang62795042009-02-04 11:25:22 -0700355 dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
356 slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
Alex Chiangf46753c2008-06-10 15:28:50 -0600357
358 down_write(&pci_bus_sem);
359 kobject_put(&slot->kobj);
360 up_write(&pci_bus_sem);
361}
362EXPORT_SYMBOL_GPL(pci_destroy_slot);
363
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900364#if defined(CONFIG_HOTPLUG_PCI) || defined(CONFIG_HOTPLUG_PCI_MODULE)
365#include <linux/pci_hotplug.h>
366/**
367 * pci_hp_create_link - create symbolic link to the hotplug driver module.
Randy Dunlap503998c2009-06-24 09:18:14 -0700368 * @pci_slot: struct pci_slot
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900369 *
370 * Helper function for pci_hotplug_core.c to create symbolic link to
371 * the hotplug driver module.
372 */
373void pci_hp_create_module_link(struct pci_slot *pci_slot)
374{
375 struct hotplug_slot *slot = pci_slot->hotplug;
376 struct kobject *kobj = NULL;
377 int no_warn;
378
379 if (!slot || !slot->ops)
380 return;
381 kobj = kset_find_obj(module_kset, slot->ops->mod_name);
382 if (!kobj)
383 return;
384 no_warn = sysfs_create_link(&pci_slot->kobj, kobj, "module");
385 kobject_put(kobj);
386}
387EXPORT_SYMBOL_GPL(pci_hp_create_module_link);
388
389/**
390 * pci_hp_remove_link - remove symbolic link to the hotplug driver module.
Randy Dunlap503998c2009-06-24 09:18:14 -0700391 * @pci_slot: struct pci_slot
Kenji Kaneshigec825bc92009-06-16 11:01:25 +0900392 *
393 * Helper function for pci_hotplug_core.c to remove symbolic link to
394 * the hotplug driver module.
395 */
396void pci_hp_remove_module_link(struct pci_slot *pci_slot)
397{
398 sysfs_remove_link(&pci_slot->kobj, "module");
399}
400EXPORT_SYMBOL_GPL(pci_hp_remove_module_link);
401#endif
402
Alex Chiangf46753c2008-06-10 15:28:50 -0600403static int pci_slot_init(void)
404{
405 struct kset *pci_bus_kset;
406
407 pci_bus_kset = bus_get_kset(&pci_bus_type);
408 pci_slots_kset = kset_create_and_add("slots", NULL,
409 &pci_bus_kset->kobj);
410 if (!pci_slots_kset) {
411 printk(KERN_ERR "PCI: Slot initialization failure\n");
412 return -ENOMEM;
413 }
414 return 0;
415}
416
417subsys_initcall(pci_slot_init);