pci-sysfs.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  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/export.h>
  21. #include <linux/topology.h>
  22. #include <linux/mm.h>
  23. #include <linux/fs.h>
  24. #include <linux/capability.h>
  25. #include <linux/security.h>
  26. #include <linux/pci-aspm.h>
  27. #include <linux/slab.h>
  28. #include <linux/vgaarb.h>
  29. #include <linux/pm_runtime.h>
  30. #include "pci.h"
  31. static int sysfs_initialized; /* = 0 */
  32. /* show configuration fields */
  33. #define pci_config_attr(field, format_string) \
  34. static ssize_t \
  35. field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  36. { \
  37. struct pci_dev *pdev; \
  38. \
  39. pdev = to_pci_dev (dev); \
  40. return sprintf (buf, format_string, pdev->field); \
  41. } \
  42. static DEVICE_ATTR_RO(field)
  43. pci_config_attr(vendor, "0x%04x\n");
  44. pci_config_attr(device, "0x%04x\n");
  45. pci_config_attr(subsystem_vendor, "0x%04x\n");
  46. pci_config_attr(subsystem_device, "0x%04x\n");
  47. pci_config_attr(class, "0x%06x\n");
  48. pci_config_attr(irq, "%u\n");
  49. static ssize_t broken_parity_status_show(struct device *dev,
  50. struct device_attribute *attr,
  51. char *buf)
  52. {
  53. struct pci_dev *pdev = to_pci_dev(dev);
  54. return sprintf (buf, "%u\n", pdev->broken_parity_status);
  55. }
  56. static ssize_t broken_parity_status_store(struct device *dev,
  57. struct device_attribute *attr,
  58. const char *buf, size_t count)
  59. {
  60. struct pci_dev *pdev = to_pci_dev(dev);
  61. unsigned long val;
  62. if (kstrtoul(buf, 0, &val) < 0)
  63. return -EINVAL;
  64. pdev->broken_parity_status = !!val;
  65. return count;
  66. }
  67. static DEVICE_ATTR_RW(broken_parity_status);
  68. static ssize_t pci_dev_show_local_cpu(struct device *dev,
  69. int type,
  70. struct device_attribute *attr,
  71. char *buf)
  72. {
  73. const struct cpumask *mask;
  74. int len;
  75. #ifdef CONFIG_NUMA
  76. mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
  77. cpumask_of_node(dev_to_node(dev));
  78. #else
  79. mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
  80. #endif
  81. len = type ?
  82. cpumask_scnprintf(buf, PAGE_SIZE-2, mask) :
  83. cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
  84. buf[len++] = '\n';
  85. buf[len] = '\0';
  86. return len;
  87. }
  88. static ssize_t local_cpus_show(struct device *dev,
  89. struct device_attribute *attr, char *buf)
  90. {
  91. return pci_dev_show_local_cpu(dev, 1, attr, buf);
  92. }
  93. static DEVICE_ATTR_RO(local_cpus);
  94. static ssize_t local_cpulist_show(struct device *dev,
  95. struct device_attribute *attr, char *buf)
  96. {
  97. return pci_dev_show_local_cpu(dev, 0, attr, buf);
  98. }
  99. static DEVICE_ATTR_RO(local_cpulist);
  100. /*
  101. * PCI Bus Class Devices
  102. */
  103. static ssize_t pci_bus_show_cpuaffinity(struct device *dev,
  104. int type,
  105. struct device_attribute *attr,
  106. char *buf)
  107. {
  108. int ret;
  109. const struct cpumask *cpumask;
  110. cpumask = cpumask_of_pcibus(to_pci_bus(dev));
  111. ret = type ?
  112. cpulist_scnprintf(buf, PAGE_SIZE-2, cpumask) :
  113. cpumask_scnprintf(buf, PAGE_SIZE-2, cpumask);
  114. buf[ret++] = '\n';
  115. buf[ret] = '\0';
  116. return ret;
  117. }
  118. static ssize_t cpuaffinity_show(struct device *dev,
  119. struct device_attribute *attr, char *buf)
  120. {
  121. return pci_bus_show_cpuaffinity(dev, 0, attr, buf);
  122. }
  123. static DEVICE_ATTR_RO(cpuaffinity);
  124. static ssize_t cpulistaffinity_show(struct device *dev,
  125. struct device_attribute *attr, char *buf)
  126. {
  127. return pci_bus_show_cpuaffinity(dev, 1, attr, buf);
  128. }
  129. static DEVICE_ATTR_RO(cpulistaffinity);
  130. /* show resources */
  131. static ssize_t
  132. resource_show(struct device * dev, struct device_attribute *attr, char * buf)
  133. {
  134. struct pci_dev * pci_dev = to_pci_dev(dev);
  135. char * str = buf;
  136. int i;
  137. int max;
  138. resource_size_t start, end;
  139. if (pci_dev->subordinate)
  140. max = DEVICE_COUNT_RESOURCE;
  141. else
  142. max = PCI_BRIDGE_RESOURCES;
  143. for (i = 0; i < max; i++) {
  144. struct resource *res = &pci_dev->resource[i];
  145. pci_resource_to_user(pci_dev, i, res, &start, &end);
  146. str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
  147. (unsigned long long)start,
  148. (unsigned long long)end,
  149. (unsigned long long)res->flags);
  150. }
  151. return (str - buf);
  152. }
  153. static DEVICE_ATTR_RO(resource);
  154. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  155. {
  156. struct pci_dev *pci_dev = to_pci_dev(dev);
  157. return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
  158. pci_dev->vendor, pci_dev->device,
  159. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  160. (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
  161. (u8)(pci_dev->class));
  162. }
  163. static DEVICE_ATTR_RO(modalias);
  164. static ssize_t enabled_store(struct device *dev,
  165. struct device_attribute *attr, const char *buf,
  166. size_t count)
  167. {
  168. struct pci_dev *pdev = to_pci_dev(dev);
  169. unsigned long val;
  170. ssize_t result = kstrtoul(buf, 0, &val);
  171. if (result < 0)
  172. return result;
  173. /* this can crash the machine when done on the "wrong" device */
  174. if (!capable(CAP_SYS_ADMIN))
  175. return -EPERM;
  176. if (!val) {
  177. if (pci_is_enabled(pdev))
  178. pci_disable_device(pdev);
  179. else
  180. result = -EIO;
  181. } else
  182. result = pci_enable_device(pdev);
  183. return result < 0 ? result : count;
  184. }
  185. static ssize_t enabled_show(struct device *dev,
  186. struct device_attribute *attr, char *buf)
  187. {
  188. struct pci_dev *pdev;
  189. pdev = to_pci_dev (dev);
  190. return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
  191. }
  192. static DEVICE_ATTR_RW(enabled);
  193. #ifdef CONFIG_NUMA
  194. static ssize_t
  195. numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
  196. {
  197. return sprintf (buf, "%d\n", dev->numa_node);
  198. }
  199. static DEVICE_ATTR_RO(numa_node);
  200. #endif
  201. static ssize_t
  202. dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
  203. {
  204. struct pci_dev *pdev = to_pci_dev(dev);
  205. return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
  206. }
  207. static DEVICE_ATTR_RO(dma_mask_bits);
  208. static ssize_t
  209. consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
  210. char *buf)
  211. {
  212. return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
  213. }
  214. static DEVICE_ATTR_RO(consistent_dma_mask_bits);
  215. static ssize_t
  216. msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
  217. {
  218. struct pci_dev *pdev = to_pci_dev(dev);
  219. if (!pdev->subordinate)
  220. return 0;
  221. return sprintf (buf, "%u\n",
  222. !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
  223. }
  224. static ssize_t
  225. msi_bus_store(struct device *dev, struct device_attribute *attr,
  226. const char *buf, size_t count)
  227. {
  228. struct pci_dev *pdev = to_pci_dev(dev);
  229. unsigned long val;
  230. if (kstrtoul(buf, 0, &val) < 0)
  231. return -EINVAL;
  232. /*
  233. * Bad things may happen if the no_msi flag is changed
  234. * while drivers are loaded.
  235. */
  236. if (!capable(CAP_SYS_ADMIN))
  237. return -EPERM;
  238. /*
  239. * Maybe devices without subordinate buses shouldn't have this
  240. * attribute in the first place?
  241. */
  242. if (!pdev->subordinate)
  243. return count;
  244. /* Is the flag going to change, or keep the value it already had? */
  245. if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
  246. !!val) {
  247. pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
  248. dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
  249. " bad things could happen\n", val ? "" : " not");
  250. }
  251. return count;
  252. }
  253. static DEVICE_ATTR_RW(msi_bus);
  254. static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
  255. size_t count)
  256. {
  257. unsigned long val;
  258. struct pci_bus *b = NULL;
  259. if (kstrtoul(buf, 0, &val) < 0)
  260. return -EINVAL;
  261. if (val) {
  262. pci_lock_rescan_remove();
  263. while ((b = pci_find_next_bus(b)) != NULL)
  264. pci_rescan_bus(b);
  265. pci_unlock_rescan_remove();
  266. }
  267. return count;
  268. }
  269. static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store);
  270. static struct attribute *pci_bus_attrs[] = {
  271. &bus_attr_rescan.attr,
  272. NULL,
  273. };
  274. static const struct attribute_group pci_bus_group = {
  275. .attrs = pci_bus_attrs,
  276. };
  277. const struct attribute_group *pci_bus_groups[] = {
  278. &pci_bus_group,
  279. NULL,
  280. };
  281. static ssize_t
  282. dev_rescan_store(struct device *dev, struct device_attribute *attr,
  283. const char *buf, size_t count)
  284. {
  285. unsigned long val;
  286. struct pci_dev *pdev = to_pci_dev(dev);
  287. if (kstrtoul(buf, 0, &val) < 0)
  288. return -EINVAL;
  289. if (val) {
  290. pci_lock_rescan_remove();
  291. pci_rescan_bus(pdev->bus);
  292. pci_unlock_rescan_remove();
  293. }
  294. return count;
  295. }
  296. static struct device_attribute dev_rescan_attr = __ATTR(rescan,
  297. (S_IWUSR|S_IWGRP),
  298. NULL, dev_rescan_store);
  299. static ssize_t
  300. remove_store(struct device *dev, struct device_attribute *attr,
  301. const char *buf, size_t count)
  302. {
  303. unsigned long val;
  304. if (kstrtoul(buf, 0, &val) < 0)
  305. return -EINVAL;
  306. if (val && device_remove_file_self(dev, attr))
  307. pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
  308. return count;
  309. }
  310. static struct device_attribute dev_remove_attr = __ATTR(remove,
  311. (S_IWUSR|S_IWGRP),
  312. NULL, remove_store);
  313. static ssize_t
  314. dev_bus_rescan_store(struct device *dev, struct device_attribute *attr,
  315. const char *buf, size_t count)
  316. {
  317. unsigned long val;
  318. struct pci_bus *bus = to_pci_bus(dev);
  319. if (kstrtoul(buf, 0, &val) < 0)
  320. return -EINVAL;
  321. if (val) {
  322. pci_lock_rescan_remove();
  323. if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
  324. pci_rescan_bus_bridge_resize(bus->self);
  325. else
  326. pci_rescan_bus(bus);
  327. pci_unlock_rescan_remove();
  328. }
  329. return count;
  330. }
  331. static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
  332. #if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
  333. static ssize_t d3cold_allowed_store(struct device *dev,
  334. struct device_attribute *attr,
  335. const char *buf, size_t count)
  336. {
  337. struct pci_dev *pdev = to_pci_dev(dev);
  338. unsigned long val;
  339. if (kstrtoul(buf, 0, &val) < 0)
  340. return -EINVAL;
  341. pdev->d3cold_allowed = !!val;
  342. pm_runtime_resume(dev);
  343. return count;
  344. }
  345. static ssize_t d3cold_allowed_show(struct device *dev,
  346. struct device_attribute *attr, char *buf)
  347. {
  348. struct pci_dev *pdev = to_pci_dev(dev);
  349. return sprintf (buf, "%u\n", pdev->d3cold_allowed);
  350. }
  351. static DEVICE_ATTR_RW(d3cold_allowed);
  352. #endif
  353. #ifdef CONFIG_PCI_IOV
  354. static ssize_t sriov_totalvfs_show(struct device *dev,
  355. struct device_attribute *attr,
  356. char *buf)
  357. {
  358. struct pci_dev *pdev = to_pci_dev(dev);
  359. return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
  360. }
  361. static ssize_t sriov_numvfs_show(struct device *dev,
  362. struct device_attribute *attr,
  363. char *buf)
  364. {
  365. struct pci_dev *pdev = to_pci_dev(dev);
  366. return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
  367. }
  368. /*
  369. * num_vfs > 0; number of VFs to enable
  370. * num_vfs = 0; disable all VFs
  371. *
  372. * Note: SRIOV spec doesn't allow partial VF
  373. * disable, so it's all or none.
  374. */
  375. static ssize_t sriov_numvfs_store(struct device *dev,
  376. struct device_attribute *attr,
  377. const char *buf, size_t count)
  378. {
  379. struct pci_dev *pdev = to_pci_dev(dev);
  380. int ret;
  381. u16 num_vfs;
  382. ret = kstrtou16(buf, 0, &num_vfs);
  383. if (ret < 0)
  384. return ret;
  385. if (num_vfs > pci_sriov_get_totalvfs(pdev))
  386. return -ERANGE;
  387. if (num_vfs == pdev->sriov->num_VFs)
  388. return count; /* no change */
  389. /* is PF driver loaded w/callback */
  390. if (!pdev->driver || !pdev->driver->sriov_configure) {
  391. dev_info(&pdev->dev, "Driver doesn't support SRIOV configuration via sysfs\n");
  392. return -ENOSYS;
  393. }
  394. if (num_vfs == 0) {
  395. /* disable VFs */
  396. ret = pdev->driver->sriov_configure(pdev, 0);
  397. if (ret < 0)
  398. return ret;
  399. return count;
  400. }
  401. /* enable VFs */
  402. if (pdev->sriov->num_VFs) {
  403. dev_warn(&pdev->dev, "%d VFs already enabled. Disable before enabling %d VFs\n",
  404. pdev->sriov->num_VFs, num_vfs);
  405. return -EBUSY;
  406. }
  407. ret = pdev->driver->sriov_configure(pdev, num_vfs);
  408. if (ret < 0)
  409. return ret;
  410. if (ret != num_vfs)
  411. dev_warn(&pdev->dev, "%d VFs requested; only %d enabled\n",
  412. num_vfs, ret);
  413. return count;
  414. }
  415. static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
  416. static struct device_attribute sriov_numvfs_attr =
  417. __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
  418. sriov_numvfs_show, sriov_numvfs_store);
  419. #endif /* CONFIG_PCI_IOV */
  420. static struct attribute *pci_dev_attrs[] = {
  421. &dev_attr_resource.attr,
  422. &dev_attr_vendor.attr,
  423. &dev_attr_device.attr,
  424. &dev_attr_subsystem_vendor.attr,
  425. &dev_attr_subsystem_device.attr,
  426. &dev_attr_class.attr,
  427. &dev_attr_irq.attr,
  428. &dev_attr_local_cpus.attr,
  429. &dev_attr_local_cpulist.attr,
  430. &dev_attr_modalias.attr,
  431. #ifdef CONFIG_NUMA
  432. &dev_attr_numa_node.attr,
  433. #endif
  434. &dev_attr_dma_mask_bits.attr,
  435. &dev_attr_consistent_dma_mask_bits.attr,
  436. &dev_attr_enabled.attr,
  437. &dev_attr_broken_parity_status.attr,
  438. &dev_attr_msi_bus.attr,
  439. #if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
  440. &dev_attr_d3cold_allowed.attr,
  441. #endif
  442. NULL,
  443. };
  444. static const struct attribute_group pci_dev_group = {
  445. .attrs = pci_dev_attrs,
  446. };
  447. const struct attribute_group *pci_dev_groups[] = {
  448. &pci_dev_group,
  449. NULL,
  450. };
  451. static struct attribute *pcibus_attrs[] = {
  452. &dev_attr_rescan.attr,
  453. &dev_attr_cpuaffinity.attr,
  454. &dev_attr_cpulistaffinity.attr,
  455. NULL,
  456. };
  457. static const struct attribute_group pcibus_group = {
  458. .attrs = pcibus_attrs,
  459. };
  460. const struct attribute_group *pcibus_groups[] = {
  461. &pcibus_group,
  462. NULL,
  463. };
  464. static ssize_t
  465. boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
  466. {
  467. struct pci_dev *pdev = to_pci_dev(dev);
  468. struct pci_dev *vga_dev = vga_default_device();
  469. if (vga_dev)
  470. return sprintf(buf, "%u\n", (pdev == vga_dev));
  471. return sprintf(buf, "%u\n",
  472. !!(pdev->resource[PCI_ROM_RESOURCE].flags &
  473. IORESOURCE_ROM_SHADOW));
  474. }
  475. static struct device_attribute vga_attr = __ATTR_RO(boot_vga);
  476. static ssize_t
  477. pci_read_config(struct file *filp, struct kobject *kobj,
  478. struct bin_attribute *bin_attr,
  479. char *buf, loff_t off, size_t count)
  480. {
  481. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  482. unsigned int size = 64;
  483. loff_t init_off = off;
  484. u8 *data = (u8*) buf;
  485. /* Several chips lock up trying to read undefined config space */
  486. if (security_capable(filp->f_cred, &init_user_ns, CAP_SYS_ADMIN) == 0) {
  487. size = dev->cfg_size;
  488. } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  489. size = 128;
  490. }
  491. if (off > size)
  492. return 0;
  493. if (off + count > size) {
  494. size -= off;
  495. count = size;
  496. } else {
  497. size = count;
  498. }
  499. pci_config_pm_runtime_get(dev);
  500. if ((off & 1) && size) {
  501. u8 val;
  502. pci_user_read_config_byte(dev, off, &val);
  503. data[off - init_off] = val;
  504. off++;
  505. size--;
  506. }
  507. if ((off & 3) && size > 2) {
  508. u16 val;
  509. pci_user_read_config_word(dev, off, &val);
  510. data[off - init_off] = val & 0xff;
  511. data[off - init_off + 1] = (val >> 8) & 0xff;
  512. off += 2;
  513. size -= 2;
  514. }
  515. while (size > 3) {
  516. u32 val;
  517. pci_user_read_config_dword(dev, off, &val);
  518. data[off - init_off] = val & 0xff;
  519. data[off - init_off + 1] = (val >> 8) & 0xff;
  520. data[off - init_off + 2] = (val >> 16) & 0xff;
  521. data[off - init_off + 3] = (val >> 24) & 0xff;
  522. off += 4;
  523. size -= 4;
  524. }
  525. if (size >= 2) {
  526. u16 val;
  527. pci_user_read_config_word(dev, off, &val);
  528. data[off - init_off] = val & 0xff;
  529. data[off - init_off + 1] = (val >> 8) & 0xff;
  530. off += 2;
  531. size -= 2;
  532. }
  533. if (size > 0) {
  534. u8 val;
  535. pci_user_read_config_byte(dev, off, &val);
  536. data[off - init_off] = val;
  537. off++;
  538. --size;
  539. }
  540. pci_config_pm_runtime_put(dev);
  541. return count;
  542. }
  543. static ssize_t
  544. pci_write_config(struct file* filp, struct kobject *kobj,
  545. struct bin_attribute *bin_attr,
  546. char *buf, loff_t off, size_t count)
  547. {
  548. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  549. unsigned int size = count;
  550. loff_t init_off = off;
  551. u8 *data = (u8*) buf;
  552. if (off > dev->cfg_size)
  553. return 0;
  554. if (off + count > dev->cfg_size) {
  555. size = dev->cfg_size - off;
  556. count = size;
  557. }
  558. pci_config_pm_runtime_get(dev);
  559. if ((off & 1) && size) {
  560. pci_user_write_config_byte(dev, off, data[off - init_off]);
  561. off++;
  562. size--;
  563. }
  564. if ((off & 3) && size > 2) {
  565. u16 val = data[off - init_off];
  566. val |= (u16) data[off - init_off + 1] << 8;
  567. pci_user_write_config_word(dev, off, val);
  568. off += 2;
  569. size -= 2;
  570. }
  571. while (size > 3) {
  572. u32 val = data[off - init_off];
  573. val |= (u32) data[off - init_off + 1] << 8;
  574. val |= (u32) data[off - init_off + 2] << 16;
  575. val |= (u32) data[off - init_off + 3] << 24;
  576. pci_user_write_config_dword(dev, off, val);
  577. off += 4;
  578. size -= 4;
  579. }
  580. if (size >= 2) {
  581. u16 val = data[off - init_off];
  582. val |= (u16) data[off - init_off + 1] << 8;
  583. pci_user_write_config_word(dev, off, val);
  584. off += 2;
  585. size -= 2;
  586. }
  587. if (size) {
  588. pci_user_write_config_byte(dev, off, data[off - init_off]);
  589. off++;
  590. --size;
  591. }
  592. pci_config_pm_runtime_put(dev);
  593. return count;
  594. }
  595. static ssize_t
  596. read_vpd_attr(struct file *filp, struct kobject *kobj,
  597. struct bin_attribute *bin_attr,
  598. char *buf, loff_t off, size_t count)
  599. {
  600. struct pci_dev *dev =
  601. to_pci_dev(container_of(kobj, struct device, kobj));
  602. if (off > bin_attr->size)
  603. count = 0;
  604. else if (count > bin_attr->size - off)
  605. count = bin_attr->size - off;
  606. return pci_read_vpd(dev, off, count, buf);
  607. }
  608. static ssize_t
  609. write_vpd_attr(struct file *filp, struct kobject *kobj,
  610. struct bin_attribute *bin_attr,
  611. char *buf, loff_t off, size_t count)
  612. {
  613. struct pci_dev *dev =
  614. to_pci_dev(container_of(kobj, struct device, kobj));
  615. if (off > bin_attr->size)
  616. count = 0;
  617. else if (count > bin_attr->size - off)
  618. count = bin_attr->size - off;
  619. return pci_write_vpd(dev, off, count, buf);
  620. }
  621. #ifdef HAVE_PCI_LEGACY
  622. /**
  623. * pci_read_legacy_io - read byte(s) from legacy I/O port space
  624. * @filp: open sysfs file
  625. * @kobj: kobject corresponding to file to read from
  626. * @bin_attr: struct bin_attribute for this file
  627. * @buf: buffer to store results
  628. * @off: offset into legacy I/O port space
  629. * @count: number of bytes to read
  630. *
  631. * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  632. * callback routine (pci_legacy_read).
  633. */
  634. static ssize_t
  635. pci_read_legacy_io(struct file *filp, struct kobject *kobj,
  636. struct bin_attribute *bin_attr,
  637. char *buf, loff_t off, size_t count)
  638. {
  639. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  640. struct device,
  641. kobj));
  642. /* Only support 1, 2 or 4 byte accesses */
  643. if (count != 1 && count != 2 && count != 4)
  644. return -EINVAL;
  645. return pci_legacy_read(bus, off, (u32 *)buf, count);
  646. }
  647. /**
  648. * pci_write_legacy_io - write byte(s) to legacy I/O port space
  649. * @filp: open sysfs file
  650. * @kobj: kobject corresponding to file to read from
  651. * @bin_attr: struct bin_attribute for this file
  652. * @buf: buffer containing value to be written
  653. * @off: offset into legacy I/O port space
  654. * @count: number of bytes to write
  655. *
  656. * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  657. * callback routine (pci_legacy_write).
  658. */
  659. static ssize_t
  660. pci_write_legacy_io(struct file *filp, struct kobject *kobj,
  661. struct bin_attribute *bin_attr,
  662. char *buf, loff_t off, size_t count)
  663. {
  664. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  665. struct device,
  666. kobj));
  667. /* Only support 1, 2 or 4 byte accesses */
  668. if (count != 1 && count != 2 && count != 4)
  669. return -EINVAL;
  670. return pci_legacy_write(bus, off, *(u32 *)buf, count);
  671. }
  672. /**
  673. * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
  674. * @filp: open sysfs file
  675. * @kobj: kobject corresponding to device to be mapped
  676. * @attr: struct bin_attribute for this file
  677. * @vma: struct vm_area_struct passed to mmap
  678. *
  679. * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
  680. * legacy memory space (first meg of bus space) into application virtual
  681. * memory space.
  682. */
  683. static int
  684. pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
  685. struct bin_attribute *attr,
  686. struct vm_area_struct *vma)
  687. {
  688. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  689. struct device,
  690. kobj));
  691. return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
  692. }
  693. /**
  694. * pci_mmap_legacy_io - map legacy PCI IO into user memory space
  695. * @filp: open sysfs file
  696. * @kobj: kobject corresponding to device to be mapped
  697. * @attr: struct bin_attribute for this file
  698. * @vma: struct vm_area_struct passed to mmap
  699. *
  700. * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
  701. * legacy IO space (first meg of bus space) into application virtual
  702. * memory space. Returns -ENOSYS if the operation isn't supported
  703. */
  704. static int
  705. pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
  706. struct bin_attribute *attr,
  707. struct vm_area_struct *vma)
  708. {
  709. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  710. struct device,
  711. kobj));
  712. return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
  713. }
  714. /**
  715. * pci_adjust_legacy_attr - adjustment of legacy file attributes
  716. * @b: bus to create files under
  717. * @mmap_type: I/O port or memory
  718. *
  719. * Stub implementation. Can be overridden by arch if necessary.
  720. */
  721. void __weak
  722. pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
  723. {
  724. return;
  725. }
  726. /**
  727. * pci_create_legacy_files - create legacy I/O port and memory files
  728. * @b: bus to create files under
  729. *
  730. * Some platforms allow access to legacy I/O port and ISA memory space on
  731. * a per-bus basis. This routine creates the files and ties them into
  732. * their associated read, write and mmap files from pci-sysfs.c
  733. *
  734. * On error unwind, but don't propagate the error to the caller
  735. * as it is ok to set up the PCI bus without these files.
  736. */
  737. void pci_create_legacy_files(struct pci_bus *b)
  738. {
  739. int error;
  740. b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
  741. GFP_ATOMIC);
  742. if (!b->legacy_io)
  743. goto kzalloc_err;
  744. sysfs_bin_attr_init(b->legacy_io);
  745. b->legacy_io->attr.name = "legacy_io";
  746. b->legacy_io->size = 0xffff;
  747. b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
  748. b->legacy_io->read = pci_read_legacy_io;
  749. b->legacy_io->write = pci_write_legacy_io;
  750. b->legacy_io->mmap = pci_mmap_legacy_io;
  751. pci_adjust_legacy_attr(b, pci_mmap_io);
  752. error = device_create_bin_file(&b->dev, b->legacy_io);
  753. if (error)
  754. goto legacy_io_err;
  755. /* Allocated above after the legacy_io struct */
  756. b->legacy_mem = b->legacy_io + 1;
  757. sysfs_bin_attr_init(b->legacy_mem);
  758. b->legacy_mem->attr.name = "legacy_mem";
  759. b->legacy_mem->size = 1024*1024;
  760. b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
  761. b->legacy_mem->mmap = pci_mmap_legacy_mem;
  762. pci_adjust_legacy_attr(b, pci_mmap_mem);
  763. error = device_create_bin_file(&b->dev, b->legacy_mem);
  764. if (error)
  765. goto legacy_mem_err;
  766. return;
  767. legacy_mem_err:
  768. device_remove_bin_file(&b->dev, b->legacy_io);
  769. legacy_io_err:
  770. kfree(b->legacy_io);
  771. b->legacy_io = NULL;
  772. kzalloc_err:
  773. printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
  774. "and ISA memory resources to sysfs\n");
  775. return;
  776. }
  777. void pci_remove_legacy_files(struct pci_bus *b)
  778. {
  779. if (b->legacy_io) {
  780. device_remove_bin_file(&b->dev, b->legacy_io);
  781. device_remove_bin_file(&b->dev, b->legacy_mem);
  782. kfree(b->legacy_io); /* both are allocated here */
  783. }
  784. }
  785. #endif /* HAVE_PCI_LEGACY */
  786. #ifdef HAVE_PCI_MMAP
  787. int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
  788. enum pci_mmap_api mmap_api)
  789. {
  790. unsigned long nr, start, size, pci_start;
  791. if (pci_resource_len(pdev, resno) == 0)
  792. return 0;
  793. nr = vma_pages(vma);
  794. start = vma->vm_pgoff;
  795. size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
  796. pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
  797. pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
  798. if (start >= pci_start && start < pci_start + size &&
  799. start + nr <= pci_start + size)
  800. return 1;
  801. return 0;
  802. }
  803. /**
  804. * pci_mmap_resource - map a PCI resource into user memory space
  805. * @kobj: kobject for mapping
  806. * @attr: struct bin_attribute for the file being mapped
  807. * @vma: struct vm_area_struct passed into the mmap
  808. * @write_combine: 1 for write_combine mapping
  809. *
  810. * Use the regular PCI mapping routines to map a PCI resource into userspace.
  811. */
  812. static int
  813. pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
  814. struct vm_area_struct *vma, int write_combine)
  815. {
  816. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  817. struct device, kobj));
  818. struct resource *res = attr->private;
  819. enum pci_mmap_state mmap_type;
  820. resource_size_t start, end;
  821. int i;
  822. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  823. if (res == &pdev->resource[i])
  824. break;
  825. if (i >= PCI_ROM_RESOURCE)
  826. return -ENODEV;
  827. if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
  828. WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
  829. "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
  830. current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
  831. pci_name(pdev), i,
  832. (u64)pci_resource_start(pdev, i),
  833. (u64)pci_resource_len(pdev, i));
  834. return -EINVAL;
  835. }
  836. /* pci_mmap_page_range() expects the same kind of entry as coming
  837. * from /proc/bus/pci/ which is a "user visible" value. If this is
  838. * different from the resource itself, arch will do necessary fixup.
  839. */
  840. pci_resource_to_user(pdev, i, res, &start, &end);
  841. vma->vm_pgoff += start >> PAGE_SHIFT;
  842. mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
  843. if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
  844. return -EINVAL;
  845. return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
  846. }
  847. static int
  848. pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
  849. struct bin_attribute *attr,
  850. struct vm_area_struct *vma)
  851. {
  852. return pci_mmap_resource(kobj, attr, vma, 0);
  853. }
  854. static int
  855. pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
  856. struct bin_attribute *attr,
  857. struct vm_area_struct *vma)
  858. {
  859. return pci_mmap_resource(kobj, attr, vma, 1);
  860. }
  861. static ssize_t
  862. pci_resource_io(struct file *filp, struct kobject *kobj,
  863. struct bin_attribute *attr, char *buf,
  864. loff_t off, size_t count, bool write)
  865. {
  866. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  867. struct device, kobj));
  868. struct resource *res = attr->private;
  869. unsigned long port = off;
  870. int i;
  871. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  872. if (res == &pdev->resource[i])
  873. break;
  874. if (i >= PCI_ROM_RESOURCE)
  875. return -ENODEV;
  876. port += pci_resource_start(pdev, i);
  877. if (port > pci_resource_end(pdev, i))
  878. return 0;
  879. if (port + count - 1 > pci_resource_end(pdev, i))
  880. return -EINVAL;
  881. switch (count) {
  882. case 1:
  883. if (write)
  884. outb(*(u8 *)buf, port);
  885. else
  886. *(u8 *)buf = inb(port);
  887. return 1;
  888. case 2:
  889. if (write)
  890. outw(*(u16 *)buf, port);
  891. else
  892. *(u16 *)buf = inw(port);
  893. return 2;
  894. case 4:
  895. if (write)
  896. outl(*(u32 *)buf, port);
  897. else
  898. *(u32 *)buf = inl(port);
  899. return 4;
  900. }
  901. return -EINVAL;
  902. }
  903. static ssize_t
  904. pci_read_resource_io(struct file *filp, struct kobject *kobj,
  905. struct bin_attribute *attr, char *buf,
  906. loff_t off, size_t count)
  907. {
  908. return pci_resource_io(filp, kobj, attr, buf, off, count, false);
  909. }
  910. static ssize_t
  911. pci_write_resource_io(struct file *filp, struct kobject *kobj,
  912. struct bin_attribute *attr, char *buf,
  913. loff_t off, size_t count)
  914. {
  915. return pci_resource_io(filp, kobj, attr, buf, off, count, true);
  916. }
  917. /**
  918. * pci_remove_resource_files - cleanup resource files
  919. * @pdev: dev to cleanup
  920. *
  921. * If we created resource files for @pdev, remove them from sysfs and
  922. * free their resources.
  923. */
  924. static void
  925. pci_remove_resource_files(struct pci_dev *pdev)
  926. {
  927. int i;
  928. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  929. struct bin_attribute *res_attr;
  930. res_attr = pdev->res_attr[i];
  931. if (res_attr) {
  932. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  933. kfree(res_attr);
  934. }
  935. res_attr = pdev->res_attr_wc[i];
  936. if (res_attr) {
  937. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  938. kfree(res_attr);
  939. }
  940. }
  941. }
  942. static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
  943. {
  944. /* allocate attribute structure, piggyback attribute name */
  945. int name_len = write_combine ? 13 : 10;
  946. struct bin_attribute *res_attr;
  947. int retval;
  948. res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
  949. if (res_attr) {
  950. char *res_attr_name = (char *)(res_attr + 1);
  951. sysfs_bin_attr_init(res_attr);
  952. if (write_combine) {
  953. pdev->res_attr_wc[num] = res_attr;
  954. sprintf(res_attr_name, "resource%d_wc", num);
  955. res_attr->mmap = pci_mmap_resource_wc;
  956. } else {
  957. pdev->res_attr[num] = res_attr;
  958. sprintf(res_attr_name, "resource%d", num);
  959. res_attr->mmap = pci_mmap_resource_uc;
  960. }
  961. if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
  962. res_attr->read = pci_read_resource_io;
  963. res_attr->write = pci_write_resource_io;
  964. }
  965. res_attr->attr.name = res_attr_name;
  966. res_attr->attr.mode = S_IRUSR | S_IWUSR;
  967. res_attr->size = pci_resource_len(pdev, num);
  968. res_attr->private = &pdev->resource[num];
  969. retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
  970. } else
  971. retval = -ENOMEM;
  972. return retval;
  973. }
  974. /**
  975. * pci_create_resource_files - create resource files in sysfs for @dev
  976. * @pdev: dev in question
  977. *
  978. * Walk the resources in @pdev creating files for each resource available.
  979. */
  980. static int pci_create_resource_files(struct pci_dev *pdev)
  981. {
  982. int i;
  983. int retval;
  984. /* Expose the PCI resources from this device as files */
  985. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  986. /* skip empty resources */
  987. if (!pci_resource_len(pdev, i))
  988. continue;
  989. retval = pci_create_attr(pdev, i, 0);
  990. /* for prefetchable resources, create a WC mappable file */
  991. if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
  992. retval = pci_create_attr(pdev, i, 1);
  993. if (retval) {
  994. pci_remove_resource_files(pdev);
  995. return retval;
  996. }
  997. }
  998. return 0;
  999. }
  1000. #else /* !HAVE_PCI_MMAP */
  1001. int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
  1002. void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
  1003. #endif /* HAVE_PCI_MMAP */
  1004. /**
  1005. * pci_write_rom - used to enable access to the PCI ROM display
  1006. * @filp: sysfs file
  1007. * @kobj: kernel object handle
  1008. * @bin_attr: struct bin_attribute for this file
  1009. * @buf: user input
  1010. * @off: file offset
  1011. * @count: number of byte in input
  1012. *
  1013. * writing anything except 0 enables it
  1014. */
  1015. static ssize_t
  1016. pci_write_rom(struct file *filp, struct kobject *kobj,
  1017. struct bin_attribute *bin_attr,
  1018. char *buf, loff_t off, size_t count)
  1019. {
  1020. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  1021. if ((off == 0) && (*buf == '0') && (count == 2))
  1022. pdev->rom_attr_enabled = 0;
  1023. else
  1024. pdev->rom_attr_enabled = 1;
  1025. return count;
  1026. }
  1027. /**
  1028. * pci_read_rom - read a PCI ROM
  1029. * @filp: sysfs file
  1030. * @kobj: kernel object handle
  1031. * @bin_attr: struct bin_attribute for this file
  1032. * @buf: where to put the data we read from the ROM
  1033. * @off: file offset
  1034. * @count: number of bytes to read
  1035. *
  1036. * Put @count bytes starting at @off into @buf from the ROM in the PCI
  1037. * device corresponding to @kobj.
  1038. */
  1039. static ssize_t
  1040. pci_read_rom(struct file *filp, struct kobject *kobj,
  1041. struct bin_attribute *bin_attr,
  1042. char *buf, loff_t off, size_t count)
  1043. {
  1044. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  1045. void __iomem *rom;
  1046. size_t size;
  1047. if (!pdev->rom_attr_enabled)
  1048. return -EINVAL;
  1049. rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
  1050. if (!rom || !size)
  1051. return -EIO;
  1052. if (off >= size)
  1053. count = 0;
  1054. else {
  1055. if (off + count > size)
  1056. count = size - off;
  1057. memcpy_fromio(buf, rom + off, count);
  1058. }
  1059. pci_unmap_rom(pdev, rom);
  1060. return count;
  1061. }
  1062. static struct bin_attribute pci_config_attr = {
  1063. .attr = {
  1064. .name = "config",
  1065. .mode = S_IRUGO | S_IWUSR,
  1066. },
  1067. .size = PCI_CFG_SPACE_SIZE,
  1068. .read = pci_read_config,
  1069. .write = pci_write_config,
  1070. };
  1071. static struct bin_attribute pcie_config_attr = {
  1072. .attr = {
  1073. .name = "config",
  1074. .mode = S_IRUGO | S_IWUSR,
  1075. },
  1076. .size = PCI_CFG_SPACE_EXP_SIZE,
  1077. .read = pci_read_config,
  1078. .write = pci_write_config,
  1079. };
  1080. int __weak pcibios_add_platform_entries(struct pci_dev *dev)
  1081. {
  1082. return 0;
  1083. }
  1084. static ssize_t reset_store(struct device *dev,
  1085. struct device_attribute *attr, const char *buf,
  1086. size_t count)
  1087. {
  1088. struct pci_dev *pdev = to_pci_dev(dev);
  1089. unsigned long val;
  1090. ssize_t result = kstrtoul(buf, 0, &val);
  1091. if (result < 0)
  1092. return result;
  1093. if (val != 1)
  1094. return -EINVAL;
  1095. result = pci_reset_function(pdev);
  1096. if (result < 0)
  1097. return result;
  1098. return count;
  1099. }
  1100. static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
  1101. static int pci_create_capabilities_sysfs(struct pci_dev *dev)
  1102. {
  1103. int retval;
  1104. struct bin_attribute *attr;
  1105. /* If the device has VPD, try to expose it in sysfs. */
  1106. if (dev->vpd) {
  1107. attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
  1108. if (!attr)
  1109. return -ENOMEM;
  1110. sysfs_bin_attr_init(attr);
  1111. attr->size = dev->vpd->len;
  1112. attr->attr.name = "vpd";
  1113. attr->attr.mode = S_IRUSR | S_IWUSR;
  1114. attr->read = read_vpd_attr;
  1115. attr->write = write_vpd_attr;
  1116. retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
  1117. if (retval) {
  1118. kfree(attr);
  1119. return retval;
  1120. }
  1121. dev->vpd->attr = attr;
  1122. }
  1123. /* Active State Power Management */
  1124. pcie_aspm_create_sysfs_dev_files(dev);
  1125. if (!pci_probe_reset_function(dev)) {
  1126. retval = device_create_file(&dev->dev, &reset_attr);
  1127. if (retval)
  1128. goto error;
  1129. dev->reset_fn = 1;
  1130. }
  1131. return 0;
  1132. error:
  1133. pcie_aspm_remove_sysfs_dev_files(dev);
  1134. if (dev->vpd && dev->vpd->attr) {
  1135. sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
  1136. kfree(dev->vpd->attr);
  1137. }
  1138. return retval;
  1139. }
  1140. int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
  1141. {
  1142. int retval;
  1143. int rom_size = 0;
  1144. struct bin_attribute *attr;
  1145. if (!sysfs_initialized)
  1146. return -EACCES;
  1147. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  1148. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
  1149. else
  1150. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  1151. if (retval)
  1152. goto err;
  1153. retval = pci_create_resource_files(pdev);
  1154. if (retval)
  1155. goto err_config_file;
  1156. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  1157. rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  1158. else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
  1159. rom_size = 0x20000;
  1160. /* If the device has a ROM, try to expose it in sysfs. */
  1161. if (rom_size) {
  1162. attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
  1163. if (!attr) {
  1164. retval = -ENOMEM;
  1165. goto err_resource_files;
  1166. }
  1167. sysfs_bin_attr_init(attr);
  1168. attr->size = rom_size;
  1169. attr->attr.name = "rom";
  1170. attr->attr.mode = S_IRUSR | S_IWUSR;
  1171. attr->read = pci_read_rom;
  1172. attr->write = pci_write_rom;
  1173. retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
  1174. if (retval) {
  1175. kfree(attr);
  1176. goto err_resource_files;
  1177. }
  1178. pdev->rom_attr = attr;
  1179. }
  1180. /* add platform-specific attributes */
  1181. retval = pcibios_add_platform_entries(pdev);
  1182. if (retval)
  1183. goto err_rom_file;
  1184. /* add sysfs entries for various capabilities */
  1185. retval = pci_create_capabilities_sysfs(pdev);
  1186. if (retval)
  1187. goto err_rom_file;
  1188. pci_create_firmware_label_files(pdev);
  1189. return 0;
  1190. err_rom_file:
  1191. if (rom_size) {
  1192. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  1193. kfree(pdev->rom_attr);
  1194. pdev->rom_attr = NULL;
  1195. }
  1196. err_resource_files:
  1197. pci_remove_resource_files(pdev);
  1198. err_config_file:
  1199. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  1200. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  1201. else
  1202. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  1203. err:
  1204. return retval;
  1205. }
  1206. static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
  1207. {
  1208. if (dev->vpd && dev->vpd->attr) {
  1209. sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
  1210. kfree(dev->vpd->attr);
  1211. }
  1212. pcie_aspm_remove_sysfs_dev_files(dev);
  1213. if (dev->reset_fn) {
  1214. device_remove_file(&dev->dev, &reset_attr);
  1215. dev->reset_fn = 0;
  1216. }
  1217. }
  1218. /**
  1219. * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
  1220. * @pdev: device whose entries we should free
  1221. *
  1222. * Cleanup when @pdev is removed from sysfs.
  1223. */
  1224. void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
  1225. {
  1226. int rom_size = 0;
  1227. if (!sysfs_initialized)
  1228. return;
  1229. pci_remove_capabilities_sysfs(pdev);
  1230. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  1231. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  1232. else
  1233. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  1234. pci_remove_resource_files(pdev);
  1235. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  1236. rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  1237. else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
  1238. rom_size = 0x20000;
  1239. if (rom_size && pdev->rom_attr) {
  1240. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  1241. kfree(pdev->rom_attr);
  1242. }
  1243. pci_remove_firmware_label_files(pdev);
  1244. }
  1245. static int __init pci_sysfs_init(void)
  1246. {
  1247. struct pci_dev *pdev = NULL;
  1248. int retval;
  1249. sysfs_initialized = 1;
  1250. for_each_pci_dev(pdev) {
  1251. retval = pci_create_sysfs_dev_files(pdev);
  1252. if (retval) {
  1253. pci_dev_put(pdev);
  1254. return retval;
  1255. }
  1256. }
  1257. return 0;
  1258. }
  1259. late_initcall(pci_sysfs_init);
  1260. static struct attribute *pci_dev_dev_attrs[] = {
  1261. &vga_attr.attr,
  1262. NULL,
  1263. };
  1264. static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
  1265. struct attribute *a, int n)
  1266. {
  1267. struct device *dev = container_of(kobj, struct device, kobj);
  1268. struct pci_dev *pdev = to_pci_dev(dev);
  1269. if (a == &vga_attr.attr)
  1270. if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  1271. return 0;
  1272. return a->mode;
  1273. }
  1274. static struct attribute *pci_dev_hp_attrs[] = {
  1275. &dev_remove_attr.attr,
  1276. &dev_rescan_attr.attr,
  1277. NULL,
  1278. };
  1279. static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
  1280. struct attribute *a, int n)
  1281. {
  1282. struct device *dev = container_of(kobj, struct device, kobj);
  1283. struct pci_dev *pdev = to_pci_dev(dev);
  1284. if (pdev->is_virtfn)
  1285. return 0;
  1286. return a->mode;
  1287. }
  1288. static struct attribute_group pci_dev_hp_attr_group = {
  1289. .attrs = pci_dev_hp_attrs,
  1290. .is_visible = pci_dev_hp_attrs_are_visible,
  1291. };
  1292. #ifdef CONFIG_PCI_IOV
  1293. static struct attribute *sriov_dev_attrs[] = {
  1294. &sriov_totalvfs_attr.attr,
  1295. &sriov_numvfs_attr.attr,
  1296. NULL,
  1297. };
  1298. static umode_t sriov_attrs_are_visible(struct kobject *kobj,
  1299. struct attribute *a, int n)
  1300. {
  1301. struct device *dev = container_of(kobj, struct device, kobj);
  1302. if (!dev_is_pf(dev))
  1303. return 0;
  1304. return a->mode;
  1305. }
  1306. static struct attribute_group sriov_dev_attr_group = {
  1307. .attrs = sriov_dev_attrs,
  1308. .is_visible = sriov_attrs_are_visible,
  1309. };
  1310. #endif /* CONFIG_PCI_IOV */
  1311. static struct attribute_group pci_dev_attr_group = {
  1312. .attrs = pci_dev_dev_attrs,
  1313. .is_visible = pci_dev_attrs_are_visible,
  1314. };
  1315. static const struct attribute_group *pci_dev_attr_groups[] = {
  1316. &pci_dev_attr_group,
  1317. &pci_dev_hp_attr_group,
  1318. #ifdef CONFIG_PCI_IOV
  1319. &sriov_dev_attr_group,
  1320. #endif
  1321. NULL,
  1322. };
  1323. struct device_type pci_dev_type = {
  1324. .groups = pci_dev_attr_groups,
  1325. };