portdrv_core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  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/aer.h>
  17. #include "../pci.h"
  18. #include "portdrv.h"
  19. struct portdrv_service_data {
  20. struct pcie_port_service_driver *drv;
  21. struct device *dev;
  22. u32 service;
  23. };
  24. /**
  25. * release_pcie_device - free PCI Express port service device structure
  26. * @dev: Port service device to release
  27. *
  28. * Invoked automatically when device is being removed in response to
  29. * device_unregister(dev). Release all resources being claimed.
  30. */
  31. static void release_pcie_device(struct device *dev)
  32. {
  33. kfree(to_pcie_device(dev));
  34. }
  35. /*
  36. * Fill in *pme, *aer, *dpc with the relevant Interrupt Message Numbers if
  37. * services are enabled in "mask". Return the number of MSI/MSI-X vectors
  38. * required to accommodate the largest Message Number.
  39. */
  40. static int pcie_message_numbers(struct pci_dev *dev, int mask,
  41. u32 *pme, u32 *aer, u32 *dpc)
  42. {
  43. u32 nvec = 0, pos;
  44. u16 reg16;
  45. /*
  46. * The Interrupt Message Number indicates which vector is used, i.e.,
  47. * the MSI-X table entry or the MSI offset between the base Message
  48. * Data and the generated interrupt message. See PCIe r3.1, sec
  49. * 7.8.2, 7.10.10, 7.31.2.
  50. */
  51. if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP |
  52. PCIE_PORT_SERVICE_BWNOTIF)) {
  53. pcie_capability_read_word(dev, PCI_EXP_FLAGS, &reg16);
  54. *pme = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
  55. nvec = *pme + 1;
  56. }
  57. #ifdef CONFIG_PCIEAER
  58. if (mask & PCIE_PORT_SERVICE_AER) {
  59. u32 reg32;
  60. pos = dev->aer_cap;
  61. if (pos) {
  62. pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS,
  63. &reg32);
  64. *aer = (reg32 & PCI_ERR_ROOT_AER_IRQ) >> 27;
  65. nvec = max(nvec, *aer + 1);
  66. }
  67. }
  68. #endif
  69. if (mask & PCIE_PORT_SERVICE_DPC) {
  70. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC);
  71. if (pos) {
  72. pci_read_config_word(dev, pos + PCI_EXP_DPC_CAP,
  73. &reg16);
  74. *dpc = reg16 & PCI_EXP_DPC_IRQ;
  75. nvec = max(nvec, *dpc + 1);
  76. }
  77. }
  78. return nvec;
  79. }
  80. /**
  81. * pcie_port_enable_irq_vec - try to set up MSI-X or MSI as interrupt mode
  82. * for given port
  83. * @dev: PCI Express port to handle
  84. * @irqs: Array of interrupt vectors to populate
  85. * @mask: Bitmask of port capabilities returned by get_port_device_capability()
  86. *
  87. * Return value: 0 on success, error code on failure
  88. */
  89. static int pcie_port_enable_irq_vec(struct pci_dev *dev, int *irqs, int mask)
  90. {
  91. int nr_entries, nvec, pcie_irq;
  92. u32 pme = 0, aer = 0, dpc = 0;
  93. /* Allocate the maximum possible number of MSI/MSI-X vectors */
  94. nr_entries = pci_alloc_irq_vectors(dev, 1, PCIE_PORT_MAX_MSI_ENTRIES,
  95. PCI_IRQ_MSIX | PCI_IRQ_MSI);
  96. if (nr_entries < 0)
  97. return nr_entries;
  98. /* See how many and which Interrupt Message Numbers we actually use */
  99. nvec = pcie_message_numbers(dev, mask, &pme, &aer, &dpc);
  100. if (nvec > nr_entries) {
  101. pci_free_irq_vectors(dev);
  102. return -EIO;
  103. }
  104. /*
  105. * If we allocated more than we need, free them and reallocate fewer.
  106. *
  107. * Reallocating may change the specific vectors we get, so
  108. * pci_irq_vector() must be done *after* the reallocation.
  109. *
  110. * If we're using MSI, hardware is *allowed* to change the Interrupt
  111. * Message Numbers when we free and reallocate the vectors, but we
  112. * assume it won't because we allocate enough vectors for the
  113. * biggest Message Number we found.
  114. */
  115. if (nvec != nr_entries) {
  116. pci_free_irq_vectors(dev);
  117. nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
  118. PCI_IRQ_MSIX | PCI_IRQ_MSI);
  119. if (nr_entries < 0)
  120. return nr_entries;
  121. }
  122. /* PME, hotplug and bandwidth notification share an MSI/MSI-X vector */
  123. if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP |
  124. PCIE_PORT_SERVICE_BWNOTIF)) {
  125. pcie_irq = pci_irq_vector(dev, pme);
  126. irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pcie_irq;
  127. irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pcie_irq;
  128. irqs[PCIE_PORT_SERVICE_BWNOTIF_SHIFT] = pcie_irq;
  129. }
  130. if (mask & PCIE_PORT_SERVICE_AER)
  131. irqs[PCIE_PORT_SERVICE_AER_SHIFT] = pci_irq_vector(dev, aer);
  132. if (mask & PCIE_PORT_SERVICE_DPC)
  133. irqs[PCIE_PORT_SERVICE_DPC_SHIFT] = pci_irq_vector(dev, dpc);
  134. return 0;
  135. }
  136. /**
  137. * pcie_init_service_irqs - initialize irqs for PCI Express port services
  138. * @dev: PCI Express port to handle
  139. * @irqs: Array of irqs to populate
  140. * @mask: Bitmask of port capabilities returned by get_port_device_capability()
  141. *
  142. * Return value: Interrupt mode associated with the port
  143. */
  144. static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
  145. {
  146. int ret, i;
  147. for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
  148. irqs[i] = -1;
  149. /*
  150. * If we support PME but can't use MSI/MSI-X for it, we have to
  151. * fall back to INTx or other interrupts, e.g., a system shared
  152. * interrupt.
  153. */
  154. if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi())
  155. goto legacy_irq;
  156. /* Try to use MSI-X or MSI if supported */
  157. if (pcie_port_enable_irq_vec(dev, irqs, mask) == 0)
  158. return 0;
  159. legacy_irq:
  160. /* fall back to legacy IRQ */
  161. ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_LEGACY);
  162. if (ret < 0)
  163. return -ENODEV;
  164. for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
  165. irqs[i] = pci_irq_vector(dev, 0);
  166. return 0;
  167. }
  168. /**
  169. * get_port_device_capability - discover capabilities of a PCI Express port
  170. * @dev: PCI Express port to examine
  171. *
  172. * The capabilities are read from the port's PCI Express configuration registers
  173. * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
  174. * 7.9 - 7.11.
  175. *
  176. * Return value: Bitmask of discovered port capabilities
  177. */
  178. static int get_port_device_capability(struct pci_dev *dev)
  179. {
  180. struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
  181. int services = 0;
  182. if (dev->is_hotplug_bridge &&
  183. (pcie_ports_native || host->native_pcie_hotplug)) {
  184. services |= PCIE_PORT_SERVICE_HP;
  185. /*
  186. * Disable hot-plug interrupts in case they have been enabled
  187. * by the BIOS and the hot-plug service driver is not loaded.
  188. */
  189. pcie_capability_clear_word(dev, PCI_EXP_SLTCTL,
  190. PCI_EXP_SLTCTL_CCIE | PCI_EXP_SLTCTL_HPIE);
  191. }
  192. #ifdef CONFIG_PCIEAER
  193. if (dev->aer_cap && pci_aer_available() &&
  194. (pcie_ports_native || host->native_aer)) {
  195. services |= PCIE_PORT_SERVICE_AER;
  196. /*
  197. * Disable AER on this port in case it's been enabled by the
  198. * BIOS (the AER service driver will enable it when necessary).
  199. */
  200. pci_disable_pcie_error_reporting(dev);
  201. }
  202. #endif
  203. /*
  204. * Root ports are capable of generating PME too. Root Complex
  205. * Event Collectors can also generate PMEs, but we don't handle
  206. * those yet.
  207. */
  208. if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT &&
  209. (pcie_ports_native || host->native_pme)) {
  210. services |= PCIE_PORT_SERVICE_PME;
  211. /*
  212. * Disable PME interrupt on this port in case it's been enabled
  213. * by the BIOS (the PME service driver will enable it when
  214. * necessary).
  215. */
  216. pcie_pme_interrupt_enable(dev, false);
  217. }
  218. /*
  219. * With dpc-native, allow Linux to use DPC even if it doesn't have
  220. * permission to use AER.
  221. */
  222. if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC) &&
  223. pci_aer_available() &&
  224. (pcie_ports_dpc_native || (services & PCIE_PORT_SERVICE_AER)))
  225. services |= PCIE_PORT_SERVICE_DPC;
  226. if (pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM ||
  227. pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
  228. u32 linkcap;
  229. pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &linkcap);
  230. if (linkcap & PCI_EXP_LNKCAP_LBNC)
  231. services |= PCIE_PORT_SERVICE_BWNOTIF;
  232. }
  233. return services;
  234. }
  235. /**
  236. * pcie_device_init - allocate and initialize PCI Express port service device
  237. * @pdev: PCI Express port to associate the service device with
  238. * @service: Type of service to associate with the service device
  239. * @irq: Interrupt vector to associate with the service device
  240. */
  241. static int pcie_device_init(struct pci_dev *pdev, int service, int irq)
  242. {
  243. int retval;
  244. struct pcie_device *pcie;
  245. struct device *device;
  246. pcie = kzalloc(sizeof(*pcie), GFP_KERNEL);
  247. if (!pcie)
  248. return -ENOMEM;
  249. pcie->port = pdev;
  250. pcie->irq = irq;
  251. pcie->service = service;
  252. /* Initialize generic device interface */
  253. device = &pcie->device;
  254. device->bus = &pcie_port_bus_type;
  255. device->release = release_pcie_device; /* callback to free pcie dev */
  256. dev_set_name(device, "%s:pcie%03x",
  257. pci_name(pdev),
  258. get_descriptor_id(pci_pcie_type(pdev), service));
  259. device->parent = &pdev->dev;
  260. device_enable_async_suspend(device);
  261. retval = device_register(device);
  262. if (retval) {
  263. put_device(device);
  264. return retval;
  265. }
  266. pm_runtime_no_callbacks(device);
  267. return 0;
  268. }
  269. /**
  270. * pcie_port_device_register - register PCI Express port
  271. * @dev: PCI Express port to register
  272. *
  273. * Allocate the port extension structure and register services associated with
  274. * the port.
  275. */
  276. int pcie_port_device_register(struct pci_dev *dev)
  277. {
  278. int status, capabilities, i, nr_service;
  279. int irqs[PCIE_PORT_DEVICE_MAXSERVICES];
  280. /* Enable PCI Express port device */
  281. status = pci_enable_device(dev);
  282. if (status)
  283. return status;
  284. /* Get and check PCI Express port services */
  285. capabilities = get_port_device_capability(dev);
  286. if (!capabilities)
  287. return 0;
  288. pci_set_master(dev);
  289. /*
  290. * Initialize service irqs. Don't use service devices that
  291. * require interrupts if there is no way to generate them.
  292. * However, some drivers may have a polling mode (e.g. pciehp_poll_mode)
  293. * that can be used in the absence of irqs. Allow them to determine
  294. * if that is to be used.
  295. */
  296. status = pcie_init_service_irqs(dev, irqs, capabilities);
  297. if (status) {
  298. capabilities &= PCIE_PORT_SERVICE_HP;
  299. if (!capabilities)
  300. goto error_disable;
  301. }
  302. /* Allocate child services if any */
  303. status = -ENODEV;
  304. nr_service = 0;
  305. for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
  306. int service = 1 << i;
  307. if (!(capabilities & service))
  308. continue;
  309. if (!pcie_device_init(dev, service, irqs[i]))
  310. nr_service++;
  311. }
  312. if (!nr_service)
  313. goto error_cleanup_irqs;
  314. return 0;
  315. error_cleanup_irqs:
  316. pci_free_irq_vectors(dev);
  317. error_disable:
  318. pci_disable_device(dev);
  319. return status;
  320. }
  321. #ifdef CONFIG_PM
  322. typedef int (*pcie_pm_callback_t)(struct pcie_device *);
  323. static int pm_iter(struct device *dev, void *data)
  324. {
  325. struct pcie_port_service_driver *service_driver;
  326. size_t offset = *(size_t *)data;
  327. pcie_pm_callback_t cb;
  328. if ((dev->bus == &pcie_port_bus_type) && dev->driver) {
  329. service_driver = to_service_driver(dev->driver);
  330. cb = *(pcie_pm_callback_t *)((void *)service_driver + offset);
  331. if (cb)
  332. return cb(to_pcie_device(dev));
  333. }
  334. return 0;
  335. }
  336. /**
  337. * pcie_port_device_suspend - suspend port services associated with a PCIe port
  338. * @dev: PCI Express port to handle
  339. */
  340. int pcie_port_device_suspend(struct device *dev)
  341. {
  342. size_t off = offsetof(struct pcie_port_service_driver, suspend);
  343. return device_for_each_child(dev, &off, pm_iter);
  344. }
  345. int pcie_port_device_resume_noirq(struct device *dev)
  346. {
  347. size_t off = offsetof(struct pcie_port_service_driver, resume_noirq);
  348. return device_for_each_child(dev, &off, pm_iter);
  349. }
  350. /**
  351. * pcie_port_device_resume - resume port services associated with a PCIe port
  352. * @dev: PCI Express port to handle
  353. */
  354. int pcie_port_device_resume(struct device *dev)
  355. {
  356. size_t off = offsetof(struct pcie_port_service_driver, resume);
  357. return device_for_each_child(dev, &off, pm_iter);
  358. }
  359. /**
  360. * pcie_port_device_runtime_suspend - runtime suspend port services
  361. * @dev: PCI Express port to handle
  362. */
  363. int pcie_port_device_runtime_suspend(struct device *dev)
  364. {
  365. size_t off = offsetof(struct pcie_port_service_driver, runtime_suspend);
  366. return device_for_each_child(dev, &off, pm_iter);
  367. }
  368. /**
  369. * pcie_port_device_runtime_resume - runtime resume port services
  370. * @dev: PCI Express port to handle
  371. */
  372. int pcie_port_device_runtime_resume(struct device *dev)
  373. {
  374. size_t off = offsetof(struct pcie_port_service_driver, runtime_resume);
  375. return device_for_each_child(dev, &off, pm_iter);
  376. }
  377. #endif /* PM */
  378. static int remove_iter(struct device *dev, void *data)
  379. {
  380. if (dev->bus == &pcie_port_bus_type)
  381. device_unregister(dev);
  382. return 0;
  383. }
  384. static int find_service_iter(struct device *device, void *data)
  385. {
  386. struct pcie_port_service_driver *service_driver;
  387. struct portdrv_service_data *pdrvs;
  388. u32 service;
  389. pdrvs = (struct portdrv_service_data *) data;
  390. service = pdrvs->service;
  391. if (device->bus == &pcie_port_bus_type && device->driver) {
  392. service_driver = to_service_driver(device->driver);
  393. if (service_driver->service == service) {
  394. pdrvs->drv = service_driver;
  395. pdrvs->dev = device;
  396. return 1;
  397. }
  398. }
  399. return 0;
  400. }
  401. /**
  402. * pcie_port_find_device - find the struct device
  403. * @dev: PCI Express port the service is associated with
  404. * @service: For the service to find
  405. *
  406. * Find the struct device associated with given service on a pci_dev
  407. */
  408. struct device *pcie_port_find_device(struct pci_dev *dev,
  409. u32 service)
  410. {
  411. struct device *device;
  412. struct portdrv_service_data pdrvs;
  413. pdrvs.dev = NULL;
  414. pdrvs.service = service;
  415. device_for_each_child(&dev->dev, &pdrvs, find_service_iter);
  416. device = pdrvs.dev;
  417. return device;
  418. }
  419. EXPORT_SYMBOL_GPL(pcie_port_find_device);
  420. /**
  421. * pcie_port_device_remove - unregister PCI Express port service devices
  422. * @dev: PCI Express port the service devices to unregister are associated with
  423. *
  424. * Remove PCI Express port service devices associated with given port and
  425. * disable MSI-X or MSI for the port.
  426. */
  427. void pcie_port_device_remove(struct pci_dev *dev)
  428. {
  429. device_for_each_child(&dev->dev, NULL, remove_iter);
  430. pci_free_irq_vectors(dev);
  431. pci_disable_device(dev);
  432. }
  433. /**
  434. * pcie_port_probe_service - probe driver for given PCI Express port service
  435. * @dev: PCI Express port service device to probe against
  436. *
  437. * If PCI Express port service driver is registered with
  438. * pcie_port_service_register(), this function will be called by the driver core
  439. * whenever match is found between the driver and a port service device.
  440. */
  441. static int pcie_port_probe_service(struct device *dev)
  442. {
  443. struct pcie_device *pciedev;
  444. struct pcie_port_service_driver *driver;
  445. int status;
  446. if (!dev || !dev->driver)
  447. return -ENODEV;
  448. driver = to_service_driver(dev->driver);
  449. if (!driver || !driver->probe)
  450. return -ENODEV;
  451. pciedev = to_pcie_device(dev);
  452. status = driver->probe(pciedev);
  453. if (status)
  454. return status;
  455. get_device(dev);
  456. return 0;
  457. }
  458. /**
  459. * pcie_port_remove_service - detach driver from given PCI Express port service
  460. * @dev: PCI Express port service device to handle
  461. *
  462. * If PCI Express port service driver is registered with
  463. * pcie_port_service_register(), this function will be called by the driver core
  464. * when device_unregister() is called for the port service device associated
  465. * with the driver.
  466. */
  467. static int pcie_port_remove_service(struct device *dev)
  468. {
  469. struct pcie_device *pciedev;
  470. struct pcie_port_service_driver *driver;
  471. if (!dev || !dev->driver)
  472. return 0;
  473. pciedev = to_pcie_device(dev);
  474. driver = to_service_driver(dev->driver);
  475. if (driver && driver->remove) {
  476. driver->remove(pciedev);
  477. put_device(dev);
  478. }
  479. return 0;
  480. }
  481. /**
  482. * pcie_port_shutdown_service - shut down given PCI Express port service
  483. * @dev: PCI Express port service device to handle
  484. *
  485. * If PCI Express port service driver is registered with
  486. * pcie_port_service_register(), this function will be called by the driver core
  487. * when device_shutdown() is called for the port service device associated
  488. * with the driver.
  489. */
  490. static void pcie_port_shutdown_service(struct device *dev) {}
  491. /**
  492. * pcie_port_service_register - register PCI Express port service driver
  493. * @new: PCI Express port service driver to register
  494. */
  495. int pcie_port_service_register(struct pcie_port_service_driver *new)
  496. {
  497. if (pcie_ports_disabled)
  498. return -ENODEV;
  499. new->driver.name = new->name;
  500. new->driver.bus = &pcie_port_bus_type;
  501. new->driver.probe = pcie_port_probe_service;
  502. new->driver.remove = pcie_port_remove_service;
  503. new->driver.shutdown = pcie_port_shutdown_service;
  504. return driver_register(&new->driver);
  505. }
  506. EXPORT_SYMBOL(pcie_port_service_register);
  507. /**
  508. * pcie_port_service_unregister - unregister PCI Express port service driver
  509. * @drv: PCI Express port service driver to unregister
  510. */
  511. void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
  512. {
  513. driver_unregister(&drv->driver);
  514. }
  515. EXPORT_SYMBOL(pcie_port_service_unregister);