pci-sysfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * drivers/pci/pci-sysfs.c
  3. *
  4. * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
  5. * (C) Copyright 2002-2004 IBM Corp.
  6. * (C) Copyright 2003 Matthew Wilcox
  7. * (C) Copyright 2003 Hewlett-Packard
  8. * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
  9. * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
  10. *
  11. * File attributes for PCI devices
  12. *
  13. * Modeled after usb's driverfs.c
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/pci.h>
  18. #include <linux/stat.h>
  19. #include <linux/topology.h>
  20. #include <linux/mm.h>
  21. #include <linux/capability.h>
  22. #include "pci.h"
  23. static int sysfs_initialized; /* = 0 */
  24. /* show configuration fields */
  25. #define pci_config_attr(field, format_string) \
  26. static ssize_t \
  27. field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  28. { \
  29. struct pci_dev *pdev; \
  30. \
  31. pdev = to_pci_dev (dev); \
  32. return sprintf (buf, format_string, pdev->field); \
  33. }
  34. pci_config_attr(vendor, "0x%04x\n");
  35. pci_config_attr(device, "0x%04x\n");
  36. pci_config_attr(subsystem_vendor, "0x%04x\n");
  37. pci_config_attr(subsystem_device, "0x%04x\n");
  38. pci_config_attr(class, "0x%06x\n");
  39. pci_config_attr(irq, "%u\n");
  40. static ssize_t broken_parity_status_show(struct device *dev,
  41. struct device_attribute *attr,
  42. char *buf)
  43. {
  44. struct pci_dev *pdev = to_pci_dev(dev);
  45. return sprintf (buf, "%u\n", pdev->broken_parity_status);
  46. }
  47. static ssize_t broken_parity_status_store(struct device *dev,
  48. struct device_attribute *attr,
  49. const char *buf, size_t count)
  50. {
  51. struct pci_dev *pdev = to_pci_dev(dev);
  52. ssize_t consumed = -EINVAL;
  53. if ((count > 0) && (*buf == '0' || *buf == '1')) {
  54. pdev->broken_parity_status = *buf == '1' ? 1 : 0;
  55. consumed = count;
  56. }
  57. return consumed;
  58. }
  59. static ssize_t local_cpus_show(struct device *dev,
  60. struct device_attribute *attr, char *buf)
  61. {
  62. cpumask_t mask;
  63. int len;
  64. mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
  65. len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
  66. strcat(buf,"\n");
  67. return 1+len;
  68. }
  69. /* show resources */
  70. static ssize_t
  71. resource_show(struct device * dev, struct device_attribute *attr, char * buf)
  72. {
  73. struct pci_dev * pci_dev = to_pci_dev(dev);
  74. char * str = buf;
  75. int i;
  76. int max = 7;
  77. resource_size_t start, end;
  78. if (pci_dev->subordinate)
  79. max = DEVICE_COUNT_RESOURCE;
  80. for (i = 0; i < max; i++) {
  81. struct resource *res = &pci_dev->resource[i];
  82. pci_resource_to_user(pci_dev, i, res, &start, &end);
  83. str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
  84. (unsigned long long)start,
  85. (unsigned long long)end,
  86. (unsigned long long)res->flags);
  87. }
  88. return (str - buf);
  89. }
  90. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  91. {
  92. struct pci_dev *pci_dev = to_pci_dev(dev);
  93. return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
  94. pci_dev->vendor, pci_dev->device,
  95. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  96. (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
  97. (u8)(pci_dev->class));
  98. }
  99. static ssize_t is_enabled_store(struct device *dev,
  100. struct device_attribute *attr, const char *buf,
  101. size_t count)
  102. {
  103. ssize_t result = -EINVAL;
  104. struct pci_dev *pdev = to_pci_dev(dev);
  105. /* this can crash the machine when done on the "wrong" device */
  106. if (!capable(CAP_SYS_ADMIN))
  107. return count;
  108. if (*buf == '0') {
  109. if (atomic_read(&pdev->enable_cnt) != 0)
  110. pci_disable_device(pdev);
  111. else
  112. result = -EIO;
  113. } else if (*buf == '1')
  114. result = pci_enable_device(pdev);
  115. return result < 0 ? result : count;
  116. }
  117. static ssize_t is_enabled_show(struct device *dev,
  118. struct device_attribute *attr, char *buf)
  119. {
  120. struct pci_dev *pdev;
  121. pdev = to_pci_dev (dev);
  122. return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
  123. }
  124. #ifdef CONFIG_NUMA
  125. static ssize_t
  126. numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
  127. {
  128. return sprintf (buf, "%d\n", dev->numa_node);
  129. }
  130. #endif
  131. static ssize_t
  132. msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
  133. {
  134. struct pci_dev *pdev = to_pci_dev(dev);
  135. if (!pdev->subordinate)
  136. return 0;
  137. return sprintf (buf, "%u\n",
  138. !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
  139. }
  140. static ssize_t
  141. msi_bus_store(struct device *dev, struct device_attribute *attr,
  142. const char *buf, size_t count)
  143. {
  144. struct pci_dev *pdev = to_pci_dev(dev);
  145. /* bad things may happen if the no_msi flag is changed
  146. * while some drivers are loaded */
  147. if (!capable(CAP_SYS_ADMIN))
  148. return count;
  149. if (!pdev->subordinate)
  150. return count;
  151. if (*buf == '0') {
  152. pdev->subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
  153. dev_warn(&pdev->dev, "forced subordinate bus to not support MSI,"
  154. " bad things could happen.\n");
  155. }
  156. if (*buf == '1') {
  157. pdev->subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
  158. dev_warn(&pdev->dev, "forced subordinate bus to support MSI,"
  159. " bad things could happen.\n");
  160. }
  161. return count;
  162. }
  163. struct device_attribute pci_dev_attrs[] = {
  164. __ATTR_RO(resource),
  165. __ATTR_RO(vendor),
  166. __ATTR_RO(device),
  167. __ATTR_RO(subsystem_vendor),
  168. __ATTR_RO(subsystem_device),
  169. __ATTR_RO(class),
  170. __ATTR_RO(irq),
  171. __ATTR_RO(local_cpus),
  172. __ATTR_RO(modalias),
  173. #ifdef CONFIG_NUMA
  174. __ATTR_RO(numa_node),
  175. #endif
  176. __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
  177. __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
  178. broken_parity_status_show,broken_parity_status_store),
  179. __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
  180. __ATTR_NULL,
  181. };
  182. static ssize_t
  183. pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
  184. char *buf, loff_t off, size_t count)
  185. {
  186. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  187. unsigned int size = 64;
  188. loff_t init_off = off;
  189. u8 *data = (u8*) buf;
  190. /* Several chips lock up trying to read undefined config space */
  191. if (capable(CAP_SYS_ADMIN)) {
  192. size = dev->cfg_size;
  193. } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  194. size = 128;
  195. }
  196. if (off > size)
  197. return 0;
  198. if (off + count > size) {
  199. size -= off;
  200. count = size;
  201. } else {
  202. size = count;
  203. }
  204. if ((off & 1) && size) {
  205. u8 val;
  206. pci_user_read_config_byte(dev, off, &val);
  207. data[off - init_off] = val;
  208. off++;
  209. size--;
  210. }
  211. if ((off & 3) && size > 2) {
  212. u16 val;
  213. pci_user_read_config_word(dev, off, &val);
  214. data[off - init_off] = val & 0xff;
  215. data[off - init_off + 1] = (val >> 8) & 0xff;
  216. off += 2;
  217. size -= 2;
  218. }
  219. while (size > 3) {
  220. u32 val;
  221. pci_user_read_config_dword(dev, off, &val);
  222. data[off - init_off] = val & 0xff;
  223. data[off - init_off + 1] = (val >> 8) & 0xff;
  224. data[off - init_off + 2] = (val >> 16) & 0xff;
  225. data[off - init_off + 3] = (val >> 24) & 0xff;
  226. off += 4;
  227. size -= 4;
  228. }
  229. if (size >= 2) {
  230. u16 val;
  231. pci_user_read_config_word(dev, off, &val);
  232. data[off - init_off] = val & 0xff;
  233. data[off - init_off + 1] = (val >> 8) & 0xff;
  234. off += 2;
  235. size -= 2;
  236. }
  237. if (size > 0) {
  238. u8 val;
  239. pci_user_read_config_byte(dev, off, &val);
  240. data[off - init_off] = val;
  241. off++;
  242. --size;
  243. }
  244. return count;
  245. }
  246. static ssize_t
  247. pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
  248. char *buf, loff_t off, size_t count)
  249. {
  250. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  251. unsigned int size = count;
  252. loff_t init_off = off;
  253. u8 *data = (u8*) buf;
  254. if (off > dev->cfg_size)
  255. return 0;
  256. if (off + count > dev->cfg_size) {
  257. size = dev->cfg_size - off;
  258. count = size;
  259. }
  260. if ((off & 1) && size) {
  261. pci_user_write_config_byte(dev, off, data[off - init_off]);
  262. off++;
  263. size--;
  264. }
  265. if ((off & 3) && size > 2) {
  266. u16 val = data[off - init_off];
  267. val |= (u16) data[off - init_off + 1] << 8;
  268. pci_user_write_config_word(dev, off, val);
  269. off += 2;
  270. size -= 2;
  271. }
  272. while (size > 3) {
  273. u32 val = data[off - init_off];
  274. val |= (u32) data[off - init_off + 1] << 8;
  275. val |= (u32) data[off - init_off + 2] << 16;
  276. val |= (u32) data[off - init_off + 3] << 24;
  277. pci_user_write_config_dword(dev, off, val);
  278. off += 4;
  279. size -= 4;
  280. }
  281. if (size >= 2) {
  282. u16 val = data[off - init_off];
  283. val |= (u16) data[off - init_off + 1] << 8;
  284. pci_user_write_config_word(dev, off, val);
  285. off += 2;
  286. size -= 2;
  287. }
  288. if (size) {
  289. pci_user_write_config_byte(dev, off, data[off - init_off]);
  290. off++;
  291. --size;
  292. }
  293. return count;
  294. }
  295. #ifdef HAVE_PCI_LEGACY
  296. /**
  297. * pci_read_legacy_io - read byte(s) from legacy I/O port space
  298. * @kobj: kobject corresponding to file to read from
  299. * @buf: buffer to store results
  300. * @off: offset into legacy I/O port space
  301. * @count: number of bytes to read
  302. *
  303. * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  304. * callback routine (pci_legacy_read).
  305. */
  306. ssize_t
  307. pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
  308. char *buf, loff_t off, size_t count)
  309. {
  310. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  311. struct class_device,
  312. kobj));
  313. /* Only support 1, 2 or 4 byte accesses */
  314. if (count != 1 && count != 2 && count != 4)
  315. return -EINVAL;
  316. return pci_legacy_read(bus, off, (u32 *)buf, count);
  317. }
  318. /**
  319. * pci_write_legacy_io - write byte(s) to legacy I/O port space
  320. * @kobj: kobject corresponding to file to read from
  321. * @buf: buffer containing value to be written
  322. * @off: offset into legacy I/O port space
  323. * @count: number of bytes to write
  324. *
  325. * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  326. * callback routine (pci_legacy_write).
  327. */
  328. ssize_t
  329. pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
  330. char *buf, loff_t off, size_t count)
  331. {
  332. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  333. struct class_device,
  334. kobj));
  335. /* Only support 1, 2 or 4 byte accesses */
  336. if (count != 1 && count != 2 && count != 4)
  337. return -EINVAL;
  338. return pci_legacy_write(bus, off, *(u32 *)buf, count);
  339. }
  340. /**
  341. * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
  342. * @kobj: kobject corresponding to device to be mapped
  343. * @attr: struct bin_attribute for this file
  344. * @vma: struct vm_area_struct passed to mmap
  345. *
  346. * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
  347. * legacy memory space (first meg of bus space) into application virtual
  348. * memory space.
  349. */
  350. int
  351. pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
  352. struct vm_area_struct *vma)
  353. {
  354. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  355. struct class_device,
  356. kobj));
  357. return pci_mmap_legacy_page_range(bus, vma);
  358. }
  359. #endif /* HAVE_PCI_LEGACY */
  360. #ifdef HAVE_PCI_MMAP
  361. /**
  362. * pci_mmap_resource - map a PCI resource into user memory space
  363. * @kobj: kobject for mapping
  364. * @attr: struct bin_attribute for the file being mapped
  365. * @vma: struct vm_area_struct passed into the mmap
  366. *
  367. * Use the regular PCI mapping routines to map a PCI resource into userspace.
  368. * FIXME: write combining? maybe automatic for prefetchable regions?
  369. */
  370. static int
  371. pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
  372. struct vm_area_struct *vma)
  373. {
  374. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  375. struct device, kobj));
  376. struct resource *res = (struct resource *)attr->private;
  377. enum pci_mmap_state mmap_type;
  378. resource_size_t start, end;
  379. int i;
  380. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  381. if (res == &pdev->resource[i])
  382. break;
  383. if (i >= PCI_ROM_RESOURCE)
  384. return -ENODEV;
  385. /* pci_mmap_page_range() expects the same kind of entry as coming
  386. * from /proc/bus/pci/ which is a "user visible" value. If this is
  387. * different from the resource itself, arch will do necessary fixup.
  388. */
  389. pci_resource_to_user(pdev, i, res, &start, &end);
  390. vma->vm_pgoff += start >> PAGE_SHIFT;
  391. mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
  392. return pci_mmap_page_range(pdev, vma, mmap_type, 0);
  393. }
  394. /**
  395. * pci_remove_resource_files - cleanup resource files
  396. * @dev: dev to cleanup
  397. *
  398. * If we created resource files for @dev, remove them from sysfs and
  399. * free their resources.
  400. */
  401. static void
  402. pci_remove_resource_files(struct pci_dev *pdev)
  403. {
  404. int i;
  405. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  406. struct bin_attribute *res_attr;
  407. res_attr = pdev->res_attr[i];
  408. if (res_attr) {
  409. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  410. kfree(res_attr);
  411. }
  412. }
  413. }
  414. /**
  415. * pci_create_resource_files - create resource files in sysfs for @dev
  416. * @dev: dev in question
  417. *
  418. * Walk the resources in @dev creating files for each resource available.
  419. */
  420. static int pci_create_resource_files(struct pci_dev *pdev)
  421. {
  422. int i;
  423. int retval;
  424. /* Expose the PCI resources from this device as files */
  425. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  426. struct bin_attribute *res_attr;
  427. /* skip empty resources */
  428. if (!pci_resource_len(pdev, i))
  429. continue;
  430. /* allocate attribute structure, piggyback attribute name */
  431. res_attr = kzalloc(sizeof(*res_attr) + 10, GFP_ATOMIC);
  432. if (res_attr) {
  433. char *res_attr_name = (char *)(res_attr + 1);
  434. pdev->res_attr[i] = res_attr;
  435. sprintf(res_attr_name, "resource%d", i);
  436. res_attr->attr.name = res_attr_name;
  437. res_attr->attr.mode = S_IRUSR | S_IWUSR;
  438. res_attr->size = pci_resource_len(pdev, i);
  439. res_attr->mmap = pci_mmap_resource;
  440. res_attr->private = &pdev->resource[i];
  441. retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
  442. if (retval) {
  443. pci_remove_resource_files(pdev);
  444. return retval;
  445. }
  446. } else {
  447. return -ENOMEM;
  448. }
  449. }
  450. return 0;
  451. }
  452. #else /* !HAVE_PCI_MMAP */
  453. static inline int pci_create_resource_files(struct pci_dev *dev) { return 0; }
  454. static inline void pci_remove_resource_files(struct pci_dev *dev) { return; }
  455. #endif /* HAVE_PCI_MMAP */
  456. /**
  457. * pci_write_rom - used to enable access to the PCI ROM display
  458. * @kobj: kernel object handle
  459. * @buf: user input
  460. * @off: file offset
  461. * @count: number of byte in input
  462. *
  463. * writing anything except 0 enables it
  464. */
  465. static ssize_t
  466. pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
  467. char *buf, loff_t off, size_t count)
  468. {
  469. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  470. if ((off == 0) && (*buf == '0') && (count == 2))
  471. pdev->rom_attr_enabled = 0;
  472. else
  473. pdev->rom_attr_enabled = 1;
  474. return count;
  475. }
  476. /**
  477. * pci_read_rom - read a PCI ROM
  478. * @kobj: kernel object handle
  479. * @buf: where to put the data we read from the ROM
  480. * @off: file offset
  481. * @count: number of bytes to read
  482. *
  483. * Put @count bytes starting at @off into @buf from the ROM in the PCI
  484. * device corresponding to @kobj.
  485. */
  486. static ssize_t
  487. pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
  488. char *buf, loff_t off, size_t count)
  489. {
  490. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  491. void __iomem *rom;
  492. size_t size;
  493. if (!pdev->rom_attr_enabled)
  494. return -EINVAL;
  495. rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
  496. if (!rom)
  497. return 0;
  498. if (off >= size)
  499. count = 0;
  500. else {
  501. if (off + count > size)
  502. count = size - off;
  503. memcpy_fromio(buf, rom + off, count);
  504. }
  505. pci_unmap_rom(pdev, rom);
  506. return count;
  507. }
  508. static struct bin_attribute pci_config_attr = {
  509. .attr = {
  510. .name = "config",
  511. .mode = S_IRUGO | S_IWUSR,
  512. },
  513. .size = 256,
  514. .read = pci_read_config,
  515. .write = pci_write_config,
  516. };
  517. static struct bin_attribute pcie_config_attr = {
  518. .attr = {
  519. .name = "config",
  520. .mode = S_IRUGO | S_IWUSR,
  521. },
  522. .size = 4096,
  523. .read = pci_read_config,
  524. .write = pci_write_config,
  525. };
  526. int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
  527. {
  528. return 0;
  529. }
  530. int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
  531. {
  532. struct bin_attribute *rom_attr = NULL;
  533. int retval;
  534. if (!sysfs_initialized)
  535. return -EACCES;
  536. if (pdev->cfg_size < 4096)
  537. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
  538. else
  539. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  540. if (retval)
  541. goto err;
  542. retval = pci_create_resource_files(pdev);
  543. if (retval)
  544. goto err_bin_file;
  545. /* If the device has a ROM, try to expose it in sysfs. */
  546. if (pci_resource_len(pdev, PCI_ROM_RESOURCE) ||
  547. (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)) {
  548. rom_attr = kzalloc(sizeof(*rom_attr), GFP_ATOMIC);
  549. if (rom_attr) {
  550. pdev->rom_attr = rom_attr;
  551. rom_attr->size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  552. rom_attr->attr.name = "rom";
  553. rom_attr->attr.mode = S_IRUSR;
  554. rom_attr->read = pci_read_rom;
  555. rom_attr->write = pci_write_rom;
  556. retval = sysfs_create_bin_file(&pdev->dev.kobj, rom_attr);
  557. if (retval)
  558. goto err_rom;
  559. } else {
  560. retval = -ENOMEM;
  561. goto err_resource_files;
  562. }
  563. }
  564. /* add platform-specific attributes */
  565. if (pcibios_add_platform_entries(pdev))
  566. goto err_rom_file;
  567. return 0;
  568. err_rom_file:
  569. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  570. sysfs_remove_bin_file(&pdev->dev.kobj, rom_attr);
  571. err_rom:
  572. kfree(rom_attr);
  573. err_resource_files:
  574. pci_remove_resource_files(pdev);
  575. err_bin_file:
  576. if (pdev->cfg_size < 4096)
  577. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  578. else
  579. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  580. err:
  581. return retval;
  582. }
  583. /**
  584. * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
  585. * @pdev: device whose entries we should free
  586. *
  587. * Cleanup when @pdev is removed from sysfs.
  588. */
  589. void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
  590. {
  591. if (!sysfs_initialized)
  592. return;
  593. if (pdev->cfg_size < 4096)
  594. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  595. else
  596. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  597. pci_remove_resource_files(pdev);
  598. if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
  599. if (pdev->rom_attr) {
  600. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  601. kfree(pdev->rom_attr);
  602. }
  603. }
  604. }
  605. static int __init pci_sysfs_init(void)
  606. {
  607. struct pci_dev *pdev = NULL;
  608. int retval;
  609. sysfs_initialized = 1;
  610. for_each_pci_dev(pdev) {
  611. retval = pci_create_sysfs_dev_files(pdev);
  612. if (retval)
  613. return retval;
  614. }
  615. return 0;
  616. }
  617. late_initcall(pci_sysfs_init);