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 == PCI_SLOT_PLACEHOLDER)
41 return sysfs_emit(buf, "%04x:%02x\n",
42 pci_domain_nr(slot->bus),
43 slot->bus->number);
44
45 /*
46 * Preserve legacy ABI expectations that hotplug drivers that manage
47 * multiple devices per slot emit 0 for the device number.
48 */
49 if (slot->number == PCI_SLOT_ALL_DEVICES)
50 return sysfs_emit(buf, "%04x:%02x:00\n",
51 pci_domain_nr(slot->bus),
52 slot->bus->number);
53
54 return sysfs_emit(buf, "%04x:%02x:%02x\n",
55 pci_domain_nr(slot->bus),
56 slot->bus->number,
57 slot->number);
58 }
59
bus_speed_read(enum pci_bus_speed speed,char * buf)60 static ssize_t bus_speed_read(enum pci_bus_speed speed, char *buf)
61 {
62 return sysfs_emit(buf, "%s\n", pci_speed_string(speed));
63 }
64
max_speed_read_file(struct pci_slot * slot,char * buf)65 static ssize_t max_speed_read_file(struct pci_slot *slot, char *buf)
66 {
67 return bus_speed_read(slot->bus->max_bus_speed, buf);
68 }
69
cur_speed_read_file(struct pci_slot * slot,char * buf)70 static ssize_t cur_speed_read_file(struct pci_slot *slot, char *buf)
71 {
72 return bus_speed_read(slot->bus->cur_bus_speed, buf);
73 }
74
pci_dev_matches_slot(struct pci_dev * dev,struct pci_slot * slot)75 static bool pci_dev_matches_slot(struct pci_dev *dev, struct pci_slot *slot)
76 {
77 if (slot->per_func_slot)
78 return dev->devfn == slot->number;
79
80 return slot->number == PCI_SLOT_ALL_DEVICES ||
81 PCI_SLOT(dev->devfn) == slot->number;
82 }
83
pci_slot_enabled_per_func(void)84 static bool pci_slot_enabled_per_func(void)
85 {
86 if (IS_ENABLED(CONFIG_S390))
87 return true;
88
89 return false;
90 }
91
pci_slot_release(struct kobject * kobj)92 static void pci_slot_release(struct kobject *kobj)
93 {
94 struct pci_dev *dev;
95 struct pci_slot *slot = to_pci_slot(kobj);
96
97 dev_dbg(&slot->bus->dev, "dev %02x, released physical slot %s\n",
98 slot->number, pci_slot_name(slot));
99
100 down_read(&pci_bus_sem);
101 list_for_each_entry(dev, &slot->bus->devices, bus_list)
102 if (pci_dev_matches_slot(dev, slot))
103 dev->slot = NULL;
104 up_read(&pci_bus_sem);
105
106 list_del(&slot->list);
107 pci_bus_put(slot->bus);
108
109 kfree(slot);
110 }
111
112 static struct pci_slot_attribute pci_slot_attr_address =
113 __ATTR(address, S_IRUGO, address_read_file, NULL);
114 static struct pci_slot_attribute pci_slot_attr_max_speed =
115 __ATTR(max_bus_speed, S_IRUGO, max_speed_read_file, NULL);
116 static struct pci_slot_attribute pci_slot_attr_cur_speed =
117 __ATTR(cur_bus_speed, S_IRUGO, cur_speed_read_file, NULL);
118
119 static struct attribute *pci_slot_default_attrs[] = {
120 &pci_slot_attr_address.attr,
121 &pci_slot_attr_max_speed.attr,
122 &pci_slot_attr_cur_speed.attr,
123 NULL,
124 };
125
126 static const struct attribute_group pci_slot_default_group = {
127 .attrs = pci_slot_default_attrs,
128 };
129
130 static const struct attribute_group *pci_slot_default_groups[] = {
131 &pci_slot_default_group,
132 #ifdef ARCH_PCI_SLOT_GROUPS
133 ARCH_PCI_SLOT_GROUPS,
134 #endif
135 NULL,
136 };
137
138 static const struct kobj_type pci_slot_ktype = {
139 .sysfs_ops = &pci_slot_sysfs_ops,
140 .release = &pci_slot_release,
141 .default_groups = pci_slot_default_groups,
142 };
143
make_slot_name(const char * name)144 static char *make_slot_name(const char *name)
145 {
146 char *new_name;
147 int len, max, dup;
148
149 new_name = kstrdup(name, GFP_KERNEL);
150 if (!new_name)
151 return NULL;
152
153 /*
154 * Make sure we hit the realloc case the first time through the
155 * loop. 'len' will be strlen(name) + 3 at that point which is
156 * enough space for "name-X" and the trailing NUL.
157 */
158 len = strlen(name) + 2;
159 max = 1;
160 dup = 1;
161
162 for (;;) {
163 struct kobject *dup_slot;
164 dup_slot = kset_find_obj(pci_slots_kset, new_name);
165 if (!dup_slot)
166 break;
167 kobject_put(dup_slot);
168 if (dup == max) {
169 len++;
170 max *= 10;
171 kfree(new_name);
172 new_name = kmalloc(len, GFP_KERNEL);
173 if (!new_name)
174 break;
175 }
176 sprintf(new_name, "%s-%d", name, dup++);
177 }
178
179 return new_name;
180 }
181
rename_slot(struct pci_slot * slot,const char * name)182 static int rename_slot(struct pci_slot *slot, const char *name)
183 {
184 int result = 0;
185 char *slot_name;
186
187 if (strcmp(pci_slot_name(slot), name) == 0)
188 return result;
189
190 slot_name = make_slot_name(name);
191 if (!slot_name)
192 return -ENOMEM;
193
194 result = kobject_rename(&slot->kobj, slot_name);
195 kfree(slot_name);
196
197 return result;
198 }
199
pci_dev_assign_slot(struct pci_dev * dev)200 void pci_dev_assign_slot(struct pci_dev *dev)
201 {
202 struct pci_slot *slot;
203
204 mutex_lock(&pci_slot_mutex);
205 list_for_each_entry(slot, &dev->bus->slots, list)
206 if (pci_dev_matches_slot(dev, slot))
207 dev->slot = slot;
208 mutex_unlock(&pci_slot_mutex);
209 }
210
get_slot(struct pci_bus * parent,int slot_nr)211 static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
212 {
213 struct pci_slot *slot;
214
215 /* We already hold pci_slot_mutex */
216 list_for_each_entry(slot, &parent->slots, list)
217 if (slot->number == slot_nr) {
218 kobject_get(&slot->kobj);
219 return slot;
220 }
221
222 return NULL;
223 }
224
225 /**
226 * pci_create_slot - create or increment refcount for physical PCI slot
227 * @parent: struct pci_bus of parent bridge
228 * @slot_nr: PCI_SLOT(pci_dev->devfn), PCI_SLOT_PLACEHOLDER for placeholder, or
229 * PCI_SLOT_ALL_DEVICES
230 * @name: user visible string presented in /sys/bus/pci/slots/<name>
231 * @hotplug: set if caller is hotplug driver, NULL otherwise
232 *
233 * PCI slots have first class attributes such as address, speed, width,
234 * and a &struct pci_slot is used to manage them. This interface will
235 * either return a new &struct pci_slot to the caller, or if the pci_slot
236 * already exists, its refcount will be incremented.
237 *
238 * Slots are uniquely identified by a @pci_bus, @slot_nr tuple.
239 *
240 * There are known platforms with broken firmware that assign the same
241 * name to multiple slots. Workaround these broken platforms by renaming
242 * the slots on behalf of the caller. If firmware assigns name N to
243 * multiple slots:
244 *
245 * The first slot is assigned N
246 * The second slot is assigned N-1
247 * The third slot is assigned N-2
248 * etc.
249 *
250 * Placeholder slots:
251 * In most cases, @pci_bus, @slot_nr will be sufficient to uniquely identify
252 * a slot. There is one notable exception - pSeries (rpaphp), where the
253 * @slot_nr cannot be determined until a device is actually inserted into
254 * the slot. In this scenario, the caller may pass PCI_SLOT_PLACEHOLDER for @slot_nr.
255 *
256 * The following semantics are imposed when the caller passes @slot_nr ==
257 * PCI_SLOT_PLACEHOLDER. First, we no longer check for an existing %struct
258 * pci_slot, as there may be many slots with @slot_nr of
259 * PCI_SLOT_PLACEHOLDER. The other change in semantics is user-visible,
260 * which is the 'address' parameter presented in sysfs will consist solely
261 * of a dddd:bb tuple, where dddd is the PCI domain of the %struct pci_bus
262 * and bb is the bus number. In other words, the devfn of the 'placeholder'
263 * slot will not be displayed.
264 *
265 * Bus-wide slots:
266 * For PCIe hotplug, the physical slot encompasses the entire secondary
267 * bus, not just a single device number. If the device supports ARI and ARI
268 * Forwarding is enabled in the upstream bridge, a multi-function device
269 * may include functions that appear to have several different device
270 * numbers, i.e., PCI_SLOT() values. Pass @slot_nr == PCI_SLOT_ALL_DEVICES
271 * to create a slot that matches all devices on the bus. Unlike placeholder
272 * slots, bus-wide slots go through normal slot lookup and reuse existing
273 * slots if present.
274 */
pci_create_slot(struct pci_bus * parent,int slot_nr,const char * name,struct hotplug_slot * hotplug)275 struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
276 const char *name,
277 struct hotplug_slot *hotplug)
278 {
279 struct pci_dev *dev;
280 struct pci_slot *slot;
281 int err = 0;
282 char *slot_name = NULL;
283
284 mutex_lock(&pci_slot_mutex);
285
286 if (slot_nr == PCI_SLOT_PLACEHOLDER)
287 goto placeholder;
288
289 /*
290 * Hotplug drivers are allowed to rename an existing slot,
291 * but only if not already claimed.
292 */
293 slot = get_slot(parent, slot_nr);
294 if (slot) {
295 if (hotplug) {
296 if (slot->hotplug) {
297 err = -EBUSY;
298 goto put_slot;
299 }
300 err = rename_slot(slot, name);
301 if (err)
302 goto put_slot;
303 }
304 goto out;
305 }
306
307 placeholder:
308 slot = kzalloc_obj(*slot);
309 if (!slot) {
310 err = -ENOMEM;
311 goto err;
312 }
313
314 slot->bus = pci_bus_get(parent);
315 slot->number = slot_nr;
316
317 if (pci_slot_enabled_per_func())
318 slot->per_func_slot = 1;
319
320 slot->kobj.kset = pci_slots_kset;
321
322 slot_name = make_slot_name(name);
323 if (!slot_name) {
324 err = -ENOMEM;
325 pci_bus_put(slot->bus);
326 kfree(slot);
327 goto err;
328 }
329
330 INIT_LIST_HEAD(&slot->list);
331 list_add(&slot->list, &parent->slots);
332
333 err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
334 "%s", slot_name);
335 if (err)
336 goto put_slot;
337
338 down_read(&pci_bus_sem);
339 list_for_each_entry(dev, &parent->devices, bus_list)
340 if (pci_dev_matches_slot(dev, slot))
341 dev->slot = slot;
342 up_read(&pci_bus_sem);
343
344 dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
345 slot_nr, pci_slot_name(slot));
346
347 out:
348 kfree(slot_name);
349 mutex_unlock(&pci_slot_mutex);
350 return slot;
351
352 put_slot:
353 kobject_put(&slot->kobj);
354 err:
355 slot = ERR_PTR(err);
356 goto out;
357 }
358 EXPORT_SYMBOL_GPL(pci_create_slot);
359
360 /**
361 * pci_destroy_slot - decrement refcount for physical PCI slot
362 * @slot: struct pci_slot to decrement
363 *
364 * %struct pci_slot is refcounted, so destroying them is really easy; we
365 * just call kobject_put on its kobj and let our release methods do the
366 * rest.
367 */
pci_destroy_slot(struct pci_slot * slot)368 void pci_destroy_slot(struct pci_slot *slot)
369 {
370 dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
371 slot->number, kref_read(&slot->kobj.kref) - 1);
372
373 mutex_lock(&pci_slot_mutex);
374 kobject_put(&slot->kobj);
375 mutex_unlock(&pci_slot_mutex);
376 }
377 EXPORT_SYMBOL_GPL(pci_destroy_slot);
378
pci_slot_init(void)379 static int pci_slot_init(void)
380 {
381 struct kset *pci_bus_kset;
382
383 pci_bus_kset = bus_get_kset(&pci_bus_type);
384 pci_slots_kset = kset_create_and_add("slots", NULL,
385 &pci_bus_kset->kobj);
386 if (!pci_slots_kset) {
387 pr_err("PCI: Slot initialization failure\n");
388 return -ENOMEM;
389 }
390 return 0;
391 }
392
393 subsys_initcall(pci_slot_init);
394