pci-sysfs.c 39 KB

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