Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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