pci-sysfs.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. const struct cpumask *mask;
  64. int len;
  65. mask = cpumask_of_pcibus(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. const struct cpumask *mask;
  75. int len;
  76. mask = cpumask_of_pcibus(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 (pci_is_enabled(pdev))
  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. #ifdef CONFIG_HOTPLUG
  184. static DEFINE_MUTEX(pci_remove_rescan_mutex);
  185. static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
  186. size_t count)
  187. {
  188. unsigned long val;
  189. struct pci_bus *b = NULL;
  190. if (strict_strtoul(buf, 0, &val) < 0)
  191. return -EINVAL;
  192. if (val) {
  193. mutex_lock(&pci_remove_rescan_mutex);
  194. while ((b = pci_find_next_bus(b)) != NULL)
  195. pci_rescan_bus(b);
  196. mutex_unlock(&pci_remove_rescan_mutex);
  197. }
  198. return count;
  199. }
  200. struct bus_attribute pci_bus_attrs[] = {
  201. __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
  202. __ATTR_NULL
  203. };
  204. static ssize_t
  205. dev_rescan_store(struct device *dev, struct device_attribute *attr,
  206. const char *buf, size_t count)
  207. {
  208. unsigned long val;
  209. struct pci_dev *pdev = to_pci_dev(dev);
  210. if (strict_strtoul(buf, 0, &val) < 0)
  211. return -EINVAL;
  212. if (val) {
  213. mutex_lock(&pci_remove_rescan_mutex);
  214. pci_rescan_bus(pdev->bus);
  215. mutex_unlock(&pci_remove_rescan_mutex);
  216. }
  217. return count;
  218. }
  219. static void remove_callback(struct device *dev)
  220. {
  221. struct pci_dev *pdev = to_pci_dev(dev);
  222. mutex_lock(&pci_remove_rescan_mutex);
  223. pci_remove_bus_device(pdev);
  224. mutex_unlock(&pci_remove_rescan_mutex);
  225. }
  226. static ssize_t
  227. remove_store(struct device *dev, struct device_attribute *dummy,
  228. const char *buf, size_t count)
  229. {
  230. int ret = 0;
  231. unsigned long val;
  232. if (strict_strtoul(buf, 0, &val) < 0)
  233. return -EINVAL;
  234. /* An attribute cannot be unregistered by one of its own methods,
  235. * so we have to use this roundabout approach.
  236. */
  237. if (val)
  238. ret = device_schedule_callback(dev, remove_callback);
  239. if (ret)
  240. count = ret;
  241. return count;
  242. }
  243. #endif
  244. struct device_attribute pci_dev_attrs[] = {
  245. __ATTR_RO(resource),
  246. __ATTR_RO(vendor),
  247. __ATTR_RO(device),
  248. __ATTR_RO(subsystem_vendor),
  249. __ATTR_RO(subsystem_device),
  250. __ATTR_RO(class),
  251. __ATTR_RO(irq),
  252. __ATTR_RO(local_cpus),
  253. __ATTR_RO(local_cpulist),
  254. __ATTR_RO(modalias),
  255. #ifdef CONFIG_NUMA
  256. __ATTR_RO(numa_node),
  257. #endif
  258. __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
  259. __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
  260. broken_parity_status_show,broken_parity_status_store),
  261. __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
  262. #ifdef CONFIG_HOTPLUG
  263. __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
  264. __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
  265. #endif
  266. __ATTR_NULL,
  267. };
  268. static ssize_t
  269. boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
  270. {
  271. struct pci_dev *pdev = to_pci_dev(dev);
  272. return sprintf(buf, "%u\n",
  273. !!(pdev->resource[PCI_ROM_RESOURCE].flags &
  274. IORESOURCE_ROM_SHADOW));
  275. }
  276. struct device_attribute vga_attr = __ATTR_RO(boot_vga);
  277. static ssize_t
  278. pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
  279. char *buf, loff_t off, size_t count)
  280. {
  281. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  282. unsigned int size = 64;
  283. loff_t init_off = off;
  284. u8 *data = (u8*) buf;
  285. /* Several chips lock up trying to read undefined config space */
  286. if (capable(CAP_SYS_ADMIN)) {
  287. size = dev->cfg_size;
  288. } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  289. size = 128;
  290. }
  291. if (off > size)
  292. return 0;
  293. if (off + count > size) {
  294. size -= off;
  295. count = size;
  296. } else {
  297. size = count;
  298. }
  299. if ((off & 1) && size) {
  300. u8 val;
  301. pci_user_read_config_byte(dev, off, &val);
  302. data[off - init_off] = val;
  303. off++;
  304. size--;
  305. }
  306. if ((off & 3) && size > 2) {
  307. u16 val;
  308. pci_user_read_config_word(dev, off, &val);
  309. data[off - init_off] = val & 0xff;
  310. data[off - init_off + 1] = (val >> 8) & 0xff;
  311. off += 2;
  312. size -= 2;
  313. }
  314. while (size > 3) {
  315. u32 val;
  316. pci_user_read_config_dword(dev, off, &val);
  317. data[off - init_off] = val & 0xff;
  318. data[off - init_off + 1] = (val >> 8) & 0xff;
  319. data[off - init_off + 2] = (val >> 16) & 0xff;
  320. data[off - init_off + 3] = (val >> 24) & 0xff;
  321. off += 4;
  322. size -= 4;
  323. }
  324. if (size >= 2) {
  325. u16 val;
  326. pci_user_read_config_word(dev, off, &val);
  327. data[off - init_off] = val & 0xff;
  328. data[off - init_off + 1] = (val >> 8) & 0xff;
  329. off += 2;
  330. size -= 2;
  331. }
  332. if (size > 0) {
  333. u8 val;
  334. pci_user_read_config_byte(dev, off, &val);
  335. data[off - init_off] = val;
  336. off++;
  337. --size;
  338. }
  339. return count;
  340. }
  341. static ssize_t
  342. pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
  343. char *buf, loff_t off, size_t count)
  344. {
  345. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  346. unsigned int size = count;
  347. loff_t init_off = off;
  348. u8 *data = (u8*) buf;
  349. if (off > dev->cfg_size)
  350. return 0;
  351. if (off + count > dev->cfg_size) {
  352. size = dev->cfg_size - off;
  353. count = size;
  354. }
  355. if ((off & 1) && size) {
  356. pci_user_write_config_byte(dev, off, data[off - init_off]);
  357. off++;
  358. size--;
  359. }
  360. if ((off & 3) && size > 2) {
  361. u16 val = data[off - init_off];
  362. val |= (u16) data[off - init_off + 1] << 8;
  363. pci_user_write_config_word(dev, off, val);
  364. off += 2;
  365. size -= 2;
  366. }
  367. while (size > 3) {
  368. u32 val = data[off - init_off];
  369. val |= (u32) data[off - init_off + 1] << 8;
  370. val |= (u32) data[off - init_off + 2] << 16;
  371. val |= (u32) data[off - init_off + 3] << 24;
  372. pci_user_write_config_dword(dev, off, val);
  373. off += 4;
  374. size -= 4;
  375. }
  376. if (size >= 2) {
  377. u16 val = data[off - init_off];
  378. val |= (u16) data[off - init_off + 1] << 8;
  379. pci_user_write_config_word(dev, off, val);
  380. off += 2;
  381. size -= 2;
  382. }
  383. if (size) {
  384. pci_user_write_config_byte(dev, off, data[off - init_off]);
  385. off++;
  386. --size;
  387. }
  388. return count;
  389. }
  390. static ssize_t
  391. read_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
  392. char *buf, loff_t off, size_t count)
  393. {
  394. struct pci_dev *dev =
  395. to_pci_dev(container_of(kobj, struct device, kobj));
  396. if (off > bin_attr->size)
  397. count = 0;
  398. else if (count > bin_attr->size - off)
  399. count = bin_attr->size - off;
  400. return pci_read_vpd(dev, off, count, buf);
  401. }
  402. static ssize_t
  403. write_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
  404. char *buf, loff_t off, size_t count)
  405. {
  406. struct pci_dev *dev =
  407. to_pci_dev(container_of(kobj, struct device, kobj));
  408. if (off > bin_attr->size)
  409. count = 0;
  410. else if (count > bin_attr->size - off)
  411. count = bin_attr->size - off;
  412. return pci_write_vpd(dev, off, count, buf);
  413. }
  414. #ifdef HAVE_PCI_LEGACY
  415. /**
  416. * pci_read_legacy_io - read byte(s) from legacy I/O port space
  417. * @kobj: kobject corresponding to file to read from
  418. * @buf: buffer to store results
  419. * @off: offset into legacy I/O port space
  420. * @count: number of bytes to read
  421. *
  422. * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  423. * callback routine (pci_legacy_read).
  424. */
  425. static ssize_t
  426. pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
  427. char *buf, loff_t off, size_t count)
  428. {
  429. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  430. struct device,
  431. kobj));
  432. /* Only support 1, 2 or 4 byte accesses */
  433. if (count != 1 && count != 2 && count != 4)
  434. return -EINVAL;
  435. return pci_legacy_read(bus, off, (u32 *)buf, count);
  436. }
  437. /**
  438. * pci_write_legacy_io - write byte(s) to legacy I/O port space
  439. * @kobj: kobject corresponding to file to read from
  440. * @buf: buffer containing value to be written
  441. * @off: offset into legacy I/O port space
  442. * @count: number of bytes to write
  443. *
  444. * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  445. * callback routine (pci_legacy_write).
  446. */
  447. static ssize_t
  448. pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
  449. char *buf, loff_t off, size_t count)
  450. {
  451. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  452. struct device,
  453. kobj));
  454. /* Only support 1, 2 or 4 byte accesses */
  455. if (count != 1 && count != 2 && count != 4)
  456. return -EINVAL;
  457. return pci_legacy_write(bus, off, *(u32 *)buf, count);
  458. }
  459. /**
  460. * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
  461. * @kobj: kobject corresponding to device to be mapped
  462. * @attr: struct bin_attribute for this file
  463. * @vma: struct vm_area_struct passed to mmap
  464. *
  465. * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
  466. * legacy memory space (first meg of bus space) into application virtual
  467. * memory space.
  468. */
  469. static int
  470. pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
  471. struct vm_area_struct *vma)
  472. {
  473. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  474. struct device,
  475. kobj));
  476. return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
  477. }
  478. /**
  479. * pci_mmap_legacy_io - map legacy PCI IO into user memory space
  480. * @kobj: kobject corresponding to device to be mapped
  481. * @attr: struct bin_attribute for this file
  482. * @vma: struct vm_area_struct passed to mmap
  483. *
  484. * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
  485. * legacy IO space (first meg of bus space) into application virtual
  486. * memory space. Returns -ENOSYS if the operation isn't supported
  487. */
  488. static int
  489. pci_mmap_legacy_io(struct kobject *kobj, struct bin_attribute *attr,
  490. struct vm_area_struct *vma)
  491. {
  492. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  493. struct device,
  494. kobj));
  495. return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
  496. }
  497. /**
  498. * pci_adjust_legacy_attr - adjustment of legacy file attributes
  499. * @b: bus to create files under
  500. * @mmap_type: I/O port or memory
  501. *
  502. * Stub implementation. Can be overridden by arch if necessary.
  503. */
  504. void __weak
  505. pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
  506. {
  507. return;
  508. }
  509. /**
  510. * pci_create_legacy_files - create legacy I/O port and memory files
  511. * @b: bus to create files under
  512. *
  513. * Some platforms allow access to legacy I/O port and ISA memory space on
  514. * a per-bus basis. This routine creates the files and ties them into
  515. * their associated read, write and mmap files from pci-sysfs.c
  516. *
  517. * On error unwind, but don't propogate the error to the caller
  518. * as it is ok to set up the PCI bus without these files.
  519. */
  520. void pci_create_legacy_files(struct pci_bus *b)
  521. {
  522. int error;
  523. b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
  524. GFP_ATOMIC);
  525. if (!b->legacy_io)
  526. goto kzalloc_err;
  527. b->legacy_io->attr.name = "legacy_io";
  528. b->legacy_io->size = 0xffff;
  529. b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
  530. b->legacy_io->read = pci_read_legacy_io;
  531. b->legacy_io->write = pci_write_legacy_io;
  532. b->legacy_io->mmap = pci_mmap_legacy_io;
  533. pci_adjust_legacy_attr(b, pci_mmap_io);
  534. error = device_create_bin_file(&b->dev, b->legacy_io);
  535. if (error)
  536. goto legacy_io_err;
  537. /* Allocated above after the legacy_io struct */
  538. b->legacy_mem = b->legacy_io + 1;
  539. b->legacy_mem->attr.name = "legacy_mem";
  540. b->legacy_mem->size = 1024*1024;
  541. b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
  542. b->legacy_mem->mmap = pci_mmap_legacy_mem;
  543. pci_adjust_legacy_attr(b, pci_mmap_mem);
  544. error = device_create_bin_file(&b->dev, b->legacy_mem);
  545. if (error)
  546. goto legacy_mem_err;
  547. return;
  548. legacy_mem_err:
  549. device_remove_bin_file(&b->dev, b->legacy_io);
  550. legacy_io_err:
  551. kfree(b->legacy_io);
  552. b->legacy_io = NULL;
  553. kzalloc_err:
  554. printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
  555. "and ISA memory resources to sysfs\n");
  556. return;
  557. }
  558. void pci_remove_legacy_files(struct pci_bus *b)
  559. {
  560. if (b->legacy_io) {
  561. device_remove_bin_file(&b->dev, b->legacy_io);
  562. device_remove_bin_file(&b->dev, b->legacy_mem);
  563. kfree(b->legacy_io); /* both are allocated here */
  564. }
  565. }
  566. #endif /* HAVE_PCI_LEGACY */
  567. #ifdef HAVE_PCI_MMAP
  568. int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma)
  569. {
  570. unsigned long nr, start, size;
  571. nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  572. start = vma->vm_pgoff;
  573. size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
  574. if (start < size && size - start >= nr)
  575. return 1;
  576. WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n",
  577. current->comm, start, start+nr, pci_name(pdev), resno, size);
  578. return 0;
  579. }
  580. /**
  581. * pci_mmap_resource - map a PCI resource into user memory space
  582. * @kobj: kobject for mapping
  583. * @attr: struct bin_attribute for the file being mapped
  584. * @vma: struct vm_area_struct passed into the mmap
  585. * @write_combine: 1 for write_combine mapping
  586. *
  587. * Use the regular PCI mapping routines to map a PCI resource into userspace.
  588. */
  589. static int
  590. pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
  591. struct vm_area_struct *vma, int write_combine)
  592. {
  593. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  594. struct device, kobj));
  595. struct resource *res = (struct resource *)attr->private;
  596. enum pci_mmap_state mmap_type;
  597. resource_size_t start, end;
  598. int i;
  599. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  600. if (res == &pdev->resource[i])
  601. break;
  602. if (i >= PCI_ROM_RESOURCE)
  603. return -ENODEV;
  604. if (!pci_mmap_fits(pdev, i, vma))
  605. return -EINVAL;
  606. /* pci_mmap_page_range() expects the same kind of entry as coming
  607. * from /proc/bus/pci/ which is a "user visible" value. If this is
  608. * different from the resource itself, arch will do necessary fixup.
  609. */
  610. pci_resource_to_user(pdev, i, res, &start, &end);
  611. vma->vm_pgoff += start >> PAGE_SHIFT;
  612. mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
  613. if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
  614. return -EINVAL;
  615. return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
  616. }
  617. static int
  618. pci_mmap_resource_uc(struct kobject *kobj, struct bin_attribute *attr,
  619. struct vm_area_struct *vma)
  620. {
  621. return pci_mmap_resource(kobj, attr, vma, 0);
  622. }
  623. static int
  624. pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr,
  625. struct vm_area_struct *vma)
  626. {
  627. return pci_mmap_resource(kobj, attr, vma, 1);
  628. }
  629. /**
  630. * pci_remove_resource_files - cleanup resource files
  631. * @dev: dev to cleanup
  632. *
  633. * If we created resource files for @dev, remove them from sysfs and
  634. * free their resources.
  635. */
  636. static void
  637. pci_remove_resource_files(struct pci_dev *pdev)
  638. {
  639. int i;
  640. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  641. struct bin_attribute *res_attr;
  642. res_attr = pdev->res_attr[i];
  643. if (res_attr) {
  644. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  645. kfree(res_attr);
  646. }
  647. res_attr = pdev->res_attr_wc[i];
  648. if (res_attr) {
  649. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  650. kfree(res_attr);
  651. }
  652. }
  653. }
  654. static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
  655. {
  656. /* allocate attribute structure, piggyback attribute name */
  657. int name_len = write_combine ? 13 : 10;
  658. struct bin_attribute *res_attr;
  659. int retval;
  660. res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
  661. if (res_attr) {
  662. char *res_attr_name = (char *)(res_attr + 1);
  663. if (write_combine) {
  664. pdev->res_attr_wc[num] = res_attr;
  665. sprintf(res_attr_name, "resource%d_wc", num);
  666. res_attr->mmap = pci_mmap_resource_wc;
  667. } else {
  668. pdev->res_attr[num] = res_attr;
  669. sprintf(res_attr_name, "resource%d", num);
  670. res_attr->mmap = pci_mmap_resource_uc;
  671. }
  672. res_attr->attr.name = res_attr_name;
  673. res_attr->attr.mode = S_IRUSR | S_IWUSR;
  674. res_attr->size = pci_resource_len(pdev, num);
  675. res_attr->private = &pdev->resource[num];
  676. retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
  677. } else
  678. retval = -ENOMEM;
  679. return retval;
  680. }
  681. /**
  682. * pci_create_resource_files - create resource files in sysfs for @dev
  683. * @dev: dev in question
  684. *
  685. * Walk the resources in @dev creating files for each resource available.
  686. */
  687. static int pci_create_resource_files(struct pci_dev *pdev)
  688. {
  689. int i;
  690. int retval;
  691. /* Expose the PCI resources from this device as files */
  692. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  693. /* skip empty resources */
  694. if (!pci_resource_len(pdev, i))
  695. continue;
  696. retval = pci_create_attr(pdev, i, 0);
  697. /* for prefetchable resources, create a WC mappable file */
  698. if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
  699. retval = pci_create_attr(pdev, i, 1);
  700. if (retval) {
  701. pci_remove_resource_files(pdev);
  702. return retval;
  703. }
  704. }
  705. return 0;
  706. }
  707. #else /* !HAVE_PCI_MMAP */
  708. int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
  709. void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
  710. #endif /* HAVE_PCI_MMAP */
  711. /**
  712. * pci_write_rom - used to enable access to the PCI ROM display
  713. * @kobj: kernel object handle
  714. * @buf: user input
  715. * @off: file offset
  716. * @count: number of byte in input
  717. *
  718. * writing anything except 0 enables it
  719. */
  720. static ssize_t
  721. pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
  722. char *buf, loff_t off, size_t count)
  723. {
  724. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  725. if ((off == 0) && (*buf == '0') && (count == 2))
  726. pdev->rom_attr_enabled = 0;
  727. else
  728. pdev->rom_attr_enabled = 1;
  729. return count;
  730. }
  731. /**
  732. * pci_read_rom - read a PCI ROM
  733. * @kobj: kernel object handle
  734. * @buf: where to put the data we read from the ROM
  735. * @off: file offset
  736. * @count: number of bytes to read
  737. *
  738. * Put @count bytes starting at @off into @buf from the ROM in the PCI
  739. * device corresponding to @kobj.
  740. */
  741. static ssize_t
  742. pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
  743. char *buf, loff_t off, size_t count)
  744. {
  745. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  746. void __iomem *rom;
  747. size_t size;
  748. if (!pdev->rom_attr_enabled)
  749. return -EINVAL;
  750. rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
  751. if (!rom || !size)
  752. return -EIO;
  753. if (off >= size)
  754. count = 0;
  755. else {
  756. if (off + count > size)
  757. count = size - off;
  758. memcpy_fromio(buf, rom + off, count);
  759. }
  760. pci_unmap_rom(pdev, rom);
  761. return count;
  762. }
  763. static struct bin_attribute pci_config_attr = {
  764. .attr = {
  765. .name = "config",
  766. .mode = S_IRUGO | S_IWUSR,
  767. },
  768. .size = PCI_CFG_SPACE_SIZE,
  769. .read = pci_read_config,
  770. .write = pci_write_config,
  771. };
  772. static struct bin_attribute pcie_config_attr = {
  773. .attr = {
  774. .name = "config",
  775. .mode = S_IRUGO | S_IWUSR,
  776. },
  777. .size = PCI_CFG_SPACE_EXP_SIZE,
  778. .read = pci_read_config,
  779. .write = pci_write_config,
  780. };
  781. int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
  782. {
  783. return 0;
  784. }
  785. static int pci_create_capabilities_sysfs(struct pci_dev *dev)
  786. {
  787. int retval;
  788. struct bin_attribute *attr;
  789. /* If the device has VPD, try to expose it in sysfs. */
  790. if (dev->vpd) {
  791. attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
  792. if (!attr)
  793. return -ENOMEM;
  794. attr->size = dev->vpd->len;
  795. attr->attr.name = "vpd";
  796. attr->attr.mode = S_IRUSR | S_IWUSR;
  797. attr->read = read_vpd_attr;
  798. attr->write = write_vpd_attr;
  799. retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
  800. if (retval) {
  801. kfree(dev->vpd->attr);
  802. return retval;
  803. }
  804. dev->vpd->attr = attr;
  805. }
  806. /* Active State Power Management */
  807. pcie_aspm_create_sysfs_dev_files(dev);
  808. return 0;
  809. }
  810. int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
  811. {
  812. int retval;
  813. int rom_size = 0;
  814. struct bin_attribute *attr;
  815. if (!sysfs_initialized)
  816. return -EACCES;
  817. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  818. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
  819. else
  820. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  821. if (retval)
  822. goto err;
  823. retval = pci_create_resource_files(pdev);
  824. if (retval)
  825. goto err_config_file;
  826. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  827. rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  828. else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
  829. rom_size = 0x20000;
  830. /* If the device has a ROM, try to expose it in sysfs. */
  831. if (rom_size) {
  832. attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
  833. if (!attr) {
  834. retval = -ENOMEM;
  835. goto err_resource_files;
  836. }
  837. attr->size = rom_size;
  838. attr->attr.name = "rom";
  839. attr->attr.mode = S_IRUSR;
  840. attr->read = pci_read_rom;
  841. attr->write = pci_write_rom;
  842. retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
  843. if (retval) {
  844. kfree(attr);
  845. goto err_resource_files;
  846. }
  847. pdev->rom_attr = attr;
  848. }
  849. if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
  850. retval = device_create_file(&pdev->dev, &vga_attr);
  851. if (retval)
  852. goto err_rom_file;
  853. }
  854. /* add platform-specific attributes */
  855. retval = pcibios_add_platform_entries(pdev);
  856. if (retval)
  857. goto err_vga_file;
  858. /* add sysfs entries for various capabilities */
  859. retval = pci_create_capabilities_sysfs(pdev);
  860. if (retval)
  861. goto err_vga_file;
  862. return 0;
  863. err_vga_file:
  864. if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
  865. device_remove_file(&pdev->dev, &vga_attr);
  866. err_rom_file:
  867. if (rom_size) {
  868. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  869. kfree(pdev->rom_attr);
  870. pdev->rom_attr = NULL;
  871. }
  872. err_resource_files:
  873. pci_remove_resource_files(pdev);
  874. err_config_file:
  875. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  876. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  877. else
  878. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  879. err:
  880. return retval;
  881. }
  882. static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
  883. {
  884. if (dev->vpd && dev->vpd->attr) {
  885. sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
  886. kfree(dev->vpd->attr);
  887. }
  888. pcie_aspm_remove_sysfs_dev_files(dev);
  889. }
  890. /**
  891. * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
  892. * @pdev: device whose entries we should free
  893. *
  894. * Cleanup when @pdev is removed from sysfs.
  895. */
  896. void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
  897. {
  898. int rom_size = 0;
  899. if (!sysfs_initialized)
  900. return;
  901. pci_remove_capabilities_sysfs(pdev);
  902. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  903. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  904. else
  905. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  906. pci_remove_resource_files(pdev);
  907. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  908. rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  909. else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
  910. rom_size = 0x20000;
  911. if (rom_size && pdev->rom_attr) {
  912. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  913. kfree(pdev->rom_attr);
  914. }
  915. }
  916. static int __init pci_sysfs_init(void)
  917. {
  918. struct pci_dev *pdev = NULL;
  919. int retval;
  920. sysfs_initialized = 1;
  921. for_each_pci_dev(pdev) {
  922. retval = pci_create_sysfs_dev_files(pdev);
  923. if (retval) {
  924. pci_dev_put(pdev);
  925. return retval;
  926. }
  927. }
  928. return 0;
  929. }
  930. late_initcall(pci_sysfs_init);