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