pci-sysfs.c 18 KB

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