pci-sysfs.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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_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. pci_rescan_bus(bus);
  310. mutex_unlock(&pci_remove_rescan_mutex);
  311. }
  312. return count;
  313. }
  314. #endif
  315. struct device_attribute pci_dev_attrs[] = {
  316. __ATTR_RO(resource),
  317. __ATTR_RO(vendor),
  318. __ATTR_RO(device),
  319. __ATTR_RO(subsystem_vendor),
  320. __ATTR_RO(subsystem_device),
  321. __ATTR_RO(class),
  322. __ATTR_RO(irq),
  323. __ATTR_RO(local_cpus),
  324. __ATTR_RO(local_cpulist),
  325. __ATTR_RO(modalias),
  326. #ifdef CONFIG_NUMA
  327. __ATTR_RO(numa_node),
  328. #endif
  329. __ATTR_RO(dma_mask_bits),
  330. __ATTR_RO(consistent_dma_mask_bits),
  331. __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
  332. __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
  333. broken_parity_status_show,broken_parity_status_store),
  334. __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
  335. #ifdef CONFIG_HOTPLUG
  336. __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
  337. __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
  338. #endif
  339. __ATTR_NULL,
  340. };
  341. struct device_attribute pcibus_dev_attrs[] = {
  342. #ifdef CONFIG_HOTPLUG
  343. __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store),
  344. #endif
  345. __ATTR(cpuaffinity, S_IRUGO, pci_bus_show_cpumaskaffinity, NULL),
  346. __ATTR(cpulistaffinity, S_IRUGO, pci_bus_show_cpulistaffinity, NULL),
  347. __ATTR_NULL,
  348. };
  349. static ssize_t
  350. boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
  351. {
  352. struct pci_dev *pdev = to_pci_dev(dev);
  353. return sprintf(buf, "%u\n",
  354. !!(pdev->resource[PCI_ROM_RESOURCE].flags &
  355. IORESOURCE_ROM_SHADOW));
  356. }
  357. struct device_attribute vga_attr = __ATTR_RO(boot_vga);
  358. static ssize_t
  359. pci_read_config(struct file *filp, struct kobject *kobj,
  360. struct bin_attribute *bin_attr,
  361. char *buf, loff_t off, size_t count)
  362. {
  363. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  364. unsigned int size = 64;
  365. loff_t init_off = off;
  366. u8 *data = (u8*) buf;
  367. /* Several chips lock up trying to read undefined config space */
  368. if (security_capable(&init_user_ns, filp->f_cred, CAP_SYS_ADMIN) == 0) {
  369. size = dev->cfg_size;
  370. } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  371. size = 128;
  372. }
  373. if (off > size)
  374. return 0;
  375. if (off + count > size) {
  376. size -= off;
  377. count = size;
  378. } else {
  379. size = count;
  380. }
  381. if ((off & 1) && size) {
  382. u8 val;
  383. pci_user_read_config_byte(dev, off, &val);
  384. data[off - init_off] = val;
  385. off++;
  386. size--;
  387. }
  388. if ((off & 3) && size > 2) {
  389. u16 val;
  390. pci_user_read_config_word(dev, off, &val);
  391. data[off - init_off] = val & 0xff;
  392. data[off - init_off + 1] = (val >> 8) & 0xff;
  393. off += 2;
  394. size -= 2;
  395. }
  396. while (size > 3) {
  397. u32 val;
  398. pci_user_read_config_dword(dev, off, &val);
  399. data[off - init_off] = val & 0xff;
  400. data[off - init_off + 1] = (val >> 8) & 0xff;
  401. data[off - init_off + 2] = (val >> 16) & 0xff;
  402. data[off - init_off + 3] = (val >> 24) & 0xff;
  403. off += 4;
  404. size -= 4;
  405. }
  406. if (size >= 2) {
  407. u16 val;
  408. pci_user_read_config_word(dev, off, &val);
  409. data[off - init_off] = val & 0xff;
  410. data[off - init_off + 1] = (val >> 8) & 0xff;
  411. off += 2;
  412. size -= 2;
  413. }
  414. if (size > 0) {
  415. u8 val;
  416. pci_user_read_config_byte(dev, off, &val);
  417. data[off - init_off] = val;
  418. off++;
  419. --size;
  420. }
  421. return count;
  422. }
  423. static ssize_t
  424. pci_write_config(struct file* filp, struct kobject *kobj,
  425. struct bin_attribute *bin_attr,
  426. char *buf, loff_t off, size_t count)
  427. {
  428. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  429. unsigned int size = count;
  430. loff_t init_off = off;
  431. u8 *data = (u8*) buf;
  432. if (off > dev->cfg_size)
  433. return 0;
  434. if (off + count > dev->cfg_size) {
  435. size = dev->cfg_size - off;
  436. count = size;
  437. }
  438. if ((off & 1) && size) {
  439. pci_user_write_config_byte(dev, off, data[off - init_off]);
  440. off++;
  441. size--;
  442. }
  443. if ((off & 3) && size > 2) {
  444. u16 val = data[off - init_off];
  445. val |= (u16) data[off - init_off + 1] << 8;
  446. pci_user_write_config_word(dev, off, val);
  447. off += 2;
  448. size -= 2;
  449. }
  450. while (size > 3) {
  451. u32 val = data[off - init_off];
  452. val |= (u32) data[off - init_off + 1] << 8;
  453. val |= (u32) data[off - init_off + 2] << 16;
  454. val |= (u32) data[off - init_off + 3] << 24;
  455. pci_user_write_config_dword(dev, off, val);
  456. off += 4;
  457. size -= 4;
  458. }
  459. if (size >= 2) {
  460. u16 val = data[off - init_off];
  461. val |= (u16) data[off - init_off + 1] << 8;
  462. pci_user_write_config_word(dev, off, val);
  463. off += 2;
  464. size -= 2;
  465. }
  466. if (size) {
  467. pci_user_write_config_byte(dev, off, data[off - init_off]);
  468. off++;
  469. --size;
  470. }
  471. return count;
  472. }
  473. static ssize_t
  474. read_vpd_attr(struct file *filp, struct kobject *kobj,
  475. struct bin_attribute *bin_attr,
  476. char *buf, loff_t off, size_t count)
  477. {
  478. struct pci_dev *dev =
  479. to_pci_dev(container_of(kobj, struct device, kobj));
  480. if (off > bin_attr->size)
  481. count = 0;
  482. else if (count > bin_attr->size - off)
  483. count = bin_attr->size - off;
  484. return pci_read_vpd(dev, off, count, buf);
  485. }
  486. static ssize_t
  487. write_vpd_attr(struct file *filp, struct kobject *kobj,
  488. struct bin_attribute *bin_attr,
  489. char *buf, loff_t off, size_t count)
  490. {
  491. struct pci_dev *dev =
  492. to_pci_dev(container_of(kobj, struct device, kobj));
  493. if (off > bin_attr->size)
  494. count = 0;
  495. else if (count > bin_attr->size - off)
  496. count = bin_attr->size - off;
  497. return pci_write_vpd(dev, off, count, buf);
  498. }
  499. #ifdef HAVE_PCI_LEGACY
  500. /**
  501. * pci_read_legacy_io - read byte(s) from legacy I/O port space
  502. * @filp: open sysfs file
  503. * @kobj: kobject corresponding to file to read from
  504. * @bin_attr: struct bin_attribute for this file
  505. * @buf: buffer to store results
  506. * @off: offset into legacy I/O port space
  507. * @count: number of bytes to read
  508. *
  509. * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  510. * callback routine (pci_legacy_read).
  511. */
  512. static ssize_t
  513. pci_read_legacy_io(struct file *filp, struct kobject *kobj,
  514. struct bin_attribute *bin_attr,
  515. char *buf, loff_t off, size_t count)
  516. {
  517. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  518. struct device,
  519. kobj));
  520. /* Only support 1, 2 or 4 byte accesses */
  521. if (count != 1 && count != 2 && count != 4)
  522. return -EINVAL;
  523. return pci_legacy_read(bus, off, (u32 *)buf, count);
  524. }
  525. /**
  526. * pci_write_legacy_io - write byte(s) to legacy I/O port space
  527. * @filp: open sysfs file
  528. * @kobj: kobject corresponding to file to read from
  529. * @bin_attr: struct bin_attribute for this file
  530. * @buf: buffer containing value to be written
  531. * @off: offset into legacy I/O port space
  532. * @count: number of bytes to write
  533. *
  534. * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  535. * callback routine (pci_legacy_write).
  536. */
  537. static ssize_t
  538. pci_write_legacy_io(struct file *filp, struct kobject *kobj,
  539. struct bin_attribute *bin_attr,
  540. char *buf, loff_t off, size_t count)
  541. {
  542. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  543. struct device,
  544. kobj));
  545. /* Only support 1, 2 or 4 byte accesses */
  546. if (count != 1 && count != 2 && count != 4)
  547. return -EINVAL;
  548. return pci_legacy_write(bus, off, *(u32 *)buf, count);
  549. }
  550. /**
  551. * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
  552. * @filp: open sysfs file
  553. * @kobj: kobject corresponding to device to be mapped
  554. * @attr: struct bin_attribute for this file
  555. * @vma: struct vm_area_struct passed to mmap
  556. *
  557. * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
  558. * legacy memory space (first meg of bus space) into application virtual
  559. * memory space.
  560. */
  561. static int
  562. pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
  563. struct bin_attribute *attr,
  564. struct vm_area_struct *vma)
  565. {
  566. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  567. struct device,
  568. kobj));
  569. return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
  570. }
  571. /**
  572. * pci_mmap_legacy_io - map legacy PCI IO into user memory space
  573. * @filp: open sysfs file
  574. * @kobj: kobject corresponding to device to be mapped
  575. * @attr: struct bin_attribute for this file
  576. * @vma: struct vm_area_struct passed to mmap
  577. *
  578. * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
  579. * legacy IO space (first meg of bus space) into application virtual
  580. * memory space. Returns -ENOSYS if the operation isn't supported
  581. */
  582. static int
  583. pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
  584. struct bin_attribute *attr,
  585. struct vm_area_struct *vma)
  586. {
  587. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  588. struct device,
  589. kobj));
  590. return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
  591. }
  592. /**
  593. * pci_adjust_legacy_attr - adjustment of legacy file attributes
  594. * @b: bus to create files under
  595. * @mmap_type: I/O port or memory
  596. *
  597. * Stub implementation. Can be overridden by arch if necessary.
  598. */
  599. void __weak
  600. pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
  601. {
  602. return;
  603. }
  604. /**
  605. * pci_create_legacy_files - create legacy I/O port and memory files
  606. * @b: bus to create files under
  607. *
  608. * Some platforms allow access to legacy I/O port and ISA memory space on
  609. * a per-bus basis. This routine creates the files and ties them into
  610. * their associated read, write and mmap files from pci-sysfs.c
  611. *
  612. * On error unwind, but don't propagate the error to the caller
  613. * as it is ok to set up the PCI bus without these files.
  614. */
  615. void pci_create_legacy_files(struct pci_bus *b)
  616. {
  617. int error;
  618. b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
  619. GFP_ATOMIC);
  620. if (!b->legacy_io)
  621. goto kzalloc_err;
  622. sysfs_bin_attr_init(b->legacy_io);
  623. b->legacy_io->attr.name = "legacy_io";
  624. b->legacy_io->size = 0xffff;
  625. b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
  626. b->legacy_io->read = pci_read_legacy_io;
  627. b->legacy_io->write = pci_write_legacy_io;
  628. b->legacy_io->mmap = pci_mmap_legacy_io;
  629. pci_adjust_legacy_attr(b, pci_mmap_io);
  630. error = device_create_bin_file(&b->dev, b->legacy_io);
  631. if (error)
  632. goto legacy_io_err;
  633. /* Allocated above after the legacy_io struct */
  634. b->legacy_mem = b->legacy_io + 1;
  635. sysfs_bin_attr_init(b->legacy_mem);
  636. b->legacy_mem->attr.name = "legacy_mem";
  637. b->legacy_mem->size = 1024*1024;
  638. b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
  639. b->legacy_mem->mmap = pci_mmap_legacy_mem;
  640. pci_adjust_legacy_attr(b, pci_mmap_mem);
  641. error = device_create_bin_file(&b->dev, b->legacy_mem);
  642. if (error)
  643. goto legacy_mem_err;
  644. return;
  645. legacy_mem_err:
  646. device_remove_bin_file(&b->dev, b->legacy_io);
  647. legacy_io_err:
  648. kfree(b->legacy_io);
  649. b->legacy_io = NULL;
  650. kzalloc_err:
  651. printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
  652. "and ISA memory resources to sysfs\n");
  653. return;
  654. }
  655. void pci_remove_legacy_files(struct pci_bus *b)
  656. {
  657. if (b->legacy_io) {
  658. device_remove_bin_file(&b->dev, b->legacy_io);
  659. device_remove_bin_file(&b->dev, b->legacy_mem);
  660. kfree(b->legacy_io); /* both are allocated here */
  661. }
  662. }
  663. #endif /* HAVE_PCI_LEGACY */
  664. #ifdef HAVE_PCI_MMAP
  665. int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
  666. enum pci_mmap_api mmap_api)
  667. {
  668. unsigned long nr, start, size, pci_start;
  669. if (pci_resource_len(pdev, resno) == 0)
  670. return 0;
  671. nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  672. start = vma->vm_pgoff;
  673. size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
  674. pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
  675. pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
  676. if (start >= pci_start && start < pci_start + size &&
  677. start + nr <= pci_start + size)
  678. return 1;
  679. return 0;
  680. }
  681. /**
  682. * pci_mmap_resource - map a PCI resource into user memory space
  683. * @kobj: kobject for mapping
  684. * @attr: struct bin_attribute for the file being mapped
  685. * @vma: struct vm_area_struct passed into the mmap
  686. * @write_combine: 1 for write_combine mapping
  687. *
  688. * Use the regular PCI mapping routines to map a PCI resource into userspace.
  689. */
  690. static int
  691. pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
  692. struct vm_area_struct *vma, int write_combine)
  693. {
  694. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  695. struct device, kobj));
  696. struct resource *res = attr->private;
  697. enum pci_mmap_state mmap_type;
  698. resource_size_t start, end;
  699. int i;
  700. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  701. if (res == &pdev->resource[i])
  702. break;
  703. if (i >= PCI_ROM_RESOURCE)
  704. return -ENODEV;
  705. if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
  706. WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
  707. "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
  708. current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
  709. pci_name(pdev), i,
  710. (u64)pci_resource_start(pdev, i),
  711. (u64)pci_resource_len(pdev, i));
  712. return -EINVAL;
  713. }
  714. /* pci_mmap_page_range() expects the same kind of entry as coming
  715. * from /proc/bus/pci/ which is a "user visible" value. If this is
  716. * different from the resource itself, arch will do necessary fixup.
  717. */
  718. pci_resource_to_user(pdev, i, res, &start, &end);
  719. vma->vm_pgoff += start >> PAGE_SHIFT;
  720. mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
  721. if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
  722. return -EINVAL;
  723. return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
  724. }
  725. static int
  726. pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
  727. struct bin_attribute *attr,
  728. struct vm_area_struct *vma)
  729. {
  730. return pci_mmap_resource(kobj, attr, vma, 0);
  731. }
  732. static int
  733. pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
  734. struct bin_attribute *attr,
  735. struct vm_area_struct *vma)
  736. {
  737. return pci_mmap_resource(kobj, attr, vma, 1);
  738. }
  739. static ssize_t
  740. pci_resource_io(struct file *filp, struct kobject *kobj,
  741. struct bin_attribute *attr, char *buf,
  742. loff_t off, size_t count, bool write)
  743. {
  744. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  745. struct device, kobj));
  746. struct resource *res = attr->private;
  747. unsigned long port = off;
  748. int i;
  749. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  750. if (res == &pdev->resource[i])
  751. break;
  752. if (i >= PCI_ROM_RESOURCE)
  753. return -ENODEV;
  754. port += pci_resource_start(pdev, i);
  755. if (port > pci_resource_end(pdev, i))
  756. return 0;
  757. if (port + count - 1 > pci_resource_end(pdev, i))
  758. return -EINVAL;
  759. switch (count) {
  760. case 1:
  761. if (write)
  762. outb(*(u8 *)buf, port);
  763. else
  764. *(u8 *)buf = inb(port);
  765. return 1;
  766. case 2:
  767. if (write)
  768. outw(*(u16 *)buf, port);
  769. else
  770. *(u16 *)buf = inw(port);
  771. return 2;
  772. case 4:
  773. if (write)
  774. outl(*(u32 *)buf, port);
  775. else
  776. *(u32 *)buf = inl(port);
  777. return 4;
  778. }
  779. return -EINVAL;
  780. }
  781. static ssize_t
  782. pci_read_resource_io(struct file *filp, struct kobject *kobj,
  783. struct bin_attribute *attr, char *buf,
  784. loff_t off, size_t count)
  785. {
  786. return pci_resource_io(filp, kobj, attr, buf, off, count, false);
  787. }
  788. static ssize_t
  789. pci_write_resource_io(struct file *filp, struct kobject *kobj,
  790. struct bin_attribute *attr, char *buf,
  791. loff_t off, size_t count)
  792. {
  793. return pci_resource_io(filp, kobj, attr, buf, off, count, true);
  794. }
  795. /**
  796. * pci_remove_resource_files - cleanup resource files
  797. * @pdev: dev to cleanup
  798. *
  799. * If we created resource files for @pdev, remove them from sysfs and
  800. * free their resources.
  801. */
  802. static void
  803. pci_remove_resource_files(struct pci_dev *pdev)
  804. {
  805. int i;
  806. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  807. struct bin_attribute *res_attr;
  808. res_attr = pdev->res_attr[i];
  809. if (res_attr) {
  810. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  811. kfree(res_attr);
  812. }
  813. res_attr = pdev->res_attr_wc[i];
  814. if (res_attr) {
  815. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  816. kfree(res_attr);
  817. }
  818. }
  819. }
  820. static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
  821. {
  822. /* allocate attribute structure, piggyback attribute name */
  823. int name_len = write_combine ? 13 : 10;
  824. struct bin_attribute *res_attr;
  825. int retval;
  826. res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
  827. if (res_attr) {
  828. char *res_attr_name = (char *)(res_attr + 1);
  829. sysfs_bin_attr_init(res_attr);
  830. if (write_combine) {
  831. pdev->res_attr_wc[num] = res_attr;
  832. sprintf(res_attr_name, "resource%d_wc", num);
  833. res_attr->mmap = pci_mmap_resource_wc;
  834. } else {
  835. pdev->res_attr[num] = res_attr;
  836. sprintf(res_attr_name, "resource%d", num);
  837. res_attr->mmap = pci_mmap_resource_uc;
  838. }
  839. if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
  840. res_attr->read = pci_read_resource_io;
  841. res_attr->write = pci_write_resource_io;
  842. }
  843. res_attr->attr.name = res_attr_name;
  844. res_attr->attr.mode = S_IRUSR | S_IWUSR;
  845. res_attr->size = pci_resource_len(pdev, num);
  846. res_attr->private = &pdev->resource[num];
  847. retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
  848. } else
  849. retval = -ENOMEM;
  850. return retval;
  851. }
  852. /**
  853. * pci_create_resource_files - create resource files in sysfs for @dev
  854. * @pdev: dev in question
  855. *
  856. * Walk the resources in @pdev creating files for each resource available.
  857. */
  858. static int pci_create_resource_files(struct pci_dev *pdev)
  859. {
  860. int i;
  861. int retval;
  862. /* Expose the PCI resources from this device as files */
  863. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  864. /* skip empty resources */
  865. if (!pci_resource_len(pdev, i))
  866. continue;
  867. retval = pci_create_attr(pdev, i, 0);
  868. /* for prefetchable resources, create a WC mappable file */
  869. if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
  870. retval = pci_create_attr(pdev, i, 1);
  871. if (retval) {
  872. pci_remove_resource_files(pdev);
  873. return retval;
  874. }
  875. }
  876. return 0;
  877. }
  878. #else /* !HAVE_PCI_MMAP */
  879. int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
  880. void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
  881. #endif /* HAVE_PCI_MMAP */
  882. /**
  883. * pci_write_rom - used to enable access to the PCI ROM display
  884. * @filp: sysfs file
  885. * @kobj: kernel object handle
  886. * @bin_attr: struct bin_attribute for this file
  887. * @buf: user input
  888. * @off: file offset
  889. * @count: number of byte in input
  890. *
  891. * writing anything except 0 enables it
  892. */
  893. static ssize_t
  894. pci_write_rom(struct file *filp, struct kobject *kobj,
  895. struct bin_attribute *bin_attr,
  896. char *buf, loff_t off, size_t count)
  897. {
  898. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  899. if ((off == 0) && (*buf == '0') && (count == 2))
  900. pdev->rom_attr_enabled = 0;
  901. else
  902. pdev->rom_attr_enabled = 1;
  903. return count;
  904. }
  905. /**
  906. * pci_read_rom - read a PCI ROM
  907. * @filp: sysfs file
  908. * @kobj: kernel object handle
  909. * @bin_attr: struct bin_attribute for this file
  910. * @buf: where to put the data we read from the ROM
  911. * @off: file offset
  912. * @count: number of bytes to read
  913. *
  914. * Put @count bytes starting at @off into @buf from the ROM in the PCI
  915. * device corresponding to @kobj.
  916. */
  917. static ssize_t
  918. pci_read_rom(struct file *filp, struct kobject *kobj,
  919. struct bin_attribute *bin_attr,
  920. char *buf, loff_t off, size_t count)
  921. {
  922. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  923. void __iomem *rom;
  924. size_t size;
  925. if (!pdev->rom_attr_enabled)
  926. return -EINVAL;
  927. rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
  928. if (!rom || !size)
  929. return -EIO;
  930. if (off >= size)
  931. count = 0;
  932. else {
  933. if (off + count > size)
  934. count = size - off;
  935. memcpy_fromio(buf, rom + off, count);
  936. }
  937. pci_unmap_rom(pdev, rom);
  938. return count;
  939. }
  940. static struct bin_attribute pci_config_attr = {
  941. .attr = {
  942. .name = "config",
  943. .mode = S_IRUGO | S_IWUSR,
  944. },
  945. .size = PCI_CFG_SPACE_SIZE,
  946. .read = pci_read_config,
  947. .write = pci_write_config,
  948. };
  949. static struct bin_attribute pcie_config_attr = {
  950. .attr = {
  951. .name = "config",
  952. .mode = S_IRUGO | S_IWUSR,
  953. },
  954. .size = PCI_CFG_SPACE_EXP_SIZE,
  955. .read = pci_read_config,
  956. .write = pci_write_config,
  957. };
  958. int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
  959. {
  960. return 0;
  961. }
  962. static ssize_t reset_store(struct device *dev,
  963. struct device_attribute *attr, const char *buf,
  964. size_t count)
  965. {
  966. struct pci_dev *pdev = to_pci_dev(dev);
  967. unsigned long val;
  968. ssize_t result = strict_strtoul(buf, 0, &val);
  969. if (result < 0)
  970. return result;
  971. if (val != 1)
  972. return -EINVAL;
  973. result = pci_reset_function(pdev);
  974. if (result < 0)
  975. return result;
  976. return count;
  977. }
  978. static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
  979. static int pci_create_capabilities_sysfs(struct pci_dev *dev)
  980. {
  981. int retval;
  982. struct bin_attribute *attr;
  983. /* If the device has VPD, try to expose it in sysfs. */
  984. if (dev->vpd) {
  985. attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
  986. if (!attr)
  987. return -ENOMEM;
  988. sysfs_bin_attr_init(attr);
  989. attr->size = dev->vpd->len;
  990. attr->attr.name = "vpd";
  991. attr->attr.mode = S_IRUSR | S_IWUSR;
  992. attr->read = read_vpd_attr;
  993. attr->write = write_vpd_attr;
  994. retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
  995. if (retval) {
  996. kfree(attr);
  997. return retval;
  998. }
  999. dev->vpd->attr = attr;
  1000. }
  1001. /* Active State Power Management */
  1002. pcie_aspm_create_sysfs_dev_files(dev);
  1003. if (!pci_probe_reset_function(dev)) {
  1004. retval = device_create_file(&dev->dev, &reset_attr);
  1005. if (retval)
  1006. goto error;
  1007. dev->reset_fn = 1;
  1008. }
  1009. return 0;
  1010. error:
  1011. pcie_aspm_remove_sysfs_dev_files(dev);
  1012. if (dev->vpd && dev->vpd->attr) {
  1013. sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
  1014. kfree(dev->vpd->attr);
  1015. }
  1016. return retval;
  1017. }
  1018. int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
  1019. {
  1020. int retval;
  1021. int rom_size = 0;
  1022. struct bin_attribute *attr;
  1023. if (!sysfs_initialized)
  1024. return -EACCES;
  1025. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  1026. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
  1027. else
  1028. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  1029. if (retval)
  1030. goto err;
  1031. retval = pci_create_resource_files(pdev);
  1032. if (retval)
  1033. goto err_config_file;
  1034. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  1035. rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  1036. else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
  1037. rom_size = 0x20000;
  1038. /* If the device has a ROM, try to expose it in sysfs. */
  1039. if (rom_size) {
  1040. attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
  1041. if (!attr) {
  1042. retval = -ENOMEM;
  1043. goto err_resource_files;
  1044. }
  1045. sysfs_bin_attr_init(attr);
  1046. attr->size = rom_size;
  1047. attr->attr.name = "rom";
  1048. attr->attr.mode = S_IRUSR | S_IWUSR;
  1049. attr->read = pci_read_rom;
  1050. attr->write = pci_write_rom;
  1051. retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
  1052. if (retval) {
  1053. kfree(attr);
  1054. goto err_resource_files;
  1055. }
  1056. pdev->rom_attr = attr;
  1057. }
  1058. if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
  1059. retval = device_create_file(&pdev->dev, &vga_attr);
  1060. if (retval)
  1061. goto err_rom_file;
  1062. }
  1063. /* add platform-specific attributes */
  1064. retval = pcibios_add_platform_entries(pdev);
  1065. if (retval)
  1066. goto err_vga_file;
  1067. /* add sysfs entries for various capabilities */
  1068. retval = pci_create_capabilities_sysfs(pdev);
  1069. if (retval)
  1070. goto err_vga_file;
  1071. pci_create_firmware_label_files(pdev);
  1072. return 0;
  1073. err_vga_file:
  1074. if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
  1075. device_remove_file(&pdev->dev, &vga_attr);
  1076. err_rom_file:
  1077. if (rom_size) {
  1078. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  1079. kfree(pdev->rom_attr);
  1080. pdev->rom_attr = NULL;
  1081. }
  1082. err_resource_files:
  1083. pci_remove_resource_files(pdev);
  1084. err_config_file:
  1085. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  1086. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  1087. else
  1088. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  1089. err:
  1090. return retval;
  1091. }
  1092. static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
  1093. {
  1094. if (dev->vpd && dev->vpd->attr) {
  1095. sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
  1096. kfree(dev->vpd->attr);
  1097. }
  1098. pcie_aspm_remove_sysfs_dev_files(dev);
  1099. if (dev->reset_fn) {
  1100. device_remove_file(&dev->dev, &reset_attr);
  1101. dev->reset_fn = 0;
  1102. }
  1103. }
  1104. /**
  1105. * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
  1106. * @pdev: device whose entries we should free
  1107. *
  1108. * Cleanup when @pdev is removed from sysfs.
  1109. */
  1110. void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
  1111. {
  1112. int rom_size = 0;
  1113. if (!sysfs_initialized)
  1114. return;
  1115. pci_remove_capabilities_sysfs(pdev);
  1116. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  1117. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  1118. else
  1119. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  1120. pci_remove_resource_files(pdev);
  1121. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  1122. rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  1123. else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
  1124. rom_size = 0x20000;
  1125. if (rom_size && pdev->rom_attr) {
  1126. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  1127. kfree(pdev->rom_attr);
  1128. }
  1129. pci_remove_firmware_label_files(pdev);
  1130. }
  1131. static int __init pci_sysfs_init(void)
  1132. {
  1133. struct pci_dev *pdev = NULL;
  1134. int retval;
  1135. sysfs_initialized = 1;
  1136. for_each_pci_dev(pdev) {
  1137. retval = pci_create_sysfs_dev_files(pdev);
  1138. if (retval) {
  1139. pci_dev_put(pdev);
  1140. return retval;
  1141. }
  1142. }
  1143. return 0;
  1144. }
  1145. late_initcall(pci_sysfs_init);