Commit 0f12a4e29368a9476076515881d9ef4e5876c6e2

Authored by Ben Hutchings
Committed by Jesse Barnes
1 parent 100b33c8bd

PCI: sysfs: Fix failure path for addition of "vpd" attribute

Commit 280c73d ("PCI: centralize the capabilities code in
pci-sysfs.c") changed the initialisation of the "rom" and "vpd"
attributes, and made the failure path for the "vpd" attribute
incorrect.  We must free the new attribute structure (attr), but
instead we currently free dev->vpd->attr.  That will normally be NULL,
resulting in a memory leak, but it might be a stale pointer, resulting
in a double-free.

Found by inspection; compile-tested only.

Cc: stable@kernel.org
Signed-off-by: Ben Hutchings <bhutchings@solarflare.com>
Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

drivers/pci/pci-sysfs.c
1 /* 1 /*
2 * drivers/pci/pci-sysfs.c 2 * drivers/pci/pci-sysfs.c
3 * 3 *
4 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com> 4 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2002-2004 IBM Corp. 5 * (C) Copyright 2002-2004 IBM Corp.
6 * (C) Copyright 2003 Matthew Wilcox 6 * (C) Copyright 2003 Matthew Wilcox
7 * (C) Copyright 2003 Hewlett-Packard 7 * (C) Copyright 2003 Hewlett-Packard
8 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com> 8 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
9 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com> 9 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
10 * 10 *
11 * File attributes for PCI devices 11 * File attributes for PCI devices
12 * 12 *
13 * Modeled after usb's driverfs.c 13 * Modeled after usb's driverfs.c
14 * 14 *
15 */ 15 */
16 16
17 17
18 #include <linux/kernel.h> 18 #include <linux/kernel.h>
19 #include <linux/sched.h> 19 #include <linux/sched.h>
20 #include <linux/pci.h> 20 #include <linux/pci.h>
21 #include <linux/stat.h> 21 #include <linux/stat.h>
22 #include <linux/topology.h> 22 #include <linux/topology.h>
23 #include <linux/mm.h> 23 #include <linux/mm.h>
24 #include <linux/fs.h> 24 #include <linux/fs.h>
25 #include <linux/capability.h> 25 #include <linux/capability.h>
26 #include <linux/pci-aspm.h> 26 #include <linux/pci-aspm.h>
27 #include <linux/slab.h> 27 #include <linux/slab.h>
28 #include "pci.h" 28 #include "pci.h"
29 29
30 static int sysfs_initialized; /* = 0 */ 30 static int sysfs_initialized; /* = 0 */
31 31
32 /* show configuration fields */ 32 /* show configuration fields */
33 #define pci_config_attr(field, format_string) \ 33 #define pci_config_attr(field, format_string) \
34 static ssize_t \ 34 static ssize_t \
35 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \ 35 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
36 { \ 36 { \
37 struct pci_dev *pdev; \ 37 struct pci_dev *pdev; \
38 \ 38 \
39 pdev = to_pci_dev (dev); \ 39 pdev = to_pci_dev (dev); \
40 return sprintf (buf, format_string, pdev->field); \ 40 return sprintf (buf, format_string, pdev->field); \
41 } 41 }
42 42
43 pci_config_attr(vendor, "0x%04x\n"); 43 pci_config_attr(vendor, "0x%04x\n");
44 pci_config_attr(device, "0x%04x\n"); 44 pci_config_attr(device, "0x%04x\n");
45 pci_config_attr(subsystem_vendor, "0x%04x\n"); 45 pci_config_attr(subsystem_vendor, "0x%04x\n");
46 pci_config_attr(subsystem_device, "0x%04x\n"); 46 pci_config_attr(subsystem_device, "0x%04x\n");
47 pci_config_attr(class, "0x%06x\n"); 47 pci_config_attr(class, "0x%06x\n");
48 pci_config_attr(irq, "%u\n"); 48 pci_config_attr(irq, "%u\n");
49 49
50 static ssize_t broken_parity_status_show(struct device *dev, 50 static ssize_t broken_parity_status_show(struct device *dev,
51 struct device_attribute *attr, 51 struct device_attribute *attr,
52 char *buf) 52 char *buf)
53 { 53 {
54 struct pci_dev *pdev = to_pci_dev(dev); 54 struct pci_dev *pdev = to_pci_dev(dev);
55 return sprintf (buf, "%u\n", pdev->broken_parity_status); 55 return sprintf (buf, "%u\n", pdev->broken_parity_status);
56 } 56 }
57 57
58 static ssize_t broken_parity_status_store(struct device *dev, 58 static ssize_t broken_parity_status_store(struct device *dev,
59 struct device_attribute *attr, 59 struct device_attribute *attr,
60 const char *buf, size_t count) 60 const char *buf, size_t count)
61 { 61 {
62 struct pci_dev *pdev = to_pci_dev(dev); 62 struct pci_dev *pdev = to_pci_dev(dev);
63 unsigned long val; 63 unsigned long val;
64 64
65 if (strict_strtoul(buf, 0, &val) < 0) 65 if (strict_strtoul(buf, 0, &val) < 0)
66 return -EINVAL; 66 return -EINVAL;
67 67
68 pdev->broken_parity_status = !!val; 68 pdev->broken_parity_status = !!val;
69 69
70 return count; 70 return count;
71 } 71 }
72 72
73 static ssize_t local_cpus_show(struct device *dev, 73 static ssize_t local_cpus_show(struct device *dev,
74 struct device_attribute *attr, char *buf) 74 struct device_attribute *attr, char *buf)
75 { 75 {
76 const struct cpumask *mask; 76 const struct cpumask *mask;
77 int len; 77 int len;
78 78
79 #ifdef CONFIG_NUMA 79 #ifdef CONFIG_NUMA
80 mask = (dev_to_node(dev) == -1) ? cpu_online_mask : 80 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
81 cpumask_of_node(dev_to_node(dev)); 81 cpumask_of_node(dev_to_node(dev));
82 #else 82 #else
83 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus); 83 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
84 #endif 84 #endif
85 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask); 85 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
86 buf[len++] = '\n'; 86 buf[len++] = '\n';
87 buf[len] = '\0'; 87 buf[len] = '\0';
88 return len; 88 return len;
89 } 89 }
90 90
91 91
92 static ssize_t local_cpulist_show(struct device *dev, 92 static ssize_t local_cpulist_show(struct device *dev,
93 struct device_attribute *attr, char *buf) 93 struct device_attribute *attr, char *buf)
94 { 94 {
95 const struct cpumask *mask; 95 const struct cpumask *mask;
96 int len; 96 int len;
97 97
98 #ifdef CONFIG_NUMA 98 #ifdef CONFIG_NUMA
99 mask = (dev_to_node(dev) == -1) ? cpu_online_mask : 99 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
100 cpumask_of_node(dev_to_node(dev)); 100 cpumask_of_node(dev_to_node(dev));
101 #else 101 #else
102 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus); 102 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
103 #endif 103 #endif
104 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask); 104 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
105 buf[len++] = '\n'; 105 buf[len++] = '\n';
106 buf[len] = '\0'; 106 buf[len] = '\0';
107 return len; 107 return len;
108 } 108 }
109 109
110 /* show resources */ 110 /* show resources */
111 static ssize_t 111 static ssize_t
112 resource_show(struct device * dev, struct device_attribute *attr, char * buf) 112 resource_show(struct device * dev, struct device_attribute *attr, char * buf)
113 { 113 {
114 struct pci_dev * pci_dev = to_pci_dev(dev); 114 struct pci_dev * pci_dev = to_pci_dev(dev);
115 char * str = buf; 115 char * str = buf;
116 int i; 116 int i;
117 int max; 117 int max;
118 resource_size_t start, end; 118 resource_size_t start, end;
119 119
120 if (pci_dev->subordinate) 120 if (pci_dev->subordinate)
121 max = DEVICE_COUNT_RESOURCE; 121 max = DEVICE_COUNT_RESOURCE;
122 else 122 else
123 max = PCI_BRIDGE_RESOURCES; 123 max = PCI_BRIDGE_RESOURCES;
124 124
125 for (i = 0; i < max; i++) { 125 for (i = 0; i < max; i++) {
126 struct resource *res = &pci_dev->resource[i]; 126 struct resource *res = &pci_dev->resource[i];
127 pci_resource_to_user(pci_dev, i, res, &start, &end); 127 pci_resource_to_user(pci_dev, i, res, &start, &end);
128 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n", 128 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
129 (unsigned long long)start, 129 (unsigned long long)start,
130 (unsigned long long)end, 130 (unsigned long long)end,
131 (unsigned long long)res->flags); 131 (unsigned long long)res->flags);
132 } 132 }
133 return (str - buf); 133 return (str - buf);
134 } 134 }
135 135
136 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf) 136 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
137 { 137 {
138 struct pci_dev *pci_dev = to_pci_dev(dev); 138 struct pci_dev *pci_dev = to_pci_dev(dev);
139 139
140 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n", 140 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
141 pci_dev->vendor, pci_dev->device, 141 pci_dev->vendor, pci_dev->device,
142 pci_dev->subsystem_vendor, pci_dev->subsystem_device, 142 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
143 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8), 143 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
144 (u8)(pci_dev->class)); 144 (u8)(pci_dev->class));
145 } 145 }
146 146
147 static ssize_t is_enabled_store(struct device *dev, 147 static ssize_t is_enabled_store(struct device *dev,
148 struct device_attribute *attr, const char *buf, 148 struct device_attribute *attr, const char *buf,
149 size_t count) 149 size_t count)
150 { 150 {
151 struct pci_dev *pdev = to_pci_dev(dev); 151 struct pci_dev *pdev = to_pci_dev(dev);
152 unsigned long val; 152 unsigned long val;
153 ssize_t result = strict_strtoul(buf, 0, &val); 153 ssize_t result = strict_strtoul(buf, 0, &val);
154 154
155 if (result < 0) 155 if (result < 0)
156 return result; 156 return result;
157 157
158 /* this can crash the machine when done on the "wrong" device */ 158 /* this can crash the machine when done on the "wrong" device */
159 if (!capable(CAP_SYS_ADMIN)) 159 if (!capable(CAP_SYS_ADMIN))
160 return -EPERM; 160 return -EPERM;
161 161
162 if (!val) { 162 if (!val) {
163 if (pci_is_enabled(pdev)) 163 if (pci_is_enabled(pdev))
164 pci_disable_device(pdev); 164 pci_disable_device(pdev);
165 else 165 else
166 result = -EIO; 166 result = -EIO;
167 } else 167 } else
168 result = pci_enable_device(pdev); 168 result = pci_enable_device(pdev);
169 169
170 return result < 0 ? result : count; 170 return result < 0 ? result : count;
171 } 171 }
172 172
173 static ssize_t is_enabled_show(struct device *dev, 173 static ssize_t is_enabled_show(struct device *dev,
174 struct device_attribute *attr, char *buf) 174 struct device_attribute *attr, char *buf)
175 { 175 {
176 struct pci_dev *pdev; 176 struct pci_dev *pdev;
177 177
178 pdev = to_pci_dev (dev); 178 pdev = to_pci_dev (dev);
179 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt)); 179 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
180 } 180 }
181 181
182 #ifdef CONFIG_NUMA 182 #ifdef CONFIG_NUMA
183 static ssize_t 183 static ssize_t
184 numa_node_show(struct device *dev, struct device_attribute *attr, char *buf) 184 numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
185 { 185 {
186 return sprintf (buf, "%d\n", dev->numa_node); 186 return sprintf (buf, "%d\n", dev->numa_node);
187 } 187 }
188 #endif 188 #endif
189 189
190 static ssize_t 190 static ssize_t
191 dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf) 191 dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
192 { 192 {
193 struct pci_dev *pdev = to_pci_dev(dev); 193 struct pci_dev *pdev = to_pci_dev(dev);
194 194
195 return sprintf (buf, "%d\n", fls64(pdev->dma_mask)); 195 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
196 } 196 }
197 197
198 static ssize_t 198 static ssize_t
199 consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr, 199 consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
200 char *buf) 200 char *buf)
201 { 201 {
202 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask)); 202 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
203 } 203 }
204 204
205 static ssize_t 205 static ssize_t
206 msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf) 206 msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
207 { 207 {
208 struct pci_dev *pdev = to_pci_dev(dev); 208 struct pci_dev *pdev = to_pci_dev(dev);
209 209
210 if (!pdev->subordinate) 210 if (!pdev->subordinate)
211 return 0; 211 return 0;
212 212
213 return sprintf (buf, "%u\n", 213 return sprintf (buf, "%u\n",
214 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)); 214 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
215 } 215 }
216 216
217 static ssize_t 217 static ssize_t
218 msi_bus_store(struct device *dev, struct device_attribute *attr, 218 msi_bus_store(struct device *dev, struct device_attribute *attr,
219 const char *buf, size_t count) 219 const char *buf, size_t count)
220 { 220 {
221 struct pci_dev *pdev = to_pci_dev(dev); 221 struct pci_dev *pdev = to_pci_dev(dev);
222 unsigned long val; 222 unsigned long val;
223 223
224 if (strict_strtoul(buf, 0, &val) < 0) 224 if (strict_strtoul(buf, 0, &val) < 0)
225 return -EINVAL; 225 return -EINVAL;
226 226
227 /* bad things may happen if the no_msi flag is changed 227 /* bad things may happen if the no_msi flag is changed
228 * while some drivers are loaded */ 228 * while some drivers are loaded */
229 if (!capable(CAP_SYS_ADMIN)) 229 if (!capable(CAP_SYS_ADMIN))
230 return -EPERM; 230 return -EPERM;
231 231
232 /* Maybe pci devices without subordinate busses shouldn't even have this 232 /* Maybe pci devices without subordinate busses shouldn't even have this
233 * attribute in the first place? */ 233 * attribute in the first place? */
234 if (!pdev->subordinate) 234 if (!pdev->subordinate)
235 return count; 235 return count;
236 236
237 /* Is the flag going to change, or keep the value it already had? */ 237 /* Is the flag going to change, or keep the value it already had? */
238 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^ 238 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
239 !!val) { 239 !!val) {
240 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI; 240 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
241 241
242 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI," 242 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
243 " bad things could happen\n", val ? "" : " not"); 243 " bad things could happen\n", val ? "" : " not");
244 } 244 }
245 245
246 return count; 246 return count;
247 } 247 }
248 248
249 #ifdef CONFIG_HOTPLUG 249 #ifdef CONFIG_HOTPLUG
250 static DEFINE_MUTEX(pci_remove_rescan_mutex); 250 static DEFINE_MUTEX(pci_remove_rescan_mutex);
251 static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf, 251 static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
252 size_t count) 252 size_t count)
253 { 253 {
254 unsigned long val; 254 unsigned long val;
255 struct pci_bus *b = NULL; 255 struct pci_bus *b = NULL;
256 256
257 if (strict_strtoul(buf, 0, &val) < 0) 257 if (strict_strtoul(buf, 0, &val) < 0)
258 return -EINVAL; 258 return -EINVAL;
259 259
260 if (val) { 260 if (val) {
261 mutex_lock(&pci_remove_rescan_mutex); 261 mutex_lock(&pci_remove_rescan_mutex);
262 while ((b = pci_find_next_bus(b)) != NULL) 262 while ((b = pci_find_next_bus(b)) != NULL)
263 pci_rescan_bus(b); 263 pci_rescan_bus(b);
264 mutex_unlock(&pci_remove_rescan_mutex); 264 mutex_unlock(&pci_remove_rescan_mutex);
265 } 265 }
266 return count; 266 return count;
267 } 267 }
268 268
269 struct bus_attribute pci_bus_attrs[] = { 269 struct bus_attribute pci_bus_attrs[] = {
270 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store), 270 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
271 __ATTR_NULL 271 __ATTR_NULL
272 }; 272 };
273 273
274 static ssize_t 274 static ssize_t
275 dev_rescan_store(struct device *dev, struct device_attribute *attr, 275 dev_rescan_store(struct device *dev, struct device_attribute *attr,
276 const char *buf, size_t count) 276 const char *buf, size_t count)
277 { 277 {
278 unsigned long val; 278 unsigned long val;
279 struct pci_dev *pdev = to_pci_dev(dev); 279 struct pci_dev *pdev = to_pci_dev(dev);
280 280
281 if (strict_strtoul(buf, 0, &val) < 0) 281 if (strict_strtoul(buf, 0, &val) < 0)
282 return -EINVAL; 282 return -EINVAL;
283 283
284 if (val) { 284 if (val) {
285 mutex_lock(&pci_remove_rescan_mutex); 285 mutex_lock(&pci_remove_rescan_mutex);
286 pci_rescan_bus(pdev->bus); 286 pci_rescan_bus(pdev->bus);
287 mutex_unlock(&pci_remove_rescan_mutex); 287 mutex_unlock(&pci_remove_rescan_mutex);
288 } 288 }
289 return count; 289 return count;
290 } 290 }
291 291
292 static void remove_callback(struct device *dev) 292 static void remove_callback(struct device *dev)
293 { 293 {
294 struct pci_dev *pdev = to_pci_dev(dev); 294 struct pci_dev *pdev = to_pci_dev(dev);
295 295
296 mutex_lock(&pci_remove_rescan_mutex); 296 mutex_lock(&pci_remove_rescan_mutex);
297 pci_remove_bus_device(pdev); 297 pci_remove_bus_device(pdev);
298 mutex_unlock(&pci_remove_rescan_mutex); 298 mutex_unlock(&pci_remove_rescan_mutex);
299 } 299 }
300 300
301 static ssize_t 301 static ssize_t
302 remove_store(struct device *dev, struct device_attribute *dummy, 302 remove_store(struct device *dev, struct device_attribute *dummy,
303 const char *buf, size_t count) 303 const char *buf, size_t count)
304 { 304 {
305 int ret = 0; 305 int ret = 0;
306 unsigned long val; 306 unsigned long val;
307 307
308 if (strict_strtoul(buf, 0, &val) < 0) 308 if (strict_strtoul(buf, 0, &val) < 0)
309 return -EINVAL; 309 return -EINVAL;
310 310
311 /* An attribute cannot be unregistered by one of its own methods, 311 /* An attribute cannot be unregistered by one of its own methods,
312 * so we have to use this roundabout approach. 312 * so we have to use this roundabout approach.
313 */ 313 */
314 if (val) 314 if (val)
315 ret = device_schedule_callback(dev, remove_callback); 315 ret = device_schedule_callback(dev, remove_callback);
316 if (ret) 316 if (ret)
317 count = ret; 317 count = ret;
318 return count; 318 return count;
319 } 319 }
320 #endif 320 #endif
321 321
322 struct device_attribute pci_dev_attrs[] = { 322 struct device_attribute pci_dev_attrs[] = {
323 __ATTR_RO(resource), 323 __ATTR_RO(resource),
324 __ATTR_RO(vendor), 324 __ATTR_RO(vendor),
325 __ATTR_RO(device), 325 __ATTR_RO(device),
326 __ATTR_RO(subsystem_vendor), 326 __ATTR_RO(subsystem_vendor),
327 __ATTR_RO(subsystem_device), 327 __ATTR_RO(subsystem_device),
328 __ATTR_RO(class), 328 __ATTR_RO(class),
329 __ATTR_RO(irq), 329 __ATTR_RO(irq),
330 __ATTR_RO(local_cpus), 330 __ATTR_RO(local_cpus),
331 __ATTR_RO(local_cpulist), 331 __ATTR_RO(local_cpulist),
332 __ATTR_RO(modalias), 332 __ATTR_RO(modalias),
333 #ifdef CONFIG_NUMA 333 #ifdef CONFIG_NUMA
334 __ATTR_RO(numa_node), 334 __ATTR_RO(numa_node),
335 #endif 335 #endif
336 __ATTR_RO(dma_mask_bits), 336 __ATTR_RO(dma_mask_bits),
337 __ATTR_RO(consistent_dma_mask_bits), 337 __ATTR_RO(consistent_dma_mask_bits),
338 __ATTR(enable, 0600, is_enabled_show, is_enabled_store), 338 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
339 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR), 339 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
340 broken_parity_status_show,broken_parity_status_store), 340 broken_parity_status_show,broken_parity_status_store),
341 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store), 341 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
342 #ifdef CONFIG_HOTPLUG 342 #ifdef CONFIG_HOTPLUG
343 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store), 343 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
344 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store), 344 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
345 #endif 345 #endif
346 __ATTR_NULL, 346 __ATTR_NULL,
347 }; 347 };
348 348
349 static ssize_t 349 static ssize_t
350 boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf) 350 boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
351 { 351 {
352 struct pci_dev *pdev = to_pci_dev(dev); 352 struct pci_dev *pdev = to_pci_dev(dev);
353 353
354 return sprintf(buf, "%u\n", 354 return sprintf(buf, "%u\n",
355 !!(pdev->resource[PCI_ROM_RESOURCE].flags & 355 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
356 IORESOURCE_ROM_SHADOW)); 356 IORESOURCE_ROM_SHADOW));
357 } 357 }
358 struct device_attribute vga_attr = __ATTR_RO(boot_vga); 358 struct device_attribute vga_attr = __ATTR_RO(boot_vga);
359 359
360 static ssize_t 360 static ssize_t
361 pci_read_config(struct file *filp, struct kobject *kobj, 361 pci_read_config(struct file *filp, struct kobject *kobj,
362 struct bin_attribute *bin_attr, 362 struct bin_attribute *bin_attr,
363 char *buf, loff_t off, size_t count) 363 char *buf, loff_t off, size_t count)
364 { 364 {
365 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj)); 365 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
366 unsigned int size = 64; 366 unsigned int size = 64;
367 loff_t init_off = off; 367 loff_t init_off = off;
368 u8 *data = (u8*) buf; 368 u8 *data = (u8*) buf;
369 369
370 /* Several chips lock up trying to read undefined config space */ 370 /* Several chips lock up trying to read undefined config space */
371 if (cap_raised(filp->f_cred->cap_effective, CAP_SYS_ADMIN)) { 371 if (cap_raised(filp->f_cred->cap_effective, CAP_SYS_ADMIN)) {
372 size = dev->cfg_size; 372 size = dev->cfg_size;
373 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { 373 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
374 size = 128; 374 size = 128;
375 } 375 }
376 376
377 if (off > size) 377 if (off > size)
378 return 0; 378 return 0;
379 if (off + count > size) { 379 if (off + count > size) {
380 size -= off; 380 size -= off;
381 count = size; 381 count = size;
382 } else { 382 } else {
383 size = count; 383 size = count;
384 } 384 }
385 385
386 if ((off & 1) && size) { 386 if ((off & 1) && size) {
387 u8 val; 387 u8 val;
388 pci_user_read_config_byte(dev, off, &val); 388 pci_user_read_config_byte(dev, off, &val);
389 data[off - init_off] = val; 389 data[off - init_off] = val;
390 off++; 390 off++;
391 size--; 391 size--;
392 } 392 }
393 393
394 if ((off & 3) && size > 2) { 394 if ((off & 3) && size > 2) {
395 u16 val; 395 u16 val;
396 pci_user_read_config_word(dev, off, &val); 396 pci_user_read_config_word(dev, off, &val);
397 data[off - init_off] = val & 0xff; 397 data[off - init_off] = val & 0xff;
398 data[off - init_off + 1] = (val >> 8) & 0xff; 398 data[off - init_off + 1] = (val >> 8) & 0xff;
399 off += 2; 399 off += 2;
400 size -= 2; 400 size -= 2;
401 } 401 }
402 402
403 while (size > 3) { 403 while (size > 3) {
404 u32 val; 404 u32 val;
405 pci_user_read_config_dword(dev, off, &val); 405 pci_user_read_config_dword(dev, off, &val);
406 data[off - init_off] = val & 0xff; 406 data[off - init_off] = val & 0xff;
407 data[off - init_off + 1] = (val >> 8) & 0xff; 407 data[off - init_off + 1] = (val >> 8) & 0xff;
408 data[off - init_off + 2] = (val >> 16) & 0xff; 408 data[off - init_off + 2] = (val >> 16) & 0xff;
409 data[off - init_off + 3] = (val >> 24) & 0xff; 409 data[off - init_off + 3] = (val >> 24) & 0xff;
410 off += 4; 410 off += 4;
411 size -= 4; 411 size -= 4;
412 } 412 }
413 413
414 if (size >= 2) { 414 if (size >= 2) {
415 u16 val; 415 u16 val;
416 pci_user_read_config_word(dev, off, &val); 416 pci_user_read_config_word(dev, off, &val);
417 data[off - init_off] = val & 0xff; 417 data[off - init_off] = val & 0xff;
418 data[off - init_off + 1] = (val >> 8) & 0xff; 418 data[off - init_off + 1] = (val >> 8) & 0xff;
419 off += 2; 419 off += 2;
420 size -= 2; 420 size -= 2;
421 } 421 }
422 422
423 if (size > 0) { 423 if (size > 0) {
424 u8 val; 424 u8 val;
425 pci_user_read_config_byte(dev, off, &val); 425 pci_user_read_config_byte(dev, off, &val);
426 data[off - init_off] = val; 426 data[off - init_off] = val;
427 off++; 427 off++;
428 --size; 428 --size;
429 } 429 }
430 430
431 return count; 431 return count;
432 } 432 }
433 433
434 static ssize_t 434 static ssize_t
435 pci_write_config(struct file* filp, struct kobject *kobj, 435 pci_write_config(struct file* filp, struct kobject *kobj,
436 struct bin_attribute *bin_attr, 436 struct bin_attribute *bin_attr,
437 char *buf, loff_t off, size_t count) 437 char *buf, loff_t off, size_t count)
438 { 438 {
439 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj)); 439 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
440 unsigned int size = count; 440 unsigned int size = count;
441 loff_t init_off = off; 441 loff_t init_off = off;
442 u8 *data = (u8*) buf; 442 u8 *data = (u8*) buf;
443 443
444 if (off > dev->cfg_size) 444 if (off > dev->cfg_size)
445 return 0; 445 return 0;
446 if (off + count > dev->cfg_size) { 446 if (off + count > dev->cfg_size) {
447 size = dev->cfg_size - off; 447 size = dev->cfg_size - off;
448 count = size; 448 count = size;
449 } 449 }
450 450
451 if ((off & 1) && size) { 451 if ((off & 1) && size) {
452 pci_user_write_config_byte(dev, off, data[off - init_off]); 452 pci_user_write_config_byte(dev, off, data[off - init_off]);
453 off++; 453 off++;
454 size--; 454 size--;
455 } 455 }
456 456
457 if ((off & 3) && size > 2) { 457 if ((off & 3) && size > 2) {
458 u16 val = data[off - init_off]; 458 u16 val = data[off - init_off];
459 val |= (u16) data[off - init_off + 1] << 8; 459 val |= (u16) data[off - init_off + 1] << 8;
460 pci_user_write_config_word(dev, off, val); 460 pci_user_write_config_word(dev, off, val);
461 off += 2; 461 off += 2;
462 size -= 2; 462 size -= 2;
463 } 463 }
464 464
465 while (size > 3) { 465 while (size > 3) {
466 u32 val = data[off - init_off]; 466 u32 val = data[off - init_off];
467 val |= (u32) data[off - init_off + 1] << 8; 467 val |= (u32) data[off - init_off + 1] << 8;
468 val |= (u32) data[off - init_off + 2] << 16; 468 val |= (u32) data[off - init_off + 2] << 16;
469 val |= (u32) data[off - init_off + 3] << 24; 469 val |= (u32) data[off - init_off + 3] << 24;
470 pci_user_write_config_dword(dev, off, val); 470 pci_user_write_config_dword(dev, off, val);
471 off += 4; 471 off += 4;
472 size -= 4; 472 size -= 4;
473 } 473 }
474 474
475 if (size >= 2) { 475 if (size >= 2) {
476 u16 val = data[off - init_off]; 476 u16 val = data[off - init_off];
477 val |= (u16) data[off - init_off + 1] << 8; 477 val |= (u16) data[off - init_off + 1] << 8;
478 pci_user_write_config_word(dev, off, val); 478 pci_user_write_config_word(dev, off, val);
479 off += 2; 479 off += 2;
480 size -= 2; 480 size -= 2;
481 } 481 }
482 482
483 if (size) { 483 if (size) {
484 pci_user_write_config_byte(dev, off, data[off - init_off]); 484 pci_user_write_config_byte(dev, off, data[off - init_off]);
485 off++; 485 off++;
486 --size; 486 --size;
487 } 487 }
488 488
489 return count; 489 return count;
490 } 490 }
491 491
492 static ssize_t 492 static ssize_t
493 read_vpd_attr(struct file *filp, struct kobject *kobj, 493 read_vpd_attr(struct file *filp, struct kobject *kobj,
494 struct bin_attribute *bin_attr, 494 struct bin_attribute *bin_attr,
495 char *buf, loff_t off, size_t count) 495 char *buf, loff_t off, size_t count)
496 { 496 {
497 struct pci_dev *dev = 497 struct pci_dev *dev =
498 to_pci_dev(container_of(kobj, struct device, kobj)); 498 to_pci_dev(container_of(kobj, struct device, kobj));
499 499
500 if (off > bin_attr->size) 500 if (off > bin_attr->size)
501 count = 0; 501 count = 0;
502 else if (count > bin_attr->size - off) 502 else if (count > bin_attr->size - off)
503 count = bin_attr->size - off; 503 count = bin_attr->size - off;
504 504
505 return pci_read_vpd(dev, off, count, buf); 505 return pci_read_vpd(dev, off, count, buf);
506 } 506 }
507 507
508 static ssize_t 508 static ssize_t
509 write_vpd_attr(struct file *filp, struct kobject *kobj, 509 write_vpd_attr(struct file *filp, struct kobject *kobj,
510 struct bin_attribute *bin_attr, 510 struct bin_attribute *bin_attr,
511 char *buf, loff_t off, size_t count) 511 char *buf, loff_t off, size_t count)
512 { 512 {
513 struct pci_dev *dev = 513 struct pci_dev *dev =
514 to_pci_dev(container_of(kobj, struct device, kobj)); 514 to_pci_dev(container_of(kobj, struct device, kobj));
515 515
516 if (off > bin_attr->size) 516 if (off > bin_attr->size)
517 count = 0; 517 count = 0;
518 else if (count > bin_attr->size - off) 518 else if (count > bin_attr->size - off)
519 count = bin_attr->size - off; 519 count = bin_attr->size - off;
520 520
521 return pci_write_vpd(dev, off, count, buf); 521 return pci_write_vpd(dev, off, count, buf);
522 } 522 }
523 523
524 #ifdef HAVE_PCI_LEGACY 524 #ifdef HAVE_PCI_LEGACY
525 /** 525 /**
526 * pci_read_legacy_io - read byte(s) from legacy I/O port space 526 * pci_read_legacy_io - read byte(s) from legacy I/O port space
527 * @filp: open sysfs file 527 * @filp: open sysfs file
528 * @kobj: kobject corresponding to file to read from 528 * @kobj: kobject corresponding to file to read from
529 * @bin_attr: struct bin_attribute for this file 529 * @bin_attr: struct bin_attribute for this file
530 * @buf: buffer to store results 530 * @buf: buffer to store results
531 * @off: offset into legacy I/O port space 531 * @off: offset into legacy I/O port space
532 * @count: number of bytes to read 532 * @count: number of bytes to read
533 * 533 *
534 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific 534 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
535 * callback routine (pci_legacy_read). 535 * callback routine (pci_legacy_read).
536 */ 536 */
537 static ssize_t 537 static ssize_t
538 pci_read_legacy_io(struct file *filp, struct kobject *kobj, 538 pci_read_legacy_io(struct file *filp, struct kobject *kobj,
539 struct bin_attribute *bin_attr, 539 struct bin_attribute *bin_attr,
540 char *buf, loff_t off, size_t count) 540 char *buf, loff_t off, size_t count)
541 { 541 {
542 struct pci_bus *bus = to_pci_bus(container_of(kobj, 542 struct pci_bus *bus = to_pci_bus(container_of(kobj,
543 struct device, 543 struct device,
544 kobj)); 544 kobj));
545 545
546 /* Only support 1, 2 or 4 byte accesses */ 546 /* Only support 1, 2 or 4 byte accesses */
547 if (count != 1 && count != 2 && count != 4) 547 if (count != 1 && count != 2 && count != 4)
548 return -EINVAL; 548 return -EINVAL;
549 549
550 return pci_legacy_read(bus, off, (u32 *)buf, count); 550 return pci_legacy_read(bus, off, (u32 *)buf, count);
551 } 551 }
552 552
553 /** 553 /**
554 * pci_write_legacy_io - write byte(s) to legacy I/O port space 554 * pci_write_legacy_io - write byte(s) to legacy I/O port space
555 * @filp: open sysfs file 555 * @filp: open sysfs file
556 * @kobj: kobject corresponding to file to read from 556 * @kobj: kobject corresponding to file to read from
557 * @bin_attr: struct bin_attribute for this file 557 * @bin_attr: struct bin_attribute for this file
558 * @buf: buffer containing value to be written 558 * @buf: buffer containing value to be written
559 * @off: offset into legacy I/O port space 559 * @off: offset into legacy I/O port space
560 * @count: number of bytes to write 560 * @count: number of bytes to write
561 * 561 *
562 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific 562 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
563 * callback routine (pci_legacy_write). 563 * callback routine (pci_legacy_write).
564 */ 564 */
565 static ssize_t 565 static ssize_t
566 pci_write_legacy_io(struct file *filp, struct kobject *kobj, 566 pci_write_legacy_io(struct file *filp, struct kobject *kobj,
567 struct bin_attribute *bin_attr, 567 struct bin_attribute *bin_attr,
568 char *buf, loff_t off, size_t count) 568 char *buf, loff_t off, size_t count)
569 { 569 {
570 struct pci_bus *bus = to_pci_bus(container_of(kobj, 570 struct pci_bus *bus = to_pci_bus(container_of(kobj,
571 struct device, 571 struct device,
572 kobj)); 572 kobj));
573 /* Only support 1, 2 or 4 byte accesses */ 573 /* Only support 1, 2 or 4 byte accesses */
574 if (count != 1 && count != 2 && count != 4) 574 if (count != 1 && count != 2 && count != 4)
575 return -EINVAL; 575 return -EINVAL;
576 576
577 return pci_legacy_write(bus, off, *(u32 *)buf, count); 577 return pci_legacy_write(bus, off, *(u32 *)buf, count);
578 } 578 }
579 579
580 /** 580 /**
581 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space 581 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
582 * @filp: open sysfs file 582 * @filp: open sysfs file
583 * @kobj: kobject corresponding to device to be mapped 583 * @kobj: kobject corresponding to device to be mapped
584 * @attr: struct bin_attribute for this file 584 * @attr: struct bin_attribute for this file
585 * @vma: struct vm_area_struct passed to mmap 585 * @vma: struct vm_area_struct passed to mmap
586 * 586 *
587 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap 587 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
588 * legacy memory space (first meg of bus space) into application virtual 588 * legacy memory space (first meg of bus space) into application virtual
589 * memory space. 589 * memory space.
590 */ 590 */
591 static int 591 static int
592 pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj, 592 pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
593 struct bin_attribute *attr, 593 struct bin_attribute *attr,
594 struct vm_area_struct *vma) 594 struct vm_area_struct *vma)
595 { 595 {
596 struct pci_bus *bus = to_pci_bus(container_of(kobj, 596 struct pci_bus *bus = to_pci_bus(container_of(kobj,
597 struct device, 597 struct device,
598 kobj)); 598 kobj));
599 599
600 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem); 600 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
601 } 601 }
602 602
603 /** 603 /**
604 * pci_mmap_legacy_io - map legacy PCI IO into user memory space 604 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
605 * @filp: open sysfs file 605 * @filp: open sysfs file
606 * @kobj: kobject corresponding to device to be mapped 606 * @kobj: kobject corresponding to device to be mapped
607 * @attr: struct bin_attribute for this file 607 * @attr: struct bin_attribute for this file
608 * @vma: struct vm_area_struct passed to mmap 608 * @vma: struct vm_area_struct passed to mmap
609 * 609 *
610 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap 610 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
611 * legacy IO space (first meg of bus space) into application virtual 611 * legacy IO space (first meg of bus space) into application virtual
612 * memory space. Returns -ENOSYS if the operation isn't supported 612 * memory space. Returns -ENOSYS if the operation isn't supported
613 */ 613 */
614 static int 614 static int
615 pci_mmap_legacy_io(struct file *filp, struct kobject *kobj, 615 pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
616 struct bin_attribute *attr, 616 struct bin_attribute *attr,
617 struct vm_area_struct *vma) 617 struct vm_area_struct *vma)
618 { 618 {
619 struct pci_bus *bus = to_pci_bus(container_of(kobj, 619 struct pci_bus *bus = to_pci_bus(container_of(kobj,
620 struct device, 620 struct device,
621 kobj)); 621 kobj));
622 622
623 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io); 623 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
624 } 624 }
625 625
626 /** 626 /**
627 * pci_adjust_legacy_attr - adjustment of legacy file attributes 627 * pci_adjust_legacy_attr - adjustment of legacy file attributes
628 * @b: bus to create files under 628 * @b: bus to create files under
629 * @mmap_type: I/O port or memory 629 * @mmap_type: I/O port or memory
630 * 630 *
631 * Stub implementation. Can be overridden by arch if necessary. 631 * Stub implementation. Can be overridden by arch if necessary.
632 */ 632 */
633 void __weak 633 void __weak
634 pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type) 634 pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
635 { 635 {
636 return; 636 return;
637 } 637 }
638 638
639 /** 639 /**
640 * pci_create_legacy_files - create legacy I/O port and memory files 640 * pci_create_legacy_files - create legacy I/O port and memory files
641 * @b: bus to create files under 641 * @b: bus to create files under
642 * 642 *
643 * Some platforms allow access to legacy I/O port and ISA memory space on 643 * Some platforms allow access to legacy I/O port and ISA memory space on
644 * a per-bus basis. This routine creates the files and ties them into 644 * a per-bus basis. This routine creates the files and ties them into
645 * their associated read, write and mmap files from pci-sysfs.c 645 * their associated read, write and mmap files from pci-sysfs.c
646 * 646 *
647 * On error unwind, but don't propogate the error to the caller 647 * On error unwind, but don't propogate the error to the caller
648 * as it is ok to set up the PCI bus without these files. 648 * as it is ok to set up the PCI bus without these files.
649 */ 649 */
650 void pci_create_legacy_files(struct pci_bus *b) 650 void pci_create_legacy_files(struct pci_bus *b)
651 { 651 {
652 int error; 652 int error;
653 653
654 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2, 654 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
655 GFP_ATOMIC); 655 GFP_ATOMIC);
656 if (!b->legacy_io) 656 if (!b->legacy_io)
657 goto kzalloc_err; 657 goto kzalloc_err;
658 658
659 sysfs_bin_attr_init(b->legacy_io); 659 sysfs_bin_attr_init(b->legacy_io);
660 b->legacy_io->attr.name = "legacy_io"; 660 b->legacy_io->attr.name = "legacy_io";
661 b->legacy_io->size = 0xffff; 661 b->legacy_io->size = 0xffff;
662 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR; 662 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
663 b->legacy_io->read = pci_read_legacy_io; 663 b->legacy_io->read = pci_read_legacy_io;
664 b->legacy_io->write = pci_write_legacy_io; 664 b->legacy_io->write = pci_write_legacy_io;
665 b->legacy_io->mmap = pci_mmap_legacy_io; 665 b->legacy_io->mmap = pci_mmap_legacy_io;
666 pci_adjust_legacy_attr(b, pci_mmap_io); 666 pci_adjust_legacy_attr(b, pci_mmap_io);
667 error = device_create_bin_file(&b->dev, b->legacy_io); 667 error = device_create_bin_file(&b->dev, b->legacy_io);
668 if (error) 668 if (error)
669 goto legacy_io_err; 669 goto legacy_io_err;
670 670
671 /* Allocated above after the legacy_io struct */ 671 /* Allocated above after the legacy_io struct */
672 b->legacy_mem = b->legacy_io + 1; 672 b->legacy_mem = b->legacy_io + 1;
673 sysfs_bin_attr_init(b->legacy_mem); 673 sysfs_bin_attr_init(b->legacy_mem);
674 b->legacy_mem->attr.name = "legacy_mem"; 674 b->legacy_mem->attr.name = "legacy_mem";
675 b->legacy_mem->size = 1024*1024; 675 b->legacy_mem->size = 1024*1024;
676 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR; 676 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
677 b->legacy_mem->mmap = pci_mmap_legacy_mem; 677 b->legacy_mem->mmap = pci_mmap_legacy_mem;
678 pci_adjust_legacy_attr(b, pci_mmap_mem); 678 pci_adjust_legacy_attr(b, pci_mmap_mem);
679 error = device_create_bin_file(&b->dev, b->legacy_mem); 679 error = device_create_bin_file(&b->dev, b->legacy_mem);
680 if (error) 680 if (error)
681 goto legacy_mem_err; 681 goto legacy_mem_err;
682 682
683 return; 683 return;
684 684
685 legacy_mem_err: 685 legacy_mem_err:
686 device_remove_bin_file(&b->dev, b->legacy_io); 686 device_remove_bin_file(&b->dev, b->legacy_io);
687 legacy_io_err: 687 legacy_io_err:
688 kfree(b->legacy_io); 688 kfree(b->legacy_io);
689 b->legacy_io = NULL; 689 b->legacy_io = NULL;
690 kzalloc_err: 690 kzalloc_err:
691 printk(KERN_WARNING "pci: warning: could not create legacy I/O port " 691 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
692 "and ISA memory resources to sysfs\n"); 692 "and ISA memory resources to sysfs\n");
693 return; 693 return;
694 } 694 }
695 695
696 void pci_remove_legacy_files(struct pci_bus *b) 696 void pci_remove_legacy_files(struct pci_bus *b)
697 { 697 {
698 if (b->legacy_io) { 698 if (b->legacy_io) {
699 device_remove_bin_file(&b->dev, b->legacy_io); 699 device_remove_bin_file(&b->dev, b->legacy_io);
700 device_remove_bin_file(&b->dev, b->legacy_mem); 700 device_remove_bin_file(&b->dev, b->legacy_mem);
701 kfree(b->legacy_io); /* both are allocated here */ 701 kfree(b->legacy_io); /* both are allocated here */
702 } 702 }
703 } 703 }
704 #endif /* HAVE_PCI_LEGACY */ 704 #endif /* HAVE_PCI_LEGACY */
705 705
706 #ifdef HAVE_PCI_MMAP 706 #ifdef HAVE_PCI_MMAP
707 707
708 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma, 708 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
709 enum pci_mmap_api mmap_api) 709 enum pci_mmap_api mmap_api)
710 { 710 {
711 unsigned long nr, start, size, pci_start; 711 unsigned long nr, start, size, pci_start;
712 712
713 if (pci_resource_len(pdev, resno) == 0) 713 if (pci_resource_len(pdev, resno) == 0)
714 return 0; 714 return 0;
715 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; 715 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
716 start = vma->vm_pgoff; 716 start = vma->vm_pgoff;
717 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1; 717 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
718 pci_start = (mmap_api == PCI_MMAP_PROCFS) ? 718 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
719 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0; 719 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
720 if (start >= pci_start && start < pci_start + size && 720 if (start >= pci_start && start < pci_start + size &&
721 start + nr <= pci_start + size) 721 start + nr <= pci_start + size)
722 return 1; 722 return 1;
723 return 0; 723 return 0;
724 } 724 }
725 725
726 /** 726 /**
727 * pci_mmap_resource - map a PCI resource into user memory space 727 * pci_mmap_resource - map a PCI resource into user memory space
728 * @kobj: kobject for mapping 728 * @kobj: kobject for mapping
729 * @attr: struct bin_attribute for the file being mapped 729 * @attr: struct bin_attribute for the file being mapped
730 * @vma: struct vm_area_struct passed into the mmap 730 * @vma: struct vm_area_struct passed into the mmap
731 * @write_combine: 1 for write_combine mapping 731 * @write_combine: 1 for write_combine mapping
732 * 732 *
733 * Use the regular PCI mapping routines to map a PCI resource into userspace. 733 * Use the regular PCI mapping routines to map a PCI resource into userspace.
734 */ 734 */
735 static int 735 static int
736 pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr, 736 pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
737 struct vm_area_struct *vma, int write_combine) 737 struct vm_area_struct *vma, int write_combine)
738 { 738 {
739 struct pci_dev *pdev = to_pci_dev(container_of(kobj, 739 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
740 struct device, kobj)); 740 struct device, kobj));
741 struct resource *res = attr->private; 741 struct resource *res = attr->private;
742 enum pci_mmap_state mmap_type; 742 enum pci_mmap_state mmap_type;
743 resource_size_t start, end; 743 resource_size_t start, end;
744 int i; 744 int i;
745 745
746 for (i = 0; i < PCI_ROM_RESOURCE; i++) 746 for (i = 0; i < PCI_ROM_RESOURCE; i++)
747 if (res == &pdev->resource[i]) 747 if (res == &pdev->resource[i])
748 break; 748 break;
749 if (i >= PCI_ROM_RESOURCE) 749 if (i >= PCI_ROM_RESOURCE)
750 return -ENODEV; 750 return -ENODEV;
751 751
752 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) { 752 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
753 WARN(1, "process \"%s\" tried to map 0x%08lx bytes " 753 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
754 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n", 754 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
755 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff, 755 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
756 pci_name(pdev), i, 756 pci_name(pdev), i,
757 (u64)pci_resource_start(pdev, i), 757 (u64)pci_resource_start(pdev, i),
758 (u64)pci_resource_len(pdev, i)); 758 (u64)pci_resource_len(pdev, i));
759 return -EINVAL; 759 return -EINVAL;
760 } 760 }
761 761
762 /* pci_mmap_page_range() expects the same kind of entry as coming 762 /* pci_mmap_page_range() expects the same kind of entry as coming
763 * from /proc/bus/pci/ which is a "user visible" value. If this is 763 * from /proc/bus/pci/ which is a "user visible" value. If this is
764 * different from the resource itself, arch will do necessary fixup. 764 * different from the resource itself, arch will do necessary fixup.
765 */ 765 */
766 pci_resource_to_user(pdev, i, res, &start, &end); 766 pci_resource_to_user(pdev, i, res, &start, &end);
767 vma->vm_pgoff += start >> PAGE_SHIFT; 767 vma->vm_pgoff += start >> PAGE_SHIFT;
768 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io; 768 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
769 769
770 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start)) 770 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
771 return -EINVAL; 771 return -EINVAL;
772 772
773 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine); 773 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
774 } 774 }
775 775
776 static int 776 static int
777 pci_mmap_resource_uc(struct file *filp, struct kobject *kobj, 777 pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
778 struct bin_attribute *attr, 778 struct bin_attribute *attr,
779 struct vm_area_struct *vma) 779 struct vm_area_struct *vma)
780 { 780 {
781 return pci_mmap_resource(kobj, attr, vma, 0); 781 return pci_mmap_resource(kobj, attr, vma, 0);
782 } 782 }
783 783
784 static int 784 static int
785 pci_mmap_resource_wc(struct file *filp, struct kobject *kobj, 785 pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
786 struct bin_attribute *attr, 786 struct bin_attribute *attr,
787 struct vm_area_struct *vma) 787 struct vm_area_struct *vma)
788 { 788 {
789 return pci_mmap_resource(kobj, attr, vma, 1); 789 return pci_mmap_resource(kobj, attr, vma, 1);
790 } 790 }
791 791
792 static ssize_t 792 static ssize_t
793 pci_resource_io(struct file *filp, struct kobject *kobj, 793 pci_resource_io(struct file *filp, struct kobject *kobj,
794 struct bin_attribute *attr, char *buf, 794 struct bin_attribute *attr, char *buf,
795 loff_t off, size_t count, bool write) 795 loff_t off, size_t count, bool write)
796 { 796 {
797 struct pci_dev *pdev = to_pci_dev(container_of(kobj, 797 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
798 struct device, kobj)); 798 struct device, kobj));
799 struct resource *res = attr->private; 799 struct resource *res = attr->private;
800 unsigned long port = off; 800 unsigned long port = off;
801 int i; 801 int i;
802 802
803 for (i = 0; i < PCI_ROM_RESOURCE; i++) 803 for (i = 0; i < PCI_ROM_RESOURCE; i++)
804 if (res == &pdev->resource[i]) 804 if (res == &pdev->resource[i])
805 break; 805 break;
806 if (i >= PCI_ROM_RESOURCE) 806 if (i >= PCI_ROM_RESOURCE)
807 return -ENODEV; 807 return -ENODEV;
808 808
809 port += pci_resource_start(pdev, i); 809 port += pci_resource_start(pdev, i);
810 810
811 if (port > pci_resource_end(pdev, i)) 811 if (port > pci_resource_end(pdev, i))
812 return 0; 812 return 0;
813 813
814 if (port + count - 1 > pci_resource_end(pdev, i)) 814 if (port + count - 1 > pci_resource_end(pdev, i))
815 return -EINVAL; 815 return -EINVAL;
816 816
817 switch (count) { 817 switch (count) {
818 case 1: 818 case 1:
819 if (write) 819 if (write)
820 outb(*(u8 *)buf, port); 820 outb(*(u8 *)buf, port);
821 else 821 else
822 *(u8 *)buf = inb(port); 822 *(u8 *)buf = inb(port);
823 return 1; 823 return 1;
824 case 2: 824 case 2:
825 if (write) 825 if (write)
826 outw(*(u16 *)buf, port); 826 outw(*(u16 *)buf, port);
827 else 827 else
828 *(u16 *)buf = inw(port); 828 *(u16 *)buf = inw(port);
829 return 2; 829 return 2;
830 case 4: 830 case 4:
831 if (write) 831 if (write)
832 outl(*(u32 *)buf, port); 832 outl(*(u32 *)buf, port);
833 else 833 else
834 *(u32 *)buf = inl(port); 834 *(u32 *)buf = inl(port);
835 return 4; 835 return 4;
836 } 836 }
837 return -EINVAL; 837 return -EINVAL;
838 } 838 }
839 839
840 static ssize_t 840 static ssize_t
841 pci_read_resource_io(struct file *filp, struct kobject *kobj, 841 pci_read_resource_io(struct file *filp, struct kobject *kobj,
842 struct bin_attribute *attr, char *buf, 842 struct bin_attribute *attr, char *buf,
843 loff_t off, size_t count) 843 loff_t off, size_t count)
844 { 844 {
845 return pci_resource_io(filp, kobj, attr, buf, off, count, false); 845 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
846 } 846 }
847 847
848 static ssize_t 848 static ssize_t
849 pci_write_resource_io(struct file *filp, struct kobject *kobj, 849 pci_write_resource_io(struct file *filp, struct kobject *kobj,
850 struct bin_attribute *attr, char *buf, 850 struct bin_attribute *attr, char *buf,
851 loff_t off, size_t count) 851 loff_t off, size_t count)
852 { 852 {
853 return pci_resource_io(filp, kobj, attr, buf, off, count, true); 853 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
854 } 854 }
855 855
856 /** 856 /**
857 * pci_remove_resource_files - cleanup resource files 857 * pci_remove_resource_files - cleanup resource files
858 * @pdev: dev to cleanup 858 * @pdev: dev to cleanup
859 * 859 *
860 * If we created resource files for @pdev, remove them from sysfs and 860 * If we created resource files for @pdev, remove them from sysfs and
861 * free their resources. 861 * free their resources.
862 */ 862 */
863 static void 863 static void
864 pci_remove_resource_files(struct pci_dev *pdev) 864 pci_remove_resource_files(struct pci_dev *pdev)
865 { 865 {
866 int i; 866 int i;
867 867
868 for (i = 0; i < PCI_ROM_RESOURCE; i++) { 868 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
869 struct bin_attribute *res_attr; 869 struct bin_attribute *res_attr;
870 870
871 res_attr = pdev->res_attr[i]; 871 res_attr = pdev->res_attr[i];
872 if (res_attr) { 872 if (res_attr) {
873 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); 873 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
874 kfree(res_attr); 874 kfree(res_attr);
875 } 875 }
876 876
877 res_attr = pdev->res_attr_wc[i]; 877 res_attr = pdev->res_attr_wc[i];
878 if (res_attr) { 878 if (res_attr) {
879 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); 879 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
880 kfree(res_attr); 880 kfree(res_attr);
881 } 881 }
882 } 882 }
883 } 883 }
884 884
885 static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine) 885 static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
886 { 886 {
887 /* allocate attribute structure, piggyback attribute name */ 887 /* allocate attribute structure, piggyback attribute name */
888 int name_len = write_combine ? 13 : 10; 888 int name_len = write_combine ? 13 : 10;
889 struct bin_attribute *res_attr; 889 struct bin_attribute *res_attr;
890 int retval; 890 int retval;
891 891
892 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC); 892 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
893 if (res_attr) { 893 if (res_attr) {
894 char *res_attr_name = (char *)(res_attr + 1); 894 char *res_attr_name = (char *)(res_attr + 1);
895 895
896 sysfs_bin_attr_init(res_attr); 896 sysfs_bin_attr_init(res_attr);
897 if (write_combine) { 897 if (write_combine) {
898 pdev->res_attr_wc[num] = res_attr; 898 pdev->res_attr_wc[num] = res_attr;
899 sprintf(res_attr_name, "resource%d_wc", num); 899 sprintf(res_attr_name, "resource%d_wc", num);
900 res_attr->mmap = pci_mmap_resource_wc; 900 res_attr->mmap = pci_mmap_resource_wc;
901 } else { 901 } else {
902 pdev->res_attr[num] = res_attr; 902 pdev->res_attr[num] = res_attr;
903 sprintf(res_attr_name, "resource%d", num); 903 sprintf(res_attr_name, "resource%d", num);
904 res_attr->mmap = pci_mmap_resource_uc; 904 res_attr->mmap = pci_mmap_resource_uc;
905 } 905 }
906 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) { 906 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
907 res_attr->read = pci_read_resource_io; 907 res_attr->read = pci_read_resource_io;
908 res_attr->write = pci_write_resource_io; 908 res_attr->write = pci_write_resource_io;
909 } 909 }
910 res_attr->attr.name = res_attr_name; 910 res_attr->attr.name = res_attr_name;
911 res_attr->attr.mode = S_IRUSR | S_IWUSR; 911 res_attr->attr.mode = S_IRUSR | S_IWUSR;
912 res_attr->size = pci_resource_len(pdev, num); 912 res_attr->size = pci_resource_len(pdev, num);
913 res_attr->private = &pdev->resource[num]; 913 res_attr->private = &pdev->resource[num];
914 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr); 914 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
915 } else 915 } else
916 retval = -ENOMEM; 916 retval = -ENOMEM;
917 917
918 return retval; 918 return retval;
919 } 919 }
920 920
921 /** 921 /**
922 * pci_create_resource_files - create resource files in sysfs for @dev 922 * pci_create_resource_files - create resource files in sysfs for @dev
923 * @pdev: dev in question 923 * @pdev: dev in question
924 * 924 *
925 * Walk the resources in @pdev creating files for each resource available. 925 * Walk the resources in @pdev creating files for each resource available.
926 */ 926 */
927 static int pci_create_resource_files(struct pci_dev *pdev) 927 static int pci_create_resource_files(struct pci_dev *pdev)
928 { 928 {
929 int i; 929 int i;
930 int retval; 930 int retval;
931 931
932 /* Expose the PCI resources from this device as files */ 932 /* Expose the PCI resources from this device as files */
933 for (i = 0; i < PCI_ROM_RESOURCE; i++) { 933 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
934 934
935 /* skip empty resources */ 935 /* skip empty resources */
936 if (!pci_resource_len(pdev, i)) 936 if (!pci_resource_len(pdev, i))
937 continue; 937 continue;
938 938
939 retval = pci_create_attr(pdev, i, 0); 939 retval = pci_create_attr(pdev, i, 0);
940 /* for prefetchable resources, create a WC mappable file */ 940 /* for prefetchable resources, create a WC mappable file */
941 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH) 941 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
942 retval = pci_create_attr(pdev, i, 1); 942 retval = pci_create_attr(pdev, i, 1);
943 943
944 if (retval) { 944 if (retval) {
945 pci_remove_resource_files(pdev); 945 pci_remove_resource_files(pdev);
946 return retval; 946 return retval;
947 } 947 }
948 } 948 }
949 return 0; 949 return 0;
950 } 950 }
951 #else /* !HAVE_PCI_MMAP */ 951 #else /* !HAVE_PCI_MMAP */
952 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; } 952 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
953 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; } 953 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
954 #endif /* HAVE_PCI_MMAP */ 954 #endif /* HAVE_PCI_MMAP */
955 955
956 /** 956 /**
957 * pci_write_rom - used to enable access to the PCI ROM display 957 * pci_write_rom - used to enable access to the PCI ROM display
958 * @filp: sysfs file 958 * @filp: sysfs file
959 * @kobj: kernel object handle 959 * @kobj: kernel object handle
960 * @bin_attr: struct bin_attribute for this file 960 * @bin_attr: struct bin_attribute for this file
961 * @buf: user input 961 * @buf: user input
962 * @off: file offset 962 * @off: file offset
963 * @count: number of byte in input 963 * @count: number of byte in input
964 * 964 *
965 * writing anything except 0 enables it 965 * writing anything except 0 enables it
966 */ 966 */
967 static ssize_t 967 static ssize_t
968 pci_write_rom(struct file *filp, struct kobject *kobj, 968 pci_write_rom(struct file *filp, struct kobject *kobj,
969 struct bin_attribute *bin_attr, 969 struct bin_attribute *bin_attr,
970 char *buf, loff_t off, size_t count) 970 char *buf, loff_t off, size_t count)
971 { 971 {
972 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj)); 972 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
973 973
974 if ((off == 0) && (*buf == '0') && (count == 2)) 974 if ((off == 0) && (*buf == '0') && (count == 2))
975 pdev->rom_attr_enabled = 0; 975 pdev->rom_attr_enabled = 0;
976 else 976 else
977 pdev->rom_attr_enabled = 1; 977 pdev->rom_attr_enabled = 1;
978 978
979 return count; 979 return count;
980 } 980 }
981 981
982 /** 982 /**
983 * pci_read_rom - read a PCI ROM 983 * pci_read_rom - read a PCI ROM
984 * @filp: sysfs file 984 * @filp: sysfs file
985 * @kobj: kernel object handle 985 * @kobj: kernel object handle
986 * @bin_attr: struct bin_attribute for this file 986 * @bin_attr: struct bin_attribute for this file
987 * @buf: where to put the data we read from the ROM 987 * @buf: where to put the data we read from the ROM
988 * @off: file offset 988 * @off: file offset
989 * @count: number of bytes to read 989 * @count: number of bytes to read
990 * 990 *
991 * Put @count bytes starting at @off into @buf from the ROM in the PCI 991 * Put @count bytes starting at @off into @buf from the ROM in the PCI
992 * device corresponding to @kobj. 992 * device corresponding to @kobj.
993 */ 993 */
994 static ssize_t 994 static ssize_t
995 pci_read_rom(struct file *filp, struct kobject *kobj, 995 pci_read_rom(struct file *filp, struct kobject *kobj,
996 struct bin_attribute *bin_attr, 996 struct bin_attribute *bin_attr,
997 char *buf, loff_t off, size_t count) 997 char *buf, loff_t off, size_t count)
998 { 998 {
999 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj)); 999 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1000 void __iomem *rom; 1000 void __iomem *rom;
1001 size_t size; 1001 size_t size;
1002 1002
1003 if (!pdev->rom_attr_enabled) 1003 if (!pdev->rom_attr_enabled)
1004 return -EINVAL; 1004 return -EINVAL;
1005 1005
1006 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */ 1006 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
1007 if (!rom || !size) 1007 if (!rom || !size)
1008 return -EIO; 1008 return -EIO;
1009 1009
1010 if (off >= size) 1010 if (off >= size)
1011 count = 0; 1011 count = 0;
1012 else { 1012 else {
1013 if (off + count > size) 1013 if (off + count > size)
1014 count = size - off; 1014 count = size - off;
1015 1015
1016 memcpy_fromio(buf, rom + off, count); 1016 memcpy_fromio(buf, rom + off, count);
1017 } 1017 }
1018 pci_unmap_rom(pdev, rom); 1018 pci_unmap_rom(pdev, rom);
1019 1019
1020 return count; 1020 return count;
1021 } 1021 }
1022 1022
1023 static struct bin_attribute pci_config_attr = { 1023 static struct bin_attribute pci_config_attr = {
1024 .attr = { 1024 .attr = {
1025 .name = "config", 1025 .name = "config",
1026 .mode = S_IRUGO | S_IWUSR, 1026 .mode = S_IRUGO | S_IWUSR,
1027 }, 1027 },
1028 .size = PCI_CFG_SPACE_SIZE, 1028 .size = PCI_CFG_SPACE_SIZE,
1029 .read = pci_read_config, 1029 .read = pci_read_config,
1030 .write = pci_write_config, 1030 .write = pci_write_config,
1031 }; 1031 };
1032 1032
1033 static struct bin_attribute pcie_config_attr = { 1033 static struct bin_attribute pcie_config_attr = {
1034 .attr = { 1034 .attr = {
1035 .name = "config", 1035 .name = "config",
1036 .mode = S_IRUGO | S_IWUSR, 1036 .mode = S_IRUGO | S_IWUSR,
1037 }, 1037 },
1038 .size = PCI_CFG_SPACE_EXP_SIZE, 1038 .size = PCI_CFG_SPACE_EXP_SIZE,
1039 .read = pci_read_config, 1039 .read = pci_read_config,
1040 .write = pci_write_config, 1040 .write = pci_write_config,
1041 }; 1041 };
1042 1042
1043 int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev) 1043 int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
1044 { 1044 {
1045 return 0; 1045 return 0;
1046 } 1046 }
1047 1047
1048 static ssize_t reset_store(struct device *dev, 1048 static ssize_t reset_store(struct device *dev,
1049 struct device_attribute *attr, const char *buf, 1049 struct device_attribute *attr, const char *buf,
1050 size_t count) 1050 size_t count)
1051 { 1051 {
1052 struct pci_dev *pdev = to_pci_dev(dev); 1052 struct pci_dev *pdev = to_pci_dev(dev);
1053 unsigned long val; 1053 unsigned long val;
1054 ssize_t result = strict_strtoul(buf, 0, &val); 1054 ssize_t result = strict_strtoul(buf, 0, &val);
1055 1055
1056 if (result < 0) 1056 if (result < 0)
1057 return result; 1057 return result;
1058 1058
1059 if (val != 1) 1059 if (val != 1)
1060 return -EINVAL; 1060 return -EINVAL;
1061 1061
1062 result = pci_reset_function(pdev); 1062 result = pci_reset_function(pdev);
1063 if (result < 0) 1063 if (result < 0)
1064 return result; 1064 return result;
1065 1065
1066 return count; 1066 return count;
1067 } 1067 }
1068 1068
1069 static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store); 1069 static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1070 1070
1071 static int pci_create_capabilities_sysfs(struct pci_dev *dev) 1071 static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1072 { 1072 {
1073 int retval; 1073 int retval;
1074 struct bin_attribute *attr; 1074 struct bin_attribute *attr;
1075 1075
1076 /* If the device has VPD, try to expose it in sysfs. */ 1076 /* If the device has VPD, try to expose it in sysfs. */
1077 if (dev->vpd) { 1077 if (dev->vpd) {
1078 attr = kzalloc(sizeof(*attr), GFP_ATOMIC); 1078 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1079 if (!attr) 1079 if (!attr)
1080 return -ENOMEM; 1080 return -ENOMEM;
1081 1081
1082 sysfs_bin_attr_init(attr); 1082 sysfs_bin_attr_init(attr);
1083 attr->size = dev->vpd->len; 1083 attr->size = dev->vpd->len;
1084 attr->attr.name = "vpd"; 1084 attr->attr.name = "vpd";
1085 attr->attr.mode = S_IRUSR | S_IWUSR; 1085 attr->attr.mode = S_IRUSR | S_IWUSR;
1086 attr->read = read_vpd_attr; 1086 attr->read = read_vpd_attr;
1087 attr->write = write_vpd_attr; 1087 attr->write = write_vpd_attr;
1088 retval = sysfs_create_bin_file(&dev->dev.kobj, attr); 1088 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1089 if (retval) { 1089 if (retval) {
1090 kfree(dev->vpd->attr); 1090 kfree(attr);
1091 return retval; 1091 return retval;
1092 } 1092 }
1093 dev->vpd->attr = attr; 1093 dev->vpd->attr = attr;
1094 } 1094 }
1095 1095
1096 /* Active State Power Management */ 1096 /* Active State Power Management */
1097 pcie_aspm_create_sysfs_dev_files(dev); 1097 pcie_aspm_create_sysfs_dev_files(dev);
1098 1098
1099 if (!pci_probe_reset_function(dev)) { 1099 if (!pci_probe_reset_function(dev)) {
1100 retval = device_create_file(&dev->dev, &reset_attr); 1100 retval = device_create_file(&dev->dev, &reset_attr);
1101 if (retval) 1101 if (retval)
1102 goto error; 1102 goto error;
1103 dev->reset_fn = 1; 1103 dev->reset_fn = 1;
1104 } 1104 }
1105 return 0; 1105 return 0;
1106 1106
1107 error: 1107 error:
1108 pcie_aspm_remove_sysfs_dev_files(dev); 1108 pcie_aspm_remove_sysfs_dev_files(dev);
1109 if (dev->vpd && dev->vpd->attr) { 1109 if (dev->vpd && dev->vpd->attr) {
1110 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr); 1110 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1111 kfree(dev->vpd->attr); 1111 kfree(dev->vpd->attr);
1112 } 1112 }
1113 1113
1114 return retval; 1114 return retval;
1115 } 1115 }
1116 1116
1117 int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev) 1117 int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
1118 { 1118 {
1119 int retval; 1119 int retval;
1120 int rom_size = 0; 1120 int rom_size = 0;
1121 struct bin_attribute *attr; 1121 struct bin_attribute *attr;
1122 1122
1123 if (!sysfs_initialized) 1123 if (!sysfs_initialized)
1124 return -EACCES; 1124 return -EACCES;
1125 1125
1126 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE) 1126 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1127 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr); 1127 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
1128 else 1128 else
1129 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr); 1129 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1130 if (retval) 1130 if (retval)
1131 goto err; 1131 goto err;
1132 1132
1133 retval = pci_create_resource_files(pdev); 1133 retval = pci_create_resource_files(pdev);
1134 if (retval) 1134 if (retval)
1135 goto err_config_file; 1135 goto err_config_file;
1136 1136
1137 if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) 1137 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1138 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE); 1138 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1139 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW) 1139 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1140 rom_size = 0x20000; 1140 rom_size = 0x20000;
1141 1141
1142 /* If the device has a ROM, try to expose it in sysfs. */ 1142 /* If the device has a ROM, try to expose it in sysfs. */
1143 if (rom_size) { 1143 if (rom_size) {
1144 attr = kzalloc(sizeof(*attr), GFP_ATOMIC); 1144 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1145 if (!attr) { 1145 if (!attr) {
1146 retval = -ENOMEM; 1146 retval = -ENOMEM;
1147 goto err_resource_files; 1147 goto err_resource_files;
1148 } 1148 }
1149 sysfs_bin_attr_init(attr); 1149 sysfs_bin_attr_init(attr);
1150 attr->size = rom_size; 1150 attr->size = rom_size;
1151 attr->attr.name = "rom"; 1151 attr->attr.name = "rom";
1152 attr->attr.mode = S_IRUSR | S_IWUSR; 1152 attr->attr.mode = S_IRUSR | S_IWUSR;
1153 attr->read = pci_read_rom; 1153 attr->read = pci_read_rom;
1154 attr->write = pci_write_rom; 1154 attr->write = pci_write_rom;
1155 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr); 1155 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1156 if (retval) { 1156 if (retval) {
1157 kfree(attr); 1157 kfree(attr);
1158 goto err_resource_files; 1158 goto err_resource_files;
1159 } 1159 }
1160 pdev->rom_attr = attr; 1160 pdev->rom_attr = attr;
1161 } 1161 }
1162 1162
1163 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) { 1163 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1164 retval = device_create_file(&pdev->dev, &vga_attr); 1164 retval = device_create_file(&pdev->dev, &vga_attr);
1165 if (retval) 1165 if (retval)
1166 goto err_rom_file; 1166 goto err_rom_file;
1167 } 1167 }
1168 1168
1169 /* add platform-specific attributes */ 1169 /* add platform-specific attributes */
1170 retval = pcibios_add_platform_entries(pdev); 1170 retval = pcibios_add_platform_entries(pdev);
1171 if (retval) 1171 if (retval)
1172 goto err_vga_file; 1172 goto err_vga_file;
1173 1173
1174 /* add sysfs entries for various capabilities */ 1174 /* add sysfs entries for various capabilities */
1175 retval = pci_create_capabilities_sysfs(pdev); 1175 retval = pci_create_capabilities_sysfs(pdev);
1176 if (retval) 1176 if (retval)
1177 goto err_vga_file; 1177 goto err_vga_file;
1178 1178
1179 pci_create_firmware_label_files(pdev); 1179 pci_create_firmware_label_files(pdev);
1180 1180
1181 return 0; 1181 return 0;
1182 1182
1183 err_vga_file: 1183 err_vga_file:
1184 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) 1184 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1185 device_remove_file(&pdev->dev, &vga_attr); 1185 device_remove_file(&pdev->dev, &vga_attr);
1186 err_rom_file: 1186 err_rom_file:
1187 if (rom_size) { 1187 if (rom_size) {
1188 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); 1188 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1189 kfree(pdev->rom_attr); 1189 kfree(pdev->rom_attr);
1190 pdev->rom_attr = NULL; 1190 pdev->rom_attr = NULL;
1191 } 1191 }
1192 err_resource_files: 1192 err_resource_files:
1193 pci_remove_resource_files(pdev); 1193 pci_remove_resource_files(pdev);
1194 err_config_file: 1194 err_config_file:
1195 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE) 1195 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1196 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); 1196 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1197 else 1197 else
1198 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); 1198 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1199 err: 1199 err:
1200 return retval; 1200 return retval;
1201 } 1201 }
1202 1202
1203 static void pci_remove_capabilities_sysfs(struct pci_dev *dev) 1203 static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1204 { 1204 {
1205 if (dev->vpd && dev->vpd->attr) { 1205 if (dev->vpd && dev->vpd->attr) {
1206 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr); 1206 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1207 kfree(dev->vpd->attr); 1207 kfree(dev->vpd->attr);
1208 } 1208 }
1209 1209
1210 pcie_aspm_remove_sysfs_dev_files(dev); 1210 pcie_aspm_remove_sysfs_dev_files(dev);
1211 if (dev->reset_fn) { 1211 if (dev->reset_fn) {
1212 device_remove_file(&dev->dev, &reset_attr); 1212 device_remove_file(&dev->dev, &reset_attr);
1213 dev->reset_fn = 0; 1213 dev->reset_fn = 0;
1214 } 1214 }
1215 } 1215 }
1216 1216
1217 /** 1217 /**
1218 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files 1218 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1219 * @pdev: device whose entries we should free 1219 * @pdev: device whose entries we should free
1220 * 1220 *
1221 * Cleanup when @pdev is removed from sysfs. 1221 * Cleanup when @pdev is removed from sysfs.
1222 */ 1222 */
1223 void pci_remove_sysfs_dev_files(struct pci_dev *pdev) 1223 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1224 { 1224 {
1225 int rom_size = 0; 1225 int rom_size = 0;
1226 1226
1227 if (!sysfs_initialized) 1227 if (!sysfs_initialized)
1228 return; 1228 return;
1229 1229
1230 pci_remove_capabilities_sysfs(pdev); 1230 pci_remove_capabilities_sysfs(pdev);
1231 1231
1232 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE) 1232 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1233 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); 1233 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1234 else 1234 else
1235 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); 1235 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1236 1236
1237 pci_remove_resource_files(pdev); 1237 pci_remove_resource_files(pdev);
1238 1238
1239 if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) 1239 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1240 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE); 1240 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1241 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW) 1241 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1242 rom_size = 0x20000; 1242 rom_size = 0x20000;
1243 1243
1244 if (rom_size && pdev->rom_attr) { 1244 if (rom_size && pdev->rom_attr) {
1245 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); 1245 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1246 kfree(pdev->rom_attr); 1246 kfree(pdev->rom_attr);
1247 } 1247 }
1248 1248
1249 pci_remove_firmware_label_files(pdev); 1249 pci_remove_firmware_label_files(pdev);
1250 1250
1251 } 1251 }
1252 1252
1253 static int __init pci_sysfs_init(void) 1253 static int __init pci_sysfs_init(void)
1254 { 1254 {
1255 struct pci_dev *pdev = NULL; 1255 struct pci_dev *pdev = NULL;
1256 int retval; 1256 int retval;
1257 1257
1258 sysfs_initialized = 1; 1258 sysfs_initialized = 1;
1259 for_each_pci_dev(pdev) { 1259 for_each_pci_dev(pdev) {
1260 retval = pci_create_sysfs_dev_files(pdev); 1260 retval = pci_create_sysfs_dev_files(pdev);
1261 if (retval) { 1261 if (retval) {
1262 pci_dev_put(pdev); 1262 pci_dev_put(pdev);
1263 return retval; 1263 return retval;
1264 } 1264 }
1265 } 1265 }
1266 1266
1267 return 0; 1267 return 0;
1268 } 1268 }
1269 1269
1270 late_initcall(pci_sysfs_init); 1270 late_initcall(pci_sysfs_init);
1271 1271