blob: 02260141dc8107be19d943f4e35475b65559cf30 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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"
26MODULE_AUTHOR(DRIVER_AUTHOR);
27MODULE_DESCRIPTION(DRIVER_DESC);
28MODULE_LICENSE("GPL");
29
30/* global data */
31static const char device_name[] = "pcieport-driver";
32
long5823d102005-06-22 09:09:54 -070033static 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
59static 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
85static 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
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -070094static int pcie_portdrv_restore_config(struct pci_dev *dev)
long5823d102005-06-22 09:09:54 -070095{
96 struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -070097 int retval;
long5823d102005-06-22 09:09:54 -070098
99 pci_restore_state(dev);
100 if (p_ext->interrupt_mode == PCIE_PORT_MSI_MODE)
101 pci_restore_msi_state(dev);
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -0700102 retval = pci_enable_device(dev);
103 if (retval)
104 return retval;
long5823d102005-06-22 09:09:54 -0700105 pci_set_master(dev);
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -0700106 return 0;
long5823d102005-06-22 09:09:54 -0700107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
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 */
117static 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
141static void pcie_portdrv_remove (struct pci_dev *dev)
142{
143 pcie_port_device_remove(dev);
long5823d102005-06-22 09:09:54 -0700144 kfree(pci_get_drvdata(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147#ifdef CONFIG_PM
Pavel Machek7f4927c2005-04-16 15:25:33 -0700148static int pcie_portdrv_suspend (struct pci_dev *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
long5823d102005-06-22 09:09:54 -0700150 int ret = pcie_port_device_suspend(dev, state);
151
152 pcie_portdrv_save_config(dev);
153 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156static int pcie_portdrv_resume (struct pci_dev *dev)
157{
long5823d102005-06-22 09:09:54 -0700158 pcie_portdrv_restore_config(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return pcie_port_device_resume(dev);
160}
161#endif
162
163/*
164 * LINUX Device Driver Model
165 */
166static 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};
171MODULE_DEVICE_TABLE(pci, port_pci_ids);
172
173static 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
186static 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
197static void __exit pcie_portdrv_exit(void)
198{
199 pci_unregister_driver(&pcie_portdrv);
200 pcie_port_bus_unregister();
201}
202
203module_init(pcie_portdrv_init);
204module_exit(pcie_portdrv_exit);