pci-sysfs.c 30 KB

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