pci-sysfs.c 39 KB

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