3 * Purpose: PCI Express Port Bus Driver's Core Functions
5 * Copyright (C) 2004 Intel
6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/slab.h>
16 #include <linux/pcieport_if.h>
17 #include <linux/aer.h>
18 #include <linux/pci-aspm.h>
24 * release_pcie_device - free PCI Express port service device structure
25 * @dev: Port service device to release
27 * Invoked automatically when device is being removed in response to
28 * device_unregister(dev). Release all resources being claimed.
30 static void release_pcie_device(struct device *dev)
32 kfree(to_pcie_device(dev));
36 * pcie_port_msix_add_entry - add entry to given array of MSI-X entries
37 * @entries: Array of MSI-X entries
38 * @new_entry: Index of the entry to add to the array
39 * @nr_entries: Number of entries aleady in the array
41 * Return value: Position of the added entry in the array
43 static int pcie_port_msix_add_entry(
44 struct msix_entry *entries, int new_entry, int nr_entries)
48 for (j = 0; j < nr_entries; j++)
49 if (entries[j].entry == new_entry)
52 entries[j].entry = new_entry;
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()
62 * Return value: 0 on success, error code on failure
64 static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask)
66 struct msix_entry *msix_entries;
67 int idx[PCIE_PORT_DEVICE_MAXSERVICES];
68 int nr_entries, status, pos, i, nvec;
72 nr_entries = pci_msix_table_size(dev);
75 if (nr_entries > PCIE_PORT_MAX_MSIX_ENTRIES)
76 nr_entries = PCIE_PORT_MAX_MSIX_ENTRIES;
78 msix_entries = kzalloc(sizeof(*msix_entries) * nr_entries, GFP_KERNEL);
83 * Allocate as many entries as the port wants, so that we can check
84 * which of them will be useful. Moreover, if nr_entries is correctly
85 * equal to the number of entries this port actually uses, we'll happily
86 * go through without any tricks.
88 for (i = 0; i < nr_entries; i++)
89 msix_entries[i].entry = i;
91 status = pci_enable_msix(dev, msix_entries, nr_entries);
95 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
100 if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
104 * The code below follows the PCI Express Base Specification 2.0
105 * stating in Section 6.1.6 that "PME and Hot-Plug Event
106 * interrupts (when both are implemented) always share the same
107 * MSI or MSI-X vector, as indicated by the Interrupt Message
108 * Number field in the PCI Express Capabilities register", where
109 * according to Section 7.8.2 of the specification "For MSI-X,
110 * the value in this field indicates which MSI-X Table entry is
111 * used to generate the interrupt message."
113 pos = pci_pcie_cap(dev);
114 pci_read_config_word(dev, pos + PCI_EXP_FLAGS, ®16);
115 entry = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
116 if (entry >= nr_entries)
119 i = pcie_port_msix_add_entry(msix_entries, entry, nvec);
123 idx[PCIE_PORT_SERVICE_PME_SHIFT] = i;
124 idx[PCIE_PORT_SERVICE_HP_SHIFT] = i;
127 if (mask & PCIE_PORT_SERVICE_AER) {
131 * The code below follows Section 7.10.10 of the PCI Express
132 * Base Specification 2.0 stating that bits 31-27 of the Root
133 * Error Status Register contain a value indicating which of the
134 * MSI/MSI-X vectors assigned to the port is going to be used
135 * for AER, where "For MSI-X, the value in this register
136 * indicates which MSI-X Table entry is used to generate the
137 * interrupt message."
139 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
140 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, ®32);
142 if (entry >= nr_entries)
145 i = pcie_port_msix_add_entry(msix_entries, entry, nvec);
149 idx[PCIE_PORT_SERVICE_AER_SHIFT] = i;
153 * If nvec is equal to the allocated number of entries, we can just use
154 * what we have. Otherwise, the port has some extra entries not for the
155 * services we know and we need to work around that.
157 if (nvec == nr_entries) {
160 /* Drop the temporary MSI-X setup */
161 pci_disable_msix(dev);
163 /* Now allocate the MSI-X vectors for real */
164 status = pci_enable_msix(dev, msix_entries, nvec);
169 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
170 vectors[i] = idx[i] >= 0 ? msix_entries[idx[i]].vector : -1;
177 pci_disable_msix(dev);
182 * init_service_irqs - initialize irqs for PCI Express port services
183 * @dev: PCI Express port to handle
184 * @irqs: Array of irqs to populate
185 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
187 * Return value: Interrupt mode associated with the port
189 static int init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
193 /* We have to use INTx if MSI cannot be used for PCIe PME. */
194 if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi()) {
200 /* Try to use MSI-X if supported */
201 if (!pcie_port_enable_msix(dev, irqs, mask))
204 /* We're not going to use MSI-X, so try MSI and fall back to INTx */
205 if (!pci_enable_msi(dev) || dev->pin)
209 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
211 irqs[PCIE_PORT_SERVICE_VC_SHIFT] = -1;
218 static void cleanup_service_irqs(struct pci_dev *dev)
220 if (dev->msix_enabled)
221 pci_disable_msix(dev);
222 else if (dev->msi_enabled)
223 pci_disable_msi(dev);
227 * get_port_device_capability - discover capabilities of a PCI Express port
228 * @dev: PCI Express port to examine
230 * The capabilities are read from the port's PCI Express configuration registers
231 * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
234 * Return value: Bitmask of discovered port capabilities
236 static int get_port_device_capability(struct pci_dev *dev)
238 int services = 0, pos;
244 err = pcie_port_platform_notify(dev, &cap_mask);
245 if (pcie_ports_auto) {
251 cap_mask = PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP
252 | PCIE_PORT_SERVICE_VC;
253 if (pci_aer_available())
254 cap_mask |= PCIE_PORT_SERVICE_AER;
257 pos = pci_pcie_cap(dev);
258 pci_read_config_word(dev, pos + PCI_EXP_FLAGS, ®16);
259 /* Hot-Plug Capable */
260 if ((cap_mask & PCIE_PORT_SERVICE_HP) && (reg16 & PCI_EXP_FLAGS_SLOT)) {
261 pci_read_config_dword(dev, pos + PCI_EXP_SLTCAP, ®32);
262 if (reg32 & PCI_EXP_SLTCAP_HPC) {
263 services |= PCIE_PORT_SERVICE_HP;
265 * Disable hot-plug interrupts in case they have been
266 * enabled by the BIOS and the hot-plug service driver
269 pos += PCI_EXP_SLTCTL;
270 pci_read_config_word(dev, pos, ®16);
271 reg16 &= ~(PCI_EXP_SLTCTL_CCIE | PCI_EXP_SLTCTL_HPIE);
272 pci_write_config_word(dev, pos, reg16);
276 if ((cap_mask & PCIE_PORT_SERVICE_AER)
277 && pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR)) {
278 services |= PCIE_PORT_SERVICE_AER;
280 * Disable AER on this port in case it's been enabled by the
281 * BIOS (the AER service driver will enable it when necessary).
283 pci_disable_pcie_error_reporting(dev);
286 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_VC))
287 services |= PCIE_PORT_SERVICE_VC;
288 /* Root ports are capable of generating PME too */
289 if ((cap_mask & PCIE_PORT_SERVICE_PME)
290 && dev->pcie_type == PCI_EXP_TYPE_ROOT_PORT) {
291 services |= PCIE_PORT_SERVICE_PME;
293 * Disable PME interrupt on this port in case it's been enabled
294 * by the BIOS (the PME service driver will enable it when
297 pcie_pme_interrupt_enable(dev, false);
304 * pcie_device_init - allocate and initialize PCI Express port service device
305 * @pdev: PCI Express port to associate the service device with
306 * @service: Type of service to associate with the service device
307 * @irq: Interrupt vector to associate with the service device
309 static int pcie_device_init(struct pci_dev *pdev, int service, int irq)
312 struct pcie_device *pcie;
313 struct device *device;
315 pcie = kzalloc(sizeof(*pcie), GFP_KERNEL);
320 pcie->service = service;
322 /* Initialize generic device interface */
323 device = &pcie->device;
324 device->bus = &pcie_port_bus_type;
325 device->release = release_pcie_device; /* callback to free pcie dev */
326 dev_set_name(device, "%s:pcie%02x",
328 get_descriptor_id(pdev->pcie_type, service));
329 device->parent = &pdev->dev;
330 device_enable_async_suspend(device);
332 retval = device_register(device);
341 * pcie_port_device_register - register PCI Express port
342 * @dev: PCI Express port to register
344 * Allocate the port extension structure and register services associated with
347 int pcie_port_device_register(struct pci_dev *dev)
349 int status, capabilities, i, nr_service;
350 int irqs[PCIE_PORT_DEVICE_MAXSERVICES];
352 /* Get and check PCI Express port services */
353 capabilities = get_port_device_capability(dev);
357 /* Enable PCI Express port device */
358 status = pci_enable_device(dev);
363 * Initialize service irqs. Don't use service devices that
364 * require interrupts if there is no way to generate them.
366 status = init_service_irqs(dev, irqs, capabilities);
368 capabilities &= PCIE_PORT_SERVICE_VC;
373 /* Allocate child services if any */
376 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
377 int service = 1 << i;
378 if (!(capabilities & service))
380 if (!pcie_device_init(dev, service, irqs[i]))
384 goto error_cleanup_irqs;
389 cleanup_service_irqs(dev);
391 pci_disable_device(dev);
396 static int suspend_iter(struct device *dev, void *data)
398 struct pcie_port_service_driver *service_driver;
400 if ((dev->bus == &pcie_port_bus_type) && dev->driver) {
401 service_driver = to_service_driver(dev->driver);
402 if (service_driver->suspend)
403 service_driver->suspend(to_pcie_device(dev));
409 * pcie_port_device_suspend - suspend port services associated with a PCIe port
410 * @dev: PCI Express port to handle
412 int pcie_port_device_suspend(struct device *dev)
414 return device_for_each_child(dev, NULL, suspend_iter);
417 static int resume_iter(struct device *dev, void *data)
419 struct pcie_port_service_driver *service_driver;
421 if ((dev->bus == &pcie_port_bus_type) &&
423 service_driver = to_service_driver(dev->driver);
424 if (service_driver->resume)
425 service_driver->resume(to_pcie_device(dev));
431 * pcie_port_device_suspend - resume port services associated with a PCIe port
432 * @dev: PCI Express port to handle
434 int pcie_port_device_resume(struct device *dev)
436 return device_for_each_child(dev, NULL, resume_iter);
440 static int remove_iter(struct device *dev, void *data)
442 if (dev->bus == &pcie_port_bus_type) {
444 device_unregister(dev);
450 * pcie_port_device_remove - unregister PCI Express port service devices
451 * @dev: PCI Express port the service devices to unregister are associated with
453 * Remove PCI Express port service devices associated with given port and
454 * disable MSI-X or MSI for the port.
456 void pcie_port_device_remove(struct pci_dev *dev)
458 device_for_each_child(&dev->dev, NULL, remove_iter);
459 cleanup_service_irqs(dev);
460 pci_disable_device(dev);
464 * pcie_port_probe_service - probe driver for given PCI Express port service
465 * @dev: PCI Express port service device to probe against
467 * If PCI Express port service driver is registered with
468 * pcie_port_service_register(), this function will be called by the driver core
469 * whenever match is found between the driver and a port service device.
471 static int pcie_port_probe_service(struct device *dev)
473 struct pcie_device *pciedev;
474 struct pcie_port_service_driver *driver;
477 if (!dev || !dev->driver)
480 driver = to_service_driver(dev->driver);
481 if (!driver || !driver->probe)
484 pciedev = to_pcie_device(dev);
485 status = driver->probe(pciedev);
487 dev_printk(KERN_DEBUG, dev, "service driver %s loaded\n",
495 * pcie_port_remove_service - detach driver from given PCI Express port service
496 * @dev: PCI Express port service device to handle
498 * If PCI Express port service driver is registered with
499 * pcie_port_service_register(), this function will be called by the driver core
500 * when device_unregister() is called for the port service device associated
503 static int pcie_port_remove_service(struct device *dev)
505 struct pcie_device *pciedev;
506 struct pcie_port_service_driver *driver;
508 if (!dev || !dev->driver)
511 pciedev = to_pcie_device(dev);
512 driver = to_service_driver(dev->driver);
513 if (driver && driver->remove) {
514 dev_printk(KERN_DEBUG, dev, "unloading service driver %s\n",
516 driver->remove(pciedev);
523 * pcie_port_shutdown_service - shut down given PCI Express port service
524 * @dev: PCI Express port service device to handle
526 * If PCI Express port service driver is registered with
527 * pcie_port_service_register(), this function will be called by the driver core
528 * when device_shutdown() is called for the port service device associated
531 static void pcie_port_shutdown_service(struct device *dev) {}
534 * pcie_port_service_register - register PCI Express port service driver
535 * @new: PCI Express port service driver to register
537 int pcie_port_service_register(struct pcie_port_service_driver *new)
539 if (pcie_ports_disabled)
542 new->driver.name = (char *)new->name;
543 new->driver.bus = &pcie_port_bus_type;
544 new->driver.probe = pcie_port_probe_service;
545 new->driver.remove = pcie_port_remove_service;
546 new->driver.shutdown = pcie_port_shutdown_service;
548 return driver_register(&new->driver);
550 EXPORT_SYMBOL(pcie_port_service_register);
553 * pcie_port_service_unregister - unregister PCI Express port service driver
554 * @drv: PCI Express port service driver to unregister
556 void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
558 driver_unregister(&drv->driver);
560 EXPORT_SYMBOL(pcie_port_service_unregister);