rdma.m4: Update pci_bus_speed/width test
[~emulex/for-vlad/old/compat.git] / compat / compat-3.12.c
1 #include <linux/debugfs.h>
2 #include <linux/fs.h>
3 #include <linux/export.h>
4 #include <linux/pci.h>
5 #include <linux/pci_hotplug.h>
6
7 #define debugfs_create_atomic_t LINUX_BACKPORT(debugfs_create_atomic_t)
8
9 static int debugfs_atomic_t_set(void *data, u64 val)
10 {
11         atomic_set((atomic_t *)data, val);
12         return 0;
13 }
14 static int debugfs_atomic_t_get(void *data, u64 *val)
15 {
16         *val = atomic_read((atomic_t *)data);
17         return 0;
18 }
19 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
20                         debugfs_atomic_t_set, "%lld\n");
21 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n");
22 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n");
23
24 /**
25  * debugfs_create_atomic_t - create a debugfs file that is used to read and
26  * write an atomic_t value
27  * @name: a pointer to a string containing the name of the file to create.
28  * @mode: the permission that the file should have
29  * @parent: a pointer to the parent dentry for this file.  This should be a
30  *          directory dentry if set.  If this parameter is %NULL, then the
31  *          file will be created in the root of the debugfs filesystem.
32  * @value: a pointer to the variable that the file should read to and write
33  *         from.
34  */
35 struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
36                                  struct dentry *parent, atomic_t *value)
37 {
38         /* if there are no write bits set, make read only */
39         if (!(mode & S_IWUGO))
40                 return debugfs_create_file(name, mode, parent, value,
41                                         &fops_atomic_t_ro);
42         /* if there are no read bits set, make write only */
43         if (!(mode & S_IRUGO))
44                 return debugfs_create_file(name, mode, parent, value,
45                                         &fops_atomic_t_wo);
46
47         return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
48 }
49 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
50
51 const unsigned char pcie_link_speed[] = {
52         PCI_SPEED_UNKNOWN,              /* 0 */
53         PCIE_SPEED_2_5GT,               /* 1 */
54         PCIE_SPEED_5_0GT,               /* 2 */
55         PCIE_SPEED_8_0GT,               /* 3 */
56         PCI_SPEED_UNKNOWN,              /* 4 */
57         PCI_SPEED_UNKNOWN,              /* 5 */
58         PCI_SPEED_UNKNOWN,              /* 6 */
59         PCI_SPEED_UNKNOWN,              /* 7 */
60         PCI_SPEED_UNKNOWN,              /* 8 */
61         PCI_SPEED_UNKNOWN,              /* 9 */
62         PCI_SPEED_UNKNOWN,              /* A */
63         PCI_SPEED_UNKNOWN,              /* B */
64         PCI_SPEED_UNKNOWN,              /* C */
65         PCI_SPEED_UNKNOWN,              /* D */
66         PCI_SPEED_UNKNOWN,              /* E */
67         PCI_SPEED_UNKNOWN               /* F */
68 };
69
70 /**
71  * pcie_get_minimum_link - determine minimum link settings of a PCI device
72  * @dev: PCI device to query
73  * @speed: storage for minimum speed
74  * @width: storage for minimum width
75  *
76  * This function will walk up the PCI device chain and determine the minimum
77  * link width and speed of the device.
78  */
79 #define pcie_get_minimum_link LINUX_BACKPORT(pcie_get_minimum_link)
80 int pcie_get_minimum_link(struct pci_dev *dev, enum pci_bus_speed *speed,
81                           enum pcie_link_width *width)
82 {
83         int ret;
84
85         *speed = PCI_SPEED_UNKNOWN;
86         *width = PCIE_LNK_WIDTH_UNKNOWN;
87
88         while (dev) {
89                 u16 lnksta;
90                 enum pci_bus_speed next_speed;
91                 enum pcie_link_width next_width;
92
93                 ret = pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
94                 if (ret)
95                         return ret;
96
97                 next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
98                 next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
99                         PCI_EXP_LNKSTA_NLW_SHIFT;
100
101                 if (next_speed < *speed)
102                         *speed = next_speed;
103
104                 if (next_width < *width)
105                         *width = next_width;
106
107                 dev = dev->bus->self;
108         }
109
110         return 0;
111 }
112 EXPORT_SYMBOL(pcie_get_minimum_link);