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 INIT_LIST_HEAD(&device
->node
);
236 INIT_LIST_HEAD(&device
->children
);
237 INIT_LIST_HEAD(&device
->bus_list
);
238 device
->bus
= &pcie_port_bus_type
;
239 device
->driver
= NULL
;
240 device
->driver_data
= NULL
;
241 device
->release
= release_pcie_device
; /* callback to free pcie dev */
242 sprintf(&device
->bus_id
[0], "pcie%02x",
243 get_descriptor_id(port_type
, service_type
));
244 device
->parent
= &parent
->dev
;
247 static struct pcie_device
* alloc_pcie_device(struct pci_dev
*parent
,
248 int port_type
, int service_type
, int irq
, int irq_mode
)
250 struct pcie_device
*device
;
252 device
= kmalloc(sizeof(struct pcie_device
), GFP_KERNEL
);
256 memset(device
, 0, sizeof(struct pcie_device
));
257 pcie_device_init(parent
, device
, port_type
, service_type
, irq
,irq_mode
);
258 printk(KERN_DEBUG
"Allocate Port Service[%s]\n", device
->device
.bus_id
);
262 int pcie_port_device_probe(struct pci_dev
*dev
)
267 if (!(pos
= pci_find_capability(dev
, PCI_CAP_ID_EXP
)))
270 pci_read_config_word(dev
, pos
+ PCIE_CAPABILITIES_REG
, ®
);
271 type
= (reg
>> 4) & PORT_TYPE_MASK
;
272 if ( type
== PCIE_RC_PORT
|| type
== PCIE_SW_UPSTREAM_PORT
||
273 type
== PCIE_SW_DOWNSTREAM_PORT
)
279 int pcie_port_device_register(struct pci_dev
*dev
)
281 int status
, type
, capabilities
, irq_mode
, i
;
282 int vectors
[PCIE_PORT_DEVICE_MAXSERVICES
];
286 pci_read_config_word(dev
,
287 pci_find_capability(dev
, PCI_CAP_ID_EXP
) +
288 PCIE_CAPABILITIES_REG
, ®16
);
289 type
= (reg16
>> 4) & PORT_TYPE_MASK
;
291 /* Now get port services */
292 capabilities
= get_port_device_capability(dev
);
293 irq_mode
= assign_interrupt_mode(dev
, vectors
, capabilities
);
295 /* Allocate child services if any */
296 for (i
= 0; i
< PCIE_PORT_DEVICE_MAXSERVICES
; i
++) {
297 struct pcie_device
*child
;
299 if (capabilities
& (1 << i
)) {
300 child
= alloc_pcie_device(
302 type
, /* port type */
303 i
, /* service type */
304 vectors
[i
], /* irq */
305 irq_mode
/* interrupt mode */);
307 status
= device_register(&child
->device
);
312 get_device(&child
->device
);
320 int pcie_port_device_suspend(struct pci_dev
*dev
, pm_message_t state
)
322 struct list_head
*head
, *tmp
;
323 struct device
*parent
, *child
;
324 struct device_driver
*driver
;
325 struct pcie_port_service_driver
*service_driver
;
328 head
= &parent
->children
;
330 while (head
!= tmp
) {
331 child
= container_of(tmp
, struct device
, node
);
333 if (child
->bus
!= &pcie_port_bus_type
)
335 driver
= child
->driver
;
338 service_driver
= to_service_driver(driver
);
339 if (service_driver
->suspend
)
340 service_driver
->suspend(to_pcie_device(child
), state
);
345 int pcie_port_device_resume(struct pci_dev
*dev
)
347 struct list_head
*head
, *tmp
;
348 struct device
*parent
, *child
;
349 struct device_driver
*driver
;
350 struct pcie_port_service_driver
*service_driver
;
353 head
= &parent
->children
;
355 while (head
!= tmp
) {
356 child
= container_of(tmp
, struct device
, node
);
358 if (child
->bus
!= &pcie_port_bus_type
)
360 driver
= child
->driver
;
363 service_driver
= to_service_driver(driver
);
364 if (service_driver
->resume
)
365 service_driver
->resume(to_pcie_device(child
));
372 void pcie_port_device_remove(struct pci_dev
*dev
)
374 struct list_head
*head
, *tmp
;
375 struct device
*parent
, *child
;
376 struct device_driver
*driver
;
377 struct pcie_port_service_driver
*service_driver
;
378 int interrupt_mode
= PCIE_PORT_INTx_MODE
;
381 head
= &parent
->children
;
383 while (head
!= tmp
) {
384 child
= container_of(tmp
, struct device
, node
);
386 if (child
->bus
!= &pcie_port_bus_type
)
388 driver
= child
->driver
;
390 service_driver
= to_service_driver(driver
);
391 if (service_driver
->remove
)
392 service_driver
->remove(to_pcie_device(child
));
394 interrupt_mode
= (to_pcie_device(child
))->interrupt_mode
;
396 device_unregister(child
);
398 /* Switch to INTx by default if MSI enabled */
399 if (interrupt_mode
== PCIE_PORT_MSIX_MODE
)
400 pci_disable_msix(dev
);
401 else if (interrupt_mode
== PCIE_PORT_MSI_MODE
)
402 pci_disable_msi(dev
);
405 void pcie_port_bus_register(void)
407 bus_register(&pcie_port_bus_type
);
410 void pcie_port_bus_unregister(void)
412 bus_unregister(&pcie_port_bus_type
);
415 int pcie_port_service_register(struct pcie_port_service_driver
*new)
417 new->driver
.name
= (char *)new->name
;
418 new->driver
.bus
= &pcie_port_bus_type
;
419 new->driver
.probe
= pcie_port_probe_service
;
420 new->driver
.remove
= pcie_port_remove_service
;
421 new->driver
.shutdown
= pcie_port_shutdown_service
;
422 new->driver
.suspend
= pcie_port_suspend_service
;
423 new->driver
.resume
= pcie_port_resume_service
;
425 return driver_register(&new->driver
);
428 void pcie_port_service_unregister(struct pcie_port_service_driver
*new)
430 driver_unregister(&new->driver
);
433 EXPORT_SYMBOL(pcie_port_service_register
);
434 EXPORT_SYMBOL(pcie_port_service_unregister
);