Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/pci.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/pm.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 14 | #include <linux/string.h> |
| 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/pcieport_if.h> |
Rafael J. Wysocki | 28eb5f2 | 2010-08-21 22:02:38 +0200 | [diff] [blame] | 17 | #include <linux/aer.h> |
| 18 | #include <linux/pci-aspm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Rafael J. Wysocki | 1bf83e5 | 2009-01-13 14:38:34 +0100 | [diff] [blame] | 20 | #include "../pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "portdrv.h" |
| 22 | |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 23 | /** |
| 24 | * release_pcie_device - free PCI Express port service device structure |
| 25 | * @dev: Port service device to release |
| 26 | * |
| 27 | * Invoked automatically when device is being removed in response to |
| 28 | * device_unregister(dev). Release all resources being claimed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | */ |
| 30 | static void release_pcie_device(struct device *dev) |
| 31 | { |
Hidetoshi Seto | 40da418 | 2009-12-15 11:38:04 +0900 | [diff] [blame] | 32 | kfree(to_pcie_device(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 35 | /** |
Rafael J. Wysocki | b43d451 | 2009-01-24 00:23:22 +0100 | [diff] [blame] | 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 |
| 40 | * |
| 41 | * Return value: Position of the added entry in the array |
| 42 | */ |
| 43 | static int pcie_port_msix_add_entry( |
| 44 | struct msix_entry *entries, int new_entry, int nr_entries) |
| 45 | { |
| 46 | int j; |
| 47 | |
| 48 | for (j = 0; j < nr_entries; j++) |
| 49 | if (entries[j].entry == new_entry) |
| 50 | return j; |
| 51 | |
| 52 | entries[j].entry = new_entry; |
| 53 | return j; |
| 54 | } |
| 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 | |
| 72 | nr_entries = pci_msix_table_size(dev); |
| 73 | if (!nr_entries) |
| 74 | return -EINVAL; |
| 75 | if (nr_entries > PCIE_PORT_MAX_MSIX_ENTRIES) |
| 76 | nr_entries = PCIE_PORT_MAX_MSIX_ENTRIES; |
| 77 | |
| 78 | msix_entries = kzalloc(sizeof(*msix_entries) * nr_entries, GFP_KERNEL); |
| 79 | if (!msix_entries) |
| 80 | return -ENOMEM; |
| 81 | |
| 82 | /* |
| 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. |
| 87 | */ |
| 88 | for (i = 0; i < nr_entries; i++) |
| 89 | msix_entries[i].entry = i; |
| 90 | |
| 91 | status = pci_enable_msix(dev, msix_entries, nr_entries); |
| 92 | if (status) |
| 93 | goto Exit; |
| 94 | |
| 95 | for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) |
| 96 | idx[i] = -1; |
| 97 | status = -EIO; |
| 98 | nvec = 0; |
| 99 | |
| 100 | if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) { |
| 101 | int entry; |
| 102 | |
| 103 | /* |
| 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." |
| 112 | */ |
Kenji Kaneshige | dba90df | 2009-11-11 14:32:42 +0900 | [diff] [blame] | 113 | pos = pci_pcie_cap(dev); |
Kenji Kaneshige | f9f4560 | 2009-11-25 21:06:51 +0900 | [diff] [blame] | 114 | pci_read_config_word(dev, pos + PCI_EXP_FLAGS, ®16); |
| 115 | entry = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9; |
Rafael J. Wysocki | b43d451 | 2009-01-24 00:23:22 +0100 | [diff] [blame] | 116 | if (entry >= nr_entries) |
| 117 | goto Error; |
| 118 | |
| 119 | i = pcie_port_msix_add_entry(msix_entries, entry, nvec); |
| 120 | if (i == nvec) |
| 121 | nvec++; |
| 122 | |
| 123 | idx[PCIE_PORT_SERVICE_PME_SHIFT] = i; |
| 124 | idx[PCIE_PORT_SERVICE_HP_SHIFT] = i; |
| 125 | } |
| 126 | |
| 127 | if (mask & PCIE_PORT_SERVICE_AER) { |
| 128 | int entry; |
| 129 | |
| 130 | /* |
| 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." |
| 138 | */ |
| 139 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR); |
| 140 | pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, ®32); |
| 141 | entry = reg32 >> 27; |
| 142 | if (entry >= nr_entries) |
| 143 | goto Error; |
| 144 | |
| 145 | i = pcie_port_msix_add_entry(msix_entries, entry, nvec); |
| 146 | if (i == nvec) |
| 147 | nvec++; |
| 148 | |
| 149 | idx[PCIE_PORT_SERVICE_AER_SHIFT] = i; |
| 150 | } |
| 151 | |
| 152 | /* |
| 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. |
| 156 | */ |
| 157 | if (nvec == nr_entries) { |
| 158 | status = 0; |
| 159 | } else { |
| 160 | /* Drop the temporary MSI-X setup */ |
| 161 | pci_disable_msix(dev); |
| 162 | |
| 163 | /* Now allocate the MSI-X vectors for real */ |
| 164 | status = pci_enable_msix(dev, msix_entries, nvec); |
| 165 | if (status) |
| 166 | goto Exit; |
| 167 | } |
| 168 | |
| 169 | for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) |
| 170 | vectors[i] = idx[i] >= 0 ? msix_entries[idx[i]].vector : -1; |
| 171 | |
| 172 | Exit: |
| 173 | kfree(msix_entries); |
| 174 | return status; |
| 175 | |
| 176 | Error: |
| 177 | pci_disable_msix(dev); |
| 178 | goto Exit; |
| 179 | } |
| 180 | |
| 181 | /** |
Kenji Kaneshige | dc53517 | 2009-11-25 21:04:00 +0900 | [diff] [blame] | 182 | * init_service_irqs - initialize irqs for PCI Express port services |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 183 | * @dev: PCI Express port to handle |
Kenji Kaneshige | dc53517 | 2009-11-25 21:04:00 +0900 | [diff] [blame] | 184 | * @irqs: Array of irqs to populate |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 185 | * @mask: Bitmask of port capabilities returned by get_port_device_capability() |
| 186 | * |
| 187 | * Return value: Interrupt mode associated with the port |
| 188 | */ |
Kenji Kaneshige | dc53517 | 2009-11-25 21:04:00 +0900 | [diff] [blame] | 189 | static int init_service_irqs(struct pci_dev *dev, int *irqs, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
Rafael J. Wysocki | c39fae1 | 2010-02-17 23:40:07 +0100 | [diff] [blame] | 191 | int i, irq = -1; |
| 192 | |
| 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()) { |
| 195 | if (dev->pin) |
| 196 | irq = dev->irq; |
| 197 | goto no_msi; |
| 198 | } |
Rafael J. Wysocki | 90e9cd5 | 2009-01-13 14:39:39 +0100 | [diff] [blame] | 199 | |
Rafael J. Wysocki | b43d451 | 2009-01-24 00:23:22 +0100 | [diff] [blame] | 200 | /* Try to use MSI-X if supported */ |
Kenji Kaneshige | dc53517 | 2009-11-25 21:04:00 +0900 | [diff] [blame] | 201 | if (!pcie_port_enable_msix(dev, irqs, mask)) |
| 202 | return 0; |
Rafael J. Wysocki | c39fae1 | 2010-02-17 23:40:07 +0100 | [diff] [blame] | 203 | |
Rafael J. Wysocki | b43d451 | 2009-01-24 00:23:22 +0100 | [diff] [blame] | 204 | /* We're not going to use MSI-X, so try MSI and fall back to INTx */ |
Kenji Kaneshige | dc53517 | 2009-11-25 21:04:00 +0900 | [diff] [blame] | 205 | if (!pci_enable_msi(dev) || dev->pin) |
| 206 | irq = dev->irq; |
Rafael J. Wysocki | b43d451 | 2009-01-24 00:23:22 +0100 | [diff] [blame] | 207 | |
Rafael J. Wysocki | c39fae1 | 2010-02-17 23:40:07 +0100 | [diff] [blame] | 208 | no_msi: |
Rafael J. Wysocki | b43d451 | 2009-01-24 00:23:22 +0100 | [diff] [blame] | 209 | for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) |
Kenji Kaneshige | dc53517 | 2009-11-25 21:04:00 +0900 | [diff] [blame] | 210 | irqs[i] = irq; |
| 211 | irqs[PCIE_PORT_SERVICE_VC_SHIFT] = -1; |
Rafael J. Wysocki | b43d451 | 2009-01-24 00:23:22 +0100 | [diff] [blame] | 212 | |
Kenji Kaneshige | dc53517 | 2009-11-25 21:04:00 +0900 | [diff] [blame] | 213 | if (irq < 0) |
| 214 | return -ENODEV; |
| 215 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Kenji Kaneshige | fbb5de7 | 2009-11-25 21:05:01 +0900 | [diff] [blame] | 218 | static void cleanup_service_irqs(struct pci_dev *dev) |
| 219 | { |
| 220 | if (dev->msix_enabled) |
| 221 | pci_disable_msix(dev); |
| 222 | else if (dev->msi_enabled) |
| 223 | pci_disable_msi(dev); |
| 224 | } |
| 225 | |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 226 | /** |
| 227 | * get_port_device_capability - discover capabilities of a PCI Express port |
| 228 | * @dev: PCI Express port to examine |
| 229 | * |
| 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 |
| 232 | * 7.9 - 7.11. |
| 233 | * |
| 234 | * Return value: Bitmask of discovered port capabilities |
| 235 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | static int get_port_device_capability(struct pci_dev *dev) |
| 237 | { |
| 238 | int services = 0, pos; |
| 239 | u16 reg16; |
| 240 | u32 reg32; |
Rafael J. Wysocki | 28eb5f2 | 2010-08-21 22:02:38 +0200 | [diff] [blame] | 241 | int cap_mask; |
| 242 | int err; |
| 243 | |
Rafael J. Wysocki | fe31e69 | 2010-12-19 15:57:16 +0100 | [diff] [blame^] | 244 | if (pcie_ports_disabled) |
| 245 | return 0; |
| 246 | |
Rafael J. Wysocki | 28eb5f2 | 2010-08-21 22:02:38 +0200 | [diff] [blame] | 247 | err = pcie_port_platform_notify(dev, &cap_mask); |
Rafael J. Wysocki | fe31e69 | 2010-12-19 15:57:16 +0100 | [diff] [blame^] | 248 | if (!pcie_ports_auto) { |
Rafael J. Wysocki | 28eb5f2 | 2010-08-21 22:02:38 +0200 | [diff] [blame] | 249 | cap_mask = PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP |
| 250 | | PCIE_PORT_SERVICE_VC; |
| 251 | if (pci_aer_available()) |
| 252 | cap_mask |= PCIE_PORT_SERVICE_AER; |
Rafael J. Wysocki | fe31e69 | 2010-12-19 15:57:16 +0100 | [diff] [blame^] | 253 | } else if (err) { |
| 254 | return 0; |
Rafael J. Wysocki | 28eb5f2 | 2010-08-21 22:02:38 +0200 | [diff] [blame] | 255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Kenji Kaneshige | dba90df | 2009-11-11 14:32:42 +0900 | [diff] [blame] | 257 | pos = pci_pcie_cap(dev); |
Kenji Kaneshige | f9f4560 | 2009-11-25 21:06:51 +0900 | [diff] [blame] | 258 | pci_read_config_word(dev, pos + PCI_EXP_FLAGS, ®16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | /* Hot-Plug Capable */ |
Rafael J. Wysocki | 28eb5f2 | 2010-08-21 22:02:38 +0200 | [diff] [blame] | 260 | if ((cap_mask & PCIE_PORT_SERVICE_HP) && (reg16 & PCI_EXP_FLAGS_SLOT)) { |
Kenji Kaneshige | f9f4560 | 2009-11-25 21:06:51 +0900 | [diff] [blame] | 261 | pci_read_config_dword(dev, pos + PCI_EXP_SLTCAP, ®32); |
Rafael J. Wysocki | 2bd50dd | 2010-08-21 01:57:39 +0200 | [diff] [blame] | 262 | if (reg32 & PCI_EXP_SLTCAP_HPC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | services |= PCIE_PORT_SERVICE_HP; |
Rafael J. Wysocki | 2bd50dd | 2010-08-21 01:57:39 +0200 | [diff] [blame] | 264 | /* |
| 265 | * Disable hot-plug interrupts in case they have been |
| 266 | * enabled by the BIOS and the hot-plug service driver |
| 267 | * is not loaded. |
| 268 | */ |
| 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); |
| 273 | } |
Rafael J. Wysocki | 1bf83e5 | 2009-01-13 14:38:34 +0100 | [diff] [blame] | 274 | } |
| 275 | /* AER capable */ |
Rafael J. Wysocki | 28eb5f2 | 2010-08-21 22:02:38 +0200 | [diff] [blame] | 276 | if ((cap_mask & PCIE_PORT_SERVICE_AER) |
Rafael J. Wysocki | 2bd50dd | 2010-08-21 01:57:39 +0200 | [diff] [blame] | 277 | && pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR)) { |
Jesse Barnes | 0927678 | 2008-10-18 17:33:19 -0700 | [diff] [blame] | 278 | services |= PCIE_PORT_SERVICE_AER; |
Rafael J. Wysocki | 2bd50dd | 2010-08-21 01:57:39 +0200 | [diff] [blame] | 279 | /* |
| 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). |
| 282 | */ |
| 283 | pci_disable_pcie_error_reporting(dev); |
| 284 | } |
Rafael J. Wysocki | 1bf83e5 | 2009-01-13 14:38:34 +0100 | [diff] [blame] | 285 | /* VC support */ |
Jesse Barnes | 0927678 | 2008-10-18 17:33:19 -0700 | [diff] [blame] | 286 | if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_VC)) |
| 287 | services |= PCIE_PORT_SERVICE_VC; |
Kenji Kaneshige | 9e5d0b1 | 2009-11-25 21:02:51 +0900 | [diff] [blame] | 288 | /* Root ports are capable of generating PME too */ |
Rafael J. Wysocki | 28eb5f2 | 2010-08-21 22:02:38 +0200 | [diff] [blame] | 289 | if ((cap_mask & PCIE_PORT_SERVICE_PME) |
Rafael J. Wysocki | 2bd50dd | 2010-08-21 01:57:39 +0200 | [diff] [blame] | 290 | && dev->pcie_type == PCI_EXP_TYPE_ROOT_PORT) { |
Kenji Kaneshige | 9e5d0b1 | 2009-11-25 21:02:51 +0900 | [diff] [blame] | 291 | services |= PCIE_PORT_SERVICE_PME; |
Rafael J. Wysocki | 2bd50dd | 2010-08-21 01:57:39 +0200 | [diff] [blame] | 292 | /* |
| 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 |
| 295 | * necessary). |
| 296 | */ |
| 297 | pcie_pme_interrupt_enable(dev, false); |
| 298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | return services; |
| 301 | } |
| 302 | |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 303 | /** |
Kenji Kaneshige | 52a0f24 | 2009-11-25 21:01:28 +0900 | [diff] [blame] | 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 |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 307 | * @irq: Interrupt vector to associate with the service device |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 308 | */ |
Kenji Kaneshige | 52a0f24 | 2009-11-25 21:01:28 +0900 | [diff] [blame] | 309 | static int pcie_device_init(struct pci_dev *pdev, int service, int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { |
Kenji Kaneshige | 52a0f24 | 2009-11-25 21:01:28 +0900 | [diff] [blame] | 311 | int retval; |
| 312 | struct pcie_device *pcie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | struct device *device; |
| 314 | |
Kenji Kaneshige | 52a0f24 | 2009-11-25 21:01:28 +0900 | [diff] [blame] | 315 | pcie = kzalloc(sizeof(*pcie), GFP_KERNEL); |
| 316 | if (!pcie) |
| 317 | return -ENOMEM; |
| 318 | pcie->port = pdev; |
| 319 | pcie->irq = irq; |
| 320 | pcie->service = service; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
| 322 | /* Initialize generic device interface */ |
Kenji Kaneshige | 52a0f24 | 2009-11-25 21:01:28 +0900 | [diff] [blame] | 323 | device = &pcie->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | device->bus = &pcie_port_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | device->release = release_pcie_device; /* callback to free pcie dev */ |
Kay Sievers | 1a92713 | 2008-10-30 02:17:49 +0100 | [diff] [blame] | 326 | dev_set_name(device, "%s:pcie%02x", |
Kenji Kaneshige | 52a0f24 | 2009-11-25 21:01:28 +0900 | [diff] [blame] | 327 | pci_name(pdev), |
| 328 | get_descriptor_id(pdev->pcie_type, service)); |
| 329 | device->parent = &pdev->dev; |
Rafael J. Wysocki | a1e4d72 | 2010-02-08 19:16:33 +0100 | [diff] [blame] | 330 | device_enable_async_suspend(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Kenji Kaneshige | 52a0f24 | 2009-11-25 21:01:28 +0900 | [diff] [blame] | 332 | retval = device_register(device); |
| 333 | if (retval) |
| 334 | kfree(pcie); |
| 335 | else |
| 336 | get_device(device); |
| 337 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 340 | /** |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 341 | * pcie_port_device_register - register PCI Express port |
| 342 | * @dev: PCI Express port to register |
| 343 | * |
| 344 | * Allocate the port extension structure and register services associated with |
| 345 | * the port. |
| 346 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | int pcie_port_device_register(struct pci_dev *dev) |
| 348 | { |
Kenji Kaneshige | 40717c3 | 2009-11-25 21:05:35 +0900 | [diff] [blame] | 349 | int status, capabilities, i, nr_service; |
Kenji Kaneshige | dc53517 | 2009-11-25 21:04:00 +0900 | [diff] [blame] | 350 | int irqs[PCIE_PORT_DEVICE_MAXSERVICES]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Kenji Kaneshige | 1ce5e83 | 2009-11-25 21:04:30 +0900 | [diff] [blame] | 352 | /* Enable PCI Express port device */ |
| 353 | status = pci_enable_device(dev); |
| 354 | if (status) |
Kenji Kaneshige | 694f88e | 2009-11-25 21:06:15 +0900 | [diff] [blame] | 355 | return status; |
Rafael J. Wysocki | fe31e69 | 2010-12-19 15:57:16 +0100 | [diff] [blame^] | 356 | |
| 357 | /* Get and check PCI Express port services */ |
| 358 | capabilities = get_port_device_capability(dev); |
| 359 | if (!capabilities) { |
| 360 | pcie_no_aspm(); |
| 361 | return 0; |
| 362 | } |
| 363 | |
Kenji Kaneshige | 1ce5e83 | 2009-11-25 21:04:30 +0900 | [diff] [blame] | 364 | pci_set_master(dev); |
Kenji Kaneshige | dc53517 | 2009-11-25 21:04:00 +0900 | [diff] [blame] | 365 | /* |
| 366 | * Initialize service irqs. Don't use service devices that |
| 367 | * require interrupts if there is no way to generate them. |
| 368 | */ |
| 369 | status = init_service_irqs(dev, irqs, capabilities); |
| 370 | if (status) { |
| 371 | capabilities &= PCIE_PORT_SERVICE_VC; |
| 372 | if (!capabilities) |
Kenji Kaneshige | 1ce5e83 | 2009-11-25 21:04:30 +0900 | [diff] [blame] | 373 | goto error_disable; |
Rafael J. Wysocki | f118c0c | 2009-01-13 14:42:01 +0100 | [diff] [blame] | 374 | } |
Rafael J. Wysocki | f118c0c | 2009-01-13 14:42:01 +0100 | [diff] [blame] | 375 | |
Rafael J. Wysocki | f118c0c | 2009-01-13 14:42:01 +0100 | [diff] [blame] | 376 | /* Allocate child services if any */ |
Kenji Kaneshige | 40717c3 | 2009-11-25 21:05:35 +0900 | [diff] [blame] | 377 | status = -ENODEV; |
| 378 | nr_service = 0; |
| 379 | for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) { |
Rafael J. Wysocki | f118c0c | 2009-01-13 14:42:01 +0100 | [diff] [blame] | 380 | int service = 1 << i; |
Rafael J. Wysocki | f118c0c | 2009-01-13 14:42:01 +0100 | [diff] [blame] | 381 | if (!(capabilities & service)) |
Rafael J. Wysocki | 90e9cd5 | 2009-01-13 14:39:39 +0100 | [diff] [blame] | 382 | continue; |
Kenji Kaneshige | 40717c3 | 2009-11-25 21:05:35 +0900 | [diff] [blame] | 383 | if (!pcie_device_init(dev, service, irqs[i])) |
| 384 | nr_service++; |
Rafael J. Wysocki | f118c0c | 2009-01-13 14:42:01 +0100 | [diff] [blame] | 385 | } |
Kenji Kaneshige | 40717c3 | 2009-11-25 21:05:35 +0900 | [diff] [blame] | 386 | if (!nr_service) |
Kenji Kaneshige | fbb5de7 | 2009-11-25 21:05:01 +0900 | [diff] [blame] | 387 | goto error_cleanup_irqs; |
Kenji Kaneshige | 40717c3 | 2009-11-25 21:05:35 +0900 | [diff] [blame] | 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | return 0; |
Rafael J. Wysocki | f118c0c | 2009-01-13 14:42:01 +0100 | [diff] [blame] | 390 | |
Kenji Kaneshige | fbb5de7 | 2009-11-25 21:05:01 +0900 | [diff] [blame] | 391 | error_cleanup_irqs: |
| 392 | cleanup_service_irqs(dev); |
Kenji Kaneshige | 1ce5e83 | 2009-11-25 21:04:30 +0900 | [diff] [blame] | 393 | error_disable: |
| 394 | pci_disable_device(dev); |
Rafael J. Wysocki | f118c0c | 2009-01-13 14:42:01 +0100 | [diff] [blame] | 395 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | #ifdef CONFIG_PM |
long | d0e2b4a | 2005-03-29 13:36:43 -0800 | [diff] [blame] | 399 | static int suspend_iter(struct device *dev, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | struct pcie_port_service_driver *service_driver; |
| 402 | |
Hidetoshi Seto | 40da418 | 2009-12-15 11:38:04 +0900 | [diff] [blame] | 403 | if ((dev->bus == &pcie_port_bus_type) && dev->driver) { |
| 404 | service_driver = to_service_driver(dev->driver); |
| 405 | if (service_driver->suspend) |
| 406 | service_driver->suspend(to_pcie_device(dev)); |
| 407 | } |
long | d0e2b4a | 2005-03-29 13:36:43 -0800 | [diff] [blame] | 408 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 411 | /** |
| 412 | * pcie_port_device_suspend - suspend port services associated with a PCIe port |
| 413 | * @dev: PCI Express port to handle |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 414 | */ |
Rafael J. Wysocki | 3a3c244 | 2009-02-15 22:32:48 +0100 | [diff] [blame] | 415 | int pcie_port_device_suspend(struct device *dev) |
long | d0e2b4a | 2005-03-29 13:36:43 -0800 | [diff] [blame] | 416 | { |
Rafael J. Wysocki | 3a3c244 | 2009-02-15 22:32:48 +0100 | [diff] [blame] | 417 | return device_for_each_child(dev, NULL, suspend_iter); |
long | d0e2b4a | 2005-03-29 13:36:43 -0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static int resume_iter(struct device *dev, void *data) |
| 421 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | struct pcie_port_service_driver *service_driver; |
| 423 | |
long | d0e2b4a | 2005-03-29 13:36:43 -0800 | [diff] [blame] | 424 | if ((dev->bus == &pcie_port_bus_type) && |
| 425 | (dev->driver)) { |
| 426 | service_driver = to_service_driver(dev->driver); |
| 427 | if (service_driver->resume) |
| 428 | service_driver->resume(to_pcie_device(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } |
long | d0e2b4a | 2005-03-29 13:36:43 -0800 | [diff] [blame] | 430 | return 0; |
| 431 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 433 | /** |
| 434 | * pcie_port_device_suspend - resume port services associated with a PCIe port |
| 435 | * @dev: PCI Express port to handle |
| 436 | */ |
Rafael J. Wysocki | 3a3c244 | 2009-02-15 22:32:48 +0100 | [diff] [blame] | 437 | int pcie_port_device_resume(struct device *dev) |
long | d0e2b4a | 2005-03-29 13:36:43 -0800 | [diff] [blame] | 438 | { |
Rafael J. Wysocki | 3a3c244 | 2009-02-15 22:32:48 +0100 | [diff] [blame] | 439 | return device_for_each_child(dev, NULL, resume_iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
Rafael J. Wysocki | 3a3c244 | 2009-02-15 22:32:48 +0100 | [diff] [blame] | 441 | #endif /* PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
long | d0e2b4a | 2005-03-29 13:36:43 -0800 | [diff] [blame] | 443 | static int remove_iter(struct device *dev, void *data) |
| 444 | { |
long | d0e2b4a | 2005-03-29 13:36:43 -0800 | [diff] [blame] | 445 | if (dev->bus == &pcie_port_bus_type) { |
Eric W. Biederman | ae40582 | 2009-02-20 20:16:07 -0800 | [diff] [blame] | 446 | put_device(dev); |
| 447 | device_unregister(dev); |
long | d0e2b4a | 2005-03-29 13:36:43 -0800 | [diff] [blame] | 448 | } |
| 449 | return 0; |
| 450 | } |
| 451 | |
Rafael J. Wysocki | facf6d1 | 2009-01-01 19:48:55 +0100 | [diff] [blame] | 452 | /** |
| 453 | * pcie_port_device_remove - unregister PCI Express port service devices |
| 454 | * @dev: PCI Express port the service devices to unregister are associated with |
| 455 | * |
| 456 | * Remove PCI Express port service devices associated with given port and |
| 457 | * disable MSI-X or MSI for the port. |
| 458 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | void pcie_port_device_remove(struct pci_dev *dev) |
| 460 | { |
Eric W. Biederman | ae40582 | 2009-02-20 20:16:07 -0800 | [diff] [blame] | 461 | device_for_each_child(&dev->dev, NULL, remove_iter); |
Kenji Kaneshige | fbb5de7 | 2009-11-25 21:05:01 +0900 | [diff] [blame] | 462 | cleanup_service_irqs(dev); |
Kenji Kaneshige | dc53517 | 2009-11-25 21:04:00 +0900 | [diff] [blame] | 463 | pci_disable_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Rafael J. Wysocki | d934737 | 2009-01-01 19:53:32 +0100 | [diff] [blame] | 466 | /** |
| 467 | * pcie_port_probe_service - probe driver for given PCI Express port service |
| 468 | * @dev: PCI Express port service device to probe against |
| 469 | * |
| 470 | * If PCI Express port service driver is registered with |
| 471 | * pcie_port_service_register(), this function will be called by the driver core |
| 472 | * whenever match is found between the driver and a port service device. |
| 473 | */ |
Rafael J. Wysocki | fa6c993 | 2009-01-01 19:52:12 +0100 | [diff] [blame] | 474 | static int pcie_port_probe_service(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
Rafael J. Wysocki | fa6c993 | 2009-01-01 19:52:12 +0100 | [diff] [blame] | 476 | struct pcie_device *pciedev; |
| 477 | struct pcie_port_service_driver *driver; |
| 478 | int status; |
| 479 | |
| 480 | if (!dev || !dev->driver) |
| 481 | return -ENODEV; |
| 482 | |
| 483 | driver = to_service_driver(dev->driver); |
| 484 | if (!driver || !driver->probe) |
| 485 | return -ENODEV; |
| 486 | |
| 487 | pciedev = to_pcie_device(dev); |
Rafael J. Wysocki | 0516c8b | 2009-01-13 14:44:19 +0100 | [diff] [blame] | 488 | status = driver->probe(pciedev); |
Rafael J. Wysocki | fa6c993 | 2009-01-01 19:52:12 +0100 | [diff] [blame] | 489 | if (!status) { |
| 490 | dev_printk(KERN_DEBUG, dev, "service driver %s loaded\n", |
| 491 | driver->name); |
| 492 | get_device(dev); |
| 493 | } |
| 494 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Rafael J. Wysocki | d934737 | 2009-01-01 19:53:32 +0100 | [diff] [blame] | 497 | /** |
| 498 | * pcie_port_remove_service - detach driver from given PCI Express port service |
| 499 | * @dev: PCI Express port service device to handle |
| 500 | * |
| 501 | * If PCI Express port service driver is registered with |
| 502 | * pcie_port_service_register(), this function will be called by the driver core |
| 503 | * when device_unregister() is called for the port service device associated |
| 504 | * with the driver. |
| 505 | */ |
Rafael J. Wysocki | fa6c993 | 2009-01-01 19:52:12 +0100 | [diff] [blame] | 506 | static int pcie_port_remove_service(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | { |
Rafael J. Wysocki | fa6c993 | 2009-01-01 19:52:12 +0100 | [diff] [blame] | 508 | struct pcie_device *pciedev; |
| 509 | struct pcie_port_service_driver *driver; |
| 510 | |
| 511 | if (!dev || !dev->driver) |
| 512 | return 0; |
| 513 | |
| 514 | pciedev = to_pcie_device(dev); |
| 515 | driver = to_service_driver(dev->driver); |
| 516 | if (driver && driver->remove) { |
| 517 | dev_printk(KERN_DEBUG, dev, "unloading service driver %s\n", |
| 518 | driver->name); |
| 519 | driver->remove(pciedev); |
| 520 | put_device(dev); |
| 521 | } |
| 522 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Rafael J. Wysocki | d934737 | 2009-01-01 19:53:32 +0100 | [diff] [blame] | 525 | /** |
| 526 | * pcie_port_shutdown_service - shut down given PCI Express port service |
| 527 | * @dev: PCI Express port service device to handle |
| 528 | * |
| 529 | * If PCI Express port service driver is registered with |
| 530 | * pcie_port_service_register(), this function will be called by the driver core |
| 531 | * when device_shutdown() is called for the port service device associated |
| 532 | * with the driver. |
| 533 | */ |
Rafael J. Wysocki | fa6c993 | 2009-01-01 19:52:12 +0100 | [diff] [blame] | 534 | static void pcie_port_shutdown_service(struct device *dev) {} |
| 535 | |
Rafael J. Wysocki | d934737 | 2009-01-01 19:53:32 +0100 | [diff] [blame] | 536 | /** |
| 537 | * pcie_port_service_register - register PCI Express port service driver |
| 538 | * @new: PCI Express port service driver to register |
| 539 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | int pcie_port_service_register(struct pcie_port_service_driver *new) |
| 541 | { |
Rafael J. Wysocki | 79dd918 | 2010-08-21 01:51:44 +0200 | [diff] [blame] | 542 | if (pcie_ports_disabled) |
| 543 | return -ENODEV; |
| 544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | new->driver.name = (char *)new->name; |
| 546 | new->driver.bus = &pcie_port_bus_type; |
| 547 | new->driver.probe = pcie_port_probe_service; |
| 548 | new->driver.remove = pcie_port_remove_service; |
| 549 | new->driver.shutdown = pcie_port_shutdown_service; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | return driver_register(&new->driver); |
long | d0e2b4a | 2005-03-29 13:36:43 -0800 | [diff] [blame] | 552 | } |
Hidetoshi Seto | 40da418 | 2009-12-15 11:38:04 +0900 | [diff] [blame] | 553 | EXPORT_SYMBOL(pcie_port_service_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
Rafael J. Wysocki | d934737 | 2009-01-01 19:53:32 +0100 | [diff] [blame] | 555 | /** |
| 556 | * pcie_port_service_unregister - unregister PCI Express port service driver |
| 557 | * @drv: PCI Express port service driver to unregister |
| 558 | */ |
Rafael J. Wysocki | fa6c993 | 2009-01-01 19:52:12 +0100 | [diff] [blame] | 559 | void pcie_port_service_unregister(struct pcie_port_service_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | { |
Rafael J. Wysocki | fa6c993 | 2009-01-01 19:52:12 +0100 | [diff] [blame] | 561 | driver_unregister(&drv->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | EXPORT_SYMBOL(pcie_port_service_unregister); |