3 * Purpose: PCI Express Port Bus Driver
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/init.h>
15 #include <linux/slab.h>
16 #include <linux/pcieport_if.h>
23 #define DRIVER_VERSION "v1.0"
24 #define DRIVER_AUTHOR "tom.l.nguyen@intel.com"
25 #define DRIVER_DESC "PCIE Port Bus Driver"
26 MODULE_AUTHOR(DRIVER_AUTHOR
);
27 MODULE_DESCRIPTION(DRIVER_DESC
);
28 MODULE_LICENSE("GPL");
31 static const char device_name
[] = "pcieport-driver";
33 static void pci_save_msi_state(struct pci_dev
*dev
)
35 struct pcie_port_device_ext
*p_ext
= pci_get_drvdata(dev
);
39 if ((pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
)) <= 0)
42 pci_read_config_dword(dev
, pos
, &p_ext
->saved_msi_config_space
[i
++]);
43 control
= p_ext
->saved_msi_config_space
[0] >> 16;
44 pci_read_config_dword(dev
, pos
+ PCI_MSI_ADDRESS_LO
,
45 &p_ext
->saved_msi_config_space
[i
++]);
46 if (control
& PCI_MSI_FLAGS_64BIT
) {
47 pci_read_config_dword(dev
, pos
+ PCI_MSI_ADDRESS_HI
,
48 &p_ext
->saved_msi_config_space
[i
++]);
49 pci_read_config_dword(dev
, pos
+ PCI_MSI_DATA_64
,
50 &p_ext
->saved_msi_config_space
[i
++]);
52 pci_read_config_dword(dev
, pos
+ PCI_MSI_DATA_32
,
53 &p_ext
->saved_msi_config_space
[i
++]);
54 if (control
& PCI_MSI_FLAGS_MASKBIT
)
55 pci_read_config_dword(dev
, pos
+ PCI_MSI_MASK_BIT
,
56 &p_ext
->saved_msi_config_space
[i
++]);
59 static void pci_restore_msi_state(struct pci_dev
*dev
)
61 struct pcie_port_device_ext
*p_ext
= pci_get_drvdata(dev
);
65 if ((pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
)) <= 0)
68 control
= p_ext
->saved_msi_config_space
[i
++] >> 16;
69 pci_write_config_word(dev
, pos
+ PCI_MSI_FLAGS
, control
);
70 pci_write_config_dword(dev
, pos
+ PCI_MSI_ADDRESS_LO
,
71 p_ext
->saved_msi_config_space
[i
++]);
72 if (control
& PCI_MSI_FLAGS_64BIT
) {
73 pci_write_config_dword(dev
, pos
+ PCI_MSI_ADDRESS_HI
,
74 p_ext
->saved_msi_config_space
[i
++]);
75 pci_write_config_dword(dev
, pos
+ PCI_MSI_DATA_64
,
76 p_ext
->saved_msi_config_space
[i
++]);
78 pci_write_config_dword(dev
, pos
+ PCI_MSI_DATA_32
,
79 p_ext
->saved_msi_config_space
[i
++]);
80 if (control
& PCI_MSI_FLAGS_MASKBIT
)
81 pci_write_config_dword(dev
, pos
+ PCI_MSI_MASK_BIT
,
82 p_ext
->saved_msi_config_space
[i
++]);
85 static void pcie_portdrv_save_config(struct pci_dev
*dev
)
87 struct pcie_port_device_ext
*p_ext
= pci_get_drvdata(dev
);
90 if (p_ext
->interrupt_mode
== PCIE_PORT_MSI_MODE
)
91 pci_save_msi_state(dev
);
94 static int pcie_portdrv_restore_config(struct pci_dev
*dev
)
96 struct pcie_port_device_ext
*p_ext
= pci_get_drvdata(dev
);
99 pci_restore_state(dev
);
100 if (p_ext
->interrupt_mode
== PCIE_PORT_MSI_MODE
)
101 pci_restore_msi_state(dev
);
102 retval
= pci_enable_device(dev
);
110 * pcie_portdrv_probe - Probe PCI-Express port devices
111 * @dev: PCI-Express port device being probed
113 * If detected invokes the pcie_port_device_register() method for
117 static int __devinit
pcie_portdrv_probe (struct pci_dev
*dev
,
118 const struct pci_device_id
*id
)
122 status
= pcie_port_device_probe(dev
);
126 if (pci_enable_device(dev
) < 0)
132 "%s->Dev[%04x:%04x] has invalid IRQ. Check vendor BIOS\n",
133 __FUNCTION__
, dev
->device
, dev
->vendor
);
135 if (pcie_port_device_register(dev
))
141 static void pcie_portdrv_remove (struct pci_dev
*dev
)
143 pcie_port_device_remove(dev
);
144 kfree(pci_get_drvdata(dev
));
148 static int pcie_portdrv_suspend (struct pci_dev
*dev
, pm_message_t state
)
150 int ret
= pcie_port_device_suspend(dev
, state
);
152 pcie_portdrv_save_config(dev
);
156 static int pcie_portdrv_resume (struct pci_dev
*dev
)
158 pcie_portdrv_restore_config(dev
);
159 return pcie_port_device_resume(dev
);
164 * LINUX Device Driver Model
166 static const struct pci_device_id port_pci_ids
[] = { {
167 /* handle any PCI-Express port */
168 PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI
<< 8) | 0x00), ~0),
169 }, { /* end: all zeroes */ }
171 MODULE_DEVICE_TABLE(pci
, port_pci_ids
);
173 static struct pci_driver pcie_portdrv
= {
174 .name
= (char *)device_name
,
175 .id_table
= &port_pci_ids
[0],
177 .probe
= pcie_portdrv_probe
,
178 .remove
= pcie_portdrv_remove
,
181 .suspend
= pcie_portdrv_suspend
,
182 .resume
= pcie_portdrv_resume
,
186 static int __init
pcie_portdrv_init(void)
190 pcie_port_bus_register();
191 retval
= pci_register_driver(&pcie_portdrv
);
193 pcie_port_bus_unregister();
197 static void __exit
pcie_portdrv_exit(void)
199 pci_unregister_driver(&pcie_portdrv
);
200 pcie_port_bus_unregister();
203 module_init(pcie_portdrv_init
);
204 module_exit(pcie_portdrv_exit
);