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/pcieport_if.h>
18 extern int pcie_mch_quirk
; /* MSI-quirk Indicator */
20 static int pcie_port_probe_service(struct device
*dev
)
22 struct pcie_device
*pciedev
;
23 struct pcie_port_service_driver
*driver
;
26 if (!dev
|| !dev
->driver
)
29 driver
= to_service_driver(dev
->driver
);
30 if (!driver
|| !driver
->probe
)
33 pciedev
= to_pcie_device(dev
);
34 status
= driver
->probe(pciedev
, driver
->id_table
);
36 printk(KERN_DEBUG
"Load service driver %s on pcie device %s\n",
37 driver
->name
, dev
->bus_id
);
43 static int pcie_port_remove_service(struct device
*dev
)
45 struct pcie_device
*pciedev
;
46 struct pcie_port_service_driver
*driver
;
48 if (!dev
|| !dev
->driver
)
51 pciedev
= to_pcie_device(dev
);
52 driver
= to_service_driver(dev
->driver
);
53 if (driver
&& driver
->remove
) {
54 printk(KERN_DEBUG
"Unload service driver %s on pcie device %s\n",
55 driver
->name
, dev
->bus_id
);
56 driver
->remove(pciedev
);
62 static void pcie_port_shutdown_service(struct device
*dev
) {}
64 static int pcie_port_suspend_service(struct device
*dev
, pm_message_t state
, u32 level
)
66 struct pcie_device
*pciedev
;
67 struct pcie_port_service_driver
*driver
;
69 if (!dev
|| !dev
->driver
)
72 pciedev
= to_pcie_device(dev
);
73 driver
= to_service_driver(dev
->driver
);
74 if (driver
&& driver
->suspend
)
75 driver
->suspend(pciedev
, state
);
79 static int pcie_port_resume_service(struct device
*dev
, u32 level
)
81 struct pcie_device
*pciedev
;
82 struct pcie_port_service_driver
*driver
;
84 if (!dev
|| !dev
->driver
)
87 pciedev
= to_pcie_device(dev
);
88 driver
= to_service_driver(dev
->driver
);
90 if (driver
&& driver
->resume
)
91 driver
->resume(pciedev
);
98 * Being invoked automatically when device is being removed
99 * in response to device_unregister(dev) call.
100 * Release all resources being claimed.
102 static void release_pcie_device(struct device
*dev
)
104 printk(KERN_DEBUG
"Free Port Service[%s]\n", dev
->bus_id
);
105 kfree(to_pcie_device(dev
));
108 static int is_msi_quirked(struct pci_dev
*dev
)
110 int port_type
, quirk
= 0;
113 pci_read_config_word(dev
,
114 pci_find_capability(dev
, PCI_CAP_ID_EXP
) +
115 PCIE_CAPABILITIES_REG
, ®16
);
116 port_type
= (reg16
>> 4) & PORT_TYPE_MASK
;
119 if (pcie_mch_quirk
== 1)
122 case PCIE_SW_UPSTREAM_PORT
:
123 case PCIE_SW_DOWNSTREAM_PORT
:
130 static int assign_interrupt_mode(struct pci_dev
*dev
, int *vectors
, int mask
)
132 int i
, pos
, nvec
, status
= -EINVAL
;
133 int interrupt_mode
= PCIE_PORT_INTx_MODE
;
135 /* Set INTx as default */
136 for (i
= 0, nvec
= 0; i
< PCIE_PORT_DEVICE_MAXSERVICES
; i
++) {
139 vectors
[i
] = dev
->irq
;
142 /* Check MSI quirk */
143 if (is_msi_quirked(dev
))
144 return interrupt_mode
;
146 /* Select MSI-X over MSI if supported */
147 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
149 struct msix_entry msix_entries
[PCIE_PORT_DEVICE_MAXSERVICES
] =
150 {{0, 0}, {0, 1}, {0, 2}, {0, 3}};
151 printk("%s Found MSIX capability\n", __FUNCTION__
);
152 status
= pci_enable_msix(dev
, msix_entries
, nvec
);
156 interrupt_mode
= PCIE_PORT_MSIX_MODE
;
157 for (i
= 0; i
< PCIE_PORT_DEVICE_MAXSERVICES
; i
++) {
159 vectors
[i
] = msix_entries
[j
++].vector
;
164 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
166 printk("%s Found MSI capability\n", __FUNCTION__
);
167 status
= pci_enable_msi(dev
);
169 interrupt_mode
= PCIE_PORT_MSI_MODE
;
170 for (i
= 0;i
< PCIE_PORT_DEVICE_MAXSERVICES
;i
++)
171 vectors
[i
] = dev
->irq
;
175 return interrupt_mode
;
178 static int get_port_device_capability(struct pci_dev
*dev
)
180 int services
= 0, pos
;
184 pos
= pci_find_capability(dev
, PCI_CAP_ID_EXP
);
185 pci_read_config_word(dev
, pos
+ PCIE_CAPABILITIES_REG
, ®16
);
186 /* Hot-Plug Capable */
187 if (reg16
& PORT_TO_SLOT_MASK
) {
188 pci_read_config_dword(dev
,
189 pos
+ PCIE_SLOT_CAPABILITIES_REG
, ®32
);
190 if (reg32
& SLOT_HP_CAPABLE_MASK
)
191 services
|= PCIE_PORT_SERVICE_HP
;
194 pos
= pci_find_capability(dev
, PCI_CAP_ID_PME
);
196 services
|= PCIE_PORT_SERVICE_PME
;
198 pos
= PCI_CFG_SPACE_SIZE
;
200 pci_read_config_dword(dev
, pos
, ®32
);
201 switch (reg32
& 0xffff) {
202 case PCI_EXT_CAP_ID_ERR
:
203 services
|= PCIE_PORT_SERVICE_AER
;
206 case PCI_EXT_CAP_ID_VC
:
207 services
|= PCIE_PORT_SERVICE_VC
;
219 static void pcie_device_init(struct pci_dev
*parent
, struct pcie_device
*dev
,
220 int port_type
, int service_type
, int irq
, int irq_mode
)
222 struct device
*device
;
225 dev
->interrupt_mode
= irq_mode
;
227 dev
->id
.vendor
= parent
->vendor
;
228 dev
->id
.device
= parent
->device
;
229 dev
->id
.port_type
= port_type
;
230 dev
->id
.service_type
= (1 << service_type
);
232 /* Initialize generic device interface */
233 device
= &dev
->device
;
234 memset(device
, 0, sizeof(struct device
));
235 device
->bus
= &pcie_port_bus_type
;
236 device
->driver
= NULL
;
237 device
->driver_data
= NULL
;
238 device
->release
= release_pcie_device
; /* callback to free pcie dev */
239 sprintf(&device
->bus_id
[0], "pcie%02x",
240 get_descriptor_id(port_type
, service_type
));
241 device
->parent
= &parent
->dev
;
244 static struct pcie_device
* alloc_pcie_device(struct pci_dev
*parent
,
245 int port_type
, int service_type
, int irq
, int irq_mode
)
247 struct pcie_device
*device
;
249 device
= kmalloc(sizeof(struct pcie_device
), GFP_KERNEL
);
253 memset(device
, 0, sizeof(struct pcie_device
));
254 pcie_device_init(parent
, device
, port_type
, service_type
, irq
,irq_mode
);
255 printk(KERN_DEBUG
"Allocate Port Service[%s]\n", device
->device
.bus_id
);
259 int pcie_port_device_probe(struct pci_dev
*dev
)
264 if (!(pos
= pci_find_capability(dev
, PCI_CAP_ID_EXP
)))
267 pci_read_config_word(dev
, pos
+ PCIE_CAPABILITIES_REG
, ®
);
268 type
= (reg
>> 4) & PORT_TYPE_MASK
;
269 if ( type
== PCIE_RC_PORT
|| type
== PCIE_SW_UPSTREAM_PORT
||
270 type
== PCIE_SW_DOWNSTREAM_PORT
)
276 int pcie_port_device_register(struct pci_dev
*dev
)
278 struct pcie_port_device_ext
*p_ext
;
279 int status
, type
, capabilities
, irq_mode
, i
;
280 int vectors
[PCIE_PORT_DEVICE_MAXSERVICES
];
283 /* Allocate port device extension */
284 if (!(p_ext
= kmalloc(sizeof(struct pcie_port_device_ext
), GFP_KERNEL
)))
287 pci_set_drvdata(dev
, p_ext
);
290 pci_read_config_word(dev
,
291 pci_find_capability(dev
, PCI_CAP_ID_EXP
) +
292 PCIE_CAPABILITIES_REG
, ®16
);
293 type
= (reg16
>> 4) & PORT_TYPE_MASK
;
295 /* Now get port services */
296 capabilities
= get_port_device_capability(dev
);
297 irq_mode
= assign_interrupt_mode(dev
, vectors
, capabilities
);
298 p_ext
->interrupt_mode
= irq_mode
;
300 /* Allocate child services if any */
301 for (i
= 0; i
< PCIE_PORT_DEVICE_MAXSERVICES
; i
++) {
302 struct pcie_device
*child
;
304 if (capabilities
& (1 << i
)) {
305 child
= alloc_pcie_device(
307 type
, /* port type */
308 i
, /* service type */
309 vectors
[i
], /* irq */
310 irq_mode
/* interrupt mode */);
312 status
= device_register(&child
->device
);
317 get_device(&child
->device
);
325 static int suspend_iter(struct device
*dev
, void *data
)
327 struct pcie_port_service_driver
*service_driver
;
328 pm_message_t state
= * (pm_message_t
*) data
;
330 if ((dev
->bus
== &pcie_port_bus_type
) &&
332 service_driver
= to_service_driver(dev
->driver
);
333 if (service_driver
->suspend
)
334 service_driver
->suspend(to_pcie_device(dev
), state
);
339 int pcie_port_device_suspend(struct pci_dev
*dev
, pm_message_t state
)
341 device_for_each_child(&dev
->dev
, &state
, suspend_iter
);
345 static int resume_iter(struct device
*dev
, void *data
)
347 struct pcie_port_service_driver
*service_driver
;
349 if ((dev
->bus
== &pcie_port_bus_type
) &&
351 service_driver
= to_service_driver(dev
->driver
);
352 if (service_driver
->resume
)
353 service_driver
->resume(to_pcie_device(dev
));
358 int pcie_port_device_resume(struct pci_dev
*dev
)
360 device_for_each_child(&dev
->dev
, NULL
, resume_iter
);
365 static int remove_iter(struct device
*dev
, void *data
)
367 struct pcie_port_service_driver
*service_driver
;
369 if (dev
->bus
== &pcie_port_bus_type
) {
371 service_driver
= to_service_driver(dev
->driver
);
372 if (service_driver
->remove
)
373 service_driver
->remove(to_pcie_device(dev
));
375 *(unsigned long*)data
= (unsigned long)dev
;
381 void pcie_port_device_remove(struct pci_dev
*dev
)
383 struct device
*device
;
384 unsigned long device_addr
;
385 int interrupt_mode
= PCIE_PORT_INTx_MODE
;
389 status
= device_for_each_child(&dev
->dev
, &device_addr
, remove_iter
);
391 device
= (struct device
*)device_addr
;
392 interrupt_mode
= (to_pcie_device(device
))->interrupt_mode
;
394 device_unregister(device
);
397 /* Switch to INTx by default if MSI enabled */
398 if (interrupt_mode
== PCIE_PORT_MSIX_MODE
)
399 pci_disable_msix(dev
);
400 else if (interrupt_mode
== PCIE_PORT_MSI_MODE
)
401 pci_disable_msi(dev
);
404 void pcie_port_bus_register(void)
406 bus_register(&pcie_port_bus_type
);
409 void pcie_port_bus_unregister(void)
411 bus_unregister(&pcie_port_bus_type
);
414 int pcie_port_service_register(struct pcie_port_service_driver
*new)
416 new->driver
.name
= (char *)new->name
;
417 new->driver
.bus
= &pcie_port_bus_type
;
418 new->driver
.probe
= pcie_port_probe_service
;
419 new->driver
.remove
= pcie_port_remove_service
;
420 new->driver
.shutdown
= pcie_port_shutdown_service
;
421 new->driver
.suspend
= pcie_port_suspend_service
;
422 new->driver
.resume
= pcie_port_resume_service
;
424 return driver_register(&new->driver
);
427 void pcie_port_service_unregister(struct pcie_port_service_driver
*new)
429 driver_unregister(&new->driver
);
432 EXPORT_SYMBOL(pcie_port_service_register
);
433 EXPORT_SYMBOL(pcie_port_service_unregister
);