portdrv_core.c 15 KB

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