pci-sysfs.c 38 KB

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