pci-sysfs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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, char *buf, loff_t off, size_t count)
  183. {
  184. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  185. unsigned int size = 64;
  186. loff_t init_off = off;
  187. u8 *data = (u8*) buf;
  188. /* Several chips lock up trying to read undefined config space */
  189. if (capable(CAP_SYS_ADMIN)) {
  190. size = dev->cfg_size;
  191. } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  192. size = 128;
  193. }
  194. if (off > size)
  195. return 0;
  196. if (off + count > size) {
  197. size -= off;
  198. count = size;
  199. } else {
  200. size = count;
  201. }
  202. if ((off & 1) && size) {
  203. u8 val;
  204. pci_user_read_config_byte(dev, off, &val);
  205. data[off - init_off] = val;
  206. off++;
  207. size--;
  208. }
  209. if ((off & 3) && size > 2) {
  210. u16 val;
  211. pci_user_read_config_word(dev, off, &val);
  212. data[off - init_off] = val & 0xff;
  213. data[off - init_off + 1] = (val >> 8) & 0xff;
  214. off += 2;
  215. size -= 2;
  216. }
  217. while (size > 3) {
  218. u32 val;
  219. pci_user_read_config_dword(dev, off, &val);
  220. data[off - init_off] = val & 0xff;
  221. data[off - init_off + 1] = (val >> 8) & 0xff;
  222. data[off - init_off + 2] = (val >> 16) & 0xff;
  223. data[off - init_off + 3] = (val >> 24) & 0xff;
  224. off += 4;
  225. size -= 4;
  226. }
  227. if (size >= 2) {
  228. u16 val;
  229. pci_user_read_config_word(dev, off, &val);
  230. data[off - init_off] = val & 0xff;
  231. data[off - init_off + 1] = (val >> 8) & 0xff;
  232. off += 2;
  233. size -= 2;
  234. }
  235. if (size > 0) {
  236. u8 val;
  237. pci_user_read_config_byte(dev, off, &val);
  238. data[off - init_off] = val;
  239. off++;
  240. --size;
  241. }
  242. return count;
  243. }
  244. static ssize_t
  245. pci_write_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
  246. {
  247. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  248. unsigned int size = count;
  249. loff_t init_off = off;
  250. u8 *data = (u8*) buf;
  251. if (off > dev->cfg_size)
  252. return 0;
  253. if (off + count > dev->cfg_size) {
  254. size = dev->cfg_size - off;
  255. count = size;
  256. }
  257. if ((off & 1) && size) {
  258. pci_user_write_config_byte(dev, off, data[off - init_off]);
  259. off++;
  260. size--;
  261. }
  262. if ((off & 3) && size > 2) {
  263. u16 val = data[off - init_off];
  264. val |= (u16) data[off - init_off + 1] << 8;
  265. pci_user_write_config_word(dev, off, val);
  266. off += 2;
  267. size -= 2;
  268. }
  269. while (size > 3) {
  270. u32 val = data[off - init_off];
  271. val |= (u32) data[off - init_off + 1] << 8;
  272. val |= (u32) data[off - init_off + 2] << 16;
  273. val |= (u32) data[off - init_off + 3] << 24;
  274. pci_user_write_config_dword(dev, off, val);
  275. off += 4;
  276. size -= 4;
  277. }
  278. if (size >= 2) {
  279. u16 val = data[off - init_off];
  280. val |= (u16) data[off - init_off + 1] << 8;
  281. pci_user_write_config_word(dev, off, val);
  282. off += 2;
  283. size -= 2;
  284. }
  285. if (size) {
  286. pci_user_write_config_byte(dev, off, data[off - init_off]);
  287. off++;
  288. --size;
  289. }
  290. return count;
  291. }
  292. #ifdef HAVE_PCI_LEGACY
  293. /**
  294. * pci_read_legacy_io - read byte(s) from legacy I/O port space
  295. * @kobj: kobject corresponding to file to read from
  296. * @buf: buffer to store results
  297. * @off: offset into legacy I/O port space
  298. * @count: number of bytes to read
  299. *
  300. * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  301. * callback routine (pci_legacy_read).
  302. */
  303. ssize_t
  304. pci_read_legacy_io(struct kobject *kobj, char *buf, loff_t off, size_t count)
  305. {
  306. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  307. struct class_device,
  308. kobj));
  309. /* Only support 1, 2 or 4 byte accesses */
  310. if (count != 1 && count != 2 && count != 4)
  311. return -EINVAL;
  312. return pci_legacy_read(bus, off, (u32 *)buf, count);
  313. }
  314. /**
  315. * pci_write_legacy_io - write byte(s) to legacy I/O port space
  316. * @kobj: kobject corresponding to file to read from
  317. * @buf: buffer containing value to be written
  318. * @off: offset into legacy I/O port space
  319. * @count: number of bytes to write
  320. *
  321. * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  322. * callback routine (pci_legacy_write).
  323. */
  324. ssize_t
  325. pci_write_legacy_io(struct kobject *kobj, char *buf, loff_t off, size_t count)
  326. {
  327. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  328. struct class_device,
  329. kobj));
  330. /* Only support 1, 2 or 4 byte accesses */
  331. if (count != 1 && count != 2 && count != 4)
  332. return -EINVAL;
  333. return pci_legacy_write(bus, off, *(u32 *)buf, count);
  334. }
  335. /**
  336. * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
  337. * @kobj: kobject corresponding to device to be mapped
  338. * @attr: struct bin_attribute for this file
  339. * @vma: struct vm_area_struct passed to mmap
  340. *
  341. * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
  342. * legacy memory space (first meg of bus space) into application virtual
  343. * memory space.
  344. */
  345. int
  346. pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
  347. struct vm_area_struct *vma)
  348. {
  349. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  350. struct class_device,
  351. kobj));
  352. return pci_mmap_legacy_page_range(bus, vma);
  353. }
  354. #endif /* HAVE_PCI_LEGACY */
  355. #ifdef HAVE_PCI_MMAP
  356. /**
  357. * pci_mmap_resource - map a PCI resource into user memory space
  358. * @kobj: kobject for mapping
  359. * @attr: struct bin_attribute for the file being mapped
  360. * @vma: struct vm_area_struct passed into the mmap
  361. *
  362. * Use the regular PCI mapping routines to map a PCI resource into userspace.
  363. * FIXME: write combining? maybe automatic for prefetchable regions?
  364. */
  365. static int
  366. pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
  367. struct vm_area_struct *vma)
  368. {
  369. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  370. struct device, kobj));
  371. struct resource *res = (struct resource *)attr->private;
  372. enum pci_mmap_state mmap_type;
  373. resource_size_t start, end;
  374. int i;
  375. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  376. if (res == &pdev->resource[i])
  377. break;
  378. if (i >= PCI_ROM_RESOURCE)
  379. return -ENODEV;
  380. /* pci_mmap_page_range() expects the same kind of entry as coming
  381. * from /proc/bus/pci/ which is a "user visible" value. If this is
  382. * different from the resource itself, arch will do necessary fixup.
  383. */
  384. pci_resource_to_user(pdev, i, res, &start, &end);
  385. vma->vm_pgoff += start >> PAGE_SHIFT;
  386. mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
  387. return pci_mmap_page_range(pdev, vma, mmap_type, 0);
  388. }
  389. /**
  390. * pci_remove_resource_files - cleanup resource files
  391. * @dev: dev to cleanup
  392. *
  393. * If we created resource files for @dev, remove them from sysfs and
  394. * free their resources.
  395. */
  396. static void
  397. pci_remove_resource_files(struct pci_dev *pdev)
  398. {
  399. int i;
  400. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  401. struct bin_attribute *res_attr;
  402. res_attr = pdev->res_attr[i];
  403. if (res_attr) {
  404. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  405. kfree(res_attr);
  406. }
  407. }
  408. }
  409. /**
  410. * pci_create_resource_files - create resource files in sysfs for @dev
  411. * @dev: dev in question
  412. *
  413. * Walk the resources in @dev creating files for each resource available.
  414. */
  415. static int pci_create_resource_files(struct pci_dev *pdev)
  416. {
  417. int i;
  418. int retval;
  419. /* Expose the PCI resources from this device as files */
  420. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  421. struct bin_attribute *res_attr;
  422. /* skip empty resources */
  423. if (!pci_resource_len(pdev, i))
  424. continue;
  425. /* allocate attribute structure, piggyback attribute name */
  426. res_attr = kzalloc(sizeof(*res_attr) + 10, GFP_ATOMIC);
  427. if (res_attr) {
  428. char *res_attr_name = (char *)(res_attr + 1);
  429. pdev->res_attr[i] = res_attr;
  430. sprintf(res_attr_name, "resource%d", i);
  431. res_attr->attr.name = res_attr_name;
  432. res_attr->attr.mode = S_IRUSR | S_IWUSR;
  433. res_attr->attr.owner = THIS_MODULE;
  434. res_attr->size = pci_resource_len(pdev, i);
  435. res_attr->mmap = pci_mmap_resource;
  436. res_attr->private = &pdev->resource[i];
  437. retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
  438. if (retval) {
  439. pci_remove_resource_files(pdev);
  440. return retval;
  441. }
  442. } else {
  443. return -ENOMEM;
  444. }
  445. }
  446. return 0;
  447. }
  448. #else /* !HAVE_PCI_MMAP */
  449. static inline int pci_create_resource_files(struct pci_dev *dev) { return 0; }
  450. static inline void pci_remove_resource_files(struct pci_dev *dev) { return; }
  451. #endif /* HAVE_PCI_MMAP */
  452. /**
  453. * pci_write_rom - used to enable access to the PCI ROM display
  454. * @kobj: kernel object handle
  455. * @buf: user input
  456. * @off: file offset
  457. * @count: number of byte in input
  458. *
  459. * writing anything except 0 enables it
  460. */
  461. static ssize_t
  462. pci_write_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
  463. {
  464. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  465. if ((off == 0) && (*buf == '0') && (count == 2))
  466. pdev->rom_attr_enabled = 0;
  467. else
  468. pdev->rom_attr_enabled = 1;
  469. return count;
  470. }
  471. /**
  472. * pci_read_rom - read a PCI ROM
  473. * @kobj: kernel object handle
  474. * @buf: where to put the data we read from the ROM
  475. * @off: file offset
  476. * @count: number of bytes to read
  477. *
  478. * Put @count bytes starting at @off into @buf from the ROM in the PCI
  479. * device corresponding to @kobj.
  480. */
  481. static ssize_t
  482. pci_read_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
  483. {
  484. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  485. void __iomem *rom;
  486. size_t size;
  487. if (!pdev->rom_attr_enabled)
  488. return -EINVAL;
  489. rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
  490. if (!rom)
  491. return 0;
  492. if (off >= size)
  493. count = 0;
  494. else {
  495. if (off + count > size)
  496. count = size - off;
  497. memcpy_fromio(buf, rom + off, count);
  498. }
  499. pci_unmap_rom(pdev, rom);
  500. return count;
  501. }
  502. static struct bin_attribute pci_config_attr = {
  503. .attr = {
  504. .name = "config",
  505. .mode = S_IRUGO | S_IWUSR,
  506. .owner = THIS_MODULE,
  507. },
  508. .size = 256,
  509. .read = pci_read_config,
  510. .write = pci_write_config,
  511. };
  512. static struct bin_attribute pcie_config_attr = {
  513. .attr = {
  514. .name = "config",
  515. .mode = S_IRUGO | S_IWUSR,
  516. .owner = THIS_MODULE,
  517. },
  518. .size = 4096,
  519. .read = pci_read_config,
  520. .write = pci_write_config,
  521. };
  522. int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
  523. {
  524. struct bin_attribute *rom_attr = NULL;
  525. int retval;
  526. if (!sysfs_initialized)
  527. return -EACCES;
  528. if (pdev->cfg_size < 4096)
  529. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
  530. else
  531. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  532. if (retval)
  533. goto err;
  534. retval = pci_create_resource_files(pdev);
  535. if (retval)
  536. goto err_bin_file;
  537. /* If the device has a ROM, try to expose it in sysfs. */
  538. if (pci_resource_len(pdev, PCI_ROM_RESOURCE) ||
  539. (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)) {
  540. rom_attr = kzalloc(sizeof(*rom_attr), GFP_ATOMIC);
  541. if (rom_attr) {
  542. pdev->rom_attr = rom_attr;
  543. rom_attr->size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  544. rom_attr->attr.name = "rom";
  545. rom_attr->attr.mode = S_IRUSR;
  546. rom_attr->attr.owner = THIS_MODULE;
  547. rom_attr->read = pci_read_rom;
  548. rom_attr->write = pci_write_rom;
  549. retval = sysfs_create_bin_file(&pdev->dev.kobj, rom_attr);
  550. if (retval)
  551. goto err_rom;
  552. } else {
  553. retval = -ENOMEM;
  554. goto err_resource_files;
  555. }
  556. }
  557. /* add platform-specific attributes */
  558. pcibios_add_platform_entries(pdev);
  559. return 0;
  560. err_rom:
  561. kfree(rom_attr);
  562. err_resource_files:
  563. pci_remove_resource_files(pdev);
  564. err_bin_file:
  565. if (pdev->cfg_size < 4096)
  566. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  567. else
  568. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  569. err:
  570. return retval;
  571. }
  572. /**
  573. * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
  574. * @pdev: device whose entries we should free
  575. *
  576. * Cleanup when @pdev is removed from sysfs.
  577. */
  578. void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
  579. {
  580. if (!sysfs_initialized)
  581. return;
  582. if (pdev->cfg_size < 4096)
  583. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  584. else
  585. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  586. pci_remove_resource_files(pdev);
  587. if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
  588. if (pdev->rom_attr) {
  589. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  590. kfree(pdev->rom_attr);
  591. }
  592. }
  593. }
  594. static int __init pci_sysfs_init(void)
  595. {
  596. struct pci_dev *pdev = NULL;
  597. int retval;
  598. sysfs_initialized = 1;
  599. for_each_pci_dev(pdev) {
  600. retval = pci_create_sysfs_dev_files(pdev);
  601. if (retval)
  602. return retval;
  603. }
  604. return 0;
  605. }
  606. late_initcall(pci_sysfs_init);