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