pci-sysfs.c 21 KB

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