pci-sysfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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/pci.h>
  18. #include <linux/stat.h>
  19. #include <linux/topology.h>
  20. #include <linux/mm.h>
  21. #include "pci.h"
  22. static int sysfs_initialized; /* = 0 */
  23. /* show configuration fields */
  24. #define pci_config_attr(field, format_string) \
  25. static ssize_t \
  26. field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  27. { \
  28. struct pci_dev *pdev; \
  29. \
  30. pdev = to_pci_dev (dev); \
  31. return sprintf (buf, format_string, pdev->field); \
  32. }
  33. pci_config_attr(vendor, "0x%04x\n");
  34. pci_config_attr(device, "0x%04x\n");
  35. pci_config_attr(subsystem_vendor, "0x%04x\n");
  36. pci_config_attr(subsystem_device, "0x%04x\n");
  37. pci_config_attr(class, "0x%06x\n");
  38. pci_config_attr(irq, "%u\n");
  39. pci_config_attr(is_enabled, "%u\n");
  40. static ssize_t broken_parity_status_show(struct device *dev,
  41. struct device_attribute *attr,
  42. char *buf)
  43. {
  44. struct pci_dev *pdev = to_pci_dev(dev);
  45. return sprintf (buf, "%u\n", pdev->broken_parity_status);
  46. }
  47. static ssize_t broken_parity_status_store(struct device *dev,
  48. struct device_attribute *attr,
  49. const char *buf, size_t count)
  50. {
  51. struct pci_dev *pdev = to_pci_dev(dev);
  52. ssize_t consumed = -EINVAL;
  53. if ((count > 0) && (*buf == '0' || *buf == '1')) {
  54. pdev->broken_parity_status = *buf == '1' ? 1 : 0;
  55. consumed = count;
  56. }
  57. return consumed;
  58. }
  59. static ssize_t local_cpus_show(struct device *dev,
  60. struct device_attribute *attr, char *buf)
  61. {
  62. cpumask_t mask;
  63. int len;
  64. mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
  65. len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
  66. strcat(buf,"\n");
  67. return 1+len;
  68. }
  69. /* show resources */
  70. static ssize_t
  71. resource_show(struct device * dev, struct device_attribute *attr, char * buf)
  72. {
  73. struct pci_dev * pci_dev = to_pci_dev(dev);
  74. char * str = buf;
  75. int i;
  76. int max = 7;
  77. resource_size_t start, end;
  78. if (pci_dev->subordinate)
  79. max = DEVICE_COUNT_RESOURCE;
  80. for (i = 0; i < max; i++) {
  81. struct resource *res = &pci_dev->resource[i];
  82. pci_resource_to_user(pci_dev, i, res, &start, &end);
  83. str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
  84. (unsigned long long)start,
  85. (unsigned long long)end,
  86. (unsigned long long)res->flags);
  87. }
  88. return (str - buf);
  89. }
  90. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  91. {
  92. struct pci_dev *pci_dev = to_pci_dev(dev);
  93. return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
  94. pci_dev->vendor, pci_dev->device,
  95. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  96. (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
  97. (u8)(pci_dev->class));
  98. }
  99. static ssize_t
  100. is_enabled_store(struct device *dev, struct device_attribute *attr,
  101. const char *buf, size_t count)
  102. {
  103. struct pci_dev *pdev = to_pci_dev(dev);
  104. /* this can crash the machine when done on the "wrong" device */
  105. if (!capable(CAP_SYS_ADMIN))
  106. return count;
  107. if (*buf == '0')
  108. pci_disable_device(pdev);
  109. if (*buf == '1')
  110. pci_enable_device(pdev);
  111. return count;
  112. }
  113. struct device_attribute pci_dev_attrs[] = {
  114. __ATTR_RO(resource),
  115. __ATTR_RO(vendor),
  116. __ATTR_RO(device),
  117. __ATTR_RO(subsystem_vendor),
  118. __ATTR_RO(subsystem_device),
  119. __ATTR_RO(class),
  120. __ATTR_RO(irq),
  121. __ATTR_RO(local_cpus),
  122. __ATTR_RO(modalias),
  123. __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
  124. __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
  125. broken_parity_status_show,broken_parity_status_store),
  126. __ATTR_NULL,
  127. };
  128. static ssize_t
  129. pci_read_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
  130. {
  131. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  132. unsigned int size = 64;
  133. loff_t init_off = off;
  134. u8 *data = (u8*) buf;
  135. /* Several chips lock up trying to read undefined config space */
  136. if (capable(CAP_SYS_ADMIN)) {
  137. size = dev->cfg_size;
  138. } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  139. size = 128;
  140. }
  141. if (off > size)
  142. return 0;
  143. if (off + count > size) {
  144. size -= off;
  145. count = size;
  146. } else {
  147. size = count;
  148. }
  149. if ((off & 1) && size) {
  150. u8 val;
  151. pci_user_read_config_byte(dev, off, &val);
  152. data[off - init_off] = val;
  153. off++;
  154. size--;
  155. }
  156. if ((off & 3) && size > 2) {
  157. u16 val;
  158. pci_user_read_config_word(dev, off, &val);
  159. data[off - init_off] = val & 0xff;
  160. data[off - init_off + 1] = (val >> 8) & 0xff;
  161. off += 2;
  162. size -= 2;
  163. }
  164. while (size > 3) {
  165. u32 val;
  166. pci_user_read_config_dword(dev, off, &val);
  167. data[off - init_off] = val & 0xff;
  168. data[off - init_off + 1] = (val >> 8) & 0xff;
  169. data[off - init_off + 2] = (val >> 16) & 0xff;
  170. data[off - init_off + 3] = (val >> 24) & 0xff;
  171. off += 4;
  172. size -= 4;
  173. }
  174. if (size >= 2) {
  175. u16 val;
  176. pci_user_read_config_word(dev, off, &val);
  177. data[off - init_off] = val & 0xff;
  178. data[off - init_off + 1] = (val >> 8) & 0xff;
  179. off += 2;
  180. size -= 2;
  181. }
  182. if (size > 0) {
  183. u8 val;
  184. pci_user_read_config_byte(dev, off, &val);
  185. data[off - init_off] = val;
  186. off++;
  187. --size;
  188. }
  189. return count;
  190. }
  191. static ssize_t
  192. pci_write_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
  193. {
  194. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  195. unsigned int size = count;
  196. loff_t init_off = off;
  197. u8 *data = (u8*) buf;
  198. if (off > dev->cfg_size)
  199. return 0;
  200. if (off + count > dev->cfg_size) {
  201. size = dev->cfg_size - off;
  202. count = size;
  203. }
  204. if ((off & 1) && size) {
  205. pci_user_write_config_byte(dev, off, data[off - init_off]);
  206. off++;
  207. size--;
  208. }
  209. if ((off & 3) && size > 2) {
  210. u16 val = data[off - init_off];
  211. val |= (u16) data[off - init_off + 1] << 8;
  212. pci_user_write_config_word(dev, off, val);
  213. off += 2;
  214. size -= 2;
  215. }
  216. while (size > 3) {
  217. u32 val = data[off - init_off];
  218. val |= (u32) data[off - init_off + 1] << 8;
  219. val |= (u32) data[off - init_off + 2] << 16;
  220. val |= (u32) data[off - init_off + 3] << 24;
  221. pci_user_write_config_dword(dev, off, val);
  222. off += 4;
  223. size -= 4;
  224. }
  225. if (size >= 2) {
  226. u16 val = data[off - init_off];
  227. val |= (u16) data[off - init_off + 1] << 8;
  228. pci_user_write_config_word(dev, off, val);
  229. off += 2;
  230. size -= 2;
  231. }
  232. if (size) {
  233. pci_user_write_config_byte(dev, off, data[off - init_off]);
  234. off++;
  235. --size;
  236. }
  237. return count;
  238. }
  239. #ifdef HAVE_PCI_LEGACY
  240. /**
  241. * pci_read_legacy_io - read byte(s) from legacy I/O port space
  242. * @kobj: kobject corresponding to file to read from
  243. * @buf: buffer to store results
  244. * @off: offset into legacy I/O port space
  245. * @count: number of bytes to read
  246. *
  247. * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  248. * callback routine (pci_legacy_read).
  249. */
  250. ssize_t
  251. pci_read_legacy_io(struct kobject *kobj, char *buf, loff_t off, size_t count)
  252. {
  253. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  254. struct class_device,
  255. kobj));
  256. /* Only support 1, 2 or 4 byte accesses */
  257. if (count != 1 && count != 2 && count != 4)
  258. return -EINVAL;
  259. return pci_legacy_read(bus, off, (u32 *)buf, count);
  260. }
  261. /**
  262. * pci_write_legacy_io - write byte(s) to legacy I/O port space
  263. * @kobj: kobject corresponding to file to read from
  264. * @buf: buffer containing value to be written
  265. * @off: offset into legacy I/O port space
  266. * @count: number of bytes to write
  267. *
  268. * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  269. * callback routine (pci_legacy_write).
  270. */
  271. ssize_t
  272. pci_write_legacy_io(struct kobject *kobj, char *buf, loff_t off, size_t count)
  273. {
  274. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  275. struct class_device,
  276. kobj));
  277. /* Only support 1, 2 or 4 byte accesses */
  278. if (count != 1 && count != 2 && count != 4)
  279. return -EINVAL;
  280. return pci_legacy_write(bus, off, *(u32 *)buf, count);
  281. }
  282. /**
  283. * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
  284. * @kobj: kobject corresponding to device to be mapped
  285. * @attr: struct bin_attribute for this file
  286. * @vma: struct vm_area_struct passed to mmap
  287. *
  288. * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
  289. * legacy memory space (first meg of bus space) into application virtual
  290. * memory space.
  291. */
  292. int
  293. pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
  294. struct vm_area_struct *vma)
  295. {
  296. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  297. struct class_device,
  298. kobj));
  299. return pci_mmap_legacy_page_range(bus, vma);
  300. }
  301. #endif /* HAVE_PCI_LEGACY */
  302. #ifdef HAVE_PCI_MMAP
  303. /**
  304. * pci_mmap_resource - map a PCI resource into user memory space
  305. * @kobj: kobject for mapping
  306. * @attr: struct bin_attribute for the file being mapped
  307. * @vma: struct vm_area_struct passed into the mmap
  308. *
  309. * Use the regular PCI mapping routines to map a PCI resource into userspace.
  310. * FIXME: write combining? maybe automatic for prefetchable regions?
  311. */
  312. static int
  313. pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
  314. struct vm_area_struct *vma)
  315. {
  316. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  317. struct device, kobj));
  318. struct resource *res = (struct resource *)attr->private;
  319. enum pci_mmap_state mmap_type;
  320. resource_size_t start, end;
  321. int i;
  322. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  323. if (res == &pdev->resource[i])
  324. break;
  325. if (i >= PCI_ROM_RESOURCE)
  326. return -ENODEV;
  327. /* pci_mmap_page_range() expects the same kind of entry as coming
  328. * from /proc/bus/pci/ which is a "user visible" value. If this is
  329. * different from the resource itself, arch will do necessary fixup.
  330. */
  331. pci_resource_to_user(pdev, i, res, &start, &end);
  332. vma->vm_pgoff += start >> PAGE_SHIFT;
  333. mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
  334. return pci_mmap_page_range(pdev, vma, mmap_type, 0);
  335. }
  336. /**
  337. * pci_create_resource_files - create resource files in sysfs for @dev
  338. * @dev: dev in question
  339. *
  340. * Walk the resources in @dev creating files for each resource available.
  341. */
  342. static void
  343. pci_create_resource_files(struct pci_dev *pdev)
  344. {
  345. int i;
  346. /* Expose the PCI resources from this device as files */
  347. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  348. struct bin_attribute *res_attr;
  349. /* skip empty resources */
  350. if (!pci_resource_len(pdev, i))
  351. continue;
  352. /* allocate attribute structure, piggyback attribute name */
  353. res_attr = kzalloc(sizeof(*res_attr) + 10, GFP_ATOMIC);
  354. if (res_attr) {
  355. char *res_attr_name = (char *)(res_attr + 1);
  356. pdev->res_attr[i] = res_attr;
  357. sprintf(res_attr_name, "resource%d", i);
  358. res_attr->attr.name = res_attr_name;
  359. res_attr->attr.mode = S_IRUSR | S_IWUSR;
  360. res_attr->attr.owner = THIS_MODULE;
  361. res_attr->size = pci_resource_len(pdev, i);
  362. res_attr->mmap = pci_mmap_resource;
  363. res_attr->private = &pdev->resource[i];
  364. sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
  365. }
  366. }
  367. }
  368. /**
  369. * pci_remove_resource_files - cleanup resource files
  370. * @dev: dev to cleanup
  371. *
  372. * If we created resource files for @dev, remove them from sysfs and
  373. * free their resources.
  374. */
  375. static void
  376. pci_remove_resource_files(struct pci_dev *pdev)
  377. {
  378. int i;
  379. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  380. struct bin_attribute *res_attr;
  381. res_attr = pdev->res_attr[i];
  382. if (res_attr) {
  383. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  384. kfree(res_attr);
  385. }
  386. }
  387. }
  388. #else /* !HAVE_PCI_MMAP */
  389. static inline void pci_create_resource_files(struct pci_dev *dev) { return; }
  390. static inline void pci_remove_resource_files(struct pci_dev *dev) { return; }
  391. #endif /* HAVE_PCI_MMAP */
  392. /**
  393. * pci_write_rom - used to enable access to the PCI ROM display
  394. * @kobj: kernel object handle
  395. * @buf: user input
  396. * @off: file offset
  397. * @count: number of byte in input
  398. *
  399. * writing anything except 0 enables it
  400. */
  401. static ssize_t
  402. pci_write_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
  403. {
  404. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  405. if ((off == 0) && (*buf == '0') && (count == 2))
  406. pdev->rom_attr_enabled = 0;
  407. else
  408. pdev->rom_attr_enabled = 1;
  409. return count;
  410. }
  411. /**
  412. * pci_read_rom - read a PCI ROM
  413. * @kobj: kernel object handle
  414. * @buf: where to put the data we read from the ROM
  415. * @off: file offset
  416. * @count: number of bytes to read
  417. *
  418. * Put @count bytes starting at @off into @buf from the ROM in the PCI
  419. * device corresponding to @kobj.
  420. */
  421. static ssize_t
  422. pci_read_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
  423. {
  424. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  425. void __iomem *rom;
  426. size_t size;
  427. if (!pdev->rom_attr_enabled)
  428. return -EINVAL;
  429. rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
  430. if (!rom)
  431. return 0;
  432. if (off >= size)
  433. count = 0;
  434. else {
  435. if (off + count > size)
  436. count = size - off;
  437. memcpy_fromio(buf, rom + off, count);
  438. }
  439. pci_unmap_rom(pdev, rom);
  440. return count;
  441. }
  442. static struct bin_attribute pci_config_attr = {
  443. .attr = {
  444. .name = "config",
  445. .mode = S_IRUGO | S_IWUSR,
  446. .owner = THIS_MODULE,
  447. },
  448. .size = 256,
  449. .read = pci_read_config,
  450. .write = pci_write_config,
  451. };
  452. static struct bin_attribute pcie_config_attr = {
  453. .attr = {
  454. .name = "config",
  455. .mode = S_IRUGO | S_IWUSR,
  456. .owner = THIS_MODULE,
  457. },
  458. .size = 4096,
  459. .read = pci_read_config,
  460. .write = pci_write_config,
  461. };
  462. int pci_create_sysfs_dev_files (struct pci_dev *pdev)
  463. {
  464. if (!sysfs_initialized)
  465. return -EACCES;
  466. if (pdev->cfg_size < 4096)
  467. sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
  468. else
  469. sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  470. pci_create_resource_files(pdev);
  471. /* If the device has a ROM, try to expose it in sysfs. */
  472. if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
  473. struct bin_attribute *rom_attr;
  474. rom_attr = kzalloc(sizeof(*rom_attr), GFP_ATOMIC);
  475. if (rom_attr) {
  476. pdev->rom_attr = rom_attr;
  477. rom_attr->size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  478. rom_attr->attr.name = "rom";
  479. rom_attr->attr.mode = S_IRUSR;
  480. rom_attr->attr.owner = THIS_MODULE;
  481. rom_attr->read = pci_read_rom;
  482. rom_attr->write = pci_write_rom;
  483. sysfs_create_bin_file(&pdev->dev.kobj, rom_attr);
  484. }
  485. }
  486. /* add platform-specific attributes */
  487. pcibios_add_platform_entries(pdev);
  488. return 0;
  489. }
  490. /**
  491. * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
  492. * @pdev: device whose entries we should free
  493. *
  494. * Cleanup when @pdev is removed from sysfs.
  495. */
  496. void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
  497. {
  498. if (pdev->cfg_size < 4096)
  499. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  500. else
  501. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  502. pci_remove_resource_files(pdev);
  503. if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
  504. if (pdev->rom_attr) {
  505. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  506. kfree(pdev->rom_attr);
  507. }
  508. }
  509. }
  510. static int __init pci_sysfs_init(void)
  511. {
  512. struct pci_dev *pdev = NULL;
  513. sysfs_initialized = 1;
  514. for_each_pci_dev(pdev)
  515. pci_create_sysfs_dev_files(pdev);
  516. return 0;
  517. }
  518. __initcall(pci_sysfs_init);