pci-sysfs.c 39 KB

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