portdrv_core.c 16 KB

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