pci-sysfs.c 15 KB

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