pci-sysfs.c 30 KB

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