pci-sysfs.c 38 KB

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