linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / drivers / pci / pcie / portdrv_pci.c
1 /*
2  * File:        portdrv_pci.c
3  * Purpose:     PCI Express Port Bus Driver
4  *
5  * Copyright (C) 2004 Intel
6  * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
7  */
8
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/pm.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/pcieport_if.h>
17
18 #include "portdrv.h"
19
20 /*
21  * Version Information
22  */
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");
29
30 /* global data */
31 static const char device_name[] = "pcieport-driver";
32
33 static void pci_save_msi_state(struct pci_dev *dev)
34 {
35         struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
36         int i = 0, pos;
37         u16 control;
38
39         if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSI)) <= 0)
40                 return;
41
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++]);
51         } else
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++]);
57 }
58
59 static void pci_restore_msi_state(struct pci_dev *dev)
60 {
61         struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
62         int i = 0, pos;
63         u16 control;
64
65         if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSI)) <= 0)
66                 return;
67
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++]);
77         } else
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++]);
83 }
84
85 static void pcie_portdrv_save_config(struct pci_dev *dev)
86 {
87         struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
88
89         pci_save_state(dev);
90         if (p_ext->interrupt_mode == PCIE_PORT_MSI_MODE)
91                 pci_save_msi_state(dev);
92 }
93
94 static int pcie_portdrv_restore_config(struct pci_dev *dev)
95 {
96         struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
97         int retval;
98
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);
103         if (retval)
104                 return retval;
105         pci_set_master(dev);
106         return 0;
107 }
108
109 /*
110  * pcie_portdrv_probe - Probe PCI-Express port devices
111  * @dev: PCI-Express port device being probed
112  *
113  * If detected invokes the pcie_port_device_register() method for 
114  * this port device.
115  *
116  */
117 static int __devinit pcie_portdrv_probe (struct pci_dev *dev, 
118                                 const struct pci_device_id *id )
119 {
120         int                     status;
121
122         status = pcie_port_device_probe(dev);
123         if (status)
124                 return status;
125
126         if (pci_enable_device(dev) < 0) 
127                 return -ENODEV;
128         
129         pci_set_master(dev);
130         if (!dev->irq) {
131                 printk(KERN_WARNING 
132                 "%s->Dev[%04x:%04x] has invalid IRQ. Check vendor BIOS\n", 
133                 __FUNCTION__, dev->device, dev->vendor);
134         }
135         if (pcie_port_device_register(dev)) 
136                 return -ENOMEM;
137
138         return 0;
139 }
140
141 static void pcie_portdrv_remove (struct pci_dev *dev)
142 {
143         pcie_port_device_remove(dev);
144         kfree(pci_get_drvdata(dev));
145 }
146
147 #ifdef CONFIG_PM
148 static int pcie_portdrv_suspend (struct pci_dev *dev, pm_message_t state)
149 {
150         int ret = pcie_port_device_suspend(dev, state);
151
152         pcie_portdrv_save_config(dev);
153         return ret;
154 }
155
156 static int pcie_portdrv_resume (struct pci_dev *dev)
157 {
158         pcie_portdrv_restore_config(dev);
159         return pcie_port_device_resume(dev);
160 }
161 #endif
162
163 /*
164  * LINUX Device Driver Model
165  */
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 */ }
170 };
171 MODULE_DEVICE_TABLE(pci, port_pci_ids);
172
173 static struct pci_driver pcie_portdrv = {
174         .name           = (char *)device_name,
175         .id_table       = &port_pci_ids[0],
176
177         .probe          = pcie_portdrv_probe,
178         .remove         = pcie_portdrv_remove,
179
180 #ifdef  CONFIG_PM
181         .suspend        = pcie_portdrv_suspend,
182         .resume         = pcie_portdrv_resume,
183 #endif  /* PM */
184 };
185
186 static int __init pcie_portdrv_init(void)
187 {
188         int retval = 0;
189
190         pcie_port_bus_register();
191         retval = pci_register_driver(&pcie_portdrv);
192         if (retval)
193                 pcie_port_bus_unregister();
194         return retval;
195 }
196
197 static void __exit pcie_portdrv_exit(void) 
198 {
199         pci_unregister_driver(&pcie_portdrv);
200         pcie_port_bus_unregister();
201 }
202
203 module_init(pcie_portdrv_init);
204 module_exit(pcie_portdrv_exit);