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
) {}
66 static int pcie_port_suspend_service(struct device
*dev
, pm_message_t state
)
68 struct pcie_device
*pciedev
;
69 struct pcie_port_service_driver
*driver
;
71 if (!dev
|| !dev
->driver
)
74 pciedev
= to_pcie_device(dev
);
75 driver
= to_service_driver(dev
->driver
);
76 if (driver
&& driver
->suspend
)
77 driver
->suspend(pciedev
, state
);
81 static int pcie_port_resume_service(struct device
*dev
)
83 struct pcie_device
*pciedev
;
84 struct pcie_port_service_driver
*driver
;
86 if (!dev
|| !dev
->driver
)
89 pciedev
= to_pcie_device(dev
);
90 driver
= to_service_driver(dev
->driver
);
92 if (driver
&& driver
->resume
)
93 driver
->resume(pciedev
);
100 * Being invoked automatically when device is being removed
101 * in response to device_unregister(dev) call.
102 * Release all resources being claimed.
104 static void release_pcie_device(struct device
*dev
)
106 dev_printk(KERN_DEBUG
, dev
, "free port service\n");
107 kfree(to_pcie_device(dev
));
110 static int is_msi_quirked(struct pci_dev
*dev
)
112 int port_type
, quirk
= 0;
115 pci_read_config_word(dev
,
116 pci_find_capability(dev
, PCI_CAP_ID_EXP
) +
117 PCIE_CAPABILITIES_REG
, ®16
);
118 port_type
= (reg16
>> 4) & PORT_TYPE_MASK
;
121 if (pcie_mch_quirk
== 1)
124 case PCIE_SW_UPSTREAM_PORT
:
125 case PCIE_SW_DOWNSTREAM_PORT
:
132 static int assign_interrupt_mode(struct pci_dev
*dev
, int *vectors
, int mask
)
134 int i
, pos
, nvec
, status
= -EINVAL
;
135 int interrupt_mode
= PCIE_PORT_INTx_MODE
;
137 /* Set INTx as default */
138 for (i
= 0, nvec
= 0; i
< PCIE_PORT_DEVICE_MAXSERVICES
; i
++) {
141 vectors
[i
] = dev
->irq
;
144 /* Check MSI quirk */
145 if (is_msi_quirked(dev
))
146 return interrupt_mode
;
148 /* Select MSI-X over MSI if supported */
149 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
151 struct msix_entry msix_entries
[PCIE_PORT_DEVICE_MAXSERVICES
] =
152 {{0, 0}, {0, 1}, {0, 2}, {0, 3}};
153 dev_info(&dev
->dev
, "found MSI-X capability\n");
154 status
= pci_enable_msix(dev
, msix_entries
, nvec
);
158 interrupt_mode
= PCIE_PORT_MSIX_MODE
;
159 for (i
= 0; i
< PCIE_PORT_DEVICE_MAXSERVICES
; i
++) {
161 vectors
[i
] = msix_entries
[j
++].vector
;
166 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
168 dev_info(&dev
->dev
, "found MSI capability\n");
169 status
= pci_enable_msi(dev
);
171 interrupt_mode
= PCIE_PORT_MSI_MODE
;
172 for (i
= 0;i
< PCIE_PORT_DEVICE_MAXSERVICES
;i
++)
173 vectors
[i
] = dev
->irq
;
177 return interrupt_mode
;
180 static int get_port_device_capability(struct pci_dev
*dev
)
182 int services
= 0, pos
;
186 pos
= pci_find_capability(dev
, PCI_CAP_ID_EXP
);
187 pci_read_config_word(dev
, pos
+ PCIE_CAPABILITIES_REG
, ®16
);
188 /* Hot-Plug Capable */
189 if (reg16
& PORT_TO_SLOT_MASK
) {
190 pci_read_config_dword(dev
,
191 pos
+ PCIE_SLOT_CAPABILITIES_REG
, ®32
);
192 if (reg32
& SLOT_HP_CAPABLE_MASK
)
193 services
|= PCIE_PORT_SERVICE_HP
;
195 /* PME Capable - root port capability */
196 if (((reg16
>> 4) & PORT_TYPE_MASK
) == PCIE_RC_PORT
)
197 services
|= PCIE_PORT_SERVICE_PME
;
199 if (pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_ERR
))
200 services
|= PCIE_PORT_SERVICE_AER
;
201 if (pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_VC
))
202 services
|= PCIE_PORT_SERVICE_VC
;
207 static void pcie_device_init(struct pci_dev
*parent
, struct pcie_device
*dev
,
208 int port_type
, int service_type
, int irq
, int irq_mode
)
210 struct device
*device
;
213 dev
->interrupt_mode
= irq_mode
;
215 dev
->id
.vendor
= parent
->vendor
;
216 dev
->id
.device
= parent
->device
;
217 dev
->id
.port_type
= port_type
;
218 dev
->id
.service_type
= (1 << service_type
);
220 /* Initialize generic device interface */
221 device
= &dev
->device
;
222 memset(device
, 0, sizeof(struct device
));
223 device
->bus
= &pcie_port_bus_type
;
224 device
->driver
= NULL
;
225 device
->driver_data
= NULL
;
226 device
->release
= release_pcie_device
; /* callback to free pcie dev */
227 dev_set_name(device
, "%s:pcie%02x",
228 pci_name(parent
), get_descriptor_id(port_type
, service_type
));
229 device
->parent
= &parent
->dev
;
232 static struct pcie_device
* alloc_pcie_device(struct pci_dev
*parent
,
233 int port_type
, int service_type
, int irq
, int irq_mode
)
235 struct pcie_device
*device
;
237 device
= kzalloc(sizeof(struct pcie_device
), GFP_KERNEL
);
241 pcie_device_init(parent
, device
, port_type
, service_type
, irq
,irq_mode
);
242 dev_printk(KERN_DEBUG
, &device
->device
, "allocate port service\n");
246 int pcie_port_device_probe(struct pci_dev
*dev
)
251 if (!(pos
= pci_find_capability(dev
, PCI_CAP_ID_EXP
)))
254 pci_read_config_word(dev
, pos
+ PCIE_CAPABILITIES_REG
, ®
);
255 type
= (reg
>> 4) & PORT_TYPE_MASK
;
256 if ( type
== PCIE_RC_PORT
|| type
== PCIE_SW_UPSTREAM_PORT
||
257 type
== PCIE_SW_DOWNSTREAM_PORT
)
263 int pcie_port_device_register(struct pci_dev
*dev
)
265 struct pcie_port_device_ext
*p_ext
;
266 int status
, type
, capabilities
, irq_mode
, i
;
267 int vectors
[PCIE_PORT_DEVICE_MAXSERVICES
];
270 /* Allocate port device extension */
271 if (!(p_ext
= kmalloc(sizeof(struct pcie_port_device_ext
), GFP_KERNEL
)))
274 pci_set_drvdata(dev
, p_ext
);
277 pci_read_config_word(dev
,
278 pci_find_capability(dev
, PCI_CAP_ID_EXP
) +
279 PCIE_CAPABILITIES_REG
, ®16
);
280 type
= (reg16
>> 4) & PORT_TYPE_MASK
;
282 /* Now get port services */
283 capabilities
= get_port_device_capability(dev
);
284 irq_mode
= assign_interrupt_mode(dev
, vectors
, capabilities
);
285 p_ext
->interrupt_mode
= irq_mode
;
287 /* Allocate child services if any */
288 for (i
= 0; i
< PCIE_PORT_DEVICE_MAXSERVICES
; i
++) {
289 struct pcie_device
*child
;
291 if (capabilities
& (1 << i
)) {
292 child
= alloc_pcie_device(
294 type
, /* port type */
295 i
, /* service type */
296 vectors
[i
], /* irq */
297 irq_mode
/* interrupt mode */);
299 status
= device_register(&child
->device
);
304 get_device(&child
->device
);
312 static int suspend_iter(struct device
*dev
, void *data
)
314 struct pcie_port_service_driver
*service_driver
;
315 pm_message_t state
= * (pm_message_t
*) data
;
317 if ((dev
->bus
== &pcie_port_bus_type
) &&
319 service_driver
= to_service_driver(dev
->driver
);
320 if (service_driver
->suspend
)
321 service_driver
->suspend(to_pcie_device(dev
), state
);
326 int pcie_port_device_suspend(struct pci_dev
*dev
, pm_message_t state
)
328 return device_for_each_child(&dev
->dev
, &state
, suspend_iter
);
331 static int resume_iter(struct device
*dev
, void *data
)
333 struct pcie_port_service_driver
*service_driver
;
335 if ((dev
->bus
== &pcie_port_bus_type
) &&
337 service_driver
= to_service_driver(dev
->driver
);
338 if (service_driver
->resume
)
339 service_driver
->resume(to_pcie_device(dev
));
344 int pcie_port_device_resume(struct pci_dev
*dev
)
346 return device_for_each_child(&dev
->dev
, NULL
, resume_iter
);
350 static int remove_iter(struct device
*dev
, void *data
)
352 struct pcie_port_service_driver
*service_driver
;
354 if (dev
->bus
== &pcie_port_bus_type
) {
356 service_driver
= to_service_driver(dev
->driver
);
357 if (service_driver
->remove
)
358 service_driver
->remove(to_pcie_device(dev
));
360 *(unsigned long*)data
= (unsigned long)dev
;
366 void pcie_port_device_remove(struct pci_dev
*dev
)
368 struct device
*device
;
369 unsigned long device_addr
;
370 int interrupt_mode
= PCIE_PORT_INTx_MODE
;
374 status
= device_for_each_child(&dev
->dev
, &device_addr
, remove_iter
);
376 device
= (struct device
*)device_addr
;
377 interrupt_mode
= (to_pcie_device(device
))->interrupt_mode
;
379 device_unregister(device
);
382 /* Switch to INTx by default if MSI enabled */
383 if (interrupt_mode
== PCIE_PORT_MSIX_MODE
)
384 pci_disable_msix(dev
);
385 else if (interrupt_mode
== PCIE_PORT_MSI_MODE
)
386 pci_disable_msi(dev
);
389 int pcie_port_bus_register(void)
391 return bus_register(&pcie_port_bus_type
);
394 void pcie_port_bus_unregister(void)
396 bus_unregister(&pcie_port_bus_type
);
399 int pcie_port_service_register(struct pcie_port_service_driver
*new)
401 new->driver
.name
= (char *)new->name
;
402 new->driver
.bus
= &pcie_port_bus_type
;
403 new->driver
.probe
= pcie_port_probe_service
;
404 new->driver
.remove
= pcie_port_remove_service
;
405 new->driver
.shutdown
= pcie_port_shutdown_service
;
406 new->driver
.suspend
= pcie_port_suspend_service
;
407 new->driver
.resume
= pcie_port_resume_service
;
409 return driver_register(&new->driver
);
412 void pcie_port_service_unregister(struct pcie_port_service_driver
*new)
414 driver_unregister(&new->driver
);
417 EXPORT_SYMBOL(pcie_port_service_register
);
418 EXPORT_SYMBOL(pcie_port_service_unregister
);