pci-sysfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. ssize_t consumed = -EINVAL;
  55. if ((count > 0) && (*buf == '0' || *buf == '1')) {
  56. pdev->broken_parity_status = *buf == '1' ? 1 : 0;
  57. consumed = count;
  58. }
  59. return consumed;
  60. }
  61. static ssize_t local_cpus_show(struct device *dev,
  62. struct device_attribute *attr, char *buf)
  63. {
  64. cpumask_t mask;
  65. int len;
  66. mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
  67. len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
  68. buf[len++] = '\n';
  69. buf[len] = '\0';
  70. return len;
  71. }
  72. static ssize_t local_cpulist_show(struct device *dev,
  73. struct device_attribute *attr, char *buf)
  74. {
  75. cpumask_t mask;
  76. int len;
  77. mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
  78. len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
  79. buf[len++] = '\n';
  80. buf[len] = '\0';
  81. return len;
  82. }
  83. /* show resources */
  84. static ssize_t
  85. resource_show(struct device * dev, struct device_attribute *attr, char * buf)
  86. {
  87. struct pci_dev * pci_dev = to_pci_dev(dev);
  88. char * str = buf;
  89. int i;
  90. int max = 7;
  91. resource_size_t start, end;
  92. if (pci_dev->subordinate)
  93. max = DEVICE_COUNT_RESOURCE;
  94. for (i = 0; i < max; i++) {
  95. struct resource *res = &pci_dev->resource[i];
  96. pci_resource_to_user(pci_dev, i, res, &start, &end);
  97. str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
  98. (unsigned long long)start,
  99. (unsigned long long)end,
  100. (unsigned long long)res->flags);
  101. }
  102. return (str - buf);
  103. }
  104. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  105. {
  106. struct pci_dev *pci_dev = to_pci_dev(dev);
  107. return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
  108. pci_dev->vendor, pci_dev->device,
  109. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  110. (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
  111. (u8)(pci_dev->class));
  112. }
  113. static ssize_t is_enabled_store(struct device *dev,
  114. struct device_attribute *attr, const char *buf,
  115. size_t count)
  116. {
  117. ssize_t result = -EINVAL;
  118. struct pci_dev *pdev = to_pci_dev(dev);
  119. /* this can crash the machine when done on the "wrong" device */
  120. if (!capable(CAP_SYS_ADMIN))
  121. return count;
  122. if (*buf == '0') {
  123. if (atomic_read(&pdev->enable_cnt) != 0)
  124. pci_disable_device(pdev);
  125. else
  126. result = -EIO;
  127. } else if (*buf == '1')
  128. result = pci_enable_device(pdev);
  129. return result < 0 ? result : count;
  130. }
  131. static ssize_t is_enabled_show(struct device *dev,
  132. struct device_attribute *attr, char *buf)
  133. {
  134. struct pci_dev *pdev;
  135. pdev = to_pci_dev (dev);
  136. return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
  137. }
  138. #ifdef CONFIG_NUMA
  139. static ssize_t
  140. numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
  141. {
  142. return sprintf (buf, "%d\n", dev->numa_node);
  143. }
  144. #endif
  145. static ssize_t
  146. msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
  147. {
  148. struct pci_dev *pdev = to_pci_dev(dev);
  149. if (!pdev->subordinate)
  150. return 0;
  151. return sprintf (buf, "%u\n",
  152. !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
  153. }
  154. static ssize_t
  155. msi_bus_store(struct device *dev, struct device_attribute *attr,
  156. const char *buf, size_t count)
  157. {
  158. struct pci_dev *pdev = to_pci_dev(dev);
  159. /* bad things may happen if the no_msi flag is changed
  160. * while some drivers are loaded */
  161. if (!capable(CAP_SYS_ADMIN))
  162. return count;
  163. if (!pdev->subordinate)
  164. return count;
  165. if (*buf == '0') {
  166. pdev->subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
  167. dev_warn(&pdev->dev, "forced subordinate bus to not support MSI,"
  168. " bad things could happen.\n");
  169. }
  170. if (*buf == '1') {
  171. pdev->subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
  172. dev_warn(&pdev->dev, "forced subordinate bus to support MSI,"
  173. " bad things could happen.\n");
  174. }
  175. return count;
  176. }
  177. struct device_attribute pci_dev_attrs[] = {
  178. __ATTR_RO(resource),
  179. __ATTR_RO(vendor),
  180. __ATTR_RO(device),
  181. __ATTR_RO(subsystem_vendor),
  182. __ATTR_RO(subsystem_device),
  183. __ATTR_RO(class),
  184. __ATTR_RO(irq),
  185. __ATTR_RO(local_cpus),
  186. __ATTR_RO(local_cpulist),
  187. __ATTR_RO(modalias),
  188. #ifdef CONFIG_NUMA
  189. __ATTR_RO(numa_node),
  190. #endif
  191. __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
  192. __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
  193. broken_parity_status_show,broken_parity_status_store),
  194. __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
  195. __ATTR_NULL,
  196. };
  197. static ssize_t
  198. pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
  199. char *buf, loff_t off, size_t count)
  200. {
  201. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  202. unsigned int size = 64;
  203. loff_t init_off = off;
  204. u8 *data = (u8*) buf;
  205. /* Several chips lock up trying to read undefined config space */
  206. if (capable(CAP_SYS_ADMIN)) {
  207. size = dev->cfg_size;
  208. } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  209. size = 128;
  210. }
  211. if (off > size)
  212. return 0;
  213. if (off + count > size) {
  214. size -= off;
  215. count = size;
  216. } else {
  217. size = count;
  218. }
  219. if ((off & 1) && size) {
  220. u8 val;
  221. pci_user_read_config_byte(dev, off, &val);
  222. data[off - init_off] = val;
  223. off++;
  224. size--;
  225. }
  226. if ((off & 3) && size > 2) {
  227. u16 val;
  228. pci_user_read_config_word(dev, off, &val);
  229. data[off - init_off] = val & 0xff;
  230. data[off - init_off + 1] = (val >> 8) & 0xff;
  231. off += 2;
  232. size -= 2;
  233. }
  234. while (size > 3) {
  235. u32 val;
  236. pci_user_read_config_dword(dev, off, &val);
  237. data[off - init_off] = val & 0xff;
  238. data[off - init_off + 1] = (val >> 8) & 0xff;
  239. data[off - init_off + 2] = (val >> 16) & 0xff;
  240. data[off - init_off + 3] = (val >> 24) & 0xff;
  241. off += 4;
  242. size -= 4;
  243. }
  244. if (size >= 2) {
  245. u16 val;
  246. pci_user_read_config_word(dev, off, &val);
  247. data[off - init_off] = val & 0xff;
  248. data[off - init_off + 1] = (val >> 8) & 0xff;
  249. off += 2;
  250. size -= 2;
  251. }
  252. if (size > 0) {
  253. u8 val;
  254. pci_user_read_config_byte(dev, off, &val);
  255. data[off - init_off] = val;
  256. off++;
  257. --size;
  258. }
  259. return count;
  260. }
  261. static ssize_t
  262. pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
  263. char *buf, loff_t off, size_t count)
  264. {
  265. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  266. unsigned int size = count;
  267. loff_t init_off = off;
  268. u8 *data = (u8*) buf;
  269. if (off > dev->cfg_size)
  270. return 0;
  271. if (off + count > dev->cfg_size) {
  272. size = dev->cfg_size - off;
  273. count = size;
  274. }
  275. if ((off & 1) && size) {
  276. pci_user_write_config_byte(dev, off, data[off - init_off]);
  277. off++;
  278. size--;
  279. }
  280. if ((off & 3) && size > 2) {
  281. u16 val = data[off - init_off];
  282. val |= (u16) data[off - init_off + 1] << 8;
  283. pci_user_write_config_word(dev, off, val);
  284. off += 2;
  285. size -= 2;
  286. }
  287. while (size > 3) {
  288. u32 val = data[off - init_off];
  289. val |= (u32) data[off - init_off + 1] << 8;
  290. val |= (u32) data[off - init_off + 2] << 16;
  291. val |= (u32) data[off - init_off + 3] << 24;
  292. pci_user_write_config_dword(dev, off, val);
  293. off += 4;
  294. size -= 4;
  295. }
  296. if (size >= 2) {
  297. u16 val = data[off - init_off];
  298. val |= (u16) data[off - init_off + 1] << 8;
  299. pci_user_write_config_word(dev, off, val);
  300. off += 2;
  301. size -= 2;
  302. }
  303. if (size) {
  304. pci_user_write_config_byte(dev, off, data[off - init_off]);
  305. off++;
  306. --size;
  307. }
  308. return count;
  309. }
  310. static ssize_t
  311. pci_read_vpd(struct kobject *kobj, struct bin_attribute *bin_attr,
  312. char *buf, loff_t off, size_t count)
  313. {
  314. struct pci_dev *dev =
  315. to_pci_dev(container_of(kobj, struct device, kobj));
  316. int end;
  317. int ret;
  318. if (off > bin_attr->size)
  319. count = 0;
  320. else if (count > bin_attr->size - off)
  321. count = bin_attr->size - off;
  322. end = off + count;
  323. while (off < end) {
  324. ret = dev->vpd->ops->read(dev, off, end - off, buf);
  325. if (ret < 0)
  326. return ret;
  327. buf += ret;
  328. off += ret;
  329. }
  330. return count;
  331. }
  332. static ssize_t
  333. pci_write_vpd(struct kobject *kobj, struct bin_attribute *bin_attr,
  334. char *buf, loff_t off, size_t count)
  335. {
  336. struct pci_dev *dev =
  337. to_pci_dev(container_of(kobj, struct device, kobj));
  338. int end;
  339. int ret;
  340. if (off > bin_attr->size)
  341. count = 0;
  342. else if (count > bin_attr->size - off)
  343. count = bin_attr->size - off;
  344. end = off + count;
  345. while (off < end) {
  346. ret = dev->vpd->ops->write(dev, off, end - off, buf);
  347. if (ret < 0)
  348. return ret;
  349. buf += ret;
  350. off += ret;
  351. }
  352. return count;
  353. }
  354. #ifdef HAVE_PCI_LEGACY
  355. /**
  356. * pci_read_legacy_io - read byte(s) from legacy I/O port space
  357. * @kobj: kobject corresponding to file to read from
  358. * @buf: buffer to store results
  359. * @off: offset into legacy I/O port space
  360. * @count: number of bytes to read
  361. *
  362. * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  363. * callback routine (pci_legacy_read).
  364. */
  365. ssize_t
  366. pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
  367. char *buf, loff_t off, size_t count)
  368. {
  369. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  370. struct device,
  371. kobj));
  372. /* Only support 1, 2 or 4 byte accesses */
  373. if (count != 1 && count != 2 && count != 4)
  374. return -EINVAL;
  375. return pci_legacy_read(bus, off, (u32 *)buf, count);
  376. }
  377. /**
  378. * pci_write_legacy_io - write byte(s) to legacy I/O port space
  379. * @kobj: kobject corresponding to file to read from
  380. * @buf: buffer containing value to be written
  381. * @off: offset into legacy I/O port space
  382. * @count: number of bytes to write
  383. *
  384. * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  385. * callback routine (pci_legacy_write).
  386. */
  387. ssize_t
  388. pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
  389. char *buf, loff_t off, size_t count)
  390. {
  391. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  392. struct device,
  393. kobj));
  394. /* Only support 1, 2 or 4 byte accesses */
  395. if (count != 1 && count != 2 && count != 4)
  396. return -EINVAL;
  397. return pci_legacy_write(bus, off, *(u32 *)buf, count);
  398. }
  399. /**
  400. * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
  401. * @kobj: kobject corresponding to device to be mapped
  402. * @attr: struct bin_attribute for this file
  403. * @vma: struct vm_area_struct passed to mmap
  404. *
  405. * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
  406. * legacy memory space (first meg of bus space) into application virtual
  407. * memory space.
  408. */
  409. int
  410. pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
  411. struct vm_area_struct *vma)
  412. {
  413. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  414. struct device,
  415. kobj));
  416. return pci_mmap_legacy_page_range(bus, vma);
  417. }
  418. #endif /* HAVE_PCI_LEGACY */
  419. #ifdef HAVE_PCI_MMAP
  420. static int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma)
  421. {
  422. unsigned long nr, start, size;
  423. nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  424. start = vma->vm_pgoff;
  425. size = pci_resource_len(pdev, resno) >> PAGE_SHIFT;
  426. if (start < size && size - start >= nr)
  427. return 1;
  428. WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n",
  429. current->comm, start, start+nr, pci_name(pdev), resno, size);
  430. return 0;
  431. }
  432. /**
  433. * pci_mmap_resource - map a PCI resource into user memory space
  434. * @kobj: kobject for mapping
  435. * @attr: struct bin_attribute for the file being mapped
  436. * @vma: struct vm_area_struct passed into the mmap
  437. * @write_combine: 1 for write_combine mapping
  438. *
  439. * Use the regular PCI mapping routines to map a PCI resource into userspace.
  440. */
  441. static int
  442. pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
  443. struct vm_area_struct *vma, int write_combine)
  444. {
  445. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  446. struct device, kobj));
  447. struct resource *res = (struct resource *)attr->private;
  448. enum pci_mmap_state mmap_type;
  449. resource_size_t start, end;
  450. int i;
  451. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  452. if (res == &pdev->resource[i])
  453. break;
  454. if (i >= PCI_ROM_RESOURCE)
  455. return -ENODEV;
  456. if (!pci_mmap_fits(pdev, i, vma))
  457. return -EINVAL;
  458. /* pci_mmap_page_range() expects the same kind of entry as coming
  459. * from /proc/bus/pci/ which is a "user visible" value. If this is
  460. * different from the resource itself, arch will do necessary fixup.
  461. */
  462. pci_resource_to_user(pdev, i, res, &start, &end);
  463. vma->vm_pgoff += start >> PAGE_SHIFT;
  464. mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
  465. return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
  466. }
  467. static int
  468. pci_mmap_resource_uc(struct kobject *kobj, struct bin_attribute *attr,
  469. struct vm_area_struct *vma)
  470. {
  471. return pci_mmap_resource(kobj, attr, vma, 0);
  472. }
  473. static int
  474. pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr,
  475. struct vm_area_struct *vma)
  476. {
  477. return pci_mmap_resource(kobj, attr, vma, 1);
  478. }
  479. /**
  480. * pci_remove_resource_files - cleanup resource files
  481. * @dev: dev to cleanup
  482. *
  483. * If we created resource files for @dev, remove them from sysfs and
  484. * free their resources.
  485. */
  486. static void
  487. pci_remove_resource_files(struct pci_dev *pdev)
  488. {
  489. int i;
  490. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  491. struct bin_attribute *res_attr;
  492. res_attr = pdev->res_attr[i];
  493. if (res_attr) {
  494. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  495. kfree(res_attr);
  496. }
  497. res_attr = pdev->res_attr_wc[i];
  498. if (res_attr) {
  499. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  500. kfree(res_attr);
  501. }
  502. }
  503. }
  504. static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
  505. {
  506. /* allocate attribute structure, piggyback attribute name */
  507. int name_len = write_combine ? 13 : 10;
  508. struct bin_attribute *res_attr;
  509. int retval;
  510. res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
  511. if (res_attr) {
  512. char *res_attr_name = (char *)(res_attr + 1);
  513. if (write_combine) {
  514. pdev->res_attr_wc[num] = res_attr;
  515. sprintf(res_attr_name, "resource%d_wc", num);
  516. res_attr->mmap = pci_mmap_resource_wc;
  517. } else {
  518. pdev->res_attr[num] = res_attr;
  519. sprintf(res_attr_name, "resource%d", num);
  520. res_attr->mmap = pci_mmap_resource_uc;
  521. }
  522. res_attr->attr.name = res_attr_name;
  523. res_attr->attr.mode = S_IRUSR | S_IWUSR;
  524. res_attr->size = pci_resource_len(pdev, num);
  525. res_attr->private = &pdev->resource[num];
  526. retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
  527. } else
  528. retval = -ENOMEM;
  529. return retval;
  530. }
  531. /**
  532. * pci_create_resource_files - create resource files in sysfs for @dev
  533. * @dev: dev in question
  534. *
  535. * Walk the resources in @dev creating files for each resource available.
  536. */
  537. static int pci_create_resource_files(struct pci_dev *pdev)
  538. {
  539. int i;
  540. int retval;
  541. /* Expose the PCI resources from this device as files */
  542. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  543. /* skip empty resources */
  544. if (!pci_resource_len(pdev, i))
  545. continue;
  546. retval = pci_create_attr(pdev, i, 0);
  547. /* for prefetchable resources, create a WC mappable file */
  548. if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
  549. retval = pci_create_attr(pdev, i, 1);
  550. if (retval) {
  551. pci_remove_resource_files(pdev);
  552. return retval;
  553. }
  554. }
  555. return 0;
  556. }
  557. #else /* !HAVE_PCI_MMAP */
  558. static inline int pci_create_resource_files(struct pci_dev *dev) { return 0; }
  559. static inline void pci_remove_resource_files(struct pci_dev *dev) { return; }
  560. #endif /* HAVE_PCI_MMAP */
  561. /**
  562. * pci_write_rom - used to enable access to the PCI ROM display
  563. * @kobj: kernel object handle
  564. * @buf: user input
  565. * @off: file offset
  566. * @count: number of byte in input
  567. *
  568. * writing anything except 0 enables it
  569. */
  570. static ssize_t
  571. pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
  572. char *buf, loff_t off, size_t count)
  573. {
  574. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  575. if ((off == 0) && (*buf == '0') && (count == 2))
  576. pdev->rom_attr_enabled = 0;
  577. else
  578. pdev->rom_attr_enabled = 1;
  579. return count;
  580. }
  581. /**
  582. * pci_read_rom - read a PCI ROM
  583. * @kobj: kernel object handle
  584. * @buf: where to put the data we read from the ROM
  585. * @off: file offset
  586. * @count: number of bytes to read
  587. *
  588. * Put @count bytes starting at @off into @buf from the ROM in the PCI
  589. * device corresponding to @kobj.
  590. */
  591. static ssize_t
  592. pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
  593. char *buf, loff_t off, size_t count)
  594. {
  595. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  596. void __iomem *rom;
  597. size_t size;
  598. if (!pdev->rom_attr_enabled)
  599. return -EINVAL;
  600. rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
  601. if (!rom)
  602. return 0;
  603. if (off >= size)
  604. count = 0;
  605. else {
  606. if (off + count > size)
  607. count = size - off;
  608. memcpy_fromio(buf, rom + off, count);
  609. }
  610. pci_unmap_rom(pdev, rom);
  611. return count;
  612. }
  613. static struct bin_attribute pci_config_attr = {
  614. .attr = {
  615. .name = "config",
  616. .mode = S_IRUGO | S_IWUSR,
  617. },
  618. .size = 256,
  619. .read = pci_read_config,
  620. .write = pci_write_config,
  621. };
  622. static struct bin_attribute pcie_config_attr = {
  623. .attr = {
  624. .name = "config",
  625. .mode = S_IRUGO | S_IWUSR,
  626. },
  627. .size = 4096,
  628. .read = pci_read_config,
  629. .write = pci_write_config,
  630. };
  631. int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
  632. {
  633. return 0;
  634. }
  635. int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
  636. {
  637. struct bin_attribute *attr = NULL;
  638. int retval;
  639. if (!sysfs_initialized)
  640. return -EACCES;
  641. if (pdev->cfg_size < 4096)
  642. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
  643. else
  644. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  645. if (retval)
  646. goto err;
  647. /* If the device has VPD, try to expose it in sysfs. */
  648. if (pdev->vpd) {
  649. attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
  650. if (attr) {
  651. pdev->vpd->attr = attr;
  652. attr->size = pdev->vpd->len;
  653. attr->attr.name = "vpd";
  654. attr->attr.mode = S_IRUSR | S_IWUSR;
  655. attr->read = pci_read_vpd;
  656. attr->write = pci_write_vpd;
  657. retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
  658. if (retval)
  659. goto err_vpd;
  660. } else {
  661. retval = -ENOMEM;
  662. goto err_config_file;
  663. }
  664. }
  665. retval = pci_create_resource_files(pdev);
  666. if (retval)
  667. goto err_vpd_file;
  668. /* If the device has a ROM, try to expose it in sysfs. */
  669. if (pci_resource_len(pdev, PCI_ROM_RESOURCE) ||
  670. (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)) {
  671. attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
  672. if (attr) {
  673. pdev->rom_attr = attr;
  674. attr->size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  675. attr->attr.name = "rom";
  676. attr->attr.mode = S_IRUSR;
  677. attr->read = pci_read_rom;
  678. attr->write = pci_write_rom;
  679. retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
  680. if (retval)
  681. goto err_rom;
  682. } else {
  683. retval = -ENOMEM;
  684. goto err_resource_files;
  685. }
  686. }
  687. /* add platform-specific attributes */
  688. if (pcibios_add_platform_entries(pdev))
  689. goto err_rom_file;
  690. pcie_aspm_create_sysfs_dev_files(pdev);
  691. return 0;
  692. err_rom_file:
  693. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  694. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  695. err_rom:
  696. kfree(pdev->rom_attr);
  697. err_resource_files:
  698. pci_remove_resource_files(pdev);
  699. err_vpd_file:
  700. if (pdev->vpd) {
  701. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->vpd->attr);
  702. err_vpd:
  703. kfree(pdev->vpd->attr);
  704. }
  705. err_config_file:
  706. if (pdev->cfg_size < 4096)
  707. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  708. else
  709. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  710. err:
  711. return retval;
  712. }
  713. /**
  714. * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
  715. * @pdev: device whose entries we should free
  716. *
  717. * Cleanup when @pdev is removed from sysfs.
  718. */
  719. void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
  720. {
  721. if (!sysfs_initialized)
  722. return;
  723. pcie_aspm_remove_sysfs_dev_files(pdev);
  724. if (pdev->vpd) {
  725. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->vpd->attr);
  726. kfree(pdev->vpd->attr);
  727. }
  728. if (pdev->cfg_size < 4096)
  729. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  730. else
  731. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  732. pci_remove_resource_files(pdev);
  733. if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
  734. if (pdev->rom_attr) {
  735. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  736. kfree(pdev->rom_attr);
  737. }
  738. }
  739. }
  740. static int __init pci_sysfs_init(void)
  741. {
  742. struct pci_dev *pdev = NULL;
  743. int retval;
  744. sysfs_initialized = 1;
  745. for_each_pci_dev(pdev) {
  746. retval = pci_create_sysfs_dev_files(pdev);
  747. if (retval) {
  748. pci_dev_put(pdev);
  749. return retval;
  750. }
  751. }
  752. return 0;
  753. }
  754. late_initcall(pci_sysfs_init);