pci-sysfs.c 33 KB

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