pci-sysfs.c 18 KB

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