1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2006 Matthew Wilcox <matthew@wil.cx>
4 * Copyright (C) 2006-2009 Hewlett-Packard Development Company, L.P.
5 * Alex Chiang <achiang@hp.com>
6 */
7
8 #include <linux/kobject.h>
9 #include <linux/slab.h>
10 #include <linux/pci.h>
11 #include <linux/err.h>
12 #include "pci.h"
13
14 struct kset *pci_slots_kset;
15 EXPORT_SYMBOL_GPL(pci_slots_kset);
16
pci_slot_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)17 static ssize_t pci_slot_attr_show(struct kobject *kobj,
18 struct attribute *attr, char *buf)
19 {
20 struct pci_slot *slot = to_pci_slot(kobj);
21 struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
22 return attribute->show ? attribute->show(slot, buf) : -EIO;
23 }
24
pci_slot_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)25 static ssize_t pci_slot_attr_store(struct kobject *kobj,
26 struct attribute *attr, const char *buf, size_t len)
27 {
28 struct pci_slot *slot = to_pci_slot(kobj);
29 struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
30 return attribute->store ? attribute->store(slot, buf, len) : -EIO;
31 }
32
33 static const struct sysfs_ops pci_slot_sysfs_ops = {
34 .show = pci_slot_attr_show,
35 .store = pci_slot_attr_store,
36 };
37
address_read_file(struct pci_slot * slot,char * buf)38 static ssize_t address_read_file(struct pci_slot *slot, char *buf)
39 {
40 if (slot->number == 0xff)
41 return sysfs_emit(buf, "%04x:%02x\n",
42 pci_domain_nr(slot->bus),
43 slot->bus->number);
44
45 return sysfs_emit(buf, "%04x:%02x:%02x\n",
46 pci_domain_nr(slot->bus),
47 slot->bus->number,
48 slot->number);
49 }
50
bus_speed_read(enum pci_bus_speed speed,char * buf)51 static ssize_t bus_speed_read(enum pci_bus_speed speed, char *buf)
52 {
53 return sysfs_emit(buf, "%s\n", pci_speed_string(speed));
54 }
55
max_speed_read_file(struct pci_slot * slot,char * buf)56 static ssize_t max_speed_read_file(struct pci_slot *slot, char *buf)
57 {
58 return bus_speed_read(slot->bus->max_bus_speed, buf);
59 }
60
cur_speed_read_file(struct pci_slot * slot,char * buf)61 static ssize_t cur_speed_read_file(struct pci_slot *slot, char *buf)
62 {
63 return bus_speed_read(slot->bus->cur_bus_speed, buf);
64 }
65
pci_slot_release(struct kobject * kobj)66 static void pci_slot_release(struct kobject *kobj)
67 {
68 struct pci_dev *dev;
69 struct pci_slot *slot = to_pci_slot(kobj);
70
71 dev_dbg(&slot->bus->dev, "dev %02x, released physical slot %s\n",
72 slot->number, pci_slot_name(slot));
73
74 down_read(&pci_bus_sem);
75 list_for_each_entry(dev, &slot->bus->devices, bus_list)
76 if (PCI_SLOT(dev->devfn) == slot->number)
77 dev->slot = NULL;
78 up_read(&pci_bus_sem);
79
80 list_del(&slot->list);
81 pci_bus_put(slot->bus);
82
83 kfree(slot);
84 }
85
86 static struct pci_slot_attribute pci_slot_attr_address =
87 __ATTR(address, S_IRUGO, address_read_file, NULL);
88 static struct pci_slot_attribute pci_slot_attr_max_speed =
89 __ATTR(max_bus_speed, S_IRUGO, max_speed_read_file, NULL);
90 static struct pci_slot_attribute pci_slot_attr_cur_speed =
91 __ATTR(cur_bus_speed, S_IRUGO, cur_speed_read_file, NULL);
92
93 static struct attribute *pci_slot_default_attrs[] = {
94 &pci_slot_attr_address.attr,
95 &pci_slot_attr_max_speed.attr,
96 &pci_slot_attr_cur_speed.attr,
97 NULL,
98 };
99 ATTRIBUTE_GROUPS(pci_slot_default);
100
101 static const struct kobj_type pci_slot_ktype = {
102 .sysfs_ops = &pci_slot_sysfs_ops,
103 .release = &pci_slot_release,
104 .default_groups = pci_slot_default_groups,
105 };
106
make_slot_name(const char * name)107 static char *make_slot_name(const char *name)
108 {
109 char *new_name;
110 int len, max, dup;
111
112 new_name = kstrdup(name, GFP_KERNEL);
113 if (!new_name)
114 return NULL;
115
116 /*
117 * Make sure we hit the realloc case the first time through the
118 * loop. 'len' will be strlen(name) + 3 at that point which is
119 * enough space for "name-X" and the trailing NUL.
120 */
121 len = strlen(name) + 2;
122 max = 1;
123 dup = 1;
124
125 for (;;) {
126 struct kobject *dup_slot;
127 dup_slot = kset_find_obj(pci_slots_kset, new_name);
128 if (!dup_slot)
129 break;
130 kobject_put(dup_slot);
131 if (dup == max) {
132 len++;
133 max *= 10;
134 kfree(new_name);
135 new_name = kmalloc(len, GFP_KERNEL);
136 if (!new_name)
137 break;
138 }
139 sprintf(new_name, "%s-%d", name, dup++);
140 }
141
142 return new_name;
143 }
144
rename_slot(struct pci_slot * slot,const char * name)145 static int rename_slot(struct pci_slot *slot, const char *name)
146 {
147 int result = 0;
148 char *slot_name;
149
150 if (strcmp(pci_slot_name(slot), name) == 0)
151 return result;
152
153 slot_name = make_slot_name(name);
154 if (!slot_name)
155 return -ENOMEM;
156
157 result = kobject_rename(&slot->kobj, slot_name);
158 kfree(slot_name);
159
160 return result;
161 }
162
pci_dev_assign_slot(struct pci_dev * dev)163 void pci_dev_assign_slot(struct pci_dev *dev)
164 {
165 struct pci_slot *slot;
166
167 mutex_lock(&pci_slot_mutex);
168 list_for_each_entry(slot, &dev->bus->slots, list)
169 if (PCI_SLOT(dev->devfn) == slot->number)
170 dev->slot = slot;
171 mutex_unlock(&pci_slot_mutex);
172 }
173
get_slot(struct pci_bus * parent,int slot_nr)174 static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
175 {
176 struct pci_slot *slot;
177
178 /* We already hold pci_slot_mutex */
179 list_for_each_entry(slot, &parent->slots, list)
180 if (slot->number == slot_nr) {
181 kobject_get(&slot->kobj);
182 return slot;
183 }
184
185 return NULL;
186 }
187
188 /**
189 * pci_create_slot - create or increment refcount for physical PCI slot
190 * @parent: struct pci_bus of parent bridge
191 * @slot_nr: PCI_SLOT(pci_dev->devfn) or -1 for placeholder
192 * @name: user visible string presented in /sys/bus/pci/slots/<name>
193 * @hotplug: set if caller is hotplug driver, NULL otherwise
194 *
195 * PCI slots have first class attributes such as address, speed, width,
196 * and a &struct pci_slot is used to manage them. This interface will
197 * either return a new &struct pci_slot to the caller, or if the pci_slot
198 * already exists, its refcount will be incremented.
199 *
200 * Slots are uniquely identified by a @pci_bus, @slot_nr tuple.
201 *
202 * There are known platforms with broken firmware that assign the same
203 * name to multiple slots. Workaround these broken platforms by renaming
204 * the slots on behalf of the caller. If firmware assigns name N to
205 * multiple slots:
206 *
207 * The first slot is assigned N
208 * The second slot is assigned N-1
209 * The third slot is assigned N-2
210 * etc.
211 *
212 * Placeholder slots:
213 * In most cases, @pci_bus, @slot_nr will be sufficient to uniquely identify
214 * a slot. There is one notable exception - pSeries (rpaphp), where the
215 * @slot_nr cannot be determined until a device is actually inserted into
216 * the slot. In this scenario, the caller may pass -1 for @slot_nr.
217 *
218 * The following semantics are imposed when the caller passes @slot_nr ==
219 * -1. First, we no longer check for an existing %struct pci_slot, as there
220 * may be many slots with @slot_nr of -1. The other change in semantics is
221 * user-visible, which is the 'address' parameter presented in sysfs will
222 * consist solely of a dddd:bb tuple, where dddd is the PCI domain of the
223 * %struct pci_bus and bb is the bus number. In other words, the devfn of
224 * the 'placeholder' slot will not be displayed.
225 */
pci_create_slot(struct pci_bus * parent,int slot_nr,const char * name,struct hotplug_slot * hotplug)226 struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
227 const char *name,
228 struct hotplug_slot *hotplug)
229 {
230 struct pci_dev *dev;
231 struct pci_slot *slot;
232 int err = 0;
233 char *slot_name = NULL;
234
235 mutex_lock(&pci_slot_mutex);
236
237 if (slot_nr == -1)
238 goto placeholder;
239
240 /*
241 * Hotplug drivers are allowed to rename an existing slot,
242 * but only if not already claimed.
243 */
244 slot = get_slot(parent, slot_nr);
245 if (slot) {
246 if (hotplug) {
247 if (slot->hotplug) {
248 err = -EBUSY;
249 goto put_slot;
250 }
251 err = rename_slot(slot, name);
252 if (err)
253 goto put_slot;
254 }
255 goto out;
256 }
257
258 placeholder:
259 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
260 if (!slot) {
261 err = -ENOMEM;
262 goto err;
263 }
264
265 slot->bus = pci_bus_get(parent);
266 slot->number = slot_nr;
267
268 slot->kobj.kset = pci_slots_kset;
269
270 slot_name = make_slot_name(name);
271 if (!slot_name) {
272 err = -ENOMEM;
273 pci_bus_put(slot->bus);
274 kfree(slot);
275 goto err;
276 }
277
278 INIT_LIST_HEAD(&slot->list);
279 list_add(&slot->list, &parent->slots);
280
281 err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
282 "%s", slot_name);
283 if (err)
284 goto put_slot;
285
286 down_read(&pci_bus_sem);
287 list_for_each_entry(dev, &parent->devices, bus_list)
288 if (PCI_SLOT(dev->devfn) == slot_nr)
289 dev->slot = slot;
290 up_read(&pci_bus_sem);
291
292 dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
293 slot_nr, pci_slot_name(slot));
294
295 out:
296 kfree(slot_name);
297 mutex_unlock(&pci_slot_mutex);
298 return slot;
299
300 put_slot:
301 kobject_put(&slot->kobj);
302 err:
303 slot = ERR_PTR(err);
304 goto out;
305 }
306 EXPORT_SYMBOL_GPL(pci_create_slot);
307
308 /**
309 * pci_destroy_slot - decrement refcount for physical PCI slot
310 * @slot: struct pci_slot to decrement
311 *
312 * %struct pci_slot is refcounted, so destroying them is really easy; we
313 * just call kobject_put on its kobj and let our release methods do the
314 * rest.
315 */
pci_destroy_slot(struct pci_slot * slot)316 void pci_destroy_slot(struct pci_slot *slot)
317 {
318 dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
319 slot->number, kref_read(&slot->kobj.kref) - 1);
320
321 mutex_lock(&pci_slot_mutex);
322 kobject_put(&slot->kobj);
323 mutex_unlock(&pci_slot_mutex);
324 }
325 EXPORT_SYMBOL_GPL(pci_destroy_slot);
326
pci_slot_init(void)327 static int pci_slot_init(void)
328 {
329 struct kset *pci_bus_kset;
330
331 pci_bus_kset = bus_get_kset(&pci_bus_type);
332 pci_slots_kset = kset_create_and_add("slots", NULL,
333 &pci_bus_kset->kobj);
334 if (!pci_slots_kset) {
335 pr_err("PCI: Slot initialization failure\n");
336 return -ENOMEM;
337 }
338 return 0;
339 }
340
341 subsys_initcall(pci_slot_init);
342