xref: /openbmc/linux/drivers/pci/pci-label.c (revision 763e9db9)
1911e1c9bSNarendra K /*
2911e1c9bSNarendra K  * Purpose: Export the firmware instance and label associated with
3911e1c9bSNarendra K  * a pci device to sysfs
4911e1c9bSNarendra K  * Copyright (C) 2010 Dell Inc.
5911e1c9bSNarendra K  * by Narendra K <Narendra_K@dell.com>,
6911e1c9bSNarendra K  * Jordan Hargrave <Jordan_Hargrave@dell.com>
7911e1c9bSNarendra K  *
8911e1c9bSNarendra K  * SMBIOS defines type 41 for onboard pci devices. This code retrieves
9911e1c9bSNarendra K  * the instance number and string from the type 41 record and exports
10911e1c9bSNarendra K  * it to sysfs.
11911e1c9bSNarendra K  *
12911e1c9bSNarendra K  * Please see http://linux.dell.com/wiki/index.php/Oss/libnetdevname for more
13911e1c9bSNarendra K  * information.
14911e1c9bSNarendra K  */
15911e1c9bSNarendra K 
16911e1c9bSNarendra K #include <linux/dmi.h>
17911e1c9bSNarendra K #include <linux/sysfs.h>
18911e1c9bSNarendra K #include <linux/pci.h>
19911e1c9bSNarendra K #include <linux/pci_ids.h>
20911e1c9bSNarendra K #include <linux/module.h>
21911e1c9bSNarendra K #include <linux/device.h>
22911e1c9bSNarendra K #include "pci.h"
23911e1c9bSNarendra K 
24911e1c9bSNarendra K enum smbios_attr_enum {
25911e1c9bSNarendra K 	SMBIOS_ATTR_NONE = 0,
26911e1c9bSNarendra K 	SMBIOS_ATTR_LABEL_SHOW,
27911e1c9bSNarendra K 	SMBIOS_ATTR_INSTANCE_SHOW,
28911e1c9bSNarendra K };
29911e1c9bSNarendra K 
30911e1c9bSNarendra K static mode_t
31911e1c9bSNarendra K find_smbios_instance_string(struct pci_dev *pdev, char *buf,
32911e1c9bSNarendra K 			    enum smbios_attr_enum attribute)
33911e1c9bSNarendra K {
34911e1c9bSNarendra K 	const struct dmi_device *dmi;
35911e1c9bSNarendra K 	struct dmi_dev_onboard *donboard;
36911e1c9bSNarendra K 	int bus;
37911e1c9bSNarendra K 	int devfn;
38911e1c9bSNarendra K 
39911e1c9bSNarendra K 	bus = pdev->bus->number;
40911e1c9bSNarendra K 	devfn = pdev->devfn;
41911e1c9bSNarendra K 
42911e1c9bSNarendra K 	dmi = NULL;
43911e1c9bSNarendra K 	while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD,
44911e1c9bSNarendra K 				      NULL, dmi)) != NULL) {
45911e1c9bSNarendra K 		donboard = dmi->device_data;
46911e1c9bSNarendra K 		if (donboard && donboard->bus == bus &&
47911e1c9bSNarendra K 					donboard->devfn == devfn) {
48911e1c9bSNarendra K 			if (buf) {
49911e1c9bSNarendra K 				if (attribute == SMBIOS_ATTR_INSTANCE_SHOW)
50911e1c9bSNarendra K 					return scnprintf(buf, PAGE_SIZE,
51911e1c9bSNarendra K 							 "%d\n",
52911e1c9bSNarendra K 							 donboard->instance);
53911e1c9bSNarendra K 				else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
54911e1c9bSNarendra K 					return scnprintf(buf, PAGE_SIZE,
55911e1c9bSNarendra K 							 "%s\n",
56911e1c9bSNarendra K 							 dmi->name);
57911e1c9bSNarendra K 			}
58911e1c9bSNarendra K 			return strlen(dmi->name);
59911e1c9bSNarendra K 		}
60911e1c9bSNarendra K 	}
61911e1c9bSNarendra K 	return 0;
62911e1c9bSNarendra K }
63911e1c9bSNarendra K 
64911e1c9bSNarendra K static mode_t
65911e1c9bSNarendra K smbios_instance_string_exist(struct kobject *kobj, struct attribute *attr,
66911e1c9bSNarendra K 			     int n)
67911e1c9bSNarendra K {
68911e1c9bSNarendra K 	struct device *dev;
69911e1c9bSNarendra K 	struct pci_dev *pdev;
70911e1c9bSNarendra K 
71911e1c9bSNarendra K 	dev = container_of(kobj, struct device, kobj);
72911e1c9bSNarendra K 	pdev = to_pci_dev(dev);
73911e1c9bSNarendra K 
74911e1c9bSNarendra K 	return find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE) ?
75911e1c9bSNarendra K 					   S_IRUGO : 0;
76911e1c9bSNarendra K }
77911e1c9bSNarendra K 
78911e1c9bSNarendra K static ssize_t
79911e1c9bSNarendra K smbioslabel_show(struct device *dev, struct device_attribute *attr, char *buf)
80911e1c9bSNarendra K {
81911e1c9bSNarendra K 	struct pci_dev *pdev;
82911e1c9bSNarendra K 	pdev = to_pci_dev(dev);
83911e1c9bSNarendra K 
84911e1c9bSNarendra K 	return find_smbios_instance_string(pdev, buf,
85911e1c9bSNarendra K 					   SMBIOS_ATTR_LABEL_SHOW);
86911e1c9bSNarendra K }
87911e1c9bSNarendra K 
88911e1c9bSNarendra K static ssize_t
89911e1c9bSNarendra K smbiosinstance_show(struct device *dev,
90911e1c9bSNarendra K 		    struct device_attribute *attr, char *buf)
91911e1c9bSNarendra K {
92911e1c9bSNarendra K 	struct pci_dev *pdev;
93911e1c9bSNarendra K 	pdev = to_pci_dev(dev);
94911e1c9bSNarendra K 
95911e1c9bSNarendra K 	return find_smbios_instance_string(pdev, buf,
96911e1c9bSNarendra K 					   SMBIOS_ATTR_INSTANCE_SHOW);
97911e1c9bSNarendra K }
98911e1c9bSNarendra K 
99911e1c9bSNarendra K static struct device_attribute smbios_attr_label = {
100763e9db9SStephen Rothwell 	.attr = {.name = "label", .mode = 0444},
101911e1c9bSNarendra K 	.show = smbioslabel_show,
102911e1c9bSNarendra K };
103911e1c9bSNarendra K 
104911e1c9bSNarendra K static struct device_attribute smbios_attr_instance = {
105763e9db9SStephen Rothwell 	.attr = {.name = "index", .mode = 0444},
106911e1c9bSNarendra K 	.show = smbiosinstance_show,
107911e1c9bSNarendra K };
108911e1c9bSNarendra K 
109911e1c9bSNarendra K static struct attribute *smbios_attributes[] = {
110911e1c9bSNarendra K 	&smbios_attr_label.attr,
111911e1c9bSNarendra K 	&smbios_attr_instance.attr,
112911e1c9bSNarendra K 	NULL,
113911e1c9bSNarendra K };
114911e1c9bSNarendra K 
115911e1c9bSNarendra K static struct attribute_group smbios_attr_group = {
116911e1c9bSNarendra K 	.attrs = smbios_attributes,
117911e1c9bSNarendra K 	.is_visible = smbios_instance_string_exist,
118911e1c9bSNarendra K };
119911e1c9bSNarendra K 
120911e1c9bSNarendra K static int
121911e1c9bSNarendra K pci_create_smbiosname_file(struct pci_dev *pdev)
122911e1c9bSNarendra K {
123911e1c9bSNarendra K 	if (!sysfs_create_group(&pdev->dev.kobj, &smbios_attr_group))
124911e1c9bSNarendra K 		return 0;
125911e1c9bSNarendra K 	return -ENODEV;
126911e1c9bSNarendra K }
127911e1c9bSNarendra K 
128911e1c9bSNarendra K static void
129911e1c9bSNarendra K pci_remove_smbiosname_file(struct pci_dev *pdev)
130911e1c9bSNarendra K {
131911e1c9bSNarendra K 	sysfs_remove_group(&pdev->dev.kobj, &smbios_attr_group);
132911e1c9bSNarendra K }
133911e1c9bSNarendra K 
134911e1c9bSNarendra K void pci_create_firmware_label_files(struct pci_dev *pdev)
135911e1c9bSNarendra K {
136911e1c9bSNarendra K 	if (!pci_create_smbiosname_file(pdev))
137911e1c9bSNarendra K 		;
138911e1c9bSNarendra K }
139911e1c9bSNarendra K 
140911e1c9bSNarendra K void pci_remove_firmware_label_files(struct pci_dev *pdev)
141911e1c9bSNarendra K {
142911e1c9bSNarendra K 	pci_remove_smbiosname_file(pdev);
143911e1c9bSNarendra K }
144