Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 1 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #include <linux/pci.h> |
| 3 | #include <linux/module.h> |
Al Viro | f6a5703 | 2006-10-18 01:47:25 -0400 | [diff] [blame] | 4 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 5 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/ioport.h> |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 7 | #include <linux/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
Adrian Bunk | 48b1914 | 2005-11-06 01:45:08 +0100 | [diff] [blame] | 9 | #include "pci.h" |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | /* |
| 12 | * This interrupt-safe spinlock protects all accesses to PCI |
| 13 | * configuration space. |
| 14 | */ |
| 15 | |
Jan Kiszka | a2e2778 | 2011-11-04 09:46:00 +0100 | [diff] [blame] | 16 | DEFINE_RAW_SPINLOCK(pci_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * Wrappers for all PCI configuration access functions. They just check |
| 20 | * alignment, do locking and call the low-level functions pointed to |
| 21 | * by pci_dev->ops. |
| 22 | */ |
| 23 | |
| 24 | #define PCI_byte_BAD 0 |
| 25 | #define PCI_word_BAD (pos & 1) |
| 26 | #define PCI_dword_BAD (pos & 3) |
| 27 | |
| 28 | #define PCI_OP_READ(size,type,len) \ |
| 29 | int pci_bus_read_config_##size \ |
| 30 | (struct pci_bus *bus, unsigned int devfn, int pos, type *value) \ |
| 31 | { \ |
| 32 | int res; \ |
| 33 | unsigned long flags; \ |
| 34 | u32 data = 0; \ |
| 35 | if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \ |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 36 | raw_spin_lock_irqsave(&pci_lock, flags); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | res = bus->ops->read(bus, devfn, pos, len, &data); \ |
| 38 | *value = (type)data; \ |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 39 | raw_spin_unlock_irqrestore(&pci_lock, flags); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | return res; \ |
| 41 | } |
| 42 | |
| 43 | #define PCI_OP_WRITE(size,type,len) \ |
| 44 | int pci_bus_write_config_##size \ |
| 45 | (struct pci_bus *bus, unsigned int devfn, int pos, type value) \ |
| 46 | { \ |
| 47 | int res; \ |
| 48 | unsigned long flags; \ |
| 49 | if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \ |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 50 | raw_spin_lock_irqsave(&pci_lock, flags); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | res = bus->ops->write(bus, devfn, pos, len, value); \ |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 52 | raw_spin_unlock_irqrestore(&pci_lock, flags); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | return res; \ |
| 54 | } |
| 55 | |
| 56 | PCI_OP_READ(byte, u8, 1) |
| 57 | PCI_OP_READ(word, u16, 2) |
| 58 | PCI_OP_READ(dword, u32, 4) |
| 59 | PCI_OP_WRITE(byte, u8, 1) |
| 60 | PCI_OP_WRITE(word, u16, 2) |
| 61 | PCI_OP_WRITE(dword, u32, 4) |
| 62 | |
| 63 | EXPORT_SYMBOL(pci_bus_read_config_byte); |
| 64 | EXPORT_SYMBOL(pci_bus_read_config_word); |
| 65 | EXPORT_SYMBOL(pci_bus_read_config_dword); |
| 66 | EXPORT_SYMBOL(pci_bus_write_config_byte); |
| 67 | EXPORT_SYMBOL(pci_bus_write_config_word); |
| 68 | EXPORT_SYMBOL(pci_bus_write_config_dword); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 69 | |
Rob Herring | 1f94a94 | 2015-01-09 20:34:39 -0600 | [diff] [blame] | 70 | int pci_generic_config_read(struct pci_bus *bus, unsigned int devfn, |
| 71 | int where, int size, u32 *val) |
| 72 | { |
| 73 | void __iomem *addr; |
| 74 | |
| 75 | addr = bus->ops->map_bus(bus, devfn, where); |
| 76 | if (!addr) { |
| 77 | *val = ~0; |
| 78 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 79 | } |
| 80 | |
| 81 | if (size == 1) |
| 82 | *val = readb(addr); |
| 83 | else if (size == 2) |
| 84 | *val = readw(addr); |
| 85 | else |
| 86 | *val = readl(addr); |
| 87 | |
| 88 | return PCIBIOS_SUCCESSFUL; |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(pci_generic_config_read); |
| 91 | |
| 92 | int pci_generic_config_write(struct pci_bus *bus, unsigned int devfn, |
| 93 | int where, int size, u32 val) |
| 94 | { |
| 95 | void __iomem *addr; |
| 96 | |
| 97 | addr = bus->ops->map_bus(bus, devfn, where); |
| 98 | if (!addr) |
| 99 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 100 | |
| 101 | if (size == 1) |
| 102 | writeb(val, addr); |
| 103 | else if (size == 2) |
| 104 | writew(val, addr); |
| 105 | else |
| 106 | writel(val, addr); |
| 107 | |
| 108 | return PCIBIOS_SUCCESSFUL; |
| 109 | } |
| 110 | EXPORT_SYMBOL_GPL(pci_generic_config_write); |
| 111 | |
| 112 | int pci_generic_config_read32(struct pci_bus *bus, unsigned int devfn, |
| 113 | int where, int size, u32 *val) |
| 114 | { |
| 115 | void __iomem *addr; |
| 116 | |
| 117 | addr = bus->ops->map_bus(bus, devfn, where & ~0x3); |
| 118 | if (!addr) { |
| 119 | *val = ~0; |
| 120 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 121 | } |
| 122 | |
| 123 | *val = readl(addr); |
| 124 | |
| 125 | if (size <= 2) |
| 126 | *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1); |
| 127 | |
| 128 | return PCIBIOS_SUCCESSFUL; |
| 129 | } |
| 130 | EXPORT_SYMBOL_GPL(pci_generic_config_read32); |
| 131 | |
| 132 | int pci_generic_config_write32(struct pci_bus *bus, unsigned int devfn, |
| 133 | int where, int size, u32 val) |
| 134 | { |
| 135 | void __iomem *addr; |
| 136 | u32 mask, tmp; |
| 137 | |
| 138 | addr = bus->ops->map_bus(bus, devfn, where & ~0x3); |
| 139 | if (!addr) |
| 140 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 141 | |
| 142 | if (size == 4) { |
| 143 | writel(val, addr); |
| 144 | return PCIBIOS_SUCCESSFUL; |
| 145 | } else { |
| 146 | mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8)); |
| 147 | } |
| 148 | |
| 149 | tmp = readl(addr) & mask; |
| 150 | tmp |= val << ((where & 0x3) * 8); |
| 151 | writel(tmp, addr); |
| 152 | |
| 153 | return PCIBIOS_SUCCESSFUL; |
| 154 | } |
| 155 | EXPORT_SYMBOL_GPL(pci_generic_config_write32); |
| 156 | |
Huang Ying | a72b46c | 2009-04-24 10:45:17 +0800 | [diff] [blame] | 157 | /** |
| 158 | * pci_bus_set_ops - Set raw operations of pci bus |
| 159 | * @bus: pci bus struct |
| 160 | * @ops: new raw operations |
| 161 | * |
| 162 | * Return previous raw operations |
| 163 | */ |
| 164 | struct pci_ops *pci_bus_set_ops(struct pci_bus *bus, struct pci_ops *ops) |
| 165 | { |
| 166 | struct pci_ops *old_ops; |
| 167 | unsigned long flags; |
| 168 | |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 169 | raw_spin_lock_irqsave(&pci_lock, flags); |
Huang Ying | a72b46c | 2009-04-24 10:45:17 +0800 | [diff] [blame] | 170 | old_ops = bus->ops; |
| 171 | bus->ops = ops; |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 172 | raw_spin_unlock_irqrestore(&pci_lock, flags); |
Huang Ying | a72b46c | 2009-04-24 10:45:17 +0800 | [diff] [blame] | 173 | return old_ops; |
| 174 | } |
| 175 | EXPORT_SYMBOL(pci_bus_set_ops); |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * pci_read_vpd - Read one entry from Vital Product Data |
| 179 | * @dev: pci device struct |
| 180 | * @pos: offset in vpd space |
| 181 | * @count: number of bytes to read |
| 182 | * @buf: pointer to where to store result |
| 183 | * |
| 184 | */ |
| 185 | ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf) |
| 186 | { |
| 187 | if (!dev->vpd || !dev->vpd->ops) |
| 188 | return -ENODEV; |
| 189 | return dev->vpd->ops->read(dev, pos, count, buf); |
| 190 | } |
| 191 | EXPORT_SYMBOL(pci_read_vpd); |
| 192 | |
| 193 | /** |
| 194 | * pci_write_vpd - Write entry to Vital Product Data |
| 195 | * @dev: pci device struct |
| 196 | * @pos: offset in vpd space |
Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 197 | * @count: number of bytes to write |
| 198 | * @buf: buffer containing write data |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 199 | * |
| 200 | */ |
| 201 | ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf) |
| 202 | { |
| 203 | if (!dev->vpd || !dev->vpd->ops) |
| 204 | return -ENODEV; |
| 205 | return dev->vpd->ops->write(dev, pos, count, buf); |
| 206 | } |
| 207 | EXPORT_SYMBOL(pci_write_vpd); |
| 208 | |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 209 | /* |
| 210 | * The following routines are to prevent the user from accessing PCI config |
| 211 | * space when it's unsafe to do so. Some devices require this during BIST and |
| 212 | * we're required to prevent it during D-state transitions. |
| 213 | * |
| 214 | * We have a bit per device to indicate it's blocked and a global wait queue |
| 215 | * for callers to sleep on until devices are unblocked. |
| 216 | */ |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 217 | static DECLARE_WAIT_QUEUE_HEAD(pci_cfg_wait); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 218 | |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 219 | static noinline void pci_wait_cfg(struct pci_dev *dev) |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 220 | { |
| 221 | DECLARE_WAITQUEUE(wait, current); |
| 222 | |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 223 | __add_wait_queue(&pci_cfg_wait, &wait); |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 224 | do { |
| 225 | set_current_state(TASK_UNINTERRUPTIBLE); |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 226 | raw_spin_unlock_irq(&pci_lock); |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 227 | schedule(); |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 228 | raw_spin_lock_irq(&pci_lock); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 229 | } while (dev->block_cfg_access); |
| 230 | __remove_wait_queue(&pci_cfg_wait, &wait); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Greg Thelen | 34e3207 | 2011-04-17 08:20:32 -0700 | [diff] [blame] | 233 | /* Returns 0 on success, negative values indicate error. */ |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 234 | #define PCI_USER_READ_CONFIG(size,type) \ |
| 235 | int pci_user_read_config_##size \ |
| 236 | (struct pci_dev *dev, int pos, type *val) \ |
| 237 | { \ |
Gavin Shan | d97ffe2 | 2014-05-21 15:23:30 +1000 | [diff] [blame] | 238 | int ret = PCIBIOS_SUCCESSFUL; \ |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 239 | u32 data = -1; \ |
Greg Thelen | 34e3207 | 2011-04-17 08:20:32 -0700 | [diff] [blame] | 240 | if (PCI_##size##_BAD) \ |
| 241 | return -EINVAL; \ |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 242 | raw_spin_lock_irq(&pci_lock); \ |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 243 | if (unlikely(dev->block_cfg_access)) \ |
| 244 | pci_wait_cfg(dev); \ |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 245 | ret = dev->bus->ops->read(dev->bus, dev->devfn, \ |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 246 | pos, sizeof(type), &data); \ |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 247 | raw_spin_unlock_irq(&pci_lock); \ |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 248 | *val = (type)data; \ |
Gavin Shan | d97ffe2 | 2014-05-21 15:23:30 +1000 | [diff] [blame] | 249 | return pcibios_err_to_errno(ret); \ |
Alex Williamson | c63587d | 2012-06-11 05:27:19 +0000 | [diff] [blame] | 250 | } \ |
| 251 | EXPORT_SYMBOL_GPL(pci_user_read_config_##size); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 252 | |
Greg Thelen | 34e3207 | 2011-04-17 08:20:32 -0700 | [diff] [blame] | 253 | /* Returns 0 on success, negative values indicate error. */ |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 254 | #define PCI_USER_WRITE_CONFIG(size,type) \ |
| 255 | int pci_user_write_config_##size \ |
| 256 | (struct pci_dev *dev, int pos, type val) \ |
| 257 | { \ |
Gavin Shan | d97ffe2 | 2014-05-21 15:23:30 +1000 | [diff] [blame] | 258 | int ret = PCIBIOS_SUCCESSFUL; \ |
Greg Thelen | 34e3207 | 2011-04-17 08:20:32 -0700 | [diff] [blame] | 259 | if (PCI_##size##_BAD) \ |
| 260 | return -EINVAL; \ |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 261 | raw_spin_lock_irq(&pci_lock); \ |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 262 | if (unlikely(dev->block_cfg_access)) \ |
| 263 | pci_wait_cfg(dev); \ |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 264 | ret = dev->bus->ops->write(dev->bus, dev->devfn, \ |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 265 | pos, sizeof(type), val); \ |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 266 | raw_spin_unlock_irq(&pci_lock); \ |
Gavin Shan | d97ffe2 | 2014-05-21 15:23:30 +1000 | [diff] [blame] | 267 | return pcibios_err_to_errno(ret); \ |
Alex Williamson | c63587d | 2012-06-11 05:27:19 +0000 | [diff] [blame] | 268 | } \ |
| 269 | EXPORT_SYMBOL_GPL(pci_user_write_config_##size); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 270 | |
| 271 | PCI_USER_READ_CONFIG(byte, u8) |
| 272 | PCI_USER_READ_CONFIG(word, u16) |
| 273 | PCI_USER_READ_CONFIG(dword, u32) |
| 274 | PCI_USER_WRITE_CONFIG(byte, u8) |
| 275 | PCI_USER_WRITE_CONFIG(word, u16) |
| 276 | PCI_USER_WRITE_CONFIG(dword, u32) |
| 277 | |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 278 | /* VPD access through PCI 2.2+ VPD capability */ |
| 279 | |
| 280 | #define PCI_VPD_PCI22_SIZE (PCI_VPD_ADDR_MASK + 1) |
| 281 | |
| 282 | struct pci_vpd_pci22 { |
| 283 | struct pci_vpd base; |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 284 | struct mutex lock; |
| 285 | u16 flag; |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 286 | bool busy; |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 287 | u8 cap; |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 290 | /* |
| 291 | * Wait for last operation to complete. |
| 292 | * This code has to spin since there is no other notification from the PCI |
| 293 | * hardware. Since the VPD is often implemented by serial attachment to an |
| 294 | * EEPROM, it may take many milliseconds to complete. |
Greg Thelen | 34e3207 | 2011-04-17 08:20:32 -0700 | [diff] [blame] | 295 | * |
| 296 | * Returns 0 on success, negative values indicate error. |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 297 | */ |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 298 | static int pci_vpd_pci22_wait(struct pci_dev *dev) |
| 299 | { |
| 300 | struct pci_vpd_pci22 *vpd = |
| 301 | container_of(dev->vpd, struct pci_vpd_pci22, base); |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 302 | unsigned long timeout = jiffies + HZ/20 + 2; |
| 303 | u16 status; |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 304 | int ret; |
| 305 | |
| 306 | if (!vpd->busy) |
| 307 | return 0; |
| 308 | |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 309 | for (;;) { |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 310 | ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR, |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 311 | &status); |
Greg Thelen | 34e3207 | 2011-04-17 08:20:32 -0700 | [diff] [blame] | 312 | if (ret < 0) |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 313 | return ret; |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 314 | |
| 315 | if ((status & PCI_VPD_ADDR_F) == vpd->flag) { |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 316 | vpd->busy = false; |
| 317 | return 0; |
| 318 | } |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 319 | |
Prarit Bhargava | 5030718 | 2010-05-17 14:25:14 -0400 | [diff] [blame] | 320 | if (time_after(jiffies, timeout)) { |
Ryan Desfosses | 227f064 | 2014-04-18 20:13:50 -0400 | [diff] [blame] | 321 | dev_printk(KERN_DEBUG, &dev->dev, "vpd r/w failed. This is likely a firmware bug on this device. Contact the card vendor for a firmware update\n"); |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 322 | return -ETIMEDOUT; |
Prarit Bhargava | 5030718 | 2010-05-17 14:25:14 -0400 | [diff] [blame] | 323 | } |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 324 | if (fatal_signal_pending(current)) |
| 325 | return -EINTR; |
| 326 | if (!cond_resched()) |
| 327 | udelay(10); |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 331 | static ssize_t pci_vpd_pci22_read(struct pci_dev *dev, loff_t pos, size_t count, |
| 332 | void *arg) |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 333 | { |
| 334 | struct pci_vpd_pci22 *vpd = |
| 335 | container_of(dev->vpd, struct pci_vpd_pci22, base); |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 336 | int ret; |
| 337 | loff_t end = pos + count; |
| 338 | u8 *buf = arg; |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 339 | |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 340 | if (pos < 0 || pos > vpd->base.len || end > vpd->base.len) |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 341 | return -EINVAL; |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 342 | |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 343 | if (mutex_lock_killable(&vpd->lock)) |
| 344 | return -EINTR; |
| 345 | |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 346 | ret = pci_vpd_pci22_wait(dev); |
| 347 | if (ret < 0) |
| 348 | goto out; |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 349 | |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 350 | while (pos < end) { |
| 351 | u32 val; |
| 352 | unsigned int i, skip; |
| 353 | |
| 354 | ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR, |
| 355 | pos & ~3); |
| 356 | if (ret < 0) |
| 357 | break; |
| 358 | vpd->busy = true; |
| 359 | vpd->flag = PCI_VPD_ADDR_F; |
| 360 | ret = pci_vpd_pci22_wait(dev); |
| 361 | if (ret < 0) |
| 362 | break; |
| 363 | |
| 364 | ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val); |
| 365 | if (ret < 0) |
| 366 | break; |
| 367 | |
| 368 | skip = pos & 3; |
| 369 | for (i = 0; i < sizeof(u32); i++) { |
| 370 | if (i >= skip) { |
| 371 | *buf++ = val; |
| 372 | if (++pos == end) |
| 373 | break; |
| 374 | } |
| 375 | val >>= 8; |
| 376 | } |
| 377 | } |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 378 | out: |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 379 | mutex_unlock(&vpd->lock); |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 380 | return ret ? ret : count; |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 383 | static ssize_t pci_vpd_pci22_write(struct pci_dev *dev, loff_t pos, size_t count, |
| 384 | const void *arg) |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 385 | { |
| 386 | struct pci_vpd_pci22 *vpd = |
| 387 | container_of(dev->vpd, struct pci_vpd_pci22, base); |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 388 | const u8 *buf = arg; |
| 389 | loff_t end = pos + count; |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 390 | int ret = 0; |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 391 | |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 392 | if (pos < 0 || (pos & 3) || (count & 3) || end > vpd->base.len) |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 393 | return -EINVAL; |
| 394 | |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 395 | if (mutex_lock_killable(&vpd->lock)) |
| 396 | return -EINTR; |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 397 | |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 398 | ret = pci_vpd_pci22_wait(dev); |
| 399 | if (ret < 0) |
| 400 | goto out; |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 401 | |
| 402 | while (pos < end) { |
| 403 | u32 val; |
| 404 | |
| 405 | val = *buf++; |
| 406 | val |= *buf++ << 8; |
| 407 | val |= *buf++ << 16; |
| 408 | val |= *buf++ << 24; |
| 409 | |
| 410 | ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val); |
| 411 | if (ret < 0) |
| 412 | break; |
| 413 | ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR, |
| 414 | pos | PCI_VPD_ADDR_F); |
| 415 | if (ret < 0) |
| 416 | break; |
| 417 | |
| 418 | vpd->busy = true; |
| 419 | vpd->flag = 0; |
| 420 | ret = pci_vpd_pci22_wait(dev); |
Greg Thelen | d97ecd8 | 2011-04-17 08:22:21 -0700 | [diff] [blame] | 421 | if (ret < 0) |
| 422 | break; |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 423 | |
| 424 | pos += sizeof(u32); |
| 425 | } |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 426 | out: |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 427 | mutex_unlock(&vpd->lock); |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 428 | return ret ? ret : count; |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 431 | static void pci_vpd_pci22_release(struct pci_dev *dev) |
| 432 | { |
| 433 | kfree(container_of(dev->vpd, struct pci_vpd_pci22, base)); |
| 434 | } |
| 435 | |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 436 | static const struct pci_vpd_ops pci_vpd_pci22_ops = { |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 437 | .read = pci_vpd_pci22_read, |
| 438 | .write = pci_vpd_pci22_write, |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 439 | .release = pci_vpd_pci22_release, |
| 440 | }; |
| 441 | |
Mark Rustad | 932c435 | 2015-07-13 11:40:02 -0700 | [diff] [blame] | 442 | static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count, |
| 443 | void *arg) |
| 444 | { |
| 445 | struct pci_dev *tdev = pci_get_slot(dev->bus, PCI_SLOT(dev->devfn)); |
| 446 | ssize_t ret; |
| 447 | |
| 448 | if (!tdev) |
| 449 | return -ENODEV; |
| 450 | |
| 451 | ret = pci_read_vpd(tdev, pos, count, arg); |
| 452 | pci_dev_put(tdev); |
| 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count, |
| 457 | const void *arg) |
| 458 | { |
| 459 | struct pci_dev *tdev = pci_get_slot(dev->bus, PCI_SLOT(dev->devfn)); |
| 460 | ssize_t ret; |
| 461 | |
| 462 | if (!tdev) |
| 463 | return -ENODEV; |
| 464 | |
| 465 | ret = pci_write_vpd(tdev, pos, count, arg); |
| 466 | pci_dev_put(tdev); |
| 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | static const struct pci_vpd_ops pci_vpd_f0_ops = { |
| 471 | .read = pci_vpd_f0_read, |
| 472 | .write = pci_vpd_f0_write, |
| 473 | .release = pci_vpd_pci22_release, |
| 474 | }; |
| 475 | |
| 476 | static int pci_vpd_f0_dev_check(struct pci_dev *dev) |
| 477 | { |
| 478 | struct pci_dev *tdev = pci_get_slot(dev->bus, PCI_SLOT(dev->devfn)); |
| 479 | int ret = 0; |
| 480 | |
| 481 | if (!tdev) |
| 482 | return -ENODEV; |
| 483 | if (!tdev->vpd || !tdev->multifunction || |
| 484 | dev->class != tdev->class || dev->vendor != tdev->vendor || |
| 485 | dev->device != tdev->device) |
| 486 | ret = -ENODEV; |
| 487 | |
| 488 | pci_dev_put(tdev); |
| 489 | return ret; |
| 490 | } |
| 491 | |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 492 | int pci_vpd_pci22_init(struct pci_dev *dev) |
| 493 | { |
| 494 | struct pci_vpd_pci22 *vpd; |
| 495 | u8 cap; |
| 496 | |
| 497 | cap = pci_find_capability(dev, PCI_CAP_ID_VPD); |
| 498 | if (!cap) |
| 499 | return -ENODEV; |
Mark Rustad | 932c435 | 2015-07-13 11:40:02 -0700 | [diff] [blame] | 500 | if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0) { |
| 501 | int ret = pci_vpd_f0_dev_check(dev); |
| 502 | |
| 503 | if (ret) |
| 504 | return ret; |
| 505 | } |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 506 | vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC); |
| 507 | if (!vpd) |
| 508 | return -ENOMEM; |
| 509 | |
Benjamin Li | 99cb233d | 2008-07-02 10:59:04 -0700 | [diff] [blame] | 510 | vpd->base.len = PCI_VPD_PCI22_SIZE; |
Mark Rustad | 932c435 | 2015-07-13 11:40:02 -0700 | [diff] [blame] | 511 | if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0) |
| 512 | vpd->base.ops = &pci_vpd_f0_ops; |
| 513 | else |
| 514 | vpd->base.ops = &pci_vpd_pci22_ops; |
Stephen Hemminger | 1120f8b | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 515 | mutex_init(&vpd->lock); |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 516 | vpd->cap = cap; |
| 517 | vpd->busy = false; |
| 518 | dev->vpd = &vpd->base; |
| 519 | return 0; |
| 520 | } |
| 521 | |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 522 | /** |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 523 | * pci_cfg_access_lock - Lock PCI config reads/writes |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 524 | * @dev: pci device struct |
| 525 | * |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 526 | * When access is locked, any userspace reads or writes to config |
| 527 | * space and concurrent lock requests will sleep until access is |
| 528 | * allowed via pci_cfg_access_unlocked again. |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 529 | */ |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 530 | void pci_cfg_access_lock(struct pci_dev *dev) |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 531 | { |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 532 | might_sleep(); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 533 | |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 534 | raw_spin_lock_irq(&pci_lock); |
| 535 | if (dev->block_cfg_access) |
| 536 | pci_wait_cfg(dev); |
| 537 | dev->block_cfg_access = 1; |
| 538 | raw_spin_unlock_irq(&pci_lock); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 539 | } |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 540 | EXPORT_SYMBOL_GPL(pci_cfg_access_lock); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 541 | |
| 542 | /** |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 543 | * pci_cfg_access_trylock - try to lock PCI config reads/writes |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 544 | * @dev: pci device struct |
| 545 | * |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 546 | * Same as pci_cfg_access_lock, but will return 0 if access is |
| 547 | * already locked, 1 otherwise. This function can be used from |
| 548 | * atomic contexts. |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 549 | */ |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 550 | bool pci_cfg_access_trylock(struct pci_dev *dev) |
| 551 | { |
| 552 | unsigned long flags; |
| 553 | bool locked = true; |
| 554 | |
| 555 | raw_spin_lock_irqsave(&pci_lock, flags); |
| 556 | if (dev->block_cfg_access) |
| 557 | locked = false; |
| 558 | else |
| 559 | dev->block_cfg_access = 1; |
| 560 | raw_spin_unlock_irqrestore(&pci_lock, flags); |
| 561 | |
| 562 | return locked; |
| 563 | } |
| 564 | EXPORT_SYMBOL_GPL(pci_cfg_access_trylock); |
| 565 | |
| 566 | /** |
| 567 | * pci_cfg_access_unlock - Unlock PCI config reads/writes |
| 568 | * @dev: pci device struct |
| 569 | * |
| 570 | * This function allows PCI config accesses to resume. |
| 571 | */ |
| 572 | void pci_cfg_access_unlock(struct pci_dev *dev) |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 573 | { |
| 574 | unsigned long flags; |
| 575 | |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 576 | raw_spin_lock_irqsave(&pci_lock, flags); |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 577 | |
| 578 | /* This indicates a problem in the caller, but we don't need |
| 579 | * to kill them, unlike a double-block above. */ |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 580 | WARN_ON(!dev->block_cfg_access); |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 581 | |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 582 | dev->block_cfg_access = 0; |
| 583 | wake_up_all(&pci_cfg_wait); |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 584 | raw_spin_unlock_irqrestore(&pci_lock, flags); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 585 | } |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 586 | EXPORT_SYMBOL_GPL(pci_cfg_access_unlock); |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 587 | |
| 588 | static inline int pcie_cap_version(const struct pci_dev *dev) |
| 589 | { |
Myron Stowe | 1c531d8 | 2013-01-25 17:55:45 -0700 | [diff] [blame] | 590 | return pcie_caps_reg(dev) & PCI_EXP_FLAGS_VERS; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 591 | } |
| 592 | |
Bjorn Helgaas | ffb4d60 | 2015-06-24 16:05:54 -0500 | [diff] [blame] | 593 | static bool pcie_downstream_port(const struct pci_dev *dev) |
| 594 | { |
| 595 | int type = pci_pcie_type(dev); |
| 596 | |
| 597 | return type == PCI_EXP_TYPE_ROOT_PORT || |
| 598 | type == PCI_EXP_TYPE_DOWNSTREAM; |
| 599 | } |
| 600 | |
Yinghai Lu | 7a1562d | 2014-11-11 12:09:46 -0800 | [diff] [blame] | 601 | bool pcie_cap_has_lnkctl(const struct pci_dev *dev) |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 602 | { |
| 603 | int type = pci_pcie_type(dev); |
| 604 | |
Bjorn Helgaas | c8b303d | 2013-08-28 11:33:53 -0600 | [diff] [blame] | 605 | return type == PCI_EXP_TYPE_ENDPOINT || |
Bjorn Helgaas | d3694d4 | 2013-08-27 09:54:40 -0600 | [diff] [blame] | 606 | type == PCI_EXP_TYPE_LEG_END || |
| 607 | type == PCI_EXP_TYPE_ROOT_PORT || |
| 608 | type == PCI_EXP_TYPE_UPSTREAM || |
| 609 | type == PCI_EXP_TYPE_DOWNSTREAM || |
| 610 | type == PCI_EXP_TYPE_PCI_BRIDGE || |
| 611 | type == PCI_EXP_TYPE_PCIE_BRIDGE; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev) |
| 615 | { |
Bjorn Helgaas | ffb4d60 | 2015-06-24 16:05:54 -0500 | [diff] [blame] | 616 | return pcie_downstream_port(dev) && |
Bjorn Helgaas | 6d3a174 | 2013-08-28 12:01:03 -0600 | [diff] [blame] | 617 | pcie_caps_reg(dev) & PCI_EXP_FLAGS_SLOT; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static inline bool pcie_cap_has_rtctl(const struct pci_dev *dev) |
| 621 | { |
| 622 | int type = pci_pcie_type(dev); |
| 623 | |
Bjorn Helgaas | c8b303d | 2013-08-28 11:33:53 -0600 | [diff] [blame] | 624 | return type == PCI_EXP_TYPE_ROOT_PORT || |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 625 | type == PCI_EXP_TYPE_RC_EC; |
| 626 | } |
| 627 | |
| 628 | static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos) |
| 629 | { |
| 630 | if (!pci_is_pcie(dev)) |
| 631 | return false; |
| 632 | |
| 633 | switch (pos) { |
Alex Williamson | 969daa3 | 2013-02-14 11:35:42 -0700 | [diff] [blame] | 634 | case PCI_EXP_FLAGS: |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 635 | return true; |
| 636 | case PCI_EXP_DEVCAP: |
| 637 | case PCI_EXP_DEVCTL: |
| 638 | case PCI_EXP_DEVSTA: |
Bjorn Helgaas | fed2451 | 2013-08-28 12:03:42 -0600 | [diff] [blame] | 639 | return true; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 640 | case PCI_EXP_LNKCAP: |
| 641 | case PCI_EXP_LNKCTL: |
| 642 | case PCI_EXP_LNKSTA: |
| 643 | return pcie_cap_has_lnkctl(dev); |
| 644 | case PCI_EXP_SLTCAP: |
| 645 | case PCI_EXP_SLTCTL: |
| 646 | case PCI_EXP_SLTSTA: |
| 647 | return pcie_cap_has_sltctl(dev); |
| 648 | case PCI_EXP_RTCTL: |
| 649 | case PCI_EXP_RTCAP: |
| 650 | case PCI_EXP_RTSTA: |
| 651 | return pcie_cap_has_rtctl(dev); |
| 652 | case PCI_EXP_DEVCAP2: |
| 653 | case PCI_EXP_DEVCTL2: |
| 654 | case PCI_EXP_LNKCAP2: |
| 655 | case PCI_EXP_LNKCTL2: |
| 656 | case PCI_EXP_LNKSTA2: |
| 657 | return pcie_cap_version(dev) > 1; |
| 658 | default: |
| 659 | return false; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Note that these accessor functions are only for the "PCI Express |
| 665 | * Capability" (see PCIe spec r3.0, sec 7.8). They do not apply to the |
| 666 | * other "PCI Express Extended Capabilities" (AER, VC, ACS, MFVC, etc.) |
| 667 | */ |
| 668 | int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val) |
| 669 | { |
| 670 | int ret; |
| 671 | |
| 672 | *val = 0; |
| 673 | if (pos & 1) |
| 674 | return -EINVAL; |
| 675 | |
| 676 | if (pcie_capability_reg_implemented(dev, pos)) { |
| 677 | ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val); |
| 678 | /* |
| 679 | * Reset *val to 0 if pci_read_config_word() fails, it may |
| 680 | * have been written as 0xFFFF if hardware error happens |
| 681 | * during pci_read_config_word(). |
| 682 | */ |
| 683 | if (ret) |
| 684 | *val = 0; |
| 685 | return ret; |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * For Functions that do not implement the Slot Capabilities, |
| 690 | * Slot Status, and Slot Control registers, these spaces must |
| 691 | * be hardwired to 0b, with the exception of the Presence Detect |
| 692 | * State bit in the Slot Status register of Downstream Ports, |
| 693 | * which must be hardwired to 1b. (PCIe Base Spec 3.0, sec 7.8) |
| 694 | */ |
Bjorn Helgaas | ffb4d60 | 2015-06-24 16:05:54 -0500 | [diff] [blame] | 695 | if (pci_is_pcie(dev) && pcie_downstream_port(dev) && |
| 696 | pos == PCI_EXP_SLTSTA) |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 697 | *val = PCI_EXP_SLTSTA_PDS; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | EXPORT_SYMBOL(pcie_capability_read_word); |
| 702 | |
| 703 | int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val) |
| 704 | { |
| 705 | int ret; |
| 706 | |
| 707 | *val = 0; |
| 708 | if (pos & 3) |
| 709 | return -EINVAL; |
| 710 | |
| 711 | if (pcie_capability_reg_implemented(dev, pos)) { |
| 712 | ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val); |
| 713 | /* |
| 714 | * Reset *val to 0 if pci_read_config_dword() fails, it may |
| 715 | * have been written as 0xFFFFFFFF if hardware error happens |
| 716 | * during pci_read_config_dword(). |
| 717 | */ |
| 718 | if (ret) |
| 719 | *val = 0; |
| 720 | return ret; |
| 721 | } |
| 722 | |
Bjorn Helgaas | ffb4d60 | 2015-06-24 16:05:54 -0500 | [diff] [blame] | 723 | if (pci_is_pcie(dev) && pcie_downstream_port(dev) && |
| 724 | pos == PCI_EXP_SLTSTA) |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 725 | *val = PCI_EXP_SLTSTA_PDS; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 726 | |
| 727 | return 0; |
| 728 | } |
| 729 | EXPORT_SYMBOL(pcie_capability_read_dword); |
| 730 | |
| 731 | int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val) |
| 732 | { |
| 733 | if (pos & 1) |
| 734 | return -EINVAL; |
| 735 | |
| 736 | if (!pcie_capability_reg_implemented(dev, pos)) |
| 737 | return 0; |
| 738 | |
| 739 | return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val); |
| 740 | } |
| 741 | EXPORT_SYMBOL(pcie_capability_write_word); |
| 742 | |
| 743 | int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val) |
| 744 | { |
| 745 | if (pos & 3) |
| 746 | return -EINVAL; |
| 747 | |
| 748 | if (!pcie_capability_reg_implemented(dev, pos)) |
| 749 | return 0; |
| 750 | |
| 751 | return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val); |
| 752 | } |
| 753 | EXPORT_SYMBOL(pcie_capability_write_dword); |
| 754 | |
| 755 | int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos, |
| 756 | u16 clear, u16 set) |
| 757 | { |
| 758 | int ret; |
| 759 | u16 val; |
| 760 | |
| 761 | ret = pcie_capability_read_word(dev, pos, &val); |
| 762 | if (!ret) { |
| 763 | val &= ~clear; |
| 764 | val |= set; |
| 765 | ret = pcie_capability_write_word(dev, pos, val); |
| 766 | } |
| 767 | |
| 768 | return ret; |
| 769 | } |
| 770 | EXPORT_SYMBOL(pcie_capability_clear_and_set_word); |
| 771 | |
| 772 | int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos, |
| 773 | u32 clear, u32 set) |
| 774 | { |
| 775 | int ret; |
| 776 | u32 val; |
| 777 | |
| 778 | ret = pcie_capability_read_dword(dev, pos, &val); |
| 779 | if (!ret) { |
| 780 | val &= ~clear; |
| 781 | val |= set; |
| 782 | ret = pcie_capability_write_dword(dev, pos, val); |
| 783 | } |
| 784 | |
| 785 | return ret; |
| 786 | } |
| 787 | EXPORT_SYMBOL(pcie_capability_clear_and_set_dword); |