portdrv_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * File: portdrv_core.c
  3. * Purpose: PCI Express Port Bus Driver's Core Functions
  4. *
  5. * Copyright (C) 2004 Intel
  6. * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
  7. */
  8. #include <linux/module.h>
  9. #include <linux/pci.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/pm.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/string.h>
  15. #include <linux/slab.h>
  16. #include <linux/pcieport_if.h>
  17. #include <linux/aer.h>
  18. #include "../pci.h"
  19. #include "portdrv.h"
  20. bool pciehp_msi_disabled;
  21. static int __init pciehp_setup(char *str)
  22. {
  23. if (!strncmp(str, "nomsi", 5))
  24. pciehp_msi_disabled = true;
  25. return 1;
  26. }
  27. __setup("pcie_hp=", pciehp_setup);
  28. /**
  29. * release_pcie_device - free PCI Express port service device structure
  30. * @dev: Port service device to release
  31. *
  32. * Invoked automatically when device is being removed in response to
  33. * device_unregister(dev). Release all resources being claimed.
  34. */
  35. static void release_pcie_device(struct device *dev)
  36. {
  37. kfree(to_pcie_device(dev));
  38. }
  39. /**
  40. * pcie_port_enable_msix - try to set up MSI-X as interrupt mode for given port
  41. * @dev: PCI Express port to handle
  42. * @irqs: Array of interrupt vectors to populate
  43. * @mask: Bitmask of port capabilities returned by get_port_device_capability()
  44. *
  45. * Return value: 0 on success, error code on failure
  46. */
  47. static int pcie_port_enable_msix(struct pci_dev *dev, int *irqs, int mask)
  48. {
  49. int nr_entries, entry, nvec = 0;
  50. /*
  51. * Allocate as many entries as the port wants, so that we can check
  52. * which of them will be useful. Moreover, if nr_entries is correctly
  53. * equal to the number of entries this port actually uses, we'll happily
  54. * go through without any tricks.
  55. */
  56. nr_entries = pci_alloc_irq_vectors(dev, 1, PCIE_PORT_MAX_MSIX_ENTRIES,
  57. PCI_IRQ_MSIX);
  58. if (nr_entries < 0)
  59. return nr_entries;
  60. if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
  61. u16 reg16;
  62. /*
  63. * The code below follows the PCI Express Base Specification 2.0
  64. * stating in Section 6.1.6 that "PME and Hot-Plug Event
  65. * interrupts (when both are implemented) always share the same
  66. * MSI or MSI-X vector, as indicated by the Interrupt Message
  67. * Number field in the PCI Express Capabilities register", where
  68. * according to Section 7.8.2 of the specification "For MSI-X,
  69. * the value in this field indicates which MSI-X Table entry is
  70. * used to generate the interrupt message."
  71. */
  72. pcie_capability_read_word(dev, PCI_EXP_FLAGS, &reg16);
  73. entry = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
  74. if (entry >= nr_entries)
  75. goto out_free_irqs;
  76. irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pci_irq_vector(dev, entry);
  77. irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pci_irq_vector(dev, entry);
  78. nvec = max(nvec, entry + 1);
  79. }
  80. if (mask & PCIE_PORT_SERVICE_AER) {
  81. u32 reg32, pos;
  82. /*
  83. * The code below follows Section 7.10.10 of the PCI Express
  84. * Base Specification 2.0 stating that bits 31-27 of the Root
  85. * Error Status Register contain a value indicating which of the
  86. * MSI/MSI-X vectors assigned to the port is going to be used
  87. * for AER, where "For MSI-X, the value in this register
  88. * indicates which MSI-X Table entry is used to generate the
  89. * interrupt message."
  90. */
  91. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  92. pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
  93. entry = reg32 >> 27;
  94. if (entry >= nr_entries)
  95. goto out_free_irqs;
  96. irqs[PCIE_PORT_SERVICE_AER_SHIFT] = pci_irq_vector(dev, entry);
  97. nvec = max(nvec, entry + 1);
  98. }
  99. /*
  100. * If nvec is equal to the allocated number of entries, we can just use
  101. * what we have. Otherwise, the port has some extra entries not for the
  102. * services we know and we need to work around that.
  103. */
  104. if (nvec != nr_entries) {
  105. /* Drop the temporary MSI-X setup */
  106. pci_free_irq_vectors(dev);
  107. /* Now allocate the MSI-X vectors for real */
  108. nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
  109. PCI_IRQ_MSIX);
  110. if (nr_entries < 0)
  111. return nr_entries;
  112. }
  113. return 0;
  114. out_free_irqs:
  115. pci_free_irq_vectors(dev);
  116. return -EIO;
  117. }
  118. /**
  119. * pcie_init_service_irqs - initialize irqs for PCI Express port services
  120. * @dev: PCI Express port to handle
  121. * @irqs: Array of irqs to populate
  122. * @mask: Bitmask of port capabilities returned by get_port_device_capability()
  123. *
  124. * Return value: Interrupt mode associated with the port
  125. */
  126. static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
  127. {
  128. unsigned flags = PCI_IRQ_LEGACY | PCI_IRQ_MSI;
  129. int ret, i;
  130. for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
  131. irqs[i] = -1;
  132. /*
  133. * If MSI cannot be used for PCIe PME or hotplug, we have to use
  134. * INTx or other interrupts, e.g. system shared interrupt.
  135. */
  136. if (((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi()) ||
  137. ((mask & PCIE_PORT_SERVICE_HP) && pciehp_no_msi())) {
  138. flags &= ~PCI_IRQ_MSI;
  139. } else {
  140. /* Try to use MSI-X if supported */
  141. if (!pcie_port_enable_msix(dev, irqs, mask))
  142. return 0;
  143. }
  144. ret = pci_alloc_irq_vectors(dev, 1, 1, flags);
  145. if (ret < 0)
  146. return -ENODEV;
  147. for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
  148. if (i != PCIE_PORT_SERVICE_VC_SHIFT)
  149. irqs[i] = pci_irq_vector(dev, 0);
  150. }
  151. return 0;
  152. }
  153. /**
  154. * get_port_device_capability - discover capabilities of a PCI Express port
  155. * @dev: PCI Express port to examine
  156. *
  157. * The capabilities are read from the port's PCI Express configuration registers
  158. * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
  159. * 7.9 - 7.11.
  160. *
  161. * Return value: Bitmask of discovered port capabilities
  162. */
  163. static int get_port_device_capability(struct pci_dev *dev)
  164. {
  165. int services = 0;
  166. int cap_mask = 0;
  167. if (pcie_ports_disabled)
  168. return 0;
  169. cap_mask = PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP
  170. | PCIE_PORT_SERVICE_VC | PCIE_PORT_SERVICE_DPC;
  171. if (pci_aer_available())
  172. cap_mask |= PCIE_PORT_SERVICE_AER;
  173. if (pcie_ports_auto)
  174. pcie_port_platform_notify(dev, &cap_mask);
  175. /* Hot-Plug Capable */
  176. if ((cap_mask & PCIE_PORT_SERVICE_HP) && dev->is_hotplug_bridge) {
  177. services |= PCIE_PORT_SERVICE_HP;
  178. /*
  179. * Disable hot-plug interrupts in case they have been enabled
  180. * by the BIOS and the hot-plug service driver is not loaded.
  181. */
  182. pcie_capability_clear_word(dev, PCI_EXP_SLTCTL,
  183. PCI_EXP_SLTCTL_CCIE | PCI_EXP_SLTCTL_HPIE);
  184. }
  185. /* AER capable */
  186. if ((cap_mask & PCIE_PORT_SERVICE_AER)
  187. && pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR)) {
  188. services |= PCIE_PORT_SERVICE_AER;
  189. /*
  190. * Disable AER on this port in case it's been enabled by the
  191. * BIOS (the AER service driver will enable it when necessary).
  192. */
  193. pci_disable_pcie_error_reporting(dev);
  194. }
  195. /* VC support */
  196. if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_VC))
  197. services |= PCIE_PORT_SERVICE_VC;
  198. /* Root ports are capable of generating PME too */
  199. if ((cap_mask & PCIE_PORT_SERVICE_PME)
  200. && pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
  201. services |= PCIE_PORT_SERVICE_PME;
  202. /*
  203. * Disable PME interrupt on this port in case it's been enabled
  204. * by the BIOS (the PME service driver will enable it when
  205. * necessary).
  206. */
  207. pcie_pme_interrupt_enable(dev, false);
  208. }
  209. if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC))
  210. services |= PCIE_PORT_SERVICE_DPC;
  211. return services;
  212. }
  213. /**
  214. * pcie_device_init - allocate and initialize PCI Express port service device
  215. * @pdev: PCI Express port to associate the service device with
  216. * @service: Type of service to associate with the service device
  217. * @irq: Interrupt vector to associate with the service device
  218. */
  219. static int pcie_device_init(struct pci_dev *pdev, int service, int irq)
  220. {
  221. int retval;
  222. struct pcie_device *pcie;
  223. struct device *device;
  224. pcie = kzalloc(sizeof(*pcie), GFP_KERNEL);
  225. if (!pcie)
  226. return -ENOMEM;
  227. pcie->port = pdev;
  228. pcie->irq = irq;
  229. pcie->service = service;
  230. /* Initialize generic device interface */
  231. device = &pcie->device;
  232. device->bus = &pcie_port_bus_type;
  233. device->release = release_pcie_device; /* callback to free pcie dev */
  234. dev_set_name(device, "%s:pcie%03x",
  235. pci_name(pdev),
  236. get_descriptor_id(pci_pcie_type(pdev), service));
  237. device->parent = &pdev->dev;
  238. device_enable_async_suspend(device);
  239. retval = device_register(device);
  240. if (retval) {
  241. put_device(device);
  242. return retval;
  243. }
  244. pm_runtime_no_callbacks(device);
  245. return 0;
  246. }
  247. /**
  248. * pcie_port_device_register - register PCI Express port
  249. * @dev: PCI Express port to register
  250. *
  251. * Allocate the port extension structure and register services associated with
  252. * the port.
  253. */
  254. int pcie_port_device_register(struct pci_dev *dev)
  255. {
  256. int status, capabilities, i, nr_service;
  257. int irqs[PCIE_PORT_DEVICE_MAXSERVICES];
  258. /* Enable PCI Express port device */
  259. status = pci_enable_device(dev);
  260. if (status)
  261. return status;
  262. /* Get and check PCI Express port services */
  263. capabilities = get_port_device_capability(dev);
  264. if (!capabilities)
  265. return 0;
  266. pci_set_master(dev);
  267. /*
  268. * Initialize service irqs. Don't use service devices that
  269. * require interrupts if there is no way to generate them.
  270. * However, some drivers may have a polling mode (e.g. pciehp_poll_mode)
  271. * that can be used in the absence of irqs. Allow them to determine
  272. * if that is to be used.
  273. */
  274. status = pcie_init_service_irqs(dev, irqs, capabilities);
  275. if (status) {
  276. capabilities &= PCIE_PORT_SERVICE_VC | PCIE_PORT_SERVICE_HP;
  277. if (!capabilities)
  278. goto error_disable;
  279. }
  280. /* Allocate child services if any */
  281. status = -ENODEV;
  282. nr_service = 0;
  283. for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
  284. int service = 1 << i;
  285. if (!(capabilities & service))
  286. continue;
  287. if (!pcie_device_init(dev, service, irqs[i]))
  288. nr_service++;
  289. }
  290. if (!nr_service)
  291. goto error_cleanup_irqs;
  292. return 0;
  293. error_cleanup_irqs:
  294. pci_free_irq_vectors(dev);
  295. error_disable:
  296. pci_disable_device(dev);
  297. return status;
  298. }
  299. #ifdef CONFIG_PM
  300. static int suspend_iter(struct device *dev, void *data)
  301. {
  302. struct pcie_port_service_driver *service_driver;
  303. if ((dev->bus == &pcie_port_bus_type) && dev->driver) {
  304. service_driver = to_service_driver(dev->driver);
  305. if (service_driver->suspend)
  306. service_driver->suspend(to_pcie_device(dev));
  307. }
  308. return 0;
  309. }
  310. /**
  311. * pcie_port_device_suspend - suspend port services associated with a PCIe port
  312. * @dev: PCI Express port to handle
  313. */
  314. int pcie_port_device_suspend(struct device *dev)
  315. {
  316. return device_for_each_child(dev, NULL, suspend_iter);
  317. }
  318. static int resume_iter(struct device *dev, void *data)
  319. {
  320. struct pcie_port_service_driver *service_driver;
  321. if ((dev->bus == &pcie_port_bus_type) &&
  322. (dev->driver)) {
  323. service_driver = to_service_driver(dev->driver);
  324. if (service_driver->resume)
  325. service_driver->resume(to_pcie_device(dev));
  326. }
  327. return 0;
  328. }
  329. /**
  330. * pcie_port_device_resume - resume port services associated with a PCIe port
  331. * @dev: PCI Express port to handle
  332. */
  333. int pcie_port_device_resume(struct device *dev)
  334. {
  335. return device_for_each_child(dev, NULL, resume_iter);
  336. }
  337. #endif /* PM */
  338. static int remove_iter(struct device *dev, void *data)
  339. {
  340. if (dev->bus == &pcie_port_bus_type)
  341. device_unregister(dev);
  342. return 0;
  343. }
  344. /**
  345. * pcie_port_device_remove - unregister PCI Express port service devices
  346. * @dev: PCI Express port the service devices to unregister are associated with
  347. *
  348. * Remove PCI Express port service devices associated with given port and
  349. * disable MSI-X or MSI for the port.
  350. */
  351. void pcie_port_device_remove(struct pci_dev *dev)
  352. {
  353. device_for_each_child(&dev->dev, NULL, remove_iter);
  354. pci_free_irq_vectors(dev);
  355. pci_disable_device(dev);
  356. }
  357. /**
  358. * pcie_port_probe_service - probe driver for given PCI Express port service
  359. * @dev: PCI Express port service device to probe against
  360. *
  361. * If PCI Express port service driver is registered with
  362. * pcie_port_service_register(), this function will be called by the driver core
  363. * whenever match is found between the driver and a port service device.
  364. */
  365. static int pcie_port_probe_service(struct device *dev)
  366. {
  367. struct pcie_device *pciedev;
  368. struct pcie_port_service_driver *driver;
  369. int status;
  370. if (!dev || !dev->driver)
  371. return -ENODEV;
  372. driver = to_service_driver(dev->driver);
  373. if (!driver || !driver->probe)
  374. return -ENODEV;
  375. pciedev = to_pcie_device(dev);
  376. status = driver->probe(pciedev);
  377. if (status)
  378. return status;
  379. get_device(dev);
  380. return 0;
  381. }
  382. /**
  383. * pcie_port_remove_service - detach driver from given PCI Express port service
  384. * @dev: PCI Express port service device to handle
  385. *
  386. * If PCI Express port service driver is registered with
  387. * pcie_port_service_register(), this function will be called by the driver core
  388. * when device_unregister() is called for the port service device associated
  389. * with the driver.
  390. */
  391. static int pcie_port_remove_service(struct device *dev)
  392. {
  393. struct pcie_device *pciedev;
  394. struct pcie_port_service_driver *driver;
  395. if (!dev || !dev->driver)
  396. return 0;
  397. pciedev = to_pcie_device(dev);
  398. driver = to_service_driver(dev->driver);
  399. if (driver && driver->remove) {
  400. driver->remove(pciedev);
  401. put_device(dev);
  402. }
  403. return 0;
  404. }
  405. /**
  406. * pcie_port_shutdown_service - shut down given PCI Express port service
  407. * @dev: PCI Express port service device to handle
  408. *
  409. * If PCI Express port service driver is registered with
  410. * pcie_port_service_register(), this function will be called by the driver core
  411. * when device_shutdown() is called for the port service device associated
  412. * with the driver.
  413. */
  414. static void pcie_port_shutdown_service(struct device *dev) {}
  415. /**
  416. * pcie_port_service_register - register PCI Express port service driver
  417. * @new: PCI Express port service driver to register
  418. */
  419. int pcie_port_service_register(struct pcie_port_service_driver *new)
  420. {
  421. if (pcie_ports_disabled)
  422. return -ENODEV;
  423. new->driver.name = new->name;
  424. new->driver.bus = &pcie_port_bus_type;
  425. new->driver.probe = pcie_port_probe_service;
  426. new->driver.remove = pcie_port_remove_service;
  427. new->driver.shutdown = pcie_port_shutdown_service;
  428. return driver_register(&new->driver);
  429. }
  430. EXPORT_SYMBOL(pcie_port_service_register);
  431. /**
  432. * pcie_port_service_unregister - unregister PCI Express port service driver
  433. * @drv: PCI Express port service driver to unregister
  434. */
  435. void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
  436. {
  437. driver_unregister(&drv->driver);
  438. }
  439. EXPORT_SYMBOL(pcie_port_service_unregister);