pci-sysfs.c 29 KB

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