pci-sysfs.c 25 KB

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