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>
20 extern int pcie_mch_quirk
; /* MSI-quirk Indicator */
22 static int pcie_port_probe_service(struct device
*dev
)
24 struct pcie_device
*pciedev
;
25 struct pcie_port_service_driver
*driver
;
28 if (!dev
|| !dev
->driver
)
31 driver
= to_service_driver(dev
->driver
);
32 if (!driver
|| !driver
->probe
)
35 pciedev
= to_pcie_device(dev
);
36 status
= driver
->probe(pciedev
, driver
->id_table
);
38 dev_printk(KERN_DEBUG
, dev
, "service driver %s loaded\n",
45 static int pcie_port_remove_service(struct device
*dev
)
47 struct pcie_device
*pciedev
;
48 struct pcie_port_service_driver
*driver
;
50 if (!dev
|| !dev
->driver
)
53 pciedev
= to_pcie_device(dev
);
54 driver
= to_service_driver(dev
->driver
);
55 if (driver
&& driver
->remove
) {
56 dev_printk(KERN_DEBUG
, dev
, "unloading service driver %s\n",
58 driver
->remove(pciedev
);
64 static void pcie_port_shutdown_service(struct device
*dev
) {}
67 * release_pcie_device - free PCI Express port service device structure
68 * @dev: Port service device to release
70 * Invoked automatically when device is being removed in response to
71 * device_unregister(dev). Release all resources being claimed.
73 static void release_pcie_device(struct device
*dev
)
75 kfree(to_pcie_device(dev
));
78 static int is_msi_quirked(struct pci_dev
*dev
)
80 int port_type
, quirk
= 0;
83 pci_read_config_word(dev
,
84 pci_find_capability(dev
, PCI_CAP_ID_EXP
) +
85 PCIE_CAPABILITIES_REG
, ®16
);
86 port_type
= (reg16
>> 4) & PORT_TYPE_MASK
;
89 if (pcie_mch_quirk
== 1)
92 case PCIE_SW_UPSTREAM_PORT
:
93 case PCIE_SW_DOWNSTREAM_PORT
:
101 * assign_interrupt_mode - choose interrupt mode for PCI Express port services
102 * (INTx, MSI-X, MSI) and set up vectors
103 * @dev: PCI Express port to handle
104 * @vectors: Array of interrupt vectors to populate
105 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
107 * Return value: Interrupt mode associated with the port
109 static int assign_interrupt_mode(struct pci_dev
*dev
, int *vectors
, int mask
)
111 int i
, pos
, nvec
, status
= -EINVAL
;
112 int interrupt_mode
= PCIE_PORT_INTx_MODE
;
114 /* Set INTx as default */
115 for (i
= 0, nvec
= 0; i
< PCIE_PORT_DEVICE_MAXSERVICES
; i
++) {
118 vectors
[i
] = dev
->irq
;
121 /* Check MSI quirk */
122 if (is_msi_quirked(dev
))
123 return interrupt_mode
;
125 /* Select MSI-X over MSI if supported */
126 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
128 struct msix_entry msix_entries
[PCIE_PORT_DEVICE_MAXSERVICES
] =
129 {{0, 0}, {0, 1}, {0, 2}, {0, 3}};
130 status
= pci_enable_msix(dev
, msix_entries
, nvec
);
134 interrupt_mode
= PCIE_PORT_MSIX_MODE
;
135 for (i
= 0; i
< PCIE_PORT_DEVICE_MAXSERVICES
; i
++) {
137 vectors
[i
] = msix_entries
[j
++].vector
;
142 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
144 status
= pci_enable_msi(dev
);
146 interrupt_mode
= PCIE_PORT_MSI_MODE
;
147 for (i
= 0;i
< PCIE_PORT_DEVICE_MAXSERVICES
;i
++)
148 vectors
[i
] = dev
->irq
;
152 return interrupt_mode
;
156 * get_port_device_capability - discover capabilities of a PCI Express port
157 * @dev: PCI Express port to examine
159 * The capabilities are read from the port's PCI Express configuration registers
160 * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
163 * Return value: Bitmask of discovered port capabilities
165 static int get_port_device_capability(struct pci_dev
*dev
)
167 int services
= 0, pos
;
171 pos
= pci_find_capability(dev
, PCI_CAP_ID_EXP
);
172 pci_read_config_word(dev
, pos
+ PCIE_CAPABILITIES_REG
, ®16
);
173 /* Hot-Plug Capable */
174 if (reg16
& PORT_TO_SLOT_MASK
) {
175 pci_read_config_dword(dev
,
176 pos
+ PCIE_SLOT_CAPABILITIES_REG
, ®32
);
177 if (reg32
& SLOT_HP_CAPABLE_MASK
)
178 services
|= PCIE_PORT_SERVICE_HP
;
180 /* PME Capable - root port capability */
181 if (((reg16
>> 4) & PORT_TYPE_MASK
) == PCIE_RC_PORT
)
182 services
|= PCIE_PORT_SERVICE_PME
;
184 if (pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_ERR
))
185 services
|= PCIE_PORT_SERVICE_AER
;
186 if (pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_VC
))
187 services
|= PCIE_PORT_SERVICE_VC
;
193 * pcie_device_init - initialize PCI Express port service device
194 * @dev: Port service device to initialize
195 * @parent: PCI Express port to associate the service device with
196 * @port_type: Type of the port
197 * @service_type: Type of service to associate with the service device
198 * @irq: Interrupt vector to associate with the service device
199 * @irq_mode: Interrupt mode of the service (INTx, MSI-X, MSI)
201 static void pcie_device_init(struct pci_dev
*parent
, struct pcie_device
*dev
,
202 int port_type
, int service_type
, int irq
, int irq_mode
)
204 struct device
*device
;
207 dev
->interrupt_mode
= irq_mode
;
209 dev
->id
.vendor
= parent
->vendor
;
210 dev
->id
.device
= parent
->device
;
211 dev
->id
.port_type
= port_type
;
212 dev
->id
.service_type
= (1 << service_type
);
214 /* Initialize generic device interface */
215 device
= &dev
->device
;
216 memset(device
, 0, sizeof(struct device
));
217 device
->bus
= &pcie_port_bus_type
;
218 device
->driver
= NULL
;
219 device
->driver_data
= NULL
;
220 device
->release
= release_pcie_device
; /* callback to free pcie dev */
221 dev_set_name(device
, "%s:pcie%02x",
222 pci_name(parent
), get_descriptor_id(port_type
, service_type
));
223 device
->parent
= &parent
->dev
;
227 * alloc_pcie_device - allocate PCI Express port service device structure
228 * @parent: PCI Express port to associate the service device with
229 * @port_type: Type of the port
230 * @service_type: Type of service to associate with the service device
231 * @irq: Interrupt vector to associate with the service device
232 * @irq_mode: Interrupt mode of the service (INTx, MSI-X, MSI)
234 static struct pcie_device
* alloc_pcie_device(struct pci_dev
*parent
,
235 int port_type
, int service_type
, int irq
, int irq_mode
)
237 struct pcie_device
*device
;
239 device
= kzalloc(sizeof(struct pcie_device
), GFP_KERNEL
);
243 pcie_device_init(parent
, device
, port_type
, service_type
, irq
,irq_mode
);
248 * pcie_port_device_probe - check if device is a PCI Express port
249 * @dev: Device to check
251 int pcie_port_device_probe(struct pci_dev
*dev
)
256 if (!(pos
= pci_find_capability(dev
, PCI_CAP_ID_EXP
)))
259 pci_read_config_word(dev
, pos
+ PCIE_CAPABILITIES_REG
, ®
);
260 type
= (reg
>> 4) & PORT_TYPE_MASK
;
261 if ( type
== PCIE_RC_PORT
|| type
== PCIE_SW_UPSTREAM_PORT
||
262 type
== PCIE_SW_DOWNSTREAM_PORT
)
269 * pcie_port_device_register - register PCI Express port
270 * @dev: PCI Express port to register
272 * Allocate the port extension structure and register services associated with
275 int pcie_port_device_register(struct pci_dev
*dev
)
277 struct pcie_port_device_ext
*p_ext
;
278 int status
, type
, capabilities
, irq_mode
, i
;
279 int vectors
[PCIE_PORT_DEVICE_MAXSERVICES
];
282 /* Allocate port device extension */
283 if (!(p_ext
= kmalloc(sizeof(struct pcie_port_device_ext
), GFP_KERNEL
)))
286 pci_set_drvdata(dev
, p_ext
);
289 pci_read_config_word(dev
,
290 pci_find_capability(dev
, PCI_CAP_ID_EXP
) +
291 PCIE_CAPABILITIES_REG
, ®16
);
292 type
= (reg16
>> 4) & PORT_TYPE_MASK
;
294 /* Now get port services */
295 capabilities
= get_port_device_capability(dev
);
296 irq_mode
= assign_interrupt_mode(dev
, vectors
, capabilities
);
297 p_ext
->interrupt_mode
= irq_mode
;
299 /* Allocate child services if any */
300 for (i
= 0; i
< PCIE_PORT_DEVICE_MAXSERVICES
; i
++) {
301 struct pcie_device
*child
;
303 if (capabilities
& (1 << i
)) {
304 child
= alloc_pcie_device(
306 type
, /* port type */
307 i
, /* service type */
308 vectors
[i
], /* irq */
309 irq_mode
/* interrupt mode */);
311 status
= device_register(&child
->device
);
316 get_device(&child
->device
);
324 static int suspend_iter(struct device
*dev
, void *data
)
326 struct pcie_port_service_driver
*service_driver
;
327 pm_message_t state
= * (pm_message_t
*) data
;
329 if ((dev
->bus
== &pcie_port_bus_type
) &&
331 service_driver
= to_service_driver(dev
->driver
);
332 if (service_driver
->suspend
)
333 service_driver
->suspend(to_pcie_device(dev
), state
);
339 * pcie_port_device_suspend - suspend port services associated with a PCIe port
340 * @dev: PCI Express port to handle
341 * @state: Representation of system power management transition in progress
343 int pcie_port_device_suspend(struct pci_dev
*dev
, pm_message_t state
)
345 return device_for_each_child(&dev
->dev
, &state
, suspend_iter
);
348 static int resume_iter(struct device
*dev
, void *data
)
350 struct pcie_port_service_driver
*service_driver
;
352 if ((dev
->bus
== &pcie_port_bus_type
) &&
354 service_driver
= to_service_driver(dev
->driver
);
355 if (service_driver
->resume
)
356 service_driver
->resume(to_pcie_device(dev
));
362 * pcie_port_device_suspend - resume port services associated with a PCIe port
363 * @dev: PCI Express port to handle
365 int pcie_port_device_resume(struct pci_dev
*dev
)
367 return device_for_each_child(&dev
->dev
, NULL
, resume_iter
);
371 static int remove_iter(struct device
*dev
, void *data
)
373 struct pcie_port_service_driver
*service_driver
;
375 if (dev
->bus
== &pcie_port_bus_type
) {
377 service_driver
= to_service_driver(dev
->driver
);
378 if (service_driver
->remove
)
379 service_driver
->remove(to_pcie_device(dev
));
381 *(unsigned long*)data
= (unsigned long)dev
;
388 * pcie_port_device_remove - unregister PCI Express port service devices
389 * @dev: PCI Express port the service devices to unregister are associated with
391 * Remove PCI Express port service devices associated with given port and
392 * disable MSI-X or MSI for the port.
394 void pcie_port_device_remove(struct pci_dev
*dev
)
396 struct device
*device
;
397 unsigned long device_addr
;
398 int interrupt_mode
= PCIE_PORT_INTx_MODE
;
402 status
= device_for_each_child(&dev
->dev
, &device_addr
, remove_iter
);
404 device
= (struct device
*)device_addr
;
405 interrupt_mode
= (to_pcie_device(device
))->interrupt_mode
;
407 device_unregister(device
);
410 /* Switch to INTx by default if MSI enabled */
411 if (interrupt_mode
== PCIE_PORT_MSIX_MODE
)
412 pci_disable_msix(dev
);
413 else if (interrupt_mode
== PCIE_PORT_MSI_MODE
)
414 pci_disable_msi(dev
);
417 int pcie_port_bus_register(void)
419 return bus_register(&pcie_port_bus_type
);
422 void pcie_port_bus_unregister(void)
424 bus_unregister(&pcie_port_bus_type
);
427 int pcie_port_service_register(struct pcie_port_service_driver
*new)
429 new->driver
.name
= (char *)new->name
;
430 new->driver
.bus
= &pcie_port_bus_type
;
431 new->driver
.probe
= pcie_port_probe_service
;
432 new->driver
.remove
= pcie_port_remove_service
;
433 new->driver
.shutdown
= pcie_port_shutdown_service
;
435 return driver_register(&new->driver
);
438 void pcie_port_service_unregister(struct pcie_port_service_driver
*new)
440 driver_unregister(&new->driver
);
443 EXPORT_SYMBOL(pcie_port_service_register
);
444 EXPORT_SYMBOL(pcie_port_service_unregister
);